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