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 NTSTATUS SubStatus
= STATUS_SUCCESS
;
324 if (LsaHandle
== NULL
)
326 Status
= OpenLogonLsaHandle();
327 if (!NT_SUCCESS(Status
))
331 RtlInitAnsiString((PANSI_STRING
)&OriginName
,
334 RtlInitUnicodeString(&DomainName
,
337 RtlInitUnicodeString(&UserName
,
340 RtlInitUnicodeString(&Password
,
343 AuthInfoLength
= sizeof(MSV1_0_INTERACTIVE_LOGON
)+
344 DomainName
.MaximumLength
+
345 UserName
.MaximumLength
+
346 Password
.MaximumLength
;
348 AuthInfo
= RtlAllocateHeap(RtlGetProcessHeap(),
351 if (AuthInfo
== NULL
)
353 Status
= STATUS_INSUFFICIENT_RESOURCES
;
357 AuthInfo
->MessageType
= MsV1_0InteractiveLogon
;
359 Ptr
= (ULONG_PTR
)AuthInfo
+ sizeof(MSV1_0_INTERACTIVE_LOGON
);
361 AuthInfo
->LogonDomainName
.Length
= DomainName
.Length
;
362 AuthInfo
->LogonDomainName
.MaximumLength
= DomainName
.MaximumLength
;
363 AuthInfo
->LogonDomainName
.Buffer
= (DomainName
.Buffer
== NULL
) ? NULL
: (PWCHAR
)Ptr
;
364 if (DomainName
.MaximumLength
> 0)
366 RtlCopyMemory(AuthInfo
->LogonDomainName
.Buffer
,
368 DomainName
.MaximumLength
);
370 Ptr
+= DomainName
.MaximumLength
;
373 AuthInfo
->UserName
.Length
= UserName
.Length
;
374 AuthInfo
->UserName
.MaximumLength
= UserName
.MaximumLength
;
375 AuthInfo
->UserName
.Buffer
= (PWCHAR
)Ptr
;
376 if (UserName
.MaximumLength
> 0)
377 RtlCopyMemory(AuthInfo
->UserName
.Buffer
,
379 UserName
.MaximumLength
);
381 Ptr
+= UserName
.MaximumLength
;
383 AuthInfo
->Password
.Length
= Password
.Length
;
384 AuthInfo
->Password
.MaximumLength
= Password
.MaximumLength
;
385 AuthInfo
->Password
.Buffer
= (PWCHAR
)Ptr
;
386 if (Password
.MaximumLength
> 0)
387 RtlCopyMemory(AuthInfo
->Password
.Buffer
,
389 Password
.MaximumLength
);
391 /* Create the Logon SID*/
392 AllocateLocallyUniqueId(&LogonId
);
393 Status
= RtlAllocateAndInitializeSid(&SystemAuthority
,
394 SECURITY_LOGON_IDS_RID_COUNT
,
395 SECURITY_LOGON_IDS_RID
,
404 if (!NT_SUCCESS(Status
))
407 /* Create the Local SID*/
408 Status
= RtlAllocateAndInitializeSid(&LocalAuthority
,
419 if (!NT_SUCCESS(Status
))
422 /* Allocate and set the token groups */
423 TokenGroups
= RtlAllocateHeap(RtlGetProcessHeap(),
425 sizeof(TOKEN_GROUPS
) + ((2 - ANYSIZE_ARRAY
) * sizeof(SID_AND_ATTRIBUTES
)));
426 if (TokenGroups
== NULL
)
428 Status
= STATUS_INSUFFICIENT_RESOURCES
;
432 TokenGroups
->GroupCount
= 2;
433 TokenGroups
->Groups
[0].Sid
= LogonSid
;
434 TokenGroups
->Groups
[0].Attributes
= SE_GROUP_MANDATORY
| SE_GROUP_ENABLED
|
435 SE_GROUP_ENABLED_BY_DEFAULT
| SE_GROUP_LOGON_ID
;
436 TokenGroups
->Groups
[1].Sid
= LocalSid
;
437 TokenGroups
->Groups
[1].Attributes
= SE_GROUP_MANDATORY
| SE_GROUP_ENABLED
|
438 SE_GROUP_ENABLED_BY_DEFAULT
;
440 /* Set the token source */
441 strcpy(TokenSource
.SourceName
, "LogonUser");
442 AllocateLocallyUniqueId(&TokenSource
.SourceIdentifier
);
444 Status
= LsaLogonUser(LsaHandle
,
447 AuthenticationPackage
,
452 (PVOID
*)&ProfileBuffer
,
453 &ProfileBufferLength
,
458 if (!NT_SUCCESS(Status
))
460 TRACE("LsaLogonUser failed (Status 0x%08lx)\n", Status
);
464 if (ProfileBuffer
!= NULL
)
466 TRACE("ProfileBuffer: %p\n", ProfileBuffer
);
467 TRACE("MessageType: %u\n", ProfileBuffer
->MessageType
);
469 TRACE("FullName: %p\n", ProfileBuffer
->FullName
.Buffer
);
470 TRACE("FullName: %S\n", ProfileBuffer
->FullName
.Buffer
);
472 TRACE("LogonServer: %p\n", ProfileBuffer
->LogonServer
.Buffer
);
473 TRACE("LogonServer: %S\n", ProfileBuffer
->LogonServer
.Buffer
);
476 TRACE("Luid: 0x%08lx%08lx\n", Luid
.HighPart
, Luid
.LowPart
);
478 if (TokenHandle
!= NULL
)
480 TRACE("TokenHandle: %p\n", TokenHandle
);
483 *phToken
= TokenHandle
;
486 if (ProfileBuffer
!= NULL
)
487 LsaFreeReturnBuffer(ProfileBuffer
);
489 if (!NT_SUCCESS(Status
))
491 if (TokenHandle
!= NULL
)
492 CloseHandle(TokenHandle
);
495 if (TokenGroups
!= NULL
)
496 RtlFreeHeap(RtlGetProcessHeap(), 0, TokenGroups
);
498 if (LocalSid
!= NULL
)
499 RtlFreeSid(LocalSid
);
501 if (LogonSid
!= NULL
)
502 RtlFreeSid(LogonSid
);
504 if (AuthInfo
!= NULL
)
505 RtlFreeHeap(RtlGetProcessHeap(), 0, AuthInfo
);
507 if (!NT_SUCCESS(Status
))
509 SetLastError(RtlNtStatusToDosError(Status
));