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 SetLastError(RtlNtStatusToDosError(Status
));
143 /* Resume the main thread */
144 if (!(dwCreationFlags
& CREATE_SUSPENDED
))
146 ResumeThread(lpProcessInformation
->hThread
);
157 CreateProcessAsUserW(HANDLE hToken
,
158 LPCWSTR lpApplicationName
,
159 LPWSTR lpCommandLine
,
160 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
161 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
162 BOOL bInheritHandles
,
163 DWORD dwCreationFlags
,
164 LPVOID lpEnvironment
,
165 LPCWSTR lpCurrentDirectory
,
166 LPSTARTUPINFOW lpStartupInfo
,
167 LPPROCESS_INFORMATION lpProcessInformation
)
169 PROCESS_ACCESS_TOKEN AccessToken
;
172 TRACE("%p %s %s %p %p %d 0x%08x %p %s %p %p\n", hToken
, debugstr_w(lpApplicationName
),
173 debugstr_w(lpCommandLine
), lpProcessAttributes
, lpThreadAttributes
, bInheritHandles
,
174 dwCreationFlags
, lpEnvironment
, debugstr_w(lpCurrentDirectory
), lpStartupInfo
, lpProcessInformation
);
176 /* Create the process with a suspended main thread */
177 if (!CreateProcessW(lpApplicationName
,
182 dwCreationFlags
| CREATE_SUSPENDED
,
186 lpProcessInformation
))
188 ERR("CreateProcessW failed! GLE: %d\n", GetLastError());
192 AccessToken
.Token
= hToken
;
193 AccessToken
.Thread
= NULL
;
195 /* Set the new process token */
196 Status
= NtSetInformationProcess(lpProcessInformation
->hProcess
,
199 sizeof(AccessToken
));
200 if (!NT_SUCCESS (Status
))
202 ERR("NtSetInformationProcess failed: 0x%08x\n", Status
);
203 SetLastError(RtlNtStatusToDosError(Status
));
207 /* Resume the main thread */
208 if (!(dwCreationFlags
& CREATE_SUSPENDED
))
210 ResumeThread(lpProcessInformation
->hThread
);
220 CreateProcessWithLogonW(LPCWSTR lpUsername
,
224 LPCWSTR lpApplicationName
,
225 LPWSTR lpCommandLine
,
226 DWORD dwCreationFlags
,
227 LPVOID lpEnvironment
,
228 LPCWSTR lpCurrentDirectory
,
229 LPSTARTUPINFOW lpStartupInfo
,
230 LPPROCESS_INFORMATION lpProcessInformation
)
232 FIXME("%s %s %s 0x%08x %s %s 0x%08x %p %s %p %p stub\n", debugstr_w(lpUsername
), debugstr_w(lpDomain
),
233 debugstr_w(lpPassword
), dwLogonFlags
, debugstr_w(lpApplicationName
),
234 debugstr_w(lpCommandLine
), dwCreationFlags
, lpEnvironment
, debugstr_w(lpCurrentDirectory
),
235 lpStartupInfo
, lpProcessInformation
);
244 LogonUserA(LPSTR lpszUsername
,
248 DWORD dwLogonProvider
,
251 UNICODE_STRING UserName
;
252 UNICODE_STRING Domain
;
253 UNICODE_STRING Password
;
256 UserName
.Buffer
= NULL
;
257 Domain
.Buffer
= NULL
;
258 Password
.Buffer
= NULL
;
260 if (!RtlCreateUnicodeStringFromAsciiz(&UserName
, lpszUsername
))
262 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
266 if (!RtlCreateUnicodeStringFromAsciiz(&Domain
, lpszDomain
))
268 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
272 if (!RtlCreateUnicodeStringFromAsciiz(&Password
, lpszPassword
))
274 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
278 ret
= LogonUserW(UserName
.Buffer
,
285 if (Password
.Buffer
!= NULL
)
286 RtlFreeUnicodeString(&Password
);
289 if (Domain
.Buffer
!= NULL
)
290 RtlFreeUnicodeString(&Domain
);
293 if (UserName
.Buffer
!= NULL
)
294 RtlFreeUnicodeString(&UserName
);
305 LogonUserW(LPWSTR lpszUsername
,
309 DWORD dwLogonProvider
,
312 SID_IDENTIFIER_AUTHORITY LocalAuthority
= {SECURITY_LOCAL_SID_AUTHORITY
};
313 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
314 PSID LogonSid
= NULL
;
315 PSID LocalSid
= NULL
;
316 LSA_STRING OriginName
;
317 UNICODE_STRING DomainName
;
318 UNICODE_STRING UserName
;
319 UNICODE_STRING Password
;
320 PMSV1_0_INTERACTIVE_LOGON AuthInfo
= NULL
;
321 ULONG AuthInfoLength
;
323 TOKEN_SOURCE TokenSource
;
324 PTOKEN_GROUPS TokenGroups
= NULL
;
325 PMSV1_0_INTERACTIVE_PROFILE ProfileBuffer
= NULL
;
326 ULONG ProfileBufferLength
= 0;
328 LUID LogonId
= {0, 0};
329 HANDLE TokenHandle
= NULL
;
330 QUOTA_LIMITS QuotaLimits
;
331 SECURITY_LOGON_TYPE LogonType
;
332 NTSTATUS SubStatus
= STATUS_SUCCESS
;
339 case LOGON32_LOGON_INTERACTIVE
:
340 LogonType
= Interactive
;
343 case LOGON32_LOGON_NETWORK
:
347 case LOGON32_LOGON_BATCH
:
351 case LOGON32_LOGON_SERVICE
:
356 ERR("Invalid logon type: %ul\n", dwLogonType
);
357 Status
= STATUS_INVALID_PARAMETER
;
361 if (LsaHandle
== NULL
)
363 Status
= OpenLogonLsaHandle();
364 if (!NT_SUCCESS(Status
))
368 RtlInitAnsiString((PANSI_STRING
)&OriginName
,
371 RtlInitUnicodeString(&DomainName
,
374 RtlInitUnicodeString(&UserName
,
377 RtlInitUnicodeString(&Password
,
380 AuthInfoLength
= sizeof(MSV1_0_INTERACTIVE_LOGON
)+
381 DomainName
.MaximumLength
+
382 UserName
.MaximumLength
+
383 Password
.MaximumLength
;
385 AuthInfo
= RtlAllocateHeap(RtlGetProcessHeap(),
388 if (AuthInfo
== NULL
)
390 Status
= STATUS_INSUFFICIENT_RESOURCES
;
394 AuthInfo
->MessageType
= MsV1_0InteractiveLogon
;
396 Ptr
= (ULONG_PTR
)AuthInfo
+ sizeof(MSV1_0_INTERACTIVE_LOGON
);
398 AuthInfo
->LogonDomainName
.Length
= DomainName
.Length
;
399 AuthInfo
->LogonDomainName
.MaximumLength
= DomainName
.MaximumLength
;
400 AuthInfo
->LogonDomainName
.Buffer
= (DomainName
.Buffer
== NULL
) ? NULL
: (PWCHAR
)Ptr
;
401 if (DomainName
.MaximumLength
> 0)
403 RtlCopyMemory(AuthInfo
->LogonDomainName
.Buffer
,
405 DomainName
.MaximumLength
);
407 Ptr
+= DomainName
.MaximumLength
;
410 AuthInfo
->UserName
.Length
= UserName
.Length
;
411 AuthInfo
->UserName
.MaximumLength
= UserName
.MaximumLength
;
412 AuthInfo
->UserName
.Buffer
= (PWCHAR
)Ptr
;
413 if (UserName
.MaximumLength
> 0)
414 RtlCopyMemory(AuthInfo
->UserName
.Buffer
,
416 UserName
.MaximumLength
);
418 Ptr
+= UserName
.MaximumLength
;
420 AuthInfo
->Password
.Length
= Password
.Length
;
421 AuthInfo
->Password
.MaximumLength
= Password
.MaximumLength
;
422 AuthInfo
->Password
.Buffer
= (PWCHAR
)Ptr
;
423 if (Password
.MaximumLength
> 0)
424 RtlCopyMemory(AuthInfo
->Password
.Buffer
,
426 Password
.MaximumLength
);
428 /* Create the Logon SID*/
429 AllocateLocallyUniqueId(&LogonId
);
430 Status
= RtlAllocateAndInitializeSid(&SystemAuthority
,
431 SECURITY_LOGON_IDS_RID_COUNT
,
432 SECURITY_LOGON_IDS_RID
,
441 if (!NT_SUCCESS(Status
))
444 /* Create the Local SID*/
445 Status
= RtlAllocateAndInitializeSid(&LocalAuthority
,
456 if (!NT_SUCCESS(Status
))
459 /* Allocate and set the token groups */
460 TokenGroups
= RtlAllocateHeap(RtlGetProcessHeap(),
462 sizeof(TOKEN_GROUPS
) + ((2 - ANYSIZE_ARRAY
) * sizeof(SID_AND_ATTRIBUTES
)));
463 if (TokenGroups
== NULL
)
465 Status
= STATUS_INSUFFICIENT_RESOURCES
;
469 TokenGroups
->GroupCount
= 2;
470 TokenGroups
->Groups
[0].Sid
= LogonSid
;
471 TokenGroups
->Groups
[0].Attributes
= SE_GROUP_MANDATORY
| SE_GROUP_ENABLED
|
472 SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_LOGON_ID
;
473 TokenGroups
->Groups
[1].Sid
= LocalSid
;
474 TokenGroups
->Groups
[1].Attributes
= SE_GROUP_MANDATORY
| SE_GROUP_ENABLED
|
475 SE_GROUP_ENABLED_BY_DEFAULT
;
477 /* Set the token source */
478 strncpy(TokenSource
.SourceName
, "Advapi ", sizeof(TokenSource
.SourceName
));
479 AllocateLocallyUniqueId(&TokenSource
.SourceIdentifier
);
481 Status
= LsaLogonUser(LsaHandle
,
484 AuthenticationPackage
,
489 (PVOID
*)&ProfileBuffer
,
490 &ProfileBufferLength
,
495 if (!NT_SUCCESS(Status
))
497 ERR("LsaLogonUser failed (Status 0x%08lx)\n", Status
);
501 if (ProfileBuffer
!= NULL
)
503 TRACE("ProfileBuffer: %p\n", ProfileBuffer
);
504 TRACE("MessageType: %u\n", ProfileBuffer
->MessageType
);
506 TRACE("FullName: %p\n", ProfileBuffer
->FullName
.Buffer
);
507 TRACE("FullName: %S\n", ProfileBuffer
->FullName
.Buffer
);
509 TRACE("LogonServer: %p\n", ProfileBuffer
->LogonServer
.Buffer
);
510 TRACE("LogonServer: %S\n", ProfileBuffer
->LogonServer
.Buffer
);
513 TRACE("Luid: 0x%08lx%08lx\n", Luid
.HighPart
, Luid
.LowPart
);
515 if (TokenHandle
!= NULL
)
517 TRACE("TokenHandle: %p\n", TokenHandle
);
520 *phToken
= TokenHandle
;
523 if (ProfileBuffer
!= NULL
)
524 LsaFreeReturnBuffer(ProfileBuffer
);
526 if (!NT_SUCCESS(Status
))
528 if (TokenHandle
!= NULL
)
529 CloseHandle(TokenHandle
);
532 if (TokenGroups
!= NULL
)
533 RtlFreeHeap(RtlGetProcessHeap(), 0, TokenGroups
);
535 if (LocalSid
!= NULL
)
536 RtlFreeSid(LocalSid
);
538 if (LogonSid
!= NULL
)
539 RtlFreeSid(LogonSid
);
541 if (AuthInfo
!= NULL
)
542 RtlFreeHeap(RtlGetProcessHeap(), 0, AuthInfo
);
544 if (!NT_SUCCESS(Status
))
546 SetLastError(RtlNtStatusToDosError(Status
));