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