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.

231 lines
5.9 KiB

  1. #include <wininetp.h>
  2. #include <urlmon.h>
  3. #include <splugin.hxx>
  4. #include "htuu.h"
  5. /*---------------------------------------------------------------------------
  6. BASIC_CTX
  7. ---------------------------------------------------------------------------*/
  8. /*---------------------------------------------------------------------------
  9. Constructor
  10. ---------------------------------------------------------------------------*/
  11. BASIC_CTX::BASIC_CTX(HTTP_REQUEST_HANDLE_OBJECT *pRequest, BOOL fIsProxy,
  12. SPMData* pSPM, PWC* pPWC)
  13. : AUTHCTX(pSPM, pPWC)
  14. {
  15. _fIsProxy = fIsProxy;
  16. _pRequest = pRequest;
  17. }
  18. /*---------------------------------------------------------------------------
  19. Destructor
  20. ---------------------------------------------------------------------------*/
  21. BASIC_CTX::~BASIC_CTX()
  22. {}
  23. /*---------------------------------------------------------------------------
  24. PreAuthUser
  25. ---------------------------------------------------------------------------*/
  26. DWORD BASIC_CTX::PreAuthUser(IN LPSTR pBuf, IN OUT LPDWORD pcbBuf)
  27. {
  28. DEBUG_ENTER ((
  29. DBG_HTTPAUTH,
  30. Dword,
  31. "BASIC_CTX::PreAuthUser",
  32. "this=%#x pBuf=%#x pcbBuf=%#x {%d}",
  33. this,
  34. pBuf,
  35. pcbBuf,
  36. *pcbBuf
  37. ));
  38. LPSTR pszUserPass = NULL;
  39. LPSTR pszPass = NULL;
  40. AuthLock();
  41. DWORD dwError = ERROR_SUCCESS;
  42. if (!_pPWC->lpszUser || !_pPWC->lpszPass)
  43. {
  44. dwError = ERROR_INVALID_PARAMETER;
  45. goto exit;
  46. }
  47. pszPass = _pPWC->GetPass();
  48. if (!pszPass)
  49. {
  50. dwError = ERROR_NOT_ENOUGH_MEMORY;
  51. goto exit;
  52. }
  53. // Prefix the header value with the auth type.
  54. const static BYTE szBasic[] = "Basic ";
  55. #define BASIC_LEN sizeof(szBasic)-1
  56. memcpy (pBuf, szBasic, BASIC_LEN);
  57. pBuf += BASIC_LEN;
  58. DWORD cbMaxUserPathLen = strlen(_pPWC->lpszUser) + 1
  59. + strlen(pszPass) + 1
  60. + 10;
  61. // HTUU_encode() parse the buffer 3 bytes at a time;
  62. // In the worst case we will be two bytes short, so add at least 2 here.
  63. // longer buffer doesn't matter, HTUU_encode will adjust appropreiately.
  64. DWORD cbUserPass;
  65. pszUserPass = new CHAR[cbMaxUserPathLen];
  66. if (pszUserPass == NULL)
  67. {
  68. dwError = ERROR_NOT_ENOUGH_MEMORY;
  69. goto exit;
  70. }
  71. cbUserPass = wsprintf(pszUserPass, "%s:%s", _pPWC->lpszUser, pszPass);
  72. INET_ASSERT (cbUserPass < sizeof(cbMaxUserPathLen));
  73. HTUU_encode ((PBYTE) pszUserPass, cbUserPass,
  74. pBuf, *pcbBuf - BASIC_LEN);
  75. *pcbBuf = BASIC_LEN + lstrlen (pBuf);
  76. _pvContext = (LPVOID) 1;
  77. exit:
  78. if (pszUserPass != NULL)
  79. delete [] pszUserPass;
  80. if (pszPass != NULL)
  81. {
  82. SecureZeroMemory(pszPass, strlen(pszPass));
  83. FREE_MEMORY(pszPass);
  84. }
  85. AuthUnlock();
  86. DEBUG_LEAVE(dwError);
  87. return dwError;
  88. }
  89. /*---------------------------------------------------------------------------
  90. UpdateFromHeaders
  91. ---------------------------------------------------------------------------*/
  92. DWORD BASIC_CTX::UpdateFromHeaders(HTTP_REQUEST_HANDLE_OBJECT *pRequest, BOOL fIsProxy)
  93. {
  94. DEBUG_ENTER ((
  95. DBG_HTTPAUTH,
  96. Dword,
  97. "BASIC_CTX::UpdateFromHeaders",
  98. "this=%#x request=%#x isproxy=%B",
  99. this,
  100. pRequest,
  101. fIsProxy
  102. ));
  103. AuthLock();
  104. DWORD dwAuthIdx, cbRealm, dwError;
  105. LPSTR szRealm = NULL;
  106. // Get the associated header.
  107. if ((dwError = FindHdrIdxFromScheme(&dwAuthIdx)) != ERROR_SUCCESS)
  108. goto exit;
  109. // Get any realm.
  110. dwError = GetAuthHeaderData(pRequest, fIsProxy, "Realm",
  111. &szRealm, &cbRealm, ALLOCATE_BUFFER, dwAuthIdx);
  112. // No realm is OK.
  113. if (dwError != ERROR_SUCCESS)
  114. szRealm = NULL;
  115. // If we already have a pwc, ensure that the realm matches. If not,
  116. // find or create a new one and set it in the auth context.
  117. if (_pPWC)
  118. {
  119. if (_pPWC->lpszRealm && szRealm && lstrcmp(_pPWC->lpszRealm, szRealm))
  120. {
  121. // Realms don't match - create a new pwc entry, release the old.
  122. _pPWC->nLockCount--;
  123. _pPWC = FindOrCreatePWC(pRequest, fIsProxy, _pSPMData, szRealm);
  124. INET_ASSERT(_pPWC->pSPM == _pSPMData);
  125. _pPWC->nLockCount++;
  126. }
  127. }
  128. // If no password cache is set in the auth context,
  129. // find or create one and set it in the auth context.
  130. else
  131. {
  132. // Find or create a password cache entry.
  133. _pPWC = FindOrCreatePWC(pRequest, fIsProxy, _pSPMData, szRealm);
  134. if (!_pPWC)
  135. {
  136. dwError = ERROR_INTERNET_INTERNAL_ERROR;
  137. goto exit;
  138. }
  139. INET_ASSERT(_pPWC->pSPM == _pSPMData);
  140. _pPWC->nLockCount++;
  141. }
  142. if (!_pPWC)
  143. {
  144. INET_ASSERT(FALSE);
  145. dwError = ERROR_INTERNET_INTERNAL_ERROR;
  146. goto exit;
  147. }
  148. dwError = ERROR_SUCCESS;
  149. exit:
  150. if (szRealm)
  151. delete []szRealm;
  152. AuthUnlock();
  153. DEBUG_LEAVE(dwError);
  154. return dwError;
  155. }
  156. /*---------------------------------------------------------------------------
  157. PostAuthUser
  158. ---------------------------------------------------------------------------*/
  159. DWORD BASIC_CTX::PostAuthUser()
  160. {
  161. DEBUG_ENTER ((
  162. DBG_HTTPAUTH,
  163. Dword,
  164. "BASIC_CTX::PostAuthUser",
  165. "this=%#x",
  166. this
  167. ));
  168. DWORD dwRet;
  169. AuthLock();
  170. if (! _pvContext && !_pRequest->GetPWC()
  171. && _pPWC->lpszUser && _pPWC->lpszPass)
  172. dwRet = ERROR_INTERNET_FORCE_RETRY;
  173. else
  174. dwRet = ERROR_INTERNET_INCORRECT_PASSWORD;
  175. _pRequest->SetPWC(NULL);
  176. _pvContext = (LPVOID) 1;
  177. AuthUnlock();
  178. DEBUG_LEAVE(dwRet);
  179. return dwRet;
  180. }