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.

932 lines
22 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. Globals.cpp
  5. Abstract:
  6. History:
  7. --*/
  8. #include "precomp.h"
  9. #include <windows.h>
  10. #include <objbase.h>
  11. #include <initguid.h>
  12. #ifndef INITGUID
  13. #define INITGUID
  14. #endif
  15. #include <wbemcli.h>
  16. #include <wbemint.h>
  17. #include <comdef.h>
  18. #include <strsafe.h>
  19. #include <winntsec.h>
  20. #include <callsec.h>
  21. #include <cominit.h>
  22. #include <Guids.h>
  23. #include <BasicTree.h>
  24. #include <Thread.h>
  25. #include <Logging.h>
  26. #include "Globals.h"
  27. #include "aggregator.h"
  28. #include "os.h"
  29. /******************************************************************************
  30. *
  31. * Name:
  32. *
  33. *
  34. * Description:
  35. *
  36. *
  37. *****************************************************************************/
  38. WmiAllocator *DecoupledProviderSubSystem_Globals :: s_Allocator = NULL ;
  39. LONG DecoupledProviderSubSystem_Globals :: s_LocksInProgress = 0 ;
  40. LONG DecoupledProviderSubSystem_Globals :: s_ObjectsInProgress = 0 ;
  41. LONG DecoupledProviderSubSystem_Globals :: s_RegistrarUsers = 0 ;
  42. LONG DecoupledProviderSubSystem_Globals :: s_CServerClassFactory_ObjectsInProgress = 0 ;
  43. LONG DecoupledProviderSubSystem_Globals :: s_CServerObject_ProviderRegistrar_ObjectsInProgress = 0 ;
  44. LONG DecoupledProviderSubSystem_Globals :: s_CServerObject_ProviderEvents_ObjectsInProgress = 0 ;
  45. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemSyncProvider_ObjectsInProgress = 0 ;
  46. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemServices_Stub_ObjectsInProgress = 0 ;
  47. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemProviderInitSink_ObjectsInProgress = 0 ;
  48. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemWaitingObjectSink_ObjectsInProgress = 0 ;
  49. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemObjectSink_ObjectsInProgress = 0 ;
  50. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemSyncObjectSink_ObjectsInProgress = 0 ;
  51. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemFilteringObjectSink_ObjectsInProgress = 0 ;
  52. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemSyncFilteringObjectSink_ObjectsInProgress = 0 ;
  53. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemObjectSinkEx_ObjectsInProgress = 0 ;
  54. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemSyncObjectSinkEx_ObjectsInProgress = 0 ;
  55. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemCombiningObjectSink_ObjectsInProgress = 0 ;
  56. LONG DecoupledProviderSubSystem_Globals :: s_CDecoupledAggregator_IWbemProvider_ObjectsInProgress=0;
  57. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemDecoupledUnboundObjectSink_ObjectsInProgress=0;
  58. LONG DecoupledProviderSubSystem_Globals :: s_CDecoupled_Batching_IWbemSyncObjectSink_ObjectsInProgress=0;
  59. LONG DecoupledProviderSubSystem_Globals :: s_CDecoupled_IWbemSyncObjectSink_ObjectsInProgress=0;
  60. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_DecoupledClient_ObjectsInProgress =0;
  61. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemDecoupledProvider_ObjectsInProgress = 0;
  62. LONG DecoupledProviderSubSystem_Globals :: s_CDecoupled_IWbemUnboundObjectSink_ObjectsInProgress=0;
  63. /******************************************************************************
  64. *
  65. * Name:
  66. *
  67. *
  68. * Description:
  69. *
  70. *
  71. *****************************************************************************/
  72. HRESULT DecoupledProviderSubSystem_Globals :: Global_Startup ()
  73. {
  74. HRESULT t_Result = S_OK ;
  75. if ( ! s_Allocator )
  76. {
  77. /*
  78. * Use the global process heap for this particular boot operation
  79. */
  80. WmiAllocator t_Allocator ;
  81. WmiStatusCode t_StatusCode = t_Allocator.New (
  82. ( void ** ) & s_Allocator ,
  83. sizeof ( WmiAllocator )
  84. ) ;
  85. if ( t_StatusCode == e_StatusCode_Success )
  86. {
  87. :: new ( ( void * ) s_Allocator ) WmiAllocator ;
  88. t_StatusCode = s_Allocator->Initialize () ;
  89. if ( t_StatusCode != e_StatusCode_Success )
  90. {
  91. t_Result = WBEM_E_OUT_OF_MEMORY ;
  92. }
  93. }
  94. else
  95. {
  96. t_Result = WBEM_E_OUT_OF_MEMORY ;
  97. }
  98. }
  99. if ( SUCCEEDED ( t_Result ) )
  100. {
  101. WmiStatusCode t_StatusCode = WmiThread <ULONG> :: Static_Initialize ( *s_Allocator ) ;
  102. }
  103. if ( SUCCEEDED ( t_Result ) )
  104. {
  105. t_Result = DecoupledProviderSubSystem_Globals::CreateSystemAces ();
  106. }
  107. if ( SUCCEEDED ( t_Result ) )
  108. {
  109. t_Result = ProviderSubSystem_Common_Globals :: CreateMethodSecurityDescriptor () ;
  110. }
  111. return t_Result ;
  112. }
  113. /******************************************************************************
  114. *
  115. * Name:
  116. *
  117. *
  118. * Description:
  119. *
  120. *
  121. *****************************************************************************/
  122. HRESULT DecoupledProviderSubSystem_Globals :: Global_Shutdown ()
  123. {
  124. HRESULT t_Result = S_OK ;
  125. WmiStatusCode t_StatusCode = WmiThread <ULONG> :: Static_UnInitialize ( *s_Allocator ) ;
  126. if ( s_Allocator )
  127. {
  128. WmiAllocator t_Allocator ;
  129. t_StatusCode = t_Allocator.Delete (
  130. ( void * ) s_Allocator
  131. ) ;
  132. }
  133. t_Result = DecoupledProviderSubSystem_Globals::DeleteSystemAces () ;
  134. t_Result = ProviderSubSystem_Common_Globals :: DeleteMethodSecurityDescriptor () ;
  135. return t_Result ;
  136. }
  137. HRESULT
  138. DecoupledProviderSubSystem_Globals::CreateSystemAces()
  139. {
  140. if (!OS::secureOS_) return S_OK;
  141. return ProviderSubSystem_Common_Globals::CreateSystemAces();
  142. };
  143. HRESULT
  144. DecoupledProviderSubSystem_Globals::DeleteSystemAces()
  145. {
  146. if (!OS::secureOS_)
  147. return S_OK;
  148. return ProviderSubSystem_Common_Globals::DeleteSystemAces();
  149. };
  150. /******************************************************************************
  151. *
  152. * Name:
  153. *
  154. *
  155. * Description:
  156. *
  157. *
  158. *****************************************************************************/
  159. HRESULT
  160. DecoupledProviderSubSystem_Globals::SetCloaking (
  161. IUnknown *a_Unknown ,
  162. DWORD a_AuthenticationLevel ,
  163. DWORD a_ImpersonationLevel)
  164. {
  165. if ( !OS::secureOS_) return S_OK;
  166. DWORD cloaking = (OS::osVer_ > OS::NT4) ? EOAC_DYNAMIC_CLOAKING : 0;
  167. DWORD impersonationLevel = (OS::osVer_ > OS::NT4) ? a_ImpersonationLevel : min(a_ImpersonationLevel,RPC_C_IMP_LEVEL_IDENTIFY) ;
  168. IClientSecurity *t_ClientSecurity = NULL ;
  169. HRESULT t_Result = a_Unknown->QueryInterface (
  170. IID_IClientSecurity ,
  171. ( void ** ) & t_ClientSecurity
  172. ) ;
  173. if ( SUCCEEDED ( t_Result ) )
  174. {
  175. t_Result = t_ClientSecurity->SetBlanket (
  176. a_Unknown ,
  177. RPC_C_AUTHN_WINNT ,
  178. RPC_C_AUTHZ_NONE ,
  179. NULL ,
  180. a_AuthenticationLevel ,
  181. impersonationLevel ,
  182. NULL ,
  183. cloaking
  184. ) ;
  185. t_ClientSecurity->Release () ;
  186. }
  187. return t_Result ;
  188. };
  189. HRESULT DecoupledProviderSubSystem_Globals :: BeginImpersonation (
  190. IUnknown *&a_OldContext ,
  191. IServerSecurity *&a_OldSecurity ,
  192. BOOL &a_Impersonating,
  193. DWORD *a_AuthenticationLevel
  194. )
  195. {
  196. if (!OS::secureOS_)
  197. return S_OK;
  198. HRESULT t_Result = S_OK ;
  199. IServerSecurity *t_ServerSecurity = NULL ;
  200. t_Result = CoGetCallContext ( IID_IUnknown , ( void ** ) & a_OldContext ) ;
  201. if ( SUCCEEDED ( t_Result ) )
  202. {
  203. t_Result = a_OldContext->QueryInterface ( IID_IServerSecurity , ( void ** ) & t_ServerSecurity ) ;
  204. if ( SUCCEEDED ( t_Result ) )
  205. {
  206. a_Impersonating = t_ServerSecurity->IsImpersonating () ;
  207. }
  208. else
  209. {
  210. a_Impersonating = FALSE ;
  211. }
  212. }
  213. CWbemCallSecurity * pSec = new CWbemCallSecurity(NULL);
  214. _IWmiCallSec *t_CallSecurity = NULL ;
  215. if (pSec == 0)
  216. t_Result = WBEM_E_OUT_OF_MEMORY;
  217. else
  218. t_Result = pSec->QueryInterface(IID__IWmiCallSec, ( void ** ) & t_CallSecurity);
  219. if ( SUCCEEDED ( t_Result ) )
  220. {
  221. _IWmiThreadSecHandle *t_ThreadSecurity = NULL ;
  222. t_Result = t_CallSecurity->GetThreadSecurity ( ( WMI_THREAD_SECURITY_ORIGIN ) ( WMI_ORIGIN_THREAD | WMI_ORIGIN_EXISTING | WMI_ORIGIN_RPC ) , & t_ThreadSecurity ) ;
  223. if ( SUCCEEDED ( t_Result ) )
  224. {
  225. t_Result = t_CallSecurity->SetThreadSecurity ( t_ThreadSecurity ) ;
  226. if ( SUCCEEDED ( t_Result ) )
  227. {
  228. t_Result = t_CallSecurity->QueryInterface ( IID_IServerSecurity , ( void ** ) & a_OldSecurity ) ;
  229. if ( SUCCEEDED ( t_Result ) )
  230. {
  231. if ( a_AuthenticationLevel )
  232. {
  233. t_Result = t_ThreadSecurity->GetAuthentication ( a_AuthenticationLevel ) ;
  234. }
  235. if ( a_Impersonating )
  236. {
  237. t_ServerSecurity->RevertToSelf () ;
  238. }
  239. }
  240. }
  241. t_ThreadSecurity->Release () ;
  242. }
  243. t_CallSecurity->Release () ;
  244. }
  245. if ( t_ServerSecurity )
  246. {
  247. t_ServerSecurity->Release () ;
  248. }
  249. return t_Result ;
  250. }
  251. HRESULT DecoupledProviderSubSystem_Globals :: BeginCallbackImpersonation (
  252. IUnknown *&a_OldContext ,
  253. IServerSecurity *&a_OldSecurity ,
  254. BOOL &a_Impersonating
  255. )
  256. {
  257. if (!OS::secureOS_)
  258. return S_OK;
  259. HRESULT t_Result = S_OK ;
  260. IServerSecurity *t_ServerSecurity = NULL ;
  261. t_Result = CoGetCallContext ( IID_IUnknown , ( void ** ) & a_OldContext ) ;
  262. if ( SUCCEEDED ( t_Result ) )
  263. {
  264. t_Result = a_OldContext->QueryInterface ( IID_IServerSecurity , ( void ** ) & t_ServerSecurity ) ;
  265. if ( SUCCEEDED ( t_Result ) )
  266. {
  267. a_Impersonating = t_ServerSecurity->IsImpersonating () ;
  268. }
  269. else
  270. {
  271. a_Impersonating = FALSE ;
  272. }
  273. }
  274. CWbemCallSecurity * pSec = new CWbemCallSecurity(NULL);
  275. _IWmiCallSec *t_CallSecurity = NULL ;
  276. if (pSec == 0)
  277. t_Result = WBEM_E_OUT_OF_MEMORY;
  278. else
  279. t_Result = pSec->QueryInterface(IID__IWmiCallSec, ( void ** ) & t_CallSecurity);
  280. if ( SUCCEEDED ( t_Result ) )
  281. {
  282. _IWmiThreadSecHandle *t_ThreadSecurity = NULL ;
  283. t_Result = t_CallSecurity->GetThreadSecurity ( ( WMI_THREAD_SECURITY_ORIGIN ) ( WMI_ORIGIN_THREAD ) , & t_ThreadSecurity ) ;
  284. if ( SUCCEEDED ( t_Result ) )
  285. {
  286. t_Result = t_CallSecurity->SetThreadSecurity ( t_ThreadSecurity ) ;
  287. if ( SUCCEEDED ( t_Result ) )
  288. {
  289. t_Result = t_CallSecurity->QueryInterface ( IID_IServerSecurity , ( void ** ) & a_OldSecurity ) ;
  290. if ( SUCCEEDED ( t_Result ) )
  291. {
  292. if ( a_Impersonating )
  293. {
  294. t_ServerSecurity->RevertToSelf () ;
  295. }
  296. }
  297. }
  298. t_ThreadSecurity->Release () ;
  299. }
  300. t_CallSecurity->Release () ;
  301. }
  302. if ( t_ServerSecurity )
  303. {
  304. t_ServerSecurity->Release () ;
  305. }
  306. return t_Result ;
  307. }
  308. /******************************************************************************
  309. *
  310. * Name:
  311. *
  312. *
  313. * Description:
  314. *
  315. *
  316. *****************************************************************************/
  317. HRESULT DecoupledProviderSubSystem_Globals :: BeginThreadImpersonation (
  318. IUnknown *&a_OldContext ,
  319. IServerSecurity *&a_OldSecurity ,
  320. BOOL &a_Impersonating
  321. )
  322. {
  323. HRESULT t_Result = S_OK ;
  324. IServerSecurity *t_ServerSecurity = NULL ;
  325. t_Result = CoGetCallContext ( IID_IUnknown , ( void ** ) & a_OldContext ) ;
  326. if ( SUCCEEDED ( t_Result ) )
  327. {
  328. t_Result = a_OldContext->QueryInterface ( IID_IServerSecurity , ( void ** ) & t_ServerSecurity ) ;
  329. if ( SUCCEEDED ( t_Result ) )
  330. {
  331. a_Impersonating = t_ServerSecurity->IsImpersonating () ;
  332. }
  333. else
  334. {
  335. a_Impersonating = FALSE ;
  336. }
  337. }
  338. CWbemCallSecurity *t_CallSecurity = CWbemCallSecurity :: New () ;
  339. if ( t_CallSecurity )
  340. {
  341. t_CallSecurity->AddRef () ;
  342. _IWmiThreadSecHandle *t_ThreadSecurity = NULL ;
  343. t_Result = t_CallSecurity->GetThreadSecurity ( ( WMI_THREAD_SECURITY_ORIGIN ) ( WMI_ORIGIN_THREAD ) , & t_ThreadSecurity ) ;
  344. if ( SUCCEEDED ( t_Result ) )
  345. {
  346. t_Result = t_CallSecurity->SetThreadSecurity ( t_ThreadSecurity ) ;
  347. if ( SUCCEEDED ( t_Result ) )
  348. {
  349. t_Result = t_CallSecurity->QueryInterface ( IID_IServerSecurity , ( void ** ) & a_OldSecurity ) ;
  350. if ( SUCCEEDED ( t_Result ) )
  351. {
  352. if ( a_Impersonating )
  353. {
  354. t_ServerSecurity->RevertToSelf () ;
  355. }
  356. }
  357. }
  358. t_ThreadSecurity->Release () ;
  359. }
  360. t_CallSecurity->Release () ;
  361. }
  362. if ( t_ServerSecurity )
  363. {
  364. t_ServerSecurity->Release () ;
  365. }
  366. return t_Result ;
  367. }
  368. /******************************************************************************
  369. *
  370. * Name:
  371. *
  372. *
  373. * Description:
  374. *
  375. *
  376. *****************************************************************************/
  377. HRESULT DecoupledProviderSubSystem_Globals :: EndThreadImpersonation (
  378. IUnknown *a_OldContext ,
  379. IServerSecurity *a_OldSecurity ,
  380. BOOL a_Impersonating
  381. )
  382. {
  383. HRESULT t_Result = S_OK ;
  384. IUnknown *t_NewContext = NULL ;
  385. t_Result = CoSwitchCallContext ( a_OldContext , & t_NewContext ) ;
  386. if ( SUCCEEDED ( t_Result ) )
  387. {
  388. if ( a_OldContext )
  389. {
  390. if ( a_Impersonating )
  391. {
  392. IServerSecurity *t_ServerSecurity = NULL ;
  393. t_Result = a_OldContext->QueryInterface ( IID_IServerSecurity , ( void ** ) & t_ServerSecurity ) ;
  394. if ( SUCCEEDED ( t_Result ) )
  395. {
  396. t_ServerSecurity->ImpersonateClient () ;
  397. t_ServerSecurity->Release () ;
  398. }
  399. }
  400. }
  401. if ( a_OldSecurity )
  402. {
  403. a_OldSecurity->Release() ;
  404. }
  405. }
  406. else
  407. {
  408. t_Result = WBEM_E_OUT_OF_MEMORY ;
  409. }
  410. /*
  411. * CoGetCallContext AddReffed this thing so now we have to release it.
  412. */
  413. if ( a_OldContext )
  414. {
  415. a_OldContext->Release () ;
  416. }
  417. return t_Result ;
  418. }
  419. /******************************************************************************
  420. *
  421. * Name:
  422. *
  423. *
  424. * Description:
  425. *
  426. *
  427. *****************************************************************************/
  428. HRESULT DecoupledProviderSubSystem_Globals :: Begin_IdentifyCall_PrvHost (
  429. WmiInternalContext a_InternalContext ,
  430. BOOL &a_Impersonating ,
  431. IUnknown *&a_OldContext ,
  432. IServerSecurity *&a_OldSecurity
  433. )
  434. {
  435. if (!OS::secureOS_)
  436. return S_OK;
  437. HRESULT t_Result = WBEM_E_INVALID_PARAMETER ;
  438. if ( a_InternalContext.m_IdentifyHandle )
  439. {
  440. HANDLE t_IdentifyToken = ( HANDLE ) a_InternalContext.m_IdentifyHandle ;
  441. BOOL t_Status = SetThreadToken ( NULL , t_IdentifyToken ) ;
  442. if ( t_Status )
  443. {
  444. t_Result = BeginThreadImpersonation ( a_OldContext , a_OldSecurity , a_Impersonating ) ;
  445. RevertToSelf () ;
  446. }
  447. else
  448. {
  449. t_Result = WBEM_E_ACCESS_DENIED ;
  450. }
  451. CloseHandle ( t_IdentifyToken ) ;
  452. }
  453. return t_Result ;
  454. }
  455. /******************************************************************************
  456. *
  457. * Name:
  458. *
  459. *
  460. * Description:
  461. *
  462. *
  463. *****************************************************************************/
  464. HRESULT DecoupledProviderSubSystem_Globals :: End_IdentifyCall_PrvHost (
  465. WmiInternalContext a_InternalContext ,
  466. IUnknown *&a_OldContext ,
  467. IServerSecurity *&a_OldSecurity ,
  468. BOOL &a_Impersonating
  469. )
  470. {
  471. if (!OS::secureOS_)
  472. return S_OK;
  473. EndThreadImpersonation ( a_OldContext , a_OldSecurity , a_Impersonating ) ;
  474. RevertToSelf () ;
  475. return S_OK ;
  476. }
  477. /******************************************************************************
  478. *
  479. * Name:
  480. *
  481. *
  482. * Description:
  483. *
  484. *
  485. *****************************************************************************/
  486. HRESULT DecoupledProviderSubSystem_Globals :: SetProxyState_SvcHost (
  487. ProxyContainer &a_Container ,
  488. ULONG a_ProxyIndex ,
  489. REFIID a_InterfaceId ,
  490. IUnknown *a_Interface ,
  491. IUnknown *&a_Proxy ,
  492. BOOL &a_Revert ,
  493. DWORD a_ProcessIdentifier ,
  494. HANDLE &a_IdentifyToken ,
  495. ACCESS_ALLOWED_ACE *a_Ace ,
  496. WORD a_AceSize,
  497. SECURITY_IMPERSONATION_LEVEL t_ImpersonationLevel
  498. )
  499. {
  500. if (!OS::secureOS_)
  501. return S_OK;
  502. a_Revert = FALSE ;
  503. HRESULT t_Result = ProviderSubSystem_Common_Globals::GetProxy ( a_Container , a_ProxyIndex , a_InterfaceId , a_Interface , a_Proxy ) ;
  504. if ( SUCCEEDED ( t_Result ) )
  505. {
  506. t_Result = CoImpersonateClient () ;
  507. if ( SUCCEEDED ( t_Result ) )
  508. {
  509. a_Revert = TRUE ;
  510. // At this point, our thread token contains all the privileges that the
  511. // client has enabled for us; however, those privileges are not enabled.
  512. // Since we are calling into a proxied provider, we need to enable all
  513. // these privileges so that they would propagate to the provider
  514. // =====================================================================
  515. HRESULT t_TempResult = ProviderSubSystem_Common_Globals::EnableAllPrivileges () ;
  516. // Get the token's impersonation level
  517. // ===================================
  518. if ( (t_ImpersonationLevel == SecurityImpersonation || t_ImpersonationLevel == SecurityDelegation ) && (OS::osVer_ > OS::NT4) )
  519. {
  520. a_IdentifyToken = 0 ;
  521. }
  522. else
  523. {
  524. t_Result = ProviderSubSystem_Common_Globals::ConstructIdentifyToken_SvcHost (
  525. a_Revert ,
  526. a_ProcessIdentifier ,
  527. a_IdentifyToken ,
  528. a_Ace ,
  529. a_AceSize,
  530. t_ImpersonationLevel
  531. ) ;
  532. if ( SUCCEEDED ( t_Result ) )
  533. {
  534. t_Result = SetInterfaceSecurity (
  535. a_Proxy ,
  536. NULL ,
  537. NULL ,
  538. NULL ,
  539. DWORD(RPC_C_AUTHN_LEVEL_DEFAULT),
  540. RPC_C_IMP_LEVEL_IDENTIFY
  541. ) ;
  542. }
  543. }
  544. }
  545. else
  546. {
  547. t_Result = WBEM_E_ACCESS_DENIED ;
  548. }
  549. }
  550. else
  551. {
  552. if ( t_Result == WBEM_E_NOT_FOUND )
  553. {
  554. }
  555. else
  556. {
  557. t_Result = WBEM_E_CRITICAL_ERROR ;
  558. }
  559. }
  560. return t_Result ;
  561. }
  562. /******************************************************************************
  563. *
  564. * Name:
  565. *
  566. *
  567. * Description:
  568. *
  569. *
  570. *****************************************************************************/
  571. HRESULT DecoupledProviderSubSystem_Globals :: RevertProxyState_SvcHost (
  572. ProxyContainer &a_Container ,
  573. ULONG a_ProxyIndex ,
  574. IUnknown *a_Proxy ,
  575. BOOL a_Revert ,
  576. DWORD a_ProcessIdentifier ,
  577. HANDLE a_IdentifyToken
  578. )
  579. {
  580. if (!OS::secureOS_)
  581. return S_OK;
  582. HRESULT t_Result = S_OK ;
  583. WmiHelper :: EnterCriticalSection ( & a_Container.GetCriticalSection () ) ;
  584. WmiStatusCode t_StatusCode = a_Container.Return ( a_Proxy , a_ProxyIndex ) ;
  585. if ( t_StatusCode == e_StatusCode_Success )
  586. {
  587. }
  588. else
  589. {
  590. a_Proxy->Release () ;
  591. t_Result = WBEM_E_OUT_OF_MEMORY ;
  592. }
  593. WmiHelper :: LeaveCriticalSection ( & a_Container.GetCriticalSection () ) ;
  594. if ( a_Revert )
  595. {
  596. t_Result = CoRevertToSelf () ;
  597. }
  598. return t_Result ;
  599. }
  600. /******************************************************************************
  601. *
  602. * Name:
  603. *
  604. *
  605. * Description:
  606. *
  607. *
  608. *****************************************************************************/
  609. HRESULT DecoupledProviderSubSystem_Globals :: GetAceWithProcessTokenUser (
  610. DWORD a_ProcessIdentifier ,
  611. WORD &a_AceSize ,
  612. ACCESS_ALLOWED_ACE *&a_Ace
  613. )
  614. {
  615. if (!OS::secureOS_)
  616. return S_OK;
  617. HRESULT t_Result = WBEM_E_ACCESS_DENIED ;
  618. HANDLE t_ProcessHandle = OpenProcess (
  619. MAXIMUM_ALLOWED ,
  620. FALSE ,
  621. a_ProcessIdentifier
  622. ) ;
  623. if ( t_ProcessHandle )
  624. {
  625. HANDLE t_ProcessToken = NULL ;
  626. BOOL t_Status = OpenProcessToken (
  627. t_ProcessHandle ,
  628. TOKEN_QUERY | TOKEN_DUPLICATE ,
  629. & t_ProcessToken
  630. ) ;
  631. if ( t_Status )
  632. {
  633. DWORD t_OwnerSize = 0 ;
  634. PSID t_OwnerSid = NULL ;
  635. BOOL t_OwnerDefaulted = FALSE ;
  636. t_Result = GetUserSid (
  637. t_ProcessToken ,
  638. & t_OwnerSize ,
  639. t_OwnerSid
  640. ) ;
  641. if ( SUCCEEDED ( t_Result ) )
  642. {
  643. ACCESS_ALLOWED_ACE *t_Ace = NULL ;
  644. DWORD t_AceSize = 0 ;
  645. t_AceSize = sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_OwnerSize - sizeof(DWORD) ) ;
  646. t_Ace = (ACCESS_ALLOWED_ACE*) new BYTE [ t_AceSize ] ;
  647. if ( t_Ace )
  648. {
  649. CopySid ( t_OwnerSize, (PSID) & t_Ace->SidStart, t_OwnerSid ) ;
  650. t_Ace->Mask = TOKEN_ALL_ACCESS ;
  651. t_Ace->Header.AceType = 0 ;
  652. t_Ace->Header.AceFlags = 0 ;
  653. t_Ace->Header.AceSize = t_AceSize ;
  654. a_Ace = t_Ace ;
  655. a_AceSize = t_AceSize ;
  656. }
  657. else
  658. {
  659. t_Result = WBEM_E_OUT_OF_MEMORY ;
  660. }
  661. delete [] ( BYTE * ) t_OwnerSid ;
  662. }
  663. CloseHandle ( t_ProcessToken ) ;
  664. }
  665. CloseHandle ( t_ProcessHandle ) ;
  666. }
  667. else
  668. {
  669. t_Result = WBEM_E_TRANSPORT_FAILURE;
  670. }
  671. return t_Result ;
  672. }
  673. /******************************************************************************
  674. *
  675. * Name:
  676. *
  677. *
  678. * Description:
  679. *
  680. *
  681. *****************************************************************************/
  682. HRESULT DecoupledProviderSubSystem_Globals :: GetUserSid (
  683. HANDLE a_Token ,
  684. ULONG *a_Size ,
  685. PSID &a_Sid
  686. )
  687. {
  688. if (!OS::secureOS_)
  689. return S_OK;
  690. HRESULT t_Result = WBEM_E_FAILED ;
  691. if ( a_Token )
  692. {
  693. if ( a_Size )
  694. {
  695. TOKEN_USER *t_TokenUser = NULL ;
  696. DWORD t_ReturnLength = 0 ;
  697. TOKEN_INFORMATION_CLASS t_TokenInformationClass = TokenUser ;
  698. BOOL t_TokenStatus = GetTokenInformation (
  699. a_Token ,
  700. t_TokenInformationClass ,
  701. t_TokenUser ,
  702. t_ReturnLength ,
  703. & t_ReturnLength
  704. ) ;
  705. if ( ! t_TokenStatus )
  706. {
  707. DWORD t_LastError = GetLastError () ;
  708. switch ( t_LastError )
  709. {
  710. case ERROR_INSUFFICIENT_BUFFER:
  711. {
  712. t_TokenUser = ( TOKEN_USER * ) new BYTE [ t_ReturnLength ] ;
  713. if ( t_TokenUser )
  714. {
  715. t_TokenStatus = GetTokenInformation (
  716. a_Token ,
  717. t_TokenInformationClass ,
  718. t_TokenUser ,
  719. t_ReturnLength ,
  720. & t_ReturnLength
  721. ) ;
  722. if ( t_TokenStatus )
  723. {
  724. DWORD t_SidLength = GetLengthSid ( t_TokenUser->User.Sid ) ;
  725. *a_Size = t_SidLength ;
  726. a_Sid = new BYTE [ t_SidLength ] ;
  727. if ( a_Sid )
  728. {
  729. CopyMemory ( a_Sid , t_TokenUser->User.Sid , t_SidLength ) ;
  730. t_Result = S_OK ;
  731. }
  732. else
  733. {
  734. t_Result = WBEM_E_OUT_OF_MEMORY ;
  735. }
  736. }
  737. delete [] t_TokenUser ;
  738. }
  739. else
  740. {
  741. t_Result = WBEM_E_OUT_OF_MEMORY ;
  742. }
  743. }
  744. break ;
  745. default:
  746. {
  747. }
  748. break ;
  749. }
  750. }
  751. }
  752. else
  753. {
  754. t_Result = WBEM_E_INVALID_PARAMETER ;
  755. }
  756. }
  757. else
  758. {
  759. t_Result = ( ERROR_INVALID_HANDLE | 0x80070000 ) ;
  760. }
  761. return t_Result ;
  762. }