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 fd = open (path, O_WRONLY | O_CREAT | O_NOCTTY, 0666);
43 reply_with_perror ("open: %s", path);
47 r = futimens (fd, NULL);
49 reply_with_perror ("futimens: %s", path);
54 if (close (fd) == -1) {
55 reply_with_perror ("close: %s", path);
63 do_cat (const char *path)
66 int alloc, size, r, max;
70 fd = open (path, O_RDONLY);
74 reply_with_perror ("open: %s", path);
78 /* Read up to GUESTFS_MESSAGE_MAX - <overhead> bytes. If it's
79 * larger than that, we need to return an error instead (for
82 max = GUESTFS_MESSAGE_MAX - 1000;
90 reply_with_error ("%s: file is too large for message buffer",
96 buf2 = realloc (buf, alloc);
98 reply_with_perror ("realloc");
106 r = read (fd, buf + size, alloc - size);
108 reply_with_perror ("read: %s", path);
121 if (close (fd) == -1) {
122 reply_with_perror ("close: %s", path);
127 return buf; /* caller will free */
131 do_read_lines (const char *path)
134 int size = 0, alloc = 0;
141 fp = fopen (path, "r");
145 reply_with_perror ("fopen: %s", path);
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')
153 else if (n >= 1 && line[n-1] == '\n')
156 if (add_string (&r, &size, &alloc, line) == -1) {
165 if (add_string (&r, &size, &alloc, NULL) == -1) {
170 if (fclose (fp) == EOF) {
171 reply_with_perror ("fclose: %s", path);
180 do_rm (const char *path)
189 reply_with_perror ("%s", path);
197 do_chmod (int mode, const char *path)
202 reply_with_error ("%s: mode is negative", path);
207 r = chmod (path, mode);
211 reply_with_perror ("%s: 0%o", path, mode);
219 do_chown (int owner, int group, const char *path)
224 r = chown (path, owner, group);
228 reply_with_perror ("%s: %d.%d", path, owner, group);
236 do_lchown (int owner, int group, const char *path)
241 r = lchown (path, owner, group);
245 reply_with_perror ("%s: %d.%d", path, owner, group);
253 do_write_file (const char *path, const char *content, int size)
257 /* This call is deprecated, and it has a broken interface. New code
258 * should use the 'guestfs_write' call instead. Because we used an
259 * XDR string type, 'content' cannot contain ASCII NUL and 'size'
260 * must never be longer than the string. We must check this to
261 * ensure random stuff from XDR or daemon memory isn't written to
262 * the file (RHBZ#597135).
265 reply_with_error ("size cannot be negative");
269 /* Note content_len must be small because of the limits on protocol
272 int content_len = (int) strlen (content);
276 else if (size > content_len) {
277 reply_with_error ("size parameter is larger than string content");
282 fd = open (path, O_WRONLY | O_TRUNC | O_CREAT | O_NOCTTY, 0666);
286 reply_with_perror ("open: %s", path);
290 if (xwrite (fd, content, size) == -1) {
291 reply_with_perror ("write");
296 if (close (fd) == -1) {
297 reply_with_perror ("close: %s", path);
305 do_read_file (const char *path, size_t *size_r)
312 fd = open (path, O_RDONLY);
316 reply_with_perror ("open: %s", path);
320 if (fstat (fd, &statbuf) == -1) {
321 reply_with_perror ("fstat: %s", path);
326 /* The actual limit on messages is smaller than this. This
327 * check just limits the amount of memory we'll try and allocate
328 * here. If the message is larger than the real limit, that will
329 * be caught later when we try to serialize the message.
331 if (statbuf.st_size >= GUESTFS_MESSAGE_MAX) {
332 reply_with_error ("%s: file is too large for the protocol, use guestfs_download instead", path);
336 r = malloc (statbuf.st_size);
338 reply_with_perror ("malloc");
343 if (xread (fd, r, statbuf.st_size) == -1) {
344 reply_with_perror ("read: %s", path);
350 if (close (fd) == -1) {
351 reply_with_perror ("close: %s", path);
356 /* Mustn't touch *size_r until we are sure that we won't return any
357 * error (RHBZ#589039).
359 *size_r = statbuf.st_size;
364 do_pread (const char *path, int count, int64_t offset, size_t *size_r)
370 /* The actual limit on messages is smaller than this. This check
371 * just limits the amount of memory we'll try and allocate in the
372 * function. If the message is larger than the real limit, that
373 * will be caught later when we try to serialize the message.
375 if (count >= GUESTFS_MESSAGE_MAX) {
376 reply_with_error ("%s: count is too large for the protocol, use smaller reads", path);
381 fd = open (path, O_RDONLY);
385 reply_with_perror ("open: %s", path);
389 buf = malloc (count);
391 reply_with_perror ("malloc");
396 r = pread (fd, buf, count, offset);
398 reply_with_perror ("pread: %s", path);
404 if (close (fd) == -1) {
405 reply_with_perror ("close: %s", path);
411 /* Mustn't touch *size_r until we are sure that we won't return any
412 * error (RHBZ#589039).
418 /* This runs the 'file' command. */
420 do_file (const char *path)
427 if (STREQLEN (path, "/dev/", 5))
430 buf = sysroot_path (path);
432 reply_with_perror ("malloc");
438 /* file(1) manpage claims "file returns 0 on success, and non-zero on
439 * error", but this is evidently not true. It always returns 0, in
440 * every scenario I can think up. So check the target is readable
443 if (access (buf, R_OK) == -1) {
444 if (freeit) free (buf);
445 reply_with_perror ("access: %s", path);
449 r = command (&out, &err, "file", "-zbsL", buf, NULL);
450 if (freeit) free (buf);
454 reply_with_error ("%s: %s", path, err);
460 /* We need to remove the trailing \n from output of file(1). */
462 if (out[len-1] == '\n')
465 return out; /* caller frees */
470 do_zfile (const char *method, const char *path)
478 if (STREQ (method, "gzip") || STREQ (method, "compress"))
480 else if (STREQ (method, "bzip2"))
483 reply_with_error ("unknown method");
487 if (asprintf_nowarn (&cmd, "%s %R | file -bsL -", zcat, path) == -1) {
488 reply_with_perror ("asprintf");
493 fprintf (stderr, "%s\n", cmd);
495 fp = popen (cmd, "r");
497 reply_with_perror ("%s", cmd);
504 if (fgets (line, sizeof line, fp) == NULL) {
505 reply_with_perror ("fgets");
510 if (fclose (fp) == -1) {
511 reply_with_perror ("fclose");
516 if (len > 0 && line[len-1] == '\n')
519 return strdup (line);
523 do_filesize (const char *path)
529 r = stat (path, &buf); /* follow symlinks */
533 reply_with_perror ("%s", path);