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.

614 lines
14 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000-2001 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // TaskGetDomains.cpp
  7. //
  8. // Description:
  9. // Get DNS/NetBIOS Domain Names for the list of domains.
  10. //
  11. // Documentation:
  12. // Yes.
  13. //
  14. // Maintained By:
  15. // Galen Barbee (GalenB) 22-NOV-1999
  16. //
  17. //////////////////////////////////////////////////////////////////////////////
  18. #include "Pch.h"
  19. #include "ClusCfgClient.h"
  20. #include "TaskGetDomains.h"
  21. // ADSI support, to get domain names
  22. #include <Lm.h>
  23. #include <Dsgetdc.h>
  24. DEFINE_THISCLASS("CTaskGetDomains")
  25. // ************************************************************************
  26. //
  27. // Constructor / Destructor
  28. //
  29. // ************************************************************************
  30. //////////////////////////////////////////////////////////////////////////////
  31. //
  32. // HRESULT
  33. // CTaskGetDomains::S_HrCreateInstance(
  34. // IUnknown ** ppunkOut
  35. // )
  36. //
  37. //////////////////////////////////////////////////////////////////////////////
  38. HRESULT
  39. CTaskGetDomains::S_HrCreateInstance(
  40. IUnknown ** ppunkOut
  41. )
  42. {
  43. TraceFunc( "" );
  44. HRESULT hr = S_OK;
  45. CTaskGetDomains * ptgd = NULL;
  46. Assert( ppunkOut != NULL );
  47. if ( ppunkOut == NULL )
  48. {
  49. hr = THR( E_POINTER );
  50. goto Cleanup;
  51. }
  52. ptgd = new CTaskGetDomains;
  53. if ( ptgd == NULL )
  54. {
  55. hr = THR( E_OUTOFMEMORY );
  56. goto Cleanup;
  57. }
  58. hr = THR( ptgd->HrInit() );
  59. if ( FAILED( hr ) )
  60. {
  61. goto Cleanup;
  62. }
  63. hr = THR( ptgd->TypeSafeQI( IUnknown, ppunkOut ) );
  64. if ( FAILED( hr ) )
  65. {
  66. goto Cleanup;
  67. }
  68. //TraceMoveToMemoryList( *ppunkOut, g_GlobalMemoryList );
  69. Cleanup:
  70. if ( ptgd != NULL )
  71. {
  72. ptgd->Release();
  73. }
  74. HRETURN( hr );
  75. } //*** CTaskGetDomains::S_HrCreateInstance
  76. //////////////////////////////////////////////////////////////////////////////
  77. //
  78. // CTaskGetDomains::CTaskGetDomains
  79. //
  80. //////////////////////////////////////////////////////////////////////////////
  81. CTaskGetDomains::CTaskGetDomains( void )
  82. : m_cRef( 1 )
  83. {
  84. TraceFunc( "" );
  85. InterlockedIncrement( &g_cObjects );
  86. TraceFuncExit();
  87. } //*** CTaskGetDomains::CTaskGetDomains
  88. //////////////////////////////////////////////////////////////////////////////
  89. //
  90. // STDMETHODIMP
  91. // CTaskGetDomains::HrInit
  92. //
  93. //////////////////////////////////////////////////////////////////////////////
  94. STDMETHODIMP
  95. CTaskGetDomains::HrInit( void )
  96. {
  97. TraceFunc( "" );
  98. HRESULT hr = S_OK;
  99. // IUnknown stuff
  100. Assert( m_cRef == 1 );
  101. // ITaskGetDomains
  102. Assert( m_pStream == NULL );
  103. Assert( m_ptgdcb == NULL );
  104. if ( InitializeCriticalSectionAndSpinCount( &m_csCallback, RECOMMENDED_SPIN_COUNT ) == 0 )
  105. {
  106. DWORD scLastError = TW32( GetLastError() );
  107. hr = HRESULT_FROM_WIN32( scLastError );
  108. }
  109. HRETURN( hr );
  110. } //*** CTaskGetDomains::HrInit
  111. //////////////////////////////////////////////////////////////////////////////
  112. //
  113. // CTaskGetDomains::~CTaskGetDomains
  114. //
  115. //////////////////////////////////////////////////////////////////////////////
  116. CTaskGetDomains::~CTaskGetDomains( void )
  117. {
  118. TraceFunc( "" );
  119. if ( m_pStream != NULL)
  120. {
  121. m_pStream->Release();
  122. }
  123. if ( m_ptgdcb != NULL )
  124. {
  125. m_ptgdcb->Release();
  126. }
  127. DeleteCriticalSection( &m_csCallback );
  128. InterlockedDecrement( &g_cObjects );
  129. TraceFuncExit();
  130. } //*** CTaskGetDomains::~CTaskGetDomains
  131. // ************************************************************************
  132. //
  133. // IUnknown
  134. //
  135. // ************************************************************************
  136. //////////////////////////////////////////////////////////////////////////////
  137. //++
  138. //
  139. // CTaskGetDomains::QueryInterface
  140. //
  141. // Description:
  142. // Query this object for the passed in interface.
  143. //
  144. // Arguments:
  145. // riidIn
  146. // Id of interface requested.
  147. //
  148. // ppvOut
  149. // Pointer to the requested interface.
  150. //
  151. // Return Value:
  152. // S_OK
  153. // If the interface is available on this object.
  154. //
  155. // E_NOINTERFACE
  156. // If the interface is not available.
  157. //
  158. // E_POINTER
  159. // ppvOut was NULL.
  160. //
  161. // Remarks:
  162. // None.
  163. //
  164. //--
  165. //////////////////////////////////////////////////////////////////////////////
  166. STDMETHODIMP
  167. CTaskGetDomains::QueryInterface(
  168. REFIID riidIn
  169. , LPVOID * ppvOut
  170. )
  171. {
  172. TraceQIFunc( riidIn, ppvOut );
  173. HRESULT hr = S_OK;
  174. //
  175. // Validate arguments.
  176. //
  177. Assert( ppvOut != NULL );
  178. if ( ppvOut == NULL )
  179. {
  180. hr = THR( E_POINTER );
  181. goto Cleanup;
  182. }
  183. //
  184. // Handle known interfaces.
  185. //
  186. if ( IsEqualIID( riidIn, IID_IUnknown ) )
  187. {
  188. *ppvOut = static_cast< ITaskGetDomains * >( this );
  189. } // if: IUnknown
  190. else if ( IsEqualIID( riidIn, IID_ITaskGetDomains ) )
  191. {
  192. *ppvOut = TraceInterface( __THISCLASS__, ITaskGetDomains, this, 0 );
  193. } // else if: ITaskGetDomains
  194. else if ( IsEqualIID( riidIn, IID_IDoTask ) )
  195. {
  196. *ppvOut = TraceInterface( __THISCLASS__, IDoTask, this, 0 );
  197. } // else if: IDoTask
  198. else
  199. {
  200. *ppvOut = NULL;
  201. hr = E_NOINTERFACE;
  202. }
  203. //
  204. // Add a reference to the interface if successful.
  205. //
  206. if ( SUCCEEDED( hr ) )
  207. {
  208. ( (IUnknown *) *ppvOut )->AddRef();
  209. } // if: success
  210. Cleanup:
  211. QIRETURN_IGNORESTDMARSHALLING( hr, riidIn );
  212. } //*** CTaskGetDomains::QueryInterface
  213. //////////////////////////////////////////////////////////////////////////////
  214. //
  215. // STDMETHODIMP_(ULONG)
  216. // CTaskGetDomains::AddRef
  217. //
  218. //////////////////////////////////////////////////////////////////////////////
  219. STDMETHODIMP_( ULONG )
  220. CTaskGetDomains::AddRef( void )
  221. {
  222. TraceFunc( "[IUnknown]" );
  223. InterlockedIncrement( &m_cRef );
  224. CRETURN( m_cRef );
  225. } //*** CTaskGetDomains::AddRef
  226. //////////////////////////////////////////////////////////////////////////////
  227. //
  228. // STDMETHODIMP_(ULONG)
  229. // CTaskGetDomains::Release
  230. //
  231. //////////////////////////////////////////////////////////////////////////////
  232. STDMETHODIMP_( ULONG )
  233. CTaskGetDomains::Release( void )
  234. {
  235. TraceFunc( "[IUnknown]" );
  236. LONG cRef;
  237. cRef = InterlockedDecrement( &m_cRef );
  238. if ( cRef == 0 )
  239. {
  240. TraceDo( delete this );
  241. }
  242. CRETURN( cRef );
  243. } //*** CTaskGetDomains::Release
  244. // ************************************************************************
  245. //
  246. // IDoTask / ITaskGetDomains
  247. //
  248. // ************************************************************************
  249. //////////////////////////////////////////////////////////////////////////////
  250. //
  251. // STDMETHODIMP
  252. // CTaskGetDomains::BeginTask(void);
  253. //
  254. //////////////////////////////////////////////////////////////////////////////
  255. STDMETHODIMP
  256. CTaskGetDomains::BeginTask( void )
  257. {
  258. TraceFunc( "[IDoTask]" );
  259. HRESULT hr;
  260. ULONG ulLen;
  261. DWORD dwRes;
  262. ULONG idx;
  263. PDS_DOMAIN_TRUSTS paDomains = NULL;
  264. hr = THR( HrUnMarshallCallback() );
  265. if ( FAILED( hr ) )
  266. {
  267. goto Cleanup;
  268. }
  269. //
  270. // Enumerate the list of Domains
  271. //
  272. dwRes = TW32( DsEnumerateDomainTrusts( NULL,
  273. DS_DOMAIN_VALID_FLAGS,
  274. &paDomains,
  275. &ulLen
  276. ) );
  277. //
  278. // Might return ERROR_NOT_SUPPORTED if the DC is pre-W2k
  279. // In that case, retry in compatible mode
  280. //
  281. if ( dwRes == ERROR_NOT_SUPPORTED )
  282. {
  283. dwRes = TW32( DsEnumerateDomainTrusts( NULL,
  284. DS_DOMAIN_VALID_FLAGS & ( ~DS_DOMAIN_DIRECT_INBOUND),
  285. &paDomains,
  286. &ulLen
  287. ) );
  288. } // if:
  289. if ( dwRes != NO_ERROR )
  290. {
  291. hr = HRESULT_FROM_WIN32( dwRes );
  292. goto Cleanup;
  293. }
  294. //
  295. // Pass the information to the UI layer.
  296. //
  297. for ( idx = 0; idx < ulLen; idx++ )
  298. {
  299. if ( paDomains[ idx ].DnsDomainName != NULL )
  300. {
  301. if ( m_ptgdcb != NULL )
  302. {
  303. BSTR bstrDomainName = TraceSysAllocString( paDomains[ idx ].DnsDomainName );
  304. if ( bstrDomainName == NULL )
  305. {
  306. hr = THR( E_OUTOFMEMORY );
  307. goto Cleanup;
  308. }
  309. hr = THR( ReceiveDomainName( bstrDomainName ) );
  310. // check error after freeing string
  311. TraceSysFreeString( bstrDomainName );
  312. if ( FAILED( hr ) )
  313. {
  314. goto Cleanup;
  315. }
  316. }
  317. else
  318. {
  319. break;
  320. }
  321. }
  322. else if ( paDomains[ idx ].NetbiosDomainName != NULL )
  323. {
  324. if ( m_ptgdcb != NULL )
  325. {
  326. BSTR bstrDomainName = TraceSysAllocString( paDomains[ idx ].NetbiosDomainName );
  327. if ( bstrDomainName == NULL )
  328. {
  329. hr = THR( E_OUTOFMEMORY );
  330. goto Cleanup;
  331. }
  332. // send data
  333. hr = THR( ReceiveDomainName( bstrDomainName ) );
  334. TraceSysFreeString( bstrDomainName );
  335. if ( FAILED( hr ) )
  336. {
  337. goto Cleanup;
  338. }
  339. }
  340. }
  341. }
  342. hr = S_OK;
  343. Cleanup:
  344. if ( paDomains != NULL )
  345. {
  346. NetApiBufferFree( paDomains );
  347. }
  348. HRETURN( hr );
  349. } //*** CTaskGetDomains::BeginTask
  350. //////////////////////////////////////////////////////////////////////////////
  351. //
  352. // STDMETHODIMP
  353. // CTaskGetDomains::StopTask( void )
  354. //
  355. //////////////////////////////////////////////////////////////////////////////
  356. STDMETHODIMP
  357. CTaskGetDomains::StopTask( void )
  358. {
  359. TraceFunc( "[IDoTask]" );
  360. HRESULT hr = S_OK;
  361. HRETURN( hr );
  362. } //*** StopTask
  363. //////////////////////////////////////////////////////////////////////////////
  364. //
  365. // STDMETHODIMP
  366. // CTaskGetDomains::SetCallback(
  367. // ITaskGetDomainsCallback * punkIn
  368. // )
  369. //
  370. //////////////////////////////////////////////////////////////////////////////
  371. STDMETHODIMP
  372. CTaskGetDomains::SetCallback(
  373. ITaskGetDomainsCallback * punkIn
  374. )
  375. {
  376. TraceFunc( "[ITaskGetDomains]" );
  377. HRESULT hr = S_FALSE;
  378. if ( punkIn != NULL )
  379. {
  380. EnterCriticalSection( &m_csCallback );
  381. hr = THR( CoMarshalInterThreadInterfaceInStream( IID_ITaskGetDomainsCallback,
  382. punkIn,
  383. &m_pStream
  384. ) );
  385. LeaveCriticalSection( &m_csCallback );
  386. }
  387. else
  388. {
  389. hr = THR( HrReleaseCurrentCallback() );
  390. }
  391. HRETURN( hr );
  392. } //*** CTaskGetDomains::SetCallback
  393. //****************************************************************************
  394. //
  395. // Private
  396. //
  397. //****************************************************************************
  398. //////////////////////////////////////////////////////////////////////////////
  399. //
  400. // HRESULT
  401. // CTaskGetDomains::HrReleaseCurrentCallback( void )
  402. //
  403. //////////////////////////////////////////////////////////////////////////////
  404. HRESULT
  405. CTaskGetDomains::HrReleaseCurrentCallback( void )
  406. {
  407. TraceFunc( "" );
  408. HRESULT hr = S_OK;
  409. EnterCriticalSection( &m_csCallback );
  410. if ( m_pStream != NULL )
  411. {
  412. hr = THR( CoUnmarshalInterface( m_pStream,
  413. TypeSafeParams( ITaskGetDomainsCallback, &m_ptgdcb )
  414. ) );
  415. m_pStream->Release();
  416. m_pStream = NULL;
  417. }
  418. if ( m_ptgdcb != NULL )
  419. {
  420. m_ptgdcb->Release();
  421. m_ptgdcb = NULL;
  422. }
  423. LeaveCriticalSection( &m_csCallback );
  424. HRETURN( hr );
  425. } //*** CTaskGetDomains::HrReleaseCurrentCallback
  426. //////////////////////////////////////////////////////////////////////////////
  427. //
  428. // HRESULT
  429. // CTaskGetDomains::HrUnMarshallCallback( void )
  430. //
  431. //////////////////////////////////////////////////////////////////////////////
  432. HRESULT
  433. CTaskGetDomains::HrUnMarshallCallback( void )
  434. {
  435. TraceFunc( "" );
  436. HRESULT hr;
  437. EnterCriticalSection( &m_csCallback );
  438. hr = THR( CoUnmarshalInterface( m_pStream,
  439. TypeSafeParams( ITaskGetDomainsCallback, &m_ptgdcb )
  440. ) );
  441. m_pStream->Release();
  442. m_pStream = NULL;
  443. LeaveCriticalSection( &m_csCallback );
  444. HRETURN( hr );
  445. } //*** CTaskGetDomains::HrUnMarshallCallback
  446. //////////////////////////////////////////////////////////////////////////////
  447. //
  448. // STDMETHODIMP
  449. // CClusDomainPage::ReceiveDomainResult(
  450. // HRESULT hrIn
  451. // )
  452. //
  453. //////////////////////////////////////////////////////////////////////////////
  454. STDMETHODIMP
  455. CTaskGetDomains::ReceiveDomainResult(
  456. HRESULT hrIn
  457. )
  458. {
  459. TraceFunc( "" );
  460. HRESULT hr = S_FALSE;
  461. EnterCriticalSection( &m_csCallback );
  462. if ( m_ptgdcb != NULL )
  463. {
  464. hr = THR( m_ptgdcb->ReceiveDomainResult( hrIn ) );
  465. }
  466. LeaveCriticalSection( &m_csCallback );
  467. HRETURN( hr );
  468. } //*** CTaskGetDomains::ReceiveResult
  469. //////////////////////////////////////////////////////////////////////////////
  470. //
  471. // STDMETHODIMP
  472. // CTaskGetDomains::ReceiveDomainName(
  473. // BSTR bstrDomainNameIn
  474. // )
  475. //
  476. //////////////////////////////////////////////////////////////////////////////
  477. STDMETHODIMP
  478. CTaskGetDomains::ReceiveDomainName(
  479. BSTR bstrDomainNameIn
  480. )
  481. {
  482. TraceFunc( "" );
  483. HRESULT hr = S_FALSE;
  484. EnterCriticalSection( &m_csCallback );
  485. if ( m_ptgdcb != NULL )
  486. {
  487. hr = THR( m_ptgdcb->ReceiveDomainName( bstrDomainNameIn ) );
  488. }
  489. LeaveCriticalSection( &m_csCallback );
  490. HRETURN( hr );
  491. } //*** CTaskGetDomains::ReceiveDomainName