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.

4430 lines
122 KiB

  1. ////////////////////////////////////////////////////////////
  2. // Module : Static/Staticadd.cpp
  3. //
  4. // Purpose : Static Add Implementation.
  5. //
  6. // Developers Name : Surya
  7. //
  8. // History :
  9. //
  10. // Date Author Comments
  11. //
  12. ////////////////////////////////////////////////////////////
  13. #include "nshipsec.h"
  14. extern HINSTANCE g_hModule;
  15. extern CNshPolStore g_NshPolStoreHandle;
  16. extern STORAGELOCATION g_StorageLocation;
  17. extern CNshPolNegFilData g_NshPolNegFilData;
  18. ////////////////////////////////////////////////////////////
  19. //
  20. //Function: HandleStaticAddPolicy()
  21. //
  22. //Date of Creation: 21st Aug 2001
  23. //
  24. //Parameters:
  25. // IN LPCWSTR pwszMachine,
  26. // IN OUT LPWSTR *ppwcArguments,
  27. // IN DWORD dwCurrentIndex,
  28. // IN DWORD dwArgCount,
  29. // IN DWORD dwFlags,
  30. // IN LPCVOID pvData,
  31. // OUT BOOL *pbDone
  32. //
  33. //Return: DWORD
  34. //
  35. //Description:
  36. // Implementation for the command "Add Policy"
  37. //
  38. //Revision History:
  39. //
  40. // Date Author Comments
  41. //
  42. ////////////////////////////////////////////////////////////
  43. DWORD WINAPI
  44. HandleStaticAddPolicy(
  45. IN LPCWSTR pwszMachine,
  46. IN OUT LPWSTR *ppwcArguments,
  47. IN DWORD dwCurrentIndex,
  48. IN DWORD dwArgCount,
  49. IN DWORD dwFlags,
  50. IN LPCVOID pvData,
  51. OUT BOOL *pbDone
  52. )
  53. {
  54. LPTSTR pszMachineName=NULL;
  55. DWORD dwReturn=ERROR_SUCCESS;
  56. DWORD dwReturnCode = ERROR_SHOW_USAGE;
  57. PPOLICYDATA pPolicyData=NULL;
  58. PARSER_PKT parser;
  59. ZeroMemory(&parser, sizeof(parser));
  60. const TAG_TYPE vcmdStaticAddPolicy[] =
  61. {
  62. { CMD_TOKEN_STR_NAME, NS_REQ_PRESENT, FALSE },
  63. { CMD_TOKEN_STR_DESCR, NS_REQ_ZERO, FALSE },
  64. { CMD_TOKEN_STR_MMPFS, NS_REQ_ZERO, FALSE },
  65. { CMD_TOKEN_STR_QMPERMM, NS_REQ_ZERO, FALSE },
  66. { CMD_TOKEN_STR_MMLIFETIME, NS_REQ_ZERO, FALSE },
  67. { CMD_TOKEN_STR_ACTIVATEDEFRULE,NS_REQ_ZERO, FALSE },
  68. { CMD_TOKEN_STR_PI, NS_REQ_ZERO, FALSE },
  69. { CMD_TOKEN_STR_ASSIGN, NS_REQ_ZERO, FALSE },
  70. { CMD_TOKEN_STR_MMSECMETHODS, NS_REQ_ZERO, FALSE }
  71. };
  72. const TOKEN_VALUE vtokStaticAddPolicy[] =
  73. {
  74. { CMD_TOKEN_STR_NAME, CMD_TOKEN_NAME },
  75. { CMD_TOKEN_STR_DESCR, CMD_TOKEN_DESCR },
  76. { CMD_TOKEN_STR_MMPFS, CMD_TOKEN_MMPFS },
  77. { CMD_TOKEN_STR_QMPERMM, CMD_TOKEN_QMPERMM },
  78. { CMD_TOKEN_STR_MMLIFETIME, CMD_TOKEN_MMLIFETIME },
  79. { CMD_TOKEN_STR_ACTIVATEDEFRULE,CMD_TOKEN_ACTIVATEDEFRULE },
  80. { CMD_TOKEN_STR_PI, CMD_TOKEN_PI },
  81. { CMD_TOKEN_STR_ASSIGN, CMD_TOKEN_ASSIGN },
  82. { CMD_TOKEN_STR_MMSECMETHODS, CMD_TOKEN_MMSECMETHODS }
  83. };
  84. //if the user asked for usage, delegate the responsibility to netsh
  85. if(dwArgCount <= 3)
  86. {
  87. dwReturnCode = ERROR_SHOW_USAGE;
  88. BAIL_OUT;
  89. }
  90. parser.ValidTok = vtokStaticAddPolicy;
  91. parser.MaxTok = SIZEOF_TOKEN_VALUE(vtokStaticAddPolicy);
  92. parser.ValidCmd = vcmdStaticAddPolicy;
  93. parser.MaxCmd = SIZEOF_TAG_TYPE(vcmdStaticAddPolicy);
  94. dwReturnCode = Parser(pwszMachine,ppwcArguments,dwCurrentIndex,dwArgCount,&parser);
  95. if(dwReturnCode != ERROR_SUCCESS)
  96. {
  97. CleanUp();
  98. if (dwReturnCode == RETURN_NO_ERROR)
  99. {
  100. dwReturnCode = ERROR_SUCCESS;
  101. }
  102. BAIL_OUT;
  103. }
  104. //Get the User specified info into local structure
  105. dwReturn = FillAddPolicyInfo(&pPolicyData,parser,vtokStaticAddPolicy);
  106. if(dwReturn==ERROR_OUTOFMEMORY)
  107. {
  108. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  109. dwReturnCode=ERROR_SUCCESS;
  110. BAIL_OUT;
  111. }
  112. if (!pPolicyData->pszPolicyName || (pPolicyData->pszPolicyName[0] == TEXT('\0')) )
  113. {
  114. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_POLICY_MISSING_POL_NAME);
  115. dwReturnCode=ERROR_SHOW_USAGE;
  116. BAIL_OUT;
  117. }
  118. // check the limits of variables
  119. if(pPolicyData->bPollIntervalSpecified && !IsWithinLimit(pPolicyData->dwPollInterval/60,POLLING_Min_MIN,POLLING_Min_MAX))
  120. {
  121. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_POLICY_POLL_INTERVAL_MSG,POLLING_Min_MIN,POLLING_Min_MAX);
  122. dwReturnCode=ERROR_SUCCESS;
  123. BAIL_OUT;
  124. }
  125. if(pPolicyData->bQMLimitSpecified && !IsWithinLimit(pPolicyData->dwQMLimit,QMPERMM_MIN,QMPERMM_MAX))
  126. {
  127. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_POLICY_QMPERMM_MSG,QMPERMM_MIN,QMPERMM_MAX);
  128. dwReturnCode=ERROR_SUCCESS;
  129. BAIL_OUT;
  130. }
  131. if(pPolicyData->bLifeTimeInsecondsSpecified && !IsWithinLimit(pPolicyData->LifeTimeInSeconds/60,P1_Min_LIFE_MIN,P1_Min_LIFE_MAX))
  132. {
  133. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_POLICY_LIFETIME_LIMIT_MSG,P1_Min_LIFE_MIN,P1_Min_LIFE_MAX);
  134. dwReturnCode=ERROR_SUCCESS;
  135. BAIL_OUT;
  136. }
  137. if(pPolicyData)
  138. {
  139. dwReturnCode = CreateNewPolicy(pPolicyData);
  140. if(dwReturnCode == ERROR_OUTOFMEMORY)
  141. {
  142. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  143. }
  144. else if (dwReturnCode == ERROR_INVALID_PARAMETER)
  145. {
  146. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_STATIC_INTERNAL_ERROR);
  147. }
  148. dwReturnCode=ERROR_SUCCESS;
  149. }
  150. error:
  151. //clean up
  152. CleanUpLocalPolicyDataStructure(pPolicyData);
  153. if (pszMachineName)
  154. {
  155. delete [] pszMachineName;
  156. }
  157. return dwReturnCode;
  158. }
  159. /////////////////////////////////////////////////////////////
  160. //
  161. //Function: CreateNewPolicy()
  162. //
  163. //Date of Creation: 21st Aug 2001
  164. //
  165. //Parameters:
  166. // IN PPOLICYDATA pPolicyData
  167. //
  168. //Return: DWORD
  169. //
  170. //Description:
  171. // Creates new policy data structure and calls the API
  172. //
  173. //Revision History:
  174. //
  175. // Date Author Comments
  176. //
  177. ////////////////////////////////////////////////////////////
  178. DWORD
  179. CreateNewPolicy(
  180. IN PPOLICYDATA pPolicyData
  181. )
  182. {
  183. HANDLE hPolicyStorage = NULL;
  184. LPTSTR pszMachineName=NULL;
  185. DWORD dwReturnCode = ERROR_SUCCESS;
  186. PIPSEC_POLICY_DATA pPolicy=NULL,pActive=NULL;
  187. RPC_STATUS RpcStat =RPC_S_OK;
  188. BOOL bExists=FALSE;
  189. // if no offer , fill defaults
  190. if(pPolicyData->dwOfferCount==0)
  191. {
  192. dwReturnCode = LoadIkeDefaults(pPolicyData,&(pPolicyData->pIpSecMMOffer));
  193. BAIL_ON_WIN32_ERROR(dwReturnCode);
  194. }
  195. if(pPolicyData->bPFSSpecified && pPolicyData->bPFS)
  196. {
  197. pPolicyData->dwQMLimit=MMPFS_QM_LIMIT;
  198. }
  199. if(!pPolicyData->bLifeTimeInsecondsSpecified)
  200. {
  201. pPolicyData->LifeTimeInSeconds= P2STORE_DEFAULT_LIFETIME;
  202. }
  203. if(!pPolicyData->bPollIntervalSpecified)
  204. {
  205. pPolicyData->dwPollInterval=P2STORE_DEFAULT_POLLINT;
  206. }
  207. if (pPolicyData->dwOfferCount != 0)
  208. {
  209. for(DWORD i=0;i<pPolicyData->dwOfferCount;i++)
  210. {
  211. pPolicyData->pIpSecMMOffer[i].Lifetime.uKeyExpirationTime=pPolicyData->LifeTimeInSeconds;
  212. pPolicyData->pIpSecMMOffer[i].dwQuickModeLimit=pPolicyData->dwQMLimit;
  213. }
  214. }
  215. if(pPolicyData->pszGPOName)
  216. {
  217. if (g_StorageLocation.dwLocation != IPSEC_DIRECTORY_PROVIDER)
  218. {
  219. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SET_STATIC_POLICY_GPO_SPECIFIED_ON_NODOMAIN_POLICY);
  220. dwReturnCode= ERROR_SHOW_USAGE;
  221. BAIL_OUT;
  222. }
  223. }
  224. // this wrapper function is for cache
  225. dwReturnCode = OpenPolicyStore(&hPolicyStorage);
  226. if (dwReturnCode != ERROR_SUCCESS)
  227. {
  228. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_FAILED_POLSTORE_OPEN);
  229. dwReturnCode = ERROR_SUCCESS;
  230. BAIL_OUT;
  231. }
  232. //check whether the policy exists in cache
  233. if(g_NshPolStoreHandle.GetBatchmodeStatus() && g_NshPolNegFilData.CheckPolicyInCacheByName(pPolicyData->pszPolicyName))
  234. {
  235. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_CRNEWPOL_1,pPolicyData->pszPolicyName);
  236. dwReturnCode=ERROR_INVALID_DATA;
  237. bExists=TRUE;
  238. }
  239. else if (CheckPolicyExistance(hPolicyStorage,pPolicyData->pszPolicyName))
  240. {
  241. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_CRNEWPOL_1,pPolicyData->pszPolicyName);
  242. dwReturnCode=ERROR_INVALID_DATA;
  243. bExists=TRUE;
  244. }
  245. if(!bExists) // if policy not exists, process further
  246. {
  247. pPolicy = (PIPSEC_POLICY_DATA) IPSecAllocPolMem(sizeof(IPSEC_POLICY_DATA));
  248. if (pPolicy == NULL)
  249. {
  250. dwReturnCode=ERROR_OUTOFMEMORY;
  251. BAIL_OUT;
  252. }
  253. pPolicy->pszIpsecName = IPSecAllocPolStr(pPolicyData->pszPolicyName);
  254. if (pPolicy->pszIpsecName == NULL)
  255. {
  256. dwReturnCode=ERROR_OUTOFMEMORY;
  257. BAIL_OUT;
  258. }
  259. if (pPolicyData->pszDescription)
  260. {
  261. pPolicy->pszDescription = IPSecAllocPolStr(pPolicyData->pszDescription);
  262. if (pPolicy->pszDescription == NULL)
  263. {
  264. dwReturnCode=ERROR_OUTOFMEMORY;
  265. BAIL_OUT;
  266. }
  267. }
  268. else
  269. {
  270. pPolicy->pszDescription = NULL;
  271. }
  272. pPolicy->dwPollingInterval = pPolicyData->dwPollInterval;
  273. RpcStat = UuidCreate(&(pPolicy->PolicyIdentifier));
  274. if (!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  275. {
  276. dwReturnCode=ERROR_INVALID_PARAMETER;
  277. BAIL_OUT;
  278. }
  279. pPolicy->pIpsecISAKMPData = NULL;
  280. pPolicy->ppIpsecNFAData = NULL;
  281. pPolicy->dwNumNFACount = 0;
  282. pPolicy->dwWhenChanged = 0;
  283. RpcStat = UuidCreate(&(pPolicy->ISAKMPIdentifier));
  284. if (!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  285. {
  286. dwReturnCode=ERROR_INVALID_PARAMETER;
  287. BAIL_OUT;
  288. }
  289. //deal with ISAKMP details
  290. pPolicy->pIpsecISAKMPData = (PIPSEC_ISAKMP_DATA) IPSecAllocPolMem(sizeof(IPSEC_ISAKMP_DATA));
  291. if(pPolicy->pIpsecISAKMPData==NULL)
  292. {
  293. dwReturnCode=ERROR_OUTOFMEMORY;
  294. BAIL_OUT;
  295. }
  296. memset(pPolicy->pIpsecISAKMPData,0,sizeof(IPSEC_ISAKMP_DATA));
  297. pPolicy->pIpsecISAKMPData->ISAKMPIdentifier = pPolicy->ISAKMPIdentifier;
  298. pPolicy->pIpsecISAKMPData->dwWhenChanged = 0;
  299. // sec methods details
  300. pPolicy->pIpsecISAKMPData->dwNumISAKMPSecurityMethods = pPolicyData->dwOfferCount;
  301. pPolicy->pIpsecISAKMPData->pSecurityMethods = (PCRYPTO_BUNDLE) IPSecAllocPolMem(sizeof(CRYPTO_BUNDLE)*pPolicyData->dwOfferCount);
  302. if(pPolicy->pIpsecISAKMPData->pSecurityMethods==NULL)
  303. {
  304. dwReturnCode=ERROR_OUTOFMEMORY;
  305. BAIL_OUT;
  306. }
  307. // fill the sec methods
  308. for (DWORD i = 0; i < pPolicyData->dwOfferCount; i++)
  309. {
  310. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].MajorVersion = 0;
  311. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].MinorVersion = 0;
  312. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].AuthenticationMethod = 0;
  313. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].PseudoRandomFunction.AlgorithmIdentifier = 0;
  314. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].PseudoRandomFunction.KeySize = 0;
  315. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].PseudoRandomFunction.Rounds = 0;
  316. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].PfsIdentityRequired = pPolicyData->bPFS;
  317. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].EncryptionAlgorithm.AlgorithmIdentifier = pPolicyData->pIpSecMMOffer[i].EncryptionAlgorithm.uAlgoIdentifier;
  318. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].EncryptionAlgorithm.KeySize = pPolicyData->pIpSecMMOffer[i].EncryptionAlgorithm.uAlgoKeyLen;
  319. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].EncryptionAlgorithm.Rounds = pPolicyData->pIpSecMMOffer[i].EncryptionAlgorithm.uAlgoRounds;
  320. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].HashAlgorithm.AlgorithmIdentifier = pPolicyData->pIpSecMMOffer[i].HashingAlgorithm.uAlgoIdentifier;
  321. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].HashAlgorithm.KeySize = pPolicyData->pIpSecMMOffer[i].HashingAlgorithm.uAlgoKeyLen;
  322. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].HashAlgorithm.Rounds = pPolicyData->pIpSecMMOffer[i].HashingAlgorithm.uAlgoRounds;
  323. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].OakleyGroup = pPolicyData->pIpSecMMOffer[i].dwDHGroup;
  324. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].QuickModeLimit = pPolicyData->pIpSecMMOffer[i].dwQuickModeLimit;
  325. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].Lifetime.KBytes = 0;
  326. pPolicy->pIpsecISAKMPData->pSecurityMethods[i].Lifetime.Seconds = pPolicyData->pIpSecMMOffer[i].Lifetime.uKeyExpirationTime;
  327. }
  328. // now for other details for ISAKMPPolicy
  329. pPolicy->pIpsecISAKMPData->ISAKMPPolicy.PolicyId = pPolicy->ISAKMPIdentifier;
  330. pPolicy->pIpsecISAKMPData->ISAKMPPolicy.IdentityProtectionRequired = 0;
  331. pPolicy->pIpsecISAKMPData->ISAKMPPolicy.PfsIdentityRequired = pPolicy->pIpsecISAKMPData->pSecurityMethods[0].PfsIdentityRequired;
  332. //call the APIs
  333. dwReturnCode = IPSecCreateISAKMPData(hPolicyStorage, pPolicy->pIpsecISAKMPData);
  334. if (dwReturnCode == ERROR_SUCCESS)
  335. {
  336. //add default rule
  337. dwReturnCode=AddDefaultResponseRule(pPolicy,hPolicyStorage,pPolicyData->bActivateDefaultRule,pPolicyData->bActivateDefaultRuleSpecified);
  338. if (dwReturnCode != ERROR_SUCCESS)
  339. {
  340. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_CRNEWPOL_2);
  341. }
  342. if ( dwReturnCode==ERROR_SUCCESS && pPolicyData->bAssign && (g_StorageLocation.dwLocation!=IPSEC_DIRECTORY_PROVIDER))
  343. {
  344. dwReturnCode = IPSecGetAssignedPolicyData(hPolicyStorage, &pActive);
  345. if ((dwReturnCode == ERROR_SUCCESS)||(dwReturnCode ==ERROR_FILE_NOT_FOUND))
  346. {
  347. dwReturnCode = pActive ? IPSecUnassignPolicy(hPolicyStorage, pActive->PolicyIdentifier) : 0,
  348. IPSecAssignPolicy(hPolicyStorage, pPolicy->PolicyIdentifier);
  349. }
  350. if (pActive)
  351. {
  352. IPSecFreePolicyData(pActive);
  353. }
  354. dwReturnCode = ERROR_SUCCESS;
  355. }
  356. if(dwReturnCode != ERROR_SUCCESS)
  357. {
  358. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_CRNEWPOL_4,pPolicy->pszIpsecName);
  359. }
  360. }
  361. else
  362. {
  363. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_CRNEWPOL_4,pPolicy->pszIpsecName);
  364. }
  365. }
  366. if (hPolicyStorage)
  367. {
  368. ClosePolicyStore(hPolicyStorage);
  369. }
  370. if(pPolicy)
  371. {
  372. FreePolicyData(pPolicy);
  373. }
  374. if(pszMachineName)
  375. {
  376. delete [] pszMachineName;
  377. }
  378. error:
  379. if(dwReturnCode == ERROR_OUTOFMEMORY)
  380. {
  381. CleanUpPolicy(pPolicy);
  382. }
  383. return dwReturnCode;
  384. }
  385. ////////////////////////////////////////////////////////////
  386. //
  387. //Function: FillAddPolicyInfo( )
  388. //
  389. //Date of Creation: 21st Aug 2001
  390. //
  391. //Parameters:
  392. // OUT PPOLICYDATA* ppFilter,
  393. // IN PARSER_PKT & parser,
  394. // IN const TOKEN_VALUE *vtokStaticAddPolicy
  395. //
  396. //Return: DWORD
  397. //
  398. //Description:
  399. // This function fills the local structure with the information got from the parser.
  400. //
  401. //Revision History:
  402. //
  403. // Date Author Comments
  404. //
  405. ////////////////////////////////////////////////////////////
  406. DWORD
  407. FillAddPolicyInfo(
  408. OUT PPOLICYDATA* ppPolicyData,
  409. IN PARSER_PKT & parser,
  410. IN const TOKEN_VALUE *vtokStaticAddPolicy
  411. )
  412. {
  413. DWORD dwCount=0,dwReturn=ERROR_SUCCESS, dwStrLength = 0;
  414. PPOLICYDATA pPolicyData=new POLICYDATA;
  415. if(pPolicyData == NULL)
  416. {
  417. dwReturn=ERROR_OUTOFMEMORY;
  418. BAIL_OUT;
  419. }
  420. memset(pPolicyData,0,sizeof(POLICYDATA));
  421. for(dwCount=0;dwCount<parser.MaxTok;dwCount++)
  422. {
  423. switch(vtokStaticAddPolicy[parser.Cmd[dwCount].dwCmdToken].dwValue)
  424. {
  425. case CMD_TOKEN_NAME :
  426. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  427. {
  428. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  429. pPolicyData->pszPolicyName = new _TCHAR[dwStrLength+1];
  430. if(pPolicyData->pszPolicyName == NULL)
  431. {
  432. dwReturn=ERROR_OUTOFMEMORY;
  433. BAIL_OUT;
  434. }
  435. _tcsncpy(pPolicyData->pszPolicyName, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  436. }
  437. break;
  438. case CMD_TOKEN_DESCR :
  439. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  440. {
  441. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  442. pPolicyData->pszDescription = new _TCHAR[dwStrLength+1];
  443. if(pPolicyData->pszDescription == NULL)
  444. {
  445. dwReturn=ERROR_OUTOFMEMORY;
  446. BAIL_OUT;
  447. }
  448. _tcsncpy(pPolicyData->pszDescription, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  449. }
  450. break;
  451. case CMD_TOKEN_MMPFS :
  452. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  453. {
  454. pPolicyData->bPFSSpecified=TRUE;
  455. pPolicyData->bPFS = *(BOOL *)parser.Cmd[dwCount].pArg;
  456. }
  457. break;
  458. case CMD_TOKEN_ACTIVATEDEFRULE :
  459. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  460. {
  461. pPolicyData->bActivateDefaultRule = *(BOOL *)parser.Cmd[dwCount].pArg;
  462. pPolicyData->bActivateDefaultRuleSpecified=TRUE;
  463. }
  464. break;
  465. case CMD_TOKEN_ASSIGN :
  466. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  467. {
  468. pPolicyData->bAssign = *(BOOL *)parser.Cmd[dwCount].pArg;
  469. }
  470. break;
  471. case CMD_TOKEN_MMLIFETIME :
  472. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  473. {
  474. pPolicyData->LifeTimeInSeconds= *(ULONG *)parser.Cmd[dwCount].pArg * 60;
  475. pPolicyData->bLifeTimeInsecondsSpecified=TRUE;
  476. }
  477. break;
  478. case CMD_TOKEN_QMPERMM :
  479. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  480. {
  481. pPolicyData->dwQMLimit= *(DWORD *)parser.Cmd[dwCount].pArg;
  482. pPolicyData->bQMLimitSpecified=TRUE;
  483. }
  484. break;
  485. case CMD_TOKEN_PI :
  486. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  487. {
  488. pPolicyData->dwPollInterval= *(DWORD *)parser.Cmd[dwCount].pArg * 60;
  489. pPolicyData->bPollIntervalSpecified=TRUE;
  490. }
  491. break;
  492. case CMD_TOKEN_CERTTOMAP :
  493. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  494. {
  495. pPolicyData->bCertToAccMappingSpecified = TRUE;
  496. pPolicyData->bCertToAccMapping = *(BOOL *)parser.Cmd[dwCount].pArg;
  497. }
  498. break;
  499. case CMD_TOKEN_MMSECMETHODS :
  500. if (parser.Cmd[dwCount].dwStatus > 0)
  501. {
  502. pPolicyData->dwOfferCount=parser.Cmd[dwCount].dwStatus;
  503. pPolicyData->pIpSecMMOffer = new IPSEC_MM_OFFER[pPolicyData->dwOfferCount];
  504. if(pPolicyData->pIpSecMMOffer == NULL)
  505. {
  506. dwReturn=ERROR_OUTOFMEMORY;
  507. BAIL_OUT;
  508. }
  509. memset(pPolicyData->pIpSecMMOffer, 0, sizeof(IPSEC_MM_OFFER) * pPolicyData->dwOfferCount);
  510. for(DWORD j=0;j<(parser.Cmd[dwCount].dwStatus);j++)
  511. {
  512. if ( ((IPSEC_MM_OFFER **)parser.Cmd[dwCount].pArg)[j] )
  513. {
  514. memcpy( &(pPolicyData->pIpSecMMOffer[j]),((IPSEC_MM_OFFER **)parser.Cmd[dwCount].pArg)[j],sizeof(IPSEC_MM_OFFER));
  515. }
  516. }
  517. }
  518. break;
  519. default :
  520. break;
  521. }
  522. }
  523. error:
  524. *ppPolicyData=pPolicyData;
  525. CleanUp();
  526. return dwReturn;
  527. }
  528. ////////////////////////////////////////////////////////////
  529. //
  530. //Function: HandleStaticAddFilterList()
  531. //
  532. //Date of Creation: 21st Aug 2001
  533. //
  534. //Parameters:
  535. // IN LPCWSTR pwszMachine,
  536. // IN OUT LPWSTR *ppwcArguments,
  537. // IN DWORD dwCurrentIndex,
  538. // IN DWORD dwArgCount,
  539. // IN DWORD dwFlags,
  540. // IN LPCVOID pvData,
  541. // OUT BOOL *pbDone
  542. //
  543. //Return: DWORD
  544. //
  545. //Description:
  546. // Implementation for the command "Add FilterList"
  547. //
  548. //Revision History:
  549. //
  550. // Date Author Comments
  551. //
  552. ////////////////////////////////////////////////////////////
  553. DWORD WINAPI
  554. HandleStaticAddFilterList(
  555. IN LPCWSTR pwszMachine,
  556. IN OUT LPWSTR *ppwcArguments,
  557. IN DWORD dwCurrentIndex,
  558. IN DWORD dwArgCount,
  559. IN DWORD dwFlags,
  560. IN LPCVOID pvData,
  561. OUT BOOL *pbDone
  562. )
  563. {
  564. LPTSTR pszFLName=NULL,pszFLDescription=NULL;
  565. DWORD dwCount=0,dwRet = ERROR_SHOW_USAGE, dwStrLength = 0;
  566. HANDLE hPolicyStorage = NULL;
  567. BOOL bFilterExists=FALSE;
  568. DWORD dwReturnCode = ERROR_SHOW_USAGE;
  569. PARSER_PKT parser;
  570. ZeroMemory(&parser, sizeof(parser));
  571. const TAG_TYPE vcmdStaticAddFilterList[] =
  572. {
  573. { CMD_TOKEN_STR_NAME, NS_REQ_PRESENT, FALSE },
  574. { CMD_TOKEN_STR_DESCR, NS_REQ_ZERO, FALSE }
  575. };
  576. const TOKEN_VALUE vtokStaticAddFilterList[] =
  577. {
  578. { CMD_TOKEN_STR_NAME, CMD_TOKEN_NAME },
  579. { CMD_TOKEN_STR_DESCR, CMD_TOKEN_DESCR }
  580. };
  581. //if the user asked for usage, delegate the responsibility to netsh
  582. if(dwArgCount <= 3)
  583. {
  584. dwReturnCode = ERROR_SHOW_USAGE;
  585. BAIL_OUT;
  586. }
  587. parser.ValidTok = vtokStaticAddFilterList;
  588. parser.MaxTok = SIZEOF_TOKEN_VALUE(vtokStaticAddFilterList);
  589. parser.ValidCmd = vcmdStaticAddFilterList;
  590. parser.MaxCmd = SIZEOF_TAG_TYPE(vcmdStaticAddFilterList);
  591. //call the parser to parse the user input
  592. dwReturnCode = Parser(pwszMachine,ppwcArguments,dwCurrentIndex,dwArgCount,&parser);
  593. if(dwReturnCode != ERROR_SUCCESS)
  594. {
  595. CleanUp();
  596. if (dwReturnCode==RETURN_NO_ERROR)
  597. {
  598. dwReturnCode = ERROR_SUCCESS;
  599. }
  600. BAIL_OUT;
  601. }
  602. // get the parsed input
  603. for(dwCount=0;dwCount<parser.MaxTok;dwCount++)
  604. {
  605. switch(vtokStaticAddFilterList[parser.Cmd[dwCount].dwCmdToken].dwValue)
  606. {
  607. case CMD_TOKEN_NAME :
  608. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  609. {
  610. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  611. pszFLName = new _TCHAR[dwStrLength+1];
  612. if(pszFLName == NULL)
  613. {
  614. dwRet=ERROR_OUTOFMEMORY;
  615. BAIL_OUT;
  616. }
  617. _tcsncpy(pszFLName, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  618. }
  619. break;
  620. case CMD_TOKEN_DESCR :
  621. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  622. {
  623. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  624. pszFLDescription = new _TCHAR[dwStrLength+1];
  625. if(pszFLDescription == NULL)
  626. {
  627. dwRet=ERROR_OUTOFMEMORY;
  628. BAIL_OUT;
  629. }
  630. _tcsncpy(pszFLDescription, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  631. }
  632. break;
  633. default :
  634. break;
  635. }
  636. }
  637. CleanUp();
  638. //in no name , bail out
  639. if(!pszFLName)
  640. {
  641. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTERLIST_1);
  642. BAIL_OUT;
  643. }
  644. dwReturnCode = OpenPolicyStore(&hPolicyStorage);
  645. if (dwReturnCode != ERROR_SUCCESS)
  646. {
  647. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_FAILED_POLSTORE_OPEN);
  648. dwReturnCode=ERROR_SUCCESS;
  649. BAIL_OUT;
  650. }
  651. //check if policy already exists
  652. if(g_NshPolStoreHandle.GetBatchmodeStatus() && g_NshPolNegFilData.CheckFilterListInCacheByName(pszFLName))
  653. {
  654. bFilterExists=TRUE;
  655. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTERLIST_2,pszFLName);
  656. }
  657. else
  658. {
  659. bFilterExists = CheckFilterListExistance(hPolicyStorage,pszFLName);
  660. if(bFilterExists)
  661. {
  662. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTERLIST_2,pszFLName);
  663. }
  664. }
  665. if(!bFilterExists) //if not exists , proceed further
  666. {
  667. dwReturnCode=CreateNewFilterList(hPolicyStorage,pszFLName,pszFLDescription);
  668. if (dwReturnCode == ERROR_INVALID_PARAMETER)
  669. {
  670. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_STATIC_INTERNAL_ERROR);
  671. dwReturnCode=ERROR_SUCCESS;
  672. }
  673. else if(dwReturnCode != ERROR_OUTOFMEMORY)
  674. {
  675. dwReturnCode=ERROR_SUCCESS;
  676. }
  677. }
  678. ClosePolicyStore(hPolicyStorage);
  679. error: //clean up routines
  680. if(dwReturnCode == ERROR_OUTOFMEMORY)
  681. {
  682. dwReturnCode = ERROR_SUCCESS;
  683. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  684. }
  685. if (pszFLName)
  686. {
  687. delete [] pszFLName;
  688. }
  689. if (pszFLDescription)
  690. {
  691. delete [] pszFLDescription;
  692. }
  693. return dwReturnCode;
  694. }
  695. ////////////////////////////////////////////////////////////
  696. //
  697. //Function: CreateNewFilterList()
  698. //
  699. //Date of Creation: 21st Aug 2001
  700. //
  701. //Parameters:
  702. // IN HANDLE hPolicyStorage,
  703. // IN LPTSTR pszFLName,
  704. // IN LPTSTR pszFLDescription
  705. //
  706. //Return: DWORD
  707. //
  708. //Description:
  709. // This function creates a new empty filter list
  710. //
  711. //Revision History:
  712. //
  713. // Date Author Comments
  714. //
  715. ////////////////////////////////////////////////////////////
  716. DWORD
  717. CreateNewFilterList(
  718. IN HANDLE hPolicyStorage,
  719. IN LPTSTR pszFLName,
  720. IN LPTSTR pszFLDescription
  721. )
  722. {
  723. DWORD dwReturnCode = ERROR_SUCCESS;
  724. RPC_STATUS RpcStat =RPC_S_OK;
  725. PIPSEC_FILTER_DATA pFilterData = (PIPSEC_FILTER_DATA) IPSecAllocPolMem(sizeof(IPSEC_FILTER_DATA));
  726. if(pFilterData==NULL)
  727. {
  728. dwReturnCode = ERROR_OUTOFMEMORY;
  729. BAIL_OUT;
  730. }
  731. RpcStat = UuidCreate(&(pFilterData->FilterIdentifier));
  732. if (!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  733. {
  734. dwReturnCode=ERROR_INVALID_PARAMETER;
  735. BAIL_OUT;
  736. }
  737. pFilterData->dwNumFilterSpecs = 0;
  738. pFilterData->ppFilterSpecs = NULL;
  739. pFilterData->dwWhenChanged = 0;
  740. //fill the name and desc
  741. if(pszFLName)
  742. {
  743. pFilterData->pszIpsecName = IPSecAllocPolStr(pszFLName);
  744. if(pFilterData->pszIpsecName==NULL)
  745. {
  746. dwReturnCode = ERROR_OUTOFMEMORY;
  747. BAIL_OUT;
  748. }
  749. }
  750. if(pszFLDescription)
  751. {
  752. pFilterData->pszDescription = IPSecAllocPolStr(pszFLDescription);
  753. if(pFilterData->pszDescription==NULL)
  754. {
  755. dwReturnCode = ERROR_OUTOFMEMORY;
  756. BAIL_OUT;
  757. }
  758. }
  759. //call the API and report if any error
  760. if (pFilterData)
  761. {
  762. dwReturnCode = CreateFilterData(hPolicyStorage, pFilterData);
  763. }
  764. if (dwReturnCode != ERROR_SUCCESS)
  765. {
  766. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTERLIST_3,pszFLName);
  767. }
  768. if (pFilterData)
  769. {
  770. FreeFilterData(pFilterData);
  771. }
  772. error:
  773. return dwReturnCode;
  774. }
  775. ////////////////////////////////////////////////////////////
  776. //
  777. //Function: HandleStaticAddFilter()
  778. //
  779. //Date of Creation: 21st Aug 2001
  780. //
  781. //Parameters:
  782. // IN LPCWSTR pwszMachine,
  783. // IN OUT LPWSTR *ppwcArguments,
  784. // IN DWORD dwCurrentIndex,
  785. // IN DWORD dwArgCount,
  786. // IN DWORD dwFlags,
  787. // IN LPCVOID pvData,
  788. // OUT BOOL *pbDone
  789. //
  790. //Return: DWORD
  791. //
  792. //Description:
  793. // Implementation for the command "Add Filter"
  794. //
  795. //Revision History:
  796. //
  797. // Date Author Comments
  798. //
  799. ////////////////////////////////////////////////////////////
  800. DWORD WINAPI
  801. HandleStaticAddFilter(
  802. IN LPCWSTR pwszMachine,
  803. IN OUT LPWSTR *ppwcArguments,
  804. IN DWORD dwCurrentIndex,
  805. IN DWORD dwArgCount,
  806. IN DWORD dwFlags,
  807. IN LPCVOID pvData,
  808. OUT BOOL *pbDone
  809. )
  810. {
  811. DWORD dwReturnCode = ERROR_SUCCESS;
  812. PIPSEC_FILTER_DATA pFilterData=NULL;
  813. DWORD LoopIndex=0;
  814. HANDLE hPolicyStorage = NULL;
  815. BOOL bFilterExists=FALSE,bFilterInFLExists=FALSE;
  816. RPC_STATUS RpcStat =RPC_S_OK;
  817. PFILTERDATA pFilter=NULL;
  818. PARSER_PKT parser;
  819. ZeroMemory(&parser, sizeof(parser));
  820. const TAG_TYPE vcmdStaticAddFilter[] =
  821. {
  822. { CMD_TOKEN_STR_FILTERLIST, NS_REQ_PRESENT, FALSE },
  823. { CMD_TOKEN_STR_SRCADDR, NS_REQ_PRESENT, FALSE },
  824. { CMD_TOKEN_STR_DSTADDR, NS_REQ_PRESENT, FALSE },
  825. { CMD_TOKEN_STR_DESCR, NS_REQ_ZERO, FALSE },
  826. { CMD_TOKEN_STR_PROTO, NS_REQ_ZERO, FALSE },
  827. { CMD_TOKEN_STR_MIRROR, NS_REQ_ZERO, FALSE },
  828. { CMD_TOKEN_STR_SRCMASK, NS_REQ_ZERO, FALSE },
  829. { CMD_TOKEN_STR_DSTMASK, NS_REQ_ZERO, FALSE },
  830. { CMD_TOKEN_STR_SRCPORT, NS_REQ_ZERO, FALSE },
  831. { CMD_TOKEN_STR_DSTPORT, NS_REQ_ZERO, FALSE }
  832. };
  833. const TOKEN_VALUE vtokStaticAddFilter[] =
  834. {
  835. { CMD_TOKEN_STR_FILTERLIST, CMD_TOKEN_FILTERLIST },
  836. { CMD_TOKEN_STR_SRCADDR, CMD_TOKEN_SRCADDR },
  837. { CMD_TOKEN_STR_DSTADDR, CMD_TOKEN_DSTADDR },
  838. { CMD_TOKEN_STR_DESCR, CMD_TOKEN_DESCR },
  839. { CMD_TOKEN_STR_PROTO, CMD_TOKEN_PROTO },
  840. { CMD_TOKEN_STR_MIRROR, CMD_TOKEN_MIRROR },
  841. { CMD_TOKEN_STR_SRCMASK, CMD_TOKEN_SRCMASK },
  842. { CMD_TOKEN_STR_DSTMASK, CMD_TOKEN_DSTMASK },
  843. { CMD_TOKEN_STR_SRCPORT, CMD_TOKEN_SRCPORT },
  844. { CMD_TOKEN_STR_DSTPORT, CMD_TOKEN_DSTPORT }
  845. };
  846. //if the user asked for usage, delegate the responsibility to netsh
  847. if(dwArgCount <= 3)
  848. {
  849. dwReturnCode = ERROR_SHOW_USAGE;
  850. BAIL_OUT;
  851. }
  852. parser.ValidTok = vtokStaticAddFilter;
  853. parser.MaxTok = SIZEOF_TOKEN_VALUE(vtokStaticAddFilter);
  854. parser.ValidCmd = vcmdStaticAddFilter;
  855. parser.MaxCmd = SIZEOF_TAG_TYPE(vcmdStaticAddFilter);
  856. dwReturnCode = Parser(pwszMachine,ppwcArguments,dwCurrentIndex,dwArgCount,&parser);
  857. if(dwReturnCode != ERROR_SUCCESS)
  858. {
  859. CleanUp();
  860. if (dwReturnCode==RETURN_NO_ERROR)
  861. {
  862. dwReturnCode = ERROR_SUCCESS;
  863. }
  864. BAIL_OUT;
  865. }
  866. //get the parsed user input
  867. dwReturnCode = FillAddFilterInfo(&pFilter,parser,vtokStaticAddFilter);
  868. BAIL_ON_WIN32_ERROR(dwReturnCode);
  869. if(!pFilter->pszFLName)
  870. {
  871. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTERLIST_1);
  872. BAIL_OUT;
  873. }
  874. //validate the user specified filter details
  875. dwReturnCode =ValidateFilterSpec(pFilter);
  876. BAIL_ON_WIN32_ERROR(dwReturnCode);
  877. dwReturnCode = OpenPolicyStore(&hPolicyStorage);
  878. if (dwReturnCode != ERROR_SUCCESS)
  879. {
  880. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_FAILED_POLSTORE_OPEN);
  881. dwReturnCode=ERROR_SUCCESS;
  882. BAIL_OUT;
  883. }
  884. //get the filterlist specified to which the filter is to be added
  885. if(g_NshPolStoreHandle.GetBatchmodeStatus())
  886. {
  887. bFilterExists=g_NshPolNegFilData.GetFilterListFromCacheByName(pFilter->pszFLName,&pFilterData);
  888. }
  889. if(!bFilterExists)
  890. {
  891. bFilterExists=GetFilterListFromStore(&pFilterData,pFilter->pszFLName,hPolicyStorage,bFilterInFLExists);
  892. }
  893. if(!bFilterExists)
  894. {
  895. // if not exists create the filterlist
  896. dwReturnCode=CreateNewFilterList(hPolicyStorage,pFilter->pszFLName,NULL);
  897. BAIL_ON_WIN32_ERROR(dwReturnCode);
  898. if(g_NshPolStoreHandle.GetBatchmodeStatus())
  899. {
  900. bFilterExists=g_NshPolNegFilData.GetFilterListFromCacheByName(pFilter->pszFLName,&pFilterData);
  901. }
  902. if(!bFilterExists)
  903. {
  904. bFilterExists=GetFilterListFromStore(&pFilterData,pFilter->pszFLName,hPolicyStorage,bFilterInFLExists);
  905. }
  906. }
  907. if(!bFilterExists)
  908. {
  909. // if creation also failed, bail out
  910. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTERLIST_3,pFilter->pszFLName);
  911. dwReturnCode = ERROR_SUCCESS;
  912. BAIL_OUT;
  913. }
  914. //check readonly flag
  915. if(pFilterData->dwFlags & POLSTORE_READONLY )
  916. {
  917. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SET_FL_READ_ONLY_OBJECT,pFilterData->pszIpsecName);
  918. BAIL_OUT;
  919. }
  920. // this for loop is for multiple DNS resolved IPs
  921. for(DWORD i=0;i < pFilter->SourceAddr.dwNumIpAddresses;i++)
  922. {
  923. for(DWORD j=0;j < pFilter->DestnAddr.dwNumIpAddresses;j++)
  924. {
  925. RpcStat = UuidCreate(&(pFilter->FilterSpecGUID));
  926. if (!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  927. {
  928. dwReturnCode=ERROR_INVALID_PARAMETER;
  929. BAIL_OUT;
  930. }
  931. pFilterData->dwNumFilterSpecs++;
  932. LoopIndex = pFilterData->dwNumFilterSpecs-1;
  933. // call realloc , to make room for another filter
  934. pFilterData->ppFilterSpecs = ReAllocFilterSpecMem(pFilterData->ppFilterSpecs,LoopIndex,LoopIndex+1);
  935. if(pFilterData->ppFilterSpecs==NULL)
  936. {
  937. dwReturnCode=ERROR_OUTOFMEMORY;
  938. BAIL_OUT;
  939. }
  940. pFilterData->ppFilterSpecs[LoopIndex] = (PIPSEC_FILTER_SPEC) IPSecAllocPolMem(sizeof(IPSEC_FILTER_SPEC));
  941. if(pFilterData->ppFilterSpecs[LoopIndex]==NULL)
  942. {
  943. dwReturnCode=ERROR_OUTOFMEMORY;
  944. BAIL_OUT;
  945. }
  946. memset(pFilterData->ppFilterSpecs[LoopIndex],0,sizeof(IPSEC_FILTER_SPEC));
  947. //src & dst DNS name
  948. if(pFilter->SourceAddr.pszDomainName)
  949. {
  950. pFilterData->ppFilterSpecs[LoopIndex]->pszSrcDNSName=IPSecAllocPolStr(pFilter->SourceAddr.pszDomainName);
  951. if(pFilterData->ppFilterSpecs[LoopIndex]->pszSrcDNSName==NULL)
  952. {
  953. dwReturnCode=ERROR_OUTOFMEMORY;
  954. BAIL_OUT;
  955. }
  956. }
  957. else
  958. {
  959. pFilterData->ppFilterSpecs[LoopIndex]->pszSrcDNSName=0;
  960. }
  961. if(pFilter->DestnAddr.pszDomainName)
  962. {
  963. pFilterData->ppFilterSpecs[LoopIndex]->pszDestDNSName = IPSecAllocPolStr(pFilter->DestnAddr.pszDomainName);
  964. if(pFilterData->ppFilterSpecs[LoopIndex]->pszDestDNSName==NULL)
  965. {
  966. dwReturnCode=ERROR_OUTOFMEMORY;
  967. BAIL_OUT;
  968. }
  969. }
  970. else
  971. {
  972. pFilterData->ppFilterSpecs[LoopIndex]->pszDestDNSName = 0;
  973. }
  974. //desc
  975. if(pFilter->pszDescription)
  976. {
  977. pFilterData->ppFilterSpecs[LoopIndex]->pszDescription = IPSecAllocPolStr(pFilter->pszDescription);
  978. if(pFilterData->ppFilterSpecs[LoopIndex]->pszDescription==NULL)
  979. {
  980. dwReturnCode=ERROR_OUTOFMEMORY;
  981. BAIL_OUT;
  982. }
  983. }
  984. else
  985. {
  986. pFilterData->ppFilterSpecs[LoopIndex]->pszDescription = NULL;
  987. }
  988. if (((pFilter->dwProtocol != PROT_ID_TCP) && (pFilter->dwProtocol != PROT_ID_UDP)) &&
  989. ((pFilter->SourPort != 0) || (pFilter->DestPort != 0)))
  990. {
  991. dwReturnCode = ERROR_INVALID_PARAMETER;
  992. BAIL_OUT;
  993. }
  994. //other details like mirrored, protocol etc
  995. pFilterData->ppFilterSpecs[LoopIndex]->dwMirrorFlag = pFilter->bMirrored;
  996. pFilterData->ppFilterSpecs[LoopIndex]->FilterSpecGUID = pFilter->FilterSpecGUID;
  997. pFilterData->ppFilterSpecs[LoopIndex]->Filter.SrcAddr = pFilter->SourceAddr.puIpAddr[i];
  998. pFilterData->ppFilterSpecs[LoopIndex]->Filter.SrcMask = pFilter->SourMask;
  999. pFilterData->ppFilterSpecs[LoopIndex]->Filter.DestAddr = pFilter->DestnAddr.puIpAddr[j];
  1000. pFilterData->ppFilterSpecs[LoopIndex]->Filter.DestMask = pFilter->DestMask;
  1001. pFilterData->ppFilterSpecs[LoopIndex]->Filter.TunnelAddr = 0;
  1002. pFilterData->ppFilterSpecs[LoopIndex]->Filter.Protocol = pFilter->dwProtocol;
  1003. pFilterData->ppFilterSpecs[LoopIndex]->Filter.SrcPort = pFilter->SourPort;
  1004. pFilterData->ppFilterSpecs[LoopIndex]->Filter.DestPort = pFilter->DestPort;
  1005. pFilterData->ppFilterSpecs[LoopIndex]->Filter.TunnelFilter = FALSE;
  1006. pFilterData->ppFilterSpecs[LoopIndex]->Filter.ExType = pFilter->ExType;
  1007. }
  1008. }
  1009. //once filling the parameters are over , call the respective API
  1010. if (pFilterData)
  1011. {
  1012. // Wrapper API is called to update cache also if required
  1013. dwReturnCode = SetFilterData(hPolicyStorage, pFilterData);
  1014. }
  1015. if (dwReturnCode != ERROR_SUCCESS)
  1016. {
  1017. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTER_2);
  1018. }
  1019. if(pFilterData)
  1020. {
  1021. FreeFilterData(pFilterData);
  1022. }
  1023. ClosePolicyStore(hPolicyStorage);
  1024. error: //cleanup and error printing
  1025. if(dwReturnCode == ERROR_OUTOFMEMORY || dwReturnCode == ERROR_INVALID_PARAMETER)
  1026. {
  1027. if(dwReturnCode == ERROR_OUTOFMEMORY)
  1028. {
  1029. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  1030. }
  1031. else
  1032. {
  1033. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_INVALID_ARGS);
  1034. }
  1035. if(pFilterData) // if allocation failed somewhere, free
  1036. {
  1037. if(pFilterData->ppFilterSpecs)
  1038. {
  1039. for (DWORD cnt=0; cnt< pFilterData->dwNumFilterSpecs;cnt++)
  1040. {
  1041. if(pFilterData->ppFilterSpecs[cnt])
  1042. {
  1043. IPSecFreePolMem(pFilterData->ppFilterSpecs[cnt]);
  1044. }
  1045. }
  1046. IPSecFreePolMem(pFilterData->ppFilterSpecs);
  1047. }
  1048. IPSecFreePolMem(pFilterData);
  1049. pFilterData = NULL;
  1050. }
  1051. }
  1052. CleanUpLocalFilterDataStructure(pFilter);
  1053. if(dwReturnCode != ERROR_SHOW_USAGE)
  1054. {
  1055. dwReturnCode = ERROR_SUCCESS;
  1056. }
  1057. return dwReturnCode;
  1058. }
  1059. ////////////////////////////////////////////////////////////
  1060. //
  1061. //Function: FillAddFilterInfo( )
  1062. //
  1063. //Date of Creation: 21st Aug 2001
  1064. //
  1065. //Parameters:
  1066. // OUT PFILTERDATA* ppFilterData,
  1067. // IN PARSER_PKT & parser,
  1068. // IN const TOKEN_VALUE *vtokStaticAddFilter
  1069. //
  1070. //Return: DWORD
  1071. //
  1072. //Description:
  1073. // This function fills the local structure with the information got from the parser.
  1074. //
  1075. //Revision History:
  1076. //
  1077. // Date Author Comments
  1078. //
  1079. ////////////////////////////////////////////////////////////
  1080. DWORD
  1081. FillAddFilterInfo(
  1082. OUT PFILTERDATA* ppFilterData,
  1083. IN PARSER_PKT & parser,
  1084. IN const TOKEN_VALUE *vtokStaticAddFilter
  1085. )
  1086. {
  1087. DWORD dwCount=0,dwReturn=ERROR_SUCCESS,dwStrLength = 0;
  1088. PFILTERDATA pFilterData=new FILTERDATA;
  1089. if(pFilterData==NULL)
  1090. {
  1091. dwReturn=ERROR_OUTOFMEMORY;
  1092. BAIL_OUT;
  1093. }
  1094. memset(pFilterData,0,sizeof(FILTERDATA));
  1095. pFilterData->bMirrored=TRUE;
  1096. pFilterData->DestPort= pFilterData->SourPort=PORT_ANY;
  1097. pFilterData->dwProtocol=PROTOCOL_ANY;
  1098. pFilterData->TunnFiltExists=FALSE;
  1099. pFilterData->SourMask = pFilterData->DestMask = MASK_ME;
  1100. for(dwCount=0;dwCount<parser.MaxTok;dwCount++)
  1101. {
  1102. switch(vtokStaticAddFilter[parser.Cmd[dwCount].dwCmdToken].dwValue)
  1103. {
  1104. case CMD_TOKEN_FILTERLIST :
  1105. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1106. {
  1107. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  1108. pFilterData->pszFLName = new _TCHAR[dwStrLength+1];
  1109. if(pFilterData->pszFLName==NULL)
  1110. {
  1111. dwReturn=ERROR_OUTOFMEMORY;
  1112. BAIL_OUT;
  1113. }
  1114. _tcsncpy(pFilterData->pszFLName, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  1115. }
  1116. break;
  1117. case CMD_TOKEN_DESCR :
  1118. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1119. {
  1120. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  1121. pFilterData->pszDescription = new _TCHAR[dwStrLength+1];
  1122. if(pFilterData->pszDescription==NULL)
  1123. {
  1124. dwReturn=ERROR_OUTOFMEMORY;
  1125. BAIL_OUT;
  1126. }
  1127. _tcsncpy(pFilterData->pszDescription, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  1128. }
  1129. break;
  1130. case CMD_TOKEN_MIRROR :
  1131. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1132. {
  1133. pFilterData->bMirrored = *(BOOL *)parser.Cmd[dwCount].pArg;
  1134. }
  1135. break;
  1136. case CMD_TOKEN_PROTO :
  1137. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1138. {
  1139. pFilterData->dwProtocol = *(DWORD *)parser.Cmd[dwCount].pArg;
  1140. }
  1141. break;
  1142. case CMD_TOKEN_SRCPORT :
  1143. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1144. {
  1145. pFilterData->SourPort = *(WORD *)parser.Cmd[dwCount].pArg;
  1146. }
  1147. break;
  1148. case CMD_TOKEN_DSTPORT :
  1149. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1150. {
  1151. pFilterData->DestPort = *(WORD *)parser.Cmd[dwCount].pArg;
  1152. }
  1153. break;
  1154. case CMD_TOKEN_SRCADDR :
  1155. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1156. {
  1157. pFilterData->bSrcAddrSpecified=TRUE;
  1158. if(((DNSIPADDR *)parser.Cmd[dwCount].pArg)->pszDomainName)
  1159. {
  1160. dwStrLength = _tcslen(((DNSIPADDR *)parser.Cmd[dwCount].pArg)->pszDomainName);
  1161. pFilterData->SourceAddr.pszDomainName = new _TCHAR[dwStrLength+1];
  1162. if(pFilterData->SourceAddr.pszDomainName==NULL)
  1163. {
  1164. dwReturn=ERROR_OUTOFMEMORY;
  1165. BAIL_OUT;
  1166. }
  1167. _tcsncpy(pFilterData->SourceAddr.pszDomainName,((DNSIPADDR *)parser.Cmd[dwCount].pArg)->pszDomainName,dwStrLength+1);
  1168. }
  1169. pFilterData->SourceAddr.dwNumIpAddresses = ((DNSIPADDR *)parser.Cmd[dwCount].pArg)->dwNumIpAddresses;
  1170. pFilterData->SourceAddr.puIpAddr= new ULONG[pFilterData->SourceAddr.dwNumIpAddresses];
  1171. if(pFilterData->SourceAddr.puIpAddr==NULL)
  1172. {
  1173. dwReturn=ERROR_OUTOFMEMORY;
  1174. BAIL_OUT;
  1175. }
  1176. for(DWORD n=0;n < pFilterData->SourceAddr.dwNumIpAddresses;n++)
  1177. {
  1178. memcpy( &(pFilterData->SourceAddr.puIpAddr[n]),&(((DNSIPADDR *)parser.Cmd[dwCount].pArg)->puIpAddr[n]),sizeof(ULONG));
  1179. }
  1180. }
  1181. else
  1182. {
  1183. //look for special server type
  1184. if(parser.Cmd[dwCount].dwStatus == SERVER_DNS)
  1185. {
  1186. pFilterData->bSrcServerSpecified=TRUE;
  1187. pFilterData->ExType=EXT_DNS_SERVER;
  1188. }
  1189. else if(parser.Cmd[dwCount].dwStatus == SERVER_WINS)
  1190. {
  1191. pFilterData->bSrcServerSpecified=TRUE;
  1192. pFilterData->ExType=EXT_WINS_SERVER;
  1193. }
  1194. else if(parser.Cmd[dwCount].dwStatus == SERVER_DHCP)
  1195. {
  1196. pFilterData->bSrcServerSpecified=TRUE;
  1197. pFilterData->ExType=EXT_DHCP_SERVER;
  1198. }
  1199. else if(parser.Cmd[dwCount].dwStatus == SERVER_GATEWAY)
  1200. {
  1201. pFilterData->bSrcServerSpecified=TRUE;
  1202. pFilterData->ExType=EXT_DEFAULT_GATEWAY;
  1203. }
  1204. else if (parser.Cmd[dwCount].dwStatus == IP_ME)
  1205. {
  1206. pFilterData->bSrcMeSpecified=TRUE;
  1207. }
  1208. else if (parser.Cmd[dwCount].dwStatus == IP_ANY)
  1209. {
  1210. pFilterData->bSrcAnySpecified=TRUE;
  1211. }
  1212. }
  1213. break;
  1214. case CMD_TOKEN_SRCMASK :
  1215. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1216. {
  1217. pFilterData->bSrcMaskSpecified=TRUE;
  1218. pFilterData->SourMask = *(DWORD *)parser.Cmd[dwCount].pArg;
  1219. }
  1220. break;
  1221. case CMD_TOKEN_DSTADDR :
  1222. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1223. {
  1224. pFilterData->bDstAddrSpecified=TRUE;
  1225. if(((DNSIPADDR *)parser.Cmd[dwCount].pArg)->pszDomainName)
  1226. {
  1227. dwStrLength = _tcslen(((DNSIPADDR *)parser.Cmd[dwCount].pArg)->pszDomainName);
  1228. pFilterData->DestnAddr.pszDomainName = new _TCHAR[dwStrLength+1];
  1229. if(pFilterData->DestnAddr.pszDomainName == NULL)
  1230. {
  1231. dwReturn=ERROR_OUTOFMEMORY;
  1232. BAIL_OUT;
  1233. }
  1234. _tcsncpy(pFilterData->DestnAddr.pszDomainName,((DNSIPADDR *)parser.Cmd[dwCount].pArg)->pszDomainName,dwStrLength+1);
  1235. }
  1236. pFilterData->DestnAddr.dwNumIpAddresses = ((DNSIPADDR *)parser.Cmd[dwCount].pArg)->dwNumIpAddresses;
  1237. pFilterData->DestnAddr.puIpAddr= new ULONG[pFilterData->DestnAddr.dwNumIpAddresses];
  1238. if(pFilterData->DestnAddr.puIpAddr == NULL)
  1239. {
  1240. dwReturn=ERROR_OUTOFMEMORY;
  1241. BAIL_OUT;
  1242. }
  1243. for(DWORD n=0;n < pFilterData->DestnAddr.dwNumIpAddresses;n++)
  1244. {
  1245. memcpy( &(pFilterData->DestnAddr.puIpAddr[n]),(&((DNSIPADDR *)parser.Cmd[dwCount].pArg)->puIpAddr[n]),sizeof(ULONG));
  1246. }
  1247. }
  1248. else
  1249. {
  1250. //look for special server type
  1251. if(parser.Cmd[dwCount].dwStatus == SERVER_DNS)
  1252. {
  1253. pFilterData->bDstServerSpecified=TRUE;
  1254. pFilterData->ExType=EXT_DNS_SERVER | EXT_DEST;
  1255. }
  1256. else if(parser.Cmd[dwCount].dwStatus == SERVER_WINS)
  1257. {
  1258. pFilterData->bDstServerSpecified=TRUE;
  1259. pFilterData->ExType=EXT_WINS_SERVER | EXT_DEST;
  1260. }
  1261. else if(parser.Cmd[dwCount].dwStatus == SERVER_DHCP)
  1262. {
  1263. pFilterData->bDstServerSpecified=TRUE;
  1264. pFilterData->ExType=EXT_DHCP_SERVER | EXT_DEST;
  1265. }
  1266. else if(parser.Cmd[dwCount].dwStatus == SERVER_GATEWAY)
  1267. {
  1268. pFilterData->bDstServerSpecified=TRUE;
  1269. pFilterData->ExType=EXT_DEFAULT_GATEWAY | EXT_DEST;
  1270. }
  1271. else if (parser.Cmd[dwCount].dwStatus == IP_ME)
  1272. {
  1273. pFilterData->bDstMeSpecified=TRUE;
  1274. }
  1275. else if (parser.Cmd[dwCount].dwStatus == IP_ANY)
  1276. {
  1277. pFilterData->bDstAnySpecified=TRUE;
  1278. }
  1279. }
  1280. break;
  1281. case CMD_TOKEN_DSTMASK :
  1282. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1283. {
  1284. pFilterData->bDstMaskSpecified=TRUE;
  1285. pFilterData->DestMask = *(DWORD *)parser.Cmd[dwCount].pArg;
  1286. }
  1287. break;
  1288. default :
  1289. break;
  1290. }
  1291. }
  1292. // take care of me and any here
  1293. if(pFilterData->bSrcMeSpecified)
  1294. {
  1295. if (pFilterData->bDstMeSpecified)
  1296. {
  1297. dwReturn = ERROR_INVALID_PARAMETER;
  1298. BAIL_OUT;
  1299. }
  1300. pFilterData->SourceAddr.dwNumIpAddresses = 1;
  1301. pFilterData->SourceAddr.puIpAddr= new ULONG[pFilterData->SourceAddr.dwNumIpAddresses];
  1302. if(pFilterData->SourceAddr.puIpAddr == NULL)
  1303. {
  1304. dwReturn=ERROR_OUTOFMEMORY;
  1305. BAIL_OUT;
  1306. }
  1307. *(pFilterData->SourceAddr.puIpAddr)=ADDR_ME;
  1308. pFilterData->SourMask = MASK_ME;
  1309. }
  1310. else
  1311. {
  1312. ADDR srcAddr, dstAddr;
  1313. if(pFilterData->bSrcAnySpecified)
  1314. {
  1315. pFilterData->SourceAddr.dwNumIpAddresses = 1;
  1316. pFilterData->SourceAddr.puIpAddr= new ULONG[pFilterData->SourceAddr.dwNumIpAddresses];
  1317. if(pFilterData->SourceAddr.puIpAddr == NULL)
  1318. {
  1319. dwReturn=ERROR_OUTOFMEMORY;
  1320. BAIL_OUT;
  1321. }
  1322. *(pFilterData->SourceAddr.puIpAddr)=ADDR_ME;
  1323. pFilterData->SourMask = ADDR_ME;
  1324. }
  1325. if (pFilterData->bDstAddrSpecified)
  1326. {
  1327. dstAddr.uIpAddr = ntohl(*(pFilterData->DestnAddr.puIpAddr));
  1328. if (pFilterData->bDstMaskSpecified)
  1329. {
  1330. dstAddr.uSubNetMask = ntohl(pFilterData->DestMask);
  1331. }
  1332. else
  1333. {
  1334. dstAddr.uSubNetMask = 0;
  1335. }
  1336. if (IsBroadcastAddress(&dstAddr) || IsMulticastAddress(&dstAddr))
  1337. {
  1338. dwReturn = ERROR_INVALID_PARAMETER;
  1339. BAIL_OUT;
  1340. }
  1341. }
  1342. if (pFilterData->bSrcAddrSpecified)
  1343. {
  1344. srcAddr.uIpAddr = ntohl(*(pFilterData->SourceAddr.puIpAddr));
  1345. if (pFilterData->bSrcMaskSpecified)
  1346. {
  1347. srcAddr.uSubNetMask = ntohl(pFilterData->SourMask);
  1348. }
  1349. else
  1350. {
  1351. srcAddr.uSubNetMask = 0;
  1352. }
  1353. if (IsBroadcastAddress(&srcAddr) || IsMulticastAddress(&srcAddr))
  1354. {
  1355. dwReturn = ERROR_INVALID_PARAMETER;
  1356. BAIL_OUT;
  1357. }
  1358. }
  1359. if (pFilterData->bSrcAddrSpecified && pFilterData->bDstAddrSpecified)
  1360. {
  1361. // if <ip> - <same IP> reject
  1362. if (!IsValidSubnet(&srcAddr) && !IsValidSubnet(&dstAddr) && (srcAddr.uIpAddr == dstAddr.uIpAddr))
  1363. {
  1364. dwReturn = ERROR_INVALID_PARAMETER;
  1365. BAIL_OUT;
  1366. }
  1367. }
  1368. }
  1369. if (pFilterData->bMirrored)
  1370. {
  1371. if (pFilterData->bSrcAddrSpecified)
  1372. {
  1373. ADDR addr;
  1374. addr.uIpAddr = ntohl(*(pFilterData->SourceAddr.puIpAddr));
  1375. addr.uSubNetMask = 0;
  1376. if (IsBroadcastAddress(&addr) || IsMulticastAddress(&addr))
  1377. {
  1378. dwReturn = ERROR_INVALID_PARAMETER;
  1379. BAIL_OUT;
  1380. }
  1381. }
  1382. }
  1383. else
  1384. {
  1385. // reject if any<->any and not mirrored
  1386. if (pFilterData->bSrcAnySpecified && pFilterData->bDstAnySpecified)
  1387. {
  1388. dwReturn = ERROR_INVALID_PARAMETER;
  1389. BAIL_OUT;
  1390. }
  1391. if (pFilterData->SourceAddr.puIpAddr && pFilterData->DestnAddr.puIpAddr)
  1392. {
  1393. // reject subnetx-subnetx if not mirrored
  1394. ADDR srcAddr;
  1395. ADDR dstAddr;
  1396. srcAddr.uIpAddr = ntohl(*(pFilterData->SourceAddr.puIpAddr));
  1397. srcAddr.uSubNetMask = ntohl(pFilterData->SourMask);
  1398. dstAddr.uIpAddr = ntohl(*(pFilterData->DestnAddr.puIpAddr));
  1399. dstAddr.uSubNetMask = ntohl(pFilterData->DestMask);
  1400. if (IsValidSubnet(&srcAddr) && IsValidSubnet(&dstAddr) && (srcAddr.uIpAddr == dstAddr.uIpAddr))
  1401. {
  1402. dwReturn = ERROR_INVALID_PARAMETER;
  1403. BAIL_OUT;
  1404. }
  1405. }
  1406. }
  1407. if(pFilterData->bDstMeSpecified)
  1408. {
  1409. pFilterData->DestnAddr.dwNumIpAddresses = 1;
  1410. pFilterData->DestnAddr.puIpAddr= new ULONG[pFilterData->DestnAddr.dwNumIpAddresses];
  1411. if(pFilterData->DestnAddr.puIpAddr == NULL)
  1412. {
  1413. dwReturn=ERROR_OUTOFMEMORY;
  1414. BAIL_OUT;
  1415. }
  1416. *(pFilterData->DestnAddr.puIpAddr)=ADDR_ME;
  1417. pFilterData->DestMask = MASK_ME;
  1418. }
  1419. else
  1420. {
  1421. if(pFilterData->bDstAnySpecified)
  1422. {
  1423. pFilterData->DestnAddr.dwNumIpAddresses = 1;
  1424. pFilterData->DestnAddr.puIpAddr= new ULONG[pFilterData->DestnAddr.dwNumIpAddresses];
  1425. if(pFilterData->DestnAddr.puIpAddr == NULL)
  1426. {
  1427. dwReturn=ERROR_OUTOFMEMORY;
  1428. BAIL_OUT;
  1429. }
  1430. *(pFilterData->DestnAddr.puIpAddr)=ADDR_ME;
  1431. pFilterData->DestMask = ADDR_ME;
  1432. }
  1433. }
  1434. //if DNS specified mask is to be made to 255.255.255.255
  1435. if (pFilterData->DestnAddr.pszDomainName)
  1436. {
  1437. pFilterData->DestMask = MASK_ME;
  1438. }
  1439. if (pFilterData->SourceAddr.pszDomainName)
  1440. {
  1441. pFilterData->SourMask = MASK_ME;
  1442. }
  1443. //if server type specified, ingore other irrelevent inputs
  1444. if(pFilterData->bSrcServerSpecified || pFilterData->bDstServerSpecified)
  1445. {
  1446. if(!(pFilterData->bSrcServerSpecified && pFilterData->bDstServerSpecified))
  1447. {
  1448. if(pFilterData->bSrcServerSpecified)
  1449. {
  1450. if(
  1451. !(pFilterData->bDstMeSpecified)
  1452. && ((pFilterData->DestnAddr.puIpAddr && pFilterData->DestnAddr.puIpAddr !=0 )
  1453. ||(pFilterData->DestMask != MASK_ME))
  1454. )
  1455. {
  1456. PrintMessageFromModule(g_hModule,ADD_STATIC_FILTER_SRCSERVER_WARNING);
  1457. }
  1458. }
  1459. else
  1460. {
  1461. if(
  1462. !(pFilterData->bSrcMeSpecified)
  1463. && ( (pFilterData->SourceAddr.puIpAddr && pFilterData->SourceAddr.puIpAddr !=0 )
  1464. ||(pFilterData->SourMask != MASK_ME))
  1465. )
  1466. {
  1467. PrintMessageFromModule(g_hModule,ADD_STATIC_FILTER_DSTSERVER_WARNING);
  1468. }
  1469. }
  1470. }
  1471. if(pFilterData->SourceAddr.pszDomainName)
  1472. {
  1473. delete [] pFilterData->SourceAddr.pszDomainName;
  1474. pFilterData->SourceAddr.pszDomainName=NULL;
  1475. }
  1476. if(pFilterData->DestnAddr.pszDomainName)
  1477. {
  1478. delete [] pFilterData->DestnAddr.pszDomainName;
  1479. pFilterData->DestnAddr.pszDomainName=NULL;
  1480. }
  1481. if(pFilterData->SourceAddr.puIpAddr)
  1482. {
  1483. delete [] pFilterData->SourceAddr.puIpAddr;
  1484. }
  1485. if(pFilterData->DestnAddr.puIpAddr)
  1486. {
  1487. delete [] pFilterData->DestnAddr.puIpAddr;
  1488. }
  1489. pFilterData->bSrcAddrSpecified=FALSE;
  1490. pFilterData->bSrcMaskSpecified=FALSE;
  1491. pFilterData->bDstAddrSpecified=FALSE;
  1492. pFilterData->bDstMaskSpecified=FALSE;
  1493. pFilterData->SourceAddr.dwNumIpAddresses = DEF_NUMBER_OF_ADDR;
  1494. pFilterData->SourceAddr.puIpAddr= new ULONG[DEF_NUMBER_OF_ADDR];
  1495. if(pFilterData->SourceAddr.puIpAddr == NULL)
  1496. {
  1497. dwReturn=ERROR_OUTOFMEMORY;
  1498. BAIL_OUT;
  1499. }
  1500. memcpy( &(pFilterData->SourceAddr.puIpAddr[0]),&ADDR_ME ,sizeof(ULONG));
  1501. pFilterData->DestnAddr.dwNumIpAddresses = DEF_NUMBER_OF_ADDR;
  1502. pFilterData->DestnAddr.puIpAddr= new ULONG[DEF_NUMBER_OF_ADDR];
  1503. if(pFilterData->DestnAddr.puIpAddr == NULL)
  1504. {
  1505. dwReturn=ERROR_OUTOFMEMORY;
  1506. BAIL_OUT;
  1507. }
  1508. memcpy( &(pFilterData->DestnAddr.puIpAddr[0]),&ADDR_ME ,sizeof(ULONG));
  1509. pFilterData->SourMask = MASK_ME;
  1510. pFilterData->DestMask = MASK_ME;
  1511. }
  1512. error:
  1513. *ppFilterData=pFilterData;
  1514. CleanUp();
  1515. return dwReturn;
  1516. }
  1517. ////////////////////////////////////////////////////////////
  1518. //
  1519. //Function: HandleStaticAddFilterActions()
  1520. //
  1521. //Date of Creation: 21st Aug 2001
  1522. //
  1523. //Parameters:
  1524. // IN LPCWSTR pwszMachine,
  1525. // IN OUT LPWSTR *ppwcArguments,
  1526. // IN DWORD dwCurrentIndex,
  1527. // IN DWORD dwArgCount,
  1528. // IN DWORD dwFlags,
  1529. // IN LPCVOID pvData,
  1530. // OUT BOOL *pbDone
  1531. //
  1532. //Return: DWORD
  1533. //
  1534. //Description:
  1535. // Implementation for the command "Add FilterActions"
  1536. //
  1537. //Revision History:
  1538. //
  1539. // Date Author Comments
  1540. //
  1541. ////////////////////////////////////////////////////////////
  1542. DWORD WINAPI
  1543. HandleStaticAddFilterActions(
  1544. IN LPCWSTR pwszMachine,
  1545. IN OUT LPWSTR *ppwcArguments,
  1546. IN DWORD dwCurrentIndex,
  1547. IN DWORD dwArgCount,
  1548. IN DWORD dwFlags,
  1549. IN LPCVOID pvData,
  1550. OUT BOOL *pbDone
  1551. )
  1552. {
  1553. HANDLE hPolicyStorage = NULL;
  1554. BOOL bNegPolExists=FALSE;
  1555. PFILTERACTION pFilterAction= NULL;
  1556. DWORD dwReturnCode = ERROR_SUCCESS;
  1557. PARSER_PKT parser;
  1558. ZeroMemory(&parser, sizeof(parser));
  1559. const TAG_TYPE vcmdStaticAddFilterAction[] =
  1560. {
  1561. { CMD_TOKEN_STR_NAME, NS_REQ_PRESENT, FALSE },
  1562. { CMD_TOKEN_STR_DESCR, NS_REQ_ZERO, FALSE },
  1563. { CMD_TOKEN_STR_QMPFS, NS_REQ_ZERO, FALSE },
  1564. { CMD_TOKEN_STR_INPASS, NS_REQ_ZERO, FALSE },
  1565. { CMD_TOKEN_STR_SOFT, NS_REQ_ZERO, FALSE },
  1566. { CMD_TOKEN_STR_ACTION, NS_REQ_ZERO, FALSE },
  1567. { CMD_TOKEN_STR_QMSECMETHODS, NS_REQ_ZERO, FALSE }
  1568. };
  1569. const TOKEN_VALUE vtokStaticAddFilterAction[] =
  1570. {
  1571. { CMD_TOKEN_STR_NAME, CMD_TOKEN_NAME },
  1572. { CMD_TOKEN_STR_DESCR, CMD_TOKEN_DESCR },
  1573. { CMD_TOKEN_STR_QMPFS, CMD_TOKEN_QMPFS },
  1574. { CMD_TOKEN_STR_INPASS, CMD_TOKEN_INPASS },
  1575. { CMD_TOKEN_STR_SOFT, CMD_TOKEN_SOFT },
  1576. { CMD_TOKEN_STR_ACTION, CMD_TOKEN_ACTION },
  1577. { CMD_TOKEN_STR_QMSECMETHODS, CMD_TOKEN_QMSECMETHODS }
  1578. };
  1579. //if the user asked for usage, delegate the responsibility to netsh
  1580. if(dwArgCount <=3)
  1581. {
  1582. dwReturnCode = ERROR_SHOW_USAGE;
  1583. BAIL_OUT;
  1584. }
  1585. parser.ValidTok = vtokStaticAddFilterAction;
  1586. parser.MaxTok = SIZEOF_TOKEN_VALUE(vtokStaticAddFilterAction);
  1587. parser.ValidCmd = vcmdStaticAddFilterAction;
  1588. parser.MaxCmd = SIZEOF_TAG_TYPE(vcmdStaticAddFilterAction);
  1589. //call parser
  1590. dwReturnCode = Parser(pwszMachine,ppwcArguments,dwCurrentIndex,dwArgCount,&parser);
  1591. if(dwReturnCode != ERROR_SUCCESS)
  1592. {
  1593. CleanUp();
  1594. if (dwReturnCode==RETURN_NO_ERROR)
  1595. {
  1596. dwReturnCode = ERROR_SUCCESS;
  1597. }
  1598. BAIL_OUT;
  1599. }
  1600. //get the parsed user input
  1601. dwReturnCode = FillAddFilterActionInfo(&pFilterAction,parser,vtokStaticAddFilterAction);
  1602. if(dwReturnCode==ERROR_OUTOFMEMORY)
  1603. {
  1604. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  1605. dwReturnCode = ERROR_SUCCESS;
  1606. BAIL_OUT;
  1607. }
  1608. // if no name, bail out
  1609. if(!pFilterAction->pszFAName)
  1610. {
  1611. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_RULE_2);
  1612. BAIL_OUT;
  1613. }
  1614. if((pFilterAction->NegPolAction==GUID_NEGOTIATION_ACTION_BLOCK)||(pFilterAction->NegPolAction==GUID_NEGOTIATION_ACTION_NO_IPSEC))
  1615. {
  1616. pFilterAction->bQMPfs=0;
  1617. pFilterAction->bSoft=0;
  1618. }
  1619. dwReturnCode = OpenPolicyStore(&hPolicyStorage);
  1620. if (dwReturnCode != ERROR_SUCCESS)
  1621. {
  1622. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_FAILED_POLSTORE_OPEN);
  1623. dwReturnCode=ERROR_SUCCESS;
  1624. BAIL_OUT;
  1625. }
  1626. // check for multiple filteractions with same name
  1627. if(g_NshPolStoreHandle.GetBatchmodeStatus() && g_NshPolNegFilData.CheckNegPolInCacheByName(pFilterAction->pszFAName))
  1628. {
  1629. bNegPolExists=TRUE;
  1630. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTERACTION_1,pFilterAction->pszFAName);
  1631. BAIL_OUT;
  1632. }
  1633. else
  1634. {
  1635. bNegPolExists = CheckFilterActionExistance(hPolicyStorage,pFilterAction->pszFAName);
  1636. if(bNegPolExists)
  1637. {
  1638. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTERACTION_1,pFilterAction->pszFAName);
  1639. BAIL_OUT;
  1640. }
  1641. }
  1642. PIPSEC_NEGPOL_DATA pNegPolData=NULL;
  1643. if(pFilterAction->dwNumSecMethodCount==0 && (!((pFilterAction->NegPolAction==GUID_NEGOTIATION_ACTION_BLOCK)|| (pFilterAction->NegPolAction==GUID_NEGOTIATION_ACTION_NO_IPSEC))))
  1644. {
  1645. dwReturnCode = LoadOfferDefaults(pFilterAction->pIpsecSecMethods,pFilterAction->dwNumSecMethodCount);
  1646. if(dwReturnCode == ERROR_OUTOFMEMORY)
  1647. {
  1648. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  1649. dwReturnCode=ERROR_SUCCESS;
  1650. BAIL_OUT;
  1651. }
  1652. }
  1653. // prepare the NEG_POL structure
  1654. dwReturnCode = MakeNegotiationPolicy(&pNegPolData,pFilterAction);
  1655. //call the API
  1656. if(dwReturnCode==ERROR_SUCCESS)
  1657. {
  1658. if (pNegPolData)
  1659. {
  1660. dwReturnCode = CreateNegPolData(hPolicyStorage, pNegPolData);
  1661. }
  1662. if (dwReturnCode != ERROR_SUCCESS)
  1663. {
  1664. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTERACTION_2,pFilterAction->pszFAName);
  1665. dwReturnCode = ERROR_SUCCESS;
  1666. }
  1667. if(pNegPolData)
  1668. {
  1669. FreeNegPolData(pNegPolData);
  1670. }
  1671. }
  1672. else if(dwReturnCode==ERROR_OUTOFMEMORY)
  1673. {
  1674. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  1675. dwReturnCode=ERROR_SUCCESS;
  1676. }
  1677. else if(dwReturnCode==ERROR_INVALID_PARAMETER)
  1678. {
  1679. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_STATIC_INTERNAL_ERROR);
  1680. dwReturnCode=ERROR_SUCCESS;
  1681. }
  1682. ClosePolicyStore(hPolicyStorage);
  1683. error:
  1684. // clean up the used structures
  1685. CleanUpLocalFilterActionDataStructure(pFilterAction);
  1686. return dwReturnCode;
  1687. }
  1688. ////////////////////////////////////////////////////////////
  1689. //
  1690. //Function: FillAddFilterActionInfo( )
  1691. //
  1692. //Date of Creation: 21st Aug 2001
  1693. //
  1694. //Parameters:
  1695. // OUT PFILTERACTION* ppFilterData,
  1696. // IN PARSER_PKT & parser,
  1697. // IN const TOKEN_VALUE *vtokStaticAddFilterAction
  1698. //
  1699. //Return: DWORD
  1700. //
  1701. //Description:
  1702. // This function fills the local structure with the information got from the parser.
  1703. //
  1704. //Revision History:
  1705. //
  1706. // Date Author Comments
  1707. //
  1708. ////////////////////////////////////////////////////////////
  1709. DWORD
  1710. FillAddFilterActionInfo(
  1711. OUT PFILTERACTION* ppFilterData,
  1712. IN PARSER_PKT & parser,
  1713. IN const TOKEN_VALUE *vtokStaticAddFilterAction
  1714. )
  1715. {
  1716. DWORD dwCount=0,dwReturn =ERROR_SUCCESS,dwStrLength = 0;
  1717. PFILTERACTION pFilterAction=new FILTERACTION;
  1718. if(pFilterAction==NULL)
  1719. {
  1720. dwReturn =ERROR_OUTOFMEMORY;
  1721. BAIL_OUT;
  1722. }
  1723. memset(pFilterAction,0,sizeof(FILTERACTION));
  1724. pFilterAction->NegPolAction=GUID_NEGOTIATION_ACTION_NORMAL_IPSEC;
  1725. for(dwCount=0;dwCount<parser.MaxTok;dwCount++)
  1726. {
  1727. switch(vtokStaticAddFilterAction[parser.Cmd[dwCount].dwCmdToken].dwValue)
  1728. {
  1729. case CMD_TOKEN_DESCR :
  1730. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1731. {
  1732. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  1733. pFilterAction->pszFADescription = new _TCHAR[dwStrLength+1];
  1734. if(pFilterAction->pszFADescription==NULL)
  1735. {
  1736. dwReturn =ERROR_OUTOFMEMORY;
  1737. BAIL_OUT;
  1738. }
  1739. _tcsncpy(pFilterAction->pszFADescription, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  1740. }
  1741. break;
  1742. case CMD_TOKEN_NAME :
  1743. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1744. {
  1745. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  1746. pFilterAction->pszFAName = new _TCHAR[dwStrLength+1];
  1747. if(pFilterAction->pszFAName==NULL)
  1748. {
  1749. dwReturn =ERROR_OUTOFMEMORY;
  1750. BAIL_OUT;
  1751. }
  1752. _tcsncpy(pFilterAction->pszFAName, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  1753. }
  1754. break;
  1755. case CMD_TOKEN_INPASS :
  1756. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1757. {
  1758. if ( *(BOOL *)parser.Cmd[dwCount].pArg == TRUE)
  1759. {
  1760. pFilterAction->NegPolAction=GUID_NEGOTIATION_ACTION_INBOUND_PASSTHRU;
  1761. }
  1762. }
  1763. break;
  1764. case CMD_TOKEN_SOFT :
  1765. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1766. {
  1767. pFilterAction->bSoft = *(BOOL *)parser.Cmd[dwCount].pArg;
  1768. }
  1769. break;
  1770. case CMD_TOKEN_QMPFS :
  1771. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1772. {
  1773. pFilterAction->bQMPfs = *(BOOL *)parser.Cmd[dwCount].pArg;
  1774. }
  1775. break;
  1776. default :
  1777. break;
  1778. }
  1779. }
  1780. for(dwCount=0;dwCount<parser.MaxTok;dwCount++)
  1781. {
  1782. switch(vtokStaticAddFilterAction[parser.Cmd[dwCount].dwCmdToken].dwValue)
  1783. {
  1784. case CMD_TOKEN_ACTION :
  1785. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1786. {
  1787. if (*(DWORD *)parser.Cmd[dwCount].pArg == TOKEN_QMSEC_PERMIT )
  1788. {
  1789. pFilterAction->NegPolAction=GUID_NEGOTIATION_ACTION_NO_IPSEC;
  1790. }
  1791. else if (*(DWORD *)parser.Cmd[dwCount].pArg == TOKEN_QMSEC_BLOCK)
  1792. {
  1793. pFilterAction->NegPolAction=GUID_NEGOTIATION_ACTION_BLOCK;
  1794. }
  1795. }
  1796. break;
  1797. default :
  1798. break;
  1799. }
  1800. }
  1801. //if action is permit or block , sec methods not required
  1802. if (!(pFilterAction->NegPolAction==GUID_NEGOTIATION_ACTION_NO_IPSEC || pFilterAction->NegPolAction==GUID_NEGOTIATION_ACTION_BLOCK))
  1803. {
  1804. for(dwCount=0;dwCount<parser.MaxTok;dwCount++)
  1805. {
  1806. switch(vtokStaticAddFilterAction[parser.Cmd[dwCount].dwCmdToken].dwValue)
  1807. {
  1808. case CMD_TOKEN_QMSECMETHODS : //qmsec methods
  1809. if (parser.Cmd[dwCount].dwStatus > 0)
  1810. {
  1811. pFilterAction->dwNumSecMethodCount=parser.Cmd[dwCount].dwStatus;
  1812. pFilterAction->pIpsecSecMethods = new IPSEC_QM_OFFER[pFilterAction->dwNumSecMethodCount];
  1813. if(pFilterAction->pIpsecSecMethods==NULL)
  1814. {
  1815. dwReturn =ERROR_OUTOFMEMORY;
  1816. BAIL_OUT;
  1817. }
  1818. memset(pFilterAction->pIpsecSecMethods, 0, sizeof(IPSEC_QM_OFFER) * pFilterAction->dwNumSecMethodCount);
  1819. for(DWORD j=0;j<(parser.Cmd[dwCount].dwStatus);j++)
  1820. {
  1821. if ( ((IPSEC_QM_OFFER **)parser.Cmd[dwCount].pArg)[j] )
  1822. {
  1823. memcpy( &(pFilterAction->pIpsecSecMethods[j]),((IPSEC_QM_OFFER **)parser.Cmd[dwCount].pArg)[j],sizeof(IPSEC_QM_OFFER));
  1824. }
  1825. }
  1826. }
  1827. break;
  1828. default :
  1829. break;
  1830. }
  1831. }
  1832. }
  1833. error:
  1834. *ppFilterData=pFilterAction;
  1835. CleanUp();
  1836. return dwReturn;
  1837. }
  1838. ////////////////////////////////////////////////////////////
  1839. //
  1840. //Function: HandleStaticAddRule()
  1841. //
  1842. //Date of Creation: 21st Aug 2001
  1843. //
  1844. //Parameters:
  1845. // IN LPCWSTR pwszMachine,
  1846. // IN OUT LPWSTR *ppwcArguments,
  1847. // IN DWORD dwCurrentIndex,
  1848. // IN DWORD dwArgCount,
  1849. // IN DWORD dwFlags,
  1850. // IN LPCVOID pvData,
  1851. // OUT BOOL *pbDone
  1852. //
  1853. //Return: DWORD
  1854. //
  1855. //Description
  1856. // Implementation for the command "Add Rule"
  1857. //
  1858. //Revision History:
  1859. //
  1860. // Date Author Comments
  1861. //
  1862. ////////////////////////////////////////////////////////////
  1863. DWORD WINAPI
  1864. HandleStaticAddRule(
  1865. IN LPCWSTR pwszMachine,
  1866. IN OUT LPWSTR *ppwcArguments,
  1867. IN DWORD dwCurrentIndex,
  1868. IN DWORD dwArgCount,
  1869. IN DWORD dwFlags,
  1870. IN LPCVOID pvData,
  1871. OUT BOOL *pbDone
  1872. )
  1873. {
  1874. DWORD dwRet = ERROR_SHOW_USAGE;
  1875. PRULEDATA pRuleData=NULL;
  1876. PARSER_PKT parser;
  1877. ZeroMemory(&parser, sizeof(parser));
  1878. const TAG_TYPE vcmdStaticAddRule[] =
  1879. {
  1880. { CMD_TOKEN_STR_NAME, NS_REQ_PRESENT, FALSE },
  1881. { CMD_TOKEN_STR_POLICY, NS_REQ_PRESENT, FALSE },
  1882. { CMD_TOKEN_STR_FILTERLIST, NS_REQ_PRESENT, FALSE },
  1883. { CMD_TOKEN_STR_FILTERACTION, NS_REQ_PRESENT, FALSE },
  1884. { CMD_TOKEN_STR_TUNNEL, NS_REQ_ZERO, FALSE },
  1885. { CMD_TOKEN_STR_CONNTYPE, NS_REQ_ZERO, FALSE },
  1886. { CMD_TOKEN_STR_ACTIVATE, NS_REQ_ZERO, FALSE },
  1887. { CMD_TOKEN_STR_DESCR, NS_REQ_ZERO, FALSE },
  1888. { CMD_TOKEN_STR_KERB, NS_REQ_ZERO, FALSE },
  1889. { CMD_TOKEN_STR_PSK, NS_REQ_ZERO, FALSE }
  1890. };
  1891. const TOKEN_VALUE vtokStaticAddRule[] =
  1892. {
  1893. { CMD_TOKEN_STR_NAME, CMD_TOKEN_NAME },
  1894. { CMD_TOKEN_STR_POLICY, CMD_TOKEN_POLICY },
  1895. { CMD_TOKEN_STR_FILTERLIST, CMD_TOKEN_FILTERLIST },
  1896. { CMD_TOKEN_STR_FILTERACTION, CMD_TOKEN_FILTERACTION },
  1897. { CMD_TOKEN_STR_TUNNEL, CMD_TOKEN_TUNNEL },
  1898. { CMD_TOKEN_STR_CONNTYPE, CMD_TOKEN_CONNTYPE },
  1899. { CMD_TOKEN_STR_ACTIVATE, CMD_TOKEN_ACTIVATE },
  1900. { CMD_TOKEN_STR_DESCR, CMD_TOKEN_DESCR },
  1901. { CMD_TOKEN_STR_KERB, CMD_TOKEN_KERB },
  1902. { CMD_TOKEN_STR_PSK, CMD_TOKEN_PSK }
  1903. };
  1904. const TOKEN_VALUE vlistStaticAddRule[] =
  1905. {
  1906. { CMD_TOKEN_STR_ROOTCA, CMD_TOKEN_ROOTCA },
  1907. };
  1908. //if the user asked for usage, delegate the responsibility to netsh
  1909. if(dwArgCount <=3)
  1910. {
  1911. dwRet = ERROR_SHOW_USAGE;
  1912. BAIL_OUT;
  1913. }
  1914. parser.ValidTok = vtokStaticAddRule;
  1915. parser.MaxTok = SIZEOF_TOKEN_VALUE(vtokStaticAddRule);
  1916. parser.ValidCmd = vcmdStaticAddRule;
  1917. parser.MaxCmd = SIZEOF_TAG_TYPE(vcmdStaticAddRule);
  1918. parser.ValidList = vlistStaticAddRule;
  1919. parser.MaxList = SIZEOF_TOKEN_VALUE(vlistStaticAddRule);
  1920. dwRet = Parser(pwszMachine,ppwcArguments,dwCurrentIndex,dwArgCount,&parser);
  1921. if(dwRet != ERROR_SUCCESS)
  1922. {
  1923. CleanUp();
  1924. if (dwRet == RETURN_NO_ERROR)
  1925. {
  1926. dwRet = ERROR_SUCCESS;
  1927. }
  1928. BAIL_OUT;
  1929. }
  1930. // get parsed user input
  1931. dwRet = FillAddRuleInfo(&pRuleData,parser,vtokStaticAddRule);
  1932. if(dwRet==ERROR_OUTOFMEMORY)
  1933. {
  1934. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  1935. dwRet=ERROR_SUCCESS;
  1936. BAIL_OUT;
  1937. }
  1938. if(pRuleData)
  1939. {
  1940. //if tunnel specified, validate it
  1941. if (pRuleData->bTunnel)
  1942. {
  1943. if(!bIsValidIPAddress(htonl(pRuleData->TunnelIPAddress),TRUE,TRUE))
  1944. {
  1945. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_RULE_INVALID_TUNNEL);
  1946. dwRet=ERROR_SUCCESS;
  1947. BAIL_OUT;
  1948. }
  1949. }
  1950. //create new rule
  1951. CreateNewRule(pRuleData);
  1952. dwRet=ERROR_SUCCESS;
  1953. }
  1954. error:
  1955. CleanUpLocalRuleDataStructure(pRuleData);
  1956. return dwRet;
  1957. }
  1958. ////////////////////////////////////////////////////////////
  1959. //
  1960. //Function: CreateNewRule()
  1961. //
  1962. //Date of Creation: 21st Aug 2001
  1963. //
  1964. //Parameters:
  1965. // IN PRULEDATA pRuleData
  1966. //
  1967. //Return: DWORD
  1968. //
  1969. //Description:
  1970. // Creates a new rule based on the user input
  1971. //
  1972. //Revision History:
  1973. //
  1974. // Date Author Comments
  1975. //
  1976. ////////////////////////////////////////////////////////////
  1977. DWORD
  1978. CreateNewRule(
  1979. IN PRULEDATA pRuleData
  1980. )
  1981. {
  1982. PIPSEC_POLICY_DATA pPolicyData = NULL;
  1983. PIPSEC_NEGPOL_DATA pNegPolData = NULL;
  1984. PIPSEC_FILTER_DATA pFilterData = NULL;
  1985. HANDLE hPolicyStorage = NULL;
  1986. BOOL bPolicyExists=FALSE,bFAExists=FALSE,bFLExists=FALSE;
  1987. BOOL bFilterExists=FALSE,bRuleExists=FALSE;
  1988. DWORD dwReturnCode = ERROR_SUCCESS;
  1989. // check for all mandatory names
  1990. if (!pRuleData->pszRuleName || (pRuleData->pszRuleName[0] == _TEXT('\0')) )
  1991. {
  1992. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_RULE_1);
  1993. return ERROR_INVALID_DATA;
  1994. }
  1995. if (!pRuleData->pszPolicyName || (pRuleData->pszPolicyName[0] == _TEXT('\0')) )
  1996. {
  1997. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_POLICY_MISSING_POL_NAME);
  1998. return ERROR_INVALID_DATA;
  1999. }
  2000. if (!pRuleData->pszFLName || (pRuleData->pszFLName[0] == _TEXT('\0')) )
  2001. {
  2002. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTERLIST_1);
  2003. return ERROR_INVALID_DATA;
  2004. }
  2005. if (!pRuleData->pszFAName || (pRuleData->pszFAName[0] == _TEXT('\0')) )
  2006. {
  2007. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_RULE_2);
  2008. return ERROR_INVALID_DATA;
  2009. }
  2010. if(!pRuleData->bConnectionTypeSpecified)
  2011. {
  2012. pRuleData->ConnectionType= INTERFACE_TYPE_ALL;
  2013. }
  2014. dwReturnCode = OpenPolicyStore(&hPolicyStorage);
  2015. if (dwReturnCode != ERROR_SUCCESS)
  2016. {
  2017. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_FAILED_POLSTORE_OPEN);
  2018. dwReturnCode = ERROR_SUCCESS;
  2019. BAIL_OUT;
  2020. }
  2021. //check for duplicate names in polstore
  2022. if(g_NshPolStoreHandle.GetBatchmodeStatus())
  2023. {
  2024. bPolicyExists=g_NshPolNegFilData.GetPolicyFromCacheByName(pRuleData->pszPolicyName,&pPolicyData);
  2025. }
  2026. if(!bPolicyExists)
  2027. {
  2028. if(!(bPolicyExists=GetPolicyFromStore(&pPolicyData,pRuleData->pszPolicyName,hPolicyStorage)))
  2029. {
  2030. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_RULE_3,pRuleData->pszPolicyName);
  2031. dwReturnCode=ERROR_INVALID_DATA;
  2032. BAIL_OUT;
  2033. }
  2034. }
  2035. //check for readonly flag
  2036. if(pPolicyData->dwFlags & POLSTORE_READONLY )
  2037. {
  2038. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SET_POL_READ_ONLY_OBJECT,pPolicyData->pszIpsecName);
  2039. dwReturnCode=ERROR_SUCCESS;
  2040. BAIL_OUT;
  2041. }
  2042. if (bPolicyExists && (bRuleExists=CheckForRuleExistance(pPolicyData,pRuleData->pszRuleName)))
  2043. {
  2044. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_RULE_4,pRuleData->pszRuleName,pRuleData->pszPolicyName);
  2045. dwReturnCode=ERROR_INVALID_DATA;
  2046. BAIL_OUT;
  2047. }
  2048. //check for filterlist & filteractions, if not available bail out
  2049. if(g_NshPolStoreHandle.GetBatchmodeStatus())
  2050. {
  2051. bFAExists=g_NshPolNegFilData.GetNegPolFromCacheByName(pRuleData->pszFAName,&pNegPolData);
  2052. }
  2053. if(!bFAExists)
  2054. {
  2055. if(!(bFAExists=GetNegPolFromStore(&pNegPolData,pRuleData->pszFAName,hPolicyStorage)))
  2056. {
  2057. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_RULE_5,pRuleData->pszFAName);
  2058. dwReturnCode=ERROR_INVALID_DATA;
  2059. BAIL_OUT;
  2060. }
  2061. }
  2062. if(g_NshPolStoreHandle.GetBatchmodeStatus())
  2063. {
  2064. bFLExists=g_NshPolNegFilData.GetFilterListFromCacheByName(pRuleData->pszFLName,&pFilterData);
  2065. }
  2066. if(!bFLExists)
  2067. {
  2068. if(!(bFLExists=GetFilterListFromStore(&pFilterData,pRuleData->pszFLName,hPolicyStorage,bFilterExists)))
  2069. {
  2070. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTER_1,pRuleData->pszFLName);
  2071. dwReturnCode=ERROR_INVALID_DATA;
  2072. BAIL_OUT;
  2073. }
  2074. }
  2075. else // if no filters bail out
  2076. {
  2077. if(pFilterData->dwNumFilterSpecs > 0)
  2078. {
  2079. bFilterExists=TRUE;
  2080. }
  2081. }
  2082. if( !bFilterExists && bFLExists)
  2083. {
  2084. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_RULE_6,pRuleData->pszFLName);
  2085. dwReturnCode=ERROR_INVALID_DATA;
  2086. BAIL_OUT;
  2087. }
  2088. //if everything is in place , proceed
  2089. if(bPolicyExists && bFAExists && bFLExists && bFilterExists)
  2090. {
  2091. dwReturnCode=AddRule(pPolicyData, pRuleData, pNegPolData, pFilterData,hPolicyStorage ) ;
  2092. if(dwReturnCode!=ERROR_SUCCESS)
  2093. {
  2094. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_RULE_7,pRuleData->pszRuleName);
  2095. }
  2096. }
  2097. ClosePolicyStore(hPolicyStorage);
  2098. if(pPolicyData)
  2099. {
  2100. FreePolicyData(pPolicyData);
  2101. }
  2102. error:
  2103. return dwReturnCode;
  2104. }
  2105. ////////////////////////////////////////////////////////////
  2106. //
  2107. //Function: CheckRuleExistance( )
  2108. //
  2109. //Date of Creation: 21st Aug 2001
  2110. //
  2111. //Parameters:
  2112. // IN PIPSEC_POLICY_DATA pPolicy,
  2113. // IN PRULEDATA pRuleData
  2114. //
  2115. //Return: BOOL
  2116. //
  2117. //Description:
  2118. // This function checks whether the user specified rule already exists.
  2119. //
  2120. //Revision History:
  2121. //
  2122. // Date Author Comments
  2123. //
  2124. ////////////////////////////////////////////////////////////
  2125. BOOL
  2126. CheckForRuleExistance(
  2127. IN PIPSEC_POLICY_DATA pPolicy,
  2128. IN LPTSTR pszRuleName
  2129. )
  2130. {
  2131. BOOL bRuleExists=FALSE;
  2132. //check whether the specified rule already exists
  2133. for (DWORD n = 0; n < pPolicy->dwNumNFACount ; n++)
  2134. {
  2135. if (pPolicy->ppIpsecNFAData[n]->pszIpsecName && pszRuleName &&(_tcscmp(pPolicy->ppIpsecNFAData[n]->pszIpsecName,pszRuleName)==0))
  2136. {
  2137. bRuleExists=TRUE;
  2138. break;
  2139. }
  2140. }
  2141. return bRuleExists;
  2142. }
  2143. ////////////////////////////////////////////////////////////
  2144. //
  2145. //Function: FillAddRuleInfo( )
  2146. //
  2147. //Date of Creation: 21st Aug 2001
  2148. //
  2149. //Parameters:
  2150. // OUT PRULEDATA* ppFilterData,
  2151. // IN PARSER_PKT & parser,
  2152. // IN const TOKEN_VALUE *vtokStaticAddRule,
  2153. // IN const TOKEN_VALUE *vlistStaticAddRule
  2154. //
  2155. //Return: DWORD
  2156. //
  2157. //Description:
  2158. // This function fills the local structure with the information got from the parser.
  2159. //
  2160. //Revision History:
  2161. //
  2162. // Date Author Comments
  2163. //
  2164. ////////////////////////////////////////////////////////////
  2165. DWORD
  2166. FillAddRuleInfo(
  2167. OUT PRULEDATA* ppRuleData,
  2168. IN PARSER_PKT & parser,
  2169. IN const TOKEN_VALUE *vtokStaticAddRule
  2170. )
  2171. {
  2172. DWORD dwCount=0, dwReturn=ERROR_SUCCESS , dwStrLength = 0;
  2173. PRULEDATA pRuleData=new RULEDATA;
  2174. PSTA_AUTH_METHODS pKerbAuth = NULL;
  2175. PSTA_AUTH_METHODS pPskAuth = NULL;
  2176. PSTA_MM_AUTH_METHODS *ppRootcaMMAuth = NULL;
  2177. if(pRuleData == NULL)
  2178. {
  2179. dwReturn = ERROR_OUTOFMEMORY;
  2180. BAIL_OUT;
  2181. }
  2182. memset(pRuleData,0,sizeof(RULEDATA));
  2183. // default is that new rule is active
  2184. pRuleData->bActivate = TRUE;
  2185. for(dwCount=0; dwCount<parser.MaxTok; dwCount++)
  2186. {
  2187. switch(vtokStaticAddRule[parser.Cmd[dwCount].dwCmdToken].dwValue)
  2188. {
  2189. case CMD_TOKEN_NAME :
  2190. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  2191. {
  2192. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  2193. pRuleData->pszRuleName = new _TCHAR[dwStrLength+1];
  2194. if(pRuleData->pszRuleName == NULL)
  2195. {
  2196. dwReturn = ERROR_OUTOFMEMORY;
  2197. BAIL_OUT;
  2198. }
  2199. _tcsncpy(pRuleData->pszRuleName, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  2200. }
  2201. break;
  2202. case CMD_TOKEN_POLICY :
  2203. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  2204. {
  2205. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  2206. pRuleData->pszPolicyName = new _TCHAR[dwStrLength+1];
  2207. if(pRuleData->pszPolicyName == NULL)
  2208. {
  2209. dwReturn = ERROR_OUTOFMEMORY;
  2210. BAIL_OUT;
  2211. }
  2212. _tcsncpy(pRuleData->pszPolicyName, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  2213. }
  2214. break;
  2215. case CMD_TOKEN_DESCR :
  2216. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  2217. {
  2218. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  2219. pRuleData->pszRuleDescription = new _TCHAR[dwStrLength+1];
  2220. if(pRuleData->pszRuleDescription == NULL)
  2221. {
  2222. dwReturn = ERROR_OUTOFMEMORY;
  2223. BAIL_OUT;
  2224. }
  2225. _tcsncpy(pRuleData->pszRuleDescription, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  2226. }
  2227. break;
  2228. case CMD_TOKEN_FILTERLIST :
  2229. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  2230. {
  2231. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  2232. pRuleData->pszFLName = new _TCHAR[dwStrLength+1];
  2233. if(pRuleData->pszFLName == NULL)
  2234. {
  2235. dwReturn = ERROR_OUTOFMEMORY;
  2236. BAIL_OUT;
  2237. }
  2238. _tcsncpy(pRuleData->pszFLName, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  2239. }
  2240. break;
  2241. case CMD_TOKEN_FILTERACTION :
  2242. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  2243. {
  2244. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  2245. pRuleData->pszFAName = new _TCHAR[dwStrLength+1];
  2246. if(pRuleData->pszFAName == NULL)
  2247. {
  2248. dwReturn = ERROR_OUTOFMEMORY;
  2249. BAIL_OUT;
  2250. }
  2251. _tcsncpy(pRuleData->pszFAName, (LPTSTR )parser.Cmd[dwCount].pArg,dwStrLength+1);
  2252. }
  2253. break;
  2254. case CMD_TOKEN_TUNNEL :
  2255. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  2256. {
  2257. pRuleData->TunnelIPAddress = *(IPADDR *)parser.Cmd[dwCount].pArg;
  2258. if(pRuleData->TunnelIPAddress)
  2259. {
  2260. pRuleData->bTunnel=TRUE;
  2261. }
  2262. else
  2263. {
  2264. pRuleData->bTunnel=FALSE;
  2265. }
  2266. ADDR addr;
  2267. addr.uIpAddr = ntohl(pRuleData->TunnelIPAddress);
  2268. addr.uSubNetMask = 0;
  2269. if (!IsValidTunnelEndpointAddress(&addr))
  2270. {
  2271. dwReturn = ERROR_INVALID_PARAMETER;
  2272. BAIL_OUT;
  2273. }
  2274. }
  2275. else
  2276. {
  2277. // if special server specified, give a warning, and proceed with no tunnel
  2278. switch(parser.Cmd[dwCount].dwStatus)
  2279. {
  2280. case SERVER_DNS :
  2281. case SERVER_WINS :
  2282. case SERVER_DHCP :
  2283. case SERVER_GATEWAY :
  2284. default :PrintMessageFromModule(g_hModule,ADD_STATIC_RULE_INVALID_TUNNEL);
  2285. break;
  2286. }
  2287. }
  2288. break;
  2289. case CMD_TOKEN_CONNTYPE :
  2290. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  2291. {
  2292. pRuleData->ConnectionType = *(IF_TYPE *)parser.Cmd[dwCount].pArg;
  2293. pRuleData->bConnectionTypeSpecified=TRUE;
  2294. }
  2295. break;
  2296. case CMD_TOKEN_ACTIVATE :
  2297. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  2298. {
  2299. pRuleData->bActivate = *(BOOL *)parser.Cmd[dwCount].pArg;
  2300. }
  2301. break;
  2302. case CMD_TOKEN_KERB :
  2303. if (parser.Cmd[dwCount].dwStatus == VALID_TOKEN)
  2304. {
  2305. pRuleData->bAuthMethodSpecified = TRUE;
  2306. ++pRuleData->AuthInfos.dwNumAuthInfos;
  2307. pKerbAuth = (PSTA_AUTH_METHODS)parser.Cmd[dwCount].pArg;
  2308. }
  2309. break;
  2310. case CMD_TOKEN_PSK :
  2311. if (parser.Cmd[dwCount].dwStatus == VALID_TOKEN)
  2312. {
  2313. pRuleData->bAuthMethodSpecified = TRUE;
  2314. ++pRuleData->AuthInfos.dwNumAuthInfos;
  2315. pPskAuth = (PSTA_AUTH_METHODS)parser.Cmd[dwCount].pArg;
  2316. }
  2317. break;
  2318. case CMD_TOKEN_ROOTCA :
  2319. // this case is special, handled below...
  2320. break;
  2321. default :
  2322. break;
  2323. }
  2324. }
  2325. size_t uiRootcaIndex = parser.MaxTok;
  2326. if (parser.Cmd[uiRootcaIndex].dwStatus > 0)
  2327. {
  2328. pRuleData->bAuthMethodSpecified = TRUE;
  2329. pRuleData->AuthInfos.dwNumAuthInfos += parser.Cmd[uiRootcaIndex].dwStatus;
  2330. ppRootcaMMAuth = (PSTA_MM_AUTH_METHODS *)(parser.Cmd[uiRootcaIndex].pArg);
  2331. }
  2332. dwReturn = AddAllAuthMethods(pRuleData, pKerbAuth, pPskAuth, ppRootcaMMAuth, TRUE);
  2333. if (dwReturn != ERROR_SUCCESS)
  2334. {
  2335. BAIL_OUT;
  2336. }
  2337. error: // if default auth loading failed, clean up everything and make 'pRuleData' as NULL
  2338. CleanupAuthData(&pKerbAuth, &pPskAuth, ppRootcaMMAuth);
  2339. if(dwReturn==ERROR_SUCCESS && pRuleData->dwAuthInfos > 0)
  2340. {
  2341. *ppRuleData=pRuleData;
  2342. }
  2343. else
  2344. {
  2345. if(dwReturn != ERROR_OUTOFMEMORY)
  2346. {
  2347. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_RULE_7,pRuleData->pszRuleName);
  2348. }
  2349. else
  2350. {
  2351. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  2352. }
  2353. CleanUpLocalRuleDataStructure(pRuleData);
  2354. }
  2355. CleanUp();
  2356. return dwReturn;
  2357. }
  2358. ////////////////////////////////////////////////////////////
  2359. //
  2360. //Function: AddDefaultResponseRule()
  2361. //
  2362. //Date of Creation: 21st Aug 2001
  2363. //
  2364. //Parameters:
  2365. // IN OUT PIPSEC_POLICY_DATA pPolicy,
  2366. // IN HANDLE hPolicyStorage,
  2367. // IN BOOL bActivateDefaultRule
  2368. //Return: DWORD
  2369. //
  2370. //Description:
  2371. // This function adds the NFA structure to the policy for default response rule
  2372. //
  2373. //Revision History:
  2374. //
  2375. // Date Author Comments
  2376. //
  2377. ////////////////////////////////////////////////////////////
  2378. DWORD
  2379. AddDefaultResponseRule(
  2380. IN OUT PIPSEC_POLICY_DATA pPolicy,
  2381. IN HANDLE hPolicyStorage,
  2382. IN BOOL bActivateDefaultRule,
  2383. IN BOOL bActivateDefaultRuleSpecified
  2384. )
  2385. {
  2386. DWORD dwReturn = ERROR_SUCCESS;
  2387. BOOL bCertConversionSuceeded=TRUE;
  2388. PIPSEC_NFA_DATA pRule = MakeDefaultResponseRule(bActivateDefaultRule,bActivateDefaultRuleSpecified);
  2389. // form policy data structures
  2390. if(pRule ==NULL)
  2391. {
  2392. dwReturn = ERROR_OUTOFMEMORY;
  2393. BAIL_OUT;
  2394. }
  2395. // if cert specified, and decode/ancode failed, bail out
  2396. if(!bCertConversionSuceeded)
  2397. {
  2398. dwReturn=ERROR_INVALID_DATA;
  2399. BAIL_OUT;
  2400. }
  2401. pPolicy->dwNumNFACount=1;
  2402. pPolicy->ppIpsecNFAData = (PIPSEC_NFA_DATA *) IPSecAllocPolMem(sizeof(PIPSEC_NFA_DATA));
  2403. if(pPolicy->ppIpsecNFAData ==NULL)
  2404. {
  2405. dwReturn = ERROR_OUTOFMEMORY;
  2406. BAIL_OUT;
  2407. }
  2408. // call the APIs
  2409. pPolicy->ppIpsecNFAData[pPolicy->dwNumNFACount-1] = pRule;
  2410. pRule->pIpsecNegPolData->NegPolType = GUID_NEGOTIATION_TYPE_DEFAULT;
  2411. //create default negpol
  2412. dwReturn = IPSecCreateNegPolData(hPolicyStorage, pRule->pIpsecNegPolData);
  2413. if (dwReturn == ERROR_SUCCESS)
  2414. {
  2415. dwReturn=CreatePolicyData(hPolicyStorage, pPolicy);
  2416. if(dwReturn==ERROR_SUCCESS)
  2417. {
  2418. dwReturn =IPSecCreateNFAData(hPolicyStorage,pPolicy->PolicyIdentifier, pRule);
  2419. if(dwReturn!=ERROR_SUCCESS)
  2420. {
  2421. IPSecDeleteISAKMPData(hPolicyStorage, pPolicy->ISAKMPIdentifier);
  2422. IPSecDeleteNegPolData(hPolicyStorage, pRule->NegPolIdentifier);
  2423. DeletePolicyData(hPolicyStorage, pPolicy);
  2424. }
  2425. }
  2426. else
  2427. {
  2428. IPSecDeleteNegPolData(hPolicyStorage, pRule->NegPolIdentifier);
  2429. }
  2430. }
  2431. error:
  2432. if(dwReturn == ERROR_OUTOFMEMORY)
  2433. {
  2434. dwReturn = ERROR_SUCCESS;
  2435. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  2436. }
  2437. return dwReturn;
  2438. }
  2439. ////////////////////////////////////////////////////////////
  2440. //
  2441. //Function: MakeDefaultResponseRule()
  2442. //
  2443. //Date of Creation: 21st Aug 2001
  2444. //
  2445. //Parameters:
  2446. // IN BOOL bActivate,
  2447. // IN BOOL bActivateSpecified
  2448. //
  2449. //Return: PIPSEC_NFA_DATA
  2450. //
  2451. //Description:
  2452. // This function fills the NFA structure for default response rule
  2453. //
  2454. //Revision History:
  2455. //
  2456. // Date Author Comments
  2457. //
  2458. ////////////////////////////////////////////////////////////
  2459. PIPSEC_NFA_DATA
  2460. MakeDefaultResponseRule (
  2461. IN BOOL bActivate,
  2462. IN BOOL bActivateSpecified
  2463. )
  2464. {
  2465. RPC_STATUS RpcStat =RPC_S_OK , dwReturn = ERROR_SUCCESS;
  2466. PIPSEC_NFA_DATA pRule = (PIPSEC_NFA_DATA) IPSecAllocPolMem(sizeof(IPSEC_NFA_DATA));
  2467. if(pRule == NULL)
  2468. {
  2469. dwReturn = ERROR_OUTOFMEMORY;
  2470. BAIL_OUT;
  2471. }
  2472. memset(pRule,0,sizeof(IPSEC_NFA_DATA));
  2473. //no name, desc, interface name or endpoint name dor default rule
  2474. pRule->pszIpsecName = pRule->pszDescription = pRule->pszInterfaceName = pRule->pszEndPointName = NULL;
  2475. RpcStat = UuidCreate(&(pRule->NFAIdentifier));
  2476. if (!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  2477. {
  2478. dwReturn=ERROR_INVALID_PARAMETER;
  2479. BAIL_OUT;
  2480. }
  2481. pRule->dwWhenChanged = 0;
  2482. // filter list
  2483. pRule->pIpsecFilterData = NULL;
  2484. RpcStat = UuidCreateNil(&(pRule->FilterIdentifier));
  2485. if (!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  2486. {
  2487. dwReturn=ERROR_INVALID_PARAMETER;
  2488. BAIL_OUT;
  2489. }
  2490. pRule->pIpsecNegPolData = MakeDefaultResponseNegotiationPolicy ();
  2491. if(pRule->pIpsecNegPolData == NULL)
  2492. {
  2493. dwReturn = ERROR_OUTOFMEMORY;
  2494. BAIL_OUT;
  2495. }
  2496. pRule->NegPolIdentifier = pRule->pIpsecNegPolData->NegPolIdentifier;
  2497. // tunnel address
  2498. pRule->dwTunnelFlags = 0;
  2499. // interface type
  2500. pRule->dwInterfaceType = (DWORD)PAS_INTERFACE_TYPE_ALL;
  2501. // active flag
  2502. if(bActivateSpecified)
  2503. {
  2504. pRule->dwActiveFlag = bActivate;
  2505. }
  2506. else
  2507. {
  2508. pRule->dwActiveFlag = TRUE;
  2509. }
  2510. // auth methods = Kerberos for the time being
  2511. pRule->dwAuthMethodCount = 1;
  2512. pRule->ppAuthMethods = (PIPSEC_AUTH_METHOD *) IPSecAllocPolMem(pRule->dwAuthMethodCount * sizeof(PIPSEC_AUTH_METHOD));
  2513. if(pRule->ppAuthMethods==NULL)
  2514. {
  2515. dwReturn = ERROR_OUTOFMEMORY;
  2516. BAIL_OUT;
  2517. }
  2518. pRule->ppAuthMethods[0] = (PIPSEC_AUTH_METHOD) IPSecAllocPolMem(sizeof(IPSEC_AUTH_METHOD));
  2519. pRule->ppAuthMethods[0]->dwAuthType = IKE_SSPI;
  2520. pRule->ppAuthMethods[0]->dwAuthLen = 0;
  2521. pRule->ppAuthMethods[0]->pszAuthMethod = NULL;
  2522. error:
  2523. if(dwReturn == ERROR_OUTOFMEMORY || dwReturn == ERROR_INVALID_PARAMETER)
  2524. {
  2525. if(pRule)
  2526. {
  2527. CleanUpAuthInfo(pRule); //this function frees only auth info.
  2528. IPSecFreePolMem(pRule); //since the above fn is used in other fns also, this free is required to cleanup other rule memory
  2529. pRule = NULL;
  2530. }
  2531. }
  2532. return pRule;
  2533. }
  2534. ////////////////////////////////////////////////////////////
  2535. //
  2536. //Function: MakeDefaultResponseNegotiationPolicy()
  2537. //
  2538. //Date of Creation: 21st Aug 2001
  2539. //
  2540. //Parameters:
  2541. // VOID
  2542. //
  2543. //Return: PIPSEC_NEGPOL_DATA
  2544. //
  2545. //Description:
  2546. // This function fills the NegPol structure for default response rule
  2547. //
  2548. //Revision History:
  2549. //
  2550. // Date Author Comments
  2551. //
  2552. ////////////////////////////////////////////////////////////
  2553. PIPSEC_NEGPOL_DATA
  2554. MakeDefaultResponseNegotiationPolicy (
  2555. VOID
  2556. )
  2557. {
  2558. RPC_STATUS RpcStat=RPC_S_OK;
  2559. DWORD dwReturn = ERROR_SUCCESS;
  2560. _TCHAR pFAName[MAXSTRLEN]={0};
  2561. PIPSEC_NEGPOL_DATA pNegPol = (PIPSEC_NEGPOL_DATA) IPSecAllocPolMem(sizeof(IPSEC_NEGPOL_DATA));
  2562. if(pNegPol == NULL)
  2563. {
  2564. dwReturn = ERROR_OUTOFMEMORY;
  2565. BAIL_OUT;
  2566. }
  2567. RpcStat = UuidCreate(&(pNegPol->NegPolIdentifier));
  2568. if (!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  2569. {
  2570. dwReturn=ERROR_INVALID_PARAMETER;
  2571. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_STATIC_INTERNAL_ERROR);
  2572. BAIL_OUT;
  2573. }
  2574. pNegPol->NegPolAction = GUID_NEGOTIATION_ACTION_NORMAL_IPSEC;
  2575. pNegPol->NegPolType = GUID_NEGOTIATION_TYPE_DEFAULT;
  2576. pNegPol->dwSecurityMethodCount = 6;
  2577. // allocate sec.methods
  2578. pNegPol->pIpsecSecurityMethods = (IPSEC_SECURITY_METHOD *) IPSecAllocPolMem(pNegPol->dwSecurityMethodCount * sizeof(IPSEC_SECURITY_METHOD));
  2579. if(pNegPol->pIpsecSecurityMethods==NULL)
  2580. {
  2581. dwReturn = ERROR_OUTOFMEMORY;
  2582. BAIL_OUT;
  2583. }
  2584. // method 0 - ESP[3DES, SHA1]
  2585. pNegPol->pIpsecSecurityMethods[0].Lifetime.KeyExpirationBytes = 0;
  2586. pNegPol->pIpsecSecurityMethods[0].Lifetime.KeyExpirationTime = 0;
  2587. pNegPol->pIpsecSecurityMethods[0].Flags = 0;
  2588. pNegPol->pIpsecSecurityMethods[0].PfsQMRequired = FALSE;
  2589. pNegPol->pIpsecSecurityMethods[0].Count = 1;
  2590. pNegPol->pIpsecSecurityMethods[0].Algos[0].algoIdentifier = CONF_ALGO_3_DES;
  2591. pNegPol->pIpsecSecurityMethods[0].Algos[0].secondaryAlgoIdentifier = AUTH_ALGO_SHA1;
  2592. pNegPol->pIpsecSecurityMethods[0].Algos[0].algoKeylen = 0;
  2593. pNegPol->pIpsecSecurityMethods[0].Algos[0].algoRounds = 0;
  2594. pNegPol->pIpsecSecurityMethods[0].Algos[0].operation = Encrypt;
  2595. // method 1 - ESP[3DES, MD5]
  2596. pNegPol->pIpsecSecurityMethods[1].Lifetime.KeyExpirationBytes = 0;
  2597. pNegPol->pIpsecSecurityMethods[1].Lifetime.KeyExpirationTime = 0;
  2598. pNegPol->pIpsecSecurityMethods[1].Flags = 0;
  2599. pNegPol->pIpsecSecurityMethods[1].PfsQMRequired = FALSE;
  2600. pNegPol->pIpsecSecurityMethods[1].Count = 1;
  2601. pNegPol->pIpsecSecurityMethods[1].Algos[0].algoIdentifier = CONF_ALGO_3_DES;
  2602. pNegPol->pIpsecSecurityMethods[1].Algos[0].secondaryAlgoIdentifier = AUTH_ALGO_MD5;
  2603. pNegPol->pIpsecSecurityMethods[1].Algos[0].algoKeylen = 0;
  2604. pNegPol->pIpsecSecurityMethods[1].Algos[0].algoRounds = 0;
  2605. pNegPol->pIpsecSecurityMethods[1].Algos[0].operation = Encrypt;
  2606. // method 2 - ESP[DES, SHA1]
  2607. pNegPol->pIpsecSecurityMethods[2].Lifetime.KeyExpirationBytes = 0;
  2608. pNegPol->pIpsecSecurityMethods[2].Lifetime.KeyExpirationTime = 0;
  2609. pNegPol->pIpsecSecurityMethods[2].Flags = 0;
  2610. pNegPol->pIpsecSecurityMethods[2].PfsQMRequired = FALSE;
  2611. pNegPol->pIpsecSecurityMethods[2].Count = 1;
  2612. pNegPol->pIpsecSecurityMethods[2].Algos[0].algoIdentifier = CONF_ALGO_DES;
  2613. pNegPol->pIpsecSecurityMethods[2].Algos[0].secondaryAlgoIdentifier = AUTH_ALGO_SHA1;
  2614. pNegPol->pIpsecSecurityMethods[2].Algos[0].algoKeylen = 0;
  2615. pNegPol->pIpsecSecurityMethods[2].Algos[0].algoRounds = 0;
  2616. pNegPol->pIpsecSecurityMethods[2].Algos[0].operation = Encrypt;
  2617. // method 3 - ESP[DES, MD5]
  2618. pNegPol->pIpsecSecurityMethods[3].Lifetime.KeyExpirationBytes = 0;
  2619. pNegPol->pIpsecSecurityMethods[3].Lifetime.KeyExpirationTime = 0;
  2620. pNegPol->pIpsecSecurityMethods[3].Flags = 0;
  2621. pNegPol->pIpsecSecurityMethods[3].PfsQMRequired = FALSE;
  2622. pNegPol->pIpsecSecurityMethods[3].Count = 1;
  2623. pNegPol->pIpsecSecurityMethods[3].Algos[0].algoIdentifier = CONF_ALGO_DES;
  2624. pNegPol->pIpsecSecurityMethods[3].Algos[0].secondaryAlgoIdentifier = AUTH_ALGO_MD5;
  2625. pNegPol->pIpsecSecurityMethods[3].Algos[0].algoKeylen = 0;
  2626. pNegPol->pIpsecSecurityMethods[3].Algos[0].algoRounds = 0;
  2627. pNegPol->pIpsecSecurityMethods[3].Algos[0].operation = Encrypt;
  2628. // method 4 - AH[SHA1]
  2629. pNegPol->pIpsecSecurityMethods[4].Lifetime.KeyExpirationBytes = 0;
  2630. pNegPol->pIpsecSecurityMethods[4].Lifetime.KeyExpirationTime = 0;
  2631. pNegPol->pIpsecSecurityMethods[4].Flags = 0;
  2632. pNegPol->pIpsecSecurityMethods[4].PfsQMRequired = FALSE;
  2633. pNegPol->pIpsecSecurityMethods[4].Count = 1;
  2634. pNegPol->pIpsecSecurityMethods[4].Algos[0].algoIdentifier = AUTH_ALGO_SHA1;
  2635. pNegPol->pIpsecSecurityMethods[4].Algos[0].secondaryAlgoIdentifier = AUTH_ALGO_NONE;
  2636. pNegPol->pIpsecSecurityMethods[4].Algos[0].algoKeylen = 0;
  2637. pNegPol->pIpsecSecurityMethods[4].Algos[0].algoRounds = 0;
  2638. pNegPol->pIpsecSecurityMethods[4].Algos[0].operation = Auth;
  2639. // method 5 - AH[MD5]
  2640. pNegPol->pIpsecSecurityMethods[5].Lifetime.KeyExpirationBytes = 0;
  2641. pNegPol->pIpsecSecurityMethods[5].Lifetime.KeyExpirationTime = 0;
  2642. pNegPol->pIpsecSecurityMethods[5].Flags = 0;
  2643. pNegPol->pIpsecSecurityMethods[5].PfsQMRequired = FALSE;
  2644. pNegPol->pIpsecSecurityMethods[5].Count = 1;
  2645. pNegPol->pIpsecSecurityMethods[5].Algos[0].algoIdentifier = AUTH_ALGO_MD5;
  2646. pNegPol->pIpsecSecurityMethods[5].Algos[0].secondaryAlgoIdentifier = AUTH_ALGO_NONE;
  2647. pNegPol->pIpsecSecurityMethods[5].Algos[0].algoKeylen = 0;
  2648. pNegPol->pIpsecSecurityMethods[5].Algos[0].algoRounds = 0;
  2649. pNegPol->pIpsecSecurityMethods[5].Algos[0].operation = Auth;
  2650. // not necessary to change to bounded printf
  2651. _stprintf(pFAName, _TEXT(""));
  2652. pNegPol->pszIpsecName = IPSecAllocPolStr(pFAName);
  2653. if(pNegPol->pszIpsecName==NULL)
  2654. {
  2655. dwReturn = ERROR_OUTOFMEMORY;
  2656. BAIL_OUT;
  2657. }
  2658. pNegPol->pszDescription = NULL;
  2659. error:
  2660. if(dwReturn == ERROR_OUTOFMEMORY || dwReturn==ERROR_INVALID_PARAMETER)
  2661. {
  2662. if(pNegPol)
  2663. {
  2664. if(pNegPol->pIpsecSecurityMethods)
  2665. {
  2666. IPSecFreePolMem(pNegPol->pIpsecSecurityMethods);
  2667. }
  2668. IPSecFreePolMem(pNegPol);
  2669. pNegPol = NULL;
  2670. }
  2671. }
  2672. return pNegPol;
  2673. }
  2674. ////////////////////////////////////////////////////////////
  2675. //
  2676. //Function: MakeNegotiationPolicy()
  2677. //
  2678. //Date of Creation: 21st Aug 2001
  2679. //
  2680. //Parameters:
  2681. // OUT PIPSEC_NEGPOL_DATA *ppNegPol,
  2682. // IN PFILTERACTION pFilterAction
  2683. //
  2684. //Return: DWORD
  2685. //
  2686. //Description:
  2687. // This function fills the Negpol structure based of the user input
  2688. //
  2689. //Revision History:
  2690. //
  2691. // Date Author Comments
  2692. //
  2693. ////////////////////////////////////////////////////////////
  2694. DWORD
  2695. MakeNegotiationPolicy(
  2696. OUT PIPSEC_NEGPOL_DATA *ppNegPol,
  2697. IN PFILTERACTION pFilterAction
  2698. )
  2699. {
  2700. RPC_STATUS RpcStat=RPC_S_OK;
  2701. DWORD i=0,dwReturn = ERROR_SUCCESS;
  2702. PIPSEC_NEGPOL_DATA pNegPol = (PIPSEC_NEGPOL_DATA) IPSecAllocPolMem(sizeof(IPSEC_NEGPOL_DATA));
  2703. if(pNegPol == NULL)
  2704. {
  2705. dwReturn = ERROR_OUTOFMEMORY;
  2706. BAIL_OUT;
  2707. }
  2708. memset(pNegPol,0,sizeof(IPSEC_NEGPOL_DATA));
  2709. RpcStat = UuidCreate(&(pNegPol->NegPolIdentifier));
  2710. if (!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  2711. {
  2712. dwReturn=ERROR_INVALID_PARAMETER;
  2713. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_STATIC_INTERNAL_ERROR);
  2714. BAIL_OUT;
  2715. }
  2716. // start with action=negotiate
  2717. pNegPol->NegPolAction = GUID_NEGOTIATION_ACTION_NORMAL_IPSEC;
  2718. pNegPol->NegPolType = GUID_NEGOTIATION_TYPE_STANDARD;
  2719. pNegPol->dwSecurityMethodCount = pFilterAction->dwNumSecMethodCount;
  2720. //if soft , increment the count
  2721. if(pFilterAction->bSoft)
  2722. {
  2723. pNegPol->dwSecurityMethodCount++;
  2724. }
  2725. if(pNegPol->dwSecurityMethodCount)
  2726. {
  2727. pNegPol->pIpsecSecurityMethods = (IPSEC_SECURITY_METHOD *) IPSecAllocPolMem(pNegPol->dwSecurityMethodCount * sizeof(IPSEC_SECURITY_METHOD));
  2728. if(pNegPol->pIpsecSecurityMethods==NULL)
  2729. {
  2730. dwReturn = ERROR_OUTOFMEMORY;
  2731. BAIL_OUT;
  2732. }
  2733. }
  2734. // handle sec.methods
  2735. for (i = 0; i < pFilterAction->dwNumSecMethodCount; i++)
  2736. {
  2737. DWORD j;
  2738. pNegPol->pIpsecSecurityMethods[i].Lifetime.KeyExpirationBytes = pFilterAction->pIpsecSecMethods[i].Lifetime.uKeyExpirationKBytes;
  2739. pNegPol->pIpsecSecurityMethods[i].Lifetime.KeyExpirationTime = pFilterAction->pIpsecSecMethods[i].Lifetime.uKeyExpirationTime;
  2740. pNegPol->pIpsecSecurityMethods[i].Flags = 0;
  2741. pNegPol->pIpsecSecurityMethods[i].PfsQMRequired = pFilterAction->bQMPfs;
  2742. pNegPol->pIpsecSecurityMethods[i].Count = pFilterAction->pIpsecSecMethods[i].dwNumAlgos;
  2743. for (j = 0; j < pNegPol->pIpsecSecurityMethods[i].Count && j < QM_MAX_ALGOS; j++)
  2744. {
  2745. pNegPol->pIpsecSecurityMethods[i].Algos[j].algoIdentifier = pFilterAction->pIpsecSecMethods[i].Algos[j].uAlgoIdentifier;
  2746. pNegPol->pIpsecSecurityMethods[i].Algos[j].secondaryAlgoIdentifier = pFilterAction->pIpsecSecMethods[i].Algos[j].uSecAlgoIdentifier;
  2747. pNegPol->pIpsecSecurityMethods[i].Algos[j].algoKeylen = pFilterAction->pIpsecSecMethods[i].Algos[j].uAlgoKeyLen;
  2748. pNegPol->pIpsecSecurityMethods[i].Algos[j].algoRounds = pFilterAction->pIpsecSecMethods[i].Algos[j].uAlgoRounds;
  2749. switch (pFilterAction->pIpsecSecMethods[i].Algos[j].Operation)
  2750. {
  2751. case AUTHENTICATION:
  2752. pNegPol->pIpsecSecurityMethods[i].Algos[j].operation = Auth;
  2753. break;
  2754. case ENCRYPTION:
  2755. pNegPol->pIpsecSecurityMethods[i].Algos[j].operation = Encrypt;
  2756. break;
  2757. default:
  2758. pNegPol->pIpsecSecurityMethods[i].Algos[j].operation = None;
  2759. }
  2760. }
  2761. }
  2762. // add soft
  2763. if (pFilterAction->bSoft)
  2764. {
  2765. memset(&(pNegPol->pIpsecSecurityMethods[pNegPol->dwSecurityMethodCount - 1]), 0, sizeof(IPSEC_SECURITY_METHOD));
  2766. }
  2767. if(pFilterAction->pszFAName)
  2768. {
  2769. pNegPol->pszIpsecName = IPSecAllocPolStr(pFilterAction->pszFAName);
  2770. if(pNegPol->pszIpsecName==NULL)
  2771. {
  2772. dwReturn = ERROR_OUTOFMEMORY;
  2773. BAIL_OUT;
  2774. }
  2775. }
  2776. if(pFilterAction->pszFADescription)
  2777. {
  2778. pNegPol->pszDescription = IPSecAllocPolStr(pFilterAction->pszFADescription);
  2779. if(pNegPol->pszDescription==NULL)
  2780. {
  2781. dwReturn = ERROR_OUTOFMEMORY;
  2782. BAIL_OUT;
  2783. }
  2784. }
  2785. // fill the relevent guid for action, got from the user
  2786. pNegPol->NegPolAction=pFilterAction->NegPolAction;
  2787. error: //clean up
  2788. if(dwReturn == ERROR_OUTOFMEMORY)
  2789. {
  2790. if(pNegPol)
  2791. {
  2792. if(pNegPol->pIpsecSecurityMethods)
  2793. {
  2794. IPSecFreePolMem(pNegPol->pIpsecSecurityMethods);
  2795. }
  2796. IPSecFreePolMem(pNegPol);
  2797. pNegPol=NULL;
  2798. }
  2799. }
  2800. *ppNegPol= pNegPol;
  2801. return dwReturn;
  2802. }
  2803. ////////////////////////////////////////////////////////////
  2804. //
  2805. //Function: GetNegPolFromStore()
  2806. //
  2807. //Date of Creation: 21st Aug 2001
  2808. //
  2809. //Parameters:
  2810. // OUT PIPSEC_NEGPOL_DATA *ppNegPol,
  2811. // IN LPTSTR pszFAName,
  2812. // IN HANDLE hPolicyStorage
  2813. //
  2814. //Return: BOOL
  2815. //
  2816. //Description:
  2817. // This function retrives the user specified NegPol data from the policy store
  2818. //
  2819. //Revision History:
  2820. //
  2821. // Date Author Comments
  2822. //
  2823. ////////////////////////////////////////////////////////////
  2824. BOOL
  2825. GetNegPolFromStore(
  2826. OUT PIPSEC_NEGPOL_DATA *ppNegPol,
  2827. IN LPTSTR pszFAName,
  2828. IN HANDLE hPolicyStorage
  2829. )
  2830. {
  2831. PIPSEC_NEGPOL_DATA *ppNegPolEnum = NULL,pNegPolData=NULL;
  2832. DWORD dwNumNegPol=0 ;
  2833. BOOL bNegPolExists=FALSE;
  2834. DWORD dwReturnCode=ERROR_SUCCESS, i=0;
  2835. //enum and get the filteraction from the store
  2836. dwReturnCode = IPSecEnumNegPolData(hPolicyStorage, &ppNegPolEnum, &dwNumNegPol);
  2837. if (dwReturnCode == ERROR_SUCCESS && dwNumNegPol> 0 && ppNegPolEnum != NULL)
  2838. {
  2839. for (i = 0; i < dwNumNegPol; i++)
  2840. {
  2841. if ((ppNegPolEnum[i]->pszIpsecName!=NULL)&&( _tcscmp(ppNegPolEnum[i]->pszIpsecName, pszFAName) == 0))
  2842. {
  2843. bNegPolExists=TRUE;
  2844. dwReturnCode = IPSecCopyNegPolData(ppNegPolEnum[i],&pNegPolData);
  2845. break;
  2846. }
  2847. }
  2848. if (dwNumNegPol > 0 && ppNegPolEnum != NULL)
  2849. {
  2850. IPSecFreeMulNegPolData(ppNegPolEnum, dwNumNegPol);
  2851. }
  2852. }
  2853. if(pNegPolData)
  2854. {
  2855. *ppNegPol=pNegPolData;
  2856. }
  2857. return bNegPolExists;
  2858. }
  2859. ////////////////////////////////////////////////////////////
  2860. //
  2861. //Function: GetFilterListFromStore()
  2862. //
  2863. //Date of Creation: 21st Aug 2001
  2864. //
  2865. //Parameters:
  2866. // OUT PIPSEC_FILTER_DATA *ppFilter,
  2867. // IN LPTSTR pszFLName,
  2868. // IN HANDLE hPolicyStorage,
  2869. // IN OUT BOOL &bFilterExists
  2870. //
  2871. //Return: BOOL
  2872. //
  2873. //Description:
  2874. // This function retrives the user specified Filter list data from the policy store
  2875. //
  2876. //Revision History:
  2877. //
  2878. // Date Author Comments
  2879. //
  2880. ////////////////////////////////////////////////////////////
  2881. BOOL
  2882. GetFilterListFromStore(
  2883. OUT PIPSEC_FILTER_DATA *ppFilter,
  2884. IN LPTSTR pszFLName,
  2885. IN HANDLE hPolicyStorage,
  2886. IN OUT BOOL &bFilterExists
  2887. )
  2888. {
  2889. PIPSEC_FILTER_DATA *ppFilterEnum = NULL,pFilterData=NULL;
  2890. DWORD dwNumFilter=0 , i=0 ;
  2891. BOOL bFilterListExists=FALSE;
  2892. DWORD dwReturnCode=ERROR_SUCCESS;
  2893. //enum and get the filterlist from the store
  2894. dwReturnCode = IPSecEnumFilterData(hPolicyStorage, &ppFilterEnum, &dwNumFilter);
  2895. if (dwReturnCode == ERROR_SUCCESS && dwNumFilter> 0 && ppFilterEnum != NULL)
  2896. {
  2897. for (i = 0; i < dwNumFilter; i++)
  2898. {
  2899. if ((ppFilterEnum[i]->pszIpsecName!=NULL)&&( _tcscmp(ppFilterEnum[i]->pszIpsecName, pszFLName) == 0))
  2900. {
  2901. bFilterListExists=TRUE;
  2902. dwReturnCode = IPSecCopyFilterData(ppFilterEnum[i],&pFilterData);
  2903. if(ppFilterEnum[i]->dwNumFilterSpecs > 0)
  2904. {
  2905. bFilterExists=TRUE;
  2906. }
  2907. break;
  2908. }
  2909. }
  2910. if (dwNumFilter > 0 && ppFilterEnum != NULL)
  2911. {
  2912. IPSecFreeMulFilterData(ppFilterEnum, dwNumFilter);
  2913. }
  2914. }
  2915. if(pFilterData)
  2916. {
  2917. *ppFilter=pFilterData;
  2918. }
  2919. return bFilterListExists;
  2920. }
  2921. ////////////////////////////////////////////////////////////
  2922. //
  2923. //Function: GetPolicyFromStore()
  2924. //
  2925. //Date of Creation: 21st Aug 2001
  2926. //
  2927. //Parameters:
  2928. // OUT PIPSEC_POLICY_DATA *ppPolicy,
  2929. // IN LPTSTR szPolicyName,
  2930. // IN HANDLE hPolicyStorage
  2931. //
  2932. //Return: BOOL
  2933. //
  2934. //Description:
  2935. // This function retrives the user specified Policy data from the policy store
  2936. //
  2937. //Revision History:
  2938. //
  2939. // Date Author Comments
  2940. //
  2941. ////////////////////////////////////////////////////////////
  2942. BOOL
  2943. GetPolicyFromStore(
  2944. OUT PIPSEC_POLICY_DATA *ppPolicy,
  2945. IN LPTSTR szPolicyName,
  2946. IN HANDLE hPolicyStorage
  2947. )
  2948. {
  2949. PIPSEC_POLICY_DATA *ppPolicyEnum = NULL,pPolicy=NULL;
  2950. BOOL bPolicyExists=FALSE;
  2951. DWORD dwNumPolicies = 0,i=0,j=0;
  2952. DWORD dwReturnCode=ERROR_SUCCESS;
  2953. RPC_STATUS RpcStat =RPC_S_OK;
  2954. //enum and get the policy from the store
  2955. dwReturnCode = IPSecEnumPolicyData(hPolicyStorage, &ppPolicyEnum, &dwNumPolicies);
  2956. if (!(dwReturnCode == ERROR_SUCCESS && dwNumPolicies > 0 && ppPolicyEnum != NULL))
  2957. {
  2958. BAIL_OUT;
  2959. }
  2960. for (i = 0; i < dwNumPolicies; i++)
  2961. {
  2962. if (szPolicyName && _tcscmp(ppPolicyEnum[i]->pszIpsecName, szPolicyName) == 0)
  2963. {
  2964. bPolicyExists=TRUE;
  2965. dwReturnCode = IPSecCopyPolicyData(ppPolicyEnum[i], &pPolicy);
  2966. if (dwReturnCode == ERROR_SUCCESS)
  2967. {
  2968. dwReturnCode = IPSecEnumNFAData(hPolicyStorage, pPolicy->PolicyIdentifier
  2969. , &(pPolicy->ppIpsecNFAData), &(pPolicy->dwNumNFACount));
  2970. }
  2971. if (dwReturnCode == ERROR_SUCCESS)
  2972. {
  2973. dwReturnCode = IPSecGetISAKMPData(hPolicyStorage, pPolicy->ISAKMPIdentifier, &(pPolicy->pIpsecISAKMPData));
  2974. if(dwReturnCode==ERROR_SUCCESS )
  2975. {
  2976. for (j = 0; j < pPolicy->dwNumNFACount; j++)
  2977. {
  2978. if (!UuidIsNil(&(pPolicy->ppIpsecNFAData[j]->NegPolIdentifier), &RpcStat))
  2979. {
  2980. IPSecGetNegPolData(hPolicyStorage,
  2981. pPolicy->ppIpsecNFAData[j]->NegPolIdentifier,
  2982. &(pPolicy->ppIpsecNFAData[j]->pIpsecNegPolData));
  2983. }
  2984. }
  2985. }
  2986. }
  2987. }
  2988. }
  2989. // clean it up
  2990. if (dwNumPolicies > 0 && ppPolicyEnum != NULL)
  2991. {
  2992. IPSecFreeMulPolicyData(ppPolicyEnum, dwNumPolicies);
  2993. }
  2994. if(pPolicy)
  2995. {
  2996. *ppPolicy=pPolicy;
  2997. }
  2998. error:
  2999. return bPolicyExists;
  3000. }
  3001. ////////////////////////////////////////////////////////////
  3002. //
  3003. //Function: MakeRule()
  3004. //
  3005. //Date of Creation: 21st Aug 2001
  3006. //
  3007. //Parameters:
  3008. // IN PRULEDATA pRuleData,
  3009. // IN PIPSEC_NEGPOL_DATA pNegPolData,
  3010. // IN PIPSEC_FILTER_DATA pFilterData,
  3011. // IN OUT BOOL &bCertConversionSuceeded
  3012. //
  3013. //Return: PIPSEC_NFA_DATA
  3014. //
  3015. //Description:
  3016. // This function forms the NFA data structure basedon the user input.
  3017. //
  3018. //Revision History:
  3019. //
  3020. // Date Author Comments
  3021. //
  3022. ////////////////////////////////////////////////////////////
  3023. PIPSEC_NFA_DATA
  3024. MakeRule(
  3025. IN PRULEDATA pRuleData,
  3026. IN PIPSEC_NEGPOL_DATA pNegPolData,
  3027. IN PIPSEC_FILTER_DATA pFilterData,
  3028. IN OUT BOOL &bCertConversionSuceeded
  3029. )
  3030. {
  3031. RPC_STATUS RpcStat =RPC_S_OK;
  3032. DWORD dwReturn = ERROR_SUCCESS;
  3033. PIPSEC_NFA_DATA pRule = (PIPSEC_NFA_DATA) IPSecAllocPolMem(sizeof(IPSEC_NFA_DATA));
  3034. if(pRule==NULL)
  3035. {
  3036. dwReturn = ERROR_OUTOFMEMORY;
  3037. BAIL_OUT;
  3038. }
  3039. memset(pRule,0,sizeof(IPSEC_NFA_DATA));
  3040. pRule->pszIpsecName = pRule->pszDescription = pRule->pszInterfaceName = pRule->pszEndPointName = NULL;
  3041. //rule name
  3042. if (pRuleData->pszRuleName)
  3043. {
  3044. pRule->pszIpsecName = IPSecAllocPolStr(pRuleData->pszRuleName);
  3045. if(pRule->pszIpsecName==NULL)
  3046. {
  3047. dwReturn = ERROR_OUTOFMEMORY;
  3048. BAIL_OUT;
  3049. }
  3050. }
  3051. //rule desc
  3052. if (pRuleData->pszRuleDescription)
  3053. {
  3054. pRule->pszDescription = IPSecAllocPolStr(pRuleData->pszRuleDescription);
  3055. if(pRule->pszDescription==NULL)
  3056. {
  3057. dwReturn = ERROR_OUTOFMEMORY;
  3058. BAIL_OUT;
  3059. }
  3060. }
  3061. RpcStat = UuidCreate(&(pRule->NFAIdentifier));
  3062. if (!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  3063. {
  3064. dwReturn=ERROR_INVALID_PARAMETER;
  3065. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_STATIC_INTERNAL_ERROR);
  3066. BAIL_OUT;
  3067. }
  3068. pRule->dwWhenChanged = 0;
  3069. //filterlist
  3070. pRule->pIpsecFilterData = NULL;
  3071. pRule->FilterIdentifier = pFilterData->FilterIdentifier;
  3072. //filteraction
  3073. pRule->pIpsecNegPolData = NULL;
  3074. pRule->NegPolIdentifier = pNegPolData->NegPolIdentifier;
  3075. // tunnel address
  3076. pRule->dwTunnelFlags = 0;
  3077. pRule->dwTunnelIpAddr= 0;
  3078. if (pRuleData->bTunnel)
  3079. {
  3080. pRule->dwTunnelFlags = 1;
  3081. pRule->dwTunnelIpAddr = pRuleData->TunnelIPAddress;
  3082. }
  3083. // interface type
  3084. if (pRuleData->ConnectionType == INTERFACE_TYPE_ALL)
  3085. {
  3086. pRule->dwInterfaceType = (DWORD)PAS_INTERFACE_TYPE_ALL;
  3087. }
  3088. else if (pRuleData->ConnectionType == INTERFACE_TYPE_LAN)
  3089. {
  3090. pRule->dwInterfaceType = (DWORD)PAS_INTERFACE_TYPE_LAN;
  3091. }
  3092. else if (pRuleData->ConnectionType == INTERFACE_TYPE_DIALUP)
  3093. {
  3094. pRule->dwInterfaceType = (DWORD)PAS_INTERFACE_TYPE_DIALUP;
  3095. }
  3096. else
  3097. {
  3098. pRule->dwInterfaceType = (DWORD)PAS_INTERFACE_TYPE_NONE;
  3099. }
  3100. // active flag
  3101. pRule->dwActiveFlag = pRuleData->bActivate;
  3102. // auth methods
  3103. pRule->dwAuthMethodCount = pRuleData->AuthInfos.dwNumAuthInfos;
  3104. pRule->ppAuthMethods = (PIPSEC_AUTH_METHOD *) IPSecAllocPolMem(pRule->dwAuthMethodCount * sizeof(PIPSEC_AUTH_METHOD));
  3105. if(pRule->ppAuthMethods==NULL)
  3106. {
  3107. dwReturn = ERROR_OUTOFMEMORY;
  3108. BAIL_OUT;
  3109. }
  3110. dwReturn = LoadAuthenticationInfos(pRuleData->AuthInfos, pRule,bCertConversionSuceeded);
  3111. error:
  3112. if(dwReturn == ERROR_OUTOFMEMORY || dwReturn == ERROR_INVALID_PARAMETER)
  3113. {
  3114. if(pRule)
  3115. {
  3116. CleanUpAuthInfo(pRule); //this function frees only auth info.
  3117. IPSecFreePolMem(pRule); //since the above fn is used in other fns also, this free is required to cleanup other rule memory
  3118. pRule = NULL;
  3119. }
  3120. }
  3121. return pRule;
  3122. }
  3123. ////////////////////////////////////////////////////////////
  3124. //
  3125. //Function: DecodeCertificateName()
  3126. //
  3127. //Date of Creation: 21st Aug 2001
  3128. //
  3129. //Parameters:
  3130. // IN LPBYTE EncodedName,
  3131. // IN DWORD EncodedNameLength,
  3132. // IN OUT LPTSTR *ppszSubjectName
  3133. //
  3134. //Return: DWORD
  3135. //
  3136. //Description:
  3137. // This function decodes the certificate name based on the user input.
  3138. //
  3139. //Revision History:
  3140. //
  3141. // Date Author Comments
  3142. //
  3143. ////////////////////////////////////////////////////////////
  3144. DWORD
  3145. DecodeCertificateName (
  3146. IN LPBYTE EncodedName,
  3147. IN DWORD EncodedNameLength,
  3148. IN OUT LPTSTR *ppszSubjectName
  3149. )
  3150. {
  3151. DWORD DecodedNameLength=0, dwReturn=ERROR_SUCCESS;
  3152. CERT_NAME_BLOB CertName;
  3153. CertName.cbData = EncodedNameLength;
  3154. CertName.pbData = EncodedName;
  3155. //this API call is to get the size
  3156. DecodedNameLength = CertNameToStr(
  3157. X509_ASN_ENCODING,
  3158. &CertName,
  3159. CERT_X500_NAME_STR,
  3160. NULL,
  3161. NULL);
  3162. if (!DecodedNameLength)
  3163. {
  3164. dwReturn = ERROR_INVALID_PARAMETER;
  3165. BAIL_OUT;
  3166. }
  3167. (*ppszSubjectName)= new _TCHAR[DecodedNameLength];
  3168. if (*ppszSubjectName == NULL)
  3169. {
  3170. dwReturn = ERROR_OUTOFMEMORY;
  3171. BAIL_OUT;
  3172. }
  3173. //this API call is for conversion
  3174. DecodedNameLength = CertNameToStr(
  3175. X509_ASN_ENCODING,
  3176. &CertName,
  3177. CERT_X500_NAME_STR,
  3178. *ppszSubjectName,
  3179. DecodedNameLength);
  3180. if (!DecodedNameLength)
  3181. {
  3182. delete (*ppszSubjectName);
  3183. (*ppszSubjectName) = 0;
  3184. dwReturn = ERROR_INVALID_PARAMETER;
  3185. }
  3186. error:
  3187. return dwReturn;
  3188. }
  3189. ////////////////////////////////////////////////////////////
  3190. //
  3191. //Function: AddRule()
  3192. //
  3193. //Date of Creation: 21st Aug 2001
  3194. //
  3195. //Parameters:
  3196. // IN OUT PIPSEC_POLICY_DATA pPolicy,
  3197. // IN PRULEDATA pRuleData,
  3198. // IN PIPSEC_NEGPOL_DATA pNegPolData,
  3199. // IN PIPSEC_FILTER_DATA pFilterData ,
  3200. // IN HANDLE hPolicyStorage
  3201. //
  3202. //Return: DWORD
  3203. //
  3204. //Description:
  3205. // This function adds the already formed NFA structure to the policy specified.
  3206. //
  3207. //Revision History:
  3208. //
  3209. // Date Author Comments
  3210. //
  3211. ////////////////////////////////////////////////////////////
  3212. DWORD
  3213. AddRule(
  3214. IN OUT PIPSEC_POLICY_DATA pPolicy,
  3215. IN PRULEDATA pRuleData,
  3216. IN PIPSEC_NEGPOL_DATA pNegPolData,
  3217. IN PIPSEC_FILTER_DATA pFilterData ,
  3218. IN HANDLE hPolicyStorage
  3219. )
  3220. {
  3221. DWORD dwReturnCode = ERROR_SUCCESS;
  3222. BOOL bCertConversionSuceeded=TRUE;
  3223. PIPSEC_NFA_DATA pRule = MakeRule(pRuleData, pNegPolData, pFilterData,bCertConversionSuceeded);
  3224. if(pRule ==NULL)
  3225. {
  3226. dwReturnCode = ERROR_OUTOFMEMORY;
  3227. BAIL_OUT;
  3228. }
  3229. if(!bCertConversionSuceeded)
  3230. {
  3231. dwReturnCode = ERROR_INVALID_DATA;
  3232. BAIL_OUT;
  3233. }
  3234. // form rule data structures
  3235. pPolicy->dwNumNFACount++;
  3236. pPolicy->ppIpsecNFAData = (PIPSEC_NFA_DATA *) ReAllocRuleMem(pPolicy->ppIpsecNFAData
  3237. , (pPolicy->dwNumNFACount-1), pPolicy->dwNumNFACount);
  3238. if(pPolicy->ppIpsecNFAData ==NULL)
  3239. {
  3240. dwReturnCode = ERROR_OUTOFMEMORY;
  3241. BAIL_OUT;
  3242. }
  3243. pPolicy->ppIpsecNFAData[pPolicy->dwNumNFACount-1] = pRule;
  3244. // Create Rule
  3245. dwReturnCode=IPSecCreateNFAData(hPolicyStorage, pPolicy->PolicyIdentifier, pRule);
  3246. if(dwReturnCode==ERROR_SUCCESS)
  3247. {
  3248. dwReturnCode=SetPolicyData(hPolicyStorage, pPolicy);
  3249. }
  3250. error:
  3251. if(dwReturnCode == ERROR_OUTOFMEMORY)
  3252. {
  3253. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  3254. dwReturnCode = ERROR_SUCCESS;
  3255. }
  3256. return dwReturnCode;
  3257. }
  3258. ////////////////////////////////////////////////////////////
  3259. //
  3260. //Function: LoadIkeDefaults()
  3261. //
  3262. //Date of Creation: 21st Aug 2001
  3263. //
  3264. //Parameters:
  3265. // IN PPOLICYDATA pPolicy,
  3266. // OUT IPSEC_MM_OFFER **ppIpSecMMOffer
  3267. //
  3268. //Return: DWORD
  3269. //
  3270. //Description:
  3271. // This function loads the IKE default values
  3272. //
  3273. //Revision History:
  3274. //
  3275. // Date Author Comments
  3276. //
  3277. ////////////////////////////////////////////////////////////
  3278. DWORD
  3279. LoadIkeDefaults(
  3280. IN PPOLICYDATA pPolicy,
  3281. OUT IPSEC_MM_OFFER **ppIpSecMMOffer
  3282. )
  3283. {
  3284. DWORD dwReturn = ERROR_SUCCESS;
  3285. pPolicy->dwOfferCount = 3;
  3286. IPSEC_MM_OFFER *pOffers = new IPSEC_MM_OFFER[pPolicy->dwOfferCount];
  3287. if(pOffers == NULL)
  3288. {
  3289. dwReturn = ERROR_OUTOFMEMORY;
  3290. BAIL_OUT;
  3291. }
  3292. memset(pOffers, 0, sizeof(IPSEC_MM_OFFER) * (pPolicy->dwOfferCount));
  3293. // init these
  3294. for (DWORD i = 0; i < pPolicy->dwOfferCount; ++i)
  3295. {
  3296. pOffers[i].dwQuickModeLimit = pPolicy->dwQMLimit;
  3297. pOffers[i].Lifetime.uKeyExpirationKBytes = 0;
  3298. pOffers[i].Lifetime.uKeyExpirationTime = pPolicy->LifeTimeInSeconds;
  3299. }
  3300. // 3DES-SHA1-2
  3301. pOffers[0].EncryptionAlgorithm.uAlgoIdentifier = CONF_ALGO_3_DES;
  3302. pOffers[0].EncryptionAlgorithm.uAlgoKeyLen = POTF_OAKLEY_ALGOKEYLEN;
  3303. pOffers[0].EncryptionAlgorithm.uAlgoRounds = POTF_OAKLEY_ALGOROUNDS;
  3304. pOffers[0].HashingAlgorithm.uAlgoIdentifier = AUTH_ALGO_SHA1;
  3305. pOffers[0].HashingAlgorithm.uAlgoKeyLen = POTF_OAKLEY_ALGOKEYLEN;
  3306. pOffers[0].dwDHGroup = POTF_OAKLEY_GROUP2;
  3307. // 3DES-MD5-2
  3308. pOffers[1].EncryptionAlgorithm.uAlgoIdentifier = CONF_ALGO_3_DES;
  3309. pOffers[1].EncryptionAlgorithm.uAlgoKeyLen = POTF_OAKLEY_ALGOKEYLEN;
  3310. pOffers[1].EncryptionAlgorithm.uAlgoRounds = POTF_OAKLEY_ALGOROUNDS;
  3311. pOffers[1].HashingAlgorithm.uAlgoIdentifier = AUTH_ALGO_MD5;
  3312. pOffers[1].HashingAlgorithm.uAlgoKeyLen = POTF_OAKLEY_ALGOKEYLEN;
  3313. pOffers[1].dwDHGroup = POTF_OAKLEY_GROUP2;
  3314. // 3DES-SHA1-3
  3315. pOffers[2].EncryptionAlgorithm.uAlgoIdentifier = CONF_ALGO_3_DES;
  3316. pOffers[2].EncryptionAlgorithm.uAlgoKeyLen = POTF_OAKLEY_ALGOKEYLEN;
  3317. pOffers[2].EncryptionAlgorithm.uAlgoRounds = POTF_OAKLEY_ALGOROUNDS;
  3318. pOffers[2].HashingAlgorithm.uAlgoIdentifier = AUTH_ALGO_SHA1;
  3319. pOffers[2].HashingAlgorithm.uAlgoKeyLen = POTF_OAKLEY_ALGOKEYLEN;
  3320. pOffers[2].dwDHGroup = POTF_OAKLEY_GROUP2048;
  3321. *ppIpSecMMOffer=pOffers;
  3322. error:
  3323. return dwReturn;
  3324. }
  3325. ////////////////////////////////////////////////////////////
  3326. //
  3327. //Function: LoadOfferDefaults()
  3328. //
  3329. //Date of Creation: 21st Aug 2001
  3330. //
  3331. //Parameters:
  3332. // OUT PIPSEC_QM_OFFER & pOffers,
  3333. // OUT DWORD & dwNumOffers
  3334. //
  3335. //Return: DWORD
  3336. //
  3337. //Description:
  3338. // This function fills the Offers structure with default values
  3339. //
  3340. //Revision History:
  3341. //
  3342. // Date Author Comments
  3343. //
  3344. ////////////////////////////////////////////////////////////
  3345. DWORD
  3346. LoadOfferDefaults(
  3347. OUT PIPSEC_QM_OFFER & pOffers,
  3348. OUT DWORD & dwNumOffers
  3349. )
  3350. {
  3351. DWORD dwReturn = ERROR_SUCCESS;
  3352. dwNumOffers = 2;
  3353. DWORD i=0;
  3354. pOffers = new IPSEC_QM_OFFER[dwNumOffers];
  3355. if(pOffers == NULL)
  3356. {
  3357. dwReturn = ERROR_OUTOFMEMORY;
  3358. BAIL_OUT;
  3359. }
  3360. memset(pOffers, 0, sizeof(IPSEC_QM_OFFER) * dwNumOffers);
  3361. for (i = 0; i < dwNumOffers; ++i)
  3362. {
  3363. pOffers[i].Lifetime.uKeyExpirationKBytes = POTF_DEFAULT_P2REKEY_BYTES;
  3364. pOffers[i].Lifetime.uKeyExpirationTime = POTF_DEFAULT_P2REKEY_TIME;
  3365. pOffers[i].bPFSRequired = FALSE;
  3366. pOffers[i].dwPFSGroup = 0;
  3367. pOffers[i].dwFlags = 0;
  3368. pOffers[i].dwNumAlgos = 1;
  3369. pOffers[i].Algos[0].Operation = ENCRYPTION;
  3370. }
  3371. //esp[3des,sha1]
  3372. pOffers[0].Algos[0].uAlgoIdentifier = CONF_ALGO_3_DES;
  3373. pOffers[0].Algos[0].uSecAlgoIdentifier = HMAC_AUTH_ALGO_SHA1;
  3374. //esp[3des,md5]
  3375. pOffers[1].Algos[0].uAlgoIdentifier = CONF_ALGO_3_DES;
  3376. pOffers[1].Algos[0].uSecAlgoIdentifier = HMAC_AUTH_ALGO_MD5;
  3377. error:
  3378. return dwReturn;
  3379. }
  3380. ////////////////////////////////////////////////////////////
  3381. //
  3382. //Function: CheckPolicyExistance()
  3383. //
  3384. //Date of Creation: 21st Aug 2001
  3385. //
  3386. //Parameters:
  3387. // IN HANDLE hPolicyStorage,
  3388. // IN LPTSTR pszPolicyName
  3389. //
  3390. //Return: BOOL
  3391. //
  3392. //Description:
  3393. // This function checks whether the specified policy exists in Policy store.
  3394. //
  3395. //Revision History:
  3396. //
  3397. // Date Author Comments
  3398. //
  3399. ////////////////////////////////////////////////////////////
  3400. BOOL
  3401. CheckPolicyExistance(
  3402. IN HANDLE hPolicyStorage,
  3403. IN LPTSTR pszPolicyName
  3404. )
  3405. {
  3406. PIPSEC_POLICY_DATA *ppPolicyEnum = NULL;
  3407. BOOL bPolicyExists=FALSE;
  3408. DWORD dwNumPolicies=0;
  3409. DWORD dwReturnCode=ERROR_SUCCESS;
  3410. // check whether the policy already exists in store
  3411. dwReturnCode = IPSecEnumPolicyData(hPolicyStorage, &ppPolicyEnum, &dwNumPolicies);
  3412. if (dwReturnCode == ERROR_SUCCESS && dwNumPolicies > 0 && ppPolicyEnum != NULL)
  3413. {
  3414. for (DWORD i = 0; i < dwNumPolicies; i++)
  3415. if (_tcscmp(ppPolicyEnum[i]->pszIpsecName, pszPolicyName) == 0)
  3416. {
  3417. bPolicyExists=TRUE;
  3418. break;
  3419. }
  3420. if (dwNumPolicies > 0 && ppPolicyEnum != NULL)
  3421. {
  3422. IPSecFreeMulPolicyData(ppPolicyEnum, dwNumPolicies);
  3423. }
  3424. }
  3425. return bPolicyExists;
  3426. }
  3427. ////////////////////////////////////////////////////////////
  3428. //
  3429. //Function: ReAllocRuleMem()
  3430. //
  3431. //Date of Creation: 21st Aug 2001
  3432. //
  3433. //Parameters:
  3434. // IN PIPSEC_NFA_DATA *ppOldMem,
  3435. // IN DWORD cbOld,
  3436. // IN DWORD cbNew
  3437. //
  3438. //Return: PIPSEC_NFA_DATA*
  3439. //
  3440. //Description:
  3441. // This function reallocates the memory for the NFA structure.
  3442. //
  3443. //Revision History:
  3444. //
  3445. // Date Author Comments
  3446. //
  3447. ////////////////////////////////////////////////////////////
  3448. PIPSEC_NFA_DATA*
  3449. ReAllocRuleMem(
  3450. IN PIPSEC_NFA_DATA *ppOldMem,
  3451. IN DWORD cbOld,
  3452. IN DWORD cbNew
  3453. )
  3454. {
  3455. PIPSEC_NFA_DATA * ppNewMem=NULL;
  3456. DWORD j=0;
  3457. //reallocate the NFA mem
  3458. ppNewMem= (PIPSEC_NFA_DATA *) IPSecAllocPolMem(cbNew * sizeof(PIPSEC_NFA_DATA));
  3459. if (ppNewMem)
  3460. {
  3461. for(DWORD i=0;i<min(cbNew, cbOld);i++)
  3462. {
  3463. while(!ppOldMem[j])
  3464. j++;
  3465. ppNewMem[i]=ppOldMem[j];
  3466. j++;
  3467. }
  3468. IPSecFreePolMem(ppOldMem);
  3469. }
  3470. return ppNewMem;
  3471. }
  3472. ////////////////////////////////////////////////////////////
  3473. //
  3474. //Function: ReAllocFilterSpecMem()
  3475. //
  3476. //Date of Creation: 21st Aug 2001
  3477. //
  3478. //Parameters:
  3479. // IN PIPSEC_FILTER_SPEC * ppOldMem,
  3480. // IN DWORD cbOld,
  3481. // IN DWORD cbNew
  3482. //
  3483. //Return: PIPSEC_FILTER_SPEC *
  3484. //
  3485. //Description:
  3486. // This function reallocates the Filter Spec memory
  3487. //
  3488. //Revision History:
  3489. //
  3490. //<Version number, Change request number, Date of modification,
  3491. // Author, Nature of change>
  3492. ////////////////////////////////////////////////////////////
  3493. PIPSEC_FILTER_SPEC *
  3494. ReAllocFilterSpecMem(
  3495. IN PIPSEC_FILTER_SPEC * ppOldMem,
  3496. IN DWORD cbOld,
  3497. IN DWORD cbNew
  3498. )
  3499. {
  3500. PIPSEC_FILTER_SPEC * ppNewMem=NULL;
  3501. DWORD j=0;
  3502. ppNewMem= (PIPSEC_FILTER_SPEC *) IPSecAllocPolMem(cbNew * sizeof(PIPSEC_FILTER_SPEC));
  3503. // reallocate the filterspec memory
  3504. if(ppNewMem)
  3505. {
  3506. for(DWORD i=0;i<min(cbNew, cbOld);i++)
  3507. {
  3508. while(!ppOldMem[j])
  3509. j++;
  3510. ppNewMem[i]=ppOldMem[j];
  3511. j++;
  3512. }
  3513. IPSecFreePolMem(ppOldMem);
  3514. }
  3515. return ppNewMem;
  3516. }
  3517. ////////////////////////////////////////////////////////////
  3518. //
  3519. //Function: ValidateFilterSpec()
  3520. //
  3521. //Date of Creation: 21st Aug 2001
  3522. //
  3523. //Parameters:
  3524. // IN PPOLICYDATA pPolicyData
  3525. //
  3526. //Return: DWORD
  3527. //
  3528. //Description:
  3529. // This function validates the Filter Spec details
  3530. //
  3531. //Revision History:
  3532. //
  3533. // Date Author Comments
  3534. //
  3535. ////////////////////////////////////////////////////////////
  3536. DWORD
  3537. ValidateFilterSpec(
  3538. IN PFILTERDATA pFilterData
  3539. )
  3540. {
  3541. DWORD dwReturn=ERROR_SUCCESS;
  3542. ADDR SrcAddr,DstAddr;
  3543. SrcAddr.uIpAddr=htonl(pFilterData->SourceAddr.puIpAddr[0]);
  3544. SrcAddr.uSubNetMask=htonl(pFilterData->SourMask);
  3545. DstAddr.uIpAddr=htonl(pFilterData->DestnAddr.puIpAddr[0]);
  3546. DstAddr.uSubNetMask=htonl(pFilterData->DestMask);
  3547. if(!pFilterData->SourceAddr.pszDomainName)
  3548. {
  3549. // if only IP addr specified
  3550. if(pFilterData->bSrcAddrSpecified && !pFilterData->bSrcMaskSpecified)
  3551. {
  3552. if(pFilterData->SourceAddr.puIpAddr[0]!=0)
  3553. {
  3554. if(!bIsValidIPAddress(htonl(pFilterData->SourceAddr.puIpAddr[0]),TRUE,TRUE))
  3555. {
  3556. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTER_6);
  3557. dwReturn=ERROR_INVALID_DATA;
  3558. BAIL_OUT;
  3559. }
  3560. }
  3561. }
  3562. else if(pFilterData->bSrcAddrSpecified && pFilterData->bSrcMaskSpecified)
  3563. {
  3564. // if both IP and mask specified
  3565. if(!((pFilterData->SourceAddr.puIpAddr[0]==0) && ((pFilterData->SourMask==0)||(pFilterData->SourMask==MASK_ME))))
  3566. {
  3567. SrcAddr.AddrType= IP_ADDR_UNIQUE;
  3568. if (!IsValidSubnettedAddress(&SrcAddr) && !IsValidSubnet(&SrcAddr))
  3569. {
  3570. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTER_7);
  3571. dwReturn=ERROR_INVALID_DATA;
  3572. BAIL_OUT;
  3573. }
  3574. }
  3575. }
  3576. }
  3577. if(!pFilterData->DestnAddr.pszDomainName)
  3578. {
  3579. // if only IP addr specified
  3580. if(pFilterData->bDstAddrSpecified && !pFilterData->bDstMaskSpecified)
  3581. {
  3582. if(pFilterData->DestnAddr.puIpAddr[0]!=0)
  3583. {
  3584. if(!bIsValidIPAddress(htonl(pFilterData->DestnAddr.puIpAddr[0]),TRUE,TRUE))
  3585. {
  3586. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTER_11);
  3587. dwReturn=ERROR_INVALID_DATA;
  3588. BAIL_OUT;
  3589. }
  3590. }
  3591. }
  3592. else if(pFilterData->bDstAddrSpecified && pFilterData->bDstMaskSpecified)
  3593. {
  3594. // if both IP and mask specified
  3595. if(!((pFilterData->DestnAddr.puIpAddr[0]==0)&& ((pFilterData->DestMask==0)||(pFilterData->DestMask==MASK_ME))))
  3596. {
  3597. DstAddr.AddrType= IP_ADDR_UNIQUE;
  3598. if(!IsValidSubnettedAddress(&DstAddr) && !IsValidSubnet(&DstAddr))
  3599. {
  3600. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTER_12);
  3601. dwReturn=ERROR_INVALID_DATA;
  3602. BAIL_OUT;
  3603. }
  3604. }
  3605. }
  3606. }
  3607. if(!pFilterData->DestnAddr.pszDomainName && !pFilterData->SourceAddr.pszDomainName)
  3608. {
  3609. // check for address conflict
  3610. if(!(pFilterData->bSrcServerSpecified || pFilterData->bDstServerSpecified))
  3611. {
  3612. SrcAddr.AddrType=IP_ADDR_SUBNET;
  3613. DstAddr.AddrType=IP_ADDR_SUBNET;
  3614. if(AddressesConflict(SrcAddr,DstAddr))
  3615. {
  3616. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTER_8);
  3617. dwReturn=ERROR_INVALID_DATA;
  3618. BAIL_OUT;
  3619. }
  3620. }
  3621. }
  3622. if(pFilterData->DestnAddr.pszDomainName && pFilterData->SourceAddr.pszDomainName)
  3623. {
  3624. // check for same src and dst DNS names
  3625. if(_tcscmp(pFilterData->SourceAddr.pszDomainName,pFilterData->DestnAddr.pszDomainName)==0)
  3626. {
  3627. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTER_8);
  3628. dwReturn=ERROR_INVALID_DATA;
  3629. BAIL_OUT;
  3630. }
  3631. }
  3632. if(pFilterData->bSrcServerSpecified || pFilterData->bDstServerSpecified)
  3633. {
  3634. //validate special server
  3635. if(!IsSpecialServ(ExTypeToAddrType(pFilterData->ExType)))
  3636. {
  3637. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTER_9);
  3638. dwReturn=ERROR_INVALID_DATA;
  3639. BAIL_OUT;
  3640. }
  3641. }
  3642. if(pFilterData->bSrcServerSpecified || pFilterData->bDstServerSpecified)
  3643. {
  3644. if(pFilterData->ExType==EXT_NORMAL)
  3645. {
  3646. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTER_10);
  3647. dwReturn=ERROR_INVALID_DATA;
  3648. BAIL_OUT;
  3649. }
  3650. }
  3651. if(pFilterData->bSrcServerSpecified && pFilterData->bDstServerSpecified)
  3652. {
  3653. // check if both sides servers specified
  3654. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTER_3);
  3655. dwReturn=ERROR_INVALID_DATA;
  3656. }
  3657. error:
  3658. return dwReturn;
  3659. }
  3660. ////////////////////////////////////////////////////////////
  3661. //
  3662. //Function: CheckFilterListExistance()
  3663. //
  3664. //Date of Creation: 21st Aug 2001
  3665. //
  3666. //Parameters:
  3667. // IN HANDLE hPolicyStorage,
  3668. // IN LPTSTR pszFLName
  3669. //
  3670. //Return: BOOL
  3671. //
  3672. //Description:
  3673. // This function checks whether the specified filterlist exists in Policy store.
  3674. //
  3675. //Revision History:
  3676. //
  3677. // Date Author Comments
  3678. //
  3679. ////////////////////////////////////////////////////////////
  3680. BOOL
  3681. CheckFilterListExistance(
  3682. IN HANDLE hPolicyStorage,
  3683. IN LPTSTR pszFLName
  3684. )
  3685. {
  3686. PIPSEC_FILTER_DATA *ppFilterEnum = NULL;
  3687. BOOL bFilterExists=FALSE;
  3688. DWORD dwNumFilters=0;
  3689. DWORD dwReturnCode=ERROR_SUCCESS;
  3690. // check for duplicate Filterlist on same name
  3691. dwReturnCode = IPSecEnumFilterData(hPolicyStorage, &ppFilterEnum, &dwNumFilters);
  3692. if (dwReturnCode == ERROR_SUCCESS && dwNumFilters > 0 && ppFilterEnum != NULL)
  3693. {
  3694. DWORD i;
  3695. for (i = 0; i < dwNumFilters; i++)
  3696. {
  3697. if ( pszFLName && ppFilterEnum[i]->pszIpsecName && ( _tcscmp(ppFilterEnum[i]->pszIpsecName, pszFLName) == 0))
  3698. {
  3699. bFilterExists=TRUE;
  3700. break;
  3701. }
  3702. }
  3703. if(ppFilterEnum && dwNumFilters>0)
  3704. {
  3705. IPSecFreeMulFilterData( ppFilterEnum,dwNumFilters);
  3706. }
  3707. }
  3708. return bFilterExists;
  3709. }
  3710. ////////////////////////////////////////////////////////////
  3711. //
  3712. //Function: CheckFilterActionExistance()
  3713. //
  3714. //Date of Creation: 21st Aug 2001
  3715. //
  3716. //Parameters:
  3717. // IN HANDLE hPolicyStorage,
  3718. // IN LPTSTR pszFAName
  3719. //
  3720. //Return: BOOL
  3721. //
  3722. //Description:
  3723. // This function checks whether the specified filteraction exists in Policy store.
  3724. //
  3725. //Revision History:
  3726. //
  3727. // Date Author Comments
  3728. //
  3729. ////////////////////////////////////////////////////////////
  3730. BOOL
  3731. CheckFilterActionExistance(
  3732. IN HANDLE hPolicyStorage,
  3733. IN LPTSTR pszFAName
  3734. )
  3735. {
  3736. PIPSEC_NEGPOL_DATA *ppNegPolEnum = NULL;
  3737. BOOL bNegPolExists=FALSE;
  3738. DWORD dwNumNegPol=0;
  3739. DWORD dwReturnCode=ERROR_SUCCESS;
  3740. // check for duplicate Filteraction on same name
  3741. dwReturnCode = IPSecEnumNegPolData(hPolicyStorage, &ppNegPolEnum, &dwNumNegPol);
  3742. if (dwReturnCode == ERROR_SUCCESS && dwNumNegPol> 0 && ppNegPolEnum != NULL)
  3743. {
  3744. DWORD i;
  3745. for (i = 0; i < dwNumNegPol; i++)
  3746. {
  3747. if (ppNegPolEnum[i]->pszIpsecName && pszFAName && ( _tcscmp(ppNegPolEnum[i]->pszIpsecName, pszFAName) == 0))
  3748. {
  3749. bNegPolExists=TRUE;
  3750. break;
  3751. }
  3752. }
  3753. if (dwNumNegPol > 0 && ppNegPolEnum != NULL)
  3754. {
  3755. IPSecFreeMulNegPolData( ppNegPolEnum,dwNumNegPol);
  3756. }
  3757. }
  3758. return bNegPolExists;
  3759. }
  3760. ////////////////////////////////////////////////////////////
  3761. //
  3762. //Function: LoadAuthenticationInfos()
  3763. //
  3764. //Date of Creation: 21st Aug 2001
  3765. //
  3766. //Parameters:
  3767. // IN INT_MM_AUTH_METHODS AuthInfos,
  3768. // IN OUT PIPSEC_NFA_DATA &pRule
  3769. //
  3770. //Return: DWORD
  3771. //
  3772. //Description:
  3773. // This function loads the authentication details
  3774. //
  3775. //Revision History:
  3776. //
  3777. // Date Author Comments
  3778. //
  3779. ////////////////////////////////////////////////////////////
  3780. DWORD
  3781. LoadAuthenticationInfos(
  3782. IN STA_AUTH_METHODS AuthInfos,
  3783. IN OUT PIPSEC_NFA_DATA &pRule,
  3784. IN OUT BOOL &bCertConversionSuceeded
  3785. )
  3786. {
  3787. DWORD dwReturn = ERROR_SUCCESS;
  3788. BOOL bWarningPrinted = FALSE;
  3789. for (DWORD i = 0; i < pRule->dwAuthMethodCount; i++)
  3790. {
  3791. pRule->ppAuthMethods[i] = (PIPSEC_AUTH_METHOD) IPSecAllocPolMem(sizeof(IPSEC_AUTH_METHOD));
  3792. if(pRule->ppAuthMethods[i]== NULL)
  3793. {
  3794. dwReturn = ERROR_OUTOFMEMORY;
  3795. BAIL_OUT;
  3796. }
  3797. ZeroMemory(pRule->ppAuthMethods[i], sizeof(IPSEC_AUTH_METHOD));
  3798. pRule->ppAuthMethods[i]->dwAuthType = AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo->AuthMethod;
  3799. if (pRule->ppAuthMethods[i]->dwAuthType == IKE_SSPI) //kerb
  3800. {
  3801. pRule->ppAuthMethods[i]->dwAuthLen = 0;
  3802. pRule->ppAuthMethods[i]->pszAuthMethod = NULL;
  3803. pRule->ppAuthMethods[i]->dwAltAuthLen = 0;
  3804. pRule->ppAuthMethods[i]->pAltAuthMethod = NULL;
  3805. pRule->ppAuthMethods[i]->dwAuthFlags = 0;
  3806. }
  3807. else if (pRule->ppAuthMethods[i]->dwAuthType == IKE_RSA_SIGNATURE) //cert
  3808. {
  3809. LPTSTR pTemp = NULL;
  3810. pRule->ppAuthMethods[i]->dwAuthFlags = AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo->dwAuthFlags;
  3811. pRule->ppAuthMethods[i]->dwAltAuthLen = AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo->dwAuthInfoSize;
  3812. pRule->ppAuthMethods[i]->pAltAuthMethod = (PBYTE) IPSecAllocPolMem(AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo->dwAuthInfoSize);
  3813. if(pRule->ppAuthMethods[i]->pAltAuthMethod==NULL)
  3814. {
  3815. dwReturn = ERROR_OUTOFMEMORY;
  3816. BAIL_OUT;
  3817. }
  3818. memcpy(pRule->ppAuthMethods[i]->pAltAuthMethod, AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo->pAuthInfo, AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo->dwAuthInfoSize);
  3819. dwReturn = DecodeCertificateName(pRule->ppAuthMethods[i]->pAltAuthMethod, pRule->ppAuthMethods[i]->dwAltAuthLen, &pTemp);
  3820. if(dwReturn != ERROR_SUCCESS)
  3821. {
  3822. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_RULE_UPDATING_INFO);
  3823. bCertConversionSuceeded=FALSE;
  3824. }
  3825. pRule->ppAuthMethods[i]->pszAuthMethod = IPSecAllocPolStr(pTemp);
  3826. if(pRule->ppAuthMethods[i]->pszAuthMethod == NULL)
  3827. {
  3828. dwReturn = ERROR_OUTOFMEMORY;
  3829. BAIL_OUT;
  3830. }
  3831. pRule->ppAuthMethods[i]->dwAuthLen = wcslen(pRule->ppAuthMethods[i]->pszAuthMethod);
  3832. delete [] pTemp;
  3833. }
  3834. else if (pRule->ppAuthMethods[i]->dwAuthType == IKE_PRESHARED_KEY) //preshared key
  3835. {
  3836. pRule->ppAuthMethods[i]->dwAuthLen = AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo->dwAuthInfoSize / sizeof(WCHAR);
  3837. pRule->ppAuthMethods[i]->pszAuthMethod = (LPWSTR) IPSecAllocPolMem(AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo->dwAuthInfoSize + sizeof(WCHAR));
  3838. if(pRule->ppAuthMethods[i]->pszAuthMethod==NULL)
  3839. {
  3840. dwReturn = ERROR_OUTOFMEMORY;
  3841. BAIL_OUT;
  3842. }
  3843. memcpy(pRule->ppAuthMethods[i]->pszAuthMethod, AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo->pAuthInfo, AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo->dwAuthInfoSize);
  3844. pRule->ppAuthMethods[i]->pszAuthMethod[pRule->ppAuthMethods[i]->dwAuthLen] = 0;
  3845. pRule->ppAuthMethods[i]->dwAltAuthLen = 0;
  3846. pRule->ppAuthMethods[i]->pAltAuthMethod = NULL;
  3847. pRule->ppAuthMethods[i]->dwAuthFlags = 0;
  3848. }
  3849. }
  3850. error:
  3851. return dwReturn;
  3852. }
  3853. ////////////////////////////////////////////////////////////
  3854. //
  3855. //Function: ConvertMMAuthToStaticLocal()
  3856. //
  3857. //Date of Creation: 21st Aug 2001
  3858. //
  3859. //Parameters:
  3860. // IN PINT_IPSEC_MM_AUTH_INFO pAuthenticationInfo,
  3861. // IN DWORD dwAuthInfos,
  3862. // IN OUT STA_AUTH_METHODS &AuthInfos
  3863. //
  3864. //Return: DWORD
  3865. //
  3866. //Description:
  3867. // This function converts the PINT_IPSEC_MM_AUTH_INFO structure to STA_AUTH_METHODS
  3868. //
  3869. //Revision History:
  3870. //
  3871. // Date Author Comments
  3872. //
  3873. ////////////////////////////////////////////////////////////
  3874. DWORD
  3875. ConvertMMAuthToStaticLocal(
  3876. IN PINT_IPSEC_MM_AUTH_INFO pAuthenticationInfo,
  3877. IN DWORD dwAuthInfos,
  3878. IN OUT STA_AUTH_METHODS &AuthInfos
  3879. )
  3880. {
  3881. DWORD dwReturn = ERROR_SUCCESS;
  3882. DWORD i = 0 ;
  3883. if(pAuthenticationInfo == NULL || dwAuthInfos == 0 )
  3884. {
  3885. dwReturn = ERROR_INVALID_PARAMETER;
  3886. BAIL_OUT;
  3887. }
  3888. AuthInfos.dwNumAuthInfos = dwAuthInfos;
  3889. AuthInfos.pAuthMethodInfo = new STA_MM_AUTH_METHODS[AuthInfos.dwNumAuthInfos];
  3890. if(AuthInfos.pAuthMethodInfo == NULL)
  3891. {
  3892. dwReturn = ERROR_OUTOFMEMORY;
  3893. BAIL_OUT;
  3894. }
  3895. for(i=0;i<AuthInfos.dwNumAuthInfos;i++)
  3896. {
  3897. memset(&(AuthInfos.pAuthMethodInfo[i]),0,sizeof(STA_MM_AUTH_METHODS));
  3898. AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo = new INT_IPSEC_MM_AUTH_INFO;
  3899. if(AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo == NULL)
  3900. {
  3901. dwReturn = ERROR_OUTOFMEMORY;
  3902. BAIL_OUT;
  3903. }
  3904. memset(AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo ,0,sizeof(INT_IPSEC_MM_AUTH_INFO));
  3905. memcpy( AuthInfos.pAuthMethodInfo[i].pAuthenticationInfo , &(pAuthenticationInfo[i]),sizeof(INT_IPSEC_MM_AUTH_INFO));
  3906. }
  3907. error:
  3908. return dwReturn;
  3909. }
  3910. DWORD
  3911. ConnectStaticMachine(
  3912. IN LPCWSTR pwszMachine,
  3913. IN DWORD dwLocation
  3914. )
  3915. {
  3916. DWORD dwReturnCode;
  3917. HANDLE hPolicyStorage;
  3918. LPWSTR pszTarget = NULL;
  3919. // Connect to the appropriate store
  3920. //
  3921. dwReturnCode =
  3922. IPSecOpenPolicyStore(
  3923. (LPWSTR)pwszMachine,
  3924. dwLocation,
  3925. NULL,
  3926. &hPolicyStorage);
  3927. if (dwReturnCode == ERROR_SUCCESS)
  3928. {
  3929. if (dwLocation == IPSEC_DIRECTORY_PROVIDER)
  3930. {
  3931. pszTarget = g_StorageLocation.pszDomainName;
  3932. }
  3933. else
  3934. {
  3935. pszTarget = g_StorageLocation.pszMachineName;
  3936. }
  3937. if (pwszMachine)
  3938. {
  3939. _tcsncpy(pszTarget, pwszMachine,MAXCOMPUTERNAMELEN-1);
  3940. }
  3941. else
  3942. {
  3943. _tcsncpy(pszTarget, _TEXT(""), 2);
  3944. }
  3945. g_StorageLocation.dwLocation = dwLocation;
  3946. g_NshPolStoreHandle.SetStorageHandle(hPolicyStorage);
  3947. }
  3948. return dwReturnCode;
  3949. }