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.

558 lines
18 KiB

  1. /******************************************************************************
  2. Copyright(c) Microsoft Corporation
  3. Module Name:
  4. end.cpp
  5. Abstract:
  6. This module terminates the schedule task which is currently running in the system
  7. Author:
  8. Venu Gopal Choudary 12-Mar-2001
  9. Revision History:
  10. Venu Gopal Choudary 12-Mar-2001 : Created it
  11. ******************************************************************************/
  12. //common header files needed for this file
  13. #include "pch.h"
  14. #include "CommonHeaderFiles.h"
  15. // Function declaration for the Usage function.
  16. VOID DisplayEndUsage();
  17. /*****************************************************************************
  18. Routine Description:
  19. This routine terminates the scheduled task(s)
  20. Arguments:
  21. [ in ] argc : Number of command line arguments
  22. [ in ] argv : Array containing command line arguments
  23. Return Value :
  24. A DWORD value indicating EXIT_SUCCESS on success else
  25. EXIT_FAILURE on failure
  26. *****************************************************************************/
  27. DWORD
  28. TerminateScheduledTask(
  29. IN DWORD argc,
  30. IN LPCTSTR argv[]
  31. )
  32. {
  33. // Variables used to find whether End option, Usage option
  34. // are specified or not
  35. BOOL bEnd = FALSE;
  36. BOOL bUsage = FALSE;
  37. BOOL bFlag = FALSE;
  38. // Set the TaskSchduler object as NULL
  39. ITaskScheduler *pITaskScheduler = NULL;
  40. // Return value
  41. HRESULT hr = S_OK;
  42. // Initialising the variables that are passed to TCMDPARSER structure
  43. LPWSTR szServer = NULL;
  44. WCHAR szTaskName[ MAX_JOB_LEN ] = L"\0";
  45. LPWSTR szUser = NULL;
  46. LPWSTR szPassword = NULL;
  47. // Dynamic Array contaning array of jobs
  48. TARRAY arrJobs = NULL;
  49. BOOL bNeedPassword = FALSE;
  50. BOOL bResult = FALSE;
  51. BOOL bCloseConnection = TRUE;
  52. //buffer for displaying error message
  53. WCHAR szMessage[2 * MAX_STRING_LENGTH] = L"\0";
  54. TCMDPARSER2 cmdEndOptions[MAX_END_OPTIONS];
  55. BOOL bReturn = FALSE;
  56. DWORD dwCheck = 0;
  57. DWORD dwPolicy = 0;
  58. // /run sub-options
  59. const WCHAR szEndnOpt[] = L"end";
  60. const WCHAR szEndHelpOpt[] = L"?";
  61. const WCHAR szEndServerOpt[] = L"s";
  62. const WCHAR szEndUserOpt[] = L"u";
  63. const WCHAR szEndPwdOpt[] = L"p";
  64. const WCHAR szEndTaskNameOpt[] = L"tn";
  65. // set all the fields to 0
  66. SecureZeroMemory( cmdEndOptions, sizeof( TCMDPARSER2 ) * MAX_END_OPTIONS );
  67. //
  68. // fill the commandline parser
  69. //
  70. // /delete option
  71. StringCopyA( cmdEndOptions[ OI_END_OPTION ].szSignature, "PARSER2\0", 8 );
  72. cmdEndOptions[ OI_END_OPTION ].dwType = CP_TYPE_BOOLEAN;
  73. cmdEndOptions[ OI_END_OPTION ].pwszOptions = szEndnOpt;
  74. cmdEndOptions[ OI_END_OPTION ].dwCount = 1;
  75. cmdEndOptions[ OI_END_OPTION ].dwFlags = 0;
  76. cmdEndOptions[ OI_END_OPTION ].pValue = &bEnd;
  77. // /? option
  78. StringCopyA( cmdEndOptions[ OI_END_USAGE ].szSignature, "PARSER2\0", 8 );
  79. cmdEndOptions[ OI_END_USAGE ].dwType = CP_TYPE_BOOLEAN;
  80. cmdEndOptions[ OI_END_USAGE ].pwszOptions = szEndHelpOpt;
  81. cmdEndOptions[ OI_END_USAGE ].dwCount = 1;
  82. cmdEndOptions[ OI_END_USAGE ].dwFlags = CP2_USAGE;
  83. cmdEndOptions[ OI_END_USAGE ].pValue = &bUsage;
  84. // /s option
  85. StringCopyA( cmdEndOptions[ OI_END_SERVER ].szSignature, "PARSER2\0", 8 );
  86. cmdEndOptions[ OI_END_SERVER ].dwType = CP_TYPE_TEXT;
  87. cmdEndOptions[ OI_END_SERVER].pwszOptions = szEndServerOpt;
  88. cmdEndOptions[ OI_END_SERVER ].dwCount = 1;
  89. cmdEndOptions[ OI_END_SERVER ].dwFlags = CP2_ALLOCMEMORY| CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL ;
  90. // /u option
  91. StringCopyA( cmdEndOptions[ OI_END_USERNAME ].szSignature, "PARSER2\0", 8 );
  92. cmdEndOptions[ OI_END_USERNAME ].dwType = CP_TYPE_TEXT;
  93. cmdEndOptions[ OI_END_USERNAME ].pwszOptions = szEndUserOpt;
  94. cmdEndOptions[ OI_END_USERNAME ].dwCount = 1;
  95. cmdEndOptions[ OI_END_USERNAME ].dwFlags = CP2_ALLOCMEMORY| CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL ;
  96. // /p option
  97. StringCopyA( cmdEndOptions[ OI_END_PASSWORD ].szSignature, "PARSER2\0", 8 );
  98. cmdEndOptions[ OI_END_PASSWORD ].dwType = CP_TYPE_TEXT;
  99. cmdEndOptions[ OI_END_PASSWORD ].pwszOptions = szEndPwdOpt;
  100. cmdEndOptions[ OI_END_PASSWORD ].dwCount = 1;
  101. cmdEndOptions[ OI_END_PASSWORD ].dwActuals = 0;
  102. cmdEndOptions[ OI_END_PASSWORD ].dwFlags = CP2_ALLOCMEMORY | CP2_VALUE_OPTIONAL;
  103. // /tn option
  104. StringCopyA( cmdEndOptions[ OI_END_TASKNAME ].szSignature, "PARSER2\0", 8 );
  105. cmdEndOptions[ OI_END_TASKNAME ].dwType = CP_TYPE_TEXT;
  106. cmdEndOptions[ OI_END_TASKNAME ].pwszOptions = szEndTaskNameOpt;
  107. cmdEndOptions[ OI_END_TASKNAME ].dwCount = 1;
  108. cmdEndOptions[ OI_END_TASKNAME ].dwFlags = CP2_MANDATORY;
  109. cmdEndOptions[ OI_END_TASKNAME ].pValue = szTaskName;
  110. cmdEndOptions[ OI_END_TASKNAME ].dwLength = MAX_JOB_LEN;
  111. //parse command line arguments
  112. bReturn = DoParseParam2( argc, argv, 0, SIZE_OF_ARRAY(cmdEndOptions), cmdEndOptions, 0);
  113. if( FALSE == bReturn) // Invalid commandline
  114. {
  115. //display an error message
  116. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  117. ReleaseGlobals();
  118. return EXIT_FAILURE;
  119. }
  120. // get the buffer pointers allocated by command line parser
  121. szServer = (LPWSTR)cmdEndOptions[ OI_RUN_SERVER ].pValue;
  122. szUser = (LPWSTR)cmdEndOptions[ OI_RUN_USERNAME ].pValue;
  123. szPassword = (LPWSTR)cmdEndOptions[ OI_RUN_PASSWORD ].pValue;
  124. if ( (argc > 3) && (bUsage == TRUE) )
  125. {
  126. ShowMessage ( stderr, GetResString (IDS_ERROR_ENDPARAM) );
  127. FreeMemory((LPVOID*) &szServer);
  128. FreeMemory((LPVOID*) &szUser);
  129. FreeMemory((LPVOID*) &szPassword);
  130. return EXIT_FAILURE;
  131. }
  132. // Displaying end usage if user specified -? with -run option
  133. if( bUsage == TRUE )
  134. {
  135. DisplayEndUsage();
  136. FreeMemory((LPVOID*) &szServer);
  137. FreeMemory((LPVOID*) &szUser);
  138. FreeMemory((LPVOID*) &szPassword);
  139. return EXIT_SUCCESS;
  140. }
  141. // check for invalid user name
  142. if( ( cmdEndOptions[OI_END_SERVER].dwActuals == 0 ) && ( cmdEndOptions[OI_END_USERNAME].dwActuals == 1 ) )
  143. {
  144. ShowMessage(stderr, GetResString(IDS_END_USER_BUT_NOMACHINE));
  145. FreeMemory((LPVOID*) &szServer);
  146. FreeMemory((LPVOID*) &szUser);
  147. FreeMemory((LPVOID*) &szPassword);
  148. return RETVAL_FAIL;
  149. }
  150. // check for invalid username
  151. if ( cmdEndOptions[ OI_END_USERNAME ].dwActuals == 0 && cmdEndOptions[ OI_END_PASSWORD ].dwActuals == 1 )
  152. {
  153. // invalid syntax
  154. ShowMessage(stderr, GetResString(IDS_EPASSWORD_BUT_NOUSERNAME));
  155. FreeMemory((LPVOID*) &szServer);
  156. FreeMemory((LPVOID*) &szUser);
  157. FreeMemory((LPVOID*) &szPassword);
  158. return RETVAL_FAIL; // indicate failure
  159. }
  160. // check for the length of the taskname
  161. if( ( StringLength( szTaskName, 0 ) > MAX_JOB_LEN ) )
  162. {
  163. ShowMessage(stderr, GetResString(IDS_INVALID_TASKLENGTH));
  164. FreeMemory((LPVOID*) &szServer);
  165. FreeMemory((LPVOID*) &szUser);
  166. FreeMemory((LPVOID*) &szPassword);
  167. return RETVAL_FAIL;
  168. }
  169. //for holding values of parameters in FormatMessage()
  170. WCHAR* szValues[1] = {NULL};
  171. // check whether the password (-p) specified in the command line or not
  172. // and also check whether '*' or empty is given for -p or not
  173. // check whether the password (-p) specified in the command line or not
  174. // and also check whether '*' or empty is given for -p or not
  175. // check whether the password (-p) specified in the command line or not
  176. // and also check whether '*' or empty is given for -p or not
  177. // check the remote connectivity information
  178. if ( szServer != NULL )
  179. {
  180. //
  181. // if -u is not specified, we need to allocate memory
  182. // in order to be able to retrive the current user name
  183. //
  184. // case 1: -p is not at all specified
  185. // as the value for this switch is optional, we have to rely
  186. // on the dwActuals to determine whether the switch is specified or not
  187. // in this case utility needs to try to connect first and if it fails
  188. // then prompt for the password -- in fact, we need not check for this
  189. // condition explicitly except for noting that we need to prompt for the
  190. // password
  191. //
  192. // case 2: -p is specified
  193. // but we need to check whether the value is specified or not
  194. // in this case user wants the utility to prompt for the password
  195. // before trying to connect
  196. //
  197. // case 3: -p * is specified
  198. // user name
  199. if ( szUser == NULL )
  200. {
  201. szUser = (LPWSTR) AllocateMemory( MAX_STRING_LENGTH * sizeof( WCHAR ) );
  202. if ( szUser == NULL )
  203. {
  204. SaveLastError();
  205. FreeMemory((LPVOID*) &szServer);
  206. FreeMemory((LPVOID*) &szUser);
  207. FreeMemory((LPVOID*) &szPassword);
  208. return RETVAL_FAIL;
  209. }
  210. }
  211. // password
  212. if ( szPassword == NULL )
  213. {
  214. bNeedPassword = TRUE;
  215. szPassword = (LPWSTR)AllocateMemory( MAX_STRING_LENGTH * sizeof( WCHAR ) );
  216. if ( szPassword == NULL )
  217. {
  218. SaveLastError();
  219. FreeMemory((LPVOID*) &szServer);
  220. FreeMemory((LPVOID*) &szUser);
  221. FreeMemory((LPVOID*) &szPassword);
  222. return RETVAL_FAIL;
  223. }
  224. }
  225. // case 1
  226. if ( cmdEndOptions[ OI_END_PASSWORD ].dwActuals == 0 )
  227. {
  228. // we need not do anything special here
  229. }
  230. // case 2
  231. else if ( cmdEndOptions[ OI_END_PASSWORD ].pValue == NULL )
  232. {
  233. StringCopy( szPassword, L"*", GetBufferSize(szPassword)/sizeof(WCHAR));
  234. }
  235. // case 3
  236. else if ( StringCompareEx( szPassword, L"*", TRUE, 0 ) == 0 )
  237. {
  238. if ( ReallocateMemory( (LPVOID*)&szPassword,
  239. MAX_STRING_LENGTH * sizeof( WCHAR ) ) == FALSE )
  240. {
  241. SaveLastError();
  242. FreeMemory((LPVOID*) &szServer);
  243. FreeMemory((LPVOID*) &szUser);
  244. FreeMemory((LPVOID*) &szPassword);
  245. return RETVAL_FAIL;
  246. }
  247. // ...
  248. bNeedPassword = TRUE;
  249. }
  250. }
  251. if( ( IsLocalSystem( szServer ) == FALSE ) || ( cmdEndOptions[OI_END_USERNAME].dwActuals == 1 ))
  252. {
  253. bFlag = TRUE;
  254. // Establish the connection on a remote machine
  255. bResult = EstablishConnection(szServer,szUser,GetBufferSize(szUser)/sizeof(WCHAR),szPassword,GetBufferSize(szPassword)/sizeof(WCHAR), bNeedPassword);
  256. if (bResult == FALSE)
  257. {
  258. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  259. //ShowMessage( stderr, GetResString(IDS_ERROR_STRING) );
  260. //ShowMessage( stderr, GetReason());
  261. FreeMemory((LPVOID*) &szServer);
  262. FreeMemory((LPVOID*) &szUser);
  263. FreeMemory((LPVOID*) &szPassword);
  264. return EXIT_FAILURE ;
  265. }
  266. else
  267. {
  268. // though the connection is successfull, some conflict might have occured
  269. switch( GetLastError() )
  270. {
  271. case I_NO_CLOSE_CONNECTION:
  272. bCloseConnection = FALSE;
  273. break;
  274. case E_LOCAL_CREDENTIALS:
  275. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  276. {
  277. bCloseConnection = FALSE;
  278. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  279. //ShowMessage( stderr, GetResString(IDS_ERROR_STRING) );
  280. //ShowMessage( stderr, GetReason());
  281. FreeMemory((LPVOID*) &szServer);
  282. FreeMemory((LPVOID*) &szUser);
  283. FreeMemory((LPVOID*) &szPassword);
  284. return EXIT_FAILURE;
  285. }
  286. default :
  287. bCloseConnection = TRUE;
  288. }
  289. }
  290. //release memory for password
  291. FreeMemory((LPVOID*) &szPassword);
  292. }
  293. // Get the task Scheduler object for the machine.
  294. pITaskScheduler = GetTaskScheduler( szServer );
  295. // If the Task Scheduler is not defined then give the error message.
  296. if ( pITaskScheduler == NULL )
  297. {
  298. if ( (TRUE == bFlag) && (bCloseConnection == TRUE) )
  299. {
  300. CloseConnection( szServer );
  301. }
  302. Cleanup(pITaskScheduler);
  303. FreeMemory((LPVOID*) &szServer);
  304. FreeMemory((LPVOID*) &szUser);
  305. FreeMemory((LPVOID*) &szPassword);
  306. return EXIT_FAILURE;
  307. }
  308. // check whether the task scheduler service is running or not.
  309. if ( TRUE == CheckServiceStatus(szServer, &dwCheck, FALSE) )
  310. {
  311. ShowMessage ( stderr, GetResString (IDS_SERVICE_NOT_RUNNING) );
  312. }
  313. // Validate the Given Task and get as TARRAY in case of taskname
  314. arrJobs = ValidateAndGetTasks( pITaskScheduler, szTaskName);
  315. if( arrJobs == NULL )
  316. {
  317. StringCchPrintf( szMessage , SIZE_OF_ARRAY(szMessage), GetResString(IDS_TASKNAME_NOTEXIST), _X( szTaskName ));
  318. ShowMessage(stderr, szMessage );
  319. if ( (TRUE == bFlag) && (bCloseConnection == TRUE) )
  320. {
  321. CloseConnection( szServer );
  322. }
  323. Cleanup(pITaskScheduler);
  324. FreeMemory((LPVOID*) &szServer);
  325. FreeMemory((LPVOID*) &szUser);
  326. FreeMemory((LPVOID*) &szPassword);
  327. return EXIT_FAILURE;
  328. }
  329. // check whether the group policy prevented user from running or not.
  330. if ( FALSE == GetGroupPolicy( szServer, szUser, TS_KEYPOLICY_DENY_EXECUTION, &dwPolicy ) )
  331. {
  332. if ( (TRUE == bFlag) && (bCloseConnection == TRUE) )
  333. {
  334. CloseConnection( szServer );
  335. }
  336. Cleanup(pITaskScheduler);
  337. FreeMemory((LPVOID*) &szServer);
  338. FreeMemory((LPVOID*) &szUser);
  339. FreeMemory((LPVOID*) &szPassword);
  340. return EXIT_FAILURE;
  341. }
  342. if ( dwPolicy > 0 )
  343. {
  344. ShowMessage ( stdout, GetResString (IDS_PREVENT_END));
  345. if ( (TRUE == bFlag) && (bCloseConnection == TRUE) )
  346. {
  347. CloseConnection( szServer );
  348. }
  349. Cleanup(pITaskScheduler);
  350. FreeMemory((LPVOID*) &szServer);
  351. FreeMemory((LPVOID*) &szUser);
  352. FreeMemory((LPVOID*) &szPassword);
  353. return EXIT_SUCCESS;
  354. }
  355. IPersistFile *pIPF = NULL;
  356. ITask *pITask = NULL;
  357. StringConcat ( szTaskName, JOB, SIZE_OF_ARRAY(szTaskName) );
  358. // return an pITask inteface for szTaskName
  359. hr = pITaskScheduler->Activate(szTaskName,IID_ITask,
  360. (IUnknown**) &pITask);
  361. if (FAILED(hr))
  362. {
  363. SetLastError ((DWORD) hr);
  364. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  365. if( pIPF )
  366. pIPF->Release();
  367. if( pITask )
  368. pITask->Release();
  369. if ( (TRUE == bFlag) && (bCloseConnection == TRUE) )
  370. {
  371. CloseConnection( szServer );
  372. }
  373. Cleanup(pITaskScheduler);
  374. FreeMemory((LPVOID*) &szServer);
  375. FreeMemory((LPVOID*) &szUser);
  376. FreeMemory((LPVOID*) &szPassword);
  377. return EXIT_FAILURE;
  378. }
  379. //WCHAR szBuffer[2 * MAX_STRING_LENGTH] = L"\0";
  380. if ( ParseTaskName( szTaskName ) )
  381. {
  382. if( pIPF )
  383. pIPF->Release();
  384. if( pITask )
  385. pITask->Release();
  386. if ( (TRUE == bFlag) && (bCloseConnection == TRUE) )
  387. {
  388. CloseConnection( szServer );
  389. }
  390. Cleanup(pITaskScheduler);
  391. FreeMemory((LPVOID*) &szServer);
  392. FreeMemory((LPVOID*) &szUser);
  393. FreeMemory((LPVOID*) &szPassword);
  394. return EXIT_FAILURE;
  395. }
  396. // terminate the scheduled task
  397. hr = pITask->Terminate();
  398. if ( FAILED(hr) )
  399. {
  400. SetLastError ((DWORD) hr);
  401. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  402. if( pIPF )
  403. pIPF->Release();
  404. if( pITask )
  405. pITask->Release();
  406. if ( (TRUE == bFlag) && (bCloseConnection == TRUE) )
  407. {
  408. CloseConnection( szServer );
  409. }
  410. Cleanup(pITaskScheduler);
  411. FreeMemory((LPVOID*) &szServer);
  412. FreeMemory((LPVOID*) &szUser);
  413. FreeMemory((LPVOID*) &szPassword);
  414. return EXIT_FAILURE;
  415. }
  416. else
  417. {
  418. szValues[0] = (WCHAR*) (szTaskName);
  419. StringCchPrintf ( szMessage, SIZE_OF_ARRAY(szMessage), GetResString(IDS_END_SUCCESSFUL), _X(szTaskName));
  420. ShowMessage(stdout, _X(szMessage));
  421. }
  422. if( pIPF )
  423. pIPF->Release();
  424. if( pITask )
  425. pITask->Release();
  426. if ( (TRUE == bFlag) && (bCloseConnection == TRUE) )
  427. {
  428. CloseConnection( szServer );
  429. }
  430. Cleanup(pITaskScheduler);
  431. FreeMemory((LPVOID*) &szServer);
  432. FreeMemory((LPVOID*) &szUser);
  433. FreeMemory((LPVOID*) &szPassword);
  434. return EXIT_SUCCESS;
  435. }
  436. /*****************************************************************************
  437. Routine Description:
  438. This routine displays the usage of -end option
  439. Arguments:
  440. None
  441. Return Value :
  442. VOID
  443. ******************************************************************************/
  444. VOID
  445. DisplayEndUsage()
  446. {
  447. // Displaying run option usage
  448. DisplayUsage( IDS_END_HLP1, IDS_END_HLP17);
  449. }