2014-11-28 21:48:44 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS Win32k subsystem
|
|
|
|
* PURPOSE: Shutdown routines
|
2015-11-10 17:41:55 +00:00
|
|
|
* FILE: win32ss/user/ntuser/shutdown.c
|
2014-11-28 21:48:44 +00:00
|
|
|
* PROGRAMER: Hermes Belusca
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <win32k.h>
|
2015-02-12 23:35:58 +00:00
|
|
|
|
2015-02-07 15:26:42 +00:00
|
|
|
DBG_DEFAULT_CHANNEL(UserShutdown);
|
2014-11-28 21:48:44 +00:00
|
|
|
|
2015-02-12 23:35:58 +00:00
|
|
|
/* Our local copy of shutdown flags */
|
|
|
|
static ULONG gdwShutdownFlags = 0;
|
|
|
|
|
2014-11-28 21:48:44 +00:00
|
|
|
/*
|
|
|
|
* Based on CSRSS and described in pages 1115 - 1118 "Windows Internals, Fifth Edition".
|
|
|
|
* CSRSS sends WM_CLIENTSHUTDOWN messages to top-level windows, and it is our job
|
|
|
|
* to send WM_QUERYENDSESSION / WM_ENDSESSION messages in response.
|
|
|
|
*/
|
|
|
|
LRESULT
|
|
|
|
IntClientShutdown(IN PWND pWindow,
|
|
|
|
IN WPARAM wParam,
|
|
|
|
IN LPARAM lParam)
|
|
|
|
{
|
|
|
|
LPARAM lParams;
|
|
|
|
BOOL KillTimers;
|
|
|
|
INT i;
|
|
|
|
LRESULT lResult = MCSR_GOODFORSHUTDOWN;
|
|
|
|
HWND *List;
|
|
|
|
|
2015-02-14 05:27:55 +00:00
|
|
|
KillTimers = wParam & MCS_ENDSESSION ? TRUE : FALSE;
|
|
|
|
lParams = lParam & (ENDSESSION_LOGOFF | ENDSESSION_CRITICAL | ENDSESSION_CLOSEAPP);
|
2014-11-28 21:48:44 +00:00
|
|
|
|
|
|
|
/* First, send end sessions to children */
|
|
|
|
List = IntWinListChildren(pWindow);
|
|
|
|
|
|
|
|
if (List)
|
|
|
|
{
|
|
|
|
for (i = 0; List[i]; i++)
|
|
|
|
{
|
|
|
|
PWND WndChild;
|
|
|
|
|
|
|
|
if (!(WndChild = UserGetWindowObject(List[i])))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (wParam & MCS_QUERYENDSESSION)
|
|
|
|
{
|
2024-01-05 00:16:44 +00:00
|
|
|
if (!co_IntSendMessage(UserHMGetHandle(WndChild), WM_QUERYENDSESSION, 0, lParams))
|
2014-11-28 21:48:44 +00:00
|
|
|
{
|
|
|
|
lResult = MCSR_DONOTSHUTDOWN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-05 00:16:44 +00:00
|
|
|
co_IntSendMessage(UserHMGetHandle(WndChild), WM_ENDSESSION, KillTimers, lParams);
|
2014-11-28 21:48:44 +00:00
|
|
|
if (KillTimers)
|
|
|
|
{
|
|
|
|
DestroyTimersForWindow(WndChild->head.pti, WndChild);
|
|
|
|
}
|
|
|
|
lResult = MCSR_SHUTDOWNFINISHED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ExFreePoolWithTag(List, USERTAG_WINDOWLIST);
|
2015-03-10 00:12:41 +00:00
|
|
|
if (lResult == MCSR_DONOTSHUTDOWN)
|
|
|
|
return lResult;
|
2014-11-28 21:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send to the caller */
|
|
|
|
if (wParam & MCS_QUERYENDSESSION)
|
|
|
|
{
|
2024-01-05 00:16:44 +00:00
|
|
|
if (!co_IntSendMessage(UserHMGetHandle(pWindow), WM_QUERYENDSESSION, 0, lParams))
|
2014-11-28 21:48:44 +00:00
|
|
|
{
|
|
|
|
lResult = MCSR_DONOTSHUTDOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-05 00:16:44 +00:00
|
|
|
co_IntSendMessage(UserHMGetHandle(pWindow), WM_ENDSESSION, KillTimers, lParams);
|
2014-11-28 21:48:44 +00:00
|
|
|
if (KillTimers)
|
|
|
|
{
|
|
|
|
DestroyTimersForWindow(pWindow->head.pti, pWindow);
|
|
|
|
}
|
|
|
|
lResult = MCSR_SHUTDOWNFINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lResult;
|
|
|
|
}
|
|
|
|
|
2015-02-07 15:26:42 +00:00
|
|
|
BOOLEAN
|
|
|
|
HasPrivilege(IN PPRIVILEGE_SET Privilege)
|
|
|
|
{
|
|
|
|
BOOLEAN Result;
|
|
|
|
SECURITY_SUBJECT_CONTEXT SubjectContext;
|
|
|
|
|
|
|
|
/* Capture and lock the security subject context */
|
|
|
|
SeCaptureSubjectContext(&SubjectContext);
|
|
|
|
SeLockSubjectContext(&SubjectContext);
|
|
|
|
|
|
|
|
/* Do privilege check */
|
|
|
|
Result = SePrivilegeCheck(Privilege, &SubjectContext, UserMode);
|
|
|
|
|
|
|
|
/* Audit the privilege */
|
|
|
|
#if 0
|
|
|
|
SePrivilegeObjectAuditAlarm(NULL,
|
|
|
|
&SubjectContext,
|
|
|
|
0,
|
|
|
|
Privilege,
|
|
|
|
Result,
|
|
|
|
UserMode);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Unlock and release the security subject context and return */
|
|
|
|
SeUnlockSubjectContext(&SubjectContext);
|
|
|
|
SeReleaseSubjectContext(&SubjectContext);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-02-12 23:35:58 +00:00
|
|
|
BOOL
|
|
|
|
NotifyLogon(IN HWND hWndSta,
|
|
|
|
IN PLUID CallerLuid,
|
|
|
|
IN ULONG Flags,
|
|
|
|
IN NTSTATUS ShutdownStatus)
|
|
|
|
{
|
|
|
|
// LUID SystemLuid = SYSTEM_LUID;
|
|
|
|
ULONG Notif, Param;
|
|
|
|
|
|
|
|
ERR("NotifyLogon(0x%lx, 0x%lx)\n", Flags, ShutdownStatus);
|
|
|
|
|
|
|
|
/* If no Winlogon notifications are needed, just return */
|
|
|
|
if (Flags & EWX_NONOTIFY)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* In case we cancelled the shutdown...*/
|
|
|
|
if (Flags & EWX_SHUTDOWN_CANCELED)
|
|
|
|
{
|
|
|
|
/* ... send a LN_LOGOFF_CANCELED to Winlogon with the real cancel status... */
|
|
|
|
Notif = LN_LOGOFF_CANCELED;
|
|
|
|
Param = ShutdownStatus;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* ... otherwise it's a real logoff. Send the shutdown flags in that case. */
|
|
|
|
Notif = LN_LOGOFF;
|
|
|
|
Param = Flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: At the moment, always send the notifications... In real world some checks are done.
|
|
|
|
// if (hwndSAS && ( (Flags & EWX_SHUTDOWN) || RtlEqualLuid(CallerLuid, &SystemLuid)) )
|
|
|
|
if (hwndSAS)
|
|
|
|
{
|
2017-09-26 09:11:51 +00:00
|
|
|
TRACE("\tSending %s message to Winlogon\n", Notif == LN_LOGOFF ? "LN_LOGOFF" : "LN_LOGOFF_CANCELED");
|
2015-02-12 23:35:58 +00:00
|
|
|
UserPostMessage(hwndSAS, WM_LOGONNOTIFY, Notif, (LPARAM)Param);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERR("hwndSAS == NULL\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-02-07 15:26:42 +00:00
|
|
|
NTSTATUS
|
|
|
|
UserInitiateShutdown(IN PETHREAD Thread,
|
|
|
|
IN OUT PULONG pFlags)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG Flags = *pFlags;
|
|
|
|
LUID CallerLuid;
|
2015-02-12 23:35:58 +00:00
|
|
|
LUID SystemLuid = SYSTEM_LUID;
|
2015-02-07 15:26:42 +00:00
|
|
|
static PRIVILEGE_SET ShutdownPrivilege =
|
|
|
|
{
|
|
|
|
1, PRIVILEGE_SET_ALL_NECESSARY,
|
|
|
|
{ {{SE_SHUTDOWN_PRIVILEGE, 0}, 0} }
|
|
|
|
};
|
|
|
|
|
|
|
|
PPROCESSINFO ppi;
|
|
|
|
|
2017-09-26 09:11:51 +00:00
|
|
|
TRACE("UserInitiateShutdown\n");
|
2015-02-07 15:26:42 +00:00
|
|
|
|
|
|
|
/* Get the caller's LUID */
|
2018-06-16 17:44:27 +00:00
|
|
|
Status = GetProcessLuid(Thread, NULL, &CallerLuid);
|
2015-02-07 15:26:42 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2017-09-26 09:11:51 +00:00
|
|
|
ERR("UserInitiateShutdown: GetProcessLuid failed\n");
|
2015-02-07 15:26:42 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2015-02-12 23:35:58 +00:00
|
|
|
/*
|
|
|
|
* Check if this is the System LUID, and adjust flags if needed.
|
|
|
|
* In particular, be sure there is no EWX_CALLER_SYSTEM flag
|
|
|
|
* spuriously set (could be the sign of malicous app!).
|
|
|
|
*/
|
|
|
|
if (RtlEqualLuid(&CallerLuid, &SystemLuid))
|
|
|
|
Flags |= EWX_CALLER_SYSTEM;
|
|
|
|
else
|
|
|
|
Flags &= ~EWX_CALLER_SYSTEM;
|
|
|
|
|
2015-02-07 15:26:42 +00:00
|
|
|
*pFlags = Flags;
|
|
|
|
|
|
|
|
/* Retrieve the Win32 process info */
|
|
|
|
ppi = PsGetProcessWin32Process(PsGetThreadProcess(Thread));
|
|
|
|
if (ppi == NULL)
|
2017-09-26 09:11:51 +00:00
|
|
|
{
|
|
|
|
ERR("UserInitiateShutdown: Failed to get win32 thread!\n");
|
2015-02-07 15:26:42 +00:00
|
|
|
return STATUS_INVALID_HANDLE;
|
2017-09-26 09:11:51 +00:00
|
|
|
}
|
2015-02-07 15:26:42 +00:00
|
|
|
|
|
|
|
/* If the caller is not Winlogon, do some security checks */
|
|
|
|
if (PsGetThreadProcessId(Thread) != gpidLogon)
|
|
|
|
{
|
2015-02-12 23:35:58 +00:00
|
|
|
/*
|
|
|
|
* Here also, be sure there is no EWX_CALLER_WINLOGON flag
|
|
|
|
* spuriously set (could be the sign of malicous app!).
|
|
|
|
*/
|
|
|
|
Flags &= ~EWX_CALLER_WINLOGON;
|
|
|
|
|
2015-02-07 15:26:42 +00:00
|
|
|
*pFlags = Flags;
|
|
|
|
|
|
|
|
/* Check whether the current process is attached to a window station */
|
|
|
|
if (ppi->prpwinsta == NULL)
|
2017-09-26 09:11:51 +00:00
|
|
|
{
|
|
|
|
ERR("UserInitiateShutdown: Process is not attached to a desktop\n");
|
2015-02-07 15:26:42 +00:00
|
|
|
return STATUS_INVALID_HANDLE;
|
2017-09-26 09:11:51 +00:00
|
|
|
}
|
2015-02-07 15:26:42 +00:00
|
|
|
|
|
|
|
/* Check whether the window station of the current process can send exit requests */
|
|
|
|
if (!RtlAreAllAccessesGranted(ppi->amwinsta, WINSTA_EXITWINDOWS))
|
2017-09-26 09:11:51 +00:00
|
|
|
{
|
|
|
|
ERR("UserInitiateShutdown: Caller doesn't have the rights to shutdown\n");
|
2015-02-07 15:26:42 +00:00
|
|
|
return STATUS_ACCESS_DENIED;
|
2017-09-26 09:11:51 +00:00
|
|
|
}
|
2015-02-07 15:26:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: USERSRV automatically adds the shutdown flag when we poweroff or reboot.
|
|
|
|
*
|
|
|
|
* If the caller wants to shutdown / reboot / power-off...
|
|
|
|
*/
|
|
|
|
if (Flags & EWX_SHUTDOWN)
|
|
|
|
{
|
|
|
|
/* ... check whether it has shutdown privilege */
|
|
|
|
if (!HasPrivilege(&ShutdownPrivilege))
|
2017-09-26 09:11:51 +00:00
|
|
|
{
|
|
|
|
ERR("UserInitiateShutdown: Caller doesn't have the rights to shutdown\n");
|
2015-02-07 15:26:42 +00:00
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
2017-09-26 09:11:51 +00:00
|
|
|
}
|
2015-02-07 15:26:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* ... but if it just wants to log-off, in case its
|
|
|
|
* window station is a non-IO one, fail the call.
|
|
|
|
*/
|
|
|
|
if (ppi->prpwinsta->Flags & WSS_NOIO)
|
2017-09-26 09:11:51 +00:00
|
|
|
{
|
|
|
|
ERR("UserInitiateShutdown: Caller doesn't have the rights to logoff\n");
|
2015-02-07 15:26:42 +00:00
|
|
|
return STATUS_INVALID_DEVICE_REQUEST;
|
2017-09-26 09:11:51 +00:00
|
|
|
}
|
2015-02-07 15:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 23:35:58 +00:00
|
|
|
/* If the caller is not Winlogon, possibly notify it to perform the real shutdown */
|
2015-02-07 15:26:42 +00:00
|
|
|
if (PsGetThreadProcessId(Thread) != gpidLogon)
|
|
|
|
{
|
|
|
|
// FIXME: HACK!! Do more checks!!
|
2017-09-26 09:11:51 +00:00
|
|
|
TRACE("UserInitiateShutdown: Notify Winlogon for shutdown\n");
|
2015-02-12 23:35:58 +00:00
|
|
|
NotifyLogon(hwndSAS, &CallerLuid, Flags, STATUS_SUCCESS);
|
2015-02-07 15:26:42 +00:00
|
|
|
return STATUS_PENDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach this point, that means it's Winlogon that triggered the shutdown.
|
2017-09-26 09:11:51 +00:00
|
|
|
TRACE("UserInitiateShutdown: Winlogon is doing a shutdown\n");
|
2015-02-07 15:26:42 +00:00
|
|
|
|
|
|
|
/*
|
2015-02-12 23:35:58 +00:00
|
|
|
* Update and save the shutdown flags globally for renotifying
|
|
|
|
* Winlogon if needed, when calling EndShutdown.
|
2015-02-07 15:26:42 +00:00
|
|
|
*/
|
2015-02-12 23:35:58 +00:00
|
|
|
Flags |= EWX_CALLER_WINLOGON; // Winlogon is doing a shutdown, be sure the internal flag is set.
|
2015-02-07 15:26:42 +00:00
|
|
|
*pFlags = Flags;
|
|
|
|
|
2015-02-12 23:35:58 +00:00
|
|
|
/* Save the shutdown flags now */
|
|
|
|
gdwShutdownFlags = Flags;
|
|
|
|
|
2015-02-07 15:26:42 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
UserEndShutdown(IN PETHREAD Thread,
|
|
|
|
IN NTSTATUS ShutdownStatus)
|
|
|
|
{
|
2015-02-12 23:35:58 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG Flags;
|
|
|
|
LUID CallerLuid;
|
|
|
|
|
2017-09-26 09:11:51 +00:00
|
|
|
TRACE("UserEndShutdown called\n");
|
2015-02-12 23:35:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: Some cleanup should be done when shutdown succeeds,
|
|
|
|
* and some reset should be done when shutdown is cancelled.
|
|
|
|
*/
|
2017-09-26 09:11:51 +00:00
|
|
|
//STUB;
|
2015-02-12 23:35:58 +00:00
|
|
|
|
2018-06-16 17:44:27 +00:00
|
|
|
Status = GetProcessLuid(Thread, NULL, &CallerLuid);
|
2015-02-12 23:35:58 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2018-06-16 17:44:27 +00:00
|
|
|
ERR("UserEndShutdown: GetProcessLuid failed\n");
|
2015-02-12 23:35:58 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the global flags because we're going to modify them for our purposes */
|
|
|
|
Flags = gdwShutdownFlags;
|
|
|
|
|
|
|
|
if (NT_SUCCESS(ShutdownStatus))
|
|
|
|
{
|
|
|
|
/* Just report success, and keep the shutdown flags as they are */
|
|
|
|
ShutdownStatus = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Report the status to Winlogon and say that we cancel the shutdown */
|
|
|
|
Flags |= EWX_SHUTDOWN_CANCELED;
|
|
|
|
// FIXME: Should we reset gdwShutdownFlags to 0 ??
|
|
|
|
}
|
|
|
|
|
2017-09-26 09:11:51 +00:00
|
|
|
TRACE("UserEndShutdown: Notify Winlogon for end of shutdown\n");
|
2015-02-12 23:35:58 +00:00
|
|
|
NotifyLogon(hwndSAS, &CallerLuid, Flags, ShutdownStatus);
|
|
|
|
|
|
|
|
/* Always return success */
|
|
|
|
return STATUS_SUCCESS;
|
2015-02-07 15:26:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 21:48:44 +00:00
|
|
|
/* EOF */
|