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.

3061 lines
108 KiB

  1. /*********************************************************************************************
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. LoggingData.cpp
  5. Abstract:
  6. Collects and displays all the data related with the logging option.
  7. Author:
  8. Wipro Technologies.
  9. Revision History:
  10. 22-Feb-2001 : Created It.
  11. *********************************************************************************************/
  12. #include "pch.h"
  13. #include "GpResult.h"
  14. #include "WMI.h"
  15. // Local function prototypes
  16. BOOL GetDomainType( LPTSTR lpszDomainName, BOOL * pbW2K, BOOL *pbLocalAccount );
  17. BOOL RsopDeleteMethod( IWbemClassObject *pClass, CHString strNameSpace,
  18. IWbemServices *pNamespace );
  19. VOID DisplayLinkSpeed( IWbemServices *pNameSpace, COAUTHIDENTITY *pAuthIdentity );
  20. VOID SortAppliedData( TARRAY arrAppliedData );
  21. /*********************************************************************************************
  22. Routine Description:
  23. This function is the main entry point for collecting and displaying the data for logging mode
  24. Arguments:
  25. None
  26. Return Value:
  27. TRUE on SUCCESS
  28. FALSE on ERROR
  29. *********************************************************************************************/
  30. BOOL CGpResult::GetLoggingData()
  31. {
  32. // Local declarations
  33. BOOL bResult = FALSE;
  34. BOOL bAllUsers = TRUE;
  35. DWORD dwBufferSize = MAX_STRING_LENGTH;
  36. DWORD dwPosition = -1;
  37. // Connect to wmi...connecting to 'cimv2' and saving the pointer in a member variable
  38. bResult = ConnectWmiEx( m_pWbemLocator, &m_pWbemServices, m_strServerName,
  39. m_strUserName, m_strPassword, &m_pAuthIdentity,
  40. m_bNeedPassword, ROOT_NAME_SPACE, &m_bLocalSystem );
  41. if( bResult == FALSE )
  42. {
  43. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  44. ShowMessage( stderr, GetReason() );
  45. return FALSE;
  46. }
  47. // check the remote system version and its compatiblity
  48. if ( m_bLocalSystem == FALSE )
  49. {
  50. // check the version compatibility
  51. DWORD dwVersion = 0;
  52. dwVersion = GetTargetVersionEx( m_pWbemServices, m_pAuthIdentity );
  53. // Check for the version W2K = 5000 and WindowsXP = 5001
  54. if ( dwVersion <= VERSION_CHECK )
  55. {
  56. // Display the appropriate error message
  57. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  58. ShowMessage( stderr, ERROR_OS_INCOMPATIBLE );
  59. return FALSE;
  60. }
  61. }
  62. // Set the password to the one got in the AUTHIDENTITY structure
  63. if( m_pAuthIdentity != NULL )
  64. {
  65. m_pwszPassword = m_pAuthIdentity->Password;
  66. }
  67. // check if it is the local system and the user credentials are specified....
  68. // if so display a warning message
  69. if( ( m_bLocalSystem == TRUE ) && ( m_strUserName.GetLength() != 0 ) )
  70. {
  71. ShowMessage( stdout, GetResString( IDS_WARNING ) );
  72. ShowMessage( stdout, GetResString( IDS_WARN_LOCAL ) );
  73. ShowMessage( stdout, NEW_LINE );
  74. // set the user name and password to NULL
  75. m_strUserName = L"";
  76. m_pwszPassword = NULL;
  77. // Get the new screen co-ordinates
  78. if ( m_hOutput != NULL )
  79. {
  80. GetConsoleScreenBufferInfo( m_hOutput, &m_csbi );
  81. }
  82. }
  83. // Connection part is over...check wether the user,for whom the RSOP data
  84. // has to be got has been specified.
  85. if( m_strUser.GetLength() == 0 )
  86. {
  87. // user is not specified....get the current logged on user
  88. LPWSTR pwszUserName = NULL;
  89. try
  90. {
  91. pwszUserName = m_strUser.GetBufferSetLength( dwBufferSize );
  92. }
  93. catch( ... )
  94. {
  95. // display the error message
  96. SetLastError( E_OUTOFMEMORY );
  97. SaveLastError();
  98. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  99. ShowMessage( stderr, GetReason() );
  100. }
  101. if ( GetUserNameEx( NameSamCompatible, pwszUserName, &dwBufferSize ) == FALSE )
  102. {
  103. // error occured while trying to get the current user info
  104. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  105. ShowMessage( stderr, GetResString( IDS_METHOD_FAILED ) );
  106. return FALSE;
  107. }
  108. // Release the buffer
  109. m_strUser.ReleaseBuffer();
  110. }
  111. // Separating the domain name from the user name for whom the data has to be retrieved
  112. if( m_strUser.Compare( TEXT_WILD_CARD ) != 0 )
  113. {
  114. bAllUsers = FALSE;
  115. dwPosition = m_strUser.Find( SLASH );
  116. try
  117. {
  118. if( dwPosition != -1 )
  119. {
  120. m_strDomainName = m_strUser.Left( dwPosition );
  121. m_strUser = m_strUser.Mid( ( dwPosition + 1 ), m_strUser.GetLength() );
  122. }
  123. else
  124. {
  125. // Try for the name@domain format (UPN format)
  126. dwPosition = m_strUser.Find( SEPARATOR_AT );
  127. if( dwPosition != -1 )
  128. {
  129. m_strDomainName = m_strUser.Mid( ( dwPosition + 1 ), m_strUser.GetLength() );
  130. m_strUser = m_strUser.Left( dwPosition );
  131. }
  132. // Remove the unwanted things in the domain name
  133. dwPosition = m_strDomainName.Find( SEPARATOR_DOT );
  134. if( dwPosition != -1 )
  135. {
  136. m_strDomainName = m_strDomainName.Left( dwPosition );
  137. }
  138. }
  139. }
  140. catch( ... )
  141. {
  142. // display the error message
  143. SetLastError( E_OUTOFMEMORY );
  144. SaveLastError();
  145. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  146. ShowMessage( stderr, GetReason() );
  147. }
  148. }
  149. PrintProgressMsg( m_hOutput, GetResString( IDS_USER_DATA ), m_csbi );
  150. //
  151. // Start the retrieval of information....
  152. // Get the user information
  153. if( GetUserData( bAllUsers ) == FALSE )
  154. {
  155. return FALSE;
  156. }
  157. return TRUE;
  158. }
  159. /*********************************************************************************************
  160. Routine Description:
  161. This function displays the non verbose data
  162. Arguments:
  163. [in] USERINFO : pointer to the user information structure
  164. [in] IWBEMSERVICES : pointer to the namespace
  165. Return Value:
  166. TRUE on SUCCESS
  167. FALSE on FAILURE
  168. *********************************************************************************************/
  169. BOOL CGpResult::DisplayData( PUSERINFO pUserInfo, IWbemServices *pRsopNameSpace )
  170. {
  171. // local variables
  172. HRESULT hResult = S_OK;
  173. BOOL bResult = FALSE;
  174. BOOL bCreatedRsop = FALSE;
  175. ULONG ulReturn = 0;
  176. DWORD dwi = 0;
  177. DWORD dwj = 0;
  178. DWORD dwMutex = 0;
  179. CHString strTemp;
  180. CHString strNameSpace;
  181. BSTR bstrTemp = NULL;
  182. IWbemClassObject *pClass = NULL;
  183. IWbemClassObject *pInClass = NULL;
  184. IWbemClassObject *pInInst = NULL;
  185. IWbemClassObject *pOutInst = NULL;
  186. IWbemServices *pNameSpace = NULL;
  187. IEnumWbemClassObject *pRsopClass = NULL;
  188. WCHAR szMutexName[512] = MUTEX_NAME;
  189. try
  190. {
  191. if( pUserInfo == NULL || pRsopNameSpace == NULL )
  192. {
  193. // erase the last status message
  194. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  195. _com_issue_error( STG_E_UNKNOWN );
  196. }
  197. PrintProgressMsg( m_hOutput, GetResString( IDS_GET_PROVIDER ), m_csbi );
  198. // Get the object for the RSOP diagnostic mode provider
  199. hResult = pRsopNameSpace->GetObject( _bstr_t( CLS_DIAGNOSTIC_PROVIDER ),
  200. 0, NULL, &pClass, NULL );
  201. CHECK_HRESULT_EX( hResult );
  202. PrintProgressMsg( m_hOutput, GetResString( IDS_GET_METHOD ), m_csbi );
  203. // get the reqd. method....create an rsop session
  204. hResult = pClass->GetMethod( _bstr_t( FN_CREATE_RSOP ), 0, &pInClass, NULL );
  205. CHECK_HRESULT_EX( hResult );
  206. // spawn the instances....get a new instance of the provider
  207. hResult = pInClass->SpawnInstance( 0, &pInInst );
  208. CHECK_HRESULT_EX( hResult );
  209. // Put the user SID...
  210. PrintProgressMsg( m_hOutput, GetResString( IDS_PUT_SID ), m_csbi );
  211. hResult = PropertyPut( pInInst, CPV_USER_SID, pUserInfo->strUserSid );
  212. CHECK_HRESULT_EX( hResult );
  213. hResult = PropertyPut( pInInst, CPV_FLAGS, FLAG_FORCE_CREATENAMESPACE );
  214. CHECK_HRESULT_EX( hResult );
  215. PrintProgressMsg( m_hOutput, GetResString( IDS_WAIT ), m_csbi );
  216. // We are ready to call the method to create a session
  217. // Check on the mutex to see if the call can be executed
  218. lstrcat( szMutexName, L"_");
  219. strTemp = pUserInfo->strUserName;
  220. LONG lPos = strTemp.Find(TEXT_BACKSLASH);
  221. if( lPos >= 0 && lPos <= strTemp.GetLength() )
  222. {
  223. strTemp.SetAt(lPos, L'_' );
  224. }
  225. lstrcat( szMutexName, strTemp );
  226. if( FALSE == CreateRsopMutex( szMutexName ) )
  227. {
  228. ShowMessage(stdout, GetResString(IDS_INFO) );
  229. SetLastError( ERROR_RETRY );
  230. ShowLastError( stdout );
  231. // release the interface pointers and exit
  232. SAFEIRELEASE( pRsopClass );
  233. SAFEIRELEASE( pClass );
  234. SAFEIRELEASE( pInClass );
  235. SAFEIRELEASE( pInInst );
  236. SAFEIRELEASE( pOutInst );
  237. return TRUE;
  238. }
  239. dwMutex = WAIT_FAILED;
  240. if( NULL == m_hMutex )
  241. {
  242. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  243. ShowMessage(stdout, GetResString(IDS_INFO) );
  244. SetLastError( ERROR_SINGLE_INSTANCE_APP );
  245. ShowLastError( stdout );
  246. // release the interface pointers and exit
  247. SAFEIRELEASE( pRsopClass );
  248. SAFEIRELEASE( pClass );
  249. SAFEIRELEASE( pInClass );
  250. SAFEIRELEASE( pInInst );
  251. SAFEIRELEASE( pOutInst );
  252. return TRUE;
  253. }
  254. if( m_hMutex != NULL )
  255. {
  256. dwMutex = WaitForSingleObject( m_hMutex, INFINITE );
  257. if( dwMutex == WAIT_FAILED )
  258. {
  259. SaveLastError();
  260. ShowMessage( stderr, GetResString(IDS_ERROR) );
  261. ShowMessage( stderr, GetReason() );
  262. }
  263. }
  264. if( dwMutex != WAIT_FAILED )
  265. {
  266. // Print the progress message
  267. strTemp.Format( GetResString( IDS_CREATE_SESSION ), pUserInfo->strUserName );
  268. PrintProgressMsg( m_hOutput, strTemp, m_csbi );
  269. // All The required properties are set, so...execute method RSopCreateSession
  270. hResult = pRsopNameSpace->ExecMethod( _bstr_t( CLS_DIAGNOSTIC_PROVIDER ),
  271. _bstr_t( FN_CREATE_RSOP ),
  272. 0, NULL, pInInst, &pOutInst, NULL);
  273. }
  274. if( pOutInst == NULL )
  275. {
  276. hResult = E_FAIL;
  277. }
  278. if( FAILED( hResult ) )
  279. {
  280. // erase the last status message
  281. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  282. // display the error message
  283. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  284. ShowMessage( stderr, GetResString( IDS_METHOD_FAILED ) );
  285. // release the interface pointers and exit
  286. SAFEIRELEASE( pRsopClass );
  287. SAFEIRELEASE( pClass );
  288. SAFEIRELEASE( pInClass );
  289. SAFEIRELEASE( pInInst );
  290. SAFEIRELEASE( pOutInst );
  291. //release the object
  292. ReleaseMutex( m_hMutex );
  293. return FALSE;
  294. }
  295. // Get the result value...
  296. bResult = PropertyGet( pOutInst, FPR_RETURN_VALUE, ulReturn, 0 );
  297. CHECK_BRESULT( bResult );
  298. if( ulReturn != 0 )
  299. {
  300. // erase the last status message
  301. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  302. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  303. ShowMessage( stderr, GetResString( IDS_METHOD_FAILED ) );
  304. // release the interface pointers and exit
  305. SAFEIRELEASE( pRsopClass );
  306. SAFEIRELEASE( pClass );
  307. SAFEIRELEASE( pInClass );
  308. SAFEIRELEASE( pInInst );
  309. SAFEIRELEASE( pOutInst );
  310. ReleaseMutex( m_hMutex );
  311. return FALSE;
  312. }
  313. // set the flag to indicate that the namespace has been created.
  314. bCreatedRsop = TRUE;
  315. // Get the resultant RSOP name space
  316. bResult = PropertyGet( pOutInst, FPR_RSOP_NAME_SPACE, strTemp, V_NOT_AVAILABLE );
  317. CHECK_BRESULT( bResult );
  318. // Check if we have got the output
  319. if( lstrcmp( strTemp, V_NOT_AVAILABLE ) == 0 )
  320. {
  321. // erase the last status message
  322. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  323. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  324. ShowMessage( stderr, GetResString( IDS_METHOD_FAILED ) );
  325. // release the allocated variables
  326. SAFEIRELEASE( pRsopClass );
  327. SAFEIRELEASE( pClass );
  328. SAFEIRELEASE( pInClass );
  329. SAFEIRELEASE( pInInst );
  330. SAFEIRELEASE( pOutInst );
  331. ReleaseMutex( m_hMutex );
  332. return FALSE;
  333. }
  334. // Got the data so start displaying
  335. // Display the information common to both scopes
  336. DisplayCommonData( pUserInfo );
  337. // Get the string starting with 'R'...as that's where the RSOP namespace starts
  338. // This is done to remove the '\'s in the beginning of the string returned.
  339. lPos = 0;
  340. strTemp.MakeLower();
  341. lPos = strTemp.Find( START_NAMESPACE );
  342. if ( lPos != -1 )
  343. {
  344. strTemp = strTemp.Mid( lPos + 1 );
  345. }
  346. else
  347. {
  348. _com_issue_error( STG_E_UNKNOWN );
  349. }
  350. // check if the computer information has to be displayed
  351. if( (m_dwScope == SCOPE_COMPUTER) || (m_dwScope == SCOPE_ALL) )
  352. {
  353. // connect to the resultant name space (computer)
  354. strNameSpace = strTemp + TEXT_BACKSLASH + TEXT_SCOPE_COMPUTER;
  355. ConnectWmi( m_pWbemLocator, &pNameSpace, m_strServerName,
  356. m_strUserName, m_pwszPassword, &m_pAuthIdentity,
  357. FALSE, strNameSpace, &hResult );
  358. CHECK_HRESULT( hResult );
  359. // get the link speed information
  360. DisplayLinkSpeed( pNameSpace, m_pAuthIdentity );
  361. // Display the heading for the scope Computer
  362. ShowMessage( stdout, GetResString( IDS_GPO_COMPUTER ) );
  363. ShowMessage( stdout, NEW_LINE );
  364. for( dwi = lstrlen( GetResString( IDS_GPO_COMPUTER ) ); dwi > 1; dwi-- )
  365. {
  366. ShowMessage( stdout, GetResString( IDS_DASH ) );
  367. }
  368. ShowMessage( stdout, GetResString( IDS_NEWLINE_TAB ) );
  369. // Display the FQDN for the computer
  370. ShowMessage( stdout, pUserInfo->strComputerFQDN );
  371. ShowMessage( stdout, GetResString( IDS_NEWLINE_TAB ) );
  372. // Display the link speed threshold value for the computer
  373. DisplayThresholdSpeedAndLastTimeInfo( TRUE );
  374. // Display the heading for the Computer GPO's
  375. ShowMessage( stdout, GetResString( IDS_GPO_DISPLAY ) );
  376. ShowMessage( stdout, GetResString( IDS_NEWLINE_TAB ) );
  377. for( dwi = lstrlen( GetResString( IDS_GPO_DISPLAY ) ); dwi > 4; dwi-- )
  378. {
  379. ShowMessage( stdout, GetResString( IDS_DASH ) );
  380. }
  381. ShowMessage( stdout, GetResString( IDS_NEWLINE ) );
  382. // Display the GPO data for computer
  383. GpoDisplay( pNameSpace, TEXT_SCOPE_COMPUTER );
  384. // Display the security groups for the system
  385. DisplaySecurityGroups( pNameSpace, TRUE );
  386. // check wether the verbose option is specified
  387. if( m_bVerbose == TRUE || m_bSuperVerbose == TRUE )
  388. {
  389. // display the verbose computer information
  390. DisplayVerboseComputerData( pNameSpace );
  391. }
  392. // release the interface pointer
  393. SAFEIRELEASE( pNameSpace );
  394. }
  395. // check for user...
  396. if( (m_dwScope == SCOPE_USER) || (m_dwScope == SCOPE_ALL) )
  397. {
  398. // connect to the resultant name space (user)
  399. strNameSpace = strTemp + TEXT_BACKSLASH + TEXT_SCOPE_USER;
  400. ConnectWmi( m_pWbemLocator, &pNameSpace, m_strServerName,
  401. m_strUserName, m_pwszPassword, &m_pAuthIdentity,
  402. FALSE, strNameSpace, &hResult );
  403. CHECK_HRESULT( hResult );
  404. // if only the user scope has been specified then the link speed
  405. // information has not yet been displayed...display it
  406. if( m_dwScope == SCOPE_USER )
  407. {
  408. // Get the link speed information
  409. DisplayLinkSpeed( pNameSpace, m_pAuthIdentity );
  410. }
  411. // Display the heading for the scope User
  412. ShowMessage( stdout, GetResString( IDS_GPO_USER ) );
  413. ShowMessage( stdout, NEW_LINE );
  414. for( dwi = lstrlen( GetResString( IDS_GPO_USER ) ); dwi > 1; dwi-- )
  415. {
  416. ShowMessage( stdout, GetResString( IDS_DASH ) );
  417. }
  418. ShowMessage( stdout, GetResString( IDS_NEWLINE_TAB ) );
  419. // Display the FQDN for the computer
  420. ShowMessage( stdout, pUserInfo->strUserFQDN );
  421. ShowMessage( stdout, GetResString( IDS_NEWLINE_TAB ) );
  422. // Display the link speed threshold value for the user
  423. DisplayThresholdSpeedAndLastTimeInfo( FALSE );
  424. // Display the heading for the User GPO's
  425. ShowMessage( stdout, GetResString( IDS_GPO_DISPLAY ) );
  426. ShowMessage( stdout, GetResString( IDS_NEWLINE_TAB ) );
  427. for( dwi = lstrlen( GetResString( IDS_GPO_DISPLAY ) ); dwi > 4; dwi-- )
  428. {
  429. ShowMessage( stdout, GetResString( IDS_DASH ) );
  430. }
  431. ShowMessage( stdout, GetResString( IDS_NEWLINE ) );
  432. // Display the GPO data for user
  433. GpoDisplay( pNameSpace, TEXT_SCOPE_USER );
  434. // Display the security groups for the user
  435. DisplaySecurityGroups( pNameSpace, FALSE );
  436. // check wether the verbose option is specified
  437. if( m_bVerbose == TRUE || m_bSuperVerbose == TRUE )
  438. {
  439. // display the verbose computer information
  440. DisplayVerboseUserData( pNameSpace );
  441. }
  442. // release the interface pointer
  443. SAFEIRELEASE( pNameSpace );
  444. }
  445. // Delete the namespace created
  446. if( RsopDeleteMethod( pClass, strTemp, pRsopNameSpace ) == FALSE )
  447. {
  448. // release the allocated variables
  449. SAFEIRELEASE( pRsopNameSpace );
  450. SAFEIRELEASE( pRsopClass );
  451. SAFEIRELEASE( pClass );
  452. SAFEIRELEASE( pInClass );
  453. SAFEIRELEASE( pInInst );
  454. SAFEIRELEASE( pOutInst );
  455. ReleaseMutex( m_hMutex );
  456. return FALSE;
  457. }
  458. }
  459. catch( _com_error & error )
  460. {
  461. // display the error message
  462. WMISaveError( error.Error() );
  463. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  464. ShowMessage( stderr, GetReason() );
  465. // Delete the name space if it has been created.
  466. if( bCreatedRsop == TRUE )
  467. {
  468. RsopDeleteMethod( pClass, strNameSpace, pRsopNameSpace );
  469. }
  470. // release the interface pointers and exit
  471. SAFEIRELEASE( pRsopClass );
  472. SAFEIRELEASE( pClass );
  473. SAFEIRELEASE( pInClass );
  474. SAFEIRELEASE( pInInst );
  475. SAFEIRELEASE( pOutInst );
  476. ReleaseMutex( m_hMutex );
  477. return FALSE;
  478. }
  479. ReleaseMutex( m_hMutex );
  480. // release the interface pointers and exit
  481. SAFEIRELEASE( pRsopClass );
  482. SAFEIRELEASE( pClass );
  483. SAFEIRELEASE( pInClass );
  484. SAFEIRELEASE( pInInst );
  485. SAFEIRELEASE( pOutInst );
  486. return TRUE;
  487. }
  488. /*********************************************************************************************
  489. Routine Description:
  490. This function gets the user data and fills the structure with the same
  491. Arguments:
  492. [in] BOOL bAllUsers : Specifies that the Rsop data has to be retrieved
  493. for all the users.
  494. Return Value:
  495. TRUE on SUCCESS
  496. FALSE on FAILURE
  497. *********************************************************************************************/
  498. BOOL CGpResult::GetUserData( BOOL bAllUsers )
  499. {
  500. // Local variables
  501. HRESULT hResult = S_OK;
  502. BOOL bResult = FALSE;
  503. BOOL bGotDomainInfo = FALSE;
  504. BOOL bConnFlag = TRUE;
  505. TCHAR szTemp[ MAX_STRING_LENGTH ];
  506. TCHAR szServer[ MAX_STRING_LENGTH ];
  507. TCHAR szName[ MAX_STRING_LENGTH ];
  508. TCHAR szDomain[ MAX_STRING_LENGTH ];
  509. TCHAR szFQDN[ MAX_STRING_LENGTH ];
  510. TCHAR szAdsiBuffer[ MAX_STRING_LENGTH ];
  511. CHString strTemp = NULL_STRING;
  512. CHString strDisplay = NULL_STRING;
  513. IEnumWbemClassObject *pEnumClass = NULL;
  514. IWbemServices *pRsopNameSpace = NULL;
  515. IWbemClassObject *pUserClass = NULL;
  516. IWbemClassObject *pInInst = NULL;
  517. IWbemClassObject *pOutInst = NULL;
  518. ULONG ulReturn = 0;
  519. LONG lCount = 0;
  520. LONG lLBound = 0;
  521. LONG lUBound = 0;
  522. DWORD dwName = MAX_STRING_LENGTH;
  523. DWORD dwDomain = MAX_STRING_LENGTH;
  524. DWORD dwBufSize = MAX_STRING_LENGTH;
  525. USERINFO *pUserInfo = new USERINFO;
  526. VARIANT vVarVerbose;
  527. VARTYPE vartype;
  528. SAFEARRAY *safeArray = NULL;
  529. PSID pSid = NULL;
  530. SID_NAME_USE *pSidNameUse = new SID_NAME_USE;
  531. try
  532. {
  533. // set the strings to NULL
  534. ZeroMemory( szTemp, MAX_STRING_LENGTH * sizeof( TCHAR ) );
  535. ZeroMemory( szName, MAX_STRING_LENGTH * sizeof( TCHAR ) );
  536. ZeroMemory( szServer, MAX_STRING_LENGTH * sizeof( TCHAR ) );
  537. ZeroMemory( szDomain, MAX_STRING_LENGTH * sizeof( TCHAR ) );
  538. ZeroMemory( szFQDN, MAX_STRING_LENGTH * sizeof( TCHAR ) );
  539. PrintProgressMsg( m_hOutput, GetResString( IDS_CONNECT_RSOP ), m_csbi );
  540. // connect to the RSOP namespace
  541. ConnectWmi( m_pWbemLocator, &pRsopNameSpace, m_strServerName,
  542. m_strUserName, m_pwszPassword, &m_pAuthIdentity,
  543. FALSE, _bstr_t( ROOT_RSOP ), &hResult );
  544. CHECK_HRESULT( hResult );
  545. // Get the object for the RSOP diagnostic mode provider
  546. hResult = pRsopNameSpace->GetObject( _bstr_t( CLS_DIAGNOSTIC_PROVIDER ),
  547. 0, NULL, &pUserClass, NULL );
  548. CHECK_HRESULT( hResult );
  549. PrintProgressMsg( m_hOutput, GetResString( IDS_GET_METHOD ), m_csbi );
  550. // get the reqd. method....to enumerate the users
  551. hResult = pUserClass->GetMethod( _bstr_t( FN_ENUM_USERS ), 0, &pInInst, NULL );
  552. CHECK_HRESULT( hResult );
  553. PrintProgressMsg( m_hOutput, GetResString( IDS_GET_SID ), m_csbi );
  554. // Execute method RSopEnumerateUsers
  555. hResult = pRsopNameSpace->ExecMethod( _bstr_t( CLS_DIAGNOSTIC_PROVIDER ),
  556. _bstr_t( FN_ENUM_USERS ),
  557. 0, NULL, pInInst, &pOutInst, NULL);
  558. if( pOutInst == NULL )
  559. {
  560. hResult = E_FAIL;
  561. }
  562. if( FAILED( hResult ) )
  563. {
  564. // erase the last status message
  565. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  566. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  567. ShowMessage( stderr, GetResString( IDS_METHOD_FAILED ) );
  568. // release the interface pointers and exit
  569. SAFEIRELEASE( pRsopNameSpace );
  570. SAFEIRELEASE( pUserClass );
  571. SAFEIRELEASE( pInInst );
  572. SAFEIRELEASE( pOutInst );
  573. SAFEIRELEASE( pEnumClass );
  574. SAFE_DELETE( pUserInfo );
  575. SAFE_DELETE( pSidNameUse );
  576. return FALSE;
  577. }
  578. // Get the result value...
  579. bResult = PropertyGet( pOutInst, FPR_RETURN_VALUE, ulReturn, 0 );
  580. CHECK_BRESULT( bResult );
  581. if( ulReturn != 0 )
  582. {
  583. // erase the last status message
  584. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  585. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  586. ShowMessage( stderr, GetResString( IDS_METHOD_FAILED ) );
  587. // release the interface pointers and exit
  588. SAFEIRELEASE( pRsopNameSpace );
  589. SAFEIRELEASE( pUserClass );
  590. SAFEIRELEASE( pInInst );
  591. SAFEIRELEASE( pOutInst );
  592. SAFEIRELEASE( pEnumClass );
  593. SAFE_DELETE( pUserInfo );
  594. SAFE_DELETE( pSidNameUse );
  595. return FALSE;
  596. }
  597. VariantInit( &vVarVerbose );
  598. hResult = pOutInst->Get( _bstr_t( CPV_USER_SIDS ), 0, &vVarVerbose, 0, 0 );
  599. CHECK_HRESULT_VAR( hResult, vVarVerbose );
  600. if( vVarVerbose.vt != VT_NULL && vVarVerbose.vt != VT_EMPTY )
  601. {
  602. // get the type of the elements in the safe array
  603. vartype = V_VT( &vVarVerbose ) & ~VT_ARRAY;
  604. //get the array of strings in to the safe array from the variant
  605. safeArray = (SAFEARRAY *)vVarVerbose.parray;
  606. //get the number of elements (subkeys)
  607. if( safeArray != NULL )
  608. {
  609. hResult = SafeArrayGetLBound( safeArray, 1, &lLBound );
  610. CHECK_HRESULT( hResult );
  611. hResult = SafeArrayGetUBound( safeArray, 1, &lUBound );
  612. CHECK_HRESULT( hResult );
  613. if( lUBound == 0xffffffff )
  614. {
  615. // erase the last status message
  616. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  617. ShowMessage( stdout, GetResString( IDS_INFO) );
  618. SetLastError( ERROR_POLICY_OBJECT_NOT_FOUND );
  619. ShowLastError( stdout );
  620. // release the interface pointers and exit
  621. SAFEIRELEASE( pRsopNameSpace );
  622. SAFEIRELEASE( pUserClass );
  623. SAFEIRELEASE( pInInst );
  624. SAFEIRELEASE( pOutInst );
  625. SAFEIRELEASE( pEnumClass );
  626. SAFE_DELETE( pUserInfo );
  627. SAFE_DELETE( pSidNameUse );
  628. return TRUE;
  629. }
  630. }
  631. // If we have to get the information from a remote machine then...
  632. // connect to the remote machine.
  633. if ( m_bLocalSystem == FALSE )
  634. {
  635. lstrcpy( szServer, m_strServerName );
  636. lstrcpy( szName, m_strUserName );
  637. // erase the last status message
  638. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  639. bResult = EstablishConnection( szServer, szName, MAX_STRING_LENGTH,
  640. m_pwszPassword, MAX_STRING_LENGTH, FALSE );
  641. if( bResult != TRUE )
  642. {
  643. // erase the last status message
  644. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  645. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  646. ShowMessage( stderr, GetReason() );
  647. // release the interface pointers and exit
  648. SAFEIRELEASE( pRsopNameSpace );
  649. SAFEIRELEASE( pUserClass );
  650. SAFEIRELEASE( pInInst );
  651. SAFEIRELEASE( pOutInst );
  652. SAFEIRELEASE( pEnumClass );
  653. SAFE_DELETE( pUserInfo );
  654. SAFE_DELETE( pSidNameUse );
  655. return FALSE;
  656. }
  657. else
  658. {
  659. switch( GetLastError() )
  660. {
  661. case I_NO_CLOSE_CONNECTION:
  662. bConnFlag = FALSE;
  663. break;
  664. case E_LOCAL_CREDENTIALS:
  665. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  666. bConnFlag = FALSE;
  667. break;
  668. default:
  669. break;
  670. }
  671. }
  672. // Get the new output co-ordinates
  673. if ( m_hOutput != NULL )
  674. {
  675. GetConsoleScreenBufferInfo( m_hOutput, &m_csbi );
  676. }
  677. }
  678. for( lCount = lLBound ; lLBound <= lUBound; lLBound++ )
  679. {
  680. bResult = GetPropertyFromSafeArray( safeArray, lLBound, strTemp, vartype );
  681. CHECK_BRESULT( bResult );
  682. // Got the SID...save it in the structure
  683. pUserInfo->strUserSid = strTemp;
  684. PrintProgressMsg( m_hOutput, GetResString( IDS_GET_NAME ), m_csbi );
  685. // Get the User Name
  686. lstrcpy( szTemp, strTemp );
  687. ConvertStringSidToSid( szTemp, &pSid );
  688. // Get the user name for the SID we have got
  689. bResult = LookupAccountSid( szServer, pSid, szName, &dwName, szDomain,
  690. &dwDomain, pSidNameUse );
  691. if( bResult == 0 )
  692. {
  693. // Could not get the name from the API try to retrieve it from WMI
  694. bResult = GetUserNameFromWMI( szTemp, szName, szDomain );
  695. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  696. if( bResult == FALSE )
  697. {
  698. // Increment the count
  699. lCount++;
  700. // If the user was not found then display a message stating the same
  701. if( lCount > lUBound )
  702. {
  703. strTemp = L"";
  704. if( bAllUsers == FALSE )
  705. {
  706. // check if we need to append the domain name
  707. if ( m_strDomainName.GetLength() != 0 )
  708. {
  709. strTemp = m_strDomainName + _T( "\\" ) + m_strUser;
  710. }
  711. else
  712. {
  713. strTemp = m_strUser;
  714. }
  715. }
  716. // Form the display string
  717. strDisplay.Format( GetResString( IDS_USER_NO_RSOP ), strTemp );
  718. ShowMessage( stderr, GetResString( IDS_INFO ) );
  719. ShowMessage( stderr, strDisplay );
  720. }
  721. // could not get a name for this SID, so continue with the next SID.
  722. continue;
  723. }
  724. }
  725. // Free the pSid
  726. if( pSid != NULL )
  727. {
  728. LocalFree( pSid );
  729. pSid = NULL;
  730. }
  731. // Check wether the Rsop data has to be retrieved for this user name.
  732. if( bAllUsers == FALSE )
  733. {
  734. if( lstrcmpi( szName, m_strUser ) != 0
  735. || ( lstrcmpi( szDomain, m_strDomainName ) != 0
  736. && m_strDomainName.GetLength() != 0 ) )
  737. {
  738. // erase the last status message
  739. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  740. // re-set the buffer sizes
  741. dwName = MAX_STRING_LENGTH;
  742. dwDomain = MAX_STRING_LENGTH;
  743. // Increment the count
  744. lCount++;
  745. // If the user was not found then display a message stating the same
  746. if( lCount > lUBound )
  747. {
  748. // check if we need to append the domain name
  749. if ( m_strDomainName.GetLength() != 0 )
  750. {
  751. strTemp = m_strDomainName + _T( "\\" ) + m_strUser;
  752. }
  753. else
  754. {
  755. strTemp = m_strUser;
  756. }
  757. // Form the display string
  758. strDisplay.Format( GetResString( IDS_USER_NO_RSOP ), strTemp );
  759. ShowMessage( stderr, GetResString( IDS_INFO ) );
  760. ShowMessage( stderr, strDisplay );
  761. }
  762. // No need to get the data for this user
  763. continue;
  764. }
  765. }
  766. // Store the user name into the structure.
  767. pUserInfo->strUserName = szName;
  768. // Append the domain name to the user name.
  769. lstrcat( szDomain, TEXT_BACKSLASH );
  770. lstrcat( szDomain, pUserInfo->strUserName );
  771. pUserInfo->strUserName = szDomain;
  772. PrintProgressMsg( m_hOutput, GetResString( IDS_GET_PROFILE ), m_csbi );
  773. // Get the user profile information
  774. if( GetUserProfile( pUserInfo ) == FALSE )
  775. {
  776. // erase the last status message
  777. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  778. // Display the error message
  779. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  780. ShowMessage( stderr, GetResString( IDS_METHOD_FAILED ) );
  781. // release the interface pointers and exit
  782. SAFEIRELEASE( pRsopNameSpace );
  783. SAFEIRELEASE( pUserClass );
  784. SAFEIRELEASE( pInInst );
  785. SAFEIRELEASE( pOutInst );
  786. SAFEIRELEASE( pEnumClass );
  787. SAFE_DELETE( pUserInfo );
  788. SAFE_DELETE( pSidNameUse );
  789. // if we have opened a connection then close the same.
  790. if( m_bLocalSystem == FALSE && bConnFlag == TRUE )
  791. {
  792. lstrcpy( szServer, m_strServerName );
  793. CloseConnection( szServer );
  794. }
  795. return FALSE;
  796. }
  797. if( bGotDomainInfo == FALSE )
  798. {
  799. PrintProgressMsg( m_hOutput, GetResString( IDS_GET_COMMON ), m_csbi );
  800. // Get the domain name and other related information
  801. if( GetDomainInfo( pUserInfo ) == FALSE )
  802. {
  803. // erase the last status message
  804. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  805. // Display the error message
  806. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  807. ShowMessage( stderr, GetResString( IDS_METHOD_FAILED ) );
  808. // release the interface pointers and exit
  809. SAFEIRELEASE( pRsopNameSpace );
  810. SAFEIRELEASE( pUserClass );
  811. SAFEIRELEASE( pInInst );
  812. SAFEIRELEASE( pOutInst );
  813. SAFEIRELEASE( pEnumClass );
  814. SAFE_DELETE( pUserInfo );
  815. SAFE_DELETE( pSidNameUse );
  816. // if we have opened a connection then close the same.
  817. if( m_bLocalSystem == FALSE && bConnFlag == TRUE )
  818. {
  819. lstrcpy( szServer, m_strServerName );
  820. CloseConnection( szServer );
  821. }
  822. return FALSE;
  823. }
  824. // Get the OS information
  825. if( GetOsInfo( pUserInfo ) == FALSE )
  826. {
  827. // erase the last status message
  828. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  829. // Display the error message
  830. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  831. ShowMessage( stderr, GetResString( IDS_METHOD_FAILED ) );
  832. // release the interface pointers and exit
  833. SAFEIRELEASE( pRsopNameSpace );
  834. SAFEIRELEASE( pUserClass );
  835. SAFEIRELEASE( pInInst );
  836. SAFEIRELEASE( pOutInst );
  837. SAFEIRELEASE( pEnumClass );
  838. SAFE_DELETE( pUserInfo );
  839. SAFE_DELETE( pSidNameUse );
  840. // if we have opened a connection then close the same.
  841. if( m_bLocalSystem == FALSE && bConnFlag == TRUE )
  842. {
  843. lstrcpy( szServer, m_strServerName );
  844. CloseConnection( szServer );
  845. }
  846. return FALSE;
  847. }
  848. // Get the FQDN of the computer
  849. // PrintProgressMsg( m_hOutput, GetResString( IDS_GET_FQDN ), m_csbi );
  850. if( m_bLocalSystem == TRUE )
  851. {
  852. // we have to get the FQDN for the local system
  853. // use the GetComputerObjectName API
  854. ulReturn = MAX_STRING_LENGTH;
  855. GetComputerObjectName( NameFullyQualifiedDN, szFQDN, &ulReturn);
  856. }
  857. else
  858. {
  859. // Get the local computers domain name
  860. GetComputerNameEx( ComputerNameDnsDomain, szAdsiBuffer, &dwBufSize );
  861. lstrcpy( szServer, m_strADSIServer );
  862. lstrcat( szServer, TEXT_DOLLAR );
  863. // Check if the machine we are querying is in the same domain
  864. if( m_strADSIDomain.CompareNoCase( szAdsiBuffer ) == 0 )
  865. {
  866. // get the FQDN from the Translate name call
  867. dwBufSize = MAX_STRING_LENGTH;
  868. TranslateName( szServer, NameDisplay, NameFullyQualifiedDN,
  869. szFQDN, &dwBufSize );
  870. }
  871. else
  872. {
  873. // Get the FQDN from ADSI directory services
  874. GetFQDNFromADSI( szFQDN, TRUE, szServer );
  875. }
  876. }
  877. // Store the FQDN into the structure.
  878. pUserInfo->strComputerFQDN = szFQDN;
  879. // Set the flag to TRUE so that this code is not executed again and again
  880. bGotDomainInfo = TRUE;
  881. }
  882. // Get the FQDN of the user
  883. if( ( m_bLocalSystem == TRUE )
  884. || ( m_strADSIDomain.CompareNoCase( szAdsiBuffer ) == 0 ) )
  885. {
  886. ZeroMemory( szFQDN, MAX_STRING_LENGTH * sizeof( TCHAR ) );
  887. dwBufSize = MAX_STRING_LENGTH;
  888. lstrcpy( szName, pUserInfo->strUserName );
  889. // get the FQDN from the Translate name call
  890. TranslateName( szName, NameSamCompatible, NameFullyQualifiedDN,
  891. szFQDN, &dwBufSize );
  892. }
  893. else
  894. {
  895. // Get the FQDN from ADSI directory services
  896. lstrcpy( szName, pUserInfo->strUserName );
  897. GetFQDNFromADSI( szFQDN, FALSE, szName );
  898. }
  899. // Store the FQDN into the structure.
  900. pUserInfo->strUserFQDN = szFQDN;
  901. // Now display the data
  902. PrintProgressMsg( m_hOutput, GetResString( IDS_STARTED_RETRIEVAL ), m_csbi );
  903. DisplayData( pUserInfo, pRsopNameSpace );
  904. // Get the new output co-ordinates
  905. if ( m_hOutput != NULL )
  906. {
  907. GetConsoleScreenBufferInfo( m_hOutput, &m_csbi );
  908. }
  909. // re-set the buffers and their sizes
  910. ZeroMemory( szTemp, MAX_STRING_LENGTH * sizeof( TCHAR ) );
  911. ZeroMemory( szName, MAX_STRING_LENGTH * sizeof( TCHAR ) );
  912. ZeroMemory( szServer, MAX_STRING_LENGTH * sizeof( TCHAR ) );
  913. ZeroMemory( szDomain, MAX_STRING_LENGTH * sizeof( TCHAR ) );
  914. dwName = MAX_STRING_LENGTH;
  915. dwDomain = MAX_STRING_LENGTH;
  916. }// for
  917. // if we have opened a connection then close the same.
  918. if( m_bLocalSystem == FALSE && bConnFlag == TRUE )
  919. {
  920. lstrcpy( szServer, m_strServerName );
  921. CloseConnection( szServer );
  922. }
  923. }
  924. else
  925. {
  926. // No classes were retrieved....display msg
  927. // erase the last status message
  928. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  929. // check if we need to append the domain name
  930. if ( m_strDomainName.GetLength() != 0 )
  931. {
  932. strTemp = m_strDomainName + _T( "\\" ) + m_strUser;
  933. }
  934. else
  935. {
  936. strTemp = m_strUser;
  937. }
  938. // Form the display string
  939. strDisplay.Format( GetResString( IDS_USER_NO_RSOP ), strTemp );
  940. ShowMessage( stderr, GetResString( IDS_INFO ) );
  941. ShowMessage( stderr, strDisplay );
  942. }
  943. VariantClear(&vVarVerbose);
  944. }
  945. catch( _com_error & error )
  946. {
  947. // erase the last status message
  948. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  949. // display the error msg
  950. WMISaveError( error.Error() );
  951. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  952. ShowMessage( stderr, GetReason() );
  953. // release the interface pointers and exit
  954. SAFEIRELEASE( pRsopNameSpace );
  955. SAFEIRELEASE( pUserClass );
  956. SAFEIRELEASE( pInInst );
  957. SAFEIRELEASE( pOutInst );
  958. SAFEIRELEASE( pEnumClass );
  959. SAFE_DELETE( pUserInfo );
  960. SAFE_DELETE( pSidNameUse );
  961. VariantClear(&vVarVerbose);
  962. return FALSE;
  963. }
  964. // release the interface pointers and exit
  965. SAFEIRELEASE( pRsopNameSpace );
  966. SAFEIRELEASE( pUserClass );
  967. SAFEIRELEASE( pInInst );
  968. SAFEIRELEASE( pOutInst );
  969. SAFEIRELEASE( pEnumClass );
  970. SAFE_DELETE( pUserInfo );
  971. SAFE_DELETE( pSidNameUse );
  972. return TRUE;
  973. }
  974. /*********************************************************************************************
  975. Routine Description:
  976. This function gets the user profile data and fills the array with the same
  977. Arguments:
  978. [in] PUSERINFO pUserInfo : Structure containing the user information.
  979. Return Value:
  980. TRUE on SUCCESS
  981. FALSE on FAILURE
  982. *********************************************************************************************/
  983. BOOL CGpResult::GetUserProfile( PUSERINFO pUserInfo )
  984. {
  985. // Local variables
  986. HRESULT hResult = S_OK;
  987. IWbemServices *pDefaultNameSpace = NULL;
  988. TCHAR szTemp[ MAX_STRING_LENGTH ];
  989. try
  990. {
  991. // connect to the default namespace
  992. ConnectWmi( m_pWbemLocator, &pDefaultNameSpace, m_strServerName,
  993. m_strUserName, m_pwszPassword, &m_pAuthIdentity,
  994. FALSE, _bstr_t( ROOT_DEFAULT ), &hResult );
  995. CHECK_HRESULT( hResult );
  996. // Set the sub key name
  997. lstrcpy( szTemp, PATH );
  998. lstrcat( szTemp, pUserInfo->strUserSid );
  999. // Get the local profile
  1000. RegQueryValueWMI( pDefaultNameSpace, HKEY_DEF, szTemp, FPR_LOCAL_VALUE,
  1001. pUserInfo->strLocalProfile, V_NOT_AVAILABLE );
  1002. // Get the roaming profile
  1003. RegQueryValueWMI( pDefaultNameSpace, HKEY_DEF, szTemp, FPR_ROAMING_VALUE,
  1004. pUserInfo->strRoamingProfile, V_NOT_AVAILABLE );
  1005. }
  1006. catch( _com_error & error )
  1007. {
  1008. WMISaveError( error.Error() );
  1009. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  1010. ShowMessage( stderr, GetReason() );
  1011. // release the allocated variables
  1012. SAFEIRELEASE( pDefaultNameSpace );
  1013. return FALSE;
  1014. }
  1015. // release the interface pointer
  1016. SAFEIRELEASE( pDefaultNameSpace );
  1017. return TRUE;
  1018. }
  1019. /*********************************************************************************************
  1020. Routine Description:
  1021. This function gets the domain information and fills the array with the same
  1022. Arguments:
  1023. [in] PUSERINFO pUserInfo : Structure containing the user information.
  1024. Return Value:
  1025. TRUE on SUCCESS
  1026. FALSE on FAILURE
  1027. *********************************************************************************************/
  1028. BOOL CGpResult::GetDomainInfo( PUSERINFO pUserInfo )
  1029. {
  1030. // Local variables
  1031. HRESULT hResult = S_OK;
  1032. BOOL bResult = FALSE;
  1033. BOOL bDone = FALSE;
  1034. IEnumWbemClassObject *pEnumClass = NULL;
  1035. IWbemClassObject *pClass = NULL;
  1036. ULONG ulReturn = 0;
  1037. CHString strTemp;
  1038. try
  1039. {
  1040. // print the progress message
  1041. PrintProgressMsg( m_hOutput, GetResString( IDS_GET_DOMAIN ), m_csbi );
  1042. // Enumerate the instances to get the domain and site names of the Win32 NT domain
  1043. hResult = m_pWbemServices->CreateInstanceEnum( _bstr_t( CLS_WIN32_SITE ),
  1044. WBEM_FLAG_FORWARD_ONLY |
  1045. WBEM_FLAG_RETURN_IMMEDIATELY,
  1046. NULL, &pEnumClass );
  1047. CHECK_HRESULT( hResult );
  1048. // Set the interface security
  1049. hResult = SetInterfaceSecurity( pEnumClass, m_pAuthIdentity );
  1050. CHECK_HRESULT( hResult );
  1051. // get the data
  1052. // since there may be more than one instance and we are looking for the instance
  1053. // with the domain controller and site name.....using a while loop and as soon as
  1054. // we get the instance we need break out of it
  1055. hResult = WBEM_S_NO_ERROR;
  1056. while( hResult == WBEM_S_NO_ERROR )
  1057. {
  1058. hResult = pEnumClass->Next( WBEM_INFINITE, 1, &pClass, &ulReturn );
  1059. CHECK_HRESULT( hResult );
  1060. if( ulReturn == 0 )
  1061. {
  1062. // no more data so break out of the loop
  1063. break;
  1064. }
  1065. // get the server name
  1066. if( bDone == FALSE )
  1067. {
  1068. bDone = TRUE;
  1069. bResult = PropertyGet( pClass, CPV_GPO_SERVER, pUserInfo->strUserServer,
  1070. V_NOT_AVAILABLE );
  1071. CHECK_BRESULT( bResult );
  1072. }
  1073. // get the domain name
  1074. bResult = PropertyGet( pClass, CPV_GPO_NAME, pUserInfo->strUserDomain,
  1075. V_NOT_AVAILABLE );
  1076. CHECK_BRESULT( bResult );
  1077. PrintProgressMsg( m_hOutput, GetResString( IDS_GET_SITE ), m_csbi );
  1078. // get the site name
  1079. bResult = PropertyGet( pClass, CPV_SITE_NAME, pUserInfo->strUserSite,
  1080. V_NOT_AVAILABLE );
  1081. CHECK_BRESULT( bResult );
  1082. // get the domain controller name
  1083. bResult = PropertyGet( pClass, CPV_DC_NAME, strTemp, V_NOT_AVAILABLE );
  1084. CHECK_BRESULT( bResult );
  1085. if( lstrcmp( strTemp, V_NOT_AVAILABLE ) != 0 )
  1086. {
  1087. // this enumeration has the domain controller name...
  1088. // we have got the enumeration we need so get the other data
  1089. break;
  1090. }
  1091. }// while
  1092. }
  1093. catch( _com_error & error )
  1094. {
  1095. // erase the last status message
  1096. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  1097. WMISaveError( error.Error() );
  1098. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  1099. ShowMessage( stderr, GetReason() );
  1100. // release the allocated variables
  1101. SAFEIRELEASE( pEnumClass );
  1102. SAFEIRELEASE( pClass );
  1103. return FALSE;
  1104. }
  1105. // release the interface pointers
  1106. SAFEIRELEASE( pEnumClass );
  1107. SAFEIRELEASE( pClass );
  1108. return TRUE;
  1109. }
  1110. /*********************************************************************************************
  1111. Routine Description:
  1112. This function returns the domain type
  1113. Arguments:
  1114. [in] lpDomainName : domain name intends to view Rsop data.
  1115. [out] pbW2K : contain whether the domain type in W2K.
  1116. [out] pbLocalAccount : contain whether the account is local.
  1117. Return Value:
  1118. TRUE - if DC found/domain name is computer name
  1119. FALSE - if DC not found
  1120. *********************************************************************************************/
  1121. BOOL GetDomainType( LPTSTR lpszDomainName, BOOL * pbW2K, BOOL *pbLocalAccount )
  1122. {
  1123. PDOMAIN_CONTROLLER_INFO pDCI;
  1124. DWORD dwResult = 0;
  1125. DWORD dwSize = 0;
  1126. TCHAR szComputerName[ MAX_PATH ];
  1127. // Check the incoming pointers
  1128. if( lpszDomainName == NULL || pbW2K == NULL || pbLocalAccount == NULL )
  1129. {
  1130. return FALSE;
  1131. }
  1132. // Check this domain for a Domain Controller
  1133. dwResult = DsGetDcName( NULL, lpszDomainName, NULL, NULL,
  1134. DS_DIRECTORY_SERVICE_PREFERRED, &pDCI );
  1135. if ( dwResult == ERROR_SUCCESS )
  1136. {
  1137. // Found a DC, does it have a DS ?
  1138. if ( pDCI->Flags & DS_DS_FLAG )
  1139. {
  1140. *pbW2K = TRUE;
  1141. }
  1142. NetApiBufferFree( pDCI );
  1143. return TRUE;
  1144. }
  1145. // Check if the domain name is also the computer name (eg: local account)
  1146. dwSize = ARRAYSIZE( szComputerName );
  1147. if ( GetComputerName ( szComputerName, &dwSize ) != 0 )
  1148. {
  1149. if ( lstrcmpi( szComputerName, lpszDomainName ) == 0 )
  1150. {
  1151. *pbLocalAccount = TRUE;
  1152. return TRUE;
  1153. }
  1154. }
  1155. return FALSE;
  1156. }
  1157. /*********************************************************************************************
  1158. Routine Description:
  1159. This function displays the data common to both scopes
  1160. Arguments:
  1161. [in] PUSERINFO pUserInfo : Structure containing the user information.
  1162. Return Value:
  1163. TRUE on SUCCESS
  1164. FALSE on FAILURE
  1165. *********************************************************************************************/
  1166. BOOL CGpResult::DisplayCommonData( PUSERINFO pUserInfo )
  1167. {
  1168. // Local variables
  1169. TCHAR szMsgBuffer[ MAX_RES_STRING ];
  1170. TCHAR szDate[ MAX_RES_STRING ];
  1171. TCHAR szTime[ MAX_RES_STRING ];
  1172. BOOL bW2KDomain = FALSE;
  1173. BOOL bLocalAccount = FALSE;
  1174. BOOL bLocaleChanged = FALSE;
  1175. DWORD dwLength = 0;
  1176. SYSTEMTIME systime;
  1177. LCID lcid;
  1178. // erase the last status message
  1179. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  1180. // Clear the Msg buffer
  1181. ZeroMemory( szMsgBuffer, MAX_RES_STRING );
  1182. // Start displaying the output
  1183. ShowMessage( stdout, NEW_LINE );
  1184. // Print the leagal information
  1185. ShowMessage( stdout, GetResString( IDS_LEGAL_INFO1 ) );
  1186. ShowMessage( stdout, GetResString( IDS_LEGAL_INFO2 ) );
  1187. // Print the date and time this report is generated
  1188. GetLocalTime( &systime );
  1189. // verify whether console supports the current locale 100% or not
  1190. lcid = GetSupportedUserLocale( bLocaleChanged );
  1191. // get the formatted date
  1192. GetDateFormat( lcid, 0, &systime, ((bLocaleChanged == TRUE) ? L"MM/dd/yyyy" : NULL),
  1193. szDate, SIZE_OF_ARRAY( szDate ) );
  1194. // now format the date
  1195. GetTimeFormat( LOCALE_USER_DEFAULT, 0, &systime, ((bLocaleChanged == TRUE) ? L"HH:mm:ss" : NULL),
  1196. szTime, SIZE_OF_ARRAY( szTime ) );
  1197. wsprintf( szMsgBuffer, GetResString( IDS_CREATED_ON ), szDate, szTime );
  1198. ShowMessage( stdout, szMsgBuffer );
  1199. ShowMessage( stdout, NEW_LINE );
  1200. // Display the common information....Domain Info
  1201. wsprintf( szMsgBuffer, GetResString( IDS_GPO_TITLE ), pUserInfo->strUserName,
  1202. pUserInfo->strUserServer );
  1203. ShowMessage( stdout, szMsgBuffer );
  1204. dwLength = lstrlen( szMsgBuffer );
  1205. ShowMessage( stdout, NEW_LINE );
  1206. // Underline the above heading
  1207. for( ; dwLength > 0; dwLength-- )
  1208. {
  1209. ShowMessage( stdout, GetResString( IDS_DASH ) );
  1210. }
  1211. ShowMessage( stdout, NEW_LINE );
  1212. lstrcpy( szMsgBuffer, pUserInfo->strUserDomain );
  1213. if( lstrlen( szMsgBuffer ) != 0 )
  1214. {
  1215. lstrcpy( szMsgBuffer, _tcstok( szMsgBuffer, GetResString( IDS_LAST_CHAR ) ) );
  1216. lstrcpy( szMsgBuffer, _tcstok( NULL, GetResString( IDS_LAST_CHAR ) ) );
  1217. }
  1218. // Show the OS information
  1219. ShowMessage( stdout, GetResString( IDS_OS_TYPE ) );
  1220. ShowMessage( stdout, pUserInfo->strOsType );
  1221. ShowMessage( stdout, GetResString( IDS_OS_CONFIG ) );
  1222. ShowMessage( stdout, pUserInfo->strOsConfig );
  1223. ShowMessage( stdout, GetResString( IDS_OS_VERSION ) );
  1224. ShowMessage( stdout, pUserInfo->strOsVersion );
  1225. ShowMessage( stdout, NEW_LINE );
  1226. // Get the domain type information
  1227. GetDomainType (szMsgBuffer, &bW2KDomain, &bLocalAccount);
  1228. ShowMessage( stdout, GetResString( IDS_DOMAIN_NAME ) );
  1229. ShowMessage( stdout, szMsgBuffer );
  1230. ShowMessage( stdout, NEW_LINE );
  1231. ShowMessage( stdout, GetResString( IDS_DOMAIN_TYPE ) );
  1232. // if it is a win2k domain type
  1233. if ( bW2KDomain )
  1234. {
  1235. ShowMessage( stdout, GetResString( IDS_W2K_DOMAIN ) );
  1236. }
  1237. else
  1238. {
  1239. if ( bLocalAccount ) // local account
  1240. {
  1241. ShowMessage( stdout, V_NOT_AVAILABLE );
  1242. ShowMessage( stdout, GetResString( IDS_LOCAL_COMP ) );
  1243. }
  1244. else //win NT4
  1245. {
  1246. ShowMessage( stdout, GetResString( IDS_NT4_DOMAIN ) );
  1247. }
  1248. }
  1249. // Display the Site name
  1250. ShowMessage( stdout, NEW_LINE );
  1251. ShowMessage( stdout, GetResString( IDS_SITE_NAME ) );
  1252. ShowMessage( stdout, pUserInfo->strUserSite );
  1253. // Display the roaming profile
  1254. ShowMessage( stdout, NEW_LINE );
  1255. ShowMessage( stdout, GetResString( IDS_ROAMING_PROFILE ) );
  1256. ShowMessage( stdout, pUserInfo->strRoamingProfile );
  1257. // Display the local profile
  1258. ShowMessage( stdout, NEW_LINE );
  1259. ShowMessage( stdout, GetResString( IDS_LOCAL_PROFILE ) );
  1260. ShowMessage( stdout, pUserInfo->strLocalProfile );
  1261. ShowMessage( stdout, NEW_LINE );
  1262. return TRUE;
  1263. }
  1264. /*********************************************************************************************
  1265. Routine Description:
  1266. This function displays the GPO information from the rsop namespace created.
  1267. Arguments:
  1268. [in] IEnumWbemClassObject : pointer to the Enumeration class object
  1269. [in] LPCTSTR : string containing the scope( USER or COMPUTER )
  1270. Return Value:
  1271. TRUE - if SUCCESS
  1272. FALSE - if ERROR
  1273. *********************************************************************************************/
  1274. BOOL CGpResult::GpoDisplay( IWbemServices *pNameSpace, LPCTSTR pszScopeName )
  1275. {
  1276. HRESULT hResult = WBEM_S_NO_ERROR;
  1277. BOOL bResult = FALSE;
  1278. BOOL bFilterAllowed = FALSE;
  1279. BOOL bLinkEnabled = FALSE;
  1280. BOOL bGpoEnabled = FALSE;
  1281. BOOL bAccessDenied = FALSE;
  1282. BOOL bConnected = FALSE;
  1283. ULONG ulReturn = 0;
  1284. ULONG ulAppliedOrder = 0;
  1285. ULONG ulVersion = 0;
  1286. DWORD dwAppliedRow = 0;
  1287. DWORD dwFilteredRow = 0;
  1288. CHString strTemp;
  1289. IEnumWbemClassObject *pRsopLinkClass = NULL;
  1290. IWbemClassObject *pRsopLinkObj = NULL;
  1291. IWbemClassObject *pRsopObj = NULL;
  1292. IWbemClassObject *pSomFilter = NULL;
  1293. IWbemServices *pPolicyNameSpace = NULL;
  1294. TARRAY arrAppliedData = NULL;
  1295. TARRAY arrFilteredData = NULL;
  1296. try
  1297. {
  1298. if( pNameSpace == NULL || pszScopeName == NULL )
  1299. {
  1300. _com_issue_error( STG_E_UNKNOWN );
  1301. }
  1302. // Create the Dynamic Arrays
  1303. arrAppliedData = CreateDynamicArray( );
  1304. arrFilteredData = CreateDynamicArray( );
  1305. // Check the memory allocations
  1306. if( arrAppliedData == NULL || arrFilteredData == NULL )
  1307. {
  1308. _com_issue_error( E_OUTOFMEMORY );
  1309. }
  1310. // enumerate the instances of the RSOP GPLink class
  1311. hResult = pNameSpace->CreateInstanceEnum( _bstr_t( CLS_RSOP_GPOLINK ),
  1312. WBEM_FLAG_FORWARD_ONLY |
  1313. WBEM_FLAG_RETURN_IMMEDIATELY,
  1314. NULL, &pRsopLinkClass );
  1315. CHECK_HRESULT( hResult );
  1316. // set the interface security
  1317. hResult = SetInterfaceSecurity( pRsopLinkClass, m_pAuthIdentity );
  1318. CHECK_HRESULT( hResult );
  1319. // Get the information from the enumerated classes
  1320. while( TRUE )
  1321. {
  1322. // Get the pointer to the next class
  1323. hResult = pRsopLinkClass->Next( WBEM_INFINITE, 1, &pRsopLinkObj, &ulReturn );
  1324. CHECK_HRESULT( hResult );
  1325. if( ulReturn == 0 )
  1326. {
  1327. break;
  1328. }
  1329. // Get the applied order for the link
  1330. bResult = PropertyGet( pRsopLinkObj, CPV_APPLIED_ORDER, ulAppliedOrder, 0 );
  1331. CHECK_BRESULT( bResult );
  1332. // Get the link enabled property
  1333. bResult = PropertyGet( pRsopLinkObj, CPV_ENABLED, bLinkEnabled, FALSE );
  1334. CHECK_BRESULT( bResult );
  1335. // Get the reference to the GPO class
  1336. bResult = PropertyGet( pRsopLinkObj, CPV_GPO_REF, strTemp, V_NOT_AVAILABLE );
  1337. CHECK_BRESULT( bResult );
  1338. // Check wether the link has a GPO class
  1339. if( strTemp.Find( GPO_REFERENCE ) != VAR_TRUE )
  1340. {
  1341. // Get the object for the GPO reference got
  1342. hResult = pNameSpace->GetObject( _bstr_t( strTemp ), 0, NULL, &pRsopObj, NULL );
  1343. if( FAILED( hResult ) )
  1344. {
  1345. if( hResult == WBEM_E_NOT_FOUND )
  1346. {
  1347. continue;
  1348. }
  1349. _com_issue_error( hResult );
  1350. }
  1351. // Get the GPO name
  1352. bResult = PropertyGet( pRsopObj, CPV_GPO_NAME, strTemp, V_NOT_AVAILABLE );
  1353. CHECK_BRESULT( bResult );
  1354. // Get the WMI filter status
  1355. bResult = PropertyGet( pRsopObj, CPV_GPO_FILTER_STATUS, bFilterAllowed, FALSE );
  1356. CHECK_BRESULT( bResult );
  1357. // Get the Gpo enabled information
  1358. bResult = PropertyGet( pRsopObj, CPV_ENABLED, bGpoEnabled, FALSE );
  1359. CHECK_BRESULT( bResult );
  1360. // Get the access denied information
  1361. bResult = PropertyGet( pRsopObj, CPV_ACCESS_DENIED, bAccessDenied, FALSE );
  1362. CHECK_BRESULT( bResult );
  1363. // Get the version
  1364. bResult = PropertyGet( pRsopObj, CPV_VERSION, ulVersion, 0 );
  1365. CHECK_BRESULT( bResult );
  1366. // If the applied order id not zero then this GPO is applied
  1367. if( ulAppliedOrder > 0 )
  1368. {
  1369. // Populate the applied Gpo array
  1370. DynArrayAppendRow( arrAppliedData, COL_MAX );
  1371. DynArraySetString2( arrAppliedData, dwAppliedRow, COL_DATA, strTemp, 0 );
  1372. DynArraySetDWORD2( arrAppliedData, dwAppliedRow, COL_ORDER, ulAppliedOrder );
  1373. dwAppliedRow++;
  1374. }
  1375. else if( bLinkEnabled != VAR_TRUE )
  1376. {
  1377. // if the link is disabled...populate the Filtered Array
  1378. DynArrayAppendRow( arrFilteredData, COL_MAX );
  1379. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_DATA, strTemp, 0 );
  1380. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_FILTER,
  1381. GetResString( IDS_LINK_DISABLED ), 0 );
  1382. dwFilteredRow++;
  1383. }
  1384. else if( bGpoEnabled != VAR_TRUE )
  1385. {
  1386. // if the GPO is disabled...populate the Filtered Array
  1387. DynArrayAppendRow( arrFilteredData, COL_MAX );
  1388. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_DATA, strTemp, 0 );
  1389. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_FILTER,
  1390. GetResString( IDS_GPO_DISABLED ), 0 );
  1391. dwFilteredRow++;
  1392. }
  1393. else if( bAccessDenied == VAR_TRUE )
  1394. {
  1395. // if the access is denied...populate the Filtered Array
  1396. DynArrayAppendRow( arrFilteredData, COL_MAX );
  1397. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_DATA, strTemp, 0 );
  1398. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_FILTER,
  1399. GetResString( IDS_ACCESS_DENIED ), 0 );
  1400. dwFilteredRow++;
  1401. }
  1402. else if( bFilterAllowed != VAR_TRUE )
  1403. {
  1404. // if the filter status is false...populate the Filtered Array
  1405. DynArrayAppendRow( arrFilteredData, COL_MAX_FILTER );
  1406. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_DATA, strTemp, 0 );
  1407. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_FILTER,
  1408. GetResString( IDS_WMI_DENIED ), 0 );
  1409. // Get the filter ID
  1410. bResult = PropertyGet( pRsopObj, CPV_GPO_FILTER_ID, strTemp, V_NOT_AVAILABLE );
  1411. CHECK_BRESULT( bResult );
  1412. // Store it in the array
  1413. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_FILTER_ID, strTemp, 0 );
  1414. dwFilteredRow++;
  1415. }
  1416. else if( ulVersion == 0 )
  1417. {
  1418. // if the version is zero...populate the Filtered Array
  1419. DynArrayAppendRow( arrFilteredData, COL_MAX );
  1420. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_DATA, strTemp, 0 );
  1421. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_FILTER,
  1422. GetResString( IDS_VERSION_ZERO ), 0 );
  1423. dwFilteredRow++;
  1424. }
  1425. else
  1426. {
  1427. // the Gpo is not applied due to an unknown reason...
  1428. // populate the Filtered Array
  1429. DynArrayAppendRow( arrFilteredData, COL_MAX );
  1430. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_DATA, strTemp, 0 );
  1431. DynArraySetString2( arrFilteredData, dwFilteredRow, COL_FILTER,
  1432. GetResString( IDS_NOT_APPLIED ), 0 );
  1433. dwFilteredRow++;
  1434. }
  1435. }
  1436. }// while
  1437. // Got the data...sort it
  1438. SortAppliedData( arrAppliedData );
  1439. // Display the applied data first
  1440. dwAppliedRow = DynArrayGetCount( arrAppliedData );
  1441. for( DWORD dwi = 0; dwi < dwAppliedRow; dwi++ )
  1442. {
  1443. ShowMessage( stdout, TAB_TWO );
  1444. ShowMessage( stdout, DynArrayItemAsString2( arrAppliedData, dwi, COL_DATA ) );
  1445. ShowMessage( stdout, GetResString( IDS_NEWLINE ) );
  1446. }
  1447. // Check if there was any data displayed
  1448. if( dwAppliedRow <= 0 )
  1449. {
  1450. ShowMessage( stdout, TAB_TWO );
  1451. ShowMessage( stdout, V_NOT_AVAILABLE );
  1452. ShowMessage( stdout, NEW_LINE );
  1453. }
  1454. // Display the filtered GPOs
  1455. // Display the header...if there are any GPO's filtered out
  1456. dwFilteredRow = DynArrayGetCount( arrFilteredData );
  1457. if( dwFilteredRow > 0 )
  1458. {
  1459. ShowMessage( stdout, GetResString( IDS_GPO_FILTERED ) );
  1460. ShowMessage( stdout, GetResString( IDS_NEWLINE_TAB ) );
  1461. for( dwi = lstrlen( GetResString( IDS_GPO_FILTERED ) ); dwi > 4; dwi-- )
  1462. {
  1463. ShowMessage( stdout, GetResString( IDS_DASH ) );
  1464. }
  1465. ShowMessage( stdout, GetResString( IDS_NEWLINE ) );
  1466. }
  1467. else
  1468. {
  1469. // There are no filtered GPO's hence put a new line and continue
  1470. // displaying the rest of the output
  1471. ShowMessage( stdout, GetResString( IDS_NEWLINE ) );
  1472. }
  1473. // display the data
  1474. for( DWORD dwi = 0; dwi < dwFilteredRow; dwi++ )
  1475. {
  1476. ShowMessage( stdout, TAB_TWO );
  1477. ShowMessage( stdout, DynArrayItemAsString2( arrFilteredData, dwi, COL_DATA ) );
  1478. ShowMessage( stdout, GetResString( IDS_FILTERING ) );
  1479. ShowMessage( stdout, DynArrayItemAsString2( arrFilteredData, dwi, COL_FILTER ) );
  1480. // Check if we have to display the filter id for the WMI filter that evaluated to false
  1481. if( lstrcmp( DynArrayItemAsString2( arrFilteredData, dwi, COL_FILTER ),
  1482. GetResString( IDS_WMI_DENIED ) ) == 0 )
  1483. {
  1484. if( bConnected == FALSE )
  1485. {
  1486. // we need to connect to Root\Policy
  1487. // connect to the default namespace
  1488. ConnectWmi( m_pWbemLocator, &pPolicyNameSpace, m_strServerName,
  1489. m_strUserName, m_pwszPassword, &m_pAuthIdentity,
  1490. FALSE, _bstr_t( ROOT_POLICY ), &hResult );
  1491. CHECK_HRESULT( hResult );
  1492. bConnected = TRUE;
  1493. }
  1494. // Get the object
  1495. hResult = pPolicyNameSpace->GetObject( _bstr_t( DynArrayItemAsString2(
  1496. arrFilteredData, dwi, COL_FILTER_ID ) ),
  1497. 0, NULL, &pSomFilter, NULL );
  1498. CHECK_HRESULT( hResult );
  1499. // Get the name of the filter applied
  1500. bResult = PropertyGet( pSomFilter, CPV_NAME, strTemp, V_NOT_AVAILABLE );
  1501. // display the filter ID
  1502. ShowMessage( stdout, GetResString( IDS_GPO_FILTER_ID ) );
  1503. ShowMessage( stdout, strTemp );
  1504. ShowMessage( stdout, GetResString( IDS_NEWLINE ) );
  1505. }
  1506. ShowMessage( stdout, GetResString( IDS_NEWLINE ) );
  1507. }
  1508. // destroy the dynamic arrays
  1509. DESTROY_ARRAY( arrAppliedData );
  1510. DESTROY_ARRAY( arrFilteredData );
  1511. }
  1512. catch( _com_error & error )
  1513. {
  1514. // display the error message
  1515. WMISaveError( error.Error() );
  1516. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  1517. ShowMessage( stderr, GetReason() );
  1518. SAFEIRELEASE( pRsopLinkObj );
  1519. SAFEIRELEASE( pRsopLinkClass );
  1520. SAFEIRELEASE( pRsopObj );
  1521. // destroy the dynamic arrays
  1522. DESTROY_ARRAY( arrAppliedData );
  1523. DESTROY_ARRAY( arrFilteredData );
  1524. return FALSE;
  1525. }
  1526. SAFEIRELEASE( pRsopLinkObj );
  1527. SAFEIRELEASE( pRsopLinkClass );
  1528. SAFEIRELEASE( pRsopObj );
  1529. return TRUE;
  1530. }
  1531. /*********************************************************************************************
  1532. Routine Description:
  1533. This function Will delete the Rsop namespace created by method RsopCreateSession.
  1534. Arguments:
  1535. [in] pClass : pointer to IWbemServices.
  1536. [in] CHString : string containing the RsopNamespace.
  1537. [in] pObject : pointer to IWbemClassObject.
  1538. Return Value:
  1539. TRUE - if SUCCESS
  1540. FALSE - if ERROR
  1541. *********************************************************************************************/
  1542. BOOL RsopDeleteMethod( IWbemClassObject *pClass, CHString strNameSpace,
  1543. IWbemServices *pNamespace )
  1544. {
  1545. HRESULT hResult = S_OK;
  1546. BOOL bResult = FALSE;
  1547. IWbemClassObject *pInClass = NULL;
  1548. IWbemClassObject *pInInst = NULL;
  1549. IWbemClassObject *pOutInst = NULL;
  1550. CHString strTemp;
  1551. DWORD ulReturn=0;
  1552. try
  1553. {
  1554. // Check the input Parameters
  1555. if( pClass == NULL )
  1556. {
  1557. _com_issue_error( STG_E_UNKNOWN );
  1558. }
  1559. //Delete the resultant RSOP namespace as the snap shot
  1560. //of RSOP has been obtained
  1561. hResult = pClass->GetMethod( _bstr_t( FN_DELETE_RSOP ), 0, &pInClass, NULL );
  1562. CHECK_HRESULT( hResult );
  1563. hResult = pInClass->SpawnInstance( 0, &pInInst );
  1564. CHECK_HRESULT( hResult );
  1565. //Put the input parameter
  1566. hResult = PropertyPut( pInInst, FPR_RSOP_NAMESPACE, strNameSpace );
  1567. CHECK_HRESULT( hResult );
  1568. // All The required properties are set so, execute method RsopDeleteSession
  1569. hResult = pNamespace->ExecMethod( _bstr_t( CLS_DIAGNOSTIC_PROVIDER ),
  1570. _bstr_t( FN_DELETE_RSOP ),
  1571. 0, NULL, pInInst, &pOutInst, NULL );
  1572. if(pOutInst == NULL)
  1573. {
  1574. hResult = E_FAIL;
  1575. }
  1576. if( FAILED( hResult ) )
  1577. {
  1578. // display the error msg
  1579. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  1580. ShowMessage( stderr, GetResString( IDS_METHOD_FAILED ) );
  1581. // release the interface pointers and exit
  1582. SAFEIRELEASE(pInClass);
  1583. SAFEIRELEASE(pInInst);
  1584. SAFEIRELEASE(pOutInst);
  1585. return FALSE;
  1586. }
  1587. //Get returned paramter to check whether the method was successfull
  1588. bResult = PropertyGet( pOutInst, FPR_RETURN_VALUE, ulReturn, 0);
  1589. CHECK_BRESULT( bResult );
  1590. // Returns some ERROR code.
  1591. if( ulReturn != 0 )
  1592. {
  1593. // Show Error Message
  1594. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  1595. ShowMessage( stderr, GetResString( IDS_METHOD_FAILED ) );
  1596. bResult = FALSE;
  1597. }
  1598. bResult = TRUE;
  1599. }
  1600. catch( _com_error & error )
  1601. {
  1602. // display the error message and set the return value to FALSE
  1603. WMISaveError( error.Error() );
  1604. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  1605. ShowMessage( stderr, GetReason() );
  1606. bResult = FALSE;
  1607. }
  1608. // release the interface pointers and exit
  1609. SAFEIRELEASE(pInClass);
  1610. SAFEIRELEASE(pInInst);
  1611. SAFEIRELEASE(pOutInst);
  1612. return bResult;
  1613. }
  1614. /*********************************************************************************************
  1615. Routine Description:
  1616. This function gets the OS information and fills the array with the same
  1617. Arguments:
  1618. [in] PUSERINFO pUserInfo : Structure containing the user information.
  1619. Return Value:
  1620. TRUE on SUCCESS
  1621. FALSE on FAILURE
  1622. *********************************************************************************************/
  1623. BOOL CGpResult::GetOsInfo( PUSERINFO pUserInfo )
  1624. {
  1625. // Local variables
  1626. HRESULT hResult = S_OK;
  1627. BOOL bResult = FALSE;
  1628. BOOL bDone = FALSE;
  1629. IEnumWbemClassObject *pEnumClass = NULL;
  1630. IWbemClassObject *pClass = NULL;
  1631. ULONG ulReturn = 0;
  1632. DWORD dwDomainRole = 0;
  1633. CHString strTemp;
  1634. try
  1635. {
  1636. // print the progress message
  1637. PrintProgressMsg( m_hOutput, GetResString( IDS_GET_OSINFO ), m_csbi );
  1638. // Enumerate the instances to get the domain and site names of the Win32 NT domain
  1639. hResult = m_pWbemServices->CreateInstanceEnum( _bstr_t( CLS_WIN32_OS ),
  1640. WBEM_FLAG_FORWARD_ONLY |
  1641. WBEM_FLAG_RETURN_IMMEDIATELY,
  1642. NULL, &pEnumClass );
  1643. CHECK_HRESULT( hResult );
  1644. // Set the interface security
  1645. hResult = SetInterfaceSecurity( pEnumClass, m_pAuthIdentity );
  1646. CHECK_HRESULT( hResult );
  1647. // get the data
  1648. hResult = pEnumClass->Next( WBEM_INFINITE, 1, &pClass, &ulReturn );
  1649. CHECK_HRESULT( hResult );
  1650. // get the OS version
  1651. bResult = PropertyGet( pClass, CPV_OS_VERSION, pUserInfo->strOsVersion,
  1652. V_NOT_AVAILABLE );
  1653. CHECK_BRESULT( bResult );
  1654. // get the OS type
  1655. bResult = PropertyGet( pClass, CPV_OS_CAPTION, pUserInfo->strOsType,
  1656. V_NOT_AVAILABLE );
  1657. CHECK_BRESULT( bResult );
  1658. // enumerate the instances of Win32_ComputerSystem class
  1659. hResult = m_pWbemServices->CreateInstanceEnum( _bstr_t( CLS_WIN32_CS ),
  1660. WBEM_FLAG_RETURN_IMMEDIATELY |
  1661. WBEM_FLAG_FORWARD_ONLY,
  1662. NULL, &pEnumClass );
  1663. // check the result of enumeration
  1664. CHECK_HRESULT( hResult );
  1665. // set the security on the obtained interface
  1666. hResult = SetInterfaceSecurity( pEnumClass, m_pAuthIdentity );
  1667. CHECK_HRESULT( hResult );
  1668. // get the enumerated objects information
  1669. // NOTE: This needs to be traversed only one time.
  1670. hResult = pEnumClass->Next( WBEM_INFINITE, 1, &pClass, &ulReturn );
  1671. CHECK_HRESULT( hResult );
  1672. // get the OS config
  1673. bResult = PropertyGet( pClass, CPV_DOMAIN_ROLE, dwDomainRole );
  1674. CHECK_BRESULT( bResult );
  1675. // get the domain name information for later use
  1676. hResult = PropertyGet( pClass, CPV_DOMAIN, m_strADSIDomain );
  1677. CHECK_BRESULT( bResult );
  1678. // get the server name for future use by LDAP
  1679. hResult = PropertyGet( pClass, CPV_NAME, m_strADSIServer );
  1680. CHECK_BRESULT( bResult );
  1681. //
  1682. // Mapping information of Win32_ComputerSystem's DomainRole property
  1683. // NOTE: Refer to the _DSROLE_MACHINE_ROLE enumeration values in DsRole.h header file
  1684. switch( dwDomainRole )
  1685. {
  1686. case DsRole_RoleStandaloneWorkstation:
  1687. pUserInfo->strOsConfig = VALUE_STANDALONEWORKSTATION;
  1688. break;
  1689. case DsRole_RoleMemberWorkstation:
  1690. pUserInfo->strOsConfig = VALUE_MEMBERWORKSTATION;
  1691. break;
  1692. case DsRole_RoleStandaloneServer:
  1693. pUserInfo->strOsConfig = VALUE_STANDALONESERVER;
  1694. break;
  1695. case DsRole_RoleMemberServer:
  1696. pUserInfo->strOsConfig = VALUE_MEMBERSERVER;
  1697. break;
  1698. case DsRole_RoleBackupDomainController:
  1699. pUserInfo->strOsConfig = VALUE_BACKUPDOMAINCONTROLLER;
  1700. break;
  1701. case DsRole_RolePrimaryDomainController:
  1702. pUserInfo->strOsConfig = VALUE_PRIMARYDOMAINCONTROLLER;
  1703. break;
  1704. default:
  1705. break;
  1706. }
  1707. }
  1708. catch( _com_error & error )
  1709. {
  1710. WMISaveError( error.Error() );
  1711. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  1712. ShowMessage( stderr, GetReason() );
  1713. // release the allocated variables
  1714. SAFEIRELEASE( pEnumClass );
  1715. SAFEIRELEASE( pClass );
  1716. return FALSE;
  1717. }
  1718. catch( CHeap_Exception )
  1719. {
  1720. // display the error message
  1721. SetLastError( E_OUTOFMEMORY );
  1722. SaveLastError();
  1723. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  1724. ShowMessage( stderr, GetReason() );
  1725. }
  1726. // release the interface pointers
  1727. SAFEIRELEASE( pEnumClass );
  1728. SAFEIRELEASE( pClass );
  1729. return TRUE;
  1730. }
  1731. /*********************************************************************************************
  1732. Routine Description:
  1733. This function displays the security groups for system and user
  1734. Arguments:
  1735. IWbemServices : pointer to the name space
  1736. BOOL : set to TRUE if the system o/p is to be displayed
  1737. Return Value:
  1738. None
  1739. *********************************************************************************************/
  1740. VOID CGpResult::DisplaySecurityGroups( IWbemServices *pNameSpace, BOOL bComputer )
  1741. {
  1742. HRESULT hResult = S_OK;
  1743. BOOL bResult = FALSE;
  1744. BOOL bGotClass = FALSE;
  1745. ULONG ulReturned = 0;
  1746. LONG lLBound = 0;
  1747. LONG lUBound = 0;
  1748. DWORD dwLength = 0;
  1749. IWbemClassObject *pClass = NULL;
  1750. IWbemClassObject *pName = NULL;
  1751. IWbemClassObject *pDomain = NULL;
  1752. IEnumWbemClassObject *pEnumClass = NULL;
  1753. IEnumWbemClassObject *pEnumDomain = NULL;
  1754. VARIANT vVarVerbose;
  1755. VARTYPE vartype;
  1756. TCHAR szTemp[ MAX_STRING_LENGTH ];
  1757. TCHAR szQueryString[ MAX_STRING_LENGTH ];
  1758. CHString strTemp;
  1759. CHString strDomain;
  1760. SAFEARRAY *safeArray = NULL;
  1761. try
  1762. {
  1763. ZeroMemory( szTemp, sizeof( szTemp ) );
  1764. ZeroMemory( szQueryString, sizeof( szQueryString ) );
  1765. // Enumerate the classes for the user privelege rights
  1766. hResult = pNameSpace->CreateInstanceEnum( _bstr_t( CLS_RSOP_SESSION ),
  1767. WBEM_FLAG_FORWARD_ONLY |
  1768. WBEM_FLAG_RETURN_IMMEDIATELY,
  1769. NULL, &pEnumClass );
  1770. CHECK_HRESULT( hResult );
  1771. // Set the interface security
  1772. hResult = SetInterfaceSecurity( pEnumClass, m_pAuthIdentity );
  1773. CHECK_HRESULT( hResult );
  1774. hResult = WBEM_S_NO_ERROR;
  1775. while( WBEM_S_NO_ERROR == hResult )
  1776. {
  1777. // Get the next class
  1778. hResult = pEnumClass->Next( WBEM_INFINITE , 1, &pClass, &ulReturned );
  1779. CHECK_HRESULT( hResult );
  1780. if( ulReturned == 0 )
  1781. {
  1782. // No more classes to enumerate
  1783. // Display N/A if there were no classes
  1784. if( bGotClass == FALSE )
  1785. {
  1786. ShowMessage( stdout, GetResString( IDS_NEWLINETAB ) );
  1787. ShowMessage( stdout, V_NOT_AVAILABLE );
  1788. ShowMessage( stdout, NEW_LINE );
  1789. }
  1790. break;
  1791. }
  1792. bGotClass = TRUE;
  1793. // Get the security groups
  1794. if( bComputer == TRUE )
  1795. {
  1796. ShowMessage( stdout, GetResString( IDS_SYS_SG ) );
  1797. ShowMessage( stdout, GetResString( IDS_NEWLINE_TAB ) );
  1798. for( dwLength = lstrlen( GetResString( IDS_SYS_SG ) ); dwLength > 4; dwLength-- )
  1799. {
  1800. ShowMessage( stdout, GetResString( IDS_DASH ) );
  1801. }
  1802. ShowMessage( stdout, GetResString( IDS_NEWLINETAB ) );
  1803. }
  1804. else
  1805. {
  1806. ShowMessage( stdout, GetResString( IDS_USER_SG ) );
  1807. ShowMessage( stdout, GetResString( IDS_NEWLINE_TAB ) );
  1808. for( dwLength = lstrlen( GetResString( IDS_USER_SG ) ); dwLength > 4; dwLength-- )
  1809. {
  1810. ShowMessage( stdout, GetResString( IDS_DASH ) );
  1811. }
  1812. ShowMessage( stdout, GetResString( IDS_NEWLINETAB ) );
  1813. }
  1814. VariantInit( &vVarVerbose );
  1815. hResult = pClass->Get( _bstr_t(CPV_SEC_GRPS), 0, &vVarVerbose, 0, 0 );
  1816. CHECK_HRESULT_VAR( hResult, vVarVerbose );
  1817. if( vVarVerbose.vt != VT_NULL && vVarVerbose.vt != VT_EMPTY )
  1818. {
  1819. // get the type of the elements in the safe array
  1820. vartype = V_VT( &vVarVerbose ) & ~VT_ARRAY;
  1821. //get the array of strings in to the safe array from the variant
  1822. safeArray = (SAFEARRAY *)vVarVerbose.parray;
  1823. //get the number of elements (subkeys)
  1824. if( safeArray != NULL )
  1825. {
  1826. hResult = SafeArrayGetLBound( safeArray, 1, &lLBound );
  1827. CHECK_HRESULT( hResult );
  1828. hResult = SafeArrayGetUBound( safeArray, 1, &lUBound );
  1829. CHECK_HRESULT( hResult );
  1830. }
  1831. for( ; lLBound <= lUBound; lLBound++ )
  1832. {
  1833. bResult = GetPropertyFromSafeArray( safeArray, lLBound, strTemp, vartype );
  1834. CHECK_BRESULT( bResult );
  1835. // Got a sid, now get it's coressponding name
  1836. // form the object path using the SID
  1837. wsprintf( szTemp, OBJECT_PATH, strTemp );
  1838. // Get the object
  1839. hResult = m_pWbemServices->GetObject( _bstr_t( szTemp ), 0, NULL, &pName, NULL );
  1840. CHECK_HRESULT( hResult );
  1841. // Get the Account name
  1842. bResult = PropertyGet( pName, CPV_ACCOUNT_NAME, strTemp, V_NOT_AVAILABLE );
  1843. // Append the appropriate prefix
  1844. if( strTemp.Compare( _T( "ANONYMOUS LOGON" ) ) == 0
  1845. || strTemp.Compare( _T( "BATCH" ) ) == 0
  1846. || strTemp.Compare( _T( "DIALUP" ) ) == 0
  1847. || strTemp.Compare( _T( "INTERACTIVE" ) ) == 0
  1848. || strTemp.Compare( _T( "SERVICE" ) ) == 0
  1849. || strTemp.Compare( _T( "SYSTEM" ) ) == 0
  1850. || strTemp.Compare( _T( "TERMINAL SERVICE USER" ) ) == 0
  1851. || strTemp.Compare( _T( "PROXY" ) ) == 0
  1852. || strTemp.Compare( _T( "NETWORK" ) ) == 0
  1853. || strTemp.Compare( _T( "ENTERPRISE DOMAIN CONTROLLERS" ) ) == 0
  1854. || strTemp.Compare( _T( "Authenticated Users" ) ) == 0
  1855. || strTemp.Compare( _T( "RESTRICTED" ) ) == 0
  1856. || strTemp.Compare( _T( "SELF" ) ) == 0 )
  1857. {
  1858. ShowMessage( stdout, _T( "NT AUTHORITY\\" ) );
  1859. }
  1860. else if( strTemp.Compare( _T( "Administrators" ) ) == 0
  1861. || strTemp.Compare( _T( "Backup Operators" ) ) == 0
  1862. || strTemp.Compare( _T( "Guests" ) ) == 0
  1863. || strTemp.Compare( _T( "Power Users" ) ) == 0
  1864. || strTemp.Compare( _T( "Replicator" ) ) == 0
  1865. || strTemp.Compare( _T( "Pre-Windows 2000 Compatible Access" ) ) == 0
  1866. || strTemp.Compare( _T( "Users" ) ) == 0 )
  1867. {
  1868. ShowMessage( stdout, _T( "BUILTIN\\" ) );
  1869. }
  1870. ShowMessage( stdout, strTemp );
  1871. ShowMessage( stdout, GetResString( IDS_NEWLINETAB ) );
  1872. }
  1873. }
  1874. else
  1875. {
  1876. ShowMessage( stdout, V_NOT_AVAILABLE );
  1877. ShowMessage( stdout, NEW_LINE );
  1878. }
  1879. VariantClear(&vVarVerbose);
  1880. }
  1881. }
  1882. catch( _com_error & error )
  1883. {
  1884. WMISaveError( error.Error() );
  1885. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  1886. ShowMessage( stderr, GetReason() );
  1887. VariantClear(&vVarVerbose);
  1888. }
  1889. // release the interface pointers
  1890. SAFEIRELEASE(pEnumClass);
  1891. SAFEIRELEASE(pEnumDomain);
  1892. SAFEIRELEASE(pClass);
  1893. SAFEIRELEASE(pName);
  1894. SAFEIRELEASE(pDomain);
  1895. return;
  1896. }
  1897. /*********************************************************************************************
  1898. Routine Description:
  1899. This function gets the link speed information
  1900. Arguments:
  1901. IWbemServices : pointer to the name space.
  1902. COAUTHIDENTITY : pointer to the AuthIdentity structure.
  1903. Return Value:
  1904. None
  1905. *********************************************************************************************/
  1906. VOID DisplayLinkSpeed( IWbemServices *pNameSpace, COAUTHIDENTITY *pAuthIdentity )
  1907. {
  1908. HRESULT hResult = S_OK;
  1909. BOOL bResult = FALSE;
  1910. BOOL bGotClass = FALSE;
  1911. BOOL bTemp = FALSE;
  1912. ULONG ulReturned = 0;
  1913. IWbemClassObject *pClass = NULL;
  1914. IEnumWbemClassObject *pEnumClass = NULL;
  1915. CHString strTemp;
  1916. VARIANT vVarTemp;
  1917. try
  1918. {
  1919. // Get the pointer to ennumerate with
  1920. hResult = pNameSpace->CreateInstanceEnum( _bstr_t( CLS_RSOP_SESSION ),
  1921. WBEM_FLAG_FORWARD_ONLY |
  1922. WBEM_FLAG_RETURN_IMMEDIATELY,
  1923. NULL, &pEnumClass );
  1924. CHECK_HRESULT( hResult );
  1925. // Set the interface security
  1926. hResult = SetInterfaceSecurity( pEnumClass, pAuthIdentity );
  1927. CHECK_HRESULT( hResult );
  1928. // Enumerate the classes one by one and get the data
  1929. hResult = WBEM_S_NO_ERROR;
  1930. while( WBEM_S_NO_ERROR == hResult )
  1931. {
  1932. // Get the next class
  1933. hResult = pEnumClass->Next( WBEM_INFINITE , 1, &pClass, &ulReturned );
  1934. CHECK_HRESULT( hResult );
  1935. if( ulReturned == 0 )
  1936. {
  1937. // No more classes to enumerate
  1938. // Display N/A if there were no classes
  1939. if( bGotClass == FALSE )
  1940. {
  1941. ShowMessage( stdout, GetResString( IDS_NEWLINETAB ) );
  1942. ShowMessage( stdout, V_NOT_AVAILABLE );
  1943. ShowMessage( stdout, NEW_LINE );
  1944. }
  1945. break;
  1946. }
  1947. bGotClass = TRUE;
  1948. VariantInit( &vVarTemp );
  1949. hResult = pClass->Get( _bstr_t( CPV_SLOW_LINK ), 0, &vVarTemp, 0, 0 );
  1950. CHECK_HRESULT_VAR( hResult, vVarTemp );
  1951. ShowMessage( stdout, GetResString( IDS_LINK_SPEED ) );
  1952. if( vVarTemp.vt != VT_NULL )
  1953. {
  1954. bTemp = vVarTemp.boolVal;
  1955. if( bTemp == VAR_TRUE )
  1956. {
  1957. ShowMessage( stdout, GetResString( IDS_YES ) );
  1958. }
  1959. else
  1960. {
  1961. ShowMessage( stdout, GetResString( IDS_NO ) );
  1962. }
  1963. }
  1964. else
  1965. {
  1966. ShowMessage( stdout, V_NOT_AVAILABLE );
  1967. }
  1968. ShowMessage( stdout, NEW_LINE );
  1969. VariantClear( &vVarTemp );
  1970. }// while
  1971. }
  1972. catch(_com_error & error)
  1973. {
  1974. WMISaveError( error.Error() );
  1975. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  1976. ShowMessage( stderr, GetReason() );
  1977. VariantClear( &vVarTemp );
  1978. }
  1979. // release the interface pointers
  1980. SAFEIRELEASE(pEnumClass);
  1981. SAFEIRELEASE(pClass);
  1982. return;
  1983. }
  1984. /*********************************************************************************************
  1985. Routine Description:
  1986. This function gets the User name and the domain name from WMI..
  1987. Arguments:
  1988. [in] szSid : string containing the SID
  1989. [out] szName : string to hold the user name
  1990. [out] szDomain : string to hold the domain name
  1991. Return Value:
  1992. TRUE on SUCCESS
  1993. FALSE on FAILURE
  1994. *********************************************************************************************/
  1995. BOOL CGpResult::GetUserNameFromWMI( TCHAR szSid[], TCHAR szName[], TCHAR szDomain[] )
  1996. {
  1997. // Local variables
  1998. HRESULT hResult = S_OK;
  1999. BOOL bResult = FALSE;
  2000. BOOL bGotClass = FALSE;
  2001. BOOL bGotDomainInfo = FALSE;
  2002. BOOL bUserSpecified = FALSE;
  2003. TCHAR szQueryString[ MAX_QUERY_STRING ];
  2004. TCHAR szTemp[ MAX_STRING_LENGTH ];
  2005. CHString strTemp = NULL_STRING;
  2006. IEnumWbemClassObject *pEnumClass = NULL;
  2007. IWbemClassObject *pClass = NULL;
  2008. ULONG ulReturn = 0;
  2009. try
  2010. {
  2011. // set the strings to NULL
  2012. ZeroMemory( szQueryString, MAX_QUERY_STRING * sizeof( TCHAR ));
  2013. ZeroMemory( szTemp, MAX_STRING_LENGTH * sizeof( TCHAR ) );
  2014. // Form the query string
  2015. lstrcpy( szTemp, QUERY_USER_NAME );
  2016. FORMAT_STRING( szQueryString, szTemp, szSid );
  2017. // execute the respective query
  2018. hResult = m_pWbemServices->ExecQuery( _bstr_t( QUERY_LANGUAGE ),
  2019. _bstr_t( szQueryString ),
  2020. WBEM_FLAG_FORWARD_ONLY |
  2021. WBEM_FLAG_RETURN_IMMEDIATELY,
  2022. NULL, &pEnumClass );
  2023. CHECK_HRESULT( hResult );
  2024. // set the security parameters
  2025. hResult = SetInterfaceSecurity( pEnumClass, m_pAuthIdentity );
  2026. CHECK_HRESULT( hResult );
  2027. // Get the user name
  2028. hResult = pEnumClass->Next( TIME_OUT_NEXT, 1, &pClass, &ulReturn );
  2029. CHECK_HRESULT( hResult );
  2030. // if there are no classes to enumerate break out of the loop
  2031. if( ulReturn == 0 )
  2032. {
  2033. // No classes were retrieved....display msg
  2034. // release the interface pointers and exit
  2035. SAFEIRELEASE( pClass );
  2036. SAFEIRELEASE( pEnumClass );
  2037. return FALSE;
  2038. }
  2039. // Get the class property(Name)
  2040. bResult = PropertyGet( pClass, CPV_NAME, strTemp, V_NOT_AVAILABLE );
  2041. CHECK_BRESULT( bResult );
  2042. // Got the name...Store it.
  2043. lstrcpy( szName, strTemp );
  2044. // Get and add the domain name if it exists
  2045. bResult = PropertyGet( pClass, CPV_DOMAIN, strTemp, V_NOT_AVAILABLE );
  2046. CHECK_BRESULT( bResult );
  2047. if( strTemp.Compare( V_NOT_AVAILABLE ) != 0 )
  2048. {
  2049. // Got the domain name...Store it.
  2050. lstrcpy( szDomain, strTemp );
  2051. }
  2052. }
  2053. catch( _com_error & error )
  2054. {
  2055. // erase the last status message
  2056. PrintProgressMsg( m_hOutput, NULL, m_csbi );
  2057. // display the error msg
  2058. WMISaveError( error.Error() );
  2059. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  2060. ShowMessage( stderr, GetReason() );
  2061. // release the interface pointers and exit
  2062. SAFEIRELEASE( pClass );
  2063. SAFEIRELEASE( pEnumClass );
  2064. return FALSE;
  2065. }
  2066. // release the interface pointers and exit
  2067. SAFEIRELEASE( pClass );
  2068. SAFEIRELEASE( pEnumClass );
  2069. return TRUE;
  2070. }
  2071. /*********************************************************************************************
  2072. Routine Description:
  2073. This function gets the threshold link speed information.
  2074. Arguments:
  2075. [in] BOOL : set to TRUE if the information is to be retrieved for the computer.
  2076. Return Value:
  2077. TRUE on SUCCESS
  2078. FALSE on FAILURE
  2079. *********************************************************************************************/
  2080. BOOL CGpResult::DisplayThresholdSpeedAndLastTimeInfo( BOOL bComputer )
  2081. {
  2082. // Local variables
  2083. HRESULT hResult = S_OK;
  2084. HKEY hKey = NULL;
  2085. HKEY hRemoteKey = NULL;
  2086. IWbemServices *pDefaultNameSpace = NULL;
  2087. TCHAR szTemp[ MAX_STRING_LENGTH ];
  2088. TCHAR szServer[ MAX_STRING_LENGTH ];
  2089. TCHAR szName[ MAX_STRING_LENGTH ];
  2090. TCHAR szTime[ MAX_STRING_LENGTH ];
  2091. TCHAR szDate[ MAX_STRING_LENGTH ];
  2092. BOOL bResult = FALSE;
  2093. BOOL bLocaleChanged = FALSE;
  2094. BOOL bConnFlag = TRUE;
  2095. CHString strTemp;
  2096. DWORD dwHkey = 0;
  2097. DWORD dwValue;
  2098. DWORD dwResult = 0;
  2099. FILETIME ftWrite;
  2100. FILETIME ftLocal;
  2101. SYSTEMTIME systime;
  2102. LCID lcid;
  2103. try
  2104. {
  2105. // If we have to get the information from a remote machine then...
  2106. // connect to the remote machine for the last time execution information.
  2107. if ( m_bLocalSystem == FALSE )
  2108. {
  2109. lstrcpy( szServer, m_strServerName );
  2110. lstrcpy( szName, m_strUserName );
  2111. bResult = EstablishConnection( szServer, szName, MAX_STRING_LENGTH,
  2112. m_pwszPassword, MAX_STRING_LENGTH, FALSE );
  2113. if( bResult != TRUE )
  2114. {
  2115. strTemp = V_NOT_AVAILABLE;
  2116. }
  2117. else
  2118. {
  2119. switch( GetLastError() )
  2120. {
  2121. case I_NO_CLOSE_CONNECTION:
  2122. bConnFlag = FALSE;
  2123. break;
  2124. case E_LOCAL_CREDENTIALS:
  2125. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  2126. bConnFlag = FALSE;
  2127. break;
  2128. default:
  2129. break;
  2130. }
  2131. }
  2132. // Connect to the remote registry
  2133. lstrcpy( szServer , _T( "\\\\" ) );
  2134. lstrcat( szServer, m_strServerName );
  2135. dwResult = RegConnectRegistry( szServer, bComputer ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
  2136. &hRemoteKey );
  2137. if( dwResult != ERROR_SUCCESS )
  2138. {
  2139. strTemp = V_NOT_AVAILABLE;
  2140. }
  2141. }
  2142. // Open the last time execution information key
  2143. dwResult = RegOpenKeyEx (bComputer ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
  2144. GROUPPOLICY_PATH, 0, KEY_READ, &hKey);
  2145. if( dwResult != ERROR_SUCCESS )
  2146. {
  2147. strTemp = V_NOT_AVAILABLE;
  2148. }
  2149. // Get the last time execution information
  2150. dwResult = RegQueryInfoKey( hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  2151. NULL, &ftWrite );
  2152. if( dwResult == ERROR_SUCCESS )
  2153. {
  2154. FileTimeToLocalFileTime( &ftWrite, &ftLocal );
  2155. FileTimeToSystemTime( &ftLocal, &systime );
  2156. // verify whether console supports the current locale 100% or not
  2157. lcid = GetSupportedUserLocale( bLocaleChanged );
  2158. // get the formatted date
  2159. GetDateFormat( lcid, 0, &systime, ((bLocaleChanged == TRUE) ? L"MM/dd/yyyy" : NULL),
  2160. szDate, SIZE_OF_ARRAY( szDate ) );
  2161. // now format the date
  2162. GetTimeFormat( LOCALE_USER_DEFAULT, 0, &systime, ((bLocaleChanged == TRUE) ? L"HH:mm:ss" : NULL),
  2163. szTime, SIZE_OF_ARRAY( szTime ) );
  2164. wsprintf( szTemp, LAST_TIME_OP, szDate, szTime );
  2165. strTemp = szTemp;
  2166. }
  2167. else
  2168. {
  2169. strTemp = V_NOT_AVAILABLE;
  2170. }
  2171. // Display the retrieved data
  2172. ShowMessage( stdout, GetResString( IDS_LAST_TIME ) );
  2173. ShowMessage( stdout, strTemp );
  2174. ShowMessage( stdout, GetResString( IDS_NEWLINE_TAB ) );
  2175. // if we have opened a connection then close the same.
  2176. if( m_bLocalSystem == FALSE && bConnFlag == TRUE )
  2177. {
  2178. lstrcpy( szServer, m_strServerName );
  2179. CloseConnection( szServer );
  2180. }
  2181. // Close the registry keys
  2182. if( hKey != NULL )
  2183. {
  2184. RegCloseKey( hKey );
  2185. }
  2186. if( hRemoteKey != NULL )
  2187. {
  2188. RegCloseKey( hRemoteKey );
  2189. }
  2190. // connect to the default namespace
  2191. ConnectWmi( m_pWbemLocator, &pDefaultNameSpace, m_strServerName,
  2192. m_strUserName, m_pwszPassword, &m_pAuthIdentity,
  2193. FALSE, _bstr_t( ROOT_DEFAULT ), &hResult );
  2194. CHECK_HRESULT( hResult );
  2195. // form the key
  2196. if( bComputer == TRUE )
  2197. {
  2198. dwHkey = HKEY_DEF;
  2199. }
  2200. else
  2201. {
  2202. dwHkey = HKEY_CURRENT_USER_DEF;
  2203. }
  2204. // Get the DC name from where the policy was applied last
  2205. RegQueryValueWMI( pDefaultNameSpace, dwHkey, APPLIED_PATH, FPR_APPLIED_FROM,
  2206. strTemp, V_NOT_AVAILABLE );
  2207. // remove the forward slashes (UNC) if exist in the begining of the server name
  2208. if ( IsUNCFormat( strTemp ) == TRUE )
  2209. {
  2210. strTemp = strTemp.Mid( 2 );
  2211. }
  2212. // Display the retrieved data
  2213. ShowMessage( stdout, GetResString( IDS_APPLIED_FROM ) );
  2214. ShowMessage( stdout, strTemp );
  2215. ShowMessage( stdout, GetResString( IDS_NEWLINE_TAB ) );
  2216. // Get the threshold link speed information
  2217. RegQueryValueWMI( pDefaultNameSpace, dwHkey, GPRESULT_PATH, FPR_LINK_SPEED_VALUE, dwValue, -1 );
  2218. if( dwValue == -1 )
  2219. {
  2220. strTemp = DEFAULT_LINK_SPEED;
  2221. }
  2222. else
  2223. {
  2224. FORMAT_STRING( szTemp, _T( "%d kbps" ), dwValue );
  2225. strTemp = szTemp;
  2226. }
  2227. // Display the retrieved data
  2228. ShowMessage( stdout, GetResString( IDS_THRESHOLD_LINK_SPEED ) );
  2229. ShowMessage( stdout, strTemp );
  2230. ShowMessage( stdout, NEW_LINE );
  2231. }
  2232. catch( _com_error & error )
  2233. {
  2234. WMISaveError( error.Error() );
  2235. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  2236. ShowMessage( stderr, GetReason() );
  2237. // release the allocated variables
  2238. SAFEIRELEASE( pDefaultNameSpace );
  2239. return FALSE;
  2240. }
  2241. catch( CHeap_Exception )
  2242. {
  2243. // display the error message
  2244. SetLastError( E_OUTOFMEMORY );
  2245. SaveLastError();
  2246. ShowMessage( stderr, GetResString( IDS_ERROR ) );
  2247. ShowMessage( stderr, GetReason() );
  2248. }
  2249. // release the interface pointer
  2250. SAFEIRELEASE( pDefaultNameSpace );
  2251. return TRUE;
  2252. }
  2253. /*********************************************************************************************
  2254. Routine Description:
  2255. This function checks if the current locale is supported or not.
  2256. Arguments:
  2257. [in] bLocaleChanged
  2258. Return Value:
  2259. LCID of the current locale.
  2260. *********************************************************************************************/
  2261. LCID GetSupportedUserLocale( BOOL& bLocaleChanged )
  2262. {
  2263. // local variables
  2264. LCID lcid;
  2265. // get the current locale
  2266. lcid = GetUserDefaultLCID();
  2267. // check whether the current locale is supported by our tool or not
  2268. // if not change the locale to the english which is our default locale
  2269. bLocaleChanged = FALSE;
  2270. if ( PRIMARYLANGID( lcid ) == LANG_ARABIC || PRIMARYLANGID( lcid ) == LANG_HEBREW ||
  2271. PRIMARYLANGID( lcid ) == LANG_THAI || PRIMARYLANGID( lcid ) == LANG_HINDI ||
  2272. PRIMARYLANGID( lcid ) == LANG_TAMIL || PRIMARYLANGID( lcid ) == LANG_FARSI )
  2273. {
  2274. bLocaleChanged = TRUE;
  2275. lcid = MAKELCID( MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ), SORT_DEFAULT ); // 0x409;
  2276. }
  2277. // return the locale
  2278. return lcid;
  2279. }
  2280. /*********************************************************************************************
  2281. Routine Description:
  2282. This function sorts the applied Gpo data in the order applied.
  2283. Arguments:
  2284. [in/out] TARRAY : Array to be sorted
  2285. Return Value:
  2286. None
  2287. *********************************************************************************************/
  2288. VOID SortAppliedData( TARRAY arrAppliedData )
  2289. {
  2290. // Local variables
  2291. TARRAY arrSortedData = NULL;
  2292. DWORD dwMax = 0;
  2293. DWORD dwCount = 0;
  2294. DWORD dwi = 0;
  2295. DWORD dwOrder = 0;
  2296. DWORD dwIndex = 0;
  2297. // Create the dynamic array to hold the sorted data temporarily
  2298. arrSortedData = CreateDynamicArray( );
  2299. dwCount = DynArrayGetCount( arrAppliedData );
  2300. // Get the max applied order in the array
  2301. for( dwi = 0; dwi < dwCount; dwi++ )
  2302. {
  2303. dwOrder = DynArrayItemAsDWORD2( arrAppliedData, dwi, COL_ORDER );
  2304. if( dwOrder > dwMax )
  2305. {
  2306. dwMax = dwOrder;
  2307. }
  2308. }
  2309. // Create the sorted array in the descending order of the applied order
  2310. for( dwi = 0; dwi < dwCount; dwi++ )
  2311. {
  2312. // re-set the index variable
  2313. dwIndex = 0;
  2314. // Get the index of the row whose order = dwMax
  2315. while( TRUE )
  2316. {
  2317. dwOrder = DynArrayItemAsDWORD2( arrAppliedData, dwIndex, COL_ORDER );
  2318. if( dwOrder == dwMax )
  2319. {
  2320. break;
  2321. }
  2322. // increment the index
  2323. dwIndex++;
  2324. // Additional check...just in case the order does not exist
  2325. // avoid an AV
  2326. if( dwIndex == dwCount )
  2327. {
  2328. break;
  2329. }
  2330. }
  2331. // Additional check...just in case the order does not exist
  2332. // avoid an AV
  2333. if( dwIndex == dwCount )
  2334. {
  2335. // could not find the index
  2336. // decrement the max order
  2337. dwMax--;
  2338. continue;
  2339. }
  2340. // Store the contents of the row
  2341. DynArrayAppendRow( arrSortedData, COL_MAX );
  2342. DynArraySetString2( arrSortedData, dwi, COL_DATA,
  2343. DynArrayItemAsString2( arrAppliedData, dwIndex, COL_DATA ), 0 );
  2344. DynArraySetDWORD2( arrSortedData, dwi, COL_ORDER, dwOrder );
  2345. // decrement the max order
  2346. dwMax--;
  2347. }
  2348. // copy the sorted data onto the applied data array
  2349. for( dwi = 0; dwi < dwCount; dwi++ )
  2350. {
  2351. DynArraySetString2( arrAppliedData, dwi, COL_DATA,
  2352. DynArrayItemAsString2( arrSortedData, dwi, COL_DATA ), 0 );
  2353. DynArraySetDWORD2( arrAppliedData, dwi, COL_ORDER,
  2354. DynArrayItemAsDWORD2( arrSortedData, dwi, COL_ORDER ) );
  2355. }
  2356. // destroy the temporarily created dynamic array
  2357. DESTROY_ARRAY( arrSortedData );
  2358. return;
  2359. }
  2360. /*********************************************************************************************
  2361. Routine Description:
  2362. This function retrieves the FQDN from ADSI
  2363. Arguments:
  2364. [out] TCHAR [] : Array to hold the FQDN
  2365. [in] BOOL : flag to specify wether the FQDN is to be retrieved for
  2366. the computer or the user
  2367. [in] LPCTSTR : The name for whom the FQDN is to be retrieved
  2368. Return Value:
  2369. None
  2370. *********************************************************************************************/
  2371. VOID CGpResult::GetFQDNFromADSI( TCHAR szFQDN[], BOOL bComputer, LPCTSTR pszName )
  2372. {
  2373. // Local variables
  2374. HRESULT hResult = S_OK;
  2375. HANDLE hDS = NULL;
  2376. DWORD dwReturn = 0;
  2377. DS_NAME_RESULT *pNameResult = NULL;
  2378. ZeroMemory( szFQDN, MAX_STRING_LENGTH * sizeof( TCHAR ) );
  2379. // Bind to the ADSI directory service
  2380. dwReturn = DsBindWithCred( NULL, m_strADSIDomain, m_pAuthIdentity, &hDS );
  2381. if( dwReturn != NO_ERROR )
  2382. {
  2383. return;
  2384. }
  2385. // Get the FQDN information
  2386. dwReturn = DsCrackNames( hDS, DS_NAME_NO_FLAGS,
  2387. bComputer ? DS_DISPLAY_NAME : DS_NT4_ACCOUNT_NAME, DS_FQDN_1779_NAME,
  2388. 1, &pszName, &pNameResult );
  2389. if( dwReturn != NO_ERROR )
  2390. {
  2391. return;
  2392. }
  2393. // store the retrieved FQDN
  2394. lstrcpy( szFQDN, pNameResult->rItems->pName );
  2395. // Free the handle to the ADSI directory services
  2396. DsUnBind( &hDS );
  2397. // Free the allocated memory
  2398. DsFreeNameResult( pNameResult );
  2399. return;
  2400. }
  2401. BOOL CGpResult::CreateRsopMutex( LPWSTR szMutexName )
  2402. {
  2403. BOOL bResult = FALSE;
  2404. SECURITY_ATTRIBUTES sa;
  2405. PSECURITY_DESCRIPTOR psd = NULL;
  2406. //
  2407. // first try to open the mutex object by its name
  2408. // if that fails it means the mutex is not yet created and
  2409. // so create it now
  2410. //
  2411. m_hMutex = OpenMutex( SYNCHRONIZE, FALSE, szMutexName );
  2412. if ( m_hMutex == NULL )
  2413. {
  2414. // check the error code why it failed to open
  2415. if ( GetLastError() == ERROR_FILE_NOT_FOUND )
  2416. {
  2417. // create the security descriptor -- just set the
  2418. // Dicretionary Access Control List (DACL)
  2419. // in order to provide security, we will deny WRITE_OWNER and WRITE_DAC
  2420. // permission to Everyone except to the owner
  2421. bResult = ConvertStringSecurityDescriptorToSecurityDescriptor(
  2422. L"D:(D;;WOWD;;;WD)(A;;GA;;;WD)", SDDL_REVISION_1, &psd, NULL );
  2423. if ( bResult == FALSE )
  2424. {
  2425. // we encountered error while creating a security descriptor
  2426. SaveLastError();
  2427. ShowMessage( stderr, GetResString(IDS_ERROR) );
  2428. ShowMessage( stderr, GetReason() );
  2429. return FALSE;
  2430. }
  2431. // initialize the SECURITY_ATTRIBUTES structure
  2432. SecureZeroMemory( &sa, sizeof( SECURITY_ATTRIBUTES ) );
  2433. sa.nLength = sizeof( SECURITY_ATTRIBUTES );
  2434. sa.lpSecurityDescriptor = psd;
  2435. sa.bInheritHandle = FALSE;
  2436. // mutex doesn't exist -- so we need to create it now
  2437. m_hMutex = CreateMutex( &sa, FALSE, szMutexName );
  2438. if (m_hMutex == NULL )
  2439. {
  2440. // we are not able to create the mutex
  2441. // cannot proceed furthur
  2442. SaveLastError();
  2443. ShowMessage( stderr, GetResString(IDS_ERROR) );
  2444. ShowMessage( stderr, GetReason() );
  2445. return FALSE;
  2446. }
  2447. LocalFree(psd);
  2448. }
  2449. else
  2450. {
  2451. // we encounter some error
  2452. // cannot proceed furthur
  2453. SaveLastError();
  2454. ShowMessage( stderr, GetResString(IDS_ERROR) );
  2455. ShowMessage( stderr, GetReason() );
  2456. return FALSE;
  2457. }
  2458. }
  2459. return TRUE;
  2460. }