2011-03-10 16:50:40 +01:00
|
|
|
#include <sys/stat.h>
|
2011-12-27 17:04:30 +01:00
|
|
|
#include <sys/wait.h>
|
2013-12-29 14:05:44 +01:00
|
|
|
#include <sys/time.h>
|
2011-03-10 16:50:40 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2013-12-29 13:45:51 +01:00
|
|
|
#include <errno.h>
|
2014-01-06 18:31:12 +01:00
|
|
|
#include <libqubes-rpc-filecopy.h>
|
2011-03-10 16:50:40 +01:00
|
|
|
#include "dvm2.h"
|
|
|
|
|
2013-11-14 21:38:27 +01:00
|
|
|
#define USER_HOME "/home/user"
|
2013-12-29 13:45:51 +01:00
|
|
|
#define TMP_LOC "/tmp/qopen/"
|
2013-11-14 21:38:27 +01:00
|
|
|
|
2014-05-04 19:16:30 +02:00
|
|
|
static const char *cleanup_filename = NULL;
|
|
|
|
|
|
|
|
static void cleanup_file(void)
|
|
|
|
{
|
|
|
|
if (cleanup_filename) {
|
|
|
|
if (unlink(cleanup_filename) < 0)
|
|
|
|
fprintf(stderr, "Failed to remove file at exit\n");
|
|
|
|
cleanup_filename = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 11:35:46 +01:00
|
|
|
const char *gettime(void)
|
2013-02-25 06:48:29 +01:00
|
|
|
{
|
|
|
|
static char retbuf[60];
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
2013-12-29 13:05:35 +01:00
|
|
|
snprintf(retbuf, sizeof(retbuf), "%lld.%06lld",
|
2013-02-25 06:48:29 +01:00
|
|
|
(long long) tv.tv_sec, (long long) tv.tv_usec);
|
|
|
|
return retbuf;
|
|
|
|
}
|
|
|
|
|
2013-12-29 13:45:51 +01:00
|
|
|
static char *get_directory(void)
|
|
|
|
{
|
|
|
|
const char *remote_domain;
|
|
|
|
char *dir;
|
|
|
|
size_t len;
|
|
|
|
struct stat dstat;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
remote_domain = getenv("QREXEC_REMOTE_DOMAIN");
|
|
|
|
if (!remote_domain) {
|
|
|
|
fprintf(stderr, "Cannot get remote domain name\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!*remote_domain || index(remote_domain, '/'))
|
|
|
|
goto fail;
|
|
|
|
if (!strcmp(remote_domain, ".") || !strcmp(remote_domain, ".."))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
len = strlen("/tmp")+1+strlen(remote_domain)+1;
|
|
|
|
dir = malloc(len);
|
|
|
|
if (!dir) {
|
|
|
|
fprintf(stderr, "Cannot allocate memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
snprintf(dir, len, "/tmp/%s", remote_domain);
|
|
|
|
|
|
|
|
ret=mkdir(dir, 0777);
|
|
|
|
if (ret<0 && errno!=EEXIST) {
|
|
|
|
perror("mkdir");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (stat(dir, &dstat)) {
|
|
|
|
perror("stat dir");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(dstat.st_mode)) {
|
|
|
|
fprintf(stderr, "%s exists and is not a directory\n", dir);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
fprintf(stderr, "Invalid remote domain name: %s\n", remote_domain);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *get_filename(void)
|
2011-03-10 16:50:40 +01:00
|
|
|
{
|
|
|
|
char buf[DVM_FILENAME_SIZE];
|
2013-12-29 13:45:51 +01:00
|
|
|
static char *retname;
|
2012-01-11 19:08:15 +01:00
|
|
|
int i;
|
2013-12-29 13:45:51 +01:00
|
|
|
char *directory;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
directory = get_directory();
|
2011-03-10 16:50:40 +01:00
|
|
|
if (!read_all(0, buf, sizeof(buf)))
|
|
|
|
exit(1);
|
2013-12-29 13:06:26 +01:00
|
|
|
buf[DVM_FILENAME_SIZE-1] = 0;
|
2011-03-10 16:50:40 +01:00
|
|
|
if (index(buf, '/')) {
|
|
|
|
fprintf(stderr, "filename contains /");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-12-29 13:06:26 +01:00
|
|
|
for (i=0; buf[i]!=0; i++) {
|
2012-01-11 19:08:15 +01:00
|
|
|
// replace some characters with _ (eg mimeopen have problems with some of them)
|
2013-12-29 13:33:39 +01:00
|
|
|
if (index(" !?\"#$%^&*()[]<>;`~|", buf[i]))
|
2012-01-11 19:08:15 +01:00
|
|
|
buf[i]='_';
|
|
|
|
}
|
2013-12-29 13:45:51 +01:00
|
|
|
len = strlen(directory)+1+strlen(buf)+1;
|
|
|
|
retname = malloc(len);
|
|
|
|
if (!retname) {
|
|
|
|
fprintf(stderr, "Cannot allocate memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
snprintf(retname, len, "%s/%s", directory, buf);
|
|
|
|
free(directory);
|
2011-03-10 16:50:40 +01:00
|
|
|
return retname;
|
|
|
|
}
|
|
|
|
|
2014-01-06 18:31:12 +01:00
|
|
|
void copy_file_by_name(const char *filename)
|
2011-03-10 16:50:40 +01:00
|
|
|
{
|
2013-12-29 13:08:50 +01:00
|
|
|
int fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
2011-03-10 16:50:40 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("open file");
|
|
|
|
exit(1);
|
|
|
|
}
|
2014-05-04 19:16:30 +02:00
|
|
|
/* we now have created a new file, ensure we delete it at the end */
|
|
|
|
cleanup_filename = filename;
|
|
|
|
atexit(cleanup_file);
|
2011-03-11 11:54:39 +01:00
|
|
|
if (!copy_fd_all(fd, 0))
|
|
|
|
exit(1);
|
2011-03-10 16:50:40 +01:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2013-12-30 11:35:46 +01:00
|
|
|
void send_file_back(const char * filename)
|
2011-03-10 16:50:40 +01:00
|
|
|
{
|
|
|
|
int fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open file");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-03-11 11:54:39 +01:00
|
|
|
if (!copy_fd_all(1, fd))
|
|
|
|
exit(1);
|
2011-03-10 16:50:40 +01:00
|
|
|
close(fd);
|
2013-12-29 13:10:15 +01:00
|
|
|
close(1);
|
2011-03-10 16:50:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
2012-01-02 03:59:14 +01:00
|
|
|
struct stat stat_pre, stat_post, session_stat;
|
2013-12-29 13:45:51 +01:00
|
|
|
char *filename = get_filename();
|
2012-09-27 02:06:26 +02:00
|
|
|
int child, status, log_fd, null_fd;
|
2012-01-02 03:59:14 +01:00
|
|
|
FILE *waiter_pidfile;
|
2011-03-10 16:50:40 +01:00
|
|
|
|
2014-01-06 18:31:12 +01:00
|
|
|
copy_file_by_name(filename);
|
2011-03-10 16:50:40 +01:00
|
|
|
if (stat(filename, &stat_pre)) {
|
|
|
|
perror("stat pre");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-02-25 06:48:29 +01:00
|
|
|
fprintf(stderr, "time=%s, waiting for qubes-session\n", gettime());
|
2012-01-02 03:59:14 +01:00
|
|
|
// wait for X server to starts (especially in DispVM)
|
|
|
|
if (stat("/tmp/qubes-session-env", &session_stat)) {
|
|
|
|
switch (child = fork()) {
|
|
|
|
case -1:
|
|
|
|
perror("fork");
|
|
|
|
exit(1);
|
|
|
|
case 0:
|
|
|
|
waiter_pidfile = fopen("/tmp/qubes-session-waiter", "a");
|
|
|
|
if (waiter_pidfile == NULL) {
|
|
|
|
perror("fopen waiter_pidfile");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fprintf(waiter_pidfile, "%d\n", getpid());
|
|
|
|
fclose(waiter_pidfile);
|
|
|
|
// check the second time, to prevent race
|
|
|
|
if (stat("/tmp/qubes-session-env", &session_stat)) {
|
|
|
|
// wait for qubes-session notify
|
|
|
|
pause();
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
default:
|
|
|
|
waitpid(child, &status, 0);
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
|
|
|
|
//propagate exit code from child
|
|
|
|
exit(WEXITSTATUS(status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-25 06:48:29 +01:00
|
|
|
fprintf(stderr, "time=%s, starting editor\n", gettime());
|
2011-10-17 02:08:53 +02:00
|
|
|
switch (child = fork()) {
|
|
|
|
case -1:
|
|
|
|
perror("fork");
|
|
|
|
exit(1);
|
|
|
|
case 0:
|
2012-09-27 02:06:26 +02:00
|
|
|
null_fd = open("/dev/null", O_RDONLY);
|
|
|
|
dup2(null_fd, 0);
|
2015-10-15 04:34:55 +02:00
|
|
|
close(null_fd);
|
2012-07-12 03:45:20 +02:00
|
|
|
|
2011-10-17 02:08:53 +02:00
|
|
|
log_fd = open("/tmp/mimeopen.log", O_CREAT | O_APPEND, 0666);
|
|
|
|
if (log_fd == -1) {
|
|
|
|
perror("open /tmp/mimeopen.log");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
dup2(log_fd, 1);
|
|
|
|
close(log_fd);
|
|
|
|
|
2013-11-14 21:38:27 +01:00
|
|
|
setenv("HOME", USER_HOME, 1);
|
2011-10-17 02:08:53 +02:00
|
|
|
setenv("DISPLAY", ":0", 1);
|
2016-02-01 12:15:09 +01:00
|
|
|
execl("/usr/bin/qubes-open", "qubes-open", filename, (char*)NULL);
|
2011-10-17 02:08:53 +02:00
|
|
|
perror("execl");
|
|
|
|
exit(1);
|
|
|
|
default:
|
|
|
|
waitpid(child, &status, 0);
|
|
|
|
if (status != 0) {
|
2012-01-02 04:00:54 +01:00
|
|
|
char cmd[512];
|
2011-03-31 13:26:41 +02:00
|
|
|
#ifdef USE_KDIALOG
|
2012-01-02 04:00:54 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
|
|
|
"HOME=/home/user DISPLAY=:0 /usr/bin/kdialog --sorry 'Unable to handle mimetype of the requested file (exit status: %d)!' > /tmp/kdialog.log 2>&1 </dev/null", status);
|
|
|
|
("HOME=/home/user DISPLAY=:0 /usr/bin/kdialog --sorry 'Unable to handle mimetype of the requested file (exit status: %d)!' > /tmp/kdialog.log 2>&1 </dev/null", status);
|
2011-03-31 13:26:41 +02:00
|
|
|
#else
|
2012-01-02 04:00:54 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
|
|
|
"HOME=/home/user DISPLAY=:0 /usr/bin/zenity --error --text 'Unable to handle mimetype of the requested file (exit status: %d)!' > /tmp/kdialog.log 2>&1 </dev/null", status);
|
2011-03-31 13:26:41 +02:00
|
|
|
#endif
|
2014-04-22 00:56:52 +02:00
|
|
|
status = system(cmd);
|
2011-10-17 02:08:53 +02:00
|
|
|
}
|
|
|
|
}
|
2011-03-31 13:26:41 +02:00
|
|
|
|
2011-03-10 16:50:40 +01:00
|
|
|
if (stat(filename, &stat_post)) {
|
|
|
|
perror("stat post");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (stat_pre.st_mtime != stat_post.st_mtime)
|
|
|
|
send_file_back(filename);
|
2013-12-29 13:45:51 +01:00
|
|
|
free(filename);
|
2011-03-10 16:50:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|