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.

1218 lines
27 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: filters-d.c
  7. //
  8. // Contents: Filter Management for directory.
  9. //
  10. //
  11. // History: KrishnaG
  12. // AbhisheV
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "precomp.h"
  16. extern LPWSTR FilterDNAttributes[];
  17. DWORD
  18. DirEnumFilterData(
  19. HLDAP hLdapBindHandle,
  20. LPWSTR pszIpsecRootContainer,
  21. PIPSEC_FILTER_DATA ** pppIpsecFilterData,
  22. PDWORD pdwNumFilterObjects
  23. )
  24. {
  25. DWORD dwError = 0;
  26. PIPSEC_FILTER_OBJECT * ppIpsecFilterObjects = NULL;
  27. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  28. PIPSEC_FILTER_DATA * ppIpsecFilterData = NULL;
  29. DWORD dwNumFilterObjects = 0;
  30. DWORD i = 0;
  31. DWORD j = 0;
  32. dwError = DirEnumFilterObjects(
  33. hLdapBindHandle,
  34. pszIpsecRootContainer,
  35. &ppIpsecFilterObjects,
  36. &dwNumFilterObjects
  37. );
  38. BAIL_ON_WIN32_ERROR(dwError);
  39. if (dwNumFilterObjects) {
  40. ppIpsecFilterData = (PIPSEC_FILTER_DATA *) AllocPolMem(
  41. dwNumFilterObjects*sizeof(PIPSEC_FILTER_DATA)
  42. );
  43. if (!ppIpsecFilterData) {
  44. dwError = ERROR_OUTOFMEMORY;
  45. BAIL_ON_WIN32_ERROR(dwError);
  46. }
  47. }
  48. for (i = 0; i < dwNumFilterObjects; i++) {
  49. dwError = DirUnmarshallFilterData(
  50. *(ppIpsecFilterObjects + i),
  51. &pIpsecFilterData
  52. );
  53. if (!dwError) {
  54. *(ppIpsecFilterData + j) = pIpsecFilterData;
  55. j++;
  56. }
  57. }
  58. if (j == 0) {
  59. if (ppIpsecFilterData) {
  60. FreePolMem(ppIpsecFilterData);
  61. ppIpsecFilterData = NULL;
  62. }
  63. }
  64. *pppIpsecFilterData = ppIpsecFilterData;
  65. *pdwNumFilterObjects = j;
  66. dwError = ERROR_SUCCESS;
  67. cleanup:
  68. if (ppIpsecFilterObjects) {
  69. FreeIpsecFilterObjects(
  70. ppIpsecFilterObjects,
  71. dwNumFilterObjects
  72. );
  73. }
  74. return(dwError);
  75. error:
  76. if (ppIpsecFilterData) {
  77. FreeMulIpsecFilterData(
  78. ppIpsecFilterData,
  79. i
  80. );
  81. }
  82. *pppIpsecFilterData = NULL;
  83. *pdwNumFilterObjects = 0;
  84. goto cleanup;
  85. }
  86. DWORD
  87. DirEnumFilterObjects(
  88. HLDAP hLdapBindHandle,
  89. LPWSTR pszIpsecRootContainer,
  90. PIPSEC_FILTER_OBJECT ** pppIpsecFilterObjects,
  91. PDWORD pdwNumFilterObjects
  92. )
  93. {
  94. LDAPMessage *res = NULL;
  95. LDAPMessage *e = NULL;
  96. DWORD dwError = 0;
  97. LPWSTR pszFilterString = NULL;
  98. DWORD i = 0;
  99. DWORD dwCount = 0;
  100. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  101. PIPSEC_FILTER_OBJECT * ppIpsecFilterObjects = NULL;
  102. DWORD dwNumFilterObjectsReturned = 0;
  103. dwError = GenerateAllFiltersQuery(
  104. &pszFilterString
  105. );
  106. BAIL_ON_WIN32_ERROR(dwError);
  107. dwError = LdapSearchST(
  108. hLdapBindHandle,
  109. pszIpsecRootContainer,
  110. LDAP_SCOPE_ONELEVEL,
  111. pszFilterString,
  112. FilterDNAttributes,
  113. 0,
  114. NULL,
  115. &res
  116. );
  117. BAIL_ON_WIN32_ERROR(dwError);
  118. dwCount = LdapCountEntries(
  119. hLdapBindHandle,
  120. res
  121. );
  122. if (!dwCount) {
  123. dwError = ERROR_DS_NO_ATTRIBUTE_OR_VALUE;
  124. BAIL_ON_WIN32_ERROR(dwError);
  125. }
  126. ppIpsecFilterObjects = (PIPSEC_FILTER_OBJECT *)AllocPolMem(
  127. sizeof(PIPSEC_FILTER_OBJECT)*dwCount
  128. );
  129. if (!ppIpsecFilterObjects) {
  130. dwError = ERROR_OUTOFMEMORY;
  131. BAIL_ON_WIN32_ERROR(dwError);
  132. }
  133. for (i = 0; i < dwCount; i++) {
  134. if (i == 0) {
  135. dwError = LdapFirstEntry(
  136. hLdapBindHandle,
  137. res,
  138. &e
  139. );
  140. BAIL_ON_WIN32_ERROR(dwError);
  141. } else {
  142. dwError = LdapNextEntry(
  143. hLdapBindHandle,
  144. e,
  145. &e
  146. );
  147. BAIL_ON_WIN32_ERROR(dwError);
  148. }
  149. dwError = UnMarshallFilterObject(
  150. hLdapBindHandle,
  151. e,
  152. &pIpsecFilterObject
  153. );
  154. if (dwError == ERROR_SUCCESS) {
  155. *(ppIpsecFilterObjects + dwNumFilterObjectsReturned) = pIpsecFilterObject;
  156. dwNumFilterObjectsReturned++;
  157. }
  158. }
  159. *pppIpsecFilterObjects = ppIpsecFilterObjects;
  160. *pdwNumFilterObjects = dwNumFilterObjectsReturned;
  161. dwError = ERROR_SUCCESS;
  162. cleanup:
  163. if (pszFilterString) {
  164. FreePolMem(pszFilterString);
  165. }
  166. if (res) {
  167. LdapMsgFree(res);
  168. }
  169. return(dwError);
  170. error:
  171. if (ppIpsecFilterObjects) {
  172. FreeIpsecFilterObjects(
  173. ppIpsecFilterObjects,
  174. dwNumFilterObjectsReturned
  175. );
  176. }
  177. *pppIpsecFilterObjects = NULL;
  178. *pdwNumFilterObjects = 0;
  179. goto cleanup;
  180. }
  181. DWORD
  182. DirSetFilterData(
  183. HLDAP hLdapBindHandle,
  184. LPWSTR pszIpsecRootContainer,
  185. PIPSEC_FILTER_DATA pIpsecFilterData
  186. )
  187. {
  188. DWORD dwError = 0;
  189. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  190. dwError = DirMarshallFilterObject(
  191. pIpsecFilterData,
  192. pszIpsecRootContainer,
  193. &pIpsecFilterObject
  194. );
  195. BAIL_ON_WIN32_ERROR(dwError);
  196. dwError = DirSetFilterObject(
  197. hLdapBindHandle,
  198. pszIpsecRootContainer,
  199. pIpsecFilterObject
  200. );
  201. BAIL_ON_WIN32_ERROR(dwError);
  202. dwError = DirBackPropIncChangesForFilterToNFA(
  203. hLdapBindHandle,
  204. pszIpsecRootContainer,
  205. pIpsecFilterData->FilterIdentifier
  206. );
  207. BAIL_ON_WIN32_ERROR(dwError);
  208. error:
  209. if (pIpsecFilterObject) {
  210. FreeIpsecFilterObject(pIpsecFilterObject);
  211. }
  212. return(dwError);
  213. }
  214. DWORD
  215. DirSetFilterObject(
  216. HLDAP hLdapBindHandle,
  217. LPWSTR pszIpsecRootContainer,
  218. PIPSEC_FILTER_OBJECT pIpsecFilterObject
  219. )
  220. {
  221. DWORD dwError = 0;
  222. LDAPModW ** ppLDAPModW = NULL;
  223. dwError = DirMarshallSetFilterObject(
  224. hLdapBindHandle,
  225. pszIpsecRootContainer,
  226. pIpsecFilterObject,
  227. &ppLDAPModW
  228. );
  229. BAIL_ON_WIN32_ERROR(dwError);
  230. dwError = LdapModifyS(
  231. hLdapBindHandle,
  232. pIpsecFilterObject->pszDistinguishedName,
  233. ppLDAPModW
  234. );
  235. BAIL_ON_WIN32_ERROR(dwError);
  236. error:
  237. //
  238. // Free the amods structures.
  239. //
  240. if (ppLDAPModW) {
  241. FreeLDAPModWs(
  242. ppLDAPModW
  243. );
  244. }
  245. return(dwError);
  246. }
  247. DWORD
  248. DirCreateFilterData(
  249. HLDAP hLdapBindHandle,
  250. LPWSTR pszIpsecRootContainer,
  251. PIPSEC_FILTER_DATA pIpsecFilterData
  252. )
  253. {
  254. DWORD dwError = 0;
  255. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  256. dwError = DirMarshallFilterObject(
  257. pIpsecFilterData,
  258. pszIpsecRootContainer,
  259. &pIpsecFilterObject
  260. );
  261. BAIL_ON_WIN32_ERROR(dwError);
  262. dwError = DirCreateFilterObject(
  263. hLdapBindHandle,
  264. pszIpsecRootContainer,
  265. pIpsecFilterObject
  266. );
  267. BAIL_ON_WIN32_ERROR(dwError);
  268. error:
  269. if (pIpsecFilterObject) {
  270. FreeIpsecFilterObject(
  271. pIpsecFilterObject
  272. );
  273. }
  274. return(dwError);
  275. }
  276. DWORD
  277. DirCreateFilterObject(
  278. HLDAP hLdapBindHandle,
  279. LPWSTR pszIpsecRootContainer,
  280. PIPSEC_FILTER_OBJECT pIpsecFilterObject
  281. )
  282. {
  283. DWORD dwError = 0;
  284. LDAPModW ** ppLDAPModW = NULL;
  285. dwError = DirMarshallAddFilterObject(
  286. hLdapBindHandle,
  287. pszIpsecRootContainer,
  288. pIpsecFilterObject,
  289. &ppLDAPModW
  290. );
  291. BAIL_ON_WIN32_ERROR(dwError);
  292. dwError = LdapAddS(
  293. hLdapBindHandle,
  294. pIpsecFilterObject->pszDistinguishedName,
  295. ppLDAPModW
  296. );
  297. BAIL_ON_WIN32_ERROR(dwError);
  298. error:
  299. //
  300. // Free the amods structures.
  301. //
  302. if (ppLDAPModW) {
  303. FreeLDAPModWs(
  304. ppLDAPModW
  305. );
  306. }
  307. return(dwError);
  308. }
  309. DWORD
  310. DirDeleteFilterData(
  311. HLDAP hLdapBindHandle,
  312. LPWSTR pszIpsecRootContainer,
  313. GUID FilterIdentifier
  314. )
  315. {
  316. DWORD dwError = ERROR_SUCCESS;
  317. WCHAR szGuid[MAX_PATH];
  318. WCHAR szDistinguishedName[MAX_PATH];
  319. LPWSTR pszStringUuid = NULL;
  320. szGuid[0] = L'\0';
  321. szDistinguishedName[0] = L'\0';
  322. dwError = UuidToString(
  323. &FilterIdentifier,
  324. &pszStringUuid
  325. );
  326. BAIL_ON_WIN32_ERROR(dwError);
  327. wcscpy(szGuid, L"{");
  328. wcscat(szGuid, pszStringUuid);
  329. wcscat(szGuid, L"}");
  330. wcscpy(szDistinguishedName,L"CN=ipsecFilter");
  331. wcscat(szDistinguishedName, szGuid);
  332. wcscat(szDistinguishedName, L",");
  333. wcscat(szDistinguishedName, pszIpsecRootContainer);
  334. dwError = LdapDeleteS(
  335. hLdapBindHandle,
  336. szDistinguishedName
  337. );
  338. BAIL_ON_WIN32_ERROR(dwError);
  339. error:
  340. if (pszStringUuid) {
  341. RpcStringFree(&pszStringUuid);
  342. }
  343. return(dwError);
  344. }
  345. DWORD
  346. DirMarshallAddFilterObject(
  347. HLDAP hLdapBindHandle,
  348. LPWSTR pszIpsecRootContainer,
  349. PIPSEC_FILTER_OBJECT pIpsecFilterObject,
  350. LDAPModW *** pppLDAPModW
  351. )
  352. {
  353. DWORD i = 0;
  354. LDAPModW ** ppLDAPModW = NULL;
  355. LDAPModW * pLDAPModW = NULL;
  356. DWORD dwNumAttributes = 6;
  357. DWORD dwError = 0;
  358. WCHAR Buffer[64];
  359. if (!pIpsecFilterObject->pszIpsecName ||
  360. !*pIpsecFilterObject->pszIpsecName) {
  361. dwNumAttributes--;
  362. }
  363. if (!pIpsecFilterObject->pszDescription ||
  364. !*pIpsecFilterObject->pszDescription) {
  365. dwNumAttributes--;
  366. }
  367. ppLDAPModW = (LDAPModW **) AllocPolMem(
  368. (dwNumAttributes+1) * sizeof(LDAPModW*)
  369. );
  370. if (!ppLDAPModW) {
  371. dwError = ERROR_OUTOFMEMORY;
  372. BAIL_ON_WIN32_ERROR(dwError);
  373. }
  374. pLDAPModW = (LDAPModW *) AllocPolMem(
  375. dwNumAttributes * sizeof(LDAPModW)
  376. );
  377. if (!pLDAPModW) {
  378. dwError = ERROR_OUTOFMEMORY;
  379. BAIL_ON_WIN32_ERROR(dwError);
  380. }
  381. //
  382. // 0. objectClass
  383. //
  384. ppLDAPModW[i] = pLDAPModW + i;
  385. dwError = AllocatePolString(
  386. L"objectClass",
  387. &(pLDAPModW +i)->mod_type
  388. );
  389. BAIL_ON_WIN32_ERROR(dwError);
  390. dwError = AllocateLDAPStringValue(
  391. L"ipsecFilter",
  392. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  393. );
  394. BAIL_ON_WIN32_ERROR(dwError);
  395. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  396. i++;
  397. //
  398. // 1. ipsecName
  399. //
  400. if (pIpsecFilterObject->pszIpsecName &&
  401. *pIpsecFilterObject->pszIpsecName) {
  402. ppLDAPModW[i] = pLDAPModW + i;
  403. dwError = AllocatePolString(
  404. L"ipsecName",
  405. &(pLDAPModW +i)->mod_type
  406. );
  407. BAIL_ON_WIN32_ERROR(dwError);
  408. dwError = AllocateLDAPStringValue(
  409. pIpsecFilterObject->pszIpsecName,
  410. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  411. );
  412. BAIL_ON_WIN32_ERROR(dwError);
  413. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  414. i++;
  415. }
  416. //
  417. // 2. ipsecID
  418. //
  419. ppLDAPModW[i] = pLDAPModW + i;
  420. dwError = AllocatePolString(
  421. L"ipsecID",
  422. &(pLDAPModW +i)->mod_type
  423. );
  424. BAIL_ON_WIN32_ERROR(dwError);
  425. dwError = AllocateLDAPStringValue(
  426. pIpsecFilterObject->pszIpsecID,
  427. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  428. );
  429. BAIL_ON_WIN32_ERROR(dwError);
  430. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  431. i++;
  432. //
  433. // 3. ipsecDataType
  434. //
  435. ppLDAPModW[i] = pLDAPModW + i;
  436. dwError = AllocatePolString(
  437. L"ipsecDataType",
  438. &(pLDAPModW +i)->mod_type
  439. );
  440. BAIL_ON_WIN32_ERROR(dwError);
  441. _itow( pIpsecFilterObject->dwIpsecDataType, Buffer, 10 );
  442. dwError = AllocateLDAPStringValue(
  443. Buffer,
  444. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  445. );
  446. BAIL_ON_WIN32_ERROR(dwError);
  447. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  448. i++;
  449. //
  450. // 4. ipsecData
  451. //
  452. ppLDAPModW[i] = pLDAPModW + i;
  453. dwError = AllocatePolString(
  454. L"ipsecData",
  455. &(pLDAPModW +i)->mod_type
  456. );
  457. BAIL_ON_WIN32_ERROR(dwError);
  458. dwError = AllocateLDAPBinaryValue(
  459. pIpsecFilterObject->pIpsecData,
  460. pIpsecFilterObject->dwIpsecDataLen,
  461. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  462. );
  463. BAIL_ON_WIN32_ERROR(dwError);
  464. (pLDAPModW+i)->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  465. i++;
  466. //
  467. // 5. description
  468. //
  469. if (pIpsecFilterObject->pszDescription &&
  470. *pIpsecFilterObject->pszDescription) {
  471. ppLDAPModW[i] = pLDAPModW + i;
  472. dwError = AllocatePolString(
  473. L"description",
  474. &(pLDAPModW +i)->mod_type
  475. );
  476. BAIL_ON_WIN32_ERROR(dwError);
  477. dwError = AllocateLDAPStringValue(
  478. pIpsecFilterObject->pszDescription,
  479. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  480. );
  481. BAIL_ON_WIN32_ERROR(dwError);
  482. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  483. i++;
  484. }
  485. *pppLDAPModW = ppLDAPModW;
  486. return(dwError);
  487. error:
  488. if (ppLDAPModW) {
  489. FreeLDAPModWs(
  490. ppLDAPModW
  491. );
  492. }
  493. *pppLDAPModW = NULL;
  494. return(dwError);
  495. }
  496. DWORD
  497. DirMarshallSetFilterObject(
  498. HLDAP hLdapBindHandle,
  499. LPWSTR pszIpsecRootContainer,
  500. PIPSEC_FILTER_OBJECT pIpsecFilterObject,
  501. LDAPModW *** pppLDAPModW
  502. )
  503. {
  504. DWORD i = 0;
  505. LDAPModW ** ppLDAPModW = NULL;
  506. LDAPModW * pLDAPModW = NULL;
  507. DWORD dwNumAttributes = 5;
  508. DWORD dwError = 0;
  509. WCHAR Buffer[64];
  510. if (!pIpsecFilterObject->pszIpsecName ||
  511. !*pIpsecFilterObject->pszIpsecName) {
  512. dwNumAttributes--;
  513. }
  514. if (!pIpsecFilterObject->pszDescription ||
  515. !*pIpsecFilterObject->pszDescription) {
  516. dwNumAttributes--;
  517. }
  518. ppLDAPModW = (LDAPModW **) AllocPolMem(
  519. (dwNumAttributes+1) * sizeof(LDAPModW*)
  520. );
  521. if (!ppLDAPModW) {
  522. dwError = ERROR_OUTOFMEMORY;
  523. BAIL_ON_WIN32_ERROR(dwError);
  524. }
  525. pLDAPModW = (LDAPModW *) AllocPolMem(
  526. dwNumAttributes * sizeof(LDAPModW)
  527. );
  528. if (!pLDAPModW) {
  529. dwError = ERROR_OUTOFMEMORY;
  530. BAIL_ON_WIN32_ERROR(dwError);
  531. }
  532. //
  533. // 1. ipsecName
  534. //
  535. if (pIpsecFilterObject->pszIpsecName &&
  536. *pIpsecFilterObject->pszIpsecName) {
  537. ppLDAPModW[i] = pLDAPModW + i;
  538. dwError = AllocatePolString(
  539. L"ipsecName",
  540. &(pLDAPModW +i)->mod_type
  541. );
  542. BAIL_ON_WIN32_ERROR(dwError);
  543. dwError = AllocateLDAPStringValue(
  544. pIpsecFilterObject->pszIpsecName,
  545. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  546. );
  547. BAIL_ON_WIN32_ERROR(dwError);
  548. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  549. i++;
  550. }
  551. //
  552. // 2. ipsecID
  553. //
  554. ppLDAPModW[i] = pLDAPModW + i;
  555. dwError = AllocatePolString(
  556. L"ipsecID",
  557. &(pLDAPModW +i)->mod_type
  558. );
  559. BAIL_ON_WIN32_ERROR(dwError);
  560. dwError = AllocateLDAPStringValue(
  561. pIpsecFilterObject->pszIpsecID,
  562. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  563. );
  564. BAIL_ON_WIN32_ERROR(dwError);
  565. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  566. i++;
  567. //
  568. // 3. ipsecDataType
  569. //
  570. ppLDAPModW[i] = pLDAPModW + i;
  571. dwError = AllocatePolString(
  572. L"ipsecDataType",
  573. &(pLDAPModW +i)->mod_type
  574. );
  575. BAIL_ON_WIN32_ERROR(dwError);
  576. _itow( pIpsecFilterObject->dwIpsecDataType, Buffer, 10 );
  577. dwError = AllocateLDAPStringValue(
  578. Buffer,
  579. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  580. );
  581. BAIL_ON_WIN32_ERROR(dwError);
  582. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  583. i++;
  584. //
  585. // 4. ipsecData
  586. //
  587. ppLDAPModW[i] = pLDAPModW + i;
  588. dwError = AllocatePolString(
  589. L"ipsecData",
  590. &(pLDAPModW +i)->mod_type
  591. );
  592. BAIL_ON_WIN32_ERROR(dwError);
  593. dwError = AllocateLDAPBinaryValue(
  594. pIpsecFilterObject->pIpsecData,
  595. pIpsecFilterObject->dwIpsecDataLen,
  596. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  597. );
  598. BAIL_ON_WIN32_ERROR(dwError);
  599. (pLDAPModW+i)->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  600. i++;
  601. //
  602. // 5. description
  603. //
  604. if (pIpsecFilterObject->pszDescription &&
  605. *pIpsecFilterObject->pszDescription) {
  606. ppLDAPModW[i] = pLDAPModW + i;
  607. dwError = AllocatePolString(
  608. L"description",
  609. &(pLDAPModW +i)->mod_type
  610. );
  611. BAIL_ON_WIN32_ERROR(dwError);
  612. dwError = AllocateLDAPStringValue(
  613. pIpsecFilterObject->pszDescription,
  614. (PLDAPOBJECT *)&(pLDAPModW +i)->mod_values
  615. );
  616. BAIL_ON_WIN32_ERROR(dwError);
  617. (pLDAPModW + i)->mod_op |= LDAP_MOD_REPLACE;
  618. i++;
  619. }
  620. *pppLDAPModW = ppLDAPModW;
  621. return(dwError);
  622. error:
  623. if (ppLDAPModW) {
  624. FreeLDAPModWs(
  625. ppLDAPModW
  626. );
  627. }
  628. *pppLDAPModW = NULL;
  629. return(dwError);
  630. }
  631. DWORD
  632. GenerateAllFiltersQuery(
  633. LPWSTR * ppszFilterString
  634. )
  635. {
  636. DWORD dwError = 0;
  637. DWORD dwLength = 0;
  638. LPWSTR pszFilterString = NULL;
  639. //
  640. // Compute Length of Buffer to be allocated
  641. //
  642. dwLength = wcslen(L"(objectclass=ipsecFilter)");
  643. pszFilterString = (LPWSTR) AllocPolMem((dwLength + 1)*sizeof(WCHAR));
  644. if (!pszFilterString) {
  645. dwError = ERROR_OUTOFMEMORY;
  646. BAIL_ON_WIN32_ERROR(dwError);
  647. }
  648. //
  649. // Now fill in the buffer
  650. //
  651. wcscpy(pszFilterString, L"(objectclass=ipsecFilter)");
  652. *ppszFilterString = pszFilterString;
  653. return(0);
  654. error:
  655. if (pszFilterString) {
  656. FreePolMem(pszFilterString);
  657. }
  658. *ppszFilterString = NULL;
  659. return(dwError);
  660. }
  661. DWORD
  662. DirUnmarshallFilterData(
  663. PIPSEC_FILTER_OBJECT pIpsecFilterObject,
  664. PIPSEC_FILTER_DATA * ppIpsecFilterData
  665. )
  666. {
  667. DWORD dwError = 0;
  668. dwError = UnmarshallFilterObject(
  669. pIpsecFilterObject,
  670. ppIpsecFilterData
  671. );
  672. return(dwError);
  673. }
  674. DWORD
  675. DirMarshallFilterObject(
  676. PIPSEC_FILTER_DATA pIpsecFilterData,
  677. LPWSTR pszIpsecRootContainer,
  678. PIPSEC_FILTER_OBJECT * ppIpsecFilterObject
  679. )
  680. {
  681. DWORD dwError = 0;
  682. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  683. WCHAR szGuid[MAX_PATH];
  684. WCHAR szDistinguishedName[MAX_PATH];
  685. LPBYTE pBuffer = NULL;
  686. DWORD dwBufferLen = 0;
  687. LPWSTR pszStringUuid = NULL;
  688. szGuid[0] = L'\0';
  689. szDistinguishedName[0] = L'\0';
  690. pIpsecFilterObject = (PIPSEC_FILTER_OBJECT)AllocPolMem(
  691. sizeof(IPSEC_FILTER_OBJECT)
  692. );
  693. if (!pIpsecFilterObject) {
  694. dwError = ERROR_OUTOFMEMORY;
  695. BAIL_ON_WIN32_ERROR(dwError);
  696. }
  697. dwError = UuidToString(
  698. &pIpsecFilterData->FilterIdentifier,
  699. &pszStringUuid
  700. );
  701. BAIL_ON_WIN32_ERROR(dwError);
  702. wcscpy(szGuid, L"{");
  703. wcscat(szGuid, pszStringUuid);
  704. wcscat(szGuid, L"}");
  705. //
  706. // Fill in the distinguishedName
  707. //
  708. wcscpy(szDistinguishedName,L"CN=ipsecFilter");
  709. wcscat(szDistinguishedName, szGuid);
  710. wcscat(szDistinguishedName, L",");
  711. wcscat(szDistinguishedName, pszIpsecRootContainer);
  712. pIpsecFilterObject->pszDistinguishedName = AllocPolStr(
  713. szDistinguishedName
  714. );
  715. if (!pIpsecFilterObject->pszDistinguishedName) {
  716. dwError = ERROR_OUTOFMEMORY;
  717. BAIL_ON_WIN32_ERROR(dwError);
  718. }
  719. //
  720. // Fill in the ipsecName
  721. //
  722. if (pIpsecFilterData->pszIpsecName &&
  723. *pIpsecFilterData->pszIpsecName) {
  724. pIpsecFilterObject->pszIpsecName = AllocPolStr(
  725. pIpsecFilterData->pszIpsecName
  726. );
  727. if (!pIpsecFilterObject->pszIpsecName) {
  728. dwError = ERROR_OUTOFMEMORY;
  729. BAIL_ON_WIN32_ERROR(dwError);
  730. }
  731. }
  732. if (pIpsecFilterData->pszDescription &&
  733. *pIpsecFilterData->pszDescription) {
  734. pIpsecFilterObject->pszDescription = AllocPolStr(
  735. pIpsecFilterData->pszDescription
  736. );
  737. if (!pIpsecFilterObject->pszDescription) {
  738. dwError = ERROR_OUTOFMEMORY;
  739. BAIL_ON_WIN32_ERROR(dwError);
  740. }
  741. }
  742. //
  743. // Fill in the ipsecID
  744. //
  745. pIpsecFilterObject->pszIpsecID = AllocPolStr(
  746. szGuid
  747. );
  748. if (!pIpsecFilterObject->pszIpsecID) {
  749. dwError = ERROR_OUTOFMEMORY;
  750. BAIL_ON_WIN32_ERROR(dwError);
  751. }
  752. //
  753. // Fill in the ipsecDataType
  754. //
  755. pIpsecFilterObject->dwIpsecDataType = 0x100;
  756. //
  757. // Marshall the pIpsecDataBuffer and the Length
  758. //
  759. dwError = MarshallFilterBuffer(
  760. pIpsecFilterData,
  761. &pBuffer,
  762. &dwBufferLen
  763. );
  764. BAIL_ON_WIN32_ERROR(dwError);
  765. pIpsecFilterObject->pIpsecData = pBuffer;
  766. pIpsecFilterObject->dwIpsecDataLen = dwBufferLen;
  767. pIpsecFilterObject->dwWhenChanged = 0;
  768. *ppIpsecFilterObject = pIpsecFilterObject;
  769. cleanup:
  770. if (pszStringUuid) {
  771. RpcStringFree(
  772. &pszStringUuid
  773. );
  774. }
  775. return(dwError);
  776. error:
  777. if (pIpsecFilterObject) {
  778. FreeIpsecFilterObject(
  779. pIpsecFilterObject
  780. );
  781. }
  782. *ppIpsecFilterObject = NULL;
  783. goto cleanup;
  784. }
  785. DWORD
  786. DirGetFilterData(
  787. HLDAP hLdapBindHandle,
  788. LPWSTR pszIpsecRootContainer,
  789. GUID FilterGUID,
  790. PIPSEC_FILTER_DATA * ppIpsecFilterData
  791. )
  792. {
  793. DWORD dwError = 0;
  794. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  795. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  796. dwError = DirGetFilterObject(
  797. hLdapBindHandle,
  798. pszIpsecRootContainer,
  799. FilterGUID,
  800. &pIpsecFilterObject
  801. );
  802. BAIL_ON_WIN32_ERROR(dwError);
  803. dwError = DirUnmarshallFilterData(
  804. pIpsecFilterObject,
  805. &pIpsecFilterData
  806. );
  807. BAIL_ON_WIN32_ERROR(dwError);
  808. *ppIpsecFilterData = pIpsecFilterData;
  809. cleanup:
  810. if (pIpsecFilterObject) {
  811. FreeIpsecFilterObject(
  812. pIpsecFilterObject
  813. );
  814. }
  815. return(dwError);
  816. error:
  817. *ppIpsecFilterData = NULL;
  818. goto cleanup;
  819. }
  820. DWORD
  821. DirGetFilterObject(
  822. HLDAP hLdapBindHandle,
  823. LPWSTR pszIpsecRootContainer,
  824. GUID FilterGUID,
  825. PIPSEC_FILTER_OBJECT * ppIpsecFilterObject
  826. )
  827. {
  828. DWORD dwError = 0;
  829. LPWSTR pszFilterString = NULL;
  830. LDAPMessage * res = NULL;
  831. DWORD dwCount = 0;
  832. LDAPMessage * e = NULL;
  833. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  834. dwError = GenerateSpecificFilterQuery(
  835. FilterGUID,
  836. &pszFilterString
  837. );
  838. BAIL_ON_WIN32_ERROR(dwError);
  839. dwError = LdapSearchST(
  840. hLdapBindHandle,
  841. pszIpsecRootContainer,
  842. LDAP_SCOPE_ONELEVEL,
  843. pszFilterString,
  844. FilterDNAttributes,
  845. 0,
  846. NULL,
  847. &res
  848. );
  849. BAIL_ON_WIN32_ERROR(dwError);
  850. dwCount = LdapCountEntries(
  851. hLdapBindHandle,
  852. res
  853. );
  854. if (!dwCount) {
  855. dwError = ERROR_DS_NO_ATTRIBUTE_OR_VALUE;
  856. BAIL_ON_WIN32_ERROR(dwError);
  857. }
  858. dwError = LdapFirstEntry(
  859. hLdapBindHandle,
  860. res,
  861. &e
  862. );
  863. BAIL_ON_WIN32_ERROR(dwError);
  864. dwError = UnMarshallFilterObject(
  865. hLdapBindHandle,
  866. e,
  867. &pIpsecFilterObject
  868. );
  869. BAIL_ON_WIN32_ERROR(dwError);
  870. *ppIpsecFilterObject = pIpsecFilterObject;
  871. dwError = ERROR_SUCCESS;
  872. cleanup:
  873. if (pszFilterString) {
  874. FreePolMem(pszFilterString);
  875. }
  876. if (res) {
  877. LdapMsgFree(res);
  878. }
  879. return(dwError);
  880. error:
  881. if (pIpsecFilterObject) {
  882. FreeIpsecFilterObject(
  883. pIpsecFilterObject
  884. );
  885. }
  886. *ppIpsecFilterObject = NULL;
  887. goto cleanup;
  888. }
  889. DWORD
  890. GenerateSpecificFilterQuery(
  891. GUID FilterIdentifier,
  892. LPWSTR * ppszFilterString
  893. )
  894. {
  895. DWORD dwError = ERROR_SUCCESS;
  896. WCHAR szGuid[MAX_PATH];
  897. WCHAR szCommonName[MAX_PATH];
  898. LPWSTR pszStringUuid = NULL;
  899. DWORD dwLength = 0;
  900. LPWSTR pszFilterString = NULL;
  901. szGuid[0] = L'\0';
  902. szCommonName[0] = L'\0';
  903. dwError = UuidToString(
  904. &FilterIdentifier,
  905. &pszStringUuid
  906. );
  907. BAIL_ON_WIN32_ERROR(dwError);
  908. wcscpy(szGuid, L"{");
  909. wcscat(szGuid, pszStringUuid);
  910. wcscat(szGuid, L"}");
  911. wcscpy(szCommonName, L"cn=ipsecFilter");
  912. wcscat(szCommonName, szGuid);
  913. //
  914. // Compute Length of Buffer to be allocated
  915. //
  916. dwLength = wcslen(L"(&(objectclass=ipsecFilter)");
  917. dwLength += wcslen(L"(");
  918. dwLength += wcslen(szCommonName);
  919. dwLength += wcslen(L"))");
  920. pszFilterString = (LPWSTR) AllocPolMem((dwLength + 1)*sizeof(WCHAR));
  921. if (!pszFilterString) {
  922. dwError = ERROR_OUTOFMEMORY;
  923. BAIL_ON_WIN32_ERROR(dwError);
  924. }
  925. wcscpy(pszFilterString, L"(&(objectclass=ipsecFilter)");
  926. wcscat(pszFilterString, L"(");
  927. wcscat(pszFilterString, szCommonName);
  928. wcscat(pszFilterString, L"))");
  929. *ppszFilterString = pszFilterString;
  930. cleanup:
  931. if (pszStringUuid) {
  932. RpcStringFree(&pszStringUuid);
  933. }
  934. return(dwError);
  935. error:
  936. if (pszFilterString) {
  937. FreePolMem(pszFilterString);
  938. }
  939. *ppszFilterString = NULL;
  940. goto cleanup;
  941. }