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