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);
156 long buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
160 const size_t s = 1ULL + (size_t)buflen;
161 char* buf = (
char*)calloc(s,
sizeof(
char));
165 struct passwd pwd = { 0 };
166 struct passwd* pw = NULL;
168 getpwnam_r(lpszUsername, &pwd, buf, WINPR_ASSERTING_INT_CAST(
size_t, buflen), &pw);
172 token->UserId = (DWORD)pw->pw_uid;
173 token->GroupId = (DWORD)pw->pw_gid;
176 *((ULONG_PTR*)phToken) = (ULONG_PTR)token;
180 free(token->Username);
186BOOL LogonUserW(WINPR_ATTR_UNUSED LPCWSTR lpszUsername, WINPR_ATTR_UNUSED LPCWSTR lpszDomain,
187 WINPR_ATTR_UNUSED LPCWSTR lpszPassword, WINPR_ATTR_UNUSED DWORD dwLogonType,
188 WINPR_ATTR_UNUSED DWORD dwLogonProvider, WINPR_ATTR_UNUSED PHANDLE phToken)
190 WLog_ERR(
"TODO",
"TODO: implement");
194BOOL LogonUserExA(WINPR_ATTR_UNUSED LPCSTR lpszUsername, WINPR_ATTR_UNUSED LPCSTR lpszDomain,
195 WINPR_ATTR_UNUSED LPCSTR lpszPassword, WINPR_ATTR_UNUSED DWORD dwLogonType,
196 WINPR_ATTR_UNUSED DWORD dwLogonProvider, WINPR_ATTR_UNUSED PHANDLE phToken,
197 WINPR_ATTR_UNUSED PSID* ppLogonSid, WINPR_ATTR_UNUSED PVOID* ppProfileBuffer,
198 WINPR_ATTR_UNUSED LPDWORD pdwProfileLength,
201 WLog_ERR(
"TODO",
"TODO: implement");
205BOOL LogonUserExW(WINPR_ATTR_UNUSED LPCWSTR lpszUsername, WINPR_ATTR_UNUSED LPCWSTR lpszDomain,
206 WINPR_ATTR_UNUSED LPCWSTR lpszPassword, WINPR_ATTR_UNUSED DWORD dwLogonType,
207 WINPR_ATTR_UNUSED DWORD dwLogonProvider, WINPR_ATTR_UNUSED PHANDLE phToken,
208 WINPR_ATTR_UNUSED PSID* ppLogonSid, WINPR_ATTR_UNUSED PVOID* ppProfileBuffer,
209 WINPR_ATTR_UNUSED LPDWORD pdwProfileLength,
212 WLog_ERR(
"TODO",
"TODO: implement");
216BOOL GetUserNameExA(EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize)
218 WINPR_ASSERT(lpNameBuffer);
223 case NameSamCompatible:
224#if defined(WINPR_HAVE_GETPWUID_R)
227 struct passwd pwd = { 0 };
228 struct passwd* result = NULL;
229 uid_t uid = getuid();
231 rc = getpwuid_r(uid, &pwd, lpNameBuffer, *nSize, &result);
237#elif defined(WINPR_HAVE_GETLOGIN_R)
238 if (getlogin_r(lpNameBuffer, *nSize) != 0)
242 const char* name = getlogin();
245 strncpy(lpNameBuffer, name, strnlen(name, *nSize));
248 const size_t len = strnlen(lpNameBuffer, *nSize);
249 if (len > UINT32_MAX)
254 case NameFullyQualifiedDN:
258 case NameUserPrincipal:
259 case NameCanonicalEx:
260 case NameServicePrincipal:
271BOOL GetUserNameExW(EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG nSize)
277 WINPR_ASSERT(lpNameBuffer);
279 name = calloc(1, *nSize + 1);
283 if (!GetUserNameExA(NameFormat, name, nSize))
286 const SSIZE_T res = ConvertUtf8ToWChar(name, lpNameBuffer, *nSize);
287 if ((res < 0) || (res >= UINT32_MAX))
290 *nSize = (UINT32)res + 1;