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.

789 lines
18 KiB

  1. #include "precomp.h"
  2. LPWSTR gpszWirelessWMIObject = L"RSOP_IEEE80211PolicySetting";
  3. HRESULT
  4. PolSysAllocString(
  5. BSTR * pbsStr,
  6. const OLECHAR * sz
  7. )
  8. {
  9. *pbsStr = SysAllocString(sz);
  10. if (!pbsStr) {
  11. return E_OUTOFMEMORY;
  12. }
  13. return S_OK;
  14. }
  15. HRESULT
  16. PersistWMIObject(
  17. IWbemServices *pWbemServices,
  18. PWIRELESS_POLICY_OBJECT pWirelessWMIPolicyObject,
  19. PGPO_INFO pGPOInfo
  20. )
  21. {
  22. HRESULT hr;
  23. IWbemClassObject *pWbemWIRELESSObj = NULL;
  24. BSTR bstrWirelessWMIObject = NULL;
  25. // If this first GPO we are writing after a policy
  26. // update, clear the WMI store.
  27. if (pGPOInfo->uiPrecedence == pGPOInfo->uiTotalGPOs) {
  28. hr = DeleteWMIClassObject(
  29. pWbemServices,
  30. gpszWirelessWMIObject
  31. );
  32. BAIL_ON_HRESULT_ERROR(hr);
  33. }
  34. bstrWirelessWMIObject = SysAllocString(gpszWirelessWMIObject);
  35. if(!bstrWirelessWMIObject) {
  36. hr = E_OUTOFMEMORY;
  37. BAIL_ON_HRESULT_ERROR(hr);
  38. }
  39. hr = IWbemServices_GetObject(pWbemServices,
  40. bstrWirelessWMIObject,
  41. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  42. 0,
  43. &pWbemWIRELESSObj,
  44. 0);
  45. SysFreeString(bstrWirelessWMIObject);
  46. BAIL_ON_HRESULT_ERROR(hr);
  47. hr = PersistPolicyObjectEx(
  48. pWbemServices,
  49. pWbemWIRELESSObj,
  50. pWirelessWMIPolicyObject,
  51. pGPOInfo
  52. );
  53. error:
  54. //close WMI?
  55. if(pWbemWIRELESSObj)
  56. IWbemClassObject_Release(pWbemWIRELESSObj);
  57. return (hr);
  58. }
  59. HRESULT
  60. PersistPolicyObjectEx(
  61. IWbemServices *pWbemServices,
  62. IWbemClassObject *pWbemClassObj,
  63. PWIRELESS_POLICY_OBJECT pWirelessPolicyObject,
  64. PGPO_INFO pGPOInfo
  65. )
  66. {
  67. HRESULT hr = 0;
  68. IWbemClassObject *pInstWIRELESSObj = NULL;
  69. VARIANT var;
  70. VariantInit(&var);
  71. //start
  72. hr = IWbemClassObject_SpawnInstance(
  73. pWbemClassObj,
  74. 0,
  75. &pInstWIRELESSObj
  76. );
  77. BAIL_ON_HRESULT_ERROR(hr);
  78. var.vt = VT_BSTR;
  79. var.bstrVal = SKIPL(pWirelessPolicyObject->pszWirelessOwnersReference);
  80. hr = IWbemClassObject_Put(
  81. pInstWIRELESSObj,
  82. L"id",
  83. 0,
  84. &var,
  85. 0
  86. );
  87. BAIL_ON_HRESULT_ERROR(hr);
  88. hr = PersistComnRSOPPolicySettings(
  89. pInstWIRELESSObj,
  90. pGPOInfo
  91. );
  92. BAIL_ON_HRESULT_ERROR(hr);
  93. var.vt = VT_BSTR;
  94. hr = PolSysAllocString(&var.bstrVal, L"msieee80211-Policy");
  95. BAIL_ON_HRESULT_ERROR(hr);
  96. hr = IWbemClassObject_Put(
  97. pInstWIRELESSObj,
  98. L"ClassName",
  99. 0,
  100. &var,
  101. 0
  102. );
  103. VariantClear(&var);
  104. BAIL_ON_HRESULT_ERROR(hr);
  105. if (pWirelessPolicyObject->pszDescription) {
  106. var.vt = VT_BSTR;
  107. var.bstrVal = SKIPL(pWirelessPolicyObject->pszDescription);
  108. hr = IWbemClassObject_Put(
  109. pInstWIRELESSObj,
  110. L"description",
  111. 0,
  112. &var,
  113. 0
  114. );
  115. BAIL_ON_HRESULT_ERROR(hr);
  116. } else {
  117. //delete description?
  118. var.vt = VT_BSTR;
  119. hr = PolSysAllocString(&var.bstrVal, L"");
  120. BAIL_ON_HRESULT_ERROR(hr);
  121. hr = IWbemClassObject_Put(
  122. pInstWIRELESSObj,
  123. L"description",
  124. 0,
  125. &var,
  126. 0
  127. );
  128. VariantClear(&var);
  129. BAIL_ON_HRESULT_ERROR(hr);
  130. }
  131. if (pWirelessPolicyObject->pszWirelessOwnersReference) {
  132. var.vt = VT_BSTR;
  133. var.bstrVal = SKIPL(pWirelessPolicyObject->pszWirelessOwnersReference);
  134. hr = IWbemClassObject_Put(
  135. pInstWIRELESSObj,
  136. L"name",
  137. 0,
  138. &var,
  139. 0
  140. );
  141. BAIL_ON_HRESULT_ERROR(hr);
  142. }
  143. if (pWirelessPolicyObject->pszWirelessName) {
  144. var.vt = VT_BSTR;
  145. var.bstrVal = SKIPL(pWirelessPolicyObject->pszWirelessName);
  146. hr = IWbemClassObject_Put(
  147. pInstWIRELESSObj,
  148. L"msieee80211Name",
  149. 0,
  150. &var,
  151. 0
  152. );
  153. BAIL_ON_HRESULT_ERROR(hr);
  154. }
  155. if (pWirelessPolicyObject->pszWirelessID) {
  156. var.vt = VT_BSTR;
  157. var.bstrVal = SKIPL(pWirelessPolicyObject->pszWirelessID);
  158. hr = IWbemClassObject_Put(
  159. pInstWIRELESSObj,
  160. L"msieee80211ID",
  161. 0,
  162. &var,
  163. 0
  164. );
  165. BAIL_ON_HRESULT_ERROR(hr);
  166. }
  167. var.vt = VT_I4;
  168. var.lVal = pWirelessPolicyObject->dwWirelessDataType;
  169. hr = IWbemClassObject_Put(
  170. pInstWIRELESSObj,
  171. L"msieee80211DataType",
  172. 0,
  173. &var,
  174. 0
  175. );
  176. BAIL_ON_HRESULT_ERROR(hr);
  177. if (pWirelessPolicyObject->pWirelessData) {
  178. hr = LogBlobPropertyEx(
  179. pInstWIRELESSObj,
  180. L"msieee80211Data",
  181. pWirelessPolicyObject->pWirelessData,
  182. pWirelessPolicyObject->dwWirelessDataLen
  183. );
  184. BAIL_ON_HRESULT_ERROR(hr);
  185. }
  186. var.vt = VT_I4;
  187. var.lVal = pWirelessPolicyObject->dwWhenChanged;
  188. hr = IWbemClassObject_Put(
  189. pInstWIRELESSObj,
  190. L"whenChanged",
  191. 0,
  192. &var,
  193. 0
  194. );
  195. BAIL_ON_HRESULT_ERROR(hr);
  196. hr = IWbemServices_PutInstance(
  197. pWbemServices,
  198. pInstWIRELESSObj,
  199. WBEM_FLAG_CREATE_OR_UPDATE,
  200. 0,
  201. 0
  202. );
  203. BAIL_ON_HRESULT_ERROR(hr);
  204. error:
  205. if (pInstWIRELESSObj) {
  206. IWbemClassObject_Release(pInstWIRELESSObj);
  207. }
  208. return(hr);
  209. }
  210. HRESULT
  211. PersistComnRSOPPolicySettings(
  212. IWbemClassObject * pInstWIRELESSObj,
  213. PGPO_INFO pGPOInfo
  214. )
  215. {
  216. HRESULT hr = S_OK;
  217. VARIANT var;
  218. VariantInit(&var);
  219. var.vt = VT_BSTR;
  220. var.bstrVal = pGPOInfo->bsCreationtime;
  221. hr = IWbemClassObject_Put(
  222. pInstWIRELESSObj,
  223. L"creationTime",
  224. 0,
  225. &var,
  226. 0
  227. );
  228. BAIL_ON_HRESULT_ERROR(hr);
  229. var.vt = VT_BSTR;
  230. var.bstrVal = pGPOInfo->bsGPOID;
  231. hr = IWbemClassObject_Put(
  232. pInstWIRELESSObj,
  233. L"GPOID",
  234. 0,
  235. &var,
  236. 0
  237. );
  238. BAIL_ON_HRESULT_ERROR(hr);
  239. var.vt = VT_I4;
  240. var.lVal = pGPOInfo->uiPrecedence;
  241. hr = IWbemClassObject_Put(
  242. pInstWIRELESSObj,
  243. L"precedence",
  244. 0,
  245. &var,
  246. 0
  247. );
  248. BAIL_ON_HRESULT_ERROR(hr);
  249. var.vt = VT_BSTR;
  250. var.bstrVal = pGPOInfo->bsSOMID;
  251. hr = IWbemClassObject_Put(
  252. pInstWIRELESSObj,
  253. L"SOMID",
  254. 0,
  255. &var,
  256. 0
  257. );
  258. BAIL_ON_HRESULT_ERROR(hr);
  259. error:
  260. var.vt = VT_EMPTY;
  261. return hr;
  262. }
  263. HRESULT
  264. CloneDirectoryPolicyObjectEx(
  265. PWIRELESS_POLICY_OBJECT pWirelessPolicyObject,
  266. PWIRELESS_POLICY_OBJECT * ppWirelessWMIPolicyObject
  267. )
  268. {
  269. DWORD dwError = 0;
  270. PWIRELESS_POLICY_OBJECT pWirelessWMIPolicyObject = NULL;
  271. LPWSTR pszUniquePolicyName = NULL;
  272. WCHAR szUniquePolicyName[MAX_PATH];
  273. //malloc policy object
  274. pWirelessWMIPolicyObject = (PWIRELESS_POLICY_OBJECT)AllocPolMem(
  275. sizeof(WIRELESS_POLICY_OBJECT)
  276. );
  277. if (!pWirelessWMIPolicyObject) {
  278. dwError = ERROR_OUTOFMEMORY;
  279. BAIL_ON_WIN32_ERROR(dwError);
  280. }
  281. //
  282. // Now copy the rest of the data in the object
  283. //
  284. //copy owners ref
  285. if (pWirelessPolicyObject->pszWirelessID) {
  286. /*
  287. dwError = CopyPolicyDSToWMIString(
  288. pWirelessPolicyObject->pszWirelessOwnersReference,
  289. &pWirelessWMIPolicyObject->pszWirelessOwnersReference
  290. );
  291. */
  292. wcscpy(szUniquePolicyName, L"\0");
  293. wcscpy(szUniquePolicyName, L"msieee80211-Policy");
  294. wcscat(szUniquePolicyName, pWirelessPolicyObject->pszWirelessID);
  295. pszUniquePolicyName = AllocPolBstrStr(szUniquePolicyName);
  296. if (!pszUniquePolicyName) {
  297. dwError = ERROR_OUTOFMEMORY;
  298. BAIL_ON_WIN32_ERROR(dwError);
  299. }
  300. pWirelessWMIPolicyObject->pszWirelessOwnersReference = pszUniquePolicyName;
  301. }
  302. //copy name
  303. if (pWirelessPolicyObject->pszWirelessName) {
  304. pWirelessWMIPolicyObject->pszWirelessName = AllocPolBstrStr(
  305. pWirelessPolicyObject->pszWirelessName
  306. );
  307. if (!pWirelessWMIPolicyObject->pszWirelessName) {
  308. dwError = ERROR_OUTOFMEMORY;
  309. BAIL_ON_WIN32_ERROR(dwError);
  310. }
  311. }
  312. //copy wirelessid
  313. if (pWirelessPolicyObject->pszWirelessID) {
  314. pWirelessWMIPolicyObject->pszWirelessID = AllocPolBstrStr(
  315. pWirelessPolicyObject->pszWirelessID
  316. );
  317. if (!pWirelessWMIPolicyObject->pszWirelessID) {
  318. dwError = ERROR_OUTOFMEMORY;
  319. BAIL_ON_WIN32_ERROR(dwError);
  320. }
  321. }
  322. //copy datatype
  323. pWirelessWMIPolicyObject->dwWirelessDataType = pWirelessPolicyObject->dwWirelessDataType;
  324. //copy wirelessdata
  325. if (pWirelessPolicyObject->pWirelessData) {
  326. dwError = CopyBinaryValue(
  327. pWirelessPolicyObject->pWirelessData,
  328. pWirelessPolicyObject->dwWirelessDataLen,
  329. &pWirelessWMIPolicyObject->pWirelessData
  330. );
  331. BAIL_ON_WIN32_ERROR(dwError);
  332. pWirelessWMIPolicyObject->dwWirelessDataLen = pWirelessPolicyObject->dwWirelessDataLen;
  333. }
  334. //copy description
  335. if (pWirelessPolicyObject->pszDescription) {
  336. pWirelessWMIPolicyObject->pszDescription = AllocPolBstrStr(
  337. pWirelessPolicyObject->pszDescription
  338. );
  339. if (!pWirelessWMIPolicyObject->pszDescription) {
  340. dwError = ERROR_OUTOFMEMORY;
  341. BAIL_ON_WIN32_ERROR(dwError);
  342. }
  343. }
  344. //copy whenchanged
  345. pWirelessWMIPolicyObject->dwWhenChanged = pWirelessPolicyObject->dwWhenChanged;
  346. //commit & return
  347. *ppWirelessWMIPolicyObject = pWirelessWMIPolicyObject;
  348. return(dwError);
  349. error:
  350. if (pWirelessWMIPolicyObject) {
  351. FreeWirelessPolicyObject(
  352. pWirelessWMIPolicyObject
  353. );
  354. }
  355. *ppWirelessWMIPolicyObject = NULL;
  356. return(HRESULT_FROM_WIN32(dwError));
  357. }
  358. DWORD
  359. CopyPolicyDSToFQWMIString(
  360. LPWSTR pszPolicyDN,
  361. LPWSTR * ppszPolicyName
  362. )
  363. {
  364. DWORD dwError = 0;
  365. WCHAR szCommonName[MAX_PATH];
  366. LPWSTR pszGuidName = NULL;
  367. LPWSTR pszPolicyName = NULL;
  368. DWORD dwStringSize = 0;
  369. dwError = ComputePrelimCN(
  370. pszPolicyDN,
  371. szCommonName
  372. );
  373. BAIL_ON_WIN32_ERROR(dwError);
  374. dwError = ComputeGUIDName(
  375. szCommonName,
  376. &pszGuidName
  377. );
  378. BAIL_ON_WIN32_ERROR(dwError);
  379. dwStringSize = wcslen(pszGuidName);
  380. dwStringSize += 1;
  381. pszPolicyName = (LPWSTR)AllocPolMem(dwStringSize*sizeof(WCHAR));
  382. if (!pszPolicyName) {
  383. dwError = ERROR_OUTOFMEMORY;
  384. BAIL_ON_WIN32_ERROR(dwError);
  385. }
  386. wcscpy(pszPolicyName, pszGuidName);
  387. *ppszPolicyName = pszPolicyName;
  388. return(dwError);
  389. error:
  390. *ppszPolicyName = NULL;
  391. return(dwError);
  392. }
  393. HRESULT
  394. WMIWriteMultiValuedString(
  395. IWbemClassObject *pInstWbemClassObject,
  396. LPWSTR pszValueName,
  397. LPWSTR * ppszStringReferences,
  398. DWORD dwNumStringReferences
  399. )
  400. {
  401. HRESULT hr = S_OK;
  402. SAFEARRAYBOUND arrayBound[1];
  403. SAFEARRAY *pSafeArray = NULL;
  404. VARIANT var;
  405. DWORD i = 0;
  406. BSTR Bstr = NULL;
  407. VariantInit(&var);
  408. if (!ppszStringReferences) {
  409. hr = E_INVALIDARG;
  410. BAIL_ON_HRESULT_ERROR(hr);
  411. }
  412. arrayBound[0].lLbound = 0;
  413. arrayBound[0].cElements = dwNumStringReferences;
  414. pSafeArray = SafeArrayCreate(VT_BSTR, 1, arrayBound);
  415. if (!pSafeArray)
  416. {
  417. hr = E_OUTOFMEMORY;
  418. BAIL_ON_HRESULT_ERROR(hr);
  419. }
  420. for (i = 0; i < dwNumStringReferences; i++)
  421. {
  422. Bstr = SysAllocString(*(ppszStringReferences + i));
  423. if(!Bstr) {
  424. hr = E_OUTOFMEMORY;
  425. BAIL_ON_HRESULT_ERROR(hr);
  426. }
  427. hr = SafeArrayPutElement(pSafeArray, (long *)&i, Bstr);
  428. SysFreeString(Bstr);
  429. BAIL_ON_HRESULT_ERROR(hr);
  430. }
  431. var.vt = VT_ARRAY|VT_BSTR;
  432. var.parray = pSafeArray;
  433. hr = IWbemClassObject_Put(
  434. pInstWbemClassObject,
  435. pszValueName,
  436. 0,
  437. &var,
  438. 0
  439. );
  440. VariantClear(&var);
  441. BAIL_ON_HRESULT_ERROR(hr);
  442. error:
  443. return(hr);
  444. }
  445. DWORD
  446. CopyFilterDSToWMIString(
  447. LPWSTR pszFilterDN,
  448. LPWSTR * ppszFilterName
  449. )
  450. {
  451. DWORD dwError = 0;
  452. WCHAR szCommonName[MAX_PATH];
  453. LPWSTR pszGuidName = NULL;
  454. LPWSTR pszFilterName = NULL;
  455. dwError = ComputePrelimCN(
  456. pszFilterDN,
  457. szCommonName
  458. );
  459. BAIL_ON_WIN32_ERROR(dwError);
  460. dwError = ComputeGUIDName(
  461. szCommonName,
  462. &pszGuidName
  463. );
  464. BAIL_ON_WIN32_ERROR(dwError);
  465. pszFilterName = AllocPolBstrStr(pszGuidName);
  466. if (!pszFilterName) {
  467. dwError = ERROR_OUTOFMEMORY;
  468. BAIL_ON_WIN32_ERROR(dwError);
  469. }
  470. *ppszFilterName = pszFilterName;
  471. return(dwError);
  472. error:
  473. *ppszFilterName = NULL;
  474. return(dwError);
  475. }
  476. DWORD
  477. CopyPolicyDSToWMIString(
  478. LPWSTR pszPolicyDN,
  479. LPWSTR * ppszPolicyName
  480. )
  481. {
  482. DWORD dwError = 0;
  483. WCHAR szCommonName[MAX_PATH];
  484. LPWSTR pszGuidName = NULL;
  485. LPWSTR pszPolicyName = NULL;
  486. dwError = ComputePrelimCN(
  487. pszPolicyDN,
  488. szCommonName
  489. );
  490. BAIL_ON_WIN32_ERROR(dwError);
  491. dwError = ComputeGUIDName(
  492. szCommonName,
  493. &pszGuidName
  494. );
  495. BAIL_ON_WIN32_ERROR(dwError);
  496. pszPolicyName = AllocPolBstrStr(pszGuidName);
  497. if (!pszPolicyName) {
  498. dwError = ERROR_OUTOFMEMORY;
  499. BAIL_ON_WIN32_ERROR(dwError);
  500. }
  501. *ppszPolicyName = pszPolicyName;
  502. return(dwError);
  503. error:
  504. *ppszPolicyName = NULL;
  505. return(dwError);
  506. }
  507. HRESULT
  508. LogBlobPropertyEx(
  509. IWbemClassObject *pInstance,
  510. BSTR bstrPropName,
  511. BYTE *pbBlob,
  512. DWORD dwLen
  513. )
  514. {
  515. HRESULT hr = S_OK;
  516. SAFEARRAYBOUND arrayBound[1];
  517. SAFEARRAY *pSafeArray = NULL;
  518. VARIANT var;
  519. DWORD i = 0;
  520. VariantInit(&var);
  521. arrayBound[0].lLbound = 0;
  522. arrayBound[0].cElements = dwLen;
  523. pSafeArray = SafeArrayCreate(VT_UI1, 1, arrayBound);
  524. if (!pSafeArray)
  525. {
  526. hr = E_OUTOFMEMORY;
  527. BAIL_ON_HRESULT_ERROR(hr);
  528. }
  529. for (i = 0; i < dwLen; i++)
  530. {
  531. hr = SafeArrayPutElement(pSafeArray, (long *)&i, &pbBlob[i]);
  532. BAIL_ON_HRESULT_ERROR(hr);
  533. }
  534. var.vt = VT_ARRAY|VT_UI1;
  535. var.parray = pSafeArray;
  536. hr = IWbemClassObject_Put(
  537. pInstance,
  538. bstrPropName,
  539. 0,
  540. &var,
  541. 0
  542. );
  543. VariantClear(&var);
  544. BAIL_ON_HRESULT_ERROR(hr);
  545. error:
  546. return(hr);
  547. }
  548. HRESULT
  549. DeleteWMIClassObject(
  550. IWbemServices *pWbemServices,
  551. LPWSTR pszWirelessWMIObject
  552. )
  553. {
  554. HRESULT hr = S_OK;
  555. IEnumWbemClassObject *pEnum = NULL;
  556. IWbemClassObject *pObj = NULL;
  557. ULONG uReturned = 0;
  558. VARIANT var;
  559. BSTR bstrWirelessWMIObject = NULL;
  560. VariantInit(&var);
  561. bstrWirelessWMIObject = SysAllocString(pszWirelessWMIObject);
  562. if(!bstrWirelessWMIObject) {
  563. hr = E_OUTOFMEMORY;
  564. BAIL_ON_HRESULT_ERROR(hr);
  565. }
  566. hr = IWbemServices_CreateInstanceEnum(
  567. pWbemServices,
  568. bstrWirelessWMIObject,
  569. WBEM_FLAG_FORWARD_ONLY,
  570. 0,
  571. &pEnum
  572. );
  573. SysFreeString(bstrWirelessWMIObject);
  574. BAIL_ON_HRESULT_ERROR(hr);
  575. while(1)
  576. {
  577. hr = IEnumWbemClassObject_Next(
  578. pEnum,
  579. WBEM_INFINITE,
  580. 1,
  581. &pObj,
  582. &uReturned
  583. );
  584. if (hr == WBEM_S_NO_ERROR) {
  585. hr = IWbemClassObject_Get(
  586. pObj,
  587. L"__RELPATH",
  588. 0,
  589. &var,
  590. 0,
  591. 0
  592. );
  593. BAIL_ON_HRESULT_ERROR(hr);
  594. hr = IWbemServices_DeleteInstance(
  595. pWbemServices,
  596. var.bstrVal,
  597. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  598. NULL,
  599. NULL
  600. );
  601. BAIL_ON_HRESULT_ERROR(hr);
  602. //free
  603. if(pObj) {
  604. IWbemClassObject_Release(pObj);
  605. pObj = NULL;
  606. }
  607. VariantClear(&var);
  608. } else {
  609. if(hr == WBEM_S_FALSE) {
  610. break;
  611. } else {
  612. BAIL_ON_HRESULT_ERROR(hr);
  613. }
  614. }
  615. }
  616. hr = S_OK;
  617. cleanup:
  618. if(pEnum)
  619. IEnumWbemClassObject_Release(pEnum);
  620. return(hr);
  621. error:
  622. if (pObj) {
  623. IWbemClassObject_Release(pObj);
  624. pObj = NULL;
  625. }
  626. VariantClear(&var);
  627. goto cleanup;
  628. }
  629. LPWSTR
  630. AllocPolBstrStr(
  631. LPCWSTR pStr
  632. )
  633. {
  634. LPWSTR pMem=NULL;
  635. DWORD StrLen;
  636. if (!pStr)
  637. return 0;
  638. StrLen = wcslen(pStr);
  639. if (pMem = (LPWSTR)AllocPolMem( StrLen*sizeof(WCHAR) + sizeof(WCHAR)
  640. + sizeof(DWORD)))
  641. wcscpy(pMem+2, pStr); // Leaving 4 bytes for length
  642. *(DWORD *)pMem = StrLen*sizeof(WCHAR);
  643. return pMem;
  644. }