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 TRACE("%p %s %s %p %p %d 0x%08x %p %s %p %p\n", hToken
, debugstr_a(lpApplicationName
),
109 debugstr_a(lpCommandLine
), lpProcessAttributes
, lpThreadAttributes
, bInheritHandles
,
110 dwCreationFlags
, lpEnvironment
, debugstr_a(lpCurrentDirectory
), lpStartupInfo
, lpProcessInformation
);
112 /* Create the process with a suspended main thread */
113 if (!CreateProcessA(lpApplicationName
,
118 dwCreationFlags
| CREATE_SUSPENDED
,
122 lpProcessInformation
))
124 ERR("CreateProcessA failed! GLE: %d\n", GetLastError());
128 AccessToken
.Token
= hToken
;
129 AccessToken
.Thread
= NULL
;
131 /* Set the new process token */
132 Status
= NtSetInformationProcess(lpProcessInformation
->hProcess
,
135 sizeof(AccessToken
));
136 if (!NT_SUCCESS (Status
))
138 ERR("NtSetInformationProcess failed: 0x%08x\n", Status
);
139 TerminateProcess(lpProcessInformation
->hProcess
, Status
);
140 SetLastError(RtlNtStatusToDosError(Status
));
144 /* Resume the main thread */
145 if (!(dwCreationFlags
& CREATE_SUSPENDED
))
147 ResumeThread(lpProcessInformation
->hThread
);
158 CreateProcessAsUserW(HANDLE hToken
,
159 LPCWSTR lpApplicationName
,
160 LPWSTR lpCommandLine
,
161 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
162 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
163 BOOL bInheritHandles
,
164 DWORD dwCreationFlags
,
165 LPVOID lpEnvironment
,
166 LPCWSTR lpCurrentDirectory
,
167 LPSTARTUPINFOW lpStartupInfo
,
168 LPPROCESS_INFORMATION lpProcessInformation
)
170 PROCESS_ACCESS_TOKEN AccessToken
;
173 TRACE("%p %s %s %p %p %d 0x%08x %p %s %p %p\n", hToken
, debugstr_w(lpApplicationName
),
174 debugstr_w(lpCommandLine
), lpProcessAttributes
, lpThreadAttributes
, bInheritHandles
,
175 dwCreationFlags
, lpEnvironment
, debugstr_w(lpCurrentDirectory
), lpStartupInfo
, lpProcessInformation
);
177 /* Create the process with a suspended main thread */
178 if (!CreateProcessW(lpApplicationName
,
183 dwCreationFlags
| CREATE_SUSPENDED
,
187 lpProcessInformation
))
189 ERR("CreateProcessW failed! GLE: %d\n", GetLastError());
193 AccessToken
.Token
= hToken
;
194 AccessToken
.Thread
= NULL
;
196 /* Set the new process token */
197 Status
= NtSetInformationProcess(lpProcessInformation
->hProcess
,
200 sizeof(AccessToken
));
201 if (!NT_SUCCESS (Status
))
203 ERR("NtSetInformationProcess failed: 0x%08x\n", Status
);
204 TerminateProcess(lpProcessInformation
->hProcess
, Status
);
205 SetLastError(RtlNtStatusToDosError(Status
));
209 /* Resume the main thread */
210 if (!(dwCreationFlags
& CREATE_SUSPENDED
))
212 ResumeThread(lpProcessInformation
->hThread
);
222 CreateProcessWithLogonW(LPCWSTR lpUsername
,
226 LPCWSTR lpApplicationName
,
227 LPWSTR lpCommandLine
,
228 DWORD dwCreationFlags
,
229 LPVOID lpEnvironment
,
230 LPCWSTR lpCurrentDirectory
,
231 LPSTARTUPINFOW lpStartupInfo
,
232 LPPROCESS_INFORMATION lpProcessInformation
)
234 FIXME("%s %s %s 0x%08x %s %s 0x%08x %p %s %p %p stub\n", debugstr_w(lpUsername
), debugstr_w(lpDomain
),
235 debugstr_w(lpPassword
), dwLogonFlags
, debugstr_w(lpApplicationName
),
236 debugstr_w(lpCommandLine
), dwCreationFlags
, lpEnvironment
, debugstr_w(lpCurrentDirectory
),
237 lpStartupInfo
, lpProcessInformation
);
246 LogonUserA(LPSTR lpszUsername
,
250 DWORD dwLogonProvider
,
253 UNICODE_STRING UserName
;
254 UNICODE_STRING Domain
;
255 UNICODE_STRING Password
;
258 UserName
.Buffer
= NULL
;
259 Domain
.Buffer
= NULL
;
260 Password
.Buffer
= NULL
;
262 if (!RtlCreateUnicodeStringFromAsciiz(&UserName
, lpszUsername
))
264 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
268 if (!RtlCreateUnicodeStringFromAsciiz(&Domain
, lpszDomain
))
270 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
274 if (!RtlCreateUnicodeStringFromAsciiz(&Password
, lpszPassword
))
276 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
280 ret
= LogonUserW(UserName
.Buffer
,
287 if (Password
.Buffer
!= NULL
)
288 RtlFreeUnicodeString(&Password
);
291 if (Domain
.Buffer
!= NULL
)
292 RtlFreeUnicodeString(&Domain
);
295 if (UserName
.Buffer
!= NULL
)
296 RtlFreeUnicodeString(&UserName
);
307 LogonUserW(LPWSTR lpszUsername
,
311 DWORD dwLogonProvider
,
314 SID_IDENTIFIER_AUTHORITY LocalAuthority
= {SECURITY_LOCAL_SID_AUTHORITY
};
315 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
316 PSID LogonSid
= NULL
;
317 PSID LocalSid
= NULL
;
318 LSA_STRING OriginName
;
319 UNICODE_STRING DomainName
;
320 UNICODE_STRING UserName
;
321 UNICODE_STRING Password
;
322 PMSV1_0_INTERACTIVE_LOGON AuthInfo
= NULL
;
323 ULONG AuthInfoLength
;
325 TOKEN_SOURCE TokenSource
;
326 PTOKEN_GROUPS TokenGroups
= NULL
;
327 PMSV1_0_INTERACTIVE_PROFILE ProfileBuffer
= NULL
;
328 ULONG ProfileBufferLength
= 0;
330 LUID LogonId
= {0, 0};
331 HANDLE TokenHandle
= NULL
;
332 QUOTA_LIMITS QuotaLimits
;
333 SECURITY_LOGON_TYPE LogonType
;
334 NTSTATUS SubStatus
= STATUS_SUCCESS
;
341 case LOGON32_LOGON_INTERACTIVE
:
342 LogonType
= Interactive
;
345 case LOGON32_LOGON_NETWORK
:
349 case LOGON32_LOGON_BATCH
:
353 case LOGON32_LOGON_SERVICE
:
358 ERR("Invalid logon type: %ul\n", dwLogonType
);
359 Status
= STATUS_INVALID_PARAMETER
;
363 if (LsaHandle
== NULL
)
365 Status
= OpenLogonLsaHandle();
366 if (!NT_SUCCESS(Status
))
370 RtlInitAnsiString((PANSI_STRING
)&OriginName
,
373 RtlInitUnicodeString(&DomainName
,
376 RtlInitUnicodeString(&UserName
,
379 RtlInitUnicodeString(&Password
,
382 AuthInfoLength
= sizeof(MSV1_0_INTERACTIVE_LOGON
)+
383 DomainName
.MaximumLength
+
384 UserName
.MaximumLength
+
385 Password
.MaximumLength
;
387 AuthInfo
= RtlAllocateHeap(RtlGetProcessHeap(),
390 if (AuthInfo
== NULL
)
392 Status
= STATUS_INSUFFICIENT_RESOURCES
;
396 AuthInfo
->MessageType
= MsV1_0InteractiveLogon
;
398 Ptr
= (ULONG_PTR
)AuthInfo
+ sizeof(MSV1_0_INTERACTIVE_LOGON
);
400 AuthInfo
->LogonDomainName
.Length
= DomainName
.Length
;
401 AuthInfo
->LogonDomainName
.MaximumLength
= DomainName
.MaximumLength
;
402 AuthInfo
->LogonDomainName
.Buffer
= (DomainName
.Buffer
== NULL
) ? NULL
: (PWCHAR
)Ptr
;
403 if (DomainName
.MaximumLength
> 0)
405 RtlCopyMemory(AuthInfo
->LogonDomainName
.Buffer
,
407 DomainName
.MaximumLength
);
409 Ptr
+= DomainName
.MaximumLength
;
412 AuthInfo
->UserName
.Length
= UserName
.Length
;
413 AuthInfo
->UserName
.MaximumLength
= UserName
.MaximumLength
;
414 AuthInfo
->UserName
.Buffer
= (PWCHAR
)Ptr
;
415 if (UserName
.MaximumLength
> 0)
416 RtlCopyMemory(AuthInfo
->UserName
.Buffer
,
418 UserName
.MaximumLength
);
420 Ptr
+= UserName
.MaximumLength
;
422 AuthInfo
->Password
.Length
= Password
.Length
;
423 AuthInfo
->Password
.MaximumLength
= Password
.MaximumLength
;
424 AuthInfo
->Password
.Buffer
= (PWCHAR
)Ptr
;
425 if (Password
.MaximumLength
> 0)
426 RtlCopyMemory(AuthInfo
->Password
.Buffer
,
428 Password
.MaximumLength
);
430 /* Create the Logon SID*/
431 AllocateLocallyUniqueId(&LogonId
);
432 Status
= RtlAllocateAndInitializeSid(&SystemAuthority
,
433 SECURITY_LOGON_IDS_RID_COUNT
,
434 SECURITY_LOGON_IDS_RID
,
443 if (!NT_SUCCESS(Status
))
446 /* Create the Local SID*/
447 Status
= RtlAllocateAndInitializeSid(&LocalAuthority
,
458 if (!NT_SUCCESS(Status
))
461 /* Allocate and set the token groups */
462 TokenGroups
= RtlAllocateHeap(RtlGetProcessHeap(),
464 sizeof(TOKEN_GROUPS
) + ((2 - ANYSIZE_ARRAY
) * sizeof(SID_AND_ATTRIBUTES
)));
465 if (TokenGroups
== NULL
)
467 Status
= STATUS_INSUFFICIENT_RESOURCES
;
471 TokenGroups
->GroupCount
= 2;
472 TokenGroups
->Groups
[0].Sid
= LogonSid
;
473 TokenGroups
->Groups
[0].Attributes
= SE_GROUP_MANDATORY
| SE_GROUP_ENABLED
|
474 SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_LOGON_ID
;
475 TokenGroups
->Groups
[1].Sid
= LocalSid
;
476 TokenGroups
->Groups
[1].Attributes
= SE_GROUP_MANDATORY
| SE_GROUP_ENABLED
|
477 SE_GROUP_ENABLED_BY_DEFAULT
;
479 /* Set the token source */
480 strncpy(TokenSource
.SourceName
, "Advapi ", sizeof(TokenSource
.SourceName
));
481 AllocateLocallyUniqueId(&TokenSource
.SourceIdentifier
);
483 Status
= LsaLogonUser(LsaHandle
,
486 AuthenticationPackage
,
491 (PVOID
*)&ProfileBuffer
,
492 &ProfileBufferLength
,
497 if (!NT_SUCCESS(Status
))
499 ERR("LsaLogonUser failed (Status 0x%08lx)\n", Status
);
503 if (ProfileBuffer
!= NULL
)
505 TRACE("ProfileBuffer: %p\n", ProfileBuffer
);
506 TRACE("MessageType: %u\n", ProfileBuffer
->MessageType
);
508 TRACE("FullName: %p\n", ProfileBuffer
->FullName
.Buffer
);
509 TRACE("FullName: %S\n", ProfileBuffer
->FullName
.Buffer
);
511 TRACE("LogonServer: %p\n", ProfileBuffer
->LogonServer
.Buffer
);
512 TRACE("LogonServer: %S\n", ProfileBuffer
->LogonServer
.Buffer
);
515 TRACE("Luid: 0x%08lx%08lx\n", Luid
.HighPart
, Luid
.LowPart
);
517 if (TokenHandle
!= NULL
)
519 TRACE("TokenHandle: %p\n", TokenHandle
);
522 *phToken
= TokenHandle
;
525 if (ProfileBuffer
!= NULL
)
526 LsaFreeReturnBuffer(ProfileBuffer
);
528 if (!NT_SUCCESS(Status
))
530 if (TokenHandle
!= NULL
)
531 CloseHandle(TokenHandle
);
534 if (TokenGroups
!= NULL
)
535 RtlFreeHeap(RtlGetProcessHeap(), 0, TokenGroups
);
537 if (LocalSid
!= NULL
)
538 RtlFreeSid(LocalSid
);
540 if (LogonSid
!= NULL
)
541 RtlFreeSid(LogonSid
);
543 if (AuthInfo
!= NULL
)
544 RtlFreeHeap(RtlGetProcessHeap(), 0, AuthInfo
);
546 if (!NT_SUCCESS(Status
))
548 SetLastError(RtlNtStatusToDosError(Status
));