vacation.c revision 1.21 1 1.21 christos /* $NetBSD: vacation.c,v 1.21 2003/04/20 01:58:00 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.21 christos __RCSID("$NetBSD: vacation.c,v 1.21 2003/04/20 01:58:00 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.10 mikel #include <errno.h>
62 1.1 cgd #include <fcntl.h>
63 1.10 mikel #include <paths.h>
64 1.1 cgd #include <pwd.h>
65 1.10 mikel #include <stdio.h>
66 1.10 mikel #include <stdlib.h>
67 1.10 mikel #include <string.h>
68 1.10 mikel #include <syslog.h>
69 1.1 cgd #include <time.h>
70 1.1 cgd #include <tzfile.h>
71 1.1 cgd #include <unistd.h>
72 1.1 cgd
73 1.1 cgd /*
74 1.1 cgd * VACATION -- return a message to the sender when on vacation.
75 1.1 cgd *
76 1.1 cgd * This program is invoked as a message receiver. It returns a
77 1.1 cgd * message specified by the user to whomever sent the mail, taking
78 1.1 cgd * care not to return a message too often to prevent "I am on
79 1.1 cgd * vacation" loops.
80 1.1 cgd */
81 1.1 cgd
82 1.1 cgd #define MAXLINE 1024 /* max line from mail header */
83 1.1 cgd #define VDB ".vacation.db" /* dbm's database */
84 1.1 cgd #define VMSG ".vacation.msg" /* vacation message */
85 1.1 cgd
86 1.1 cgd typedef struct alias {
87 1.1 cgd struct alias *next;
88 1.15 mycroft const char *name;
89 1.17 mjl } alias_t;
90 1.17 mjl alias_t *names;
91 1.1 cgd
92 1.1 cgd DB *db;
93 1.6 jtc char from[MAXLINE];
94 1.1 cgd
95 1.17 mjl int main(int, char **);
96 1.17 mjl int junkmail(void);
97 1.17 mjl int nsearch(const char *, const char *);
98 1.17 mjl void readheaders(void);
99 1.17 mjl int recent(void);
100 1.17 mjl void sendmessage(const char *);
101 1.17 mjl void setinterval(time_t);
102 1.17 mjl void setreply(void);
103 1.17 mjl void usage(void);
104 1.1 cgd
105 1.6 jtc int
106 1.17 mjl main(int argc, char **argv)
107 1.1 cgd {
108 1.1 cgd struct passwd *pw;
109 1.17 mjl alias_t *cur;
110 1.1 cgd time_t interval;
111 1.1 cgd int ch, iflag;
112 1.1 cgd
113 1.1 cgd opterr = iflag = 0;
114 1.1 cgd interval = -1;
115 1.19 lukem openlog("vacation", 0, LOG_USER);
116 1.10 mikel while ((ch = getopt(argc, argv, "a:Iir:")) != -1)
117 1.1 cgd switch((char)ch) {
118 1.1 cgd case 'a': /* alias */
119 1.17 mjl if (!(cur = (alias_t *)malloc((size_t)sizeof(alias_t))))
120 1.1 cgd break;
121 1.1 cgd cur->name = optarg;
122 1.1 cgd cur->next = names;
123 1.1 cgd names = cur;
124 1.1 cgd break;
125 1.1 cgd case 'I': /* backward compatible */
126 1.1 cgd case 'i': /* init the database */
127 1.1 cgd iflag = 1;
128 1.1 cgd break;
129 1.1 cgd case 'r':
130 1.16 christos if (isdigit((unsigned char)*optarg)) {
131 1.1 cgd interval = atol(optarg) * SECSPERDAY;
132 1.1 cgd if (interval < 0)
133 1.1 cgd usage();
134 1.1 cgd }
135 1.1 cgd else
136 1.7 cgd interval = (time_t)LONG_MAX; /* XXX */
137 1.1 cgd break;
138 1.1 cgd case '?':
139 1.1 cgd default:
140 1.1 cgd usage();
141 1.1 cgd }
142 1.1 cgd argc -= optind;
143 1.1 cgd argv += optind;
144 1.1 cgd
145 1.1 cgd if (argc != 1) {
146 1.1 cgd if (!iflag)
147 1.1 cgd usage();
148 1.1 cgd if (!(pw = getpwuid(getuid()))) {
149 1.1 cgd syslog(LOG_ERR,
150 1.13 mikel "vacation: no such user uid %u.", getuid());
151 1.1 cgd exit(1);
152 1.1 cgd }
153 1.1 cgd }
154 1.1 cgd else if (!(pw = getpwnam(*argv))) {
155 1.13 mikel syslog(LOG_ERR, "vacation: no such user %s.", *argv);
156 1.1 cgd exit(1);
157 1.1 cgd }
158 1.1 cgd if (chdir(pw->pw_dir)) {
159 1.18 lukem syslog(LOG_ERR,
160 1.13 mikel "vacation: no such directory %s.", pw->pw_dir);
161 1.1 cgd exit(1);
162 1.1 cgd }
163 1.1 cgd
164 1.2 proven db = dbopen(VDB, O_CREAT|O_RDWR | (iflag ? O_TRUNC : 0),
165 1.5 jtc S_IRUSR|S_IWUSR, DB_HASH, NULL);
166 1.1 cgd if (!db) {
167 1.18 lukem syslog(LOG_ERR, "vacation: %s: %m", VDB);
168 1.1 cgd exit(1);
169 1.1 cgd }
170 1.1 cgd
171 1.1 cgd if (interval != -1)
172 1.1 cgd setinterval(interval);
173 1.1 cgd
174 1.1 cgd if (iflag) {
175 1.1 cgd (void)(db->close)(db);
176 1.1 cgd exit(0);
177 1.1 cgd }
178 1.1 cgd
179 1.17 mjl if (!(cur = malloc((size_t)sizeof(alias_t))))
180 1.1 cgd exit(1);
181 1.1 cgd cur->name = pw->pw_name;
182 1.1 cgd cur->next = names;
183 1.1 cgd names = cur;
184 1.1 cgd
185 1.1 cgd readheaders();
186 1.1 cgd if (!recent()) {
187 1.1 cgd setreply();
188 1.1 cgd (void)(db->close)(db);
189 1.1 cgd sendmessage(pw->pw_name);
190 1.1 cgd }
191 1.5 jtc else
192 1.5 jtc (void)(db->close)(db);
193 1.1 cgd exit(0);
194 1.1 cgd /* NOTREACHED */
195 1.1 cgd }
196 1.1 cgd
197 1.1 cgd /*
198 1.1 cgd * readheaders --
199 1.1 cgd * read mail headers
200 1.1 cgd */
201 1.6 jtc void
202 1.20 perry readheaders(void)
203 1.1 cgd {
204 1.17 mjl alias_t *cur;
205 1.10 mikel char *p;
206 1.1 cgd int tome, cont;
207 1.1 cgd char buf[MAXLINE];
208 1.1 cgd
209 1.1 cgd cont = tome = 0;
210 1.1 cgd while (fgets(buf, sizeof(buf), stdin) && *buf != '\n')
211 1.1 cgd switch(*buf) {
212 1.1 cgd case 'F': /* "From " */
213 1.21 christos /* XXX should instead consider Return-Path: and Sender: */
214 1.1 cgd cont = 0;
215 1.1 cgd if (!strncmp(buf, "From ", 5)) {
216 1.1 cgd for (p = buf + 5; *p && *p != ' '; ++p);
217 1.1 cgd *p = '\0';
218 1.1 cgd (void)strcpy(from, buf + 5);
219 1.10 mikel if ((p = strchr(from, '\n')))
220 1.1 cgd *p = '\0';
221 1.1 cgd if (junkmail())
222 1.1 cgd exit(0);
223 1.1 cgd }
224 1.1 cgd break;
225 1.1 cgd case 'P': /* "Precedence:" */
226 1.1 cgd cont = 0;
227 1.1 cgd if (strncasecmp(buf, "Precedence", 10) ||
228 1.10 mikel (buf[10] != ':' && buf[10] != ' ' &&
229 1.10 mikel buf[10] != '\t'))
230 1.1 cgd break;
231 1.10 mikel if (!(p = strchr(buf, ':')))
232 1.1 cgd break;
233 1.16 christos while (*++p && isspace((unsigned char)*p));
234 1.1 cgd if (!*p)
235 1.1 cgd break;
236 1.1 cgd if (!strncasecmp(p, "junk", 4) ||
237 1.5 jtc !strncasecmp(p, "bulk", 4) ||
238 1.5 jtc !strncasecmp(p, "list", 4))
239 1.1 cgd exit(0);
240 1.1 cgd break;
241 1.1 cgd case 'C': /* "Cc:" */
242 1.1 cgd if (strncmp(buf, "Cc:", 3))
243 1.1 cgd break;
244 1.1 cgd cont = 1;
245 1.1 cgd goto findme;
246 1.1 cgd case 'T': /* "To:" */
247 1.1 cgd if (strncmp(buf, "To:", 3))
248 1.21 christos break;
249 1.21 christos cont = 1;
250 1.21 christos goto findme;
251 1.21 christos case 'A':
252 1.21 christos if (strncmp(buf, "Apparently-To:", 3))
253 1.21 christos break;
254 1.21 christos cont = 1;
255 1.21 christos goto findme;
256 1.21 christos case 'D':
257 1.21 christos if (strncmp(buf, "Delivered-To:", 3))
258 1.1 cgd break;
259 1.1 cgd cont = 1;
260 1.1 cgd goto findme;
261 1.1 cgd default:
262 1.16 christos if (!isspace((unsigned char)*buf) || !cont || tome) {
263 1.1 cgd cont = 0;
264 1.1 cgd break;
265 1.1 cgd }
266 1.1 cgd findme: for (cur = names; !tome && cur; cur = cur->next)
267 1.1 cgd tome += nsearch(cur->name, buf);
268 1.1 cgd }
269 1.1 cgd if (!tome)
270 1.1 cgd exit(0);
271 1.1 cgd if (!*from) {
272 1.18 lukem syslog(LOG_ERR, "vacation: no initial \"From\" line.");
273 1.1 cgd exit(1);
274 1.1 cgd }
275 1.1 cgd }
276 1.1 cgd
277 1.1 cgd /*
278 1.1 cgd * nsearch --
279 1.1 cgd * do a nice, slow, search of a string for a substring.
280 1.1 cgd */
281 1.6 jtc int
282 1.17 mjl nsearch(const char *name, const char *str)
283 1.1 cgd {
284 1.10 mikel size_t len;
285 1.1 cgd
286 1.1 cgd for (len = strlen(name); *str; ++str)
287 1.17 mjl if (!strncasecmp(name, str, len))
288 1.1 cgd return(1);
289 1.1 cgd return(0);
290 1.1 cgd }
291 1.1 cgd
292 1.1 cgd /*
293 1.1 cgd * junkmail --
294 1.5 jtc * read the header and return if automagic/junk/bulk/list mail
295 1.1 cgd */
296 1.6 jtc int
297 1.20 perry junkmail(void)
298 1.1 cgd {
299 1.1 cgd static struct ignore {
300 1.1 cgd char *name;
301 1.1 cgd int len;
302 1.1 cgd } ignore[] = {
303 1.8 pk { "-request", 8 },
304 1.8 pk { "postmaster", 10 },
305 1.8 pk { "uucp", 4 },
306 1.8 pk { "mailer-daemon", 13 },
307 1.8 pk { "mailer", 6 },
308 1.8 pk { "-relay", 6 },
309 1.8 pk {NULL, 0 }
310 1.1 cgd };
311 1.10 mikel struct ignore *cur;
312 1.10 mikel int len;
313 1.10 mikel char *p;
314 1.1 cgd
315 1.1 cgd /*
316 1.1 cgd * This is mildly amusing, and I'm not positive it's right; trying
317 1.1 cgd * to find the "real" name of the sender, assuming that addresses
318 1.1 cgd * will be some variant of:
319 1.1 cgd *
320 1.1 cgd * From site!site!SENDER%site.domain%site.domain (at) site.domain
321 1.1 cgd */
322 1.10 mikel if (!(p = strchr(from, '%')))
323 1.10 mikel if (!(p = strchr(from, '@'))) {
324 1.10 mikel if ((p = strrchr(from, '!')))
325 1.1 cgd ++p;
326 1.1 cgd else
327 1.1 cgd p = from;
328 1.1 cgd for (; *p; ++p);
329 1.1 cgd }
330 1.1 cgd len = p - from;
331 1.1 cgd for (cur = ignore; cur->name; ++cur)
332 1.1 cgd if (len >= cur->len &&
333 1.1 cgd !strncasecmp(cur->name, p - cur->len, cur->len))
334 1.1 cgd return(1);
335 1.1 cgd return(0);
336 1.1 cgd }
337 1.1 cgd
338 1.1 cgd #define VIT "__VACATION__INTERVAL__TIMER__"
339 1.1 cgd
340 1.1 cgd /*
341 1.1 cgd * recent --
342 1.1 cgd * find out if user has gotten a vacation message recently.
343 1.11 lukem * use memmove for machines with alignment restrictions
344 1.1 cgd */
345 1.6 jtc int
346 1.20 perry recent(void)
347 1.1 cgd {
348 1.1 cgd DBT key, data;
349 1.1 cgd time_t then, next;
350 1.1 cgd
351 1.1 cgd /* get interval time */
352 1.1 cgd key.data = VIT;
353 1.1 cgd key.size = sizeof(VIT);
354 1.1 cgd if ((db->get)(db, &key, &data, 0))
355 1.1 cgd next = SECSPERDAY * DAYSPERWEEK;
356 1.1 cgd else
357 1.11 lukem memmove(&next, data.data, sizeof(next));
358 1.1 cgd
359 1.1 cgd /* get record for this address */
360 1.1 cgd key.data = from;
361 1.1 cgd key.size = strlen(from);
362 1.1 cgd if (!(db->get)(db, &key, &data, 0)) {
363 1.11 lukem memmove(&then, data.data, sizeof(then));
364 1.7 cgd if (next == (time_t)LONG_MAX || /* XXX */
365 1.7 cgd then + next > time(NULL))
366 1.1 cgd return(1);
367 1.1 cgd }
368 1.1 cgd return(0);
369 1.1 cgd }
370 1.1 cgd
371 1.1 cgd /*
372 1.1 cgd * setinterval --
373 1.1 cgd * store the reply interval
374 1.1 cgd */
375 1.6 jtc void
376 1.17 mjl setinterval(time_t interval)
377 1.1 cgd {
378 1.1 cgd DBT key, data;
379 1.1 cgd
380 1.1 cgd key.data = VIT;
381 1.1 cgd key.size = sizeof(VIT);
382 1.1 cgd data.data = &interval;
383 1.1 cgd data.size = sizeof(interval);
384 1.3 mycroft (void)(db->put)(db, &key, &data, 0);
385 1.1 cgd }
386 1.1 cgd
387 1.1 cgd /*
388 1.1 cgd * setreply --
389 1.1 cgd * store that this user knows about the vacation.
390 1.1 cgd */
391 1.6 jtc void
392 1.20 perry setreply(void)
393 1.1 cgd {
394 1.1 cgd DBT key, data;
395 1.1 cgd time_t now;
396 1.1 cgd
397 1.1 cgd key.data = from;
398 1.1 cgd key.size = strlen(from);
399 1.1 cgd (void)time(&now);
400 1.1 cgd data.data = &now;
401 1.1 cgd data.size = sizeof(now);
402 1.3 mycroft (void)(db->put)(db, &key, &data, 0);
403 1.1 cgd }
404 1.1 cgd
405 1.1 cgd /*
406 1.1 cgd * sendmessage --
407 1.1 cgd * exec sendmail to send the vacation file to sender
408 1.1 cgd */
409 1.6 jtc void
410 1.17 mjl sendmessage(const char *myname)
411 1.1 cgd {
412 1.5 jtc FILE *mfp, *sfp;
413 1.5 jtc int i;
414 1.5 jtc int pvect[2];
415 1.5 jtc char buf[MAXLINE];
416 1.5 jtc
417 1.5 jtc mfp = fopen(VMSG, "r");
418 1.5 jtc if (mfp == NULL) {
419 1.18 lukem syslog(LOG_ERR, "vacation: no ~%s/%s file.", myname, VMSG);
420 1.1 cgd exit(1);
421 1.1 cgd }
422 1.5 jtc if (pipe(pvect) < 0) {
423 1.13 mikel syslog(LOG_ERR, "vacation: pipe: %m");
424 1.5 jtc exit(1);
425 1.5 jtc }
426 1.5 jtc i = vfork();
427 1.5 jtc if (i < 0) {
428 1.13 mikel syslog(LOG_ERR, "vacation: fork: %m");
429 1.5 jtc exit(1);
430 1.5 jtc }
431 1.5 jtc if (i == 0) {
432 1.5 jtc dup2(pvect[0], 0);
433 1.5 jtc close(pvect[0]);
434 1.5 jtc close(pvect[1]);
435 1.14 cgd close(fileno(mfp));
436 1.9 mrg execl(_PATH_SENDMAIL, "sendmail", "-f", myname, "--", from,
437 1.9 mrg NULL);
438 1.13 mikel syslog(LOG_ERR, "vacation: can't exec %s: %m",
439 1.13 mikel _PATH_SENDMAIL);
440 1.12 thorpej _exit(1);
441 1.5 jtc }
442 1.5 jtc close(pvect[0]);
443 1.5 jtc sfp = fdopen(pvect[1], "w");
444 1.5 jtc fprintf(sfp, "To: %s\n", from);
445 1.5 jtc while (fgets(buf, sizeof buf, mfp))
446 1.5 jtc fputs(buf, sfp);
447 1.5 jtc fclose(mfp);
448 1.5 jtc fclose(sfp);
449 1.1 cgd }
450 1.1 cgd
451 1.6 jtc void
452 1.20 perry usage(void)
453 1.1 cgd {
454 1.10 mikel
455 1.18 lukem syslog(LOG_ERR, "uid %u: usage: vacation [-i] [-a alias] login",
456 1.1 cgd getuid());
457 1.1 cgd exit(1);
458 1.1 cgd }
459