Annotation of src/usr.bin/vacation/vacation.c, Revision 1.12
1.12 ! marc 1: /* $OpenBSD: vacation.c,v 1.11 1999/02/12 01:21:07 marc Exp $ */
1.1 deraadt 2: /* $NetBSD: vacation.c,v 1.7 1995/04/29 05:58:27 cgd Exp $ */
3:
4: /*
5: * Copyright (c) 1983, 1987, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) 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
1.12 ! marc 47: static char rcsid[] = "$OpenBSD: vacation.c,v 1.11 1999/02/12 01:21:07 marc Exp $";
1.1 deraadt 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: #include <fcntl.h>
59: #include <pwd.h>
60: #include <db.h>
61: #include <time.h>
62: #include <syslog.h>
63: #include <tzfile.h>
64: #include <errno.h>
65: #include <unistd.h>
66: #include <stdio.h>
67: #include <ctype.h>
68: #include <stdlib.h>
69: #include <string.h>
70: #include <paths.h>
71:
72: /*
73: * VACATION -- return a message to the sender when on vacation.
74: *
75: * This program is invoked as a message receiver. It returns a
76: * message specified by the user to whomever sent the mail, taking
77: * care not to return a message too often to prevent "I am on
78: * vacation" loops.
79: */
80:
81: #define MAXLINE 1024 /* max line from mail header */
82: #define VDB ".vacation.db" /* dbm's database */
83: #define VMSG ".vacation.msg" /* vacation message */
84:
85: typedef struct alias {
86: struct alias *next;
87: char *name;
88: } ALIAS;
89: ALIAS *names;
90:
91: DB *db;
92: char from[MAXLINE];
1.12 ! marc 93: char subj[MAXLINE];
1.1 deraadt 94:
95: int junkmail __P((void));
96: int nsearch __P((char *, char *));
97: void readheaders __P((void));
98: int recent __P((void));
99: void sendmessage __P((char *));
100: void setinterval __P((time_t));
101: void setreply __P((void));
102: void usage __P((void));
103:
104: int
105: main(argc, argv)
106: int argc;
107: char **argv;
108: {
109: struct passwd *pw;
1.9 millert 110: struct stat sb;
1.1 deraadt 111: ALIAS *cur;
112: time_t interval;
1.9 millert 113: int ch, iflag, flags;
1.1 deraadt 114:
115: opterr = iflag = 0;
116: interval = -1;
1.4 millert 117: while ((ch = getopt(argc, argv, "a:Iir:")) != -1)
1.1 deraadt 118: switch((char)ch) {
119: case 'a': /* alias */
120: if (!(cur = (ALIAS *)malloc((u_int)sizeof(ALIAS))))
121: break;
122: cur->name = optarg;
123: cur->next = names;
124: names = cur;
125: break;
126: case 'I': /* backward compatible */
127: case 'i': /* init the database */
128: iflag = 1;
129: break;
130: case 'r':
131: if (isdigit(*optarg)) {
132: interval = atol(optarg) * SECSPERDAY;
133: if (interval < 0)
134: usage();
135: }
136: else
137: interval = (time_t)LONG_MAX; /* XXX */
138: break;
139: case '?':
140: default:
141: usage();
142: }
143: argc -= optind;
144: argv += optind;
145:
146: if (argc != 1) {
147: if (!iflag)
148: usage();
149: if (!(pw = getpwuid(getuid()))) {
150: syslog(LOG_ERR,
1.11 marc 151: "no such user uid %u.", getuid());
1.1 deraadt 152: exit(1);
153: }
154: }
155: else if (!(pw = getpwnam(*argv))) {
1.11 marc 156: syslog(LOG_ERR, "no such user %s.", *argv);
1.1 deraadt 157: exit(1);
158: }
159: if (chdir(pw->pw_dir)) {
160: syslog(LOG_NOTICE,
1.11 marc 161: "no such directory %s.", pw->pw_dir);
1.1 deraadt 162: exit(1);
163: }
164:
1.9 millert 165: /*
166: * dbopen(3) can not deal with a zero-length file w/o O_TRUNC.
167: */
168: if (iflag == 1 || (stat(VDB, &sb) == 0 && sb.st_size == (off_t)0))
169: flags = O_CREAT|O_RDWR|O_TRUNC;
170: else
171: flags = O_CREAT|O_RDWR;
172:
173: db = dbopen(VDB, flags, S_IRUSR|S_IWUSR, DB_HASH, NULL);
1.1 deraadt 174: if (!db) {
1.11 marc 175: syslog(LOG_NOTICE, "%s: %m", VDB);
1.1 deraadt 176: exit(1);
177: }
178:
179: if (interval != -1)
180: setinterval(interval);
181:
182: if (iflag) {
183: (void)(db->close)(db);
184: exit(0);
185: }
186:
187: if (!(cur = malloc((u_int)sizeof(ALIAS))))
188: exit(1);
189: cur->name = pw->pw_name;
190: cur->next = names;
191: names = cur;
192:
193: readheaders();
194: if (!recent()) {
195: setreply();
196: (void)(db->close)(db);
197: sendmessage(pw->pw_name);
198: }
199: else
200: (void)(db->close)(db);
201: exit(0);
202: /* NOTREACHED */
203: }
204:
205: /*
206: * readheaders --
207: * read mail headers
208: */
209: void
210: readheaders()
211: {
212: register ALIAS *cur;
213: register char *p;
214: int tome, cont;
215: char buf[MAXLINE];
216:
217: cont = tome = 0;
218: while (fgets(buf, sizeof(buf), stdin) && *buf != '\n')
219: switch(*buf) {
220: case 'F': /* "From " */
221: cont = 0;
222: if (!strncmp(buf, "From ", 5)) {
1.12 ! marc 223: for (p = buf + 5; *p && *p != ' '; ++p)
! 224: ;
1.1 deraadt 225: *p = '\0';
226: (void)strcpy(from, buf + 5);
1.5 millert 227: if (p = strchr(from, '\n'))
1.1 deraadt 228: *p = '\0';
229: if (junkmail())
230: exit(0);
231: }
232: break;
1.11 marc 233: case 'R': /* "Return-Path:" */
234: cont = 0;
235: if (strncasecmp(buf, "Return-Path:",
236: sizeof("Return-Path:")-1) ||
237: buf[12] != ' ' && buf[12] != '\t')
238: break;
1.12 ! marc 239: for (p = buf + 12; *p && isspace(*p); ++p)
! 240: ;
1.11 marc 241: if (strlcpy(from, p, sizeof from ) > sizeof from) {
242: syslog(LOG_NOTICE,
243: "Return-Path %s exceeds limits", p);
244: exit(1);
245: }
246: if (p = strchr(from, '\n'))
247: *p = '\0';
248: if (junkmail())
249: exit(0);
250: break;
1.1 deraadt 251: case 'P': /* "Precedence:" */
252: cont = 0;
253: if (strncasecmp(buf, "Precedence", 10) ||
254: buf[10] != ':' && buf[10] != ' ' && buf[10] != '\t')
255: break;
1.5 millert 256: if (!(p = strchr(buf, ':')))
1.1 deraadt 257: break;
258: while (*++p && isspace(*p));
259: if (!*p)
260: break;
261: if (!strncasecmp(p, "junk", 4) ||
262: !strncasecmp(p, "bulk", 4) ||
263: !strncasecmp(p, "list", 4))
264: exit(0);
265: break;
1.12 ! marc 266: case 'S': /* Subject: */
! 267: cont = 0;
! 268: if (strncasecmp(buf, "Subject:",
! 269: sizeof("Subject:")-1) ||
! 270: buf[8] != ' ' && buf[8] != '\t')
! 271: break;
! 272: for (p = buf + 8; *p && isspace(*p); ++p)
! 273: ;
! 274: if (strlcpy(subj, p, sizeof subj ) > sizeof subj) {
! 275: syslog(LOG_NOTICE,
! 276: "Subject %s exceeds limits", p);
! 277: exit(1);
! 278: }
! 279: if (p = strchr(subj, '\n'))
! 280: *p = '\0';
! 281: break;
1.1 deraadt 282: case 'C': /* "Cc:" */
283: if (strncmp(buf, "Cc:", 3))
284: break;
285: cont = 1;
286: goto findme;
287: case 'T': /* "To:" */
288: if (strncmp(buf, "To:", 3))
289: break;
290: cont = 1;
291: goto findme;
292: default:
293: if (!isspace(*buf) || !cont || tome) {
294: cont = 0;
295: break;
296: }
297: findme: for (cur = names; !tome && cur; cur = cur->next)
298: tome += nsearch(cur->name, buf);
299: }
300: if (!tome)
301: exit(0);
302: if (!*from) {
1.11 marc 303: syslog(LOG_NOTICE, "no initial \"From\" or \"Return-Path\"line.");
1.1 deraadt 304: exit(1);
305: }
306: }
307:
308: /*
309: * nsearch --
310: * do a nice, slow, search of a string for a substring.
311: */
312: int
313: nsearch(name, str)
314: register char *name, *str;
315: {
316: register int len;
317:
318: for (len = strlen(name); *str; ++str)
319: if (*str == *name && !strncasecmp(name, str, len))
320: return(1);
321: return(0);
322: }
323:
324: /*
325: * junkmail --
326: * read the header and return if automagic/junk/bulk/list mail
327: */
328: int
329: junkmail()
330: {
331: static struct ignore {
332: char *name;
333: int len;
334: } ignore[] = {
335: "-request", 8, "postmaster", 10, "uucp", 4,
336: "mailer-daemon", 13, "mailer", 6, "-relay", 6,
337: NULL, NULL,
338: };
339: register struct ignore *cur;
340: register int len;
341: register char *p;
342:
343: /*
344: * This is mildly amusing, and I'm not positive it's right; trying
345: * to find the "real" name of the sender, assuming that addresses
346: * will be some variant of:
347: *
348: * From site!site!SENDER%site.domain%site.domain@site.domain
349: */
1.5 millert 350: if (!(p = strchr(from, '%')))
351: if (!(p = strchr(from, '@'))) {
352: if (p = strrchr(from, '!'))
1.1 deraadt 353: ++p;
354: else
355: p = from;
1.12 ! marc 356: for (; *p; ++p)
! 357: ;
1.1 deraadt 358: }
359: len = p - from;
360: for (cur = ignore; cur->name; ++cur)
361: if (len >= cur->len &&
362: !strncasecmp(cur->name, p - cur->len, cur->len))
363: return(1);
364: return(0);
365: }
366:
367: #define VIT "__VACATION__INTERVAL__TIMER__"
368:
369: /*
370: * recent --
371: * find out if user has gotten a vacation message recently.
372: * use bcopy for machines with alignment restrictions
373: */
374: int
375: recent()
376: {
377: DBT key, data;
378: time_t then, next;
379:
380: /* get interval time */
381: key.data = VIT;
382: key.size = sizeof(VIT);
383: if ((db->get)(db, &key, &data, 0))
384: next = SECSPERDAY * DAYSPERWEEK;
385: else
386: bcopy(data.data, &next, sizeof(next));
387:
388: /* get record for this address */
389: key.data = from;
390: key.size = strlen(from);
391: if (!(db->get)(db, &key, &data, 0)) {
392: bcopy(data.data, &then, sizeof(then));
393: if (next == (time_t)LONG_MAX || /* XXX */
394: then + next > time(NULL))
395: return(1);
396: }
397: return(0);
398: }
399:
400: /*
401: * setinterval --
402: * store the reply interval
403: */
404: void
405: setinterval(interval)
406: time_t interval;
407: {
408: DBT key, data;
409:
410: key.data = VIT;
411: key.size = sizeof(VIT);
412: data.data = &interval;
413: data.size = sizeof(interval);
414: (void)(db->put)(db, &key, &data, 0);
415: }
416:
417: /*
418: * setreply --
419: * store that this user knows about the vacation.
420: */
421: void
422: setreply()
423: {
424: DBT key, data;
425: time_t now;
426:
427: key.data = from;
428: key.size = strlen(from);
429: (void)time(&now);
430: data.data = &now;
431: data.size = sizeof(now);
432: (void)(db->put)(db, &key, &data, 0);
433: }
434:
435: /*
436: * sendmessage --
437: * exec sendmail to send the vacation file to sender
438: */
439: void
440: sendmessage(myname)
441: char *myname;
442: {
443: FILE *mfp, *sfp;
444: int i;
445: int pvect[2];
446: char buf[MAXLINE];
447:
448: mfp = fopen(VMSG, "r");
449: if (mfp == NULL) {
1.11 marc 450: syslog(LOG_NOTICE, "no ~%s/%s file.", myname, VMSG);
1.1 deraadt 451: exit(1);
452: }
453: if (pipe(pvect) < 0) {
1.11 marc 454: syslog(LOG_ERR, "pipe: %m");
1.1 deraadt 455: exit(1);
456: }
457: i = vfork();
458: if (i < 0) {
1.11 marc 459: syslog(LOG_ERR, "fork: %m");
1.1 deraadt 460: exit(1);
461: }
462: if (i == 0) {
463: dup2(pvect[0], 0);
464: close(pvect[0]);
465: close(pvect[1]);
1.10 deraadt 466: close(fileno(mfp));
1.8 deraadt 467: execl(_PATH_SENDMAIL, "sendmail", "-f", myname, "--",
468: from, NULL);
1.11 marc 469: syslog(LOG_ERR, "can't exec %s: %m", _PATH_SENDMAIL);
1.3 deraadt 470: _exit(1);
1.1 deraadt 471: }
472: close(pvect[0]);
473: sfp = fdopen(pvect[1], "w");
474: fprintf(sfp, "To: %s\n", from);
1.12 ! marc 475: while (fgets(buf, sizeof buf, mfp)) {
! 476: char *s = strstr(buf, "$SUBJECT");
! 477: if ( s ) {
! 478: *s = 0;
! 479: fputs(buf, sfp);
! 480: fputs(subj, sfp);
! 481: fputs(s+8, sfp);
! 482: } else {
! 483: fputs(buf, sfp);
! 484: }
! 485: }
1.1 deraadt 486: fclose(mfp);
487: fclose(sfp);
488: }
489:
490: void
491: usage()
492: {
1.9 millert 493: syslog(LOG_NOTICE, "uid %u: usage: vacation [-i] [-a alias] login",
1.1 deraadt 494: getuid());
495: exit(1);
496: }