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.

905 lines
19 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 <cominit.h>
  18. #include <wbemcli.h>
  19. #include <wbemint.h>
  20. #include <winntsec.h>
  21. #include <wbemcomn.h>
  22. #include <callsec.h>
  23. #include <cominit.h>
  24. #include <Guids.h>
  25. #include <BasicTree.h>
  26. #include <Thread.h>
  27. #include <Logging.h>
  28. #include "CGlobals.h"
  29. #include "Globals.h"
  30. /******************************************************************************
  31. *
  32. * Name:
  33. *
  34. *
  35. * Description:
  36. *
  37. *
  38. *****************************************************************************/
  39. WmiAllocator *DecoupledProviderSubSystem_Globals :: s_Allocator = NULL ;
  40. LONG DecoupledProviderSubSystem_Globals :: s_LocksInProgress = 0 ;
  41. LONG DecoupledProviderSubSystem_Globals :: s_ObjectsInProgress = 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_DecoupledClient_ObjectsInProgress = 0 ;
  46. LONG DecoupledProviderSubSystem_Globals :: s_CDecoupled_IWbemSyncObjectSink_ObjectsInProgress = 0 ;
  47. LONG DecoupledProviderSubSystem_Globals :: s_CDecoupled_Batching_IWbemSyncObjectSink_ObjectsInProgress = 0 ;
  48. LONG DecoupledProviderSubSystem_Globals :: s_CInterceptor_IWbemDecoupledUnboundObjectSink_ObjectsInProgress = 0 ;
  49. /******************************************************************************
  50. *
  51. * Name:
  52. *
  53. *
  54. * Description:
  55. *
  56. *
  57. *****************************************************************************/
  58. HRESULT DecoupledProviderSubSystem_Globals :: AdjustSecurityDescriptorWithSystemSid (
  59. SECURITY_DESCRIPTOR *&a_SecurityDescriptor ,
  60. SECURITY_DESCRIPTOR *&a_AlteredSecurityDescriptor
  61. )
  62. {
  63. HRESULT t_Result = S_OK ;
  64. SECURITY_DESCRIPTOR *t_SecurityDescriptor = NULL ;
  65. PACL t_Dacl = NULL ;
  66. PACL t_Sacl = NULL ;
  67. PSID t_Owner = NULL ;
  68. PSID t_PrimaryGroup = NULL ;
  69. SECURITY_DESCRIPTOR *t_AlteredSecurityDescriptor = NULL ;
  70. DWORD t_AlteredSecurityDescriptorSize = sizeof ( SECURITY_DESCRIPTOR ) ;
  71. DWORD t_DaclSize = 0 ;
  72. DWORD t_SaclSize = 0 ;
  73. DWORD t_OwnerSize = 0 ;
  74. DWORD t_PrimaryGroupSize = 0 ;
  75. BOOL t_Status = MakeAbsoluteSD (
  76. a_SecurityDescriptor ,
  77. t_AlteredSecurityDescriptor ,
  78. & t_AlteredSecurityDescriptorSize ,
  79. t_Dacl,
  80. & t_DaclSize,
  81. t_Sacl,
  82. & t_SaclSize,
  83. t_Owner,
  84. & t_OwnerSize,
  85. t_PrimaryGroup,
  86. & t_PrimaryGroupSize
  87. ) ;
  88. if ( ( t_Status == FALSE ) && GetLastError () == ERROR_INSUFFICIENT_BUFFER )
  89. {
  90. t_Dacl = ( PACL ) new BYTE [ t_DaclSize ] ;
  91. t_Sacl = ( PACL ) new BYTE [ t_SaclSize ] ;
  92. t_Owner = ( PSID ) new BYTE [ t_OwnerSize ] ;
  93. t_PrimaryGroup = ( PSID ) new BYTE [ t_PrimaryGroupSize ] ;
  94. t_AlteredSecurityDescriptor = ( SECURITY_DESCRIPTOR * ) new BYTE [ t_AlteredSecurityDescriptorSize ] ;
  95. if ( t_AlteredSecurityDescriptor && t_Dacl && t_Sacl && t_Owner && t_PrimaryGroup )
  96. {
  97. BOOL t_Status = InitializeSecurityDescriptor ( t_AlteredSecurityDescriptor , SECURITY_DESCRIPTOR_REVISION ) ;
  98. if ( t_Status )
  99. {
  100. t_Status = MakeAbsoluteSD (
  101. a_SecurityDescriptor ,
  102. t_AlteredSecurityDescriptor ,
  103. & t_AlteredSecurityDescriptorSize ,
  104. t_Dacl,
  105. & t_DaclSize,
  106. t_Sacl,
  107. & t_SaclSize,
  108. t_Owner,
  109. & t_OwnerSize,
  110. t_PrimaryGroup,
  111. & t_PrimaryGroupSize
  112. ) ;
  113. if ( t_Status )
  114. {
  115. t_SecurityDescriptor = t_AlteredSecurityDescriptor ;
  116. }
  117. else
  118. {
  119. t_Result = WBEM_E_CRITICAL_ERROR ;
  120. }
  121. }
  122. else
  123. {
  124. t_Result = WBEM_E_CRITICAL_ERROR ;
  125. }
  126. }
  127. else
  128. {
  129. t_Result = WBEM_E_OUT_OF_MEMORY ;
  130. }
  131. }
  132. else
  133. {
  134. t_Result = WBEM_E_CRITICAL_ERROR ;
  135. }
  136. PACL t_ExtraDacl = NULL ;
  137. if ( SUCCEEDED ( t_Result ) )
  138. {
  139. ACL_SIZE_INFORMATION t_Size ;
  140. if ( t_Dacl )
  141. {
  142. BOOL t_Status = GetAclInformation (
  143. t_Dacl ,
  144. & t_Size ,
  145. sizeof ( t_Size ) ,
  146. AclSizeInformation
  147. );
  148. if ( t_Status )
  149. {
  150. DWORD t_ExtraSize = t_Size.AclBytesInUse + t_Size.AclBytesFree + ProviderSubSystem_Common_Globals :: s_System_ACESize ;
  151. t_ExtraDacl = ( PACL ) new BYTE [ t_ExtraSize ] ;
  152. if ( t_ExtraDacl )
  153. {
  154. CopyMemory ( t_ExtraDacl , t_Dacl , t_Size.AclBytesInUse + t_Size.AclBytesFree ) ;
  155. t_ExtraDacl->AclSize = t_ExtraSize ;
  156. }
  157. else
  158. {
  159. t_Result = WBEM_E_OUT_OF_MEMORY ;
  160. }
  161. }
  162. else
  163. {
  164. t_Result = WBEM_E_CRITICAL_ERROR ;
  165. }
  166. DWORD t_AceIndex = 0 ;
  167. if ( SUCCEEDED ( t_Result ) )
  168. {
  169. if ( ProviderSubSystem_Common_Globals :: s_System_ACESize && :: AddAce ( t_ExtraDacl , ACL_REVISION , t_AceIndex , ProviderSubSystem_Common_Globals :: s_Token_All_Access_System_ACE , ProviderSubSystem_Common_Globals :: s_System_ACESize ) )
  170. {
  171. t_AceIndex ++ ;
  172. }
  173. else
  174. {
  175. t_Result = WBEM_E_CRITICAL_ERROR ;
  176. }
  177. }
  178. t_Status = SetSecurityDescriptorDacl (
  179. t_SecurityDescriptor ,
  180. TRUE ,
  181. t_ExtraDacl ,
  182. FALSE
  183. ) ;
  184. if ( ! t_Status )
  185. {
  186. t_Result = WBEM_E_CRITICAL_ERROR ;
  187. }
  188. }
  189. if ( SUCCEEDED ( t_Result ) )
  190. {
  191. DWORD t_FinalLength = 0 ;
  192. t_Status = MakeSelfRelativeSD (
  193. t_SecurityDescriptor ,
  194. a_AlteredSecurityDescriptor ,
  195. & t_FinalLength
  196. ) ;
  197. if ( t_Status == FALSE && GetLastError () == ERROR_INSUFFICIENT_BUFFER )
  198. {
  199. a_AlteredSecurityDescriptor = ( SECURITY_DESCRIPTOR * ) new BYTE [ t_FinalLength ] ;
  200. if ( a_AlteredSecurityDescriptor )
  201. {
  202. t_Status = MakeSelfRelativeSD (
  203. t_SecurityDescriptor ,
  204. a_AlteredSecurityDescriptor ,
  205. & t_FinalLength
  206. ) ;
  207. if ( t_Status == FALSE )
  208. {
  209. t_Result = WBEM_E_CRITICAL_ERROR ;
  210. }
  211. }
  212. else
  213. {
  214. t_Result = WBEM_E_OUT_OF_MEMORY ;
  215. }
  216. }
  217. }
  218. delete [] t_ExtraDacl ;
  219. }
  220. delete [] ( BYTE * ) t_Dacl ;
  221. delete [] ( BYTE * ) t_Sacl ;
  222. delete [] ( BYTE * ) t_Owner ;
  223. delete [] ( BYTE * ) t_PrimaryGroup ;
  224. delete [] ( BYTE * ) t_AlteredSecurityDescriptor ;
  225. return t_Result ;
  226. }
  227. /******************************************************************************
  228. *
  229. * Name:
  230. *
  231. *
  232. * Description:
  233. *
  234. *
  235. *****************************************************************************/
  236. HRESULT DecoupledProviderSubSystem_Globals :: OpenTokenToSystem ()
  237. {
  238. HANDLE t_ProcessToken ;
  239. BOOL t_Status = OpenProcessToken (
  240. GetCurrentProcess () ,
  241. TOKEN_QUERY | WRITE_DAC | READ_CONTROL ,
  242. & t_ProcessToken
  243. ) ;
  244. if ( t_Status )
  245. {
  246. HRESULT t_Result = S_OK ;
  247. SECURITY_DESCRIPTOR *t_SecurityDescriptor = NULL ;
  248. DWORD t_LengthRequested = 0 ;
  249. DWORD t_LengthReturned = 0 ;
  250. t_Status = GetKernelObjectSecurity (
  251. t_ProcessToken ,
  252. DACL_SECURITY_INFORMATION ,
  253. & t_SecurityDescriptor ,
  254. t_LengthRequested ,
  255. & t_LengthReturned
  256. ) ;
  257. if ( ( t_Status == FALSE ) && ( GetLastError () == ERROR_INSUFFICIENT_BUFFER ) )
  258. {
  259. t_SecurityDescriptor = ( SECURITY_DESCRIPTOR * ) new BYTE [ t_LengthReturned ] ;
  260. if ( t_SecurityDescriptor )
  261. {
  262. t_LengthRequested = t_LengthReturned ;
  263. t_Status = GetKernelObjectSecurity (
  264. t_ProcessToken ,
  265. DACL_SECURITY_INFORMATION ,
  266. t_SecurityDescriptor ,
  267. t_LengthRequested ,
  268. & t_LengthReturned
  269. ) ;
  270. if ( t_LengthRequested != t_LengthReturned )
  271. {
  272. t_Result = WBEM_E_UNEXPECTED ;
  273. }
  274. }
  275. else
  276. {
  277. t_Result = WBEM_E_OUT_OF_MEMORY ;
  278. }
  279. }
  280. else
  281. {
  282. t_Result = WBEM_E_ACCESS_DENIED ;
  283. }
  284. if ( SUCCEEDED ( t_Result ) )
  285. {
  286. SECURITY_DESCRIPTOR *t_AdjustedSecurityDescriptor = NULL ;
  287. t_Result = AdjustSecurityDescriptorWithSystemSid (
  288. t_SecurityDescriptor ,
  289. t_AdjustedSecurityDescriptor
  290. ) ;
  291. if ( SUCCEEDED ( t_Result ) )
  292. {
  293. BOOL t_Status = SetKernelObjectSecurity (
  294. t_ProcessToken ,
  295. DACL_SECURITY_INFORMATION ,
  296. t_AdjustedSecurityDescriptor
  297. ) ;
  298. if ( ! t_Status )
  299. {
  300. DWORD t_LastError = GetLastError () ;
  301. t_Result = WBEM_E_ACCESS_DENIED ;
  302. }
  303. delete [] t_AdjustedSecurityDescriptor ;
  304. }
  305. }
  306. CloseHandle ( t_ProcessToken ) ;
  307. delete [] t_SecurityDescriptor ;
  308. return t_Result ;
  309. }
  310. else
  311. {
  312. return WBEM_E_ACCESS_DENIED ;
  313. }
  314. }
  315. /******************************************************************************
  316. *
  317. * Name:
  318. *
  319. *
  320. * Description:
  321. *
  322. *
  323. *****************************************************************************/
  324. HRESULT DecoupledProviderSubSystem_Globals :: Global_Startup ()
  325. {
  326. HRESULT t_Result = S_OK ;
  327. if ( ! s_Allocator )
  328. {
  329. /*
  330. * Use the global process heap for this particular boot operation
  331. */
  332. WmiAllocator t_Allocator ;
  333. WmiStatusCode t_StatusCode = t_Allocator.New (
  334. ( void ** ) & s_Allocator ,
  335. sizeof ( WmiAllocator )
  336. ) ;
  337. if ( t_StatusCode == e_StatusCode_Success )
  338. {
  339. :: new ( ( void * ) s_Allocator ) WmiAllocator ;
  340. t_StatusCode = s_Allocator->Initialize () ;
  341. if ( t_StatusCode != e_StatusCode_Success )
  342. {
  343. t_Result = WBEM_E_OUT_OF_MEMORY ;
  344. }
  345. }
  346. else
  347. {
  348. t_Result = WBEM_E_OUT_OF_MEMORY ;
  349. }
  350. }
  351. if ( SUCCEEDED ( t_Result ) )
  352. {
  353. WmiStatusCode t_StatusCode = WmiThread <ULONG> :: Static_Initialize ( *s_Allocator ) ;
  354. }
  355. if ( SUCCEEDED ( t_Result ) )
  356. {
  357. t_Result = ProviderSubSystem_Common_Globals :: CreateSystemAces () ;
  358. }
  359. return t_Result ;
  360. }
  361. /******************************************************************************
  362. *
  363. * Name:
  364. *
  365. *
  366. * Description:
  367. *
  368. *
  369. *****************************************************************************/
  370. HRESULT DecoupledProviderSubSystem_Globals :: Global_Shutdown ()
  371. {
  372. HRESULT t_Result = ProviderSubSystem_Common_Globals :: DeleteSystemAces () ;
  373. WmiStatusCode t_StatusCode = WmiThread <ULONG> :: Static_UnInitialize ( *s_Allocator ) ;
  374. if ( s_Allocator )
  375. {
  376. /*
  377. * Use the global process heap for this particular boot operation
  378. */
  379. WmiAllocator t_Allocator ;
  380. WmiStatusCode t_StatusCode = t_Allocator.Delete (
  381. ( void * ) s_Allocator
  382. ) ;
  383. if ( t_StatusCode != e_StatusCode_Success )
  384. {
  385. t_Result = WBEM_E_OUT_OF_MEMORY ;
  386. }
  387. }
  388. return t_Result ;
  389. }
  390. /******************************************************************************
  391. *
  392. * Name:
  393. *
  394. *
  395. * Description:
  396. *
  397. *
  398. *****************************************************************************/
  399. void * __cdecl operator new ( size_t a_Size )
  400. {
  401. void *t_Ptr ;
  402. WmiStatusCode t_StatusCode = DecoupledProviderSubSystem_Globals :: s_Allocator->New (
  403. ( void ** ) & t_Ptr ,
  404. a_Size
  405. ) ;
  406. if ( t_StatusCode != e_StatusCode_Success )
  407. {
  408. throw Wmi_Heap_Exception (
  409. Wmi_Heap_Exception::HEAP_ERROR::E_ALLOCATION_ERROR
  410. ) ;
  411. }
  412. return t_Ptr ;
  413. }
  414. /******************************************************************************
  415. *
  416. * Name:
  417. *
  418. *
  419. * Description:
  420. *
  421. *
  422. *****************************************************************************/
  423. void __cdecl operator delete ( void *a_Ptr )
  424. {
  425. if ( a_Ptr )
  426. {
  427. WmiStatusCode t_StatusCode = DecoupledProviderSubSystem_Globals :: s_Allocator->Delete (
  428. ( void * ) a_Ptr
  429. ) ;
  430. }
  431. }
  432. /******************************************************************************
  433. *
  434. * Name:
  435. *
  436. *
  437. * Description:
  438. *
  439. *
  440. *****************************************************************************/
  441. HRESULT DecoupledProviderSubSystem_Globals :: BeginThreadImpersonation (
  442. IUnknown *&a_OldContext ,
  443. IServerSecurity *&a_OldSecurity ,
  444. BOOL &a_Impersonating
  445. )
  446. {
  447. HRESULT t_Result = S_OK ;
  448. IServerSecurity *t_ServerSecurity = NULL ;
  449. t_Result = CoGetCallContext ( IID_IUnknown , ( void ** ) & a_OldContext ) ;
  450. if ( SUCCEEDED ( t_Result ) )
  451. {
  452. t_Result = a_OldContext->QueryInterface ( IID_IServerSecurity , ( void ** ) & t_ServerSecurity ) ;
  453. if ( SUCCEEDED ( t_Result ) )
  454. {
  455. a_Impersonating = t_ServerSecurity->IsImpersonating () ;
  456. }
  457. else
  458. {
  459. a_Impersonating = FALSE ;
  460. }
  461. }
  462. CWbemCallSecurity *t_CallSecurity = CWbemCallSecurity :: New () ;
  463. if ( t_CallSecurity )
  464. {
  465. t_CallSecurity->AddRef () ;
  466. _IWmiThreadSecHandle *t_ThreadSecurity = NULL ;
  467. t_Result = t_CallSecurity->GetThreadSecurity ( ( WMI_THREAD_SECURITY_ORIGIN ) ( WMI_ORIGIN_THREAD ) , & t_ThreadSecurity ) ;
  468. if ( SUCCEEDED ( t_Result ) )
  469. {
  470. t_Result = t_CallSecurity->SetThreadSecurity ( t_ThreadSecurity ) ;
  471. if ( SUCCEEDED ( t_Result ) )
  472. {
  473. t_Result = t_CallSecurity->QueryInterface ( IID_IServerSecurity , ( void ** ) & a_OldSecurity ) ;
  474. if ( SUCCEEDED ( t_Result ) )
  475. {
  476. if ( a_Impersonating )
  477. {
  478. HRESULT t_TempResult = t_ServerSecurity->RevertToSelf () ;
  479. #if DBG
  480. if ( FAILED ( t_TempResult ) )
  481. {
  482. DebugBreak () ;
  483. }
  484. #endif
  485. }
  486. }
  487. }
  488. t_ThreadSecurity->Release () ;
  489. }
  490. t_CallSecurity->Release () ;
  491. }
  492. if ( t_ServerSecurity )
  493. {
  494. t_ServerSecurity->Release () ;
  495. }
  496. return t_Result ;
  497. }
  498. /******************************************************************************
  499. *
  500. * Name:
  501. *
  502. *
  503. * Description:
  504. *
  505. *
  506. *****************************************************************************/
  507. HRESULT DecoupledProviderSubSystem_Globals :: EndThreadImpersonation (
  508. IUnknown *a_OldContext ,
  509. IServerSecurity *a_OldSecurity ,
  510. BOOL a_Impersonating
  511. )
  512. {
  513. HRESULT t_Result = S_OK ;
  514. IUnknown *t_NewContext = NULL ;
  515. t_Result = CoSwitchCallContext ( a_OldContext , & t_NewContext ) ;
  516. if ( SUCCEEDED ( t_Result ) )
  517. {
  518. if ( a_OldContext )
  519. {
  520. if ( a_Impersonating )
  521. {
  522. IServerSecurity *t_ServerSecurity = NULL ;
  523. t_Result = a_OldContext->QueryInterface ( IID_IServerSecurity , ( void ** ) & t_ServerSecurity ) ;
  524. if ( SUCCEEDED ( t_Result ) )
  525. {
  526. t_Result = t_ServerSecurity->ImpersonateClient () ;
  527. t_ServerSecurity->Release () ;
  528. }
  529. }
  530. }
  531. if ( a_OldSecurity )
  532. {
  533. a_OldSecurity->Release() ;
  534. }
  535. }
  536. else
  537. {
  538. t_Result = WBEM_E_OUT_OF_MEMORY ;
  539. }
  540. /*
  541. * CoGetCallContext AddReffed this thing so now we have to release it.
  542. */
  543. if ( a_OldContext )
  544. {
  545. a_OldContext->Release () ;
  546. }
  547. return t_Result ;
  548. }
  549. /******************************************************************************
  550. *
  551. * Name:
  552. *
  553. *
  554. * Description:
  555. *
  556. *
  557. *****************************************************************************/
  558. HRESULT DecoupledProviderSubSystem_Globals :: Begin_IdentifyCall_PrvHost (
  559. WmiInternalContext a_InternalContext ,
  560. BOOL &a_Impersonating ,
  561. IUnknown *&a_OldContext ,
  562. IServerSecurity *&a_OldSecurity
  563. )
  564. {
  565. HRESULT t_Result = WBEM_E_INVALID_PARAMETER ;
  566. if ( a_InternalContext.m_IdentifyHandle )
  567. {
  568. HANDLE t_IdentifyToken = ( HANDLE ) a_InternalContext.m_IdentifyHandle ;
  569. BOOL t_Status = SetThreadToken ( NULL , t_IdentifyToken ) ;
  570. if ( t_Status )
  571. {
  572. t_Result = BeginThreadImpersonation ( a_OldContext , a_OldSecurity , a_Impersonating ) ;
  573. HRESULT t_TempResult = RevertToSelf () ;
  574. #if DBG
  575. if ( FAILED ( t_TempResult ) )
  576. {
  577. DebugBreak () ;
  578. }
  579. #endif
  580. }
  581. else
  582. {
  583. t_Result = WBEM_E_ACCESS_DENIED ;
  584. }
  585. CloseHandle ( t_IdentifyToken ) ;
  586. }
  587. return t_Result ;
  588. }
  589. /******************************************************************************
  590. *
  591. * Name:
  592. *
  593. *
  594. * Description:
  595. *
  596. *
  597. *****************************************************************************/
  598. HRESULT DecoupledProviderSubSystem_Globals :: End_IdentifyCall_PrvHost (
  599. WmiInternalContext a_InternalContext ,
  600. IUnknown *&a_OldContext ,
  601. IServerSecurity *&a_OldSecurity ,
  602. BOOL &a_Impersonating
  603. )
  604. {
  605. HRESULT t_Result = EndThreadImpersonation ( a_OldContext , a_OldSecurity , a_Impersonating ) ;
  606. #if DBG
  607. if ( FAILED ( t_Result ) )
  608. {
  609. DebugBreak () ;
  610. }
  611. #endif
  612. t_Result = RevertToSelf () ;
  613. #if DBG
  614. if ( FAILED ( t_Result ) )
  615. {
  616. DebugBreak () ;
  617. }
  618. #endif
  619. return S_OK ;
  620. }
  621. /******************************************************************************
  622. *
  623. * Name:
  624. *
  625. *
  626. * Description:
  627. *
  628. *
  629. *****************************************************************************/
  630. HRESULT DecoupledProviderSubSystem_Globals :: Begin_IdentifyCall_SvcHost (
  631. WmiInternalContext a_InternalContext ,
  632. BOOL &a_Impersonating ,
  633. IUnknown *&a_OldContext ,
  634. IServerSecurity *&a_OldSecurity
  635. )
  636. {
  637. HRESULT t_Result = WBEM_E_INVALID_PARAMETER ;
  638. if ( a_InternalContext.m_IdentifyHandle )
  639. {
  640. HANDLE t_IdentifyToken = NULL ;
  641. t_Result = CoImpersonateClient () ;
  642. if ( SUCCEEDED ( t_Result ) )
  643. {
  644. HANDLE t_CallerIdentifyToken = ( HANDLE ) a_InternalContext.m_IdentifyHandle ;
  645. DWORD t_ProcessIdentifier = a_InternalContext.m_ProcessIdentifier ;
  646. HANDLE t_ProcessHandle = OpenProcess (
  647. PROCESS_DUP_HANDLE ,
  648. FALSE ,
  649. t_ProcessIdentifier
  650. ) ;
  651. if ( t_ProcessHandle )
  652. {
  653. BOOL t_Status = DuplicateHandle (
  654. t_ProcessHandle ,
  655. t_CallerIdentifyToken ,
  656. GetCurrentProcess () ,
  657. & t_IdentifyToken ,
  658. MAXIMUM_ALLOWED | TOKEN_DUPLICATE | TOKEN_IMPERSONATE ,
  659. TRUE ,
  660. 0
  661. ) ;
  662. if ( t_Status )
  663. {
  664. }
  665. else
  666. {
  667. t_Result = WBEM_E_ACCESS_DENIED ;
  668. }
  669. CloseHandle ( t_ProcessHandle ) ;
  670. }
  671. else
  672. {
  673. t_Result = WBEM_E_ACCESS_DENIED ;
  674. }
  675. HRESULT t_TempResult = CoRevertToSelf () ;
  676. #if DBG
  677. if ( FAILED ( t_TempResult ) )
  678. {
  679. DebugBreak () ;
  680. }
  681. #endif
  682. }
  683. if ( SUCCEEDED ( t_Result ) )
  684. {
  685. BOOL t_Status = SetThreadToken ( NULL , t_IdentifyToken ) ;
  686. if ( t_Status )
  687. {
  688. t_Result = BeginThreadImpersonation ( a_OldContext , a_OldSecurity , a_Impersonating ) ;
  689. HRESULT t_TempResult = CoRevertToSelf () ;
  690. #if DBG
  691. if ( FAILED ( t_TempResult ) )
  692. {
  693. DebugBreak () ;
  694. }
  695. #endif
  696. t_TempResult = RevertToSelf () ;
  697. #if DBG
  698. if ( FAILED ( t_TempResult ) )
  699. {
  700. DebugBreak () ;
  701. }
  702. #endif
  703. }
  704. else
  705. {
  706. t_Result = WBEM_E_ACCESS_DENIED ;
  707. }
  708. CloseHandle ( t_IdentifyToken ) ;
  709. }
  710. }
  711. return t_Result ;
  712. }
  713. /******************************************************************************
  714. *
  715. * Name:
  716. *
  717. *
  718. * Description:
  719. *
  720. *
  721. *****************************************************************************/
  722. HRESULT DecoupledProviderSubSystem_Globals :: End_IdentifyCall_SvcHost (
  723. WmiInternalContext a_InternalContext ,
  724. IUnknown *&a_OldContext ,
  725. IServerSecurity *&a_OldSecurity ,
  726. BOOL &a_Impersonating
  727. )
  728. {
  729. HRESULT t_Result = EndThreadImpersonation ( a_OldContext , a_OldSecurity , a_Impersonating ) ;
  730. #if DBG
  731. if ( FAILED ( t_Result ) )
  732. {
  733. DebugBreak () ;
  734. }
  735. #endif
  736. t_Result = RevertToSelf () ;
  737. #if DBG
  738. if ( FAILED ( t_Result ) )
  739. {
  740. DebugBreak () ;
  741. }
  742. #endif
  743. return S_OK ;
  744. }