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.

2176 lines
58 KiB

  1. //*************************************************************
  2. //
  3. // Resultant set of policy, Logger classes
  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 "stdio.h"
  13. #include "uenv.h"
  14. #include <wbemcli.h>
  15. #include "reghash.h"
  16. #include "logger.h"
  17. #include "..\rsoputil\wbemtime.h"
  18. #include "SmartPtr.h"
  19. #include "rsopinc.h"
  20. #include <ntdsapi.h>
  21. BOOL PrintToString( XPtrST<WCHAR>& xwszValue, WCHAR *wszString,
  22. WCHAR *pwszParam1, WCHAR *pwszParam2, DWORD dwParam3 );
  23. BOOL LogTimeProperty( IWbemClassObject *pInstance, BSTR bstrPropName, SYSTEMTIME *pSysTime );
  24. HRESULT LogSecurityGroups( IWbemClassObject *pInstance, BSTR bstrPropName, PTOKEN_GROUPS pTokenGroups );
  25. LPWSTR DsUnquoteDN( LPCWSTR szQDN, BOOL bGpLink = FALSE );
  26. const MAX_LENGTH = 100; // Length of stringized guid
  27. //*************************************************************
  28. //
  29. // DsUnquoteDN()
  30. //
  31. // Purpose: Convert a quoted/escaped DN to an unquoted/unescaped DN
  32. //
  33. // Parameters: szQDN - quoted/escaped DN ou=\+test\+1,dc="nt dev",dc=com
  34. //
  35. // Return:
  36. // szUDN - unquoted/unescaped DN ou=+test+1,dc=nt dev,dc=com
  37. //
  38. //*************************************************************
  39. LPWSTR
  40. DsUnquoteDN( LPCWSTR szQDN, BOOL bGpLink )
  41. {
  42. DWORD dwError = ERROR_SUCCESS;
  43. DWORD cQDN = wcslen( szQDN );
  44. DWORD cKey = 0, cVal = 0;
  45. LPCWSTR szKey = 0, szVal = 0;
  46. XPtrLF<WCHAR> szUDN = LocalAlloc( LPTR, ( wcslen( szQDN ) + 32 ) * sizeof( WCHAR ) );
  47. if ( !szUDN )
  48. {
  49. return 0;
  50. }
  51. while ( cQDN )
  52. {
  53. //
  54. // crack the DN and get the RDN
  55. // ou=\+test,dc=microsoft,dc=com
  56. //
  57. dwError = DsGetRdnW(&szQDN,
  58. &cQDN,
  59. &szKey,
  60. &cKey,
  61. &szVal,
  62. &cVal );
  63. if ( dwError == ERROR_SUCCESS )
  64. {
  65. //
  66. // slap on the key, ou=
  67. //
  68. if ( cKey )
  69. {
  70. wcsncat( szUDN, szKey, cKey );
  71. wcscat( szUDN, L"=" );
  72. }
  73. if ( cVal )
  74. {
  75. //
  76. // unquote/unescape the RDN
  77. //
  78. DWORD cURDN = 255;
  79. WCHAR szURDN[255];
  80. dwError = DsUnquoteRdnValue(cVal,
  81. szVal,
  82. &cURDN,
  83. szURDN );
  84. if ( dwError == ERROR_SUCCESS )
  85. {
  86. //
  87. // slap on the unquoted value, +test,
  88. //
  89. LPWSTR szEnd = szUDN + wcslen( szUDN );
  90. for ( DWORD i = 0 ; i < cURDN ; i++ )
  91. {
  92. if ( szURDN[i] == L'"' )
  93. {
  94. *szEnd++ = L'\\';
  95. if ( bGpLink )
  96. {
  97. *szEnd++ = L'\\';
  98. *szEnd++ = L'\\';
  99. }
  100. }
  101. *szEnd++ = szURDN[i];
  102. }
  103. *szEnd++ = L',';
  104. *szEnd = 0;
  105. }
  106. else
  107. {
  108. break;
  109. }
  110. }
  111. }
  112. else
  113. {
  114. break;
  115. }
  116. }
  117. if ( dwError == ERROR_SUCCESS )
  118. {
  119. //
  120. // remove the extra trailing ','
  121. //
  122. cKey = wcslen( szUDN );
  123. if ( cKey )
  124. {
  125. szUDN[cKey-1] = 0;
  126. }
  127. return szUDN.Acquire();
  128. }
  129. return 0;
  130. }
  131. //*************************************************************
  132. //
  133. // CSessionLogger::CSessionLogger()
  134. //
  135. // Purpose: Constructor
  136. //
  137. // Parameters: pWbemServices - Wbem services
  138. //
  139. //*************************************************************
  140. CSessionLogger::CSessionLogger( IWbemServices *pWbemServices )
  141. : m_bInitialized(FALSE),
  142. m_pWbemServices(pWbemServices)
  143. {
  144. m_xbstrId = L"id";
  145. if ( !m_xbstrId )
  146. return;
  147. m_xbstrTargetName = L"targetName";
  148. if ( !m_xbstrTargetName )
  149. return;
  150. m_xbstrSOM = L"SOM";
  151. if ( !m_xbstrSOM )
  152. return;
  153. m_xbstrSecurityGroups = L"SecurityGroups";
  154. if ( !m_xbstrSecurityGroups )
  155. return;
  156. m_xbstrSite = L"Site";
  157. if ( !m_xbstrSite )
  158. return;
  159. m_xbstrCreationTime = L"creationTime";
  160. if ( !m_xbstrCreationTime )
  161. return;
  162. m_xbstrIsSlowLink = L"slowLink";
  163. if ( !m_xbstrIsSlowLink )
  164. return;
  165. m_xbstrVersion = L"version";
  166. if ( !m_xbstrVersion )
  167. return;
  168. m_xbstrClass = L"RSOP_Session";
  169. if ( !m_xbstrClass )
  170. return;
  171. HRESULT hr = m_pWbemServices->GetObject( m_xbstrClass,
  172. 0L,
  173. NULL,
  174. &m_xClass,
  175. NULL );
  176. if ( FAILED(hr) ) {
  177. DebugMsg((DM_WARNING, TEXT("CSessionLogger::CSessionLogger: GetObject failed with 0x%x" ), hr ));
  178. return;
  179. }
  180. m_bInitialized = TRUE;
  181. }
  182. //*************************************************************
  183. //
  184. // CSessionLogger::Log()
  185. //
  186. // Purpose: Logs an instance of session object
  187. //
  188. //*************************************************************
  189. BOOL CSessionLogger::Log(LPRSOPSESSIONDATA lprsopSessionData )
  190. {
  191. if ( !m_bInitialized ) {
  192. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Failed to initialize." )));
  193. return FALSE;
  194. }
  195. //
  196. // First get the creation time
  197. //
  198. XBStr xbstrCreationTimeValue; // initialised to null
  199. XSafeArray xsaSecurityGrps;
  200. XBStr xbstrInstancePath = L"RSOP_Session.id=\"Session1\"";
  201. if(!xbstrInstancePath)
  202. {
  203. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Failed to allocate memory.")));
  204. return FALSE;
  205. }
  206. XInterface<IWbemClassObject>xpInstance = NULL;
  207. HRESULT hr = m_pWbemServices->GetObject(xbstrInstancePath, 0, NULL, &xpInstance, NULL);
  208. if(SUCCEEDED(hr))
  209. {
  210. VARIANT var;
  211. VariantInit(&var);
  212. XVariant xVar(&var);
  213. hr = xpInstance->Get(m_xbstrCreationTime, 0, &var, NULL, NULL);
  214. if((SUCCEEDED(hr)) && ( var.vt != VT_NULL ))
  215. {
  216. xbstrCreationTimeValue = var.bstrVal;
  217. }
  218. else
  219. {
  220. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: Get failed. hr=0x%08X."), hr));
  221. }
  222. VariantClear(&var);
  223. hr = xpInstance->Get(m_xbstrSecurityGroups, 0, &var, NULL, NULL);
  224. if((SUCCEEDED(hr)) && ( var.vt != VT_NULL ))
  225. {
  226. SafeArrayCopy(var.parray, &xsaSecurityGrps);
  227. }
  228. else
  229. {
  230. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: Get failed. hr=0x%08X."), hr));
  231. }
  232. }
  233. else {
  234. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: GetObject failed. hr=0x%08X"), hr));
  235. }
  236. IWbemClassObject *pInstance = NULL;
  237. hr = m_xClass->SpawnInstance( 0, &pInstance );
  238. if ( FAILED(hr) ) {
  239. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: SpawnInstance failed with 0x%x" ), hr ));
  240. return FALSE;
  241. }
  242. XInterface<IWbemClassObject> xInstance( pInstance );
  243. XBStr xId( L"Session1" );
  244. if ( !xId ) {
  245. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Failed to allocate memory" )));
  246. return FALSE;
  247. }
  248. VARIANT var;
  249. var.vt = VT_BSTR;
  250. var.bstrVal = xId;
  251. hr = pInstance->Put( m_xbstrId, 0, &var, 0 );
  252. if ( FAILED(hr) ) {
  253. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Put failed with 0x%x" ), hr ));
  254. return FALSE;
  255. }
  256. //
  257. // Version
  258. //
  259. var.vt = VT_I4;
  260. var.lVal = RSOP_MOF_SCHEMA_VERSION;
  261. hr = pInstance->Put( m_xbstrVersion, 0, &var, 0 );
  262. if ( FAILED(hr) ) {
  263. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Put failed with 0x%x" ), hr ));
  264. return FALSE;
  265. }
  266. //
  267. // IsSlowLink
  268. //
  269. var.vt = VT_BOOL;
  270. var.boolVal = lprsopSessionData->bSlowLink ? VARIANT_TRUE : VARIANT_FALSE;
  271. hr = pInstance->Put( m_xbstrIsSlowLink, 0, &var, 0 );
  272. if ( FAILED(hr) )
  273. {
  274. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Put failed with 0x%x" ), hr ));
  275. return FALSE;
  276. }
  277. //
  278. // Target Name, can be null for dummy user in planning mode
  279. //
  280. XBStr xTarget( lprsopSessionData->pwszTargetName );
  281. if ( lprsopSessionData->pwszTargetName ) {
  282. if ( !xTarget ) {
  283. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Failed to allocate memory" )));
  284. return FALSE;
  285. }
  286. var.vt = VT_BSTR;
  287. var.bstrVal = xTarget;
  288. hr = pInstance->Put( m_xbstrTargetName, 0, &var, 0 );
  289. if ( FAILED(hr) ) {
  290. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Put failed with 0x%x" ), hr ));
  291. return FALSE;
  292. }
  293. }
  294. //
  295. // SOM, if Applicable (non NULL)
  296. //
  297. XBStr xSOM( lprsopSessionData->pwszSOM );
  298. if (lprsopSessionData->pwszSOM) {
  299. if ( !xSOM ) {
  300. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Failed to allocate memory" )));
  301. return FALSE;
  302. }
  303. var.vt = VT_BSTR;
  304. var.bstrVal = xSOM;
  305. hr = pInstance->Put( m_xbstrSOM, 0, &var, 0 );
  306. if ( FAILED(hr) ) {
  307. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Put failed with 0x%x" ), hr ));
  308. return FALSE;
  309. }
  310. }
  311. else {
  312. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: new SOM is NULL")));
  313. }
  314. //
  315. // Security Group, if Applicable (non NULL)
  316. //
  317. if (lprsopSessionData->bLogSecurityGroup) {
  318. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: logging new security grps" )));
  319. hr = LogSecurityGroups(pInstance, m_xbstrSecurityGroups, lprsopSessionData->pSecurityGroups);
  320. }
  321. else {
  322. if ( lprsopSessionData->pSecurityGroups && !xsaSecurityGrps ) {
  323. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: logging new security grps because it wasn't defined before" )));
  324. hr = LogSecurityGroups(pInstance, m_xbstrSecurityGroups, lprsopSessionData->pSecurityGroups);
  325. }
  326. else {
  327. //
  328. // reset the old value
  329. //
  330. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: restoring old security grps" )));
  331. var.vt = VT_ARRAY | VT_BSTR;
  332. var.parray = (SAFEARRAY *)xsaSecurityGrps;
  333. hr = pInstance->Put( m_xbstrSecurityGroups, 0, &var, 0 );
  334. }
  335. }
  336. if ( FAILED(hr) ) {
  337. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Put failed with 0x%x" ), hr ));
  338. return FALSE;
  339. }
  340. //
  341. // Site, if Applicable (non NULL)
  342. //
  343. XBStr xSite( lprsopSessionData->pwszSite );
  344. if (lprsopSessionData->pwszSite) {
  345. if ( !xSite ) {
  346. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Failed to allocate memory" )));
  347. return FALSE;
  348. }
  349. var.vt = VT_BSTR;
  350. var.bstrVal = lprsopSessionData->pwszSite;
  351. hr = pInstance->Put( m_xbstrSite, 0, &var, 0 );
  352. if ( FAILED(hr) ) {
  353. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Put failed with 0x%x" ), hr ));
  354. return FALSE;
  355. }
  356. }
  357. else {
  358. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: new Site is NULL")));
  359. }
  360. //
  361. // Update or set the creation Time
  362. //
  363. SYSTEMTIME sysTime;
  364. if (xbstrCreationTimeValue) {
  365. var.vt = VT_BSTR;
  366. var.bstrVal = xbstrCreationTimeValue;
  367. hr = pInstance->Put( m_xbstrCreationTime, 0, &var, 0 );
  368. if ( FAILED(hr) ) {
  369. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: Put failed for creationtime with 0x%x" ), hr ));
  370. return FALSE;
  371. }
  372. }
  373. else {
  374. // if it doesn't exist already create it as current time..
  375. GetSystemTime(&sysTime);
  376. LogTimeProperty( pInstance, m_xbstrCreationTime, &sysTime );
  377. }
  378. //
  379. // Instantiate...
  380. //
  381. hr = m_pWbemServices->PutInstance( pInstance, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL );
  382. if ( FAILED(hr) ) {
  383. DebugMsg((DM_WARNING, TEXT("CSessionLogger::Log: PutInstance failed with 0x%x" ), hr ));
  384. return FALSE;
  385. }
  386. return TRUE;
  387. }
  388. //*************************************************************
  389. //
  390. // CSomLogger::CSOMLogger()
  391. //
  392. // Purpose: Constructor
  393. //
  394. // Parameters: dwFlags - Flags
  395. // pWbemServices - Wbem services
  396. //
  397. //*************************************************************
  398. CSOMLogger::CSOMLogger( DWORD dwFlags, IWbemServices *pWbemServices )
  399. : m_bInitialized(FALSE),
  400. m_dwFlags(dwFlags),
  401. m_pWbemServices(pWbemServices)
  402. {
  403. m_xbstrId = L"id";
  404. if ( !m_xbstrId )
  405. return;
  406. m_xbstrType = L"type";
  407. if ( !m_xbstrType )
  408. return;
  409. m_xbstrOrder = L"SOMOrder";
  410. if ( !m_xbstrOrder )
  411. return;
  412. m_xbstrBlocking = L"blocking";
  413. if ( !m_xbstrBlocking )
  414. return;
  415. m_xbstrBlocked = L"blocked";
  416. if ( !m_xbstrBlocked )
  417. return;
  418. m_xbstrReason = L"reason";
  419. if ( !m_xbstrReason )
  420. return;
  421. m_xbstrClass = L"RSOP_SOM";
  422. if ( !m_xbstrClass )
  423. return;
  424. HRESULT hr = m_pWbemServices->GetObject( m_xbstrClass,
  425. 0L,
  426. NULL,
  427. &m_xClass,
  428. NULL );
  429. if ( FAILED(hr) )
  430. {
  431. DebugMsg((DM_WARNING, TEXT("CSOMLogger::CSOMLogger: GetObject failed with 0x%x" ), hr ));
  432. return;
  433. }
  434. hr = m_xClass->SpawnInstance( 0, &m_pInstance );
  435. if ( FAILED(hr) )
  436. {
  437. DebugMsg((DM_WARNING, TEXT("CSOMLogger::CSOMLogger: SpawnInstance failed with 0x%x" ), hr ));
  438. return;
  439. }
  440. m_bInitialized = TRUE;
  441. }
  442. //*************************************************************
  443. //
  444. // CSomLogger::Log()
  445. //
  446. // Purpose: Logs an instance of scope of management
  447. //
  448. // Parameters: pSOM - SOM to log
  449. //
  450. //*************************************************************
  451. BOOL CSOMLogger::Log( SCOPEOFMGMT *pSOM, DWORD dwOrder, BOOL bLoopback )
  452. {
  453. if ( !m_bInitialized )
  454. {
  455. DebugMsg((DM_WARNING, TEXT("CSOMLogger::Log: Failed to initialize." )));
  456. return FALSE;
  457. }
  458. HRESULT hr;
  459. VARIANT var;
  460. var.vt = VT_I4;
  461. var.lVal = pSOM->dwType;
  462. hr = m_pInstance->Put( m_xbstrType, 0, &var, 0 );
  463. if ( FAILED(hr) )
  464. {
  465. DebugMsg((DM_WARNING, TEXT("CSOMLogger::Log: Put failed with 0x%x" ), hr ));
  466. return FALSE;
  467. }
  468. var.lVal = dwOrder;
  469. hr = m_pInstance->Put( m_xbstrOrder, 0, &var, 0 );
  470. if ( FAILED(hr) ) {
  471. DebugMsg((DM_WARNING, TEXT("CSOMLogger::Log: Put failed with 0x%x" ), hr ));
  472. return FALSE;
  473. }
  474. var.vt = VT_BOOL;
  475. var.boolVal = pSOM->bBlocking ? VARIANT_TRUE : VARIANT_FALSE;
  476. hr = m_pInstance->Put( m_xbstrBlocking, 0, &var, 0 );
  477. if ( FAILED(hr) ) {
  478. DebugMsg((DM_WARNING, TEXT("CSOMLogger::Log: Put failed with 0x%x" ), hr ));
  479. return FALSE;
  480. }
  481. var.vt = VT_BOOL;
  482. var.boolVal = pSOM->bBlocked ? VARIANT_TRUE : VARIANT_FALSE;
  483. hr = m_pInstance->Put( m_xbstrBlocked, 0, &var, 0 );
  484. if ( FAILED(hr) ) {
  485. DebugMsg((DM_WARNING, TEXT("CSOMLogger::Log: Put failed with 0x%x" ), hr ));
  486. return FALSE;
  487. }
  488. var.vt = VT_I4;
  489. var.lVal = bLoopback ? 2 : 1;
  490. hr = m_pInstance->Put( m_xbstrReason, 0, &var, 0 );
  491. if ( FAILED(hr) ) {
  492. DebugMsg((DM_WARNING, TEXT("CSOMLogger::Log: Put failed with 0x%x" ), hr ));
  493. return FALSE;
  494. }
  495. XPtrLF<WCHAR> xDSPath = DsUnquoteDN( pSOM->pwszSOMId );
  496. XBStr xId;
  497. if ( !xDSPath )
  498. {
  499. xId = pSOM->pwszSOMId;
  500. }
  501. else
  502. {
  503. xId = xDSPath;
  504. }
  505. if ( !xId ) {
  506. DebugMsg((DM_WARNING, TEXT("CSOMLogger::Log: Failed to allocate memory" )));
  507. return FALSE;
  508. }
  509. var.vt = VT_BSTR;
  510. var.bstrVal = xId;
  511. hr = m_pInstance->Put( m_xbstrId, 0, &var, 0 );
  512. if ( FAILED(hr) ) {
  513. DebugMsg((DM_WARNING, TEXT("CSOMLogger::Log: Put failed with 0x%x" ), hr ));
  514. return FALSE;
  515. }
  516. hr = m_pWbemServices->PutInstance( m_pInstance, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL );
  517. if ( FAILED(hr) ) {
  518. DebugMsg((DM_WARNING, TEXT("CSOMLogger::Log: PutInstance failed with 0x%x" ), hr ));
  519. return FALSE;
  520. }
  521. return TRUE;
  522. }
  523. //*************************************************************
  524. //
  525. // CGpoLogger::CGpoLogger()
  526. //
  527. // Purpose: Constructor
  528. //
  529. // Parameters: pWbemServices - Wbem services
  530. //
  531. //*************************************************************
  532. CGpoLogger::CGpoLogger( DWORD dwFlags, IWbemServices *pWbemServices )
  533. : m_bInitialized(FALSE),
  534. m_dwFlags(dwFlags),
  535. m_pWbemServices(pWbemServices)
  536. {
  537. m_xbstrId = L"id";
  538. if ( !m_xbstrId )
  539. return;
  540. m_xbstrGuidName = L"guidName";
  541. if ( !m_xbstrGuidName )
  542. return;
  543. m_xbstrDisplayName = L"name";
  544. if ( !m_xbstrDisplayName )
  545. return;
  546. m_xbstrFileSysPath = L"fileSystemPath";
  547. if ( !m_xbstrFileSysPath )
  548. return;
  549. m_xbstrVer = L"version";
  550. if ( !m_xbstrVer )
  551. return;
  552. m_xbstrAccessDenied = L"accessDenied";
  553. if ( !m_xbstrAccessDenied )
  554. return;
  555. m_xbstrEnabled = L"enabled";
  556. if ( !m_xbstrEnabled )
  557. return;
  558. m_xbstrSD = L"securityDescriptor";
  559. if ( !m_xbstrSD )
  560. return;
  561. m_xbstrFilterAllowed = L"filterAllowed";
  562. if ( !m_xbstrFilterAllowed )
  563. return;
  564. m_xbstrFilterId = L"filterId";
  565. if ( !m_xbstrFilterId )
  566. return;
  567. m_xbstrClass = L"RSOP_GPO";
  568. if ( !m_xbstrClass )
  569. return;
  570. HRESULT hr = m_pWbemServices->GetObject( m_xbstrClass,
  571. 0L,
  572. NULL,
  573. &m_xClass,
  574. NULL );
  575. if ( FAILED(hr) )
  576. {
  577. DebugMsg((DM_WARNING, TEXT("CGpoLogger::CGpoLogger: GetObject failed with 0x%x" ), hr ));
  578. return;
  579. }
  580. hr = m_xClass->SpawnInstance( 0, &m_pInstance );
  581. if ( FAILED(hr) )
  582. {
  583. DebugMsg((DM_WARNING, TEXT("CGpoLogger::CGpoLogger: SpawnInstance failed with 0x%x" ), hr ));
  584. return;
  585. }
  586. m_bInitialized = TRUE;
  587. }
  588. //*************************************************************
  589. //
  590. // CGpoLogger::Log()
  591. //
  592. // Purpose: Logs an instance of scope of management
  593. //
  594. // Parameters: pGpContainer - Gp container
  595. //
  596. //*************************************************************
  597. BOOL CGpoLogger::Log( GPCONTAINER *pGpContainer )
  598. {
  599. if ( !m_bInitialized ) {
  600. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Failed to initialize." )));
  601. return FALSE;
  602. }
  603. HRESULT hr;
  604. VARIANT var;
  605. var.vt = VT_I4;
  606. if ( m_dwFlags & GP_MACHINE )
  607. var.lVal = pGpContainer->dwMachVersion;
  608. else
  609. var.lVal = pGpContainer->dwUserVersion;
  610. hr = m_pInstance->Put( m_xbstrVer, 0, &var, 0 );
  611. if ( FAILED(hr) ) {
  612. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Put failed with 0x%x" ), hr ));
  613. return FALSE;
  614. }
  615. //
  616. // Note the change from disabled <--> enabled
  617. //
  618. var.vt = VT_BOOL;
  619. if ( m_dwFlags & GP_MACHINE )
  620. var.boolVal = pGpContainer->bMachDisabled ? VARIANT_FALSE : VARIANT_TRUE;
  621. else
  622. var.boolVal = pGpContainer->bUserDisabled ? VARIANT_FALSE : VARIANT_TRUE;
  623. hr = m_pInstance->Put( m_xbstrEnabled, 0, &var, 0 );
  624. if ( FAILED(hr) ) {
  625. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Put failed with 0x%x" ), hr ));
  626. return FALSE;
  627. }
  628. var.boolVal = pGpContainer->bAccessDenied ? VARIANT_TRUE : VARIANT_FALSE;
  629. hr = m_pInstance->Put( m_xbstrAccessDenied, 0, &var, 0 );
  630. if ( FAILED(hr) ) {
  631. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Put failed with 0x%x" ), hr ));
  632. return FALSE;
  633. }
  634. var.boolVal = pGpContainer->bFilterAllowed ? VARIANT_TRUE : VARIANT_FALSE;
  635. hr = m_pInstance->Put( m_xbstrFilterAllowed, 0, &var, 0 );
  636. if ( FAILED(hr) ) {
  637. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Put failed with 0x%x" ), hr ));
  638. return FALSE;
  639. }
  640. if ( pGpContainer->pwszFilterId ) {
  641. XBStr xFilterId( pGpContainer->pwszFilterId );
  642. if ( !xFilterId ) {
  643. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Failed to allocate memory" )));
  644. return FALSE;
  645. }
  646. var.vt = VT_BSTR;
  647. var.bstrVal = xFilterId;
  648. hr = m_pInstance->Put( m_xbstrFilterId, 0, &var, 0 );
  649. if ( FAILED(hr) ) {
  650. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Put failed with 0x%x" ), hr ));
  651. return FALSE;
  652. }
  653. }
  654. XBStr xId = pGpContainer->pwszDSPath;
  655. if ( !xId ) {
  656. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Failed to allocate memory" )));
  657. return FALSE;
  658. }
  659. var.vt = VT_BSTR;
  660. var.bstrVal = xId;
  661. hr = m_pInstance->Put( m_xbstrId, 0, &var, 0 );
  662. if ( FAILED(hr) ) {
  663. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Put failed with 0x%x" ), hr ));
  664. return FALSE;
  665. }
  666. XBStr xGuid( pGpContainer->pwszGPOName );
  667. if ( !xGuid ) {
  668. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Failed to allocate memory" )));
  669. return FALSE;
  670. }
  671. var.bstrVal = xGuid;
  672. hr = m_pInstance->Put( m_xbstrGuidName, 0, &var, 0 );
  673. if ( FAILED(hr) ) {
  674. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Put failed with 0x%x" ), hr ));
  675. return FALSE;
  676. }
  677. XBStr xDisplay( pGpContainer->pwszDisplayName );
  678. if ( !xDisplay ) {
  679. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Failed to allocate memory" )));
  680. return FALSE;
  681. }
  682. var.bstrVal = xDisplay;
  683. hr = m_pInstance->Put( m_xbstrDisplayName, 0, &var, 0 );
  684. if ( FAILED(hr) ) {
  685. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Put failed with 0x%x" ), hr ));
  686. return FALSE;
  687. }
  688. XBStr xFile( pGpContainer->pwszFileSysPath );
  689. if ( !xFile ) {
  690. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Failed to allocate memory" )));
  691. return FALSE;
  692. }
  693. var.bstrVal = xFile;
  694. hr = m_pInstance->Put( m_xbstrFileSysPath, 0, &var, 0 );
  695. if ( FAILED(hr) ) {
  696. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: Put failed with 0x%x" ), hr ));
  697. return FALSE;
  698. }
  699. if ( !LogBlobProperty( m_pInstance, m_xbstrSD,
  700. (BYTE *) pGpContainer->pSD, pGpContainer->cbSDLen ) ) {
  701. return FALSE;
  702. }
  703. hr = m_pWbemServices->PutInstance( m_pInstance, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL );
  704. if ( FAILED(hr) ) {
  705. DebugMsg((DM_WARNING, TEXT("CGpoLogger::Log: PutInstance failed with 0x%x" ), hr ));
  706. return FALSE;
  707. }
  708. return TRUE;
  709. }
  710. //*************************************************************
  711. //
  712. // CGpLinkLogger::CGpLinkLogger()
  713. //
  714. // Purpose: Constructor
  715. //
  716. // Parameters: pWbemServices - Wbem services
  717. //
  718. //*************************************************************
  719. CGpLinkLogger::CGpLinkLogger( IWbemServices *pWbemServices )
  720. : m_bInitialized(FALSE),
  721. m_pWbemServices(pWbemServices)
  722. {
  723. m_xbstrSOM = L"SOM";
  724. if ( !m_xbstrSOM )
  725. return;
  726. m_xbstrGPO = L"GPO";
  727. if ( !m_xbstrGPO )
  728. return;
  729. m_xbstrOrder = L"somOrder";
  730. if ( !m_xbstrOrder )
  731. return;
  732. m_xbstrLinkOrder = L"linkOrder";
  733. if ( !m_xbstrLinkOrder )
  734. return;
  735. m_xbstrAppliedOrder = L"appliedOrder";
  736. if ( !m_xbstrAppliedOrder )
  737. return;
  738. m_xbstrEnabled = L"Enabled";
  739. if ( !m_xbstrEnabled )
  740. return;
  741. m_xbstrEnforced = L"noOverride";
  742. if ( !m_xbstrEnforced )
  743. return;
  744. m_xbstrClass = L"RSOP_GPLink";
  745. if ( !m_xbstrClass )
  746. return;
  747. HRESULT hr = m_pWbemServices->GetObject( m_xbstrClass,
  748. 0L,
  749. NULL,
  750. &m_xClass,
  751. NULL );
  752. if ( FAILED(hr) ) {
  753. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::CGpLinkLogger: GetObject failed with 0x%x" ), hr ));
  754. return;
  755. }
  756. hr = m_xClass->SpawnInstance( 0, &m_pInstance );
  757. if ( FAILED(hr) )
  758. {
  759. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::CGpLinkLogger: SpawnInstance failed with 0x%x" ), hr ));
  760. return;
  761. }
  762. m_bInitialized = TRUE;
  763. }
  764. //*************************************************************
  765. //
  766. // CGpLinkLogger::Log()
  767. //
  768. // Purpose: Logs an instance of scope of management
  769. //
  770. // Parameters: pwszSOMID - SOM that the Gpos are linked to
  771. // pGpLink - Gpo
  772. // dwOrder - Order of Gpo in SOM
  773. //
  774. //*************************************************************
  775. BOOL CGpLinkLogger::Log( WCHAR *pwszSOMId, BOOL bLoopback, GPLINK *pGpLink, DWORD dwSomOrder,
  776. DWORD dwLinkOrder, DWORD dwAppliedOrder )
  777. {
  778. if ( !m_bInitialized ) {
  779. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::Log: Failed to initialize." )));
  780. return FALSE;
  781. }
  782. VARIANT var;
  783. HRESULT hr;
  784. var.vt = VT_I4;
  785. var.lVal = dwSomOrder;
  786. hr = m_pInstance->Put( m_xbstrOrder, 0, &var, 0 );
  787. if ( FAILED(hr) ) {
  788. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::Log: Put failed with 0x%x" ), hr ));
  789. return FALSE;
  790. }
  791. var.vt = VT_I4;
  792. var.lVal = dwLinkOrder;
  793. hr = m_pInstance->Put( m_xbstrLinkOrder, 0, &var, 0 );
  794. if ( FAILED(hr) ) {
  795. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::Log: Put failed with 0x%x" ), hr ));
  796. return FALSE;
  797. }
  798. var.vt = VT_I4;
  799. var.lVal = dwAppliedOrder;
  800. hr = m_pInstance->Put( m_xbstrAppliedOrder, 0, &var, 0 );
  801. if ( FAILED(hr) ) {
  802. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::Log: Put failed with 0x%x" ), hr ));
  803. return FALSE;
  804. }
  805. var.vt = VT_BOOL;
  806. var.boolVal = pGpLink->bEnabled ? VARIANT_TRUE : VARIANT_FALSE;
  807. hr = m_pInstance->Put( m_xbstrEnabled, 0, &var, 0 );
  808. if ( FAILED(hr) ) {
  809. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::Log: Put failed with 0x%x" ), hr ));
  810. return FALSE;
  811. }
  812. var.boolVal = pGpLink->bNoOverride ? VARIANT_TRUE : VARIANT_FALSE;
  813. hr = m_pInstance->Put( m_xbstrEnforced, 0, &var, 0 );
  814. if ( FAILED(hr) ) {
  815. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::Log: Put failed with 0x%x" ), hr ));
  816. return FALSE;
  817. }
  818. XPtrLF<WCHAR> szUnquote = DsUnquoteDN( pwszSOMId, TRUE );
  819. WCHAR wszSOMRef[] = L"RSOP_SOM.id=\"%ws\",reason=%s";
  820. XPtrST<WCHAR> xwszSOMValue;
  821. if (szUnquote)
  822. {
  823. if ( !PrintToString( xwszSOMValue, wszSOMRef, szUnquote, bLoopback ? L"2" : L"1", 0 ) )
  824. return FALSE;
  825. }
  826. else
  827. {
  828. if ( !PrintToString( xwszSOMValue, wszSOMRef, pwszSOMId, bLoopback ? L"2" : L"1", 0 ) )
  829. return FALSE;
  830. }
  831. XBStr xbstrSOMValue( xwszSOMValue );
  832. if ( !xbstrSOMValue ) {
  833. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::Log: Failed to allocate memory" )));
  834. return FALSE;
  835. }
  836. var.vt = VT_BSTR;
  837. var.bstrVal = xbstrSOMValue;
  838. hr = m_pInstance->Put( m_xbstrSOM, 0, &var, 0 );
  839. if ( FAILED(hr) ) {
  840. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::Log: Put failed with 0x%x" ), hr ));
  841. return FALSE;
  842. }
  843. WCHAR wszGPORef[] = L"RSOP_GPO.id=\"%ws\"";
  844. XPtrST<WCHAR> xwszGPOValue;
  845. if ( !PrintToString( xwszGPOValue, wszGPORef, pGpLink->pwszGPO, 0, 0 ) )
  846. return FALSE;
  847. XBStr xbstrGPOValue( xwszGPOValue );
  848. if ( !xbstrGPOValue ) {
  849. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::Log: Failed to allocate memory" )));
  850. return FALSE;
  851. }
  852. var.bstrVal = xbstrGPOValue;
  853. hr = m_pInstance->Put( m_xbstrGPO, 0, &var, 0 );
  854. if ( FAILED(hr) ) {
  855. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::Log: Put failed with 0x%x" ), hr ));
  856. return FALSE;
  857. }
  858. hr = m_pWbemServices->PutInstance( m_pInstance, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL );
  859. if ( FAILED(hr) ) {
  860. DebugMsg((DM_WARNING, TEXT("CGpLinkLogger::Log: PutInstance failed with 0x%x" ), hr ));
  861. return FALSE;
  862. }
  863. return TRUE;
  864. }
  865. //*************************************************************
  866. //
  867. // StripPrefix()
  868. //
  869. // Purpose: Strips out prefix to get canonical path to Gpo
  870. //
  871. // Parameters: lpGPOInfo - Gpo Info
  872. // pWbemServices - Wbem services
  873. //
  874. // Returns: Pointer to suffix
  875. //
  876. //*************************************************************
  877. WCHAR *StripPrefix( WCHAR *pwszPath )
  878. {
  879. WCHAR wszMachPrefix[] = TEXT("LDAP://CN=Machine,");
  880. INT iMachPrefixLen = lstrlen( wszMachPrefix );
  881. WCHAR wszUserPrefix[] = TEXT("LDAP://CN=User,");
  882. INT iUserPrefixLen = lstrlen( wszUserPrefix );
  883. WCHAR *pwszPathSuffix;
  884. //
  885. // Strip out prefix to get the canonical path to Gpo
  886. //
  887. if ( CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE,
  888. pwszPath, iUserPrefixLen, wszUserPrefix, iUserPrefixLen ) == CSTR_EQUAL ) {
  889. pwszPathSuffix = pwszPath + iUserPrefixLen;
  890. } else if ( CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE,
  891. pwszPath, iMachPrefixLen, wszMachPrefix, iMachPrefixLen ) == CSTR_EQUAL ) {
  892. pwszPathSuffix = pwszPath + iMachPrefixLen;
  893. } else
  894. pwszPathSuffix = pwszPath;
  895. return pwszPathSuffix;
  896. }
  897. //*************************************************************
  898. //
  899. // StripLinkPrefix()
  900. //
  901. // Purpose: Strips out prefix to get canonical path to DS
  902. // object
  903. //
  904. // Parameters: pwszPath - path to strip
  905. //
  906. // Returns: Pointer to suffix
  907. //
  908. //*************************************************************
  909. WCHAR *StripLinkPrefix( WCHAR *pwszPath )
  910. {
  911. WCHAR wszPrefix[] = TEXT("LDAP://");
  912. INT iPrefixLen = lstrlen( wszPrefix );
  913. WCHAR *pwszPathSuffix;
  914. //
  915. // Strip out prefix to get the canonical path to Som
  916. //
  917. if ( wcslen(pwszPath) <= (DWORD) iPrefixLen ) {
  918. return pwszPath;
  919. }
  920. if ( CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE,
  921. pwszPath, iPrefixLen, wszPrefix, iPrefixLen ) == CSTR_EQUAL ) {
  922. pwszPathSuffix = pwszPath + iPrefixLen;
  923. } else
  924. pwszPathSuffix = pwszPath;
  925. return pwszPathSuffix;
  926. }
  927. //*************************************************************
  928. //
  929. // CRegistryLogger::CRegistryLogger()
  930. //
  931. // Purpose: Constructor
  932. //
  933. // Parameters: dwFlags - Flags
  934. // pWbemServices - Wbem services
  935. //
  936. //*************************************************************
  937. CRegistryLogger::CRegistryLogger( DWORD dwFlags, IWbemServices *pWbemServices )
  938. : m_bInitialized(FALSE),
  939. m_dwFlags(dwFlags),
  940. m_pWbemServices(pWbemServices)
  941. {
  942. m_xbstrId = L"id";
  943. if ( !m_xbstrId )
  944. return;
  945. m_xbstrName = L"name";
  946. if ( !m_xbstrName )
  947. return;
  948. m_xbstrGPO = L"GPOID";
  949. if ( !m_xbstrGPO )
  950. return;
  951. m_xbstrSOM = L"SOMID";
  952. if ( !m_xbstrSOM )
  953. return;
  954. m_xbstrPrecedence = L"precedence";
  955. if ( !m_xbstrPrecedence )
  956. return;
  957. m_xbstrKey = L"registryKey";
  958. if ( !m_xbstrKey )
  959. return;
  960. m_xbstrValueName = L"valueName";
  961. if ( !m_xbstrValueName )
  962. return;
  963. m_xbstrDeleted = L"deleted";
  964. if ( !m_xbstrDeleted )
  965. return;
  966. m_xbstrValueType = L"valueType";
  967. if ( !m_xbstrValueType )
  968. return;
  969. m_xbstrValue = L"value";
  970. if ( !m_xbstrValue )
  971. return;
  972. m_xbstrCommand = L"command";
  973. if ( !m_xbstrCommand )
  974. return;
  975. m_xbstrClass = L"RSOP_RegistryPolicySetting";
  976. if ( !m_xbstrClass )
  977. return;
  978. HRESULT hr = m_pWbemServices->GetObject( m_xbstrClass,
  979. 0L,
  980. NULL,
  981. &m_xClass,
  982. NULL );
  983. if ( FAILED(hr) ) {
  984. DebugMsg((DM_WARNING, TEXT("CGpLinkListLogger::CGpLinkListLogger: GetObject failed with 0x%x" ), hr ));
  985. return;
  986. }
  987. m_bInitialized = TRUE;
  988. }
  989. //*************************************************************
  990. //
  991. // CRegistryLogger::Log()
  992. //
  993. // Purpose: Logs an instance of registry policy object
  994. //
  995. // Parameters: pwszKeyName - Registry key name
  996. // pwszValueName - Value name
  997. // pDataEntry - Data entry
  998. // dwOrder - Precedence order
  999. //
  1000. //*************************************************************
  1001. BOOL CRegistryLogger::Log( WCHAR *pwszKeyName, WCHAR *pwszValueName,
  1002. REGDATAENTRY *pDataEntry, DWORD dwOrder )
  1003. {
  1004. if ( !m_bInitialized ) {
  1005. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Failed to initialize." )));
  1006. return FALSE;
  1007. }
  1008. IWbemClassObject *pInstance = NULL;
  1009. HRESULT hr = m_xClass->SpawnInstance( 0, &pInstance );
  1010. if ( FAILED(hr) ) {
  1011. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: SpawnInstance failed with 0x%x" ), hr ));
  1012. return FALSE;
  1013. }
  1014. XInterface<IWbemClassObject> xInstance( pInstance );
  1015. VARIANT var;
  1016. var.vt = VT_I4;
  1017. var.lVal = dwOrder;
  1018. hr = pInstance->Put( m_xbstrPrecedence, 0, &var, 0 );
  1019. if ( FAILED(hr) ) {
  1020. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Put failed with 0x%x" ), hr ));
  1021. return FALSE;
  1022. }
  1023. if ( pDataEntry->bDeleted ) {
  1024. var.vt = VT_BOOL;
  1025. var.boolVal = VARIANT_TRUE;
  1026. hr = pInstance->Put( m_xbstrDeleted, 0, &var, 0 );
  1027. if ( FAILED(hr) ) {
  1028. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Put failed with 0x%x" ), hr ));
  1029. return FALSE;
  1030. }
  1031. }
  1032. XBStr xName( pwszValueName );
  1033. if ( !xName ) {
  1034. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Failed to allocate memory" )));
  1035. return FALSE;
  1036. }
  1037. var.vt = VT_BSTR;
  1038. var.bstrVal = xName;
  1039. hr = pInstance->Put( m_xbstrName, 0, &var, 0 );
  1040. if ( FAILED(hr) ) {
  1041. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Put failed with 0x%x" ), hr ));
  1042. return FALSE;
  1043. }
  1044. hr = pInstance->Put( m_xbstrValueName, 0, &var, 0 );
  1045. if ( FAILED(hr) ) {
  1046. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Put failed with 0x%x" ), hr ));
  1047. return FALSE;
  1048. }
  1049. XBStr xKey( pwszKeyName );
  1050. if ( !xKey ) {
  1051. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Failed to allocate memory" )));
  1052. return FALSE;
  1053. }
  1054. var.bstrVal = xKey;
  1055. hr = pInstance->Put( m_xbstrKey, 0, &var, 0 );
  1056. if ( FAILED(hr) ) {
  1057. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Put failed with 0x%x" ), hr ));
  1058. return FALSE;
  1059. }
  1060. WCHAR *pwszPath = StripPrefix( pDataEntry->pwszGPO );
  1061. XBStr xGPO( pwszPath );
  1062. if ( !xGPO ) {
  1063. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Failed to allocate memory" )));
  1064. return FALSE;
  1065. }
  1066. var.bstrVal = xGPO;
  1067. hr = pInstance->Put( m_xbstrGPO, 0, &var, 0 );
  1068. if ( FAILED(hr) ) {
  1069. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Put failed with 0x%x" ), hr ));
  1070. return FALSE;
  1071. }
  1072. WCHAR *pwszSomPath = StripLinkPrefix( pDataEntry->pwszSOM );
  1073. XBStr xSOM( pwszSomPath );
  1074. if ( !xSOM ) {
  1075. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Failed to allocate memory" )));
  1076. return FALSE;
  1077. }
  1078. var.bstrVal = xSOM;
  1079. hr = pInstance->Put( m_xbstrSOM, 0, &var, 0 );
  1080. if ( FAILED(hr) ) {
  1081. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Put failed with 0x%x" ), hr ));
  1082. return FALSE;
  1083. }
  1084. XBStr xCommand( pDataEntry->pwszCommand );
  1085. if ( !xCommand ) {
  1086. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Failed to allocate memory" )));
  1087. return FALSE;
  1088. }
  1089. var.bstrVal = xCommand;
  1090. hr = pInstance->Put( m_xbstrCommand, 0, &var, 0 );
  1091. if ( FAILED(hr) ) {
  1092. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Put failed with 0x%x" ), hr ));
  1093. return FALSE;
  1094. }
  1095. WCHAR wszId[MAX_LENGTH];
  1096. GUID guid;
  1097. OLE32_API *pOle32Api = LoadOle32Api();
  1098. if ( pOle32Api == NULL )
  1099. return FALSE;
  1100. hr = pOle32Api->pfnCoCreateGuid( &guid );
  1101. if ( FAILED(hr) ) {
  1102. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Failed to obtain guid" )));
  1103. return FALSE;
  1104. }
  1105. GuidToString( &guid, wszId );
  1106. XBStr xId( wszId );
  1107. if ( !xId ) {
  1108. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Failed to allocate memory" )));
  1109. return FALSE;
  1110. }
  1111. var.bstrVal = xId;
  1112. hr = pInstance->Put( m_xbstrId, 0, &var, 0 );
  1113. if ( FAILED(hr) ) {
  1114. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Put failed with 0x%x" ), hr ));
  1115. return FALSE;
  1116. }
  1117. if ( !LogBlobProperty( pInstance, m_xbstrValue,
  1118. pDataEntry->pData, pDataEntry->dwDataLen ) ) {
  1119. return FALSE;
  1120. }
  1121. var.vt = VT_I4;
  1122. var.lVal = pDataEntry->dwValueType;
  1123. hr = pInstance->Put( m_xbstrValueType, 0, &var, 0 );
  1124. if ( FAILED(hr) ) {
  1125. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: Put failed with 0x%x" ), hr ));
  1126. return FALSE;
  1127. }
  1128. hr = m_pWbemServices->PutInstance( pInstance, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL );
  1129. if ( FAILED(hr) ) {
  1130. DebugMsg((DM_WARNING, TEXT("CRegistryLogger::Log: PutInstance failed with 0x%x" ), hr ));
  1131. return FALSE;
  1132. }
  1133. return TRUE;
  1134. }
  1135. //*************************************************************
  1136. //
  1137. // LogSecurityGroups
  1138. //
  1139. // Purpose: Logs token_groups as an array of strings
  1140. //
  1141. //*************************************************************
  1142. HRESULT LogSecurityGroups( IWbemClassObject *pInstance, BSTR bstrPropName, PTOKEN_GROUPS pTokenGroups )
  1143. {
  1144. SAFEARRAYBOUND arrayBound[1];
  1145. arrayBound[0].lLbound = 0;
  1146. arrayBound[0].cElements = pTokenGroups->GroupCount;
  1147. HRESULT hr;
  1148. NTSTATUS ntStatus;
  1149. UNICODE_STRING unicodeStr;
  1150. XSafeArray xSafeArray = SafeArrayCreate( VT_BSTR, 1, arrayBound );
  1151. if ( xSafeArray == 0 )
  1152. {
  1153. DebugMsg((DM_WARNING, TEXT("LogSecurityGroups: Failed to allocate memory" )));
  1154. return HRESULT_FROM_WIN32(GetLastError());
  1155. }
  1156. for ( DWORD i = 0 ; i < (pTokenGroups->GroupCount) ; i++ )
  1157. {
  1158. //
  1159. // Convert user SID to a string.
  1160. //
  1161. ntStatus = RtlConvertSidToUnicodeString( &unicodeStr,
  1162. pTokenGroups->Groups[i].Sid,
  1163. (BOOLEAN)TRUE ); // Allocate
  1164. if ( !NT_SUCCESS(ntStatus) ) {
  1165. DebugMsg((DM_WARNING, TEXT("LogSecurityGroups: RtlConvertSidToUnicodeString failed, status = 0x%x"),
  1166. ntStatus));
  1167. return HRESULT_FROM_NT(ntStatus);
  1168. }
  1169. XBStr xbstrSid(unicodeStr.Buffer);
  1170. RtlFreeUnicodeString( &unicodeStr );
  1171. hr = SafeArrayPutElement( xSafeArray, (long *)&i, xbstrSid );
  1172. if ( FAILED( hr ) )
  1173. {
  1174. DebugMsg((DM_WARNING, TEXT("LogSecurityGroups: Failed to SafeArrayPutElement with 0x%x" ), hr ));
  1175. return hr;
  1176. }
  1177. }
  1178. VARIANT var;
  1179. var.vt = VT_ARRAY | VT_BSTR;
  1180. var.parray = xSafeArray;
  1181. hr = pInstance->Put( bstrPropName, 0, &var, 0 );
  1182. if ( FAILED(hr) )
  1183. {
  1184. DebugMsg((DM_WARNING, TEXT("LogSecurityGroups: PutInstance failed with 0x%x" ), hr ));
  1185. return hr;
  1186. }
  1187. return S_OK;
  1188. }
  1189. //*************************************************************
  1190. //
  1191. // CRegistryLogger::LogBlobProperty()
  1192. //
  1193. // Purpose: Logs an instance of registry policy object
  1194. //
  1195. // Parameters: pwszKeyName - Registry key name
  1196. // pwszValueName - Value name
  1197. // pDataEntry - Data entry
  1198. // dwOrder - Precedence order
  1199. //
  1200. //*************************************************************
  1201. BOOL LogBlobProperty( IWbemClassObject *pInstance, BSTR bstrPropName, BYTE *pbBlob, DWORD dwLen )
  1202. {
  1203. SAFEARRAYBOUND arrayBound[1];
  1204. arrayBound[0].lLbound = 0;
  1205. arrayBound[0].cElements = dwLen;
  1206. HRESULT hr;
  1207. XSafeArray xSafeArray = SafeArrayCreate( VT_UI1, 1, arrayBound );
  1208. if ( xSafeArray == 0 )
  1209. {
  1210. DebugMsg((DM_WARNING, TEXT("LogBlobProperty: Failed to allocate memory" )));
  1211. return FALSE;
  1212. }
  1213. for ( DWORD i = 0 ; i < dwLen ; i++ )
  1214. {
  1215. hr = SafeArrayPutElement( xSafeArray, (long *)&i, &pbBlob[i] );
  1216. if ( FAILED( hr ) )
  1217. {
  1218. DebugMsg((DM_WARNING, TEXT("LogBlobProperty: Failed to SafeArrayPutElement with 0x%x" ), hr ));
  1219. return FALSE;
  1220. }
  1221. }
  1222. VARIANT var;
  1223. var.vt = VT_ARRAY | VT_UI1;
  1224. var.parray = xSafeArray;
  1225. hr = pInstance->Put( bstrPropName, 0, &var, 0 );
  1226. if ( FAILED(hr) )
  1227. {
  1228. DebugMsg((DM_WARNING, TEXT("LogBlobProperty: PutInstance failed with 0x%x" ), hr ));
  1229. return FALSE;
  1230. }
  1231. return TRUE;
  1232. }
  1233. //*************************************************************
  1234. //
  1235. // CAdmFileLogger::CAdmFileLogger()
  1236. //
  1237. // Purpose: Constructor
  1238. //
  1239. // Parameters: pWbemServices - Wbem services
  1240. //
  1241. //*************************************************************
  1242. CAdmFileLogger::CAdmFileLogger( IWbemServices *pWbemServices )
  1243. : m_bInitialized(FALSE),
  1244. m_pWbemServices(pWbemServices)
  1245. {
  1246. m_xbstrName = L"name";
  1247. if ( !m_xbstrName )
  1248. return;
  1249. m_xbstrGpoId = L"GPOID";
  1250. if ( !m_xbstrGpoId )
  1251. return;
  1252. m_xbstrWriteTime = L"lastWriteTime";
  1253. if ( !m_xbstrWriteTime )
  1254. return;
  1255. m_xbstrData = L"data";
  1256. if ( !m_xbstrData )
  1257. return;
  1258. m_xbstrClass = L"RSOP_AdministrativeTemplateFile";
  1259. if ( !m_xbstrClass )
  1260. return;
  1261. HRESULT hr = m_pWbemServices->GetObject( m_xbstrClass,
  1262. 0L,
  1263. NULL,
  1264. &m_xClass,
  1265. NULL );
  1266. if ( FAILED(hr) ) {
  1267. DebugMsg((DM_WARNING, TEXT("CAdmFileLogger::CAdmFileLogger: GetObject failed with 0x%x" ), hr ));
  1268. return;
  1269. }
  1270. m_bInitialized = TRUE;
  1271. }
  1272. //*************************************************************
  1273. //
  1274. // CAdmFileLogger::Log()
  1275. //
  1276. // Purpose: Logs an instance of Adm file object
  1277. //
  1278. // Parameters: pAdmInfo - Adm file info object
  1279. //
  1280. //*************************************************************
  1281. BOOL CAdmFileLogger::Log( ADMFILEINFO *pAdmInfo )
  1282. {
  1283. if ( !m_bInitialized ) {
  1284. DebugMsg((DM_WARNING, TEXT("CAdmFileLogger::Log: Failed to initialize." )));
  1285. return FALSE;
  1286. }
  1287. DebugMsg((DM_VERBOSE, TEXT("CAdmFileLogger::Log: Logging %s" ), pAdmInfo->pwszFile));
  1288. IWbemClassObject *pInstance = NULL;
  1289. HRESULT hr = m_xClass->SpawnInstance( 0, &pInstance );
  1290. if ( FAILED(hr) ) {
  1291. DebugMsg((DM_WARNING, TEXT("CAdmFileLogger::Log: SpawnInstance failed with 0x%x" ), hr ));
  1292. return FALSE;
  1293. }
  1294. XInterface<IWbemClassObject> xInstance( pInstance );
  1295. XBStr xName( pAdmInfo->pwszFile );
  1296. if ( !xName ) {
  1297. DebugMsg((DM_WARNING, TEXT("CAdmFileLogger::Log: Failed to allocate memory" )));
  1298. return FALSE;
  1299. }
  1300. VARIANT var;
  1301. var.vt = VT_BSTR;
  1302. var.bstrVal = xName;
  1303. hr = pInstance->Put( m_xbstrName, 0, &var, 0 );
  1304. if ( FAILED(hr) ) {
  1305. DebugMsg((DM_WARNING, TEXT("CAdmFileLogger::Log: Put failed with 0x%x" ), hr ));
  1306. return FALSE;
  1307. }
  1308. WCHAR *pwszPath = StripPrefix( pAdmInfo->pwszGPO );
  1309. XBStr xGPO( pwszPath );
  1310. if ( !xGPO ) {
  1311. DebugMsg((DM_WARNING, TEXT("CAdmFileLogger::Log: Failed to allocate memory" )));
  1312. return FALSE;
  1313. }
  1314. var.bstrVal = xGPO;
  1315. hr = pInstance->Put( m_xbstrGpoId, 0, &var, 0 );
  1316. if ( FAILED(hr) ) {
  1317. DebugMsg((DM_WARNING, TEXT("CAdmFileLogger::Log: Put failed with 0x%x" ), hr ));
  1318. return FALSE;
  1319. }
  1320. SYSTEMTIME sysTime;
  1321. if ( !FileTimeToSystemTime( &pAdmInfo->ftWrite, &sysTime ) ) {
  1322. DebugMsg((DM_WARNING, TEXT("CAdmPolicyLogger::Log: FileTimeToSystemTime failed with 0x%x" ), GetLastError() ));
  1323. return FALSE;
  1324. }
  1325. if ( !LogTimeProperty( pInstance, m_xbstrWriteTime, &sysTime ) ) {
  1326. return FALSE;
  1327. }
  1328. hr = m_pWbemServices->PutInstance( pInstance, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL );
  1329. if ( FAILED(hr) ) {
  1330. DebugMsg((DM_WARNING, TEXT("CAdmPolicyLogger::Log: PutInstance failed with 0x%x" ), hr ));
  1331. return FALSE;
  1332. }
  1333. return TRUE;
  1334. }
  1335. //*************************************************************
  1336. //
  1337. // LogTimeProperty()
  1338. //
  1339. // Purpose: Logs an instance of a datetime property
  1340. //
  1341. // Parameters: pInstance - Instance pointer
  1342. // pwszPropName - Property name
  1343. // pSysTime - System time
  1344. //
  1345. //*************************************************************
  1346. BOOL LogTimeProperty( IWbemClassObject *pInstance, BSTR bstrPropName, SYSTEMTIME *pSysTime )
  1347. {
  1348. if(!pInstance || !bstrPropName || !pSysTime)
  1349. {
  1350. DebugMsg((DM_WARNING, TEXT("LogTimeProperty: Function called with invalid parameters.")));
  1351. return FALSE;
  1352. }
  1353. XBStr xbstrTime;
  1354. HRESULT hr = SystemTimeToWbemTime(*pSysTime, xbstrTime);
  1355. if(FAILED(hr) || !xbstrTime)
  1356. {
  1357. DebugMsg((DM_WARNING, TEXT("LogTimeProperty: Call to SystemTimeToWbemTime failed. hr=0x%08X"),hr));
  1358. return FALSE;
  1359. }
  1360. VARIANT var;
  1361. var.vt = VT_BSTR;
  1362. var.bstrVal = xbstrTime;
  1363. hr = pInstance->Put( bstrPropName, 0, &var, 0 );
  1364. if ( FAILED(hr) ) {
  1365. DebugMsg((DM_WARNING, TEXT("LogTimeProperty: Put failed with 0x%x" ), hr ));
  1366. return FALSE;
  1367. }
  1368. return TRUE;
  1369. }
  1370. //*************************************************************
  1371. //
  1372. // CExtSessionLogger::CExtSessionLogger()
  1373. //
  1374. // Purpose: Constructor
  1375. //
  1376. // Parameters: pWbemServices - Wbem services
  1377. //
  1378. //*************************************************************
  1379. CExtSessionLogger::CExtSessionLogger( IWbemServices *pWbemServices )
  1380. : m_bInitialized(FALSE),
  1381. m_pWbemServices(pWbemServices)
  1382. {
  1383. m_xbstrExtGuid = L"extensionGuid";
  1384. if ( !m_xbstrExtGuid )
  1385. return;
  1386. m_xbstrDisplayName = L"displayName";
  1387. if ( !m_xbstrDisplayName )
  1388. return;
  1389. m_xbstrPolicyBeginTime = L"beginTime";
  1390. if ( !m_xbstrPolicyBeginTime )
  1391. return;
  1392. m_xbstrPolicyEndTime = L"endTime";
  1393. if ( !m_xbstrPolicyEndTime )
  1394. return;
  1395. m_xbstrStatus = L"loggingStatus";
  1396. if ( !m_xbstrStatus )
  1397. return;
  1398. m_xbstrError = L"error";
  1399. if ( !m_xbstrError )
  1400. return;
  1401. m_xbstrClass = L"RSOP_ExtensionStatus";
  1402. if ( !m_xbstrClass )
  1403. return;
  1404. HRESULT hr = m_pWbemServices->GetObject( m_xbstrClass,
  1405. 0L,
  1406. NULL,
  1407. &m_xClass,
  1408. NULL );
  1409. if ( FAILED(hr) ) {
  1410. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::CExtSessionLogger: GetObject failed with 0x%x" ), hr ));
  1411. return;
  1412. }
  1413. if ( 0 == LoadString (g_hDllInstance, IDS_GPCORE_NAME, m_szGPCoreNameBuf, ARRAYSIZE(m_szGPCoreNameBuf) - 1) ) {
  1414. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::CExtSessionLogger: LoadString for GP Core Name failed with 0x%x" ), GetLastError() ));
  1415. return;
  1416. }
  1417. m_bInitialized = TRUE;
  1418. }
  1419. //*************************************************************
  1420. //
  1421. // CExtSessionLogger::Log()
  1422. //
  1423. // Purpose: Logs an instance of ExtensionSessionStatus
  1424. //
  1425. //*************************************************************
  1426. #define EXT_INSTPATH_FMT L"RSOP_ExtensionStatus.extensionGuid=\"%s\""
  1427. BOOL CExtSessionLogger::Log( LPGPEXT lpExt, BOOL bSupported )
  1428. {
  1429. LPTSTR lpGuid=NULL, lpName=NULL;
  1430. HRESULT hr;
  1431. if ( !m_bInitialized ) {
  1432. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Failed to initialize." )));
  1433. return FALSE;
  1434. }
  1435. lpGuid = lpExt ? lpExt->lpKeyName : GPCORE_GUID;
  1436. lpName = lpExt ? lpExt->lpDisplayName : m_szGPCoreNameBuf;
  1437. XInterface<IWbemClassObject>xInstance = NULL;
  1438. XBStr xDisplayName;
  1439. hr = m_xClass->SpawnInstance( 0, &xInstance );
  1440. if ( FAILED(hr) ) {
  1441. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: SpawnInstance failed with 0x%x" ), hr ));
  1442. return FALSE;
  1443. }
  1444. xDisplayName = lpName;
  1445. if ( !xDisplayName ) {
  1446. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Failed to allocate memory" )));
  1447. return FALSE;
  1448. }
  1449. XBStr xGuid ( lpGuid );
  1450. if ( !xGuid ) {
  1451. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Failed to allocate memory" )));
  1452. return FALSE;
  1453. }
  1454. VARIANT var;
  1455. var.vt = VT_BSTR;
  1456. var.bstrVal = xGuid;
  1457. hr = xInstance->Put( m_xbstrExtGuid, 0, &var, 0 );
  1458. if ( FAILED(hr) ) {
  1459. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Put failed with 0x%x" ), hr ));
  1460. return FALSE;
  1461. }
  1462. var.bstrVal = xDisplayName;
  1463. hr = xInstance->Put( m_xbstrDisplayName, 0, &var, 0 );
  1464. if ( FAILED(hr) ) {
  1465. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Put failed with 0x%x" ), hr ));
  1466. return FALSE;
  1467. }
  1468. SYSTEMTIME sysTime;
  1469. GetSystemTime(&sysTime);
  1470. if ( !LogTimeProperty( xInstance, m_xbstrPolicyBeginTime, &sysTime ) )
  1471. {
  1472. return FALSE;
  1473. }
  1474. var.vt = VT_I4;
  1475. var.lVal = bSupported ? 2 : 3;
  1476. hr = xInstance->Put( m_xbstrStatus, 0, &var, 0 );
  1477. if ( FAILED(hr) ) {
  1478. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Put failed with 0x%x" ), hr ));
  1479. return FALSE;
  1480. }
  1481. var.vt = VT_I4;
  1482. var.lVal = 0;
  1483. hr = xInstance->Put( m_xbstrError, 0, &var, 0 );
  1484. if ( FAILED(hr) ) {
  1485. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Put failed with 0x%x" ), hr ));
  1486. return FALSE;
  1487. }
  1488. hr = m_pWbemServices->PutInstance( xInstance, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL );
  1489. if ( FAILED(hr) ) {
  1490. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: PutInstance failed with 0x%x" ), hr ));
  1491. return FALSE;
  1492. }
  1493. return TRUE;
  1494. }
  1495. //*************************************************************
  1496. //
  1497. // CExtSessionLogger::Update()
  1498. //
  1499. // Purpose: Logs an instance of ExtensionSessionStatus
  1500. //
  1501. //*************************************************************
  1502. BOOL CExtSessionLogger::Update( LPTSTR lpKeyName, BOOL bLoggingIncomplete, DWORD dwErr )
  1503. {
  1504. LPTSTR lpGuid=NULL;
  1505. HRESULT hr;
  1506. if ( !m_bInitialized ) {
  1507. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Failed to initialize." )));
  1508. return FALSE;
  1509. }
  1510. lpGuid = lpKeyName ? lpKeyName : GPCORE_GUID;
  1511. XInterface<IWbemClassObject>xInstance = NULL;
  1512. //
  1513. // We should have an instance
  1514. //
  1515. XPtrLF<TCHAR> xszInstPath = (LPTSTR) LocalAlloc(LPTR, sizeof(WCHAR)*
  1516. (lstrlen(EXT_INSTPATH_FMT)+lstrlen(lpGuid)+10));
  1517. if (!xszInstPath) {
  1518. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Not enough memory." )));
  1519. return FALSE;
  1520. }
  1521. wsprintf(xszInstPath, EXT_INSTPATH_FMT, lpGuid);
  1522. XBStr xbstrInstancePath = xszInstPath;
  1523. if(!xbstrInstancePath)
  1524. {
  1525. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Failed to allocate memory.")));
  1526. return FALSE;
  1527. }
  1528. hr = m_pWbemServices->GetObject(xbstrInstancePath, 0, NULL, &xInstance, NULL);
  1529. if (FAILED(hr)) {
  1530. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Didn't find an instance of the extension object when trying to set the dirty flag.")));
  1531. return FALSE;
  1532. }
  1533. VARIANT var;
  1534. VariantInit(&var);
  1535. XVariant xVar(&var);
  1536. //
  1537. // Display name
  1538. //
  1539. hr = xInstance->Get(m_xbstrDisplayName, 0, &var, NULL, NULL);
  1540. if(FAILED(hr)) {
  1541. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: Get failed for displayname. hr=0x%08X."), hr));
  1542. return FALSE;
  1543. }
  1544. hr = xInstance->Put(m_xbstrDisplayName, 0, &var, NULL);
  1545. if(FAILED(hr)) {
  1546. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: Put failed for displayname. hr=0x%08X."), hr));
  1547. return FALSE;
  1548. }
  1549. //
  1550. // Start time
  1551. //
  1552. hr = xInstance->Get(m_xbstrPolicyBeginTime, 0, &var, NULL, NULL);
  1553. if(FAILED(hr)) {
  1554. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: Get failed for displayname. hr=0x%08X."), hr));
  1555. return FALSE;
  1556. }
  1557. hr = xInstance->Put(m_xbstrPolicyBeginTime, 0, &var, NULL);
  1558. if(FAILED(hr)) {
  1559. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: Put failed for displayname. hr=0x%08X."), hr));
  1560. return FALSE;
  1561. }
  1562. //
  1563. // end time
  1564. //
  1565. SYSTEMTIME sysTime;
  1566. GetSystemTime(&sysTime);
  1567. if (!LogTimeProperty( xInstance, m_xbstrPolicyEndTime, &sysTime) ) {
  1568. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: Get failed for dispayname. hr=0x%08X."), hr));
  1569. return FALSE;
  1570. }
  1571. //
  1572. // Dirty flag
  1573. //
  1574. if (bLoggingIncomplete) {
  1575. hr = xInstance->Get(m_xbstrStatus, 0, &var, NULL, NULL);
  1576. if(FAILED(hr)) {
  1577. DebugMsg((DM_VERBOSE, TEXT("CSessionLogger::Log: Get failed for loggingstatus. hr=0x%08X."), hr));
  1578. return FALSE;
  1579. }
  1580. }
  1581. else {
  1582. var.vt = VT_I4;
  1583. var.lVal = 1; // logging completed successfully
  1584. }
  1585. hr = xInstance->Put( m_xbstrStatus, 0, &var, 0 );
  1586. if ( FAILED(hr) ) {
  1587. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Put failed with 0x%x" ), hr ));
  1588. return FALSE;
  1589. }
  1590. //
  1591. // Error code
  1592. //
  1593. var.vt = VT_I4;
  1594. var.lVal = dwErr;
  1595. hr = xInstance->Put( m_xbstrError, 0, &var, 0 );
  1596. if ( FAILED(hr) ) {
  1597. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Put failed with 0x%x" ), hr ));
  1598. return FALSE;
  1599. }
  1600. hr = m_pWbemServices->PutInstance( xInstance, WBEM_FLAG_UPDATE_ONLY, NULL, NULL );
  1601. if ( FAILED(hr) ) {
  1602. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: PutInstance failed with 0x%x" ), hr ));
  1603. return FALSE;
  1604. }
  1605. return TRUE;
  1606. }
  1607. //*************************************************************
  1608. //
  1609. // CExtSessionLogger::Update()
  1610. //
  1611. // Purpose: Logs an instance of ExtensionSessionStatus
  1612. //
  1613. //*************************************************************
  1614. BOOL CExtSessionLogger::Set( LPGPEXT lpExt, BOOL bSupported, LPRSOPEXTSTATUS lpRsopExtStatus )
  1615. {
  1616. LPTSTR lpGuid=NULL, lpName=NULL;
  1617. HRESULT hr;
  1618. if ( !m_bInitialized ) {
  1619. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Failed to initialize." )));
  1620. return FALSE;
  1621. }
  1622. lpGuid = lpExt ? lpExt->lpKeyName : GPCORE_GUID;
  1623. lpName = lpExt ? lpExt->lpDisplayName : m_szGPCoreNameBuf;
  1624. XInterface<IWbemClassObject>xInstance = NULL;
  1625. XBStr xDisplayName;
  1626. hr = m_xClass->SpawnInstance( 0, &xInstance );
  1627. if ( FAILED(hr) ) {
  1628. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: SpawnInstance failed with 0x%x" ), hr ));
  1629. return FALSE;
  1630. }
  1631. xDisplayName = lpName;
  1632. if ( !xDisplayName ) {
  1633. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Failed to allocate memory" )));
  1634. return FALSE;
  1635. }
  1636. XBStr xGuid ( lpGuid );
  1637. if ( !xGuid ) {
  1638. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Failed to allocate memory" )));
  1639. return FALSE;
  1640. }
  1641. VARIANT var;
  1642. var.vt = VT_BSTR;
  1643. var.bstrVal = xGuid;
  1644. hr = xInstance->Put( m_xbstrExtGuid, 0, &var, 0 );
  1645. if ( FAILED(hr) ) {
  1646. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Put failed with 0x%x" ), hr ));
  1647. return FALSE;
  1648. }
  1649. var.bstrVal = xDisplayName;
  1650. hr = xInstance->Put( m_xbstrDisplayName, 0, &var, 0 );
  1651. if ( FAILED(hr) ) {
  1652. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Put failed with 0x%x" ), hr ));
  1653. return FALSE;
  1654. }
  1655. SYSTEMTIME sysTime;
  1656. if (!FileTimeToSystemTime(&(lpRsopExtStatus->ftStartTime), &sysTime)) {
  1657. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: FileTimeToSystemTime failed with 0x%x" ), GetLastError() ));
  1658. return FALSE;
  1659. }
  1660. if ( !LogTimeProperty( xInstance, m_xbstrPolicyBeginTime, &sysTime ) )
  1661. {
  1662. return FALSE;
  1663. }
  1664. if (!FileTimeToSystemTime(&(lpRsopExtStatus->ftEndTime), &sysTime)) {
  1665. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: FileTimeToSystemTime failed with 0x%x" ), GetLastError() ));
  1666. return FALSE;
  1667. }
  1668. if ( !LogTimeProperty( xInstance, m_xbstrPolicyEndTime, &sysTime ) )
  1669. {
  1670. return FALSE;
  1671. }
  1672. var.vt = VT_I4;
  1673. var.lVal = (!bSupported) ? 3 : (FAILED(lpRsopExtStatus->dwLoggingStatus) ? 2 : 1 );
  1674. hr = xInstance->Put( m_xbstrStatus, 0, &var, 0 );
  1675. if ( FAILED(hr) ) {
  1676. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Put failed with 0x%x" ), hr ));
  1677. return FALSE;
  1678. }
  1679. var.vt = VT_I4;
  1680. var.lVal = lpRsopExtStatus->dwStatus;
  1681. hr = xInstance->Put( m_xbstrError, 0, &var, 0 );
  1682. if ( FAILED(hr) ) {
  1683. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: Put failed with 0x%x" ), hr ));
  1684. return FALSE;
  1685. }
  1686. hr = m_pWbemServices->PutInstance( xInstance, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL );
  1687. if ( FAILED(hr) ) {
  1688. DebugMsg((DM_WARNING, TEXT("CExtSessionLogger::Log: PutInstance failed with 0x%x" ), hr ));
  1689. return FALSE;
  1690. }
  1691. return TRUE;
  1692. }
  1693. //*************************************************************
  1694. //
  1695. // GetRsopSchemaVersionNumber
  1696. //
  1697. // Purpose: Gets the Rsop schema version number from the namespace that we are
  1698. // connected to
  1699. //
  1700. // returns S_OK on success, failur code o/w
  1701. //*************************************************************
  1702. HRESULT GetRsopSchemaVersionNumber(IWbemServices *pWbemServices, DWORD *dwVersionNumber)
  1703. {
  1704. XInterface<IWbemClassObject>xpInstance = NULL;
  1705. VARIANT var;
  1706. XBStr xbstrInstancePath;
  1707. XBStr xbstrVersion;
  1708. HRESULT hr;
  1709. *dwVersionNumber = 0;
  1710. xbstrVersion = L"version";
  1711. if ( !xbstrVersion ) {
  1712. DebugMsg((DM_WARNING, TEXT("CompileMof: Failed to allocate memory.")));
  1713. return HRESULT_FROM_WIN32(GetLastError());
  1714. }
  1715. xbstrInstancePath = L"RSOP_Session.id=\"Session1\"";
  1716. if(!xbstrInstancePath)
  1717. {
  1718. DebugMsg((DM_WARNING, TEXT("CompileMof: Failed to allocate memory.")));
  1719. return HRESULT_FROM_WIN32(GetLastError());
  1720. }
  1721. hr = pWbemServices->GetObject(xbstrInstancePath, 0, NULL, &xpInstance, NULL);
  1722. if(SUCCEEDED(hr)) {
  1723. VariantInit(&var);
  1724. XVariant xVar(&var);
  1725. hr = xpInstance->Get(xbstrVersion, 0, &var, NULL, NULL);
  1726. if((SUCCEEDED(hr)) && ( var.vt != VT_NULL ))
  1727. *dwVersionNumber = var.lVal;
  1728. }
  1729. return S_OK;
  1730. }