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.

150 lines
3.7 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. Token.hxx
  5. Abstract:
  6. Wrapper for holding onto a particular user token.
  7. Author:
  8. Mario Goertzel [MarioGo]
  9. Revision History:
  10. MarioGo 12/20/1995 Bits 'n pieces
  11. JSimmons 03/19/2001 Made CToken implement IUserToken; this is so
  12. we can re-use the CToken cache for catalog
  13. lookups, and have better refcounting
  14. (ie, cleanup at logoff).
  15. --*/
  16. #ifndef __TOKEN_HXX
  17. #define __TOKEN_HXX
  18. class CToken;
  19. extern CRITICAL_SECTION gcsTokenLock;
  20. extern ORSTATUS LookupOrCreateToken(IN handle_t hCaller,
  21. IN BOOL fLocal,
  22. OUT CToken **ppToken);
  23. class CToken : public IUserToken
  24. {
  25. public:
  26. CToken(HANDLE hToken,
  27. HANDLE hJobObject,
  28. LUID luid,
  29. PSID psid,
  30. DWORD dwSize)
  31. : _lRefs(1), // constructed with refcount=1
  32. _lHKeyRefs(0),
  33. _hHKCRKey(NULL),
  34. _hImpersonationToken(hToken),
  35. _hJobObject(hJobObject),
  36. _luid(luid)
  37. {
  38. ASSERT(IsValidSid(psid));
  39. ASSERT(dwSize == GetLengthSid(psid));
  40. OrMemoryCopy(&_sid, psid, dwSize);
  41. }
  42. ~CToken();
  43. // IUnknown methods
  44. STDMETHOD(QueryInterface)(REFIID riid, LPVOID* ppv);
  45. STDMETHOD_(ULONG,AddRef)();
  46. STDMETHOD_(ULONG,Release)();
  47. // IUserToken
  48. STDMETHOD(GetUserClassesRootKey)(HKEY* phKey);
  49. STDMETHOD(ReleaseUserClassesRootKey)();
  50. STDMETHOD(GetUserSid)(BYTE **ppSid, USHORT *pcbSid);
  51. void Impersonate();
  52. void Revert();
  53. PSID GetSid() {
  54. return &_sid;
  55. }
  56. HANDLE GetToken() {
  57. return _hImpersonationToken;
  58. }
  59. BOOL MatchLuid(LUID luid) {
  60. return( luid.LowPart == _luid.LowPart
  61. && luid.HighPart == _luid.HighPart);
  62. }
  63. BOOL MatchModifiedLuid(LUID luid);
  64. static CToken *ContainingRecord(CListElement *ple) {
  65. return CONTAINING_RECORD(ple, CToken, _list);
  66. }
  67. void Insert() {
  68. gpTokenList->Insert(&_list);
  69. }
  70. CListElement *Remove() {
  71. return(gpTokenList->Remove(&_list));
  72. }
  73. ULONG GetSessionId();
  74. HRESULT MatchToken(HANDLE hToken, BOOL bMatchRestricted);
  75. HRESULT MatchToken2(CToken *pToken, BOOL bMatchRestricted);
  76. HRESULT MatchTokenSessionID(CToken *pToken);
  77. HRESULT MatchSessionID(LONG lSessionID)
  78. {
  79. return (lSessionID == (LONG) GetSessionId()) ? S_OK : S_FALSE;
  80. }
  81. HRESULT MatchTokenLuid(CToken* pToken);
  82. //
  83. // Compare the safer levels of the two tokens. Returns:
  84. //
  85. // S_FALSE: This token is of lesser authorization than the
  86. // token passed in. (The trust level of the token passed in
  87. // is higher or equal to the trust level of this token.)
  88. // S_OK: This token is of greater or equal authorization
  89. // than the token passed in. (The trust level of the
  90. // token passed in is lower than the trust level of this
  91. // token.)
  92. // Other: An error occured comparing tokens.
  93. //
  94. HRESULT CompareSaferLevels(CToken *pToken);
  95. HRESULT CompareSaferLevels(HANDLE hToken);
  96. #if(_WIN32_WINNT >= 0x0500)
  97. HANDLE GetJobObject() {
  98. return _hJobObject;
  99. }
  100. #endif //(_WIN32_WINNT >= 0x0500)
  101. private:
  102. LONG _lRefs;
  103. LONG _lHKeyRefs;
  104. HKEY _hHKCRKey;
  105. CListElement _list;
  106. HANDLE _hImpersonationToken;
  107. HANDLE _hJobObject;
  108. LUID _luid; // Logon id
  109. SID _sid; // Security (user) id, dynamically sized)
  110. };
  111. #endif