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.

544 lines
14 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. passport.cpp
  5. Abstract:
  6. WinInet/WinHttp- Passport Auenthtication Package Interface implementation.
  7. Author:
  8. Biao Wang (biaow) 01-Oct-2000
  9. --*/
  10. #include "ppdefs.h"
  11. #include "wininet.h"
  12. #include "Session.h"
  13. #include "ole2.h"
  14. #include "logon.h"
  15. #include "passport.h"
  16. // #include "passport.tmh"
  17. BOOL g_fIgnoreCachedCredsForPassport = FALSE;
  18. BOOL g_fCurrentProcessLoggedOn = FALSE;
  19. WCHAR g_szUserNameLoggedOn[INTERNET_MAX_USER_NAME_LENGTH];
  20. // -----------------------------------------------------------------------------
  21. PP_CONTEXT
  22. PP_InitContext(
  23. PCWSTR pwszHttpStack,
  24. HINTERNET hSession
  25. )
  26. {
  27. // WPP_INIT_TRACING(L"Microsoft\\Passport1.4");
  28. if (pwszHttpStack == NULL)
  29. {
  30. DoTraceMessage(PP_LOG_ERROR, "PP_InitConect() : pwszHttpStack is null");
  31. return 0;
  32. }
  33. SESSION* pSession;
  34. if (SESSION::CreateObject(pwszHttpStack, hSession, pSession) == FALSE)
  35. {
  36. return 0;
  37. }
  38. DoTraceMessage(PP_LOG_INFO, "Passport Context Initialized");
  39. return reinterpret_cast<PP_CONTEXT>(pSession);
  40. }
  41. // -----------------------------------------------------------------------------
  42. VOID
  43. PP_FreeContext(
  44. PP_CONTEXT hPP
  45. )
  46. {
  47. if (hPP == 0)
  48. {
  49. DoTraceMessage(PP_LOG_ERROR, "PP_FreeContext() : hPP is null");
  50. return;
  51. }
  52. SESSION* pSession = reinterpret_cast<SESSION*>(hPP);
  53. if (pSession->RefCount() > 0)
  54. {
  55. DoTraceMessage(PP_LOG_ERROR, "Passport Context ref count not zero before freed");
  56. }
  57. delete pSession;
  58. DoTraceMessage(PP_LOG_INFO, "Passport Context Freed");
  59. // WPP_CLEANUP();
  60. }
  61. BOOL
  62. PP_GetRealm(
  63. PP_CONTEXT hPP,
  64. PWSTR pwszDARealm, // user supplied buffer ...
  65. PDWORD pdwDARealmLen // ... and length (will be updated to actual length
  66. // on successful return)
  67. )
  68. {
  69. if (hPP == 0)
  70. {
  71. DoTraceMessage(PP_LOG_ERROR, "PP_FreeContext() : hPP is null");
  72. return FALSE;
  73. }
  74. SESSION* pSession = reinterpret_cast<SESSION*>(hPP);
  75. return pSession->GetRealm(pwszDARealm, pdwDARealmLen);
  76. }
  77. // -----------------------------------------------------------------------------
  78. PP_LOGON_CONTEXT
  79. PP_InitLogonContext(
  80. PP_CONTEXT hPP,
  81. PCWSTR pwszPartnerInfo,
  82. DWORD dwParentFlags
  83. )
  84. {
  85. if (hPP == NULL)
  86. {
  87. DoTraceMessage(PP_LOG_ERROR, "PP_InitLogonContext() : hPP is null");
  88. return 0;
  89. }
  90. LOGON* pLogon = new LOGON(reinterpret_cast<SESSION*>(hPP), dwParentFlags);
  91. if (pLogon == NULL)
  92. {
  93. DoTraceMessage(PP_LOG_ERROR, "PP_InitLogonContext() failed; not enough memory");
  94. return 0;
  95. }
  96. if (pLogon->Open(pwszPartnerInfo) == FALSE)
  97. {
  98. delete pLogon;
  99. return 0;
  100. }
  101. DoTraceMessage(PP_LOG_INFO, "Passport Logon Context Initialized");
  102. return reinterpret_cast<PP_LOGON_CONTEXT>(pLogon);
  103. }
  104. // -----------------------------------------------------------------------------
  105. VOID
  106. PP_FreeLogonContext(
  107. PP_LOGON_CONTEXT hPPLogon
  108. )
  109. {
  110. if (hPPLogon == 0)
  111. {
  112. DoTraceMessage(PP_LOG_ERROR, "PP_FreeLogonContext() : hPPLogon is null");
  113. return;
  114. }
  115. LOGON* pLogon = reinterpret_cast<LOGON*>(hPPLogon);
  116. delete pLogon;
  117. DoTraceMessage(PP_LOG_INFO, "Passport Logon Context Freed");
  118. }
  119. // -----------------------------------------------------------------------------
  120. DWORD
  121. PP_Logon(
  122. PP_LOGON_CONTEXT hPPLogon,
  123. BOOL fAnonymous,
  124. HANDLE hEvent,
  125. PFN_LOGON_CALLBACK pfnLogonCallback,
  126. DWORD dwContext
  127. )
  128. {
  129. if (hPPLogon == 0)
  130. {
  131. DoTraceMessage(PP_LOG_ERROR, "PP_Logon() : hPPLogon is null");
  132. return 0;
  133. }
  134. LOGON* pLogon = reinterpret_cast<LOGON*>(hPPLogon);
  135. return pLogon->Logon(fAnonymous);
  136. }
  137. PLIST_ENTRY
  138. PP_GetPrivacyEvents(
  139. IN PP_LOGON_CONTEXT hPPLogon
  140. )
  141. {
  142. if (hPPLogon == 0)
  143. {
  144. DoTraceMessage(PP_LOG_ERROR, "PP_GetPrivacyEvents() : hPPLogon is null");
  145. return 0;
  146. }
  147. LOGON* pLogon = reinterpret_cast<LOGON*>(hPPLogon);
  148. return pLogon->GetPrivacyEvents();
  149. }
  150. // -----------------------------------------------------------------------------
  151. BOOL
  152. PP_GetChallengeInfo(
  153. PP_LOGON_CONTEXT hPPLogon,
  154. HBITMAP* phBitmap,
  155. PBOOL pfPrompt,
  156. PWSTR pwszCbText,
  157. PDWORD pdwTextLen,
  158. PWSTR pwszRealm,
  159. DWORD dwMaxRealmLen,
  160. PWSTR pwszReqUserName,
  161. PDWORD pdwReqUserNameLen
  162. )
  163. {
  164. if (hPPLogon == 0)
  165. {
  166. DoTraceMessage(PP_LOG_ERROR, "PP_GetInfoFromChallenge() : hPPLogon is null");
  167. return FALSE;
  168. }
  169. //PP_ASSERT(ppBitmap != NULL);
  170. //PP_ASSERT(pfPrompt != NULL);
  171. LOGON* pLogon = reinterpret_cast<LOGON*>(hPPLogon);
  172. return pLogon->GetChallengeInfo(phBitmap,
  173. pfPrompt,
  174. pwszCbText,
  175. pdwTextLen,
  176. pwszRealm,
  177. dwMaxRealmLen,
  178. pwszReqUserName,
  179. pdwReqUserNameLen);
  180. }
  181. // -----------------------------------------------------------------------------
  182. BOOL
  183. PP_SetCredentials(
  184. PP_LOGON_CONTEXT hPPLogon,
  185. PCWSTR pwszRealm,
  186. PCWSTR pwszTarget,
  187. PCWSTR pwszSignIn,
  188. PCWSTR pwszPassword,
  189. PSYSTEMTIME pTimeCredsEntered
  190. )
  191. {
  192. if (hPPLogon == 0)
  193. {
  194. DoTraceMessage(PP_LOG_ERROR, "PP_SetCredentials() : hPPLogon is null");
  195. return FALSE;
  196. }
  197. LOGON* pLogon = reinterpret_cast<LOGON*>(hPPLogon);
  198. return pLogon->SetCredentials(pwszRealm,
  199. pwszTarget,
  200. pwszSignIn,
  201. pwszPassword,
  202. pTimeCredsEntered);
  203. }
  204. BOOL
  205. PP_GetLogonHost(
  206. IN PP_LOGON_CONTEXT hPPLogon,
  207. IN PWSTR pwszHostName, // user supplied buffer ...
  208. IN OUT PDWORD pdwHostNameLen // ... and length (will be updated to actual length
  209. )
  210. {
  211. if (hPPLogon == 0)
  212. {
  213. DoTraceMessage(PP_LOG_ERROR, "PP_GetLogonHost() : hPPLogon is null");
  214. return FALSE;
  215. }
  216. LOGON* pLogon = reinterpret_cast<LOGON*>(hPPLogon);
  217. return pLogon->GetLogonHost(pwszHostName, pdwHostNameLen);
  218. }
  219. // -----------------------------------------------------------------------------
  220. BOOL
  221. PP_GetAuthorizationInfo(
  222. PP_LOGON_CONTEXT hPPLogon,
  223. PWSTR pwszTicket, // e.g. "from-PP = ..."
  224. OUT PDWORD pdwTicketLen,
  225. PBOOL pfKeepVerb, // if TRUE, no data will be copied into pwszUrl
  226. PWSTR pwszUrl, // user supplied buffer ...
  227. OUT PDWORD pdwUrlLen // ... and length (will be updated to actual length
  228. // on successful return)
  229. )
  230. {
  231. if (hPPLogon == 0)
  232. {
  233. DoTraceMessage(PP_LOG_ERROR, "PP_GetReturnVerbAndUrl() : hPPLogon is null");
  234. return FALSE;
  235. }
  236. LOGON* pLogon = reinterpret_cast<LOGON*>(hPPLogon);
  237. return pLogon->GetAuthorizationInfo(pwszTicket,
  238. pdwTicketLen,
  239. pfKeepVerb,
  240. pwszUrl,
  241. pdwUrlLen);
  242. }
  243. BOOL
  244. PP_GetChallengeContent(
  245. IN PP_LOGON_CONTEXT hPPLogon,
  246. IN PBYTE pContent,
  247. IN OUT PDWORD pdwContentLen
  248. )
  249. {
  250. if (hPPLogon == 0)
  251. {
  252. DoTraceMessage(PP_LOG_ERROR, "PP_GetChallengeContent() : hPPLogon is null");
  253. return FALSE;
  254. }
  255. LOGON* pLogon = reinterpret_cast<LOGON*>(hPPLogon);
  256. return pLogon->GetChallengeContent(pContent,
  257. pdwContentLen);
  258. }
  259. // -----------------------------------------------------------------------------
  260. VOID
  261. PP_Logout(
  262. IN PP_LOGON_CONTEXT hPPLogon,
  263. IN DWORD dwFlags
  264. )
  265. {
  266. // todo - flush passport cookies
  267. // set flag to ignore credmgr so we don't just auto-logon again
  268. g_fIgnoreCachedCredsForPassport = TRUE;
  269. // unset our login flag and username
  270. g_fCurrentProcessLoggedOn = FALSE;
  271. memset ( g_szUserNameLoggedOn, 0, INTERNET_MAX_USER_NAME_LENGTH*sizeof(WCHAR) );
  272. }
  273. BOOL
  274. PP_ForceNexusLookup(
  275. PP_LOGON_CONTEXT hPP,
  276. IN BOOL fForce,
  277. IN PWSTR pwszRegUrl, // user supplied buffer ...
  278. IN OUT PDWORD pdwRegUrlLen, // ... and length (will be updated to actual length
  279. // on successful return)
  280. IN PWSTR pwszDARealm, // user supplied buffer ...
  281. IN OUT PDWORD pdwDARealmLen // ... and length (will be updated to actual length
  282. // on successful return)
  283. )
  284. {
  285. SESSION* pSession = reinterpret_cast<SESSION*>(hPP);
  286. if ( pSession != NULL )
  287. {
  288. return pSession->GetDAInfoFromPPNexus(fForce,
  289. pwszRegUrl,
  290. pdwRegUrlLen,
  291. pwszDARealm,
  292. pdwDARealmLen);
  293. }
  294. else
  295. {
  296. return FALSE;
  297. }
  298. }
  299. #define PASSPORT_MAX_REALM_LENGTH 256
  300. // returns TRUE if it was found, with the value copied to pszRealm.
  301. // pszRealm is expected to be at least PASSPORT_MAX_REALM_LENGTH in length
  302. // returns FALSE if not found
  303. BOOL ReadPassportRealmFromRegistry (
  304. WCHAR* pszRealm
  305. )
  306. {
  307. BOOL retval = FALSE;
  308. HKEY key;
  309. if ( pszRealm == NULL )
  310. return FALSE;
  311. if ( RegOpenKeyExW(
  312. HKEY_LOCAL_MACHINE,
  313. L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Passport",
  314. 0,
  315. KEY_READ,
  316. &key) == ERROR_SUCCESS)
  317. {
  318. DWORD dwType;
  319. DWORD dwSize;
  320. dwSize = PASSPORT_MAX_REALM_LENGTH * sizeof(WCHAR);
  321. if ( RegQueryValueExW(
  322. key,
  323. L"LoginServerRealm",
  324. NULL,
  325. &dwType,
  326. (LPBYTE)(pszRealm),
  327. &dwSize) == ERROR_SUCCESS )
  328. {
  329. if ( wcslen(pszRealm) > 0 )
  330. retval = TRUE;
  331. else
  332. retval = FALSE;
  333. }
  334. else
  335. {
  336. retval = FALSE;
  337. pszRealm[0] = L'\0';
  338. }
  339. RegCloseKey(key);
  340. }
  341. return retval;
  342. }
  343. // if either pwszUsername or pwszPassword is not NULL, it must represent a string at least
  344. // INTERNET_MAX_USER_NAME_LENGTH or INTERNET_MAX_PASSWORD_LENGTH chars long, respectively
  345. BOOL
  346. PP_GetCachedCredential(
  347. PP_LOGON_CONTEXT hPP,
  348. IN PWSTR pwszRealm,
  349. IN PWSTR pwszTarget,
  350. OUT PWSTR pwszUsername,
  351. OUT PWSTR pwszPassword
  352. )
  353. {
  354. BOOL bRetVal = FALSE;
  355. SESSION* pSession = reinterpret_cast<SESSION*>(hPP);
  356. if ( pSession != NULL )
  357. {
  358. PCREDENTIALW* ppCreds;
  359. DWORD dwNumCreds;
  360. WCHAR szRealm[PASSPORT_MAX_REALM_LENGTH];
  361. WCHAR* pszRealm;
  362. if ( pwszRealm == NULL )
  363. {
  364. ReadPassportRealmFromRegistry ( szRealm );
  365. pszRealm = szRealm;
  366. }
  367. else
  368. {
  369. pszRealm = pwszRealm;
  370. }
  371. if (pSession->GetCachedCreds(pszRealm,
  372. pwszTarget,
  373. &ppCreds,
  374. &dwNumCreds) )
  375. {
  376. // look for the right cred
  377. WCHAR wPass[256];
  378. PCREDENTIALW pCredToUse = NULL;
  379. if (dwNumCreds > 0 && ppCreds[0] != NULL )
  380. {
  381. for ( DWORD idx = 0; idx < dwNumCreds; idx++ )
  382. {
  383. if ( ppCreds[idx]->Type == CRED_TYPE_DOMAIN_VISIBLE_PASSWORD )
  384. {
  385. // check to see if prompt bit is set. If set, keep looking, only use if
  386. // the prompt bit isn't set.
  387. if ( !(ppCreds[idx]->Flags & CRED_FLAGS_PROMPT_NOW) )
  388. {
  389. pCredToUse = ppCreds[idx];
  390. break;
  391. }
  392. }
  393. }
  394. }
  395. if (pCredToUse )
  396. {
  397. bRetVal = TRUE;
  398. DecryptPassword(wPass,
  399. PVOID(pCredToUse->CredentialBlob),
  400. pCredToUse->CredentialBlobSize);
  401. if ( pwszUsername != NULL )
  402. {
  403. wcsncpy ( pwszUsername, pCredToUse->UserName, INTERNET_MAX_USER_NAME_LENGTH-1 );
  404. }
  405. if ( pwszPassword != NULL )
  406. {
  407. wcsncpy ( pwszPassword, wPass, INTERNET_MAX_PASSWORD_LENGTH-1 );
  408. }
  409. }
  410. }
  411. }
  412. return bRetVal;
  413. }
  414. #ifdef PP_DEMO
  415. // -----------------------------------------------------------------------------
  416. BOOL PP_ContactPartner(
  417. PP_CONTEXT hPP,
  418. PCWSTR pwszPartnerUrl,
  419. PCWSTR pwszVerb,
  420. PCWSTR pwszHeaders,
  421. PWSTR pwszData,
  422. PDWORD pdwDataLength
  423. )
  424. {
  425. if (hPP == 0)
  426. {
  427. DoTraceMessage(PP_LOG_ERROR, "PP_ContactPartner() : hPPLogon is null");
  428. return FALSE;
  429. }
  430. SESSION* pSession = reinterpret_cast<SESSION*>(hPP);
  431. return pSession->ContactPartner(pwszPartnerUrl,
  432. pwszVerb,
  433. pwszHeaders,
  434. pwszData,
  435. pdwDataLength
  436. );
  437. }
  438. #endif // PP_DEMO