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