]> git.cameronkatri.com Git - apple_cmds.git/blob - misc_cmds/calendar/io.c
Merge branch 'apple'
[apple_cmds.git] / misc_cmds / calendar / io.c
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: src/usr.bin/calendar/io.c,v 1.24 2007/12/30 22:04:04 grog Exp $");
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 <libiosexec.h>
67
68 #include "pathnames.h"
69 #include "calendar.h"
70
71
72 const char *calendarFile = "calendar"; /* default calendar file */
73 const char *calendarHomes[] = { ".calendar", _PATH_INCLUDE }; /* HOME */
74 const char *calendarNoMail = "nomail"; /* don't sent mail if this file exist */
75
76 struct fixs neaster, npaskha;
77
78 struct iovec header[] = {
79 {"From: ", 6},
80 {NULL, 0},
81 {" (Reminder Service)\nTo: ", 24},
82 {NULL, 0},
83 {"\nSubject: ", 10},
84 {NULL, 0},
85 {"'s Calendar\nPrecedence: bulk\n\n", 30},
86 };
87
88
89 void
90 cal(void)
91 {
92 int printing;
93 char *p;
94 FILE *fp;
95 int ch, l;
96 int month;
97 int day;
98 int var;
99 static int d_first = -1;
100 char buf[2048 + 1];
101 struct event *events = NULL;
102
103 if ((fp = opencal()) == NULL)
104 return;
105 for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
106 if ((p = strchr(buf, '\n')) != NULL)
107 *p = '\0';
108 else
109 while ((ch = getchar()) != '\n' && ch != EOF);
110 for (l = strlen(buf);
111 l > 0 && isspace((unsigned char)buf[l - 1]);
112 l--)
113 ;
114 buf[l] = '\0';
115 if (buf[0] == '\0')
116 continue;
117 if (strncmp(buf, "LANG=", 5) == 0) {
118 (void) setlocale(LC_ALL, buf + 5);
119 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
120 setnnames();
121 continue;
122 }
123 if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) {
124 if (neaster.name != NULL)
125 free(neaster.name);
126 if ((neaster.name = strdup(buf + 7)) == NULL)
127 errx(1, "cannot allocate memory");
128 neaster.len = strlen(buf + 7);
129 continue;
130 }
131 if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) {
132 if (npaskha.name != NULL)
133 free(npaskha.name);
134 if ((npaskha.name = strdup(buf + 7)) == NULL)
135 errx(1, "cannot allocate memory");
136 npaskha.len = strlen(buf + 7);
137 continue;
138 }
139 if (buf[0] != '\t') {
140 printing = isnow(buf, &month, &day, &var) ? 1 : 0;
141 if ((p = strchr(buf, '\t')) == NULL) {
142 #if DEBUG
143 fprintf(stderr, "[continue <%s>]\n", buf);
144 #endif
145 continue;
146 }
147 if (p > buf && p[-1] == '*')
148 var = 1;
149 if (printing) {
150 #if DEBUG
151 fprintf(stderr, "[Add event]\n");
152 #endif
153 struct tm tm;
154 char dbuf[80];
155
156 if (d_first < 0)
157 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
158 tm.tm_sec = 0; /* unused */
159 tm.tm_min = 0; /* unused */
160 tm.tm_hour = 0; /* unused */
161 tm.tm_wday = 0; /* unused */
162 tm.tm_mon = month - 1;
163 tm.tm_mday = day;
164 tm.tm_year = tp->tm_year; /* unused */
165 (void)strftime(dbuf, sizeof(dbuf),
166 d_first ? "%e %b" : "%b %e",
167 &tm);
168 events = event_add(events, month, day, dbuf, var, p);
169 }
170 }
171 else if (printing)
172 event_continue(events, buf);
173 }
174
175 event_print_all(fp, events);
176 closecal(fp);
177 }
178
179 /*
180 * Functions to handle buffered calendar events.
181 */
182 struct event *
183 event_add(struct event *events, int month, int day, char *date, int var, char *txt)
184 {
185 struct event *e;
186
187 e = (struct event *)calloc(1, sizeof(struct event));
188 if (e == NULL)
189 errx(1, "event_add: cannot allocate memory");
190 e->month = month;
191 e->day = day;
192 e->var = var;
193 e->date = strdup(date);
194 if (e->date == NULL)
195 errx(1, "event_add: cannot allocate memory");
196 e->text = strdup(txt);
197 if (e->text == NULL)
198 errx(1, "event_add: cannot allocate memory");
199 e->next = events;
200
201 return e;
202 }
203
204 void
205 event_continue(struct event *e, char *txt)
206 {
207 char *text;
208
209 text = strdup(e->text);
210 if (text == NULL)
211 errx(1, "event_continue: cannot allocate memory");
212
213 free(e->text);
214 e->text = (char *)malloc(strlen(text) + strlen(txt) + 3);
215 if (e->text == NULL)
216 errx(1, "event_continue: cannot allocate memory");
217 strcpy(e->text, text);
218 strcat(e->text, "\n");
219 strcat(e->text, txt);
220 free(text);
221
222 return;
223 }
224
225 void
226 event_print_all(FILE *fp, struct event *events)
227 {
228 struct event *e, *e_next;
229 int daycount = f_dayAfter + f_dayBefore;
230 int daycounter;
231 int day, month;
232
233 for (daycounter = 0; daycounter <= daycount; daycounter++) {
234 day = tp->tm_yday - f_dayBefore + daycounter;
235 if (day < 0) day += yrdays;
236 if (day >= yrdays) day -= yrdays;
237
238 month = 1;
239 while (month <= 12) {
240 if (day <= cumdays[month])
241 break;
242 month++;
243 }
244 month--;
245 day -= cumdays[month];
246
247 #ifdef DEBUG
248 fprintf(stderr,"event_print_allmonth: %d, day: %d\n",month,day);
249 #endif
250
251 for (e = events; e != NULL; e = e_next ) {
252 e_next = e->next;
253
254 if (month != e->month || day != e->day)
255 continue;
256
257 (void)fprintf(fp, "%s%c%s\n", e->date,
258 e->var ? '*' : ' ', e->text);
259 }
260 }
261 }
262
263 int
264 getfield(char *p, char **endp, int *flags)
265 {
266 int val, var;
267 char *start, savech;
268
269 for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p)
270 && *p != '*'; ++p);
271 if (*p == '*') { /* `*' is current month */
272 *flags |= F_ISMONTH;
273 *endp = p+1;
274 return (tp->tm_mon + 1);
275 }
276 if (isdigit((unsigned char)*p)) {
277 val = strtol(p, &p, 10); /* if 0, it's failure */
278 for (; !isdigit((unsigned char)*p)
279 && !isalpha((unsigned char)*p) && *p != '*'; ++p);
280 *endp = p;
281 return (val);
282 }
283 for (start = p; isalpha((unsigned char)*++p););
284
285 /* Sunday-1 */
286 if (*p == '+' || *p == '-')
287 for(; isdigit((unsigned char)*++p););
288
289 savech = *p;
290 *p = '\0';
291
292 /* Month */
293 if ((val = getmonth(start)) != 0)
294 *flags |= F_ISMONTH;
295
296 /* Day */
297 else if ((val = getday(start)) != 0) {
298 *flags |= F_ISDAY;
299
300 /* variable weekday */
301 if ((var = getdayvar(start)) != 0) {
302 if (var <=5 && var >= -4)
303 val += var * 10;
304 #ifdef DEBUG
305 printf("var: %d\n", var);
306 #endif
307 }
308 }
309
310 /* Easter */
311 else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
312 *flags |= F_EASTER;
313
314 /* Paskha */
315 else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
316 *flags |= F_EASTER;
317
318 /* undefined rest */
319 else {
320 *p = savech;
321 return (0);
322 }
323 for (*p = savech; !isdigit((unsigned char)*p)
324 && !isalpha((unsigned char)*p) && *p != '*'; ++p);
325 *endp = p;
326 return (val);
327 }
328
329 char path[MAXPATHLEN];
330
331 FILE *
332 opencal(void)
333 {
334 uid_t uid;
335 size_t i;
336 int fd, found, pdes[2];
337 struct stat sbuf;
338
339 /* open up calendar file as stdin */
340 if (!freopen(calendarFile, "r", stdin)) {
341 if (doall) {
342 if (chdir(calendarHomes[0]) != 0)
343 return (NULL);
344 if (stat(calendarNoMail, &sbuf) == 0)
345 return (NULL);
346 if (!freopen(calendarFile, "r", stdin))
347 return (NULL);
348 } else {
349 char *home = getenv("HOME");
350 if (home == NULL || *home == '\0')
351 errx(1, "cannot get home directory");
352 chdir(home);
353 for (found = i = 0; i < sizeof(calendarHomes) /
354 sizeof(calendarHomes[0]); i++)
355 if (chdir(calendarHomes[i]) == 0 &&
356 freopen(calendarFile, "r", stdin)) {
357 found = 1;
358 break;
359 }
360 if (!found)
361 errx(1, "can't open calendar file \"%s\": %s (%d)",
362 calendarFile, strerror(errno), errno);
363 }
364 }
365 if (pipe(pdes) < 0)
366 return (NULL);
367 switch (fork()) {
368 case -1: /* error */
369 (void)close(pdes[0]);
370 (void)close(pdes[1]);
371 return (NULL);
372 case 0:
373 /* child -- stdin already setup, set stdout to pipe input */
374 if (pdes[1] != STDOUT_FILENO) {
375 (void)dup2(pdes[1], STDOUT_FILENO);
376 (void)close(pdes[1]);
377 }
378 (void)close(pdes[0]);
379 uid = geteuid();
380 if (setuid(getuid()) < 0) {
381 warnx("first setuid failed");
382 _exit(1);
383 };
384 if (setgid(getegid()) < 0) {
385 warnx("setgid failed");
386 _exit(1);
387 }
388 if (setuid(uid) < 0) {
389 warnx("setuid failed");
390 _exit(1);
391 }
392 execl(_PATH_CPP, "cpp", "-w", "-P",
393 "-traditional", "-nostdinc", /* GCC specific opts */
394 "-I.", "-I" _PATH_INCLUDE, (char *)NULL);
395 warn(_PATH_CPP);
396 _exit(1);
397 }
398 /* parent -- set stdin to pipe output */
399 (void)dup2(pdes[0], STDIN_FILENO);
400 (void)close(pdes[0]);
401 (void)close(pdes[1]);
402
403 /* not reading all calendar files, just set output to stdout */
404 if (!doall)
405 return (stdout);
406
407 /* set output to a temporary file, so if no output don't send mail */
408 (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
409 if ((fd = mkstemp(path)) < 0)
410 return (NULL);
411 return (fdopen(fd, "w+"));
412 }
413
414 void
415 closecal(FILE *fp)
416 {
417 uid_t uid;
418 struct stat sbuf;
419 int nread, pdes[2], status;
420 char buf[1024];
421
422 if (!doall)
423 return;
424
425 (void)rewind(fp);
426 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
427 goto done;
428 if (pipe(pdes) < 0)
429 goto done;
430 switch (fork()) {
431 case -1: /* error */
432 (void)close(pdes[0]);
433 (void)close(pdes[1]);
434 goto done;
435 case 0:
436 /* child -- set stdin to pipe output */
437 if (pdes[0] != STDIN_FILENO) {
438 (void)dup2(pdes[0], STDIN_FILENO);
439 (void)close(pdes[0]);
440 }
441 (void)close(pdes[1]);
442 uid = geteuid();
443 if (setuid(getuid()) < 0) {
444 warnx("setuid failed");
445 _exit(1);
446 };
447 if (setgid(getegid()) < 0) {
448 warnx("setgid failed");
449 _exit(1);
450 }
451 if (setuid(uid) < 0) {
452 warnx("setuid failed");
453 _exit(1);
454 }
455 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
456 "\"Reminder Service\"", (char *)NULL);
457 warn(_PATH_SENDMAIL);
458 _exit(1);
459 }
460 /* parent -- write to pipe input */
461 (void)close(pdes[0]);
462
463 header[1].iov_base = header[3].iov_base = pw->pw_name;
464 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
465 writev(pdes[1], header, 7);
466 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
467 (void)write(pdes[1], buf, nread);
468 (void)close(pdes[1]);
469 done: (void)fclose(fp);
470 (void)unlink(path);
471 while (wait(&status) >= 0);
472 }