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.

593 lines
15 KiB

  1. #include "precomp.h"
  2. LPWSTR gpszWirelessWMINamespace = L"root\\rsop\\computer";
  3. DWORD
  4. Win32FromWmiHresult(
  5. HRESULT hr
  6. )
  7. {
  8. if (FAILED(hr)) {
  9. switch (hr) {
  10. case WBEM_E_ACCESS_DENIED:
  11. return ERROR_ACCESS_DENIED;
  12. case REGDB_E_CLASSNOTREG:
  13. case CLASS_E_NOAGGREGATION:
  14. case E_NOINTERFACE:
  15. case WBEM_E_INVALID_NAMESPACE:
  16. case WBEM_E_INVALID_PARAMETER:
  17. case WBEM_E_NOT_FOUND:
  18. case WBEM_E_INVALID_CLASS:
  19. case WBEM_E_INVALID_OBJECT_PATH:
  20. return ERROR_INVALID_PARAMETER;
  21. case WBEM_E_OUT_OF_MEMORY:
  22. return ERROR_OUTOFMEMORY;
  23. case WBEM_E_TRANSPORT_FAILURE:
  24. return RPC_S_CALL_FAILED;
  25. case WBEM_E_FAILED:
  26. default:
  27. return ERROR_WMI_TRY_AGAIN;
  28. }
  29. } else {
  30. return ERROR_SUCCESS;
  31. }
  32. }
  33. DWORD
  34. UnMarshallWMIPolicyObject(
  35. IWbemClassObject *pWbemClassObject,
  36. PWIRELESS_POLICY_OBJECT * ppWirelessPolicyObject
  37. )
  38. {
  39. PWIRELESS_POLICY_OBJECT pWirelessPolicyObject = NULL;
  40. HKEY hRegKey = NULL;
  41. DWORD dwType = 0;
  42. DWORD dwSize = 0;
  43. DWORD dwWirelessDataType = 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 pszString = NULL;
  51. LPWSTR pszRelativeName = NULL;
  52. DWORD dwRootPathLen = 0;
  53. ////start
  54. VARIANT var; //contains pszWirelessPolicyDN
  55. VariantInit(&var);
  56. hr = IWbemClassObject_Get(pWbemClassObject,
  57. L"id",
  58. 0,
  59. &var,
  60. 0,
  61. 0);
  62. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  63. pWirelessPolicyObject = (PWIRELESS_POLICY_OBJECT)AllocPolMem(sizeof(WIRELESS_POLICY_OBJECT));
  64. if (!pWirelessPolicyObject) {
  65. dwError = ERROR_OUTOFMEMORY;
  66. BAIL_ON_WIN32_ERROR(dwError);
  67. }
  68. pWirelessPolicyObject->pszWirelessOwnersReference = AllocPolStr((LPWSTR)var.bstrVal);
  69. VariantClear(&var);
  70. if (!pWirelessPolicyObject->pszWirelessOwnersReference) {
  71. dwError = ERROR_OUTOFMEMORY;
  72. BAIL_ON_WIN32_ERROR(dwError);
  73. }
  74. pWirelessPolicyObject->pRsopInfo = (PRSOP_INFO)AllocPolMem(sizeof(RSOP_INFO));
  75. if (!pWirelessPolicyObject->pRsopInfo) {
  76. dwError = ERROR_OUTOFMEMORY;
  77. BAIL_ON_WIN32_ERROR(dwError);
  78. }
  79. dwError = WMIstoreQueryValue(pWbemClassObject,
  80. L"creationtime",
  81. VT_BSTR,
  82. (LPBYTE *)&pWirelessPolicyObject->pRsopInfo->pszCreationtime,
  83. &dwSize);
  84. //BAIL_ON_WIN32_ERROR(dwError);
  85. dwError = WMIstoreQueryValue(pWbemClassObject,
  86. L"GPOID",
  87. VT_BSTR,
  88. (LPBYTE *)&pWirelessPolicyObject->pRsopInfo->pszGPOID,
  89. &dwSize);
  90. //BAIL_ON_WIN32_ERROR(dwError);
  91. dwError = WMIstoreQueryValue(pWbemClassObject,
  92. L"id",
  93. VT_BSTR,
  94. (LPBYTE *)&pWirelessPolicyObject->pRsopInfo->pszID,
  95. &dwSize);
  96. BAIL_ON_WIN32_ERROR(dwError);
  97. dwError = WMIstoreQueryValue(pWbemClassObject,
  98. L"name",
  99. VT_BSTR,
  100. (LPBYTE *)&pWirelessPolicyObject->pRsopInfo->pszName,
  101. &dwSize);
  102. BAIL_ON_WIN32_ERROR(dwError);
  103. dwError = WMIstoreQueryValue(pWbemClassObject,
  104. L"SOMID",
  105. VT_BSTR,
  106. (LPBYTE *)&pWirelessPolicyObject->pRsopInfo->pszSOMID,
  107. &dwSize);
  108. //BAIL_ON_WIN32_ERROR(dwError);
  109. hr = IWbemClassObject_Get(pWbemClassObject,
  110. L"precedence",
  111. 0,
  112. &var,
  113. 0,
  114. 0);
  115. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  116. pWirelessPolicyObject->pRsopInfo->uiPrecedence = var.lVal;
  117. dwError = WMIstoreQueryValue(pWbemClassObject,
  118. L"msieee80211Name",
  119. VT_BSTR,
  120. (LPBYTE *)&pWirelessPolicyObject->pszWirelessName,
  121. &dwSize);
  122. BAIL_ON_WIN32_ERROR(dwError);
  123. dwError = WMIstoreQueryValue(pWbemClassObject,
  124. L"description",
  125. VT_BSTR,
  126. (LPBYTE *)&pWirelessPolicyObject->pszDescription,
  127. &dwSize);
  128. // BAIL_ON_WIN32_ERROR(dwError);
  129. dwError = WMIstoreQueryValue(pWbemClassObject,
  130. L"msieee80211ID",
  131. VT_BSTR,
  132. (LPBYTE *)&pWirelessPolicyObject->pszWirelessID,
  133. &dwSize);
  134. BAIL_ON_WIN32_ERROR(dwError);
  135. hr = IWbemClassObject_Get(pWbemClassObject,
  136. L"msieee80211DataType",
  137. 0,
  138. &var,
  139. 0,
  140. 0);
  141. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  142. dwWirelessDataType = var.lVal;
  143. pWirelessPolicyObject->dwWirelessDataType = dwWirelessDataType;
  144. dwError = WMIstoreQueryValue(pWbemClassObject,
  145. L"msieee80211Data",
  146. VT_ARRAY|VT_UI1,
  147. &pWirelessPolicyObject->pWirelessData,
  148. &pWirelessPolicyObject->dwWirelessDataLen);
  149. BAIL_ON_WIN32_ERROR(dwError);
  150. hr = IWbemClassObject_Get(pWbemClassObject,
  151. L"whenChanged",
  152. 0,
  153. &var,
  154. 0,
  155. 0);
  156. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  157. dwWhenChanged = var.lVal;
  158. pWirelessPolicyObject->dwWhenChanged = dwWhenChanged;
  159. *ppWirelessPolicyObject = pWirelessPolicyObject;
  160. cleanup:
  161. return(dwError);
  162. error:
  163. if (pWirelessPolicyObject) {
  164. FreeWirelessPolicyObject(pWirelessPolicyObject);
  165. }
  166. *ppWirelessPolicyObject = NULL;
  167. goto cleanup;
  168. }
  169. DWORD
  170. WMIstoreQueryValue(
  171. IWbemClassObject *pWbemClassObject,
  172. LPWSTR pszValueName,
  173. DWORD dwType,
  174. LPBYTE *ppValueData,
  175. LPDWORD pdwSize
  176. )
  177. {
  178. DWORD dwSize = 0;
  179. LPWSTR pszValueData = NULL;
  180. DWORD dwError = 0;
  181. HRESULT hr = S_OK;
  182. LPBYTE pBuffer = NULL;
  183. LPWSTR pszBuf = NULL;
  184. SAFEARRAY *pSafeArray = NULL;
  185. VARIANT var;
  186. DWORD i = 0;
  187. DWORD dw = 0;
  188. LPWSTR pszTmp = NULL;
  189. LPWSTR pszString = NULL;
  190. LPWSTR pMem = NULL;
  191. LPWSTR *ppszTmp = NULL;
  192. long lUbound = 0;
  193. DWORD dwCount = 0;
  194. LPBYTE pdw = NULL;
  195. BSTR HUGEP *pbstrTmp = NULL;
  196. BYTE HUGEP *pbyteTmp = NULL;
  197. VariantInit(&var);
  198. if(!pWbemClassObject) {
  199. dwError = ERROR_INVALID_DATA;
  200. BAIL_ON_WIN32_ERROR(dwError);
  201. }
  202. hr = IWbemClassObject_Get(pWbemClassObject,
  203. pszValueName,
  204. 0,
  205. &var,
  206. 0,
  207. 0);
  208. if(hr == WBEM_E_NOT_FOUND) {
  209. dwError = ERROR_INVALID_DATA;
  210. BAIL_ON_WIN32_ERROR(dwError);
  211. }
  212. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  213. ////sanity check
  214. if(dwType != var.vt) {
  215. dwError = ERROR_INVALID_DATA;
  216. BAIL_ON_WIN32_ERROR(dwError);
  217. }
  218. switch(dwType) {
  219. case VT_BSTR:
  220. pszTmp = var.bstrVal;
  221. dwSize = wcslen(pszTmp)*sizeof(WCHAR);
  222. pBuffer = (LPBYTE)AllocPolStr(pszTmp);
  223. if (!pBuffer) {
  224. dwError = ERROR_OUTOFMEMORY;
  225. BAIL_ON_WIN32_ERROR(dwError);
  226. }
  227. break;
  228. case (VT_ARRAY|VT_UI1):
  229. pSafeArray = var.parray;
  230. if(!pSafeArray) {
  231. dwError = ERROR_INVALID_DATA;
  232. BAIL_ON_WIN32_ERROR(dwError);
  233. }
  234. hr = SafeArrayGetUBound(
  235. pSafeArray,
  236. 1,
  237. &lUbound
  238. );
  239. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  240. dwSize = lUbound+1;
  241. if (dwSize == 0) {
  242. dwError = ERROR_INVALID_DATA;
  243. BAIL_ON_WIN32_ERROR(dwError);
  244. }
  245. pBuffer = (LPBYTE)AllocPolMem(dwSize);
  246. if (!pBuffer) {
  247. dwError = ERROR_OUTOFMEMORY;
  248. BAIL_ON_WIN32_ERROR(dwError);
  249. }
  250. for(i = 0; i < dwSize; i++) {
  251. hr = SafeArrayGetElement(pSafeArray, (long *)&i, &pBuffer[i]);
  252. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  253. }
  254. break;
  255. case (VT_ARRAY|VT_BSTR):
  256. pSafeArray = var.parray;
  257. if(!pSafeArray) {
  258. dwError = ERROR_INVALID_DATA;
  259. BAIL_ON_WIN32_ERROR(dwError);
  260. }
  261. hr = SafeArrayGetUBound(
  262. pSafeArray,
  263. 1,
  264. &lUbound
  265. );
  266. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  267. dwCount = lUbound+1;
  268. if (dwCount == 0) {
  269. dwError = ERROR_INVALID_DATA;
  270. BAIL_ON_WIN32_ERROR(dwError);
  271. }
  272. ppszTmp = (LPWSTR *)AllocPolMem(
  273. sizeof(LPWSTR)*dwCount
  274. );
  275. if (!ppszTmp) {
  276. dwError = ERROR_OUTOFMEMORY;
  277. BAIL_ON_WIN32_ERROR(dwError);
  278. }
  279. hr = SafeArrayAccessData(
  280. pSafeArray,
  281. (void HUGEP**)&pbstrTmp
  282. );
  283. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  284. for(i = 0; i < dwCount; i++) {
  285. pszTmp = pbstrTmp[i];
  286. ppszTmp[i] = AllocPolStr(pszTmp);
  287. if (!ppszTmp[i]) {
  288. dwError = ERROR_OUTOFMEMORY;
  289. BAIL_ON_WIN32_ERROR(dwError);
  290. }
  291. }
  292. SafeArrayUnaccessData(pSafeArray);
  293. //ppszTmp => string array
  294. for(i = 0; i < dwCount; i++) {
  295. dwSize += wcslen(ppszTmp[i])+1;
  296. }
  297. dwSize++;
  298. pMem = (LPWSTR)AllocPolMem(sizeof(WCHAR)*dwSize);
  299. if (!pMem) {
  300. dwError = ERROR_OUTOFMEMORY;
  301. BAIL_ON_WIN32_ERROR(dwError);
  302. }
  303. //adjust dwSize to byte size
  304. dwSize *= sizeof(WCHAR);
  305. pszString = pMem;
  306. for(i = 0; i < dwCount; i++) {
  307. memcpy(pszString, ppszTmp[i], wcslen(ppszTmp[i])*sizeof(WCHAR));
  308. pszString += wcslen(pszString)+1;
  309. }
  310. pBuffer = (LPBYTE)pMem;
  311. break;
  312. default:
  313. dwError = ERROR_INVALID_PARAMETER;
  314. BAIL_ON_WIN32_ERROR(dwError);
  315. break;
  316. }
  317. switch(dwType) {
  318. case VT_BSTR:
  319. pszBuf = (LPWSTR)pBuffer;
  320. if (!pszBuf || !*pszBuf) {
  321. dwError = ERROR_INVALID_DATA;
  322. BAIL_ON_WIN32_ERROR(dwError);
  323. }
  324. break;
  325. default:
  326. break;
  327. }
  328. *ppValueData = pBuffer;
  329. *pdwSize = dwSize;
  330. VariantClear(&var);
  331. cleanup:
  332. if(ppszTmp) {
  333. FreePolMem(ppszTmp);
  334. }
  335. return(dwError);
  336. error:
  337. if (pBuffer) {
  338. FreePolMem(pBuffer);
  339. }
  340. *ppValueData = NULL;
  341. *pdwSize = 0;
  342. goto cleanup;
  343. }
  344. HRESULT
  345. ReadPolicyObjectFromDirectoryEx(
  346. LPWSTR pszMachineName,
  347. LPWSTR pszPolicyDN,
  348. BOOL bDeepRead,
  349. PWIRELESS_POLICY_OBJECT * ppWirelessPolicyObject
  350. )
  351. {
  352. DWORD dwError = 0;
  353. HLDAP hLdapBindHandle = NULL;
  354. LPWSTR pszDefaultDirectory = NULL;
  355. PWIRELESS_POLICY_OBJECT pWirelessPolicyObject = NULL;
  356. if (!pszMachineName || !*pszMachineName) {
  357. dwError = ComputeDefaultDirectory(
  358. &pszDefaultDirectory
  359. );
  360. BAIL_ON_WIN32_ERROR(dwError);
  361. dwError = OpenDirectoryServerHandle(
  362. pszDefaultDirectory,
  363. 389,
  364. &hLdapBindHandle
  365. );
  366. BAIL_ON_WIN32_ERROR(dwError);
  367. } else {
  368. dwError = OpenDirectoryServerHandle(
  369. pszMachineName,
  370. 389,
  371. &hLdapBindHandle
  372. );
  373. BAIL_ON_WIN32_ERROR(dwError);
  374. }
  375. dwError = ReadPolicyObjectFromDirectory(
  376. hLdapBindHandle,
  377. pszPolicyDN,
  378. &pWirelessPolicyObject
  379. );
  380. BAIL_ON_WIN32_ERROR(dwError);
  381. *ppWirelessPolicyObject = pWirelessPolicyObject;
  382. cleanup:
  383. if (pszDefaultDirectory) {
  384. FreePolStr(pszDefaultDirectory);
  385. }
  386. if (hLdapBindHandle) {
  387. CloseDirectoryServerHandle(hLdapBindHandle);
  388. }
  389. return (HRESULT_FROM_WIN32(dwError));
  390. error:
  391. *ppWirelessPolicyObject = NULL;
  392. goto cleanup;
  393. }
  394. HRESULT
  395. WritePolicyObjectDirectoryToWMI(
  396. IWbemServices *pWbemServices,
  397. PWIRELESS_POLICY_OBJECT pWirelessPolicyObject,
  398. PGPO_INFO pGPOInfo
  399. )
  400. {
  401. HRESULT hr = S_OK;
  402. PWIRELESS_POLICY_OBJECT pWirelessWMIPolicyObject = NULL;
  403. //
  404. // Create a copy of the directory policy in WMI terms
  405. //
  406. hr = CloneDirectoryPolicyObjectEx(
  407. pWirelessPolicyObject,
  408. &pWirelessWMIPolicyObject
  409. );
  410. BAIL_ON_HRESULT_ERROR(hr);
  411. //
  412. // Write the WMI policy
  413. //
  414. hr = PersistWMIObject(
  415. pWbemServices,
  416. pWirelessWMIPolicyObject,
  417. pGPOInfo
  418. );
  419. BAIL_ON_HRESULT_ERROR(hr);
  420. cleanup:
  421. if (pWirelessWMIPolicyObject) {
  422. FreeWirelessPolicyObject(
  423. pWirelessWMIPolicyObject
  424. );
  425. }
  426. return(hr);
  427. error:
  428. goto cleanup;
  429. }
  430. DWORD
  431. CreateIWbemServices(
  432. LPWSTR pszWirelessWMINamespace,
  433. IWbemServices **ppWbemServices
  434. )
  435. {
  436. DWORD dwError = 0;
  437. HRESULT hr = S_OK;
  438. IWbemLocator *pWbemLocator = NULL;
  439. LPWSTR pszWirelessWMIPath = NULL;
  440. BSTR bstrWirelessWMIPath = NULL;
  441. if(!pszWirelessWMINamespace || !*pszWirelessWMINamespace) {
  442. pszWirelessWMIPath = gpszWirelessWMINamespace;
  443. } else {
  444. pszWirelessWMIPath = pszWirelessWMINamespace;
  445. }
  446. hr = CoCreateInstance(
  447. &CLSID_WbemLocator,
  448. NULL,
  449. CLSCTX_INPROC_SERVER,
  450. &IID_IWbemLocator,
  451. &pWbemLocator
  452. );
  453. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  454. bstrWirelessWMIPath = SysAllocString(pszWirelessWMIPath);
  455. if(!bstrWirelessWMIPath) {
  456. dwError = ERROR_OUTOFMEMORY;
  457. BAIL_ON_WIN32_ERROR(dwError);
  458. }
  459. hr = IWbemLocator_ConnectServer(
  460. pWbemLocator,
  461. bstrWirelessWMIPath,
  462. NULL,
  463. NULL,
  464. NULL,
  465. 0,
  466. NULL,
  467. NULL,
  468. ppWbemServices
  469. );
  470. SysFreeString(bstrWirelessWMIPath);
  471. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  472. if(pWbemLocator)
  473. IWbemLocator_Release(pWbemLocator);
  474. error:
  475. return (dwError);
  476. }