Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2541 lines
69 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(DWORD argc , LPCTSTR argv[])
  31. {
  32. // declarations of structures
  33. TCREATESUBOPTS tcresubops;
  34. TCREATEOPVALS tcreoptvals;
  35. DWORD dwScheduleType = 0;
  36. WORD wUserStatus = FALSE;
  37. //Initialise the structure to neutral values.
  38. lstrcpy(tcresubops.szServer, NULL_STRING);
  39. lstrcpy(tcresubops.szUser, NULL_STRING);
  40. lstrcpy(tcresubops.szPassword, NULL_STRING);
  41. lstrcpy(tcresubops.szRunAsUser, NULL_STRING);
  42. lstrcpy(tcresubops.szRunAsPassword, NULL_STRING);
  43. lstrcpy(tcresubops.szSchedType, NULL_STRING);
  44. lstrcpy(tcresubops.szModifier, NULL_STRING);
  45. lstrcpy(tcresubops.szDays, NULL_STRING);
  46. lstrcpy(tcresubops.szMonths, NULL_STRING);
  47. lstrcpy(tcresubops.szIdleTime, NULL_STRING);
  48. lstrcpy(tcresubops.szTaskName, NULL_STRING);
  49. lstrcpy(tcresubops.szStartTime, NULL_STRING);
  50. lstrcpy(tcresubops.szStartDate, NULL_STRING);
  51. lstrcpy(tcresubops.szEndDate, NULL_STRING);
  52. lstrcpy(tcresubops.szTaskRun, NULL_STRING);
  53. tcresubops.bCreate = FALSE;
  54. tcresubops.bUsage = FALSE;
  55. tcreoptvals.bSetStartDateToCurDate = FALSE;
  56. tcreoptvals.bSetStartTimeToCurTime = FALSE;
  57. tcreoptvals.bPassword = FALSE;
  58. tcreoptvals.bRunAsPassword = FALSE;
  59. // process the options for -create option
  60. if( ProcessCreateOptions ( argc, argv, tcresubops, tcreoptvals, &dwScheduleType, &wUserStatus ) )
  61. {
  62. if(tcresubops.bUsage == TRUE)
  63. {
  64. return S_OK;
  65. }
  66. else
  67. {
  68. return E_FAIL;
  69. }
  70. }
  71. // calls the function to create a scheduled task
  72. return CreateTask(tcresubops,tcreoptvals,dwScheduleType, wUserStatus );
  73. }
  74. /******************************************************************************
  75. Routine Description:
  76. This routine creates a new scheduled task according to the user
  77. specified format
  78. Arguments:
  79. [ in ] tcresubops : Structure containing the task's properties
  80. [ out ] tcreoptvals : Structure containing optional values to set
  81. [ in ] dwScheduleType : Type of schedule[Daily,once,weekly etc]
  82. [ in ] bUserStatus : bUserStatus will be TRUE when -ru given else FALSE
  83. Return Value :
  84. A HRESULT value indicating S_OK on success else S_FALSE on failure
  85. ******************************************************************************/
  86. HRESULT
  87. CreateTask(TCREATESUBOPTS tcresubops, TCREATEOPVALS &tcreoptvals, DWORD dwScheduleType,
  88. WORD wUserStatus)
  89. {
  90. // Declarations related to the system time
  91. WORD wStartDay = 0;
  92. WORD wStartMonth = 0;
  93. WORD wStartYear = 0;
  94. WORD wStartHour = 0;
  95. WORD wStartMin = 0;
  96. WORD wStartSec = 0;
  97. WORD wEndDay = 0;
  98. WORD wEndYear = 0;
  99. WORD wEndMonth = 0;
  100. WORD wIdleTime = 0;
  101. SYSTEMTIME systime = {0,0,0,0,0,0,0,0};
  102. // Declarations related to the new task
  103. WCHAR wszJobName[MAX_TASKNAME_LEN] = NULL_U_STRING;
  104. WCHAR wszTime[MAX_TIMESTR_LEN] = NULL_U_STRING;
  105. WCHAR wszUserName[MAX_STRING_LENGTH] = NULL_U_STRING;
  106. WCHAR wszPassword[MAX_STRING_LENGTH] = NULL_U_STRING;
  107. WCHAR wszCommand[_MAX_FNAME] = NULL_U_STRING;
  108. WCHAR wszApplName[_MAX_FNAME] = NULL_U_STRING;
  109. TCHAR szErrorDesc[MAX_RES_STRING] = NULL_STRING;
  110. TCHAR szRPassword[MAX_USERNAME_LENGTH] = NULL_STRING;
  111. HRESULT hr = S_OK;
  112. IPersistFile *pIPF = NULL;
  113. ITask *pITask = NULL;
  114. ITaskTrigger *pITaskTrig = NULL;
  115. ITaskScheduler *pITaskScheduler = NULL;
  116. WORD wTrigNumber = 0;
  117. TASK_TRIGGER TaskTrig;
  118. ZeroMemory(&TaskTrig, sizeof (TASK_TRIGGER));
  119. TaskTrig.cbTriggerSize = sizeof (TASK_TRIGGER);
  120. TaskTrig.Reserved1 = 0; // reserved field and must be set to 0.
  121. TaskTrig.Reserved2 = 0; // reserved field and must be set to 0.
  122. _TCHAR* szValues[1] = {NULL};//To pass to FormatMessage() API
  123. // Buffer to store the string obtained from the string table
  124. TCHAR szBuffer[MAX_STRING_LENGTH] = NULL_STRING;
  125. BOOL bPassWord = FALSE;
  126. BOOL bUserName = FALSE;
  127. BOOL bRet = FALSE;
  128. BOOL bResult = FALSE;
  129. BOOL bCloseConnection = TRUE;
  130. ULONG ulLong = MAX_RES_STRING;
  131. BOOL bVal = FALSE;
  132. // check whether the taskname contains the characters such
  133. // as '<','>',':','/','\\','|'
  134. bRet = VerifyJobName(tcresubops.szTaskName);
  135. if(bRet == FALSE)
  136. {
  137. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_TASKNAME1));
  138. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_TASKNAME2));
  139. Cleanup(pITaskScheduler);
  140. return E_FAIL;
  141. }
  142. // check for the length of taskname
  143. if( ( lstrlen(tcresubops.szTaskName) > MAX_JOB_LEN ) ||
  144. ( lstrlen(tcresubops.szTaskName) == 0 ) )
  145. {
  146. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_TASKLENGTH));
  147. Cleanup(pITaskScheduler);
  148. return E_FAIL;
  149. }
  150. // check for the length of taskrun
  151. if(( lstrlen(tcresubops.szTaskRun) > MAX_TASK_LEN ) ||
  152. ( lstrlen(tcresubops.szTaskRun) == 0 ) )
  153. {
  154. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_TASKRUN));
  155. Cleanup(pITaskScheduler);
  156. return E_FAIL;
  157. }
  158. // check for the length of username
  159. if( ( lstrlen(tcresubops.szRunAsUser) > MAX_USERNAME_LENGTH ) ||
  160. ( lstrlen(tcresubops.szUser) > MAX_USERNAME_LENGTH ) )
  161. {
  162. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_UNAME ));
  163. Cleanup(pITaskScheduler);
  164. return E_FAIL;
  165. }
  166. // check for the length of password
  167. if( ( lstrlen(tcresubops.szRunAsPassword) > MAX_PASSWORD_LENGTH ) ||
  168. ( lstrlen(tcresubops.szPassword) > MAX_PASSWORD_LENGTH ) )
  169. {
  170. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  171. Cleanup(pITaskScheduler);
  172. return E_FAIL;
  173. }
  174. // check for empty password
  175. if( ( tcreoptvals.bRunAsPassword == TRUE ) && ( lstrlen(tcresubops.szRunAsPassword) == 0 ) &&
  176. ( lstrlen ( tcresubops.szRunAsUser ) != 0 ) &&
  177. ( lstrcmpi(tcresubops.szRunAsUser, NTAUTHORITY_USER ) != 0 ) &&
  178. ( lstrcmpi(tcresubops.szRunAsUser, SYSTEM_USER ) != 0 ) )
  179. {
  180. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  181. }
  182. // Convert the task name specified by the user to wide char or unicode format
  183. if( GetAsUnicodeString(tcresubops.szTaskName,wszJobName,SIZE_OF_ARRAY(wszJobName) ) == NULL )
  184. {
  185. Cleanup(pITaskScheduler);
  186. return E_FAIL;
  187. }
  188. // Convert the start time specified by the user to wide char or unicode format
  189. if(GetAsUnicodeString(tcresubops.szStartTime,wszTime,SIZE_OF_ARRAY(wszTime)) == NULL )
  190. {
  191. Cleanup(pITaskScheduler);
  192. return E_FAIL;
  193. }
  194. // Convert the task to run specified by the user to wide char or unicode format
  195. if( GetAsUnicodeString(tcresubops.szTaskRun,wszCommand,SIZE_OF_ARRAY(wszCommand)) == NULL )
  196. {
  197. Cleanup(pITaskScheduler);
  198. return E_FAIL;
  199. }
  200. // check for the local system
  201. if ( ( IsLocalSystem( tcresubops.szServer ) == TRUE ) &&
  202. ( lstrlen ( tcresubops.szRunAsUser ) != 0 ) &&
  203. ( lstrcmpi(tcresubops.szRunAsUser, NTAUTHORITY_USER ) != 0 ) &&
  204. ( lstrcmpi(tcresubops.szRunAsUser, SYSTEM_USER ) != 0 ) )
  205. {
  206. // Establish the connection on a remote machine
  207. bResult = EstablishConnection(tcresubops.szServer,tcresubops.szUser,SIZE_OF_ARRAY(tcresubops.szUser),tcresubops.szPassword,SIZE_OF_ARRAY(tcresubops.szPassword), !(tcreoptvals.bPassword));
  208. if (bResult == FALSE)
  209. {
  210. DISPLAY_MESSAGE( stderr, GetResString(IDS_ERROR_STRING) );
  211. DISPLAY_MESSAGE( stderr, GetReason());
  212. Cleanup(pITaskScheduler);
  213. return EXIT_FAILURE ;
  214. }
  215. else
  216. {
  217. // though the connection is successfull, some conflict might have occured
  218. switch( GetLastError() )
  219. {
  220. case I_NO_CLOSE_CONNECTION:
  221. bCloseConnection = FALSE;
  222. break;
  223. // check for mismatched credentials
  224. case E_LOCAL_CREDENTIALS:
  225. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  226. {
  227. bCloseConnection = FALSE;
  228. DISPLAY_MESSAGE( stderr, GetResString(IDS_ERROR_STRING) );
  229. DISPLAY_MESSAGE( stderr, GetReason());
  230. Cleanup(pITaskScheduler);
  231. return EXIT_FAILURE;
  232. }
  233. }
  234. }
  235. if ( lstrlen (tcresubops.szRunAsUser) != 0 )
  236. {
  237. // Convert the user name specified by the user to wide char or unicode format
  238. if ( GetAsUnicodeString( tcresubops.szRunAsUser, wszUserName, SIZE_OF_ARRAY(wszUserName)) == NULL )
  239. {
  240. Cleanup(pITaskScheduler);
  241. return E_FAIL;
  242. }
  243. bUserName = TRUE;
  244. if ( tcreoptvals.bRunAsPassword == FALSE )
  245. {
  246. szValues[0] = (_TCHAR*) (wszUserName);
  247. //Display that the task will be created under logged in user name,ask for password
  248. MessageBeep(MB_ICONEXCLAMATION);
  249. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  250. GetResString(IDS_PROMPT_PASSWD),0,MAKELANGID(LANG_NEUTRAL,
  251. SUBLANG_DEFAULT),szBuffer,
  252. MAX_STRING_LENGTH,(va_list*)szValues );
  253. DISPLAY_MESSAGE(stdout,szBuffer);
  254. // getting the password
  255. if (GetPassword(szRPassword, MAX_PASSWORD_LENGTH) == FALSE )
  256. {
  257. Cleanup(pITaskScheduler);
  258. return E_FAIL;
  259. }
  260. // check for empty password
  261. if( lstrlen ( szRPassword ) == 0 )
  262. {
  263. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  264. }
  265. // check for the length of password
  266. if( lstrlen( szRPassword ) > MAX_PASSWORD_LENGTH )
  267. {
  268. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  269. Cleanup(pITaskScheduler);
  270. return E_FAIL;
  271. }
  272. if(lstrlen(szRPassword))
  273. {
  274. // Convert the password specified by the user to wide char or unicode format
  275. if (GetAsUnicodeString(szRPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  276. {
  277. Cleanup(pITaskScheduler);
  278. return E_FAIL;
  279. }
  280. }
  281. bPassWord = TRUE;
  282. }
  283. else
  284. {
  285. // Convert the password specified by the user to wide char or unicode format
  286. if (GetAsUnicodeString(tcresubops.szRunAsPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  287. {
  288. Cleanup(pITaskScheduler);
  289. return E_FAIL;
  290. }
  291. bPassWord = TRUE;
  292. }
  293. }
  294. }
  295. // check whether -s option only specified in the cmd line or not
  296. else if( ( IsLocalSystem( tcresubops.szServer ) == FALSE ) && ( wUserStatus == OI_SERVER ) )
  297. {
  298. // Establish the connection on a remote machine
  299. bResult = EstablishConnection(tcresubops.szServer,tcresubops.szUser,SIZE_OF_ARRAY(tcresubops.szUser),tcresubops.szPassword,SIZE_OF_ARRAY(tcresubops.szPassword), !(tcreoptvals.bPassword));
  300. if (bResult == FALSE)
  301. {
  302. DISPLAY_MESSAGE( stderr, GetResString(IDS_ERROR_STRING) );
  303. DISPLAY_MESSAGE( stderr, GetReason());
  304. Cleanup(pITaskScheduler);
  305. return EXIT_FAILURE ;
  306. }
  307. else
  308. {
  309. // though the connection is successfull, some conflict might have occured
  310. switch( GetLastError() )
  311. {
  312. case I_NO_CLOSE_CONNECTION:
  313. bCloseConnection = FALSE;
  314. break;
  315. // check for mismatched credentials
  316. case E_LOCAL_CREDENTIALS:
  317. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  318. {
  319. bCloseConnection = FALSE;
  320. DISPLAY_MESSAGE( stdout, GetResString(IDS_ERROR_STRING) );
  321. DISPLAY_MESSAGE( stdout, GetReason());
  322. Cleanup(pITaskScheduler);
  323. return EXIT_FAILURE;
  324. }
  325. }
  326. }
  327. if ( ( bCloseConnection == FALSE ) && ( lstrlen (tcresubops.szUser) == 0 ) )
  328. {
  329. //get the current logged on username
  330. if ( GetUserNameEx ( NameSamCompatible, tcresubops.szUser , &ulLong) == FALSE )
  331. {
  332. DISPLAY_MESSAGE( stderr, GetResString( IDS_LOGGED_USER_ERR ) );
  333. Cleanup(pITaskScheduler);
  334. return E_FAIL;
  335. }
  336. bUserName = TRUE;
  337. // Convert the user name specified by the user to wide char or unicode format
  338. if ( GetAsUnicodeString( tcresubops.szUser, wszUserName, SIZE_OF_ARRAY(wszUserName)) == NULL )
  339. {
  340. Cleanup(pITaskScheduler);
  341. return E_FAIL;
  342. }
  343. szValues[0] = (_TCHAR*) (wszUserName);
  344. //Display that the task will be created under logged in user name,ask for password
  345. MessageBeep(MB_ICONEXCLAMATION);
  346. // format the message for currently logged-on user name
  347. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  348. GetResString(IDS_TASK_INFO),0,MAKELANGID(LANG_NEUTRAL,
  349. SUBLANG_DEFAULT),szBuffer,
  350. MAX_STRING_LENGTH,(va_list*)szValues );
  351. DISPLAY_MESSAGE(stdout,szBuffer);
  352. // format the message for getting the password from the console
  353. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  354. GetResString(IDS_PROMPT_PASSWD),0,MAKELANGID(LANG_NEUTRAL,
  355. SUBLANG_DEFAULT),szBuffer,
  356. MAX_STRING_LENGTH,(va_list*)szValues );
  357. DISPLAY_MESSAGE(stdout,szBuffer);
  358. if (GetPassword(tcresubops.szRunAsPassword, MAX_PASSWORD_LENGTH) == FALSE )
  359. {
  360. Cleanup(pITaskScheduler);
  361. return E_FAIL;
  362. }
  363. // check for empty password
  364. if( lstrlen ( tcresubops.szRunAsPassword ) == 0 )
  365. {
  366. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  367. }
  368. // check for the length of password
  369. if( lstrlen( tcresubops.szRunAsPassword ) > MAX_PASSWORD_LENGTH )
  370. {
  371. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  372. Cleanup(pITaskScheduler);
  373. return E_FAIL;
  374. }
  375. // check for the length of the password
  376. if(lstrlen(tcresubops.szRunAsPassword))
  377. {
  378. // Convert the password specified by the user to wide char or unicode format
  379. if (GetAsUnicodeString(tcresubops.szRunAsPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  380. {
  381. Cleanup(pITaskScheduler);
  382. return E_FAIL;
  383. }
  384. }
  385. bPassWord = TRUE;
  386. }
  387. // Convert the user name specified by the user to wide char or unicode format
  388. if( GetAsUnicodeString(tcresubops.szUser,wszUserName,SIZE_OF_ARRAY(wszUserName)) == NULL )
  389. {
  390. Cleanup(pITaskScheduler);
  391. return E_FAIL;
  392. }
  393. // check whether the run as password is specified in the cmdline or not
  394. if ( tcreoptvals.bRunAsPassword == TRUE )
  395. {
  396. // check for -rp "*" or -rp " " to prompt for password
  397. if ( lstrcmpi( tcresubops.szRunAsPassword, ASTERIX ) == 0 )
  398. {
  399. // format the message for getting the password
  400. szValues[0] = (_TCHAR*) (wszUserName);
  401. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  402. GetResString(IDS_PROMPT_PASSWD),0,MAKELANGID(LANG_NEUTRAL,
  403. SUBLANG_DEFAULT),szBuffer,
  404. MAX_STRING_LENGTH,(va_list*)szValues
  405. );
  406. DISPLAY_MESSAGE(stdout,szBuffer);
  407. // Get the run as password from the command line
  408. if ( GetPassword(tcresubops.szRunAsPassword, MAX_PASSWORD_LENGTH ) == FALSE )
  409. {
  410. Cleanup(pITaskScheduler);
  411. return E_FAIL;
  412. }
  413. // check for empty password
  414. if( lstrlen ( tcresubops.szRunAsPassword ) == 0 )
  415. {
  416. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  417. }
  418. // check for the length of the password
  419. if( lstrlen(tcresubops.szRunAsPassword) > MAX_PASSWORD_LENGTH )
  420. {
  421. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  422. Cleanup(pITaskScheduler);
  423. return E_FAIL;
  424. }
  425. // Convert the password specified by the user to wide char or unicode format
  426. if ( GetAsUnicodeString( tcresubops.szRunAsPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  427. {
  428. Cleanup(pITaskScheduler);
  429. return E_FAIL;
  430. }
  431. }
  432. }
  433. else
  434. {
  435. // Convert the password specified by the user to wide char or unicode format
  436. if ( GetAsUnicodeString( tcresubops.szPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  437. {
  438. Cleanup(pITaskScheduler);
  439. return E_FAIL;
  440. }
  441. }
  442. // set the BOOL variables to TRUE
  443. bUserName = TRUE;
  444. bPassWord = TRUE;
  445. }
  446. // check for -s and -u options only specified in the cmd line or not
  447. else if ( wUserStatus == OI_USERNAME )
  448. {
  449. // Establish the connection on a remote machine
  450. bResult = EstablishConnection(tcresubops.szServer,tcresubops.szUser,SIZE_OF_ARRAY(tcresubops.szUser),tcresubops.szPassword,SIZE_OF_ARRAY(tcresubops.szPassword), !(tcreoptvals.bPassword));
  451. if (bResult == FALSE)
  452. {
  453. DISPLAY_MESSAGE( stderr, GetResString(IDS_ERROR_STRING) );
  454. DISPLAY_MESSAGE( stderr, GetReason());
  455. Cleanup(pITaskScheduler);
  456. return EXIT_FAILURE ;
  457. }
  458. else
  459. {
  460. // though the connection is successfull, some conflict might have occured
  461. switch( GetLastError() )
  462. {
  463. case I_NO_CLOSE_CONNECTION:
  464. bCloseConnection = FALSE;
  465. break;
  466. // for mismatched credentials
  467. case E_LOCAL_CREDENTIALS:
  468. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  469. {
  470. bCloseConnection = FALSE;
  471. DISPLAY_MESSAGE( stderr, GetResString(IDS_ERROR_STRING) );
  472. DISPLAY_MESSAGE( stderr, GetReason());
  473. Cleanup(pITaskScheduler);
  474. return EXIT_FAILURE;
  475. }
  476. }
  477. }
  478. // Convert the user name specified by the user to wide char or unicode format
  479. if( GetAsUnicodeString(tcresubops.szUser,wszUserName,SIZE_OF_ARRAY(wszUserName)) == NULL )
  480. {
  481. Cleanup(pITaskScheduler);
  482. return E_FAIL;
  483. }
  484. // check whether run as password is specified in the command line or not
  485. if ( tcreoptvals.bRunAsPassword == TRUE )
  486. {
  487. // check for -rp "*" or -rp " " to prompt for password
  488. if ( lstrcmpi( tcresubops.szRunAsPassword, ASTERIX ) == 0 )
  489. {
  490. // format the message for getting the password from console
  491. szValues[0] = (_TCHAR*) (wszUserName);
  492. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  493. GetResString(IDS_PROMPT_PASSWD),0,MAKELANGID(LANG_NEUTRAL,
  494. SUBLANG_DEFAULT),szBuffer,
  495. MAX_STRING_LENGTH,(va_list*)szValues
  496. );
  497. DISPLAY_MESSAGE(stdout,szBuffer);
  498. // Get the password from the command line
  499. if ( GetPassword(tcresubops.szRunAsPassword, MAX_PASSWORD_LENGTH ) == FALSE )
  500. {
  501. Cleanup(pITaskScheduler);
  502. return E_FAIL;
  503. }
  504. // check for empty password
  505. if( lstrlen ( tcresubops.szRunAsPassword ) == 0 )
  506. {
  507. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  508. }
  509. // check for the length of password
  510. if( lstrlen(tcresubops.szRunAsPassword) > MAX_PASSWORD_LENGTH )
  511. {
  512. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  513. Cleanup(pITaskScheduler);
  514. return E_FAIL;
  515. }
  516. // Convert the password specified by the user to wide char or unicode format
  517. if ( GetAsUnicodeString( tcresubops.szRunAsPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  518. {
  519. Cleanup(pITaskScheduler);
  520. return E_FAIL;
  521. }
  522. }
  523. else
  524. {
  525. // Convert the password specified by the user to wide char or unicode format
  526. if ( GetAsUnicodeString( tcresubops.szRunAsPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  527. {
  528. Cleanup(pITaskScheduler);
  529. return E_FAIL;
  530. }
  531. bPassWord = TRUE;
  532. }
  533. }
  534. else
  535. {
  536. if ( lstrlen(tcresubops.szPassword) != 0 )
  537. {
  538. // Convert the password specified by the user to wide char or unicode format
  539. if ( GetAsUnicodeString( tcresubops.szPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  540. {
  541. Cleanup(pITaskScheduler);
  542. return E_FAIL;
  543. }
  544. }
  545. else
  546. {
  547. // format the message for getting the password from console
  548. szValues[0] = (_TCHAR*) (wszUserName);
  549. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  550. GetResString(IDS_PROMPT_PASSWD),0,MAKELANGID(LANG_NEUTRAL,
  551. SUBLANG_DEFAULT),szBuffer,
  552. MAX_STRING_LENGTH,(va_list*)szValues
  553. );
  554. DISPLAY_MESSAGE(stdout,szBuffer);
  555. // Get the password from the command line
  556. if ( GetPassword(tcresubops.szRunAsPassword, MAX_PASSWORD_LENGTH ) == FALSE )
  557. {
  558. Cleanup(pITaskScheduler);
  559. return E_FAIL;
  560. }
  561. // check for empty password
  562. if( lstrlen ( tcresubops.szRunAsPassword ) == 0 )
  563. {
  564. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  565. }
  566. // check for the length of password
  567. if( lstrlen(tcresubops.szRunAsPassword) > MAX_PASSWORD_LENGTH )
  568. {
  569. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  570. Cleanup(pITaskScheduler);
  571. return E_FAIL;
  572. }
  573. // Convert the password specified by the user to wide char or unicode format
  574. if ( GetAsUnicodeString( tcresubops.szRunAsPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  575. {
  576. Cleanup(pITaskScheduler);
  577. return E_FAIL;
  578. }
  579. }
  580. }
  581. bUserName = TRUE;
  582. bPassWord = TRUE;
  583. }
  584. // check for -s, -ru or -u options specified in the cmd line or not
  585. else if ( ( lstrlen (tcresubops.szServer) != 0 ) && ( wUserStatus == OI_RUNASUSERNAME || wUserStatus == OI_RUNANDUSER ) )
  586. {
  587. // Establish the connection on a remote machine
  588. bResult = EstablishConnection(tcresubops.szServer,tcresubops.szUser,SIZE_OF_ARRAY(tcresubops.szUser),tcresubops.szPassword,SIZE_OF_ARRAY(tcresubops.szPassword), !(tcreoptvals.bPassword));
  589. if (bResult == FALSE)
  590. {
  591. DISPLAY_MESSAGE( stderr, GetResString(IDS_ERROR_STRING) );
  592. DISPLAY_MESSAGE( stderr, GetReason());
  593. Cleanup(pITaskScheduler);
  594. return EXIT_FAILURE ;
  595. }
  596. else
  597. {
  598. // though the connection is successfull, some conflict might have occured
  599. switch( GetLastError() )
  600. {
  601. case I_NO_CLOSE_CONNECTION:
  602. bCloseConnection = FALSE;
  603. break;
  604. case E_LOCAL_CREDENTIALS:
  605. case ERROR_SESSION_CREDENTIAL_CONFLICT:
  606. {
  607. bCloseConnection = FALSE;
  608. DISPLAY_MESSAGE( stderr, GetResString(IDS_ERROR_STRING) );
  609. DISPLAY_MESSAGE( stderr, GetReason());
  610. Cleanup(pITaskScheduler);
  611. return EXIT_FAILURE;
  612. }
  613. }
  614. }
  615. if ( ( ( lstrlen ( tcresubops.szRunAsUser ) == 0 ) ||
  616. ( lstrcmpi( tcresubops.szRunAsUser, NTAUTHORITY_USER ) == 0 ) ||
  617. ( lstrcmpi( tcresubops.szRunAsUser, SYSTEM_USER ) == 0 ) ) )
  618. {
  619. // Convert the run as user name specified by the user to wide char or unicode format
  620. if( GetAsUnicodeString(tcresubops.szRunAsUser,wszUserName,SIZE_OF_ARRAY(wszUserName)) == NULL )
  621. {
  622. Cleanup(pITaskScheduler);
  623. return E_FAIL;
  624. }
  625. szValues[0] = (_TCHAR*) (wszJobName);
  626. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  627. GetResString(IDS_NTAUTH_SYSTEM_INFO),0,MAKELANGID(LANG_NEUTRAL,
  628. SUBLANG_DEFAULT),szBuffer,
  629. MAX_STRING_LENGTH,(va_list*)szValues
  630. );
  631. DISPLAY_MESSAGE(stdout,szBuffer);
  632. if ( ( tcreoptvals.bRunAsPassword == TRUE ) &&
  633. ( lstrlen (tcresubops.szRunAsPassword) != 0 ) )
  634. {
  635. DISPLAY_MESSAGE( stdout, GetResString( IDS_PASSWORD_NOEFFECT ) );
  636. }
  637. bUserName = TRUE;
  638. bPassWord = TRUE;
  639. bVal = TRUE;
  640. }
  641. else
  642. {
  643. // check for the length of password
  644. if ( lstrlen ( tcresubops.szRunAsUser ) != 0 )
  645. {
  646. // Convert the run as user name specified by the user to wide char or unicode format
  647. if( GetAsUnicodeString(tcresubops.szRunAsUser,wszUserName,SIZE_OF_ARRAY(wszUserName)) == NULL )
  648. {
  649. Cleanup(pITaskScheduler);
  650. return E_FAIL;
  651. }
  652. bUserName = TRUE;
  653. }
  654. }
  655. // check whether -u and -ru are the same or not. if they are same, we need to
  656. // prompt for the run as password. otherwise, will consoder -rp as -p
  657. if ( lstrcmpi( tcresubops.szRunAsUser, tcresubops.szUser ) != 0)
  658. {
  659. if ( tcreoptvals.bRunAsPassword == TRUE )
  660. {
  661. if ( (lstrlen(tcresubops.szRunAsUser) != 0) && (lstrcmpi( tcresubops.szRunAsPassword, ASTERIX ) == 0 ) &&
  662. ( lstrcmpi( tcresubops.szRunAsUser, NTAUTHORITY_USER ) != 0 ) &&
  663. ( lstrcmpi( tcresubops.szRunAsUser, SYSTEM_USER ) != 0 ) )
  664. {
  665. szValues[0] = (_TCHAR*) (wszUserName);
  666. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  667. GetResString(IDS_PROMPT_PASSWD),0,MAKELANGID(LANG_NEUTRAL,
  668. SUBLANG_DEFAULT),szBuffer,
  669. MAX_STRING_LENGTH,(va_list*)szValues
  670. );
  671. DISPLAY_MESSAGE(stdout,szBuffer);
  672. // prompt for the run as password
  673. if ( GetPassword(szRPassword, MAX_PASSWORD_LENGTH ) == FALSE )
  674. {
  675. Cleanup(pITaskScheduler);
  676. return E_FAIL;
  677. }
  678. // check for empty password
  679. if( lstrlen ( szRPassword ) == 0 )
  680. {
  681. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  682. }
  683. // check for the length of password
  684. if( lstrlen(szRPassword) > MAX_PASSWORD_LENGTH )
  685. {
  686. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  687. Cleanup(pITaskScheduler);
  688. return E_FAIL;
  689. }
  690. // Convert the password specified by the user to wide char or unicode format
  691. if ( GetAsUnicodeString( szRPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  692. {
  693. Cleanup(pITaskScheduler);
  694. return E_FAIL;
  695. }
  696. bUserName = TRUE;
  697. bPassWord = TRUE;
  698. }
  699. else
  700. {
  701. // Convert the password specified by the user to wide char or unicode format
  702. if ( GetAsUnicodeString( tcresubops.szRunAsPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  703. {
  704. Cleanup(pITaskScheduler);
  705. return E_FAIL;
  706. }
  707. bUserName = TRUE;
  708. bPassWord = TRUE;
  709. }
  710. }
  711. else
  712. {
  713. // check for the length of password
  714. if ( ( bVal == FALSE ) && ( lstrlen(tcresubops.szRunAsUser) != 0) )
  715. {
  716. // format the message for getting the password from console
  717. szValues[0] = (_TCHAR*) (wszUserName);
  718. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  719. GetResString(IDS_PROMPT_PASSWD),0,MAKELANGID(LANG_NEUTRAL,
  720. SUBLANG_DEFAULT),szBuffer,
  721. MAX_STRING_LENGTH,(va_list*)szValues
  722. );
  723. DISPLAY_MESSAGE(stdout,szBuffer);
  724. // prompt for the run as password
  725. if ( GetPassword(szRPassword, MAX_PASSWORD_LENGTH ) == FALSE )
  726. {
  727. Cleanup(pITaskScheduler);
  728. return E_FAIL;
  729. }
  730. // check for empty password
  731. if( lstrlen ( szRPassword ) == 0 )
  732. {
  733. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  734. }
  735. // check for the length of password
  736. if( lstrlen(szRPassword) > MAX_PASSWORD_LENGTH )
  737. {
  738. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  739. Cleanup(pITaskScheduler);
  740. return E_FAIL;
  741. }
  742. // Convert the password specified by the user to wide char or unicode format
  743. if ( GetAsUnicodeString( szRPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  744. {
  745. Cleanup(pITaskScheduler);
  746. return E_FAIL;
  747. }
  748. }
  749. bUserName = TRUE;
  750. bPassWord = TRUE;
  751. }
  752. }
  753. else
  754. {
  755. // check whether run as password is specified in the cmdline or not
  756. if ( tcreoptvals.bRunAsPassword == TRUE )
  757. {
  758. if ( ( lstrlen ( tcresubops.szRunAsUser ) != 0 ) && ( lstrcmpi( tcresubops.szRunAsPassword, ASTERIX ) == 0 ) )
  759. {
  760. szValues[0] = (_TCHAR*) (wszUserName);
  761. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  762. GetResString(IDS_PROMPT_PASSWD),0,MAKELANGID(LANG_NEUTRAL,
  763. SUBLANG_DEFAULT),szBuffer,
  764. MAX_STRING_LENGTH,(va_list*)szValues
  765. );
  766. DISPLAY_MESSAGE(stdout,szBuffer);
  767. // prompt for the run as password
  768. if ( GetPassword(szRPassword, MAX_PASSWORD_LENGTH ) == FALSE )
  769. {
  770. Cleanup(pITaskScheduler);
  771. return E_FAIL;
  772. }
  773. // check for empty password
  774. if( lstrlen ( szRPassword ) == 0 )
  775. {
  776. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  777. }
  778. // check for the length of password
  779. if( lstrlen(szRPassword) > MAX_PASSWORD_LENGTH )
  780. {
  781. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  782. Cleanup(pITaskScheduler);
  783. return E_FAIL;
  784. }
  785. // Convert the password specified by the user to wide char or unicode format
  786. if ( GetAsUnicodeString( szRPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  787. {
  788. Cleanup(pITaskScheduler);
  789. return E_FAIL;
  790. }
  791. }
  792. else
  793. {
  794. // Convert the password specified by the user to wide char or unicode format
  795. if ( GetAsUnicodeString( tcresubops.szRunAsPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  796. {
  797. Cleanup(pITaskScheduler);
  798. return E_FAIL;
  799. }
  800. }
  801. }
  802. else
  803. {
  804. if ( lstrlen (tcresubops.szPassword) )
  805. {
  806. // Convert the password specified by the user to wide char or unicode format
  807. if ( GetAsUnicodeString( tcresubops.szPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  808. {
  809. Cleanup(pITaskScheduler);
  810. return E_FAIL;
  811. }
  812. }
  813. else
  814. {
  815. if (( lstrlen ( tcresubops.szRunAsUser ) != 0 ) &&
  816. ( lstrcmpi(tcresubops.szRunAsUser, NTAUTHORITY_USER ) != 0 ) &&
  817. ( lstrcmpi(tcresubops.szRunAsUser, SYSTEM_USER ) != 0 ) )
  818. {
  819. szValues[0] = (_TCHAR*) (wszUserName);
  820. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  821. GetResString(IDS_PROMPT_PASSWD),0,MAKELANGID(LANG_NEUTRAL,
  822. SUBLANG_DEFAULT),szBuffer,
  823. MAX_STRING_LENGTH,(va_list*)szValues
  824. );
  825. DISPLAY_MESSAGE(stdout,szBuffer);
  826. // prompt for the run as password
  827. if ( GetPassword(szRPassword, MAX_PASSWORD_LENGTH ) == FALSE )
  828. {
  829. Cleanup(pITaskScheduler);
  830. return E_FAIL;
  831. }
  832. // check for empty password
  833. if( lstrlen ( szRPassword ) == 0 )
  834. {
  835. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  836. }
  837. // check for the length of password
  838. if( lstrlen(szRPassword) > MAX_PASSWORD_LENGTH )
  839. {
  840. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  841. Cleanup(pITaskScheduler);
  842. return E_FAIL;
  843. }
  844. // Convert the password specified by the user to wide char or unicode format
  845. if ( GetAsUnicodeString( szRPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  846. {
  847. Cleanup(pITaskScheduler);
  848. return E_FAIL;
  849. }
  850. }
  851. }
  852. }
  853. bUserName = TRUE;
  854. bPassWord = TRUE;
  855. }
  856. }
  857. // To check for the -ru values "", "NT AUTHORITY\SYSTEM", "SYSTEM"
  858. if( ( ( bVal == FALSE ) && ( wUserStatus == OI_RUNASUSERNAME ) && ( lstrlen( tcresubops.szRunAsUser) == 0 ) && ( tcreoptvals.bRunAsPassword == FALSE ) ) ||
  859. ( ( bVal == FALSE ) && ( wUserStatus == OI_RUNASUSERNAME ) && ( lstrlen( tcresubops.szRunAsUser) == 0 ) && ( lstrlen(tcresubops.szRunAsPassword ) == 0 ) ) ||
  860. ( ( bVal == FALSE ) && ( wUserStatus == OI_RUNASUSERNAME ) && ( lstrcmpi(tcresubops.szRunAsUser, NTAUTHORITY_USER ) == 0 ) && ( tcreoptvals.bRunAsPassword == FALSE ) ) ||
  861. ( ( bVal == FALSE ) && ( wUserStatus == OI_RUNASUSERNAME ) && ( lstrcmpi(tcresubops.szRunAsUser, NTAUTHORITY_USER ) == 0 ) && ( lstrlen( tcresubops.szRunAsPassword) == 0 ) ) ||
  862. ( ( bVal == FALSE ) && ( wUserStatus == OI_RUNASUSERNAME ) && ( lstrcmpi(tcresubops.szRunAsUser, SYSTEM_USER ) == 0 ) && ( tcreoptvals.bRunAsPassword == FALSE ) ) ||
  863. ( ( bVal == FALSE ) && ( wUserStatus == OI_RUNASUSERNAME ) && ( lstrcmpi(tcresubops.szRunAsUser, SYSTEM_USER ) == 0 ) && ( lstrlen(tcresubops.szRunAsPassword) == 0 ) ) )
  864. {
  865. //format the message to display the taskname will be created under "NT AUTHORITY\SYSTEM"
  866. szValues[0] = (_TCHAR*) (wszJobName);
  867. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  868. GetResString(IDS_NTAUTH_SYSTEM_INFO),0,MAKELANGID(LANG_NEUTRAL,
  869. SUBLANG_DEFAULT),szBuffer,
  870. MAX_STRING_LENGTH,(va_list*)szValues
  871. );
  872. DISPLAY_MESSAGE(stdout,szBuffer);
  873. bUserName = TRUE;
  874. bPassWord = TRUE;
  875. bVal = TRUE;
  876. }
  877. // check whether the -rp value is given with the -ru "", "NT AUTHORITY\SYSTEM",
  878. // "SYSTEM" or not
  879. else if( ( ( bVal == FALSE ) && ( wUserStatus == OI_RUNASUSERNAME || wUserStatus == OI_RUNANDUSER) && ( lstrlen(tcresubops.szRunAsUser) == 0 ) && ( lstrlen(tcresubops.szRunAsPassword) != 0 ) ) ||
  880. ( ( bVal == FALSE ) && ( wUserStatus == OI_RUNASUSERNAME || wUserStatus == OI_RUNANDUSER) && ( lstrcmpi( tcresubops.szRunAsUser, NTAUTHORITY_USER ) == 0 ) && ( tcreoptvals.bRunAsPassword == TRUE ) ) ||
  881. ( ( bVal == FALSE ) && ( wUserStatus == OI_RUNASUSERNAME || wUserStatus == OI_RUNANDUSER) && ( lstrcmpi( tcresubops.szRunAsUser, SYSTEM_USER ) == 0 ) && ( tcreoptvals.bRunAsPassword == TRUE ) ) )
  882. {
  883. szValues[0] = (_TCHAR*) (wszJobName);
  884. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  885. GetResString(IDS_NTAUTH_SYSTEM_INFO),0,MAKELANGID(LANG_NEUTRAL,
  886. SUBLANG_DEFAULT),szBuffer,
  887. MAX_STRING_LENGTH,(va_list*)szValues
  888. );
  889. DISPLAY_MESSAGE(stdout,szBuffer);
  890. // to display a warning message as password will not effect for the system account
  891. DISPLAY_MESSAGE( stdout, GetResString( IDS_PASSWORD_NOEFFECT ) );
  892. bUserName = TRUE;
  893. bPassWord = TRUE;
  894. bVal = TRUE;
  895. }
  896. // check whether -s, -u, -ru options are given in the cmdline or not
  897. else if( ( wUserStatus != OI_SERVER ) && ( wUserStatus != OI_USERNAME ) &&
  898. ( wUserStatus != OI_RUNASUSERNAME ) && ( wUserStatus != OI_RUNANDUSER ) &&
  899. ( lstrcmpi( tcresubops.szRunAsPassword , NULL_STRING ) == 0 ) )
  900. {
  901. if (tcreoptvals.bRunAsPassword == TRUE)
  902. {
  903. bPassWord = TRUE;
  904. }
  905. else
  906. {
  907. bPassWord = FALSE;
  908. }
  909. }
  910. else if ( ( lstrlen(tcresubops.szServer) == 0 ) && (lstrlen ( tcresubops.szRunAsUser ) != 0 ) )
  911. {
  912. // Convert the run as user name specified by the user to wide char or unicode format
  913. if ( GetAsUnicodeString( tcresubops.szRunAsUser, wszUserName, SIZE_OF_ARRAY(wszUserName)) == NULL )
  914. {
  915. Cleanup(pITaskScheduler);
  916. return E_FAIL;
  917. }
  918. bUserName = TRUE;
  919. if ( lstrlen ( tcresubops.szRunAsPassword ) == 0 )
  920. {
  921. bPassWord = TRUE;
  922. }
  923. else
  924. {
  925. // check whether "*" or NULL value is given for -rp or not
  926. if ( lstrcmpi ( tcresubops.szRunAsPassword , ASTERIX ) == 0 )
  927. {
  928. // format a message for getting the password from the console
  929. szValues[0] = (_TCHAR*) (wszUserName);
  930. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  931. GetResString(IDS_PROMPT_PASSWD),0,MAKELANGID(LANG_NEUTRAL,
  932. SUBLANG_DEFAULT),szBuffer,
  933. MAX_STRING_LENGTH,(va_list*)szValues
  934. );
  935. DISPLAY_MESSAGE(stdout,szBuffer);
  936. // Get the password from the command line
  937. if (GetPassword(szRPassword, MAX_PASSWORD_LENGTH ) == FALSE )
  938. {
  939. Cleanup(pITaskScheduler);
  940. return E_FAIL;
  941. }
  942. // check for empty password
  943. if( lstrlen ( szRPassword ) == 0 )
  944. {
  945. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  946. }
  947. // check for the length of password
  948. if( lstrlen( szRPassword ) > MAX_PASSWORD_LENGTH )
  949. {
  950. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  951. Cleanup(pITaskScheduler);
  952. return E_FAIL;
  953. }
  954. if(lstrlen(szRPassword))
  955. {
  956. // Convert the password specified by the user to wide char or unicode format
  957. if (GetAsUnicodeString(szRPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  958. {
  959. Cleanup(pITaskScheduler);
  960. return E_FAIL;
  961. }
  962. }
  963. }
  964. else
  965. {
  966. // Convert the password specified by the user to wide char or unicode format
  967. if ( GetAsUnicodeString(tcresubops.szRunAsPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  968. {
  969. Cleanup(pITaskScheduler);
  970. return E_FAIL;
  971. }
  972. }
  973. bPassWord = TRUE;
  974. }
  975. }
  976. // check whether -ru or -u values are specified in the cmdline or not
  977. if ( wUserStatus == OI_RUNASUSERNAME || wUserStatus == OI_RUNANDUSER )
  978. {
  979. if( ( bUserName == TRUE ) && ( bPassWord == FALSE ) )
  980. {
  981. szValues[0] = (_TCHAR*) (wszUserName);
  982. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  983. GetResString(IDS_PROMPT_PASSWD),0,MAKELANGID(LANG_NEUTRAL,
  984. SUBLANG_DEFAULT),szBuffer,
  985. MAX_STRING_LENGTH,(va_list*)szValues
  986. );
  987. DISPLAY_MESSAGE(stdout,szBuffer);
  988. // getting the password from the console
  989. if ( GetPassword(tcresubops.szRunAsPassword, MAX_PASSWORD_LENGTH ) == FALSE )
  990. {
  991. Cleanup(pITaskScheduler);
  992. return E_FAIL;
  993. }
  994. // check for empty password
  995. if( lstrlen ( tcresubops.szRunAsPassword ) == 0 )
  996. {
  997. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  998. }
  999. // check for the length of password
  1000. if( lstrlen(tcresubops.szRunAsPassword) > MAX_PASSWORD_LENGTH )
  1001. {
  1002. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  1003. Cleanup(pITaskScheduler);
  1004. return E_FAIL;
  1005. }
  1006. if(lstrlen(tcresubops.szRunAsPassword))
  1007. {
  1008. // Convert the password specified by the user to wide char or unicode format
  1009. if ( GetAsUnicodeString(tcresubops.szRunAsPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  1010. {
  1011. Cleanup(pITaskScheduler);
  1012. return E_FAIL;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. //if the user name is not specifed set the current logged on user settings
  1018. DWORD dwUserLen = MAX_USERNAME_LEN;
  1019. DWORD dwResult = 0;
  1020. _TCHAR szUserName[MAX_RES_STRING];
  1021. #ifdef _WIN64
  1022. INT64 dwPos = 0;
  1023. #else
  1024. DWORD dwPos = 0;
  1025. #endif
  1026. if( ( bUserName == FALSE ) )
  1027. {
  1028. //get the current logged on username
  1029. if ( GetUserNameEx ( NameSamCompatible, szUserName , &ulLong) == FALSE )
  1030. {
  1031. DISPLAY_MESSAGE( stderr, GetResString( IDS_LOGGED_USER_ERR ) );
  1032. Cleanup(pITaskScheduler);
  1033. return E_FAIL;
  1034. }
  1035. // Convert the user name specified by the user to wide char or unicode format
  1036. if ( GetAsUnicodeString( szUserName, wszUserName, SIZE_OF_ARRAY(wszUserName)) == NULL )
  1037. {
  1038. Cleanup(pITaskScheduler);
  1039. return E_FAIL;
  1040. }
  1041. szValues[0] = (_TCHAR*) (wszUserName);
  1042. //Display that the task will be created under logged in user name,ask for password
  1043. MessageBeep(MB_ICONEXCLAMATION);
  1044. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  1045. GetResString(IDS_TASK_INFO),0,MAKELANGID(LANG_NEUTRAL,
  1046. SUBLANG_DEFAULT),szBuffer,
  1047. MAX_STRING_LENGTH,(va_list*)szValues );
  1048. DISPLAY_MESSAGE(stdout,szBuffer);
  1049. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  1050. GetResString(IDS_PROMPT_PASSWD),0,MAKELANGID(LANG_NEUTRAL,
  1051. SUBLANG_DEFAULT),szBuffer,
  1052. MAX_STRING_LENGTH,(va_list*)szValues );
  1053. DISPLAY_MESSAGE(stdout,szBuffer);
  1054. // getting the password
  1055. if (GetPassword(szRPassword, MAX_PASSWORD_LENGTH) == FALSE )
  1056. {
  1057. Cleanup(pITaskScheduler);
  1058. return E_FAIL;
  1059. }
  1060. // check for empty password
  1061. if( lstrlen ( szRPassword ) == 0 )
  1062. {
  1063. DISPLAY_MESSAGE(stdout, GetResString(IDS_WARN_EMPTY_PASSWORD));
  1064. }
  1065. // check for the length of password
  1066. if( lstrlen( szRPassword ) > MAX_PASSWORD_LENGTH )
  1067. {
  1068. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_PASSWORD));
  1069. Cleanup(pITaskScheduler);
  1070. return E_FAIL;
  1071. }
  1072. if(lstrlen(szRPassword))
  1073. {
  1074. // Convert the password specified by the user to wide char or unicode format
  1075. if (GetAsUnicodeString(szRPassword,wszPassword,SIZE_OF_ARRAY(wszPassword)) == NULL )
  1076. {
  1077. Cleanup(pITaskScheduler);
  1078. return E_FAIL;
  1079. }
  1080. }
  1081. }
  1082. // Get the task Scheduler object for the machine.
  1083. pITaskScheduler = GetTaskScheduler( tcresubops.szServer );
  1084. // If the Task Scheduler is not defined then give the error message.
  1085. if ( pITaskScheduler == NULL )
  1086. {
  1087. // close the connection that was established by the utility
  1088. if ( bCloseConnection == TRUE )
  1089. CloseConnection( tcresubops.szServer );
  1090. Cleanup(pITaskScheduler);
  1091. return E_FAIL;
  1092. }
  1093. // create a work item wszJobName
  1094. hr = pITaskScheduler->NewWorkItem(wszJobName,CLSID_CTask,IID_ITask,
  1095. (IUnknown**)&pITask);
  1096. if(hr == HRESULT_FROM_WIN32 (ERROR_FILE_EXISTS))
  1097. {
  1098. DISPLAY_MESSAGE(stderr,GetResString(IDS_CREATE_TASK_EXISTS));
  1099. // close the connection that was established by the utility
  1100. if ( bCloseConnection == TRUE )
  1101. CloseConnection( tcresubops.szServer );
  1102. Cleanup(pITaskScheduler);
  1103. return hr;
  1104. }
  1105. if (FAILED(hr))
  1106. {
  1107. DisplayErrorMsg(hr);
  1108. // close the connection that was established by the utility
  1109. if ( bCloseConnection == TRUE )
  1110. CloseConnection( tcresubops.szServer );
  1111. Cleanup(pITaskScheduler);
  1112. return hr;
  1113. }
  1114. // Return a pointer to a specified interface on an object
  1115. hr = pITask->QueryInterface(IID_IPersistFile, (void **) &pIPF);
  1116. if (FAILED(hr))
  1117. {
  1118. DisplayErrorMsg(hr);
  1119. if( pIPF )
  1120. {
  1121. pIPF->Release();
  1122. }
  1123. if( pITask )
  1124. {
  1125. pITask->Release();
  1126. }
  1127. // close the connection that was established by the utility
  1128. if ( bCloseConnection == TRUE )
  1129. CloseConnection( tcresubops.szServer );
  1130. Cleanup(pITaskScheduler);
  1131. return hr;
  1132. }
  1133. // declaration for parameter arguments
  1134. wchar_t wcszParam[MAX_RES_STRING] = L"";
  1135. DWORD dwProcessCode = 0 ;
  1136. dwProcessCode = ProcessFilePath(wszCommand,wszApplName,wcszParam);
  1137. if(dwProcessCode == RETVAL_FAIL)
  1138. {
  1139. if( pIPF )
  1140. {
  1141. pIPF->Release();
  1142. }
  1143. if( pITask )
  1144. {
  1145. pITask->Release();
  1146. }
  1147. // close the connection that was established by the utility
  1148. if ( bCloseConnection == TRUE )
  1149. CloseConnection( tcresubops.szServer );
  1150. Cleanup(pITaskScheduler);
  1151. return hr;
  1152. }
  1153. // check for .exe substring string in the given task to run string
  1154. // Set command name with ITask::SetApplicationName
  1155. hr = pITask->SetApplicationName(wszApplName);
  1156. if (FAILED(hr))
  1157. {
  1158. DisplayErrorMsg(hr);
  1159. if( pIPF )
  1160. {
  1161. pIPF->Release();
  1162. }
  1163. if( pITask )
  1164. {
  1165. pITask->Release();
  1166. }
  1167. // close the connection that was established by the utility
  1168. if ( bCloseConnection == TRUE )
  1169. CloseConnection( tcresubops.szServer );
  1170. Cleanup(pITaskScheduler);
  1171. return hr;
  1172. }
  1173. //[Working directory = exe pathname - exe name]
  1174. wchar_t* wcszStartIn = wcsrchr(wszApplName,_T('\\'));
  1175. if(wcszStartIn != NULL)
  1176. *( wcszStartIn ) = _T('\0');
  1177. // set the command working directory
  1178. hr = pITask->SetWorkingDirectory(wszApplName);
  1179. if (FAILED(hr))
  1180. {
  1181. DisplayErrorMsg(hr);
  1182. if( pIPF )
  1183. {
  1184. pIPF->Release();
  1185. }
  1186. if( pITask )
  1187. {
  1188. pITask->Release();
  1189. }
  1190. // close the connection that was established by the utility
  1191. if ( bCloseConnection == TRUE )
  1192. CloseConnection( tcresubops.szServer );
  1193. Cleanup(pITaskScheduler);
  1194. return hr;
  1195. }
  1196. // set the command line parameters for the task
  1197. hr = pITask->SetParameters(wcszParam);
  1198. if (FAILED(hr))
  1199. {
  1200. DisplayErrorMsg(hr);
  1201. if( pIPF )
  1202. {
  1203. pIPF->Release();
  1204. }
  1205. if( pITask )
  1206. {
  1207. pITask->Release();
  1208. }
  1209. // close the connection that was established by the utility
  1210. if ( bCloseConnection == TRUE )
  1211. CloseConnection( tcresubops.szServer );
  1212. Cleanup(pITaskScheduler);
  1213. return hr;
  1214. }
  1215. // set the flag TASK_FLAG_DELETE_WHEN_DONE
  1216. DWORD dwTaskFlags = TASK_FLAG_DELETE_WHEN_DONE;
  1217. hr = pITask->SetFlags(dwTaskFlags);
  1218. if (FAILED(hr))
  1219. {
  1220. DisplayErrorMsg(hr);
  1221. if( pIPF )
  1222. {
  1223. pIPF->Release();
  1224. }
  1225. if( pITask )
  1226. {
  1227. pITask->Release();
  1228. }
  1229. // close the connection that was established by the utility
  1230. if ( bCloseConnection == TRUE )
  1231. CloseConnection( tcresubops.szServer );
  1232. Cleanup(pITaskScheduler);
  1233. return hr;
  1234. }
  1235. if ( bVal == TRUE )
  1236. {
  1237. // Set account information for "NT AUTHORITY\SYSTEM" user
  1238. hr = pITask->SetAccountInformation(L"",NULL);
  1239. }
  1240. else
  1241. {
  1242. // set the account information with the user name and password
  1243. hr = pITask->SetAccountInformation(wszUserName,wszPassword);
  1244. }
  1245. if ((FAILED(hr)) && (hr != SCHED_E_NO_SECURITY_SERVICES))
  1246. {
  1247. DisplayErrorMsg(hr);
  1248. DISPLAY_MESSAGE ( stdout, _T("\n") );
  1249. DISPLAY_MESSAGE ( stdout, GetResString( IDS_ACCNAME_ERR ) );
  1250. if( pIPF )
  1251. {
  1252. pIPF->Release();
  1253. }
  1254. if( pITask )
  1255. {
  1256. pITask->Release();
  1257. }
  1258. // close the connection that was established by the utility
  1259. if ( bCloseConnection == TRUE )
  1260. CloseConnection( tcresubops.szServer );
  1261. Cleanup(pITaskScheduler);
  1262. return hr;
  1263. }
  1264. //Assign start date
  1265. if(tcreoptvals.bSetStartDateToCurDate )
  1266. {
  1267. GetLocalTime(&systime);
  1268. wStartDay = systime.wDay;
  1269. wStartMonth = systime.wMonth;
  1270. wStartYear = systime.wYear;
  1271. }
  1272. else if(lstrlen(tcresubops.szStartDate) > 0)
  1273. {
  1274. GetDateFieldEntities(tcresubops.szStartDate, &wStartDay, &wStartMonth, &wStartYear);
  1275. }
  1276. //Assign start time
  1277. if(tcreoptvals.bSetStartTimeToCurTime && (dwScheduleType != SCHED_TYPE_ONIDLE) )
  1278. {
  1279. GetLocalTime(&systime);
  1280. wStartHour = systime.wHour;
  1281. wStartMin = systime.wMinute;
  1282. wStartSec = systime.wSecond;
  1283. }
  1284. else if(lstrlen(tcresubops.szStartTime) > 0)
  1285. {
  1286. GetTimeFieldEntities(tcresubops.szStartTime, &wStartHour, &wStartMin, &wStartSec);
  1287. }
  1288. //Set the falgs specific to ONIDLE
  1289. if(dwScheduleType == SCHED_TYPE_ONIDLE)
  1290. {
  1291. pITask->SetFlags(TASK_FLAG_START_ONLY_IF_IDLE);
  1292. wIdleTime = (WORD)AsLong(tcresubops.szIdleTime, BASE_TEN);
  1293. pITask->SetIdleWait(wIdleTime, 0);
  1294. }
  1295. //create trigger for the corresponding task
  1296. hr = pITask->CreateTrigger(&wTrigNumber, &pITaskTrig);
  1297. if (FAILED(hr))
  1298. {
  1299. DisplayErrorMsg(hr);
  1300. if( pIPF )
  1301. {
  1302. pIPF->Release();
  1303. }
  1304. if( pITaskTrig )
  1305. {
  1306. pITaskTrig->Release();
  1307. }
  1308. if( pITask )
  1309. {
  1310. pITask->Release();
  1311. }
  1312. // close the connection that was established by the utility
  1313. if ( bCloseConnection == TRUE )
  1314. CloseConnection( tcresubops.szServer );
  1315. Cleanup(pITaskScheduler);
  1316. return hr;
  1317. }
  1318. DWORD dwTrigFlags = 0;
  1319. WORD wWhichWeek;
  1320. LONG lMonthlyModifier = 0;
  1321. DWORD dwDays = 1;
  1322. switch( dwScheduleType )
  1323. {
  1324. case SCHED_TYPE_MINUTE:
  1325. TaskTrig.TriggerType = TASK_TIME_TRIGGER_DAILY;
  1326. TaskTrig.Type.Daily.DaysInterval = 1;
  1327. TaskTrig.MinutesInterval = AsLong(tcresubops.szModifier, BASE_TEN);
  1328. TaskTrig.MinutesDuration = (WORD)(HOURS_PER_DAY*MINUTES_PER_HOUR);
  1329. TaskTrig.wStartHour = wStartHour;
  1330. TaskTrig.wStartMinute = wStartMin;
  1331. TaskTrig.wBeginDay = wStartDay;
  1332. TaskTrig.wBeginMonth = wStartMonth;
  1333. TaskTrig.wBeginYear = wStartYear;
  1334. if(lstrlen(tcresubops.szEndDate) > 0)
  1335. {
  1336. // Make end date valid; otherwise the enddate parameter is ignored.
  1337. TaskTrig.rgFlags = TASK_TRIGGER_FLAG_HAS_END_DATE;
  1338. // Now set the end date entities.
  1339. GetDateFieldEntities(tcresubops.szEndDate, &wEndDay, &wEndMonth, &wEndYear);
  1340. TaskTrig.wEndDay = wEndDay;
  1341. TaskTrig.wEndMonth = wEndMonth;
  1342. TaskTrig.wEndYear = wEndYear;
  1343. }
  1344. break;
  1345. case SCHED_TYPE_HOURLY:
  1346. TaskTrig.TriggerType = TASK_TIME_TRIGGER_DAILY;
  1347. TaskTrig.Type.Daily.DaysInterval = 1;
  1348. TaskTrig.MinutesInterval = (WORD)(AsLong(tcresubops.szModifier, BASE_TEN)
  1349. * MINUTES_PER_HOUR);
  1350. TaskTrig.MinutesDuration = (WORD)(HOURS_PER_DAY*MINUTES_PER_HOUR);
  1351. TaskTrig.wStartHour = wStartHour;
  1352. TaskTrig.wStartMinute = wStartMin;
  1353. TaskTrig.wBeginDay = wStartDay;
  1354. TaskTrig.wBeginMonth = wStartMonth;
  1355. TaskTrig.wBeginYear = wStartYear;
  1356. // Now set end date parameters, if the enddate is specified.
  1357. if(lstrlen(tcresubops.szEndDate) > 0)
  1358. {
  1359. // Make end date valid; otherwise the enddate parameter is ignored.
  1360. TaskTrig.rgFlags = TASK_TRIGGER_FLAG_HAS_END_DATE;
  1361. // Now set the end date entities.
  1362. GetDateFieldEntities(tcresubops.szEndDate, &wEndDay, &wEndMonth, &wEndYear);
  1363. TaskTrig.wEndDay = wEndDay;
  1364. TaskTrig.wEndMonth = wEndMonth;
  1365. TaskTrig.wEndYear = wEndYear;
  1366. }
  1367. break;
  1368. // Schedule type is Daily
  1369. case SCHED_TYPE_DAILY:
  1370. TaskTrig.TriggerType = TASK_TIME_TRIGGER_DAILY;
  1371. TaskTrig.wBeginDay = wStartDay;
  1372. TaskTrig.wBeginMonth = wStartMonth;
  1373. TaskTrig.wBeginYear = wStartYear;
  1374. TaskTrig.wStartHour = wStartHour;
  1375. TaskTrig.wStartMinute = wStartMin;
  1376. if( lstrlen(tcresubops.szModifier) > 0 )
  1377. {
  1378. // Set the duration between days to the modifier value specified, if the modifier is specified.
  1379. TaskTrig.Type.Daily.DaysInterval = (WORD) AsLong(tcresubops.szModifier,
  1380. BASE_TEN);
  1381. }
  1382. else
  1383. {
  1384. // Set value for on which day of the week?
  1385. TaskTrig.Type.Weekly.rgfDaysOfTheWeek = GetTaskTrigwDayForDay(tcresubops.szDays);
  1386. TaskTrig.Type.Weekly.WeeksInterval = 1;
  1387. }
  1388. // Now set end date parameters, if the enddate is specified.
  1389. if(lstrlen(tcresubops.szEndDate) > 0)
  1390. {
  1391. // Make end date valid; otherwise the enddate parameter is ignored.
  1392. TaskTrig.rgFlags = TASK_TRIGGER_FLAG_HAS_END_DATE;
  1393. // Now set the end date entities.
  1394. GetDateFieldEntities(tcresubops.szEndDate, &wEndDay, &wEndMonth, &wEndYear);
  1395. TaskTrig.wEndDay = wEndDay;
  1396. TaskTrig.wEndMonth = wEndMonth;
  1397. TaskTrig.wEndYear = wEndYear;
  1398. }
  1399. // No more settings for a Daily type scheduled item.
  1400. break;
  1401. // Schedule type is Weekly
  1402. case SCHED_TYPE_WEEKLY:
  1403. TaskTrig.TriggerType = TASK_TIME_TRIGGER_WEEKLY;
  1404. TaskTrig.Type.Weekly.WeeksInterval = (WORD)AsLong(tcresubops.szModifier, BASE_TEN);
  1405. // Set value for on which day of the week?
  1406. TaskTrig.Type.Weekly.rgfDaysOfTheWeek = GetTaskTrigwDayForDay(tcresubops.szDays);
  1407. TaskTrig.wStartHour = wStartHour;
  1408. TaskTrig.wStartMinute = wStartMin;
  1409. TaskTrig.wBeginDay = wStartDay;
  1410. TaskTrig.wBeginMonth = wStartMonth;
  1411. TaskTrig.wBeginYear = wStartYear;
  1412. // Now set end date parameters, if the enddate is specified.
  1413. if(lstrlen(tcresubops.szEndDate) > 0)
  1414. {
  1415. // Make end date valid; otherwise the enddate parameter is ignored.
  1416. TaskTrig.rgFlags = TASK_TRIGGER_FLAG_HAS_END_DATE;
  1417. // Now set the end date entities.
  1418. GetDateFieldEntities(tcresubops.szEndDate, &wEndDay, &wEndMonth, &wEndYear);
  1419. TaskTrig.wEndDay = wEndDay;
  1420. TaskTrig.wEndMonth = wEndMonth;
  1421. TaskTrig.wEndYear = wEndYear;
  1422. }
  1423. break;
  1424. // Schedule type is Monthly
  1425. case SCHED_TYPE_MONTHLY:
  1426. TaskTrig.wStartHour = wStartHour;
  1427. TaskTrig.wStartMinute = wStartMin;
  1428. TaskTrig.wBeginDay = wStartDay;
  1429. TaskTrig.wBeginMonth = wStartMonth;
  1430. TaskTrig.wBeginYear = wStartYear;
  1431. // Now set end date parameters, if the enddate is specified.
  1432. if(lstrlen(tcresubops.szEndDate) > 0)
  1433. {
  1434. // Make end date valid; otherwise the enddate parameter is ignored.
  1435. TaskTrig.rgFlags = TASK_TRIGGER_FLAG_HAS_END_DATE;
  1436. // Set the end date entities.
  1437. GetDateFieldEntities(tcresubops.szEndDate, &wEndDay, &wEndMonth, &wEndYear);
  1438. TaskTrig.wEndDay = wEndDay;
  1439. TaskTrig.wEndMonth = wEndMonth;
  1440. TaskTrig.wEndYear = wEndYear;
  1441. }
  1442. //Find out from modifier which option like 1 - 12 days
  1443. //or FIRST,SECOND ,THIRD ,.... LAST.
  1444. if(lstrlen(tcresubops.szModifier) > 0)
  1445. {
  1446. lMonthlyModifier = AsLong(tcresubops.szModifier, BASE_TEN);
  1447. if(lMonthlyModifier >= 1 && lMonthlyModifier <= 12)
  1448. {
  1449. if(lstrlen(tcresubops.szDays) == 0 )
  1450. {
  1451. dwDays = 1;//default value for days
  1452. }
  1453. else
  1454. {
  1455. dwDays = (WORD)AsLong(tcresubops.szDays, BASE_TEN);
  1456. }
  1457. TaskTrig.TriggerType = TASK_TIME_TRIGGER_MONTHLYDATE;
  1458. //set the appropriate day bit in rgfDays
  1459. TaskTrig.Type.MonthlyDate.rgfDays = (1 << (dwDays -1)) ;
  1460. TaskTrig.Type.MonthlyDate.rgfMonths = GetMonthId(lMonthlyModifier);
  1461. }
  1462. else
  1463. {
  1464. if( lstrcmpi( tcresubops.szModifier , GetResString( IDS_DAY_MODIFIER_LASTDAY ) ) == 0)
  1465. {
  1466. TaskTrig.TriggerType = TASK_TIME_TRIGGER_MONTHLYDATE;
  1467. //set the appropriate day bit in rgfDays
  1468. TaskTrig.Type.MonthlyDate.rgfDays =
  1469. (1 << (GetNumDaysInaMonth(tcresubops.szMonths, wStartYear ) -1));
  1470. TaskTrig.Type.MonthlyDate.rgfMonths = GetTaskTrigwMonthForMonth(
  1471. tcresubops.szMonths);
  1472. break;
  1473. }
  1474. if( lstrcmpi(tcresubops.szModifier,
  1475. GetResString( IDS_TASK_FIRSTWEEK ) ) == 0 )
  1476. {
  1477. wWhichWeek = TASK_FIRST_WEEK;
  1478. }
  1479. else if( lstrcmpi(tcresubops.szModifier,
  1480. GetResString( IDS_TASK_SECONDWEEK )) == 0 )
  1481. {
  1482. wWhichWeek = TASK_SECOND_WEEK;
  1483. }
  1484. else if( lstrcmpi(tcresubops.szModifier,
  1485. GetResString( IDS_TASK_THIRDWEEK )) == 0 )
  1486. {
  1487. wWhichWeek = TASK_THIRD_WEEK;
  1488. }
  1489. else if( lstrcmpi(tcresubops.szModifier,
  1490. GetResString( IDS_TASK_FOURTHWEEK )) == 0 )
  1491. {
  1492. wWhichWeek = TASK_FOURTH_WEEK;
  1493. }
  1494. else if( lstrcmpi(tcresubops.szModifier,
  1495. GetResString( IDS_TASK_LASTWEEK )) == 0 )
  1496. {
  1497. wWhichWeek = TASK_LAST_WEEK;
  1498. }
  1499. TaskTrig.TriggerType = TASK_TIME_TRIGGER_MONTHLYDOW;
  1500. TaskTrig.Type.MonthlyDOW.wWhichWeek = wWhichWeek;
  1501. TaskTrig.Type.MonthlyDOW.rgfDaysOfTheWeek = GetTaskTrigwDayForDay(
  1502. tcresubops.szDays);
  1503. TaskTrig.Type.MonthlyDOW.rgfMonths = GetTaskTrigwMonthForMonth(
  1504. tcresubops.szMonths);
  1505. }
  1506. }
  1507. else
  1508. {
  1509. TaskTrig.TriggerType = TASK_TIME_TRIGGER_MONTHLYDATE;
  1510. TaskTrig.Type.MonthlyDate.rgfMonths = GetTaskTrigwMonthForMonth(
  1511. tcresubops.szMonths);
  1512. dwDays = (WORD)AsLong(tcresubops.szDays, BASE_TEN);
  1513. if(dwDays > 1)
  1514. {
  1515. //set the appropriate day bit in rgfDays
  1516. TaskTrig.Type.MonthlyDate.rgfDays = (1 << (dwDays -1));
  1517. }
  1518. else
  1519. {
  1520. TaskTrig.Type.MonthlyDate.rgfDays = 1;
  1521. }
  1522. }
  1523. break;
  1524. // Schedule type is Onetime
  1525. case SCHED_TYPE_ONETIME:
  1526. TaskTrig.TriggerType = TASK_TIME_TRIGGER_ONCE;
  1527. TaskTrig.wStartHour = wStartHour;
  1528. TaskTrig.wStartMinute = wStartMin;
  1529. TaskTrig.wBeginDay = wStartDay;
  1530. TaskTrig.wBeginMonth = wStartMonth;
  1531. TaskTrig.wBeginYear = wStartYear;
  1532. break;
  1533. // Schedule type is Onlogon
  1534. case SCHED_TYPE_ONSTART:
  1535. case SCHED_TYPE_ONLOGON:
  1536. if(dwScheduleType == SCHED_TYPE_ONLOGON )
  1537. TaskTrig.TriggerType = TASK_EVENT_TRIGGER_AT_LOGON;
  1538. if(dwScheduleType == SCHED_TYPE_ONSTART )
  1539. TaskTrig.TriggerType = TASK_EVENT_TRIGGER_AT_SYSTEMSTART;
  1540. TaskTrig.wBeginDay = wStartDay;
  1541. TaskTrig.wBeginMonth = wStartMonth;
  1542. TaskTrig.wBeginYear = wStartYear;
  1543. break;
  1544. // Schedule type is Onidle
  1545. case SCHED_TYPE_ONIDLE:
  1546. TaskTrig.TriggerType = TASK_EVENT_TRIGGER_ON_IDLE;
  1547. TaskTrig.wBeginDay = wStartDay;
  1548. TaskTrig.wBeginMonth = wStartMonth;
  1549. TaskTrig.wBeginYear = wStartYear;
  1550. break;
  1551. default:
  1552. // close the connection that was established by the utility
  1553. if ( bCloseConnection == TRUE )
  1554. CloseConnection( tcresubops.szServer );
  1555. Cleanup(pITaskScheduler);
  1556. return E_FAIL;
  1557. }
  1558. szValues[0] = (_TCHAR*) (tcresubops.szTaskName);
  1559. // set the task trigger
  1560. hr = pITaskTrig->SetTrigger(&TaskTrig);
  1561. if (hr != S_OK)
  1562. {
  1563. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  1564. GetResString(IDS_CREATEFAIL_INVALIDARGS),0,MAKELANGID(LANG_NEUTRAL,
  1565. SUBLANG_DEFAULT),szBuffer,
  1566. MAX_STRING_LENGTH,(va_list*)szValues
  1567. );
  1568. DISPLAY_MESSAGE(stderr, szBuffer);
  1569. if( pIPF )
  1570. {
  1571. pIPF->Release();
  1572. }
  1573. if( pITaskTrig )
  1574. {
  1575. pITaskTrig->Release();
  1576. }
  1577. if( pITask )
  1578. {
  1579. pITask->Release();
  1580. }
  1581. // close the connection that was established by the utility
  1582. if ( bCloseConnection == TRUE )
  1583. CloseConnection( tcresubops.szServer );
  1584. Cleanup(pITaskScheduler);
  1585. return hr;
  1586. }
  1587. // save the copy of an object
  1588. hr = pIPF->Save(NULL,TRUE);
  1589. if( FAILED(hr) )
  1590. {
  1591. szValues[0] = (_TCHAR*) (tcresubops.szTaskName);
  1592. if ( hr == SCHEDULER_NOT_RUNNING_ERROR_CODE )
  1593. {
  1594. // displays the warning message for an invalid username
  1595. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  1596. GetResString(IDS_SCHEDULER_NOT_RUNNING),0,MAKELANGID(LANG_NEUTRAL,
  1597. SUBLANG_DEFAULT),szBuffer,
  1598. MAX_STRING_LENGTH,(va_list*)szValues
  1599. );
  1600. }
  1601. else if ( hr == RPC_SERVER_NOT_AVAILABLE )
  1602. {
  1603. szValues[1] = (_TCHAR*) (tcresubops.szServer);
  1604. // displays the warning message for an invalid username
  1605. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  1606. GetResString(IDS_RPC_SERVER_NOT_AVAIL),0,MAKELANGID(LANG_NEUTRAL,
  1607. SUBLANG_DEFAULT),szBuffer,
  1608. MAX_STRING_LENGTH,(va_list*)szValues
  1609. );
  1610. }
  1611. else
  1612. {
  1613. // displays the warning message for an invalid username
  1614. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  1615. GetResString(IDS_INVALID_USER),0,MAKELANGID(LANG_NEUTRAL,
  1616. SUBLANG_DEFAULT),szBuffer,
  1617. MAX_STRING_LENGTH,(va_list*)szValues
  1618. );
  1619. }
  1620. DISPLAY_MESSAGE(stdout, szBuffer);
  1621. if(pIPF)
  1622. {
  1623. pIPF->Release();
  1624. }
  1625. if(pITaskTrig)
  1626. {
  1627. pITaskTrig->Release();
  1628. }
  1629. if(pITask)
  1630. {
  1631. pITask->Release();
  1632. }
  1633. // close the connection that was established by the utility
  1634. if ( bCloseConnection == TRUE )
  1635. CloseConnection( tcresubops.szServer );
  1636. Cleanup(pITaskScheduler);
  1637. return EXIT_SUCCESS;
  1638. }
  1639. //displays success message for valid user
  1640. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  1641. GetResString(IDS_CREATE_SUCCESSFUL),0,MAKELANGID(LANG_NEUTRAL,
  1642. SUBLANG_DEFAULT),szBuffer,
  1643. MAX_STRING_LENGTH,(va_list*)szValues
  1644. );
  1645. DISPLAY_MESSAGE(stdout,szBuffer);
  1646. // Release interface pointers
  1647. if(pIPF)
  1648. {
  1649. pIPF->Release();
  1650. }
  1651. if(pITask)
  1652. {
  1653. pITask->Release();
  1654. }
  1655. if(pITaskTrig)
  1656. {
  1657. pITaskTrig->Release();
  1658. }
  1659. // close the connection that was established by the utility
  1660. if ( bCloseConnection == TRUE )
  1661. CloseConnection( tcresubops.szServer );
  1662. Cleanup(pITaskScheduler);
  1663. return hr;
  1664. }
  1665. /******************************************************************************
  1666. Routine Description:
  1667. This routine displays the create option usage
  1668. Arguments:
  1669. None
  1670. Return Value :
  1671. None
  1672. ******************************************************************************/
  1673. DWORD
  1674. DisplayCreateUsage()
  1675. {
  1676. TCHAR szBuffer[MAX_STRING_LENGTH] = NULL_STRING;
  1677. _TCHAR* szValues[1] = {NULL};//To pass to FormatMessage() API
  1678. TCHAR szFormat[MAX_DATE_STR_LEN] = NULL_STRING;
  1679. WORD wFormatID = 0;
  1680. if ( GetDateFormatString( szFormat) )
  1681. {
  1682. return RETVAL_FAIL;
  1683. }
  1684. szValues[0] = (_TCHAR*) (szFormat);
  1685. // Displaying Create usage
  1686. for( DWORD dw = IDS_CREATE_HLP1; dw <= IDS_CREATE_HLP96; dw++ )
  1687. {
  1688. if ( dw == IDS_CREATE_HLP63)
  1689. {
  1690. // To format start date
  1691. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  1692. GetResString(IDS_CREATE_HLP63),0,MAKELANGID(LANG_NEUTRAL,
  1693. SUBLANG_DEFAULT),szBuffer,
  1694. MAX_STRING_LENGTH,(va_list*)szValues );
  1695. DISPLAY_MESSAGE(stdout,szBuffer);
  1696. }
  1697. else if ( dw == IDS_CREATE_HLP66 )
  1698. {
  1699. // To format end date
  1700. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  1701. GetResString(IDS_CREATE_HLP66),0,MAKELANGID(LANG_NEUTRAL,
  1702. SUBLANG_DEFAULT),szBuffer,
  1703. MAX_STRING_LENGTH,(va_list*)szValues );
  1704. DISPLAY_MESSAGE(stdout,szBuffer);
  1705. }
  1706. else if ( dw == IDS_CREATE_HLP87 )
  1707. {
  1708. // get the date format
  1709. if ( RETVAL_FAIL == GetDateFieldFormat( &wFormatID ))
  1710. {
  1711. return RETVAL_FAIL;
  1712. }
  1713. if ( wFormatID == 0)
  1714. {
  1715. lstrcpy (szValues[0], GetResString (IDS_MMDDYY_VALUE));
  1716. }
  1717. else if ( wFormatID == 1)
  1718. {
  1719. lstrcpy (szValues[0], GetResString (IDS_DDMMYY_VALUE));
  1720. }
  1721. else
  1722. {
  1723. lstrcpy (szValues[0], GetResString (IDS_YYMMDD_VALUE));
  1724. }
  1725. // To format -sd and -ed values
  1726. FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  1727. GetResString(IDS_CREATE_HLP87),0,MAKELANGID(LANG_NEUTRAL,
  1728. SUBLANG_DEFAULT),szBuffer,
  1729. MAX_STRING_LENGTH,(va_list*)szValues );
  1730. DISPLAY_MESSAGE(stdout,szBuffer);
  1731. }
  1732. else
  1733. {
  1734. DISPLAY_MESSAGE(stdout,GetResString(dw));
  1735. }
  1736. }
  1737. return RETVAL_FAIL;
  1738. }
  1739. /******************************************************************************
  1740. Routine Description:
  1741. This routine validates the options specified by the user & determines
  1742. the type of a scheduled task
  1743. Arguments:
  1744. [ in ] argc : The count of arguments given by the user.
  1745. [ in ] argv : Array containing the command line arguments.
  1746. [ in ] tcresubops : Structure containing Scheduled task's properties.
  1747. [ in ] tcreoptvals : Structure containing optional properties to set for a
  1748. scheduledtask .
  1749. [ out ] pdwRetScheType : pointer to the type of a schedule task
  1750. [Daily,once,weekly etc].
  1751. [ out ] pbUserStatus : pointer to check whether the -ru is given in
  1752. the command line or not.
  1753. Return Value :
  1754. A DWORD value indicating RETVAL_SUCCESS on success else RETVAL_FAIL
  1755. on failure
  1756. ******************************************************************************/
  1757. DWORD
  1758. ProcessCreateOptions(DWORD argc, LPCTSTR argv[],TCREATESUBOPTS &tcresubops,
  1759. TCREATEOPVALS &tcreoptvals, DWORD* pdwRetScheType, WORD *pwUserStatus)
  1760. {
  1761. DWORD dwScheduleType = 0;
  1762. BOOL bRet = FALSE;
  1763. lstrcpy( tcresubops.szPassword, ASTERIX);
  1764. lstrcpy( tcresubops.szRunAsPassword, ASTERIX);
  1765. // fill the TCMDPARSER structure
  1766. TCMDPARSER cmdOptions[] = {
  1767. {
  1768. CMDOPTION_CREATE,
  1769. CP_MAIN_OPTION,
  1770. OPTION_COUNT,
  1771. 0,
  1772. &tcresubops.bCreate,
  1773. NULL_STRING,
  1774. NULL,
  1775. NULL
  1776. },
  1777. {
  1778. SWITCH_SERVER,
  1779. CP_TYPE_TEXT | CP_VALUE_MANDATORY,
  1780. OPTION_COUNT,
  1781. 0,
  1782. &tcresubops.szServer,
  1783. NULL_STRING,
  1784. NULL,
  1785. NULL
  1786. },
  1787. {
  1788. SWITCH_RUNAS_USER,
  1789. CP_TYPE_TEXT | CP_VALUE_MANDATORY,
  1790. OPTION_COUNT,
  1791. 0,
  1792. tcresubops.szRunAsUser,
  1793. NULL_STRING,
  1794. NULL,
  1795. NULL
  1796. },
  1797. {
  1798. SWITCH_RUNAS_PASSWORD,
  1799. CP_TYPE_TEXT | CP_VALUE_OPTIONAL,
  1800. OPTION_COUNT,
  1801. 0,
  1802. &tcresubops.szRunAsPassword,
  1803. NULL_STRING,
  1804. NULL,
  1805. NULL
  1806. },
  1807. {
  1808. SWITCH_USER,
  1809. CP_TYPE_TEXT | CP_VALUE_MANDATORY,
  1810. OPTION_COUNT,
  1811. 0,
  1812. tcresubops.szUser,
  1813. NULL_STRING,
  1814. NULL,
  1815. NULL
  1816. },
  1817. {
  1818. SWITCH_PASSWORD,
  1819. CP_TYPE_TEXT | CP_VALUE_OPTIONAL,
  1820. OPTION_COUNT,
  1821. 0,
  1822. &tcresubops.szPassword,
  1823. NULL_STRING,
  1824. NULL,
  1825. NULL
  1826. },
  1827. {
  1828. SWITCH_SCHEDULETYPE,
  1829. CP_TYPE_TEXT | CP_VALUE_MANDATORY | CP_MANDATORY,
  1830. OPTION_COUNT,
  1831. 0,
  1832. tcresubops.szSchedType,
  1833. NULL_STRING,
  1834. NULL,
  1835. NULL
  1836. },
  1837. {
  1838. SWITCH_MODIFIER,
  1839. CP_TYPE_TEXT | CP_VALUE_MANDATORY,
  1840. OPTION_COUNT,
  1841. 0,
  1842. tcresubops.szModifier,
  1843. NULL_STRING,
  1844. NULL,
  1845. NULL
  1846. },
  1847. {
  1848. SWITCH_DAY,
  1849. CP_TYPE_TEXT | CP_VALUE_MANDATORY,
  1850. OPTION_COUNT,
  1851. 0,
  1852. tcresubops.szDays,
  1853. NULL_STRING,
  1854. NULL,
  1855. NULL
  1856. },
  1857. {
  1858. SWITCH_MONTHS,
  1859. CP_TYPE_TEXT | CP_VALUE_MANDATORY,
  1860. OPTION_COUNT,
  1861. 0,
  1862. tcresubops.szMonths,
  1863. NULL_STRING,
  1864. NULL,
  1865. NULL
  1866. },
  1867. {
  1868. SWITCH_IDLETIME,
  1869. CP_TYPE_TEXT | CP_VALUE_MANDATORY,
  1870. OPTION_COUNT,
  1871. 0,
  1872. tcresubops.szIdleTime,
  1873. NULL_STRING,
  1874. NULL,
  1875. NULL
  1876. },
  1877. {
  1878. SWITCH_TASKNAME,
  1879. CP_TYPE_TEXT | CP_VALUE_MANDATORY | CP_MANDATORY,
  1880. OPTION_COUNT,
  1881. 0,
  1882. tcresubops.szTaskName,
  1883. NULL_STRING,
  1884. NULL,
  1885. NULL
  1886. },
  1887. {
  1888. SWITCH_TASKRUN,
  1889. CP_TYPE_TEXT | CP_VALUE_MANDATORY | CP_MANDATORY,
  1890. OPTION_COUNT,
  1891. 0,
  1892. tcresubops.szTaskRun,
  1893. NULL_STRING,
  1894. NULL,
  1895. NULL
  1896. },
  1897. {
  1898. SWITCH_STARTTIME,
  1899. CP_TYPE_TEXT | CP_VALUE_MANDATORY,
  1900. OPTION_COUNT,
  1901. 0,
  1902. tcresubops.szStartTime,
  1903. NULL_STRING,
  1904. NULL,
  1905. NULL
  1906. },
  1907. {
  1908. SWITCH_STARTDATE,
  1909. CP_TYPE_TEXT | CP_VALUE_MANDATORY,
  1910. OPTION_COUNT,
  1911. 0,
  1912. tcresubops.szStartDate,
  1913. NULL_STRING,
  1914. NULL,
  1915. NULL
  1916. },
  1917. {
  1918. SWITCH_ENDDATE,
  1919. CP_TYPE_TEXT | CP_VALUE_MANDATORY,
  1920. OPTION_COUNT,
  1921. 0,
  1922. tcresubops.szEndDate,
  1923. NULL_STRING,
  1924. NULL,
  1925. NULL
  1926. },
  1927. {
  1928. CMDOPTION_USAGE,
  1929. CP_USAGE ,
  1930. OPTION_COUNT,
  1931. 0,
  1932. &tcresubops.bUsage,
  1933. 0,
  1934. 0
  1935. }
  1936. };
  1937. // Parsing the copy option switches
  1938. if ( DoParseParam( argc, argv, SIZE_OF_ARRAY(cmdOptions), cmdOptions ) == FALSE )
  1939. {
  1940. //display error message
  1941. DISPLAY_MESSAGE( stderr, GetResString(IDS_LOGTYPE_ERROR ));
  1942. DISPLAY_MESSAGE( stderr, GetReason() );
  1943. //DISPLAY_MESSAGE(stderr,GetResString(IDS_CREATE_ERR));
  1944. return RETVAL_FAIL;
  1945. }
  1946. // trim the blank spaces in string values
  1947. StrTrim(tcresubops.szServer, TRIM_SPACES );
  1948. StrTrim(tcresubops.szTaskName, TRIM_SPACES );
  1949. StrTrim(tcresubops.szTaskRun, TRIM_SPACES );
  1950. StrTrim(tcresubops.szModifier, TRIM_SPACES );
  1951. StrTrim(tcresubops.szMonths, TRIM_SPACES );
  1952. StrTrim(tcresubops.szUser, TRIM_SPACES );
  1953. StrTrim(tcresubops.szRunAsUser, TRIM_SPACES );
  1954. StrTrim(tcresubops.szSchedType, TRIM_SPACES );
  1955. StrTrim(tcresubops.szEndDate, TRIM_SPACES );
  1956. StrTrim(tcresubops.szStartDate, TRIM_SPACES );
  1957. StrTrim(tcresubops.szStartTime, TRIM_SPACES );
  1958. StrTrim(tcresubops.szIdleTime, TRIM_SPACES );
  1959. StrTrim(tcresubops.szDays, TRIM_SPACES );
  1960. // check whether password (-p) specified in the command line or not.
  1961. if ( cmdOptions[OI_PASSWORD].dwActuals == 0 )
  1962. {
  1963. lstrcpy(tcresubops.szPassword, NULL_STRING);
  1964. }
  1965. // check whether run as password (-rp) specified in the command line or not.
  1966. if ( cmdOptions[OI_RUNASPASSWORD].dwActuals == 0 )
  1967. {
  1968. lstrcpy(tcresubops.szRunAsPassword, NULL_STRING);
  1969. }
  1970. // Display create usage if user specified -create -? option
  1971. if( tcresubops.bUsage == TRUE)
  1972. {
  1973. DisplayCreateUsage();
  1974. return RETVAL_FAIL;
  1975. }
  1976. if( ( cmdOptions[OI_SERVER].dwActuals == 1 ) && ( lstrlen( tcresubops.szServer ) == 0 ) )
  1977. {
  1978. DISPLAY_MESSAGE(stderr,GetResString(IDS_NO_SERVER));
  1979. return RETVAL_FAIL;
  1980. }
  1981. // check for invalid user name
  1982. if( ( cmdOptions[OI_SERVER].dwActuals == 0 ) && ( cmdOptions[OI_USERNAME].dwActuals == 1 ) )
  1983. {
  1984. DISPLAY_MESSAGE(stderr, GetResString(IDS_CREATE_USER_BUT_NOMACHINE));
  1985. return RETVAL_FAIL;
  1986. }
  1987. // check for the length of user name
  1988. if( ( cmdOptions[OI_SERVER].dwActuals == 1 ) && ( cmdOptions[OI_USERNAME].dwActuals == 1 ) &&
  1989. ( lstrlen( tcresubops.szUser ) == 0 ) )
  1990. {
  1991. DISPLAY_MESSAGE(stderr, GetResString(IDS_INVALID_USERNAME));
  1992. return RETVAL_FAIL;
  1993. }
  1994. //Determine scheduled type
  1995. if( lstrcmpi(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_MINUTE)) == 0 )
  1996. {
  1997. dwScheduleType = SCHED_TYPE_MINUTE;
  1998. }
  1999. else if( lstrcmpi(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_HOUR)) == 0 )
  2000. {
  2001. dwScheduleType = SCHED_TYPE_HOURLY;
  2002. }
  2003. else if( lstrcmpi(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_DAILY)) == 0 )
  2004. {
  2005. dwScheduleType = SCHED_TYPE_DAILY;
  2006. }
  2007. else if( lstrcmpi(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_WEEK)) == 0 )
  2008. {
  2009. dwScheduleType = SCHED_TYPE_WEEKLY;
  2010. }
  2011. else if( lstrcmpi(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_MONTHLY)) == 0 )
  2012. {
  2013. dwScheduleType = SCHED_TYPE_MONTHLY;
  2014. }
  2015. else if( lstrcmpi(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_ONCE)) == 0 )
  2016. {
  2017. dwScheduleType = SCHED_TYPE_ONETIME;
  2018. }
  2019. else if( lstrcmpi(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_STARTUP)) == 0 )
  2020. {
  2021. dwScheduleType = SCHED_TYPE_ONSTART;
  2022. }
  2023. else if( lstrcmpi(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_LOGON)) == 0 )
  2024. {
  2025. dwScheduleType = SCHED_TYPE_ONLOGON;
  2026. }
  2027. else if( lstrcmpi(tcresubops.szSchedType,GetResString(IDS_SCHEDTYPE_IDLE)) == 0 )
  2028. {
  2029. dwScheduleType = SCHED_TYPE_ONIDLE;
  2030. }
  2031. else
  2032. {
  2033. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_SCHEDTYPE));
  2034. return RETVAL_FAIL;
  2035. }
  2036. // Assign the scheduled type to the out parameter.
  2037. *pdwRetScheType = dwScheduleType;
  2038. // To find whether run as user name is given in the cmd line or not
  2039. if( ( cmdOptions[1].dwActuals == 1 ) &&
  2040. ( (cmdOptions[2].dwActuals == 0) && (cmdOptions[4].dwActuals == 0) ) )
  2041. {
  2042. *pwUserStatus = OI_SERVER;
  2043. }
  2044. else if( (cmdOptions[2].dwActuals == 1) && (cmdOptions[4].dwActuals == 1) )
  2045. {
  2046. *pwUserStatus = OI_RUNANDUSER;
  2047. }
  2048. else if( cmdOptions[2].dwActuals == 1 )
  2049. {
  2050. *pwUserStatus = OI_RUNASUSERNAME;
  2051. }
  2052. else if ( cmdOptions[4].dwActuals == 1 )
  2053. {
  2054. *pwUserStatus = OI_USERNAME;
  2055. }
  2056. // Start validations for the sub-options
  2057. if( ValidateSuboptVal(tcresubops, tcreoptvals, cmdOptions, dwScheduleType) == RETVAL_FAIL )
  2058. {
  2059. return(RETVAL_FAIL);
  2060. }
  2061. return RETVAL_SUCCESS;
  2062. }
  2063. /******************************************************************************
  2064. Routine Description:
  2065. This routine splits the input parameters into 2 substrings and returns it.
  2066. Arguments:
  2067. [ in ] szInput : Input string.
  2068. [ in ] szFirstString : First Output string containing the path of the
  2069. file.
  2070. [ in ] szSecondString : The second output containing the paramters.
  2071. Return Value :
  2072. A DWORD value indicating RETVAL_SUCCESS on success else RETVAL_FAIL
  2073. on failure
  2074. ******************************************************************************/
  2075. DWORD ProcessFilePath(LPTSTR szInput,LPTSTR szFirstString,LPTSTR szSecondString)
  2076. {
  2077. _TCHAR *pszTok = NULL ;
  2078. _TCHAR *pszSep = NULL ;
  2079. _TCHAR szTmpString[MAX_RES_STRING] = NULL_STRING;
  2080. _TCHAR szTmpInStr[MAX_RES_STRING] = NULL_STRING;
  2081. _TCHAR szTmpOutStr[MAX_RES_STRING] = NULL_STRING;
  2082. _TCHAR szTmpString1[MAX_RES_STRING] = NULL_STRING;
  2083. DWORD dwCnt = 0 ;
  2084. DWORD dwLen = 0 ;
  2085. #ifdef _WIN64
  2086. INT64 dwPos ;
  2087. #else
  2088. DWORD dwPos ;
  2089. #endif
  2090. //checking if the input parameters are NULL and if so
  2091. // return FAILURE. This condition will not come
  2092. // but checking for safety sake.
  2093. if( (szInput == NULL) || (_tcslen(szInput)==0))
  2094. {
  2095. return RETVAL_FAIL ;
  2096. }
  2097. _tcscpy(szTmpString,szInput);
  2098. _tcscpy(szTmpString1,szInput);
  2099. _tcscpy(szTmpInStr,szInput);
  2100. // check for first double quote (")
  2101. if ( szTmpInStr[0] == _T('\"') )
  2102. {
  2103. // trim the first double quote
  2104. StrTrim( szTmpInStr, _T("\""));
  2105. // check for end double quote
  2106. pszSep = _tcschr(szTmpInStr,_T('\"')) ;
  2107. // get the position
  2108. dwPos = pszSep - szTmpInStr + 1;
  2109. }
  2110. else
  2111. {
  2112. // check for the space
  2113. pszSep = _tcschr(szTmpInStr,_T(' ')) ;
  2114. // get the position
  2115. dwPos = pszSep - szTmpInStr;
  2116. }
  2117. if ( pszSep != NULL )
  2118. {
  2119. szTmpInStr[dwPos] = _T('\0');
  2120. }
  2121. else
  2122. {
  2123. _tcscpy(szFirstString, szTmpString);
  2124. _tcscpy(szSecondString,NULL_STRING);
  2125. return RETVAL_SUCCESS;
  2126. }
  2127. // intialize the variable
  2128. dwCnt = 0 ;
  2129. // get the length of the string
  2130. dwLen = _tcslen ( szTmpString );
  2131. // check for end of string
  2132. while ( ( dwPos <= dwLen ) && szTmpString[dwPos++] != _T('\0') )
  2133. {
  2134. szTmpOutStr[dwCnt++] = szTmpString[dwPos];
  2135. }
  2136. // trim the executable and arguments
  2137. StrTrim( szTmpInStr, _T("\""));
  2138. StrTrim( szTmpInStr, _T(" "));
  2139. _tcscpy(szFirstString, szTmpInStr);
  2140. _tcscpy(szSecondString,szTmpOutStr);
  2141. // return success
  2142. return RETVAL_SUCCESS;
  2143. }