Source code of Windows XP (NT5)
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.

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