Add function to get user information by UID

Moving to a uid_t to store the user information makes a lot
of sense. When doing this, we need a function to get information
about a user from the uid_t

As well as creating the function g_getuser_info_by_uid() we also
rename g_getuser_info() to g_getuser_info_by_name() and make the
parameter ordering more usual.
This commit is contained in:
matt335672 2022-09-08 14:00:36 +01:00
parent 752b4bcaa5
commit a16e56f711
4 changed files with 73 additions and 16 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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)
{