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