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.

1201 lines
30 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 "main.h"
  14. #include "rsoputil.h"
  15. #pragma warning(4:4535) // set_se_translator used w/o /EHa from sdkinc\provexce.h
  16. #include "wbemtime.h"
  17. //+--------------------------------------------------------------------------
  18. //
  19. // Function: SetParameter
  20. //
  21. // Synopsis: sets a paramter's value in a WMI parameter list
  22. //
  23. // Arguments: [pInst] - instance on which to set the value
  24. // [szParam] - the name of the parameter
  25. // [xData] - the data
  26. //
  27. // History: 10-08-1999 stevebl Created
  28. //
  29. // Notes: There may be several flavors of this procedure, one for
  30. // each data type.
  31. //
  32. //---------------------------------------------------------------------------
  33. HRESULT SetParameter(IWbemClassObject * pInst, TCHAR * szParam, TCHAR * szData)
  34. {
  35. VARIANT var;
  36. HRESULT hr = S_OK;
  37. var.vt = VT_BSTR;
  38. var.bstrVal = SysAllocString(szData);
  39. hr = pInst->Put(szParam, 0, &var, 0);
  40. SysFreeString(var.bstrVal);
  41. return hr;
  42. }
  43. HRESULT SetParameter(IWbemClassObject * pInst, TCHAR * szParam, SAFEARRAY * psa)
  44. {
  45. VARIANT var;
  46. HRESULT hr = S_OK;
  47. var.vt = VT_BSTR | VT_ARRAY;
  48. var.parray = psa;
  49. hr = pInst->Put(szParam, 0, &var, 0);
  50. return hr;
  51. }
  52. HRESULT SetParameter(IWbemClassObject * pInst, TCHAR * szParam, UINT uiData)
  53. {
  54. VARIANT var;
  55. HRESULT hr = S_OK;
  56. var.vt = VT_I4;
  57. var.lVal = uiData;
  58. hr = pInst->Put(szParam, 0, &var, 0);
  59. return hr;
  60. }
  61. //+--------------------------------------------------------------------------
  62. //
  63. // Function: SetParameterToNull
  64. //
  65. // Synopsis: sets a paramter's value in a WMI parameter list to null
  66. //
  67. // Arguments: [pInst] - instance on which to set the value
  68. // [szParam] - the name of the parameter
  69. //
  70. // History: 03-01-2000 ericflo Created
  71. //
  72. // Notes:
  73. //
  74. //---------------------------------------------------------------------------
  75. HRESULT SetParameterToNull(IWbemClassObject * pInst, TCHAR * szParam)
  76. {
  77. VARIANT var;
  78. var.vt = VT_NULL;
  79. return (pInst->Put(szParam, 0, &var, 0));
  80. }
  81. //+--------------------------------------------------------------------------
  82. //
  83. // Function: GetParameter
  84. //
  85. // Synopsis: retrieves a parameter value from a WMI paramter list
  86. //
  87. // Arguments: [pInst] - instance to get the paramter value from
  88. // [szParam] - the name of the paramter
  89. // [xData] - [out] data
  90. //
  91. // History: 10-08-1999 stevebl Created
  92. //
  93. // Notes: There are several flavors of this procedure, one for each
  94. // data type.
  95. //
  96. //---------------------------------------------------------------------------
  97. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, TCHAR * &szData, BOOL bUseLocalAlloc /*=FALSE*/ )
  98. {
  99. VARIANT var;
  100. HRESULT hr = S_OK;
  101. if (!pInst)
  102. return E_INVALIDARG;
  103. VariantInit(&var);
  104. hr = pInst->Get(szParam, 0, &var, 0, 0);
  105. if (SUCCEEDED(hr))
  106. {
  107. if (var.vt == VT_BSTR)
  108. {
  109. if ( szData != NULL )
  110. {
  111. if ( bUseLocalAlloc )
  112. {
  113. LocalFree( szData );
  114. }
  115. else
  116. {
  117. delete [] szData;
  118. }
  119. szData = NULL;
  120. }
  121. ULONG ulNoChars;
  122. ulNoChars = _tcslen(var.bstrVal)+1;
  123. if ( bUseLocalAlloc )
  124. {
  125. szData = (TCHAR*)LocalAlloc( LPTR, sizeof(TCHAR) * ulNoChars );
  126. }
  127. else
  128. {
  129. szData = new TCHAR[ulNoChars];
  130. }
  131. if ( szData != NULL )
  132. {
  133. hr = StringCchCopy(szData, ulNoChars, var.bstrVal);
  134. }
  135. else
  136. {
  137. hr = E_OUTOFMEMORY;
  138. }
  139. }
  140. else
  141. {
  142. hr = E_UNEXPECTED;
  143. }
  144. }
  145. VariantClear(&var);
  146. return hr;
  147. }
  148. HRESULT GetParameterBSTR(IWbemClassObject * pInst, TCHAR * szParam, BSTR &bstrData)
  149. {
  150. VARIANT var;
  151. HRESULT hr = S_OK;
  152. if (!pInst)
  153. return E_INVALIDARG;
  154. VariantInit(&var);
  155. hr = pInst->Get(szParam, 0, &var, 0, 0);
  156. if (SUCCEEDED(hr))
  157. {
  158. if (var.vt == VT_BSTR)
  159. {
  160. if (bstrData)
  161. {
  162. SysFreeString(bstrData);
  163. }
  164. bstrData = SysAllocStringLen(var.bstrVal, SysStringLen(var.bstrVal));
  165. if (NULL == bstrData)
  166. {
  167. hr = E_OUTOFMEMORY;
  168. }
  169. }
  170. else
  171. {
  172. hr = E_UNEXPECTED;
  173. }
  174. }
  175. VariantClear(&var);
  176. return hr;
  177. }
  178. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, BOOL &fData)
  179. {
  180. VARIANT var;
  181. HRESULT hr = S_OK;
  182. if (!pInst)
  183. return E_INVALIDARG;
  184. VariantInit(&var);
  185. hr = pInst->Get(szParam, 0, &var, 0, 0);
  186. if (SUCCEEDED(hr))
  187. {
  188. if (var.vt == VT_BOOL)
  189. {
  190. fData = var.bVal;
  191. }
  192. else
  193. {
  194. hr = E_UNEXPECTED;
  195. }
  196. }
  197. VariantClear(&var);
  198. return hr;
  199. }
  200. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, LPWSTR *&szStringArray, DWORD &dwSize)
  201. {
  202. VARIANT var;
  203. HRESULT hr = S_OK;
  204. SAFEARRAY *pArray;
  205. long lSafeArrayUBound;
  206. long lSafeArrayLBound;
  207. long lSafeArrayLen;
  208. long lCount = 0;
  209. if (!pInst)
  210. return E_INVALIDARG;
  211. VariantInit(&var);
  212. hr = pInst->Get(szParam, 0, &var, 0, 0);
  213. if (SUCCEEDED(hr))
  214. {
  215. if (var.vt == (VT_ARRAY | VT_BSTR))
  216. {
  217. pArray = var.parray;
  218. hr = SafeArrayGetUBound(pArray, 1, &lSafeArrayUBound);
  219. if (FAILED(hr)) {
  220. return hr;
  221. }
  222. hr = SafeArrayGetLBound(pArray, 1, &lSafeArrayLBound);
  223. if (FAILED(hr)) {
  224. return hr;
  225. }
  226. lSafeArrayLen = 1+(lSafeArrayUBound-lSafeArrayLBound);
  227. szStringArray = (LPWSTR *)LocalAlloc(LPTR, sizeof(LPWSTR)*lSafeArrayLen);
  228. if (!szStringArray) {
  229. hr = E_OUTOFMEMORY;
  230. return hr;
  231. }
  232. for (lCount = 0; lCount < lSafeArrayLen; lCount++) {
  233. long lIndex = lSafeArrayLBound+lCount;
  234. BSTR bstrElement;
  235. hr = SafeArrayGetElement(pArray, &lIndex, &bstrElement);
  236. if (FAILED(hr)) {
  237. break;
  238. }
  239. ULONG ulNoChars = 1+lstrlen(bstrElement);
  240. szStringArray[lCount] = (LPWSTR)LocalAlloc(LPTR, sizeof(WCHAR)*ulNoChars);
  241. if (!szStringArray[lCount]) {
  242. hr = E_OUTOFMEMORY;
  243. SysFreeString(bstrElement);
  244. break;
  245. }
  246. hr = StringCchCopy(szStringArray[lCount], ulNoChars, bstrElement);
  247. ASSERT(SUCCEEDED(hr));
  248. SysFreeString(bstrElement);
  249. }
  250. // if there was an error free the remaining elements
  251. if (FAILED(hr)) {
  252. for (;lCount > 0; lCount-- ) {
  253. LocalFree(szStringArray[lCount]);
  254. }
  255. szStringArray = NULL;
  256. }
  257. else {
  258. dwSize = lSafeArrayLen;
  259. hr = S_OK;
  260. }
  261. }
  262. else
  263. {
  264. hr = E_UNEXPECTED;
  265. }
  266. }
  267. VariantClear(&var);
  268. return hr;
  269. }
  270. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, HRESULT &hrData)
  271. {
  272. VARIANT var;
  273. HRESULT hr = S_OK;
  274. if (!pInst)
  275. return E_INVALIDARG;
  276. VariantInit(&var);
  277. hr = pInst->Get(szParam, 0, &var, 0, 0);
  278. if (SUCCEEDED(hr))
  279. {
  280. if (var.vt == VT_I4)
  281. {
  282. hrData = (HRESULT) var.lVal;
  283. }
  284. else
  285. {
  286. hr = E_UNEXPECTED;
  287. }
  288. }
  289. VariantClear(&var);
  290. return hr;
  291. }
  292. HRESULT GetParameter(IWbemClassObject * pInst, TCHAR * szParam, ULONG &ulData)
  293. {
  294. VARIANT var;
  295. HRESULT hr = S_OK;
  296. if (!pInst)
  297. return E_INVALIDARG;
  298. VariantInit(&var);
  299. hr = pInst->Get(szParam, 0, &var, 0, 0);
  300. if (SUCCEEDED(hr))
  301. {
  302. if ((var.vt == VT_UI4) || (var.vt == VT_I4))
  303. {
  304. ulData = var.ulVal;
  305. }
  306. else
  307. {
  308. hr = E_UNEXPECTED;
  309. }
  310. }
  311. VariantClear(&var);
  312. return hr;
  313. }
  314. HRESULT GetParameterBytes(IWbemClassObject * pInst, TCHAR * szParam, LPBYTE * lpData, DWORD *dwDataSize)
  315. {
  316. VARIANT var;
  317. HRESULT hr = S_OK;
  318. SAFEARRAY * pSafeArray;
  319. DWORD dwSrcDataSize;
  320. LPBYTE lpSrcData;
  321. if (!pInst)
  322. return E_INVALIDARG;
  323. VariantInit(&var);
  324. hr = pInst->Get(szParam, 0, &var, 0, 0);
  325. if (SUCCEEDED(hr))
  326. {
  327. if (var.vt != VT_NULL)
  328. {
  329. pSafeArray = var.parray;
  330. dwSrcDataSize = pSafeArray->rgsabound[0].cElements;
  331. lpSrcData = (LPBYTE) pSafeArray->pvData;
  332. *lpData = (LPBYTE)LocalAlloc (LPTR, dwSrcDataSize);
  333. if (!(*lpData))
  334. {
  335. return HRESULT_FROM_WIN32(GetLastError());
  336. }
  337. CopyMemory (*lpData, lpSrcData, dwSrcDataSize);
  338. *dwDataSize = dwSrcDataSize;
  339. }
  340. else
  341. {
  342. hr = E_UNEXPECTED;
  343. }
  344. }
  345. VariantClear(&var);
  346. return hr;
  347. }
  348. HRESULT WINAPI ImportRSoPData (LPOLESTR lpNameSpace, LPOLESTR lpFileName)
  349. {
  350. IMofCompiler * pIMofCompiler;
  351. HRESULT hr;
  352. WBEM_COMPILE_STATUS_INFO info;
  353. //
  354. // Check args
  355. //
  356. if (!lpNameSpace)
  357. {
  358. DebugMsg((DM_WARNING, TEXT("LoadNameSpaceFromFile: Null namespace arg")));
  359. return E_INVALIDARG;
  360. }
  361. if (!lpFileName)
  362. {
  363. DebugMsg((DM_WARNING, TEXT("LoadNameSpaceFromFile: Null filename arg")));
  364. return E_INVALIDARG;
  365. }
  366. //
  367. // Create an instance of the mof compiler
  368. //
  369. hr = CoCreateInstance(CLSID_MofCompiler, NULL, CLSCTX_INPROC_SERVER,
  370. IID_IMofCompiler, (LPVOID *) &pIMofCompiler);
  371. if (FAILED(hr))
  372. {
  373. DebugMsg((DM_WARNING, TEXT("LoadNameSpaceFromFile: CoCreateInstance failed with 0x%x"), hr));
  374. return hr;
  375. }
  376. //
  377. // Compile the file
  378. //
  379. ZeroMemory (&info, sizeof(info));
  380. hr = pIMofCompiler->CompileFile (lpFileName, lpNameSpace, NULL, NULL, NULL,
  381. 0, 0, 0, &info);
  382. if (FAILED(hr))
  383. {
  384. DebugMsg((DM_WARNING, TEXT("LoadNameSpaceFromFile: CompileFile failed with 0x%x"), hr));
  385. }
  386. pIMofCompiler->Release();
  387. return hr;
  388. }
  389. BOOL WriteMofFile (HANDLE hFile, LPTSTR lpData)
  390. {
  391. DWORD dwBytesWritten, dwAnsiDataSize, dwByteCount, dwLFCount = 0;
  392. LPSTR lpAnsiData;
  393. LPTSTR lpTemp, lpRealData, lpDest;
  394. //
  395. // The lpData argument contains linefeed characters only. We need to convert
  396. // these to CR LF characters. Loop through the data to determine how many LFs
  397. // need to be converted.
  398. //
  399. lpTemp = lpData;
  400. while (*lpTemp)
  401. {
  402. if (*lpTemp == 0x0A)
  403. {
  404. dwLFCount++;
  405. }
  406. lpTemp++;
  407. }
  408. //
  409. // Allocate a new buffer to hold the string plus CR characters
  410. //
  411. lpRealData = (LPTSTR) LocalAlloc (LPTR, (lstrlen(lpData) + dwLFCount + 1) * sizeof(TCHAR));
  412. if (!lpRealData)
  413. {
  414. DebugMsg((DM_WARNING, TEXT("WriteMofFile: LocalAlloc failed with %d"), GetLastError()));
  415. return FALSE;
  416. }
  417. //
  418. // Copy the string replacing LF with CRLF as we find them
  419. //
  420. lpDest = lpRealData;
  421. lpTemp = lpData;
  422. while (*lpTemp)
  423. {
  424. if (*lpTemp == 0x0A)
  425. {
  426. *lpDest = 0x0D;
  427. lpDest++;
  428. }
  429. *lpDest = *lpTemp;
  430. lpDest++;
  431. lpTemp++;
  432. }
  433. //
  434. // Allocate a buffer to hold the ANSI data
  435. //
  436. dwAnsiDataSize = lstrlen (lpRealData) * 2;
  437. lpAnsiData = (LPSTR) LocalAlloc (LPTR, dwAnsiDataSize);
  438. if (!lpAnsiData)
  439. {
  440. DebugMsg((DM_WARNING, TEXT("WriteMofFile: LocalAlloc failed with %d"), GetLastError()));
  441. LocalFree (lpRealData);
  442. return FALSE;
  443. }
  444. //
  445. // Convert the buffer
  446. //
  447. dwByteCount = (DWORD) WideCharToMultiByte (CP_ACP, 0, lpRealData, lstrlen(lpRealData), lpAnsiData, dwAnsiDataSize, NULL, NULL);
  448. LocalFree (lpRealData);
  449. if (!dwByteCount)
  450. {
  451. DebugMsg((DM_WARNING, TEXT("WriteMofFile: WriteFile failed with %d"), GetLastError()));
  452. LocalFree (lpAnsiData);
  453. return FALSE;
  454. }
  455. //
  456. // Write the mof description to the file
  457. //
  458. if (!WriteFile (hFile, lpAnsiData, dwByteCount, &dwBytesWritten, NULL))
  459. {
  460. DebugMsg((DM_WARNING, TEXT("WriteMofFile: WriteFile failed with %d"), GetLastError()));
  461. LocalFree (lpAnsiData);
  462. return FALSE;
  463. }
  464. LocalFree (lpAnsiData);
  465. //
  466. // Make sure it was all written to the file
  467. //
  468. if (dwByteCount != dwBytesWritten)
  469. {
  470. DebugMsg((DM_WARNING, TEXT("WriteMofFile: Failed to write the correct amount of data.")));
  471. SetLastError(ERROR_INVALID_DATA);
  472. return FALSE;
  473. }
  474. return TRUE;
  475. }
  476. HRESULT EnumInstances (IWbemServices * pIWbemServices, BSTR pClassName, HANDLE hFile)
  477. {
  478. IWbemClassObject *pObjects[2], *pObject;
  479. IEnumWbemClassObject *pEnum = NULL;
  480. ULONG ulCount;
  481. HRESULT hr;
  482. BSTR bstrClass;
  483. DWORD dwError;
  484. //
  485. // Create the instance enumerator
  486. //
  487. hr = pIWbemServices->CreateInstanceEnum (pClassName, WBEM_FLAG_DEEP | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnum);
  488. if (FAILED(hr))
  489. {
  490. DebugMsg((DM_WARNING, TEXT("EnumInstances: CreateInstanceEnum failed with 0x%x"), hr));
  491. return hr;
  492. }
  493. //
  494. // Walk through the list
  495. //
  496. while (pEnum->Next(WBEM_INFINITE, 1, pObjects, &ulCount) == S_OK)
  497. {
  498. pObject = pObjects[0];
  499. //
  500. // Get the mof description of this class
  501. //
  502. hr = pObject->GetObjectText (0, &bstrClass);
  503. pObject->Release();
  504. if (FAILED(hr))
  505. {
  506. DebugMsg((DM_WARNING, TEXT("EnumInstances: GetObjectText failed with 0x%x"), hr));
  507. pEnum->Release();
  508. return hr;
  509. }
  510. //
  511. // Write the mof description to the file
  512. //
  513. if (!WriteMofFile (hFile, bstrClass))
  514. {
  515. dwError = GetLastError();
  516. DebugMsg((DM_WARNING, TEXT("EnumInstances: WriteMofFile failed with %d"), dwError));
  517. SysFreeString (bstrClass);
  518. pEnum->Release();
  519. return HRESULT_FROM_WIN32(dwError);
  520. }
  521. SysFreeString (bstrClass);
  522. }
  523. pEnum->Release();
  524. return hr;
  525. }
  526. HRESULT EnumNameSpace (IWbemServices * pIWbemServices, HANDLE hFile)
  527. {
  528. IWbemClassObject *pObjects[2], *pObject;
  529. IEnumWbemClassObject *pEnum = NULL;
  530. ULONG ulCount;
  531. HRESULT hr;
  532. VARIANT var;
  533. BSTR bstrClass;
  534. DWORD dwError;
  535. //
  536. // Create the class enumerator
  537. //
  538. hr = pIWbemServices->CreateClassEnum (NULL, WBEM_FLAG_DEEP | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnum);
  539. if (FAILED(hr))
  540. {
  541. DebugMsg((DM_WARNING, TEXT("EnumNameSpace: CreateClassEnum failed with 0x%x"), hr));
  542. return hr;
  543. }
  544. //
  545. // Walk through the list
  546. //
  547. while (pEnum->Next(WBEM_INFINITE, 1, pObjects, &ulCount) == S_OK)
  548. {
  549. pObject = pObjects[0];
  550. //
  551. // Get the class name
  552. //
  553. hr = pObject->Get (TEXT("__CLASS"), 0, &var, NULL, NULL);
  554. if (FAILED(hr))
  555. {
  556. DebugMsg((DM_WARNING, TEXT("EnumNameSpace: Failed to get class name with 0x%x"), hr));
  557. pEnum->Release();
  558. return hr;
  559. }
  560. //
  561. // Check if this is a system class. System classes start with "_"
  562. //
  563. if (var.bstrVal[0] != TEXT('_'))
  564. {
  565. //
  566. // Get the mof description of this class
  567. //
  568. hr = pObject->GetObjectText (0, &bstrClass);
  569. if (FAILED(hr))
  570. {
  571. DebugMsg((DM_WARNING, TEXT("EnumNameSpace: GetObjectText failed with 0x%x"), hr));
  572. VariantClear (&var);
  573. pEnum->Release();
  574. return hr;
  575. }
  576. //
  577. // Write the mof description to the file
  578. //
  579. if (!WriteMofFile (hFile, bstrClass))
  580. {
  581. dwError = GetLastError();
  582. DebugMsg((DM_WARNING, TEXT("EnumNameSpace: WriteMofFile failed with %d"), dwError));
  583. SysFreeString (bstrClass);
  584. VariantClear (&var);
  585. pEnum->Release();
  586. return HRESULT_FROM_WIN32(dwError);
  587. }
  588. SysFreeString (bstrClass);
  589. //
  590. // Now enumerate the instances of this class
  591. //
  592. hr = EnumInstances (pIWbemServices, var.bstrVal, hFile);
  593. if (FAILED(hr))
  594. {
  595. DebugMsg((DM_WARNING, TEXT("EnumNameSpace: EnumInstances failed with 0x%x"), hr));
  596. VariantClear (&var);
  597. pEnum->Release();
  598. return hr;
  599. }
  600. }
  601. VariantClear (&var);
  602. }
  603. pEnum->Release();
  604. return hr;
  605. }
  606. HRESULT WINAPI ExportRSoPData (LPTSTR lpNameSpace, LPTSTR lpFileName)
  607. {
  608. IWbemLocator *pIWbemLocator;
  609. IWbemServices *pIWbemServices;
  610. HANDLE hFile;
  611. HRESULT hr;
  612. DWORD dwError;
  613. //
  614. // Open the data file
  615. //
  616. hFile = CreateFile (lpFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  617. FILE_ATTRIBUTE_NORMAL, NULL);
  618. if (hFile == INVALID_HANDLE_VALUE)
  619. {
  620. dwError = GetLastError();
  621. DebugMsg((DM_WARNING, TEXT("ExportRSoPData: CreateFile for %s failed with %d"), lpFileName, dwError));
  622. return HRESULT_FROM_WIN32(dwError);
  623. }
  624. //
  625. // Create a locater instance
  626. //
  627. hr = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
  628. IID_IWbemLocator, (LPVOID *) &pIWbemLocator);
  629. if (FAILED(hr))
  630. {
  631. DebugMsg((DM_WARNING, TEXT("ExportRSoPData: CoCreateInstance failed with 0x%x"), hr));
  632. CloseHandle (hFile);
  633. return hr;
  634. }
  635. //
  636. // Connect to the server
  637. //
  638. BSTR bstrNameSpace = SysAllocString( lpNameSpace );
  639. if ( bstrNameSpace == NULL )
  640. {
  641. DebugMsg((DM_WARNING, TEXT("ExportRSoPData: Failed to allocate BSTR memory.")));
  642. pIWbemLocator->Release();
  643. CloseHandle(hFile);
  644. return E_OUTOFMEMORY;
  645. }
  646. hr = pIWbemLocator->ConnectServer(bstrNameSpace, NULL, NULL, 0, 0, NULL, NULL, &pIWbemServices);
  647. SysFreeString( bstrNameSpace );
  648. if (FAILED(hr))
  649. {
  650. DebugMsg((DM_WARNING, TEXT("ExportRSoPData: ConnectServer to %s failed with 0x%x"), lpNameSpace, hr));
  651. pIWbemLocator->Release();
  652. CloseHandle (hFile);
  653. return hr;
  654. }
  655. // Set the proper security to encrypt the data
  656. hr = CoSetProxyBlanket(pIWbemServices,
  657. RPC_C_AUTHN_DEFAULT,
  658. RPC_C_AUTHZ_DEFAULT,
  659. COLE_DEFAULT_PRINCIPAL,
  660. RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
  661. RPC_C_IMP_LEVEL_IMPERSONATE,
  662. NULL,
  663. 0);
  664. if (FAILED(hr))
  665. {
  666. DebugMsg((DM_WARNING, TEXT("ExportRSoPData: CoSetProxyBlanket failed with 0x%x"), hr));
  667. pIWbemServices->Release();
  668. pIWbemLocator->Release();
  669. CloseHandle (hFile);
  670. return hr;
  671. }
  672. //
  673. // Enumerate the classes and instances
  674. //
  675. hr = EnumNameSpace (pIWbemServices, hFile);
  676. if (FAILED(hr))
  677. {
  678. DebugMsg((DM_WARNING, TEXT("ExportRSoPData: EnumNameSpace failed with 0x%x"), hr));
  679. }
  680. CloseHandle (hFile);
  681. pIWbemServices->Release();
  682. pIWbemLocator->Release();
  683. return hr;
  684. }
  685. //******************************************************************************
  686. //
  687. // Function: WbemTimeToSystemTime
  688. //
  689. // Description:
  690. //
  691. // Parameters:
  692. //
  693. // Return:
  694. //
  695. // History: 08-16-2000 stevebl rewrote to use WBEMTime class
  696. //
  697. //******************************************************************************
  698. HRESULT WbemTimeToSystemTime(XBStr& xbstrWbemTime, SYSTEMTIME& sysTime)
  699. {
  700. HRESULT hr = E_FAIL;
  701. WBEMTime wt(xbstrWbemTime);
  702. if (wt.GetSYSTEMTIME(&sysTime))
  703. {
  704. hr = S_OK;
  705. }
  706. return hr;
  707. }
  708. //-------------------------------------------------------
  709. HRESULT ExtractWQLFilters (LPTSTR lpNameSpace, DWORD* pdwCount, LPTSTR** paszNames, LPTSTR** paszFilters,
  710. BOOL bReturnIfTrueOnly )
  711. {
  712. HRESULT hr;
  713. ULONG n;
  714. IWbemClassObject * pObjGPO = NULL;
  715. IEnumWbemClassObject * pEnum = NULL;
  716. BSTR strQueryLanguage = SysAllocString(TEXT("WQL"));
  717. BSTR strQuery = SysAllocString(TEXT("SELECT * FROM RSOP_GPO"));
  718. BSTR bstrFilterId = NULL;
  719. IWbemLocator * pLocator = NULL;
  720. IWbemServices * pNamespace = NULL;
  721. LPTSTR lpDisplayName;
  722. INT iIndex;
  723. LONG lCount=0, lCountAllocated = 0, l=0;
  724. LPTSTR* aszWQLFilterIds = NULL;
  725. // Get a locator instance
  726. hr = CoCreateInstance(CLSID_WbemLocator,
  727. 0,
  728. CLSCTX_INPROC_SERVER,
  729. IID_IWbemLocator,
  730. (LPVOID *)&pLocator);
  731. if (FAILED(hr))
  732. {
  733. DebugMsg((DM_WARNING, TEXT("CRSOPComponentData::ExtractWQLFilters: CoCreateInstance failed with 0x%x"), hr));
  734. goto cleanup;
  735. }
  736. // Connect to the namespace
  737. BSTR bstrNameSpace = SysAllocString( lpNameSpace );
  738. if ( bstrNameSpace == NULL )
  739. {
  740. DebugMsg((DM_WARNING, TEXT("ExportRSoPData: Failed to allocate BSTR memory.")));
  741. hr = E_OUTOFMEMORY;
  742. goto cleanup;
  743. }
  744. hr = pLocator->ConnectServer(bstrNameSpace,
  745. NULL,
  746. NULL,
  747. NULL,
  748. 0,
  749. NULL,
  750. NULL,
  751. &pNamespace);
  752. SysFreeString( bstrNameSpace );
  753. if (FAILED(hr))
  754. {
  755. DebugMsg((DM_WARNING, TEXT("CRSOPComponentData::ExtractWQLFilters: ConnectServer failed with 0x%x"), hr));
  756. goto cleanup;
  757. }
  758. // Set the proper security to encrypt the data
  759. hr = CoSetProxyBlanket(pNamespace,
  760. RPC_C_AUTHN_DEFAULT,
  761. RPC_C_AUTHZ_DEFAULT,
  762. COLE_DEFAULT_PRINCIPAL,
  763. RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
  764. RPC_C_IMP_LEVEL_IMPERSONATE,
  765. NULL,
  766. 0);
  767. if (FAILED(hr))
  768. {
  769. DebugMsg((DM_WARNING, TEXT("CRSOPComponentData::ExtractWQLFilters: CoSetProxyBlanket failed with 0x%x"), hr));
  770. goto cleanup;
  771. }
  772. // Query for the RSOP_GPO instances
  773. hr = pNamespace->ExecQuery(strQueryLanguage,
  774. strQuery,
  775. WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
  776. NULL,
  777. &pEnum);
  778. if (FAILED(hr))
  779. {
  780. DebugMsg((DM_WARNING, TEXT("CRSOPComponentData::ExtractWQLFilters: ExecQuery failed with 0x%x"), hr));
  781. goto cleanup;
  782. }
  783. // Allocate memory for 10 - should be enough for most cases
  784. aszWQLFilterIds = (LPTSTR*)LocalAlloc(LPTR, sizeof(LPTSTR)*(lCountAllocated+10));
  785. if ( aszWQLFilterIds == 0 )
  786. {
  787. hr = HRESULT_FROM_WIN32(GetLastError());
  788. DebugMsg((DM_WARNING, TEXT("CRSOPComponentData::ExtractWQLFilters: LocalAlloc failed with 0x%x"), hr));
  789. goto cleanup;
  790. }
  791. lCountAllocated += 10;
  792. // Loop through the results
  793. while (TRUE)
  794. {
  795. // Get one instance of RSOP_GPO
  796. hr = pEnum->Next(WBEM_INFINITE, 1, &pObjGPO, &n);
  797. if (FAILED(hr) || (n == 0))
  798. {
  799. hr = S_OK;
  800. break;
  801. }
  802. // Allocate more memory if necessary
  803. if ( lCount == lCountAllocated )
  804. {
  805. LPTSTR* aszNewWQLFilterIds = (LPTSTR*)LocalAlloc(LPTR, sizeof(LPTSTR)*(lCountAllocated+10));
  806. if ( aszNewWQLFilterIds == NULL )
  807. {
  808. hr = HRESULT_FROM_WIN32(GetLastError());
  809. DebugMsg((DM_WARNING, TEXT("CRSOPComponentData::ExtractWQLFilters: LocalAlloc failed with 0x%x"), hr));
  810. goto cleanup;
  811. }
  812. lCountAllocated += 10;
  813. for (l=0; l < lCount; l++)
  814. {
  815. aszNewWQLFilterIds[l] = aszWQLFilterIds[l];
  816. }
  817. LocalFree(aszWQLFilterIds);
  818. aszWQLFilterIds = aszNewWQLFilterIds;
  819. }
  820. // Get the filter id
  821. aszWQLFilterIds[lCount] = NULL;
  822. hr = GetParameter(pObjGPO, TEXT("filterId"), aszWQLFilterIds[lCount], TRUE);
  823. if (FAILED(hr))
  824. {
  825. goto LoopAgain;
  826. }
  827. // if only successful filters need to be returned, check gilterallowed attribute as well
  828. if (bReturnIfTrueOnly) {
  829. BOOL bFilterAllowed;
  830. hr = GetParameter(pObjGPO, TEXT("filterAllowed"), bFilterAllowed);
  831. // if it is not there assume it to be false
  832. if (FAILED(hr))
  833. {
  834. goto LoopAgain;
  835. }
  836. if (!bFilterAllowed) {
  837. goto LoopAgain;
  838. }
  839. DebugMsg((DM_VERBOSE, TEXT("CRSOPComponentData::ExtractWQLFilters: Filter <%s> evaluates to true on gpo"), aszWQLFilterIds[lCount]));
  840. }
  841. if (!aszWQLFilterIds[lCount] || !(*aszWQLFilterIds[lCount]) || (*aszWQLFilterIds[lCount] == TEXT(' ')))
  842. {
  843. if (aszWQLFilterIds[lCount])
  844. {
  845. LocalFree( aszWQLFilterIds[lCount] );
  846. }
  847. goto LoopAgain;
  848. }
  849. #ifdef DBG
  850. BSTR bstrGPOName;
  851. bstrGPOName = NULL;
  852. hr = GetParameterBSTR(pObjGPO, TEXT("name"), bstrGPOName);
  853. if ( (SUCCEEDED(hr)) && (bstrGPOName) )
  854. {
  855. DebugMsg((DM_VERBOSE, TEXT("CRSOPComponentData::ExtractWQLFilters: Found filter on GPO <%s>"), bstrGPOName));
  856. SysFreeString (bstrGPOName);
  857. bstrGPOName = NULL;
  858. }
  859. hr = S_OK;
  860. #endif
  861. // Eliminate duplicates
  862. for ( l=0; l < lCount; l++ )
  863. {
  864. if (lstrcmpi(aszWQLFilterIds[lCount], aszWQLFilterIds[l]) == 0)
  865. {
  866. break;
  867. }
  868. }
  869. if ((lCount != 0) && (l != lCount))
  870. {
  871. DebugMsg((DM_VERBOSE, TEXT("CRSOPComponentData::ExtractWQLFilters: filter = <%s> is a duplicate"), aszWQLFilterIds[lCount]));
  872. LocalFree(aszWQLFilterIds[lCount]);
  873. goto LoopAgain;
  874. }
  875. DebugMsg((DM_VERBOSE, TEXT("CRSOPComponentData::ExtractWQLFilters: filter = <%s>"), aszWQLFilterIds[lCount]));
  876. lCount++;
  877. LoopAgain:
  878. pObjGPO->Release();
  879. pObjGPO = NULL;
  880. }
  881. // Now allocate arrays.
  882. if ( lCount == 0 )
  883. {
  884. *pdwCount = 0;
  885. *paszNames = NULL;
  886. *paszFilters = NULL;
  887. goto cleanup;
  888. }
  889. *paszNames = (LPTSTR*)LocalAlloc( LPTR, sizeof(LPTSTR) * lCount );
  890. if ( *paszNames == NULL )
  891. {
  892. hr = E_FAIL;
  893. goto cleanup;
  894. }
  895. *paszFilters = (LPTSTR*)LocalAlloc( LPTR, sizeof(LPTSTR) * lCount );
  896. if ( *paszFilters == NULL )
  897. {
  898. LocalFree( *paszNames );
  899. *paszNames = NULL;
  900. hr = E_FAIL;
  901. goto cleanup;
  902. }
  903. *pdwCount = lCount;
  904. for ( l = 0; l < lCount; l++ )
  905. {
  906. (*paszFilters)[l] = aszWQLFilterIds[l];
  907. // Get the filter's friendly display name
  908. lpDisplayName = GetWMIFilterDisplayName (NULL, aszWQLFilterIds[l], FALSE, TRUE);
  909. if ( lpDisplayName == NULL )
  910. {
  911. ULONG ulNoChars = _tcslen(aszWQLFilterIds[l])+1;
  912. DebugMsg((DM_WARNING, TEXT("CRSOPComponentData::ExtractWQLFilters: Failed to get display name for filter id: %s"), aszWQLFilterIds[l]));
  913. (*paszNames)[l] = (LPTSTR)LocalAlloc( LPTR, sizeof(TCHAR) * ulNoChars );
  914. if ( (*paszNames)[l] != NULL )
  915. {
  916. hr = StringCchCopy( (*paszNames)[l], ulNoChars, aszWQLFilterIds[l] );
  917. ASSERT(SUCCEEDED(hr));
  918. }
  919. }
  920. else {
  921. ULONG ulNoChars = _tcslen(lpDisplayName)+1;
  922. (*paszNames)[l] = (LPTSTR)LocalAlloc( LPTR, sizeof(TCHAR) * ulNoChars );
  923. if ( (*paszNames)[l] != NULL )
  924. {
  925. hr = StringCchCopy( (*paszNames)[l], ulNoChars, lpDisplayName );
  926. ASSERT(SUCCEEDED(hr));
  927. }
  928. delete lpDisplayName;
  929. }
  930. }
  931. LocalFree( aszWQLFilterIds );
  932. aszWQLFilterIds = NULL;
  933. cleanup:
  934. if (bstrFilterId)
  935. {
  936. SysFreeString (bstrFilterId);
  937. }
  938. if (pObjGPO)
  939. {
  940. pObjGPO->Release();
  941. }
  942. if (pEnum)
  943. {
  944. pEnum->Release();
  945. }
  946. if (pNamespace)
  947. {
  948. pNamespace->Release();
  949. }
  950. if (pLocator)
  951. {
  952. pLocator->Release();
  953. }
  954. if (aszWQLFilterIds) {
  955. for (l=0; l < lCount; l++) {
  956. if (aszWQLFilterIds[l])
  957. LocalFree(aszWQLFilterIds[l]);
  958. }
  959. LocalFree(aszWQLFilterIds);
  960. }
  961. SysFreeString(strQueryLanguage);
  962. SysFreeString(strQuery);
  963. return hr;
  964. }
  965. //-------------------------------------------------------