File: tsk.c

package info (click to toggle)
libguestfs 1%3A1.44.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,932 kB
  • sloc: ansic: 458,017; ml: 51,424; sh: 13,191; java: 9,578; makefile: 7,931; cs: 6,328; haskell: 5,674; python: 3,871; perl: 3,528; erlang: 2,446; xml: 1,347; ruby: 350; pascal: 257; javascript: 157; lex: 135; yacc: 128; cpp: 10
file content (351 lines) | stat: -rw-r--r-- 9,852 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* libguestfs - the guestfsd daemon
 * Copyright (C) 2016 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <rpc/types.h>
#include <rpc/xdr.h>

#include "guestfs_protocol.h"
#include "daemon.h"
#include "actions.h"
#include "optgroups.h"

#ifdef HAVE_LIBTSK

#include <tsk/libtsk.h>

enum tsk_dirent_flags {
  DIRENT_UNALLOC = 0x00,
  DIRENT_ALLOC = 0x01,
  DIRENT_REALLOC = 0x02,
  DIRENT_COMPRESSED = 0x04
};

static int open_filesystem (const char *, TSK_IMG_INFO **, TSK_FS_INFO **);
static TSK_WALK_RET_ENUM fswalk_callback (TSK_FS_FILE *, const char *, void *);
static TSK_WALK_RET_ENUM findino_callback (TSK_FS_FILE *, const char *, void *);
static int send_dirent_info (TSK_FS_FILE *, const char *);
static char file_type (TSK_FS_FILE *);
static int file_flags (TSK_FS_FILE *fsfile);
static void file_metadata (TSK_FS_META *, guestfs_int_tsk_dirent *);
static void reply_with_tsk_error (const char *);
static int entry_is_dot (TSK_FS_FILE *);

int
do_internal_filesystem_walk (const mountable_t *mountable)
{
  int ret = -1;
  TSK_FS_INFO *fs = NULL;
  TSK_IMG_INFO *img = NULL;  /* Used internally by tsk_fs_dir_walk */
  const int flags =
    TSK_FS_DIR_WALK_FLAG_ALLOC | TSK_FS_DIR_WALK_FLAG_UNALLOC |
    TSK_FS_DIR_WALK_FLAG_RECURSE | TSK_FS_DIR_WALK_FLAG_NOORPHAN;

  ret = open_filesystem (mountable->device, &img, &fs);
  if (ret < 0)
    return ret;

  reply (NULL, NULL);  /* Reply message. */

  ret = tsk_fs_dir_walk (fs, fs->root_inum, flags, fswalk_callback, NULL);
  if (ret == 0)
    ret = send_file_end (0);  /* File transfer end. */
  else
    send_file_end (1);  /* Cancel file transfer. */

  fs->close (fs);
  img->close (img);

  return ret;
}

int
do_internal_find_inode (const mountable_t *mountable, int64_t inode)
{
  int ret = -1;
  TSK_FS_INFO *fs = NULL;
  TSK_IMG_INFO *img = NULL;  /* Used internally by tsk_fs_dir_walk */
  const int flags =
    TSK_FS_DIR_WALK_FLAG_ALLOC | TSK_FS_DIR_WALK_FLAG_UNALLOC |
    TSK_FS_DIR_WALK_FLAG_RECURSE | TSK_FS_DIR_WALK_FLAG_NOORPHAN;

  ret = open_filesystem (mountable->device, &img, &fs);
  if (ret < 0)
    return ret;

  reply (NULL, NULL);  /* Reply message. */

  ret = tsk_fs_dir_walk (fs, fs->root_inum, flags,
                         findino_callback, (void *) &inode);
  if (ret == 0)
    ret = send_file_end (0);  /* File transfer end. */
  else
    send_file_end (1);  /* Cancel file transfer. */

  fs->close (fs);
  img->close (img);

  return ret;
}

/* Inspect the device and initialises the img and fs structures.
 * Return 0 on success, -1 on error.
 */
static int
open_filesystem (const char *device, TSK_IMG_INFO **img, TSK_FS_INFO **fs)
{
  const char *images[] = { device };

  *img = tsk_img_open (1, images, TSK_IMG_TYPE_DETECT, 0);
  if (*img == NULL) {
    reply_with_tsk_error ("tsk_image_open");
    return -1;
  }

  *fs = tsk_fs_open_img (*img, 0, TSK_FS_TYPE_DETECT);
  if (*fs == NULL) {
    reply_with_tsk_error ("tsk_fs_open_img");
    (*img)->close (*img);
    return -1;
  }

  return 0;
}

