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.

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