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*59563Seric static char sccsid[] = "@(#)recipient.c	6.38 (Berkeley) 04/30/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 		{
246*59563Seric 			int ret;
24758247Seric 
24858151Seric 			message("including file %s", a->q_user);
249*59563Seric 			ret = include(a->q_user, FALSE, a, sendq, e);
250*59563Seric 			if (transienterror(ret))
251*59563Seric 			{
252*59563Seric #ifdef LOG
253*59563Seric 				if (LogLevel > 2)
254*59563Seric 					syslog(LOG_NOTICE, "%s: include %s: transient error: %e",
255*59563Seric 						e->e_id, a->q_user);
256*59563Seric #endif
25758247Seric 				a->q_flags |= QQUEUEUP|QDONTSEND;
258*59563Seric 				usrerr("451 Cannot open %s: %s",
259*59563Seric 					a->q_user, errstring(ret));
260*59563Seric 			}
261*59563Seric 			else if (ret != 0)
262*59563Seric 			{
263*59563Seric 				usrerr("550 Cannot open %s: %s",
264*59563Seric 					a->q_user, errstring(ret));
265*59563Seric 				a->q_flags |= QBADADDR;
266*59563Seric 			}
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();
30457402Seric 
30557402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
30657402Seric 		{
30758247Seric 			a->q_flags |= QQUEUEUP|QDONTSEND;
30857402Seric 			if (e->e_message == NULL)
30957402Seric 				e->e_message = newstr("Deferred: user database error");
31057402Seric # ifdef LOG
31158020Seric 			if (LogLevel > 8)
31257402Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand",
31357402Seric 					e->e_id);
31457402Seric # endif
31558151Seric 			message("queued (user database error)");
31657642Seric 			e->e_nrcpts++;
31757402Seric 			return (a);
31857402Seric 		}
31957402Seric 	}
32057402Seric # endif
32157402Seric 
32257402Seric 	/* if it was an alias or a UDB expansion, just return now */
32358247Seric 	if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags))
32457402Seric 		return (a);
32557402Seric 
32651317Seric 	/*
32751317Seric 	**  If we have a level two config file, then pass the name through
32851317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
32951317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
33051317Seric 	**  after local aliasing has been done.
33151317Seric 	*/
33251317Seric 
33351317Seric 	if (tTd(29, 5))
33451317Seric 	{
33551317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
33651317Seric 			ConfigLevel, RewriteRules[5]);
33751317Seric 		printaddr(a, FALSE);
33851317Seric 	}
33951317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
34051317Seric 	    RewriteRules[5] != NULL)
34151317Seric 	{
34255012Seric 		maplocaluser(a, sendq, e);
34351317Seric 	}
34451317Seric 
34551317Seric 	/*
34651317Seric 	**  If it didn't get rewritten to another mailer, go ahead
34751317Seric 	**  and deliver it.
34851317Seric 	*/
34951317Seric 
35058247Seric 	if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags))
35151317Seric 	{
35255354Seric 		auto bool fuzzy;
35351317Seric 		register struct passwd *pw;
35451317Seric 		extern struct passwd *finduser();
35551317Seric 
35651317Seric 		/* warning -- finduser may trash buf */
35755354Seric 		pw = finduser(buf, &fuzzy);
35851317Seric 		if (pw == NULL)
35951317Seric 		{
36058680Seric 			a->q_flags |= QBADADDR;
36158337Seric 			giveresponse(EX_NOUSER, m, NULL, e);
36251317Seric 		}
3634174Seric 		else
3644174Seric 		{
36551317Seric 			char nbuf[MAXNAME];
3664373Seric 
36755354Seric 			if (fuzzy)
3684174Seric 			{
36953735Seric 				/* name was a fuzzy match */
37051317Seric 				a->q_user = newstr(pw->pw_name);
37153735Seric 				if (findusercount++ > 3)
37253735Seric 				{
37358680Seric 					a->q_flags |= QBADADDR;
37458151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
37553735Seric 						pw->pw_name);
37653735Seric 					return (a);
37753735Seric 				}
37853735Seric 
37953735Seric 				/* see if it aliases */
38051317Seric 				(void) strcpy(buf, pw->pw_name);
38153735Seric 				goto trylocaluser;
3824174Seric 			}
38351317Seric 			a->q_home = newstr(pw->pw_dir);
38451317Seric 			a->q_uid = pw->pw_uid;
38551317Seric 			a->q_gid = pw->pw_gid;
38659083Seric 			a->q_ruser = newstr(pw->pw_name);
38751317Seric 			a->q_flags |= QGOODUID;
38851317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
38951317Seric 			if (nbuf[0] != '\0')
39051317Seric 				a->q_fullname = newstr(nbuf);
39151317Seric 			if (!quoted)
39255012Seric 				forward(a, sendq, e);
3934174Seric 		}
3944174Seric 	}
39557642Seric 	if (!bitset(QDONTSEND, a->q_flags))
39657642Seric 		e->e_nrcpts++;
39712613Seric 	return (a);
3984174Seric }
3994174Seric /*
4004373Seric **  FINDUSER -- find the password entry for a user.
4014373Seric **
4024373Seric **	This looks a lot like getpwnam, except that it may want to
4034373Seric **	do some fancier pattern matching in /etc/passwd.
4044373Seric **
4059379Seric **	This routine contains most of the time of many sendmail runs.
4069379Seric **	It deserves to be optimized.
4079379Seric **
4084373Seric **	Parameters:
4094373Seric **		name -- the name to match against.
41055354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
41155354Seric **			was found using the fuzzy matching algorithm;
41255354Seric **			set to FALSE otherwise.
4134373Seric **
4144373Seric **	Returns:
4154373Seric **		A pointer to a pw struct.
4164373Seric **		NULL if name is unknown or ambiguous.
4174373Seric **
4184373Seric **	Side Effects:
4194407Seric **		may modify name.
4204373Seric */
4214373Seric 
4224373Seric struct passwd *
42355354Seric finduser(name, fuzzyp)
4244373Seric 	char *name;
42555354Seric 	bool *fuzzyp;
4264373Seric {
4274376Seric 	register struct passwd *pw;
4284407Seric 	register char *p;
42915325Seric 	extern struct passwd *getpwent();
43015325Seric 	extern struct passwd *getpwnam();
4314373Seric 
43255354Seric 	if (tTd(29, 4))
43355354Seric 		printf("finduser(%s): ", name);
43455354Seric 
43555354Seric 	*fuzzyp = FALSE;
4364407Seric 
43725777Seric 	/* look up this login name using fast path */
43812634Seric 	if ((pw = getpwnam(name)) != NULL)
43955354Seric 	{
44055354Seric 		if (tTd(29, 4))
44155354Seric 			printf("found (non-fuzzy)\n");
44212634Seric 		return (pw);
44355354Seric 	}
44412634Seric 
44553735Seric #ifdef MATCHGECOS
44653735Seric 	/* see if fuzzy matching allowed */
44753735Seric 	if (!MatchGecos)
44855354Seric 	{
44955354Seric 		if (tTd(29, 4))
45055354Seric 			printf("not found (fuzzy disabled)\n");
45153735Seric 		return NULL;
45255354Seric 	}
45353735Seric 
45412634Seric 	/* search for a matching full name instead */
45525777Seric 	for (p = name; *p != '\0'; p++)
45625777Seric 	{
45725777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
45825777Seric 			*p = ' ';
45925777Seric 	}
46023107Seric 	(void) setpwent();
4614376Seric 	while ((pw = getpwent()) != NULL)
4624376Seric 	{
4634998Seric 		char buf[MAXNAME];
4644376Seric 
4654998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
46656795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
4674381Seric 		{
46855354Seric 			if (tTd(29, 4))
46955354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
47058151Seric 			message("sending to login name %s", pw->pw_name);
47155354Seric 			*fuzzyp = TRUE;
4724376Seric 			return (pw);
4734377Seric 		}
4744376Seric 	}
47555354Seric 	if (tTd(29, 4))
47655354Seric 		printf("no fuzzy match found\n");
47759015Seric #else
47859015Seric 	if (tTd(29, 4))
47959015Seric 		printf("not found (fuzzy disabled)\n");
48059015Seric #endif
4814376Seric 	return (NULL);
4824373Seric }
4834373Seric /*
4844329Seric **  WRITABLE -- predicate returning if the file is writable.
4854329Seric **
4864329Seric **	This routine must duplicate the algorithm in sys/fio.c.
4874329Seric **	Unfortunately, we cannot use the access call since we
4884329Seric **	won't necessarily be the real uid when we try to
4894329Seric **	actually open the file.
4904329Seric **
4914329Seric **	Notice that ANY file with ANY execute bit is automatically
4924329Seric **	not writable.  This is also enforced by mailfile.
4934329Seric **
4944329Seric **	Parameters:
4954329Seric **		s -- pointer to a stat struct for the file.
4964329Seric **
4974329Seric **	Returns:
4984329Seric **		TRUE -- if we will be able to write this file.
4994329Seric **		FALSE -- if we cannot write this file.
5004329Seric **
5014329Seric **	Side Effects:
5024329Seric **		none.
5034329Seric */
5044329Seric 
5054329Seric bool
5064329Seric writable(s)
5074329Seric 	register struct stat *s;
5084329Seric {
50955372Seric 	uid_t euid;
51055372Seric 	gid_t egid;
5114329Seric 	int bits;
5124329Seric 
5134329Seric 	if (bitset(0111, s->st_mode))
5144329Seric 		return (FALSE);
5154329Seric 	euid = getruid();
5164329Seric 	egid = getrgid();
5174329Seric 	if (geteuid() == 0)
5184329Seric 	{
5194329Seric 		if (bitset(S_ISUID, s->st_mode))
5204329Seric 			euid = s->st_uid;
5214329Seric 		if (bitset(S_ISGID, s->st_mode))
5224329Seric 			egid = s->st_gid;
5234329Seric 	}
5244329Seric 
5254329Seric 	if (euid == 0)
5264329Seric 		return (TRUE);
5274329Seric 	bits = S_IWRITE;
5284329Seric 	if (euid != s->st_uid)
5294329Seric 	{
5304329Seric 		bits >>= 3;
5314329Seric 		if (egid != s->st_gid)
5324329Seric 			bits >>= 3;
5334329Seric 	}
5344329Seric 	return ((s->st_mode & bits) != 0);
5354329Seric }
5364329Seric /*
5374174Seric **  INCLUDE -- handle :include: specification.
5384174Seric **
5394174Seric **	Parameters:
5404174Seric **		fname -- filename to include.
54153037Seric **		forwarding -- if TRUE, we are reading a .forward file.
54253037Seric **			if FALSE, it's a :include: file.
5434399Seric **		ctladdr -- address template to use to fill in these
5444399Seric **			addresses -- effective user/group id are
5454399Seric **			the important things.
5465006Seric **		sendq -- a pointer to the head of the send queue
5475006Seric **			to put these addresses in.
5484174Seric **
5494174Seric **	Returns:
55057136Seric **		open error status
5514174Seric **
5524174Seric **	Side Effects:
5534174Seric **		reads the :include: file and sends to everyone
5544174Seric **		listed in that file.
5554174Seric */
5564174Seric 
55753037Seric static jmp_buf	CtxIncludeTimeout;
55853037Seric 
55957136Seric int
56055012Seric include(fname, forwarding, ctladdr, sendq, e)
5614174Seric 	char *fname;
56253037Seric 	bool forwarding;
5634399Seric 	ADDRESS *ctladdr;
5645006Seric 	ADDRESS **sendq;
56555012Seric 	ENVELOPE *e;
5664174Seric {
5674174Seric 	register FILE *fp;
56855012Seric 	char *oldto = e->e_to;
5699379Seric 	char *oldfilename = FileName;
5709379Seric 	int oldlinenumber = LineNumber;
57153037Seric 	register EVENT *ev = NULL;
57258082Seric 	int nincludes;
57358247Seric 	int ret;
57453037Seric 	char buf[MAXLINE];
57553037Seric 	static int includetimeout();
5764174Seric 
57757186Seric 	if (tTd(27, 2))
57857186Seric 		printf("include(%s)\n", fname);
57957186Seric 
58053037Seric 	/*
58153037Seric 	**  If home directory is remote mounted but server is down,
58253037Seric 	**  this can hang or give errors; use a timeout to avoid this
58353037Seric 	*/
58453037Seric 
58553037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
58653037Seric 	{
58753037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
58853037Seric 		errno = 0;
58953037Seric 		usrerr("451 open timeout on %s", fname);
59057136Seric 		return ETIMEDOUT;
59153037Seric 	}
59253037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
59353037Seric 
59453037Seric 	/* if forwarding, the input file must be marked safe */
59558247Seric 	if (forwarding && (ret = safefile(fname, ctladdr->q_uid, S_IREAD)) != 0)
59653037Seric 	{
59753037Seric 		/* don't use this .forward file */
59853037Seric 		clrevent(ev);
59957186Seric 		if (tTd(27, 4))
60058247Seric 			printf("include: not safe (uid=%d): %s\n",
60158247Seric 				ctladdr->q_uid, errstring(ret));
60258247Seric 		return ret;
60353037Seric 	}
60453037Seric 
6054174Seric 	fp = fopen(fname, "r");
6064174Seric 	if (fp == NULL)
6074174Seric 	{
60857136Seric 		int ret = errno;
60957136Seric 
61058061Seric 		clrevent(ev);
61157136Seric 		return ret;
6124174Seric 	}
61353037Seric 
6144406Seric 	if (getctladdr(ctladdr) == NULL)
6154406Seric 	{
6164406Seric 		struct stat st;
6174174Seric 
6184406Seric 		if (fstat(fileno(fp), &st) < 0)
61958061Seric 		{
62058061Seric 			int ret = errno;
62158061Seric 
62258061Seric 			clrevent(ev);
6234406Seric 			syserr("Cannot fstat %s!", fname);
62458061Seric 			return ret;
62558061Seric 		}
6264406Seric 		ctladdr->q_uid = st.st_uid;
6274406Seric 		ctladdr->q_gid = st.st_gid;
6284406Seric 		ctladdr->q_flags |= QGOODUID;
6294406Seric 	}
6304406Seric 
63153037Seric 	clrevent(ev);
63253037Seric 
63358092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
63458092Seric 	{
63558092Seric 		/* don't do any more now */
63658868Seric 		ctladdr->q_flags |= QVERIFIED;
63758884Seric 		e->e_nrcpts++;
63858680Seric 		xfclose(fp, "include", fname);
63958092Seric 		return 0;
64058092Seric 	}
64158092Seric 
6424174Seric 	/* read the file -- each line is a comma-separated list. */
6439379Seric 	FileName = fname;
6449379Seric 	LineNumber = 0;
64558082Seric 	ctladdr->q_flags &= ~QSELFREF;
64658082Seric 	nincludes = 0;
6474174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6484174Seric 	{
64956795Seric 		register char *p = strchr(buf, '\n');
6504174Seric 
65140963Sbostic 		LineNumber++;
6524174Seric 		if (p != NULL)
6534174Seric 			*p = '\0';
65457186Seric 		if (buf[0] == '#' || buf[0] == '\0')
65557139Seric 			continue;
65658008Seric 		e->e_to = NULL;
65758151Seric 		message("%s to %s",
65853037Seric 			forwarding ? "forwarding" : "sending", buf);
65957977Seric #ifdef LOG
66058020Seric 		if (forwarding && LogLevel > 9)
66157977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
66257977Seric 				e->e_id, oldto, buf);
66357977Seric #endif
66457977Seric 
6654176Seric 		AliasLevel++;
66658082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
6674176Seric 		AliasLevel--;
6684174Seric 	}
66958082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
67058065Seric 	{
67158065Seric 		if (tTd(27, 5))
67258065Seric 		{
67358065Seric 			printf("include: QDONTSEND ");
67458065Seric 			printaddr(ctladdr, FALSE);
67558065Seric 		}
67658065Seric 		ctladdr->q_flags |= QDONTSEND;
67758065Seric 	}
6784174Seric 
67958680Seric 	(void) xfclose(fp, "include", fname);
6809379Seric 	FileName = oldfilename;
6819379Seric 	LineNumber = oldlinenumber;
68257136Seric 	return 0;
6834174Seric }
68453037Seric 
68553037Seric static
68653037Seric includetimeout()
68753037Seric {
68853037Seric 	longjmp(CtxIncludeTimeout, 1);
68953037Seric }
6904324Seric /*
6914324Seric **  SENDTOARGV -- send to an argument vector.
6924324Seric **
6934324Seric **	Parameters:
6944324Seric **		argv -- argument vector to send to.
69558247Seric **		e -- the current envelope.
6964324Seric **
6974324Seric **	Returns:
6984324Seric **		none.
6994324Seric **
7004324Seric **	Side Effects:
7014324Seric **		puts all addresses on the argument vector onto the
7024324Seric **			send queue.
7034324Seric */
7044324Seric 
70555012Seric sendtoargv(argv, e)
7064324Seric 	register char **argv;
70755012Seric 	register ENVELOPE *e;
7084324Seric {
7094324Seric 	register char *p;
7104324Seric 
7114324Seric 	while ((p = *argv++) != NULL)
7124324Seric 	{
71358082Seric 		(void) sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7144324Seric 	}
7154324Seric }
7164399Seric /*
7174399Seric **  GETCTLADDR -- get controlling address from an address header.
7184399Seric **
7194399Seric **	If none, get one corresponding to the effective userid.
7204399Seric **
7214399Seric **	Parameters:
7224399Seric **		a -- the address to find the controller of.
7234399Seric **
7244399Seric **	Returns:
7254399Seric **		the controlling address.
7264399Seric **
7274399Seric **	Side Effects:
7284399Seric **		none.
7294399Seric */
7304399Seric 
7314399Seric ADDRESS *
7324399Seric getctladdr(a)
7334399Seric 	register ADDRESS *a;
7344399Seric {
7354404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7364399Seric 		a = a->q_alias;
7374399Seric 	return (a);
7384399Seric }
739