xref: /freebsd/usr.bin/calendar/io.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
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
35 static 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
42 static char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
43 #endif
44 #endif
45 
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
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 
70 const char *calendarFile = "calendar";  /* default calendar file */
71 const char *calendarHomes[] = { ".calendar", _PATH_INCLUDE }; /* HOME */
72 const char *calendarNoMail = "nomail";  /* don't sent mail if this file exist */
73 
74 struct fixs neaster, npaskha;
75 
76 struct 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 
87 void
88 cal()
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 
168 int
169 getfield(p, endp, flags)
170 	char *p, **endp;
171 	int *flags;
172 {
173 	int val, var;
174 	char *start, savech;
175 
176 	for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
177 	if (*p == '*') {			/* `*' is current month */
178 		*flags |= F_ISMONTH;
179 		*endp = p+1;
180 		return (tp->tm_mon + 1);
181 	}
182 	if (isdigit((unsigned char)*p)) {
183 		val = strtol(p, &p, 10);	/* if 0, it's failure */
184 		for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
185 		*endp = p;
186 		return (val);
187 	}
188 	for (start = p; isalpha((unsigned char)*++p););
189 
190 	/* Sunday-1 */
191 	if (*p == '+' || *p == '-')
192 	    for(; isdigit((unsigned char)*++p););
193 
194 	savech = *p;
195 	*p = '\0';
196 
197 	/* Month */
198 	if ((val = getmonth(start)) != 0)
199 		*flags |= F_ISMONTH;
200 
201 	/* Day */
202 	else if ((val = getday(start)) != 0) {
203 	    *flags |= F_ISDAY;
204 
205 	    /* variable weekday */
206 	    if ((var = getdayvar(start)) != 0) {
207 		if (var <=5 && var >= -4)
208 		    val += var * 10;
209 #ifdef DEBUG
210 		printf("var: %d\n", var);
211 #endif
212 	    }
213 	}
214 
215 	/* Easter */
216 	else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
217 	    *flags |= F_EASTER;
218 
219 	/* Paskha */
220 	else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
221 	    *flags |= F_EASTER;
222 
223 	/* undefined rest */
224 	else {
225 		*p = savech;
226 		return (0);
227 	}
228 	for (*p = savech; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
229 	*endp = p;
230 	return (val);
231 }
232 
233 char path[MAXPATHLEN];
234 
235 FILE *
236 opencal()
237 {
238 	uid_t uid;
239 	size_t i;
240 	int fd, found, pdes[2];
241 	struct stat sbuf;
242 
243 	/* open up calendar file as stdin */
244 	if (!freopen(calendarFile, "r", stdin)) {
245 		if (doall) {
246 		    if (chdir(calendarHomes[0]) != 0)
247 			return (NULL);
248 		    if (stat(calendarNoMail, &sbuf) == 0)
249 		        return (NULL);
250 		    if (!freopen(calendarFile, "r", stdin))
251 		        return (NULL);
252 		} else {
253 		        chdir(getenv("HOME"));
254 			for (found = i = 0; i < sizeof(calendarHomes) /
255 			    sizeof(calendarHomes[0]); i++)
256 			    if (chdir(calendarHomes[i]) == 0 &&
257 			          freopen(calendarFile, "r", stdin)) {
258 				    found = 1;
259 				    break;
260 			    }
261 			if (!found)
262 			    errx(1, "no calendar file: ``%s''", calendarFile);
263 		}
264 	}
265 	if (pipe(pdes) < 0)
266 		return (NULL);
267 	switch (fork()) {
268 	case -1:			/* error */
269 		(void)close(pdes[0]);
270 		(void)close(pdes[1]);
271 		return (NULL);
272 	case 0:
273 		/* child -- stdin already setup, set stdout to pipe input */
274 		if (pdes[1] != STDOUT_FILENO) {
275 			(void)dup2(pdes[1], STDOUT_FILENO);
276 			(void)close(pdes[1]);
277 		}
278 		(void)close(pdes[0]);
279 		uid = geteuid();
280 		if (setuid(getuid()) < 0) {
281 			warnx("first setuid failed");
282 			_exit(1);
283 		};
284 		if (setgid(getegid()) < 0) {
285 			warnx("setgid failed");
286 			_exit(1);
287 		}
288 		if (setuid(uid) < 0) {
289 			warnx("setuid failed");
290 			_exit(1);
291 		}
292 		execl(_PATH_CPP, "cpp", "-P",
293 		    "-traditional", "-nostdinc",	/* GCC specific opts */
294 		    "-I.", "-I", _PATH_INCLUDE, (char *)NULL);
295 		warn(_PATH_CPP);
296 		_exit(1);
297 	}
298 	/* parent -- set stdin to pipe output */
299 	(void)dup2(pdes[0], STDIN_FILENO);
300 	(void)close(pdes[0]);
301 	(void)close(pdes[1]);
302 
303 	/* not reading all calendar files, just set output to stdout */
304 	if (!doall)
305 		return (stdout);
306 
307 	/* set output to a temporary file, so if no output don't send mail */
308 	(void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
309 	if ((fd = mkstemp(path)) < 0)
310 		return (NULL);
311 	return (fdopen(fd, "w+"));
312 }
313 
314 void
315 closecal(fp)
316 	FILE *fp;
317 {
318 	uid_t uid;
319 	struct stat sbuf;
320 	int nread, pdes[2], status;
321 	char buf[1024];
322 
323 	if (!doall)
324 		return;
325 
326 	(void)rewind(fp);
327 	if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
328 		goto done;
329 	if (pipe(pdes) < 0)
330 		goto done;
331 	switch (fork()) {
332 	case -1:			/* error */
333 		(void)close(pdes[0]);
334 		(void)close(pdes[1]);
335 		goto done;
336 	case 0:
337 		/* child -- set stdin to pipe output */
338 		if (pdes[0] != STDIN_FILENO) {
339 			(void)dup2(pdes[0], STDIN_FILENO);
340 			(void)close(pdes[0]);
341 		}
342 		(void)close(pdes[1]);
343 		uid = geteuid();
344 		if (setuid(getuid()) < 0) {
345 			warnx("setuid failed");
346 			_exit(1);
347 		};
348 		if (setgid(getegid()) < 0) {
349 			warnx("setgid failed");
350 			_exit(1);
351 		}
352 		if (setuid(uid) < 0) {
353 			warnx("setuid failed");
354 			_exit(1);
355 		}
356 		execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
357 		    "\"Reminder Service\"", (char *)NULL);
358 		warn(_PATH_SENDMAIL);
359 		_exit(1);
360 	}
361 	/* parent -- write to pipe input */
362 	(void)close(pdes[0]);
363 
364 	header[1].iov_base = header[3].iov_base = pw->pw_name;
365 	header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
366 	writev(pdes[1], header, 7);
367 	while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
368 		(void)write(pdes[1], buf, nread);
369 	(void)close(pdes[1]);
370 done:	(void)fclose(fp);
371 	(void)unlink(path);
372 	while (wait(&status) >= 0);
373 }
374