Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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