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