Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

728 lines
20 KiB

  1. /******************************************************************************
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. EventTriggers.cpp
  5. Abstract:
  6. This module implements the command-line parsing to create/delete/query
  7. EventTriggers on current running on local and remote systems.
  8. Author:
  9. Akhil V. Gokhale (akhil.gokhale@wipro.com)
  10. Revision History:
  11. Akhil V. Gokhale (akhil.gokhale@wipro.com) 03-Oct-2000 (Created.)
  12. ******************************************************************************/
  13. #include "pch.h"
  14. #include "ETCommon.h"
  15. #include "EventTriggers.h"
  16. #include "ShowError.h"
  17. #include "ETCreate.h"
  18. #include "ETDelete.h"
  19. #include "ETQuery.h"
  20. DWORD g_dwOptionFlag;
  21. DWORD __cdecl
  22. _tmain(
  23. IN DWORD argc,
  24. IN LPCTSTR argv[]
  25. )
  26. /*++
  27. Routine Description:
  28. This module reads the input from commond line and calls appropriate
  29. functions to achive to functionality of EventTrigger-Client.
  30. Arguments:
  31. [ in ] argc : argument(s) count specified at the command prompt
  32. [ in ] argv : argument(s) specified at the command prompt
  33. Return Value:
  34. The below are actually not return values but are the exit values
  35. returned to the OS by this application
  36. 0 : utility is successfull
  37. 1 : utility failed
  38. --*/
  39. {
  40. // local variables
  41. CEventTriggers eventTriggers;
  42. BOOL bResult = DIRTY_EXIT; // Programs return value status variable.
  43. g_dwOptionFlag = 0;
  44. TCHAR szErrorMsg[(MAX_RES_STRING*2)+1];
  45. try
  46. {
  47. if( 1 == argc )
  48. {
  49. if( FALSE == IsWin2KOrLater())
  50. {
  51. ShowMessage(stderr,ERROR_OS_INCOMPATIBLE);
  52. }
  53. else
  54. {
  55. // If no command line parameter is given then -query option
  56. // will be taken as default.
  57. g_dwOptionFlag = 3;
  58. CETQuery etQuery(MAX_RES_STRING,
  59. FALSE);
  60. // Initializes variables.
  61. etQuery.Initialize();
  62. // execute query method to query EventTriggers in WMI.
  63. if( TRUE == etQuery.ExecuteQuery ())
  64. {
  65. // as ExecuteQuery routine returns with TRUE,
  66. // exit from program with error level CLEAN_EXIT
  67. bResult = CLEAN_EXIT;
  68. }
  69. }
  70. }
  71. else
  72. {
  73. // As commandline parameter is specified so command parscing will
  74. // required.
  75. // Initialize variables for eventTriggers object.
  76. eventTriggers.Initialize ();
  77. // Process command line parameters.
  78. eventTriggers.ProcessOption(argc,argv);
  79. //if usage option is selected
  80. if( TRUE == eventTriggers.IsUsage() )
  81. {
  82. if(TRUE == eventTriggers.IsCreate())
  83. {
  84. //Display create usage
  85. eventTriggers.ShowCreateUsage ();
  86. }
  87. else if( TRUE == eventTriggers.IsDelete())
  88. {
  89. //Display delete usage
  90. eventTriggers.ShowDeleteUsage ();
  91. }
  92. else if(TRUE == eventTriggers.IsQuery())
  93. {
  94. //Display query usage
  95. eventTriggers.ShowQueryUsage ();
  96. }
  97. else
  98. {
  99. //Display main usage
  100. eventTriggers.ShowMainUsage ();
  101. }
  102. bResult = CLEAN_EXIT;
  103. }
  104. //if user selected create
  105. else if( TRUE == eventTriggers.IsCreate())
  106. {
  107. // creates a object of type CETCreate.
  108. //for create option
  109. g_dwOptionFlag = 1;
  110. CETCreate etCreate(255,
  111. eventTriggers.GetNeedPassword());
  112. // Initializes variables.
  113. etCreate.Initialize ();
  114. // Process command line argument for -create option.
  115. etCreate.ProcessOption (argc,argv);
  116. // execute create method to create EventTriggers in WMI.
  117. if( TRUE == etCreate.ExecuteCreate())
  118. {
  119. // as ExecuteCreate routine returns with TRUE,
  120. // exit from program with error level CLEAN_EXIT
  121. bResult = CLEAN_EXIT;
  122. }
  123. }
  124. //if user selected delete
  125. else if( TRUE == eventTriggers.IsDelete ())
  126. {
  127. // creates a object of type CETDelete.
  128. //for create option
  129. g_dwOptionFlag = 2;
  130. CETDelete etDelete(255,
  131. eventTriggers.GetNeedPassword());
  132. // Initializes variables.
  133. etDelete.Initialize ();
  134. // Process command line argument for -delete option.
  135. etDelete.ProcessOption (argc,argv);
  136. // execute delete method to delete EventTriggers in WMI.
  137. if( TRUE == etDelete.ExecuteDelete())
  138. {
  139. // as ExecuteDelete routine returns with TRUE,
  140. // exit from program with error level CLEAN_EXIT
  141. bResult = CLEAN_EXIT;
  142. }
  143. }
  144. //if user selected -query.
  145. else if( TRUE == eventTriggers.IsQuery())
  146. {
  147. // creates a object of type CETQuery.
  148. //for create option set value to 3
  149. g_dwOptionFlag = 3;
  150. CETQuery etQuery(255,
  151. eventTriggers.GetNeedPassword ());
  152. // Initializes variables.
  153. etQuery.Initialize();
  154. // Process command line argument for -Query option.
  155. etQuery.ProcessOption(argc,argv);
  156. // execute query method to query EventTriggers in WMI.
  157. if( TRUE == etQuery.ExecuteQuery())
  158. {
  159. // as ExecuteQuery routine returns with TRUE,
  160. // exit from program with error level CLEAN_EXIT
  161. bResult = CLEAN_EXIT;
  162. }
  163. }
  164. else
  165. {
  166. // Although this condition will never occure, for safe side
  167. // show error message as "ERROR: Invalid Syntax.
  168. TCHAR szTemp[(MAX_RES_STRING*2)+1];
  169. StringCchPrintfW(szTemp,SIZE_OF_ARRAY(szTemp),
  170. GetResString(IDS_INCORRECT_SYNTAX),GetResString(IDS_UTILITY_NAME));
  171. SetReason(szTemp);
  172. throw CShowError(MK_E_SYNTAX);
  173. }
  174. } // End else
  175. }// try block
  176. catch(CShowError se)
  177. {
  178. // Show Error message on screen depending on value passed through
  179. // through machanism.
  180. StringCchPrintfW(szErrorMsg,SIZE_OF_ARRAY(szErrorMsg),L"%s %s",TAG_ERROR,se.ShowReason());
  181. ShowMessage(stderr,szErrorMsg);
  182. }
  183. catch(CHeap_Exception ch)
  184. {
  185. SetLastError( ERROR_OUTOFMEMORY );
  186. SaveLastError();
  187. StringCchPrintfW(szErrorMsg,SIZE_OF_ARRAY(szErrorMsg),L"%s %s",TAG_ERROR,GetReason());
  188. ShowMessage(stderr, szErrorMsg);
  189. }
  190. // Returns from program with error level stored in bResult.
  191. ReleaseGlobals();
  192. return bResult;
  193. }
  194. CEventTriggers::CEventTriggers()
  195. /*++
  196. Routine Description:
  197. CEventTriggers contructor
  198. Arguments:
  199. NONE
  200. Return Value:
  201. NONE
  202. --*/
  203. {
  204. // init to defaults
  205. m_pszServerNameToShow = NULL;
  206. m_bNeedDisconnect = FALSE;
  207. m_bNeedPassword = FALSE;
  208. m_bUsage = FALSE;
  209. m_bCreate = FALSE;
  210. m_bDelete = FALSE;
  211. m_bQuery = FALSE;
  212. m_arrTemp = NULL;
  213. }
  214. CEventTriggers::~CEventTriggers()
  215. /*++
  216. Routine Description:
  217. CEventTriggers destructor
  218. Arguments:
  219. NONE
  220. Return Value:
  221. NONE
  222. --*/
  223. {
  224. //
  225. // de-allocate memory allocations
  226. //
  227. DESTROY_ARRAY(m_arrTemp);
  228. }
  229. void
  230. CEventTriggers::Initialize()
  231. /*++
  232. Routine Description:
  233. initialize the EventTriggers utility
  234. Arguments:
  235. NONE
  236. Return Value:
  237. NONE
  238. --*/
  239. {
  240. // if at all any occurs, we know that is because of the
  241. // failure in memory allocation ... so set the error
  242. SetLastError( ERROR_OUTOFMEMORY );
  243. SaveLastError();
  244. // Allocates memory
  245. m_arrTemp = CreateDynamicArray();
  246. if( NULL == m_arrTemp)
  247. {
  248. // error occures while allocating required memory, so throw
  249. // exception.
  250. throw CShowError(E_OUTOFMEMORY);
  251. }
  252. SecureZeroMemory(cmdOptions,sizeof(TCMDPARSER2) * MAX_COMMANDLINE_OPTION);
  253. // initialization is successful
  254. SetLastError( NOERROR ); // clear the error
  255. SetReason( L"" ); // clear the reason
  256. }
  257. BOOL
  258. CEventTriggers::ProcessOption(
  259. IN DWORD argc,
  260. IN LPCTSTR argv[]
  261. )
  262. /*++
  263. Routine Description:
  264. This function will process/parce the command line options.
  265. Arguments:
  266. [ in ] argc : argument(s) count specified at the command prompt
  267. [ in ] argv : argument(s) specified at the command prompt
  268. Return Value:
  269. TRUE : On Successful
  270. FALSE : On Error
  271. --*/
  272. {
  273. // local variable
  274. BOOL bReturn = TRUE;// stores return value of function.
  275. TCHAR szTemp[MAX_RES_STRING];
  276. TCHAR szStr [MAX_RES_STRING];
  277. StringCopy(szStr,GetResString(IDS_UTILITY_NAME),SIZE_OF_ARRAY(szStr));
  278. StringCchPrintfW(szTemp,SIZE_OF_ARRAY(szTemp),
  279. GetResString(IDS_INCORRECT_SYNTAX), szStr);
  280. PrepareCMDStruct();
  281. // do the actual parsing of the command line arguments and check the result
  282. bReturn = DoParseParam2( argc, argv,-1,MAX_COMMANDLINE_OPTION, cmdOptions,0 );
  283. if( FALSE == bReturn)
  284. {
  285. // Command line contains invalid parameter(s) so throw exception for
  286. // invalid syntax.
  287. // Valid reason already set in DoParceParam,.
  288. throw CShowError(MK_E_SYNTAX);
  289. }
  290. if(( TRUE == m_bUsage) && argc>3)
  291. {
  292. // Only one option can be accepted along with -? option
  293. // Example: EvTrig.exe -? -query -nh should be invalid.
  294. SetReason(szTemp);
  295. throw CShowError(MK_E_SYNTAX);
  296. }
  297. if((m_bCreate+m_bDelete+m_bQuery)>1)
  298. {
  299. // Only ONE OF the -create -delete and -query can be given as
  300. // valid command line parameter.
  301. SetReason(szTemp);
  302. throw CShowError(MK_E_SYNTAX);
  303. }
  304. else if((2 == argc)&&( TRUE == m_bUsage))
  305. {
  306. // if -? alone given its a valid conmmand line
  307. bReturn = TRUE;
  308. }
  309. else if((argc>=2)&& ( FALSE == m_bCreate)&&
  310. (FALSE == m_bDelete)&&(FALSE == m_bQuery))
  311. {
  312. // If command line argument is equals or greater than 2 atleast one
  313. // of -query OR -create OR -delete should be present in it.
  314. // (for "-?" previous condition already takes care)
  315. // This to prevent from following type of command line argument:
  316. // EvTrig.exe -nh ... Which is a invalid syntax.
  317. SetReason(szTemp);
  318. throw CShowError(MK_E_SYNTAX);
  319. }
  320. // Following checking done if user given command like
  321. // -? -nh OR -? -v , its an invalid syntax.
  322. else if((TRUE == m_bUsage)&&( FALSE == m_bCreate)&&
  323. (FALSE == m_bDelete )&&(FALSE == m_bQuery)&&
  324. (3 == argc))
  325. {
  326. SetReason(szTemp);
  327. throw CShowError(MK_E_SYNTAX);
  328. }
  329. // Any how following variables do not required.
  330. DESTROY_ARRAY(m_arrTemp);
  331. return bReturn;
  332. }
  333. void
  334. CEventTriggers::PrepareCMDStruct()
  335. /*++
  336. Routine Description:
  337. This function will prepare column structure for DoParseParam Function.
  338. Arguments:
  339. none
  340. Return Value:
  341. none
  342. --*/
  343. {
  344. // Filling cmdOptions structure
  345. // -?
  346. StringCopyA( cmdOptions[ ID_HELP ].szSignature, "PARSER2\0", 8 );
  347. cmdOptions[ ID_HELP ].dwType = CP_TYPE_BOOLEAN;
  348. cmdOptions[ ID_HELP ].pwszOptions = szHelpOption;
  349. cmdOptions[ ID_HELP ].dwCount = 1;
  350. cmdOptions[ ID_HELP ].dwActuals = 0;
  351. cmdOptions[ ID_HELP ].dwFlags = CP_USAGE;
  352. cmdOptions[ ID_HELP ].pValue = &m_bUsage;
  353. cmdOptions[ ID_HELP ].dwLength = 0;
  354. // -create
  355. StringCopyA( cmdOptions[ ID_CREATE ].szSignature, "PARSER2\0", 8 );
  356. cmdOptions[ ID_CREATE ].dwType = CP_TYPE_BOOLEAN;
  357. cmdOptions[ ID_CREATE ].pwszOptions = szCreateOption;
  358. cmdOptions[ ID_CREATE ].dwCount = 1;
  359. cmdOptions[ ID_CREATE ].dwActuals = 0;
  360. cmdOptions[ ID_CREATE ].dwFlags = 0;
  361. cmdOptions[ ID_CREATE ].pValue = &m_bCreate;
  362. cmdOptions[ ID_CREATE ].dwLength = 0;
  363. // -delete
  364. StringCopyA( cmdOptions[ ID_DELETE ].szSignature, "PARSER2\0", 8 );
  365. cmdOptions[ ID_DELETE ].dwType = CP_TYPE_BOOLEAN;
  366. cmdOptions[ ID_DELETE ].pwszOptions = szDeleteOption;
  367. cmdOptions[ ID_DELETE ].dwCount = 1;
  368. cmdOptions[ ID_DELETE ].dwActuals = 0;
  369. cmdOptions[ ID_DELETE ].dwFlags = 0;
  370. cmdOptions[ ID_DELETE ].pValue = &m_bDelete;
  371. cmdOptions[ ID_DELETE ].dwLength = 0;
  372. // -query
  373. StringCopyA( cmdOptions[ ID_QUERY ].szSignature, "PARSER2\0", 8 );
  374. cmdOptions[ ID_QUERY ].dwType = CP_TYPE_BOOLEAN;
  375. cmdOptions[ ID_QUERY ].pwszOptions = szQueryOption;
  376. cmdOptions[ ID_QUERY ].dwCount = 1;
  377. cmdOptions[ ID_QUERY ].dwActuals = 0;
  378. cmdOptions[ ID_QUERY ].dwFlags = 0;
  379. cmdOptions[ ID_QUERY ].pValue = &m_bQuery;
  380. cmdOptions[ ID_QUERY ].dwLength = 0;
  381. // default ..
  382. // Although there is no default option for this utility...
  383. // At this moment all the switches other than specified above will be
  384. // treated as default parameter for Main DoParceParam.
  385. // Exact parcing depending on optins (-create -query or -delete) will be done
  386. // at that respective places.
  387. StringCopyA( cmdOptions[ ID_DEFAULT ].szSignature, "PARSER2\0", 8 );
  388. cmdOptions[ ID_DEFAULT].dwType = CP_TYPE_TEXT;
  389. cmdOptions[ ID_DEFAULT ].pwszOptions = NULL;
  390. cmdOptions[ ID_DEFAULT ].pwszFriendlyName = NULL;
  391. cmdOptions[ ID_DEFAULT ].pwszValues = NULL;
  392. cmdOptions[ ID_DEFAULT ].dwCount = 0;
  393. cmdOptions[ ID_DEFAULT ].dwActuals = 0;
  394. cmdOptions[ ID_DEFAULT ].dwFlags = CP2_MODE_ARRAY|CP2_DEFAULT;
  395. cmdOptions[ ID_DEFAULT ].pValue = &m_arrTemp;
  396. cmdOptions[ ID_DEFAULT ].dwLength = 0;
  397. }
  398. void
  399. CEventTriggers::ShowMainUsage()
  400. /*++
  401. Routine Description:
  402. Displays Eventriggers main usage
  403. Arguments:
  404. None
  405. Return Value:
  406. None
  407. --*/
  408. {
  409. // Displaying main usage
  410. for(DWORD dwIndx=IDS_HELP_M1;dwIndx<=IDS_HELP_END;dwIndx++)
  411. {
  412. ShowMessage(stdout,GetResString(dwIndx));
  413. }
  414. }
  415. BOOL
  416. CEventTriggers::GetNeedPassword()
  417. /*++
  418. Routine Description:
  419. Returns whether to ask for password or not.
  420. Arguments:
  421. None
  422. Return Value:
  423. BOOL
  424. --*/
  425. {
  426. return m_bNeedPassword;
  427. }
  428. BOOL
  429. CEventTriggers::IsCreate()
  430. /*++
  431. Routine Description:
  432. Returns if create option is selected.
  433. Arguments:
  434. None
  435. Return Value:
  436. BOOL
  437. --*/
  438. {
  439. return m_bCreate;
  440. }
  441. BOOL
  442. CEventTriggers::IsUsage()
  443. /*++
  444. Routine Description:
  445. Returns if usage option is selected.
  446. Arguments:
  447. None
  448. Return Value:
  449. BOOL
  450. --*/
  451. {
  452. return m_bUsage;
  453. }
  454. BOOL
  455. CEventTriggers::IsDelete()
  456. /*++
  457. Routine Description:
  458. Returns if delete option is selected.
  459. Arguments:
  460. None
  461. Return Value:
  462. BOOL
  463. --*/
  464. {
  465. return m_bDelete;
  466. }
  467. BOOL
  468. CEventTriggers::IsQuery()
  469. /*++
  470. Routine Description:
  471. Returns if Query option is selected.
  472. Arguments:
  473. None
  474. Return Value:
  475. BOOL
  476. --*/
  477. {
  478. return m_bQuery;
  479. }
  480. void
  481. CEventTriggers::ShowCreateUsage()
  482. /*++
  483. Routine Description
  484. This function shows help message for EventTriggers utility for
  485. -create operation
  486. Arguments:
  487. NONE
  488. Return Value
  489. None
  490. --*/
  491. {
  492. // Displaying Create usage
  493. for(int iIndx=IDS_HELP_C1;iIndx<=IDS_HELP_CREATE_END;iIndx++)
  494. {
  495. ShowMessage(stdout,GetResString(iIndx));
  496. }
  497. return;
  498. }
  499. void
  500. CEventTriggers::ShowDeleteUsage()
  501. /*++
  502. Routine Description
  503. This function shows help message for EventTriggers utility for
  504. -delete operation
  505. Arguments:
  506. NONE
  507. Return Value
  508. None
  509. --*/
  510. {
  511. for(int iIndx=IDS_HELP_D1;iIndx<=IDS_HELP_DELETE_END;iIndx++)
  512. {
  513. ShowMessage(stdout,GetResString(iIndx));
  514. }
  515. return;
  516. }
  517. void
  518. CEventTriggers::ShowQueryUsage()
  519. /*++
  520. Routine Description
  521. This function shows help message for EventTriggers utility for
  522. -query operation
  523. Arguments:
  524. NONE
  525. Return Value
  526. None
  527. --*/
  528. {
  529. for(int iIndx=IDS_HELP_Q1;iIndx<=IDS_HELP_QUERY_END;iIndx++)
  530. {
  531. ShowMessage(stdout,GetResString(iIndx));
  532. }
  533. return;
  534. }
  535. HRESULT
  536. PropertyGet1(
  537. IN IWbemClassObject* pWmiObject,
  538. IN LPCTSTR szProperty,
  539. IN OUT LPVOID pValue,
  540. IN DWORD dwSize
  541. )
  542. /*++
  543. Routine Description:
  544. Get the value of a property for the given instance .
  545. Arguments:
  546. [in] pWmiObject - A pointer to wmi class.
  547. [in] szProperty - property name whose value to be returned.
  548. [in out] pValue - Variable to hold the data.
  549. [in] dwSize - size of the variable.
  550. Return Value:
  551. HRESULT value.
  552. --*/
  553. {
  554. // local variables
  555. HRESULT hr = S_OK;
  556. VARIANT varValue;
  557. DEBUG_INFO;
  558. // value should not be NULL
  559. if ( NULL == pValue )
  560. {
  561. SetLastError(ERROR_INVALID_PARAMETER);
  562. SaveLastError();
  563. return S_FALSE;
  564. }
  565. // initialize the values with zeros ... to be on safe side
  566. SecureZeroMemory( pValue,dwSize );
  567. // initialize the variant and then get the value of the specified property
  568. VariantInit( &varValue );
  569. hr = pWmiObject->Get( szProperty, 0, &varValue, NULL, NULL );
  570. if ( FAILED( hr ) )
  571. {
  572. // clear the variant variable
  573. VariantClear( &varValue );
  574. // failed to get the value for the property
  575. return hr;
  576. }
  577. // get and put the value
  578. switch( varValue.vt )
  579. {
  580. case VT_EMPTY:
  581. case VT_NULL:
  582. break;
  583. case VT_I2:
  584. *( ( short* ) pValue ) = V_I2( &varValue );
  585. break;
  586. case VT_I4:
  587. *( ( long* ) pValue ) = V_I4( &varValue );
  588. break;
  589. case VT_R4:
  590. *( ( float* ) pValue ) = V_R4( &varValue );
  591. break;
  592. case VT_R8:
  593. *( ( double* ) pValue ) = V_R8( &varValue );
  594. break;
  595. case VT_UI1:
  596. *( ( UINT* ) pValue ) = V_UI1( &varValue );
  597. break;
  598. case VT_BSTR:
  599. {
  600. // get the unicode value
  601. LPWSTR pszTmp = V_BSTR(&varValue);
  602. StringCopy((LPWSTR)pValue,pszTmp,dwSize);
  603. break;
  604. }
  605. }
  606. // clear the variant variable
  607. if(FAILED(VariantClear( &varValue )))
  608. {
  609. return E_FAIL;
  610. }
  611. // inform success
  612. return S_OK;
  613. }