/[pkg-cron]/trunk/crontab.c
ViewVC logotype

Contents of /trunk/crontab.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (show annotations) (download)
Sun Apr 11 19:34:04 1999 UTC (14 years, 1 month ago) by steveg
File MIME type: text/plain
File size: 14794 byte(s)
Added all debian changes
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
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
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
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 (void) strncpy(User, pw->pw_name, (sizeof User)-1);
147 User[(sizeof User)-1] = '\0';
148 strcpy(RealUser, User);
149 Filename[0] = '\0';
150 Option = opt_unknown;
151 while (EOF != (argch = getopt(argc, argv, "u:lerx:"))) {
152 switch (argch) {
153 case 'x':
154 if (!set_debug_flags(optarg))
155 usage("bad debug option");
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 }
164 if (!(pw = getpwnam(optarg)))
165 {
166 fprintf(stderr, "%s: user `%s' unknown\n",
167 ProgramName, optarg);
168 exit(ERROR_EXIT);
169 }
170 (void) strncpy(User, pw->pw_name, (sizeof User)-1);
171 User[(sizeof User)-1] = '\0';
172 break;
173 case 'l':
174 if (Option != opt_unknown)
175 usage("only one operation permitted");
176 Option = opt_list;
177 break;
178 case 'r':
179 if (Option != opt_unknown)
180 usage("only one operation permitted");
181 Option = opt_delete;
182 break;
183 case 'e':
184 if (Option != opt_unknown)
185 usage("only one operation permitted");
186 Option = opt_edit;
187 break;
188 default:
189 usage("unrecognized option");
190 }
191 }
192
193 endpwent();
194
195 if (Option != opt_unknown) {
196 if (argv[optind] != NULL) {
197 usage("no arguments permitted after this option");
198 }
199 } else {
200 if (argv[optind] != NULL) {
201 Option = opt_replace;
202 (void) strncpy (Filename, argv[optind], (sizeof Filename)-1);
203 Filename[(sizeof Filename)-1] = '\0';
204
205 } else {
206 usage("file name must be specified for replace");
207 }
208 }
209
210 if (Option == opt_replace) {
211 /* we have to open the file here because we're going to
212 * chdir(2) into /var/cron before we get around to
213 * reading the file.
214 */
215 if (!strcmp(Filename, "-")) {
216 NewCrontab = stdin;
217 } else {
218 /* relinquish the setuid status of the binary during
219 * the open, lest nonroot users read files they should
220 * not be able to read. we can't use access() here
221 * since there's a race condition. thanks go out to
222 * Arnt Gulbrandsen <agulbra@pvv.unit.no> for spotting
223 * the race.
224 */
225
226 if (swap_uids() < OK) {
227 perror("swapping uids");
228 exit(ERROR_EXIT);
229 }
230 if (!(NewCrontab = fopen(Filename, "r"))) {
231 perror(Filename);
232 exit(ERROR_EXIT);
233 }
234 if (swap_uids_back() < OK) {
235 perror("swapping uids back");
236 exit(ERROR_EXIT);
237 }
238 }
239 }
240
241 Debug(DMISC, ("user=%s, file=%s, option=%s\n",
242 User, Filename, Options[(int)Option]))
243 }
244
245
246 static void
247 list_cmd() {
248 char n[MAX_FNAME];
249 FILE *f;
250 int ch;
251 #ifdef DEBIAN
252 int x;
253 char *ctnh;
254 #endif
255
256 log_it(RealUser, Pid, "LIST", User);
257 (void) sprintf(n, CRON_TAB(User));
258 if (!(f = fopen(n, "r"))) {
259 if (errno == ENOENT)
260 fprintf(stderr, "no crontab for %s\n", User);
261 else
262 perror(n);
263 exit(ERROR_EXIT);
264 }
265
266 /* file is open. copy to stdout, close.
267 */
268 Set_LineNum(1)
269 #ifdef DEBIAN
270 /* DEBIAN: Don't list header lines if CRONTAB_NOHEADER is
271 'Y'. Later we'll change this to the default */
272 /* ignore the top few comments since we probably put them there.
273 */
274 if ((ctnh = getenv("CRONTAB_NOHEADER")) &&
275 toupper(*ctnh) == 'Y') {
276 for (x = 0; x < NHEADER_LINES; x++) {
277 ch = get_char(f);
278 if (EOF == ch)
279 break;
280 if ('#' != ch) {
281 putc(ch, NewCrontab);
282 break;
283 }
284 while (EOF != (ch = get_char(f)))
285 if (ch == '\n')
286 break;
287 if (EOF == ch)
288 break;
289 }
290 }
291 #endif
292 while (EOF != (ch = get_char(f)))
293 putchar(ch);
294 fclose(f);
295 }
296
297
298 static void
299 delete_cmd() {
300 char n[MAX_FNAME];
301
302 log_it(RealUser, Pid, "DELETE", User);
303 (void) sprintf(n, CRON_TAB(User));
304 if (unlink(n)) {
305 if (errno == ENOENT)
306 fprintf(stderr, "no crontab for %s\n", User);
307 else
308 perror(n);
309 exit(ERROR_EXIT);
310 }
311 poke_daemon();
312 }
313
314
315 static void
316 check_error(msg)
317 char *msg;
318 {
319 CheckErrorCount++;
320 fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg);
321 }
322
323
324 static void
325 edit_cmd() {
326 char n[MAX_FNAME], q[MAX_TEMPSTR], *editor;
327 FILE *f;
328 int ch, t, x;
329 struct stat statbuf;
330 time_t mtime;
331 WAIT_T waiter;
332 PID_T pid, xpid;
333 mode_t um;
334
335 log_it(RealUser, Pid, "BEGIN EDIT", User);
336 (void) sprintf(n, CRON_TAB(User));
337 if (!(f = fopen(n, "r"))) {
338 if (errno != ENOENT) {
339 perror(n);
340 exit(ERROR_EXIT);
341 }
342 fprintf(stderr, "no crontab for %s - using an empty one\n",
343 User);
344 if (!(f = fopen("/dev/null", "r"))) {
345 perror("/dev/null");
346 exit(ERROR_EXIT);
347 }
348 }
349
350 um = umask(077);
351
352 if (getenv("TMPDIR")) {
353 strcpy(Filename, getenv("TMPDIR"));
354 } else {
355 strcpy(Filename,"/tmp");
356 }
357
358 (void) sprintf(Filename+strlen(Filename), "/crontab.XXXXXXXXXX");
359 if ((t = mkstemp(Filename)) == -1) {
360 perror(Filename);
361 (void) umask(um);
362 goto fatal;
363 }
364 (void) umask(um);
365 #ifdef HAS_FCHOWN
366 if (fchown(t, getuid(), getgid()) < 0) {
367 #else
368 if (chown(Filename, getuid(), getgid()) < 0) {
369 #endif
370 perror("fchown");
371 goto fatal;
372 }
373 if (!(NewCrontab = fdopen(t, "r+"))) {
374 perror("fdopen");
375 goto fatal;
376 }
377
378 Set_LineNum(1)
379
380 /* ignore the top few comments since we probably put them there.
381 */
382 for (x = 0; x < NHEADER_LINES; x++) {
383 ch = get_char(f);
384 if (EOF == ch)
385 break;
386 if ('#' != ch) {
387 putc(ch, NewCrontab);
388 break;
389 }
390 while (EOF != (ch = get_char(f)))
391 if (ch == '\n')
392 break;
393 if (EOF == ch)
394 break;
395 }
396
397 /* copy the rest of the crontab (if any) to the temp file.
398 */
399 if (EOF != ch)
400 while (EOF != (ch = get_char(f)))
401 putc(ch, NewCrontab);
402 fclose(f);
403 if (fflush(NewCrontab) < OK) {
404 perror(Filename);
405 exit(ERROR_EXIT);
406 }
407 again:
408 rewind(NewCrontab);
409 if (ferror(NewCrontab)) {
410 fprintf(stderr, "%s: error while writing new crontab to %s\n",
411 ProgramName, Filename);
412 fatal: unlink(Filename);
413 exit(ERROR_EXIT);
414 }
415 if (fstat(t, &statbuf) < 0) {
416 perror("fstat");
417 goto fatal;
418 }
419 mtime = statbuf.st_mtime;
420
421 if ((!(editor = getenv("VISUAL")))
422 && (!(editor = getenv("EDITOR")))
423 ) {
424 editor = EDITOR;
425 }
426
427 /* we still have the file open. editors will generally rewrite the
428 * original file rather than renaming/unlinking it and starting a
429 * new one; even backup files are supposed to be made by copying
430 * rather than by renaming. if some editor does not support this,
431 * then don't use it. the security problems are more severe if we
432 * close and reopen the file around the edit.
433 */
434
435 switch (pid = fork()) {
436 case -1:
437 perror("fork");
438 goto fatal;
439 case 0:
440 /* child */
441 if (setuid(getuid()) < 0) {
442 perror("setuid(getuid())");
443 exit(ERROR_EXIT);
444 }
445 if (chdir("/tmp") < 0) {
446 perror("chdir(/tmp)");
447 exit(ERROR_EXIT);
448 }
449 if (strlen(editor) + strlen(Filename) + 2 >= MAX_TEMPSTR) {
450 fprintf(stderr, "%s: editor or filename too long\n",
451 ProgramName);
452 exit(ERROR_EXIT);
453 }
454 sprintf(q, "%s %s", editor, Filename);
455 execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", q, NULL);
456 perror(editor);
457 exit(ERROR_EXIT);
458 /*NOTREACHED*/
459 default:
460 /* parent */
461 break;
462 }
463
464 /* parent */
465 xpid = wait(&waiter);
466 if (xpid != pid) {
467 fprintf(stderr, "%s: wrong PID (%d != %d) from \"%s\"\n",
468 ProgramName, xpid, pid, editor);
469 goto fatal;
470 }
471 if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) {
472 fprintf(stderr, "%s: \"%s\" exited with status %d\n",
473 ProgramName, editor, WEXITSTATUS(waiter));
474 goto fatal;
475 }
476 if (WIFSIGNALED(waiter)) {
477 fprintf(stderr,
478 "%s: \"%s\" killed; signal %d (%score dumped)\n",
479 ProgramName, editor, WTERMSIG(waiter),
480 WCOREDUMP(waiter) ?"" :"no ");
481 goto fatal;
482 }
483 if (fstat(t, &statbuf) < 0) {
484 perror("fstat");
485 goto fatal;
486 }
487 if (mtime == statbuf.st_mtime) {
488 fprintf(stderr, "%s: no changes made to crontab\n",
489 ProgramName);
490 goto remove;
491 }
492 fprintf(stderr, "%s: installing new crontab\n", ProgramName);
493 switch (replace_cmd()) {
494 case 0:
495 break;
496 case -1:
497 for (;;) {
498 printf("Do you want to retry the same edit? ");
499 fflush(stdout);
500 q[0] = '\0';
501 (void) fgets(q, sizeof q, stdin);
502 switch (islower(q[0]) ? q[0] : tolower(q[0])) {
503 case 'y':
504 goto again;
505 case 'n':
506 goto abandon;
507 default:
508 fprintf(stderr, "Enter Y or N\n");
509 }
510 }
511 /*NOTREACHED*/
512 case -2:
513 abandon:
514 fprintf(stderr, "%s: edits left in %s\n",
515 ProgramName, Filename);
516 goto done;
517 default:
518 fprintf(stderr, "%s: panic: bad switch() in replace_cmd()\n",
519 ProgramName);
520 goto fatal;
521 }
522 remove:
523 unlink(Filename);
524 done:
525 log_it(RealUser, Pid, "END EDIT", User);
526 }
527
528
529 /* returns 0 on success
530 * -1 on syntax error
531 * -2 on install error
532 */
533 static int
534 replace_cmd() {
535 char n[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME];
536 FILE *tmp;
537 int ch, eof;
538 entry *e;
539 time_t now = time(NULL);
540 char **envp = env_init();
541
542 if (envp == NULL) {
543 fprintf(stderr, "%s: Cannot allocate memory.\n", ProgramName);
544 return (-2);
545 }
546
547 (void) sprintf(n, "tmp.%d", Pid);
548 (void) sprintf(tn, CRON_TAB(n));
549 if (!(tmp = fopen(tn, "w+"))) {
550 perror(tn);
551 return (-2);
552 }
553
554 /* write a signature at the top of the file.
555 *
556 * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code.
557 */
558 fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n");
559 fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now));
560 fprintf(tmp, "# (Cron version -- %s)\n", rcsid);
561
562 /* copy the crontab to the tmp
563 */
564 rewind(NewCrontab);
565 Set_LineNum(1)
566 while (EOF != (ch = get_char(NewCrontab)))
567 putc(ch, tmp);
568 ftruncate(fileno(tmp), ftell(tmp));
569 fflush(tmp); rewind(tmp);
570
571 if (ferror(tmp)) {
572 fprintf(stderr, "%s: error while writing new crontab to %s\n",
573 ProgramName, tn);
574 fclose(tmp); unlink(tn);
575 return (-2);
576 }
577
578 /* check the syntax of the file being installed.
579 */
580
581 /* BUG: was reporting errors after the EOF if there were any errors
582 * in the file proper -- kludged it by stopping after first error.
583 * vix 31mar87
584 */
585 Set_LineNum(1 - NHEADER_LINES)
586 CheckErrorCount = 0; eof = FALSE;
587 while (!CheckErrorCount && !eof) {
588 switch (load_env(envstr, tmp)) {
589 case ERR:
590 eof = TRUE;
591 break;
592 case FALSE:
593 e = load_entry(tmp, check_error, pw, envp);
594 if (e)
595 free(e);
596 break;
597 case TRUE:
598 break;
599 }
600 }
601
602 if (CheckErrorCount != 0) {
603 fprintf(stderr, "errors in crontab file, can't install.\n");
604 fclose(tmp); unlink(tn);
605 return (-1);
606 }
607
608 #ifdef HAS_FCHOWN
609 if (fchown(fileno(tmp), ROOT_UID, -1) < OK)
610 #else
611 if (chown(tn, ROOT_UID, -1) < OK)
612 #endif
613 {
614 perror("chown");
615 fclose(tmp); unlink(tn);
616 return (-2);
617 }
618
619 #ifdef HAS_FCHMOD
620 if (fchmod(fileno(tmp), 0600) < OK)
621 #else
622 if (chmod(tn, 0600) < OK)
623 #endif
624 {
625 perror("chown");
626 fclose(tmp); unlink(tn);
627 return (-2);
628 }
629
630 if (fclose(tmp) == EOF) {
631 perror("fclose");
632 unlink(tn);
633 return (-2);
634 }
635
636 (void) sprintf(n, CRON_TAB(User));
637 if (rename(tn, n)) {
638 fprintf(stderr, "%s: error renaming %s to %s\n",
639 ProgramName, tn, n);
640 perror("rename");
641 unlink(tn);
642 return (-2);
643 }
644 log_it(RealUser, Pid, "REPLACE", User);
645
646 poke_daemon();
647
648 return (0);
649 }
650
651
652 static void
653 poke_daemon() {
654 #ifdef USE_UTIMES
655 struct timeval tvs[2];
656 struct timezone tz;
657
658 (void) gettimeofday(&tvs[0], &tz);
659 tvs[1] = tvs[0];
660 if (utimes(SPOOL_DIR, tvs) < OK) {
661 fprintf(stderr, "crontab: can't update mtime on spooldir\n");
662 perror(SPOOL_DIR);
663 return;
664 }
665 #else
666 if (utime(SPOOL_DIR, NULL) < OK) {
667 fprintf(stderr, "crontab: can't update mtime on spooldir\n");
668 perror(SPOOL_DIR);
669 return;
670 }
671 #endif /*USE_UTIMES*/
672 }

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.5