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.

3122 lines
81 KiB

  1. //*************************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation 1998
  4. // All rights reserved
  5. //
  6. // manapp.cxx
  7. //
  8. //*************************************************************
  9. #include "appmgext.hxx"
  10. #pragma warning(disable:4355)
  11. CManagedAppProcessor::CManagedAppProcessor(
  12. DWORD dwFlags,
  13. HANDLE hUserToken,
  14. HKEY hKeyRoot,
  15. PFNSTATUSMESSAGECALLBACK pfnStatusCallback,
  16. BOOL bIncludeLegacy,
  17. BOOL bRegularPolicyRun,
  18. CRsopAppContext* pRsopContext,
  19. DWORD & Status
  20. ) : _Apps( this, &_RsopContext ), _LocalScripts( this ), _pfnStatusCallback(pfnStatusCallback)
  21. {
  22. DWORD Size;
  23. DWORD LastArchLang;
  24. BOOL bFullPolicy;
  25. HRESULT hr;
  26. _bUser = ! (dwFlags & GPO_INFO_FLAG_MACHINE);
  27. _bNoChanges = (dwFlags & GPO_INFO_FLAG_NOCHANGES) && ! (gDebugLevel & DL_APPLY) && ! ( dwFlags & GPO_INFO_FLAG_LOGRSOP_TRANSITION );
  28. _bAsync = (dwFlags & GPO_INFO_FLAG_ASYNC_FOREGROUND) && ! (gDebugLevel & DL_APPLY) && ! ( dwFlags & GPO_INFO_FLAG_LOGRSOP_TRANSITION );
  29. _bARPList = FALSE;
  30. _hkRoot = 0;
  31. _hkPolicy = 0;
  32. _hkAppmgmt = 0;
  33. _hUserToken = 0;
  34. _NewUsn = 0;
  35. _ArchLang = 0;
  36. _pwszLocalPath = 0;
  37. _bIncludeLegacy = bIncludeLegacy;
  38. _bDeleteGPOs = FALSE;
  39. _bRegularPolicyRun = bRegularPolicyRun;
  40. _ErrorReason = 0;
  41. //
  42. // In the case of gpo removal, we cannot apply this during an async refresh
  43. //
  44. if ( _bAsync && ! bRegularPolicyRun )
  45. {
  46. _ErrorReason = ERRORREASON_PROCESS;
  47. DebugMsg((DM_VERBOSE, IDS_ABORT_OPERATION));
  48. Status = ERROR_SYNC_FOREGROUND_REFRESH_REQUIRED;
  49. return;
  50. }
  51. if ( CRsopAppContext::POLICY_REFRESH == pRsopContext->GetContext() )
  52. {
  53. if ( _bAsync )
  54. {
  55. DebugMsg((DM_VERBOSE, IDS_ASYNC_REFRESH));
  56. }
  57. else
  58. {
  59. DebugMsg((DM_VERBOSE, IDS_SYNC_REFRESH));
  60. }
  61. }
  62. hr = GetRsopContext()->MoveAppContextState( pRsopContext );
  63. if ( FAILED( hr ) )
  64. {
  65. Status = GetWin32ErrFromHResult( hr );
  66. goto CManagedAppProcessor__CManagedAppProcessor_Exit;
  67. }
  68. if ( _bUser && ! GetRsopContext()->IsPlanningModeEnabled() )
  69. {
  70. if ( ! DuplicateToken( hUserToken, SecurityImpersonation, &_hUserToken ) )
  71. {
  72. goto CManagedAppProcessor__CManagedAppProcessor_Exit;
  73. }
  74. }
  75. //
  76. // Act as if there are changes when planning mode is enabled
  77. //
  78. if ( GetRsopContext()->IsPlanningModeEnabled() )
  79. {
  80. _bNoChanges = FALSE;
  81. }
  82. if ( ! GetRsopContext()->IsPlanningModeEnabled() )
  83. {
  84. Status = RegOpenKeyEx(
  85. hKeyRoot,
  86. NULL,
  87. 0,
  88. KEY_READ | KEY_WRITE,
  89. &_hkRoot );
  90. if ( Status != ERROR_SUCCESS )
  91. goto CManagedAppProcessor__CManagedAppProcessor_Exit;
  92. Status = RegCreateKeyEx(
  93. _hkRoot,
  94. POLICYKEY,
  95. 0,
  96. NULL,
  97. REG_OPTION_NON_VOLATILE,
  98. KEY_READ | KEY_WRITE,
  99. NULL,
  100. &_hkPolicy,
  101. NULL );
  102. if ( Status != ERROR_SUCCESS )
  103. goto CManagedAppProcessor__CManagedAppProcessor_Exit;
  104. Status = RegCreateKeyEx(
  105. _hkPolicy,
  106. APPMGMTSUBKEY,
  107. 0,
  108. NULL,
  109. REG_OPTION_NON_VOLATILE,
  110. KEY_READ | KEY_WRITE,
  111. NULL,
  112. &_hkAppmgmt,
  113. NULL );
  114. if ( Status != ERROR_SUCCESS )
  115. goto CManagedAppProcessor__CManagedAppProcessor_Exit;
  116. }
  117. BOOL bForcedRefresh;
  118. bForcedRefresh = FALSE;
  119. if ( bRegularPolicyRun )
  120. {
  121. SYSTEM_INFO SystemInfo;
  122. //
  123. // The service sets the FULLPOLICY value when the user does an uninstall.
  124. // This forces us to do a full policy run to pick up any new app that may
  125. // need to be applied now.
  126. //
  127. bFullPolicy = FALSE;
  128. Size = sizeof( bFullPolicy );
  129. if (!GetRsopContext()->IsPlanningModeEnabled())
  130. {
  131. (void) RegQueryValueEx(
  132. _hkAppmgmt,
  133. FULLPOLICY,
  134. NULL,
  135. NULL,
  136. (LPBYTE) &bFullPolicy,
  137. &Size );
  138. (void) RegDeleteValue( _hkAppmgmt, FULLPOLICY );
  139. if ( _bNoChanges )
  140. {
  141. bForcedRefresh = bFullPolicy;
  142. }
  143. }
  144. else
  145. {
  146. bFullPolicy = TRUE;
  147. }
  148. if ( bFullPolicy )
  149. _bNoChanges = FALSE;
  150. _ArchLang = GetSystemDefaultLangID();
  151. GetSystemInfo( &SystemInfo );
  152. _ArchLang |= (SystemInfo.wProcessorArchitecture << 16);
  153. if (!GetRsopContext()->IsPlanningModeEnabled())
  154. {
  155. Size = sizeof( LastArchLang );
  156. Status = RegQueryValueEx(
  157. _hkAppmgmt,
  158. LASTARCHLANG,
  159. NULL,
  160. NULL,
  161. (LPBYTE) &LastArchLang,
  162. &Size );
  163. if ( (ERROR_SUCCESS == Status) && _bNoChanges && (_ArchLang != LastArchLang) )
  164. {
  165. if ( _bNoChanges )
  166. {
  167. bForcedRefresh = TRUE;
  168. }
  169. _bNoChanges = FALSE;
  170. if ( Async() )
  171. {
  172. DebugMsg((DM_VERBOSE, IDS_ABORT_OPERATION));
  173. }
  174. }
  175. }
  176. }
  177. Status = GetScriptDirPath( _bUser ? _hUserToken : NULL, 0, &_pwszLocalPath );
  178. if ( ERROR_SUCCESS == Status && ! GetRsopContext()->IsPlanningModeEnabled() )
  179. Status = CreateAndSecureScriptDir();
  180. if ( (ERROR_SUCCESS == Status) && ! GetRsopContext()->IsPlanningModeEnabled() )
  181. Status = GetLocalScriptAppList( _LocalScripts );
  182. if ( Status != ERROR_SUCCESS )
  183. {
  184. DebugMsg((DM_WARNING, IDS_CREATEDIR_FAIL, Status));
  185. goto CManagedAppProcessor__CManagedAppProcessor_Exit;
  186. }
  187. if ( _bNoChanges )
  188. {
  189. if ( DetectLostApps() )
  190. {
  191. bForcedRefresh = TRUE;
  192. _bNoChanges = FALSE;
  193. }
  194. }
  195. //
  196. // Ensure that the rsop context is properly initialized, even if the
  197. // group policy engine did not give us a context but we need to log data
  198. //
  199. (void) GetRsopContext()->InitializeRsopContext(
  200. UserToken(),
  201. AppmgmtKey(),
  202. bForcedRefresh,
  203. &_bNoChanges);
  204. CManagedAppProcessor__CManagedAppProcessor_Exit:
  205. return;
  206. }
  207. #pragma warning(default:4355)
  208. CManagedAppProcessor::~CManagedAppProcessor()
  209. {
  210. if ( _hkPolicy )
  211. RegCloseKey( _hkPolicy );
  212. if ( _hkAppmgmt )
  213. RegCloseKey( _hkAppmgmt );
  214. if ( _hkRoot )
  215. RegCloseKey( _hkRoot );
  216. if ( _hUserToken )
  217. CloseHandle( _hUserToken );
  218. delete _pwszLocalPath;
  219. }
  220. BOOL
  221. CManagedAppProcessor::AddGPO(
  222. PGROUP_POLICY_OBJECT pGPOInfo
  223. )
  224. {
  225. CGPOInfo * pGPO;
  226. BOOL bStatus;
  227. //
  228. // Prevent duplicates in the list. A GPO could be linked to multiple
  229. // OUs, so only keep the last instance of a policy.
  230. //
  231. pGPO = _GPOs.Find( pGPOInfo->szGPOName );
  232. if ( pGPO )
  233. {
  234. pGPO->Remove();
  235. delete pGPO;
  236. }
  237. return _GPOs.Add( pGPOInfo );
  238. }
  239. DWORD
  240. CManagedAppProcessor::Delete()
  241. {
  242. DWORD Status;
  243. _bDeleteGPOs = TRUE;
  244. ASSERT( ! Async() );
  245. Status = GetRemovedApps();
  246. if ( Status != ERROR_SUCCESS )
  247. {
  248. _ErrorReason = ERRORREASON_LOCAL;
  249. return Status;
  250. }
  251. _CSPath.Commit(_hUserToken);
  252. Status = _Apps.ProcessPolicy();
  253. if ( Status != ERROR_SUCCESS )
  254. _ErrorReason = ERRORREASON_PROCESS;
  255. return Status;
  256. }
  257. DWORD
  258. CManagedAppProcessor::GetRemovedApps()
  259. {
  260. CAppList LocalApps( NULL );
  261. CGPOInfo * pGPOInfo;
  262. CAppInfo * pAppInfo;
  263. DWORD Status;
  264. Status = GetOrderedLocalAppList( LocalApps );
  265. if ( ERROR_SUCCESS == Status )
  266. Status = Impersonate();
  267. if ( Status != ERROR_SUCCESS )
  268. return Status;
  269. _GPOs.Reset();
  270. for ( pGPOInfo = (CGPOInfo *) _GPOs.GetCurrentItem();
  271. pGPOInfo;
  272. _GPOs.MoveNext(), pGPOInfo = (CGPOInfo *) _GPOs.GetCurrentItem() )
  273. {
  274. DebugMsg((DM_VERBOSE, IDS_REMOVE_POLICY, pGPOInfo->_pwszGPOName));
  275. LocalApps.Reset();
  276. for ( pAppInfo = (CAppInfo *) LocalApps.GetCurrentItem();
  277. pAppInfo;
  278. pAppInfo = (CAppInfo *) LocalApps.GetCurrentItem() )
  279. {
  280. //
  281. // Look for apps in the removed policy.
  282. //
  283. // Ignore apps which are not currently assigned or published from the removed
  284. // policy except for apps which have been uninstalled from machines other
  285. // than this one. This is what the second logic check is doing. In this case
  286. // we have to uninstall it at this machine as well.
  287. //
  288. if ( ! (pAppInfo->_State & (APPSTATE_ASSIGNED | APPSTATE_PUBLISHED | APPSTATE_UNINSTALLED)) ||
  289. ((pAppInfo->_State & APPSTATE_UNINSTALLED) && ! (pAppInfo->_State & APPSTATE_SCRIPT_PRESENT)) ||
  290. (lstrcmpi( pAppInfo->_pwszGPOId, pGPOInfo->_pwszGPOId ) != 0) )
  291. {
  292. LocalApps.MoveNext();
  293. continue;
  294. }
  295. //
  296. // On very rare occasion, a policy could be removed at the same time a
  297. // first time logon to a machine is made. In this case we will need
  298. // to copy scripts for uninstalled apps. We attempt to get the script
  299. // path here. This may fail for permission reasons, if the policy
  300. // is being removed, it's likely it will not be accessible for this
  301. // user/machine.
  302. //
  303. if ( (pAppInfo->_State & APPSTATE_POLICYREMOVE_UNINSTALL) &&
  304. ! (pAppInfo->_State & APPSTATE_SCRIPT_PRESENT) )
  305. {
  306. PACKAGEDISPINFO PackageInfo;
  307. HRESULT hr;
  308. hr = GetDsPackageFromGPO(
  309. pGPOInfo,
  310. &(pAppInfo->_DeploymentId),
  311. &PackageInfo);
  312. if ( S_OK == hr )
  313. {
  314. pAppInfo->_pwszGPTScriptPath = StringDuplicate( PackageInfo.pszScriptPath );
  315. ReleasePackageInfo( &PackageInfo );
  316. if ( ! pAppInfo->_pwszGPTScriptPath )
  317. {
  318. Revert();
  319. return ERROR_OUTOFMEMORY;
  320. }
  321. }
  322. }
  323. if ( pAppInfo->_State & APPSTATE_POLICYREMOVE_UNINSTALL )
  324. {
  325. pAppInfo->SetAction(
  326. ACTION_UNINSTALL,
  327. APP_ATTRIBUTE_REMOVALCAUSE_SCOPELOSS,
  328. NULL);
  329. }
  330. else
  331. {
  332. pAppInfo->SetAction(
  333. ACTION_ORPHAN,
  334. APP_ATTRIBUTE_REMOVALCAUSE_SCOPELOSS,
  335. NULL);
  336. }
  337. LocalApps.MoveNext();
  338. pAppInfo->Remove();
  339. _Apps.InsertFIFO( pAppInfo );
  340. }
  341. LocalApps.ResetEnd();
  342. }
  343. _GPOs.ResetEnd();
  344. Revert();
  345. return Status;
  346. }
  347. DWORD
  348. CManagedAppProcessor::Process()
  349. {
  350. CGPOInfo * pGPOInfo;
  351. DWORD Status;
  352. Status = Impersonate();
  353. if ( Status != ERROR_SUCCESS )
  354. return Status;
  355. for ( _GPOs.Reset(); pGPOInfo = (CGPOInfo *) _GPOs.GetCurrentItem(); _GPOs.MoveNext() )
  356. {
  357. Status = _CSPath.AddComponent( pGPOInfo->_pwszGPOPath, pGPOInfo->_pwszGPOName );
  358. if ( Status != ERROR_SUCCESS )
  359. break;
  360. }
  361. Revert();
  362. _GPOs.ResetEnd();
  363. if ( ! GetRsopContext()->IsPlanningModeEnabled() )
  364. {
  365. if ( ERROR_SUCCESS == Status )
  366. {
  367. Status = _CSPath.Commit( _hUserToken );
  368. }
  369. if ( Status != ERROR_SUCCESS )
  370. {
  371. if ( CS_E_NO_CLASSSTORE == Status )
  372. {
  373. return ERROR_SUCCESS;
  374. }
  375. else
  376. {
  377. _ErrorReason = ERRORREASON_CSPATH;
  378. return Status;
  379. }
  380. }
  381. }
  382. if ( _bNoChanges )
  383. {
  384. DebugMsg((DM_VERBOSE, IDS_NOCHANGES));
  385. }
  386. else
  387. {
  388. if ( ! GetRsopContext()->IsPlanningModeEnabled() )
  389. {
  390. LogonMsgApplying();
  391. }
  392. // Really returns an HRESULT.
  393. Status = (DWORD) GetAppsFromDirectory();
  394. if ( Status != ERROR_SUCCESS )
  395. _ErrorReason = ERRORREASON_ENUM;
  396. }
  397. if ( ERROR_SUCCESS == Status )
  398. Status = GetAppsFromLocal();
  399. if ( ERROR_SUCCESS == Status )
  400. Status = CommitPolicyList();
  401. if ( ERROR_SUCCESS == Status )
  402. Status = GetLostApps();
  403. if ( Status != ERROR_SUCCESS )
  404. _ErrorReason = ERRORREASON_LOCAL;
  405. if ( ERROR_SUCCESS == Status )
  406. Status = _Apps.ProcessPolicy();
  407. if ( Status != ERROR_SUCCESS )
  408. _ErrorReason = ERRORREASON_PROCESS;
  409. if ( ! GetRsopContext()->IsPlanningModeEnabled() )
  410. {
  411. if ( (ERROR_SUCCESS == Status) && (_ArchLang != 0) )
  412. {
  413. (void) RegSetValueEx(
  414. _hkAppmgmt,
  415. LASTARCHLANG,
  416. 0,
  417. REG_DWORD,
  418. (LPBYTE) &_ArchLang,
  419. sizeof(_ArchLang) );
  420. }
  421. if ( ! _bNoChanges )
  422. LogonMsgDefault();
  423. }
  424. //
  425. // If we are processing asynchronously and changes are detected,
  426. // we should ensure that a synchronous refresh occurs next time
  427. //
  428. if ( ( ERROR_SUCCESS == Status ) && Async() &&
  429. ! _bNoChanges )
  430. {
  431. Status = ERROR_SYNC_FOREGROUND_REFRESH_REQUIRED;
  432. }
  433. return Status;
  434. }
  435. void
  436. CManagedAppProcessor::WriteRsopLogs()
  437. {
  438. HRESULT hr;
  439. BOOL ResultantSetChanged;
  440. hr = S_OK;
  441. //
  442. // By default, the resultant set changes only if policy has changed
  443. //
  444. ResultantSetChanged = ! _bNoChanges;
  445. #if DBG
  446. DWORD DebugStatus;
  447. #endif // DBG
  448. //
  449. // If we're in diagnostic mode, make sure we reset
  450. // the diagnostic namespace if policy has changed
  451. //
  452. if (
  453. ( GetRsopContext()->IsDiagnosticModeEnabled() && ResultantSetChanged ) &&
  454. ( CRsopAppContext::POLICY_REFRESH == GetRsopContext()->GetContext() ) )
  455. {
  456. if ( ! GetRsopContext()->IsPlanningModeEnabled() && ! GetRsopContext()->ForcedRefresh() )
  457. {
  458. //
  459. // Reset the namespace
  460. //
  461. GetRsopContext()->DeleteSavedNameSpace();
  462. }
  463. }
  464. //
  465. // For ARP, ensure that no one tries to read the namespace to
  466. // which we are logging until we are finished.
  467. //
  468. if ( ARPList() )
  469. {
  470. hr = GetRsopContext()->GetExclusiveLoggingAccess( NULL == UserToken() );
  471. }
  472. //
  473. // First, make sure rsop logging is enabled
  474. //
  475. if ( SUCCEEDED(hr) && GetRsopContext()->IsRsopEnabled() )
  476. {
  477. if ( ResultantSetChanged )
  478. {
  479. hr = _Apps.WriteLog();
  480. if (FAILED(hr))
  481. {
  482. GetRsopContext()->DisableRsop( hr );
  483. }
  484. }
  485. else
  486. {
  487. //
  488. // Disable rsop if there are no changes -- there is nothing
  489. // to log
  490. //
  491. GetRsopContext()->DisableRsop( S_OK );
  492. }
  493. }
  494. if ( GetRsopContext()->IsRsopEnabled() &&
  495. GetRsopContext()->IsPlanningModeEnabled() &&
  496. ARPList() )
  497. {
  498. CCategoryInfoLog CategoryLog( GetRsopContext(), NULL);
  499. hr = CategoryLog.WriteLog();
  500. if (FAILED(hr))
  501. {
  502. GetRsopContext()->DisableRsop( hr );
  503. }
  504. }
  505. //
  506. // We will not set ARP's logging namespace if logging is not enabled
  507. //
  508. if ( GetRsopContext()->IsRsopEnabled() && ! GetRsopContext()->ForcedRefresh() )
  509. {
  510. //
  511. // First, record the namespace so that app management
  512. // service can perform rsop logging
  513. //
  514. if ( ! ARPList() && ! GetRsopContext()->IsPlanningModeEnabled() )
  515. {
  516. (void) GetRsopContext()->SaveNameSpace();
  517. //
  518. // For users, whose apps will roam if they have a user profile,
  519. // write a version into the profile so we can determine if their
  520. // profile is in sync with the machine's current rsop data -- this
  521. // gets updated at each policy run and each time an app is installed
  522. //
  523. if ( IsUserPolicy() )
  524. {
  525. (void) GetRsopContext()->WriteCurrentRsopVersion( AppmgmtKey() );
  526. }
  527. }
  528. }
  529. //
  530. // For ARP, we are now finished logging and users may read
  531. // the logged data now -- release our lock
  532. //
  533. if ( ARPList() )
  534. {
  535. (void) GetRsopContext()->ReleaseExclusiveLoggingAccess();
  536. }
  537. }
  538. HRESULT
  539. CManagedAppProcessor::GetAppsFromDirectory()
  540. {
  541. IEnumPackage * pEnumPackage;
  542. DWORD Size;
  543. DWORD Type;
  544. DWORD AppFlags;
  545. DWORD Status;
  546. HRESULT hr;
  547. Status = Impersonate();
  548. if ( Status != ERROR_SUCCESS )
  549. return HRESULT_FROM_WIN32( Status );
  550. //
  551. // Determine what apps to ask the Diretory for
  552. //
  553. AppFlags = GetDSQuery();
  554. if ( DebugLevelOn( DM_VERBOSE ) )
  555. {
  556. WCHAR Name[32];
  557. DWORD NameLength = sizeof(Name) / sizeof(WCHAR);
  558. Name[0] = 0;
  559. if ( _bUser )
  560. {
  561. if ( ! GetUserName( Name, &NameLength) )
  562. {
  563. if ( LoadLoadString() )
  564. (*pfnLoadStringW)( ghDllInstance, IDS_UNKNOWN, Name, NameLength );
  565. }
  566. DebugMsg((DM_VERBOSE, IDS_USERAPPS_NOCAT, Name, AppFlags));
  567. }
  568. else
  569. {
  570. if ( ! GetComputerName( Name, &NameLength) )
  571. {
  572. if ( LoadLoadString() )
  573. (*pfnLoadStringW)( ghDllInstance, IDS_UNKNOWN, Name, NameLength );
  574. }
  575. DebugMsg((DM_VERBOSE, IDS_MACHINEAPPS, Name, AppFlags));
  576. }
  577. }
  578. //
  579. // If we are not in planning mode, we can use a function that uses
  580. // the cached class store ds paths to determine which parts of the
  581. // ds to query -- this function obtains an enumerator that returns
  582. // query results from the cached ds paths
  583. //
  584. if ( ! GetRsopContext()->IsPlanningModeEnabled() )
  585. {
  586. hr = CsEnumApps(
  587. NULL,
  588. NULL,
  589. NULL,
  590. AppFlags,
  591. &pEnumPackage );
  592. }
  593. else
  594. {
  595. //
  596. // In planning mode, we have no cached ds paths and must explicitly
  597. // specify it in order to obtain an enumerator
  598. //
  599. hr = GetPackageEnumeratorFromPath(
  600. _CSPath.GetPath(),
  601. NULL,
  602. AppFlags,
  603. &pEnumPackage);
  604. }
  605. if ( S_OK == hr )
  606. {
  607. hr = EnumerateApps(pEnumPackage);
  608. pEnumPackage->Release();
  609. }
  610. else
  611. {
  612. DebugMsg((DM_WARNING, IDS_CSENUMAPPS_FAIL, hr));
  613. }
  614. Revert();
  615. return hr;
  616. }
  617. HRESULT
  618. CManagedAppProcessor::EnumerateApps(
  619. IEnumPackage * pEnumPackages
  620. )
  621. {
  622. PACKAGEDISPINFO rgPackages[PACKAGEINFO_ALLOC_COUNT];
  623. ULONG cRetrieved;
  624. CAppList AppList( this );
  625. CAppInfo * pAppInfo;
  626. CAppInfo * pAppInfoOldest;
  627. CGPOInfo * pGPOInfo;
  628. WCHAR * pwszGPOName;
  629. DWORD AppCount;
  630. HRESULT hr;
  631. BOOL bStatus;
  632. memset( rgPackages, 0, sizeof(rgPackages) );
  633. for (;;)
  634. {
  635. hr = pEnumPackages->Next(
  636. PACKAGEINFO_ALLOC_COUNT,
  637. rgPackages,
  638. &cRetrieved);
  639. if ( FAILED(hr) )
  640. return hr;
  641. // This call only fails on out of memory.
  642. bStatus = AddAppsFromDirectory( cRetrieved, rgPackages, AppList );
  643. for ( DWORD n = 0; n < cRetrieved; n++ )
  644. ReleasePackageInfo( &rgPackages[n] );
  645. if ( ! bStatus )
  646. return E_OUTOFMEMORY;
  647. if ( hr == S_FALSE )
  648. break;
  649. }
  650. //
  651. // Now that we have all the packages from the DS, we sort them within each policy
  652. // from oldest to newest deployment time and put them in our final app list.
  653. //
  654. for ( _GPOs.Reset(); pGPOInfo = (CGPOInfo *) _GPOs.GetCurrentItem(); _GPOs.MoveNext() )
  655. {
  656. pwszGPOName = 0;
  657. AppCount = 0;
  658. for (;;)
  659. {
  660. pAppInfoOldest = 0;
  661. for ( AppList.Reset(); pAppInfo = (CAppInfo *) AppList.GetCurrentItem(); AppList.MoveNext() )
  662. {
  663. if ( lstrcmpi( pGPOInfo->_pwszGPOId, pAppInfo->_pwszGPOId ) != 0 )
  664. break;
  665. if ( ! pAppInfoOldest ||
  666. (CompareFileTime( &pAppInfo->_USN, &pAppInfoOldest->_USN ) < 0) )
  667. {
  668. pAppInfoOldest = pAppInfo;
  669. }
  670. }
  671. AppList.ResetEnd();
  672. if ( ! pAppInfoOldest )
  673. break;
  674. if ( 0 == AppCount )
  675. {
  676. pwszGPOName = pAppInfoOldest->_pwszGPOName;
  677. DebugMsg((DM_VERBOSE, IDS_GPOAPPS, pwszGPOName));
  678. }
  679. AppCount++;
  680. if ( DebugLevelOn( DM_VERBOSE ) )
  681. {
  682. if ( pAppInfoOldest->_ActFlags & ACTFLG_Assigned )
  683. {
  684. DebugMsg((DM_VERBOSE, IDS_ADDASSIGNED, pAppInfoOldest->_pwszDeploymentName, pAppInfoOldest->_ActFlags));
  685. }
  686. else if ( pAppInfoOldest->_ActFlags & ACTFLG_Published )
  687. {
  688. DebugMsg((DM_VERBOSE, IDS_ADDPUBLISHED, pAppInfoOldest->_pwszDeploymentName, pAppInfoOldest->_ActFlags));
  689. }
  690. else if ( pAppInfoOldest->_ActFlags & ACTFLG_Orphan )
  691. {
  692. DebugMsg((DM_VERBOSE, IDS_ADDORPHANED, pAppInfoOldest->_pwszDeploymentName));
  693. }
  694. else if ( pAppInfoOldest->_ActFlags & ACTFLG_Uninstall )
  695. {
  696. DebugMsg((DM_VERBOSE, IDS_ADDUNINSTALLED, pAppInfoOldest->_pwszDeploymentName));
  697. }
  698. else
  699. {
  700. DebugMsg((DM_VERBOSE, IDS_ADDUNKNOWN, pAppInfoOldest->_pwszDeploymentName));
  701. }
  702. }
  703. pAppInfoOldest->Remove();
  704. _Apps.InsertFIFO( pAppInfoOldest );
  705. }
  706. if ( AppCount > 0 )
  707. DebugMsg((DM_VERBOSE, IDS_NUMAPPS, AppCount, pwszGPOName));
  708. }
  709. _GPOs.ResetEnd();
  710. return S_OK;
  711. }
  712. BOOL
  713. CManagedAppProcessor::AddAppsFromDirectory(
  714. ULONG cApps,
  715. PACKAGEDISPINFO * rgPackageInfo,
  716. CAppList & AppList
  717. )
  718. {
  719. CAppInfo * pAppInfo;
  720. BOOL bStatus;
  721. for ( DWORD App = 0; App < cApps; App++)
  722. {
  723. switch ( rgPackageInfo[App].PathType )
  724. {
  725. case DrwFilePath :
  726. break;
  727. case SetupNamePath :
  728. if ( ! _bIncludeLegacy )
  729. continue;
  730. break;
  731. default :
  732. continue;
  733. }
  734. bStatus = FALSE;
  735. pAppInfo = new CAppInfo( this, &(rgPackageInfo[App]), FALSE, bStatus );
  736. if ( ! bStatus )
  737. {
  738. if ( pAppInfo )
  739. delete pAppInfo;
  740. pAppInfo = 0;
  741. }
  742. if ( ! pAppInfo )
  743. return FALSE;
  744. AppList.InsertLIFO( pAppInfo );
  745. }
  746. return TRUE;
  747. }
  748. DWORD
  749. CManagedAppProcessor::GetDSQuery()
  750. {
  751. DWORD AppFlags;
  752. //
  753. // We perform different queries depending on whether or not RSoP
  754. // is enabled as well as whether we are doing a query for the
  755. // ARP list of apps or for a policy run
  756. //
  757. if ( GetRsopContext()->IsRsopEnabled() )
  758. {
  759. if (ARPList())
  760. {
  761. AppFlags = APPQUERY_RSOP_ARP;
  762. }
  763. else
  764. {
  765. AppFlags = APPQUERY_RSOP_LOGGING;
  766. }
  767. }
  768. else
  769. {
  770. if (ARPList())
  771. {
  772. AppFlags = APPQUERY_USERDISPLAY;
  773. }
  774. else
  775. {
  776. AppFlags = APPQUERY_POLICY;
  777. }
  778. }
  779. return AppFlags;
  780. }
  781. DWORD
  782. CManagedAppProcessor::GetManagedApplications(
  783. GUID * pCategory,
  784. ARPCONTEXT* pArpContext /* allocated on separate waiting thread */
  785. )
  786. {
  787. WCHAR wszCategoryGuid[40];
  788. DWORD Status;
  789. BOOL bStatus;
  790. MANAGED_APPLIST * pAppList;
  791. BOOL fPlanningMode;
  792. _bARPList = TRUE;
  793. fPlanningMode = pArpContext == NULL;
  794. //
  795. // *********IMPORTANT********
  796. // Note that we should not access the pArpContext structure after we've signaled
  797. // that enumeration is complete using the hEventAppsEnumerated member --
  798. // otherwise, the stack on which this structure is allocated will disappear
  799. // once its thread unblocks waiting for us
  800. //
  801. if ( ! fPlanningMode )
  802. {
  803. pAppList = pArpContext->pAppList;
  804. }
  805. Status = ERROR_SUCCESS;
  806. //
  807. // GPO precedence list is needed for sorting the apps based on USN
  808. // and because the upgrade processing logic requires having the GPO
  809. // precedence list.
  810. //
  811. if ( ! GetRsopContext()->IsPlanningModeEnabled() )
  812. {
  813. Status = LoadPolicyList();
  814. }
  815. else
  816. {
  817. CGPOInfo* pGPOInfo;
  818. for ( _GPOs.Reset(); pGPOInfo = (CGPOInfo *) _GPOs.GetCurrentItem(); _GPOs.MoveNext() )
  819. {
  820. Status = _CSPath.AddComponent( pGPOInfo->_pwszGPOPath, pGPOInfo->_pwszGPOName );
  821. if ( Status != ERROR_SUCCESS )
  822. break;
  823. }
  824. }
  825. if ( ERROR_SUCCESS == Status )
  826. Status = GetAppsFromDirectory();
  827. //
  828. // Not all managed applications should be visible to the caller --
  829. // filter out the ones the caller doesn't want
  830. //
  831. if ( ERROR_SUCCESS == Status )
  832. Status = _Apps.ProcessARPList();
  833. if ( ( Status != ERROR_SUCCESS ) ||
  834. GetRsopContext()->IsPlanningModeEnabled() )
  835. {
  836. goto GetManagedApplications_WriteLogsAndExit;
  837. }
  838. if ( pCategory )
  839. {
  840. GuidToString( *pCategory, wszCategoryGuid);
  841. DebugMsg((DM_VERBOSE, IDS_USERAPPS_CAT, wszCategoryGuid));
  842. }
  843. //
  844. // Now that we have the correct list of apps,
  845. // we need to allocate space for all the apps
  846. // and copy the data for each app to give back to the user
  847. //
  848. //
  849. // First we must count the number of apps we're giving back.
  850. //
  851. // We also determine which apps have common display names and tag them
  852. // to have their policy name catenated to their display names.
  853. //
  854. DWORD dwCount;
  855. CAppInfo * pAppInfo;
  856. CAppInfo * pAppInfoOther;
  857. dwCount = 0;
  858. _Apps.Reset();
  859. for ( pAppInfo = (CAppInfo *) _Apps.GetCurrentItem();
  860. pAppInfo;
  861. _Apps.MoveNext(), pAppInfo = (CAppInfo *) _Apps.GetCurrentItem() )
  862. {
  863. if ( (pAppInfo->_Action != ACTION_INSTALL) )
  864. continue;
  865. if ( pCategory && ! pAppInfo->HasCategory( wszCategoryGuid ) )
  866. {
  867. pAppInfo->SetAction(
  868. ACTION_UNINSTALL,
  869. 0,
  870. NULL);
  871. continue;
  872. }
  873. dwCount++;
  874. }
  875. _Apps.ResetEnd();
  876. //
  877. // Now that we know how many apps we have, we can allocate
  878. // space for them.
  879. //
  880. if ( ! fPlanningMode )
  881. {
  882. pAppList->rgApps = (MANAGED_APP*) midl_user_allocate( sizeof(MANAGED_APP) * dwCount);
  883. if (!(pAppList->rgApps))
  884. {
  885. pArpContext->Status = ERROR_NOT_ENOUGH_MEMORY;
  886. return ERROR_NOT_ENOUGH_MEMORY;
  887. }
  888. memset(pAppList->rgApps, 0, dwCount * sizeof(MANAGED_APP));
  889. //
  890. // Now we do the copying
  891. //
  892. DWORD dwApp;
  893. dwApp = 0;
  894. _Apps.Reset();
  895. for (;;)
  896. {
  897. CAppInfo* pAppInfoCopy;
  898. pAppInfoCopy = (CAppInfo *) _Apps.GetCurrentItem();
  899. if ( ! pAppInfoCopy )
  900. break;
  901. _Apps.MoveNext();
  902. if ( ACTION_INSTALL == pAppInfoCopy->_Action)
  903. {
  904. Status = pAppInfoCopy->CopyToManagedApplication(&(pAppList->rgApps[dwApp]));
  905. if ( Status != ERROR_SUCCESS )
  906. break;
  907. dwApp++;
  908. }
  909. }
  910. _Apps.ResetEnd();
  911. if ( Status != ERROR_SUCCESS )
  912. {
  913. DWORD dwCopiedApp;
  914. //
  915. // On failure, we need to clear any apps we allocated
  916. // before the failure occurred
  917. //
  918. for ( dwCopiedApp = 0; dwCopiedApp < dwApp; dwCopiedApp++ )
  919. {
  920. ClearManagedApp( & ( pAppList->rgApps[ dwCopiedApp ] ) );
  921. }
  922. midl_user_free( pAppList->rgApps );
  923. pAppList->rgApps = 0;
  924. pArpContext->Status = Status;
  925. return Status;
  926. }
  927. pAppList->Applications = dwApp;
  928. }
  929. GetManagedApplications_WriteLogsAndExit:
  930. //
  931. // Store the status of this operation in the waiting thread's context --
  932. // note that this is the last time we can safely access this structure
  933. // since we will next signal its thread to unblock, and the stack
  934. // frame in which this structure is allocated will disappear
  935. //
  936. if ( ! fPlanningMode )
  937. {
  938. pArpContext->Status = Status;
  939. //
  940. // Signal the waiting thread that we are finished enumerating
  941. //
  942. GetRsopContext()->SetAppsEnumerated();
  943. }
  944. //
  945. // Write any Rsop logs -- this is a no op if
  946. // rsop logging is not enabled
  947. //
  948. WriteRsopLogs();
  949. return Status;
  950. }
  951. DWORD
  952. CManagedAppProcessor::GetAppsFromLocal()
  953. {
  954. CAppList LocalApps( NULL );
  955. CAppInfo * pAppInfo;
  956. CAppInfo * pAppInfoInsert;
  957. CAppInfo * pScriptInfo;
  958. int GPOCompare;
  959. DWORD Count;
  960. DWORD Status;
  961. HRESULT hr;
  962. BOOL bStatus;
  963. if ( GetRsopContext()->IsPlanningModeEnabled() )
  964. {
  965. return ERROR_SUCCESS;
  966. }
  967. Status = GetOrderedLocalAppList( LocalApps );
  968. if ( ERROR_SUCCESS == Status )
  969. Status = Impersonate();
  970. if ( Status != ERROR_SUCCESS )
  971. return Status;
  972. Count = 0;
  973. LocalApps.Reset();
  974. for ( pAppInfo = (CAppInfo *) LocalApps.GetCurrentItem();
  975. pAppInfo;
  976. pAppInfo = (CAppInfo *) LocalApps.GetCurrentItem() )
  977. {
  978. //
  979. // Remember which scripts are associated with app entries we find
  980. // in the registry. We'll use this later as a hint to detect roaming
  981. // profile merge problems with our app entries in hkcu.
  982. //
  983. pScriptInfo = _LocalScripts.Find( pAppInfo->_DeploymentId );
  984. if ( pScriptInfo )
  985. pScriptInfo->_State = APPSTATE_SCRIPT_PRESENT;
  986. if ( _Apps.Find( pAppInfo->_DeploymentId ) != NULL )
  987. {
  988. LocalApps.MoveNext();
  989. continue;
  990. }
  991. if ( pAppInfo->_State & APPSTATE_ASSIGNED )
  992. {
  993. DebugMsg((DM_VERBOSE, IDS_LOCALASSIGN_APP, pAppInfo->_pwszDeploymentName, pAppInfo->_pwszGPOName));
  994. }
  995. else if ( pAppInfo->_State & APPSTATE_PUBLISHED )
  996. {
  997. DebugMsg((DM_VERBOSE, IDS_LOCALPUBLISHED_APP, pAppInfo->_pwszDeploymentName, pAppInfo->_pwszGPOName));
  998. }
  999. else if ( pAppInfo->_State & APPSTATE_ORPHANED )
  1000. {
  1001. DebugMsg((DM_VERBOSE, IDS_LOCALORPHAN_APP, pAppInfo->_pwszDeploymentName, pAppInfo->_pwszGPOName));
  1002. }
  1003. else if ( pAppInfo->_State & APPSTATE_UNINSTALLED )
  1004. {
  1005. DebugMsg((DM_VERBOSE, IDS_LOCALUNINSTALL_APP, pAppInfo->_pwszDeploymentName, pAppInfo->_pwszGPOName));
  1006. }
  1007. LocalApps.MoveNext();
  1008. pAppInfo->Remove();
  1009. Count++;
  1010. //
  1011. // If this app is currently applied, check it's real state in the DS. We
  1012. // didn't get in the query results either because it didn't match the search
  1013. // criteria or because it really did go out of scope. Currently applied apps include
  1014. // those listed in the registry as published or assigned, as well as those listed
  1015. // in the registry as unmanaged or uninstalled that currently have a script present
  1016. // on this machine.
  1017. // In the case of no-changes, we have to query for published apps which we
  1018. // don't have scripts for so that we can retrieve the proper sysvol path to
  1019. // get the script.
  1020. //
  1021. if ( (! _bNoChanges && (pAppInfo->_State & (APPSTATE_ASSIGNED | APPSTATE_PUBLISHED | APPSTATE_SCRIPT_EXISTED))) ||
  1022. (_bNoChanges && (pAppInfo->_State & APPSTATE_PUBLISHED) && (pAppInfo->_State & APPSTATE_SCRIPT_NOT_EXISTED)) )
  1023. {
  1024. uCLSSPEC ClassSpec;
  1025. PACKAGEDISPINFO PackageInfo;
  1026. memset( &PackageInfo, 0, sizeof(PackageInfo) );
  1027. ClassSpec.tyspec = TYSPEC_OBJECTID;
  1028. memcpy( &ClassSpec.tagged_union.ByObjectId.ObjectId, &pAppInfo->_DeploymentId, sizeof(GUID) );
  1029. StringToGuid( pAppInfo->_pwszGPOId, &ClassSpec.tagged_union.ByObjectId.PolicyId );
  1030. DebugMsg((DM_VERBOSE, IDS_CHECK_APP, pAppInfo->_pwszDeploymentName, pAppInfo->_pwszGPOName));
  1031. hr = CsGetAppInfo( &ClassSpec, NULL, &PackageInfo );
  1032. if ( S_OK == hr )
  1033. {
  1034. BOOL bRestored;
  1035. bRestored = pAppInfo->_bRestored;
  1036. DebugMsg((DM_VERBOSE, IDS_CHECK_APP_FOUND, pAppInfo->_pwszDeploymentName, pAppInfo->_pwszGPOName, PackageInfo.dwActFlags));
  1037. delete pAppInfo;
  1038. pAppInfo = new CAppInfo( this, &PackageInfo, FALSE, bStatus );
  1039. if ( ! bStatus )
  1040. {
  1041. delete pAppInfo;
  1042. pAppInfo = 0;
  1043. }
  1044. if ( ! pAppInfo )
  1045. Status = ERROR_OUTOFMEMORY;
  1046. ReleasePackageInfo( &PackageInfo );
  1047. if ( pAppInfo )
  1048. pAppInfo->_bRestored = bRestored;
  1049. }
  1050. else if ( CS_E_PACKAGE_NOTFOUND == hr )
  1051. {
  1052. DebugMsg((DM_VERBOSE, IDS_CHECK_APP_NOTFOUND, pAppInfo->_pwszDeploymentName, pAppInfo->_pwszGPOName));
  1053. }
  1054. else
  1055. {
  1056. Status = (DWORD) hr;
  1057. }
  1058. if ( Status != ERROR_SUCCESS )
  1059. {
  1060. DebugMsg((DM_VERBOSE, IDS_CHECK_APP_FAIL, pAppInfo->_pwszDeploymentName, pAppInfo->_pwszGPOName, Status));
  1061. Revert();
  1062. return Status;
  1063. }
  1064. }
  1065. pAppInfoInsert = 0;
  1066. //
  1067. // Now we insert the locally discovered app into the proper sorted spot in
  1068. // our master list generated from the initial DS query.
  1069. //
  1070. for ( _Apps.Reset(); pAppInfoInsert = (CAppInfo *) _Apps.GetCurrentItem(); _Apps.MoveNext() )
  1071. {
  1072. GPOCompare = _GPOs.Compare( pAppInfoInsert->_pwszGPOId, pAppInfo->_pwszGPOId );
  1073. if ( -1 == GPOCompare )
  1074. continue;
  1075. if ( 1 == GPOCompare )
  1076. break;
  1077. // Smallest USN is oldest. We sort from oldest to newest.
  1078. if ( CompareFileTime( &pAppInfoInsert->_USN, &pAppInfo->_USN ) >= 0 )
  1079. break;
  1080. }
  1081. // FIFO insert handles both the empty list and end of list conditions.
  1082. if ( ! pAppInfoInsert )
  1083. _Apps.InsertFIFO( pAppInfo );
  1084. else
  1085. pAppInfoInsert->InsertBefore( pAppInfo );
  1086. }
  1087. LocalApps.ResetEnd();
  1088. Revert();
  1089. DebugMsg((DM_VERBOSE, IDS_LOCALAPP_COUNT, Count));
  1090. return ERROR_SUCCESS;
  1091. }
  1092. BOOL
  1093. CManagedAppProcessor::DetectLostApps()
  1094. {
  1095. HKEY hkApp;
  1096. WCHAR wszDeploymentId[GUIDSTRLEN+1];
  1097. CAppInfo * pScriptInfo;
  1098. DWORD Size;
  1099. DWORD State;
  1100. DWORD Status;
  1101. if ( GetRsopContext()->IsPlanningModeEnabled() || ! _bUser )
  1102. return FALSE;
  1103. for ( _LocalScripts.Reset(); pScriptInfo = (CAppInfo *) _LocalScripts.GetCurrentItem(); _LocalScripts.MoveNext() )
  1104. {
  1105. GuidToString( pScriptInfo->_DeploymentId, wszDeploymentId);
  1106. Status = RegOpenKeyEx(
  1107. _hkAppmgmt,
  1108. wszDeploymentId,
  1109. 0,
  1110. KEY_READ,
  1111. &hkApp );
  1112. if ( Status != ERROR_SUCCESS )
  1113. {
  1114. _LocalScripts.ResetEnd();
  1115. DebugMsg((DM_VERBOSE, IDS_DETECTED_LOST_APPS));
  1116. return TRUE;
  1117. }
  1118. else
  1119. {
  1120. Size = sizeof(DWORD);
  1121. State = 0;
  1122. Status = RegQueryValueEx(
  1123. hkApp,
  1124. APPSTATEVALUE,
  1125. 0,
  1126. NULL,
  1127. (LPBYTE) &State,
  1128. &Size );
  1129. RegCloseKey( hkApp );
  1130. //
  1131. // This isn't a lost app, but rather an app which was orphaned or
  1132. // uninstalled on another computer. We will force a full policy
  1133. // run in this case as well to process the removal. This is similar
  1134. // to the case where we find the FullPolicy value set.
  1135. //
  1136. if ( ! (State & (APPSTATE_PUBLISHED | APPSTATE_ASSIGNED)) )
  1137. return TRUE;
  1138. }
  1139. }
  1140. _LocalScripts.ResetEnd();
  1141. return FALSE;
  1142. }
  1143. DWORD
  1144. CManagedAppProcessor::GetLostApps()
  1145. {
  1146. uCLSSPEC ClassSpec;
  1147. PACKAGEDISPINFO PackageInfo;
  1148. CAppInfo * pScriptInfo;
  1149. CAppInfo * pAppInfo;
  1150. CAppInfo * pAppInfoInsert;
  1151. GUID GPOId;
  1152. int GPOCompare;
  1153. LONG RedeployCount;
  1154. DWORD Status;
  1155. HRESULT hr;
  1156. BOOL bStatus;
  1157. BOOL bInsertNew;
  1158. if ( GetRsopContext()->IsPlanningModeEnabled() || ! _bUser )
  1159. return ERROR_SUCCESS;
  1160. //
  1161. // In this routine we are detecting app entries which are erroneously
  1162. // missing from our hkcu data. This can occur in various scenarios
  1163. // involving roaming profiles. An unassociated script file and our
  1164. // rsop data is used for the detection.
  1165. //
  1166. // Note:
  1167. // There is quite a bit of duplicated code here and in the above routine
  1168. // ::GetAppsFromLocal. That is because this change was added late in
  1169. // WindowsXP and we wanted to isolate it from existing functionality.
  1170. // In future this could be cleaned up if this codebase is taken forward.
  1171. //
  1172. Status = ERROR_SUCCESS;
  1173. for ( _LocalScripts.Reset(); pScriptInfo = (CAppInfo *) _LocalScripts.GetCurrentItem(); _LocalScripts.MoveNext() )
  1174. {
  1175. if ( pScriptInfo->_State != 0 )
  1176. continue;
  1177. DebugMsg((DM_VERBOSE, IDS_UNMATCHED_SCRIPT));
  1178. pAppInfo = 0;
  1179. hr = FindAppInRSoP( pScriptInfo, &GPOId, &RedeployCount );
  1180. if ( ! SUCCEEDED(hr) )
  1181. {
  1182. if ( WBEM_E_NOT_FOUND == hr )
  1183. {
  1184. DebugMsg((DM_VERBOSE, IDS_SCRIPTNOTINRSOP1));
  1185. DeleteScriptFile( pScriptInfo->_DeploymentId );
  1186. continue;
  1187. }
  1188. else
  1189. {
  1190. Status = (DWORD) hr;
  1191. DebugMsg((DM_VERBOSE, IDS_SCRIPTNOTINRSOP2, hr));
  1192. break;
  1193. }
  1194. }
  1195. DebugMsg((DM_VERBOSE, IDS_SCRIPTINRSOP));
  1196. pAppInfo = _Apps.Find( pScriptInfo->_DeploymentId );
  1197. if ( pAppInfo )
  1198. {
  1199. bInsertNew = FALSE;
  1200. DebugMsg((DM_VERBOSE, IDS_SCRIPTAPP_INDS2, pAppInfo->_pwszDeploymentName, pAppInfo->_pwszGPOName));
  1201. goto GetLostAppsInsert;
  1202. }
  1203. memset( &PackageInfo, 0, sizeof(PackageInfo) );
  1204. ClassSpec.tyspec = TYSPEC_OBJECTID;
  1205. memcpy( &ClassSpec.tagged_union.ByObjectId.ObjectId, &pScriptInfo->_DeploymentId, sizeof(GUID) );
  1206. memcpy( &ClassSpec.tagged_union.ByObjectId.PolicyId, &GPOId, sizeof(GUID) );
  1207. Status = Impersonate();
  1208. if ( ERROR_SUCCESS == Status )
  1209. {
  1210. hr = CsGetAppInfo( &ClassSpec, NULL, &PackageInfo );
  1211. Revert();
  1212. }
  1213. else
  1214. {
  1215. hr = HRESULT_FROM_WIN32( Status );
  1216. }
  1217. if ( S_OK == hr )
  1218. {
  1219. bStatus = TRUE;
  1220. pAppInfo = new CAppInfo( this, &PackageInfo, FALSE, bStatus );
  1221. ReleasePackageInfo( &PackageInfo );
  1222. bInsertNew = TRUE;
  1223. if ( ! bStatus )
  1224. {
  1225. delete pAppInfo;
  1226. pAppInfo = 0;
  1227. }
  1228. if ( ! pAppInfo )
  1229. Status = ERROR_OUTOFMEMORY;
  1230. else
  1231. DebugMsg((DM_VERBOSE, IDS_SCRIPTAPP_INDS, pAppInfo->_pwszDeploymentName, pAppInfo->_pwszGPOName));
  1232. }
  1233. else if ( CS_E_PACKAGE_NOTFOUND == hr )
  1234. {
  1235. DebugMsg((DM_VERBOSE, IDS_SCRIPTAPP_NODS));
  1236. //
  1237. // Since the app is not visible to this user, delete the
  1238. // orphaned script.
  1239. //
  1240. DeleteScriptFile( pScriptInfo->_DeploymentId );
  1241. }
  1242. else
  1243. {
  1244. Status = (DWORD) hr;
  1245. }
  1246. if ( Status != ERROR_SUCCESS )
  1247. {
  1248. DebugMsg((DM_VERBOSE, IDS_SCRIPTAPP_ERRORDS, Status));
  1249. break;
  1250. }
  1251. if ( ! pAppInfo )
  1252. continue;
  1253. GetLostAppsInsert:
  1254. //
  1255. // Because we're restoring this app, much of the persisted state is lost.
  1256. // We artifically re-create the key aspects here.
  1257. //
  1258. pAppInfo->_State |= APPSTATE_PUBLISHED | APPSTATE_RESTORED;
  1259. pAppInfo->_AssignCount = 1;
  1260. pAppInfo->_LocalRevision = (DWORD) RedeployCount;
  1261. pAppInfo->_ScriptTime = pScriptInfo->_ScriptTime;
  1262. // Switch to full policy mode whenever we force a lost app back into scope.
  1263. _bNoChanges = FALSE;
  1264. if ( ! bInsertNew )
  1265. continue;
  1266. //
  1267. // Our newly discovered app now needs to be added to our processing list.
  1268. //
  1269. for ( _Apps.Reset(); pAppInfoInsert = (CAppInfo *) _Apps.GetCurrentItem(); _Apps.MoveNext() )
  1270. {
  1271. GPOCompare = _GPOs.Compare( pAppInfoInsert->_pwszGPOId, pAppInfo->_pwszGPOId );
  1272. if ( -1 == GPOCompare )
  1273. continue;
  1274. if ( 1 == GPOCompare )
  1275. break;
  1276. // Smallest USN is oldest. We sort from oldest to newest.
  1277. if ( CompareFileTime( &pAppInfoInsert->_USN, &pAppInfo->_USN ) >= 0 )
  1278. break;
  1279. }
  1280. // FIFO insert handles both the empty list and end of list conditions.
  1281. if ( ! pAppInfoInsert )
  1282. _Apps.InsertFIFO( pAppInfo );
  1283. else
  1284. pAppInfoInsert->InsertBefore( pAppInfo );
  1285. }
  1286. _LocalScripts.ResetEnd();
  1287. return Status;
  1288. }
  1289. HRESULT
  1290. CManagedAppProcessor::FindAppInRSoP(
  1291. CAppInfo * pScriptInfo,
  1292. GUID * pGPOId,
  1293. LONG * pRedeployCount
  1294. )
  1295. {
  1296. WCHAR wszGPOId[128];
  1297. WCHAR * pwszGPOId;
  1298. LONG ValueLen;
  1299. HRESULT hr;
  1300. hr = _Apps.InitRsopLog();
  1301. if ( ! SUCCEEDED(hr) )
  1302. return hr;
  1303. pwszGPOId = wszGPOId;
  1304. ValueLen = sizeof(wszGPOId) / sizeof(wszGPOId[0]);
  1305. //
  1306. // Close your eyes, this is ugly. The CAppInfo which is only used to track
  1307. // script files only have the _DeploymentId member set. However, OpenExistingRecord
  1308. // needs a couple of other fields to operate correctly. We feed those here.
  1309. //
  1310. pScriptInfo->_pManApp = this;
  1311. pScriptInfo->_State = APPSTATE_PUBLISHED;
  1312. CConflict ScriptRecord( pScriptInfo );
  1313. hr = _Apps.OpenExistingRecord( &ScriptRecord );
  1314. if ( SUCCEEDED(hr) )
  1315. {
  1316. hr = ScriptRecord.GetValue( RSOP_ATTRIBUTE_GPOID, pwszGPOId, &ValueLen );
  1317. if ( S_FALSE == hr )
  1318. {
  1319. pwszGPOId = new WCHAR[ValueLen];
  1320. if ( pwszGPOId )
  1321. hr = ScriptRecord.GetValue( RSOP_ATTRIBUTE_GPOID, pwszGPOId, &ValueLen );
  1322. else
  1323. hr = E_OUTOFMEMORY;
  1324. }
  1325. if ( SUCCEEDED(hr) )
  1326. hr = ScriptRecord.GetValue( APP_ATTRIBUTE_REDEPLOYCOUNT, pRedeployCount );
  1327. }
  1328. if ( SUCCEEDED(hr) )
  1329. {
  1330. WCHAR * pwszNull;
  1331. // The GPOId comes back from GetValue like CN={gpoguid},CN=Policies,...
  1332. pwszNull = wcschr( pwszGPOId, L'}' );
  1333. if ( pwszNull )
  1334. {
  1335. pwszNull[1] = 0;
  1336. StringToGuid( &pwszGPOId[3], pGPOId );
  1337. }
  1338. else
  1339. {
  1340. hr = E_UNEXPECTED;
  1341. }
  1342. }
  1343. if ( pwszGPOId != wszGPOId )
  1344. delete [] pwszGPOId;
  1345. return hr;
  1346. }
  1347. void
  1348. CManagedAppProcessor::DeleteScriptFile(
  1349. GUID & DeploymentId
  1350. )
  1351. {
  1352. DWORD Length;
  1353. WCHAR * pwszLocalScriptPath;
  1354. Length = lstrlen( LocalScriptDir() );
  1355. pwszLocalScriptPath = new WCHAR[Length + GUIDSTRLEN + 5];
  1356. if ( ! pwszLocalScriptPath )
  1357. return;
  1358. (void) StringCchCopy( pwszLocalScriptPath, Length+GUIDSTRLEN+5, LocalScriptDir() );
  1359. GuidToString( DeploymentId, &pwszLocalScriptPath[Length]);
  1360. (void) StringCchCopy( &pwszLocalScriptPath[Length+GUIDSTRLEN], 5, L".aas" );
  1361. DeleteFile( pwszLocalScriptPath );
  1362. delete [] pwszLocalScriptPath;
  1363. }
  1364. HRESULT
  1365. CManagedAppProcessor::GetPackageEnumeratorFromPath(
  1366. WCHAR* wszClassStorePath,
  1367. GUID* pCategory,
  1368. DWORD dwAppFlags,
  1369. IEnumPackage** ppIEnumPackage)
  1370. {
  1371. HRESULT hr;
  1372. IClassAccess * pIClassAccess = NULL;
  1373. //
  1374. // Get an IClassAccess
  1375. //
  1376. hr = GetClassAccessFromPath(
  1377. wszClassStorePath,
  1378. &pIClassAccess);
  1379. if (SUCCEEDED(hr))
  1380. {
  1381. //
  1382. // Get the enumerator
  1383. //
  1384. hr = pIClassAccess->EnumPackages(
  1385. NULL,
  1386. pCategory,
  1387. NULL,
  1388. dwAppFlags,
  1389. ppIEnumPackage
  1390. );
  1391. pIClassAccess->Release();
  1392. }
  1393. return hr;
  1394. }
  1395. HRESULT
  1396. CManagedAppProcessor::GetDsPackageFromGPO(
  1397. CGPOInfo* pGpoInfo,
  1398. GUID* pDeploymentId,
  1399. PACKAGEDISPINFO* pPackageInfo)
  1400. {
  1401. HRESULT hr;
  1402. memset( pPackageInfo, 0, sizeof(*pPackageInfo) );
  1403. //
  1404. // Determine the class store path for this gpo
  1405. //
  1406. WCHAR* pwszClassStorePath;
  1407. pwszClassStorePath = NULL;
  1408. //
  1409. // The path returned is allocated by the callee so we must
  1410. // free it later
  1411. //
  1412. hr = CsGetClassStorePath( pGpoInfo->GetGPOPath(), &pwszClassStorePath );
  1413. if ( FAILED(hr) )
  1414. {
  1415. return hr;
  1416. }
  1417. //
  1418. // Terminate the class store path list with a delimiter to satisfy
  1419. // class store syntax requirements
  1420. //
  1421. WCHAR* pwszTerminatedClassStorePath;
  1422. ULONG ulSize;
  1423. ulSize = lstrlen( pwszClassStorePath ) + 1 + 1;
  1424. pwszTerminatedClassStorePath = new WCHAR[ ulSize ];
  1425. if ( pwszTerminatedClassStorePath )
  1426. {
  1427. IClassAccess * pIClassAccess = NULL;
  1428. uCLSSPEC ClassSpec;
  1429. ClassSpec.tyspec = TYSPEC_OBJECTID;
  1430. memcpy( &ClassSpec.tagged_union.ByObjectId.ObjectId, pDeploymentId, sizeof(GUID) );
  1431. StringToGuid( pGpoInfo->_pwszGPOId, &ClassSpec.tagged_union.ByObjectId.PolicyId );
  1432. //
  1433. // Perform the actual termination
  1434. //
  1435. hr = StringCchCopy( pwszTerminatedClassStorePath, ulSize, pwszClassStorePath );
  1436. if (SUCCEEDED(hr))
  1437. {
  1438. hr = StringCchCat( pwszTerminatedClassStorePath, ulSize, L";" );
  1439. }
  1440. if (SUCCEEDED(hr))
  1441. {
  1442. //
  1443. // Get an IClassAccess using this class store path
  1444. //
  1445. hr = GetClassAccessFromPath(
  1446. pwszTerminatedClassStorePath,
  1447. &pIClassAccess);
  1448. }
  1449. if (SUCCEEDED(hr))
  1450. {
  1451. //
  1452. // Perform the search for the requested deployment
  1453. //
  1454. hr = pIClassAccess->GetAppInfo(
  1455. &ClassSpec,
  1456. NULL,
  1457. pPackageInfo);
  1458. pIClassAccess->Release();
  1459. }
  1460. }
  1461. else
  1462. {
  1463. hr = E_OUTOFMEMORY;
  1464. }
  1465. if ( pwszClassStorePath )
  1466. {
  1467. LocalFree( pwszClassStorePath );
  1468. }
  1469. delete [] pwszTerminatedClassStorePath;
  1470. return hr;
  1471. }
  1472. HRESULT
  1473. CManagedAppProcessor::GetClassAccessFromPath(
  1474. WCHAR* wszClassStorePath,
  1475. IClassAccess** ppIClassAccess)
  1476. {
  1477. HRESULT hr;
  1478. PRSOP_TARGET pRsopTarget;
  1479. *ppIClassAccess = NULL;
  1480. pRsopTarget = GetRsopContext()->_pRsopTarget;
  1481. //
  1482. // Get an IClassAccess
  1483. //
  1484. hr = CsGetClassAccess(ppIClassAccess);
  1485. //
  1486. // Set the IClassAccess to use the class stores
  1487. // corresponding to the class store path passed in
  1488. //
  1489. if (SUCCEEDED(hr))
  1490. {
  1491. hr = (*ppIClassAccess)->SetClassStorePath(
  1492. wszClassStorePath,
  1493. pRsopTarget ? pRsopTarget->pRsopToken : NULL );
  1494. }
  1495. return hr;
  1496. }
  1497. DWORD
  1498. CManagedAppProcessor::CommitPolicyList()
  1499. {
  1500. CGPOInfo * pGPO;
  1501. WCHAR * pwszGPOList;
  1502. DWORD Length;
  1503. DWORD Status;
  1504. HRESULT hr;
  1505. if ( GetRsopContext()->IsPlanningModeEnabled() )
  1506. {
  1507. return ERROR_SUCCESS;
  1508. }
  1509. Length = 1;
  1510. _GPOs.Reset();
  1511. for ( pGPO = (CGPOInfo *) _GPOs.GetCurrentItem();
  1512. pGPO;
  1513. _GPOs.MoveNext(), pGPO = (CGPOInfo *) _GPOs.GetCurrentItem() )
  1514. {
  1515. Length += lstrlen( pGPO->_pwszGPOId ) + 1;
  1516. }
  1517. _GPOs.ResetEnd();
  1518. pwszGPOList = new WCHAR[Length];
  1519. if ( ! pwszGPOList )
  1520. return ERROR_OUTOFMEMORY;
  1521. pwszGPOList[0] = 0;
  1522. _GPOs.Reset();
  1523. for ( pGPO = (CGPOInfo *) _GPOs.GetCurrentItem();
  1524. pGPO;
  1525. _GPOs.MoveNext(), pGPO = (CGPOInfo *) _GPOs.GetCurrentItem() )
  1526. {
  1527. hr = StringCchCat( pwszGPOList, Length, pGPO->_pwszGPOId );
  1528. if (SUCCEEDED(hr))
  1529. {
  1530. hr = StringCchCat( pwszGPOList, Length, L";" );
  1531. }
  1532. if (FAILED(hr))
  1533. {
  1534. delete [] pwszGPOList;
  1535. return hr;
  1536. }
  1537. }
  1538. _GPOs.ResetEnd();
  1539. Status = RegSetValueEx(
  1540. _hkAppmgmt,
  1541. POLICYLISTVALUE,
  1542. 0,
  1543. REG_SZ,
  1544. (LPBYTE) pwszGPOList,
  1545. Length * sizeof(WCHAR) );
  1546. delete [] pwszGPOList;
  1547. return Status;
  1548. }
  1549. DWORD
  1550. CManagedAppProcessor::LoadPolicyList()
  1551. {
  1552. PGROUP_POLICY_OBJECT pGPOList = NULL;
  1553. DWORD Status;
  1554. Status = Impersonate();
  1555. if ( ERROR_SUCCESS == Status )
  1556. {
  1557. Status = GetCurrentUserGPOList( &pGPOList );
  1558. Revert();
  1559. }
  1560. if (ERROR_SUCCESS == Status)
  1561. {
  1562. Status = SetPolicyListFromGPOList( pGPOList );
  1563. }
  1564. if ( pGPOList )
  1565. {
  1566. FreeGPOList( pGPOList );
  1567. }
  1568. if ( ERROR_SUCCESS == Status )
  1569. {
  1570. MergePolicyList();
  1571. }
  1572. return Status;
  1573. }
  1574. DWORD
  1575. CManagedAppProcessor::SetPolicyListFromGPOList(
  1576. PGROUP_POLICY_OBJECT pGPOList
  1577. )
  1578. {
  1579. DWORD Status;
  1580. WCHAR * pwszGPOList;
  1581. WCHAR * pwszGPO;
  1582. WCHAR * pwszGPOEnd;
  1583. DWORD Size;
  1584. Status = ERROR_SUCCESS;
  1585. pwszGPOList = 0;
  1586. Size = 0;
  1587. PGROUP_POLICY_OBJECT pCurrentGPO;
  1588. for (pCurrentGPO = pGPOList; NULL != pCurrentGPO; pCurrentGPO = pCurrentGPO->pNext)
  1589. {
  1590. BOOL bStatus;
  1591. DebugMsg((DM_VERBOSE, IDS_GPO_NAME, pCurrentGPO->lpDisplayName, pCurrentGPO->szGPOName));
  1592. DebugMsg((DM_VERBOSE, IDS_GPO_FILESYSPATH, pCurrentGPO->lpFileSysPath));
  1593. DebugMsg((DM_VERBOSE, IDS_GPO_DSPATH, pCurrentGPO->lpDSPath));
  1594. bStatus = AddGPO( pCurrentGPO );
  1595. if ( ! bStatus )
  1596. {
  1597. Status = ERROR_OUTOFMEMORY;
  1598. break;
  1599. }
  1600. }
  1601. return Status;
  1602. }
  1603. DWORD
  1604. CManagedAppProcessor::MergePolicyList()
  1605. {
  1606. WCHAR * pwszGPOList;
  1607. WCHAR * pwszGPO;
  1608. WCHAR * pwszGPOEnd;
  1609. DWORD Size;
  1610. DWORD Status;
  1611. BOOL bStatus;
  1612. pwszGPOList = 0;
  1613. Size = 0;
  1614. Status = RegQueryValueEx(
  1615. _hkAppmgmt,
  1616. POLICYLISTVALUE,
  1617. 0,
  1618. NULL,
  1619. (LPBYTE) NULL,
  1620. &Size );
  1621. if ( ERROR_FILE_NOT_FOUND == Status )
  1622. return ERROR_SUCCESS;
  1623. if ( ERROR_SUCCESS == Status )
  1624. {
  1625. pwszGPOList = new WCHAR[Size/2];
  1626. if ( ! pwszGPOList )
  1627. return ERROR_OUTOFMEMORY;
  1628. Status = RegQueryValueEx(
  1629. _hkAppmgmt,
  1630. POLICYLISTVALUE,
  1631. 0,
  1632. NULL,
  1633. (LPBYTE) pwszGPOList,
  1634. &Size );
  1635. }
  1636. if ( ERROR_SUCCESS == Status )
  1637. {
  1638. GROUP_POLICY_OBJECT GPOInfo;
  1639. memset( &GPOInfo, 0, sizeof( GPOInfo ) );
  1640. for ( pwszGPO = pwszGPOList; *pwszGPO; pwszGPO = pwszGPOEnd + 1 )
  1641. {
  1642. pwszGPOEnd = wcschr( pwszGPO, L';' );
  1643. if ( ! pwszGPOEnd )
  1644. {
  1645. Status = ERROR_INVALID_PARAMETER;
  1646. break;
  1647. }
  1648. *pwszGPOEnd = 0;
  1649. if ( ! _GPOs.Find( pwszGPO ) )
  1650. {
  1651. HRESULT hr;
  1652. hr = StringCchCopy( GPOInfo.szGPOName,
  1653. sizeof(GPOInfo.szGPOName)/sizeof(GPOInfo.szGPOName[0]),
  1654. pwszGPO );
  1655. if (FAILED(hr))
  1656. {
  1657. Status = HRESULT_CODE(hr);
  1658. break;
  1659. }
  1660. GPOInfo.lpDisplayName = L"";
  1661. GPOInfo.lpDSPath = L"";
  1662. GPOInfo.lpLink = L"";
  1663. bStatus = _GPOs.Add( &GPOInfo );
  1664. if ( ! bStatus )
  1665. {
  1666. Status = ERROR_OUTOFMEMORY;
  1667. break;
  1668. }
  1669. }
  1670. }
  1671. }
  1672. delete [] pwszGPOList;
  1673. return Status;
  1674. }
  1675. DWORD
  1676. CManagedAppProcessor::CreateAndSecureScriptDir()
  1677. {
  1678. SECURITY_DESCRIPTOR SecDesc;
  1679. SECURITY_ATTRIBUTES SecAttr;
  1680. SID_IDENTIFIER_AUTHORITY AuthorityNT = SECURITY_NT_AUTHORITY;
  1681. SID_IDENTIFIER_AUTHORITY AuthorityEveryone = SECURITY_WORLD_SID_AUTHORITY;
  1682. PSID pSidUser;
  1683. PSID pSidEveryone;
  1684. PSID pSidSystem;
  1685. PSID pSidAdmin;
  1686. PACL pAcl;
  1687. ACE_HEADER * pAceHeader;
  1688. PSID pSid;
  1689. DWORD AclSize;
  1690. DWORD AceIndex;
  1691. DWORD Length;
  1692. DWORD Attributes;
  1693. DWORD Size;
  1694. DWORD Status;
  1695. BOOL bStatus;
  1696. Status = ERROR_SUCCESS;
  1697. //
  1698. // The following check is used to determine if the appmgmt directories
  1699. // for this user/machine exist in the proper win2001 format. If so we
  1700. // can quickly exit.
  1701. // When the directories exist but without the system bit set, this means
  1702. // we need to migrate to the new win2001 ACL format.
  1703. //
  1704. Attributes = GetFileAttributes( _pwszLocalPath );
  1705. if ( (Attributes != (DWORD) -1) && (Attributes & FILE_ATTRIBUTE_SYSTEM) )
  1706. return ERROR_SUCCESS;
  1707. //
  1708. // If a user object is moved within a domain forest the SID will change.
  1709. // Here we check for that case and rename the previous SID dir to the
  1710. // new SID. This is only a necessary check if a script dir by the current
  1711. // SID name does not exist.
  1712. //
  1713. if ( ((DWORD) -1 == Attributes) && _bUser )
  1714. {
  1715. WCHAR * pwszPreviousSid = 0;
  1716. Status = GetPreviousSid( _hUserToken, _pwszLocalPath, &pwszPreviousSid );
  1717. if ( (ERROR_SUCCESS == Status) && pwszPreviousSid )
  1718. {
  1719. Status = RenameScriptDir( pwszPreviousSid, _pwszLocalPath );
  1720. delete pwszPreviousSid;
  1721. return Status;
  1722. }
  1723. if ( Status != ERROR_SUCCESS )
  1724. return Status;
  1725. }
  1726. pSidEveryone = 0;
  1727. pSidSystem = 0;
  1728. pSidAdmin = 0;
  1729. pSidUser = 0;
  1730. pAcl = 0;
  1731. if ( _bUser )
  1732. {
  1733. pSidUser = AppmgmtGetUserSid( _hUserToken );
  1734. if ( ! pSidUser )
  1735. return ERROR_OUTOFMEMORY;
  1736. }
  1737. bStatus = AllocateAndInitializeSid( &AuthorityEveryone, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pSidEveryone );
  1738. if ( bStatus )
  1739. bStatus = AllocateAndInitializeSid( &AuthorityNT, 1, SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, &pSidSystem );
  1740. if ( bStatus )
  1741. bStatus = AllocateAndInitializeSid( &AuthorityNT, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pSidAdmin );
  1742. if ( ! bStatus )
  1743. {
  1744. Status = GetLastError();
  1745. goto SecureScriptDirEnd;
  1746. }
  1747. AclSize = GetLengthSid(pSidSystem) +
  1748. GetLengthSid(pSidAdmin) +
  1749. sizeof(ACL) + (3 * (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)));
  1750. if ( _bUser )
  1751. AclSize += GetLengthSid(pSidUser);
  1752. else
  1753. AclSize += GetLengthSid(pSidEveryone);
  1754. pAcl = (PACL) LocalAlloc( 0, AclSize );
  1755. if ( pAcl )
  1756. {
  1757. bStatus = InitializeAcl( pAcl, AclSize, ACL_REVISION );
  1758. if ( ! bStatus )
  1759. Status = GetLastError();
  1760. }
  1761. else
  1762. {
  1763. Status = ERROR_OUTOFMEMORY;
  1764. }
  1765. if ( Status != ERROR_SUCCESS )
  1766. goto SecureScriptDirEnd;
  1767. //
  1768. // Access is as follows :
  1769. // %systemroot%\system32
  1770. // appmgmt - LocalSystem (Full), Admin Group (Full), Everyone (Read/Execute, this folder only)
  1771. // appmgmt\machine - LocalSystem (Full), Admin Group (Full)
  1772. // appmgmt\<usersid> - LocalSystem (Full), Admin Group (Full), <usersid> (Read/Execute)
  1773. //
  1774. AceIndex = 0;
  1775. bStatus = AddAccessAllowedAce(pAcl, ACL_REVISION, FILE_ALL_ACCESS, pSidSystem);
  1776. if ( bStatus )
  1777. {
  1778. bStatus = GetAce(pAcl, AceIndex, (void **) &pAceHeader);
  1779. if ( bStatus )
  1780. pAceHeader->AceFlags |= (OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE);
  1781. }
  1782. if ( bStatus )
  1783. {
  1784. AceIndex++;
  1785. bStatus = AddAccessAllowedAce(pAcl, ACL_REVISION, FILE_ALL_ACCESS, pSidAdmin);
  1786. if ( bStatus )
  1787. {
  1788. bStatus = GetAce(pAcl, AceIndex, (void **) &pAceHeader);
  1789. if ( bStatus )
  1790. pAceHeader->AceFlags |= (OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE);
  1791. }
  1792. }
  1793. if ( bStatus )
  1794. {
  1795. AceIndex++;
  1796. bStatus = AddAccessAllowedAce(pAcl, ACL_REVISION, FILE_GENERIC_READ | FILE_GENERIC_EXECUTE, pSidEveryone);
  1797. }
  1798. if ( ! bStatus )
  1799. {
  1800. Status = GetLastError();
  1801. goto SecureScriptDirEnd;
  1802. }
  1803. bStatus = InitializeSecurityDescriptor( &SecDesc, SECURITY_DESCRIPTOR_REVISION );
  1804. if ( bStatus )
  1805. bStatus = SetSecurityDescriptorDacl( &SecDesc, TRUE, pAcl, FALSE );
  1806. if ( bStatus )
  1807. {
  1808. PWCHAR pwszSlash1, pwszSlash2;
  1809. //
  1810. // We are always creating dirs of the form "%systemroot%\system32\appmgmt\<sid>\".
  1811. //
  1812. pwszSlash1 = wcsrchr( _pwszLocalPath, L'\\' );
  1813. *pwszSlash1 = 0;
  1814. pwszSlash2 = wcsrchr( _pwszLocalPath, L'\\' );
  1815. *pwszSlash2 = 0;
  1816. SecAttr.nLength = sizeof( SecAttr );
  1817. SecAttr.lpSecurityDescriptor = &SecDesc;
  1818. SecAttr.bInheritHandle = FALSE;
  1819. // This creates the root appmgmt dir.
  1820. bStatus = CreateDirectory( _pwszLocalPath, &SecAttr );
  1821. if ( ! bStatus && (ERROR_ALREADY_EXISTS == GetLastError()) )
  1822. {
  1823. bStatus = SetFileSecurity( _pwszLocalPath, DACL_SECURITY_INFORMATION, &SecDesc );
  1824. }
  1825. *pwszSlash1 = L'\\';
  1826. *pwszSlash2 = L'\\';
  1827. if ( bStatus )
  1828. {
  1829. //
  1830. // We always remove the Everyone ACE, but only in the case of a user
  1831. // (rather then the machine) subdir do we then add in a user specific
  1832. // ACE to replace it below.
  1833. //
  1834. bStatus = DeleteAce( pAcl, AceIndex );
  1835. if ( _bUser )
  1836. {
  1837. if ( bStatus )
  1838. bStatus = AddAccessAllowedAce(pAcl, ACL_REVISION, FILE_GENERIC_READ | FILE_GENERIC_EXECUTE, pSidUser);
  1839. if ( bStatus )
  1840. {
  1841. bStatus = GetAce(pAcl, AceIndex, (void **) &pAceHeader);
  1842. if ( bStatus )
  1843. pAceHeader->AceFlags |= (OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE);
  1844. }
  1845. }
  1846. // This now creates the user specific subdir.
  1847. if ( bStatus )
  1848. bStatus = CreateDirectory( _pwszLocalPath, &SecAttr );
  1849. }
  1850. if ( ! bStatus && (ERROR_ALREADY_EXISTS == GetLastError()) )
  1851. {
  1852. bStatus = SetFileSecurity( _pwszLocalPath, DACL_SECURITY_INFORMATION, &SecDesc );
  1853. if ( bStatus )
  1854. bStatus = SetFileAttributes( _pwszLocalPath, FILE_ATTRIBUTE_SYSTEM );
  1855. }
  1856. }
  1857. if ( ! bStatus )
  1858. Status = GetLastError();
  1859. SecureScriptDirEnd:
  1860. FreeSid( pSidUser );
  1861. FreeSid( pSidEveryone );
  1862. FreeSid( pSidSystem );
  1863. FreeSid( pSidAdmin );
  1864. LocalFree( pAcl );
  1865. return Status;
  1866. }
  1867. DWORD
  1868. CManagedAppProcessor::GetOrderedLocalAppList(
  1869. CAppList & AppList
  1870. )
  1871. {
  1872. CAppList RegAppList( NULL );
  1873. CAppInfo * pAppInfo;
  1874. WCHAR wszDeploymentId[44];
  1875. WCHAR * pwszGPOList;
  1876. WCHAR * pwszGPO;
  1877. WCHAR * pwszGPOEnd;
  1878. DWORD Index;
  1879. DWORD Size;
  1880. DWORD Status;
  1881. BOOL bStatus;
  1882. DebugMsg((DM_VERBOSE, IDS_GET_LOCAL_APPS));
  1883. pwszGPOList = 0;
  1884. Size = 0;
  1885. Status = ERROR_FILE_NOT_FOUND;
  1886. if ( !GetRsopContext()->IsPlanningModeEnabled() )
  1887. {
  1888. Status = RegQueryValueEx(
  1889. _hkAppmgmt,
  1890. POLICYLISTVALUE,
  1891. 0,
  1892. NULL,
  1893. (LPBYTE) NULL,
  1894. &Size );
  1895. }
  1896. if ( ERROR_SUCCESS == Status )
  1897. {
  1898. pwszGPOList = new WCHAR[Size/2];
  1899. if ( ! pwszGPOList )
  1900. {
  1901. Status = ERROR_OUTOFMEMORY;
  1902. goto GetOrderedLocalAppListEnd;
  1903. }
  1904. Status = RegQueryValueEx(
  1905. _hkAppmgmt,
  1906. POLICYLISTVALUE,
  1907. 0,
  1908. NULL,
  1909. (LPBYTE) pwszGPOList,
  1910. &Size );
  1911. }
  1912. else
  1913. {
  1914. //
  1915. // The policylist named value will not exist the first time policy
  1916. // runs. Therefor there will be no apps already on the machine if
  1917. // this value is not present.
  1918. //
  1919. // Note however, that because this is a new value for NT5 beta3,
  1920. // beta2+ clients may have apps, but won't have this value. For
  1921. // those machines, this new value will be written during the first
  1922. // full policy run. That will not pose any problems.
  1923. //
  1924. return ERROR_SUCCESS;
  1925. }
  1926. if ( Status != ERROR_SUCCESS )
  1927. goto GetOrderedLocalAppListEnd;
  1928. Index = 0;
  1929. for (;;)
  1930. {
  1931. Status = RegEnumKey(
  1932. _hkAppmgmt,
  1933. Index++,
  1934. wszDeploymentId,
  1935. sizeof(wszDeploymentId) / sizeof(WCHAR) );
  1936. if ( ERROR_NO_MORE_ITEMS == Status )
  1937. {
  1938. Index--;
  1939. Status = ERROR_SUCCESS;
  1940. break;
  1941. }
  1942. if ( Status != ERROR_SUCCESS )
  1943. break;
  1944. pAppInfo = new CAppInfo( this, wszDeploymentId, bStatus );
  1945. if ( ! pAppInfo || ! bStatus )
  1946. Status = ERROR_OUTOFMEMORY;
  1947. if ( Status != ERROR_SUCCESS )
  1948. {
  1949. if ( pAppInfo )
  1950. delete pAppInfo;
  1951. break;
  1952. }
  1953. RegAppList.InsertFIFO( pAppInfo );
  1954. }
  1955. if ( Status != ERROR_SUCCESS )
  1956. goto GetOrderedLocalAppListEnd;
  1957. if ( 0 == Index )
  1958. {
  1959. DebugMsg((DM_VERBOSE, IDS_NO_LOCAL_APPS));
  1960. }
  1961. else
  1962. {
  1963. DebugMsg((DM_VERBOSE, IDS_LOCAL_APP_COUNT, Index));
  1964. }
  1965. //
  1966. // We first gather all apps for policies we know about and order them
  1967. // according to the policy precedences.
  1968. //
  1969. for ( pwszGPO = pwszGPOList; *pwszGPO; pwszGPO = pwszGPOEnd + 1 )
  1970. {
  1971. pwszGPOEnd = wcschr( pwszGPO, L';' );
  1972. *pwszGPOEnd = 0;
  1973. RegAppList.Reset();
  1974. for ( pAppInfo = (CAppInfo *) RegAppList.GetCurrentItem();
  1975. pAppInfo;
  1976. pAppInfo = (CAppInfo *) RegAppList.GetCurrentItem() )
  1977. {
  1978. if ( lstrcmpi( pAppInfo->_pwszGPOId, pwszGPO ) != 0 )
  1979. {
  1980. RegAppList.MoveNext();
  1981. continue;
  1982. }
  1983. DebugMsg((DM_VERBOSE, IDS_LOCAL_APP_DUMP, pAppInfo->_pwszDeploymentName, pAppInfo->_pwszGPOName, pAppInfo->_State, pAppInfo->_AssignCount));
  1984. RegAppList.MoveNext();
  1985. pAppInfo->Remove();
  1986. AppList.InsertFIFO( pAppInfo );
  1987. }
  1988. RegAppList.ResetEnd();
  1989. }
  1990. //
  1991. // In some instances we will still have apps in the registry that are not in the
  1992. // current list of policies. We add them to the front of the final list.
  1993. //
  1994. for ( RegAppList.Reset(); pAppInfo = (CAppInfo *) RegAppList.GetCurrentItem(); )
  1995. {
  1996. DebugMsg((DM_VERBOSE, IDS_LOCAL_APP_DUMP, pAppInfo->_pwszDeploymentName, pAppInfo->_pwszGPOName, pAppInfo->_State, pAppInfo->_AssignCount));
  1997. RegAppList.MoveNext();
  1998. pAppInfo->Remove();
  1999. AppList.InsertLIFO( pAppInfo );
  2000. }
  2001. RegAppList.ResetEnd();
  2002. GetOrderedLocalAppListEnd:
  2003. delete [] pwszGPOList;
  2004. if ( Status != ERROR_SUCCESS )
  2005. DebugMsg((DM_WARNING, IDS_GETLOCALAPPS_FAIL, Status));
  2006. return Status;
  2007. }
  2008. DWORD
  2009. CManagedAppProcessor::GetLocalScriptAppList(
  2010. CAppList & AppList
  2011. )
  2012. {
  2013. WIN32_FIND_DATA FindData;
  2014. CAppInfo * pAppInfo;
  2015. WCHAR * pwszPath;
  2016. HANDLE hFind;
  2017. DWORD Status;
  2018. HRESULT hr;
  2019. ULONG ulSize;
  2020. ulSize = lstrlen(_pwszLocalPath) + 7;
  2021. pwszPath = new WCHAR[ulSize];
  2022. if ( ! pwszPath )
  2023. return ERROR_OUTOFMEMORY;
  2024. hr = StringCchCopy( pwszPath, ulSize, _pwszLocalPath );
  2025. if (SUCCEEDED(hr))
  2026. {
  2027. hr = StringCchCat( pwszPath, ulSize, L"\\*.aas" );
  2028. }
  2029. if (FAILED(hr))
  2030. {
  2031. delete [] pwszPath;
  2032. return HRESULT_CODE(hr);
  2033. }
  2034. hFind = FindFirstFile( pwszPath, &FindData );
  2035. delete [] pwszPath;
  2036. if ( INVALID_HANDLE_VALUE == hFind )
  2037. return ERROR_SUCCESS;
  2038. do
  2039. {
  2040. if ( FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
  2041. continue;
  2042. pwszPath = wcschr( FindData.cFileName, L'.' );
  2043. if ( ! pwszPath )
  2044. continue;
  2045. *pwszPath = 0;
  2046. pAppInfo = new CAppInfo( FindData.cFileName );
  2047. if ( ! pAppInfo )
  2048. return ERROR_OUTOFMEMORY;
  2049. pAppInfo->_ScriptTime = FindData.ftLastWriteTime;
  2050. AppList.InsertFIFO( pAppInfo );
  2051. } while ( FindNextFile( hFind, &FindData ) );
  2052. FindClose( hFind );
  2053. return ERROR_SUCCESS;
  2054. }
  2055. DWORD
  2056. GetScriptDirPath(
  2057. HANDLE hToken,
  2058. DWORD ExtraPathChars,
  2059. WCHAR ** ppwszPath,
  2060. DWORD *pdwAllocatedLength
  2061. )
  2062. {
  2063. WCHAR wszPath[MAX_PATH];
  2064. WCHAR * pwszSystemDir;
  2065. DWORD AllocLength;
  2066. DWORD Length;
  2067. DWORD Status;
  2068. UNICODE_STRING SidString;
  2069. Status = ERROR_SUCCESS;
  2070. *ppwszPath = 0;
  2071. if (NULL != pdwAllocatedLength)
  2072. {
  2073. *pdwAllocatedLength = 0;
  2074. }
  2075. pwszSystemDir = wszPath;
  2076. AllocLength = sizeof(wszPath) / sizeof(WCHAR);
  2077. RtlInitUnicodeString( &SidString, NULL );
  2078. for (;;)
  2079. {
  2080. Length = GetSystemDirectory(
  2081. pwszSystemDir,
  2082. AllocLength );
  2083. if ( 0 == Length )
  2084. return GetLastError();
  2085. if ( Length >= AllocLength )
  2086. {
  2087. AllocLength = Length + 1;
  2088. pwszSystemDir = (WCHAR *) LocalAlloc( 0, AllocLength * sizeof(WCHAR) );
  2089. if ( ! pwszSystemDir )
  2090. return ERROR_OUTOFMEMORY;
  2091. continue;
  2092. }
  2093. break;
  2094. }
  2095. if ( hToken )
  2096. {
  2097. Status = GetSidString( hToken, &SidString );
  2098. }
  2099. else
  2100. {
  2101. RtlInitUnicodeString( &SidString, L"MACHINE" );
  2102. }
  2103. if ( ERROR_SUCCESS == Status )
  2104. {
  2105. HRESULT hr;
  2106. ULONG ulSize;
  2107. // System dir + \appmgmt\ + Sid + \ + null
  2108. ulSize = Length + 11 + (SidString.Length / 2) + ExtraPathChars;
  2109. *ppwszPath = new WCHAR[ulSize];
  2110. if ( *ppwszPath )
  2111. {
  2112. hr = StringCchCopy( *ppwszPath, ulSize, pwszSystemDir );
  2113. if (SUCCEEDED(hr))
  2114. {
  2115. if ( pwszSystemDir[lstrlen(pwszSystemDir)-1] != L'\\' )
  2116. {
  2117. hr = StringCchCat( *ppwszPath, ulSize, L"\\" );
  2118. }
  2119. if (SUCCEEDED(hr))
  2120. {
  2121. hr = StringCchCat( *ppwszPath, ulSize, L"appmgmt\\" );
  2122. if (SUCCEEDED(hr))
  2123. {
  2124. hr = StringCchCat( *ppwszPath, ulSize, SidString.Buffer );
  2125. if (SUCCEEDED(hr))
  2126. {
  2127. hr = StringCchCat( *ppwszPath, ulSize, L"\\" );
  2128. }
  2129. }
  2130. }
  2131. }
  2132. if (SUCCEEDED(hr))
  2133. {
  2134. if (NULL != pdwAllocatedLength)
  2135. {
  2136. *pdwAllocatedLength = ulSize;
  2137. }
  2138. }
  2139. else
  2140. {
  2141. delete [] *ppwszPath;
  2142. *ppwszPath = NULL;
  2143. Status = HRESULT_CODE(hr);
  2144. }
  2145. }
  2146. else
  2147. {
  2148. Status = ERROR_OUTOFMEMORY;
  2149. }
  2150. }
  2151. if ( hToken )
  2152. RtlFreeUnicodeString( &SidString );
  2153. if ( pwszSystemDir != wszPath )
  2154. LocalFree( pwszSystemDir );
  2155. return Status;
  2156. }
  2157. void
  2158. CManagedAppProcessor::LogonMsg(
  2159. DWORD MsgId,
  2160. ...
  2161. )
  2162. {
  2163. WCHAR wszMsg[80];
  2164. WCHAR wszBuffer[256];
  2165. va_list VAList;
  2166. int Status;
  2167. if ( ! _pfnStatusCallback || ! LoadLoadString() )
  2168. return;
  2169. Status = (*pfnLoadStringW)( ghDllInstance, MsgId, wszMsg, sizeof(wszMsg) / sizeof(WCHAR) );
  2170. if ( 0 == Status )
  2171. return;
  2172. va_start( VAList, MsgId );
  2173. (void) StringCchVPrintf( wszBuffer, sizeof(wszBuffer)/sizeof(wszBuffer[0]), wszMsg, VAList);
  2174. va_end( VAList );
  2175. _pfnStatusCallback( FALSE, wszBuffer );
  2176. }
  2177. //
  2178. // CGPOInfoList
  2179. //
  2180. CGPOInfoList::~CGPOInfoList()
  2181. {
  2182. CGPOInfo * pGPO;
  2183. Reset();
  2184. while ( pGPO = (CGPOInfo *) GetCurrentItem() )
  2185. {
  2186. MoveNext();
  2187. pGPO->Remove();
  2188. delete pGPO;
  2189. }
  2190. ResetEnd();
  2191. }
  2192. BOOL
  2193. CGPOInfoList::Add(
  2194. PGROUP_POLICY_OBJECT pGPOInfo
  2195. )
  2196. {
  2197. CGPOInfo * pGPO;
  2198. BOOL bStatus = FALSE;
  2199. pGPO = new CGPOInfo( pGPOInfo, bStatus );
  2200. if ( ! bStatus )
  2201. {
  2202. if ( pGPO )
  2203. delete pGPO;
  2204. pGPO = 0;
  2205. }
  2206. if ( ! pGPO )
  2207. return FALSE;
  2208. InsertFIFO( pGPO );
  2209. return TRUE;
  2210. }
  2211. CGPOInfo *
  2212. CGPOInfoList::Find(
  2213. WCHAR * pwszGPOId
  2214. )
  2215. {
  2216. CGPOInfo * pGPO;
  2217. pGPO = NULL;
  2218. for ( Reset(); pGPO = (CGPOInfo *) GetCurrentItem(); MoveNext() )
  2219. {
  2220. if ( lstrcmpi( pwszGPOId, pGPO->_pwszGPOId ) == 0 )
  2221. break;
  2222. }
  2223. ResetEnd();
  2224. return pGPO;
  2225. }
  2226. int
  2227. CGPOInfoList::Compare(
  2228. WCHAR * pwszGPOId1,
  2229. WCHAR * pwszGPOId2
  2230. )
  2231. {
  2232. CGPOInfo * pGPO;
  2233. int Index;
  2234. int Index1;
  2235. int Index2;
  2236. Index1 = Index2 = -1;
  2237. Reset();
  2238. Index = 0;
  2239. while ( pGPO = (CGPOInfo *) GetCurrentItem() )
  2240. {
  2241. if ( lstrcmpi( pGPO->_pwszGPOId, pwszGPOId1 ) == 0 )
  2242. Index1 = Index;
  2243. if ( lstrcmpi( pGPO->_pwszGPOId, pwszGPOId2 ) == 0 )
  2244. Index2 = Index;
  2245. MoveNext();
  2246. Index++;
  2247. }
  2248. ResetEnd();
  2249. if ( Index1 == Index2 )
  2250. return 0;
  2251. if ( Index1 < Index2 )
  2252. return -1;
  2253. else
  2254. return 1;
  2255. }
  2256. //
  2257. // CGPOInfo
  2258. //
  2259. CGPOInfo::CGPOInfo(
  2260. PGROUP_POLICY_OBJECT pGPOInfo,
  2261. BOOL & bStatus
  2262. )
  2263. {
  2264. bStatus = TRUE;
  2265. _pwszGPOId = StringDuplicate( (PWCHAR) pGPOInfo->szGPOName );
  2266. _pwszGPOName = StringDuplicate( (PWCHAR) pGPOInfo->lpDisplayName );
  2267. _pwszGPOPath = StringDuplicate( (PWCHAR) pGPOInfo->lpDSPath );
  2268. if ( pGPOInfo->lpLink )
  2269. {
  2270. _pwszSOMPath = StringDuplicate( StripLinkPrefix(pGPOInfo->lpLink) );
  2271. }
  2272. else
  2273. {
  2274. _pwszSOMPath = NULL;
  2275. }
  2276. if ( ! _pwszGPOId || ! _pwszGPOName || ! _pwszGPOPath || ! _pwszSOMPath )
  2277. bStatus = FALSE;
  2278. }
  2279. CGPOInfo::~CGPOInfo()
  2280. {
  2281. delete [] _pwszGPOId;
  2282. delete [] _pwszGPOName;
  2283. delete [] _pwszGPOPath;
  2284. delete [] _pwszSOMPath;
  2285. }
  2286. CRsopAppContext::CRsopAppContext(
  2287. DWORD dwContext,
  2288. HANDLE hEventAppsEnumerated, // = NULL
  2289. APPKEY* pAppType) : // = NULL
  2290. CRsopContext( APPMGMTEXTENSIONGUID ),
  2291. _dwContext( dwContext ),
  2292. _wszDemandSpec( NULL ),
  2293. _bTransition( FALSE ),
  2294. _dwInstallType( DEMAND_INSTALL_NONE ),
  2295. _bRemovalPurge( FALSE ),
  2296. _bRemoveGPOApps( FALSE ),
  2297. _bForcedRefresh( FALSE ),
  2298. _dwCurrentRsopVersion( 0 ),
  2299. _hEventAppsEnumerated( hEventAppsEnumerated ),
  2300. _StatusAbort( ERROR_SUCCESS )
  2301. {
  2302. WCHAR wszClsid[ MAX_SZGUID_LEN ];
  2303. WCHAR* wszDemandSpec;
  2304. if ( pAppType )
  2305. {
  2306. switch (pAppType->Type)
  2307. {
  2308. case FILEEXT :
  2309. _dwInstallType = DEMAND_INSTALL_FILEEXT;
  2310. wszDemandSpec = pAppType->uType.FileExt;
  2311. break;
  2312. case PROGID :
  2313. _dwInstallType = DEMAND_INSTALL_PROGID;
  2314. wszDemandSpec = pAppType->uType.ProgId;
  2315. break;
  2316. case COMCLASS :
  2317. _dwInstallType = DEMAND_INSTALL_CLSID;
  2318. GuidToString( pAppType->uType.COMClass.Clsid, wszClsid);
  2319. wszDemandSpec = wszClsid;
  2320. break;
  2321. case APPNAME :
  2322. _dwInstallType = DEMAND_INSTALL_NAME;
  2323. wszDemandSpec = NULL;
  2324. break;
  2325. default:
  2326. wszDemandSpec = NULL;
  2327. }
  2328. _wszDemandSpec = StringDuplicate( wszDemandSpec );
  2329. }
  2330. }
  2331. CRsopAppContext::~CRsopAppContext()
  2332. {
  2333. //
  2334. // If policy was aborted before we attempted to apply it,
  2335. // we will have logged only the applications that caused us to
  2336. // abort. So in this case, the rsop data is incomplete
  2337. //
  2338. if ( ERROR_SUCCESS != _StatusAbort )
  2339. {
  2340. HRESULT hr;
  2341. hr = HRESULT_FROM_WIN32( _StatusAbort );
  2342. //
  2343. // The rsop data is incomplete, so disable rsop with the
  2344. // error code below so that the administrator will know
  2345. // that the data are not complete.
  2346. //
  2347. (void) DisableRsop( hr );
  2348. }
  2349. }
  2350. void
  2351. CRsopAppContext::InitializeRsopContext(
  2352. HANDLE hUserToken,
  2353. HKEY hkUser,
  2354. BOOL bForcedRefresh,
  2355. BOOL* pbNoChanges)
  2356. {
  2357. //
  2358. // In planning mode, all initialization is
  2359. // already done, there is nothing to do here
  2360. //
  2361. if ( IsPlanningModeEnabled() )
  2362. {
  2363. return;
  2364. }
  2365. //
  2366. // To get an rsop namespace, we need to know the user's sid
  2367. //
  2368. PSID pSid;
  2369. BOOL bProfileConsistent;
  2370. pSid = NULL;
  2371. bProfileConsistent = TRUE;
  2372. //
  2373. // The token will only be non-NULL if we are in user policy
  2374. //
  2375. if ( hUserToken )
  2376. {
  2377. pSid = AppmgmtGetUserSid( hUserToken );
  2378. if ( ! pSid )
  2379. {
  2380. (void) DisableRsop( ERROR_OUTOFMEMORY );
  2381. return;
  2382. }
  2383. }
  2384. //
  2385. // Perform the base context initialization -- pSid will be NULL
  2386. // here if we are in machine policy, which is ok.
  2387. //
  2388. (void) InitializeContext( pSid );
  2389. if ( pSid )
  2390. {
  2391. (void) FreeSid( pSid );
  2392. DWORD dwMachineVersion;
  2393. DWORD dwUserVersion;
  2394. DWORD dwSize;
  2395. dwMachineVersion = 0;
  2396. dwUserVersion = 0;
  2397. dwSize = sizeof( dwMachineVersion );
  2398. //
  2399. // Read machine version
  2400. //
  2401. (void) RegQueryValueEx(
  2402. GetNameSpaceKey(),
  2403. RSOPVERSION,
  2404. NULL,
  2405. NULL,
  2406. (LPBYTE) &dwMachineVersion,
  2407. &dwSize);
  2408. dwSize = sizeof( dwUserVersion );
  2409. //
  2410. // Read user version
  2411. //
  2412. (void) RegQueryValueEx(
  2413. hkUser,
  2414. RSOPVERSION,
  2415. NULL,
  2416. NULL,
  2417. (LPBYTE) &dwUserVersion,
  2418. &dwSize);
  2419. //
  2420. // Always sync the current version for this machine to the profile's version
  2421. //
  2422. _dwCurrentRsopVersion = dwUserVersion;
  2423. bProfileConsistent = dwUserVersion == dwMachineVersion;
  2424. }
  2425. //
  2426. // In the policy refresh case, we are done initializing if
  2427. // a forced refresh isn't demanded
  2428. //
  2429. if ( ( CRsopAppContext::POLICY_REFRESH == GetContext() ) && ! bForcedRefresh )
  2430. {
  2431. //
  2432. // Force a refresh if the profile is not consitent with the machine's rsop
  2433. //
  2434. if ( *pbNoChanges && ! bProfileConsistent )
  2435. {
  2436. bForcedRefresh = TRUE;
  2437. *pbNoChanges = FALSE;
  2438. DebugMsg((DM_VERBOSE, IDS_CHANGES_RSOP_CHANGE));
  2439. }
  2440. else
  2441. {
  2442. return;
  2443. }
  2444. }
  2445. _bForcedRefresh = bForcedRefresh;
  2446. //
  2447. // In this case, the gp engine did not pass in a
  2448. // namespace for logging, either because we are executing outside of
  2449. // policy refresh context or because there were no changes and
  2450. // we decided to reapply policy regardless. Since the gp engine did
  2451. // not give us a namespace, we must initialize from a saved namespace
  2452. //
  2453. (void) InitializeSavedNameSpace();
  2454. }
  2455. HRESULT
  2456. CRsopAppContext::MoveAppContextState( CRsopAppContext* pRsopContext )
  2457. {
  2458. HRESULT hr;
  2459. hr = S_OK;
  2460. if ( pRsopContext->_wszDemandSpec )
  2461. {
  2462. _wszDemandSpec = StringDuplicate( pRsopContext->_wszDemandSpec );
  2463. if ( ! _wszDemandSpec )
  2464. {
  2465. hr = E_OUTOFMEMORY;
  2466. }
  2467. }
  2468. if ( SUCCEEDED( hr ) )
  2469. {
  2470. hr = MoveContextState( pRsopContext );
  2471. }
  2472. _dwContext = pRsopContext->_dwContext;
  2473. _dwInstallType = pRsopContext->_dwInstallType;
  2474. _bTransition = pRsopContext->_bTransition;
  2475. _bRemovalPurge = pRsopContext->_bRemovalPurge;
  2476. _bRemoveGPOApps = pRsopContext->_bRemoveGPOApps;
  2477. _bForcedRefresh = pRsopContext->_bForcedRefresh;
  2478. _dwCurrentRsopVersion = pRsopContext->_dwCurrentRsopVersion;
  2479. _hEventAppsEnumerated = pRsopContext->_hEventAppsEnumerated;
  2480. _StatusAbort = pRsopContext->_StatusAbort;
  2481. return hr;
  2482. }
  2483. HRESULT
  2484. CRsopAppContext::SetARPContext()
  2485. {
  2486. if ( ! IsPlanningModeEnabled() )
  2487. {
  2488. return E_INVALIDARG;
  2489. }
  2490. _dwContext = ARPLIST;
  2491. return S_OK;
  2492. }
  2493. DWORD
  2494. CRsopAppContext::WriteCurrentRsopVersion( HKEY hkUser )
  2495. {
  2496. if ( ! IsRsopEnabled() || IsPlanningModeEnabled() )
  2497. {
  2498. return ERROR_SUCCESS;
  2499. }
  2500. DWORD dwCurrentVersion;
  2501. LONG Status;
  2502. dwCurrentVersion = _dwCurrentRsopVersion + 1;
  2503. //
  2504. // Write the machine version
  2505. //
  2506. Status = RegSetValueEx(
  2507. GetNameSpaceKey(),
  2508. RSOPVERSION,
  2509. 0,
  2510. REG_DWORD,
  2511. (LPBYTE) &dwCurrentVersion,
  2512. sizeof( DWORD ) );
  2513. LONG StatusUser;
  2514. //
  2515. // Read user version
  2516. //
  2517. StatusUser = RegSetValueEx(
  2518. hkUser,
  2519. RSOPVERSION,
  2520. 0,
  2521. REG_DWORD,
  2522. (LPBYTE) &dwCurrentVersion,
  2523. sizeof( DWORD ) );
  2524. if ( ERROR_SUCCESS == Status )
  2525. {
  2526. Status = StatusUser;
  2527. }
  2528. return Status;
  2529. }
  2530. void
  2531. CRsopAppContext::SetPolicyAborted( DWORD Status )
  2532. {
  2533. if ( ERROR_SUCCESS == _StatusAbort )
  2534. {
  2535. _StatusAbort = Status;
  2536. }
  2537. }
  2538. BOOL
  2539. CRsopAppContext::HasPolicyAborted()
  2540. {
  2541. return ERROR_SUCCESS != _StatusAbort;
  2542. }
  2543. void
  2544. CRsopAppContext::SetAppsEnumerated()
  2545. {
  2546. if ( _hEventAppsEnumerated )
  2547. {
  2548. (void) SetEvent( _hEventAppsEnumerated );
  2549. }
  2550. }