summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2019-03-10 09:58:13 +0000
committerRon Yorston <rmy@pobox.com>2019-03-10 13:04:55 +0000
commitb04bbc0109046ee69806a472fd7e44313c646687 (patch)
tree6e7a1985aecdbee605ae58ce7cc342c5d4e23027
parent399b1dd641c16113c3340933a9b1ab1793a13d8a (diff)
downloadbusybox-w32-b04bbc0109046ee69806a472fd7e44313c646687.tar.gz
busybox-w32-b04bbc0109046ee69806a472fd7e44313c646687.tar.bz2
busybox-w32-b04bbc0109046ee69806a472fd7e44313c646687.zip
win32: changes to user ids
Formalise the use of 0 as the uid of a process running with elevated privileges: - Rewrite getuid(2) to return DEFAULT_UID by default and 0 if the process has elevated privileges. - geteuid(2) and the corresponding functions for groups are aliases for getuid(2). - Change root's home directory to be whatever GetSystemDirectory() returns, probably C:/Windows/System32 in most cases. - Remove the special handling of geteuid(2) in the line editing code. With these changes the shell started by 'su' is a lot more like a *nix root shell.
-rw-r--r--include/mingw.h11
-rw-r--r--libbb/lineedit.c3
-rw-r--r--loginutils/suw32.c8
-rw-r--r--shell/ash.c2
-rw-r--r--win32/mingw.c50
5 files changed, 42 insertions, 32 deletions
diff --git a/include/mingw.h b/include/mingw.h
index ead07ae82..7aa2d5cc9 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -9,7 +9,7 @@ typedef int gid_t;
9typedef int uid_t; 9typedef int uid_t;
10 10
11#define DEFAULT_UID 4095 11#define DEFAULT_UID 4095
12#define DEFAULT_GID 4095 12#define DEFAULT_GID DEFAULT_UID
13 13
14/* 14/*
15 * arpa/inet.h 15 * arpa/inet.h
@@ -388,13 +388,13 @@ char *mingw_getcwd(char *pointer, int len);
388off_t mingw_lseek(int fd, off_t offset, int whence); 388off_t mingw_lseek(int fd, off_t offset, int whence);
389 389
390 390
391IMPL(getgid,int,DEFAULT_GID,void); 391int getuid(void);
392#define getgid getuid
393#define geteuid getuid
394#define getegid getuid
392int getgroups(int n, gid_t *groups); 395int getgroups(int n, gid_t *groups);
393IMPL(getppid,int,1,void); 396IMPL(getppid,int,1,void);
394IMPL(getegid,int,DEFAULT_GID,void);
395IMPL(geteuid,int,DEFAULT_UID,void);
396NOIMPL(getsid,pid_t pid UNUSED_PARAM); 397NOIMPL(getsid,pid_t pid UNUSED_PARAM);
397IMPL(getuid,int,DEFAULT_UID,void);
398int getlogin_r(char *buf, size_t len); 398int getlogin_r(char *buf, size_t len);
399int fcntl(int fd, int cmd, ...); 399int fcntl(int fd, int cmd, ...);
400int fsync(int fd); 400int fsync(int fd);
@@ -521,4 +521,3 @@ ULONGLONG CompatGetTickCount64(void);
521ssize_t get_random_bytes(void *buf, ssize_t count); 521ssize_t get_random_bytes(void *buf, ssize_t count);
522int enumerate_links(const char *file, char *name); 522int enumerate_links(const char *file, char *name);
523void hide_console(void); 523void hide_console(void);
524int is_admin(void);
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 9e24d410d..95f92aa75 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -45,9 +45,6 @@
45# define _POSIX_VDISABLE '\0' 45# define _POSIX_VDISABLE '\0'
46#endif 46#endif
47 47
48#if ENABLE_PLATFORM_MINGW32
49# define geteuid() (is_admin() ? 0 : DEFAULT_UID)
50#endif
51 48
52#ifdef TEST 49#ifdef TEST
53# define ENABLE_FEATURE_EDITING 0 50# define ENABLE_FEATURE_EDITING 0
diff --git a/loginutils/suw32.c b/loginutils/suw32.c
index 787bcbbef..6396a7b88 100644
--- a/loginutils/suw32.c
+++ b/loginutils/suw32.c
@@ -47,10 +47,12 @@ int suw32_main(int argc UNUSED_PARAM, char **argv)
47 /* info.hwnd = NULL; */ 47 /* info.hwnd = NULL; */
48 info.lpVerb = "runas"; 48 info.lpVerb = "runas";
49 info.lpFile = bb_path; 49 info.lpFile = bb_path;
50 /* ShellExecuteEx() always runs system binaries in C:\Windows\System32. 50 /* It seems that when ShellExecuteEx() runs binaries residing in
51 * Pass the directory we want to the shell. */ 51 * certain 'system' directories it sets the current directory of
52 * the process to %SYSTEMROOT%\System32. Override this by passing
53 * the directory we want to the shell. */
52 info.lpParameters = 54 info.lpParameters =
53 xasprintf("--busybox ash -d \"%s\" -t \"BusyBox ash (su)\" ", 55 xasprintf("--busybox ash -d \"%s\" -t \"BusyBox ash (Admin)\" ",
54 getcwd(NULL, 0)); 56 getcwd(NULL, 0));
55 if (opt_command) 57 if (opt_command)
56 info.lpParameters = 58 info.lpParameters =
diff --git a/shell/ash.c b/shell/ash.c
index 6b70dcde3..10da3ebed 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -15014,9 +15014,11 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
15014 state2: 15014 state2:
15015 state = 3; 15015 state = 3;
15016 if ( 15016 if (
15017#if ENABLE_PLATFORM_POSIX
15017#ifndef linux 15018#ifndef linux
15018 getuid() == geteuid() && getgid() == getegid() && 15019 getuid() == geteuid() && getgid() == getegid() &&
15019#endif 15020#endif
15021#endif
15020 iflag 15022 iflag
15021 ) { 15023 ) {
15022 const char *shinit = lookupvar("ENV"); 15024 const char *shinit = lookupvar("ENV");
diff --git a/win32/mingw.c b/win32/mingw.c
index b50c1ecee..84b059506 100644
--- a/win32/mingw.c
+++ b/win32/mingw.c
@@ -872,6 +872,25 @@ static char *get_user_name(void)
872 return user_name; 872 return user_name;
873} 873}
874 874
875int getuid(void)
876{
877 int ret = DEFAULT_UID;
878 HANDLE h;
879
880 if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &h)) {
881 TOKEN_ELEVATION elevation;
882 DWORD size = sizeof(TOKEN_ELEVATION);
883
884 if (GetTokenInformation(h, TokenElevation, &elevation,
885 sizeof(elevation), &size)) {
886 if (elevation.TokenIsElevated)
887 ret = 0;
888 }
889 CloseHandle(h);
890 }
891 return ret;
892}
893
875struct passwd *getpwnam(const char *name) 894struct passwd *getpwnam(const char *name)
876{ 895{
877 const char *myname; 896 const char *myname;
@@ -892,8 +911,17 @@ struct passwd *getpwuid(uid_t uid)
892 static struct passwd p; 911 static struct passwd p;
893 912
894 if (uid == 0) { 913 if (uid == 0) {
914 static char *buf = NULL;
915 char dir[PATH_MAX];
916
917 if (!buf) {
918 buf = xzalloc(PATH_MAX);
919 GetSystemDirectory(dir, PATH_MAX);
920 realpath(dir, buf);
921 }
922
895 p.pw_name = (char *)"root"; 923 p.pw_name = (char *)"root";
896 p.pw_dir = (char *)"/"; 924 p.pw_dir = buf;
897 } 925 }
898 else if (uid == DEFAULT_UID && (p.pw_name=get_user_name()) != NULL) { 926 else if (uid == DEFAULT_UID && (p.pw_name=get_user_name()) != NULL) {
899 p.pw_dir = gethomedir(); 927 p.pw_dir = gethomedir();
@@ -949,7 +977,7 @@ int getgroups(int n, gid_t *groups)
949 return 1; 977 return 1;
950 } 978 }
951 979
952 groups[0] = DEFAULT_GID; 980 groups[0] = getgid();
953 return 1; 981 return 1;
954} 982}
955 983
@@ -1576,21 +1604,3 @@ void hide_console(void)
1576 } 1604 }
1577} 1605}
1578#endif 1606#endif
1579
1580int is_admin(void)
1581{
1582 int ret = FALSE;
1583 HANDLE h;
1584
1585 if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &h)) {
1586 TOKEN_ELEVATION elevation;
1587 DWORD size = sizeof(TOKEN_ELEVATION);
1588
1589 if (GetTokenInformation(h, TokenElevation, &elevation,
1590 sizeof(elevation), &size)) {
1591 ret = elevation.TokenIsElevated;
1592 }
1593 CloseHandle(h);
1594 }
1595 return ret;
1596}