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