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>
|
2013-02-23 13:11:59 +01:00
|
|
|
#define INCOMING_DIR_ROOT "/home/user/QubesIncoming"
|
2013-12-28 12:49:30 +01:00
|
|
|
int prepare_creds_return_uid(const char *username)
|
2011-03-15 16:43:43 +01:00
|
|
|
{
|
2013-12-30 11:35:46 +01:00
|
|
|
const struct passwd *pwd;
|
2011-03-15 16:43:43 +01:00
|
|
|
pwd = getpwnam(username);
|
|
|
|
if (!pwd) {
|
|
|
|
perror("getpwnam");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
setenv("HOME", pwd->pw_dir, 1);
|
|
|
|
setenv("USER", username, 1);
|
2013-12-28 12:50:18 +01:00
|
|
|
if (setgid(pwd->pw_gid) < 0)
|
|
|
|
gui_fatal("Error setting group permissions");
|
|
|
|
if (initgroups(username, pwd->pw_gid) < 0)
|
|
|
|
gui_fatal("Error initializing groups");
|
|
|
|
if (setfsuid(pwd->pw_uid) < 0)
|
|
|
|
gui_fatal("Error setting filesystem level permissions");
|
2011-03-15 16:43:43 +01:00
|
|
|
return pwd->pw_uid;
|
|
|
|
}
|
|
|
|
|
2014-02-16 11:34:22 +01:00
|
|
|
int main(int argc __attribute((__unused__)), char ** argv __attribute__((__unused__)))
|
2011-03-15 16:43:43 +01:00
|
|
|
{
|
|
|
|
char *incoming_dir;
|
2015-01-11 05:42:15 +01:00
|
|
|
int uid, ret;
|
|
|
|
pid_t pid;
|
2013-12-28 12:49:30 +01:00
|
|
|
const char *remote_domain;
|
2015-01-11 05:42:15 +01:00
|
|
|
char *procdir_path;
|
|
|
|
int procfs_fd;
|
2011-03-15 16:43:43 +01:00
|
|
|
|
|
|
|
uid = prepare_creds_return_uid("user");
|
|
|
|
|
2012-07-14 22:54:23 +02:00
|
|
|
remote_domain = getenv("QREXEC_REMOTE_DOMAIN");
|
|
|
|
if (!remote_domain) {
|
|
|
|
gui_fatal("Cannot get remote domain name");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-03-15 16:43:43 +01:00
|
|
|
mkdir(INCOMING_DIR_ROOT, 0700);
|
2013-12-28 12:50:18 +01:00
|
|
|
if (asprintf(&incoming_dir, "%s/%s", INCOMING_DIR_ROOT, remote_domain) < 0)
|
|
|
|
gui_fatal("Error allocating memory");
|
2011-03-15 16:43:43 +01:00
|
|
|
mkdir(incoming_dir, 0700);
|
|
|
|
if (chdir(incoming_dir))
|
|
|
|
gui_fatal("Error chdir to %s", incoming_dir);
|
2015-01-11 05:42:15 +01:00
|
|
|
|
|
|
|
if (mount(".", ".", NULL, MS_BIND | MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL) < 0)
|
|
|
|
gui_fatal("Failed to mount a directory %s", incoming_dir);
|
|
|
|
|
|
|
|
/* 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:
|
2015-01-23 00:15:01 +01:00
|
|
|
if (asprintf(&procdir_path, "/proc/%d/fd", getpid()) < 0) {
|
|
|
|
gui_fatal("Error allocating memory");
|
|
|
|
}
|
2015-01-11 05:42:15 +01:00
|
|
|
procfs_fd = open(procdir_path, O_DIRECTORY | O_RDONLY);
|
|
|
|
if (procfs_fd < 0)
|
2015-01-29 00:44:38 +01:00
|
|
|
perror("Failed to open /proc");
|
|
|
|
else
|
|
|
|
set_procfs_fd(procfs_fd);
|
2015-01-11 05:42:15 +01:00
|
|
|
free(procdir_path);
|
|
|
|
|
|
|
|
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");
|
|
|
|
return ret;
|
2011-03-15 16:43:43 +01:00
|
|
|
}
|