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.

318 lines
8.0 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name :
  4. anonymousprovider.cxx
  5. Abstract:
  6. Anonymous authentication provider
  7. Author:
  8. Bilal Alam (balam) 10-Jan-2000
  9. Environment:
  10. Win32 - User Mode
  11. Project:
  12. ULW3.DLL
  13. --*/
  14. #include "precomp.hxx"
  15. #include "anonymousprovider.hxx"
  16. HRESULT
  17. ANONYMOUS_AUTH_PROVIDER::DoesApply(
  18. W3_MAIN_CONTEXT * pMainContext,
  19. BOOL * pfApplies
  20. )
  21. /*++
  22. Routine Description:
  23. Does anonymous apply to this request?
  24. Arguments:
  25. pMainContext - Main context representing request
  26. pfApplies - Set to true if SSPI is applicable
  27. Return Value:
  28. HRESULT
  29. --*/
  30. {
  31. UNREFERENCED_PARAMETER( pMainContext );
  32. if ( pfApplies == NULL )
  33. {
  34. DBG_ASSERT( FALSE );
  35. return HRESULT_FROM_WIN32( ERROR_INVALID_PARAMETER );
  36. }
  37. //
  38. // Anonymous ALWAYS applies!
  39. //
  40. *pfApplies = TRUE;
  41. return NO_ERROR;
  42. }
  43. HRESULT
  44. ANONYMOUS_AUTH_PROVIDER::DoAuthenticate(
  45. W3_MAIN_CONTEXT * pMainContext,
  46. BOOL * pfFilterFinished
  47. )
  48. /*++
  49. Routine Description:
  50. Do anonymous authentication (trivial)
  51. Arguments:
  52. pMainContext - Main context representing request
  53. pfFilterFinished - Set to TRUE if filter wants out
  54. Return Value:
  55. HRESULT
  56. --*/
  57. {
  58. W3_METADATA * pMetaData = NULL;
  59. TOKEN_CACHE_ENTRY * pCachedToken = NULL;
  60. HRESULT hr;
  61. ANONYMOUS_USER_CONTEXT* pUserContext = NULL;
  62. BOOL fRet;
  63. DWORD dwLogonError;
  64. BOOL fPossibleUPNLogon = FALSE;
  65. BOOL fFilterSetUser = FALSE;
  66. // add 1 to strUserDomain for separator "\"
  67. STACK_STRA( strUserDomain, UNLEN + IIS_DNLEN + 1 + 1 );
  68. if ( pMainContext == NULL )
  69. {
  70. DBG_ASSERT( FALSE );
  71. return HRESULT_FROM_WIN32( ERROR_INVALID_PARAMETER );
  72. }
  73. pMetaData = pMainContext->QueryUrlContext()->QueryMetaData();
  74. DBG_ASSERT( pMetaData != NULL );
  75. //
  76. // Notify authentication filters
  77. //
  78. if ( pMainContext->IsNotificationNeeded( SF_NOTIFY_AUTHENTICATION ) )
  79. {
  80. HTTP_FILTER_AUTHENT filterAuthent;
  81. STACK_STRA( strPassword, PWLEN + 1 );
  82. // add 1 to strUserDomainW for separator "\"
  83. STACK_STRU( strUserDomainW, UNLEN + IIS_DNLEN + 1 + 1 );
  84. STACK_STRU( strPasswordW, PWLEN + 1 );
  85. STACK_STRU( strDomainNameW, IIS_DNLEN + 1 );
  86. STACK_STRU( strUserNameW, UNLEN + 1 );
  87. DBG_ASSERT( strUserDomain.IsEmpty() );
  88. hr = strUserDomain.Resize( SF_MAX_USERNAME );
  89. if ( FAILED( hr ) )
  90. {
  91. return hr;
  92. }
  93. DBG_ASSERT( strPassword.IsEmpty() );
  94. hr = strPassword.Resize( SF_MAX_PASSWORD );
  95. if ( FAILED( hr ) )
  96. {
  97. return hr;
  98. }
  99. filterAuthent.pszUser = strUserDomain.QueryStr();
  100. filterAuthent.cbUserBuff = SF_MAX_USERNAME;
  101. filterAuthent.pszPassword = strPassword.QueryStr();
  102. filterAuthent.cbPasswordBuff = SF_MAX_PASSWORD;
  103. fRet = pMainContext->NotifyFilters( SF_NOTIFY_AUTHENTICATION,
  104. &filterAuthent,
  105. pfFilterFinished );
  106. if ( !fRet )
  107. {
  108. return HRESULT_FROM_WIN32( GetLastError() );
  109. }
  110. if ( *pfFilterFinished )
  111. {
  112. return NO_ERROR;
  113. }
  114. strUserDomain.SetLen( strlen( strUserDomain.QueryStr() ) );
  115. strPassword.SetLen( strlen( strPassword.QueryStr() ) );
  116. //
  117. // If the filter set a user/password, then use it
  118. //
  119. if ( strUserDomain.QueryCCH() > 0 )
  120. {
  121. fFilterSetUser = TRUE;
  122. //
  123. // Convert to unicode
  124. //
  125. hr = strUserDomainW.CopyA( strUserDomain.QueryStr() );
  126. if ( FAILED( hr ) )
  127. {
  128. return hr;
  129. }
  130. hr = strPasswordW.CopyA( strPassword.QueryStr() );
  131. if ( FAILED( hr ) )
  132. {
  133. return hr;
  134. }
  135. //
  136. // Get username/domain out of domain\username
  137. //
  138. hr = W3_STATE_AUTHENTICATION::SplitUserDomain( strUserDomainW,
  139. &strUserNameW,
  140. &strDomainNameW,
  141. NULL,
  142. &fPossibleUPNLogon );
  143. if ( FAILED( hr ) )
  144. {
  145. return hr;
  146. }
  147. //
  148. // Try to get the token
  149. //
  150. DBG_ASSERT( g_pW3Server->QueryTokenCache() != NULL );
  151. hr = g_pW3Server->QueryTokenCache()->GetCachedToken(
  152. strUserNameW.QueryStr(),
  153. strDomainNameW.QueryStr(),
  154. strPasswordW.QueryStr(),
  155. pMetaData->QueryLogonMethod(),
  156. FALSE,
  157. fPossibleUPNLogon,
  158. NULL,
  159. &pCachedToken,
  160. &dwLogonError );
  161. if ( FAILED( hr ) )
  162. {
  163. return hr;
  164. }
  165. }
  166. }
  167. if ( !fFilterSetUser )
  168. {
  169. //
  170. // If anonymous is not allowed, and a filter didn't
  171. // set credentials, then we need to fail.
  172. //
  173. if ( pMainContext->QueryCheckAnonAuthTypeSupported() &&
  174. !pMetaData->QueryAuthTypeSupported( MD_AUTH_ANONYMOUS ) )
  175. {
  176. return SEC_E_NO_CREDENTIALS;
  177. }
  178. //
  179. // Use the IUSR account
  180. //
  181. hr = pMetaData->GetAndRefAnonymousToken( &pCachedToken );
  182. if( FAILED( hr ) )
  183. {
  184. return hr;
  185. }
  186. }
  187. if ( pCachedToken == NULL )
  188. {
  189. //
  190. // Bogus anonymous account
  191. //
  192. pMainContext->QueryResponse()->SetStatus( HttpStatusUnauthorized,
  193. Http401BadLogon );
  194. return NO_ERROR;
  195. }
  196. //
  197. // For perf reasons, the anonymous user context is inline
  198. //
  199. pUserContext = new (pMainContext) ANONYMOUS_USER_CONTEXT( this );
  200. DBG_ASSERT( pUserContext != NULL );
  201. hr = pUserContext->Create( pCachedToken );
  202. if ( FAILED( hr ) )
  203. {
  204. return hr;
  205. }
  206. if ( fFilterSetUser )
  207. {
  208. //
  209. // Store the new username set by the filter
  210. //
  211. hr = pUserContext->SetUserNameA( strUserDomain.QueryStr() );
  212. if ( FAILED( hr ) )
  213. {
  214. return hr;
  215. }
  216. }
  217. pMainContext->SetUserContext( pUserContext );
  218. return NO_ERROR;
  219. }
  220. HRESULT
  221. ANONYMOUS_USER_CONTEXT::Create(
  222. TOKEN_CACHE_ENTRY * pCachedToken
  223. )
  224. /*++
  225. Routine Description:
  226. Initialize anonymous context
  227. Arguments:
  228. pCachedToken - anonymous user token
  229. Return Value:
  230. HRESULT
  231. --*/
  232. {
  233. if ( pCachedToken == NULL )
  234. {
  235. DBG_ASSERT( FALSE );
  236. return HRESULT_FROM_WIN32( ERROR_INVALID_PARAMETER );
  237. }
  238. _pCachedToken = pCachedToken;
  239. SetCachedToken( TRUE );
  240. return _strUserName.Copy( L"" );
  241. }