Annotation of src/usr.bin/calendar/calendar.c, Revision 1.34
1.34 ! semarie 1: /* $OpenBSD: calendar.c,v 1.33 2015/11/16 17:49:35 pascal Exp $ */
1.1 deraadt 2:
3: /*
4: * Copyright (c) 1989, 1993, 1994
5: * The Regents of the University of California. All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
1.20 millert 15: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
31:
1.15 pjanzen 32: #include <sys/stat.h>
33: #include <sys/types.h>
34: #include <sys/wait.h>
1.1 deraadt 35: #include <err.h>
36: #include <errno.h>
1.5 millert 37: #include <locale.h>
1.15 pjanzen 38: #include <login_cap.h>
1.1 deraadt 39: #include <pwd.h>
1.15 pjanzen 40: #include <signal.h>
1.1 deraadt 41: #include <stdio.h>
42: #include <stdlib.h>
43: #include <string.h>
1.31 deraadt 44: #include <limits.h>
1.5 millert 45: #include <time.h>
1.1 deraadt 46: #include <unistd.h>
47:
48: #include "pathnames.h"
1.5 millert 49: #include "calendar.h"
1.1 deraadt 50:
1.15 pjanzen 51: char *calendarFile = "calendar"; /* default calendar file */
52: char *calendarHome = ".calendar"; /* HOME */
53: char *calendarNoMail = "nomail"; /* don't sent mail if this file exists */
54:
1.1 deraadt 55: struct passwd *pw;
1.5 millert 56: int doall = 0;
57: time_t f_time = 0;
1.16 mickey 58: int bodun_always = 0;
1.1 deraadt 59:
1.5 millert 60: int f_dayAfter = 0; /* days after current date */
61: int f_dayBefore = 0; /* days before current date */
1.24 mickey 62: int f_SetdayAfter = 0; /* calendar invoked with -A */
1.1 deraadt 63:
1.12 pjanzen 64: struct specialev spev[NUMEV];
65:
1.17 millert 66: void childsig(int);
1.15 pjanzen 67:
1.1 deraadt 68: int
1.21 deraadt 69: main(int argc, char *argv[])
1.1 deraadt 70: {
1.15 pjanzen 71: int ch;
1.31 deraadt 72: const char *errstr;
1.1 deraadt 73: char *caldir;
74:
1.12 pjanzen 75: (void)setlocale(LC_ALL, "");
1.5 millert 76:
1.19 millert 77: while ((ch = getopt(argc, argv, "abf:t:A:B:-")) != -1)
1.1 deraadt 78: switch (ch) {
79: case '-': /* backward contemptible */
80: case 'a':
1.5 millert 81: if (getuid())
1.13 millert 82: errx(1, "%s", strerror(EPERM));
1.1 deraadt 83: doall = 1;
84: break;
1.5 millert 85:
1.16 mickey 86: case 'b':
1.29 deraadt 87: bodun_always = 1;
1.16 mickey 88: break;
89:
1.5 millert 90: case 'f': /* other calendar file */
91: calendarFile = optarg;
92: break;
93:
94: case 't': /* other date, undocumented, for tests */
1.12 pjanzen 95: if ((f_time = Mktime(optarg)) <= 0)
1.11 pjanzen 96: errx(1, "specified date is outside allowed range");
1.5 millert 97: break;
98:
99: case 'A': /* days after current date */
1.31 deraadt 100: f_dayAfter = strtonum(optarg, 0, INT_MAX, &errstr);
101: if (errstr)
102: errx(1, "-A %s: %s", optarg, errstr);
1.24 mickey 103: f_SetdayAfter = 1;
1.5 millert 104: break;
105:
106: case 'B': /* days before current date */
1.31 deraadt 107: f_dayBefore = strtonum(optarg, 0, INT_MAX, &errstr);
108: if (errstr)
109: errx(1, "-B %s: %s", optarg, errstr);
1.5 millert 110: break;
111:
1.1 deraadt 112: default:
113: usage();
114: }
115: argc -= optind;
116: argv += optind;
117:
118: if (argc)
119: usage();
1.33 pascal 120:
121: if (doall) {
1.34 ! semarie 122: if (pledge("stdio rpath tmppath fattr getpw id proc exec", NULL)
1.33 pascal 123: == -1)
124: err(1, "pledge");
125: } else {
126: if (pledge("stdio rpath proc exec", NULL) == -1)
127: err(1, "pledge");
128: }
1.1 deraadt 129:
1.5 millert 130: /* use current time */
131: if (f_time <= 0)
132: (void)time(&f_time);
133:
1.12 pjanzen 134: if (f_dayBefore) {
135: /* Move back in time and only look forwards */
136: f_dayAfter += f_dayBefore;
137: f_time -= SECSPERDAY * f_dayBefore;
138: f_dayBefore = 0;
139: }
140: settime(&f_time);
1.5 millert 141:
1.10 millert 142: if (doall) {
1.15 pjanzen 143: pid_t kid, deadkid;
144: int kidstat, kidreaped, runningkids;
145: int acstat;
146: struct stat sbuf;
147: time_t t;
148: unsigned int sleeptime;
149:
150: signal(SIGCHLD, childsig);
151: runningkids = 0;
152: t = time(NULL);
1.1 deraadt 153: while ((pw = getpwent()) != NULL) {
1.15 pjanzen 154: acstat = 0;
155: /* Avoid unnecessary forks. The calendar file is only
156: * opened as the user later; if it can't be opened,
157: * it's no big deal. Also, get to correct directory.
158: * Note that in an NFS environment root may get EACCES
159: * on a chdir(), in which case we have to fork. As long as
160: * we can chdir() we can stat(), unless the user is
161: * modifying permissions while this is running.
162: */
163: if (chdir(pw->pw_dir)) {
164: if (errno == EACCES)
165: acstat = 1;
166: else
167: continue;
168: }
169: if (stat(calendarFile, &sbuf) != 0) {
170: if (chdir(calendarHome)) {
171: if (errno == EACCES)
172: acstat = 1;
173: else
174: continue;
175: }
176: if (stat(calendarNoMail, &sbuf) == 0 ||
177: stat(calendarFile, &sbuf) != 0)
178: continue;
179: }
180: sleeptime = USERTIMEOUT;
181: switch ((kid = fork())) {
182: case -1: /* error */
183: warn("fork");
184: continue;
185: case 0: /* child */
1.28 otto 186: (void)setpgid(getpid(), getpid());
1.15 pjanzen 187: (void)setlocale(LC_ALL, "");
188: if (setusercontext(NULL, pw, pw->pw_uid,
189: LOGIN_SETALL ^ LOGIN_SETLOGIN))
1.18 deraadt 190: err(1, "unable to set user context (uid %u)",
191: pw->pw_uid);
1.15 pjanzen 192: if (acstat) {
193: if (chdir(pw->pw_dir) ||
194: stat(calendarFile, &sbuf) != 0 ||
1.32 zhuk 195: chdir(calendarHome) ||
1.15 pjanzen 196: stat(calendarNoMail, &sbuf) == 0 ||
197: stat(calendarFile, &sbuf) != 0)
198: exit(0);
199: }
1.1 deraadt 200: cal();
1.15 pjanzen 201: exit(0);
202: }
203: /* parent: wait a reasonable time, then kill child if
204: * necessary.
205: */
206: runningkids++;
207: kidreaped = 0;
208: do {
209: sleeptime = sleep(sleeptime);
210: /* Note that there is the possibility, if the sleep
211: * stops early due to some other signal, of the child
212: * terminating and not getting detected during the next
213: * sleep. In that unlikely worst case, we just sleep
214: * too long for that user.
215: */
216: for (;;) {
217: deadkid = waitpid(-1, &kidstat, WNOHANG);
218: if (deadkid <= 0)
219: break;
220: runningkids--;
221: if (deadkid == kid) {
222: kidreaped = 1;
223: sleeptime = 0;
224: }
225: }
226: } while (sleeptime);
227:
228: if (!kidreaped) {
229: /* It doesn't _really_ matter if the kill fails, e.g.
230: * if there's only a zombie now.
231: */
1.28 otto 232: if (getpgid(kid) != getpgrp())
233: (void)killpg(getpgid(kid), SIGTERM);
234: else
235: (void)kill(kid, SIGTERM);
1.18 deraadt 236: warnx("uid %u did not finish in time", pw->pw_uid);
1.12 pjanzen 237: }
1.15 pjanzen 238: if (time(NULL) - t >= SECSPERDAY)
1.25 deraadt 239: errx(2, "'calendar -a' took more than a day; "
240: "stopped at uid %u",
1.18 deraadt 241: pw->pw_uid);
1.15 pjanzen 242: }
243: for (;;) {
244: deadkid = waitpid(-1, &kidstat, WNOHANG);
245: if (deadkid <= 0)
246: break;
247: runningkids--;
1.1 deraadt 248: }
1.15 pjanzen 249: if (runningkids)
1.25 deraadt 250: warnx("%d child processes still running when "
251: "'calendar -a' finished", runningkids);
252: } else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
1.5 millert 253: if(!chdir(caldir))
254: cal();
1.1 deraadt 255: } else
256: cal();
1.5 millert 257:
1.1 deraadt 258: exit(0);
259: }
260:
261:
262: void
1.21 deraadt 263: usage(void)
1.1 deraadt 264: {
1.5 millert 265: (void)fprintf(stderr,
1.22 jmc 266: "usage: calendar [-ab] [-A num] [-B num] [-f calendarfile] "
1.27 jmc 267: "[-t [[[cc]yy]mm]dd]\n");
1.1 deraadt 268: exit(1);
1.15 pjanzen 269: }
270:
271:
272: void
1.21 deraadt 273: childsig(int signo)
1.15 pjanzen 274: {
1.1 deraadt 275: }