File: sleuthkit.c

package info (click to toggle)
libguestfs 1%3A1.34.6-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 112,152 kB
  • ctags: 48,429
  • sloc: ansic: 438,854; ml: 49,329; sh: 15,718; java: 9,326; perl: 8,954; makefile: 7,318; cs: 6,153; haskell: 5,531; python: 3,161; erlang: 2,386; xml: 1,739; ruby: 350; pascal: 248; lex: 134; yacc: 128; cpp: 10
file content (159 lines) | stat: -rw-r--r-- 3,805 bytes parent folder | download
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
/* 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 "guestfs_protocol.h"
#include "daemon.h"
#include "actions.h"
#include "optgroups.h"

static int send_command_output (const char *cmd);

GUESTFSD_EXT_CMD(str_icat, icat);
GUESTFSD_EXT_CMD(str_blkls, blkls);

int
do_download_inode (const mountable_t *mountable, int64_t inode)
{
  int ret;
  CLEANUP_FREE char *cmd = NULL;

  /* Inode must be greater than 0 */
  if (inode < 0) {
    reply_with_error ("inode must be >= 0");
    return -1;
  }

  /* Construct the command. */
  ret = asprintf (&cmd, "%s -r %s %" PRIi64,
                  str_icat, mountable->device, inode);
  if (ret < 0) {
    reply_with_perror ("asprintf");
    return -1;
  }

  return send_command_output (cmd);
}

/* Takes optional arguments, consult optargs_bitmask. */
int
do_download_blocks (const mountable_t *mountable, int64_t start, int64_t stop,
                    int unallocated)
{
  int ret;
  const char *params;
  CLEANUP_FREE char *cmd = NULL;

  /* Data unit address start must be greater than 0 */
  if (start < 0) {
    reply_with_error ("starting address must be greater than zero");
    return -1;
  }

  /* Data unit address end must be greater than start */
  if (stop <= start) {
    reply_with_error ("stopping address must greater than starting address");
    return -1;
  }

  if (!(optargs_bitmask & GUESTFS_DOWNLOAD_BLOCKS_UNALLOCATED_BITMASK))
    params = " -e";
  else
    params = "";

  /* Construct the command. */
  ret = asprintf (&cmd, "%s %s %s %" PRIi64 "-%" PRIi64,
                  str_blkls, mountable->device, params, start, stop);
  if (ret < 0) {
    reply_with_perror ("asprintf");
    return -1;
  }

  return send_command_output (cmd);
}

/* Run the given command, collect the output and send it to the appliance.
 * Return 0 on success, -1 on error.
 */
static int
send_command_output (const char *cmd)
{
  int ret;
  FILE *fp;
  CLEANUP_FREE char *buffer = NULL;

  if (verbose)
    fprintf (stderr, "%s\n", cmd);

  buffer = malloc (GUESTFS_MAX_CHUNK_SIZE);
  if (buffer == NULL) {
    reply_with_perror ("malloc");
    return -1;
  }

  fp = popen (cmd, "r");
  if (fp == NULL) {
    reply_with_perror ("%s", cmd);
    return -1;
  }

  /* Send reply message before the file content. */
  reply (NULL, NULL);

  while ((ret = fread (buffer, 1, GUESTFS_MAX_CHUNK_SIZE, fp)) > 0) {
    ret = send_file_write (buffer, ret);
    if (ret < 0) {
      pclose (fp);
      return -1;
    }
  }

  ret = ferror (fp);
  if (ret != 0) {
    fprintf (stderr, "fread: %m");
    send_file_end (1);		/* Cancel. */
    pclose (fp);
    return -1;
  }

  ret = pclose (fp);
  if (ret != 0) {
    fprintf (stderr, "pclose: %m");
    send_file_end (1);		/* Cancel. */
    return -1;
  }

  ret = send_file_end (0);      /* Normal end of file. */
  if (ret != 0)
    return -1;

  return 0;
}

int
optgroup_sleuthkit_available (void)
{
  return prog_exists (str_icat);
}