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