📄 logon.c
字号:
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
* FILE: lib/advapi32/misc/logon.c
* PURPOSE: Logon functions
* PROGRAMMER: Eric Kohl
*/
#include <advapi32.h>
#define NDEBUG
#include <debug.h>
/* FUNCTIONS ***************************************************************/
/*
* @implemented
*/
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)
{
PROCESS_ACCESS_TOKEN AccessToken;
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;
}
AccessToken.Token = hToken;
AccessToken.Thread = NULL;
/* Set the new process token */
Status = NtSetInformationProcess (lpProcessInformation->hProcess,
ProcessAccessToken,
(PVOID)&AccessToken,
sizeof (AccessToken));
if (!NT_SUCCESS (Status))
{
SetLastError (RtlNtStatusToDosError (Status));
return FALSE;
}
/* Resume the main thread */
if (!(dwCreationFlags & CREATE_SUSPENDED))
{
ResumeThread (lpProcessInformation->hThread);
}
return TRUE;
}
/*
* @implemented
*/
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)
{
PROCESS_ACCESS_TOKEN AccessToken;
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;
}
AccessToken.Token = hToken;
AccessToken.Thread = NULL;
/* Set the new process token */
Status = NtSetInformationProcess (lpProcessInformation->hProcess,
ProcessAccessToken,
(PVOID)&AccessToken,
sizeof (AccessToken));
if (!NT_SUCCESS (Status))
{
SetLastError (RtlNtStatusToDosError (Status));
return FALSE;
}
/* Resume the main thread */
if (!(dwCreationFlags & CREATE_SUSPENDED))
{
ResumeThread (lpProcessInformation->hThread);
}
return TRUE;
}
/*
* @implemented
*/
BOOL STDCALL
LogonUserA(LPSTR lpszUsername,
LPSTR lpszDomain,
LPSTR lpszPassword,
DWORD dwLogonType,
DWORD dwLogonProvider,
PHANDLE phToken)
{
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;
}
static BOOL STDCALL
SamGetUserSid (LPCWSTR UserName,
PSID *Sid)
{
PSID lpSid;
DWORD dwLength;
HKEY hUsersKey;
HKEY hUserKey;
if (Sid != NULL)
*Sid = NULL;
/* Open the Users key */
if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
L"SAM\\SAM\\Domains\\Account\\Users",
0,
KEY_READ,
&hUsersKey))
{
DPRINT1 ("Failed to open Users key! (Error %lu)\n", GetLastError());
return FALSE;
}
/* Open the user key */
if (RegOpenKeyExW (hUsersKey,
UserName,
0,
KEY_READ,
&hUserKey))
{
if (GetLastError () == ERROR_FILE_NOT_FOUND)
{
DPRINT1 ("Invalid user name!\n");
SetLastError (ERROR_NO_SUCH_USER);
}
else
{
DPRINT1 ("Failed to open user key! (Error %lu)\n", GetLastError());
}
RegCloseKey (hUsersKey);
return FALSE;
}
RegCloseKey (hUsersKey);
/* Get SID size */
dwLength = 0;
if (RegQueryValueExW (hUserKey,
L"Sid",
NULL,
NULL,
NULL,
&dwLength))
{
DPRINT1 ("Failed to read the SID size! (Error %lu)\n", GetLastError());
RegCloseKey (hUserKey);
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 (hUserKey);
return FALSE;
}
/* Read sid */
if (RegQueryValueExW (hUserKey,
L"Sid",
NULL,
NULL,
(LPBYTE)lpSid,
&dwLength))
{
DPRINT1 ("Failed to read the SID! (Error %lu)\n", GetLastError());
RtlFreeHeap (RtlGetProcessHeap (),
0,
lpSid);
RegCloseKey (hUserKey);
return FALSE;
}
RegCloseKey (hUserKey);
*Sid = lpSid;
return TRUE;
}
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -