Leaked source code of windows server 2003
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.

108 lines
2.5 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. HANDLE hToken;
  36. TOKEN_PRIVILEGES structPtr;
  37. LUID luid;
  38. if (uFlags & (EWX_POWEROFF | EWX_REBOOT | EWX_SHUTDOWN)) {
  39. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) {
  40. structPtr.PrivilegeCount = 1;
  41. if (LookupPrivilegeValueW(NULL, SE_SHUTDOWN_NAME, &luid)) {
  42. structPtr.Privileges[0].Luid = luid;
  43. structPtr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  44. LOGN(
  45. eDbgLevelError,
  46. "[ExitWindowsEx] Adding process privileges for restart.");
  47. AdjustTokenPrivileges(hToken, FALSE, &structPtr, 0, NULL, NULL);
  48. }
  49. }
  50. CSTRING_TRY
  51. {
  52. CString csCL(COMMAND_LINE);
  53. if (csCL.CompareNoCase(L"CLOSE_PROCESS") == 0) {
  54. LOGN(
  55. eDbgLevelError,
  56. "[ExitWindowsEx] Closing process.");
  57. ExitProcess(1);
  58. }
  59. }
  60. CSTRING_CATCH
  61. {
  62. // Do nothing
  63. }
  64. }
  65. return ORIGINAL_API(ExitWindowsEx)(uFlags, dwReserved);
  66. }
  67. /*++
  68. Register hooked functions
  69. --*/
  70. HOOK_BEGIN
  71. APIHOOK_ENTRY(USER32.DLL, ExitWindowsEx)
  72. HOOK_END
  73. IMPLEMENT_SHIM_END