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.

2647 lines
67 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: dsstore.c
  7. //
  8. // Contents: Policy management for directory.
  9. //
  10. //
  11. // History: KrishnaG.
  12. // AbhisheV.
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "precomp.h"
  16. LPWSTR gpszIpSecContainer = L"CN=IP Security,CN=System,DC=ntdev,DC=microsoft,DC=com";
  17. LPWSTR PolicyDNAttributes[] = {
  18. L"ipsecID",
  19. L"description",
  20. L"ipsecDataType",
  21. L"ipsecISAKMPReference",
  22. L"ipsecData",
  23. L"ipsecNFAReference",
  24. L"ipsecName",
  25. L"distinguishedName",
  26. L"whenChanged",
  27. NULL
  28. };
  29. LPWSTR NFADNAttributes[] = {
  30. L"distinguishedName",
  31. L"description",
  32. L"ipsecName",
  33. L"ipsecID",
  34. L"ipsecDataType",
  35. L"ipsecData",
  36. L"ipsecOwnersReference",
  37. L"ipsecFilterReference",
  38. L"ipsecNegotiationPolicyReference",
  39. L"whenChanged",
  40. NULL
  41. };
  42. LPWSTR FilterDNAttributes[] = {
  43. L"distinguishedName",
  44. L"description",
  45. L"ipsecName",
  46. L"ipsecID",
  47. L"ipsecDataType",
  48. L"ipsecData",
  49. L"ipsecOwnersReference",
  50. L"whenChanged",
  51. NULL
  52. };
  53. LPWSTR NegPolDNAttributes[] = {
  54. L"distinguishedName",
  55. L"description",
  56. L"ipsecName",
  57. L"ipsecID",
  58. L"ipsecDataType",
  59. L"ipsecData",
  60. L"ipsecNegotiationPolicyAction",
  61. L"ipsecNegotiationPolicyType",
  62. L"ipsecOwnersReference",
  63. L"whenChanged",
  64. NULL
  65. };
  66. LPWSTR ISAKMPDNAttributes[] = {
  67. L"distinguishedName",
  68. L"ipsecName",
  69. L"ipsecID",
  70. L"ipsecDataType",
  71. L"ipsecData",
  72. L"ipsecOwnersReference",
  73. L"whenChanged",
  74. NULL
  75. };
  76. DWORD
  77. OpenDirectoryServerHandle(
  78. LPWSTR pszDomainName,
  79. DWORD dwPortNumber,
  80. HLDAP * phLdapBindHandle
  81. )
  82. {
  83. DWORD dwError = 0;
  84. *phLdapBindHandle = NULL;
  85. dwError = LdapOpen(
  86. pszDomainName,
  87. dwPortNumber,
  88. phLdapBindHandle
  89. );
  90. BAIL_ON_WIN32_ERROR(dwError);
  91. dwError = LdapBind(
  92. *phLdapBindHandle
  93. );
  94. BAIL_ON_WIN32_ERROR(dwError);
  95. return(dwError);
  96. error:
  97. if (*phLdapBindHandle) {
  98. CloseDirectoryServerHandle(
  99. *phLdapBindHandle
  100. );
  101. *phLdapBindHandle = NULL;
  102. }
  103. return(dwError);
  104. }
  105. DWORD
  106. CloseDirectoryServerHandle(
  107. HLDAP hLdapBindHandle
  108. )
  109. {
  110. int ldaperr = 0;
  111. if (hLdapBindHandle) {
  112. ldaperr = ldap_unbind(hLdapBindHandle);
  113. }
  114. return(0);
  115. }
  116. DWORD
  117. ReadPolicyObjectFromDirectory(
  118. HLDAP hLdapBindHandle,
  119. LPWSTR pszPolicyDN,
  120. PIPSEC_POLICY_OBJECT * ppIpsecPolicyObject
  121. )
  122. {
  123. LDAPMessage *res = NULL;
  124. LDAPMessage *e = NULL;
  125. LPWSTR szFilterString = L"(objectClass=*)";
  126. DWORD dwError = 0;
  127. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  128. DWORD dwNumNFAObjectsReturned = 0;
  129. PIPSEC_NFA_OBJECT * ppIpsecNFAObjects = NULL;
  130. LPWSTR * ppszFilterReferences = NULL;
  131. DWORD dwNumFilterReferences = 0;
  132. LPWSTR * ppszNegPolReferences = NULL;
  133. DWORD dwNumNegPolReferences = 0;
  134. PIPSEC_FILTER_OBJECT * ppIpsecFilterObjects = NULL;
  135. DWORD dwNumFilterObjects = 0;
  136. PIPSEC_NEGPOL_OBJECT * ppIpsecNegPolObjects = NULL;
  137. DWORD dwNumNegPolObjects = 0;
  138. PIPSEC_ISAKMP_OBJECT * ppIpsecISAKMPObjects = NULL;
  139. DWORD dwNumISAKMPObjects = 0;
  140. LPWSTR pszPolicyContainer = NULL;
  141. dwError = ComputePolicyContainerDN(
  142. pszPolicyDN,
  143. &pszPolicyContainer
  144. );
  145. BAIL_ON_WIN32_ERROR(dwError);
  146. dwError = LdapSearchST(
  147. hLdapBindHandle,
  148. pszPolicyDN,
  149. LDAP_SCOPE_BASE,
  150. szFilterString,
  151. PolicyDNAttributes,
  152. 0,
  153. NULL,
  154. &res
  155. );
  156. BAIL_ON_WIN32_ERROR(dwError);
  157. dwError = UnMarshallPolicyObject(
  158. hLdapBindHandle,
  159. pszPolicyDN,
  160. &pIpsecPolicyObject,
  161. res
  162. );
  163. BAIL_ON_WIN32_ERROR(dwError);
  164. dwError = ReadNFAObjectsFromDirectory(
  165. hLdapBindHandle,
  166. pszPolicyContainer,
  167. pIpsecPolicyObject->pszIpsecOwnersReference,
  168. pIpsecPolicyObject->ppszIpsecNFAReferences,
  169. pIpsecPolicyObject->NumberofRules,
  170. &ppIpsecNFAObjects,
  171. &dwNumNFAObjectsReturned,
  172. &ppszFilterReferences,
  173. &dwNumFilterReferences,
  174. &ppszNegPolReferences,
  175. &dwNumNegPolReferences
  176. );
  177. BAIL_ON_WIN32_ERROR(dwError);
  178. dwError = ReadFilterObjectsFromDirectory(
  179. hLdapBindHandle,
  180. pszPolicyContainer,
  181. ppszFilterReferences,
  182. dwNumFilterReferences,
  183. &ppIpsecFilterObjects,
  184. &dwNumFilterObjects
  185. );
  186. BAIL_ON_WIN32_ERROR(dwError);
  187. dwError = ReadNegPolObjectsFromDirectory(
  188. hLdapBindHandle,
  189. pszPolicyContainer,
  190. ppszNegPolReferences,
  191. dwNumNegPolReferences,
  192. &ppIpsecNegPolObjects,
  193. &dwNumNegPolObjects
  194. );
  195. BAIL_ON_WIN32_ERROR(dwError);
  196. dwError = ReadISAKMPObjectsFromDirectory(
  197. hLdapBindHandle,
  198. pszPolicyContainer,
  199. &pIpsecPolicyObject->pszIpsecISAKMPReference,
  200. 1,
  201. &ppIpsecISAKMPObjects,
  202. &dwNumISAKMPObjects
  203. );
  204. BAIL_ON_WIN32_ERROR(dwError);
  205. pIpsecPolicyObject->ppIpsecNFAObjects = ppIpsecNFAObjects;
  206. pIpsecPolicyObject->NumberofRulesReturned = dwNumNFAObjectsReturned;
  207. pIpsecPolicyObject->NumberofFilters = dwNumFilterObjects;
  208. pIpsecPolicyObject->ppIpsecFilterObjects = ppIpsecFilterObjects;
  209. pIpsecPolicyObject->ppIpsecNegPolObjects = ppIpsecNegPolObjects;
  210. pIpsecPolicyObject->NumberofNegPols = dwNumNegPolObjects;
  211. pIpsecPolicyObject->NumberofISAKMPs = dwNumISAKMPObjects;
  212. pIpsecPolicyObject->ppIpsecISAKMPObjects = ppIpsecISAKMPObjects;
  213. *ppIpsecPolicyObject = pIpsecPolicyObject;
  214. cleanup:
  215. if (res) {
  216. LdapMsgFree(res);
  217. }
  218. if (ppszFilterReferences) {
  219. FreeFilterReferences(
  220. ppszFilterReferences,
  221. dwNumFilterReferences
  222. );
  223. }
  224. if (ppszNegPolReferences) {
  225. FreeNegPolReferences(
  226. ppszNegPolReferences,
  227. dwNumNegPolReferences
  228. );
  229. }
  230. return(dwError);
  231. error:
  232. if (pIpsecPolicyObject) {
  233. FreeIpsecPolicyObject(
  234. pIpsecPolicyObject
  235. );
  236. }
  237. *ppIpsecPolicyObject = NULL;
  238. goto cleanup;
  239. }
  240. DWORD
  241. ReadNFAObjectsFromDirectory(
  242. HLDAP hLdapBindHandle,
  243. LPWSTR pszIpsecRootContainer,
  244. LPWSTR pszIpsecOwnerReference,
  245. LPWSTR * ppszNFADNs,
  246. DWORD dwNumNfaObjects,
  247. PIPSEC_NFA_OBJECT ** pppIpsecNFAObjects,
  248. PDWORD pdwNumNfaObjects,
  249. LPWSTR ** pppszFilterReferences,
  250. PDWORD pdwNumFilterReferences,
  251. LPWSTR ** pppszNegPolReferences,
  252. PDWORD pdwNumNegPolReferences
  253. )
  254. {
  255. LDAPMessage *res = NULL;
  256. LDAPMessage *e = NULL;
  257. DWORD dwError = 0;
  258. LPWSTR pszFilterString = NULL;
  259. DWORD i = 0;
  260. DWORD dwCount = 0;
  261. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  262. PIPSEC_NFA_OBJECT * ppIpsecNFAObjects = NULL;
  263. LPWSTR * ppszFilterReferences = NULL;
  264. LPWSTR * ppszNegPolReferences = NULL;
  265. LPWSTR pszFilterReference = NULL;
  266. LPWSTR pszNegPolReference = NULL;
  267. DWORD dwNumFilterReferences = 0;
  268. DWORD dwNumNegPolReferences = 0;
  269. DWORD dwNumNFAObjectsReturned = 0;
  270. dwError = GenerateNFAQuery(
  271. ppszNFADNs,
  272. dwNumNfaObjects,
  273. &pszFilterString
  274. );
  275. dwError = LdapSearchST(
  276. hLdapBindHandle,
  277. pszIpsecRootContainer,
  278. LDAP_SCOPE_ONELEVEL,
  279. pszFilterString,
  280. NFADNAttributes,
  281. 0,
  282. NULL,
  283. &res
  284. );
  285. BAIL_ON_WIN32_ERROR(dwError);
  286. dwCount = LdapCountEntries(
  287. hLdapBindHandle,
  288. res
  289. );
  290. if (!dwCount) {
  291. dwError = ERROR_DS_NO_ATTRIBUTE_OR_VALUE;
  292. BAIL_ON_WIN32_ERROR(dwError);
  293. }
  294. ppIpsecNFAObjects = (PIPSEC_NFA_OBJECT *)AllocPolMem(
  295. sizeof(PIPSEC_NFA_OBJECT)*dwCount
  296. );
  297. if (!ppIpsecNFAObjects) {
  298. dwError = ERROR_OUTOFMEMORY;
  299. BAIL_ON_WIN32_ERROR(dwError);
  300. }
  301. ppszFilterReferences = (LPWSTR *)AllocPolMem(
  302. sizeof(LPWSTR)*dwCount
  303. );
  304. if (!ppszFilterReferences) {
  305. dwError = ERROR_OUTOFMEMORY;
  306. BAIL_ON_WIN32_ERROR(dwError);
  307. }
  308. ppszNegPolReferences = (LPWSTR *)AllocPolMem(
  309. sizeof(LPWSTR)*dwCount
  310. );
  311. if (!ppszNegPolReferences) {
  312. dwError = ERROR_OUTOFMEMORY;
  313. BAIL_ON_WIN32_ERROR(dwError);
  314. }
  315. for (i = 0; i < dwCount; i++) {
  316. if (i == 0) {
  317. dwError = LdapFirstEntry(
  318. hLdapBindHandle,
  319. res,
  320. &e
  321. );
  322. BAIL_ON_WIN32_ERROR(dwError);
  323. }else {
  324. dwError = LdapNextEntry(
  325. hLdapBindHandle,
  326. e,
  327. &e
  328. );
  329. }
  330. dwError =UnMarshallNFAObject(
  331. hLdapBindHandle,
  332. e,
  333. &pIpsecNFAObject,
  334. &pszFilterReference,
  335. &pszNegPolReference
  336. );
  337. if (dwError == ERROR_SUCCESS) {
  338. *(ppIpsecNFAObjects + dwNumNFAObjectsReturned) = pIpsecNFAObject;
  339. if (pszFilterReference) {
  340. *(ppszFilterReferences + dwNumFilterReferences) = pszFilterReference;
  341. dwNumFilterReferences++;
  342. }
  343. if (pszNegPolReference) {
  344. *(ppszNegPolReferences + dwNumNegPolReferences) = pszNegPolReference;
  345. dwNumNegPolReferences++;
  346. }
  347. dwNumNFAObjectsReturned++;
  348. }
  349. }
  350. if (dwNumNFAObjectsReturned == 0) {
  351. dwError = ERROR_INVALID_DATA;
  352. BAIL_ON_WIN32_ERROR(dwError);
  353. }
  354. *pppszFilterReferences = ppszFilterReferences;
  355. *pppszNegPolReferences = ppszNegPolReferences;
  356. *pppIpsecNFAObjects = ppIpsecNFAObjects;
  357. *pdwNumNfaObjects = dwNumNFAObjectsReturned;
  358. *pdwNumNegPolReferences = dwNumNegPolReferences;
  359. *pdwNumFilterReferences = dwNumFilterReferences;
  360. dwError = ERROR_SUCCESS;
  361. cleanup:
  362. if (res) {
  363. LdapMsgFree(res);
  364. }
  365. if (pszFilterString) {
  366. FreePolStr(pszFilterString);
  367. }
  368. return(dwError);
  369. error:
  370. if (ppszNegPolReferences) {
  371. FreeNegPolReferences(
  372. ppszNegPolReferences,
  373. dwNumNFAObjectsReturned
  374. );
  375. }
  376. if (ppszFilterReferences) {
  377. FreeFilterReferences(
  378. ppszFilterReferences,
  379. dwNumNFAObjectsReturned
  380. );
  381. }
  382. if (ppIpsecNFAObjects) {
  383. FreeIpsecNFAObjects(
  384. ppIpsecNFAObjects,
  385. dwNumNFAObjectsReturned
  386. );
  387. }
  388. *pppszNegPolReferences = NULL;
  389. *pppszFilterReferences = NULL;
  390. *pdwNumNegPolReferences = 0;
  391. *pdwNumFilterReferences = 0;
  392. *pppIpsecNFAObjects = NULL;
  393. *pdwNumNfaObjects = 0;
  394. goto cleanup;
  395. }
  396. DWORD
  397. ReadFilterObjectsFromDirectory(
  398. HLDAP hLdapBindHandle,
  399. LPWSTR pszIpsecRootContainer,
  400. LPWSTR * ppszFilterDNs,
  401. DWORD dwNumFilterObjects,
  402. PIPSEC_FILTER_OBJECT ** pppIpsecFilterObjects,
  403. PDWORD pdwNumFilterObjects
  404. )
  405. {
  406. LDAPMessage *res = NULL;
  407. LDAPMessage *e = NULL;
  408. DWORD dwError = 0;
  409. LPWSTR pszFilterString = NULL;
  410. DWORD i = 0;
  411. DWORD dwCount = 0;
  412. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  413. PIPSEC_FILTER_OBJECT * ppIpsecFilterObjects = NULL;
  414. DWORD dwNumFilterObjectsReturned = 0;
  415. //
  416. // It is possible to have zero filter objects - if we have
  417. // a single rule with no filters in it, then we should return
  418. // success with zero filters.
  419. //
  420. if (!dwNumFilterObjects) {
  421. *pppIpsecFilterObjects = 0;
  422. *pdwNumFilterObjects = 0;
  423. return(ERROR_SUCCESS);
  424. }
  425. dwError = GenerateFilterQuery(
  426. ppszFilterDNs,
  427. dwNumFilterObjects,
  428. &pszFilterString
  429. );
  430. BAIL_ON_WIN32_ERROR(dwError);
  431. dwError = LdapSearchST(
  432. hLdapBindHandle,
  433. pszIpsecRootContainer,
  434. LDAP_SCOPE_ONELEVEL,
  435. pszFilterString,
  436. FilterDNAttributes,
  437. 0,
  438. NULL,
  439. &res
  440. );
  441. BAIL_ON_WIN32_ERROR(dwError);
  442. dwCount = LdapCountEntries(
  443. hLdapBindHandle,
  444. res
  445. );
  446. if (!dwCount) {
  447. dwError = ERROR_DS_NO_ATTRIBUTE_OR_VALUE;
  448. BAIL_ON_WIN32_ERROR(dwError);
  449. }
  450. ppIpsecFilterObjects = (PIPSEC_FILTER_OBJECT *)AllocPolMem(
  451. sizeof(PIPSEC_FILTER_OBJECT)*dwCount
  452. );
  453. if (!ppIpsecFilterObjects) {
  454. dwError = ERROR_OUTOFMEMORY;
  455. BAIL_ON_WIN32_ERROR(dwError);
  456. }
  457. for (i = 0; i < dwCount; i++) {
  458. if (i == 0) {
  459. dwError = LdapFirstEntry(
  460. hLdapBindHandle,
  461. res,
  462. &e
  463. );
  464. BAIL_ON_WIN32_ERROR(dwError);
  465. }else {
  466. dwError = LdapNextEntry(
  467. hLdapBindHandle,
  468. e,
  469. &e
  470. );
  471. }
  472. dwError =UnMarshallFilterObject(
  473. hLdapBindHandle,
  474. e,
  475. &pIpsecFilterObject
  476. );
  477. if (dwError == ERROR_SUCCESS) {
  478. *(ppIpsecFilterObjects + dwNumFilterObjectsReturned) = pIpsecFilterObject;
  479. dwNumFilterObjectsReturned++;
  480. }
  481. }
  482. *pppIpsecFilterObjects = ppIpsecFilterObjects;
  483. *pdwNumFilterObjects = dwNumFilterObjectsReturned;
  484. dwError = ERROR_SUCCESS;
  485. cleanup:
  486. if (pszFilterString) {
  487. FreePolMem(pszFilterString);
  488. }
  489. if (res) {
  490. LdapMsgFree(res);
  491. }
  492. return(dwError);
  493. error:
  494. if (ppIpsecFilterObjects) {
  495. FreeIpsecFilterObjects(
  496. ppIpsecFilterObjects,
  497. dwNumFilterObjectsReturned
  498. );
  499. }
  500. *pppIpsecFilterObjects = NULL;
  501. *pdwNumFilterObjects = 0;
  502. goto cleanup;
  503. }
  504. DWORD
  505. ReadNegPolObjectsFromDirectory(
  506. HLDAP hLdapBindHandle,
  507. LPWSTR pszIpsecRootContainer,
  508. LPWSTR * ppszNegPolDNs,
  509. DWORD dwNumNegPolObjects,
  510. PIPSEC_NEGPOL_OBJECT ** pppIpsecNegPolObjects,
  511. PDWORD pdwNumNegPolObjects
  512. )
  513. {
  514. LDAPMessage *res = NULL;
  515. LDAPMessage *e = NULL;
  516. DWORD dwError = 0;
  517. LPWSTR pszNegPolString = NULL;
  518. DWORD i = 0;
  519. DWORD dwCount = 0;
  520. PIPSEC_NEGPOL_OBJECT pIpsecNegPolObject = NULL;
  521. PIPSEC_NEGPOL_OBJECT * ppIpsecNegPolObjects = NULL;
  522. DWORD dwNumNegPolObjectsReturned = 0;
  523. dwError = GenerateNegPolQuery(
  524. ppszNegPolDNs,
  525. dwNumNegPolObjects,
  526. &pszNegPolString
  527. );
  528. BAIL_ON_WIN32_ERROR(dwError);
  529. dwError = LdapSearchST(
  530. hLdapBindHandle,
  531. pszIpsecRootContainer,
  532. LDAP_SCOPE_ONELEVEL,
  533. pszNegPolString,
  534. NegPolDNAttributes,
  535. 0,
  536. NULL,
  537. &res
  538. );
  539. BAIL_ON_WIN32_ERROR(dwError);
  540. dwCount = LdapCountEntries(
  541. hLdapBindHandle,
  542. res
  543. );
  544. if (!dwCount) {
  545. dwError = ERROR_DS_NO_ATTRIBUTE_OR_VALUE;
  546. BAIL_ON_WIN32_ERROR(dwError);
  547. }
  548. ppIpsecNegPolObjects = (PIPSEC_NEGPOL_OBJECT *)AllocPolMem(
  549. sizeof(PIPSEC_NEGPOL_OBJECT)*dwCount
  550. );
  551. if (!ppIpsecNegPolObjects) {
  552. dwError = ERROR_OUTOFMEMORY;
  553. BAIL_ON_WIN32_ERROR(dwError);
  554. }
  555. for (i = 0; i < dwCount; i++) {
  556. if (i == 0) {
  557. dwError = LdapFirstEntry(
  558. hLdapBindHandle,
  559. res,
  560. &e
  561. );
  562. BAIL_ON_WIN32_ERROR(dwError);
  563. }else {
  564. dwError = LdapNextEntry(
  565. hLdapBindHandle,
  566. e,
  567. &e
  568. );
  569. }
  570. dwError =UnMarshallNegPolObject(
  571. hLdapBindHandle,
  572. e,
  573. &pIpsecNegPolObject
  574. );
  575. if (dwError == ERROR_SUCCESS) {
  576. *(ppIpsecNegPolObjects + dwNumNegPolObjectsReturned) = pIpsecNegPolObject;
  577. dwNumNegPolObjectsReturned++;
  578. }
  579. }
  580. if (dwNumNegPolObjectsReturned == 0) {
  581. dwError = ERROR_INVALID_DATA;
  582. BAIL_ON_WIN32_ERROR(dwError);
  583. }
  584. *pppIpsecNegPolObjects = ppIpsecNegPolObjects;
  585. *pdwNumNegPolObjects = dwNumNegPolObjectsReturned;
  586. dwError = ERROR_SUCCESS;
  587. cleanup:
  588. if (pszNegPolString) {
  589. FreePolMem(pszNegPolString);
  590. }
  591. if (res) {
  592. LdapMsgFree(res);
  593. }
  594. return(dwError);
  595. error:
  596. if (ppIpsecNegPolObjects) {
  597. FreeIpsecNegPolObjects(
  598. ppIpsecNegPolObjects,
  599. dwNumNegPolObjectsReturned
  600. );
  601. }
  602. *pppIpsecNegPolObjects = NULL;
  603. *pdwNumNegPolObjects = 0;
  604. goto cleanup;
  605. }
  606. DWORD
  607. ReadISAKMPObjectsFromDirectory(
  608. HLDAP hLdapBindHandle,
  609. LPWSTR pszIpsecRootContainer,
  610. LPWSTR * ppszISAKMPDNs,
  611. DWORD dwNumISAKMPObjects,
  612. PIPSEC_ISAKMP_OBJECT ** pppIpsecISAKMPObjects,
  613. PDWORD pdwNumISAKMPObjects
  614. )
  615. {
  616. LDAPMessage *res = NULL;
  617. LDAPMessage *e = NULL;
  618. DWORD dwError = 0;
  619. LPWSTR pszISAKMPString = NULL;
  620. DWORD i = 0;
  621. DWORD dwCount = 0;
  622. PIPSEC_ISAKMP_OBJECT pIpsecISAKMPObject = NULL;
  623. PIPSEC_ISAKMP_OBJECT * ppIpsecISAKMPObjects = NULL;
  624. DWORD dwNumISAKMPObjectsReturned = 0;
  625. dwError = GenerateISAKMPQuery(
  626. ppszISAKMPDNs,
  627. dwNumISAKMPObjects,
  628. &pszISAKMPString
  629. );
  630. BAIL_ON_WIN32_ERROR(dwError);
  631. dwError = LdapSearchST(
  632. hLdapBindHandle,
  633. pszIpsecRootContainer,
  634. LDAP_SCOPE_ONELEVEL,
  635. pszISAKMPString,
  636. ISAKMPDNAttributes,
  637. 0,
  638. NULL,
  639. &res
  640. );
  641. BAIL_ON_WIN32_ERROR(dwError);
  642. dwCount = LdapCountEntries(
  643. hLdapBindHandle,
  644. res
  645. );
  646. if (!dwCount) {
  647. dwError = ERROR_DS_NO_ATTRIBUTE_OR_VALUE;
  648. BAIL_ON_WIN32_ERROR(dwError);
  649. }
  650. ppIpsecISAKMPObjects = (PIPSEC_ISAKMP_OBJECT *)AllocPolMem(
  651. sizeof(PIPSEC_ISAKMP_OBJECT)*dwCount
  652. );
  653. if (!ppIpsecISAKMPObjects) {
  654. dwError = ERROR_OUTOFMEMORY;
  655. BAIL_ON_WIN32_ERROR(dwError);
  656. }
  657. for (i = 0; i < dwCount; i++) {
  658. if (i == 0) {
  659. dwError = LdapFirstEntry(
  660. hLdapBindHandle,
  661. res,
  662. &e
  663. );
  664. BAIL_ON_WIN32_ERROR(dwError);
  665. }else {
  666. dwError = LdapNextEntry(
  667. hLdapBindHandle,
  668. e,
  669. &e
  670. );
  671. }
  672. dwError =UnMarshallISAKMPObject(
  673. hLdapBindHandle,
  674. e,
  675. &pIpsecISAKMPObject
  676. );
  677. if (dwError == ERROR_SUCCESS) {
  678. *(ppIpsecISAKMPObjects + dwNumISAKMPObjectsReturned) = pIpsecISAKMPObject;
  679. dwNumISAKMPObjectsReturned++;
  680. }
  681. }
  682. if (dwNumISAKMPObjectsReturned == 0) {
  683. dwError = ERROR_INVALID_DATA;
  684. BAIL_ON_WIN32_ERROR(dwError);
  685. }
  686. *pppIpsecISAKMPObjects = ppIpsecISAKMPObjects;
  687. *pdwNumISAKMPObjects = dwNumISAKMPObjectsReturned;
  688. dwError = ERROR_SUCCESS;
  689. cleanup:
  690. if (pszISAKMPString) {
  691. FreePolMem(pszISAKMPString);
  692. }
  693. if (res) {
  694. LdapMsgFree(res);
  695. }
  696. return(dwError);
  697. error:
  698. if (ppIpsecISAKMPObjects) {
  699. FreeIpsecISAKMPObjects(
  700. ppIpsecISAKMPObjects,
  701. dwNumISAKMPObjectsReturned
  702. );
  703. }
  704. *pppIpsecISAKMPObjects = NULL;
  705. *pdwNumISAKMPObjects = 0;
  706. goto cleanup;
  707. }
  708. DWORD
  709. UnMarshallPolicyObject(
  710. HLDAP hLdapBindHandle,
  711. LPWSTR pszPolicyDN,
  712. PIPSEC_POLICY_OBJECT * ppIpsecPolicyObject,
  713. LDAPMessage *res
  714. )
  715. {
  716. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  717. DWORD dwCount = 0;
  718. DWORD dwLen = 0;
  719. LPBYTE pBuffer = NULL;
  720. DWORD i = 0;
  721. DWORD dwError = 0;
  722. LDAPMessage *e = NULL;
  723. WCHAR **strvalues = NULL;
  724. struct berval ** bvalues = NULL;
  725. LPWSTR * ppszIpsecNFANames = NULL;
  726. LPWSTR pszIpsecNFAName = NULL;
  727. LPWSTR * ppszTemp = NULL;
  728. pIpsecPolicyObject = (PIPSEC_POLICY_OBJECT)AllocPolMem(
  729. sizeof(IPSEC_POLICY_OBJECT)
  730. );
  731. if (!pIpsecPolicyObject) {
  732. dwError = ERROR_OUTOFMEMORY;
  733. BAIL_ON_WIN32_ERROR(dwError);
  734. }
  735. dwError = LdapFirstEntry(
  736. hLdapBindHandle,
  737. res,
  738. &e
  739. );
  740. BAIL_ON_WIN32_ERROR(dwError);
  741. /*
  742. strvalues = NULL;
  743. dwError = LdapGetValues(
  744. hLdapBindHandle,
  745. e,
  746. L"distinguishedName",
  747. (WCHAR ***)&strvalues,
  748. (int *)&dwCount
  749. );
  750. BAIL_ON_WIN32_ERROR(dwError);
  751. */
  752. pIpsecPolicyObject->pszIpsecOwnersReference = AllocPolStr(
  753. pszPolicyDN
  754. );
  755. if (!pIpsecPolicyObject->pszIpsecOwnersReference) {
  756. dwError = ERROR_OUTOFMEMORY;
  757. BAIL_ON_WIN32_ERROR(dwError);
  758. }
  759. strvalues = NULL;
  760. dwError = LdapGetValues(
  761. hLdapBindHandle,
  762. e,
  763. L"ipsecName",
  764. (WCHAR ***)&strvalues,
  765. (int *)&dwCount
  766. );
  767. BAIL_ON_WIN32_ERROR(dwError);
  768. pIpsecPolicyObject->pszIpsecName = AllocPolStr(
  769. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  770. );
  771. if (!pIpsecPolicyObject->pszIpsecName) {
  772. dwError = ERROR_OUTOFMEMORY;
  773. BAIL_ON_WIN32_ERROR(dwError);
  774. }
  775. LdapValueFree(strvalues);
  776. strvalues = NULL;
  777. dwError = LdapGetValues(
  778. hLdapBindHandle,
  779. e,
  780. L"description",
  781. (WCHAR ***)&strvalues,
  782. (int *)&dwCount
  783. );
  784. // BAIL_ON_WIN32_ERROR(dwError);
  785. if (strvalues && LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)) {
  786. pIpsecPolicyObject->pszDescription = AllocPolStr(
  787. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  788. );
  789. if (!pIpsecPolicyObject->pszDescription) {
  790. dwError = ERROR_OUTOFMEMORY;
  791. BAIL_ON_WIN32_ERROR(dwError);
  792. }
  793. LdapValueFree(strvalues);
  794. } else {
  795. pIpsecPolicyObject->pszDescription = NULL;
  796. }
  797. strvalues = NULL;
  798. dwError = LdapGetValues(
  799. hLdapBindHandle,
  800. e,
  801. L"ipsecID",
  802. (WCHAR ***)&strvalues,
  803. (int *)&dwCount
  804. );
  805. BAIL_ON_WIN32_ERROR(dwError);
  806. pIpsecPolicyObject->pszIpsecID = AllocPolStr(
  807. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  808. );
  809. if (!pIpsecPolicyObject->pszIpsecID) {
  810. dwError = ERROR_OUTOFMEMORY;
  811. BAIL_ON_WIN32_ERROR(dwError);
  812. }
  813. LdapValueFree(strvalues);
  814. strvalues = NULL;
  815. dwError = LdapGetValues(
  816. hLdapBindHandle,
  817. e,
  818. L"ipsecDataType",
  819. (WCHAR ***)&strvalues,
  820. (int *)&dwCount
  821. );
  822. BAIL_ON_WIN32_ERROR(dwError);
  823. pIpsecPolicyObject->dwIpsecDataType = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  824. LdapValueFree(strvalues);
  825. strvalues = NULL;
  826. dwError = LdapGetValues(
  827. hLdapBindHandle,
  828. e,
  829. L"whenChanged",
  830. (WCHAR ***)&strvalues,
  831. (int *)&dwCount
  832. );
  833. BAIL_ON_WIN32_ERROR(dwError);
  834. pIpsecPolicyObject->dwWhenChanged = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  835. LdapValueFree(strvalues);
  836. strvalues = NULL;
  837. dwError = LdapGetValues(
  838. hLdapBindHandle,
  839. e,
  840. L"ipsecISAKMPReference",
  841. (WCHAR ***)&strvalues,
  842. (int *)&dwCount
  843. );
  844. BAIL_ON_WIN32_ERROR(dwError);
  845. pIpsecPolicyObject->pszIpsecISAKMPReference = AllocPolStr(
  846. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  847. );
  848. if (!pIpsecPolicyObject->pszIpsecISAKMPReference) {
  849. dwError = ERROR_OUTOFMEMORY;
  850. BAIL_ON_WIN32_ERROR(dwError);
  851. }
  852. LdapValueFree(strvalues);
  853. //
  854. // unmarshall the ipsecData blob
  855. //
  856. dwError = LdapGetValuesLen(
  857. hLdapBindHandle,
  858. e,
  859. L"ipsecData",
  860. (struct berval ***)&bvalues,
  861. (int *)&dwCount
  862. );
  863. BAIL_ON_WIN32_ERROR(dwError);
  864. dwLen = LDAPOBJECT_BERVAL_LEN((PLDAPOBJECT)bvalues);
  865. pBuffer = (LPBYTE)AllocPolMem(dwLen);
  866. if (!pBuffer) {
  867. dwError = ERROR_OUTOFMEMORY;
  868. BAIL_ON_WIN32_ERROR(dwError);
  869. }
  870. memcpy( pBuffer, LDAPOBJECT_BERVAL_VAL((PLDAPOBJECT)bvalues), dwLen );
  871. pIpsecPolicyObject->pIpsecData = pBuffer;
  872. pIpsecPolicyObject->dwIpsecDataLen = dwLen;
  873. LdapValueFreeLen(bvalues);
  874. strvalues = NULL;
  875. dwError = LdapGetValues(
  876. hLdapBindHandle,
  877. e,
  878. L"ipsecNFAReference",
  879. (WCHAR ***)&strvalues,
  880. (int *)&dwCount
  881. );
  882. BAIL_ON_WIN32_ERROR(dwError);
  883. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  884. sizeof(LPWSTR)*dwCount
  885. );
  886. if (!ppszIpsecNFANames) {
  887. dwError = ERROR_OUTOFMEMORY;
  888. BAIL_ON_WIN32_ERROR(dwError);
  889. }
  890. for (i = 0; i < dwCount; i++) {
  891. ppszTemp = (strvalues + i);
  892. //
  893. // Unmarshall all the values you can possibly have
  894. //
  895. pszIpsecNFAName = AllocPolStr(*ppszTemp);
  896. if (!pszIpsecNFAName) {
  897. dwError = ERROR_OUTOFMEMORY;
  898. pIpsecPolicyObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  899. pIpsecPolicyObject->NumberofRules = i;
  900. BAIL_ON_WIN32_ERROR(dwError);
  901. }
  902. *(ppszIpsecNFANames + i) = pszIpsecNFAName;
  903. }
  904. pIpsecPolicyObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  905. pIpsecPolicyObject->NumberofRules = dwCount;
  906. LdapValueFree(strvalues);
  907. *ppIpsecPolicyObject = pIpsecPolicyObject;
  908. return(dwError);
  909. error:
  910. if (pIpsecPolicyObject) {
  911. FreeIpsecPolicyObject(pIpsecPolicyObject);
  912. }
  913. *ppIpsecPolicyObject = NULL;
  914. return(dwError);
  915. }
  916. DWORD
  917. UnMarshallNFAObject(
  918. HLDAP hLdapBindHandle,
  919. LDAPMessage *e,
  920. PIPSEC_NFA_OBJECT * ppIpsecNFAObject,
  921. LPWSTR * ppszFilterReference,
  922. LPWSTR * ppszNegPolReference
  923. )
  924. {
  925. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  926. DWORD dwCount = 0;
  927. DWORD dwLen = 0;
  928. LPBYTE pBuffer = NULL;
  929. DWORD i = 0;
  930. DWORD dwError = 0;
  931. WCHAR **strvalues = NULL;
  932. struct berval ** bvalues = NULL;
  933. LPWSTR * ppszIpsecNFANames = NULL;
  934. LPWSTR pszIpsecNFAName = NULL;
  935. LPWSTR * ppszTemp = NULL;
  936. LPWSTR pszTempFilterReference = NULL;
  937. LPWSTR pszTempNegPolReference = NULL;
  938. pIpsecNFAObject = (PIPSEC_NFA_OBJECT)AllocPolMem(
  939. sizeof(IPSEC_NFA_OBJECT)
  940. );
  941. if (!pIpsecNFAObject) {
  942. dwError = ERROR_OUTOFMEMORY;
  943. BAIL_ON_WIN32_ERROR(dwError);
  944. }
  945. strvalues = NULL;
  946. dwError = LdapGetValues(
  947. hLdapBindHandle,
  948. e,
  949. L"distinguishedName",
  950. (WCHAR ***)&strvalues,
  951. (int *)&dwCount
  952. );
  953. BAIL_ON_WIN32_ERROR(dwError);
  954. pIpsecNFAObject->pszDistinguishedName = AllocPolStr(
  955. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  956. );
  957. if (!pIpsecNFAObject->pszDistinguishedName) {
  958. dwError = ERROR_OUTOFMEMORY;
  959. BAIL_ON_WIN32_ERROR(dwError);
  960. }
  961. LdapValueFree(strvalues);
  962. strvalues = NULL;
  963. dwError = LdapGetValues(
  964. hLdapBindHandle,
  965. e,
  966. L"ipsecName",
  967. (WCHAR ***)&strvalues,
  968. (int *)&dwCount
  969. );
  970. //
  971. // Client does not always write the Name for an NFA.
  972. //
  973. // BAIL_ON_WIN32_ERROR(dwError);
  974. if (strvalues && LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)) {
  975. pIpsecNFAObject->pszIpsecName = AllocPolStr(
  976. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  977. );
  978. if (!pIpsecNFAObject->pszIpsecName) {
  979. dwError = ERROR_OUTOFMEMORY;
  980. BAIL_ON_WIN32_ERROR(dwError);
  981. }
  982. LdapValueFree(strvalues);
  983. } else {
  984. pIpsecNFAObject->pszIpsecName = NULL;
  985. }
  986. strvalues = NULL;
  987. dwError = LdapGetValues(
  988. hLdapBindHandle,
  989. e,
  990. L"description",
  991. (WCHAR ***)&strvalues,
  992. (int *)&dwCount
  993. );
  994. // BAIL_ON_WIN32_ERROR(dwError);
  995. if (strvalues && LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)) {
  996. pIpsecNFAObject->pszDescription = AllocPolStr(
  997. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  998. );
  999. if (!pIpsecNFAObject->pszDescription) {
  1000. dwError = ERROR_OUTOFMEMORY;
  1001. BAIL_ON_WIN32_ERROR(dwError);
  1002. }
  1003. LdapValueFree(strvalues);
  1004. } else {
  1005. pIpsecNFAObject->pszDescription = NULL;
  1006. }
  1007. strvalues = NULL;
  1008. dwError = LdapGetValues(
  1009. hLdapBindHandle,
  1010. e,
  1011. L"ipsecID",
  1012. (WCHAR ***)&strvalues,
  1013. (int *)&dwCount
  1014. );
  1015. BAIL_ON_WIN32_ERROR(dwError);
  1016. pIpsecNFAObject->pszIpsecID = AllocPolStr(
  1017. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1018. );
  1019. if (!pIpsecNFAObject->pszIpsecID) {
  1020. dwError = ERROR_OUTOFMEMORY;
  1021. BAIL_ON_WIN32_ERROR(dwError);
  1022. }
  1023. LdapValueFree(strvalues);
  1024. strvalues = NULL;
  1025. dwError = LdapGetValues(
  1026. hLdapBindHandle,
  1027. e,
  1028. L"ipsecDataType",
  1029. (WCHAR ***)&strvalues,
  1030. (int *)&dwCount
  1031. );
  1032. BAIL_ON_WIN32_ERROR(dwError);
  1033. pIpsecNFAObject->dwIpsecDataType = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  1034. LdapValueFree(strvalues);
  1035. strvalues = NULL;
  1036. dwError = LdapGetValues(
  1037. hLdapBindHandle,
  1038. e,
  1039. L"whenChanged",
  1040. (WCHAR ***)&strvalues,
  1041. (int *)&dwCount
  1042. );
  1043. BAIL_ON_WIN32_ERROR(dwError);
  1044. pIpsecNFAObject->dwWhenChanged = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  1045. LdapValueFree(strvalues);
  1046. //
  1047. // unmarshall the ipsecData blob
  1048. //
  1049. dwError = LdapGetValuesLen(
  1050. hLdapBindHandle,
  1051. e,
  1052. L"ipsecData",
  1053. (struct berval ***)&bvalues,
  1054. (int *)&dwCount
  1055. );
  1056. BAIL_ON_WIN32_ERROR(dwError);
  1057. dwLen = LDAPOBJECT_BERVAL_LEN((PLDAPOBJECT)bvalues);
  1058. pBuffer = (LPBYTE)AllocPolMem(dwLen);
  1059. if (!pBuffer) {
  1060. dwError = ERROR_OUTOFMEMORY;
  1061. BAIL_ON_WIN32_ERROR(dwError);
  1062. }
  1063. memcpy( pBuffer, LDAPOBJECT_BERVAL_VAL((PLDAPOBJECT)bvalues), dwLen );
  1064. pIpsecNFAObject->pIpsecData = pBuffer;
  1065. pIpsecNFAObject->dwIpsecDataLen = dwLen;
  1066. LdapValueFreeLen(bvalues);
  1067. strvalues = NULL;
  1068. dwError = LdapGetValues(
  1069. hLdapBindHandle,
  1070. e,
  1071. L"ipsecOwnersReference",
  1072. (WCHAR ***)&strvalues,
  1073. (int *)&dwCount
  1074. );
  1075. //BAIL_ON_WIN32_ERROR(dwError);
  1076. if (!dwError && strvalues) {
  1077. pIpsecNFAObject->pszIpsecOwnersReference = AllocPolStr(
  1078. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1079. );
  1080. if (!pIpsecNFAObject->pszIpsecOwnersReference) {
  1081. dwError = ERROR_OUTOFMEMORY;
  1082. BAIL_ON_WIN32_ERROR(dwError);
  1083. }
  1084. LdapValueFree(strvalues);
  1085. }
  1086. strvalues = NULL;
  1087. dwError = LdapGetValues(
  1088. hLdapBindHandle,
  1089. e,
  1090. L"ipsecNegotiationPolicyReference",
  1091. (WCHAR ***)&strvalues,
  1092. (int *)&dwCount
  1093. );
  1094. BAIL_ON_WIN32_ERROR(dwError);
  1095. pIpsecNFAObject->pszIpsecNegPolReference = AllocPolStr(
  1096. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1097. );
  1098. if (!pIpsecNFAObject->pszIpsecNegPolReference) {
  1099. dwError = ERROR_OUTOFMEMORY;
  1100. BAIL_ON_WIN32_ERROR(dwError);
  1101. }
  1102. LdapValueFree(strvalues);
  1103. strvalues = NULL;
  1104. dwError = LdapGetValues(
  1105. hLdapBindHandle,
  1106. e,
  1107. L"ipsecFilterReference",
  1108. (WCHAR ***)&strvalues,
  1109. (int *)&dwCount
  1110. );
  1111. // BAIL_ON_WIN32_ERROR(dwError);
  1112. if (strvalues) {
  1113. pIpsecNFAObject->pszIpsecFilterReference = AllocPolStr(
  1114. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1115. );
  1116. if (!pIpsecNFAObject->pszIpsecFilterReference) {
  1117. dwError = ERROR_OUTOFMEMORY;
  1118. BAIL_ON_WIN32_ERROR(dwError);
  1119. }
  1120. pszTempFilterReference = AllocPolStr(
  1121. pIpsecNFAObject->pszIpsecFilterReference
  1122. );
  1123. if (!pszTempFilterReference) {
  1124. dwError = ERROR_OUTOFMEMORY;
  1125. BAIL_ON_WIN32_ERROR(dwError);
  1126. }
  1127. }
  1128. else {
  1129. pIpsecNFAObject->pszIpsecFilterReference = NULL;
  1130. }
  1131. pszTempNegPolReference = AllocPolStr(
  1132. pIpsecNFAObject->pszIpsecNegPolReference
  1133. );
  1134. if (!pszTempNegPolReference) {
  1135. dwError = ERROR_OUTOFMEMORY;
  1136. BAIL_ON_WIN32_ERROR(dwError);
  1137. }
  1138. LdapValueFree(strvalues);
  1139. *ppszFilterReference = pszTempFilterReference;
  1140. *ppszNegPolReference = pszTempNegPolReference;
  1141. *ppIpsecNFAObject = pIpsecNFAObject;
  1142. return(0);
  1143. error:
  1144. if (pIpsecNFAObject) {
  1145. FreeIpsecNFAObject(pIpsecNFAObject);
  1146. }
  1147. if (pszTempFilterReference) {
  1148. FreePolStr(pszTempFilterReference);
  1149. }
  1150. if (pszTempNegPolReference) {
  1151. FreePolStr(pszTempNegPolReference);
  1152. }
  1153. *ppIpsecNFAObject = NULL;
  1154. *ppszFilterReference = NULL;
  1155. *ppszNegPolReference = NULL;
  1156. return(dwError);
  1157. }
  1158. DWORD
  1159. UnMarshallFilterObject(
  1160. HLDAP hLdapBindHandle,
  1161. LDAPMessage *e,
  1162. PIPSEC_FILTER_OBJECT * ppIpsecFilterObject
  1163. )
  1164. {
  1165. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  1166. DWORD dwCount = 0;
  1167. DWORD dwLen = 0;
  1168. LPBYTE pBuffer = NULL;
  1169. DWORD i = 0;
  1170. DWORD dwError = 0;
  1171. WCHAR **strvalues = NULL;
  1172. struct berval ** bvalues = NULL;
  1173. LPWSTR * ppszIpsecFilterNames = NULL;
  1174. LPWSTR pszIpsecFilterName = NULL;
  1175. LPWSTR * ppszTemp = NULL;
  1176. LPWSTR * ppszIpsecNFANames = NULL;
  1177. LPWSTR pszIpsecNFAName = NULL;
  1178. pIpsecFilterObject = (PIPSEC_FILTER_OBJECT)AllocPolMem(
  1179. sizeof(IPSEC_FILTER_OBJECT)
  1180. );
  1181. if (!pIpsecFilterObject) {
  1182. dwError = ERROR_OUTOFMEMORY;
  1183. BAIL_ON_WIN32_ERROR(dwError);
  1184. }
  1185. strvalues = NULL;
  1186. dwError = LdapGetValues(
  1187. hLdapBindHandle,
  1188. e,
  1189. L"distinguishedName",
  1190. (WCHAR ***)&strvalues,
  1191. (int *)&dwCount
  1192. );
  1193. BAIL_ON_WIN32_ERROR(dwError);
  1194. pIpsecFilterObject->pszDistinguishedName = AllocPolStr(
  1195. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1196. );
  1197. if (!pIpsecFilterObject->pszDistinguishedName) {
  1198. dwError = ERROR_OUTOFMEMORY;
  1199. BAIL_ON_WIN32_ERROR(dwError);
  1200. }
  1201. LdapValueFree(strvalues);
  1202. strvalues = NULL;
  1203. dwError = LdapGetValues(
  1204. hLdapBindHandle,
  1205. e,
  1206. L"description",
  1207. (WCHAR ***)&strvalues,
  1208. (int *)&dwCount
  1209. );
  1210. // BAIL_ON_WIN32_ERROR(dwError);
  1211. if (!dwError && strvalues) {
  1212. pIpsecFilterObject->pszDescription = AllocPolStr(
  1213. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1214. );
  1215. if (!pIpsecFilterObject->pszDescription) {
  1216. dwError = ERROR_OUTOFMEMORY;
  1217. BAIL_ON_WIN32_ERROR(dwError);
  1218. }
  1219. LdapValueFree(strvalues);
  1220. }
  1221. strvalues = NULL;
  1222. dwError = LdapGetValues(
  1223. hLdapBindHandle,
  1224. e,
  1225. L"ipsecName",
  1226. (WCHAR ***)&strvalues,
  1227. (int *)&dwCount
  1228. );
  1229. // BAIL_ON_WIN32_ERROR(dwError);
  1230. if (!dwError && strvalues) {
  1231. pIpsecFilterObject->pszIpsecName = AllocPolStr(
  1232. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1233. );
  1234. if (!pIpsecFilterObject->pszIpsecName) {
  1235. dwError = ERROR_OUTOFMEMORY;
  1236. BAIL_ON_WIN32_ERROR(dwError);
  1237. }
  1238. LdapValueFree(strvalues);
  1239. }
  1240. strvalues = NULL;
  1241. dwError = LdapGetValues(
  1242. hLdapBindHandle,
  1243. e,
  1244. L"ipsecID",
  1245. (WCHAR ***)&strvalues,
  1246. (int *)&dwCount
  1247. );
  1248. BAIL_ON_WIN32_ERROR(dwError);
  1249. pIpsecFilterObject->pszIpsecID = AllocPolStr(
  1250. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1251. );
  1252. if (!pIpsecFilterObject->pszIpsecID) {
  1253. dwError = ERROR_OUTOFMEMORY;
  1254. BAIL_ON_WIN32_ERROR(dwError);
  1255. }
  1256. LdapValueFree(strvalues);
  1257. strvalues = NULL;
  1258. dwError = LdapGetValues(
  1259. hLdapBindHandle,
  1260. e,
  1261. L"ipsecDataType",
  1262. (WCHAR ***)&strvalues,
  1263. (int *)&dwCount
  1264. );
  1265. BAIL_ON_WIN32_ERROR(dwError);
  1266. pIpsecFilterObject->dwIpsecDataType = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  1267. LdapValueFree(strvalues);
  1268. strvalues = NULL;
  1269. dwError = LdapGetValues(
  1270. hLdapBindHandle,
  1271. e,
  1272. L"whenChanged",
  1273. (WCHAR ***)&strvalues,
  1274. (int *)&dwCount
  1275. );
  1276. BAIL_ON_WIN32_ERROR(dwError);
  1277. pIpsecFilterObject->dwWhenChanged = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  1278. LdapValueFree(strvalues);
  1279. //
  1280. // unmarshall the ipsecData blob
  1281. //
  1282. dwError = LdapGetValuesLen(
  1283. hLdapBindHandle,
  1284. e,
  1285. L"ipsecData",
  1286. (struct berval ***)&bvalues,
  1287. (int *)&dwCount
  1288. );
  1289. BAIL_ON_WIN32_ERROR(dwError);
  1290. dwLen = LDAPOBJECT_BERVAL_LEN((PLDAPOBJECT)bvalues);
  1291. pBuffer = (LPBYTE)AllocPolMem(dwLen);
  1292. if (!pBuffer) {
  1293. dwError = ERROR_OUTOFMEMORY;
  1294. BAIL_ON_WIN32_ERROR(dwError);
  1295. }
  1296. memcpy( pBuffer, LDAPOBJECT_BERVAL_VAL((PLDAPOBJECT)bvalues), dwLen );
  1297. pIpsecFilterObject->pIpsecData = pBuffer;
  1298. pIpsecFilterObject->dwIpsecDataLen = dwLen;
  1299. LdapValueFreeLen(bvalues);
  1300. strvalues = NULL;
  1301. dwError = LdapGetValues(
  1302. hLdapBindHandle,
  1303. e,
  1304. L"ipsecOwnersReference",
  1305. (WCHAR ***)&strvalues,
  1306. (int *)&dwCount
  1307. );
  1308. if (!dwError && strvalues) {
  1309. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  1310. sizeof(LPWSTR)*dwCount
  1311. );
  1312. if (!ppszIpsecNFANames) {
  1313. dwError = ERROR_OUTOFMEMORY;
  1314. BAIL_ON_WIN32_ERROR(dwError);
  1315. }
  1316. for (i = 0; i < dwCount; i++) {
  1317. ppszTemp = (strvalues + i);
  1318. //
  1319. // Unmarshall all the values you can possibly have
  1320. //
  1321. pszIpsecNFAName = AllocPolStr(*ppszTemp);
  1322. if (!pszIpsecNFAName) {
  1323. dwError = ERROR_OUTOFMEMORY;
  1324. pIpsecFilterObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  1325. pIpsecFilterObject->dwNFACount = i;
  1326. BAIL_ON_WIN32_ERROR(dwError);
  1327. }
  1328. *(ppszIpsecNFANames + i) = pszIpsecNFAName;
  1329. }
  1330. pIpsecFilterObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  1331. pIpsecFilterObject->dwNFACount = dwCount;
  1332. LdapValueFree(strvalues);
  1333. }
  1334. *ppIpsecFilterObject = pIpsecFilterObject;
  1335. return(0);
  1336. error:
  1337. if (pIpsecFilterObject) {
  1338. FreeIpsecFilterObject(pIpsecFilterObject);
  1339. }
  1340. *ppIpsecFilterObject = NULL;
  1341. return(dwError);
  1342. }
  1343. DWORD
  1344. UnMarshallNegPolObject(
  1345. HLDAP hLdapBindHandle,
  1346. LDAPMessage *e,
  1347. PIPSEC_NEGPOL_OBJECT * ppIpsecPolicyObject
  1348. )
  1349. {
  1350. PIPSEC_NEGPOL_OBJECT pIpsecPolicyObject = NULL;
  1351. DWORD dwCount = 0;
  1352. DWORD dwLen = 0;
  1353. LPBYTE pBuffer = NULL;
  1354. DWORD i = 0;
  1355. DWORD dwError = 0;
  1356. WCHAR **strvalues = NULL;
  1357. struct berval ** bvalues = NULL;
  1358. LPWSTR * ppszIpsecNFANames = NULL;
  1359. LPWSTR pszIpsecNFAName = NULL;
  1360. LPWSTR * ppszTemp = NULL;
  1361. pIpsecPolicyObject = (PIPSEC_NEGPOL_OBJECT)AllocPolMem(
  1362. sizeof(IPSEC_NEGPOL_OBJECT)
  1363. );
  1364. if (!pIpsecPolicyObject) {
  1365. dwError = ERROR_OUTOFMEMORY;
  1366. BAIL_ON_WIN32_ERROR(dwError);
  1367. }
  1368. strvalues = NULL;
  1369. dwError = LdapGetValues(
  1370. hLdapBindHandle,
  1371. e,
  1372. L"distinguishedName",
  1373. (WCHAR ***)&strvalues,
  1374. (int *)&dwCount
  1375. );
  1376. BAIL_ON_WIN32_ERROR(dwError);
  1377. pIpsecPolicyObject->pszDistinguishedName = AllocPolStr(
  1378. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1379. );
  1380. if (!pIpsecPolicyObject->pszDistinguishedName) {
  1381. dwError = ERROR_OUTOFMEMORY;
  1382. BAIL_ON_WIN32_ERROR(dwError);
  1383. }
  1384. LdapValueFree(strvalues);
  1385. strvalues = NULL;
  1386. dwError = LdapGetValues(
  1387. hLdapBindHandle,
  1388. e,
  1389. L"ipsecName",
  1390. (WCHAR ***)&strvalues,
  1391. (int *)&dwCount
  1392. );
  1393. // BAIL_ON_WIN32_ERROR(dwError);
  1394. //
  1395. // Names do not get written on an NegPol Object.
  1396. //
  1397. if (strvalues && LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)) {
  1398. pIpsecPolicyObject->pszIpsecName = AllocPolStr(
  1399. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1400. );
  1401. if (!pIpsecPolicyObject->pszIpsecName) {
  1402. dwError = ERROR_OUTOFMEMORY;
  1403. BAIL_ON_WIN32_ERROR(dwError);
  1404. }
  1405. LdapValueFree(strvalues);
  1406. } else {
  1407. pIpsecPolicyObject->pszIpsecName = NULL;
  1408. }
  1409. strvalues = NULL;
  1410. dwError = LdapGetValues(
  1411. hLdapBindHandle,
  1412. e,
  1413. L"description",
  1414. (WCHAR ***)&strvalues,
  1415. (int *)&dwCount
  1416. );
  1417. // BAIL_ON_WIN32_ERROR(dwError);
  1418. if (strvalues && LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)) {
  1419. pIpsecPolicyObject->pszDescription = AllocPolStr(
  1420. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1421. );
  1422. if (!pIpsecPolicyObject->pszDescription) {
  1423. dwError = ERROR_OUTOFMEMORY;
  1424. BAIL_ON_WIN32_ERROR(dwError);
  1425. }
  1426. LdapValueFree(strvalues);
  1427. } else {
  1428. pIpsecPolicyObject->pszDescription = NULL;
  1429. }
  1430. strvalues = NULL;
  1431. dwError = LdapGetValues(
  1432. hLdapBindHandle,
  1433. e,
  1434. L"ipsecID",
  1435. (WCHAR ***)&strvalues,
  1436. (int *)&dwCount
  1437. );
  1438. BAIL_ON_WIN32_ERROR(dwError);
  1439. pIpsecPolicyObject->pszIpsecID = AllocPolStr(
  1440. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1441. );
  1442. if (!pIpsecPolicyObject->pszIpsecID) {
  1443. dwError = ERROR_OUTOFMEMORY;
  1444. BAIL_ON_WIN32_ERROR(dwError);
  1445. }
  1446. LdapValueFree(strvalues);
  1447. strvalues = NULL;
  1448. dwError = LdapGetValues(
  1449. hLdapBindHandle,
  1450. e,
  1451. L"ipsecNegotiationPolicyAction",
  1452. (WCHAR ***)&strvalues,
  1453. (int *)&dwCount
  1454. );
  1455. BAIL_ON_WIN32_ERROR(dwError);
  1456. pIpsecPolicyObject->pszIpsecNegPolAction = AllocPolStr(
  1457. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1458. );
  1459. if (!pIpsecPolicyObject->pszIpsecNegPolAction) {
  1460. dwError = ERROR_OUTOFMEMORY;
  1461. BAIL_ON_WIN32_ERROR(dwError);
  1462. }
  1463. LdapValueFree(strvalues);
  1464. strvalues = NULL;
  1465. dwError = LdapGetValues(
  1466. hLdapBindHandle,
  1467. e,
  1468. L"ipsecNegotiationPolicyType",
  1469. (WCHAR ***)&strvalues,
  1470. (int *)&dwCount
  1471. );
  1472. BAIL_ON_WIN32_ERROR(dwError);
  1473. pIpsecPolicyObject->pszIpsecNegPolType = AllocPolStr(
  1474. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1475. );
  1476. if (!pIpsecPolicyObject->pszIpsecNegPolType) {
  1477. dwError = ERROR_OUTOFMEMORY;
  1478. BAIL_ON_WIN32_ERROR(dwError);
  1479. }
  1480. LdapValueFree(strvalues);
  1481. strvalues = NULL;
  1482. dwError = LdapGetValues(
  1483. hLdapBindHandle,
  1484. e,
  1485. L"ipsecOwnersReference",
  1486. (WCHAR ***)&strvalues,
  1487. (int *)&dwCount
  1488. );
  1489. if (!dwError && strvalues) {
  1490. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  1491. sizeof(LPWSTR)*dwCount
  1492. );
  1493. if (!ppszIpsecNFANames) {
  1494. dwError = ERROR_OUTOFMEMORY;
  1495. BAIL_ON_WIN32_ERROR(dwError);
  1496. }
  1497. for (i = 0; i < dwCount; i++) {
  1498. ppszTemp = (strvalues + i);
  1499. //
  1500. // Unmarshall all the values you can possibly have
  1501. //
  1502. pszIpsecNFAName = AllocPolStr(*ppszTemp);
  1503. if (!pszIpsecNFAName) {
  1504. dwError = ERROR_OUTOFMEMORY;
  1505. pIpsecPolicyObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  1506. pIpsecPolicyObject->dwNFACount = i;
  1507. BAIL_ON_WIN32_ERROR(dwError);
  1508. }
  1509. *(ppszIpsecNFANames + i) = pszIpsecNFAName;
  1510. }
  1511. pIpsecPolicyObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  1512. pIpsecPolicyObject->dwNFACount = dwCount;
  1513. LdapValueFree(strvalues);
  1514. }
  1515. strvalues = NULL;
  1516. dwError = LdapGetValues(
  1517. hLdapBindHandle,
  1518. e,
  1519. L"ipsecDataType",
  1520. (WCHAR ***)&strvalues,
  1521. (int *)&dwCount
  1522. );
  1523. BAIL_ON_WIN32_ERROR(dwError);
  1524. pIpsecPolicyObject->dwIpsecDataType = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  1525. LdapValueFree(strvalues);
  1526. strvalues = NULL;
  1527. dwError = LdapGetValues(
  1528. hLdapBindHandle,
  1529. e,
  1530. L"whenChanged",
  1531. (WCHAR ***)&strvalues,
  1532. (int *)&dwCount
  1533. );
  1534. BAIL_ON_WIN32_ERROR(dwError);
  1535. pIpsecPolicyObject->dwWhenChanged = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  1536. LdapValueFree(strvalues);
  1537. //
  1538. // unmarshall the ipsecData blob
  1539. //
  1540. dwError = LdapGetValuesLen(
  1541. hLdapBindHandle,
  1542. e,
  1543. L"ipsecData",
  1544. (struct berval ***)&bvalues,
  1545. (int *)&dwCount
  1546. );
  1547. BAIL_ON_WIN32_ERROR(dwError);
  1548. dwLen = LDAPOBJECT_BERVAL_LEN((PLDAPOBJECT)bvalues);
  1549. pBuffer = (LPBYTE)AllocPolMem(dwLen);
  1550. if (!pBuffer) {
  1551. dwError = ERROR_OUTOFMEMORY;
  1552. BAIL_ON_WIN32_ERROR(dwError);
  1553. }
  1554. memcpy( pBuffer, LDAPOBJECT_BERVAL_VAL((PLDAPOBJECT)bvalues), dwLen );
  1555. pIpsecPolicyObject->pIpsecData = pBuffer;
  1556. pIpsecPolicyObject->dwIpsecDataLen = dwLen;
  1557. LdapValueFreeLen(bvalues);
  1558. *ppIpsecPolicyObject = pIpsecPolicyObject;
  1559. return(0);
  1560. error:
  1561. if (pIpsecPolicyObject) {
  1562. FreeIpsecNegPolObject(pIpsecPolicyObject);
  1563. }
  1564. *ppIpsecPolicyObject = NULL;
  1565. return(dwError);
  1566. }
  1567. DWORD
  1568. UnMarshallISAKMPObject(
  1569. HLDAP hLdapBindHandle,
  1570. LDAPMessage *e,
  1571. PIPSEC_ISAKMP_OBJECT * ppIpsecISAKMPObject
  1572. )
  1573. {
  1574. PIPSEC_ISAKMP_OBJECT pIpsecISAKMPObject = NULL;
  1575. DWORD dwCount = 0;
  1576. DWORD dwLen = 0;
  1577. LPBYTE pBuffer = NULL;
  1578. DWORD i = 0;
  1579. DWORD dwError = 0;
  1580. WCHAR **strvalues = NULL;
  1581. struct berval ** bvalues = NULL;
  1582. LPWSTR * ppszIpsecISAKMPNames = NULL;
  1583. LPWSTR pszIpsecISAKMPName = NULL;
  1584. LPWSTR * ppszTemp = NULL;
  1585. LPWSTR * ppszIpsecNFANames = NULL;
  1586. LPWSTR pszIpsecNFAName = NULL;
  1587. pIpsecISAKMPObject = (PIPSEC_ISAKMP_OBJECT)AllocPolMem(
  1588. sizeof(IPSEC_ISAKMP_OBJECT)
  1589. );
  1590. if (!pIpsecISAKMPObject) {
  1591. dwError = ERROR_OUTOFMEMORY;
  1592. BAIL_ON_WIN32_ERROR(dwError);
  1593. }
  1594. strvalues = NULL;
  1595. dwError = LdapGetValues(
  1596. hLdapBindHandle,
  1597. e,
  1598. L"distinguishedName",
  1599. (WCHAR ***)&strvalues,
  1600. (int *)&dwCount
  1601. );
  1602. BAIL_ON_WIN32_ERROR(dwError);
  1603. pIpsecISAKMPObject->pszDistinguishedName = AllocPolStr(
  1604. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1605. );
  1606. if (!pIpsecISAKMPObject->pszDistinguishedName) {
  1607. dwError = ERROR_OUTOFMEMORY;
  1608. BAIL_ON_WIN32_ERROR(dwError);
  1609. }
  1610. LdapValueFree(strvalues);
  1611. strvalues = NULL;
  1612. dwError = LdapGetValues(
  1613. hLdapBindHandle,
  1614. e,
  1615. L"ipsecName",
  1616. (WCHAR ***)&strvalues,
  1617. (int *)&dwCount
  1618. );
  1619. // BAIL_ON_WIN32_ERROR(dwError);
  1620. //
  1621. // Names are not set for ISAKMP objects.
  1622. //
  1623. if (strvalues && LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)) {
  1624. pIpsecISAKMPObject->pszIpsecName = AllocPolStr(
  1625. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1626. );
  1627. if (!pIpsecISAKMPObject->pszIpsecName) {
  1628. dwError = ERROR_OUTOFMEMORY;
  1629. BAIL_ON_WIN32_ERROR(dwError);
  1630. }
  1631. LdapValueFree(strvalues);
  1632. } else {
  1633. pIpsecISAKMPObject->pszIpsecName = NULL;
  1634. }
  1635. strvalues = NULL;
  1636. dwError = LdapGetValues(
  1637. hLdapBindHandle,
  1638. e,
  1639. L"ipsecID",
  1640. (WCHAR ***)&strvalues,
  1641. (int *)&dwCount
  1642. );
  1643. BAIL_ON_WIN32_ERROR(dwError);
  1644. pIpsecISAKMPObject->pszIpsecID = AllocPolStr(
  1645. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  1646. );
  1647. if (!pIpsecISAKMPObject->pszIpsecID) {
  1648. dwError = ERROR_OUTOFMEMORY;
  1649. BAIL_ON_WIN32_ERROR(dwError);
  1650. }
  1651. LdapValueFree(strvalues);
  1652. strvalues = NULL;
  1653. dwError = LdapGetValues(
  1654. hLdapBindHandle,
  1655. e,
  1656. L"ipsecDataType",
  1657. (WCHAR ***)&strvalues,
  1658. (int *)&dwCount
  1659. );
  1660. BAIL_ON_WIN32_ERROR(dwError);
  1661. pIpsecISAKMPObject->dwIpsecDataType = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  1662. LdapValueFree(strvalues);
  1663. strvalues = NULL;
  1664. dwError = LdapGetValues(
  1665. hLdapBindHandle,
  1666. e,
  1667. L"whenChanged",
  1668. (WCHAR ***)&strvalues,
  1669. (int *)&dwCount
  1670. );
  1671. BAIL_ON_WIN32_ERROR(dwError);
  1672. pIpsecISAKMPObject->dwWhenChanged = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  1673. LdapValueFree(strvalues);
  1674. //
  1675. // unmarshall the ipsecData blob
  1676. //
  1677. dwError = LdapGetValuesLen(
  1678. hLdapBindHandle,
  1679. e,
  1680. L"ipsecData",
  1681. (struct berval ***)&bvalues,
  1682. (int *)&dwCount
  1683. );
  1684. BAIL_ON_WIN32_ERROR(dwError);
  1685. dwLen = LDAPOBJECT_BERVAL_LEN((PLDAPOBJECT)bvalues);
  1686. pBuffer = (LPBYTE)AllocPolMem(dwLen);
  1687. if (!pBuffer) {
  1688. dwError = ERROR_OUTOFMEMORY;
  1689. BAIL_ON_WIN32_ERROR(dwError);
  1690. }
  1691. memcpy( pBuffer, LDAPOBJECT_BERVAL_VAL((PLDAPOBJECT)bvalues), dwLen );
  1692. pIpsecISAKMPObject->pIpsecData = pBuffer;
  1693. pIpsecISAKMPObject->dwIpsecDataLen = dwLen;
  1694. LdapValueFreeLen(bvalues);
  1695. strvalues = NULL;
  1696. dwError = LdapGetValues(
  1697. hLdapBindHandle,
  1698. e,
  1699. L"ipsecOwnersReference",
  1700. (WCHAR ***)&strvalues,
  1701. (int *)&dwCount
  1702. );
  1703. // BAIL_ON_WIN32_ERROR(dwError);
  1704. //
  1705. // ipsecOwnersReference not written.
  1706. //
  1707. if (!dwError && strvalues) {
  1708. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  1709. sizeof(LPWSTR)*dwCount
  1710. );
  1711. if (!ppszIpsecNFANames) {
  1712. dwError = ERROR_OUTOFMEMORY;
  1713. BAIL_ON_WIN32_ERROR(dwError);
  1714. }
  1715. for (i = 0; i < dwCount; i++) {
  1716. ppszTemp = (strvalues + i);
  1717. //
  1718. // Unmarshall all the values you can possibly have
  1719. //
  1720. pszIpsecNFAName = AllocPolStr(*ppszTemp);
  1721. if (!pszIpsecNFAName) {
  1722. dwError = ERROR_OUTOFMEMORY;
  1723. pIpsecISAKMPObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  1724. pIpsecISAKMPObject->dwNFACount = i;
  1725. BAIL_ON_WIN32_ERROR(dwError);
  1726. }
  1727. *(ppszIpsecNFANames + i) = pszIpsecNFAName;
  1728. }
  1729. pIpsecISAKMPObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  1730. pIpsecISAKMPObject->dwNFACount = dwCount;
  1731. LdapValueFree(strvalues);
  1732. }
  1733. *ppIpsecISAKMPObject = pIpsecISAKMPObject;
  1734. return(0);
  1735. error:
  1736. if (pIpsecISAKMPObject) {
  1737. FreeIpsecISAKMPObject(pIpsecISAKMPObject);
  1738. }
  1739. *ppIpsecISAKMPObject = NULL;
  1740. return(dwError);
  1741. }
  1742. DWORD
  1743. GenerateFilterQuery(
  1744. LPWSTR * ppszFilterDNs,
  1745. DWORD dwNumFilterObjects,
  1746. LPWSTR * ppszQueryBuffer
  1747. )
  1748. {
  1749. DWORD i = 0;
  1750. WCHAR szCommonName[512];
  1751. DWORD dwError = 0;
  1752. DWORD dwLength = 0;
  1753. LPWSTR pszQueryBuffer = NULL;
  1754. //
  1755. // Compute Length of Buffer to be allocated
  1756. //
  1757. dwLength = wcslen(L"(&(objectclass=ipsecFilter)");
  1758. dwLength += wcslen(L"(|");
  1759. for (i = 0; i < dwNumFilterObjects; i++) {
  1760. dwError = ComputePrelimCN(
  1761. *(ppszFilterDNs + i),
  1762. szCommonName
  1763. );
  1764. dwLength += wcslen(L"(");
  1765. dwLength += wcslen(szCommonName);
  1766. dwLength += wcslen( L")");
  1767. }
  1768. dwLength += wcslen(L")");
  1769. dwLength += wcslen(L")");
  1770. pszQueryBuffer = (LPWSTR)AllocPolMem((dwLength + 1)*sizeof(WCHAR));
  1771. if (!pszQueryBuffer) {
  1772. dwError = ERROR_OUTOFMEMORY;
  1773. BAIL_ON_WIN32_ERROR(dwError);
  1774. }
  1775. //
  1776. // Now fill in the buffer
  1777. //
  1778. wcscpy(pszQueryBuffer,L"(&(objectclass=ipsecFilter)");
  1779. wcscat(pszQueryBuffer, L"(|");
  1780. for (i = 0; i < dwNumFilterObjects; i++) {
  1781. dwError = ComputePrelimCN(
  1782. *(ppszFilterDNs + i),
  1783. szCommonName
  1784. );
  1785. wcscat(pszQueryBuffer, L"(");
  1786. wcscat(pszQueryBuffer, szCommonName);
  1787. wcscat(pszQueryBuffer, L")");
  1788. }
  1789. wcscat(pszQueryBuffer, L")");
  1790. wcscat(pszQueryBuffer, L")");
  1791. *ppszQueryBuffer = pszQueryBuffer;
  1792. return(0);
  1793. error:
  1794. if (pszQueryBuffer) {
  1795. FreePolMem(pszQueryBuffer);
  1796. }
  1797. *ppszQueryBuffer = NULL;
  1798. return(dwError);
  1799. }
  1800. DWORD
  1801. GenerateNegPolQuery(
  1802. LPWSTR * ppszNegPolDNs,
  1803. DWORD dwNumNegPolObjects,
  1804. LPWSTR * ppszQueryBuffer
  1805. )
  1806. {
  1807. DWORD i = 0;
  1808. WCHAR szCommonName[512];
  1809. DWORD dwError = 0;
  1810. DWORD dwLength = 0;
  1811. LPWSTR pszQueryBuffer = NULL;
  1812. //
  1813. // Compute Length of Buffer to be allocated
  1814. //
  1815. dwLength = wcslen(L"(&(objectclass=ipsecNegotiationPolicy)");
  1816. dwLength += wcslen(L"(|");
  1817. for (i = 0; i < dwNumNegPolObjects; i++) {
  1818. dwError = ComputePrelimCN(
  1819. *(ppszNegPolDNs + i),
  1820. szCommonName
  1821. );
  1822. dwLength += wcslen(L"(");
  1823. dwLength += wcslen(szCommonName);
  1824. dwLength += wcslen( L")");
  1825. }
  1826. dwLength += wcslen(L")");
  1827. dwLength += wcslen(L")");
  1828. pszQueryBuffer = (LPWSTR)AllocPolMem((dwLength + 1)*sizeof(WCHAR));
  1829. if (!pszQueryBuffer) {
  1830. dwError = ERROR_OUTOFMEMORY;
  1831. BAIL_ON_WIN32_ERROR(dwError);
  1832. }
  1833. //
  1834. // Now fill in the buffer
  1835. //
  1836. wcscpy(pszQueryBuffer,L"(&(objectclass=ipsecNegotiationPolicy)");
  1837. wcscat(pszQueryBuffer, L"(|");
  1838. for (i = 0; i < dwNumNegPolObjects; i++) {
  1839. dwError = ComputePrelimCN(
  1840. *(ppszNegPolDNs + i),
  1841. szCommonName
  1842. );
  1843. wcscat(pszQueryBuffer, L"(");
  1844. wcscat(pszQueryBuffer, szCommonName);
  1845. wcscat(pszQueryBuffer, L")");
  1846. }
  1847. wcscat(pszQueryBuffer, L")");
  1848. wcscat(pszQueryBuffer, L")");
  1849. *ppszQueryBuffer = pszQueryBuffer;
  1850. return(0);
  1851. error:
  1852. if (pszQueryBuffer) {
  1853. FreePolMem(pszQueryBuffer);
  1854. }
  1855. *ppszQueryBuffer = NULL;
  1856. return(dwError);
  1857. }
  1858. DWORD
  1859. GenerateNFAQuery(
  1860. LPWSTR * ppszNFADNs,
  1861. DWORD dwNumNFAObjects,
  1862. LPWSTR * ppszQueryBuffer
  1863. )
  1864. {
  1865. DWORD i = 0;
  1866. WCHAR szCommonName[512];
  1867. DWORD dwError = 0;
  1868. DWORD dwLength = 0;
  1869. LPWSTR pszQueryBuffer = NULL;
  1870. //
  1871. // Compute Length of Buffer to be allocated
  1872. //
  1873. dwLength = wcslen(L"(&(objectclass=ipsecNFA)");
  1874. dwLength += wcslen(L"(|");
  1875. for (i = 0; i < dwNumNFAObjects; i++) {
  1876. dwError = ComputePrelimCN(
  1877. *(ppszNFADNs + i),
  1878. szCommonName
  1879. );
  1880. dwLength += wcslen(L"(");
  1881. dwLength += wcslen(szCommonName);
  1882. dwLength += wcslen( L")");
  1883. }
  1884. dwLength += wcslen(L")");
  1885. dwLength += wcslen(L")");
  1886. pszQueryBuffer = (LPWSTR)AllocPolMem((dwLength + 1)*sizeof(WCHAR));
  1887. if (!pszQueryBuffer) {
  1888. dwError = ERROR_OUTOFMEMORY;
  1889. BAIL_ON_WIN32_ERROR(dwError);
  1890. }
  1891. //
  1892. // Now fill in the buffer
  1893. //
  1894. wcscpy(pszQueryBuffer,L"(&(objectclass=ipsecNFA)");
  1895. wcscat(pszQueryBuffer, L"(|");
  1896. for (i = 0; i < dwNumNFAObjects; i++) {
  1897. dwError = ComputePrelimCN(
  1898. *(ppszNFADNs + i),
  1899. szCommonName
  1900. );
  1901. wcscat(pszQueryBuffer, L"(");
  1902. wcscat(pszQueryBuffer, szCommonName);
  1903. wcscat(pszQueryBuffer, L")");
  1904. }
  1905. wcscat(pszQueryBuffer, L")");
  1906. wcscat(pszQueryBuffer, L")");
  1907. *ppszQueryBuffer = pszQueryBuffer;
  1908. return(0);
  1909. error:
  1910. if (pszQueryBuffer) {
  1911. FreePolMem(pszQueryBuffer);
  1912. }
  1913. *ppszQueryBuffer = NULL;
  1914. return(dwError);
  1915. }
  1916. DWORD
  1917. GenerateISAKMPQuery(
  1918. LPWSTR * ppszISAKMPDNs,
  1919. DWORD dwNumISAKMPObjects,
  1920. LPWSTR * ppszQueryBuffer
  1921. )
  1922. {
  1923. DWORD i = 0;
  1924. WCHAR szCommonName[512];
  1925. DWORD dwError = 0;
  1926. DWORD dwLength = 0;
  1927. LPWSTR pszQueryBuffer = NULL;
  1928. //
  1929. // Compute Length of Buffer to be allocated
  1930. //
  1931. dwLength = wcslen(L"(&(objectclass=ipsecISAKMPPolicy)");
  1932. dwLength += wcslen(L"(|");
  1933. for (i = 0; i < dwNumISAKMPObjects; i++) {
  1934. dwError = ComputePrelimCN(
  1935. *(ppszISAKMPDNs + i),
  1936. szCommonName
  1937. );
  1938. dwLength += wcslen(L"(");
  1939. dwLength += wcslen(szCommonName);
  1940. dwLength += wcslen( L")");
  1941. }
  1942. dwLength += wcslen(L")");
  1943. dwLength += wcslen(L")");
  1944. pszQueryBuffer = (LPWSTR)AllocPolMem((dwLength + 1)*sizeof(WCHAR));
  1945. if (!pszQueryBuffer) {
  1946. dwError = ERROR_OUTOFMEMORY;
  1947. BAIL_ON_WIN32_ERROR(dwError);
  1948. }
  1949. //
  1950. // Now fill in the buffer
  1951. //
  1952. wcscpy(pszQueryBuffer,L"(&(objectclass=ipsecISAKMPPolicy)");
  1953. wcscat(pszQueryBuffer, L"(|");
  1954. for (i = 0; i < dwNumISAKMPObjects; i++) {
  1955. dwError = ComputePrelimCN(
  1956. *(ppszISAKMPDNs + i),
  1957. szCommonName
  1958. );
  1959. wcscat(pszQueryBuffer, L"(");
  1960. wcscat(pszQueryBuffer, szCommonName);
  1961. wcscat(pszQueryBuffer, L")");
  1962. }
  1963. wcscat(pszQueryBuffer, L")");
  1964. wcscat(pszQueryBuffer, L")");
  1965. *ppszQueryBuffer = pszQueryBuffer;
  1966. return(0);
  1967. error:
  1968. if (pszQueryBuffer) {
  1969. FreePolMem(pszQueryBuffer);
  1970. }
  1971. *ppszQueryBuffer = NULL;
  1972. return(dwError);
  1973. }
  1974. DWORD
  1975. ComputePrelimCN(
  1976. LPWSTR szNFADN,
  1977. LPWSTR szCommonName
  1978. )
  1979. {
  1980. LPWSTR pszComma = NULL;
  1981. pszComma = wcschr(szNFADN, L',');
  1982. if (!pszComma) {
  1983. return (ERROR_INVALID_DATA);
  1984. }
  1985. *pszComma = L'\0';
  1986. wcscpy(szCommonName, szNFADN);
  1987. *pszComma = L',';
  1988. return(0);
  1989. }
  1990. DWORD
  1991. ComputePolicyContainerDN(
  1992. LPWSTR pszPolicyDN,
  1993. LPWSTR * ppszPolicyContainerDN
  1994. )
  1995. {
  1996. LPWSTR pszComma = NULL;
  1997. LPWSTR pszPolicyContainer = NULL;
  1998. DWORD dwError = 0;
  1999. *ppszPolicyContainerDN = NULL;
  2000. pszComma = wcschr(pszPolicyDN, L',');
  2001. pszPolicyContainer = AllocPolStr(
  2002. pszComma + 1
  2003. );
  2004. if (!pszPolicyContainer) {
  2005. dwError = ERROR_OUTOFMEMORY;
  2006. BAIL_ON_WIN32_ERROR(dwError);
  2007. }
  2008. *ppszPolicyContainerDN = pszPolicyContainer;
  2009. error:
  2010. return(dwError);
  2011. }
  2012. DWORD
  2013. ComputeDefaultDirectory(
  2014. LPWSTR * ppszDefaultDirectory
  2015. )
  2016. {
  2017. PDOMAIN_CONTROLLER_INFOW pDomainControllerInfo = NULL;
  2018. DWORD dwError = 0;
  2019. DWORD Flags = DS_DIRECTORY_SERVICE_REQUIRED | DS_RETURN_DNS_NAME;
  2020. LPWSTR pszDefaultDirectory = NULL;
  2021. *ppszDefaultDirectory = NULL;
  2022. dwError = DsGetDcNameW(
  2023. NULL,
  2024. NULL,
  2025. NULL,
  2026. NULL,
  2027. Flags,
  2028. &pDomainControllerInfo
  2029. ) ;
  2030. BAIL_ON_WIN32_ERROR(dwError);
  2031. pszDefaultDirectory = AllocPolStr(
  2032. pDomainControllerInfo->DomainName
  2033. );
  2034. if (!pszDefaultDirectory) {
  2035. dwError = ERROR_OUTOFMEMORY;
  2036. BAIL_ON_WIN32_ERROR(dwError);
  2037. }
  2038. *ppszDefaultDirectory = pszDefaultDirectory;
  2039. error:
  2040. if (pDomainControllerInfo) {
  2041. (void) NetApiBufferFree(pDomainControllerInfo) ;
  2042. }
  2043. return(dwError);
  2044. }