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.

1439 lines
30 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. ProvSubS.cpp
  5. Abstract:
  6. History:
  7. --*/
  8. #include <precomp.h>
  9. #include <wbemint.h>
  10. #include <HelperFuncs.h>
  11. #include <Logging.h>
  12. #include <CGlobals.h>
  13. #include "Globals.h"
  14. #include "ClassFac.h"
  15. #include "ProvInSk.h"
  16. #include "ProvLoad.h"
  17. #include "ProvHost.h"
  18. #include "ProvCache.h"
  19. #include "ProvRMgr.h"
  20. #include "Guids.h"
  21. #include <tpwrap.cpp>
  22. #ifdef WMIASLOCAL
  23. #include "Main.h"
  24. #endif
  25. /******************************************************************************
  26. *
  27. * Name:
  28. *
  29. *
  30. * Description:
  31. *
  32. *
  33. *****************************************************************************/
  34. HRESULT CServerObject_HostInterceptor :: AbnormalShutdown (
  35. HostCacheKey &a_Key
  36. )
  37. {
  38. WmiStatusCode t_StatusCode = ProviderSubSystem_Globals :: GetHostController ()->Shutdown ( a_Key ) ;
  39. if ( t_StatusCode == e_StatusCode_Success )
  40. {
  41. }
  42. return S_OK ;
  43. }
  44. /******************************************************************************
  45. *
  46. * Name:
  47. *
  48. *
  49. * Description:
  50. *
  51. *
  52. *****************************************************************************/
  53. HRESULT CServerObject_HostInterceptor :: CreateUsingToken (
  54. HostCacheKey &a_Key ,
  55. _IWmiProviderHost **a_Host ,
  56. _IWbemRefresherMgr **a_RefresherManager ,
  57. IWbemContext *a_Context
  58. )
  59. {
  60. HRESULT t_Result = FindHost (
  61. a_Context ,
  62. a_Key ,
  63. IID__IWmiProviderHost ,
  64. ( void ** ) a_Host
  65. ) ;
  66. if ( SUCCEEDED ( t_Result ) )
  67. {
  68. t_Result = (*a_Host)->GetObject (
  69. CLSID__WbemHostedRefresherMgr ,
  70. 0 ,
  71. NULL ,
  72. IID__IWbemRefresherMgr,
  73. ( void ** ) a_RefresherManager
  74. ) ;
  75. if ( FAILED ( t_Result ) )
  76. {
  77. if ( ( HRESULT_ERROR_FUNC(t_Result) == RPC_S_SERVER_UNAVAILABLE ) || ( HRESULT_ERROR_FUNC(t_Result) == RPC_S_CALL_FAILED_DNE ) )
  78. {
  79. AbnormalShutdown ( a_Key ) ;
  80. }
  81. }
  82. }
  83. return t_Result ;
  84. }
  85. /******************************************************************************
  86. *
  87. * Name:
  88. *
  89. *
  90. * Description:
  91. *
  92. *
  93. *****************************************************************************/
  94. HRESULT CServerObject_HostInterceptor :: CreateUsingAccount (
  95. HostCacheKey &a_Key ,
  96. LPWSTR a_User ,
  97. LPWSTR a_Domain ,
  98. _IWmiProviderHost **a_Host ,
  99. _IWbemRefresherMgr **a_RefresherManager ,
  100. IWbemContext *a_Context
  101. )
  102. {
  103. HRESULT t_Result = S_OK ;
  104. /*
  105. * Revert to LocalService
  106. */
  107. CoRevertToSelf () ;
  108. /*
  109. * Logon the system defined Account
  110. */
  111. HANDLE t_ThreadToken = NULL ;
  112. BOOL t_Status = LogonUser (
  113. a_User ,
  114. a_Domain ,
  115. NULL ,
  116. LOGON32_LOGON_SERVICE ,
  117. LOGON32_PROVIDER_WINNT50,
  118. & t_ThreadToken
  119. ) ;
  120. if ( t_Status )
  121. {
  122. t_Status = ImpersonateLoggedOnUser ( t_ThreadToken ) ;
  123. if ( t_Status )
  124. {
  125. ProviderSubSystem_Common_Globals :: EnableAllPrivileges () ;
  126. }
  127. else
  128. {
  129. t_Result = WBEM_E_ACCESS_DENIED ;
  130. DWORD t_LastError = GetLastError () ;
  131. }
  132. }
  133. else
  134. {
  135. t_Result = WBEM_E_ACCESS_DENIED ;
  136. DWORD t_LastError = GetLastError () ;
  137. }
  138. if ( SUCCEEDED ( t_Result ) )
  139. {
  140. t_Result = FindHost (
  141. a_Context ,
  142. a_Key ,
  143. IID__IWmiProviderHost ,
  144. ( void ** ) a_Host
  145. ) ;
  146. }
  147. RevertToSelf () ;
  148. if ( t_ThreadToken )
  149. {
  150. CloseHandle ( t_ThreadToken ) ;
  151. }
  152. if ( SUCCEEDED ( t_Result ) )
  153. {
  154. t_Result = (*a_Host)->GetObject (
  155. CLSID__WbemHostedRefresherMgr ,
  156. 0 ,
  157. NULL ,
  158. IID__IWbemRefresherMgr,
  159. ( void ** ) a_RefresherManager
  160. ) ;
  161. if ( FAILED ( t_Result ) )
  162. {
  163. if ( ( HRESULT_ERROR_FUNC(t_Result) == RPC_S_SERVER_UNAVAILABLE ) || ( HRESULT_ERROR_FUNC(t_Result) == RPC_S_CALL_FAILED_DNE ) )
  164. {
  165. AbnormalShutdown ( a_Key ) ;
  166. }
  167. }
  168. }
  169. return t_Result ;
  170. }
  171. /******************************************************************************
  172. *
  173. * Name:
  174. *
  175. *
  176. * Description:
  177. *
  178. *
  179. *****************************************************************************/
  180. HRESULT CServerObject_HostInterceptor :: CreateUsingToken (
  181. HostCacheKey &a_Key ,
  182. _IWmiProviderHost **a_Host ,
  183. _IWmiProviderFactory **a_Factory ,
  184. IWbemContext *a_Context
  185. )
  186. {
  187. HRESULT t_Result = FindHost (
  188. a_Context ,
  189. a_Key ,
  190. IID__IWmiProviderHost ,
  191. ( void ** ) a_Host
  192. ) ;
  193. if ( SUCCEEDED ( t_Result ) )
  194. {
  195. t_Result = (*a_Host)->GetObject (
  196. CLSID_WmiProviderHostedInProcFactory ,
  197. 0 ,
  198. NULL ,
  199. IID__IWmiProviderFactory,
  200. ( void ** ) a_Factory
  201. ) ;
  202. if ( FAILED ( t_Result ) )
  203. {
  204. if ( ( HRESULT_ERROR_FUNC(t_Result) == RPC_S_SERVER_UNAVAILABLE ) || ( HRESULT_ERROR_FUNC(t_Result) == RPC_S_CALL_FAILED_DNE ) )
  205. {
  206. AbnormalShutdown ( a_Key ) ;
  207. }
  208. }
  209. }
  210. return t_Result ;
  211. }
  212. /******************************************************************************
  213. *
  214. * Name:
  215. *
  216. *
  217. * Description:
  218. *
  219. *
  220. *****************************************************************************/
  221. HRESULT CServerObject_HostInterceptor :: CreateUsingAccount (
  222. HostCacheKey &a_Key ,
  223. LPWSTR a_User ,
  224. LPWSTR a_Domain ,
  225. _IWmiProviderHost **a_Host ,
  226. _IWmiProviderFactory **a_Factory ,
  227. IWbemContext *a_Context
  228. )
  229. {
  230. HRESULT t_Result = S_OK ;
  231. /*
  232. * Revert to LocalService
  233. */
  234. CoRevertToSelf () ;
  235. /*
  236. * Logon the system defined Account
  237. */
  238. HANDLE t_ThreadToken = NULL ;
  239. BOOL t_Status = LogonUser (
  240. a_User ,
  241. a_Domain ,
  242. NULL ,
  243. LOGON32_LOGON_SERVICE ,
  244. LOGON32_PROVIDER_WINNT50,
  245. & t_ThreadToken
  246. ) ;
  247. if ( t_Status )
  248. {
  249. t_Status = ImpersonateLoggedOnUser ( t_ThreadToken ) ;
  250. if ( t_Status )
  251. {
  252. ProviderSubSystem_Common_Globals :: EnableAllPrivileges () ;
  253. }
  254. else
  255. {
  256. t_Result = WBEM_E_ACCESS_DENIED ;
  257. DWORD t_LastError = GetLastError () ;
  258. }
  259. }
  260. else
  261. {
  262. t_Result = WBEM_E_ACCESS_DENIED ;
  263. DWORD t_LastError = GetLastError () ;
  264. }
  265. if ( SUCCEEDED ( t_Result ) )
  266. {
  267. t_Result = FindHost (
  268. a_Context ,
  269. a_Key ,
  270. IID__IWmiProviderHost ,
  271. ( void ** ) a_Host
  272. ) ;
  273. }
  274. RevertToSelf () ;
  275. if ( t_ThreadToken )
  276. {
  277. CloseHandle ( t_ThreadToken ) ;
  278. }
  279. if ( SUCCEEDED ( t_Result ) )
  280. {
  281. t_Result = (*a_Host)->GetObject (
  282. CLSID_WmiProviderHostedInProcFactory ,
  283. 0 ,
  284. NULL ,
  285. IID__IWmiProviderFactory,
  286. ( void ** ) a_Factory
  287. ) ;
  288. if ( FAILED ( t_Result ) )
  289. {
  290. if ( ( HRESULT_ERROR_FUNC(t_Result) == RPC_S_SERVER_UNAVAILABLE ) || ( HRESULT_ERROR_FUNC(t_Result) == RPC_S_CALL_FAILED_DNE ) )
  291. {
  292. AbnormalShutdown ( a_Key ) ;
  293. }
  294. }
  295. }
  296. return t_Result ;
  297. }
  298. /******************************************************************************
  299. *
  300. * Name:
  301. *
  302. *
  303. * Description:
  304. *
  305. *
  306. *****************************************************************************/
  307. HRESULT CServerObject_HostInterceptor :: FindHost (
  308. IWbemContext *a_Context ,
  309. HostCacheKey &a_Key ,
  310. REFIID a_RIID ,
  311. void **a_Interface
  312. )
  313. {
  314. HRESULT t_Result = S_OK;
  315. *a_Interface = NULL ;
  316. ProviderSubSystem_Globals :: GetHostController ()->Lock () ;
  317. CWbemGlobal_IWmiHostController_Cache_Iterator t_Iterator ;
  318. WmiStatusCode t_StatusCode = ProviderSubSystem_Globals :: GetHostController ()->Find ( a_Key , t_Iterator ) ;
  319. if ( t_StatusCode == e_StatusCode_Success )
  320. {
  321. HostCacheElement *t_Element = t_Iterator.GetElement () ;
  322. ProviderSubSystem_Globals :: GetHostController ()->UnLock () ;
  323. CServerObject_HostInterceptor *t_HostInterceptor = NULL ;
  324. t_Result = t_Element->QueryInterface (
  325. IID_CServerObject_HostInterceptor ,
  326. ( void ** ) & t_HostInterceptor
  327. ) ;
  328. if ( SUCCEEDED ( t_Result ) )
  329. {
  330. t_Result = t_HostInterceptor->WaitHost ( a_Context , DEFAULT_PROVIDER_LOAD_TIMEOUT ) ;
  331. if ( SUCCEEDED ( t_Result ) )
  332. {
  333. t_Result = t_HostInterceptor->GetInitializeResult () ;
  334. if ( SUCCEEDED ( t_Result ) )
  335. {
  336. t_Result = t_Element->QueryInterface (
  337. a_RIID ,
  338. a_Interface
  339. ) ;
  340. if ( FAILED ( t_Result ) )
  341. {
  342. t_Result = WBEM_E_PROVIDER_NOT_CAPABLE ;
  343. }
  344. }
  345. }
  346. t_HostInterceptor->Release () ;
  347. }
  348. else
  349. {
  350. t_Result = WBEM_E_UNEXPECTED ;
  351. }
  352. t_Element->Release () ;
  353. }
  354. else
  355. {
  356. CServerObject_HostInterceptor *t_HostInterceptor = new CServerObject_HostInterceptor (
  357. *ProviderSubSystem_Globals :: s_Allocator ,
  358. ProviderSubSystem_Globals :: GetHostController () ,
  359. a_Key ,
  360. ProviderSubSystem_Globals :: s_InternalCacheTimeout ,
  361. a_Context
  362. ) ;
  363. if ( t_HostInterceptor )
  364. {
  365. t_HostInterceptor->AddRef () ;
  366. CServerObject_ProviderInitSink *t_ProviderInitSink = new CServerObject_ProviderInitSink ;
  367. if ( t_ProviderInitSink )
  368. {
  369. t_ProviderInitSink->AddRef () ;
  370. t_Result = t_ProviderInitSink->SinkInitialize () ;
  371. if ( SUCCEEDED ( t_Result ) )
  372. {
  373. t_Result = t_HostInterceptor->Initialize (
  374. NULL ,
  375. t_ProviderInitSink
  376. ) ;
  377. if ( SUCCEEDED ( t_Result ) )
  378. {
  379. t_ProviderInitSink->Wait () ;
  380. t_Result = t_ProviderInitSink->GetResult () ;
  381. }
  382. }
  383. t_ProviderInitSink->Release () ;
  384. }
  385. else
  386. {
  387. t_Result = WBEM_E_OUT_OF_MEMORY ;
  388. }
  389. }
  390. else
  391. {
  392. t_Result = WBEM_E_OUT_OF_MEMORY ;
  393. }
  394. if ( SUCCEEDED ( t_Result ) )
  395. {
  396. WmiStatusCode t_StatusCode = ProviderSubSystem_Globals :: GetHostController ()->Insert (
  397. *t_HostInterceptor ,
  398. t_Iterator
  399. ) ;
  400. if ( t_StatusCode == e_StatusCode_Success )
  401. {
  402. ProviderSubSystem_Globals :: GetHostController ()->UnLock () ;
  403. IUnknown *t_Host = NULL ;
  404. t_Result = ProviderSubSystem_Common_Globals :: CreateInstance (
  405. CLSID_WmiProviderHost ,
  406. NULL ,
  407. CLSCTX_LOCAL_SERVER | CLSCTX_ENABLE_AAA ,
  408. IID_IUnknown ,
  409. ( void ** ) & t_Host
  410. ) ;
  411. if ( SUCCEEDED ( t_Result ) )
  412. {
  413. t_Result = t_HostInterceptor->SetHost ( t_Host ) ;
  414. t_Host->Release () ;
  415. }
  416. }
  417. else
  418. {
  419. ProviderSubSystem_Globals :: GetHostController ()->UnLock () ;
  420. t_Result = WBEM_E_OUT_OF_MEMORY ;
  421. }
  422. }
  423. else
  424. {
  425. ProviderSubSystem_Globals :: GetHostController ()->UnLock () ;
  426. }
  427. if ( SUCCEEDED ( t_Result ) )
  428. {
  429. DWORD t_ProcessIdentifier = 0 ;
  430. t_Result = t_HostInterceptor->GetProcessIdentifier ( & t_ProcessIdentifier ) ;
  431. if ( SUCCEEDED ( t_Result ) )
  432. {
  433. RevertToSelf () ;
  434. HANDLE t_Handle = OpenProcess (
  435. PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE | PROCESS_SET_QUOTA | PROCESS_TERMINATE ,
  436. FALSE ,
  437. t_ProcessIdentifier
  438. ) ;
  439. if ( t_Handle )
  440. {
  441. Task_ProcessTermination *t_Task = new Task_ProcessTermination ( *ProviderSubSystem_Globals :: s_Allocator , t_Handle , t_ProcessIdentifier ) ;
  442. if ( t_Task )
  443. {
  444. if (Dispatcher::registerHandlerOnce(*t_Task))
  445. {
  446. t_Result = ProviderSubSystem_Globals :: AssignProcessToJobObject ( t_Handle ) ;
  447. }
  448. else
  449. {
  450. t_Result = WBEM_E_OUT_OF_MEMORY ;
  451. }
  452. t_Task->Release();
  453. }
  454. else
  455. {
  456. t_Result = WBEM_E_OUT_OF_MEMORY ;
  457. }
  458. }
  459. else
  460. {
  461. DWORD t_LastError = GetLastError () ;
  462. t_Result = WBEM_E_ACCESS_DENIED ;
  463. }
  464. }
  465. if ( SUCCEEDED ( t_Result ) )
  466. {
  467. t_Result = t_HostInterceptor->QueryInterface (
  468. a_RIID ,
  469. a_Interface
  470. ) ;
  471. }
  472. }
  473. if ( FAILED ( t_Result ) )
  474. {
  475. WmiStatusCode t_StatusCode = ProviderSubSystem_Globals :: GetHostController ()->Shutdown ( a_Key ) ;
  476. }
  477. if ( t_HostInterceptor )
  478. {
  479. t_HostInterceptor->SetInitialized ( t_Result ) ;
  480. t_HostInterceptor->Release () ;
  481. }
  482. }
  483. return t_Result ;
  484. }
  485. /******************************************************************************
  486. *
  487. * Name:
  488. *
  489. *
  490. * Description:
  491. *
  492. *
  493. *****************************************************************************/
  494. #pragma warning( disable : 4355 )
  495. CServerObject_HostInterceptor :: CServerObject_HostInterceptor (
  496. WmiAllocator &a_Allocator ,
  497. CWbemGlobal_IWmiHostController *a_Controller ,
  498. const HostCacheKey &a_Key ,
  499. const ULONG &a_Period ,
  500. IWbemContext *a_InitializationContext
  501. ) : HostCacheElement (
  502. a_Controller ,
  503. a_Key ,
  504. a_Period
  505. ) ,
  506. m_Allocator ( a_Allocator ) ,
  507. m_ProxyContainer ( m_Allocator , 2 , MAX_PROXIES ) ,
  508. m_Unknown ( NULL ) ,
  509. m_Host_IWmiProviderHost ( NULL ) ,
  510. m_Host_IWbemShutdown ( NULL ) ,
  511. m_ProcessIdentifier ( 0 ) ,
  512. m_UnInitialized ( 0 ) ,
  513. m_Initialized ( 0 ) ,
  514. m_InitializeResult ( S_OK ) ,
  515. m_InitializedEvent ( NULL ) ,
  516. m_InitializationContext ( a_InitializationContext )
  517. {
  518. InterlockedIncrement ( & ProviderSubSystem_Globals :: s_CServerObject_HostInterceptor_ObjectsInProgress ) ;
  519. ProviderSubSystem_Globals :: Increment_Global_Object_Count () ;
  520. if ( m_InitializationContext )
  521. {
  522. m_InitializationContext->AddRef () ;
  523. }
  524. }
  525. #pragma warning( default : 4355 )
  526. /******************************************************************************
  527. *
  528. * Name:
  529. *
  530. *
  531. * Description:
  532. *
  533. *
  534. *****************************************************************************/
  535. CServerObject_HostInterceptor :: ~CServerObject_HostInterceptor ()
  536. {
  537. if ( m_InitializationContext )
  538. {
  539. m_InitializationContext->Release () ;
  540. }
  541. if ( m_InitializedEvent )
  542. {
  543. CloseHandle ( m_InitializedEvent ) ;
  544. }
  545. InterlockedDecrement ( & ProviderSubSystem_Globals :: s_CServerObject_HostInterceptor_ObjectsInProgress ) ;
  546. ProviderSubSystem_Globals :: Decrement_Global_Object_Count () ;
  547. }
  548. /******************************************************************************
  549. *
  550. * Name:
  551. *
  552. *
  553. * Description:
  554. *
  555. *
  556. *****************************************************************************/
  557. STDMETHODIMP CServerObject_HostInterceptor :: QueryInterface (
  558. REFIID iid ,
  559. LPVOID FAR *iplpv
  560. )
  561. {
  562. *iplpv = NULL ;
  563. if ( iid == IID_IUnknown )
  564. {
  565. *iplpv = ( LPVOID ) this ;
  566. }
  567. else if ( iid == IID__IWmiProviderHost )
  568. {
  569. *iplpv = ( LPVOID ) ( _IWmiProviderHost * ) this ;
  570. }
  571. else if ( iid == IID_IWbemShutdown )
  572. {
  573. *iplpv = ( LPVOID ) ( IWbemShutdown * ) this ;
  574. }
  575. else if ( iid == IID_CServerObject_HostInterceptor )
  576. {
  577. *iplpv = ( LPVOID ) ( CServerObject_HostInterceptor * ) this ;
  578. }
  579. if ( *iplpv )
  580. {
  581. ( ( LPUNKNOWN ) *iplpv )->AddRef () ;
  582. return ResultFromScode ( S_OK ) ;
  583. }
  584. else
  585. {
  586. return ResultFromScode ( E_NOINTERFACE ) ;
  587. }
  588. }
  589. /******************************************************************************
  590. *
  591. * Name:
  592. *
  593. *
  594. * Description:
  595. *
  596. *
  597. *****************************************************************************/
  598. STDMETHODIMP_( ULONG ) CServerObject_HostInterceptor :: AddRef ()
  599. {
  600. return HostCacheElement :: AddRef () ;
  601. }
  602. /******************************************************************************
  603. *
  604. * Name:
  605. *
  606. *
  607. * Description:
  608. *
  609. *
  610. *****************************************************************************/
  611. STDMETHODIMP_(ULONG) CServerObject_HostInterceptor :: Release ()
  612. {
  613. return HostCacheElement :: Release () ;
  614. }
  615. /******************************************************************************
  616. *
  617. * Name:
  618. *
  619. *
  620. * Description:
  621. *
  622. *
  623. *****************************************************************************/
  624. HRESULT CServerObject_HostInterceptor :: Initialize (
  625. IWbemContext *a_Context ,
  626. IWbemProviderInitSink *a_Sink // For init signals
  627. )
  628. {
  629. HRESULT t_Result = S_OK ;
  630. WmiStatusCode t_StatusCode = m_ProxyContainer.Initialize () ;
  631. if ( t_StatusCode != e_StatusCode_Success )
  632. {
  633. t_Result = WBEM_E_OUT_OF_MEMORY ;
  634. }
  635. if ( SUCCEEDED ( t_Result ) )
  636. {
  637. m_InitializedEvent = CreateEvent ( NULL , TRUE , FALSE , NULL ) ;
  638. if ( m_InitializedEvent == NULL )
  639. {
  640. t_Result = t_Result = WBEM_E_OUT_OF_MEMORY ;
  641. }
  642. }
  643. a_Sink->SetStatus ( t_Result , 0 ) ;
  644. return t_Result ;
  645. }
  646. /******************************************************************************
  647. *
  648. * Name:
  649. *
  650. *
  651. * Description:
  652. *
  653. *
  654. *****************************************************************************/
  655. HRESULT CServerObject_HostInterceptor :: SetHost ( IUnknown *a_Unknown )
  656. {
  657. m_Unknown = a_Unknown ;
  658. m_Unknown->AddRef () ;
  659. HRESULT t_Result = m_Unknown->QueryInterface ( IID_IWbemShutdown , ( void ** ) & m_Host_IWbemShutdown ) ;
  660. if ( SUCCEEDED ( t_Result ) )
  661. {
  662. t_Result = m_Unknown->QueryInterface ( IID__IWmiProviderHost , ( void ** ) & m_Host_IWmiProviderHost ) ;
  663. }
  664. return t_Result ;
  665. }
  666. /******************************************************************************
  667. *
  668. * Name:
  669. *
  670. *
  671. * Description:
  672. *
  673. *
  674. *****************************************************************************/
  675. HRESULT CServerObject_HostInterceptor :: SetInitialized ( HRESULT a_InitializeResult )
  676. {
  677. m_InitializeResult = a_InitializeResult ;
  678. InterlockedExchange ( & m_Initialized , 1 ) ;
  679. if ( m_InitializedEvent )
  680. {
  681. SetEvent ( m_InitializedEvent ) ;
  682. }
  683. return S_OK ;
  684. }
  685. /******************************************************************************
  686. *
  687. * Name:
  688. *
  689. *
  690. * Description:
  691. *
  692. *
  693. *****************************************************************************/
  694. HRESULT CServerObject_HostInterceptor :: IsIndependant ( IWbemContext *a_Context )
  695. {
  696. BOOL t_DependantCall = FALSE ;
  697. HRESULT t_Result = ProviderSubSystem_Common_Globals :: IsDependantCall ( m_InitializationContext , a_Context , t_DependantCall ) ;
  698. if ( SUCCEEDED ( t_Result ) )
  699. {
  700. if ( t_DependantCall == FALSE )
  701. {
  702. }
  703. else
  704. {
  705. return S_FALSE ;
  706. }
  707. }
  708. return t_Result ;
  709. }
  710. /******************************************************************************
  711. *
  712. * Name:
  713. *
  714. *
  715. * Description:
  716. *
  717. *
  718. *****************************************************************************/
  719. HRESULT CServerObject_HostInterceptor :: WaitHost ( IWbemContext *a_Context , ULONG a_Timeout )
  720. {
  721. HRESULT t_Result = S_OK ;
  722. if ( m_Initialized == 0 )
  723. {
  724. if ( WaitForSingleObject ( m_InitializedEvent , a_Timeout ) != WAIT_OBJECT_0 )
  725. {
  726. return WBEM_E_PROVIDER_LOAD_FAILURE ;
  727. }
  728. }
  729. return t_Result ;
  730. }
  731. /******************************************************************************
  732. *
  733. * Name:
  734. *
  735. *
  736. * Description:
  737. *
  738. *
  739. *****************************************************************************/
  740. void CServerObject_HostInterceptor :: CallBackInternalRelease ()
  741. {
  742. if ( InterlockedCompareExchange ( & m_UnInitialized , 1 , 0 ) == 0 )
  743. {
  744. WmiStatusCode t_StatusCode = m_ProxyContainer.UnInitialize () ;
  745. if ( m_Unknown )
  746. {
  747. m_Unknown->Release () ;
  748. m_Unknown = NULL ;
  749. }
  750. if ( m_Host_IWmiProviderHost )
  751. {
  752. m_Host_IWmiProviderHost->Release () ;
  753. m_Host_IWmiProviderHost = NULL ;
  754. }
  755. if ( m_Host_IWbemShutdown )
  756. {
  757. m_Host_IWbemShutdown->Release () ;
  758. m_Host_IWbemShutdown = NULL ;
  759. }
  760. }
  761. }
  762. /******************************************************************************
  763. *
  764. * Name:
  765. *
  766. *
  767. * Description:
  768. *
  769. *
  770. *****************************************************************************/
  771. HRESULT CServerObject_HostInterceptor :: GetObject (
  772. REFCLSID a_Clsid ,
  773. long a_Flags ,
  774. IWbemContext *a_Context ,
  775. REFIID a_Riid ,
  776. void **a_Interface
  777. )
  778. {
  779. HRESULT t_Result = S_OK ;
  780. if ( m_Host_IWmiProviderHost )
  781. {
  782. BOOL t_Impersonating = FALSE ;
  783. IUnknown *t_OldContext = NULL ;
  784. IServerSecurity *t_OldSecurity = NULL ;
  785. t_Result = ProviderSubSystem_Common_Globals :: BeginCallbackImpersonation ( t_OldContext , t_OldSecurity , t_Impersonating ) ;
  786. if ( SUCCEEDED ( t_Result ) )
  787. {
  788. BOOL t_Revert = FALSE ;
  789. IUnknown *t_Proxy = NULL ;
  790. t_Result = ProviderSubSystem_Common_Globals :: SetProxyState ( m_ProxyContainer , ProxyIndex_Host__IWmiProviderHost , IID__IWmiProviderHost , m_Host_IWmiProviderHost , t_Proxy , t_Revert ) ;
  791. if ( t_Result == WBEM_E_NOT_FOUND )
  792. {
  793. t_Result = m_Host_IWmiProviderHost->GetObject (
  794. a_Clsid ,
  795. a_Flags ,
  796. a_Context ,
  797. a_Riid ,
  798. a_Interface
  799. ) ;
  800. }
  801. else
  802. {
  803. if ( SUCCEEDED ( t_Result ) )
  804. {
  805. _IWmiProviderHost *t_Host = ( _IWmiProviderHost * ) t_Proxy ;
  806. // Set cloaking on the proxy
  807. // =========================
  808. DWORD t_ImpersonationLevel = ProviderSubSystem_Common_Globals :: GetCurrentImpersonationLevel () ;
  809. t_Result = ProviderSubSystem_Common_Globals :: SetCloaking (
  810. t_Host ,
  811. RPC_C_AUTHN_LEVEL_DEFAULT ,
  812. t_ImpersonationLevel
  813. ) ;
  814. if ( SUCCEEDED ( t_Result ) )
  815. {
  816. t_Result = CoImpersonateClient () ;
  817. if ( SUCCEEDED ( t_Result ) )
  818. {
  819. t_Result = t_Host->GetObject (
  820. a_Clsid ,
  821. a_Flags ,
  822. a_Context ,
  823. a_Riid ,
  824. a_Interface
  825. ) ;
  826. CoRevertToSelf () ;
  827. }
  828. else
  829. {
  830. t_Result = WBEM_E_ACCESS_DENIED ;
  831. }
  832. }
  833. HRESULT t_TempResult = ProviderSubSystem_Common_Globals :: RevertProxyState ( m_ProxyContainer , ProxyIndex_Host__IWmiProviderHost , t_Proxy , t_Revert ) ;
  834. }
  835. }
  836. ProviderSubSystem_Common_Globals :: EndImpersonation ( t_OldContext , t_OldSecurity , t_Impersonating ) ;
  837. }
  838. }
  839. return t_Result ;
  840. }
  841. /******************************************************************************
  842. *
  843. * Name:
  844. *
  845. *
  846. * Description:
  847. *
  848. *
  849. *****************************************************************************/
  850. HRESULT CServerObject_HostInterceptor :: GetProcessIdentifier ( DWORD *a_ProcessIdentifier )
  851. {
  852. HRESULT t_Result = S_OK ;
  853. if ( m_Host_IWmiProviderHost )
  854. {
  855. BOOL t_Impersonating = FALSE ;
  856. IUnknown *t_OldContext = NULL ;
  857. IServerSecurity *t_OldSecurity = NULL ;
  858. t_Result = ProviderSubSystem_Common_Globals :: BeginCallbackImpersonation ( t_OldContext , t_OldSecurity , t_Impersonating ) ;
  859. if ( SUCCEEDED ( t_Result ) )
  860. {
  861. BOOL t_Revert = FALSE ;
  862. IUnknown *t_Proxy = NULL ;
  863. t_Result = ProviderSubSystem_Common_Globals :: SetProxyState ( m_ProxyContainer , ProxyIndex_Host__IWmiProviderHost , IID__IWmiProviderHost , m_Host_IWmiProviderHost , t_Proxy , t_Revert ) ;
  864. if ( t_Result == WBEM_E_NOT_FOUND )
  865. {
  866. t_Result = m_Host_IWmiProviderHost->GetProcessIdentifier (
  867. a_ProcessIdentifier
  868. ) ;
  869. if ( SUCCEEDED ( t_Result ) )
  870. {
  871. m_ProcessIdentifier = *a_ProcessIdentifier ;
  872. }
  873. }
  874. else
  875. {
  876. if ( SUCCEEDED ( t_Result ) )
  877. {
  878. _IWmiProviderHost *t_Host = ( _IWmiProviderHost * ) t_Proxy ;
  879. // Set cloaking on the proxy
  880. // =========================
  881. DWORD t_ImpersonationLevel = ProviderSubSystem_Common_Globals :: GetCurrentImpersonationLevel () ;
  882. t_Result = ProviderSubSystem_Common_Globals :: SetCloaking (
  883. t_Host ,
  884. RPC_C_AUTHN_LEVEL_DEFAULT ,
  885. t_ImpersonationLevel
  886. ) ;
  887. if ( SUCCEEDED ( t_Result ) )
  888. {
  889. t_Result = CoImpersonateClient () ;
  890. if ( SUCCEEDED ( t_Result ) )
  891. {
  892. t_Result = t_Host->GetProcessIdentifier (
  893. a_ProcessIdentifier
  894. ) ;
  895. if ( SUCCEEDED ( t_Result ) )
  896. {
  897. m_ProcessIdentifier = *a_ProcessIdentifier ;
  898. }
  899. CoRevertToSelf () ;
  900. }
  901. else
  902. {
  903. t_Result = WBEM_E_ACCESS_DENIED ;
  904. }
  905. }
  906. HRESULT t_TempResult = ProviderSubSystem_Common_Globals :: RevertProxyState ( m_ProxyContainer , ProxyIndex_Host__IWmiProviderHost , t_Proxy , t_Revert ) ;
  907. }
  908. }
  909. ProviderSubSystem_Common_Globals :: EndImpersonation ( t_OldContext , t_OldSecurity , t_Impersonating ) ;
  910. }
  911. }
  912. return t_Result ;
  913. }
  914. /******************************************************************************
  915. *
  916. * Name:
  917. *
  918. *
  919. * Description:
  920. *
  921. *
  922. *****************************************************************************/
  923. HRESULT CServerObject_HostInterceptor :: Shutdown (
  924. LONG a_Flags ,
  925. ULONG a_MaxMilliSeconds ,
  926. IWbemContext *a_Context
  927. )
  928. {
  929. HRESULT t_Result = S_OK ;
  930. if ( m_Host_IWbemShutdown )
  931. {
  932. BOOL t_Impersonating = FALSE ;
  933. IUnknown *t_OldContext = NULL ;
  934. IServerSecurity *t_OldSecurity = NULL ;
  935. t_Result = ProviderSubSystem_Common_Globals :: BeginCallbackImpersonation ( t_OldContext , t_OldSecurity , t_Impersonating ) ;
  936. if ( SUCCEEDED ( t_Result ) )
  937. {
  938. BOOL t_Revert = FALSE ;
  939. IUnknown *t_Proxy = NULL ;
  940. t_Result = ProviderSubSystem_Common_Globals :: SetProxyState ( m_ProxyContainer , ProxyIndex_Host_IWbemShutdown , IID_IWbemShutdown , m_Host_IWbemShutdown , t_Proxy , t_Revert ) ;
  941. if ( t_Result == WBEM_E_NOT_FOUND )
  942. {
  943. t_Result = m_Host_IWbemShutdown->Shutdown (
  944. a_Flags ,
  945. a_MaxMilliSeconds ,
  946. a_Context
  947. ) ;
  948. }
  949. else
  950. {
  951. if ( SUCCEEDED ( t_Result ) )
  952. {
  953. IWbemShutdown *t_Host = ( IWbemShutdown * ) t_Proxy ;
  954. // Set cloaking on the proxy
  955. // =========================
  956. DWORD t_ImpersonationLevel = ProviderSubSystem_Common_Globals :: GetCurrentImpersonationLevel () ;
  957. t_Result = ProviderSubSystem_Common_Globals :: SetCloaking (
  958. t_Host ,
  959. RPC_C_AUTHN_LEVEL_DEFAULT ,
  960. t_ImpersonationLevel
  961. ) ;
  962. if ( SUCCEEDED ( t_Result ) )
  963. {
  964. t_Result = CoImpersonateClient () ;
  965. if ( SUCCEEDED ( t_Result ) )
  966. {
  967. t_Result = t_Host->Shutdown (
  968. a_Flags ,
  969. a_MaxMilliSeconds ,
  970. a_Context
  971. ) ;
  972. CoRevertToSelf () ;
  973. }
  974. else
  975. {
  976. t_Result = WBEM_E_ACCESS_DENIED ;
  977. }
  978. }
  979. HRESULT t_TempResult = ProviderSubSystem_Common_Globals :: RevertProxyState ( m_ProxyContainer , ProxyIndex_Host_IWbemShutdown , t_Proxy , t_Revert ) ;
  980. }
  981. }
  982. ProviderSubSystem_Common_Globals :: EndImpersonation ( t_OldContext , t_OldSecurity , t_Impersonating ) ;
  983. }
  984. }
  985. return t_Result ;
  986. }
  987. /******************************************************************************
  988. *
  989. * Name:
  990. *
  991. *
  992. * Description:
  993. *
  994. *
  995. *****************************************************************************/
  996. #pragma warning( disable : 4355 )
  997. CServerObject_Host :: CServerObject_Host (
  998. WmiAllocator &a_Allocator
  999. ) : m_Allocator ( a_Allocator )
  1000. {
  1001. InterlockedIncrement ( & ProviderSubSystem_Globals :: s_CServerObject_Host_ObjectsInProgress ) ;
  1002. ProviderSubSystem_Globals :: Increment_Global_Object_Count () ;
  1003. }
  1004. #pragma warning( default : 4355 )
  1005. /******************************************************************************
  1006. *
  1007. * Name:
  1008. *
  1009. *
  1010. * Description:
  1011. *
  1012. *
  1013. *****************************************************************************/
  1014. CServerObject_Host :: ~CServerObject_Host ()
  1015. {
  1016. LONG hosts = InterlockedDecrement ( & ProviderSubSystem_Globals :: s_CServerObject_Host_ObjectsInProgress ) ;
  1017. ProviderSubSystem_Globals :: Decrement_Global_Object_Count () ;
  1018. }
  1019. /******************************************************************************
  1020. *
  1021. * Name:
  1022. *
  1023. *
  1024. * Description:
  1025. *
  1026. *
  1027. *****************************************************************************/
  1028. STDMETHODIMP CServerObject_Host :: QueryInterface (
  1029. REFIID iid ,
  1030. LPVOID FAR *iplpv
  1031. )
  1032. {
  1033. *iplpv = NULL ;
  1034. if ( iid == IID_IUnknown )
  1035. {
  1036. *iplpv = ( LPVOID ) this ;
  1037. }
  1038. else if ( iid == IID__IWmiProviderHost )
  1039. {
  1040. *iplpv = ( LPVOID ) ( _IWmiProviderHost * ) this ;
  1041. }
  1042. else if ( iid == IID_IWbemShutdown )
  1043. {
  1044. *iplpv = ( LPVOID ) ( IWbemShutdown * )this ;
  1045. }
  1046. if ( *iplpv )
  1047. {
  1048. ( ( LPUNKNOWN ) *iplpv )->AddRef () ;
  1049. return ResultFromScode ( S_OK ) ;
  1050. }
  1051. else
  1052. {
  1053. return ResultFromScode ( E_NOINTERFACE ) ;
  1054. }
  1055. }
  1056. /******************************************************************************
  1057. *
  1058. * Name:
  1059. *
  1060. *
  1061. * Description:
  1062. *
  1063. *
  1064. *****************************************************************************/
  1065. STDMETHODIMP_( ULONG ) CServerObject_Host :: AddRef ()
  1066. {
  1067. return InterlockedIncrement ( & m_ReferenceCount ) ;
  1068. }
  1069. /******************************************************************************
  1070. *
  1071. * Name:
  1072. *
  1073. *
  1074. * Description:
  1075. *
  1076. *
  1077. *****************************************************************************/
  1078. STDMETHODIMP_(ULONG) CServerObject_Host :: Release ()
  1079. {
  1080. ULONG t_ReferenceCount = InterlockedDecrement ( & m_ReferenceCount ) ;
  1081. if ( t_ReferenceCount == 0 )
  1082. {
  1083. delete this ;
  1084. }
  1085. return t_ReferenceCount ;
  1086. }
  1087. /******************************************************************************
  1088. *
  1089. * Name:
  1090. *
  1091. *
  1092. * Description:
  1093. *
  1094. *
  1095. *****************************************************************************/
  1096. HRESULT CServerObject_Host :: GetObject (
  1097. REFCLSID a_Clsid ,
  1098. long a_Flags ,
  1099. IWbemContext *a_Context ,
  1100. REFIID a_Riid ,
  1101. void **a_Interface
  1102. )
  1103. {
  1104. if ( a_Clsid == CLSID_WmiProviderHostedInProcFactory )
  1105. {
  1106. IUnknown *lpunk = ( _IWmiProviderFactory * ) new CServerObject_RawFactory ( *ProviderSubSystem_Globals :: s_Allocator );
  1107. if ( lpunk )
  1108. {
  1109. HRESULT t_Result = lpunk->QueryInterface ( a_Riid , a_Interface ) ;
  1110. if ( SUCCEEDED ( t_Result ) )
  1111. {
  1112. }
  1113. else
  1114. {
  1115. delete lpunk ;
  1116. }
  1117. return t_Result ;
  1118. }
  1119. else
  1120. {
  1121. return E_OUTOFMEMORY ;
  1122. }
  1123. }
  1124. else if ( a_Clsid == CLSID__WbemHostedRefresherMgr )
  1125. {
  1126. IUnknown *lpunk = ( _IWbemRefresherMgr * ) new CServerObject_ProviderRefresherManager ( *ProviderSubSystem_Globals :: s_Allocator );
  1127. if ( lpunk )
  1128. {
  1129. HRESULT t_Result = lpunk->QueryInterface ( a_Riid , a_Interface ) ;
  1130. if ( SUCCEEDED ( t_Result ) )
  1131. {
  1132. }
  1133. else
  1134. {
  1135. delete lpunk ;
  1136. }
  1137. return t_Result ;
  1138. }
  1139. else
  1140. {
  1141. return E_OUTOFMEMORY ;
  1142. }
  1143. }
  1144. else
  1145. {
  1146. return CLASS_E_CLASSNOTAVAILABLE ;
  1147. }
  1148. }
  1149. /******************************************************************************
  1150. *
  1151. * Name:
  1152. *
  1153. *
  1154. * Description:
  1155. *
  1156. *
  1157. *****************************************************************************/
  1158. HRESULT CServerObject_Host :: GetProcessIdentifier ( DWORD *a_ProcessIdentifier )
  1159. {
  1160. HRESULT t_Result = S_OK ;
  1161. if ( a_ProcessIdentifier )
  1162. {
  1163. *a_ProcessIdentifier = GetCurrentProcessId () ;
  1164. }
  1165. else
  1166. {
  1167. t_Result = WBEM_E_INVALID_PARAMETER ;
  1168. }
  1169. return t_Result ;
  1170. }
  1171. /******************************************************************************
  1172. *
  1173. * Name:
  1174. *
  1175. *
  1176. * Description:
  1177. *
  1178. *
  1179. *****************************************************************************/
  1180. HRESULT CServerObject_Host :: Shutdown (
  1181. LONG a_Flags ,
  1182. ULONG a_MaxMilliSeconds ,
  1183. IWbemContext *a_Context
  1184. )
  1185. {
  1186. return S_OK ;
  1187. }