From 5512e4eadab846b52883b22c0160d36da60454b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Fri, 16 Feb 2018 04:25:56 +0100 Subject: [PATCH] qrexec: use exec_qubes_rpc_if_requested() from qubes-utils This avoids duplicating service call parsing in multiple places. Further improvements to that code (like avoid using shell) can be implemented in one place. --- qrexec/qrexec-agent.c | 18 ++++++++++-------- qrexec/qrexec-agent.h | 2 +- qrexec/qrexec-client-vm.c | 2 +- qrexec/qrexec-fork-server.c | 16 ++++++++-------- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/qrexec/qrexec-agent.c b/qrexec/qrexec-agent.c index 15e43e2..46e5566 100644 --- a/qrexec/qrexec-agent.c +++ b/qrexec/qrexec-agent.c @@ -133,9 +133,8 @@ static struct pam_conv conv = { * If dom0 sends overly long cmd, it will probably crash qrexec-agent (unless * process can allocate up to 4GB on both stack and heap), sorry. */ -void do_exec(const char *cmd) +void do_exec(char *cmd) { - char buf[strlen(QUBES_RPC_MULTIPLEXER_PATH) + strlen(cmd) - RPC_REQUEST_COMMAND_LEN + 1]; char *realcmd = index(cmd, ':'), *user; #ifdef HAVE_PAM int retval, status; @@ -157,12 +156,6 @@ void do_exec(const char *cmd) /* ignore "nogui:" prefix in linux agent */ if (strncmp(realcmd, NOGUI_CMD_PREFIX, NOGUI_CMD_PREFIX_LEN) == 0) realcmd += NOGUI_CMD_PREFIX_LEN; - /* replace magic RPC cmd with RPC multiplexer path */ - if (strncmp(realcmd, RPC_REQUEST_COMMAND " ", RPC_REQUEST_COMMAND_LEN+1)==0) { - strcpy(buf, QUBES_RPC_MULTIPLEXER_PATH); - strcpy(buf + strlen(QUBES_RPC_MULTIPLEXER_PATH), realcmd + RPC_REQUEST_COMMAND_LEN); - realcmd = buf; - } signal(SIGCHLD, SIG_DFL); signal(SIGPIPE, SIG_DFL); @@ -260,6 +253,11 @@ void do_exec(const char *cmd) retval = chdir(pw->pw_dir); if (retval == -1) warn("chdir(%s)", pw->pw_dir); + + /* call QUBESRPC if requested */ + exec_qubes_rpc_if_requested(realcmd, env); + + /* otherwise exec shell */ execle(pw->pw_shell, arg0, "-c", realcmd, (char*)NULL, env); exit(127); default: @@ -294,6 +292,10 @@ error: pam_end(pamh, PAM_ABORT); exit(1); #else + /* call QUBESRPC if requested */ + exec_qubes_rpc_if_requested(realcmd, environ); + + /* otherwise exec shell */ execl("/bin/su", "su", "-", user, "-c", realcmd, NULL); perror("execl"); exit(1); diff --git a/qrexec/qrexec-agent.h b/qrexec/qrexec-agent.h index 71214de..05b86c1 100644 --- a/qrexec/qrexec-agent.h +++ b/qrexec/qrexec-agent.h @@ -28,7 +28,7 @@ int handle_handshake(libvchan_t *ctrl); void handle_vchan_error(const char *op); -void do_exec(const char *cmd); +void do_exec(char *cmd); /* call before fork() for service handling process (either end) */ void prepare_child_env(); diff --git a/qrexec/qrexec-client-vm.c b/qrexec/qrexec-client-vm.c index 69bfb96..6d27d8a 100644 --- a/qrexec/qrexec-client-vm.c +++ b/qrexec/qrexec-client-vm.c @@ -37,7 +37,7 @@ void handle_vchan_error(const char *op) exit(1); } -void do_exec(const char *cmd __attribute__((__unused__))) { +void do_exec(char *cmd __attribute__((__unused__))) { fprintf(stderr, "BUG: do_exec function shouldn't be called!\n"); exit(1); } diff --git a/qrexec/qrexec-fork-server.c b/qrexec/qrexec-fork-server.c index 8d53144..43bfd54 100644 --- a/qrexec/qrexec-fork-server.c +++ b/qrexec/qrexec-fork-server.c @@ -33,19 +33,19 @@ #include "libqrexec-utils.h" #include "qrexec-agent.h" -void do_exec(const char *cmd) +extern char **environ; + +void do_exec(char *cmd) { char *shell; - char buf[strlen(QUBES_RPC_MULTIPLEXER_PATH) + strlen(cmd) - strlen(RPC_REQUEST_COMMAND) + 1]; - /* replace magic RPC cmd with RPC multiplexer path */ - if (strncmp(cmd, RPC_REQUEST_COMMAND " ", strlen(RPC_REQUEST_COMMAND)+1)==0) { - strcpy(buf, QUBES_RPC_MULTIPLEXER_PATH); - strcpy(buf + strlen(QUBES_RPC_MULTIPLEXER_PATH), cmd + strlen(RPC_REQUEST_COMMAND)); - cmd = buf; - } + signal(SIGCHLD, SIG_DFL); signal(SIGPIPE, SIG_DFL); + /* call QUBESRPC if requested */ + exec_qubes_rpc_if_requested(cmd, environ); + + /* otherwise, pass it to shell */ shell = getenv("SHELL"); if (!shell) shell = "/bin/sh";