Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1678 lines
40 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: policy-d.c
  7. //
  8. // Contents: Policy management for directory.
  9. //
  10. //
  11. // History: AbhisheV (05/11/00)
  12. //
  13. //----------------------------------------------------------------------------
  14. #include "precomp.h"
  15. extern LPWSTR PolicyDNAttributes[];
  16. DWORD
  17. DirEnumPolicyData(
  18. HLDAP hLdapBindHandle,
  19. LPWSTR pszIpsecRootContainer,
  20. PIPSEC_POLICY_DATA ** pppIpsecPolicyData,
  21. PDWORD pdwNumPolicyObjects
  22. )
  23. {
  24. DWORD dwError = 0;
  25. PIPSEC_POLICY_OBJECT * ppIpsecPolicyObjects = NULL;
  26. PIPSEC_POLICY_DATA pIpsecPolicyData = NULL;
  27. PIPSEC_POLICY_DATA * ppIpsecPolicyData = NULL;
  28. DWORD dwNumPolicyObjects = 0;
  29. DWORD i = 0;
  30. DWORD j = 0;
  31. dwError = DirEnumPolicyObjects(
  32. hLdapBindHandle,
  33. pszIpsecRootContainer,
  34. &ppIpsecPolicyObjects,
  35. &dwNumPolicyObjects
  36. );
  37. BAIL_ON_WIN32_ERROR(dwError);
  38. if (dwNumPolicyObjects) {
  39. ppIpsecPolicyData = (PIPSEC_POLICY_DATA *) AllocPolMem(
  40. dwNumPolicyObjects*sizeof(PIPSEC_POLICY_DATA));
  41. if (!ppIpsecPolicyData) {
  42. dwError = ERROR_OUTOFMEMORY;
  43. BAIL_ON_WIN32_ERROR(dwError);
  44. }
  45. }
  46. for (i = 0; i < dwNumPolicyObjects; i++) {
  47. dwError = DirUnmarshallPolicyData(
  48. *(ppIpsecPolicyObjects + i),
  49. &pIpsecPolicyData
  50. );
  51. if (!dwError) {
  52. *(ppIpsecPolicyData + j) = pIpsecPolicyData;
  53. j++;
  54. }
  55. }
  56. if (j == 0) {
  57. if (ppIpsecPolicyData) {
  58. FreePolMem(ppIpsecPolicyData);
  59. ppIpsecPolicyData = NULL;
  60. }
  61. }
  62. *pppIpsecPolicyData = ppIpsecPolicyData;
  63. *pdwNumPolicyObjects = j;
  64. dwError = ERROR_SUCCESS;
  65. cleanup:
  66. if (ppIpsecPolicyObjects) {
  67. FreeIpsecPolicyObjects(
  68. ppIpsecPolicyObjects,
  69. dwNumPolicyObjects
  70. );
  71. }
  72. return(dwError);
  73. error:
  74. if (ppIpsecPolicyData) {
  75. FreeMulIpsecPolicyData(
  76. ppIpsecPolicyData,
  77. i
  78. );
  79. }
  80. *pppIpsecPolicyData = NULL;
  81. *pdwNumPolicyObjects = 0;
  82. goto cleanup;
  83. }
  84. DWORD
  85. DirEnumPolicyObjects(
  86. HLDAP hLdapBindHandle,
  87. LPWSTR pszIpsecRootContainer,
  88. PIPSEC_POLICY_OBJECT ** pppIpsecPolicyObjects,
  89. PDWORD pdwNumPolicyObjects
  90. )
  91. {
  92. LDAPMessage *res = NULL;
  93. LDAPMessage *e = NULL;
  94. DWORD dwError = 0;
  95. LPWSTR pszPolicyString = NULL;
  96. DWORD i = 0;
  97. DWORD dwCount = 0;
  98. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  99. PIPSEC_POLICY_OBJECT * ppIpsecPolicyObjects = NULL;
  100. DWORD dwNumPolicyObjectsReturned = 0;
  101. dwError = GenerateAllPolicyQuery(
  102. &pszPolicyString
  103. );
  104. BAIL_ON_WIN32_ERROR(dwError);
  105. dwError = LdapSearchST(
  106. hLdapBindHandle,
  107. pszIpsecRootContainer,
  108. LDAP_SCOPE_ONELEVEL,
  109. pszPolicyString,
  110. PolicyDNAttributes,
  111. 0,
  112. NULL,
  113. &res
  114. );
  115. BAIL_ON_WIN32_ERROR(dwError);
  116. dwCount = LdapCountEntries(
  117. hLdapBindHandle,
  118. res
  119. );
  120. if (!dwCount) {
  121. dwError = ERROR_DS_NO_ATTRIBUTE_OR_VALUE;
  122. BAIL_ON_WIN32_ERROR(dwError);
  123. }
  124. ppIpsecPolicyObjects = (PIPSEC_POLICY_OBJECT *)AllocPolMem(
  125. sizeof(PIPSEC_POLICY_OBJECT)*dwCount
  126. );
  127. if (!ppIpsecPolicyObjects) {
  128. dwError = ERROR_OUTOFMEMORY;
  129. BAIL_ON_WIN32_ERROR(dwError);
  130. }
  131. for (i = 0; i < dwCount; i++) {
  132. if (i == 0) {
  133. dwError = LdapFirstEntry(
  134. hLdapBindHandle,
  135. res,
  136. &e
  137. );
  138. BAIL_ON_WIN32_ERROR(dwError);
  139. } else {
  140. dwError = LdapNextEntry(
  141. hLdapBindHandle,
  142. e,
  143. &e
  144. );
  145. BAIL_ON_WIN32_ERROR(dwError);
  146. }
  147. dwError = UnMarshallPolicyObject2(
  148. hLdapBindHandle,
  149. e,
  150. &pIpsecPolicyObject
  151. );
  152. if (dwError == ERROR_SUCCESS) {
  153. *(ppIpsecPolicyObjects + dwNumPolicyObjectsReturned) = pIpsecPolicyObject;
  154. dwNumPolicyObjectsReturned++;
  155. }
  156. }
  157. *pppIpsecPolicyObjects = ppIpsecPolicyObjects;
  158. *pdwNumPolicyObjects = dwNumPolicyObjectsReturned;
  159. dwError = ERROR_SUCCESS;
  160. cleanup:
  161. if (pszPolicyString) {
  162. FreePolMem(pszPolicyString);
  163. }
  164. if (res) {
  165. LdapMsgFree(res);
  166. }
  167. return(dwError);
  168. error:
  169. if (ppIpsecPolicyObjects) {
  170. FreeIpsecPolicyObjects(
  171. ppIpsecPolicyObjects,
  172. dwNumPolicyObjectsReturned
  173. );
  174. }
  175. *pppIpsecPolicyObjects = NULL;
  176. *pdwNumPolicyObjects = 0;
  177. goto cleanup;
  178. }
  179. DWORD
  180. GenerateAllPolicyQuery(
  181. LPWSTR * ppszPolicyString
  182. )
  183. {
  184. DWORD dwError = 0;
  185. DWORD dwLength = 0;
  186. LPWSTR pszPolicyString = NULL;
  187. //
  188. // Compute Length of Buffer to be allocated
  189. //
  190. dwLength = wcslen(L"(objectclass=ipsecPolicy)");
  191. pszPolicyString = (LPWSTR) AllocPolMem((dwLength + 1)*sizeof(WCHAR));
  192. if (!pszPolicyString) {
  193. dwError = ERROR_OUTOFMEMORY;
  194. BAIL_ON_WIN32_ERROR(dwError);
  195. }
  196. //
  197. // Now fill in the buffer
  198. //
  199. wcscpy(pszPolicyString, L"(objectclass=ipsecPolicy)");
  200. *ppszPolicyString = pszPolicyString;
  201. return(0);
  202. error:
  203. if (pszPolicyString) {
  204. FreePolMem(pszPolicyString);
  205. }
  206. *ppszPolicyString = NULL;
  207. return(dwError);
  208. }
  209. DWORD
  210. UnMarshallPolicyObject2(
  211. HLDAP hLdapBindHandle,
  212. LDAPMessage *e,
  213. PIPSEC_POLICY_OBJECT * ppIpsecPolicyObject
  214. )
  215. {
  216. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  217. DWORD dwCount = 0;
  218. DWORD dwLen = 0;
  219. LPBYTE pBuffer = NULL;
  220. DWORD i = 0;
  221. DWORD dwError = 0;
  222. WCHAR **strvalues = NULL;
  223. struct berval ** bvalues = NULL;
  224. LPWSTR * ppszIpsecNFANames = NULL;
  225. LPWSTR pszIpsecNFAName = NULL;
  226. LPWSTR * ppszTemp = NULL;
  227. pIpsecPolicyObject = (PIPSEC_POLICY_OBJECT)AllocPolMem(
  228. sizeof(IPSEC_POLICY_OBJECT)
  229. );
  230. if (!pIpsecPolicyObject) {
  231. dwError = ERROR_OUTOFMEMORY;
  232. BAIL_ON_WIN32_ERROR(dwError);
  233. }
  234. strvalues = NULL;
  235. dwError = LdapGetValues(
  236. hLdapBindHandle,
  237. e,
  238. L"distinguishedName",
  239. (WCHAR ***)&strvalues,
  240. (int *)&dwCount
  241. );
  242. BAIL_ON_WIN32_ERROR(dwError);
  243. pIpsecPolicyObject->pszIpsecOwnersReference = AllocPolStr(
  244. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  245. );
  246. if (!pIpsecPolicyObject->pszIpsecOwnersReference) {
  247. dwError = ERROR_OUTOFMEMORY;
  248. BAIL_ON_WIN32_ERROR(dwError);
  249. }
  250. LdapValueFree(strvalues);
  251. strvalues = NULL;
  252. dwError = LdapGetValues(
  253. hLdapBindHandle,
  254. e,
  255. L"ipsecName",
  256. (WCHAR ***)&strvalues,
  257. (int *)&dwCount
  258. );
  259. BAIL_ON_WIN32_ERROR(dwError);
  260. pIpsecPolicyObject->pszIpsecName = AllocPolStr(
  261. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  262. );
  263. if (!pIpsecPolicyObject->pszIpsecName) {
  264. dwError = ERROR_OUTOFMEMORY;
  265. BAIL_ON_WIN32_ERROR(dwError);
  266. }
  267. LdapValueFree(strvalues);
  268. strvalues = NULL;
  269. dwError = LdapGetValues(
  270. hLdapBindHandle,
  271. e,
  272. L"description",
  273. (WCHAR ***)&strvalues,
  274. (int *)&dwCount
  275. );
  276. // BAIL_ON_WIN32_ERROR(dwError);
  277. if (strvalues && LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)) {
  278. pIpsecPolicyObject->pszDescription = AllocPolStr(
  279. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  280. );
  281. if (!pIpsecPolicyObject->pszDescription) {
  282. dwError = ERROR_OUTOFMEMORY;
  283. BAIL_ON_WIN32_ERROR(dwError);
  284. }
  285. LdapValueFree(strvalues);
  286. } else {
  287. pIpsecPolicyObject->pszDescription = NULL;
  288. }
  289. strvalues = NULL;
  290. dwError = LdapGetValues(
  291. hLdapBindHandle,
  292. e,
  293. L"ipsecID",
  294. (WCHAR ***)&strvalues,
  295. (int *)&dwCount
  296. );
  297. BAIL_ON_WIN32_ERROR(dwError);
  298. pIpsecPolicyObject->pszIpsecID = AllocPolStr(
  299. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  300. );
  301. if (!pIpsecPolicyObject->pszIpsecID) {
  302. dwError = ERROR_OUTOFMEMORY;
  303. BAIL_ON_WIN32_ERROR(dwError);
  304. }
  305. LdapValueFree(strvalues);
  306. strvalues = NULL;
  307. dwError = LdapGetValues(
  308. hLdapBindHandle,
  309. e,
  310. L"ipsecDataType",
  311. (WCHAR ***)&strvalues,
  312. (int *)&dwCount
  313. );
  314. BAIL_ON_WIN32_ERROR(dwError);
  315. pIpsecPolicyObject->dwIpsecDataType = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  316. LdapValueFree(strvalues);
  317. strvalues = NULL;
  318. dwError = LdapGetValues(
  319. hLdapBindHandle,
  320. e,
  321. L"whenChanged",
  322. (WCHAR ***)&strvalues,
  323. (int *)&dwCount
  324. );
  325. BAIL_ON_WIN32_ERROR(dwError);
  326. pIpsecPolicyObject->dwWhenChanged = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  327. LdapValueFree(strvalues);
  328. strvalues = NULL;
  329. dwError = LdapGetValues(
  330. hLdapBindHandle,
  331. e,
  332. L"ipsecISAKMPReference",
  333. (WCHAR ***)&strvalues,
  334. (int *)&dwCount
  335. );
  336. BAIL_ON_WIN32_ERROR(dwError);
  337. pIpsecPolicyObject->pszIpsecISAKMPReference = AllocPolStr(
  338. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  339. );
  340. if (!pIpsecPolicyObject->pszIpsecISAKMPReference) {
  341. dwError = ERROR_OUTOFMEMORY;
  342. BAIL_ON_WIN32_ERROR(dwError);
  343. }
  344. LdapValueFree(strvalues);
  345. //
  346. // unmarshall the ipsecData blob
  347. //
  348. dwError = LdapGetValuesLen(
  349. hLdapBindHandle,
  350. e,
  351. L"ipsecData",
  352. (struct berval ***)&bvalues,
  353. (int *)&dwCount
  354. );
  355. BAIL_ON_WIN32_ERROR(dwError);
  356. dwLen = LDAPOBJECT_BERVAL_LEN((PLDAPOBJECT)bvalues);
  357. pBuffer = (LPBYTE)AllocPolMem(dwLen);
  358. if (!pBuffer) {
  359. dwError = ERROR_OUTOFMEMORY;
  360. BAIL_ON_WIN32_ERROR(dwError);
  361. }
  362. memcpy( pBuffer, LDAPOBJECT_BERVAL_VAL((PLDAPOBJECT)bvalues), dwLen );
  363. pIpsecPolicyObject->pIpsecData = pBuffer;
  364. pIpsecPolicyObject->dwIpsecDataLen = dwLen;
  365. LdapValueFreeLen(bvalues);
  366. strvalues = NULL;
  367. dwError = LdapGetValues(
  368. hLdapBindHandle,
  369. e,
  370. L"ipsecNFAReference",
  371. (WCHAR ***)&strvalues,
  372. (int *)&dwCount
  373. );
  374. BAIL_ON_WIN32_ERROR(dwError);
  375. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  376. sizeof(LPWSTR)*dwCount
  377. );
  378. if (!ppszIpsecNFANames) {
  379. dwError = ERROR_OUTOFMEMORY;
  380. BAIL_ON_WIN32_ERROR(dwError);
  381. }
  382. for (i = 0; i < dwCount; i++) {
  383. ppszTemp = (strvalues + i);
  384. //
  385. // Unmarshall all the values you can possibly have
  386. //
  387. pszIpsecNFAName = AllocPolStr(*ppszTemp);
  388. if (!pszIpsecNFAName) {
  389. dwError = ERROR_OUTOFMEMORY;
  390. pIpsecPolicyObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  391. pIpsecPolicyObject->NumberofRules = i;
  392. BAIL_ON_WIN32_ERROR(dwError);
  393. }
  394. *(ppszIpsecNFANames + i) = pszIpsecNFAName;
  395. }
  396. pIpsecPolicyObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  397. pIpsecPolicyObject->NumberofRules = dwCount;
  398. LdapValueFree(strvalues);
  399. *ppIpsecPolicyObject = pIpsecPolicyObject;
  400. return(dwError);
  401. error:
  402. if (pIpsecPolicyObject) {
  403. FreeIpsecPolicyObject(pIpsecPolicyObject);
  404. }
  405. *ppIpsecPolicyObject = NULL;
  406. return(dwError);
  407. }
  408. DWORD
  409. DirUnmarshallPolicyData(
  410. PIPSEC_POLICY_OBJECT pIpsecPolicyObject,
  411. PIPSEC_POLICY_DATA * ppIpsecPolicyData
  412. )
  413. {
  414. DWORD dwError = 0;
  415. dwError = UnmarshallPolicyObject(
  416. pIpsecPolicyObject,
  417. IPSEC_DIRECTORY_PROVIDER,
  418. ppIpsecPolicyData
  419. );
  420. return(dwError);
  421. }
  422. DWORD
  423. DirCreatePolicyData(
  424. HLDAP hLdapBindHandle,
  425. LPWSTR pszIpsecRootContainer,
  426. PIPSEC_POLICY_DATA pIpsecPolicyData
  427. )
  428. {
  429. DWORD dwError = 0;
  430. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  431. dwError = DirMarshallPolicyObject(
  432. pIpsecPolicyData,
  433. pszIpsecRootContainer,
  434. &pIpsecPolicyObject
  435. );
  436. BAIL_ON_WIN32_ERROR(dwError);
  437. dwError = DirCreatePolicyObject(
  438. hLdapBindHandle,
  439. pszIpsecRootContainer,
  440. pIpsecPolicyObject
  441. );
  442. BAIL_ON_WIN32_ERROR(dwError);
  443. //
  444. // Write the ISAKMP object reference.
  445. //
  446. dwError = DirAddPolicyReferenceToISAKMPObject(
  447. hLdapBindHandle,
  448. pIpsecPolicyObject->pszIpsecISAKMPReference,
  449. pIpsecPolicyObject->pszIpsecOwnersReference
  450. );
  451. BAIL_ON_WIN32_ERROR(dwError);
  452. //
  453. // Write the Policy object reference.
  454. //
  455. dwError = DirAddISAKMPReferenceToPolicyObject(
  456. hLdapBindHandle,
  457. pIpsecPolicyObject->pszIpsecOwnersReference,
  458. pIpsecPolicyObject->pszIpsecISAKMPReference
  459. );
  460. BAIL_ON_WIN32_ERROR(dwError);
  461. error:
  462. if (pIpsecPolicyObject) {
  463. FreeIpsecPolicyObject(
  464. pIpsecPolicyObject
  465. );
  466. }
  467. return(dwError);
  468. }
  469. DWORD
  470. DirMarshallPolicyObject(
  471. PIPSEC_POLICY_DATA pIpsecPolicyData,
  472. LPWSTR pszIpsecRootContainer,
  473. PIPSEC_POLICY_OBJECT * ppIpsecPolicyObject
  474. )
  475. {
  476. DWORD dwError = 0;
  477. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  478. WCHAR szGuid[MAX_PATH];
  479. WCHAR szDistinguishedName[MAX_PATH];
  480. LPBYTE pBuffer = NULL;
  481. DWORD dwBufferLen = 0;
  482. LPWSTR pszStringUuid = NULL;
  483. LPWSTR pszIpsecISAKMPReference = NULL;
  484. szGuid[0] = L'\0';
  485. szDistinguishedName[0] = L'\0';
  486. pIpsecPolicyObject = (PIPSEC_POLICY_OBJECT)AllocPolMem(
  487. sizeof(IPSEC_POLICY_OBJECT)
  488. );
  489. if (!pIpsecPolicyObject) {
  490. dwError = ERROR_OUTOFMEMORY;
  491. BAIL_ON_WIN32_ERROR(dwError);
  492. }
  493. dwError = UuidToString(
  494. &pIpsecPolicyData->PolicyIdentifier,
  495. &pszStringUuid
  496. );
  497. BAIL_ON_WIN32_ERROR(dwError);
  498. wcscpy(szGuid, L"{");
  499. wcscat(szGuid, pszStringUuid);
  500. wcscat(szGuid, L"}");
  501. //
  502. // Fill in the distinguishedName
  503. //
  504. wcscpy(szDistinguishedName,L"CN=ipsecPolicy");
  505. wcscat(szDistinguishedName, szGuid);
  506. wcscat(szDistinguishedName, L",");
  507. wcscat(szDistinguishedName, pszIpsecRootContainer);
  508. pIpsecPolicyObject->pszIpsecOwnersReference = AllocPolStr(
  509. szDistinguishedName
  510. );
  511. if (!pIpsecPolicyObject->pszIpsecOwnersReference) {
  512. dwError = ERROR_OUTOFMEMORY;
  513. BAIL_ON_WIN32_ERROR(dwError);
  514. }
  515. //
  516. // Fill in the ipsecName
  517. //
  518. if (pIpsecPolicyData->pszIpsecName &&
  519. *pIpsecPolicyData->pszIpsecName) {
  520. pIpsecPolicyObject->pszIpsecName = AllocPolStr(
  521. pIpsecPolicyData->pszIpsecName
  522. );
  523. if (!pIpsecPolicyObject->pszIpsecName) {
  524. dwError = ERROR_OUTOFMEMORY;
  525. BAIL_ON_WIN32_ERROR(dwError);
  526. }
  527. }
  528. if (pIpsecPolicyData->pszDescription &&
  529. *pIpsecPolicyData->pszDescription) {
  530. pIpsecPolicyObject->pszDescription = AllocPolStr(
  531. pIpsecPolicyData->pszDescription
  532. );
  533. if (!pIpsecPolicyObject->pszDescription) {
  534. dwError = ERROR_OUTOFMEMORY;
  535. BAIL_ON_WIN32_ERROR(dwError);
  536. }
  537. }
  538. //
  539. // Fill in the ipsecID
  540. //
  541. pIpsecPolicyObject->pszIpsecID = AllocPolStr(
  542. szGuid
  543. );
  544. if (!pIpsecPolicyObject->pszIpsecID) {
  545. dwError = ERROR_OUTOFMEMORY;
  546. BAIL_ON_WIN32_ERROR(dwError);
  547. }
  548. //
  549. // Fill in the ipsecDataType
  550. //
  551. pIpsecPolicyObject->dwIpsecDataType = 0x100;
  552. //
  553. // Marshall the pIpsecDataBuffer and the Length
  554. //
  555. dwError = MarshallPolicyBuffer(
  556. pIpsecPolicyData,
  557. &pBuffer,
  558. &dwBufferLen
  559. );
  560. BAIL_ON_WIN32_ERROR(dwError);
  561. pIpsecPolicyObject->pIpsecData = pBuffer;
  562. pIpsecPolicyObject->dwIpsecDataLen = dwBufferLen;
  563. dwError = ConvertGuidToDirISAKMPString(
  564. pIpsecPolicyData->ISAKMPIdentifier,
  565. pszIpsecRootContainer,
  566. &pszIpsecISAKMPReference
  567. );
  568. BAIL_ON_WIN32_ERROR(dwError);
  569. pIpsecPolicyObject->pszIpsecISAKMPReference = pszIpsecISAKMPReference;
  570. pIpsecPolicyObject->dwWhenChanged = 0;
  571. *ppIpsecPolicyObject = pIpsecPolicyObject;
  572. cleanup:
  573. if (pszStringUuid) {
  574. RpcStringFree(
  575. &pszStringUuid
  576. );
  577. }
  578. return(dwError);
  579. error:
  580. if (pIpsecPolicyObject) {
  581. FreeIpsecPolicyObject(
  582. pIpsecPolicyObject
  583. );
  584. }
  585. *ppIpsecPolicyObject = NULL;
  586. goto cleanup;
  587. }
  588. DWORD
  589. ConvertGuidToDirISAKMPString(
  590. GUID ISAKMPIdentifier,
  591. LPWSTR pszIpsecRootContainer,
  592. LPWSTR * ppszIpsecISAKMPReference
  593. )
  594. {
  595. DWORD dwError = 0;
  596. LPWSTR pszStringUuid = NULL;
  597. WCHAR szGuidString[MAX_PATH];
  598. WCHAR szISAKMPReference[MAX_PATH];
  599. LPWSTR pszIpsecISAKMPReference = NULL;
  600. dwError = UuidToString(
  601. &ISAKMPIdentifier,
  602. &pszStringUuid
  603. );
  604. BAIL_ON_WIN32_ERROR(dwError);
  605. szGuidString[0] = L'\0';
  606. wcscpy(szGuidString, L"{");
  607. wcscat(szGuidString, pszStringUuid);
  608. wcscat(szGuidString, L"}");
  609. szISAKMPReference[0] = L'\0';
  610. wcscpy(szISAKMPReference,L"CN=ipsecISAKMPPolicy");
  611. wcscat(szISAKMPReference, szGuidString);
  612. wcscat(szISAKMPReference, L",");
  613. wcscat(szISAKMPReference, pszIpsecRootContainer);
  614. pszIpsecISAKMPReference = AllocPolStr(
  615. szISAKMPReference
  616. );
  617. if (!pszIpsecISAKMPReference) {
  618. dwError = ERROR_OUTOFMEMORY;
  619. BAIL_ON_WIN32_ERROR(dwError);
  620. }
  621. *ppszIpsecISAKMPReference = pszIpsecISAKMPReference;
  622. cleanup:
  623. if (pszStringUuid) {
  624. RpcStringFree(&pszStringUuid);
  625. }
  626. return(dwError);
  627. error:
  628. *ppszIpsecISAKMPReference = NULL;
  629. goto cleanup;
  630. }
  631. DWORD
  632. DirCreatePolicyObject(
  633. HLDAP hLdapBindHandle,
  634. LPWSTR pszIpsecRootContainer,
  635. PIPSEC_POLICY_OBJECT pIpsecPolicyObject
  636. )
  637. {
  638. DWORD dwError = 0;
  639. LDAPModW ** ppLDAPModW = NULL;
  640. dwError = DirMarshallAddPolicyObject(
  641. hLdapBindHandle,
  642. pszIpsecRootContainer,
  643. pIpsecPolicyObject,
  644. &ppLDAPModW
  645. );
  646. BAIL_ON_WIN32_ERROR(dwError);
  647. dwError = LdapAddS(
  648. hLdapBindHandle,
  649. pIpsecPolicyObject->pszIpsecOwnersReference,
  650. ppLDAPModW
  651. );
  652. BAIL_ON_WIN32_ERROR(dwError);
  653. error:
  654. //
  655. // Free the amods structures.
  656. //
  657. if (ppLDAPModW) {
  658. FreeLDAPModWs(
  659. ppLDAPModW
  660. );
  661. }
  662. return(dwError);
  663. }
  664. DWORD
  665. DirMarshallAddPolicyObject(
  666. HLDAP hLdapBindHandle,
  667. LPWSTR pszIpsecRootContainer,
  668. PIPSEC_POLICY_OBJECT pIpsecPolicyObject,
  669. LDAPModW *** pppLDAPModW
  670. )
  671. {
  672. DWORD i = 0;
  673. LDAPModW ** ppLDAPModW = NULL;
  674. LDAPModW * pLDAPModW = NULL;
  675. DWORD dwNumAttributes = 6;
  676. DWORD dwError = 0;
  677. WCHAR Buffer[64];
  678. if (!pIpsecPolicyObject->pszIpsecName ||
  679. !*pIpsecPolicyObject->pszIpsecName) {
  680. dwNumAttributes--;
  681. }
  682. if (!pIpsecPolicyObject->pszDescription ||
  683. !*pIpsecPolicyObject->pszDescription) {
  684. dwNumAttributes--;
  685. }
  686. ppLDAPModW = (LDAPModW **) AllocPolMem(
  687. (dwNumAttributes+1) * sizeof(LDAPModW*)
  688. );
  689. if (!ppLDAPModW) {
  690. dwError = ERROR_OUTOFMEMORY;
  691. BAIL_ON_WIN32_ERROR(dwError);
  692. }
  693. pLDAPModW = (LDAPModW *) AllocPolMem(
  694. dwNumAttributes * sizeof(LDAPModW)
  695. );
  696. if (!pLDAPModW) {
  697. dwError = ERROR_OUTOFMEMORY;
  698. BAIL_ON_WIN32_ERROR(dwError);
  699. }
  700. //
  701. // 0. objectClass
  702. //
  703. ppLDAPModW[i] = pLDAPModW + i;
  704. dwError = AllocatePolString(
  705. L"objectClass",
  706. &(pLDAPModW +i)->mod_type
  707. );
  708. BAIL_ON_WIN32_ERROR(dwError);
  709. dwError = AllocateLDAPStringValue(
  710. L"ipsecPolicy",
  711. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  712. );
  713. BAIL_ON_WIN32_ERROR(dwError);
  714. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  715. i++;
  716. //
  717. // 1. ipsecName
  718. //
  719. if (pIpsecPolicyObject->pszIpsecName &&
  720. *pIpsecPolicyObject->pszIpsecName) {
  721. ppLDAPModW[i] = pLDAPModW + i;
  722. dwError = AllocatePolString(
  723. L"ipsecName",
  724. &(pLDAPModW +i)->mod_type
  725. );
  726. BAIL_ON_WIN32_ERROR(dwError);
  727. dwError = AllocateLDAPStringValue(
  728. pIpsecPolicyObject->pszIpsecName,
  729. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  730. );
  731. BAIL_ON_WIN32_ERROR(dwError);
  732. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  733. i++;
  734. }
  735. //
  736. // 2. ipsecID
  737. //
  738. ppLDAPModW[i] = pLDAPModW + i;
  739. dwError = AllocatePolString(
  740. L"ipsecID",
  741. &(pLDAPModW +i)->mod_type
  742. );
  743. BAIL_ON_WIN32_ERROR(dwError);
  744. dwError = AllocateLDAPStringValue(
  745. pIpsecPolicyObject->pszIpsecID,
  746. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  747. );
  748. BAIL_ON_WIN32_ERROR(dwError);
  749. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  750. i++;
  751. //
  752. // 3. ipsecDataType
  753. //
  754. ppLDAPModW[i] = pLDAPModW + i;
  755. dwError = AllocatePolString(
  756. L"ipsecDataType",
  757. &(pLDAPModW +i)->mod_type
  758. );
  759. BAIL_ON_WIN32_ERROR(dwError);
  760. _itow( pIpsecPolicyObject->dwIpsecDataType, Buffer, 10 );
  761. dwError = AllocateLDAPStringValue(
  762. Buffer,
  763. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  764. );
  765. BAIL_ON_WIN32_ERROR(dwError);
  766. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  767. i++;
  768. //
  769. // 4. ipsecData
  770. //
  771. ppLDAPModW[i] = pLDAPModW + i;
  772. dwError = AllocatePolString(
  773. L"ipsecData",
  774. &(pLDAPModW +i)->mod_type
  775. );
  776. BAIL_ON_WIN32_ERROR(dwError);
  777. dwError = AllocateLDAPBinaryValue(
  778. pIpsecPolicyObject->pIpsecData,
  779. pIpsecPolicyObject->dwIpsecDataLen,
  780. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  781. );
  782. BAIL_ON_WIN32_ERROR(dwError);
  783. (pLDAPModW+i)->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  784. i++;
  785. //
  786. // 5. description
  787. //
  788. if (pIpsecPolicyObject->pszDescription &&
  789. *pIpsecPolicyObject->pszDescription) {
  790. ppLDAPModW[i] = pLDAPModW + i;
  791. dwError = AllocatePolString(
  792. L"description",
  793. &(pLDAPModW +i)->mod_type
  794. );
  795. BAIL_ON_WIN32_ERROR(dwError);
  796. dwError = AllocateLDAPStringValue(
  797. pIpsecPolicyObject->pszDescription,
  798. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  799. );
  800. BAIL_ON_WIN32_ERROR(dwError);
  801. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  802. i++;
  803. }
  804. *pppLDAPModW = ppLDAPModW;
  805. return(dwError);
  806. error:
  807. if (ppLDAPModW) {
  808. FreeLDAPModWs(
  809. ppLDAPModW
  810. );
  811. }
  812. *pppLDAPModW = NULL;
  813. return(dwError);
  814. }
  815. DWORD
  816. DirSetPolicyData(
  817. HLDAP hLdapBindHandle,
  818. LPWSTR pszIpsecRootContainer,
  819. PIPSEC_POLICY_DATA pIpsecPolicyData
  820. )
  821. {
  822. DWORD dwError = 0;
  823. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  824. LPWSTR pszOldIpsecISAKMPReference = NULL;
  825. dwError = DirMarshallPolicyObject(
  826. pIpsecPolicyData,
  827. pszIpsecRootContainer,
  828. &pIpsecPolicyObject
  829. );
  830. BAIL_ON_WIN32_ERROR(dwError);
  831. dwError = DirGetPolicyExistingISAKMPRef(
  832. hLdapBindHandle,
  833. pszIpsecRootContainer,
  834. pIpsecPolicyData,
  835. &pszOldIpsecISAKMPReference
  836. );
  837. // BAIL_ON_WIN32_ERROR(dwError);
  838. dwError = DirSetPolicyObject(
  839. hLdapBindHandle,
  840. pszIpsecRootContainer,
  841. pIpsecPolicyObject
  842. );
  843. BAIL_ON_WIN32_ERROR(dwError);
  844. if (pszOldIpsecISAKMPReference && *pszOldIpsecISAKMPReference) {
  845. dwError = DirRemovePolicyReferenceFromISAKMPObject(
  846. hLdapBindHandle,
  847. pszOldIpsecISAKMPReference,
  848. pIpsecPolicyObject->pszIpsecOwnersReference
  849. );
  850. // BAIL_ON_WIN32_ERROR(dwError);
  851. }
  852. dwError = DirAddPolicyReferenceToISAKMPObject(
  853. hLdapBindHandle,
  854. pIpsecPolicyObject->pszIpsecISAKMPReference,
  855. pIpsecPolicyObject->pszIpsecOwnersReference
  856. );
  857. BAIL_ON_WIN32_ERROR(dwError);
  858. dwError = DirUpdateISAKMPReferenceInPolicyObject(
  859. hLdapBindHandle,
  860. pIpsecPolicyObject->pszIpsecOwnersReference,
  861. pszOldIpsecISAKMPReference,
  862. pIpsecPolicyObject->pszIpsecISAKMPReference
  863. );
  864. BAIL_ON_WIN32_ERROR(dwError);
  865. error:
  866. if (pIpsecPolicyObject) {
  867. FreeIpsecPolicyObject(pIpsecPolicyObject);
  868. }
  869. if (pszOldIpsecISAKMPReference) {
  870. FreePolStr(pszOldIpsecISAKMPReference);
  871. }
  872. return(dwError);
  873. }
  874. DWORD
  875. DirSetPolicyObject(
  876. HLDAP hLdapBindHandle,
  877. LPWSTR pszIpsecRootContainer,
  878. PIPSEC_POLICY_OBJECT pIpsecPolicyObject
  879. )
  880. {
  881. DWORD dwError = 0;
  882. LDAPModW ** ppLDAPModW = NULL;
  883. dwError = DirMarshallSetPolicyObject(
  884. hLdapBindHandle,
  885. pszIpsecRootContainer,
  886. pIpsecPolicyObject,
  887. &ppLDAPModW
  888. );
  889. BAIL_ON_WIN32_ERROR(dwError);
  890. dwError = LdapModifyS(
  891. hLdapBindHandle,
  892. pIpsecPolicyObject->pszIpsecOwnersReference,
  893. ppLDAPModW
  894. );
  895. BAIL_ON_WIN32_ERROR(dwError);
  896. error:
  897. //
  898. // Free the amods structures.
  899. //
  900. if (ppLDAPModW) {
  901. FreeLDAPModWs(
  902. ppLDAPModW
  903. );
  904. }
  905. return(dwError);
  906. }
  907. DWORD
  908. DirMarshallSetPolicyObject(
  909. HLDAP hLdapBindHandle,
  910. LPWSTR pszIpsecRootContainer,
  911. PIPSEC_POLICY_OBJECT pIpsecPolicyObject,
  912. LDAPModW *** pppLDAPModW
  913. )
  914. {
  915. DWORD i = 0;
  916. LDAPModW ** ppLDAPModW = NULL;
  917. LDAPModW * pLDAPModW = NULL;
  918. DWORD dwNumAttributes = 5;
  919. DWORD dwError = 0;
  920. WCHAR Buffer[64];
  921. if (!pIpsecPolicyObject->pszIpsecName ||
  922. !*pIpsecPolicyObject->pszIpsecName) {
  923. dwNumAttributes--;
  924. }
  925. if (!pIpsecPolicyObject->pszDescription ||
  926. !*pIpsecPolicyObject->pszDescription) {
  927. dwNumAttributes--;
  928. }
  929. ppLDAPModW = (LDAPModW **) AllocPolMem(
  930. (dwNumAttributes+1) * sizeof(LDAPModW*)
  931. );
  932. if (!ppLDAPModW) {
  933. dwError = ERROR_OUTOFMEMORY;
  934. BAIL_ON_WIN32_ERROR(dwError);
  935. }
  936. pLDAPModW = (LDAPModW *) AllocPolMem(
  937. dwNumAttributes * sizeof(LDAPModW)
  938. );
  939. if (!pLDAPModW) {
  940. dwError = ERROR_OUTOFMEMORY;
  941. BAIL_ON_WIN32_ERROR(dwError);
  942. }
  943. //
  944. // 1. ipsecName
  945. //
  946. if (pIpsecPolicyObject->pszIpsecName &&
  947. *pIpsecPolicyObject->pszIpsecName) {
  948. ppLDAPModW[i] = pLDAPModW + i;
  949. dwError = AllocatePolString(
  950. L"ipsecName",
  951. &(pLDAPModW +i)->mod_type
  952. );
  953. BAIL_ON_WIN32_ERROR(dwError);
  954. dwError = AllocateLDAPStringValue(
  955. pIpsecPolicyObject->pszIpsecName,
  956. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  957. );
  958. BAIL_ON_WIN32_ERROR(dwError);
  959. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  960. i++;
  961. }
  962. //
  963. // 2. ipsecID
  964. //
  965. ppLDAPModW[i] = pLDAPModW + i;
  966. dwError = AllocatePolString(
  967. L"ipsecID",
  968. &(pLDAPModW +i)->mod_type
  969. );
  970. BAIL_ON_WIN32_ERROR(dwError);
  971. dwError = AllocateLDAPStringValue(
  972. pIpsecPolicyObject->pszIpsecID,
  973. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  974. );
  975. BAIL_ON_WIN32_ERROR(dwError);
  976. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  977. i++;
  978. //
  979. // 3. ipsecDataType
  980. //
  981. ppLDAPModW[i] = pLDAPModW + i;
  982. dwError = AllocatePolString(
  983. L"ipsecDataType",
  984. &(pLDAPModW +i)->mod_type
  985. );
  986. BAIL_ON_WIN32_ERROR(dwError);
  987. _itow( pIpsecPolicyObject->dwIpsecDataType, Buffer, 10 );
  988. dwError = AllocateLDAPStringValue(
  989. Buffer,
  990. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  991. );
  992. BAIL_ON_WIN32_ERROR(dwError);
  993. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  994. i++;
  995. //
  996. // 4. ipsecData
  997. //
  998. ppLDAPModW[i] = pLDAPModW + i;
  999. dwError = AllocatePolString(
  1000. L"ipsecData",
  1001. &(pLDAPModW +i)->mod_type
  1002. );
  1003. BAIL_ON_WIN32_ERROR(dwError);
  1004. dwError = AllocateLDAPBinaryValue(
  1005. pIpsecPolicyObject->pIpsecData,
  1006. pIpsecPolicyObject->dwIpsecDataLen,
  1007. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  1008. );
  1009. BAIL_ON_WIN32_ERROR(dwError);
  1010. (pLDAPModW+i)->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1011. i++;
  1012. //
  1013. // 5. description
  1014. //
  1015. if (pIpsecPolicyObject->pszDescription &&
  1016. *pIpsecPolicyObject->pszDescription) {
  1017. ppLDAPModW[i] = pLDAPModW + i;
  1018. dwError = AllocatePolString(
  1019. L"description",
  1020. &(pLDAPModW +i)->mod_type
  1021. );
  1022. BAIL_ON_WIN32_ERROR(dwError);
  1023. dwError = AllocateLDAPStringValue(
  1024. pIpsecPolicyObject->pszDescription,
  1025. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  1026. );
  1027. BAIL_ON_WIN32_ERROR(dwError);
  1028. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  1029. i++;
  1030. }
  1031. *pppLDAPModW = ppLDAPModW;
  1032. return(dwError);
  1033. error:
  1034. if (ppLDAPModW) {
  1035. FreeLDAPModWs(
  1036. ppLDAPModW
  1037. );
  1038. }
  1039. *pppLDAPModW = NULL;
  1040. return(dwError);
  1041. }
  1042. DWORD
  1043. DirGetPolicyExistingISAKMPRef(
  1044. HLDAP hLdapBindHandle,
  1045. LPWSTR pszIpsecRootContainer,
  1046. PIPSEC_POLICY_DATA pIpsecPolicyData,
  1047. LPWSTR * ppszIpsecISAKMPName
  1048. )
  1049. {
  1050. DWORD dwError = 0;
  1051. LPWSTR pszPolicyString = NULL;
  1052. LDAPMessage * res = NULL;
  1053. DWORD dwCount = 0;
  1054. LDAPMessage * e = NULL;
  1055. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  1056. LPWSTR pszIpsecISAKMPName = NULL;
  1057. dwError = GenerateSpecificPolicyQuery(
  1058. pIpsecPolicyData->PolicyIdentifier,
  1059. &pszPolicyString
  1060. );
  1061. BAIL_ON_WIN32_ERROR(dwError);
  1062. dwError = LdapSearchST(
  1063. hLdapBindHandle,
  1064. pszIpsecRootContainer,
  1065. LDAP_SCOPE_ONELEVEL,
  1066. pszPolicyString,
  1067. PolicyDNAttributes,
  1068. 0,
  1069. NULL,
  1070. &res
  1071. );
  1072. BAIL_ON_WIN32_ERROR(dwError);
  1073. dwCount = LdapCountEntries(
  1074. hLdapBindHandle,
  1075. res
  1076. );
  1077. if (!dwCount) {
  1078. dwError = ERROR_DS_NO_ATTRIBUTE_OR_VALUE;
  1079. BAIL_ON_WIN32_ERROR(dwError);
  1080. }
  1081. dwError = LdapFirstEntry(
  1082. hLdapBindHandle,
  1083. res,
  1084. &e
  1085. );
  1086. BAIL_ON_WIN32_ERROR(dwError);
  1087. dwError = UnMarshallPolicyObject2(
  1088. hLdapBindHandle,
  1089. e,
  1090. &pIpsecPolicyObject
  1091. );
  1092. BAIL_ON_WIN32_ERROR(dwError);
  1093. pszIpsecISAKMPName = AllocPolStr(
  1094. pIpsecPolicyObject->pszIpsecISAKMPReference
  1095. );
  1096. if (!pszIpsecISAKMPName) {
  1097. dwError = ERROR_OUTOFMEMORY;
  1098. BAIL_ON_WIN32_ERROR(dwError);
  1099. }
  1100. *ppszIpsecISAKMPName = pszIpsecISAKMPName;
  1101. dwError = ERROR_SUCCESS;
  1102. cleanup:
  1103. if (pszPolicyString) {
  1104. FreePolMem(pszPolicyString);
  1105. }
  1106. if (res) {
  1107. LdapMsgFree(res);
  1108. }
  1109. if (pIpsecPolicyObject) {
  1110. FreeIpsecPolicyObject(pIpsecPolicyObject);
  1111. }
  1112. return(dwError);
  1113. error:
  1114. *ppszIpsecISAKMPName = NULL;
  1115. goto cleanup;
  1116. }
  1117. DWORD
  1118. GenerateSpecificPolicyQuery(
  1119. GUID PolicyIdentifier,
  1120. LPWSTR * ppszPolicyString
  1121. )
  1122. {
  1123. DWORD dwError = ERROR_SUCCESS;
  1124. WCHAR szGuid[MAX_PATH];
  1125. WCHAR szCommonName[MAX_PATH];
  1126. LPWSTR pszStringUuid = NULL;
  1127. DWORD dwLength = 0;
  1128. LPWSTR pszPolicyString = NULL;
  1129. szGuid[0] = L'\0';
  1130. szCommonName[0] = L'\0';
  1131. dwError = UuidToString(
  1132. &PolicyIdentifier,
  1133. &pszStringUuid
  1134. );
  1135. BAIL_ON_WIN32_ERROR(dwError);
  1136. wcscpy(szGuid, L"{");
  1137. wcscat(szGuid, pszStringUuid);
  1138. wcscat(szGuid, L"}");
  1139. wcscpy(szCommonName, L"cn=ipsecPolicy");
  1140. wcscat(szCommonName, szGuid);
  1141. //
  1142. // Compute Length of Buffer to be allocated
  1143. //
  1144. dwLength = wcslen(L"(&(objectclass=ipsecPolicy)");
  1145. dwLength += wcslen(L"(");
  1146. dwLength += wcslen(szCommonName);
  1147. dwLength += wcslen(L"))");
  1148. pszPolicyString = (LPWSTR) AllocPolMem((dwLength + 1)*sizeof(WCHAR));
  1149. if (!pszPolicyString) {
  1150. dwError = ERROR_OUTOFMEMORY;
  1151. BAIL_ON_WIN32_ERROR(dwError);
  1152. }
  1153. wcscpy(pszPolicyString, L"(&(objectclass=ipsecPolicy)");
  1154. wcscat(pszPolicyString, L"(");
  1155. wcscat(pszPolicyString, szCommonName);
  1156. wcscat(pszPolicyString, L"))");
  1157. *ppszPolicyString = pszPolicyString;
  1158. cleanup:
  1159. if (pszStringUuid) {
  1160. RpcStringFree(&pszStringUuid);
  1161. }
  1162. return(dwError);
  1163. error:
  1164. if (pszPolicyString) {
  1165. FreePolMem(pszPolicyString);
  1166. }
  1167. *ppszPolicyString = NULL;
  1168. goto cleanup;
  1169. }
  1170. DWORD
  1171. DirDeletePolicyData(
  1172. HLDAP hLdapBindHandle,
  1173. LPWSTR pszIpsecRootContainer,
  1174. PIPSEC_POLICY_DATA pIpsecPolicyData
  1175. )
  1176. {
  1177. DWORD dwError = 0;
  1178. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  1179. dwError = DirMarshallPolicyObject(
  1180. pIpsecPolicyData,
  1181. pszIpsecRootContainer,
  1182. &pIpsecPolicyObject
  1183. );
  1184. BAIL_ON_WIN32_ERROR(dwError);
  1185. dwError = DirRemovePolicyReferenceFromISAKMPObject(
  1186. hLdapBindHandle,
  1187. pIpsecPolicyObject->pszIpsecISAKMPReference,
  1188. pIpsecPolicyObject->pszIpsecOwnersReference
  1189. );
  1190. // BAIL_ON_WIN32_ERROR(dwError);
  1191. dwError = LdapDeleteS(
  1192. hLdapBindHandle,
  1193. pIpsecPolicyObject->pszIpsecOwnersReference
  1194. );
  1195. BAIL_ON_WIN32_ERROR(dwError);
  1196. error:
  1197. if (pIpsecPolicyObject) {
  1198. FreeIpsecPolicyObject(pIpsecPolicyObject);
  1199. }
  1200. return(dwError);
  1201. }
  1202. DWORD
  1203. ConvertGuidToDirPolicyString(
  1204. GUID PolicyIdentifier,
  1205. LPWSTR pszIpsecRootContainer,
  1206. LPWSTR * ppszIpsecPolicyReference
  1207. )
  1208. {
  1209. DWORD dwError = 0;
  1210. LPWSTR pszStringUuid = NULL;
  1211. WCHAR szGuidString[MAX_PATH];
  1212. WCHAR szPolicyReference[MAX_PATH];
  1213. LPWSTR pszIpsecPolicyReference = NULL;
  1214. dwError = UuidToString(
  1215. &PolicyIdentifier,
  1216. &pszStringUuid
  1217. );
  1218. BAIL_ON_WIN32_ERROR(dwError);
  1219. szGuidString[0] = L'\0';
  1220. wcscpy(szGuidString, L"{");
  1221. wcscat(szGuidString, pszStringUuid);
  1222. wcscat(szGuidString, L"}");
  1223. szPolicyReference[0] = L'\0';
  1224. wcscpy(szPolicyReference,L"CN=ipsecPolicy");
  1225. wcscat(szPolicyReference, szGuidString);
  1226. wcscat(szPolicyReference, L",");
  1227. wcscat(szPolicyReference, pszIpsecRootContainer);
  1228. pszIpsecPolicyReference = AllocPolStr(
  1229. szPolicyReference
  1230. );
  1231. if (!pszIpsecPolicyReference) {
  1232. dwError = ERROR_OUTOFMEMORY;
  1233. BAIL_ON_WIN32_ERROR(dwError);
  1234. }
  1235. *ppszIpsecPolicyReference = pszIpsecPolicyReference;
  1236. cleanup:
  1237. if (pszStringUuid) {
  1238. RpcStringFree(&pszStringUuid);
  1239. }
  1240. return(dwError);
  1241. error:
  1242. *ppszIpsecPolicyReference = NULL;
  1243. goto cleanup;
  1244. }
  1245. DWORD
  1246. DirGetPolicyData(
  1247. HLDAP hLdapBindHandle,
  1248. LPWSTR pszIpsecRootContainer,
  1249. GUID PolicyIdentifier,
  1250. PIPSEC_POLICY_DATA * ppIpsecPolicyData
  1251. )
  1252. {
  1253. DWORD dwError = 0;
  1254. LPWSTR pszPolicyString = NULL;
  1255. LDAPMessage * res = NULL;
  1256. DWORD dwCount = 0;
  1257. LDAPMessage * e = NULL;
  1258. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  1259. *ppIpsecPolicyData = NULL;
  1260. dwError = GenerateSpecificPolicyQuery(
  1261. PolicyIdentifier,
  1262. &pszPolicyString
  1263. );
  1264. BAIL_ON_WIN32_ERROR(dwError);
  1265. dwError = LdapSearchST(
  1266. hLdapBindHandle,
  1267. pszIpsecRootContainer,
  1268. LDAP_SCOPE_ONELEVEL,
  1269. pszPolicyString,
  1270. PolicyDNAttributes,
  1271. 0,
  1272. NULL,
  1273. &res
  1274. );
  1275. BAIL_ON_WIN32_ERROR(dwError);
  1276. dwCount = LdapCountEntries(
  1277. hLdapBindHandle,
  1278. res
  1279. );
  1280. if (!dwCount) {
  1281. dwError = ERROR_DS_NO_ATTRIBUTE_OR_VALUE;
  1282. BAIL_ON_WIN32_ERROR(dwError);
  1283. }
  1284. dwError = LdapFirstEntry(
  1285. hLdapBindHandle,
  1286. res,
  1287. &e
  1288. );
  1289. BAIL_ON_WIN32_ERROR(dwError);
  1290. dwError = UnMarshallPolicyObject2(
  1291. hLdapBindHandle,
  1292. e,
  1293. &pIpsecPolicyObject
  1294. );
  1295. BAIL_ON_WIN32_ERROR(dwError);
  1296. dwError = DirUnmarshallPolicyData(
  1297. pIpsecPolicyObject,
  1298. ppIpsecPolicyData
  1299. );
  1300. BAIL_ON_WIN32_ERROR(dwError);
  1301. error:
  1302. if (pszPolicyString) {
  1303. FreePolStr(pszPolicyString);
  1304. }
  1305. if (res) {
  1306. LdapMsgFree(res);
  1307. }
  1308. if (pIpsecPolicyObject) {
  1309. FreeIpsecPolicyObject(pIpsecPolicyObject);
  1310. }
  1311. return (dwError);
  1312. }