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