2015-03-05 00:56:48 +03:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Shell Functions
|
|
|
|
*
|
|
|
|
* Copyright 2015 Dell Software <Mike.McDonald@software.dell.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2015-03-05 00:56:48 +03:00
|
|
|
|
|
|
|
#include <winpr/shell.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* shell32.dll:
|
|
|
|
*
|
|
|
|
* GetUserProfileDirectoryA
|
|
|
|
* GetUserProfileDirectoryW
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_UNISTD_H
|
2015-03-05 00:56:48 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
|
|
|
#include "../handle/handle.h"
|
|
|
|
|
|
|
|
#include "../security/security.h"
|
|
|
|
|
|
|
|
BOOL GetUserProfileDirectoryA(HANDLE hToken, LPSTR lpProfileDir, LPDWORD lpcchSize)
|
|
|
|
{
|
2015-05-22 17:03:21 +03:00
|
|
|
char* buf;
|
|
|
|
int buflen;
|
|
|
|
int status;
|
2015-03-05 00:56:48 +03:00
|
|
|
DWORD cchDirSize;
|
2015-05-22 17:03:21 +03:00
|
|
|
struct passwd pwd;
|
|
|
|
struct passwd* pw = NULL;
|
2015-03-05 00:56:48 +03:00
|
|
|
WINPR_ACCESS_TOKEN* token;
|
2019-11-06 17:24:51 +03:00
|
|
|
token = (WINPR_ACCESS_TOKEN*)hToken;
|
2015-03-05 00:56:48 +03:00
|
|
|
|
2022-04-27 16:56:39 +03:00
|
|
|
if (!AccessTokenIsValid(hToken))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!lpcchSize)
|
2015-03-05 00:56:48 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-05-22 17:03:21 +03:00
|
|
|
buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
|
|
|
|
|
|
|
|
if (buflen == -1)
|
|
|
|
buflen = 8196;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
buf = (char*)malloc(buflen);
|
2015-05-22 17:03:21 +03:00
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
status = getpwnam_r(token->Username, &pwd, buf, buflen, &pw);
|
|
|
|
|
|
|
|
if ((status != 0) || !pw)
|
2015-03-05 00:56:48 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
2018-08-24 10:54:25 +03:00
|
|
|
free(buf);
|
2015-03-05 00:56:48 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cchDirSize = strlen(pw->pw_dir) + 1;
|
|
|
|
|
2015-05-22 17:03:21 +03:00
|
|
|
if (!lpProfileDir || (*lpcchSize < cchDirSize))
|
2015-03-05 00:56:48 +03:00
|
|
|
{
|
|
|
|
*lpcchSize = cchDirSize;
|
|
|
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
2015-05-22 17:03:21 +03:00
|
|
|
free(buf);
|
2015-03-05 00:56:48 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZeroMemory(lpProfileDir, *lpcchSize);
|
2018-08-24 10:54:25 +03:00
|
|
|
sprintf_s(lpProfileDir, *lpcchSize, "%s", pw->pw_dir);
|
2015-03-05 00:56:48 +03:00
|
|
|
*lpcchSize = cchDirSize;
|
2015-05-22 17:03:21 +03:00
|
|
|
free(buf);
|
2015-03-05 00:56:48 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL GetUserProfileDirectoryW(HANDLE hToken, LPWSTR lpProfileDir, LPDWORD lpcchSize)
|
|
|
|
{
|
|
|
|
BOOL bStatus;
|
|
|
|
DWORD cchSizeA;
|
|
|
|
LPSTR lpProfileDirA;
|
|
|
|
|
2015-05-22 17:03:21 +03:00
|
|
|
if (!lpcchSize)
|
2015-03-05 00:56:48 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cchSizeA = *lpcchSize;
|
|
|
|
lpProfileDirA = NULL;
|
|
|
|
|
|
|
|
if (lpProfileDir)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
lpProfileDirA = (LPSTR)malloc(cchSizeA);
|
2015-05-22 17:03:21 +03:00
|
|
|
|
2015-03-05 00:56:48 +03:00
|
|
|
if (lpProfileDirA == NULL)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bStatus = GetUserProfileDirectoryA(hToken, lpProfileDirA, &cchSizeA);
|
2015-05-22 17:03:21 +03:00
|
|
|
|
2015-03-05 00:56:48 +03:00
|
|
|
if (bStatus)
|
|
|
|
{
|
2022-10-28 09:09:27 +03:00
|
|
|
SSIZE_T size = ConvertUtf8NToWChar(lpProfileDirA, cchSizeA, lpProfileDir, *lpcchSize);
|
|
|
|
bStatus = size >= 0;
|
2015-03-05 00:56:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lpProfileDirA)
|
|
|
|
{
|
|
|
|
free(lpProfileDirA);
|
|
|
|
}
|
|
|
|
|
|
|
|
*lpcchSize = cchSizeA;
|
|
|
|
return bStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|