Home | History | Annotate | Line # | Download | only in vacation
vacation.c revision 1.1.1.2
      1 /*
      2  * Copyright (c) 1983, 1987, 1993
      3  *	The Regents of the University of California.  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 static char copyright[] =
     36 "@(#) Copyright (c) 1983, 1987, 1993\n\
     37 	The Regents of the University of California.  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 static char sccsid[] = "@(#)vacation.c	8.2 (Berkeley) 1/26/94";
     42 #endif /* not lint */
     43 
     44 /*
     45 **  Vacation
     46 **  Copyright (c) 1983  Eric P. Allman
     47 **  Berkeley, California
     48 */
     49 
     50 #include <sys/param.h>
     51 #include <sys/stat.h>
     52 #include <fcntl.h>
     53 #include <pwd.h>
     54 #include <db.h>
     55 #include <time.h>
     56 #include <syslog.h>
     57 #include <tzfile.h>
     58 #include <errno.h>
     59 #include <unistd.h>
     60 #include <stdio.h>
     61 #include <ctype.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <paths.h>
     65 
     66 /*
     67  *  VACATION -- return a message to the sender when on vacation.
     68  *
     69  *	This program is invoked as a message receiver.  It returns a
     70  *	message specified by the user to whomever sent the mail, taking
     71  *	care not to return a message too often to prevent "I am on
     72  *	vacation" loops.
     73  */
     74 
     75 #define	MAXLINE	1024			/* max line from mail header */
     76 #define	VDB	".vacation.db"		/* dbm's database */
     77 #define	VMSG	".vacation.msg"		/* vacation message */
     78 
     79 typedef struct alias {
     80 	struct alias *next;
     81 	char *name;
     82 } ALIAS;
     83 ALIAS *names;
     84 
     85 DB *db;
     86 
     87 char from[MAXLINE];
     88 
     89 main(argc, argv)
     90 	int argc;
     91 	char **argv;
     92 {
     93 	extern int optind, opterr;
     94 	extern char *optarg;
     95 	struct passwd *pw;
     96 	ALIAS *cur;
     97 	time_t interval;
     98 	int ch, iflag;
     99 
    100 	opterr = iflag = 0;
    101 	interval = -1;
    102 	while ((ch = getopt(argc, argv, "a:Iir:")) != EOF)
    103 		switch((char)ch) {
    104 		case 'a':			/* alias */
    105 			if (!(cur = (ALIAS *)malloc((u_int)sizeof(ALIAS))))
    106 				break;
    107 			cur->name = optarg;
    108 			cur->next = names;
    109 			names = cur;
    110 			break;
    111 		case 'I':			/* backward compatible */
    112 		case 'i':			/* init the database */
    113 			iflag = 1;
    114 			break;
    115 		case 'r':
    116 			if (isdigit(*optarg)) {
    117 				interval = atol(optarg) * SECSPERDAY;
    118 				if (interval < 0)
    119 					usage();
    120 			}
    121 			else
    122 				interval = LONG_MAX;
    123 			break;
    124 		case '?':
    125 		default:
    126 			usage();
    127 		}
    128 	argc -= optind;
    129 	argv += optind;
    130 
    131 	if (argc != 1) {
    132 		if (!iflag)
    133 			usage();
    134 		if (!(pw = getpwuid(getuid()))) {
    135 			syslog(LOG_ERR,
    136 			    "vacation: no such user uid %u.\n", getuid());
    137 			exit(1);
    138 		}
    139 	}
    140 	else if (!(pw = getpwnam(*argv))) {
    141 		syslog(LOG_ERR, "vacation: no such user %s.\n", *argv);
    142 		exit(1);
    143 	}
    144 	if (chdir(pw->pw_dir)) {
    145 		syslog(LOG_NOTICE,
    146 		    "vacation: no such directory %s.\n", pw->pw_dir);
    147 		exit(1);
    148 	}
    149 
    150 	db = dbopen(VDB, O_CREAT|O_RDWR | (iflag ? O_TRUNC : 0),
    151 	    S_IRUSR|S_IWUSR, DB_HASH, NULL);
    152 	if (!db) {
    153 		syslog(LOG_NOTICE, "vacation: %s: %s\n", VDB, strerror(errno));
    154 		exit(1);
    155 	}
    156 
    157 	if (interval != -1)
    158 		setinterval(interval);
    159 
    160 	if (iflag) {
    161 		(void)(db->close)(db);
    162 		exit(0);
    163 	}
    164 
    165 	if (!(cur = malloc((u_int)sizeof(ALIAS))))
    166 		exit(1);
    167 	cur->name = pw->pw_name;
    168 	cur->next = names;
    169 	names = cur;
    170 
    171 	readheaders();
    172 	if (!recent()) {
    173 		setreply();
    174 		(void)(db->close)(db);
    175 		sendmessage(pw->pw_name);
    176 	}
    177 	else
    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 			    !strncasecmp(p, "list", 4))
    222 				exit(0);
    223 			break;
    224 		case 'C':		/* "Cc:" */
    225 			if (strncmp(buf, "Cc:", 3))
    226 				break;
    227 			cont = 1;
    228 			goto findme;
    229 		case 'T':		/* "To:" */
    230 			if (strncmp(buf, "To:", 3))
    231 				break;
    232 			cont = 1;
    233 			goto findme;
    234 		default:
    235 			if (!isspace(*buf) || !cont || tome) {
    236 				cont = 0;
    237 				break;
    238 			}
    239 findme:			for (cur = names; !tome && cur; cur = cur->next)
    240 				tome += nsearch(cur->name, buf);
    241 		}
    242 	if (!tome)
    243 		exit(0);
    244 	if (!*from) {
    245 		syslog(LOG_NOTICE, "vacation: no initial \"From\" line.\n");
    246 		exit(1);
    247 	}
    248 }
    249 
    250 /*
    251  * nsearch --
    252  *	do a nice, slow, search of a string for a substring.
    253  */
    254 nsearch(name, str)
    255 	register char *name, *str;
    256 {
    257 	register int len;
    258 
    259 	for (len = strlen(name); *str; ++str)
    260 		if (*str == *name && !strncasecmp(name, str, len))
    261 			return(1);
    262 	return(0);
    263 }
    264 
    265 /*
    266  * junkmail --
    267  *	read the header and return if automagic/junk/bulk/list mail
    268  */
    269 junkmail()
    270 {
    271 	static struct ignore {
    272 		char	*name;
    273 		int	len;
    274 	} ignore[] = {
    275 		"-request", 8,		"postmaster", 10,	"uucp", 4,
    276 		"mailer-daemon", 13,	"mailer", 6,		"-relay", 6,
    277 		NULL, NULL,
    278 	};
    279 	register struct ignore *cur;
    280 	register int len;
    281 	register char *p;
    282 
    283 	/*
    284 	 * This is mildly amusing, and I'm not positive it's right; trying
    285 	 * to find the "real" name of the sender, assuming that addresses
    286 	 * will be some variant of:
    287 	 *
    288 	 * From site!site!SENDER%site.domain%site.domain (at) site.domain
    289 	 */
    290 	if (!(p = index(from, '%')))
    291 		if (!(p = index(from, '@'))) {
    292 			if (p = rindex(from, '!'))
    293 				++p;
    294 			else
    295 				p = from;
    296 			for (; *p; ++p);
    297 		}
    298 	len = p - from;
    299 	for (cur = ignore; cur->name; ++cur)
    300 		if (len >= cur->len &&
    301 		    !strncasecmp(cur->name, p - cur->len, cur->len))
    302 			return(1);
    303 	return(0);
    304 }
    305 
    306 #define	VIT	"__VACATION__INTERVAL__TIMER__"
    307 
    308 /*
    309  * recent --
    310  *	find out if user has gotten a vacation message recently.
    311  *	use bcopy for machines with alignment restrictions
    312  */
    313 recent()
    314 {
    315 	DBT key, data;
    316 	time_t then, next;
    317 
    318 	/* get interval time */
    319 	key.data = VIT;
    320 	key.size = sizeof(VIT);
    321 	if ((db->get)(db, &key, &data, 0))
    322 		next = SECSPERDAY * DAYSPERWEEK;
    323 	else
    324 		bcopy(data.data, &next, sizeof(next));
    325 
    326 	/* get record for this address */
    327 	key.data = from;
    328 	key.size = strlen(from);
    329 	if (!(db->get)(db, &key, &data, 0)) {
    330 		bcopy(data.data, &then, sizeof(then));
    331 		if (next == LONG_MAX || then + next > time(NULL))
    332 			return(1);
    333 	}
    334 	return(0);
    335 }
    336 
    337 /*
    338  * setinterval --
    339  *	store the reply interval
    340  */
    341 setinterval(interval)
    342 	time_t interval;
    343 {
    344 	DBT key, data;
    345 
    346 	key.data = VIT;
    347 	key.size = sizeof(VIT);
    348 	data.data = &interval;
    349 	data.size = sizeof(interval);
    350 	(void)(db->put)(db, &key, &data, 0);
    351 }
    352 
    353 /*
    354  * setreply --
    355  *	store that this user knows about the vacation.
    356  */
    357 setreply()
    358 {
    359 	DBT key, data;
    360 	time_t now;
    361 
    362 	key.data = from;
    363 	key.size = strlen(from);
    364 	(void)time(&now);
    365 	data.data = &now;
    366 	data.size = sizeof(now);
    367 	(void)(db->put)(db, &key, &data, 0);
    368 }
    369 
    370 /*
    371  * sendmessage --
    372  *	exec sendmail to send the vacation file to sender
    373  */
    374 sendmessage(myname)
    375 	char *myname;
    376 {
    377 	FILE *mfp, *sfp;
    378 	int i;
    379 	int pvect[2];
    380 	char buf[MAXLINE];
    381 
    382 	mfp = fopen(VMSG, "r");
    383 	if (mfp == NULL) {
    384 		syslog(LOG_NOTICE, "vacation: no ~%s/%s file.\n", myname, VMSG);
    385 		exit(1);
    386 	}
    387 	if (pipe(pvect) < 0) {
    388 		syslog(LOG_ERR, "vacation: pipe: %s", strerror(errno));
    389 		exit(1);
    390 	}
    391 	i = vfork();
    392 	if (i < 0) {
    393 		syslog(LOG_ERR, "vacation: fork: %s", strerror(errno));
    394 		exit(1);
    395 	}
    396 	if (i == 0) {
    397 		dup2(pvect[0], 0);
    398 		close(pvect[0]);
    399 		close(pvect[1]);
    400 		fclose(mfp);
    401 		execl(_PATH_SENDMAIL, "sendmail", "-f", myname, from, NULL);
    402 		syslog(LOG_ERR, "vacation: can't exec %s: %s",
    403 			_PATH_SENDMAIL, strerror(errno));
    404 		exit(1);
    405 	}
    406 	close(pvect[0]);
    407 	sfp = fdopen(pvect[1], "w");
    408 	fprintf(sfp, "To: %s\n", from);
    409 	while (fgets(buf, sizeof buf, mfp))
    410 		fputs(buf, sfp);
    411 	fclose(mfp);
    412 	fclose(sfp);
    413 }
    414 
    415 usage()
    416 {
    417 	syslog(LOG_NOTICE, "uid %u: usage: vacation [-i] [-a alias] login\n",
    418 	    getuid());
    419 	exit(1);
    420 }
    421