]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.bin/calendar/io.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.bin / 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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1989, 1993\n\
33         The Regents of the University of California.  All rights reserved.\n";
34 #endif
35
36 #if 0
37 #ifndef lint
38 static char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
39 #endif
40 #endif
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <sys/wait.h>
48 #include <ctype.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <langinfo.h>
52 #include <locale.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #include "pathnames.h"
60 #include "calendar.h"
61
62 const char *calendarFile = "calendar";  /* default calendar file */
63 static const char *calendarHomes[] = {".calendar", _PATH_INCLUDE}; /* HOME */
64 static const char *calendarNoMail = "nomail";/* don't sent mail if file exist */
65
66 static char path[MAXPATHLEN];
67
68 struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon;
69 struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice;
70
71 #define REPLACE(string, slen, struct_) \
72                 if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \
73                         if (struct_.name != NULL)                             \
74                                 free(struct_.name);                           \
75                         if ((struct_.name = strdup(buf + (slen))) == NULL)    \
76                                 errx(1, "cannot allocate memory");            \
77                         struct_.len = strlen(buf + (slen));                   \
78                         continue;                                             \
79                 }
80 void
81 cal(void)
82 {
83         char *pp, p;
84         FILE *fpin;
85         FILE *fpout;
86         int l;
87         int count, i;
88         int month[MAXCOUNT];
89         int day[MAXCOUNT];
90         int year[MAXCOUNT];
91         char **extradata;       /* strings of 20 length */
92         int flags;
93         static int d_first = -1;
94         char buf[2048 + 1];
95         struct event *events[MAXCOUNT];
96         struct tm tm;
97         char dbuf[80];
98
99         initcpp();
100         extradata = (char **)calloc(MAXCOUNT, sizeof(char *));
101         for (i = 0; i < MAXCOUNT; i++) {
102                 extradata[i] = (char *)calloc(1, 20);
103         }
104
105         /* Unused */
106         tm.tm_sec = 0;
107         tm.tm_min = 0;
108         tm.tm_hour = 0;
109         tm.tm_wday = 0;
110
111         count = 0;
112         if ((fpin = opencalin()) == NULL) {
113                 free(extradata);
114                 return;
115         }
116         if ((fpout = opencalout()) == NULL) {
117                 fclose(fpin);
118                 free(extradata);
119                 return;
120         }
121         while ((fpin = fincludegets(buf, sizeof(buf), fpin)) != NULL) {
122                 if (*buf == '\0')
123                         continue;
124                 for (l = strlen(buf);
125                      l > 0 && isspace((unsigned char)buf[l - 1]);
126                      l--)
127                         ;
128                 buf[l] = '\0';
129                 if (buf[0] == '\0')
130                         continue;
131
132                 /* Parse special definitions: LANG, Easter, Paskha etc */
133                 if (strncmp(buf, "LANG=", 5) == 0) {
134                         (void)setlocale(LC_ALL, buf + 5);
135                         d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
136                         setnnames();
137                         continue;
138                 }
139                 REPLACE("Easter=", 7, neaster);
140                 REPLACE("Paskha=", 7, npaskha);
141                 REPLACE("ChineseNewYear=", 15, ncny);
142                 REPLACE("NewMoon=", 8, nnewmoon);
143                 REPLACE("FullMoon=", 9, nfullmoon);
144                 REPLACE("MarEquinox=", 11, nmarequinox);
145                 REPLACE("SepEquinox=", 11, nsepequinox);
146                 REPLACE("JunSolstice=", 12, njunsolstice);
147                 REPLACE("DecSolstice=", 12, ndecsolstice);
148                 if (strncmp(buf, "SEQUENCE=", 9) == 0) {
149                         setnsequences(buf + 9);
150                         continue;
151                 }
152
153                 /*
154                  * If the line starts with a tab, the data has to be
155                  * added to the previous line
156                  */
157                 if (buf[0] == '\t') {
158                         for (i = 0; i < count; i++)
159                                 event_continue(events[i], buf);
160                         continue;
161                 }
162
163                 /* Get rid of leading spaces (non-standard) */
164                 while (isspace((unsigned char)buf[0]))
165                         memcpy(buf, buf + 1, strlen(buf));
166
167                 /* No tab in the line, then not a valid line */
168                 if ((pp = strchr(buf, '\t')) == NULL)
169                         continue;
170
171                 /* Trim spaces in front of the tab */
172                 while (isspace((unsigned char)pp[-1]))
173                         pp--;
174
175                 p = *pp;
176                 *pp = '\0';
177                 if ((count = parsedaymonth(buf, year, month, day, &flags,
178                     extradata)) == 0)
179                         continue;
180                 *pp = p;
181                 if (count < 0) {
182                         /* Show error status based on return value */
183                         if (debug)
184                                 fprintf(stderr, "Ignored: %s\n", buf);
185                         if (count == -1)
186                                 continue;
187                         count = -count + 1;
188                 }
189
190                 /* Find the last tab */
191                 while (pp[1] == '\t')
192                         pp++;
193
194                 if (d_first < 0)
195                         d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
196
197                 for (i = 0; i < count; i++) {
198                         tm.tm_mon = month[i] - 1;
199                         tm.tm_mday = day[i];
200                         tm.tm_year = year[i] - 1900;
201                         (void)strftime(dbuf, sizeof(dbuf),
202                             d_first ? "%e %b" : "%b %e", &tm);
203                         if (debug)
204                                 fprintf(stderr, "got %s\n", pp);
205                         events[i] = event_add(year[i], month[i], day[i], dbuf,
206                             ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp,
207                             extradata[i]);
208                 }
209         }
210
211         event_print_all(fpout);
212         closecal(fpout);
213         free(extradata);
214 }
215
216 FILE *
217 opencalin(void)
218 {
219         size_t i;
220         int found;
221         struct stat sbuf;
222         FILE *fpin;
223
224         /* open up calendar file */
225         if ((fpin = fopen(calendarFile, "r")) == NULL) {
226                 if (doall) {
227                         if (chdir(calendarHomes[0]) != 0)
228                                 return (NULL);
229                         if (stat(calendarNoMail, &sbuf) == 0)
230                                 return (NULL);
231                         if ((fpin = fopen(calendarFile, "r")) == NULL)
232                                 return (NULL);
233                 } else {
234                         char *home = getenv("HOME");
235                         if (home == NULL || *home == '\0')
236                                 errx(1, "cannot get home directory");
237                         if (chdir(home) != 0)
238                                 errx(1, "cannot enter home directory");
239                         for (found = i = 0; i < sizeof(calendarHomes) /
240                             sizeof(calendarHomes[0]); i++)
241                                 if (chdir(calendarHomes[i]) == 0 &&
242                                     (fpin = fopen(calendarFile, "r")) != NULL) {
243                                         found = 1;
244                                         break;
245                                 }
246                         if (!found)
247                                 errx(1,
248                                     "can't open calendar file \"%s\": %s (%d)",
249                                     calendarFile, strerror(errno), errno);
250                 }
251         }
252         return (fpin);
253 }
254
255 FILE *
256 opencalout(void)
257 {
258         int fd;
259
260         /* not reading all calendar files, just set output to stdout */
261         if (!doall)
262                 return (stdout);
263
264         /* set output to a temporary file, so if no output don't send mail */
265         snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
266         if ((fd = mkstemp(path)) < 0)
267                 return (NULL);
268         return (fdopen(fd, "w+"));
269 }
270
271 void
272 closecal(FILE *fp)
273 {
274         uid_t uid;
275         struct stat sbuf;
276         int nread, pdes[2], status;
277         char buf[1024];
278
279         if (!doall)
280                 return;
281
282         rewind(fp);
283         if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
284                 goto done;
285         if (pipe(pdes) < 0)
286                 goto done;
287         switch (fork()) {
288         case -1:                        /* error */
289                 (void)close(pdes[0]);
290                 (void)close(pdes[1]);
291                 goto done;
292         case 0:
293                 /* child -- set stdin to pipe output */
294                 if (pdes[0] != STDIN_FILENO) {
295                         (void)dup2(pdes[0], STDIN_FILENO);
296                         (void)close(pdes[0]);
297                 }
298                 (void)close(pdes[1]);
299                 uid = geteuid();
300                 if (setuid(getuid()) < 0) {
301                         warnx("setuid failed");
302                         _exit(1);
303                 };
304                 if (setgid(getegid()) < 0) {
305                         warnx("setgid failed");
306                         _exit(1);
307                 }
308                 if (setuid(uid) < 0) {
309                         warnx("setuid failed");
310                         _exit(1);
311                 }
312                 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
313                     "\"Reminder Service\"", (char *)NULL);
314                 warn(_PATH_SENDMAIL);
315                 _exit(1);
316         }
317         /* parent -- write to pipe input */
318         (void)close(pdes[0]);
319
320         write(pdes[1], "From: \"Reminder Service\" <", 26);
321         write(pdes[1], pw->pw_name, strlen(pw->pw_name));
322         write(pdes[1], ">\nTo: <", 7);
323         write(pdes[1], pw->pw_name, strlen(pw->pw_name));
324         write(pdes[1], ">\nSubject: ", 11);
325         write(pdes[1], dayname, strlen(dayname));
326         write(pdes[1], "'s Calendar\nPrecedence: bulk\n\n", 30);
327
328         while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
329                 (void)write(pdes[1], buf, nread);
330         (void)close(pdes[1]);
331 done:   (void)fclose(fp);
332         (void)unlink(path);
333         while (wait(&status) >= 0);
334 }