parted: Check partition number >= 1 in several calls.
[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
38   CHROOT_IN;
39   fd = open (path, O_WRONLY | O_CREAT | O_NOCTTY, 0666);
40   CHROOT_OUT;
41
42   if (fd == -1) {
43     reply_with_perror ("open: %s", path);
44     return -1;
45   }
46
47   r = futimens (fd, NULL);
48   if (r == -1) {
49     reply_with_perror ("futimens: %s", path);
50     close (fd);
51     return -1;
52   }
53
54   if (close (fd) == -1) {
55     reply_with_perror ("close: %s", path);
56     return -1;
57   }
58
59   return 0;
60 }
61
62 char *
63 do_cat (const char *path)
64 {
65   int fd;
66   int alloc, size, r, max;
67   char *buf, *buf2;
68
69   CHROOT_IN;
70   fd = open (path, O_RDONLY);
71   CHROOT_OUT;
72
73   if (fd == -1) {
74     reply_with_perror ("open: %s", path);
75     return NULL;
76   }
77
78   /* Read up to GUESTFS_MESSAGE_MAX - <overhead> bytes.  If it's
79    * larger than that, we need to return an error instead (for
80    * correctness).
81    */
82   max = GUESTFS_MESSAGE_MAX - 1000;
83   buf = NULL;
84   size = alloc = 0;
85
86   for (;;) {
87     if (size >= alloc) {
88       alloc += 8192;
89       if (alloc > max) {
90         reply_with_error ("%s: file is too large for message buffer",
91                           path);
92         free (buf);
93         close (fd);
94         return NULL;
95       }
96       buf2 = realloc (buf, alloc);
97       if (buf2 == NULL) {
98         reply_with_perror ("realloc");
99         free (buf);
100         close (fd);
101         return NULL;
102       }
103       buf = buf2;
104     }
105
106     r = read (fd, buf + size, alloc - size);
107     if (r == -1) {
108       reply_with_perror ("read: %s", path);
109       free (buf);
110       close (fd);
111       return NULL;
112     }
113     if (r == 0) {
114       buf[size] = '\0';
115       break;
116     }
117     if (r > 0)
118       size += r;
119   }
120
121   if (close (fd) == -1) {
122     reply_with_perror ("close: %s", path);
123     free (buf);
124     return NULL;
125   }
126
127   return buf;                   /* caller will free */
128 }
129
130 char **
131 do_read_lines (const char *path)
132 {
133   char **r = NULL;
134   int size = 0, alloc = 0;
135   FILE *fp;
136   char *line = NULL;
137   size_t len = 0;
138   ssize_t n;
139
140   CHROOT_IN;
141   fp = fopen (path, "r");
142   CHROOT_OUT;
143
144   if (!fp) {
145     reply_with_perror ("fopen: %s", path);
146     return NULL;
147   }
148
149   while ((n = getline (&line, &len, fp)) != -1) {
150     /* Remove either LF or CRLF. */
151     if (n >= 2 && line[n-2] == '\r' && line[n-1] == '\n')
152       line[n-2] = '\0';
153     else if (n >= 1 && line[n-1] == '\n')
154       line[n-1] = '\0';
155
156     if (add_string (&r, &size, &alloc, line) == -1) {
157       free (line);
158       fclose (fp);
159       return NULL;
160     }
161   }
162
163   free (line);
164
165   if (add_string (&r, &size, &alloc, NULL) == -1) {
166     fclose (fp);
167     return NULL;
168   }
169
170   if (fclose (fp) == EOF) {
171     reply_with_perror ("fclose: %s", path);
172     free_strings (r);
173     return NULL;
174   }
175
176   return r;
177 }
178
179 int
180 do_rm (const char *path)
181 {
182   int r;
183
184   CHROOT_IN;
185   r = unlink (path);
186   CHROOT_OUT;
187
188   if (r == -1) {
189     reply_with_perror ("%s", path);
190     return -1;
191   }
192
193   return 0;
194 }
195
196 int
197 do_chmod (int mode, const char *path)
198 {
199   int r;
200
201   if (mode < 0) {
202     reply_with_error ("%s: mode is negative", path);
203     return -1;
204   }
205
206   CHROOT_IN;
207   r = chmod (path, mode);
208   CHROOT_OUT;
209
210   if (r == -1) {
211     reply_with_perror ("%s: 0%o", path, mode);
212     return -1;
213   }
214
215   return 0;
216 }
217
218 int
219 do_chown (int owner, int group, const char *path)
220 {
221   int r;
222
223   CHROOT_IN;
224   r = chown (path, owner, group);
225   CHROOT_OUT;
226
227   if (r == -1) {
228     reply_with_perror ("%s: %d.%d", path, owner, group);
229     return -1;
230   }
231
232   return 0;
233 }
234
235 int
236 do_lchown (int owner, int group, const char *path)
237 {
238   int r;
239
240   CHROOT_IN;
241   r = lchown (path, owner, group);
242   CHROOT_OUT;
243
244   if (r == -1) {
245     reply_with_perror ("%s: %d.%d", path, owner, group);
246     return -1;
247   }
248
249   return 0;
250 }
251
252 int
253 do_exists (const char *path)
254 {
255   int r;
256
257   CHROOT_IN;
258   r = access (path, F_OK);
259   CHROOT_OUT;
260
261   return r == 0;
262 }
263
264 int
265 do_is_file (const char *path)
266 {
267   int r;
268   struct stat buf;
269
270   CHROOT_IN;
271   r = lstat (path, &buf);
272   CHROOT_OUT;
273
274   if (r == -1) {
275     if (errno != ENOENT && errno != ENOTDIR) {
276       reply_with_perror ("stat: %s", path);
277       return -1;
278     }
279     else
280       return 0;                 /* Not a file. */
281   }
282
283   return S_ISREG (buf.st_mode);
284 }
285
286 int
287 do_write_file (const char *path, const char *content, int size)
288 {
289   int fd;
290
291   /* This call is deprecated, and it has a broken interface.  New code
292    * should use the 'guestfs_write' call instead.  Because we used an
293    * XDR string type, 'content' cannot contain ASCII NUL and 'size'
294    * must never be longer than the string.  We must check this to
295    * ensure random stuff from XDR or daemon memory isn't written to
296    * the file (RHBZ#597135).
297    */
298   if (size < 0) {
299     reply_with_error ("size cannot be negative");
300     return -1;
301   }
302
303   /* Note content_len must be small because of the limits on protocol
304    * message size.
305    */
306   int content_len = (int) strlen (content);
307
308   if (size == 0)
309     size = content_len;
310   else if (size > content_len) {
311     reply_with_error ("size parameter is larger than string content");
312     return -1;
313   }
314
315   CHROOT_IN;
316   fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
317   CHROOT_OUT;
318
319   if (fd == -1) {
320     reply_with_perror ("open: %s", path);
321     return -1;
322   }
323
324   if (xwrite (fd, content, size) == -1) {
325     reply_with_perror ("write");
326     close (fd);
327     return -1;
328   }
329
330   if (close (fd) == -1) {
331     reply_with_perror ("close: %s", path);
332     return -1;
333   }
334
335   return 0;
336 }
337
338 int
339 do_write (const char *path, const char *content, size_t size)
340 {
341   int fd;
342
343   CHROOT_IN;
344   fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
345   CHROOT_OUT;
346
347   if (fd == -1) {
348     reply_with_perror ("open: %s", path);
349     return -1;
350   }
351
352   if (xwrite (fd, content, size) == -1) {
353     reply_with_perror ("write");
354     close (fd);
355     return -1;
356   }
357
358   if (close (fd) == -1) {
359     reply_with_perror ("close: %s", path);
360     return -1;
361   }
362
363   return 0;
364 }
365
366 char *
367 do_read_file (const char *path, size_t *size_r)
368 {
369   int fd;
370   struct stat statbuf;
371   char *r;
372
373   CHROOT_IN;
374   fd = open (path, O_RDONLY);
375   CHROOT_OUT;
376
377   if (fd == -1) {
378     reply_with_perror ("open: %s", path);
379     return NULL;
380   }
381
382   if (fstat (fd, &statbuf) == -1) {
383     reply_with_perror ("fstat: %s", path);
384     close (fd);
385     return NULL;
386   }
387
388   /* The actual limit on messages is smaller than this.  This
389    * check just limits the amount of memory we'll try and allocate
390    * here.  If the message is larger than the real limit, that will
391    * be caught later when we try to serialize the message.
392    */
393   if (statbuf.st_size >= GUESTFS_MESSAGE_MAX) {
394     reply_with_error ("%s: file is too large for the protocol, use guestfs_download instead", path);
395     close (fd);
396     return NULL;
397   }
398   r = malloc (statbuf.st_size);
399   if (r == NULL) {
400     reply_with_perror ("malloc");
401     close (fd);
402     return NULL;
403   }
404
405   if (xread (fd, r, statbuf.st_size) == -1) {
406     reply_with_perror ("read: %s", path);
407     close (fd);
408     free (r);
409     return NULL;
410   }
411
412   if (close (fd) == -1) {
413     reply_with_perror ("close: %s", path);
414     free (r);
415     return NULL;
416   }
417
418   /* Mustn't touch *size_r until we are sure that we won't return any
419    * error (RHBZ#589039).
420    */
421   *size_r = statbuf.st_size;
422   return r;
423 }
424
425 char *
426 do_pread (const char *path, int count, int64_t offset, size_t *size_r)
427 {
428   int fd;
429   ssize_t r;
430   char *buf;
431
432   /* The actual limit on messages is smaller than this.  This check
433    * just limits the amount of memory we'll try and allocate in the
434    * function.  If the message is larger than the real limit, that
435    * will be caught later when we try to serialize the message.
436    */
437   if (count >= GUESTFS_MESSAGE_MAX) {
438     reply_with_error ("%s: count is too large for the protocol, use smaller reads", path);
439     return NULL;
440   }
441
442   CHROOT_IN;
443   fd = open (path, O_RDONLY);
444   CHROOT_OUT;
445
446   if (fd == -1) {
447     reply_with_perror ("open: %s", path);
448     return NULL;
449   }
450
451   buf = malloc (count);
452   if (buf == NULL) {
453     reply_with_perror ("malloc");
454     close (fd);
455     return NULL;
456   }
457
458   r = pread (fd, buf, count, offset);
459   if (r == -1) {
460     reply_with_perror ("pread: %s", path);
461     close (fd);
462     free (buf);
463     return NULL;
464   }
465
466   if (close (fd) == -1) {
467     reply_with_perror ("close: %s", path);
468     close (fd);
469     free (buf);
470     return NULL;
471   }
472
473   /* Mustn't touch *size_r until we are sure that we won't return any
474    * error (RHBZ#589039).
475    */
476   *size_r = r;
477   return buf;
478 }
479
480 int
481 do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
482 {
483   int fd;
484   ssize_t r;
485
486   CHROOT_IN;
487   fd = open (path, O_WRONLY);
488   CHROOT_OUT;
489
490   if (fd == -1) {
491     reply_with_perror ("open: %s", path);
492     return -1;
493   }
494
495   r = pwrite (fd, content, size, offset);
496   if (r == -1) {
497     reply_with_perror ("pwrite: %s", path);
498     close (fd);
499     return -1;
500   }
501
502   if (close (fd) == -1) {
503     reply_with_perror ("close: %s", path);
504     close (fd);
505     return -1;
506   }
507
508   return r;
509 }
510
511 /* This runs the 'file' command. */
512 char *
513 do_file (const char *path)
514 {
515   char *out, *err;
516   int r, freeit = 0;
517   char *buf;
518   int len;
519
520   if (STREQLEN (path, "/dev/", 5))
521     buf = (char *) path;
522   else {
523     buf = sysroot_path (path);
524     if (!buf) {
525       reply_with_perror ("malloc");
526       return NULL;
527     }
528     freeit = 1;
529   }
530
531   /* file(1) manpage claims "file returns 0 on success, and non-zero on
532    * error", but this is evidently not true.  It always returns 0, in
533    * every scenario I can think up.  So check the target is readable
534    * first.
535    */
536   if (access (buf, R_OK) == -1) {
537     if (freeit) free (buf);
538     reply_with_perror ("access: %s", path);
539     return NULL;
540   }
541
542   r = command (&out, &err, "file", "-zbsL", buf, NULL);
543   if (freeit) free (buf);
544
545   if (r == -1) {
546     free (out);
547     reply_with_error ("%s: %s", path, err);
548     free (err);
549     return NULL;
550   }
551   free (err);
552
553   /* We need to remove the trailing \n from output of file(1). */
554   len = strlen (out);
555   if (out[len-1] == '\n')
556     out[len-1] = '\0';
557
558   return out;                   /* caller frees */
559 }
560
561 /* zcat | file */
562 char *
563 do_zfile (const char *method, const char *path)
564 {
565   int len;
566   const char *zcat;
567   char *cmd;
568   FILE *fp;
569   char line[256];
570
571   if (STREQ (method, "gzip") || STREQ (method, "compress"))
572     zcat = "zcat";
573   else if (STREQ (method, "bzip2"))
574     zcat = "bzcat";
575   else {
576     reply_with_error ("unknown method");
577     return NULL;
578   }
579
580   if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
581     reply_with_perror ("asprintf");
582     return NULL;
583   }
584
585   if (verbose)
586     fprintf (stderr, "%s\n", cmd);
587
588   fp = popen (cmd, "r");
589   if (fp == NULL) {
590     reply_with_perror ("%s", cmd);
591     free (cmd);
592     return NULL;
593   }
594
595   free (cmd);
596
597   if (fgets (line, sizeof line, fp) == NULL) {
598     reply_with_perror ("fgets");
599     fclose (fp);
600     return NULL;
601   }
602
603   if (fclose (fp) == -1) {
604     reply_with_perror ("fclose");
605     return NULL;
606   }
607
608   len = strlen (line);
609   if (len > 0 && line[len-1] == '\n')
610     line[len-1] = '\0';
611
612   return strdup (line);
613 }
614
615 int64_t
616 do_filesize (const char *path)
617 {
618   int r;
619   struct stat buf;
620
621   CHROOT_IN;
622   r = stat (path, &buf);        /* follow symlinks */
623   CHROOT_OUT;
624
625   if (r == -1) {
626     reply_with_perror ("%s", path);
627     return -1;
628   }
629
630   return buf.st_size;
631 }