2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/advapi32/misc/logon.c
5 * PURPOSE: Logon functions
6 * PROGRAMMER: Eric Kohl
11 #include <wine/debug.h>
12 WINE_DEFAULT_DEBUG_CHANNEL(advapi
);
15 /* FUNCTIONS ***************************************************************/
21 CreateProcessAsUserA (HANDLE hToken
,
22 LPCSTR lpApplicationName
,
24 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
25 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
27 DWORD dwCreationFlags
,
29 LPCSTR lpCurrentDirectory
,
30 LPSTARTUPINFOA lpStartupInfo
,
31 LPPROCESS_INFORMATION lpProcessInformation
)
33 PROCESS_ACCESS_TOKEN AccessToken
;
36 /* Create the process with a suspended main thread */
37 if (!CreateProcessA (lpApplicationName
,
42 dwCreationFlags
| CREATE_SUSPENDED
,
46 lpProcessInformation
))
51 AccessToken
.Token
= hToken
;
52 AccessToken
.Thread
= NULL
;
54 /* Set the new process token */
55 Status
= NtSetInformationProcess (lpProcessInformation
->hProcess
,
58 sizeof (AccessToken
));
59 if (!NT_SUCCESS (Status
))
61 SetLastError (RtlNtStatusToDosError (Status
));
65 /* Resume the main thread */
66 if (!(dwCreationFlags
& CREATE_SUSPENDED
))
68 ResumeThread (lpProcessInformation
->hThread
);
79 CreateProcessAsUserW (HANDLE hToken
,
80 LPCWSTR lpApplicationName
,
82 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
83 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
85 DWORD dwCreationFlags
,
87 LPCWSTR lpCurrentDirectory
,
88 LPSTARTUPINFOW lpStartupInfo
,
89 LPPROCESS_INFORMATION lpProcessInformation
)
91 PROCESS_ACCESS_TOKEN AccessToken
;
94 /* Create the process with a suspended main thread */
95 if (!CreateProcessW (lpApplicationName
,
100 dwCreationFlags
| CREATE_SUSPENDED
,
104 lpProcessInformation
))
109 AccessToken
.Token
= hToken
;
110 AccessToken
.Thread
= NULL
;
112 /* Set the new process token */
113 Status
= NtSetInformationProcess (lpProcessInformation
->hProcess
,
116 sizeof (AccessToken
));
117 if (!NT_SUCCESS (Status
))
119 SetLastError (RtlNtStatusToDosError (Status
));
123 /* Resume the main thread */
124 if (!(dwCreationFlags
& CREATE_SUSPENDED
))
126 ResumeThread (lpProcessInformation
->hThread
);
137 LogonUserA(LPSTR lpszUsername
,
141 DWORD dwLogonProvider
,
144 UNICODE_STRING UserName
;
145 UNICODE_STRING Domain
;
146 UNICODE_STRING Password
;
150 UserName
.Buffer
= NULL
;
151 Domain
.Buffer
= NULL
;
152 Password
.Buffer
= NULL
;
154 Status
= RtlCreateUnicodeStringFromAsciiz(&UserName
,
156 if (!NT_SUCCESS(Status
))
158 SetLastError(RtlNtStatusToDosError(Status
));
162 Status
= RtlCreateUnicodeStringFromAsciiz(&Domain
,
164 if (!NT_SUCCESS(Status
))
166 SetLastError(RtlNtStatusToDosError(Status
));
170 Status
= RtlCreateUnicodeStringFromAsciiz(&Password
,
172 if (!NT_SUCCESS(Status
))
174 SetLastError(RtlNtStatusToDosError(Status
));
178 ret
= LogonUserW(UserName
.Buffer
,
185 if (Password
.Buffer
!= NULL
)
186 RtlFreeUnicodeString(&Password
);
189 if (Domain
.Buffer
!= NULL
)
190 RtlFreeUnicodeString(&Domain
);
193 if (UserName
.Buffer
!= NULL
)
194 RtlFreeUnicodeString(&UserName
);
202 SamGetUserSid (LPCWSTR UserName
,
213 /* Open the Users key */
214 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
215 L
"SAM\\SAM\\Domains\\Account\\Users",
220 ERR("Failed to open Users key! (Error %lu)\n", GetLastError());
224 /* Open the user key */
225 if (RegOpenKeyExW (hUsersKey
,
231 if (GetLastError () == ERROR_FILE_NOT_FOUND
)
233 ERR("Invalid user name!\n");
234 SetLastError (ERROR_NO_SUCH_USER
);
238 ERR("Failed to open user key! (Error %lu)\n", GetLastError());
241 RegCloseKey (hUsersKey
);
245 RegCloseKey (hUsersKey
);
249 if (RegQueryValueExW (hUserKey
,
256 ERR("Failed to read the SID size! (Error %lu)\n", GetLastError());
257 RegCloseKey (hUserKey
);
261 /* Allocate sid buffer */
262 TRACE("Required SID buffer size: %lu\n", dwLength
);
263 lpSid
= (PSID
)RtlAllocateHeap (RtlGetProcessHeap (),
268 ERR("Failed to allocate SID buffer!\n");
269 RegCloseKey (hUserKey
);
274 if (RegQueryValueExW (hUserKey
,
281 ERR("Failed to read the SID! (Error %lu)\n", GetLastError());
282 RtlFreeHeap (RtlGetProcessHeap (),
285 RegCloseKey (hUserKey
);
289 RegCloseKey (hUserKey
);
298 SamGetDomainSid(PSID
*Sid
)
304 TRACE("SamGetDomainSid() called\n");
309 /* Open the account domain key */
310 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
311 L
"SAM\\SAM\\Domains\\Account",
316 ERR("Failed to open the account domain key! (Error %lu)\n", GetLastError());
322 if (RegQueryValueExW(hDomainKey
,
329 ERR("Failed to read the SID size! (Error %lu)\n", GetLastError());
330 RegCloseKey(hDomainKey
);
334 /* Allocate sid buffer */
335 TRACE("Required SID buffer size: %lu\n", dwLength
);
336 lpSid
= (PSID
)RtlAllocateHeap(RtlGetProcessHeap(),
341 ERR("Failed to allocate SID buffer!\n");
342 RegCloseKey(hDomainKey
);
347 if (RegQueryValueExW(hDomainKey
,
354 ERR("Failed to read the SID! (Error %lu)\n", GetLastError());
355 RtlFreeHeap(RtlGetProcessHeap(),
358 RegCloseKey(hDomainKey
);
362 RegCloseKey(hDomainKey
);
366 TRACE("SamGetDomainSid() done\n");
373 AppendRidToSid(PSID SrcSid
,
376 ULONG Rids
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
381 RidCount
= *RtlSubAuthorityCountSid(SrcSid
);
385 for (i
= 0; i
< RidCount
; i
++)
386 Rids
[i
] = *RtlSubAuthoritySid(SrcSid
, i
);
388 Rids
[RidCount
] = Rid
;
391 RtlAllocateAndInitializeSid(RtlIdentifierAuthoritySid(SrcSid
),
409 OUT PSID
*PrimaryGroupSid
,
412 SID_IDENTIFIER_AUTHORITY WorldAuthority
= {SECURITY_WORLD_SID_AUTHORITY
};
413 SID_IDENTIFIER_AUTHORITY LocalAuthority
= {SECURITY_LOCAL_SID_AUTHORITY
};
414 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
415 PTOKEN_GROUPS TokenGroups
;
417 DWORD GroupCount
= 0;
423 Status
= NtAllocateLocallyUniqueId(&Luid
);
424 if (!NT_SUCCESS(Status
))
427 if (!SamGetDomainSid(&DomainSid
))
430 TokenGroups
= RtlAllocateHeap(
432 sizeof(TOKEN_GROUPS
) +
433 MAX_GROUPS
* sizeof(SID_AND_ATTRIBUTES
));
434 if (TokenGroups
== NULL
)
436 RtlFreeHeap(RtlGetProcessHeap(), 0, DomainSid
);
440 Sid
= AppendRidToSid(DomainSid
, DOMAIN_GROUP_RID_USERS
);
441 RtlFreeHeap(RtlGetProcessHeap(), 0, DomainSid
);
443 /* Member of the domain */
444 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
445 TokenGroups
->Groups
[GroupCount
].Attributes
= SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
446 *PrimaryGroupSid
= Sid
;
449 /* Member of 'Everyone' */
450 RtlAllocateAndInitializeSid(
462 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
463 TokenGroups
->Groups
[GroupCount
].Attributes
= SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
467 /* Member of 'Administrators' */
468 RtlAllocateAndInitializeSid(
471 SECURITY_BUILTIN_DOMAIN_RID
,
472 DOMAIN_ALIAS_RID_ADMINS
,
480 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
481 TokenGroups
->Groups
[GroupCount
].Attributes
= SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
484 TRACE("Not adding user to Administrators group\n");
487 /* Member of 'Users' */
488 RtlAllocateAndInitializeSid(
491 SECURITY_BUILTIN_DOMAIN_RID
,
492 DOMAIN_ALIAS_RID_USERS
,
500 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
501 TokenGroups
->Groups
[GroupCount
].Attributes
= SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
505 RtlAllocateAndInitializeSid(
507 SECURITY_LOGON_IDS_RID_COUNT
,
508 SECURITY_LOGON_IDS_RID
,
517 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
518 TokenGroups
->Groups
[GroupCount
].Attributes
= SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
| SE_GROUP_LOGON_ID
;
522 /* Member of 'Local users */
523 RtlAllocateAndInitializeSid(
535 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
536 TokenGroups
->Groups
[GroupCount
].Attributes
= SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
539 /* Member of 'Interactive users' */
540 RtlAllocateAndInitializeSid(
543 SECURITY_INTERACTIVE_RID
,
552 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
553 TokenGroups
->Groups
[GroupCount
].Attributes
= SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
556 /* Member of 'Authenticated users' */
557 RtlAllocateAndInitializeSid(
560 SECURITY_AUTHENTICATED_USER_RID
,
569 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
570 TokenGroups
->Groups
[GroupCount
].Attributes
= SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
573 TokenGroups
->GroupCount
= GroupCount
;
574 ASSERT(TokenGroups
->GroupCount
<= MAX_GROUPS
);
581 FreeGroupSids(PTOKEN_GROUPS TokenGroups
)
585 for (i
= 0; i
< TokenGroups
->GroupCount
; i
++)
587 if (TokenGroups
->Groups
[i
].Sid
!= NULL
)
588 RtlFreeHeap(GetProcessHeap(), 0, TokenGroups
->Groups
[i
].Sid
);
591 RtlFreeHeap(GetProcessHeap(), 0, TokenGroups
);
599 LogonUserW (LPWSTR lpszUsername
,
603 DWORD dwLogonProvider
,
606 /* FIXME shouldn't use hard-coded list of privileges */
614 { L
"SeUnsolicitedInputPrivilege", 0 },
615 { L
"SeMachineAccountPrivilege", 0 },
616 { L
"SeSecurityPrivilege", 0 },
617 { L
"SeTakeOwnershipPrivilege", 0 },
618 { L
"SeLoadDriverPrivilege", 0 },
619 { L
"SeSystemProfilePrivilege", 0 },
620 { L
"SeSystemtimePrivilege", 0 },
621 { L
"SeProfileSingleProcessPrivilege", 0 },
622 { L
"SeIncreaseBasePriorityPrivilege", 0 },
623 { L
"SeCreatePagefilePrivilege", 0 },
624 { L
"SeBackupPrivilege", 0 },
625 { L
"SeRestorePrivilege", 0 },
626 { L
"SeShutdownPrivilege", 0 },
627 { L
"SeDebugPrivilege", 0 },
628 { L
"SeSystemEnvironmentPrivilege", 0 },
629 { L
"SeChangeNotifyPrivilege", SE_PRIVILEGE_ENABLED
| SE_PRIVILEGE_ENABLED_BY_DEFAULT
},
630 { L
"SeRemoteShutdownPrivilege", 0 },
631 { L
"SeUndockPrivilege", 0 },
632 { L
"SeEnableDelegationPrivilege", 0 },
633 { L
"SeImpersonatePrivilege", SE_PRIVILEGE_ENABLED
| SE_PRIVILEGE_ENABLED_BY_DEFAULT
},
634 { L
"SeCreateGlobalPrivilege", SE_PRIVILEGE_ENABLED
| SE_PRIVILEGE_ENABLED_BY_DEFAULT
}
636 OBJECT_ATTRIBUTES ObjectAttributes
;
637 SECURITY_QUALITY_OF_SERVICE Qos
;
638 TOKEN_USER TokenUser
;
639 TOKEN_OWNER TokenOwner
;
640 TOKEN_PRIMARY_GROUP TokenPrimaryGroup
;
641 PTOKEN_GROUPS TokenGroups
;
642 PTOKEN_PRIVILEGES TokenPrivileges
;
643 TOKEN_DEFAULT_DACL TokenDefaultDacl
;
644 LARGE_INTEGER ExpirationTime
;
645 LUID AuthenticationId
;
646 TOKEN_SOURCE TokenSource
;
648 PSID PrimaryGroupSid
= NULL
;
649 PSID OwnerSid
= NULL
;
653 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
656 Qos
.Length
= sizeof(SECURITY_QUALITY_OF_SERVICE
);
657 Qos
.ImpersonationLevel
= SecurityAnonymous
;
658 Qos
.ContextTrackingMode
= SECURITY_STATIC_TRACKING
;
659 Qos
.EffectiveOnly
= FALSE
;
661 ObjectAttributes
.Length
= sizeof(OBJECT_ATTRIBUTES
);
662 ObjectAttributes
.RootDirectory
= NULL
;
663 ObjectAttributes
.ObjectName
= NULL
;
664 ObjectAttributes
.Attributes
= 0;
665 ObjectAttributes
.SecurityDescriptor
= NULL
;
666 ObjectAttributes
.SecurityQualityOfService
= &Qos
;
668 Status
= NtAllocateLocallyUniqueId(&AuthenticationId
);
669 if (!NT_SUCCESS(Status
))
673 ExpirationTime
.QuadPart
= -1;
675 /* Get the user SID from the registry */
676 if (!SamGetUserSid (lpszUsername
, &UserSid
))
678 ERR("SamGetUserSid() failed\n");
682 TokenUser
.User
.Sid
= UserSid
;
683 TokenUser
.User
.Attributes
= 0;
685 /* Allocate and initialize token groups */
686 TokenGroups
= AllocateGroupSids(&PrimaryGroupSid
,
688 if (NULL
== TokenGroups
)
691 SetLastError(ERROR_OUTOFMEMORY
);
695 /* Allocate and initialize token privileges */
696 TokenPrivileges
= RtlAllocateHeap(GetProcessHeap(), 0,
697 sizeof(TOKEN_PRIVILEGES
)
698 + sizeof(DefaultPrivs
) / sizeof(DefaultPrivs
[0])
699 * sizeof(LUID_AND_ATTRIBUTES
));
700 if (NULL
== TokenPrivileges
)
702 FreeGroupSids(TokenGroups
);
704 SetLastError(ERROR_OUTOFMEMORY
);
708 TokenPrivileges
->PrivilegeCount
= 0;
709 for (i
= 0; i
< sizeof(DefaultPrivs
) / sizeof(DefaultPrivs
[0]); i
++)
711 if (! LookupPrivilegeValueW(NULL
, DefaultPrivs
[i
].PrivName
,
712 &TokenPrivileges
->Privileges
[TokenPrivileges
->PrivilegeCount
].Luid
))
714 WARN("Can't set privilege %S\n", DefaultPrivs
[i
].PrivName
);
718 TokenPrivileges
->Privileges
[TokenPrivileges
->PrivilegeCount
].Attributes
= DefaultPrivs
[i
].Attributes
;
719 TokenPrivileges
->PrivilegeCount
++;
723 TokenOwner
.Owner
= OwnerSid
;
724 TokenPrimaryGroup
.PrimaryGroup
= PrimaryGroupSid
;
727 Dacl
= RtlAllocateHeap(GetProcessHeap(), 0, 1024);
730 FreeGroupSids(TokenGroups
);
732 SetLastError(ERROR_OUTOFMEMORY
);
736 Status
= RtlCreateAcl(Dacl
, 1024, ACL_REVISION
);
737 if (!NT_SUCCESS(Status
))
739 RtlFreeHeap(GetProcessHeap(), 0, Dacl
);
740 FreeGroupSids(TokenGroups
);
741 RtlFreeHeap(GetProcessHeap(), 0, TokenPrivileges
);
746 RtlAddAccessAllowedAce(Dacl
,
751 RtlAllocateAndInitializeSid(&SystemAuthority
,
753 SECURITY_LOCAL_SYSTEM_RID
,
764 RtlAddAccessAllowedAce(Dacl
,
769 RtlFreeSid(LocalSystemSid
);
771 TokenDefaultDacl
.DefaultDacl
= Dacl
;
773 memcpy(TokenSource
.SourceName
,
776 Status
= NtAllocateLocallyUniqueId(&TokenSource
.SourceIdentifier
);
777 if (!NT_SUCCESS(Status
))
779 RtlFreeHeap(GetProcessHeap(), 0, Dacl
);
780 FreeGroupSids(TokenGroups
);
781 RtlFreeHeap(GetProcessHeap(), 0, TokenPrivileges
);
786 Status
= NtCreateToken(phToken
,
800 RtlFreeHeap(GetProcessHeap(), 0, Dacl
);
801 FreeGroupSids(TokenGroups
);
802 RtlFreeHeap(GetProcessHeap(), 0, TokenPrivileges
);
805 return NT_SUCCESS(Status
);