Change protocol to send Linux errno from daemon to library.
[libguestfs.git] / daemon / file.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 <unistd.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27
28 #include "../src/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_exists (const char *path)
274 {
275   int r;
276
277   CHROOT_IN;
278   r = access (path, F_OK);
279   CHROOT_OUT;
280
281   return r == 0;
282 }
283
284 int
285 do_is_file (const char *path)
286 {
287   int r;
288   struct stat buf;
289
290   CHROOT_IN;
291   r = lstat (path, &buf);
292   CHROOT_OUT;
293
294   if (r == -1) {
295     if (errno != ENOENT && errno != ENOTDIR) {
296       reply_with_perror ("stat: %s", path);
297       return -1;
298     }
299     else
300       return 0;                 /* Not a file. */
301   }
302
303   return S_ISREG (buf.st_mode);
304 }
305
306 int
307 do_write_file (const char *path, const char *content, int size)
308 {
309   int fd;
310
311   /* This call is deprecated, and it has a broken interface.  New code
312    * should use the 'guestfs_write' call instead.  Because we used an
313    * XDR string type, 'content' cannot contain ASCII NUL and 'size'
314    * must never be longer than the string.  We must check this to
315    * ensure random stuff from XDR or daemon memory isn't written to
316    * the file (RHBZ#597135).
317    */
318   if (size < 0) {
319     reply_with_error ("size cannot be negative");
320     return -1;
321   }
322
323   /* Note content_len must be small because of the limits on protocol
324    * message size.
325    */
326   int content_len = (int) strlen (content);
327
328   if (size == 0)
329     size = content_len;
330   else if (size > content_len) {
331     reply_with_error ("size parameter is larger than string content");
332     return -1;
333   }
334
335   CHROOT_IN;
336   fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
337   CHROOT_OUT;
338
339   if (fd == -1) {
340     reply_with_perror ("open: %s", path);
341     return -1;
342   }
343
344   if (xwrite (fd, content, size) == -1) {
345     reply_with_perror ("write");
346     close (fd);
347     return -1;
348   }
349
350   if (close (fd) == -1) {
351     reply_with_perror ("close: %s", path);
352     return -1;
353   }
354
355   return 0;
356 }
357
358 int
359 do_write (const char *path, const char *content, size_t size)
360 {
361   int fd;
362
363   CHROOT_IN;
364   fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
365   CHROOT_OUT;
366
367   if (fd == -1) {
368     reply_with_perror ("open: %s", path);
369     return -1;
370   }
371
372   if (xwrite (fd, content, size) == -1) {
373     reply_with_perror ("write");
374     close (fd);
375     return -1;
376   }
377
378   if (close (fd) == -1) {
379     reply_with_perror ("close: %s", path);
380     return -1;
381   }
382
383   return 0;
384 }
385
386 char *
387 do_read_file (const char *path, size_t *size_r)
388 {
389   int fd;
390   struct stat statbuf;
391   char *r;
392
393   CHROOT_IN;
394   fd = open (path, O_RDONLY);
395   CHROOT_OUT;
396
397   if (fd == -1) {
398     reply_with_perror ("open: %s", path);
399     return NULL;
400   }
401
402   if (fstat (fd, &statbuf) == -1) {
403     reply_with_perror ("fstat: %s", path);
404     close (fd);
405     return NULL;
406   }
407
408   /* The actual limit on messages is smaller than this.  This
409    * check just limits the amount of memory we'll try and allocate
410    * here.  If the message is larger than the real limit, that will
411    * be caught later when we try to serialize the message.
412    */
413   if (statbuf.st_size >= GUESTFS_MESSAGE_MAX) {
414     reply_with_error ("%s: file is too large for the protocol, use guestfs_download instead", path);
415     close (fd);
416     return NULL;
417   }
418   r = malloc (statbuf.st_size);
419   if (r == NULL) {
420     reply_with_perror ("malloc");
421     close (fd);
422     return NULL;
423   }
424
425   if (xread (fd, r, statbuf.st_size) == -1) {
426     reply_with_perror ("read: %s", path);
427     close (fd);
428     free (r);
429     return NULL;
430   }
431
432   if (close (fd) == -1) {
433     reply_with_perror ("close: %s", path);
434     free (r);
435     return NULL;
436   }
437
438   /* Mustn't touch *size_r until we are sure that we won't return any
439    * error (RHBZ#589039).
440    */
441   *size_r = statbuf.st_size;
442   return r;
443 }
444
445 char *
446 do_pread (const char *path, int count, int64_t offset, size_t *size_r)
447 {
448   int fd;
449   ssize_t r;
450   char *buf;
451
452   /* The actual limit on messages is smaller than this.  This check
453    * just limits the amount of memory we'll try and allocate in the
454    * function.  If the message is larger than the real limit, that
455    * will be caught later when we try to serialize the message.
456    */
457   if (count >= GUESTFS_MESSAGE_MAX) {
458     reply_with_error ("%s: count is too large for the protocol, use smaller reads", path);
459     return NULL;
460   }
461
462   CHROOT_IN;
463   fd = open (path, O_RDONLY);
464   CHROOT_OUT;
465
466   if (fd == -1) {
467     reply_with_perror ("open: %s", path);
468     return NULL;
469   }
470
471   buf = malloc (count);
472   if (buf == NULL) {
473     reply_with_perror ("malloc");
474     close (fd);
475     return NULL;
476   }
477
478   r = pread (fd, buf, count, offset);
479   if (r == -1) {
480     reply_with_perror ("pread: %s", path);
481     close (fd);
482     free (buf);
483     return NULL;
484   }
485
486   if (close (fd) == -1) {
487     reply_with_perror ("close: %s", path);
488     close (fd);
489     free (buf);
490     return NULL;
491   }
492
493   /* Mustn't touch *size_r until we are sure that we won't return any
494    * error (RHBZ#589039).
495    */
496   *size_r = r;
497   return buf;
498 }
499
500 int
501 do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
502 {
503   int fd;
504   ssize_t r;
505
506   CHROOT_IN;
507   fd = open (path, O_WRONLY);
508   CHROOT_OUT;
509
510   if (fd == -1) {
511     reply_with_perror ("open: %s", path);
512     return -1;
513   }
514
515   r = pwrite (fd, content, size, offset);
516   if (r == -1) {
517     reply_with_perror ("pwrite: %s", path);
518     close (fd);
519     return -1;
520   }
521
522   if (close (fd) == -1) {
523     reply_with_perror ("close: %s", path);
524     close (fd);
525     return -1;
526   }
527
528   return r;
529 }
530
531 /* This runs the 'file' command. */
532 char *
533 do_file (const char *path)
534 {
535   char *buf = NULL;
536   const char *display_path = path;
537
538   int is_dev = STRPREFIX (path, "/dev/");
539
540   if (!is_dev) {
541     buf = sysroot_path (path);
542     if (!buf) {
543       reply_with_perror ("malloc");
544       return NULL;
545     }
546     path = buf;
547
548     /* For non-dev, check this is a regular file, else just return the
549      * file type as a string (RHBZ#582484).
550      */
551     struct stat statbuf;
552     if (lstat (path, &statbuf) == -1) {
553       reply_with_perror ("lstat: %s", display_path);
554       free (buf);
555       return NULL;
556     }
557
558     if (! S_ISREG (statbuf.st_mode)) {
559       char *ret;
560
561       free (buf);
562
563       if (S_ISDIR (statbuf.st_mode))
564         ret = strdup ("directory");
565       else if (S_ISCHR (statbuf.st_mode))
566         ret = strdup ("character device");
567       else if (S_ISBLK (statbuf.st_mode))
568         ret = strdup ("block device");
569       else if (S_ISFIFO (statbuf.st_mode))
570         ret = strdup ("FIFO");
571       else if (S_ISLNK (statbuf.st_mode))
572         ret = strdup ("symbolic link");
573       else if (S_ISSOCK (statbuf.st_mode))
574         ret = strdup ("socket");
575       else
576         ret = strdup ("unknown, not regular file");
577
578       if (ret == NULL)
579         reply_with_perror ("strdup");
580       return ret;
581     }
582   }
583
584   /* Which flags to use?  For /dev paths, follow links because
585    * /dev/VG/LV is a symbolic link.
586    */
587   const char *flags = is_dev ? "-zbsL" : "-zb";
588
589   char *out, *err;
590   int r = command (&out, &err, "file", flags, path, NULL);
591   free (buf);
592
593   if (r == -1) {
594     free (out);
595     reply_with_error ("%s: %s", display_path, err);
596     free (err);
597     return NULL;
598   }
599   free (err);
600
601   /* We need to remove the trailing \n from output of file(1). */
602   size_t len = strlen (out);
603   if (len > 0 && out[len-1] == '\n')
604     out[len-1] = '\0';
605
606   return out;                   /* caller frees */
607 }
608
609 /* zcat | file */
610 char *
611 do_zfile (const char *method, const char *path)
612 {
613   int len;
614   const char *zcat;
615   char *cmd;
616   FILE *fp;
617   char line[256];
618
619   if (STREQ (method, "gzip") || STREQ (method, "compress"))
620     zcat = "zcat";
621   else if (STREQ (method, "bzip2"))
622     zcat = "bzcat";
623   else {
624     reply_with_error ("unknown method");
625     return NULL;
626   }
627
628   if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
629     reply_with_perror ("asprintf");
630     return NULL;
631   }
632
633   if (verbose)
634     fprintf (stderr, "%s\n", cmd);
635
636   fp = popen (cmd, "r");
637   if (fp == NULL) {
638     reply_with_perror ("%s", cmd);
639     free (cmd);
640     return NULL;
641   }
642
643   free (cmd);
644
645   if (fgets (line, sizeof line, fp) == NULL) {
646     reply_with_perror ("fgets");
647     fclose (fp);
648     return NULL;
649   }
650
651   if (fclose (fp) == -1) {
652     reply_with_perror ("fclose");
653     return NULL;
654   }
655
656   len = strlen (line);
657   if (len > 0 && line[len-1] == '\n')
658     line[len-1] = '\0';
659
660   return strdup (line);
661 }
662
663 int64_t
664 do_filesize (const char *path)
665 {
666   int r;
667   struct stat buf;
668
669   CHROOT_IN;
670   r = stat (path, &buf);        /* follow symlinks */
671   CHROOT_OUT;
672
673   if (r == -1) {
674     reply_with_perror ("%s", path);
675     return -1;
676   }
677
678   return buf.st_size;
679 }