io.c revision 169343
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
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1989, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif
39
40#if 0
41#ifndef lint
42static char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
43#endif
44#endif
45
46#include <sys/cdefs.h>
47__FBSDID("$FreeBSD: head/usr.bin/calendar/io.c 169343 2007-05-07 11:18:30Z dwmalone $");
48
49#include <sys/types.h>
50#include <sys/param.h>
51#include <sys/stat.h>
52#include <sys/time.h>
53#include <sys/uio.h>
54#include <sys/wait.h>
55#include <ctype.h>
56#include <err.h>
57#include <errno.h>
58#include <langinfo.h>
59#include <locale.h>
60#include <pwd.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <unistd.h>
65
66#include "pathnames.h"
67#include "calendar.h"
68
69
70const char *calendarFile = "calendar";  /* default calendar file */
71const char *calendarHomes[] = { ".calendar", _PATH_INCLUDE }; /* HOME */
72const char *calendarNoMail = "nomail";  /* don't sent mail if this file exist */
73
74struct fixs neaster, npaskha;
75
76struct iovec header[] = {
77	{"From: ", 6},
78	{NULL, 0},
79	{" (Reminder Service)\nTo: ", 24},
80	{NULL, 0},
81	{"\nSubject: ", 10},
82	{NULL, 0},
83	{"'s Calendar\nPrecedence: bulk\n\n",  30},
84};
85
86
87void
88cal(void)
89{
90	int printing;
91	char *p;
92	FILE *fp;
93	int ch, l;
94	int month;
95	int day;
96	int var;
97	static int d_first = -1;
98	char buf[2048 + 1];
99
100	if ((fp = opencal()) == NULL)
101		return;
102	for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
103		if ((p = strchr(buf, '\n')) != NULL)
104			*p = '\0';
105		else
106			while ((ch = getchar()) != '\n' && ch != EOF);
107		for (l = strlen(buf);
108		     l > 0 && isspace((unsigned char)buf[l - 1]);
109		     l--)
110			;
111		buf[l] = '\0';
112		if (buf[0] == '\0')
113			continue;
114		if (strncmp(buf, "LANG=", 5) == 0) {
115			(void) setlocale(LC_ALL, buf + 5);
116			d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
117			setnnames();
118			continue;
119		}
120		if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) {
121			if (neaster.name != NULL)
122				free(neaster.name);
123			if ((neaster.name = strdup(buf + 7)) == NULL)
124				errx(1, "cannot allocate memory");
125			neaster.len = strlen(buf + 7);
126			continue;
127		}
128		if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) {
129			if (npaskha.name != NULL)
130				free(npaskha.name);
131			if ((npaskha.name = strdup(buf + 7)) == NULL)
132				errx(1, "cannot allocate memory");
133			npaskha.len = strlen(buf + 7);
134			continue;
135		}
136		if (buf[0] != '\t') {
137			printing = isnow(buf, &month, &day, &var) ? 1 : 0;
138			if ((p = strchr(buf, '\t')) == NULL)
139				continue;
140			if (p > buf && p[-1] == '*')
141				var = 1;
142			if (printing) {
143				struct tm tm;
144				char dbuf[80];
145
146				if (d_first < 0)
147					d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
148				tm.tm_sec = 0;  /* unused */
149				tm.tm_min = 0;  /* unused */
150				tm.tm_hour = 0; /* unused */
151				tm.tm_wday = 0; /* unused */
152				tm.tm_mon = month - 1;
153				tm.tm_mday = day;
154				tm.tm_year = tp->tm_year; /* unused */
155				(void)strftime(dbuf, sizeof(dbuf),
156					       d_first ? "%e %b" : "%b %e",
157					       &tm);
158				(void)fprintf(fp, "%s%c%s\n", dbuf,
159				    var ? '*' : ' ', p);
160			}
161		}
162		else if (printing)
163			fprintf(fp, "%s\n", buf);
164	}
165	closecal(fp);
166}
167
168int
169getfield(char *p, char **endp, int *flags)
170{
171	int val, var;
172	char *start, savech;
173
174	for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
175	if (*p == '*') {			/* `*' is current month */
176		*flags |= F_ISMONTH;
177		*endp = p+1;
178		return (tp->tm_mon + 1);
179	}
180	if (isdigit((unsigned char)*p)) {
181		val = strtol(p, &p, 10);	/* if 0, it's failure */
182		for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
183		*endp = p;
184		return (val);
185	}
186	for (start = p; isalpha((unsigned char)*++p););
187
188	/* Sunday-1 */
189	if (*p == '+' || *p == '-')
190	    for(; isdigit((unsigned char)*++p););
191
192	savech = *p;
193	*p = '\0';
194
195	/* Month */
196	if ((val = getmonth(start)) != 0)
197		*flags |= F_ISMONTH;
198
199	/* Day */
200	else if ((val = getday(start)) != 0) {
201	    *flags |= F_ISDAY;
202
203	    /* variable weekday */
204	    if ((var = getdayvar(start)) != 0) {
205		if (var <=5 && var >= -4)
206		    val += var * 10;
207#ifdef DEBUG
208		printf("var: %d\n", var);
209#endif
210	    }
211	}
212
213	/* Easter */
214	else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
215	    *flags |= F_EASTER;
216
217	/* Paskha */
218	else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
219	    *flags |= F_EASTER;
220
221	/* undefined rest */
222	else {
223		*p = savech;
224		return (0);
225	}
226	for (*p = savech; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
227	*endp = p;
228	return (val);
229}
230
231char path[MAXPATHLEN];
232
233FILE *
234opencal(void)
235{
236	uid_t uid;
237	size_t i;
238	int fd, found, pdes[2];
239	struct stat sbuf;
240
241	/* open up calendar file as stdin */
242	if (!freopen(calendarFile, "r", stdin)) {
243		if (doall) {
244		    if (chdir(calendarHomes[0]) != 0)
245			return (NULL);
246		    if (stat(calendarNoMail, &sbuf) == 0)
247		        return (NULL);
248		    if (!freopen(calendarFile, "r", stdin))
249		        return (NULL);
250		} else {
251		        chdir(getenv("HOME"));
252			for (found = i = 0; i < sizeof(calendarHomes) /
253			    sizeof(calendarHomes[0]); i++)
254			    if (chdir(calendarHomes[i]) == 0 &&
255			          freopen(calendarFile, "r", stdin)) {
256				    found = 1;
257				    break;
258			    }
259			if (!found)
260			    errx(1, "no calendar file: ``%s''", calendarFile);
261		}
262	}
263	if (pipe(pdes) < 0)
264		return (NULL);
265	switch (fork()) {
266	case -1:			/* error */
267		(void)close(pdes[0]);
268		(void)close(pdes[1]);
269		return (NULL);
270	case 0:
271		/* child -- stdin already setup, set stdout to pipe input */
272		if (pdes[1] != STDOUT_FILENO) {
273			(void)dup2(pdes[1], STDOUT_FILENO);
274			(void)close(pdes[1]);
275		}
276		(void)close(pdes[0]);
277		uid = geteuid();
278		if (setuid(getuid()) < 0) {
279			warnx("first setuid failed");
280			_exit(1);
281		};
282		if (setgid(getegid()) < 0) {
283			warnx("setgid failed");
284			_exit(1);
285		}
286		if (setuid(uid) < 0) {
287			warnx("setuid failed");
288			_exit(1);
289		}
290		execl(_PATH_CPP, "cpp", "-P",
291		    "-traditional", "-nostdinc",	/* GCC specific opts */
292		    "-I.", "-I", _PATH_INCLUDE, (char *)NULL);
293		warn(_PATH_CPP);
294		_exit(1);
295	}
296	/* parent -- set stdin to pipe output */
297	(void)dup2(pdes[0], STDIN_FILENO);
298	(void)close(pdes[0]);
299	(void)close(pdes[1]);
300
301	/* not reading all calendar files, just set output to stdout */
302	if (!doall)
303		return (stdout);
304
305	/* set output to a temporary file, so if no output don't send mail */
306	(void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
307	if ((fd = mkstemp(path)) < 0)
308		return (NULL);
309	return (fdopen(fd, "w+"));
310}
311
312void
313closecal(FILE *fp)
314{
315	uid_t uid;
316	struct stat sbuf;
317	int nread, pdes[2], status;
318	char buf[1024];
319
320	if (!doall)
321		return;
322
323	(void)rewind(fp);
324	if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
325		goto done;
326	if (pipe(pdes) < 0)
327		goto done;
328	switch (fork()) {
329	case -1:			/* error */
330		(void)close(pdes[0]);
331		(void)close(pdes[1]);
332		goto done;
333	case 0:
334		/* child -- set stdin to pipe output */
335		if (pdes[0] != STDIN_FILENO) {
336			(void)dup2(pdes[0], STDIN_FILENO);
337			(void)close(pdes[0]);
338		}
339		(void)close(pdes[1]);
340		uid = geteuid();
341		if (setuid(getuid()) < 0) {
342			warnx("setuid failed");
343			_exit(1);
344		};
345		if (setgid(getegid()) < 0) {
346			warnx("setgid failed");
347			_exit(1);
348		}
349		if (setuid(uid) < 0) {
350			warnx("setuid failed");
351			_exit(1);
352		}
353		execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
354		    "\"Reminder Service\"", (char *)NULL);
355		warn(_PATH_SENDMAIL);
356		_exit(1);
357	}
358	/* parent -- write to pipe input */
359	(void)close(pdes[0]);
360
361	header[1].iov_base = header[3].iov_base = pw->pw_name;
362	header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
363	writev(pdes[1], header, 7);
364	while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
365		(void)write(pdes[1], buf, nread);
366	(void)close(pdes[1]);
367done:	(void)fclose(fp);
368	(void)unlink(path);
369	while (wait(&status) >= 0);
370}
371