File xrdp-Add-function-to-get-user-information-by-UID.patch of Package xrdp.37241

From a16e56f711d1109311d1e51b612ef33ed55444c6 Mon Sep 17 00:00:00 2001
From: matt335672 <30179339+matt335672@users.noreply.github.com>
Date: Thu, 8 Sep 2022 14:00:36 +0100
Subject: [PATCH 01/11] 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.
---
 common/os_calls.c | 76 ++++++++++++++++++++++++++++++++++++++++-------
 common/os_calls.h |  6 ++--
 sesman/access.c   |  4 +--
 sesman/env.c      |  3 +-
 4 files changed, 73 insertions(+), 16 deletions(-)

Index: xrdp-0.9.13.1/common/os_calls.c
===================================================================
--- xrdp-0.9.13.1.orig/common/os_calls.c
+++ xrdp-0.9.13.1/common/os_calls.c
@@ -3235,7 +3235,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);
@@ -3438,26 +3438,85 @@ g_sigterm(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_message(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)
@@ -3465,11 +3524,6 @@ g_getuser_info(const char *username, int
             *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);
Index: xrdp-0.9.13.1/common/os_calls.h
===================================================================
--- xrdp-0.9.13.1.orig/common/os_calls.h
+++ xrdp-0.9.13.1/common/os_calls.h
@@ -170,8 +170,10 @@ char*    g_getenv(const char* name);
 int      g_exit(int exit_code);
 int      g_getpid(void);
 int      g_sigterm(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);
Index: xrdp-0.9.13.1/sesman/access.c
===================================================================
--- xrdp-0.9.13.1.orig/sesman/access.c
+++ xrdp-0.9.13.1/sesman/access.c
@@ -53,7 +53,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_message(LOG_LEVEL_ERROR, "Cannot read user info! - login denied");
         return 0;
@@ -102,7 +102,7 @@ access_login_mng_allowed(const char *use
         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_message(LOG_LEVEL_ERROR, "[MNG] Cannot read user info! - login denied");
         return 0;
Index: xrdp-0.9.13.1/sesman/env.c
===================================================================
--- xrdp-0.9.13.1.orig/sesman/env.c
+++ xrdp-0.9.13.1/sesman/env.c
@@ -108,7 +108,8 @@ env_set_user(const char *username, char
     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)
     {
openSUSE Build Service is sponsored by