PageRenderTime 79ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/uClinux-dist/user/vixie-cron/crontab.c

https://bitbucket.org/__wp__/mb-linux-msli
C | 624 lines | 492 code | 66 blank | 66 comment | 104 complexity | 568d3e5253ae3c8b0136828389c395ec MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0, LGPL-2.0, MPL-2.0, ISC, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, 0BSD, CC-BY-SA-3.0, GPL-3.0, LGPL-3.0, AGPL-1.0, Unlicense
  1. /* Copyright 1988,1990,1993,1994 by Paul Vixie
  2. * All rights reserved
  3. *
  4. * Distribute freely, except: don't remove my name from the source or
  5. * documentation (don't take credit for my work), mark your changes (don't
  6. * get me blamed for your possible bugs), don't alter or remove this
  7. * notice. May be sold if buildable source is provided to buyer. No
  8. * warrantee of any kind, express or implied, is included with this
  9. * software; use at your own risk, responsibility for damages (if any) to
  10. * anyone resulting from the use of this software rests entirely with the
  11. * user.
  12. *
  13. * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
  14. * I'll try to keep a version up to date. I can be reached as follows:
  15. * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
  16. */
  17. #if !defined(lint) && !defined(LINT)
  18. static char rcsid[] = "$Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $";
  19. #endif
  20. /* crontab - install and manage per-user crontab files
  21. * vix 02may87 [RCS has the rest of the log]
  22. * vix 26jan87 [original]
  23. */
  24. #define MAIN_PROGRAM
  25. #include "cron.h"
  26. #include <errno.h>
  27. #include <fcntl.h>
  28. #include <sys/file.h>
  29. #include <sys/stat.h>
  30. #ifdef USE_UTIMES
  31. # include <sys/time.h>
  32. #else
  33. # include <time.h>
  34. # include <utime.h>
  35. #endif
  36. #if defined(POSIX)
  37. # include <locale.h>
  38. #endif
  39. #define NHEADER_LINES 3
  40. enum opt_t { opt_unknown, opt_list, opt_delete, opt_edit, opt_replace };
  41. #if DEBUGGING
  42. static char *Options[] = { "???", "list", "delete", "edit", "replace" };
  43. #endif
  44. static PID_T Pid;
  45. static char User[MAX_UNAME], RealUser[MAX_UNAME];
  46. static char Filename[MAX_FNAME];
  47. static FILE *NewCrontab;
  48. static int CheckErrorCount;
  49. static enum opt_t Option;
  50. static struct passwd *pw;
  51. static void list_cmd __P((void)),
  52. delete_cmd __P((void)),
  53. edit_cmd __P((void)),
  54. poke_daemon __P((void)),
  55. check_error __P((char *)),
  56. parse_args __P((int c, char *v[]));
  57. static int replace_cmd __P((void));
  58. static void
  59. usage(msg)
  60. char *msg;
  61. {
  62. fprintf(stderr, "%s: usage error: %s\n", ProgramName, msg);
  63. fprintf(stderr, "usage:\t%s [-u user] file\n", ProgramName);
  64. fprintf(stderr, "\t%s [-u user] { -e | -l | -r }\n", ProgramName);
  65. fprintf(stderr, "\t\t(default operation is replace, per 1003.2)\n");
  66. fprintf(stderr, "\t-e\t(edit user's crontab)\n");
  67. fprintf(stderr, "\t-l\t(list user's crontab)\n");
  68. fprintf(stderr, "\t-r\t(delete user's crontab)\n");
  69. exit(ERROR_EXIT);
  70. }
  71. int
  72. main(argc, argv)
  73. int argc;
  74. char *argv[];
  75. {
  76. int exitstatus;
  77. Pid = getpid();
  78. ProgramName = argv[0];
  79. #if defined(POSIX)
  80. setlocale(LC_ALL, "");
  81. #endif
  82. #if defined(BSD)
  83. setlinebuf(stderr);
  84. #endif
  85. parse_args(argc, argv); /* sets many globals, opens a file */
  86. set_cron_uid();
  87. set_cron_cwd();
  88. if (!allowed(User)) {
  89. fprintf(stderr,
  90. "You (%s) are not allowed to use this program (%s)\n",
  91. User, ProgramName);
  92. fprintf(stderr, "See crontab(1) for more information\n");
  93. log_it(RealUser, Pid, "AUTH", "crontab command not allowed");
  94. exit(ERROR_EXIT);
  95. }
  96. exitstatus = OK_EXIT;
  97. switch (Option) {
  98. case opt_list: list_cmd();
  99. break;
  100. case opt_delete: delete_cmd();
  101. break;
  102. case opt_edit: edit_cmd();
  103. break;
  104. case opt_replace: if (replace_cmd() < 0)
  105. exitstatus = ERROR_EXIT;
  106. break;
  107. }
  108. exit(0);
  109. /*NOTREACHED*/
  110. }
  111. static void
  112. parse_args(argc, argv)
  113. int argc;
  114. char *argv[];
  115. {
  116. int argch;
  117. if (!(pw = getpwuid(getuid()))) {
  118. fprintf(stderr, "%s: your UID isn't in the passwd file.\n",
  119. ProgramName);
  120. fprintf(stderr, "bailing out.\n");
  121. exit(ERROR_EXIT);
  122. }
  123. strcpy(User, pw->pw_name);
  124. strcpy(RealUser, User);
  125. Filename[0] = '\0';
  126. Option = opt_unknown;
  127. while (EOF != (argch = getopt(argc, argv, "u:lerx:"))) {
  128. switch (argch) {
  129. case 'x':
  130. if (!set_debug_flags(optarg))
  131. usage("bad debug option");
  132. break;
  133. case 'u':
  134. if (getuid() != ROOT_UID)
  135. {
  136. fprintf(stderr,
  137. "must be privileged to use -u\n");
  138. exit(ERROR_EXIT);
  139. }
  140. if (!(pw = getpwnam(optarg)))
  141. {
  142. fprintf(stderr, "%s: user `%s' unknown\n",
  143. ProgramName, optarg);
  144. exit(ERROR_EXIT);
  145. }
  146. (void) strcpy(User, optarg);
  147. break;
  148. case 'l':
  149. if (Option != opt_unknown)
  150. usage("only one operation permitted");
  151. Option = opt_list;
  152. break;
  153. case 'r':
  154. if (Option != opt_unknown)
  155. usage("only one operation permitted");
  156. Option = opt_delete;
  157. break;
  158. case 'e':
  159. if (Option != opt_unknown)
  160. usage("only one operation permitted");
  161. Option = opt_edit;
  162. break;
  163. default:
  164. usage("unrecognized option");
  165. }
  166. }
  167. endpwent();
  168. if (Option != opt_unknown) {
  169. if (argv[optind] != NULL) {
  170. usage("no arguments permitted after this option");
  171. }
  172. } else {
  173. if (argv[optind] != NULL) {
  174. Option = opt_replace;
  175. (void) strcpy (Filename, argv[optind]);
  176. } else {
  177. usage("file name must be specified for replace");
  178. }
  179. }
  180. if (Option == opt_replace) {
  181. /* we have to open the file here because we're going to
  182. * chdir(2) into /var/cron before we get around to
  183. * reading the file.
  184. */
  185. if (!strcmp(Filename, "-")) {
  186. NewCrontab = stdin;
  187. } else {
  188. /* relinquish the setuid status of the binary during
  189. * the open, lest nonroot users read files they should
  190. * not be able to read. we can't use access() here
  191. * since there's a race condition. thanks go out to
  192. * Arnt Gulbrandsen <agulbra@pvv.unit.no> for spotting
  193. * the race.
  194. */
  195. if (swap_uids() < OK) {
  196. perror("swapping uids");
  197. exit(ERROR_EXIT);
  198. }
  199. if (!(NewCrontab = fopen(Filename, "r"))) {
  200. perror(Filename);
  201. exit(ERROR_EXIT);
  202. }
  203. if (swap_uids() < OK) {
  204. perror("swapping uids back");
  205. exit(ERROR_EXIT);
  206. }
  207. }
  208. }
  209. Debug(DMISC, ("user=%s, file=%s, option=%s\n",
  210. User, Filename, Options[(int)Option]))
  211. }
  212. static void
  213. list_cmd() {
  214. char n[MAX_FNAME];
  215. FILE *f;
  216. int ch;
  217. log_it(RealUser, Pid, "LIST", User);
  218. (void) sprintf(n, CRON_TAB(User));
  219. if (!(f = fopen(n, "r"))) {
  220. if (errno == ENOENT)
  221. fprintf(stderr, "no crontab for %s\n", User);
  222. else
  223. perror(n);
  224. exit(ERROR_EXIT);
  225. }
  226. /* file is open. copy to stdout, close.
  227. */
  228. Set_LineNum(1)
  229. while (EOF != (ch = get_char(f)))
  230. putchar(ch);
  231. fclose(f);
  232. }
  233. static void
  234. delete_cmd() {
  235. char n[MAX_FNAME];
  236. log_it(RealUser, Pid, "DELETE", User);
  237. (void) sprintf(n, CRON_TAB(User));
  238. if (unlink(n)) {
  239. if (errno == ENOENT)
  240. fprintf(stderr, "no crontab for %s\n", User);
  241. else
  242. perror(n);
  243. exit(ERROR_EXIT);
  244. }
  245. poke_daemon();
  246. }
  247. static void
  248. check_error(msg)
  249. char *msg;
  250. {
  251. CheckErrorCount++;
  252. fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg);
  253. }
  254. static void
  255. edit_cmd() {
  256. char n[MAX_FNAME], q[MAX_TEMPSTR], *editor;
  257. FILE *f;
  258. int ch, t, x;
  259. struct stat statbuf;
  260. time_t mtime;
  261. WAIT_T waiter;
  262. PID_T pid, xpid;
  263. log_it(RealUser, Pid, "BEGIN EDIT", User);
  264. (void) sprintf(n, CRON_TAB(User));
  265. if (!(f = fopen(n, "r"))) {
  266. if (errno != ENOENT) {
  267. perror(n);
  268. exit(ERROR_EXIT);
  269. }
  270. fprintf(stderr, "no crontab for %s - using an empty one\n",
  271. User);
  272. if (!(f = fopen("/dev/null", "r"))) {
  273. perror("/dev/null");
  274. exit(ERROR_EXIT);
  275. }
  276. }
  277. (void) sprintf(Filename, "/tmp/crontab.%d", Pid);
  278. if (-1 == (t = open(Filename, O_CREAT|O_EXCL|O_RDWR, 0600))) {
  279. perror(Filename);
  280. goto fatal;
  281. }
  282. #ifdef HAS_FCHOWN
  283. if (fchown(t, getuid(), getgid()) < 0) {
  284. #else
  285. if (chown(Filename, getuid(), getgid()) < 0) {
  286. #endif
  287. perror("fchown");
  288. goto fatal;
  289. }
  290. if (!(NewCrontab = fdopen(t, "r+"))) {
  291. perror("fdopen");
  292. goto fatal;
  293. }
  294. Set_LineNum(1)
  295. /* ignore the top few comments since we probably put them there.
  296. */
  297. for (x = 0; x < NHEADER_LINES; x++) {
  298. ch = get_char(f);
  299. if (EOF == ch)
  300. break;
  301. if ('#' != ch) {
  302. putc(ch, NewCrontab);
  303. break;
  304. }
  305. while (EOF != (ch = get_char(f)))
  306. if (ch == '\n')
  307. break;
  308. if (EOF == ch)
  309. break;
  310. }
  311. /* copy the rest of the crontab (if any) to the temp file.
  312. */
  313. if (EOF != ch)
  314. while (EOF != (ch = get_char(f)))
  315. putc(ch, NewCrontab);
  316. fclose(f);
  317. if (fflush(NewCrontab) < OK) {
  318. perror(Filename);
  319. exit(ERROR_EXIT);
  320. }
  321. again:
  322. rewind(NewCrontab);
  323. if (ferror(NewCrontab)) {
  324. fprintf(stderr, "%s: error while writing new crontab to %s\n",
  325. ProgramName, Filename);
  326. fatal: unlink(Filename);
  327. exit(ERROR_EXIT);
  328. }
  329. if (fstat(t, &statbuf) < 0) {
  330. perror("fstat");
  331. goto fatal;
  332. }
  333. mtime = statbuf.st_mtime;
  334. if ((!(editor = getenv("VISUAL")))
  335. && (!(editor = getenv("EDITOR")))
  336. ) {
  337. editor = EDITOR;
  338. }
  339. /* we still have the file open. editors will generally rewrite the
  340. * original file rather than renaming/unlinking it and starting a
  341. * new one; even backup files are supposed to be made by copying
  342. * rather than by renaming. if some editor does not support this,
  343. * then don't use it. the security problems are more severe if we
  344. * close and reopen the file around the edit.
  345. */
  346. switch (pid = fork()) {
  347. case -1:
  348. perror("fork");
  349. goto fatal;
  350. case 0:
  351. /* child */
  352. if (setuid(getuid()) < 0) {
  353. perror("setuid(getuid())");
  354. exit(ERROR_EXIT);
  355. }
  356. if (chdir("/tmp") < 0) {
  357. perror("chdir(/tmp)");
  358. exit(ERROR_EXIT);
  359. }
  360. if (strlen(editor) + strlen(Filename) + 2 >= MAX_TEMPSTR) {
  361. fprintf(stderr, "%s: editor or filename too long\n",
  362. ProgramName);
  363. exit(ERROR_EXIT);
  364. }
  365. sprintf(q, "%s %s", editor, Filename);
  366. execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", q, NULL);
  367. perror(editor);
  368. exit(ERROR_EXIT);
  369. /*NOTREACHED*/
  370. default:
  371. /* parent */
  372. break;
  373. }
  374. /* parent */
  375. xpid = wait(&waiter);
  376. if (xpid != pid) {
  377. fprintf(stderr, "%s: wrong PID (%d != %d) from \"%s\"\n",
  378. ProgramName, xpid, pid, editor);
  379. goto fatal;
  380. }
  381. if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) {
  382. fprintf(stderr, "%s: \"%s\" exited with status %d\n",
  383. ProgramName, editor, WEXITSTATUS(waiter));
  384. goto fatal;
  385. }
  386. if (WIFSIGNALED(waiter)) {
  387. fprintf(stderr,
  388. "%s: \"%s\" killed; signal %d (%score dumped)\n",
  389. ProgramName, editor, WTERMSIG(waiter),
  390. WCOREDUMP(waiter) ?"" :"no ");
  391. goto fatal;
  392. }
  393. if (fstat(t, &statbuf) < 0) {
  394. perror("fstat");
  395. goto fatal;
  396. }
  397. if (mtime == statbuf.st_mtime) {
  398. fprintf(stderr, "%s: no changes made to crontab\n",
  399. ProgramName);
  400. goto remove;
  401. }
  402. fprintf(stderr, "%s: installing new crontab\n", ProgramName);
  403. switch (replace_cmd()) {
  404. case 0:
  405. break;
  406. case -1:
  407. for (;;) {
  408. printf("Do you want to retry the same edit? ");
  409. fflush(stdout);
  410. q[0] = '\0';
  411. (void) fgets(q, sizeof q, stdin);
  412. switch (islower(q[0]) ? q[0] : tolower(q[0])) {
  413. case 'y':
  414. goto again;
  415. case 'n':
  416. goto abandon;
  417. default:
  418. fprintf(stderr, "Enter Y or N\n");
  419. }
  420. }
  421. /*NOTREACHED*/
  422. case -2:
  423. abandon:
  424. fprintf(stderr, "%s: edits left in %s\n",
  425. ProgramName, Filename);
  426. goto done;
  427. default:
  428. fprintf(stderr, "%s: panic: bad switch() in replace_cmd()\n");
  429. goto fatal;
  430. }
  431. remove:
  432. unlink(Filename);
  433. done:
  434. log_it(RealUser, Pid, "END EDIT", User);
  435. }
  436. /* returns 0 on success
  437. * -1 on syntax error
  438. * -2 on install error
  439. */
  440. static int
  441. replace_cmd() {
  442. char n[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME];
  443. FILE *tmp;
  444. int ch, eof;
  445. entry *e;
  446. time_t now = time(NULL);
  447. char **envp = env_init();
  448. (void) sprintf(n, "tmp.%d", Pid);
  449. (void) sprintf(tn, CRON_TAB(n));
  450. if (!(tmp = fopen(tn, "w+"))) {
  451. perror(tn);
  452. return (-2);
  453. }
  454. /* write a signature at the top of the file.
  455. *
  456. * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code.
  457. */
  458. fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n");
  459. fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now));
  460. fprintf(tmp, "# (Cron version -- %s)\n", rcsid);
  461. /* copy the crontab to the tmp
  462. */
  463. rewind(NewCrontab);
  464. Set_LineNum(1)
  465. while (EOF != (ch = get_char(NewCrontab)))
  466. putc(ch, tmp);
  467. ftruncate(fileno(tmp), ftell(tmp));
  468. fflush(tmp); rewind(tmp);
  469. if (ferror(tmp)) {
  470. fprintf(stderr, "%s: error while writing new crontab to %s\n",
  471. ProgramName, tn);
  472. fclose(tmp); unlink(tn);
  473. return (-2);
  474. }
  475. /* check the syntax of the file being installed.
  476. */
  477. /* BUG: was reporting errors after the EOF if there were any errors
  478. * in the file proper -- kludged it by stopping after first error.
  479. * vix 31mar87
  480. */
  481. Set_LineNum(1 - NHEADER_LINES)
  482. CheckErrorCount = 0; eof = FALSE;
  483. while (!CheckErrorCount && !eof) {
  484. switch (load_env(envstr, tmp)) {
  485. case ERR:
  486. eof = TRUE;
  487. break;
  488. case FALSE:
  489. e = load_entry(tmp, check_error, pw, envp);
  490. if (e)
  491. free(e);
  492. break;
  493. case TRUE:
  494. break;
  495. }
  496. }
  497. if (CheckErrorCount != 0) {
  498. fprintf(stderr, "errors in crontab file, can't install.\n");
  499. fclose(tmp); unlink(tn);
  500. return (-1);
  501. }
  502. #ifdef HAS_FCHOWN
  503. if (fchown(fileno(tmp), ROOT_UID, -1) < OK)
  504. #else
  505. if (chown(tn, ROOT_UID, -1) < OK)
  506. #endif
  507. {
  508. perror("chown");
  509. fclose(tmp); unlink(tn);
  510. return (-2);
  511. }
  512. #ifdef HAS_FCHMOD
  513. if (fchmod(fileno(tmp), 0600) < OK)
  514. #else
  515. if (chmod(tn, 0600) < OK)
  516. #endif
  517. {
  518. perror("chown");
  519. fclose(tmp); unlink(tn);
  520. return (-2);
  521. }
  522. if (fclose(tmp) == EOF) {
  523. perror("fclose");
  524. unlink(tn);
  525. return (-2);
  526. }
  527. (void) sprintf(n, CRON_TAB(User));
  528. if (rename(tn, n)) {
  529. fprintf(stderr, "%s: error renaming %s to %s\n",
  530. ProgramName, tn, n);
  531. perror("rename");
  532. unlink(tn);
  533. return (-2);
  534. }
  535. log_it(RealUser, Pid, "REPLACE", User);
  536. poke_daemon();
  537. return (0);
  538. }
  539. static void
  540. poke_daemon() {
  541. #ifdef USE_UTIMES
  542. struct timeval tvs[2];
  543. struct timezone tz;
  544. (void) gettimeofday(&tvs[0], &tz);
  545. tvs[1] = tvs[0];
  546. if (utimes(SPOOL_DIR, tvs) < OK) {
  547. fprintf(stderr, "crontab: can't update mtime on spooldir\n");
  548. perror(SPOOL_DIR);
  549. return;
  550. }
  551. #else
  552. if (utime(SPOOL_DIR, NULL) < OK) {
  553. fprintf(stderr, "crontab: can't update mtime on spooldir\n");
  554. perror(SPOOL_DIR);
  555. return;
  556. }
  557. #endif /*USE_UTIMES*/
  558. }