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.

1831 lines
63 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. #include <Lmcons.h>
  20. #define NTAUTHORITY_USER L"NT AUTHORITY\\SYSTEM"
  21. #define SYSTEM_USER L"SYSTEM"
  22. CETCreate::CETCreate()
  23. /*++
  24. Routine Description:
  25. Class constructor
  26. Arguments:
  27. None
  28. Return Value:
  29. None
  30. --*/
  31. {
  32. m_pszServerName = NULL;
  33. m_pszUserName = NULL;
  34. m_pszPassword = NULL;
  35. m_arrLogNames = NULL;
  36. m_pszRunAsUserName = NULL;
  37. m_pszRunAsUserPassword = NULL;
  38. m_bNeedPassword = FALSE;
  39. m_bCreate = FALSE;
  40. m_bIsCOMInitialize = FALSE;
  41. m_lMinMemoryReq = 0;
  42. m_pWbemLocator = NULL;
  43. m_pWbemServices = NULL;
  44. m_pEnumObjects = NULL;
  45. m_pAuthIdentity = NULL;
  46. m_pClass = NULL;
  47. m_pOutInst = NULL;
  48. m_pInClass = NULL;
  49. m_pInInst = NULL;
  50. bstrTemp = NULL;
  51. m_pEnumWin32_NTEventLogFile = NULL;
  52. }
  53. CETCreate::CETCreate(
  54. LONG lMinMemoryReq,
  55. BOOL bNeedPassword
  56. )
  57. /*++
  58. Routine Description:
  59. Class constructor
  60. Arguments:
  61. None
  62. Return Value:
  63. None
  64. --*/
  65. {
  66. m_pszServerName = NULL;
  67. m_pszUserName = NULL;
  68. m_pszPassword = NULL;
  69. m_arrLogNames = NULL;
  70. m_bCreate = FALSE;
  71. m_dwID = 0;
  72. m_pszRunAsUserName = NULL;
  73. m_pszRunAsUserPassword = NULL;
  74. m_bIsCOMInitialize = FALSE;
  75. m_pWbemLocator = NULL;
  76. m_pWbemServices = NULL;
  77. m_pEnumObjects = NULL;
  78. m_pAuthIdentity = NULL;
  79. m_pClass = NULL;
  80. m_pOutInst = NULL;
  81. m_pInClass = NULL;
  82. m_pInInst = NULL;
  83. bstrTemp = NULL;
  84. m_lMinMemoryReq = lMinMemoryReq;
  85. m_pEnumWin32_NTEventLogFile = NULL;
  86. m_bNeedPassword = bNeedPassword;
  87. }
  88. CETCreate::~CETCreate()
  89. /*++
  90. Routine Description:
  91. Class destructor
  92. Arguments:
  93. None
  94. Return Value:
  95. None
  96. --*/
  97. {
  98. // Release all memory which is allocated.
  99. FreeMemory((LPVOID*)& m_pszServerName);
  100. FreeMemory((LPVOID*)& m_pszUserName);
  101. FreeMemory((LPVOID*)& m_pszPassword);
  102. FreeMemory((LPVOID*)& m_pszRunAsUserName);
  103. FreeMemory((LPVOID*)& m_pszRunAsUserPassword);
  104. DESTROY_ARRAY(m_arrLogNames);
  105. SAFE_RELEASE_INTERFACE(m_pWbemLocator);
  106. SAFE_RELEASE_INTERFACE(m_pWbemServices);
  107. SAFE_RELEASE_INTERFACE(m_pEnumObjects);
  108. SAFE_RELEASE_INTERFACE(m_pClass);
  109. SAFE_RELEASE_INTERFACE(m_pOutInst);
  110. SAFE_RELEASE_INTERFACE(m_pInClass);
  111. SAFE_RELEASE_INTERFACE(m_pInInst);
  112. SAFE_RELEASE_INTERFACE(m_pEnumWin32_NTEventLogFile);
  113. // Release Authority
  114. WbemFreeAuthIdentity(&m_pAuthIdentity);
  115. // Uninitialize COM only if it is initialized.
  116. if( TRUE == m_bIsCOMInitialize )
  117. {
  118. CoUninitialize();
  119. }
  120. }
  121. void
  122. CETCreate::Initialize()
  123. /*++
  124. Routine Description:
  125. Allocates and initialize variables.
  126. Arguments:
  127. NONE
  128. Return Value:
  129. NONE
  130. --*/
  131. {
  132. // if at all any occurs, we know that is 'coz of the
  133. // failure in memory allocation ... so set the error
  134. DEBUG_INFO;
  135. SetLastError( ERROR_OUTOFMEMORY );
  136. SaveLastError();
  137. SecureZeroMemory(m_szWMIQueryString,sizeof(m_szWMIQueryString));
  138. SecureZeroMemory(m_szTaskName,sizeof(m_szTaskName));
  139. SecureZeroMemory(m_szTriggerName,sizeof(m_szTriggerName));
  140. SecureZeroMemory(m_szDescription,sizeof(m_szDescription));
  141. SecureZeroMemory(m_szType,sizeof(m_szType));
  142. SecureZeroMemory(m_szSource,sizeof(m_szSource));
  143. m_arrLogNames = CreateDynamicArray();
  144. if( NULL == m_arrLogNames )
  145. {
  146. throw CShowError(E_OUTOFMEMORY);
  147. }
  148. SecureZeroMemory(cmdOptions,sizeof(TCMDPARSER2) * MAX_COMMANDLINE_C_OPTION);
  149. // initialization is successful
  150. SetLastError( NOERROR ); // clear the error
  151. SetReason( L""); // clear the reason
  152. DEBUG_INFO;
  153. return;
  154. }
  155. void
  156. CETCreate::ProcessOption(
  157. DWORD argc,
  158. LPCTSTR argv[]
  159. )
  160. /*++
  161. Routine Description:
  162. This function will process/parce the command line options.
  163. Arguments:
  164. [ in ] argc : argument(s) count specified at the command prompt
  165. [ in ] argv : argument(s) specified at the command prompt
  166. Return Value:
  167. none
  168. --*/
  169. {
  170. // local variable
  171. BOOL bReturn = TRUE;
  172. CHString szTempString;
  173. DEBUG_INFO;
  174. PrepareCMDStruct();
  175. DEBUG_INFO;
  176. // do the actual parsing of the command line arguments and check the result
  177. bReturn = DoParseParam2( argc, argv,ID_C_CREATE,MAX_COMMANDLINE_C_OPTION, cmdOptions,0);
  178. // Take values from 'cmdOptions' structure
  179. m_pszServerName = (LPWSTR)cmdOptions[ ID_C_SERVER ].pValue;
  180. m_pszUserName = (LPWSTR)cmdOptions[ ID_C_USERNAME ].pValue;
  181. m_pszPassword = (LPWSTR)cmdOptions[ ID_C_PASSWORD ].pValue;
  182. m_pszRunAsUserName = (LPWSTR)cmdOptions[ ID_C_RU ].pValue;
  183. m_pszRunAsUserPassword = (LPWSTR)cmdOptions[ ID_C_RP ].pValue;
  184. if(FALSE == bReturn )
  185. {
  186. DEBUG_INFO;
  187. throw CShowError(MK_E_SYNTAX);
  188. }
  189. DEBUG_INFO;
  190. CHString str = m_szTriggerName;
  191. if (-1 != str.FindOneOf(INVALID_TRIGGER_NAME_CHARACTERS))
  192. {
  193. DEBUG_INFO;
  194. throw CShowError(IDS_ID_INVALID_TRIG_NAME);
  195. }
  196. DEBUG_INFO;
  197. // At least any of -so , -t OR -i should be given .
  198. if((0 == cmdOptions[ ID_C_SOURCE].dwActuals ) &&
  199. (0 == cmdOptions[ ID_C_TYPE].dwActuals ) &&
  200. (0 == cmdOptions[ ID_C_ID ].dwActuals ))
  201. {
  202. throw CShowError(IDS_ID_TYPE_SOURCE);
  203. }
  204. // Trigger ID (/EID) should not be ZERO.
  205. if ( (1 == cmdOptions[ ID_C_ID ].dwActuals) && (0 == m_dwID))
  206. {
  207. throw CShowError(IDS_TRIGGER_ID_NON_ZERO);
  208. }
  209. // "-u" should not be specified without "-s"
  210. if ( 0 == cmdOptions[ ID_C_SERVER ].dwActuals &&
  211. 0 != cmdOptions[ ID_C_USERNAME ].dwActuals )
  212. {
  213. throw CShowError(IDS_ERROR_USERNAME_BUT_NOMACHINE);
  214. }
  215. // "-p" should not be specified without -u
  216. if ( ( 0 == cmdOptions[ID_C_USERNAME].dwActuals ) &&
  217. ( 0 != cmdOptions[ID_C_PASSWORD].dwActuals ))
  218. {
  219. // invalid syntax
  220. throw CShowError(IDS_USERNAME_REQUIRED);
  221. }
  222. // "-rp" should not be specified without -ru
  223. if (( 0 == cmdOptions[ID_C_RU].dwActuals ) &&
  224. ( 0 != cmdOptions[ID_C_RP].dwActuals ))
  225. {
  226. // invalid syntax
  227. throw CShowError(IDS_RUN_AS_USERNAME_REQUIRED);
  228. }
  229. // added on 06/12/02 if /rp is given without any value set it to *
  230. if( ( 0 != cmdOptions[ID_C_RP].dwActuals ) &&
  231. ( NULL == cmdOptions[ID_C_RP].pValue ) )
  232. {
  233. if ( m_pszRunAsUserPassword == NULL )
  234. {
  235. m_pszRunAsUserPassword = (LPTSTR)AllocateMemory( MAX_STRING_LENGTH * sizeof( WCHAR ) );
  236. if ( m_pszRunAsUserPassword == NULL )
  237. {
  238. DEBUG_INFO;
  239. SaveLastError();
  240. throw CShowError(E_OUTOFMEMORY);
  241. }
  242. }
  243. StringCopy( m_pszRunAsUserPassword, L"*", SIZE_OF_DYN_ARRAY(m_pszRunAsUserPassword));
  244. }
  245. // check the remote connectivity information
  246. if ( m_pszServerName != NULL )
  247. {
  248. //
  249. // if -u is not specified, we need to allocate memory
  250. // in order to be able to retrive the current user name
  251. //
  252. // case 1: -p is not at all specified
  253. // as the value for this switch is optional, we have to rely
  254. // on the dwActuals to determine whether the switch is specified or not
  255. // in this case utility needs to try to connect first and if it fails
  256. // then prompt for the password -- in fact, we need not check for this
  257. // condition explicitly except for noting that we need to prompt for the
  258. // password
  259. //
  260. // case 2: -p is specified
  261. // but we need to check whether the value is specified or not
  262. // in this case user wants the utility to prompt for the password
  263. // before trying to connect
  264. //
  265. // case 3: -p * is specified
  266. // user name
  267. DEBUG_INFO;
  268. if ( m_pszUserName == NULL )
  269. {
  270. DEBUG_INFO;
  271. m_pszUserName = (LPTSTR) AllocateMemory( MAX_STRING_LENGTH * sizeof( WCHAR ) );
  272. if ( m_pszUserName == NULL )
  273. {
  274. DEBUG_INFO;
  275. SaveLastError();
  276. throw CShowError(E_OUTOFMEMORY);
  277. }
  278. }
  279. // password
  280. DEBUG_INFO;
  281. if ( m_pszPassword == NULL )
  282. {
  283. m_bNeedPassword = TRUE;
  284. m_pszPassword = (LPTSTR)AllocateMemory( MAX_STRING_LENGTH * sizeof( WCHAR ) );
  285. if ( m_pszPassword == NULL )
  286. {
  287. DEBUG_INFO;
  288. SaveLastError();
  289. throw CShowError(E_OUTOFMEMORY);
  290. }
  291. }
  292. // case 1
  293. if ( cmdOptions[ ID_C_PASSWORD ].dwActuals == 0 )
  294. {
  295. // we need not do anything special here
  296. }
  297. // case 2
  298. else if ( cmdOptions[ ID_C_PASSWORD ].pValue == NULL )
  299. {
  300. StringCopy( m_pszPassword, L"*", SIZE_OF_DYN_ARRAY(m_pszPassword));
  301. }
  302. // case 3
  303. else if ( StringCompare( m_pszPassword, L"*", TRUE, 0 ) == 0 )
  304. {
  305. if ( ReallocateMemory( (LPVOID*)&m_pszPassword,
  306. MAX_STRING_LENGTH * sizeof( WCHAR ) ) == FALSE )
  307. {
  308. SaveLastError();
  309. throw CShowError(E_OUTOFMEMORY);
  310. }
  311. // ...
  312. m_bNeedPassword = TRUE;
  313. DEBUG_INFO;
  314. }
  315. }
  316. }
  317. void
  318. CETCreate::PrepareCMDStruct()
  319. /*++
  320. Routine Description:
  321. This function will prepare column structure for DoParseParam Function.
  322. Arguments:
  323. none
  324. Return Value:
  325. none
  326. --*/
  327. {
  328. DEBUG_INFO;
  329. // Filling cmdOptions structure
  330. // -create
  331. StringCopyA( cmdOptions[ ID_C_CREATE ].szSignature, "PARSER2\0", 8 );
  332. cmdOptions[ ID_C_CREATE ].dwType = CP_TYPE_BOOLEAN;
  333. cmdOptions[ ID_C_CREATE ].pwszOptions = szCreateOption;
  334. cmdOptions[ ID_C_CREATE ].dwCount = 1;
  335. cmdOptions[ ID_C_CREATE ].dwActuals = 0;
  336. cmdOptions[ ID_C_CREATE ].dwFlags = 0;
  337. cmdOptions[ ID_C_CREATE ].pValue = &m_bCreate;
  338. cmdOptions[ ID_C_CREATE ].dwLength = 0;
  339. // -s (servername)
  340. StringCopyA( cmdOptions[ ID_C_SERVER ].szSignature, "PARSER2\0", 8 );
  341. cmdOptions[ ID_C_SERVER ].dwType = CP_TYPE_TEXT;
  342. cmdOptions[ ID_C_SERVER ].pwszOptions = szServerNameOption;
  343. cmdOptions[ ID_C_SERVER ].dwCount = 1;
  344. cmdOptions[ ID_C_SERVER ].dwActuals = 0;
  345. cmdOptions[ ID_C_SERVER ].dwFlags = CP2_ALLOCMEMORY|CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL;
  346. cmdOptions[ ID_C_SERVER ].pValue = NULL; //m_pszServerName
  347. cmdOptions[ ID_C_SERVER ].dwLength = 0;
  348. // -u (username)
  349. StringCopyA( cmdOptions[ ID_C_USERNAME ].szSignature, "PARSER2\0", 8 );
  350. cmdOptions[ ID_C_USERNAME ].dwType = CP_TYPE_TEXT;
  351. cmdOptions[ ID_C_USERNAME ].pwszOptions = szUserNameOption;
  352. cmdOptions[ ID_C_USERNAME ].dwCount = 1;
  353. cmdOptions[ ID_C_USERNAME ].dwActuals = 0;
  354. cmdOptions[ ID_C_USERNAME ].dwFlags = CP2_ALLOCMEMORY|CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL;
  355. cmdOptions[ ID_C_USERNAME ].pValue = NULL; //m_pszUserName
  356. cmdOptions[ ID_C_USERNAME ].dwLength = 0;
  357. // -p (password)
  358. StringCopyA( cmdOptions[ ID_C_PASSWORD ].szSignature, "PARSER2\0", 8 );
  359. cmdOptions[ ID_C_PASSWORD ].dwType = CP_TYPE_TEXT;
  360. cmdOptions[ ID_C_PASSWORD ].pwszOptions = szPasswordOption;
  361. cmdOptions[ ID_C_PASSWORD ].dwCount = 1;
  362. cmdOptions[ ID_C_PASSWORD ].dwActuals = 0;
  363. cmdOptions[ ID_C_PASSWORD ].dwFlags = CP2_ALLOCMEMORY | CP2_VALUE_OPTIONAL;
  364. cmdOptions[ ID_C_PASSWORD ].pValue = NULL; //m_pszPassword
  365. cmdOptions[ ID_C_PASSWORD ].dwLength = 0;
  366. // -tr
  367. StringCopyA( cmdOptions[ ID_C_TRIGGERNAME ].szSignature, "PARSER2\0", 8 );
  368. cmdOptions[ ID_C_TRIGGERNAME ].dwType = CP_TYPE_TEXT;
  369. cmdOptions[ ID_C_TRIGGERNAME ].pwszOptions = szTriggerNameOption;
  370. cmdOptions[ ID_C_TRIGGERNAME ].dwCount = 1;
  371. cmdOptions[ ID_C_TRIGGERNAME ].dwActuals = 0;
  372. cmdOptions[ ID_C_TRIGGERNAME ].dwFlags = CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL
  373. |CP2_MANDATORY;
  374. cmdOptions[ ID_C_TRIGGERNAME ].pValue = m_szTriggerName;
  375. cmdOptions[ ID_C_TRIGGERNAME ].dwLength = MAX_TRIGGER_NAME;
  376. //-l
  377. StringCopyA( cmdOptions[ ID_C_LOGNAME ].szSignature, "PARSER2\0", 8 );
  378. cmdOptions[ ID_C_LOGNAME ].dwType = CP_TYPE_TEXT;
  379. cmdOptions[ ID_C_LOGNAME ].pwszOptions = szLogNameOption;
  380. cmdOptions[ ID_C_LOGNAME ].dwCount = 0;
  381. cmdOptions[ ID_C_LOGNAME ].dwActuals = 0;
  382. cmdOptions[ ID_C_LOGNAME ].dwFlags = CP2_MODE_ARRAY|CP2_VALUE_TRIMINPUT|
  383. CP2_VALUE_NONULL|CP2_VALUE_NODUPLICATES;
  384. cmdOptions[ ID_C_LOGNAME ].pValue = &m_arrLogNames;
  385. cmdOptions[ ID_C_LOGNAME ].dwLength = 0;
  386. // -eid
  387. StringCopyA( cmdOptions[ ID_C_ID ].szSignature, "PARSER2\0", 8 );
  388. cmdOptions[ ID_C_ID ].dwType = CP_TYPE_UNUMERIC;
  389. cmdOptions[ ID_C_ID ].pwszOptions = szEIDOption;
  390. cmdOptions[ ID_C_ID ].dwCount = 1;
  391. cmdOptions[ ID_C_ID ].dwActuals = 0;
  392. cmdOptions[ ID_C_ID ].dwFlags = 0;
  393. cmdOptions[ ID_C_ID ].pValue = &m_dwID;
  394. // -t (type)
  395. StringCopyA( cmdOptions[ ID_C_TYPE ].szSignature, "PARSER2\0", 8 );
  396. cmdOptions[ ID_C_TYPE ].dwType = CP_TYPE_TEXT;
  397. cmdOptions[ ID_C_TYPE ].pwszOptions = szTypeOption;
  398. cmdOptions[ ID_C_TYPE ].pwszValues = GetResString(IDS_TYPE_OPTIONS);
  399. cmdOptions[ ID_C_TYPE ].dwCount = 1;
  400. cmdOptions[ ID_C_TYPE ].dwActuals = 0;
  401. cmdOptions[ ID_C_TYPE ].dwFlags = CP2_VALUE_TRIMINPUT|
  402. CP2_VALUE_NONULL|CP2_MODE_VALUES;
  403. cmdOptions[ ID_C_TYPE ].pValue = m_szType;
  404. cmdOptions[ ID_C_TYPE ].dwLength = MAX_STRING_LENGTH;
  405. // -so (source)
  406. StringCopyA( cmdOptions[ ID_C_SOURCE ].szSignature, "PARSER2\0", 8 );
  407. cmdOptions[ ID_C_SOURCE ].dwType = CP_TYPE_TEXT;
  408. cmdOptions[ ID_C_SOURCE ].pwszOptions = szSource;
  409. cmdOptions[ ID_C_SOURCE ].dwCount = 1;
  410. cmdOptions[ ID_C_SOURCE ].dwActuals = 0;
  411. cmdOptions[ ID_C_SOURCE ].dwFlags = CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL;
  412. cmdOptions[ ID_C_SOURCE ].pValue = m_szSource;
  413. cmdOptions[ ID_C_SOURCE ].dwLength = MAX_STRING_LENGTH;
  414. // -d (description)
  415. StringCopyA( cmdOptions[ ID_C_DESCRIPTION ].szSignature, "PARSER2\0", 8 );
  416. cmdOptions[ ID_C_DESCRIPTION ].dwType = CP_TYPE_TEXT;
  417. cmdOptions[ ID_C_DESCRIPTION ].pwszOptions = szDescriptionOption;
  418. cmdOptions[ ID_C_DESCRIPTION ].dwCount = 1;
  419. cmdOptions[ ID_C_DESCRIPTION ].dwActuals = 0;
  420. cmdOptions[ ID_C_DESCRIPTION ].dwFlags = CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL;
  421. cmdOptions[ ID_C_DESCRIPTION ].pValue = m_szDescription;
  422. cmdOptions[ ID_C_DESCRIPTION ].dwLength = MAX_STRING_LENGTH;
  423. // -tk (task)
  424. StringCopyA( cmdOptions[ ID_C_TASK ].szSignature, "PARSER2\0", 8 );
  425. cmdOptions[ ID_C_TASK ].dwType = CP_TYPE_TEXT;
  426. cmdOptions[ ID_C_TASK ].pwszOptions = szTaskNameOption;
  427. cmdOptions[ ID_C_TASK ].dwCount = 1;
  428. cmdOptions[ ID_C_TASK ].dwActuals = 0;
  429. cmdOptions[ ID_C_TASK ].dwFlags = CP2_VALUE_NONULL|CP2_MANDATORY;
  430. cmdOptions[ ID_C_TASK ].pValue = m_szTaskName;
  431. cmdOptions[ ID_C_TASK ].dwLength = MAX_TASK_NAME;
  432. // -ru (RunAsUserName)
  433. StringCopyA( cmdOptions[ ID_C_RU ].szSignature, "PARSER2\0", 8 );
  434. cmdOptions[ ID_C_RU ].dwType = CP_TYPE_TEXT;
  435. cmdOptions[ ID_C_RU ].pwszOptions = szRunAsUserNameOption;
  436. cmdOptions[ ID_C_RU ].dwCount = 1;
  437. cmdOptions[ ID_C_RU ].dwActuals = 0;
  438. cmdOptions[ ID_C_RU ].dwFlags = CP2_ALLOCMEMORY|CP2_VALUE_TRIMINPUT;
  439. cmdOptions[ ID_C_RU ].pValue = NULL; //m_pszRunAsUserName
  440. cmdOptions[ ID_C_RU ].dwLength = 0;
  441. // -rp (Run As User password)
  442. StringCopyA( cmdOptions[ ID_C_RP ].szSignature, "PARSER2\0", 8 );
  443. cmdOptions[ ID_C_RP ].dwType = CP_TYPE_TEXT;
  444. cmdOptions[ ID_C_RP ].pwszOptions = szRunAsPasswordOption;
  445. cmdOptions[ ID_C_RP ].dwCount = 1;
  446. cmdOptions[ ID_C_RP ].dwActuals = 0;
  447. cmdOptions[ ID_C_RP ].dwFlags = CP2_ALLOCMEMORY | CP2_VALUE_OPTIONAL;
  448. cmdOptions[ ID_C_RP ].pValue = NULL; //m_pszRunAsUserPassword
  449. cmdOptions[ ID_C_RP ].dwLength = 0;
  450. DEBUG_INFO;
  451. }
  452. BOOL
  453. CETCreate::ExecuteCreate()
  454. /*++
  455. Routine Description:
  456. This routine will actualy creates eventtrigers in WMI.
  457. Arguments:
  458. None
  459. Return Value:
  460. None
  461. --*/
  462. {
  463. // local variables...
  464. BOOL bResult = FALSE;// Stores return status of function
  465. HRESULT hr = 0; // Stores return code.
  466. try
  467. {
  468. DEBUG_INFO;
  469. // Initialize COM
  470. InitializeCom(&m_pWbemLocator);
  471. // make m_bIsCOMInitialize to true which will be useful when
  472. // uninitialize COM.
  473. m_bIsCOMInitialize = TRUE;
  474. {
  475. // brackets used to restrict scope of following declered variables.
  476. CHString szTempUser = m_pszUserName; // Temp. variabe to store user
  477. // name.
  478. CHString szTempPassword = m_pszPassword;// Temp. variable to store
  479. // password.
  480. m_bLocalSystem = TRUE;
  481. // Connect remote / local WMI.
  482. DEBUG_INFO;
  483. bResult = ConnectWmiEx( m_pWbemLocator,
  484. &m_pWbemServices,
  485. m_pszServerName,
  486. szTempUser,
  487. szTempPassword,
  488. &m_pAuthIdentity,
  489. m_bNeedPassword,
  490. WMI_NAMESPACE_CIMV2,
  491. &m_bLocalSystem);
  492. if( FALSE == bResult)
  493. {
  494. DEBUG_INFO;
  495. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  496. return FALSE;
  497. }
  498. AssignMinMemory();
  499. // Initialize. Required for XP version check. 5001
  500. bResult = FALSE;
  501. // check the remote system version and its compatiblity
  502. if ( FALSE == m_bLocalSystem )
  503. {
  504. DEBUG_INFO;
  505. DWORD dwVersion = 0;
  506. dwVersion = GetTargetVersionEx( m_pWbemServices,
  507. m_pAuthIdentity);
  508. if ( dwVersion <= 5000 )// to block win2k versions
  509. {
  510. SetReason( E_REMOTE_INCOMPATIBLE );
  511. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  512. return FALSE;
  513. }
  514. // If remote system is a XP system then
  515. // have to take different route to accomplish the task.
  516. // Set boolean to TRUE.
  517. if( 5001 == dwVersion )
  518. {
  519. bResult = TRUE;
  520. }
  521. }
  522. // check the local credentials and if need display warning
  523. DEBUG_INFO;
  524. if ( m_bLocalSystem && ( 0 != StringLength(m_pszUserName,0)) )
  525. {
  526. WMISaveError( WBEM_E_LOCAL_CREDENTIALS );
  527. ShowLastErrorEx(stderr,SLE_TYPE_WARNING|SLE_INTERNAL);
  528. }
  529. if(0!= szTempUser.GetAllocLength())
  530. {
  531. DEBUG_INFO;
  532. LONG lSize = SIZE_OF_DYN_ARRAY(m_pszUserName);
  533. if (NULL == m_pszUserName ||
  534. (lSize< (szTempUser.GetAllocLength())))
  535. {
  536. DEBUG_INFO;
  537. if ( ReallocateMemory( (LPVOID*)&m_pszUserName,
  538. (szTempUser.GetAllocLength()* sizeof( WCHAR ))+1 ) == FALSE )
  539. {
  540. DEBUG_INFO;
  541. SaveLastError();
  542. throw CShowError(E_OUTOFMEMORY);
  543. }
  544. }
  545. }
  546. DEBUG_INFO;
  547. if(0 != szTempPassword.GetAllocLength())
  548. {
  549. DEBUG_INFO;
  550. LONG lSize = SIZE_OF_DYN_ARRAY(m_pszUserName);
  551. if (NULL == m_pszUserName || (lSize< szTempPassword.GetAllocLength()))
  552. {
  553. DEBUG_INFO;
  554. if ( ReallocateMemory( (LPVOID*)&m_pszPassword,
  555. (szTempPassword.GetAllocLength()* sizeof( WCHAR ))+1 ) == FALSE )
  556. {
  557. SaveLastError();
  558. throw CShowError(E_OUTOFMEMORY);
  559. }
  560. }
  561. }
  562. // Copy username and password returned from ConnectWmiEx
  563. if(m_pszUserName)
  564. {
  565. DEBUG_INFO;
  566. StringCopy(m_pszUserName, szTempUser, SIZE_OF_DYN_ARRAY(m_pszUserName));
  567. }
  568. if(m_pszPassword)
  569. {
  570. DEBUG_INFO;
  571. StringCopy(m_pszPassword, szTempPassword, SIZE_OF_DYN_ARRAY(m_pszPassword));
  572. }
  573. }
  574. CheckRpRu();
  575. // Password is no longer is needed now. For security reason release it.
  576. FreeMemory((LPVOID*)& m_pszPassword);
  577. // This will check for XP system. Version - 5001
  578. if( TRUE == bResult )
  579. {
  580. if( TRUE == CreateXPResults() )
  581. {
  582. // Displayed triggers present.
  583. return TRUE;
  584. }
  585. else
  586. {
  587. // Failed to display results .
  588. // Error message is displayed.
  589. return FALSE;
  590. }
  591. }
  592. // retrieves TriggerEventCosumer class
  593. DEBUG_INFO;
  594. bstrTemp = SysAllocString(CLS_TRIGGER_EVENT_CONSUMER);
  595. hr = m_pWbemServices->GetObject(bstrTemp,
  596. 0, NULL, &m_pClass, NULL);
  597. SAFE_RELEASE_BSTR(bstrTemp);
  598. ON_ERROR_THROW_EXCEPTION(hr);
  599. // Gets information about the "CreateETrigger" method of
  600. // "TriggerEventCosumer" class
  601. DEBUG_INFO;
  602. bstrTemp = SysAllocString(FN_CREATE_ETRIGGER);
  603. hr = m_pClass->GetMethod(bstrTemp, 0, &m_pInClass, NULL);
  604. SAFE_RELEASE_BSTR(bstrTemp);
  605. ON_ERROR_THROW_EXCEPTION(hr);
  606. // create a new instance of a class "TriggerEventCosumer".
  607. DEBUG_INFO;
  608. hr = m_pInClass->SpawnInstance(0, &m_pInInst);
  609. ON_ERROR_THROW_EXCEPTION(hr);
  610. // Set the sTriggerName property .
  611. // sets a "TriggerName" property for Newly created Instance
  612. DEBUG_INFO;
  613. hr = PropertyPut(m_pInInst,FPR_TRIGGER_NAME,(m_szTriggerName));
  614. ON_ERROR_THROW_EXCEPTION(hr);
  615. // Set the sTriggerAction property to Variant.
  616. DEBUG_INFO;
  617. hr = PropertyPut(m_pInInst,FPR_TRIGGER_ACTION,(m_szTaskName));
  618. ON_ERROR_THROW_EXCEPTION(hr);
  619. // Set the sTriggerDesc property to Variant .
  620. DEBUG_INFO;
  621. hr = PropertyPut(m_pInInst,FPR_TRIGGER_DESC,(m_szDescription));
  622. ON_ERROR_THROW_EXCEPTION(hr);
  623. // Set the RunAsUserName property .
  624. DEBUG_INFO;
  625. hr = PropertyPut(m_pInInst,FPR_RUN_AS_USER,(m_pszRunAsUserName));
  626. ON_ERROR_THROW_EXCEPTION(hr);
  627. // Set the RunAsUserNamePAssword property .
  628. DEBUG_INFO;
  629. hr = PropertyPut( m_pInInst,FPR_RUN_AS_USER_PASSWORD,
  630. (m_pszRunAsUserPassword));
  631. ON_ERROR_THROW_EXCEPTION(hr);
  632. // Password is no longer is needed now. For security reason release it.
  633. FreeMemory((LPVOID*)& m_pszRunAsUserPassword);
  634. StringCopy(m_szWMIQueryString ,QUERY_STRING,SIZE_OF_ARRAY(m_szWMIQueryString));
  635. if( TRUE == ConstructWMIQueryString())
  636. {
  637. TCHAR szMsgString[MAX_RES_STRING * 4];
  638. TCHAR szMsgFormat[MAX_RES_STRING];
  639. DEBUG_INFO;
  640. hr = PropertyPut(m_pInInst,FPR_TRIGGER_QUERY,m_szWMIQueryString);
  641. ON_ERROR_THROW_EXCEPTION(hr);
  642. // All The required properties sets, so
  643. // executes CreateETrigger method to create eventtrigger
  644. DEBUG_INFO;
  645. hr = m_pWbemServices->ExecMethod(_bstr_t(CLS_TRIGGER_EVENT_CONSUMER),
  646. _bstr_t(FN_CREATE_ETRIGGER),
  647. 0, NULL, m_pInInst, &m_pOutInst,NULL);
  648. ON_ERROR_THROW_EXCEPTION( hr );
  649. DEBUG_INFO;
  650. VARIANT vtValue;
  651. // initialize the variant and then get the value of the specified property
  652. VariantInit( &vtValue );
  653. hr = m_pOutInst->Get( _bstr_t( FPR_RETURN_VALUE ), 0, &vtValue, NULL, NULL );
  654. ON_ERROR_THROW_EXCEPTION( hr );
  655. //Get Output paramters.
  656. hr = vtValue.lVal;
  657. // Clear the variant variable
  658. VariantClear( &vtValue );
  659. if(FAILED(hr))
  660. {
  661. // added on 07/12/02 if unable to set account info of schedule task
  662. // show the error instead of warning as access denied
  663. if( hr == ERROR_UNABLE_SET_RU )
  664. {
  665. SetLastError(hr);
  666. SaveLastError();
  667. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_SYSTEM);
  668. return FALSE;
  669. }
  670. // Check if return code is cutomized or not.
  671. if( !(ERROR_TRIGNAME_ALREADY_EXIST == hr || (ERROR_INVALID_RU == hr) ||
  672. (SCHEDULER_NOT_RUNNING_ERROR_CODE == hr) ||
  673. (RPC_SERVER_NOT_AVAILABLE == hr) ||
  674. // (ERROR_UNABLE_SET_RU == hr) || ///commented on 07/12/02
  675. (ERROR_INVALID_USER == hr) ||
  676. (ERROR_TRIGGER_ID_EXCEED == hr)))
  677. {
  678. ON_ERROR_THROW_EXCEPTION( hr );
  679. }
  680. }
  681. DEBUG_INFO;
  682. if(SUCCEEDED(hr))
  683. {
  684. // SUCCESS: message on screen
  685. DEBUG_INFO;
  686. StringCopy(szMsgFormat, GetResString(IDS_CREATE_SUCCESS),
  687. SIZE_OF_ARRAY(szMsgFormat));
  688. StringCchPrintfW(szMsgString,SIZE_OF_ARRAY(szMsgString),
  689. szMsgFormat,_X(m_szTriggerName));
  690. DEBUG_INFO;
  691. // Message shown on screen will be...
  692. // SUCCESS: The Event Trigger "EventTrigger Name" has
  693. // successfully been created.
  694. ShowMessage(stdout,szMsgString);
  695. }
  696. else if(ERROR_TRIGNAME_ALREADY_EXIST == hr) // Means duplicate id found.
  697. {
  698. // Show Error Message
  699. DEBUG_INFO;
  700. StringCopy(szMsgFormat, GetResString(IDS_DUPLICATE_TRG_NAME),
  701. SIZE_OF_ARRAY(szMsgFormat));
  702. StringCchPrintfW(szMsgString,SIZE_OF_ARRAY(szMsgString),
  703. szMsgFormat,_X(m_szTriggerName));
  704. // Message shown on screen will be...
  705. // ERROR:Event Trigger Name "EventTrigger Name"
  706. // already exits.
  707. ShowMessage(stderr,szMsgString);
  708. return FALSE;
  709. }
  710. else if (ERROR_TRIGGER_ID_EXCEED == hr)
  711. {
  712. StringCopy(szMsgFormat, GetResString(IDS_TRIGGER_ID_EXCCED_LIMIT),
  713. SIZE_OF_ARRAY(szMsgFormat));
  714. StringCchPrintfW(szMsgString,SIZE_OF_ARRAY(szMsgString),
  715. szMsgFormat,UINT_MAX);
  716. // Message shown on screen will be...
  717. // ERROR:Event Trigger Name "EventTrigger Name"
  718. // already exits.
  719. ShowMessage(stderr,szMsgString);
  720. return FALSE;
  721. }
  722. if( ( ERROR_INVALID_RU == hr) || // (ERROR_UNABLE_SET_RU == hr) || ---commented on 07/12/02 as it is already handled
  723. (ERROR_INVALID_USER == hr))
  724. // Means ru is invalid so show warning....
  725. // along with success message.
  726. {
  727. DEBUG_INFO;
  728. // WARNING: The new event trigger ""%s"" has been created,
  729. // but may not run because the account information could not be set.
  730. //changed on 07/12/02 the message as error instead of warning as account info could not be set
  731. StringCchPrintf ( szMsgString, SIZE_OF_ARRAY(szMsgString),
  732. GetResString(IDS_INVALID_PARAMETER), _X(m_szTriggerName));
  733. ShowMessage ( stderr, _X(szMsgString));
  734. }
  735. else if ( hr == SCHEDULER_NOT_RUNNING_ERROR_CODE || hr == RPC_SERVER_NOT_AVAILABLE)
  736. {
  737. StringCchPrintf ( szMsgString, SIZE_OF_ARRAY(szMsgString),
  738. GetResString(IDS_SCHEDULER_NOT_RUNNING), _X(m_szTriggerName));
  739. ShowMessage ( stderr, _X(szMsgString));
  740. }
  741. }
  742. else
  743. {
  744. return FALSE;
  745. }
  746. }
  747. catch(_com_error)
  748. {
  749. DEBUG_INFO;
  750. if(0x80041002 == hr )// WMI returns string for this hr value is
  751. // "Not Found." which is not user friendly. So
  752. // changing the message text.
  753. {
  754. ShowMessage( stderr,GetResString(IDS_CLASS_NOT_REG));
  755. }
  756. else
  757. {
  758. DEBUG_INFO;
  759. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  760. }
  761. return FALSE;
  762. }
  763. DEBUG_INFO;
  764. return TRUE;
  765. }
  766. BOOL
  767. CETCreate::ConstructWMIQueryString()
  768. /*++
  769. Routine Description:
  770. This function Will create a WMI Query String depending on other
  771. parameters supplied with -create parameter
  772. Arguments:
  773. none
  774. Return Value:
  775. TRUE - if Successfully creates Query string
  776. FALSE - if ERROR
  777. --*/
  778. {
  779. // Local variable
  780. TCHAR szLogName[MAX_RES_STRING+1];
  781. DWORD dNoOfLogNames = DynArrayGetCount( m_arrLogNames );
  782. DWORD dwIndx = 0;
  783. BOOL bBracket = FALSE;//user to check if brecket is used in WQL
  784. BOOL bAddLogToSQL = FALSE; // check whether to add log names to WQL
  785. BOOL bRequiredToCheckLogName = TRUE;// check whether to check log names
  786. DEBUG_INFO;
  787. // Check whether "*" is given for -log
  788. // if it is there skip adding log to SQL
  789. for (dwIndx=0;dwIndx<dNoOfLogNames;dwIndx++)
  790. {
  791. if( NULL != m_arrLogNames)
  792. {
  793. StringCopy(szLogName,DynArrayItemAsString(m_arrLogNames,dwIndx),
  794. SIZE_OF_ARRAY(szLogName));
  795. DEBUG_INFO;
  796. }
  797. else
  798. {
  799. SetLastError(ERROR_INVALID_PARAMETER);
  800. SaveLastError();
  801. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  802. return FALSE;
  803. }
  804. bAddLogToSQL = TRUE;
  805. if( 0 == StringCompare(szLogName,ASTERIX,TRUE,0))
  806. {
  807. DWORD dwNewIndx = 0;
  808. try
  809. {
  810. SAFE_RELEASE_BSTR(bstrTemp);
  811. bstrTemp = SysAllocString(CLS_WIN32_NT_EVENT_LOGFILE);
  812. DEBUG_INFO;
  813. HRESULT hr = m_pWbemServices->CreateInstanceEnum(bstrTemp,
  814. WBEM_FLAG_SHALLOW,
  815. NULL,
  816. &m_pEnumWin32_NTEventLogFile);
  817. SAFE_RELEASE_BSTR(bstrTemp);
  818. ON_ERROR_THROW_EXCEPTION( hr );
  819. // set the security at the interface level also
  820. hr = SetInterfaceSecurity( m_pEnumWin32_NTEventLogFile,
  821. m_pAuthIdentity );
  822. ON_ERROR_THROW_EXCEPTION(hr);
  823. // remove all from parrLogName which is initialy filled by
  824. //DoParceParam()
  825. DynArrayRemoveAll(m_arrLogNames);
  826. DEBUG_INFO;
  827. while( TRUE == GetLogName(szLogName,
  828. m_pEnumWin32_NTEventLogFile))
  829. {
  830. if( -1 == DynArrayInsertString(m_arrLogNames,
  831. dwNewIndx++,szLogName,
  832. StringLength(szLogName,0)))
  833. {
  834. ShowMessage(stderr,GetResString(IDS_OUTOF_MEMORY));
  835. return FALSE;
  836. }
  837. }
  838. bAddLogToSQL = TRUE;
  839. bRequiredToCheckLogName = FALSE; // as log names are taken
  840. // from target system so
  841. // no need to check log names.
  842. dNoOfLogNames = DynArrayGetCount( m_arrLogNames );
  843. break;
  844. }
  845. catch(_com_error )
  846. {
  847. DEBUG_INFO;
  848. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  849. return FALSE;
  850. }
  851. }
  852. }
  853. DEBUG_INFO;
  854. if( TRUE == bAddLogToSQL)
  855. {
  856. for (dwIndx=0;dwIndx<dNoOfLogNames;dwIndx++)
  857. {
  858. if( NULL != m_arrLogNames)
  859. {
  860. StringCopy(szLogName,DynArrayItemAsString(m_arrLogNames,dwIndx),
  861. SIZE_OF_ARRAY(szLogName));
  862. }
  863. else
  864. {
  865. ShowMessage(stderr,GetResString(IDS_OUTOF_MEMORY));
  866. return FALSE;
  867. }
  868. DEBUG_INFO;
  869. if(bRequiredToCheckLogName ? CheckLogName(szLogName,m_pWbemServices)
  870. : 1)
  871. {
  872. DEBUG_INFO;
  873. if( 0 == dwIndx)
  874. {
  875. if( 1 != dNoOfLogNames)
  876. {
  877. StringConcat(m_szWMIQueryString,
  878. L" AND (targetinstance.LogFile =\"",
  879. SIZE_OF_ARRAY(m_szWMIQueryString));
  880. bBracket = TRUE;
  881. }
  882. else
  883. {
  884. StringConcat(m_szWMIQueryString,
  885. L" AND targetinstance.LogFile =\"",
  886. SIZE_OF_ARRAY(m_szWMIQueryString));
  887. }
  888. }
  889. else
  890. {
  891. StringConcat(m_szWMIQueryString,
  892. L" OR targetinstance.LogFile =\"",
  893. SIZE_OF_ARRAY(m_szWMIQueryString));
  894. }
  895. DEBUG_INFO;
  896. StringConcat(m_szWMIQueryString,szLogName,SIZE_OF_ARRAY(m_szWMIQueryString));
  897. StringConcat(m_szWMIQueryString,L"\"",SIZE_OF_ARRAY(m_szWMIQueryString));
  898. if( (dNoOfLogNames-1) == dwIndx &&( TRUE == bBracket))
  899. {
  900. StringConcat(m_szWMIQueryString,L")",SIZE_OF_ARRAY(m_szWMIQueryString));
  901. }
  902. }
  903. else
  904. {
  905. return FALSE;
  906. }
  907. }
  908. }
  909. DEBUG_INFO;
  910. if( 1 == cmdOptions[ ID_C_TYPE ].dwActuals)// Updates Query string only if Event Type given
  911. {
  912. // In help -t can except "SUCCESSAUDIT" and "FAILUREAUDIT"
  913. // but this string directly cannot be appended to WQL as valid wmi
  914. // string for these two are "audit success" and "audit failure"
  915. // respectively
  916. DEBUG_INFO;
  917. StringConcat(m_szWMIQueryString,L" AND targetinstance.Type =\"",
  918. SIZE_OF_ARRAY(m_szWMIQueryString));
  919. if(0 == StringCompare(m_szType,GetResString(IDS_FAILURE_AUDIT),
  920. TRUE,0))
  921. {
  922. StringConcat(m_szWMIQueryString,GetResString(IDS_AUDIT_FAILURE),SIZE_OF_ARRAY(m_szWMIQueryString));
  923. }
  924. else if(0 == StringCompare(m_szType, GetResString(IDS_SUCCESS_AUDIT),
  925. TRUE,0))
  926. {
  927. StringConcat(m_szWMIQueryString,GetResString(IDS_AUDIT_SUCCESS),SIZE_OF_ARRAY(m_szWMIQueryString));
  928. }
  929. else
  930. {
  931. StringConcat(m_szWMIQueryString,m_szType,SIZE_OF_ARRAY(m_szWMIQueryString));
  932. }
  933. DEBUG_INFO;
  934. StringConcat(m_szWMIQueryString,L"\"",SIZE_OF_ARRAY(m_szWMIQueryString));
  935. }
  936. if( 1 == cmdOptions[ ID_C_SOURCE ].dwActuals)// Updates Query string only if Event Source
  937. // given
  938. {
  939. DEBUG_INFO;
  940. StringConcat(m_szWMIQueryString,
  941. L" AND targetinstance.SourceName =\"",
  942. SIZE_OF_ARRAY(m_szWMIQueryString));
  943. StringConcat(m_szWMIQueryString,m_szSource,SIZE_OF_ARRAY(m_szWMIQueryString));
  944. StringConcat(m_szWMIQueryString,L"\"",SIZE_OF_ARRAY(m_szWMIQueryString));
  945. }
  946. if(m_dwID>0)
  947. {
  948. DEBUG_INFO;
  949. TCHAR szID[15];
  950. _itot(m_dwID,szID,10);
  951. StringConcat(m_szWMIQueryString,
  952. L" AND targetinstance.EventCode = ",
  953. SIZE_OF_ARRAY(m_szWMIQueryString));
  954. StringConcat(m_szWMIQueryString,szID,SIZE_OF_ARRAY(m_szWMIQueryString));
  955. }
  956. DEBUG_INFO;
  957. return TRUE;
  958. }
  959. BOOL
  960. CETCreate::GetLogName(
  961. OUT PTCHAR pszLogName,
  962. IN IEnumWbemClassObject *pEnumWin32_NTEventLogFile
  963. )
  964. /*++
  965. Routine Description:
  966. This function Will return all available log available in system
  967. Arguments:
  968. [out] pszLogName : Will have the NT Event Log names .
  969. [in ] pEnumWin32_NTEventLogFile : Pointer to WBEM Class object Enum.
  970. Return Value:
  971. TRUE - if Log name returned
  972. FALSE - if no log name
  973. --*/
  974. {
  975. HRESULT hr = 0;
  976. BOOL bReturn = FALSE;
  977. IWbemClassObject *pObj = NULL;
  978. try
  979. {
  980. VARIANT vVariant;// variable used to get/set values from/to
  981. // COM functions
  982. ULONG uReturned = 0;
  983. TCHAR szTempLogName[MAX_RES_STRING];
  984. DEBUG_INFO;
  985. hr = pEnumWin32_NTEventLogFile->Next(0,1,&pObj,&uReturned);
  986. ON_ERROR_THROW_EXCEPTION(hr);
  987. if( 0 == uReturned )
  988. {
  989. SAFE_RELEASE_INTERFACE(pObj);
  990. return FALSE;
  991. }
  992. DEBUG_INFO;
  993. VariantInit(&vVariant);
  994. SAFE_RELEASE_BSTR(bstrTemp);
  995. bstrTemp = SysAllocString(L"LogfileName");
  996. hr = pObj->Get(bstrTemp, 0, &vVariant, 0, 0);
  997. SAFE_RELEASE_BSTR(bstrTemp);
  998. ON_ERROR_THROW_EXCEPTION(hr);
  999. StringCopy(szTempLogName, V_BSTR(&vVariant), SIZE_OF_ARRAY(szTempLogName));
  1000. hr = VariantClear(&vVariant);
  1001. ON_ERROR_THROW_EXCEPTION(hr);
  1002. StringCopy(pszLogName,szTempLogName,SIZE_OF_DYN_ARRAY(pszLogName));
  1003. bReturn = TRUE;
  1004. DEBUG_INFO;
  1005. }
  1006. catch(_com_error )
  1007. {
  1008. DEBUG_INFO;
  1009. SAFE_RELEASE_INTERFACE(pObj);
  1010. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  1011. bReturn = FALSE;
  1012. }
  1013. SAFE_RELEASE_BSTR(bstrTemp);
  1014. SAFE_RELEASE_INTERFACE(pObj);
  1015. DEBUG_INFO;
  1016. return bReturn;
  1017. }
  1018. BOOL
  1019. CETCreate::CheckLogName(
  1020. IN PTCHAR pszLogName,
  1021. IN IWbemServices *pNamespace
  1022. )
  1023. /*++
  1024. Routine Description:
  1025. This function Will return whether log name given at commandline is a valid
  1026. log name or not. It chekcs the log name with WMI
  1027. Arguments:
  1028. [in] pszLogName : Log name which is to be checked.
  1029. [in] pNamespace : Wbem service pointer.
  1030. Return Value:
  1031. TRUE - if Successfully Log name founds in WMI
  1032. FALSE - if ERROR
  1033. --*/
  1034. {
  1035. // Local Variables
  1036. IEnumWbemClassObject* pEnumWin32_NTEventLogFile = NULL;
  1037. IWbemClassObject *pObj = NULL;
  1038. HRESULT hr = 0;
  1039. BOOL bReturn = FALSE;
  1040. BSTR bstrTemp = NULL;
  1041. BOOL bAlwaysTrue = TRUE;
  1042. BOOL bIsException = FALSE;
  1043. try
  1044. {
  1045. SAFE_RELEASE_BSTR(bstrTemp);
  1046. DEBUG_INFO;
  1047. bstrTemp = SysAllocString(CLS_WIN32_NT_EVENT_LOGFILE);
  1048. hr = pNamespace->CreateInstanceEnum(bstrTemp,
  1049. WBEM_FLAG_SHALLOW,
  1050. NULL,
  1051. &pEnumWin32_NTEventLogFile);
  1052. DEBUG_INFO;
  1053. ON_ERROR_THROW_EXCEPTION(hr);
  1054. // set the security at the interface level also
  1055. hr = SetInterfaceSecurity(pEnumWin32_NTEventLogFile, m_pAuthIdentity);
  1056. DEBUG_INFO;
  1057. ON_ERROR_THROW_EXCEPTION(hr);
  1058. pEnumWin32_NTEventLogFile->Reset();
  1059. while(bAlwaysTrue)
  1060. {
  1061. VARIANT vVariant;// variable used to get/set values from/to
  1062. // COM functions
  1063. ULONG uReturned = 0;
  1064. TCHAR szTempLogName[MAX_RES_STRING];
  1065. DEBUG_INFO;
  1066. hr = pEnumWin32_NTEventLogFile->Next(0,1,&pObj,&uReturned);
  1067. ON_ERROR_THROW_EXCEPTION(hr);
  1068. if( 0 == uReturned )
  1069. {
  1070. SAFE_RELEASE_INTERFACE(pObj);
  1071. bReturn = FALSE;
  1072. break;
  1073. }
  1074. // clear variant, containts not used now
  1075. VariantInit(&vVariant);
  1076. SAFE_RELEASE_BSTR(bstrTemp);// string will be no loger be used
  1077. bstrTemp = SysAllocString(L"LogfileName");
  1078. hr = pObj->Get(bstrTemp, 0, &vVariant, 0, 0);
  1079. SAFE_RELEASE_BSTR(bstrTemp);
  1080. StringCopy(szTempLogName, V_BSTR(&vVariant),
  1081. SIZE_OF_ARRAY(szTempLogName));
  1082. hr = VariantClear(&vVariant);
  1083. ON_ERROR_THROW_EXCEPTION(hr);
  1084. // Means log name found in WMI
  1085. if( 0 == StringCompare(szTempLogName,pszLogName,TRUE,0))
  1086. {
  1087. DEBUG_INFO;
  1088. SAFE_RELEASE_INTERFACE(pObj);
  1089. bReturn = TRUE;
  1090. break;
  1091. }
  1092. }
  1093. }
  1094. catch(_com_error )
  1095. {
  1096. DEBUG_INFO;
  1097. SAFE_RELEASE_INTERFACE(pEnumWin32_NTEventLogFile);
  1098. SAFE_RELEASE_INTERFACE(pObj);
  1099. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  1100. bReturn = FALSE;
  1101. bIsException = TRUE;
  1102. }
  1103. SAFE_RELEASE_BSTR(bstrTemp);
  1104. SAFE_RELEASE_INTERFACE(pObj);
  1105. SAFE_RELEASE_INTERFACE(pEnumWin32_NTEventLogFile);
  1106. DEBUG_INFO;
  1107. if ((FALSE == bReturn) && (FALSE == bIsException))
  1108. {
  1109. TCHAR szMsgFormat[MAX_STRING_LENGTH];
  1110. TCHAR szMsgString[MAX_STRING_LENGTH];
  1111. SecureZeroMemory(szMsgFormat,sizeof(szMsgFormat));
  1112. SecureZeroMemory(szMsgString,sizeof(szMsgString));
  1113. // Show Log name doesn't exit.
  1114. StringCopy(szMsgFormat,GetResString(IDS_LOG_NOT_EXISTS),
  1115. SIZE_OF_ARRAY(szMsgFormat));
  1116. StringCchPrintfW(szMsgString, SIZE_OF_ARRAY(szMsgString),
  1117. szMsgFormat,pszLogName);
  1118. // Message shown on screen will be...
  1119. // FAILURE: "Log Name" Log not exists on system
  1120. ShowMessage(stderr,szMsgString);
  1121. }
  1122. return bReturn;
  1123. }
  1124. void
  1125. CETCreate::CheckRpRu(
  1126. void
  1127. )
  1128. /*++
  1129. Routine Description:
  1130. This function will check/set values for rp and ru.
  1131. Arguments:
  1132. None
  1133. Return Value:
  1134. none
  1135. --*/
  1136. {
  1137. TCHAR szTemp[MAX_STRING_LENGTH]; // To Show Messages
  1138. TCHAR szTemp1[MAX_STRING_LENGTH];// To Show Messages
  1139. TCHAR szWarnPassWord[MAX_STRING_LENGTH];
  1140. SecureZeroMemory(szTemp,sizeof(szTemp));
  1141. SecureZeroMemory(szTemp1,sizeof(szTemp1));
  1142. SecureZeroMemory(szWarnPassWord,sizeof(szWarnPassWord));
  1143. StringCchPrintfW(szWarnPassWord,SIZE_OF_ARRAY(szWarnPassWord),
  1144. GetResString(IDS_WARNING_PASSWORD),NTAUTHORITY_USER);
  1145. // Check if run as username is "NT AUTHORITY\SYSTEM" OR "SYSTEM" Then
  1146. // make this as BLANK (L"") and do not ask for password, any how
  1147. // Compare the string irrespective of language.
  1148. DEBUG_INFO;
  1149. INT iCompareResult1 = CompareString(MAKELCID( MAKELANGID(LANG_ENGLISH,
  1150. SUBLANG_ENGLISH_US),
  1151. SORT_DEFAULT),
  1152. NORM_IGNORECASE,
  1153. m_pszRunAsUserName,
  1154. StringLength(m_pszRunAsUserName,0),
  1155. NTAUTHORITY_USER ,
  1156. StringLength(NTAUTHORITY_USER,0)
  1157. );
  1158. INT iCompareResult2 = CompareString(MAKELCID( MAKELANGID(LANG_ENGLISH,
  1159. SUBLANG_ENGLISH_US),
  1160. SORT_DEFAULT),
  1161. NORM_IGNORECASE,
  1162. m_pszRunAsUserName,
  1163. StringLength(m_pszRunAsUserName,0),
  1164. SYSTEM_USER ,
  1165. StringLength(SYSTEM_USER,0)
  1166. );
  1167. if((CSTR_EQUAL == iCompareResult1) || (CSTR_EQUAL == iCompareResult2))
  1168. {
  1169. DEBUG_INFO;
  1170. if( 1 == cmdOptions[ ID_C_RP ].dwActuals)
  1171. DISPLAY_MESSAGE(stderr,szWarnPassWord);
  1172. return;
  1173. }
  1174. // added on 07/12/02 /ru is given and is "" and /rp is given
  1175. if( ( 1 == cmdOptions[ ID_C_RU ].dwActuals ) &&
  1176. ( 0 == StringLength(m_pszRunAsUserName,0)) &&
  1177. ( 1 == cmdOptions[ ID_C_RP ].dwActuals ))
  1178. {
  1179. DISPLAY_MESSAGE(stderr,szWarnPassWord);
  1180. return;
  1181. }
  1182. // /rp is given and is "" (blank), show warning message.
  1183. if( (1 == cmdOptions[ ID_C_RP ].dwActuals) &&
  1184. (0 == StringLength(m_pszRunAsUserPassword,0)))
  1185. {
  1186. ShowMessage(stderr,GetResString(IDS_WARN_NULL_PASSWORD));
  1187. return;
  1188. }
  1189. // /rp is given and is '*', ask for the password only if -ru is not equal to ""
  1190. // added on 07/12/02
  1191. else if(( 1 == cmdOptions[ ID_C_RP ].dwActuals ) &&
  1192. ( 0 == StringCompare(m_pszRunAsUserPassword,ASTERIX,FALSE,0)) &&
  1193. ( 0 != StringLength(m_pszRunAsUserName,0)))
  1194. {
  1195. DEBUG_INFO;
  1196. // Free the allocated memory;
  1197. FreeMemory((LPVOID*)& m_pszRunAsUserPassword);
  1198. m_pszRunAsUserPassword = (LPTSTR) AllocateMemory(MAX_STRING_LENGTH * sizeof(WCHAR));
  1199. if(NULL == m_pszRunAsUserPassword)
  1200. {
  1201. throw CShowError(E_OUTOFMEMORY);
  1202. }
  1203. StringCopy(szTemp, GetResString(IDS_ASK_PASSWORD),
  1204. SIZE_OF_ARRAY(szTemp));
  1205. StringCchPrintfW(szTemp1, SIZE_OF_ARRAY(szTemp1), szTemp,
  1206. m_pszRunAsUserName);
  1207. ShowMessage(stdout,szTemp1);
  1208. GetPassword(m_pszRunAsUserPassword,
  1209. SIZE_OF_DYN_ARRAY(m_pszRunAsUserPassword));
  1210. if( 0 == StringLength(m_pszRunAsUserPassword,0))
  1211. {
  1212. ShowMessage(stderr,GetResString(IDS_WARN_NULL_PASSWORD));
  1213. }
  1214. return;
  1215. }
  1216. if( TRUE == m_bLocalSystem)
  1217. {
  1218. // RULES:
  1219. // For the local system following cases are considered.
  1220. // if /ru not given, ru will be current logged on user
  1221. // and for 'rp' utility will prompt for the password.
  1222. // If /ru is given and /rp is not given, utiliry
  1223. // will ask for the password.
  1224. if( 0 == cmdOptions[ ID_C_RU ].dwActuals)
  1225. {
  1226. DEBUG_INFO;
  1227. SetToLoggedOnUser();
  1228. return;
  1229. }
  1230. // added on 07/12/02 check -ru is not equal to ""
  1231. else if(( 1 == cmdOptions[ ID_C_RU ].dwActuals) &&
  1232. (( 0 == cmdOptions[ ID_C_RP ].dwActuals)) && (0 != StringLength(m_pszRunAsUserName,0)) )
  1233. {
  1234. StringCopy(szTemp,GetResString(IDS_ASK_PASSWORD),
  1235. SIZE_OF_ARRAY(szTemp));
  1236. StringCchPrintfW(szTemp1, SIZE_OF_ARRAY(szTemp1), szTemp,
  1237. m_pszRunAsUserName);
  1238. ShowMessage(stdout,szTemp1);
  1239. GetPassword(m_pszRunAsUserPassword,SIZE_OF_DYN_ARRAY(m_pszRunAsUserPassword));
  1240. if( 0 == StringLength(m_pszRunAsUserPassword,0))
  1241. {
  1242. ShowMessage(stderr,GetResString(IDS_WARN_NULL_PASSWORD));
  1243. }
  1244. DEBUG_INFO;
  1245. return;
  1246. }
  1247. }
  1248. else // remote system
  1249. {
  1250. // RULES:
  1251. // For the local system following cases are considered.
  1252. // 1. /u, /p , /ru and /rp not given:
  1253. // 'ru' will be current logged on user and for 'rp'
  1254. // utility will prompt for password.
  1255. // 2. /u given and /p , /ru and /rp not given:
  1256. // /ru will be /u and for 'rp' utility will
  1257. // prompt for the password.
  1258. // 3. /u and /p given and /ru - /rp not given:
  1259. // /ru will be /u and /rp will be /p
  1260. if( (0 == cmdOptions[ ID_C_USERNAME ].dwActuals) &&
  1261. (0 == cmdOptions[ ID_C_RU ].dwActuals))
  1262. {
  1263. DEBUG_INFO;
  1264. SetToLoggedOnUser();
  1265. }
  1266. else if ((1 == cmdOptions[ ID_C_USERNAME ].dwActuals) &&
  1267. (0 == cmdOptions[ ID_C_RU ].dwActuals) )
  1268. {
  1269. DEBUG_INFO;
  1270. // free memory if at at all it is allocated
  1271. FreeMemory((LPVOID*)& m_pszRunAsUserName);
  1272. m_pszRunAsUserName = (LPTSTR) AllocateMemory(GetBufferSize((LPVOID)m_pszUserName)+1);
  1273. if( (NULL == m_pszRunAsUserName))
  1274. {
  1275. throw CShowError(E_OUTOFMEMORY);
  1276. }
  1277. StringCopy(m_pszRunAsUserName,m_pszUserName,SIZE_OF_DYN_ARRAY(m_pszRunAsUserName));
  1278. // ask for the password (rp).
  1279. // NOTE: memory is already allocated for 'm_pszRunAsUserPassword'.
  1280. StringCopy(szTemp, GetResString(IDS_ASK_PASSWORD),
  1281. SIZE_OF_ARRAY(szTemp));
  1282. StringCchPrintfW(szTemp1, SIZE_OF_ARRAY(szTemp1), szTemp,
  1283. m_pszRunAsUserName);
  1284. ShowMessage(stdout,szTemp1);
  1285. GetPassword(m_pszRunAsUserPassword,
  1286. SIZE_OF_DYN_ARRAY(m_pszRunAsUserPassword));
  1287. if( 0 == StringLength(m_pszRunAsUserPassword,0))
  1288. {
  1289. ShowMessage(stderr,GetResString(IDS_WARN_NULL_PASSWORD));
  1290. }
  1291. }
  1292. else if ((0 == cmdOptions[ ID_C_USERNAME ].dwActuals) &&
  1293. (1 == cmdOptions[ ID_C_RU ].dwActuals) &&
  1294. (0 == cmdOptions[ ID_C_RP ].dwActuals))
  1295. {
  1296. // ask for the password (rp).
  1297. // NOTE: memory is already allocated for 'm_pszRunAsUserPassword'.
  1298. StringCopy(szTemp, GetResString(IDS_ASK_PASSWORD),
  1299. SIZE_OF_ARRAY(szTemp));
  1300. StringCchPrintfW(szTemp1, SIZE_OF_ARRAY(szTemp1), szTemp,
  1301. m_pszRunAsUserName);
  1302. ShowMessage(stdout,szTemp1);
  1303. GetPassword(m_pszRunAsUserPassword,
  1304. SIZE_OF_DYN_ARRAY(m_pszRunAsUserPassword));
  1305. if( 0 == StringLength(m_pszRunAsUserPassword,0))
  1306. {
  1307. ShowMessage(stderr,GetResString(IDS_WARN_NULL_PASSWORD));
  1308. }
  1309. }
  1310. else if ((1 == cmdOptions[ ID_C_USERNAME ].dwActuals) &&
  1311. (1 == cmdOptions[ ID_C_RU ].dwActuals) &&
  1312. (0 == cmdOptions[ ID_C_RP ].dwActuals))
  1313. {
  1314. if( 0 == StringCompare(m_pszUserName,m_pszRunAsUserName,TRUE,0))
  1315. {
  1316. StringCopy(m_pszRunAsUserPassword,m_pszPassword,
  1317. SIZE_OF_ARRAY(m_pszRunAsUserPassword));
  1318. }
  1319. else
  1320. {
  1321. StringCopy(szTemp,GetResString(IDS_ASK_PASSWORD),
  1322. SIZE_OF_ARRAY(szTemp));
  1323. StringCchPrintfW(szTemp1, SIZE_OF_ARRAY(szTemp1), szTemp,
  1324. m_pszRunAsUserName);
  1325. ShowMessage(stdout,szTemp1);
  1326. GetPassword( m_pszRunAsUserPassword,
  1327. SIZE_OF_DYN_ARRAY(m_pszRunAsUserPassword));
  1328. if(StringLength(m_pszRunAsUserPassword,0) == 0)
  1329. {
  1330. ShowMessage(stderr,
  1331. GetResString(IDS_WARN_NULL_PASSWORD));
  1332. }
  1333. }
  1334. }
  1335. }
  1336. DEBUG_INFO;
  1337. return;
  1338. }
  1339. void
  1340. CETCreate::AssignMinMemory(
  1341. void
  1342. )
  1343. /*++
  1344. Routine Description:
  1345. This function will allocate memory to those string pointer which
  1346. are NULL.
  1347. NOTE
  1348. Arguments:
  1349. None
  1350. Return Value:
  1351. none
  1352. --*/
  1353. {
  1354. DEBUG_INFO;
  1355. if( NULL == m_pszServerName)
  1356. {
  1357. m_pszServerName = (LPTSTR)AllocateMemory(MAX_STRING_LENGTH * sizeof(WCHAR));
  1358. if(NULL == m_pszServerName)
  1359. {
  1360. throw CShowError(E_OUTOFMEMORY);
  1361. }
  1362. }
  1363. if( NULL == m_pszUserName)
  1364. {
  1365. m_pszUserName = (LPTSTR)AllocateMemory(MAX_STRING_LENGTH* sizeof(WCHAR));
  1366. if(NULL == m_pszUserName)
  1367. {
  1368. throw CShowError(E_OUTOFMEMORY);
  1369. }
  1370. }
  1371. if( NULL == m_pszPassword)
  1372. {
  1373. m_pszPassword = (LPTSTR)AllocateMemory(MAX_STRING_LENGTH* sizeof(WCHAR));
  1374. if(NULL == m_pszPassword)
  1375. {
  1376. throw CShowError(E_OUTOFMEMORY);
  1377. }
  1378. }
  1379. if( NULL == m_pszRunAsUserName)
  1380. {
  1381. m_pszRunAsUserName = (LPTSTR)AllocateMemory(MAX_STRING_LENGTH* sizeof(WCHAR));
  1382. if(NULL == m_pszRunAsUserName)
  1383. {
  1384. throw CShowError(E_OUTOFMEMORY);
  1385. }
  1386. }
  1387. if( NULL == m_pszRunAsUserPassword)
  1388. {
  1389. m_pszRunAsUserPassword = (LPTSTR)AllocateMemory(MAX_STRING_LENGTH* sizeof(WCHAR));
  1390. if(NULL == m_pszRunAsUserPassword)
  1391. {
  1392. throw CShowError(E_OUTOFMEMORY);
  1393. }
  1394. }
  1395. DEBUG_INFO;
  1396. }
  1397. void
  1398. CETCreate::SetToLoggedOnUser(
  1399. void
  1400. )
  1401. /*++
  1402. Routine Description:
  1403. This function will Set RunAsUser to current logged on user.
  1404. and also ask for its password (RunAsPassword).
  1405. Arguments:
  1406. None
  1407. Return Value:
  1408. none
  1409. --*/
  1410. {
  1411. TCHAR szTemp[MAX_STRING_LENGTH]; // To Show Messages
  1412. TCHAR szTemp1[MAX_STRING_LENGTH];// To Show Messages
  1413. TCHAR szWarnPassWord[MAX_STRING_LENGTH];
  1414. SecureZeroMemory(szTemp,sizeof(szTemp));
  1415. SecureZeroMemory(szTemp1,sizeof(szTemp1));
  1416. SecureZeroMemory(szWarnPassWord,sizeof(szWarnPassWord));
  1417. StringCchPrintfW(szWarnPassWord,SIZE_OF_ARRAY(szWarnPassWord),
  1418. GetResString(IDS_WARNING_PASSWORD),NTAUTHORITY_USER);
  1419. // Get Current logged on user name.
  1420. ULONG ulSize = UNLEN + 1;
  1421. // free memory if at at all it is allocated
  1422. FreeMemory((LPVOID*)& m_pszRunAsUserName);
  1423. m_pszRunAsUserName = (LPTSTR) AllocateMemory( ulSize * sizeof( WCHAR ));
  1424. if( (NULL == m_pszRunAsUserName))
  1425. {
  1426. throw CShowError(E_OUTOFMEMORY);
  1427. }
  1428. if(0 == GetUserName(m_pszRunAsUserName,&ulSize))
  1429. {
  1430. // display error
  1431. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_SYSTEM);
  1432. throw 5000;
  1433. }
  1434. // ask for the password (rp).
  1435. // NOTE: memory is already allocated for 'm_pszRunAsUserPassword'.
  1436. StringCopy(szTemp, GetResString(IDS_ASK_PASSWORD),
  1437. SIZE_OF_ARRAY(szTemp));
  1438. StringCchPrintfW(szTemp1, SIZE_OF_ARRAY(szTemp1), szTemp,
  1439. m_pszRunAsUserName);
  1440. ShowMessage(stdout,szTemp1);
  1441. GetPassword(m_pszRunAsUserPassword,
  1442. SIZE_OF_DYN_ARRAY(m_pszRunAsUserPassword));
  1443. if( 0 == StringLength(m_pszRunAsUserPassword,0))
  1444. {
  1445. ShowMessage(stderr,GetResString(IDS_WARN_NULL_PASSWORD));
  1446. }
  1447. }
  1448. BOOL
  1449. CETCreate::CreateXPResults(
  1450. void
  1451. )
  1452. /*++
  1453. Routine Description:
  1454. This function creates a new trigger if not present on a remote XP machine.
  1455. This function is for compatibility of .NET to XP machine only.
  1456. Arguments:
  1457. NONE
  1458. Return Value:
  1459. BOOL: TRUE - If succedded in creating a new trigger results.
  1460. FALSE- otherwise
  1461. --*/
  1462. {
  1463. DWORD dwRetVal = 0; // Check whether creation of trigger is succesful.
  1464. HRESULT hr = S_OK;
  1465. try
  1466. {
  1467. // retrieves TriggerEventCosumer class
  1468. DEBUG_INFO;
  1469. hr = m_pWbemServices->GetObject(_bstr_t( CLS_TRIGGER_EVENT_CONSUMER ),
  1470. 0, NULL, &m_pClass, NULL);
  1471. ON_ERROR_THROW_EXCEPTION(hr);
  1472. // Gets information about the "CreateETrigger" method of
  1473. // "TriggerEventCosumer" class
  1474. DEBUG_INFO;
  1475. hr = m_pClass->GetMethod(_bstr_t( FN_CREATE_ETRIGGER_XP ), 0, &m_pInClass, NULL);
  1476. ON_ERROR_THROW_EXCEPTION(hr);
  1477. // create a new instance of a class "TriggerEventCosumer".
  1478. DEBUG_INFO;
  1479. hr = m_pInClass->SpawnInstance(0, &m_pInInst);
  1480. ON_ERROR_THROW_EXCEPTION(hr);
  1481. // Set the sTriggerName property .
  1482. // sets a "TriggerName" property for Newly created Instance
  1483. DEBUG_INFO;
  1484. hr = PropertyPut(m_pInInst,FPR_TRIGGER_NAME,(m_szTriggerName));
  1485. ON_ERROR_THROW_EXCEPTION(hr);
  1486. // Set the sTriggerAction property to Variant.
  1487. DEBUG_INFO;
  1488. hr = PropertyPut(m_pInInst,FPR_TRIGGER_ACTION,(m_szTaskName));
  1489. ON_ERROR_THROW_EXCEPTION(hr);
  1490. // Set the sTriggerDesc property to Variant .
  1491. DEBUG_INFO;
  1492. hr = PropertyPut(m_pInInst,FPR_TRIGGER_DESC,(m_szDescription));
  1493. ON_ERROR_THROW_EXCEPTION(hr);
  1494. // Set the RunAsUserName property .
  1495. DEBUG_INFO;
  1496. hr = PropertyPut(m_pInInst,FPR_RUN_AS_USER,(m_pszRunAsUserName));
  1497. ON_ERROR_THROW_EXCEPTION(hr);
  1498. // Set the RunAsUserNamePAssword property .
  1499. DEBUG_INFO;
  1500. hr = PropertyPut( m_pInInst,FPR_RUN_AS_USER_PASSWORD,
  1501. (m_pszRunAsUserPassword));
  1502. ON_ERROR_THROW_EXCEPTION(hr);
  1503. // Password is no longer is needed now. For security reason release it.
  1504. FreeMemory((LPVOID*)& m_pszRunAsUserPassword);
  1505. StringCopy(m_szWMIQueryString ,QUERY_STRING,SIZE_OF_ARRAY(m_szWMIQueryString));
  1506. if( TRUE == ConstructWMIQueryString())
  1507. {
  1508. TCHAR szMsgString[MAX_RES_STRING * 4];
  1509. TCHAR szMsgFormat[MAX_RES_STRING];
  1510. VARIANT vtValue;
  1511. // initialize the variant and then get the value of the specified property
  1512. VariantInit( &vtValue );
  1513. DEBUG_INFO;
  1514. hr = PropertyPut(m_pInInst,FPR_TRIGGER_QUERY,m_szWMIQueryString);
  1515. ON_ERROR_THROW_EXCEPTION(hr);
  1516. // All The required properties sets, so
  1517. // executes CreateETrigger method to create eventtrigger
  1518. DEBUG_INFO;
  1519. hr = m_pWbemServices->ExecMethod(_bstr_t(CLS_TRIGGER_EVENT_CONSUMER),
  1520. _bstr_t(FN_CREATE_ETRIGGER_XP),
  1521. 0, NULL, m_pInInst, &m_pOutInst,NULL);
  1522. ON_ERROR_THROW_EXCEPTION( hr );
  1523. DEBUG_INFO;
  1524. hr = m_pOutInst->Get( _bstr_t( FPR_RETURN_VALUE ), 0, &vtValue, NULL, NULL );
  1525. ON_ERROR_THROW_EXCEPTION( hr );
  1526. //Get Output paramters.
  1527. dwRetVal = ( DWORD )vtValue.lVal;
  1528. VariantClear( &vtValue );
  1529. switch( dwRetVal )
  1530. {
  1531. case 0: // Success i ncreation a new trigger.
  1532. // SUCCESS: message on screen
  1533. DEBUG_INFO;
  1534. StringCopy(szMsgFormat, GetResString(IDS_CREATE_SUCCESS),
  1535. SIZE_OF_ARRAY(szMsgFormat));
  1536. StringCchPrintfW(szMsgString,SIZE_OF_ARRAY(szMsgString),
  1537. szMsgFormat,_X(m_szTriggerName));
  1538. DEBUG_INFO;
  1539. // Message shown on screen will be...
  1540. // SUCCESS: The Event Trigger "EventTrigger Name" has
  1541. // successfully been created.
  1542. ShowMessage(stdout,L"\n");
  1543. ShowMessage(stdout,szMsgString);
  1544. break;
  1545. case 1: // Duplicate id found. Failed to create.
  1546. // Show Error Message
  1547. DEBUG_INFO;
  1548. StringCopy(szMsgFormat, GetResString(IDS_DUPLICATE_TRG_NAME),
  1549. SIZE_OF_ARRAY(szMsgFormat));
  1550. StringCchPrintfW(szMsgString,SIZE_OF_ARRAY(szMsgString),
  1551. szMsgFormat,_X(m_szTriggerName));
  1552. // Message shown on screen will be...
  1553. // ERROR:Event Trigger Name "EventTrigger Name"
  1554. // already exits.
  1555. ShowMessage(stderr,szMsgString);
  1556. return FALSE;
  1557. case 2: // Means ru is invalid so show warning....
  1558. DEBUG_INFO;
  1559. // WARNING: The new event trigger ""%s"" has been created,
  1560. // but may not run because the account information could not be set.
  1561. StringCchPrintf ( szMsgString, SIZE_OF_ARRAY(szMsgString),
  1562. GetResString(IDS_INVALID_R_U), _X(m_szTriggerName));
  1563. ShowMessage ( stderr, _X(szMsgString));
  1564. return FALSE;
  1565. break;
  1566. default:
  1567. // Control should not come here.
  1568. DEBUG_INFO;
  1569. StringCopy(szMsgFormat, GetResString(IDS_DUPLICATE_TRG_NAME),
  1570. SIZE_OF_ARRAY(szMsgFormat));
  1571. StringCchPrintfW(szMsgString,SIZE_OF_ARRAY(szMsgString),
  1572. szMsgFormat,_X(m_szTriggerName));
  1573. // Message shown on screen will be...
  1574. // ERROR:Event Trigger Name "EventTrigger Name"
  1575. // already exits.
  1576. ShowMessage(stderr,szMsgString);
  1577. return FALSE;
  1578. break;
  1579. }
  1580. }
  1581. else
  1582. {
  1583. return FALSE;
  1584. }
  1585. }
  1586. catch( _com_error e )
  1587. {
  1588. DEBUG_INFO;
  1589. // WMI returns string for this hr value is "Not Found." which is not
  1590. // user friendly. So changing the message text.
  1591. if( 0x80041002 == hr )
  1592. {
  1593. ShowMessage( stderr,GetResString(IDS_CLASS_NOT_REG));
  1594. }
  1595. else
  1596. {
  1597. DEBUG_INFO;
  1598. WMISaveError( e );
  1599. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  1600. }
  1601. return FALSE;
  1602. }
  1603. catch( CHeap_Exception )
  1604. {
  1605. WMISaveError( WBEM_E_OUT_OF_MEMORY );
  1606. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  1607. return FALSE;
  1608. }
  1609. // Operation successful.
  1610. return TRUE;
  1611. }