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.

1487 lines
39 KiB

  1. #include "precomp.h"
  2. LPWSTR gpszIpsecWMINamespace = L"root\\rsop\\computer";
  3. DWORD
  4. Win32FromWmiHresult(
  5. HRESULT hr
  6. )
  7. {
  8. if (SUCCEEDED(hr)) {
  9. return ERROR_SUCCESS;
  10. } else {
  11. switch (hr) {
  12. case WBEM_E_ACCESS_DENIED:
  13. return ERROR_ACCESS_DENIED;
  14. case REGDB_E_CLASSNOTREG:
  15. case CLASS_E_NOAGGREGATION:
  16. case E_NOINTERFACE:
  17. case WBEM_E_INVALID_NAMESPACE:
  18. case WBEM_E_INVALID_PARAMETER:
  19. case WBEM_E_NOT_FOUND:
  20. case WBEM_E_INVALID_CLASS:
  21. case WBEM_E_INVALID_OBJECT_PATH:
  22. return ERROR_INVALID_PARAMETER;
  23. case WBEM_E_OUT_OF_MEMORY:
  24. return ERROR_OUTOFMEMORY;
  25. case WBEM_E_TRANSPORT_FAILURE:
  26. return RPC_S_CALL_FAILED;
  27. case WBEM_E_FAILED:
  28. default:
  29. return ERROR_WMI_TRY_AGAIN;
  30. }
  31. }
  32. }
  33. DWORD
  34. UnMarshallWMIPolicyObject(
  35. IWbemClassObject *pWbemClassObject,
  36. PIPSEC_POLICY_OBJECT * ppIpsecPolicyObject
  37. )
  38. {
  39. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  40. HKEY hRegKey = NULL;
  41. DWORD dwType = 0;
  42. DWORD dwSize = 0;
  43. DWORD dwIpsecDataType = 0;
  44. DWORD dwWhenChanged = 0;
  45. LPBYTE pBuffer = NULL;
  46. DWORD i = 0;
  47. DWORD dwCount = 0;
  48. DWORD dwError = 0;
  49. HRESULT hr = S_OK;
  50. LPWSTR * ppszIpsecNFANames = NULL;
  51. LPWSTR pszIpsecNFAName = NULL;
  52. LPWSTR pszTemp = NULL;
  53. LPWSTR pszString = NULL;
  54. LPWSTR pszIpsecNFAReference = NULL;
  55. LPWSTR pszRelativeName = NULL;
  56. DWORD dwRootPathLen = 0;
  57. ////start
  58. VARIANT var; //contains pszIpsecPolicyDN
  59. VariantInit(&var);
  60. hr = IWbemClassObject_Get(pWbemClassObject,
  61. L"id",
  62. 0,
  63. &var,
  64. 0,
  65. 0);
  66. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  67. pIpsecPolicyObject = (PIPSEC_POLICY_OBJECT)AllocPolMem(sizeof(IPSEC_POLICY_OBJECT));
  68. if (!pIpsecPolicyObject) {
  69. dwError = ERROR_OUTOFMEMORY;
  70. BAIL_ON_WIN32_ERROR(dwError);
  71. }
  72. pIpsecPolicyObject->pszIpsecOwnersReference = AllocPolStr((LPWSTR)var.bstrVal);
  73. VariantClear(&var);
  74. if (!pIpsecPolicyObject->pszIpsecOwnersReference) {
  75. dwError = ERROR_OUTOFMEMORY;
  76. BAIL_ON_WIN32_ERROR(dwError);
  77. }
  78. pIpsecPolicyObject->pRsopInfo = (PRSOP_INFO)AllocPolMem(sizeof(RSOP_INFO));
  79. if (!pIpsecPolicyObject->pRsopInfo) {
  80. dwError = ERROR_OUTOFMEMORY;
  81. BAIL_ON_WIN32_ERROR(dwError);
  82. }
  83. dwError = WMIstoreQueryValue(pWbemClassObject,
  84. L"creationtime",
  85. VT_BSTR,
  86. (LPBYTE *)&pIpsecPolicyObject->pRsopInfo->pszCreationtime,
  87. &dwSize);
  88. dwError = WMIstoreQueryValue(pWbemClassObject,
  89. L"GPOID",
  90. VT_BSTR,
  91. (LPBYTE *)&pIpsecPolicyObject->pRsopInfo->pszGPOID,
  92. &dwSize);
  93. dwError = WMIstoreQueryValue(pWbemClassObject,
  94. L"id",
  95. VT_BSTR,
  96. (LPBYTE *)&pIpsecPolicyObject->pRsopInfo->pszID,
  97. &dwSize);
  98. dwError = WMIstoreQueryValue(pWbemClassObject,
  99. L"name",
  100. VT_BSTR,
  101. (LPBYTE *)&pIpsecPolicyObject->pRsopInfo->pszName,
  102. &dwSize);
  103. dwError = WMIstoreQueryValue(pWbemClassObject,
  104. L"SOMID",
  105. VT_BSTR,
  106. (LPBYTE *)&pIpsecPolicyObject->pRsopInfo->pszSOMID,
  107. &dwSize);
  108. hr = IWbemClassObject_Get(pWbemClassObject,
  109. L"precedence",
  110. 0,
  111. &var,
  112. 0,
  113. 0);
  114. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  115. pIpsecPolicyObject->pRsopInfo->uiPrecedence = var.lVal;
  116. dwError = WMIstoreQueryValue(pWbemClassObject,
  117. L"ipsecName",
  118. VT_BSTR,
  119. (LPBYTE *)&pIpsecPolicyObject->pszIpsecName,
  120. &dwSize);
  121. BAIL_ON_WIN32_ERROR(dwError);
  122. dwError = WMIstoreQueryValue(pWbemClassObject,
  123. L"description",
  124. VT_BSTR,
  125. (LPBYTE *)&pIpsecPolicyObject->pszDescription,
  126. &dwSize);
  127. // BAIL_ON_WIN32_ERROR(dwError);
  128. dwError = WMIstoreQueryValue(pWbemClassObject,
  129. L"ipsecID",
  130. VT_BSTR,
  131. (LPBYTE *)&pIpsecPolicyObject->pszIpsecID,
  132. &dwSize);
  133. BAIL_ON_WIN32_ERROR(dwError);
  134. hr = IWbemClassObject_Get(pWbemClassObject,
  135. L"ipsecDataType",
  136. 0,
  137. &var,
  138. 0,
  139. 0);
  140. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  141. dwIpsecDataType = var.lVal;
  142. pIpsecPolicyObject->dwIpsecDataType = dwIpsecDataType;
  143. dwError = WMIstoreQueryValue(pWbemClassObject,
  144. L"ipsecData",
  145. VT_ARRAY|VT_UI1,
  146. &pIpsecPolicyObject->pIpsecData,
  147. &pIpsecPolicyObject->dwIpsecDataLen);
  148. BAIL_ON_WIN32_ERROR(dwError);
  149. dwError = WMIstoreQueryValue(pWbemClassObject,
  150. L"ipsecISAKMPReference",
  151. VT_BSTR,
  152. (LPBYTE *)&pIpsecPolicyObject->pszIpsecISAKMPReference,
  153. &dwSize);
  154. BAIL_ON_WIN32_ERROR(dwError);
  155. dwError = WMIstoreQueryValue(pWbemClassObject,
  156. L"ipsecNFAReference",
  157. VT_ARRAY|VT_BSTR,
  158. (LPBYTE *)&pszIpsecNFAReference,
  159. &dwSize);
  160. BAIL_ON_WIN32_ERROR(dwError);
  161. ////errr, multi-string processing
  162. pszTemp = pszIpsecNFAReference;
  163. while (*pszTemp != L'\0') {
  164. pszTemp += wcslen(pszTemp) + 1;
  165. dwCount++;
  166. }
  167. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(sizeof(LPWSTR)*dwCount);
  168. if (!ppszIpsecNFANames) {
  169. dwError = ERROR_OUTOFMEMORY;
  170. BAIL_ON_WIN32_ERROR(dwError);
  171. }
  172. pszTemp = pszIpsecNFAReference;
  173. for (i = 0; i < dwCount; i++) {
  174. pszString = AllocPolStr(pszTemp);
  175. if (!pszString) {
  176. dwError = ERROR_OUTOFMEMORY;
  177. pIpsecPolicyObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  178. pIpsecPolicyObject->NumberofRules = i;
  179. BAIL_ON_WIN32_ERROR(dwError);
  180. }
  181. *(ppszIpsecNFANames + i) = pszString;
  182. pszTemp += wcslen(pszTemp) + 1; //for the null terminator;
  183. }
  184. if (pszIpsecNFAReference) {
  185. FreePolStr(pszIpsecNFAReference);
  186. }
  187. pIpsecPolicyObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  188. pIpsecPolicyObject->NumberofRules = dwCount;
  189. hr = IWbemClassObject_Get(pWbemClassObject,
  190. L"whenChanged",
  191. 0,
  192. &var,
  193. 0,
  194. 0);
  195. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  196. dwWhenChanged = var.lVal;
  197. pIpsecPolicyObject->dwWhenChanged = dwWhenChanged;
  198. *ppIpsecPolicyObject = pIpsecPolicyObject;
  199. cleanup:
  200. return(dwError);
  201. error:
  202. if (pszIpsecNFAReference) {
  203. FreePolStr(pszIpsecNFAReference);
  204. }
  205. if (pIpsecPolicyObject) {
  206. FreeIpsecPolicyObject(pIpsecPolicyObject);
  207. }
  208. *ppIpsecPolicyObject = NULL;
  209. goto cleanup;
  210. }
  211. DWORD
  212. UnMarshallWMIFilterObject(
  213. IWbemClassObject *pWbemClassObject,
  214. PIPSEC_FILTER_OBJECT * ppIpsecFilterObject
  215. )
  216. {
  217. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  218. HKEY hRegKey = NULL;
  219. DWORD dwType = 0;
  220. DWORD dwSize = 0;
  221. DWORD dwIpsecDataType = 0;
  222. DWORD dwWhenChanged = 0;
  223. LPBYTE pBuffer = NULL;
  224. DWORD dwCount = 0;
  225. DWORD i = 0;
  226. LPWSTR * ppszIpsecNFANames = NULL;
  227. LPWSTR pszString = NULL;
  228. LPWSTR pszIpsecNFAReference = NULL;
  229. LPWSTR pszTemp = NULL;
  230. DWORD dwError = 0;
  231. HRESULT hr = S_OK;
  232. LPWSTR pszRelativeName = NULL;
  233. DWORD dwRootPathLen = 0;
  234. //start
  235. VARIANT var; //=>pszIpsecFilterReference
  236. VariantInit(&var);
  237. hr = IWbemClassObject_Get(pWbemClassObject,
  238. L"id",
  239. 0,
  240. &var,
  241. 0,
  242. 0);
  243. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  244. pIpsecFilterObject = (PIPSEC_FILTER_OBJECT)AllocPolMem(
  245. sizeof(IPSEC_FILTER_OBJECT)
  246. );
  247. if (!pIpsecFilterObject) {
  248. dwError = ERROR_OUTOFMEMORY;
  249. BAIL_ON_WIN32_ERROR(dwError);
  250. }
  251. pIpsecFilterObject->pszDistinguishedName = AllocPolStr(
  252. (LPWSTR)var.bstrVal
  253. );
  254. VariantClear(&var);
  255. if (!pIpsecFilterObject->pszDistinguishedName) {
  256. dwError = ERROR_OUTOFMEMORY;
  257. BAIL_ON_WIN32_ERROR(dwError);
  258. }
  259. dwError = WMIstoreQueryValue(
  260. pWbemClassObject,
  261. L"description",
  262. VT_BSTR,
  263. (LPBYTE *)&pIpsecFilterObject->pszDescription,
  264. &dwSize
  265. );
  266. //BAIL_ON_WIN32_ERROR(dwError);
  267. dwError = WMIstoreQueryValue(
  268. pWbemClassObject,
  269. L"ipsecName",
  270. VT_BSTR,
  271. (LPBYTE *)&pIpsecFilterObject->pszIpsecName,
  272. &dwSize
  273. );
  274. //BAIL_ON_WIN32_ERROR(dwError);
  275. dwError = WMIstoreQueryValue(
  276. pWbemClassObject,
  277. L"ipsecID",
  278. VT_BSTR,
  279. (LPBYTE *)&pIpsecFilterObject->pszIpsecID,
  280. &dwSize
  281. );
  282. BAIL_ON_WIN32_ERROR(dwError);
  283. hr = IWbemClassObject_Get(pWbemClassObject,
  284. L"ipsecDataType",
  285. 0,
  286. &var,
  287. 0,
  288. 0);
  289. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  290. dwIpsecDataType = var.lVal;
  291. pIpsecFilterObject->dwIpsecDataType = dwIpsecDataType;
  292. //
  293. // unmarshall the ipsecData blob
  294. //
  295. dwError = WMIstoreQueryValue(
  296. pWbemClassObject,
  297. L"ipsecData",
  298. VT_ARRAY|VT_UI1,
  299. &pIpsecFilterObject->pIpsecData,
  300. &pIpsecFilterObject->dwIpsecDataLen
  301. );
  302. BAIL_ON_WIN32_ERROR(dwError);
  303. //
  304. // Owner's reference
  305. //
  306. dwError = WMIstoreQueryValue(
  307. pWbemClassObject,
  308. L"ipsecOwnersReference",
  309. VT_ARRAY|VT_BSTR,
  310. (LPBYTE *)&pszIpsecNFAReference,
  311. &dwSize
  312. );
  313. //BAIL_ON_WIN32_ERROR(dwError);
  314. if (!dwError) { //no error
  315. pszTemp = pszIpsecNFAReference;
  316. while (*pszTemp != L'\0') {
  317. pszTemp += wcslen(pszTemp) + 1;
  318. dwCount++;
  319. }
  320. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  321. sizeof(LPWSTR)*dwCount
  322. );
  323. if (!ppszIpsecNFANames) {
  324. dwError = ERROR_OUTOFMEMORY;
  325. BAIL_ON_WIN32_ERROR(dwError);
  326. }
  327. pszTemp = pszIpsecNFAReference;
  328. for (i = 0; i < dwCount; i++) {
  329. pszString = AllocPolStr(pszTemp);
  330. if (!pszString) {
  331. dwError = ERROR_OUTOFMEMORY;
  332. pIpsecFilterObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  333. pIpsecFilterObject->dwNFACount = i;
  334. if (pszIpsecNFAReference) {
  335. FreePolStr(pszIpsecNFAReference);
  336. }
  337. BAIL_ON_WIN32_ERROR(dwError);
  338. }
  339. *(ppszIpsecNFANames + i) = pszString;
  340. pszTemp += wcslen(pszTemp) + 1; //for the null terminator;
  341. }
  342. if (pszIpsecNFAReference) {
  343. FreePolStr(pszIpsecNFAReference);
  344. }
  345. pIpsecFilterObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  346. pIpsecFilterObject->dwNFACount = dwCount;
  347. }
  348. hr = IWbemClassObject_Get(pWbemClassObject,
  349. L"whenChanged",
  350. 0,
  351. &var,
  352. 0,
  353. 0);
  354. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  355. dwWhenChanged = var.lVal;
  356. pIpsecFilterObject->dwWhenChanged = dwWhenChanged;
  357. *ppIpsecFilterObject = pIpsecFilterObject;
  358. cleanup:
  359. return(dwError);
  360. error:
  361. if (pIpsecFilterObject) {
  362. FreeIpsecFilterObject(pIpsecFilterObject);
  363. }
  364. *ppIpsecFilterObject = NULL;
  365. goto cleanup;
  366. }
  367. DWORD
  368. UnMarshallWMINegPolObject(
  369. IWbemClassObject *pWbemClassObject,
  370. PIPSEC_NEGPOL_OBJECT * ppIpsecNegPolObject
  371. )
  372. {
  373. PIPSEC_NEGPOL_OBJECT pIpsecNegPolObject = NULL;
  374. HKEY hRegKey = NULL;
  375. DWORD dwType = 0;
  376. DWORD dwSize = 0;
  377. DWORD dwIpsecDataType = 0;
  378. DWORD dwWhenChanged = 0;
  379. LPBYTE pBuffer = NULL;
  380. DWORD dwCount = 0;
  381. DWORD i = 0;
  382. LPWSTR * ppszIpsecNFANames = NULL;
  383. LPWSTR pszString = NULL;
  384. LPWSTR pszIpsecNFAReference = NULL;
  385. LPWSTR pszTemp = NULL;
  386. DWORD dwError = 0;
  387. HRESULT hr = S_OK;
  388. LPWSTR pszRelativeName = NULL;
  389. DWORD dwRootPathLen = 0;
  390. //start
  391. VARIANT var; //=>pszIpsecNegPolReference
  392. VariantInit(&var);
  393. hr = IWbemClassObject_Get(pWbemClassObject,
  394. L"id",
  395. 0,
  396. &var,
  397. 0,
  398. 0);
  399. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  400. pIpsecNegPolObject = (PIPSEC_NEGPOL_OBJECT)AllocPolMem(
  401. sizeof(IPSEC_NEGPOL_OBJECT)
  402. );
  403. if (!pIpsecNegPolObject) {
  404. dwError = ERROR_OUTOFMEMORY;
  405. BAIL_ON_WIN32_ERROR(dwError);
  406. }
  407. pIpsecNegPolObject->pszDistinguishedName = AllocPolStr(
  408. (LPWSTR)var.bstrVal
  409. );
  410. VariantClear(&var);
  411. if (!pIpsecNegPolObject->pszDistinguishedName) {
  412. dwError = ERROR_OUTOFMEMORY;
  413. BAIL_ON_WIN32_ERROR(dwError);
  414. }
  415. //
  416. // Names do not get written on an NegPol Object
  417. //
  418. dwError = WMIstoreQueryValue(
  419. pWbemClassObject,
  420. L"ipsecName",
  421. VT_BSTR,
  422. (LPBYTE *)&pIpsecNegPolObject->pszIpsecName,
  423. &dwSize
  424. );
  425. // BAIL_ON_WIN32_ERROR(dwError);
  426. dwError = WMIstoreQueryValue(
  427. pWbemClassObject,
  428. L"description",
  429. VT_BSTR,
  430. (LPBYTE *)&pIpsecNegPolObject->pszDescription,
  431. &dwSize
  432. );
  433. // BAIL_ON_WIN32_ERROR(dwError);
  434. dwError = WMIstoreQueryValue(
  435. pWbemClassObject,
  436. L"ipsecID",
  437. VT_BSTR,
  438. (LPBYTE *)&pIpsecNegPolObject->pszIpsecID,
  439. &dwSize
  440. );
  441. BAIL_ON_WIN32_ERROR(dwError);
  442. dwError = WMIstoreQueryValue(
  443. pWbemClassObject,
  444. L"ipsecNegotiationPolicyAction",
  445. VT_BSTR,
  446. (LPBYTE *)&pIpsecNegPolObject->pszIpsecNegPolAction,
  447. &dwSize
  448. );
  449. BAIL_ON_WIN32_ERROR(dwError);
  450. dwError = WMIstoreQueryValue(
  451. pWbemClassObject,
  452. L"ipsecNegotiationPolicyType",
  453. VT_BSTR,
  454. (LPBYTE *)&pIpsecNegPolObject->pszIpsecNegPolType,
  455. &dwSize
  456. );
  457. BAIL_ON_WIN32_ERROR(dwError);
  458. hr= IWbemClassObject_Get(pWbemClassObject,
  459. L"ipsecDataType",
  460. 0,
  461. &var,
  462. 0,
  463. 0);
  464. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  465. dwIpsecDataType = var.lVal;
  466. pIpsecNegPolObject->dwIpsecDataType = dwIpsecDataType;
  467. dwError = WMIstoreQueryValue(
  468. pWbemClassObject,
  469. L"ipsecData",
  470. VT_ARRAY|VT_UI1,
  471. &pIpsecNegPolObject->pIpsecData,
  472. &pIpsecNegPolObject->dwIpsecDataLen
  473. );
  474. BAIL_ON_WIN32_ERROR(dwError);
  475. dwError = WMIstoreQueryValue(
  476. pWbemClassObject,
  477. L"ipsecOwnersReference",
  478. VT_ARRAY|VT_BSTR,
  479. (LPBYTE *)&pszIpsecNFAReference,
  480. &dwSize
  481. );
  482. // BAIL_ON_WIN32_ERROR(dwError);
  483. if (!dwError) {
  484. pszTemp = pszIpsecNFAReference;
  485. while (*pszTemp != L'\0') {
  486. pszTemp += wcslen(pszTemp) + 1;
  487. dwCount++;
  488. }
  489. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  490. sizeof(LPWSTR)*dwCount
  491. );
  492. if (!ppszIpsecNFANames) {
  493. dwError = ERROR_OUTOFMEMORY;
  494. BAIL_ON_WIN32_ERROR(dwError);
  495. }
  496. pszTemp = pszIpsecNFAReference;
  497. for (i = 0; i < dwCount; i++) {
  498. pszString = AllocPolStr(pszTemp);
  499. if (!pszString) {
  500. dwError = ERROR_OUTOFMEMORY;
  501. pIpsecNegPolObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  502. pIpsecNegPolObject->dwNFACount = i;
  503. if (pszIpsecNFAReference) {
  504. FreePolStr(pszIpsecNFAReference);
  505. }
  506. BAIL_ON_WIN32_ERROR(dwError);
  507. }
  508. *(ppszIpsecNFANames + i) = pszString;
  509. pszTemp += wcslen(pszTemp) + 1; //for the null terminator;
  510. }
  511. if (pszIpsecNFAReference) {
  512. FreePolStr(pszIpsecNFAReference);
  513. }
  514. pIpsecNegPolObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  515. pIpsecNegPolObject->dwNFACount = dwCount;
  516. }
  517. hr = IWbemClassObject_Get(pWbemClassObject,
  518. L"whenChanged",
  519. 0,
  520. &var,
  521. 0,
  522. 0);
  523. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  524. dwWhenChanged = var.lVal;
  525. pIpsecNegPolObject->dwWhenChanged = dwWhenChanged;
  526. *ppIpsecNegPolObject = pIpsecNegPolObject;
  527. cleanup:
  528. return(dwError);
  529. error:
  530. if (pIpsecNegPolObject) {
  531. FreeIpsecNegPolObject(pIpsecNegPolObject);
  532. }
  533. *ppIpsecNegPolObject = NULL;
  534. goto cleanup;
  535. }
  536. DWORD
  537. UnMarshallWMIISAKMPObject(
  538. IWbemClassObject *pWbemClassObject,
  539. PIPSEC_ISAKMP_OBJECT * ppIpsecISAKMPObject
  540. )
  541. {
  542. PIPSEC_ISAKMP_OBJECT pIpsecISAKMPObject = NULL;
  543. HKEY hRegKey = NULL;
  544. DWORD dwType = 0;
  545. DWORD dwSize = 0;
  546. DWORD dwIpsecDataType = 0;
  547. DWORD dwWhenChanged = 0;
  548. LPBYTE pBuffer = NULL;
  549. DWORD dwCount = 0;
  550. DWORD i = 0;
  551. LPWSTR * ppszIpsecNFANames = NULL;
  552. LPWSTR pszString = NULL;
  553. LPWSTR pszIpsecNFAReference = NULL;
  554. LPWSTR pszTemp = NULL;
  555. DWORD dwError = 0;
  556. HRESULT hr = S_OK;
  557. LPWSTR pszRelativeName = NULL;
  558. DWORD dwRootPathLen = 0;
  559. //start
  560. VARIANT var; //=>pszIpsecISAKMPReference
  561. VariantInit(&var);
  562. hr = IWbemClassObject_Get(pWbemClassObject,
  563. L"id",
  564. 0,
  565. &var,
  566. 0,
  567. 0);
  568. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  569. pIpsecISAKMPObject = (PIPSEC_ISAKMP_OBJECT)AllocPolMem(
  570. sizeof(IPSEC_ISAKMP_OBJECT)
  571. );
  572. if (!pIpsecISAKMPObject) {
  573. dwError = ERROR_OUTOFMEMORY;
  574. BAIL_ON_WIN32_ERROR(dwError);
  575. }
  576. pIpsecISAKMPObject->pszDistinguishedName = AllocPolStr(
  577. (LPWSTR)var.bstrVal
  578. );
  579. VariantClear(&var);
  580. if (!pIpsecISAKMPObject->pszDistinguishedName) {
  581. dwError = ERROR_OUTOFMEMORY;
  582. BAIL_ON_WIN32_ERROR(dwError);
  583. }
  584. //
  585. // Names are not set for ISAKMP objects
  586. //
  587. dwError = WMIstoreQueryValue(
  588. pWbemClassObject,
  589. L"ipsecName",
  590. VT_BSTR,
  591. (LPBYTE *)&pIpsecISAKMPObject->pszIpsecName,
  592. &dwSize
  593. );
  594. // BAIL_ON_WIN32_ERROR(dwError);
  595. dwError = WMIstoreQueryValue(
  596. pWbemClassObject,
  597. L"ipsecID",
  598. VT_BSTR,
  599. (LPBYTE *)&pIpsecISAKMPObject->pszIpsecID,
  600. &dwSize
  601. );
  602. BAIL_ON_WIN32_ERROR(dwError);
  603. hr = IWbemClassObject_Get(pWbemClassObject,
  604. L"ipsecDataType",
  605. 0,
  606. &var,
  607. 0,
  608. 0);
  609. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  610. dwIpsecDataType = var.lVal;
  611. pIpsecISAKMPObject->dwIpsecDataType = dwIpsecDataType;
  612. //
  613. // unmarshall the ipsecData blob
  614. //
  615. dwError = WMIstoreQueryValue(
  616. pWbemClassObject,
  617. L"ipsecData",
  618. VT_ARRAY|VT_UI1,
  619. &pIpsecISAKMPObject->pIpsecData,
  620. &pIpsecISAKMPObject->dwIpsecDataLen
  621. );
  622. BAIL_ON_WIN32_ERROR(dwError);
  623. //
  624. // ipsecOwnersReference not written
  625. //
  626. dwError = WMIstoreQueryValue(
  627. pWbemClassObject,
  628. L"ipsecOwnersReference",
  629. VT_ARRAY|VT_BSTR,
  630. (LPBYTE *)&pszIpsecNFAReference,
  631. &dwSize
  632. );
  633. // BAIL_ON_WIN32_ERROR(dwError);
  634. if (!dwError) {
  635. pszTemp = pszIpsecNFAReference;
  636. while (*pszTemp != L'\0') {
  637. pszTemp += wcslen(pszTemp) + 1;
  638. dwCount++;
  639. }
  640. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  641. sizeof(LPWSTR)*dwCount
  642. );
  643. if (!ppszIpsecNFANames) {
  644. dwError = ERROR_OUTOFMEMORY;
  645. BAIL_ON_WIN32_ERROR(dwError);
  646. }
  647. pszTemp = pszIpsecNFAReference;
  648. for (i = 0; i < dwCount; i++) {
  649. pszString = AllocPolStr(pszTemp);
  650. if (!pszString) {
  651. dwError = ERROR_OUTOFMEMORY;
  652. pIpsecISAKMPObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  653. pIpsecISAKMPObject->dwNFACount = i;
  654. if (pszIpsecNFAReference) {
  655. FreePolStr(pszIpsecNFAReference);
  656. }
  657. BAIL_ON_WIN32_ERROR(dwError);
  658. }
  659. *(ppszIpsecNFANames + i) = pszString;
  660. pszTemp += wcslen(pszTemp) + 1; //for the null terminator;
  661. }
  662. if (pszIpsecNFAReference) {
  663. FreePolStr(pszIpsecNFAReference);
  664. }
  665. pIpsecISAKMPObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  666. pIpsecISAKMPObject->dwNFACount = dwCount;
  667. }
  668. hr = IWbemClassObject_Get(pWbemClassObject,
  669. L"whenChanged",
  670. 0,
  671. &var,
  672. 0,
  673. 0);
  674. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  675. dwWhenChanged = var.lVal;
  676. pIpsecISAKMPObject->dwWhenChanged = dwWhenChanged;
  677. *ppIpsecISAKMPObject = pIpsecISAKMPObject;
  678. cleanup:
  679. return(dwError);
  680. error:
  681. if (pIpsecISAKMPObject) {
  682. FreeIpsecISAKMPObject(pIpsecISAKMPObject);
  683. }
  684. *ppIpsecISAKMPObject = NULL;
  685. goto cleanup;
  686. }
  687. DWORD
  688. UnMarshallWMINFAObject(
  689. IWbemServices *pWbemServices,
  690. LPWSTR pszIpsecNFAReference,
  691. PIPSEC_NFA_OBJECT * ppIpsecNFAObject,
  692. LPWSTR * ppszFilterReference,
  693. LPWSTR * ppszNegPolReference
  694. )
  695. {
  696. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  697. HKEY hRegKey = NULL;
  698. DWORD dwType = 0;
  699. DWORD dwSize = 0;
  700. DWORD dwIpsecDataType = 0;
  701. DWORD dwWhenChanged = 0;
  702. LPBYTE pBuffer = NULL;
  703. DWORD i = 0;
  704. DWORD dwCount = 0;
  705. DWORD dwError = 0;
  706. LPWSTR pszTempFilterReference = NULL;
  707. LPWSTR pszTempNegPolReference = NULL;
  708. LPWSTR pszRelativeName = NULL;
  709. DWORD dwRootPathLen = 0;
  710. HRESULT hr = S_OK;
  711. ////start
  712. VARIANT var; //=>pszIpsecNFAReference
  713. IWbemClassObject *pWbemClassObject = NULL;
  714. ////wbem
  715. IWbemClassObject *pObj = NULL;
  716. LPWSTR objPathA = L"RSOP_IPSECPolicySetting.id=";
  717. LPWSTR objPath = NULL;
  718. BSTR bstrObjPath = NULL;
  719. VariantInit(&var);
  720. ////keep
  721. if (!pszIpsecNFAReference || !*pszIpsecNFAReference) {
  722. dwError = ERROR_INVALID_DATA;
  723. BAIL_ON_WIN32_ERROR(dwError);
  724. }
  725. objPath = (LPWSTR)AllocPolMem(
  726. sizeof(WCHAR)*(wcslen(objPathA)+wcslen(pszIpsecNFAReference)+3)
  727. );
  728. if(!objPath) {
  729. dwError = ERROR_OUTOFMEMORY;
  730. BAIL_ON_WIN32_ERROR(dwError);
  731. }
  732. wcscpy(objPath, objPathA);
  733. wcscat(objPath, L"\"");
  734. wcscat(objPath, pszIpsecNFAReference);
  735. wcscat(objPath, L"\"");
  736. bstrObjPath = SysAllocString(objPath);
  737. if(!bstrObjPath) {
  738. dwError = ERROR_OUTOFMEMORY;
  739. BAIL_ON_WIN32_ERROR(dwError);
  740. }
  741. hr = IWbemServices_GetObject(
  742. pWbemServices,
  743. bstrObjPath,
  744. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  745. 0,
  746. &pObj,
  747. 0
  748. );
  749. SysFreeString(bstrObjPath);
  750. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  751. pWbemClassObject = pObj;
  752. pIpsecNFAObject = (PIPSEC_NFA_OBJECT)AllocPolMem(
  753. sizeof(IPSEC_NFA_OBJECT)
  754. );
  755. if (!pIpsecNFAObject) {
  756. dwError = ERROR_OUTOFMEMORY;
  757. BAIL_ON_WIN32_ERROR(dwError);
  758. }
  759. pIpsecNFAObject->pszDistinguishedName = AllocPolStr(
  760. pszIpsecNFAReference //(LPWSTR)var.bstrVal
  761. );
  762. if (!pIpsecNFAObject->pszDistinguishedName) {
  763. dwError = ERROR_OUTOFMEMORY;
  764. BAIL_ON_WIN32_ERROR(dwError);
  765. }
  766. //
  767. // Client does not always write the Name for an NFA
  768. //
  769. dwError = WMIstoreQueryValue(
  770. pWbemClassObject,
  771. L"ipsecName",
  772. VT_BSTR,
  773. (LPBYTE *)&pIpsecNFAObject->pszIpsecName,
  774. &dwSize
  775. );
  776. // BAIL_ON_WIN32_ERROR(dwError); //req
  777. dwError = WMIstoreQueryValue(
  778. pWbemClassObject,
  779. L"description",
  780. VT_BSTR,
  781. (LPBYTE *)&pIpsecNFAObject->pszDescription,
  782. &dwSize
  783. );
  784. // BAIL_ON_WIN32_ERROR(dwError); //req
  785. dwError = WMIstoreQueryValue(
  786. pWbemClassObject,
  787. L"ipsecID",
  788. VT_BSTR,
  789. (LPBYTE *)&pIpsecNFAObject->pszIpsecID,
  790. &dwSize
  791. );
  792. BAIL_ON_WIN32_ERROR(dwError);
  793. hr = IWbemClassObject_Get(pWbemClassObject,
  794. L"ipsecDataType",
  795. 0,
  796. &var,
  797. 0,
  798. 0);
  799. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  800. dwIpsecDataType = var.lVal;
  801. pIpsecNFAObject->dwIpsecDataType = dwIpsecDataType;
  802. //
  803. // unmarshall the ipsecData blob
  804. //
  805. dwError = WMIstoreQueryValue(
  806. pWbemClassObject,
  807. L"ipsecData",
  808. VT_ARRAY|VT_UI1,
  809. &pIpsecNFAObject->pIpsecData,
  810. &pIpsecNFAObject->dwIpsecDataLen
  811. );
  812. BAIL_ON_WIN32_ERROR(dwError);
  813. dwError = WMIstoreQueryValue(
  814. pWbemClassObject,
  815. L"ipsecOwnersReference",
  816. VT_ARRAY|VT_BSTR,
  817. (LPBYTE *)&pIpsecNFAObject->pszIpsecOwnersReference,
  818. &dwSize
  819. );
  820. // BAIL_ON_WIN32_ERROR(dwError); //req
  821. dwError = WMIstoreQueryValue(
  822. pWbemClassObject,
  823. L"ipsecNegotiationPolicyReference",
  824. VT_BSTR,
  825. (LPBYTE *)&pIpsecNFAObject->pszIpsecNegPolReference,
  826. &dwSize
  827. );
  828. BAIL_ON_WIN32_ERROR(dwError);
  829. dwError = WMIstoreQueryValue(
  830. pWbemClassObject,
  831. L"ipsecFilterReference",
  832. VT_ARRAY|VT_BSTR,
  833. (LPBYTE *)&pIpsecNFAObject->pszIpsecFilterReference,
  834. &dwSize
  835. );
  836. // BAIL_ON_WIN32_ERROR(dwError); //req
  837. hr = IWbemClassObject_Get(pWbemClassObject,
  838. L"whenChanged",
  839. 0,
  840. &var,
  841. 0,
  842. 0);
  843. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  844. dwWhenChanged = var.lVal;
  845. pIpsecNFAObject->dwWhenChanged = dwWhenChanged;
  846. if (pIpsecNFAObject->pszIpsecFilterReference && *(pIpsecNFAObject->pszIpsecFilterReference)) {
  847. pszTempFilterReference = AllocPolStr(
  848. pIpsecNFAObject->pszIpsecFilterReference
  849. );
  850. if (!pszTempFilterReference) {
  851. dwError = ERROR_OUTOFMEMORY;
  852. BAIL_ON_WIN32_ERROR(dwError);
  853. }
  854. }
  855. pszTempNegPolReference = AllocPolStr(
  856. pIpsecNFAObject->pszIpsecNegPolReference
  857. );
  858. if (!pszTempNegPolReference) {
  859. dwError = ERROR_OUTOFMEMORY;
  860. BAIL_ON_WIN32_ERROR(dwError);
  861. }
  862. *ppszFilterReference = pszTempFilterReference;
  863. *ppszNegPolReference = pszTempNegPolReference;
  864. *ppIpsecNFAObject = pIpsecNFAObject;
  865. cleanup:
  866. if(objPath) {
  867. FreePolStr(objPath);
  868. }
  869. if(pWbemClassObject)
  870. IWbemClassObject_Release(pWbemClassObject);
  871. return(dwError);
  872. error:
  873. if (pIpsecNFAObject) {
  874. FreeIpsecNFAObject(pIpsecNFAObject);
  875. }
  876. if (pszTempFilterReference) {
  877. FreePolStr(pszTempFilterReference);
  878. }
  879. if (pszTempNegPolReference) {
  880. FreePolStr(pszTempNegPolReference);
  881. }
  882. *ppIpsecNFAObject = NULL;
  883. *ppszFilterReference = NULL;
  884. *ppszNegPolReference = NULL;
  885. goto cleanup;
  886. }
  887. DWORD
  888. WMIstoreQueryValue(
  889. IWbemClassObject *pWbemClassObject,
  890. LPWSTR pszValueName,
  891. DWORD dwType,
  892. LPBYTE *ppValueData,
  893. LPDWORD pdwSize
  894. )
  895. {
  896. DWORD dwSize = 0;
  897. LPWSTR pszValueData = NULL;
  898. DWORD dwError = 0;
  899. HRESULT hr = S_OK;
  900. LPBYTE pBuffer = NULL;
  901. LPWSTR pszBuf = NULL;
  902. SAFEARRAY *pSafeArray = NULL;
  903. VARIANT var;
  904. DWORD i = 0;
  905. DWORD dw = 0;
  906. LPWSTR pszTmp = NULL;
  907. WCHAR pszTemp[MAX_PATH];
  908. LPWSTR pszString = NULL;
  909. LPWSTR pMem = NULL;
  910. LPWSTR *ppszTmp = NULL;
  911. long lUbound = 0;
  912. DWORD dwCount = 0;
  913. LPBYTE pdw = NULL;
  914. BSTR HUGEP *pbstrTmp = NULL;
  915. BYTE HUGEP *pbyteTmp = NULL;
  916. VariantInit(&var);
  917. if(!pWbemClassObject) {
  918. dwError = ERROR_INVALID_DATA;
  919. BAIL_ON_WIN32_ERROR(dwError);
  920. }
  921. hr = IWbemClassObject_Get(pWbemClassObject,
  922. pszValueName,
  923. 0,
  924. &var,
  925. 0,
  926. 0);
  927. if(hr == WBEM_E_NOT_FOUND) {
  928. dwError = ERROR_INVALID_DATA;
  929. BAIL_ON_WIN32_ERROR(dwError);
  930. }
  931. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  932. ////sanity check
  933. if(dwType != var.vt) {
  934. dwError = ERROR_INVALID_DATA;
  935. BAIL_ON_WIN32_ERROR(dwError);
  936. }
  937. switch(dwType) {
  938. case VT_BSTR:
  939. pszTmp = var.bstrVal;
  940. dwSize = wcslen(pszTmp)*sizeof(WCHAR);
  941. pBuffer = (LPBYTE)AllocPolStr(pszTmp);
  942. if (!pBuffer) {
  943. dwError = ERROR_OUTOFMEMORY;
  944. BAIL_ON_WIN32_ERROR(dwError);
  945. }
  946. break;
  947. case (VT_ARRAY|VT_UI1):
  948. pSafeArray = var.parray;
  949. if(!pSafeArray) {
  950. dwError = ERROR_INVALID_DATA;
  951. BAIL_ON_WIN32_ERROR(dwError);
  952. }
  953. hr = SafeArrayGetUBound(
  954. pSafeArray,
  955. 1,
  956. &lUbound
  957. );
  958. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  959. dwSize = lUbound+1;
  960. if (dwSize == 0) {
  961. dwError = ERROR_INVALID_DATA;
  962. BAIL_ON_WIN32_ERROR(dwError);
  963. }
  964. pBuffer = (LPBYTE)AllocPolMem(dwSize);
  965. if (!pBuffer) {
  966. dwError = ERROR_OUTOFMEMORY;
  967. BAIL_ON_WIN32_ERROR(dwError);
  968. }
  969. for(i = 0; i < dwSize; i++) {
  970. hr = SafeArrayGetElement(pSafeArray, (long *)&i, &pBuffer[i]);
  971. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  972. }
  973. break;
  974. case (VT_ARRAY|VT_BSTR):
  975. pSafeArray = var.parray;
  976. if(!pSafeArray) {
  977. dwError = ERROR_INVALID_DATA;
  978. BAIL_ON_WIN32_ERROR(dwError);
  979. }
  980. hr = SafeArrayGetUBound(
  981. pSafeArray,
  982. 1,
  983. &lUbound
  984. );
  985. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  986. dwCount = lUbound+1;
  987. if (dwCount == 0) {
  988. dwError = ERROR_INVALID_DATA;
  989. BAIL_ON_WIN32_ERROR(dwError);
  990. }
  991. ppszTmp = (LPWSTR *)AllocPolMem(
  992. sizeof(LPWSTR)*dwCount
  993. );
  994. if (!ppszTmp) {
  995. dwError = ERROR_OUTOFMEMORY;
  996. BAIL_ON_WIN32_ERROR(dwError);
  997. }
  998. hr = SafeArrayAccessData(
  999. pSafeArray,
  1000. (void HUGEP**)&pbstrTmp
  1001. );
  1002. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  1003. for(i = 0; i < dwCount; i++) {
  1004. pszTmp = pbstrTmp[i];
  1005. ppszTmp[i] = AllocPolStr(pszTmp);
  1006. if (!ppszTmp[i]) {
  1007. dwError = ERROR_OUTOFMEMORY;
  1008. BAIL_ON_WIN32_ERROR(dwError);
  1009. }
  1010. }
  1011. SafeArrayUnaccessData(pSafeArray);
  1012. //ppszTmp => string array
  1013. for(i = 0; i < dwCount; i++) {
  1014. dwSize += wcslen(ppszTmp[i])+1;
  1015. }
  1016. dwSize++;
  1017. pMem = (LPWSTR)AllocPolMem(sizeof(WCHAR)*dwSize);
  1018. if (!pMem) {
  1019. dwError = ERROR_OUTOFMEMORY;
  1020. BAIL_ON_WIN32_ERROR(dwError);
  1021. }
  1022. //adjust dwSize to byte size
  1023. dwSize *= sizeof(WCHAR);
  1024. pszString = pMem;
  1025. for(i = 0; i < dwCount; i++) {
  1026. memcpy(pszString, ppszTmp[i], wcslen(ppszTmp[i])*sizeof(WCHAR));
  1027. pszString += wcslen(pszString)+1;
  1028. }
  1029. pBuffer = (LPBYTE)pMem;
  1030. break;
  1031. default:
  1032. dwError = ERROR_INVALID_PARAMETER;
  1033. BAIL_ON_WIN32_ERROR(dwError);
  1034. break;
  1035. }
  1036. switch(dwType) {
  1037. case VT_BSTR:
  1038. pszBuf = (LPWSTR)pBuffer;
  1039. if (!pszBuf || !*pszBuf) {
  1040. dwError = ERROR_INVALID_DATA;
  1041. BAIL_ON_WIN32_ERROR(dwError);
  1042. }
  1043. break;
  1044. default:
  1045. break;
  1046. }
  1047. *ppValueData = pBuffer;
  1048. *pdwSize = dwSize;
  1049. VariantClear(&var);
  1050. cleanup:
  1051. if(ppszTmp) {
  1052. FreePolMem(ppszTmp);
  1053. }
  1054. return(dwError);
  1055. error:
  1056. if (pBuffer) {
  1057. FreePolMem(pBuffer);
  1058. }
  1059. *ppValueData = NULL;
  1060. *pdwSize = 0;
  1061. goto cleanup;
  1062. }
  1063. HRESULT
  1064. ReadPolicyObjectFromDirectoryEx(
  1065. LPWSTR pszMachineName,
  1066. LPWSTR pszPolicyDN,
  1067. BOOL bDeepRead,
  1068. PIPSEC_POLICY_OBJECT * ppIpsecPolicyObject
  1069. )
  1070. {
  1071. DWORD dwError = 0;
  1072. HLDAP hLdapBindHandle = NULL;
  1073. LPWSTR pszDefaultDirectory = NULL;
  1074. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  1075. if (!pszMachineName || !*pszMachineName) {
  1076. dwError = ComputeDefaultDirectory(
  1077. &pszDefaultDirectory
  1078. );
  1079. BAIL_ON_WIN32_ERROR(dwError);
  1080. dwError = OpenDirectoryServerHandle(
  1081. pszDefaultDirectory,
  1082. 389,
  1083. &hLdapBindHandle
  1084. );
  1085. BAIL_ON_WIN32_ERROR(dwError);
  1086. } else {
  1087. dwError = OpenDirectoryServerHandle(
  1088. pszMachineName,
  1089. 389,
  1090. &hLdapBindHandle
  1091. );
  1092. BAIL_ON_WIN32_ERROR(dwError);
  1093. }
  1094. if (bDeepRead) {
  1095. dwError = ReadPolicyObjectFromDirectory(
  1096. hLdapBindHandle,
  1097. pszPolicyDN,
  1098. &pIpsecPolicyObject
  1099. );
  1100. BAIL_ON_WIN32_ERROR(dwError);
  1101. } else {
  1102. dwError = ShallowReadPolicyObjectFromDirectory(
  1103. hLdapBindHandle,
  1104. pszPolicyDN,
  1105. &pIpsecPolicyObject
  1106. );
  1107. BAIL_ON_WIN32_ERROR(dwError);
  1108. }
  1109. *ppIpsecPolicyObject = pIpsecPolicyObject;
  1110. cleanup:
  1111. if (pszDefaultDirectory) {
  1112. FreePolStr(pszDefaultDirectory);
  1113. }
  1114. if (hLdapBindHandle) {
  1115. CloseDirectoryServerHandle(hLdapBindHandle);
  1116. }
  1117. return (HRESULT_FROM_WIN32(dwError));
  1118. error:
  1119. *ppIpsecPolicyObject = NULL;
  1120. goto cleanup;
  1121. }
  1122. HRESULT
  1123. WritePolicyObjectDirectoryToWMI(
  1124. IWbemServices *pWbemServices,
  1125. PIPSEC_POLICY_OBJECT pIpsecPolicyObject,
  1126. PGPO_INFO pGPOInfo
  1127. )
  1128. {
  1129. HRESULT hr = S_OK;
  1130. PIPSEC_POLICY_OBJECT pIpsecWMIPolicyObject = NULL;
  1131. //
  1132. // Create a copy of the directory policy in WMI terms
  1133. //
  1134. hr = CloneDirectoryPolicyObjectEx(
  1135. pIpsecPolicyObject,
  1136. &pIpsecWMIPolicyObject
  1137. );
  1138. BAIL_ON_HRESULT_ERROR(hr);
  1139. //
  1140. // Write the WMI policy
  1141. //
  1142. hr = PersistWMIObject(
  1143. pWbemServices,
  1144. pIpsecWMIPolicyObject,
  1145. pGPOInfo
  1146. );
  1147. BAIL_ON_HRESULT_ERROR(hr);
  1148. cleanup:
  1149. if (pIpsecWMIPolicyObject) {
  1150. FreeIpsecPolicyObject(
  1151. pIpsecWMIPolicyObject
  1152. );
  1153. }
  1154. return(hr);
  1155. error:
  1156. goto cleanup;
  1157. }
  1158. DWORD
  1159. CreateIWbemServices(
  1160. LPWSTR pszIpsecWMINamespace,
  1161. IWbemServices **ppWbemServices
  1162. )
  1163. {
  1164. DWORD dwError = 0;
  1165. HRESULT hr = S_OK;
  1166. IWbemLocator *pWbemLocator = NULL;
  1167. LPWSTR pszIpsecWMIPath = NULL;
  1168. BSTR bstrIpsecWMIPath = NULL;
  1169. if(!pszIpsecWMINamespace || !*pszIpsecWMINamespace) {
  1170. pszIpsecWMIPath = gpszIpsecWMINamespace;
  1171. } else {
  1172. pszIpsecWMIPath = pszIpsecWMINamespace;
  1173. }
  1174. hr = CoCreateInstance(
  1175. &CLSID_WbemLocator,
  1176. NULL,
  1177. CLSCTX_INPROC_SERVER,
  1178. &IID_IWbemLocator,
  1179. &pWbemLocator
  1180. );
  1181. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  1182. bstrIpsecWMIPath = SysAllocString(pszIpsecWMIPath);
  1183. if(!bstrIpsecWMIPath) {
  1184. dwError = ERROR_OUTOFMEMORY;
  1185. BAIL_ON_WIN32_ERROR(dwError);
  1186. }
  1187. hr = IWbemLocator_ConnectServer(
  1188. pWbemLocator,
  1189. bstrIpsecWMIPath,
  1190. NULL,
  1191. NULL,
  1192. NULL,
  1193. 0,
  1194. NULL,
  1195. NULL,
  1196. ppWbemServices
  1197. );
  1198. SysFreeString(bstrIpsecWMIPath);
  1199. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  1200. if(pWbemLocator)
  1201. IWbemLocator_Release(pWbemLocator);
  1202. error:
  1203. return (dwError);
  1204. }