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