vacation.c revision 1.14 1 /* $NetBSD: vacation.c,v 1.14 1998/07/08 00:34:53 cgd 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.14 1998/07/08 00:34:53 cgd 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 char *name;
89 } ALIAS;
90 ALIAS *names;
91
92 DB *db;
93 char from[MAXLINE];
94
95 int main __P((int, char **));
96 int junkmail __P((void));
97 int nsearch __P((const char *, const char *));
98 void readheaders __P((void));
99 int recent __P((void));
100 void sendmessage __P((char *));
101 void setinterval __P((time_t));
102 void setreply __P((void));
103 void usage __P((void));
104
105 int
106 main(argc, argv)
107 int argc;
108 char **argv;
109 {
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:")) != -1)
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.", getuid());
152 exit(1);
153 }
154 }
155 else if (!(pw = getpwnam(*argv))) {
156 syslog(LOG_ERR, "vacation: no such user %s.", *argv);
157 exit(1);
158 }
159 if (chdir(pw->pw_dir)) {
160 syslog(LOG_NOTICE,
161 "vacation: no such directory %s.", 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: %m", VDB);
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 ALIAS *cur;
206 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 = strchr(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] != ' ' &&
229 buf[10] != '\t'))
230 break;
231 if (!(p = strchr(buf, ':')))
232 break;
233 while (*++p && isspace(*p));
234 if (!*p)
235 break;
236 if (!strncasecmp(p, "junk", 4) ||
237 !strncasecmp(p, "bulk", 4) ||
238 !strncasecmp(p, "list", 4))
239 exit(0);
240 break;
241 case 'C': /* "Cc:" */
242 if (strncmp(buf, "Cc:", 3))
243 break;
244 cont = 1;
245 goto findme;
246 case 'T': /* "To:" */
247 if (strncmp(buf, "To:", 3))
248 break;
249 cont = 1;
250 goto findme;
251 default:
252 if (!isspace(*buf) || !cont || tome) {
253 cont = 0;
254 break;
255 }
256 findme: for (cur = names; !tome && cur; cur = cur->next)
257 tome += nsearch(cur->name, buf);
258 }
259 if (!tome)
260 exit(0);
261 if (!*from) {
262 syslog(LOG_NOTICE, "vacation: no initial \"From\" line.");
263 exit(1);
264 }
265 }
266
267 /*
268 * nsearch --
269 * do a nice, slow, search of a string for a substring.
270 */
271 int
272 nsearch(name, str)
273 const char *name, *str;
274 {
275 size_t len;
276
277 for (len = strlen(name); *str; ++str)
278 if (*str == *name && !strncasecmp(name, str, len))
279 return(1);
280 return(0);
281 }
282
283 /*
284 * junkmail --
285 * read the header and return if automagic/junk/bulk/list mail
286 */
287 int
288 junkmail()
289 {
290 static struct ignore {
291 char *name;
292 int len;
293 } ignore[] = {
294 { "-request", 8 },
295 { "postmaster", 10 },
296 { "uucp", 4 },
297 { "mailer-daemon", 13 },
298 { "mailer", 6 },
299 { "-relay", 6 },
300 {NULL, 0 }
301 };
302 struct ignore *cur;
303 int len;
304 char *p;
305
306 /*
307 * This is mildly amusing, and I'm not positive it's right; trying
308 * to find the "real" name of the sender, assuming that addresses
309 * will be some variant of:
310 *
311 * From site!site!SENDER%site.domain%site.domain (at) site.domain
312 */
313 if (!(p = strchr(from, '%')))
314 if (!(p = strchr(from, '@'))) {
315 if ((p = strrchr(from, '!')))
316 ++p;
317 else
318 p = from;
319 for (; *p; ++p);
320 }
321 len = p - from;
322 for (cur = ignore; cur->name; ++cur)
323 if (len >= cur->len &&
324 !strncasecmp(cur->name, p - cur->len, cur->len))
325 return(1);
326 return(0);
327 }
328
329 #define VIT "__VACATION__INTERVAL__TIMER__"
330
331 /*
332 * recent --
333 * find out if user has gotten a vacation message recently.
334 * use memmove for machines with alignment restrictions
335 */
336 int
337 recent()
338 {
339 DBT key, data;
340 time_t then, next;
341
342 /* get interval time */
343 key.data = VIT;
344 key.size = sizeof(VIT);
345 if ((db->get)(db, &key, &data, 0))
346 next = SECSPERDAY * DAYSPERWEEK;
347 else
348 memmove(&next, data.data, sizeof(next));
349
350 /* get record for this address */
351 key.data = from;
352 key.size = strlen(from);
353 if (!(db->get)(db, &key, &data, 0)) {
354 memmove(&then, data.data, sizeof(then));
355 if (next == (time_t)LONG_MAX || /* XXX */
356 then + next > time(NULL))
357 return(1);
358 }
359 return(0);
360 }
361
362 /*
363 * setinterval --
364 * store the reply interval
365 */
366 void
367 setinterval(interval)
368 time_t interval;
369 {
370 DBT key, data;
371
372 key.data = VIT;
373 key.size = sizeof(VIT);
374 data.data = &interval;
375 data.size = sizeof(interval);
376 (void)(db->put)(db, &key, &data, 0);
377 }
378
379 /*
380 * setreply --
381 * store that this user knows about the vacation.
382 */
383 void
384 setreply()
385 {
386 DBT key, data;
387 time_t now;
388
389 key.data = from;
390 key.size = strlen(from);
391 (void)time(&now);
392 data.data = &now;
393 data.size = sizeof(now);
394 (void)(db->put)(db, &key, &data, 0);
395 }
396
397 /*
398 * sendmessage --
399 * exec sendmail to send the vacation file to sender
400 */
401 void
402 sendmessage(myname)
403 char *myname;
404 {
405 FILE *mfp, *sfp;
406 int i;
407 int pvect[2];
408 char buf[MAXLINE];
409
410 mfp = fopen(VMSG, "r");
411 if (mfp == NULL) {
412 syslog(LOG_NOTICE, "vacation: no ~%s/%s file.", myname, VMSG);
413 exit(1);
414 }
415 if (pipe(pvect) < 0) {
416 syslog(LOG_ERR, "vacation: pipe: %m");
417 exit(1);
418 }
419 i = vfork();
420 if (i < 0) {
421 syslog(LOG_ERR, "vacation: fork: %m");
422 exit(1);
423 }
424 if (i == 0) {
425 dup2(pvect[0], 0);
426 close(pvect[0]);
427 close(pvect[1]);
428 close(fileno(mfp));
429 execl(_PATH_SENDMAIL, "sendmail", "-f", myname, "--", from,
430 NULL);
431 syslog(LOG_ERR, "vacation: can't exec %s: %m",
432 _PATH_SENDMAIL);
433 _exit(1);
434 }
435 close(pvect[0]);
436 sfp = fdopen(pvect[1], "w");
437 fprintf(sfp, "To: %s\n", from);
438 while (fgets(buf, sizeof buf, mfp))
439 fputs(buf, sfp);
440 fclose(mfp);
441 fclose(sfp);
442 }
443
444 void
445 usage()
446 {
447
448 syslog(LOG_NOTICE, "uid %u: usage: vacation [-i] [-a alias] login",
449 getuid());
450 exit(1);
451 }
452