/* Detect if chown(2) is supported on the target directory. */ static int is_chown_supported (const char *dir) { CLEANUP_FREE char *buf = NULL; int fd, r, err, saved_errno; /* Create a randomly named file. */ if (asprintf (&buf, "%s%s/XXXXXXXX.XXX", sysroot, dir) == -1) { err = errno; r = cancel_receive (); errno = err; reply_with_perror ("asprintf"); return -1; } if (random_name (buf) == -1) { err = errno; r = cancel_receive (); errno = err; reply_with_perror ("random_name"); return -1; } /* Maybe 'dir' is not a directory or filesystem not writable? */ fd = open (buf, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0666); if (fd == -1) { err = errno; r = cancel_receive (); errno = err; reply_with_perror ("%s", dir); return -1; } /* This is the test. */ r = fchown (fd, 1000, 1000); saved_errno = errno; /* Make sure the test file is removed. */ close (fd); unlink (buf); if (r == -1 && saved_errno == EPERM) { /* This means chown is not supported by the filesystem. */ return 0; } if (r == -1) { /* Some other error? */ err = errno; r = cancel_receive (); errno = err; reply_with_perror_errno (saved_errno, "unexpected error in fchown"); return -1; } /* Else chown is supported. */ return 1; }
/* Has one FileIn parameter. */ int do_base64_in (const char *file) { int err, r; FILE *fp; CLEANUP_FREE char *cmd = NULL; int fd; if (asprintf_nowarn (&cmd, "%s -d -i > %R", str_base64, file) == -1) { err = errno; cancel_receive (); errno = err; reply_with_perror ("asprintf"); return -1; } if (verbose) fprintf (stderr, "%s\n", cmd); fp = popen (cmd, "w"); if (fp == NULL) { err = errno; cancel_receive (); errno = err; reply_with_perror ("%s", cmd); return -1; } /* The semantics of fwrite are too undefined, so write to the * file descriptor directly instead. */ fd = fileno (fp); r = receive_file (write_cb, &fd); if (r == -1) { /* write error */ cancel_receive (); reply_with_error ("write error on file: %s", file); pclose (fp); return -1; } if (r == -2) { /* cancellation from library */ /* This error is ignored by the library since it initiated the * cancel. Nevertheless we must send an error reply here. */ reply_with_error ("file upload cancelled"); pclose (fp); return -1; } if (pclose (fp) != 0) { reply_with_error ("base64 subcommand failed on file: %s", file); return -1; } return 0; }
/* Has one FileIn parameter. */ int do_internal_rhbz914931 (int count) { int r; if (count <= 0 || count > 1000) { reply_with_error ("count out of range"); return -1; } r = receive_file (crash_cb, &count); if (r == -1) { /* write error */ int err = errno; cancel_receive (); errno = err; reply_with_error ("write error"); return -1; } if (r == -2) { /* cancellation from library */ /* This error is ignored by the library since it initiated the * cancel. Nevertheless we must send an error reply here. */ reply_with_error ("file upload cancelled"); return -1; } return 0; }
/* Has one FileIn parameter. */ int do_debug_upload (const char *filename, int mode) { /* Not chrooted - this command lets you upload a file to anywhere * in the appliance. */ int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY|O_CLOEXEC, mode); if (fd == -1) { int err = errno; cancel_receive (); errno = err; reply_with_perror ("%s", filename); return -1; } int r = receive_file (write_cb, &fd); if (r == -1) { /* write error */ int err = errno; cancel_receive (); errno = err; reply_with_error ("write error: %s", filename); close (fd); return -1; } if (r == -2) { /* cancellation from library */ /* This error is ignored by the library since it initiated the * cancel. Nevertheless we must send an error reply here. */ reply_with_error ("file upload cancelled"); close (fd); return -1; } if (close (fd) == -1) { reply_with_perror ("close: %s", filename); return -1; } return 0; }
/* Takes optional arguments, consult optargs_bitmask. */ int do_tar_in (const char *dir, const char *compress) { const char *filter; int err, r; FILE *fp; CLEANUP_FREE char *cmd = NULL; char error_file[] = "/tmp/tarXXXXXX"; int fd, chown_supported; chown_supported = is_chown_supported (dir); if (chown_supported == -1) return -1; if ((optargs_bitmask & GUESTFS_TAR_IN_COMPRESS_BITMASK)) { if (STREQ (compress, "compress")) filter = " --compress"; else if (STREQ (compress, "gzip")) filter = " --gzip"; else if (STREQ (compress, "bzip2")) filter = " --bzip2"; else if (STREQ (compress, "xz")) filter = " --xz"; else if (STREQ (compress, "lzop")) filter = " --lzop"; else { reply_with_error ("unknown compression type: %s", compress); return -1; } } else filter = ""; fd = mkstemp (error_file); if (fd == -1) { reply_with_perror ("mkstemp"); return -1; } close (fd); /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */ if (asprintf_nowarn (&cmd, "%s -C %R%s -xf - %s2> %s", str_tar, dir, filter, chown_supported ? "" : "--no-same-owner ", error_file) == -1) { err = errno; r = cancel_receive (); errno = err; reply_with_perror ("asprintf"); unlink (error_file); return -1; } if (verbose) fprintf (stderr, "%s\n", cmd); fp = popen (cmd, "w"); if (fp == NULL) { err = errno; r = cancel_receive (); errno = err; reply_with_perror ("%s", cmd); unlink (error_file); return -1; } /* The semantics of fwrite are too undefined, so write to the * file descriptor directly instead. */ fd = fileno (fp); r = receive_file (write_cb, &fd); if (r == -1) { /* write error */ cancel_receive (); CLEANUP_FREE char *errstr = read_error_file (error_file); reply_with_error ("write error on directory: %s: %s", dir, errstr); unlink (error_file); pclose (fp); return -1; } if (r == -2) { /* cancellation from library */ /* This error is ignored by the library since it initiated the * cancel. Nevertheless we must send an error reply here. */ reply_with_error ("file upload cancelled"); pclose (fp); unlink (error_file); return -1; } if (pclose (fp) != 0) { CLEANUP_FREE char *errstr = read_error_file (error_file); reply_with_error ("tar subcommand failed on directory: %s: %s", dir, errstr); unlink (error_file); return -1; } unlink (error_file); return 0; }
/* Has one FileIn parameter. */ int do_ntfsclone_in (const char *device) { int err, r; FILE *fp; char *cmd; char error_file[] = "/tmp/ntfscloneXXXXXX"; int fd; fd = mkstemp (error_file); if (fd == -1) { reply_with_perror ("mkstemp"); return -1; } close (fd); /* Construct the command. */ if (asprintf_nowarn (&cmd, "ntfsclone -O %s --restore-image - 2> %s", device, error_file) == -1) { err = errno; r = cancel_receive (); errno = err; reply_with_perror ("asprintf"); unlink (error_file); return -1; } if (verbose) fprintf (stderr, "%s\n", cmd); fp = popen (cmd, "w"); if (fp == NULL) { err = errno; r = cancel_receive (); errno = err; reply_with_perror ("%s", cmd); unlink (error_file); free (cmd); return -1; } free (cmd); /* The semantics of fwrite are too undefined, so write to the * file descriptor directly instead. */ fd = fileno (fp); r = receive_file (write_cb, &fd); if (r == -1) { /* write error */ cancel_receive (); char *errstr = read_error_file (error_file); reply_with_error ("write error on device: %s: %s", device, errstr); free (errstr); unlink (error_file); pclose (fp); return -1; } if (r == -2) { /* cancellation from library */ /* This error is ignored by the library since it initiated the * cancel. Nevertheless we must send an error reply here. */ reply_with_error ("ntfsclone cancelled"); pclose (fp); unlink (error_file); return -1; } if (pclose (fp) != 0) { char *errstr = read_error_file (error_file); reply_with_error ("ntfsclone subcommand failed on device: %s: %s", device, errstr); free (errstr); unlink (error_file); return -1; } unlink (error_file); return 0; }