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.

945 lines
31 KiB

  1. //*************************************************************
  2. //
  3. // Copyright (c)1999 Microsoft Corporation, All Rights Reserved
  4. //
  5. // gpdas.cpp
  6. //
  7. // Module: Rsop Planning mode Provider
  8. //
  9. // History: 11-Jul-99 MickH Created
  10. //
  11. //*************************************************************
  12. #include "stdafx.h"
  13. #include "planprov.h"
  14. #include "gpdas.h"
  15. #include <lm.h>
  16. #include <dsgetdc.h>
  17. #define SECURITY_WIN32
  18. #include <security.h>
  19. #include "userenv.h"
  20. #include "userenvp.h"
  21. #include "rsopinc.h"
  22. #include "rsoputil.h"
  23. #include "rsopdbg.h"
  24. #include "rsopsec.h"
  25. #include "Indicate.h"
  26. #include "events.h"
  27. #include "gpfilter.h"
  28. CDebug dbgRsop( L"Software\\Microsoft\\Windows NT\\CurrentVersion\\winlogon",
  29. L"RsopDebugLevel",
  30. L"gpdas.log",
  31. L"gpdas.bak",
  32. TRUE );
  33. CDebug dbgCommon( L"Software\\Microsoft\\Windows NT\\CurrentVersion\\winlogon",
  34. L"RsopDebugLevel",
  35. L"gpdas.log",
  36. L"gpdas.bak",
  37. FALSE );
  38. extern "C" PSID GetUserSid (HANDLE UserToken);
  39. class CAutoNetApiBufferFree
  40. {
  41. private:
  42. LPVOID _pV;
  43. public:
  44. CAutoNetApiBufferFree(LPVOID pV)
  45. : _pV(pV)
  46. {
  47. }
  48. ~CAutoNetApiBufferFree()
  49. {
  50. if (_pV)
  51. NetApiBufferFree(_pV);
  52. }
  53. };
  54. bool SplitName(LPCWSTR pszUser, LPWSTR* ppszUserDomain, LPWSTR* ppszUserName)
  55. {
  56. HRESULT hr = S_OK;
  57. if(!pszUser)
  58. {
  59. return false;
  60. }
  61. *ppszUserDomain = NULL;
  62. *ppszUserName = NULL;
  63. wchar_t* p = wcschr(pszUser, L'\\');
  64. if(p)
  65. {
  66. LONG userDomainLength = (LONG)(p - pszUser);
  67. if(!userDomainLength)
  68. {
  69. return false;
  70. }
  71. *ppszUserDomain = new wchar_t[userDomainLength + 1];
  72. if(!*ppszUserDomain)
  73. {
  74. return false;
  75. }
  76. int userNameLength = wcslen(pszUser) - userDomainLength - 1;
  77. *ppszUserName = new wchar_t[userNameLength + 1];
  78. if(!*ppszUserName)
  79. {
  80. delete[] *ppszUserDomain;
  81. *ppszUserDomain = NULL;
  82. return false;
  83. }
  84. wcsncpy(*ppszUserDomain, pszUser, userDomainLength);
  85. hr = StringCchCopy(*ppszUserName, userNameLength + 1, pszUser + userDomainLength + 1);
  86. if(FAILED(hr))
  87. {
  88. delete[] *ppszUserDomain;
  89. delete[] *ppszUserName;
  90. *ppszUserDomain = NULL;
  91. *ppszUserName = NULL;
  92. return false;
  93. }
  94. }
  95. else
  96. {
  97. int userNameLength = wcslen(pszUser);
  98. *ppszUserName = new wchar_t[userNameLength + 1];
  99. if(!*ppszUserName)
  100. {
  101. return false;
  102. }
  103. *ppszUserDomain = NULL;
  104. hr = StringCchCopy(*ppszUserName, userNameLength + 1, pszUser);
  105. if(FAILED(hr))
  106. {
  107. delete[] *ppszUserName;
  108. *ppszUserName = NULL;
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. //*************************************************************
  115. //
  116. // RsopPlanningModeProvider::RsopPlanningModeProvider()
  117. //
  118. // Purpose: Constructor
  119. //
  120. //*************************************************************
  121. RsopPlanningModeProvider::RsopPlanningModeProvider()
  122. : m_pWbemServices(NULL),
  123. m_bInitialized(FALSE),
  124. m_pStream(NULL)
  125. {
  126. _Module.IncrementServiceCount();
  127. m_xbstrMachName = L"computerName";
  128. if ( !m_xbstrMachName )
  129. return;
  130. m_xbstrMachSOM = L"computerSOM";
  131. if ( !m_xbstrMachSOM )
  132. return;
  133. m_xbstrMachGroups = L"computerSecurityGroups";
  134. if ( !m_xbstrMachGroups )
  135. return;
  136. m_xbstrUserName = L"userName";
  137. if ( !m_xbstrUserName )
  138. return;
  139. m_xbstrUserSOM = L"userSOM";
  140. if ( !m_xbstrUserSOM )
  141. return;
  142. m_xbstrUserGroups = L"userSecurityGroups";
  143. if ( !m_xbstrUserGroups )
  144. return;
  145. m_xbstrSite = L"site";
  146. if ( !m_xbstrSite )
  147. return;
  148. m_xbstrUserGpoFilter = L"userGPOFilters";
  149. if ( !m_xbstrUserGpoFilter )
  150. return;
  151. m_xbstrComputerGpoFilter = L"computerGPOFilters";
  152. if ( !m_xbstrComputerGpoFilter )
  153. return;
  154. m_xbstrFlags = L"flags";
  155. if ( !m_xbstrFlags )
  156. return;
  157. m_xbstrNameSpace = L"nameSpace";
  158. if ( !m_xbstrNameSpace )
  159. return;
  160. m_xbstrResult = L"hResult";
  161. if ( !m_xbstrResult )
  162. return;
  163. m_xbstrExtendedInfo = L"ExtendedInfo";
  164. if ( !m_xbstrExtendedInfo )
  165. return;
  166. m_xbstrClass = L"RsopPlanningModeProvider";
  167. if ( !m_xbstrClass )
  168. return;
  169. // m_xptrInvokerName = 0;
  170. m_bInitialized = TRUE;
  171. }
  172. //*************************************************************
  173. //
  174. // Initialize()
  175. //
  176. // Purpose: WbemProvider's initialize method
  177. //
  178. // Parameters: See IWbemProivderInit::Initialize
  179. //
  180. // Return: hresult
  181. //
  182. //*************************************************************
  183. STDMETHODIMP RsopPlanningModeProvider::Initialize( LPWSTR pszUser,
  184. LONG lFlags,
  185. LPWSTR pszNamespace,
  186. LPWSTR pszLocale,
  187. IWbemServices __RPC_FAR *pNamespace,
  188. IWbemContext __RPC_FAR *pCtx,
  189. IWbemProviderInitSink __RPC_FAR *pInitSink )
  190. {
  191. HRESULT hr;
  192. if ( !m_bInitialized ) {
  193. hr = pInitSink->SetStatus(E_FAIL, 0);
  194. return hr;
  195. }
  196. if ( !pszUser )
  197. {
  198. hr = pInitSink->SetStatus(E_INVALIDARG, 0);
  199. return hr;
  200. }
  201. if(m_pWbemServices)
  202. {
  203. m_pWbemServices->Release();
  204. m_pWbemServices = NULL;
  205. }
  206. m_pWbemServices = pNamespace;
  207. m_pWbemServices->AddRef();
  208. hr = CoMarshalInterThreadInterfaceInStream(__uuidof(IWbemServices), m_pWbemServices, &m_pStream);
  209. if(SUCCEEDED(hr))
  210. hr = pInitSink->SetStatus(WBEM_S_INITIALIZED, 0);
  211. else
  212. {
  213. m_pWbemServices->Release();
  214. hr = pInitSink->SetStatus(hr, 0);
  215. }
  216. return hr;
  217. }
  218. //*************************************************************
  219. //
  220. // ExecMethodAsync()
  221. //
  222. // Purpose: Execute method
  223. //
  224. // Parameters: See IWbemServices::ExecMethodAsync
  225. //
  226. // Return: hresult
  227. //
  228. //*************************************************************
  229. STDMETHODIMP RsopPlanningModeProvider::ExecMethodAsync( BSTR bstrObject,
  230. BSTR bstrMethod,
  231. long lFlags,
  232. IWbemContext* pCtx,
  233. IWbemClassObject* pInParams,
  234. IWbemObjectSink* pResponseHandler )
  235. {
  236. dbgRsop.Initialize( L"Software\\Microsoft\\Windows NT\\CurrentVersion\\winlogon",
  237. L"RsopDebugLevel",
  238. L"gpdas.log",
  239. L"gpdas.bak",
  240. FALSE );
  241. dbgCommon.Initialize( L"Software\\Microsoft\\Windows NT\\CurrentVersion\\winlogon",
  242. L"RsopDebugLevel",
  243. L"gpdas.log",
  244. L"gpdas.bak",
  245. FALSE );
  246. //
  247. // Initialize the return status object to fail status
  248. //
  249. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("ExecMethodAsync: Entering") );
  250. CFailRetStatus retStatus( pResponseHandler );
  251. HRESULT hr;
  252. XInterface<IWbemLocator> xLocator;
  253. hr = CoCreateInstance( CLSID_WbemLocator,
  254. NULL,
  255. CLSCTX_INPROC_SERVER,
  256. IID_IWbemLocator,
  257. (LPVOID *) &xLocator );
  258. if ( FAILED(hr) )
  259. {
  260. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecMethodAsync: CoCreateInstance returned 0x%x"), hr );
  261. retStatus.SetError( hr );
  262. return hr;
  263. }
  264. IWbemClassObject* pProvClass = NULL;
  265. IWbemClassObject* pOutClass = NULL;
  266. IWbemClassObject* pOutParams = NULL;
  267. IWbemServices* pWbemServices = NULL;
  268. hr = CoGetInterfaceAndReleaseStream(m_pStream, __uuidof(IWbemServices), (void**)&pWbemServices);
  269. if ( FAILED(hr) )
  270. {
  271. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::CoGetInterfaceAndReleaseStream failed with 0x%x."), hr );
  272. retStatus.SetError( hr );
  273. return hr;
  274. }
  275. hr = pWbemServices->GetObject( m_xbstrClass, WBEM_FLAG_RETURN_WBEM_COMPLETE, pCtx, &pProvClass, NULL);
  276. if ( FAILED(hr) )
  277. {
  278. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::GetObject failed with 0x%x."), hr );
  279. // Try to marshall stream back before quiting
  280. HRESULT hrMarshall = CoMarshalInterThreadInterfaceInStream(__uuidof(IWbemServices), pWbemServices, &m_pStream);
  281. if ( FAILED(hrMarshall) )
  282. {
  283. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::CoMarshallInterThreadInterfaceInStream failed with 0x%x."), hrMarshall );
  284. m_pStream = NULL;
  285. }
  286. retStatus.SetError( hr );
  287. return hr;
  288. }
  289. XInterface<IWbemClassObject> xProvClass( pProvClass );
  290. hr = CoMarshalInterThreadInterfaceInStream(__uuidof(IWbemServices), pWbemServices, &m_pStream);
  291. if ( FAILED(hr) )
  292. {
  293. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::CoMarshallInterThreadInterfaceInStream failed with 0x%x."), hr );
  294. retStatus.SetError( hr );
  295. return hr;
  296. }
  297. hr = pProvClass->GetMethod( bstrMethod, 0, NULL, &pOutClass);
  298. if ( FAILED(hr) )
  299. {
  300. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::GetMethod failed with 0x%x."), hr );
  301. retStatus.SetError( hr );
  302. return hr;
  303. }
  304. XInterface<IWbemClassObject> xOutClass( pOutClass );
  305. hr = pOutClass->SpawnInstance(0, &pOutParams);
  306. if ( FAILED(hr) )
  307. {
  308. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::SpawnInstance failed with 0x%x."), hr );
  309. retStatus.SetError( hr );
  310. return hr;
  311. }
  312. XInterface<IWbemClassObject> xOutParams( pOutParams );
  313. XHandle xhUserToken;
  314. {
  315. XImpersonate xImp;
  316. if ( FAILED( xImp.Status() ) )
  317. {
  318. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::CoImpersonateClient() failed with 0x%x."), xImp.Status() );
  319. retStatus.SetError( xImp.Status() );
  320. return xImp.Status();
  321. }
  322. BOOL bRet = OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, &xhUserToken);
  323. if(!bRet)
  324. hr = HRESULT_FROM_WIN32(GetLastError());
  325. // call revert
  326. HRESULT hrRev = xImp.Revert();
  327. if (FAILED(hrRev)){
  328. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod:: Failed to revert context with 0x%x"), hrRev);
  329. retStatus.SetError(hrRev);
  330. return hrRev;
  331. }
  332. if (!bRet) {
  333. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Openthreadtoken failed with 0x%x after impersonation"), hr );
  334. retStatus.SetError(hr);
  335. return hr;
  336. }
  337. }
  338. if ( _wcsicmp( (WCHAR *) bstrMethod, L"RsopDeleteSession" ) == 0 )
  339. {
  340. VARIANT vNameSpace;
  341. hr = pInParams->Get( m_xbstrNameSpace, 0, &vNameSpace, NULL, NULL);
  342. if ( FAILED(hr) )
  343. {
  344. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get machine name failed with 0x%x."), hr );
  345. retStatus.SetError( hr );
  346. return hr;
  347. }
  348. XVariant xvNameSpace( &vNameSpace );
  349. if ( vNameSpace.vt == VT_NULL )
  350. hr = E_INVALIDARG;
  351. else {
  352. hr = ProviderDeleteRsopNameSpace( xLocator,
  353. vNameSpace.bstrVal,
  354. xhUserToken,
  355. NULL,
  356. SETUP_NS_PM);
  357. }
  358. VARIANT var;
  359. var.vt = VT_I4;
  360. var.lVal = hr;
  361. hr = pOutParams->Put( m_xbstrResult, 0, &var, 0);
  362. if ( FAILED(hr) )
  363. {
  364. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Put result failed with 0x%x"), hr );
  365. retStatus.SetError( hr );
  366. return hr;
  367. }
  368. hr = pResponseHandler->Indicate(1, &pOutParams);
  369. if ( FAILED( hr ) )
  370. {
  371. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod:: Indicate failed with 0x%x"), hr );
  372. retStatus.SetError( hr );
  373. return hr;
  374. }
  375. }
  376. else if ( _wcsicmp( (WCHAR *) bstrMethod, L"RsopCreateSession" ) == 0 )
  377. {
  378. //
  379. // Code for RsopCreateSession method
  380. //
  381. BOOL bMachineData = TRUE;
  382. BOOL bUserData = TRUE;
  383. VARIANT vMachName;
  384. VariantInit( &vMachName );
  385. hr = pInParams->Get( m_xbstrMachName, 0, &vMachName, NULL, NULL);
  386. if ( FAILED(hr) )
  387. {
  388. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get machine name failed with 0x%x."), hr );
  389. retStatus.SetError( hr );
  390. return hr;
  391. }
  392. XVariant xvMachName( &vMachName );
  393. VARIANT vMachSOM;
  394. VariantInit( &vMachSOM );
  395. hr = pInParams->Get( m_xbstrMachSOM, 0, &vMachSOM, NULL, NULL);
  396. if ( FAILED(hr) )
  397. {
  398. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get machine SOM failed with 0x%x."), hr );
  399. retStatus.SetError( hr );
  400. return hr;
  401. }
  402. if ( vMachSOM.vt == VT_EMPTY || vMachSOM.vt == VT_NULL )
  403. {
  404. if ( vMachName.vt == VT_EMPTY || vMachName.vt == VT_NULL )
  405. {
  406. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("ExecAsyncMethod:: Machine name and SOM are NULL."));
  407. bMachineData = FALSE;
  408. }
  409. else
  410. {
  411. DWORD dwMachLength = wcslen( vMachName.bstrVal ) + 2;
  412. XPtrLF<WCHAR> szMachine = LocalAlloc( LPTR, ( dwMachLength ) * sizeof( WCHAR ) );
  413. if ( !szMachine )
  414. {
  415. hr = HRESULT_FROM_WIN32( GetLastError() );
  416. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Not enough memory 0x%x."), hr );
  417. retStatus.SetError( hr );
  418. return hr;
  419. }
  420. hr = StringCchCopy( szMachine, dwMachLength, vMachName.bstrVal );
  421. if(FAILED(hr))
  422. {
  423. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::StringCchCpy failed with 0x%x."), hr );
  424. retStatus.SetError( hr );
  425. return hr;
  426. }
  427. XBStr xbstrSOM = GetSOM( szMachine );
  428. if ( !xbstrSOM )
  429. {
  430. hr = HRESULT_FROM_WIN32( GetLastError() );
  431. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get machine SOM failed with 0x%x."), hr );
  432. retStatus.SetError( hr );
  433. return hr;
  434. }
  435. vMachSOM.vt = VT_BSTR;
  436. vMachSOM.bstrVal = xbstrSOM.Acquire();
  437. }
  438. }
  439. CProgressIndicator Indicator( pResponseHandler,
  440. (lFlags & WBEM_FLAG_SEND_STATUS) != 0 );
  441. Indicator.IncrementBy( 5 );
  442. //
  443. // vMachSOM is going to have at least empty data in it all cases
  444. //
  445. XVariant xvMachSOM( &vMachSOM );
  446. VARIANT vUserName;
  447. VariantInit( &vUserName );
  448. hr = pInParams->Get( m_xbstrUserName, 0, &vUserName, NULL, NULL);
  449. if ( FAILED(hr) )
  450. {
  451. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get user name failed with 0x%x."), hr );
  452. retStatus.SetError( hr );
  453. return hr;
  454. }
  455. XVariant xvUserName( &vUserName );
  456. VARIANT vUserSOM;
  457. VariantInit( &vUserSOM );
  458. hr = pInParams->Get( m_xbstrUserSOM, 0, &vUserSOM, NULL, NULL);
  459. if ( FAILED(hr) )
  460. {
  461. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get user SOM failed with 0x%x."), GetLastError() );
  462. retStatus.SetError( hr );
  463. return hr;
  464. }
  465. if ( vUserSOM.vt == VT_EMPTY || vUserSOM.vt == VT_NULL )
  466. {
  467. if ( vUserName.vt == VT_EMPTY || vUserName.vt == VT_NULL )
  468. {
  469. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("ExecAsyncMethod:: User name and SOM are NULL."));
  470. bUserData = FALSE;
  471. }
  472. else
  473. {
  474. XBStr xbstrSOM = GetSOM( vUserName.bstrVal );
  475. if ( !xbstrSOM )
  476. {
  477. hr = HRESULT_FROM_WIN32( GetLastError() );
  478. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get user SOM failed with 0x%x."), GetLastError() );
  479. retStatus.SetError( hr );
  480. return hr;
  481. }
  482. vUserSOM.vt = VT_BSTR;
  483. vUserSOM.bstrVal = xbstrSOM.Acquire();
  484. }
  485. }
  486. //
  487. // vUserSOM is going to have at least empty data in it all cases
  488. //
  489. XVariant xvUserSOM( &vUserSOM );
  490. //
  491. // Nothing was asked for..
  492. //
  493. if ( (!bMachineData) && (!bUserData) ) {
  494. hr = S_OK;
  495. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod:: User and machine (both name and SOM) are NULL."));
  496. retStatus.SetError( WBEM_E_INVALID_PARAMETER );
  497. return hr;
  498. }
  499. VARIANT vMachGroups;
  500. hr = pInParams->Get( m_xbstrMachGroups, 0, &vMachGroups, NULL, NULL);
  501. if ( FAILED(hr) )
  502. {
  503. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get machine groups failed with 0x%x."), hr );
  504. retStatus.SetError( hr );
  505. return hr;
  506. }
  507. XVariant xvMachGroups( &vMachGroups );
  508. VARIANT vUserGroups;
  509. hr = pInParams->Get( m_xbstrUserGroups, 0, &vUserGroups, NULL, NULL);
  510. if ( FAILED(hr) )
  511. {
  512. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get user groups failed with 0x%x."), hr );
  513. retStatus.SetError( hr );
  514. return hr;
  515. }
  516. XVariant xvUserGroups( &vUserGroups );
  517. VARIANT vSite;
  518. hr = pInParams->Get( m_xbstrSite, 0, &vSite, NULL, NULL);
  519. if ( FAILED(hr) )
  520. {
  521. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get site failed with 0x%x."), hr );
  522. retStatus.SetError( hr );
  523. return hr;
  524. }
  525. XVariant xvSite( &vSite );
  526. //
  527. // Add computer gpo filters
  528. //
  529. VARIANT vComputerGpoFilter;
  530. hr = pInParams->Get( m_xbstrComputerGpoFilter, 0, &vComputerGpoFilter, NULL, NULL);
  531. if ( FAILED(hr) )
  532. {
  533. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get Gpo filter failed with 0x%x."), hr );
  534. retStatus.SetError( hr );
  535. return hr;
  536. }
  537. XVariant xvComputerGpoFilter( &vComputerGpoFilter );
  538. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("ExecAsyncMethod::Gpo filter:Adding Computer filters") );
  539. CGpoFilter computerGpoFilter;
  540. hr = computerGpoFilter.Add( &vComputerGpoFilter );
  541. if ( FAILED(hr) )
  542. {
  543. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Gpo filter:Add failed with 0x%x."), hr );
  544. retStatus.SetError( hr );
  545. return hr;
  546. }
  547. //
  548. // Add user gpo filters
  549. //
  550. VARIANT vUserGpoFilter;
  551. hr = pInParams->Get( m_xbstrUserGpoFilter, 0, &vUserGpoFilter, NULL, NULL);
  552. if ( FAILED(hr) )
  553. {
  554. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get Gpo filter failed with 0x%x."), hr );
  555. retStatus.SetError( hr );
  556. return hr;
  557. }
  558. XVariant xvUserGpoFilter( &vUserGpoFilter );
  559. CGpoFilter userGpoFilter;
  560. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("ExecAsyncMethod::Gpo filter:Adding User filters") );
  561. hr = userGpoFilter.Add( &vUserGpoFilter );
  562. if ( FAILED(hr) )
  563. {
  564. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Gpo filter:Add failed with 0x%x."), hr );
  565. retStatus.SetError( hr );
  566. return hr;
  567. }
  568. VARIANT vFlags;
  569. VariantInit( &vFlags );
  570. hr = pInParams->Get( m_xbstrFlags, 0, &vFlags, NULL, NULL);
  571. if ( FAILED(hr) )
  572. {
  573. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Get dwFlags failed with 0x%x."), hr );
  574. retStatus.SetError( hr );
  575. return hr;
  576. }
  577. DWORD dwFlags = vFlags.vt == VT_EMPTY || vFlags.vt == VT_NULL ? 0 : vFlags.ulVal;
  578. dwFlags &= ~FLAG_INTERNAL_MASK;
  579. if ( dwFlags & FLAG_NO_GPO_FILTER )
  580. {
  581. dwFlags |= FLAG_NO_CSE_INVOKE;
  582. }
  583. //
  584. // do some parameter checks
  585. //
  586. if ((dwFlags & FLAG_LOOPBACK_MERGE) && (dwFlags & FLAG_LOOPBACK_REPLACE)) {
  587. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Loopback merge and replace, both are specified. failing"));
  588. retStatus.SetError( WBEM_E_INVALID_PARAMETER );
  589. return S_OK;
  590. }
  591. if (dwFlags & FLAG_LOOPBACK_MERGE) {
  592. if (!bMachineData || !bUserData) {
  593. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod:: Loopback mode both user AND computer data needs to be specified"));
  594. retStatus.SetError( WBEM_E_INVALID_PARAMETER );
  595. return S_OK;
  596. }
  597. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("ExecAsyncMethod:: Loopback merge mode specified"));
  598. }
  599. if (dwFlags & FLAG_LOOPBACK_REPLACE) {
  600. if (!bMachineData) {
  601. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod:: Loopback mode computer data needs to be specified"));
  602. retStatus.SetError( WBEM_E_INVALID_PARAMETER );
  603. return S_OK;
  604. }
  605. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("ExecAsyncMethod:: Loopback replace mode specified"));
  606. }
  607. //
  608. // Below is a hack...
  609. // In case of replace mode, user som or account doesn't need to be supplied...
  610. // but we need to fool the rest of the code to think that user data is specified
  611. // and desired and we need to access check againt user som alone.
  612. // Copy Mach som to user som
  613. //
  614. if (dwFlags & FLAG_LOOPBACK_REPLACE) {
  615. xvUserSOM = NULL;
  616. // reinit user som
  617. VariantInit( &vUserSOM );
  618. vUserSOM.vt = VT_BSTR;
  619. vUserSOM.bstrVal = SysAllocString(vMachSOM.bstrVal);
  620. if (!vUserSOM.bstrVal) {
  621. hr = E_OUTOFMEMORY;
  622. retStatus.SetError( hr );
  623. return hr;
  624. }
  625. xvUserSOM = &vUserSOM;
  626. }
  627. //
  628. // We can dump out all the input parameters here later on.
  629. // Currently dumping only remote Computer.
  630. //
  631. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("ExecAsyncMethod::---------------RsopCreateSession::Input Parameters--------------------"));
  632. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("ExecAsyncMethod::dwFlags = 0x%x"), dwFlags);
  633. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("ExecAsyncMethod::---------------RsopCreateSession::Input Parameters--------------------"));
  634. // by this point we have finished all param checks. All future errors needs to be
  635. // returned in the method specific hResult
  636. //
  637. // Check for access before entering policy critical section
  638. //
  639. DWORD dwExtendedInfo = 0;
  640. hr = AuthenticateUser( xhUserToken,
  641. vMachSOM.vt != VT_NULL ? vMachSOM.bstrVal : 0,
  642. vUserSOM.vt != VT_NULL ? vUserSOM.bstrVal : 0,
  643. FALSE,
  644. &dwExtendedInfo );
  645. if ( FAILED( hr ) )
  646. {
  647. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod:: AuthenticateUser() failed with 0x%x."), hr );
  648. }
  649. //
  650. // Synchronize with garbage collection thread in userenv.dll by acquiring Group Policy critical section
  651. //
  652. if (SUCCEEDED(hr)) {
  653. XCriticalPolicySection criticalPolicySectionUSER( EnterCriticalPolicySection(FALSE) );
  654. if( !criticalPolicySectionUSER )
  655. {
  656. hr = HRESULT_FROM_WIN32( GetLastError() );
  657. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecMethodAsync::EnterCriticalPolicySection (user) failed with 0x%x"), hr );
  658. retStatus.SetError( hr );
  659. return hr;
  660. }
  661. XCriticalPolicySection criticalPolicySectionMACHINE( EnterCriticalPolicySection(TRUE) );
  662. if(!criticalPolicySectionMACHINE)
  663. {
  664. hr = HRESULT_FROM_WIN32( GetLastError() );
  665. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecMethodAsync::EnterCriticalPolicySection (machine) failed with 0x%x"), hr );
  666. retStatus.SetError( hr );
  667. return hr;
  668. }
  669. XPtrLF<WCHAR> xwszNameSpace;
  670. XPtrLF<SID> xSid = GetUserSid(xhUserToken);
  671. if (!xSid) {
  672. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::GetUserSid failed."));
  673. return HRESULT_FROM_WIN32(E_FAIL);
  674. }
  675. hr = SetupNewNameSpace( &xwszNameSpace,
  676. 0, // namespace on this machine
  677. NULL, xSid,
  678. xLocator, SETUP_NS_PM, NULL);
  679. if ( FAILED( hr ) )
  680. {
  681. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::SetupNewNameSpace failed with 0x%x"), hr );
  682. }
  683. else
  684. {
  685. BOOL bOk;
  686. bOk = GenerateRsopPolicy( dwFlags,
  687. vMachName.vt == VT_NULL ? 0 : vMachName.bstrVal,
  688. vMachSOM.vt == VT_NULL ? 0 : vMachSOM.bstrVal,
  689. vMachGroups.vt == VT_NULL ? 0 : vMachGroups.parray,
  690. vUserName.vt == VT_NULL ? 0 : vUserName.bstrVal,
  691. vUserSOM.vt == VT_NULL ? 0 : vUserSOM.bstrVal,
  692. vUserGroups.vt == VT_NULL ? 0 : vUserGroups.parray,
  693. vSite.vt == VT_NULL ? 0 : vSite.bstrVal,
  694. xwszNameSpace,
  695. &Indicator,
  696. &computerGpoFilter,
  697. &userGpoFilter );
  698. if ( !bOk )
  699. {
  700. hr = HRESULT_FROM_WIN32( GetLastError() );
  701. if ( SUCCEEDED( hr ) )
  702. {
  703. hr = E_FAIL;
  704. }
  705. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::GenerateRsopPolicy failed with 0x%x"), hr );
  706. HRESULT hrDel = DeleteRsopNameSpace( xwszNameSpace, xLocator );
  707. if ( FAILED( hrDel ) )
  708. {
  709. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::DeleteRsopNameSpace failed with 0x%x"), hrDel );
  710. }
  711. }
  712. else
  713. {
  714. XBStr xbstrNS( xwszNameSpace );
  715. if ( !xbstrNS )
  716. {
  717. hr = HRESULT_FROM_WIN32( GetLastError() );
  718. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Memory allocate failed") );
  719. retStatus.SetError( hr );
  720. return hr;
  721. }
  722. VARIANT var;
  723. var.vt = VT_BSTR;
  724. var.bstrVal = xbstrNS;
  725. hr = pOutParams->Put( m_xbstrNameSpace, 0, &var, 0);
  726. if ( FAILED(hr) )
  727. {
  728. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Put namespace failed with 0x%x"), hr );
  729. retStatus.SetError( hr );
  730. return hr;
  731. }
  732. }
  733. }
  734. }
  735. VARIANT var;
  736. var.vt = VT_I4;
  737. var.lVal = hr;
  738. hr = pOutParams->Put( m_xbstrResult, 0, &var, 0);
  739. if ( FAILED( hr ) )
  740. {
  741. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Put result failed with 0x%x"), hr );
  742. retStatus.SetError( hr );
  743. return hr;
  744. }
  745. var.lVal = dwExtendedInfo;
  746. hr = pOutParams->Put( m_xbstrExtendedInfo, 0, &var, 0);
  747. if ( FAILED( hr ) )
  748. {
  749. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Put result failed with 0x%x"), hr );
  750. retStatus.SetError( hr );
  751. return hr;
  752. }
  753. //
  754. // change all returns to retStatus = error_code; return S_OK;
  755. //
  756. hr = Indicator.SetComplete();
  757. if ( FAILED( hr ) )
  758. {
  759. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod::Increment() failed with 0x%x"), hr );
  760. retStatus.SetError( hr );
  761. return hr;
  762. }
  763. hr = pResponseHandler->Indicate(1, &pOutParams);
  764. if ( FAILED( hr ) )
  765. {
  766. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("ExecAsyncMethod:: Indicate failed with 0x%x"), hr );
  767. retStatus.SetError( hr );
  768. return hr;
  769. }
  770. }
  771. else
  772. {
  773. hr = E_INVALIDARG;
  774. }
  775. return hr;
  776. }