From 59abbf391be4ddaab0e0335b4f3691cc48590574 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 26 Feb 2015 15:50:39 +0000 Subject: Make uid/gid handling more consistent Various fake POSIX routines returned different values for uid/gid: getuid/getgid used 1, stat used 0 and getpwuid used 1000. Standardise on 1000. Also, add fake getgrgid. --- include/mingw.h | 16 ++++++++------ win32/mingw.c | 65 ++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 61 insertions(+), 20 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index 090dd1776..970b689fd 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -13,6 +13,9 @@ typedef int pid_t; typedef __int64 pid_t; #endif +#define DEFAULT_UID 1000 +#define DEFAULT_GID 1000 + /* * arpa/inet.h */ @@ -42,7 +45,7 @@ struct group { char **gr_mem; }; IMPL(getgrnam,struct group *,NULL,const char *name UNUSED_PARAM); -IMPL(getgrgid,struct group *,NULL,gid_t gid UNUSED_PARAM); +struct group *getgrgid(gid_t gid); NOIMPL(initgroups,const char *group UNUSED_PARAM,gid_t gid UNUSED_PARAM); static inline void endgrent(void) {} @@ -71,6 +74,7 @@ struct sockaddr_un { */ struct passwd { char *pw_name; + char *pw_passwd; char *pw_gecos; char *pw_dir; char *pw_shell; @@ -79,7 +83,7 @@ struct passwd { }; IMPL(getpwnam,struct passwd *,NULL,const char *name UNUSED_PARAM); -struct passwd *getpwuid(int uid); +struct passwd *getpwuid(uid_t uid); static inline void setpwent(void) {} static inline void endpwent(void) {} IMPL(getpwent_r,int,ENOENT,struct passwd *pwbuf UNUSED_PARAM,char *buf UNUSED_PARAM,size_t buflen UNUSED_PARAM,struct passwd **pwbufp UNUSED_PARAM); @@ -360,13 +364,13 @@ int mingw_dup2 (int fd, int fdto); char *mingw_getcwd(char *pointer, int len); -IMPL(getgid,int,1,void); +IMPL(getgid,int,DEFAULT_GID,void); NOIMPL(getgroups,int n UNUSED_PARAM,gid_t *groups UNUSED_PARAM); IMPL(getppid,int,1,void); -IMPL(getegid,int,1,void); -IMPL(geteuid,int,1,void); +IMPL(getegid,int,DEFAULT_GID,void); +IMPL(geteuid,int,DEFAULT_UID,void); NOIMPL(getsid,pid_t pid UNUSED_PARAM); -IMPL(getuid,int,1,void); +IMPL(getuid,int,DEFAULT_UID,void); int fcntl(int fd, int cmd, ...); #define fork() -1 IMPL(fsync,int,0,int fd UNUSED_PARAM); diff --git a/win32/mingw.c b/win32/mingw.c index 83a7ab3a7..197f331f6 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -233,8 +233,8 @@ static int do_lstat(int follow, const char *file_name, struct mingw_stat *buf) int len = strlen(file_name); buf->st_ino = 0; - buf->st_gid = 0; - buf->st_uid = 0; + buf->st_uid = DEFAULT_UID; + buf->st_gid = DEFAULT_GID; buf->st_nlink = 1; buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); if (len > 4 && (!strcasecmp(file_name+len-4, ".exe") || @@ -340,8 +340,8 @@ int mingw_fstat(int fd, struct mingw_stat *buf) buf->st_ino = 0; buf->st_mode = S_IREAD|S_IWRITE; buf->st_nlink = 1; - buf->st_uid = 0; - buf->st_gid = 0; + buf->st_uid = DEFAULT_UID; + buf->st_gid = DEFAULT_GID; buf->st_rdev = 0; buf->st_size = buf64.st_size; buf->st_atime = buf64.st_atime; @@ -353,8 +353,8 @@ int mingw_fstat(int fd, struct mingw_stat *buf) if (GetFileInformationByHandle(fh, &fdata)) { buf->st_ino = 0; - buf->st_gid = 0; - buf->st_uid = 0; + buf->st_uid = DEFAULT_UID; + buf->st_gid = DEFAULT_GID; buf->st_nlink = 1; buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); buf->st_size = fdata.nFileSizeLow | @@ -568,25 +568,62 @@ static char *gethomedir(void) return buf; } -struct passwd *getpwuid(int uid UNUSED_PARAM) +static char *get_user_name(void) { - static char user_name[100]; - static struct passwd p; + static char user_name[100] = ""; + char *s; DWORD len = sizeof(user_name); - user_name[0] = '\0'; - if (!GetUserName(user_name, &len)) + if ( user_name[0] != '\0' ) { + return user_name; + } + + if ( !GetUserName(user_name, &len) ) { return NULL; - p.pw_name = user_name; + } + + for ( s=user_name; *s; ++s ) { + if ( *s == ' ' ) { + *s = '_'; + } + } + + return user_name; +} + +struct passwd *getpwuid(uid_t uid UNUSED_PARAM) +{ + static struct passwd p; + + if ( (p.pw_name=get_user_name()) == NULL ) { + return NULL; + } + p.pw_passwd = (char *)"secret"; p.pw_gecos = (char *)"unknown"; p.pw_dir = gethomedir(); p.pw_shell = NULL; - p.pw_uid = 1000; - p.pw_gid = 1000; + p.pw_uid = DEFAULT_UID; + p.pw_gid = DEFAULT_GID; return &p; } +struct group *getgrgid(gid_t gid UNUSED_PARAM) +{ + static char *members[2] = { NULL, NULL }; + static struct group g; + + if ( (g.gr_name=get_user_name()) == NULL ) { + return NULL; + } + g.gr_passwd = (char *)"secret"; + g.gr_gid = DEFAULT_GID; + members[0] = g.gr_name; + g.gr_mem = members; + + return &g; +} + long sysconf(int name) { if ( name == _SC_CLK_TCK ) { -- cgit v1.2.3-55-g6feb