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
10 WINE_DEFAULT_DEBUG_CHANNEL(advapi
);
13 /* FUNCTIONS ***************************************************************/
19 CreateProcessAsUserA(HANDLE hToken
,
20 LPCSTR lpApplicationName
,
22 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
23 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
25 DWORD dwCreationFlags
,
27 LPCSTR lpCurrentDirectory
,
28 LPSTARTUPINFOA lpStartupInfo
,
29 LPPROCESS_INFORMATION lpProcessInformation
)
31 PROCESS_ACCESS_TOKEN AccessToken
;
34 /* Create the process with a suspended main thread */
35 if (!CreateProcessA(lpApplicationName
,
40 dwCreationFlags
| CREATE_SUSPENDED
,
44 lpProcessInformation
))
49 AccessToken
.Token
= hToken
;
50 AccessToken
.Thread
= NULL
;
52 /* Set the new process token */
53 Status
= NtSetInformationProcess(lpProcessInformation
->hProcess
,
57 if (!NT_SUCCESS (Status
))
59 SetLastError(RtlNtStatusToDosError(Status
));
63 /* Resume the main thread */
64 if (!(dwCreationFlags
& CREATE_SUSPENDED
))
66 ResumeThread(lpProcessInformation
->hThread
);
77 CreateProcessAsUserW(HANDLE hToken
,
78 LPCWSTR lpApplicationName
,
80 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
81 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
83 DWORD dwCreationFlags
,
85 LPCWSTR lpCurrentDirectory
,
86 LPSTARTUPINFOW lpStartupInfo
,
87 LPPROCESS_INFORMATION lpProcessInformation
)
89 PROCESS_ACCESS_TOKEN AccessToken
;
92 /* Create the process with a suspended main thread */
93 if (!CreateProcessW(lpApplicationName
,
98 dwCreationFlags
| CREATE_SUSPENDED
,
102 lpProcessInformation
))
107 AccessToken
.Token
= hToken
;
108 AccessToken
.Thread
= NULL
;
110 /* Set the new process token */
111 Status
= NtSetInformationProcess(lpProcessInformation
->hProcess
,
114 sizeof(AccessToken
));
115 if (!NT_SUCCESS (Status
))
117 SetLastError(RtlNtStatusToDosError(Status
));
121 /* Resume the main thread */
122 if (!(dwCreationFlags
& CREATE_SUSPENDED
))
124 ResumeThread(lpProcessInformation
->hThread
);
133 BOOL WINAPI
CreateProcessWithLogonW( LPCWSTR lpUsername
, LPCWSTR lpDomain
, LPCWSTR lpPassword
, DWORD dwLogonFlags
,
134 LPCWSTR lpApplicationName
, LPWSTR lpCommandLine
, DWORD dwCreationFlags
, LPVOID lpEnvironment
,
135 LPCWSTR lpCurrentDirectory
, LPSTARTUPINFOW lpStartupInfo
, LPPROCESS_INFORMATION lpProcessInformation
)
137 FIXME("%s %s %s 0x%08x %s %s 0x%08x %p %s %p %p stub\n", debugstr_w(lpUsername
), debugstr_w(lpDomain
),
138 debugstr_w(lpPassword
), dwLogonFlags
, debugstr_w(lpApplicationName
),
139 debugstr_w(lpCommandLine
), dwCreationFlags
, lpEnvironment
, debugstr_w(lpCurrentDirectory
),
140 lpStartupInfo
, lpProcessInformation
);
149 LogonUserA(LPSTR lpszUsername
,
153 DWORD dwLogonProvider
,
156 UNICODE_STRING UserName
;
157 UNICODE_STRING Domain
;
158 UNICODE_STRING Password
;
162 UserName
.Buffer
= NULL
;
163 Domain
.Buffer
= NULL
;
164 Password
.Buffer
= NULL
;
166 Status
= RtlCreateUnicodeStringFromAsciiz(&UserName
,
168 if (!NT_SUCCESS(Status
))
170 SetLastError(RtlNtStatusToDosError(Status
));
174 Status
= RtlCreateUnicodeStringFromAsciiz(&Domain
,
176 if (!NT_SUCCESS(Status
))
178 SetLastError(RtlNtStatusToDosError(Status
));
182 Status
= RtlCreateUnicodeStringFromAsciiz(&Password
,
184 if (!NT_SUCCESS(Status
))
186 SetLastError(RtlNtStatusToDosError(Status
));
190 ret
= LogonUserW(UserName
.Buffer
,
197 if (Password
.Buffer
!= NULL
)
198 RtlFreeUnicodeString(&Password
);
201 if (Domain
.Buffer
!= NULL
)
202 RtlFreeUnicodeString(&Domain
);
205 if (UserName
.Buffer
!= NULL
)
206 RtlFreeUnicodeString(&UserName
);
214 SamGetUserSid(LPCWSTR UserName
,
225 /* Open the Users key */
226 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
227 L
"SAM\\SAM\\Domains\\Account\\Users",
232 ERR("Failed to open Users key! (Error %lu)\n", GetLastError());
236 /* Open the user key */
237 if (RegOpenKeyExW(hUsersKey
,
243 if (GetLastError() == ERROR_FILE_NOT_FOUND
)
245 ERR("Invalid user name!\n");
246 SetLastError(ERROR_NO_SUCH_USER
);
250 ERR("Failed to open user key! (Error %lu)\n", GetLastError());
253 RegCloseKey(hUsersKey
);
257 RegCloseKey (hUsersKey
);
261 if (RegQueryValueExW(hUserKey
,
268 ERR("Failed to read the SID size! (Error %lu)\n", GetLastError());
269 RegCloseKey(hUserKey
);
273 /* Allocate sid buffer */
274 TRACE("Required SID buffer size: %lu\n", dwLength
);
275 lpSid
= (PSID
)RtlAllocateHeap(RtlGetProcessHeap(),
280 ERR("Failed to allocate SID buffer!\n");
281 RegCloseKey(hUserKey
);
286 if (RegQueryValueExW(hUserKey
,
293 ERR("Failed to read the SID! (Error %lu)\n", GetLastError());
294 RtlFreeHeap(RtlGetProcessHeap(),
297 RegCloseKey(hUserKey
);
301 RegCloseKey(hUserKey
);
310 GetDomainSid(PSID
*Sid
)
312 PPOLICY_ACCOUNT_DOMAIN_INFO Info
= NULL
;
313 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
314 LSA_HANDLE PolicyHandle
;
321 memset(&ObjectAttributes
, 0, sizeof(LSA_OBJECT_ATTRIBUTES
));
322 ObjectAttributes
.Length
= sizeof(LSA_OBJECT_ATTRIBUTES
);
324 Status
= LsaOpenPolicy(NULL
,
328 if (!NT_SUCCESS(Status
))
330 ERR("LsaOpenPolicy failed (Status: 0x%08lx)\n", Status
);
334 Status
= LsaQueryInformationPolicy(PolicyHandle
,
335 PolicyAccountDomainInformation
,
337 if (!NT_SUCCESS(Status
))
339 ERR("LsaQueryInformationPolicy failed (Status: 0x%08lx)\n", Status
);
340 LsaClose(PolicyHandle
);
344 Length
= RtlLengthSid(Info
->DomainSid
);
346 lpSid
= RtlAllocateHeap(RtlGetProcessHeap(),
351 ERR("Failed to allocate SID buffer!\n");
353 LsaClose(PolicyHandle
);
357 memcpy(lpSid
, Info
->DomainSid
, Length
);
362 LsaClose(PolicyHandle
);
369 AppendRidToSid(PSID SrcSid
,
372 ULONG Rids
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
377 RidCount
= *RtlSubAuthorityCountSid(SrcSid
);
381 for (i
= 0; i
< RidCount
; i
++)
382 Rids
[i
] = *RtlSubAuthoritySid(SrcSid
, i
);
384 Rids
[RidCount
] = Rid
;
387 RtlAllocateAndInitializeSid(RtlIdentifierAuthoritySid(SrcSid
),
404 AllocateGroupSids(OUT PSID
*PrimaryGroupSid
,
407 SID_IDENTIFIER_AUTHORITY WorldAuthority
= {SECURITY_WORLD_SID_AUTHORITY
};
408 SID_IDENTIFIER_AUTHORITY LocalAuthority
= {SECURITY_LOCAL_SID_AUTHORITY
};
409 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
410 PTOKEN_GROUPS TokenGroups
;
412 DWORD GroupCount
= 0;
418 Status
= NtAllocateLocallyUniqueId(&Luid
);
419 if (!NT_SUCCESS(Status
))
422 if (!GetDomainSid(&DomainSid
))
425 TokenGroups
= RtlAllocateHeap(
427 sizeof(TOKEN_GROUPS
) +
428 MAX_GROUPS
* sizeof(SID_AND_ATTRIBUTES
));
429 if (TokenGroups
== NULL
)
431 RtlFreeHeap(RtlGetProcessHeap(), 0, DomainSid
);
435 Sid
= AppendRidToSid(DomainSid
, DOMAIN_GROUP_RID_USERS
);
436 RtlFreeHeap(RtlGetProcessHeap(), 0, DomainSid
);
438 /* Member of the domain */
439 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
440 TokenGroups
->Groups
[GroupCount
].Attributes
=
441 SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
442 *PrimaryGroupSid
= Sid
;
445 /* Member of 'Everyone' */
446 RtlAllocateAndInitializeSid(&WorldAuthority
,
457 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
458 TokenGroups
->Groups
[GroupCount
].Attributes
=
459 SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
463 /* Member of 'Administrators' */
464 RtlAllocateAndInitializeSid(&SystemAuthority
,
466 SECURITY_BUILTIN_DOMAIN_RID
,
467 DOMAIN_ALIAS_RID_ADMINS
,
475 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
476 TokenGroups
->Groups
[GroupCount
].Attributes
=
477 SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
480 TRACE("Not adding user to Administrators group\n");
483 /* Member of 'Users' */
484 RtlAllocateAndInitializeSid(&SystemAuthority
,
486 SECURITY_BUILTIN_DOMAIN_RID
,
487 DOMAIN_ALIAS_RID_USERS
,
495 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
496 TokenGroups
->Groups
[GroupCount
].Attributes
=
497 SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
501 RtlAllocateAndInitializeSid(&SystemAuthority
,
502 SECURITY_LOGON_IDS_RID_COUNT
,
503 SECURITY_LOGON_IDS_RID
,
512 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
513 TokenGroups
->Groups
[GroupCount
].Attributes
=
514 SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
| SE_GROUP_LOGON_ID
;
518 /* Member of 'Local users */
519 RtlAllocateAndInitializeSid(&LocalAuthority
,
530 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
531 TokenGroups
->Groups
[GroupCount
].Attributes
=
532 SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
535 /* Member of 'Interactive users' */
536 RtlAllocateAndInitializeSid(&SystemAuthority
,
538 SECURITY_INTERACTIVE_RID
,
547 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
548 TokenGroups
->Groups
[GroupCount
].Attributes
=
549 SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
552 /* Member of 'Authenticated users' */
553 RtlAllocateAndInitializeSid(&SystemAuthority
,
555 SECURITY_AUTHENTICATED_USER_RID
,
564 TokenGroups
->Groups
[GroupCount
].Sid
= Sid
;
565 TokenGroups
->Groups
[GroupCount
].Attributes
=
566 SE_GROUP_ENABLED
| SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_MANDATORY
;
569 TokenGroups
->GroupCount
= GroupCount
;
570 ASSERT(TokenGroups
->GroupCount
<= MAX_GROUPS
);
577 FreeGroupSids(PTOKEN_GROUPS TokenGroups
)
581 for (i
= 0; i
< TokenGroups
->GroupCount
; i
++)
583 if (TokenGroups
->Groups
[i
].Sid
!= NULL
)
584 RtlFreeHeap(GetProcessHeap(), 0, TokenGroups
->Groups
[i
].Sid
);
587 RtlFreeHeap(GetProcessHeap(), 0, TokenGroups
);
595 LogonUserW(LPWSTR lpszUsername
,
599 DWORD dwLogonProvider
,
602 /* FIXME shouldn't use hard-coded list of privileges */
610 { L
"SeMachineAccountPrivilege", 0 },
611 { L
"SeSecurityPrivilege", 0 },
612 { L
"SeTakeOwnershipPrivilege", 0 },
613 { L
"SeLoadDriverPrivilege", 0 },
614 { L
"SeSystemProfilePrivilege", 0 },
615 { L
"SeSystemtimePrivilege", 0 },
616 { L
"SeProfileSingleProcessPrivilege", 0 },
617 { L
"SeIncreaseBasePriorityPrivilege", 0 },
618 { L
"SeCreatePagefilePrivilege", 0 },
619 { L
"SeBackupPrivilege", 0 },
620 { L
"SeRestorePrivilege", 0 },
621 { L
"SeShutdownPrivilege", 0 },
622 { L
"SeDebugPrivilege", 0 },
623 { L
"SeSystemEnvironmentPrivilege", 0 },
624 { L
"SeChangeNotifyPrivilege", SE_PRIVILEGE_ENABLED
| SE_PRIVILEGE_ENABLED_BY_DEFAULT
},
625 { L
"SeRemoteShutdownPrivilege", 0 },
626 { L
"SeUndockPrivilege", 0 },
627 { L
"SeEnableDelegationPrivilege", 0 },
628 { L
"SeImpersonatePrivilege", SE_PRIVILEGE_ENABLED
| SE_PRIVILEGE_ENABLED_BY_DEFAULT
},
629 { L
"SeCreateGlobalPrivilege", SE_PRIVILEGE_ENABLED
| SE_PRIVILEGE_ENABLED_BY_DEFAULT
}
631 OBJECT_ATTRIBUTES ObjectAttributes
;
632 SECURITY_QUALITY_OF_SERVICE Qos
;
633 TOKEN_USER TokenUser
;
634 TOKEN_OWNER TokenOwner
;
635 TOKEN_PRIMARY_GROUP TokenPrimaryGroup
;
636 PTOKEN_GROUPS TokenGroups
;
637 PTOKEN_PRIVILEGES TokenPrivileges
;
638 TOKEN_DEFAULT_DACL TokenDefaultDacl
;
639 LARGE_INTEGER ExpirationTime
;
640 LUID AuthenticationId
;
641 TOKEN_SOURCE TokenSource
;
643 PSID PrimaryGroupSid
= NULL
;
644 PSID OwnerSid
= NULL
;
648 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
651 Qos
.Length
= sizeof(SECURITY_QUALITY_OF_SERVICE
);
652 Qos
.ImpersonationLevel
= SecurityAnonymous
;
653 Qos
.ContextTrackingMode
= SECURITY_STATIC_TRACKING
;
654 Qos
.EffectiveOnly
= FALSE
;
656 ObjectAttributes
.Length
= sizeof(OBJECT_ATTRIBUTES
);
657 ObjectAttributes
.RootDirectory
= NULL
;
658 ObjectAttributes
.ObjectName
= NULL
;
659 ObjectAttributes
.Attributes
= 0;
660 ObjectAttributes
.SecurityDescriptor
= NULL
;
661 ObjectAttributes
.SecurityQualityOfService
= &Qos
;
663 Status
= NtAllocateLocallyUniqueId(&AuthenticationId
);
664 if (!NT_SUCCESS(Status
))
669 ExpirationTime
.QuadPart
= -1;
671 /* Get the user SID from the registry */
672 if (!SamGetUserSid (lpszUsername
, &UserSid
))
674 ERR("SamGetUserSid() failed\n");
678 TokenUser
.User
.Sid
= UserSid
;
679 TokenUser
.User
.Attributes
= 0;
681 /* Allocate and initialize token groups */
682 TokenGroups
= AllocateGroupSids(&PrimaryGroupSid
,
684 if (NULL
== TokenGroups
)
687 SetLastError(ERROR_OUTOFMEMORY
);
691 /* Allocate and initialize token privileges */
692 TokenPrivileges
= RtlAllocateHeap(GetProcessHeap(), 0,
693 sizeof(TOKEN_PRIVILEGES
)
694 + sizeof(DefaultPrivs
) / sizeof(DefaultPrivs
[0])
695 * sizeof(LUID_AND_ATTRIBUTES
));
696 if (NULL
== TokenPrivileges
)
698 FreeGroupSids(TokenGroups
);
700 SetLastError(ERROR_OUTOFMEMORY
);
704 TokenPrivileges
->PrivilegeCount
= 0;
705 for (i
= 0; i
< sizeof(DefaultPrivs
) / sizeof(DefaultPrivs
[0]); i
++)
707 if (! LookupPrivilegeValueW(NULL
,
708 DefaultPrivs
[i
].PrivName
,
709 &TokenPrivileges
->Privileges
[TokenPrivileges
->PrivilegeCount
].Luid
))
711 WARN("Can't set privilege %S\n", DefaultPrivs
[i
].PrivName
);
715 TokenPrivileges
->Privileges
[TokenPrivileges
->PrivilegeCount
].Attributes
= DefaultPrivs
[i
].Attributes
;
716 TokenPrivileges
->PrivilegeCount
++;
720 TokenOwner
.Owner
= OwnerSid
;
721 TokenPrimaryGroup
.PrimaryGroup
= PrimaryGroupSid
;
723 Dacl
= RtlAllocateHeap(GetProcessHeap(), 0, 1024);
726 FreeGroupSids(TokenGroups
);
728 SetLastError(ERROR_OUTOFMEMORY
);
732 Status
= RtlCreateAcl(Dacl
, 1024, ACL_REVISION
);
733 if (!NT_SUCCESS(Status
))
735 RtlFreeHeap(GetProcessHeap(), 0, Dacl
);
736 FreeGroupSids(TokenGroups
);
737 RtlFreeHeap(GetProcessHeap(), 0, TokenPrivileges
);
742 RtlAddAccessAllowedAce(Dacl
,
747 RtlAllocateAndInitializeSid(&SystemAuthority
,
749 SECURITY_LOCAL_SYSTEM_RID
,
760 RtlAddAccessAllowedAce(Dacl
,
765 RtlFreeSid(LocalSystemSid
);
767 TokenDefaultDacl
.DefaultDacl
= Dacl
;
769 memcpy(TokenSource
.SourceName
,
773 Status
= NtAllocateLocallyUniqueId(&TokenSource
.SourceIdentifier
);
774 if (!NT_SUCCESS(Status
))
776 RtlFreeHeap(GetProcessHeap(), 0, Dacl
);
777 FreeGroupSids(TokenGroups
);
778 RtlFreeHeap(GetProcessHeap(), 0, TokenPrivileges
);
783 Status
= NtCreateToken(phToken
,
797 RtlFreeHeap(GetProcessHeap(), 0, Dacl
);
798 FreeGroupSids(TokenGroups
);
799 RtlFreeHeap(GetProcessHeap(), 0, TokenPrivileges
);
802 return NT_SUCCESS(Status
);