xref: /onnv-gate/usr/src/cmd/sendmail/aux/vacation.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  *	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
6*0Sstevel@tonic-gate  *	  All Rights Reserved
7*0Sstevel@tonic-gate  */
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate  *  Vacation
11*0Sstevel@tonic-gate  *  Copyright (c) 1983  Eric P. Allman
12*0Sstevel@tonic-gate  *  Berkeley, California
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  *  Copyright (c) 1983 Regents of the University of California.
15*0Sstevel@tonic-gate  *  All rights reserved.  The Berkeley software License Agreement
16*0Sstevel@tonic-gate  *  specifies the terms and conditions for redistribution.
17*0Sstevel@tonic-gate  */
18*0Sstevel@tonic-gate 
19*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #ifndef lint
22*0Sstevel@tonic-gate static char	SccsId[] = "%W%	%E% SMI";
23*0Sstevel@tonic-gate #endif /* not lint */
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate #include <stdio.h>
26*0Sstevel@tonic-gate #include <stdarg.h>
27*0Sstevel@tonic-gate #include <stdlib.h>
28*0Sstevel@tonic-gate #include <unistd.h>
29*0Sstevel@tonic-gate #include <sysexits.h>
30*0Sstevel@tonic-gate #include <pwd.h>
31*0Sstevel@tonic-gate #include <ndbm.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <ctype.h>
34*0Sstevel@tonic-gate #include <fcntl.h>
35*0Sstevel@tonic-gate #include <strings.h>
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate  *  VACATION -- return a message to the sender when on vacation.
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  *	This program could be invoked as a message receiver
42*0Sstevel@tonic-gate  *	when someone is on vacation.  It returns a message
43*0Sstevel@tonic-gate  *	specified by the user to whoever sent the mail, taking
44*0Sstevel@tonic-gate  *	care not to return a message too often to prevent
45*0Sstevel@tonic-gate  *	"I am on vacation" loops.
46*0Sstevel@tonic-gate  *
47*0Sstevel@tonic-gate  *	For best operation, this program should run setuid to
48*0Sstevel@tonic-gate  *	root or uucp or someone else that sendmail will believe
49*0Sstevel@tonic-gate  *	a -f flag from.  Otherwise, the user must be careful
50*0Sstevel@tonic-gate  *	to include a header on his .vacation.msg file.
51*0Sstevel@tonic-gate  *
52*0Sstevel@tonic-gate  *	Positional Parameters:
53*0Sstevel@tonic-gate  *		the user to collect the vacation message from.
54*0Sstevel@tonic-gate  *
55*0Sstevel@tonic-gate  *	Flag Parameters:
56*0Sstevel@tonic-gate  *		-I	initialize the database.
57*0Sstevel@tonic-gate  *		-d	turn on debugging.
58*0Sstevel@tonic-gate  *		-tT	set the timeout to T.  messages arriving more
59*0Sstevel@tonic-gate  *			often than T will be ignored to avoid loops.
60*0Sstevel@tonic-gate  *
61*0Sstevel@tonic-gate  *	Side Effects:
62*0Sstevel@tonic-gate  *		A message is sent back to the sender.
63*0Sstevel@tonic-gate  *
64*0Sstevel@tonic-gate  *	Author:
65*0Sstevel@tonic-gate  *		Eric Allman
66*0Sstevel@tonic-gate  *		UCB/INGRES
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate #define	MAXLINE	256	/* max size of a line */
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate #define	ONEWEEK	(60L*60L*24L*7L)
72*0Sstevel@tonic-gate #define	MsgFile "/.vacation.msg"
73*0Sstevel@tonic-gate #define	FilterFile "/.vacation.filter"
74*0Sstevel@tonic-gate #define	DbFileBase "/.vacation"
75*0Sstevel@tonic-gate #define	_PATH_TMP	"/tmp/vacation.XXXXXX"
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate typedef int bool;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate #define	FALSE	0
80*0Sstevel@tonic-gate #define	TRUE	1
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate static time_t	Timeout = ONEWEEK;	/* timeout between notices per user */
83*0Sstevel@tonic-gate static DBM	*db;
84*0Sstevel@tonic-gate static bool	Debug = FALSE;
85*0Sstevel@tonic-gate static bool	AnswerAll = FALSE;	/* default: answer if in To:/Cc: only */
86*0Sstevel@tonic-gate static char	*Subject = NULL;	/* subject in message header */
87*0Sstevel@tonic-gate static char	*EncodedSubject = NULL;	/* subject in message header */
88*0Sstevel@tonic-gate static char	Charset[MAXLINE];	/* for use in reply message */
89*0Sstevel@tonic-gate static char	*AliasList[MAXLINE];	/* list of aliases to allow */
90*0Sstevel@tonic-gate static int	AliasCount = 0;
91*0Sstevel@tonic-gate static char	*myname;		/* name of person "on vacation" */
92*0Sstevel@tonic-gate static char	*homedir;		/* home directory of said person */
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate extern time_t	convtime(char *, char);
95*0Sstevel@tonic-gate extern bool	decode_rfc2047(char *, char *, char *);
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate static bool	ask(char *);
98*0Sstevel@tonic-gate static bool	junkmail(char *);
99*0Sstevel@tonic-gate static bool	filter_ok(char *, char *);
100*0Sstevel@tonic-gate static bool	knows(char *);
101*0Sstevel@tonic-gate static bool	sameword(char *, char *);
102*0Sstevel@tonic-gate static char	*getfrom(char **);
103*0Sstevel@tonic-gate static char	*newstr(char *);
104*0Sstevel@tonic-gate static void	AutoInstall();
105*0Sstevel@tonic-gate static void	initialize(char *);
106*0Sstevel@tonic-gate static void	sendmessage(char *, char *, char *);
107*0Sstevel@tonic-gate static void	setknows(char *);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate void	usrerr(const char *, ...);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate int
112*0Sstevel@tonic-gate main(argc, argv)
113*0Sstevel@tonic-gate 	int argc;
114*0Sstevel@tonic-gate 	char **argv;
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate 	char *from;
117*0Sstevel@tonic-gate 	char *p, *at, *c;
118*0Sstevel@tonic-gate 	struct passwd *pw;
119*0Sstevel@tonic-gate 	char *shortfrom;
120*0Sstevel@tonic-gate 	char buf[MAXLINE];
121*0Sstevel@tonic-gate 	char *message_file = MsgFile;
122*0Sstevel@tonic-gate 	char *db_file_base = DbFileBase;
123*0Sstevel@tonic-gate 	char *filter_file = FilterFile;
124*0Sstevel@tonic-gate 	char *sender;
125*0Sstevel@tonic-gate 	bool sender_oob = FALSE;
126*0Sstevel@tonic-gate 	bool initialize_only = FALSE;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	/* process arguments */
129*0Sstevel@tonic-gate 	while (--argc > 0 && (p = *++argv) != NULL && *p == '-')
130*0Sstevel@tonic-gate 	{
131*0Sstevel@tonic-gate 		switch (*++p)
132*0Sstevel@tonic-gate 		{
133*0Sstevel@tonic-gate 		    case 'a':	/* add this to list of acceptable aliases */
134*0Sstevel@tonic-gate 			AliasList[AliasCount++] = argv[1];
135*0Sstevel@tonic-gate 			if (argc > 0) {
136*0Sstevel@tonic-gate 				argc--; argv++;
137*0Sstevel@tonic-gate 			}
138*0Sstevel@tonic-gate 			break;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 		    case 'd':	/* debug */
141*0Sstevel@tonic-gate 			Debug = TRUE;
142*0Sstevel@tonic-gate 			break;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 		    case 'e':	/* alternate filter file */
145*0Sstevel@tonic-gate 			filter_file = argv[1];
146*0Sstevel@tonic-gate 			if (argc > 0) {
147*0Sstevel@tonic-gate 				argc--; argv++;
148*0Sstevel@tonic-gate 			}
149*0Sstevel@tonic-gate 			break;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 		    case 'f':	/* alternate database file name base */
152*0Sstevel@tonic-gate 			db_file_base = argv[1];
153*0Sstevel@tonic-gate 			if (argc > 0) {
154*0Sstevel@tonic-gate 				argc--; argv++;
155*0Sstevel@tonic-gate 			}
156*0Sstevel@tonic-gate 			break;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 		    case 'I':	/* initialize */
159*0Sstevel@tonic-gate 			initialize_only = TRUE;
160*0Sstevel@tonic-gate 			break;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 		    case 'j':	/* answer all mail, even if not in To/Cc */
163*0Sstevel@tonic-gate 			AnswerAll = TRUE;
164*0Sstevel@tonic-gate 			break;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 		    case 'm':	/* alternate message file */
167*0Sstevel@tonic-gate 			message_file = argv[1];
168*0Sstevel@tonic-gate 			if (argc > 0) {
169*0Sstevel@tonic-gate 				argc--; argv++;
170*0Sstevel@tonic-gate 			}
171*0Sstevel@tonic-gate 			break;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 		    case 's':	/* sender: use this instead of getfrom() */
174*0Sstevel@tonic-gate 			sender = argv[1];
175*0Sstevel@tonic-gate 			sender_oob = TRUE;
176*0Sstevel@tonic-gate 			if (argc > 0) {
177*0Sstevel@tonic-gate 				argc--; argv++;
178*0Sstevel@tonic-gate 			}
179*0Sstevel@tonic-gate 			break;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 		    case 't':	/* set timeout */
182*0Sstevel@tonic-gate 			Timeout = convtime(++p, 'w');
183*0Sstevel@tonic-gate 			break;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 		    default:
186*0Sstevel@tonic-gate 			usrerr("Unknown flag -%s", p);
187*0Sstevel@tonic-gate 			exit(EX_USAGE);
188*0Sstevel@tonic-gate 		}
189*0Sstevel@tonic-gate 	}
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	if (initialize_only)
192*0Sstevel@tonic-gate 	{
193*0Sstevel@tonic-gate 		initialize(db_file_base);
194*0Sstevel@tonic-gate 		exit(EX_OK);
195*0Sstevel@tonic-gate 	}
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	/* verify recipient argument */
198*0Sstevel@tonic-gate 	if (argc == 0)
199*0Sstevel@tonic-gate 		AutoInstall();
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if (argc != 1)
202*0Sstevel@tonic-gate 	{
203*0Sstevel@tonic-gate 		usrerr("Usage: vacation username (or) vacation -I");
204*0Sstevel@tonic-gate 		exit(EX_USAGE);
205*0Sstevel@tonic-gate 	}
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	myname = p;
208*0Sstevel@tonic-gate 	Charset[0] = '\0';
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	/* find user's home directory */
211*0Sstevel@tonic-gate 	pw = getpwnam(myname);
212*0Sstevel@tonic-gate 	if (pw == NULL)
213*0Sstevel@tonic-gate 	{
214*0Sstevel@tonic-gate 		usrerr("user %s look up failed, name services outage ?",
215*0Sstevel@tonic-gate 		    myname);
216*0Sstevel@tonic-gate 		exit(EX_TEMPFAIL);
217*0Sstevel@tonic-gate 	}
218*0Sstevel@tonic-gate 	homedir = newstr(pw->pw_dir);
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s%s%s", homedir,
221*0Sstevel@tonic-gate 			(db_file_base[0] == '/') ? "" : "/", db_file_base);
222*0Sstevel@tonic-gate 	if (!(db = dbm_open(buf, O_RDWR, 0))) {
223*0Sstevel@tonic-gate 		usrerr("%s: %s\n", buf, strerror(errno));
224*0Sstevel@tonic-gate 		exit(EX_DATAERR);
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	if (sender_oob)
228*0Sstevel@tonic-gate 	{
229*0Sstevel@tonic-gate 		at = strchr(sender, '@');
230*0Sstevel@tonic-gate 		if (at != NULL)
231*0Sstevel@tonic-gate 			for (c = at + 1; *c; c++)
232*0Sstevel@tonic-gate 				*c = (char)tolower((char)*c);
233*0Sstevel@tonic-gate 		from = sender;
234*0Sstevel@tonic-gate 		shortfrom = sender;
235*0Sstevel@tonic-gate 	}
236*0Sstevel@tonic-gate 	else
237*0Sstevel@tonic-gate 		/* read message from standard input (just from line) */
238*0Sstevel@tonic-gate 		from = getfrom(&shortfrom);
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	/* check if junk mail or this person is already informed */
241*0Sstevel@tonic-gate 	if (!junkmail(shortfrom) && filter_ok(shortfrom, filter_file) &&
242*0Sstevel@tonic-gate 	    !knows(shortfrom))
243*0Sstevel@tonic-gate 	{
244*0Sstevel@tonic-gate 		/* mark this person as knowing */
245*0Sstevel@tonic-gate 		setknows(shortfrom);
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 		/* send the message back */
248*0Sstevel@tonic-gate 		(void) strlcpy(buf, homedir, sizeof (buf));
249*0Sstevel@tonic-gate 		if (message_file[0] != '/')
250*0Sstevel@tonic-gate 		    (void) strlcat(buf, "/", sizeof (buf));
251*0Sstevel@tonic-gate 		(void) strlcat(buf, message_file, sizeof (buf));
252*0Sstevel@tonic-gate 		if (Debug)
253*0Sstevel@tonic-gate 			printf("Sending %s to %s\n", buf, from);
254*0Sstevel@tonic-gate 		else
255*0Sstevel@tonic-gate 		{
256*0Sstevel@tonic-gate 			sendmessage(buf, from, myname);
257*0Sstevel@tonic-gate 			/*NOTREACHED*/
258*0Sstevel@tonic-gate 		}
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate 	return (EX_OK);
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate /*
264*0Sstevel@tonic-gate  *  GETFROM -- read message from standard input and return sender
265*0Sstevel@tonic-gate  *
266*0Sstevel@tonic-gate  *	Parameters:
267*0Sstevel@tonic-gate  *		none.
268*0Sstevel@tonic-gate  *
269*0Sstevel@tonic-gate  *	Returns:
270*0Sstevel@tonic-gate  *		pointer to the sender address.
271*0Sstevel@tonic-gate  *
272*0Sstevel@tonic-gate  *	Side Effects:
273*0Sstevel@tonic-gate  *		Reads first line from standard input.
274*0Sstevel@tonic-gate  */
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate static char *
277*0Sstevel@tonic-gate getfrom(shortp)
278*0Sstevel@tonic-gate char **shortp;
279*0Sstevel@tonic-gate {
280*0Sstevel@tonic-gate 	static char line[MAXLINE];
281*0Sstevel@tonic-gate 	char *p, *start, *at, *bang, *c;
282*0Sstevel@tonic-gate 	char saveat;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	/* read the from line */
285*0Sstevel@tonic-gate 	if (fgets(line, sizeof (line), stdin) == NULL ||
286*0Sstevel@tonic-gate 	    strncmp(line, "From ", 5) != NULL)
287*0Sstevel@tonic-gate 	{
288*0Sstevel@tonic-gate 		usrerr("No initial From line");
289*0Sstevel@tonic-gate 		exit(EX_PROTOCOL);
290*0Sstevel@tonic-gate 	}
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	/* find the end of the sender address and terminate it */
293*0Sstevel@tonic-gate 	start = &line[5];
294*0Sstevel@tonic-gate 	p = strchr(start, ' ');
295*0Sstevel@tonic-gate 	if (p == NULL)
296*0Sstevel@tonic-gate 	{
297*0Sstevel@tonic-gate 		usrerr("Funny From line '%s'", line);
298*0Sstevel@tonic-gate 		exit(EX_PROTOCOL);
299*0Sstevel@tonic-gate 	}
300*0Sstevel@tonic-gate 	*p = '\0';
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	/*
303*0Sstevel@tonic-gate 	 * Strip all but the rightmost UUCP host
304*0Sstevel@tonic-gate 	 * to prevent loops due to forwarding.
305*0Sstevel@tonic-gate 	 * Start searching leftward from the leftmost '@'.
306*0Sstevel@tonic-gate 	 *	a!b!c!d yields a short name of c!d
307*0Sstevel@tonic-gate 	 *	a!b!c!d@e yields a short name of c!d@e
308*0Sstevel@tonic-gate 	 *	e@a!b!c yields the same short name
309*0Sstevel@tonic-gate 	 */
310*0Sstevel@tonic-gate #ifdef VDEBUG
311*0Sstevel@tonic-gate printf("start='%s'\n", start);
312*0Sstevel@tonic-gate #endif /* VDEBUG */
313*0Sstevel@tonic-gate 	*shortp = start;			/* assume whole addr */
314*0Sstevel@tonic-gate 	if ((at = strchr(start, '@')) == NULL)	/* leftmost '@' */
315*0Sstevel@tonic-gate 		at = p;				/* if none, use end of addr */
316*0Sstevel@tonic-gate 	saveat = *at;
317*0Sstevel@tonic-gate 	*at = '\0';
318*0Sstevel@tonic-gate 	if ((bang = strrchr(start, '!')) != NULL) {	/* rightmost '!' */
319*0Sstevel@tonic-gate 		char *bang2;
320*0Sstevel@tonic-gate 		*bang = '\0';
321*0Sstevel@tonic-gate 		/* 2nd rightmost '!' */
322*0Sstevel@tonic-gate 		if ((bang2 = strrchr(start, '!')) != NULL)
323*0Sstevel@tonic-gate 			*shortp = bang2 + 1;		/* move past ! */
324*0Sstevel@tonic-gate 		*bang = '!';
325*0Sstevel@tonic-gate 	}
326*0Sstevel@tonic-gate 	*at = saveat;
327*0Sstevel@tonic-gate #ifdef VDEBUG
328*0Sstevel@tonic-gate printf("place='%s'\n", *shortp);
329*0Sstevel@tonic-gate #endif /* VDEBUG */
330*0Sstevel@tonic-gate 	for (c = at + 1; *c; c++)
331*0Sstevel@tonic-gate 		*c = (char)tolower((char)*c);
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	/* return the sender address */
334*0Sstevel@tonic-gate 	return (start);
335*0Sstevel@tonic-gate }
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate /*
338*0Sstevel@tonic-gate  *  JUNKMAIL -- read the header and tell us if this is junk/bulk mail.
339*0Sstevel@tonic-gate  *
340*0Sstevel@tonic-gate  *	Parameters:
341*0Sstevel@tonic-gate  *		from -- the Return-Path of the sender.  We assume that
342*0Sstevel@tonic-gate  *			anything from "*-REQUEST@*" is bulk mail.
343*0Sstevel@tonic-gate  *
344*0Sstevel@tonic-gate  *	Returns:
345*0Sstevel@tonic-gate  *		TRUE -- if this is junk or bulk mail (that is, if the
346*0Sstevel@tonic-gate  *			sender shouldn't receive a response).
347*0Sstevel@tonic-gate  *		FALSE -- if the sender deserves a response.
348*0Sstevel@tonic-gate  *
349*0Sstevel@tonic-gate  *	Side Effects:
350*0Sstevel@tonic-gate  *		May read the header from standard input.  When this
351*0Sstevel@tonic-gate  *		returns the position on stdin is undefined.
352*0Sstevel@tonic-gate  */
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate static bool
355*0Sstevel@tonic-gate junkmail(from)
356*0Sstevel@tonic-gate 	char *from;
357*0Sstevel@tonic-gate {
358*0Sstevel@tonic-gate 	register char *p;
359*0Sstevel@tonic-gate 	char buf[MAXLINE+1];
360*0Sstevel@tonic-gate 	bool inside, onlist;
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 	/* test for inhuman sender */
363*0Sstevel@tonic-gate 	p = strrchr(from, '@');
364*0Sstevel@tonic-gate 	if (p != NULL)
365*0Sstevel@tonic-gate 	{
366*0Sstevel@tonic-gate 		*p = '\0';
367*0Sstevel@tonic-gate 		if (sameword(&p[-8],  "-REQUEST") ||
368*0Sstevel@tonic-gate 		    sameword(&p[-10], "Postmaster") ||
369*0Sstevel@tonic-gate 		    sameword(&p[-13], "MAILER-DAEMON"))
370*0Sstevel@tonic-gate 		{
371*0Sstevel@tonic-gate 			*p = '@';
372*0Sstevel@tonic-gate 			return (TRUE);
373*0Sstevel@tonic-gate 		}
374*0Sstevel@tonic-gate 		*p = '@';
375*0Sstevel@tonic-gate 	}
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate #define	Delims " \n\t:,:;()<>@!"
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	/* read the header looking for "interesting" lines */
380*0Sstevel@tonic-gate 	inside = FALSE;
381*0Sstevel@tonic-gate 	onlist = FALSE;
382*0Sstevel@tonic-gate 	while (fgets(buf, MAXLINE, stdin) != NULL && buf[0] != '\n')
383*0Sstevel@tonic-gate 	{
384*0Sstevel@tonic-gate 		if (buf[0] != ' ' && buf[0] != '\t' && strchr(buf, ':') == NULL)
385*0Sstevel@tonic-gate 			return (FALSE);			/* no header found */
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 		p = strtok(buf, Delims);
388*0Sstevel@tonic-gate 		if (p == NULL)
389*0Sstevel@tonic-gate 			continue;
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 		if (sameword(p, "To") || sameword(p, "Cc"))
392*0Sstevel@tonic-gate 		{
393*0Sstevel@tonic-gate 			inside = TRUE;
394*0Sstevel@tonic-gate 			p = strtok((char *)NULL, Delims);
395*0Sstevel@tonic-gate 			if (p == NULL)
396*0Sstevel@tonic-gate 				continue;
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 		} else				/* continuation line? */
399*0Sstevel@tonic-gate 		    if (inside)
400*0Sstevel@tonic-gate 			inside =  (buf[0] == ' ' || buf[0] == '\t');
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 		if (inside) {
403*0Sstevel@tonic-gate 		    int i;
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 		    do {
406*0Sstevel@tonic-gate 			if (sameword(p, myname))
407*0Sstevel@tonic-gate 				onlist = TRUE;		/* I am on the list */
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 			for (i = 0; i < AliasCount; i++)
410*0Sstevel@tonic-gate 			    if (sameword(p, AliasList[i]))
411*0Sstevel@tonic-gate 				onlist = TRUE;		/* alias on list */
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 		    } while (p = strtok((char *)NULL, Delims));
414*0Sstevel@tonic-gate 		    continue;
415*0Sstevel@tonic-gate 		}
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 		if (sameword(p, "Precedence"))
418*0Sstevel@tonic-gate 		{
419*0Sstevel@tonic-gate 			/* find the value of this field */
420*0Sstevel@tonic-gate 			p = strtok((char *)NULL, Delims);
421*0Sstevel@tonic-gate 			if (p == NULL)
422*0Sstevel@tonic-gate 				continue;
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate 			/* see if it is "junk" or "bulk" */
425*0Sstevel@tonic-gate 			p[4] = '\0';
426*0Sstevel@tonic-gate 			if (sameword(p, "junk") || sameword(p, "bulk"))
427*0Sstevel@tonic-gate 				return (TRUE);
428*0Sstevel@tonic-gate 		}
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 		if (sameword(p, "Subject"))
431*0Sstevel@tonic-gate 		{
432*0Sstevel@tonic-gate 			char *decoded_subject;
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 			Subject = newstr(buf+9);
435*0Sstevel@tonic-gate 			if (p = strrchr(Subject, '\n'))
436*0Sstevel@tonic-gate 				*p = '\0';
437*0Sstevel@tonic-gate 			EncodedSubject = newstr(Subject);
438*0Sstevel@tonic-gate 			decoded_subject = newstr(Subject);
439*0Sstevel@tonic-gate 			if (decode_rfc2047(Subject, decoded_subject, Charset))
440*0Sstevel@tonic-gate 				Subject = decoded_subject;
441*0Sstevel@tonic-gate 			else
442*0Sstevel@tonic-gate 				Charset[0] = '\0';
443*0Sstevel@tonic-gate 			if (Debug)
444*0Sstevel@tonic-gate 				printf("Subject=%s\n", Subject);
445*0Sstevel@tonic-gate 		}
446*0Sstevel@tonic-gate 	}
447*0Sstevel@tonic-gate 	if (AnswerAll)
448*0Sstevel@tonic-gate 		return (FALSE);
449*0Sstevel@tonic-gate 	else
450*0Sstevel@tonic-gate 		return (!onlist);
451*0Sstevel@tonic-gate }
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate /*
454*0Sstevel@tonic-gate  *  FILTER_OK -- see if the Return-Path is in the filter file.
455*0Sstevel@tonic-gate  *		 Note that a non-existent filter file means everything
456*0Sstevel@tonic-gate  *		 is OK, but an empty file means nothing is OK.
457*0Sstevel@tonic-gate  *
458*0Sstevel@tonic-gate  *	Parameters:
459*0Sstevel@tonic-gate  *		from -- the Return-Path of the sender.
460*0Sstevel@tonic-gate  *
461*0Sstevel@tonic-gate  *	Returns:
462*0Sstevel@tonic-gate  *		TRUE -- if this is in the filter file
463*0Sstevel@tonic-gate  *			(sender should receive a response).
464*0Sstevel@tonic-gate  *		FALSE -- if the sender does not deserve a response.
465*0Sstevel@tonic-gate  */
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate static bool
468*0Sstevel@tonic-gate filter_ok(from, filter_file)
469*0Sstevel@tonic-gate 	char *from;
470*0Sstevel@tonic-gate 	char *filter_file;
471*0Sstevel@tonic-gate {
472*0Sstevel@tonic-gate 	char file[MAXLINE];
473*0Sstevel@tonic-gate 	char line[MAXLINE];
474*0Sstevel@tonic-gate 	size_t line_len, from_len;
475*0Sstevel@tonic-gate 	bool result = FALSE;
476*0Sstevel@tonic-gate 	FILE *f;
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate 	from_len = strlen(from);
479*0Sstevel@tonic-gate 	(void) strlcpy(file, homedir, sizeof (file));
480*0Sstevel@tonic-gate 	if (filter_file[0] != '/')
481*0Sstevel@tonic-gate 	    (void) strlcat(file, "/", sizeof (file));
482*0Sstevel@tonic-gate 	(void) strlcat(file, filter_file, sizeof (file));
483*0Sstevel@tonic-gate 	f = fopen(file, "r");
484*0Sstevel@tonic-gate 	if (f == NULL) {
485*0Sstevel@tonic-gate 		/*
486*0Sstevel@tonic-gate 		 * If the file does not exist, then there is no filter to
487*0Sstevel@tonic-gate 		 * apply, so we simply return TRUE.
488*0Sstevel@tonic-gate 		 */
489*0Sstevel@tonic-gate 		if (Debug)
490*0Sstevel@tonic-gate 			(void) printf("%s does not exist, filter ok.\n",
491*0Sstevel@tonic-gate 			    file);
492*0Sstevel@tonic-gate 		return (TRUE);
493*0Sstevel@tonic-gate 	}
494*0Sstevel@tonic-gate 	while (fgets(line, MAXLINE, f)) {
495*0Sstevel@tonic-gate 		line_len = strlen(line);
496*0Sstevel@tonic-gate 		/* zero out trailing newline */
497*0Sstevel@tonic-gate 		if (line[line_len - 1] == '\n')
498*0Sstevel@tonic-gate 			line[--line_len] = '\0';
499*0Sstevel@tonic-gate 		/* skip blank lines */
500*0Sstevel@tonic-gate 		if (line_len == 0)
501*0Sstevel@tonic-gate 			continue;
502*0Sstevel@tonic-gate 		/* skip comment lines */
503*0Sstevel@tonic-gate 		if (line[0] == '#')
504*0Sstevel@tonic-gate 			continue;
505*0Sstevel@tonic-gate 		if (strchr(line, '@') != NULL) {
506*0Sstevel@tonic-gate 			/* @ => full address */
507*0Sstevel@tonic-gate 			if (strcasecmp(line, from) == 0) {
508*0Sstevel@tonic-gate 				result = TRUE;
509*0Sstevel@tonic-gate 				if (Debug)
510*0Sstevel@tonic-gate 					(void) printf("filter match on %s\n",
511*0Sstevel@tonic-gate 					    line);
512*0Sstevel@tonic-gate 				break;
513*0Sstevel@tonic-gate 			}
514*0Sstevel@tonic-gate 		} else {
515*0Sstevel@tonic-gate 			/* no @ => domain */
516*0Sstevel@tonic-gate 			if (from_len <= line_len)
517*0Sstevel@tonic-gate 				continue;
518*0Sstevel@tonic-gate 			/*
519*0Sstevel@tonic-gate 			 * Make sure the last part of from is the domain line
520*0Sstevel@tonic-gate 			 * and that the character immediately preceding is an
521*0Sstevel@tonic-gate 			 * '@' or a '.', otherwise we could get false positives
522*0Sstevel@tonic-gate 			 * from e.g. twinsun.com for sun.com .
523*0Sstevel@tonic-gate 			 */
524*0Sstevel@tonic-gate 			if (strncasecmp(&from[from_len - line_len], line,
525*0Sstevel@tonic-gate 			    line_len) == 0 &&
526*0Sstevel@tonic-gate 			    (from[from_len - line_len -1] == '@' ||
527*0Sstevel@tonic-gate 			    from[from_len - line_len -1] == '.')) {
528*0Sstevel@tonic-gate 				result = TRUE;
529*0Sstevel@tonic-gate 				if (Debug)
530*0Sstevel@tonic-gate 					(void) printf("filter match on %s\n",
531*0Sstevel@tonic-gate 					    line);
532*0Sstevel@tonic-gate 				break;
533*0Sstevel@tonic-gate 			}
534*0Sstevel@tonic-gate 		}
535*0Sstevel@tonic-gate 	}
536*0Sstevel@tonic-gate 	(void) fclose(f);
537*0Sstevel@tonic-gate 	if (Debug && !result)
538*0Sstevel@tonic-gate 		(void) printf("no filter match\n");
539*0Sstevel@tonic-gate 	return (result);
540*0Sstevel@tonic-gate }
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate /*
543*0Sstevel@tonic-gate  *  KNOWS -- predicate telling if user has already been informed.
544*0Sstevel@tonic-gate  *
545*0Sstevel@tonic-gate  *	Parameters:
546*0Sstevel@tonic-gate  *		user -- the user who sent this message.
547*0Sstevel@tonic-gate  *
548*0Sstevel@tonic-gate  *	Returns:
549*0Sstevel@tonic-gate  *		TRUE if 'user' has already been informed that the
550*0Sstevel@tonic-gate  *			recipient is on vacation.
551*0Sstevel@tonic-gate  *		FALSE otherwise.
552*0Sstevel@tonic-gate  *
553*0Sstevel@tonic-gate  *	Side Effects:
554*0Sstevel@tonic-gate  *		none.
555*0Sstevel@tonic-gate  */
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate static bool
558*0Sstevel@tonic-gate knows(user)
559*0Sstevel@tonic-gate 	char *user;
560*0Sstevel@tonic-gate {
561*0Sstevel@tonic-gate 	datum key, data;
562*0Sstevel@tonic-gate 	time_t now, then;
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 	(void) time(&now);
565*0Sstevel@tonic-gate 	key.dptr = user;
566*0Sstevel@tonic-gate 	key.dsize = strlen(user) + 1;
567*0Sstevel@tonic-gate 	data = dbm_fetch(db, key);
568*0Sstevel@tonic-gate 	if (data.dptr == NULL)
569*0Sstevel@tonic-gate 		return (FALSE);
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate 	bcopy(data.dptr, (char *)&then, sizeof (then));
572*0Sstevel@tonic-gate 	if (then + Timeout < now)
573*0Sstevel@tonic-gate 		return (FALSE);
574*0Sstevel@tonic-gate 	if (Debug)
575*0Sstevel@tonic-gate 		printf("User %s already knows\n", user);
576*0Sstevel@tonic-gate 	return (TRUE);
577*0Sstevel@tonic-gate }
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate /*
580*0Sstevel@tonic-gate  *  SETKNOWS -- set that this user knows about the vacation.
581*0Sstevel@tonic-gate  *
582*0Sstevel@tonic-gate  *	Parameters:
583*0Sstevel@tonic-gate  *		user -- the user who should be marked.
584*0Sstevel@tonic-gate  *
585*0Sstevel@tonic-gate  *	Returns:
586*0Sstevel@tonic-gate  *		none.
587*0Sstevel@tonic-gate  *
588*0Sstevel@tonic-gate  *	Side Effects:
589*0Sstevel@tonic-gate  *		The dbm file is updated as appropriate.
590*0Sstevel@tonic-gate  */
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate static void
593*0Sstevel@tonic-gate setknows(user)
594*0Sstevel@tonic-gate 	char *user;
595*0Sstevel@tonic-gate {
596*0Sstevel@tonic-gate 	datum key, data;
597*0Sstevel@tonic-gate 	time_t now;
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	key.dptr = user;
600*0Sstevel@tonic-gate 	key.dsize = strlen(user) + 1;
601*0Sstevel@tonic-gate 	(void) time(&now);
602*0Sstevel@tonic-gate 	data.dptr = (char *)&now;
603*0Sstevel@tonic-gate 	data.dsize = sizeof (now);
604*0Sstevel@tonic-gate 	dbm_store(db, key, data, DBM_REPLACE);
605*0Sstevel@tonic-gate }
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate static bool
608*0Sstevel@tonic-gate any8bitchars(line)
609*0Sstevel@tonic-gate 	char *line;
610*0Sstevel@tonic-gate {
611*0Sstevel@tonic-gate 	char *c;
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 	for (c = line; *c; c++)
614*0Sstevel@tonic-gate 		if (*c & 0x80)
615*0Sstevel@tonic-gate 			return (TRUE);
616*0Sstevel@tonic-gate 	return (FALSE);
617*0Sstevel@tonic-gate }
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate /*
620*0Sstevel@tonic-gate  *  SENDMESSAGE -- send a message to a particular user.
621*0Sstevel@tonic-gate  *
622*0Sstevel@tonic-gate  *	Parameters:
623*0Sstevel@tonic-gate  *		msgf -- filename containing the message.
624*0Sstevel@tonic-gate  *		user -- user who should receive it.
625*0Sstevel@tonic-gate  *
626*0Sstevel@tonic-gate  *	Returns:
627*0Sstevel@tonic-gate  *		none.
628*0Sstevel@tonic-gate  *
629*0Sstevel@tonic-gate  *	Side Effects:
630*0Sstevel@tonic-gate  *		sends mail to 'user' using /usr/lib/sendmail.
631*0Sstevel@tonic-gate  */
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate static void
634*0Sstevel@tonic-gate sendmessage(msgf, user, myname)
635*0Sstevel@tonic-gate 	char *msgf;
636*0Sstevel@tonic-gate 	char *user;
637*0Sstevel@tonic-gate 	char *myname;
638*0Sstevel@tonic-gate {
639*0Sstevel@tonic-gate 	FILE *f, *fpipe, *tmpf;
640*0Sstevel@tonic-gate 	char line[MAXLINE];
641*0Sstevel@tonic-gate 	char *p, *tmpf_name;
642*0Sstevel@tonic-gate 	int i, pipefd[2], tmpfd;
643*0Sstevel@tonic-gate 	bool seen8bitchars = FALSE;
644*0Sstevel@tonic-gate 	bool in_header = TRUE;
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 	/* find the message to send */
647*0Sstevel@tonic-gate 	f = fopen(msgf, "r");
648*0Sstevel@tonic-gate 	if (f == NULL)
649*0Sstevel@tonic-gate 	{
650*0Sstevel@tonic-gate 		f = fopen("/etc/mail/vacation.def", "r");
651*0Sstevel@tonic-gate 		if (f == NULL)
652*0Sstevel@tonic-gate 			usrerr("No message to send");
653*0Sstevel@tonic-gate 			exit(EX_OSFILE);
654*0Sstevel@tonic-gate 	}
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate 	if (pipe(pipefd) < 0) {
657*0Sstevel@tonic-gate 		usrerr("pipe() failed");
658*0Sstevel@tonic-gate 		exit(EX_OSERR);
659*0Sstevel@tonic-gate 	}
660*0Sstevel@tonic-gate 	i = fork();
661*0Sstevel@tonic-gate 	if (i < 0) {
662*0Sstevel@tonic-gate 		usrerr("fork() failed");
663*0Sstevel@tonic-gate 		exit(EX_OSERR);
664*0Sstevel@tonic-gate 	}
665*0Sstevel@tonic-gate 	if (i == 0) {
666*0Sstevel@tonic-gate 		dup2(pipefd[0], 0);
667*0Sstevel@tonic-gate 		close(pipefd[0]);
668*0Sstevel@tonic-gate 		close(pipefd[1]);
669*0Sstevel@tonic-gate 		fclose(f);
670*0Sstevel@tonic-gate 		execl("/usr/lib/sendmail", "sendmail", "-eq", "-f", myname,
671*0Sstevel@tonic-gate 			"--", user, NULL);
672*0Sstevel@tonic-gate 		usrerr("can't exec /usr/lib/sendmail");
673*0Sstevel@tonic-gate 		exit(EX_OSERR);
674*0Sstevel@tonic-gate 	}
675*0Sstevel@tonic-gate 	close(pipefd[0]);
676*0Sstevel@tonic-gate 	fpipe = fdopen(pipefd[1], "w");
677*0Sstevel@tonic-gate 	if (fpipe == NULL) {
678*0Sstevel@tonic-gate 		usrerr("fdopen() failed");
679*0Sstevel@tonic-gate 		exit(EX_OSERR);
680*0Sstevel@tonic-gate 	}
681*0Sstevel@tonic-gate 	fprintf(fpipe, "To: %s\n", user);
682*0Sstevel@tonic-gate 	fputs("Auto-Submitted: auto-replied\n", fpipe);
683*0Sstevel@tonic-gate 	fputs("X-Mailer: vacation %I%\n", fpipe);
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 	/*
686*0Sstevel@tonic-gate 	 * We used to write directly to the pipe.  But now we need to know
687*0Sstevel@tonic-gate 	 * what character set to use, and we need to examine the entire
688*0Sstevel@tonic-gate 	 * message to determine this.  So write to a temp file first.
689*0Sstevel@tonic-gate 	 */
690*0Sstevel@tonic-gate 	tmpf_name = strdup(_PATH_TMP);
691*0Sstevel@tonic-gate 	if (tmpf_name == NULL) {
692*0Sstevel@tonic-gate 		usrerr("newstr: cannot alloc memory");
693*0Sstevel@tonic-gate 		exit(EX_OSERR);
694*0Sstevel@tonic-gate 	}
695*0Sstevel@tonic-gate 	tmpfd = -1;
696*0Sstevel@tonic-gate 	tmpfd = mkstemp(tmpf_name);
697*0Sstevel@tonic-gate 	if (tmpfd == -1) {
698*0Sstevel@tonic-gate 		usrerr("can't open temp file %s", tmpf_name);
699*0Sstevel@tonic-gate 		exit(EX_OSERR);
700*0Sstevel@tonic-gate 	}
701*0Sstevel@tonic-gate 	tmpf = fdopen(tmpfd, "w");
702*0Sstevel@tonic-gate 	if (tmpf == NULL) {
703*0Sstevel@tonic-gate 		usrerr("can't open temp file %s", tmpf_name);
704*0Sstevel@tonic-gate 		exit(EX_OSERR);
705*0Sstevel@tonic-gate 	}
706*0Sstevel@tonic-gate 	while (fgets(line, MAXLINE, f)) {
707*0Sstevel@tonic-gate 		/*
708*0Sstevel@tonic-gate 		 * Check for a line with no ':' character.  If it's just \n,
709*0Sstevel@tonic-gate 		 * we're at the end of the headers and all is fine.  Or if
710*0Sstevel@tonic-gate 		 * it starts with white-space, then it's a continuation header.
711*0Sstevel@tonic-gate 		 * Otherwise, it's the start of the body, which means the
712*0Sstevel@tonic-gate 		 * header/body separator was skipped.  So output it.
713*0Sstevel@tonic-gate 		 */
714*0Sstevel@tonic-gate 		if (in_header && line[0] != '\0' && strchr(line, ':') == NULL) {
715*0Sstevel@tonic-gate 			if (line[0] == '\n')
716*0Sstevel@tonic-gate 				in_header = FALSE;
717*0Sstevel@tonic-gate 			else if (!isspace(line[0])) {
718*0Sstevel@tonic-gate 				in_header = FALSE;
719*0Sstevel@tonic-gate 				fputs("\n", tmpf);
720*0Sstevel@tonic-gate 			}
721*0Sstevel@tonic-gate 		}
722*0Sstevel@tonic-gate 		p = strchr(line, '$');
723*0Sstevel@tonic-gate 		if (p && strncmp(p, "$SUBJECT", 8) == 0) {
724*0Sstevel@tonic-gate 			*p = '\0';
725*0Sstevel@tonic-gate 			seen8bitchars |= any8bitchars(line);
726*0Sstevel@tonic-gate 			fputs(line, tmpf);
727*0Sstevel@tonic-gate 			if (Subject) {
728*0Sstevel@tonic-gate 				if (in_header)
729*0Sstevel@tonic-gate 					fputs(EncodedSubject, tmpf);
730*0Sstevel@tonic-gate 				else {
731*0Sstevel@tonic-gate 					seen8bitchars |= any8bitchars(Subject);
732*0Sstevel@tonic-gate 					fputs(Subject, tmpf);
733*0Sstevel@tonic-gate 				}
734*0Sstevel@tonic-gate 			}
735*0Sstevel@tonic-gate 			seen8bitchars |= any8bitchars(p+8);
736*0Sstevel@tonic-gate 			fputs(p+8, tmpf);
737*0Sstevel@tonic-gate 			continue;
738*0Sstevel@tonic-gate 		}
739*0Sstevel@tonic-gate 		seen8bitchars |= any8bitchars(line);
740*0Sstevel@tonic-gate 		fputs(line, tmpf);
741*0Sstevel@tonic-gate 	}
742*0Sstevel@tonic-gate 	fclose(f);
743*0Sstevel@tonic-gate 	fclose(tmpf);
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	/*
746*0Sstevel@tonic-gate 	 * If we haven't seen a funky Subject with Charset, use the default.
747*0Sstevel@tonic-gate 	 * If we have and it's us-ascii, 8-bit chars in the message file will
748*0Sstevel@tonic-gate 	 * still result in iso-8859-1.
749*0Sstevel@tonic-gate 	 */
750*0Sstevel@tonic-gate 	if (Charset[0] == '\0')
751*0Sstevel@tonic-gate 		(void) strlcpy(Charset, (seen8bitchars) ? "iso-8859-1" :
752*0Sstevel@tonic-gate 		    "us-ascii", sizeof (Charset));
753*0Sstevel@tonic-gate 	else if ((strcasecmp(Charset, "us-ascii") == 0) && seen8bitchars)
754*0Sstevel@tonic-gate 		(void) strlcpy(Charset, "iso-8859-1", sizeof (Charset));
755*0Sstevel@tonic-gate 	if (Debug)
756*0Sstevel@tonic-gate 		printf("Charset is %s\n", Charset);
757*0Sstevel@tonic-gate 	fprintf(fpipe, "Content-Type: text/plain; charset=%s\n", Charset);
758*0Sstevel@tonic-gate 	fputs("Mime-Version: 1.0\n", fpipe);
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate 	/*
761*0Sstevel@tonic-gate 	 * Now read back in from the temp file and write to the pipe.
762*0Sstevel@tonic-gate 	 */
763*0Sstevel@tonic-gate 	tmpf = fopen(tmpf_name, "r");
764*0Sstevel@tonic-gate 	if (tmpf == NULL) {
765*0Sstevel@tonic-gate 		usrerr("can't open temp file %s", tmpf_name);
766*0Sstevel@tonic-gate 		exit(EX_OSERR);
767*0Sstevel@tonic-gate 	}
768*0Sstevel@tonic-gate 	while (fgets(line, MAXLINE, tmpf))
769*0Sstevel@tonic-gate 		fputs(line, fpipe);
770*0Sstevel@tonic-gate 	fclose(fpipe);
771*0Sstevel@tonic-gate 	fclose(tmpf);
772*0Sstevel@tonic-gate 	(void) unlink(tmpf_name);
773*0Sstevel@tonic-gate 	free(tmpf_name);
774*0Sstevel@tonic-gate }
775*0Sstevel@tonic-gate 
776*0Sstevel@tonic-gate /*
777*0Sstevel@tonic-gate  *  INITIALIZE -- initialize the database before leaving for vacation
778*0Sstevel@tonic-gate  *
779*0Sstevel@tonic-gate  *	Parameters:
780*0Sstevel@tonic-gate  *		none.
781*0Sstevel@tonic-gate  *
782*0Sstevel@tonic-gate  *	Returns:
783*0Sstevel@tonic-gate  *		none.
784*0Sstevel@tonic-gate  *
785*0Sstevel@tonic-gate  *	Side Effects:
786*0Sstevel@tonic-gate  *		Initializes the files .vacation.{pag,dir} in the
787*0Sstevel@tonic-gate  *		caller's home directory.
788*0Sstevel@tonic-gate  */
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate static void
791*0Sstevel@tonic-gate initialize(db_file_base)
792*0Sstevel@tonic-gate 	char *db_file_base;
793*0Sstevel@tonic-gate {
794*0Sstevel@tonic-gate 	char *homedir;
795*0Sstevel@tonic-gate 	char buf[MAXLINE];
796*0Sstevel@tonic-gate 	DBM *db;
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	setgid(getgid());
799*0Sstevel@tonic-gate 	setuid(getuid());
800*0Sstevel@tonic-gate 	homedir = getenv("HOME");
801*0Sstevel@tonic-gate 	if (homedir == NULL) {
802*0Sstevel@tonic-gate 		usrerr("No home!");
803*0Sstevel@tonic-gate 		exit(EX_NOUSER);
804*0Sstevel@tonic-gate 	}
805*0Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s%s%s", homedir,
806*0Sstevel@tonic-gate 		(db_file_base[0] == '/') ? "" : "/", db_file_base);
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 	if (!(db = dbm_open(buf, O_WRONLY|O_CREAT|O_TRUNC, 0644))) {
809*0Sstevel@tonic-gate 		usrerr("%s: %s\n", buf, strerror(errno));
810*0Sstevel@tonic-gate 		exit(EX_DATAERR);
811*0Sstevel@tonic-gate 	}
812*0Sstevel@tonic-gate 	dbm_close(db);
813*0Sstevel@tonic-gate }
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate /*
816*0Sstevel@tonic-gate  *  USRERR -- print user error
817*0Sstevel@tonic-gate  *
818*0Sstevel@tonic-gate  *	Parameters:
819*0Sstevel@tonic-gate  *		f -- format.
820*0Sstevel@tonic-gate  *
821*0Sstevel@tonic-gate  *	Returns:
822*0Sstevel@tonic-gate  *		none.
823*0Sstevel@tonic-gate  *
824*0Sstevel@tonic-gate  *	Side Effects:
825*0Sstevel@tonic-gate  *		none.
826*0Sstevel@tonic-gate  */
827*0Sstevel@tonic-gate 
828*0Sstevel@tonic-gate /* PRINTFLIKE1 */
829*0Sstevel@tonic-gate void
830*0Sstevel@tonic-gate usrerr(const char *f, ...)
831*0Sstevel@tonic-gate {
832*0Sstevel@tonic-gate 	va_list alist;
833*0Sstevel@tonic-gate 
834*0Sstevel@tonic-gate 	va_start(alist, f);
835*0Sstevel@tonic-gate 	(void) fprintf(stderr, "vacation: ");
836*0Sstevel@tonic-gate 	(void) vfprintf(stderr, f, alist);
837*0Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
838*0Sstevel@tonic-gate 	va_end(alist);
839*0Sstevel@tonic-gate }
840*0Sstevel@tonic-gate 
841*0Sstevel@tonic-gate /*
842*0Sstevel@tonic-gate  *  NEWSTR -- copy a string
843*0Sstevel@tonic-gate  *
844*0Sstevel@tonic-gate  *	Parameters:
845*0Sstevel@tonic-gate  *		s -- the string to copy.
846*0Sstevel@tonic-gate  *
847*0Sstevel@tonic-gate  *	Returns:
848*0Sstevel@tonic-gate  *		A copy of the string.
849*0Sstevel@tonic-gate  *
850*0Sstevel@tonic-gate  *	Side Effects:
851*0Sstevel@tonic-gate  *		none.
852*0Sstevel@tonic-gate  */
853*0Sstevel@tonic-gate 
854*0Sstevel@tonic-gate static char *
855*0Sstevel@tonic-gate newstr(s)
856*0Sstevel@tonic-gate 	char *s;
857*0Sstevel@tonic-gate {
858*0Sstevel@tonic-gate 	char *p;
859*0Sstevel@tonic-gate 	size_t s_sz = strlen(s);
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 	p = malloc(s_sz + 1);
862*0Sstevel@tonic-gate 	if (p == NULL)
863*0Sstevel@tonic-gate 	{
864*0Sstevel@tonic-gate 		usrerr("newstr: cannot alloc memory");
865*0Sstevel@tonic-gate 		exit(EX_OSERR);
866*0Sstevel@tonic-gate 	}
867*0Sstevel@tonic-gate 	(void) strlcpy(p, s, s_sz + 1);
868*0Sstevel@tonic-gate 	return (p);
869*0Sstevel@tonic-gate }
870*0Sstevel@tonic-gate 
871*0Sstevel@tonic-gate /*
872*0Sstevel@tonic-gate  *  SAMEWORD -- return TRUE if the words are the same
873*0Sstevel@tonic-gate  *
874*0Sstevel@tonic-gate  *	Ignores case.
875*0Sstevel@tonic-gate  *
876*0Sstevel@tonic-gate  *	Parameters:
877*0Sstevel@tonic-gate  *		a, b -- the words to compare.
878*0Sstevel@tonic-gate  *
879*0Sstevel@tonic-gate  *	Returns:
880*0Sstevel@tonic-gate  *		TRUE if a & b match exactly (modulo case)
881*0Sstevel@tonic-gate  *		FALSE otherwise.
882*0Sstevel@tonic-gate  *
883*0Sstevel@tonic-gate  *	Side Effects:
884*0Sstevel@tonic-gate  *		none.
885*0Sstevel@tonic-gate  */
886*0Sstevel@tonic-gate 
887*0Sstevel@tonic-gate static bool
888*0Sstevel@tonic-gate sameword(a, b)
889*0Sstevel@tonic-gate 	register char *a, *b;
890*0Sstevel@tonic-gate {
891*0Sstevel@tonic-gate 	char ca, cb;
892*0Sstevel@tonic-gate 
893*0Sstevel@tonic-gate 	do
894*0Sstevel@tonic-gate 	{
895*0Sstevel@tonic-gate 		ca = *a++;
896*0Sstevel@tonic-gate 		cb = *b++;
897*0Sstevel@tonic-gate 		if (isascii(ca) && isupper(ca))
898*0Sstevel@tonic-gate 			ca = ca - 'A' + 'a';
899*0Sstevel@tonic-gate 		if (isascii(cb) && isupper(cb))
900*0Sstevel@tonic-gate 			cb = cb - 'A' + 'a';
901*0Sstevel@tonic-gate 	} while (ca != '\0' && ca == cb);
902*0Sstevel@tonic-gate 	return (ca == cb);
903*0Sstevel@tonic-gate }
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate /*
906*0Sstevel@tonic-gate  * When invoked with no arguments, we fall into an automatic installation
907*0Sstevel@tonic-gate  * mode, stepping the user through a default installation.
908*0Sstevel@tonic-gate  */
909*0Sstevel@tonic-gate 
910*0Sstevel@tonic-gate static void
911*0Sstevel@tonic-gate AutoInstall()
912*0Sstevel@tonic-gate {
913*0Sstevel@tonic-gate 	char file[MAXLINE];
914*0Sstevel@tonic-gate 	char forward[MAXLINE];
915*0Sstevel@tonic-gate 	char cmd[MAXLINE];
916*0Sstevel@tonic-gate 	char line[MAXLINE];
917*0Sstevel@tonic-gate 	char *editor;
918*0Sstevel@tonic-gate 	FILE *f;
919*0Sstevel@tonic-gate 	struct passwd *pw;
920*0Sstevel@tonic-gate 	extern mode_t umask(mode_t cmask);
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate 	umask(022);
923*0Sstevel@tonic-gate 	pw = getpwuid(getuid());
924*0Sstevel@tonic-gate 	if (pw == NULL) {
925*0Sstevel@tonic-gate 		usrerr("User ID unknown");
926*0Sstevel@tonic-gate 		exit(EX_NOUSER);
927*0Sstevel@tonic-gate 	}
928*0Sstevel@tonic-gate 	myname = strdup(pw->pw_name);
929*0Sstevel@tonic-gate 	if (myname == NULL) {
930*0Sstevel@tonic-gate 		usrerr("Out of memory");
931*0Sstevel@tonic-gate 		exit(EX_OSERR);
932*0Sstevel@tonic-gate 	}
933*0Sstevel@tonic-gate 	homedir = getenv("HOME");
934*0Sstevel@tonic-gate 	if (homedir == NULL) {
935*0Sstevel@tonic-gate 		usrerr("Home directory unknown");
936*0Sstevel@tonic-gate 		exit(EX_NOUSER);
937*0Sstevel@tonic-gate 	}
938*0Sstevel@tonic-gate 
939*0Sstevel@tonic-gate 	printf("This program can be used to answer your mail automatically\n");
940*0Sstevel@tonic-gate 	printf("when you go away on vacation.\n");
941*0Sstevel@tonic-gate 	(void) strlcpy(file, homedir, sizeof (file));
942*0Sstevel@tonic-gate 	(void) strlcat(file, MsgFile, sizeof (file));
943*0Sstevel@tonic-gate 	do {
944*0Sstevel@tonic-gate 		f = fopen(file, "r");
945*0Sstevel@tonic-gate 		if (f) {
946*0Sstevel@tonic-gate 			printf("You have a message file in %s.\n", file);
947*0Sstevel@tonic-gate 			if (ask("Would you like to see it")) {
948*0Sstevel@tonic-gate 				(void) snprintf(cmd, sizeof (cmd),
949*0Sstevel@tonic-gate 				    "/usr/bin/more %s", file);
950*0Sstevel@tonic-gate 				system(cmd);
951*0Sstevel@tonic-gate 			}
952*0Sstevel@tonic-gate 			if (ask("Would you like to edit it"))
953*0Sstevel@tonic-gate 				f = NULL;
954*0Sstevel@tonic-gate 		} else {
955*0Sstevel@tonic-gate 			printf("You need to create a message file"
956*0Sstevel@tonic-gate 				" in %s first.\n", file);
957*0Sstevel@tonic-gate 			f = fopen(file, "w");
958*0Sstevel@tonic-gate 			if (f == NULL) {
959*0Sstevel@tonic-gate 				usrerr("Cannot open %s", file);
960*0Sstevel@tonic-gate 				exit(EX_CANTCREAT);
961*0Sstevel@tonic-gate 			}
962*0Sstevel@tonic-gate 			fprintf(f, "Subject: away from my mail\n");
963*0Sstevel@tonic-gate 			fprintf(f, "\nI will not be reading my mail"
964*0Sstevel@tonic-gate 				" for a while.\n");
965*0Sstevel@tonic-gate 			fprintf(f, "Your mail regarding \"$SUBJECT\" will"
966*0Sstevel@tonic-gate 				" be read when I return.\n");
967*0Sstevel@tonic-gate 			fclose(f);
968*0Sstevel@tonic-gate 			f = NULL;
969*0Sstevel@tonic-gate 		}
970*0Sstevel@tonic-gate 		if (f == NULL) {
971*0Sstevel@tonic-gate 			editor = getenv("VISUAL");
972*0Sstevel@tonic-gate 			if (editor == NULL)
973*0Sstevel@tonic-gate 				editor = getenv("EDITOR");
974*0Sstevel@tonic-gate 			if (editor == NULL)
975*0Sstevel@tonic-gate 				editor = "/usr/bin/vi";
976*0Sstevel@tonic-gate 			(void) snprintf(cmd, sizeof (cmd), "%s %s", editor,
977*0Sstevel@tonic-gate 			    file);
978*0Sstevel@tonic-gate 			printf("Please use your editor (%s)"
979*0Sstevel@tonic-gate 				" to edit this file.\n", editor);
980*0Sstevel@tonic-gate 			system(cmd);
981*0Sstevel@tonic-gate 		}
982*0Sstevel@tonic-gate 	} while (f == NULL);
983*0Sstevel@tonic-gate 	fclose(f);
984*0Sstevel@tonic-gate 	(void) strlcpy(forward, homedir, sizeof (forward));
985*0Sstevel@tonic-gate 	(void) strlcat(forward, "/.forward", sizeof (forward));
986*0Sstevel@tonic-gate 	f = fopen(forward, "r");
987*0Sstevel@tonic-gate 	if (f) {
988*0Sstevel@tonic-gate 		printf("You have a .forward file"
989*0Sstevel@tonic-gate 			" in your home directory containing:\n");
990*0Sstevel@tonic-gate 		while (fgets(line, MAXLINE, f))
991*0Sstevel@tonic-gate 			printf("    %s", line);
992*0Sstevel@tonic-gate 		fclose(f);
993*0Sstevel@tonic-gate 		if (!ask("Would you like to remove it and"
994*0Sstevel@tonic-gate 				" disable the vacation feature"))
995*0Sstevel@tonic-gate 			exit(EX_OK);
996*0Sstevel@tonic-gate 		if (unlink(forward))
997*0Sstevel@tonic-gate 			perror("Error removing .forward file:");
998*0Sstevel@tonic-gate 		else
999*0Sstevel@tonic-gate 			printf("Back to normal reception of mail.\n");
1000*0Sstevel@tonic-gate 		exit(EX_OK);
1001*0Sstevel@tonic-gate 	}
1002*0Sstevel@tonic-gate 
1003*0Sstevel@tonic-gate 	printf("To enable the vacation feature"
1004*0Sstevel@tonic-gate 		" a \".forward\" file is created.\n");
1005*0Sstevel@tonic-gate 	if (!ask("Would you like to enable the vacation feature")) {
1006*0Sstevel@tonic-gate 		printf("OK, vacation feature NOT enabled.\n");
1007*0Sstevel@tonic-gate 		exit(EX_OK);
1008*0Sstevel@tonic-gate 	}
1009*0Sstevel@tonic-gate 	f = fopen(forward, "w");
1010*0Sstevel@tonic-gate 	if (f == NULL) {
1011*0Sstevel@tonic-gate 		perror("Error opening .forward file");
1012*0Sstevel@tonic-gate 		exit(EX_CANTCREAT);
1013*0Sstevel@tonic-gate 	}
1014*0Sstevel@tonic-gate 	fprintf(f, "\\%s, \"|/usr/bin/vacation %s\"\n", myname, myname);
1015*0Sstevel@tonic-gate 	fclose(f);
1016*0Sstevel@tonic-gate 	printf("Vacation feature ENABLED."
1017*0Sstevel@tonic-gate 		" Please remember to turn it off when\n");
1018*0Sstevel@tonic-gate 	printf("you get back from vacation. Bon voyage.\n");
1019*0Sstevel@tonic-gate 
1020*0Sstevel@tonic-gate 	initialize(DbFileBase);
1021*0Sstevel@tonic-gate 	exit(EX_OK);
1022*0Sstevel@tonic-gate }
1023*0Sstevel@tonic-gate 
1024*0Sstevel@tonic-gate 
1025*0Sstevel@tonic-gate /*
1026*0Sstevel@tonic-gate  * Ask the user a question until we get a reasonable answer
1027*0Sstevel@tonic-gate  */
1028*0Sstevel@tonic-gate 
1029*0Sstevel@tonic-gate static bool
1030*0Sstevel@tonic-gate ask(prompt)
1031*0Sstevel@tonic-gate 	char *prompt;
1032*0Sstevel@tonic-gate {
1033*0Sstevel@tonic-gate 	char line[MAXLINE];
1034*0Sstevel@tonic-gate 	char *res;
1035*0Sstevel@tonic-gate 
1036*0Sstevel@tonic-gate 	for (;;) {
1037*0Sstevel@tonic-gate 		printf("%s? ", prompt);
1038*0Sstevel@tonic-gate 		fflush(stdout);
1039*0Sstevel@tonic-gate 		res = fgets(line, sizeof (line), stdin);
1040*0Sstevel@tonic-gate 		if (res == NULL)
1041*0Sstevel@tonic-gate 			return (FALSE);
1042*0Sstevel@tonic-gate 		if (res[0] == 'y' || res[0] == 'Y')
1043*0Sstevel@tonic-gate 			return (TRUE);
1044*0Sstevel@tonic-gate 		if (res[0] == 'n' || res[0] == 'N')
1045*0Sstevel@tonic-gate 			return (FALSE);
1046*0Sstevel@tonic-gate 		printf("Please reply \"yes\" or \"no\" (\'y\' or \'n\')\n");
1047*0Sstevel@tonic-gate 	}
1048*0Sstevel@tonic-gate }
1049