Annotation of src/usr.bin/vacation/vacation.c, Revision 1.2
1.2 ! deraadt 1: /* $OpenBSD: vacation.c,v 1.7 1995/04/29 05:58:27 cgd 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.2 ! deraadt 47: static char rcsid[] = "$OpenBSD: vacation.c,v 1.7 1995/04/29 05:58:27 cgd 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];
93:
94: int junkmail __P((void));
95: int nsearch __P((char *, char *));
96: void readheaders __P((void));
97: int recent __P((void));
98: void sendmessage __P((char *));
99: void setinterval __P((time_t));
100: void setreply __P((void));
101: void usage __P((void));
102:
103: int
104: main(argc, argv)
105: int argc;
106: char **argv;
107: {
108: extern int optind, opterr;
109: extern char *optarg;
110: struct passwd *pw;
111: ALIAS *cur;
112: time_t interval;
113: int ch, iflag;
114:
115: opterr = iflag = 0;
116: interval = -1;
117: while ((ch = getopt(argc, argv, "a:Iir:")) != EOF)
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,
151: "vacation: no such user uid %u.\n", getuid());
152: exit(1);
153: }
154: }
155: else if (!(pw = getpwnam(*argv))) {
156: syslog(LOG_ERR, "vacation: no such user %s.\n", *argv);
157: exit(1);
158: }
159: if (chdir(pw->pw_dir)) {
160: syslog(LOG_NOTICE,
161: "vacation: no such directory %s.\n", pw->pw_dir);
162: exit(1);
163: }
164:
165: db = dbopen(VDB, O_CREAT|O_RDWR | (iflag ? O_TRUNC : 0),
166: S_IRUSR|S_IWUSR, DB_HASH, NULL);
167: if (!db) {
168: syslog(LOG_NOTICE, "vacation: %s: %s\n", VDB, strerror(errno));
169: exit(1);
170: }
171:
172: if (interval != -1)
173: setinterval(interval);
174:
175: if (iflag) {
176: (void)(db->close)(db);
177: exit(0);
178: }
179:
180: if (!(cur = malloc((u_int)sizeof(ALIAS))))
181: exit(1);
182: cur->name = pw->pw_name;
183: cur->next = names;
184: names = cur;
185:
186: readheaders();
187: if (!recent()) {
188: setreply();
189: (void)(db->close)(db);
190: sendmessage(pw->pw_name);
191: }
192: else
193: (void)(db->close)(db);
194: exit(0);
195: /* NOTREACHED */
196: }
197:
198: /*
199: * readheaders --
200: * read mail headers
201: */
202: void
203: readheaders()
204: {
205: register ALIAS *cur;
206: register char *p;
207: int tome, cont;
208: char buf[MAXLINE];
209:
210: cont = tome = 0;
211: while (fgets(buf, sizeof(buf), stdin) && *buf != '\n')
212: switch(*buf) {
213: case 'F': /* "From " */
214: cont = 0;
215: if (!strncmp(buf, "From ", 5)) {
216: for (p = buf + 5; *p && *p != ' '; ++p);
217: *p = '\0';
218: (void)strcpy(from, buf + 5);
219: if (p = index(from, '\n'))
220: *p = '\0';
221: if (junkmail())
222: exit(0);
223: }
224: break;
225: case 'P': /* "Precedence:" */
226: cont = 0;
227: if (strncasecmp(buf, "Precedence", 10) ||
228: buf[10] != ':' && buf[10] != ' ' && buf[10] != '\t')
229: break;
230: if (!(p = index(buf, ':')))
231: break;
232: while (*++p && isspace(*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(*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_NOTICE, "vacation: no initial \"From\" line.\n");
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(name, str)
272: register char *name, *str;
273: {
274: register int len;
275:
276: for (len = strlen(name); *str; ++str)
277: if (*str == *name && !strncasecmp(name, str, len))
278: return(1);
279: return(0);
280: }
281:
282: /*
283: * junkmail --
284: * read the header and return if automagic/junk/bulk/list mail
285: */
286: int
287: junkmail()
288: {
289: static struct ignore {
290: char *name;
291: int len;
292: } ignore[] = {
293: "-request", 8, "postmaster", 10, "uucp", 4,
294: "mailer-daemon", 13, "mailer", 6, "-relay", 6,
295: NULL, NULL,
296: };
297: register struct ignore *cur;
298: register int len;
299: register char *p;
300:
301: /*
302: * This is mildly amusing, and I'm not positive it's right; trying
303: * to find the "real" name of the sender, assuming that addresses
304: * will be some variant of:
305: *
306: * From site!site!SENDER%site.domain%site.domain@site.domain
307: */
308: if (!(p = index(from, '%')))
309: if (!(p = index(from, '@'))) {
310: if (p = rindex(from, '!'))
311: ++p;
312: else
313: p = from;
314: for (; *p; ++p);
315: }
316: len = p - from;
317: for (cur = ignore; cur->name; ++cur)
318: if (len >= cur->len &&
319: !strncasecmp(cur->name, p - cur->len, cur->len))
320: return(1);
321: return(0);
322: }
323:
324: #define VIT "__VACATION__INTERVAL__TIMER__"
325:
326: /*
327: * recent --
328: * find out if user has gotten a vacation message recently.
329: * use bcopy for machines with alignment restrictions
330: */
331: int
332: recent()
333: {
334: DBT key, data;
335: time_t then, next;
336:
337: /* get interval time */
338: key.data = VIT;
339: key.size = sizeof(VIT);
340: if ((db->get)(db, &key, &data, 0))
341: next = SECSPERDAY * DAYSPERWEEK;
342: else
343: bcopy(data.data, &next, sizeof(next));
344:
345: /* get record for this address */
346: key.data = from;
347: key.size = strlen(from);
348: if (!(db->get)(db, &key, &data, 0)) {
349: bcopy(data.data, &then, sizeof(then));
350: if (next == (time_t)LONG_MAX || /* XXX */
351: then + next > time(NULL))
352: return(1);
353: }
354: return(0);
355: }
356:
357: /*
358: * setinterval --
359: * store the reply interval
360: */
361: void
362: setinterval(interval)
363: time_t interval;
364: {
365: DBT key, data;
366:
367: key.data = VIT;
368: key.size = sizeof(VIT);
369: data.data = &interval;
370: data.size = sizeof(interval);
371: (void)(db->put)(db, &key, &data, 0);
372: }
373:
374: /*
375: * setreply --
376: * store that this user knows about the vacation.
377: */
378: void
379: setreply()
380: {
381: DBT key, data;
382: time_t now;
383:
384: key.data = from;
385: key.size = strlen(from);
386: (void)time(&now);
387: data.data = &now;
388: data.size = sizeof(now);
389: (void)(db->put)(db, &key, &data, 0);
390: }
391:
392: /*
393: * sendmessage --
394: * exec sendmail to send the vacation file to sender
395: */
396: void
397: sendmessage(myname)
398: char *myname;
399: {
400: FILE *mfp, *sfp;
401: int i;
402: int pvect[2];
403: char buf[MAXLINE];
404:
405: mfp = fopen(VMSG, "r");
406: if (mfp == NULL) {
407: syslog(LOG_NOTICE, "vacation: no ~%s/%s file.\n", myname, VMSG);
408: exit(1);
409: }
410: if (pipe(pvect) < 0) {
411: syslog(LOG_ERR, "vacation: pipe: %s", strerror(errno));
412: exit(1);
413: }
414: i = vfork();
415: if (i < 0) {
416: syslog(LOG_ERR, "vacation: fork: %s", strerror(errno));
417: exit(1);
418: }
419: if (i == 0) {
420: dup2(pvect[0], 0);
421: close(pvect[0]);
422: close(pvect[1]);
423: fclose(mfp);
424: execl(_PATH_SENDMAIL, "sendmail", "-f", myname, from, NULL);
425: syslog(LOG_ERR, "vacation: can't exec %s: %s",
426: _PATH_SENDMAIL, strerror(errno));
427: exit(1);
428: }
429: close(pvect[0]);
430: sfp = fdopen(pvect[1], "w");
431: fprintf(sfp, "To: %s\n", from);
432: while (fgets(buf, sizeof buf, mfp))
433: fputs(buf, sfp);
434: fclose(mfp);
435: fclose(sfp);
436: }
437:
438: void
439: usage()
440: {
441: syslog(LOG_NOTICE, "uid %u: usage: vacation [-i] [-a alias] login\n",
442: getuid());
443: exit(1);
444: }