1 /* $NetBSD: calendar.c,v 1.27 2002/11/30 03:10:55 lukem 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) 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 #if 0 44 static char sccsid[] = "@(#)calendar.c 8.4 (Berkeley) 1/7/95"; 45 #endif 46 __RCSID("$NetBSD: calendar.c,v 1.27 2002/11/30 03:10:55 lukem Exp $"); 47 #endif /* not lint */ 48 49 #include <sys/param.h> 50 #include <sys/time.h> 51 #include <sys/stat.h> 52 #include <sys/uio.h> 53 #include <sys/wait.h> 54 55 #include <ctype.h> 56 #include <err.h> 57 #include <errno.h> 58 #include <fcntl.h> 59 #include <pwd.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <time.h> 64 #include <tzfile.h> 65 #include <unistd.h> 66 67 #include "pathnames.h" 68 69 #ifndef TRUE 70 #define TRUE 1 71 #endif 72 #ifndef FALSE 73 #define FALSE 0 74 #endif 75 76 static unsigned short lookahead = 1, weekend = 2; 77 static char *fname = "calendar", *datestr = NULL; 78 static struct passwd *pw; 79 static int doall; 80 static char path[MAXPATHLEN + 1]; 81 82 /* 1-based month, 0-based days, cumulative */ 83 static int daytab[][14] = { 84 { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 }, 85 { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, 86 }; 87 static struct tm *tp; 88 static int *cumdays, offset, yrdays; 89 static char dayname[10]; 90 91 static struct iovec header[] = { 92 { "From: ", 6 }, 93 { NULL, 0 }, 94 { " (Reminder Service)\nTo: ", 24 }, 95 { NULL, 0 }, 96 { "\nSubject: ", 10 }, 97 { NULL, 0 }, 98 { "'s Calendar\nPrecedence: bulk\n\n", 30 }, 99 }; 100 101 static char *days[] = { 102 "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL, 103 }; 104 105 static char *months[] = { 106 "jan", "feb", "mar", "apr", "may", "jun", 107 "jul", "aug", "sep", "oct", "nov", "dec", NULL, 108 }; 109 110 int main(int, char **); 111 112 static void atodays(int, char *, unsigned short *); 113 static void cal(void); 114 static void closecal(FILE *); 115 static int getday(char *); 116 static int getfield(char *, char **, int *); 117 static void getmmdd(struct tm *, char *); 118 static int getmonth(char *); 119 static int isnow(char *); 120 static FILE *opencal(void); 121 static void settime(void); 122 static void usage(void) __attribute__((__noreturn__)); 123 124 int 125 main(argc, argv) 126 int argc; 127 char *argv[]; 128 { 129 int ch; 130 const char *caldir; 131 132 while ((ch = getopt(argc, argv, "-ad:f:l:w:")) != -1) 133 switch (ch) { 134 case '-': /* backward contemptible */ 135 case 'a': 136 if (getuid()) { 137 errno = EPERM; 138 err(1, NULL); 139 } 140 doall = 1; 141 break; 142 case 'd': 143 datestr = optarg; 144 break; 145 case 'f': 146 fname = optarg; 147 break; 148 case 'l': 149 atodays(ch, optarg, &lookahead); 150 break; 151 case 'w': 152 atodays(ch, optarg, &weekend); 153 break; 154 case '?': 155 default: 156 usage(); 157 } 158 argc -= optind; 159 argv += optind; 160 161 if (argc) 162 usage(); 163 164 settime(); 165 if (doall) { 166 while ((pw = getpwent()) != NULL) { 167 (void)setegid(pw->pw_gid); 168 (void)seteuid(pw->pw_uid); 169 if (!chdir(pw->pw_dir)) 170 cal(); 171 (void)seteuid(0); 172 } 173 } else if ((caldir = getenv("CALENDAR_DIR")) != NULL) { 174 if(!chdir(caldir)) 175 cal(); 176 } else { 177 cal(); 178 } 179 exit(0); 180 } 181 182 static void 183 cal(void) 184 { 185 int printing; 186 FILE *fp; 187 char *line; 188 189 if ((fp = opencal()) == NULL) 190 return; 191 while ((line = fparseln(stdin, NULL, NULL, NULL, 0)) != NULL) { 192 if (line[0] == '\0') 193 continue; 194 if (line[0] != '\t') 195 printing = isnow(line) ? 1 : 0; 196 if (printing) 197 (void)fprintf(fp, "%s\n", line); 198 free(line); 199 200 } 201 closecal(fp); 202 } 203 204 205 static void 206 settime(void) 207 { 208 time_t now; 209 210 (void)time(&now); 211 tp = localtime(&now); 212 if (datestr) { 213 getmmdd(tp, datestr); 214 } 215 if (isleap(tp->tm_year + TM_YEAR_BASE)) { 216 yrdays = DAYSPERLYEAR; 217 cumdays = daytab[1]; 218 } else { 219 yrdays = DAYSPERNYEAR; 220 cumdays = daytab[0]; 221 } 222 /* Friday displays Monday's events */ 223 offset = tp->tm_wday == 5 ? lookahead + weekend : lookahead; 224 header[5].iov_base = dayname; 225 header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp); 226 } 227 228 /* 229 * Possible date formats include any combination of: 230 * 3-charmonth (January, Jan, Jan) 231 * 3-charweekday (Friday, Monday, mon.) 232 * numeric month or day (1, 2, 04) 233 * 234 * Any character may separate them, or they may not be separated. Any line, 235 * following a line that is matched, that starts with "whitespace", is shown 236 * along with the matched line. 237 */ 238 static int 239 isnow(endp) 240 char *endp; 241 { 242 int day, flags, month, v1, v2; 243 244 #define F_ISMONTH 0x01 245 #define F_ISDAY 0x02 246 flags = 0; 247 /* didn't recognize anything, skip it */ 248 if (!(v1 = getfield(endp, &endp, &flags))) 249 return (0); 250 if (flags & F_ISDAY || v1 > 12) { 251 /* found a day */ 252 day = v1; 253 month = tp->tm_mon + 1; 254 } else if (flags & F_ISMONTH) { 255 month = v1; 256 /* if no recognizable day, assume the first */ 257 if (!(day = getfield(endp, &endp, &flags))) 258 day = 1; 259 } else { 260 v2 = getfield(endp, &endp, &flags); 261 if (flags & F_ISMONTH) { 262 day = v1; 263 month = v2; 264 } else { 265 /* F_ISDAY set, v2 > 12, or no way to tell */ 266 month = v1; 267 /* if no recognizable day, assume the first */ 268 day = v2 ? v2 : 1; 269 } 270 } 271 if (flags & F_ISDAY) 272 day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); 273 day = cumdays[month] + day; 274 275 /* if today or today + offset days */ 276 if (day >= tp->tm_yday && day <= tp->tm_yday + offset) 277 return (1); 278 /* if number of days left in this year + days to event in next year */ 279 if (yrdays - tp->tm_yday + day <= offset) 280 return (1); 281 return (0); 282 } 283 284 static int 285 getfield(p, endp, flags) 286 char *p, **endp; 287 int *flags; 288 { 289 int val; 290 char *start, savech; 291 292 #define FLDCHAR(a) (*p != '\0' && !isdigit((unsigned char)*p) && \ 293 !isalpha((unsigned char)*p) && *p != '*') 294 295 for (; FLDCHAR(*p); ++p) 296 continue; 297 if (*p == '*') { /* `*' is current month */ 298 *flags |= F_ISMONTH; 299 *endp = p+1; 300 return (tp->tm_mon + 1); 301 } 302 if (isdigit((unsigned char)*p)) { 303 val = strtol(p, &p, 10); /* if 0, it's failure */ 304 for (; FLDCHAR(*p); ++p) 305 continue; 306 *endp = p; 307 return (val); 308 } 309 for (start = p; *p != '\0' && isalpha((unsigned char)*++p);) 310 continue; 311 savech = *p; 312 *p = '\0'; 313 if ((val = getmonth(start)) != 0) { 314 *flags |= F_ISMONTH; 315 } else if ((val = getday(start)) != 0) { 316 *flags |= F_ISDAY; 317 } else { 318 *p = savech; 319 return (0); 320 } 321 for (*p = savech; FLDCHAR(*p); ++p) 322 continue; 323 *endp = p; 324 return (val); 325 } 326 327 static FILE * 328 opencal(void) 329 { 330 int fd, pdes[2]; 331 332 /* open up calendar file as stdin */ 333 if (!freopen(fname, "rf", stdin)) { 334 if (doall) 335 return (NULL); 336 err(1, "Cannot open `%s'", fname); 337 } 338 if (pipe(pdes) < 0) { 339 warn("Cannot open pipe"); 340 return (NULL); 341 } 342 switch (fork()) { 343 case -1: /* error */ 344 (void)close(pdes[0]); 345 (void)close(pdes[1]); 346 return (NULL); 347 case 0: 348 /* child -- stdin already setup, set stdout to pipe input */ 349 if (pdes[1] != STDOUT_FILENO) { 350 (void)dup2(pdes[1], STDOUT_FILENO); 351 (void)close(pdes[1]); 352 } 353 (void)close(pdes[0]); 354 (void)execl(_PATH_CPP, "cpp", "-traditional", "-P", "-I.", 355 "-I" _PATH_CALENDARS, NULL); 356 err(1, "Cannot exec `%s'", _PATH_CPP); 357 /*NOTREACHED*/ 358 } 359 /* parent -- set stdin to pipe output */ 360 (void)dup2(pdes[0], STDIN_FILENO); 361 (void)close(pdes[0]); 362 (void)close(pdes[1]); 363 364 /* not reading all calendar files, just set output to stdout */ 365 if (!doall) 366 return (stdout); 367 368 /* set output to a temporary file, so if no output don't send mail */ 369 (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP); 370 if ((fd = mkstemp(path)) < 0) { 371 warn("Cannot create temporary file"); 372 return (NULL); 373 } 374 return (fdopen(fd, "w+")); 375 } 376 377 static void 378 closecal(fp) 379 FILE *fp; 380 { 381 struct stat sbuf; 382 int nread, pdes[2], status; 383 char buf[1024]; 384 385 if (!doall) 386 return; 387 388 (void)rewind(fp); 389 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size) 390 goto done; 391 if (pipe(pdes) < 0) 392 goto done; 393 switch (fork()) { 394 case -1: /* error */ 395 (void)close(pdes[0]); 396 (void)close(pdes[1]); 397 goto done; 398 case 0: 399 /* child -- set stdin to pipe output */ 400 if (pdes[0] != STDIN_FILENO) { 401 (void)dup2(pdes[0], STDIN_FILENO); 402 (void)close(pdes[0]); 403 } 404 (void)close(pdes[1]); 405 (void)execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F", 406 "\"Reminder Service\"", "-f", "root", NULL); 407 err(1, "Cannot exec `%s'", _PATH_SENDMAIL); 408 /*NOTREACHED*/ 409 } 410 /* parent -- write to pipe input */ 411 (void)close(pdes[0]); 412 413 header[1].iov_base = header[3].iov_base = (void *)pw->pw_name; 414 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name); 415 writev(pdes[1], header, 7); 416 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0) 417 (void)write(pdes[1], buf, nread); 418 (void)close(pdes[1]); 419 420 done: (void)fclose(fp); 421 (void)unlink(path); 422 while (wait(&status) >= 0) 423 continue; 424 } 425 426 static int 427 getmonth(s) 428 char *s; 429 { 430 char **p; 431 432 for (p = months; *p; ++p) 433 if (!strncasecmp(s, *p, 3)) 434 return ((p - months) + 1); 435 return (0); 436 } 437 438 static int 439 getday(s) 440 char *s; 441 { 442 char **p; 443 444 for (p = days; *p; ++p) 445 if (!strncasecmp(s, *p, 3)) 446 return ((p - days) + 1); 447 return (0); 448 } 449 450 static void 451 atodays(int ch, char *optarg, unsigned short *days) 452 { 453 int u; 454 455 u = atoi(optarg); 456 if ((u < 0) || (u > 366)) { 457 warnx("-%c %d out of range 0-366, ignored.", ch, u); 458 } else { 459 *days = u; 460 } 461 } 462 463 #define todigit(x) ((x) - '0') 464 #define ATOI2(x) (todigit((x)[0]) * 10 + todigit((x)[1])) 465 #define ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1])) 466 467 static void 468 getmmdd(struct tm *tp, char *ds) 469 { 470 int ok = FALSE; 471 struct tm ttm; 472 473 ttm = *tp; 474 ttm.tm_isdst = -1; 475 476 if (ISDIG2(ds)) { 477 ttm.tm_mon = ATOI2(ds) - 1; 478 ds += 2; 479 } 480 481 if (ISDIG2(ds)) { 482 ttm.tm_mday = ATOI2(ds); 483 ds += 2; 484 485 ok = TRUE; 486 } 487 488 if (ok) { 489 if (ISDIG2(ds) && ISDIG2(ds + 2)) { 490 ttm.tm_year = ATOI2(ds) * 100 - TM_YEAR_BASE; 491 ds += 2; 492 ttm.tm_year += ATOI2(ds); 493 } else if (ISDIG2(ds)) { 494 ttm.tm_year = ATOI2(ds); 495 if (ttm.tm_year < 69) 496 ttm.tm_year += 2000 - TM_YEAR_BASE; 497 else 498 ttm.tm_year += 1900 - TM_YEAR_BASE; 499 } 500 } 501 502 if (ok && (mktime(&ttm) < 0)) { 503 ok = FALSE; 504 } 505 506 if (ok) { 507 *tp = ttm; 508 } else { 509 warnx("Can't convert `%s' to date, ignored.", ds); 510 usage(); 511 } 512 } 513 514 static void 515 usage(void) 516 { 517 (void)fprintf(stderr, "Usage: %s [-a] [-d MMDD[[YY]YY]" 518 " [-f fname] [-l days] [-w days]\n", getprogname()); 519 exit(1); 520 } 521