diff options
author | Ron Yorston <rmy@pobox.com> | 2023-04-21 15:08:28 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2023-04-21 15:08:28 +0100 |
commit | 9c20ed5246abbfe2fdae16a70b7fac1d0d1e51f0 (patch) | |
tree | 82f08695e5407b62fdf9ee99f14e403d063a0f46 | |
parent | ff8242f51aac4e1b358eac833d6b384ebe1a1116 (diff) | |
download | busybox-w32-su_cmd.tar.gz busybox-w32-su_cmd.tar.bz2 busybox-w32-su_cmd.zip |
su: allow additional arguments after su -csu_cmd
The syntax becomes:
su [-c CMD [ARG...]]
This makes it a lot easier to use values from the current shell,
instead of shell-quoting them for re-input and embedding at CMD.
Also, previously -c expected an option-argument, and now it's a flag
which requires a CMD operand, just like 'sh', so that both these
forms now error the same way (as they should):
sh -c --
su -c --
(previously su -c -- did not error because '--' was assumed CMD)
Adds 64 bytes.
(Based on GitHub PR #317)
-rw-r--r-- | loginutils/suw32.c | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/loginutils/suw32.c b/loginutils/suw32.c index df6d07232..46196d14c 100644 --- a/loginutils/suw32.c +++ b/loginutils/suw32.c | |||
@@ -16,10 +16,10 @@ | |||
16 | //kbuild:lib-$(CONFIG_SUW32) += suw32.o | 16 | //kbuild:lib-$(CONFIG_SUW32) += suw32.o |
17 | 17 | ||
18 | //usage:#define suw32_trivial_usage | 18 | //usage:#define suw32_trivial_usage |
19 | //usage: "[-c \"CMD\"]" | 19 | //usage: "[-c \"CMD\" [ARG...]]" |
20 | //usage:#define suw32_full_usage "\n\n" | 20 | //usage:#define suw32_full_usage "\n\n" |
21 | //usage: "Run shell with elevated privileges\n" | 21 | //usage: "Run shell with elevated privileges\n" |
22 | //usage: "\n -c CMD Command to pass to 'sh -c'" | 22 | //usage: "\n -c CMD [ARG...] Command [args] to pass to 'sh -c'" |
23 | 23 | ||
24 | #include "libbb.h" | 24 | #include "libbb.h" |
25 | #include "lazyload.h" | 25 | #include "lazyload.h" |
@@ -27,14 +27,15 @@ | |||
27 | int suw32_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 27 | int suw32_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
28 | int suw32_main(int argc UNUSED_PARAM, char **argv) | 28 | int suw32_main(int argc UNUSED_PARAM, char **argv) |
29 | { | 29 | { |
30 | char *opt_command = NULL; | ||
31 | SHELLEXECUTEINFO info; | 30 | SHELLEXECUTEINFO info; |
31 | unsigned opts, c_opt; | ||
32 | char *bb_path, *cwd; | 32 | char *bb_path, *cwd; |
33 | DECLARE_PROC_ADDR(BOOL, ShellExecuteExA, SHELLEXECUTEINFOA *); | 33 | DECLARE_PROC_ADDR(BOOL, ShellExecuteExA, SHELLEXECUTEINFOA *); |
34 | 34 | ||
35 | getopt32(argv, "c:", &opt_command); | 35 | opts = getopt32(argv, "c"); |
36 | if (argv[optind]) | 36 | c_opt = opts & 1; |
37 | bb_show_usage(); | 37 | if (!c_opt != !argv[optind]) |
38 | bb_show_usage(); // -c without CMD, or operand without -c | ||
38 | 39 | ||
39 | /* ShellExecuteEx() needs backslash as separator in UNC paths. */ | 40 | /* ShellExecuteEx() needs backslash as separator in UNC paths. */ |
40 | bb_path = xstrdup(bb_busybox_exec_path); | 41 | bb_path = xstrdup(bb_busybox_exec_path); |
@@ -59,9 +60,16 @@ int suw32_main(int argc UNUSED_PARAM, char **argv) | |||
59 | cwd = xmalloc_realpath(getcwd(NULL, 0)); | 60 | cwd = xmalloc_realpath(getcwd(NULL, 0)); |
60 | info.lpParameters = | 61 | info.lpParameters = |
61 | xasprintf("--busybox ash -d \"%s\" -t \"BusyBox ash (Admin)\" ", cwd); | 62 | xasprintf("--busybox ash -d \"%s\" -t \"BusyBox ash (Admin)\" ", cwd); |
62 | if (opt_command) | 63 | |
63 | info.lpParameters = | 64 | if (c_opt) { |
64 | xasprintf("%s -s -c %s", info.lpParameters, quote_arg(opt_command)); | 65 | info.lpParameters = xappendword(info.lpParameters, "-s -c --"); |
66 | while (argv[optind]) { | ||
67 | char *a = quote_arg(argv[optind++]); | ||
68 | info.lpParameters = xappendword(info.lpParameters, a); | ||
69 | free(a); | ||
70 | } | ||
71 | } | ||
72 | |||
65 | /* info.lpDirectory = NULL; */ | 73 | /* info.lpDirectory = NULL; */ |
66 | info.nShow = SW_SHOWNORMAL; | 74 | info.nShow = SW_SHOWNORMAL; |
67 | 75 | ||