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.

475 lines
10 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994 - 1997.
  5. //
  6. // File: rsoputil.cpp
  7. //
  8. // Contents: helper functions for working with the RSOP databases
  9. //
  10. // History: 10-18-1999 stevebl Created
  11. //
  12. //---------------------------------------------------------------------------
  13. #include "precomp.hxx"
  14. #include <wbemcli.h>
  15. #include "rsoputil.h"
  16. // Something in the build environment is causing this
  17. // to be #defined to a version before 0x0500 and
  18. // that causes the ConvertStringSecurityDescriptor... function
  19. // to be undefined.
  20. #undef _WIN32_WINNT
  21. #define _WIN32_WINNT 0x0500
  22. #include "sddl.h"
  23. //+--------------------------------------------------------------------------
  24. //
  25. // Function: SetParameter
  26. //
  27. // Synopsis: sets a paramter's value in a WMI parameter list
  28. //
  29. // Arguments: [pInst] - instance on which to set the value
  30. // [szParam] - the name of the parameter
  31. // [xData] - the data
  32. //
  33. // History: 10-08-1999 stevebl Created
  34. //
  35. // Notes: There may be several flavors of this procedure, one for
  36. // each data type.
  37. //
  38. //---------------------------------------------------------------------------
  39. HRESULT SetParameter(IWbemClassObject * pInst, TCHAR * szParam, TCHAR * szData)
  40. {
  41. VARIANT var;
  42. HRESULT hr = S_OK;
  43. var.vt = VT_BSTR;
  44. var.bstrVal = SysAllocString(szData);
  45. hr = pInst->Put(szParam, 0, &var, 0);
  46. SysFreeString(var.bstrVal);
  47. return hr;
  48. }
  49. //+--------------------------------------------------------------------------
  50. //
  51. // Function: GetParameter
  52. //
  53. // Synopsis: retrieves a parameter value from a WMI paramter list
  54. //
  55. // Arguments: [pInst] - instance to get the paramter value from
  56. // [szParam] - the name of the paramter
  57. // [xData] - [out] data
  58. //
  59. // History: 10-08-1999 stevebl Created
  60. //
  61. // Notes: There are several flavors of this procedure, one for each
  62. // data type.
  63. //
  64. //---------------------------------------------------------------------------
  65. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, TCHAR * &szData)
  66. {
  67. VARIANT var;
  68. HRESULT hr = S_OK;
  69. VariantInit(&var);
  70. if (szData)
  71. {
  72. delete szData;
  73. szData = NULL;
  74. }
  75. hr = pInst->Get(szParam, 0, &var, 0, 0);
  76. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  77. {
  78. if (var.bstrVal)
  79. {
  80. szData = (TCHAR *) OLEALLOC(sizeof(TCHAR) * (_tcslen(var.bstrVal) + 1));
  81. if (szData)
  82. {
  83. _tcscpy(szData, var.bstrVal);
  84. }
  85. else
  86. {
  87. hr = E_OUTOFMEMORY;
  88. }
  89. }
  90. }
  91. VariantClear(&var);
  92. return hr;
  93. }
  94. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, CString &szData)
  95. {
  96. VARIANT var;
  97. HRESULT hr = S_OK;
  98. VariantInit(&var);
  99. if (szData)
  100. {
  101. szData = "";
  102. }
  103. hr = pInst->Get(szParam, 0, &var, 0, 0);
  104. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  105. {
  106. if (var.bstrVal)
  107. {
  108. szData = var.bstrVal;
  109. }
  110. }
  111. VariantClear(&var);
  112. return hr;
  113. }
  114. HRESULT GetParameterBSTR(IWbemClassObject * pInst, TCHAR * szParam, BSTR &bstrData)
  115. {
  116. VARIANT var;
  117. HRESULT hr = S_OK;
  118. VariantInit(&var);
  119. if (bstrData)
  120. {
  121. SysFreeString(bstrData);
  122. }
  123. hr = pInst->Get(szParam, 0, &var, 0, 0);
  124. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  125. {
  126. bstrData = SysAllocStringLen(var.bstrVal, SysStringLen(var.bstrVal));
  127. if (NULL == bstrData)
  128. {
  129. hr = E_OUTOFMEMORY;
  130. }
  131. }
  132. VariantClear(&var);
  133. return hr;
  134. }
  135. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, BOOL &fData)
  136. {
  137. VARIANT var;
  138. HRESULT hr = S_OK;
  139. VariantInit(&var);
  140. hr = pInst->Get(szParam, 0, &var, 0, 0);
  141. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  142. {
  143. fData = var.bVal;
  144. }
  145. VariantClear(&var);
  146. return hr;
  147. }
  148. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, HRESULT &hrData)
  149. {
  150. VARIANT var;
  151. HRESULT hr = S_OK;
  152. VariantInit(&var);
  153. hr = pInst->Get(szParam, 0, &var, 0, 0);
  154. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  155. {
  156. hrData = (HRESULT) var.lVal;
  157. }
  158. VariantClear(&var);
  159. return hr;
  160. }
  161. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, ULONG &ulData)
  162. {
  163. VARIANT var;
  164. HRESULT hr = S_OK;
  165. VariantInit(&var);
  166. hr = pInst->Get(szParam, 0, &var, 0, 0);
  167. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  168. {
  169. ulData = var.ulVal;
  170. }
  171. VariantClear(&var);
  172. return hr;
  173. }
  174. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, GUID &guid)
  175. {
  176. TCHAR * sz = NULL;
  177. memset(&guid, 0, sizeof(GUID));
  178. HRESULT hr = GetParameter(pInst, szParam, sz);
  179. if (SUCCEEDED(hr))
  180. {
  181. hr = CLSIDFromString(sz, &guid);
  182. }
  183. if (sz)
  184. {
  185. OLESAFE_DELETE(sz);
  186. }
  187. return hr;
  188. }
  189. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, unsigned int &ui)
  190. {
  191. VARIANT var;
  192. HRESULT hr = S_OK;
  193. VariantInit(&var);
  194. ui = 0;
  195. hr = pInst->Get(szParam, 0, &var, 0, 0);
  196. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  197. {
  198. ui = (HRESULT) var.uiVal;
  199. }
  200. VariantClear(&var);
  201. return hr;
  202. }
  203. // array variation - gets an array of guids and a count
  204. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR *szParam, UINT &uiCount, GUID * &rgGuid)
  205. {
  206. VARIANT var;
  207. HRESULT hr = S_OK;
  208. VariantInit(&var);
  209. hr = pInst->Get(szParam, 0, &var, 0, 0);
  210. if (SUCCEEDED(hr) && var.vt == (VT_ARRAY | VT_BSTR))
  211. {
  212. // build the array
  213. SAFEARRAY * parray = var.parray;
  214. uiCount = parray->rgsabound[0].cElements;
  215. if (uiCount > 0)
  216. {
  217. rgGuid = (GUID *)OLEALLOC(sizeof(GUID) * uiCount);
  218. if (rgGuid)
  219. {
  220. BSTR * rgData = (BSTR *)parray->pvData;
  221. UINT ui = uiCount;
  222. while (ui--)
  223. {
  224. hr = CLSIDFromString(rgData[ui], &rgGuid[ui]);
  225. if (FAILED(hr))
  226. {
  227. return hr;
  228. }
  229. }
  230. }
  231. else
  232. {
  233. uiCount = 0;
  234. hr = E_OUTOFMEMORY;
  235. }
  236. }
  237. }
  238. VariantClear(&var);
  239. return hr;
  240. }
  241. // array variation - gets an array of strings and a count
  242. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR *szParam, UINT &uiCount, TCHAR ** &rgszData)
  243. {
  244. VARIANT var;
  245. HRESULT hr = S_OK;
  246. VariantInit(&var);
  247. hr = pInst->Get(szParam, 0, &var, 0, 0);
  248. if (SUCCEEDED(hr) && var.vt == (VT_ARRAY | VT_BSTR))
  249. {
  250. // build the array
  251. SAFEARRAY * parray = var.parray;
  252. uiCount = parray->rgsabound[0].cElements;
  253. if (uiCount > 0)
  254. {
  255. rgszData = (TCHAR **)OLEALLOC(sizeof(TCHAR *) * uiCount);
  256. if (rgszData)
  257. {
  258. BSTR * rgData = (BSTR *)parray->pvData;
  259. UINT ui = uiCount;
  260. while (ui--)
  261. {
  262. OLESAFE_COPYSTRING(rgszData[ui], rgData[ui]);
  263. }
  264. }
  265. else
  266. {
  267. uiCount = 0;
  268. hr = E_OUTOFMEMORY;
  269. }
  270. }
  271. }
  272. VariantClear(&var);
  273. return hr;
  274. }
  275. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, PSECURITY_DESCRIPTOR &psd)
  276. {
  277. VARIANT var;
  278. HRESULT hr = S_OK;
  279. if (psd)
  280. {
  281. LocalFree(psd);
  282. psd = NULL;
  283. }
  284. VariantInit(&var);
  285. hr = pInst->Get(szParam, 0, &var, 0, 0);
  286. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  287. {
  288. PSECURITY_DESCRIPTOR psdTemp;
  289. BOOL f = ConvertStringSecurityDescriptorToSecurityDescriptor(
  290. var.bstrVal,
  291. //(LPCTSTR)var.parray->pvData,
  292. SDDL_REVISION_1,
  293. &psd,
  294. NULL);
  295. if (!f)
  296. {
  297. // it failed
  298. // could call GetLastError here to figure out why
  299. // but at the moment I don't really care
  300. }
  301. if (!IsValidSecurityDescriptor(psd))
  302. {
  303. LocalFree(psd);
  304. psd = NULL;
  305. }
  306. }
  307. VariantClear(&var);
  308. return hr;
  309. }
  310. HRESULT GetGPOFriendlyName(IWbemServices *pIWbemServices,
  311. LPTSTR lpGPOID, BSTR pLanguage,
  312. LPTSTR *pGPOName)
  313. {
  314. BSTR pQuery = NULL, pName = NULL;
  315. LPTSTR lpQuery = NULL;
  316. IEnumWbemClassObject * pEnum = NULL;
  317. IWbemClassObject *pObjects[2];
  318. HRESULT hr;
  319. ULONG ulRet;
  320. VARIANT varGPOName;
  321. //
  322. // Set the default
  323. //
  324. *pGPOName = NULL;
  325. //
  326. // Build the query
  327. //
  328. lpQuery = (LPTSTR) LocalAlloc (LPTR, ((lstrlen(lpGPOID) + 50) * sizeof(TCHAR)));
  329. if (!lpQuery)
  330. {
  331. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  332. goto Exit;
  333. }
  334. wsprintf (lpQuery, TEXT("SELECT name, id FROM RSOP_GPO where id=\"%s\""), lpGPOID);
  335. pQuery = SysAllocString (lpQuery);
  336. if (!pQuery)
  337. {
  338. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  339. goto Exit;
  340. }
  341. //
  342. // Allocate BSTRs for the property names we want to retreive
  343. //
  344. pName = SysAllocString (TEXT("name"));
  345. if (!pName)
  346. {
  347. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  348. goto Exit;
  349. }
  350. //
  351. // Execute the query
  352. //
  353. hr = pIWbemServices->ExecQuery (pLanguage, pQuery,
  354. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  355. NULL, &pEnum);
  356. if (FAILED(hr))
  357. {
  358. goto Exit;
  359. }
  360. //
  361. // Loop through the results
  362. //
  363. hr = pEnum->Next(WBEM_INFINITE, 1, pObjects, &ulRet);
  364. if (FAILED(hr))
  365. {
  366. goto Exit;
  367. }
  368. //
  369. // Check for the "data not available case"
  370. //
  371. if (ulRet == 0)
  372. {
  373. hr = S_OK;
  374. goto Exit;
  375. }
  376. //
  377. // Get the name
  378. //
  379. hr = pObjects[0]->Get (pName, 0, &varGPOName, NULL, NULL);
  380. if (FAILED(hr))
  381. {
  382. goto Exit;
  383. }
  384. //
  385. // Save the name
  386. //
  387. *pGPOName = (LPTSTR) LocalAlloc (LPTR, (lstrlen(varGPOName.bstrVal) + 1) * sizeof(TCHAR));
  388. if (!(*pGPOName))
  389. {
  390. goto Exit;
  391. }
  392. lstrcpy (*pGPOName, varGPOName.bstrVal);
  393. VariantClear (&varGPOName);
  394. hr = S_OK;
  395. Exit:
  396. if (pEnum)
  397. {
  398. pEnum->Release();
  399. }
  400. if (pQuery)
  401. {
  402. SysFreeString (pQuery);
  403. }
  404. if (lpQuery)
  405. {
  406. LocalFree (lpQuery);
  407. }
  408. if (pName)
  409. {
  410. SysFreeString (pName);
  411. }
  412. return hr;
  413. }