io.c revision 74583
1/*
2 * Copyright (c) 1989, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: head/usr.bin/calendar/io.c 74583 2001-03-21 15:41:02Z ache $
34 */
35
36#ifndef lint
37static const char copyright[] =
38"@(#) Copyright (c) 1989, 1993\n\
39	The Regents of the University of California.  All rights reserved.\n";
40#endif /* not lint */
41
42#ifndef lint
43static const char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
44#endif /* not lint */
45
46#include <sys/param.h>
47#include <stdio.h>
48#include <ctype.h>
49#include <sys/types.h>
50#include <sys/stat.h>
51#include <unistd.h>
52#include <err.h>
53#include <errno.h>
54#include <langinfo.h>
55#include <locale.h>
56#include <string.h>
57#include <sys/uio.h>
58#include <sys/time.h>
59#include <stdlib.h>
60#include <pwd.h>
61#include <sys/wait.h>
62
63#include "pathnames.h"
64#include "calendar.h"
65
66
67char *calendarFile = "calendar";  /* default calendar file */
68char *calendarHome = ".calendar"; /* HOME */
69char *calendarNoMail = "nomail";  /* don't sent mail if this file exist */
70
71struct fixs neaster, npaskha;
72
73struct iovec header[] = {
74	{"From: ", 6},
75	{NULL, 0},
76	{" (Reminder Service)\nTo: ", 24},
77	{NULL, 0},
78	{"\nSubject: ", 10},
79	{NULL, 0},
80	{"'s Calendar\nPrecedence: bulk\n\n",  30},
81};
82
83
84void
85cal()
86{
87	register int printing;
88	register char *p;
89	FILE *fp;
90	int ch, l;
91	int month;
92	int day;
93	int var;
94	static int d_first = -1;
95	char buf[2048 + 1];
96
97	if ((fp = opencal()) == NULL)
98		return;
99	for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
100		if ((p = strchr(buf, '\n')) != NULL)
101			*p = '\0';
102		else
103			while ((ch = getchar()) != '\n' && ch != EOF);
104		for (l = strlen(buf);
105		     l > 0 && isspace((unsigned char)buf[l - 1]);
106		     l--)
107			;
108		buf[l] = '\0';
109		if (buf[0] == '\0')
110			continue;
111		if (strncmp(buf, "LANG=", 5) == 0) {
112			(void) setlocale(LC_ALL, buf + 5);
113			d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
114			setnnames();
115			continue;
116		}
117		if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) {
118			if (neaster.name != NULL)
119				free(neaster.name);
120			if ((neaster.name = strdup(buf + 7)) == NULL)
121				errx(1, "cannot allocate memory");
122			neaster.len = strlen(buf + 7);
123			continue;
124		}
125		if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) {
126			if (npaskha.name != NULL)
127				free(npaskha.name);
128			if ((npaskha.name = strdup(buf + 7)) == NULL)
129				errx(1, "cannot allocate memory");
130			npaskha.len = strlen(buf + 7);
131			continue;
132		}
133		if (buf[0] != '\t') {
134			printing = isnow(buf, &month, &day, &var) ? 1 : 0;
135			if ((p = strchr(buf, '\t')) == NULL)
136				continue;
137			if (p > buf && p[-1] == '*')
138				var = 1;
139			if (printing) {
140				struct tm tm;
141				char dbuf[80];
142
143				if (d_first < 0)
144					d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
145				tm.tm_sec = 0;  /* unused */
146				tm.tm_min = 0;  /* unused */
147				tm.tm_hour = 0; /* unused */
148				tm.tm_wday = 0; /* unused */
149				tm.tm_mon = month - 1;
150				tm.tm_mday = day;
151				tm.tm_year = tp->tm_year; /* unused */
152				(void)strftime(dbuf, sizeof(dbuf),
153					       d_first ? "%e %b" : "%b %e",
154					       &tm);
155				(void)fprintf(fp, "%s%c%s\n", dbuf,
156				    var ? '*' : ' ', p);
157			}
158		}
159		else if (printing)
160			fprintf(fp, "%s\n", buf);
161	}
162	closecal(fp);
163}
164
165int
166getfield(p, endp, flags)
167	char *p, **endp;
168	int *flags;
169{
170	int val, var;
171	char *start, savech;
172
173	for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
174	if (*p == '*') {			/* `*' is current month */
175		*flags |= F_ISMONTH;
176		*endp = p+1;
177		return (tp->tm_mon + 1);
178	}
179	if (isdigit((unsigned char)*p)) {
180		val = strtol(p, &p, 10);	/* if 0, it's failure */
181		for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
182		*endp = p;
183		return (val);
184	}
185	for (start = p; isalpha((unsigned char)*++p););
186
187	/* Sunday-1 */
188	if (*p == '+' || *p == '-')
189	    for(; isdigit((unsigned char)*++p););
190
191	savech = *p;
192	*p = '\0';
193
194	/* Month */
195	if ((val = getmonth(start)) != 0)
196		*flags |= F_ISMONTH;
197
198	/* Day */
199	else if ((val = getday(start)) != 0) {
200	    *flags |= F_ISDAY;
201
202	    /* variable weekday */
203	    if ((var = getdayvar(start)) != 0) {
204		if (var <=5 && var >= -4)
205		    val += var * 10;
206#ifdef DEBUG
207		printf("var: %d\n", var);
208#endif
209	    }
210	}
211
212	/* Easter */
213	else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
214	    *flags |= F_EASTER;
215
216	/* Paskha */
217	else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
218	    *flags |= F_EASTER;
219
220	/* undefined rest */
221	else {
222		*p = savech;
223		return (0);
224	}
225	for (*p = savech; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
226	*endp = p;
227	return (val);
228}
229
230char path[MAXPATHLEN];
231
232FILE *
233opencal()
234{
235	uid_t uid;
236	int fd, pdes[2];
237	struct stat sbuf;
238
239	/* open up calendar file as stdin */
240	if (!freopen(calendarFile, "r", stdin)) {
241		if (doall) {
242		    if (chdir(calendarHome) != 0)
243			return (NULL);
244		    if (stat(calendarNoMail, &sbuf) == 0)
245		        return (NULL);
246		    if (!freopen(calendarFile, "r", stdin))
247		        return (NULL);
248		} else {
249		        chdir(getenv("HOME"));
250			if (!(chdir(calendarHome) == 0 &&
251			      freopen(calendarFile, "r", stdin)))
252				errx(1, "no calendar file: ``%s'' or ``~/%s/%s\n", calendarFile, calendarHome, calendarFile);
253		}
254	}
255	if (pipe(pdes) < 0)
256		return (NULL);
257	switch (fork()) {
258	case -1:			/* error */
259		(void)close(pdes[0]);
260		(void)close(pdes[1]);
261		return (NULL);
262	case 0:
263		/* child -- stdin already setup, set stdout to pipe input */
264		if (pdes[1] != STDOUT_FILENO) {
265			(void)dup2(pdes[1], STDOUT_FILENO);
266			(void)close(pdes[1]);
267		}
268		(void)close(pdes[0]);
269		uid = geteuid();
270		if (setuid(getuid()) < 0) {
271			warnx("first setuid failed");
272			_exit(1);
273		};
274		if (setgid(getegid()) < 0) {
275			warnx("setgid failed");
276			_exit(1);
277		}
278		if (setuid(uid) < 0) {
279			warnx("setuid failed");
280			_exit(1);
281		}
282		execl(_PATH_CPP, "cpp", "-P",
283		    "-traditional", "-nostdinc",	/* GCC specific opts */
284		    "-I.", "-I", _PATH_INCLUDE, NULL);
285		warn(_PATH_CPP);
286		_exit(1);
287	}
288	/* parent -- set stdin to pipe output */
289	(void)dup2(pdes[0], STDIN_FILENO);
290	(void)close(pdes[0]);
291	(void)close(pdes[1]);
292
293	/* not reading all calendar files, just set output to stdout */
294	if (!doall)
295		return (stdout);
296
297	/* set output to a temporary file, so if no output don't send mail */
298	(void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
299	if ((fd = mkstemp(path)) < 0)
300		return (NULL);
301	return (fdopen(fd, "w+"));
302}
303
304void
305closecal(fp)
306	FILE *fp;
307{
308	uid_t uid;
309	struct stat sbuf;
310	int nread, pdes[2], status;
311	char buf[1024];
312
313	if (!doall)
314		return;
315
316	(void)rewind(fp);
317	if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
318		goto done;
319	if (pipe(pdes) < 0)
320		goto done;
321	switch (fork()) {
322	case -1:			/* error */
323		(void)close(pdes[0]);
324		(void)close(pdes[1]);
325		goto done;
326	case 0:
327		/* child -- set stdin to pipe output */
328		if (pdes[0] != STDIN_FILENO) {
329			(void)dup2(pdes[0], STDIN_FILENO);
330			(void)close(pdes[0]);
331		}
332		(void)close(pdes[1]);
333		uid = geteuid();
334		if (setuid(getuid()) < 0) {
335			warnx("setuid failed");
336			_exit(1);
337		};
338		if (setgid(getegid()) < 0) {
339			warnx("setgid failed");
340			_exit(1);
341		}
342		if (setuid(uid) < 0) {
343			warnx("setuid failed");
344			_exit(1);
345		}
346		execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
347		    "\"Reminder Service\"", NULL);
348		warn(_PATH_SENDMAIL);
349		_exit(1);
350	}
351	/* parent -- write to pipe input */
352	(void)close(pdes[0]);
353
354	header[1].iov_base = header[3].iov_base = pw->pw_name;
355	header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
356	writev(pdes[1], header, 7);
357	while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
358		(void)write(pdes[1], buf, nread);
359	(void)close(pdes[1]);
360done:	(void)fclose(fp);
361	(void)unlink(path);
362	while (wait(&status) >= 0);
363}
364