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.

126 lines
3.7 KiB

  1. // --------------------------------------------------------------------------
  2. // Module Name: PrivilegeEnable.cpp
  3. //
  4. // Copyright (c) 1999-2000, Microsoft Corporation
  5. //
  6. // Classes that handle state preservation, changing and restoration.
  7. //
  8. // History: 1999-08-18 vtan created
  9. // 1999-11-16 vtan separate file
  10. // 2000-02-01 vtan moved from Neptune to Whistler
  11. // --------------------------------------------------------------------------
  12. #include "StandardHeader.h"
  13. #include "PrivilegeEnable.h"
  14. // --------------------------------------------------------------------------
  15. // CThreadToken::CThreadToken
  16. //
  17. // Arguments: <none>
  18. //
  19. // Returns: <none>
  20. //
  21. // Purpose: Initializes the CThreadToken object. Try the thread token
  22. // first and if this fails try the process token.
  23. //
  24. // History: 1999-08-18 vtan created
  25. // --------------------------------------------------------------------------
  26. CThreadToken::CThreadToken (DWORD dwDesiredAccess) :
  27. _hToken(NULL)
  28. {
  29. if (OpenThreadToken(GetCurrentThread(), dwDesiredAccess, FALSE, &_hToken) == FALSE)
  30. {
  31. TBOOL(OpenProcessToken(GetCurrentProcess(), dwDesiredAccess, &_hToken));
  32. }
  33. }
  34. // --------------------------------------------------------------------------
  35. // CThreadToken::~CThreadToken
  36. //
  37. // Arguments: <none>
  38. //
  39. // Returns: <none>
  40. //
  41. // Purpose: Releases resources used by the CThreadToken object.
  42. //
  43. // History: 1999-08-18 vtan created
  44. // --------------------------------------------------------------------------
  45. CThreadToken::~CThreadToken (void)
  46. {
  47. ReleaseHandle(_hToken);
  48. }
  49. // --------------------------------------------------------------------------
  50. // CThreadToken::~CThreadToken
  51. //
  52. // Arguments: <none>
  53. //
  54. // Returns: <none>
  55. //
  56. // Purpose: Magically converts a CThreadToken to a HANDLE.
  57. //
  58. // History: 1999-08-18 vtan created
  59. // --------------------------------------------------------------------------
  60. CThreadToken::operator HANDLE (void) const
  61. {
  62. return(_hToken);
  63. }
  64. // --------------------------------------------------------------------------
  65. // CPrivilegeEnable::CPrivilegeEnable
  66. //
  67. // Arguments: pszName = Name of the privilege to enable.
  68. //
  69. // Returns: <none>
  70. //
  71. // Purpose: Gets the current state of the privilege and enables it. The
  72. // privilege is specified by name and looked up.
  73. //
  74. // History: 1999-08-23 vtan created
  75. // --------------------------------------------------------------------------
  76. CPrivilegeEnable::CPrivilegeEnable (const TCHAR *pszName) :
  77. _fSet(false),
  78. _hToken(TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY)
  79. {
  80. TOKEN_PRIVILEGES newPrivilege;
  81. if (LookupPrivilegeValue(NULL, pszName, &newPrivilege.Privileges[0].Luid) != FALSE)
  82. {
  83. DWORD dwReturnTokenPrivilegesSize;
  84. newPrivilege.PrivilegeCount = 1;
  85. newPrivilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  86. _fSet = (AdjustTokenPrivileges(_hToken, FALSE, &newPrivilege, sizeof(newPrivilege), &_oldPrivilege, &dwReturnTokenPrivilegesSize) != FALSE);
  87. }
  88. }
  89. // --------------------------------------------------------------------------
  90. // CPrivilegeEnable::~CPrivilegeEnable
  91. //
  92. // Arguments: <none>
  93. //
  94. // Returns: <none>
  95. //
  96. // Purpose: Restores the previous state of the privilege prior to
  97. // instantiation of the object.
  98. //
  99. // History: 1999-08-23 vtan created
  100. // --------------------------------------------------------------------------
  101. CPrivilegeEnable::~CPrivilegeEnable (void)
  102. {
  103. if (_fSet)
  104. {
  105. TBOOL(AdjustTokenPrivileges(_hToken, FALSE, &_oldPrivilege, 0, NULL, NULL));
  106. }
  107. }