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.

513 lines
12 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. ULONG ulNoChars;
  81. ulNoChars = _tcslen(var.bstrVal) + 1;
  82. szData = (TCHAR *) OLEALLOC(sizeof(TCHAR) * ulNoChars);
  83. if (szData)
  84. {
  85. hr = StringCchCopy(szData, ulNoChars, var.bstrVal);
  86. if (FAILED(hr))
  87. {
  88. OLESAFE_DELETE(szData);
  89. }
  90. }
  91. else
  92. {
  93. hr = E_OUTOFMEMORY;
  94. }
  95. }
  96. }
  97. VariantClear(&var);
  98. return hr;
  99. }
  100. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, CString &szData)
  101. {
  102. VARIANT var;
  103. HRESULT hr = S_OK;
  104. VariantInit(&var);
  105. if (szData)
  106. {
  107. szData = "";
  108. }
  109. hr = pInst->Get(szParam, 0, &var, 0, 0);
  110. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  111. {
  112. if (var.bstrVal)
  113. {
  114. szData = var.bstrVal;
  115. }
  116. }
  117. VariantClear(&var);
  118. return hr;
  119. }
  120. HRESULT GetParameterBSTR(IWbemClassObject * pInst, TCHAR * szParam, BSTR &bstrData)
  121. {
  122. VARIANT var;
  123. HRESULT hr = S_OK;
  124. VariantInit(&var);
  125. if (bstrData)
  126. {
  127. SysFreeString(bstrData);
  128. }
  129. hr = pInst->Get(szParam, 0, &var, 0, 0);
  130. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  131. {
  132. bstrData = SysAllocStringLen(var.bstrVal, SysStringLen(var.bstrVal));
  133. if (NULL == bstrData)
  134. {
  135. hr = E_OUTOFMEMORY;
  136. }
  137. }
  138. VariantClear(&var);
  139. return hr;
  140. }
  141. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, BOOL &fData)
  142. {
  143. VARIANT var;
  144. HRESULT hr = S_OK;
  145. VariantInit(&var);
  146. hr = pInst->Get(szParam, 0, &var, 0, 0);
  147. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  148. {
  149. fData = var.bVal;
  150. }
  151. VariantClear(&var);
  152. return hr;
  153. }
  154. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, HRESULT &hrData)
  155. {
  156. VARIANT var;
  157. HRESULT hr = S_OK;
  158. VariantInit(&var);
  159. hr = pInst->Get(szParam, 0, &var, 0, 0);
  160. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  161. {
  162. hrData = (HRESULT) var.lVal;
  163. }
  164. VariantClear(&var);
  165. return hr;
  166. }
  167. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, ULONG &ulData)
  168. {
  169. VARIANT var;
  170. HRESULT hr = S_OK;
  171. VariantInit(&var);
  172. hr = pInst->Get(szParam, 0, &var, 0, 0);
  173. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  174. {
  175. ulData = var.ulVal;
  176. }
  177. VariantClear(&var);
  178. return hr;
  179. }
  180. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, GUID &guid)
  181. {
  182. TCHAR * sz = NULL;
  183. memset(&guid, 0, sizeof(GUID));
  184. HRESULT hr = GetParameter(pInst, szParam, sz);
  185. if (SUCCEEDED(hr))
  186. {
  187. hr = CLSIDFromString(sz, &guid);
  188. }
  189. if (sz)
  190. {
  191. OLESAFE_DELETE(sz);
  192. }
  193. return hr;
  194. }
  195. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, unsigned int &ui)
  196. {
  197. VARIANT var;
  198. HRESULT hr = S_OK;
  199. VariantInit(&var);
  200. ui = 0;
  201. hr = pInst->Get(szParam, 0, &var, 0, 0);
  202. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  203. {
  204. ui = (HRESULT) var.uiVal;
  205. }
  206. VariantClear(&var);
  207. return hr;
  208. }
  209. // array variation - gets an array of guids and a count
  210. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR *szParam, UINT &uiCount, GUID * &rgGuid)
  211. {
  212. VARIANT var;
  213. HRESULT hr = S_OK;
  214. VariantInit(&var);
  215. hr = pInst->Get(szParam, 0, &var, 0, 0);
  216. if (SUCCEEDED(hr) && var.vt == (VT_ARRAY | VT_BSTR))
  217. {
  218. // build the array
  219. SAFEARRAY * parray = var.parray;
  220. uiCount = parray->rgsabound[0].cElements;
  221. if (uiCount > 0)
  222. {
  223. rgGuid = (GUID *)OLEALLOC(sizeof(GUID) * uiCount);
  224. if (rgGuid)
  225. {
  226. BSTR * rgData = (BSTR *)parray->pvData;
  227. UINT ui = uiCount;
  228. while (ui--)
  229. {
  230. hr = CLSIDFromString(rgData[ui], &rgGuid[ui]);
  231. if (FAILED(hr))
  232. {
  233. return hr;
  234. }
  235. }
  236. }
  237. else
  238. {
  239. uiCount = 0;
  240. hr = E_OUTOFMEMORY;
  241. }
  242. }
  243. }
  244. VariantClear(&var);
  245. return hr;
  246. }
  247. // array variation - gets an array of strings and a count
  248. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR *szParam, UINT &uiCount, TCHAR ** &rgszData)
  249. {
  250. VARIANT var;
  251. HRESULT hr = S_OK;
  252. VariantInit(&var);
  253. hr = pInst->Get(szParam, 0, &var, 0, 0);
  254. if (SUCCEEDED(hr) && var.vt == (VT_ARRAY | VT_BSTR))
  255. {
  256. // build the array
  257. SAFEARRAY * parray = var.parray;
  258. uiCount = parray->rgsabound[0].cElements;
  259. if (uiCount > 0)
  260. {
  261. rgszData = (TCHAR **)OLEALLOC(sizeof(TCHAR *) * uiCount);
  262. if (rgszData)
  263. {
  264. BSTR * rgData = (BSTR *)parray->pvData;
  265. UINT ui = uiCount;
  266. while (ui--)
  267. {
  268. //
  269. // Allocate memory and copy the string
  270. //
  271. OLESAFE_COPYSTRING(rgszData[ui], rgData[ui]);
  272. if ( ! rgszData[ui] )
  273. {
  274. //
  275. // If we failed one allocation, free all the preceding
  276. // allocations
  277. //
  278. for (int iCurrent = ui + 1; iCurrent < uiCount; iCurrent++ )
  279. {
  280. OLESAFE_DELETE( rgszData[iCurrent] );
  281. }
  282. OLESAFE_DELETE( rgszData );
  283. uiCount = 0;
  284. hr = E_OUTOFMEMORY;
  285. break;
  286. }
  287. }
  288. }
  289. else
  290. {
  291. uiCount = 0;
  292. hr = E_OUTOFMEMORY;
  293. }
  294. }
  295. }
  296. VariantClear(&var);
  297. return hr;
  298. }
  299. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, PSECURITY_DESCRIPTOR &psd)
  300. {
  301. VARIANT var;
  302. HRESULT hr = S_OK;
  303. if (psd)
  304. {
  305. LocalFree(psd);
  306. psd = NULL;
  307. }
  308. VariantInit(&var);
  309. hr = pInst->Get(szParam, 0, &var, 0, 0);
  310. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  311. {
  312. PSECURITY_DESCRIPTOR psdTemp;
  313. BOOL f = ConvertStringSecurityDescriptorToSecurityDescriptor(
  314. var.bstrVal,
  315. //(LPCTSTR)var.parray->pvData,
  316. SDDL_REVISION_1,
  317. &psd,
  318. NULL);
  319. if (!f)
  320. {
  321. // it failed
  322. // could call GetLastError here to figure out why
  323. // but at the moment I don't really care
  324. }
  325. if (!IsValidSecurityDescriptor(psd))
  326. {
  327. LocalFree(psd);
  328. psd = NULL;
  329. }
  330. }
  331. VariantClear(&var);
  332. return hr;
  333. }
  334. HRESULT GetGPOFriendlyName(IWbemServices *pIWbemServices,
  335. LPTSTR lpGPOID, BSTR pLanguage,
  336. LPTSTR *pGPOName)
  337. {
  338. BSTR pQuery = NULL, pName = NULL;
  339. LPTSTR lpQuery = NULL;
  340. IEnumWbemClassObject * pEnum = NULL;
  341. IWbemClassObject *pObjects[2];
  342. HRESULT hr;
  343. ULONG ulRet;
  344. VARIANT varGPOName;
  345. //
  346. // Set the default
  347. //
  348. *pGPOName = NULL;
  349. //
  350. // Build the query
  351. //
  352. ULONG ulNoChars = lstrlen(lpGPOID) + 50;
  353. lpQuery = (LPTSTR) LocalAlloc (LPTR, ulNoChars * sizeof(TCHAR));
  354. if (!lpQuery)
  355. {
  356. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  357. goto Exit;
  358. }
  359. WCHAR szQuery[] = L"SELECT name, id FROM RSOP_GPO where id=\"%s\"";
  360. hr = StringCchPrintf (lpQuery, ulNoChars, szQuery, lpGPOID);
  361. if (FAILED(hr))
  362. {
  363. goto Exit;
  364. }
  365. pQuery = SysAllocString (lpQuery);
  366. if (!pQuery)
  367. {
  368. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  369. goto Exit;
  370. }
  371. //
  372. // Allocate BSTRs for the property names we want to retreive
  373. //
  374. pName = SysAllocString (TEXT("name"));
  375. if (!pName)
  376. {
  377. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  378. goto Exit;
  379. }
  380. //
  381. // Execute the query
  382. //
  383. hr = pIWbemServices->ExecQuery (pLanguage, pQuery,
  384. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  385. NULL, &pEnum);
  386. if (FAILED(hr))
  387. {
  388. goto Exit;
  389. }
  390. //
  391. // Loop through the results
  392. //
  393. hr = pEnum->Next(WBEM_INFINITE, 1, pObjects, &ulRet);
  394. if (FAILED(hr))
  395. {
  396. goto Exit;
  397. }
  398. //
  399. // Check for the "data not available case"
  400. //
  401. if (ulRet == 0)
  402. {
  403. hr = S_OK;
  404. goto Exit;
  405. }
  406. //
  407. // Get the name
  408. //
  409. hr = pObjects[0]->Get (pName, 0, &varGPOName, NULL, NULL);
  410. if (FAILED(hr))
  411. {
  412. goto Exit;
  413. }
  414. //
  415. // Save the name
  416. //
  417. ULONG ulNoCharsGPOName;
  418. ulNoCharsGPOName = lstrlen(varGPOName.bstrVal) + 1;
  419. *pGPOName = (LPTSTR) LocalAlloc (LPTR, ulNoCharsGPOName * sizeof(TCHAR));
  420. if (!(*pGPOName))
  421. {
  422. goto Exit;
  423. }
  424. hr = StringCchCopy (*pGPOName, ulNoCharsGPOName, varGPOName.bstrVal);
  425. ASSERT(SUCCEEDED(hr));
  426. VariantClear (&varGPOName);
  427. hr = S_OK;
  428. Exit:
  429. if (pEnum)
  430. {
  431. pEnum->Release();
  432. }
  433. if (pQuery)
  434. {
  435. SysFreeString (pQuery);
  436. }
  437. if (lpQuery)
  438. {
  439. LocalFree (lpQuery);
  440. }
  441. if (pName)
  442. {
  443. SysFreeString (pName);
  444. }
  445. return hr;
  446. }