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.

568 lines
14 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 "sddl.h"
  15. //+--------------------------------------------------------------------------
  16. //
  17. // Function: SetParameter
  18. //
  19. // Synopsis: sets a paramter's value in a WMI parameter list
  20. //
  21. // Arguments: [pInst] - instance on which to set the value
  22. // [szParam] - the name of the parameter
  23. // [xData] - the data
  24. //
  25. // History: 10-08-1999 stevebl Created
  26. //
  27. // Notes: There may be several flavors of this procedure, one for
  28. // each data type.
  29. //
  30. //---------------------------------------------------------------------------
  31. HRESULT SetParameter(IWbemClassObject * pInst, TCHAR * szParam, TCHAR * szData)
  32. {
  33. VARIANT var;
  34. HRESULT hr = S_OK;
  35. var.vt = VT_BSTR;
  36. var.bstrVal = SysAllocString(szData);
  37. hr = pInst->Put(szParam, 0, &var, 0);
  38. SysFreeString(var.bstrVal);
  39. return hr;
  40. }
  41. //+--------------------------------------------------------------------------
  42. //
  43. // Function: GetParameter
  44. //
  45. // Synopsis: retrieves a parameter value from a WMI paramter list
  46. //
  47. // Arguments: [pInst] - instance to get the paramter value from
  48. // [szParam] - the name of the paramter
  49. // [xData] - [out] data
  50. //
  51. // History: 10-08-1999 stevebl Created
  52. //
  53. // Notes: There are several flavors of this procedure, one for each
  54. // data type.
  55. //
  56. //---------------------------------------------------------------------------
  57. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, TCHAR * &szData)
  58. {
  59. VARIANT var;
  60. HRESULT hr = S_OK;
  61. VariantInit(&var);
  62. if (szData)
  63. {
  64. delete [] szData;
  65. szData = NULL;
  66. }
  67. hr = pInst->Get(szParam, 0, &var, 0, 0);
  68. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  69. {
  70. if (var.bstrVal)
  71. {
  72. ULONG ulNoChars = _tcslen(var.bstrVal) + 1;
  73. szData = (TCHAR *) OLEALLOC(sizeof(TCHAR) * ulNoChars);
  74. if (szData)
  75. {
  76. hr = StringCchCopy(szData, ulNoChars, var.bstrVal);
  77. ASSERT(SUCCEEDED(hr));
  78. }
  79. else
  80. {
  81. hr = E_OUTOFMEMORY;
  82. }
  83. }
  84. }
  85. VariantClear(&var);
  86. return hr;
  87. }
  88. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, CString &szData)
  89. {
  90. VARIANT var;
  91. HRESULT hr = S_OK;
  92. VariantInit(&var);
  93. if (szData)
  94. {
  95. szData = "";
  96. }
  97. hr = pInst->Get(szParam, 0, &var, 0, 0);
  98. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  99. {
  100. if (var.bstrVal)
  101. {
  102. szData = var.bstrVal;
  103. }
  104. }
  105. VariantClear(&var);
  106. return hr;
  107. }
  108. HRESULT GetParameterBSTR(IWbemClassObject * pInst, TCHAR * szParam, BSTR &bstrData)
  109. {
  110. VARIANT var;
  111. HRESULT hr = S_OK;
  112. VariantInit(&var);
  113. if (bstrData)
  114. {
  115. SysFreeString(bstrData);
  116. }
  117. hr = pInst->Get(szParam, 0, &var, 0, 0);
  118. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  119. {
  120. bstrData = SysAllocStringLen(var.bstrVal, SysStringLen(var.bstrVal));
  121. if (NULL == bstrData)
  122. {
  123. hr = E_OUTOFMEMORY;
  124. }
  125. }
  126. VariantClear(&var);
  127. return hr;
  128. }
  129. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, BOOL &fData)
  130. {
  131. VARIANT var;
  132. HRESULT hr = S_OK;
  133. VariantInit(&var);
  134. hr = pInst->Get(szParam, 0, &var, 0, 0);
  135. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  136. {
  137. fData = var.bVal;
  138. }
  139. VariantClear(&var);
  140. return hr;
  141. }
  142. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, HRESULT &hrData)
  143. {
  144. VARIANT var;
  145. HRESULT hr = S_OK;
  146. VariantInit(&var);
  147. hr = pInst->Get(szParam, 0, &var, 0, 0);
  148. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  149. {
  150. hrData = (HRESULT) var.lVal;
  151. }
  152. VariantClear(&var);
  153. return hr;
  154. }
  155. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, ULONG &ulData)
  156. {
  157. VARIANT var;
  158. HRESULT hr = S_OK;
  159. VariantInit(&var);
  160. hr = pInst->Get(szParam, 0, &var, 0, 0);
  161. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  162. {
  163. ulData = var.ulVal;
  164. }
  165. VariantClear(&var);
  166. return hr;
  167. }
  168. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, GUID &guid)
  169. {
  170. TCHAR * sz = NULL;
  171. memset(&guid, 0, sizeof(GUID));
  172. HRESULT hr = GetParameter(pInst, szParam, sz);
  173. if (SUCCEEDED(hr))
  174. {
  175. hr = CLSIDFromString(sz, &guid);
  176. }
  177. if (sz)
  178. {
  179. OLESAFE_DELETE(sz);
  180. }
  181. return hr;
  182. }
  183. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, unsigned int &ui)
  184. {
  185. VARIANT var;
  186. HRESULT hr = S_OK;
  187. VariantInit(&var);
  188. ui = 0;
  189. hr = pInst->Get(szParam, 0, &var, 0, 0);
  190. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  191. {
  192. ui = (HRESULT) var.uiVal;
  193. }
  194. VariantClear(&var);
  195. return hr;
  196. }
  197. // array variation - gets an array of guids and a count
  198. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR *szParam, UINT &uiCount, GUID * &rgGuid)
  199. {
  200. VARIANT var;
  201. HRESULT hr = S_OK;
  202. uiCount = 0;
  203. VariantInit(&var);
  204. hr = pInst->Get(szParam, 0, &var, 0, 0);
  205. if (SUCCEEDED(hr) && var.vt == (VT_ARRAY | VT_BSTR))
  206. {
  207. // build the array
  208. SAFEARRAY * parray = var.parray;
  209. uiCount = parray->rgsabound[0].cElements;
  210. if (uiCount > 0)
  211. {
  212. rgGuid = (GUID *)OLEALLOC(sizeof(GUID) * uiCount);
  213. if (rgGuid)
  214. {
  215. BSTR * rgData = (BSTR *)parray->pvData;
  216. UINT ui = uiCount;
  217. while (ui--)
  218. {
  219. hr = CLSIDFromString(rgData[ui], &rgGuid[ui]);
  220. if (FAILED(hr))
  221. {
  222. return hr;
  223. }
  224. }
  225. }
  226. else
  227. {
  228. uiCount = 0;
  229. hr = E_OUTOFMEMORY;
  230. }
  231. }
  232. }
  233. VariantClear(&var);
  234. return hr;
  235. }
  236. // array variation - gets an array of strings and a count
  237. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR *szParam, UINT &uiCount, TCHAR ** &rgszData)
  238. {
  239. VARIANT var;
  240. HRESULT hr = S_OK;
  241. uiCount = 0;
  242. VariantInit(&var);
  243. hr = pInst->Get(szParam, 0, &var, 0, 0);
  244. if (SUCCEEDED(hr) && var.vt == (VT_ARRAY | VT_BSTR))
  245. {
  246. // build the array
  247. SAFEARRAY * parray = var.parray;
  248. uiCount = parray->rgsabound[0].cElements;
  249. if (uiCount > 0)
  250. {
  251. rgszData = (TCHAR **)OLEALLOC(sizeof(TCHAR *) * uiCount);
  252. if (rgszData)
  253. {
  254. BSTR * rgData = (BSTR *)parray->pvData;
  255. UINT ui = uiCount;
  256. while (ui--)
  257. {
  258. OLESAFE_COPYSTRING(rgszData[ui], rgData[ui]);
  259. }
  260. }
  261. else
  262. {
  263. uiCount = 0;
  264. hr = E_OUTOFMEMORY;
  265. }
  266. }
  267. }
  268. VariantClear(&var);
  269. return hr;
  270. }
  271. // array variation - gets an array of CSPLATFORM objects and a count
  272. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, UINT &uiCount, CSPLATFORM * &rgData)
  273. {
  274. VARIANT var;
  275. HRESULT hr = S_OK;
  276. uiCount = 0;
  277. VariantInit(&var);
  278. hr = pInst->Get(szParam, 0, &var, 0, 0);
  279. if (SUCCEEDED(hr) && var.vt == (VT_ARRAY | VT_I4))
  280. {
  281. // build the array
  282. SAFEARRAY * parray = var.parray;
  283. uiCount = parray->rgsabound[0].cElements;
  284. if (uiCount > 0)
  285. {
  286. rgData = (CSPLATFORM *)OLEALLOC(sizeof(CSPLATFORM) * uiCount);
  287. if (rgData)
  288. {
  289. ULONG * rgulData = (ULONG *)parray->pvData;
  290. UINT ui = uiCount;
  291. while (ui--)
  292. {
  293. rgData[ui].dwPlatformId = VER_PLATFORM_WIN32_NT;
  294. rgData[ui].dwVersionHi = 5;
  295. rgData[ui].dwVersionLo = 0;
  296. rgData[ui].dwProcessorArch = rgulData[ui];
  297. }
  298. }
  299. else
  300. {
  301. uiCount = 0;
  302. hr = E_OUTOFMEMORY;
  303. }
  304. }
  305. }
  306. VariantClear(&var);
  307. return hr;
  308. }
  309. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, PSECURITY_DESCRIPTOR &psd)
  310. {
  311. VARIANT var;
  312. HRESULT hr = S_OK;
  313. if (psd)
  314. {
  315. LocalFree(psd);
  316. psd = NULL;
  317. }
  318. VariantInit(&var);
  319. hr = pInst->Get(szParam, 0, &var, 0, 0);
  320. if (SUCCEEDED(hr) && var.vt != VT_NULL)
  321. {
  322. psd = (PSECURITY_DESCRIPTOR) LocalAlloc( LPTR, var.parray->rgsabound[0].cElements * sizeof( BYTE ) );
  323. if ( psd )
  324. {
  325. memcpy( psd, var.parray->pvData, var.parray->rgsabound[0].cElements * sizeof( BYTE ) );
  326. if (!IsValidSecurityDescriptor(psd))
  327. {
  328. LocalFree(psd);
  329. psd = NULL;
  330. }
  331. }
  332. else
  333. {
  334. hr = E_OUTOFMEMORY;
  335. }
  336. }
  337. VariantClear(&var);
  338. return hr;
  339. }
  340. HRESULT GetGPOFriendlyName(IWbemServices *pIWbemServices,
  341. LPTSTR lpGPOID, BSTR pLanguage,
  342. LPTSTR *pGPOName)
  343. {
  344. BSTR pQuery = NULL, pName = NULL;
  345. LPTSTR lpQuery = NULL;
  346. IEnumWbemClassObject * pEnum = NULL;
  347. IWbemClassObject *pObjects[2];
  348. HRESULT hr;
  349. ULONG ulRet;
  350. VARIANT varGPOName;
  351. //
  352. // Set the default
  353. //
  354. *pGPOName = NULL;
  355. //
  356. // Build the query
  357. //
  358. ULONG ulQueryNoChars = lstrlen(lpGPOID) + 50;
  359. lpQuery = (LPTSTR) LocalAlloc (LPTR, ulQueryNoChars * sizeof(TCHAR));
  360. if (!lpQuery)
  361. {
  362. DebugMsg((DM_WARNING, TEXT("GetGPOFriendlyName: Failed to allocate memory for unicode query")));
  363. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  364. goto Exit;
  365. }
  366. WCHAR szQueryFormat[] = TEXT("SELECT name, id FROM RSOP_GPO where id=\"%s\"");
  367. hr = StringCchPrintf (lpQuery, ulQueryNoChars, szQueryFormat, lpGPOID);
  368. if (FAILED(hr))
  369. {
  370. DebugMsg((DM_WARNING, TEXT("GetGPOFriendlyName: StringCchPrintf returned error")));
  371. goto Exit;
  372. }
  373. pQuery = SysAllocString (lpQuery);
  374. if (!pQuery)
  375. {
  376. DebugMsg((DM_WARNING, TEXT("GetGPOFriendlyName: Failed to allocate memory for query")));
  377. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  378. goto Exit;
  379. }
  380. //
  381. // Allocate BSTRs for the property names we want to retreive
  382. //
  383. pName = SysAllocString (TEXT("name"));
  384. if (!pName)
  385. {
  386. DebugMsg((DM_WARNING, TEXT("GetGPOFriendlyName: Failed to allocate memory for name")));
  387. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  388. goto Exit;
  389. }
  390. //
  391. // Execute the query
  392. //
  393. hr = pIWbemServices->ExecQuery (pLanguage, pQuery,
  394. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  395. NULL, &pEnum);
  396. if (FAILED(hr))
  397. {
  398. DebugMsg((DM_WARNING, TEXT("GetGPOFriendlyName: Failed to query for %s with 0x%x"),
  399. pQuery, hr));
  400. goto Exit;
  401. }
  402. //
  403. // Loop through the results
  404. //
  405. hr = pEnum->Next(WBEM_INFINITE, 1, pObjects, &ulRet);
  406. if (FAILED(hr))
  407. {
  408. DebugMsg((DM_WARNING, TEXT("GetGPOFriendlyName: Failed to get first item in query results for %s with 0x%x"),
  409. pQuery, hr));
  410. goto Exit;
  411. }
  412. //
  413. // Check for the "data not available case"
  414. //
  415. if (ulRet == 0)
  416. {
  417. ULONG ulNoChars = lstrlen(lpGPOID) + 1;
  418. //
  419. // In this case, we cannot find the gpo -- it has most likely been deleted. To give the user some
  420. // useful information, we will fall back to the guid.
  421. //
  422. *pGPOName = (LPTSTR) LocalAlloc (LPTR, ulNoChars * sizeof(TCHAR));
  423. if ( *pGPOName )
  424. {
  425. DebugMsg((DM_WARNING, TEXT("GetGPOFriendlyName: Using GPO guid for friendly name because GPO can't be found")));
  426. hr = StringCchCopy( *pGPOName, ulNoChars, lpGPOID );
  427. ASSERT(SUCCEEDED(hr));
  428. }
  429. else
  430. {
  431. DebugMsg((DM_WARNING, TEXT("GetGPOFriendlyName: Failed to allocate memory for GPO Name in GUID form")));
  432. hr = E_OUTOFMEMORY;
  433. }
  434. goto Exit;
  435. }
  436. //
  437. // Get the name
  438. //
  439. hr = pObjects[0]->Get (pName, 0, &varGPOName, NULL, NULL);
  440. if (FAILED(hr))
  441. {
  442. DebugMsg((DM_WARNING, TEXT("GetGPOFriendlyName: Failed to get gponame in query results for %s with 0x%x"),
  443. pQuery, hr));
  444. goto Exit;
  445. }
  446. //
  447. // Save the name
  448. //
  449. ULONG ulNoChars = lstrlen(varGPOName.bstrVal) + 1;
  450. *pGPOName = (LPTSTR) LocalAlloc (LPTR, ulNoChars * sizeof(TCHAR));
  451. if (!(*pGPOName))
  452. {
  453. DebugMsg((DM_WARNING, TEXT("GetGPOFriendlyName: Failed to allocate memory for GPO Name")));
  454. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  455. goto Exit;
  456. }
  457. hr = StringCchCopy (*pGPOName, ulNoChars, varGPOName.bstrVal);
  458. ASSERT(SUCCEEDED(hr));
  459. VariantClear (&varGPOName);
  460. hr = S_OK;
  461. Exit:
  462. if (pEnum)
  463. {
  464. pEnum->Release();
  465. }
  466. if (pQuery)
  467. {
  468. SysFreeString (pQuery);
  469. }
  470. if (lpQuery)
  471. {
  472. LocalFree (lpQuery);
  473. }
  474. if (pName)
  475. {
  476. SysFreeString (pName);
  477. }
  478. return hr;
  479. }
  480. HRESULT CStringFromWBEMTime(CString &szOut, BSTR bstrIn, BOOL fShortFormat)
  481. {
  482. HRESULT hr = E_FAIL;
  483. WBEMTime wt(bstrIn);
  484. FILETIME ft;
  485. if (wt.GetFILETIME(&ft))
  486. {
  487. CTime t(ft);
  488. if (fShortFormat)
  489. {
  490. szOut = t.Format(TEXT("%x"));
  491. }
  492. else
  493. {
  494. szOut = t.Format(TEXT("%#c"));
  495. }
  496. hr = S_OK;
  497. }
  498. return hr;
  499. }