diff options
author | Ron Yorston <rmy@pobox.com> | 2019-03-07 14:54:41 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2019-03-07 15:34:24 +0000 |
commit | 0533794afd81b37684669ee0c5afc7a5e1ff159d (patch) | |
tree | a6afcd3e6c5e6430093b5e01111d799ed1a30622 /loginutils | |
parent | 585d17d262efabce4a9a87f33f531ef9ab7c0e36 (diff) | |
download | busybox-w32-0533794afd81b37684669ee0c5afc7a5e1ff159d.tar.gz busybox-w32-0533794afd81b37684669ee0c5afc7a5e1ff159d.tar.bz2 busybox-w32-0533794afd81b37684669ee0c5afc7a5e1ff159d.zip |
su: add a basic implementation for WIN32
Use the undocumented 'runas' verb in a call to ShellExecuteEx()
to run a shell with elevated privileges.
Because of the way ShellExecuteEx() works this:
- requires that you acknowledge a User Account Control prompt (if
you're an Administrator);
- requires that you enter an Administrator's password (if you aren't
an Administrator);
- creates a separate console window for the privileged shell.
Variables from the parent shell aren't passed to its privileged child,
only variables from the environment.
It's possible to specify a command to run when the shell starts.
This can be used to pass shell variables:
su -c "HELLO='hello world'; GOODBYE=$GOODBYE"
Or do fancy things like:
su -c "ls -l; read -p 'Hit return to exit: '; exit"
It's probably best to put double quotes around the command and use
single quotes inside it. Apparently ShellExecuteEx() requires
double quotes to be entered in triplicate:
su -c 'HELLO="""hello world"""'
Diffstat (limited to 'loginutils')
-rw-r--r-- | loginutils/su.c | 1 | ||||
-rw-r--r-- | loginutils/suw32.c | 50 |
2 files changed, 51 insertions, 0 deletions
diff --git a/loginutils/su.c b/loginutils/su.c index 41291ea8f..2e1b309b0 100644 --- a/loginutils/su.c +++ b/loginutils/su.c | |||
@@ -8,6 +8,7 @@ | |||
8 | //config: bool "su (19 kb)" | 8 | //config: bool "su (19 kb)" |
9 | //config: default y | 9 | //config: default y |
10 | //config: select FEATURE_SYSLOG | 10 | //config: select FEATURE_SYSLOG |
11 | //config: depends on PLATFORM_POSIX | ||
11 | //config: help | 12 | //config: help |
12 | //config: su is used to become another user during a login session. | 13 | //config: su is used to become another user during a login session. |
13 | //config: Invoked without a username, su defaults to becoming the super user. | 14 | //config: Invoked without a username, su defaults to becoming the super user. |
diff --git a/loginutils/suw32.c b/loginutils/suw32.c new file mode 100644 index 000000000..3aa478f33 --- /dev/null +++ b/loginutils/suw32.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini su implementation for busybox-w32 | ||
4 | * | ||
5 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. | ||
6 | */ | ||
7 | //config:config SUW32 | ||
8 | //config: bool "su for Microsoft Windows" | ||
9 | //config: default y | ||
10 | //config: depends on PLATFORM_MINGW32 && ASH | ||
11 | //config: help | ||
12 | //config: su runs a shell with elevated privileges. | ||
13 | |||
14 | //applet:IF_SUW32(APPLET_ODDNAME(su, suw32, BB_DIR_BIN, BB_SUID_DROP, suw32)) | ||
15 | |||
16 | //kbuild:lib-$(CONFIG_SUW32) += suw32.o | ||
17 | |||
18 | //usage:#define suw32_trivial_usage | ||
19 | //usage: "[-c \"CMD\"]" | ||
20 | //usage:#define suw32_full_usage "\n\n" | ||
21 | //usage: "Run shell with elevated privileges\n" | ||
22 | //usage: "\n -c CMD Command to pass to 'sh -c'" | ||
23 | |||
24 | #include "libbb.h" | ||
25 | |||
26 | int suw32_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
27 | int suw32_main(int argc UNUSED_PARAM, char **argv) | ||
28 | { | ||
29 | char *opt_command = NULL; | ||
30 | SHELLEXECUTEINFO info; | ||
31 | |||
32 | getopt32(argv, "c:", &opt_command); | ||
33 | if (argv[optind]) | ||
34 | bb_show_usage(); | ||
35 | |||
36 | memset(&info, 0, sizeof(SHELLEXECUTEINFO)); | ||
37 | info.cbSize = sizeof(SHELLEXECUTEINFO); | ||
38 | /* info.fMask = SEE_MASK_DEFAULT; */ | ||
39 | /* info.hwnd = NULL; */ | ||
40 | info.lpVerb = "runas"; | ||
41 | info.lpFile = bb_busybox_exec_path; | ||
42 | if (opt_command) | ||
43 | info.lpParameters = xasprintf("ash -s -c \"%s\"", opt_command); | ||
44 | else | ||
45 | info.lpParameters = "ash"; | ||
46 | /* info.lpDirectory = NULL; */ | ||
47 | info.nShow = SW_SHOWNORMAL; | ||
48 | |||
49 | return !ShellExecuteEx(&info); | ||
50 | } | ||