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