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
);
12 /* GLOBALS *****************************************************************/
14 HANDLE LsaHandle
= NULL
;
15 ULONG AuthenticationPackage
= 0;
17 /* FUNCTIONS ***************************************************************/
21 OpenLogonLsaHandle(VOID
)
23 LSA_STRING LogonProcessName
;
24 LSA_STRING PackageName
;
25 LSA_OPERATIONAL_MODE SecurityMode
= 0;
28 RtlInitAnsiString((PANSI_STRING
)&LogonProcessName
,
29 "User32LogonProcess");
31 Status
= LsaRegisterLogonProcess(&LogonProcessName
,
34 if (!NT_SUCCESS(Status
))
36 TRACE("LsaRegisterLogonProcess failed (Status 0x%08lx)\n", Status
);
40 RtlInitAnsiString((PANSI_STRING
)&PackageName
,
43 Status
= LsaLookupAuthenticationPackage(LsaHandle
,
45 &AuthenticationPackage
);
46 if (!NT_SUCCESS(Status
))
48 TRACE("LsaLookupAuthenticationPackage failed (Status 0x%08lx)\n", Status
);
52 TRACE("AuthenticationPackage: 0x%08lx\n", AuthenticationPackage
);
55 if (!NT_SUCCESS(Status
))
57 if (LsaHandle
!= NULL
)
59 Status
= LsaDeregisterLogonProcess(LsaHandle
);
60 if (!NT_SUCCESS(Status
))
62 TRACE("LsaDeregisterLogonProcess failed (Status 0x%08lx)\n", Status
);
72 CloseLogonLsaHandle(VOID
)
74 NTSTATUS Status
= STATUS_SUCCESS
;
76 if (LsaHandle
!= NULL
)
78 Status
= LsaDeregisterLogonProcess(LsaHandle
);
79 if (!NT_SUCCESS(Status
))
81 TRACE("LsaDeregisterLogonProcess failed (Status 0x%08lx)\n", Status
);
93 CreateProcessAsUserA(HANDLE hToken
,
94 LPCSTR lpApplicationName
,
96 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
97 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
99 DWORD dwCreationFlags
,
100 LPVOID lpEnvironment
,
101 LPCSTR lpCurrentDirectory
,
102 LPSTARTUPINFOA lpStartupInfo
,
103 LPPROCESS_INFORMATION lpProcessInformation
)
105 PROCESS_ACCESS_TOKEN AccessToken
;
108 /* Create the process with a suspended main thread */
109 if (!CreateProcessA(lpApplicationName
,
114 dwCreationFlags
| CREATE_SUSPENDED
,
118 lpProcessInformation
))
123 AccessToken
.Token
= hToken
;
124 AccessToken
.Thread
= NULL
;
126 /* Set the new process token */
127 Status
= NtSetInformationProcess(lpProcessInformation
->hProcess
,
130 sizeof(AccessToken
));
131 if (!NT_SUCCESS (Status
))
133 SetLastError(RtlNtStatusToDosError(Status
));
137 /* Resume the main thread */
138 if (!(dwCreationFlags
& CREATE_SUSPENDED
))
140 ResumeThread(lpProcessInformation
->hThread
);
151 CreateProcessAsUserW(HANDLE hToken
,
152 LPCWSTR lpApplicationName
,
153 LPWSTR lpCommandLine
,
154 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
155 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
156 BOOL bInheritHandles
,
157 DWORD dwCreationFlags
,
158 LPVOID lpEnvironment
,
159 LPCWSTR lpCurrentDirectory
,
160 LPSTARTUPINFOW lpStartupInfo
,
161 LPPROCESS_INFORMATION lpProcessInformation
)
163 PROCESS_ACCESS_TOKEN AccessToken
;
166 /* Create the process with a suspended main thread */
167 if (!CreateProcessW(lpApplicationName
,
172 dwCreationFlags
| CREATE_SUSPENDED
,
176 lpProcessInformation
))
181 AccessToken
.Token
= hToken
;
182 AccessToken
.Thread
= NULL
;
184 /* Set the new process token */
185 Status
= NtSetInformationProcess(lpProcessInformation
->hProcess
,
188 sizeof(AccessToken
));
189 if (!NT_SUCCESS (Status
))
191 SetLastError(RtlNtStatusToDosError(Status
));
195 /* Resume the main thread */
196 if (!(dwCreationFlags
& CREATE_SUSPENDED
))
198 ResumeThread(lpProcessInformation
->hThread
);
208 CreateProcessWithLogonW(LPCWSTR lpUsername
,
212 LPCWSTR lpApplicationName
,
213 LPWSTR lpCommandLine
,
214 DWORD dwCreationFlags
,
215 LPVOID lpEnvironment
,
216 LPCWSTR lpCurrentDirectory
,
217 LPSTARTUPINFOW lpStartupInfo
,
218 LPPROCESS_INFORMATION lpProcessInformation
)
220 FIXME("%s %s %s 0x%08x %s %s 0x%08x %p %s %p %p stub\n", debugstr_w(lpUsername
), debugstr_w(lpDomain
),
221 debugstr_w(lpPassword
), dwLogonFlags
, debugstr_w(lpApplicationName
),
222 debugstr_w(lpCommandLine
), dwCreationFlags
, lpEnvironment
, debugstr_w(lpCurrentDirectory
),
223 lpStartupInfo
, lpProcessInformation
);
232 LogonUserA(LPSTR lpszUsername
,
236 DWORD dwLogonProvider
,
239 UNICODE_STRING UserName
;
240 UNICODE_STRING Domain
;
241 UNICODE_STRING Password
;
244 UserName
.Buffer
= NULL
;
245 Domain
.Buffer
= NULL
;
246 Password
.Buffer
= NULL
;
248 if (!RtlCreateUnicodeStringFromAsciiz(&UserName
, lpszUsername
))
250 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
254 if (!RtlCreateUnicodeStringFromAsciiz(&Domain
, lpszDomain
))
256 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
260 if (!RtlCreateUnicodeStringFromAsciiz(&Password
, lpszPassword
))
262 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
266 ret
= LogonUserW(UserName
.Buffer
,
273 if (Password
.Buffer
!= NULL
)
274 RtlFreeUnicodeString(&Password
);
277 if (Domain
.Buffer
!= NULL
)
278 RtlFreeUnicodeString(&Domain
);
281 if (UserName
.Buffer
!= NULL
)
282 RtlFreeUnicodeString(&UserName
);
293 LogonUserW(LPWSTR lpszUsername
,
297 DWORD dwLogonProvider
,
300 SID_IDENTIFIER_AUTHORITY LocalAuthority
= {SECURITY_LOCAL_SID_AUTHORITY
};
301 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
302 PSID LogonSid
= NULL
;
303 PSID LocalSid
= NULL
;
304 LSA_STRING OriginName
;
305 UNICODE_STRING DomainName
;
306 UNICODE_STRING UserName
;
307 UNICODE_STRING Password
;
308 PMSV1_0_INTERACTIVE_LOGON AuthInfo
= NULL
;
309 ULONG AuthInfoLength
;
311 TOKEN_SOURCE TokenSource
;
312 PTOKEN_GROUPS TokenGroups
= NULL
;
313 PMSV1_0_INTERACTIVE_PROFILE ProfileBuffer
= NULL
;
314 ULONG ProfileBufferLength
= 0;
316 LUID LogonId
= {0, 0};
317 HANDLE TokenHandle
= NULL
;
318 QUOTA_LIMITS QuotaLimits
;
319 SECURITY_LOGON_TYPE LogonType
;
320 NTSTATUS SubStatus
= STATUS_SUCCESS
;
327 case LOGON32_LOGON_INTERACTIVE
:
328 LogonType
= Interactive
;
331 case LOGON32_LOGON_NETWORK
:
335 case LOGON32_LOGON_BATCH
:
339 case LOGON32_LOGON_SERVICE
:
344 ERR("Invalid logon type: %ul\n", dwLogonType
);
345 Status
= STATUS_INVALID_PARAMETER
;
349 if (LsaHandle
== NULL
)
351 Status
= OpenLogonLsaHandle();
352 if (!NT_SUCCESS(Status
))
356 RtlInitAnsiString((PANSI_STRING
)&OriginName
,
359 RtlInitUnicodeString(&DomainName
,
362 RtlInitUnicodeString(&UserName
,
365 RtlInitUnicodeString(&Password
,
368 AuthInfoLength
= sizeof(MSV1_0_INTERACTIVE_LOGON
)+
369 DomainName
.MaximumLength
+
370 UserName
.MaximumLength
+
371 Password
.MaximumLength
;
373 AuthInfo
= RtlAllocateHeap(RtlGetProcessHeap(),
376 if (AuthInfo
== NULL
)
378 Status
= STATUS_INSUFFICIENT_RESOURCES
;
382 AuthInfo
->MessageType
= MsV1_0InteractiveLogon
;
384 Ptr
= (ULONG_PTR
)AuthInfo
+ sizeof(MSV1_0_INTERACTIVE_LOGON
);
386 AuthInfo
->LogonDomainName
.Length
= DomainName
.Length
;
387 AuthInfo
->LogonDomainName
.MaximumLength
= DomainName
.MaximumLength
;
388 AuthInfo
->LogonDomainName
.Buffer
= (DomainName
.Buffer
== NULL
) ? NULL
: (PWCHAR
)Ptr
;
389 if (DomainName
.MaximumLength
> 0)
391 RtlCopyMemory(AuthInfo
->LogonDomainName
.Buffer
,
393 DomainName
.MaximumLength
);
395 Ptr
+= DomainName
.MaximumLength
;
398 AuthInfo
->UserName
.Length
= UserName
.Length
;
399 AuthInfo
->UserName
.MaximumLength
= UserName
.MaximumLength
;
400 AuthInfo
->UserName
.Buffer
= (PWCHAR
)Ptr
;
401 if (UserName
.MaximumLength
> 0)
402 RtlCopyMemory(AuthInfo
->UserName
.Buffer
,
404 UserName
.MaximumLength
);
406 Ptr
+= UserName
.MaximumLength
;
408 AuthInfo
->Password
.Length
= Password
.Length
;
409 AuthInfo
->Password
.MaximumLength
= Password
.MaximumLength
;
410 AuthInfo
->Password
.Buffer
= (PWCHAR
)Ptr
;
411 if (Password
.MaximumLength
> 0)
412 RtlCopyMemory(AuthInfo
->Password
.Buffer
,
414 Password
.MaximumLength
);
416 /* Create the Logon SID*/
417 AllocateLocallyUniqueId(&LogonId
);
418 Status
= RtlAllocateAndInitializeSid(&SystemAuthority
,
419 SECURITY_LOGON_IDS_RID_COUNT
,
420 SECURITY_LOGON_IDS_RID
,
429 if (!NT_SUCCESS(Status
))
432 /* Create the Local SID*/
433 Status
= RtlAllocateAndInitializeSid(&LocalAuthority
,
444 if (!NT_SUCCESS(Status
))
447 /* Allocate and set the token groups */
448 TokenGroups
= RtlAllocateHeap(RtlGetProcessHeap(),
450 sizeof(TOKEN_GROUPS
) + ((2 - ANYSIZE_ARRAY
) * sizeof(SID_AND_ATTRIBUTES
)));
451 if (TokenGroups
== NULL
)
453 Status
= STATUS_INSUFFICIENT_RESOURCES
;
457 TokenGroups
->GroupCount
= 2;
458 TokenGroups
->Groups
[0].Sid
= LogonSid
;
459 TokenGroups
->Groups
[0].Attributes
= SE_GROUP_MANDATORY
| SE_GROUP_ENABLED
|
460 SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_LOGON_ID
;
461 TokenGroups
->Groups
[1].Sid
= LocalSid
;
462 TokenGroups
->Groups
[1].Attributes
= SE_GROUP_MANDATORY
| SE_GROUP_ENABLED
|
463 SE_GROUP_ENABLED_BY_DEFAULT
;
465 /* Set the token source */
466 strcpy(TokenSource
.SourceName
, "LogonUser");
467 AllocateLocallyUniqueId(&TokenSource
.SourceIdentifier
);
469 Status
= LsaLogonUser(LsaHandle
,
472 AuthenticationPackage
,
477 (PVOID
*)&ProfileBuffer
,
478 &ProfileBufferLength
,
483 if (!NT_SUCCESS(Status
))
485 ERR("LsaLogonUser failed (Status 0x%08lx)\n", Status
);
489 if (ProfileBuffer
!= NULL
)
491 TRACE("ProfileBuffer: %p\n", ProfileBuffer
);
492 TRACE("MessageType: %u\n", ProfileBuffer
->MessageType
);
494 TRACE("FullName: %p\n", ProfileBuffer
->FullName
.Buffer
);
495 TRACE("FullName: %S\n", ProfileBuffer
->FullName
.Buffer
);
497 TRACE("LogonServer: %p\n", ProfileBuffer
->LogonServer
.Buffer
);
498 TRACE("LogonServer: %S\n", ProfileBuffer
->LogonServer
.Buffer
);
501 TRACE("Luid: 0x%08lx%08lx\n", Luid
.HighPart
, Luid
.LowPart
);
503 if (TokenHandle
!= NULL
)
505 TRACE("TokenHandle: %p\n", TokenHandle
);
508 *phToken
= TokenHandle
;
511 if (ProfileBuffer
!= NULL
)
512 LsaFreeReturnBuffer(ProfileBuffer
);
514 if (!NT_SUCCESS(Status
))
516 if (TokenHandle
!= NULL
)
517 CloseHandle(TokenHandle
);
520 if (TokenGroups
!= NULL
)
521 RtlFreeHeap(RtlGetProcessHeap(), 0, TokenGroups
);
523 if (LocalSid
!= NULL
)
524 RtlFreeSid(LocalSid
);
526 if (LogonSid
!= NULL
)
527 RtlFreeSid(LogonSid
);
529 if (AuthInfo
!= NULL
)
530 RtlFreeHeap(RtlGetProcessHeap(), 0, AuthInfo
);
532 if (!NT_SUCCESS(Status
))
534 SetLastError(RtlNtStatusToDosError(Status
));