Added support for getpwuid_r, replaced duplicate code

* Use GetUserNameExA to get current user name when required.
* Add support for getpwuid_r if available
This commit is contained in:
akallabeth
2022-01-25 12:27:17 +01:00
committed by akallabeth
parent d0488f8692
commit 7dedfbb0b0
8 changed files with 61 additions and 41 deletions

View File

@@ -581,8 +581,6 @@ if(UNIX OR CYGWIN)
set(WAYLAND_FEATURE_TYPE "RECOMMENDED")
include(CheckFunctionExists)
check_function_exists(getlogin_r HAVE_GETLOGIN_R)
else()
set(X11_FEATURE_TYPE "DISABLED")
set(WAYLAND_FEATURE_TYPE "DISABLED")

View File

@@ -35,6 +35,7 @@
#include <winpr/stream.h>
#include <winpr/print.h>
#include <winpr/sspicli.h>
#include <freerdp/types.h>
#include <freerdp/constants.h>
@@ -646,16 +647,13 @@ static BOOL isAutomountLocation(const char* path)
{
const size_t nrLocations = sizeof(automountLocations) / sizeof(automountLocations[0]);
size_t x;
char buffer[MAX_PATH];
char buffer[MAX_PATH] = { 0 };
uid_t uid = getuid();
char uname[MAX_PATH] = { 0 };
ULONG size = sizeof(uname) - 1;
#ifndef HAVE_GETLOGIN_R
strncpy(uname, getlogin(), sizeof(uname));
#else
if (getlogin_r(uname, sizeof(uname)) != 0)
if (!GetUserNameExA(NameSamCompatible, uname, &size))
return FALSE;
#endif
if (!path)
return FALSE;

View File

@@ -27,6 +27,8 @@
#endif
#include <winpr/assert.h>
#include <winpr/sspicli.h>
#include <float.h>
#include <X11/Xlib.h>
@@ -1180,20 +1182,12 @@ static BOOL xf_pre_connect(freerdp* instance)
if (!settings->Username && !settings->CredentialsFromStdin && !settings->SmartcardLogon)
{
int rc;
char login_name[MAX_PATH] = { 0 };
ULONG size = sizeof(login_name) - 1;
#ifdef HAVE_GETLOGIN_R
rc = getlogin_r(login_name, sizeof(login_name));
#else
strncpy(login_name, getlogin(), sizeof(login_name));
rc = 0;
#endif
if (rc == 0)
if (GetUserNameExA(NameSamCompatible, login_name, &size))
{
settings->Username = _strdup(login_name);
if (!settings->Username)
if (!freerdp_settings_set_string(settings, FreeRDP_Username, login_name))
return FALSE;
WLog_INFO(TAG, "No user name set. - Using login name: %s", settings->Username);

View File

@@ -16,7 +16,6 @@
#cmakedefine HAVE_JOURNALD_H
#cmakedefine HAVE_VALGRIND_MEMCHECK_H
#cmakedefine HAVE_EXECINFO_H
#cmakedefine HAVE_GETLOGIN_R
#cmakedefine HAVE_STRNDUP
/* Features */

View File

@@ -6,7 +6,6 @@
#cmakedefine HAVE_POLL_H
#cmakedefine HAVE_SYSLOG_H
#cmakedefine HAVE_JOURNALD_H
#cmakedefine HAVE_GETLOGIN_R
#cmakedefine HAVE_STRNDUP
#cmakedefine HAVE_PIXMAN_REGION

View File

@@ -158,6 +158,8 @@ if(NOT IOS)
if (HAVE_SYS_EVENTFD_H)
check_symbol_exists(eventfd_read sys/eventfd.h WITH_EVENTFD_READ_WRITE)
endif()
check_function_exists(getlogin_r HAVE_GETLOGIN_R)
check_function_exists(getpwuid_r HAVE_GETPWUID_R)
else()
set(HAVE_FCNTL_H 1)
set(HAVE_UNISTD_H 1)

View File

@@ -23,6 +23,7 @@
#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK
#cmakedefine HAVE_EXECINFO_H
#cmakedefine HAVE_GETLOGIN_R
#cmakedefine HAVE_GETPWUID_R
#cmakedefine HAVE_STRNDUP
#cmakedefine WITH_EVENTFD_READ_WRITE

View File

@@ -21,6 +21,7 @@
#include "config.h"
#endif
#include <winpr/assert.h>
#include <winpr/sspicli.h>
/**
@@ -60,6 +61,12 @@
#include <unistd.h>
#endif
#if defined(HAVE_GETPWUID_R)
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#endif
#include <pthread.h>
#include <pwd.h>
@@ -200,30 +207,30 @@ BOOL LogonUserExW(LPCWSTR lpszUsername, LPCWSTR lpszDomain, LPCWSTR lpszPassword
BOOL GetUserNameExA(EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize)
{
size_t length;
char login[MAX_PATH];
switch (NameFormat)
{
case NameSamCompatible:
#ifndef HAVE_GETLOGIN_R
strncpy(login, getlogin(), sizeof(login));
#else
if (getlogin_r(login, sizeof(login)) != 0)
#if defined(HAVE_GETPWUID_R)
{
int rc;
struct passwd pwd = { 0 };
struct passwd* result = NULL;
uid_t uid = getuid();
rc = getpwuid_r(uid, &pwd, lpNameBuffer, *nSize, &result);
if (rc != 0)
return FALSE;
if (result == NULL)
return FALSE;
}
#elif defined(HAVE_GETLOGIN_R)
if (getlogin_r(lpNameBuffer, *nSize) != 0)
return FALSE;
#else
strncpy(lpNameBuffer, getlogin(), *nSize);
#endif
length = strlen(login);
if (*nSize >= length)
{
CopyMemory(lpNameBuffer, login, length + 1);
return TRUE;
}
else
{
*nSize = length + 1;
}
if (*nSize > 1)
*nSize = strnlen(lpNameBuffer, *nSize - 1) + 1;
break;
case NameFullyQualifiedDN:
@@ -245,7 +252,29 @@ BOOL GetUserNameExA(EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG
BOOL GetUserNameExW(EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG nSize)
{
return 0;
int res;
BOOL rc = FALSE;
char* name;
WINPR_ASSERT(nSize);
WINPR_ASSERT(lpNameBuffer);
name = calloc(1, *nSize + 1);
if (!name)
goto fail;
if (!GetUserNameExA(NameFormat, name, nSize))
goto fail;
res = ConvertToUnicode(CP_UTF8, 0, name, -1, &lpNameBuffer, *nSize);
if (res < 0)
goto fail;
*nSize = res + 1;
rc = TRUE;
fail:
free(name);
return rc;
}
#endif