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.

4260 lines
89 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 <sddl.h>
  12. #include <initguid.h>
  13. #ifndef INITGUID
  14. #define INITGUID
  15. #endif
  16. #include <wbemcli.h>
  17. #include <wbemint.h>
  18. #include <callsec.h>
  19. #include <cominit.h>
  20. #include <BasicTree.h>
  21. #include <Thread.h>
  22. #include <Logging.h>
  23. #include <PSSException.h>
  24. #include <Cache.h>
  25. #include "DateTime.h"
  26. #include "CGlobals.h"
  27. #include <Allocator.cpp>
  28. #include <HelperFuncs.cpp>
  29. #include <Logging.cpp>
  30. #include <Cache.cpp>
  31. #include <CallSec.h>
  32. #include <OS.h>
  33. #include <ssdlhelper.h>
  34. #include "ProvRegInfo.h"
  35. /******************************************************************************
  36. *
  37. * Name:
  38. *
  39. *
  40. * Description:
  41. *
  42. *
  43. *****************************************************************************/
  44. LPCWSTR ProviderSubSystem_Common_Globals :: s_Wql = L"Wql" ;
  45. LPCWSTR ProviderSubSystem_Common_Globals :: s_Provider = L"Provider" ;
  46. WORD ProviderSubSystem_Common_Globals :: s_System_ACESize = 0 ;
  47. WORD ProviderSubSystem_Common_Globals :: s_LocalService_ACESize = 0 ;
  48. WORD ProviderSubSystem_Common_Globals :: s_NetworkService_ACESize = 0 ;
  49. WORD ProviderSubSystem_Common_Globals :: s_LocalAdmins_ACESize = 0 ;
  50. ACCESS_ALLOWED_ACE *ProviderSubSystem_Common_Globals :: s_Provider_System_ACE = NULL ;
  51. ACCESS_ALLOWED_ACE *ProviderSubSystem_Common_Globals :: s_Provider_LocalService_ACE = NULL ;
  52. ACCESS_ALLOWED_ACE *ProviderSubSystem_Common_Globals :: s_Provider_NetworkService_ACE = NULL ;
  53. ACCESS_ALLOWED_ACE *ProviderSubSystem_Common_Globals :: s_Provider_LocalAdmins_ACE = NULL ;
  54. ACCESS_ALLOWED_ACE *ProviderSubSystem_Common_Globals :: s_Token_All_Access_System_ACE = NULL ;
  55. ACCESS_ALLOWED_ACE *ProviderSubSystem_Common_Globals :: s_Token_All_Access_LocalService_ACE = NULL ;
  56. ACCESS_ALLOWED_ACE *ProviderSubSystem_Common_Globals :: s_Token_All_Access_NetworkService_ACE = NULL ;
  57. ACCESS_ALLOWED_ACE *ProviderSubSystem_Common_Globals :: s_Token_All_Access_LocalAdmins_ACE = NULL ;
  58. SECURITY_DESCRIPTOR *ProviderSubSystem_Common_Globals :: s_MethodSecurityDescriptor = NULL ;
  59. ULONG ProviderSubSystem_Common_Globals :: s_TransmitBufferSize = SYNCPROV_BATCH_TRANSMIT_SIZE ;
  60. ULONG ProviderSubSystem_Common_Globals :: s_DefaultStackSize = 0 ;
  61. /******************************************************************************
  62. *
  63. * Name:
  64. *
  65. *
  66. * Description:
  67. *
  68. *
  69. *****************************************************************************/
  70. HRESULT ProviderSubSystem_Common_Globals :: CreateInstance (
  71. const CLSID &a_ReferenceClsid ,
  72. LPUNKNOWN a_OuterUnknown ,
  73. const DWORD &a_ClassContext ,
  74. const UUID &a_ReferenceInterfaceId ,
  75. void **a_ObjectInterface
  76. )
  77. {
  78. HRESULT t_Result = S_OK ;
  79. COAUTHINFO t_AuthenticationInfo ;
  80. ZeroMemory ( & t_AuthenticationInfo , sizeof ( t_AuthenticationInfo ) ) ;
  81. t_AuthenticationInfo.dwAuthnSvc = RPC_C_AUTHN_DEFAULT ;
  82. t_AuthenticationInfo.dwAuthzSvc = RPC_C_AUTHZ_DEFAULT ;
  83. t_AuthenticationInfo.pwszServerPrincName = NULL ;
  84. t_AuthenticationInfo.dwAuthnLevel = RPC_C_AUTHN_LEVEL_CONNECT ;
  85. t_AuthenticationInfo.dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE ;
  86. t_AuthenticationInfo.dwCapabilities = EOAC_NONE ;
  87. t_AuthenticationInfo.pAuthIdentityData = NULL ;
  88. COSERVERINFO t_ServerInfo ;
  89. ZeroMemory ( & t_ServerInfo , sizeof ( t_ServerInfo ) ) ;
  90. t_ServerInfo.pwszName = NULL ;
  91. t_ServerInfo.dwReserved2 = 0 ;
  92. t_ServerInfo.pAuthInfo = & t_AuthenticationInfo ;
  93. IClassFactory *t_ClassFactory = NULL ;
  94. t_Result = CoGetClassObject (
  95. a_ReferenceClsid ,
  96. a_ClassContext ,
  97. & t_ServerInfo ,
  98. IID_IClassFactory ,
  99. ( void ** ) & t_ClassFactory
  100. ) ;
  101. if ( SUCCEEDED ( t_Result ) )
  102. {
  103. t_Result = t_ClassFactory->CreateInstance (
  104. a_OuterUnknown ,
  105. a_ReferenceInterfaceId ,
  106. a_ObjectInterface
  107. );
  108. t_ClassFactory->Release () ;
  109. }
  110. return t_Result ;
  111. }
  112. /******************************************************************************
  113. *
  114. * Name:
  115. *
  116. *
  117. * Description:
  118. *
  119. *
  120. *****************************************************************************/
  121. HRESULT ProviderSubSystem_Common_Globals :: GetNamespaceServerPath (
  122. IWbemPath *a_Namespace ,
  123. wchar_t *&a_ServerNamespacePath
  124. )
  125. {
  126. a_ServerNamespacePath = NULL ;
  127. wchar_t *t_Server = NULL ;
  128. ULONG t_ServerLength = 0 ;
  129. HRESULT t_Result = a_Namespace->GetServer (
  130. & t_ServerLength ,
  131. t_Server
  132. ) ;
  133. if ( SUCCEEDED ( t_Result ) )
  134. {
  135. t_Server = new wchar_t [ t_ServerLength + 1 ] ;
  136. t_Result = a_Namespace->GetServer (
  137. & t_ServerLength ,
  138. t_Server
  139. ) ;
  140. if ( FAILED ( t_Result ) )
  141. {
  142. delete [] t_Server ;
  143. }
  144. }
  145. else
  146. {
  147. t_Result = WBEM_E_INVALID_NAMESPACE ;
  148. }
  149. if ( SUCCEEDED ( t_Result ) )
  150. {
  151. wchar_t *t_ConcatString = NULL ;
  152. WmiStatusCode t_StatusCode = WmiHelper :: ConcatenateStrings_Wchar (
  153. 2 ,
  154. & t_ConcatString ,
  155. L"\\\\" ,
  156. t_Server
  157. ) ;
  158. delete [] t_Server ;
  159. if ( t_StatusCode == e_StatusCode_Success )
  160. {
  161. a_ServerNamespacePath = t_ConcatString ;
  162. }
  163. else
  164. {
  165. t_Result = WBEM_E_OUT_OF_MEMORY ;
  166. }
  167. }
  168. if ( SUCCEEDED ( t_Result ) )
  169. {
  170. ULONG t_NamespaceCount = 0 ;
  171. t_Result = a_Namespace->GetNamespaceCount (
  172. & t_NamespaceCount
  173. ) ;
  174. if ( t_NamespaceCount )
  175. {
  176. for ( ULONG t_Index = 0 ; t_Index < t_NamespaceCount ; t_Index ++ )
  177. {
  178. wchar_t *t_Namespace = NULL ;
  179. ULONG t_NamespaceLength = 0 ;
  180. t_Result = a_Namespace->GetNamespaceAt (
  181. t_Index ,
  182. & t_NamespaceLength ,
  183. t_Namespace
  184. ) ;
  185. if ( SUCCEEDED ( t_Result ) )
  186. {
  187. t_Namespace = new wchar_t [ t_NamespaceLength + 1 ] ;
  188. if (0 != t_Namespace)
  189. {
  190. t_Result = WBEM_E_OUT_OF_MEMORY ;
  191. break;
  192. }
  193. t_Result = a_Namespace->GetNamespaceAt (
  194. t_Index ,
  195. & t_NamespaceLength ,
  196. t_Namespace
  197. ) ;
  198. if ( SUCCEEDED ( t_Result ) )
  199. {
  200. wchar_t *t_ConcatString = NULL ;
  201. WmiStatusCode t_StatusCode = WmiHelper :: ConcatenateStrings_Wchar (
  202. 3 ,
  203. & t_ConcatString ,
  204. a_ServerNamespacePath ,
  205. L"\\" ,
  206. t_Namespace
  207. ) ;
  208. delete [] t_Namespace ;
  209. if ( t_StatusCode == e_StatusCode_Success )
  210. {
  211. delete [] a_ServerNamespacePath ;
  212. a_ServerNamespacePath = t_ConcatString ;
  213. }
  214. else
  215. {
  216. t_Result = WBEM_E_OUT_OF_MEMORY ;
  217. break;
  218. }
  219. }
  220. else
  221. {
  222. delete[] t_Namespace;
  223. t_Result = WBEM_E_CRITICAL_ERROR ;
  224. break ;
  225. }
  226. }
  227. else
  228. {
  229. t_Result = WBEM_E_CRITICAL_ERROR ;
  230. break ;
  231. }
  232. }
  233. }
  234. else
  235. {
  236. t_Result = WBEM_E_INVALID_NAMESPACE ;
  237. }
  238. }
  239. if (FAILED(t_Result))
  240. {
  241. delete []a_ServerNamespacePath;
  242. }
  243. return t_Result ;
  244. }
  245. /******************************************************************************
  246. *
  247. * Name:
  248. *
  249. *
  250. * Description:
  251. *
  252. *
  253. *****************************************************************************/
  254. HRESULT ProviderSubSystem_Common_Globals :: GetNamespacePath (
  255. IWbemPath *a_Namespace ,
  256. wchar_t *&a_NamespacePath
  257. )
  258. {
  259. a_NamespacePath = NULL ;
  260. ULONG t_NamespaceCount = 0 ;
  261. HRESULT t_Result = a_Namespace->GetNamespaceCount (
  262. & t_NamespaceCount
  263. ) ;
  264. if ( t_NamespaceCount )
  265. {
  266. for ( ULONG t_Index = 0 ; t_Index < t_NamespaceCount ; t_Index ++ )
  267. {
  268. wchar_t *t_Namespace = NULL ;
  269. ULONG t_NamespaceLength = 0 ;
  270. t_Result = a_Namespace->GetNamespaceAt (
  271. t_Index ,
  272. & t_NamespaceLength ,
  273. t_Namespace
  274. ) ;
  275. if ( SUCCEEDED ( t_Result ) )
  276. {
  277. t_Namespace = new wchar_t [ t_NamespaceLength + 1 ] ;
  278. t_Result = a_Namespace->GetNamespaceAt (
  279. t_Index ,
  280. & t_NamespaceLength ,
  281. t_Namespace
  282. ) ;
  283. if ( SUCCEEDED ( t_Result ) )
  284. {
  285. wchar_t *t_ConcatString = NULL ;
  286. WmiStatusCode t_StatusCode = WmiHelper :: ConcatenateStrings_Wchar (
  287. 3 ,
  288. & t_ConcatString ,
  289. a_NamespacePath ,
  290. t_Index ? L"\\" : NULL ,
  291. t_Namespace
  292. ) ;
  293. delete [] t_Namespace ;
  294. if ( t_StatusCode == e_StatusCode_Success )
  295. {
  296. delete [] a_NamespacePath ;
  297. a_NamespacePath = t_ConcatString ;
  298. }
  299. else
  300. {
  301. t_Result = WBEM_E_OUT_OF_MEMORY ;
  302. }
  303. }
  304. else
  305. {
  306. t_Result = WBEM_E_CRITICAL_ERROR ;
  307. break ;
  308. }
  309. }
  310. else
  311. {
  312. t_Result = WBEM_E_CRITICAL_ERROR ;
  313. break ;
  314. }
  315. }
  316. }
  317. else
  318. {
  319. t_Result = WBEM_E_INVALID_NAMESPACE ;
  320. }
  321. if ( FAILED ( t_Result ) )
  322. {
  323. delete [] a_NamespacePath ;
  324. }
  325. return t_Result ;
  326. }
  327. /******************************************************************************
  328. *
  329. * Name:
  330. *
  331. *
  332. * Description:
  333. *
  334. *
  335. *****************************************************************************/
  336. HRESULT ProviderSubSystem_Common_Globals :: GetPathText (
  337. IWbemPath *a_Path ,
  338. wchar_t *&a_ObjectPath
  339. )
  340. {
  341. ULONG t_ObjectPathLength = 0 ;
  342. HRESULT t_Result = a_Path->GetText (
  343. 0 ,
  344. & t_ObjectPathLength ,
  345. NULL
  346. ) ;
  347. if ( SUCCEEDED ( t_Result ) )
  348. {
  349. a_ObjectPath = new wchar_t [ t_ObjectPathLength + 1 ] ;
  350. if ( a_ObjectPath )
  351. {
  352. t_Result = a_Path->GetText (
  353. 0 ,
  354. & t_ObjectPathLength ,
  355. a_ObjectPath
  356. ) ;
  357. }
  358. else
  359. {
  360. t_Result = WBEM_E_OUT_OF_MEMORY;
  361. }
  362. }
  363. return t_Result ;
  364. }
  365. /******************************************************************************
  366. *
  367. * Name:
  368. *
  369. *
  370. * Description:
  371. *
  372. *
  373. *****************************************************************************/
  374. HRESULT ProviderSubSystem_Common_Globals :: BeginCallbackImpersonation (
  375. IUnknown *&a_OldContext ,
  376. IServerSecurity *&a_OldSecurity ,
  377. BOOL &a_Impersonating
  378. )
  379. {
  380. HRESULT t_Result = S_OK ;
  381. IServerSecurity *t_ServerSecurity = NULL ;
  382. t_Result = CoGetCallContext ( IID_IUnknown , ( void ** ) & a_OldContext ) ;
  383. if ( SUCCEEDED ( t_Result ) )
  384. {
  385. t_Result = a_OldContext->QueryInterface ( IID_IServerSecurity , ( void ** ) & t_ServerSecurity ) ;
  386. if ( SUCCEEDED ( t_Result ) )
  387. {
  388. a_Impersonating = t_ServerSecurity->IsImpersonating () ;
  389. }
  390. else
  391. {
  392. a_Impersonating = FALSE ;
  393. }
  394. }
  395. _IWmiCallSec *t_CallSecurity = NULL ;
  396. t_Result = ProviderSubSystem_Common_Globals :: CreateInstance (
  397. CLSID__IWbemCallSec ,
  398. NULL ,
  399. CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
  400. IID__IWmiCallSec ,
  401. ( void ** ) & t_CallSecurity
  402. ) ;
  403. if ( SUCCEEDED ( t_Result ) )
  404. {
  405. _IWmiThreadSecHandle *t_ThreadSecurity = NULL ;
  406. t_Result = t_CallSecurity->GetThreadSecurity ( ( WMI_THREAD_SECURITY_ORIGIN ) ( WMI_ORIGIN_THREAD ) , & t_ThreadSecurity ) ;
  407. if ( SUCCEEDED ( t_Result ) )
  408. {
  409. t_Result = t_CallSecurity->SetThreadSecurity ( t_ThreadSecurity ) ;
  410. if ( SUCCEEDED ( t_Result ) )
  411. {
  412. t_Result = t_CallSecurity->QueryInterface ( IID_IServerSecurity , ( void ** ) & a_OldSecurity ) ;
  413. if ( SUCCEEDED ( t_Result ) )
  414. {
  415. if ( a_Impersonating )
  416. {
  417. t_ServerSecurity->RevertToSelf () ;
  418. }
  419. }
  420. }
  421. t_ThreadSecurity->Release () ;
  422. }
  423. t_CallSecurity->Release () ;
  424. }
  425. if ( t_ServerSecurity )
  426. {
  427. t_ServerSecurity->Release () ;
  428. }
  429. return t_Result ;
  430. }
  431. /******************************************************************************
  432. *
  433. * Name:
  434. *
  435. *
  436. * Description:
  437. *
  438. *
  439. *****************************************************************************/
  440. HRESULT ProviderSubSystem_Common_Globals :: BeginImpersonation (
  441. IUnknown *&a_OldContext ,
  442. IServerSecurity *&a_OldSecurity ,
  443. BOOL &a_Impersonating ,
  444. DWORD *a_AuthenticationLevel
  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. _IWmiCallSec *t_CallSecurity = NULL ;
  463. t_Result = ProviderSubSystem_Common_Globals :: CreateInstance (
  464. CLSID__IWbemCallSec ,
  465. NULL ,
  466. CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
  467. IID__IWmiCallSec ,
  468. ( void ** ) & t_CallSecurity
  469. ) ;
  470. if ( SUCCEEDED ( t_Result ) )
  471. {
  472. _IWmiThreadSecHandle *t_ThreadSecurity = NULL ;
  473. t_Result = t_CallSecurity->GetThreadSecurity ( ( WMI_THREAD_SECURITY_ORIGIN ) ( WMI_ORIGIN_THREAD | WMI_ORIGIN_EXISTING | WMI_ORIGIN_RPC ) , & t_ThreadSecurity ) ;
  474. if ( SUCCEEDED ( t_Result ) )
  475. {
  476. t_Result = t_CallSecurity->SetThreadSecurity ( t_ThreadSecurity ) ;
  477. if ( SUCCEEDED ( t_Result ) )
  478. {
  479. t_Result = t_CallSecurity->QueryInterface ( IID_IServerSecurity , ( void ** ) & a_OldSecurity ) ;
  480. if ( SUCCEEDED ( t_Result ) )
  481. {
  482. if ( a_AuthenticationLevel )
  483. {
  484. t_Result = t_ThreadSecurity->GetAuthentication ( a_AuthenticationLevel ) ;
  485. }
  486. if ( a_Impersonating )
  487. {
  488. t_ServerSecurity->RevertToSelf () ;
  489. }
  490. }
  491. }
  492. t_ThreadSecurity->Release () ;
  493. }
  494. t_CallSecurity->Release () ;
  495. }
  496. if ( t_ServerSecurity )
  497. {
  498. t_ServerSecurity->Release () ;
  499. }
  500. return t_Result ;
  501. }
  502. /******************************************************************************
  503. *
  504. * Name:
  505. *
  506. *
  507. * Description:
  508. *
  509. *
  510. *****************************************************************************/
  511. HRESULT ProviderSubSystem_Common_Globals :: EndImpersonation (
  512. IUnknown *a_OldContext ,
  513. IServerSecurity *a_OldSecurity ,
  514. BOOL a_Impersonating
  515. )
  516. {
  517. HRESULT t_Result = S_OK ;
  518. IUnknown *t_NewContext = NULL ;
  519. t_Result = CoSwitchCallContext ( a_OldContext , & t_NewContext ) ;
  520. if ( SUCCEEDED ( t_Result ) )
  521. {
  522. if ( a_OldContext )
  523. {
  524. if ( a_Impersonating )
  525. {
  526. IServerSecurity *t_ServerSecurity = NULL ;
  527. t_Result = a_OldContext->QueryInterface ( IID_IServerSecurity , ( void ** ) & t_ServerSecurity ) ;
  528. if ( SUCCEEDED ( t_Result ) )
  529. {
  530. t_Result = t_ServerSecurity->ImpersonateClient () ;
  531. t_ServerSecurity->Release () ;
  532. }
  533. }
  534. }
  535. if ( a_OldSecurity )
  536. {
  537. a_OldSecurity->Release() ;
  538. }
  539. }
  540. else
  541. {
  542. t_Result = WBEM_E_OUT_OF_MEMORY ;
  543. }
  544. if ( a_OldContext )
  545. {
  546. a_OldContext->Release () ;
  547. }
  548. return t_Result ;
  549. }
  550. /******************************************************************************
  551. *
  552. * Name:
  553. *
  554. *
  555. * Description:
  556. *
  557. *
  558. *****************************************************************************/
  559. HRESULT ProviderSubSystem_Common_Globals :: GetProxy (
  560. REFIID a_InterfaceId ,
  561. IUnknown *a_Interface ,
  562. IUnknown *&a_Proxy
  563. )
  564. {
  565. IUnknown *t_Unknown = NULL ;
  566. HRESULT t_Result = a_Interface->QueryInterface (
  567. a_InterfaceId ,
  568. ( void ** ) & t_Unknown
  569. ) ;
  570. if ( SUCCEEDED ( t_Result ) )
  571. {
  572. IClientSecurity *t_ClientSecurity = NULL ;
  573. t_Result = a_Interface->QueryInterface (
  574. IID_IClientSecurity ,
  575. ( void ** ) & t_ClientSecurity
  576. ) ;
  577. if ( SUCCEEDED ( t_Result ) )
  578. {
  579. t_Result = t_ClientSecurity->CopyProxy (
  580. a_Interface ,
  581. ( IUnknown ** ) & a_Proxy
  582. ) ;
  583. t_ClientSecurity->Release () ;
  584. }
  585. else if ( t_Result == E_NOINTERFACE)
  586. {
  587. t_Result = WBEM_E_NOT_FOUND ;
  588. }
  589. t_Unknown->Release () ;
  590. }
  591. return t_Result ;
  592. }
  593. /******************************************************************************
  594. *
  595. * Name:
  596. *
  597. *
  598. * Description:
  599. *
  600. *
  601. *****************************************************************************/
  602. HRESULT ProviderSubSystem_Common_Globals :: GetProxy (
  603. ProxyContainer &a_Container ,
  604. ULONG a_ProxyIndex ,
  605. REFIID a_InterfaceId ,
  606. IUnknown *a_Interface ,
  607. IUnknown *&a_Proxy
  608. )
  609. {
  610. IUnknown *t_Unknown = NULL ;
  611. HRESULT t_Result = a_Interface->QueryInterface (
  612. a_InterfaceId ,
  613. ( void ** ) & t_Unknown
  614. ) ;
  615. if ( SUCCEEDED ( t_Result ) )
  616. {
  617. IClientSecurity *t_ClientSecurity = NULL ;
  618. t_Result = a_Interface->QueryInterface (
  619. IID_IClientSecurity ,
  620. ( void ** ) & t_ClientSecurity
  621. ) ;
  622. if ( SUCCEEDED ( t_Result ) )
  623. {
  624. WmiHelper :: EnterCriticalSection ( & a_Container.GetCriticalSection () ) ;
  625. WmiStatusCode t_StatusCode = a_Container.Top ( a_Proxy , a_ProxyIndex ) ;
  626. if ( t_StatusCode == e_StatusCode_Success )
  627. {
  628. t_StatusCode = a_Container.Reserve ( a_ProxyIndex ) ;
  629. }
  630. else
  631. {
  632. if ( a_Container.GetCurrentSize () < a_Container.GetTopSize () )
  633. {
  634. t_Result = t_ClientSecurity->CopyProxy (
  635. a_Interface ,
  636. ( IUnknown ** ) & a_Proxy
  637. ) ;
  638. if ( SUCCEEDED ( t_Result ) )
  639. {
  640. a_Container.SetCurrentSize ( a_Container.GetCurrentSize () + 1 ) ;
  641. }
  642. }
  643. else
  644. {
  645. t_Result = WBEM_E_OUT_OF_MEMORY ;
  646. }
  647. }
  648. WmiHelper :: LeaveCriticalSection ( & a_Container.GetCriticalSection () ) ;
  649. t_ClientSecurity->Release () ;
  650. }
  651. else if (E_NOINTERFACE == t_Result)
  652. {
  653. t_Result = WBEM_E_NOT_FOUND ;
  654. }
  655. t_Unknown->Release () ;
  656. }
  657. return t_Result ;
  658. }
  659. /******************************************************************************
  660. *
  661. * Name:
  662. *
  663. *
  664. * Description:
  665. *
  666. *
  667. *****************************************************************************/
  668. HRESULT ProviderSubSystem_Common_Globals :: SetCloaking (
  669. IUnknown *a_Unknown ,
  670. DWORD a_AuthenticationLevel ,
  671. DWORD a_ImpersonationLevel
  672. )
  673. {
  674. IClientSecurity *t_ClientSecurity = NULL ;
  675. HRESULT t_Result = a_Unknown->QueryInterface (
  676. IID_IClientSecurity ,
  677. ( void ** ) & t_ClientSecurity
  678. ) ;
  679. if ( SUCCEEDED ( t_Result ) )
  680. {
  681. t_Result = t_ClientSecurity->SetBlanket (
  682. a_Unknown ,
  683. RPC_C_AUTHN_WINNT ,
  684. RPC_C_AUTHZ_NONE ,
  685. NULL ,
  686. a_AuthenticationLevel ,
  687. a_ImpersonationLevel ,
  688. NULL ,
  689. EOAC_DYNAMIC_CLOAKING
  690. ) ;
  691. t_ClientSecurity->Release () ;
  692. }
  693. return t_Result ;
  694. }
  695. /******************************************************************************
  696. *
  697. * Name:
  698. *
  699. *
  700. * Description:
  701. *
  702. *
  703. *****************************************************************************/
  704. HRESULT ProviderSubSystem_Common_Globals :: SetCloaking (
  705. IUnknown *a_Unknown
  706. )
  707. {
  708. IClientSecurity *t_ClientSecurity = NULL ;
  709. HRESULT t_Result = a_Unknown->QueryInterface (
  710. IID_IClientSecurity ,
  711. ( void ** ) & t_ClientSecurity
  712. ) ;
  713. if ( SUCCEEDED ( t_Result ) )
  714. {
  715. t_Result = t_ClientSecurity->SetBlanket (
  716. a_Unknown ,
  717. RPC_C_AUTHN_WINNT ,
  718. RPC_C_AUTHZ_NONE ,
  719. NULL ,
  720. RPC_C_AUTHN_LEVEL_DEFAULT ,
  721. RPC_C_IMP_LEVEL_DEFAULT ,
  722. NULL ,
  723. EOAC_DYNAMIC_CLOAKING
  724. ) ;
  725. t_ClientSecurity->Release () ;
  726. }
  727. return t_Result ;
  728. }
  729. /******************************************************************************
  730. *
  731. * Name:
  732. *
  733. *
  734. * Description:
  735. *
  736. *
  737. *****************************************************************************/
  738. BOOL ProviderSubSystem_Common_Globals :: IsProxy ( IUnknown *a_Unknown )
  739. {
  740. BOOL t_IsProxy ;
  741. IClientSecurity *t_ClientSecurity = NULL ;
  742. HRESULT t_Result = a_Unknown->QueryInterface (
  743. IID_IClientSecurity ,
  744. ( void ** ) & t_ClientSecurity
  745. ) ;
  746. if ( SUCCEEDED ( t_Result ) )
  747. {
  748. t_IsProxy = TRUE ;
  749. t_ClientSecurity->Release () ;
  750. }
  751. else
  752. {
  753. t_IsProxy = FALSE ;
  754. }
  755. return t_IsProxy ;
  756. }
  757. /******************************************************************************
  758. *
  759. * Name:
  760. *
  761. *
  762. * Description:
  763. *
  764. *
  765. *****************************************************************************/
  766. DWORD ProviderSubSystem_Common_Globals :: GetCurrentImpersonationLevel ()
  767. {
  768. DWORD t_ImpersonationLevel = RPC_C_IMP_LEVEL_ANONYMOUS ;
  769. HANDLE t_ThreadToken = NULL ;
  770. BOOL t_Status = OpenThreadToken (
  771. GetCurrentThread() ,
  772. TOKEN_QUERY,
  773. TRUE ,
  774. &t_ThreadToken
  775. ) ;
  776. if ( t_Status )
  777. {
  778. SECURITY_IMPERSONATION_LEVEL t_Level = SecurityAnonymous ;
  779. DWORD t_Returned = 0 ;
  780. t_Status = GetTokenInformation (
  781. t_ThreadToken ,
  782. TokenImpersonationLevel ,
  783. & t_Level ,
  784. sizeof ( SECURITY_IMPERSONATION_LEVEL ) ,
  785. & t_Returned
  786. ) ;
  787. CloseHandle ( t_ThreadToken ) ;
  788. if ( t_Status == FALSE )
  789. {
  790. t_ImpersonationLevel = RPC_C_IMP_LEVEL_ANONYMOUS ;
  791. }
  792. else
  793. {
  794. switch ( t_Level )
  795. {
  796. case SecurityAnonymous:
  797. {
  798. t_ImpersonationLevel = RPC_C_IMP_LEVEL_ANONYMOUS ;
  799. }
  800. break ;
  801. case SecurityIdentification:
  802. {
  803. t_ImpersonationLevel = RPC_C_IMP_LEVEL_IDENTIFY ;
  804. }
  805. break ;
  806. case SecurityImpersonation:
  807. {
  808. t_ImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE ;
  809. }
  810. break ;
  811. case SecurityDelegation:
  812. {
  813. t_ImpersonationLevel = RPC_C_IMP_LEVEL_DELEGATE ;
  814. }
  815. break ;
  816. default:
  817. {
  818. t_ImpersonationLevel = RPC_C_IMP_LEVEL_ANONYMOUS ;
  819. }
  820. break ;
  821. }
  822. }
  823. }
  824. else
  825. {
  826. ULONG t_LastError = GetLastError () ;
  827. if ( t_LastError == ERROR_NO_IMPERSONATION_TOKEN || t_LastError == ERROR_NO_TOKEN )
  828. {
  829. t_ImpersonationLevel = RPC_C_IMP_LEVEL_DELEGATE ;
  830. }
  831. else
  832. {
  833. if ( t_LastError == ERROR_CANT_OPEN_ANONYMOUS )
  834. {
  835. t_ImpersonationLevel = RPC_C_IMP_LEVEL_ANONYMOUS ;
  836. }
  837. else
  838. {
  839. t_ImpersonationLevel = RPC_C_IMP_LEVEL_ANONYMOUS ;
  840. }
  841. }
  842. }
  843. return t_ImpersonationLevel ;
  844. }
  845. /******************************************************************************
  846. *
  847. * Name:
  848. *
  849. *
  850. * Description:
  851. *
  852. *
  853. *****************************************************************************/
  854. HRESULT ProviderSubSystem_Common_Globals :: EnableAllPrivileges ( HANDLE a_Token )
  855. {
  856. HRESULT t_Result = S_OK ;
  857. DWORD t_ReturnedLength = 0 ;
  858. BOOL t_Status = GetTokenInformation (
  859. a_Token ,
  860. TokenPrivileges ,
  861. NULL ,
  862. 0 ,
  863. & t_ReturnedLength
  864. ) ;
  865. UCHAR *t_Buffer = new UCHAR [ t_ReturnedLength ] ;
  866. if ( t_Buffer )
  867. {
  868. t_Status = GetTokenInformation (
  869. a_Token ,
  870. TokenPrivileges ,
  871. t_Buffer ,
  872. t_ReturnedLength ,
  873. & t_ReturnedLength
  874. ) ;
  875. if ( t_Status )
  876. {
  877. TOKEN_PRIVILEGES *t_Privileges = ( TOKEN_PRIVILEGES * ) t_Buffer ;
  878. BOOL bNeedToAdjust = FALSE;
  879. for ( ULONG t_Index = 0; t_Index < t_Privileges->PrivilegeCount ; t_Index ++ )
  880. {
  881. if (!(t_Privileges->Privileges [ t_Index ].Attributes & SE_PRIVILEGE_ENABLED))
  882. {
  883. bNeedToAdjust = TRUE;
  884. t_Privileges->Privileges [ t_Index ].Attributes |= SE_PRIVILEGE_ENABLED ;
  885. }
  886. }
  887. if (bNeedToAdjust)
  888. {
  889. t_Status = AdjustTokenPrivileges (
  890. a_Token,
  891. FALSE,
  892. t_Privileges ,
  893. 0,
  894. NULL,
  895. NULL
  896. ) ;
  897. }
  898. if ( t_Status == FALSE )
  899. {
  900. t_Result = WBEM_E_ACCESS_DENIED ;
  901. }
  902. }
  903. else
  904. {
  905. t_Status = WBEM_E_ACCESS_DENIED ;
  906. }
  907. delete [] t_Buffer ;
  908. }
  909. else
  910. {
  911. t_Result = WBEM_E_OUT_OF_MEMORY ;
  912. }
  913. return t_Result ;
  914. }
  915. /******************************************************************************
  916. *
  917. * Name:
  918. *
  919. *
  920. * Description:
  921. *
  922. *
  923. *****************************************************************************/
  924. HRESULT ProviderSubSystem_Common_Globals :: EnableAllPrivileges ()
  925. {
  926. HRESULT t_Result = S_OK ;
  927. HANDLE t_Token = NULL ;
  928. BOOL t_Status = TRUE ;
  929. t_Status = OpenThreadToken (
  930. GetCurrentThread (),
  931. TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES ,
  932. FALSE,
  933. &t_Token
  934. ) ;
  935. if ( t_Status )
  936. {
  937. DWORD t_ReturnedLength = 0 ;
  938. t_Status = GetTokenInformation (
  939. t_Token ,
  940. TokenPrivileges ,
  941. NULL ,
  942. 0 ,
  943. & t_ReturnedLength
  944. ) ;
  945. UCHAR *t_Buffer = new UCHAR [ t_ReturnedLength ] ;
  946. if ( t_Buffer )
  947. {
  948. t_Status = GetTokenInformation (
  949. t_Token ,
  950. TokenPrivileges ,
  951. t_Buffer ,
  952. t_ReturnedLength ,
  953. & t_ReturnedLength
  954. ) ;
  955. if ( t_Status )
  956. {
  957. TOKEN_PRIVILEGES *t_Privileges = ( TOKEN_PRIVILEGES * ) t_Buffer ;
  958. BOOL bNeedToAdjust = FALSE;
  959. for ( ULONG t_Index = 0; t_Index < t_Privileges->PrivilegeCount ; t_Index ++ )
  960. {
  961. if (!(t_Privileges->Privileges [ t_Index ].Attributes & SE_PRIVILEGE_ENABLED))
  962. {
  963. t_Privileges->Privileges [ t_Index ].Attributes |= SE_PRIVILEGE_ENABLED ;
  964. bNeedToAdjust = TRUE;
  965. }
  966. }
  967. if (bNeedToAdjust)
  968. {
  969. t_Status = AdjustTokenPrivileges (
  970. t_Token,
  971. FALSE,
  972. t_Privileges ,
  973. 0,
  974. NULL,
  975. NULL
  976. ) ;
  977. }
  978. if ( t_Status == FALSE )
  979. {
  980. t_Result = WBEM_E_ACCESS_DENIED ;
  981. }
  982. }
  983. else
  984. {
  985. t_Status = WBEM_E_ACCESS_DENIED ;
  986. }
  987. delete [] t_Buffer ;
  988. }
  989. else
  990. {
  991. t_Result = WBEM_E_OUT_OF_MEMORY ;
  992. }
  993. CloseHandle ( t_Token ) ;
  994. }
  995. else
  996. {
  997. DWORD t_LastError = GetLastError () ;
  998. t_Result = WBEM_E_ACCESS_DENIED;
  999. }
  1000. return t_Result ;
  1001. }
  1002. /******************************************************************************
  1003. *
  1004. * Name:
  1005. *
  1006. *
  1007. * Description:
  1008. *
  1009. *
  1010. *****************************************************************************/
  1011. HRESULT ProviderSubSystem_Common_Globals :: SetProxyState (
  1012. ProxyContainer &a_Container ,
  1013. ULONG a_ProxyIndex ,
  1014. REFIID a_InterfaceId ,
  1015. IUnknown *a_Interface ,
  1016. IUnknown *&a_Proxy ,
  1017. BOOL &a_Revert
  1018. )
  1019. {
  1020. a_Revert = FALSE ;
  1021. HRESULT t_Result = GetProxy ( a_Container , a_ProxyIndex , a_InterfaceId , a_Interface , a_Proxy ) ;
  1022. if ( SUCCEEDED ( t_Result ) )
  1023. {
  1024. t_Result = CoImpersonateClient () ;
  1025. if ( SUCCEEDED ( t_Result ) )
  1026. {
  1027. a_Revert = TRUE ;
  1028. // At this point, our thread token contains all the privileges that the
  1029. // client has enabled for us; however, those privileges are not enabled.
  1030. // Since we are calling into a proxied provider, we need to enable all
  1031. // these privileges so that they would propagate to the provider
  1032. // =====================================================================
  1033. HRESULT t_TempResult = EnableAllPrivileges () ;
  1034. // Get the token's impersonation level
  1035. // ===================================
  1036. DWORD t_ImpersonationLevel = GetCurrentImpersonationLevel () ;
  1037. if ( t_ImpersonationLevel == RPC_C_IMP_LEVEL_IMPERSONATE || t_ImpersonationLevel == RPC_C_IMP_LEVEL_DELEGATE )
  1038. {
  1039. }
  1040. else
  1041. {
  1042. t_Result = SetInterfaceSecurity (
  1043. a_Proxy ,
  1044. NULL ,
  1045. NULL ,
  1046. NULL ,
  1047. DWORD(RPC_C_AUTHN_LEVEL_DEFAULT),
  1048. RPC_C_IMP_LEVEL_IDENTIFY
  1049. ) ;
  1050. }
  1051. }
  1052. else
  1053. {
  1054. t_Result = WBEM_E_ACCESS_DENIED ;
  1055. }
  1056. if ( FAILED ( t_Result ) )
  1057. {
  1058. RevertProxyState (
  1059. a_Container ,
  1060. a_ProxyIndex ,
  1061. a_Proxy ,
  1062. a_Revert
  1063. ) ;
  1064. }
  1065. }
  1066. else
  1067. {
  1068. if ( t_Result == WBEM_E_NOT_FOUND )
  1069. {
  1070. }
  1071. else
  1072. {
  1073. t_Result = WBEM_E_CRITICAL_ERROR ;
  1074. }
  1075. }
  1076. return t_Result ;
  1077. }
  1078. /******************************************************************************
  1079. *
  1080. * Name:
  1081. *
  1082. *
  1083. * Description:
  1084. *
  1085. *
  1086. *****************************************************************************/
  1087. HRESULT ProviderSubSystem_Common_Globals :: RevertProxyState (
  1088. ProxyContainer &a_Container ,
  1089. ULONG a_ProxyIndex ,
  1090. IUnknown *a_Proxy ,
  1091. BOOL a_Revert
  1092. )
  1093. {
  1094. HRESULT t_Result = S_OK ;
  1095. WmiHelper :: EnterCriticalSection ( & a_Container.GetCriticalSection () ) ;
  1096. WmiStatusCode t_StatusCode = a_Container.Return ( a_Proxy , a_ProxyIndex ) ;
  1097. if ( t_StatusCode == e_StatusCode_Success )
  1098. {
  1099. }
  1100. else
  1101. {
  1102. a_Proxy->Release () ;
  1103. t_Result = WBEM_E_OUT_OF_MEMORY ;
  1104. }
  1105. WmiHelper :: LeaveCriticalSection ( & a_Container.GetCriticalSection () ) ;
  1106. if ( a_Revert )
  1107. {
  1108. t_Result = CoRevertToSelf () ;
  1109. }
  1110. return t_Result ;
  1111. }
  1112. /******************************************************************************
  1113. *
  1114. * Name:
  1115. *
  1116. *
  1117. * Description:
  1118. *
  1119. *
  1120. *****************************************************************************/
  1121. HRESULT ProviderSubSystem_Common_Globals :: ConstructIdentifyToken_SvcHost (
  1122. BOOL &a_Revert ,
  1123. DWORD a_ProcessIdentifier ,
  1124. HANDLE &a_IdentifyToken ,
  1125. ACCESS_ALLOWED_ACE *a_Ace ,
  1126. WORD a_AceSize,
  1127. SECURITY_IMPERSONATION_LEVEL impersonationLevel
  1128. )
  1129. {
  1130. HRESULT t_Result = S_OK ;
  1131. HANDLE t_ThreadToken = NULL ;
  1132. BOOL t_Status = OpenThreadToken (
  1133. GetCurrentThread () ,
  1134. MAXIMUM_ALLOWED ,
  1135. TRUE ,
  1136. & t_ThreadToken
  1137. ) ;
  1138. DWORD dummy = 0;
  1139. if ( t_Status )
  1140. {
  1141. CoRevertToSelf () ;
  1142. a_Revert = FALSE ;
  1143. SECURITY_DESCRIPTOR *t_SecurityDescriptor = NULL ;
  1144. DWORD t_LengthRequested = 0 ;
  1145. DWORD t_LengthReturned = 0 ;
  1146. t_Status = GetKernelObjectSecurity (
  1147. t_ThreadToken ,
  1148. DACL_SECURITY_INFORMATION ,
  1149. & t_SecurityDescriptor ,
  1150. t_LengthRequested ,
  1151. & t_LengthReturned
  1152. ) ;
  1153. if ( ( t_Status == FALSE ) && ( GetLastError () == ERROR_INSUFFICIENT_BUFFER ) )
  1154. {
  1155. t_SecurityDescriptor = ( SECURITY_DESCRIPTOR * ) new BYTE [ t_LengthReturned ] ;
  1156. if ( t_SecurityDescriptor )
  1157. {
  1158. t_LengthRequested = t_LengthReturned ;
  1159. t_Status = GetKernelObjectSecurity (
  1160. t_ThreadToken ,
  1161. DACL_SECURITY_INFORMATION ,
  1162. t_SecurityDescriptor ,
  1163. t_LengthRequested ,
  1164. & t_LengthReturned
  1165. ) ;
  1166. if ( t_LengthRequested != t_LengthReturned )
  1167. {
  1168. t_Result = WBEM_E_UNEXPECTED ;
  1169. }
  1170. }
  1171. else
  1172. {
  1173. t_Result = WBEM_E_OUT_OF_MEMORY ;
  1174. }
  1175. }
  1176. else
  1177. {
  1178. t_Result = WBEM_E_ACCESS_DENIED ;
  1179. }
  1180. HANDLE t_AdjustedThreadToken = NULL ;
  1181. if ( SUCCEEDED ( t_Result ) )
  1182. {
  1183. PACL t_ExtraDacl = NULL ;
  1184. ACL *t_Dacl = NULL ;
  1185. BOOL t_DaclPresent = FALSE ;
  1186. BOOL t_DaclDefaulted = FALSE ;
  1187. t_Status = GetSecurityDescriptorDacl (
  1188. t_SecurityDescriptor ,
  1189. & t_DaclPresent ,
  1190. & t_Dacl ,
  1191. & t_DaclDefaulted
  1192. ) ;
  1193. if ( t_Status )
  1194. {
  1195. ACL_SIZE_INFORMATION t_Size ;
  1196. if ( t_Dacl )
  1197. {
  1198. BOOL t_Status = GetAclInformation (
  1199. t_Dacl ,
  1200. & t_Size ,
  1201. sizeof ( t_Size ) ,
  1202. AclSizeInformation
  1203. );
  1204. if ( t_Status )
  1205. {
  1206. DWORD t_ExtraSize = t_Size.AclBytesInUse + t_Size.AclBytesFree + a_AceSize ;
  1207. t_ExtraDacl = ( PACL ) new BYTE [ t_ExtraSize ] ;
  1208. if ( t_ExtraDacl )
  1209. {
  1210. CopyMemory ( t_ExtraDacl , t_Dacl , t_Size.AclBytesInUse + t_Size.AclBytesFree ) ;
  1211. t_ExtraDacl->AclSize = t_ExtraSize ;
  1212. BOOL t_Status = :: AddAce ( t_ExtraDacl , ACL_REVISION, t_Size.AceCount , a_Ace , a_AceSize ) ;
  1213. if ( t_Status )
  1214. {
  1215. SECURITY_DESCRIPTOR t_AdjustedSecurityDescriptor ;
  1216. if ( SUCCEEDED ( t_Result ) )
  1217. {
  1218. BOOL t_Status = InitializeSecurityDescriptor ( & t_AdjustedSecurityDescriptor , SECURITY_DESCRIPTOR_REVISION ) ;
  1219. if ( t_Status )
  1220. {
  1221. t_Status = SetSecurityDescriptorDacl (
  1222. & t_AdjustedSecurityDescriptor ,
  1223. t_DaclPresent ,
  1224. t_ExtraDacl ,
  1225. t_DaclDefaulted
  1226. ) ;
  1227. if ( t_Status )
  1228. {
  1229. SECURITY_ATTRIBUTES t_SecurityAttributes ;
  1230. t_SecurityAttributes.nLength = GetSecurityDescriptorLength ( & t_AdjustedSecurityDescriptor ) ;
  1231. t_SecurityAttributes.lpSecurityDescriptor = & t_AdjustedSecurityDescriptor ;
  1232. t_SecurityAttributes.bInheritHandle = FALSE ;
  1233. t_Status = DuplicateTokenEx (
  1234. t_ThreadToken,
  1235. MAXIMUM_ALLOWED ,//| TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY,
  1236. & t_SecurityAttributes ,
  1237. impersonationLevel ,
  1238. TokenImpersonation ,
  1239. & t_AdjustedThreadToken
  1240. ) ;
  1241. if ( t_Status == FALSE )
  1242. {
  1243. t_Result = WBEM_E_ACCESS_DENIED ;
  1244. }
  1245. }
  1246. else
  1247. {
  1248. t_Result = WBEM_E_CRITICAL_ERROR ;
  1249. }
  1250. }
  1251. else
  1252. {
  1253. t_Result = WBEM_E_UNEXPECTED ;
  1254. }
  1255. }
  1256. }
  1257. else
  1258. {
  1259. t_Result = WBEM_E_CRITICAL_ERROR ;
  1260. }
  1261. delete [] ( BYTE * ) t_ExtraDacl ;
  1262. }
  1263. else
  1264. {
  1265. t_Result = WBEM_E_OUT_OF_MEMORY ;
  1266. }
  1267. }
  1268. else
  1269. {
  1270. t_Result = WBEM_E_CRITICAL_ERROR ;
  1271. }
  1272. }
  1273. else
  1274. {
  1275. t_Result = WBEM_E_CRITICAL_ERROR ;
  1276. }
  1277. }
  1278. else
  1279. {
  1280. t_Result = WBEM_E_CRITICAL_ERROR ;
  1281. }
  1282. }
  1283. if ( SUCCEEDED ( t_Result ) )
  1284. {
  1285. HANDLE t_ProcessHandle = OpenProcess (
  1286. PROCESS_DUP_HANDLE,
  1287. FALSE ,
  1288. a_ProcessIdentifier
  1289. ) ;
  1290. if ( t_ProcessHandle )
  1291. {
  1292. t_Status = DuplicateHandle (
  1293. GetCurrentProcess() ,
  1294. t_AdjustedThreadToken ,
  1295. t_ProcessHandle ,
  1296. & a_IdentifyToken ,
  1297. 0,
  1298. TRUE ,
  1299. DUPLICATE_SAME_ACCESS
  1300. ) ;
  1301. if ( t_Status )
  1302. {
  1303. }
  1304. else
  1305. {
  1306. t_Result = WBEM_E_ACCESS_DENIED ;
  1307. }
  1308. CloseHandle ( t_ProcessHandle ) ;
  1309. }
  1310. else
  1311. {
  1312. t_Result = WBEM_E_ACCESS_DENIED ;
  1313. }
  1314. }
  1315. if ( t_SecurityDescriptor )
  1316. {
  1317. delete [] ( BYTE * ) t_SecurityDescriptor ;
  1318. }
  1319. if ( t_AdjustedThreadToken )
  1320. {
  1321. CloseHandle ( t_AdjustedThreadToken ) ;
  1322. }
  1323. CloseHandle ( t_ThreadToken ) ;
  1324. }
  1325. else
  1326. {
  1327. t_Result = WBEM_E_ACCESS_DENIED ;
  1328. }
  1329. return t_Result ;
  1330. }
  1331. /******************************************************************************
  1332. *
  1333. * Name:
  1334. *
  1335. *
  1336. * Description:
  1337. *
  1338. *
  1339. *****************************************************************************/
  1340. HRESULT ProviderSubSystem_Common_Globals :: ConstructIdentifyToken_PrvHost (
  1341. BOOL &a_Revert ,
  1342. DWORD a_ProcessIdentifier ,
  1343. HANDLE &a_IdentifyToken ,
  1344. ACCESS_ALLOWED_ACE *a_Ace ,
  1345. WORD a_AceSize
  1346. )
  1347. {
  1348. HRESULT t_Result = S_OK ;
  1349. HANDLE t_ThreadToken = NULL ;
  1350. BOOL t_Status = OpenThreadToken (
  1351. GetCurrentThread () ,
  1352. MAXIMUM_ALLOWED ,
  1353. TRUE ,
  1354. & t_ThreadToken
  1355. ) ;
  1356. if ( t_Status )
  1357. {
  1358. CoRevertToSelf () ;
  1359. a_Revert = FALSE ;
  1360. SECURITY_DESCRIPTOR *t_SecurityDescriptor = NULL ;
  1361. DWORD t_LengthRequested = 0 ;
  1362. DWORD t_LengthReturned = 0 ;
  1363. t_Status = GetKernelObjectSecurity (
  1364. t_ThreadToken ,
  1365. DACL_SECURITY_INFORMATION ,
  1366. & t_SecurityDescriptor ,
  1367. t_LengthRequested ,
  1368. & t_LengthReturned
  1369. ) ;
  1370. if ( ( t_Status == FALSE ) && ( GetLastError () == ERROR_INSUFFICIENT_BUFFER ) )
  1371. {
  1372. t_SecurityDescriptor = ( SECURITY_DESCRIPTOR * ) new BYTE [ t_LengthReturned ] ;
  1373. if ( t_SecurityDescriptor )
  1374. {
  1375. t_LengthRequested = t_LengthReturned ;
  1376. t_Status = GetKernelObjectSecurity (
  1377. t_ThreadToken ,
  1378. DACL_SECURITY_INFORMATION ,
  1379. t_SecurityDescriptor ,
  1380. t_LengthRequested ,
  1381. & t_LengthReturned
  1382. ) ;
  1383. if ( t_LengthRequested != t_LengthReturned )
  1384. {
  1385. t_Result = WBEM_E_UNEXPECTED ;
  1386. }
  1387. }
  1388. else
  1389. {
  1390. t_Result = WBEM_E_OUT_OF_MEMORY ;
  1391. }
  1392. }
  1393. else
  1394. {
  1395. t_Result = WBEM_E_ACCESS_DENIED ;
  1396. }
  1397. if ( SUCCEEDED ( t_Result ) )
  1398. {
  1399. PACL t_ExtraDacl = NULL ;
  1400. ACL *t_Dacl = NULL ;
  1401. BOOL t_DaclPresent = FALSE ;
  1402. BOOL t_DaclDefaulted = FALSE ;
  1403. t_Status = GetSecurityDescriptorDacl (
  1404. t_SecurityDescriptor ,
  1405. & t_DaclPresent ,
  1406. & t_Dacl ,
  1407. & t_DaclDefaulted
  1408. ) ;
  1409. if ( t_Status )
  1410. {
  1411. ACL_SIZE_INFORMATION t_Size ;
  1412. if ( t_Dacl )
  1413. {
  1414. BOOL t_Status = GetAclInformation (
  1415. t_Dacl ,
  1416. & t_Size ,
  1417. sizeof ( t_Size ) ,
  1418. AclSizeInformation
  1419. );
  1420. if ( t_Status )
  1421. {
  1422. DWORD t_ExtraSize = t_Size.AclBytesInUse + t_Size.AclBytesFree + a_AceSize ;
  1423. t_ExtraDacl = ( PACL ) new BYTE [ t_ExtraSize ] ;
  1424. if ( t_ExtraDacl )
  1425. {
  1426. CopyMemory ( t_ExtraDacl , t_Dacl , t_Size.AclBytesInUse + t_Size.AclBytesFree ) ;
  1427. t_ExtraDacl->AclSize = t_ExtraSize ;
  1428. BOOL t_Status = :: AddAce ( t_ExtraDacl , ACL_REVISION, t_Size.AceCount , a_Ace , a_AceSize ) ;
  1429. if ( t_Status )
  1430. {
  1431. SECURITY_DESCRIPTOR t_AdjustedSecurityDescriptor ;
  1432. if ( SUCCEEDED ( t_Result ) )
  1433. {
  1434. BOOL t_Status = InitializeSecurityDescriptor ( & t_AdjustedSecurityDescriptor , SECURITY_DESCRIPTOR_REVISION ) ;
  1435. if ( t_Status )
  1436. {
  1437. t_Status = SetSecurityDescriptorDacl (
  1438. & t_AdjustedSecurityDescriptor ,
  1439. t_DaclPresent ,
  1440. t_ExtraDacl ,
  1441. t_DaclDefaulted
  1442. ) ;
  1443. if ( t_Status )
  1444. {
  1445. SECURITY_ATTRIBUTES t_SecurityAttributes ;
  1446. t_SecurityAttributes.nLength = GetSecurityDescriptorLength ( & t_AdjustedSecurityDescriptor ) ;
  1447. t_SecurityAttributes.lpSecurityDescriptor = & t_AdjustedSecurityDescriptor ;
  1448. t_SecurityAttributes.bInheritHandle = FALSE ;
  1449. t_Status = DuplicateTokenEx (
  1450. t_ThreadToken,
  1451. DUPLICATE_SAME_ACCESS ,
  1452. & t_SecurityAttributes ,
  1453. ( SECURITY_IMPERSONATION_LEVEL ) SecurityIdentification ,
  1454. TokenImpersonation ,
  1455. & a_IdentifyToken
  1456. ) ;
  1457. if ( t_Status == FALSE )
  1458. {
  1459. t_Result = WBEM_E_ACCESS_DENIED ;
  1460. }
  1461. }
  1462. else
  1463. {
  1464. t_Result = WBEM_E_CRITICAL_ERROR ;
  1465. }
  1466. }
  1467. else
  1468. {
  1469. t_Result = WBEM_E_UNEXPECTED ;
  1470. }
  1471. }
  1472. }
  1473. else
  1474. {
  1475. t_Result = WBEM_E_CRITICAL_ERROR ;
  1476. }
  1477. delete [] ( BYTE * ) t_ExtraDacl ;
  1478. }
  1479. else
  1480. {
  1481. t_Result = WBEM_E_OUT_OF_MEMORY ;
  1482. }
  1483. }
  1484. else
  1485. {
  1486. t_Result = WBEM_E_CRITICAL_ERROR ;
  1487. }
  1488. }
  1489. else
  1490. {
  1491. t_Result = WBEM_E_CRITICAL_ERROR ;
  1492. }
  1493. }
  1494. else
  1495. {
  1496. t_Result = WBEM_E_CRITICAL_ERROR ;
  1497. }
  1498. }
  1499. if ( t_SecurityDescriptor )
  1500. {
  1501. delete [] ( BYTE * ) t_SecurityDescriptor ;
  1502. }
  1503. CloseHandle ( t_ThreadToken ) ;
  1504. }
  1505. else
  1506. {
  1507. t_Result = WBEM_E_ACCESS_DENIED ;
  1508. }
  1509. return t_Result ;
  1510. }
  1511. HRESULT ProviderSubSystem_Common_Globals :: RevertProxyState_SvcHost (
  1512. ProxyContainer &a_Container ,
  1513. ULONG a_ProxyIndex ,
  1514. IUnknown *a_Proxy ,
  1515. BOOL a_Revert ,
  1516. DWORD a_ProcessIdentifier ,
  1517. HANDLE a_IdentifyToken
  1518. )
  1519. {
  1520. HRESULT t_Result = S_OK ;
  1521. WmiHelper :: EnterCriticalSection ( & a_Container.GetCriticalSection () ) ;
  1522. WmiStatusCode t_StatusCode = a_Container.Return ( a_Proxy , a_ProxyIndex ) ;
  1523. if ( t_StatusCode == e_StatusCode_Success )
  1524. {
  1525. }
  1526. else
  1527. {
  1528. a_Proxy->Release () ;
  1529. t_Result = WBEM_E_OUT_OF_MEMORY ;
  1530. }
  1531. WmiHelper :: LeaveCriticalSection ( & a_Container.GetCriticalSection () ) ;
  1532. if ( a_Revert )
  1533. {
  1534. t_Result = CoRevertToSelf () ;
  1535. }
  1536. return t_Result ;
  1537. }
  1538. /******************************************************************************
  1539. *
  1540. * Name:
  1541. *
  1542. *
  1543. * Description:
  1544. *
  1545. *
  1546. *****************************************************************************/
  1547. HRESULT ProviderSubSystem_Common_Globals :: SetProxyState_PrvHost (
  1548. ProxyContainer &a_Container ,
  1549. ULONG a_ProxyIndex ,
  1550. REFIID a_InterfaceId ,
  1551. IUnknown *a_Interface ,
  1552. IUnknown *&a_Proxy ,
  1553. BOOL &a_Revert ,
  1554. DWORD a_ProcessIdentifier ,
  1555. HANDLE &a_IdentifyToken
  1556. )
  1557. {
  1558. a_Revert = FALSE ;
  1559. HRESULT t_Result = GetProxy ( a_Container , a_ProxyIndex , a_InterfaceId , a_Interface , a_Proxy ) ;
  1560. if ( SUCCEEDED ( t_Result ) )
  1561. {
  1562. t_Result = CoImpersonateClient () ;
  1563. if ( SUCCEEDED ( t_Result ) )
  1564. {
  1565. a_Revert = TRUE ;
  1566. // At this point, our thread token contains all the privileges that the
  1567. // client has enabled for us; however, those privileges are not enabled.
  1568. // Since we are calling into a proxied provider, we need to enable all
  1569. // these privileges so that they would propagate to the provider
  1570. // =====================================================================
  1571. HRESULT t_TempResult = EnableAllPrivileges () ;
  1572. // Get the token's impersonation level
  1573. // ===================================
  1574. DWORD t_ImpersonationLevel = GetCurrentImpersonationLevel () ;
  1575. if ( t_ImpersonationLevel == RPC_C_IMP_LEVEL_IMPERSONATE || t_ImpersonationLevel == RPC_C_IMP_LEVEL_DELEGATE )
  1576. {
  1577. a_IdentifyToken = 0 ;
  1578. }
  1579. else
  1580. {
  1581. t_Result = ConstructIdentifyToken_PrvHost (
  1582. a_Revert ,
  1583. a_ProcessIdentifier ,
  1584. a_IdentifyToken ,
  1585. s_Token_All_Access_System_ACE ,
  1586. s_System_ACESize
  1587. ) ;
  1588. if ( SUCCEEDED ( t_Result ) )
  1589. {
  1590. t_Result = SetInterfaceSecurity (
  1591. a_Proxy ,
  1592. NULL ,
  1593. NULL ,
  1594. NULL ,
  1595. DWORD(RPC_C_AUTHN_LEVEL_DEFAULT),
  1596. RPC_C_IMP_LEVEL_IDENTIFY
  1597. ) ;
  1598. }
  1599. }
  1600. CoRevertToSelf () ;
  1601. }
  1602. else
  1603. {
  1604. t_Result = WBEM_E_ACCESS_DENIED ;
  1605. }
  1606. }
  1607. else
  1608. {
  1609. if ( t_Result == WBEM_E_NOT_FOUND )
  1610. {
  1611. }
  1612. else
  1613. {
  1614. t_Result = WBEM_E_CRITICAL_ERROR ;
  1615. }
  1616. }
  1617. return t_Result ;
  1618. }
  1619. /******************************************************************************
  1620. *
  1621. * Name:
  1622. *
  1623. *
  1624. * Description:
  1625. *
  1626. *
  1627. *****************************************************************************/
  1628. HRESULT ProviderSubSystem_Common_Globals :: RevertProxyState_PrvHost (
  1629. ProxyContainer &a_Container ,
  1630. ULONG a_ProxyIndex ,
  1631. IUnknown *a_Proxy ,
  1632. BOOL a_Revert ,
  1633. DWORD a_ProcessIdentifier ,
  1634. HANDLE a_IdentifyToken
  1635. )
  1636. {
  1637. HRESULT t_Result = S_OK ;
  1638. WmiHelper :: EnterCriticalSection ( & a_Container.GetCriticalSection () ) ;
  1639. WmiStatusCode t_StatusCode = a_Container.Return ( a_Proxy , a_ProxyIndex ) ;
  1640. if ( t_StatusCode == e_StatusCode_Success )
  1641. {
  1642. }
  1643. else
  1644. {
  1645. a_Proxy->Release () ;
  1646. t_Result = WBEM_E_OUT_OF_MEMORY ;
  1647. }
  1648. WmiHelper :: LeaveCriticalSection ( & a_Container.GetCriticalSection () ) ;
  1649. if ( a_Revert )
  1650. {
  1651. t_Result = CoRevertToSelf () ;
  1652. }
  1653. CloseHandle ( a_IdentifyToken ) ;
  1654. return t_Result ;
  1655. }
  1656. /******************************************************************************
  1657. *
  1658. * Name:
  1659. *
  1660. *
  1661. * Description:
  1662. *
  1663. *
  1664. *****************************************************************************/
  1665. HRESULT ProviderSubSystem_Common_Globals :: RevertProxyState_SvcHost (
  1666. IUnknown *a_Proxy ,
  1667. BOOL a_Revert ,
  1668. DWORD a_ProcessIdentifier ,
  1669. HANDLE a_IdentifyToken
  1670. )
  1671. {
  1672. HRESULT t_Result = S_OK ;
  1673. a_Proxy->Release () ;
  1674. if ( a_Revert )
  1675. {
  1676. t_Result = CoRevertToSelf () ;
  1677. }
  1678. return t_Result ;
  1679. }
  1680. /******************************************************************************
  1681. *
  1682. * Name:
  1683. *
  1684. *
  1685. * Description:
  1686. *
  1687. *
  1688. *****************************************************************************/
  1689. HRESULT ProviderSubSystem_Common_Globals :: SetProxyState_PrvHost (
  1690. REFIID a_InterfaceId ,
  1691. IUnknown *a_Interface ,
  1692. IUnknown *&a_Proxy ,
  1693. BOOL &a_Revert ,
  1694. DWORD a_ProcessIdentifier ,
  1695. HANDLE &a_IdentifyToken
  1696. )
  1697. {
  1698. a_Revert = FALSE ;
  1699. HRESULT t_Result = GetProxy ( a_InterfaceId , a_Interface , a_Proxy ) ;
  1700. if ( SUCCEEDED ( t_Result ) )
  1701. {
  1702. t_Result = CoImpersonateClient () ;
  1703. if ( SUCCEEDED ( t_Result ) )
  1704. {
  1705. a_Revert = TRUE ;
  1706. // At this point, our thread token contains all the privileges that the
  1707. // client has enabled for us; however, those privileges are not enabled.
  1708. // Since we are calling into a proxied provider, we need to enable all
  1709. // these privileges so that they would propagate to the provider
  1710. // =====================================================================
  1711. HRESULT t_TempResult = EnableAllPrivileges () ;
  1712. // Get the token's impersonation level
  1713. // ===================================
  1714. DWORD t_ImpersonationLevel = GetCurrentImpersonationLevel () ;
  1715. if ( t_ImpersonationLevel == RPC_C_IMP_LEVEL_IMPERSONATE || t_ImpersonationLevel == RPC_C_IMP_LEVEL_DELEGATE )
  1716. {
  1717. a_IdentifyToken = 0 ;
  1718. }
  1719. else
  1720. {
  1721. t_Result = ConstructIdentifyToken_PrvHost (
  1722. a_Revert ,
  1723. a_ProcessIdentifier ,
  1724. a_IdentifyToken ,
  1725. s_Token_All_Access_System_ACE ,
  1726. s_System_ACESize
  1727. ) ;
  1728. if ( SUCCEEDED ( t_Result ) )
  1729. {
  1730. t_Result = SetInterfaceSecurity (
  1731. a_Proxy ,
  1732. NULL ,
  1733. NULL ,
  1734. NULL ,
  1735. DWORD(RPC_C_AUTHN_LEVEL_DEFAULT),
  1736. RPC_C_IMP_LEVEL_IDENTIFY
  1737. ) ;
  1738. }
  1739. }
  1740. CoRevertToSelf () ;
  1741. }
  1742. else
  1743. {
  1744. t_Result = WBEM_E_ACCESS_DENIED ;
  1745. }
  1746. }
  1747. else
  1748. {
  1749. if ( t_Result == WBEM_E_NOT_FOUND )
  1750. {
  1751. }
  1752. else
  1753. {
  1754. t_Result = WBEM_E_CRITICAL_ERROR ;
  1755. }
  1756. }
  1757. return t_Result ;
  1758. }
  1759. /******************************************************************************
  1760. *
  1761. * Name:
  1762. *
  1763. *
  1764. * Description:
  1765. *
  1766. *
  1767. *****************************************************************************/
  1768. HRESULT ProviderSubSystem_Common_Globals :: RevertProxyState_PrvHost (
  1769. IUnknown *a_Proxy ,
  1770. BOOL a_Revert ,
  1771. DWORD a_ProcessIdentifier ,
  1772. HANDLE a_IdentifyToken
  1773. )
  1774. {
  1775. HRESULT t_Result = S_OK ;
  1776. a_Proxy->Release () ;
  1777. if ( a_Revert )
  1778. {
  1779. t_Result = CoRevertToSelf () ;
  1780. }
  1781. CloseHandle ( a_IdentifyToken ) ;
  1782. return t_Result ;
  1783. }
  1784. /******************************************************************************
  1785. *
  1786. * Name:
  1787. *
  1788. *
  1789. * Description:
  1790. *
  1791. *
  1792. *****************************************************************************/
  1793. HRESULT ProviderSubSystem_Common_Globals :: SetCallState (
  1794. IUnknown *a_Interface ,
  1795. BOOL &a_Revert
  1796. )
  1797. {
  1798. a_Revert = FALSE ;
  1799. HRESULT t_Result = S_OK ;
  1800. if ( IsProxy ( a_Interface ) )
  1801. {
  1802. t_Result = CoImpersonateClient () ;
  1803. if ( SUCCEEDED ( t_Result ) )
  1804. {
  1805. a_Revert = TRUE ;
  1806. // At this point, our thread token contains all the privileges that the
  1807. // client has enabled for us; however, those privileges are not enabled.
  1808. // Since we are calling into a proxied provider, we need to enable all
  1809. // these privileges so that they would propagate to the provider
  1810. // =====================================================================
  1811. t_Result = EnableAllPrivileges () ;
  1812. if ( SUCCEEDED ( t_Result ) )
  1813. {
  1814. }
  1815. else
  1816. {
  1817. CoRevertToSelf () ;
  1818. a_Revert = FALSE ;
  1819. t_Result = WBEM_E_ACCESS_DENIED ;
  1820. }
  1821. }
  1822. else
  1823. {
  1824. t_Result = WBEM_E_ACCESS_DENIED ;
  1825. }
  1826. }
  1827. else
  1828. {
  1829. t_Result = WBEM_E_NOT_FOUND ;
  1830. }
  1831. return t_Result ;
  1832. }
  1833. /******************************************************************************
  1834. *
  1835. * Name:
  1836. *
  1837. *
  1838. * Description:
  1839. *
  1840. *
  1841. *****************************************************************************/
  1842. HRESULT ProviderSubSystem_Common_Globals :: RevertCallState (
  1843. BOOL a_Revert
  1844. )
  1845. {
  1846. HRESULT t_Result = S_OK ;
  1847. if ( a_Revert )
  1848. {
  1849. t_Result = CoRevertToSelf () ;
  1850. }
  1851. return t_Result ;
  1852. }
  1853. /******************************************************************************
  1854. *
  1855. * Name:
  1856. *
  1857. *
  1858. * Description:
  1859. *
  1860. *
  1861. *****************************************************************************/
  1862. HRESULT ProviderSubSystem_Common_Globals :: SetProxyState (
  1863. REFIID a_InterfaceId ,
  1864. IUnknown *a_Interface ,
  1865. IUnknown *&a_Proxy ,
  1866. BOOL &a_Revert
  1867. )
  1868. {
  1869. a_Revert = FALSE ;
  1870. HRESULT t_Result = GetProxy ( a_InterfaceId , a_Interface , a_Proxy ) ;
  1871. if ( SUCCEEDED ( t_Result ) )
  1872. {
  1873. t_Result = CoImpersonateClient () ;
  1874. if ( SUCCEEDED ( t_Result ) )
  1875. {
  1876. a_Revert = TRUE ;
  1877. // At this point, our thread token contains all the privileges that the
  1878. // client has enabled for us; however, those privileges are not enabled.
  1879. // Since we are calling into a proxied provider, we need to enable all
  1880. // these privileges so that they would propagate to the provider
  1881. // =====================================================================
  1882. HRESULT t_TempResult = EnableAllPrivileges () ;
  1883. // Get the token's impersonation level
  1884. // ===================================
  1885. DWORD t_ImpersonationLevel = GetCurrentImpersonationLevel () ;
  1886. if ( t_ImpersonationLevel == RPC_C_IMP_LEVEL_IMPERSONATE || t_ImpersonationLevel == RPC_C_IMP_LEVEL_DELEGATE )
  1887. {
  1888. }
  1889. else
  1890. {
  1891. t_Result = SetInterfaceSecurity (
  1892. a_Proxy ,
  1893. NULL ,
  1894. NULL ,
  1895. NULL ,
  1896. DWORD(RPC_C_AUTHN_LEVEL_DEFAULT),
  1897. RPC_C_IMP_LEVEL_IDENTIFY
  1898. ) ;
  1899. }
  1900. }
  1901. else
  1902. {
  1903. t_Result = WBEM_E_ACCESS_DENIED ;
  1904. }
  1905. }
  1906. else
  1907. {
  1908. if ( t_Result == WBEM_E_NOT_FOUND )
  1909. {
  1910. }
  1911. else
  1912. {
  1913. t_Result = WBEM_E_CRITICAL_ERROR ;
  1914. }
  1915. }
  1916. return t_Result ;
  1917. }
  1918. /******************************************************************************
  1919. *
  1920. * Name:
  1921. *
  1922. *
  1923. * Description:
  1924. *
  1925. *
  1926. *****************************************************************************/
  1927. HRESULT ProviderSubSystem_Common_Globals :: RevertProxyState ( IUnknown *a_Proxy , BOOL a_Revert )
  1928. {
  1929. HRESULT t_Result = S_OK ;
  1930. if ( a_Revert )
  1931. {
  1932. t_Result = CoRevertToSelf () ;
  1933. }
  1934. a_Proxy->Release () ;
  1935. return t_Result ;
  1936. }
  1937. /******************************************************************************
  1938. *
  1939. * Name:
  1940. *
  1941. *
  1942. * Description:
  1943. *
  1944. *
  1945. *****************************************************************************/
  1946. HRESULT ProviderSubSystem_Common_Globals :: Load_DWORD ( HKEY a_Key , LPCWSTR a_Name , DWORD &a_Value )
  1947. {
  1948. HRESULT t_Result = S_OK ;
  1949. DWORD t_ValueType = REG_DWORD ;
  1950. DWORD t_Data = 0 ;
  1951. DWORD t_DataSize = sizeof ( t_ValueType ) ;
  1952. LONG t_RegResult = OS::RegQueryValueEx (
  1953. a_Key ,
  1954. a_Name ,
  1955. 0 ,
  1956. & t_ValueType ,
  1957. LPBYTE ( & t_Data ) ,
  1958. & t_DataSize
  1959. ) ;
  1960. if ( ( t_RegResult == ERROR_SUCCESS ) && ( t_ValueType == REG_DWORD ) )
  1961. {
  1962. a_Value = t_Data ;
  1963. }
  1964. else
  1965. {
  1966. t_Result = ERROR_FILE_NOT_FOUND ;
  1967. }
  1968. return t_Result ;
  1969. }
  1970. /******************************************************************************
  1971. *
  1972. * Name:
  1973. *
  1974. *
  1975. * Description:
  1976. *
  1977. *
  1978. *****************************************************************************/
  1979. HRESULT ProviderSubSystem_Common_Globals :: Load_String ( HKEY a_Key , LPCWSTR a_Name , BSTR &a_Value )
  1980. {
  1981. HRESULT t_Result = S_OK ;
  1982. DWORD t_ValueType = REG_SZ ;
  1983. wchar_t *t_Data = NULL ;
  1984. DWORD t_DataSize = 0 ;
  1985. LONG t_RegResult = OS::RegQueryValueEx (
  1986. a_Key ,
  1987. a_Name ,
  1988. 0 ,
  1989. & t_ValueType ,
  1990. NULL ,
  1991. & t_DataSize
  1992. ) ;
  1993. if ( ( t_RegResult == ERROR_SUCCESS ) && ( t_ValueType == REG_SZ ) )
  1994. {
  1995. t_Data = new wchar_t [ t_DataSize / sizeof ( wchar_t ) ] ;
  1996. if ( t_Data )
  1997. {
  1998. t_RegResult = OS::RegQueryValueEx (
  1999. a_Key ,
  2000. a_Name ,
  2001. 0 ,
  2002. & t_ValueType ,
  2003. LPBYTE ( t_Data ) ,
  2004. & t_DataSize
  2005. ) ;
  2006. if ( t_RegResult == ERROR_SUCCESS )
  2007. {
  2008. a_Value = SysAllocString ( t_Data ) ;
  2009. if ( a_Value == NULL )
  2010. {
  2011. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2012. }
  2013. delete [] t_Data ;
  2014. }
  2015. else
  2016. {
  2017. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2018. DWORD t_LastError = GetLastError () ;
  2019. }
  2020. }
  2021. else
  2022. {
  2023. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2024. }
  2025. }
  2026. else
  2027. {
  2028. t_Result = ERROR_FILE_NOT_FOUND ;
  2029. }
  2030. return t_Result ;
  2031. }
  2032. /******************************************************************************
  2033. *
  2034. * Name:
  2035. *
  2036. *
  2037. * Description:
  2038. *
  2039. *
  2040. *****************************************************************************/
  2041. HRESULT ProviderSubSystem_Common_Globals :: Load_ByteArray ( HKEY a_Key , LPCWSTR a_Name , BYTE *&a_Value , DWORD &a_ValueLength )
  2042. {
  2043. HRESULT t_Result = S_OK ;
  2044. DWORD t_ValueType = REG_BINARY ;
  2045. BYTE *t_Data = NULL ;
  2046. DWORD t_DataSize = 0 ;
  2047. LONG t_RegResult = OS::RegQueryValueEx (
  2048. a_Key ,
  2049. a_Name ,
  2050. 0 ,
  2051. & t_ValueType ,
  2052. NULL ,
  2053. & t_DataSize
  2054. ) ;
  2055. if ( ( t_RegResult == ERROR_SUCCESS ) && ( t_ValueType == REG_BINARY ) )
  2056. {
  2057. t_Data = new BYTE [ t_DataSize ] ;
  2058. if ( t_Data )
  2059. {
  2060. t_RegResult = OS::RegQueryValueEx (
  2061. a_Key ,
  2062. a_Name ,
  2063. 0 ,
  2064. & t_ValueType ,
  2065. LPBYTE ( t_Data ) ,
  2066. & t_DataSize
  2067. ) ;
  2068. if ( t_RegResult == ERROR_SUCCESS )
  2069. {
  2070. a_Value = t_Data ;
  2071. a_ValueLength = t_DataSize ;
  2072. }
  2073. else
  2074. {
  2075. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2076. }
  2077. }
  2078. else
  2079. {
  2080. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2081. }
  2082. }
  2083. else
  2084. {
  2085. t_Result = ERROR_FILE_NOT_FOUND ;
  2086. }
  2087. return t_Result ;
  2088. }
  2089. /******************************************************************************
  2090. *
  2091. * Name:
  2092. *
  2093. *
  2094. * Description:
  2095. *
  2096. *
  2097. *****************************************************************************/
  2098. HRESULT ProviderSubSystem_Common_Globals :: Save_DWORD ( HKEY a_Key , LPCWSTR a_Name , DWORD a_Value )
  2099. {
  2100. HRESULT t_Result = S_OK ;
  2101. DWORD t_ValueType = REG_DWORD ;
  2102. DWORD t_DataSize = sizeof ( t_ValueType ) ;
  2103. LONG t_RegResult = OS::RegSetValueEx (
  2104. a_Key ,
  2105. a_Name ,
  2106. 0 ,
  2107. t_ValueType ,
  2108. LPBYTE ( & a_Value ) ,
  2109. t_DataSize
  2110. ) ;
  2111. if ( t_RegResult != ERROR_SUCCESS )
  2112. {
  2113. t_Result = WBEM_E_INVALID_PROVIDER_REGISTRATION ;
  2114. }
  2115. return t_Result ;
  2116. }
  2117. /******************************************************************************
  2118. *
  2119. * Name:
  2120. *
  2121. *
  2122. * Description:
  2123. *
  2124. *
  2125. *****************************************************************************/
  2126. HRESULT ProviderSubSystem_Common_Globals :: Save_String ( HKEY a_Key , LPCWSTR a_Name , BSTR a_Value )
  2127. {
  2128. HRESULT t_Result = S_OK ;
  2129. DWORD t_ValueType = REG_SZ ;
  2130. DWORD t_DataSize = wcslen ( a_Value ) + 1 ;
  2131. LONG t_RegResult = OS::RegSetValueEx (
  2132. a_Key ,
  2133. a_Name ,
  2134. 0 ,
  2135. t_ValueType ,
  2136. LPBYTE ( a_Value ) ,
  2137. t_DataSize * sizeof ( wchar_t )
  2138. ) ;
  2139. if ( t_RegResult != ERROR_SUCCESS )
  2140. {
  2141. t_Result = WBEM_E_INVALID_PROVIDER_REGISTRATION ;
  2142. }
  2143. return t_Result ;
  2144. }
  2145. /******************************************************************************
  2146. *
  2147. * Name:
  2148. *
  2149. *
  2150. * Description:
  2151. *
  2152. *
  2153. *****************************************************************************/
  2154. HRESULT ProviderSubSystem_Common_Globals :: Save_ByteArray ( HKEY a_Key , LPCWSTR a_Name , BYTE *a_Value , DWORD a_ValueLength )
  2155. {
  2156. HRESULT t_Result = S_OK ;
  2157. DWORD t_ValueType = REG_BINARY ;
  2158. LONG t_RegResult = OS::RegSetValueEx (
  2159. a_Key ,
  2160. a_Name ,
  2161. 0 ,
  2162. t_ValueType ,
  2163. LPBYTE ( a_Value ) ,
  2164. a_ValueLength
  2165. ) ;
  2166. if ( t_RegResult != ERROR_SUCCESS )
  2167. {
  2168. t_Result = WBEM_E_INVALID_PROVIDER_REGISTRATION ;
  2169. }
  2170. return t_Result ;
  2171. }
  2172. /******************************************************************************
  2173. *
  2174. * Name:
  2175. *
  2176. *
  2177. * Description:
  2178. *
  2179. *
  2180. *****************************************************************************/
  2181. HRESULT ProviderSubSystem_Common_Globals :: UnMarshalRegistration (
  2182. IUnknown **a_Unknown ,
  2183. BYTE *a_MarshaledProxy ,
  2184. DWORD a_MarshaledProxyLength
  2185. )
  2186. {
  2187. HRESULT t_Result = S_OK ;
  2188. IStream *t_Stream = NULL ;
  2189. HGLOBAL t_Global = GlobalAlloc (
  2190. GHND ,
  2191. a_MarshaledProxyLength
  2192. ) ;
  2193. if ( t_Global )
  2194. {
  2195. void *t_Memory = GlobalLock ( t_Global ) ;
  2196. CopyMemory ( t_Memory , a_MarshaledProxy , a_MarshaledProxyLength ) ;
  2197. GlobalUnlock ( t_Global ) ;
  2198. t_Result = CreateStreamOnHGlobal (
  2199. t_Global ,
  2200. TRUE ,
  2201. & t_Stream
  2202. ) ;
  2203. if ( SUCCEEDED ( t_Result ) )
  2204. {
  2205. t_Result = CoUnmarshalInterface (
  2206. t_Stream ,
  2207. IID_IUnknown ,
  2208. ( void ** ) a_Unknown
  2209. ) ;
  2210. t_Stream->Release () ;
  2211. }
  2212. }
  2213. else
  2214. {
  2215. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2216. }
  2217. return t_Result ;
  2218. }
  2219. /******************************************************************************
  2220. *
  2221. * Name:
  2222. *
  2223. *
  2224. * Description:
  2225. *
  2226. *
  2227. *****************************************************************************/
  2228. HRESULT ProviderSubSystem_Common_Globals :: ReleaseRegistration (
  2229. BYTE *a_MarshaledProxy ,
  2230. DWORD a_MarshaledProxyLength
  2231. )
  2232. {
  2233. HRESULT t_Result = S_OK ;
  2234. IStream *t_Stream = NULL ;
  2235. HGLOBAL t_Global = GlobalAlloc (
  2236. GHND ,
  2237. a_MarshaledProxyLength
  2238. ) ;
  2239. if ( t_Global )
  2240. {
  2241. void *t_Memory = GlobalLock ( t_Global ) ;
  2242. CopyMemory ( t_Memory , a_MarshaledProxy , a_MarshaledProxyLength ) ;
  2243. GlobalUnlock ( t_Global ) ;
  2244. t_Result = CreateStreamOnHGlobal (
  2245. t_Global ,
  2246. TRUE ,
  2247. & t_Stream
  2248. ) ;
  2249. if ( SUCCEEDED ( t_Result ) )
  2250. {
  2251. t_Result = CoReleaseMarshalData (
  2252. t_Stream
  2253. ) ;
  2254. t_Stream->Release () ;
  2255. }
  2256. }
  2257. else
  2258. {
  2259. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2260. }
  2261. return t_Result ;
  2262. }
  2263. /******************************************************************************
  2264. *
  2265. * Name:
  2266. *
  2267. *
  2268. * Description:
  2269. *
  2270. *
  2271. *****************************************************************************/
  2272. HRESULT ProviderSubSystem_Common_Globals :: MarshalRegistration (
  2273. IUnknown *a_Unknown ,
  2274. BYTE *&a_MarshaledProxy ,
  2275. DWORD &a_MarshaledProxyLength
  2276. )
  2277. {
  2278. HRESULT t_Result = S_OK ;
  2279. t_Result = CoGetMarshalSizeMax (
  2280. & a_MarshaledProxyLength ,
  2281. IID_IUnknown ,
  2282. a_Unknown ,
  2283. MSHCTX_LOCAL ,
  2284. NULL ,
  2285. MSHLFLAGS_TABLEWEAK
  2286. ) ;
  2287. if ( SUCCEEDED ( t_Result ) )
  2288. {
  2289. IStream *t_Stream = NULL ;
  2290. HGLOBAL t_Global = GlobalAlloc (
  2291. GHND ,
  2292. a_MarshaledProxyLength
  2293. ) ;
  2294. if ( t_Global )
  2295. {
  2296. t_Result = CreateStreamOnHGlobal (
  2297. t_Global ,
  2298. TRUE ,
  2299. & t_Stream
  2300. ) ;
  2301. if ( SUCCEEDED ( t_Result ) )
  2302. {
  2303. t_Result = CoMarshalInterface (
  2304. t_Stream ,
  2305. IID_IUnknown ,
  2306. a_Unknown ,
  2307. MSHCTX_LOCAL ,
  2308. NULL ,
  2309. MSHLFLAGS_TABLESTRONG
  2310. ) ;
  2311. if ( SUCCEEDED ( t_Result ) )
  2312. {
  2313. a_MarshaledProxy = new BYTE [ a_MarshaledProxyLength ] ;
  2314. if ( a_MarshaledProxy )
  2315. {
  2316. void *t_Memory = GlobalLock ( t_Global ) ;
  2317. CopyMemory ( a_MarshaledProxy , t_Memory , a_MarshaledProxyLength ) ;
  2318. GlobalUnlock ( t_Global ) ;
  2319. }
  2320. else
  2321. {
  2322. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2323. }
  2324. }
  2325. t_Stream->Release();
  2326. }
  2327. }
  2328. else
  2329. {
  2330. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2331. }
  2332. }
  2333. return t_Result ;
  2334. }
  2335. /******************************************************************************
  2336. *
  2337. * Name:
  2338. *
  2339. *
  2340. * Description:
  2341. *
  2342. *
  2343. *****************************************************************************/
  2344. HRESULT ProviderSubSystem_Common_Globals :: IsDependantCall ( IWbemContext *a_ParentContext , IWbemContext *a_ChildContext , BOOL &a_DependantCall )
  2345. {
  2346. HRESULT t_Result = WBEM_E_UNEXPECTED ;
  2347. if ( a_ParentContext )
  2348. {
  2349. if ( a_ChildContext )
  2350. {
  2351. IWbemCausalityAccess *t_ParentCausality = NULL ;
  2352. t_Result = a_ParentContext->QueryInterface ( IID_IWbemCausalityAccess , ( void ** ) & t_ParentCausality ) ;
  2353. if ( SUCCEEDED ( t_Result ) )
  2354. {
  2355. IWbemCausalityAccess *t_ChildCausality = NULL ;
  2356. t_Result = a_ChildContext->QueryInterface ( IID_IWbemCausalityAccess , ( void ** ) & t_ChildCausality ) ;
  2357. if ( SUCCEEDED ( t_Result ) )
  2358. {
  2359. REQUESTID t_ParentId ;
  2360. t_Result = t_ParentCausality->GetRequestId ( & t_ParentId ) ;
  2361. if ( SUCCEEDED ( t_Result ) )
  2362. {
  2363. t_Result = t_ChildCausality->IsChildOf ( t_ParentId ) ;
  2364. if ( SUCCEEDED ( t_Result ) )
  2365. {
  2366. a_DependantCall = ( t_Result == S_FALSE ) ? FALSE : TRUE ;
  2367. }
  2368. }
  2369. t_ChildCausality->Release () ;
  2370. }
  2371. else
  2372. {
  2373. t_Result = WBEM_E_UNEXPECTED ;
  2374. }
  2375. t_ParentCausality->Release () ;
  2376. }
  2377. }
  2378. }
  2379. return S_OK ;
  2380. }
  2381. /******************************************************************************
  2382. *
  2383. * Name:
  2384. *
  2385. *
  2386. * Description:
  2387. *
  2388. *
  2389. *****************************************************************************/
  2390. HRESULT ProviderSubSystem_Common_Globals :: Check_SecurityDescriptor_CallIdentity (
  2391. SECURITY_DESCRIPTOR *a_SecurityDescriptor ,
  2392. DWORD a_Access ,
  2393. GENERIC_MAPPING *a_Mapping
  2394. )
  2395. {
  2396. HRESULT t_Result = S_OK ;
  2397. if (!OS::secureOS_)
  2398. return S_OK;
  2399. SECURITY_DESCRIPTOR *t_SecurityDescriptor = a_SecurityDescriptor ? a_SecurityDescriptor : GetMethodSecurityDescriptor ();
  2400. if (t_SecurityDescriptor == NULL) return S_OK;
  2401. HANDLE t_Token = NULL ;
  2402. BOOL t_Status = OpenThreadToken (
  2403. GetCurrentThread () ,
  2404. TOKEN_QUERY ,
  2405. TRUE ,
  2406. & t_Token
  2407. ) ;
  2408. DWORD t_LastError = GetLastError () ;
  2409. if ( ! t_Status && ( t_LastError == ERROR_NO_IMPERSONATION_TOKEN || t_LastError == ERROR_NO_TOKEN ) )
  2410. {
  2411. HANDLE t_ProcessToken = NULL ;
  2412. t_Status = OpenProcessToken (
  2413. GetCurrentProcess () ,
  2414. TOKEN_QUERY | TOKEN_DUPLICATE ,
  2415. & t_ProcessToken
  2416. ) ;
  2417. if ( t_Status )
  2418. {
  2419. t_Status = ImpersonateLoggedOnUser ( t_ProcessToken ) ;
  2420. if ( t_Status )
  2421. {
  2422. BOOL t_Status = OpenThreadToken (
  2423. GetCurrentThread () ,
  2424. TOKEN_QUERY ,
  2425. TRUE ,
  2426. & t_Token
  2427. ) ;
  2428. if ( ! t_Status )
  2429. {
  2430. DWORD t_LastError = GetLastError () ;
  2431. t_Result = WBEM_E_ACCESS_DENIED ;
  2432. }
  2433. RevertToSelf () ;
  2434. }
  2435. else
  2436. {
  2437. DWORD t_LastError = GetLastError () ;
  2438. t_Result = WBEM_E_ACCESS_DENIED ;
  2439. }
  2440. CloseHandle ( t_ProcessToken ) ;
  2441. }
  2442. else
  2443. {
  2444. t_Result = WBEM_E_ACCESS_DENIED ;
  2445. }
  2446. }
  2447. else
  2448. {
  2449. if ( ! t_Status )
  2450. {
  2451. t_Result = WBEM_E_ACCESS_DENIED ;
  2452. }
  2453. }
  2454. if ( SUCCEEDED ( t_Result ) )
  2455. {
  2456. DWORD t_Access = 0 ;
  2457. BOOL t_AccessStatus = FALSE ;
  2458. PRIVILEGE_SET *t_PrivilegeSet = NULL ;
  2459. DWORD t_PrivilegeSetSize = 0 ;
  2460. MapGenericMask (
  2461. & a_Access ,
  2462. a_Mapping
  2463. ) ;
  2464. t_Status = AccessCheck (
  2465. t_SecurityDescriptor ,
  2466. t_Token,
  2467. a_Access ,
  2468. a_Mapping ,
  2469. NULL ,
  2470. & t_PrivilegeSetSize ,
  2471. & t_Access ,
  2472. & t_AccessStatus
  2473. ) ;
  2474. if ( t_Status && t_AccessStatus )
  2475. {
  2476. }
  2477. else
  2478. {
  2479. DWORD t_LastError = GetLastError () ;
  2480. if ( t_LastError == ERROR_INSUFFICIENT_BUFFER )
  2481. {
  2482. t_PrivilegeSet = ( PRIVILEGE_SET * ) new BYTE [ t_PrivilegeSetSize ] ;
  2483. if ( t_PrivilegeSet )
  2484. {
  2485. t_Status = AccessCheck (
  2486. t_SecurityDescriptor ,
  2487. t_Token,
  2488. a_Access ,
  2489. a_Mapping ,
  2490. t_PrivilegeSet ,
  2491. & t_PrivilegeSetSize ,
  2492. & t_Access ,
  2493. & t_AccessStatus
  2494. ) ;
  2495. if ( t_Status && t_AccessStatus )
  2496. {
  2497. }
  2498. else
  2499. {
  2500. t_Result = WBEM_E_ACCESS_DENIED ;
  2501. }
  2502. delete [] ( BYTE * ) t_PrivilegeSet ;
  2503. }
  2504. else
  2505. {
  2506. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2507. }
  2508. }
  2509. else
  2510. {
  2511. t_Result = WBEM_E_ACCESS_DENIED;
  2512. }
  2513. }
  2514. CloseHandle ( t_Token ) ;
  2515. }
  2516. return t_Result ;
  2517. }
  2518. /******************************************************************************
  2519. *
  2520. * Name:
  2521. *
  2522. *
  2523. * Description:
  2524. *
  2525. *
  2526. *****************************************************************************/
  2527. HRESULT ProviderSubSystem_Common_Globals :: AdjustSecurityDescriptorWithSid (
  2528. SID *a_OwnerSid ,
  2529. SID *a_GroupSid ,
  2530. DWORD a_Access ,
  2531. SECURITY_DESCRIPTOR *&a_SecurityDescriptor ,
  2532. SECURITY_DESCRIPTOR *&a_AlteredSecurityDescriptor
  2533. )
  2534. {
  2535. HRESULT t_Result = S_OK ;
  2536. SECURITY_DESCRIPTOR t_CreatedSecurityDescriptor ;
  2537. SECURITY_DESCRIPTOR *t_SecurityDescriptor = NULL ;
  2538. PACL t_Dacl = NULL ;
  2539. PACL t_Sacl = NULL ;
  2540. PSID t_Owner = NULL ;
  2541. PSID t_PrimaryGroup = NULL ;
  2542. SECURITY_DESCRIPTOR *t_AlteredSecurityDescriptor = NULL ;
  2543. if ( SUCCEEDED ( t_Result ) )
  2544. {
  2545. if ( a_SecurityDescriptor )
  2546. {
  2547. DWORD t_AlteredSecurityDescriptorSize = sizeof ( SECURITY_DESCRIPTOR ) ;
  2548. DWORD t_DaclSize = 0 ;
  2549. DWORD t_SaclSize = 0 ;
  2550. DWORD t_OwnerSize = 0 ;
  2551. DWORD t_PrimaryGroupSize = 0 ;
  2552. BOOL t_Status = MakeAbsoluteSD (
  2553. a_SecurityDescriptor ,
  2554. t_AlteredSecurityDescriptor ,
  2555. & t_AlteredSecurityDescriptorSize ,
  2556. t_Dacl,
  2557. & t_DaclSize,
  2558. t_Sacl,
  2559. & t_SaclSize,
  2560. t_Owner,
  2561. & t_OwnerSize,
  2562. t_PrimaryGroup,
  2563. & t_PrimaryGroupSize
  2564. ) ;
  2565. if ( ( t_Status == FALSE ) && GetLastError () == ERROR_INSUFFICIENT_BUFFER )
  2566. {
  2567. DWORD t_SidLength = GetLengthSid ( a_OwnerSid ) ;
  2568. DWORD t_ExtraSize = sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ;
  2569. t_Dacl = ( PACL ) new BYTE [ t_DaclSize + t_ExtraSize ] ;
  2570. t_Sacl = ( PACL ) new BYTE [ t_SaclSize ] ;
  2571. t_Owner = ( PSID ) new BYTE [ t_OwnerSize ] ;
  2572. t_PrimaryGroup = ( PSID ) new BYTE [ t_PrimaryGroupSize ] ;
  2573. t_AlteredSecurityDescriptor = ( SECURITY_DESCRIPTOR * ) new BYTE [ t_AlteredSecurityDescriptorSize ] ;
  2574. if ( t_AlteredSecurityDescriptor && t_Dacl && t_Sacl && t_Owner && t_PrimaryGroup )
  2575. {
  2576. BOOL t_Status = InitializeSecurityDescriptor ( t_AlteredSecurityDescriptor , SECURITY_DESCRIPTOR_REVISION ) ;
  2577. if ( t_Status )
  2578. {
  2579. t_Status = MakeAbsoluteSD (
  2580. a_SecurityDescriptor ,
  2581. t_AlteredSecurityDescriptor ,
  2582. & t_AlteredSecurityDescriptorSize ,
  2583. t_Dacl,
  2584. & t_DaclSize,
  2585. t_Sacl,
  2586. & t_SaclSize,
  2587. t_Owner,
  2588. & t_OwnerSize,
  2589. t_PrimaryGroup,
  2590. & t_PrimaryGroupSize
  2591. ) ;
  2592. if ( t_Status )
  2593. {
  2594. t_SecurityDescriptor = t_AlteredSecurityDescriptor ;
  2595. if ( t_OwnerSize == 0 )
  2596. {
  2597. t_Status = SetSecurityDescriptorOwner (
  2598. t_SecurityDescriptor ,
  2599. a_OwnerSid ,
  2600. FALSE
  2601. ) ;
  2602. if ( ! t_Status )
  2603. {
  2604. t_Result = WBEM_E_CRITICAL_ERROR ;
  2605. }
  2606. }
  2607. if ( SUCCEEDED ( t_Result ) )
  2608. {
  2609. if ( t_PrimaryGroupSize == 0 )
  2610. {
  2611. t_Status = SetSecurityDescriptorGroup (
  2612. t_SecurityDescriptor ,
  2613. a_GroupSid ,
  2614. FALSE
  2615. ) ;
  2616. if ( ! t_Status )
  2617. {
  2618. t_Result = WBEM_E_CRITICAL_ERROR ;
  2619. }
  2620. }
  2621. }
  2622. }
  2623. else
  2624. {
  2625. t_Result = WBEM_E_CRITICAL_ERROR ;
  2626. }
  2627. }
  2628. else
  2629. {
  2630. t_Result = WBEM_E_CRITICAL_ERROR ;
  2631. }
  2632. }
  2633. else
  2634. {
  2635. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2636. }
  2637. }
  2638. else
  2639. {
  2640. t_Result = WBEM_E_CRITICAL_ERROR ;
  2641. }
  2642. }
  2643. else
  2644. {
  2645. BOOL t_Status = InitializeSecurityDescriptor ( & t_CreatedSecurityDescriptor , SECURITY_DESCRIPTOR_REVISION ) ;
  2646. if ( t_Status )
  2647. {
  2648. t_Status = SetSecurityDescriptorOwner (
  2649. & t_CreatedSecurityDescriptor ,
  2650. a_OwnerSid ,
  2651. FALSE
  2652. ) ;
  2653. if ( ! t_Status )
  2654. {
  2655. t_Result = WBEM_E_CRITICAL_ERROR ;
  2656. }
  2657. if ( SUCCEEDED ( t_Result ) )
  2658. {
  2659. t_Status = SetSecurityDescriptorGroup (
  2660. & t_CreatedSecurityDescriptor ,
  2661. a_GroupSid ,
  2662. FALSE
  2663. ) ;
  2664. if ( ! t_Status )
  2665. {
  2666. t_Result = WBEM_E_CRITICAL_ERROR ;
  2667. }
  2668. }
  2669. }
  2670. else
  2671. {
  2672. t_Result = WBEM_E_CRITICAL_ERROR ;
  2673. }
  2674. t_SecurityDescriptor = & t_CreatedSecurityDescriptor ;
  2675. }
  2676. }
  2677. SID_IDENTIFIER_AUTHORITY t_NtAuthoritySid = SECURITY_NT_AUTHORITY ;
  2678. DWORD t_SidLength = GetLengthSid ( a_OwnerSid ) ;
  2679. PACL t_ExtraDacl = NULL ;
  2680. ACCESS_ALLOWED_ACE *t_Ace = NULL ;
  2681. DWORD t_AceSize = 0 ;
  2682. if ( SUCCEEDED ( t_Result ) )
  2683. {
  2684. t_AceSize = sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ;
  2685. t_Ace = (ACCESS_ALLOWED_ACE*) new BYTE [ t_AceSize ] ;
  2686. if ( t_Ace )
  2687. {
  2688. CopySid ( t_SidLength, (PSID) & t_Ace->SidStart, a_OwnerSid ) ;
  2689. t_Ace->Mask = a_Access ;
  2690. t_Ace->Header.AceType = 0 ;
  2691. t_Ace->Header.AceFlags = 0 ;
  2692. t_Ace->Header.AceSize = t_AceSize ;
  2693. }
  2694. else
  2695. {
  2696. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2697. }
  2698. }
  2699. if ( SUCCEEDED ( t_Result ) )
  2700. {
  2701. ACL_SIZE_INFORMATION t_Size ;
  2702. if ( t_Dacl )
  2703. {
  2704. BOOL t_Status = GetAclInformation (
  2705. t_Dacl ,
  2706. & t_Size ,
  2707. sizeof ( t_Size ) ,
  2708. AclSizeInformation
  2709. );
  2710. if ( t_Status )
  2711. {
  2712. DWORD t_ExtraSize = t_Size.AclBytesInUse + t_Size.AclBytesFree + ( sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ) ;
  2713. t_ExtraSize = t_ExtraSize + s_LocalService_ACESize + s_NetworkService_ACESize + s_System_ACESize + s_LocalAdmins_ACESize ;
  2714. t_ExtraDacl = ( PACL ) new BYTE [ t_ExtraSize ] ;
  2715. if ( t_ExtraDacl )
  2716. {
  2717. CopyMemory ( t_ExtraDacl , t_Dacl , t_Size.AclBytesInUse + t_Size.AclBytesFree ) ;
  2718. t_ExtraDacl->AclSize = t_ExtraSize ;
  2719. }
  2720. else
  2721. {
  2722. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2723. }
  2724. }
  2725. else
  2726. {
  2727. t_Result = WBEM_E_CRITICAL_ERROR ;
  2728. }
  2729. }
  2730. else
  2731. {
  2732. DWORD t_SidLength = GetLengthSid ( a_OwnerSid ) ;
  2733. DWORD t_ExtraSize = sizeof ( ACL ) + ( sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ) ;
  2734. t_ExtraSize = t_ExtraSize + s_LocalService_ACESize + s_NetworkService_ACESize + s_System_ACESize + s_LocalAdmins_ACESize ;
  2735. t_ExtraDacl = ( PACL ) new BYTE [ t_ExtraSize ] ;
  2736. if ( t_ExtraDacl )
  2737. {
  2738. BOOL t_Status = InitializeAcl (
  2739. t_ExtraDacl ,
  2740. t_ExtraSize ,
  2741. ACL_REVISION
  2742. ) ;
  2743. if ( t_Status )
  2744. {
  2745. BOOL t_Status = GetAclInformation (
  2746. t_ExtraDacl ,
  2747. & t_Size ,
  2748. sizeof ( t_Size ) ,
  2749. AclSizeInformation
  2750. );
  2751. if ( ! t_Status )
  2752. {
  2753. t_Result = WBEM_E_CRITICAL_ERROR ;
  2754. }
  2755. }
  2756. else
  2757. {
  2758. t_Result = WBEM_E_CRITICAL_ERROR ;
  2759. }
  2760. }
  2761. else
  2762. {
  2763. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2764. }
  2765. }
  2766. DWORD t_AceIndex = 0 ;
  2767. if ( SUCCEEDED ( t_Result ) )
  2768. {
  2769. BOOL t_Status = :: AddAce ( t_ExtraDacl , ACL_REVISION, t_Size.AceCount , t_Ace , t_AceSize ) ;
  2770. if ( t_Status )
  2771. {
  2772. t_AceIndex ++ ;
  2773. }
  2774. else
  2775. {
  2776. t_Result = WBEM_E_CRITICAL_ERROR ;
  2777. }
  2778. }
  2779. if ( SUCCEEDED ( t_Result ) )
  2780. {
  2781. if ( s_System_ACESize && :: AddAce ( t_ExtraDacl , ACL_REVISION , t_AceIndex , s_Provider_System_ACE , s_System_ACESize ) )
  2782. {
  2783. t_AceIndex ++ ;
  2784. }
  2785. else
  2786. {
  2787. t_Result = WBEM_E_CRITICAL_ERROR ;
  2788. }
  2789. }
  2790. if ( SUCCEEDED ( t_Result ) )
  2791. {
  2792. if ( s_LocalService_ACESize && :: AddAce ( t_ExtraDacl , ACL_REVISION , t_AceIndex , s_Provider_LocalService_ACE , s_LocalService_ACESize ) )
  2793. {
  2794. t_AceIndex ++ ;
  2795. }
  2796. else
  2797. {
  2798. t_Result = WBEM_E_CRITICAL_ERROR ;
  2799. }
  2800. }
  2801. if ( SUCCEEDED ( t_Result ) )
  2802. {
  2803. if ( s_NetworkService_ACESize && :: AddAce ( t_ExtraDacl , ACL_REVISION , t_AceIndex , s_Provider_NetworkService_ACE , s_NetworkService_ACESize ) )
  2804. {
  2805. t_AceIndex ++ ;
  2806. }
  2807. else
  2808. {
  2809. t_Result = WBEM_E_CRITICAL_ERROR ;
  2810. }
  2811. }
  2812. if ( SUCCEEDED ( t_Result ) )
  2813. {
  2814. if ( s_LocalAdmins_ACESize && :: AddAce ( t_ExtraDacl , ACL_REVISION , t_AceIndex , s_Provider_LocalAdmins_ACE , s_LocalAdmins_ACESize ) )
  2815. {
  2816. t_AceIndex ++ ;
  2817. }
  2818. else
  2819. {
  2820. t_Result = WBEM_E_CRITICAL_ERROR ;
  2821. }
  2822. }
  2823. if ( SUCCEEDED ( t_Result ) )
  2824. {
  2825. BOOL t_Status = SetSecurityDescriptorDacl (
  2826. t_SecurityDescriptor ,
  2827. TRUE ,
  2828. t_ExtraDacl ,
  2829. FALSE
  2830. ) ;
  2831. if ( t_Status )
  2832. {
  2833. DWORD t_FinalLength = 0 ;
  2834. t_Status = MakeSelfRelativeSD (
  2835. t_SecurityDescriptor ,
  2836. a_AlteredSecurityDescriptor ,
  2837. & t_FinalLength
  2838. ) ;
  2839. if ( t_Status == FALSE && GetLastError () == ERROR_INSUFFICIENT_BUFFER )
  2840. {
  2841. a_AlteredSecurityDescriptor = ( SECURITY_DESCRIPTOR * ) new BYTE [ t_FinalLength ] ;
  2842. if ( a_AlteredSecurityDescriptor )
  2843. {
  2844. t_Status = MakeSelfRelativeSD (
  2845. t_SecurityDescriptor ,
  2846. a_AlteredSecurityDescriptor ,
  2847. & t_FinalLength
  2848. ) ;
  2849. if ( t_Status == FALSE )
  2850. {
  2851. t_Result = WBEM_E_CRITICAL_ERROR ;
  2852. }
  2853. }
  2854. else
  2855. {
  2856. t_Result = WBEM_E_OUT_OF_MEMORY ;
  2857. }
  2858. }
  2859. }
  2860. else
  2861. {
  2862. t_Result = WBEM_E_CRITICAL_ERROR ;
  2863. }
  2864. }
  2865. delete [] t_Ace ;
  2866. delete [] t_ExtraDacl ;
  2867. }
  2868. delete [] ( BYTE * ) t_Dacl ;
  2869. delete [] ( BYTE * ) t_Sacl ;
  2870. delete [] ( BYTE * ) t_Owner ;
  2871. delete [] ( BYTE * ) t_PrimaryGroup ;
  2872. delete [] ( BYTE * ) t_AlteredSecurityDescriptor ;
  2873. return t_Result ;
  2874. }
  2875. /******************************************************************************
  2876. *
  2877. * Name:
  2878. *
  2879. *
  2880. * Description:
  2881. *
  2882. *
  2883. *****************************************************************************/
  2884. HRESULT ProviderSubSystem_Common_Globals :: CreateSystemAces ()
  2885. {
  2886. HRESULT t_Result = S_OK ;
  2887. SID_IDENTIFIER_AUTHORITY t_NtAuthoritySid = SECURITY_NT_AUTHORITY ;
  2888. PSID t_System_Sid = NULL ;
  2889. PSID t_LocalService_Sid = NULL ;
  2890. PSID t_NetworkService_Sid = NULL ;
  2891. PSID t_LocalAdmins_Sid = NULL ;
  2892. BOOL t_BoolResult = AllocateAndInitializeSid (
  2893. & t_NtAuthoritySid ,
  2894. 1 ,
  2895. SECURITY_LOCAL_SYSTEM_RID,
  2896. 0,
  2897. 0,
  2898. 0,
  2899. 0,
  2900. 0,
  2901. 0,
  2902. 0,
  2903. & t_System_Sid
  2904. );
  2905. if ( t_BoolResult )
  2906. {
  2907. DWORD t_SidLength = ::GetLengthSid ( t_System_Sid );
  2908. s_System_ACESize = sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ;
  2909. s_Provider_System_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ s_System_ACESize ] ;
  2910. if ( s_Provider_System_ACE )
  2911. {
  2912. CopySid ( t_SidLength, (PSID) & s_Provider_System_ACE->SidStart, t_System_Sid ) ;
  2913. s_Provider_System_ACE->Mask = MASK_PROVIDER_BINDING_BIND ;
  2914. s_Provider_System_ACE->Header.AceType = 0 ;
  2915. s_Provider_System_ACE->Header.AceFlags = 3 ;
  2916. s_Provider_System_ACE->Header.AceSize = s_System_ACESize ;
  2917. s_Token_All_Access_System_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ s_System_ACESize ] ;
  2918. if ( s_Token_All_Access_System_ACE )
  2919. {
  2920. CopySid ( t_SidLength, (PSID) & s_Token_All_Access_System_ACE->SidStart, t_System_Sid ) ;
  2921. s_Token_All_Access_System_ACE->Mask = TOKEN_ALL_ACCESS ;
  2922. s_Token_All_Access_System_ACE->Header.AceType = 0 ;
  2923. s_Token_All_Access_System_ACE->Header.AceFlags = 3 ;
  2924. s_Token_All_Access_System_ACE->Header.AceSize = s_System_ACESize ;
  2925. }
  2926. else
  2927. {
  2928. t_Result = E_OUTOFMEMORY ;
  2929. }
  2930. }
  2931. else
  2932. {
  2933. t_Result = E_OUTOFMEMORY ;
  2934. }
  2935. }
  2936. else
  2937. {
  2938. DWORD t_LastError = ::GetLastError();
  2939. t_Result = E_OUTOFMEMORY ;
  2940. }
  2941. if ( SUCCEEDED ( t_Result ) )
  2942. {
  2943. t_BoolResult = AllocateAndInitializeSid (
  2944. & t_NtAuthoritySid ,
  2945. 1 ,
  2946. SECURITY_LOCAL_SERVICE_RID,
  2947. 0,
  2948. 0,
  2949. 0,
  2950. 0,
  2951. 0,
  2952. 0,
  2953. 0,
  2954. & t_LocalService_Sid
  2955. );
  2956. if ( t_BoolResult )
  2957. {
  2958. DWORD t_SidLength = ::GetLengthSid ( t_LocalService_Sid );
  2959. s_LocalService_ACESize = sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ;
  2960. s_Provider_LocalService_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ s_LocalService_ACESize ] ;
  2961. if ( s_Provider_LocalService_ACE )
  2962. {
  2963. CopySid ( t_SidLength, (PSID) & s_Provider_LocalService_ACE->SidStart, t_LocalService_Sid ) ;
  2964. s_Provider_LocalService_ACE->Mask = MASK_PROVIDER_BINDING_BIND ;
  2965. s_Provider_LocalService_ACE->Header.AceType = 0 ;
  2966. s_Provider_LocalService_ACE->Header.AceFlags = 3 ;
  2967. s_Provider_LocalService_ACE->Header.AceSize = s_LocalService_ACESize ;
  2968. s_Token_All_Access_LocalService_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ s_LocalService_ACESize ] ;
  2969. if ( s_Token_All_Access_LocalService_ACE )
  2970. {
  2971. CopySid ( t_SidLength, (PSID) & s_Token_All_Access_LocalService_ACE->SidStart, t_LocalService_Sid ) ;
  2972. s_Token_All_Access_LocalService_ACE->Mask = TOKEN_ALL_ACCESS ;
  2973. s_Token_All_Access_LocalService_ACE->Header.AceType = 0 ;
  2974. s_Token_All_Access_LocalService_ACE->Header.AceFlags = 3 ;
  2975. s_Token_All_Access_LocalService_ACE->Header.AceSize = s_LocalService_ACESize ;
  2976. }
  2977. else
  2978. {
  2979. t_Result = E_OUTOFMEMORY ;
  2980. }
  2981. }
  2982. else
  2983. {
  2984. t_Result = E_OUTOFMEMORY ;
  2985. }
  2986. }
  2987. else
  2988. {
  2989. DWORD t_LastError = ::GetLastError();
  2990. t_Result = E_OUTOFMEMORY ;
  2991. }
  2992. }
  2993. if ( SUCCEEDED ( t_Result ) )
  2994. {
  2995. t_BoolResult = AllocateAndInitializeSid (
  2996. & t_NtAuthoritySid ,
  2997. 1 ,
  2998. SECURITY_NETWORK_SERVICE_RID,
  2999. 0,
  3000. 0,
  3001. 0,
  3002. 0,
  3003. 0,
  3004. 0,
  3005. 0,
  3006. & t_NetworkService_Sid
  3007. );
  3008. if ( t_BoolResult )
  3009. {
  3010. DWORD t_SidLength = ::GetLengthSid ( t_NetworkService_Sid );
  3011. s_NetworkService_ACESize = sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ;
  3012. s_Provider_NetworkService_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ s_NetworkService_ACESize ] ;
  3013. if ( s_Provider_NetworkService_ACE )
  3014. {
  3015. CopySid ( t_SidLength, (PSID) & s_Provider_NetworkService_ACE->SidStart, t_NetworkService_Sid ) ;
  3016. s_Provider_NetworkService_ACE->Mask = MASK_PROVIDER_BINDING_BIND ;
  3017. s_Provider_NetworkService_ACE->Header.AceType = 0 ;
  3018. s_Provider_NetworkService_ACE->Header.AceFlags = 3 ;
  3019. s_Provider_NetworkService_ACE->Header.AceSize = s_NetworkService_ACESize ;
  3020. s_Token_All_Access_NetworkService_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ s_NetworkService_ACESize ] ;
  3021. if ( s_Token_All_Access_NetworkService_ACE )
  3022. {
  3023. CopySid ( t_SidLength, (PSID) & s_Token_All_Access_NetworkService_ACE->SidStart, t_NetworkService_Sid ) ;
  3024. s_Token_All_Access_NetworkService_ACE->Mask = TOKEN_ALL_ACCESS ;
  3025. s_Token_All_Access_NetworkService_ACE->Header.AceType = 0 ;
  3026. s_Token_All_Access_NetworkService_ACE->Header.AceFlags = 3 ;
  3027. s_Token_All_Access_NetworkService_ACE->Header.AceSize = s_NetworkService_ACESize ;
  3028. }
  3029. else
  3030. {
  3031. t_Result = E_OUTOFMEMORY ;
  3032. }
  3033. }
  3034. else
  3035. {
  3036. t_Result = E_OUTOFMEMORY ;
  3037. }
  3038. }
  3039. else
  3040. {
  3041. DWORD t_LastError = ::GetLastError();
  3042. t_Result = E_OUTOFMEMORY ;
  3043. }
  3044. }
  3045. if ( SUCCEEDED ( t_Result ) )
  3046. {
  3047. t_BoolResult = AllocateAndInitializeSid (
  3048. & t_NtAuthoritySid ,
  3049. 2 ,
  3050. SECURITY_BUILTIN_DOMAIN_RID ,
  3051. DOMAIN_ALIAS_RID_ADMINS ,
  3052. 0,
  3053. 0,
  3054. 0,
  3055. 0,
  3056. 0,
  3057. 0,
  3058. & t_LocalAdmins_Sid
  3059. );
  3060. if ( t_BoolResult )
  3061. {
  3062. DWORD t_SidLength = ::GetLengthSid ( t_LocalAdmins_Sid );
  3063. s_LocalAdmins_ACESize = sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ;
  3064. s_Provider_LocalAdmins_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ s_LocalAdmins_ACESize ] ;
  3065. if ( s_Provider_LocalAdmins_ACE )
  3066. {
  3067. CopySid ( t_SidLength, (PSID) & s_Provider_LocalAdmins_ACE->SidStart, t_LocalAdmins_Sid ) ;
  3068. s_Provider_LocalAdmins_ACE->Mask = MASK_PROVIDER_BINDING_BIND ;
  3069. s_Provider_LocalAdmins_ACE->Header.AceType = 0 ;
  3070. s_Provider_LocalAdmins_ACE->Header.AceFlags = 3 ;
  3071. s_Provider_LocalAdmins_ACE->Header.AceSize = s_LocalAdmins_ACESize ;
  3072. s_Token_All_Access_LocalAdmins_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ s_LocalAdmins_ACESize ] ;
  3073. if ( s_Token_All_Access_LocalAdmins_ACE )
  3074. {
  3075. CopySid ( t_SidLength, (PSID) & s_Token_All_Access_LocalAdmins_ACE->SidStart, t_LocalAdmins_Sid ) ;
  3076. s_Token_All_Access_LocalAdmins_ACE->Mask = TOKEN_ALL_ACCESS ;
  3077. s_Token_All_Access_LocalAdmins_ACE->Header.AceType = 0 ;
  3078. s_Token_All_Access_LocalAdmins_ACE->Header.AceFlags = 3 ;
  3079. s_Token_All_Access_LocalAdmins_ACE->Header.AceSize = s_LocalAdmins_ACESize ;
  3080. }
  3081. else
  3082. {
  3083. t_Result = E_OUTOFMEMORY ;
  3084. }
  3085. }
  3086. else
  3087. {
  3088. t_Result = E_OUTOFMEMORY ;
  3089. }
  3090. }
  3091. else
  3092. {
  3093. DWORD t_LastError = ::GetLastError();
  3094. t_Result = E_OUTOFMEMORY ;
  3095. }
  3096. }
  3097. if ( t_LocalAdmins_Sid )
  3098. {
  3099. FreeSid ( t_LocalAdmins_Sid ) ;
  3100. }
  3101. if ( t_System_Sid )
  3102. {
  3103. FreeSid ( t_System_Sid ) ;
  3104. }
  3105. if ( t_LocalService_Sid )
  3106. {
  3107. FreeSid ( t_LocalService_Sid ) ;
  3108. }
  3109. if ( t_NetworkService_Sid )
  3110. {
  3111. FreeSid ( t_NetworkService_Sid ) ;
  3112. }
  3113. return t_Result ;
  3114. }
  3115. /******************************************************************************
  3116. *
  3117. * Name:
  3118. *
  3119. *
  3120. * Description:
  3121. *
  3122. *
  3123. *****************************************************************************/
  3124. HRESULT ProviderSubSystem_Common_Globals :: DeleteSystemAces ()
  3125. {
  3126. if ( s_Provider_System_ACE )
  3127. {
  3128. delete [] ( ( BYTE * ) s_Provider_System_ACE ) ;
  3129. s_Provider_System_ACE = NULL;
  3130. }
  3131. if ( s_Provider_LocalService_ACE )
  3132. {
  3133. delete [] ( ( BYTE * ) s_Provider_LocalService_ACE ) ;
  3134. s_Provider_LocalService_ACE = NULL;
  3135. }
  3136. if ( s_Provider_NetworkService_ACE )
  3137. {
  3138. delete [] ( ( BYTE * ) s_Provider_NetworkService_ACE ) ;
  3139. s_Provider_NetworkService_ACE = NULL;
  3140. }
  3141. if ( s_Provider_LocalAdmins_ACE )
  3142. {
  3143. delete [] ( ( BYTE * ) s_Provider_LocalAdmins_ACE ) ;
  3144. s_Provider_LocalAdmins_ACE = NULL;
  3145. }
  3146. if ( s_Token_All_Access_System_ACE )
  3147. {
  3148. delete [] ( ( BYTE * ) s_Token_All_Access_System_ACE ) ;
  3149. s_Token_All_Access_System_ACE = NULL;
  3150. }
  3151. if ( s_Token_All_Access_LocalService_ACE )
  3152. {
  3153. delete [] ( ( BYTE * ) s_Token_All_Access_LocalService_ACE ) ;
  3154. s_Token_All_Access_LocalService_ACE = NULL;
  3155. }
  3156. if ( s_Token_All_Access_NetworkService_ACE )
  3157. {
  3158. delete [] ( ( BYTE * ) s_Token_All_Access_NetworkService_ACE ) ;
  3159. s_Token_All_Access_NetworkService_ACE = NULL;
  3160. }
  3161. if ( s_Token_All_Access_LocalAdmins_ACE )
  3162. {
  3163. delete [] ( ( BYTE * ) s_Token_All_Access_LocalAdmins_ACE ) ;
  3164. s_Token_All_Access_LocalAdmins_ACE = NULL;
  3165. }
  3166. return S_OK ;
  3167. }
  3168. /******************************************************************************
  3169. *
  3170. * Name:
  3171. *
  3172. *
  3173. * Description:
  3174. *
  3175. *
  3176. *****************************************************************************/
  3177. HRESULT ProviderSubSystem_Common_Globals :: CheckAccess (
  3178. SECURITY_DESCRIPTOR *a_SecurityDescriptor ,
  3179. DWORD a_Access ,
  3180. GENERIC_MAPPING *a_Mapping
  3181. )
  3182. {
  3183. HRESULT t_Result = S_OK ;
  3184. if ( a_SecurityDescriptor )
  3185. {
  3186. t_Result = CoImpersonateClient () ;
  3187. if ( SUCCEEDED ( t_Result ) || t_Result == RPC_E_CALL_COMPLETE )
  3188. {
  3189. t_Result = ProviderSubSystem_Common_Globals :: Check_SecurityDescriptor_CallIdentity (
  3190. a_SecurityDescriptor ,
  3191. a_Access ,
  3192. a_Mapping
  3193. ) ;
  3194. CoRevertToSelf () ;
  3195. }
  3196. }
  3197. return t_Result ;
  3198. }
  3199. /******************************************************************************
  3200. *
  3201. * Name:
  3202. *
  3203. *
  3204. * Description:
  3205. *
  3206. *
  3207. *****************************************************************************/
  3208. HRESULT ProviderSubSystem_Common_Globals :: GetUserSid (
  3209. HANDLE a_Token ,
  3210. ULONG *a_Size ,
  3211. PSID &a_Sid
  3212. )
  3213. {
  3214. HRESULT t_Result = WBEM_E_FAILED ;
  3215. if ( a_Token )
  3216. {
  3217. if ( a_Size )
  3218. {
  3219. TOKEN_USER *t_TokenUser = NULL ;
  3220. DWORD t_ReturnLength = 0 ;
  3221. TOKEN_INFORMATION_CLASS t_TokenInformationClass = TokenUser ;
  3222. BOOL t_TokenStatus = GetTokenInformation (
  3223. a_Token ,
  3224. t_TokenInformationClass ,
  3225. t_TokenUser ,
  3226. t_ReturnLength ,
  3227. & t_ReturnLength
  3228. ) ;
  3229. if ( ! t_TokenStatus )
  3230. {
  3231. DWORD t_LastError = GetLastError () ;
  3232. switch ( t_LastError )
  3233. {
  3234. case ERROR_INSUFFICIENT_BUFFER:
  3235. {
  3236. t_TokenUser = ( TOKEN_USER * ) new BYTE [ t_ReturnLength ] ;
  3237. if ( t_TokenUser )
  3238. {
  3239. t_TokenStatus = GetTokenInformation (
  3240. a_Token ,
  3241. t_TokenInformationClass ,
  3242. t_TokenUser ,
  3243. t_ReturnLength ,
  3244. & t_ReturnLength
  3245. ) ;
  3246. if ( t_TokenStatus )
  3247. {
  3248. DWORD t_SidLength = GetLengthSid ( t_TokenUser->User.Sid ) ;
  3249. *a_Size = t_SidLength ;
  3250. a_Sid = new BYTE [ t_SidLength ] ;
  3251. if ( a_Sid )
  3252. {
  3253. CopyMemory ( a_Sid , t_TokenUser->User.Sid , t_SidLength ) ;
  3254. t_Result = S_OK ;
  3255. }
  3256. else
  3257. {
  3258. t_Result = WBEM_E_OUT_OF_MEMORY ;
  3259. }
  3260. }
  3261. delete [] t_TokenUser ;
  3262. }
  3263. else
  3264. {
  3265. t_Result = WBEM_E_OUT_OF_MEMORY ;
  3266. }
  3267. }
  3268. break ;
  3269. default:
  3270. {
  3271. }
  3272. break ;
  3273. }
  3274. }
  3275. }
  3276. else
  3277. {
  3278. t_Result = WBEM_E_INVALID_PARAMETER ;
  3279. }
  3280. }
  3281. else
  3282. {
  3283. t_Result = ( ERROR_INVALID_HANDLE | 0x80070000 ) ;
  3284. }
  3285. return t_Result ;
  3286. }
  3287. /******************************************************************************
  3288. *
  3289. * Name:
  3290. *
  3291. *
  3292. * Description:
  3293. *
  3294. *
  3295. *****************************************************************************/
  3296. HRESULT ProviderSubSystem_Common_Globals :: GetGroupSid (
  3297. HANDLE a_Token ,
  3298. ULONG *a_Size ,
  3299. PSID &a_Sid
  3300. )
  3301. {
  3302. HRESULT t_Result = WBEM_E_FAILED ;
  3303. if ( a_Token )
  3304. {
  3305. if ( a_Size )
  3306. {
  3307. TOKEN_PRIMARY_GROUP *t_TokenGroup = NULL ;
  3308. DWORD t_ReturnLength = 0 ;
  3309. TOKEN_INFORMATION_CLASS t_TokenInformationClass = TokenPrimaryGroup ;
  3310. BOOL t_TokenStatus = GetTokenInformation (
  3311. a_Token ,
  3312. t_TokenInformationClass ,
  3313. t_TokenGroup ,
  3314. t_ReturnLength ,
  3315. & t_ReturnLength
  3316. ) ;
  3317. if ( ! t_TokenStatus )
  3318. {
  3319. DWORD t_LastError = GetLastError () ;
  3320. switch ( t_LastError )
  3321. {
  3322. case ERROR_INSUFFICIENT_BUFFER:
  3323. {
  3324. t_TokenGroup = ( TOKEN_PRIMARY_GROUP * ) new BYTE [ t_ReturnLength ] ;
  3325. if ( t_TokenGroup )
  3326. {
  3327. t_TokenStatus = GetTokenInformation (
  3328. a_Token ,
  3329. t_TokenInformationClass ,
  3330. t_TokenGroup ,
  3331. t_ReturnLength ,
  3332. & t_ReturnLength
  3333. ) ;
  3334. if ( t_TokenStatus )
  3335. {
  3336. DWORD t_SidLength = GetLengthSid ( t_TokenGroup->PrimaryGroup ) ;
  3337. *a_Size = t_SidLength ;
  3338. a_Sid = new BYTE [ t_SidLength ] ;
  3339. if ( a_Sid )
  3340. {
  3341. CopyMemory ( a_Sid , t_TokenGroup->PrimaryGroup , t_SidLength ) ;
  3342. t_Result = S_OK ;
  3343. }
  3344. else
  3345. {
  3346. t_Result = WBEM_E_OUT_OF_MEMORY ;
  3347. }
  3348. }
  3349. delete [] t_TokenGroup ;
  3350. }
  3351. else
  3352. {
  3353. t_Result = WBEM_E_OUT_OF_MEMORY ;
  3354. }
  3355. }
  3356. break ;
  3357. default:
  3358. {
  3359. }
  3360. break ;
  3361. }
  3362. }
  3363. }
  3364. else
  3365. {
  3366. t_Result = WBEM_E_INVALID_PARAMETER ;
  3367. }
  3368. }
  3369. else
  3370. {
  3371. t_Result = ( ERROR_INVALID_HANDLE | 0x80070000 ) ;
  3372. }
  3373. return t_Result ;
  3374. }
  3375. /******************************************************************************
  3376. *
  3377. * Name:
  3378. *
  3379. *
  3380. * Description:
  3381. *
  3382. *
  3383. *****************************************************************************/
  3384. HRESULT ProviderSubSystem_Common_Globals :: SinkAccessInitialize (
  3385. SECURITY_DESCRIPTOR *a_RegistrationSecurityDescriptor ,
  3386. SECURITY_DESCRIPTOR *&a_SinkSecurityDescriptor
  3387. )
  3388. {
  3389. HRESULT t_Result = CoImpersonateClient () ;
  3390. HANDLE t_Token = NULL ;
  3391. BOOL t_Status = FALSE;
  3392. if ( SUCCEEDED ( t_Result ) )
  3393. {
  3394. t_Status = OpenThreadToken (
  3395. GetCurrentThread () ,
  3396. TOKEN_QUERY,
  3397. TRUE ,
  3398. &t_Token
  3399. ) ;
  3400. CoRevertToSelf () ;
  3401. if (!t_Status)
  3402. {
  3403. t_Result = WBEM_E_ACCESS_DENIED;
  3404. }
  3405. }
  3406. else if ( t_Result == RPC_E_CALL_COMPLETE )
  3407. {
  3408. t_Status = OpenProcessToken(
  3409. GetCurrentProcess () ,
  3410. TOKEN_QUERY ,
  3411. &t_Token
  3412. );
  3413. if ( !t_Status )
  3414. {
  3415. t_Result = WBEM_E_ACCESS_DENIED;
  3416. }
  3417. }
  3418. if ( t_Status )
  3419. {
  3420. DWORD t_OwnerSize = 0 ;
  3421. PSID t_OwnerSid = NULL ;
  3422. BOOL t_OwnerDefaulted = FALSE ;
  3423. t_Result = GetUserSid (
  3424. t_Token ,
  3425. & t_OwnerSize ,
  3426. t_OwnerSid
  3427. ) ;
  3428. if ( SUCCEEDED ( t_Result ) )
  3429. {
  3430. DWORD t_GroupSize = 0 ;
  3431. PSID t_GroupSid = NULL ;
  3432. BOOL t_GroupDefaulted = FALSE ;
  3433. t_Result = GetGroupSid (
  3434. t_Token ,
  3435. & t_GroupSize ,
  3436. t_GroupSid
  3437. ) ;
  3438. if ( SUCCEEDED ( t_Result ) )
  3439. {
  3440. t_Result = ProviderSubSystem_Common_Globals :: AdjustSecurityDescriptorWithSid (
  3441. ( SID * ) t_OwnerSid ,
  3442. ( SID * ) t_GroupSid ,
  3443. MASK_PROVIDER_BINDING_BIND ,
  3444. a_RegistrationSecurityDescriptor ,
  3445. a_SinkSecurityDescriptor
  3446. ) ;
  3447. delete [] ( BYTE * ) t_GroupSid ;
  3448. }
  3449. delete [] ( BYTE * ) t_OwnerSid ;
  3450. }
  3451. else
  3452. {
  3453. t_Result = WBEM_E_CRITICAL_ERROR ;
  3454. }
  3455. CloseHandle ( t_Token ) ;
  3456. }
  3457. return t_Result ;
  3458. }
  3459. HRESULT ProviderSubSystem_Common_Globals :: ValidateClientSecurity (
  3460. CServerObject_ProviderRegistrationV1& t_Registration)
  3461. {
  3462. HRESULT t_Result = S_OK ;
  3463. if (!OS::secureOS_)
  3464. return S_OK;
  3465. SECURITY_DESCRIPTOR *t_SecurityDescriptor = t_Registration.GetComRegistration ().GetSecurityDescriptor () ;
  3466. t_Result = ProviderSubSystem_Common_Globals :: Check_SecurityDescriptor_CallIdentity (
  3467. t_SecurityDescriptor ,
  3468. MASK_PROVIDER_BINDING_BIND ,
  3469. & g_ProviderBindingMapping
  3470. ) ;
  3471. return t_Result ;
  3472. }
  3473. /*
  3474. DWORD defultDecoupledSD[] =
  3475. {0x80040001,0x00000070 ,0x00000080 ,0x00000000,
  3476. 0x00000014 ,0x005c0002 ,0x00000004 ,0x00180000,
  3477. 0x10000001 ,0x00000201 ,0x05000000 ,0x00000020,
  3478. 0x00000220 ,0x00140000 ,0x10000001 ,0x00000101,
  3479. 0x05000000 ,0x00000012 ,0x00140000 ,0x10000001,
  3480. 0x00000101 ,0x05000000 ,0x00000003 ,0x00140000,
  3481. 0x10000001 ,0x00000101 ,0x05000000 ,0x00000006,
  3482. 0x00000201 ,0x05000000 ,0x00000020 ,0x00000220,
  3483. 0x00000201 ,0x05000000 ,0x00000020 ,0x00000220};
  3484. */
  3485. HRESULT ProviderSubSystem_Common_Globals :: CreateMethodSecurityDescriptor ()
  3486. {
  3487. HRESULT t_Result = S_OK ;
  3488. if (SDDL::hasSDDLSupport())
  3489. {
  3490. BOOL t_Status = SDDL::ConvertStringSecurityDescriptorToSecurityDescriptor (
  3491. L"O:BAG:BAD:(A;;0x10000001;;;BA)(A;;0x10000001;;;SY)(A;;0x10000001;;;LA)"
  3492. L"(A;;0x10000001;;;S-1-5-3) (A;;0x10000001;;;S-1-5-6)",
  3493. SDDL_REVISION_1 ,
  3494. ( PSECURITY_DESCRIPTOR * ) & s_MethodSecurityDescriptor ,
  3495. NULL
  3496. ) ;
  3497. if ( t_Status )
  3498. {
  3499. }
  3500. else
  3501. {
  3502. t_Result = WBEM_E_CRITICAL_ERROR ;
  3503. }
  3504. }
  3505. return t_Result ;
  3506. }
  3507. /******************************************************************************
  3508. *
  3509. * Name:
  3510. *
  3511. *
  3512. * Description:
  3513. *
  3514. *
  3515. *****************************************************************************/
  3516. HRESULT ProviderSubSystem_Common_Globals :: DeleteMethodSecurityDescriptor ()
  3517. {
  3518. if ( s_MethodSecurityDescriptor )
  3519. {
  3520. LocalFree ( s_MethodSecurityDescriptor ) ;
  3521. s_MethodSecurityDescriptor = NULL;
  3522. }
  3523. return S_OK ;
  3524. }