libwinpr-shell: use getpwnam_r

This commit is contained in:
Marc-André Moreau 2015-05-22 10:03:21 -04:00
parent 89642923d1
commit 7a06640c16
1 changed files with 25 additions and 6 deletions

View File

@ -47,20 +47,35 @@
BOOL GetUserProfileDirectoryA(HANDLE hToken, LPSTR lpProfileDir, LPDWORD lpcchSize) BOOL GetUserProfileDirectoryA(HANDLE hToken, LPSTR lpProfileDir, LPDWORD lpcchSize)
{ {
char* buf;
int buflen;
int status;
DWORD cchDirSize; DWORD cchDirSize;
struct passwd* pw; struct passwd pwd;
struct passwd* pw = NULL;
WINPR_ACCESS_TOKEN* token; WINPR_ACCESS_TOKEN* token;
token = (WINPR_ACCESS_TOKEN*) hToken; token = (WINPR_ACCESS_TOKEN*) hToken;
if ((token == NULL) || (token->Type != HANDLE_TYPE_ACCESS_TOKEN) || (lpcchSize == NULL)) if (!token || (token->Type != HANDLE_TYPE_ACCESS_TOKEN) || !lpcchSize)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
} }
pw = getpwnam(token->Username); buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
if (pw == NULL)
if (buflen == -1)
buflen = 8196;
buf = (char*) malloc(buflen);
if (!buf)
return FALSE;
status = getpwnam_r(token->Username, &pwd, buf, buflen, &pw);
if ((status != 0) || !pw)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -68,16 +83,18 @@ BOOL GetUserProfileDirectoryA(HANDLE hToken, LPSTR lpProfileDir, LPDWORD lpcchSi
cchDirSize = strlen(pw->pw_dir) + 1; cchDirSize = strlen(pw->pw_dir) + 1;
if ((lpProfileDir == NULL) || (*lpcchSize < cchDirSize)) if (!lpProfileDir || (*lpcchSize < cchDirSize))
{ {
*lpcchSize = cchDirSize; *lpcchSize = cchDirSize;
SetLastError(ERROR_INSUFFICIENT_BUFFER); SetLastError(ERROR_INSUFFICIENT_BUFFER);
free(buf);
return FALSE; return FALSE;
} }
ZeroMemory(lpProfileDir, *lpcchSize); ZeroMemory(lpProfileDir, *lpcchSize);
strcpy(lpProfileDir, pw->pw_dir); strcpy(lpProfileDir, pw->pw_dir);
*lpcchSize = cchDirSize; *lpcchSize = cchDirSize;
free(buf);
return TRUE; return TRUE;
} }
@ -88,7 +105,7 @@ BOOL GetUserProfileDirectoryW(HANDLE hToken, LPWSTR lpProfileDir, LPDWORD lpcchS
DWORD cchSizeA; DWORD cchSizeA;
LPSTR lpProfileDirA; LPSTR lpProfileDirA;
if (lpcchSize == NULL) if (!lpcchSize)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -100,6 +117,7 @@ BOOL GetUserProfileDirectoryW(HANDLE hToken, LPWSTR lpProfileDir, LPDWORD lpcchS
if (lpProfileDir) if (lpProfileDir)
{ {
lpProfileDirA = (LPSTR) malloc(cchSizeA); lpProfileDirA = (LPSTR) malloc(cchSizeA);
if (lpProfileDirA == NULL) if (lpProfileDirA == NULL)
{ {
SetLastError(ERROR_OUTOFMEMORY); SetLastError(ERROR_OUTOFMEMORY);
@ -108,6 +126,7 @@ BOOL GetUserProfileDirectoryW(HANDLE hToken, LPWSTR lpProfileDir, LPDWORD lpcchS
} }
bStatus = GetUserProfileDirectoryA(hToken, lpProfileDirA, &cchSizeA); bStatus = GetUserProfileDirectoryA(hToken, lpProfileDirA, &cchSizeA);
if (bStatus) if (bStatus)
{ {
MultiByteToWideChar(CP_ACP, 0, lpProfileDirA, cchSizeA, lpProfileDir, *lpcchSize); MultiByteToWideChar(CP_ACP, 0, lpProfileDirA, cchSizeA, lpProfileDir, *lpcchSize);