2011-03-11 12:44:19 +01:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdlib.h>
|
2014-01-06 18:31:12 +01:00
|
|
|
#include <libqubes-rpc-filecopy.h>
|
2011-03-11 12:44:19 +01:00
|
|
|
#include <unistd.h>
|
2018-05-26 01:39:07 +02:00
|
|
|
#include <getopt.h>
|
2011-03-15 13:12:21 +01:00
|
|
|
#include <gui-fatal.h>
|
2011-03-11 12:44:19 +01:00
|
|
|
#include "dvm2.h"
|
|
|
|
|
2018-05-26 01:41:16 +02:00
|
|
|
void send_file(const char *fname, int view_only)
|
2011-03-11 12:44:19 +01:00
|
|
|
{
|
2018-05-26 00:54:09 +02:00
|
|
|
const char *base;
|
2018-05-26 01:41:16 +02:00
|
|
|
char sendbuf[DVM_FILENAME_SIZE] = {0};
|
|
|
|
size_t sendbuf_size = DVM_FILENAME_SIZE;
|
2018-05-26 00:54:09 +02:00
|
|
|
int fd = open(fname, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
gui_fatal("open %s", fname);
|
2018-05-28 22:45:39 +02:00
|
|
|
|
|
|
|
_Static_assert(DVM_FILENAME_SIZE > sizeof(DVM_VIEW_ONLY_PREFIX),
|
|
|
|
"DVM_FILENAME_SIZE > sizeof(DVM_VIEW_ONLY_PREFIX)");
|
|
|
|
|
2018-05-26 01:41:16 +02:00
|
|
|
if (view_only) {
|
|
|
|
strncpy(sendbuf, DVM_VIEW_ONLY_PREFIX, sendbuf_size);
|
|
|
|
sendbuf_size -= strlen(DVM_VIEW_ONLY_PREFIX);
|
|
|
|
}
|
2018-05-26 00:54:09 +02:00
|
|
|
base = rindex(fname, '/');
|
|
|
|
if (!base)
|
|
|
|
base = fname;
|
|
|
|
else
|
|
|
|
base++;
|
2018-05-26 01:41:16 +02:00
|
|
|
if (strlen(base) >= sendbuf_size)
|
|
|
|
base += strlen(base) - sendbuf_size + 1;
|
|
|
|
strncat(sendbuf,base,sendbuf_size - 1); /* fills out with NULs */
|
|
|
|
sendbuf[DVM_FILENAME_SIZE - 1] = '\0';
|
2018-05-26 00:54:09 +02:00
|
|
|
if (!write_all(1, sendbuf, DVM_FILENAME_SIZE))
|
|
|
|
gui_fatal("send filename to dispVM");
|
|
|
|
if (!copy_fd_all(1, fd))
|
|
|
|
gui_fatal("send file to dispVM");
|
|
|
|
close(1);
|
|
|
|
close(fd);
|
2011-03-11 12:44:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int copy_and_return_nonemptiness(int tmpfd)
|
|
|
|
{
|
2018-05-26 00:54:09 +02:00
|
|
|
struct stat st;
|
|
|
|
if (!copy_fd_all(tmpfd, 0))
|
|
|
|
gui_fatal("receiving file from dispVM");
|
|
|
|
if (fstat(tmpfd, &st))
|
|
|
|
gui_fatal("fstat");
|
|
|
|
close(tmpfd);
|
2011-03-11 12:44:19 +01:00
|
|
|
|
2018-05-26 00:54:09 +02:00
|
|
|
return st.st_size > 0;
|
2011-03-11 12:44:19 +01:00
|
|
|
}
|
|
|
|
|
2013-12-30 11:35:46 +01:00
|
|
|
void recv_file_nowrite(const char *fname)
|
2011-03-11 12:44:19 +01:00
|
|
|
{
|
2018-05-26 00:54:09 +02:00
|
|
|
char *tempfile;
|
|
|
|
char *errmsg;
|
|
|
|
int tmpfd = -1;
|
2011-03-11 12:44:19 +01:00
|
|
|
|
2018-05-26 00:54:09 +02:00
|
|
|
if (asprintf(&tempfile, "/tmp/file_edited_in_dvm.XXXXXX") != -1)
|
|
|
|
tmpfd = mkstemp(tempfile);
|
|
|
|
if (tmpfd < 0)
|
|
|
|
gui_fatal("unable to create any temporary file, aborting");
|
|
|
|
if (!copy_and_return_nonemptiness(tmpfd)) {
|
|
|
|
unlink(tempfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (asprintf(&errmsg,
|
|
|
|
"The file %s has been edited in Disposable VM and the modified content has been received, "
|
|
|
|
"but this file is in nonwritable directory and thus cannot be modified safely. The edited file has been "
|
|
|
|
"saved to %s", fname, tempfile) != -1)
|
2014-04-22 00:56:52 +02:00
|
|
|
gui_nonfatal(errmsg);
|
2011-03-11 12:44:19 +01:00
|
|
|
}
|
|
|
|
|
2013-12-30 11:35:46 +01:00
|
|
|
void actually_recv_file(const char *fname, const char *tempfile, int tmpfd)
|
2011-03-11 12:44:19 +01:00
|
|
|
{
|
2018-05-26 00:54:09 +02:00
|
|
|
if (!copy_and_return_nonemptiness(tmpfd)) {
|
|
|
|
unlink(tempfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rename(tempfile, fname))
|
|
|
|
gui_fatal("rename");
|
2011-03-11 12:44:19 +01:00
|
|
|
}
|
|
|
|
|
2013-12-30 11:35:46 +01:00
|
|
|
void recv_file(const char *fname)
|
2011-03-11 12:44:19 +01:00
|
|
|
{
|
2018-05-26 00:54:09 +02:00
|
|
|
int tmpfd = -1;
|
|
|
|
char *tempfile;
|
|
|
|
if (asprintf(&tempfile, "%s.XXXXXX", fname) != -1) {
|
|
|
|
tmpfd = mkstemp(tempfile);
|
|
|
|
}
|
|
|
|
if (tmpfd < 0)
|
|
|
|
recv_file_nowrite(fname);
|
|
|
|
else
|
|
|
|
actually_recv_file(fname, tempfile, tmpfd);
|
2011-03-11 12:44:19 +01:00
|
|
|
}
|
|
|
|
|
2011-07-06 12:32:20 +02:00
|
|
|
int main(int argc, char ** argv)
|
2011-03-11 12:44:19 +01:00
|
|
|
{
|
2018-05-26 01:39:07 +02:00
|
|
|
char *fname;
|
|
|
|
int view_only = 0;
|
|
|
|
int ret;
|
|
|
|
const struct option opts[] = {
|
|
|
|
{"view-only", no_argument, &view_only, 1},
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((ret=getopt_long(argc, argv, "", opts, NULL)) != -1) {
|
|
|
|
if (ret == '?') {
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-26 00:54:09 +02:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2018-05-26 01:39:07 +02:00
|
|
|
|
|
|
|
if (optind >= argc)
|
2018-05-26 00:54:09 +02:00
|
|
|
gui_fatal("OpenInVM - no file given?");
|
2018-05-26 01:39:07 +02:00
|
|
|
fname = argv[optind];
|
2018-05-26 01:41:16 +02:00
|
|
|
send_file(fname, view_only);
|
2018-05-26 01:39:07 +02:00
|
|
|
if (!view_only) {
|
|
|
|
recv_file(fname);
|
|
|
|
} else {
|
|
|
|
/* discard received data */
|
|
|
|
int null_fd = open("/dev/null", O_WRONLY);
|
|
|
|
copy_fd_all(null_fd, 0);
|
|
|
|
close(null_fd);
|
|
|
|
}
|
2018-05-26 00:54:09 +02:00
|
|
|
return 0;
|
2015-10-15 04:34:55 +02:00
|
|
|
}
|