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.

1351 lines
31 KiB

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