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.

766 lines
18 KiB

  1. #include "precomp.h"
  2. extern LPWSTR PolicyDNAttributes[];
  3. DWORD
  4. ValidateISAKMPData(
  5. PIPSEC_ISAKMP_DATA pIpsecISAKMPData
  6. )
  7. {
  8. DWORD dwError = 0;
  9. if (!pIpsecISAKMPData) {
  10. dwError = ERROR_INVALID_PARAMETER;
  11. BAIL_ON_WIN32_ERROR(dwError);
  12. }
  13. if (!(pIpsecISAKMPData->pSecurityMethods) ||
  14. !(pIpsecISAKMPData->dwNumISAKMPSecurityMethods)) {
  15. dwError = ERROR_INVALID_PARAMETER;
  16. BAIL_ON_WIN32_ERROR(dwError);
  17. }
  18. error:
  19. return (dwError);
  20. }
  21. DWORD
  22. ValidateNegPolData(
  23. PIPSEC_NEGPOL_DATA pIpsecNegPolData
  24. )
  25. {
  26. DWORD dwError = 0;
  27. if (!pIpsecNegPolData) {
  28. dwError = ERROR_INVALID_PARAMETER;
  29. BAIL_ON_WIN32_ERROR(dwError);
  30. }
  31. if (!IsClearOnly(pIpsecNegPolData->NegPolAction) &&
  32. !IsBlocking(pIpsecNegPolData->NegPolAction)) {
  33. if (!(pIpsecNegPolData->pIpsecSecurityMethods) ||
  34. !(pIpsecNegPolData->dwSecurityMethodCount)) {
  35. dwError = ERROR_INVALID_PARAMETER;
  36. BAIL_ON_WIN32_ERROR(dwError);
  37. }
  38. }
  39. error:
  40. return (dwError);
  41. }
  42. BOOL
  43. IsClearOnly(
  44. GUID gNegPolAction
  45. )
  46. {
  47. if (!memcmp(
  48. &gNegPolAction,
  49. &(GUID_NEGOTIATION_ACTION_NO_IPSEC),
  50. sizeof(GUID))) {
  51. return (TRUE);
  52. }
  53. else {
  54. return (FALSE);
  55. }
  56. }
  57. BOOL
  58. IsBlocking(
  59. GUID gNegPolAction
  60. )
  61. {
  62. if (!memcmp(
  63. &gNegPolAction,
  64. &(GUID_NEGOTIATION_ACTION_BLOCK),
  65. sizeof(GUID))) {
  66. return (TRUE);
  67. }
  68. else {
  69. return (FALSE);
  70. }
  71. }
  72. DWORD
  73. ValidateISAKMPDataDeletion(
  74. HANDLE hPolicyStore,
  75. GUID ISAKMPIdentifier
  76. )
  77. {
  78. DWORD dwError = 0;
  79. PIPSEC_POLICY_STORE pPolicyStore = NULL;
  80. LPWSTR pszIpsecISAKMPReference = NULL;
  81. DWORD dwRootPathLen = 0;
  82. LPWSTR pszRelativeName = NULL;
  83. LPWSTR * ppszIpsecPolicyReferences = NULL;
  84. DWORD dwNumReferences = 0;
  85. pPolicyStore = (PIPSEC_POLICY_STORE) hPolicyStore;
  86. switch (pPolicyStore->dwProvider) {
  87. case IPSEC_REGISTRY_PROVIDER:
  88. dwError = ConvertGuidToISAKMPString(
  89. ISAKMPIdentifier,
  90. pPolicyStore->pszIpsecRootContainer,
  91. &pszIpsecISAKMPReference
  92. );
  93. BAIL_ON_WIN32_ERROR(dwError);
  94. dwRootPathLen = wcslen(pPolicyStore->pszIpsecRootContainer);
  95. pszRelativeName = pszIpsecISAKMPReference + dwRootPathLen + 1;
  96. dwError = RegGetPolicyReferencesForISAKMP(
  97. pPolicyStore->hRegistryKey,
  98. pPolicyStore->pszIpsecRootContainer,
  99. pszRelativeName,
  100. &ppszIpsecPolicyReferences,
  101. &dwNumReferences
  102. );
  103. break;
  104. case IPSEC_DIRECTORY_PROVIDER:
  105. dwError = DirGetPolicyReferencesForISAKMP(
  106. pPolicyStore->hLdapBindHandle,
  107. pPolicyStore->pszIpsecRootContainer,
  108. ISAKMPIdentifier,
  109. &ppszIpsecPolicyReferences,
  110. &dwNumReferences
  111. );
  112. break;
  113. default:
  114. dwError = ERROR_INVALID_PARAMETER;
  115. return (dwError);
  116. break;
  117. }
  118. if (!dwNumReferences) {
  119. dwError = ERROR_SUCCESS;
  120. }
  121. else {
  122. dwError = ERROR_INVALID_DATA;
  123. }
  124. error:
  125. if (pszIpsecISAKMPReference) {
  126. FreePolStr(pszIpsecISAKMPReference);
  127. }
  128. if (ppszIpsecPolicyReferences) {
  129. FreeNFAReferences(
  130. ppszIpsecPolicyReferences,
  131. dwNumReferences
  132. );
  133. }
  134. return (dwError);
  135. }
  136. DWORD
  137. ValidateNegPolDataDeletion(
  138. HANDLE hPolicyStore,
  139. GUID NegPolIdentifier
  140. )
  141. {
  142. DWORD dwError = 0;
  143. PIPSEC_POLICY_STORE pPolicyStore = NULL;
  144. LPWSTR pszIpsecNegPolReference = NULL;
  145. DWORD dwRootPathLen = 0;
  146. LPWSTR pszRelativeName = NULL;
  147. LPWSTR * ppszIpsecNFAReferences = NULL;
  148. DWORD dwNumReferences = 0;
  149. pPolicyStore = (PIPSEC_POLICY_STORE) hPolicyStore;
  150. switch (pPolicyStore->dwProvider) {
  151. case IPSEC_REGISTRY_PROVIDER:
  152. dwError = ConvertGuidToNegPolString(
  153. NegPolIdentifier,
  154. pPolicyStore->pszIpsecRootContainer,
  155. &pszIpsecNegPolReference
  156. );
  157. BAIL_ON_WIN32_ERROR(dwError);
  158. dwRootPathLen = wcslen(pPolicyStore->pszIpsecRootContainer);
  159. pszRelativeName = pszIpsecNegPolReference + dwRootPathLen + 1;
  160. dwError = RegGetNFAReferencesForNegPol(
  161. pPolicyStore->hRegistryKey,
  162. pPolicyStore->pszIpsecRootContainer,
  163. pszRelativeName,
  164. &ppszIpsecNFAReferences,
  165. &dwNumReferences
  166. );
  167. break;
  168. case IPSEC_DIRECTORY_PROVIDER:
  169. dwError = DirGetNFAReferencesForNegPol(
  170. pPolicyStore->hLdapBindHandle,
  171. pPolicyStore->pszIpsecRootContainer,
  172. NegPolIdentifier,
  173. &ppszIpsecNFAReferences,
  174. &dwNumReferences
  175. );
  176. break;
  177. default:
  178. dwError = ERROR_INVALID_PARAMETER;
  179. return (dwError);
  180. break;
  181. }
  182. if (!dwNumReferences) {
  183. dwError = ERROR_SUCCESS;
  184. }
  185. else {
  186. dwError = ERROR_INVALID_DATA;
  187. }
  188. error:
  189. if (pszIpsecNegPolReference) {
  190. FreePolStr(pszIpsecNegPolReference);
  191. }
  192. if (ppszIpsecNFAReferences) {
  193. FreeNFAReferences(
  194. ppszIpsecNFAReferences,
  195. dwNumReferences
  196. );
  197. }
  198. return (dwError);
  199. }
  200. DWORD
  201. ValidateFilterDataDeletion(
  202. HANDLE hPolicyStore,
  203. GUID FilterIdentifier
  204. )
  205. {
  206. DWORD dwError = 0;
  207. PIPSEC_POLICY_STORE pPolicyStore = NULL;
  208. LPWSTR pszIpsecFilterReference = NULL;
  209. DWORD dwRootPathLen = 0;
  210. LPWSTR pszRelativeName = NULL;
  211. LPWSTR * ppszIpsecNFAReferences = NULL;
  212. DWORD dwNumReferences = 0;
  213. pPolicyStore = (PIPSEC_POLICY_STORE) hPolicyStore;
  214. switch (pPolicyStore->dwProvider) {
  215. case IPSEC_REGISTRY_PROVIDER:
  216. dwError = ConvertGuidToFilterString(
  217. FilterIdentifier,
  218. pPolicyStore->pszIpsecRootContainer,
  219. &pszIpsecFilterReference
  220. );
  221. BAIL_ON_WIN32_ERROR(dwError);
  222. dwRootPathLen = wcslen(pPolicyStore->pszIpsecRootContainer);
  223. pszRelativeName = pszIpsecFilterReference + dwRootPathLen + 1;
  224. dwError = RegGetNFAReferencesForFilter(
  225. pPolicyStore->hRegistryKey,
  226. pPolicyStore->pszIpsecRootContainer,
  227. pszRelativeName,
  228. &ppszIpsecNFAReferences,
  229. &dwNumReferences
  230. );
  231. break;
  232. case IPSEC_DIRECTORY_PROVIDER:
  233. dwError = DirGetNFAReferencesForFilter(
  234. pPolicyStore->hLdapBindHandle,
  235. pPolicyStore->pszIpsecRootContainer,
  236. FilterIdentifier,
  237. &ppszIpsecNFAReferences,
  238. &dwNumReferences
  239. );
  240. break;
  241. default:
  242. dwError = ERROR_INVALID_PARAMETER;
  243. return (dwError);
  244. break;
  245. }
  246. if (!dwNumReferences) {
  247. dwError = ERROR_SUCCESS;
  248. }
  249. else {
  250. dwError = ERROR_INVALID_DATA;
  251. }
  252. error:
  253. if (pszIpsecFilterReference) {
  254. FreePolStr(pszIpsecFilterReference);
  255. }
  256. if (ppszIpsecNFAReferences) {
  257. FreeNFAReferences(
  258. ppszIpsecNFAReferences,
  259. dwNumReferences
  260. );
  261. }
  262. return (dwError);
  263. }
  264. DWORD
  265. ValidatePolicyDataDeletion(
  266. HANDLE hPolicyStore,
  267. PIPSEC_POLICY_DATA pIpsecPolicyData
  268. )
  269. {
  270. DWORD dwError = 0;
  271. PIPSEC_POLICY_STORE pPolicyStore = NULL;
  272. LPWSTR pszIpsecPolicyReference = NULL;
  273. DWORD dwRootPathLen = 0;
  274. LPWSTR pszRelativeName = NULL;
  275. LPWSTR * ppszIpsecNFAReferences = NULL;
  276. DWORD dwNumReferences = 0;
  277. pPolicyStore = (PIPSEC_POLICY_STORE) hPolicyStore;
  278. switch (pPolicyStore->dwProvider) {
  279. case IPSEC_REGISTRY_PROVIDER:
  280. dwError = ConvertGuidToPolicyString(
  281. pIpsecPolicyData->PolicyIdentifier,
  282. pPolicyStore->pszIpsecRootContainer,
  283. &pszIpsecPolicyReference
  284. );
  285. BAIL_ON_WIN32_ERROR(dwError);
  286. dwRootPathLen = wcslen(pPolicyStore->pszIpsecRootContainer);
  287. pszRelativeName = pszIpsecPolicyReference + dwRootPathLen + 1;
  288. dwError = RegGetNFAReferencesForPolicy(
  289. pPolicyStore->hRegistryKey,
  290. pPolicyStore->pszIpsecRootContainer,
  291. pszRelativeName,
  292. &ppszIpsecNFAReferences,
  293. &dwNumReferences
  294. );
  295. break;
  296. case IPSEC_DIRECTORY_PROVIDER:
  297. dwError = GenerateSpecificPolicyQuery(
  298. pIpsecPolicyData->PolicyIdentifier,
  299. &pszIpsecPolicyReference
  300. );
  301. BAIL_ON_WIN32_ERROR(dwError);
  302. dwError = DirGetNFADNsForPolicy(
  303. pPolicyStore->hLdapBindHandle,
  304. pPolicyStore->pszIpsecRootContainer,
  305. pszIpsecPolicyReference,
  306. &ppszIpsecNFAReferences,
  307. &dwNumReferences
  308. );
  309. break;
  310. default:
  311. dwError = ERROR_INVALID_PARAMETER;
  312. return (dwError);
  313. break;
  314. }
  315. if (!dwNumReferences) {
  316. dwError = ERROR_SUCCESS;
  317. }
  318. else {
  319. dwError = ERROR_INVALID_DATA;
  320. }
  321. error:
  322. if (pszIpsecPolicyReference) {
  323. FreePolStr(pszIpsecPolicyReference);
  324. }
  325. if (ppszIpsecNFAReferences) {
  326. FreeNFAReferences(
  327. ppszIpsecNFAReferences,
  328. dwNumReferences
  329. );
  330. }
  331. return (dwError);
  332. }
  333. DWORD
  334. ValidatePolicyData(
  335. HANDLE hPolicyStore,
  336. PIPSEC_POLICY_DATA pIpsecPolicyData
  337. )
  338. {
  339. DWORD dwError = 0;
  340. PIPSEC_ISAKMP_DATA pIpsecISAKMPData = NULL;
  341. dwError = IPSecGetISAKMPData(
  342. hPolicyStore,
  343. pIpsecPolicyData->ISAKMPIdentifier,
  344. &pIpsecISAKMPData
  345. );
  346. BAIL_ON_WIN32_ERROR(dwError);
  347. error:
  348. if (pIpsecISAKMPData) {
  349. FreeIpsecISAKMPData(
  350. pIpsecISAKMPData
  351. );
  352. }
  353. return (dwError);
  354. }
  355. DWORD
  356. ValidateNFAData(
  357. HANDLE hPolicyStore,
  358. GUID PolicyIdentifier,
  359. PIPSEC_NFA_DATA pIpsecNFAData
  360. )
  361. {
  362. DWORD dwError = 0;
  363. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  364. PIPSEC_NEGPOL_DATA pIpsecNegPolData = NULL;
  365. GUID gZeroGUID;
  366. memset(&gZeroGUID, 0, sizeof(GUID));
  367. if (memcmp(
  368. &gZeroGUID,
  369. &pIpsecNFAData->FilterIdentifier,
  370. sizeof(GUID))) {
  371. dwError = IPSecGetFilterData(
  372. hPolicyStore,
  373. pIpsecNFAData->FilterIdentifier,
  374. &pIpsecFilterData
  375. );
  376. BAIL_ON_WIN32_ERROR(dwError);
  377. }
  378. dwError = IPSecGetNegPolData(
  379. hPolicyStore,
  380. pIpsecNFAData->NegPolIdentifier,
  381. &pIpsecNegPolData
  382. );
  383. BAIL_ON_WIN32_ERROR(dwError);
  384. dwError = VerifyPolicyDataExistence(
  385. hPolicyStore,
  386. PolicyIdentifier
  387. );
  388. BAIL_ON_WIN32_ERROR(dwError);
  389. error:
  390. if (pIpsecFilterData) {
  391. FreeIpsecFilterData(
  392. pIpsecFilterData
  393. );
  394. }
  395. if (pIpsecNegPolData) {
  396. FreeIpsecNegPolData(
  397. pIpsecNegPolData
  398. );
  399. }
  400. return (dwError);
  401. }
  402. DWORD
  403. VerifyPolicyDataExistence(
  404. HANDLE hPolicyStore,
  405. GUID PolicyIdentifier
  406. )
  407. {
  408. DWORD dwError = 0;
  409. PIPSEC_POLICY_STORE pPolicyStore = NULL;
  410. pPolicyStore = (PIPSEC_POLICY_STORE) hPolicyStore;
  411. switch (pPolicyStore->dwProvider) {
  412. case IPSEC_REGISTRY_PROVIDER:
  413. dwError = RegVerifyPolicyDataExistence(
  414. pPolicyStore->hRegistryKey,
  415. pPolicyStore->pszIpsecRootContainer,
  416. PolicyIdentifier
  417. );
  418. break;
  419. case IPSEC_DIRECTORY_PROVIDER:
  420. dwError = DirVerifyPolicyDataExistence(
  421. pPolicyStore->hLdapBindHandle,
  422. pPolicyStore->pszIpsecRootContainer,
  423. PolicyIdentifier
  424. );
  425. break;
  426. default:
  427. dwError = ERROR_INVALID_PARAMETER;
  428. break;
  429. }
  430. return (dwError);
  431. }
  432. DWORD
  433. RegGetNFAReferencesForPolicy(
  434. HKEY hRegistryKey,
  435. LPWSTR pszIpsecRootContainer,
  436. LPWSTR pszIpsecRelPolicyName,
  437. LPWSTR ** pppszIpsecNFANames,
  438. PDWORD pdwNumNFANames
  439. )
  440. {
  441. DWORD dwError = 0;
  442. HKEY hRegKey = 0;
  443. LPWSTR pszIpsecNFAReference = NULL;
  444. DWORD dwSize = 0;
  445. LPWSTR pszTemp = NULL;
  446. DWORD dwCount = 0;
  447. LPWSTR * ppszIpsecNFANames = NULL;
  448. LPWSTR pszString = NULL;
  449. DWORD i = 0;
  450. dwError = RegOpenKeyExW(
  451. hRegistryKey,
  452. pszIpsecRelPolicyName,
  453. 0,
  454. KEY_ALL_ACCESS,
  455. &hRegKey
  456. );
  457. BAIL_ON_WIN32_ERROR(dwError);
  458. dwError = RegstoreQueryValue(
  459. hRegKey,
  460. L"ipsecNFAReference",
  461. REG_MULTI_SZ,
  462. (LPBYTE *)&pszIpsecNFAReference,
  463. &dwSize
  464. );
  465. BAIL_ON_WIN32_ERROR(dwError);
  466. pszTemp = pszIpsecNFAReference;
  467. while (*pszTemp != L'\0') {
  468. pszTemp += wcslen(pszTemp) + 1;
  469. dwCount++;
  470. }
  471. if (!dwCount) {
  472. dwError = ERROR_NO_DATA;
  473. BAIL_ON_WIN32_ERROR(dwError);
  474. }
  475. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  476. sizeof(LPWSTR)*dwCount
  477. );
  478. if (!ppszIpsecNFANames) {
  479. dwError = ERROR_OUTOFMEMORY;
  480. BAIL_ON_WIN32_ERROR(dwError);
  481. }
  482. pszTemp = pszIpsecNFAReference;
  483. for (i = 0; i < dwCount; i++) {
  484. pszString = AllocPolStr(pszTemp);
  485. if (!pszString) {
  486. dwError = ERROR_OUTOFMEMORY;
  487. BAIL_ON_WIN32_ERROR(dwError);
  488. }
  489. *(ppszIpsecNFANames + i) = pszString;
  490. pszTemp += wcslen(pszTemp) + 1; //for the null terminator;
  491. }
  492. *pppszIpsecNFANames = ppszIpsecNFANames;
  493. *pdwNumNFANames = dwCount;
  494. dwError = ERROR_SUCCESS;
  495. cleanup:
  496. if (hRegKey) {
  497. RegCloseKey(hRegKey);
  498. }
  499. if (pszIpsecNFAReference) {
  500. FreePolStr(pszIpsecNFAReference);
  501. }
  502. return(dwError);
  503. error:
  504. if (ppszIpsecNFANames) {
  505. FreeNFAReferences(
  506. ppszIpsecNFANames,
  507. dwCount
  508. );
  509. }
  510. *pppszIpsecNFANames = NULL;
  511. *pdwNumNFANames = 0;
  512. goto cleanup;
  513. }
  514. DWORD
  515. RegVerifyPolicyDataExistence(
  516. HKEY hRegistryKey,
  517. LPWSTR pszIpsecRootContainer,
  518. GUID PolicyGUID
  519. )
  520. {
  521. DWORD dwError = 0;
  522. WCHAR szIpsecPolicyName[MAX_PATH];
  523. LPWSTR pszPolicyName = NULL;
  524. HKEY hRegKey = NULL;
  525. szIpsecPolicyName[0] = L'\0';
  526. wcscpy(szIpsecPolicyName, L"ipsecPolicy");
  527. dwError = UuidToString(&PolicyGUID, &pszPolicyName);
  528. BAIL_ON_WIN32_ERROR(dwError);
  529. wcscat(szIpsecPolicyName, L"{");
  530. wcscat(szIpsecPolicyName, pszPolicyName);
  531. wcscat(szIpsecPolicyName, L"}");
  532. dwError = RegOpenKeyExW(
  533. hRegistryKey,
  534. szIpsecPolicyName,
  535. 0,
  536. KEY_ALL_ACCESS,
  537. &hRegKey
  538. );
  539. BAIL_ON_WIN32_ERROR(dwError);
  540. error:
  541. if (pszPolicyName) {
  542. RpcStringFree(&pszPolicyName);
  543. }
  544. if (hRegKey) {
  545. RegCloseKey(hRegKey);
  546. }
  547. return (dwError);
  548. }
  549. DWORD
  550. DirVerifyPolicyDataExistence(
  551. HLDAP hLdapBindHandle,
  552. LPWSTR pszIpsecRootContainer,
  553. GUID PolicyGUID
  554. )
  555. {
  556. DWORD dwError = 0;
  557. LPWSTR pszPolicyString = NULL;
  558. LDAPMessage * res = NULL;
  559. DWORD dwCount = 0;
  560. dwError = GenerateSpecificPolicyQuery(
  561. PolicyGUID,
  562. &pszPolicyString
  563. );
  564. BAIL_ON_WIN32_ERROR(dwError);
  565. dwError = LdapSearchST(
  566. hLdapBindHandle,
  567. pszIpsecRootContainer,
  568. LDAP_SCOPE_ONELEVEL,
  569. pszPolicyString,
  570. PolicyDNAttributes,
  571. 0,
  572. NULL,
  573. &res
  574. );
  575. BAIL_ON_WIN32_ERROR(dwError);
  576. dwCount = LdapCountEntries(
  577. hLdapBindHandle,
  578. res
  579. );
  580. if (!dwCount) {
  581. dwError = ERROR_DS_NO_ATTRIBUTE_OR_VALUE;
  582. BAIL_ON_WIN32_ERROR(dwError);
  583. }
  584. error:
  585. if (pszPolicyString) {
  586. FreePolStr(pszPolicyString);
  587. }
  588. if (res) {
  589. LdapMsgFree(res);
  590. }
  591. return (dwError);
  592. }