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*59623Seric static char sccsid[] = "@(#)recipient.c	6.40 (Berkeley) 05/01/93";
1133731Sbostic #endif /* not lint */
1222710Sdist 
1358332Seric # include "sendmail.h"
144174Seric # include <pwd.h>
154174Seric 
164174Seric /*
179622Seric **  SENDTOLIST -- Designate a send list.
184174Seric **
194174Seric **	The parameter is a comma-separated list of people to send to.
204174Seric **	This routine arranges to send to all of them.
214174Seric **
224174Seric **	Parameters:
234174Seric **		list -- the send list.
244399Seric **		ctladdr -- the address template for the person to
254399Seric **			send to -- effective uid/gid are important.
265006Seric **			This is typically the alias that caused this
275006Seric **			expansion.
285006Seric **		sendq -- a pointer to the head of a queue to put
295006Seric **			these people into.
3058247Seric **		e -- the envelope in which to add these recipients.
314174Seric **
324174Seric **	Returns:
3358082Seric **		The number of addresses actually on the list.
344174Seric **
354174Seric **	Side Effects:
364174Seric **		none.
374174Seric */
384174Seric 
394174Seric # define MAXRCRSN	10
404174Seric 
4155012Seric sendtolist(list, ctladdr, sendq, e)
424174Seric 	char *list;
434399Seric 	ADDRESS *ctladdr;
445198Seric 	ADDRESS **sendq;
4555012Seric 	register ENVELOPE *e;
464174Seric {
474174Seric 	register char *p;
488223Seric 	register ADDRESS *al;	/* list of addresses to send to */
494423Seric 	bool firstone;		/* set on first address sent */
5011446Seric 	char delimiter;		/* the address delimiter */
5158082Seric 	int naddrs;
524174Seric 
537676Seric 	if (tTd(25, 1))
544444Seric 	{
554444Seric 		printf("sendto: %s\n   ctladdr=", list);
564444Seric 		printaddr(ctladdr, FALSE);
574444Seric 	}
584324Seric 
598223Seric 	/* heuristic to determine old versus new style addresses */
608230Seric 	if (ctladdr == NULL &&
6156795Seric 	    (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
6256795Seric 	     strchr(list, '<') != NULL || strchr(list, '(') != NULL))
6355012Seric 		e->e_flags &= ~EF_OLDSTYLE;
6411446Seric 	delimiter = ' ';
6555012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
6611446Seric 		delimiter = ',';
678223Seric 
684423Seric 	firstone = TRUE;
694324Seric 	al = NULL;
7058082Seric 	naddrs = 0;
718223Seric 
728081Seric 	for (p = list; *p != '\0'; )
734174Seric 	{
7458333Seric 		auto char *delimptr;
758081Seric 		register ADDRESS *a;
764319Seric 
778081Seric 		/* parse the address */
7858050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
794174Seric 			p++;
8058333Seric 		a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter, &delimptr, e);
8158333Seric 		p = delimptr;
829297Seric 		if (a == NULL)
834174Seric 			continue;
844324Seric 		a->q_next = al;
854399Seric 		a->q_alias = ctladdr;
864444Seric 
874444Seric 		/* see if this should be marked as a primary address */
884423Seric 		if (ctladdr == NULL ||
898081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
904423Seric 			a->q_flags |= QPRIMARY;
914444Seric 
929379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
9358061Seric 			ctladdr->q_flags |= QSELFREF;
9457731Seric 		al = a;
954423Seric 		firstone = FALSE;
964324Seric 	}
974324Seric 
984324Seric 	/* arrange to send to everyone on the local send list */
994324Seric 	while (al != NULL)
1004324Seric 	{
1014324Seric 		register ADDRESS *a = al;
10212613Seric 		extern ADDRESS *recipient();
1034324Seric 
1044324Seric 		al = a->q_next;
10555012Seric 		a = recipient(a, sendq, e);
1064993Seric 
1074998Seric 		/* arrange to inherit full name */
1084998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1094998Seric 			a->q_fullname = ctladdr->q_fullname;
11058082Seric 		naddrs++;
1114174Seric 	}
1124324Seric 
11355012Seric 	e->e_to = NULL;
11458082Seric 	return (naddrs);
1154174Seric }
1164174Seric /*
1174174Seric **  RECIPIENT -- Designate a message recipient
1184174Seric **
1194174Seric **	Saves the named person for future mailing.
1204174Seric **
1214174Seric **	Parameters:
1224174Seric **		a -- the (preparsed) address header for the recipient.
1235006Seric **		sendq -- a pointer to the head of a queue to put the
1245006Seric **			recipient in.  Duplicate supression is done
1255006Seric **			in this queue.
12657731Seric **		e -- the current envelope.
1274174Seric **
1284174Seric **	Returns:
12912613Seric **		The actual address in the queue.  This will be "a" if
13012613Seric **		the address is not a duplicate, else the original address.
1314174Seric **
1324174Seric **	Side Effects:
1334174Seric **		none.
1344174Seric */
1354174Seric 
13646928Sbostic extern ADDRESS *getctladdr();
13746928Sbostic 
13812613Seric ADDRESS *
13955012Seric recipient(a, sendq, e)
1404174Seric 	register ADDRESS *a;
1415006Seric 	register ADDRESS **sendq;
14255012Seric 	register ENVELOPE *e;
1434174Seric {
1444174Seric 	register ADDRESS *q;
1454319Seric 	ADDRESS **pq;
1464174Seric 	register struct mailer *m;
1479210Seric 	register char *p;
1489210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
14953735Seric 	int findusercount = 0;
1509210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
15158247Seric 	extern int safefile();
1524174Seric 
15355012Seric 	e->e_to = a->q_paddr;
1544600Seric 	m = a->q_mailer;
1554174Seric 	errno = 0;
1567676Seric 	if (tTd(26, 1))
1574444Seric 	{
1584444Seric 		printf("\nrecipient: ");
1594444Seric 		printaddr(a, FALSE);
1604444Seric 	}
1614174Seric 
1624174Seric 	/* break aliasing loops */
1634174Seric 	if (AliasLevel > MAXRCRSN)
1644174Seric 	{
16558151Seric 		usrerr("554 aliasing/forwarding loop broken");
16612613Seric 		return (a);
1674174Seric 	}
1684174Seric 
1694174Seric 	/*
1704627Seric 	**  Finish setting up address structure.
1714174Seric 	*/
1724174Seric 
17316160Seric 	/* set the queue timeout */
17458737Seric 	a->q_timeout = TimeOuts.to_q_return;
1754627Seric 
17616160Seric 	/* get unquoted user for file, program or user.name check */
1779210Seric 	(void) strcpy(buf, a->q_user);
1789210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1799210Seric 	{
18054993Seric 		if (*p == '\\')
1819210Seric 			quoted = TRUE;
1829210Seric 	}
18354983Seric 	stripquotes(buf);
1849210Seric 
18557402Seric 	/* check for direct mailing to restricted mailers */
18658737Seric 	if (a->q_alias == NULL && m == ProgMailer &&
18758737Seric 	    !bitset(EF_QUEUERUN, e->e_flags))
1884174Seric 	{
18958680Seric 		a->q_flags |= QBADADDR;
19058151Seric 		usrerr("550 Cannot mail directly to programs", m->m_name);
1914174Seric 	}
1924174Seric 
1934174Seric 	/*
1944419Seric 	**  Look up this person in the recipient list.
1954419Seric 	**	If they are there already, return, otherwise continue.
1964419Seric 	**	If the list is empty, just add it.  Notice the cute
1974419Seric 	**	hack to make from addresses suppress things correctly:
1984419Seric 	**	the QDONTSEND bit will be set in the send list.
1994419Seric 	**	[Please note: the emphasis is on "hack."]
2004174Seric 	*/
2014174Seric 
2025006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2034174Seric 	{
20458294Seric 		if (sameaddr(q, a))
2054174Seric 		{
2067676Seric 			if (tTd(26, 1))
2074444Seric 			{
2084444Seric 				printf("%s in sendq: ", a->q_paddr);
2094444Seric 				printaddr(q, FALSE);
2104444Seric 			}
2114423Seric 			if (!bitset(QPRIMARY, q->q_flags))
21258065Seric 			{
21358065Seric 				if (!bitset(QDONTSEND, a->q_flags))
21458151Seric 					message("duplicate suppressed");
2154423Seric 				q->q_flags |= a->q_flags;
21658065Seric 			}
21712613Seric 			return (q);
2184174Seric 		}
2194319Seric 	}
2204174Seric 
2214319Seric 	/* add address on list */
22258884Seric 	*pq = a;
22358884Seric 	a->q_next = NULL;
2244174Seric 
2254174Seric 	/*
22657402Seric 	**  Alias the name and handle special mailer types.
2274174Seric 	*/
2284174Seric 
22953735Seric   trylocaluser:
23055354Seric 	if (tTd(29, 7))
23155354Seric 		printf("at trylocaluser %s\n", a->q_user);
23255354Seric 
23358680Seric 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
23457402Seric 		return (a);
23557402Seric 
23657402Seric 	if (m == InclMailer)
2374174Seric 	{
23857402Seric 		a->q_flags |= QDONTSEND;
23958737Seric 		if (a->q_alias == NULL && !bitset(EF_QUEUERUN, e->e_flags))
2404174Seric 		{
24158680Seric 			a->q_flags |= QBADADDR;
24258151Seric 			usrerr("550 Cannot mail directly to :include:s");
2434174Seric 		}
2444174Seric 		else
24550556Seric 		{
24659563Seric 			int ret;
24758247Seric 
24858151Seric 			message("including file %s", a->q_user);
24959563Seric 			ret = include(a->q_user, FALSE, a, sendq, e);
25059563Seric 			if (transienterror(ret))
25159563Seric 			{
25259563Seric #ifdef LOG
25359563Seric 				if (LogLevel > 2)
25459615Seric 					syslog(LOG_ERR, "%s: include %s: transient error: %e",
255*59623Seric 						e->e_id, a->q_user, errstring(ret));
25659563Seric #endif
25758247Seric 				a->q_flags |= QQUEUEUP|QDONTSEND;
25859563Seric 				usrerr("451 Cannot open %s: %s",
25959563Seric 					a->q_user, errstring(ret));
26059563Seric 			}
26159563Seric 			else if (ret != 0)
26259563Seric 			{
26359563Seric 				usrerr("550 Cannot open %s: %s",
26459563Seric 					a->q_user, errstring(ret));
26559563Seric 				a->q_flags |= QBADADDR;
26659563Seric 			}
26750556Seric 		}
2684174Seric 	}
26957642Seric 	else if (m == FileMailer)
2704174Seric 	{
2714329Seric 		struct stat stb;
2724329Seric 		extern bool writable();
2734174Seric 
27456795Seric 		p = strrchr(buf, '/');
27551317Seric 		/* check if writable or creatable */
27658737Seric 		if (a->q_alias == NULL && !bitset(EF_QUEUERUN, e->e_flags))
2774174Seric 		{
27858680Seric 			a->q_flags |= QBADADDR;
27958151Seric 			usrerr("550 Cannot mail directly to files");
2804174Seric 		}
28151317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
28258247Seric 		    (*p = '\0', safefile(buf, getruid(), S_IWRITE|S_IEXEC) != 0))
28351317Seric 		{
28458680Seric 			a->q_flags |= QBADADDR;
28558337Seric 			giveresponse(EX_CANTCREAT, m, NULL, e);
28651317Seric 		}
28751317Seric 	}
28851317Seric 
28957402Seric 	if (m != LocalMailer)
29057642Seric 	{
29157642Seric 		if (!bitset(QDONTSEND, a->q_flags))
29257642Seric 			e->e_nrcpts++;
29357402Seric 		return (a);
29457642Seric 	}
29557402Seric 
29657402Seric 	/* try aliasing */
29757402Seric 	alias(a, sendq, e);
29857402Seric 
29957402Seric # ifdef USERDB
30057402Seric 	/* if not aliased, look it up in the user database */
30158918Seric 	if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags))
30257402Seric 	{
30357402Seric 		extern int udbexpand();
30459615Seric 		extern int errno;
30557402Seric 
30657402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
30757402Seric 		{
30858247Seric 			a->q_flags |= QQUEUEUP|QDONTSEND;
30957402Seric 			if (e->e_message == NULL)
31057402Seric 				e->e_message = newstr("Deferred: user database error");
31157402Seric # ifdef LOG
31258020Seric 			if (LogLevel > 8)
313*59623Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand: %s",
314*59623Seric 					e->e_id, errstring(errno));
31557402Seric # endif
31659615Seric 			message("queued (user database error): %s",
31759615Seric 				errstring(errno));
31857642Seric 			e->e_nrcpts++;
31957402Seric 			return (a);
32057402Seric 		}
32157402Seric 	}
32257402Seric # endif
32357402Seric 
32457402Seric 	/* if it was an alias or a UDB expansion, just return now */
32558247Seric 	if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags))
32657402Seric 		return (a);
32757402Seric 
32851317Seric 	/*
32951317Seric 	**  If we have a level two config file, then pass the name through
33051317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
33151317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
33251317Seric 	**  after local aliasing has been done.
33351317Seric 	*/
33451317Seric 
33551317Seric 	if (tTd(29, 5))
33651317Seric 	{
33751317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
33851317Seric 			ConfigLevel, RewriteRules[5]);
33951317Seric 		printaddr(a, FALSE);
34051317Seric 	}
34151317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
34251317Seric 	    RewriteRules[5] != NULL)
34351317Seric 	{
34455012Seric 		maplocaluser(a, sendq, e);
34551317Seric 	}
34651317Seric 
34751317Seric 	/*
34851317Seric 	**  If it didn't get rewritten to another mailer, go ahead
34951317Seric 	**  and deliver it.
35051317Seric 	*/
35151317Seric 
35258247Seric 	if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags))
35351317Seric 	{
35455354Seric 		auto bool fuzzy;
35551317Seric 		register struct passwd *pw;
35651317Seric 		extern struct passwd *finduser();
35751317Seric 
35851317Seric 		/* warning -- finduser may trash buf */
35955354Seric 		pw = finduser(buf, &fuzzy);
36051317Seric 		if (pw == NULL)
36151317Seric 		{
36258680Seric 			a->q_flags |= QBADADDR;
36358337Seric 			giveresponse(EX_NOUSER, m, NULL, e);
36451317Seric 		}
3654174Seric 		else
3664174Seric 		{
36751317Seric 			char nbuf[MAXNAME];
3684373Seric 
36955354Seric 			if (fuzzy)
3704174Seric 			{
37153735Seric 				/* name was a fuzzy match */
37251317Seric 				a->q_user = newstr(pw->pw_name);
37353735Seric 				if (findusercount++ > 3)
37453735Seric 				{
37558680Seric 					a->q_flags |= QBADADDR;
37658151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
37753735Seric 						pw->pw_name);
37853735Seric 					return (a);
37953735Seric 				}
38053735Seric 
38153735Seric 				/* see if it aliases */
38251317Seric 				(void) strcpy(buf, pw->pw_name);
38353735Seric 				goto trylocaluser;
3844174Seric 			}
38551317Seric 			a->q_home = newstr(pw->pw_dir);
38651317Seric 			a->q_uid = pw->pw_uid;
38751317Seric 			a->q_gid = pw->pw_gid;
38859083Seric 			a->q_ruser = newstr(pw->pw_name);
38951317Seric 			a->q_flags |= QGOODUID;
39051317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
39151317Seric 			if (nbuf[0] != '\0')
39251317Seric 				a->q_fullname = newstr(nbuf);
39351317Seric 			if (!quoted)
39455012Seric 				forward(a, sendq, e);
3954174Seric 		}
3964174Seric 	}
39757642Seric 	if (!bitset(QDONTSEND, a->q_flags))
39857642Seric 		e->e_nrcpts++;
39912613Seric 	return (a);
4004174Seric }
4014174Seric /*
4024373Seric **  FINDUSER -- find the password entry for a user.
4034373Seric **
4044373Seric **	This looks a lot like getpwnam, except that it may want to
4054373Seric **	do some fancier pattern matching in /etc/passwd.
4064373Seric **
4079379Seric **	This routine contains most of the time of many sendmail runs.
4089379Seric **	It deserves to be optimized.
4099379Seric **
4104373Seric **	Parameters:
4114373Seric **		name -- the name to match against.
41255354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
41355354Seric **			was found using the fuzzy matching algorithm;
41455354Seric **			set to FALSE otherwise.
4154373Seric **
4164373Seric **	Returns:
4174373Seric **		A pointer to a pw struct.
4184373Seric **		NULL if name is unknown or ambiguous.
4194373Seric **
4204373Seric **	Side Effects:
4214407Seric **		may modify name.
4224373Seric */
4234373Seric 
4244373Seric struct passwd *
42555354Seric finduser(name, fuzzyp)
4264373Seric 	char *name;
42755354Seric 	bool *fuzzyp;
4284373Seric {
4294376Seric 	register struct passwd *pw;
4304407Seric 	register char *p;
43115325Seric 	extern struct passwd *getpwent();
43215325Seric 	extern struct passwd *getpwnam();
4334373Seric 
43455354Seric 	if (tTd(29, 4))
43555354Seric 		printf("finduser(%s): ", name);
43655354Seric 
43755354Seric 	*fuzzyp = FALSE;
4384407Seric 
43925777Seric 	/* look up this login name using fast path */
44012634Seric 	if ((pw = getpwnam(name)) != NULL)
44155354Seric 	{
44255354Seric 		if (tTd(29, 4))
44355354Seric 			printf("found (non-fuzzy)\n");
44412634Seric 		return (pw);
44555354Seric 	}
44612634Seric 
44753735Seric #ifdef MATCHGECOS
44853735Seric 	/* see if fuzzy matching allowed */
44953735Seric 	if (!MatchGecos)
45055354Seric 	{
45155354Seric 		if (tTd(29, 4))
45255354Seric 			printf("not found (fuzzy disabled)\n");
45353735Seric 		return NULL;
45455354Seric 	}
45553735Seric 
45612634Seric 	/* search for a matching full name instead */
45725777Seric 	for (p = name; *p != '\0'; p++)
45825777Seric 	{
45925777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
46025777Seric 			*p = ' ';
46125777Seric 	}
46223107Seric 	(void) setpwent();
4634376Seric 	while ((pw = getpwent()) != NULL)
4644376Seric 	{
4654998Seric 		char buf[MAXNAME];
4664376Seric 
4674998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
46856795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
4694381Seric 		{
47055354Seric 			if (tTd(29, 4))
47155354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
47258151Seric 			message("sending to login name %s", pw->pw_name);
47355354Seric 			*fuzzyp = TRUE;
4744376Seric 			return (pw);
4754377Seric 		}
4764376Seric 	}
47755354Seric 	if (tTd(29, 4))
47855354Seric 		printf("no fuzzy match found\n");
47959015Seric #else
48059015Seric 	if (tTd(29, 4))
48159015Seric 		printf("not found (fuzzy disabled)\n");
48259015Seric #endif
4834376Seric 	return (NULL);
4844373Seric }
4854373Seric /*
4864329Seric **  WRITABLE -- predicate returning if the file is writable.
4874329Seric **
4884329Seric **	This routine must duplicate the algorithm in sys/fio.c.
4894329Seric **	Unfortunately, we cannot use the access call since we
4904329Seric **	won't necessarily be the real uid when we try to
4914329Seric **	actually open the file.
4924329Seric **
4934329Seric **	Notice that ANY file with ANY execute bit is automatically
4944329Seric **	not writable.  This is also enforced by mailfile.
4954329Seric **
4964329Seric **	Parameters:
4974329Seric **		s -- pointer to a stat struct for the file.
4984329Seric **
4994329Seric **	Returns:
5004329Seric **		TRUE -- if we will be able to write this file.
5014329Seric **		FALSE -- if we cannot write this file.
5024329Seric **
5034329Seric **	Side Effects:
5044329Seric **		none.
5054329Seric */
5064329Seric 
5074329Seric bool
5084329Seric writable(s)
5094329Seric 	register struct stat *s;
5104329Seric {
51155372Seric 	uid_t euid;
51255372Seric 	gid_t 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:
55257136Seric **		open error status
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 
56157136Seric int
56255012Seric include(fname, forwarding, ctladdr, sendq, e)
5634174Seric 	char *fname;
56453037Seric 	bool forwarding;
5654399Seric 	ADDRESS *ctladdr;
5665006Seric 	ADDRESS **sendq;
56755012Seric 	ENVELOPE *e;
5684174Seric {
5694174Seric 	register FILE *fp;
57055012Seric 	char *oldto = e->e_to;
5719379Seric 	char *oldfilename = FileName;
5729379Seric 	int oldlinenumber = LineNumber;
57353037Seric 	register EVENT *ev = NULL;
57458082Seric 	int nincludes;
57558247Seric 	int ret;
57653037Seric 	char buf[MAXLINE];
57753037Seric 	static int includetimeout();
5784174Seric 
57957186Seric 	if (tTd(27, 2))
58057186Seric 		printf("include(%s)\n", fname);
58157186Seric 
58253037Seric 	/*
58353037Seric 	**  If home directory is remote mounted but server is down,
58453037Seric 	**  this can hang or give errors; use a timeout to avoid this
58553037Seric 	*/
58653037Seric 
58753037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
58853037Seric 	{
58953037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
59053037Seric 		errno = 0;
59153037Seric 		usrerr("451 open timeout on %s", fname);
59257136Seric 		return ETIMEDOUT;
59353037Seric 	}
59453037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
59553037Seric 
59653037Seric 	/* if forwarding, the input file must be marked safe */
59758247Seric 	if (forwarding && (ret = safefile(fname, ctladdr->q_uid, S_IREAD)) != 0)
59853037Seric 	{
59953037Seric 		/* don't use this .forward file */
60053037Seric 		clrevent(ev);
60157186Seric 		if (tTd(27, 4))
60258247Seric 			printf("include: not safe (uid=%d): %s\n",
60358247Seric 				ctladdr->q_uid, errstring(ret));
60458247Seric 		return ret;
60553037Seric 	}
60653037Seric 
6074174Seric 	fp = fopen(fname, "r");
6084174Seric 	if (fp == NULL)
6094174Seric 	{
61057136Seric 		int ret = errno;
61157136Seric 
61258061Seric 		clrevent(ev);
61357136Seric 		return ret;
6144174Seric 	}
61553037Seric 
6164406Seric 	if (getctladdr(ctladdr) == NULL)
6174406Seric 	{
6184406Seric 		struct stat st;
6194174Seric 
6204406Seric 		if (fstat(fileno(fp), &st) < 0)
62158061Seric 		{
62258061Seric 			int ret = errno;
62358061Seric 
62458061Seric 			clrevent(ev);
6254406Seric 			syserr("Cannot fstat %s!", fname);
62658061Seric 			return ret;
62758061Seric 		}
6284406Seric 		ctladdr->q_uid = st.st_uid;
6294406Seric 		ctladdr->q_gid = st.st_gid;
6304406Seric 		ctladdr->q_flags |= QGOODUID;
6314406Seric 	}
6324406Seric 
63353037Seric 	clrevent(ev);
63453037Seric 
63558092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
63658092Seric 	{
63758092Seric 		/* don't do any more now */
63858868Seric 		ctladdr->q_flags |= QVERIFIED;
63958884Seric 		e->e_nrcpts++;
64058680Seric 		xfclose(fp, "include", fname);
64158092Seric 		return 0;
64258092Seric 	}
64358092Seric 
6444174Seric 	/* read the file -- each line is a comma-separated list. */
6459379Seric 	FileName = fname;
6469379Seric 	LineNumber = 0;
64758082Seric 	ctladdr->q_flags &= ~QSELFREF;
64858082Seric 	nincludes = 0;
6494174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6504174Seric 	{
65156795Seric 		register char *p = strchr(buf, '\n');
6524174Seric 
65340963Sbostic 		LineNumber++;
6544174Seric 		if (p != NULL)
6554174Seric 			*p = '\0';
65657186Seric 		if (buf[0] == '#' || buf[0] == '\0')
65757139Seric 			continue;
65858008Seric 		e->e_to = NULL;
65958151Seric 		message("%s to %s",
66053037Seric 			forwarding ? "forwarding" : "sending", buf);
66157977Seric #ifdef LOG
66258020Seric 		if (forwarding && LogLevel > 9)
66357977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
66457977Seric 				e->e_id, oldto, buf);
66557977Seric #endif
66657977Seric 
6674176Seric 		AliasLevel++;
66858082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
6694176Seric 		AliasLevel--;
6704174Seric 	}
67158082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
67258065Seric 	{
67358065Seric 		if (tTd(27, 5))
67458065Seric 		{
67558065Seric 			printf("include: QDONTSEND ");
67658065Seric 			printaddr(ctladdr, FALSE);
67758065Seric 		}
67858065Seric 		ctladdr->q_flags |= QDONTSEND;
67958065Seric 	}
6804174Seric 
68158680Seric 	(void) xfclose(fp, "include", fname);
6829379Seric 	FileName = oldfilename;
6839379Seric 	LineNumber = oldlinenumber;
68457136Seric 	return 0;
6854174Seric }
68653037Seric 
68753037Seric static
68853037Seric includetimeout()
68953037Seric {
69053037Seric 	longjmp(CtxIncludeTimeout, 1);
69153037Seric }
6924324Seric /*
6934324Seric **  SENDTOARGV -- send to an argument vector.
6944324Seric **
6954324Seric **	Parameters:
6964324Seric **		argv -- argument vector to send to.
69758247Seric **		e -- the current envelope.
6984324Seric **
6994324Seric **	Returns:
7004324Seric **		none.
7014324Seric **
7024324Seric **	Side Effects:
7034324Seric **		puts all addresses on the argument vector onto the
7044324Seric **			send queue.
7054324Seric */
7064324Seric 
70755012Seric sendtoargv(argv, e)
7084324Seric 	register char **argv;
70955012Seric 	register ENVELOPE *e;
7104324Seric {
7114324Seric 	register char *p;
7124324Seric 
7134324Seric 	while ((p = *argv++) != NULL)
7144324Seric 	{
71558082Seric 		(void) sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7164324Seric 	}
7174324Seric }
7184399Seric /*
7194399Seric **  GETCTLADDR -- get controlling address from an address header.
7204399Seric **
7214399Seric **	If none, get one corresponding to the effective userid.
7224399Seric **
7234399Seric **	Parameters:
7244399Seric **		a -- the address to find the controller of.
7254399Seric **
7264399Seric **	Returns:
7274399Seric **		the controlling address.
7284399Seric **
7294399Seric **	Side Effects:
7304399Seric **		none.
7314399Seric */
7324399Seric 
7334399Seric ADDRESS *
7344399Seric getctladdr(a)
7354399Seric 	register ADDRESS *a;
7364399Seric {
7374404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7384399Seric 		a = a->q_alias;
7394399Seric 	return (a);
7404399Seric }
741