122710Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822710Sdist 
922710Sdist #ifndef lint
10*55012Seric static char sccsid[] = "@(#)recipient.c	5.33 (Berkeley) 07/12/92";
1133731Sbostic #endif /* not lint */
1222710Sdist 
1336928Sbostic # include <sys/types.h>
1436928Sbostic # include <sys/stat.h>
1552046Seric # include <sys/file.h>
164174Seric # include <pwd.h>
174627Seric # include "sendmail.h"
184174Seric 
194174Seric /*
209622Seric **  SENDTOLIST -- Designate a send list.
214174Seric **
224174Seric **	The parameter is a comma-separated list of people to send to.
234174Seric **	This routine arranges to send to all of them.
244174Seric **
254174Seric **	Parameters:
264174Seric **		list -- the send list.
274399Seric **		ctladdr -- the address template for the person to
284399Seric **			send to -- effective uid/gid are important.
295006Seric **			This is typically the alias that caused this
305006Seric **			expansion.
315006Seric **		sendq -- a pointer to the head of a queue to put
325006Seric **			these people into.
334174Seric **
344174Seric **	Returns:
354998Seric **		none
364174Seric **
374174Seric **	Side Effects:
384174Seric **		none.
394174Seric */
404174Seric 
414174Seric # define MAXRCRSN	10
424174Seric 
43*55012Seric sendtolist(list, ctladdr, sendq, e)
444174Seric 	char *list;
454399Seric 	ADDRESS *ctladdr;
465198Seric 	ADDRESS **sendq;
47*55012Seric 	register ENVELOPE *e;
484174Seric {
494174Seric 	register char *p;
508223Seric 	register ADDRESS *al;	/* list of addresses to send to */
514423Seric 	bool firstone;		/* set on first address sent */
524444Seric 	bool selfref;		/* set if this list includes ctladdr */
5311446Seric 	char delimiter;		/* the address delimiter */
544174Seric 
557676Seric 	if (tTd(25, 1))
564444Seric 	{
574444Seric 		printf("sendto: %s\n   ctladdr=", list);
584444Seric 		printaddr(ctladdr, FALSE);
594444Seric 	}
604324Seric 
618223Seric 	/* heuristic to determine old versus new style addresses */
628230Seric 	if (ctladdr == NULL &&
638230Seric 	    (index(list, ',') != NULL || index(list, ';') != NULL ||
648230Seric 	     index(list, '<') != NULL || index(list, '(') != NULL))
65*55012Seric 		e->e_flags &= ~EF_OLDSTYLE;
6611446Seric 	delimiter = ' ';
67*55012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
6811446Seric 		delimiter = ',';
698223Seric 
704423Seric 	firstone = TRUE;
714444Seric 	selfref = FALSE;
724324Seric 	al = NULL;
738223Seric 
748081Seric 	for (p = list; *p != '\0'; )
754174Seric 	{
768081Seric 		register ADDRESS *a;
778081Seric 		extern char *DelimChar;		/* defined in prescan */
784319Seric 
798081Seric 		/* parse the address */
808081Seric 		while (isspace(*p) || *p == ',')
814174Seric 			p++;
82*55012Seric 		a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter, e);
839297Seric 		p = DelimChar;
849297Seric 		if (a == NULL)
854174Seric 			continue;
864324Seric 		a->q_next = al;
874399Seric 		a->q_alias = ctladdr;
884444Seric 
894444Seric 		/* see if this should be marked as a primary address */
904423Seric 		if (ctladdr == NULL ||
918081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
924423Seric 			a->q_flags |= QPRIMARY;
934444Seric 
944444Seric 		/* put on send queue or suppress self-reference */
959379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
964444Seric 			selfref = TRUE;
974444Seric 		else
984444Seric 			al = a;
994423Seric 		firstone = FALSE;
1004324Seric 	}
1014324Seric 
1024444Seric 	/* if this alias doesn't include itself, delete ctladdr */
1034444Seric 	if (!selfref && ctladdr != NULL)
1044444Seric 		ctladdr->q_flags |= QDONTSEND;
1054444Seric 
1064324Seric 	/* arrange to send to everyone on the local send list */
1074324Seric 	while (al != NULL)
1084324Seric 	{
1094324Seric 		register ADDRESS *a = al;
11012613Seric 		extern ADDRESS *recipient();
1114324Seric 
1124324Seric 		al = a->q_next;
113*55012Seric 		a = recipient(a, sendq, e);
1144993Seric 
1154998Seric 		/* arrange to inherit full name */
1164998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1174998Seric 			a->q_fullname = ctladdr->q_fullname;
1184174Seric 	}
1194324Seric 
120*55012Seric 	e->e_to = NULL;
1214174Seric }
1224174Seric /*
1234174Seric **  RECIPIENT -- Designate a message recipient
1244174Seric **
1254174Seric **	Saves the named person for future mailing.
1264174Seric **
1274174Seric **	Parameters:
1284174Seric **		a -- the (preparsed) address header for the recipient.
1295006Seric **		sendq -- a pointer to the head of a queue to put the
1305006Seric **			recipient in.  Duplicate supression is done
1315006Seric **			in this queue.
1324174Seric **
1334174Seric **	Returns:
13412613Seric **		The actual address in the queue.  This will be "a" if
13512613Seric **		the address is not a duplicate, else the original address.
1364174Seric **
1374174Seric **	Side Effects:
1384174Seric **		none.
1394174Seric */
1404174Seric 
14146928Sbostic extern ADDRESS *getctladdr();
14252046Seric extern char	*RcptLogFile;
14346928Sbostic 
14412613Seric ADDRESS *
145*55012Seric recipient(a, sendq, e)
1464174Seric 	register ADDRESS *a;
1475006Seric 	register ADDRESS **sendq;
148*55012Seric 	register ENVELOPE *e;
1494174Seric {
1504174Seric 	register ADDRESS *q;
1514319Seric 	ADDRESS **pq;
1524174Seric 	register struct mailer *m;
1539210Seric 	register char *p;
1549210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
15553735Seric 	int findusercount = 0;
1569210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
1574627Seric 	extern bool safefile();
1584174Seric 
159*55012Seric 	e->e_to = a->q_paddr;
1604600Seric 	m = a->q_mailer;
1614174Seric 	errno = 0;
1627676Seric 	if (tTd(26, 1))
1634444Seric 	{
1644444Seric 		printf("\nrecipient: ");
1654444Seric 		printaddr(a, FALSE);
1664444Seric 	}
1674174Seric 
1684174Seric 	/* break aliasing loops */
1694174Seric 	if (AliasLevel > MAXRCRSN)
1704174Seric 	{
1714174Seric 		usrerr("aliasing/forwarding loop broken");
17212613Seric 		return (a);
1734174Seric 	}
1744174Seric 
1754174Seric 	/*
1764627Seric 	**  Finish setting up address structure.
1774174Seric 	*/
1784174Seric 
17916160Seric 	/* set the queue timeout */
1804627Seric 	a->q_timeout = TimeOut;
1814627Seric 
18216160Seric 	/* map user & host to lower case if requested on non-aliases */
18316160Seric 	if (a->q_alias == NULL)
18416160Seric 		loweraddr(a);
18516160Seric 
18616160Seric 	/* get unquoted user for file, program or user.name check */
1879210Seric 	(void) strcpy(buf, a->q_user);
1889210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1899210Seric 	{
19054993Seric 		if (*p == '\\')
1919210Seric 			quoted = TRUE;
1929210Seric 	}
19354983Seric 	stripquotes(buf);
1949210Seric 
1954627Seric 	/* do sickly crude mapping for program mailing, etc. */
1969210Seric 	if (m == LocalMailer && buf[0] == '|')
1974174Seric 	{
1989210Seric 		a->q_mailer = m = ProgMailer;
1999210Seric 		a->q_user++;
20054975Seric 		if (a->q_alias == NULL && !ForceMail)
2014174Seric 		{
20229915Seric 			a->q_flags |= QDONTSEND|QBADADDR;
2039210Seric 			usrerr("Cannot mail directly to programs");
2044174Seric 		}
2054174Seric 	}
2064174Seric 
2074174Seric 	/*
2084419Seric 	**  Look up this person in the recipient list.
2094419Seric 	**	If they are there already, return, otherwise continue.
2104419Seric 	**	If the list is empty, just add it.  Notice the cute
2114419Seric 	**	hack to make from addresses suppress things correctly:
2124419Seric 	**	the QDONTSEND bit will be set in the send list.
2134419Seric 	**	[Please note: the emphasis is on "hack."]
2144174Seric 	*/
2154174Seric 
2165006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2174174Seric 	{
2189379Seric 		if (!ForceMail && sameaddr(q, a))
2194174Seric 		{
2207676Seric 			if (tTd(26, 1))
2214444Seric 			{
2224444Seric 				printf("%s in sendq: ", a->q_paddr);
2234444Seric 				printaddr(q, FALSE);
2244444Seric 			}
2257054Seric 			if (!bitset(QDONTSEND, a->q_flags))
2264324Seric 				message(Arpa_Info, "duplicate suppressed");
2274423Seric 			if (!bitset(QPRIMARY, q->q_flags))
2284423Seric 				q->q_flags |= a->q_flags;
22912613Seric 			return (q);
2304174Seric 		}
2314319Seric 	}
2324174Seric 
2334319Seric 	/* add address on list */
2344319Seric 	*pq = a;
2354174Seric 	a->q_next = NULL;
236*55012Seric 	e->e_nrcpts++;
2374174Seric 
23852046Seric 	if (a->q_alias == NULL && RcptLogFile != NULL &&
23952046Seric 	    !bitset(QDONTSEND, a->q_flags))
24052046Seric 	{
24152046Seric 		static int RcptLogFd = -1;
24252046Seric 
24352046Seric 		/*
24452046Seric 		**  Log the incoming recipient name before aliasing,
24552046Seric 		**  expanding, forwarding, rewriting, and all that jazz.
24652046Seric 		**  We'll use this to track down out-of-date aliases,
24752046Seric 		**  host names, and so forth.
24852046Seric 		*/
24952046Seric 
25052046Seric 		if (RcptLogFd < 0)
25152046Seric 		{
25252046Seric 			/* try to open the log file */
25352046Seric 			RcptLogFd = open(RcptLogFile, O_WRONLY|O_APPEND|O_CREAT, 0666);
25452047Seric 			if (RcptLogFd >= 0)
25552047Seric 				(void) fcntl(RcptLogFd, F_SETFD, 1);
25652046Seric 		}
25752046Seric 		if (RcptLogFd >= 0)
25852046Seric 		{
25952046Seric 			int l = strlen(a->q_paddr);
26052046Seric 
26152046Seric 			a->q_paddr[l] = '\n';
26252046Seric 			if (write(RcptLogFd, a->q_paddr, l + 1) < 0)
26352046Seric 			{
26452046Seric 				(void) close(RcptLogFd);
26552046Seric 				RcptLogFd = -1;
26652046Seric 			}
26752046Seric 			a->q_paddr[l] = '\0';
26852046Seric 		}
26952046Seric 	}
27052046Seric 
2714174Seric 	/*
2724174Seric 	**  Alias the name and handle :include: specs.
2734174Seric 	*/
2744174Seric 
27553735Seric   trylocaluser:
2769210Seric 	if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags))
2774174Seric 	{
2784174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2794174Seric 		{
2804174Seric 			a->q_flags |= QDONTSEND;
28154975Seric 			if (a->q_alias == NULL && !ForceMail)
28229915Seric 			{
28329915Seric 				a->q_flags |= QBADADDR;
2844399Seric 				usrerr("Cannot mail directly to :include:s");
28529915Seric 			}
2864399Seric 			else
2874399Seric 			{
2887054Seric 				message(Arpa_Info, "including file %s", &a->q_user[9]);
289*55012Seric 				include(&a->q_user[9], FALSE, a, sendq, e);
2904399Seric 			}
2914174Seric 		}
2924174Seric 		else
29350556Seric 		{
29450556Seric 			/* try aliasing */
295*55012Seric 			alias(a, sendq, e);
29650556Seric 
29750556Seric # ifdef USERDB
29851923Seric 			/* if not aliased, look it up in the user database */
29951360Seric 			if (!bitset(QDONTSEND|QNOTREMOTE, a->q_flags))
30051923Seric 			{
30151923Seric 				extern int udbexpand();
30251923Seric 
303*55012Seric 				if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
30451923Seric 				{
30551923Seric 					a->q_flags |= QQUEUEUP;
306*55012Seric 					if (e->e_message == NULL)
307*55012Seric 						e->e_message = newstr("Deferred: user database error");
30851923Seric # ifdef LOG
30951923Seric 					if (LogLevel > 3)
31051923Seric 						syslog(LOG_INFO, "%s: deferred: udbexpand",
311*55012Seric 							e->e_id);
31250556Seric # endif
31351923Seric 					message(Arpa_Info, "queued (user database error)");
31451923Seric 					return (a);
31551923Seric 				}
31651923Seric 			}
31751923Seric # endif
31850556Seric 		}
3194174Seric 	}
3204174Seric 
3214174Seric 	/*
3224174Seric 	**  If the user is local and still being sent, verify that
3234174Seric 	**  the address is good.  If it is, try to forward.
3244174Seric 	**  If the address is already good, we have a forwarding
3254174Seric 	**  loop.  This can be broken by just sending directly to
3264174Seric 	**  the user (which is probably correct anyway).
3274174Seric 	*/
3284174Seric 
32951317Seric 	if (bitset(QDONTSEND, a->q_flags) || m != LocalMailer)
33051317Seric 		return (a);
33151317Seric 
33251317Seric 	/* see if this is to a file */
33351317Seric 	if (buf[0] == '/')
3344174Seric 	{
3354329Seric 		struct stat stb;
3364329Seric 		extern bool writable();
3374174Seric 
33851317Seric 		p = rindex(buf, '/');
33951317Seric 		/* check if writable or creatable */
34051317Seric 		if (a->q_alias == NULL && !QueueRun && !ForceMail)
3414174Seric 		{
34251317Seric 			a->q_flags |= QDONTSEND|QBADADDR;
34351317Seric 			usrerr("Cannot mail directly to files");
3444174Seric 		}
34551317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
34651317Seric 		    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
34751317Seric 		{
34851317Seric 			a->q_flags |= QBADADDR;
349*55012Seric 			giveresponse(EX_CANTCREAT, m, e);
35051317Seric 		}
35151317Seric 		return (a);
35251317Seric 	}
35351317Seric 
35451317Seric 	/*
35551317Seric 	**  If we have a level two config file, then pass the name through
35651317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
35751317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
35851317Seric 	**  after local aliasing has been done.
35951317Seric 	*/
36051317Seric 
36151317Seric 	if (tTd(29, 5))
36251317Seric 	{
36351317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
36451317Seric 			ConfigLevel, RewriteRules[5]);
36551317Seric 		printaddr(a, FALSE);
36651317Seric 	}
36751317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
36851317Seric 	    RewriteRules[5] != NULL)
36951317Seric 	{
370*55012Seric 		maplocaluser(a, sendq, e);
37151317Seric 	}
37251317Seric 
37351317Seric 	/*
37451317Seric 	**  If it didn't get rewritten to another mailer, go ahead
37551317Seric 	**  and deliver it.
37651317Seric 	*/
37751317Seric 
37851317Seric 	if (!bitset(QDONTSEND, a->q_flags))
37951317Seric 	{
38051317Seric 		register struct passwd *pw;
38151317Seric 		extern struct passwd *finduser();
38251317Seric 
38351317Seric 		/* warning -- finduser may trash buf */
38451317Seric 		pw = finduser(buf);
38551317Seric 		if (pw == NULL)
38651317Seric 		{
38751317Seric 			a->q_flags |= QBADADDR;
388*55012Seric 			giveresponse(EX_NOUSER, m, e);
38951317Seric 		}
3904174Seric 		else
3914174Seric 		{
39251317Seric 			char nbuf[MAXNAME];
3934373Seric 
39451317Seric 			if (strcmp(a->q_user, pw->pw_name) != 0)
3954174Seric 			{
39653735Seric 				/* name was a fuzzy match */
39751317Seric 				a->q_user = newstr(pw->pw_name);
39853735Seric 				if (findusercount++ > 3)
39953735Seric 				{
40053735Seric 					usrerr("aliasing/forwarding loop for %s broken",
40153735Seric 						pw->pw_name);
40253735Seric 					return (a);
40353735Seric 				}
40453735Seric 
40553735Seric 				/* see if it aliases */
40651317Seric 				(void) strcpy(buf, pw->pw_name);
40753735Seric 				goto trylocaluser;
4084174Seric 			}
40951317Seric 			a->q_home = newstr(pw->pw_dir);
41051317Seric 			a->q_uid = pw->pw_uid;
41151317Seric 			a->q_gid = pw->pw_gid;
41251317Seric 			a->q_flags |= QGOODUID;
41351317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
41451317Seric 			if (nbuf[0] != '\0')
41551317Seric 				a->q_fullname = newstr(nbuf);
41651317Seric 			if (!quoted)
417*55012Seric 				forward(a, sendq, e);
4184174Seric 		}
4194174Seric 	}
42012613Seric 	return (a);
4214174Seric }
4224174Seric /*
4234373Seric **  FINDUSER -- find the password entry for a user.
4244373Seric **
4254373Seric **	This looks a lot like getpwnam, except that it may want to
4264373Seric **	do some fancier pattern matching in /etc/passwd.
4274373Seric **
4289379Seric **	This routine contains most of the time of many sendmail runs.
4299379Seric **	It deserves to be optimized.
4309379Seric **
4314373Seric **	Parameters:
4324373Seric **		name -- the name to match against.
4334373Seric **
4344373Seric **	Returns:
4354373Seric **		A pointer to a pw struct.
4364373Seric **		NULL if name is unknown or ambiguous.
4374373Seric **
4384373Seric **	Side Effects:
4394407Seric **		may modify name.
4404373Seric */
4414373Seric 
4424373Seric struct passwd *
4434373Seric finduser(name)
4444373Seric 	char *name;
4454373Seric {
4464376Seric 	register struct passwd *pw;
4474407Seric 	register char *p;
44815325Seric 	extern struct passwd *getpwent();
44915325Seric 	extern struct passwd *getpwnam();
4504373Seric 
45125777Seric 	/* map upper => lower case */
4524407Seric 	for (p = name; *p != '\0'; p++)
4534407Seric 	{
45425777Seric 		if (isascii(*p) && isupper(*p))
45525568Seric 			*p = tolower(*p);
4564407Seric 	}
4574407Seric 
45825777Seric 	/* look up this login name using fast path */
45912634Seric 	if ((pw = getpwnam(name)) != NULL)
46012634Seric 		return (pw);
46112634Seric 
46253735Seric #ifdef MATCHGECOS
46353735Seric 	/* see if fuzzy matching allowed */
46453735Seric 	if (!MatchGecos)
46553735Seric 		return NULL;
46653735Seric 
46712634Seric 	/* search for a matching full name instead */
46825777Seric 	for (p = name; *p != '\0'; p++)
46925777Seric 	{
47025777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
47125777Seric 			*p = ' ';
47225777Seric 	}
47323107Seric 	(void) setpwent();
4744376Seric 	while ((pw = getpwent()) != NULL)
4754376Seric 	{
4764998Seric 		char buf[MAXNAME];
4774376Seric 
4784998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
47933725Sbostic 		if (index(buf, ' ') != NULL && !strcasecmp(buf, name))
4804381Seric 		{
4817054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
4824376Seric 			return (pw);
4834377Seric 		}
4844376Seric 	}
48553735Seric #endif
4864376Seric 	return (NULL);
4874373Seric }
4884373Seric /*
4894329Seric **  WRITABLE -- predicate returning if the file is writable.
4904329Seric **
4914329Seric **	This routine must duplicate the algorithm in sys/fio.c.
4924329Seric **	Unfortunately, we cannot use the access call since we
4934329Seric **	won't necessarily be the real uid when we try to
4944329Seric **	actually open the file.
4954329Seric **
4964329Seric **	Notice that ANY file with ANY execute bit is automatically
4974329Seric **	not writable.  This is also enforced by mailfile.
4984329Seric **
4994329Seric **	Parameters:
5004329Seric **		s -- pointer to a stat struct for the file.
5014329Seric **
5024329Seric **	Returns:
5034329Seric **		TRUE -- if we will be able to write this file.
5044329Seric **		FALSE -- if we cannot write this file.
5054329Seric **
5064329Seric **	Side Effects:
5074329Seric **		none.
5084329Seric */
5094329Seric 
5104329Seric bool
5114329Seric writable(s)
5124329Seric 	register struct stat *s;
5134329Seric {
5144329Seric 	int euid, egid;
5154329Seric 	int bits;
5164329Seric 
5174329Seric 	if (bitset(0111, s->st_mode))
5184329Seric 		return (FALSE);
5194329Seric 	euid = getruid();
5204329Seric 	egid = getrgid();
5214329Seric 	if (geteuid() == 0)
5224329Seric 	{
5234329Seric 		if (bitset(S_ISUID, s->st_mode))
5244329Seric 			euid = s->st_uid;
5254329Seric 		if (bitset(S_ISGID, s->st_mode))
5264329Seric 			egid = s->st_gid;
5274329Seric 	}
5284329Seric 
5294329Seric 	if (euid == 0)
5304329Seric 		return (TRUE);
5314329Seric 	bits = S_IWRITE;
5324329Seric 	if (euid != s->st_uid)
5334329Seric 	{
5344329Seric 		bits >>= 3;
5354329Seric 		if (egid != s->st_gid)
5364329Seric 			bits >>= 3;
5374329Seric 	}
5384329Seric 	return ((s->st_mode & bits) != 0);
5394329Seric }
5404329Seric /*
5414174Seric **  INCLUDE -- handle :include: specification.
5424174Seric **
5434174Seric **	Parameters:
5444174Seric **		fname -- filename to include.
54553037Seric **		forwarding -- if TRUE, we are reading a .forward file.
54653037Seric **			if FALSE, it's a :include: file.
5474399Seric **		ctladdr -- address template to use to fill in these
5484399Seric **			addresses -- effective user/group id are
5494399Seric **			the important things.
5505006Seric **		sendq -- a pointer to the head of the send queue
5515006Seric **			to put these addresses in.
5524174Seric **
5534174Seric **	Returns:
5544174Seric **		none.
5554174Seric **
5564174Seric **	Side Effects:
5574174Seric **		reads the :include: file and sends to everyone
5584174Seric **		listed in that file.
5594174Seric */
5604174Seric 
56153037Seric static jmp_buf	CtxIncludeTimeout;
56253037Seric 
563*55012Seric include(fname, forwarding, ctladdr, sendq, e)
5644174Seric 	char *fname;
56553037Seric 	bool forwarding;
5664399Seric 	ADDRESS *ctladdr;
5675006Seric 	ADDRESS **sendq;
568*55012Seric 	ENVELOPE *e;
5694174Seric {
5704174Seric 	register FILE *fp;
571*55012Seric 	char *oldto = e->e_to;
5729379Seric 	char *oldfilename = FileName;
5739379Seric 	int oldlinenumber = LineNumber;
57453037Seric 	register EVENT *ev = NULL;
57553037Seric 	char buf[MAXLINE];
57653037Seric 	static int includetimeout();
5774174Seric 
57853037Seric 	/*
57953037Seric 	**  If home directory is remote mounted but server is down,
58053037Seric 	**  this can hang or give errors; use a timeout to avoid this
58153037Seric 	*/
58253037Seric 
58353037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
58453037Seric 	{
58553037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
58653037Seric 		errno = 0;
58753037Seric 		usrerr("451 open timeout on %s", fname);
58853037Seric 		return;
58953037Seric 	}
59053037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
59153037Seric 
59253037Seric 	/* if forwarding, the input file must be marked safe */
59353037Seric 	if (forwarding && !safefile(fname, ctladdr->q_uid, S_IREAD))
59453037Seric 	{
59553037Seric 		/* don't use this .forward file */
59653037Seric 		clrevent(ev);
59753037Seric 		return;
59853037Seric 	}
59953037Seric 
6004174Seric 	fp = fopen(fname, "r");
6014174Seric 	if (fp == NULL)
6024174Seric 	{
6034174Seric 		usrerr("Cannot open %s", fname);
6044174Seric 		return;
6054174Seric 	}
60653037Seric 
6074406Seric 	if (getctladdr(ctladdr) == NULL)
6084406Seric 	{
6094406Seric 		struct stat st;
6104174Seric 
6114406Seric 		if (fstat(fileno(fp), &st) < 0)
6124406Seric 			syserr("Cannot fstat %s!", fname);
6134406Seric 		ctladdr->q_uid = st.st_uid;
6144406Seric 		ctladdr->q_gid = st.st_gid;
6154406Seric 		ctladdr->q_flags |= QGOODUID;
6164406Seric 	}
6174406Seric 
61853037Seric 	clrevent(ev);
61953037Seric 
6204174Seric 	/* read the file -- each line is a comma-separated list. */
6219379Seric 	FileName = fname;
6229379Seric 	LineNumber = 0;
6234174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6244174Seric 	{
6254174Seric 		register char *p = index(buf, '\n');
6264174Seric 
62740963Sbostic 		LineNumber++;
6284174Seric 		if (p != NULL)
6294174Seric 			*p = '\0';
63051781Seric 		if (buf[0] == '\0' || buf[0] == '#')
6314174Seric 			continue;
632*55012Seric 		e->e_to = oldto;
63353037Seric 		message(Arpa_Info, "%s to %s",
63453037Seric 			forwarding ? "forwarding" : "sending", buf);
6354176Seric 		AliasLevel++;
636*55012Seric 		sendtolist(buf, ctladdr, sendq, e);
6374176Seric 		AliasLevel--;
6384174Seric 	}
6394174Seric 
6404319Seric 	(void) fclose(fp);
6419379Seric 	FileName = oldfilename;
6429379Seric 	LineNumber = oldlinenumber;
6434174Seric }
64453037Seric 
64553037Seric static
64653037Seric includetimeout()
64753037Seric {
64853037Seric 	longjmp(CtxIncludeTimeout, 1);
64953037Seric }
6504324Seric /*
6514324Seric **  SENDTOARGV -- send to an argument vector.
6524324Seric **
6534324Seric **	Parameters:
6544324Seric **		argv -- argument vector to send to.
6554324Seric **
6564324Seric **	Returns:
6574324Seric **		none.
6584324Seric **
6594324Seric **	Side Effects:
6604324Seric **		puts all addresses on the argument vector onto the
6614324Seric **			send queue.
6624324Seric */
6634324Seric 
664*55012Seric sendtoargv(argv, e)
6654324Seric 	register char **argv;
666*55012Seric 	register ENVELOPE *e;
6674324Seric {
6684324Seric 	register char *p;
6694324Seric 
6704324Seric 	while ((p = *argv++) != NULL)
6714324Seric 	{
67233725Sbostic 		if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at"))
6734324Seric 		{
6744324Seric 			char nbuf[MAXNAME];
6754324Seric 
6764324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
6774324Seric 				usrerr("address overflow");
6784324Seric 			else
6794324Seric 			{
6804324Seric 				(void) strcpy(nbuf, p);
6814324Seric 				(void) strcat(nbuf, "@");
6824324Seric 				(void) strcat(nbuf, argv[1]);
6834324Seric 				p = newstr(nbuf);
6844324Seric 				argv += 2;
6854324Seric 			}
6864324Seric 		}
687*55012Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
6884324Seric 	}
6894324Seric }
6904399Seric /*
6914399Seric **  GETCTLADDR -- get controlling address from an address header.
6924399Seric **
6934399Seric **	If none, get one corresponding to the effective userid.
6944399Seric **
6954399Seric **	Parameters:
6964399Seric **		a -- the address to find the controller of.
6974399Seric **
6984399Seric **	Returns:
6994399Seric **		the controlling address.
7004399Seric **
7014399Seric **	Side Effects:
7024399Seric **		none.
7034399Seric */
7044399Seric 
7054399Seric ADDRESS *
7064399Seric getctladdr(a)
7074399Seric 	register ADDRESS *a;
7084399Seric {
7094404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7104399Seric 		a = a->q_alias;
7114399Seric 	return (a);
7124399Seric }
713