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.

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