/* Filesystem walk callback, it gets called on every FS node.
 * Parse the node, encode it into an XDR structure and send it to the library.
 * Return TSK_WALK_CONT on success, TSK_WALK_ERROR on error.
 */
static TSK_WALK_RET_ENUM
fswalk_callback (TSK_FS_FILE *fsfile, const char *path, void *data)
{
  int ret = 0;

  if (entry_is_dot (fsfile))
    return TSK_WALK_CONT;

  ret = send_dirent_info (fsfile, path);

  return (ret == 0) ? TSK_WALK_CONT : TSK_WALK_ERROR;
}

/* Find inode, it gets called on every FS node.
 * If the FS node address is the given one, parse it,
 * encode it into an XDR structure and send it to the library.
 * Return TSK_WALK_CONT on success, TSK_WALK_ERROR on error.
 */
static TSK_WALK_RET_ENUM
findino_callback (TSK_FS_FILE *fsfile, const char *path, void *data)
{
  int ret = 0;
  uint64_t *inode = (uint64_t *) data;

  if (*inode != fsfile->name->meta_addr)
    return TSK_WALK_CONT;

  if (entry_is_dot (fsfile))
    return TSK_WALK_CONT;

  ret = send_dirent_info (fsfile, path);

  return (ret == 0) ? TSK_WALK_CONT : TSK_WALK_ERROR;
}

/* Extract the information from the entry, serialize and send it out.
 * Return 0 on success, -1 on error.
 */
static int
send_dirent_info (TSK_FS_FILE *fsfile, const char *path)
{
  XDR xdr;
  int ret = 0;
  size_t len = 0;
  struct guestfs_int_tsk_dirent dirent;
  CLEANUP_FREE char *buf = NULL, *fname = NULL;

  /* Set dirent fields */
  memset (&dirent, 0, sizeof dirent);

  /* Build the full relative path of the entry */
  ret = asprintf (&fname, "%s%s", path, fsfile->name->name);
  if (ret < 0) {
    perror ("asprintf");
    return -1;
  }

  dirent.tsk_inode = fsfile->name->meta_addr;
  dirent.tsk_type = file_type (fsfile);
  dirent.tsk_name = fname;
  dirent.tsk_flags = file_flags (fsfile);

  file_metadata (fsfile->meta, &dirent);

  /* Serialize tsk_dirent struct. */
  buf = malloc (GUESTFS_MAX_CHUNK_SIZE);
  if (buf == NULL) {
    perror ("malloc");
    return -1;
  }

  xdrmem_create (&xdr, buf, GUESTFS_MAX_CHUNK_SIZE, XDR_ENCODE);

  ret = xdr_guestfs_int_tsk_dirent (&xdr, &dirent);
  if (ret == 0) {
    perror ("xdr_guestfs_int_tsk_dirent");
    return -1;
  }

  len = xdr_getpos (&xdr);

  xdr_destroy (&xdr);

  /* Send serialised tsk_dirent out. */
  return send_file_write (buf, len);
}

