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