2011-03-15 16:43:43 +01:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <grp.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <sys/stat.h>
|
2015-01-11 05:42:15 +01:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <fcntl.h>
|
2011-03-15 16:43:43 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/fsuid.h>
|
|
|
|
#include <gui-fatal.h>
|
|
|
|
#include <errno.h>
|
2013-03-20 06:21:16 +01:00
|
|
|
#include <libqubes-rpc-filecopy.h>
|
2020-01-07 16:54:19 +01:00
|
|
|
|
|
|
|
#define INCOMING_DIR_NAME "QubesIncoming"
|
|
|
|
|
|
|
|
char *prepare_creds_return_dir(int uid)
|
2011-03-15 16:43:43 +01:00
|
|
|
{
|
2018-05-26 00:54:09 +02:00
|
|
|
const struct passwd *pwd;
|
2020-01-07 16:54:19 +01:00
|
|
|
pwd = getpwuid(uid);
|
2018-05-26 00:54:09 +02:00
|
|
|
if (!pwd) {
|
2020-01-07 16:54:19 +01:00
|
|
|
perror("getpwuid");
|
2018-05-26 00:54:09 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
setenv("HOME", pwd->pw_dir, 1);
|
2020-01-07 16:54:19 +01:00
|
|
|
setenv("USER", pwd->pw_name, 1);
|
2018-05-26 00:54:09 +02:00
|
|
|
if (setgid(pwd->pw_gid) < 0)
|
|
|
|
gui_fatal("Error setting group permissions");
|
2020-01-07 16:54:19 +01:00
|
|
|
if (initgroups(pwd->pw_name, pwd->pw_gid) < 0)
|
2018-05-26 00:54:09 +02:00
|
|
|
gui_fatal("Error initializing groups");
|
|
|
|
if (setfsuid(pwd->pw_uid) < 0)
|
|
|
|
gui_fatal("Error setting filesystem level permissions");
|
2020-01-07 16:54:19 +01:00
|
|
|
return pwd->pw_dir;
|
2011-03-15 16:43:43 +01:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:28:23 +02:00
|
|
|
int main(int argc, char ** argv)
|
2011-03-15 16:43:43 +01:00
|
|
|
{
|
2020-01-07 16:54:19 +01:00
|
|
|
char *home_dir;
|
|
|
|
char *incoming_dir_root;
|
2018-05-26 00:54:09 +02:00
|
|
|
char *incoming_dir;
|
|
|
|
int uid, ret;
|
|
|
|
pid_t pid;
|
|
|
|
const char *remote_domain;
|
|
|
|
char *procdir_path;
|
|
|
|
int procfs_fd;
|
2019-09-03 19:28:23 +02:00
|
|
|
int i;
|
2011-03-15 16:43:43 +01:00
|
|
|
|
2019-09-03 19:28:23 +02:00
|
|
|
if (argc >= 3) {
|
|
|
|
errno = 0;
|
|
|
|
uid = strtol(argv[1], NULL, 10);
|
|
|
|
if (errno)
|
|
|
|
gui_fatal("Invalid user ID argument");
|
|
|
|
home_dir = prepare_creds_return_dir(uid);
|
|
|
|
incoming_dir = argv[2];
|
|
|
|
} else {
|
|
|
|
uid = getuid();
|
|
|
|
home_dir = prepare_creds_return_dir(uid);
|
|
|
|
remote_domain = getenv("QREXEC_REMOTE_DOMAIN");
|
|
|
|
if (!remote_domain) {
|
|
|
|
gui_fatal("Cannot get remote domain name");
|
|
|
|
}
|
2011-03-15 16:43:43 +01:00
|
|
|
|
2019-09-03 19:28:23 +02:00
|
|
|
if (asprintf(&incoming_dir_root, "%s/%s", home_dir, INCOMING_DIR_NAME) < 0) {
|
|
|
|
gui_fatal("Error allocating memory");
|
|
|
|
}
|
|
|
|
mkdir(incoming_dir_root, 0700);
|
|
|
|
if (asprintf(&incoming_dir, "%s/%s", incoming_dir_root, remote_domain) < 0)
|
|
|
|
gui_fatal("Error allocating memory");
|
|
|
|
mkdir(incoming_dir, 0700);
|
2018-05-26 00:54:09 +02:00
|
|
|
}
|
2020-01-07 16:54:19 +01:00
|
|
|
|
2019-09-03 19:28:23 +02:00
|
|
|
for (i = 3; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "-v") == 0)
|
|
|
|
set_verbose(1);
|
2019-09-06 17:13:43 +02:00
|
|
|
else if (strcmp(argv[i], "-w") == 0)
|
|
|
|
if (i+1 < argc && argv[i+1][0] != '-') {
|
|
|
|
set_wait_for_space(atoi(argv[i+1]));
|
|
|
|
i++;
|
|
|
|
} else
|
|
|
|
set_wait_for_space(1);
|
2019-09-03 19:28:23 +02:00
|
|
|
else
|
|
|
|
gui_fatal("Invalid option %s", argv[i]);
|
2020-01-07 16:54:19 +01:00
|
|
|
}
|
2019-09-03 19:28:23 +02:00
|
|
|
|
2018-05-26 00:54:09 +02:00
|
|
|
if (chdir(incoming_dir))
|
|
|
|
gui_fatal("Error chdir to %s", incoming_dir);
|
2015-01-11 05:42:15 +01:00
|
|
|
|
2018-05-26 00:54:09 +02:00
|
|
|
if (mount(".", ".", NULL, MS_BIND | MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL) < 0)
|
|
|
|
gui_fatal("Failed to mount a directory %s", incoming_dir);
|
2015-01-11 05:42:15 +01:00
|
|
|
|
2018-05-26 00:54:09 +02:00
|
|
|
/* parse the input in unprivileged child process, parent will hold root
|
|
|
|
* access to unmount incoming dir */
|
|
|
|
switch (pid=fork()) {
|
|
|
|
case -1:
|
|
|
|
gui_fatal("Failed to create new process");
|
|
|
|
case 0:
|
|
|
|
if (asprintf(&procdir_path, "/proc/%d/fd", getpid()) < 0) {
|
|
|
|
gui_fatal("Error allocating memory");
|
|
|
|
}
|
|
|
|
procfs_fd = open(procdir_path, O_DIRECTORY | O_RDONLY);
|
|
|
|
if (procfs_fd < 0)
|
|
|
|
perror("Failed to open /proc");
|
|
|
|
else
|
|
|
|
set_procfs_fd(procfs_fd);
|
|
|
|
free(procdir_path);
|
2015-01-11 05:42:15 +01:00
|
|
|
|
2018-05-26 00:54:09 +02:00
|
|
|
if (chroot("."))
|
|
|
|
gui_fatal("Error chroot to %s", incoming_dir);
|
|
|
|
if (setuid(uid) < 0) {
|
|
|
|
/* no kdialog inside chroot */
|
|
|
|
perror("setuid");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return do_unpack();
|
|
|
|
}
|
|
|
|
if (waitpid(pid, &ret, 0) < 0) {
|
|
|
|
gui_fatal("Failed to wait for child process");
|
|
|
|
}
|
|
|
|
if (umount2(".", MNT_DETACH) < 0)
|
|
|
|
gui_fatal("Cannot umount incoming directory");
|
|
|
|
if (!WIFEXITED(ret)) {
|
|
|
|
gui_fatal("Child process exited abnormally");
|
|
|
|
}
|
|
|
|
return WEXITSTATUS(ret);
|
2011-03-15 16:43:43 +01:00
|
|
|
}
|