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.

774 lines
22 KiB

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation.
  4. //
  5. // File: WLBSProvider.CPP
  6. //
  7. // Module: WLBS Instance Provider
  8. //
  9. // Purpose: Defines the CWLBSProvider class. An object of this class is
  10. // created by the class factory for each connection.
  11. //
  12. // History:
  13. //
  14. ////////////////////////////////////////////////////////////////////////////////
  15. #define ENABLE_PROFILE
  16. #include <objbase.h>
  17. #include <process.h>
  18. #include "WLBS_Provider.h"
  19. #include "ClusterWrapper.h"
  20. #include "ControlWrapper.h"
  21. #include "utils.h"
  22. #include "WLBS_Provider.tmh" // for event tracing
  23. ////////////////////////////////////////////////////////////////////////////////
  24. //
  25. //CWLBSProvider::CWLBSProvider
  26. // CWLBSProvider::~CWLBSProvider
  27. //
  28. ////////////////////////////////////////////////////////////////////////////////
  29. CWLBSProvider::CWLBSProvider(
  30. BSTR a_strObjectPath,
  31. BSTR a_strUser,
  32. BSTR a_strPassword,
  33. IWbemContext * a_pContex
  34. )
  35. {
  36. m_pNamespace = NULL;
  37. InterlockedIncrement(&g_cComponents);
  38. return;
  39. }
  40. CWLBSProvider::~CWLBSProvider(void)
  41. {
  42. InterlockedDecrement(&g_cComponents);
  43. return;
  44. }
  45. ////////////////////////////////////////////////////////////////////////////////
  46. //
  47. // CWLBSProvider::Initialize
  48. //
  49. // Purpose: This is the implementation of IWbemProviderInit. The method
  50. // is called by WinMgMt.
  51. //
  52. ////////////////////////////////////////////////////////////////////////////////
  53. STDMETHODIMP CWLBSProvider::Initialize(
  54. LPWSTR a_pszUser,
  55. LONG a_lFlags,
  56. LPWSTR a_pszNamespace,
  57. LPWSTR a_pszLocale,
  58. IWbemServices * a_pNamespace,
  59. IWbemContext * a_pCtx,
  60. IWbemProviderInitSink * a_pInitSink
  61. )
  62. {
  63. TRACE_CRIT("->%!FUNC!");
  64. try {
  65. //!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  66. //g_pWlbsControl must be initialized when the first COM instance is invoked
  67. //and it must stay alive for the lifetime of the DLL, i.e. do NOT DESTROY
  68. //it in the destructor of this CLASS until the API cache of the Cluster IP
  69. //and Password are REMOVED.
  70. //DO NOT INITIALIZE g_pWlbsControl in DLLMain. DLLMain is invoked with regsvr32
  71. //and we do not want to initialize WinSock at that time!!! This will BREAK the
  72. //installation process of the provider.
  73. //!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  74. HRESULT hr;
  75. // The CoImpersonateClient call below is required for CreateFile call to succeed
  76. hr = CoImpersonateClient();
  77. if (hr != S_OK) {
  78. TRACE_CRIT("%!FUNC! CoImpersonateClient() returned error : 0x%x, Throwing an exception", hr);
  79. throw _com_error( hr );
  80. }
  81. // Check for Admin privileges
  82. if (IsCallerAdmin() == FALSE)
  83. {
  84. TRACE_CRIT("%!FUNC! IsCallerAdmin() returned FALSE, Throwing com_error WBEM_E_ACCESS_DENIED exception");
  85. throw _com_error( WBEM_E_ACCESS_DENIED );
  86. }
  87. if( g_pWlbsControl == NULL ) {
  88. g_pWlbsControl = new CWlbsControlWrapper();
  89. if( g_pWlbsControl == NULL)
  90. {
  91. TRACE_CRIT("%!FUNC! new returned NULL, Throwing com_error WBEM_E_OUT_OF_MEMORY exception");
  92. throw _com_error( WBEM_E_OUT_OF_MEMORY );
  93. }
  94. }
  95. g_pWlbsControl->Initialize();
  96. hr = CImpersonatedProvider::Initialize
  97. (
  98. a_pszUser,
  99. a_lFlags,
  100. a_pszNamespace,
  101. a_pszLocale,
  102. a_pNamespace,
  103. a_pCtx,
  104. a_pInitSink
  105. );
  106. TRACE_CRIT("<-%!FUNC! return 0x%x (returned by CImpersonatedProvider::Initialize)", hr);
  107. return hr;
  108. }
  109. catch (...)
  110. {
  111. TRACE_CRIT("%!FUNC! caught an exception");
  112. TRACE_CRIT("<-%!FUNC! return WBEM_E_FAILED");
  113. return WBEM_E_FAILED;
  114. }
  115. }
  116. ////////////////////////////////////////////////////////////////////////////////
  117. //
  118. // CWLBSProvider::CreateInstanceEnumAsync
  119. //
  120. // Purpose: Asynchronously enumerates the instances.
  121. //
  122. ////////////////////////////////////////////////////////////////////////////////
  123. STDMETHODIMP CWLBSProvider::DoCreateInstanceEnumAsync(
  124. BSTR a_strClass,
  125. long a_lFlags,
  126. IWbemContext * a_pIContex,
  127. IWbemObjectSink * a_pIResponseHandler
  128. )
  129. {
  130. TRACE_CRIT("->%!FUNC!");
  131. try {
  132. // Check for Admin privileges
  133. if (IsCallerAdmin() == FALSE)
  134. {
  135. TRACE_CRIT("%!FUNC! IsCallerAdmin() returned FALSE, Throwing com_error WBEM_E_ACCESS_DENIED exception");
  136. throw _com_error( WBEM_E_ACCESS_DENIED );
  137. }
  138. ASSERT(g_pWlbsControl);
  139. if (g_pWlbsControl)
  140. {
  141. //
  142. // Re-enumerate all the clusters
  143. //
  144. g_pWlbsControl->ReInitialize();
  145. }
  146. auto_ptr<CWlbs_Root> pMofClass;
  147. //create an instance of the appropriate MOF support class
  148. HRESULT hRes = GetMOFSupportClass(a_strClass, pMofClass, a_pIResponseHandler);
  149. //call the appropriate wrapper class GetInstance method
  150. if( SUCCEEDED( hRes ) && pMofClass.get() != NULL)
  151. hRes = pMofClass->EnumInstances( a_strClass );
  152. TRACE_CRIT("<-%!FUNC! return 0x%x", hRes);
  153. return hRes;
  154. }
  155. catch(_com_error HResErr ) {
  156. TRACE_CRIT("%!FUNC! caught 0x%x com_error exception",HResErr.Error());
  157. a_pIResponseHandler->SetStatus(0, HResErr.Error(), NULL, NULL);
  158. TRACE_CRIT("<-%!FUNC! return 0x%x", HResErr.Error());
  159. return HResErr.Error();
  160. }
  161. catch(...) {
  162. TRACE_CRIT("%!FUNC! caught an exception");
  163. a_pIResponseHandler->SetStatus(0, WBEM_E_FAILED, NULL, NULL);
  164. TRACE_CRIT("<-%!FUNC! return WBEM_E_FAILED");
  165. return WBEM_E_FAILED;
  166. }
  167. TRACE_CRIT("<-%!FUNC!");
  168. }
  169. ////////////////////////////////////////////////////////////////////////////////
  170. //
  171. // CWLBSProvider::GetObjectAsync
  172. //
  173. // Purpose: Gets an instance for a particular object path
  174. //
  175. ////////////////////////////////////////////////////////////////////////////////
  176. STDMETHODIMP CWLBSProvider::DoGetObjectAsync(
  177. BSTR a_strObjectPath,
  178. long a_lFlags,
  179. IWbemContext * a_pIContex,
  180. IWbemObjectSink * a_pIResponseHandler
  181. )
  182. {
  183. TRACE_CRIT("->%!FUNC!");
  184. ParsedObjectPath* pParsedPath = NULL;
  185. try {
  186. // Check for Admin privileges
  187. if (IsCallerAdmin() == FALSE)
  188. {
  189. TRACE_CRIT("%!FUNC! IsCallerAdmin() returned FALSE, Throwing com_error WBEM_E_ACCESS_DENIED exception");
  190. throw _com_error( WBEM_E_ACCESS_DENIED );
  191. }
  192. ASSERT(g_pWlbsControl);
  193. if (g_pWlbsControl)
  194. {
  195. //
  196. // Re-enumerate all the clusters
  197. //
  198. g_pWlbsControl->ReInitialize();
  199. }
  200. auto_ptr<CWlbs_Root> pMofClass;
  201. //parse the object path
  202. ParseObjectPath( a_strObjectPath, &pParsedPath );
  203. //create an instance of the appropriate MOF support class
  204. HRESULT hRes = GetMOFSupportClass( pParsedPath->m_pClass, pMofClass, a_pIResponseHandler );
  205. //call the appropriate wrapper class GetInstance method
  206. if( SUCCEEDED( hRes ) && pMofClass.get() != NULL)
  207. hRes = pMofClass->GetInstance( pParsedPath );
  208. if( pParsedPath )
  209. CObjectPathParser().Free( pParsedPath );
  210. TRACE_CRIT("<-%!FUNC! return 0x%x", hRes);
  211. return hRes;
  212. }
  213. catch(_com_error HResErr) {
  214. TRACE_CRIT("%!FUNC! caught 0x%x com_error exception",HResErr.Error());
  215. a_pIResponseHandler->SetStatus(0, HResErr.Error(), NULL, NULL);
  216. if( pParsedPath )
  217. CObjectPathParser().Free( pParsedPath );
  218. TRACE_CRIT("<-%!FUNC! return 0x%x", HResErr.Error());
  219. return HResErr.Error();
  220. }
  221. catch(...) {
  222. TRACE_CRIT("%!FUNC! caught an exception");
  223. if( pParsedPath )
  224. CObjectPathParser().Free( pParsedPath );
  225. a_pIResponseHandler->SetStatus(0, WBEM_E_FAILED, NULL, NULL);
  226. TRACE_CRIT("<-%!FUNC! return WBEM_E_FAILED");
  227. return WBEM_E_FAILED;
  228. }
  229. TRACE_CRIT("<-%!FUNC!");
  230. }
  231. ////////////////////////////////////////////////////////////////////////////////
  232. //
  233. // CWLBSProvider::DoDeleteInstanceAsync
  234. //
  235. // Purpose: Gets an instance from a particular object path
  236. //
  237. ////////////////////////////////////////////////////////////////////////////////
  238. STDMETHODIMP CWLBSProvider::DoDeleteInstanceAsync(
  239. BSTR a_strObjectPath,
  240. long a_lFlags,
  241. IWbemContext * a_pIContex,
  242. IWbemObjectSink * a_pIResponseHandler
  243. )
  244. {
  245. TRACE_CRIT("->%!FUNC!");
  246. ParsedObjectPath* pParsedPath = NULL;
  247. try {
  248. // Check for Admin privileges
  249. if (IsCallerAdmin() == FALSE)
  250. {
  251. TRACE_CRIT("%!FUNC! IsCallerAdmin() returned FALSE, Throwing com_error WBEM_E_ACCESS_DENIED exception");
  252. throw _com_error( WBEM_E_ACCESS_DENIED );
  253. }
  254. ASSERT(g_pWlbsControl);
  255. if (g_pWlbsControl)
  256. {
  257. //
  258. // Re-enumerate all the clusters
  259. //
  260. g_pWlbsControl->ReInitialize();
  261. }
  262. auto_ptr<CWlbs_Root> pMofClass;
  263. //parse the object path
  264. ParseObjectPath( a_strObjectPath, &pParsedPath );
  265. //create an instance of the appropriate MOF support class
  266. HRESULT hRes = GetMOFSupportClass( pParsedPath->m_pClass, pMofClass, a_pIResponseHandler );
  267. //call the appropriate wrapper class GetInstance method
  268. if( SUCCEEDED( hRes ) && pMofClass.get() != NULL)
  269. hRes = pMofClass->DeleteInstance( pParsedPath );
  270. if( pParsedPath )
  271. CObjectPathParser().Free( pParsedPath );
  272. TRACE_CRIT("<-%!FUNC! return 0x%x", hRes);
  273. return hRes;
  274. }
  275. catch(_com_error HResErr) {
  276. TRACE_CRIT("%!FUNC! caught 0x%x com_error exception",HResErr.Error());
  277. a_pIResponseHandler->SetStatus(0, HResErr.Error(), NULL, NULL);
  278. if( pParsedPath )
  279. CObjectPathParser().Free( pParsedPath );
  280. TRACE_CRIT("<-%!FUNC! return 0x%x", HResErr.Error());
  281. return HResErr.Error();
  282. }
  283. catch(...) {
  284. TRACE_CRIT("%!FUNC! caught an exception");
  285. if( pParsedPath )
  286. CObjectPathParser().Free( pParsedPath );
  287. a_pIResponseHandler->SetStatus(0, WBEM_E_FAILED, NULL, NULL);
  288. TRACE_CRIT("<-%!FUNC! return WBEM_E_FAILED");
  289. return WBEM_E_FAILED;
  290. }
  291. TRACE_CRIT("<-%!FUNC!");
  292. }
  293. ////////////////////////////////////////////////////////////////////////////////
  294. //
  295. // CWLBSProvider::ExecMethodAsync
  296. //
  297. // Purpose: Executes a MOF class method.
  298. //
  299. ////////////////////////////////////////////////////////////////////////////////
  300. STDMETHODIMP CWLBSProvider::DoExecMethodAsync(
  301. BSTR a_strObjectPath,
  302. BSTR a_strMethodName,
  303. long a_lFlags,
  304. IWbemContext * a_pIContex,
  305. IWbemClassObject * a_pIInParams,
  306. IWbemObjectSink * a_pIResponseHandler
  307. )
  308. {
  309. TRACE_CRIT("->%!FUNC!");
  310. ParsedObjectPath* pParsedPath = NULL;
  311. try {
  312. // Check for Admin privileges
  313. if (IsCallerAdmin() == FALSE)
  314. {
  315. TRACE_CRIT("%!FUNC! IsCallerAdmin() returned FALSE, Throwing com_error WBEM_E_ACCESS_DENIED exception");
  316. throw _com_error( WBEM_E_ACCESS_DENIED );
  317. }
  318. ASSERT(g_pWlbsControl);
  319. if (g_pWlbsControl)
  320. {
  321. //
  322. // Re-enumerate all the clusters
  323. //
  324. g_pWlbsControl->ReInitialize();
  325. }
  326. //parse the object path
  327. auto_ptr<CWlbs_Root> pMofClass;
  328. //parse the object path
  329. ParseObjectPath(a_strObjectPath, &pParsedPath);
  330. //create an instance of the appropriate MOF support class
  331. HRESULT hRes = GetMOFSupportClass(pParsedPath->m_pClass, pMofClass, a_pIResponseHandler);
  332. //execute MOF class method
  333. if( SUCCEEDED( hRes ) && pMofClass.get() != NULL)
  334. hRes = pMofClass->ExecMethod( pParsedPath,
  335. a_strMethodName,
  336. 0,
  337. NULL,
  338. a_pIInParams );
  339. if( pParsedPath )
  340. CObjectPathParser().Free( pParsedPath );
  341. TRACE_CRIT("<-%!FUNC! return 0x%x", hRes);
  342. return hRes;
  343. }
  344. catch(_com_error HResErr) {
  345. TRACE_CRIT("%!FUNC! caught 0x%x com_error exception",HResErr.Error());
  346. a_pIResponseHandler->SetStatus(0, HResErr.Error(), NULL, NULL);
  347. if( pParsedPath )
  348. CObjectPathParser().Free( pParsedPath );
  349. TRACE_CRIT("<-%!FUNC! return 0x%x", HResErr.Error());
  350. return HResErr.Error();
  351. }
  352. catch(...) {
  353. TRACE_CRIT("%!FUNC! caught an exception");
  354. if( pParsedPath )
  355. CObjectPathParser().Free( pParsedPath );
  356. a_pIResponseHandler->SetStatus(0, WBEM_E_FAILED, NULL, NULL);
  357. TRACE_CRIT("<-%!FUNC! return WBEM_E_FAILED");
  358. return WBEM_E_FAILED;
  359. }
  360. TRACE_CRIT("<-%!FUNC!");
  361. }
  362. ////////////////////////////////////////////////////////////////////////////////
  363. //
  364. // CWLBSProvider::PutInstanceAsync
  365. //
  366. // Purpose: Creates or modifies an instance
  367. //
  368. ////////////////////////////////////////////////////////////////////////////////
  369. STDMETHODIMP CWLBSProvider::DoPutInstanceAsync
  370. (
  371. IWbemClassObject* a_pInst,
  372. long a_lFlags,
  373. IWbemContext* a_pIContex,
  374. IWbemObjectSink* a_pIResponseHandler
  375. )
  376. {
  377. TRACE_CRIT("->%!FUNC!");
  378. ParsedObjectPath* pParsedPath = NULL;
  379. HRESULT hRes = 0;
  380. try {
  381. // Check for Admin privileges
  382. if (IsCallerAdmin() == FALSE)
  383. {
  384. TRACE_CRIT("%!FUNC! IsCallerAdmin() returned FALSE, Throwing com_error WBEM_E_ACCESS_DENIED exception");
  385. throw _com_error( WBEM_E_ACCESS_DENIED );
  386. }
  387. ASSERT(g_pWlbsControl);
  388. if (g_pWlbsControl)
  389. {
  390. //
  391. // Re-enumerate all the clusters
  392. //
  393. g_pWlbsControl->ReInitialize();
  394. }
  395. wstring szClass;
  396. auto_ptr<CWlbs_Root> pMofClass;
  397. //retrieve the class name
  398. GetClass( a_pInst, szClass );
  399. //create an instance of the appropriate MOF support class
  400. hRes = GetMOFSupportClass( szClass.c_str(), pMofClass, a_pIResponseHandler );
  401. //call the appropriate wrapper class PutInstance method
  402. if( SUCCEEDED( hRes ) && pMofClass.get() != NULL)
  403. hRes = pMofClass->PutInstance( a_pInst );
  404. if( pParsedPath )
  405. CObjectPathParser().Free( pParsedPath );
  406. }
  407. catch(_com_error HResErr) {
  408. TRACE_CRIT("%!FUNC! caught 0x%x com_error exception",HResErr.Error());
  409. a_pIResponseHandler->SetStatus(0, HResErr.Error(), NULL, NULL);
  410. if( pParsedPath )
  411. CObjectPathParser().Free( pParsedPath );
  412. hRes = HResErr.Error();
  413. }
  414. catch(...) {
  415. TRACE_CRIT("%!FUNC! caught an exception");
  416. if( pParsedPath )
  417. CObjectPathParser().Free( pParsedPath );
  418. a_pIResponseHandler->SetStatus(0, WBEM_E_FAILED, NULL, NULL);
  419. hRes = WBEM_E_FAILED;
  420. }
  421. TRACE_CRIT("<-%!FUNC! return 0x%x", hRes);
  422. return hRes;
  423. }
  424. ////////////////////////////////////////////////////////////////////////////////
  425. //
  426. // CWLBSProvider::GetMOFSupportClass
  427. //
  428. // Purpose: Determines which MOF class is requested and instantiates the
  429. // appropriate internal support class.
  430. //
  431. ////////////////////////////////////////////////////////////////////////////////
  432. HRESULT CWLBSProvider::GetMOFSupportClass(
  433. LPCWSTR a_szObjectClass,
  434. auto_ptr<CWlbs_Root>& a_pMofClass,
  435. IWbemObjectSink* a_pResponseHandler )
  436. {
  437. TRACE_VERB("->%!FUNC!");
  438. HRESULT hRes = 0;
  439. try {
  440. for( DWORD i = 0; i < MOF_CLASSES::NumClasses; i++ ) {
  441. if( _wcsicmp( a_szObjectClass, MOF_CLASSES::g_szMOFClassList[i] ) == 0) {
  442. a_pMofClass = auto_ptr<CWlbs_Root>
  443. (MOF_CLASSES::g_pCreateFunc[i]( m_pNamespace, a_pResponseHandler ));
  444. break;
  445. }
  446. }
  447. }
  448. catch(CErrorWlbsControl Err) {
  449. TRACE_VERB("%!FUNC! Caught a Wlbs exception : %ls", (PWCHAR)(Err.Description()));
  450. IWbemClassObject* pWbemExtStat = NULL;
  451. CWlbs_Root::CreateExtendedStatus( m_pNamespace,
  452. &pWbemExtStat,
  453. Err.Error(),
  454. (PWCHAR)(Err.Description()) );
  455. a_pResponseHandler->SetStatus(0, WBEM_E_FAILED, NULL, pWbemExtStat);
  456. //do not return WBEM_E_FAILED, this causes a race condition
  457. hRes = WBEM_S_NO_ERROR;
  458. pWbemExtStat->Release();
  459. }
  460. TRACE_VERB("<-%!FUNC! return 0x%x", hRes);
  461. return hRes;
  462. }
  463. ////////////////////////////////////////////////////////////////////////////////
  464. //
  465. // CWLBSProvider::ParseObjectPath
  466. //
  467. // Purpose:
  468. //
  469. ////////////////////////////////////////////////////////////////////////////////
  470. void CWLBSProvider::ParseObjectPath(
  471. const BSTR a_strObjectPath,
  472. ParsedObjectPath **a_pParsedObjectPath )
  473. {
  474. CObjectPathParser PathParser;
  475. TRACE_VERB("->%!FUNC! a_strObjectPath : %ls", a_strObjectPath);
  476. ASSERT( a_pParsedObjectPath );
  477. //make sure this is NULL
  478. *a_pParsedObjectPath = NULL;
  479. int nStatus = PathParser.Parse(a_strObjectPath, a_pParsedObjectPath);
  480. if(nStatus != 0) {
  481. if( *a_pParsedObjectPath)
  482. PathParser.Free( *a_pParsedObjectPath );
  483. TRACE_CRIT("%!FUNC! CObjectPathParser::Parse failed, Throwing com_error WBEM_E_FAILED exception");
  484. TRACE_VERB("<-%!FUNC!");
  485. throw _com_error( WBEM_E_FAILED );
  486. }
  487. TRACE_VERB("<-%!FUNC!");
  488. }
  489. ////////////////////////////////////////////////////////////////////////////////
  490. //
  491. // CWLBSProvider::GetClass
  492. //
  493. // Purpose: Retrieve the class name from an IWbemClassObject.
  494. //
  495. ////////////////////////////////////////////////////////////////////////////////
  496. void CWLBSProvider::GetClass(
  497. IWbemClassObject* a_pClassObject,
  498. wstring& a_szClass )
  499. {
  500. BSTR strClassName = NULL;
  501. VARIANT vClassName;
  502. HRESULT hRes;
  503. TRACE_VERB("->%!FUNC!");
  504. try {
  505. VariantInit( &vClassName );
  506. strClassName = SysAllocString( L"__Class" );
  507. if( !strClassName )
  508. {
  509. TRACE_CRIT("%!FUNC! SysAllocString returned NULL, Throwing com_error WBEM_E_OUT_OF_MEMORY exception");
  510. throw _com_error( WBEM_E_OUT_OF_MEMORY );
  511. }
  512. hRes = a_pClassObject->Get( strClassName,
  513. 0,
  514. &vClassName,
  515. NULL,
  516. NULL);
  517. if( FAILED( hRes ) )
  518. {
  519. TRACE_CRIT("%!FUNC! IWbemClassObject::Get returned error : 0x%x, Throwing com_error exception", hRes);
  520. throw _com_error( hRes );
  521. }
  522. a_szClass.assign( static_cast<LPWSTR>(vClassName.bstrVal) );
  523. // CLD: Need to check return code for error
  524. if (S_OK != VariantClear( &vClassName ))
  525. {
  526. TRACE_CRIT("%!FUNC! VariantClear returned error, Throwing com_error WBEM_E_FAILED exception");
  527. throw _com_error( WBEM_E_FAILED );
  528. }
  529. if( strClassName ) {
  530. SysFreeString( strClassName );
  531. strClassName = NULL;
  532. }
  533. }
  534. catch( ... ) {
  535. TRACE_CRIT("%!FUNC! Caught an exception");
  536. // CLD: Need to check return code for error
  537. // No throw here since we are already throwing an exception.
  538. VariantClear( &vClassName );
  539. if( strClassName ) {
  540. SysFreeString( strClassName );
  541. strClassName = NULL;
  542. }
  543. TRACE_CRIT("%!FUNC! Rethrowing exception");
  544. TRACE_VERB("<-%!FUNC!");
  545. throw;
  546. }
  547. TRACE_VERB("<-%!FUNC!");
  548. }
  549. ////////////////////////////////////////////////////////////////////////////////
  550. //
  551. // Name : IsCallerAdmin
  552. // Description : This function checks if the caller is a member of the
  553. // Administrators local group. Since the provider is acting on
  554. // behalf of the client, it is important to IMPERSONATE the client
  555. // BEFORE calling this function. Impersonating the client will ensure
  556. // that this function checks if the client (& NOT this process that
  557. // runs under the identity of System(?)) is a member of the Administrators
  558. // local group.
  559. // Arguments : None.
  560. // Return Value:
  561. // TRUE - Caller is a member of Administrators local group.
  562. // FALSE - Caller is NOT a member of Administrators local group.
  563. ////////////////////////////////////////////////////////////////////////////////
  564. BOOL CWLBSProvider::IsCallerAdmin(VOID)
  565. {
  566. BOOL bRet;
  567. PSID AdministratorsGroup;
  568. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  569. TRACE_VERB(L"->%!FUNC!");
  570. //
  571. // Allocate and Initialize SID for Administrators in the built-in system domain
  572. //
  573. bRet = AllocateAndInitializeSid(&NtAuthority,
  574. 2,
  575. SECURITY_BUILTIN_DOMAIN_RID, // The built-in system domain (S-1-5-32)
  576. DOMAIN_ALIAS_RID_ADMINS, // Local group used for administration of the domain
  577. 0, 0, 0, 0, 0, 0,
  578. &AdministratorsGroup);
  579. if(bRet)
  580. {
  581. //
  582. // Is SID enabled in the impersonation token of the calling thread ?
  583. //
  584. if (!CheckTokenMembership(NULL, // Use the Impersonation token of the calling thread
  585. AdministratorsGroup,
  586. &bRet))
  587. {
  588. bRet = FALSE;
  589. TRACE_CRIT(L"%!FUNC! CheckTokenMembership() failed. Error : 0x%x", GetLastError());
  590. }
  591. FreeSid(AdministratorsGroup);
  592. }
  593. else
  594. {
  595. TRACE_CRIT("%!FUNC! AllocateAndInitializeSid() failed. Error : 0x%x", GetLastError());
  596. }
  597. TRACE_VERB(L"<-%!FUNC! Returning %ls", bRet ? L"TRUE" : L"FALSE");
  598. return bRet;
  599. }