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#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1989, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif 39 40#if 0 41#ifndef lint 42static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; 43#endif 44#endif 45 46#include <sys/cdefs.h> 47__FBSDID("$FreeBSD: src/usr.bin/calendar/io.c,v 1.24 2007/12/30 22:04:04 grog Exp $"); 48 49#include <sys/types.h> 50#include <sys/param.h> 51#include <sys/stat.h> 52#include <sys/time.h> 53#include <sys/uio.h> 54#include <sys/wait.h> 55#include <ctype.h> 56#include <err.h> 57#include <errno.h> 58#include <langinfo.h> 59#include <locale.h> 60#include <pwd.h> 61#include <stdio.h> 62#include <stdlib.h> 63#include <string.h> 64#include <unistd.h> 65 66#include "pathnames.h" 67#include "calendar.h" 68 69 70const char *calendarFile = "calendar"; /* default calendar file */ 71const char *calendarHomes[] = { ".calendar", _PATH_INCLUDE }; /* HOME */ 72const char *calendarNoMail = "nomail"; /* don't sent mail if this file exist */ 73 74struct fixs neaster, npaskha; 75 76struct iovec header[] = { 77 {"From: ", 6}, 78 {NULL, 0}, 79 {" (Reminder Service)\nTo: ", 24}, 80 {NULL, 0}, 81 {"\nSubject: ", 10}, 82 {NULL, 0}, 83 {"'s Calendar\nPrecedence: bulk\n\n", 30}, 84}; 85 86 87void 88cal(void) 89{ 90 int printing; 91 char *p; 92 FILE *fp; 93 int ch, l; 94 int month; 95 int day; 96 int var; 97 static int d_first = -1; 98 char buf[2048 + 1]; 99 struct event *events = NULL; 100 101 if ((fp = opencal()) == NULL) 102 return; 103 for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) { 104 if ((p = strchr(buf, '\n')) != NULL) 105 *p = '\0'; 106 else 107 while ((ch = getchar()) != '\n' && ch != EOF); 108 for (l = strlen(buf); 109 l > 0 && isspace((unsigned char)buf[l - 1]); 110 l--) 111 ; 112 buf[l] = '\0'; 113 if (buf[0] == '\0') 114 continue; 115 if (strncmp(buf, "LANG=", 5) == 0) { 116 (void) setlocale(LC_ALL, buf + 5); 117 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 118 setnnames(); 119 continue; 120 } 121 if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) { 122 if (neaster.name != NULL) 123 free(neaster.name); 124 if ((neaster.name = strdup(buf + 7)) == NULL) 125 errx(1, "cannot allocate memory"); 126 neaster.len = strlen(buf + 7); 127 continue; 128 } 129 if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) { 130 if (npaskha.name != NULL) 131 free(npaskha.name); 132 if ((npaskha.name = strdup(buf + 7)) == NULL) 133 errx(1, "cannot allocate memory"); 134 npaskha.len = strlen(buf + 7); 135 continue; 136 } 137 if (buf[0] != '\t') { 138 printing = isnow(buf, &month, &day, &var) ? 1 : 0; 139 if ((p = strchr(buf, '\t')) == NULL) { 140#if DEBUG 141 fprintf(stderr, "[continue <%s>]\n", buf); 142#endif 143 continue; 144 } 145 if (p > buf && p[-1] == '*') 146 var = 1; 147 if (printing) { 148#if DEBUG 149 fprintf(stderr, "[Add event]\n"); 150#endif 151 struct tm tm; 152 char dbuf[80]; 153 154 if (d_first < 0) 155 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 156 tm.tm_sec = 0; /* unused */ 157 tm.tm_min = 0; /* unused */ 158 tm.tm_hour = 0; /* unused */ 159 tm.tm_wday = 0; /* unused */ 160 tm.tm_mon = month - 1; 161 tm.tm_mday = day; 162 tm.tm_year = tp->tm_year; /* unused */ 163 (void)strftime(dbuf, sizeof(dbuf), 164 d_first ? "%e %b" : "%b %e", 165 &tm); 166 events = event_add(events, month, day, dbuf, var, p); 167 } 168 } 169 else if (printing) 170 event_continue(events, buf); 171 } 172 173 event_print_all(fp, events); 174 closecal(fp); 175} 176 177/* 178 * Functions to handle buffered calendar events. 179 */ 180struct event * 181event_add(struct event *events, int month, int day, char *date, int var, char *txt) 182{ 183 struct event *e; 184 185 e = (struct event *)calloc(1, sizeof(struct event)); 186 if (e == NULL) 187 errx(1, "event_add: cannot allocate memory"); 188 e->month = month; 189 e->day = day; 190 e->var = var; 191 e->date = strdup(date); 192 if (e->date == NULL) 193 errx(1, "event_add: cannot allocate memory"); 194 e->text = strdup(txt); 195 if (e->text == NULL) 196 errx(1, "event_add: cannot allocate memory"); 197 e->next = events; 198 199 return e; 200} 201 202void 203event_continue(struct event *e, char *txt) 204{ 205 char *text; 206 207 text = strdup(e->text); 208 if (text == NULL) 209 errx(1, "event_continue: cannot allocate memory"); 210 211 free(e->text); 212 e->text = (char *)malloc(strlen(text) + strlen(txt) + 3); 213 if (e->text == NULL) 214 errx(1, "event_continue: cannot allocate memory"); 215 strcpy(e->text, text); 216 strcat(e->text, "\n"); 217 strcat(e->text, txt); 218 free(text); 219 220 return; 221} 222 223void 224event_print_all(FILE *fp, struct event *events) 225{ 226 struct event *e, *e_next; 227 int daycount = f_dayAfter + f_dayBefore; 228 int daycounter; 229 int day, month; 230 231 for (daycounter = 0; daycounter <= daycount; daycounter++) { 232 day = tp->tm_yday - f_dayBefore + daycounter; 233 if (day < 0) day += yrdays; 234 if (day >= yrdays) day -= yrdays; 235 236 month = 1; 237 while (month <= 12) { 238 if (day <= cumdays[month]) 239 break; 240 month++; 241 } 242 month--; 243 day -= cumdays[month]; 244 245#ifdef DEBUG 246 fprintf(stderr,"event_print_allmonth: %d, day: %d\n",month,day); 247#endif 248 249 for (e = events; e != NULL; e = e_next ) { 250 e_next = e->next; 251 252 if (month != e->month || day != e->day) 253 continue; 254 255 (void)fprintf(fp, "%s%c%s\n", e->date, 256 e->var ? '*' : ' ', e->text); 257 } 258 } 259} 260 261int 262getfield(char *p, char **endp, int *flags) 263{ 264 int val, var; 265 char *start, savech; 266 267 for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) 268 && *p != '*'; ++p); 269 if (*p == '*') { /* `*' is current month */ 270 *flags |= F_ISMONTH; 271 *endp = p+1; 272 return (tp->tm_mon + 1); 273 } 274 if (isdigit((unsigned char)*p)) { 275 val = strtol(p, &p, 10); /* if 0, it's failure */ 276 for (; !isdigit((unsigned char)*p) 277 && !isalpha((unsigned char)*p) && *p != '*'; ++p); 278 *endp = p; 279 return (val); 280 } 281 for (start = p; isalpha((unsigned char)*++p);); 282 283 /* Sunday-1 */ 284 if (*p == '+' || *p == '-') 285 for(; isdigit((unsigned char)*++p);); 286 287 savech = *p; 288 *p = '\0'; 289 290 /* Month */ 291 if ((val = getmonth(start)) != 0) 292 *flags |= F_ISMONTH; 293 294 /* Day */ 295 else if ((val = getday(start)) != 0) { 296 *flags |= F_ISDAY; 297 298 /* variable weekday */ 299 if ((var = getdayvar(start)) != 0) { 300 if (var <=5 && var >= -4) 301 val += var * 10; 302#ifdef DEBUG 303 printf("var: %d\n", var); 304#endif 305 } 306 } 307 308 /* Easter */ 309 else if ((val = geteaster(start, tp->tm_year + 1900)) != 0) 310 *flags |= F_EASTER; 311 312 /* Paskha */ 313 else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0) 314 *flags |= F_EASTER; 315 316 /* undefined rest */ 317 else { 318 *p = savech; 319 return (0); 320 } 321 for (*p = savech; !isdigit((unsigned char)*p) 322 && !isalpha((unsigned char)*p) && *p != '*'; ++p); 323 *endp = p; 324 return (val); 325} 326 327char path[MAXPATHLEN]; 328 329FILE * 330opencal(void) 331{ 332 uid_t uid; 333 size_t i; 334 int fd, found, pdes[2]; 335 struct stat sbuf; 336 337 /* open up calendar file as stdin */ 338 if (!freopen(calendarFile, "r", stdin)) { 339 if (doall) { 340 if (chdir(calendarHomes[0]) != 0) 341 return (NULL); 342 if (stat(calendarNoMail, &sbuf) == 0) 343 return (NULL); 344 if (!freopen(calendarFile, "r", stdin)) 345 return (NULL); 346 } else { 347 char *home = getenv("HOME"); 348 if (home == NULL || *home == '\0') 349 errx(1, "cannot get home directory"); 350 chdir(home); 351 for (found = i = 0; i < sizeof(calendarHomes) / 352 sizeof(calendarHomes[0]); i++) 353 if (chdir(calendarHomes[i]) == 0 && 354 freopen(calendarFile, "r", stdin)) { 355 found = 1; 356 break; 357 } 358 if (!found) 359 errx(1, "can't open calendar file \"%s\": %s (%d)", 360 calendarFile, strerror(errno), errno); 361 } 362 } 363 if (pipe(pdes) < 0) 364 return (NULL); 365 switch (fork()) { 366 case -1: /* error */ 367 (void)close(pdes[0]); 368 (void)close(pdes[1]); 369 return (NULL); 370 case 0: 371 /* child -- stdin already setup, set stdout to pipe input */ 372 if (pdes[1] != STDOUT_FILENO) { 373 (void)dup2(pdes[1], STDOUT_FILENO); 374 (void)close(pdes[1]); 375 } 376 (void)close(pdes[0]); 377 uid = geteuid(); 378 if (setuid(getuid()) < 0) { 379 warnx("first setuid failed"); 380 _exit(1); 381 }; 382 if (setgid(getegid()) < 0) { 383 warnx("setgid failed"); 384 _exit(1); 385 } 386 if (setuid(uid) < 0) { 387 warnx("setuid failed"); 388 _exit(1); 389 } 390 execl(_PATH_CPP, "cpp", "-w", "-P", 391 "-traditional", "-nostdinc", /* GCC specific opts */ 392 "-I.", "-I" _PATH_INCLUDE, (char *)NULL); 393 warn(_PATH_CPP); 394 _exit(1); 395 } 396 /* parent -- set stdin to pipe output */ 397 (void)dup2(pdes[0], STDIN_FILENO); 398 (void)close(pdes[0]); 399 (void)close(pdes[1]); 400 401 /* not reading all calendar files, just set output to stdout */ 402 if (!doall) 403 return (stdout); 404 405 /* set output to a temporary file, so if no output don't send mail */ 406 (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP); 407 if ((fd = mkstemp(path)) < 0) 408 return (NULL); 409 return (fdopen(fd, "w+")); 410} 411 412void 413closecal(FILE *fp) 414{ 415 uid_t uid; 416 struct stat sbuf; 417 int nread, pdes[2], status; 418 char buf[1024]; 419 420 if (!doall) 421 return; 422 423 (void)rewind(fp); 424 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size) 425 goto done; 426 if (pipe(pdes) < 0) 427 goto done; 428 switch (fork()) { 429 case -1: /* error */ 430 (void)close(pdes[0]); 431 (void)close(pdes[1]); 432 goto done; 433 case 0: 434 /* child -- set stdin to pipe output */ 435 if (pdes[0] != STDIN_FILENO) { 436 (void)dup2(pdes[0], STDIN_FILENO); 437 (void)close(pdes[0]); 438 } 439 (void)close(pdes[1]); 440 uid = geteuid(); 441 if (setuid(getuid()) < 0) { 442 warnx("setuid failed"); 443 _exit(1); 444 }; 445 if (setgid(getegid()) < 0) { 446 warnx("setgid failed"); 447 _exit(1); 448 } 449 if (setuid(uid) < 0) { 450 warnx("setuid failed"); 451 _exit(1); 452 } 453 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F", 454 "\"Reminder Service\"", (char *)NULL); 455 warn(_PATH_SENDMAIL); 456 _exit(1); 457 } 458 /* parent -- write to pipe input */ 459 (void)close(pdes[0]); 460 461 header[1].iov_base = header[3].iov_base = pw->pw_name; 462 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name); 463 writev(pdes[1], header, 7); 464 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0) 465 (void)write(pdes[1], buf, nread); 466 (void)close(pdes[1]); 467done: (void)fclose(fp); 468 (void)unlink(path); 469 while (wait(&status) >= 0); 470} 471