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.

3347 lines
120 KiB

  1. /******************************************************************************
  2. Copyright(c) Microsoft Corporation
  3. Module Name:
  4. create.cpp
  5. Abstract:
  6. This module validates the options specied by the user & if correct creates
  7. a scheduled task.
  8. Author:
  9. Raghu babu 10-oct-2000
  10. Revision History:
  11. Raghu babu 10-Oct-2000 : Created it
  12. G.Surender Reddy 25-oct-2000 : Modified it
  13. G.Surender Reddy 27-oct-2000 : Modified it
  14. G.Surender Reddy 30-oct-2000 : Modified it
  15. ******************************************************************************/
  16. //common header files needed for this file
  17. #include "pch.h"
  18. #include "CommonHeaderFiles.h"
  19. /******************************************************************************
  20. Routine Description:
  21. This routine initialises the variables to neutral values ,helps in
  22. creating a new scheduled task
  23. Arguments:
  24. [ in ] argc : The count of arguments specified in the command line
  25. [ in ] argv : Array of command line arguments
  26. Return Value :
  27. A HRESULT value indicating S_OK on success else S_FALSE on failure
  28. ******************************************************************************/
  29. HRESULT
  30. CreateScheduledTask(
  31. IN DWORD argc ,
  32. IN LPCTSTR argv[]
  33. )
  34. {
  35. // declarations of structures
  36. TCREATESUBOPTS tcresubops;
  37. TCREATEOPVALS tcreoptvals;
  38. DWORD dwScheduleType = 0;
  39. WORD wUserStatus = FALSE;
  40. //Initialize structures to neutral values.
  41. SecureZeroMemory( &tcresubops, sizeof( TCREATESUBOPTS ) );
  42. SecureZeroMemory( &tcreoptvals, sizeof( TCREATEOPVALS ) );
  43. // process the options for -create option
  44. if( ProcessCreateOptions ( argc, argv, tcresubops, tcreoptvals, &dwScheduleType, &wUserStatus ) )
  45. {
  46. ReleaseMemory(&tcresubops);
  47. if(tcresubops.bUsage == TRUE)
  48. {
  49. return S_OK;
  50. }
  51. else
  52. {
  53. return E_FAIL;
  54. }
  55. }
  56. // calls the function to create a scheduled task
  57. return CreateTask(tcresubops,tcreoptvals,dwScheduleType, wUserStatus );
  58. }
  59. /******************************************************************************
  60. Routine Description:
  61. This routine creates a new scheduled task according to the user
  62. specified format
  63. Arguments:
  64. [ in ] tcresubops : Structure containing the task's properties
  65. [ out ] tcreoptvals : Structure containing optional values to set
  66. [ in ] dwScheduleType : Type of schedule[Daily,once,weekly etc]
  67. [ in ] bUserStatus : bUserStatus will be TRUE when -ru given else FALSE
  68. Return Value :
  69. A HRESULT value indicating S_OK on success else S_FALSE on failure
  70. ******************************************************************************/
  71. HRESULT
  72. CreateTask(
  73. IN TCREATESUBOPTS tcresubops,
  74. IN OUT TCREATEOPVALS &tcreoptvals,
  75. IN DWORD dwScheduleType,
  76. IN WORD wUserStatus
  77. )
  78. {
  79. // Declarations related to the system time
  80. WORD wStartDay = 0;
  81. WORD wStartMonth = 0;
  82. WORD wStartYear = 0;
  83. WORD wStartHour = 0;
  84. WORD wStartMin = 0;
  85. WORD wEndHour = 0;
  86. WORD wEndMin = 0;
  87. WORD wEndDay = 0;
  88. WORD wEndYear = 0;
  89. WORD wEndMonth = 0;
  90. WORD wIdleTime = 0;
  91. WORD wCurrentHour = 0;
  92. WORD wCurrentMin = 0;
  93. DWORD dwCurrentTimeInMin = 0;
  94. WORD wCurrentYear = 0;
  95. WORD wCurrentMonth = 0;
  96. WORD wCurrentDay = 0;
  97. SYSTEMTIME systime = {0,0,0,0,0,0,0,0};
  98. // Declarations related to the new task
  99. LPWSTR wszUserName = NULL;
  100. LPWSTR wszPassword = NULL;
  101. WCHAR wszTaskName[MAX_JOB_LEN];
  102. WCHAR wszApplName[_MAX_FNAME];
  103. WCHAR szRPassword[MAX_STRING_LENGTH];
  104. HRESULT hr = S_OK;
  105. IPersistFile *pIPF = NULL;
  106. ITask *pITask = NULL;
  107. ITaskTrigger *pITaskTrig = NULL;
  108. ITaskScheduler *pITaskScheduler = NULL;
  109. WORD wTrigNumber = 0;
  110. TASK_TRIGGER TaskTrig;
  111. SecureZeroMemory(&TaskTrig, sizeof (TASK_TRIGGER));
  112. TaskTrig.cbTriggerSize = sizeof (TASK_TRIGGER);
  113. TaskTrig.Reserved1 = 0; // reserved field and must be set to 0.
  114. TaskTrig.Reserved2 = 0; // reserved field and must be set to 0.
  115. WCHAR* szValues[2] = {NULL};//To pass to FormatMessage() API
  116. // Buffer to store the string obtained from the string table
  117. WCHAR szBuffer[2 * MAX_STRING_LENGTH];
  118. BOOL bPassWord = FALSE;
  119. BOOL bUserName = FALSE;
  120. BOOL bRet = FALSE;
  121. BOOL bResult = FALSE;
  122. BOOL bCloseConnection = TRUE;
  123. ULONG ulLong = MAX_STRING_LENGTH;
  124. BOOL bVal = FALSE;
  125. DWORD dwStartTimeInMin = 0;
  126. DWORD dwEndTimeInMin = 0;
  127. DWORD dwDuration = 0;
  128. DWORD dwModifierVal = 0;
  129. DWORD dwRepeat = 0;
  130. BOOL bCancel = FALSE;
  131. BOOL bReplace = FALSE;
  132. BOOL bScOnce = FALSE;
  133. BOOL bStartDate = FALSE;
  134. LPWSTR pszStopString = NULL;
  135. DWORD dwPolicy = 0;
  136. //initialize the variables
  137. SecureZeroMemory (wszTaskName, SIZE_OF_ARRAY(wszTaskName));
  138. SecureZeroMemory (wszApplName, SIZE_OF_ARRAY(wszApplName));
  139. SecureZeroMemory (szRPassword, SIZE_OF_ARRAY(szRPassword));
  140. SecureZeroMemory (szBuffer, SIZE_OF_ARRAY(szBuffer));
  141. // check whether the taskname contains the characters such
  142. // as '<','>',':','/','\\','|'
  143. bRet = VerifyJobName(tcresubops.szTaskName);
  144. if(bRet == FALSE)
  145. {
  146. ShowMessage(stderr,GetResString(IDS_INVALID_TASKNAME1));
  147. ShowMessage(stderr,GetResString(IDS_INVALID_TASKNAME2));
  148. Cleanup(pITaskScheduler);
  149. ReleaseMemory(&tcresubops);
  150. return E_FAIL;
  151. }
  152. // check for the length of taskname
  153. if( ( StringLength(tcresubops.szTaskName, 0) > MAX_JOB_LEN ) )
  154. {
  155. ShowMessage(stderr,GetResString(IDS_INVALID_TASKLENGTH));
  156. Cleanup(pITaskScheduler);
  157. ReleaseMemory(&tcresubops);
  158. return E_FAIL;
  159. }
  160. // check for the length of taskrun
  161. if(( StringLength(tcresubops.szTaskRun, 0) > MAX_TASK_LEN ) )
  162. {
  163. ShowMessage(stderr,GetResString(IDS_INVALID_TASKRUN));
  164. Cleanup(pITaskScheduler);
  165. ReleaseMemory(&tcresubops);
  166. return E_FAIL;
  167. }
  168. StringCopy ( wszTaskName, tcresubops.szTaskName, SIZE_OF_ARRAY(wszTaskName));
  169. // check whether /IT is specified with /RU "NT AUTHORITY\SYSTEM" or not
  170. if ( ( ( TRUE == tcresubops.bActive) && ( wUserStatus == OI_CREATE_RUNASUSERNAME )) &&
  171. ( ( StringLength ( tcresubops.szRunAsUser, 0 ) == 0 ) ||
  172. ( StringCompare( tcresubops.szRunAsUser, NTAUTHORITY_USER, TRUE, 0 ) == 0 ) ||
  173. ( StringCompare( tcresubops.szRunAsUser, SYSTEM_USER, TRUE, 0 ) == 0 ) ) )
  174. {
  175. ShowMessage ( stderr, GetResString (IDS_IT_SWITCH_NA) );
  176. Cleanup(pITaskScheduler);
  177. ReleaseMemory(&tcresubops);
  178. return E_FAIL;
  179. }
  180. // check for empty password
  181. if( ( tcreoptvals.bRunAsPassword == TRUE ) && ( StringLength(tcresubops.szRunAsPassword, 0) == 0 ) &&
  182. ( StringLength ( tcresubops.szRunAsUser, 0 ) != 0 ) &&
  183. ( StringCompare(tcresubops.szRunAsUser, NTAUTHORITY_USER, TRUE, 0 ) != 0 ) &&
  184. ( StringCompare(tcresubops.szRunAsUser, SYSTEM_USER, TRUE, 0 ) != 0 ) )
  185. {
  186. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  187. }
  188. //Assign start time
  189. if(tcreoptvals.bSetStartTimeToCurTime && (dwScheduleType != SCHED_TYPE_ONIDLE) )
  190. {
  191. GetLocalTime(&systime);
  192. wStartHour = systime.wHour;
  193. wStartMin = systime.wMinute;
  194. }
  195. else if(StringLength(tcresubops.szStartTime, 0) > 0)
  196. {
  197. // get the Start time in terms of hours, minutes and seconds
  198. GetTimeFieldEntities(tcresubops.szStartTime, &wStartHour, &wStartMin );
  199. }
  200. // get the End time in terms of hours, minutes and seconds
  201. if(StringLength(tcresubops.szEndTime, 0) > 0)
  202. {
  203. GetTimeFieldEntities(tcresubops.szEndTime, &wEndHour, &wEndMin );
  204. }
  205. // default repetition interval-> 10 mins and duration ->60 mins
  206. dwRepeat = 10;
  207. dwDuration = 60;
  208. if(StringLength(tcresubops.szRepeat, 0) > 0)
  209. {
  210. // get the repetition value
  211. dwRepeat = wcstol(tcresubops.szRepeat, &pszStopString, BASE_TEN);
  212. if ((errno == ERANGE) ||
  213. ((pszStopString != NULL) && (StringLength (pszStopString, 0) != 0) ) ||
  214. ( (dwRepeat < MIN_REPETITION_INTERVAL ) || ( dwRepeat > MAX_REPETITION_INTERVAL) ) )
  215. {
  216. // display an error message as .. invalid value specified for /RT
  217. ShowMessage ( stderr, GetResString (IDS_INVALID_RT_VALUE) );
  218. ReleaseMemory(&tcresubops);
  219. return E_FAIL;
  220. }
  221. //check whether the specified repetition interval is greater than 9999..
  222. // if so, set the maximum repetition interval as 9999.
  223. if ( (dwRepeat > 9999) && ( (dwRepeat % 60) != 0) )
  224. {
  225. //display some warning message as.. max value (less than the specified interval)
  226. // divisible by 60.
  227. ShowMessage ( stderr, GetResString (IDS_WARN_VALUE) );
  228. dwRepeat -= (dwRepeat % 60);
  229. }
  230. }
  231. if ( ( dwScheduleType != SCHED_TYPE_ONSTART ) && ( dwScheduleType != SCHED_TYPE_ONLOGON ) && ( dwScheduleType != SCHED_TYPE_ONIDLE ))
  232. {
  233. if(( StringLength(tcresubops.szEndTime, 0) > 0) && ( StringLength(tcresubops.szDuration, 0) == 0) )
  234. {
  235. // calculate start time in minutes
  236. dwStartTimeInMin = (DWORD) ( wStartHour * MINUTES_PER_HOUR * SECS_PER_MINUTE + wStartMin * SECS_PER_MINUTE )/ SECS_PER_MINUTE ;
  237. // calculate end time in minutes
  238. dwEndTimeInMin = (DWORD) ( wEndHour * MINUTES_PER_HOUR * SECS_PER_MINUTE + wEndMin * SECS_PER_MINUTE ) / SECS_PER_MINUTE ;
  239. // check whether end time is later than start time
  240. if ( dwEndTimeInMin >= dwStartTimeInMin )
  241. {
  242. // if the end and start time in the same day..
  243. // get the duration between end and start time (in minutes)
  244. dwDuration = dwEndTimeInMin - dwStartTimeInMin ;
  245. }
  246. else
  247. {
  248. // if the start and end time not in the same day..
  249. // get the duration between start and end time (in minutes)
  250. // and subtract that duration by 1440(max value in minutes)..
  251. dwDuration = 1440 - (dwStartTimeInMin - dwEndTimeInMin ) ;
  252. }
  253. if ( dwScheduleType == SCHED_TYPE_MINUTE )
  254. {
  255. dwModifierVal = AsLong(tcresubops.szModifier, BASE_TEN) ;
  256. }
  257. else if (dwScheduleType == SCHED_TYPE_HOURLY)
  258. {
  259. dwModifierVal = AsLong(tcresubops.szModifier, BASE_TEN) * MINUTES_PER_HOUR;
  260. }
  261. //check whether The duration is greater than the repetition interval or not.
  262. if ( (dwDuration <= dwModifierVal) || ( ( dwScheduleType != SCHED_TYPE_MINUTE ) &&
  263. ( dwScheduleType != SCHED_TYPE_HOURLY ) && (dwDuration <= dwRepeat) ) )
  264. {
  265. ShowMessage ( stderr, GetResString (IDS_INVALID_DURATION1) );
  266. Cleanup(pITaskScheduler);
  267. ReleaseMemory(&tcresubops);
  268. return E_FAIL;
  269. }
  270. }
  271. else if(( StringLength(tcresubops.szEndTime, 0) == 0) && ( StringLength(tcresubops.szDuration, 0) > 0) )
  272. {
  273. WCHAR tHours[MAX_RES_STRING];
  274. WCHAR tMins[MAX_RES_STRING];
  275. DWORD dwDurationHours = 0;
  276. DWORD dwDurationMin = 0;
  277. //initializes the arrays
  278. SecureZeroMemory ( tHours, SIZE_OF_ARRAY(tHours));
  279. SecureZeroMemory ( tMins, SIZE_OF_ARRAY(tMins));
  280. if ( ( StringLength (tcresubops.szDuration, 0) != 7 ) || (tcresubops.szDuration[4] != TIME_SEPARATOR_CHAR) )
  281. {
  282. ShowMessage ( stderr, GetResString (IDS_INVALIDDURATION_FORMAT) );
  283. Cleanup(pITaskScheduler);
  284. ReleaseMemory(&tcresubops);
  285. return E_FAIL;
  286. }
  287. StringCopy(tHours, wcstok(tcresubops.szDuration,TIME_SEPARATOR_STR), SIZE_OF_ARRAY(tHours)); // Get the Hours field.
  288. if(StringLength(tHours, 0) > 0)
  289. {
  290. StringCopy(tMins, wcstok(NULL,TIME_SEPARATOR_STR), SIZE_OF_ARRAY(tMins)); // Get the Minutes field.
  291. }
  292. dwDurationHours = wcstol(tHours, &pszStopString, BASE_TEN);
  293. if ((errno == ERANGE) ||
  294. ((pszStopString != NULL) && (StringLength (pszStopString, 0) != 0) ) )
  295. {
  296. ShowMessage ( stderr, GetResString (IDS_INVALID_DU_VALUE) );
  297. Cleanup(pITaskScheduler);
  298. ReleaseMemory(&tcresubops);
  299. return E_FAIL;
  300. }
  301. // Get the duration in hours
  302. dwDurationHours = dwDurationHours * MINUTES_PER_HOUR;
  303. // Get the duration in minutes
  304. dwDurationMin = wcstol(tMins, &pszStopString, BASE_TEN);
  305. if ((errno == ERANGE) || ( dwDurationMin > 59 ) ||
  306. ((pszStopString != NULL) && (StringLength (pszStopString, 0) != 0) ) )
  307. {
  308. ShowMessage ( stderr, GetResString (IDS_INVALID_DU_VALUE) );
  309. Cleanup(pITaskScheduler);
  310. ReleaseMemory(&tcresubops);
  311. return E_FAIL;
  312. }
  313. // Get the total duration in minutes
  314. dwDuration = dwDurationHours + dwDurationMin ;
  315. if ( dwScheduleType == SCHED_TYPE_MINUTE )
  316. {
  317. dwModifierVal = AsLong(tcresubops.szModifier, BASE_TEN) ;
  318. }
  319. else if (dwScheduleType == SCHED_TYPE_HOURLY)
  320. {
  321. dwModifierVal = AsLong(tcresubops.szModifier, BASE_TEN) * MINUTES_PER_HOUR;
  322. }
  323. //check whether The duration is greater than the repetition interval or not.
  324. if ( dwDuration <= dwModifierVal || ( ( dwScheduleType != SCHED_TYPE_MINUTE ) &&
  325. ( dwScheduleType != SCHED_TYPE_HOURLY ) && (dwDuration <= dwRepeat) ) )
  326. {
  327. ShowMessage ( stderr, GetResString (IDS_INVALID_DURATION2) );
  328. Cleanup(pITaskScheduler);
  329. ReleaseMemory(&tcresubops);
  330. return E_FAIL;
  331. }
  332. }
  333. }
  334. // check whether the group policy prevented user from creating new tasks or not.
  335. if ( FALSE == GetGroupPolicy( tcresubops.szServer, tcresubops.szUser, TS_KEYPOLICY_DENY_CREATE_TASK, &dwPolicy ) )
  336. {
  337. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  338. Cleanup(pITaskScheduler);
  339. ReleaseMemory(&tcresubops);
  340. return EXIT_FAILURE;
  341. }
  342. if ( dwPolicy > 0 )
  343. {
  344. ShowMessage ( stdout, GetResString (IDS_PREVENT_CREATE));
  345. Cleanup(pITaskScheduler);
  346. ReleaseMemory(&tcresubops);
  347. return EXIT_SUCCESS;
  348. }
  349. // check for the local system
  350. if ( ( IsLocalSystem( tcresubops.szServer ) == TRUE ) &&
  351. ( StringLength ( tcresubops.szRunAsUser, 0 ) != 0 ) &&
  352. ( StringCompare(tcresubops.szRunAsUser, NTAUTHORITY_USER, TRUE, 0 ) != 0 ) &&
  353. ( StringCompare(tcresubops.szRunAsUser, SYSTEM_USER, TRUE, 0 ) != 0 ) )
  354. {
  355. // Establish the connection on a remote machine
  356. bResult = EstablishConnection(tcresubops.szServer,tcresubops.szUser,GetBufferSize(tcresubops.szUser)/sizeof(WCHAR),tcresubops.szPassword,GetBufferSize(tcresubops.szPassword)/sizeof(WCHAR), tcreoptvals.bPassword);
  357. if (bResult == FALSE)
  358. {
  359. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  360. Cleanup(pITaskScheduler);
  361. ReleaseMemory(&tcresubops);
  362. return E_FAIL ;
  363. }
  364. else
  365. {
  366. // though the connection is successfull, some conflict might have occured
  367. switch( GetLastError() )
  368. {
  369. case I_NO_CLOSE_CONNECTION:
  370. bCloseConnection = FALSE;
  371. break;
  372. // check for mismatched credentials
  373. case E_LOCAL_CREDENTIALS:
  374. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  375. {
  376. bCloseConnection = FALSE;
  377. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  378. Cleanup(pITaskScheduler);
  379. ReleaseMemory(&tcresubops);
  380. return E_FAIL;
  381. }
  382. default :
  383. bCloseConnection = TRUE;
  384. }
  385. }
  386. if ( StringLength (tcresubops.szRunAsUser, 0) != 0 )
  387. {
  388. wszUserName = tcresubops.szRunAsUser;
  389. bUserName = TRUE;
  390. if ( tcreoptvals.bRunAsPassword == FALSE )
  391. {
  392. szValues[0] = (WCHAR*) (wszUserName);
  393. //Display that the task will be created under logged in user name,ask for password
  394. MessageBeep(MB_ICONEXCLAMATION);
  395. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_PROMPT_PASSWD), _X(wszUserName));
  396. // getting the password
  397. if (GetPassword(szRPassword, MAX_STRING_LENGTH) == FALSE )
  398. {
  399. Cleanup(pITaskScheduler);
  400. // close the connection that was established by the utility
  401. if ( bCloseConnection == TRUE )
  402. {
  403. CloseConnection( tcresubops.szServer );
  404. }
  405. ReleaseMemory(&tcresubops);
  406. return E_FAIL;
  407. }
  408. // check for empty password
  409. if( StringLength ( szRPassword, 0 ) == 0 )
  410. {
  411. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  412. }
  413. wszPassword = szRPassword;
  414. bPassWord = TRUE;
  415. }
  416. else
  417. {
  418. wszPassword = tcresubops.szRunAsPassword;
  419. bPassWord = TRUE;
  420. }
  421. }
  422. }
  423. // check whether -s option only specified in the cmd line or not
  424. else if( ( IsLocalSystem( tcresubops.szServer ) == FALSE ) && ( wUserStatus == OI_CREATE_SERVER ) )
  425. {
  426. // Establish the connection on a remote machine
  427. bResult = EstablishConnection(tcresubops.szServer,tcresubops.szUser,GetBufferSize(tcresubops.szUser)/sizeof(WCHAR),tcresubops.szPassword,GetBufferSize(tcresubops.szPassword)/sizeof(WCHAR), tcreoptvals.bPassword);
  428. if (bResult == FALSE)
  429. {
  430. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  431. Cleanup(pITaskScheduler);
  432. ReleaseMemory(&tcresubops);
  433. return E_FAIL ;
  434. }
  435. else
  436. {
  437. // though the connection is successfull, some conflict might have occured
  438. switch( GetLastError() )
  439. {
  440. case I_NO_CLOSE_CONNECTION:
  441. bCloseConnection = FALSE;
  442. break;
  443. // check for mismatched credentials
  444. case E_LOCAL_CREDENTIALS:
  445. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  446. {
  447. bCloseConnection = FALSE;
  448. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  449. Cleanup(pITaskScheduler);
  450. ReleaseMemory(&tcresubops);
  451. return E_FAIL;
  452. }
  453. default :
  454. bCloseConnection = TRUE;
  455. }
  456. }
  457. if ( ( StringLength (tcresubops.szUser, 0) == 0 ) )
  458. {
  459. //get the current logged on username
  460. if ( GetUserNameEx ( NameSamCompatible, tcresubops.szUser , &ulLong) == FALSE )
  461. {
  462. ShowMessage( stderr, GetResString( IDS_LOGGED_USER_ERR ) );
  463. Cleanup(pITaskScheduler);
  464. // close the connection that was established by the utility
  465. if ( bCloseConnection == TRUE )
  466. {
  467. CloseConnection( tcresubops.szServer );
  468. }
  469. ReleaseMemory(&tcresubops);
  470. return E_FAIL;
  471. }
  472. bUserName = TRUE;
  473. wszUserName = tcresubops.szUser;
  474. szValues[0] = (WCHAR*) (wszUserName);
  475. //Display that the task will be created under logged in user name,ask for password
  476. MessageBeep(MB_ICONEXCLAMATION);
  477. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_TASK_INFO), _X(wszUserName));
  478. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_PROMPT_PASSWD), _X(wszUserName));
  479. if (GetPassword(tcresubops.szRunAsPassword, GetBufferSize(tcresubops.szRunAsPassword)/sizeof(WCHAR)) == FALSE )
  480. {
  481. Cleanup(pITaskScheduler);
  482. // close the connection that was established by the utility
  483. if ( bCloseConnection == TRUE )
  484. {
  485. CloseConnection( tcresubops.szServer );
  486. }
  487. ReleaseMemory(&tcresubops);
  488. return E_FAIL;
  489. }
  490. // check for empty password
  491. if( StringLength ( tcresubops.szRunAsPassword, 0 ) == 0 )
  492. {
  493. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  494. }
  495. // check for the length of the password
  496. wszPassword = tcresubops.szRunAsPassword;
  497. bPassWord = TRUE;
  498. }
  499. wszUserName = tcresubops.szUser;
  500. // check whether the run as password is specified in the cmdline or not
  501. if ( tcreoptvals.bRunAsPassword == TRUE )
  502. {
  503. // check for -rp "*" or -rp " " to prompt for password
  504. if ( StringCompare( tcresubops.szRunAsPassword, ASTERIX, TRUE, 0 ) == 0 )
  505. {
  506. // format the message for getting the password
  507. szValues[0] = (WCHAR*) (wszUserName);
  508. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_PROMPT_PASSWD), _X(wszUserName));
  509. //re-alloc the memory for /rp
  510. if ( ReallocateMemory( (LPVOID*)&tcresubops.szRunAsPassword,
  511. MAX_STRING_LENGTH * sizeof( WCHAR ) ) == FALSE )
  512. {
  513. SaveLastError();
  514. // close the connection that was established by the utility
  515. if ( bCloseConnection == TRUE )
  516. {
  517. CloseConnection( tcresubops.szServer );
  518. }
  519. return E_FAIL;
  520. }
  521. // Get the run as password from the command line
  522. if ( GetPassword(tcresubops.szRunAsPassword, GetBufferSize(tcresubops.szRunAsPassword)/sizeof(WCHAR) ) == FALSE )
  523. {
  524. Cleanup(pITaskScheduler);
  525. // close the connection that was established by the utility
  526. if ( bCloseConnection == TRUE )
  527. {
  528. CloseConnection( tcresubops.szServer );
  529. }
  530. ReleaseMemory(&tcresubops);
  531. return E_FAIL;
  532. }
  533. // check for empty password
  534. if( StringLength ( tcresubops.szRunAsPassword, 0 ) == 0 )
  535. {
  536. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  537. }
  538. wszPassword = tcresubops.szRunAsPassword;
  539. }
  540. }
  541. else
  542. {
  543. wszPassword = tcresubops.szPassword;
  544. }
  545. // set the BOOL variables to TRUE
  546. bUserName = TRUE;
  547. bPassWord = TRUE;
  548. }
  549. // check for -s and -u options only specified in the cmd line or not
  550. else if ( wUserStatus == OI_CREATE_USERNAME )
  551. {
  552. // Establish the connection on a remote machine
  553. bResult = EstablishConnection(tcresubops.szServer,tcresubops.szUser,GetBufferSize(tcresubops.szUser)/sizeof(WCHAR),tcresubops.szPassword,GetBufferSize(tcresubops.szPassword)/sizeof(WCHAR), tcreoptvals.bPassword);
  554. if (bResult == FALSE)
  555. {
  556. ShowMessage( stderr, GetResString(IDS_ERROR_STRING) );
  557. ShowMessage( stderr, GetReason());
  558. Cleanup(pITaskScheduler);
  559. ReleaseMemory(&tcresubops);
  560. return E_FAIL ;
  561. }
  562. else
  563. {
  564. // though the connection is successfull, some conflict might have occured
  565. switch( GetLastError() )
  566. {
  567. case I_NO_CLOSE_CONNECTION:
  568. bCloseConnection = FALSE;
  569. break;
  570. // for mismatched credentials
  571. case E_LOCAL_CREDENTIALS:
  572. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  573. {
  574. bCloseConnection = FALSE;
  575. ShowMessage( stderr, GetResString(IDS_ERROR_STRING) );
  576. ShowMessage( stderr, GetReason());
  577. Cleanup(pITaskScheduler);
  578. ReleaseMemory(&tcresubops);
  579. return E_FAIL;
  580. }
  581. default :
  582. bCloseConnection = TRUE;
  583. }
  584. }
  585. wszUserName = tcresubops.szUser;
  586. // check whether run as password is specified in the command line or not
  587. if ( tcreoptvals.bRunAsPassword == TRUE )
  588. {
  589. // check for -rp "*" or -rp " " to prompt for password
  590. if ( StringCompare( tcresubops.szRunAsPassword, ASTERIX, TRUE, 0 ) == 0 )
  591. {
  592. // format the message for getting the password from console
  593. szValues[0] = (WCHAR*) (wszUserName);
  594. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_PROMPT_PASSWD), _X(wszUserName));
  595. //re-alloc the memory for /rp
  596. if ( ReallocateMemory( (LPVOID*)&tcresubops.szRunAsPassword,
  597. MAX_STRING_LENGTH * sizeof( WCHAR ) ) == FALSE )
  598. {
  599. SaveLastError();
  600. if ( bCloseConnection == TRUE )
  601. CloseConnection( tcresubops.szServer );
  602. //release memory for password
  603. ReleaseMemory(&tcresubops);
  604. return E_FAIL;
  605. }
  606. // Get the password from the command line
  607. if ( GetPassword(tcresubops.szRunAsPassword, GetBufferSize(tcresubops.szRunAsPassword)/sizeof(WCHAR) ) == FALSE )
  608. {
  609. Cleanup(pITaskScheduler);
  610. // close the connection that was established by the utility
  611. if ( bCloseConnection == TRUE )
  612. {
  613. CloseConnection( tcresubops.szServer );
  614. }
  615. ReleaseMemory(&tcresubops);
  616. return E_FAIL;
  617. }
  618. // check for empty password
  619. if( StringLength ( tcresubops.szRunAsPassword, 0 ) == 0 )
  620. {
  621. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  622. }
  623. wszPassword = tcresubops.szRunAsPassword;
  624. }
  625. else
  626. {
  627. wszPassword = tcresubops.szRunAsPassword;
  628. bPassWord = TRUE;
  629. }
  630. }
  631. else
  632. {
  633. if ( StringLength(tcresubops.szPassword, 0) != 0 )
  634. {
  635. wszPassword = tcresubops.szPassword;
  636. }
  637. else
  638. {
  639. // format the message for getting the password from console
  640. szValues[0] = (WCHAR*) (wszUserName);
  641. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_PROMPT_PASSWD), _X(wszUserName));
  642. // Get the password from the command line
  643. if ( GetPassword(tcresubops.szRunAsPassword, GetBufferSize(tcresubops.szRunAsPassword)/sizeof(WCHAR) ) == FALSE )
  644. {
  645. Cleanup(pITaskScheduler);
  646. // close the connection that was established by the utility
  647. if ( bCloseConnection == TRUE )
  648. {
  649. CloseConnection( tcresubops.szServer );
  650. }
  651. ReleaseMemory(&tcresubops);
  652. return E_FAIL;
  653. }
  654. // check for empty password
  655. if( StringLength ( tcresubops.szRunAsPassword, 0 ) == 0 )
  656. {
  657. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  658. }
  659. wszPassword = tcresubops.szRunAsPassword;
  660. }
  661. }
  662. bUserName = TRUE;
  663. bPassWord = TRUE;
  664. }
  665. // check for -s, -ru or -u options specified in the cmd line or not
  666. else if ( ( StringLength (tcresubops.szServer, 0) != 0 ) && ( wUserStatus == OI_CREATE_RUNASUSERNAME || wUserStatus == OI_RUNANDUSER ) )
  667. {
  668. // Establish the connection on a remote machine
  669. bResult = EstablishConnection(tcresubops.szServer,tcresubops.szUser,GetBufferSize(tcresubops.szUser)/sizeof(WCHAR),tcresubops.szPassword,GetBufferSize(tcresubops.szPassword)/sizeof(WCHAR), tcreoptvals.bPassword);
  670. if (bResult == FALSE)
  671. {
  672. ShowMessage( stderr, GetResString(IDS_ERROR_STRING) );
  673. ShowMessage( stderr, GetReason());
  674. Cleanup(pITaskScheduler);
  675. ReleaseMemory(&tcresubops);
  676. return E_FAIL ;
  677. }
  678. else
  679. {
  680. // though the connection is successfull, some conflict might have occured
  681. switch( GetLastError() )
  682. {
  683. case I_NO_CLOSE_CONNECTION:
  684. bCloseConnection = FALSE;
  685. break;
  686. case E_LOCAL_CREDENTIALS:
  687. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  688. {
  689. bCloseConnection = FALSE;
  690. ShowMessage( stderr, GetResString(IDS_ERROR_STRING) );
  691. ShowMessage( stderr, GetReason());
  692. Cleanup(pITaskScheduler);
  693. ReleaseMemory(&tcresubops);
  694. return E_FAIL;
  695. }
  696. default :
  697. bCloseConnection = TRUE;
  698. }
  699. }
  700. if ( ( ( StringLength ( tcresubops.szRunAsUser, 0 ) == 0 ) ||
  701. ( StringCompare( tcresubops.szRunAsUser, NTAUTHORITY_USER, TRUE, 0 ) == 0 ) ||
  702. ( StringCompare( tcresubops.szRunAsUser, SYSTEM_USER, TRUE, 0 ) == 0 ) ) )
  703. {
  704. wszUserName = tcresubops.szRunAsUser;
  705. szValues[0] = (WCHAR*) (tcresubops.szTaskName);
  706. //ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_NTAUTH_SYSTEM_INFO), _X(wszTaskName));
  707. StringCchPrintf ( szBuffer, SIZE_OF_ARRAY(szBuffer), GetResString(IDS_NTAUTH_SYSTEM_INFO), _X(wszTaskName));
  708. ShowMessage ( stdout, _X(szBuffer));
  709. if ( ( tcreoptvals.bRunAsPassword == TRUE ) &&
  710. ( StringLength (tcresubops.szRunAsPassword, 0) != 0 ) )
  711. {
  712. ShowMessage( stderr, GetResString( IDS_PASSWORD_NOEFFECT ) );
  713. }
  714. bUserName = TRUE;
  715. bPassWord = TRUE;
  716. bVal = TRUE;
  717. }
  718. else
  719. {
  720. // check for the length of password
  721. if ( StringLength ( tcresubops.szRunAsUser, 0 ) != 0 )
  722. {
  723. wszUserName = tcresubops.szRunAsUser;
  724. bUserName = TRUE;
  725. }
  726. }
  727. // check whether -u and -ru are the same or not. if they are same, we need to
  728. // prompt for the run as password. otherwise, will consoder -rp as -p
  729. if ( StringCompare( tcresubops.szRunAsUser, tcresubops.szUser, TRUE, 0 ) != 0)
  730. {
  731. if ( tcreoptvals.bRunAsPassword == TRUE )
  732. {
  733. if ( (StringLength(tcresubops.szRunAsUser, 0) != 0) && (StringCompare( tcresubops.szRunAsPassword, ASTERIX, TRUE, 0 ) == 0 ) &&
  734. ( StringCompare( tcresubops.szRunAsUser, NTAUTHORITY_USER, TRUE, 0 ) != 0 ) &&
  735. ( StringCompare( tcresubops.szRunAsUser, SYSTEM_USER, TRUE, 0 ) != 0 ) )
  736. {
  737. szValues[0] = (WCHAR*) (wszUserName);
  738. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_PROMPT_PASSWD), _X(wszUserName));
  739. // prompt for the run as password
  740. if ( GetPassword(szRPassword, MAX_STRING_LENGTH ) == FALSE )
  741. {
  742. Cleanup(pITaskScheduler);
  743. // close the connection that was established by the utility
  744. if ( bCloseConnection == TRUE )
  745. {
  746. CloseConnection( tcresubops.szServer );
  747. }
  748. ReleaseMemory(&tcresubops);
  749. return E_FAIL;
  750. }
  751. // check for empty password
  752. if( StringLength ( szRPassword, 0 ) == 0 )
  753. {
  754. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  755. }
  756. wszPassword = szRPassword;
  757. bUserName = TRUE;
  758. bPassWord = TRUE;
  759. }
  760. else
  761. {
  762. wszPassword = tcresubops.szRunAsPassword;
  763. bUserName = TRUE;
  764. bPassWord = TRUE;
  765. }
  766. }
  767. else
  768. {
  769. // check for the length of password
  770. if ( ( bVal == FALSE ) && ( StringLength(tcresubops.szRunAsUser, 0) != 0) )
  771. {
  772. // format the message for getting the password from console
  773. szValues[0] = (WCHAR*) (wszUserName);
  774. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_PROMPT_PASSWD), _X(wszUserName));
  775. // prompt for the run as password
  776. if ( GetPassword(szRPassword, MAX_STRING_LENGTH ) == FALSE )
  777. {
  778. Cleanup(pITaskScheduler);
  779. // close the connection that was established by the utility
  780. if ( bCloseConnection == TRUE )
  781. {
  782. CloseConnection( tcresubops.szServer );
  783. }
  784. ReleaseMemory(&tcresubops);
  785. return E_FAIL;
  786. }
  787. // check for empty password
  788. if( StringLength ( szRPassword, 0 ) == 0 )
  789. {
  790. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  791. }
  792. wszPassword = szRPassword;
  793. }
  794. bUserName = TRUE;
  795. bPassWord = TRUE;
  796. }
  797. }
  798. else
  799. {
  800. // check whether run as password is specified in the cmdline or not
  801. if ( tcreoptvals.bRunAsPassword == TRUE )
  802. {
  803. if ( ( StringLength ( tcresubops.szRunAsUser, 0 ) != 0 ) && ( StringCompare( tcresubops.szRunAsPassword, ASTERIX, TRUE, 0 ) == 0 ) )
  804. {
  805. szValues[0] = (WCHAR*) (wszUserName);
  806. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_PROMPT_PASSWD), _X(wszUserName));
  807. // prompt for the run as password
  808. if ( GetPassword(szRPassword, MAX_STRING_LENGTH ) == FALSE )
  809. {
  810. Cleanup(pITaskScheduler);
  811. // close the connection that was established by the utility
  812. if ( bCloseConnection == TRUE )
  813. {
  814. CloseConnection( tcresubops.szServer );
  815. }
  816. ReleaseMemory(&tcresubops);
  817. return E_FAIL;
  818. }
  819. // check for empty password
  820. if( StringLength ( szRPassword, 0 ) == 0 )
  821. {
  822. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  823. }
  824. wszPassword = szRPassword;
  825. }
  826. else
  827. {
  828. wszPassword = tcresubops.szRunAsPassword;
  829. }
  830. }
  831. else
  832. {
  833. if ( StringLength (tcresubops.szPassword, 0) )
  834. {
  835. wszPassword = tcresubops.szPassword;
  836. }
  837. else
  838. {
  839. if (( StringLength ( tcresubops.szRunAsUser, 0 ) != 0 ) &&
  840. ( StringCompare(tcresubops.szRunAsUser, NTAUTHORITY_USER, TRUE, 0 ) != 0 ) &&
  841. ( StringCompare(tcresubops.szRunAsUser, SYSTEM_USER, TRUE, 0 ) != 0 ) )
  842. {
  843. szValues[0] = (WCHAR*) (wszUserName);
  844. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_PROMPT_PASSWD), _X(wszUserName));
  845. // prompt for the run as password
  846. if ( GetPassword(szRPassword, MAX_STRING_LENGTH ) == FALSE )
  847. {
  848. Cleanup(pITaskScheduler);
  849. // close the connection that was established by the utility
  850. if ( bCloseConnection == TRUE )
  851. {
  852. CloseConnection( tcresubops.szServer );
  853. }
  854. ReleaseMemory(&tcresubops);
  855. return E_FAIL;
  856. }
  857. // check for empty password
  858. if( StringLength ( szRPassword, 0 ) == 0 )
  859. {
  860. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  861. }
  862. wszPassword = szRPassword;
  863. }
  864. }
  865. }
  866. bUserName = TRUE;
  867. bPassWord = TRUE;
  868. }
  869. }
  870. // To check for the -ru values "", "NT AUTHORITY\SYSTEM", "SYSTEM"
  871. if( ( ( bVal == FALSE ) && ( wUserStatus == OI_CREATE_RUNASUSERNAME ) && ( StringLength( tcresubops.szRunAsUser, 0) == 0 ) && ( tcreoptvals.bRunAsPassword == FALSE ) ) ||
  872. ( ( bVal == FALSE ) && ( wUserStatus == OI_CREATE_RUNASUSERNAME ) && ( StringLength( tcresubops.szRunAsUser, 0) == 0 ) && ( StringLength(tcresubops.szRunAsPassword, 0 ) == 0 ) ) ||
  873. ( ( bVal == FALSE ) && ( wUserStatus == OI_CREATE_RUNASUSERNAME ) && ( StringCompare(tcresubops.szRunAsUser, NTAUTHORITY_USER, TRUE, 0 ) == 0 ) && ( tcreoptvals.bRunAsPassword == FALSE ) ) ||
  874. ( ( bVal == FALSE ) && ( wUserStatus == OI_CREATE_RUNASUSERNAME ) && ( StringCompare(tcresubops.szRunAsUser, NTAUTHORITY_USER, TRUE, 0 ) == 0 ) && ( StringLength( tcresubops.szRunAsPassword, 0) == 0 ) ) ||
  875. ( ( bVal == FALSE ) && ( wUserStatus == OI_CREATE_RUNASUSERNAME ) && ( StringCompare(tcresubops.szRunAsUser, SYSTEM_USER, TRUE, 0 ) == 0 ) && ( tcreoptvals.bRunAsPassword == FALSE ) ) ||
  876. ( ( bVal == FALSE ) && ( wUserStatus == OI_CREATE_RUNASUSERNAME ) && ( StringCompare(tcresubops.szRunAsUser, SYSTEM_USER, TRUE, 0 ) == 0 ) && ( StringLength(tcresubops.szRunAsPassword, 0) == 0 ) ) )
  877. {
  878. //format the message to display the taskname will be created under "NT AUTHORITY\SYSTEM"
  879. szValues[0] = (WCHAR*) (tcresubops.szTaskName);
  880. //ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_NTAUTH_SYSTEM_INFO), _X(wszTaskName));
  881. StringCchPrintf ( szBuffer, SIZE_OF_ARRAY(szBuffer), GetResString(IDS_NTAUTH_SYSTEM_INFO), _X(wszTaskName));
  882. ShowMessage ( stdout, _X(szBuffer));
  883. bUserName = TRUE;
  884. bPassWord = TRUE;
  885. bVal = TRUE;
  886. }
  887. // check whether the -rp value is given with the -ru "", "NT AUTHORITY\SYSTEM",
  888. // "SYSTEM" or not
  889. else if( ( ( bVal == FALSE ) && ( wUserStatus == OI_CREATE_RUNASUSERNAME || wUserStatus == OI_RUNANDUSER) && ( StringLength(tcresubops.szRunAsUser, 0) == 0 ) && ( StringLength(tcresubops.szRunAsPassword, 0) != 0 ) ) ||
  890. ( ( bVal == FALSE ) && ( wUserStatus == OI_CREATE_RUNASUSERNAME || wUserStatus == OI_RUNANDUSER) && ( StringCompare( tcresubops.szRunAsUser, NTAUTHORITY_USER, TRUE, 0 ) == 0 ) && ( tcreoptvals.bRunAsPassword == TRUE ) ) ||
  891. ( ( bVal == FALSE ) && ( wUserStatus == OI_CREATE_RUNASUSERNAME || wUserStatus == OI_RUNANDUSER) && ( StringCompare( tcresubops.szRunAsUser, SYSTEM_USER, TRUE, 0 ) == 0 ) && ( tcreoptvals.bRunAsPassword == TRUE ) ) )
  892. {
  893. szValues[0] = (WCHAR*) (tcresubops.szTaskName);
  894. //ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_NTAUTH_SYSTEM_INFO), _X(wszTaskName));
  895. StringCchPrintf ( szBuffer, SIZE_OF_ARRAY(szBuffer), GetResString(IDS_NTAUTH_SYSTEM_INFO), _X(wszTaskName));
  896. ShowMessage ( stdout, _X(szBuffer));
  897. // to display a warning message as password will not effect for the system account
  898. ShowMessage( stderr, GetResString( IDS_PASSWORD_NOEFFECT ) );
  899. bUserName = TRUE;
  900. bPassWord = TRUE;
  901. bVal = TRUE;
  902. }
  903. // check whether -s, -u, -ru options are given in the cmdline or not
  904. else if( ( wUserStatus != OI_CREATE_SERVER ) && ( wUserStatus != OI_CREATE_USERNAME ) &&
  905. ( wUserStatus != OI_CREATE_RUNASUSERNAME ) && ( wUserStatus != OI_RUNANDUSER ) &&
  906. ( StringCompare( tcresubops.szRunAsPassword , L"\0", TRUE, 0 ) == 0 ) )
  907. {
  908. if (tcreoptvals.bRunAsPassword == TRUE)
  909. {
  910. bPassWord = TRUE;
  911. }
  912. else
  913. {
  914. bPassWord = FALSE;
  915. }
  916. }
  917. else if ( ( StringLength(tcresubops.szServer, 0) == 0 ) && (StringLength ( tcresubops.szRunAsUser, 0 ) != 0 ) )
  918. {
  919. wszUserName = tcresubops.szRunAsUser;
  920. bUserName = TRUE;
  921. if ( StringLength ( tcresubops.szRunAsPassword, 0 ) == 0 )
  922. {
  923. bPassWord = TRUE;
  924. }
  925. else
  926. {
  927. // check whether "*" or NULL value is given for -rp or not
  928. if ( StringCompare ( tcresubops.szRunAsPassword , ASTERIX, TRUE, 0 ) == 0 )
  929. {
  930. // format a message for getting the password from the console
  931. szValues[0] = (WCHAR*) (wszUserName);
  932. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_PROMPT_PASSWD), _X(wszUserName));
  933. // Get the password from the command line
  934. if (GetPassword(szRPassword, MAX_STRING_LENGTH ) == FALSE )
  935. {
  936. Cleanup(pITaskScheduler);
  937. ReleaseMemory(&tcresubops);
  938. return E_FAIL;
  939. }
  940. // check for empty password
  941. if( StringLength ( szRPassword, 0 ) == 0 )
  942. {
  943. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  944. }
  945. wszPassword = szRPassword;
  946. }
  947. else
  948. {
  949. wszPassword = tcresubops.szRunAsPassword;
  950. }
  951. bPassWord = TRUE;
  952. }
  953. }
  954. // check whether -ru or -u values are specified in the cmdline or not
  955. if ( wUserStatus == OI_CREATE_RUNASUSERNAME || wUserStatus == OI_RUNANDUSER )
  956. {
  957. if( ( bUserName == TRUE ) && ( bPassWord == FALSE ) )
  958. {
  959. szValues[0] = (WCHAR*) (wszUserName);
  960. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_PROMPT_PASSWD), _X(wszUserName));
  961. // getting the password from the console
  962. if ( GetPassword(tcresubops.szRunAsPassword, GetBufferSize(tcresubops.szRunAsPassword)/sizeof(WCHAR) ) == FALSE )
  963. {
  964. Cleanup(pITaskScheduler);
  965. ReleaseMemory(&tcresubops);
  966. return E_FAIL;
  967. }
  968. // check for empty password
  969. if( StringLength ( tcresubops.szRunAsPassword, 0 ) == 0 )
  970. {
  971. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  972. }
  973. wszPassword = tcresubops.szRunAsPassword;
  974. }
  975. }
  976. //if the user name is not specifed set the current logged on user settings
  977. WCHAR szUserName[MAX_STRING_LENGTH];
  978. DWORD dwCheck = 0;
  979. if( ( bUserName == FALSE ) )
  980. {
  981. //get the current logged on username
  982. if ( GetUserNameEx ( NameSamCompatible, szUserName , &ulLong) == FALSE )
  983. {
  984. ShowMessage( stderr, GetResString( IDS_LOGGED_USER_ERR ) );
  985. Cleanup(pITaskScheduler);
  986. ReleaseMemory(&tcresubops);
  987. return E_FAIL;
  988. }
  989. wszUserName = szUserName;
  990. szValues[0] = (WCHAR*) (wszUserName);
  991. //Display that the task will be created under logged in user name,ask for password
  992. MessageBeep(MB_ICONEXCLAMATION);
  993. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_TASK_INFO), _X(wszUserName));
  994. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_PROMPT_PASSWD), _X(wszUserName));
  995. // getting the password
  996. if (GetPassword(szRPassword, MAX_STRING_LENGTH) == FALSE )
  997. {
  998. Cleanup(pITaskScheduler);
  999. ReleaseMemory(&tcresubops);
  1000. return E_FAIL;
  1001. }
  1002. // check for empty password
  1003. if( StringLength ( szRPassword, 0 ) == 0 )
  1004. {
  1005. ShowMessage(stderr, GetResString(IDS_WARN_EMPTY_PASSWORD));
  1006. }
  1007. wszPassword = szRPassword;
  1008. }
  1009. // Get the task Scheduler object for the machine.
  1010. pITaskScheduler = GetTaskScheduler( tcresubops.szServer );
  1011. // If the Task Scheduler is not defined then give the error message.
  1012. if ( pITaskScheduler == NULL )
  1013. {
  1014. // close the connection that was established by the utility
  1015. if ( bCloseConnection == TRUE )
  1016. CloseConnection( tcresubops.szServer );
  1017. Cleanup(pITaskScheduler);
  1018. ReleaseMemory(&tcresubops);
  1019. return E_FAIL;
  1020. }
  1021. //check whether service is running or not
  1022. if ((FALSE == CheckServiceStatus ( tcresubops.szServer , &dwCheck, TRUE)) && (0 != dwCheck) && ( GetLastError () != ERROR_ACCESS_DENIED))
  1023. {
  1024. // close the connection that was established by the utility
  1025. if ( bCloseConnection == TRUE )
  1026. CloseConnection( tcresubops.szServer );
  1027. Cleanup(pITaskScheduler);
  1028. ReleaseMemory(&tcresubops);
  1029. if ( 1 == dwCheck )
  1030. {
  1031. ShowMessage ( stderr, GetResString (IDS_NOT_START_SERVICE));
  1032. return EXIT_FAILURE;
  1033. }
  1034. else if (2 == dwCheck )
  1035. {
  1036. return E_FAIL;
  1037. }
  1038. else if (3 == dwCheck )
  1039. {
  1040. return EXIT_SUCCESS;
  1041. }
  1042. }
  1043. StringConcat ( tcresubops.szTaskName, JOB, SIZE_OF_ARRAY(tcresubops.szTaskName) );
  1044. // create a work item tcresubops.szTaskName
  1045. hr = pITaskScheduler->NewWorkItem(tcresubops.szTaskName,CLSID_CTask,IID_ITask,
  1046. (IUnknown**)&pITask);
  1047. // check whether the specified scheduled task is created under
  1048. // some other user. If so, display an error message as unable to create a
  1049. // specified taskname as it is already exists.
  1050. // If the taskname created under some other user return value
  1051. // of above API must 0x80070005.
  1052. if( hr == 0x80070005 )
  1053. {
  1054. ShowMessage(stderr,GetResString(IDS_SYSTEM_TASK_EXISTS));
  1055. // close the connection that was established by the utility
  1056. if ( bCloseConnection == TRUE )
  1057. CloseConnection( tcresubops.szServer );
  1058. Cleanup(pITaskScheduler);
  1059. ReleaseMemory(&tcresubops);
  1060. return hr;
  1061. }
  1062. // check whether task is exists in the system or not.
  1063. if( hr == HRESULT_FROM_WIN32 (ERROR_FILE_EXISTS))
  1064. {
  1065. // flag to specify .. need to replace exisitng task..
  1066. bReplace = TRUE;
  1067. szValues[0] = (WCHAR*) (tcresubops.szTaskName);
  1068. // check whether /F option is specified or not..
  1069. // if /F option is specified .. then suppress the warning message..
  1070. if ( FALSE == tcresubops.bForce )
  1071. {
  1072. StringCchPrintf ( szBuffer, SIZE_OF_ARRAY(szBuffer), GetResString(IDS_CREATE_TASK_EXISTS), _X(wszTaskName));
  1073. ShowMessage ( stdout, _X(szBuffer));
  1074. if ( EXIT_FAILURE == ConfirmInput(&bCancel))
  1075. {
  1076. if ( bCloseConnection == TRUE )
  1077. CloseConnection( tcresubops.szServer );
  1078. Cleanup(pITaskScheduler);
  1079. ReleaseMemory(&tcresubops);
  1080. // invalid input entered.. return failure..
  1081. return E_FAIL;
  1082. }
  1083. if ( TRUE == bCancel )
  1084. {
  1085. if ( bCloseConnection == TRUE )
  1086. CloseConnection( tcresubops.szServer );
  1087. Cleanup(pITaskScheduler);
  1088. ReleaseMemory(&tcresubops);
  1089. // operation cancelled.. return with success..
  1090. return EXIT_SUCCESS;
  1091. }
  1092. }
  1093. //reset to NULL
  1094. pITask = NULL;
  1095. //StringConcat ( tcresubops.szTaskName, JOB, SIZE_OF_ARRAY(tcresubops.szTaskName) );
  1096. // Gets an active interface for a specified szTaskName
  1097. hr = pITaskScheduler->Activate(tcresubops.szTaskName,IID_ITask,
  1098. (IUnknown**) &pITask);
  1099. //check whether the job file os corrupted or not..
  1100. if ( (hr == 0x8007000D) || (hr == SCHED_E_UNKNOWN_OBJECT_VERSION) || (hr == E_INVALIDARG))
  1101. {
  1102. //set the variable to FALSE..
  1103. bReplace = FALSE;
  1104. //Since the job file is corrupted.. delete a work item
  1105. hr = pITaskScheduler->Delete(tcresubops.szTaskName);
  1106. if ( FAILED(hr))
  1107. {
  1108. SetLastError ((DWORD) hr);
  1109. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1110. // close the connection that was established by the utility
  1111. if ( bCloseConnection == TRUE )
  1112. CloseConnection( tcresubops.szServer );
  1113. Cleanup(pITaskScheduler);
  1114. ReleaseMemory(&tcresubops);
  1115. return hr;
  1116. }
  1117. // create a work item tcresubops.szTaskName
  1118. hr = pITaskScheduler->NewWorkItem(tcresubops.szTaskName,CLSID_CTask,IID_ITask,
  1119. (IUnknown**)&pITask);
  1120. }
  1121. // check for failure..
  1122. if ( FAILED(hr))
  1123. {
  1124. SetLastError ((DWORD) hr);
  1125. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1126. // close the connection that was established by the utility
  1127. if ( bCloseConnection == TRUE )
  1128. CloseConnection( tcresubops.szServer );
  1129. Cleanup(pITaskScheduler);
  1130. ReleaseMemory(&tcresubops);
  1131. return hr;
  1132. }
  1133. }
  1134. else if (FAILED(hr))
  1135. {
  1136. SetLastError ((DWORD) hr);
  1137. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1138. // close the connection that was established by the utility
  1139. if ( bCloseConnection == TRUE )
  1140. CloseConnection( tcresubops.szServer );
  1141. Cleanup(pITaskScheduler);
  1142. ReleaseMemory(&tcresubops);
  1143. return hr;
  1144. }
  1145. // Return a pointer to a specified interface on an object
  1146. hr = pITask->QueryInterface(IID_IPersistFile, (void **) &pIPF);
  1147. if (FAILED(hr))
  1148. {
  1149. SetLastError ((DWORD) hr);
  1150. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1151. if( pIPF )
  1152. {
  1153. pIPF->Release();
  1154. }
  1155. if( pITask )
  1156. {
  1157. pITask->Release();
  1158. }
  1159. // close the connection that was established by the utility
  1160. if ( bCloseConnection == TRUE )
  1161. CloseConnection( tcresubops.szServer );
  1162. Cleanup(pITaskScheduler);
  1163. ReleaseMemory(&tcresubops);
  1164. return hr;
  1165. }
  1166. // declaration for parameter arguments
  1167. wchar_t wcszParam[MAX_RES_STRING] = L"\0";
  1168. DWORD dwProcessCode = 0 ;
  1169. dwProcessCode = ProcessFilePath(tcresubops.szTaskRun,wszApplName,wcszParam);
  1170. if(dwProcessCode == RETVAL_FAIL)
  1171. {
  1172. if( pIPF )
  1173. {
  1174. pIPF->Release();
  1175. }
  1176. if( pITask )
  1177. {
  1178. pITask->Release();
  1179. }
  1180. // close the connection that was established by the utility
  1181. if ( bCloseConnection == TRUE )
  1182. CloseConnection( tcresubops.szServer );
  1183. Cleanup(pITaskScheduler);
  1184. ReleaseMemory(&tcresubops);
  1185. return hr;
  1186. }
  1187. // check for .exe substring string in the given task to run string
  1188. // Set command name with ITask::SetApplicationName
  1189. hr = pITask->SetApplicationName(wszApplName);
  1190. if (FAILED(hr))
  1191. {
  1192. SetLastError ((DWORD) hr);
  1193. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1194. if( pIPF )
  1195. {
  1196. pIPF->Release();
  1197. }
  1198. if( pITask )
  1199. {
  1200. pITask->Release();
  1201. }
  1202. // close the connection that was established by the utility
  1203. if ( bCloseConnection == TRUE )
  1204. CloseConnection( tcresubops.szServer );
  1205. Cleanup(pITaskScheduler);
  1206. ReleaseMemory(&tcresubops);
  1207. return hr;
  1208. }
  1209. //[Working directory = exe pathname - exe name]
  1210. wchar_t* wcszStartIn = wcsrchr(wszApplName,_T('\\'));
  1211. if(wcszStartIn != NULL)
  1212. *( wcszStartIn ) = _T('\0');
  1213. // set the command working directory
  1214. hr = pITask->SetWorkingDirectory(wszApplName);
  1215. if (FAILED(hr))
  1216. {
  1217. SetLastError ((DWORD) hr);
  1218. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1219. if( pIPF )
  1220. {
  1221. pIPF->Release();
  1222. }
  1223. if( pITask )
  1224. {
  1225. pITask->Release();
  1226. }
  1227. // close the connection that was established by the utility
  1228. if ( bCloseConnection == TRUE )
  1229. CloseConnection( tcresubops.szServer );
  1230. Cleanup(pITaskScheduler);
  1231. ReleaseMemory(&tcresubops);
  1232. return hr;
  1233. }
  1234. // set the command line parameters for the task
  1235. hr = pITask->SetParameters(wcszParam);
  1236. if (FAILED(hr))
  1237. {
  1238. SetLastError ((DWORD) hr);
  1239. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1240. if( pIPF )
  1241. {
  1242. pIPF->Release();
  1243. }
  1244. if( pITask )
  1245. {
  1246. pITask->Release();
  1247. }
  1248. // close the connection that was established by the utility
  1249. if ( bCloseConnection == TRUE )
  1250. CloseConnection( tcresubops.szServer );
  1251. Cleanup(pITaskScheduler);
  1252. ReleaseMemory(&tcresubops);
  1253. return hr;
  1254. }
  1255. // sub-variable declaration
  1256. DWORD dwTaskFlags = 0;
  1257. // set flag to run the task interactively
  1258. if ( ( FALSE == bVal ) && ( TRUE == tcresubops.bActive) )
  1259. {
  1260. dwTaskFlags = TASK_FLAG_RUN_ONLY_IF_LOGGED_ON | TASK_FLAG_DONT_START_IF_ON_BATTERIES | TASK_FLAG_KILL_IF_GOING_ON_BATTERIES ;
  1261. }
  1262. else
  1263. {
  1264. dwTaskFlags = TASK_FLAG_DONT_START_IF_ON_BATTERIES | TASK_FLAG_KILL_IF_GOING_ON_BATTERIES;
  1265. }
  1266. // if /z is specified .. enables the falg to delete the task if not scheduled to
  1267. // run again...
  1268. if ( TRUE == tcresubops.bIsDeleteNoSched )
  1269. {
  1270. dwTaskFlags |= TASK_FLAG_DELETE_WHEN_DONE;
  1271. }
  1272. hr = pITask->SetFlags(dwTaskFlags);
  1273. if (FAILED(hr))
  1274. {
  1275. SetLastError ((DWORD) hr);
  1276. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1277. if( pIPF )
  1278. {
  1279. pIPF->Release();
  1280. }
  1281. if( pITask )
  1282. {
  1283. pITask->Release();
  1284. }
  1285. // close the connection that was established by the utility
  1286. if ( bCloseConnection == TRUE )
  1287. CloseConnection( tcresubops.szServer );
  1288. Cleanup(pITaskScheduler);
  1289. ReleaseMemory(&tcresubops);
  1290. return hr;
  1291. }
  1292. if ( bVal == TRUE )
  1293. {
  1294. // Set account information for "NT AUTHORITY\SYSTEM" user
  1295. hr = pITask->SetAccountInformation(L"",NULL);
  1296. }
  1297. else
  1298. {
  1299. // set the account information with the user name and password
  1300. hr = pITask->SetAccountInformation(wszUserName,wszPassword);
  1301. }
  1302. if ((FAILED(hr)) && (hr != SCHED_E_NO_SECURITY_SERVICES))
  1303. {
  1304. SetLastError ((DWORD) hr);
  1305. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1306. ShowMessage ( stdout, _T("\n") );
  1307. ShowMessage ( stdout, GetResString( IDS_ACCNAME_ERR ) );
  1308. if( pIPF )
  1309. {
  1310. pIPF->Release();
  1311. }
  1312. if( pITask )
  1313. {
  1314. pITask->Release();
  1315. }
  1316. // close the connection that was established by the utility
  1317. if ( bCloseConnection == TRUE )
  1318. CloseConnection( tcresubops.szServer );
  1319. Cleanup(pITaskScheduler);
  1320. ReleaseMemory(&tcresubops);
  1321. return hr;
  1322. }
  1323. //Assign start date
  1324. if(tcreoptvals.bSetStartDateToCurDate )
  1325. {
  1326. GetLocalTime(&systime);
  1327. wStartDay = systime.wDay;
  1328. wStartMonth = systime.wMonth;
  1329. wStartYear = systime.wYear;
  1330. }
  1331. else if(StringLength(tcresubops.szStartDate, 0) > 0)
  1332. {
  1333. GetDateFieldEntities(tcresubops.szStartDate, &wStartDay, &wStartMonth, &wStartYear);
  1334. }
  1335. //Set the flags specific to ONIDLE
  1336. if(dwScheduleType == SCHED_TYPE_ONIDLE)
  1337. {
  1338. pITask->SetFlags(TASK_FLAG_START_ONLY_IF_IDLE);
  1339. wIdleTime = (WORD)AsLong(tcresubops.szIdleTime, BASE_TEN);
  1340. pITask->SetIdleWait(wIdleTime, 0);
  1341. }
  1342. //if specified task already exists... we need to replace the task..
  1343. if ( TRUE == bReplace )
  1344. {
  1345. //create trigger for the corresponding task
  1346. hr = pITask->GetTrigger(wTrigNumber, &pITaskTrig);
  1347. if (FAILED(hr))
  1348. {
  1349. SetLastError ((DWORD) hr);
  1350. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1351. if( pIPF )
  1352. {
  1353. pIPF->Release();
  1354. }
  1355. if( pITaskTrig )
  1356. {
  1357. pITaskTrig->Release();
  1358. }
  1359. if( pITask )
  1360. {
  1361. pITask->Release();
  1362. }
  1363. // close the connection that was established by the utility
  1364. if ( bCloseConnection == TRUE )
  1365. CloseConnection( tcresubops.szServer );
  1366. Cleanup(pITaskScheduler);
  1367. ReleaseMemory(&tcresubops);
  1368. return hr;
  1369. }
  1370. // get the current logged-on user name
  1371. WCHAR wszLogonUser [MAX_STRING_LENGTH + 20] = L"";
  1372. DWORD dwLogonUserLen = SIZE_OF_ARRAY(wszLogonUser);
  1373. if ( FALSE == GetUserName (wszLogonUser, &dwLogonUserLen) )
  1374. {
  1375. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1376. if( pIPF )
  1377. {
  1378. pIPF->Release();
  1379. }
  1380. if( pITaskTrig )
  1381. {
  1382. pITaskTrig->Release();
  1383. }
  1384. if( pITask )
  1385. {
  1386. pITask->Release();
  1387. }
  1388. // close the connection that was established by the utility
  1389. if ( bCloseConnection == TRUE )
  1390. CloseConnection( tcresubops.szServer );
  1391. Cleanup(pITaskScheduler);
  1392. ReleaseMemory(&tcresubops);
  1393. return hr;
  1394. }
  1395. //set the creator name i.e logged-on user name
  1396. hr = pITask->SetCreator(wszLogonUser);
  1397. if (FAILED(hr))
  1398. {
  1399. SetLastError ((DWORD) hr);
  1400. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1401. if( pIPF )
  1402. {
  1403. pIPF->Release();
  1404. }
  1405. if( pITaskTrig )
  1406. {
  1407. pITaskTrig->Release();
  1408. }
  1409. if( pITask )
  1410. {
  1411. pITask->Release();
  1412. }
  1413. // close the connection that was established by the utility
  1414. if ( bCloseConnection == TRUE )
  1415. CloseConnection( tcresubops.szServer );
  1416. Cleanup(pITaskScheduler);
  1417. ReleaseMemory(&tcresubops);
  1418. return hr;
  1419. }
  1420. }
  1421. else
  1422. {
  1423. //create trigger for the corresponding task
  1424. hr = pITask->CreateTrigger(&wTrigNumber, &pITaskTrig);
  1425. if (FAILED(hr))
  1426. {
  1427. SetLastError ((DWORD) hr);
  1428. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  1429. if( pIPF )
  1430. {
  1431. pIPF->Release();
  1432. }
  1433. if( pITaskTrig )
  1434. {
  1435. pITaskTrig->Release();
  1436. }
  1437. if( pITask )
  1438. {
  1439. pITask->Release();
  1440. }
  1441. // close the connection that was established by the utility
  1442. if ( bCloseConnection == TRUE )
  1443. CloseConnection( tcresubops.szServer );
  1444. Cleanup(pITaskScheduler);
  1445. ReleaseMemory(&tcresubops);
  1446. return hr;
  1447. }
  1448. }
  1449. WORD wWhichWeek = 0;
  1450. LONG lMonthlyModifier = 0;
  1451. DWORD dwDays = 1;
  1452. //check whether /K is specified or not
  1453. if ( TRUE == tcresubops.bIsDurEnd )
  1454. {
  1455. // set the flag to terminate the task at the end of lifetime.
  1456. TaskTrig.rgFlags = TASK_TRIGGER_FLAG_KILL_AT_DURATION_END ;
  1457. }
  1458. if( ( StringLength(tcresubops.szEndTime, 0) == 0) && (StringLength(tcresubops.szDuration, 0) == 0) &&
  1459. (StringLength(tcresubops.szRepeat, 0) == 0))
  1460. {
  1461. TaskTrig.MinutesInterval = 0;
  1462. TaskTrig.MinutesDuration = 0;
  1463. }
  1464. else
  1465. {
  1466. // if repetition interval is not 0.. then set actual value of /RI
  1467. if ( 0 != dwRepeat )
  1468. {
  1469. // set the MinutesInterval
  1470. TaskTrig.MinutesInterval = dwRepeat;
  1471. }
  1472. // if duration is not 0.. set the actual value of /DU
  1473. if ( 0 != dwDuration )
  1474. {
  1475. // set the duration value
  1476. TaskTrig.MinutesDuration = dwDuration ;
  1477. }
  1478. }
  1479. //check whether The duration is greater than the repetition interval or not.
  1480. if ( ( dwScheduleType != SCHED_TYPE_MINUTE ) &&
  1481. ( dwScheduleType != SCHED_TYPE_HOURLY ) && (dwDuration <= dwRepeat) )
  1482. {
  1483. ShowMessage ( stderr, GetResString (IDS_INVALID_DURATION2) );
  1484. if( pIPF )
  1485. {
  1486. pIPF->Release();
  1487. }
  1488. if( pITaskTrig )
  1489. {
  1490. pITaskTrig->Release();
  1491. }
  1492. if( pITask )
  1493. {
  1494. pITask->Release();
  1495. }
  1496. // close the connection that was established by the utility
  1497. if ( bCloseConnection == TRUE )
  1498. CloseConnection( tcresubops.szServer );
  1499. Cleanup(pITaskScheduler);
  1500. ReleaseMemory(&tcresubops);
  1501. return EXIT_FAILURE;
  1502. }
  1503. switch( dwScheduleType )
  1504. {
  1505. case SCHED_TYPE_MINUTE:
  1506. TaskTrig.TriggerType = TASK_TIME_TRIGGER_DAILY;
  1507. TaskTrig.Type.Daily.DaysInterval = 1;
  1508. if (StringLength ( tcresubops.szModifier, 0 ) > 0)
  1509. {
  1510. TaskTrig.MinutesInterval = AsLong(tcresubops.szModifier, BASE_TEN);
  1511. }
  1512. if(( StringLength(tcresubops.szEndTime, 0) > 0) || (StringLength(tcresubops.szDuration, 0) > 0) )
  1513. {
  1514. // calculate start time in minutes
  1515. TaskTrig.MinutesDuration = dwDuration ;
  1516. }
  1517. else
  1518. {
  1519. TaskTrig.MinutesDuration = (WORD)(HOURS_PER_DAY*MINUTES_PER_HOUR);
  1520. }
  1521. TaskTrig.wStartHour = wStartHour;
  1522. TaskTrig.wStartMinute = wStartMin;
  1523. TaskTrig.wBeginDay = wStartDay;
  1524. TaskTrig.wBeginMonth = wStartMonth;
  1525. TaskTrig.wBeginYear = wStartYear;
  1526. if(StringLength(tcresubops.szEndDate, 0) > 0)
  1527. {
  1528. // Make end date valid; otherwise the enddate parameter is ignored.
  1529. TaskTrig.rgFlags |= TASK_TRIGGER_FLAG_HAS_END_DATE;
  1530. // Now set the end date entities.
  1531. GetDateFieldEntities(tcresubops.szEndDate, &wEndDay, &wEndMonth, &wEndYear);
  1532. TaskTrig.wEndDay = wEndDay;
  1533. TaskTrig.wEndMonth = wEndMonth;
  1534. TaskTrig.wEndYear = wEndYear;
  1535. }
  1536. break;
  1537. case SCHED_TYPE_HOURLY:
  1538. TaskTrig.TriggerType = TASK_TIME_TRIGGER_DAILY;
  1539. TaskTrig.Type.Daily.DaysInterval = 1;
  1540. if (StringLength ( tcresubops.szModifier, 0 ) > 0)
  1541. {
  1542. //set the MinutesInterval
  1543. TaskTrig.MinutesInterval = (AsLong(tcresubops.szModifier, BASE_TEN)
  1544. * MINUTES_PER_HOUR);
  1545. }
  1546. if ( (StringLength(tcresubops.szEndTime, 0) > 0) || (StringLength(tcresubops.szDuration, 0) > 0) )
  1547. {
  1548. //set the duration value
  1549. TaskTrig.MinutesDuration = dwDuration ;
  1550. }
  1551. else
  1552. {
  1553. TaskTrig.MinutesDuration = (WORD)(HOURS_PER_DAY*MINUTES_PER_HOUR);
  1554. }
  1555. TaskTrig.wStartHour = wStartHour;
  1556. TaskTrig.wStartMinute = wStartMin;
  1557. TaskTrig.wBeginDay = wStartDay;
  1558. TaskTrig.wBeginMonth = wStartMonth;
  1559. TaskTrig.wBeginYear = wStartYear;
  1560. // Now set end date parameters, if the enddate is specified.
  1561. if(StringLength(tcresubops.szEndDate, 0) > 0)
  1562. {
  1563. // Make end date valid; otherwise the enddate parameter is ignored.
  1564. TaskTrig.rgFlags |= TASK_TRIGGER_FLAG_HAS_END_DATE;
  1565. // Now set the end date entities.
  1566. GetDateFieldEntities(tcresubops.szEndDate, &wEndDay, &wEndMonth, &wEndYear);
  1567. TaskTrig.wEndDay = wEndDay;
  1568. TaskTrig.wEndMonth = wEndMonth;
  1569. TaskTrig.wEndYear = wEndYear;
  1570. }
  1571. break;
  1572. // Schedule type is Daily
  1573. case SCHED_TYPE_DAILY:
  1574. TaskTrig.TriggerType = TASK_TIME_TRIGGER_DAILY;
  1575. TaskTrig.wBeginDay = wStartDay;
  1576. TaskTrig.wBeginMonth = wStartMonth;
  1577. TaskTrig.wBeginYear = wStartYear;
  1578. TaskTrig.wStartHour = wStartHour;
  1579. TaskTrig.wStartMinute = wStartMin;
  1580. if( StringLength(tcresubops.szModifier, 0) > 0 )
  1581. {
  1582. // Set the duration between days to the modifier value specified, if the modifier is specified.
  1583. TaskTrig.Type.Daily.DaysInterval = (WORD) AsLong(tcresubops.szModifier,
  1584. BASE_TEN);
  1585. }
  1586. else
  1587. {
  1588. // Set value for on which day of the week?
  1589. TaskTrig.Type.Weekly.rgfDaysOfTheWeek = GetTaskTrigwDayForDay(tcresubops.szDays);
  1590. TaskTrig.Type.Weekly.WeeksInterval = 1;
  1591. }
  1592. // Now set end date parameters, if the enddate is specified.
  1593. if(StringLength(tcresubops.szEndDate, 0) > 0)
  1594. {
  1595. // Make end date valid; otherwise the enddate parameter is ignored.
  1596. TaskTrig.rgFlags |= TASK_TRIGGER_FLAG_HAS_END_DATE;
  1597. // Now set the end date entities.
  1598. GetDateFieldEntities(tcresubops.szEndDate, &wEndDay, &wEndMonth, &wEndYear);
  1599. TaskTrig.wEndDay = wEndDay;
  1600. TaskTrig.wEndMonth = wEndMonth;
  1601. TaskTrig.wEndYear = wEndYear;
  1602. }
  1603. // No more settings for a Daily type scheduled item.
  1604. break;
  1605. // Schedule type is Weekly
  1606. case SCHED_TYPE_WEEKLY:
  1607. TaskTrig.TriggerType = TASK_TIME_TRIGGER_WEEKLY;
  1608. TaskTrig.Type.Weekly.WeeksInterval = (WORD)AsLong(tcresubops.szModifier, BASE_TEN);
  1609. // Set value for on which day of the week?
  1610. TaskTrig.Type.Weekly.rgfDaysOfTheWeek = GetTaskTrigwDayForDay(tcresubops.szDays);
  1611. TaskTrig.wStartHour = wStartHour;
  1612. TaskTrig.wStartMinute = wStartMin;
  1613. TaskTrig.wBeginDay = wStartDay;
  1614. TaskTrig.wBeginMonth = wStartMonth;
  1615. TaskTrig.wBeginYear = wStartYear;
  1616. // Now set end date parameters, if the enddate is specified.
  1617. if(StringLength(tcresubops.szEndDate, 0) > 0)
  1618. {
  1619. // Make end date valid; otherwise the enddate parameter is ignored.
  1620. TaskTrig.rgFlags |= TASK_TRIGGER_FLAG_HAS_END_DATE;
  1621. // Now set the end date entities.
  1622. GetDateFieldEntities(tcresubops.szEndDate, &wEndDay, &wEndMonth, &wEndYear);
  1623. TaskTrig.wEndDay = wEndDay;
  1624. TaskTrig.wEndMonth = wEndMonth;
  1625. TaskTrig.wEndYear = wEndYear;
  1626. }
  1627. break;
  1628. // Schedule type is Monthly
  1629. case SCHED_TYPE_MONTHLY:
  1630. TaskTrig.wStartHour = wStartHour;
  1631. TaskTrig.wStartMinute = wStartMin;
  1632. TaskTrig.wBeginDay = wStartDay;
  1633. TaskTrig.wBeginMonth = wStartMonth;
  1634. TaskTrig.wBeginYear = wStartYear;
  1635. // Now set end date parameters, if the enddate is specified.
  1636. if(StringLength(tcresubops.szEndDate, 0) > 0)
  1637. {
  1638. // Make end date valid; otherwise the enddate parameter is ignored.
  1639. TaskTrig.rgFlags |= TASK_TRIGGER_FLAG_HAS_END_DATE;
  1640. // Set the end date entities.
  1641. GetDateFieldEntities(tcresubops.szEndDate, &wEndDay, &wEndMonth, &wEndYear);
  1642. TaskTrig.wEndDay = wEndDay;
  1643. TaskTrig.wEndMonth = wEndMonth;
  1644. TaskTrig.wEndYear = wEndYear;
  1645. }
  1646. //Find out from modifier which option like 1 - 12 days
  1647. //or FIRST,SECOND ,THIRD ,.... LAST.
  1648. if(StringLength(tcresubops.szModifier, 0) > 0)
  1649. {
  1650. lMonthlyModifier = AsLong(tcresubops.szModifier, BASE_TEN);
  1651. if(lMonthlyModifier >= 1 && lMonthlyModifier <= 12)
  1652. {
  1653. if(StringLength(tcresubops.szDays, 0) == 0 )
  1654. {
  1655. dwDays = 1;//default value for days
  1656. }
  1657. else
  1658. {
  1659. dwDays = (WORD)AsLong(tcresubops.szDays, BASE_TEN);
  1660. }
  1661. TaskTrig.TriggerType = TASK_TIME_TRIGGER_MONTHLYDATE;
  1662. //set the appropriate day bit in rgfDays
  1663. TaskTrig.Type.MonthlyDate.rgfDays = (1 << (dwDays -1)) ;
  1664. TaskTrig.Type.MonthlyDate.rgfMonths = GetMonthId(lMonthlyModifier);
  1665. }
  1666. else
  1667. {
  1668. if( StringCompare( tcresubops.szModifier , GetResString( IDS_DAY_MODIFIER_LASTDAY ), TRUE, 0 ) == 0)
  1669. {
  1670. TaskTrig.TriggerType = TASK_TIME_TRIGGER_MONTHLYDATE;
  1671. //set the appropriate day bit in rgfDays
  1672. TaskTrig.Type.MonthlyDate.rgfDays =
  1673. (1 << (GetNumDaysInaMonth(tcresubops.szMonths, wStartYear ) -1));
  1674. TaskTrig.Type.MonthlyDate.rgfMonths = GetTaskTrigwMonthForMonth(
  1675. tcresubops.szMonths);
  1676. break;
  1677. }
  1678. if( StringCompare(tcresubops.szModifier,
  1679. GetResString( IDS_TASK_FIRSTWEEK ), TRUE, 0 ) == 0 )
  1680. {
  1681. wWhichWeek = TASK_FIRST_WEEK;
  1682. }
  1683. else if( StringCompare(tcresubops.szModifier,
  1684. GetResString( IDS_TASK_SECONDWEEK ), TRUE, 0) == 0 )
  1685. {
  1686. wWhichWeek = TASK_SECOND_WEEK;
  1687. }
  1688. else if( StringCompare(tcresubops.szModifier,
  1689. GetResString( IDS_TASK_THIRDWEEK ), TRUE, 0) == 0 )
  1690. {
  1691. wWhichWeek = TASK_THIRD_WEEK;
  1692. }
  1693. else if( StringCompare(tcresubops.szModifier,
  1694. GetResString( IDS_TASK_FOURTHWEEK ), TRUE, 0) == 0 )
  1695. {
  1696. wWhichWeek = TASK_FOURTH_WEEK;
  1697. }
  1698. else if( StringCompare(tcresubops.szModifier,
  1699. GetResString( IDS_TASK_LASTWEEK ), TRUE, 0) == 0 )
  1700. {
  1701. wWhichWeek = TASK_LAST_WEEK;
  1702. }
  1703. TaskTrig.TriggerType = TASK_TIME_TRIGGER_MONTHLYDOW;
  1704. TaskTrig.Type.MonthlyDOW.wWhichWeek = wWhichWeek;
  1705. TaskTrig.Type.MonthlyDOW.rgfDaysOfTheWeek = GetTaskTrigwDayForDay(
  1706. tcresubops.szDays);
  1707. TaskTrig.Type.MonthlyDOW.rgfMonths = GetTaskTrigwMonthForMonth(
  1708. tcresubops.szMonths);
  1709. }
  1710. }
  1711. else
  1712. {
  1713. TaskTrig.TriggerType = TASK_TIME_TRIGGER_MONTHLYDATE;
  1714. TaskTrig.Type.MonthlyDate.rgfMonths = GetTaskTrigwMonthForMonth(
  1715. tcresubops.szMonths);
  1716. dwDays = (WORD)AsLong(tcresubops.szDays, BASE_TEN);
  1717. if(dwDays > 1)
  1718. {
  1719. //set the appropriate day bit in rgfDays
  1720. TaskTrig.Type.MonthlyDate.rgfDays = (1 << (dwDays -1));
  1721. }
  1722. else
  1723. {
  1724. TaskTrig.Type.MonthlyDate.rgfDays = 1;
  1725. }
  1726. }
  1727. break;
  1728. // Schedule type is Onetime
  1729. case SCHED_TYPE_ONETIME:
  1730. //
  1731. //display a WARNING message if start time is earlier than current time
  1732. //
  1733. //get current time
  1734. GetLocalTime(&systime);
  1735. wCurrentHour = systime.wHour;
  1736. wCurrentMin = systime.wMinute;
  1737. wCurrentYear = systime.wYear;
  1738. wCurrentMonth = systime.wMonth;
  1739. wCurrentDay = systime.wDay;
  1740. if( (FALSE == tcreoptvals.bSetStartDateToCurDate) )
  1741. {
  1742. if( ( wCurrentYear == wStartYear ) )
  1743. {
  1744. // For same years if the end month is less than start month or for same years and same months
  1745. // if the endday is less than the startday.
  1746. if ( ( wStartMonth < wCurrentMonth ) || ( ( wCurrentMonth == wStartMonth ) && ( wStartDay < wCurrentDay ) ) )
  1747. {
  1748. bScOnce = TRUE;
  1749. }
  1750. else if ( ( wStartMonth > wCurrentMonth ) || ( ( wCurrentMonth == wStartMonth ) && ( wStartDay > wCurrentDay ) ) )
  1751. {
  1752. bStartDate = TRUE;
  1753. }
  1754. }
  1755. else if ( wStartYear < wCurrentYear )
  1756. {
  1757. bScOnce = TRUE;
  1758. }
  1759. else
  1760. {
  1761. bStartDate = TRUE;
  1762. }
  1763. }
  1764. // calculate current time in minutes
  1765. // calculate start time in minutes
  1766. dwCurrentTimeInMin = (DWORD) ( wCurrentHour * MINUTES_PER_HOUR * SECS_PER_MINUTE + wCurrentMin * SECS_PER_MINUTE )/ SECS_PER_MINUTE ;
  1767. // calculate start time in minutes
  1768. dwStartTimeInMin = (DWORD) ( wStartHour * MINUTES_PER_HOUR * SECS_PER_MINUTE + wStartMin * SECS_PER_MINUTE )/ SECS_PER_MINUTE ;
  1769. if ( (FALSE == bStartDate ) && ((dwStartTimeInMin < dwCurrentTimeInMin) || (TRUE == bScOnce) ))
  1770. {
  1771. ShowMessage ( stderr, GetResString (IDS_WARN_ST_LESS_CT) );
  1772. }
  1773. TaskTrig.TriggerType = TASK_TIME_TRIGGER_ONCE;
  1774. TaskTrig.wStartHour = wStartHour;
  1775. TaskTrig.wStartMinute = wStartMin;
  1776. TaskTrig.wBeginDay = wStartDay;
  1777. TaskTrig.wBeginMonth = wStartMonth;
  1778. TaskTrig.wBeginYear = wStartYear;
  1779. break;
  1780. // Schedule type is Onlogon
  1781. case SCHED_TYPE_ONSTART:
  1782. case SCHED_TYPE_ONLOGON:
  1783. if(dwScheduleType == SCHED_TYPE_ONLOGON )
  1784. TaskTrig.TriggerType = TASK_EVENT_TRIGGER_AT_LOGON;
  1785. if(dwScheduleType == SCHED_TYPE_ONSTART )
  1786. TaskTrig.TriggerType = TASK_EVENT_TRIGGER_AT_SYSTEMSTART;
  1787. TaskTrig.wBeginDay = wStartDay;
  1788. TaskTrig.wBeginMonth = wStartMonth;
  1789. TaskTrig.wBeginYear = wStartYear;
  1790. break;
  1791. // Schedule type is Onidle
  1792. case SCHED_TYPE_ONIDLE:
  1793. TaskTrig.TriggerType = TASK_EVENT_TRIGGER_ON_IDLE;
  1794. TaskTrig.wBeginDay = wStartDay;
  1795. TaskTrig.wBeginMonth = wStartMonth;
  1796. TaskTrig.wBeginYear = wStartYear;
  1797. break;
  1798. default:
  1799. // close the connection that was established by the utility
  1800. if ( bCloseConnection == TRUE )
  1801. CloseConnection( tcresubops.szServer );
  1802. Cleanup(pITaskScheduler);
  1803. ReleaseMemory(&tcresubops);
  1804. return E_FAIL;
  1805. }
  1806. if(tcresubops.szTaskName != NULL)
  1807. {
  1808. //remove the .job extension from the task name
  1809. if (ParseTaskName(tcresubops.szTaskName))
  1810. {
  1811. if( pIPF )
  1812. {
  1813. pIPF->Release();
  1814. }
  1815. if( pITaskTrig )
  1816. {
  1817. pITaskTrig->Release();
  1818. }
  1819. if( pITask )
  1820. {
  1821. pITask->Release();
  1822. }
  1823. // close the connection that was established by the utility
  1824. if ( bCloseConnection == TRUE )
  1825. CloseConnection( tcresubops.szServer );
  1826. Cleanup(pITaskScheduler);
  1827. ReleaseMemory(&tcresubops);
  1828. return E_FAIL;
  1829. }
  1830. }
  1831. szValues[0] = (WCHAR*) (tcresubops.szTaskName);
  1832. // set the task trigger
  1833. hr = pITaskTrig->SetTrigger(&TaskTrig);
  1834. if (hr != S_OK)
  1835. {
  1836. ShowMessageEx ( stderr, 1, FALSE, GetResString(IDS_CREATEFAIL_INVALIDARGS), _X(tcresubops.szTaskName));
  1837. if( pIPF )
  1838. {
  1839. pIPF->Release();
  1840. }
  1841. if( pITaskTrig )
  1842. {
  1843. pITaskTrig->Release();
  1844. }
  1845. if( pITask )
  1846. {
  1847. pITask->Release();
  1848. }
  1849. // close the connection that was established by the utility
  1850. if ( bCloseConnection == TRUE )
  1851. CloseConnection( tcresubops.szServer );
  1852. Cleanup(pITaskScheduler);
  1853. ReleaseMemory(&tcresubops);
  1854. return hr;
  1855. }
  1856. // save the copy of an object
  1857. hr = pIPF->Save(NULL,TRUE);
  1858. if( FAILED(hr) )
  1859. {
  1860. szValues[0] = (WCHAR*) (tcresubops.szTaskName);
  1861. if ( hr == SCHEDULER_NOT_RUNNING_ERROR_CODE )
  1862. {
  1863. StringCchPrintf ( szBuffer, SIZE_OF_ARRAY(szBuffer), GetResString(IDS_SCHEDULER_NOT_RUNNING), _X(tcresubops.szTaskName));
  1864. ShowMessage ( stderr, _X(szBuffer));
  1865. }
  1866. else if ( hr == RPC_SERVER_NOT_AVAILABLE )
  1867. {
  1868. szValues[1] = (WCHAR*) (tcresubops.szServer);
  1869. StringCchPrintf ( szBuffer, SIZE_OF_ARRAY(szBuffer), GetResString(IDS_RPC_SERVER_NOT_AVAIL), _X(tcresubops.szTaskName), _X(tcresubops.szServer));
  1870. ShowMessage ( stderr, _X(szBuffer));
  1871. }
  1872. else
  1873. {
  1874. StringCchPrintf ( szBuffer, SIZE_OF_ARRAY(szBuffer), GetResString(IDS_INVALID_USER), _X(tcresubops.szTaskName));
  1875. ShowMessage ( stderr, _X(szBuffer));
  1876. }
  1877. if(pIPF)
  1878. {
  1879. pIPF->Release();
  1880. }
  1881. if(pITaskTrig)
  1882. {
  1883. pITaskTrig->Release();
  1884. }
  1885. if(pITask)
  1886. {
  1887. pITask->Release();
  1888. }
  1889. // close the connection that was established by the utility
  1890. if ( bCloseConnection == TRUE )
  1891. CloseConnection( tcresubops.szServer );
  1892. Cleanup(pITaskScheduler);
  1893. ReleaseMemory(&tcresubops);
  1894. return EXIT_SUCCESS;
  1895. }
  1896. StringCchPrintf ( szBuffer, SIZE_OF_ARRAY(szBuffer), GetResString(IDS_CREATE_SUCCESSFUL), _X(tcresubops.szTaskName));
  1897. ShowMessage ( stdout, _X(szBuffer));
  1898. // Release interface pointers
  1899. if(pIPF)
  1900. {
  1901. pIPF->Release();
  1902. }
  1903. if(pITask)
  1904. {
  1905. pITask->Release();
  1906. }
  1907. if(pITaskTrig)
  1908. {
  1909. pITaskTrig->Release();
  1910. }
  1911. // close the connection that was established by the utility
  1912. if ( bCloseConnection == TRUE )
  1913. CloseConnection( tcresubops.szServer );
  1914. Cleanup(pITaskScheduler);
  1915. //release memory
  1916. ReleaseMemory(&tcresubops);
  1917. return hr;
  1918. }
  1919. /******************************************************************************
  1920. Routine Description:
  1921. This routine displays the create option usage
  1922. Arguments:
  1923. None
  1924. Return Value :
  1925. DWORD
  1926. ******************************************************************************/
  1927. DWORD
  1928. DisplayCreateUsage()
  1929. {
  1930. WCHAR szTmpBuffer[ 2 * MAX_STRING_LENGTH];
  1931. WCHAR szBuffer[ 2 * MAX_STRING_LENGTH];
  1932. WCHAR szFormat[MAX_DATE_STR_LEN];
  1933. WORD wFormatID = 0;
  1934. // initialize to zero
  1935. SecureZeroMemory ( szTmpBuffer, SIZE_OF_ARRAY(szTmpBuffer));
  1936. SecureZeroMemory ( szBuffer, SIZE_OF_ARRAY(szBuffer));
  1937. SecureZeroMemory ( szFormat, SIZE_OF_ARRAY(szFormat));
  1938. // get the date format
  1939. if ( GetDateFormatString( szFormat) )
  1940. {
  1941. return RETVAL_FAIL;
  1942. }
  1943. // Displaying Create usage
  1944. for( DWORD dw = IDS_CREATE_HLP1; dw <= IDS_CREATE_HLP141; dw++ )
  1945. {
  1946. switch (dw)
  1947. {
  1948. case IDS_CREATE_HLP78:
  1949. StringCchPrintf ( szTmpBuffer, SIZE_OF_ARRAY(szTmpBuffer), GetResString(IDS_CREATE_HLP79), _X(szFormat) );
  1950. StringCchPrintf ( szBuffer, SIZE_OF_ARRAY(szBuffer), L"%s%s%s", GetResString(IDS_CREATE_HLP78), _X(szTmpBuffer), GetResString(IDS_CREATE_HLP80) );
  1951. ShowMessage ( stdout, _X(szBuffer) );
  1952. dw = IDS_CREATE_HLP80;
  1953. break;
  1954. case IDS_CREATE_HLP81:
  1955. StringCchPrintf ( szTmpBuffer, SIZE_OF_ARRAY(szTmpBuffer), GetResString(IDS_CREATE_HLP82), _X(szFormat) );
  1956. StringCchPrintf ( szBuffer, SIZE_OF_ARRAY(szBuffer), L"%s%s", GetResString(IDS_CREATE_HLP81), _X(szTmpBuffer) );
  1957. ShowMessage ( stdout, _X(szBuffer) );
  1958. dw = IDS_CREATE_HLP82;
  1959. break;
  1960. case IDS_CREATE_HLP115:
  1961. // get the date format
  1962. if ( RETVAL_FAIL == GetDateFieldFormat( &wFormatID ))
  1963. {
  1964. return RETVAL_FAIL;
  1965. }
  1966. if ( wFormatID == 0)
  1967. {
  1968. StringCopy (szFormat, GetResString (IDS_MMDDYY_VALUE), SIZE_OF_ARRAY(szFormat) );
  1969. }
  1970. else if ( wFormatID == 1)
  1971. {
  1972. StringCopy (szFormat, GetResString (IDS_DDMMYY_VALUE), SIZE_OF_ARRAY(szFormat));
  1973. }
  1974. else
  1975. {
  1976. StringCopy (szFormat, GetResString (IDS_YYMMDD_VALUE), SIZE_OF_ARRAY(szFormat));
  1977. }
  1978. ShowMessageEx ( stdout, 1, FALSE, GetResString(IDS_CREATE_HLP115), _X(szFormat));
  1979. break;
  1980. default :
  1981. ShowMessage(stdout, GetResString(dw));
  1982. break;
  1983. }
  1984. }
  1985. return EXIT_SUCCESS;
  1986. }
  1987. /******************************************************************************
  1988. Routine Description:
  1989. This routine validates the options specified by the user & determines
  1990. the type of a scheduled task
  1991. Arguments:
  1992. [ in ] argc : The count of arguments given by the user.
  1993. [ in ] argv : Array containing the command line arguments.
  1994. [ out ] tcresubops : Structure containing Scheduled task's properties.
  1995. [ out ] tcreoptvals : Structure containing optional properties to set for a
  1996. scheduledtask .
  1997. [ out ] pdwRetScheType : pointer to the type of a schedule task
  1998. [Daily,once,weekly etc].
  1999. [ out ] pbUserStatus : pointer to check whether the -ru is given in
  2000. the command line or not.
  2001. Return Value :
  2002. A DWORD value indicating RETVAL_SUCCESS on success else E_FAIL
  2003. on failure
  2004. ******************************************************************************/
  2005. DWORD
  2006. ProcessCreateOptions(
  2007. IN DWORD argc,
  2008. IN LPCTSTR argv[],
  2009. IN OUT TCREATESUBOPTS &tcresubops,
  2010. IN OUT TCREATEOPVALS &tcreoptvals,
  2011. OUT DWORD* pdwRetScheType,
  2012. OUT WORD *pwUserStatus
  2013. )
  2014. {
  2015. DWORD dwScheduleType = 0;
  2016. TCMDPARSER2 cmdCreateOptions[MAX_CREATE_OPTIONS];
  2017. BOOL bReturn = FALSE;
  2018. // /create sub-options
  2019. const WCHAR szCreateOpt[] = L"create";
  2020. const WCHAR szCreateHelpOpt[] = L"?";
  2021. const WCHAR szCreateServerOpt[] = L"s";
  2022. const WCHAR szCreateRunAsUserOpt[] = L"ru";
  2023. const WCHAR szCreateRunAsPwd[] = L"rp";
  2024. const WCHAR szCreateUserOpt[] = L"u";
  2025. const WCHAR szCreatePwdOpt[] = L"p";
  2026. const WCHAR szCreateSCTypeOpt[] = L"sc";
  2027. const WCHAR szCreateModifierOpt[] = L"mo";
  2028. const WCHAR szCreateDayOpt[] = L"d";
  2029. const WCHAR szCreateMonthsOpt[] = L"m";
  2030. const WCHAR szCreateIdleTimeOpt[] = L"i";
  2031. const WCHAR szCreateTaskNameOpt[] = L"tn";
  2032. const WCHAR szCreateTaskRunOpt[] = L"tr";
  2033. const WCHAR szCreateStartTimeOpt[] = L"st";
  2034. const WCHAR szCreateEndTimeOpt[] = L"et";
  2035. const WCHAR szCreateStartDateOpt[] = L"sd";
  2036. const WCHAR szCreateEndDateOpt[] = L"ed";
  2037. const WCHAR szCreateInteractiveOpt[] = L"it";
  2038. const WCHAR szCreateKillAtDurOpt[] = L"k";
  2039. const WCHAR szCreateDurationOpt[] = L"du";
  2040. const WCHAR szCreateRepeatOpt[] = L"ri";
  2041. const WCHAR szCreateDeleteNoSchedOpt[] = L"z";
  2042. const WCHAR szCreateForceOpt[] = L"f" ;
  2043. // set all the fields to 0
  2044. SecureZeroMemory( cmdCreateOptions, sizeof( TCMDPARSER2 ) * MAX_CREATE_OPTIONS );
  2045. //
  2046. // fill the commandline parser
  2047. //
  2048. // /create option
  2049. StringCopyA( cmdCreateOptions[ OI_CREATE_OPTION ].szSignature, "PARSER2\0", 8 );
  2050. cmdCreateOptions[ OI_CREATE_OPTION ].dwType = CP_TYPE_BOOLEAN;
  2051. cmdCreateOptions[ OI_CREATE_OPTION ].pwszOptions = szCreateOpt;
  2052. cmdCreateOptions[ OI_CREATE_OPTION ].dwCount = 1;
  2053. cmdCreateOptions[ OI_CREATE_OPTION ].dwFlags = 0;
  2054. cmdCreateOptions[ OI_CREATE_OPTION ].pValue = &tcresubops.bCreate;
  2055. // /? option
  2056. StringCopyA( cmdCreateOptions[ OI_CREATE_USAGE ].szSignature, "PARSER2\0", 8 );
  2057. cmdCreateOptions[ OI_CREATE_USAGE ].dwType = CP_TYPE_BOOLEAN;
  2058. cmdCreateOptions[ OI_CREATE_USAGE ].pwszOptions = szCreateHelpOpt;
  2059. cmdCreateOptions[ OI_CREATE_USAGE ].dwCount = 1;
  2060. cmdCreateOptions[ OI_CREATE_USAGE ].dwFlags = CP2_USAGE;
  2061. cmdCreateOptions[ OI_CREATE_USAGE ].pValue = &tcresubops.bUsage;
  2062. // /s option
  2063. StringCopyA( cmdCreateOptions[ OI_CREATE_SERVER ].szSignature, "PARSER2\0", 8 );
  2064. cmdCreateOptions[ OI_CREATE_SERVER ].dwType = CP_TYPE_TEXT;
  2065. cmdCreateOptions[ OI_CREATE_SERVER ].pwszOptions = szCreateServerOpt;
  2066. cmdCreateOptions[ OI_CREATE_SERVER ].dwCount = 1;
  2067. cmdCreateOptions[ OI_CREATE_SERVER ].dwFlags = CP2_ALLOCMEMORY| CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL ;
  2068. // /u option
  2069. StringCopyA( cmdCreateOptions[ OI_CREATE_USERNAME ].szSignature, "PARSER2\0", 8 );
  2070. cmdCreateOptions[ OI_CREATE_USERNAME ].dwType = CP_TYPE_TEXT;
  2071. cmdCreateOptions[ OI_CREATE_USERNAME ].pwszOptions = szCreateUserOpt;
  2072. cmdCreateOptions[ OI_CREATE_USERNAME ].dwCount = 1;
  2073. cmdCreateOptions[ OI_CREATE_USERNAME ].dwFlags = CP2_ALLOCMEMORY | CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL ;
  2074. // /p option
  2075. StringCopyA( cmdCreateOptions[ OI_CREATE_PASSWORD ].szSignature, "PARSER2\0", 8 );
  2076. cmdCreateOptions[ OI_CREATE_PASSWORD ].dwType = CP_TYPE_TEXT;
  2077. cmdCreateOptions[ OI_CREATE_PASSWORD ].pwszOptions = szCreatePwdOpt;
  2078. cmdCreateOptions[ OI_CREATE_PASSWORD ].dwCount = 1;
  2079. cmdCreateOptions[ OI_CREATE_PASSWORD ].dwActuals = 0;
  2080. cmdCreateOptions[ OI_CREATE_PASSWORD ].dwFlags = CP2_ALLOCMEMORY | CP2_VALUE_OPTIONAL ;
  2081. // /ru option
  2082. StringCopyA( cmdCreateOptions[ OI_CREATE_RUNASUSERNAME ].szSignature, "PARSER2\0", 8 );
  2083. cmdCreateOptions[ OI_CREATE_RUNASUSERNAME ].dwType = CP_TYPE_TEXT;
  2084. cmdCreateOptions[ OI_CREATE_RUNASUSERNAME ].pwszOptions = szCreateRunAsUserOpt;
  2085. cmdCreateOptions[ OI_CREATE_RUNASUSERNAME ].dwCount = 1;
  2086. cmdCreateOptions[ OI_CREATE_RUNASUSERNAME ].dwFlags = CP2_ALLOCMEMORY| CP2_VALUE_TRIMINPUT ;
  2087. // /rp option
  2088. StringCopyA( cmdCreateOptions[ OI_CREATE_RUNASPASSWORD ].szSignature, "PARSER2\0", 8 );
  2089. cmdCreateOptions[ OI_CREATE_RUNASPASSWORD ].dwType = CP_TYPE_TEXT;
  2090. cmdCreateOptions[ OI_CREATE_RUNASPASSWORD ].pwszOptions = szCreateRunAsPwd;
  2091. cmdCreateOptions[ OI_CREATE_RUNASPASSWORD ].dwCount = 1;
  2092. cmdCreateOptions[ OI_CREATE_RUNASPASSWORD ].dwActuals = 0;
  2093. cmdCreateOptions[ OI_CREATE_RUNASPASSWORD ].dwFlags = CP2_ALLOCMEMORY | CP2_VALUE_OPTIONAL;
  2094. // /sc option
  2095. StringCopyA( cmdCreateOptions[ OI_CREATE_SCHEDTYPE ].szSignature, "PARSER2\0", 8 );
  2096. cmdCreateOptions[ OI_CREATE_SCHEDTYPE ].dwType = CP_TYPE_TEXT;
  2097. cmdCreateOptions[ OI_CREATE_SCHEDTYPE ].pwszOptions = szCreateSCTypeOpt;
  2098. cmdCreateOptions[ OI_CREATE_SCHEDTYPE ].dwCount = 1;
  2099. cmdCreateOptions[ OI_CREATE_SCHEDTYPE ].dwFlags = CP2_MANDATORY| CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  2100. cmdCreateOptions[ OI_CREATE_SCHEDTYPE ].pValue = tcresubops.szSchedType;
  2101. cmdCreateOptions[ OI_CREATE_SCHEDTYPE ].dwLength = MAX_STRING_LENGTH;
  2102. // /mo option
  2103. StringCopyA( cmdCreateOptions[ OI_CREATE_MODIFIER ].szSignature, "PARSER2\0", 8 );
  2104. cmdCreateOptions[ OI_CREATE_MODIFIER ].dwType = CP_TYPE_TEXT;
  2105. cmdCreateOptions[ OI_CREATE_MODIFIER ].pwszOptions = szCreateModifierOpt;
  2106. cmdCreateOptions[ OI_CREATE_MODIFIER ].dwCount = 1;
  2107. cmdCreateOptions[ OI_CREATE_MODIFIER ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  2108. cmdCreateOptions[ OI_CREATE_MODIFIER ].pValue = tcresubops.szModifier;
  2109. cmdCreateOptions[ OI_CREATE_MODIFIER ].dwLength = MAX_STRING_LENGTH;
  2110. // /d option
  2111. StringCopyA( cmdCreateOptions[ OI_CREATE_DAY ].szSignature, "PARSER2\0", 8 );
  2112. cmdCreateOptions[ OI_CREATE_DAY ].dwType = CP_TYPE_TEXT;
  2113. cmdCreateOptions[ OI_CREATE_DAY ].pwszOptions = szCreateDayOpt;
  2114. cmdCreateOptions[ OI_CREATE_DAY ].dwCount = 1;
  2115. cmdCreateOptions[ OI_CREATE_DAY ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  2116. cmdCreateOptions[ OI_CREATE_DAY ].pValue = tcresubops.szDays;
  2117. cmdCreateOptions[ OI_CREATE_DAY ].dwLength = MAX_STRING_LENGTH;
  2118. // /m option
  2119. StringCopyA( cmdCreateOptions[ OI_CREATE_MONTHS ].szSignature, "PARSER2\0", 8 );
  2120. cmdCreateOptions[ OI_CREATE_MONTHS ].dwType = CP_TYPE_TEXT;
  2121. cmdCreateOptions[ OI_CREATE_MONTHS ].pwszOptions = szCreateMonthsOpt;
  2122. cmdCreateOptions[ OI_CREATE_MONTHS ].dwCount = 1;
  2123. cmdCreateOptions[ OI_CREATE_MONTHS ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  2124. cmdCreateOptions[ OI_CREATE_MONTHS ].pValue = tcresubops.szMonths;
  2125. cmdCreateOptions[ OI_CREATE_MONTHS ].dwLength = MAX_STRING_LENGTH;
  2126. // /i option
  2127. StringCopyA( cmdCreateOptions[ OI_CREATE_IDLETIME ].szSignature, "PARSER2\0", 8 );
  2128. cmdCreateOptions[ OI_CREATE_IDLETIME ].dwType = CP_TYPE_TEXT;
  2129. cmdCreateOptions[ OI_CREATE_IDLETIME ].pwszOptions = szCreateIdleTimeOpt;
  2130. cmdCreateOptions[ OI_CREATE_IDLETIME ].dwCount = 1;
  2131. cmdCreateOptions[ OI_CREATE_IDLETIME ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  2132. cmdCreateOptions[ OI_CREATE_IDLETIME ].pValue = tcresubops.szIdleTime;
  2133. cmdCreateOptions[ OI_CREATE_IDLETIME ].dwLength = MAX_STRING_LENGTH;
  2134. // /tn option
  2135. StringCopyA( cmdCreateOptions[ OI_CREATE_TASKNAME ].szSignature, "PARSER2\0", 8 );
  2136. cmdCreateOptions[ OI_CREATE_TASKNAME ].dwType = CP_TYPE_TEXT;
  2137. cmdCreateOptions[ OI_CREATE_TASKNAME ].pwszOptions = szCreateTaskNameOpt;
  2138. cmdCreateOptions[ OI_CREATE_TASKNAME ].dwCount = 1;
  2139. cmdCreateOptions[ OI_CREATE_TASKNAME ].dwFlags = CP2_MANDATORY;
  2140. cmdCreateOptions[ OI_CREATE_TASKNAME ].pValue = tcresubops.szTaskName;
  2141. cmdCreateOptions[ OI_CREATE_TASKNAME ].dwLength = MAX_JOB_LEN;
  2142. // /tr option
  2143. StringCopyA( cmdCreateOptions[ OI_CREATE_TASKRUN ].szSignature, "PARSER2\0", 8 );
  2144. cmdCreateOptions[ OI_CREATE_TASKRUN ].dwType = CP_TYPE_TEXT;
  2145. cmdCreateOptions[ OI_CREATE_TASKRUN ].pwszOptions = szCreateTaskRunOpt;
  2146. cmdCreateOptions[ OI_CREATE_TASKRUN ].dwCount = 1;
  2147. cmdCreateOptions[ OI_CREATE_TASKRUN ].dwFlags = CP2_MANDATORY| CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL ;
  2148. cmdCreateOptions[ OI_CREATE_TASKRUN ].pValue = tcresubops.szTaskRun;
  2149. cmdCreateOptions[ OI_CREATE_TASKRUN ].dwLength = MAX_TASK_LEN;
  2150. // /st option
  2151. StringCopyA( cmdCreateOptions[ OI_CREATE_STARTTIME ].szSignature, "PARSER2\0", 8 );
  2152. cmdCreateOptions[ OI_CREATE_STARTTIME ].dwType = CP_TYPE_TEXT;
  2153. cmdCreateOptions[ OI_CREATE_STARTTIME ].pwszOptions = szCreateStartTimeOpt;
  2154. cmdCreateOptions[ OI_CREATE_STARTTIME ].dwCount = 1;
  2155. cmdCreateOptions[ OI_CREATE_STARTTIME ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  2156. cmdCreateOptions[ OI_CREATE_STARTTIME ].pValue = tcresubops.szStartTime;
  2157. cmdCreateOptions[ OI_CREATE_STARTTIME ].dwLength = MAX_STRING_LENGTH;
  2158. // /sd option
  2159. StringCopyA( cmdCreateOptions[ OI_CREATE_STARTDATE ].szSignature, "PARSER2\0", 8 );
  2160. cmdCreateOptions[ OI_CREATE_STARTDATE ].dwType = CP_TYPE_TEXT;
  2161. cmdCreateOptions[ OI_CREATE_STARTDATE ].pwszOptions = szCreateStartDateOpt;
  2162. cmdCreateOptions[ OI_CREATE_STARTDATE ].dwCount = 1;
  2163. cmdCreateOptions[ OI_CREATE_STARTDATE ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  2164. cmdCreateOptions[ OI_CREATE_STARTDATE ].pValue = tcresubops.szStartDate;
  2165. cmdCreateOptions[ OI_CREATE_STARTDATE ].dwLength = MAX_STRING_LENGTH;
  2166. // /ed option
  2167. StringCopyA( cmdCreateOptions[ OI_CREATE_ENDDATE ].szSignature, "PARSER2\0", 8 );
  2168. cmdCreateOptions[ OI_CREATE_ENDDATE ].dwType = CP_TYPE_TEXT;
  2169. cmdCreateOptions[ OI_CREATE_ENDDATE ].pwszOptions = szCreateEndDateOpt;
  2170. cmdCreateOptions[ OI_CREATE_ENDDATE ].dwCount = 1;
  2171. cmdCreateOptions[ OI_CREATE_ENDDATE ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  2172. cmdCreateOptions[ OI_CREATE_ENDDATE ].pValue = tcresubops.szEndDate;
  2173. cmdCreateOptions[ OI_CREATE_ENDDATE ].dwLength = MAX_STRING_LENGTH;
  2174. // /it option
  2175. StringCopyA( cmdCreateOptions[ OI_CREATE_LOGON_ACTIVE ].szSignature, "PARSER2\0", 8 );
  2176. cmdCreateOptions[ OI_CREATE_LOGON_ACTIVE ].dwType = CP_TYPE_BOOLEAN;
  2177. cmdCreateOptions[ OI_CREATE_LOGON_ACTIVE ].pwszOptions = szCreateInteractiveOpt;
  2178. cmdCreateOptions[ OI_CREATE_LOGON_ACTIVE ].dwCount = 1;
  2179. cmdCreateOptions[ OI_CREATE_LOGON_ACTIVE ].pValue = &tcresubops.bActive;
  2180. // /et option
  2181. StringCopyA( cmdCreateOptions[ OI_CREATE_ENDTIME ].szSignature, "PARSER2\0", 8 );
  2182. cmdCreateOptions[ OI_CREATE_ENDTIME ].dwType = CP_TYPE_TEXT;
  2183. cmdCreateOptions[ OI_CREATE_ENDTIME ].pwszOptions = szCreateEndTimeOpt;
  2184. cmdCreateOptions[ OI_CREATE_ENDTIME ].dwCount = 1;
  2185. cmdCreateOptions[ OI_CREATE_ENDTIME ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  2186. cmdCreateOptions[ OI_CREATE_ENDTIME ].pValue = &tcresubops.szEndTime;
  2187. cmdCreateOptions[ OI_CREATE_ENDTIME ].dwLength = MAX_STRING_LENGTH;
  2188. // /k option
  2189. StringCopyA( cmdCreateOptions[ OI_CREATE_DUR_END ].szSignature, "PARSER2\0", 8 );
  2190. cmdCreateOptions[ OI_CREATE_DUR_END ].dwType = CP_TYPE_BOOLEAN ;
  2191. cmdCreateOptions[ OI_CREATE_DUR_END ].pwszOptions = szCreateKillAtDurOpt ;
  2192. cmdCreateOptions[ OI_CREATE_DUR_END ].dwCount = 1 ;
  2193. cmdCreateOptions[ OI_CREATE_DUR_END ].pValue = &tcresubops.bIsDurEnd;
  2194. // /du option
  2195. StringCopyA( cmdCreateOptions[ OI_CREATE_DURATION ].szSignature, "PARSER2\0", 8 );
  2196. cmdCreateOptions[ OI_CREATE_DURATION ].dwType = CP_TYPE_TEXT;
  2197. cmdCreateOptions[ OI_CREATE_DURATION ].pwszOptions = szCreateDurationOpt;
  2198. cmdCreateOptions[ OI_CREATE_DURATION ].dwCount = 1;
  2199. cmdCreateOptions[ OI_CREATE_DURATION ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  2200. cmdCreateOptions[ OI_CREATE_DURATION ].pValue = tcresubops.szDuration;
  2201. cmdCreateOptions[ OI_CREATE_DURATION ].dwLength = MAX_STRING_LENGTH;
  2202. // /ri option
  2203. StringCopyA( cmdCreateOptions[ OI_CREATE_REPEAT_INTERVAL ].szSignature, "PARSER2\0", 8 );
  2204. cmdCreateOptions[ OI_CREATE_REPEAT_INTERVAL ].dwType = CP_TYPE_TEXT;
  2205. cmdCreateOptions[ OI_CREATE_REPEAT_INTERVAL ].pwszOptions = szCreateRepeatOpt;
  2206. cmdCreateOptions[ OI_CREATE_REPEAT_INTERVAL ].dwCount = 1;
  2207. cmdCreateOptions[ OI_CREATE_REPEAT_INTERVAL ].dwFlags = CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  2208. cmdCreateOptions[ OI_CREATE_REPEAT_INTERVAL ].pValue = tcresubops.szRepeat;
  2209. cmdCreateOptions[ OI_CREATE_REPEAT_INTERVAL ].dwLength = MAX_STRING_LENGTH;
  2210. // /z option
  2211. StringCopyA( cmdCreateOptions[ OI_CREATE_DELNOSCHED ].szSignature, "PARSER2\0", 8 );
  2212. cmdCreateOptions[ OI_CREATE_DELNOSCHED ].dwType = CP_TYPE_BOOLEAN ;
  2213. cmdCreateOptions[ OI_CREATE_DELNOSCHED ].pwszOptions = szCreateDeleteNoSchedOpt ;
  2214. cmdCreateOptions[ OI_CREATE_DELNOSCHED ].dwCount = 1 ;
  2215. cmdCreateOptions[ OI_CREATE_DELNOSCHED ].pValue = &tcresubops.bIsDeleteNoSched;
  2216. // /f option
  2217. StringCopyA( cmdCreateOptions[ OI_CREATE_FORCE ].szSignature, "PARSER2\0", 8 );
  2218. cmdCreateOptions[ OI_CREATE_FORCE ].dwType = CP_TYPE_BOOLEAN ;
  2219. cmdCreateOptions[ OI_CREATE_FORCE ].pwszOptions = szCreateForceOpt ;
  2220. cmdCreateOptions[ OI_CREATE_FORCE ].dwCount = 1 ;
  2221. cmdCreateOptions[ OI_CREATE_FORCE ].pValue = &tcresubops.bForce;
  2222. //parse command line arguments
  2223. bReturn = DoParseParam2( argc, argv, 0, SIZE_OF_ARRAY(cmdCreateOptions), cmdCreateOptions, 0);
  2224. if( FALSE == bReturn) // Invalid commandline
  2225. {
  2226. //display an error message
  2227. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  2228. //release memory
  2229. ReleaseMemory(&tcresubops);
  2230. return EXIT_FAILURE;
  2231. }
  2232. // get the buffer pointers allocated by command line parser
  2233. tcresubops.szServer = (LPWSTR)cmdCreateOptions[ OI_CREATE_SERVER ].pValue;
  2234. tcresubops.szUser = (LPWSTR)cmdCreateOptions[ OI_CREATE_USERNAME ].pValue;
  2235. tcresubops.szPassword = (LPWSTR)cmdCreateOptions[ OI_CREATE_PASSWORD ].pValue;
  2236. tcresubops.szRunAsUser = (LPWSTR)cmdCreateOptions[ OI_CREATE_RUNASUSERNAME ].pValue;
  2237. tcresubops.szRunAsPassword = (LPWSTR)cmdCreateOptions[ OI_CREATE_RUNASPASSWORD ].pValue;
  2238. // If -rp is not specified allocate the memory
  2239. if ( cmdCreateOptions[OI_CREATE_RUNASPASSWORD].dwActuals == 0 )
  2240. {
  2241. // password
  2242. if ( tcresubops.szRunAsPassword == NULL )
  2243. {
  2244. tcresubops.szRunAsPassword = (LPWSTR)AllocateMemory( MAX_STRING_LENGTH * sizeof( WCHAR ) );
  2245. if ( tcresubops.szRunAsPassword == NULL )
  2246. {
  2247. SaveLastError();
  2248. //release memory
  2249. ReleaseMemory(&tcresubops);
  2250. return EXIT_FAILURE;
  2251. }
  2252. }
  2253. }
  2254. if ( (argc > 3) && (tcresubops.bUsage == TRUE) )
  2255. {
  2256. ShowMessage ( stderr, GetResString (IDS_ERROR_CREATEPARAM) );
  2257. //release memory
  2258. ReleaseMemory(&tcresubops);
  2259. return RETVAL_FAIL;
  2260. }
  2261. // Display create usage if user specified -create -? option
  2262. if( tcresubops.bUsage == TRUE)
  2263. {
  2264. DisplayCreateUsage();
  2265. //release memory
  2266. ReleaseMemory(&tcresubops);
  2267. return RETVAL_FAIL;
  2268. }
  2269. //
  2270. //check for INVALID SYNTAX
  2271. //
  2272. // check for invalid user name
  2273. if( ( cmdCreateOptions[OI_CREATE_SERVER].dwActuals == 0 ) && ( cmdCreateOptions[OI_CREATE_USERNAME].dwActuals == 1 ) )
  2274. {
  2275. ShowMessage(stderr, GetResString(IDS_CREATE_USER_BUT_NOMACHINE));
  2276. //release memory
  2277. ReleaseMemory(&tcresubops);
  2278. return RETVAL_FAIL;
  2279. }
  2280. //Determine scheduled type
  2281. if( StringCompare(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_MINUTE), TRUE, 0) == 0 )
  2282. {
  2283. dwScheduleType = SCHED_TYPE_MINUTE;
  2284. }
  2285. else if( StringCompare(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_HOUR), TRUE, 0) == 0 )
  2286. {
  2287. dwScheduleType = SCHED_TYPE_HOURLY;
  2288. }
  2289. else if( StringCompare(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_DAILY), TRUE, 0) == 0 )
  2290. {
  2291. dwScheduleType = SCHED_TYPE_DAILY;
  2292. }
  2293. else if( StringCompare(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_WEEK), TRUE, 0) == 0 )
  2294. {
  2295. dwScheduleType = SCHED_TYPE_WEEKLY;
  2296. }
  2297. else if( StringCompare(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_MONTHLY), TRUE, 0) == 0 )
  2298. {
  2299. dwScheduleType = SCHED_TYPE_MONTHLY;
  2300. }
  2301. else if( StringCompare(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_ONCE), TRUE, 0) == 0 )
  2302. {
  2303. dwScheduleType = SCHED_TYPE_ONETIME;
  2304. }
  2305. else if( StringCompare(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_STARTUP), TRUE, 0) == 0 )
  2306. {
  2307. dwScheduleType = SCHED_TYPE_ONSTART;
  2308. }
  2309. else if( StringCompare(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_LOGON), TRUE, 0) == 0 )
  2310. {
  2311. dwScheduleType = SCHED_TYPE_ONLOGON;
  2312. }
  2313. else if( StringCompare( tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_IDLE), TRUE, 0) == 0 )
  2314. {
  2315. dwScheduleType = SCHED_TYPE_ONIDLE;
  2316. }
  2317. else
  2318. {
  2319. ShowMessage(stderr,GetResString(IDS_INVALID_SCHEDTYPE));
  2320. //release memory
  2321. ReleaseMemory(&tcresubops);
  2322. return RETVAL_FAIL;
  2323. }
  2324. // check whether /RT is specified for the schedule type minute or hourly
  2325. if ( ( ( dwScheduleType == SCHED_TYPE_MINUTE) || ( dwScheduleType == SCHED_TYPE_HOURLY) ) &&
  2326. ( ( cmdCreateOptions[OI_CREATE_REPEAT_INTERVAL].dwActuals == 1 ) ) )
  2327. {
  2328. // display an error message as .. /RT is not applicable for minute or hourly types..
  2329. ShowMessage ( stderr, GetResString (IDS_REPEAT_NA) );
  2330. ReleaseMemory(&tcresubops);
  2331. return RETVAL_FAIL;
  2332. }
  2333. // check whether the options /RI, /DU, /ST, /SD, /ET, /ED and /K are specified for
  2334. // the schedule type ONSTRAT, ONIDLE and ONLOGON
  2335. if ( ( ( dwScheduleType == SCHED_TYPE_ONSTART) || ( dwScheduleType == SCHED_TYPE_ONLOGON) ||
  2336. ( dwScheduleType == SCHED_TYPE_ONIDLE) ) &&
  2337. ( ( cmdCreateOptions[OI_CREATE_REPEAT_INTERVAL].dwActuals == 1 ) ||
  2338. ( cmdCreateOptions[OI_CREATE_STARTTIME].dwActuals == 1 ) || ( cmdCreateOptions[OI_CREATE_STARTDATE].dwActuals == 1 ) ||
  2339. ( cmdCreateOptions[OI_CREATE_ENDTIME].dwActuals == 1 ) || ( cmdCreateOptions[OI_CREATE_ENDDATE].dwActuals == 1 ) ||
  2340. ( cmdCreateOptions[OI_CREATE_DURATION].dwActuals == 1 ) || ( cmdCreateOptions[OI_CREATE_DUR_END].dwActuals == 1 ) )
  2341. )
  2342. {
  2343. // display an error message as .. /RT is not applicable for minute or hourly types..
  2344. ShowMessage ( stderr, GetResString (IDS_OPTIONS_NA) );
  2345. ReleaseMemory(&tcresubops);
  2346. return RETVAL_FAIL;
  2347. }
  2348. // check whether /SD o /ED is specified for the scheduled type ONETIME
  2349. if( ( dwScheduleType == SCHED_TYPE_ONETIME) && ( cmdCreateOptions[OI_CREATE_ENDDATE].dwActuals == 1 ) )
  2350. {
  2351. // display an error message as.. /SD or /ED is not allowed for ONCE
  2352. ShowMessage(stderr, GetResString(IDS_ONCE_NA_OPTIONS));
  2353. //release memory
  2354. ReleaseMemory(&tcresubops);
  2355. return RETVAL_FAIL;
  2356. }
  2357. // check whether /K is specified without specifying either /RT
  2358. if ( (( dwScheduleType != SCHED_TYPE_MINUTE) && ( dwScheduleType != SCHED_TYPE_HOURLY)) &&
  2359. (( cmdCreateOptions[OI_CREATE_ENDTIME].dwActuals == 0 ) && ( cmdCreateOptions[OI_CREATE_DURATION].dwActuals == 0 ) ) &&
  2360. ( cmdCreateOptions[OI_CREATE_DUR_END].dwActuals == 1 ) )
  2361. {
  2362. // display an erroe message as .. /K cannot be specified without specifying either /ET or /DU
  2363. ShowMessage(stderr, GetResString(IDS_NO_ENDTIME));
  2364. //release memory
  2365. ReleaseMemory(&tcresubops);
  2366. return RETVAL_FAIL;
  2367. }
  2368. // check whether /ET and /DU specified..
  2369. if( ( cmdCreateOptions[OI_CREATE_DURATION].dwActuals == 1 ) && ( cmdCreateOptions[OI_CREATE_ENDTIME].dwActuals == 1 ) )
  2370. {
  2371. // display an error message as.. /ET and /DU are mutual exclusive
  2372. ShowMessage(stderr, GetResString(IDS_DURATION_NOT_ENDTIME));
  2373. //release memory
  2374. ReleaseMemory(&tcresubops);
  2375. return RETVAL_FAIL;
  2376. }
  2377. // Assign the scheduled type to the out parameter.
  2378. *pdwRetScheType = dwScheduleType;
  2379. // To find whether run as user name is given in the cmd line or not
  2380. if( ( cmdCreateOptions[OI_CREATE_SERVER].dwActuals == 1 ) &&
  2381. ( (cmdCreateOptions[OI_CREATE_RUNASUSERNAME].dwActuals == 0) && (cmdCreateOptions[OI_CREATE_USERNAME].dwActuals == 0) ) )
  2382. {
  2383. *pwUserStatus = OI_CREATE_SERVER;
  2384. }
  2385. else if( (cmdCreateOptions[OI_CREATE_RUNASUSERNAME].dwActuals == 1) && (cmdCreateOptions[OI_CREATE_USERNAME].dwActuals == 1) )
  2386. {
  2387. *pwUserStatus = OI_RUNANDUSER;
  2388. }
  2389. else if( cmdCreateOptions[OI_CREATE_RUNASUSERNAME].dwActuals == 1 )
  2390. {
  2391. *pwUserStatus = OI_CREATE_RUNASUSERNAME;
  2392. }
  2393. else if ( cmdCreateOptions[OI_CREATE_USERNAME].dwActuals == 1 )
  2394. {
  2395. *pwUserStatus = OI_CREATE_USERNAME;
  2396. }
  2397. // Start validations for the sub-options
  2398. if( RETVAL_FAIL == ValidateSuboptVal(tcresubops, tcreoptvals, cmdCreateOptions, dwScheduleType) )
  2399. {
  2400. //release memory
  2401. ReleaseMemory(&tcresubops);
  2402. return(RETVAL_FAIL);
  2403. }
  2404. return RETVAL_SUCCESS;
  2405. }
  2406. /******************************************************************************
  2407. Routine Description:
  2408. This routine splits the input parameters into 2 substrings and returns it.
  2409. Arguments:
  2410. [ in ] szInput : Input string.
  2411. [ out ] szFirstString : First Output string containing the path of the
  2412. file.
  2413. [ out ] szSecondString : The second output containing the paramters.
  2414. Return Value :
  2415. A DWORD value indicating RETVAL_SUCCESS on success else E_FAIL
  2416. on failure
  2417. ******************************************************************************/
  2418. DWORD
  2419. ProcessFilePath(
  2420. IN LPWSTR szInput,
  2421. OUT LPWSTR szFirstString,
  2422. OUT LPWSTR szSecondString
  2423. )
  2424. {
  2425. WCHAR *pszSep = NULL ;
  2426. WCHAR szTmpString[MAX_RES_STRING] = L"\0";
  2427. WCHAR szTmpInStr[MAX_RES_STRING] = L"\0";
  2428. WCHAR szTmpOutStr[MAX_RES_STRING] = L"\0";
  2429. WCHAR szTmpString1[MAX_RES_STRING] = L"\0";
  2430. DWORD dwCnt = 0 ;
  2431. DWORD dwLen = 0 ;
  2432. #ifdef _WIN64
  2433. INT64 dwPos ;
  2434. #else
  2435. DWORD dwPos ;
  2436. #endif
  2437. //checking if the input parameters are NULL and if so
  2438. // return FAILURE. This condition will not come
  2439. // but checking for safety sake.
  2440. if( (szInput == NULL) || (StringLength(szInput, 0)==0))
  2441. {
  2442. return RETVAL_FAIL ;
  2443. }
  2444. StringCopy(szTmpString, szInput, SIZE_OF_ARRAY(szTmpString));
  2445. StringCopy(szTmpString1, szInput, SIZE_OF_ARRAY(szTmpString1));
  2446. StringCopy(szTmpInStr, szInput, SIZE_OF_ARRAY(szTmpInStr));
  2447. // check for first double quote (")
  2448. if ( szTmpInStr[0] == _T('\"') )
  2449. {
  2450. // trim the first double quote
  2451. TrimString2( szTmpInStr, _T("\""), TRIM_ALL);
  2452. // check for end double quote
  2453. pszSep = (LPWSTR)FindChar(szTmpInStr,_T('\"'), 0) ;
  2454. // get the position
  2455. dwPos = pszSep - szTmpInStr + 1;
  2456. }
  2457. else
  2458. {
  2459. // check for the space
  2460. pszSep = (LPWSTR)FindChar(szTmpInStr, _T(' '), 0) ;
  2461. // get the position
  2462. dwPos = pszSep - szTmpInStr;
  2463. }
  2464. if ( pszSep != NULL )
  2465. {
  2466. szTmpInStr[dwPos] = _T('\0');
  2467. }
  2468. else
  2469. {
  2470. StringCopy(szFirstString, szTmpString, MAX_RES_STRING);
  2471. StringCopy(szSecondString, L"\0", MAX_RES_STRING);
  2472. return RETVAL_SUCCESS;
  2473. }
  2474. // intialize the variable
  2475. dwCnt = 0 ;
  2476. // get the length of the string
  2477. dwLen = StringLength ( szTmpString, 0 );
  2478. // check for end of string
  2479. while ( ( dwPos <= dwLen ) && szTmpString[dwPos++] != _T('\0') )
  2480. {
  2481. szTmpOutStr[dwCnt++] = szTmpString[dwPos];
  2482. }
  2483. // trim the executable and arguments
  2484. TrimString2( szTmpInStr, _T("\""), TRIM_ALL);
  2485. TrimString2( szTmpInStr, _T(" "), TRIM_ALL);
  2486. StringCopy(szFirstString, szTmpInStr, MAX_RES_STRING);
  2487. StringCopy(szSecondString, szTmpOutStr, MAX_RES_STRING);
  2488. // return success
  2489. return RETVAL_SUCCESS;
  2490. }
  2491. /******************************************************************************
  2492. Routine Description:
  2493. Release memory
  2494. Arguments:
  2495. [ in ] pParam : cmdOptions structure
  2496. Return Value :
  2497. TRUE on success
  2498. ******************************************************************************/
  2499. BOOL
  2500. ReleaseMemory(
  2501. IN PTCREATESUBOPTS pParams
  2502. )
  2503. {
  2504. // release memory
  2505. FreeMemory((LPVOID *) &pParams->szServer);
  2506. FreeMemory((LPVOID *) &pParams->szUser);
  2507. FreeMemory((LPVOID *) &pParams->szPassword);
  2508. FreeMemory((LPVOID *) &pParams->szRunAsUser);
  2509. FreeMemory((LPVOID *) &pParams->szRunAsPassword);
  2510. //reset all fields to 0
  2511. SecureZeroMemory( &pParams, sizeof( PTCREATESUBOPTS ) );
  2512. return TRUE;
  2513. }
  2514. DWORD
  2515. ConfirmInput (
  2516. OUT BOOL *pbCancel
  2517. )
  2518. /*++
  2519. Routine Description:
  2520. This function validates the input given by user.
  2521. Arguments:
  2522. None
  2523. Return Value:
  2524. EXIT_FAILURE : On failure
  2525. EXIT_SUCCESS : On success
  2526. --*/
  2527. {
  2528. // sub-local variables
  2529. DWORD dwCharsRead = 0;
  2530. DWORD dwPrevConsoleMode = 0;
  2531. HANDLE hInputConsole = NULL;
  2532. BOOL bIndirectionInput = FALSE;
  2533. CHAR chAnsi = '\0';
  2534. CHAR szAnsiBuf[ 10 ] = "\0";
  2535. WCHAR chTmp = L'\0';
  2536. WCHAR wch = L'\0';
  2537. DWORD dwCharsWritten = 0;
  2538. WCHAR szBuffer[MAX_RES_STRING];
  2539. WCHAR szBackup[MAX_RES_STRING];
  2540. WCHAR szTmpBuf[MAX_RES_STRING];
  2541. DWORD dwIndex = 0 ;
  2542. BOOL bNoBreak = TRUE;
  2543. SecureZeroMemory ( szBuffer, SIZE_OF_ARRAY(szBuffer));
  2544. SecureZeroMemory ( szTmpBuf, SIZE_OF_ARRAY(szTmpBuf));
  2545. SecureZeroMemory ( szBackup, SIZE_OF_ARRAY(szBackup));
  2546. // Get the handle for the standard input
  2547. hInputConsole = GetStdHandle( STD_INPUT_HANDLE );
  2548. if ( hInputConsole == INVALID_HANDLE_VALUE )
  2549. {
  2550. SaveLastError();
  2551. // could not get the handle so return failure
  2552. return EXIT_FAILURE;
  2553. }
  2554. MessageBeep(MB_ICONEXCLAMATION);
  2555. // display the message .. Do you want to continue? ...
  2556. //DISPLAY_MESSAGE ( stdout, GetResString ( IDS_INPUT_DATA ) );
  2557. // Check for the input redirect
  2558. if( ( hInputConsole != (HANDLE)0x0000000F ) &&
  2559. ( hInputConsole != (HANDLE)0x00000003 ) &&
  2560. ( hInputConsole != INVALID_HANDLE_VALUE ) )
  2561. {
  2562. bIndirectionInput = TRUE;
  2563. }
  2564. // if there is no redirection
  2565. if ( bIndirectionInput == FALSE )
  2566. {
  2567. // Get the current input mode of the input buffer
  2568. if ( FALSE == GetConsoleMode( hInputConsole, &dwPrevConsoleMode ))
  2569. {
  2570. SaveLastError();
  2571. // could not set the mode, return failure
  2572. return EXIT_FAILURE;
  2573. }
  2574. // Set the mode such that the control keys are processed by the system
  2575. if ( FALSE == SetConsoleMode( hInputConsole, ENABLE_PROCESSED_INPUT ) )
  2576. {
  2577. SaveLastError();
  2578. // could not set the mode, return failure
  2579. return EXIT_FAILURE;
  2580. }
  2581. }
  2582. // redirect the data into the console
  2583. if ( bIndirectionInput == TRUE )
  2584. {
  2585. do {
  2586. //read the contents of file
  2587. if ( ReadFile(hInputConsole, &chAnsi, 1, &dwCharsRead, NULL) == FALSE )
  2588. {
  2589. SaveLastError();
  2590. // could not get the handle so return failure
  2591. return EXIT_FAILURE;
  2592. }
  2593. // check if number of characters read were zero.. or
  2594. // any carriage return pressed..
  2595. if ( dwCharsRead == 0 || chTmp == CARRIAGE_RETURN || chTmp == L'\n' || chTmp == L'\t')
  2596. {
  2597. bNoBreak = FALSE;
  2598. // exit from the loop
  2599. break;
  2600. }
  2601. else
  2602. {
  2603. // convert the ANSI character into UNICODE character
  2604. szAnsiBuf[ 0 ] = chAnsi;
  2605. dwCharsRead = SIZE_OF_ARRAY( szBuffer );
  2606. GetAsUnicodeString2( szAnsiBuf, szBuffer, &dwCharsRead );
  2607. chTmp = szBuffer[ 0 ];
  2608. }
  2609. // write the contents to the console
  2610. if ( FALSE == WriteFile ( GetStdHandle( STD_OUTPUT_HANDLE ), &chTmp, 1, &dwCharsRead, NULL ) )
  2611. {
  2612. SaveLastError();
  2613. // could not get the handle so return failure
  2614. return EXIT_FAILURE;
  2615. }
  2616. // copy the character
  2617. wch = chTmp;
  2618. StringCchPrintf ( szBackup, SIZE_OF_ARRAY(szBackup), L"%c" , wch );
  2619. // increment the index
  2620. dwIndex++;
  2621. } while (TRUE == bNoBreak);
  2622. }
  2623. else
  2624. {
  2625. do {
  2626. // Get the Character and loop accordingly.
  2627. if ( ReadConsole( hInputConsole, &chTmp, 1, &dwCharsRead, NULL ) == FALSE )
  2628. {
  2629. SaveLastError();
  2630. // Set the original console settings
  2631. if ( FALSE == SetConsoleMode( hInputConsole, dwPrevConsoleMode ) )
  2632. {
  2633. SaveLastError();
  2634. }
  2635. // return failure
  2636. return EXIT_FAILURE;
  2637. }
  2638. // check if number of chars read were zero..if so, continue...
  2639. if ( dwCharsRead == 0 )
  2640. {
  2641. continue;
  2642. }
  2643. // check if any carriage return pressed...
  2644. if ( chTmp == CARRIAGE_RETURN )
  2645. {
  2646. bNoBreak = FALSE;
  2647. // exit from the loop
  2648. break;
  2649. }
  2650. wch = chTmp;
  2651. if ( wch != BACK_SPACE )
  2652. {
  2653. StringCchPrintf ( szTmpBuf, SIZE_OF_ARRAY(szTmpBuf), L"%c" , wch );
  2654. StringConcat ( szBackup, szTmpBuf , SIZE_OF_ARRAY(szBackup));
  2655. }
  2656. // Check id back space is hit
  2657. if ( wch == BACK_SPACE )
  2658. {
  2659. if ( dwIndex != 0 )
  2660. {
  2661. //
  2662. // Remove a asterix from the console
  2663. // move the cursor one character back
  2664. StringCchPrintf( szBuffer, SIZE_OF_ARRAY(szBuffer), L"%c" , BACK_SPACE );
  2665. if ( FALSE == WriteConsole( GetStdHandle( STD_OUTPUT_HANDLE ), szBuffer, 1,
  2666. &dwCharsWritten, NULL ) )
  2667. {
  2668. SaveLastError();
  2669. // return failure
  2670. return EXIT_FAILURE;
  2671. }
  2672. // replace the existing character with space
  2673. StringCchPrintf( szBuffer, SIZE_OF_ARRAY(szBuffer), L"%c" , BLANK_CHAR );
  2674. if ( FALSE == WriteConsole( GetStdHandle( STD_OUTPUT_HANDLE ), szBuffer, 1,
  2675. &dwCharsWritten, NULL ))
  2676. {
  2677. SaveLastError();
  2678. // return failure
  2679. return EXIT_FAILURE;
  2680. }
  2681. // now set the cursor at back position
  2682. StringCchPrintf( szBuffer, SIZE_OF_ARRAY(szBuffer), L"%c" , BACK_SPACE );
  2683. if ( FALSE == WriteConsole( GetStdHandle( STD_OUTPUT_HANDLE ), szBuffer, 1,
  2684. &dwCharsWritten, NULL ))
  2685. {
  2686. SaveLastError();
  2687. // return failure
  2688. return EXIT_FAILURE;
  2689. }
  2690. szBackup [StringLength(szBackup, 0) - 1] = L'\0';
  2691. // decrement the index
  2692. dwIndex--;
  2693. }
  2694. // process the next character
  2695. continue;
  2696. }
  2697. // write the contents onto console
  2698. if ( FALSE == WriteFile ( GetStdHandle( STD_OUTPUT_HANDLE ), &wch, 1, &dwCharsRead, NULL ) )
  2699. {
  2700. SaveLastError();
  2701. // return failure
  2702. return EXIT_FAILURE;
  2703. }
  2704. // increment the index value
  2705. dwIndex++;
  2706. } while (TRUE == bNoBreak);
  2707. }
  2708. DISPLAY_MESSAGE(stdout, _T("\n") );
  2709. // check if 'Y' or 'y' is pressed
  2710. if ( ( dwIndex == 1 ) &&
  2711. ( StringCompare ( szBackup, GetResString (IDS_UPPER_YES), TRUE, 0 ) == 0 ) )
  2712. {
  2713. return EXIT_SUCCESS;
  2714. }
  2715. // check if 'N' or 'n' is pressed
  2716. else if ( ( dwIndex == 1 ) &&
  2717. ( StringCompare ( szBackup, GetResString(IDS_UPPER_NO), TRUE, 0 ) == 0 ) )
  2718. {
  2719. *pbCancel = TRUE;
  2720. // display a message as .. operation has been cancelled...
  2721. DISPLAY_MESSAGE ( stdout, GetResString (IDS_OPERATION_CANCELLED ) );
  2722. return EXIT_SUCCESS;
  2723. }
  2724. else
  2725. {
  2726. // display an error message as .. wrong input specified...
  2727. DISPLAY_MESSAGE(stderr, GetResString( IDS_WRONG_INPUT ));
  2728. // Already displayed the ERROR message as above...There is no need to display any
  2729. // success message now.. thats why assigning EXIT_ON_ERROR flag to g_dwRetVal
  2730. return EXIT_FAILURE;
  2731. }
  2732. // return success
  2733. //return EXIT_SUCCESS;
  2734. }