1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
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
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
17 * From Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp
18 * $FreeBSD: src/usr.sbin/cron/crontab/crontab.c,v 1.12.2.4 2001/06/16 03:18:37 peter Exp $
19 * $DragonFly: src/usr.sbin/cron/crontab/crontab.c,v 1.3 2003/11/03 19:31:36 eirikn Exp $
22 /* crontab - install and manage per-user crontab files
23 * vix 02may87 [RCS has the rest of the log]
24 * vix 26jan87 [original]
27 static char version[] = "$DragonFly: src/usr.sbin/cron/crontab/crontab.c,v 1.3 2003/11/03 19:31:36 eirikn Exp $";
38 # include <sys/time.h>
48 #define NHEADER_LINES 3
51 enum opt_t { opt_unknown, opt_list, opt_delete, opt_edit, opt_replace };
54 static char *Options[] = { "???", "list", "delete", "edit", "replace" };
59 static char User[MAX_UNAME], RealUser[MAX_UNAME];
60 static char Filename[MAX_FNAME];
61 static FILE *NewCrontab;
62 static int CheckErrorCount;
63 static enum opt_t Option;
64 static struct passwd *pw;
65 static void list_cmd(void),
70 parse_args(int c, char *v[]);
71 static int replace_cmd(void);
78 fprintf(stderr, "crontab: usage error: %s\n", msg);
79 fprintf(stderr, "%s\n%s\n",
80 "usage: crontab [-u user] file",
81 " crontab [-u user] { -e | -l | -r }");
94 ProgramName = argv[0];
97 setlocale(LC_ALL, "");
103 parse_args(argc, argv); /* sets many globals, opens a file */
106 if (!allowed(User)) {
107 warnx("you (%s) are not allowed to use this program", User);
108 log_it(RealUser, Pid, "AUTH", "crontab command not allowed");
111 exitstatus = OK_EXIT;
113 case opt_list: list_cmd();
115 case opt_delete: delete_cmd();
117 case opt_edit: edit_cmd();
119 case opt_replace: if (replace_cmd() < 0)
120 exitstatus = ERROR_EXIT;
131 parse_args(argc, argv)
137 if (!(pw = getpwuid(getuid())))
138 errx(ERROR_EXIT, "your UID isn't in the passwd file, bailing out");
139 (void) strncpy(User, pw->pw_name, (sizeof User)-1);
140 User[(sizeof User)-1] = '\0';
141 strcpy(RealUser, User);
143 Option = opt_unknown;
144 while ((argch = getopt(argc, argv, "u:lerx:")) != -1) {
147 if (!set_debug_flags(optarg))
148 usage("bad debug option");
151 if (getuid() != ROOT_UID)
152 errx(ERROR_EXIT, "must be privileged to use -u");
153 if (!(pw = getpwnam(optarg)))
154 errx(ERROR_EXIT, "user `%s' unknown", optarg);
155 (void) strncpy(User, pw->pw_name, (sizeof User)-1);
156 User[(sizeof User)-1] = '\0';
159 if (Option != opt_unknown)
160 usage("only one operation permitted");
164 if (Option != opt_unknown)
165 usage("only one operation permitted");
169 if (Option != opt_unknown)
170 usage("only one operation permitted");
174 usage("unrecognized option");
180 if (Option != opt_unknown) {
181 if (argv[optind] != NULL) {
182 usage("no arguments permitted after this option");
185 if (argv[optind] != NULL) {
186 Option = opt_replace;
187 (void) strncpy (Filename, argv[optind], (sizeof Filename)-1);
188 Filename[(sizeof Filename)-1] = '\0';
191 usage("file name must be specified for replace");
195 if (Option == opt_replace) {
196 /* we have to open the file here because we're going to
197 * chdir(2) into /var/cron before we get around to
200 if (!strcmp(Filename, "-")) {
203 /* relinquish the setuid status of the binary during
204 * the open, lest nonroot users read files they should
205 * not be able to read. we can't use access() here
206 * since there's a race condition. thanks go out to
207 * Arnt Gulbrandsen <agulbra@pvv.unit.no> for spotting
211 if (swap_uids() < OK)
212 err(ERROR_EXIT, "swapping uids");
213 if (!(NewCrontab = fopen(Filename, "r")))
214 err(ERROR_EXIT, "%s", Filename);
215 if (swap_uids() < OK)
216 err(ERROR_EXIT, "swapping uids back");
220 Debug(DMISC, ("user=%s, file=%s, option=%s\n",
221 User, Filename, Options[(int)Option]))
231 log_it(RealUser, Pid, "LIST", User);
232 (void) sprintf(n, CRON_TAB(User));
233 if (!(f = fopen(n, "r"))) {
235 errx(ERROR_EXIT, "no crontab for %s", User);
237 err(ERROR_EXIT, "%s", n);
240 /* file is open. copy to stdout, close.
244 /* ignore the top few comments since we probably put them there.
246 for (x = 0; x < NHEADER_LINES; x++) {
254 while (EOF != (ch = get_char(f)))
261 while (EOF != (ch = get_char(f)))
272 if (isatty(STDIN_FILENO)) {
273 (void)fprintf(stderr, "remove crontab for %s? ", User);
274 first = ch = getchar();
275 while (ch != '\n' && ch != EOF)
277 if (first != 'y' && first != 'Y')
281 log_it(RealUser, Pid, "DELETE", User);
282 (void) sprintf(n, CRON_TAB(User));
285 errx(ERROR_EXIT, "no crontab for %s", User);
287 err(ERROR_EXIT, "%s", n);
298 fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg);
304 char n[MAX_FNAME], q[MAX_TEMPSTR], *editor;
307 struct stat statbuf, fsbuf;
313 log_it(RealUser, Pid, "BEGIN EDIT", User);
314 (void) sprintf(n, CRON_TAB(User));
315 if (!(f = fopen(n, "r"))) {
317 err(ERROR_EXIT, "%s", n);
318 warnx("no crontab for %s - using an empty one", User);
319 if (!(f = fopen(_PATH_DEVNULL, "r")))
320 err(ERROR_EXIT, _PATH_DEVNULL);
324 (void) sprintf(Filename, "/tmp/crontab.XXXXXXXXXX");
325 if ((t = mkstemp(Filename)) == -1) {
326 warn("%s", Filename);
332 if (fchown(t, getuid(), getgid()) < 0) {
334 if (chown(Filename, getuid(), getgid()) < 0) {
339 if (!(NewCrontab = fdopen(t, "r+"))) {
346 /* ignore the top few comments since we probably put them there.
348 for (x = 0; x < NHEADER_LINES; x++) {
353 putc(ch, NewCrontab);
356 while (EOF != (ch = get_char(f)))
363 /* copy the rest of the crontab (if any) to the temp file.
366 while (EOF != (ch = get_char(f)))
367 putc(ch, NewCrontab);
369 if (fflush(NewCrontab))
370 err(ERROR_EXIT, "%s", Filename);
371 if (fstat(t, &fsbuf) < 0) {
372 warn("unable to fstat temp file");
376 if (stat(Filename, &statbuf) < 0) {
378 fatal: unlink(Filename);
381 if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino)
382 errx(ERROR_EXIT, "temp file must be edited in place");
383 mtime = statbuf.st_mtime;
385 if ((!(editor = getenv("VISUAL")))
386 && (!(editor = getenv("EDITOR")))
391 /* we still have the file open. editors will generally rewrite the
392 * original file rather than renaming/unlinking it and starting a
393 * new one; even backup files are supposed to be made by copying
394 * rather than by renaming. if some editor does not support this,
395 * then don't use it. the security problems are more severe if we
396 * close and reopen the file around the edit.
399 switch (pid = fork()) {
405 if (setuid(getuid()) < 0)
406 err(ERROR_EXIT, "setuid(getuid())");
407 if (chdir("/tmp") < 0)
408 err(ERROR_EXIT, "chdir(/tmp)");
409 if (strlen(editor) + strlen(Filename) + 2 >= MAX_TEMPSTR)
410 errx(ERROR_EXIT, "editor or filename too long");
411 execlp(editor, editor, Filename, NULL);
412 err(ERROR_EXIT, "%s", editor);
422 f[0] = signal(SIGHUP, SIG_IGN);
423 f[1] = signal(SIGINT, SIG_IGN);
424 f[2] = signal(SIGTERM, SIG_IGN);
425 xpid = wait(&waiter);
426 signal(SIGHUP, f[0]);
427 signal(SIGINT, f[1]);
428 signal(SIGTERM, f[2]);
431 warnx("wrong PID (%d != %d) from \"%s\"", xpid, pid, editor);
434 if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) {
435 warnx("\"%s\" exited with status %d", editor, WEXITSTATUS(waiter));
438 if (WIFSIGNALED(waiter)) {
439 warnx("\"%s\" killed; signal %d (%score dumped)",
440 editor, WTERMSIG(waiter), WCOREDUMP(waiter) ?"" :"no ");
443 if (stat(Filename, &statbuf) < 0) {
447 if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino)
448 errx(ERROR_EXIT, "temp file must be edited in place");
449 if (mtime == statbuf.st_mtime) {
450 warnx("no changes made to crontab");
453 warnx("installing new crontab");
454 switch (replace_cmd()) {
459 printf("Do you want to retry the same edit? ");
462 (void) fgets(q, sizeof q, stdin);
463 switch (islower(q[0]) ? q[0] : tolower(q[0])) {
469 fprintf(stderr, "Enter Y or N\n");
475 warnx("edits left in %s", Filename);
478 warnx("panic: bad switch() in replace_cmd()");
484 log_it(RealUser, Pid, "END EDIT", User);
488 /* returns 0 on success
490 * -2 on install error
494 char n[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME];
498 time_t now = time(NULL);
499 char **envp = env_init();
502 warnx("cannot allocate memory");
506 (void) sprintf(n, "tmp.%d", Pid);
507 (void) sprintf(tn, CRON_TAB(n));
508 if (!(tmp = fopen(tn, "w+"))) {
513 /* write a signature at the top of the file.
515 * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code.
517 fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n");
518 fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now));
519 fprintf(tmp, "# (Cron version -- %s)\n", version);
521 /* copy the crontab to the tmp
525 while (EOF != (ch = get_char(NewCrontab)))
527 ftruncate(fileno(tmp), ftell(tmp));
528 fflush(tmp); rewind(tmp);
531 warnx("error while writing new crontab to %s", tn);
532 fclose(tmp); unlink(tn);
536 /* check the syntax of the file being installed.
539 /* BUG: was reporting errors after the EOF if there were any errors
540 * in the file proper -- kludged it by stopping after first error.
543 Set_LineNum(1 - NHEADER_LINES)
544 CheckErrorCount = 0; eof = FALSE;
545 while (!CheckErrorCount && !eof) {
546 switch (load_env(envstr, tmp)) {
551 e = load_entry(tmp, check_error, pw, envp);
560 if (CheckErrorCount != 0) {
561 warnx("errors in crontab file, can't install");
562 fclose(tmp); unlink(tn);
567 if (fchown(fileno(tmp), ROOT_UID, -1) < OK)
569 if (chown(tn, ROOT_UID, -1) < OK)
573 fclose(tmp); unlink(tn);
578 if (fchmod(fileno(tmp), 0600) < OK)
580 if (chmod(tn, 0600) < OK)
584 fclose(tmp); unlink(tn);
588 if (fclose(tmp) == EOF) {
594 (void) sprintf(n, CRON_TAB(User));
596 warn("error renaming %s to %s", tn, n);
600 log_it(RealUser, Pid, "REPLACE", User);
611 struct timeval tvs[2];
614 (void) gettimeofday(&tvs[0], &tz);
616 if (utimes(SPOOL_DIR, tvs) < OK) {
617 warn("can't update mtime on spooldir %s", SPOOL_DIR);
621 if (utime(SPOOL_DIR, NULL) < OK) {
622 warn("can't update mtime on spooldir %s", SPOOL_DIR);
625 #endif /*USE_UTIMES*/