3636#include <netdb.h>
3737#include <unistd.h>
3838#include <fcntl.h>
39+
3940#include "qrexec.h"
4041#include "libqrexec-utils.h"
4142#include "private.h"
@@ -54,110 +55,98 @@ static bool should_strip_env_var(const char *var)
5455 return strncmp (var + (sizeof "QREXEC" - 1 ), "_SERVICE_PATH=" , sizeof "_SERVICE_PATH" ) != 0 ;
5556}
5657
57- void exec_qubes_rpc_if_requested (const char * program , const char * cmd , char * const envp []) {
58+ void exec_qubes_rpc (const char * program , const char * cmd , char * const envp []) {
5859 /* avoid calling RPC service through shell */
59- if (program ) {
60- assert (program );
61- char * prog_copy ;
62- char * tok , * savetok ;
63- const char * argv [6 ];
64- size_t i = 0 ;
60+ char * prog_copy ;
61+ char * tok , * savetok ;
62+ const char * argv [6 ];
63+ size_t i = 0 ;
6564#define MAX_ADDED_ENV_VARS 5
66- size_t const extra_env_vars = MAX_ADDED_ENV_VARS ;
67- size_t env_amount = extra_env_vars ;
68-
69- if (strncmp (cmd , RPC_REQUEST_COMMAND " " , RPC_REQUEST_COMMAND_LEN + 1 ) != 0 ) {
70- LOG (ERROR , "program != NULL, but '%s' does not start with '%s '" ,
71- cmd , RPC_REQUEST_COMMAND " " );
72- assert (!"Invalid command" );
73- _exit (QREXEC_EXIT_PROBLEM );
74- }
75-
76- for (char * const * env = envp ; * env ; ++ env ) {
77- // Set this 0 to 1 if adding new variable settings below,
78- // to ensure that MAX_ADDED_ENV_VARS is correct.
79- if (0 && should_strip_env_var (* env ))
80- continue ;
81- env_amount ++ ;
82- }
65+ size_t const extra_env_vars = MAX_ADDED_ENV_VARS ;
66+ size_t env_amount = extra_env_vars ;
67+
68+ for (char * const * env = envp ; * env ; ++ env ) {
69+ // Set this 0 to 1 if adding new variable settings below,
70+ // to ensure that MAX_ADDED_ENV_VARS is correct.
71+ if (0 && should_strip_env_var (* env ))
72+ continue ;
73+ env_amount ++ ;
74+ }
8375#define EXTEND (...) \
84- do { \
85- if (iterator >= env_amount) \
86- abort(); \
87- if (asprintf(&buf[iterator++], __VA_ARGS__) < 0) \
88- goto bad_asprintf; \
89- } while (0)
76+ do { \
77+ if (iterator >= env_amount) \
78+ abort(); \
79+ if (asprintf(&buf[iterator++], __VA_ARGS__) < 0) \
80+ goto bad_asprintf; \
81+ } while (0)
9082#define EXTEND_RAW (arg ) \
91- do { \
92- if (iterator >= env_amount) \
93- abort(); \
94- buf[iterator++] = (arg); \
95- } while (0)
83+ do { \
84+ if (iterator >= env_amount) \
85+ abort(); \
86+ buf[iterator++] = (arg); \
87+ } while (0)
9688
97- char * * buf = calloc (env_amount + 1 , sizeof (char * ));
98- if (buf == NULL ) {
99- LOG (ERROR , "calloc(%zu, %zu) failed: %m" , env_amount , sizeof (char * ));
100- _exit (QREXEC_EXIT_PROBLEM );
101- }
102- size_t iterator = 0 ;
103- for (char * const * env = envp ; * env ; ++ env ) {
104- if (!should_strip_env_var (* env )) {
105- EXTEND_RAW (* env );
106- }
89+ char * * buf = calloc (env_amount + 1 , sizeof (char * ));
90+ if (buf == NULL ) {
91+ LOG (ERROR , "calloc(%zu, %zu) failed: %m" , env_amount , sizeof (char * ));
92+ _exit (QREXEC_EXIT_PROBLEM );
93+ }
94+ size_t iterator = 0 ;
95+ for (char * const * env = envp ; * env ; ++ env ) {
96+ if (!should_strip_env_var (* env )) {
97+ EXTEND_RAW (* env );
10798 }
99+ }
108100
109- prog_copy = strdup (cmd + RPC_REQUEST_COMMAND_LEN + 1 );
110- if (!prog_copy ) {
111- PERROR ("strdup" );
112- _exit (QREXEC_EXIT_PROBLEM );
113- }
101+ prog_copy = strdup (cmd );
102+ if (!prog_copy ) {
103+ PERROR ("strdup" );
104+ _exit (QREXEC_EXIT_PROBLEM );
105+ }
114106
115- argv [i ++ ] = (char * )program ;
116- tok = strtok_r (prog_copy , " " , & savetok );
117- while (tok != NULL ) {
118- if (i >= sizeof (argv )/sizeof (argv [0 ])- 1 ) {
119- LOG (ERROR , "Too many arguments to %s" , RPC_REQUEST_COMMAND );
120- _exit (QREXEC_EXIT_PROBLEM );
121- }
122- argv [i ++ ] = tok ;
123- tok = strtok_r (NULL , " " , & savetok );
124- }
125- argv [i ] = NULL ;
126- if (i == 5 ) {
127- EXTEND ("QREXEC_REQUESTED_TARGET_TYPE=%s" , argv [3 ]);
128- if (strcmp (argv [3 ], "name" ) == 0 ) {
129- EXTEND ("QREXEC_REQUESTED_TARGET=%s" , argv [4 ]);
130- } else if (strcmp (argv [3 ], "keyword" ) == 0 ) {
131- EXTEND ("QREXEC_REQUESTED_TARGET_KEYWORD=%s" , argv [4 ]);
132- } else {
133- // requested target type unknown, ignore
134- }
135- } else if (i == 3 ) {
136- EXTEND_RAW ("QREXEC_REQUESTED_TARGET_TYPE=" );
137- } else {
138- LOG (ERROR , "invalid number of arguments: %zu" , i );
107+ argv [i ++ ] = (char * )program ;
108+ tok = strtok_r (prog_copy , " " , & savetok );
109+ while (tok != NULL ) {
110+ if (i >= sizeof (argv )/sizeof (argv [0 ])- 1 ) {
111+ LOG (ERROR , "Too many arguments to %s" , RPC_REQUEST_COMMAND );
139112 _exit (QREXEC_EXIT_PROBLEM );
140113 }
141- EXTEND ("QREXEC_SERVICE_FULL_NAME=%s" , argv [1 ]);
142- EXTEND ("QREXEC_REMOTE_DOMAIN=%s" , argv [2 ]);
143- const char * p = strchr (argv [1 ], '+' );
144- argv [1 ] = NULL ;
145- argv [2 ] = NULL ;
146- if (p != NULL ) {
147- EXTEND ("QREXEC_SERVICE_ARGUMENT=%s" , p + 1 );
148- if (p [1 ])
149- argv [1 ] = p + 1 ;
114+ argv [i ++ ] = tok ;
115+ tok = strtok_r (NULL , " " , & savetok );
116+ }
117+ argv [i ] = NULL ;
118+ if (i == 5 ) {
119+ EXTEND ("QREXEC_REQUESTED_TARGET_TYPE=%s" , argv [3 ]);
120+ if (strcmp (argv [3 ], "name" ) == 0 ) {
121+ EXTEND ("QREXEC_REQUESTED_TARGET=%s" , argv [4 ]);
122+ } else if (strcmp (argv [3 ], "keyword" ) == 0 ) {
123+ EXTEND ("QREXEC_REQUESTED_TARGET_KEYWORD=%s" , argv [4 ]);
124+ } else {
125+ // requested target type unknown or not given, ignore
150126 }
151- assert (iterator <= env_amount );
152- buf [iterator ] = NULL ;
153- execve (argv [0 ], (char * const * )argv , buf );
154- _exit (errno == ENOENT ? QREXEC_EXIT_SERVICE_NOT_FOUND : QREXEC_EXIT_PROBLEM );
155- bad_asprintf :
156- PERROR ("asprintf" );
157- _exit (QREXEC_EXIT_PROBLEM );
127+ } else if (i == 3 ) {
128+ EXTEND_RAW ("QREXEC_REQUESTED_TARGET_TYPE=" );
158129 } else {
159- assert (strncmp (cmd , RPC_REQUEST_COMMAND , RPC_REQUEST_COMMAND_LEN ) != 0 );
130+ LOG (ERROR , "invalid number of arguments: %zu" , i );
131+ _exit (QREXEC_EXIT_PROBLEM );
160132 }
133+ EXTEND ("QREXEC_SERVICE_FULL_NAME=%s" , argv [1 ]);
134+ EXTEND ("QREXEC_REMOTE_DOMAIN=%s" , argv [2 ]);
135+ const char * p = strchr (argv [1 ], '+' );
136+ argv [1 ] = NULL ;
137+ argv [2 ] = NULL ;
138+ if (p != NULL ) {
139+ EXTEND ("QREXEC_SERVICE_ARGUMENT=%s" , p + 1 );
140+ if (p [1 ])
141+ argv [1 ] = p + 1 ;
142+ }
143+ assert (iterator <= env_amount );
144+ buf [iterator ] = NULL ;
145+ execve (argv [0 ], (char * const * )argv , buf );
146+ _exit (errno == ENOENT ? QREXEC_EXIT_SERVICE_NOT_FOUND : QREXEC_EXIT_PROBLEM );
147+ bad_asprintf :
148+ PERROR ("asprintf" );
149+ _exit (QREXEC_EXIT_PROBLEM );
161150}
162151
163152void fix_fds (int fdin , int fdout , int fderr )
@@ -493,7 +482,9 @@ struct qrexec_parsed_command *parse_qubes_rpc_command(
493482 }
494483
495484 /* Parse service descriptor ("qubes.Service+arg") */
496- start = cmd -> command + RPC_REQUEST_COMMAND_LEN + 1 ;
485+ cmd -> command += RPC_REQUEST_COMMAND_LEN + 1 ;
486+
487+ start = cmd -> command ;
497488 end = strchr (start , ' ' );
498489 if (!end ) {
499490 LOG (ERROR , "No space found after service descriptor" );
@@ -608,7 +599,7 @@ int execute_parsed_qubes_rpc_command(
608599 return 0 ;
609600 }
610601 return do_fork_exec (buf .data , cmd -> username , cmd -> command ,
611- pid , stdin_fd , stdout_fd , stderr_fd );
602+ pid , stdin_fd , stdout_fd , stderr_fd );
612603 } else {
613604 // Legacy qrexec behavior: spawn shell directly
614605 return do_fork_exec (NULL , cmd -> username , cmd -> command ,
@@ -732,7 +723,7 @@ int find_qrexec_service(
732723
733724 if (cmd -> send_service_descriptor ) {
734725 /* send part after "QUBESRPC ", including trailing NUL */
735- const char * desc = cmd -> command + RPC_REQUEST_COMMAND_LEN + 1 ;
726+ const char * desc = cmd -> command ;
736727 buffer_append (stdin_buffer , desc , strlen (desc ) + 1 );
737728 }
738729
@@ -792,7 +783,7 @@ int find_qrexec_service(
792783
793784 if (cmd -> send_service_descriptor ) {
794785 /* send part after "QUBESRPC ", including trailing NUL */
795- const char * desc = cmd -> command + RPC_REQUEST_COMMAND_LEN + 1 ;
786+ const char * desc = cmd -> command ;
796787 buffer_append (stdin_buffer , desc , strlen (desc ) + 1 );
797788 }
798789
0 commit comments