Home | History | Annotate | Line # | Download | only in vacation
vacation.c revision 1.27
      1  1.27  christos /*	$NetBSD: vacation.c,v 1.27 2004/04/04 01:19:35 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.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.27  christos __RCSID("$NetBSD: vacation.c,v 1.27 2004/04/04 01:19:35 christos 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.1       cgd #define	VDB	".vacation.db"		/* dbm's database */
     81   1.1       cgd #define	VMSG	".vacation.msg"		/* vacation message */
     82   1.1       cgd 
     83   1.1       cgd typedef struct alias {
     84   1.1       cgd 	struct alias *next;
     85  1.15   mycroft 	const char *name;
     86  1.17       mjl } alias_t;
     87  1.17       mjl alias_t *names;
     88   1.1       cgd 
     89   1.1       cgd DB *db;
     90   1.6       jtc char from[MAXLINE];
     91  1.22  christos static int tflag = 0;
     92  1.25  christos #define	APPARENTLY_TO		1
     93  1.25  christos #define	DELIVERED_TO		2
     94  1.25  christos static int fflag = 0;
     95  1.25  christos #define	FROM_FROM		1
     96  1.25  christos #define	RETURN_PATH_FROM	2
     97  1.25  christos #define	SENDER_FROM		4
     98  1.27  christos static int debug = 0;
     99   1.1       cgd 
    100  1.17       mjl int main(int, char **);
    101  1.17       mjl int junkmail(void);
    102  1.17       mjl int nsearch(const char *, const char *);
    103  1.27  christos int readheaders(void);
    104  1.17       mjl int recent(void);
    105  1.25  christos void getfrom(char *);
    106  1.17       mjl void sendmessage(const char *);
    107  1.17       mjl void setinterval(time_t);
    108  1.17       mjl void setreply(void);
    109  1.17       mjl void usage(void);
    110   1.1       cgd 
    111   1.6       jtc int
    112  1.17       mjl main(int argc, char **argv)
    113   1.1       cgd {
    114   1.1       cgd 	struct passwd *pw;
    115  1.17       mjl 	alias_t *cur;
    116   1.1       cgd 	time_t interval;
    117  1.27  christos 	int ch, iflag, rv;
    118  1.22  christos 	char *p;
    119   1.1       cgd 
    120   1.1       cgd 	opterr = iflag = 0;
    121   1.1       cgd 	interval = -1;
    122  1.25  christos 	openlog(getprogname(), 0, LOG_USER);
    123  1.27  christos 	while ((ch = getopt(argc, argv, "a:df:Iir:t:")) != -1)
    124   1.1       cgd 		switch((char)ch) {
    125   1.1       cgd 		case 'a':			/* alias */
    126  1.17       mjl 			if (!(cur = (alias_t *)malloc((size_t)sizeof(alias_t))))
    127   1.1       cgd 				break;
    128   1.1       cgd 			cur->name = optarg;
    129   1.1       cgd 			cur->next = names;
    130   1.1       cgd 			names = cur;
    131   1.1       cgd 			break;
    132  1.27  christos 		case 'd':
    133  1.27  christos 			debug++;
    134  1.27  christos 			break;
    135  1.25  christos 		case 'f':
    136  1.25  christos 			for (p = optarg; *p; p++)
    137  1.25  christos 				switch (*p) {
    138  1.25  christos 				case 'F':
    139  1.25  christos 					fflag |= FROM_FROM;
    140  1.25  christos 					break;
    141  1.25  christos 				case 'R':
    142  1.25  christos 					fflag |= RETURN_PATH_FROM;
    143  1.25  christos 					break;
    144  1.25  christos 				case 'S':
    145  1.25  christos 					fflag |= SENDER_FROM;
    146  1.25  christos 					break;
    147  1.25  christos 				default:
    148  1.25  christos 					errx(1, "Unknown -f option `%c'", *p);
    149  1.25  christos 				}
    150  1.25  christos 			break;
    151   1.1       cgd 		case 'I':			/* backward compatible */
    152   1.1       cgd 		case 'i':			/* init the database */
    153   1.1       cgd 			iflag = 1;
    154   1.1       cgd 			break;
    155   1.1       cgd 		case 'r':
    156  1.16  christos 			if (isdigit((unsigned char)*optarg)) {
    157   1.1       cgd 				interval = atol(optarg) * SECSPERDAY;
    158   1.1       cgd 				if (interval < 0)
    159   1.1       cgd 					usage();
    160   1.1       cgd 			}
    161   1.1       cgd 			else
    162   1.7       cgd 				interval = (time_t)LONG_MAX;	/* XXX */
    163   1.1       cgd 			break;
    164  1.22  christos 		case 't':
    165  1.22  christos 			for (p = optarg; *p; p++)
    166  1.22  christos 				switch (*p) {
    167  1.22  christos 				case 'A':
    168  1.22  christos 					tflag |= APPARENTLY_TO;
    169  1.22  christos 					break;
    170  1.22  christos 				case 'D':
    171  1.22  christos 					tflag |= DELIVERED_TO;
    172  1.22  christos 					break;
    173  1.22  christos 				default:
    174  1.22  christos 					errx(1, "Unknown -t option `%c'", *p);
    175  1.22  christos 				}
    176  1.22  christos 			break;
    177   1.1       cgd 		case '?':
    178   1.1       cgd 		default:
    179   1.1       cgd 			usage();
    180   1.1       cgd 		}
    181   1.1       cgd 	argc -= optind;
    182   1.1       cgd 	argv += optind;
    183   1.1       cgd 
    184   1.1       cgd 	if (argc != 1) {
    185   1.1       cgd 		if (!iflag)
    186   1.1       cgd 			usage();
    187   1.1       cgd 		if (!(pw = getpwuid(getuid()))) {
    188  1.27  christos 			syslog(LOG_ERR, "%s: no such user uid %u.",
    189  1.27  christos 			    getprogname(), getuid());
    190   1.1       cgd 			exit(1);
    191   1.1       cgd 		}
    192   1.1       cgd 	}
    193   1.1       cgd 	else if (!(pw = getpwnam(*argv))) {
    194  1.27  christos 		syslog(LOG_ERR, "%s: no such user %s.",
    195  1.27  christos 		    getprogname(), *argv);
    196   1.1       cgd 		exit(1);
    197   1.1       cgd 	}
    198   1.1       cgd 	if (chdir(pw->pw_dir)) {
    199  1.27  christos 		syslog(LOG_ERR, "%s: no such directory %s.",
    200  1.27  christos 		    getprogname(), pw->pw_dir);
    201   1.1       cgd 		exit(1);
    202   1.1       cgd 	}
    203   1.1       cgd 
    204   1.2    proven 	db = dbopen(VDB, O_CREAT|O_RDWR | (iflag ? O_TRUNC : 0),
    205   1.5       jtc 	    S_IRUSR|S_IWUSR, DB_HASH, NULL);
    206   1.1       cgd 	if (!db) {
    207  1.27  christos 		syslog(LOG_ERR, "%s: %s: %m", getprogname(), VDB);
    208   1.1       cgd 		exit(1);
    209   1.1       cgd 	}
    210   1.1       cgd 
    211   1.1       cgd 	if (interval != -1)
    212   1.1       cgd 		setinterval(interval);
    213   1.1       cgd 
    214   1.1       cgd 	if (iflag) {
    215   1.1       cgd 		(void)(db->close)(db);
    216   1.1       cgd 		exit(0);
    217   1.1       cgd 	}
    218   1.1       cgd 
    219  1.27  christos 	if (!(cur = malloc((size_t)sizeof(alias_t)))) {
    220  1.27  christos 		syslog(LOG_ERR, "%s: %m", getprogname());
    221  1.27  christos 		(void)(db->close)(db);
    222   1.1       cgd 		exit(1);
    223  1.27  christos 	}
    224   1.1       cgd 	cur->name = pw->pw_name;
    225   1.1       cgd 	cur->next = names;
    226   1.1       cgd 	names = cur;
    227   1.1       cgd 
    228  1.27  christos 	if ((rv = readheaders()) != -1) {
    229  1.27  christos 		(void)(db->close)(db);
    230  1.27  christos 		exit(rv);
    231  1.27  christos 	}
    232  1.27  christos 
    233   1.1       cgd 	if (!recent()) {
    234   1.1       cgd 		setreply();
    235   1.1       cgd 		(void)(db->close)(db);
    236   1.1       cgd 		sendmessage(pw->pw_name);
    237   1.1       cgd 	}
    238   1.5       jtc 	else
    239   1.5       jtc 		(void)(db->close)(db);
    240   1.1       cgd 	exit(0);
    241   1.1       cgd 	/* NOTREACHED */
    242   1.1       cgd }
    243   1.1       cgd 
    244   1.1       cgd /*
    245   1.1       cgd  * readheaders --
    246   1.1       cgd  *	read mail headers
    247   1.1       cgd  */
    248  1.27  christos int
    249  1.20     perry readheaders(void)
    250   1.1       cgd {
    251  1.17       mjl 	alias_t *cur;
    252  1.10     mikel 	char *p;
    253   1.1       cgd 	int tome, cont;
    254   1.1       cgd 	char buf[MAXLINE];
    255   1.1       cgd 
    256   1.1       cgd 	cont = tome = 0;
    257   1.1       cgd 	while (fgets(buf, sizeof(buf), stdin) && *buf != '\n')
    258   1.1       cgd 		switch(*buf) {
    259  1.25  christos 		case 'F':		/* "From " or "From:" */
    260   1.1       cgd 			cont = 0;
    261  1.25  christos 			if (!strncmp(buf, "From ", 5))
    262  1.25  christos 				getfrom(buf + 5);
    263  1.26  christos 			if ((fflag & FROM_FROM) != 0 &&
    264  1.26  christos 			    strncmp(buf, "From:", 5) == 0)
    265  1.25  christos 				getfrom(buf + 5);
    266   1.1       cgd 			break;
    267   1.1       cgd 		case 'P':		/* "Precedence:" */
    268   1.1       cgd 			cont = 0;
    269   1.1       cgd 			if (strncasecmp(buf, "Precedence", 10) ||
    270  1.10     mikel 			    (buf[10] != ':' && buf[10] != ' ' &&
    271  1.10     mikel 			    buf[10] != '\t'))
    272   1.1       cgd 				break;
    273  1.10     mikel 			if (!(p = strchr(buf, ':')))
    274   1.1       cgd 				break;
    275  1.25  christos 			while (*++p && isspace((unsigned char)*p))
    276  1.25  christos 				continue;
    277   1.1       cgd 			if (!*p)
    278   1.1       cgd 				break;
    279   1.1       cgd 			if (!strncasecmp(p, "junk", 4) ||
    280   1.5       jtc 			    !strncasecmp(p, "bulk", 4) ||
    281   1.5       jtc 			    !strncasecmp(p, "list", 4))
    282   1.1       cgd 				exit(0);
    283   1.1       cgd 			break;
    284   1.1       cgd 		case 'C':		/* "Cc:" */
    285   1.1       cgd 			if (strncmp(buf, "Cc:", 3))
    286   1.1       cgd 				break;
    287   1.1       cgd 			cont = 1;
    288   1.1       cgd 			goto findme;
    289   1.1       cgd 		case 'T':		/* "To:" */
    290   1.1       cgd 			if (strncmp(buf, "To:", 3))
    291  1.21  christos 				break;
    292  1.21  christos 			cont = 1;
    293  1.21  christos 			goto findme;
    294  1.25  christos 		case 'A':		/* "Apparently-To:" */
    295  1.22  christos 			if ((tflag & APPARENTLY_TO) == 0 ||
    296  1.25  christos 			    strncmp(buf, "Apparently-To:", 14))
    297  1.21  christos 				break;
    298  1.21  christos 			cont = 1;
    299  1.21  christos 			goto findme;
    300  1.25  christos 		case 'D':		/* "Delivered-To:" */
    301  1.22  christos 			if ((tflag & DELIVERED_TO) == 0 ||
    302  1.25  christos 			    strncmp(buf, "Delivered-To:", 13))
    303   1.1       cgd 				break;
    304   1.1       cgd 			cont = 1;
    305   1.1       cgd 			goto findme;
    306  1.25  christos 		case 'R':		/* "Return-Path:" */
    307  1.25  christos 			cont = 0;
    308  1.26  christos 			if ((fflag & RETURN_PATH_FROM) != 0 &&
    309  1.26  christos 			    strncmp(buf, "Return-Path:", 12) == 0)
    310  1.25  christos 				getfrom(buf + 12);
    311  1.25  christos 			break;
    312  1.25  christos 		case 'S':		/* "Sender:" */
    313  1.25  christos 			cont = 0;
    314  1.26  christos 			if ((fflag & SENDER_FROM) != 0 &&
    315  1.26  christos 			    strncmp(buf, "Sender:", 7) == 0)
    316  1.25  christos 				getfrom(buf + 7);
    317  1.25  christos 			break;
    318   1.1       cgd 		default:
    319  1.16  christos 			if (!isspace((unsigned char)*buf) || !cont || tome) {
    320   1.1       cgd 				cont = 0;
    321   1.1       cgd 				break;
    322   1.1       cgd 			}
    323   1.1       cgd findme:			for (cur = names; !tome && cur; cur = cur->next)
    324   1.1       cgd 				tome += nsearch(cur->name, buf);
    325   1.1       cgd 		}
    326   1.1       cgd 	if (!tome)
    327  1.27  christos 		return 0;
    328   1.1       cgd 	if (!*from) {
    329  1.27  christos 		syslog(LOG_ERR, "%s: no initial \"From\" line.",
    330  1.27  christos 			getprogname());
    331  1.27  christos 		return 1;
    332   1.1       cgd 	}
    333  1.27  christos 	return -1;
    334   1.1       cgd }
    335   1.1       cgd 
    336   1.1       cgd /*
    337   1.1       cgd  * nsearch --
    338   1.1       cgd  *	do a nice, slow, search of a string for a substring.
    339   1.1       cgd  */
    340   1.6       jtc int
    341  1.17       mjl nsearch(const char *name, const char *str)
    342   1.1       cgd {
    343  1.10     mikel 	size_t len;
    344   1.1       cgd 
    345   1.1       cgd 	for (len = strlen(name); *str; ++str)
    346  1.17       mjl 		if (!strncasecmp(name, str, len))
    347   1.1       cgd 			return(1);
    348   1.1       cgd 	return(0);
    349   1.1       cgd }
    350   1.1       cgd 
    351   1.1       cgd /*
    352  1.25  christos  * getfrom --
    353  1.25  christos  *	return the first string in the buffer, stripping leading and trailing
    354  1.25  christos  *	blanks and <>.
    355  1.25  christos  */
    356  1.25  christos void
    357  1.25  christos getfrom(char *buf)
    358  1.25  christos {
    359  1.25  christos 	char *s, *p;
    360  1.25  christos 
    361  1.27  christos 	if ((s = strchr(buf, '<')) != NULL)
    362  1.27  christos 		s++;
    363  1.27  christos 	else
    364  1.27  christos 		s = buf;
    365  1.27  christos 
    366  1.27  christos 	for (; *s && isspace((unsigned char)*s); s++)
    367  1.25  christos 		continue;
    368  1.25  christos 	for (p = s; *p && !isspace((unsigned char)*p); p++)
    369  1.25  christos 		continue;
    370  1.25  christos 	if (*--p == '>')
    371  1.25  christos 		*p = '\0';
    372  1.25  christos 	else
    373  1.27  christos 		*++p = '\0';
    374  1.25  christos 	(void)strlcpy(from, s, sizeof(from));
    375  1.25  christos 	if (junkmail())
    376  1.25  christos 		exit(0);
    377  1.25  christos }
    378  1.25  christos 
    379  1.25  christos /*
    380   1.1       cgd  * junkmail --
    381   1.5       jtc  *	read the header and return if automagic/junk/bulk/list mail
    382   1.1       cgd  */
    383   1.6       jtc int
    384  1.20     perry junkmail(void)
    385   1.1       cgd {
    386   1.1       cgd 	static struct ignore {
    387   1.1       cgd 		char	*name;
    388   1.1       cgd 		int	len;
    389   1.1       cgd 	} ignore[] = {
    390   1.8        pk 		{ "-request", 8 },
    391   1.8        pk 		{ "postmaster", 10 },
    392   1.8        pk 		{ "uucp", 4 },
    393   1.8        pk 		{ "mailer-daemon", 13 },
    394   1.8        pk 		{ "mailer", 6 },
    395   1.8        pk 		{ "-relay", 6 },
    396   1.8        pk 		{NULL, 0 }
    397   1.1       cgd 	};
    398  1.10     mikel 	struct ignore *cur;
    399  1.10     mikel 	int len;
    400  1.10     mikel 	char *p;
    401   1.1       cgd 
    402   1.1       cgd 	/*
    403   1.1       cgd 	 * This is mildly amusing, and I'm not positive it's right; trying
    404   1.1       cgd 	 * to find the "real" name of the sender, assuming that addresses
    405   1.1       cgd 	 * will be some variant of:
    406   1.1       cgd 	 *
    407   1.1       cgd 	 * From site!site!SENDER%site.domain%site.domain (at) site.domain
    408   1.1       cgd 	 */
    409  1.10     mikel 	if (!(p = strchr(from, '%')))
    410  1.10     mikel 		if (!(p = strchr(from, '@'))) {
    411  1.10     mikel 			if ((p = strrchr(from, '!')))
    412   1.1       cgd 				++p;
    413   1.1       cgd 			else
    414   1.1       cgd 				p = from;
    415   1.1       cgd 			for (; *p; ++p);
    416   1.1       cgd 		}
    417   1.1       cgd 	len = p - from;
    418   1.1       cgd 	for (cur = ignore; cur->name; ++cur)
    419   1.1       cgd 		if (len >= cur->len &&
    420   1.1       cgd 		    !strncasecmp(cur->name, p - cur->len, cur->len))
    421   1.1       cgd 			return(1);
    422   1.1       cgd 	return(0);
    423   1.1       cgd }
    424   1.1       cgd 
    425   1.1       cgd #define	VIT	"__VACATION__INTERVAL__TIMER__"
    426   1.1       cgd 
    427   1.1       cgd /*
    428   1.1       cgd  * recent --
    429   1.1       cgd  *	find out if user has gotten a vacation message recently.
    430  1.11     lukem  *	use memmove for machines with alignment restrictions
    431   1.1       cgd  */
    432   1.6       jtc int
    433  1.20     perry recent(void)
    434   1.1       cgd {
    435   1.1       cgd 	DBT key, data;
    436   1.1       cgd 	time_t then, next;
    437   1.1       cgd 
    438   1.1       cgd 	/* get interval time */
    439   1.1       cgd 	key.data = VIT;
    440   1.1       cgd 	key.size = sizeof(VIT);
    441   1.1       cgd 	if ((db->get)(db, &key, &data, 0))
    442   1.1       cgd 		next = SECSPERDAY * DAYSPERWEEK;
    443   1.1       cgd 	else
    444  1.11     lukem 		memmove(&next, data.data, sizeof(next));
    445   1.1       cgd 
    446   1.1       cgd 	/* get record for this address */
    447   1.1       cgd 	key.data = from;
    448   1.1       cgd 	key.size = strlen(from);
    449   1.1       cgd 	if (!(db->get)(db, &key, &data, 0)) {
    450  1.11     lukem 		memmove(&then, data.data, sizeof(then));
    451   1.7       cgd 		if (next == (time_t)LONG_MAX ||			/* XXX */
    452   1.7       cgd 		    then + next > time(NULL))
    453   1.1       cgd 			return(1);
    454   1.1       cgd 	}
    455   1.1       cgd 	return(0);
    456   1.1       cgd }
    457   1.1       cgd 
    458   1.1       cgd /*
    459   1.1       cgd  * setinterval --
    460   1.1       cgd  *	store the reply interval
    461   1.1       cgd  */
    462   1.6       jtc void
    463  1.17       mjl setinterval(time_t interval)
    464   1.1       cgd {
    465   1.1       cgd 	DBT key, data;
    466   1.1       cgd 
    467   1.1       cgd 	key.data = VIT;
    468   1.1       cgd 	key.size = sizeof(VIT);
    469   1.1       cgd 	data.data = &interval;
    470   1.1       cgd 	data.size = sizeof(interval);
    471   1.3   mycroft 	(void)(db->put)(db, &key, &data, 0);
    472   1.1       cgd }
    473   1.1       cgd 
    474   1.1       cgd /*
    475   1.1       cgd  * setreply --
    476   1.1       cgd  *	store that this user knows about the vacation.
    477   1.1       cgd  */
    478   1.6       jtc void
    479  1.20     perry setreply(void)
    480   1.1       cgd {
    481   1.1       cgd 	DBT key, data;
    482   1.1       cgd 	time_t now;
    483   1.1       cgd 
    484   1.1       cgd 	key.data = from;
    485   1.1       cgd 	key.size = strlen(from);
    486   1.1       cgd 	(void)time(&now);
    487   1.1       cgd 	data.data = &now;
    488   1.1       cgd 	data.size = sizeof(now);
    489   1.3   mycroft 	(void)(db->put)(db, &key, &data, 0);
    490   1.1       cgd }
    491   1.1       cgd 
    492   1.1       cgd /*
    493   1.1       cgd  * sendmessage --
    494   1.1       cgd  *	exec sendmail to send the vacation file to sender
    495   1.1       cgd  */
    496   1.6       jtc void
    497  1.17       mjl sendmessage(const char *myname)
    498   1.1       cgd {
    499   1.5       jtc 	FILE *mfp, *sfp;
    500   1.5       jtc 	int i;
    501   1.5       jtc 	int pvect[2];
    502   1.5       jtc 	char buf[MAXLINE];
    503   1.5       jtc 
    504   1.5       jtc 	mfp = fopen(VMSG, "r");
    505   1.5       jtc 	if (mfp == NULL) {
    506  1.27  christos 		syslog(LOG_ERR, "%s: no ~%s/%s file.", getprogname(),
    507  1.27  christos 		    myname, VMSG);
    508   1.1       cgd 		exit(1);
    509   1.1       cgd 	}
    510  1.27  christos 
    511  1.27  christos 	if (debug) {
    512  1.27  christos 		sfp = stdout;
    513  1.27  christos 	} else {
    514  1.27  christos 		if (pipe(pvect) < 0) {
    515  1.27  christos 			syslog(LOG_ERR, "%s: pipe: %m", getprogname());
    516  1.27  christos 			exit(1);
    517  1.27  christos 		}
    518  1.27  christos 		i = vfork();
    519  1.27  christos 		if (i < 0) {
    520  1.27  christos 			syslog(LOG_ERR, "%s: fork: %m", getprogname());
    521  1.27  christos 			exit(1);
    522  1.27  christos 		}
    523  1.27  christos 		if (i == 0) {
    524  1.27  christos 			dup2(pvect[0], 0);
    525  1.27  christos 			close(pvect[0]);
    526  1.27  christos 			close(pvect[1]);
    527  1.27  christos 			close(fileno(mfp));
    528  1.27  christos 			execl(_PATH_SENDMAIL, "sendmail", "-f", myname,
    529  1.27  christos 			    "--", from, NULL);
    530  1.27  christos 			syslog(LOG_ERR, "%s: can't exec %s: %m",
    531  1.27  christos 			    getprogname(), _PATH_SENDMAIL);
    532  1.27  christos 			_exit(1);
    533  1.27  christos 		}
    534  1.27  christos 		(void)close(pvect[0]);
    535  1.27  christos 		sfp = fdopen(pvect[1], "w");
    536  1.27  christos 	}
    537   1.5       jtc 	fprintf(sfp, "To: %s\n", from);
    538   1.5       jtc 	while (fgets(buf, sizeof buf, mfp))
    539   1.5       jtc 		fputs(buf, sfp);
    540  1.27  christos 	(void)fclose(mfp);
    541  1.27  christos 	if (sfp != stdout)
    542  1.27  christos 		(void)fclose(sfp);
    543   1.1       cgd }
    544   1.1       cgd 
    545   1.6       jtc void
    546  1.20     perry usage(void)
    547   1.1       cgd {
    548  1.10     mikel 
    549  1.27  christos 	syslog(LOG_ERR, "uid %u: Usage: %s [-id] [-a alias] [-t A|D] [-f F|R|S]"
    550  1.27  christos 	    " login", getuid(), getprogname());
    551   1.1       cgd 	exit(1);
    552   1.1       cgd }
    553