xref: /openbsd-src/usr.bin/calendar/io.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: io.c,v 1.36 2013/11/26 13:18:53 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <sys/uio.h>
37 #include <sys/wait.h>
38 
39 #include <ctype.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <locale.h>
44 #include <pwd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <tzfile.h>
49 #include <unistd.h>
50 
51 #include "pathnames.h"
52 #include "calendar.h"
53 
54 
55 struct iovec header[] = {
56 	{ "From: ", 6 },
57 	{ NULL, 0 },
58 	{ " (Reminder Service)\nTo: ", 24 },
59 	{ NULL, 0 },
60 	{ "\nSubject: ", 10 },
61 	{ NULL, 0 },
62 	{ "'s Calendar\nPrecedence: bulk\n",  29 },
63 	{ "Auto-Submitted: auto-generated\n\n", 32 },
64 };
65 
66 
67 void
68 cal(void)
69 {
70 	int ch, l, i, bodun = 0, bodun_maybe = 0, var, printing;
71 	struct event *events, *cur_evt, *ev1, *tmp;
72 	char buf[2048 + 1], *prefix = NULL, *p;
73 	struct match *m;
74 	FILE *fp;
75 
76 	events = NULL;
77 	cur_evt = NULL;
78 	if ((fp = opencal()) == NULL)
79 		return;
80 	for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
81 		if ((p = strchr(buf, '\n')) != NULL)
82 			*p = '\0';
83 		else
84 			while ((ch = getchar()) != '\n' && ch != EOF);
85 		for (l = strlen(buf); l > 0 && isspace(buf[l - 1]); l--)
86 			;
87 		buf[l] = '\0';
88 		if (buf[0] == '\0')
89 			continue;
90 		if (strncmp(buf, "LANG=", 5) == 0) {
91 			(void) setlocale(LC_ALL, buf + 5);
92 			setnnames();
93 			if (!strcmp(buf + 5, "ru_RU.KOI8-R") ||
94 			    !strcmp(buf + 5, "uk_UA.KOI8-U") ||
95 			    !strcmp(buf + 5, "by_BY.KOI8-B")) {
96 				bodun_maybe++;
97 				bodun = 0;
98 				if (prefix)
99 					free(prefix);
100 				prefix = NULL;
101 			} else
102 				bodun_maybe = 0;
103 			continue;
104 		} else if (strncmp(buf, "CALENDAR=", 9) == 0) {
105 			char *ep;
106 
107 			if (buf[9] == '\0')
108 				calendar = 0;
109 			else if (!strcasecmp(buf + 9, "julian")) {
110 				calendar = JULIAN;
111 				errno = 0;
112 				julian = strtoul(buf + 14, &ep, 10);
113 				if (buf[0] == '\0' || *ep != '\0')
114 					julian = 13;
115 				if ((errno == ERANGE && julian == ULONG_MAX) ||
116 				    julian > 14)
117 					errx(1, "Julian calendar offset is too large");
118 			} else if (!strcasecmp(buf + 9, "gregorian"))
119 				calendar = GREGORIAN;
120 			else if (!strcasecmp(buf + 9, "lunar"))
121 				calendar = LUNAR;
122 		} else if (bodun_maybe && strncmp(buf, "BODUN=", 6) == 0) {
123 			bodun++;
124 			if (prefix)
125 				free(prefix);
126 			if ((prefix = strdup(buf + 6)) == NULL)
127 				err(1, NULL);
128 			continue;
129 		}
130 		/* User defined names for special events */
131 		if ((p = strchr(buf, '='))) {
132 			for (i = 0; i < NUMEV; i++) {
133 				if (strncasecmp(buf, spev[i].name,
134 				    spev[i].nlen) == 0 &&
135 				    (p - buf == spev[i].nlen) &&
136 				    buf[spev[i].nlen + 1]) {
137 					p++;
138 					if (spev[i].uname != NULL)
139 						free(spev[i].uname);
140 					if ((spev[i].uname = strdup(p)) == NULL)
141 						err(1, NULL);
142 					spev[i].ulen = strlen(p);
143 					i = NUMEV + 1;
144 				}
145 			}
146 			if (i > NUMEV)
147 				continue;
148 		}
149 		if (buf[0] != '\t') {
150 			printing = (m = isnow(buf, bodun)) ? 1 : 0;
151 			if ((p = strchr(buf, '\t')) == NULL) {
152 				printing = 0;
153 				continue;
154 			}
155 			/* Need the following to catch hardwired "variable"
156 			 * dates */
157 			if (p > buf && p[-1] == '*')
158 				var = 1;
159 			else
160 				var = 0;
161 			if (printing) {
162 				struct match *foo;
163 
164 				ev1 = NULL;
165 				while (m) {
166 					cur_evt = malloc(sizeof(struct event));
167 					if (cur_evt == NULL)
168 						err(1, NULL);
169 
170 					cur_evt->when = m->when;
171 					snprintf(cur_evt->print_date,
172 					    sizeof(cur_evt->print_date), "%s%c",
173 					    m->print_date, (var + m->var) ? '*' : ' ');
174 					if (ev1) {
175 						cur_evt->desc = ev1->desc;
176 						cur_evt->ldesc = NULL;
177 					} else {
178 						if (m->bodun && prefix) {
179 							if (asprintf(&cur_evt->ldesc,
180 							    "\t%s %s", prefix,
181 							    p + 1) == -1)
182 								err(1, NULL);
183 						} else if ((cur_evt->ldesc =
184 						    strdup(p)) == NULL)
185 							err(1, NULL);
186 						cur_evt->desc = &(cur_evt->ldesc);
187 						ev1 = cur_evt;
188 					}
189 					insert(&events, cur_evt);
190 					foo = m;
191 					m = m->next;
192 					free(foo);
193 				}
194 			}
195 		} else if (printing) {
196 			if (asprintf(&p, "%s\n%s", ev1->ldesc,
197 			    buf) == -1)
198 				err(1, NULL);
199 			free(ev1->ldesc);
200 			ev1->ldesc = p;
201 		}
202 	}
203 	tmp = events;
204 	while (tmp) {
205 		(void)fprintf(fp, "%s%s\n", tmp->print_date, *(tmp->desc));
206 		tmp = tmp->next;
207 	}
208 	tmp = events;
209 	while (tmp) {
210 		events = tmp;
211 		if (tmp->ldesc)
212 			free(tmp->ldesc);
213 		tmp = tmp->next;
214 		free(events);
215 	}
216 	closecal(fp);
217 }
218 
219 int
220 getfield(char *p, char **endp, int *flags)
221 {
222 	int val, var, i;
223 	char *start, savech;
224 
225 	for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) &&
226 	    *p != '*' && *p != '\t'; ++p)
227 		;
228 	if (*p == '*') {			/* `*' is every month */
229 		*flags |= F_ISMONTH;
230 		*endp = p+1;
231 		return (-1);	/* means 'every month' */
232 	}
233 	if (isdigit((unsigned char)*p)) {
234 		val = strtol(p, &p, 10);	/* if 0, it's failure */
235 		for (; !isdigit((unsigned char)*p) &&
236 		    !isalpha((unsigned char)*p) && *p != '*'; ++p)
237 			;
238 		*endp = p;
239 		return (val);
240 	}
241 	for (start = p; isalpha((unsigned char)*++p);)
242 		;
243 
244 	/* Sunday-1 */
245 	if (*p == '+' || *p == '-')
246 		for(; isdigit((unsigned char)*++p); )
247 			;
248 
249 	savech = *p;
250 	*p = '\0';
251 
252 	/* Month */
253 	if ((val = getmonth(start)) != 0)
254 		*flags |= F_ISMONTH;
255 
256 	/* Day */
257 	else if ((val = getday(start)) != 0) {
258 		*flags |= F_ISDAY;
259 
260 		/* variable weekday */
261 		if ((var = getdayvar(start)) != 0) {
262 			if (var <= 5 && var >= -4)
263 				val += var * 10;
264 #ifdef DEBUG
265 			printf("var: %d\n", var);
266 #endif
267 		}
268 	}
269 
270 	/* Try specials (Easter, Paskha, ...) */
271 	else {
272 		for (i = 0; i < NUMEV; i++) {
273 			if (strncasecmp(start, spev[i].name, spev[i].nlen) == 0) {
274 				start += spev[i].nlen;
275 				val = i + 1;
276 				i = NUMEV + 1;
277 			} else if (spev[i].uname != NULL &&
278 			    strncasecmp(start, spev[i].uname, spev[i].ulen) == 0) {
279 				start += spev[i].ulen;
280 				val = i + 1;
281 				i = NUMEV + 1;
282 			}
283 		}
284 		if (i > NUMEV) {
285 			switch(*start) {
286 			case '-':
287 			case '+':
288 				var = atoi(start);
289 				if (var > 365 || var < -365)
290 					return (0); /* Someone is just being silly */
291 				val += (NUMEV + 1) * var;
292 				/* We add one to the matching event and multiply by
293 				 * (NUMEV + 1) so as not to return 0 if there's a match.
294 				 * val will overflow if there is an obscenely large
295 				 * number of special events. */
296 				break;
297 			}
298 			*flags |= F_SPECIAL;
299 		}
300 		if (!(*flags & F_SPECIAL)) {
301 			/* undefined rest */
302 			*p = savech;
303 			return (0);
304 		}
305 	}
306 	for (*p = savech; !isdigit((unsigned char)*p) &&
307 	    !isalpha((unsigned char)*p) && *p != '*' && *p != '\t'; ++p)
308 		;
309 	*endp = p;
310 	return (val);
311 }
312 
313 
314 FILE *
315 opencal(void)
316 {
317 	int pdes[2], fdin;
318 	struct stat st;
319 
320 	/* open up calendar file as stdin */
321 	if ((fdin = open(calendarFile, O_RDONLY)) == -1 ||
322 	    fstat(fdin, &st) == -1 || !S_ISREG(st.st_mode)) {
323 		if (!doall) {
324 			char *home = getenv("HOME");
325 			if (home == NULL || *home == '\0')
326 				errx(1, "cannot get home directory");
327 			if (!(chdir(home) == 0 &&
328 			    chdir(calendarHome) == 0 &&
329 			    (fdin = open(calendarFile, O_RDONLY)) != -1))
330 				errx(1, "no calendar file: ``%s'' or ``~/%s/%s''",
331 				    calendarFile, calendarHome, calendarFile);
332 		}
333 	}
334 
335 	if (pipe(pdes) < 0)
336 		return (NULL);
337 	switch (vfork()) {
338 	case -1:			/* error */
339 		(void)close(pdes[0]);
340 		(void)close(pdes[1]);
341 		return (NULL);
342 	case 0:
343 		dup2(fdin, STDIN_FILENO);
344 		/* child -- set stdout to pipe input */
345 		if (pdes[1] != STDOUT_FILENO) {
346 			(void)dup2(pdes[1], STDOUT_FILENO);
347 			(void)close(pdes[1]);
348 		}
349 		(void)close(pdes[0]);
350 		/*
351 		 * Set stderr to /dev/null.  Necessary so that cron does not
352 		 * wait for cpp to finish if it's running calendar -a.
353 		 */
354 		if (doall) {
355 			int fderr;
356 			fderr = open(_PATH_DEVNULL, O_WRONLY, 0);
357 			if (fderr == -1)
358 				_exit(0);
359 			(void)dup2(fderr, STDERR_FILENO);
360 			(void)close(fderr);
361 		}
362 		execl(_PATH_CPP, "cpp", "-traditional", "-undef", "-U__GNUC__",
363 		    "-P", "-I.", _PATH_INCLUDE, (char *)NULL);
364 		warn(_PATH_CPP);
365 		_exit(1);
366 	}
367 	/* parent -- set stdin to pipe output */
368 	(void)dup2(pdes[0], STDIN_FILENO);
369 	(void)close(pdes[0]);
370 	(void)close(pdes[1]);
371 
372 	/* not reading all calendar files, just set output to stdout */
373 	if (!doall)
374 		return (stdout);
375 
376 	/* set output to a temporary file, so if no output don't send mail */
377 	return(tmpfile());
378 }
379 
380 void
381 closecal(FILE *fp)
382 {
383 	struct stat sbuf;
384 	int nread, pdes[2], status;
385 	char buf[1024];
386 
387 	if (!doall)
388 		return;
389 
390 	(void)rewind(fp);
391 	if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
392 		goto done;
393 	if (pipe(pdes) < 0)
394 		goto done;
395 	switch (vfork()) {
396 	case -1:			/* error */
397 		(void)close(pdes[0]);
398 		(void)close(pdes[1]);
399 		goto done;
400 	case 0:
401 		/* child -- set stdin to pipe output */
402 		if (pdes[0] != STDIN_FILENO) {
403 			(void)dup2(pdes[0], STDIN_FILENO);
404 			(void)close(pdes[0]);
405 		}
406 		(void)close(pdes[1]);
407 		execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
408 		    "\"Reminder Service\"", (char *)NULL);
409 		warn(_PATH_SENDMAIL);
410 		_exit(1);
411 	}
412 	/* parent -- write to pipe input */
413 	(void)close(pdes[0]);
414 
415 	header[1].iov_base = header[3].iov_base = pw->pw_name;
416 	header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
417 	writev(pdes[1], header, 8);
418 	while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
419 		(void)write(pdes[1], buf, nread);
420 	(void)close(pdes[1]);
421 done:	(void)fclose(fp);
422 	while (wait(&status) >= 0)
423 		;
424 }
425 
426 
427 void
428 insert(struct event **head, struct event *cur_evt)
429 {
430 	struct event *tmp, *tmp2;
431 
432 	if (*head) {
433 		/* Insert this one in order */
434 		tmp = *head;
435 		tmp2 = NULL;
436 		while (tmp->next &&
437 		    tmp->when <= cur_evt->when) {
438 			tmp2 = tmp;
439 			tmp = tmp->next;
440 		}
441 		if (tmp->when > cur_evt->when) {
442 			cur_evt->next = tmp;
443 			if (tmp2)
444 				tmp2->next = cur_evt;
445 			else
446 				*head = cur_evt;
447 		} else {
448 			cur_evt->next = tmp->next;
449 			tmp->next = cur_evt;
450 		}
451 	} else {
452 		*head = cur_evt;
453 		cur_evt->next = NULL;
454 	}
455 }
456