Source code of Windows XP (NT5)
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.

655 lines
15 KiB

  1. //#--------------------------------------------------------------
  2. //
  3. // File: proxystate.cpp
  4. //
  5. // Synopsis: Implementation of CProxyState class methods
  6. //
  7. //
  8. // History: 10/16/97 MKarki Created
  9. // 08/28/98 MKarki Updated to use perimeter class
  10. //
  11. // Copyright (C) 1997-98 Microsoft Corporation
  12. // All rights reserved.
  13. //
  14. //----------------------------------------------------------------
  15. #include "radcommon.h"
  16. #include "proxystate.h"
  17. //++--------------------------------------------------------------
  18. //
  19. // Function: CProxyState
  20. //
  21. // Synopsis: This is CProxyState class constructor
  22. //
  23. // Arguments: NONE
  24. //
  25. // Returns: NONE
  26. //
  27. //
  28. // History: MKarki Created 10/16/97
  29. //
  30. //----------------------------------------------------------------
  31. CProxyState::CProxyState()
  32. {
  33. m_dwTableIndex = 0;
  34. m_pProxyStateTable = NULL;
  35. return;
  36. } // end of CProxyState constructor
  37. //++--------------------------------------------------------------
  38. //
  39. // Function: ~CProxyState
  40. //
  41. // Synopsis: This is CProxyState class destructor
  42. //
  43. // Arguments: NONE
  44. //
  45. // Returns: NONE
  46. //
  47. //
  48. // History: MKarki Created 10/16/97
  49. //
  50. //----------------------------------------------------------------
  51. CProxyState::~CProxyState()
  52. {
  53. if (m_pProxyStateTable)
  54. {
  55. HeapFree (GetProcessHeap (), 0, m_pProxyStateTable);
  56. }
  57. return;
  58. } // end of CProxyState destructor
  59. //++--------------------------------------------------------------
  60. //
  61. // Function: Init
  62. //
  63. // Synopsis: This is CProxyState class public method used
  64. // for initializing the class object
  65. //
  66. // Arguments:
  67. // [in] DWORD - proxy state hash table size
  68. //
  69. // Returns: BOOL - status
  70. //
  71. //
  72. // History: MKarki Created 11/24/97
  73. //
  74. //----------------------------------------------------------------
  75. BOOL
  76. CProxyState::Init (
  77. DWORD dwIPAddress,
  78. DWORD dwTableSize
  79. )
  80. {
  81. BOOL bRetVal = FALSE;
  82. __try
  83. {
  84. if ((0 == dwIPAddress) || (0 == dwTableSize))
  85. __leave;
  86. //
  87. // initialize the table
  88. //
  89. m_pProxyStateTable = static_cast <CProxyInfo**>
  90. (HeapAlloc (
  91. GetProcessHeap (),
  92. 0,
  93. dwTableSize*sizeof (CProxyInfo*)
  94. ));
  95. if (NULL == m_pProxyStateTable) { __leave; }
  96. m_dwTableIndex = 0;
  97. m_dwTableSize = dwTableSize;
  98. //
  99. // initialize the IP address
  100. //
  101. m_dwIPAddress = dwIPAddress;
  102. //
  103. // DEBUGGING CODE START
  104. //
  105. /*
  106. BYTE ClientAuthenticator[AUTHENTICATOR_SIZE];
  107. BYTE ProxyAuthenticator[AUTHENTICATOR_SIZE];
  108. DWORD dwClientAddress = 0;
  109. WORD wClientPort = 0;
  110. ZeroMemory (ProxyAuthenticator, AUTHENTICATOR_SIZE);
  111. ProxyAuthenticator[0] = 24;
  112. CProxyInfo *pCProxyInfo = new CProxyInfo;
  113. pCProxyInfo->Init (
  114. ClientAuthenticator,
  115. ProxyAuthenticator,
  116. dwClientAddress,
  117. wClientPort
  118. );
  119. m_pProxyStateTable[m_dwTableIndex++] = pCProxyInfo;
  120. */
  121. //
  122. // DEBUGGING CODE ENDS
  123. //
  124. //
  125. // initialization successfull
  126. //
  127. bRetVal = TRUE;
  128. }
  129. __finally
  130. {
  131. //
  132. // nothing here for now
  133. //
  134. if ((FALSE == bRetVal) && (NULL != m_pProxyStateTable))
  135. {
  136. HeapFree (GetProcessHeap (), 0, m_pProxyStateTable);
  137. }
  138. }
  139. return (bRetVal);
  140. } // end of CProxyState::Init method
  141. //++--------------------------------------------------------------
  142. //
  143. // Function: SetProxyStateInfo
  144. //
  145. // Synopsis: This is CProxyState class public method used
  146. // for setting the Proxy state information, once
  147. // you have received the attribute
  148. //
  149. //
  150. // Arguments:
  151. // [in] - CPacketRadius*
  152. // [in] DWORD - state id
  153. //
  154. // Returns: BOOL status
  155. //
  156. // History: MKarki Created 10/16/97
  157. //
  158. //----------------------------------------------------------------
  159. BOOL CProxyState::SetProxyStateInfo(
  160. CPacketRadius *pCPacketRadius,
  161. DWORD dwProxyStateId,
  162. PBYTE pbyProxyReqAuthenticator
  163. )
  164. {
  165. BYTE ClientAuthenticator[AUTHENTICATOR_SIZE];
  166. DWORD dwClientAddress = 0;
  167. WORD wClientPort = 0;
  168. BOOL bRetVal = FALSE;
  169. CProxyInfo *pCProxyInfo = NULL;
  170. BOOL bStatus = FALSE;
  171. /*
  172. __try
  173. {
  174. if ((dwProxyStateId > m_dwTableSize) ||
  175. (NULL == pCPacketRadius)||
  176. (NULL == pbyProxyReqAuthenticator)
  177. )
  178. { __leave; }
  179. //
  180. // extract the NAS authenticator from the packet
  181. //
  182. bStatus = pCPacketRadius->GetInRequestAuthenticator (
  183. reinterpret_cast <PBYTE> (ClientAuthenticator)
  184. );
  185. if (FALSE == bStatus)
  186. __leave;
  187. //
  188. // get the NAS IP address
  189. //
  190. dwClientAddress = pCPacketRadius->GetInAddress ();
  191. //
  192. // get the NAS UDP port
  193. //
  194. wClientPort = pCPacketRadius->GetPort ();
  195. //
  196. // get a new CProxyInfo class object
  197. //
  198. pCProxyInfo = new CProxyInfo ();
  199. if (NULL == pCProxyInfo)
  200. __leave;
  201. //
  202. // set the values in the object now
  203. //
  204. bStatus = pCProxyInfo->Init (
  205. ClientAuthenticator,
  206. pbyProxyReqAuthenticator,
  207. dwClientAddress,
  208. wClientPort
  209. );
  210. if (FALSE == bStatus) {__leave;}
  211. //
  212. // lock the ProxyStateTable for write
  213. //
  214. LockExclusive ();
  215. //
  216. // insert the CProxyInfo object into the table
  217. //
  218. m_pProxyStateTable[dwProxyStateId] = pCProxyInfo;
  219. //
  220. // unlock the table now
  221. //
  222. Unlock ();
  223. //
  224. // we have successfully store the proxy information
  225. //
  226. bRetVal = TRUE;
  227. }
  228. __finally
  229. {
  230. }
  231. */
  232. return (bRetVal);
  233. } // end of CProxyState::SetProxyInfo method
  234. //++--------------------------------------------------------------
  235. //
  236. // Function: GenerateProxyState
  237. //
  238. // Synopsis: This is CProxyState class public method used
  239. // to generate a proxy state RADIUS attribute to
  240. // be added to the outbound packet
  241. //
  242. // Arguments:
  243. // [in] CPacketRadius*
  244. // [out] PDWORD - returns the Proxy state id
  245. //
  246. // Returns: BOOL - status
  247. //
  248. //
  249. // History: MKarki Created 11/24/97
  250. //
  251. //
  252. //----------------------------------------------------------------
  253. BOOL
  254. CProxyState::GenerateProxyState (
  255. CPacketRadius *pCPacketRadius,
  256. PDWORD pdwProxyStateId
  257. )
  258. {
  259. BOOL bRetVal = FALSE;
  260. BOOL bStatus = FALSE;
  261. DWORD dwCheckSum = 0;
  262. PROXYSTATE ProxyState;
  263. const DWORD PROXY_STATE_SIZE = sizeof (PROXYSTATE);
  264. DWORD dwSize = 0;
  265. /*
  266. __try
  267. {
  268. if ((NULL == pCPacketRadius) || (NULL == pdwProxyStateId))
  269. {
  270. __leave;
  271. }
  272. //
  273. // get a new attribute from CPacketRadius
  274. //
  275. bStatus = pCPacketRadius->CreateOutAttribute (
  276. &pCAttribute,
  277. PROXY_STATE_SIZE
  278. );
  279. if (FALSE == bStatus) { __leave; }
  280. if (dwSize < PROXY_STATE_SIZE) { __leave; }
  281. //
  282. // set Index value
  283. //
  284. LockExclusive ();
  285. ProxyState.dwIndex = htonl (m_dwTableIndex);
  286. m_dwTableIndex = (m_dwTableIndex + 1) % m_dwTableSize;
  287. Unlock ();
  288. //
  289. // set IP address
  290. //
  291. ProxyState.dwIPAddress = htonl (m_dwIPAddress);
  292. //
  293. // get check sum
  294. //
  295. bStatus = CalculateCheckSum (&ProxyState,&dwCheckSum);
  296. if (FALSE == bStatus)
  297. __leave;
  298. //
  299. // set checksum
  300. //
  301. ProxyState.dwCheckSum = htonl (dwCheckSum);
  302. //
  303. // set the attribute information
  304. //
  305. bStatus = pCAttribute->SetInfo (
  306. PROXY_STATE_ATTRIB,
  307. reinterpret_cast <PBYTE> (&ProxyState),
  308. PROXY_STATE_SIZE
  309. );
  310. if (FALSE == bStatus)
  311. __leave;
  312. //
  313. // store this CAttribute class object now
  314. //
  315. bStatus = pCPacketRadius->StoreOutAttribute (pCAttribute);
  316. if (FALSE == bStatus)
  317. __leave;
  318. //
  319. // set the proxy id to be returned
  320. //
  321. *pdwProxyStateId = ProxyState.dwIndex;
  322. //
  323. // attribute generated successfully
  324. //
  325. bRetVal = TRUE;
  326. }
  327. __finally
  328. {
  329. //
  330. // nothing here for now
  331. //
  332. }
  333. */
  334. return (bRetVal);
  335. } // end of CProxyState::GenerateProxyState method
  336. //++--------------------------------------------------------------
  337. //
  338. // Function: Validate
  339. //
  340. // Synopsis: This is CProxyState class private method used
  341. // for carrying out validation of the proxy state
  342. // attribute
  343. //
  344. // Arguments:
  345. // [out] PROXYSTATE
  346. //
  347. // Returns: BOOL - status
  348. //
  349. //
  350. // History: MKarki Created 11/24/97
  351. //
  352. // Called By: CProxyState::GetProxyStateInfo method
  353. //
  354. //----------------------------------------------------------------
  355. BOOL
  356. CProxyState::Validate (
  357. PPROXYSTATE pProxyState
  358. )
  359. {
  360. BOOL bRetVal = FALSE;
  361. BOOL bStatus = FALSE;
  362. DWORD dwLength = 0;
  363. DWORD dwCalculatedCheckSum = 0;
  364. /*
  365. __try
  366. {
  367. if ((NULL == pCAttribute) || (NULL == pProxyState))
  368. {
  369. __leave;
  370. }
  371. //
  372. // get the length of the attribute
  373. //
  374. dwLength = pCAttribute->GetLength ();
  375. //
  376. // check if the length of the proxy state is equal
  377. // to what is should be
  378. //
  379. if ((dwLength - ATTRIBUTE_HEADER_SIZE) != sizeof (PROXYSTATE))
  380. {
  381. __leave;
  382. }
  383. //
  384. // get the proxy state value now
  385. //
  386. bStatus = pCAttribute->GetValue (
  387. reinterpret_cast <PBYTE> (pProxyState),
  388. &dwLength
  389. );
  390. if (FALSE == bStatus) { __leave; }
  391. //
  392. // calculate the check sum
  393. //
  394. bStatus = CalculateCheckSum (
  395. pProxyState,
  396. &dwCalculatedCheckSum
  397. );
  398. if (FALSE == bStatus) { __leave; }
  399. //
  400. // verify the check sum
  401. //
  402. if (dwCalculatedCheckSum != ntohl (pProxyState->dwCheckSum))
  403. {
  404. __leave;
  405. }
  406. //
  407. // verify IP address
  408. //
  409. if (m_dwIPAddress != ntohl (pProxyState->dwIPAddress)) { __leave;}
  410. //
  411. // validation successful
  412. //
  413. bRetVal = TRUE;
  414. }
  415. __finally
  416. {
  417. //
  418. // nothing here for now
  419. //
  420. }
  421. */
  422. return (bRetVal);
  423. } // end of CProxyState::Validate method
  424. //++--------------------------------------------------------------
  425. //
  426. // Function: CalculateCheckSum
  427. //
  428. // Synopsis: This is CProxyState class private method used
  429. // for calculating the checksum over the stream
  430. // of bytes in the proxy state attributes
  431. //
  432. // Algo: We just do the XOR on 2 DWORDs for now
  433. // Arguments:
  434. // [in] PPROXYSTATE
  435. // [out] PDWORD - pdwCheckSum
  436. //
  437. // Returns: NONE
  438. //
  439. //
  440. // History: MKarki Created 10/16/97
  441. //
  442. // Called By: CProxyState::Validate method
  443. //
  444. //----------------------------------------------------------------
  445. BOOL
  446. CProxyState::CalculateCheckSum (
  447. PPROXYSTATE pProxyState,
  448. PDWORD pdwCheckSum
  449. )
  450. {
  451. DWORD dwCount = 0;
  452. BOOL bStatus = FALSE;
  453. BOOL bRetVal = FALSE;
  454. /*
  455. __try
  456. {
  457. if ((NULL == pProxyState) || (NULL == pdwCheckSum)) { __leave; }
  458. *pdwCheckSum = ntohl (pProxyState->dwIPAddress) ^
  459. ntohl (pProxyState->dwIndex);
  460. //
  461. // successfully calculated checksum
  462. //
  463. bRetVal = TRUE;
  464. }
  465. __finally
  466. {
  467. //
  468. // nothing here for now
  469. //
  470. }
  471. */
  472. return (bRetVal);
  473. } // end of CProxyState::CalculateCheckSum method
  474. //++--------------------------------------------------------------
  475. //
  476. // Function: ValidateProxyState
  477. //
  478. // Synopsis: This is CProxyState class public method
  479. // that validates the proxy state returned by the
  480. // server
  481. //
  482. // Arguments: [in] - CPacketRadius*
  483. // [out] PBYTE - Proxy Request Authenticator
  484. //
  485. // Returns: BOOL - status
  486. //
  487. //
  488. // History: MKarki Created 9/28/97
  489. //
  490. // Called By: CValidatorProxyPkt::ValidateInPacket method
  491. //
  492. //----------------------------------------------------------------
  493. BOOL
  494. CProxyState::ValidateProxyState (
  495. CPacketRadius *pCPacketRadius,
  496. PBYTE pbyReqAuthenticator
  497. )
  498. {
  499. BOOL bStatus = FALSE;
  500. BOOL bRetVal = FALSE;
  501. /*
  502. __try
  503. {
  504. //
  505. // now get the attributes collection from the radius packet
  506. //
  507. pCAttribColl = pCPacketRadius->GetInAttributes ();
  508. //
  509. // get the last proxy attribute, as we append a proxy attribute
  510. // at the end
  511. //
  512. bStatus = pCAttribColl->GetLast (
  513. PROXY_STATE_ATTRIB,
  514. &pCAttrib
  515. );
  516. if (FALSE == bStatus) { __leave; }
  517. //
  518. // validate the proxy state found
  519. //
  520. bStatus = GetProxyStateInfo (
  521. pCAttrib,
  522. pCPacketRadius,
  523. pbyReqAuthenticator
  524. );
  525. if (FALSE == bStatus)
  526. {
  527. //
  528. // if could not validate
  529. // find a new one and try again
  530. //
  531. while (TRUE)
  532. {
  533. bStatus = pCAttribColl->GetPrevious (
  534. PROXY_STATE_ATTRIB,
  535. &pCAttrib
  536. );
  537. if (FALSE == bStatus) { __leave; }
  538. bStatus = GetProxyStateInfo (
  539. pCAttrib,
  540. pCPacketRadius,
  541. pbyReqAuthenticator
  542. );
  543. if (TRUE == bStatus) {break;}
  544. }
  545. }
  546. //
  547. // we have successfully validated the proxy state
  548. //
  549. bRetVal = TRUE;
  550. } // __try
  551. __finally
  552. {
  553. //
  554. // nothing here for now
  555. //
  556. }
  557. */
  558. return (bRetVal);
  559. } // end of CProxyState::ValidateProxyState method