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.

161 lines
4.0 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
  21. ORSTATUS LookupOrCreateTokenForRPCClient(
  22. IN handle_t hCaller,
  23. IN BOOL fAllowUnsecure,
  24. OUT CToken **ppToken,
  25. OUT BOOL* pfUnsecure);
  26. extern
  27. ORSTATUS
  28. LookupOrCreateTokenFromHandle(
  29. IN HANDLE hClientToken,
  30. OUT CToken **ppToken
  31. );
  32. class CToken : public IUserToken
  33. {
  34. public:
  35. CToken(HANDLE hToken,
  36. HANDLE hJobObject,
  37. LUID luid,
  38. PSID psid,
  39. DWORD dwSize)
  40. : _lRefs(1), // constructed with refcount=1
  41. _lHKeyRefs(0),
  42. _hHKCRKey(NULL),
  43. _hImpersonationToken(hToken),
  44. _hJobObject(hJobObject),
  45. _luid(luid)
  46. {
  47. ASSERT(IsValidSid(psid));
  48. ASSERT(dwSize == GetLengthSid(psid));
  49. OrMemoryCopy(&_sid, psid, dwSize);
  50. }
  51. ~CToken();
  52. // IUnknown methods
  53. STDMETHOD(QueryInterface)(REFIID riid, LPVOID* ppv);
  54. STDMETHOD_(ULONG,AddRef)();
  55. STDMETHOD_(ULONG,Release)();
  56. // IUserToken
  57. STDMETHOD(GetUserClassesRootKey)(HKEY* phKey);
  58. STDMETHOD(ReleaseUserClassesRootKey)();
  59. STDMETHOD(GetUserSid)(BYTE **ppSid, USHORT *pcbSid);
  60. STDMETHOD(GetUserToken)(HANDLE* phToken);
  61. void Impersonate();
  62. void Revert();
  63. PSID GetSid() {
  64. return &_sid;
  65. }
  66. HANDLE GetToken() {
  67. return _hImpersonationToken;
  68. }
  69. BOOL MatchLuid(LUID luid) {
  70. return( luid.LowPart == _luid.LowPart
  71. && luid.HighPart == _luid.HighPart);
  72. }
  73. BOOL MatchModifiedLuid(LUID luid);
  74. static CToken *ContainingRecord(CListElement *ple) {
  75. return CONTAINING_RECORD(ple, CToken, _list);
  76. }
  77. void Insert() {
  78. gpTokenList->Insert(&_list);
  79. }
  80. CListElement *Remove() {
  81. return(gpTokenList->Remove(&_list));
  82. }
  83. ULONG GetSessionId();
  84. HRESULT MatchToken(HANDLE hToken, BOOL bMatchRestricted);
  85. HRESULT MatchToken2(CToken *pToken, BOOL bMatchRestricted);
  86. HRESULT MatchTokenSessionID(CToken *pToken);
  87. HRESULT MatchSessionID(LONG lSessionID)
  88. {
  89. return (lSessionID == (LONG) GetSessionId()) ? S_OK : S_FALSE;
  90. }
  91. HRESULT MatchTokenLuid(CToken* pToken);
  92. //
  93. // Compare the safer levels of the two tokens. Returns:
  94. //
  95. // S_FALSE: This token is of lesser authorization than the
  96. // token passed in. (The trust level of the token passed in
  97. // is higher or equal to the trust level of this token.)
  98. // S_OK: This token is of greater or equal authorization
  99. // than the token passed in. (The trust level of the
  100. // token passed in is lower than the trust level of this
  101. // token.)
  102. // Other: An error occured comparing tokens.
  103. //
  104. HRESULT CompareSaferLevels(CToken *pToken);
  105. HRESULT CompareSaferLevels(HANDLE hToken);
  106. #if(_WIN32_WINNT >= 0x0500)
  107. HANDLE GetJobObject() {
  108. return _hJobObject;
  109. }
  110. #endif //(_WIN32_WINNT >= 0x0500)
  111. private:
  112. LONG _lRefs;
  113. LONG _lHKeyRefs;
  114. HKEY _hHKCRKey;
  115. CListElement _list;
  116. HANDLE _hImpersonationToken;
  117. HANDLE _hJobObject;
  118. LUID _luid; // Logon id
  119. SID _sid; // Security (user) id, dynamically sized)
  120. };
  121. #endif