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.

2292 lines
61 KiB

  1. ////////////////////////////////////////////////////////////
  2. // Module: Static/Staticdelete.cpp
  3. //
  4. // Purpose: Static Module Implementation.
  5. //
  6. // Developers Name: surya
  7. //
  8. // History:
  9. //
  10. // Date Author Comments
  11. // 10-8-2001 Bharat Initial Version.
  12. // 21-8-2001 Surya
  13. ////////////////////////////////////////////////////////////
  14. #include "nshipsec.h"
  15. extern HINSTANCE g_hModule;
  16. extern STORAGELOCATION g_StorageLocation;
  17. extern CNshPolStore g_NshPolStoreHandle;
  18. extern CNshPolNegFilData g_NshPolNegFilData;
  19. ////////////////////////////////////////////////////////////
  20. //
  21. //Function: HandleStaticDeletePolicy()
  22. //
  23. //Date of Creation: 21st Aug 2001
  24. //
  25. //Parameters:
  26. // IN LPCWSTR pwszMachine,
  27. // IN OUT LPWSTR *ppwcArguments,
  28. // IN DWORD dwCurrentIndex,
  29. // IN DWORD dwArgCount,
  30. // IN DWORD dwFlags,
  31. // IN LPCVOID pvData,
  32. // OUT BOOL *pbDone
  33. //
  34. //Return: DWORD
  35. //
  36. //Description:
  37. // Implementation for the command "Delete Policy"
  38. //
  39. //Revision History:
  40. //
  41. //Date Author Comments
  42. //
  43. ////////////////////////////////////////////////////////////
  44. DWORD WINAPI
  45. HandleStaticDeletePolicy(
  46. IN LPCWSTR pwszMachine,
  47. IN OUT LPWSTR *ppwcArguments,
  48. IN DWORD dwCurrentIndex,
  49. IN DWORD dwArgCount,
  50. IN DWORD dwFlags,
  51. IN LPCVOID pvData,
  52. OUT BOOL *pbDone
  53. )
  54. {
  55. BOOL bExists=FALSE;
  56. BOOL bAll=FALSE,bAssigned=FALSE,bCompleteDelete=FALSE;
  57. LPTSTR pszPolicyName=NULL;
  58. PIPSEC_POLICY_DATA *ppPolicyEnum = NULL,pPolicy=NULL;
  59. DWORD dwNumPolicies = 0,dwRet = ERROR_SHOW_USAGE,dwCount=0,i=0;
  60. HANDLE hPolicyStorage=NULL;
  61. _TCHAR szUserInput[STRING_SIZE]={0};
  62. DWORD dwReturnCode = ERROR_SUCCESS,dwStrLength = 0;
  63. PARSER_PKT parser;
  64. ZeroMemory(&parser, sizeof(parser));
  65. const TAG_TYPE vcmdStaticDeletePolicy[] =
  66. {
  67. { CMD_TOKEN_STR_NAME, NS_REQ_ZERO, FALSE },
  68. { CMD_TOKEN_STR_ALL, NS_REQ_ZERO, FALSE }
  69. };
  70. const TOKEN_VALUE vtokStaticDeletePolicy[] =
  71. {
  72. { CMD_TOKEN_STR_NAME, CMD_TOKEN_NAME },
  73. { CMD_TOKEN_STR_ALL, CMD_TOKEN_ALL }
  74. };
  75. //if the user asked for usage, delegate the responsibility to netsh
  76. if(dwArgCount <= 3)
  77. {
  78. dwRet = ERROR_SHOW_USAGE;
  79. BAIL_OUT;
  80. }
  81. parser.ValidTok = vtokStaticDeletePolicy;
  82. parser.MaxTok = SIZEOF_TOKEN_VALUE(vtokStaticDeletePolicy);
  83. parser.ValidCmd = vcmdStaticDeletePolicy;
  84. parser.MaxCmd = SIZEOF_TAG_TYPE(vcmdStaticDeletePolicy);
  85. dwRet = Parser(pwszMachine,ppwcArguments,dwCurrentIndex,dwArgCount,&parser);
  86. if(dwRet != ERROR_SUCCESS)
  87. {
  88. CleanUp();
  89. if (dwRet==RETURN_NO_ERROR)
  90. {
  91. dwRet = ERROR_SUCCESS;
  92. }
  93. BAIL_OUT;
  94. }
  95. //get the user input
  96. for(dwCount=0;dwCount<parser.MaxTok;dwCount++)
  97. {
  98. switch(vtokStaticDeletePolicy[parser.Cmd[dwCount].dwCmdToken].dwValue)
  99. {
  100. case CMD_TOKEN_NAME:
  101. if (parser.Cmd[dwCount].dwStatus == VALID_TOKEN)
  102. {
  103. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  104. pszPolicyName= new _TCHAR[dwStrLength+1];
  105. if(pszPolicyName == NULL)
  106. {
  107. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  108. dwRet=ERROR_SUCCESS;
  109. BAIL_OUT;
  110. }
  111. _tcsncpy(pszPolicyName,(LPTSTR)parser.Cmd[dwCount].pArg,dwStrLength+1);
  112. }
  113. break;
  114. case CMD_TOKEN_ALL :
  115. if (parser.Cmd[dwCount].dwStatus == VALID_TOKEN)
  116. {
  117. bAll= *(BOOL *)parser.Cmd[dwCount].pArg;
  118. }
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. CleanUp();
  125. bCompleteDelete = TRUE;
  126. dwReturnCode = OpenPolicyStore(&hPolicyStorage);
  127. if (dwReturnCode != ERROR_SUCCESS)
  128. {
  129. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_FAILED_POLSTORE_OPEN);
  130. dwRet=ERROR_SUCCESS;
  131. BAIL_OUT;
  132. }
  133. dwReturnCode = IPSecEnumPolicyData(hPolicyStorage, &ppPolicyEnum, &dwNumPolicies);
  134. if (!(dwReturnCode == ERROR_SUCCESS && dwNumPolicies > 0 && ppPolicyEnum != NULL))
  135. {
  136. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_NO_POLICY);
  137. dwRet= ERROR_SUCCESS;
  138. BAIL_OUT;
  139. }
  140. //check for the required policy
  141. for (i = 0; i < dwNumPolicies; i++)
  142. {
  143. if ( bAll || (pszPolicyName && (_tcscmp(ppPolicyEnum[i]->pszIpsecName, pszPolicyName) == 0)))
  144. {
  145. bExists=TRUE;
  146. dwReturnCode = IPSecCopyPolicyData(ppPolicyEnum[i], &pPolicy);
  147. if (dwReturnCode != ERROR_SUCCESS)
  148. {
  149. dwRet = dwReturnCode;
  150. BAIL_OUT;
  151. }
  152. dwReturnCode = IPSecEnumNFAData(hPolicyStorage, pPolicy->PolicyIdentifier
  153. , &(pPolicy->ppIpsecNFAData), &(pPolicy->dwNumNFACount));
  154. if (dwReturnCode == ERROR_SUCCESS)
  155. {
  156. dwReturnCode=IPSecGetISAKMPData(hPolicyStorage, pPolicy->ISAKMPIdentifier, &(pPolicy->pIpsecISAKMPData));
  157. }
  158. if((dwReturnCode == ERROR_SUCCESS)&& !bAssigned )
  159. {
  160. if(g_StorageLocation.dwLocation!=IPSEC_DIRECTORY_PROVIDER)
  161. {
  162. dwReturnCode=IsAssigned(pPolicy,hPolicyStorage,bAssigned);
  163. }
  164. if(((dwReturnCode == ERROR_SUCCESS)||(dwReturnCode == ERROR_FILE_NOT_FOUND))&& bAssigned)
  165. {
  166. dwReturnCode=IPSecUnassignPolicy(hPolicyStorage, pPolicy->PolicyIdentifier);
  167. }
  168. }
  169. if (((dwReturnCode == ERROR_SUCCESS)||(dwReturnCode == ERROR_FILE_NOT_FOUND)))
  170. {
  171. // if found, proceed deleting
  172. dwReturnCode = DeletePolicy(pPolicy,hPolicyStorage,bCompleteDelete);
  173. if (dwReturnCode != ERROR_SUCCESS)
  174. {
  175. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_POLICY_3,pPolicy->pszIpsecName);
  176. }
  177. }
  178. }
  179. if((!bAll && bExists)||(dwReturnCode != ERROR_SUCCESS))
  180. break;
  181. }
  182. //incase due to some thing deletion failed, tell it to the user
  183. if((!bExists)&& (!bAll) && (dwReturnCode == ERROR_SUCCESS))
  184. {
  185. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SHW_STATIC_POLICY_2,pszPolicyName);
  186. }
  187. if (dwNumPolicies > 0 && ppPolicyEnum != NULL)
  188. {
  189. IPSecFreeMulPolicyData(ppPolicyEnum, dwNumPolicies);
  190. }
  191. dwRet= ERROR_SUCCESS;
  192. if (pPolicy)
  193. {
  194. IPSecFreePolicyData(pPolicy);
  195. }
  196. ClosePolicyStore(hPolicyStorage);
  197. error:
  198. if (pszPolicyName)
  199. {
  200. delete [] pszPolicyName;
  201. }
  202. return dwRet;
  203. }
  204. ////////////////////////////////////////////////////////////
  205. //
  206. //Function: HandleStaticDeleteFilterList()
  207. //
  208. //Date of Creation: 21st Aug 2001
  209. //
  210. //Parameters:
  211. // IN LPCWSTR pwszMachine,
  212. // IN OUT LPWSTR *ppwcArguments,
  213. // IN DWORD dwCurrentIndex,
  214. // IN DWORD dwArgCount,
  215. // IN DWORD dwFlags,
  216. // IN LPCVOID pvData,
  217. // OUT BOOL *pbDone
  218. //
  219. //Return: DWORD
  220. //
  221. //Description:
  222. // Implementation for the command "Delete FilterList"
  223. //
  224. //Revision History:
  225. //
  226. //Date Author Comments
  227. //
  228. ////////////////////////////////////////////////////////////
  229. DWORD WINAPI
  230. HandleStaticDeleteFilterList(
  231. IN LPCWSTR pwszMachine,
  232. IN OUT LPWSTR *ppwcArguments,
  233. IN DWORD dwCurrentIndex,
  234. IN DWORD dwArgCount,
  235. IN DWORD dwFlags,
  236. IN LPCVOID pvData,
  237. OUT BOOL *pbDone
  238. )
  239. {
  240. LPTSTR pszFListName=NULL;
  241. DWORD dwCount=0;
  242. PIPSEC_FILTER_DATA *ppFilterEnum = NULL,pFilter=NULL;
  243. DWORD dwNumFilters=0,cnt=0,dwRet = ERROR_SHOW_USAGE;
  244. HANDLE hPolicyStorage=NULL;
  245. _TCHAR szUserInput[STRING_SIZE]={0};
  246. BOOL bExists=FALSE,bAll=FALSE;
  247. DWORD dwReturnCode = ERROR_SUCCESS,dwStrLength = 0;
  248. PARSER_PKT parser;
  249. ZeroMemory(&parser, sizeof(parser));
  250. const TAG_TYPE vcmdStaticDeleteFilterList[] =
  251. {
  252. { CMD_TOKEN_STR_NAME, NS_REQ_ZERO, FALSE },
  253. { CMD_TOKEN_STR_ALL, NS_REQ_ZERO, FALSE }
  254. };
  255. const TOKEN_VALUE vtokStaticDeleteFilterList[] =
  256. {
  257. { CMD_TOKEN_STR_NAME, CMD_TOKEN_NAME },
  258. { CMD_TOKEN_STR_ALL, CMD_TOKEN_ALL }
  259. };
  260. //if the user asked for usage, delegate the responsibility to netsh
  261. if(dwArgCount <= 3)
  262. {
  263. dwRet = ERROR_SHOW_USAGE;
  264. BAIL_OUT;
  265. }
  266. parser.ValidTok = vtokStaticDeleteFilterList;
  267. parser.MaxTok = SIZEOF_TOKEN_VALUE(vtokStaticDeleteFilterList);
  268. parser.ValidCmd = vcmdStaticDeleteFilterList;
  269. parser.MaxCmd = SIZEOF_TAG_TYPE(vcmdStaticDeleteFilterList);
  270. dwRet = Parser(pwszMachine,ppwcArguments,dwCurrentIndex,dwArgCount,&parser);
  271. if(dwRet !=ERROR_SUCCESS)
  272. {
  273. CleanUp();
  274. if (dwRet==RETURN_NO_ERROR)
  275. {
  276. dwRet = ERROR_SUCCESS;
  277. }
  278. BAIL_OUT;
  279. }
  280. // get the parsed user input
  281. for(dwCount=0;dwCount<parser.MaxTok;dwCount++)
  282. {
  283. switch(vtokStaticDeleteFilterList[parser.Cmd[dwCount].dwCmdToken].dwValue)
  284. {
  285. case CMD_TOKEN_NAME:
  286. if (parser.Cmd[dwCount].dwStatus == VALID_TOKEN)
  287. {
  288. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  289. pszFListName= new _TCHAR[dwStrLength+1];
  290. if(pszFListName == NULL)
  291. {
  292. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  293. dwRet=ERROR_SUCCESS;
  294. BAIL_OUT;
  295. }
  296. _tcsncpy(pszFListName,(LPTSTR)parser.Cmd[dwCount].pArg,dwStrLength+1);
  297. }
  298. break;
  299. case CMD_TOKEN_ALL :
  300. if (parser.Cmd[dwCount].dwStatus == VALID_TOKEN)
  301. {
  302. bAll= *(BOOL *)parser.Cmd[dwCount].pArg;
  303. }
  304. break;
  305. default:
  306. break;
  307. }
  308. }
  309. CleanUp();
  310. dwReturnCode = OpenPolicyStore(&hPolicyStorage);
  311. if (dwReturnCode != ERROR_SUCCESS)
  312. {
  313. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_FAILED_POLSTORE_OPEN);
  314. dwRet=ERROR_SUCCESS;
  315. BAIL_OUT;
  316. }
  317. dwReturnCode = IPSecEnumFilterData(hPolicyStorage, &ppFilterEnum, &dwNumFilters);
  318. if (!(dwReturnCode == ERROR_SUCCESS && dwNumFilters > 0 && ppFilterEnum != NULL))
  319. {
  320. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_NO_FILTER_LIST);
  321. dwRet = ERROR_SUCCESS;
  322. BAIL_OUT;
  323. }
  324. // try to look for the specified filter
  325. for(cnt=0; cnt < dwNumFilters;cnt++)
  326. {
  327. if ( bAll ||((ppFilterEnum[cnt]->pszIpsecName)&&(pszFListName)&&(_tcscmp(ppFilterEnum[cnt]->pszIpsecName,pszFListName)==0)))
  328. {
  329. dwReturnCode = IPSecCopyFilterData(ppFilterEnum[cnt], &pFilter);
  330. if ((dwReturnCode == ERROR_SUCCESS) && (pFilter != NULL))
  331. {
  332. bExists=TRUE;
  333. dwReturnCode = DeleteFilterList(pFilter,hPolicyStorage);
  334. if (dwReturnCode == ERROR_INVALID_DATA)
  335. {
  336. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_FILTERLIST_2,pFilter->pszIpsecName);
  337. // if cannot delete, tell the user where it is used
  338. ShowWhereFLUsed(pFilter,hPolicyStorage);
  339. }
  340. else if (dwReturnCode != ERROR_SUCCESS)
  341. {
  342. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_FILTERLIST_ALL_FL_DEL_QUERY,pFilter->pszIpsecName);
  343. }
  344. }
  345. }
  346. if((!bAll && bExists)||((dwReturnCode != ERROR_SUCCESS)&&(dwReturnCode != ERROR_INVALID_DATA)))
  347. break;
  348. }
  349. // throw the error messages to the user
  350. if ((!bExists)&&(pszFListName)&&(dwReturnCode == ERROR_SUCCESS))
  351. {
  352. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SHW_STATIC_TAB_PRTALLFL_3,pszFListName);
  353. }
  354. if(ppFilterEnum && dwNumFilters>0)
  355. {
  356. IPSecFreeMulFilterData( ppFilterEnum,dwNumFilters);
  357. }
  358. if(pFilter)
  359. {
  360. IPSecFreeFilterData(pFilter);
  361. }
  362. dwRet = ERROR_SUCCESS;
  363. ClosePolicyStore(hPolicyStorage);
  364. error:
  365. if(pszFListName)
  366. {
  367. delete [] pszFListName;
  368. }
  369. return dwRet;
  370. }
  371. ////////////////////////////////////////////////////////////
  372. //
  373. //Function: HandleStaticDeleteFilter()
  374. //
  375. //Date of Creation: 21st Aug 2001
  376. //
  377. //Parameters:
  378. // IN LPCWSTR pwszMachine,
  379. // IN OUT LPWSTR *ppwcArguments,
  380. // IN DWORD dwCurrentIndex,
  381. // IN DWORD dwArgCount,
  382. // IN DWORD dwFlags,
  383. // IN LPCVOID pvData,
  384. // OUT BOOL *pbDone
  385. //
  386. //Return: DWORD
  387. //
  388. //Description:
  389. // Implementation for the command "Delete Filter"
  390. //
  391. //Revision History:
  392. //
  393. //Date Author Comments
  394. //
  395. ////////////////////////////////////////////////////////////
  396. DWORD WINAPI
  397. HandleStaticDeleteFilter(
  398. IN LPCWSTR pwszMachine,
  399. IN OUT LPWSTR *ppwcArguments,
  400. IN DWORD dwCurrentIndex,
  401. IN DWORD dwArgCount,
  402. IN DWORD dwFlags,
  403. IN LPCVOID pvData,
  404. OUT BOOL *pbDone
  405. )
  406. {
  407. DWORD dwNumFilters = 0,dwRet = ERROR_SHOW_USAGE,dwReturn=0,i=0;
  408. PIPSEC_FILTER_DATA *ppFilterEnum = NULL,pFilterData=NULL;
  409. HANDLE hPolicyStorage=NULL;
  410. BOOL bFilterExists=FALSE;
  411. PDELFILTERDATA pFilter=NULL;
  412. DWORD dwReturnCode = ERROR_SUCCESS;
  413. PARSER_PKT parser;
  414. ZeroMemory(&parser, sizeof(parser));
  415. const TAG_TYPE vcmdStaticDeleteFilter[] =
  416. {
  417. { CMD_TOKEN_STR_FILTERLIST, NS_REQ_PRESENT, FALSE },
  418. { CMD_TOKEN_STR_SRCADDR, NS_REQ_PRESENT, FALSE },
  419. { CMD_TOKEN_STR_DSTADDR, NS_REQ_PRESENT, FALSE },
  420. { CMD_TOKEN_STR_PROTO, NS_REQ_ZERO, FALSE },
  421. { CMD_TOKEN_STR_SRCMASK, NS_REQ_ZERO, FALSE },
  422. { CMD_TOKEN_STR_DSTMASK, NS_REQ_ZERO, FALSE },
  423. { CMD_TOKEN_STR_SRCPORT, NS_REQ_ZERO, FALSE },
  424. { CMD_TOKEN_STR_DSTPORT, NS_REQ_ZERO, FALSE },
  425. { CMD_TOKEN_STR_MIRROR, NS_REQ_ZERO, FALSE }
  426. };
  427. const TOKEN_VALUE vtokStaticDeleteFilter[] =
  428. {
  429. { CMD_TOKEN_STR_FILTERLIST, CMD_TOKEN_FILTERLIST },
  430. { CMD_TOKEN_STR_SRCADDR, CMD_TOKEN_SRCADDR },
  431. { CMD_TOKEN_STR_DSTADDR, CMD_TOKEN_DSTADDR },
  432. { CMD_TOKEN_STR_PROTO, CMD_TOKEN_PROTO },
  433. { CMD_TOKEN_STR_SRCMASK, CMD_TOKEN_SRCMASK },
  434. { CMD_TOKEN_STR_DSTMASK, CMD_TOKEN_DSTMASK },
  435. { CMD_TOKEN_STR_SRCPORT, CMD_TOKEN_SRCPORT },
  436. { CMD_TOKEN_STR_DSTPORT, CMD_TOKEN_DSTPORT },
  437. { CMD_TOKEN_STR_MIRROR, CMD_TOKEN_MIRROR }
  438. };
  439. //if the user asked for usage, delegate the responsibility to netsh
  440. if(dwArgCount <= 3)
  441. {
  442. dwRet = ERROR_SHOW_USAGE;
  443. BAIL_OUT;
  444. }
  445. parser.ValidTok = vtokStaticDeleteFilter;
  446. parser.MaxTok = SIZEOF_TOKEN_VALUE(vtokStaticDeleteFilter);
  447. parser.ValidCmd = vcmdStaticDeleteFilter;
  448. parser.MaxCmd = SIZEOF_TAG_TYPE(vcmdStaticDeleteFilter);
  449. dwRet = Parser(pwszMachine,ppwcArguments,dwCurrentIndex,dwArgCount,&parser);
  450. if(dwRet!=ERROR_SUCCESS)
  451. {
  452. CleanUp();
  453. if (dwRet==RETURN_NO_ERROR)
  454. {
  455. dwRet = ERROR_SUCCESS;
  456. }
  457. BAIL_OUT;
  458. }
  459. // get the parsed user input
  460. dwRet = FillDelFilterInfo(&pFilter,parser,vtokStaticDeleteFilter);
  461. if(dwReturn==ERROR_OUTOFMEMORY)
  462. {
  463. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  464. dwRet=ERROR_SUCCESS;
  465. BAIL_OUT;
  466. }
  467. if(pFilter->bSrcServerSpecified && pFilter->bDstServerSpecified)
  468. {
  469. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_STATIC_FILTER_3);
  470. BAIL_OUT;
  471. }
  472. dwReturnCode = OpenPolicyStore(&hPolicyStorage);
  473. if (dwReturnCode != ERROR_SUCCESS)
  474. {
  475. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_FAILED_POLSTORE_OPEN);
  476. dwRet=ERROR_SUCCESS;
  477. BAIL_OUT;
  478. }
  479. dwReturnCode = IPSecEnumFilterData(hPolicyStorage, &ppFilterEnum, &dwNumFilters);
  480. if (!(dwReturnCode == ERROR_SUCCESS && dwNumFilters > 0 && ppFilterEnum != NULL))
  481. {
  482. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_NO_FILTER_LIST);
  483. dwRet = ERROR_SUCCESS;
  484. BAIL_OUT;
  485. }
  486. // check for the specified filterlist
  487. for (i = 0; i < dwNumFilters; i++)
  488. {
  489. if ((ppFilterEnum[i]->pszIpsecName)&& (pFilter->pszFLName) && ( _tcscmp(ppFilterEnum[i]->pszIpsecName, pFilter->pszFLName) == 0))
  490. {
  491. bFilterExists=TRUE;
  492. dwReturnCode = IPSecCopyFilterData(ppFilterEnum[i], &pFilterData);
  493. }
  494. if (bFilterExists)
  495. break;
  496. }
  497. if(!bFilterExists)
  498. {
  499. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SHW_STATIC_TAB_PRTALLFL_3,pFilter->pszFLName);
  500. }
  501. if(ppFilterEnum && dwNumFilters>0)
  502. {
  503. IPSecFreeMulFilterData( ppFilterEnum,dwNumFilters);
  504. }
  505. // check for the specified filterspec in the found filterlist
  506. if ( bFilterExists && dwReturnCode == ERROR_SUCCESS)
  507. {
  508. if(pFilterData->dwFlags & POLSTORE_READONLY )
  509. {
  510. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SET_FL_READ_ONLY_OBJECT,pFilterData->pszIpsecName);
  511. BAIL_OUT;
  512. }
  513. if (!DeleteSpecifiedFilter(pFilterData,pFilter))
  514. {
  515. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_FILTER_1,pFilter->pszFLName);
  516. }
  517. else
  518. {
  519. dwReturnCode = IPSecSetFilterData(hPolicyStorage, pFilterData);
  520. if (dwReturnCode != ERROR_SUCCESS)
  521. {
  522. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_FILTER_2,pFilter->pszFLName);
  523. }
  524. }
  525. }
  526. ClosePolicyStore(hPolicyStorage);
  527. dwRet=ERROR_SUCCESS;
  528. error:
  529. CleanUpLocalDelFilterDataStructure(pFilter);
  530. return dwRet;
  531. }
  532. //////////////////////////////////////////////////////////////////
  533. //
  534. //Function: DeleteSpecifiedFilter()
  535. //
  536. //Date of Creation: 21st Aug 2001
  537. //
  538. //Parameters:
  539. // IN OUT PIPSEC_FILTER_DATA pFilterData,
  540. // IN PDELFILTERDATA pDeleteFilter
  541. //
  542. //Return: BOOL
  543. //
  544. //Description:
  545. // This function deletes a specific filter out of the filterlist
  546. //
  547. //Revision History:
  548. //
  549. //Date Author Comments
  550. //
  551. ////////////////////////////////////////////////////////////////////
  552. BOOL
  553. DeleteSpecifiedFilter(
  554. IN OUT PIPSEC_FILTER_DATA pFilterData,
  555. IN PDELFILTERDATA pDeleteFilter
  556. )
  557. {
  558. DWORD i=0;
  559. BOOL bFilterDeleted=FALSE;
  560. BOOL bDeletedFilter=FALSE;
  561. DWORD dwOldNumFilters=pFilterData->dwNumFilterSpecs;
  562. while(dwOldNumFilters > i)
  563. {
  564. if(
  565. (
  566. pDeleteFilter->SourceAddr.puIpAddr && (pFilterData->ppFilterSpecs[i]->Filter.SrcAddr == pDeleteFilter->SourceAddr.puIpAddr[0]) ||
  567. (
  568. pDeleteFilter->SourceAddr.pszDomainName && pFilterData->ppFilterSpecs[i]->pszSrcDNSName &&
  569. (_tcscmp(pFilterData->ppFilterSpecs[i]->pszSrcDNSName,pDeleteFilter->SourceAddr.pszDomainName)==0)
  570. )
  571. )
  572. &&
  573. (
  574. pDeleteFilter->DestnAddr.puIpAddr && (pFilterData->ppFilterSpecs[i]->Filter.DestAddr == pDeleteFilter->DestnAddr.puIpAddr[0]) ||
  575. (
  576. pDeleteFilter->DestnAddr.pszDomainName && pFilterData->ppFilterSpecs[i]->pszDestDNSName &&
  577. (_tcscmp(pFilterData->ppFilterSpecs[i]->pszDestDNSName,pDeleteFilter->DestnAddr.pszDomainName)==0)
  578. )
  579. )
  580. )
  581. {
  582. bFilterDeleted=TRUE;
  583. if (pDeleteFilter->bMirrorSpecified) // if mirror specified
  584. {
  585. if(pFilterData->ppFilterSpecs[i]->dwMirrorFlag == (DWORD) (pDeleteFilter->bMirrored))
  586. {
  587. bFilterDeleted=TRUE;
  588. }
  589. else
  590. {
  591. bFilterDeleted=FALSE;
  592. }
  593. }
  594. if (bFilterDeleted && pDeleteFilter->bSrcMaskSpecified) // if srcmask specified
  595. {
  596. if(pFilterData->ppFilterSpecs[i]->Filter.SrcMask == pDeleteFilter->SourMask)
  597. {
  598. bFilterDeleted=TRUE;
  599. }
  600. else
  601. {
  602. bFilterDeleted=FALSE;
  603. }
  604. }
  605. if (bFilterDeleted && pDeleteFilter->bDstMaskSpecified) // if dstmask specified
  606. {
  607. if(pFilterData->ppFilterSpecs[i]->Filter.DestMask == pDeleteFilter->DestMask)
  608. {
  609. bFilterDeleted=TRUE;
  610. }
  611. else
  612. {
  613. bFilterDeleted=FALSE;
  614. }
  615. }
  616. if (bFilterDeleted && pDeleteFilter->bProtocolSpecified)
  617. {
  618. if(pFilterData->ppFilterSpecs[i]->Filter.Protocol == pDeleteFilter->dwProtocol)
  619. {
  620. bFilterDeleted=TRUE;
  621. }
  622. else
  623. {
  624. bFilterDeleted=FALSE;
  625. }
  626. }
  627. if (bFilterDeleted && pDeleteFilter->bSrcPortSpecified)
  628. {
  629. if(pFilterData->ppFilterSpecs[i]->Filter.SrcPort == pDeleteFilter->SourPort)
  630. {
  631. bFilterDeleted=TRUE;
  632. }
  633. else
  634. {
  635. bFilterDeleted=FALSE;
  636. }
  637. }
  638. if (bFilterDeleted && pDeleteFilter->bDstPortSpecified)
  639. {
  640. if(pFilterData->ppFilterSpecs[i]->Filter.DestPort == pDeleteFilter->DestPort)
  641. {
  642. bFilterDeleted=TRUE;
  643. }
  644. else
  645. {
  646. bFilterDeleted=FALSE;
  647. }
  648. }
  649. if (bFilterDeleted && (pDeleteFilter->bSrcServerSpecified || pDeleteFilter->bDstServerSpecified))
  650. {
  651. if(pFilterData->ppFilterSpecs[i]->Filter.ExType == pDeleteFilter->ExType)
  652. {
  653. bFilterDeleted=TRUE;
  654. }
  655. else
  656. {
  657. bFilterDeleted=FALSE;
  658. }
  659. }
  660. }
  661. if(bFilterDeleted) // if found delete and realloc memory
  662. {
  663. IPSecFreeFilterSpec(pFilterData->ppFilterSpecs[i]);
  664. pFilterData->ppFilterSpecs[i]=NULL;
  665. pFilterData->dwNumFilterSpecs--;
  666. bDeletedFilter=TRUE;
  667. bFilterDeleted=FALSE;
  668. }
  669. i++;
  670. }
  671. if(bDeletedFilter)
  672. {
  673. pFilterData->ppFilterSpecs=ReAllocFilterSpecMem(pFilterData->ppFilterSpecs,dwOldNumFilters,pFilterData->dwNumFilterSpecs);
  674. }
  675. return bDeletedFilter;
  676. }
  677. //////////////////////////////////////////////////////////////////
  678. //
  679. //Function: HandleStaticDeleteFilterActions()
  680. //
  681. //Date of Creation: 21st Aug 2001
  682. //
  683. //Parameters:
  684. // IN LPCWSTR pwszMachine,
  685. // IN OUT LPWSTR *ppwcArguments,
  686. // IN DWORD dwCurrentIndex,
  687. // IN DWORD dwArgCount,
  688. // IN DWORD dwFlags,
  689. // IN LPCVOID pvData,
  690. // OUT BOOL *pbDone
  691. //
  692. //Return: DWORD
  693. //
  694. //Description:
  695. // Implementation for the command "Delete FilterActions"
  696. //
  697. //Revision History:
  698. //
  699. //Date Author Comments
  700. //
  701. //////////////////////////////////////////////////////////////////
  702. DWORD WINAPI
  703. HandleStaticDeleteFilterActions(
  704. IN LPCWSTR pwszMachine,
  705. IN OUT LPWSTR *ppwcArguments,
  706. IN DWORD dwCurrentIndex,
  707. IN DWORD dwArgCount,
  708. IN DWORD dwFlags,
  709. IN LPCVOID pvData,
  710. OUT BOOL *pbDone
  711. )
  712. {
  713. LPTSTR pszFactName=NULL;
  714. DWORD dwCount=0;
  715. PIPSEC_NEGPOL_DATA *ppNegPolEnum = NULL,pNegPol=NULL;
  716. DWORD dwNumNegPolicies=0,cnt=0,dwRet = ERROR_SHOW_USAGE;
  717. HANDLE hPolicyStorage=NULL;
  718. _TCHAR szUserInput[STRING_SIZE]={0};
  719. BOOL bExists=FALSE,bAll=FALSE;
  720. DWORD dwReturnCode = ERROR_SUCCESS , dwStrLength = 0;
  721. PARSER_PKT parser;
  722. ZeroMemory(&parser, sizeof(parser));
  723. const TAG_TYPE vcmdStaticDeleteFilterActions[] =
  724. {
  725. { CMD_TOKEN_STR_NAME, NS_REQ_ZERO, FALSE },
  726. { CMD_TOKEN_STR_ALL, NS_REQ_ZERO, FALSE }
  727. };
  728. const TOKEN_VALUE vtokStaticDeleteFilterActions[] =
  729. {
  730. { CMD_TOKEN_STR_NAME, CMD_TOKEN_NAME },
  731. { CMD_TOKEN_STR_ALL, CMD_TOKEN_ALL }
  732. };
  733. //if the user asked for usage, delegate the responsibility to netsh
  734. if(dwArgCount <= 3)
  735. {
  736. dwRet = ERROR_SHOW_USAGE;
  737. BAIL_OUT;
  738. }
  739. parser.ValidTok = vtokStaticDeleteFilterActions;
  740. parser.MaxTok = SIZEOF_TOKEN_VALUE(vtokStaticDeleteFilterActions);
  741. parser.ValidCmd = vcmdStaticDeleteFilterActions;
  742. parser.MaxCmd = SIZEOF_TAG_TYPE(vcmdStaticDeleteFilterActions);
  743. dwRet = Parser(pwszMachine,ppwcArguments,dwCurrentIndex,dwArgCount,&parser);
  744. if(dwRet != ERROR_SUCCESS)
  745. {
  746. CleanUp();
  747. if (dwRet==RETURN_NO_ERROR)
  748. {
  749. dwRet = ERROR_SUCCESS;
  750. }
  751. BAIL_OUT;
  752. }
  753. for(dwCount=0;dwCount<parser.MaxTok;dwCount++)
  754. {
  755. switch(vtokStaticDeleteFilterActions[parser.Cmd[dwCount].dwCmdToken].dwValue)
  756. {
  757. case CMD_TOKEN_NAME:
  758. if (parser.Cmd[dwCount].dwStatus == VALID_TOKEN)
  759. {
  760. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  761. pszFactName= new _TCHAR[dwStrLength+1];
  762. if(pszFactName==NULL)
  763. {
  764. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  765. dwRet=ERROR_SUCCESS;
  766. BAIL_OUT;
  767. }
  768. _tcsncpy(pszFactName,(LPTSTR)parser.Cmd[dwCount].pArg,dwStrLength+1);
  769. }
  770. break;
  771. case CMD_TOKEN_ALL :
  772. if (parser.Cmd[dwCount].dwStatus == VALID_TOKEN)
  773. {
  774. bAll= *(BOOL *)parser.Cmd[dwCount].pArg;
  775. }
  776. break;
  777. default:
  778. break;
  779. }
  780. }
  781. CleanUp();
  782. dwReturnCode = OpenPolicyStore(&hPolicyStorage);
  783. if (dwReturnCode != ERROR_SUCCESS)
  784. {
  785. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_FAILED_POLSTORE_OPEN);
  786. dwRet=ERROR_SUCCESS;
  787. BAIL_OUT;
  788. }
  789. dwReturnCode = IPSecEnumNegPolData(hPolicyStorage, &ppNegPolEnum, &dwNumNegPolicies);
  790. if (!(dwReturnCode == ERROR_SUCCESS && dwNumNegPolicies > 0 && ppNegPolEnum != NULL))
  791. {
  792. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_NO_FILTER_ACTION);
  793. dwRet = ERROR_SUCCESS;
  794. BAIL_OUT;
  795. }
  796. // check for the specified filteraction
  797. for(cnt=0; cnt < dwNumNegPolicies;cnt++)
  798. {
  799. if(IsEqualGUID(ppNegPolEnum[cnt]->NegPolType,GUID_NEGOTIATION_TYPE_DEFAULT) ||(!ppNegPolEnum[cnt]->pszIpsecName))
  800. {
  801. DeleteFilterAction(ppNegPolEnum[cnt],hPolicyStorage);
  802. continue;
  803. }
  804. if (bAll || (pszFactName &&(_tcscmp(ppNegPolEnum[cnt]->pszIpsecName,pszFactName)==0)))
  805. {
  806. dwReturnCode = IPSecCopyNegPolData(ppNegPolEnum[cnt], &pNegPol);
  807. if ((dwReturnCode == ERROR_SUCCESS) && pNegPol)
  808. {
  809. bExists=TRUE;
  810. dwReturnCode = DeleteFilterAction(pNegPol,hPolicyStorage);
  811. if (dwReturnCode == ERROR_INVALID_DATA)
  812. {
  813. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_FILTERACTION_2,pNegPol->pszIpsecName);
  814. ShowWhereFAUsed(pNegPol,hPolicyStorage);
  815. }
  816. else if (dwReturnCode != ERROR_SUCCESS)
  817. {
  818. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_FILTERACTION_ALL_FA_DEL_QUERY,pNegPol->pszIpsecName);
  819. }
  820. }
  821. }
  822. if((!bAll && bExists)||((dwReturnCode != ERROR_SUCCESS)&&(dwReturnCode != ERROR_INVALID_DATA)))
  823. break;
  824. }
  825. // throw the error messages to the user
  826. if ((!bAll)&&(!bExists)&&(dwReturnCode == ERROR_SUCCESS))
  827. {
  828. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SHW_STATIC_TAB_PRTALLFA_FA_COUNT_LIST,pszFactName);
  829. }
  830. if (dwNumNegPolicies > 0 && ppNegPolEnum != NULL)
  831. {
  832. IPSecFreeMulNegPolData(ppNegPolEnum, dwNumNegPolicies);
  833. }
  834. if(pNegPol)
  835. {
  836. IPSecFreeNegPolData(pNegPol);
  837. }
  838. ClosePolicyStore(hPolicyStorage);
  839. dwRet=ERROR_SUCCESS;
  840. error:
  841. if(pszFactName)
  842. {
  843. delete [] pszFactName;
  844. }
  845. return dwRet;
  846. }
  847. //////////////////////////////////////////////////////////////////
  848. //
  849. //Function: HandleStaticDeleteRule()
  850. //
  851. //Date of Creation: 21st Aug 2001
  852. //
  853. //Parameters:
  854. // IN LPCWSTR pwszMachine,
  855. // IN OUT LPWSTR *ppwcArguments,
  856. // IN DWORD dwCurrentIndex,
  857. // IN DWORD dwArgCount,
  858. // IN DWORD dwFlags,
  859. // IN LPCVOID pvData,
  860. // OUT BOOL *pbDone
  861. //
  862. //Return: DWORD
  863. //
  864. //Description:
  865. // Implementation for the command "Delete Rule"
  866. //
  867. //Revision History:
  868. //
  869. //Date Author Comments
  870. //
  871. //////////////////////////////////////////////////////////////////
  872. DWORD WINAPI
  873. HandleStaticDeleteRule(
  874. IN LPCWSTR pwszMachine,
  875. IN OUT LPWSTR *ppwcArguments,
  876. IN DWORD dwCurrentIndex,
  877. IN DWORD dwArgCount,
  878. IN DWORD dwFlags,
  879. IN LPCVOID pvData,
  880. OUT BOOL *pbDone
  881. )
  882. {
  883. BOOL bAll=FALSE,bExists=FALSE,bRuleExists=FALSE,bAssigned=FALSE,bCompleteDelete=FALSE;
  884. LPTSTR pszPolicyName=NULL,pszRuleName=NULL;
  885. DWORD dwNumPolicies = 0,dwRuleId = 0;
  886. DWORD dwRet = ERROR_SHOW_USAGE,i=0,dwCount=0;
  887. PIPSEC_POLICY_DATA *ppPolicyEnum = NULL,pPolicy=NULL;
  888. HANDLE hPolicyStorage=NULL;
  889. RPC_STATUS RpcStat =RPC_S_OK;
  890. _TCHAR szUserInput[STRING_SIZE]={0};
  891. DWORD dwReturnCode = ERROR_SUCCESS , dwStrLength = 0;
  892. PARSER_PKT parser;
  893. ZeroMemory(&parser, sizeof(parser));
  894. const TAG_TYPE vcmdStaticDeleteRule[] =
  895. {
  896. { CMD_TOKEN_STR_NAME, NS_REQ_ZERO, FALSE },
  897. { CMD_TOKEN_STR_ID, NS_REQ_ZERO, FALSE },
  898. { CMD_TOKEN_STR_ALL, NS_REQ_ZERO, FALSE },
  899. { CMD_TOKEN_STR_POLICY, NS_REQ_ZERO, FALSE },
  900. };
  901. const TOKEN_VALUE vtokStaticDeleteRule[] =
  902. {
  903. { CMD_TOKEN_STR_NAME, CMD_TOKEN_NAME },
  904. { CMD_TOKEN_STR_ID, CMD_TOKEN_ID },
  905. { CMD_TOKEN_STR_ALL, CMD_TOKEN_ALL },
  906. { CMD_TOKEN_STR_POLICY, CMD_TOKEN_POLICY }
  907. };
  908. //if the user asked for usage, delegate the responsibility to netsh
  909. if(dwArgCount <= 3)
  910. {
  911. dwRet = ERROR_SHOW_USAGE;
  912. BAIL_OUT;
  913. }
  914. parser.ValidTok = vtokStaticDeleteRule;
  915. parser.MaxTok = SIZEOF_TOKEN_VALUE(vtokStaticDeleteRule);
  916. parser.ValidCmd = vcmdStaticDeleteRule;
  917. parser.MaxCmd = SIZEOF_TAG_TYPE(vcmdStaticDeleteRule);
  918. dwRet = Parser(pwszMachine,ppwcArguments,dwCurrentIndex,dwArgCount,&parser);
  919. if(dwRet != ERROR_SUCCESS)
  920. {
  921. CleanUp();
  922. if (dwRet==RETURN_NO_ERROR)
  923. {
  924. dwRet = ERROR_SUCCESS;
  925. }
  926. BAIL_OUT;
  927. }
  928. // get the parsed user input
  929. for(dwCount=0;dwCount < parser.MaxTok;dwCount++)
  930. {
  931. switch(vtokStaticDeleteRule[parser.Cmd[dwCount].dwCmdToken].dwValue)
  932. {
  933. case CMD_TOKEN_NAME :
  934. if (parser.Cmd[dwCount].dwStatus == VALID_TOKEN)
  935. {
  936. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  937. pszRuleName= new _TCHAR[dwStrLength+1];
  938. if(pszRuleName==NULL)
  939. {
  940. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  941. dwRet=ERROR_SUCCESS;
  942. BAIL_OUT;
  943. }
  944. _tcsncpy(pszRuleName,(LPTSTR)parser.Cmd[dwCount].pArg,dwStrLength+1);
  945. }
  946. break;
  947. case CMD_TOKEN_POLICY :
  948. if (parser.Cmd[dwCount].dwStatus == VALID_TOKEN)
  949. {
  950. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  951. pszPolicyName= new _TCHAR[dwStrLength+1];
  952. if(pszPolicyName==NULL)
  953. {
  954. PrintErrorMessage(WIN32_ERR,ERROR_OUTOFMEMORY,NULL);
  955. dwRet=ERROR_SUCCESS;
  956. BAIL_OUT;
  957. }
  958. _tcsncpy(pszPolicyName,(LPTSTR)parser.Cmd[dwCount].pArg,dwStrLength+1);
  959. }
  960. break;
  961. case CMD_TOKEN_ID :
  962. if (parser.Cmd[dwCount].dwStatus == VALID_TOKEN)
  963. {
  964. dwRuleId= *(DWORD *)parser.Cmd[dwCount].pArg;
  965. }
  966. break;
  967. case CMD_TOKEN_ALL :
  968. if (parser.Cmd[dwCount].dwStatus == VALID_TOKEN)
  969. {
  970. bAll= *(BOOL *)parser.Cmd[dwCount].pArg;
  971. }
  972. break;
  973. default:
  974. break;
  975. }
  976. }
  977. CleanUp();
  978. // if no policy name, bail out
  979. if (!pszPolicyName)
  980. {
  981. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SHW_STATIC_RULE_4);
  982. return dwRet;
  983. }
  984. dwReturnCode = OpenPolicyStore(&hPolicyStorage);
  985. if (dwReturnCode != ERROR_SUCCESS)
  986. {
  987. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_FAILED_POLSTORE_OPEN);
  988. dwRet=ERROR_SUCCESS;
  989. BAIL_OUT;
  990. }
  991. dwReturnCode = IPSecEnumPolicyData(hPolicyStorage, &ppPolicyEnum, &dwNumPolicies);
  992. if (!(dwReturnCode == ERROR_SUCCESS && dwNumPolicies > 0 && ppPolicyEnum != NULL))
  993. {
  994. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_NO_POLICY);
  995. dwRet = ERROR_SUCCESS;
  996. BAIL_OUT;
  997. }
  998. // get the required policy
  999. for (i = 0; i < dwNumPolicies; i++)
  1000. {
  1001. if ((pszPolicyName!=NULL)&&( wcscmp(ppPolicyEnum[i]->pszIpsecName, pszPolicyName) == 0))
  1002. {
  1003. bExists=TRUE;
  1004. dwReturnCode = IPSecCopyPolicyData(ppPolicyEnum[i], &pPolicy);
  1005. if (dwReturnCode != ERROR_SUCCESS)
  1006. {
  1007. dwRet = dwReturnCode;
  1008. BAIL_OUT;
  1009. }
  1010. dwReturnCode = IPSecEnumNFAData(hPolicyStorage, pPolicy->PolicyIdentifier
  1011. , &(pPolicy->ppIpsecNFAData), &(pPolicy->dwNumNFACount));
  1012. if (dwReturnCode == ERROR_SUCCESS)
  1013. {
  1014. DWORD j;
  1015. for (j = 0; j < pPolicy->dwNumNFACount && (dwReturnCode == ERROR_SUCCESS); j++)
  1016. {
  1017. if (!UuidIsNil(&(pPolicy->ppIpsecNFAData[j]->NegPolIdentifier), &RpcStat))
  1018. {
  1019. dwReturnCode=IPSecGetNegPolData(hPolicyStorage, pPolicy->ppIpsecNFAData[j]->NegPolIdentifier,&(pPolicy->ppIpsecNFAData[j]->pIpsecNegPolData));
  1020. }
  1021. if ((!UuidIsNil(&(pPolicy->ppIpsecNFAData[j]->FilterIdentifier), &RpcStat))&&(dwReturnCode == ERROR_SUCCESS))
  1022. {
  1023. dwReturnCode=IPSecGetFilterData(hPolicyStorage, pPolicy->ppIpsecNFAData[j]->FilterIdentifier,&(pPolicy->ppIpsecNFAData[j]->pIpsecFilterData));
  1024. }
  1025. }
  1026. }
  1027. for (DWORD n = 0; n < pPolicy->dwNumNFACount && (dwReturnCode == ERROR_SUCCESS); n++)
  1028. if (bAll||((pPolicy->ppIpsecNFAData[n]->pszIpsecName)&& pszRuleName &&(_tcscmp(pPolicy->ppIpsecNFAData[n]->pszIpsecName,pszRuleName)==0))||((dwRuleId-1)==n))
  1029. {
  1030. bRuleExists=TRUE;
  1031. if((dwReturnCode == ERROR_SUCCESS)&&(!bAssigned) && g_StorageLocation.dwLocation != IPSEC_DIRECTORY_PROVIDER)
  1032. {
  1033. dwReturnCode=IsAssigned(pPolicy,hPolicyStorage,bAssigned);
  1034. if(( dwReturnCode == ERROR_SUCCESS || dwReturnCode == ERROR_FILE_NOT_FOUND )&&bAssigned)
  1035. {
  1036. dwReturnCode=IPSecUnassignPolicy(hPolicyStorage, pPolicy->PolicyIdentifier);
  1037. }
  1038. }
  1039. // if user asked to delete default rule, throw error to the user
  1040. if(pPolicy->ppIpsecNFAData[n]->pIpsecNegPolData->NegPolType!=GUID_NEGOTIATION_TYPE_DEFAULT)
  1041. {
  1042. if (((dwReturnCode == ERROR_SUCCESS)||(dwReturnCode == ERROR_FILE_NOT_FOUND)))
  1043. {
  1044. dwReturnCode=DeleteRule(pPolicy,pPolicy->ppIpsecNFAData[n],hPolicyStorage,bCompleteDelete);
  1045. if (dwReturnCode!=ERROR_SUCCESS)
  1046. {
  1047. if (bAll&&(pPolicy->ppIpsecNFAData[n]->pszIpsecName))
  1048. {
  1049. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_RULE_3,pPolicy->ppIpsecNFAData[n]->pszIpsecName);
  1050. }
  1051. else if (!bAll && pszRuleName)
  1052. {
  1053. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_RULE_3,pszRuleName);
  1054. }
  1055. else
  1056. {
  1057. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_RULE_4,dwRuleId);
  1058. }
  1059. dwRet = ERROR_SUCCESS;
  1060. }
  1061. }
  1062. }
  1063. else
  1064. {
  1065. PrintMessageFromModule(g_hModule,ERR_DEL_STATIC_RULE_DEF_DEL_NOT_ALLOWED);
  1066. dwRet = ERROR_SUCCESS;
  1067. }
  1068. if(!bAll && bExists)
  1069. break;
  1070. }
  1071. if (bAssigned && g_StorageLocation.dwLocation != IPSEC_DIRECTORY_PROVIDER)
  1072. {
  1073. IPSecAssignPolicy(hPolicyStorage, pPolicy->PolicyIdentifier);
  1074. }
  1075. if((!bRuleExists)&& pszRuleName &&(dwReturnCode==ERROR_SUCCESS))
  1076. {
  1077. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_RULE_10,pszRuleName);
  1078. }
  1079. else if(!bRuleExists)
  1080. {
  1081. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SHW_STATIC_RULE_3,dwRuleId);
  1082. }
  1083. }
  1084. if(bExists)
  1085. break;
  1086. }
  1087. if(!bExists )
  1088. {
  1089. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SHW_STATIC_POLICY_2,pszPolicyName);
  1090. }
  1091. if (dwNumPolicies > 0 && ppPolicyEnum != NULL)
  1092. {
  1093. IPSecFreeMulPolicyData(ppPolicyEnum, dwNumPolicies);
  1094. }
  1095. ClosePolicyStore(hPolicyStorage);
  1096. dwRet=ERROR_SUCCESS;
  1097. if (pPolicy)
  1098. {
  1099. IPSecFreePolicyData(pPolicy);
  1100. }
  1101. error: // clean up
  1102. if (pszPolicyName)
  1103. {
  1104. delete [] pszPolicyName;
  1105. }
  1106. if (pszRuleName)
  1107. {
  1108. delete [] pszRuleName;
  1109. }
  1110. return dwRet;
  1111. }
  1112. //////////////////////////////////////////////////////////////////////
  1113. //
  1114. //Function: HandleStaticDeleteAll()
  1115. //
  1116. //Date of Creation: 21st Aug 2001
  1117. //
  1118. //Parameters:
  1119. // IN LPCWSTR pwszMachine,
  1120. // IN OUT LPWSTR *ppwcArguments,
  1121. // IN DWORD dwCurrentIndex,
  1122. // IN DWORD dwArgCount,
  1123. // IN DWORD dwFlags,
  1124. // IN LPCVOID pvData,
  1125. // OUT BOOL *pbDone
  1126. //
  1127. //Return: DWORD
  1128. //
  1129. //Description:
  1130. // Implementation for the command "Delete All"
  1131. //
  1132. //Revision History:
  1133. //
  1134. //Date Author Comments
  1135. //
  1136. ///////////////////////////////////////////////////////////////////////
  1137. DWORD WINAPI
  1138. HandleStaticDeleteAll(
  1139. IN LPCWSTR pwszMachine,
  1140. IN OUT LPWSTR *ppwcArguments,
  1141. IN DWORD dwCurrentIndex,
  1142. IN DWORD dwArgCount,
  1143. IN DWORD dwFlags,
  1144. IN LPCVOID pvData,
  1145. OUT BOOL *pbDone
  1146. )
  1147. {
  1148. BOOL bAssigned=FALSE,bCompleteDelete=TRUE;
  1149. DWORD dwNumPolicies = 0,dwRet = ERROR_SUCCESS,i=0;
  1150. PIPSEC_POLICY_DATA *ppPolicyEnum = NULL,pPolicy=NULL;
  1151. HANDLE hPolicyStorage=NULL;
  1152. _TCHAR szUserInput[STRING_SIZE]={0};
  1153. DWORD dwReturnCode = ERROR_SUCCESS;
  1154. UpdateGetLastError(NULL); // Error Success
  1155. dwReturnCode = OpenPolicyStore(&hPolicyStorage);
  1156. if (dwReturnCode != ERROR_SUCCESS)
  1157. {
  1158. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_FAILED_POLSTORE_OPEN);
  1159. dwRet=ERROR_SUCCESS;
  1160. BAIL_OUT;
  1161. }
  1162. // enum all policies and delete one by one
  1163. dwReturnCode = IPSecEnumPolicyData(hPolicyStorage, &ppPolicyEnum, &dwNumPolicies);
  1164. if (dwReturnCode == ERROR_SUCCESS && dwNumPolicies > 0 && ppPolicyEnum != NULL)
  1165. {
  1166. for (i = 0; i < dwNumPolicies; i++)
  1167. {
  1168. dwReturnCode = IPSecCopyPolicyData(ppPolicyEnum[i], &pPolicy);
  1169. if (dwReturnCode != ERROR_SUCCESS)
  1170. {
  1171. dwRet = dwReturnCode;
  1172. BAIL_OUT;
  1173. }
  1174. dwReturnCode = IPSecEnumNFAData(hPolicyStorage, pPolicy->PolicyIdentifier
  1175. , &(pPolicy->ppIpsecNFAData), &(pPolicy->dwNumNFACount));
  1176. if (dwReturnCode == ERROR_SUCCESS)
  1177. {
  1178. dwReturnCode=IPSecGetISAKMPData(hPolicyStorage, pPolicy->ISAKMPIdentifier, &(pPolicy->pIpsecISAKMPData));
  1179. }
  1180. if((dwReturnCode == ERROR_SUCCESS)&&(!bAssigned))
  1181. {
  1182. if(g_StorageLocation.dwLocation!=IPSEC_DIRECTORY_PROVIDER)
  1183. {
  1184. dwReturnCode=IsAssigned(pPolicy,hPolicyStorage,bAssigned);
  1185. }
  1186. if(( dwReturnCode == ERROR_SUCCESS || dwReturnCode == ERROR_FILE_NOT_FOUND )&&bAssigned)
  1187. {
  1188. dwReturnCode=IPSecUnassignPolicy(hPolicyStorage, pPolicy->PolicyIdentifier);
  1189. }
  1190. }
  1191. dwReturnCode = DeletePolicy(pPolicy,hPolicyStorage,bCompleteDelete);
  1192. if (dwReturnCode != ERROR_SUCCESS)
  1193. {
  1194. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_POLICY_3,pPolicy->pszIpsecName);
  1195. }
  1196. if (pPolicy)
  1197. {
  1198. IPSecFreePolicyData(pPolicy);
  1199. }
  1200. }
  1201. }
  1202. if (dwNumPolicies > 0 && ppPolicyEnum != NULL)
  1203. {
  1204. IPSecFreeMulPolicyData(ppPolicyEnum, dwNumPolicies);
  1205. }
  1206. // delete the left out filterlists and filter action(Standalone)
  1207. dwReturnCode =DeleteStandAloneFL(hPolicyStorage);
  1208. if( dwReturnCode==ERROR_SUCCESS || dwReturnCode==ERROR_INVALID_DATA)
  1209. {
  1210. dwReturnCode =DeleteStandAloneFA(hPolicyStorage);
  1211. }
  1212. dwRet= ERROR_SUCCESS;
  1213. ClosePolicyStore(hPolicyStorage);
  1214. error:
  1215. return dwRet;
  1216. }
  1217. ///////////////////////////////////////////////////////////////////////////////
  1218. //
  1219. //Function: DeleteStandAloneFL()
  1220. //
  1221. //Date of Creation: 21st Aug 2001
  1222. //
  1223. //Parameters:
  1224. // IN HANDLE hPolicyStorage
  1225. //
  1226. //Return: DWORD
  1227. //
  1228. //Description:
  1229. // This function deletes all the stand alone filter lists
  1230. //
  1231. //Revision History:
  1232. //
  1233. //Date Author Comments
  1234. //
  1235. ///////////////////////////////////////////////////////////////////////////////
  1236. DWORD
  1237. DeleteStandAloneFL(
  1238. IN HANDLE hPolicyStorage
  1239. )
  1240. {
  1241. DWORD dwReturnCode=ERROR_SUCCESS;
  1242. PIPSEC_FILTER_DATA *ppFilterEnum = NULL,pFilter=NULL;
  1243. DWORD cnt=0,dwNumFilters=0;
  1244. dwReturnCode = IPSecEnumFilterData(hPolicyStorage, &ppFilterEnum, &dwNumFilters);
  1245. if (!(dwReturnCode == ERROR_SUCCESS && dwNumFilters > 0 && ppFilterEnum != NULL))
  1246. {
  1247. dwReturnCode=ERROR_SUCCESS;
  1248. BAIL_OUT;
  1249. }
  1250. // delete one by one
  1251. for(cnt=0; cnt < dwNumFilters;cnt++)
  1252. {
  1253. dwReturnCode = IPSecCopyFilterData(ppFilterEnum[cnt], &pFilter);
  1254. if ((dwReturnCode == ERROR_SUCCESS) && (pFilter != NULL))
  1255. {
  1256. dwReturnCode = DeleteFilterList(pFilter,hPolicyStorage);
  1257. if (dwReturnCode == ERROR_INVALID_DATA)
  1258. {
  1259. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_FILTERLIST_2,pFilter->pszIpsecName);
  1260. // if cannot be deleted , show where it is used
  1261. ShowWhereFLUsed(pFilter,hPolicyStorage);
  1262. }
  1263. else if (dwReturnCode != ERROR_SUCCESS)
  1264. {
  1265. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_FILTERLIST_ALL_FL_DEL_QUERY,pFilter->pszIpsecName);
  1266. }
  1267. }
  1268. }
  1269. if(ppFilterEnum && dwNumFilters>0)
  1270. {
  1271. IPSecFreeMulFilterData( ppFilterEnum,dwNumFilters);
  1272. }
  1273. if(pFilter)
  1274. {
  1275. IPSecFreeFilterData(pFilter);
  1276. }
  1277. error:
  1278. return dwReturnCode;
  1279. }
  1280. ///////////////////////////////////////////////////////////////////////////////
  1281. //
  1282. //Function: DeleteStandAloneFA()
  1283. //
  1284. //Date of Creation: 21st Aug 2001
  1285. //
  1286. //Parameters:
  1287. // IN HANDLE hPolicyStorage
  1288. //
  1289. //Return: DWORD
  1290. //
  1291. //Description:
  1292. // This function deletes all the stand alone filter actions
  1293. //
  1294. //Revision History:
  1295. //
  1296. //Date Author Comments
  1297. //
  1298. ///////////////////////////////////////////////////////////////////////////////
  1299. DWORD
  1300. DeleteStandAloneFA(
  1301. IN HANDLE hPolicyStorage
  1302. )
  1303. {
  1304. DWORD dwReturnCode=ERROR_SUCCESS;
  1305. PIPSEC_NEGPOL_DATA *ppNegPolEnum = NULL,pNegPol=NULL;
  1306. DWORD cnt=0,dwNumNegPolicies=0;
  1307. dwReturnCode = IPSecEnumNegPolData(hPolicyStorage, &ppNegPolEnum, &dwNumNegPolicies);
  1308. if (!(dwReturnCode == ERROR_SUCCESS && dwNumNegPolicies > 0 && ppNegPolEnum != NULL))
  1309. {
  1310. dwReturnCode = ERROR_SUCCESS;
  1311. BAIL_OUT;
  1312. }
  1313. for(cnt=0; cnt < dwNumNegPolicies;cnt++)
  1314. {
  1315. // ignore the default filteraction
  1316. if(IsEqualGUID(ppNegPolEnum[cnt]->NegPolType,GUID_NEGOTIATION_TYPE_DEFAULT)||(!ppNegPolEnum[cnt]->pszIpsecName))
  1317. {
  1318. DeleteFilterAction(ppNegPolEnum[cnt],hPolicyStorage);
  1319. continue;
  1320. }
  1321. dwReturnCode = IPSecCopyNegPolData(ppNegPolEnum[cnt], &pNegPol);
  1322. if ((dwReturnCode == ERROR_SUCCESS) && (pNegPol != NULL))
  1323. {
  1324. dwReturnCode = DeleteFilterAction(pNegPol,hPolicyStorage);
  1325. if (dwReturnCode == ERROR_INVALID_DATA)
  1326. {
  1327. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_FILTERACTION_2,pNegPol->pszIpsecName);
  1328. //if delete failed, show where it is used
  1329. ShowWhereFAUsed(pNegPol,hPolicyStorage);
  1330. }
  1331. else if (dwReturnCode != ERROR_SUCCESS)
  1332. {
  1333. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_DEL_STATIC_FILTERACTION_ALL_FA_DEL_QUERY,pNegPol->pszIpsecName);
  1334. }
  1335. }
  1336. }
  1337. if (dwNumNegPolicies > 0 && ppNegPolEnum != NULL)
  1338. {
  1339. IPSecFreeMulNegPolData( ppNegPolEnum,dwNumNegPolicies);
  1340. }
  1341. if(pNegPol)
  1342. {
  1343. IPSecFreeNegPolData(pNegPol);
  1344. }
  1345. error:
  1346. return dwReturnCode;
  1347. }
  1348. ///////////////////////////////////////////////////////////////////////////////
  1349. //
  1350. //Function: DeletePolicy()
  1351. //
  1352. //Date of Creation: 21st Aug 2001
  1353. //
  1354. //Parameters:
  1355. // IN PIPSEC_POLICY_DATA pPolicy,
  1356. // IN HANDLE hStore,
  1357. // IN BOOL bCompleteDelete
  1358. //
  1359. //Return: DWORD
  1360. //
  1361. //Description:
  1362. // Deletes a specific policy from the policy store
  1363. //
  1364. //Revision History:
  1365. //
  1366. //Date Author Comments
  1367. //
  1368. ///////////////////////////////////////////////////////////////////////////////
  1369. DWORD
  1370. DeletePolicy(
  1371. IN PIPSEC_POLICY_DATA pPolicy,
  1372. IN HANDLE hStore,
  1373. IN BOOL bCompleteDelete
  1374. )
  1375. {
  1376. DWORD dwReturnCode = ERROR_SUCCESS;
  1377. DWORD i=0;
  1378. RPC_STATUS RpcStat=RPC_S_OK;
  1379. GUID guidISAKMP = pPolicy->ISAKMPIdentifier,guidNegPol,guidFilter;
  1380. //check for read only flag
  1381. if(pPolicy->dwFlags & POLSTORE_READONLY )
  1382. {
  1383. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SET_POL_READ_ONLY_OBJECT,pPolicy->pszIpsecName);
  1384. BAIL_OUT;
  1385. }
  1386. //delete sequentially the policy data structure
  1387. for (i = 0; i < pPolicy->dwNumNFACount && ((dwReturnCode == ERROR_SUCCESS)||(dwReturnCode==ERROR_INVALID_DATA)); i++)
  1388. {
  1389. if(bCompleteDelete)
  1390. {
  1391. guidNegPol=pPolicy->ppIpsecNFAData[i]->NegPolIdentifier;
  1392. guidFilter=pPolicy->ppIpsecNFAData[i]->FilterIdentifier;
  1393. }
  1394. dwReturnCode = IPSecDeleteNFAData(hStore, pPolicy->PolicyIdentifier, pPolicy->ppIpsecNFAData[i]);
  1395. if(bCompleteDelete) // if the user asked, delete the filterlist and filteractions also
  1396. {
  1397. if ((dwReturnCode == ERROR_SUCCESS)||(dwReturnCode==ERROR_INVALID_DATA))
  1398. {
  1399. if (!UuidIsNil(&guidFilter, &RpcStat))
  1400. {
  1401. dwReturnCode = DeleteFilterData(hStore, guidFilter);
  1402. }
  1403. }
  1404. if ((dwReturnCode == ERROR_SUCCESS)||(dwReturnCode==ERROR_INVALID_DATA))
  1405. {
  1406. if (!UuidIsNil(&guidNegPol, &RpcStat))
  1407. {
  1408. dwReturnCode = DeleteNegPolData(hStore, guidNegPol);
  1409. }
  1410. }
  1411. }
  1412. }
  1413. if ((dwReturnCode == ERROR_SUCCESS)||(dwReturnCode==ERROR_INVALID_DATA))
  1414. {
  1415. dwReturnCode = DeletePolicyData(hStore, pPolicy);
  1416. }
  1417. if (dwReturnCode == ERROR_SUCCESS)
  1418. {
  1419. dwReturnCode = IPSecDeleteISAKMPData(hStore, guidISAKMP);
  1420. }
  1421. error:
  1422. return dwReturnCode;
  1423. }
  1424. ////////////////////////////////////////////////////////////////////////////////
  1425. //
  1426. //Function: DeleteRule()
  1427. //
  1428. //Date of Creation: 21st Aug 2001
  1429. //
  1430. //Parameters:
  1431. // IN PIPSEC_POLICY_DATA pPolicy,
  1432. // IN PIPSEC_NFA_DATA pIpsecNFAData,
  1433. // IN HANDLE hStore,
  1434. // IN BOOL bCompleteDelete
  1435. //
  1436. //Return: DWORD
  1437. //
  1438. //Description:
  1439. // Deletes a specific rule from the policy specified.
  1440. // Note : It will deny to delete the Default response rule.
  1441. //
  1442. //Revision History:
  1443. //
  1444. //Date Author Comments
  1445. //
  1446. ///////////////////////////////////////////////////////////////////////////////
  1447. DWORD
  1448. DeleteRule(
  1449. IN PIPSEC_POLICY_DATA pPolicy,
  1450. IN PIPSEC_NFA_DATA pIpsecNFAData,
  1451. IN HANDLE hStore,
  1452. IN BOOL bCompleteDelete
  1453. )
  1454. {
  1455. DWORD dwReturnCode = ERROR_SUCCESS;
  1456. RPC_STATUS RpcStat=RPC_S_OK;
  1457. GUID guidNegPol,guidFilter;
  1458. //check for readonly flag
  1459. if(pPolicy->dwFlags & POLSTORE_READONLY )
  1460. {
  1461. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SET_POL_READ_ONLY_OBJECT,pPolicy->pszIpsecName);
  1462. BAIL_OUT;
  1463. }
  1464. // delete sequentially the NFA data structure
  1465. if(bCompleteDelete)
  1466. {
  1467. guidNegPol=pIpsecNFAData->NegPolIdentifier;
  1468. guidFilter=pIpsecNFAData->FilterIdentifier;
  1469. }
  1470. dwReturnCode = IPSecDeleteNFAData(hStore, pPolicy->PolicyIdentifier, pIpsecNFAData);
  1471. if(bCompleteDelete) // if the user asked, delete the filterlist and filteractions also
  1472. {
  1473. if (dwReturnCode == ERROR_SUCCESS)
  1474. {
  1475. if (!UuidIsNil(&guidFilter, &RpcStat))
  1476. {
  1477. dwReturnCode = DeleteFilterData(hStore, guidFilter);
  1478. }
  1479. }
  1480. if ((dwReturnCode == ERROR_SUCCESS)||(dwReturnCode==ERROR_INVALID_DATA))
  1481. {
  1482. if (!UuidIsNil(&guidNegPol, &RpcStat))
  1483. {
  1484. dwReturnCode = DeleteNegPolData(hStore, guidNegPol);
  1485. }
  1486. }
  1487. }
  1488. if(g_NshPolStoreHandle.GetBatchmodeStatus())
  1489. {
  1490. g_NshPolNegFilData.DeletePolicyFromCache(pPolicy);
  1491. }
  1492. if (dwReturnCode==ERROR_INVALID_DATA)
  1493. {
  1494. dwReturnCode = ERROR_SUCCESS;
  1495. }
  1496. error:
  1497. return dwReturnCode;
  1498. }
  1499. ///////////////////////////////////////////////////////////////////////////////
  1500. //
  1501. //Function: DeleteFilterAction()
  1502. //
  1503. //Date of Creation: 21st Aug 2001
  1504. //
  1505. //Parameters:
  1506. // IN PIPSEC_NEGPOL_DATA pNegPolData,
  1507. // IN HANDLE hStore
  1508. //
  1509. //Return: DWORD
  1510. //
  1511. //Description:
  1512. // Deletes a specific Filter Action from the policy store
  1513. // Note : It will object to delete the FA attached to some policy/Rule.
  1514. // And it displays the policies/rules which are using it.
  1515. //
  1516. //Revision History:
  1517. //
  1518. //Date Author Comments
  1519. //
  1520. ///////////////////////////////////////////////////////////////////////////////
  1521. DWORD
  1522. DeleteFilterAction(
  1523. IN PIPSEC_NEGPOL_DATA pNegPolData,
  1524. IN HANDLE hStore
  1525. )
  1526. {
  1527. DWORD dwReturnCode = ERROR_SUCCESS;
  1528. RPC_STATUS RpcStat=RPC_S_OK;
  1529. if (!UuidIsNil(&(pNegPolData->NegPolIdentifier), &RpcStat))
  1530. {
  1531. dwReturnCode = DeleteNegPolData(hStore, pNegPolData->NegPolIdentifier);
  1532. }
  1533. return dwReturnCode;
  1534. }
  1535. ///////////////////////////////////////////////////////////////////////////////
  1536. //
  1537. //Function: DeleteFilterList()
  1538. //
  1539. //Date of Creation: 21st Aug 2001
  1540. //
  1541. //Parameters:
  1542. // IN PIPSEC_FILTER_DATA pFilterData,
  1543. // IN HANDLE hStore
  1544. //
  1545. //Return: DWORD
  1546. //
  1547. //Description:
  1548. // Deletes a specific Filter List from the policy store
  1549. // Note : It will object to delete the FL attached to some policy/Rule.
  1550. // And it displays the policies/rules which are using it.
  1551. //
  1552. //Revision History:
  1553. //
  1554. //Date Author Comments
  1555. //
  1556. ///////////////////////////////////////////////////////////////////////////////
  1557. DWORD
  1558. DeleteFilterList(
  1559. IN PIPSEC_FILTER_DATA pFilterData,
  1560. IN HANDLE hStore
  1561. )
  1562. {
  1563. DWORD dwReturnCode = ERROR_SUCCESS;
  1564. RPC_STATUS RpcStat=RPC_S_OK;
  1565. if(pFilterData->dwFlags & POLSTORE_READONLY )
  1566. {
  1567. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SET_FL_READ_ONLY_OBJECT,pFilterData->pszIpsecName);
  1568. BAIL_OUT;
  1569. }
  1570. if (!UuidIsNil(&(pFilterData->FilterIdentifier), &RpcStat))
  1571. {
  1572. dwReturnCode = DeleteFilterData(hStore, pFilterData->FilterIdentifier);
  1573. }
  1574. error:
  1575. return dwReturnCode;
  1576. }
  1577. ///////////////////////////////////////////////////////////////////////////////
  1578. //
  1579. //Function: ShowWhereFLUsed()
  1580. //
  1581. //Date of Creation: 21st Aug 2001
  1582. //
  1583. //Parameters:
  1584. // IN PIPSEC_FILTER_DATA pIpsecFilterData,
  1585. // IN HANDLE hPolicyStorage
  1586. //
  1587. //Return: DWORD
  1588. //
  1589. //Description:
  1590. // This function displays the policies/rules which are using a specific Filter list.
  1591. //
  1592. //Revision History:
  1593. //
  1594. //Date Author Comments
  1595. //
  1596. ///////////////////////////////////////////////////////////////////////////////
  1597. VOID
  1598. ShowWhereFLUsed(
  1599. IN PIPSEC_FILTER_DATA pIpsecFilterData,
  1600. IN HANDLE hPolicyStorage
  1601. )
  1602. {
  1603. PIPSEC_POLICY_DATA *ppPolicyEnum = NULL,pPolicy=NULL;
  1604. BOOL bPrinted=FALSE;
  1605. DWORD dwNumPolicies = 0 , i=0;
  1606. DWORD dwReturnCode = ERROR_SUCCESS;
  1607. PrintMessageFromModule(g_hModule,DEL_STATIC_SHWRUSED_TITLE);
  1608. PrintMessageFromModule(g_hModule,DEL_STATIC_SHWRUSED_UNDERLINE);
  1609. dwReturnCode = IPSecEnumPolicyData(hPolicyStorage, &ppPolicyEnum, &dwNumPolicies);
  1610. if (!(dwReturnCode == ERROR_SUCCESS && dwNumPolicies > 0 && ppPolicyEnum != NULL))
  1611. {
  1612. BAIL_OUT;
  1613. }
  1614. for (i = 0; i < dwNumPolicies; i++)
  1615. {
  1616. bPrinted=FALSE;
  1617. dwReturnCode = IPSecCopyPolicyData(ppPolicyEnum[i], &pPolicy);
  1618. BAIL_ON_WIN32_ERROR(dwReturnCode);
  1619. dwReturnCode = IPSecEnumNFAData(hPolicyStorage, pPolicy->PolicyIdentifier
  1620. , &(pPolicy->ppIpsecNFAData), &(pPolicy->dwNumNFACount));
  1621. for (DWORD n = 0; n < pPolicy->dwNumNFACount && (dwReturnCode == ERROR_SUCCESS); n++)
  1622. {
  1623. //check where this filterlist is used
  1624. if(IsEqualGUID(pPolicy->ppIpsecNFAData[n]->FilterIdentifier,pIpsecFilterData->FilterIdentifier))
  1625. {
  1626. if ((!bPrinted)&&(pPolicy->pszIpsecName))
  1627. {
  1628. PrintMessageFromModule(g_hModule,SHW_STATIC_FILTERLIST_POL_NAME,pPolicy->pszIpsecName);
  1629. bPrinted=TRUE;
  1630. }
  1631. if (pPolicy->ppIpsecNFAData[n]->pszIpsecName)
  1632. {
  1633. PrintMessageFromModule(g_hModule,DEL_STATIC_SHWRUSED_RULE_NAME_STR,pPolicy->ppIpsecNFAData[n]->pszIpsecName );
  1634. }
  1635. else
  1636. {
  1637. PrintMessageFromModule(g_hModule,DEL_STATIC_SHWRUSED_RULE_NAME_NONE);
  1638. }
  1639. }
  1640. }
  1641. if (pPolicy)
  1642. {
  1643. IPSecFreePolicyData(pPolicy);
  1644. }
  1645. }
  1646. if (dwNumPolicies > 0 && ppPolicyEnum != NULL)
  1647. {
  1648. IPSecFreeMulPolicyData(ppPolicyEnum, dwNumPolicies);
  1649. }
  1650. error:
  1651. return;
  1652. }
  1653. ///////////////////////////////////////////////////////////////////////////////
  1654. //
  1655. //Function: ShowWhereFAUsed()
  1656. //
  1657. //Date of Creation: 21st Aug 2001
  1658. //
  1659. //Parameters:
  1660. // IN PIPSEC_NEGPOL_DATA pIpsecNegPolData,
  1661. // IN HANDLE hPolicyStorage
  1662. //
  1663. //Return: DWORD
  1664. //
  1665. //Description:
  1666. // This function displays the policies/rules which are using a specific Filter Action.
  1667. //
  1668. //Revision History:
  1669. //
  1670. //Date Author Comments
  1671. //
  1672. ///////////////////////////////////////////////////////////////////////////////
  1673. VOID
  1674. ShowWhereFAUsed(
  1675. IN PIPSEC_NEGPOL_DATA pIpsecNegPolData,
  1676. IN HANDLE hPolicyStorage
  1677. )
  1678. {
  1679. PIPSEC_POLICY_DATA *ppPolicyEnum = NULL,pPolicy=NULL;
  1680. BOOL bPrinted=FALSE;
  1681. DWORD dwNumPolicies = 0 , i = 0;
  1682. DWORD dwReturnCode = ERROR_SUCCESS;
  1683. PrintMessageFromModule(g_hModule,DEL_STATIC_SHWRUSED_TITLE);
  1684. PrintMessageFromModule(g_hModule,DEL_STATIC_SHWRUSED_UNDERLINE);
  1685. dwReturnCode = IPSecEnumPolicyData(hPolicyStorage, &ppPolicyEnum, &dwNumPolicies);
  1686. if (!(dwReturnCode == ERROR_SUCCESS && dwNumPolicies > 0 && ppPolicyEnum != NULL))
  1687. {
  1688. BAIL_OUT;
  1689. }
  1690. for (i = 0; i < dwNumPolicies; i++)
  1691. {
  1692. bPrinted=FALSE;
  1693. dwReturnCode = IPSecCopyPolicyData(ppPolicyEnum[i], &pPolicy);
  1694. if (dwReturnCode != ERROR_SUCCESS)
  1695. {
  1696. BAIL_OUT;
  1697. }
  1698. dwReturnCode = IPSecEnumNFAData(hPolicyStorage, pPolicy->PolicyIdentifier
  1699. , &(pPolicy->ppIpsecNFAData), &(pPolicy->dwNumNFACount));
  1700. for (DWORD n = 0; n < pPolicy->dwNumNFACount && (dwReturnCode == ERROR_SUCCESS); n++)
  1701. {
  1702. //check where this filteraction is used
  1703. if(IsEqualGUID(pPolicy->ppIpsecNFAData[n]->NegPolIdentifier,pIpsecNegPolData->NegPolIdentifier))
  1704. {
  1705. if (!bPrinted)
  1706. {
  1707. PrintMessageFromModule(g_hModule,SHW_STATIC_FILTERLIST_POL_NAME,pPolicy->pszIpsecName);
  1708. bPrinted=TRUE;
  1709. }
  1710. if (pPolicy->ppIpsecNFAData[n]->pszIpsecName)
  1711. {
  1712. PrintMessageFromModule(g_hModule,DEL_STATIC_SHWRUSED_RULE_NAME_STR,pPolicy->ppIpsecNFAData[n]->pszIpsecName );
  1713. }
  1714. else
  1715. {
  1716. PrintMessageFromModule(g_hModule,DEL_STATIC_SHWRUSED_RULE_NAME_NONE);
  1717. }
  1718. }
  1719. }
  1720. if (pPolicy)
  1721. {
  1722. IPSecFreePolicyData(pPolicy);
  1723. }
  1724. }
  1725. if (dwNumPolicies > 0 && ppPolicyEnum != NULL)
  1726. {
  1727. IPSecFreeMulPolicyData(ppPolicyEnum, dwNumPolicies);
  1728. }
  1729. error :
  1730. return;
  1731. }
  1732. ///////////////////////////////////////////////////////////////////////////////
  1733. //
  1734. //Function: FillDelFilterInfo( )
  1735. //
  1736. //Date of Creation: 21st Aug 2001
  1737. //
  1738. //Parameters:
  1739. // OUT PDELFILTERDATA* ppFilter,
  1740. // IN PARSER_PKT & parser,
  1741. // IN const TOKEN_VALUE *vtokStaticDeleteFilter
  1742. //
  1743. //Return: DWORD
  1744. //
  1745. //Description:
  1746. // This function fills the local structure with the information got from the parser.
  1747. //
  1748. //Revision History:
  1749. //
  1750. //Date Author Comments
  1751. //
  1752. ///////////////////////////////////////////////////////////////////////////////
  1753. DWORD
  1754. FillDelFilterInfo(
  1755. OUT PDELFILTERDATA* ppFilter,
  1756. IN PARSER_PKT & parser,
  1757. IN const TOKEN_VALUE *vtokStaticDeleteFilter
  1758. )
  1759. {
  1760. DWORD dwCount=0,dwReturn=ERROR_SUCCESS , dwStrLength = 0;
  1761. PDELFILTERDATA pFilter=new DELFILTERDATA;
  1762. if(pFilter==NULL)
  1763. {
  1764. dwReturn = ERROR_OUTOFMEMORY;
  1765. BAIL_OUT;
  1766. }
  1767. memset(pFilter,0,sizeof(DELFILTERDATA));
  1768. pFilter->bMirrored=TRUE;
  1769. pFilter->SourMask = MASK_ME;
  1770. pFilter->DestMask = MASK_ME;
  1771. for(dwCount=0;dwCount < parser.MaxTok;dwCount++)
  1772. {
  1773. switch(vtokStaticDeleteFilter[parser.Cmd[dwCount].dwCmdToken].dwValue)
  1774. {
  1775. case CMD_TOKEN_FILTERLIST :
  1776. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1777. {
  1778. dwStrLength = _tcslen((LPTSTR )parser.Cmd[dwCount].pArg);
  1779. pFilter->pszFLName=new _TCHAR[dwStrLength+1];
  1780. if(pFilter->pszFLName==NULL)
  1781. {
  1782. dwReturn = ERROR_OUTOFMEMORY;
  1783. BAIL_OUT;
  1784. }
  1785. _tcsncpy(pFilter->pszFLName,(LPTSTR)parser.Cmd[dwCount].pArg,dwStrLength+1);
  1786. }
  1787. break;
  1788. case CMD_TOKEN_SRCADDR :
  1789. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1790. {
  1791. if(((DNSIPADDR *)parser.Cmd[dwCount].pArg)->pszDomainName)
  1792. {
  1793. dwStrLength = _tcslen(((DNSIPADDR *)parser.Cmd[dwCount].pArg)->pszDomainName);
  1794. pFilter->SourceAddr.pszDomainName = new _TCHAR[dwStrLength+1];
  1795. if(pFilter->SourceAddr.pszDomainName==NULL)
  1796. {
  1797. dwReturn = ERROR_OUTOFMEMORY;
  1798. BAIL_OUT;
  1799. }
  1800. _tcsncpy(pFilter->SourceAddr.pszDomainName,((DNSIPADDR *)parser.Cmd[dwCount].pArg)->pszDomainName,dwStrLength+1);
  1801. }
  1802. else
  1803. {
  1804. pFilter->SourceAddr.puIpAddr = new ULONG;
  1805. if(pFilter->SourceAddr.puIpAddr==NULL)
  1806. {
  1807. dwReturn = ERROR_OUTOFMEMORY;
  1808. BAIL_OUT;
  1809. }
  1810. memcpy( &(pFilter->SourceAddr.puIpAddr[0]),(&((DNSIPADDR *)parser.Cmd[dwCount].pArg)->puIpAddr[0]),sizeof(ULONG));
  1811. }
  1812. }
  1813. else // deal if special server specified
  1814. {
  1815. if(parser.Cmd[dwCount].dwStatus == SERVER_DNS)
  1816. {
  1817. pFilter->bSrcServerSpecified=TRUE;
  1818. pFilter->ExType=EXT_DNS_SERVER;
  1819. }
  1820. else if(parser.Cmd[dwCount].dwStatus == SERVER_WINS)
  1821. {
  1822. pFilter->bSrcServerSpecified=TRUE;
  1823. pFilter->ExType=EXT_WINS_SERVER;
  1824. }
  1825. else if(parser.Cmd[dwCount].dwStatus == SERVER_DHCP)
  1826. {
  1827. pFilter->bSrcServerSpecified=TRUE;
  1828. pFilter->ExType=EXT_DHCP_SERVER;
  1829. }
  1830. else if(parser.Cmd[dwCount].dwStatus == SERVER_GATEWAY)
  1831. {
  1832. pFilter->bSrcServerSpecified=TRUE;
  1833. pFilter->ExType=EXT_DEFAULT_GATEWAY;
  1834. }
  1835. else if (parser.Cmd[dwCount].dwStatus == IP_ME)
  1836. {
  1837. pFilter->bSrcMeSpecified=TRUE;
  1838. }
  1839. else if (parser.Cmd[dwCount].dwStatus == IP_ANY)
  1840. {
  1841. pFilter->bSrcAnySpecified=TRUE;
  1842. }
  1843. }
  1844. break;
  1845. case CMD_TOKEN_DSTADDR :
  1846. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1847. {
  1848. if(((DNSIPADDR *)parser.Cmd[dwCount].pArg)->pszDomainName)
  1849. {
  1850. dwStrLength = _tcslen(((DNSIPADDR *)parser.Cmd[dwCount].pArg)->pszDomainName);
  1851. pFilter->DestnAddr.pszDomainName = new _TCHAR[dwStrLength+1];
  1852. if(pFilter->DestnAddr.pszDomainName==NULL)
  1853. {
  1854. dwReturn = ERROR_OUTOFMEMORY;
  1855. BAIL_OUT;
  1856. }
  1857. _tcsncpy(pFilter->DestnAddr.pszDomainName,((DNSIPADDR *)parser.Cmd[dwCount].pArg)->pszDomainName,dwStrLength+1);
  1858. }
  1859. else
  1860. {
  1861. pFilter->DestnAddr.puIpAddr = new ULONG;
  1862. if(pFilter->DestnAddr.puIpAddr==NULL)
  1863. {
  1864. dwReturn = ERROR_OUTOFMEMORY;
  1865. BAIL_OUT;
  1866. }
  1867. memcpy( &(pFilter->DestnAddr.puIpAddr[0]),(&((DNSIPADDR *)parser.Cmd[dwCount].pArg)->puIpAddr[0]),sizeof(ULONG));
  1868. }
  1869. }
  1870. else // deal if special server specified
  1871. {
  1872. if(parser.Cmd[dwCount].dwStatus == SERVER_DNS)
  1873. {
  1874. pFilter->bDstServerSpecified=TRUE;
  1875. pFilter->ExType=EXT_DNS_SERVER | EXT_DEST;
  1876. }
  1877. else if(parser.Cmd[dwCount].dwStatus == SERVER_WINS)
  1878. {
  1879. pFilter->bDstServerSpecified=TRUE;
  1880. pFilter->ExType=EXT_WINS_SERVER | EXT_DEST;
  1881. }
  1882. else if(parser.Cmd[dwCount].dwStatus == SERVER_DHCP)
  1883. {
  1884. pFilter->bDstServerSpecified=TRUE;
  1885. pFilter->ExType=EXT_DHCP_SERVER | EXT_DEST;
  1886. }
  1887. else if(parser.Cmd[dwCount].dwStatus == SERVER_GATEWAY)
  1888. {
  1889. pFilter->bDstServerSpecified=TRUE;
  1890. pFilter->ExType=EXT_DEFAULT_GATEWAY | EXT_DEST;
  1891. }
  1892. else if (parser.Cmd[dwCount].dwStatus == IP_ME)
  1893. {
  1894. pFilter->bDstMeSpecified=TRUE;
  1895. }
  1896. else if (parser.Cmd[dwCount].dwStatus == IP_ANY)
  1897. {
  1898. pFilter->bDstAnySpecified=TRUE;
  1899. }
  1900. }
  1901. break;
  1902. case CMD_TOKEN_SRCMASK :
  1903. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1904. {
  1905. pFilter->SourMask= *(DWORD *)parser.Cmd[dwCount].pArg;
  1906. pFilter->bSrcMaskSpecified=TRUE;
  1907. }
  1908. break;
  1909. case CMD_TOKEN_DSTMASK :
  1910. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1911. {
  1912. pFilter->DestMask= *(DWORD *)parser.Cmd[dwCount].pArg;
  1913. pFilter->bDstMaskSpecified=TRUE;
  1914. }
  1915. break;
  1916. case CMD_TOKEN_MIRROR :
  1917. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1918. {
  1919. pFilter->bMirrored= *(BOOL *)parser.Cmd[dwCount].pArg;
  1920. pFilter->bMirrorSpecified=TRUE;
  1921. }
  1922. break;
  1923. case CMD_TOKEN_PROTO :
  1924. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1925. {
  1926. pFilter->dwProtocol= *(DWORD *)parser.Cmd[dwCount].pArg;
  1927. pFilter->bProtocolSpecified=TRUE;
  1928. }
  1929. break;
  1930. case CMD_TOKEN_SRCPORT :
  1931. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1932. {
  1933. pFilter->SourPort= *(WORD *)parser.Cmd[dwCount].pArg;
  1934. pFilter->bSrcPortSpecified=TRUE;
  1935. }
  1936. break;
  1937. case CMD_TOKEN_DSTPORT :
  1938. if ((parser.Cmd[dwCount].dwStatus == VALID_TOKEN)&&(parser.Cmd[dwCount].pArg))
  1939. {
  1940. pFilter->DestPort= *(WORD *)parser.Cmd[dwCount].pArg;
  1941. pFilter->bDstPortSpecified=TRUE;
  1942. }
  1943. break;
  1944. default:
  1945. break;
  1946. }
  1947. }
  1948. // deal me and any cases
  1949. if(pFilter->bSrcMeSpecified)
  1950. {
  1951. pFilter->SourceAddr.dwNumIpAddresses = 1;
  1952. pFilter->SourceAddr.puIpAddr= new ULONG[pFilter->SourceAddr.dwNumIpAddresses];
  1953. if(pFilter->SourceAddr.puIpAddr==NULL)
  1954. {
  1955. dwReturn = ERROR_OUTOFMEMORY;
  1956. BAIL_OUT;
  1957. }
  1958. *(pFilter->SourceAddr.puIpAddr)=ADDR_ME;
  1959. pFilter->SourMask = MASK_ME;
  1960. if (pFilter->bDstMeSpecified)
  1961. {
  1962. dwReturn = ERROR_INVALID_PARAMETER;
  1963. BAIL_OUT;
  1964. }
  1965. }
  1966. else if(pFilter->bSrcAnySpecified)
  1967. {
  1968. pFilter->SourceAddr.dwNumIpAddresses = 1;
  1969. pFilter->SourceAddr.puIpAddr= new ULONG[pFilter->SourceAddr.dwNumIpAddresses];
  1970. if(pFilter->SourceAddr.puIpAddr==NULL)
  1971. {
  1972. dwReturn = ERROR_OUTOFMEMORY;
  1973. BAIL_OUT;
  1974. }
  1975. *(pFilter->SourceAddr.puIpAddr)=ADDR_ME;
  1976. pFilter->SourMask = ADDR_ME;
  1977. }
  1978. if(pFilter->bDstMeSpecified)
  1979. {
  1980. pFilter->DestnAddr.dwNumIpAddresses = 1;
  1981. pFilter->DestnAddr.puIpAddr= new ULONG[pFilter->DestnAddr.dwNumIpAddresses];
  1982. if(pFilter->DestnAddr.puIpAddr==NULL)
  1983. {
  1984. dwReturn = ERROR_OUTOFMEMORY;
  1985. BAIL_OUT;
  1986. }
  1987. *(pFilter->DestnAddr.puIpAddr)=ADDR_ME;
  1988. pFilter->DestMask = MASK_ME;
  1989. }
  1990. else if(pFilter->bDstAnySpecified)
  1991. {
  1992. pFilter->DestnAddr.dwNumIpAddresses = 1;
  1993. pFilter->DestnAddr.puIpAddr= new ULONG[pFilter->DestnAddr.dwNumIpAddresses];
  1994. if(pFilter->DestnAddr.puIpAddr==NULL)
  1995. {
  1996. dwReturn = ERROR_OUTOFMEMORY;
  1997. BAIL_OUT;
  1998. }
  1999. *(pFilter->DestnAddr.puIpAddr)=ADDR_ME;
  2000. pFilter->DestMask = ADDR_ME;
  2001. }
  2002. if (((pFilter->dwProtocol != PROT_ID_TCP) && (pFilter->dwProtocol != PROT_ID_UDP)) &&
  2003. ((pFilter->SourPort != 0) || (pFilter->DestPort != 0)))
  2004. {
  2005. dwReturn = ERROR_INVALID_PARAMETER;
  2006. BAIL_OUT;
  2007. }
  2008. if(pFilter->bSrcServerSpecified || pFilter->bDstServerSpecified)
  2009. {
  2010. //deal the special server cases. ignore irrelevant input parameters
  2011. if(pFilter->SourceAddr.pszDomainName)
  2012. {
  2013. delete [] pFilter->SourceAddr.pszDomainName;
  2014. pFilter->SourceAddr.pszDomainName=NULL;
  2015. }
  2016. if(pFilter->DestnAddr.pszDomainName)
  2017. {
  2018. delete [] pFilter->DestnAddr.pszDomainName;
  2019. pFilter->DestnAddr.pszDomainName=NULL;
  2020. }
  2021. if(pFilter->SourceAddr.puIpAddr)
  2022. {
  2023. delete [] pFilter->SourceAddr.puIpAddr;
  2024. }
  2025. if(pFilter->DestnAddr.puIpAddr)
  2026. {
  2027. delete [] pFilter->DestnAddr.puIpAddr;
  2028. }
  2029. pFilter->SourceAddr.dwNumIpAddresses = DEF_NUMBER_OF_ADDR;
  2030. pFilter->SourceAddr.puIpAddr= new ULONG[DEF_NUMBER_OF_ADDR];
  2031. if(pFilter->SourceAddr.puIpAddr==NULL)
  2032. {
  2033. dwReturn = ERROR_OUTOFMEMORY;
  2034. BAIL_OUT;
  2035. }
  2036. memcpy( &(pFilter->SourceAddr.puIpAddr[0]),&ADDR_ME ,sizeof(ULONG));
  2037. pFilter->DestnAddr.dwNumIpAddresses = DEF_NUMBER_OF_ADDR;
  2038. pFilter->DestnAddr.puIpAddr= new ULONG[DEF_NUMBER_OF_ADDR];
  2039. if(pFilter->DestnAddr.puIpAddr==NULL)
  2040. {
  2041. dwReturn = ERROR_OUTOFMEMORY;
  2042. BAIL_OUT;
  2043. }
  2044. memcpy( &(pFilter->DestnAddr.puIpAddr[0]),&ADDR_ME ,sizeof(ULONG));
  2045. pFilter->SourMask = MASK_ME;
  2046. pFilter->DestMask = MASK_ME;
  2047. }
  2048. error:
  2049. *ppFilter=pFilter;
  2050. CleanUp();
  2051. return dwReturn;
  2052. }