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;
166 const int rc = getpwnam_r(lpszUsername, &pwd, buf, buflen, &pw);
170 token->UserId = (DWORD)pw->pw_uid;
171 token->GroupId = (DWORD)pw->pw_gid;
174 *((ULONG_PTR*)phToken) = (ULONG_PTR)token;
178 free(token->Username);
184 BOOL LogonUserW(LPCWSTR lpszUsername, LPCWSTR lpszDomain, LPCWSTR lpszPassword, DWORD dwLogonType,
185 DWORD dwLogonProvider, PHANDLE phToken)
190 BOOL LogonUserExA(LPCSTR lpszUsername, LPCSTR lpszDomain, LPCSTR lpszPassword, DWORD dwLogonType,
191 DWORD dwLogonProvider, PHANDLE phToken, PSID* ppLogonSid, PVOID* ppProfileBuffer,
197 BOOL LogonUserExW(LPCWSTR lpszUsername, LPCWSTR lpszDomain, LPCWSTR lpszPassword, DWORD dwLogonType,
198 DWORD dwLogonProvider, PHANDLE phToken, PSID* ppLogonSid, PVOID* ppProfileBuffer,
204 BOOL GetUserNameExA(EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize)
206 WINPR_ASSERT(lpNameBuffer);
211 case NameSamCompatible:
212 #if defined(WINPR_HAVE_GETPWUID_R)
215 struct passwd pwd = { 0 };
216 struct passwd* result = NULL;
217 uid_t uid = getuid();
219 rc = getpwuid_r(uid, &pwd, lpNameBuffer, *nSize, &result);
225 #elif defined(WINPR_HAVE_GETLOGIN_R)
226 if (getlogin_r(lpNameBuffer, *nSize) != 0)
230 const char* name = getlogin();
233 strncpy(lpNameBuffer, name, strnlen(name, *nSize));
236 const size_t len = strnlen(lpNameBuffer, *nSize);
237 if (len > UINT32_MAX)
242 case NameFullyQualifiedDN:
246 case NameUserPrincipal:
247 case NameCanonicalEx:
248 case NameServicePrincipal:
259 BOOL GetUserNameExW(EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG nSize)
265 WINPR_ASSERT(lpNameBuffer);
267 name = calloc(1, *nSize + 1);
271 if (!GetUserNameExA(NameFormat, name, nSize))
274 const SSIZE_T res = ConvertUtf8ToWChar(name, lpNameBuffer, *nSize);
275 if ((res < 0) || (res >= UINT32_MAX))
278 *nSize = (UINT32)res + 1;