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.

322 lines
6.9 KiB

  1. #ifndef _AUTHPROVIDER_HXX_
  2. #define _AUTHPROVIDER_HXX_
  3. #define UNINITIALIZED_ID 0xffff
  4. class CONNECTION_AUTH_CONTEXT: public W3_CONNECTION_STATE
  5. {
  6. public:
  7. CONNECTION_AUTH_CONTEXT()
  8. :m_dwInternalId( UNINITIALIZED_ID )
  9. {
  10. if ( sm_pTraceLog != NULL )
  11. {
  12. WriteRefTraceLog( sm_pTraceLog,
  13. 1,
  14. this );
  15. }
  16. }
  17. virtual
  18. ~CONNECTION_AUTH_CONTEXT()
  19. {
  20. if ( sm_pTraceLog != NULL )
  21. {
  22. WriteRefTraceLog( sm_pTraceLog,
  23. 0,
  24. this );
  25. }
  26. }
  27. DWORD
  28. QueryInternalId(
  29. VOID
  30. )
  31. {
  32. return m_dwInternalId;
  33. }
  34. VOID
  35. SetInternalId(
  36. DWORD dwId
  37. )
  38. {
  39. m_dwInternalId = dwId;
  40. }
  41. virtual
  42. BOOL
  43. CheckSignature(
  44. VOID
  45. )
  46. {
  47. return FALSE;
  48. }
  49. VOID
  50. SetSignature(
  51. DWORD dwSignature
  52. )
  53. {
  54. m_dwSignature = dwSignature;
  55. }
  56. DWORD
  57. QuerySignature(
  58. VOID )
  59. {
  60. return m_dwSignature;
  61. }
  62. static
  63. HRESULT
  64. Initialize(
  65. VOID
  66. )
  67. {
  68. #if DBG
  69. sm_pTraceLog = CreateRefTraceLog( 2000, 0 );
  70. #else
  71. sm_pTraceLog = NULL;
  72. #endif
  73. return NO_ERROR;
  74. }
  75. static
  76. VOID
  77. Terminate(
  78. VOID
  79. )
  80. {
  81. if ( sm_pTraceLog != NULL )
  82. {
  83. DestroyRefTraceLog( sm_pTraceLog );
  84. sm_pTraceLog = NULL;
  85. }
  86. }
  87. private:
  88. DWORD m_dwSignature;
  89. DWORD m_dwInternalId;
  90. static PTRACE_LOG sm_pTraceLog;
  91. };
  92. class AUTH_PROVIDER
  93. {
  94. public:
  95. AUTH_PROVIDER()
  96. {
  97. m_dwInternalId = UNINITIALIZED_ID;
  98. }
  99. virtual ~AUTH_PROVIDER()
  100. {
  101. }
  102. virtual
  103. HRESULT
  104. Initialize(
  105. DWORD dwInternalId
  106. ) = 0;
  107. virtual
  108. VOID
  109. Terminate(
  110. VOID
  111. ) = 0;
  112. virtual
  113. HRESULT
  114. DoesApply(
  115. W3_MAIN_CONTEXT * pMainContext,
  116. BOOL * pfApplies
  117. ) = 0;
  118. virtual
  119. HRESULT
  120. DoAuthenticate(
  121. W3_MAIN_CONTEXT * pMainContext
  122. ) = 0;
  123. virtual
  124. HRESULT
  125. OnAccessDenied(
  126. W3_MAIN_CONTEXT * pMainContext
  127. ) = 0;
  128. virtual
  129. DWORD
  130. QueryAuthType(
  131. VOID
  132. ) = 0;
  133. CONNECTION_AUTH_CONTEXT *
  134. QueryConnectionAuthContext(
  135. W3_MAIN_CONTEXT * pMainContext
  136. )
  137. /*++
  138. Description:
  139. Authentication schemes may need to remember authenticaion context
  140. associated with current connection in order to be able to
  141. perform authentication handshake
  142. good example is NTLM that needs 3 legs of authentication
  143. Arguments:
  144. pMainContext - main context
  145. Return Value:
  146. CONNECTION_AUTH_CONTEXT * - NULL if there is no context available
  147. or if there is one but for different
  148. authentication scheme
  149. --*/
  150. {
  151. W3_CONNECTION * pW3Connection = NULL;
  152. CONNECTION_AUTH_CONTEXT * pAuthContext = NULL;
  153. DBG_ASSERT( pMainContext != NULL );
  154. pW3Connection = pMainContext->QueryConnection( FALSE );
  155. if ( pW3Connection != NULL )
  156. {
  157. pAuthContext =
  158. ( CONNECTION_AUTH_CONTEXT * )pW3Connection->
  159. QueryConnectionState( CONTEXT_STATE_AUTHENTICATION );
  160. if ( pAuthContext != NULL &&
  161. pAuthContext->QueryInternalId() == QueryInternalId() )
  162. {
  163. DBG_ASSERT( pAuthContext->CheckSignature() );
  164. return pAuthContext;
  165. }
  166. }
  167. //
  168. // Context we retrieved is either NULL or
  169. // is valid for different auth type
  170. //
  171. return NULL;
  172. }
  173. HRESULT
  174. SetConnectionAuthContext(
  175. W3_MAIN_CONTEXT * pMainContext,
  176. CONNECTION_AUTH_CONTEXT * pNewAuthContext
  177. )
  178. {
  179. /*++
  180. Description:
  181. Authentication schemes may need to remember authenticaion context
  182. associated with current connection in order to be able to
  183. perform authentication handshake
  184. good example is NTLM that needs 3 legs of authentication
  185. Arguments:
  186. pMainContext - main context
  187. pNewAuthContext - new authenticaion context. If there is
  188. some authenticaion context already stored
  189. it will be deleted and replaced with new one
  190. Return Value:
  191. HRESULT
  192. --*/
  193. W3_CONNECTION * pW3Connection = NULL;
  194. CONNECTION_AUTH_CONTEXT * pAuthContext = NULL;
  195. DBG_ASSERT( pMainContext != NULL );
  196. if ( pNewAuthContext == NULL )
  197. {
  198. //
  199. // Perform cleanup if needed
  200. //
  201. pW3Connection = pMainContext->QueryConnection( FALSE );
  202. if ( pW3Connection != NULL )
  203. {
  204. pAuthContext =
  205. ( CONNECTION_AUTH_CONTEXT * )pW3Connection->
  206. QueryConnectionState( CONTEXT_STATE_AUTHENTICATION );
  207. if ( pAuthContext != NULL )
  208. {
  209. pW3Connection->SetConnectionState( CONTEXT_STATE_AUTHENTICATION,
  210. NULL );
  211. delete pAuthContext;
  212. pAuthContext = NULL;
  213. }
  214. }
  215. }
  216. else
  217. {
  218. pW3Connection = pMainContext->QueryConnection( TRUE );
  219. if ( pW3Connection != NULL )
  220. {
  221. pAuthContext =
  222. ( CONNECTION_AUTH_CONTEXT * )pW3Connection->
  223. QueryConnectionState( CONTEXT_STATE_AUTHENTICATION );
  224. if ( pAuthContext != NULL )
  225. {
  226. DBG_ASSERT( pAuthContext->CheckSignature() );
  227. delete pAuthContext;
  228. pAuthContext = NULL;
  229. }
  230. pNewAuthContext->SetInternalId( QueryInternalId() );
  231. pW3Connection->SetConnectionState( CONTEXT_STATE_AUTHENTICATION,
  232. pNewAuthContext );
  233. }
  234. else
  235. {
  236. //
  237. // pMainContext->QueryConnection doesn't return error code
  238. // if it fails return generic error
  239. //
  240. return E_FAIL;
  241. }
  242. }
  243. return NO_ERROR;
  244. }
  245. DWORD
  246. QueryInternalId(
  247. VOID
  248. )
  249. {
  250. DBG_ASSERT( m_dwInternalId != UNINITIALIZED_ID );
  251. return m_dwInternalId;
  252. }
  253. VOID
  254. SetInternalId(
  255. DWORD dwId
  256. )
  257. {
  258. m_dwInternalId = dwId;
  259. }
  260. private:
  261. DWORD m_dwInternalId;
  262. };
  263. #endif