1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009-2011 Red Hat Inc.
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.
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.
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.
28 #include "guestfs_protocol.h"
33 do_touch (const char *path)
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.
43 r = lstat (path, &buf);
47 if (errno != ENOENT) {
48 reply_with_perror ("lstat: %s", path);
52 if (! S_ISREG (buf.st_mode)) {
53 reply_with_error ("%s: touch can only be used on a regular files", path);
59 fd = open (path, O_WRONLY | O_CREAT | O_NOCTTY, 0666);
63 reply_with_perror ("open: %s", path);
67 r = futimens (fd, NULL);
69 reply_with_perror ("futimens: %s", path);
74 if (close (fd) == -1) {
75 reply_with_perror ("close: %s", path);
83 do_cat (const char *path)
86 int alloc, size, r, max;
90 fd = open (path, O_RDONLY);
94 reply_with_perror ("open: %s", path);
98 /* Read up to GUESTFS_MESSAGE_MAX - <overhead> bytes. If it's
99 * larger than that, we need to return an error instead (for
102 max = GUESTFS_MESSAGE_MAX - 1000;
110 reply_with_error ("%s: file is too large for message buffer",
116 buf2 = realloc (buf, alloc);
118 reply_with_perror ("realloc");
126 r = read (fd, buf + size, alloc - size);
128 reply_with_perror ("read: %s", path);
141 if (close (fd) == -1) {
142 reply_with_perror ("close: %s", path);
147 return buf; /* caller will free */
151 do_read_lines (const char *path)
154 int size = 0, alloc = 0;
161 fp = fopen (path, "r");
165 reply_with_perror ("fopen: %s", path);
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')
173 else if (n >= 1 && line[n-1] == '\n')
176 if (add_string (&r, &size, &alloc, line) == -1) {
185 if (add_string (&r, &size, &alloc, NULL) == -1) {
190 if (fclose (fp) == EOF) {
191 reply_with_perror ("fclose: %s", path);
200 do_rm (const char *path)
209 reply_with_perror ("%s", path);
217 do_chmod (int mode, const char *path)
222 reply_with_error ("%s: mode is negative", path);
227 r = chmod (path, mode);
231 reply_with_perror ("%s: 0%o", path, mode);
239 do_chown (int owner, int group, const char *path)
244 r = chown (path, owner, group);
248 reply_with_perror ("%s: %d.%d", path, owner, group);
256 do_lchown (int owner, int group, const char *path)
261 r = lchown (path, owner, group);
265 reply_with_perror ("%s: %d.%d", path, owner, group);
273 do_write_file (const char *path, const char *content, int size)
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).
285 reply_with_error ("size cannot be negative");
289 /* Note content_len must be small because of the limits on protocol
292 int content_len = (int) strlen (content);
296 else if (size > content_len) {
297 reply_with_error ("size parameter is larger than string content");
302 fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
306 reply_with_perror ("open: %s", path);
310 if (xwrite (fd, content, size) == -1) {
311 reply_with_perror ("write");
316 if (close (fd) == -1) {
317 reply_with_perror ("close: %s", path);
325 do_write (const char *path, const char *content, size_t size)
330 fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
334 reply_with_perror ("open: %s", path);
338 if (xwrite (fd, content, size) == -1) {
339 reply_with_perror ("write");
344 if (close (fd) == -1) {
345 reply_with_perror ("close: %s", path);
353 do_write_append (const char *path, const char *content, size_t size)
358 fd = open (path, O_WRONLY | O_APPEND | O_CREAT | O_NOCTTY, 0666);
362 reply_with_perror ("open: %s", path);
366 if (xwrite (fd, content, size) == -1) {
367 reply_with_perror ("write");
372 if (close (fd) == -1) {
373 reply_with_perror ("close: %s", path);
381 do_read_file (const char *path, size_t *size_r)
388 fd = open (path, O_RDONLY);
392 reply_with_perror ("open: %s", path);
396 if (fstat (fd, &statbuf) == -1) {
397 reply_with_perror ("fstat: %s", path);
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.
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);
412 r = malloc (statbuf.st_size);
414 reply_with_perror ("malloc");
419 if (xread (fd, r, statbuf.st_size) == -1) {
420 reply_with_perror ("read: %s", path);
426 if (close (fd) == -1) {
427 reply_with_perror ("close: %s", path);
432 /* Mustn't touch *size_r until we are sure that we won't return any
433 * error (RHBZ#589039).
435 *size_r = statbuf.st_size;
440 pread_fd (int fd, int count, int64_t offset, size_t *size_r,
441 const char *display_path)
447 reply_with_error ("count is negative");
453 reply_with_error ("offset is negative");
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.
463 if (count >= GUESTFS_MESSAGE_MAX) {
464 reply_with_error ("%s: count is too large for the protocol, use smaller reads", display_path);
469 buf = malloc (count);
471 reply_with_perror ("malloc");
476 r = pread (fd, buf, count, offset);
478 reply_with_perror ("pread: %s", display_path);
484 if (close (fd) == -1) {
485 reply_with_perror ("close: %s", display_path);
491 /* Mustn't touch *size_r until we are sure that we won't return any
492 * error (RHBZ#589039).
499 do_pread (const char *path, int count, int64_t offset, size_t *size_r)
504 fd = open (path, O_RDONLY);
508 reply_with_perror ("open: %s", path);
512 return pread_fd (fd, count, offset, size_r, path);
516 do_pread_device (const char *device, int count, int64_t offset, size_t *size_r)
518 int fd = open (device, O_RDONLY);
520 reply_with_perror ("open: %s", device);
524 return pread_fd (fd, count, offset, size_r, device);
528 pwrite_fd (int fd, const char *content, size_t size, int64_t offset,
529 const char *display_path)
533 r = pwrite (fd, content, size, offset);
535 reply_with_perror ("pwrite: %s", display_path);
540 if (close (fd) == -1) {
541 reply_with_perror ("close: %s", display_path);
550 do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
555 reply_with_error ("offset is negative");
560 fd = open (path, O_WRONLY);
564 reply_with_perror ("open: %s", path);
568 return pwrite_fd (fd, content, size, offset, path);
572 do_pwrite_device (const char *device, const char *content, size_t size,
576 reply_with_error ("offset is negative");
580 int fd = open (device, O_WRONLY);
582 reply_with_perror ("open: %s", device);
586 return pwrite_fd (fd, content, size, offset, device);
589 /* This runs the 'file' command. */
591 do_file (const char *path)
594 const char *display_path = path;
596 int is_dev = STRPREFIX (path, "/dev/");
599 buf = sysroot_path (path);
601 reply_with_perror ("malloc");
606 /* For non-dev, check this is a regular file, else just return the
607 * file type as a string (RHBZ#582484).
610 if (lstat (path, &statbuf) == -1) {
611 reply_with_perror ("lstat: %s", display_path);
616 if (! S_ISREG (statbuf.st_mode)) {
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");
634 ret = strdup ("unknown, not regular file");
637 reply_with_perror ("strdup");
642 /* Which flags to use? For /dev paths, follow links because
643 * /dev/VG/LV is a symbolic link.
645 const char *flags = is_dev ? "-zbsL" : "-zb";
648 int r = command (&out, &err, "file", flags, path, NULL);
653 reply_with_error ("%s: %s", display_path, err);
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')
664 return out; /* caller frees */
669 do_zfile (const char *method, const char *path)
677 if (STREQ (method, "gzip") || STREQ (method, "compress"))
679 else if (STREQ (method, "bzip2"))
682 reply_with_error ("unknown method");
686 if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
687 reply_with_perror ("asprintf");
692 fprintf (stderr, "%s\n", cmd);
694 fp = popen (cmd, "r");
696 reply_with_perror ("%s", cmd);
703 if (fgets (line, sizeof line, fp) == NULL) {
704 reply_with_perror ("fgets");
709 if (fclose (fp) == -1) {
710 reply_with_perror ("fclose");
715 if (len > 0 && line[len-1] == '\n')
718 return strdup (line);
722 do_filesize (const char *path)
728 r = stat (path, &buf); /* follow symlinks */
732 reply_with_perror ("%s", path);