2005-08-27 14:56:17 +00:00
|
|
|
/*
|
2004-01-20 01:40:19 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
|
|
|
* FILE: lib/advapi32/misc/logon.c
|
|
|
|
* PURPOSE: Logon functions
|
|
|
|
* PROGRAMMER: Eric Kohl
|
|
|
|
*/
|
|
|
|
|
2005-08-05 10:31:28 +00:00
|
|
|
#include <advapi32.h>
|
2004-03-09 15:04:28 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
|
2004-01-20 01:40:19 +00:00
|
|
|
/* FUNCTIONS ***************************************************************/
|
|
|
|
|
|
|
|
/*
|
2004-01-20 23:16:42 +00:00
|
|
|
* @implemented
|
2004-01-20 01:40:19 +00:00
|
|
|
*/
|
|
|
|
BOOL STDCALL
|
|
|
|
CreateProcessAsUserA (HANDLE hToken,
|
|
|
|
LPCSTR lpApplicationName,
|
|
|
|
LPSTR lpCommandLine,
|
|
|
|
LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
|
|
|
LPSECURITY_ATTRIBUTES lpThreadAttributes,
|
|
|
|
BOOL bInheritHandles,
|
|
|
|
DWORD dwCreationFlags,
|
|
|
|
LPVOID lpEnvironment,
|
|
|
|
LPCSTR lpCurrentDirectory,
|
|
|
|
LPSTARTUPINFOA lpStartupInfo,
|
|
|
|
LPPROCESS_INFORMATION lpProcessInformation)
|
|
|
|
{
|
2005-01-21 20:31:24 +00:00
|
|
|
PROCESS_ACCESS_TOKEN AccessToken;
|
2004-01-20 23:16:42 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
/* Create the process with a suspended main thread */
|
|
|
|
if (!CreateProcessA (lpApplicationName,
|
|
|
|
lpCommandLine,
|
|
|
|
lpProcessAttributes,
|
|
|
|
lpThreadAttributes,
|
|
|
|
bInheritHandles,
|
|
|
|
dwCreationFlags | CREATE_SUSPENDED,
|
|
|
|
lpEnvironment,
|
|
|
|
lpCurrentDirectory,
|
|
|
|
lpStartupInfo,
|
|
|
|
lpProcessInformation))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-01-21 20:31:24 +00:00
|
|
|
AccessToken.Token = hToken;
|
|
|
|
AccessToken.Thread = NULL;
|
|
|
|
|
2004-01-20 23:16:42 +00:00
|
|
|
/* Set the new process token */
|
|
|
|
Status = NtSetInformationProcess (lpProcessInformation->hProcess,
|
|
|
|
ProcessAccessToken,
|
2005-01-21 20:31:24 +00:00
|
|
|
(PVOID)&AccessToken,
|
|
|
|
sizeof (AccessToken));
|
2004-01-20 23:16:42 +00:00
|
|
|
if (!NT_SUCCESS (Status))
|
|
|
|
{
|
|
|
|
SetLastError (RtlNtStatusToDosError (Status));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Resume the main thread */
|
|
|
|
if (!(dwCreationFlags & CREATE_SUSPENDED))
|
|
|
|
{
|
|
|
|
ResumeThread (lpProcessInformation->hThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2004-01-20 01:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-01-20 23:16:42 +00:00
|
|
|
* @implemented
|
2004-01-20 01:40:19 +00:00
|
|
|
*/
|
|
|
|
BOOL STDCALL
|
|
|
|
CreateProcessAsUserW (HANDLE hToken,
|
|
|
|
LPCWSTR lpApplicationName,
|
|
|
|
LPWSTR lpCommandLine,
|
|
|
|
LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
|
|
|
LPSECURITY_ATTRIBUTES lpThreadAttributes,
|
|
|
|
BOOL bInheritHandles,
|
|
|
|
DWORD dwCreationFlags,
|
|
|
|
LPVOID lpEnvironment,
|
|
|
|
LPCWSTR lpCurrentDirectory,
|
|
|
|
LPSTARTUPINFOW lpStartupInfo,
|
|
|
|
LPPROCESS_INFORMATION lpProcessInformation)
|
|
|
|
{
|
2005-01-21 20:31:24 +00:00
|
|
|
PROCESS_ACCESS_TOKEN AccessToken;
|
2004-01-20 23:16:42 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
/* Create the process with a suspended main thread */
|
|
|
|
if (!CreateProcessW (lpApplicationName,
|
|
|
|
lpCommandLine,
|
|
|
|
lpProcessAttributes,
|
|
|
|
lpThreadAttributes,
|
|
|
|
bInheritHandles,
|
|
|
|
dwCreationFlags | CREATE_SUSPENDED,
|
|
|
|
lpEnvironment,
|
|
|
|
lpCurrentDirectory,
|
|
|
|
lpStartupInfo,
|
|
|
|
lpProcessInformation))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-01-21 20:31:24 +00:00
|
|
|
AccessToken.Token = hToken;
|
|
|
|
AccessToken.Thread = NULL;
|
|
|
|
|
2004-01-20 23:16:42 +00:00
|
|
|
/* Set the new process token */
|
|
|
|
Status = NtSetInformationProcess (lpProcessInformation->hProcess,
|
|
|
|
ProcessAccessToken,
|
2005-01-21 20:31:24 +00:00
|
|
|
(PVOID)&AccessToken,
|
|
|
|
sizeof (AccessToken));
|
2004-01-20 23:16:42 +00:00
|
|
|
if (!NT_SUCCESS (Status))
|
|
|
|
{
|
|
|
|
SetLastError (RtlNtStatusToDosError (Status));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Resume the main thread */
|
|
|
|
if (!(dwCreationFlags & CREATE_SUSPENDED))
|
|
|
|
{
|
|
|
|
ResumeThread (lpProcessInformation->hThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2004-01-20 01:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-08-27 14:56:17 +00:00
|
|
|
* @implemented
|
2004-01-20 01:40:19 +00:00
|
|
|
*/
|
|
|
|
BOOL STDCALL
|
2005-08-27 14:56:17 +00:00
|
|
|
LogonUserA(LPSTR lpszUsername,
|
|
|
|
LPSTR lpszDomain,
|
|
|
|
LPSTR lpszPassword,
|
|
|
|
DWORD dwLogonType,
|
|
|
|
DWORD dwLogonProvider,
|
|
|
|
PHANDLE phToken)
|
2004-01-20 01:40:19 +00:00
|
|
|
{
|
2005-08-27 14:56:17 +00:00
|
|
|
UNICODE_STRING UserName;
|
|
|
|
UNICODE_STRING Domain;
|
|
|
|
UNICODE_STRING Password;
|
|
|
|
NTSTATUS Status;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
UserName.Buffer = NULL;
|
|
|
|
Domain.Buffer = NULL;
|
|
|
|
Password.Buffer = NULL;
|
|
|
|
|
|
|
|
Status = RtlCreateUnicodeStringFromAsciiz(&UserName,
|
|
|
|
lpszUsername);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
SetLastError(RtlNtStatusToDosError(Status));
|
|
|
|
goto UsernameDone;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = RtlCreateUnicodeStringFromAsciiz(&Domain,
|
|
|
|
lpszDomain);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
SetLastError(RtlNtStatusToDosError(Status));
|
|
|
|
goto DomainDone;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = RtlCreateUnicodeStringFromAsciiz(&Password,
|
|
|
|
lpszPassword);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
SetLastError(RtlNtStatusToDosError(Status));
|
|
|
|
goto PasswordDone;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = LogonUserW(UserName.Buffer,
|
|
|
|
Domain.Buffer,
|
|
|
|
Password.Buffer,
|
|
|
|
dwLogonType,
|
|
|
|
dwLogonProvider,
|
|
|
|
phToken);
|
|
|
|
|
|
|
|
if (Password.Buffer != NULL)
|
|
|
|
RtlFreeUnicodeString(&Password);
|
|
|
|
|
|
|
|
PasswordDone:
|
|
|
|
if (Domain.Buffer != NULL)
|
|
|
|
RtlFreeUnicodeString(&Domain);
|
|
|
|
|
|
|
|
DomainDone:
|
|
|
|
if (UserName.Buffer != NULL)
|
|
|
|
RtlFreeUnicodeString(&UserName);
|
|
|
|
|
|
|
|
UsernameDone:
|
|
|
|
return ret;
|
2004-01-20 01:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-23 10:35:52 +00:00
|
|
|
static BOOL STDCALL
|
|
|
|
SamGetUserSid (LPCWSTR UserName,
|
|
|
|
PSID *Sid)
|
|
|
|
{
|
|
|
|
PSID lpSid;
|
|
|
|
DWORD dwLength;
|
2004-03-09 15:04:28 +00:00
|
|
|
HKEY hUsersKey;
|
2004-01-23 10:35:52 +00:00
|
|
|
HKEY hUserKey;
|
|
|
|
|
|
|
|
if (Sid != NULL)
|
|
|
|
*Sid = NULL;
|
|
|
|
|
2004-03-09 15:04:28 +00:00
|
|
|
/* Open the Users key */
|
2004-01-23 10:35:52 +00:00
|
|
|
if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
|
2004-03-09 15:04:28 +00:00
|
|
|
L"SAM\\SAM\\Domains\\Account\\Users",
|
2004-01-23 10:35:52 +00:00
|
|
|
0,
|
|
|
|
KEY_READ,
|
2004-03-09 15:04:28 +00:00
|
|
|
&hUsersKey))
|
2004-01-23 10:35:52 +00:00
|
|
|
{
|
2004-03-09 15:04:28 +00:00
|
|
|
DPRINT1 ("Failed to open Users key! (Error %lu)\n", GetLastError());
|
2004-01-23 10:35:52 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the user key */
|
2004-03-09 15:04:28 +00:00
|
|
|
if (RegOpenKeyExW (hUsersKey,
|
2004-01-23 10:35:52 +00:00
|
|
|
UserName,
|
|
|
|
0,
|
|
|
|
KEY_READ,
|
|
|
|
&hUserKey))
|
|
|
|
{
|
|
|
|
if (GetLastError () == ERROR_FILE_NOT_FOUND)
|
|
|
|
{
|
2004-03-09 15:04:28 +00:00
|
|
|
DPRINT1 ("Invalid user name!\n");
|
2004-01-23 10:35:52 +00:00
|
|
|
SetLastError (ERROR_NO_SUCH_USER);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-03-09 15:04:28 +00:00
|
|
|
DPRINT1 ("Failed to open user key! (Error %lu)\n", GetLastError());
|
2004-01-23 10:35:52 +00:00
|
|
|
}
|
|
|
|
|
2004-03-09 15:04:28 +00:00
|
|
|
RegCloseKey (hUsersKey);
|
2004-01-23 10:35:52 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-03-09 15:04:28 +00:00
|
|
|
RegCloseKey (hUsersKey);
|
2004-01-23 10:35:52 +00:00
|
|
|
|
|
|
|
/* Get SID size */
|
|
|
|
dwLength = 0;
|
|
|
|
if (RegQueryValueExW (hUserKey,
|
|
|
|
L"Sid",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&dwLength))
|
|
|
|
{
|
2004-03-09 15:04:28 +00:00
|
|
|
DPRINT1 ("Failed to read the SID size! (Error %lu)\n", GetLastError());
|
2004-01-23 10:35:52 +00:00
|
|
|
RegCloseKey (hUserKey);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-03-09 15:04:28 +00:00
|
|
|
/* Allocate sid buffer */
|
|
|
|
DPRINT ("Required SID buffer size: %lu\n", dwLength);
|
2004-01-23 10:35:52 +00:00
|
|
|
lpSid = (PSID)RtlAllocateHeap (RtlGetProcessHeap (),
|
|
|
|
0,
|
|
|
|
dwLength);
|
|
|
|
if (lpSid == NULL)
|
|
|
|
{
|
2004-03-09 15:04:28 +00:00
|
|
|
DPRINT1 ("Failed to allocate SID buffer!\n");
|
2004-01-23 10:35:52 +00:00
|
|
|
RegCloseKey (hUserKey);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read sid */
|
|
|
|
if (RegQueryValueExW (hUserKey,
|
|
|
|
L"Sid",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(LPBYTE)lpSid,
|
|
|
|
&dwLength))
|
|
|
|
{
|
2004-03-09 15:04:28 +00:00
|
|
|
DPRINT1 ("Failed to read the SID! (Error %lu)\n", GetLastError());
|
2004-01-23 10:35:52 +00:00
|
|
|
RtlFreeHeap (RtlGetProcessHeap (),
|
|
|
|
0,
|
|
|
|
lpSid);
|
|
|
|
RegCloseKey (hUserKey);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey (hUserKey);
|
|
|
|
|
|
|
|
*Sid = lpSid;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-10 13:12:24 +00:00
|
|
|
static BOOL STDCALL
|
|
|
|
SamGetDomainSid(PSID *Sid)
|
|
|
|
{
|
|
|
|
PSID lpSid;
|
|
|
|
DWORD dwLength;
|
|
|
|
HKEY hDomainKey;
|
|
|
|
|
|
|
|
DPRINT("SamGetDomainSid() called\n");
|
|
|
|
|
|
|
|
if (Sid != NULL)
|
|
|
|
*Sid = NULL;
|
|
|
|
|
|
|
|
/* Open the account domain key */
|
|
|
|
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
|
|
|
|
L"SAM\\SAM\\Domains\\Account",
|
|
|
|
0,
|
|
|
|
KEY_READ,
|
|
|
|
&hDomainKey))
|
|
|
|
{
|
|
|
|
DPRINT1("Failed to open the account domain key! (Error %lu)\n", GetLastError());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get SID size */
|
|
|
|
dwLength = 0;
|
|
|
|
if (RegQueryValueExW(hDomainKey,
|
|
|
|
L"Sid",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&dwLength))
|
|
|
|
{
|
|
|
|
DPRINT1("Failed to read the SID size! (Error %lu)\n", GetLastError());
|
|
|
|
RegCloseKey(hDomainKey);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate sid buffer */
|
|
|
|
DPRINT("Required SID buffer size: %lu\n", dwLength);
|
|
|
|
lpSid = (PSID)RtlAllocateHeap(RtlGetProcessHeap(),
|
|
|
|
0,
|
|
|
|
dwLength);
|
|
|
|
if (lpSid == NULL)
|
|
|
|
{
|
|
|
|
DPRINT1("Failed to allocate SID buffer!\n");
|
|
|
|
RegCloseKey(hDomainKey);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read sid */
|
|
|
|
if (RegQueryValueExW(hDomainKey,
|
|
|
|
L"Sid",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(LPBYTE)lpSid,
|
|
|
|
&dwLength))
|
|
|
|
{
|
|
|
|
DPRINT1("Failed to read the SID! (Error %lu)\n", GetLastError());
|
|
|
|
RtlFreeHeap(RtlGetProcessHeap(),
|
|
|
|
0,
|
|
|
|
lpSid);
|
|
|
|
RegCloseKey(hDomainKey);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey(hDomainKey);
|
|
|
|
|
|
|
|
*Sid = lpSid;
|
|
|
|
|
|
|
|
DPRINT("SamGetDomainSid() done\n");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PSID
|
|
|
|
AppendRidToSid(PSID SrcSid,
|
|
|
|
ULONG Rid)
|
|
|
|
{
|
|
|
|
ULONG Rids[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
UCHAR RidCount;
|
|
|
|
PSID DstSid;
|
|
|
|
ULONG i;
|
|
|
|
|
|
|
|
RidCount = *RtlSubAuthorityCountSid(SrcSid);
|
|
|
|
if (RidCount >= 8)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < RidCount; i++)
|
|
|
|
Rids[i] = *RtlSubAuthoritySid(SrcSid, i);
|
|
|
|
|
|
|
|
Rids[RidCount] = Rid;
|
|
|
|
RidCount++;
|
|
|
|
|
|
|
|
RtlAllocateAndInitializeSid(RtlIdentifierAuthoritySid(SrcSid),
|
|
|
|
RidCount,
|
|
|
|
Rids[0],
|
|
|
|
Rids[1],
|
|
|
|
Rids[2],
|
|
|
|
Rids[3],
|
|
|
|
Rids[4],
|
|
|
|
Rids[5],
|
|
|
|
Rids[6],
|
|
|
|
Rids[7],
|
|
|
|
&DstSid);
|
|
|
|
|
|
|
|
return DstSid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PTOKEN_GROUPS
|
2007-10-31 09:41:56 +00:00
|
|
|
AllocateGroupSids(
|
|
|
|
OUT PSID *PrimaryGroupSid,
|
|
|
|
OUT PSID *OwnerSid)
|
2004-07-10 13:12:24 +00:00
|
|
|
{
|
2007-10-31 09:41:56 +00:00
|
|
|
SID_IDENTIFIER_AUTHORITY WorldAuthority = {SECURITY_WORLD_SID_AUTHORITY};
|
|
|
|
SID_IDENTIFIER_AUTHORITY LocalAuthority = {SECURITY_LOCAL_SID_AUTHORITY};
|
|
|
|
SID_IDENTIFIER_AUTHORITY SystemAuthority = {SECURITY_NT_AUTHORITY};
|
|
|
|
PTOKEN_GROUPS TokenGroups;
|
|
|
|
#define MAX_GROUPS 8
|
|
|
|
DWORD GroupCount = 0;
|
|
|
|
PSID DomainSid;
|
|
|
|
PSID Sid;
|
|
|
|
LUID Luid;
|
|
|
|
NTSTATUS Status;
|
2004-07-10 13:12:24 +00:00
|
|
|
|
2007-10-31 09:41:56 +00:00
|
|
|
Status = NtAllocateLocallyUniqueId(&Luid);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return NULL;
|
2004-07-10 13:12:24 +00:00
|
|
|
|
2007-10-31 09:41:56 +00:00
|
|
|
if (!SamGetDomainSid(&DomainSid))
|
|
|
|
return NULL;
|
2004-07-10 13:12:24 +00:00
|
|
|
|
2007-10-31 09:41:56 +00:00
|
|
|
TokenGroups = RtlAllocateHeap(
|
|
|
|
GetProcessHeap(), 0,
|
|
|
|
sizeof(TOKEN_GROUPS) +
|
|
|
|
MAX_GROUPS * sizeof(SID_AND_ATTRIBUTES));
|
|
|
|
if (TokenGroups == NULL)
|
2004-07-10 13:12:24 +00:00
|
|
|
{
|
2007-10-31 09:41:56 +00:00
|
|
|
RtlFreeHeap(RtlGetProcessHeap(), 0, DomainSid);
|
|
|
|
return NULL;
|
2004-07-10 13:12:24 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 09:41:56 +00:00
|
|
|
Sid = AppendRidToSid(DomainSid, DOMAIN_GROUP_RID_USERS);
|
|
|
|
RtlFreeHeap(RtlGetProcessHeap(), 0, DomainSid);
|
|
|
|
|
|
|
|
/* Member of the domain */
|
|
|
|
TokenGroups->Groups[GroupCount].Sid = Sid;
|
|
|
|
TokenGroups->Groups[GroupCount].Attributes = SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY;
|
|
|
|
*PrimaryGroupSid = Sid;
|
|
|
|
GroupCount++;
|
|
|
|
|
|
|
|
/* Member of 'Everyone' */
|
|
|
|
RtlAllocateAndInitializeSid(
|
|
|
|
&WorldAuthority,
|
|
|
|
1,
|
|
|
|
SECURITY_WORLD_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
&Sid);
|
|
|
|
TokenGroups->Groups[GroupCount].Sid = Sid;
|
|
|
|
TokenGroups->Groups[GroupCount].Attributes = SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY;
|
|
|
|
GroupCount++;
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
/* Member of 'Administrators' */
|
|
|
|
RtlAllocateAndInitializeSid(
|
|
|
|
&SystemAuthority,
|
|
|
|
2,
|
|
|
|
SECURITY_BUILTIN_DOMAIN_RID,
|
|
|
|
DOMAIN_ALIAS_RID_ADMINS,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
&Sid);
|
|
|
|
TokenGroups->Groups[GroupCount].Sid = Sid;
|
|
|
|
TokenGroups->Groups[GroupCount].Attributes = SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY;
|
|
|
|
GroupCount++;
|
|
|
|
#else
|
|
|
|
DPRINT1("Not adding user to Administrators group\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Member of 'Users' */
|
|
|
|
RtlAllocateAndInitializeSid(
|
|
|
|
&SystemAuthority,
|
|
|
|
2,
|
|
|
|
SECURITY_BUILTIN_DOMAIN_RID,
|
|
|
|
DOMAIN_ALIAS_RID_USERS,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
&Sid);
|
|
|
|
TokenGroups->Groups[GroupCount].Sid = Sid;
|
|
|
|
TokenGroups->Groups[GroupCount].Attributes = SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY;
|
|
|
|
GroupCount++;
|
|
|
|
|
|
|
|
/* Logon SID */
|
|
|
|
RtlAllocateAndInitializeSid(
|
|
|
|
&SystemAuthority,
|
|
|
|
SECURITY_LOGON_IDS_RID_COUNT,
|
|
|
|
SECURITY_LOGON_IDS_RID,
|
|
|
|
Luid.HighPart,
|
|
|
|
Luid.LowPart,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
&Sid);
|
|
|
|
TokenGroups->Groups[GroupCount].Sid = Sid;
|
|
|
|
TokenGroups->Groups[GroupCount].Attributes = SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY | SE_GROUP_LOGON_ID;
|
|
|
|
GroupCount++;
|
|
|
|
*OwnerSid = Sid;
|
|
|
|
|
|
|
|
/* Member of 'Local users */
|
|
|
|
RtlAllocateAndInitializeSid(
|
|
|
|
&LocalAuthority,
|
|
|
|
1,
|
|
|
|
SECURITY_LOCAL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
&Sid);
|
|
|
|
TokenGroups->Groups[GroupCount].Sid = Sid;
|
|
|
|
TokenGroups->Groups[GroupCount].Attributes = SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY;
|
|
|
|
GroupCount++;
|
|
|
|
|
|
|
|
/* Member of 'Interactive users' */
|
|
|
|
RtlAllocateAndInitializeSid(
|
|
|
|
&SystemAuthority,
|
|
|
|
1,
|
|
|
|
SECURITY_INTERACTIVE_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
&Sid);
|
|
|
|
TokenGroups->Groups[GroupCount].Sid = Sid;
|
|
|
|
TokenGroups->Groups[GroupCount].Attributes = SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY;
|
|
|
|
GroupCount++;
|
|
|
|
|
|
|
|
/* Member of 'Authenticated users' */
|
|
|
|
RtlAllocateAndInitializeSid(
|
|
|
|
&SystemAuthority,
|
|
|
|
1,
|
|
|
|
SECURITY_AUTHENTICATED_USER_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
&Sid);
|
|
|
|
TokenGroups->Groups[GroupCount].Sid = Sid;
|
|
|
|
TokenGroups->Groups[GroupCount].Attributes = SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY;
|
|
|
|
GroupCount++;
|
|
|
|
|
|
|
|
TokenGroups->GroupCount = GroupCount;
|
|
|
|
ASSERT(TokenGroups->GroupCount <= MAX_GROUPS);
|
|
|
|
|
|
|
|
return TokenGroups;
|
2004-07-10 13:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static VOID
|
|
|
|
FreeGroupSids(PTOKEN_GROUPS TokenGroups)
|
|
|
|
{
|
|
|
|
ULONG i;
|
|
|
|
|
|
|
|
for (i = 0; i < TokenGroups->GroupCount; i++)
|
|
|
|
{
|
|
|
|
if (TokenGroups->Groups[i].Sid != NULL)
|
|
|
|
RtlFreeHeap(GetProcessHeap(), 0, TokenGroups->Groups[i].Sid);
|
|
|
|
}
|
|
|
|
|
|
|
|
RtlFreeHeap(GetProcessHeap(), 0, TokenGroups);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-20 01:40:19 +00:00
|
|
|
/*
|
|
|
|
* @unimplemented
|
|
|
|
*/
|
|
|
|
BOOL STDCALL
|
2004-06-17 09:07:12 +00:00
|
|
|
LogonUserW (LPWSTR lpszUsername,
|
|
|
|
LPWSTR lpszDomain,
|
|
|
|
LPWSTR lpszPassword,
|
2004-01-20 01:40:19 +00:00
|
|
|
DWORD dwLogonType,
|
|
|
|
DWORD dwLogonProvider,
|
|
|
|
PHANDLE phToken)
|
|
|
|
{
|
2004-07-07 08:41:47 +00:00
|
|
|
/* FIXME shouldn't use hard-coded list of privileges */
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
LPCWSTR PrivName;
|
|
|
|
DWORD Attributes;
|
|
|
|
}
|
|
|
|
DefaultPrivs[] =
|
|
|
|
{
|
|
|
|
{ L"SeUnsolicitedInputPrivilege", 0 },
|
|
|
|
{ L"SeMachineAccountPrivilege", 0 },
|
|
|
|
{ L"SeSecurityPrivilege", 0 },
|
|
|
|
{ L"SeTakeOwnershipPrivilege", 0 },
|
|
|
|
{ L"SeLoadDriverPrivilege", 0 },
|
|
|
|
{ L"SeSystemProfilePrivilege", 0 },
|
|
|
|
{ L"SeSystemtimePrivilege", 0 },
|
|
|
|
{ L"SeProfileSingleProcessPrivilege", 0 },
|
|
|
|
{ L"SeIncreaseBasePriorityPrivilege", 0 },
|
|
|
|
{ L"SeCreatePagefilePrivilege", 0 },
|
|
|
|
{ L"SeBackupPrivilege", 0 },
|
|
|
|
{ L"SeRestorePrivilege", 0 },
|
|
|
|
{ L"SeShutdownPrivilege", 0 },
|
|
|
|
{ L"SeDebugPrivilege", 0 },
|
|
|
|
{ L"SeSystemEnvironmentPrivilege", 0 },
|
|
|
|
{ L"SeChangeNotifyPrivilege", SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ L"SeRemoteShutdownPrivilege", 0 },
|
|
|
|
{ L"SeUndockPrivilege", 0 },
|
|
|
|
{ L"SeEnableDelegationPrivilege", 0 },
|
|
|
|
{ L"SeImpersonatePrivilege", SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ L"SeCreateGlobalPrivilege", SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT }
|
|
|
|
};
|
2004-01-20 23:16:42 +00:00
|
|
|
OBJECT_ATTRIBUTES ObjectAttributes;
|
|
|
|
SECURITY_QUALITY_OF_SERVICE Qos;
|
|
|
|
TOKEN_USER TokenUser;
|
|
|
|
TOKEN_OWNER TokenOwner;
|
|
|
|
TOKEN_PRIMARY_GROUP TokenPrimaryGroup;
|
2004-07-10 13:12:24 +00:00
|
|
|
PTOKEN_GROUPS TokenGroups;
|
2004-07-07 08:41:47 +00:00
|
|
|
PTOKEN_PRIVILEGES TokenPrivileges;
|
2004-01-20 23:16:42 +00:00
|
|
|
TOKEN_DEFAULT_DACL TokenDefaultDacl;
|
|
|
|
LARGE_INTEGER ExpirationTime;
|
|
|
|
LUID AuthenticationId;
|
|
|
|
TOKEN_SOURCE TokenSource;
|
2004-07-10 13:12:24 +00:00
|
|
|
PSID UserSid = NULL;
|
|
|
|
PSID PrimaryGroupSid = NULL;
|
|
|
|
PSID OwnerSid = NULL;
|
2004-07-10 21:15:26 +00:00
|
|
|
PSID LocalSystemSid;
|
|
|
|
PACL Dacl;
|
2004-01-20 23:16:42 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
SID_IDENTIFIER_AUTHORITY SystemAuthority = {SECURITY_NT_AUTHORITY};
|
2004-07-07 08:41:47 +00:00
|
|
|
unsigned i;
|
2004-01-20 23:16:42 +00:00
|
|
|
|
|
|
|
Qos.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
|
|
|
|
Qos.ImpersonationLevel = SecurityAnonymous;
|
|
|
|
Qos.ContextTrackingMode = SECURITY_STATIC_TRACKING;
|
|
|
|
Qos.EffectiveOnly = FALSE;
|
|
|
|
|
|
|
|
ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
|
|
|
|
ObjectAttributes.RootDirectory = NULL;
|
|
|
|
ObjectAttributes.ObjectName = NULL;
|
|
|
|
ObjectAttributes.Attributes = 0;
|
|
|
|
ObjectAttributes.SecurityDescriptor = NULL;
|
|
|
|
ObjectAttributes.SecurityQualityOfService = &Qos;
|
|
|
|
|
2004-08-10 21:18:16 +00:00
|
|
|
Status = NtAllocateLocallyUniqueId(&AuthenticationId);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-01-20 23:16:42 +00:00
|
|
|
ExpirationTime.QuadPart = -1;
|
|
|
|
|
2004-01-23 10:35:52 +00:00
|
|
|
/* Get the user SID from the registry */
|
|
|
|
if (!SamGetUserSid (lpszUsername, &UserSid))
|
|
|
|
{
|
2007-10-31 09:41:56 +00:00
|
|
|
DPRINT1 ("SamGetUserSid() failed\n");
|
|
|
|
return FALSE;
|
2004-01-23 10:35:52 +00:00
|
|
|
}
|
2004-01-20 23:16:42 +00:00
|
|
|
|
|
|
|
TokenUser.User.Sid = UserSid;
|
|
|
|
TokenUser.User.Attributes = 0;
|
|
|
|
|
2004-07-10 13:12:24 +00:00
|
|
|
/* Allocate and initialize token groups */
|
|
|
|
TokenGroups = AllocateGroupSids(&PrimaryGroupSid,
|
|
|
|
&OwnerSid);
|
|
|
|
if (NULL == TokenGroups)
|
|
|
|
{
|
2004-07-10 21:15:26 +00:00
|
|
|
RtlFreeSid(UserSid);
|
2004-07-10 13:12:24 +00:00
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-01-20 23:16:42 +00:00
|
|
|
|
2004-07-10 13:12:24 +00:00
|
|
|
/* Allocate and initialize token privileges */
|
2004-07-07 08:41:47 +00:00
|
|
|
TokenPrivileges = RtlAllocateHeap(GetProcessHeap(), 0,
|
|
|
|
sizeof(TOKEN_PRIVILEGES)
|
|
|
|
+ sizeof(DefaultPrivs) / sizeof(DefaultPrivs[0])
|
|
|
|
* sizeof(LUID_AND_ATTRIBUTES));
|
|
|
|
if (NULL == TokenPrivileges)
|
|
|
|
{
|
2004-07-10 13:12:24 +00:00
|
|
|
FreeGroupSids(TokenGroups);
|
2004-07-10 21:15:26 +00:00
|
|
|
RtlFreeSid(UserSid);
|
2004-07-07 08:41:47 +00:00
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-07-10 13:12:24 +00:00
|
|
|
|
2004-07-07 08:41:47 +00:00
|
|
|
TokenPrivileges->PrivilegeCount = 0;
|
|
|
|
for (i = 0; i < sizeof(DefaultPrivs) / sizeof(DefaultPrivs[0]); i++)
|
|
|
|
{
|
|
|
|
if (! LookupPrivilegeValueW(NULL, DefaultPrivs[i].PrivName,
|
|
|
|
&TokenPrivileges->Privileges[TokenPrivileges->PrivilegeCount].Luid))
|
|
|
|
{
|
|
|
|
DPRINT1("Can't set privilege %S\n", DefaultPrivs[i].PrivName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TokenPrivileges->Privileges[TokenPrivileges->PrivilegeCount].Attributes = DefaultPrivs[i].Attributes;
|
|
|
|
TokenPrivileges->PrivilegeCount++;
|
|
|
|
}
|
|
|
|
}
|
2004-01-20 23:16:42 +00:00
|
|
|
|
2004-07-10 13:12:24 +00:00
|
|
|
TokenOwner.Owner = OwnerSid;
|
|
|
|
TokenPrimaryGroup.PrimaryGroup = PrimaryGroupSid;
|
2004-01-20 23:16:42 +00:00
|
|
|
|
2004-07-10 21:15:26 +00:00
|
|
|
|
|
|
|
Dacl = RtlAllocateHeap(GetProcessHeap(), 0, 1024);
|
|
|
|
if (Dacl == NULL)
|
|
|
|
{
|
|
|
|
FreeGroupSids(TokenGroups);
|
|
|
|
RtlFreeSid(UserSid);
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = RtlCreateAcl(Dacl, 1024, ACL_REVISION);
|
2004-01-20 23:16:42 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2004-07-10 21:15:26 +00:00
|
|
|
RtlFreeHeap(GetProcessHeap(), 0, Dacl);
|
2004-07-10 13:12:24 +00:00
|
|
|
FreeGroupSids(TokenGroups);
|
2004-07-07 08:41:47 +00:00
|
|
|
RtlFreeHeap(GetProcessHeap(), 0, TokenPrivileges);
|
2004-07-10 21:15:26 +00:00
|
|
|
RtlFreeSid(UserSid);
|
2004-01-20 23:16:42 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-07-10 21:15:26 +00:00
|
|
|
RtlAddAccessAllowedAce(Dacl,
|
|
|
|
ACL_REVISION,
|
|
|
|
GENERIC_ALL,
|
|
|
|
OwnerSid);
|
2004-01-20 23:16:42 +00:00
|
|
|
|
2004-07-10 21:15:26 +00:00
|
|
|
RtlAllocateAndInitializeSid(&SystemAuthority,
|
|
|
|
1,
|
|
|
|
SECURITY_LOCAL_SYSTEM_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
SECURITY_NULL_RID,
|
|
|
|
&LocalSystemSid);
|
|
|
|
|
|
|
|
/* SID: S-1-5-18 */
|
|
|
|
RtlAddAccessAllowedAce(Dacl,
|
|
|
|
ACL_REVISION,
|
|
|
|
GENERIC_ALL,
|
|
|
|
LocalSystemSid);
|
|
|
|
|
|
|
|
RtlFreeSid(LocalSystemSid);
|
|
|
|
|
|
|
|
TokenDefaultDacl.DefaultDacl = Dacl;
|
|
|
|
|
|
|
|
memcpy(TokenSource.SourceName,
|
|
|
|
"User32 ",
|
|
|
|
8);
|
|
|
|
Status = NtAllocateLocallyUniqueId(&TokenSource.SourceIdentifier);
|
2004-01-20 23:16:42 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2004-07-10 21:15:26 +00:00
|
|
|
RtlFreeHeap(GetProcessHeap(), 0, Dacl);
|
2004-07-10 13:12:24 +00:00
|
|
|
FreeGroupSids(TokenGroups);
|
|
|
|
RtlFreeHeap(GetProcessHeap(), 0, TokenPrivileges);
|
2004-07-10 21:15:26 +00:00
|
|
|
RtlFreeSid(UserSid);
|
2004-01-20 23:16:42 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-07-10 21:15:26 +00:00
|
|
|
Status = NtCreateToken(phToken,
|
|
|
|
TOKEN_ALL_ACCESS,
|
|
|
|
&ObjectAttributes,
|
|
|
|
TokenPrimary,
|
|
|
|
&AuthenticationId,
|
|
|
|
&ExpirationTime,
|
|
|
|
&TokenUser,
|
|
|
|
TokenGroups,
|
|
|
|
TokenPrivileges,
|
|
|
|
&TokenOwner,
|
|
|
|
&TokenPrimaryGroup,
|
|
|
|
&TokenDefaultDacl,
|
|
|
|
&TokenSource);
|
|
|
|
|
|
|
|
RtlFreeHeap(GetProcessHeap(), 0, Dacl);
|
2004-07-10 13:12:24 +00:00
|
|
|
FreeGroupSids(TokenGroups);
|
2004-07-07 08:41:47 +00:00
|
|
|
RtlFreeHeap(GetProcessHeap(), 0, TokenPrivileges);
|
2004-07-10 21:15:26 +00:00
|
|
|
RtlFreeSid(UserSid);
|
2004-01-20 23:16:42 +00:00
|
|
|
|
2004-07-07 08:41:47 +00:00
|
|
|
return NT_SUCCESS(Status);
|
2004-01-20 01:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|