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.

735 lines
16 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000-2001 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // CClusCfgCredentials.cpp
  7. //
  8. // Description:
  9. // This file contains the definition of the CClusCfgCredentials
  10. // class.
  11. //
  12. // The class CClusCfgCredentials is the representation of
  13. // account credentials. It implements the IClusCfgCredentials interface.
  14. //
  15. // Documentation:
  16. //
  17. // Header File:
  18. // CClusCfgCredentials.h
  19. //
  20. // Maintained By:
  21. // Galen Barbee (GalenB) 17-May-2000
  22. //
  23. //////////////////////////////////////////////////////////////////////////////
  24. //////////////////////////////////////////////////////////////////////////////
  25. // Include Files
  26. //////////////////////////////////////////////////////////////////////////////
  27. #include "pch.h"
  28. #include "CClusCfgCredentials.h"
  29. //////////////////////////////////////////////////////////////////////////////
  30. // Constant Definitions
  31. //////////////////////////////////////////////////////////////////////////////
  32. DEFINE_THISCLASS( "CClusCfgCredentials" );
  33. //*************************************************************************//
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CClusCfgCredentials class
  36. /////////////////////////////////////////////////////////////////////////////
  37. //////////////////////////////////////////////////////////////////////////////
  38. //++
  39. //
  40. // CClusCfgCredentials::S_HrCreateInstance()
  41. //
  42. // Description:
  43. // Create a CClusCfgCredentials instance.
  44. //
  45. // Arguments:
  46. // None.
  47. //
  48. // Return Values:
  49. // Pointer to CClusCfgCredentials instance.
  50. //
  51. //--
  52. //////////////////////////////////////////////////////////////////////////////
  53. HRESULT
  54. CClusCfgCredentials::S_HrCreateInstance( IUnknown ** ppunkOut )
  55. {
  56. TraceFunc( "" );
  57. HRESULT hr;
  58. CClusCfgCredentials * lpccs = NULL;
  59. if ( ppunkOut == NULL )
  60. {
  61. hr = THR( E_POINTER );
  62. goto Exit;
  63. } // if:
  64. lpccs = new CClusCfgCredentials();
  65. if ( lpccs == NULL )
  66. {
  67. hr = THR( E_OUTOFMEMORY );
  68. goto Exit;
  69. } // if: error allocating object
  70. hr = THR( lpccs->HrInit() );
  71. if ( FAILED( hr ) )
  72. {
  73. goto Exit;
  74. } // if: HrInit() failed
  75. hr = THR( lpccs->TypeSafeQI( IUnknown, ppunkOut ) );
  76. Exit:
  77. if ( FAILED( hr ) )
  78. {
  79. LogMsg( L"Server: CClusCfgCredentials::S_HrCreateInstance() failed. (hr = %#08x)", hr );
  80. } // if:
  81. if ( lpccs != NULL )
  82. {
  83. lpccs->Release();
  84. } // if:
  85. HRETURN( hr );
  86. } //*** CClusCfgCredentials::S_HrCreateInstance()
  87. //////////////////////////////////////////////////////////////////////////////
  88. //++
  89. //
  90. // CClusCfgCredentials::CClusCfgCredentials()
  91. //
  92. // Description:
  93. // Constructor of the CClusCfgCredentials class. This initializes
  94. // the m_cRef variable to 1 instead of 0 to account of possible
  95. // QueryInterface failure in DllGetClassObject.
  96. //
  97. // Arguments:
  98. // None.
  99. //
  100. // Return Value:
  101. // None.
  102. //
  103. // Remarks:
  104. // None.
  105. //
  106. //--
  107. //////////////////////////////////////////////////////////////////////////////
  108. CClusCfgCredentials::CClusCfgCredentials( void )
  109. : m_cRef( 1 )
  110. , m_lcid( LOCALE_NEUTRAL )
  111. {
  112. TraceFunc( "" );
  113. // Increment the count of components in memory so the DLL hosting this
  114. // object cannot be unloaded.
  115. InterlockedIncrement( &g_cObjects );
  116. Assert( m_aiInfo.bstrName == NULL );
  117. Assert( m_aiInfo.bstrPassword == NULL );
  118. Assert( m_aiInfo.bstrDomain == NULL );
  119. Assert( m_picccCallback == NULL );
  120. TraceFuncExit();
  121. } //*** CClusCfgCredentials::CClusCfgCredentials
  122. //////////////////////////////////////////////////////////////////////////////
  123. //++
  124. //
  125. // CClusCfgCredentials::~CClusCfgCredentials()
  126. //
  127. // Description:
  128. // Desstructor of the CClusCfgCredentials class.
  129. //
  130. // Arguments:
  131. // None.
  132. //
  133. // Return Value:
  134. // None.
  135. //
  136. // Remarks:
  137. // None.
  138. //
  139. //--
  140. //////////////////////////////////////////////////////////////////////////////
  141. CClusCfgCredentials::~CClusCfgCredentials( void )
  142. {
  143. TraceFunc( "" );
  144. // There's going to be one less component in memory. Decrement component count.
  145. InterlockedDecrement( &g_cObjects );
  146. if ( m_picccCallback != NULL )
  147. {
  148. m_picccCallback->Release();
  149. } // if:
  150. TraceSysFreeString( m_aiInfo.bstrName );
  151. TraceSysFreeString( m_aiInfo.bstrPassword );
  152. TraceSysFreeString( m_aiInfo.bstrDomain );
  153. TraceFuncExit();
  154. } //*** CClusCfgCredentials::~CClusCfgCredentials
  155. //*************************************************************************//
  156. /////////////////////////////////////////////////////////////////////////////
  157. // CClusterConfiguration -- IUknkown interface.
  158. /////////////////////////////////////////////////////////////////////////////
  159. //////////////////////////////////////////////////////////////////////////////
  160. //++
  161. //
  162. // STDMETHODIMP_( ULONG )
  163. // CClusCfgCredentials:: [IUNKNOWN] AddRef()
  164. //
  165. // Description:
  166. // Increment the reference count of this object by one.
  167. //
  168. // Arguments:
  169. // None.
  170. //
  171. // Return Value:
  172. // The new reference count.
  173. //
  174. // Remarks:
  175. // None.
  176. //
  177. //--
  178. //////////////////////////////////////////////////////////////////////////////
  179. STDMETHODIMP_( ULONG )
  180. CClusCfgCredentials::AddRef( void )
  181. {
  182. TraceFunc( "[IUnknown]" );
  183. InterlockedIncrement( & m_cRef );
  184. RETURN( m_cRef );
  185. } //*** CClusCfgCredentials::AddRef()
  186. //////////////////////////////////////////////////////////////////////////////
  187. //++
  188. //
  189. // STDMETHODIMP_( ULONG )
  190. // CClusCfgCredentials:: [IUNKNOWN] Release()
  191. //
  192. // Description:
  193. // Decrement the reference count of this object by one.
  194. //
  195. // Arguments:
  196. // None.
  197. //
  198. // Return Value:
  199. // The new reference count.
  200. //
  201. // Remarks:
  202. // None.
  203. //
  204. //--
  205. //////////////////////////////////////////////////////////////////////////////
  206. STDMETHODIMP_( ULONG )
  207. CClusCfgCredentials::Release( void )
  208. {
  209. TraceFunc( "[IUnknown]" );
  210. InterlockedDecrement( &m_cRef );
  211. if ( m_cRef > 0 )
  212. {
  213. RETURN( m_cRef );
  214. } // if: reference count greater than zero
  215. TraceDo( delete this );
  216. RETURN( 0 );
  217. } //*** CClusCfgCredentials::Release()
  218. //////////////////////////////////////////////////////////////////////////////
  219. //++
  220. //
  221. // CClusCfgCredentials:: [INKNOWN] QueryInterface()
  222. //
  223. // Description:
  224. // Query this object for the passed in interface.
  225. //
  226. // Arguments:
  227. // IN REFIID riid,
  228. // Id of interface requested.
  229. //
  230. // OUT void ** ppv
  231. // Pointer to the requested interface.
  232. //
  233. // Return Value:
  234. // S_OK
  235. // If the interface is available on this object.
  236. //
  237. // E_NOINTERFACE
  238. // If the interface is not available.
  239. //
  240. // Remarks:
  241. // None.
  242. //
  243. //--
  244. //////////////////////////////////////////////////////////////////////////////
  245. STDMETHODIMP
  246. CClusCfgCredentials::QueryInterface( REFIID riid, void ** ppv )
  247. {
  248. TraceQIFunc( riid, ppv );
  249. HRESULT hr = E_NOINTERFACE;
  250. if ( IsEqualIID( riid, IID_IUnknown ) )
  251. {
  252. *ppv = static_cast< IClusCfgCredentials * >( this );
  253. hr = S_OK;
  254. } // if: IUnknown
  255. else if ( IsEqualIID( riid, IID_IClusCfgCredentials ) )
  256. {
  257. *ppv = TraceInterface( __THISCLASS__, IClusCfgCredentials, this, 0 );
  258. hr = S_OK;
  259. } // else if:
  260. else if ( IsEqualIID( riid, IID_IClusCfgInitialize ) )
  261. {
  262. *ppv = TraceInterface( __THISCLASS__, IClusCfgInitialize, this, 0 );
  263. hr = S_OK;
  264. } // else if:
  265. else if ( IsEqualIID( riid, IID_IClusCfgSetCredentials ) )
  266. {
  267. *ppv = TraceInterface( __THISCLASS__, IClusCfgSetCredentials, this, 0 );
  268. hr = S_OK;
  269. } // else if:
  270. if ( SUCCEEDED( hr ) )
  271. {
  272. ((IUnknown *) *ppv)->AddRef( );
  273. } // if: success
  274. QIRETURN_IGNORESTDMARSHALLING1( hr, riid, IID_IClusCfgWbemServices );
  275. } //*** CClusCfgCredentials::QueryInterface()
  276. //*************************************************************************//
  277. /////////////////////////////////////////////////////////////////////////////
  278. // CClusCfgCredentials -- IClusCfgInitialze interface.
  279. /////////////////////////////////////////////////////////////////////////////
  280. //////////////////////////////////////////////////////////////////////////////
  281. //++
  282. //
  283. // CClusCfgCredentials::Initialize()
  284. //
  285. // Description:
  286. // Initialize this component.
  287. //
  288. // Arguments:
  289. // IN IUknown * punkCallbackIn
  290. //
  291. // IN LCID lcidIn
  292. //
  293. // Return Value:
  294. // S_OK
  295. // Success
  296. //
  297. // Remarks:
  298. // None.
  299. //
  300. //--
  301. //////////////////////////////////////////////////////////////////////////////
  302. STDMETHODIMP
  303. CClusCfgCredentials::Initialize(
  304. IUnknown * punkCallbackIn,
  305. LCID lcidIn
  306. )
  307. {
  308. TraceFunc( "[IClusCfgInitialize]" );
  309. HRESULT hr = S_OK;
  310. m_lcid = lcidIn;
  311. Assert( m_picccCallback == NULL );
  312. if ( punkCallbackIn != NULL )
  313. {
  314. hr = THR( punkCallbackIn->TypeSafeQI( IClusCfgCallback, &m_picccCallback ) );
  315. } // if:
  316. HRETURN( hr );
  317. } //*** CClusCfgCredentials::Initialize()
  318. //*************************************************************************//
  319. /////////////////////////////////////////////////////////////////////////////
  320. // CClusCfgCredentials -- IClusCfgCredentials interface.
  321. /////////////////////////////////////////////////////////////////////////////
  322. //////////////////////////////////////////////////////////////////////////////
  323. //++
  324. //
  325. // CClusCfgCredentials::GetCredentials()
  326. //
  327. // Description:
  328. //
  329. // Arguments:
  330. //
  331. // Return Value:
  332. //
  333. // Remarks:
  334. // None.
  335. //
  336. //--
  337. //////////////////////////////////////////////////////////////////////////////
  338. STDMETHODIMP
  339. CClusCfgCredentials::GetCredentials(
  340. BSTR * pbstrNameOut,
  341. BSTR * pbstrDomainOut,
  342. BSTR * pbstrPasswordOut
  343. )
  344. {
  345. TraceFunc( "[IClusCfgCredentials]" );
  346. HRESULT hr;
  347. //
  348. // Only return the requested parameters. If an argument is NULL,
  349. // that means the caller did not want to retrieve that piece of
  350. // information.
  351. //
  352. if ( pbstrNameOut != NULL )
  353. {
  354. *pbstrNameOut = SysAllocString( m_aiInfo.bstrName );
  355. if ( ( *pbstrNameOut == NULL ) && ( m_aiInfo.bstrName != NULL ) )
  356. {
  357. goto OutOfMemory;
  358. } // if:
  359. } // if:
  360. if ( pbstrPasswordOut != NULL )
  361. {
  362. *pbstrPasswordOut = SysAllocString( m_aiInfo.bstrPassword );
  363. if ( ( *pbstrPasswordOut == NULL ) && ( m_aiInfo.bstrPassword != NULL ) )
  364. {
  365. goto OutOfMemory;
  366. } // if:
  367. } // if:
  368. if ( pbstrDomainOut != NULL )
  369. {
  370. *pbstrDomainOut = SysAllocString( m_aiInfo.bstrDomain );
  371. if ( ( *pbstrDomainOut == NULL ) && ( m_aiInfo.bstrDomain != NULL ) )
  372. {
  373. goto OutOfMemory;
  374. } // if:
  375. } // if:
  376. hr = S_OK;
  377. goto Exit;
  378. OutOfMemory:
  379. hr = THR( E_OUTOFMEMORY );
  380. Exit:
  381. HRETURN( hr );
  382. } //*** CClusCfgCredentials::GetCredentials()
  383. //////////////////////////////////////////////////////////////////////////////
  384. //++
  385. //
  386. // CClusCfgCredentials::SetCredentials()
  387. //
  388. // Description:
  389. //
  390. // Arguments:
  391. //
  392. // Return Value:
  393. //
  394. // Remarks:
  395. // None.
  396. //
  397. //--
  398. //////////////////////////////////////////////////////////////////////////////
  399. STDMETHODIMP
  400. CClusCfgCredentials::SetCredentials(
  401. LPCWSTR pcszNameIn,
  402. LPCWSTR pcszDomainIn,
  403. LPCWSTR pcszPasswordIn
  404. )
  405. {
  406. TraceFunc( "[IClusCfgCredentials]" );
  407. HRESULT hr = S_OK;
  408. // HANDLE hToken = NULL;
  409. BSTR bstrNewName = NULL;
  410. BSTR bstrNewDomain = NULL;
  411. BSTR bstrNewPassword = NULL;
  412. //
  413. // Logon the passed in user to ensure that it is valid.
  414. //
  415. /*
  416. //
  417. // KB: 04 May 2000 GalenB
  418. //
  419. // New for Whistler... You no longer have to grant your processes TCB privilege. But it doesn't seem to work!
  420. //
  421. if ( !LogonUser( pcszNameIn, pcszDomainIn, pcszPasswordIn, LOGON32_LOGON_BATCH, LOGON32_PROVIDER_DEFAULT, &hToken ) )
  422. {
  423. DWORD sc;
  424. sc = TW32( GetLastError() );
  425. hr = HRESULT_FROM_WIN32( );
  426. goto CleanUp;
  427. } // if:
  428. */
  429. if ( pcszNameIn != NULL )
  430. {
  431. bstrNewName = TraceSysAllocString( pcszNameIn );
  432. if ( bstrNewName == NULL )
  433. {
  434. goto OutOfMemory;
  435. } // if:
  436. } // if:
  437. if ( pcszPasswordIn != NULL )
  438. {
  439. bstrNewPassword = TraceSysAllocString( pcszPasswordIn );
  440. if ( bstrNewPassword == NULL )
  441. {
  442. goto OutOfMemory;
  443. } // if:
  444. } // if:
  445. if ( pcszDomainIn != NULL )
  446. {
  447. bstrNewDomain = TraceSysAllocString( pcszDomainIn );
  448. if ( bstrNewDomain == NULL )
  449. {
  450. goto OutOfMemory;
  451. } // if:
  452. } // if:
  453. if ( bstrNewName != NULL )
  454. {
  455. TraceSysFreeString( m_aiInfo.bstrName );
  456. } // if:
  457. if ( bstrNewPassword != NULL )
  458. {
  459. TraceSysFreeString( m_aiInfo.bstrPassword );
  460. } // if:
  461. if ( bstrNewDomain != NULL )
  462. {
  463. TraceSysFreeString( m_aiInfo.bstrDomain );
  464. } // if:
  465. m_aiInfo.bstrName = bstrNewName;
  466. m_aiInfo.bstrPassword = bstrNewPassword;
  467. m_aiInfo.bstrDomain = bstrNewDomain;
  468. goto CleanUp;
  469. OutOfMemory:
  470. hr = THR( E_OUTOFMEMORY );
  471. CleanUp:
  472. // if ( hToken != NULL )
  473. // {
  474. // CloseHandle( hToken );
  475. // } // if:
  476. HRETURN( hr );
  477. } //*** CClusCfgCredentials::SetCredentials()
  478. //*************************************************************************//
  479. /////////////////////////////////////////////////////////////////////////////
  480. // CClusCfgCredentials -- IClusCfgSetCredentials interface.
  481. /////////////////////////////////////////////////////////////////////////////
  482. //////////////////////////////////////////////////////////////////////////////
  483. //++
  484. //
  485. // CClusCfgCredentials::SetDomainCredentials()
  486. //
  487. // Description:
  488. //
  489. // Arguments:
  490. //
  491. // Return Value:
  492. //
  493. // Remarks:
  494. // None.
  495. //
  496. //--
  497. //////////////////////////////////////////////////////////////////////////////
  498. STDMETHODIMP
  499. CClusCfgCredentials::SetDomainCredentials( LPCWSTR pcszCredentials )
  500. {
  501. TraceFunc( "[IClusSetCfgCredentials]" );
  502. HRESULT hr = S_OK;
  503. WCHAR * psz;
  504. if ( pcszCredentials == NULL )
  505. {
  506. hr = THR( E_POINTER );
  507. LogMsg( L"Server: CClusCfgCredentials::SetDomainCredentials() was given a NULL pointer argument." );
  508. goto Exit;
  509. } // if:
  510. //
  511. // Are the credentials in domain\user format?
  512. //
  513. psz = wcschr( pcszCredentials, L'\\' );
  514. if ( psz != NULL )
  515. {
  516. *psz = L'\0';
  517. psz++;
  518. m_aiInfo.bstrDomain = TraceSysAllocString( pcszCredentials );
  519. if ( m_aiInfo.bstrDomain == NULL )
  520. {
  521. goto OutOfMemory;
  522. } // if:
  523. m_aiInfo.bstrName = TraceSysAllocString( psz );
  524. if ( m_aiInfo.bstrName == NULL )
  525. {
  526. goto OutOfMemory;
  527. } // if:
  528. goto Exit;
  529. } // if:
  530. //
  531. // Are the credentials in user@domain format?
  532. //
  533. psz = wcschr( pcszCredentials, L'@' );
  534. if ( psz != NULL )
  535. {
  536. *psz = L'\0';
  537. psz++;
  538. m_aiInfo.bstrName = TraceSysAllocString( pcszCredentials );
  539. if ( m_aiInfo.bstrName == NULL )
  540. {
  541. goto OutOfMemory;
  542. } // if:
  543. m_aiInfo.bstrDomain = TraceSysAllocString( psz );
  544. if ( m_aiInfo.bstrDomain == NULL )
  545. {
  546. goto OutOfMemory;
  547. } // if:
  548. goto Exit;
  549. } // if:
  550. //
  551. // Must be local, simply rememeber then as the user and get the machine name for the domain.
  552. //
  553. m_aiInfo.bstrName = TraceSysAllocString( pcszCredentials );
  554. if ( m_aiInfo.bstrName == NULL )
  555. {
  556. goto OutOfMemory;
  557. } // if:
  558. hr = THR( HrGetComputerName( ComputerNameDnsFullyQualified, &m_aiInfo.bstrDomain ) );
  559. goto Exit;
  560. OutOfMemory:
  561. hr = THR( E_OUTOFMEMORY );
  562. Exit:
  563. HRETURN( hr );
  564. } //*** CClusCfgCredentials::SetDomainCredentials()
  565. //*************************************************************************//
  566. /////////////////////////////////////////////////////////////////////////////
  567. // CClusCfgCredentials class -- Private Methods.
  568. /////////////////////////////////////////////////////////////////////////////
  569. //////////////////////////////////////////////////////////////////////////////
  570. //++
  571. //
  572. // CClusCfgCredentials::HrInit()
  573. //
  574. // Description:
  575. // Initialize this component.
  576. //
  577. // Arguments:
  578. // None.
  579. //
  580. // Return Value:
  581. //
  582. //
  583. // Remarks:
  584. // None.
  585. //
  586. //--
  587. //////////////////////////////////////////////////////////////////////////////
  588. HRESULT
  589. CClusCfgCredentials::HrInit( void )
  590. {
  591. TraceFunc( "" );
  592. HRESULT hr = S_OK;
  593. HRETURN( hr );
  594. } //*** CClusCfgCredentials::HrInit()