Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.4 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. EnableRestarts.cpp
  5. Abstract:
  6. This DLL APIHooks ExitWindowsEx and gives the process enough privileges to
  7. restart the computer.
  8. Notes:
  9. This is a general purpose shim.
  10. History:
  11. 11/10/1999 v-johnwh Created.
  12. 10/19/2000 andyseti Close process option added with command line to handle
  13. a case where A process cancel ExitWindowsEx request by
  14. B process because A process is waiting for process B to
  15. quit while process B never quit. In Win9x, process B
  16. quit as soon as it calls ExitWindowsEx so process A can
  17. quit also and the system restarts.
  18. --*/
  19. #include "precomp.h"
  20. IMPLEMENT_SHIM_BEGIN(EnableRestarts)
  21. #include "ShimHookMacro.h"
  22. APIHOOK_ENUM_BEGIN
  23. APIHOOK_ENUM_ENTRY(ExitWindowsEx)
  24. APIHOOK_ENUM_END
  25. /*++
  26. This stub function enables appropriate privileges for the process so that it
  27. can restart the machine.
  28. --*/
  29. BOOL
  30. APIHOOK(ExitWindowsEx)(
  31. UINT uFlags,
  32. DWORD dwReserved
  33. )
  34. {
  35. BOOL bRet;
  36. HANDLE hToken;
  37. TOKEN_PRIVILEGES structPtr;
  38. LUID luid;
  39. if (uFlags & (EWX_POWEROFF | EWX_REBOOT | EWX_SHUTDOWN)) {
  40. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) {
  41. structPtr.PrivilegeCount = 1;
  42. if (LookupPrivilegeValueW(NULL, SE_SHUTDOWN_NAME, &luid)) {
  43. structPtr.Privileges[0].Luid = luid;
  44. structPtr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  45. LOGN(
  46. eDbgLevelError,
  47. "[ExitWindowsEx] Adding process privileges for restart.");
  48. AdjustTokenPrivileges(hToken, FALSE, &structPtr, 0, NULL, NULL);
  49. }
  50. }
  51. CSTRING_TRY
  52. {
  53. CString csCL(COMMAND_LINE);
  54. if (csCL.CompareNoCase(L"CLOSE_PROCESS") == 0) {
  55. LOGN(
  56. eDbgLevelError,
  57. "[ExitWindowsEx] Closing process.");
  58. ExitProcess(1);
  59. }
  60. }
  61. CSTRING_CATCH
  62. {
  63. // Do nothing
  64. }
  65. }
  66. return ORIGINAL_API(ExitWindowsEx)(uFlags, dwReserved);
  67. }
  68. /*++
  69. Register hooked functions
  70. --*/
  71. HOOK_BEGIN
  72. APIHOOK_ENTRY(USER32.DLL, ExitWindowsEx)
  73. HOOK_END
  74. IMPLEMENT_SHIM_END