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.

165 lines
5.0 KiB

  1. #include <wininetp.h>
  2. #include <splugin.hxx>
  3. #include "htuu.h"
  4. /*---------------------------------------------------------------------------
  5. BASIC_CTX
  6. ---------------------------------------------------------------------------*/
  7. /*---------------------------------------------------------------------------
  8. Constructor
  9. ---------------------------------------------------------------------------*/
  10. BASIC_CTX::BASIC_CTX(HTTP_REQUEST_HANDLE_OBJECT *pRequest, BOOL fIsProxy,
  11. SPMData* pSPM, AUTH_CREDS* pCreds)
  12. : AUTHCTX(pSPM, pCreds)
  13. {
  14. _fIsProxy = fIsProxy;
  15. _pRequest = pRequest;
  16. }
  17. /*---------------------------------------------------------------------------
  18. Destructor
  19. ---------------------------------------------------------------------------*/
  20. BASIC_CTX::~BASIC_CTX()
  21. {}
  22. /*---------------------------------------------------------------------------
  23. PreAuthUser
  24. ---------------------------------------------------------------------------*/
  25. DWORD BASIC_CTX::PreAuthUser(IN LPSTR pBuf, IN OUT LPDWORD pcbBuf)
  26. {
  27. if (!_pCreds->lpszUser || !_pCreds->lpszPass)
  28. return ERROR_INVALID_PARAMETER;
  29. // Prefix the header value with the auth type.
  30. const static BYTE szBasic[] = "Basic ";
  31. #define BASIC_LEN sizeof(szBasic)-1
  32. memcpy (pBuf, szBasic, BASIC_LEN);
  33. pBuf += BASIC_LEN;
  34. DWORD cbUserLen = _pCreds->lpszUser ? strlen(_pCreds->lpszUser) : 0;
  35. DWORD cbPassLen = _pCreds->lpszPass ? strlen(_pCreds->lpszPass) : 0;
  36. // Generate rest of header value by uuencoding user:pass.
  37. DWORD cbMaxUserPathLen = cbUserLen + 1 + cbPassLen + 1
  38. + 2; // HTUU_encode() parse the buffer 3 bytes at a time;
  39. // In the worst case we will be two bytes short, so add 2 here.
  40. // longer buffer doesn't matter, HTUU_encode will adjust appropreiately.
  41. LPSTR pszUserPass = New CHAR[cbMaxUserPathLen];
  42. if (pszUserPass)
  43. {
  44. DWORD cbUserPass;
  45. cbUserPass = wsprintf(pszUserPass, "%s:%s", _pCreds->lpszUser, _pCreds->lpszPass);
  46. INET_ASSERT (cbUserPass < cbMaxUserPathLen);
  47. HTUU_encode ((PBYTE) pszUserPass, cbUserPass,
  48. pBuf, *pcbBuf);
  49. delete [] pszUserPass;
  50. *pcbBuf = BASIC_LEN + lstrlen (pBuf);
  51. _pvContext = (LPVOID) 1;
  52. return ERROR_SUCCESS;
  53. }
  54. else
  55. {
  56. return ERROR_NOT_ENOUGH_MEMORY;
  57. }
  58. }
  59. /*---------------------------------------------------------------------------
  60. UpdateFromHeaders
  61. ---------------------------------------------------------------------------*/
  62. DWORD BASIC_CTX::UpdateFromHeaders(HTTP_REQUEST_HANDLE_OBJECT *pRequest, BOOL fIsProxy)
  63. {
  64. DWORD dwAuthIdx, cbRealm, dwError;
  65. LPSTR szRealm = NULL;
  66. // Get the associated header.
  67. if ((dwError = FindHdrIdxFromScheme(&dwAuthIdx)) != ERROR_SUCCESS)
  68. goto exit;
  69. // Get any realm.
  70. dwError = GetAuthHeaderData(pRequest, fIsProxy, "Realm",
  71. &szRealm, &cbRealm, ALLOCATE_BUFFER, dwAuthIdx);
  72. // No realm is OK.
  73. if (dwError != ERROR_SUCCESS)
  74. szRealm = NULL;
  75. // If we already have a Creds, ensure that the realm matches. If not,
  76. // find or create a new one and set it in the auth context.
  77. if (_pCreds)
  78. {
  79. INET_ASSERT(_pCreds->lpszRealm);
  80. if (/*_pCreds->lpszRealm && */szRealm && lstrcmp(_pCreds->lpszRealm, szRealm))
  81. {
  82. // Realms don't match - create a new Creds entry, release the old.
  83. delete _pCreds;
  84. _pCreds = CreateCreds(pRequest, fIsProxy, _pSPMData, szRealm);
  85. INET_ASSERT(_pCreds->pSPM == _pSPMData);
  86. }
  87. }
  88. // If no password cache is set in the auth context,
  89. // find or create one and set it in the auth context.
  90. else
  91. {
  92. // Find or create a password cache entry.
  93. _pCreds = CreateCreds(pRequest, fIsProxy, _pSPMData, szRealm);
  94. if (!_pCreds)
  95. {
  96. dwError = ERROR_WINHTTP_INTERNAL_ERROR;
  97. goto exit;
  98. }
  99. INET_ASSERT(_pCreds->pSPM == _pSPMData);
  100. // _pCreds->nLockCount++;
  101. }
  102. if (!_pCreds)
  103. {
  104. INET_ASSERT(FALSE);
  105. dwError = ERROR_WINHTTP_INTERNAL_ERROR;
  106. goto exit;
  107. }
  108. dwError = ERROR_SUCCESS;
  109. exit:
  110. if (szRealm)
  111. delete []szRealm;
  112. return dwError;
  113. }
  114. /*---------------------------------------------------------------------------
  115. PostAuthUser
  116. ---------------------------------------------------------------------------*/
  117. DWORD BASIC_CTX::PostAuthUser()
  118. {
  119. DWORD dwRet;
  120. if (! _pvContext && !_pRequest->GetCreds()
  121. && _pCreds->lpszUser && _pCreds->lpszPass)
  122. dwRet = ERROR_WINHTTP_FORCE_RETRY;
  123. else
  124. dwRet = ERROR_WINHTTP_INCORRECT_PASSWORD;
  125. _pRequest->SetCreds(NULL);
  126. _pvContext = (LPVOID) 1;
  127. return dwRet;
  128. }