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.

2619 lines
64 KiB

  1. #include <process.h>
  2. #include <windows.h>
  3. //#include <winbase.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <time.h>
  7. #include <assert.h>
  8. #include <shlwapi.h>
  9. #include <conio.h>
  10. //#include <shlwapip.h>
  11. #include "resource.h"
  12. #include "lists.hxx"
  13. //#define DBG
  14. #define DEBUG
  15. #define _DEBUG
  16. #include "main.hxx"
  17. #define MAX_HANDLES 255
  18. ////////////////////////////////////////
  19. //
  20. // G L O B A L S
  21. //
  22. //
  23. ////////////////////////////////////////
  24. CSessionAttributeList * g_pSessionAttributeList = NULL;
  25. CSessionList * g_pSessionList = NULL;
  26. DWORD dwThreads = 0;
  27. int iNumIterations = 0;
  28. unsigned uSeed = 0;
  29. DWORD dwDebugLogCategory;
  30. DWORD dwConsoleLogCategory;
  31. DWORD dwDebugBreakCategory;
  32. DWORD dwUIDialogMode = MODE_ALL;
  33. BOOL fLogToDebugTerminal = FALSE;
  34. BOOL fLogToConsole = TRUE;
  35. BOOL fDebugBreak = FALSE;
  36. #define MALLOC( x ) malloc(sizeof(x))
  37. #define FIND_FUNCTION_KERNEL( x, y ) ( x##_P = (FN##x) GetProcAddress( hModule, #x ));
  38. #define FIND_FUNCTION( x , y ) ( x##_P = (FN##x) GetProcAddress( hModule, y ));
  39. #define IS_ARG(c) ( c == '-' )
  40. //
  41. // ACTION
  42. // A routine which takes a LPVOID as input,
  43. // performs some action
  44. // and returns another LPVOID
  45. //
  46. typedef LPVOID ( WINAPI * ACTION)(LPVOID);
  47. BOOL Test();
  48. ////////////////////////////////////////////////////////////////////////////////////
  49. //
  50. // D E C L A R A T I O N S
  51. //
  52. ////////////////////////////////////////////////////////////////////////////////////
  53. typedef enum {
  54. MODE_NONE = -1,
  55. MODE_BUSY,
  56. MODE_FREE,
  57. MODE_CAPTURED
  58. } MODE;
  59. /*
  60. CONTEXT_RECORD CAPTURED_CTX = { { 0xcabdcabd, 0xcabdcabd };
  61. CONTEXT_RECORD FREE_CTX = {{ 0, 0 };
  62. CONTEXT_RECORD BUSY_CTX = {{ 0xb00bb00b, 0xb00bb00b };
  63. */
  64. CredHandle CAPTURED_CRED = { 0xcabdcabd, 0xcabdcabd };
  65. CredHandle FREE_CRED = { 0, 0 };
  66. CredHandle BUSY_CRED = { 0xb00bb00b, 0xb00bb00b };
  67. typedef struct _CONTEXT_RECORD {
  68. // handle to the credential
  69. CredHandle hCred;
  70. // App ctx associated with this credential
  71. LPSTR szAppCtx;
  72. // User Ctx associated with this credential
  73. LPSTR szUserCtx;
  74. // Timestamp
  75. DWORD dwTickCount;
  76. // MODE
  77. MODE Mode;
  78. } CONTEXT_RECORD, * LPCONTEXT_RECORD;
  79. // forward declarations
  80. class CSessionAttribute;
  81. #define MAX_APP_CONTEXT_LENGTH 32
  82. #define MAX_USER_CONTEXT_LENGTH MAX_APP_CONTEXT_LENGTH
  83. typedef struct _CREDENTIAL_STRUCT {
  84. // username
  85. LPSTR szUserName;
  86. // password
  87. LPSTR szPassword;
  88. // realm
  89. LPSTR szRealm;
  90. } CREDENTIAL_STRUCT, *LPCREDENTIAL_STRUCT;
  91. typedef struct _HANDLE_RECORD {
  92. DWORD dwSignature;
  93. CONTEXT_RECORD hCredArray[MAX_HANDLES];
  94. int Count; // count of handles in use.
  95. CRITICAL_SECTION Lock;
  96. } HANDLE_RECORD, *LPHANDLE_RECORD;
  97. #define CTXHANDLE_ARRAY_SIGNATURE 'xtch' // 'hctx'
  98. #define IS_VALID_CTXHANDLE_ARRAY(x) { assert( x -> dwSignature == CTXHANDLE_ARRAY_SIGNATURE ); }
  99. #define IDENTITY_1 "Application_1"
  100. // bugbug: The values of the CredHandles in these structures should match
  101. // the corresponding *_CRED structure values.
  102. //
  103. #ifdef NEW_LOOKUP
  104. MODE ModeCaptured = MODE_CAPTURED;
  105. MODE ModeFree = MODE_FREE;
  106. MODE ModeBusy = MODE_BUSY;
  107. #define CAPTURED_CTX_REC ModeCaptured
  108. #define FREE_CTX_REC ModeFree
  109. #define BUSY_CTX_REC ModeBusy
  110. #else
  111. CONTEXT_RECORD CAPTURED_CTX_REC = {
  112. { 0xcabdcabd, 0xcabdcabd },
  113. NULL,
  114. NULL};
  115. CONTEXT_RECORD FREE_CTX_REC = {
  116. { 0, 0 },
  117. NULL,
  118. NULL};
  119. CONTEXT_RECORD BUSY_CTX_REC = {
  120. { 0xb00bb00b, 0xb00bb00b },
  121. NULL,
  122. NULL};
  123. #endif // ifdef NEW_LOOKUP
  124. BOOL operator==(const CredHandle op1, const CredHandle op2)
  125. {
  126. return (( op1.dwUpper == op2.dwUpper ) && ( op1.dwUpper == op2.dwUpper ));
  127. }
  128. BOOL operator!=(const CredHandle op1, const CredHandle op2)
  129. {
  130. return (( op1.dwUpper != op2.dwUpper ) || ( op1.dwUpper != op2.dwUpper ));
  131. }
  132. BOOL operator==(const CONTEXT_RECORD op1, const CONTEXT_RECORD op2)
  133. {
  134. // we only compare the CredHandle
  135. return (op1.hCred == op2.hCred );
  136. }
  137. BOOL operator!=(const CONTEXT_RECORD op1, const CONTEXT_RECORD op2)
  138. {
  139. // we only compare the CredHandle
  140. return (op1.hCred != op2.hCred );
  141. }
  142. typedef struct {
  143. // a string in the DWORD
  144. DWORD dwSignature;
  145. // handles to contexts
  146. HANDLE_RECORD * hCredentialHandles;
  147. // count of iterations
  148. int iCount;
  149. } CONTEXT_DATA, * LPCONTEXT_DATA;
  150. #define CONTEXT_SIGNATURE 'tnoc'
  151. #define IS_VALID_CONTEXT(s) { assert ( s -> dwSignature == CONTEXT_SIGNATURE ); }
  152. #define SET_CONTEXT_SIGNATURE(s) { s -> dwSignature = CONTEXT_SIGNATURE; }
  153. //
  154. // contexts passed to threads, and RegisterWaits() etc.
  155. //
  156. typedef struct _CALLBACK_CONTEXT {
  157. DWORD dwSignature;
  158. LPCONTEXT_DATA lpContext;
  159. LPHANDLE lpThreadHandle;
  160. LPHANDLE lpHandle;
  161. } CALLBACK_CONTEXT, * LPCALLBACK_CONTEXT;
  162. #define CALLBACK_CONTEXT_SIGNATURE 'kblc' // clbk
  163. #define IS_VALID_CALLBACK_CONTEXT(x) ( assert( s -> dwSignature ) == CALLBACK_CONTEXT_SIGNATURE )
  164. #ifdef NEW_LOOKUP
  165. LPCONTEXT_RECORD
  166. FindFreeSlot(
  167. HANDLE_RECORD * hArray,
  168. LPCONTEXT_RECORD hMode);
  169. #else
  170. LPCONTEXT_RECORD
  171. FindFreeSlot(
  172. HANDLE_RECORD * hArray,
  173. LPCONTEXT_RECORD hMode);
  174. #endif
  175. LPHANDLE_RECORD
  176. new_handle_record(DWORD dwSignature);
  177. LPCALLBACK_CONTEXT
  178. new_callback_context();
  179. LPVOID
  180. WINAPI fnAppLogon(
  181. LPVOID lpvData);
  182. LPVOID
  183. WINAPI fnAppLogonExclusive(
  184. LPVOID lpvData);
  185. LPVOID
  186. WINAPI fnAppLogonShared(
  187. LPVOID lpvData);
  188. LPVOID
  189. WINAPI fnAppLogoff(
  190. LPVOID lpvData);
  191. LPVOID
  192. WINAPI fnInit(
  193. LPVOID lpvData);
  194. LPVOID
  195. WINAPI fnPopulateCredentials(
  196. LPVOID lpvData);
  197. LPVOID
  198. WINAPI fnAuthChallenge(
  199. LPVOID lpvData);
  200. LPVOID
  201. WINAPI fnAuthChallengeAny(
  202. LPVOID lpvData);
  203. LPVOID
  204. WINAPI fnAuthChallengeUser(
  205. LPVOID lpvData);
  206. LPVOID
  207. WINAPI fnAuthChallengeUserPassword(
  208. LPVOID lpvData);
  209. LPVOID
  210. WINAPI fnUiPrompt(
  211. LPVOID lpvData);
  212. LPVOID
  213. WINAPI fnUiPromptAny(
  214. LPVOID lpvData);
  215. LPVOID
  216. WINAPI fnUiPromptUser(
  217. LPVOID lpvData);
  218. LPVOID
  219. WINAPI fnFlushCredentials(
  220. LPVOID lpvData);
  221. LPVOID
  222. WINAPI fnFlushCredentialsGlobal(
  223. LPVOID lpvData);
  224. LPVOID
  225. WINAPI fnFlushCredentialsSession(
  226. LPVOID lpvData);
  227. BOOL
  228. SetUIUserNameAndPassword(
  229. LPSTR szUsername,
  230. LPSTR szPassword,
  231. BOOL fPersist);
  232. //DWORD WINAPI fnRegisterWaitCallback( PVOID pvData );
  233. //
  234. // Enum Type for STATE
  235. //
  236. typedef enum _State
  237. {
  238. STATE_INVALID,
  239. STATE_NONE,
  240. STATE_INIT,
  241. STATE_APP_LOGON,
  242. STATE_APP_LOGON_EXCLUSIVE,
  243. STATE_APP_LOGON_SHARED,
  244. STATE_APP_LOGOFF,
  245. STATE_POPULATE_CREDENTIALS,
  246. STATE_AUTH_CHALLENGE,
  247. STATE_AUTH_CHALLENGE_ANY,
  248. STATE_AUTH_CHALLENGE_USER,
  249. STATE_AUTH_CHALLENGE_USER_PASS,
  250. STATE_PREAUTH_CHALLENGE_ANY,
  251. STATE_PREAUTH_CHALLENGE_USER,
  252. STATE_PREAUTH_CHALLENGE_USER_PASS,
  253. STATE_UI_PROMPT,
  254. STATE_UI_PROMPT_ANY,
  255. STATE_UI_PROMPT_USER,
  256. STATE_FLUSH_CREDENTIALS,
  257. STATE_FLUSH_CREDENTIALS_GLOBAL,
  258. STATE_FLUSH_CREDENTIALS_SESSION,
  259. STATE_NUKE_TRUSTED_HOSTS,
  260. STATE_STATISTICS,
  261. STATE_STALL,
  262. STATE_DONE
  263. } STATE;
  264. //
  265. // STATE Table definition
  266. //
  267. typedef struct _STATE_TABLE {
  268. //
  269. // The current state we are in
  270. //
  271. STATE CurrentState;
  272. //
  273. // THe next state which we will transition to
  274. //
  275. STATE NextState;
  276. //
  277. // Action ( function ) to be performed in the "CurrentState"
  278. //
  279. ACTION Action;
  280. //
  281. // prob of going from CurrentState to NextState if there
  282. // are two or more such transitions from the same state
  283. // present in the table
  284. //
  285. DWORD dwProbability;
  286. } STATE_TABLE, *LPSTATE_TABLE;
  287. STATE_TABLE TRANSITION_TABLE[] =
  288. {
  289. // transitions out of STATE_INIT
  290. {
  291. STATE_INIT, STATE_APP_LOGON,
  292. fnInit,
  293. 50
  294. },
  295. // transitions out of STATE_INIT
  296. {
  297. STATE_INIT, STATE_FLUSH_CREDENTIALS,
  298. fnInit,
  299. 100
  300. },
  301. // transitions out of STATE_APP_LOGON
  302. {
  303. STATE_APP_LOGON, STATE_UI_PROMPT,
  304. fnAppLogon,
  305. 60
  306. },
  307. {
  308. STATE_APP_LOGON, STATE_POPULATE_CREDENTIALS,
  309. fnAppLogon,
  310. 70
  311. },
  312. {
  313. STATE_APP_LOGON, STATE_AUTH_CHALLENGE,
  314. fnAppLogon,
  315. 100
  316. },
  317. // transitions out of STATE_POPULATE_CREDENTIALS
  318. {
  319. STATE_POPULATE_CREDENTIALS, STATE_INIT,
  320. fnPopulateCredentials,
  321. 30
  322. },
  323. {
  324. STATE_POPULATE_CREDENTIALS, STATE_APP_LOGOFF,
  325. fnPopulateCredentials,
  326. 60
  327. },
  328. {
  329. STATE_POPULATE_CREDENTIALS, STATE_UI_PROMPT,
  330. fnPopulateCredentials,
  331. 100
  332. },
  333. // transitions out of STATE_AUTH_CHALLENGE
  334. {
  335. STATE_AUTH_CHALLENGE, STATE_APP_LOGON,
  336. fnAuthChallenge,
  337. 100
  338. },
  339. // transitions out of STATE_UI_PROMPT
  340. {
  341. STATE_UI_PROMPT, STATE_INIT,
  342. fnUiPrompt,
  343. 100
  344. },
  345. // transitions out of STATE_FLUSH_CREDENTIALS
  346. {
  347. STATE_FLUSH_CREDENTIALS, STATE_APP_LOGON,
  348. fnFlushCredentials,
  349. 100
  350. },
  351. // transitions out of STATE_APP_LOGOFF
  352. {
  353. STATE_APP_LOGOFF, STATE_APP_LOGON,
  354. fnAppLogoff,
  355. 100
  356. },
  357. // transitions out of STATE_INVALID
  358. {
  359. STATE_INVALID, STATE_INVALID,
  360. NULL,
  361. 100
  362. },
  363. // transitions out of STATE_DONE
  364. {
  365. STATE_DONE, STATE_INVALID,
  366. NULL,
  367. 100
  368. }
  369. };
  370. STATE_TABLE APP_LOGON_TRANSITION_TABLE[] =
  371. {
  372. // transitions out of STATE_INIT
  373. {
  374. STATE_INIT, STATE_APP_LOGON_SHARED,
  375. fnInit,
  376. 50
  377. },
  378. {
  379. STATE_INIT, STATE_APP_LOGON_EXCLUSIVE,
  380. fnInit,
  381. 100
  382. },
  383. // transitions out of STATE_APP_LOGON_EXCLUSIVE
  384. {
  385. STATE_APP_LOGON_EXCLUSIVE, STATE_DONE,
  386. fnAppLogonExclusive,
  387. 50
  388. },
  389. {
  390. STATE_APP_LOGON_SHARED, STATE_DONE,
  391. fnAppLogonShared,
  392. 100
  393. },
  394. // transitions out of STATE_DONE
  395. {
  396. STATE_DONE, STATE_DONE,
  397. NULL,
  398. 100
  399. }
  400. };
  401. STATE_TABLE AUTH_CHALLENGE_TRANSITION_TABLE[] =
  402. {
  403. // transitions out of STATE_INIT
  404. {
  405. STATE_INIT, STATE_AUTH_CHALLENGE_ANY,
  406. fnInit,
  407. 30
  408. },
  409. {
  410. STATE_INIT, STATE_AUTH_CHALLENGE_USER,
  411. fnInit,
  412. 70
  413. },
  414. {
  415. STATE_INIT, STATE_AUTH_CHALLENGE_USER_PASS,
  416. fnInit,
  417. 100
  418. },
  419. // transitions out of STATE_AUTH_CHALLENGE_ANY
  420. {
  421. STATE_AUTH_CHALLENGE_ANY, STATE_DONE,
  422. fnAuthChallengeAny,
  423. 50
  424. },
  425. // transitions out of STATE_AUTH_CHALLENGE_USER
  426. {
  427. STATE_AUTH_CHALLENGE_USER, STATE_DONE,
  428. fnAuthChallengeUser,
  429. 100
  430. },
  431. // transitions out of STATE_AUTH_CHALLENGE_USER_PASS
  432. {
  433. STATE_AUTH_CHALLENGE_USER_PASS, STATE_DONE,
  434. fnAuthChallengeUserPassword,
  435. 100
  436. },
  437. // transitions out of STATE_DONE
  438. {
  439. STATE_DONE, STATE_DONE,
  440. NULL,
  441. 100
  442. }
  443. };
  444. STATE_TABLE UI_PROMPT_TRANSITION_TABLE[] =
  445. {
  446. // transitions out of STATE_INIT
  447. {
  448. STATE_INIT, STATE_UI_PROMPT_ANY,
  449. fnInit,
  450. 50
  451. },
  452. {
  453. STATE_INIT, STATE_UI_PROMPT_USER,
  454. fnInit,
  455. 100
  456. },
  457. // transitions out of STATE_UI_PROMPT_ANY
  458. {
  459. STATE_UI_PROMPT_ANY, STATE_DONE,
  460. fnUiPromptAny,
  461. 100
  462. },
  463. // transitions out of STATE_UI_PROMPT_USER
  464. {
  465. STATE_UI_PROMPT_USER, STATE_DONE,
  466. fnUiPromptUser,
  467. 100
  468. },
  469. // transitions out of STATE_DONE
  470. {
  471. STATE_DONE, STATE_DONE,
  472. NULL,
  473. 100
  474. }
  475. };
  476. STATE_TABLE FLUSH_CREDENTIALS_TRANSITION_TABLE[] =
  477. {
  478. // transitions out of STATE_INIT
  479. {
  480. STATE_INIT, STATE_FLUSH_CREDENTIALS_SESSION,
  481. fnInit,
  482. 50
  483. },
  484. {
  485. STATE_INIT, STATE_FLUSH_CREDENTIALS_GLOBAL,
  486. fnInit,
  487. 100
  488. },
  489. // transitions out of STATE_FLUSH_CREDENTIALS_SESSION
  490. {
  491. STATE_FLUSH_CREDENTIALS_SESSION, STATE_DONE,
  492. fnFlushCredentialsSession,
  493. 100
  494. },
  495. // transitions out of STATE_FLUSH_CREDENTIALS_GLOBAL
  496. {
  497. STATE_FLUSH_CREDENTIALS_GLOBAL, STATE_DONE,
  498. fnFlushCredentialsGlobal,
  499. 100
  500. },
  501. // transitions out of STATE_DONE
  502. {
  503. STATE_DONE, STATE_DONE,
  504. NULL,
  505. 100
  506. }
  507. };
  508. VOID WINAPI fnRegisterWaitCallback(
  509. PVOID pvData,
  510. BOOLEAN fAlertable);
  511. VOID WINAPI fnTimerCallback(
  512. PVOID pvData,
  513. BOOLEAN fAlertable);
  514. LPCONTEXT_DATA
  515. new_context();
  516. DWORD
  517. TuringMachine(
  518. STATE_TABLE StateTable[],
  519. STATE InitialState,
  520. LPVOID lpvData);
  521. //WAITORTIMERCALLBACKFUNC fnRegisterWaitCallback;
  522. //extern HANDLE RegisterWaitForSingleObject( HANDLE, WAITORTIMERCALLBACKFUNC, PVOID, DWORD);
  523. STATE
  524. NEXT_STATE( STATE_TABLE Table[], STATE CurrentState );
  525. ACTION
  526. GET_STATE_ACTION( STATE_TABLE Table[], STATE CurrentState );
  527. LPSTR
  528. MapState( STATE State );
  529. void
  530. usage(void);
  531. int __cdecl _sprintf( char * buffer, char * format, va_list );
  532. LPVOID
  533. WINAPI DefaultAction(
  534. LPVOID lpvData);
  535. #ifdef NEW_LOOKUP
  536. LPCONTEXT_RECORD
  537. FindFreeSlot( HANDLE_RECORD * hArray, MODE * Mode )
  538. {
  539. // hMode is for doing a context-sensitive search
  540. //
  541. // If hMode == FREE_CTX,
  542. // begin
  543. // find a free-slot;
  544. // mark it busy
  545. // return the slot;
  546. // end
  547. // else
  548. // if hMode == BUSY_CTX
  549. // begin
  550. // find a busy-slot
  551. // return slot
  552. // end
  553. // else
  554. // /* this means that a search is being requested */
  555. // find a record corresponding to hMode
  556. // return it
  557. //
  558. int i;
  559. HANDLE hTemp = NULL, hOrig = NULL;
  560. LPCONTEXT_RECORD phRet = NULL;
  561. int Cnt=0;
  562. dprintf( ENTRY_EXIT, "Enter: FindFreeSlot( %#X, %s )\n",
  563. hArray,
  564. ((*Mode == MODE_FREE)
  565. ?"FREE"
  566. :((*Mode == MODE_BUSY)
  567. ?"BUSY"
  568. :"CAPTURED")));
  569. EnterCriticalSection( &hArray -> Lock );
  570. for( i = 0; (i < MAX_HANDLES) && (Cnt <= hArray -> Count); i ++, Cnt++ ) {
  571. if( // requesting a free slot
  572. (
  573. ( *Mode == MODE_FREE )
  574. && ( hArray -> hCredArray[i].Mode == MODE_FREE )
  575. )
  576. || // requesting any slot having valid credentials
  577. (
  578. ( *Mode == MODE_BUSY )
  579. && ( hArray -> hCredArray[i].Mode == MODE_BUSY )
  580. //&& ( hArray -> hCredArray[i].Mode != MODE_FREE )
  581. //&& ( hArray -> hCredArray[i].Mode != MODE_CAPTURED )
  582. )
  583. //|| // doing a context sensitive search
  584. // ( // bugbug: what happens when szAppCtx stored is zero ?
  585. // //( hArray -> hCredArray[i].Mode != MODE_FREE )
  586. // ( hArray -> hCredArray[i].Mode == MODE_BUSY )
  587. // && ( hMode -> szAppCtx && *hMode -> szAppCtx )
  588. // && !strcmp( hArray -> hCredArray[i].szAppCtx, hMode -> szAppCtx )
  589. // )
  590. ) {
  591. // capture the handle if the handle requested is a free handle
  592. if( *Mode == MODE_FREE )
  593. hArray -> hCredArray[i].Mode = MODE_CAPTURED;
  594. phRet = &hArray -> hCredArray[i];
  595. break;
  596. }
  597. }
  598. LeaveCriticalSection( &hArray -> Lock );
  599. if(( i == MAX_HANDLES ) || (Cnt > hArray -> Count) )
  600. phRet = NULL;
  601. else {
  602. ++ hArray -> Count;
  603. }
  604. if( phRet != NULL ) {
  605. dprintf( ENTRY_EXIT, "Exit: FindFreeSlot returns [%#x,%#x]\n",
  606. phRet->hCred.dwUpper,
  607. phRet->hCred.dwLower);
  608. } else {
  609. dprintf( ENTRY_EXIT, "Exit: FindFreeSlot returns %#x\n",phRet);
  610. }
  611. return phRet;
  612. }
  613. #else
  614. LPCONTEXT_RECORD
  615. FindFreeSlot( HANDLE_RECORD * hArray, LPCONTEXT_RECORD hMode )
  616. {
  617. // hMode is for doing a context-sensitive search
  618. //
  619. // If hMode == FREE_CTX,
  620. // begin
  621. // find a free-slot;
  622. // mark it busy
  623. // return the slot;
  624. // end
  625. // else
  626. // if hMode == BUSY_CTX
  627. // begin
  628. // find a busy-slot
  629. // return slot
  630. // end
  631. // else
  632. // /* this means that a search is being requested */
  633. // find a record corresponding to hMode
  634. // return it
  635. //
  636. int i;
  637. HANDLE hTemp = NULL, hOrig = NULL;
  638. LPCONTEXT_RECORD phRet = NULL;
  639. int Cnt=0;
  640. dprintf( ENTRY_EXIT, "Enter: FindFreeSlot( %#X, %#X )\n",hArray, hMode );
  641. EnterCriticalSection( &hArray -> Lock );
  642. for( i = 0; (i < MAX_HANDLES) && (Cnt <= hArray -> Count); i ++, Cnt++ ) {
  643. if( // requesting a free slot
  644. (
  645. ( hMode -> hCred == FREE_CRED )
  646. && ( hArray -> hCredArray[i].hCred == hMode -> hCred )
  647. )
  648. || // requesting any slot having valid credentials
  649. (
  650. ( hMode -> hCred == BUSY_CRED )
  651. && ( hArray -> hCredArray[i].hCred != FREE_CRED )
  652. && ( hArray -> hCredArray[i].hCred != CAPTURED_CRED )
  653. )
  654. || // doing a context sensitive search
  655. ( // bugbug: what happens when szAppCtx stored is zero ?
  656. ( hArray -> hCredArray[i].hCred != FREE_CRED )
  657. && ( hMode -> szAppCtx && *hMode -> szAppCtx )
  658. && !strcmp( hArray -> hCredArray[i].szAppCtx, hMode -> szAppCtx )
  659. )
  660. ) {
  661. // capture the handle if the handle requested is a free handle
  662. if( hMode->hCred == FREE_CRED )
  663. hArray -> hCredArray[i].hCred = CAPTURED_CRED;
  664. phRet = &hArray -> hCredArray[i];
  665. break;
  666. }
  667. }
  668. LeaveCriticalSection( &hArray -> Lock );
  669. if(( i == MAX_HANDLES ) || (Cnt > hArray -> Count) )
  670. phRet = NULL;
  671. else {
  672. ++ hArray -> Count;
  673. }
  674. if( phRet != NULL ) {
  675. dprintf( ENTRY_EXIT, "Exit: FindFreeSlot returns %#x(%#x)\n",phRet,*phRet);
  676. } else {
  677. dprintf( ENTRY_EXIT, "Exit: FindFreeSlot returns %#x\n",phRet);
  678. }
  679. return phRet;
  680. }
  681. #endif
  682. int __cdecl dprintf(DWORD dwCategory, char * format, ...) {
  683. va_list args;
  684. char buf[1024];
  685. char * ptr = buf;
  686. DWORD dwThreadId = GetCurrentThreadId();
  687. int n;
  688. ptr += sprintf(buf,"< %d:%#x > ", uSeed, dwThreadId );
  689. va_start(args, format);
  690. n = vsprintf(ptr, format, args);
  691. va_end(args);
  692. if(
  693. (fLogToDebugTerminal )
  694. && (dwCategory >= dwDebugLogCategory)
  695. )
  696. OutputDebugString(buf);
  697. if(
  698. ( fLogToConsole)
  699. && ( dwCategory >= dwConsoleLogCategory)
  700. )
  701. printf("%s", buf );
  702. if(
  703. fDebugBreak
  704. && ( dwCategory >= dwDebugBreakCategory )
  705. ) {
  706. DebugBreak();
  707. }
  708. return n;
  709. }
  710. void
  711. usage()
  712. {
  713. dprintf( INFO, "thrdtest \n"
  714. " -n<number-of-iterations> \n"
  715. " -s: Directly Load the DLL \n"
  716. " -d<Level>: What to log to debug terminal (default: NO logging)\n"
  717. " -c<Level>: What to log to console (Default: INFO)\n"
  718. " <Level>: INFO %d\n"
  719. " ENTRY_EXIT %d\n"
  720. " STATISTICS %d\n"
  721. " API %d\n"
  722. " ERROR %d\n"
  723. " FATAL %d\n",
  724. INFO,
  725. ENTRY_EXIT,
  726. STATISTICS,
  727. API,
  728. ERROR,
  729. FATAL
  730. );
  731. exit(0);
  732. }
  733. LPVOID
  734. WINAPI fnEndMonkey(
  735. LPVOID lpvData)
  736. {
  737. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  738. dprintf( ENTRY_EXIT, "ENTER: fnEndMonkey : %X\n", lpvData );
  739. dprintf( INFO, "\n\n\n Statistics ...................................\n");
  740. dprintf( ENTRY_EXIT, "EXIT: fnEndMonkey : %X\n", lpvData );
  741. return lpvData;
  742. }
  743. LPCONTEXT_DATA
  744. new_context()
  745. {
  746. LPCONTEXT_DATA lpContext;
  747. lpContext = (LPCONTEXT_DATA) MALLOC( CONTEXT_DATA );
  748. if( !lpContext ) {
  749. dprintf( ERROR, "Error allocating context \n");
  750. exit(0);
  751. }
  752. ZeroMemory( lpContext, sizeof(CONTEXT_DATA) );
  753. lpContext -> dwSignature = CONTEXT_SIGNATURE;
  754. return lpContext;
  755. }
  756. LPHANDLE_RECORD
  757. new_handle_record(DWORD dwSignature)
  758. {
  759. LPHANDLE_RECORD lpContext;
  760. dprintf( ENTRY_EXIT, "Enter: new_handle_record \n");
  761. lpContext = (LPHANDLE_RECORD) MALLOC( HANDLE_RECORD );
  762. if( !lpContext ) {
  763. dprintf( ERROR, "Error allocating handle record \n");
  764. exit(0);
  765. }
  766. ZeroMemory( lpContext, sizeof(HANDLE_RECORD) );
  767. for(int i=0; i < MAX_HANDLES; i++ ) {
  768. lpContext->hCredArray[i].Mode = MODE_FREE;
  769. lpContext->hCredArray[i].dwTickCount = 0;
  770. lpContext->hCredArray[i].hCred.dwUpper = 0;
  771. lpContext->hCredArray[i].hCred.dwLower = 0;
  772. lpContext->hCredArray[i].szAppCtx = NULL;
  773. lpContext->hCredArray[i].szUserCtx = NULL;
  774. }
  775. lpContext -> dwSignature = dwSignature;
  776. InitializeCriticalSection( &lpContext->Lock);
  777. //lpContext -> dwSignature = CONTEXT_SIGNATURE;
  778. dprintf( ENTRY_EXIT, "Exit: new_handle_record \n");
  779. return lpContext;
  780. }
  781. LPCALLBACK_CONTEXT
  782. new_callback_context()
  783. {
  784. LPCALLBACK_CONTEXT lpContext;
  785. dprintf( ENTRY_EXIT, "Enter: new_callback_context \n");
  786. lpContext = (LPCALLBACK_CONTEXT) MALLOC( CALLBACK_CONTEXT );
  787. if( !lpContext ) {
  788. dprintf( ERROR, "Error allocating callback context \n");
  789. exit(0);
  790. }
  791. ZeroMemory( lpContext, sizeof(CALLBACK_CONTEXT) );
  792. lpContext -> dwSignature = CALLBACK_CONTEXT_SIGNATURE;
  793. dprintf( ENTRY_EXIT, "Exit: new_callback_context \n");
  794. return lpContext;
  795. }
  796. int
  797. __cdecl main( int ac, char * av[] )
  798. {
  799. DWORD dwError;
  800. LPCONTEXT_DATA lpContext;
  801. //ZeroMemory( (LPVOID)&_Context, sizeof(CONTEXT_DATA) );
  802. HMODULE hModule = NULL;
  803. BOOL fExpectingIterations = FALSE;
  804. BOOL fUseDigestDllOnly = FALSE;
  805. BOOL fExpectingSeedValue = FALSE;
  806. BOOL fTest = FALSE;
  807. dwDebugLogCategory = ERROR;
  808. dwConsoleLogCategory = INFO;
  809. uSeed = (unsigned )time(NULL) ;
  810. for( ac--, av++; ac; ac--, av++) {
  811. if(IS_ARG(**av)) {
  812. switch(*++*av) {
  813. case 'n' :
  814. if( *++*av) {
  815. iNumIterations = atoi(*av);
  816. } else
  817. fExpectingIterations = TRUE;
  818. break;
  819. case 's' :
  820. fUseDigestDllOnly = TRUE;
  821. break;
  822. case 'd' :
  823. fLogToDebugTerminal = TRUE;
  824. if( *++*av) {
  825. dwDebugLogCategory = (DWORD)atoi(*av);
  826. } else
  827. dwDebugLogCategory = ERROR;
  828. break;
  829. case 'c' :
  830. if( *++*av) {
  831. dwConsoleLogCategory = (DWORD)atoi(*av);
  832. } else
  833. dwConsoleLogCategory = INFO;
  834. break;
  835. case 'b' :
  836. fDebugBreak = TRUE;
  837. if( *++*av) {
  838. dwDebugBreakCategory = (DWORD)atoi(*av);
  839. } else
  840. dwDebugBreakCategory = ERROR;
  841. break;
  842. case 'r' :
  843. if( *++*av) {
  844. uSeed = atol(*av);
  845. } else
  846. fExpectingSeedValue = TRUE;
  847. break;
  848. case 't' :
  849. fTest = TRUE;
  850. break;
  851. case 'i' :
  852. dwUIDialogMode = MODE_IE_ONLY;
  853. break;
  854. case '?':
  855. case 'h':
  856. default:
  857. usage();
  858. exit(0);
  859. break;
  860. } // switch
  861. } else {
  862. if( fExpectingIterations ) {
  863. if( *av ) {
  864. iNumIterations = atoi(*av);
  865. fExpectingIterations = FALSE;
  866. }
  867. } else
  868. if( fExpectingSeedValue ) {
  869. if( *av ) {
  870. uSeed = atol(*av);
  871. fExpectingSeedValue = FALSE;
  872. }
  873. } else
  874. usage();
  875. } // if IS_ARG
  876. } // for
  877. if( fExpectingIterations )
  878. iNumIterations = -1; // infinite
  879. dprintf( INFO, "Monkey Circus Starts ... \n");
  880. // Get (global) dispatch table.
  881. InitializeSecurityInterface(fUseDigestDllOnly );
  882. // Check to see if we have digest.
  883. if (!HaveDigest())
  884. {
  885. goto cleanup;
  886. }
  887. if( fTest ) {
  888. Test();
  889. goto cleanup;
  890. }
  891. //
  892. // initialize global session lists
  893. //
  894. #ifdef AI
  895. g_pSessionAttributeList = new CSessionAttributeList();
  896. g_pSessionList = new CSessionList();
  897. #endif
  898. lpContext = new_context();
  899. lpContext -> hCredentialHandles = new_handle_record( CTXHANDLE_ARRAY_SIGNATURE );
  900. srand( uSeed );
  901. dwError = TuringMachine(
  902. TRANSITION_TABLE,
  903. STATE_INIT,
  904. (LPVOID) lpContext
  905. );
  906. cleanup:
  907. dprintf( INFO, "Monkey circus ending ...\n");
  908. if( hModule )
  909. FreeLibrary( hModule );
  910. return 0;
  911. }
  912. DWORD
  913. TuringMachine(
  914. STATE_TABLE StateTable[],
  915. STATE InitialState,
  916. LPVOID lpvData)
  917. {
  918. LPCONTEXT_DATA lpContext = ( LPCONTEXT_DATA ) lpvData;
  919. BOOL fDone = FALSE;
  920. STATE CurrentState;
  921. STATE NextState;
  922. ACTION Action;
  923. LPVOID lpNewContext;
  924. CurrentState = InitialState;
  925. #define MAP_STATE(s) MapState(s)
  926. while(
  927. ( !fDone )
  928. && ( lpContext -> iCount != iNumIterations)
  929. ) {
  930. //fnStatistics( lpvData );
  931. NextState = NEXT_STATE( StateTable, CurrentState );
  932. #ifdef DEBUG
  933. dprintf( INFO, "Current State : %s, Next : %s\n", MAP_STATE( CurrentState ), MAP_STATE( NextState ) );
  934. #endif
  935. // increment the count of iterations thru the monkey
  936. ++ lpContext -> iCount;
  937. switch( CurrentState ) {
  938. case STATE_INIT :
  939. case STATE_STATISTICS:
  940. case STATE_STALL:
  941. case STATE_APP_LOGON:
  942. case STATE_APP_LOGON_EXCLUSIVE:
  943. case STATE_APP_LOGON_SHARED:
  944. case STATE_APP_LOGOFF:
  945. case STATE_POPULATE_CREDENTIALS:
  946. case STATE_AUTH_CHALLENGE:
  947. case STATE_AUTH_CHALLENGE_ANY:
  948. case STATE_AUTH_CHALLENGE_USER:
  949. case STATE_AUTH_CHALLENGE_USER_PASS:
  950. case STATE_PREAUTH_CHALLENGE_ANY:
  951. case STATE_PREAUTH_CHALLENGE_USER:
  952. case STATE_PREAUTH_CHALLENGE_USER_PASS:
  953. case STATE_UI_PROMPT:
  954. case STATE_UI_PROMPT_ANY:
  955. case STATE_UI_PROMPT_USER:
  956. case STATE_FLUSH_CREDENTIALS:
  957. case STATE_FLUSH_CREDENTIALS_GLOBAL:
  958. case STATE_FLUSH_CREDENTIALS_SESSION:
  959. Action = GET_STATE_ACTION( StateTable, CurrentState );
  960. lpNewContext = (LPVOID) Action((LPVOID)lpContext);
  961. break;
  962. case STATE_INVALID :
  963. case STATE_DONE :
  964. fDone = TRUE;
  965. goto finish;
  966. break;
  967. default:
  968. dprintf( INFO, "BUGBUG: Reached default state \n");
  969. break;
  970. //break;
  971. ;
  972. }
  973. CurrentState = NextState;
  974. NextState = STATE_INVALID;
  975. }
  976. //scanf("%d",&i);
  977. finish:
  978. return ERROR_SUCCESS;
  979. }
  980. STATE
  981. NEXT_STATE( STATE_TABLE Table[], STATE CurrentState )
  982. {
  983. STATE NextState = STATE_INVALID;
  984. int i;
  985. DWORD dwRand,
  986. dwPreviousStateProbability = 0,
  987. dwProbability = 0;
  988. BOOL fFound = FALSE;
  989. // first generate a random number between 0 & 100 ( 0 .. 99 )
  990. i = (int)(rand() % 100);
  991. dwRand = (DWORD) i;
  992. #ifdef _DEBUGG
  993. for( i=0; Table[i].Action; i++ ) {
  994. dprintf( INFO, "--- \t %s %s %X %d\n",
  995. MAP_STATE( Table[i].CurrentState ),
  996. MAP_STATE( Table[i].NextState ),
  997. Table[i].Action,
  998. Table[i].dwProbability );
  999. }
  1000. #endif
  1001. //
  1002. // BUGBUG: We assume that the transition table entries are ordered in the ascending order of probabilities
  1003. for( i = 0; Table[i].Action; i++ ) {
  1004. if( Table[i].CurrentState != CurrentState )
  1005. continue;
  1006. dwProbability = Table[i].dwProbability;
  1007. NextState = Table[i].NextState;
  1008. #ifdef _DEBUGG
  1009. dprintf( INFO, "RAND: %d CurrentState: %s Considering Next State %s, prob %d\n",
  1010. dwRand, MAP_STATE( CurrentState ), MAP_STATE( NextState ), Table[i].dwProbability );
  1011. #endif
  1012. if(
  1013. ( Table[i].CurrentState == CurrentState )
  1014. && (
  1015. ( Table[i].dwProbability == 100 )
  1016. || (
  1017. ( dwRand <= Table[i].dwProbability )
  1018. && ( dwRand > dwPreviousStateProbability )
  1019. )
  1020. )
  1021. ) {
  1022. fFound = TRUE;
  1023. #ifdef _DEBUGG
  1024. dprintf( INFO, ">> RAND: %d Selected Next State %s, prob %d\n",
  1025. dwRand, MAP_STATE( NextState ), Table[i].dwProbability );
  1026. #endif
  1027. break;
  1028. }
  1029. dwPreviousStateProbability = Table[i].dwProbability;
  1030. }
  1031. return fFound?NextState:STATE_INVALID;
  1032. }
  1033. ACTION
  1034. GET_STATE_ACTION( STATE_TABLE Table[], STATE CurrentState )
  1035. {
  1036. STATE NextState = STATE_INVALID;
  1037. ACTION Action = DefaultAction;
  1038. int i;
  1039. for( i = 0; Table[i].Action; i++ ) {
  1040. if( Table[i].CurrentState == CurrentState )
  1041. Action = Table[i].Action;
  1042. }
  1043. return Action;
  1044. }
  1045. LPSTR
  1046. MapState( STATE State )
  1047. {
  1048. #define MAP_STRING( x ) case x : return #x; break;
  1049. switch( State )
  1050. {
  1051. MAP_STRING( STATE_INVALID )
  1052. MAP_STRING( STATE_NONE )
  1053. MAP_STRING( STATE_INIT )
  1054. MAP_STRING( STATE_STATISTICS )
  1055. MAP_STRING( STATE_STALL )
  1056. MAP_STRING( STATE_DONE )
  1057. MAP_STRING( STATE_APP_LOGON )
  1058. MAP_STRING( STATE_APP_LOGON_EXCLUSIVE )
  1059. MAP_STRING( STATE_APP_LOGON_SHARED )
  1060. MAP_STRING( STATE_APP_LOGOFF )
  1061. MAP_STRING( STATE_POPULATE_CREDENTIALS )
  1062. MAP_STRING( STATE_AUTH_CHALLENGE )
  1063. MAP_STRING( STATE_AUTH_CHALLENGE_ANY )
  1064. MAP_STRING( STATE_AUTH_CHALLENGE_USER )
  1065. MAP_STRING( STATE_AUTH_CHALLENGE_USER_PASS )
  1066. MAP_STRING( STATE_PREAUTH_CHALLENGE_ANY )
  1067. MAP_STRING( STATE_PREAUTH_CHALLENGE_USER )
  1068. MAP_STRING( STATE_PREAUTH_CHALLENGE_USER_PASS )
  1069. MAP_STRING( STATE_UI_PROMPT )
  1070. MAP_STRING( STATE_UI_PROMPT_USER )
  1071. MAP_STRING( STATE_UI_PROMPT_ANY )
  1072. MAP_STRING( STATE_FLUSH_CREDENTIALS )
  1073. MAP_STRING( STATE_FLUSH_CREDENTIALS_GLOBAL )
  1074. MAP_STRING( STATE_FLUSH_CREDENTIALS_SESSION )
  1075. default:
  1076. return "???";
  1077. break;
  1078. }
  1079. }
  1080. LPVOID
  1081. WINAPI DefaultAction(
  1082. LPVOID lpvData)
  1083. {
  1084. dprintf( ENTRY_EXIT, "DefaultAction : %X\n", lpvData );
  1085. return lpvData;
  1086. }
  1087. LPVOID
  1088. WINAPI fnInit(
  1089. LPVOID lpvData)
  1090. {
  1091. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1092. dprintf( ENTRY_EXIT, "Enter: fnInit %#x \n", lpvData );
  1093. dprintf( ENTRY_EXIT, "Exit: fnInit %#x \n", lpvData );
  1094. return lpvData;
  1095. }
  1096. LPVOID
  1097. WINAPI fnAppLogoff(
  1098. LPVOID lpvData)
  1099. {
  1100. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1101. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1102. DWORD dwError = ERROR_SUCCESS;
  1103. SECURITY_STATUS ss;
  1104. dprintf( ENTRY_EXIT, "Enter: fnAppLogoff %#x \n", lpvData );
  1105. lpCtxRecord = FindFreeSlot( lpContext -> hCredentialHandles, &BUSY_CTX_REC );
  1106. if( lpCtxRecord != NULL ) {
  1107. ss = LogoffOfDigestPkg(&lpCtxRecord -> hCred);
  1108. if(!SEC_SUCCESS(ss) ) {
  1109. dprintf(ERROR,"FreeCredentialHandle failed %s\n",
  1110. issperr2str(ss));
  1111. }
  1112. lpCtxRecord -> hCred = FREE_CRED;
  1113. lpCtxRecord ->szAppCtx = NULL;
  1114. lpCtxRecord ->szUserCtx = NULL;
  1115. lpCtxRecord->Mode = MODE_FREE;
  1116. }
  1117. dprintf( ENTRY_EXIT, "Exit: fnAppLogoff %#x \n", lpvData );
  1118. return lpvData;
  1119. }
  1120. LPVOID
  1121. WINAPI fnAppLogon(
  1122. LPVOID lpvData)
  1123. {
  1124. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1125. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1126. DWORD dwError = ERROR_SUCCESS;
  1127. dprintf( ENTRY_EXIT, "Enter: fnAppLogon %#x \n", lpvData );
  1128. dwError = TuringMachine(
  1129. APP_LOGON_TRANSITION_TABLE,
  1130. STATE_INIT,
  1131. lpvData
  1132. );
  1133. dprintf( ENTRY_EXIT, "Exit: fnAppLogon %#x \n", lpvData );
  1134. return lpvData;
  1135. }
  1136. LPVOID
  1137. WINAPI fnAppLogonExclusive(
  1138. LPVOID lpvData)
  1139. {
  1140. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1141. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1142. SECURITY_STATUS ss;
  1143. dprintf( ENTRY_EXIT, "Enter: fnAppLogonExclusive %#x \n", lpvData );
  1144. lpCtxRecord = FindFreeSlot( lpContext -> hCredentialHandles, &FREE_CTX_REC );
  1145. if( lpCtxRecord != NULL ) {
  1146. lpCtxRecord -> szAppCtx = NULL;
  1147. lpCtxRecord -> szUserCtx = IDENTITY_1;
  1148. //
  1149. // BUGBUG: Need to ensure new usernames every time
  1150. //
  1151. ss = LogonToDigestPkg(NULL, IDENTITY_1, &lpCtxRecord -> hCred);
  1152. if(!SEC_SUCCESS(ss) ) {
  1153. dprintf(ERROR,"AcquireCredentialHandle(%s,%s) failed (%s)\n",
  1154. lpCtxRecord -> szAppCtx,
  1155. lpCtxRecord -> szUserCtx,
  1156. issperr2str(ss));
  1157. //
  1158. // Since we failed, Release the slot
  1159. //
  1160. lpCtxRecord->Mode = MODE_FREE;
  1161. } else {
  1162. lpCtxRecord->Mode = MODE_BUSY;
  1163. }
  1164. }
  1165. dprintf( ENTRY_EXIT, "Exit: fnAppLogonExclusive %#x \n", lpvData );
  1166. return lpvData;
  1167. }
  1168. LPVOID
  1169. WINAPI fnAppLogonShared(
  1170. LPVOID lpvData)
  1171. {
  1172. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1173. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1174. SECURITY_STATUS ss;
  1175. dprintf( ENTRY_EXIT, "Enter: fnAppLogonShared %#x \n", lpvData );
  1176. lpCtxRecord = FindFreeSlot( lpContext -> hCredentialHandles, &FREE_CTX_REC );
  1177. if( lpCtxRecord != NULL ) {
  1178. lpCtxRecord -> szAppCtx = NULL;
  1179. lpCtxRecord -> szUserCtx = NULL;
  1180. ss = LogonToDigestPkg(NULL, NULL, &lpCtxRecord -> hCred);
  1181. if(!SEC_SUCCESS(ss) ) {
  1182. dprintf(ERROR,"AcquireCredentialHandle(%s,%s) failed (%s)\n",
  1183. lpCtxRecord -> szAppCtx,
  1184. lpCtxRecord -> szUserCtx,
  1185. issperr2str(ss));
  1186. //
  1187. // Since we failed, Release the slot
  1188. //
  1189. lpCtxRecord->Mode = MODE_FREE;
  1190. } else {
  1191. lpCtxRecord->Mode = MODE_BUSY;
  1192. }
  1193. }
  1194. dprintf( ENTRY_EXIT, "Exit: fnAppLogonShared %#x \n", lpvData );
  1195. return lpvData;
  1196. }
  1197. LPVOID
  1198. WINAPI fnPopulateCredentials(
  1199. LPVOID lpvData)
  1200. {
  1201. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1202. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1203. dprintf( ENTRY_EXIT, "Enter: fnPopulateCredentials %#x \n", lpvData );
  1204. lpCtxRecord = FindFreeSlot( lpContext -> hCredentialHandles, &BUSY_CTX_REC );
  1205. if( lpCtxRecord != NULL ) {
  1206. // realm 1
  1207. PrimeCredCache(lpCtxRecord -> hCred, "[email protected]", "[email protected]", "pass1_1");
  1208. PrimeCredCache(lpCtxRecord -> hCred, "[email protected]", "[email protected]", "pass2_1");
  1209. PrimeCredCache(lpCtxRecord -> hCred, "[email protected]", "[email protected]", "pass3_1");
  1210. // realm 2
  1211. PrimeCredCache(lpCtxRecord -> hCred, "[email protected]", "[email protected]", "pass1_2");
  1212. // realm 3
  1213. PrimeCredCache(lpCtxRecord -> hCred, "[email protected]", "[email protected]", "pass1_3");
  1214. }
  1215. dprintf( ENTRY_EXIT, "Exit: fnPopulateCredentials %#x \n", lpvData );
  1216. return lpvData;
  1217. }
  1218. LPVOID
  1219. WINAPI fnAuthChallenge(
  1220. LPVOID lpvData)
  1221. {
  1222. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1223. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1224. DWORD dwError = ERROR_SUCCESS;
  1225. dprintf( ENTRY_EXIT, "Enter: fnAuthChallenge %#x \n", lpvData );
  1226. dwError = TuringMachine(
  1227. AUTH_CHALLENGE_TRANSITION_TABLE,
  1228. STATE_INIT,
  1229. lpvData
  1230. );
  1231. dprintf( ENTRY_EXIT, "Exit: fnAuthChallenge %#x \n", lpvData );
  1232. return lpvData;
  1233. }
  1234. LPVOID
  1235. WINAPI fnAuthChallengeAny(
  1236. LPVOID lpvData)
  1237. {
  1238. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1239. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1240. dprintf( ENTRY_EXIT, "Enter: fnAuthChallengeAny %#x \n", lpvData );
  1241. lpCtxRecord = FindFreeSlot( lpContext -> hCredentialHandles, &BUSY_CTX_REC );
  1242. SECURITY_STATUS ss;
  1243. if( lpCtxRecord != NULL ) {
  1244. //LPSTR szChallenge;
  1245. //szChallenge = "realm=\"[email protected]\", stale = FALSE, qop=\"auth,auth-int\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"";
  1246. TCHAR szChallenge[512];
  1247. DWORD cbChallenge=512;
  1248. GenerateServerChallenge(szChallenge,cbChallenge);
  1249. // Package will dump response into this buffer.
  1250. CHAR szResponse[4096];
  1251. CtxtHandle hCtxt = {0,0};
  1252. memset((LPVOID)szResponse,0,4096);
  1253. // First try at authenticating.
  1254. ss = DoAuthenticate( &lpCtxRecord -> hCred, // Cred from logging on.
  1255. NULL, // Ctxt not specified first time.
  1256. &hCtxt, // Output context.
  1257. ISC_REQ_USE_SUPPLIED_CREDS, // auth from cache.
  1258. szChallenge, // Server challenge header.
  1259. NULL, // no realm since not preauth.
  1260. "www.foo.com", // Host.
  1261. "/bar/baz/boz/bif.html", // Url.
  1262. "GET", // Method.
  1263. NULL, // no Username
  1264. NULL, // no Password.
  1265. NULL, // no nonce
  1266. NULL, // don't need hdlg for auth.
  1267. szResponse, // Response buffer.
  1268. 4096);
  1269. }
  1270. if(!SEC_SUCCESS(ss) ) {
  1271. dprintf(ERROR,"ISC(use-supplied-cred) Failed %s \n", issperr2str(ss) );
  1272. }
  1273. dprintf( ENTRY_EXIT, "Exit: fnAuthChallengeAny %#x \n", lpvData );
  1274. return lpvData;
  1275. }
  1276. LPVOID
  1277. WINAPI fnAuthChallengeUser(
  1278. LPVOID lpvData)
  1279. {
  1280. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1281. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1282. dprintf( ENTRY_EXIT, "Enter: fnAuthChallengeUser %#x \n", lpvData );
  1283. lpCtxRecord = FindFreeSlot( lpContext -> hCredentialHandles, &BUSY_CTX_REC );
  1284. SECURITY_STATUS ss;
  1285. if( lpCtxRecord != NULL ) {
  1286. //LPSTR szChallenge;
  1287. //szChallenge = "realm=\"[email protected]\", stale = FALSE, qop=\"auth,auth-int\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"";
  1288. TCHAR szChallenge[512];
  1289. DWORD cbChallenge=512;
  1290. GenerateServerChallenge(szChallenge,cbChallenge);
  1291. // Package will dump response into this buffer.
  1292. CHAR szResponse[4096];
  1293. CtxtHandle hCtxt = {0,0};
  1294. memset((LPVOID)szResponse,0,4096);
  1295. // First try at authenticating.
  1296. ss = DoAuthenticate( &lpCtxRecord -> hCred, // Cred from logging on.
  1297. NULL, // Ctxt not specified first time.
  1298. &hCtxt, // Output context.
  1299. ISC_REQ_USE_SUPPLIED_CREDS, // auth from cache.
  1300. szChallenge, // Server challenge header.
  1301. NULL, // no realm since not preauth.
  1302. "www.foo.com", // Host.
  1303. "/bar/baz/boz/bif.html", // Url.
  1304. "GET", // Method.
  1305. "[email protected]", // no Username
  1306. NULL, // no Password.
  1307. NULL, // no nonce
  1308. NULL, // don't need hdlg for auth.
  1309. szResponse, // Response buffer.
  1310. 4096);
  1311. }
  1312. if(!SEC_SUCCESS(ss) ) {
  1313. dprintf(ERROR,"ISC(use-supplied-cred) Failed %s \n", issperr2str(ss) );
  1314. }
  1315. dprintf( ENTRY_EXIT, "Exit: fnAuthChallengeUser %#x \n", lpvData );
  1316. return lpvData;
  1317. }
  1318. LPVOID
  1319. WINAPI fnAuthChallengeUserPassword(
  1320. LPVOID lpvData)
  1321. {
  1322. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1323. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1324. dprintf( ENTRY_EXIT, "Enter: fnAuthChallengeUserPassword %#x \n", lpvData );
  1325. lpCtxRecord = FindFreeSlot( lpContext -> hCredentialHandles, &BUSY_CTX_REC );
  1326. SECURITY_STATUS ss;
  1327. if( lpCtxRecord != NULL ) {
  1328. //LPSTR szChallenge;
  1329. //szChallenge = "realm=\"[email protected]\", stale = FALSE, qop=\"auth,auth-int\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"";
  1330. TCHAR szChallenge[512];
  1331. DWORD cbChallenge=512;
  1332. GenerateServerChallenge(szChallenge,cbChallenge);
  1333. // Package will dump response into this buffer.
  1334. CHAR szResponse[4096];
  1335. CtxtHandle hCtxt = {0,0};
  1336. memset((LPVOID)szResponse,0,4096);
  1337. // First try at authenticating.
  1338. ss =
  1339. DoAuthenticate( &lpCtxRecord -> hCred, // Cred from logging on.
  1340. NULL, // Ctxt not specified first time.
  1341. &hCtxt, // Output context.
  1342. ISC_REQ_USE_SUPPLIED_CREDS, // auth from cache.
  1343. szChallenge, // Server challenge header.
  1344. NULL, // no realm since not preauth.
  1345. "www.foo.com", // Host.
  1346. "/bar/baz/boz/bif.html", // Url.
  1347. "GET", // Method.
  1348. "[email protected]", // no Username
  1349. "pass1_1", // no Password.
  1350. NULL, // no nonce
  1351. NULL, // don't need hdlg for auth.
  1352. szResponse, // Response buffer.
  1353. 4096);
  1354. }
  1355. if(!SEC_SUCCESS(ss) ) {
  1356. dprintf(ERROR,"ISC(use-supplied-cred) Failed %s \n", issperr2str(ss) );
  1357. DebugBreak();
  1358. }
  1359. dprintf( ENTRY_EXIT, "Exit: fnAuthChallengeUserPassword %#x \n", lpvData );
  1360. return lpvData;
  1361. }
  1362. BOOL
  1363. SetUIUserNameAndPassword(
  1364. LPSTR szUsername,
  1365. LPSTR szPassword,
  1366. BOOL fPersist)
  1367. {
  1368. #define DIALOGCLASS 32770
  1369. #define IDB_OK_BUTTON 1
  1370. #define ODS(s) dprintf(FATAL,s);
  1371. LPSTR m_szMainCaption = "Enter Network Password";
  1372. HWND hdlg;
  1373. DWORD dwTime;
  1374. DWORD dwCount=0;
  1375. dwTime = GetTickCount();
  1376. //Use this to get the hdlg for the dialog window
  1377. hdlg =::FindWindow(MAKEINTATOM(32770),(LPCTSTR)m_szMainCaption);
  1378. while ((NULL==hdlg) && (GetTickCount() - dwTime <= 10000)) {
  1379. //dprintf(FATAL,"Cannot find dialog window: %d\n",GetLastError());
  1380. // return FALSE;
  1381. hdlg =::FindWindow(MAKEINTATOM(32770),(LPCTSTR)m_szMainCaption);
  1382. }
  1383. if( hdlg == NULL ) {
  1384. dprintf(FATAL,"Cannot find dialog window: %d\n",GetLastError());
  1385. //DebugBreak();
  1386. return FALSE;
  1387. }
  1388. dprintf(INFO,"Found window.....\n");
  1389. //Use this after you've got the handle to the main dialog window.
  1390. //This will look for the edit control and enter text.
  1391. if( fPersist ) {
  1392. dprintf(INFO,"************ PASSWORD WILL BE PERSISTED ****************\n");
  1393. }
  1394. HWND hwnd = NULL;
  1395. int iEditId = 0;
  1396. UI_CONTROL uiControl;
  1397. uiControl.szUserName = szUsername;
  1398. uiControl.szPassword = szPassword;
  1399. uiControl.fPersist = fPersist;
  1400. uiControl.dwCount = 0;
  1401. uiControl.hOK = NULL;
  1402. uiControl.hPassword = NULL;
  1403. uiControl.hPasswordSave = NULL;
  1404. uiControl.hUserName = NULL;
  1405. Sleep(3000);
  1406. #if 1
  1407. uiControl.hOK = GetDlgItem(hdlg,IDB_OK_BUTTON);
  1408. uiControl.hPassword = GetDlgItem(hdlg,IDC_PASSWORD_FIELD);
  1409. uiControl.hPasswordSave = GetDlgItem(hdlg,IDC_SAVE_PASSWORD);
  1410. uiControl.hUserName = GetDlgItem(hdlg,IDC_COMBO1);
  1411. #else
  1412. EnumChildWindows(hdlg, EnumerateWindowCallback, (LPARAM)&uiControl);
  1413. #endif
  1414. dprintf(INFO,"EnumWindows Returned :\n");
  1415. dprintf(INFO,"hUsername %#x, hPassword %#x, hPasswordSave %#x, hOK %#x\n",
  1416. uiControl.hUserName,
  1417. uiControl.hPassword,
  1418. uiControl.hPasswordSave,
  1419. uiControl.hOK);
  1420. //getch();
  1421. if (uiControl.hPasswordSave != NULL) {
  1422. hdlg = uiControl.hPasswordSave;
  1423. printf("\n");
  1424. dprintf(INFO,"=============== Found SAVE_PASSWORD check box field\n");
  1425. if(!(uiControl.fPersist)) {
  1426. dprintf(INFO,"DONT WANNA PERSIST @@@@@ !\n");
  1427. } else {
  1428. //Sleep(2000);//not required remove later
  1429. if(!::PostMessage(hdlg, BM_CLICK, (WPARAM)0, (LPARAM)0)) {
  1430. ODS("FAILED: to send message to SAVE_PASSWORD check Box");
  1431. //DebugBreak();
  1432. } else {
  1433. ODS("sent message successfullly to SAVE_PASSWORD Edit Control\n");
  1434. }
  1435. // Sleep(2000);
  1436. }
  1437. ++ uiControl.dwCount;
  1438. }
  1439. //getch();
  1440. if (uiControl.hUserName != NULL) {
  1441. hdlg = uiControl.hUserName;
  1442. dprintf(INFO,"Sending Message To USERNAME field (%#x)\n",hdlg);
  1443. #if 0
  1444. SendMessage(
  1445. hdlg,
  1446. CB_SHOWDROPDOWN,
  1447. (WPARAM) TRUE,
  1448. (LPARAM) 0);
  1449. #endif
  1450. //Sleep(2000);//not required remove later
  1451. if(!::SendMessage(hdlg, WM_SETTEXT, (WPARAM) 0, (LPARAM)(LPCTSTR) szUsername)) {
  1452. ODS("FAILED: to send message to Edit Box\n");
  1453. //DebugBreak();
  1454. } else {
  1455. ODS("sent message successfullly to USERNAME Edit Control\n");
  1456. }
  1457. ++ uiControl.dwCount;
  1458. }
  1459. //getch();
  1460. if (uiControl.hPassword != NULL) {
  1461. hdlg = uiControl.hPassword;
  1462. printf("\n");
  1463. dprintf(INFO,"Sending Message To PASSWORD field (%#X)\n",hdlg);
  1464. //Sleep(2000);//not required remove later
  1465. if(!::SendMessage(hdlg, WM_SETTEXT, 0, (LPARAM)(LPCTSTR) szPassword)) {
  1466. ODS("FAILED: to send message to Edit Box");
  1467. //DebugBreak();
  1468. } else {
  1469. ODS("sent message successfullly to PASSWORD Edit Control\n");
  1470. }
  1471. ++ uiControl.dwCount;
  1472. }
  1473. dprintf(INFO,"Clicking on OK button (%#X) in dialog \n",uiControl.hOK );
  1474. Sleep(2000);
  1475. SendMessage(uiControl.hOK, BM_CLICK, 0, 0);
  1476. //PostMessage(hdlg,
  1477. // WM_COMMAND,
  1478. // MAKEWPARAM(IDB_OK_BUTTON,BN_CLICKED),
  1479. // MAKELPARAM(,0));
  1480. return TRUE;
  1481. }
  1482. LPVOID
  1483. WINAPI fnUiPrompt(
  1484. LPVOID lpvData)
  1485. {
  1486. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1487. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1488. DWORD dwError = ERROR_SUCCESS;
  1489. dprintf( ENTRY_EXIT, "Enter: fnUiPrompt %#x \n", lpvData );
  1490. dwError = TuringMachine(
  1491. UI_PROMPT_TRANSITION_TABLE,
  1492. STATE_INIT,
  1493. lpvData
  1494. );
  1495. dprintf( ENTRY_EXIT, "Exit: fnUiPrompt %#x \n", lpvData );
  1496. return lpvData;
  1497. }
  1498. LPVOID
  1499. WINAPI fnUiPromptUser(
  1500. LPVOID lpvData)
  1501. {
  1502. //LPSTR szChallenge;
  1503. //szChallenge = "realm=\"[email protected]\", stale = FALSE, qop=\"auth,auth-int\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"";
  1504. TCHAR szChallenge[512];
  1505. DWORD cbChallenge=512;
  1506. GenerateServerChallenge(szChallenge,cbChallenge);
  1507. // Package will dump response into this buffer.
  1508. CHAR szResponse[4096];
  1509. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1510. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1511. dprintf( ENTRY_EXIT, "Enter: fnUiPromptUser %#x \n", lpvData );
  1512. lpCtxRecord = FindFreeSlot( lpContext -> hCredentialHandles, &BUSY_CTX_REC );
  1513. CtxtHandle hCtxt = {0,0};
  1514. SECURITY_STATUS ssResult;
  1515. memset((LPVOID)szResponse,0,4096);
  1516. // First try at authenticating.
  1517. ssResult =
  1518. DoAuthenticate( &lpCtxRecord -> hCred, // Cred from logging on.
  1519. NULL, // Ctxt not specified first time.
  1520. &hCtxt, // Output context.
  1521. 0, // auth from cache.
  1522. szChallenge, // Server challenge header.
  1523. NULL, // no realm since not preauth.
  1524. "www.foo.com", // Host.
  1525. "/bar/baz/boz/bif.html", // Url.
  1526. "GET", // Method.
  1527. "[email protected]", // Username
  1528. NULL, // no Password.
  1529. NULL, // no nonce
  1530. NULL, // don't need hdlg for auth.
  1531. szResponse, // Response buffer.
  1532. 4096);
  1533. // Expect to not have credentials the first time - prompt.
  1534. if (ssResult == SEC_E_NO_CREDENTIALS)
  1535. {
  1536. memset((LPVOID)szResponse,0,4096);
  1537. ssResult =
  1538. DoAuthenticate( &lpCtxRecord -> hCred, // Cred from logging on.
  1539. &hCtxt, // Ctxt from previous call
  1540. &hCtxt, // Output context (same as from previous).
  1541. ISC_REQ_PROMPT_FOR_CREDS, // prompt
  1542. szChallenge, // Server challenge
  1543. NULL, // No realm
  1544. "www.foo.com", // Host
  1545. "/bar/baz/boz/bif.html", // Url
  1546. "GET", // Method
  1547. NULL, // no username
  1548. NULL, // no password
  1549. NULL, // no nonce
  1550. GetDesktopWindow(), // desktop window
  1551. szResponse, // Response buffer.
  1552. 4096);
  1553. }
  1554. // We now have credentials and this will generate the output string.
  1555. //
  1556. // BUGBUG:
  1557. // THis has just been fixed by AdriaanC, so we put in a hack here
  1558. // We will only prompt if the string has not been generated yet.
  1559. //
  1560. if (
  1561. (ssResult == SEC_E_OK)
  1562. && (!*szResponse)
  1563. ) {
  1564. memset((LPVOID)szResponse,0,4096);
  1565. ssResult =
  1566. DoAuthenticate( &lpCtxRecord -> hCred, // Cred from logging on.
  1567. &hCtxt, // Ctxt not specified first time.
  1568. &hCtxt, // Output context.
  1569. 0, // auth
  1570. szChallenge, // Server challenge.
  1571. NULL, // no realm
  1572. "www.foo.com", // Host.
  1573. "/bar/baz/boz/bif.html", // Url.
  1574. "GET", // Method.
  1575. NULL, // no username
  1576. NULL, // no password
  1577. NULL, // no nonce
  1578. NULL, // no hdlg
  1579. szResponse, // Response buffer.
  1580. 4096);
  1581. }
  1582. return lpvData;
  1583. }
  1584. LPVOID
  1585. WINAPI fnUiPromptAny(
  1586. LPVOID lpvData)
  1587. {
  1588. //LPSTR szChallenge;
  1589. //szChallenge = "realm=\"[email protected]\", stale = FALSE, qop=\"auth,auth-int\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"";
  1590. TCHAR szChallenge[512];
  1591. DWORD cbChallenge=512;
  1592. GenerateServerChallenge(szChallenge,cbChallenge);
  1593. // Package will dump response into this buffer.
  1594. CHAR szResponse[4096];
  1595. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1596. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1597. dprintf( ENTRY_EXIT, "Enter: fnUiPromptAny %#x \n", lpvData );
  1598. lpCtxRecord = FindFreeSlot( lpContext -> hCredentialHandles, &BUSY_CTX_REC );
  1599. CtxtHandle hCtxt = {0,0};
  1600. SECURITY_STATUS ssResult;
  1601. memset((LPVOID)szResponse,0,4096);
  1602. // First try at authenticating.
  1603. ssResult =
  1604. DoAuthenticate( &lpCtxRecord -> hCred, // Cred from logging on.
  1605. NULL, // Ctxt not specified first time.
  1606. &hCtxt, // Output context.
  1607. 0, // auth from cache.
  1608. szChallenge, // Server challenge header.
  1609. NULL, // no realm since not preauth.
  1610. "www.foo.com", // Host.
  1611. "/bar/baz/boz/bif.html", // Url.
  1612. "GET", // Method.
  1613. NULL, // no Username
  1614. NULL, // no Password.
  1615. NULL, // no nonce
  1616. NULL, // don't need hdlg for auth.
  1617. szResponse, // Response buffer.
  1618. 4096);
  1619. // Expect to not have credentials the first time - prompt.
  1620. if (ssResult == SEC_E_NO_CREDENTIALS)
  1621. {
  1622. memset((LPVOID)szResponse,0,4096);
  1623. ssResult =
  1624. DoAuthenticate( &lpCtxRecord -> hCred, // Cred from logging on.
  1625. &hCtxt, // Ctxt from previous call
  1626. &hCtxt, // Output context (same as from previous).
  1627. ISC_REQ_PROMPT_FOR_CREDS, // prompt
  1628. szChallenge, // Server challenge
  1629. NULL, // No realm
  1630. "www.foo.com", // Host
  1631. "/bar/baz/boz/bif.html", // Url
  1632. "GET", // Method
  1633. NULL, // no username
  1634. NULL, // no password
  1635. NULL, // no nonce
  1636. GetDesktopWindow(), // desktop window
  1637. szResponse, // Response buffer.
  1638. 4096);
  1639. }
  1640. // We now have credentials and this will generate the output string.
  1641. //
  1642. // BUGBUG:
  1643. // THis has just been fixed by AdriaanC, so we put in a hack here
  1644. // We will only prompt if the string has not been generated yet.
  1645. //
  1646. if (
  1647. (ssResult == SEC_E_OK)
  1648. && (!*szResponse)
  1649. ) {
  1650. memset((LPVOID)szResponse,0,4096);
  1651. ssResult =
  1652. DoAuthenticate( &lpCtxRecord -> hCred, // Cred from logging on.
  1653. &hCtxt, // Ctxt not specified first time.
  1654. &hCtxt, // Output context.
  1655. 0, // auth
  1656. szChallenge, // Server challenge.
  1657. NULL, // no realm
  1658. "www.foo.com", // Host.
  1659. "/bar/baz/boz/bif.html", // Url.
  1660. "GET", // Method.
  1661. NULL, // no username
  1662. NULL, // no password
  1663. NULL, // no nonce
  1664. NULL, // no hdlg
  1665. szResponse, // Response buffer.
  1666. 4096);
  1667. }
  1668. return lpvData;
  1669. }
  1670. LPVOID
  1671. WINAPI fnFlushCredentials(
  1672. LPVOID lpvData)
  1673. {
  1674. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1675. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1676. DWORD dwError = ERROR_SUCCESS;
  1677. dprintf( ENTRY_EXIT, "Enter: fnFlushCredentials %#x \n", lpvData );
  1678. dwError = TuringMachine(
  1679. FLUSH_CREDENTIALS_TRANSITION_TABLE,
  1680. STATE_INIT,
  1681. lpvData
  1682. );
  1683. dprintf( ENTRY_EXIT, "Exit: fnFlushCredentials %#x \n", lpvData );
  1684. return lpvData;
  1685. }
  1686. LPVOID
  1687. WINAPI fnFlushCredentialsGlobal(
  1688. LPVOID lpvData)
  1689. {
  1690. dprintf( ENTRY_EXIT, "Enter: fnFlushCredentialsGlobal %#x \n", lpvData );
  1691. SECURITY_STATUS ssResult;
  1692. ssResult =
  1693. DoAuthenticate( NULL, // Cred from logging on.
  1694. NULL, // Ctxt not specified first time.
  1695. NULL, // Output context.
  1696. ISC_REQ_NULL_SESSION, // auth from cache.
  1697. NULL,//szChallenge, // Server challenge header.
  1698. NULL, // no realm since not preauth.
  1699. NULL,//"www.foo.com", // Host.
  1700. NULL,//"/bar/baz/boz/bif.html", // Url.
  1701. NULL,//"GET", // Method.
  1702. NULL,//"user1", // no Username
  1703. NULL, // no Password.
  1704. NULL, // no nonce
  1705. NULL, // don't need hdlg for auth.
  1706. NULL, // Response buffer.
  1707. 0);
  1708. dprintf( ENTRY_EXIT, "Exit: fnFlushCredentialsGlobal %#x \n", lpvData );
  1709. return lpvData;
  1710. }
  1711. LPVOID
  1712. WINAPI fnFlushCredentialsSession(
  1713. LPVOID lpvData)
  1714. {
  1715. LPCONTEXT_DATA lpContext = (LPCONTEXT_DATA) lpvData;
  1716. LPCONTEXT_RECORD lpCtxRecord = NULL;
  1717. dprintf( ENTRY_EXIT, "Enter: fnFlushCredentialsSession %#x \n", lpvData );
  1718. lpCtxRecord = FindFreeSlot( lpContext -> hCredentialHandles, &BUSY_CTX_REC );
  1719. SECURITY_STATUS ssResult;
  1720. ssResult =
  1721. DoAuthenticate( &lpCtxRecord->hCred, // Cred from logging on.
  1722. NULL, // Ctxt not specified first time.
  1723. NULL,//&hCtxt, // Output context.
  1724. ISC_REQ_NULL_SESSION, // auth from cache.
  1725. NULL,//szChallenge, // Server challenge header.
  1726. NULL, // no realm since not preauth.
  1727. NULL,//"www.foo.com", // Host.
  1728. NULL,//"/bar/baz/boz/bif.html", // Url.
  1729. NULL,//"GET", // Method.
  1730. NULL,//"user1", // no Username
  1731. NULL, // no Password.
  1732. NULL, // no nonce
  1733. NULL, // don't need hdlg for auth.
  1734. NULL,
  1735. 0);//szResponse); // Response buffer.
  1736. return lpvData;
  1737. }
  1738. BOOL
  1739. GenerateServerChallenge(
  1740. LPSTR szChallenge,
  1741. DWORD cbChallenge)
  1742. {
  1743. int i;
  1744. TCHAR * szAlgorithm = NULL,
  1745. * szQop = NULL,
  1746. * szNonce = NULL,
  1747. * szOpaque = NULL,
  1748. * szRealm = NULL,
  1749. * szPtr = NULL,
  1750. * szMs_message = NULL,
  1751. * szMs_reserved = NULL,
  1752. * szMs_trustmark = NULL;
  1753. //
  1754. // BUGBUG: we want to make sure that this generates a string which will fit
  1755. // in the given buffer.
  1756. //
  1757. dprintf(ENTRY_EXIT,"ENTER: GenerateServerChallenge\n" );
  1758. i = rand() % 100;
  1759. if( i < 50 ) {
  1760. szAlgorithm = _T("MD5");
  1761. } else {
  1762. szAlgorithm = _T("MD5-Sess");
  1763. }
  1764. i = rand() % 100;
  1765. if((i >= 0) && ( i < 20 )) {
  1766. szRealm = _T("[email protected]");
  1767. } else
  1768. if(( i >= 20 ) && ( i < 40)) {
  1769. szRealm = _T("[email protected]");
  1770. } else
  1771. if(( i >= 40 ) && ( i < 60)) {
  1772. szRealm = _T("[email protected]");
  1773. } else {
  1774. szRealm = _T("[email protected]");
  1775. }
  1776. #if 0
  1777. i = rand() % 100;
  1778. if( i < 50 ) {
  1779. szMs_message = _T("\"This is a test microsoft message\"");
  1780. } else {
  1781. szMs_message = NULL;
  1782. }
  1783. i = rand() % 100;
  1784. if( i < 30 ) {
  1785. szMs_trustmark = _T("\"http://www.bbbonline.org\"");
  1786. } else
  1787. if(( i >= 30 ) && (i < 80)) {
  1788. szMs_trustmark = _T("\"http://www.truste.org\"");
  1789. } else {
  1790. szMs_trustmark = NULL;
  1791. }
  1792. i = rand() % 100;
  1793. if( i < 30 ) {
  1794. szMs_reserved = _T("\"MSEXT::CAPTION=%Enter Network Password%REGISTER=%http://www.microsoft.com%\"");
  1795. }
  1796. //else
  1797. //if(( i >= 30 ) && (i < 80)) {
  1798. //szMs_trustmark = _T("\"http://www.truste.org\"");
  1799. //} else {
  1800. //szMs_trustmark = NULL;
  1801. //}
  1802. #endif
  1803. szQop = _T("auth");
  1804. szOpaque = _T("101010101010");
  1805. szNonce = _T("abcdef0123456789");
  1806. szPtr = szChallenge;
  1807. szPtr += sprintf(szChallenge,"realm=\"%s\", qop=\"%s\", algorithm=\"%s\", nonce=\"%s\", opaque=\"%s\"",
  1808. szRealm,
  1809. szQop,
  1810. szAlgorithm,
  1811. szNonce,
  1812. szOpaque);
  1813. #if 0
  1814. if( szMs_message && *szMs_message ) {
  1815. szPtr += sprintf(szPtr,", ms-message=%s, ms-message-lang=\"EN\"",
  1816. szMs_message);
  1817. }
  1818. if( szMs_trustmark && *szMs_trustmark ) {
  1819. szPtr += sprintf(szPtr,", ms-trustmark=%s",
  1820. szMs_trustmark);
  1821. }
  1822. if( szMs_reserved && *szMs_reserved ) {
  1823. szPtr += sprintf(szPtr,", ms-reserved=%s",
  1824. szMs_reserved);
  1825. }
  1826. i = rand() % 100;
  1827. if( i < 50 ) {
  1828. szPtr += sprintf( szPtr,", MS-Logoff=\"TRUE\"");
  1829. }
  1830. #endif
  1831. dprintf(ENTRY_EXIT,"EXIT: GenerateServerChallenge returns \n%s\n", szChallenge );
  1832. return TRUE;
  1833. }
  1834. #define IDENTITY_1 "app1"
  1835. #define IDENTITY_2 "app2"
  1836. BOOL Test()
  1837. {
  1838. CSessionAttributeList * g_pSessionAttributeList = NULL;
  1839. CSessionList * g_pSessionList = NULL;
  1840. CSession * pSession = NULL;
  1841. CSessionAttribute * pSessionAttribute = NULL;
  1842. CredHandle hCred1, hCred2, hCred3;
  1843. SECURITY_STATUS ssResult;
  1844. g_pSessionAttributeList = new CSessionAttributeList();
  1845. g_pSessionList = new CSessionList();
  1846. pSessionAttribute = g_pSessionAttributeList -> getNewSession( FALSE );
  1847. if( !pSessionAttribute ) {
  1848. goto cleanup;
  1849. }
  1850. LogonToDigestPkg(
  1851. pSessionAttribute ->getAppCtx() ,
  1852. pSessionAttribute->getUserCtx(),
  1853. &hCred1);
  1854. pSession = new CSession( hCred1 );
  1855. pSession -> setAttribute( pSessionAttribute );
  1856. g_pSessionList->put( pSession );
  1857. pSession = NULL;
  1858. pSessionAttribute = NULL;
  1859. //
  1860. // now get shared SessionAttribute
  1861. //
  1862. pSessionAttribute = g_pSessionAttributeList -> getNewSession( TRUE );
  1863. LogonToDigestPkg(
  1864. pSessionAttribute ->getAppCtx() ,
  1865. pSessionAttribute->getUserCtx(),
  1866. &hCred2);
  1867. pSession = new CSession( hCred2 );
  1868. pSession -> setAttribute( pSessionAttribute );
  1869. g_pSessionList -> put( pSession);
  1870. LogonToDigestPkg(
  1871. pSessionAttribute ->getAppCtx() ,
  1872. pSessionAttribute->getUserCtx(),
  1873. &hCred3);
  1874. pSession = new CSession( hCred3 );
  1875. pSession -> setAttribute( pSessionAttribute );
  1876. g_pSessionList -> put( pSession);
  1877. //
  1878. // lets add a credential to this session
  1879. //
  1880. pSession -> addCredential( "[email protected]", "user1", "pass1" );
  1881. pSession -> addCredential( "[email protected]", "user1", "pass11" );
  1882. // add one more
  1883. pSession -> addCredential( "[email protected]", "user2", "pass2" );
  1884. // add one more
  1885. pSession -> addCredential( "[email protected]", "user3", "pass3" );
  1886. // replace
  1887. pSession -> addCredential( "[email protected]", "user3", "pass31" );
  1888. // replace
  1889. pSession -> addCredential( "[email protected]", "user2", "pass21" );
  1890. pSession -> addCredential( "[email protected]", "user2", "pass22" );
  1891. cleanup:
  1892. return FALSE;
  1893. }
  1894. BOOL CALLBACK EnumerateWindowCallback1(HWND hdlg, LPARAM lParam)
  1895. {
  1896. int iEditId = 0;
  1897. BOOL fRet = TRUE;
  1898. LPUI_CONTROL lpuiControl = (LPUI_CONTROL) lParam;
  1899. LPTSTR szUsername = lpuiControl->szUserName;
  1900. LPTSTR szPassword = lpuiControl->szPassword;
  1901. dprintf(ENTRY_EXIT,"ENTER: EnumChildProc(hdlg=%#x,lParam=%#x)\n",hdlg,lParam);
  1902. dprintf(ENTRY_EXIT,"UI_CONTROL( %s %s %d %s %#x ) \n",
  1903. szUsername,
  1904. szPassword,
  1905. lpuiControl->dwCount,
  1906. (lpuiControl->fPersist?"TRUE":"FALSE"),
  1907. lpuiControl->hOK);
  1908. iEditId =::GetDlgCtrlID(hdlg);
  1909. dprintf(INFO,"The iEditId is %d!\n", iEditId);
  1910. if (iEditId == IDC_SAVE_PASSWORD) {
  1911. printf("\n");
  1912. dprintf(INFO,"=============== Found SAVE_PASSWORD check box field\n");
  1913. if(lpuiControl->dwCount == 3) {
  1914. dprintf(INFO,"Already sent message to this control\n");
  1915. goto done;;
  1916. }
  1917. if(!(lpuiControl -> fPersist)) {
  1918. dprintf(INFO,"DONT WANNA PERSIST @@@@@ !\n");
  1919. } else {
  1920. //Sleep(2000);//not required remove later
  1921. if(!::PostMessage(hdlg, BM_CLICK, (WPARAM)0, (LPARAM)0)) {
  1922. ODS("FAILED: to send message to SAVE_PASSWORD check Box");
  1923. DebugBreak();
  1924. } else {
  1925. ODS("sent message successfullly to SAVE_PASSWORD Edit Control\n");
  1926. }
  1927. // Sleep(2000);
  1928. }
  1929. ++ lpuiControl->dwCount;
  1930. } else
  1931. if (iEditId == IDC_COMBO1) {
  1932. printf("\n");
  1933. dprintf(INFO,"Found USERNAME field\n");
  1934. if(lpuiControl->dwCount == 3) {
  1935. dprintf(INFO,"Already sent message to this control\n");
  1936. goto done;;
  1937. }
  1938. #if 0
  1939. SendMessage(
  1940. hdlg,
  1941. CB_SHOWDROPDOWN,
  1942. (WPARAM) TRUE,
  1943. (LPARAM) 0);
  1944. #endif
  1945. //Sleep(2000);//not required remove later
  1946. if(!::SendMessage(hdlg, WM_SETTEXT, (WPARAM) 0, (LPARAM)(LPCTSTR) szUsername)) {
  1947. ODS("FAILED: to send message to Edit Box\n");
  1948. DebugBreak();
  1949. } else {
  1950. ODS("sent message successfullly to USERNAME Edit Control\n");
  1951. }
  1952. ++ lpuiControl->dwCount;
  1953. } else
  1954. if (iEditId == IDC_PASSWORD_FIELD) {
  1955. printf("\n");
  1956. dprintf(INFO,"Found PASSWORD field\n");
  1957. //Sleep(2000);//not required remove later
  1958. if(lpuiControl->dwCount == 3) {
  1959. dprintf(INFO,"Already sent message to this control\n");
  1960. goto done;;
  1961. }
  1962. if(!::SendMessage(hdlg, WM_SETTEXT, 0, (LPARAM)(LPCTSTR) szPassword)) {
  1963. ODS("FAILED: to send message to Edit Box");
  1964. DebugBreak();
  1965. } else {
  1966. ODS("sent message successfullly to PASSWORD Edit Control\n");
  1967. }
  1968. ++ lpuiControl->dwCount;
  1969. } else
  1970. if( iEditId == IDB_OK_BUTTON ) {
  1971. lpuiControl->hOK = hdlg;
  1972. }
  1973. if( lpuiControl -> dwCount == 3 ) {
  1974. if( lpuiControl->hOK ) {
  1975. dprintf(INFO,"ALL WINDOWS FOUND, OK BUTTON FOUND, ABORT\n");
  1976. fRet = FALSE;
  1977. goto done;;
  1978. }
  1979. }
  1980. done:
  1981. dprintf(ENTRY_EXIT,"EXIT: EnumChileProc() returning %s\n",fRet?"TRUE":"FALSE");
  1982. return fRet;
  1983. }
  1984. BOOL CALLBACK EnumerateWindowCallback(HWND hdlg, LPARAM lParam)
  1985. {
  1986. int iEditId = 0;
  1987. BOOL fRet = TRUE;
  1988. LPUI_CONTROL lpuiControl = (LPUI_CONTROL) lParam;
  1989. dprintf(ENTRY_EXIT,"ENTER: EnumChildProc(hdlg=%#x,lParam=%#x)\n",hdlg,lParam);
  1990. dprintf(ENTRY_EXIT,"UI_CONTROL( %s %s %d %s %#x ) \n",
  1991. lpuiControl->szUserName,
  1992. lpuiControl->szPassword,
  1993. lpuiControl->dwCount,
  1994. (lpuiControl->fPersist?"TRUE":"FALSE"),
  1995. lpuiControl->hOK);
  1996. if ( (iEditId =::GetDlgCtrlID(hdlg)) == 0)
  1997. {
  1998. dprintf(INFO,"GetDlgCtrlID(hdlg) failed. GetLastError returned %d!\n", GetLastError());
  1999. return FALSE;
  2000. }
  2001. dprintf(INFO,"The iEditId is %d!\n", iEditId);
  2002. if (iEditId == IDC_SAVE_PASSWORD) {
  2003. printf("\n");
  2004. dprintf(INFO,"=============== Found SAVE_PASSWORD check box field\n");
  2005. if(lpuiControl->hPasswordSave != NULL) {
  2006. dprintf(INFO,"Already found window to this control\n");
  2007. goto done;;
  2008. }
  2009. lpuiControl->hPasswordSave = hdlg;
  2010. ++ lpuiControl->dwCount;
  2011. } else
  2012. if (iEditId == IDC_COMBO1) {
  2013. printf("\n");
  2014. dprintf(INFO,"Found USERNAME field\n");
  2015. if(lpuiControl->hUserName != NULL) {
  2016. dprintf(INFO,"Already found window to this control\n");
  2017. goto done;;
  2018. }
  2019. lpuiControl->hUserName = hdlg;
  2020. ++ lpuiControl->dwCount;
  2021. } else
  2022. if (iEditId == IDC_PASSWORD_FIELD) {
  2023. printf("\n");
  2024. dprintf(INFO,"Found PASSWORD field\n");
  2025. //Sleep(2000);//not required remove later
  2026. if(lpuiControl->hPassword != NULL) {
  2027. dprintf(INFO,"Already found window to this control\n");
  2028. goto done;;
  2029. }
  2030. lpuiControl->hPassword = hdlg;
  2031. ++ lpuiControl->dwCount;
  2032. } else
  2033. if( iEditId == IDB_OK_BUTTON ) {
  2034. lpuiControl->hOK = hdlg;
  2035. ++ lpuiControl->dwCount;
  2036. }
  2037. if( lpuiControl -> dwCount == 4 ) {
  2038. //if( lpuiControl->hOK ) {
  2039. dprintf(INFO,"ALL WINDOWS FOUND, OK BUTTON FOUND, ABORT\n");
  2040. fRet = FALSE;
  2041. goto done;
  2042. //}
  2043. }
  2044. done:
  2045. dprintf(ENTRY_EXIT,"EXIT: EnumChileProc() returning %s\n",fRet?"TRUE":"FALSE");
  2046. return fRet;
  2047. }