1 /* $NetBSD: vacation.c,v 1.19 2001/01/10 23:15:42 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1987, 1993 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 38 #ifndef lint 39 __COPYRIGHT("@(#) Copyright (c) 1983, 1987, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"); 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static char sccsid[] = "@(#)vacation.c 8.2 (Berkeley) 1/26/94"; 46 #endif 47 __RCSID("$NetBSD: vacation.c,v 1.19 2001/01/10 23:15:42 lukem Exp $"); 48 #endif /* not lint */ 49 50 /* 51 ** Vacation 52 ** Copyright (c) 1983 Eric P. Allman 53 ** Berkeley, California 54 */ 55 56 #include <sys/param.h> 57 #include <sys/stat.h> 58 59 #include <ctype.h> 60 #include <db.h> 61 #include <errno.h> 62 #include <fcntl.h> 63 #include <paths.h> 64 #include <pwd.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 #include <syslog.h> 69 #include <time.h> 70 #include <tzfile.h> 71 #include <unistd.h> 72 73 /* 74 * VACATION -- return a message to the sender when on vacation. 75 * 76 * This program is invoked as a message receiver. It returns a 77 * message specified by the user to whomever sent the mail, taking 78 * care not to return a message too often to prevent "I am on 79 * vacation" loops. 80 */ 81 82 #define MAXLINE 1024 /* max line from mail header */ 83 #define VDB ".vacation.db" /* dbm's database */ 84 #define VMSG ".vacation.msg" /* vacation message */ 85 86 typedef struct alias { 87 struct alias *next; 88 const char *name; 89 } alias_t; 90 alias_t *names; 91 92 DB *db; 93 char from[MAXLINE]; 94 95 int main(int, char **); 96 int junkmail(void); 97 int nsearch(const char *, const char *); 98 void readheaders(void); 99 int recent(void); 100 void sendmessage(const char *); 101 void setinterval(time_t); 102 void setreply(void); 103 void usage(void); 104 105 int 106 main(int argc, char **argv) 107 { 108 struct passwd *pw; 109 alias_t *cur; 110 time_t interval; 111 int ch, iflag; 112 113 opterr = iflag = 0; 114 interval = -1; 115 openlog("vacation", 0, LOG_USER); 116 while ((ch = getopt(argc, argv, "a:Iir:")) != -1) 117 switch((char)ch) { 118 case 'a': /* alias */ 119 if (!(cur = (alias_t *)malloc((size_t)sizeof(alias_t)))) 120 break; 121 cur->name = optarg; 122 cur->next = names; 123 names = cur; 124 break; 125 case 'I': /* backward compatible */ 126 case 'i': /* init the database */ 127 iflag = 1; 128 break; 129 case 'r': 130 if (isdigit((unsigned char)*optarg)) { 131 interval = atol(optarg) * SECSPERDAY; 132 if (interval < 0) 133 usage(); 134 } 135 else 136 interval = (time_t)LONG_MAX; /* XXX */ 137 break; 138 case '?': 139 default: 140 usage(); 141 } 142 argc -= optind; 143 argv += optind; 144 145 if (argc != 1) { 146 if (!iflag) 147 usage(); 148 if (!(pw = getpwuid(getuid()))) { 149 syslog(LOG_ERR, 150 "vacation: no such user uid %u.", getuid()); 151 exit(1); 152 } 153 } 154 else if (!(pw = getpwnam(*argv))) { 155 syslog(LOG_ERR, "vacation: no such user %s.", *argv); 156 exit(1); 157 } 158 if (chdir(pw->pw_dir)) { 159 syslog(LOG_ERR, 160 "vacation: no such directory %s.", pw->pw_dir); 161 exit(1); 162 } 163 164 db = dbopen(VDB, O_CREAT|O_RDWR | (iflag ? O_TRUNC : 0), 165 S_IRUSR|S_IWUSR, DB_HASH, NULL); 166 if (!db) { 167 syslog(LOG_ERR, "vacation: %s: %m", VDB); 168 exit(1); 169 } 170 171 if (interval != -1) 172 setinterval(interval); 173 174 if (iflag) { 175 (void)(db->close)(db); 176 exit(0); 177 } 178 179 if (!(cur = malloc((size_t)sizeof(alias_t)))) 180 exit(1); 181 cur->name = pw->pw_name; 182 cur->next = names; 183 names = cur; 184 185 readheaders(); 186 if (!recent()) { 187 setreply(); 188 (void)(db->close)(db); 189 sendmessage(pw->pw_name); 190 } 191 else 192 (void)(db->close)(db); 193 exit(0); 194 /* NOTREACHED */ 195 } 196 197 /* 198 * readheaders -- 199 * read mail headers 200 */ 201 void 202 readheaders() 203 { 204 alias_t *cur; 205 char *p; 206 int tome, cont; 207 char buf[MAXLINE]; 208 209 cont = tome = 0; 210 while (fgets(buf, sizeof(buf), stdin) && *buf != '\n') 211 switch(*buf) { 212 case 'F': /* "From " */ 213 cont = 0; 214 if (!strncmp(buf, "From ", 5)) { 215 for (p = buf + 5; *p && *p != ' '; ++p); 216 *p = '\0'; 217 (void)strcpy(from, buf + 5); 218 if ((p = strchr(from, '\n'))) 219 *p = '\0'; 220 if (junkmail()) 221 exit(0); 222 } 223 break; 224 case 'P': /* "Precedence:" */ 225 cont = 0; 226 if (strncasecmp(buf, "Precedence", 10) || 227 (buf[10] != ':' && buf[10] != ' ' && 228 buf[10] != '\t')) 229 break; 230 if (!(p = strchr(buf, ':'))) 231 break; 232 while (*++p && isspace((unsigned char)*p)); 233 if (!*p) 234 break; 235 if (!strncasecmp(p, "junk", 4) || 236 !strncasecmp(p, "bulk", 4) || 237 !strncasecmp(p, "list", 4)) 238 exit(0); 239 break; 240 case 'C': /* "Cc:" */ 241 if (strncmp(buf, "Cc:", 3)) 242 break; 243 cont = 1; 244 goto findme; 245 case 'T': /* "To:" */ 246 if (strncmp(buf, "To:", 3)) 247 break; 248 cont = 1; 249 goto findme; 250 default: 251 if (!isspace((unsigned char)*buf) || !cont || tome) { 252 cont = 0; 253 break; 254 } 255 findme: for (cur = names; !tome && cur; cur = cur->next) 256 tome += nsearch(cur->name, buf); 257 } 258 if (!tome) 259 exit(0); 260 if (!*from) { 261 syslog(LOG_ERR, "vacation: no initial \"From\" line."); 262 exit(1); 263 } 264 } 265 266 /* 267 * nsearch -- 268 * do a nice, slow, search of a string for a substring. 269 */ 270 int 271 nsearch(const char *name, const char *str) 272 { 273 size_t len; 274 275 for (len = strlen(name); *str; ++str) 276 if (!strncasecmp(name, str, len)) 277 return(1); 278 return(0); 279 } 280 281 /* 282 * junkmail -- 283 * read the header and return if automagic/junk/bulk/list mail 284 */ 285 int 286 junkmail() 287 { 288 static struct ignore { 289 char *name; 290 int len; 291 } ignore[] = { 292 { "-request", 8 }, 293 { "postmaster", 10 }, 294 { "uucp", 4 }, 295 { "mailer-daemon", 13 }, 296 { "mailer", 6 }, 297 { "-relay", 6 }, 298 {NULL, 0 } 299 }; 300 struct ignore *cur; 301 int len; 302 char *p; 303 304 /* 305 * This is mildly amusing, and I'm not positive it's right; trying 306 * to find the "real" name of the sender, assuming that addresses 307 * will be some variant of: 308 * 309 * From site!site!SENDER%site.domain%site.domain@site.domain 310 */ 311 if (!(p = strchr(from, '%'))) 312 if (!(p = strchr(from, '@'))) { 313 if ((p = strrchr(from, '!'))) 314 ++p; 315 else 316 p = from; 317 for (; *p; ++p); 318 } 319 len = p - from; 320 for (cur = ignore; cur->name; ++cur) 321 if (len >= cur->len && 322 !strncasecmp(cur->name, p - cur->len, cur->len)) 323 return(1); 324 return(0); 325 } 326 327 #define VIT "__VACATION__INTERVAL__TIMER__" 328 329 /* 330 * recent -- 331 * find out if user has gotten a vacation message recently. 332 * use memmove for machines with alignment restrictions 333 */ 334 int 335 recent() 336 { 337 DBT key, data; 338 time_t then, next; 339 340 /* get interval time */ 341 key.data = VIT; 342 key.size = sizeof(VIT); 343 if ((db->get)(db, &key, &data, 0)) 344 next = SECSPERDAY * DAYSPERWEEK; 345 else 346 memmove(&next, data.data, sizeof(next)); 347 348 /* get record for this address */ 349 key.data = from; 350 key.size = strlen(from); 351 if (!(db->get)(db, &key, &data, 0)) { 352 memmove(&then, data.data, sizeof(then)); 353 if (next == (time_t)LONG_MAX || /* XXX */ 354 then + next > time(NULL)) 355 return(1); 356 } 357 return(0); 358 } 359 360 /* 361 * setinterval -- 362 * store the reply interval 363 */ 364 void 365 setinterval(time_t interval) 366 { 367 DBT key, data; 368 369 key.data = VIT; 370 key.size = sizeof(VIT); 371 data.data = &interval; 372 data.size = sizeof(interval); 373 (void)(db->put)(db, &key, &data, 0); 374 } 375 376 /* 377 * setreply -- 378 * store that this user knows about the vacation. 379 */ 380 void 381 setreply() 382 { 383 DBT key, data; 384 time_t now; 385 386 key.data = from; 387 key.size = strlen(from); 388 (void)time(&now); 389 data.data = &now; 390 data.size = sizeof(now); 391 (void)(db->put)(db, &key, &data, 0); 392 } 393 394 /* 395 * sendmessage -- 396 * exec sendmail to send the vacation file to sender 397 */ 398 void 399 sendmessage(const char *myname) 400 { 401 FILE *mfp, *sfp; 402 int i; 403 int pvect[2]; 404 char buf[MAXLINE]; 405 406 mfp = fopen(VMSG, "r"); 407 if (mfp == NULL) { 408 syslog(LOG_ERR, "vacation: no ~%s/%s file.", myname, VMSG); 409 exit(1); 410 } 411 if (pipe(pvect) < 0) { 412 syslog(LOG_ERR, "vacation: pipe: %m"); 413 exit(1); 414 } 415 i = vfork(); 416 if (i < 0) { 417 syslog(LOG_ERR, "vacation: fork: %m"); 418 exit(1); 419 } 420 if (i == 0) { 421 dup2(pvect[0], 0); 422 close(pvect[0]); 423 close(pvect[1]); 424 close(fileno(mfp)); 425 execl(_PATH_SENDMAIL, "sendmail", "-f", myname, "--", from, 426 NULL); 427 syslog(LOG_ERR, "vacation: can't exec %s: %m", 428 _PATH_SENDMAIL); 429 _exit(1); 430 } 431 close(pvect[0]); 432 sfp = fdopen(pvect[1], "w"); 433 fprintf(sfp, "To: %s\n", from); 434 while (fgets(buf, sizeof buf, mfp)) 435 fputs(buf, sfp); 436 fclose(mfp); 437 fclose(sfp); 438 } 439 440 void 441 usage() 442 { 443 444 syslog(LOG_ERR, "uid %u: usage: vacation [-i] [-a alias] login", 445 getuid()); 446 exit(1); 447 } 448