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 "../src/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_exists (const char *path)
278 r = access (path, F_OK);
285 do_is_file (const char *path)
291 r = lstat (path, &buf);
295 if (errno != ENOENT && errno != ENOTDIR) {
296 reply_with_perror ("stat: %s", path);
300 return 0; /* Not a file. */
303 return S_ISREG (buf.st_mode);
307 do_write_file (const char *path, const char *content, int size)
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).
319 reply_with_error ("size cannot be negative");
323 /* Note content_len must be small because of the limits on protocol
326 int content_len = (int) strlen (content);
330 else if (size > content_len) {
331 reply_with_error ("size parameter is larger than string content");
336 fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
340 reply_with_perror ("open: %s", path);
344 if (xwrite (fd, content, size) == -1) {
345 reply_with_perror ("write");
350 if (close (fd) == -1) {
351 reply_with_perror ("close: %s", path);
359 do_write (const char *path, const char *content, size_t size)
364 fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
368 reply_with_perror ("open: %s", path);
372 if (xwrite (fd, content, size) == -1) {
373 reply_with_perror ("write");
378 if (close (fd) == -1) {
379 reply_with_perror ("close: %s", path);
387 do_read_file (const char *path, size_t *size_r)
394 fd = open (path, O_RDONLY);
398 reply_with_perror ("open: %s", path);
402 if (fstat (fd, &statbuf) == -1) {
403 reply_with_perror ("fstat: %s", path);
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.
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);
418 r = malloc (statbuf.st_size);
420 reply_with_perror ("malloc");
425 if (xread (fd, r, statbuf.st_size) == -1) {
426 reply_with_perror ("read: %s", path);
432 if (close (fd) == -1) {
433 reply_with_perror ("close: %s", path);
438 /* Mustn't touch *size_r until we are sure that we won't return any
439 * error (RHBZ#589039).
441 *size_r = statbuf.st_size;
446 do_pread (const char *path, int count, int64_t offset, size_t *size_r)
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.
457 if (count >= GUESTFS_MESSAGE_MAX) {
458 reply_with_error ("%s: count is too large for the protocol, use smaller reads", path);
463 fd = open (path, O_RDONLY);
467 reply_with_perror ("open: %s", path);
471 buf = malloc (count);
473 reply_with_perror ("malloc");
478 r = pread (fd, buf, count, offset);
480 reply_with_perror ("pread: %s", path);
486 if (close (fd) == -1) {
487 reply_with_perror ("close: %s", path);
493 /* Mustn't touch *size_r until we are sure that we won't return any
494 * error (RHBZ#589039).
501 do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
507 fd = open (path, O_WRONLY);
511 reply_with_perror ("open: %s", path);
515 r = pwrite (fd, content, size, offset);
517 reply_with_perror ("pwrite: %s", path);
522 if (close (fd) == -1) {
523 reply_with_perror ("close: %s", path);
531 /* This runs the 'file' command. */
533 do_file (const char *path)
536 const char *display_path = path;
538 int is_dev = STRPREFIX (path, "/dev/");
541 buf = sysroot_path (path);
543 reply_with_perror ("malloc");
548 /* For non-dev, check this is a regular file, else just return the
549 * file type as a string (RHBZ#582484).
552 if (lstat (path, &statbuf) == -1) {
553 reply_with_perror ("lstat: %s", display_path);
558 if (! S_ISREG (statbuf.st_mode)) {
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");
576 ret = strdup ("unknown, not regular file");
579 reply_with_perror ("strdup");
584 /* Which flags to use? For /dev paths, follow links because
585 * /dev/VG/LV is a symbolic link.
587 const char *flags = is_dev ? "-zbsL" : "-zb";
590 int r = command (&out, &err, "file", flags, path, NULL);
595 reply_with_error ("%s: %s", display_path, err);
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')
606 return out; /* caller frees */
611 do_zfile (const char *method, const char *path)
619 if (STREQ (method, "gzip") || STREQ (method, "compress"))
621 else if (STREQ (method, "bzip2"))
624 reply_with_error ("unknown method");
628 if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
629 reply_with_perror ("asprintf");
634 fprintf (stderr, "%s\n", cmd);
636 fp = popen (cmd, "r");
638 reply_with_perror ("%s", cmd);
645 if (fgets (line, sizeof line, fp) == NULL) {
646 reply_with_perror ("fgets");
651 if (fclose (fp) == -1) {
652 reply_with_perror ("fclose");
657 if (len > 0 && line[len-1] == '\n')
660 return strdup (line);
664 do_filesize (const char *path)
670 r = stat (path, &buf); /* follow symlinks */
674 reply_with_perror ("%s", path);