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.

301 lines
5.9 KiB

  1. #ifndef _TOKENCACHE_HXX_
  2. #define _TOKENCACHE_HXX_
  3. #include "usercache.hxx"
  4. #include "stringa.hxx"
  5. #define DEFAULT_MD5_HASH_SIZE 16
  6. class TOKEN_CACHE_KEY : public CACHE_KEY
  7. {
  8. public:
  9. TOKEN_CACHE_KEY()
  10. : _strHashKey( _achHashKey, sizeof( _achHashKey ) )
  11. {
  12. }
  13. BOOL
  14. QueryIsEqual(
  15. const CACHE_KEY * pCompareKey
  16. ) const
  17. {
  18. TOKEN_CACHE_KEY * pTokenKey = (TOKEN_CACHE_KEY*) pCompareKey;
  19. DBG_ASSERT( pTokenKey != NULL );
  20. //
  21. // If lengths are not equal, this is easy
  22. //
  23. if ( _strHashKey.QueryCCH() != pTokenKey->_strHashKey.QueryCCH() )
  24. {
  25. return FALSE;
  26. }
  27. //
  28. // Do strcmp
  29. //
  30. return memcmp( _strHashKey.QueryStr(),
  31. pTokenKey->_strHashKey.QueryStr(),
  32. _strHashKey.QueryCCH() ) == 0;
  33. }
  34. DWORD
  35. QueryKeyHash(
  36. VOID
  37. ) const
  38. {
  39. return HashString( _strHashKey.QueryStr() );
  40. }
  41. HRESULT
  42. SetKey(
  43. TOKEN_CACHE_KEY * pCacheKey
  44. )
  45. {
  46. return _strHashKey.Copy( pCacheKey->_strHashKey.QueryStr() );
  47. }
  48. HRESULT
  49. GenMD5HashKey(
  50. IN STRU & strKey,
  51. OUT STRA * strHashKey
  52. );
  53. HRESULT
  54. CreateCacheKey(
  55. WCHAR * pszUserName,
  56. WCHAR * pszDomainName,
  57. WCHAR * pszPassword,
  58. DWORD dwLogonMethod
  59. );
  60. private:
  61. //
  62. // The hashed binary data will be converted to ASCII hex representation,
  63. // so the size would be twice as big as the original one
  64. //
  65. CHAR _achHashKey[ 2 * DEFAULT_MD5_HASH_SIZE + 1 ];
  66. STRA _strHashKey;
  67. };
  68. //
  69. // The check period for how long a token can be in the cache.
  70. // Tokens can be in the cache for up to two times this value
  71. // (in seconds)
  72. //
  73. #define DEFAULT_CACHED_TOKEN_TTL ( 15 * 60 )
  74. #define SID_DEFAULT_SIZE 64
  75. #define TOKEN_CACHE_ENTRY_SIGNATURE 'TC3W'
  76. #define TOKEN_CACHE_ENTRY_FREE_SIGNATURE 'fC3W'
  77. class TOKEN_CACHE_ENTRY : public CACHE_ENTRY
  78. {
  79. public:
  80. TOKEN_CACHE_ENTRY( OBJECT_CACHE * pObjectCache )
  81. : CACHE_ENTRY( pObjectCache ),
  82. m_dwSignature( TOKEN_CACHE_ENTRY_SIGNATURE ),
  83. m_hImpersonationToken( NULL ),
  84. m_hPrimaryToken( NULL ),
  85. m_pSid( NULL )
  86. {
  87. m_liPwdExpiry.HighPart = 0x7fffffff;
  88. m_liPwdExpiry.LowPart = 0xffffffff;
  89. }
  90. CACHE_KEY *
  91. QueryCacheKey(
  92. VOID
  93. ) const
  94. {
  95. return (CACHE_KEY*) &m_cacheKey;
  96. }
  97. HRESULT
  98. SetCacheKey(
  99. TOKEN_CACHE_KEY * pCacheKey
  100. )
  101. {
  102. return m_cacheKey.SetKey( pCacheKey );
  103. }
  104. BOOL
  105. CheckSignature(
  106. VOID
  107. ) const
  108. {
  109. return m_dwSignature == TOKEN_CACHE_ENTRY_SIGNATURE;
  110. }
  111. VOID *
  112. operator new(
  113. size_t size
  114. )
  115. {
  116. DBG_ASSERT( size == sizeof( TOKEN_CACHE_ENTRY ) );
  117. DBG_ASSERT( sm_pachTokenCacheEntry != NULL );
  118. return sm_pachTokenCacheEntry->Alloc();
  119. }
  120. VOID
  121. operator delete(
  122. VOID * pTokenCacheEntry
  123. )
  124. {
  125. DBG_ASSERT( pTokenCacheEntry != NULL );
  126. DBG_ASSERT( sm_pachTokenCacheEntry != NULL );
  127. DBG_REQUIRE( sm_pachTokenCacheEntry->Free( pTokenCacheEntry ) );
  128. }
  129. HANDLE
  130. QueryImpersonationToken(
  131. VOID
  132. );
  133. HANDLE
  134. QueryPrimaryToken(
  135. VOID
  136. );
  137. PSID
  138. QuerySid(
  139. VOID
  140. );
  141. LARGE_INTEGER *
  142. QueryExpiry(
  143. VOID
  144. )
  145. {
  146. return &m_liPwdExpiry;
  147. }
  148. HRESULT
  149. Create(
  150. HANDLE hToken,
  151. LARGE_INTEGER *pliPwdExpiry,
  152. BOOL fImpersonation
  153. );
  154. static
  155. HRESULT
  156. Initialize(
  157. VOID
  158. );
  159. static
  160. VOID
  161. Terminate(
  162. VOID
  163. );
  164. private:
  165. ~TOKEN_CACHE_ENTRY()
  166. {
  167. if ( m_hImpersonationToken != NULL )
  168. {
  169. CloseHandle( m_hImpersonationToken );
  170. m_hImpersonationToken = NULL;
  171. }
  172. if ( m_hPrimaryToken != NULL )
  173. {
  174. CloseHandle( m_hPrimaryToken );
  175. m_hPrimaryToken = NULL;
  176. }
  177. DBG_ASSERT( CheckSignature() );
  178. m_dwSignature = TOKEN_CACHE_ENTRY_FREE_SIGNATURE;
  179. }
  180. DWORD m_dwSignature;
  181. //
  182. // Cache key
  183. //
  184. TOKEN_CACHE_KEY m_cacheKey;
  185. //
  186. // The actual tokens
  187. //
  188. HANDLE m_hImpersonationToken;
  189. HANDLE m_hPrimaryToken;
  190. //
  191. // Time to expire for the token
  192. //
  193. LARGE_INTEGER m_liPwdExpiry;
  194. //
  195. // Keep the sid for file cache purposes
  196. //
  197. PSID m_pSid;
  198. BYTE m_abSid[ SID_DEFAULT_SIZE ];
  199. //
  200. // Allocation cache for TOKEN_CACHE_ENTRY's
  201. //
  202. static ALLOC_CACHE_HANDLER * sm_pachTokenCacheEntry;
  203. };
  204. class TOKEN_CACHE : public OBJECT_CACHE
  205. {
  206. public:
  207. HRESULT
  208. GetCachedToken(
  209. IN LPWSTR pszUserName,
  210. IN LPWSTR pszDomain,
  211. IN LPWSTR pszPassword,
  212. IN DWORD dwLogonMethod,
  213. IN BOOL fPossibleUPNLogon,
  214. OUT TOKEN_CACHE_ENTRY ** ppCachedToken,
  215. OUT DWORD * pdwLogonError,
  216. BOOL fAllowLocalSystem = FALSE
  217. );
  218. WCHAR *
  219. QueryName(
  220. VOID
  221. ) const
  222. {
  223. return L"TOKEN_CACHE";
  224. }
  225. HRESULT
  226. Initialize(
  227. VOID
  228. );
  229. VOID
  230. Terminate(
  231. VOID
  232. );
  233. private:
  234. DWORD m_dwLastPriorityUPNLogon;
  235. };
  236. HRESULT
  237. ToHex(
  238. IN BUFFER & buffSrc,
  239. OUT STRA & strDst
  240. );
  241. #endif