calendar.c revision 1.53 1 1.53 agc /* $NetBSD: calendar.c,v 1.53 2016/06/03 02:06:40 agc Exp $ */
2 1.7 glass
3 1.1 cgd /*
4 1.7 glass * Copyright (c) 1989, 1993, 1994
5 1.7 glass * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.28 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.11 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.46 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
35 1.46 lukem The Regents of the University of California. All rights reserved.");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.7 glass #if 0
40 1.8 jtc static char sccsid[] = "@(#)calendar.c 8.4 (Berkeley) 1/7/95";
41 1.7 glass #endif
42 1.53 agc __RCSID("$NetBSD: calendar.c,v 1.53 2016/06/03 02:06:40 agc Exp $");
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.52 dholland #include <sys/ioctl.h>
47 1.1 cgd #include <sys/time.h>
48 1.1 cgd #include <sys/stat.h>
49 1.1 cgd #include <sys/uio.h>
50 1.7 glass #include <sys/wait.h>
51 1.7 glass
52 1.51 dholland #include <assert.h>
53 1.7 glass #include <ctype.h>
54 1.7 glass #include <err.h>
55 1.7 glass #include <errno.h>
56 1.7 glass #include <fcntl.h>
57 1.1 cgd #include <pwd.h>
58 1.44 christos #include <stdbool.h>
59 1.7 glass #include <stdio.h>
60 1.7 glass #include <stdlib.h>
61 1.7 glass #include <string.h>
62 1.13 kleink #include <time.h>
63 1.1 cgd #include <tzfile.h>
64 1.1 cgd #include <unistd.h>
65 1.7 glass
66 1.1 cgd #include "pathnames.h"
67 1.1 cgd
68 1.53 agc #define CALENDAR_VERSION "calendar-20160601"
69 1.53 agc
70 1.48 wiz /* flags used by calendar file parser */
71 1.48 wiz #define F_ISMONTH 0x01
72 1.48 wiz #define F_ISDAY 0x02
73 1.48 wiz #define F_ISDOW 0x04
74 1.48 wiz #define F_WILDMONTH 0x10
75 1.48 wiz #define F_WILDDAY 0x20
76 1.48 wiz
77 1.44 christos static unsigned short lookahead = 1;
78 1.44 christos static unsigned short weekend = 2;
79 1.44 christos static char *fname = NULL;
80 1.44 christos static char *datestr = NULL;
81 1.44 christos static const char *defaultnames[] = {"calendar", ".calendar", _PATH_SYSTEM_CALENDAR, NULL};
82 1.26 christos static struct passwd *pw;
83 1.26 christos static char path[MAXPATHLEN + 1];
84 1.44 christos static bool doall = false;
85 1.44 christos static bool cpp_restricted = false;
86 1.26 christos
87 1.26 christos /* 1-based month, 0-based days, cumulative */
88 1.44 christos static const int daytab[][14] = {
89 1.26 christos { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
90 1.26 christos { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
91 1.26 christos };
92 1.26 christos static struct tm *tp;
93 1.44 christos static const int *cumdays;
94 1.44 christos static int offset, yrdays;
95 1.26 christos static char dayname[10];
96 1.26 christos
97 1.26 christos static struct iovec header[] = {
98 1.44 christos { __UNCONST("From: "), 6 },
99 1.26 christos { NULL, 0 },
100 1.44 christos { __UNCONST(" (Reminder Service)\nTo: "), 24 },
101 1.26 christos { NULL, 0 },
102 1.44 christos { __UNCONST("\nSubject: "), 10 },
103 1.26 christos { NULL, 0 },
104 1.44 christos { __UNCONST("'s Calendar\nPrecedence: bulk\n\n"), 30 },
105 1.26 christos };
106 1.26 christos
107 1.44 christos static const char *days[] = {
108 1.26 christos "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
109 1.26 christos };
110 1.26 christos
111 1.44 christos static const char *months[] = {
112 1.26 christos "jan", "feb", "mar", "apr", "may", "jun",
113 1.26 christos "jul", "aug", "sep", "oct", "nov", "dec", NULL,
114 1.26 christos };
115 1.26 christos
116 1.26 christos static void atodays(int, char *, unsigned short *);
117 1.26 christos static void cal(void);
118 1.26 christos static void closecal(FILE *);
119 1.51 dholland static void changeuser(void);
120 1.26 christos static int getday(char *);
121 1.26 christos static int getfield(char *, char **, int *);
122 1.26 christos static void getmmdd(struct tm *, char *);
123 1.26 christos static int getmonth(char *);
124 1.44 christos static bool isnow(char *);
125 1.49 matthias static FILE *opencal(FILE **);
126 1.52 dholland static int tryopen(const char *, int);
127 1.26 christos static void settime(void);
128 1.42 perry static void usage(void) __dead;
129 1.7 glass
130 1.7 glass int
131 1.44 christos main(int argc, char **argv)
132 1.1 cgd {
133 1.1 cgd int ch;
134 1.15 mycroft const char *caldir;
135 1.1 cgd
136 1.44 christos (void)setprogname(argv[0]); /* for portability */
137 1.44 christos
138 1.53 agc while ((ch = getopt(argc, argv, "-ad:f:l:vw:x")) != -1) {
139 1.7 glass switch (ch) {
140 1.1 cgd case '-': /* backward contemptible */
141 1.1 cgd case 'a':
142 1.1 cgd if (getuid()) {
143 1.7 glass errno = EPERM;
144 1.44 christos err(EXIT_FAILURE, NULL);
145 1.1 cgd }
146 1.44 christos doall = true;
147 1.1 cgd break;
148 1.10 thorpej case 'd':
149 1.10 thorpej datestr = optarg;
150 1.10 thorpej break;
151 1.10 thorpej case 'f':
152 1.10 thorpej fname = optarg;
153 1.10 thorpej break;
154 1.10 thorpej case 'l':
155 1.10 thorpej atodays(ch, optarg, &lookahead);
156 1.10 thorpej break;
157 1.53 agc case 'v':
158 1.53 agc printf("%s\n", CALENDAR_VERSION);
159 1.53 agc return 0;
160 1.10 thorpej case 'w':
161 1.10 thorpej atodays(ch, optarg, &weekend);
162 1.10 thorpej break;
163 1.33 jwise case 'x':
164 1.44 christos cpp_restricted = true;
165 1.38 wiz break;
166 1.1 cgd case '?':
167 1.1 cgd default:
168 1.1 cgd usage();
169 1.1 cgd }
170 1.44 christos }
171 1.1 cgd argc -= optind;
172 1.1 cgd argv += optind;
173 1.1 cgd
174 1.1 cgd if (argc)
175 1.1 cgd usage();
176 1.1 cgd
177 1.1 cgd settime();
178 1.26 christos if (doall) {
179 1.44 christos /*
180 1.44 christos * XXX - This ignores the user's CALENDAR_DIR variable.
181 1.44 christos * Run under user's login shell?
182 1.44 christos */
183 1.51 dholland if (setgroups(0, NULL) == -1) {
184 1.51 dholland err(EXIT_FAILURE, "setgroups");
185 1.51 dholland }
186 1.7 glass while ((pw = getpwent()) != NULL) {
187 1.51 dholland if (setegid(pw->pw_gid) == -1) {
188 1.51 dholland warn("%s: setegid", pw->pw_name);
189 1.51 dholland continue;
190 1.51 dholland }
191 1.51 dholland if (seteuid(pw->pw_uid) == -1) {
192 1.51 dholland warn("%s: seteuid", pw->pw_name);
193 1.51 dholland continue;
194 1.51 dholland }
195 1.51 dholland if (chdir(pw->pw_dir) != -1) {
196 1.1 cgd cal();
197 1.51 dholland }
198 1.51 dholland if (seteuid(0) == -1) {
199 1.51 dholland warn("%s: seteuid back to 0", pw->pw_name);
200 1.51 dholland }
201 1.1 cgd }
202 1.26 christos } else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
203 1.44 christos if (chdir(caldir) != -1)
204 1.26 christos cal();
205 1.44 christos } else if ((pw = getpwuid(geteuid())) != NULL) {
206 1.44 christos if (chdir(pw->pw_dir) != -1)
207 1.32 jwise cal();
208 1.26 christos }
209 1.44 christos return 0;
210 1.1 cgd }
211 1.1 cgd
212 1.26 christos static void
213 1.26 christos cal(void)
214 1.1 cgd {
215 1.44 christos bool printing;
216 1.49 matthias FILE *fp, *in = NULL;
217 1.26 christos char *line;
218 1.1 cgd
219 1.49 matthias if ((fp = opencal(&in)) == NULL || in == NULL)
220 1.1 cgd return;
221 1.44 christos printing = false;
222 1.49 matthias while ((line = fparseln(in,
223 1.44 christos NULL, NULL, NULL, FPARSELN_UNESCCOMM)) != NULL) {
224 1.26 christos if (line[0] == '\0')
225 1.1 cgd continue;
226 1.26 christos if (line[0] != '\t')
227 1.44 christos printing = isnow(line);
228 1.1 cgd if (printing)
229 1.26 christos (void)fprintf(fp, "%s\n", line);
230 1.26 christos free(line);
231 1.26 christos
232 1.1 cgd }
233 1.1 cgd closecal(fp);
234 1.1 cgd }
235 1.1 cgd
236 1.26 christos static void
237 1.26 christos settime(void)
238 1.1 cgd {
239 1.7 glass time_t now;
240 1.1 cgd
241 1.1 cgd (void)time(&now);
242 1.1 cgd tp = localtime(&now);
243 1.44 christos if (datestr)
244 1.10 thorpej getmmdd(tp, datestr);
245 1.44 christos
246 1.12 christos if (isleap(tp->tm_year + TM_YEAR_BASE)) {
247 1.1 cgd yrdays = DAYSPERLYEAR;
248 1.1 cgd cumdays = daytab[1];
249 1.1 cgd } else {
250 1.1 cgd yrdays = DAYSPERNYEAR;
251 1.1 cgd cumdays = daytab[0];
252 1.1 cgd }
253 1.1 cgd /* Friday displays Monday's events */
254 1.10 thorpej offset = tp->tm_wday == 5 ? lookahead + weekend : lookahead;
255 1.1 cgd header[5].iov_base = dayname;
256 1.1 cgd header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
257 1.1 cgd }
258 1.1 cgd
259 1.1 cgd /*
260 1.1 cgd * Possible date formats include any combination of:
261 1.1 cgd * 3-charmonth (January, Jan, Jan)
262 1.1 cgd * 3-charweekday (Friday, Monday, mon.)
263 1.1 cgd * numeric month or day (1, 2, 04)
264 1.1 cgd *
265 1.1 cgd * Any character may separate them, or they may not be separated. Any line,
266 1.1 cgd * following a line that is matched, that starts with "whitespace", is shown
267 1.1 cgd * along with the matched line.
268 1.1 cgd */
269 1.44 christos static bool
270 1.44 christos isnow(char *endp)
271 1.1 cgd {
272 1.44 christos int day;
273 1.44 christos int flags;
274 1.44 christos int month;
275 1.44 christos int v1;
276 1.44 christos int v2;
277 1.1 cgd
278 1.1 cgd flags = 0;
279 1.35 jwise
280 1.1 cgd /* didn't recognize anything, skip it */
281 1.1 cgd if (!(v1 = getfield(endp, &endp, &flags)))
282 1.44 christos return false;
283 1.44 christos
284 1.48 wiz if ((flags & (F_ISDAY|F_ISDOW)) || v1 > 12) {
285 1.1 cgd /* found a day */
286 1.1 cgd day = v1;
287 1.43 christos /* if no recognizable month, assume wildcard ('*') month */
288 1.45 christos if ((month = getfield(endp, &endp, &flags)) == 0) {
289 1.43 christos flags |= F_ISMONTH | F_WILDMONTH;
290 1.43 christos month = tp->tm_mon + 1;
291 1.43 christos }
292 1.7 glass } else if (flags & F_ISMONTH) {
293 1.1 cgd month = v1;
294 1.1 cgd /* if no recognizable day, assume the first */
295 1.45 christos if ((day = getfield(endp, &endp, &flags)) == 0)
296 1.1 cgd day = 1;
297 1.1 cgd } else {
298 1.1 cgd v2 = getfield(endp, &endp, &flags);
299 1.7 glass if (flags & F_ISMONTH) {
300 1.1 cgd day = v1;
301 1.1 cgd month = v2;
302 1.1 cgd } else {
303 1.1 cgd /* F_ISDAY set, v2 > 12, or no way to tell */
304 1.1 cgd month = v1;
305 1.1 cgd /* if no recognizable day, assume the first */
306 1.1 cgd day = v2 ? v2 : 1;
307 1.1 cgd }
308 1.1 cgd }
309 1.47 dholland /* if month is out of range, treat it as '*' */
310 1.47 dholland if (month < 1 || month > 12) {
311 1.47 dholland flags |= F_ISMONTH | F_WILDMONTH;
312 1.47 dholland month = tp->tm_mon + 1;
313 1.47 dholland }
314 1.47 dholland
315 1.44 christos if (flags & F_WILDMONTH && flags & F_WILDDAY)
316 1.44 christos return true;
317 1.35 jwise
318 1.44 christos if (flags & F_WILDMONTH && flags & F_ISDAY && day == tp->tm_mday)
319 1.44 christos return true;
320 1.35 jwise
321 1.48 wiz if (flags & F_WILDMONTH && flags & F_ISDOW && day == tp->tm_wday + 1)
322 1.48 wiz return true;
323 1.48 wiz
324 1.44 christos if (flags & F_ISMONTH && flags & F_WILDDAY && month == tp->tm_mon + 1)
325 1.44 christos return true;
326 1.35 jwise
327 1.48 wiz if (flags & F_ISMONTH && flags & F_ISDOW && month == tp->tm_mon + 1 &&
328 1.48 wiz day == tp->tm_wday + 1)
329 1.48 wiz return true;
330 1.48 wiz
331 1.48 wiz if (flags & F_ISDOW)
332 1.4 cgd day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
333 1.1 cgd day = cumdays[month] + day;
334 1.1 cgd
335 1.1 cgd /* if today or today + offset days */
336 1.1 cgd if (day >= tp->tm_yday && day <= tp->tm_yday + offset)
337 1.44 christos return true;
338 1.44 christos
339 1.1 cgd /* if number of days left in this year + days to event in next year */
340 1.1 cgd if (yrdays - tp->tm_yday + day <= offset)
341 1.44 christos return true;
342 1.44 christos
343 1.44 christos return false;
344 1.1 cgd }
345 1.1 cgd
346 1.26 christos static int
347 1.44 christos getfield(char *p, char **endp, int *flags)
348 1.1 cgd {
349 1.1 cgd int val;
350 1.44 christos char *start;
351 1.44 christos char savech;
352 1.1 cgd
353 1.48 wiz /*
354 1.48 wiz * note this macro has an arg that isn't used ... it is retained
355 1.48 wiz * (it is believed) to make the macro call look more "natural"
356 1.48 wiz * and suggest at the call site what is happening.
357 1.48 wiz */
358 1.18 christos #define FLDCHAR(a) (*p != '\0' && !isdigit((unsigned char)*p) && \
359 1.18 christos !isalpha((unsigned char)*p) && *p != '*')
360 1.18 christos
361 1.48 wiz val = 0;
362 1.44 christos for (/*EMPTY*/; FLDCHAR(*p); ++p)
363 1.26 christos continue;
364 1.1 cgd if (*p == '*') { /* `*' is current month */
365 1.35 jwise if (!(*flags & F_ISMONTH)) {
366 1.44 christos *flags |= F_ISMONTH | F_WILDMONTH;
367 1.44 christos *endp = p + 1;
368 1.44 christos return tp->tm_mon + 1;
369 1.35 jwise } else {
370 1.44 christos *flags |= F_ISDAY | F_WILDDAY;
371 1.44 christos *endp = p + 1;
372 1.44 christos return 1;
373 1.35 jwise }
374 1.1 cgd }
375 1.18 christos if (isdigit((unsigned char)*p)) {
376 1.44 christos val = (int)strtol(p, &p, 10); /* if 0, it's failure */
377 1.44 christos for (/*EMPTY*/; FLDCHAR(*p); ++p)
378 1.26 christos continue;
379 1.1 cgd *endp = p;
380 1.44 christos return val;
381 1.1 cgd }
382 1.48 wiz for (start = p; *p != '\0' && isalpha((unsigned char)*p); p++)
383 1.26 christos continue;
384 1.48 wiz
385 1.1 cgd savech = *p;
386 1.48 wiz if (p != start) {
387 1.48 wiz *p = '\0';
388 1.48 wiz if ((val = getmonth(start)) != 0)
389 1.48 wiz *flags |= F_ISMONTH;
390 1.48 wiz else if ((val = getday(start)) != 0)
391 1.48 wiz *flags |= F_ISDOW;
392 1.48 wiz else {
393 1.48 wiz *p = savech;
394 1.48 wiz *endp = start;
395 1.48 wiz return 0;
396 1.48 wiz }
397 1.4 cgd }
398 1.18 christos for (*p = savech; FLDCHAR(*p); ++p)
399 1.26 christos continue;
400 1.1 cgd *endp = p;
401 1.44 christos return val;
402 1.1 cgd }
403 1.1 cgd
404 1.26 christos static FILE *
405 1.49 matthias opencal(FILE **in)
406 1.1 cgd {
407 1.50 christos int fd = -1;
408 1.44 christos int pdes[2];
409 1.1 cgd
410 1.1 cgd /* open up calendar file as stdin */
411 1.31 jwise if (fname == NULL) {
412 1.50 christos for (const char **name = defaultnames; *name != NULL; name++) {
413 1.52 dholland if ((fd = tryopen(*name, O_RDONLY)) == -1)
414 1.31 jwise continue;
415 1.31 jwise else
416 1.31 jwise break;
417 1.31 jwise }
418 1.50 christos if (fd == -1) {
419 1.31 jwise if (doall)
420 1.44 christos return NULL;
421 1.44 christos err(EXIT_FAILURE, "Cannot open calendar file");
422 1.31 jwise }
423 1.52 dholland } else if ((fd = tryopen(fname, O_RDONLY)) == -1) {
424 1.1 cgd if (doall)
425 1.44 christos return NULL;
426 1.44 christos err(EXIT_FAILURE, "Cannot open `%s'", fname);
427 1.1 cgd }
428 1.31 jwise
429 1.44 christos if (pipe(pdes) == -1) {
430 1.26 christos warn("Cannot open pipe");
431 1.44 christos return NULL;
432 1.26 christos }
433 1.31 jwise
434 1.26 christos switch (fork()) {
435 1.44 christos case -1:
436 1.44 christos /* error */
437 1.1 cgd (void)close(pdes[0]);
438 1.1 cgd (void)close(pdes[1]);
439 1.44 christos return NULL;
440 1.1 cgd case 0:
441 1.49 matthias /* child */
442 1.49 matthias /* set stdin to calendar file */
443 1.49 matthias if (fd != STDIN_FILENO) {
444 1.49 matthias (void)dup2(fd, STDIN_FILENO);
445 1.49 matthias (void)close(fd);
446 1.49 matthias }
447 1.49 matthias /* set stdout to pipe input */
448 1.1 cgd if (pdes[1] != STDOUT_FILENO) {
449 1.1 cgd (void)dup2(pdes[1], STDOUT_FILENO);
450 1.1 cgd (void)close(pdes[1]);
451 1.1 cgd }
452 1.1 cgd (void)close(pdes[0]);
453 1.51 dholland if (doall) {
454 1.51 dholland /* become the user properly */
455 1.51 dholland changeuser();
456 1.51 dholland }
457 1.33 jwise /* tell CPP to only open regular files */
458 1.44 christos if(!cpp_restricted && setenv("CPP_RESTRICTED", "", 1) == -1)
459 1.44 christos err(EXIT_FAILURE, "Cannot restrict cpp");
460 1.44 christos cpp_restricted = true;
461 1.33 jwise
462 1.26 christos (void)execl(_PATH_CPP, "cpp", "-traditional", "-P", "-I.",
463 1.26 christos "-I" _PATH_CALENDARS, NULL);
464 1.44 christos err(EXIT_FAILURE, "Cannot exec `%s'", _PATH_CPP);
465 1.26 christos /*NOTREACHED*/
466 1.44 christos default:
467 1.49 matthias /* parent -- fdopen *in to pipe output */
468 1.49 matthias *in = fdopen(pdes[0], "r");
469 1.44 christos (void)close(pdes[1]);
470 1.31 jwise
471 1.49 matthias /* close calendar file */
472 1.49 matthias close(fd);
473 1.49 matthias
474 1.44 christos /* not reading all calendar files, just set output to stdout */
475 1.44 christos if (!doall)
476 1.44 christos return stdout;
477 1.44 christos
478 1.44 christos /*
479 1.44 christos * Set output to a temporary file, so if no output
480 1.44 christos * don't send mail.
481 1.44 christos */
482 1.44 christos (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
483 1.44 christos if ((fd = mkstemp(path)) == -1) {
484 1.44 christos warn("Cannot create temporary file");
485 1.44 christos return NULL;
486 1.44 christos }
487 1.44 christos return fdopen(fd, "w+");
488 1.26 christos }
489 1.44 christos /*NOTREACHED*/
490 1.1 cgd }
491 1.1 cgd
492 1.52 dholland static int
493 1.52 dholland tryopen(const char *pathname, int flags)
494 1.52 dholland {
495 1.52 dholland int fd, serrno, zero;
496 1.52 dholland struct stat st;
497 1.52 dholland
498 1.52 dholland /*
499 1.52 dholland * XXX: cpp_restricted has inverted sense; it is false by default,
500 1.52 dholland * and -x sets it to true. CPP_RESTRICTED is set in the environment
501 1.52 dholland * if cpp_restricted is false... go figure. This should be fixed
502 1.52 dholland * later.
503 1.52 dholland */
504 1.52 dholland if (doall && cpp_restricted == false) {
505 1.52 dholland /*
506 1.52 dholland * We are running with the user's euid, so they can't
507 1.52 dholland * cause any mayhem (e.g. opening rewinding tape
508 1.52 dholland * devices) that they couldn't do easily enough on
509 1.52 dholland * their own. All we really need to worry about is opens
510 1.52 dholland * that hang, because that would DoS the calendar run.
511 1.52 dholland */
512 1.52 dholland fd = open(pathname, flags | O_NONBLOCK);
513 1.52 dholland if (fd == -1) {
514 1.52 dholland return -1;
515 1.52 dholland }
516 1.52 dholland if (fstat(fd, &st) == -1) {
517 1.52 dholland serrno = errno;
518 1.52 dholland close(fd);
519 1.52 dholland errno = serrno;
520 1.52 dholland return -1;
521 1.52 dholland }
522 1.52 dholland if (S_ISCHR(st.st_mode) ||
523 1.52 dholland S_ISBLK(st.st_mode) ||
524 1.52 dholland S_ISFIFO(st.st_mode)) {
525 1.52 dholland close(fd);
526 1.52 dholland
527 1.52 dholland /* Call shenanigans in the daily output */
528 1.52 dholland errno = EPERM;
529 1.52 dholland warn("%s: %s", pw->pw_name, pathname);
530 1.52 dholland
531 1.52 dholland errno = EPERM;
532 1.52 dholland return -1;
533 1.52 dholland }
534 1.52 dholland if (S_ISDIR(st.st_mode)) {
535 1.52 dholland /* Don't warn about this */
536 1.52 dholland close(fd);
537 1.52 dholland errno = EISDIR;
538 1.52 dholland return -1;
539 1.52 dholland }
540 1.52 dholland if (!S_ISREG(st.st_mode)) {
541 1.52 dholland /* There shouldn't be other cases to go here */
542 1.52 dholland close(fd);
543 1.52 dholland errno = EINVAL;
544 1.52 dholland return -1;
545 1.52 dholland }
546 1.52 dholland zero = 0;
547 1.52 dholland if (ioctl(fd, FIONBIO, &zero) == -1) {
548 1.52 dholland serrno = errno;
549 1.52 dholland warn("%s: %s: FIONBIO", pw->pw_name, pathname);
550 1.52 dholland close(fd);
551 1.52 dholland errno = serrno;
552 1.52 dholland return -1;
553 1.52 dholland }
554 1.52 dholland return fd;
555 1.52 dholland } else {
556 1.52 dholland return open(pathname, flags);
557 1.52 dholland }
558 1.52 dholland }
559 1.52 dholland
560 1.26 christos static void
561 1.44 christos closecal(FILE *fp)
562 1.1 cgd {
563 1.1 cgd struct stat sbuf;
564 1.44 christos ssize_t nread;
565 1.44 christos int pdes[2];
566 1.44 christos int status;
567 1.7 glass char buf[1024];
568 1.1 cgd
569 1.1 cgd if (!doall)
570 1.1 cgd return;
571 1.1 cgd
572 1.1 cgd (void)rewind(fp);
573 1.44 christos if (fstat(fileno(fp), &sbuf) == -1 || sbuf.st_size == 0)
574 1.1 cgd goto done;
575 1.44 christos if (pipe(pdes) == -1)
576 1.1 cgd goto done;
577 1.44 christos
578 1.26 christos switch (fork()) {
579 1.44 christos case -1:
580 1.44 christos /* error */
581 1.1 cgd (void)close(pdes[0]);
582 1.1 cgd (void)close(pdes[1]);
583 1.44 christos break;
584 1.10 thorpej case 0:
585 1.1 cgd /* child -- set stdin to pipe output */
586 1.1 cgd if (pdes[0] != STDIN_FILENO) {
587 1.1 cgd (void)dup2(pdes[0], STDIN_FILENO);
588 1.1 cgd (void)close(pdes[0]);
589 1.1 cgd }
590 1.1 cgd (void)close(pdes[1]);
591 1.51 dholland if (doall) {
592 1.51 dholland /* become the user properly */
593 1.51 dholland changeuser();
594 1.51 dholland }
595 1.26 christos (void)execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
596 1.1 cgd "\"Reminder Service\"", "-f", "root", NULL);
597 1.44 christos err(EXIT_FAILURE, "Cannot exec `%s'", _PATH_SENDMAIL);
598 1.26 christos /*NOTREACHED*/
599 1.44 christos default:
600 1.44 christos /* parent -- write to pipe input */
601 1.44 christos (void)close(pdes[0]);
602 1.44 christos
603 1.44 christos header[1].iov_base = header[3].iov_base = (void *)pw->pw_name;
604 1.44 christos header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
605 1.44 christos (void)writev(pdes[1], header, 7);
606 1.44 christos while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
607 1.44 christos (void)write(pdes[1], buf, (size_t)nread);
608 1.44 christos (void)close(pdes[1]);
609 1.44 christos break;
610 1.1 cgd }
611 1.26 christos
612 1.1 cgd done: (void)fclose(fp);
613 1.1 cgd (void)unlink(path);
614 1.44 christos while (wait(&status) != -1)
615 1.26 christos continue;
616 1.1 cgd }
617 1.1 cgd
618 1.51 dholland static void
619 1.51 dholland changeuser(void)
620 1.51 dholland {
621 1.51 dholland uid_t uid;
622 1.51 dholland gid_t gid;
623 1.51 dholland
624 1.51 dholland uid = geteuid();
625 1.51 dholland gid = getegid();
626 1.51 dholland assert(uid == pw->pw_uid);
627 1.51 dholland assert(gid == pw->pw_gid);
628 1.51 dholland
629 1.51 dholland if (seteuid(0) == -1) {
630 1.51 dholland err(EXIT_FAILURE, "%s: changing user: cannot reassert uid 0",
631 1.51 dholland pw->pw_name);
632 1.51 dholland }
633 1.51 dholland if (setgid(gid) == -1) {
634 1.51 dholland err(EXIT_FAILURE, "%s: cannot assume gid %d",
635 1.51 dholland pw->pw_name, (int)gid);
636 1.51 dholland }
637 1.51 dholland if (initgroups(pw->pw_name, gid) == -1) {
638 1.51 dholland err(EXIT_FAILURE, "%s: cannot initgroups", pw->pw_name);
639 1.51 dholland }
640 1.51 dholland if (setuid(uid) == -1) {
641 1.51 dholland err(EXIT_FAILURE, "%s: cannot assume uid %d",
642 1.51 dholland pw->pw_name, (int)uid);
643 1.51 dholland }
644 1.51 dholland }
645 1.51 dholland
646 1.26 christos static int
647 1.44 christos getmonth(char *s)
648 1.1 cgd {
649 1.44 christos const char **p;
650 1.1 cgd
651 1.1 cgd for (p = months; *p; ++p)
652 1.44 christos if (strncasecmp(s, *p, 3) == 0)
653 1.44 christos return (int)(p - months) + 1;
654 1.44 christos return 0;
655 1.1 cgd }
656 1.1 cgd
657 1.26 christos static int
658 1.44 christos getday(char *s)
659 1.1 cgd {
660 1.44 christos const char **p;
661 1.1 cgd
662 1.1 cgd for (p = days; *p; ++p)
663 1.44 christos if (strncasecmp(s, *p, 3) == 0)
664 1.44 christos return (int)(p - days) + 1;
665 1.44 christos return 0;
666 1.1 cgd }
667 1.1 cgd
668 1.26 christos static void
669 1.44 christos atodays(int ch, char *arg, unsigned short *rvp)
670 1.10 thorpej {
671 1.10 thorpej int u;
672 1.10 thorpej
673 1.44 christos u = atoi(arg);
674 1.44 christos if (u < 0 || u > 366)
675 1.26 christos warnx("-%c %d out of range 0-366, ignored.", ch, u);
676 1.44 christos else
677 1.44 christos *rvp = u;
678 1.10 thorpej }
679 1.10 thorpej
680 1.10 thorpej #define todigit(x) ((x) - '0')
681 1.10 thorpej #define ATOI2(x) (todigit((x)[0]) * 10 + todigit((x)[1]))
682 1.18 christos #define ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1]))
683 1.10 thorpej
684 1.26 christos static void
685 1.44 christos getmmdd(struct tm *ptm, char *ds)
686 1.10 thorpej {
687 1.44 christos bool ok = false;
688 1.10 thorpej struct tm ttm;
689 1.10 thorpej
690 1.44 christos ttm = *ptm;
691 1.10 thorpej ttm.tm_isdst = -1;
692 1.10 thorpej
693 1.10 thorpej if (ISDIG2(ds)) {
694 1.10 thorpej ttm.tm_mon = ATOI2(ds) - 1;
695 1.10 thorpej ds += 2;
696 1.10 thorpej }
697 1.10 thorpej if (ISDIG2(ds)) {
698 1.10 thorpej ttm.tm_mday = ATOI2(ds);
699 1.10 thorpej ds += 2;
700 1.44 christos ok = true;
701 1.10 thorpej }
702 1.10 thorpej if (ok) {
703 1.10 thorpej if (ISDIG2(ds) && ISDIG2(ds + 2)) {
704 1.12 christos ttm.tm_year = ATOI2(ds) * 100 - TM_YEAR_BASE;
705 1.10 thorpej ds += 2;
706 1.10 thorpej ttm.tm_year += ATOI2(ds);
707 1.10 thorpej } else if (ISDIG2(ds)) {
708 1.10 thorpej ttm.tm_year = ATOI2(ds);
709 1.12 christos if (ttm.tm_year < 69)
710 1.12 christos ttm.tm_year += 2000 - TM_YEAR_BASE;
711 1.12 christos else
712 1.12 christos ttm.tm_year += 1900 - TM_YEAR_BASE;
713 1.10 thorpej }
714 1.10 thorpej }
715 1.44 christos if (ok && mktime(&ttm) == -1)
716 1.44 christos ok = false;
717 1.44 christos
718 1.44 christos if (ok)
719 1.44 christos *ptm = ttm;
720 1.44 christos else {
721 1.26 christos warnx("Can't convert `%s' to date, ignored.", ds);
722 1.10 thorpej usage();
723 1.10 thorpej }
724 1.10 thorpej }
725 1.10 thorpej
726 1.44 christos __dead
727 1.26 christos static void
728 1.26 christos usage(void)
729 1.1 cgd {
730 1.34 wiz (void)fprintf(stderr, "usage: %s [-ax] [-d MMDD[[YY]YY]"
731 1.26 christos " [-f fname] [-l days] [-w days]\n", getprogname());
732 1.1 cgd exit(1);
733 1.1 cgd }
734