diff --git a/common/os_calls.c b/common/os_calls.c index 095359c1..a30b60f7 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -2797,7 +2797,7 @@ g_initgroups(const char *username) return 0; #else int gid; - int error = g_getuser_info(username, &gid, NULL, NULL, NULL, NULL); + int error = g_getuser_info_by_name(username, NULL, &gid, NULL, NULL, NULL); if (error == 0) { error = initgroups(username, gid); @@ -3060,26 +3060,85 @@ g_sighup(int pid) /* the caller is responsible to free the buffs */ /* does not work in win32 */ int -g_getuser_info(const char *username, int *gid, int *uid, char **shell, - char **dir, char **gecos) +g_getuser_info_by_name(const char *username, int *uid, int *gid, + char **shell, char **dir, char **gecos) +{ + int rv = 1; +#if !defined(_WIN32) + + if (username == NULL) + { + LOG(LOG_LEVEL_ERROR, "g_getuser_info_by_name() called for NULL user"); + } + else + { + struct passwd *pwd_1 = getpwnam(username); + + if (pwd_1 != 0) + { + rv = 0; + + if (uid != 0) + { + *uid = pwd_1->pw_uid; + } + + if (gid != 0) + { + *gid = pwd_1->pw_gid; + } + + if (shell != 0) + { + *shell = g_strdup(pwd_1->pw_shell); + } + + if (dir != 0) + { + *dir = g_strdup(pwd_1->pw_dir); + } + + if (gecos != 0) + { + *gecos = g_strdup(pwd_1->pw_gecos); + } + } + } +#endif + return rv; +} + + +/*****************************************************************************/ +/* returns 0 if ok */ +/* the caller is responsible to free the buffs */ +/* does not work in win32 */ +int +g_getuser_info_by_uid(int uid, char **username, int *gid, + char **shell, char **dir, char **gecos) { #if defined(_WIN32) return 1; #else struct passwd *pwd_1; - pwd_1 = getpwnam(username); + pwd_1 = getpwuid(uid); if (pwd_1 != 0) { + if (username != NULL) + { + *username = g_strdup(pwd_1->pw_name); + } + if (gid != 0) { *gid = pwd_1->pw_gid; } - if (uid != 0) + if (shell != 0) { - *uid = pwd_1->pw_uid; + *shell = g_strdup(pwd_1->pw_shell); } if (dir != 0) @@ -3087,11 +3146,6 @@ g_getuser_info(const char *username, int *gid, int *uid, char **shell, *dir = g_strdup(pwd_1->pw_dir); } - if (shell != 0) - { - *shell = g_strdup(pwd_1->pw_shell); - } - if (gecos != 0) { *gecos = g_strdup(pwd_1->pw_gecos); diff --git a/common/os_calls.h b/common/os_calls.h index df24cd69..b1153571 100644 --- a/common/os_calls.h +++ b/common/os_calls.h @@ -193,8 +193,10 @@ int g_exit(int exit_code); int g_getpid(void); int g_sigterm(int pid); int g_sighup(int pid); -int g_getuser_info(const char *username, int *gid, int *uid, char **shell, - char **dir, char **gecos); +int g_getuser_info_by_name(const char *username, int *uid, int *gid, + char **shell, char **dir, char **gecos); +int g_getuser_info_by_uid(int uid, char **username, int *gid, + char **shell, char **dir, char **gecos); int g_getgroup_info(const char *groupname, int *gid); int g_check_user_in_group(const char *username, int gid, int *ok); int g_time1(void); diff --git a/sesman/access.c b/sesman/access.c index 442d9e7b..6c7eb264 100644 --- a/sesman/access.c +++ b/sesman/access.c @@ -51,7 +51,7 @@ access_login_allowed(const char *user) return 1; } - if (0 != g_getuser_info(user, &gid, 0, 0, 0, 0)) + if (0 != g_getuser_info_by_name(user, 0, &gid, 0, 0, 0)) { LOG(LOG_LEVEL_ERROR, "Cannot read user info! - login denied"); return 0; @@ -100,7 +100,7 @@ access_login_mng_allowed(const char *user) return 1; } - if (0 != g_getuser_info(user, &gid, 0, 0, 0, 0)) + if (0 != g_getuser_info_by_name(user, 0, &gid, 0, 0, 0)) { LOG(LOG_LEVEL_ERROR, "[MNG] Cannot read user info! - login denied"); return 0; diff --git a/sesman/env.c b/sesman/env.c index 34a6a97f..0a4e6220 100644 --- a/sesman/env.c +++ b/sesman/env.c @@ -107,7 +107,8 @@ env_set_user(const char *username, char **passwd_file, int display, pw_shell = 0; pw_dir = 0; - error = g_getuser_info(username, &pw_gid, &pw_uid, &pw_shell, &pw_dir, 0); + error = g_getuser_info_by_name(username, &pw_uid, &pw_gid, &pw_shell, + &pw_dir, 0); if (error == 0) {