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