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*54975Seric static char sccsid[] = "@(#)recipient.c	5.30 (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 
439622Seric sendtolist(list, ctladdr, sendq)
444174Seric 	char *list;
454399Seric 	ADDRESS *ctladdr;
465198Seric 	ADDRESS **sendq;
474174Seric {
484174Seric 	register char *p;
498223Seric 	register ADDRESS *al;	/* list of addresses to send to */
504423Seric 	bool firstone;		/* set on first address sent */
514444Seric 	bool selfref;		/* set if this list includes ctladdr */
5211446Seric 	char delimiter;		/* the address delimiter */
534174Seric 
547676Seric 	if (tTd(25, 1))
554444Seric 	{
564444Seric 		printf("sendto: %s\n   ctladdr=", list);
574444Seric 		printaddr(ctladdr, FALSE);
584444Seric 	}
594324Seric 
608223Seric 	/* heuristic to determine old versus new style addresses */
618230Seric 	if (ctladdr == NULL &&
628230Seric 	    (index(list, ',') != NULL || index(list, ';') != NULL ||
638230Seric 	     index(list, '<') != NULL || index(list, '(') != NULL))
649340Seric 		CurEnv->e_flags &= ~EF_OLDSTYLE;
6511446Seric 	delimiter = ' ';
6611446Seric 	if (!bitset(EF_OLDSTYLE, CurEnv->e_flags) || ctladdr != NULL)
6711446Seric 		delimiter = ',';
688223Seric 
694423Seric 	firstone = TRUE;
704444Seric 	selfref = FALSE;
714324Seric 	al = NULL;
728223Seric 
738081Seric 	for (p = list; *p != '\0'; )
744174Seric 	{
758081Seric 		register ADDRESS *a;
768081Seric 		extern char *DelimChar;		/* defined in prescan */
774319Seric 
788081Seric 		/* parse the address */
798081Seric 		while (isspace(*p) || *p == ',')
804174Seric 			p++;
8111446Seric 		a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter);
829297Seric 		p = DelimChar;
839297Seric 		if (a == NULL)
844174Seric 			continue;
854324Seric 		a->q_next = al;
864399Seric 		a->q_alias = ctladdr;
874444Seric 
884444Seric 		/* see if this should be marked as a primary address */
894423Seric 		if (ctladdr == NULL ||
908081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
914423Seric 			a->q_flags |= QPRIMARY;
924444Seric 
934444Seric 		/* put on send queue or suppress self-reference */
949379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
954444Seric 			selfref = TRUE;
964444Seric 		else
974444Seric 			al = a;
984423Seric 		firstone = FALSE;
994324Seric 	}
1004324Seric 
1014444Seric 	/* if this alias doesn't include itself, delete ctladdr */
1024444Seric 	if (!selfref && ctladdr != NULL)
1034444Seric 		ctladdr->q_flags |= QDONTSEND;
1044444Seric 
1054324Seric 	/* arrange to send to everyone on the local send list */
1064324Seric 	while (al != NULL)
1074324Seric 	{
1084324Seric 		register ADDRESS *a = al;
10912613Seric 		extern ADDRESS *recipient();
1104324Seric 
1114324Seric 		al = a->q_next;
11212613Seric 		a = recipient(a, sendq);
1134993Seric 
1144998Seric 		/* arrange to inherit full name */
1154998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1164998Seric 			a->q_fullname = ctladdr->q_fullname;
1174174Seric 	}
1184324Seric 
1196906Seric 	CurEnv->e_to = NULL;
1204174Seric }
1214174Seric /*
1224174Seric **  RECIPIENT -- Designate a message recipient
1234174Seric **
1244174Seric **	Saves the named person for future mailing.
1254174Seric **
1264174Seric **	Parameters:
1274174Seric **		a -- the (preparsed) address header for the recipient.
1285006Seric **		sendq -- a pointer to the head of a queue to put the
1295006Seric **			recipient in.  Duplicate supression is done
1305006Seric **			in this queue.
1314174Seric **
1324174Seric **	Returns:
13312613Seric **		The actual address in the queue.  This will be "a" if
13412613Seric **		the address is not a duplicate, else the original address.
1354174Seric **
1364174Seric **	Side Effects:
1374174Seric **		none.
1384174Seric */
1394174Seric 
14046928Sbostic extern ADDRESS *getctladdr();
14152046Seric extern char	*RcptLogFile;
14246928Sbostic 
14312613Seric ADDRESS *
1445006Seric recipient(a, sendq)
1454174Seric 	register ADDRESS *a;
1465006Seric 	register ADDRESS **sendq;
1474174Seric {
1484174Seric 	register ADDRESS *q;
1494319Seric 	ADDRESS **pq;
1504174Seric 	register struct mailer *m;
1519210Seric 	register char *p;
1529210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
15353735Seric 	int findusercount = 0;
1549210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
1554627Seric 	extern bool safefile();
1564174Seric 
1576906Seric 	CurEnv->e_to = a->q_paddr;
1584600Seric 	m = a->q_mailer;
1594174Seric 	errno = 0;
1607676Seric 	if (tTd(26, 1))
1614444Seric 	{
1624444Seric 		printf("\nrecipient: ");
1634444Seric 		printaddr(a, FALSE);
1644444Seric 	}
1654174Seric 
1664174Seric 	/* break aliasing loops */
1674174Seric 	if (AliasLevel > MAXRCRSN)
1684174Seric 	{
1694174Seric 		usrerr("aliasing/forwarding loop broken");
17012613Seric 		return (a);
1714174Seric 	}
1724174Seric 
1734174Seric 	/*
1744627Seric 	**  Finish setting up address structure.
1754174Seric 	*/
1764174Seric 
17716160Seric 	/* set the queue timeout */
1784627Seric 	a->q_timeout = TimeOut;
1794627Seric 
18016160Seric 	/* map user & host to lower case if requested on non-aliases */
18116160Seric 	if (a->q_alias == NULL)
18216160Seric 		loweraddr(a);
18316160Seric 
18416160Seric 	/* get unquoted user for file, program or user.name check */
1859210Seric 	(void) strcpy(buf, a->q_user);
1869210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1879210Seric 	{
1889210Seric 		if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377))
1899210Seric 			quoted = TRUE;
1909210Seric 	}
1919210Seric 	stripquotes(buf, TRUE);
1929210Seric 
1934627Seric 	/* do sickly crude mapping for program mailing, etc. */
1949210Seric 	if (m == LocalMailer && buf[0] == '|')
1954174Seric 	{
1969210Seric 		a->q_mailer = m = ProgMailer;
1979210Seric 		a->q_user++;
198*54975Seric 		if (a->q_alias == NULL && !ForceMail)
1994174Seric 		{
20029915Seric 			a->q_flags |= QDONTSEND|QBADADDR;
2019210Seric 			usrerr("Cannot mail directly to programs");
2024174Seric 		}
2034174Seric 	}
2044174Seric 
2054174Seric 	/*
2064419Seric 	**  Look up this person in the recipient list.
2074419Seric 	**	If they are there already, return, otherwise continue.
2084419Seric 	**	If the list is empty, just add it.  Notice the cute
2094419Seric 	**	hack to make from addresses suppress things correctly:
2104419Seric 	**	the QDONTSEND bit will be set in the send list.
2114419Seric 	**	[Please note: the emphasis is on "hack."]
2124174Seric 	*/
2134174Seric 
2145006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2154174Seric 	{
2169379Seric 		if (!ForceMail && sameaddr(q, a))
2174174Seric 		{
2187676Seric 			if (tTd(26, 1))
2194444Seric 			{
2204444Seric 				printf("%s in sendq: ", a->q_paddr);
2214444Seric 				printaddr(q, FALSE);
2224444Seric 			}
2237054Seric 			if (!bitset(QDONTSEND, a->q_flags))
2244324Seric 				message(Arpa_Info, "duplicate suppressed");
2254423Seric 			if (!bitset(QPRIMARY, q->q_flags))
2264423Seric 				q->q_flags |= a->q_flags;
22712613Seric 			return (q);
2284174Seric 		}
2294319Seric 	}
2304174Seric 
2314319Seric 	/* add address on list */
2324319Seric 	*pq = a;
2334174Seric 	a->q_next = NULL;
23424951Seric 	CurEnv->e_nrcpts++;
2354174Seric 
23652046Seric 	if (a->q_alias == NULL && RcptLogFile != NULL &&
23752046Seric 	    !bitset(QDONTSEND, a->q_flags))
23852046Seric 	{
23952046Seric 		static int RcptLogFd = -1;
24052046Seric 
24152046Seric 		/*
24252046Seric 		**  Log the incoming recipient name before aliasing,
24352046Seric 		**  expanding, forwarding, rewriting, and all that jazz.
24452046Seric 		**  We'll use this to track down out-of-date aliases,
24552046Seric 		**  host names, and so forth.
24652046Seric 		*/
24752046Seric 
24852046Seric 		if (RcptLogFd < 0)
24952046Seric 		{
25052046Seric 			/* try to open the log file */
25152046Seric 			RcptLogFd = open(RcptLogFile, O_WRONLY|O_APPEND|O_CREAT, 0666);
25252047Seric 			if (RcptLogFd >= 0)
25352047Seric 				(void) fcntl(RcptLogFd, F_SETFD, 1);
25452046Seric 		}
25552046Seric 		if (RcptLogFd >= 0)
25652046Seric 		{
25752046Seric 			int l = strlen(a->q_paddr);
25852046Seric 
25952046Seric 			a->q_paddr[l] = '\n';
26052046Seric 			if (write(RcptLogFd, a->q_paddr, l + 1) < 0)
26152046Seric 			{
26252046Seric 				(void) close(RcptLogFd);
26352046Seric 				RcptLogFd = -1;
26452046Seric 			}
26552046Seric 			a->q_paddr[l] = '\0';
26652046Seric 		}
26752046Seric 	}
26852046Seric 
2694174Seric 	/*
2704174Seric 	**  Alias the name and handle :include: specs.
2714174Seric 	*/
2724174Seric 
27353735Seric   trylocaluser:
2749210Seric 	if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags))
2754174Seric 	{
2764174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2774174Seric 		{
2784174Seric 			a->q_flags |= QDONTSEND;
279*54975Seric 			if (a->q_alias == NULL && !ForceMail)
28029915Seric 			{
28129915Seric 				a->q_flags |= QBADADDR;
2824399Seric 				usrerr("Cannot mail directly to :include:s");
28329915Seric 			}
2844399Seric 			else
2854399Seric 			{
2867054Seric 				message(Arpa_Info, "including file %s", &a->q_user[9]);
28753037Seric 				include(&a->q_user[9], FALSE, a, sendq);
2884399Seric 			}
2894174Seric 		}
2904174Seric 		else
29150556Seric 		{
29250556Seric 			/* try aliasing */
2935006Seric 			alias(a, sendq);
29450556Seric 
29550556Seric # ifdef USERDB
29651923Seric 			/* if not aliased, look it up in the user database */
29751360Seric 			if (!bitset(QDONTSEND|QNOTREMOTE, a->q_flags))
29851923Seric 			{
29951923Seric 				extern int udbexpand();
30051923Seric 
30151923Seric 				if (udbexpand(a, sendq) == EX_TEMPFAIL)
30251923Seric 				{
30351923Seric 					a->q_flags |= QQUEUEUP;
30451923Seric 					if (CurEnv->e_message == NULL)
30551923Seric 						CurEnv->e_message = newstr("Deferred: user database error");
30651923Seric # ifdef LOG
30751923Seric 					if (LogLevel > 3)
30851923Seric 						syslog(LOG_INFO, "%s: deferred: udbexpand",
30951923Seric 							CurEnv->e_id);
31050556Seric # endif
31151923Seric 					message(Arpa_Info, "queued (user database error)");
31251923Seric 					return (a);
31351923Seric 				}
31451923Seric 			}
31551923Seric # endif
31650556Seric 		}
3174174Seric 	}
3184174Seric 
3194174Seric 	/*
3204174Seric 	**  If the user is local and still being sent, verify that
3214174Seric 	**  the address is good.  If it is, try to forward.
3224174Seric 	**  If the address is already good, we have a forwarding
3234174Seric 	**  loop.  This can be broken by just sending directly to
3244174Seric 	**  the user (which is probably correct anyway).
3254174Seric 	*/
3264174Seric 
32751317Seric 	if (bitset(QDONTSEND, a->q_flags) || m != LocalMailer)
32851317Seric 		return (a);
32951317Seric 
33051317Seric 	/* see if this is to a file */
33151317Seric 	if (buf[0] == '/')
3324174Seric 	{
3334329Seric 		struct stat stb;
3344329Seric 		extern bool writable();
3354174Seric 
33651317Seric 		p = rindex(buf, '/');
33751317Seric 		/* check if writable or creatable */
33851317Seric 		if (a->q_alias == NULL && !QueueRun && !ForceMail)
3394174Seric 		{
34051317Seric 			a->q_flags |= QDONTSEND|QBADADDR;
34151317Seric 			usrerr("Cannot mail directly to files");
3424174Seric 		}
34351317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
34451317Seric 		    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
34551317Seric 		{
34651317Seric 			a->q_flags |= QBADADDR;
34751317Seric 			giveresponse(EX_CANTCREAT, m, CurEnv);
34851317Seric 		}
34951317Seric 		return (a);
35051317Seric 	}
35151317Seric 
35251317Seric 	/*
35351317Seric 	**  If we have a level two config file, then pass the name through
35451317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
35551317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
35651317Seric 	**  after local aliasing has been done.
35751317Seric 	*/
35851317Seric 
35951317Seric 	if (tTd(29, 5))
36051317Seric 	{
36151317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
36251317Seric 			ConfigLevel, RewriteRules[5]);
36351317Seric 		printaddr(a, FALSE);
36451317Seric 	}
36551317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
36651317Seric 	    RewriteRules[5] != NULL)
36751317Seric 	{
36851317Seric 		maplocaluser(a, sendq);
36951317Seric 	}
37051317Seric 
37151317Seric 	/*
37251317Seric 	**  If it didn't get rewritten to another mailer, go ahead
37351317Seric 	**  and deliver it.
37451317Seric 	*/
37551317Seric 
37651317Seric 	if (!bitset(QDONTSEND, a->q_flags))
37751317Seric 	{
37851317Seric 		register struct passwd *pw;
37951317Seric 		extern struct passwd *finduser();
38051317Seric 
38151317Seric 		/* warning -- finduser may trash buf */
38251317Seric 		pw = finduser(buf);
38351317Seric 		if (pw == NULL)
38451317Seric 		{
38551317Seric 			a->q_flags |= QBADADDR;
38651317Seric 			giveresponse(EX_NOUSER, m, CurEnv);
38751317Seric 		}
3884174Seric 		else
3894174Seric 		{
39051317Seric 			char nbuf[MAXNAME];
3914373Seric 
39251317Seric 			if (strcmp(a->q_user, pw->pw_name) != 0)
3934174Seric 			{
39453735Seric 				/* name was a fuzzy match */
39551317Seric 				a->q_user = newstr(pw->pw_name);
39653735Seric 				if (findusercount++ > 3)
39753735Seric 				{
39853735Seric 					usrerr("aliasing/forwarding loop for %s broken",
39953735Seric 						pw->pw_name);
40053735Seric 					return (a);
40153735Seric 				}
40253735Seric 
40353735Seric 				/* see if it aliases */
40451317Seric 				(void) strcpy(buf, pw->pw_name);
40553735Seric 				goto trylocaluser;
4064174Seric 			}
40751317Seric 			a->q_home = newstr(pw->pw_dir);
40851317Seric 			a->q_uid = pw->pw_uid;
40951317Seric 			a->q_gid = pw->pw_gid;
41051317Seric 			a->q_flags |= QGOODUID;
41151317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
41251317Seric 			if (nbuf[0] != '\0')
41351317Seric 				a->q_fullname = newstr(nbuf);
41451317Seric 			if (!quoted)
41551317Seric 				forward(a, sendq);
4164174Seric 		}
4174174Seric 	}
41812613Seric 	return (a);
4194174Seric }
4204174Seric /*
4214373Seric **  FINDUSER -- find the password entry for a user.
4224373Seric **
4234373Seric **	This looks a lot like getpwnam, except that it may want to
4244373Seric **	do some fancier pattern matching in /etc/passwd.
4254373Seric **
4269379Seric **	This routine contains most of the time of many sendmail runs.
4279379Seric **	It deserves to be optimized.
4289379Seric **
4294373Seric **	Parameters:
4304373Seric **		name -- the name to match against.
4314373Seric **
4324373Seric **	Returns:
4334373Seric **		A pointer to a pw struct.
4344373Seric **		NULL if name is unknown or ambiguous.
4354373Seric **
4364373Seric **	Side Effects:
4374407Seric **		may modify name.
4384373Seric */
4394373Seric 
4404373Seric struct passwd *
4414373Seric finduser(name)
4424373Seric 	char *name;
4434373Seric {
4444376Seric 	register struct passwd *pw;
4454407Seric 	register char *p;
44615325Seric 	extern struct passwd *getpwent();
44715325Seric 	extern struct passwd *getpwnam();
4484373Seric 
44925777Seric 	/* map upper => lower case */
4504407Seric 	for (p = name; *p != '\0'; p++)
4514407Seric 	{
45225777Seric 		if (isascii(*p) && isupper(*p))
45325568Seric 			*p = tolower(*p);
4544407Seric 	}
4554407Seric 
45625777Seric 	/* look up this login name using fast path */
45712634Seric 	if ((pw = getpwnam(name)) != NULL)
45812634Seric 		return (pw);
45912634Seric 
46053735Seric #ifdef MATCHGECOS
46153735Seric 	/* see if fuzzy matching allowed */
46253735Seric 	if (!MatchGecos)
46353735Seric 		return NULL;
46453735Seric 
46512634Seric 	/* search for a matching full name instead */
46625777Seric 	for (p = name; *p != '\0'; p++)
46725777Seric 	{
46825777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
46925777Seric 			*p = ' ';
47025777Seric 	}
47123107Seric 	(void) setpwent();
4724376Seric 	while ((pw = getpwent()) != NULL)
4734376Seric 	{
4744998Seric 		char buf[MAXNAME];
4754376Seric 
4764998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
47733725Sbostic 		if (index(buf, ' ') != NULL && !strcasecmp(buf, name))
4784381Seric 		{
4797054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
4804376Seric 			return (pw);
4814377Seric 		}
4824376Seric 	}
48353735Seric #endif
4844376Seric 	return (NULL);
4854373Seric }
4864373Seric /*
4874329Seric **  WRITABLE -- predicate returning if the file is writable.
4884329Seric **
4894329Seric **	This routine must duplicate the algorithm in sys/fio.c.
4904329Seric **	Unfortunately, we cannot use the access call since we
4914329Seric **	won't necessarily be the real uid when we try to
4924329Seric **	actually open the file.
4934329Seric **
4944329Seric **	Notice that ANY file with ANY execute bit is automatically
4954329Seric **	not writable.  This is also enforced by mailfile.
4964329Seric **
4974329Seric **	Parameters:
4984329Seric **		s -- pointer to a stat struct for the file.
4994329Seric **
5004329Seric **	Returns:
5014329Seric **		TRUE -- if we will be able to write this file.
5024329Seric **		FALSE -- if we cannot write this file.
5034329Seric **
5044329Seric **	Side Effects:
5054329Seric **		none.
5064329Seric */
5074329Seric 
5084329Seric bool
5094329Seric writable(s)
5104329Seric 	register struct stat *s;
5114329Seric {
5124329Seric 	int euid, egid;
5134329Seric 	int bits;
5144329Seric 
5154329Seric 	if (bitset(0111, s->st_mode))
5164329Seric 		return (FALSE);
5174329Seric 	euid = getruid();
5184329Seric 	egid = getrgid();
5194329Seric 	if (geteuid() == 0)
5204329Seric 	{
5214329Seric 		if (bitset(S_ISUID, s->st_mode))
5224329Seric 			euid = s->st_uid;
5234329Seric 		if (bitset(S_ISGID, s->st_mode))
5244329Seric 			egid = s->st_gid;
5254329Seric 	}
5264329Seric 
5274329Seric 	if (euid == 0)
5284329Seric 		return (TRUE);
5294329Seric 	bits = S_IWRITE;
5304329Seric 	if (euid != s->st_uid)
5314329Seric 	{
5324329Seric 		bits >>= 3;
5334329Seric 		if (egid != s->st_gid)
5344329Seric 			bits >>= 3;
5354329Seric 	}
5364329Seric 	return ((s->st_mode & bits) != 0);
5374329Seric }
5384329Seric /*
5394174Seric **  INCLUDE -- handle :include: specification.
5404174Seric **
5414174Seric **	Parameters:
5424174Seric **		fname -- filename to include.
54353037Seric **		forwarding -- if TRUE, we are reading a .forward file.
54453037Seric **			if FALSE, it's a :include: file.
5454399Seric **		ctladdr -- address template to use to fill in these
5464399Seric **			addresses -- effective user/group id are
5474399Seric **			the important things.
5485006Seric **		sendq -- a pointer to the head of the send queue
5495006Seric **			to put these addresses in.
5504174Seric **
5514174Seric **	Returns:
5524174Seric **		none.
5534174Seric **
5544174Seric **	Side Effects:
5554174Seric **		reads the :include: file and sends to everyone
5564174Seric **		listed in that file.
5574174Seric */
5584174Seric 
55953037Seric static jmp_buf	CtxIncludeTimeout;
56053037Seric 
56153037Seric include(fname, forwarding, ctladdr, sendq)
5624174Seric 	char *fname;
56353037Seric 	bool forwarding;
5644399Seric 	ADDRESS *ctladdr;
5655006Seric 	ADDRESS **sendq;
5664174Seric {
5674174Seric 	register FILE *fp;
5686906Seric 	char *oldto = CurEnv->e_to;
5699379Seric 	char *oldfilename = FileName;
5709379Seric 	int oldlinenumber = LineNumber;
57153037Seric 	register EVENT *ev = NULL;
57253037Seric 	char buf[MAXLINE];
57353037Seric 	static int includetimeout();
5744174Seric 
57553037Seric 	/*
57653037Seric 	**  If home directory is remote mounted but server is down,
57753037Seric 	**  this can hang or give errors; use a timeout to avoid this
57853037Seric 	*/
57953037Seric 
58053037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
58153037Seric 	{
58253037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
58353037Seric 		errno = 0;
58453037Seric 		usrerr("451 open timeout on %s", fname);
58553037Seric 		return;
58653037Seric 	}
58753037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
58853037Seric 
58953037Seric 	/* if forwarding, the input file must be marked safe */
59053037Seric 	if (forwarding && !safefile(fname, ctladdr->q_uid, S_IREAD))
59153037Seric 	{
59253037Seric 		/* don't use this .forward file */
59353037Seric 		clrevent(ev);
59453037Seric 		return;
59553037Seric 	}
59653037Seric 
5974174Seric 	fp = fopen(fname, "r");
5984174Seric 	if (fp == NULL)
5994174Seric 	{
6004174Seric 		usrerr("Cannot open %s", fname);
6014174Seric 		return;
6024174Seric 	}
60353037Seric 
6044406Seric 	if (getctladdr(ctladdr) == NULL)
6054406Seric 	{
6064406Seric 		struct stat st;
6074174Seric 
6084406Seric 		if (fstat(fileno(fp), &st) < 0)
6094406Seric 			syserr("Cannot fstat %s!", fname);
6104406Seric 		ctladdr->q_uid = st.st_uid;
6114406Seric 		ctladdr->q_gid = st.st_gid;
6124406Seric 		ctladdr->q_flags |= QGOODUID;
6134406Seric 	}
6144406Seric 
61553037Seric 	clrevent(ev);
61653037Seric 
6174174Seric 	/* read the file -- each line is a comma-separated list. */
6189379Seric 	FileName = fname;
6199379Seric 	LineNumber = 0;
6204174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6214174Seric 	{
6224174Seric 		register char *p = index(buf, '\n');
6234174Seric 
62440963Sbostic 		LineNumber++;
6254174Seric 		if (p != NULL)
6264174Seric 			*p = '\0';
62751781Seric 		if (buf[0] == '\0' || buf[0] == '#')
6284174Seric 			continue;
6296906Seric 		CurEnv->e_to = oldto;
63053037Seric 		message(Arpa_Info, "%s to %s",
63153037Seric 			forwarding ? "forwarding" : "sending", buf);
6324176Seric 		AliasLevel++;
6339622Seric 		sendtolist(buf, ctladdr, sendq);
6344176Seric 		AliasLevel--;
6354174Seric 	}
6364174Seric 
6374319Seric 	(void) fclose(fp);
6389379Seric 	FileName = oldfilename;
6399379Seric 	LineNumber = oldlinenumber;
6404174Seric }
64153037Seric 
64253037Seric static
64353037Seric includetimeout()
64453037Seric {
64553037Seric 	longjmp(CtxIncludeTimeout, 1);
64653037Seric }
6474324Seric /*
6484324Seric **  SENDTOARGV -- send to an argument vector.
6494324Seric **
6504324Seric **	Parameters:
6514324Seric **		argv -- argument vector to send to.
6524324Seric **
6534324Seric **	Returns:
6544324Seric **		none.
6554324Seric **
6564324Seric **	Side Effects:
6574324Seric **		puts all addresses on the argument vector onto the
6584324Seric **			send queue.
6594324Seric */
6604324Seric 
6614324Seric sendtoargv(argv)
6624324Seric 	register char **argv;
6634324Seric {
6644324Seric 	register char *p;
6654324Seric 
6664324Seric 	while ((p = *argv++) != NULL)
6674324Seric 	{
66833725Sbostic 		if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at"))
6694324Seric 		{
6704324Seric 			char nbuf[MAXNAME];
6714324Seric 
6724324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
6734324Seric 				usrerr("address overflow");
6744324Seric 			else
6754324Seric 			{
6764324Seric 				(void) strcpy(nbuf, p);
6774324Seric 				(void) strcat(nbuf, "@");
6784324Seric 				(void) strcat(nbuf, argv[1]);
6794324Seric 				p = newstr(nbuf);
6804324Seric 				argv += 2;
6814324Seric 			}
6824324Seric 		}
6839622Seric 		sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
6844324Seric 	}
6854324Seric }
6864399Seric /*
6874399Seric **  GETCTLADDR -- get controlling address from an address header.
6884399Seric **
6894399Seric **	If none, get one corresponding to the effective userid.
6904399Seric **
6914399Seric **	Parameters:
6924399Seric **		a -- the address to find the controller of.
6934399Seric **
6944399Seric **	Returns:
6954399Seric **		the controlling address.
6964399Seric **
6974399Seric **	Side Effects:
6984399Seric **		none.
6994399Seric */
7004399Seric 
7014399Seric ADDRESS *
7024399Seric getctladdr(a)
7034399Seric 	register ADDRESS *a;
7044399Seric {
7054404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7064399Seric 		a = a->q_alias;
7074399Seric 	return (a);
7084399Seric }
709