Update FSF address.
[libguestfs.git] / daemon / file.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27
28 #include "guestfs_protocol.h"
29 #include "daemon.h"
30 #include "actions.h"
31
32 int
33 do_touch (const char *path)
34 {
35   int fd;
36   int r;
37   struct stat buf;
38
39   /* RHBZ#582484: Restrict touch to regular files.  It's also OK
40    * here if the file does not exist, since we will create it.
41    */
42   CHROOT_IN;
43   r = lstat (path, &buf);
44   CHROOT_OUT;
45
46   if (r == -1) {
47     if (errno != ENOENT) {
48       reply_with_perror ("lstat: %s", path);
49       return -1;
50     }
51   } else {
52     if (! S_ISREG (buf.st_mode)) {
53       reply_with_error ("%s: touch can only be used on a regular files", path);
54       return -1;
55     }
56   }
57
58   CHROOT_IN;
59   fd = open (path, O_WRONLY | O_CREAT | O_NOCTTY, 0666);
60   CHROOT_OUT;
61
62   if (fd == -1) {
63     reply_with_perror ("open: %s", path);
64     return -1;
65   }
66
67   r = futimens (fd, NULL);
68   if (r == -1) {
69     reply_with_perror ("futimens: %s", path);
70     close (fd);
71     return -1;
72   }
73
74   if (close (fd) == -1) {
75     reply_with_perror ("close: %s", path);
76     return -1;
77   }
78
79   return 0;
80 }
81
82 char *
83 do_cat (const char *path)
84 {
85   int fd;
86   int alloc, size, r, max;
87   char *buf, *buf2;
88
89   CHROOT_IN;
90   fd = open (path, O_RDONLY);
91   CHROOT_OUT;
92
93   if (fd == -1) {
94     reply_with_perror ("open: %s", path);
95     return NULL;
96   }
97
98   /* Read up to GUESTFS_MESSAGE_MAX - <overhead> bytes.  If it's
99    * larger than that, we need to return an error instead (for
100    * correctness).
101    */
102   max = GUESTFS_MESSAGE_MAX - 1000;
103   buf = NULL;
104   size = alloc = 0;
105
106   for (;;) {
107     if (size >= alloc) {
108       alloc += 8192;
109       if (alloc > max) {
110         reply_with_error ("%s: file is too large for message buffer",
111                           path);
112         free (buf);
113         close (fd);
114         return NULL;
115       }
116       buf2 = realloc (buf, alloc);
117       if (buf2 == NULL) {
118         reply_with_perror ("realloc");
119         free (buf);
120         close (fd);
121         return NULL;
122       }
123       buf = buf2;
124     }
125
126     r = read (fd, buf + size, alloc - size);
127     if (r == -1) {
128       reply_with_perror ("read: %s", path);
129       free (buf);
130       close (fd);
131       return NULL;
132     }
133     if (r == 0) {
134       buf[size] = '\0';
135       break;
136     }
137     if (r > 0)
138       size += r;
139   }
140
141   if (close (fd) == -1) {
142     reply_with_perror ("close: %s", path);
143     free (buf);
144     return NULL;
145   }
146
147   return buf;                   /* caller will free */
148 }
149
150 char **
151 do_read_lines (const char *path)
152 {
153   char **r = NULL;
154   int size = 0, alloc = 0;
155   FILE *fp;
156   char *line = NULL;
157   size_t len = 0;
158   ssize_t n;
159
160   CHROOT_IN;
161   fp = fopen (path, "r");
162   CHROOT_OUT;
163
164   if (!fp) {
165     reply_with_perror ("fopen: %s", path);
166     return NULL;
167   }
168
169   while ((n = getline (&line, &len, fp)) != -1) {
170     /* Remove either LF or CRLF. */
171     if (n >= 2 && line[n-2] == '\r' && line[n-1] == '\n')
172       line[n-2] = '\0';
173     else if (n >= 1 && line[n-1] == '\n')
174       line[n-1] = '\0';
175
176     if (add_string (&r, &size, &alloc, line) == -1) {
177       free (line);
178       fclose (fp);
179       return NULL;
180     }
181   }
182
183   free (line);
184
185   if (add_string (&r, &size, &alloc, NULL) == -1) {
186     fclose (fp);
187     return NULL;
188   }
189
190   if (fclose (fp) == EOF) {
191     reply_with_perror ("fclose: %s", path);
192     free_strings (r);
193     return NULL;
194   }
195
196   return r;
197 }
198
199 int
200 do_rm (const char *path)
201 {
202   int r;
203
204   CHROOT_IN;
205   r = unlink (path);
206   CHROOT_OUT;
207
208   if (r == -1) {
209     reply_with_perror ("%s", path);
210     return -1;
211   }
212
213   return 0;
214 }
215
216 int
217 do_chmod (int mode, const char *path)
218 {
219   int r;
220
221   if (mode < 0) {
222     reply_with_error ("%s: mode is negative", path);
223     return -1;
224   }
225
226   CHROOT_IN;
227   r = chmod (path, mode);
228   CHROOT_OUT;
229
230   if (r == -1) {
231     reply_with_perror ("%s: 0%o", path, mode);
232     return -1;
233   }
234
235   return 0;
236 }
237
238 int
239 do_chown (int owner, int group, const char *path)
240 {
241   int r;
242
243   CHROOT_IN;
244   r = chown (path, owner, group);
245   CHROOT_OUT;
246
247   if (r == -1) {
248     reply_with_perror ("%s: %d.%d", path, owner, group);
249     return -1;
250   }
251
252   return 0;
253 }
254
255 int
256 do_lchown (int owner, int group, const char *path)
257 {
258   int r;
259
260   CHROOT_IN;
261   r = lchown (path, owner, group);
262   CHROOT_OUT;
263
264   if (r == -1) {
265     reply_with_perror ("%s: %d.%d", path, owner, group);
266     return -1;
267   }
268
269   return 0;
270 }
271
272 int
273 do_write_file (const char *path, const char *content, int size)
274 {
275   int fd;
276
277   /* This call is deprecated, and it has a broken interface.  New code
278    * should use the 'guestfs_write' call instead.  Because we used an
279    * XDR string type, 'content' cannot contain ASCII NUL and 'size'
280    * must never be longer than the string.  We must check this to
281    * ensure random stuff from XDR or daemon memory isn't written to
282    * the file (RHBZ#597135).
283    */
284   if (size < 0) {
285     reply_with_error ("size cannot be negative");
286     return -1;
287   }
288
289   /* Note content_len must be small because of the limits on protocol
290    * message size.
291    */
292   int content_len = (int) strlen (content);
293
294   if (size == 0)
295     size = content_len;
296   else if (size > content_len) {
297     reply_with_error ("size parameter is larger than string content");
298     return -1;
299   }
300
301   CHROOT_IN;
302   fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
303   CHROOT_OUT;
304
305   if (fd == -1) {
306     reply_with_perror ("open: %s", path);
307     return -1;
308   }
309
310   if (xwrite (fd, content, size) == -1) {
311     reply_with_perror ("write");
312     close (fd);
313     return -1;
314   }
315
316   if (close (fd) == -1) {
317     reply_with_perror ("close: %s", path);
318     return -1;
319   }
320
321   return 0;
322 }
323
324 int
325 do_write (const char *path, const char *content, size_t size)
326 {
327   int fd;
328
329   CHROOT_IN;
330   fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
331   CHROOT_OUT;
332
333   if (fd == -1) {
334     reply_with_perror ("open: %s", path);
335     return -1;
336   }
337
338   if (xwrite (fd, content, size) == -1) {
339     reply_with_perror ("write");
340     close (fd);
341     return -1;
342   }
343
344   if (close (fd) == -1) {
345     reply_with_perror ("close: %s", path);
346     return -1;
347   }
348
349   return 0;
350 }
351
352 int
353 do_write_append (const char *path, const char *content, size_t size)
354 {
355   int fd;
356
357   CHROOT_IN;
358   fd = open (path, O_WRONLY | O_APPEND | O_CREAT | O_NOCTTY, 0666);
359   CHROOT_OUT;
360
361   if (fd == -1) {
362     reply_with_perror ("open: %s", path);
363     return -1;
364   }
365
366   if (xwrite (fd, content, size) == -1) {
367     reply_with_perror ("write");
368     close (fd);
369     return -1;
370   }
371
372   if (close (fd) == -1) {
373     reply_with_perror ("close: %s", path);
374     return -1;
375   }
376
377   return 0;
378 }
379
380 char *
381 do_read_file (const char *path, size_t *size_r)
382 {
383   int fd;
384   struct stat statbuf;
385   char *r;
386
387   CHROOT_IN;
388   fd = open (path, O_RDONLY);
389   CHROOT_OUT;
390
391   if (fd == -1) {
392     reply_with_perror ("open: %s", path);
393     return NULL;
394   }
395
396   if (fstat (fd, &statbuf) == -1) {
397     reply_with_perror ("fstat: %s", path);
398     close (fd);
399     return NULL;
400   }
401
402   /* The actual limit on messages is smaller than this.  This
403    * check just limits the amount of memory we'll try and allocate
404    * here.  If the message is larger than the real limit, that will
405    * be caught later when we try to serialize the message.
406    */
407   if (statbuf.st_size >= GUESTFS_MESSAGE_MAX) {
408     reply_with_error ("%s: file is too large for the protocol, use guestfs_download instead", path);
409     close (fd);
410     return NULL;
411   }
412   r = malloc (statbuf.st_size);
413   if (r == NULL) {
414     reply_with_perror ("malloc");
415     close (fd);
416     return NULL;
417   }
418
419   if (xread (fd, r, statbuf.st_size) == -1) {
420     reply_with_perror ("read: %s", path);
421     close (fd);
422     free (r);
423     return NULL;
424   }
425
426   if (close (fd) == -1) {
427     reply_with_perror ("close: %s", path);
428     free (r);
429     return NULL;
430   }
431
432   /* Mustn't touch *size_r until we are sure that we won't return any
433    * error (RHBZ#589039).
434    */
435   *size_r = statbuf.st_size;
436   return r;
437 }
438
439 static char *
440 pread_fd (int fd, int count, int64_t offset, size_t *size_r,
441           const char *display_path)
442 {
443   ssize_t r;
444   char *buf;
445
446   if (count < 0) {
447     reply_with_error ("count is negative");
448     close (fd);
449     return NULL;
450   }
451
452   if (offset < 0) {
453     reply_with_error ("offset is negative");
454     close (fd);
455     return NULL;
456   }
457
458   /* The actual limit on messages is smaller than this.  This check
459    * just limits the amount of memory we'll try and allocate in the
460    * function.  If the message is larger than the real limit, that
461    * will be caught later when we try to serialize the message.
462    */
463   if (count >= GUESTFS_MESSAGE_MAX) {
464     reply_with_error ("%s: count is too large for the protocol, use smaller reads", display_path);
465     close (fd);
466     return NULL;
467   }
468
469   buf = malloc (count);
470   if (buf == NULL) {
471     reply_with_perror ("malloc");
472     close (fd);
473     return NULL;
474   }
475
476   r = pread (fd, buf, count, offset);
477   if (r == -1) {
478     reply_with_perror ("pread: %s", display_path);
479     close (fd);
480     free (buf);
481     return NULL;
482   }
483
484   if (close (fd) == -1) {
485     reply_with_perror ("close: %s", display_path);
486     close (fd);
487     free (buf);
488     return NULL;
489   }
490
491   /* Mustn't touch *size_r until we are sure that we won't return any
492    * error (RHBZ#589039).
493    */
494   *size_r = r;
495   return buf;
496 }
497
498 char *
499 do_pread (const char *path, int count, int64_t offset, size_t *size_r)
500 {
501   int fd;
502
503   CHROOT_IN;
504   fd = open (path, O_RDONLY);
505   CHROOT_OUT;
506
507   if (fd == -1) {
508     reply_with_perror ("open: %s", path);
509     return NULL;
510   }
511
512   return pread_fd (fd, count, offset, size_r, path);
513 }
514
515 char *
516 do_pread_device (const char *device, int count, int64_t offset, size_t *size_r)
517 {
518   int fd = open (device, O_RDONLY);
519   if (fd == -1) {
520     reply_with_perror ("open: %s", device);
521     return NULL;
522   }
523
524   return pread_fd (fd, count, offset, size_r, device);
525 }
526
527 static int
528 pwrite_fd (int fd, const char *content, size_t size, int64_t offset,
529            const char *display_path)
530 {
531   ssize_t r;
532
533   r = pwrite (fd, content, size, offset);
534   if (r == -1) {
535     reply_with_perror ("pwrite: %s", display_path);
536     close (fd);
537     return -1;
538   }
539
540   if (close (fd) == -1) {
541     reply_with_perror ("close: %s", display_path);
542     close (fd);
543     return -1;
544   }
545
546   return r;
547 }
548
549 int
550 do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
551 {
552   int fd;
553
554   if (offset < 0) {
555     reply_with_error ("offset is negative");
556     return -1;
557   }
558
559   CHROOT_IN;
560   fd = open (path, O_WRONLY);
561   CHROOT_OUT;
562
563   if (fd == -1) {
564     reply_with_perror ("open: %s", path);
565     return -1;
566   }
567
568   return pwrite_fd (fd, content, size, offset, path);
569 }
570
571 int
572 do_pwrite_device (const char *device, const char *content, size_t size,
573                   int64_t offset)
574 {
575   if (offset < 0) {
576     reply_with_error ("offset is negative");
577     return -1;
578   }
579
580   int fd = open (device, O_WRONLY);
581   if (fd == -1) {
582     reply_with_perror ("open: %s", device);
583     return -1;
584   }
585
586   return pwrite_fd (fd, content, size, offset, device);
587 }
588
589 /* This runs the 'file' command. */
590 char *
591 do_file (const char *path)
592 {
593   char *buf = NULL;
594   const char *display_path = path;
595
596   int is_dev = STRPREFIX (path, "/dev/");
597
598   if (!is_dev) {
599     buf = sysroot_path (path);
600     if (!buf) {
601       reply_with_perror ("malloc");
602       return NULL;
603     }
604     path = buf;
605
606     /* For non-dev, check this is a regular file, else just return the
607      * file type as a string (RHBZ#582484).
608      */
609     struct stat statbuf;
610     if (lstat (path, &statbuf) == -1) {
611       reply_with_perror ("lstat: %s", display_path);
612       free (buf);
613       return NULL;
614     }
615
616     if (! S_ISREG (statbuf.st_mode)) {
617       char *ret;
618
619       free (buf);
620
621       if (S_ISDIR (statbuf.st_mode))
622         ret = strdup ("directory");
623       else if (S_ISCHR (statbuf.st_mode))
624         ret = strdup ("character device");
625       else if (S_ISBLK (statbuf.st_mode))
626         ret = strdup ("block device");
627       else if (S_ISFIFO (statbuf.st_mode))
628         ret = strdup ("FIFO");
629       else if (S_ISLNK (statbuf.st_mode))
630         ret = strdup ("symbolic link");
631       else if (S_ISSOCK (statbuf.st_mode))
632         ret = strdup ("socket");
633       else
634         ret = strdup ("unknown, not regular file");
635
636       if (ret == NULL)
637         reply_with_perror ("strdup");
638       return ret;
639     }
640   }
641
642   /* Which flags to use?  For /dev paths, follow links because
643    * /dev/VG/LV is a symbolic link.
644    */
645   const char *flags = is_dev ? "-zbsL" : "-zb";
646
647   char *out, *err;
648   int r = command (&out, &err, "file", flags, path, NULL);
649   free (buf);
650
651   if (r == -1) {
652     free (out);
653     reply_with_error ("%s: %s", display_path, err);
654     free (err);
655     return NULL;
656   }
657   free (err);
658
659   /* We need to remove the trailing \n from output of file(1). */
660   size_t len = strlen (out);
661   if (len > 0 && out[len-1] == '\n')
662     out[len-1] = '\0';
663
664   return out;                   /* caller frees */
665 }
666
667 /* zcat | file */
668 char *
669 do_zfile (const char *method, const char *path)
670 {
671   int len;
672   const char *zcat;
673   char *cmd;
674   FILE *fp;
675   char line[256];
676
677   if (STREQ (method, "gzip") || STREQ (method, "compress"))
678     zcat = "zcat";
679   else if (STREQ (method, "bzip2"))
680     zcat = "bzcat";
681   else {
682     reply_with_error ("unknown method");
683     return NULL;
684   }
685
686   if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
687     reply_with_perror ("asprintf");
688     return NULL;
689   }
690
691   if (verbose)
692     fprintf (stderr, "%s\n", cmd);
693
694   fp = popen (cmd, "r");
695   if (fp == NULL) {
696     reply_with_perror ("%s", cmd);
697     free (cmd);
698     return NULL;
699   }
700
701   free (cmd);
702
703   if (fgets (line, sizeof line, fp) == NULL) {
704     reply_with_perror ("fgets");
705     fclose (fp);
706     return NULL;
707   }
708
709   if (fclose (fp) == -1) {
710     reply_with_perror ("fclose");
711     return NULL;
712   }
713
714   len = strlen (line);
715   if (len > 0 && line[len-1] == '\n')
716     line[len-1] = '\0';
717
718   return strdup (line);
719 }
720
721 int64_t
722 do_filesize (const char *path)
723 {
724   int r;
725   struct stat buf;
726
727   CHROOT_IN;
728   r = stat (path, &buf);        /* follow symlinks */
729   CHROOT_OUT;
730
731   if (r == -1) {
732     reply_with_perror ("%s", path);
733     return -1;
734   }
735
736   return buf.st_size;
737 }