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);
102 token->Username =
nullptr;
104 token->Domain =
nullptr;
109 LogonUserCloseHandle,
130BOOL LogonUserA(LPCSTR lpszUsername, LPCSTR lpszDomain, WINPR_ATTR_UNUSED LPCSTR lpszPassword,
131 WINPR_ATTR_UNUSED DWORD dwLogonType, WINPR_ATTR_UNUSED DWORD dwLogonProvider,
137 WINPR_ACCESS_TOKEN* token = (WINPR_ACCESS_TOKEN*)calloc(1,
sizeof(WINPR_ACCESS_TOKEN));
142 WINPR_HANDLE_SET_TYPE_AND_MODE(token, HANDLE_TYPE_ACCESS_TOKEN, WINPR_FD_READ);
143 token->common.ops = &ops;
144 token->Username = _strdup(lpszUsername);
146 if (!token->Username)
151 token->Domain = _strdup(lpszDomain);
158 long buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
163 const size_t s = 1ULL + (size_t)buflen;
164 char* buf = (
char*)calloc(s,
sizeof(
char));
169 struct passwd pwd = WINPR_C_ARRAY_INIT;
170 struct passwd* pw =
nullptr;
171 const int rc = getpwnam_r(lpszUsername, &pwd, buf,
172 WINPR_ASSERTING_INT_CAST(
size_t, buflen), &pw);
176 token->UserId = (DWORD)pw->pw_uid;
177 token->GroupId = (DWORD)pw->pw_gid;
180 *((ULONG_PTR*)phToken) = (ULONG_PTR)token;
186 free(token->Username);
192BOOL LogonUserW(WINPR_ATTR_UNUSED LPCWSTR lpszUsername, WINPR_ATTR_UNUSED LPCWSTR lpszDomain,
193 WINPR_ATTR_UNUSED LPCWSTR lpszPassword, WINPR_ATTR_UNUSED DWORD dwLogonType,
194 WINPR_ATTR_UNUSED DWORD dwLogonProvider, WINPR_ATTR_UNUSED PHANDLE phToken)
196 WLog_ERR(
"TODO",
"TODO: implement");
200BOOL LogonUserExA(WINPR_ATTR_UNUSED LPCSTR lpszUsername, WINPR_ATTR_UNUSED LPCSTR lpszDomain,
201 WINPR_ATTR_UNUSED LPCSTR lpszPassword, WINPR_ATTR_UNUSED DWORD dwLogonType,
202 WINPR_ATTR_UNUSED DWORD dwLogonProvider, WINPR_ATTR_UNUSED PHANDLE phToken,
203 WINPR_ATTR_UNUSED PSID* ppLogonSid, WINPR_ATTR_UNUSED PVOID* ppProfileBuffer,
204 WINPR_ATTR_UNUSED LPDWORD pdwProfileLength,
207 WLog_ERR(
"TODO",
"TODO: implement");
211BOOL LogonUserExW(WINPR_ATTR_UNUSED LPCWSTR lpszUsername, WINPR_ATTR_UNUSED LPCWSTR lpszDomain,
212 WINPR_ATTR_UNUSED LPCWSTR lpszPassword, WINPR_ATTR_UNUSED DWORD dwLogonType,
213 WINPR_ATTR_UNUSED DWORD dwLogonProvider, WINPR_ATTR_UNUSED PHANDLE phToken,
214 WINPR_ATTR_UNUSED PSID* ppLogonSid, WINPR_ATTR_UNUSED PVOID* ppProfileBuffer,
215 WINPR_ATTR_UNUSED LPDWORD pdwProfileLength,
218 WLog_ERR(
"TODO",
"TODO: implement");
222BOOL GetUserNameExA(EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize)
224 WINPR_ASSERT(lpNameBuffer);
229 case NameSamCompatible:
230#if defined(WINPR_HAVE_GETPWUID_R)
233 struct passwd pwd = WINPR_C_ARRAY_INIT;
234 struct passwd* result =
nullptr;
235 uid_t uid = getuid();
237 rc = getpwuid_r(uid, &pwd, lpNameBuffer, *nSize, &result);
240 if (result ==
nullptr)
243#elif defined(WINPR_HAVE_GETLOGIN_R)
244 if (getlogin_r(lpNameBuffer, *nSize) != 0)
248 const char* name = getlogin();
251 strncpy(lpNameBuffer, name, strnlen(name, *nSize));
255 const size_t len = strnlen(lpNameBuffer, *nSize);
256 if (len > UINT32_MAX)
262 case NameFullyQualifiedDN:
266 case NameUserPrincipal:
267 case NameCanonicalEx:
268 case NameServicePrincipal:
279BOOL GetUserNameExW(EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG nSize)
282 char* name =
nullptr;
285 WINPR_ASSERT(lpNameBuffer);
287 name = calloc(1, *nSize + 1);
291 if (!GetUserNameExA(NameFormat, name, nSize))
295 const SSIZE_T res = ConvertUtf8ToWChar(name, lpNameBuffer, *nSize);
296 if ((res < 0) || (res >= UINT32_MAX))
299 *nSize = (UINT32)res + 1;