touch: Restrict touch to regular files only (RHBZ#582484).
[libguestfs.git] / daemon / debug.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <dirent.h>
29
30 #include "../src/guestfs_protocol.h"
31 #include "daemon.h"
32 #include "actions.h"
33
34 /* This command exposes debugging information, internals and
35  * status.  There is no comprehensive documentation for this
36  * command.  You have to look at the source code in this file
37  * to find out what you can do.
38  *
39  * Commands always output a freeform string.
40  */
41
42 #if ENABLE_DEBUG_COMMAND
43 struct cmd {
44   const char *cmd;
45   char * (*f) (const char *subcmd, int argc, char *const *const argv);
46 };
47
48 static char *debug_help (const char *subcmd, int argc, char *const *const argv);
49 static char *debug_env (const char *subcmd, int argc, char *const *const argv);
50 static char *debug_fds (const char *subcmd, int argc, char *const *const argv);
51 static char *debug_ls (const char *subcmd, int argc, char *const *const argv);
52 static char *debug_ll (const char *subcmd, int argc, char *const *const argv);
53 static char *debug_segv (const char *subcmd, int argc, char *const *const argv);
54 static char *debug_sh (const char *subcmd, int argc, char *const *const argv);
55
56 static struct cmd cmds[] = {
57   { "help", debug_help },
58   { "env", debug_env },
59   { "fds", debug_fds },
60   { "ls", debug_ls },
61   { "ll", debug_ll },
62   { "segv", debug_segv },
63   { "sh", debug_sh },
64   { NULL, NULL }
65 };
66 #endif
67
68 #if ! ENABLE_DEBUG_COMMAND
69 # define MAYBE_UNUSED ATTRIBUTE_UNUSED
70 #else
71 # define MAYBE_UNUSED /* empty */
72 #endif
73
74 char *
75 do_debug (const char *subcmd MAYBE_UNUSED, char *const *argv MAYBE_UNUSED)
76 {
77 #if ENABLE_DEBUG_COMMAND
78   int argc, i;
79
80   for (i = argc = 0; argv[i] != NULL; ++i)
81     argc++;
82
83   for (i = 0; cmds[i].cmd != NULL; ++i) {
84     if (STRCASEEQ (subcmd, cmds[i].cmd))
85       return cmds[i].f (subcmd, argc, argv);
86   }
87
88   reply_with_error ("use 'debug help' to list the supported commands");
89   return NULL;
90 #else
91   reply_with_error ("guestfsd was not configured with --enable-debug-command");
92   return NULL;
93 #endif
94 }
95
96 #if ENABLE_DEBUG_COMMAND
97 static char *
98 debug_help (const char *subcmd, int argc, char *const *const argv)
99 {
100   int len, i;
101   char *r, *p;
102
103   r = strdup ("Commands supported:");
104   if (!r) {
105     reply_with_perror ("strdup");
106     return NULL;
107   }
108
109   len = strlen (r);
110   for (i = 0; cmds[i].cmd != NULL; ++i) {
111     len += strlen (cmds[i].cmd) + 1; /* space + new command */
112     p = realloc (r, len + 1);        /* +1 for the final NUL */
113     if (p == NULL) {
114       reply_with_perror ("realloc");
115       free (r);
116       return NULL;
117     }
118     r = p;
119
120     strcat (r, " ");
121     strcat (r, cmds[i].cmd);
122   }
123
124   return r;
125 }
126
127 /* Show open FDs. */
128 static char *
129 debug_fds (const char *subcmd, int argc, char *const *const argv)
130 {
131   int r;
132   char *out;
133   size_t size;
134   FILE *fp;
135   DIR *dir;
136   struct dirent *d;
137   char fname[256], link[256];
138   struct stat statbuf;
139
140   fp = open_memstream (&out, &size);
141   if (!fp) {
142     reply_with_perror ("open_memstream");
143     return NULL;
144   }
145
146   dir = opendir ("/proc/self/fd");
147   if (!dir) {
148     reply_with_perror ("opendir: /proc/self/fd");
149     fclose (fp);
150     return NULL;
151   }
152
153   while ((d = readdir (dir)) != NULL) {
154     if (STREQ (d->d_name, ".") || STREQ (d->d_name, ".."))
155       continue;
156
157     snprintf (fname, sizeof fname, "/proc/self/fd/%s", d->d_name);
158
159     r = lstat (fname, &statbuf);
160     if (r == -1) {
161       reply_with_perror ("stat: %s", fname);
162       fclose (fp);
163       free (out);
164       closedir (dir);
165       return NULL;
166     }
167
168     if (S_ISLNK (statbuf.st_mode)) {
169       r = readlink (fname, link, sizeof link - 1);
170       if (r == -1) {
171         reply_with_perror ("readline: %s", fname);
172         fclose (fp);
173         free (out);
174         closedir (dir);
175         return NULL;
176       }
177       link[r] = '\0';
178
179       fprintf (fp, "%2s %s\n", d->d_name, link);
180     } else
181       fprintf (fp, "%2s 0%o\n", d->d_name, statbuf.st_mode);
182   }
183
184   fclose (fp);
185
186   if (closedir (dir) == -1) {
187     reply_with_perror ("closedir");
188     free (out);
189     return NULL;
190   }
191
192   return out;
193 }
194
195 /* Force a segfault in the daemon. */
196 static char *
197 debug_segv (const char *subcmd, int argc, char *const *const argv)
198 {
199   *(int*)0 = 0;
200   return NULL;
201 }
202
203 /* Run an arbitrary shell command using /bin/sh from the appliance.
204  *
205  * Note this is somewhat different from the ordinary guestfs_sh command
206  * because it's not using the guest shell, and is not chrooted.
207  */
208 static char *
209 debug_sh (const char *subcmd, int argc, char *const *const argv)
210 {
211   if (argc < 1) {
212     reply_with_error ("sh: expecting a command to run");
213     return NULL;
214   }
215
216   char *cmd;
217   int len, i, j;
218
219   /* guestfish splits the parameter(s) into a list of strings,
220    * and we have to reassemble them here.  Not ideal. XXX
221    */
222   for (i = len = 0; i < argc; ++i)
223     len += strlen (argv[i]) + 1;
224   cmd = malloc (len);
225   if (!cmd) {
226     reply_with_perror ("malloc");
227     return NULL;
228   }
229   for (i = j = 0; i < argc; ++i) {
230     len = strlen (argv[i]);
231     memcpy (&cmd[j], argv[i], len);
232     j += len;
233     cmd[j] = ' ';
234     j++;
235   }
236   cmd[j-1] = '\0';
237
238   /* Set up some environment variables. */
239   setenv ("root", sysroot, 1);
240   if (access ("/sys/block/sda", F_OK) == 0)
241     setenv ("sd", "sd", 1);
242   else if (access ("/sys/block/hda", F_OK) == 0)
243     setenv ("sd", "hd", 1);
244   else if (access ("/sys/block/vda", F_OK) == 0)
245     setenv ("sd", "vd", 1);
246
247   char *err;
248   int r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
249                     "/bin/sh", "-c", cmd, NULL);
250   free (cmd);
251
252   if (r == -1) {
253     reply_with_error ("%s", err);
254     free (err);
255     return NULL;
256   }
257
258   return err;
259 }
260
261 /* Print the environment that commands get (by running external printenv). */
262 static char *
263 debug_env (const char *subcmd, int argc, char *const *const argv)
264 {
265   int r;
266   char *out, *err;
267
268   r = command (&out, &err, "printenv", NULL);
269   if (r == -1) {
270     reply_with_error ("printenv: %s", err);
271     free (out);
272     free (err);
273     return NULL;
274   }
275
276   free (err);
277
278   return out;
279 }
280
281 /* List files in the appliance. */
282 static char *
283 debug_ls (const char *subcmd, int argc, char *const *const argv)
284 {
285   int len = count_strings (argv);
286   const char *cargv[len+3];
287   int i;
288
289   cargv[0] = "ls";
290   cargv[1] = "-a";
291   for (i = 0; i < len; ++i)
292     cargv[2+i] = argv[i];
293   cargv[2+len] = NULL;
294
295   int r;
296   char *out, *err;
297
298   r = commandv (&out, &err, (void *) cargv);
299   if (r == -1) {
300     reply_with_error ("ls: %s", err);
301     free (out);
302     free (err);
303     return NULL;
304   }
305
306   free (err);
307
308   return out;
309 }
310
311 /* List files in the appliance. */
312 static char *
313 debug_ll (const char *subcmd, int argc, char *const *const argv)
314 {
315   int len = count_strings (argv);
316   const char *cargv[len+3];
317   int i;
318
319   cargv[0] = "ls";
320   cargv[1] = "-la";
321   for (i = 0; i < len; ++i)
322     cargv[2+i] = argv[i];
323   cargv[2+len] = NULL;
324
325   int r;
326   char *out, *err;
327
328   r = commandv (&out, &err, (void *) cargv);
329   if (r == -1) {
330     reply_with_error ("ll: %s", err);
331     free (out);
332     free (err);
333     return NULL;
334   }
335
336   free (err);
337
338   return out;
339 }
340
341 #endif /* ENABLE_DEBUG_COMMAND */
342
343 #if ENABLE_DEBUG_COMMAND
344 static int
345 write_cb (void *fd_ptr, const void *buf, size_t len)
346 {
347   int fd = *(int *)fd_ptr;
348   return xwrite (fd, buf, len);
349 }
350 #endif
351
352 /* Has one FileIn parameter. */
353 int
354 do_debug_upload (const char *filename MAYBE_UNUSED, int mode MAYBE_UNUSED)
355 {
356 #if ENABLE_DEBUG_COMMAND
357   /* Not chrooted - this command lets you upload a file to anywhere
358    * in the appliance.
359    */
360   int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, mode);
361
362   if (fd == -1) {
363     int err = errno;
364     cancel_receive ();
365     errno = err;
366     reply_with_perror ("%s", filename);
367     return -1;
368   }
369
370   int r = receive_file (write_cb, &fd);
371   if (r == -1) {                /* write error */
372     int err = errno;
373     cancel_receive ();
374     errno = err;
375     reply_with_error ("write error: %s", filename);
376     close (fd);
377     return -1;
378   }
379   if (r == -2) {                /* cancellation from library */
380     close (fd);
381     /* Do NOT send any error. */
382     return -1;
383   }
384
385   if (close (fd) == -1) {
386     int err = errno;
387     if (r == -1)                /* if r == 0, file transfer ended already */
388       cancel_receive ();
389     errno = err;
390     reply_with_perror ("close: %s", filename);
391     return -1;
392   }
393
394   return 0;
395 #else
396   reply_with_error ("guestfsd was not configured with --enable-debug-command");
397   return NULL;
398 #endif
399 }