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.

1884 lines
52 KiB

  1. //*************************************************************
  2. //
  3. // Resultant set of policy
  4. //
  5. // Microsoft Confidential
  6. // Copyright (c) Microsoft Corporation 1995
  7. // All rights reserved
  8. //
  9. // History: 7-Jun-99 SitaramR Created
  10. //
  11. //*************************************************************
  12. #include "uenv.h"
  13. #include "wbemcli.h"
  14. #include "reghash.h"
  15. #include "rsop.h"
  16. #include "logger.h"
  17. #include "RsopInc.h"
  18. #include "rsopsec.h"
  19. #include "locator.h"
  20. BOOL LogSessionData( LPGPOINFO lpGPOInfo, LPRSOPSESSIONDATA lprsopSessionData );
  21. BOOL LogSOMData( LPGPOINFO lpGPOInfo );
  22. BOOL LogGpoData( LPGPOINFO lpGPOInfo );
  23. BOOL LogGpLinkData( LPGPOINFO lpGPOInfo );
  24. /*
  25. BOOL LogGpLinkListData( LPGPOINFO lpGPOInfo );
  26. */
  27. BOOL DeleteInstances( WCHAR *pwszClass, IWbemServices *pWbemServices );
  28. BOOL ConnectToNameSpace(LPGPOINFO lpGPOInfo, WCHAR *pwszRootNameSpace,
  29. BOOL bPlanningMode, IWbemLocator *pWbemLocator,
  30. IWbemServices **ppWbemServices, BOOL *pbCreated);
  31. HRESULT
  32. CreateCSE_EventSourceAssoc( IWbemServices* pServices,
  33. LPWSTR szCSEGuid,
  34. LPWSTR szEventLogSources );
  35. HRESULT
  36. DeleteCSE_EventSourceAssoc( IWbemServices* pServices,
  37. LPWSTR szCSEGuid );
  38. //*************************************************************
  39. //
  40. // GetWbemServices()
  41. //
  42. // Purpose: Returns IWbemServices ptr to namespace
  43. //
  44. // Parameters: lpGPOInfo - Gpo info
  45. // pwszNameSpace - namespace
  46. // bPlanningMode - Is this called during planning mode ?
  47. //
  48. // Return: True if successful
  49. //
  50. //*************************************************************
  51. BOOL GetWbemServices( LPGPOINFO lpGPOInfo,
  52. WCHAR *pwszRootNameSpace,
  53. BOOL bPlanningMode,
  54. BOOL *bCreated,
  55. IWbemServices **ppWbemServices)
  56. {
  57. HRESULT hr;
  58. OLE32_API *pOle32Api = LoadOle32Api();
  59. if ( pOle32Api == NULL )
  60. return FALSE;
  61. IWbemLocator *pWbemLocator = NULL;
  62. hr = pOle32Api->pfnCoCreateInstance( CLSID_WbemLocator,
  63. NULL,
  64. CLSCTX_INPROC_SERVER,
  65. IID_IWbemLocator,
  66. (LPVOID *) &pWbemLocator );
  67. if ( FAILED(hr) ) {
  68. DebugMsg((DM_WARNING, TEXT("GetWbemServices: CoCreateInstance returned 0x%x"), hr));
  69. return FALSE;
  70. }
  71. DebugMsg((DM_VERBOSE, TEXT("GetWbemServices: CoCreateInstance succeeded")));
  72. XInterface<IWbemLocator> xLocator( pWbemLocator );
  73. //
  74. // get the appropriate name space and connect
  75. //
  76. if (!ConnectToNameSpace( lpGPOInfo, pwszRootNameSpace, bPlanningMode, pWbemLocator, ppWbemServices, bCreated)) {
  77. DebugMsg((DM_WARNING, TEXT("GetWbemServices: ConnectToNameSpace failed with 0x%x" ), GetLastError()));
  78. return FALSE;
  79. }
  80. return TRUE;
  81. }
  82. //*************************************************************
  83. //
  84. // ReleaseWbemServices()
  85. //
  86. // Purpose: Releases wbem service pointer
  87. //
  88. // Parameters: lpGPOInfo - Gpo info
  89. //
  90. //*************************************************************
  91. void ReleaseWbemServices( LPGPOINFO lpGPOInfo )
  92. {
  93. if ( lpGPOInfo->pWbemServices ) {
  94. lpGPOInfo->pWbemServices->Release();
  95. lpGPOInfo->pWbemServices = NULL;
  96. }
  97. }
  98. //*************************************************************
  99. //
  100. // LogRsopData()
  101. //
  102. // Purpose: Logs Rsop data to Cimom database
  103. //
  104. // Parameters: lpGPOInfo - Gpo Info
  105. //
  106. // Return: True if successful
  107. //
  108. //*************************************************************
  109. BOOL LogRsopData( LPGPOINFO lpGPOInfo, LPRSOPSESSIONDATA lprsopSessionData )
  110. {
  111. HRESULT hr;
  112. if ( lpGPOInfo->pWbemServices == NULL ) {
  113. DebugMsg((DM_WARNING, TEXT("LogRsopData: Null wbem services pointer, so cannot log Rsop data" )));
  114. return FALSE;
  115. }
  116. if ( !LogSessionData( lpGPOInfo, lprsopSessionData ) )
  117. return FALSE;
  118. if ( !LogSOMData( lpGPOInfo ) )
  119. return FALSE;
  120. if ( !LogGpoData( lpGPOInfo ) )
  121. return FALSE;
  122. if ( !LogGpLinkData( lpGPOInfo ) )
  123. return FALSE;
  124. DebugMsg((DM_VERBOSE, TEXT("LogRsopData: Successfully logged Rsop data" )));
  125. return TRUE;
  126. }
  127. //*************************************************************
  128. //
  129. // LogSessionData()
  130. //
  131. // Purpose: Logs scopes of management data
  132. //
  133. // Parameters: lpGPOInfo - Gpo Info
  134. //
  135. // Return: True if successful
  136. //
  137. //*************************************************************
  138. BOOL LogSessionData( LPGPOINFO lpGPOInfo, LPRSOPSESSIONDATA lprsopSessionData )
  139. {
  140. CSessionLogger sessionLogger( lpGPOInfo->pWbemServices );
  141. if ( !sessionLogger.Log(lprsopSessionData) )
  142. return FALSE;
  143. return TRUE;
  144. }
  145. //*************************************************************
  146. //
  147. // LogSOMData()
  148. //
  149. // Purpose: Logs scopes of management data
  150. //
  151. // Parameters: lpGPOInfo - Gpo Info
  152. //
  153. // Return: True if successful
  154. //
  155. //*************************************************************
  156. BOOL LogSOMData( LPGPOINFO lpGPOInfo )
  157. {
  158. IWbemServices *pWbemServices = lpGPOInfo->pWbemServices;
  159. if ( !(lpGPOInfo->dwFlags & GP_BACKGROUND_THREAD) ) {
  160. //
  161. // Clean up SOM data at foreground refresh only. Otherwise extensions that run in
  162. // foreground only may have policy data that have dangling references to SOM that
  163. // existed at foreground refresh time.
  164. //
  165. if ( !DeleteInstances( L"RSOP_SOM", pWbemServices ) )
  166. return FALSE;
  167. }
  168. DWORD dwOrder = 1;
  169. CSOMLogger somLogger( lpGPOInfo->dwFlags, pWbemServices );
  170. LPSCOPEOFMGMT pSOMList = lpGPOInfo->lpSOMList;
  171. while ( pSOMList ) {
  172. if ( !somLogger.Log( pSOMList, dwOrder, FALSE ) )
  173. return FALSE;
  174. dwOrder++;
  175. pSOMList = pSOMList->pNext;
  176. }
  177. pSOMList = lpGPOInfo->lpLoopbackSOMList;
  178. while ( pSOMList ) {
  179. if ( !somLogger.Log( pSOMList, dwOrder, TRUE ) )
  180. return FALSE;
  181. dwOrder++;
  182. pSOMList = pSOMList->pNext;
  183. }
  184. return TRUE;
  185. }
  186. //*************************************************************
  187. //
  188. // LogGpoData()
  189. //
  190. // Purpose: Logs GPO data
  191. //
  192. // Parameters: lpGPOInfo - Gpo Info
  193. //
  194. // Return: True if successful
  195. //
  196. //*************************************************************
  197. BOOL LogGpoData( LPGPOINFO lpGPOInfo )
  198. {
  199. IWbemServices *pWbemServices = lpGPOInfo->pWbemServices;
  200. if ( !(lpGPOInfo->dwFlags & GP_BACKGROUND_THREAD) ) {
  201. //
  202. // Clean up SOM data at foreground refresh only. Otherwise extensions that run in
  203. // foreground only may have policy data that have dangling references to SOM that
  204. // existed at foreground refresh time.
  205. //
  206. if ( !DeleteInstances( L"RSOP_GPO", pWbemServices ) )
  207. return FALSE;
  208. }
  209. CGpoLogger gpoLogger( lpGPOInfo->dwFlags, pWbemServices );
  210. GPCONTAINER *pGpContainer = lpGPOInfo->lpGpContainerList;
  211. while ( pGpContainer ) {
  212. if ( !gpoLogger.Log( pGpContainer ) )
  213. return FALSE;
  214. pGpContainer = pGpContainer->pNext;
  215. }
  216. pGpContainer = lpGPOInfo->lpLoopbackGpContainerList;
  217. while ( pGpContainer ) {
  218. if ( !gpoLogger.Log( pGpContainer ) )
  219. return FALSE;
  220. pGpContainer = pGpContainer->pNext;
  221. }
  222. return TRUE;
  223. }
  224. //*************************************************************
  225. //
  226. // FindGPO()
  227. //
  228. // Purpose: Finds order of GPO in SOM
  229. //
  230. // Parameters: pSOM - SOM
  231. // pGPO - GPO
  232. //
  233. // Return: Order #
  234. //
  235. //*************************************************************
  236. DWORD FindGPO( LPEXTFILTERLIST pGPOFilterList, LPSCOPEOFMGMT pSOM, GPLINK *pGpLink )
  237. {
  238. DWORD dwOrder = 1;
  239. WCHAR *pwszLinkGPOPath = StripLinkPrefix( pGpLink->pwszGPO );
  240. WCHAR *pwszLinkSOMPath = StripLinkPrefix( pSOM->pwszSOMId );
  241. //
  242. // If the SOM is blocked then the GPO is linked here
  243. // only if the GPO is forced
  244. //
  245. if ( pSOM->bBlocked && !pGpLink->bNoOverride )
  246. return 0;
  247. while ( pGPOFilterList )
  248. {
  249. WCHAR *pwszAppliedGPOPath = StripPrefix( pGPOFilterList->lpGPO->lpDSPath );
  250. WCHAR *pwszAppliedGPOSomPath = StripLinkPrefix( pGPOFilterList->lpGPO->lpLink );
  251. if ( !pGPOFilterList->bLogged &&
  252. (CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE, pwszLinkGPOPath, -1,
  253. pwszAppliedGPOPath, -1 ) == CSTR_EQUAL) &&
  254. (CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE, pwszLinkSOMPath, -1,
  255. pwszAppliedGPOSomPath, -1 ) == CSTR_EQUAL))
  256. {
  257. pGPOFilterList->bLogged = TRUE;
  258. return dwOrder;
  259. }
  260. pGPOFilterList = pGPOFilterList->pNext;
  261. dwOrder++;
  262. }
  263. return 0;
  264. }
  265. void
  266. ClearLoggedFlag( LPEXTFILTERLIST pGPOFilterList )
  267. {
  268. while ( pGPOFilterList )
  269. {
  270. pGPOFilterList->bLogged = FALSE;
  271. pGPOFilterList = pGPOFilterList->pNext;
  272. }
  273. }
  274. //*************************************************************
  275. //
  276. // LogGpLinkData()
  277. //
  278. // Purpose: Logs GPLINK data
  279. //
  280. // Parameters: lpGPOInfo - Gpo Info
  281. //
  282. // Return: True if successful
  283. //
  284. //*************************************************************
  285. BOOL LogGpLinkData( LPGPOINFO lpGPOInfo )
  286. {
  287. IWbemServices *pWbemServices = lpGPOInfo->pWbemServices;
  288. DWORD dwListOrder = 1;
  289. DWORD dwAppliedOrder = 1;
  290. if ( !DeleteInstances( L"RSOP_GPLink", pWbemServices ) )
  291. return FALSE;
  292. CGpLinkLogger gpLinkLogger( pWbemServices );
  293. // The GPO application order
  294. LPEXTFILTERLIST pFilterList = lpGPOInfo->lpExtFilterList;
  295. ClearLoggedFlag( pFilterList );
  296. // the function takes care of Null list.
  297. //
  298. // Normal case
  299. //
  300. SCOPEOFMGMT *pSOMList = lpGPOInfo->lpSOMList;
  301. while ( pSOMList ) {
  302. GPLINK *pGpLinkList = pSOMList->pGpLinkList;
  303. DWORD dwSomOrder = 1;
  304. while ( pGpLinkList ) {
  305. dwAppliedOrder = FindGPO( pFilterList, pSOMList, pGpLinkList );
  306. if ( !gpLinkLogger.Log( pSOMList->pwszSOMId, FALSE, pGpLinkList, dwSomOrder, dwListOrder, dwAppliedOrder ) )
  307. return FALSE;
  308. pGpLinkList = pGpLinkList->pNext;
  309. dwSomOrder++;
  310. dwListOrder++;
  311. }
  312. pSOMList = pSOMList->pNext;
  313. }
  314. //
  315. // Loopback case
  316. //
  317. pSOMList = lpGPOInfo->lpLoopbackSOMList;
  318. while ( pSOMList ) {
  319. GPLINK *pGpLinkList = pSOMList->pGpLinkList;
  320. DWORD dwSomOrder = 1;
  321. while ( pGpLinkList ) {
  322. //
  323. // If the SOM is blocked then the GPO is linked here
  324. // only if the GPO is forced
  325. //
  326. dwAppliedOrder = FindGPO( pFilterList, pSOMList, pGpLinkList );
  327. if ( !gpLinkLogger.Log( pSOMList->pwszSOMId, TRUE, pGpLinkList, dwSomOrder, dwListOrder, dwAppliedOrder ) )
  328. return FALSE;
  329. pGpLinkList = pGpLinkList->pNext;
  330. dwSomOrder++;
  331. dwListOrder++;
  332. }
  333. pSOMList = pSOMList->pNext;
  334. }
  335. return TRUE;
  336. }
  337. /*
  338. //*************************************************************
  339. //
  340. // LogGpLinkListData()
  341. //
  342. // Purpose: Logs GPLinkList data
  343. //
  344. // Parameters: lpGPOInfo - Gpo Info
  345. //
  346. // Return: True if successful
  347. //
  348. //*************************************************************
  349. BOOL LogGpLinkListData( LPGPOINFO lpGPOInfo )
  350. {
  351. BOOL bLoopback;
  352. IWbemServices *pWbemServices = lpGPOInfo->pWbemServices;
  353. if ( !DeleteInstances( L"RSOP_GPLinkList", pWbemServices ) )
  354. {
  355. return FALSE;
  356. }
  357. ClearLoggedFlag( lpGPOInfo->lpSOMList );
  358. ClearLoggedFlag( lpGPOInfo->lpLoopbackSOMList );
  359. // the function takes care of Null list.
  360. CGpLinkListLogger gpLinkListLogger( lpGPOInfo->dwFlags, pWbemServices );
  361. DWORD dwTypeOrder = 1;
  362. LPEXTFILTERLIST pFilterList = lpGPOInfo->lpExtFilterList;
  363. while ( pFilterList )
  364. {
  365. PGROUP_POLICY_OBJECT pGPO = pFilterList->lpGPO;
  366. DWORD dwSOMOrder = 0;
  367. BOOL bFound = FALSE;
  368. LPSCOPEOFMGMT pSOMList = lpGPOInfo->lpSOMList;
  369. bLoopback = FALSE;
  370. while ( pSOMList )
  371. {
  372. if ( CompareString( LOCALE_USER_DEFAULT,
  373. NORM_IGNORECASE,
  374. pSOMList->pwszSOMId,
  375. -1,
  376. StripLinkPrefix(pGPO->lpLink),
  377. -1 ) == CSTR_EQUAL )
  378. {
  379. dwSOMOrder = FindGPO( pSOMList, pGPO );
  380. if ( dwSOMOrder != 0 ) {
  381. bFound = TRUE;
  382. break;
  383. }
  384. }
  385. pSOMList = pSOMList->pNext;
  386. }
  387. if ( !bFound )
  388. {
  389. pSOMList = lpGPOInfo->lpLoopbackSOMList;
  390. bLoopback = TRUE;
  391. while ( pSOMList )
  392. {
  393. if ( CompareString( LOCALE_USER_DEFAULT,
  394. NORM_IGNORECASE,
  395. pSOMList->pwszSOMId,
  396. -1,
  397. StripLinkPrefix(pGPO->lpLink),
  398. -1 ) == CSTR_EQUAL )
  399. {
  400. dwSOMOrder = FindGPO( pSOMList, pGPO );
  401. if ( dwSOMOrder != 0 ) {
  402. bFound = TRUE;
  403. break;
  404. }
  405. }
  406. pSOMList = pSOMList->pNext;
  407. }
  408. }
  409. if ( !bFound )
  410. {
  411. ClearLoggedFlag( lpGPOInfo->lpSOMList );
  412. return FALSE;
  413. }
  414. if ( !gpLinkListLogger.Log( pGPO, dwSOMOrder, bLoopback, dwTypeOrder ) )
  415. {
  416. ClearLoggedFlag( lpGPOInfo->lpSOMList );
  417. ClearLoggedFlag( lpGPOInfo->lpLoopbackSOMList );
  418. return FALSE;
  419. }
  420. dwTypeOrder++;
  421. pFilterList = pFilterList->pNext;
  422. }
  423. ClearLoggedFlag( lpGPOInfo->lpSOMList );
  424. ClearLoggedFlag( lpGPOInfo->lpLoopbackSOMList );
  425. return TRUE;
  426. }
  427. */
  428. //*************************************************************
  429. //
  430. // DeleteInstaces()
  431. //
  432. // Purpose: Deletes all instances of a specified class
  433. //
  434. // Parameters: pwszClass - Class name
  435. // pWbemServices - Wbem services
  436. //
  437. // Return: True if successful
  438. //
  439. //*************************************************************
  440. BOOL DeleteInstances( WCHAR *pwszClass, IWbemServices *pWbemServices )
  441. {
  442. IEnumWbemClassObject *pEnum = NULL;
  443. XBStr xbstrClass( pwszClass );
  444. if ( !xbstrClass ) {
  445. DebugMsg((DM_WARNING, TEXT("DeleteInstances: Failed to allocate memory" )));
  446. return FALSE;
  447. }
  448. HRESULT hr = pWbemServices->CreateInstanceEnum( xbstrClass,
  449. WBEM_FLAG_SHALLOW,
  450. NULL,
  451. &pEnum );
  452. if ( FAILED(hr) ) {
  453. DebugMsg((DM_WARNING, TEXT("DeleteInstances: DeleteInstances failed with 0x%x" ), hr ));
  454. return FALSE;
  455. }
  456. XInterface<IEnumWbemClassObject> xEnum( pEnum );
  457. XBStr xbstrPath( L"__PATH" );
  458. if ( !xbstrPath ) {
  459. DebugMsg((DM_WARNING, TEXT("DeleteInstances: Failed to allocate memory" )));
  460. return FALSE;
  461. }
  462. IWbemClassObject *pInstance = NULL;
  463. ULONG ulReturned = 1;
  464. LONG TIMEOUT = -1;
  465. while ( ulReturned == 1 ) {
  466. hr = pEnum->Next( TIMEOUT,
  467. 1,
  468. &pInstance,
  469. &ulReturned );
  470. if ( hr == S_OK && ulReturned == 1 ) {
  471. XInterface<IWbemClassObject> xInstance( pInstance );
  472. VARIANT var;
  473. VariantInit( &var );
  474. hr = pInstance->Get( xbstrPath,
  475. 0L,
  476. &var,
  477. NULL,
  478. NULL );
  479. if ( FAILED(hr) ) {
  480. DebugMsg((DM_WARNING, TEXT("DeleteInstances: Get failed with 0x%x" ), hr ));
  481. return FALSE;
  482. }
  483. hr = pWbemServices->DeleteInstance( var.bstrVal,
  484. 0L,
  485. NULL,
  486. NULL );
  487. VariantClear( &var );
  488. if ( FAILED(hr) ) {
  489. DebugMsg((DM_WARNING, TEXT("DeleteInstances: DeleteInstance failed with 0x%x" ), hr ));
  490. return FALSE;
  491. }
  492. }
  493. }
  494. return TRUE;
  495. }
  496. //*************************************************************
  497. //
  498. // LogRegistryRsopData()
  499. //
  500. // Purpose: Logs registry Rsop data to Cimom database
  501. //
  502. // Parameters: dwFlags - Gpo Info flags
  503. // pHashTable - Hash table with registry policy data
  504. // pWbemServices - Namespace pointer for logging
  505. //
  506. // Return: True if successful
  507. //
  508. //*************************************************************
  509. BOOL LogRegistryRsopData( DWORD dwFlags, REGHASHTABLE *pHashTable, IWbemServices *pWbemServices )
  510. {
  511. HRESULT hr;
  512. DWORD i;
  513. if ( !DeleteInstances( L"RSOP_RegistryPolicySetting", pWbemServices ) )
  514. return FALSE;
  515. CRegistryLogger regLogger( dwFlags, pWbemServices );
  516. for ( i=0; i<HASH_TABLE_SIZE; i++ ) {
  517. REGKEYENTRY *pKeyEntry = pHashTable->aHashTable[i];
  518. while ( pKeyEntry ) {
  519. WCHAR *pwszKeyName = pKeyEntry->pwszKeyName;
  520. REGVALUEENTRY *pValueEntry = pKeyEntry->pValueList;
  521. while ( pValueEntry ) {
  522. DWORD dwOrder = 1;
  523. WCHAR *pwszValueName = pValueEntry->pwszValueName;
  524. REGDATAENTRY *pDataEntry = pValueEntry->pDataList;
  525. while ( pDataEntry ) {
  526. if ( !regLogger.Log( pwszKeyName,
  527. pwszValueName,
  528. pDataEntry,
  529. dwOrder ) )
  530. return FALSE;
  531. pDataEntry = pDataEntry->pNext;
  532. dwOrder++;
  533. }
  534. pValueEntry = pValueEntry->pNext;
  535. } // while pValueEntry
  536. pKeyEntry = pKeyEntry->pNext;
  537. } // while pKeyEntry
  538. } // for
  539. DebugMsg((DM_VERBOSE, TEXT("LogRegistry RsopData: Successfully logged registry Rsop data" )));
  540. return TRUE;
  541. }
  542. //*************************************************************
  543. //
  544. // LogAdmRsopData()
  545. //
  546. // Purpose: Logs Rsop ADM template data to Cimom database
  547. //
  548. // Parameters: pAdmFileCache - List of adm file to log
  549. // pWbemServices - Namespace pointer
  550. //
  551. // Return: True if successful
  552. //
  553. //*************************************************************
  554. BOOL LogAdmRsopData( ADMFILEINFO *pAdmFileCache, IWbemServices *pWbemServices )
  555. {
  556. if ( !DeleteInstances( L"RSOP_AdministrativeTemplateFile", pWbemServices ) )
  557. return FALSE;
  558. CAdmFileLogger admLogger( pWbemServices );
  559. while ( pAdmFileCache ) {
  560. if ( !admLogger.Log( pAdmFileCache ) )
  561. return FALSE;
  562. pAdmFileCache = pAdmFileCache->pNext;
  563. }
  564. DebugMsg((DM_VERBOSE, TEXT("LogAdmRsopData: Successfully logged Adm data" )));
  565. return TRUE;
  566. }
  567. //*************************************************************
  568. //
  569. // LogExtSessionStatus()
  570. //
  571. // Purpose: Logs ExtensionSessionStatus at the beginning of processing
  572. //
  573. // Parameters: pWbemServices - Namespace pointer
  574. // lpExt - Extension description
  575. // bSupported - Rsop Logging Supported
  576. //
  577. // Return: True if successful
  578. //
  579. //*************************************************************
  580. BOOL LogExtSessionStatus(IWbemServices *pWbemServices, LPGPEXT lpExt, BOOL bSupported, BOOL bLogEventSrc )
  581. {
  582. CExtSessionLogger extLogger( pWbemServices );
  583. if (!extLogger.Log(lpExt, bSupported))
  584. return FALSE;
  585. if ( !bLogEventSrc )
  586. {
  587. return TRUE;
  588. }
  589. HRESULT hr;
  590. if ( lpExt )
  591. {
  592. hr = DeleteCSE_EventSourceAssoc(pWbemServices,
  593. lpExt->lpKeyName );
  594. if ( FAILED( hr ) )
  595. {
  596. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: DeleteCSE_EventSourceAssoc failed with 0x%x" ), hr ));
  597. return FALSE;
  598. }
  599. if ( lpExt->szEventLogSources )
  600. {
  601. if ( lpExt->lpKeyName )
  602. {
  603. //
  604. // good CSE
  605. //
  606. hr = CreateCSE_EventSourceAssoc(pWbemServices,
  607. lpExt->lpKeyName,
  608. lpExt->szEventLogSources );
  609. if ( FAILED(hr) )
  610. {
  611. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: CreateCSEEventSourceNameAssoc failed with 0x%x" ), hr ));
  612. return FALSE;
  613. }
  614. }
  615. else
  616. {
  617. return FALSE;
  618. }
  619. }
  620. else
  621. {
  622. //
  623. // most likely the Registry CSE
  624. //
  625. if ( !lpExt->lpDllName || !lpExt->lpKeyName )
  626. {
  627. return FALSE;
  628. }
  629. else
  630. {
  631. WCHAR szEventLogSources[MAX_PATH];
  632. wcscpy( szEventLogSources, L"(" );
  633. wcscat( szEventLogSources, lpExt->lpDllName );
  634. LPWSTR szTemp = wcsrchr( szEventLogSources, L'.' );
  635. if ( szTemp )
  636. {
  637. *szTemp = 0;
  638. }
  639. // duble null terminate it
  640. wcscat( szEventLogSources, L",Application)");
  641. szEventLogSources[lstrlen(szEventLogSources)+1] = L'\0';
  642. hr = CreateCSE_EventSourceAssoc(pWbemServices,
  643. lpExt->lpKeyName,
  644. szEventLogSources );
  645. if ( FAILED(hr) )
  646. {
  647. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: CreateCSEEventSourceNameAssoc failed with 0x%x" ), hr ));
  648. return FALSE;
  649. }
  650. }
  651. }
  652. }
  653. else
  654. {
  655. hr = DeleteCSE_EventSourceAssoc(pWbemServices,
  656. GPCORE_GUID );
  657. if ( FAILED( hr ) )
  658. {
  659. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: DeleteCSE_EventSourceAssoc failed with 0x%x" ), hr ));
  660. return FALSE;
  661. }
  662. //
  663. // gp engine
  664. //
  665. WCHAR szEventLogSources[] = L"(userenv,Application)\0";
  666. hr = CreateCSE_EventSourceAssoc(pWbemServices,
  667. GPCORE_GUID,
  668. szEventLogSources );
  669. if ( FAILED(hr) )
  670. {
  671. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: CreateCSEEventSourceNameAssoc failed with 0x%x" ), hr ));
  672. return FALSE;
  673. }
  674. }
  675. DebugMsg((DM_VERBOSE, TEXT("LogExtSessionStatus: Successfully logged Extension Session data" )));
  676. return TRUE;
  677. }
  678. //*************************************************************
  679. //
  680. // UpdateExtSessionStatus()
  681. //
  682. // Purpose: Updates ExtensionSessionStatus at the end of processing
  683. //
  684. // Parameters: pWbemServices - Namespace pointer
  685. // lpKeyName - Extension Guid Can be NULL in which case it means GPEngine
  686. // bDirty - Logging was done successfully..
  687. // dwErr - Error in processing
  688. //
  689. // Return: True if successful
  690. //
  691. //*************************************************************
  692. BOOL UpdateExtSessionStatus(IWbemServices *pWbemServices, LPTSTR lpKeyName, BOOL bIncomplete, DWORD dwErr )
  693. {
  694. CExtSessionLogger extLogger( pWbemServices );
  695. if (!extLogger.Update(lpKeyName, bIncomplete, dwErr))
  696. return FALSE;
  697. return TRUE;
  698. }
  699. //*************************************************************
  700. //
  701. // RsopDeleteAllValues ()
  702. //
  703. // Purpose: Deletes all values under specified key
  704. //
  705. // Parameters: hKey - Key to delete values from
  706. //
  707. // Return:
  708. //
  709. // Comments: Same as util.c!DeleteAllValues except that it logs
  710. // Data into the rsop hash table
  711. //
  712. //*************************************************************
  713. BOOL RsopDeleteAllValues(HKEY hKey, REGHASHTABLE *pHashTable,
  714. WCHAR *lpKeyName, WCHAR *pwszGPO, WCHAR *pwszSOM, WCHAR *szCommand, BOOL *bLoggingOk)
  715. {
  716. TCHAR ValueName[MAX_PATH+1];
  717. DWORD dwSize = MAX_PATH+1;
  718. LONG lResult;
  719. BOOL bFirst=TRUE;
  720. while (RegEnumValue(hKey, 0, ValueName, &dwSize,
  721. NULL, NULL, NULL, NULL) == ERROR_SUCCESS) {
  722. lResult = RegDeleteValue(hKey, ValueName);
  723. if (lResult != ERROR_SUCCESS) {
  724. DebugMsg((DM_WARNING, TEXT("RsopDeleteAllValues: Failed to delete value <%s> with %d."), ValueName, lResult));
  725. return FALSE;
  726. } else {
  727. DebugMsg((DM_VERBOSE, TEXT("RsopDeleteAllValues: Deleted <%s>"), ValueName));
  728. }
  729. *bLoggingOk = AddRegHashEntry( pHashTable, REG_DELETEVALUE, lpKeyName,
  730. ValueName, 0, 0, NULL,
  731. pwszGPO, pwszSOM, szCommand, bFirst );
  732. bFirst = FALSE;
  733. dwSize = MAX_PATH+1;
  734. }
  735. return TRUE;
  736. }
  737. //*************************************************************
  738. //
  739. // SetRsopTargetName()
  740. //
  741. // Purpose: Allocates and returns the target name under which Rsop data will be logged.
  742. //
  743. // Parameters: lpGPOInfo - GPOInfo structure
  744. //
  745. // Return: TRUE if successful
  746. // FALSE otherwise
  747. //
  748. //*************************************************************
  749. BOOL SetRsopTargetName(LPGPOINFO lpGPOInfo)
  750. {
  751. XPtrLF<TCHAR> xszFullName;
  752. XPtrLF<TCHAR> xszTargetName; // return value
  753. HANDLE hOldToken;
  754. if ( lpGPOInfo->szName && lpGPOInfo->szTargetName )
  755. {
  756. return TRUE;
  757. }
  758. if ( lpGPOInfo->szName )
  759. {
  760. LocalFree( lpGPOInfo->szName );
  761. }
  762. if ( lpGPOInfo->szTargetName )
  763. {
  764. LocalFree( lpGPOInfo->szTargetName );
  765. }
  766. //
  767. // fill up the right target name
  768. //
  769. if ( lpGPOInfo->dwFlags & GP_MACHINE ) {
  770. if ( lpGPOInfo->dwFlags & GP_APPLY_DS_POLICY ) {
  771. xszFullName = MyGetComputerName (NameSamCompatible);
  772. }
  773. else {
  774. DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
  775. xszFullName = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR)*(MAX_COMPUTERNAME_LENGTH + 1));
  776. if (xszFullName) {
  777. if (!GetComputerName(xszFullName, &dwSize)) {
  778. xszFullName = NULL;
  779. }
  780. }
  781. }
  782. }
  783. else {
  784. if (!ImpersonateUser(lpGPOInfo->hToken, &hOldToken)) {
  785. DebugMsg((DM_WARNING, TEXT("RsopTargetName: Failed to impersonate user")));
  786. return FALSE;
  787. }
  788. xszFullName = MyGetUserName (NameSamCompatible);
  789. RevertToUser(&hOldToken);
  790. }
  791. if (!xszFullName) {
  792. DebugMsg((DM_WARNING, TEXT("RsopTargetName: Failed to get the %s name, error = %d"),
  793. (lpGPOInfo->dwFlags & GP_MACHINE ? TEXT("Computer"): TEXT("User")), GetLastError()));
  794. return FALSE;
  795. }
  796. xszTargetName = (LPTSTR) LocalAlloc(LPTR, sizeof(TCHAR)*(lstrlen(xszFullName)+1));
  797. if (!xszTargetName)
  798. return FALSE;
  799. //
  800. // Format the TargetName appropriately.
  801. //
  802. // We are just going to look for the first slash if present and treat the rest of
  803. // it as username.
  804. //
  805. LPTSTR lpTemp = xszFullName;
  806. while (*lpTemp && ((*lpTemp) != TEXT('\\')))
  807. lpTemp++;
  808. if ((*lpTemp) == TEXT('\\'))
  809. lstrcpy(xszTargetName, lpTemp+1);
  810. else
  811. lstrcpy(xszTargetName, xszFullName);
  812. //
  813. // To be consistent we will also remove the final $ in the machine name
  814. //
  815. lpTemp = xszTargetName;
  816. if ( lpGPOInfo->dwFlags & GP_MACHINE ) {
  817. if ((*lpTemp) && (lpTemp[lstrlen(lpTemp)-1] == TEXT('$')))
  818. lpTemp[lstrlen(lpTemp)-1] = TEXT('\0');
  819. }
  820. // let the structure own it
  821. lpGPOInfo->szTargetName = xszTargetName.Acquire();
  822. lpGPOInfo->szName = xszFullName.Acquire();
  823. return TRUE;
  824. }
  825. //*************************************************************
  826. //
  827. // ConnectToNameSpace()
  828. //
  829. // Purpose: Creates (if necessary) and connects to the appropriate name space
  830. //
  831. // Parameters: lpGPOInfo - GPOInfo structure
  832. // pwszRootNameSpace - Root name space
  833. // bPlanningMode - Is this planning mode
  834. // pWbemLocator - locator pointer
  835. // [out] ppWbemServices - pointer to WbemServices to a connected pointer
  836. // [out] pbCreated - Is the name space created. Optional Can be null
  837. //
  838. // Return: TRUE if successful
  839. // FALSE otherwise
  840. //
  841. //*************************************************************
  842. BOOL ConnectToNameSpace(LPGPOINFO lpGPOInfo, WCHAR *pwszRootNameSpace,
  843. BOOL bPlanningMode, IWbemLocator *pWbemLocator,
  844. IWbemServices **ppWbemServices, BOOL *pbCreated)
  845. {
  846. XPtrLF<WCHAR> xwszNameSpace = NULL;
  847. XInterface<IWbemServices> xWbemServices;
  848. LPTSTR lpEnd = NULL;
  849. XPtrLF<WCHAR> xszWmiNameFromUserSid;
  850. DWORD dwCurrentVersion;
  851. *ppWbemServices = NULL;
  852. if (pbCreated)
  853. *pbCreated = FALSE;
  854. if (!bPlanningMode) {
  855. //
  856. // Diagnostic mode
  857. //
  858. if (lpGPOInfo->dwFlags & GP_MACHINE) {
  859. xwszNameSpace = (LPTSTR)LocalAlloc(LPTR, (lstrlen(pwszRootNameSpace)+lstrlen(RSOP_NS_DIAG_MACHINE_OFFSET)+5)*sizeof(TCHAR));
  860. if (!xwszNameSpace)
  861. return FALSE;
  862. lstrcpy(xwszNameSpace, pwszRootNameSpace);
  863. lpEnd = CheckSlash(xwszNameSpace);
  864. lstrcpy(lpEnd, RSOP_NS_DIAG_MACHINE_OFFSET);
  865. }
  866. else {
  867. xszWmiNameFromUserSid = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR)*(lstrlen(lpGPOInfo->lpwszSidUser)+1));
  868. if (!xszWmiNameFromUserSid)
  869. {
  870. DebugMsg(( DM_WARNING, TEXT("ConnectToNameSpace::CreateNameSpace couldn't allocate memory with error %d"), GetLastError() ));
  871. return FALSE;
  872. }
  873. ConvertSidToWMIName(lpGPOInfo->lpwszSidUser, xszWmiNameFromUserSid);
  874. xwszNameSpace = (LPTSTR)LocalAlloc(LPTR, (lstrlen(pwszRootNameSpace)+lstrlen(RSOP_NS_DIAG_USER_OFFSET_FMT)+
  875. lstrlen(xszWmiNameFromUserSid)+5)*sizeof(TCHAR));
  876. if (!xwszNameSpace)
  877. return FALSE;
  878. lstrcpy(xwszNameSpace, pwszRootNameSpace);
  879. lpEnd = CheckSlash(xwszNameSpace);
  880. wsprintf(lpEnd, RSOP_NS_DIAG_USER_OFFSET_FMT, xszWmiNameFromUserSid);
  881. }
  882. }
  883. else {
  884. //
  885. // Planning Mode
  886. //
  887. if (lpGPOInfo->dwFlags & GP_MACHINE) {
  888. //
  889. // Machine
  890. //
  891. xwszNameSpace = (LPTSTR)LocalAlloc(LPTR, (lstrlen(pwszRootNameSpace)+lstrlen(RSOP_NS_PM_MACHINE_OFFSET)+5)*sizeof(TCHAR));
  892. if (!xwszNameSpace)
  893. return FALSE;
  894. lstrcpy(xwszNameSpace, pwszRootNameSpace);
  895. lpEnd = CheckSlash(xwszNameSpace);
  896. lstrcpy(lpEnd, RSOP_NS_PM_MACHINE_OFFSET);
  897. }
  898. else {
  899. //
  900. // User
  901. //
  902. xwszNameSpace = (LPTSTR)LocalAlloc(LPTR, (lstrlen(pwszRootNameSpace)+lstrlen(RSOP_NS_PM_USER_OFFSET)+5)*sizeof(TCHAR));
  903. if (!xwszNameSpace)
  904. return FALSE;
  905. lstrcpy(xwszNameSpace, pwszRootNameSpace);
  906. lpEnd = CheckSlash(xwszNameSpace);
  907. lstrcpy(lpEnd, RSOP_NS_PM_USER_OFFSET);
  908. }
  909. }
  910. XBStr xNameSpace( xwszNameSpace );
  911. HRESULT hr = pWbemLocator->ConnectServer( xNameSpace,
  912. NULL,
  913. NULL,
  914. 0L,
  915. 0L,
  916. NULL,
  917. NULL,
  918. &xWbemServices );
  919. DebugMsg((DM_VERBOSE, TEXT("ConnectToNameSpace: ConnectServer returned 0x%x"), hr));
  920. if (bPlanningMode) {
  921. if (FAILED(hr)) {
  922. DebugMsg((DM_WARNING, TEXT("ConnectToNameSpace: ConnectServer failed with 0x%x" ), hr ));
  923. }
  924. *ppWbemServices = xWbemServices.Acquire();
  925. return (SUCCEEDED(hr));
  926. }
  927. //
  928. // only diagnostic mode logging should reach here
  929. //
  930. if (FAILED(hr)) {
  931. DebugMsg((DM_VERBOSE, TEXT("ConnectToNameSpace: ConnectServer failed with 0x%x, trying to recreate the name space" ), hr ));
  932. if (lpGPOInfo->dwFlags & GP_MACHINE) {
  933. return FALSE;
  934. }
  935. }
  936. if (SUCCEEDED(hr)) {
  937. //
  938. // Now check whether there is an RSOP_Session instance under this namespace
  939. // to set the *pbCreated flag
  940. //
  941. hr = GetRsopSchemaVersionNumber(xWbemServices, &dwCurrentVersion);
  942. //
  943. // We don't have an Rsop schema version number
  944. //
  945. if (FAILED(hr)) {
  946. return FALSE;
  947. }
  948. if (dwCurrentVersion == 0) {
  949. DebugMsg((DM_VERBOSE, TEXT("ConnectToNameSpace: Rsop data has not been logged before or a major schema upg happened. relogging.." )));
  950. if (pbCreated)
  951. *pbCreated = TRUE;
  952. }
  953. if (lpGPOInfo->dwFlags & GP_MACHINE) {
  954. *ppWbemServices = xWbemServices.Acquire();
  955. return TRUE;
  956. }
  957. if (dwCurrentVersion != RSOP_MOF_SCHEMA_VERSION) {
  958. BOOL bAbort = FALSE;
  959. DebugMsg((DM_VERBOSE, TEXT("ConnectToNameSpace: Minor schema upg happened. copying classes. " )));
  960. hr = CopyNameSpace(RSOP_NS_USER, xNameSpace, FALSE, &bAbort, pWbemLocator );
  961. if ( FAILED(hr) )
  962. {
  963. DebugMsg((DM_WARNING, TEXT("ConnectToNameSpace: CopyNameSpace failed with 0x%x" ), hr ));
  964. return FALSE;
  965. }
  966. }
  967. *ppWbemServices = xWbemServices.Acquire();
  968. return TRUE;
  969. }
  970. // Only user mode in diagnostic mode should reach here
  971. // when it couldn't find the namespace
  972. //
  973. //
  974. XPtrLF<TCHAR> xRootNameSpace = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR)*(lstrlen(pwszRootNameSpace)+
  975. lstrlen(RSOP_NS_DIAG_ROOTUSER_OFFSET)+20));
  976. if (!xRootNameSpace) {
  977. // there is nothing more we can do
  978. DebugMsg((DM_WARNING, TEXT("ConnectToNameSpace: Failed to allocate memory.3")));
  979. return FALSE;
  980. }
  981. lstrcpy(xRootNameSpace, pwszRootNameSpace);
  982. lpEnd = CheckSlash(xRootNameSpace);
  983. lstrcpy(lpEnd, RSOP_NS_DIAG_ROOTUSER_OFFSET);
  984. //
  985. // The security descriptor
  986. //
  987. XPtrLF<SID> xSid = GetUserSid(lpGPOInfo->hToken);
  988. if (!xSid) {
  989. DebugMsg((DM_WARNING, TEXT("ConnectToNameSpace::GetUserSid failed with %d"), GetLastError()));
  990. return FALSE;
  991. }
  992. XPtrLF<SECURITY_DESCRIPTOR> xsd;
  993. SECURITY_ATTRIBUTES sa;
  994. CSecDesc Csd;
  995. Csd.AddLocalSystem(RSOP_ALL_PERMS, CONTAINER_INHERIT_ACE);
  996. Csd.AddAdministrators(RSOP_ALL_PERMS, CONTAINER_INHERIT_ACE);
  997. Csd.AddSid(xSid, RSOP_READ_PERMS, CONTAINER_INHERIT_ACE);
  998. Csd.AddAdministratorsAsOwner();
  999. Csd.AddAdministratorsAsGroup();
  1000. xsd = Csd.MakeSelfRelativeSD();
  1001. if (!xsd) {
  1002. DebugMsg((DM_WARNING, TEXT("ConnectToNameSpace::MakeselfRelativeSD failed with %d"), GetLastError()));
  1003. return FALSE;
  1004. }
  1005. if (!SetSecurityDescriptorControl( (SECURITY_DESCRIPTOR *)xsd, SE_DACL_PROTECTED, SE_DACL_PROTECTED )) {
  1006. DebugMsg((DM_WARNING, TEXT("ConnectToNameSpace::SetSecurityDescriptorControl failed with %d"), GetLastError()));
  1007. return FALSE;
  1008. }
  1009. hr = CreateAndCopyNameSpace(pWbemLocator,
  1010. xRootNameSpace,
  1011. xRootNameSpace,
  1012. xszWmiNameFromUserSid,
  1013. NEW_NS_FLAGS_COPY_CLASSES,
  1014. xsd,
  1015. 0);
  1016. if ( FAILED( hr ) )
  1017. {
  1018. DebugMsg((DM_WARNING, TEXT("ConnectToNameSpace:: CreateAndCopyNameSpace failed. Error=0x%08X."), hr));
  1019. return FALSE;
  1020. }
  1021. hr = pWbemLocator->ConnectServer( xNameSpace,
  1022. NULL,
  1023. NULL,
  1024. 0L,
  1025. 0L,
  1026. NULL,
  1027. NULL,
  1028. &xWbemServices );
  1029. if (FAILED(hr)) {
  1030. DebugMsg((DM_WARNING, TEXT("ConnectToNameSpace:: ConnectServer failed. hr=0x%08X."), hr));
  1031. return FALSE;
  1032. }
  1033. *ppWbemServices = xWbemServices.Acquire();
  1034. if (pbCreated)
  1035. *pbCreated = TRUE;
  1036. return TRUE;
  1037. }
  1038. //*************************************************************
  1039. //
  1040. // RsopDeleteUserNameSpace()
  1041. //
  1042. // Purpose: Deletes the name space for the user.
  1043. // This should be used with cae because it calls
  1044. // CoInitializeEx and can have other effects
  1045. //
  1046. // Parameters:
  1047. // szComputer - Computer name
  1048. // lpSid - Name of the User Name Space
  1049. //
  1050. // Return: TRUE if successful
  1051. // FALSE otherwise
  1052. //
  1053. //*************************************************************
  1054. BOOL RsopDeleteUserNameSpace(LPTSTR szComputer, LPTSTR lpSid)
  1055. {
  1056. IWbemLocator *pLocalWbemLocator=NULL;
  1057. BOOL bStatus = TRUE;
  1058. XCoInitialize xCoInit;
  1059. if (FAILED(xCoInit.Status())) {
  1060. DebugMsg((DM_VERBOSE, TEXT("ApplyGroupPolicy: CoInitializeEx failed with 0x%x."), xCoInit.Status() ));
  1061. }
  1062. {
  1063. CLocator locator;
  1064. LPTSTR szLocComputer;
  1065. szLocComputer = szComputer ? szComputer : TEXT(".");
  1066. XPtrLF<WCHAR> xszParentNameSpace = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR)*(lstrlen(RSOP_NS_DIAG_REMOTE_USERROOT_FMT)
  1067. +lstrlen(szLocComputer)+5));
  1068. if (!xszParentNameSpace) {
  1069. DebugMsg(( DM_WARNING, TEXT("RsopDeleteUserNameSpace: Unable to allocate memory 0" )));
  1070. return FALSE;
  1071. }
  1072. XPtrLF<WCHAR> xszWmiName = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR)*(lstrlen(lpSid)+1));
  1073. if (!xszWmiName)
  1074. {
  1075. DebugMsg(( DM_WARNING, TEXT("RsopDeleteUserNameSpace::couldn't allocate memory with error %d"), GetLastError()));
  1076. return FALSE;
  1077. }
  1078. ConvertSidToWMIName(lpSid, xszWmiName);
  1079. pLocalWbemLocator = locator.GetWbemLocator();
  1080. if (!pLocalWbemLocator) {
  1081. return FALSE;
  1082. }
  1083. if ( (szLocComputer[0] == TEXT('\\')) && (szLocComputer[1] == TEXT('\\')) )
  1084. szLocComputer += 2;
  1085. wsprintf(xszParentNameSpace, RSOP_NS_DIAG_REMOTE_USERROOT_FMT, szLocComputer);
  1086. HRESULT hr = DeleteNameSpace( xszWmiName, xszParentNameSpace, pLocalWbemLocator );
  1087. return SUCCEEDED( hr );
  1088. }
  1089. }
  1090. HRESULT
  1091. CreateCSE_EventSourceAssoc( IWbemServices* pServices,
  1092. LPWSTR szCSEGuid,
  1093. LPWSTR szEventLogSources )
  1094. {
  1095. HRESULT hr;
  1096. if ( !pServices || !szCSEGuid || !szEventLogSources )
  1097. {
  1098. DebugMsg( ( DM_WARNING, L"CreateCSEEventSourceNameAssoc: invalid arguments" ) );
  1099. return E_INVALIDARG;
  1100. }
  1101. //
  1102. // get the RSOP_ExtensionEventSource class
  1103. //
  1104. XBStr bstr = L"RSOP_ExtensionEventSource";
  1105. XInterface<IWbemClassObject> xClassSrc;
  1106. hr = pServices->GetObject( bstr,
  1107. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  1108. 0,
  1109. &xClassSrc,
  1110. 0 );
  1111. if ( FAILED( hr ) )
  1112. {
  1113. DebugMsg( ( DM_WARNING, L"CreateCSEEventSourceNameAssoc: GetObject failed, 0x%x", hr ) );
  1114. return hr;
  1115. }
  1116. //
  1117. // spawn the RSOP_ExtensionEventSource instance
  1118. //
  1119. XInterface<IWbemClassObject> xInstSrc;
  1120. hr = xClassSrc->SpawnInstance( 0, &xInstSrc );
  1121. if ( FAILED (hr) )
  1122. {
  1123. DebugMsg( ( DM_WARNING, L"CreateCSEEventSourceNameAssoc: SpawnInstance failed, 0x%x", hr ) );
  1124. return hr;
  1125. }
  1126. //
  1127. // get the RSOP_ExtensionEventSourceLink class
  1128. //
  1129. XInterface<IWbemClassObject> xClassLink;
  1130. bstr = L"RSOP_ExtensionEventSourceLink";
  1131. hr = pServices->GetObject( bstr,
  1132. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  1133. 0,
  1134. &xClassLink,
  1135. 0 );
  1136. if ( FAILED( hr ) )
  1137. {
  1138. DebugMsg( ( DM_WARNING, L"CreateCSEEventSourceNameAssoc: GetObject failed, 0x%x", hr ) );
  1139. return hr;
  1140. }
  1141. //
  1142. // spawn the RSOP_ExtensionEventSourceLink class
  1143. //
  1144. XInterface<IWbemClassObject> xInstLink;
  1145. hr = xClassLink->SpawnInstance( 0, &xInstLink );
  1146. if ( FAILED (hr) )
  1147. {
  1148. DebugMsg( ( DM_WARNING, L"CreateCSEEventSourceNameAssoc: SpawnInstance failed, 0x%x", hr ) );
  1149. return hr;
  1150. }
  1151. //
  1152. // RSOP_ExtensionEventSourceLink
  1153. //
  1154. //
  1155. // create the first key
  1156. //
  1157. const LPCWSTR szFormat = L"RSOP_ExtensionStatus.extensionGuid=\"%s\"";
  1158. XPtrLF<WCHAR> szCSE = LocalAlloc( LPTR, sizeof(WCHAR) * ( 48 + wcslen(szCSEGuid) ) );
  1159. if ( !szCSE )
  1160. {
  1161. DebugMsg( ( DM_WARNING, L"CreateCSEEventSourceNameAssoc: LocalAlloc failed, 0x%x", GetLastError() ) );
  1162. return E_OUTOFMEMORY;
  1163. }
  1164. //
  1165. // e.g. RSOP_ExtensionStatus.extensionGuid="{00000000-0000-0000-0000-000000000000}"
  1166. //
  1167. wsprintf( szCSE, szFormat, szCSEGuid );
  1168. VARIANT var;
  1169. XBStr bstrVal;
  1170. XBStr bstreventLogSource = L"eventLogSource";
  1171. XBStr bstreventLogName = L"eventLogName";
  1172. XBStr bstrextensionStatus = L"extensionStatus";
  1173. XBStr bstreventSource = L"eventSource";
  1174. XBStr bstrid = L"id";
  1175. var.vt = VT_BSTR;
  1176. //
  1177. // szEventLogSources is in the format,
  1178. // "(source, name)"
  1179. // "(source, name)"
  1180. // ...
  1181. //
  1182. LPWSTR szStart = szEventLogSources;
  1183. while ( *szStart )
  1184. {
  1185. //
  1186. // extensionStatus
  1187. //
  1188. bstrVal = szCSE;
  1189. var.bstrVal = bstrVal;
  1190. hr = xInstLink->Put(bstrextensionStatus,
  1191. 0,
  1192. &var,
  1193. 0 );
  1194. if ( FAILED( hr ) )
  1195. {
  1196. return hr;
  1197. }
  1198. GUID guid;
  1199. //
  1200. // create the [key]
  1201. //
  1202. hr = CoCreateGuid( &guid );
  1203. if ( FAILED(hr) )
  1204. {
  1205. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: CoCreateGuid failed, 0x%x", hr ) );
  1206. return hr;
  1207. }
  1208. WCHAR szGuid[64];
  1209. wsprintf( szGuid,
  1210. L"{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  1211. guid.Data1,
  1212. guid.Data2,
  1213. guid.Data3,
  1214. guid.Data4[0], guid.Data4[1],
  1215. guid.Data4[2], guid.Data4[3],
  1216. guid.Data4[4], guid.Data4[5],
  1217. guid.Data4[6], guid.Data4[7] );
  1218. LPWSTR szFormat = L"RSOP_ExtensionEventSource.id=\"%s\"";
  1219. DWORD dwSrcLen = wcslen(szGuid);
  1220. XPtrLF<WCHAR> szKey = LocalAlloc( LPTR, sizeof(WCHAR) * (dwSrcLen + 48));
  1221. if ( !szKey )
  1222. {
  1223. return E_OUTOFMEMORY;
  1224. }
  1225. wsprintf( szKey, szFormat, szGuid );
  1226. //
  1227. // eventSource
  1228. //
  1229. bstrVal = szKey;
  1230. var.bstrVal = bstrVal;
  1231. hr = xInstLink->Put(bstreventSource,
  1232. 0,
  1233. &var,
  1234. 0 );
  1235. if ( FAILED( hr ) )
  1236. {
  1237. return hr;
  1238. }
  1239. //
  1240. // RSOP_ExtensionEventSourceLink
  1241. //
  1242. hr = pServices->PutInstance(xInstLink,
  1243. WBEM_FLAG_CREATE_OR_UPDATE,
  1244. 0,
  1245. 0 );
  1246. if ( FAILED( hr ) )
  1247. {
  1248. return hr;
  1249. }
  1250. //
  1251. // id
  1252. //
  1253. bstrVal = szGuid;
  1254. var.bstrVal = bstrVal;
  1255. hr = xInstSrc->Put( bstrid,
  1256. 0,
  1257. &var,
  1258. 0 );
  1259. if ( FAILED( hr ) )
  1260. {
  1261. return hr;
  1262. }
  1263. //
  1264. // search for '('
  1265. //
  1266. szStart = wcschr( szStart, L'(' );
  1267. if ( !szStart )
  1268. {
  1269. break;
  1270. }
  1271. szStart++;
  1272. //
  1273. // search for ,
  1274. //
  1275. LPWSTR szEnd = wcschr( szStart, L',' );
  1276. if ( szEnd )
  1277. {
  1278. if ( szStart == szEnd )
  1279. {
  1280. return E_INVALIDARG;
  1281. }
  1282. *szEnd = 0;
  1283. }
  1284. else
  1285. {
  1286. return E_INVALIDARG;
  1287. }
  1288. //
  1289. // eventLogSource
  1290. //
  1291. bstrVal = szStart;
  1292. var.bstrVal = bstrVal;
  1293. hr = xInstSrc->Put( bstreventLogSource,
  1294. 0,
  1295. &var,
  1296. 0 );
  1297. if ( FAILED( hr ) )
  1298. {
  1299. return hr;
  1300. }
  1301. *szEnd = L',';
  1302. szStart = szEnd + 1;
  1303. //
  1304. // search for )
  1305. //
  1306. szEnd = wcschr( szStart, L')' );
  1307. if ( szEnd )
  1308. {
  1309. if ( szStart == szEnd )
  1310. {
  1311. return E_INVALIDARG;
  1312. }
  1313. *szEnd = 0;
  1314. }
  1315. else
  1316. {
  1317. return E_INVALIDARG;
  1318. }
  1319. //
  1320. // eventLogName
  1321. //
  1322. bstrVal = szStart;
  1323. var.bstrVal = bstrVal;
  1324. hr = xInstSrc->Put( bstreventLogName,
  1325. 0,
  1326. &var,
  1327. 0 );
  1328. if ( FAILED( hr ) )
  1329. {
  1330. return hr;
  1331. }
  1332. //
  1333. // RSOP_ExtensionEventSource
  1334. //
  1335. hr = pServices->PutInstance(xInstSrc,
  1336. WBEM_FLAG_CREATE_OR_UPDATE,
  1337. 0,
  1338. 0 );
  1339. if ( FAILED( hr ) )
  1340. {
  1341. return hr;
  1342. }
  1343. //
  1344. // next
  1345. //
  1346. *szEnd = L')';
  1347. szStart = wcschr( szEnd, 0 );
  1348. szStart++;
  1349. }
  1350. return hr;
  1351. }
  1352. HRESULT
  1353. DeleteCSE_EventSourceAssoc( IWbemServices* pServices,
  1354. LPWSTR szCSEGuid )
  1355. {
  1356. HRESULT hr = S_OK;
  1357. if ( !pServices || !szCSEGuid )
  1358. {
  1359. DebugMsg( ( DM_WARNING, L"DeleteCSE_EventSourceAssoc: invalid arguments" ) );
  1360. return E_INVALIDARG;
  1361. }
  1362. //
  1363. // construct the query
  1364. //
  1365. WCHAR szQuery[256];
  1366. LPWSTR szFormat = L"SELECT * FROM RSOP_ExtensionEventSourceLink WHERE extensionStatus=\"RSOP_ExtensionStatus.extensionGuid=\\\"%s\\\"\"";
  1367. wsprintf( szQuery, szFormat, szCSEGuid );
  1368. XBStr bstrLanguage = L"WQL";
  1369. XBStr bstrQuery = szQuery;
  1370. XInterface<IEnumWbemClassObject> pEnum;
  1371. XBStr bstrPath = L"__PATH";
  1372. XBStr bstrEventSource = L"eventSource";
  1373. //
  1374. // search for RSOP_ExtensionEventSourceLink
  1375. //
  1376. hr = pServices->ExecQuery( bstrLanguage,
  1377. bstrQuery,
  1378. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_ENSURE_LOCATABLE,
  1379. 0,
  1380. &pEnum );
  1381. if ( SUCCEEDED( hr ) )
  1382. {
  1383. DWORD dwReturned = 0;
  1384. do
  1385. {
  1386. XInterface<IWbemClassObject> xInst;
  1387. hr = pEnum->Next( WBEM_INFINITE,
  1388. 1,
  1389. &xInst,
  1390. &dwReturned );
  1391. if ( SUCCEEDED( hr ) && dwReturned == 1 )
  1392. {
  1393. //
  1394. // delete RSOP_ExtensionEventSource
  1395. //
  1396. VARIANT varSource;
  1397. VariantInit( &varSource );
  1398. XVariant xVarSource( &varSource );
  1399. hr = xInst->Get(bstrEventSource,
  1400. 0,
  1401. &varSource,
  1402. 0,
  1403. 0 );
  1404. if ( SUCCEEDED( hr ) )
  1405. {
  1406. hr = pServices->DeleteInstance( varSource.bstrVal,
  1407. 0L,
  1408. 0,
  1409. 0 );
  1410. if ( SUCCEEDED( hr ) )
  1411. {
  1412. //
  1413. // delete RSOP_ExtensionEventSourceLink
  1414. //
  1415. VARIANT varLink;
  1416. VariantInit( &varLink );
  1417. hr = xInst->Get(bstrPath,
  1418. 0L,
  1419. &varLink,
  1420. 0,
  1421. 0 );
  1422. if ( SUCCEEDED(hr) )
  1423. {
  1424. XVariant xVarLink( &varLink );
  1425. hr = pServices->DeleteInstance( varLink.bstrVal,
  1426. 0L,
  1427. 0,
  1428. 0 );
  1429. if ( FAILED( hr ) )
  1430. {
  1431. return hr;
  1432. }
  1433. }
  1434. }
  1435. }
  1436. }
  1437. } while ( SUCCEEDED( hr ) && dwReturned == 1 );
  1438. }
  1439. if ( hr == S_FALSE )
  1440. {
  1441. hr = S_OK;
  1442. }
  1443. return hr;
  1444. }
  1445. HRESULT UpdateGPCoreStatus(IWbemLocator *pWbemLocator,
  1446. LPWSTR szSid, LPWSTR szNameSpace)
  1447. {
  1448. RSOPEXTSTATUS GPCoreRsopExtStatus;
  1449. BOOL bMachine = (szSid == NULL);
  1450. XPtrLF<WCHAR> xszFullNameSpace;
  1451. HRESULT hr = S_OK;
  1452. LPWSTR lpEnd = NULL;
  1453. DWORD dwError = ERROR_SUCCESS;
  1454. xszFullNameSpace = (LPWSTR) LocalAlloc(LPTR, sizeof(WCHAR)*(wcslen(szNameSpace)+5+
  1455. (MAX(lstrlen(RSOP_NS_USER_OFFSET), lstrlen(RSOP_NS_MACHINE_OFFSET)))));
  1456. if (!xszFullNameSpace) {
  1457. hr = HRESULT_FROM_WIN32(GetLastError());
  1458. DebugMsg( ( DM_WARNING, L"UpdateGPCoreStatus: failed to allocate memory, 0x%x", hr ) );
  1459. return hr;
  1460. }
  1461. //
  1462. // Construct the namespace
  1463. //
  1464. wcscpy(xszFullNameSpace, szNameSpace);
  1465. lpEnd = CheckSlash(xszFullNameSpace);
  1466. wcscpy(lpEnd, bMachine ? RSOP_NS_MACHINE_OFFSET : RSOP_NS_USER_OFFSET);
  1467. DebugMsg( ( DM_VERBOSE, L"UpdateGPCoreStatus: updating status from <%s> registry for gp core",
  1468. bMachine ? RSOP_NS_MACHINE_OFFSET : RSOP_NS_USER_OFFSET) );
  1469. //
  1470. // read the GP Core extension status
  1471. //
  1472. dwError = ReadLoggingStatus(szSid, NULL, &GPCoreRsopExtStatus);
  1473. if (dwError != ERROR_SUCCESS) {
  1474. return HRESULT_FROM_WIN32(dwError);
  1475. }
  1476. //
  1477. // Get th wbem interface pointer to the namespace constructed
  1478. //
  1479. XInterface<IWbemServices> xWbemServices;
  1480. hr = GetWbemServicesPtr( xszFullNameSpace, &pWbemLocator, &xWbemServices );
  1481. if (FAILED(hr)) {
  1482. DebugMsg( ( DM_WARNING, L"UpdateGPCoreStatus: GetWbemServicesPtr failed, hr = 0x%x", hr ) );
  1483. return hr;
  1484. }
  1485. GPTEXT_API* pGpText = LoadGpTextApi();
  1486. if ( pGpText )
  1487. {
  1488. hr = pGpText->pfnScrRegGPOListToWbem( szSid, xWbemServices );
  1489. if ( FAILED( hr ) )
  1490. {
  1491. DebugMsg( ( DM_WARNING, L"UpdateGPCoreStatus: ScrRegGPOListToWbem failed, hr = 0x%x", hr ) );
  1492. return hr;
  1493. }
  1494. }
  1495. //
  1496. // Log the data actually
  1497. //
  1498. CExtSessionLogger extLogger( xWbemServices );
  1499. if (!extLogger.Set(NULL, TRUE, &GPCoreRsopExtStatus))
  1500. return E_FAIL;
  1501. return S_OK;
  1502. }