/* Inspect fsfile to identify its type. */
static char
file_type (TSK_FS_FILE *fsfile)
{
  if (fsfile->name->type < TSK_FS_NAME_TYPE_STR_MAX)
    switch (fsfile->name->type) {
    case TSK_FS_NAME_TYPE_UNDEF: return 'u';
    case TSK_FS_NAME_TYPE_FIFO: return 'f';
    case TSK_FS_NAME_TYPE_CHR: return 'c';
    case TSK_FS_NAME_TYPE_DIR: return 'd';
    case TSK_FS_NAME_TYPE_BLK: return 'b';
    case TSK_FS_NAME_TYPE_REG: return 'r';
    case TSK_FS_NAME_TYPE_LNK: return 'l';
    case TSK_FS_NAME_TYPE_SOCK: return 's';
    case TSK_FS_NAME_TYPE_SHAD: return 'h';
    case TSK_FS_NAME_TYPE_WHT: return 'w';
    case TSK_FS_NAME_TYPE_VIRT: return 'u';  /* Temp files created by TSK */
#if TSK_VERSION_NUM >= 0x040500ff
    case TSK_FS_NAME_TYPE_VIRT_DIR: return 'u';  /* Temp files created by TSK */
#endif
    }
  else if (fsfile->meta != NULL &&
           fsfile->meta->type < TSK_FS_META_TYPE_STR_MAX)
    switch (fsfile->name->type) {
    case TSK_FS_NAME_TYPE_UNDEF: return 'u';
    case TSK_FS_META_TYPE_REG: return 'r';
    case TSK_FS_META_TYPE_DIR: return 'd';
    case TSK_FS_META_TYPE_FIFO: return 'f';
    case TSK_FS_META_TYPE_CHR: return 'c';
    case TSK_FS_META_TYPE_BLK: return 'b';
    case TSK_FS_META_TYPE_LNK: return 'l';
    case TSK_FS_META_TYPE_SHAD: return 'h';
    case TSK_FS_META_TYPE_SOCK: return 's';
    case TSK_FS_META_TYPE_WHT: return 'w';
    case TSK_FS_META_TYPE_VIRT: return 'u';  /* Temp files created by TSK */
#if TSK_VERSION_NUM >= 0x040500ff
    case TSK_FS_META_TYPE_VIRT_DIR: return 'u';  /* Temp files created by TSK */
#endif
    }

  return 'u';
}

/* Inspect fsfile to retrieve file flags. */
static int
file_flags (TSK_FS_FILE *fsfile)
{
  int flags = DIRENT_UNALLOC;

  if (fsfile->name->flags & TSK_FS_NAME_FLAG_UNALLOC) {
    if (fsfile->meta && fsfile->meta->flags & TSK_FS_META_FLAG_ALLOC)
      flags |= DIRENT_REALLOC;
  }
  else
    flags |= DIRENT_ALLOC;

  if (fsfile->meta && fsfile->meta->flags & TSK_FS_META_FLAG_COMP)
    flags |= DIRENT_COMPRESSED;

  return flags;
}

/* Inspect fsfile to retrieve file metadata. */
static void
file_metadata (TSK_FS_META *fsmeta, guestfs_int_tsk_dirent *dirent)
{
  if (fsmeta != NULL) {
    dirent->tsk_size = fsmeta->size;
    dirent->tsk_nlink = fsmeta->nlink;
    dirent->tsk_atime_sec = fsmeta->atime;
    dirent->tsk_atime_nsec = fsmeta->atime_nano;
    dirent->tsk_mtime_sec = fsmeta->mtime;
    dirent->tsk_mtime_nsec = fsmeta->mtime_nano;
    dirent->tsk_ctime_sec = fsmeta->ctime;
    dirent->tsk_ctime_nsec = fsmeta->ctime_nano;
    dirent->tsk_crtime_sec = fsmeta->crtime;
    dirent->tsk_crtime_nsec = fsmeta->crtime_nano;
    /* tsk_link never changes */
    dirent->tsk_link = (fsmeta->link != NULL) ? fsmeta->link : (char *) "";
  }
  else {
    dirent->tsk_size = -1;
    /* tsk_link never changes */
    dirent->tsk_link = (char *) "";
  }
}

/* Parse TSK error and send it to the appliance. */
static void
reply_with_tsk_error (const char *funcname)
{
  int ret = 0;
  const char *buf = NULL;

  ret = tsk_error_get_errno ();
  if (ret != 0) {
    buf = tsk_error_get ();
    reply_with_error ("%s: %s", funcname, buf);
  }
  else
    reply_with_error ("%s: unknown error", funcname);
}

/* Check whether the entry is dot and is not Root.
 * Return 1 if it is dot, 0 otherwise or if it is the Root entry.
 */
static int
entry_is_dot (TSK_FS_FILE *fsfile)
{
  return (TSK_FS_ISDOT (fsfile->name->name) &&
          !(fsfile->fs_info->root_inum == fsfile->name->meta_addr &&  /* Root */
            STREQ (fsfile->name->name, ".")));  /* Avoid 'bin/..' 'etc/..' */
}

int
optgroup_libtsk_available (void)
{
  return 1;
}

#else   /* !HAVE_LIBTSK */

OPTGROUP_LIBTSK_NOT_AVAILABLE

#endif  /* !HAVE_LIBTSK */