From cee32d4f900b7e3a6276bff6e203be962e2e428e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Tue, 3 Sep 2019 19:28:23 +0200 Subject: [PATCH] qfile-unpacker: add option for custom user and target directory QubesOS/qubes-issues#930 --- qubes-rpc/qfile-unpacker.c | 41 +++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/qubes-rpc/qfile-unpacker.c b/qubes-rpc/qfile-unpacker.c index 5d16121..268c9a4 100644 --- a/qubes-rpc/qfile-unpacker.c +++ b/qubes-rpc/qfile-unpacker.c @@ -36,7 +36,7 @@ char *prepare_creds_return_dir(int uid) return pwd->pw_dir; } -int main(int argc __attribute((__unused__)), char ** argv __attribute__((__unused__))) +int main(int argc, char ** argv) { char *home_dir; char *incoming_dir_root; @@ -46,22 +46,39 @@ int main(int argc __attribute((__unused__)), char ** argv __attribute__((__unuse const char *remote_domain; char *procdir_path; int procfs_fd; + int i; - uid = getuid(); - home_dir = prepare_creds_return_dir(uid); + 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"); + } - remote_domain = getenv("QREXEC_REMOTE_DOMAIN"); - if (!remote_domain) { - gui_fatal("Cannot get remote domain name"); + 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); } - if (asprintf(&incoming_dir_root, "%s/%s", home_dir, INCOMING_DIR_NAME) < 0) { - gui_fatal("Error allocating memory"); + for (i = 3; i < argc; i++) { + if (strcmp(argv[i], "-v") == 0) + set_verbose(1); + else + gui_fatal("Invalid option %s", argv[i]); } - 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); + if (chdir(incoming_dir)) gui_fatal("Error chdir to %s", incoming_dir);