From b04bbc0109046ee69806a472fd7e44313c646687 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 10 Mar 2019 09:58:13 +0000 Subject: 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. --- win32/mingw.c | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'win32') 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) return user_name; } +int getuid(void) +{ + int ret = DEFAULT_UID; + HANDLE h; + + if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &h)) { + TOKEN_ELEVATION elevation; + DWORD size = sizeof(TOKEN_ELEVATION); + + if (GetTokenInformation(h, TokenElevation, &elevation, + sizeof(elevation), &size)) { + if (elevation.TokenIsElevated) + ret = 0; + } + CloseHandle(h); + } + return ret; +} + struct passwd *getpwnam(const char *name) { const char *myname; @@ -892,8 +911,17 @@ struct passwd *getpwuid(uid_t uid) static struct passwd p; if (uid == 0) { + static char *buf = NULL; + char dir[PATH_MAX]; + + if (!buf) { + buf = xzalloc(PATH_MAX); + GetSystemDirectory(dir, PATH_MAX); + realpath(dir, buf); + } + p.pw_name = (char *)"root"; - p.pw_dir = (char *)"/"; + p.pw_dir = buf; } else if (uid == DEFAULT_UID && (p.pw_name=get_user_name()) != NULL) { p.pw_dir = gethomedir(); @@ -949,7 +977,7 @@ int getgroups(int n, gid_t *groups) return 1; } - groups[0] = DEFAULT_GID; + groups[0] = getgid(); return 1; } @@ -1576,21 +1604,3 @@ void hide_console(void) } } #endif - -int is_admin(void) -{ - int ret = FALSE; - HANDLE h; - - if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &h)) { - TOKEN_ELEVATION elevation; - DWORD size = sizeof(TOKEN_ELEVATION); - - if (GetTokenInformation(h, TokenElevation, &elevation, - sizeof(elevation), &size)) { - ret = elevation.TokenIsElevated; - } - CloseHandle(h); - } - return ret; -} -- cgit v1.2.3-55-g6feb