20 #include <winpr/config.h>
22 #include <winpr/assert.h>
23 #include <winpr/sspicli.h>
56 #include <winpr/crt.h>
58 #ifdef WINPR_HAVE_UNISTD_H
62 #if defined(WINPR_HAVE_GETPWUID_R)
63 #include <sys/types.h>
71 #include "../handle/handle.h"
73 #include "../security/security.h"
75 static BOOL LogonUserCloseHandle(HANDLE handle);
77 static BOOL LogonUserIsHandled(HANDLE handle)
79 return WINPR_HANDLE_IS_HANDLED(handle, HANDLE_TYPE_ACCESS_TOKEN, FALSE);
82 static int LogonUserGetFd(HANDLE handle)
84 WINPR_ACCESS_TOKEN* pLogonUser = (WINPR_ACCESS_TOKEN*)handle;
86 if (!LogonUserIsHandled(handle))
94 BOOL LogonUserCloseHandle(HANDLE handle)
96 WINPR_ACCESS_TOKEN* token = (WINPR_ACCESS_TOKEN*)handle;
98 if (!handle || !LogonUserIsHandled(handle))
101 free(token->Username);
108 LogonUserCloseHandle,
129 BOOL LogonUserA(LPCSTR lpszUsername, LPCSTR lpszDomain, LPCSTR lpszPassword, DWORD dwLogonType,
130 DWORD dwLogonProvider, PHANDLE phToken)
135 WINPR_ACCESS_TOKEN* token = (WINPR_ACCESS_TOKEN*)calloc(1,
sizeof(WINPR_ACCESS_TOKEN));
140 WINPR_HANDLE_SET_TYPE_AND_MODE(token, HANDLE_TYPE_ACCESS_TOKEN, WINPR_FD_READ);
141 token->common.ops = &ops;
142 token->Username = _strdup(lpszUsername);
144 if (!token->Username)
149 token->Domain = _strdup(lpszDomain);
155 long buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
159 const size_t s = 1ULL + (size_t)buflen;
160 char* buf = (
char*)calloc(s,
sizeof(
char));
164 struct passwd pwd = { 0 };
165 struct passwd* pw = NULL;
167 getpwnam_r(lpszUsername, &pwd, buf, WINPR_ASSERTING_INT_CAST(
size_t, buflen), &pw);
171 token->UserId = (DWORD)pw->pw_uid;
172 token->GroupId = (DWORD)pw->pw_gid;
175 *((ULONG_PTR*)phToken) = (ULONG_PTR)token;
179 free(token->Username);
185 BOOL LogonUserW(LPCWSTR lpszUsername, LPCWSTR lpszDomain, LPCWSTR lpszPassword, DWORD dwLogonType,
186 DWORD dwLogonProvider, PHANDLE phToken)
191 BOOL LogonUserExA(LPCSTR lpszUsername, LPCSTR lpszDomain, LPCSTR lpszPassword, DWORD dwLogonType,
192 DWORD dwLogonProvider, PHANDLE phToken, PSID* ppLogonSid, PVOID* ppProfileBuffer,
198 BOOL LogonUserExW(LPCWSTR lpszUsername, LPCWSTR lpszDomain, LPCWSTR lpszPassword, DWORD dwLogonType,
199 DWORD dwLogonProvider, PHANDLE phToken, PSID* ppLogonSid, PVOID* ppProfileBuffer,
205 BOOL GetUserNameExA(EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize)
207 WINPR_ASSERT(lpNameBuffer);
212 case NameSamCompatible:
213 #if defined(WINPR_HAVE_GETPWUID_R)
216 struct passwd pwd = { 0 };
217 struct passwd* result = NULL;
218 uid_t uid = getuid();
220 rc = getpwuid_r(uid, &pwd, lpNameBuffer, *nSize, &result);
226 #elif defined(WINPR_HAVE_GETLOGIN_R)
227 if (getlogin_r(lpNameBuffer, *nSize) != 0)
231 const char* name = getlogin();
234 strncpy(lpNameBuffer, name, strnlen(name, *nSize));
237 const size_t len = strnlen(lpNameBuffer, *nSize);
238 if (len > UINT32_MAX)
243 case NameFullyQualifiedDN:
247 case NameUserPrincipal:
248 case NameCanonicalEx:
249 case NameServicePrincipal:
260 BOOL GetUserNameExW(EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG nSize)
266 WINPR_ASSERT(lpNameBuffer);
268 name = calloc(1, *nSize + 1);
272 if (!GetUserNameExA(NameFormat, name, nSize))
275 const SSIZE_T res = ConvertUtf8ToWChar(name, lpNameBuffer, *nSize);
276 if ((res < 0) || (res >= UINT32_MAX))
279 *nSize = (UINT32)res + 1;