1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 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., 675 Mass Ave, Cambridge, MA 02139, 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_read_file (const char *path, size_t *size_r)
360 fd = open (path, O_RDONLY);
364 reply_with_perror ("open: %s", path);
368 if (fstat (fd, &statbuf) == -1) {
369 reply_with_perror ("fstat: %s", path);
374 /* The actual limit on messages is smaller than this. This
375 * check just limits the amount of memory we'll try and allocate
376 * here. If the message is larger than the real limit, that will
377 * be caught later when we try to serialize the message.
379 if (statbuf.st_size >= GUESTFS_MESSAGE_MAX) {
380 reply_with_error ("%s: file is too large for the protocol, use guestfs_download instead", path);
384 r = malloc (statbuf.st_size);
386 reply_with_perror ("malloc");
391 if (xread (fd, r, statbuf.st_size) == -1) {
392 reply_with_perror ("read: %s", path);
398 if (close (fd) == -1) {
399 reply_with_perror ("close: %s", path);
404 /* Mustn't touch *size_r until we are sure that we won't return any
405 * error (RHBZ#589039).
407 *size_r = statbuf.st_size;
412 pread_fd (int fd, int count, int64_t offset, size_t *size_r,
413 const char *display_path)
419 reply_with_error ("count is negative");
425 reply_with_error ("offset is negative");
430 /* The actual limit on messages is smaller than this. This check
431 * just limits the amount of memory we'll try and allocate in the
432 * function. If the message is larger than the real limit, that
433 * will be caught later when we try to serialize the message.
435 if (count >= GUESTFS_MESSAGE_MAX) {
436 reply_with_error ("%s: count is too large for the protocol, use smaller reads", display_path);
441 buf = malloc (count);
443 reply_with_perror ("malloc");
448 r = pread (fd, buf, count, offset);
450 reply_with_perror ("pread: %s", display_path);
456 if (close (fd) == -1) {
457 reply_with_perror ("close: %s", display_path);
463 /* Mustn't touch *size_r until we are sure that we won't return any
464 * error (RHBZ#589039).
471 do_pread (const char *path, int count, int64_t offset, size_t *size_r)
476 fd = open (path, O_RDONLY);
480 reply_with_perror ("open: %s", path);
484 return pread_fd (fd, count, offset, size_r, path);
488 do_pread_device (const char *device, int count, int64_t offset, size_t *size_r)
490 int fd = open (device, O_RDONLY);
492 reply_with_perror ("open: %s", device);
496 return pread_fd (fd, count, offset, size_r, device);
500 pwrite_fd (int fd, const char *content, size_t size, int64_t offset,
501 const char *display_path)
505 r = pwrite (fd, content, size, offset);
507 reply_with_perror ("pwrite: %s", display_path);
512 if (close (fd) == -1) {
513 reply_with_perror ("close: %s", display_path);
522 do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
527 reply_with_error ("offset is negative");
532 fd = open (path, O_WRONLY);
536 reply_with_perror ("open: %s", path);
540 return pwrite_fd (fd, content, size, offset, path);
544 do_pwrite_device (const char *device, const char *content, size_t size,
548 reply_with_error ("offset is negative");
552 int fd = open (device, O_WRONLY);
554 reply_with_perror ("open: %s", device);
558 return pwrite_fd (fd, content, size, offset, device);
561 /* This runs the 'file' command. */
563 do_file (const char *path)
566 const char *display_path = path;
568 int is_dev = STRPREFIX (path, "/dev/");
571 buf = sysroot_path (path);
573 reply_with_perror ("malloc");
578 /* For non-dev, check this is a regular file, else just return the
579 * file type as a string (RHBZ#582484).
582 if (lstat (path, &statbuf) == -1) {
583 reply_with_perror ("lstat: %s", display_path);
588 if (! S_ISREG (statbuf.st_mode)) {
593 if (S_ISDIR (statbuf.st_mode))
594 ret = strdup ("directory");
595 else if (S_ISCHR (statbuf.st_mode))
596 ret = strdup ("character device");
597 else if (S_ISBLK (statbuf.st_mode))
598 ret = strdup ("block device");
599 else if (S_ISFIFO (statbuf.st_mode))
600 ret = strdup ("FIFO");
601 else if (S_ISLNK (statbuf.st_mode))
602 ret = strdup ("symbolic link");
603 else if (S_ISSOCK (statbuf.st_mode))
604 ret = strdup ("socket");
606 ret = strdup ("unknown, not regular file");
609 reply_with_perror ("strdup");
614 /* Which flags to use? For /dev paths, follow links because
615 * /dev/VG/LV is a symbolic link.
617 const char *flags = is_dev ? "-zbsL" : "-zb";
620 int r = command (&out, &err, "file", flags, path, NULL);
625 reply_with_error ("%s: %s", display_path, err);
631 /* We need to remove the trailing \n from output of file(1). */
632 size_t len = strlen (out);
633 if (len > 0 && out[len-1] == '\n')
636 return out; /* caller frees */
641 do_zfile (const char *method, const char *path)
649 if (STREQ (method, "gzip") || STREQ (method, "compress"))
651 else if (STREQ (method, "bzip2"))
654 reply_with_error ("unknown method");
658 if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
659 reply_with_perror ("asprintf");
664 fprintf (stderr, "%s\n", cmd);
666 fp = popen (cmd, "r");
668 reply_with_perror ("%s", cmd);
675 if (fgets (line, sizeof line, fp) == NULL) {
676 reply_with_perror ("fgets");
681 if (fclose (fp) == -1) {
682 reply_with_perror ("fclose");
687 if (len > 0 && line[len-1] == '\n')
690 return strdup (line);
694 do_filesize (const char *path)
700 r = stat (path, &buf); /* follow symlinks */
704 reply_with_perror ("%s", path);