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.

245 lines
8.4 KiB

  1. #include <stdafx.h>
  2. #include "misc.h"
  3. #define HTTPS_URL_SCHEME L"https://"
  4. #define HTTPS_URL_SCHEME_CCH (ARRAYSIZE(HTTPS_URL_SCHEME) - 1)
  5. // wininet reg key
  6. #define WININET_REG_LOC L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\passport"
  7. #define WININET_NEXUS_API "ForceNexusLookupExW"
  8. #define PASSPORT_MAX_URL 1024
  9. typedef BOOL (STDAPICALLTYPE *PFNFORCENEXUSLOOKUPEXW) (
  10. IN BOOL fForce,
  11. IN PWSTR pwszRegUrl, // user supplied buffer ...
  12. IN OUT PDWORD pdwRegUrlLen, // ... and length (will be updated to actual length
  13. // on successful return)
  14. IN PWSTR pwszDARealm, // user supplied buffer ...
  15. IN OUT PDWORD pdwDARealmLen // ... and length (will be updated to actual length
  16. // on successful return)
  17. );
  18. // Misc functions
  19. VOID PassportForceNexusRepopulate();
  20. class CPassportClientServices :
  21. public CObjectSafety,
  22. public CImpIDispatch,
  23. public IPassportClientServices
  24. {
  25. public:
  26. CPassportClientServices() :
  27. CImpIDispatch(LIBID_Shell32, 1, 0, IID_IPassportClientServices),
  28. _cRef(1) {}
  29. // IUnknown
  30. STDMETHOD(QueryInterface)(REFIID riid, void **ppvObj);
  31. STDMETHOD_(ULONG,AddRef)(void);
  32. STDMETHOD_(ULONG,Release)(void);
  33. // IDispatch
  34. STDMETHODIMP GetTypeInfoCount(UINT *pctinfo)
  35. { return E_NOTIMPL; }
  36. STDMETHODIMP GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
  37. { return CImpIDispatch::GetTypeInfo(iTInfo, lcid, ppTInfo); }
  38. STDMETHODIMP GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
  39. { return CImpIDispatch::GetIDsOfNames(riid, rgszNames, cNames, lcid, rgDispId); }
  40. STDMETHODIMP Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
  41. { return CImpIDispatch::Invoke(dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr); }
  42. // IPassportClientServices
  43. STDMETHOD(MemberExists)(BSTR bstrUser, BSTR bstrPassword, VARIANT_BOOL* pvfExists);
  44. private:
  45. long _cRef;
  46. };
  47. STDAPI CPassportClientServices_CreateInstance(IUnknown* pUnkOuter, IUnknown** ppunk, LPCOBJECTINFO poi)
  48. {
  49. CPassportClientServices *pPCS = new CPassportClientServices();
  50. if (!pPCS)
  51. return E_OUTOFMEMORY;
  52. HRESULT hr = pPCS->QueryInterface(IID_PPV_ARG(IUnknown, ppunk));
  53. pPCS->Release();
  54. return hr;
  55. }
  56. ULONG CPassportClientServices::AddRef()
  57. {
  58. return InterlockedIncrement(&_cRef);
  59. }
  60. ULONG CPassportClientServices::Release()
  61. {
  62. ASSERT( 0 != _cRef );
  63. ULONG cRef = InterlockedDecrement(&_cRef);
  64. if ( 0 == cRef )
  65. {
  66. delete this;
  67. }
  68. return cRef;
  69. }
  70. HRESULT CPassportClientServices::QueryInterface(REFIID riid, void **ppv)
  71. {
  72. static const QITAB qit[] =
  73. {
  74. QITABENT(CPassportClientServices, IObjectSafety), // IID_IObjectSafety
  75. QITABENT(CPassportClientServices, IDispatch), // IID_IDispatch
  76. QITABENT(CPassportClientServices, IPassportClientServices), // IID_IPassportClientServices
  77. {0, 0 },
  78. };
  79. return QISearch(this, qit, riid, ppv);
  80. }
  81. // DONT_USE_HTTPS - Uncomment this #define to turn off secure sending of information - for debugging purposes only
  82. HRESULT CPassportClientServices::MemberExists(BSTR bstrUser, BSTR bstrPassword, VARIANT_BOOL* pvfExists)
  83. {
  84. *pvfExists = VARIANT_FALSE;
  85. WCHAR szURL[PASSPORT_MAX_URL];
  86. HRESULT hr = PassportGetURL(PASSPORTURL_LOGON, szURL, PASSPORT_MAX_URL);
  87. if (SUCCEEDED(hr))
  88. {
  89. PBYTE lpBuffer = NULL;
  90. if (0 == StrCmpNI(szURL, HTTPS_URL_SCHEME, HTTPS_URL_SCHEME_CCH))
  91. {
  92. PWSTR pszServer = szURL + HTTPS_URL_SCHEME_CCH;
  93. // NULL terminate
  94. PWSTR psz = wcschr(pszServer, L'/');
  95. if (psz)
  96. {
  97. *psz = L'\0';
  98. }
  99. HINTERNET hInternet = InternetOpen(L"Shell Registration",
  100. INTERNET_OPEN_TYPE_PRECONFIG,
  101. NULL,
  102. NULL,
  103. 0);
  104. if (hInternet)
  105. {
  106. HINTERNET hConnection = InternetConnectW(hInternet,
  107. pszServer,
  108. INTERNET_DEFAULT_HTTPS_PORT,
  109. bstrUser,
  110. bstrPassword,
  111. INTERNET_SERVICE_HTTP,
  112. 0,
  113. 0);
  114. if (psz)
  115. {
  116. *psz = L'/';
  117. }
  118. if (hConnection)
  119. {
  120. // set username/pwd
  121. // send the GET request
  122. HINTERNET hRequest = HttpOpenRequest(hConnection,
  123. NULL,
  124. psz,
  125. L"HTTP/1.1",
  126. NULL,
  127. NULL,
  128. INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_SECURE,
  129. 0);
  130. if (hRequest)
  131. {
  132. if (HttpSendRequest(hRequest, NULL, 0, NULL, 0))
  133. {
  134. DWORD dwStatus, dwLength = sizeof(dwStatus);
  135. if (HttpQueryInfo(hRequest,
  136. HTTP_QUERY_STATUS_CODE |
  137. HTTP_QUERY_FLAG_NUMBER,
  138. &dwStatus,
  139. &dwLength,
  140. NULL))
  141. {
  142. // if 200, member is there ...
  143. if (dwStatus == 200)
  144. {
  145. *pvfExists = VARIANT_TRUE;
  146. }
  147. }
  148. }
  149. InternetCloseHandle(hRequest);
  150. }
  151. InternetCloseHandle(hConnection);
  152. }
  153. InternetCloseHandle(hInternet);
  154. }
  155. }
  156. }
  157. return S_OK;
  158. }
  159. //
  160. // read registry for the desired URL
  161. //
  162. HRESULT _PassportGetURLFromHKey(HKEY hkey, PCWSTR pszName, PWSTR pszBuf, DWORD cchBuf)
  163. {
  164. HRESULT hr = E_FAIL;
  165. HKEY hk;
  166. LONG lErr = RegOpenKeyExW(hkey,
  167. WININET_REG_LOC,
  168. 0,
  169. KEY_READ,
  170. &hk);
  171. if (!lErr)
  172. {
  173. DWORD type;
  174. DWORD cbBuf = cchBuf * sizeof WCHAR;
  175. lErr = RegQueryValueExW(hk,
  176. pszName,
  177. 0,
  178. &type,
  179. (PBYTE)pszBuf,
  180. &cbBuf);
  181. if ((!lErr) &&
  182. (L'\0' != *pszBuf))
  183. {
  184. hr = S_OK;
  185. }
  186. RegCloseKey(hk);
  187. }
  188. return hr;
  189. }
  190. HRESULT PassportGetURL(PCWSTR pszName, PWSTR pszBuf, DWORD cchBuf)
  191. {
  192. PassportForceNexusRepopulate();
  193. HRESULT hr = _PassportGetURLFromHKey(HKEY_LOCAL_MACHINE, pszName, pszBuf, cchBuf);
  194. if (FAILED(hr))
  195. {
  196. hr = _PassportGetURLFromHKey(HKEY_CURRENT_USER, pszName, pszBuf, cchBuf);
  197. }
  198. return hr;
  199. }
  200. //
  201. // populate nexus values
  202. //
  203. // #define USE_PRIVATE_WININET
  204. VOID PassportForceNexusRepopulate()
  205. {
  206. HMODULE hm = LoadLibraryA("wininet.dll");
  207. if (hm)
  208. {
  209. PFNFORCENEXUSLOOKUPEXW pfnForceNexusLookupExW = (PFNFORCENEXUSLOOKUPEXW) GetProcAddress(hm, WININET_NEXUS_API);
  210. if (pfnForceNexusLookupExW)
  211. {
  212. pfnForceNexusLookupExW(TRUE, NULL, 0, NULL, 0);
  213. }
  214. FreeLibrary(hm);
  215. }
  216. }