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