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.

860 lines
24 KiB

  1. ////////////////////////////////////////////////////////////////////////
  2. //
  3. // Module : FrameWork/Nshipsec.cpp
  4. //
  5. // Purpose : Netshell Frame Work for IPSec Implementation.
  6. //
  7. // Developers Name : Bharat/Radhika
  8. //
  9. // History :
  10. //
  11. // Date Author Comments
  12. // 8-10-2001 Bharat Initial Version. V1.0
  13. //
  14. ////////////////////////////////////////////////////////////////////////
  15. #include "nshipsec.h"
  16. //Object to cache the policy store handle
  17. CNshPolStore g_NshPolStoreHandle;
  18. //Object to cache the Policy,filterlist and negpol
  19. CNshPolNegFilData g_NshPolNegFilData;
  20. //Storage Location structure
  21. STORAGELOCATION g_StorageLocation={ {0},{0},IPSEC_REGISTRY_PROVIDER};
  22. HKEY g_hGlobalRegistryKey = HKEY_LOCAL_MACHINE;
  23. HINSTANCE g_hModule = NULL;
  24. PSTA_MM_AUTH_METHODS g_paRootca[MAX_ARGS] = {NULL};
  25. _TCHAR g_wszLastErrorMessage[MAX_STR_LEN] = {0};
  26. PIPSEC_QM_OFFER g_pQmsec[IPSEC_MAX_QM_OFFERS] = {NULL};
  27. PIPSEC_MM_OFFER g_pMmsec[IPSEC_MAX_MM_OFFERS] = {NULL};
  28. void *g_AllocPtr[MAX_ARGS]= {NULL};
  29. _TCHAR g_szMachine[MAX_COMPUTERNAME_LENGTH + 1] = {0};
  30. _TCHAR *g_szDynamicMachine = NULL;
  31. //
  32. //These are the commands other than group...
  33. //
  34. CMD_ENTRY g_TopLevelStaticCommands[] =
  35. {
  36. CREATE_CMD_ENTRY(STATIC_EXPORTPOLICY, HandleStaticExportPolicy),
  37. CREATE_CMD_ENTRY(STATIC_IMPORTPOLICY, HandleStaticImportPolicy),
  38. CREATE_CMD_ENTRY(STATIC_RESTOREDEFAULTS, HandleStaticRestoreDefaults)
  39. };
  40. //
  41. //These are the commands static add group...
  42. //
  43. CMD_ENTRY g_StaticAddCommands[] =
  44. {
  45. CREATE_CMD_ENTRY(STATIC_ADD_FILTER, HandleStaticAddFilter),
  46. CREATE_CMD_ENTRY(STATIC_ADD_FILTERLIST, HandleStaticAddFilterList),
  47. CREATE_CMD_ENTRY(STATIC_ADD_FILTERACTIONS, HandleStaticAddFilterActions),
  48. CREATE_CMD_ENTRY(STATIC_ADD_POLICY, HandleStaticAddPolicy),
  49. CREATE_CMD_ENTRY(STATIC_ADD_RULE, HandleStaticAddRule)
  50. };
  51. //
  52. //These are the commands static set group...
  53. //
  54. CMD_ENTRY g_StaticSetCommands[] =
  55. {
  56. CREATE_CMD_ENTRY(STATIC_SET_FILTERLIST, HandleStaticSetFilterList),
  57. CREATE_CMD_ENTRY(STATIC_SET_FILTERACTIONS, HandleStaticSetFilterActions),
  58. CREATE_CMD_ENTRY(STATIC_SET_POLICY, HandleStaticSetPolicy),
  59. CREATE_CMD_ENTRY(STATIC_SET_RULE, HandleStaticSetRule),
  60. CREATE_CMD_ENTRY(STATIC_SET_STORE , HandleStaticSetStore),
  61. CREATE_CMD_ENTRY(STATIC_SET_DEFAULTRULE, HandleStaticSetDefaultRule),
  62. //CREATE_CMD_ENTRY(STATIC_SET_INTERACTIVE, HandleStaticSetInteractive),
  63. // CREATE_CMD_ENTRY(STATIC_SET_BATCH, HandleStaticSetBatch)
  64. };
  65. //
  66. //These are the commands static delete group...
  67. //
  68. CMD_ENTRY g_StaticDeleteCommands[] =
  69. {
  70. CREATE_CMD_ENTRY(STATIC_DELETE_FILTER, HandleStaticDeleteFilter),
  71. CREATE_CMD_ENTRY(STATIC_DELETE_FILTERLIST, HandleStaticDeleteFilterList),
  72. CREATE_CMD_ENTRY(STATIC_DELETE_FILTERACTIONS, HandleStaticDeleteFilterActions),
  73. CREATE_CMD_ENTRY(STATIC_DELETE_POLICY, HandleStaticDeletePolicy),
  74. CREATE_CMD_ENTRY(STATIC_DELETE_RULE, HandleStaticDeleteRule),
  75. CREATE_CMD_ENTRY(STATIC_DELETE_ALL, HandleStaticDeleteAll)
  76. };
  77. //
  78. //These are the commands static show group...
  79. //
  80. CMD_ENTRY g_StaticShowCommands[] =
  81. {
  82. CREATE_CMD_ENTRY(STATIC_SHOW_FILTERLIST, HandleStaticShowFilterList),
  83. CREATE_CMD_ENTRY(STATIC_SHOW_FILTERACTIONS, HandleStaticShowFilterActions),
  84. CREATE_CMD_ENTRY(STATIC_SHOW_POLICY, HandleStaticShowPolicy),
  85. CREATE_CMD_ENTRY(STATIC_SHOW_RULE, HandleStaticShowRule),
  86. CREATE_CMD_ENTRY(STATIC_SHOW_ALL, HandleStaticShowAll),
  87. CREATE_CMD_ENTRY(STATIC_SHOW_STORE, HandleStaticShowStore),
  88. CREATE_CMD_ENTRY(STATIC_SHOW_GPOASSIGNEDPOLICY, HandleStaticShowGPOAssignedPolicy)
  89. };
  90. //
  91. //Static Grouping commands...
  92. //
  93. CMD_GROUP_ENTRY g_StaticGroups[] =
  94. {
  95. CREATE_CMD_GROUP_ENTRY(STATIC_GROUP_ADD, g_StaticAddCommands),
  96. CREATE_CMD_GROUP_ENTRY(STATIC_GROUP_DELETE, g_StaticDeleteCommands),
  97. CREATE_CMD_GROUP_ENTRY(STATIC_GROUP_SET, g_StaticSetCommands),
  98. CREATE_CMD_GROUP_ENTRY(STATIC_GROUP_SHOW, g_StaticShowCommands)
  99. };
  100. //
  101. // Dynamic Add commands
  102. //
  103. CMD_ENTRY g_DynamicAddCommands[] =
  104. {
  105. CREATE_CMD_ENTRY(DYNAMIC_ADD_QMPOLICY, HandleDynamicAddQMPolicy),
  106. CREATE_CMD_ENTRY(DYNAMIC_ADD_MMPOLICY, HandleDynamicAddMMPolicy),
  107. CREATE_CMD_ENTRY(DYNAMIC_ADD_RULE, HandleDynamicAddRule)
  108. };
  109. //
  110. // Dynamic Set commands
  111. //
  112. CMD_ENTRY g_DynamicSetCommands[] =
  113. {
  114. CREATE_CMD_ENTRY(DYNAMIC_SET_QMPOLICY, HandleDynamicSetQMPolicy),
  115. CREATE_CMD_ENTRY(DYNAMIC_SET_MMPOLICY, HandleDynamicSetMMPolicy),
  116. CREATE_CMD_ENTRY(DYNAMIC_SET_CONFIG, HandleDynamicSetConfig),
  117. CREATE_CMD_ENTRY(DYNAMIC_SET_RULE, HandleDynamicSetRule)
  118. };
  119. //
  120. // Dynamic Delete commands
  121. //
  122. CMD_ENTRY g_DynamicDeleteCommands[] =
  123. {
  124. CREATE_CMD_ENTRY(DYNAMIC_DELETE_QMPOLICY, HandleDynamicDeleteQMPolicy),
  125. CREATE_CMD_ENTRY(DYNAMIC_DELETE_MMPOLICY, HandleDynamicDeleteMMPolicy),
  126. CREATE_CMD_ENTRY(DYNAMIC_DELETE_RULE, HandleDynamicDeleteRule),
  127. CREATE_CMD_ENTRY(DYNAMIC_DELETE_ALL, HandleDynamicDeleteAll)
  128. };
  129. //
  130. // Dynamic Show commands
  131. //
  132. CMD_ENTRY g_DynamicShowCommands[] =
  133. {
  134. CREATE_CMD_ENTRY(DYNAMIC_SHOW_ALL, HandleDynamicShowAll),
  135. CREATE_CMD_ENTRY(DYNAMIC_SHOW_MMPOLICY, HandleDynamicShowMMPolicy),
  136. CREATE_CMD_ENTRY(DYNAMIC_SHOW_QMPOLICY, HandleDynamicShowQMPolicy),
  137. CREATE_CMD_ENTRY(DYNAMIC_SHOW_MMFILTER, HandleDynamicShowMMFilter),
  138. CREATE_CMD_ENTRY(DYNAMIC_SHOW_QMFILTER, HandleDynamicShowQMFilter),
  139. CREATE_CMD_ENTRY(DYNAMIC_SHOW_STATS, HandleDynamicShowStats),
  140. CREATE_CMD_ENTRY(DYNAMIC_SHOW_MMSAS, HandleDynamicShowMMSas),
  141. CREATE_CMD_ENTRY(DYNAMIC_SHOW_QMSAS, HandleDynamicShowQMSas),
  142. CREATE_CMD_ENTRY(DYNAMIC_SHOW_REGKEYS, HandleDynamicShowRegKeys),
  143. CREATE_CMD_ENTRY(DYNAMIC_SHOW_RULE, HandleDynamicShowRule)
  144. };
  145. //
  146. //Dynamic Grouping commands...
  147. //
  148. CMD_GROUP_ENTRY g_DynamicGroups[] =
  149. {
  150. CREATE_CMD_GROUP_ENTRY(DYNAMIC_GROUP_ADD, g_DynamicAddCommands),
  151. CREATE_CMD_GROUP_ENTRY(DYNAMIC_GROUP_SET, g_DynamicSetCommands),
  152. CREATE_CMD_GROUP_ENTRY(DYNAMIC_GROUP_DELETE, g_DynamicDeleteCommands),
  153. CREATE_CMD_GROUP_ENTRY(DYNAMIC_GROUP_SHOW, g_DynamicShowCommands)
  154. };
  155. DWORD
  156. IpsecConnectInternal(
  157. IN LPCWSTR pwszMachine);
  158. ////////////////////////////////////////////////////////////////////////////////////////
  159. //
  160. //Function: GenericStopHelper
  161. //
  162. //Date of Creation: 10-8-2001
  163. //
  164. //Parameters: IN DWORD dwReserved
  165. //
  166. //Return: DWORD
  167. //
  168. //Description: This Function called by Netshell Frame work
  169. // when Helper is stopped. This can be utilized for
  170. // diagnostic purposes. To satisfy the frame work.
  171. //
  172. //Revision History:
  173. //
  174. // Date Author Comments
  175. //
  176. //////////////////////////////////////////////////////////////////////////////////////
  177. DWORD WINAPI GenericStopHelper(IN DWORD dwReserved)
  178. {
  179. return ERROR_SUCCESS;
  180. }
  181. ////////////////////////////////////////////////////////////////////////////////////////
  182. //
  183. // Function : DllMain
  184. //
  185. // Date of Creation: 10-8-2001
  186. //
  187. // Parameters : IN HINSTANCE hinstDLL, // handle to DLL module
  188. // IN DWORD fdwReason, // reason for calling function
  189. // IN LPVOID lpvReserved // reserved
  190. // Return : BOOL
  191. //
  192. // Description : This is an optional method to entry into dll.
  193. // Here we can save the instance handle.
  194. //
  195. // History :
  196. //
  197. // Date Author Comments
  198. //
  199. //////////////////////////////////////////////////////////////////////////////////////
  200. extern "C"
  201. BOOL WINAPI DllMain(
  202. HINSTANCE hinstDLL, // handle to DLL module
  203. DWORD fdwReason, // reason for calling function
  204. PVOID pReserved ) // reserved
  205. {
  206. UNREFERENCED_PARAMETER(pReserved);
  207. if(fdwReason == DLL_PROCESS_ATTACH)
  208. {
  209. //save the HINSTANCE
  210. g_hModule = hinstDLL;
  211. }
  212. return TRUE;
  213. }
  214. /////////////////////////////////////////////////////////////////////////////////////
  215. //
  216. //Function: InitHelperDll
  217. //
  218. //Date of Creation: 10-8-2001
  219. //
  220. ///Parameters: IN DWORD dwNetshVersion,
  221. // OUT PVOID pReserved
  222. //Return: DWORD
  223. //
  224. //Description: This Function called by Netshell Frame work
  225. // at the start up. Registers the contexts.
  226. //
  227. //Revision History:
  228. //
  229. // Date Author Comments
  230. //
  231. ///////////////////////////////////////////////////////////////////////////////////////////
  232. DWORD WINAPI InitHelperDll(
  233. IN DWORD dwNetshVersion,
  234. OUT PVOID pReserved
  235. )
  236. {
  237. DWORD dwReturn = ERROR_SUCCESS;
  238. NS_HELPER_ATTRIBUTES MyAttributes;
  239. if(g_hModule == NULL)
  240. {
  241. _tprintf(_TEXT("\n nshipsec.dll handle not available, not registering the IPSec Helper.\n"));
  242. BAIL_OUT;
  243. }
  244. ZeroMemory(&MyAttributes, sizeof(MyAttributes));
  245. MyAttributes.dwVersion = IPSEC_HELPER_VERSION;
  246. MyAttributes.pfnStart = StartHelpers;
  247. MyAttributes.pfnStop = GenericStopHelper;
  248. //
  249. // Set the GUID of IPSec helper.
  250. //
  251. MyAttributes.guidHelper = g_IPSecGuid;
  252. //
  253. // Specify g_RootGuid as the parent helper to indicate
  254. // that any contexts registered by this helper will be top
  255. // level contexts.
  256. //
  257. dwReturn = RegisterHelper(&g_RootGuid,&MyAttributes);
  258. if (dwReturn != ERROR_SUCCESS)
  259. {
  260. BAIL_OUT;
  261. }
  262. //
  263. // Set the GUID for Static Sub context.
  264. //
  265. MyAttributes.guidHelper = g_StaticGuid;
  266. dwReturn = RegisterHelper(&g_IPSecGuid, &MyAttributes);
  267. if(dwReturn != ERROR_SUCCESS)
  268. {
  269. BAIL_OUT;
  270. }
  271. //
  272. // Set the GUID of Dynamic Sub context...
  273. //
  274. MyAttributes.guidHelper = g_DynamicGuid;
  275. dwReturn = RegisterHelper(&g_IPSecGuid, &MyAttributes);
  276. IpsecConnectInternal(NULL);
  277. error:
  278. return dwReturn;
  279. }
  280. ///////////////////////////////////////////////////////////////////////////////////////
  281. //
  282. //Function: StartHelpers
  283. //
  284. //Date of Creation: 10-8-2001
  285. //
  286. //Parameters: IN CONST GUID * pguidParent,
  287. // IN DWORD dwVersion
  288. //Return: DWORD
  289. //
  290. //Description: This Function called by Netshell Frame work,
  291. // at the start up and as enters to every context.
  292. //
  293. //Revision History:
  294. //
  295. // Date Author Comments
  296. //
  297. ///////////////////////////////////////////////////////////////////////////////////////
  298. DWORD WINAPI StartHelpers(
  299. IN CONST GUID * pguidParent,
  300. IN DWORD dwVersion
  301. )
  302. {
  303. DWORD dwReturn = ERROR_INVALID_PARAMETER;
  304. NS_CONTEXT_ATTRIBUTES ContextAttributes;
  305. ZeroMemory(&ContextAttributes,sizeof(ContextAttributes));
  306. ContextAttributes.dwVersion = IPSEC_HELPER_VERSION;
  307. if (IsEqualGUID(*pguidParent, g_RootGuid))
  308. {
  309. ContextAttributes.dwFlags = 0;
  310. ContextAttributes.dwVersion = 1;
  311. ContextAttributes.ulPriority = DEFAULT_CONTEXT_PRIORITY;
  312. ContextAttributes.pwszContext = TOKEN_IPSEC;
  313. ContextAttributes.guidHelper = g_IPSecGuid;
  314. ContextAttributes.ulNumTopCmds = 0;
  315. ContextAttributes.pTopCmds = NULL;
  316. ContextAttributes.ulNumGroups = 0;
  317. ContextAttributes.pCmdGroups = NULL;
  318. ContextAttributes.pfnCommitFn = NULL;
  319. ContextAttributes.pfnConnectFn = NULL;
  320. ContextAttributes.pfnDumpFn = NULL;
  321. ContextAttributes.pfnOsVersionCheck = CheckOsVersion;
  322. //
  323. //Registering IPSec Main Context...
  324. //
  325. dwReturn = RegisterContext(&ContextAttributes);
  326. }
  327. else if (IsEqualGUID(*pguidParent, g_IPSecGuid))
  328. {
  329. //
  330. //Registering SubContexts under IPSec Main Context...
  331. //
  332. ContextAttributes.dwFlags = 0;
  333. ContextAttributes.dwVersion = 1;
  334. ContextAttributes.ulPriority = DEFAULT_CONTEXT_PRIORITY;
  335. ContextAttributes.pwszContext = TOKEN_STATIC;
  336. ContextAttributes.guidHelper = g_StaticGuid;
  337. ContextAttributes.ulNumTopCmds = sizeof(g_TopLevelStaticCommands)/sizeof(CMD_ENTRY);
  338. ContextAttributes.pTopCmds = (CMD_ENTRY (*)[])g_TopLevelStaticCommands;
  339. ContextAttributes.ulNumGroups = sizeof(g_StaticGroups)/sizeof(CMD_GROUP_ENTRY);
  340. ContextAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_StaticGroups;
  341. ContextAttributes.pfnCommitFn = NULL;
  342. ContextAttributes.pfnConnectFn = IpsecConnect;
  343. ContextAttributes.pfnOsVersionCheck = CheckOsVersion;
  344. ContextAttributes.pfnDumpFn = NULL;
  345. //
  346. //Registering Static SubContext
  347. //...
  348. dwReturn = RegisterContext(&ContextAttributes);
  349. //even if static sub context not succeeds,
  350. //proceed to register the dynamic context
  351. ContextAttributes.dwFlags = 0;
  352. ContextAttributes.dwVersion = 1;
  353. ContextAttributes.ulPriority = DEFAULT_CONTEXT_PRIORITY;
  354. ContextAttributes.pwszContext = TOKEN_DYNAMIC;
  355. ContextAttributes.guidHelper = g_DynamicGuid;
  356. ContextAttributes.ulNumTopCmds = sizeof(g_DynamicGroups)/sizeof(CMD_ENTRY);
  357. ContextAttributes.pTopCmds = (CMD_ENTRY (*)[])g_DynamicGroups;
  358. ContextAttributes.ulNumGroups = sizeof(g_DynamicGroups)/sizeof(CMD_GROUP_ENTRY);
  359. ContextAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_DynamicGroups;
  360. ContextAttributes.pfnCommitFn = NULL;
  361. ContextAttributes.pfnConnectFn = IpsecConnect;
  362. ContextAttributes.pfnOsVersionCheck = CheckOsVersion;
  363. ContextAttributes.pfnDumpFn = NULL;
  364. //
  365. //Registering Dynamic Sub context...
  366. //
  367. dwReturn = RegisterContext(&ContextAttributes);
  368. }
  369. return dwReturn;
  370. }
  371. DWORD
  372. IpsecConnectInternal(
  373. IN LPCWSTR pwszMachine)
  374. {
  375. DWORD dwReturn, dwReturn2;
  376. if(pwszMachine)
  377. {
  378. _tcsncpy(g_szMachine, pwszMachine, MAX_COMPUTERNAME_LENGTH);
  379. g_szMachine[MAX_COMPUTERNAME_LENGTH] = '\0';
  380. }
  381. else
  382. {
  383. g_szMachine[0] = '\0';
  384. }
  385. g_szDynamicMachine = (_TCHAR*)g_szMachine;
  386. // Have the static and dynamic contexts connect to the specified
  387. // machine. Return an error if either attempt fails.
  388. //
  389. dwReturn = ConnectStaticMachine(g_szMachine, g_StorageLocation.dwLocation);
  390. dwReturn2 = ConnectDynamicMachine(g_szDynamicMachine);
  391. dwReturn = (dwReturn) ? dwReturn : dwReturn2;
  392. return dwReturn;
  393. }
  394. //////////////////////////////////////////////////////////////////////////////////////////////////////
  395. //
  396. //Function: IpsecConnect
  397. //
  398. //Date of Creation: October 4th 2001
  399. //
  400. //Parameters: IN LPCWSTR pwszMachine
  401. //
  402. //Return: DWORD
  403. //
  404. //Description: Displays Win32 Error message in locale language for
  405. // given Win 32Error Code.
  406. //
  407. //Revision History:
  408. //
  409. // Date Author Comments
  410. //
  411. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  412. DWORD WINAPI
  413. IpsecConnect( IN LPCWSTR pwszMachine )
  414. {
  415. DWORD dwReturn = ERROR_SUCCESS, dwReturn2 = ERROR_SUCCESS;
  416. BOOL bSetMachine = FALSE;
  417. if((pwszMachine != NULL) && (g_szMachine[0] == '\0'))
  418. {
  419. bSetMachine = TRUE;
  420. }
  421. if((pwszMachine == NULL) && (g_szMachine[0] != '\0'))
  422. {
  423. bSetMachine = TRUE;
  424. }
  425. if((pwszMachine != NULL) && (g_szMachine[0] != '\0'))
  426. {
  427. if(_tcscmp(pwszMachine, g_szMachine) != 0)
  428. {
  429. bSetMachine = TRUE;
  430. }
  431. }
  432. if(bSetMachine)
  433. {
  434. dwReturn = IpsecConnectInternal( pwszMachine );
  435. }
  436. return dwReturn;
  437. }
  438. //////////////////////////////////////////////////////////////////////////////
  439. //
  440. //Function: PrintErrorMessage()
  441. //
  442. //Date of Creation: October 4th 2001
  443. //
  444. //Parameters:
  445. // IN DWORD dwErrorType,
  446. // IN DWORD dwWin32ErrorCode,
  447. // IN DWORD dwIpsecErrorCode,
  448. // ...
  449. //
  450. //
  451. //Return: DWORD
  452. //
  453. //Description: Prints the IPSEC and WIN32 error messages.
  454. //
  455. //Revision History:
  456. //
  457. // Date Author Comments
  458. //
  459. //////////////////////////////////////////////////////////////////////////////
  460. void PrintErrorMessage(IN DWORD dwErrorType,
  461. IN DWORD dwWin32ErrorCode,
  462. IN DWORD dwIpsecErrorCode,
  463. ...)
  464. {
  465. va_list arg_ptr;
  466. BOOL bFound = FALSE;
  467. DWORD i,dwStatus = 0;
  468. DWORD dwRcIndex = 0xFFFF;
  469. LPVOID szWin32Msg = NULL;
  470. DWORD dwMaxErrMsg = sizeof(ERROR_RC)/sizeof(ERROR_TO_RC);
  471. for(i=0;i<dwMaxErrMsg;i++)
  472. {
  473. if (dwIpsecErrorCode == ERROR_RC[i].dwErrCode)
  474. {
  475. bFound = TRUE;
  476. dwRcIndex = ERROR_RC[i].dwRcCode;
  477. break;
  478. }
  479. }
  480. if(dwWin32ErrorCode == ERROR_OUTOFMEMORY)
  481. {
  482. PrintMessageFromModule(g_hModule,ERR_OUTOF_MEMORY);
  483. BAIL_OUT;
  484. }
  485. switch (dwErrorType)
  486. {
  487. case WIN32_ERR :
  488. dwStatus = FormatMessageW(
  489. FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  490. NULL,
  491. dwWin32ErrorCode,
  492. 0, // Default country ID.
  493. (LPWSTR)&szWin32Msg,
  494. 0,
  495. NULL);
  496. if (dwStatus)
  497. {
  498. PrintMessageFromModule(g_hModule,ERR_WIN32_FMT,dwWin32ErrorCode,szWin32Msg);
  499. UpdateGetLastError((LPWSTR)szWin32Msg);
  500. }
  501. else
  502. {
  503. UpdateGetLastError(_TEXT("ERR Win32\n"));
  504. PrintMessageFromModule(g_hModule,ERR_WIN32_INVALID_WIN32CODE,dwWin32ErrorCode);
  505. }
  506. break;
  507. case IPSEC_ERR :
  508. if (!bFound)
  509. {
  510. PrintMessageFromModule(g_hModule, DYNAMIC_SHOW_NEWLINE);
  511. }
  512. else
  513. {
  514. PrintMessageFromModule(g_hModule,ERR_IPSEC_FMT,dwIpsecErrorCode);
  515. va_start(arg_ptr,dwIpsecErrorCode);
  516. PrintErrorMessageFromModule(g_hModule,dwRcIndex,&arg_ptr);
  517. va_end(arg_ptr);
  518. }
  519. break;
  520. default :
  521. break;
  522. }
  523. if(szWin32Msg != NULL)
  524. {
  525. LocalFree(szWin32Msg);
  526. }
  527. error:
  528. return;
  529. }
  530. //////////////////////////////////////////////////////////////////////////////
  531. //
  532. //Function: DisplayErrorMessage()
  533. //
  534. //Date of Creation:October 4th 2001
  535. //
  536. //Parameters:
  537. // IN LPCWSTR pwszFormat,
  538. // IN va_list *parglist
  539. //
  540. //
  541. //Return: DWORD
  542. //
  543. //Description:Displays error message and updates the last error
  544. //
  545. //Revision History:
  546. //
  547. // Date Author Comments
  548. //
  549. //////////////////////////////////////////////////////////////////////////////
  550. DWORD
  551. DisplayErrorMessage(
  552. IN LPCWSTR pwszFormat,
  553. IN va_list *parglist
  554. )
  555. {
  556. DWORD dwMsgLen = 0;
  557. LPWSTR pwszOutput = NULL;
  558. do
  559. {
  560. dwMsgLen = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_STRING,
  561. pwszFormat,
  562. 0,
  563. 0L,
  564. (LPWSTR)&pwszOutput,
  565. 0,
  566. parglist);
  567. if((dwMsgLen == 0) || (pwszOutput == NULL))
  568. {
  569. BAIL_OUT;
  570. }
  571. PrintMessageFromModule(g_hModule, DYNAMIC_SHOW_PADD, pwszOutput);
  572. UpdateGetLastError(pwszOutput);
  573. } while ( FALSE );
  574. if ( pwszOutput)
  575. {
  576. LocalFree( pwszOutput );
  577. }
  578. error:
  579. return dwMsgLen;
  580. }
  581. //////////////////////////////////////////////////////////////////////////////
  582. //
  583. //Function: PrintErrorMessageFromModule()
  584. //
  585. //Date of Creation: October 4th 2001
  586. //
  587. //Parameters:
  588. // IN HANDLE hModule,
  589. // IN DWORD dwMsgId,
  590. // IN va_list *parglist
  591. //
  592. //
  593. //Return: DWORD
  594. //
  595. //Description: Prints the error message
  596. //
  597. //Revision History:
  598. //
  599. // Date Author Comments
  600. //
  601. //////////////////////////////////////////////////////////////////////////////
  602. DWORD
  603. PrintErrorMessageFromModule(
  604. IN HANDLE hModule,
  605. IN DWORD dwMsgId,
  606. IN va_list *parglist
  607. )
  608. {
  609. WCHAR rgwcInput[MAX_STR_LEN + 1] = {0};
  610. if ( !LoadStringW(g_hModule,
  611. dwMsgId,
  612. rgwcInput,
  613. MAX_STR_LEN) )
  614. {
  615. return 0;
  616. }
  617. return DisplayErrorMessage(rgwcInput, parglist);
  618. }
  619. //////////////////////////////////////////////////////////////////////////////
  620. //
  621. //Function: UpdateGetLastError()
  622. //
  623. //Date of Creation: October 4th 2001
  624. //
  625. //Parameters:
  626. // IN LPWSTR pwszOutput
  627. //
  628. //Return: VOID
  629. //
  630. //Description: Updates the contents of the global string for GetLastErrorMessage
  631. // If the operation was success, empty string to be passed to the
  632. // UpdateGetLastError function.
  633. //
  634. //Revision History:
  635. //
  636. // Date Author Comments
  637. //
  638. //////////////////////////////////////////////////////////////////////////////
  639. VOID
  640. UpdateGetLastError(LPWSTR pwszOutput)
  641. {
  642. if (pwszOutput)
  643. {
  644. _tcsncpy(g_wszLastErrorMessage,pwszOutput,MAX_STR_LEN-1);
  645. }
  646. else
  647. {
  648. _tcsncpy(g_wszLastErrorMessage,_TEXT(""), _tcslen(_TEXT(""))+1); // Operation Ok.
  649. }
  650. }
  651. //////////////////////////////////////////////////////////////////////////////
  652. //
  653. //Function: GetIpsecLastError()
  654. //
  655. //Date of Creation: October 4th 2001
  656. //
  657. //Parameters:
  658. // IN VOID
  659. //
  660. //Return: LPWSTR
  661. //
  662. //Description: Returns the error message for the last operation, If the last operation
  663. // was success returns NULL
  664. //
  665. //Revision History:
  666. //
  667. // Date Author Comments
  668. //
  669. //////////////////////////////////////////////////////////////////////////////
  670. LPCWSTR
  671. GetIpsecLastError(VOID)
  672. {
  673. LPTSTR wszLastErrorMessage = NULL;
  674. if(_tcscmp(g_wszLastErrorMessage, _TEXT("")) != 0)
  675. {
  676. wszLastErrorMessage = g_wszLastErrorMessage;
  677. }
  678. return (LPCWSTR)wszLastErrorMessage;
  679. }
  680. //////////////////////////////////////////////////////////////////////////////
  681. //
  682. //Function: CheckOsVersion
  683. //
  684. //Date of Creation: 10-8-2001
  685. //
  686. //Parameters: IN UINT CIMOSType,
  687. // IN UINT CIMOSProductSuite,
  688. // IN LPCWSTR CIMOSVersion,
  689. // IN LPCWSTR CIMOSBuildNumber,
  690. // IN LPCWSTR CIMServicePackMajorVersion,
  691. // IN LPCWSTR CIMServicePackMinorVersion,
  692. // IN UINT CIMProcessorArchitecture,
  693. // IN DWORD dwReserved
  694. //Return: BOOL
  695. //
  696. //Description: This Function called by Netshell Frame work
  697. // for every command. This can be utilized for
  698. // diagnostic purposes. To satisfy the frame work.
  699. //
  700. //Revision History:
  701. //
  702. // Date Author Comments
  703. //
  704. //////////////////////////////////////////////////////////////////////////////
  705. BOOL
  706. WINAPI CheckOsVersion(
  707. IN UINT CIMOSType,
  708. IN UINT CIMOSProductSuite,
  709. IN LPCWSTR CIMOSVersion,
  710. IN LPCWSTR CIMOSBuildNumber,
  711. IN LPCWSTR CIMServicePackMajorVersion,
  712. IN LPCWSTR CIMServicePackMinorVersion,
  713. IN UINT CIMProcessorArchitecture,
  714. IN DWORD dwReserved
  715. )
  716. {
  717. DWORD dwStatus =0;
  718. DWORD dwBuildNumber=0;
  719. static BOOL bDisplayOnce = FALSE;
  720. dwBuildNumber = _ttol(CIMOSBuildNumber);
  721. if (dwStatus)
  722. if(dwBuildNumber < NSHIPSEC_BUILD_NUMBER)
  723. {
  724. if (!bDisplayOnce)
  725. {
  726. PrintMessageFromModule(g_hModule,NSHIPSEC_CHECK,NSHIPSEC_BUILD_NUMBER);
  727. bDisplayOnce = TRUE;
  728. }
  729. return FALSE;
  730. }
  731. return TRUE;
  732. }
  733. VOID
  734. CleanupAuthMethod(
  735. PSTA_AUTH_METHODS *ppAuthMethod,
  736. BOOL bIsArray
  737. )
  738. {
  739. if (ppAuthMethod && *ppAuthMethod)
  740. {
  741. CleanupMMAuthMethod(&((*ppAuthMethod)->pAuthMethodInfo), bIsArray);
  742. delete *ppAuthMethod;
  743. *ppAuthMethod = NULL;
  744. }
  745. }
  746. VOID
  747. CleanupMMAuthMethod(
  748. PSTA_MM_AUTH_METHODS *ppMMAuth,
  749. BOOL bIsArray
  750. )
  751. {
  752. if (*ppMMAuth)
  753. {
  754. if (bIsArray)
  755. {
  756. delete [] *ppMMAuth;
  757. }
  758. else
  759. {
  760. if ((*ppMMAuth)->pAuthenticationInfo)
  761. {
  762. if ((*ppMMAuth)->pAuthenticationInfo->pAuthInfo)
  763. {
  764. delete (*ppMMAuth)->pAuthenticationInfo->pAuthInfo;
  765. }
  766. delete (*ppMMAuth)->pAuthenticationInfo;
  767. }
  768. delete *ppMMAuth;
  769. }
  770. *ppMMAuth = NULL;
  771. }
  772. }
  773. VOID
  774. CleanupAuthData(
  775. PSTA_AUTH_METHODS *ppKerbAuth,
  776. PSTA_AUTH_METHODS *ppPskAuth,
  777. PSTA_MM_AUTH_METHODS *ppRootcaMMAuth
  778. )
  779. {
  780. CleanupAuthMethod(ppKerbAuth);
  781. CleanupAuthMethod(ppPskAuth);
  782. if (ppRootcaMMAuth)
  783. {
  784. CleanupMMAuthMethod(ppRootcaMMAuth, TRUE);
  785. }
  786. }