1 /* $OpenBSD: io.c,v 1.35 2009/10/27 23:59:36 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(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p) 226 ; 227 if (*p == '*') { /* `*' is every month */ 228 *flags |= F_ISMONTH; 229 *endp = p+1; 230 return (-1); /* means 'every month' */ 231 } 232 if (isdigit(*p)) { 233 val = strtol(p, &p, 10); /* if 0, it's failure */ 234 for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p) 235 ; 236 *endp = p; 237 return (val); 238 } 239 for (start = p; isalpha(*++p);) 240 ; 241 242 /* Sunday-1 */ 243 if (*p == '+' || *p == '-') 244 for(; isdigit(*++p); ) 245 ; 246 247 savech = *p; 248 *p = '\0'; 249 250 /* Month */ 251 if ((val = getmonth(start)) != 0) 252 *flags |= F_ISMONTH; 253 254 /* Day */ 255 else if ((val = getday(start)) != 0) { 256 *flags |= F_ISDAY; 257 258 /* variable weekday */ 259 if ((var = getdayvar(start)) != 0) { 260 if (var <= 5 && var >= -4) 261 val += var * 10; 262 #ifdef DEBUG 263 printf("var: %d\n", var); 264 #endif 265 } 266 } 267 268 /* Try specials (Easter, Paskha, ...) */ 269 else { 270 for (i = 0; i < NUMEV; i++) { 271 if (strncasecmp(start, spev[i].name, spev[i].nlen) == 0) { 272 start += spev[i].nlen; 273 val = i + 1; 274 i = NUMEV + 1; 275 } else if (spev[i].uname != NULL && 276 strncasecmp(start, spev[i].uname, spev[i].ulen) == 0) { 277 start += spev[i].ulen; 278 val = i + 1; 279 i = NUMEV + 1; 280 } 281 } 282 if (i > NUMEV) { 283 switch(*start) { 284 case '-': 285 case '+': 286 var = atoi(start); 287 if (var > 365 || var < -365) 288 return (0); /* Someone is just being silly */ 289 val += (NUMEV + 1) * var; 290 /* We add one to the matching event and multiply by 291 * (NUMEV + 1) so as not to return 0 if there's a match. 292 * val will overflow if there is an obscenely large 293 * number of special events. */ 294 break; 295 } 296 *flags |= F_SPECIAL; 297 } 298 if (!(*flags & F_SPECIAL)) { 299 /* undefined rest */ 300 *p = savech; 301 return (0); 302 } 303 } 304 for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p) 305 ; 306 *endp = p; 307 return (val); 308 } 309 310 311 FILE * 312 opencal(void) 313 { 314 int pdes[2], fdin; 315 struct stat st; 316 317 /* open up calendar file as stdin */ 318 if ((fdin = open(calendarFile, O_RDONLY)) == -1 || 319 fstat(fdin, &st) == -1 || !S_ISREG(st.st_mode)) { 320 if (!doall) { 321 char *home = getenv("HOME"); 322 if (home == NULL || *home == '\0') 323 errx(1, "cannot get home directory"); 324 if (!(chdir(home) == 0 && 325 chdir(calendarHome) == 0 && 326 (fdin = open(calendarFile, O_RDONLY)) != -1)) 327 errx(1, "no calendar file: ``%s'' or ``~/%s/%s''", 328 calendarFile, calendarHome, calendarFile); 329 } 330 } 331 332 if (pipe(pdes) < 0) 333 return (NULL); 334 switch (vfork()) { 335 case -1: /* error */ 336 (void)close(pdes[0]); 337 (void)close(pdes[1]); 338 return (NULL); 339 case 0: 340 dup2(fdin, STDIN_FILENO); 341 /* child -- set stdout to pipe input */ 342 if (pdes[1] != STDOUT_FILENO) { 343 (void)dup2(pdes[1], STDOUT_FILENO); 344 (void)close(pdes[1]); 345 } 346 (void)close(pdes[0]); 347 /* 348 * Set stderr to /dev/null. Necessary so that cron does not 349 * wait for cpp to finish if it's running calendar -a. 350 */ 351 if (doall) { 352 int fderr; 353 fderr = open(_PATH_DEVNULL, O_WRONLY, 0); 354 if (fderr == -1) 355 _exit(0); 356 (void)dup2(fderr, STDERR_FILENO); 357 (void)close(fderr); 358 } 359 execl(_PATH_CPP, "cpp", "-traditional", "-undef", "-U__GNUC__", 360 "-P", "-I.", _PATH_INCLUDE, (char *)NULL); 361 warn(_PATH_CPP); 362 _exit(1); 363 } 364 /* parent -- set stdin to pipe output */ 365 (void)dup2(pdes[0], STDIN_FILENO); 366 (void)close(pdes[0]); 367 (void)close(pdes[1]); 368 369 /* not reading all calendar files, just set output to stdout */ 370 if (!doall) 371 return (stdout); 372 373 /* set output to a temporary file, so if no output don't send mail */ 374 return(tmpfile()); 375 } 376 377 void 378 closecal(FILE *fp) 379 { 380 struct stat sbuf; 381 int nread, pdes[2], status; 382 char buf[1024]; 383 384 if (!doall) 385 return; 386 387 (void)rewind(fp); 388 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size) 389 goto done; 390 if (pipe(pdes) < 0) 391 goto done; 392 switch (vfork()) { 393 case -1: /* error */ 394 (void)close(pdes[0]); 395 (void)close(pdes[1]); 396 goto done; 397 case 0: 398 /* child -- set stdin to pipe output */ 399 if (pdes[0] != STDIN_FILENO) { 400 (void)dup2(pdes[0], STDIN_FILENO); 401 (void)close(pdes[0]); 402 } 403 (void)close(pdes[1]); 404 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F", 405 "\"Reminder Service\"", (char *)NULL); 406 warn(_PATH_SENDMAIL); 407 _exit(1); 408 } 409 /* parent -- write to pipe input */ 410 (void)close(pdes[0]); 411 412 header[1].iov_base = header[3].iov_base = pw->pw_name; 413 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name); 414 writev(pdes[1], header, 8); 415 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0) 416 (void)write(pdes[1], buf, nread); 417 (void)close(pdes[1]); 418 done: (void)fclose(fp); 419 while (wait(&status) >= 0) 420 ; 421 } 422 423 424 void 425 insert(struct event **head, struct event *cur_evt) 426 { 427 struct event *tmp, *tmp2; 428 429 if (*head) { 430 /* Insert this one in order */ 431 tmp = *head; 432 tmp2 = NULL; 433 while (tmp->next && 434 tmp->when <= cur_evt->when) { 435 tmp2 = tmp; 436 tmp = tmp->next; 437 } 438 if (tmp->when > cur_evt->when) { 439 cur_evt->next = tmp; 440 if (tmp2) 441 tmp2->next = cur_evt; 442 else 443 *head = cur_evt; 444 } else { 445 cur_evt->next = tmp->next; 446 tmp->next = cur_evt; 447 } 448 } else { 449 *head = cur_evt; 450 cur_evt->next = NULL; 451 } 452 } 453