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