1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/**
* 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.
*/
#include <winpr/config.h>
#include <winpr/shell.h>
/**
* shell32.dll:
*
* GetUserProfileDirectoryA
* GetUserProfileDirectoryW
*/
#ifndef _WIN32
#include <winpr/crt.h>
#ifdef WINPR_HAVE_UNISTD_H
#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)
{
char* buf = NULL;
int buflen = 0;
int status = 0;
DWORD cchDirSize = 0;
struct passwd pwd;
struct passwd* pw = NULL;
WINPR_ACCESS_TOKEN* token = NULL;
token = (WINPR_ACCESS_TOKEN*)hToken;
if (!AccessTokenIsValid(hToken))
return FALSE;
if (!lpcchSize)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
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);
free(buf);
return FALSE;
}
cchDirSize = strlen(pw->pw_dir) + 1;
if (!lpProfileDir || (*lpcchSize < cchDirSize))
{
*lpcchSize = cchDirSize;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
free(buf);
return FALSE;
}
ZeroMemory(lpProfileDir, *lpcchSize);
sprintf_s(lpProfileDir, *lpcchSize, "%s", pw->pw_dir);
*lpcchSize = cchDirSize;
free(buf);
return TRUE;
}
BOOL GetUserProfileDirectoryW(HANDLE hToken, LPWSTR lpProfileDir, LPDWORD lpcchSize)
{
BOOL bStatus = 0;
DWORD cchSizeA = 0;
LPSTR lpProfileDirA = NULL;
if (!lpcchSize)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
cchSizeA = *lpcchSize;
lpProfileDirA = NULL;
if (lpProfileDir)
{
lpProfileDirA = (LPSTR)malloc(cchSizeA);
if (lpProfileDirA == NULL)
{
SetLastError(ERROR_OUTOFMEMORY);
return FALSE;
}
}
bStatus = GetUserProfileDirectoryA(hToken, lpProfileDirA, &cchSizeA);
if (bStatus)
{
SSIZE_T size = ConvertUtf8NToWChar(lpProfileDirA, cchSizeA, lpProfileDir, *lpcchSize);
bStatus = size >= 0;
}
if (lpProfileDirA)
{
free(lpProfileDirA);
}
*lpcchSize = cchSizeA;
return bStatus;
}
#endif
|