diff options
author | Ron Yorston <rmy@pobox.com> | 2023-03-23 08:00:36 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2023-03-23 08:00:36 +0000 |
commit | 138e956a2e34d1b708366b6286687766a7176341 (patch) | |
tree | 44db68aae211c5e198a69a4256dd2ebf5455fc04 | |
parent | d9e8bb61cf380264511a4524281af2ca57498bce (diff) | |
download | busybox-w32-138e956a2e34d1b708366b6286687766a7176341.tar.gz busybox-w32-138e956a2e34d1b708366b6286687766a7176341.tar.bz2 busybox-w32-138e956a2e34d1b708366b6286687766a7176341.zip |
drop: search PATH for cmd.exe/PowerShell
Rather than hardcode the paths of cmd.exe and PowerShell find them
by searching PATH.
Saves 104-128 bytes.
(GitHub issue #240)
-rw-r--r-- | miscutils/drop.c | 52 |
1 files changed, 25 insertions, 27 deletions
diff --git a/miscutils/drop.c b/miscutils/drop.c index f73125e1b..9ebee3f04 100644 --- a/miscutils/drop.c +++ b/miscutils/drop.c | |||
@@ -60,7 +60,6 @@ | |||
60 | int drop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 60 | int drop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
61 | int drop_main(int argc, char **argv) | 61 | int drop_main(int argc, char **argv) |
62 | { | 62 | { |
63 | const char *exe; | ||
64 | SAFER_LEVEL_HANDLE safer; | 63 | SAFER_LEVEL_HANDLE safer; |
65 | HANDLE token; | 64 | HANDLE token; |
66 | STARTUPINFO si; | 65 | STARTUPINFO si; |
@@ -72,7 +71,6 @@ int drop_main(int argc, char **argv) | |||
72 | 0x00, 0x00, 0x00, 0x10, | 71 | 0x00, 0x00, 0x00, 0x10, |
73 | 0x00, 0x20, 0x00, 0x00 | 72 | 0x00, 0x20, 0x00, 0x00 |
74 | }; | 73 | }; |
75 | char *cmd, *q, *newcmd, **a; | ||
76 | DWORD code; | 74 | DWORD code; |
77 | // This shouldn't be necessary but without it the binary complains | 75 | // This shouldn't be necessary but without it the binary complains |
78 | // it can't find CreateProcessAsUserA on older versions of Windows. | 76 | // it can't find CreateProcessAsUserA on older versions of Windows. |
@@ -97,55 +95,55 @@ int drop_main(int argc, char **argv) | |||
97 | TIL.Label.Attributes = SE_GROUP_INTEGRITY; | 95 | TIL.Label.Attributes = SE_GROUP_INTEGRITY; |
98 | if (SetTokenInformation(token, TokenIntegrityLevel, &TIL, | 96 | if (SetTokenInformation(token, TokenIntegrityLevel, &TIL, |
99 | sizeof(TOKEN_MANDATORY_LABEL))) { | 97 | sizeof(TOKEN_MANDATORY_LABEL))) { |
100 | int skip = 1; | 98 | char **a = argv + 1; |
99 | const char *arg; | ||
100 | char *exe, *cmd; | ||
101 | 101 | ||
102 | if (argc == 1 || strcmp(argv[1], "-c") == 0 | 102 | if (argc == 1 || strcmp(argv[1], "-c") == 0 |
103 | IF_CDROP(|| strcmp(argv[1], "/c") == 0)) { | 103 | IF_CDROP(|| strcmp(argv[1], "/c") == 0)) { |
104 | #if ENABLE_PDROP | 104 | #if ENABLE_PDROP |
105 | if (*applet_name == 'p') { | 105 | if (*applet_name == 'p') { |
106 | exe = "C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"; | 106 | arg = "powershell.exe"; |
107 | cmd = xstrdup("powershell"); | 107 | exe = find_first_executable(arg); |
108 | } else | 108 | } else |
109 | #endif | 109 | #endif |
110 | #if ENABLE_CDROP | 110 | #if ENABLE_CDROP |
111 | if (*applet_name == 'c') { | 111 | if (*applet_name == 'c') { |
112 | exe = "C:/Windows/System32/cmd.exe"; | 112 | arg = "cmd.exe"; |
113 | cmd = xstrdup("cmd"); | 113 | exe = find_first_executable(arg); |
114 | } else | 114 | } else |
115 | #endif | 115 | #endif |
116 | { | 116 | { |
117 | exe = bb_busybox_exec_path; | 117 | arg = "sh"; |
118 | cmd = xstrdup("sh"); | 118 | exe = xstrdup(bb_busybox_exec_path); |
119 | } | 119 | } |
120 | skip = 0; | ||
121 | } else { | 120 | } else { |
122 | char *file; | 121 | arg = *a++; |
123 | 122 | ||
124 | #if ENABLE_FEATURE_PREFER_APPLETS | 123 | #if ENABLE_FEATURE_PREFER_APPLETS |
125 | if (!has_path(argv[1]) && find_applet_by_name(argv[1]) >= 0) { | 124 | if (!has_path(arg) && find_applet_by_name(arg) >= 0) { |
126 | file = xstrdup(bb_busybox_exec_path); | 125 | exe = xstrdup(bb_busybox_exec_path); |
127 | } else | 126 | } else |
128 | #endif | 127 | #endif |
129 | if (has_path(argv[1])) { | 128 | if (has_path(arg)) { |
130 | file = file_is_win32_exe(argv[1]); | 129 | exe = file_is_win32_exe(arg); |
131 | } else { | 130 | } else { |
132 | file = find_first_executable(argv[1]); | 131 | exe = find_first_executable(arg); |
133 | } | ||
134 | |||
135 | if (file == NULL) { | ||
136 | xfunc_error_retval = 127; | ||
137 | bb_error_msg_and_die("can't find '%s'", argv[1]); | ||
138 | } | 132 | } |
133 | } | ||
139 | 134 | ||
140 | slash_to_bs(file); | 135 | if (exe == NULL) { |
141 | exe = file; | 136 | xfunc_error_retval = 127; |
142 | cmd = quote_arg(argv[1]); | 137 | bb_error_msg_and_die("can't find '%s'", arg); |
143 | } | 138 | } |
144 | 139 | ||
140 | slash_to_bs(exe); | ||
141 | cmd = quote_arg(arg); | ||
142 | |||
145 | // Build the command line | 143 | // Build the command line |
146 | for (a = argv + 1 + skip; *a; ++a) { | 144 | while (*a) { |
147 | q = quote_arg(*a); | 145 | char *q = quote_arg(*a++); |
148 | newcmd = xasprintf("%s %s", cmd, q); | 146 | char *newcmd = xasprintf("%s %s", cmd, q); |
149 | free(q); | 147 | free(q); |
150 | free(cmd); | 148 | free(cmd); |
151 | cmd = newcmd; | 149 | cmd = newcmd; |