Move functionality to parse command strings into argv_parse_cmd().
That is a preparation for the upcoming refactoring of argv_printf().
Signed-off-by: Heiko Hund <heiko.hund@sophos.com>
Acked-by: David Sommerseth <davids@redhat.com>
Message-Id: <1477672963-5724-5-git-send-email-heiko.hund@sophos.com>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg12816.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
... | ... |
@@ -198,7 +198,6 @@ argv_msg_prefix (const int msglev, const struct argv *a, const char *prefix) |
198 | 198 |
static void |
199 | 199 |
argv_printf_arglist (struct argv *a, const char *format, va_list arglist) |
200 | 200 |
{ |
201 |
- struct gc_arena gc = gc_new (); |
|
202 | 201 |
char *term; |
203 | 202 |
const char *f = format; |
204 | 203 |
|
... | ... |
@@ -215,29 +214,6 @@ argv_printf_arglist (struct argv *a, const char *format, va_list arglist) |
215 | 215 |
s = ""; |
216 | 216 |
argv_append (a, string_alloc (s, NULL)); |
217 | 217 |
} |
218 |
- else if (!strcmp (term, "%sc")) |
|
219 |
- { |
|
220 |
- char *s = va_arg (arglist, char *); |
|
221 |
- if (s) |
|
222 |
- { |
|
223 |
- int nparms; |
|
224 |
- char *parms[MAX_PARMS+1]; |
|
225 |
- int i; |
|
226 |
- |
|
227 |
- nparms = parse_line (s, parms, MAX_PARMS, "SCRIPT-ARGV", 0, D_ARGV_PARSE_CMD, &gc); |
|
228 |
- if (nparms) |
|
229 |
- { |
|
230 |
- for (i = 0; i < nparms; ++i) |
|
231 |
- argv_append (a, string_alloc (parms[i], NULL)); |
|
232 |
- } |
|
233 |
- else |
|
234 |
- argv_append (a, string_alloc (s, NULL)); |
|
235 |
- } |
|
236 |
- else |
|
237 |
- { |
|
238 |
- argv_append (a, string_alloc ("", NULL)); |
|
239 |
- } |
|
240 |
- } |
|
241 | 218 |
else if (!strcmp (term, "%d")) |
242 | 219 |
{ |
243 | 220 |
char numstr[64]; |
... | ... |
@@ -295,7 +271,6 @@ argv_printf_arglist (struct argv *a, const char *format, va_list arglist) |
295 | 295 |
argv_append (a, term); |
296 | 296 |
} |
297 | 297 |
} |
298 |
- gc_free (&gc); |
|
299 | 298 |
} |
300 | 299 |
|
301 | 300 |
void |
... | ... |
@@ -316,3 +291,26 @@ argv_printf_cat (struct argv *a, const char *format, ...) |
316 | 316 |
argv_printf_arglist (a, format, arglist); |
317 | 317 |
va_end (arglist); |
318 | 318 |
} |
319 |
+ |
|
320 |
+void |
|
321 |
+argv_parse_cmd (struct argv *a, const char *s) |
|
322 |
+{ |
|
323 |
+ int nparms; |
|
324 |
+ char *parms[MAX_PARMS + 1]; |
|
325 |
+ struct gc_arena gc = gc_new (); |
|
326 |
+ |
|
327 |
+ argv_reset (a); |
|
328 |
+ argv_extend (a, 1); /* ensure trailing NULL */ |
|
329 |
+ |
|
330 |
+ nparms = parse_line (s, parms, MAX_PARMS, "SCRIPT-ARGV", 0, D_ARGV_PARSE_CMD, &gc); |
|
331 |
+ if (nparms) |
|
332 |
+ { |
|
333 |
+ int i; |
|
334 |
+ for (i = 0; i < nparms; ++i) |
|
335 |
+ argv_append (a, string_alloc (parms[i], NULL)); |
|
336 |
+ } |
|
337 |
+ else |
|
338 |
+ argv_append (a, string_alloc (s, NULL)); |
|
339 |
+ |
|
340 |
+ gc_free (&gc); |
|
341 |
+} |
... | ... |
@@ -45,6 +45,7 @@ const char *argv_str (const struct argv *a, struct gc_arena *gc, const unsigned |
45 | 45 |
struct argv argv_insert_head (const struct argv *a, const char *head); |
46 | 46 |
void argv_msg (const int msglev, const struct argv *a); |
47 | 47 |
void argv_msg_prefix (const int msglev, const struct argv *a, const char *prefix); |
48 |
+void argv_parse_cmd (struct argv *a, const char *s); |
|
48 | 49 |
|
49 | 50 |
void argv_printf (struct argv *a, const char *format, ...) |
50 | 51 |
#ifdef __GNUC__ |
... | ... |
@@ -142,7 +142,7 @@ |
142 | 142 |
#define D_PS_PROXY_DEBUG LOGLEV(7, 70, M_DEBUG) /* port share proxy debug */ |
143 | 143 |
#define D_AUTO_USERID LOGLEV(7, 70, M_DEBUG) /* AUTO_USERID debugging */ |
144 | 144 |
#define D_TLS_KEYSELECT LOGLEV(7, 70, M_DEBUG) /* show information on key selection for data channel */ |
145 |
-#define D_ARGV_PARSE_CMD LOGLEV(7, 70, M_DEBUG) /* show parse_line() errors in argv_printf %sc */ |
|
145 |
+#define D_ARGV_PARSE_CMD LOGLEV(7, 70, M_DEBUG) /* show parse_line() errors in argv_parse_cmd */ |
|
146 | 146 |
#define D_CRYPTO_DEBUG LOGLEV(7, 70, M_DEBUG) /* show detailed info from crypto.c routines */ |
147 | 147 |
#define D_PID_DEBUG LOGLEV(7, 70, M_DEBUG) /* show packet-id debugging info */ |
148 | 148 |
#define D_PF_DROPPED_BCAST LOGLEV(7, 71, M_DEBUG) /* packet filter dropped a broadcast packet */ |
... | ... |
@@ -1350,7 +1350,7 @@ do_route (const struct options *options, |
1350 | 1350 |
{ |
1351 | 1351 |
struct argv argv = argv_new (); |
1352 | 1352 |
setenv_str (es, "script_type", "route-up"); |
1353 |
- argv_printf (&argv, "%sc", options->route_script); |
|
1353 |
+ argv_parse_cmd (&argv, options->route_script); |
|
1354 | 1354 |
openvpn_run_script (&argv, es, 0, "--route-up"); |
1355 | 1355 |
argv_reset (&argv); |
1356 | 1356 |
} |
... | ... |
@@ -120,13 +120,9 @@ run_up_down (const char *command, |
120 | 120 |
struct argv argv = argv_new (); |
121 | 121 |
ASSERT (arg); |
122 | 122 |
setenv_str (es, "script_type", script_type); |
123 |
- argv_printf (&argv, |
|
124 |
- "%sc %s %d %d %s %s %s", |
|
125 |
- command, |
|
126 |
- arg, |
|
127 |
- tun_mtu, link_mtu, |
|
128 |
- ifconfig_local, ifconfig_remote, |
|
129 |
- context); |
|
123 |
+ argv_parse_cmd (&argv, command); |
|
124 |
+ argv_printf_cat (&argv, "%s %d %d %s %s %s", arg, tun_mtu, link_mtu, |
|
125 |
+ ifconfig_local, ifconfig_remote, context); |
|
130 | 126 |
argv_msg (M_INFO, &argv); |
131 | 127 |
openvpn_run_script (&argv, es, S_FATAL, "--up/--down"); |
132 | 128 |
argv_reset (&argv); |
... | ... |
@@ -126,10 +126,8 @@ learn_address_script (const struct multi_context *m, |
126 | 126 |
{ |
127 | 127 |
struct argv argv = argv_new (); |
128 | 128 |
setenv_str (es, "script_type", "learn-address"); |
129 |
- argv_printf (&argv, "%sc %s %s", |
|
130 |
- m->top.options.learn_address_script, |
|
131 |
- op, |
|
132 |
- mroute_addr_print (addr, &gc)); |
|
129 |
+ argv_parse_cmd (&argv, m->top.options.learn_address_script); |
|
130 |
+ argv_printf_cat (&argv, "%s %s", op, mroute_addr_print (addr, &gc)); |
|
133 | 131 |
if (mi) |
134 | 132 |
argv_printf_cat (&argv, "%s", tls_common_name (mi->context.c2.tls_multi, false)); |
135 | 133 |
if (!openvpn_run_script (&argv, es, 0, "--learn-address")) |
... | ... |
@@ -545,7 +543,7 @@ multi_client_disconnect_script (struct multi_context *m, |
545 | 545 |
{ |
546 | 546 |
struct argv argv = argv_new (); |
547 | 547 |
setenv_str (mi->context.c2.es, "script_type", "client-disconnect"); |
548 |
- argv_printf (&argv, "%sc", mi->context.options.client_disconnect_script); |
|
548 |
+ argv_parse_cmd (&argv, mi->context.options.client_disconnect_script); |
|
549 | 549 |
openvpn_run_script (&argv, mi->context.c2.es, 0, "--client-disconnect"); |
550 | 550 |
argv_reset (&argv); |
551 | 551 |
} |
... | ... |
@@ -1834,9 +1832,8 @@ multi_connection_established (struct multi_context *m, struct multi_instance *mi |
1834 | 1834 |
goto script_failed; |
1835 | 1835 |
} |
1836 | 1836 |
|
1837 |
- argv_printf (&argv, "%sc %s", |
|
1838 |
- mi->context.options.client_connect_script, |
|
1839 |
- dc_file); |
|
1837 |
+ argv_parse_cmd (&argv, mi->context.options.client_connect_script); |
|
1838 |
+ argv_printf_cat (&argv, "%s", dc_file); |
|
1840 | 1839 |
|
1841 | 1840 |
if (openvpn_run_script (&argv, mi->context.c2.es, 0, "--client-connect")) |
1842 | 1841 |
{ |
... | ... |
@@ -2796,7 +2796,7 @@ check_cmd_access(const char *command, const char *opt, const char *chroot) |
2796 | 2796 |
|
2797 | 2797 |
/* Extract executable path and arguments */ |
2798 | 2798 |
argv = argv_new (); |
2799 |
- argv_printf (&argv, "%sc", command); |
|
2799 |
+ argv_parse_cmd (&argv, command); |
|
2800 | 2800 |
|
2801 | 2801 |
/* if an executable is specified then check it; otherwise, complain */ |
2802 | 2802 |
if (argv.argv[0]) |
... | ... |
@@ -2023,9 +2023,10 @@ ipchange_fmt (const bool include_cmd, struct argv *argv, const struct link_socke |
2023 | 2023 |
{ |
2024 | 2024 |
const char *host = print_sockaddr_ex (&info->lsa->actual.dest.addr.sa, " ", PS_SHOW_PORT , gc); |
2025 | 2025 |
if (include_cmd) |
2026 |
- argv_printf (argv, "%sc %s", |
|
2027 |
- info->ipchange_command, |
|
2028 |
- host); |
|
2026 |
+ { |
|
2027 |
+ argv_parse_cmd (argv, info->ipchange_command); |
|
2028 |
+ argv_printf_cat (argv, "%s", host); |
|
2029 |
+ } |
|
2029 | 2030 |
else |
2030 | 2031 |
argv_printf (argv, "%s", host); |
2031 | 2032 |
|
... | ... |
@@ -516,7 +516,8 @@ verify_cert_call_command(const char *verify_command, struct env_set *es, |
516 | 516 |
} |
517 | 517 |
} |
518 | 518 |
|
519 |
- argv_printf (&argv, "%sc %d %s", verify_command, cert_depth, subject); |
|
519 |
+ argv_parse_cmd (&argv, verify_command); |
|
520 |
+ argv_printf_cat (&argv, "%d %s", cert_depth, subject); |
|
520 | 521 |
|
521 | 522 |
argv_msg_prefix (D_TLS_DEBUG, &argv, "TLS: executing verify command"); |
522 | 523 |
ret = openvpn_run_script (&argv, es, 0, "--tls-verify script"); |
... | ... |
@@ -983,7 +984,8 @@ verify_user_pass_script (struct tls_session *session, const struct user_pass *up |
983 | 983 |
setenv_untrusted (session); |
984 | 984 |
|
985 | 985 |
/* format command line */ |
986 |
- argv_printf (&argv, "%sc %s", session->opt->auth_user_pass_verify_script, tmp_file); |
|
986 |
+ argv_parse_cmd (&argv, session->opt->auth_user_pass_verify_script); |
|
987 |
+ argv_printf_cat (&argv, "%s", tmp_file); |
|
987 | 988 |
|
988 | 989 |
/* call command */ |
989 | 990 |
ret = openvpn_run_script (&argv, session->opt->es, 0, |
... | ... |
@@ -99,14 +99,23 @@ argv_printf__combined_path_with_spaces__argc_correct (void **state) |
99 | 99 |
} |
100 | 100 |
|
101 | 101 |
static void |
102 |
-argv_printf__script_command__argc_correct (void **state) |
|
102 |
+argv_parse_cmd__command_string__argc_correct (void **state) |
|
103 | 103 |
{ |
104 | 104 |
struct argv a = argv_new (); |
105 | 105 |
|
106 |
- argv_printf (&a, "%sc", SCRIPT_CMD); |
|
106 |
+ argv_parse_cmd (&a, SCRIPT_CMD); |
|
107 | 107 |
assert_int_equal (a.argc, 3); |
108 | 108 |
|
109 |
- argv_printf (&a, "bar baz %sc %d %s", SCRIPT_CMD, 42, PATH1); |
|
109 |
+ argv_reset (&a); |
|
110 |
+} |
|
111 |
+ |
|
112 |
+static void |
|
113 |
+argv_parse_cmd__command_and_extra_options__argc_correct (void **state) |
|
114 |
+{ |
|
115 |
+ struct argv a = argv_new (); |
|
116 |
+ |
|
117 |
+ argv_parse_cmd (&a, SCRIPT_CMD); |
|
118 |
+ argv_printf_cat (&a, "bar baz %d %s", 42, PATH1); |
|
110 | 119 |
assert_int_equal (a.argc, 7); |
111 | 120 |
|
112 | 121 |
argv_reset (&a); |
... | ... |
@@ -184,7 +193,8 @@ main(void) |
184 | 184 |
cmocka_unit_test (argv_printf__multiple_spaces_in_format__parsed_as_one), |
185 | 185 |
cmocka_unit_test (argv_printf_cat__multiple_spaces_in_format__parsed_as_one), |
186 | 186 |
cmocka_unit_test (argv_printf__combined_path_with_spaces__argc_correct), |
187 |
- cmocka_unit_test (argv_printf__script_command__argc_correct), |
|
187 |
+ cmocka_unit_test (argv_parse_cmd__command_string__argc_correct), |
|
188 |
+ cmocka_unit_test (argv_parse_cmd__command_and_extra_options__argc_correct), |
|
188 | 189 |
cmocka_unit_test (argv_printf_cat__used_twice__argc_correct), |
189 | 190 |
cmocka_unit_test (argv_str__multiple_argv__correct_output), |
190 | 191 |
cmocka_unit_test (argv_insert_head__non_empty_argv__head_added), |