20#include <winpr/config.h>
22#include <winpr/assert.h>
23#include <winpr/sspicli.h>
58#ifdef WINPR_HAVE_UNISTD_H
62#if defined(WINPR_HAVE_GETPWUID_R)
71#include "../handle/handle.h"
73#include "../security/security.h"
75static BOOL LogonUserCloseHandle(HANDLE handle);
77static BOOL LogonUserIsHandled(HANDLE handle)
79 return WINPR_HANDLE_IS_HANDLED(handle, HANDLE_TYPE_ACCESS_TOKEN, FALSE);
82static int LogonUserGetFd(HANDLE handle)
84 WINPR_ACCESS_TOKEN* pLogonUser = (WINPR_ACCESS_TOKEN*)handle;
86 if (!LogonUserIsHandled(handle))
94BOOL LogonUserCloseHandle(HANDLE handle)
96 WINPR_ACCESS_TOKEN* token = (WINPR_ACCESS_TOKEN*)handle;
98 if (!handle || !LogonUserIsHandled(handle))
101 free(token->Username);
108 LogonUserCloseHandle,
129BOOL LogonUserA(LPCSTR lpszUsername, LPCSTR lpszDomain, WINPR_ATTR_UNUSED LPCSTR lpszPassword,
130 WINPR_ATTR_UNUSED DWORD dwLogonType, WINPR_ATTR_UNUSED DWORD dwLogonProvider,
136 WINPR_ACCESS_TOKEN* token = (WINPR_ACCESS_TOKEN*)calloc(1,
sizeof(WINPR_ACCESS_TOKEN));
141 WINPR_HANDLE_SET_TYPE_AND_MODE(token, HANDLE_TYPE_ACCESS_TOKEN, WINPR_FD_READ);
142 token->common.ops = &ops;
143 token->Username = _strdup(lpszUsername);
145 if (!token->Username)
150 token->Domain = _strdup(lpszDomain);
157 long buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
162 const size_t s = 1ULL + (size_t)buflen;
163 char* buf = (
char*)calloc(s,
sizeof(
char));
168 struct passwd pwd = { 0 };
169 struct passwd* pw = NULL;
170 const int rc = getpwnam_r(lpszUsername, &pwd, buf,
171 WINPR_ASSERTING_INT_CAST(
size_t, buflen), &pw);
175 token->UserId = (DWORD)pw->pw_uid;
176 token->GroupId = (DWORD)pw->pw_gid;
179 *((ULONG_PTR*)phToken) = (ULONG_PTR)token;
185 free(token->Username);
191BOOL LogonUserW(WINPR_ATTR_UNUSED LPCWSTR lpszUsername, WINPR_ATTR_UNUSED LPCWSTR lpszDomain,
192 WINPR_ATTR_UNUSED LPCWSTR lpszPassword, WINPR_ATTR_UNUSED DWORD dwLogonType,
193 WINPR_ATTR_UNUSED DWORD dwLogonProvider, WINPR_ATTR_UNUSED PHANDLE phToken)
195 WLog_ERR(
"TODO",
"TODO: implement");
199BOOL LogonUserExA(WINPR_ATTR_UNUSED LPCSTR lpszUsername, WINPR_ATTR_UNUSED LPCSTR lpszDomain,
200 WINPR_ATTR_UNUSED LPCSTR lpszPassword, WINPR_ATTR_UNUSED DWORD dwLogonType,
201 WINPR_ATTR_UNUSED DWORD dwLogonProvider, WINPR_ATTR_UNUSED PHANDLE phToken,
202 WINPR_ATTR_UNUSED PSID* ppLogonSid, WINPR_ATTR_UNUSED PVOID* ppProfileBuffer,
203 WINPR_ATTR_UNUSED LPDWORD pdwProfileLength,
206 WLog_ERR(
"TODO",
"TODO: implement");
210BOOL LogonUserExW(WINPR_ATTR_UNUSED LPCWSTR lpszUsername, WINPR_ATTR_UNUSED LPCWSTR lpszDomain,
211 WINPR_ATTR_UNUSED LPCWSTR lpszPassword, WINPR_ATTR_UNUSED DWORD dwLogonType,
212 WINPR_ATTR_UNUSED DWORD dwLogonProvider, WINPR_ATTR_UNUSED PHANDLE phToken,
213 WINPR_ATTR_UNUSED PSID* ppLogonSid, WINPR_ATTR_UNUSED PVOID* ppProfileBuffer,
214 WINPR_ATTR_UNUSED LPDWORD pdwProfileLength,
217 WLog_ERR(
"TODO",
"TODO: implement");
221BOOL GetUserNameExA(EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize)
223 WINPR_ASSERT(lpNameBuffer);
228 case NameSamCompatible:
229#if defined(WINPR_HAVE_GETPWUID_R)
232 struct passwd pwd = { 0 };
233 struct passwd* result = NULL;
234 uid_t uid = getuid();
236 rc = getpwuid_r(uid, &pwd, lpNameBuffer, *nSize, &result);
242#elif defined(WINPR_HAVE_GETLOGIN_R)
243 if (getlogin_r(lpNameBuffer, *nSize) != 0)
247 const char* name = getlogin();
250 strncpy(lpNameBuffer, name, strnlen(name, *nSize));
254 const size_t len = strnlen(lpNameBuffer, *nSize);
255 if (len > UINT32_MAX)
261 case NameFullyQualifiedDN:
265 case NameUserPrincipal:
266 case NameCanonicalEx:
267 case NameServicePrincipal:
278BOOL GetUserNameExW(EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG nSize)
284 WINPR_ASSERT(lpNameBuffer);
286 name = calloc(1, *nSize + 1);
290 if (!GetUserNameExA(NameFormat, name, nSize))
294 const SSIZE_T res = ConvertUtf8ToWChar(name, lpNameBuffer, *nSize);
295 if ((res < 0) || (res >= UINT32_MAX))
298 *nSize = (UINT32)res + 1;