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.

1276 lines
45 KiB

  1. /*****************************************************************************
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. ETCreate.CPP
  5. Abstract:
  6. This module is intended to have the functionality for EVENTTRIGGERS.EXE
  7. with -create parameter.
  8. This will Create Event Triggers in local / remote system.
  9. Author:
  10. Akhil Gokhale 03-Oct.-2000 (Created it)
  11. Revision History:
  12. ******************************************************************************/
  13. #include "pch.h"
  14. #include "ETCommon.h"
  15. #include "resource.h"
  16. #include "ShowError.h"
  17. #include "ETCreate.h"
  18. #include "WMI.h"
  19. #define NTAUTHORITY_USER L"NT AUTHORITY\\SYSTEM"
  20. #define SYSTEM_USER L"SYSTEM"
  21. //***************************************************************************
  22. // Routine Description:
  23. // Class constructor
  24. //
  25. // Arguments:
  26. // None
  27. // Return Value:
  28. // None
  29. //
  30. //***************************************************************************
  31. CETCreate::CETCreate()
  32. {
  33. m_pszServerName = NULL;
  34. m_pszUserName = NULL;
  35. m_pszPassword = NULL;
  36. m_pszTriggerName = NULL;
  37. m_arrLogNames = NULL;
  38. m_pszType = NULL;
  39. m_pszSource = NULL;
  40. m_pszDescription = NULL;
  41. m_pszTaskName = NULL;
  42. m_pszRunAsUserName = NULL;
  43. m_pszRunAsUserPassword = NULL;
  44. m_bNeedPassword = FALSE;
  45. m_bCreate = FALSE;
  46. m_bIsCOMInitialize = FALSE;
  47. m_lMinMemoryReq = 0;
  48. m_pWbemLocator = NULL;
  49. m_pWbemServices = NULL;
  50. m_pEnumObjects = NULL;
  51. m_pAuthIdentity = NULL;
  52. m_pClass = NULL;
  53. m_pOutInst = NULL;
  54. m_pInClass = NULL;
  55. m_pInInst = NULL;
  56. m_pszWMIQueryString = NULL;
  57. bstrTemp = NULL;
  58. m_hStdHandle = NULL;
  59. m_pEnumWin32_NTEventLogFile = NULL;
  60. }
  61. //***************************************************************************
  62. // Routine Description:
  63. // Class constructor
  64. //
  65. // Arguments:
  66. // None
  67. // Return Value:
  68. // None
  69. //
  70. //***************************************************************************
  71. CETCreate::CETCreate(LONG lMinMemoryReq,BOOL bNeedPassword)
  72. {
  73. m_pszServerName = NULL;
  74. m_pszUserName = NULL;
  75. m_pszPassword = NULL;
  76. m_pszTriggerName = NULL;
  77. m_arrLogNames = NULL;
  78. m_pszType = NULL;
  79. m_pszSource = NULL;
  80. m_pszDescription = NULL;
  81. m_pszTaskName = NULL;
  82. m_bNeedPassword = bNeedPassword;
  83. m_bCreate = FALSE;
  84. m_dwID = 0;
  85. m_pszRunAsUserName = NULL;
  86. m_pszRunAsUserPassword = NULL;
  87. m_bIsCOMInitialize = FALSE;
  88. m_pWbemLocator = NULL;
  89. m_pWbemServices = NULL;
  90. m_pEnumObjects = NULL;
  91. m_pAuthIdentity = NULL;
  92. m_pClass = NULL;
  93. m_pOutInst = NULL;
  94. m_pInClass = NULL;
  95. m_pInInst = NULL;
  96. bstrTemp = NULL;
  97. m_hStdHandle = NULL;
  98. m_pszWMIQueryString = NULL;
  99. m_lMinMemoryReq = lMinMemoryReq;
  100. m_pEnumWin32_NTEventLogFile = NULL;
  101. }
  102. //***************************************************************************
  103. // Routine Description:
  104. // Class destructor
  105. //
  106. // Arguments:
  107. // None
  108. // Return Value:
  109. // None
  110. //
  111. //***************************************************************************
  112. CETCreate::~CETCreate()
  113. {
  114. // Release all memory which is allocated.
  115. RELEASE_MEMORY_EX(m_pszServerName);
  116. RELEASE_MEMORY_EX(m_pszUserName);
  117. RELEASE_MEMORY_EX(m_pszPassword);
  118. RELEASE_MEMORY_EX(m_pszTriggerName);
  119. DESTROY_ARRAY(m_arrLogNames);
  120. RELEASE_MEMORY_EX(m_pszType);
  121. RELEASE_MEMORY_EX(m_pszSource);
  122. RELEASE_MEMORY_EX(m_pszDescription);
  123. RELEASE_MEMORY_EX(m_pszTaskName);
  124. RELEASE_MEMORY_EX(m_pszWMIQueryString);
  125. RELEASE_MEMORY_EX(m_pszRunAsUserName);
  126. RELEASE_MEMORY_EX(m_pszRunAsUserPassword);
  127. SAFE_RELEASE_INTERFACE(m_pWbemLocator);
  128. SAFE_RELEASE_INTERFACE(m_pWbemServices);
  129. SAFE_RELEASE_INTERFACE(m_pEnumObjects);
  130. SAFE_RELEASE_INTERFACE(m_pClass);
  131. SAFE_RELEASE_INTERFACE(m_pOutInst);
  132. SAFE_RELEASE_INTERFACE(m_pInClass);
  133. SAFE_RELEASE_INTERFACE(m_pInInst);
  134. SAFE_RELEASE_INTERFACE(m_pEnumWin32_NTEventLogFile);
  135. // Uninitialize COM only if it is initialized.
  136. if(m_bIsCOMInitialize == TRUE)
  137. {
  138. CoUninitialize();
  139. }
  140. }
  141. // ***************************************************************************
  142. // Routine Description:
  143. // Allocates and initialize variables.
  144. //
  145. // Arguments:
  146. // NONE
  147. //
  148. // Return Value:
  149. // NONE
  150. //
  151. //***************************************************************************
  152. void
  153. CETCreate::Initialize()
  154. {
  155. // local variable
  156. LONG lTemp = 0;
  157. // if at all any occurs, we know that is 'coz of the
  158. // failure in memory allocation ... so set the error
  159. SetLastError( E_OUTOFMEMORY );
  160. SaveLastError();
  161. // allocate memory at least MAX_COMPUTER_NAME_LENGTH+1 (its windows )
  162. // constant
  163. lTemp = (m_lMinMemoryReq>MAX_COMPUTERNAME_LENGTH)?
  164. m_lMinMemoryReq:MAX_COMPUTERNAME_LENGTH+1;
  165. m_pszServerName = new TCHAR[lTemp+1];
  166. CheckAndSetMemoryAllocation (m_pszServerName,lTemp);
  167. // allocate memory at least MAX_USERNAME_LENGTH+1 (its windows )
  168. // constant
  169. lTemp = (m_lMinMemoryReq>MAX_USERNAME_LENGTH)?
  170. m_lMinMemoryReq:MAX_USERNAME_LENGTH+1;
  171. m_pszUserName = new TCHAR[lTemp+1];
  172. CheckAndSetMemoryAllocation (m_pszUserName,lTemp);
  173. m_pszRunAsUserName = new TCHAR[lTemp+1];
  174. CheckAndSetMemoryAllocation (m_pszRunAsUserName,lTemp);
  175. // allocate memory at least MAX_PASSWORD_LENGTH+1 (its windows )
  176. // constant
  177. lTemp = (m_lMinMemoryReq>MAX_PASSWORD_LENGTH)?
  178. m_lMinMemoryReq:MAX_PASSWORD_LENGTH+1;
  179. m_pszPassword = new TCHAR[lTemp+1];
  180. CheckAndSetMemoryAllocation (m_pszPassword,lTemp);
  181. m_pszRunAsUserPassword = new TCHAR[lTemp+1];
  182. CheckAndSetMemoryAllocation (m_pszRunAsUserPassword,lTemp);
  183. m_pszTriggerName = new TCHAR[m_lMinMemoryReq+1];
  184. CheckAndSetMemoryAllocation (m_pszTriggerName,m_lMinMemoryReq);
  185. m_pszType = new TCHAR[m_lMinMemoryReq+1];
  186. CheckAndSetMemoryAllocation (m_pszType,m_lMinMemoryReq);
  187. m_pszSource = new TCHAR[m_lMinMemoryReq+1];
  188. CheckAndSetMemoryAllocation (m_pszSource,m_lMinMemoryReq);
  189. m_pszDescription = new TCHAR[m_lMinMemoryReq+1];
  190. CheckAndSetMemoryAllocation (m_pszDescription,m_lMinMemoryReq);
  191. m_pszTaskName = new TCHAR[m_lMinMemoryReq+1];
  192. CheckAndSetMemoryAllocation (m_pszTaskName,m_lMinMemoryReq);
  193. m_pszWMIQueryString = new TCHAR[(MAX_RES_STRING*2)+1];
  194. CheckAndSetMemoryAllocation (m_pszWMIQueryString,(MAX_RES_STRING*2));
  195. m_arrLogNames = CreateDynamicArray();
  196. if(m_arrLogNames == NULL)
  197. {
  198. throw CShowError(E_OUTOFMEMORY);
  199. }
  200. // initialization is successful
  201. SetLastError( NOERROR ); // clear the error
  202. SetReason( NULL_STRING ); // clear the reason
  203. return;
  204. }
  205. // ***************************************************************************
  206. // Routine Description:
  207. // Function will allocate memory to a string
  208. //
  209. // Arguments:
  210. // [in][out] pszStr : String variable to which memory to be allocated
  211. // [in] : Number of bytes to be allocated.
  212. // Return Value:
  213. // NONE
  214. //
  215. //***************************************************************************
  216. void
  217. CETCreate::CheckAndSetMemoryAllocation(LPTSTR pszStr,LONG lSize)
  218. {
  219. if(pszStr == NULL)
  220. {
  221. throw CShowError(E_OUTOFMEMORY);
  222. }
  223. // init to ZERO's
  224. ZeroMemory( pszStr, lSize * sizeof( TCHAR ) );
  225. }
  226. // ***************************************************************************
  227. // Routine Description:
  228. // This function will process/parce the command line options.
  229. //
  230. // Arguments:
  231. // [ in ] argc : argument(s) count specified at the command prompt
  232. // [ in ] argv : argument(s) specified at the command prompt
  233. //
  234. // Return Value:
  235. // none
  236. // ***************************************************************************
  237. void
  238. CETCreate::ProcessOption(DWORD argc, LPCTSTR argv[])
  239. {
  240. // local variable
  241. BOOL bReturn = TRUE;
  242. CHString szTempString;
  243. PrepareCMDStruct();
  244. // initialize the password variable with '*'
  245. if ( m_pszPassword != NULL )
  246. lstrcpy( m_pszPassword, _T( "*" ) );
  247. // do the actual parsing of the command line arguments and check the result
  248. bReturn = DoParseParam( argc, argv, MAX_COMMANDLINE_C_OPTION, cmdOptions);
  249. if(bReturn == FALSE)
  250. throw CShowError(MK_E_SYNTAX);
  251. // empty Server is not valid
  252. // At least any of -so , -t OR -i should be given .
  253. if((cmdOptions[ ID_C_SOURCE].dwActuals == 0) &&
  254. (cmdOptions[ ID_C_TYPE].dwActuals == 0) &&
  255. (cmdOptions[ ID_C_ID ].dwActuals == 0))
  256. {
  257. throw CShowError(IDS_ID_TYPE_SOURCE);
  258. }
  259. if ( (cmdOptions[ID_C_SERVER].dwActuals != 0) &&
  260. (lstrlen( m_pszServerName) == 0 ))
  261. {
  262. szTempString = m_pszServerName;
  263. szTempString.TrimRight();
  264. szTempString.TrimLeft();
  265. lstrcpy(m_pszServerName,(LPCWSTR)szTempString);
  266. throw CShowError(IDS_ERROR_SERVERNAME_EMPTY);
  267. }
  268. // "-u" should not be specified without "-s"
  269. if ( cmdOptions[ ID_C_SERVER ].dwActuals == 0 &&
  270. cmdOptions[ ID_C_USERNAME ].dwActuals != 0 )
  271. {
  272. throw CShowError(IDS_ERROR_USERNAME_BUT_NOMACHINE);
  273. }
  274. if ( (cmdOptions[ID_C_USERNAME].dwActuals != 0) &&
  275. (lstrlen( m_pszUserName) == 0 ))
  276. {
  277. // empty user is not valid
  278. szTempString = m_pszUserName;
  279. szTempString.TrimRight();
  280. szTempString.TrimLeft();
  281. lstrcpy(m_pszUserName,(LPCWSTR)szTempString);
  282. throw CShowError(IDS_ERROR_USERNAME_EMPTY);
  283. }
  284. // "-p" should not be specified without -u
  285. if ( (cmdOptions[ID_C_USERNAME].dwActuals == 0) &&
  286. (cmdOptions[ID_C_PASSWORD].dwActuals != 0 ))
  287. {
  288. // invalid syntax
  289. throw CShowError(IDS_USERNAME_REQUIRED);
  290. }
  291. if(cmdOptions[ ID_C_RU].dwActuals==1)
  292. {
  293. // Trim all blank spaces before and after
  294. szTempString = m_pszRunAsUserName;
  295. szTempString.TrimRight();
  296. szTempString.TrimLeft();
  297. lstrcpy(m_pszRunAsUserName,(LPCWSTR)szTempString);
  298. }
  299. // "-rp" should not be specified without -ru
  300. if ( (cmdOptions[ID_C_RU].dwActuals == 0) &&
  301. (cmdOptions[ID_C_RP].dwActuals != 0 ))
  302. {
  303. // invalid syntax
  304. throw CShowError(IDS_RUN_AS_USERNAME_REQUIRED);
  305. }
  306. // check whether caller should accept the password or not
  307. // if user has specified -s (or) -u and no "-p", then utility should accept
  308. // password the user will be prompter for the password only if establish
  309. // connection is failed without the credentials information
  310. if ( cmdOptions[ ID_C_PASSWORD ].dwActuals != 0 &&
  311. m_pszPassword != NULL && lstrcmp( m_pszPassword, _T( "*" ) ) == 0 )
  312. {
  313. // user wants the utility to prompt for the password before trying to connect
  314. m_bNeedPassword = TRUE;
  315. }
  316. else if ( cmdOptions[ ID_C_PASSWORD ].dwActuals == 0 &&
  317. ( cmdOptions[ ID_C_SERVER ].dwActuals != 0 || cmdOptions[ ID_C_USERNAME ].dwActuals != 0 ) )
  318. {
  319. // -s, -u is specified without password ...
  320. // utility needs to try to connect first and if it fails then prompt for the password
  321. m_bNeedPassword = TRUE;
  322. if ( m_pszPassword != NULL )
  323. {
  324. lstrcpy( m_pszPassword, _T( "" ) );
  325. }
  326. }
  327. // Check if -d (Description) is given and if given it cannot be empty
  328. if(cmdOptions[ ID_C_DESCRIPTION].dwActuals==1)
  329. {
  330. szTempString = m_pszDescription;
  331. szTempString.TrimRight();
  332. szTempString.TrimLeft();
  333. lstrcpy(m_pszDescription,(LPCWSTR)szTempString);
  334. if(lstrlen(m_pszDescription)==0)
  335. throw CShowError(IDS_ID_DESC_EMPTY);
  336. }
  337. // Check if -so (source) is given and if given it cannot be empty
  338. if(cmdOptions[ ID_C_SOURCE].dwActuals==1)
  339. {
  340. szTempString = m_pszSource;
  341. szTempString.TrimRight();
  342. szTempString.TrimLeft();
  343. lstrcpy(m_pszSource,(LPCWSTR)szTempString);
  344. if(lstrlen(m_pszSource)==0)
  345. throw CShowError(IDS_ID_SOURCE_EMPTY);
  346. }
  347. // Check if -l (log) is given and if given it cannot be empty
  348. if(cmdOptions[ ID_C_LOGNAME].dwActuals!=0)
  349. {
  350. DWORD dwIndx = 0;
  351. DWORD dNoOfLogNames = DynArrayGetCount( m_arrLogNames );
  352. for(;dwIndx<dNoOfLogNames;dwIndx++)
  353. {
  354. szTempString = DynArrayItemAsString(m_arrLogNames,dwIndx);
  355. szTempString.TrimRight();
  356. szTempString.TrimLeft();
  357. if(szTempString.GetLength()==0)
  358. throw CShowError(IDS_ID_LOG_EMPTY);
  359. }
  360. }
  361. // checks for -tk (taskname) It must be there and should not be empty
  362. szTempString = m_pszTaskName;
  363. szTempString.TrimRight();
  364. szTempString.TrimLeft();
  365. lstrcpy(m_pszTaskName,(LPCWSTR)szTempString);
  366. if((cmdOptions[ ID_C_TASK ].dwActuals == 0) ||
  367. (lstrlen(m_pszTaskName) == 0))
  368. {
  369. throw CShowError(IDS_ID_TK_NAME_MISSING);
  370. }
  371. // checks for -tr (Triggername). It must be there and should not be empty
  372. szTempString = m_pszTriggerName;
  373. szTempString.TrimRight();
  374. szTempString.TrimLeft();
  375. lstrcpy(m_pszTriggerName,(LPCWSTR)szTempString);
  376. if((cmdOptions[ ID_C_TRIGGERNAME ].dwActuals == 0) ||
  377. (lstrlen(m_pszTriggerName) == 0))
  378. {
  379. throw CShowError(IDS_ID_TRIG_NAME_MISSING);
  380. }
  381. // Check for invalid trigger name
  382. szTempString = m_pszTriggerName;
  383. szTempString.TrimRight();
  384. szTempString.TrimLeft();
  385. if(szTempString.FindOneOf(L"[]:|<>+=;,?$#{}~^'@`!()*%\\/")!=-1)
  386. {
  387. throw CShowError(IDS_ID_INVALID_TRIG_NAME);
  388. }
  389. // with -i option only positive integers
  390. if( (cmdOptions[ ID_C_ID ].dwActuals == 1) &&
  391. ((m_dwID ==0)||(m_dwID>USHRT_MAX)))
  392. {
  393. throw CShowError(IDS_INVALID_ID);
  394. }
  395. }
  396. // ***************************************************************************
  397. // Routine Description:
  398. // This function will prepare column structure for DoParseParam Function.
  399. //
  400. // Arguments:
  401. // none
  402. // Return Value:
  403. // none
  404. // ***************************************************************************
  405. void
  406. CETCreate::PrepareCMDStruct()
  407. {
  408. // Filling cmdOptions structure
  409. // -create
  410. lstrcpy(cmdOptions[ ID_C_CREATE ].szOption,OPTION_CREATE);
  411. cmdOptions[ ID_C_CREATE ].dwFlags = CP_MAIN_OPTION;
  412. cmdOptions[ ID_C_CREATE ].dwCount = 1;
  413. cmdOptions[ ID_C_CREATE ].dwActuals = 0;
  414. cmdOptions[ ID_C_CREATE ].pValue = &m_bCreate;
  415. lstrcpy(cmdOptions[ ID_C_CREATE ].szValues,NULL_STRING);
  416. cmdOptions[ ID_C_CREATE ].pFunction = NULL;
  417. cmdOptions[ ID_C_CREATE ].pFunctionData = NULL;
  418. cmdOptions[ ID_C_CREATE ].pFunctionData = NULL;
  419. // -s (servername)
  420. lstrcpy(cmdOptions[ ID_C_SERVER ].szOption,OPTION_SERVER);
  421. cmdOptions[ ID_C_SERVER ].dwFlags = CP_TYPE_TEXT|CP_VALUE_MANDATORY;
  422. cmdOptions[ ID_C_SERVER ].dwCount = 1;
  423. cmdOptions[ ID_C_SERVER ].dwActuals = 0;
  424. cmdOptions[ ID_C_SERVER ].pValue = m_pszServerName;
  425. lstrcpy(cmdOptions[ ID_C_SERVER ].szValues,NULL_STRING);
  426. cmdOptions[ ID_C_SERVER ].pFunction = NULL;
  427. cmdOptions[ ID_C_SERVER ].pFunctionData = NULL;
  428. // -u (username)
  429. lstrcpy(cmdOptions[ ID_C_USERNAME ].szOption,OPTION_USERNAME);
  430. cmdOptions[ ID_C_USERNAME ].dwFlags = CP_TYPE_TEXT|CP_VALUE_MANDATORY;
  431. cmdOptions[ ID_C_USERNAME ].dwCount = 1;
  432. cmdOptions[ ID_C_USERNAME ].dwActuals = 0;
  433. cmdOptions[ ID_C_USERNAME ].pValue = m_pszUserName;
  434. lstrcpy(cmdOptions[ ID_C_USERNAME ].szValues,NULL_STRING);
  435. cmdOptions[ ID_C_USERNAME ].pFunction = NULL;
  436. cmdOptions[ ID_C_USERNAME ].pFunctionData = NULL;
  437. // -p (password)
  438. lstrcpy(cmdOptions[ ID_C_PASSWORD ].szOption,OPTION_PASSWORD);
  439. cmdOptions[ ID_C_PASSWORD ].dwFlags = CP_TYPE_TEXT|CP_VALUE_OPTIONAL;
  440. cmdOptions[ ID_C_PASSWORD ].dwCount = 1;
  441. cmdOptions[ ID_C_PASSWORD ].dwActuals = 0;
  442. cmdOptions[ ID_C_PASSWORD ].pValue = m_pszPassword;
  443. lstrcpy(cmdOptions[ ID_C_PASSWORD ].szValues,NULL_STRING);
  444. cmdOptions[ ID_C_PASSWORD ].pFunction = NULL;
  445. cmdOptions[ ID_C_PASSWORD ].pFunctionData = NULL;
  446. // -tr
  447. lstrcpy(cmdOptions[ ID_C_TRIGGERNAME ].szOption,OPTION_TRIGGERNAME);
  448. cmdOptions[ ID_C_TRIGGERNAME ].dwFlags = CP_MANDATORY|CP_TYPE_TEXT|CP_VALUE_MANDATORY;
  449. cmdOptions[ ID_C_TRIGGERNAME ].dwCount = 1;
  450. cmdOptions[ ID_C_TRIGGERNAME ].dwActuals = 0;
  451. cmdOptions[ ID_C_TRIGGERNAME ].pValue = m_pszTriggerName;
  452. lstrcpy(cmdOptions[ ID_C_TRIGGERNAME ].szValues,NULL_STRING);
  453. cmdOptions[ ID_C_TRIGGERNAME ].pFunction = NULL;
  454. cmdOptions[ ID_C_TRIGGERNAME ].pFunctionData = NULL;
  455. //-l
  456. lstrcpy(cmdOptions[ ID_C_LOGNAME ].szOption,OPTION_LOGNAME);
  457. cmdOptions[ ID_C_LOGNAME ].dwFlags = CP_TYPE_TEXT | CP_VALUE_MANDATORY|
  458. CP_MODE_ARRAY|CP_VALUE_NODUPLICATES;
  459. cmdOptions[ ID_C_LOGNAME ].dwCount = 0;
  460. cmdOptions[ ID_C_LOGNAME ].dwActuals = 0;
  461. cmdOptions[ ID_C_LOGNAME ].pValue = &m_arrLogNames ;
  462. lstrcpy(cmdOptions[ ID_C_LOGNAME ].szValues,NULL_STRING);
  463. cmdOptions[ ID_C_LOGNAME ].pFunction = NULL;
  464. cmdOptions[ ID_C_LOGNAME ].pFunctionData = NULL;
  465. // -eid
  466. lstrcpy(cmdOptions[ ID_C_ID ].szOption,OPTION_EID);
  467. cmdOptions[ ID_C_ID ].dwFlags = CP_TYPE_UNUMERIC|CP_VALUE_MANDATORY;
  468. cmdOptions[ ID_C_ID ].dwCount = 1;
  469. cmdOptions[ ID_C_ID ].dwActuals = 0;
  470. cmdOptions[ ID_C_ID ].pValue = &m_dwID;
  471. lstrcpy(cmdOptions[ ID_C_ID ].szValues,NULL_STRING);
  472. cmdOptions[ ID_C_ID ].pFunction = NULL;
  473. cmdOptions[ ID_C_ID ].pFunctionData = NULL;
  474. // -t (type)
  475. lstrcpy(cmdOptions[ ID_C_TYPE ].szOption,OPTION_TYPE);
  476. cmdOptions[ ID_C_TYPE ].dwFlags = CP_TYPE_TEXT|CP_VALUE_MANDATORY|
  477. CP_MODE_VALUES;
  478. cmdOptions[ ID_C_TYPE].dwCount = 1;
  479. cmdOptions[ ID_C_TYPE].dwActuals = 0;
  480. cmdOptions[ ID_C_TYPE].pValue = m_pszType ;
  481. lstrcpy(cmdOptions[ ID_C_TYPE].szValues,GetResString(IDS_TYPE_OPTIONS));
  482. cmdOptions[ ID_C_TYPE].pFunction = NULL;
  483. cmdOptions[ ID_C_TYPE].pFunctionData = NULL;
  484. // -so (source)
  485. lstrcpy(cmdOptions[ ID_C_SOURCE].szOption,OPTION_SOURCE);
  486. cmdOptions[ ID_C_SOURCE].dwFlags = CP_TYPE_TEXT|CP_VALUE_MANDATORY;
  487. cmdOptions[ ID_C_SOURCE ].dwCount = 1;
  488. cmdOptions[ ID_C_SOURCE].dwActuals = 0;
  489. cmdOptions[ ID_C_SOURCE].pValue = m_pszSource ;
  490. lstrcpy(cmdOptions[ ID_C_SOURCE].szValues,NULL_STRING);
  491. cmdOptions[ ID_C_SOURCE].pFunction = NULL;
  492. cmdOptions[ ID_C_SOURCE].pFunctionData = NULL;
  493. // -d (description)
  494. lstrcpy(cmdOptions[ ID_C_DESCRIPTION].szOption,OPTION_DESCRIPTION);
  495. cmdOptions[ ID_C_DESCRIPTION].dwFlags = CP_TYPE_TEXT|CP_VALUE_MANDATORY;
  496. cmdOptions[ ID_C_DESCRIPTION].dwCount = 1;
  497. cmdOptions[ ID_C_DESCRIPTION].dwActuals = 0;
  498. cmdOptions[ ID_C_DESCRIPTION].pValue = m_pszDescription ;
  499. lstrcpy(cmdOptions[ ID_C_DESCRIPTION].szValues,NULL_STRING);
  500. cmdOptions[ ID_C_DESCRIPTION].pFunction = NULL;
  501. cmdOptions[ ID_C_DESCRIPTION].pFunctionData = NULL;
  502. // -tk (task)
  503. lstrcpy(cmdOptions[ ID_C_TASK].szOption,OPTION_TASK);
  504. cmdOptions[ ID_C_TASK].dwFlags = CP_MANDATORY|CP_TYPE_TEXT|CP_VALUE_MANDATORY;
  505. cmdOptions[ ID_C_TASK].dwCount = 1;
  506. cmdOptions[ ID_C_TASK].dwActuals = 0;
  507. cmdOptions[ ID_C_TASK].pValue = m_pszTaskName;
  508. lstrcpy(cmdOptions[ ID_C_TASK].szValues,NULL_STRING);
  509. cmdOptions[ ID_C_TASK].pFunction = NULL;
  510. cmdOptions[ ID_C_TASK].pFunctionData = NULL;
  511. // -ru (RunAsUserName)
  512. lstrcpy(cmdOptions[ ID_C_RU].szOption,OPTION_RU);
  513. cmdOptions[ ID_C_RU ].dwFlags = CP_TYPE_TEXT|CP_VALUE_MANDATORY;
  514. cmdOptions[ ID_C_RU ].dwCount = 1;
  515. cmdOptions[ ID_C_RU ].dwActuals = 0;
  516. cmdOptions[ ID_C_RU ].pValue = m_pszRunAsUserName;
  517. lstrcpy(cmdOptions[ ID_C_RU ].szValues,NULL_STRING);
  518. cmdOptions[ ID_C_RU ].pFunction = NULL;
  519. cmdOptions[ ID_C_RU ].pFunctionData = NULL;
  520. lstrcpy(m_pszRunAsUserPassword,_T("*"));
  521. // -pp (Run As User password)
  522. lstrcpy(cmdOptions[ ID_C_RP ].szOption,OPTION_RP);
  523. cmdOptions[ ID_C_RP ].dwFlags = CP_TYPE_TEXT|CP_VALUE_OPTIONAL;
  524. cmdOptions[ ID_C_RP ].dwCount = 1;
  525. cmdOptions[ ID_C_RP ].dwActuals = 0;
  526. cmdOptions[ ID_C_RP ].pValue = m_pszRunAsUserPassword;
  527. lstrcpy(cmdOptions[ ID_C_RP ].szValues,NULL_STRING);
  528. cmdOptions[ ID_C_RP ].pFunction = NULL;
  529. cmdOptions[ ID_C_RP ].pFunctionData = NULL;
  530. }
  531. //***************************************************************************
  532. // Routine Description:
  533. // This routine will actualy creates eventtrigers in WMI.
  534. //
  535. // Arguments:
  536. // None
  537. // Return Value:
  538. // None
  539. //
  540. //***************************************************************************
  541. BOOL
  542. CETCreate::ExecuteCreate()
  543. {
  544. // local variables...
  545. BOOL bResult = FALSE;// Stores return status of function
  546. HRESULT hr = 0; // Stores return code.
  547. BOOL bReturn = FALSE; // return value for function
  548. TCHAR szErrorMsg[MAX_RES_STRING+1];
  549. try
  550. {
  551. // Initialize COM
  552. InitializeCom(&m_pWbemLocator);
  553. // make m_bIsCOMInitialize to true which will be useful when
  554. // uninitialize COM.
  555. m_bIsCOMInitialize = TRUE;
  556. { // brackets used to restrict scope of following declered variables.
  557. CHString szTempUser = m_pszUserName; // Temp. variabe to store user
  558. // name.
  559. CHString szTempPassword = m_pszPassword;// Temp. variable to store
  560. // password.
  561. m_bLocalSystem = TRUE;
  562. // Connect remote / local WMI.
  563. bResult = ConnectWmiEx( m_pWbemLocator,
  564. &m_pWbemServices,
  565. m_pszServerName,
  566. szTempUser,
  567. szTempPassword,
  568. &m_pAuthIdentity,
  569. m_bNeedPassword,
  570. WMI_NAMESPACE_CIMV2,
  571. &m_bLocalSystem);
  572. if(bResult == FALSE)
  573. {
  574. // ConnectWmiEx will set for reason for failure.
  575. TCHAR szErrorMsg[MAX_RES_STRING+1];
  576. DISPLAY_MESSAGE2( stderr, szErrorMsg, L"%s %s", TAG_ERROR,
  577. GetReason());
  578. return FALSE;
  579. }
  580. // check the remote system version and its compatiblity
  581. if ( m_bLocalSystem == FALSE )
  582. {
  583. DWORD dwVersion = 0;
  584. dwVersion = GetTargetVersionEx( m_pWbemServices, m_pAuthIdentity );
  585. if ( dwVersion <= 5000 )// to block win2k versions
  586. {
  587. SetReason( ERROR_OS_INCOMPATIBLE );
  588. DISPLAY_MESSAGE2( stderr, szErrorMsg, L"%s %s", TAG_ERROR,
  589. GetReason());
  590. return FALSE;
  591. }
  592. }
  593. // check the local credentials and if need display warning
  594. if ( m_bLocalSystem && (lstrlen(m_pszUserName)!=0) )
  595. {
  596. CHString str;
  597. WMISaveError( WBEM_E_LOCAL_CREDENTIALS );
  598. str.Format( L"%s %s", TAG_WARNING, GetReason() );
  599. ShowMessage( stdout, str );
  600. }
  601. // Copy username and password returned from ConnectWmiEx
  602. lstrcpy(m_pszUserName,szTempUser);
  603. lstrcpy(m_pszPassword,szTempPassword);
  604. }
  605. if(cmdOptions[ ID_C_RU ].dwActuals ==1)
  606. {
  607. CheckRpRu();
  608. }
  609. // Show wait message...............
  610. m_hStdHandle = GetStdHandle(STD_ERROR_HANDLE);
  611. if(m_hStdHandle!=NULL)
  612. {
  613. GetConsoleScreenBufferInfo(m_hStdHandle,&m_ScreenBufferInfo);
  614. }
  615. PrintProgressMsg(m_hStdHandle,GetResString(IDS_MSG_EVTRIG_C),m_ScreenBufferInfo);
  616. // retrieves TriggerEventCosumer class
  617. bstrTemp = SysAllocString(CLS_TRIGGER_EVENT_CONSUMER);
  618. hr = m_pWbemServices->GetObject(bstrTemp,
  619. 0, NULL, &m_pClass, NULL);
  620. SAFE_RELEASE_BSTR(bstrTemp);
  621. ON_ERROR_THROW_EXCEPTION(hr);
  622. // Gets information about the "CreateETrigger" method of
  623. // "TriggerEventCosumer" class
  624. bstrTemp = SysAllocString(FN_CREATE_ETRIGGER);
  625. hr = m_pClass->GetMethod(bstrTemp, 0, &m_pInClass, NULL);
  626. SAFE_RELEASE_BSTR(bstrTemp);
  627. ON_ERROR_THROW_EXCEPTION(hr);
  628. // create a new instance of a class "TriggerEventCosumer".
  629. hr = m_pInClass->SpawnInstance(0, &m_pInInst);
  630. ON_ERROR_THROW_EXCEPTION(hr);
  631. // Set the sTriggerName property .
  632. // sets a "TriggerName" property for Newly created Instance
  633. hr = PropertyPut(m_pInInst,FPR_TRIGGER_NAME,m_pszTriggerName);
  634. ON_ERROR_THROW_EXCEPTION(hr);
  635. // Set the sTriggerAction property to Variant.
  636. hr = PropertyPut(m_pInInst,FPR_TRIGGER_ACTION,m_pszTaskName);
  637. ON_ERROR_THROW_EXCEPTION(hr);
  638. // Set the sTriggerDesc property to Variant .
  639. hr = PropertyPut(m_pInInst,FPR_TRIGGER_DESC,m_pszDescription);
  640. ON_ERROR_THROW_EXCEPTION(hr);
  641. // Set the RunAsUserName property .
  642. hr = PropertyPut(m_pInInst,FPR_RUN_AS_USER,_bstr_t(m_pszRunAsUserName));
  643. ON_ERROR_THROW_EXCEPTION(hr);
  644. // Set the RunAsUserNamePAssword property .
  645. hr = PropertyPut(m_pInInst,FPR_RUN_AS_USER_PASSWORD,_bstr_t(m_pszRunAsUserPassword));
  646. ON_ERROR_THROW_EXCEPTION(hr);
  647. lstrcpy(m_pszWMIQueryString ,QUERY_STRING);
  648. if(ConstructWMIQueryString() ==TRUE)
  649. {
  650. // Set the sTriggerQuery property to Variant
  651. LONG lTemp = 0;
  652. TCHAR szMsgString[MAX_RES_STRING];
  653. TCHAR szMsgFormat[MAX_RES_STRING];
  654. hr = PropertyPut(m_pInInst,FPR_TRIGGER_QUERY,m_pszWMIQueryString);
  655. ON_ERROR_THROW_EXCEPTION(hr);
  656. // All The required properties sets, so
  657. // executes CreateETrigger method to create eventtrigger
  658. hr = m_pWbemServices->ExecMethod(_bstr_t(CLS_TRIGGER_EVENT_CONSUMER),
  659. _bstr_t(FN_CREATE_ETRIGGER),
  660. 0, NULL, m_pInInst, &m_pOutInst,NULL);
  661. ON_ERROR_THROW_EXCEPTION( hr );
  662. DWORD dwTemp;
  663. if(PropertyGet(m_pOutInst,FPR_RETURN_VALUE,dwTemp)==FALSE)
  664. {
  665. return FALSE;
  666. }
  667. lTemp = (LONG)dwTemp;
  668. PrintProgressMsg(m_hStdHandle,NULL_STRING,m_ScreenBufferInfo);
  669. if(lTemp==0)
  670. {
  671. // SUCCESS: message on screen
  672. lstrcpy(szMsgFormat,GetResString(IDS_CREATE_SUCCESS));
  673. FORMAT_STRING(szMsgString,szMsgFormat,_X(m_pszTriggerName));
  674. // Message shown on screen will be...
  675. // SUCCESS: The Event Trigger "EventTrigger Name" has
  676. // successfully been created.
  677. ShowMessage(stdout,szMsgString);
  678. }
  679. else if(lTemp==1) // Means duplicate id found.
  680. {
  681. // Show Error Message
  682. lstrcpy(szMsgFormat,GetResString(IDS_DUPLICATE_TRG_NAME));
  683. FORMAT_STRING(szMsgString,szMsgFormat,_X(m_pszTriggerName));
  684. // Message shown on screen will be...
  685. // ERROR:Event Trigger Name "EventTrigger Name"
  686. // already exits.
  687. ShowMessage(stderr,szMsgString);
  688. return FALSE;
  689. }
  690. else if(lTemp==2) // Means ru is invalid so show warning....
  691. // along with success message.
  692. {
  693. // Show Warning message..
  694. ShowMessage(stdout,GetResString(IDS_INVALID_R_U));
  695. // SUCCESS: message on screen
  696. lstrcpy(szMsgFormat,GetResString(IDS_CREATE_SUCCESS));
  697. FORMAT_STRING(szMsgString,szMsgFormat,_X(m_pszTriggerName));
  698. // Message shown on screen will be...
  699. // SUCCESS: The Event Trigger "EventTrigger Name" has
  700. // successfully been created.
  701. ShowMessage(stdout,szMsgString);
  702. return FALSE;
  703. }
  704. else
  705. {
  706. // Prectically this error will never occur
  707. lstrcpy(szMsgFormat,GetResString(IDS_INVALID_PARAMETER));
  708. FORMAT_STRING(szMsgString,szMsgFormat,_X(m_pszTriggerName));
  709. // Message shown on screen will be...
  710. // ERROR:Invalid parameter passed.
  711. ShowMessage(stderr,szMsgString);
  712. return FALSE;
  713. }
  714. }
  715. else
  716. {
  717. return FALSE;
  718. }
  719. }
  720. catch(_com_error)
  721. {
  722. TCHAR szErrorMsg[MAX_RES_STRING+1];
  723. PrintProgressMsg(m_hStdHandle,NULL_STRING,m_ScreenBufferInfo);
  724. if(hr == 0x80041002)// WMI returns string for this hr value is
  725. // "Not Found." which is not user friendly. So
  726. // changing the message text.
  727. {
  728. ShowMessage( stderr,GetResString(IDS_CLASS_NOT_REG));
  729. }
  730. else
  731. {
  732. DISPLAY_MESSAGE2( stderr, szErrorMsg, L"%s %s" , TAG_ERROR,
  733. GetReason() );
  734. }
  735. return FALSE;
  736. }
  737. return TRUE;
  738. }
  739. /*****************************************************************************
  740. Routine Description:
  741. This function Will create a WMI Query String depending on other
  742. parameters supplied with -create parameter
  743. Arguments:
  744. none
  745. Return Value:
  746. TRUE - if Successfully creates Query string
  747. FALSE - if ERROR
  748. ******************************************************************************/
  749. BOOL
  750. CETCreate::ConstructWMIQueryString()
  751. {
  752. // Local variable
  753. TCHAR szLogName[MAX_RES_STRING+1];
  754. DWORD dNoOfLogNames = DynArrayGetCount( m_arrLogNames );
  755. DWORD dwIndx = 0;
  756. BOOL bBracket = FALSE;//user to check if brecket is used in WQL
  757. BOOL bAddLogToSQL = FALSE; // check whether to add log names to WQL
  758. BOOL bRequiredToCheckLogName = TRUE;// check whether to check log names
  759. TCHAR szMsgString[MAX_RES_STRING]; // To Show Error msg
  760. TCHAR szMsgFormat[MAX_RES_STRING]; // To Show Error msg
  761. // Check whether "*" is given for -log
  762. // if it is there skip adding log to SQL
  763. for (dwIndx=0;dwIndx<dNoOfLogNames;dwIndx++)
  764. {
  765. if(m_arrLogNames!=NULL)
  766. {
  767. lstrcpy(szLogName,DynArrayItemAsString(m_arrLogNames,dwIndx));
  768. }
  769. else
  770. {
  771. PrintProgressMsg(m_hStdHandle,NULL_STRING,m_ScreenBufferInfo);
  772. ShowMessage(stderr,GetResString(IDS_OUTOF_MEMORY));
  773. return FALSE;
  774. }
  775. bAddLogToSQL = TRUE;
  776. if(lstrcmp(szLogName,ASTERIX)==0)
  777. {
  778. DWORD dwNewIndx = 0;
  779. try
  780. {
  781. SAFE_RELEASE_BSTR(bstrTemp);
  782. bstrTemp = SysAllocString(CLS_WIN32_NT_EVENT_LOGFILE);
  783. HRESULT hr = m_pWbemServices->CreateInstanceEnum(bstrTemp,
  784. WBEM_FLAG_SHALLOW,
  785. NULL,
  786. &m_pEnumWin32_NTEventLogFile);
  787. SAFE_RELEASE_BSTR(bstrTemp);
  788. ON_ERROR_THROW_EXCEPTION( hr );
  789. // set the security at the interface level also
  790. hr = SetInterfaceSecurity( m_pEnumWin32_NTEventLogFile,
  791. m_pAuthIdentity );
  792. ON_ERROR_THROW_EXCEPTION(hr);
  793. // remove all from parrLogName which is initialy filled by
  794. //DoParceParam()
  795. DynArrayRemoveAll(m_arrLogNames);
  796. while(GetLogName(szLogName,m_pEnumWin32_NTEventLogFile)==TRUE)
  797. {
  798. if(DynArrayInsertString(m_arrLogNames,dwNewIndx++,szLogName,
  799. lstrlen(szLogName))==-1)
  800. {
  801. PrintProgressMsg(m_hStdHandle,NULL_STRING,m_ScreenBufferInfo);
  802. ShowMessage(stderr,GetResString(IDS_OUTOF_MEMORY));
  803. return FALSE;
  804. }
  805. }
  806. bAddLogToSQL = TRUE;
  807. bRequiredToCheckLogName = FALSE; // as log names are taken
  808. // from target system so
  809. // no need to check log names.
  810. dNoOfLogNames = DynArrayGetCount( m_arrLogNames );
  811. break;
  812. }
  813. catch(_com_error & error)
  814. {
  815. PrintProgressMsg(m_hStdHandle,NULL_STRING,m_ScreenBufferInfo);
  816. if(error.ErrorMessage ()!=NULL)
  817. ShowMessage(stderr,error.ErrorMessage ());
  818. else
  819. ShowMessage(stderr,GetResString(IDS_COM_ERROR));
  820. return FALSE;
  821. }
  822. }
  823. }
  824. if(bAddLogToSQL==TRUE)
  825. {
  826. for (dwIndx=0;dwIndx<dNoOfLogNames;dwIndx++)
  827. {
  828. if(m_arrLogNames!=NULL)
  829. {
  830. lstrcpy(szLogName,DynArrayItemAsString(m_arrLogNames,dwIndx));
  831. }
  832. else
  833. {
  834. PrintProgressMsg(m_hStdHandle,NULL_STRING,m_ScreenBufferInfo);
  835. ShowMessage(stderr,GetResString(IDS_OUTOF_MEMORY));
  836. return FALSE;
  837. }
  838. if(bRequiredToCheckLogName?CheckLogName(szLogName,m_pWbemServices):1)
  839. {
  840. if((dwIndx==0))
  841. {
  842. if(dNoOfLogNames!=1)
  843. {
  844. lstrcat((m_pszWMIQueryString),
  845. L" AND (targetinstance.LogFile =\"");
  846. bBracket = TRUE;
  847. }
  848. else
  849. {
  850. lstrcat((m_pszWMIQueryString),
  851. L" AND targetinstance.LogFile =\"");
  852. }
  853. }
  854. else
  855. {
  856. lstrcat((m_pszWMIQueryString),
  857. L" OR targetinstance.LogFile =\"");
  858. }
  859. lstrcat((m_pszWMIQueryString),szLogName);
  860. lstrcat((m_pszWMIQueryString),L"\"");
  861. if(dwIndx==(dNoOfLogNames-1)&&(bBracket==TRUE))
  862. {
  863. lstrcat((m_pszWMIQueryString),L")");
  864. }
  865. }
  866. else
  867. {
  868. // Show Log name doesn't exit.
  869. lstrcpy(szMsgFormat,GetResString(IDS_LOG_NOT_EXISTS));
  870. FORMAT_STRING(szMsgString,szMsgFormat,szLogName);
  871. // Message shown on screen will be...
  872. // FAILURE: "Log Name" Log not exists on system
  873. PrintProgressMsg(m_hStdHandle,NULL_STRING,m_ScreenBufferInfo);
  874. ShowMessage(stderr,szMsgString);
  875. return FALSE;
  876. }
  877. }
  878. }
  879. if(lstrlen(m_pszType)>0)// Updates Query string only if Event Type given
  880. {
  881. // In help -t can except "SUCCESSAUDIT" and "FAILUREAUDIT"
  882. // but this string directly cannot be appended to WQL as valid wmi
  883. // string for these two are "audit success" and "audit failure"
  884. // respectively
  885. if(lstrcmpi(m_pszType,GetResString(IDS_FAILURE_AUDIT))==0)
  886. {
  887. lstrcpy(m_pszType,GetResString(IDS_AUDIT_FAILURE));
  888. }
  889. else if(lstrcmpi(m_pszType,GetResString(IDS_SUCCESS_AUDIT))==0)
  890. {
  891. lstrcpy(m_pszType,GetResString(IDS_AUDIT_SUCCESS));
  892. }
  893. lstrcat((m_pszWMIQueryString),L" AND targetinstance.Type =\"");
  894. lstrcat((m_pszWMIQueryString),(m_pszType));
  895. lstrcat((m_pszWMIQueryString),L"\"");
  896. }
  897. if(lstrlen(m_pszSource)>0)// Updates Query string only if Event Source
  898. // given
  899. {
  900. lstrcat((m_pszWMIQueryString),L" AND targetinstance.SourceName =\"");
  901. lstrcat((m_pszWMIQueryString),(m_pszSource));
  902. lstrcat((m_pszWMIQueryString),L"\"");
  903. }
  904. if(m_dwID>0)
  905. {
  906. TCHAR szID[15];
  907. _itot(m_dwID,szID,10);
  908. lstrcat(m_pszWMIQueryString,L" AND targetinstance.EventCode = ");
  909. lstrcat(m_pszWMIQueryString,szID);
  910. }
  911. return TRUE;
  912. }
  913. /*****************************************************************************
  914. Routine Description:
  915. This function Will return all available log available in system
  916. Arguments:
  917. [out] pszLogName : Will have the NT Event Log names .
  918. Return Value:
  919. TRUE - if Log name returned
  920. FALSE - if no log name
  921. *****************************************************************************/
  922. BOOL
  923. CETCreate::GetLogName(PTCHAR pszLogName,
  924. IEnumWbemClassObject *pEnumWin32_NTEventLogFile)
  925. {
  926. HRESULT hr = 0;
  927. BOOL bReturn = FALSE;
  928. try
  929. {
  930. IWbemClassObject *pObj = NULL;
  931. VARIANT vVariant;// variable used to get/set values from/to
  932. // COM functions
  933. ULONG uReturned = 0;
  934. TCHAR szTempLogName[MAX_RES_STRING];
  935. hr = pEnumWin32_NTEventLogFile->Next(0,1,&pObj,&uReturned);
  936. ON_ERROR_THROW_EXCEPTION(hr);
  937. if(uReturned == 0)
  938. {
  939. SAFE_RELEASE_INTERFACE(pObj);
  940. return FALSE;
  941. }
  942. VariantInit(&vVariant);
  943. SAFE_RELEASE_BSTR(bstrTemp);
  944. bstrTemp = SysAllocString(L"LogfileName");
  945. hr = pObj->Get(bstrTemp, 0, &vVariant, 0, 0);
  946. SAFE_RELEASE_BSTR(bstrTemp);
  947. ON_ERROR_THROW_EXCEPTION(hr);
  948. if(GetCompatibleStringFromUnicode(V_BSTR(&vVariant),
  949. szTempLogName,
  950. MAX_RES_STRING)==NULL)
  951. {
  952. SAFE_RELEASE_INTERFACE(pObj);
  953. return FALSE;
  954. }
  955. hr = VariantClear(&vVariant);
  956. ON_ERROR_THROW_EXCEPTION(hr);
  957. lstrcpy(pszLogName,szTempLogName);
  958. bReturn = TRUE;
  959. }
  960. catch(_com_error & error)
  961. {
  962. PrintProgressMsg(m_hStdHandle,NULL_STRING,m_ScreenBufferInfo);
  963. if(error.ErrorMessage ()!=NULL)
  964. ShowMessage(stderr,error.ErrorMessage ());
  965. else
  966. ShowMessage(stderr,GetResString(IDS_COM_ERROR));
  967. bReturn = FALSE;
  968. }
  969. SAFE_RELEASE_BSTR(bstrTemp);
  970. return bReturn;
  971. }
  972. /******************************************************************************
  973. Routine Description:
  974. This function Will return whether log name given at commandline is a valid
  975. log name or not. It chekcs the log name with WMI
  976. Arguments:
  977. None
  978. Return Value:
  979. TRUE - if Successfully Log name founds in WMI
  980. FALSE - if ERROR
  981. ******************************************************************************/
  982. BOOL
  983. CETCreate::CheckLogName(PTCHAR pszLogName,IWbemServices *pNamespace)
  984. {
  985. // Local Variables
  986. IEnumWbemClassObject* pEnumWin32_NTEventLogFile = NULL;
  987. HRESULT hr = 0;
  988. BOOL bReturn = FALSE;
  989. BSTR bstrTemp = NULL;
  990. try
  991. {
  992. SAFE_RELEASE_BSTR(bstrTemp);
  993. bstrTemp = SysAllocString(CLS_WIN32_NT_EVENT_LOGFILE);
  994. hr = pNamespace->CreateInstanceEnum(bstrTemp,
  995. WBEM_FLAG_SHALLOW,
  996. NULL,
  997. &pEnumWin32_NTEventLogFile);
  998. // set the security at the interface level also
  999. hr = SetInterfaceSecurity(pEnumWin32_NTEventLogFile, m_pAuthIdentity);
  1000. ON_ERROR_THROW_EXCEPTION(hr);
  1001. pEnumWin32_NTEventLogFile->Reset();
  1002. while(1)
  1003. {
  1004. IWbemClassObject *pObj = NULL;
  1005. VARIANT vVariant;// variable used to get/set values from/to
  1006. // COM functions
  1007. ULONG uReturned = 0;
  1008. TCHAR szTempLogName[MAX_RES_STRING];
  1009. hr = pEnumWin32_NTEventLogFile->Next(0,1,&pObj,&uReturned);
  1010. ON_ERROR_THROW_EXCEPTION(hr);
  1011. if(uReturned == 0)
  1012. {
  1013. SAFE_RELEASE_INTERFACE(pObj);
  1014. bReturn = FALSE;
  1015. break;
  1016. }
  1017. VariantInit(&vVariant);// clear variant, containts not used now
  1018. SAFE_RELEASE_BSTR(bstrTemp);// string will be no loger be used
  1019. bstrTemp = SysAllocString(L"LogfileName");
  1020. hr = pObj->Get(bstrTemp, 0, &vVariant, 0, 0);
  1021. SAFE_RELEASE_BSTR(bstrTemp);
  1022. if(GetCompatibleStringFromUnicode(V_BSTR(&vVariant),
  1023. szTempLogName,
  1024. MAX_RES_STRING)==NULL)
  1025. {
  1026. SAFE_RELEASE_INTERFACE(pObj);
  1027. bReturn = FALSE;
  1028. break;
  1029. }
  1030. hr = VariantClear(&vVariant);
  1031. ON_ERROR_THROW_EXCEPTION(hr);
  1032. if(lstrcmpi(szTempLogName,pszLogName)==0) // Means log name
  1033. // found in WMI
  1034. {
  1035. SAFE_RELEASE_INTERFACE(pObj);
  1036. bReturn = TRUE;
  1037. break;
  1038. }
  1039. }
  1040. }
  1041. catch(_com_error & error)
  1042. {
  1043. PrintProgressMsg(m_hStdHandle,NULL_STRING,m_ScreenBufferInfo);
  1044. if(error.ErrorMessage ()!=NULL)
  1045. ShowMessage(stderr,error.ErrorMessage ());
  1046. else
  1047. ShowMessage(stderr,GetResString(IDS_COM_ERROR));
  1048. bReturn = FALSE;
  1049. }
  1050. SAFE_RELEASE_BSTR(bstrTemp);
  1051. SAFE_RELEASE_INTERFACE(pEnumWin32_NTEventLogFile);
  1052. return bReturn;
  1053. }
  1054. /******************************************************************************
  1055. Routine Description:
  1056. This function will check/set values for rp and ru.
  1057. Arguments:
  1058. None
  1059. Return Value:
  1060. none
  1061. ******************************************************************************/
  1062. void CETCreate::CheckRpRu(void)
  1063. {
  1064. TCHAR szTemp[MAX_RES_STRING]; // To Show Messages
  1065. TCHAR szTemp1[MAX_RES_STRING];// To Show Messages
  1066. TCHAR szWarnPassWord[MAX_RES_STRING];
  1067. wsprintf(szWarnPassWord,GetResString(IDS_WARNING_PASSWORD),NTAUTHORITY_USER);
  1068. // Check if run as username is "NT AUTHORITY\SYSTEM" OR "SYSTEM" Then
  1069. // make this as BLANK (NULL_STRING) and do not ask for password, any how
  1070. if((lstrcmpi(m_pszRunAsUserName,NTAUTHORITY_USER) == 0) ||
  1071. (lstrcmpi(m_pszRunAsUserName,SYSTEM_USER) == 0 ))
  1072. {
  1073. lstrcpy(m_pszRunAsUserName,NULL_STRING); // make string blank
  1074. lstrcpy(m_pszRunAsUserPassword,NULL_STRING); // make string blank
  1075. if(cmdOptions[ ID_C_RP ].dwActuals ==1)
  1076. DISPLAY_MESSAGE(stderr,szWarnPassWord);
  1077. return;
  1078. }
  1079. if((cmdOptions[ ID_C_RP ].dwActuals ==1) &&
  1080. (lstrcmpi(m_pszRunAsUserPassword,_T("*"))==0)&&(lstrlen(m_pszRunAsUserName)!=0))
  1081. {
  1082. lstrcpy(szTemp,GetResString(IDS_ASK_PASSWORD));
  1083. wsprintf(szTemp1,szTemp,m_pszRunAsUserName);
  1084. DISPLAY_MESSAGE(stderr,szTemp1);
  1085. GetPassword(m_pszRunAsUserPassword,MAX_PASSWORD_LENGTH);
  1086. if(lstrlen(m_pszRunAsUserPassword) == 0)
  1087. DISPLAY_MESSAGE(stderr,GetResString(IDS_WARN_NULL_PASSWORD));
  1088. return;
  1089. }
  1090. else
  1091. {
  1092. if((lstrlen(m_pszRunAsUserPassword) == 0) && (lstrlen(m_pszRunAsUserName)!=0))
  1093. DISPLAY_MESSAGE(stderr,GetResString(IDS_WARN_NULL_PASSWORD));
  1094. }
  1095. if(m_bLocalSystem==TRUE)
  1096. {
  1097. if((lstrlen(m_pszRunAsUserName)==0) && (cmdOptions[ ID_C_RP ].dwActuals == 1))
  1098. {
  1099. DISPLAY_MESSAGE(stderr,szWarnPassWord);
  1100. return;
  1101. }
  1102. else if((lstrlen(m_pszRunAsUserName)!=0) && ((cmdOptions[ ID_C_RP ].dwActuals ==0)))
  1103. {
  1104. lstrcpy(szTemp,GetResString(IDS_ASK_PASSWORD));
  1105. wsprintf(szTemp1,szTemp,m_pszRunAsUserName);
  1106. DISPLAY_MESSAGE(stderr,szTemp1);
  1107. GetPassword(m_pszRunAsUserPassword,MAX_PASSWORD_LENGTH);
  1108. if(lstrlen(m_pszRunAsUserPassword) == 0)
  1109. DISPLAY_MESSAGE(stderr,GetResString(IDS_WARN_NULL_PASSWORD));
  1110. return;
  1111. }
  1112. }
  1113. else // remote system
  1114. {
  1115. if((lstrlen(m_pszRunAsUserName)==0)&&((cmdOptions[ ID_C_RP ].dwActuals ==1)))
  1116. {
  1117. DISPLAY_MESSAGE(stderr,szWarnPassWord);
  1118. return;
  1119. }
  1120. if(lstrlen(m_pszRunAsUserName)==0)
  1121. return;
  1122. if (lstrlen(m_pszUserName)!=0)
  1123. {
  1124. if((lstrcmpi(m_pszUserName,m_pszRunAsUserName)==0))
  1125. {
  1126. if(cmdOptions[ ID_C_RP ].dwActuals ==0)
  1127. {
  1128. // Username and runasusername are same so password are same
  1129. lstrcpy(m_pszRunAsUserPassword,m_pszPassword);
  1130. }
  1131. }
  1132. else // Username and runasusername are not same
  1133. {
  1134. if(cmdOptions[ ID_C_RP ].dwActuals ==0)
  1135. {
  1136. lstrcpy(szTemp,GetResString(IDS_ASK_PASSWORD));
  1137. wsprintf(szTemp1,szTemp,m_pszRunAsUserName);
  1138. DISPLAY_MESSAGE(stderr,szTemp1);
  1139. GetPassword(m_pszRunAsUserPassword,MAX_PASSWORD_LENGTH);
  1140. if(lstrlen(m_pszRunAsUserPassword) == 0)
  1141. DISPLAY_MESSAGE(stderr,GetResString(IDS_WARN_NULL_PASSWORD));
  1142. return;
  1143. }
  1144. }
  1145. }
  1146. }
  1147. }