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*63581Seric static char sccsid[] = "@(#)recipient.c	6.42 (Berkeley) 06/24/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;
1024324Seric 
1034324Seric 		al = a->q_next;
10455012Seric 		a = recipient(a, sendq, e);
1054993Seric 
1064998Seric 		/* arrange to inherit full name */
1074998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1084998Seric 			a->q_fullname = ctladdr->q_fullname;
10958082Seric 		naddrs++;
1104174Seric 	}
1114324Seric 
11255012Seric 	e->e_to = NULL;
11358082Seric 	return (naddrs);
1144174Seric }
1154174Seric /*
1164174Seric **  RECIPIENT -- Designate a message recipient
1174174Seric **
1184174Seric **	Saves the named person for future mailing.
1194174Seric **
1204174Seric **	Parameters:
1214174Seric **		a -- the (preparsed) address header for the recipient.
1225006Seric **		sendq -- a pointer to the head of a queue to put the
1235006Seric **			recipient in.  Duplicate supression is done
1245006Seric **			in this queue.
12557731Seric **		e -- the current envelope.
1264174Seric **
1274174Seric **	Returns:
12812613Seric **		The actual address in the queue.  This will be "a" if
12912613Seric **		the address is not a duplicate, else the original address.
1304174Seric **
1314174Seric **	Side Effects:
1324174Seric **		none.
1334174Seric */
1344174Seric 
13512613Seric ADDRESS *
13655012Seric recipient(a, sendq, e)
1374174Seric 	register ADDRESS *a;
1385006Seric 	register ADDRESS **sendq;
13955012Seric 	register ENVELOPE *e;
1404174Seric {
1414174Seric 	register ADDRESS *q;
1424319Seric 	ADDRESS **pq;
1434174Seric 	register struct mailer *m;
1449210Seric 	register char *p;
1459210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
14653735Seric 	int findusercount = 0;
1479210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
14858247Seric 	extern int safefile();
1494174Seric 
15055012Seric 	e->e_to = a->q_paddr;
1514600Seric 	m = a->q_mailer;
1524174Seric 	errno = 0;
1537676Seric 	if (tTd(26, 1))
1544444Seric 	{
1554444Seric 		printf("\nrecipient: ");
1564444Seric 		printaddr(a, FALSE);
1574444Seric 	}
1584174Seric 
1594174Seric 	/* break aliasing loops */
1604174Seric 	if (AliasLevel > MAXRCRSN)
1614174Seric 	{
16258151Seric 		usrerr("554 aliasing/forwarding loop broken");
16312613Seric 		return (a);
1644174Seric 	}
1654174Seric 
1664174Seric 	/*
1674627Seric 	**  Finish setting up address structure.
1684174Seric 	*/
1694174Seric 
17016160Seric 	/* set the queue timeout */
17158737Seric 	a->q_timeout = TimeOuts.to_q_return;
1724627Seric 
17316160Seric 	/* get unquoted user for file, program or user.name check */
1749210Seric 	(void) strcpy(buf, a->q_user);
1759210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1769210Seric 	{
17754993Seric 		if (*p == '\\')
1789210Seric 			quoted = TRUE;
1799210Seric 	}
18054983Seric 	stripquotes(buf);
1819210Seric 
18257402Seric 	/* check for direct mailing to restricted mailers */
18358737Seric 	if (a->q_alias == NULL && m == ProgMailer &&
18458737Seric 	    !bitset(EF_QUEUERUN, e->e_flags))
1854174Seric 	{
18658680Seric 		a->q_flags |= QBADADDR;
18758151Seric 		usrerr("550 Cannot mail directly to programs", m->m_name);
1884174Seric 	}
1894174Seric 
1904174Seric 	/*
1914419Seric 	**  Look up this person in the recipient list.
1924419Seric 	**	If they are there already, return, otherwise continue.
1934419Seric 	**	If the list is empty, just add it.  Notice the cute
1944419Seric 	**	hack to make from addresses suppress things correctly:
1954419Seric 	**	the QDONTSEND bit will be set in the send list.
1964419Seric 	**	[Please note: the emphasis is on "hack."]
1974174Seric 	*/
1984174Seric 
1995006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2004174Seric 	{
20158294Seric 		if (sameaddr(q, a))
2024174Seric 		{
2037676Seric 			if (tTd(26, 1))
2044444Seric 			{
2054444Seric 				printf("%s in sendq: ", a->q_paddr);
2064444Seric 				printaddr(q, FALSE);
2074444Seric 			}
2084423Seric 			if (!bitset(QPRIMARY, q->q_flags))
20958065Seric 			{
21058065Seric 				if (!bitset(QDONTSEND, a->q_flags))
21158151Seric 					message("duplicate suppressed");
2124423Seric 				q->q_flags |= a->q_flags;
21358065Seric 			}
21412613Seric 			return (q);
2154174Seric 		}
2164319Seric 	}
2174174Seric 
2184319Seric 	/* add address on list */
21958884Seric 	*pq = a;
22058884Seric 	a->q_next = NULL;
2214174Seric 
2224174Seric 	/*
22357402Seric 	**  Alias the name and handle special mailer types.
2244174Seric 	*/
2254174Seric 
22653735Seric   trylocaluser:
22755354Seric 	if (tTd(29, 7))
22855354Seric 		printf("at trylocaluser %s\n", a->q_user);
22955354Seric 
23058680Seric 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
23157402Seric 		return (a);
23257402Seric 
23357402Seric 	if (m == InclMailer)
2344174Seric 	{
23557402Seric 		a->q_flags |= QDONTSEND;
23658737Seric 		if (a->q_alias == NULL && !bitset(EF_QUEUERUN, e->e_flags))
2374174Seric 		{
23858680Seric 			a->q_flags |= QBADADDR;
23958151Seric 			usrerr("550 Cannot mail directly to :include:s");
2404174Seric 		}
2414174Seric 		else
24250556Seric 		{
24359563Seric 			int ret;
24458247Seric 
24558151Seric 			message("including file %s", a->q_user);
24659563Seric 			ret = include(a->q_user, FALSE, a, sendq, e);
24759563Seric 			if (transienterror(ret))
24859563Seric 			{
24959563Seric #ifdef LOG
25059563Seric 				if (LogLevel > 2)
25159615Seric 					syslog(LOG_ERR, "%s: include %s: transient error: %e",
25259623Seric 						e->e_id, a->q_user, errstring(ret));
25359563Seric #endif
25458247Seric 				a->q_flags |= QQUEUEUP|QDONTSEND;
25559563Seric 				usrerr("451 Cannot open %s: %s",
25659563Seric 					a->q_user, errstring(ret));
25759563Seric 			}
25859563Seric 			else if (ret != 0)
25959563Seric 			{
26059563Seric 				usrerr("550 Cannot open %s: %s",
26159563Seric 					a->q_user, errstring(ret));
26259563Seric 				a->q_flags |= QBADADDR;
26359563Seric 			}
26450556Seric 		}
2654174Seric 	}
26657642Seric 	else if (m == FileMailer)
2674174Seric 	{
2684329Seric 		struct stat stb;
2694329Seric 		extern bool writable();
2704174Seric 
27156795Seric 		p = strrchr(buf, '/');
27251317Seric 		/* check if writable or creatable */
27358737Seric 		if (a->q_alias == NULL && !bitset(EF_QUEUERUN, e->e_flags))
2744174Seric 		{
27558680Seric 			a->q_flags |= QBADADDR;
27658151Seric 			usrerr("550 Cannot mail directly to files");
2774174Seric 		}
27851317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
27958247Seric 		    (*p = '\0', safefile(buf, getruid(), S_IWRITE|S_IEXEC) != 0))
28051317Seric 		{
28158680Seric 			a->q_flags |= QBADADDR;
28258337Seric 			giveresponse(EX_CANTCREAT, m, NULL, e);
28351317Seric 		}
28451317Seric 	}
28551317Seric 
28657402Seric 	if (m != LocalMailer)
28757642Seric 	{
28857642Seric 		if (!bitset(QDONTSEND, a->q_flags))
28957642Seric 			e->e_nrcpts++;
29057402Seric 		return (a);
29157642Seric 	}
29257402Seric 
29357402Seric 	/* try aliasing */
29457402Seric 	alias(a, sendq, e);
29557402Seric 
29657402Seric # ifdef USERDB
29757402Seric 	/* if not aliased, look it up in the user database */
29858918Seric 	if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags))
29957402Seric 	{
30057402Seric 		extern int udbexpand();
30159615Seric 		extern int errno;
30257402Seric 
30357402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
30457402Seric 		{
30558247Seric 			a->q_flags |= QQUEUEUP|QDONTSEND;
30657402Seric 			if (e->e_message == NULL)
30757402Seric 				e->e_message = newstr("Deferred: user database error");
30857402Seric # ifdef LOG
30958020Seric 			if (LogLevel > 8)
31059623Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand: %s",
31159623Seric 					e->e_id, errstring(errno));
31257402Seric # endif
31359615Seric 			message("queued (user database error): %s",
31459615Seric 				errstring(errno));
31557642Seric 			e->e_nrcpts++;
31657402Seric 			return (a);
31757402Seric 		}
31857402Seric 	}
31957402Seric # endif
32057402Seric 
32157402Seric 	/* if it was an alias or a UDB expansion, just return now */
32258247Seric 	if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags))
32357402Seric 		return (a);
32457402Seric 
32551317Seric 	/*
32651317Seric 	**  If we have a level two config file, then pass the name through
32751317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
32851317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
32951317Seric 	**  after local aliasing has been done.
33051317Seric 	*/
33151317Seric 
33251317Seric 	if (tTd(29, 5))
33351317Seric 	{
33451317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
33551317Seric 			ConfigLevel, RewriteRules[5]);
33651317Seric 		printaddr(a, FALSE);
33751317Seric 	}
33851317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
33951317Seric 	    RewriteRules[5] != NULL)
34051317Seric 	{
34155012Seric 		maplocaluser(a, sendq, e);
34251317Seric 	}
34351317Seric 
34451317Seric 	/*
34551317Seric 	**  If it didn't get rewritten to another mailer, go ahead
34651317Seric 	**  and deliver it.
34751317Seric 	*/
34851317Seric 
34958247Seric 	if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags))
35051317Seric 	{
35155354Seric 		auto bool fuzzy;
35251317Seric 		register struct passwd *pw;
35351317Seric 		extern struct passwd *finduser();
35451317Seric 
35551317Seric 		/* warning -- finduser may trash buf */
35655354Seric 		pw = finduser(buf, &fuzzy);
35751317Seric 		if (pw == NULL)
35851317Seric 		{
35958680Seric 			a->q_flags |= QBADADDR;
36058337Seric 			giveresponse(EX_NOUSER, m, NULL, e);
36151317Seric 		}
3624174Seric 		else
3634174Seric 		{
36451317Seric 			char nbuf[MAXNAME];
3654373Seric 
36655354Seric 			if (fuzzy)
3674174Seric 			{
36853735Seric 				/* name was a fuzzy match */
36951317Seric 				a->q_user = newstr(pw->pw_name);
37053735Seric 				if (findusercount++ > 3)
37153735Seric 				{
37258680Seric 					a->q_flags |= QBADADDR;
37358151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
37453735Seric 						pw->pw_name);
37553735Seric 					return (a);
37653735Seric 				}
37753735Seric 
37853735Seric 				/* see if it aliases */
37951317Seric 				(void) strcpy(buf, pw->pw_name);
38053735Seric 				goto trylocaluser;
3814174Seric 			}
38251317Seric 			a->q_home = newstr(pw->pw_dir);
38351317Seric 			a->q_uid = pw->pw_uid;
38451317Seric 			a->q_gid = pw->pw_gid;
38559083Seric 			a->q_ruser = newstr(pw->pw_name);
38651317Seric 			a->q_flags |= QGOODUID;
38751317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
38851317Seric 			if (nbuf[0] != '\0')
38951317Seric 				a->q_fullname = newstr(nbuf);
39051317Seric 			if (!quoted)
39155012Seric 				forward(a, sendq, e);
3924174Seric 		}
3934174Seric 	}
39457642Seric 	if (!bitset(QDONTSEND, a->q_flags))
39557642Seric 		e->e_nrcpts++;
39612613Seric 	return (a);
3974174Seric }
3984174Seric /*
3994373Seric **  FINDUSER -- find the password entry for a user.
4004373Seric **
4014373Seric **	This looks a lot like getpwnam, except that it may want to
4024373Seric **	do some fancier pattern matching in /etc/passwd.
4034373Seric **
4049379Seric **	This routine contains most of the time of many sendmail runs.
4059379Seric **	It deserves to be optimized.
4069379Seric **
4074373Seric **	Parameters:
4084373Seric **		name -- the name to match against.
40955354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
41055354Seric **			was found using the fuzzy matching algorithm;
41155354Seric **			set to FALSE otherwise.
4124373Seric **
4134373Seric **	Returns:
4144373Seric **		A pointer to a pw struct.
4154373Seric **		NULL if name is unknown or ambiguous.
4164373Seric **
4174373Seric **	Side Effects:
4184407Seric **		may modify name.
4194373Seric */
4204373Seric 
4214373Seric struct passwd *
42255354Seric finduser(name, fuzzyp)
4234373Seric 	char *name;
42455354Seric 	bool *fuzzyp;
4254373Seric {
4264376Seric 	register struct passwd *pw;
4274407Seric 	register char *p;
42815325Seric 	extern struct passwd *getpwent();
42915325Seric 	extern struct passwd *getpwnam();
4304373Seric 
43155354Seric 	if (tTd(29, 4))
43255354Seric 		printf("finduser(%s): ", name);
43355354Seric 
43455354Seric 	*fuzzyp = FALSE;
4354407Seric 
43625777Seric 	/* look up this login name using fast path */
43712634Seric 	if ((pw = getpwnam(name)) != NULL)
43855354Seric 	{
43955354Seric 		if (tTd(29, 4))
44055354Seric 			printf("found (non-fuzzy)\n");
44112634Seric 		return (pw);
44255354Seric 	}
44312634Seric 
44453735Seric #ifdef MATCHGECOS
44553735Seric 	/* see if fuzzy matching allowed */
44653735Seric 	if (!MatchGecos)
44755354Seric 	{
44855354Seric 		if (tTd(29, 4))
44955354Seric 			printf("not found (fuzzy disabled)\n");
45053735Seric 		return NULL;
45155354Seric 	}
45253735Seric 
45312634Seric 	/* search for a matching full name instead */
45425777Seric 	for (p = name; *p != '\0'; p++)
45525777Seric 	{
45625777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
45725777Seric 			*p = ' ';
45825777Seric 	}
45923107Seric 	(void) setpwent();
4604376Seric 	while ((pw = getpwent()) != NULL)
4614376Seric 	{
4624998Seric 		char buf[MAXNAME];
4634376Seric 
4644998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
46556795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
4664381Seric 		{
46755354Seric 			if (tTd(29, 4))
46855354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
46958151Seric 			message("sending to login name %s", pw->pw_name);
47055354Seric 			*fuzzyp = TRUE;
4714376Seric 			return (pw);
4724377Seric 		}
4734376Seric 	}
47455354Seric 	if (tTd(29, 4))
47555354Seric 		printf("no fuzzy match found\n");
47659015Seric #else
47759015Seric 	if (tTd(29, 4))
47859015Seric 		printf("not found (fuzzy disabled)\n");
47959015Seric #endif
4804376Seric 	return (NULL);
4814373Seric }
4824373Seric /*
4834329Seric **  WRITABLE -- predicate returning if the file is writable.
4844329Seric **
4854329Seric **	This routine must duplicate the algorithm in sys/fio.c.
4864329Seric **	Unfortunately, we cannot use the access call since we
4874329Seric **	won't necessarily be the real uid when we try to
4884329Seric **	actually open the file.
4894329Seric **
4904329Seric **	Notice that ANY file with ANY execute bit is automatically
4914329Seric **	not writable.  This is also enforced by mailfile.
4924329Seric **
4934329Seric **	Parameters:
4944329Seric **		s -- pointer to a stat struct for the file.
4954329Seric **
4964329Seric **	Returns:
4974329Seric **		TRUE -- if we will be able to write this file.
4984329Seric **		FALSE -- if we cannot write this file.
4994329Seric **
5004329Seric **	Side Effects:
5014329Seric **		none.
5024329Seric */
5034329Seric 
5044329Seric bool
5054329Seric writable(s)
5064329Seric 	register struct stat *s;
5074329Seric {
50855372Seric 	uid_t euid;
50955372Seric 	gid_t egid;
5104329Seric 	int bits;
5114329Seric 
5124329Seric 	if (bitset(0111, s->st_mode))
5134329Seric 		return (FALSE);
5144329Seric 	euid = getruid();
5154329Seric 	egid = getrgid();
5164329Seric 	if (geteuid() == 0)
5174329Seric 	{
5184329Seric 		if (bitset(S_ISUID, s->st_mode))
5194329Seric 			euid = s->st_uid;
5204329Seric 		if (bitset(S_ISGID, s->st_mode))
5214329Seric 			egid = s->st_gid;
5224329Seric 	}
5234329Seric 
5244329Seric 	if (euid == 0)
5254329Seric 		return (TRUE);
5264329Seric 	bits = S_IWRITE;
5274329Seric 	if (euid != s->st_uid)
5284329Seric 	{
5294329Seric 		bits >>= 3;
5304329Seric 		if (egid != s->st_gid)
5314329Seric 			bits >>= 3;
5324329Seric 	}
5334329Seric 	return ((s->st_mode & bits) != 0);
5344329Seric }
5354329Seric /*
5364174Seric **  INCLUDE -- handle :include: specification.
5374174Seric **
5384174Seric **	Parameters:
5394174Seric **		fname -- filename to include.
54053037Seric **		forwarding -- if TRUE, we are reading a .forward file.
54153037Seric **			if FALSE, it's a :include: file.
5424399Seric **		ctladdr -- address template to use to fill in these
5434399Seric **			addresses -- effective user/group id are
5444399Seric **			the important things.
5455006Seric **		sendq -- a pointer to the head of the send queue
5465006Seric **			to put these addresses in.
5474174Seric **
5484174Seric **	Returns:
54957136Seric **		open error status
5504174Seric **
5514174Seric **	Side Effects:
5524174Seric **		reads the :include: file and sends to everyone
5534174Seric **		listed in that file.
5544174Seric */
5554174Seric 
55653037Seric static jmp_buf	CtxIncludeTimeout;
55753037Seric 
55857136Seric int
55955012Seric include(fname, forwarding, ctladdr, sendq, e)
5604174Seric 	char *fname;
56153037Seric 	bool forwarding;
5624399Seric 	ADDRESS *ctladdr;
5635006Seric 	ADDRESS **sendq;
56455012Seric 	ENVELOPE *e;
5654174Seric {
5664174Seric 	register FILE *fp;
56755012Seric 	char *oldto = e->e_to;
5689379Seric 	char *oldfilename = FileName;
5699379Seric 	int oldlinenumber = LineNumber;
57053037Seric 	register EVENT *ev = NULL;
57158082Seric 	int nincludes;
57258247Seric 	int ret;
573*63581Seric 	ADDRESS *ca;
574*63581Seric 	uid_t uid;
57553037Seric 	char buf[MAXLINE];
57653037Seric 	static int includetimeout();
5774174Seric 
57857186Seric 	if (tTd(27, 2))
57957186Seric 		printf("include(%s)\n", fname);
580*63581Seric 	if (tTd(27, 14))
581*63581Seric 	{
582*63581Seric 		printf("ctladdr ");
583*63581Seric 		printaddr(ctladdr, FALSE);
584*63581Seric 	}
58557186Seric 
58653037Seric 	/*
58753037Seric 	**  If home directory is remote mounted but server is down,
58853037Seric 	**  this can hang or give errors; use a timeout to avoid this
58953037Seric 	*/
59053037Seric 
591*63581Seric 	ca = getctladdr(ctladdr);
592*63581Seric 	if (ca == NULL)
593*63581Seric 		uid = 0;
594*63581Seric 	else
595*63581Seric 		uid = ca->q_uid;
596*63581Seric 
59753037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
59853037Seric 	{
59953037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
60053037Seric 		errno = 0;
60153037Seric 		usrerr("451 open timeout on %s", fname);
60257136Seric 		return ETIMEDOUT;
60353037Seric 	}
60453037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
60553037Seric 
606*63581Seric 	/* the input file must be marked safe */
607*63581Seric 	if ((ret = safefile(fname, uid, S_IREAD)) != 0)
60853037Seric 	{
60953037Seric 		/* don't use this .forward file */
61053037Seric 		clrevent(ev);
61157186Seric 		if (tTd(27, 4))
61258247Seric 			printf("include: not safe (uid=%d): %s\n",
613*63581Seric 				uid, errstring(ret));
61458247Seric 		return ret;
61553037Seric 	}
61653037Seric 
6174174Seric 	fp = fopen(fname, "r");
6184174Seric 	if (fp == NULL)
6194174Seric 	{
62057136Seric 		int ret = errno;
62157136Seric 
62258061Seric 		clrevent(ev);
62357136Seric 		return ret;
6244174Seric 	}
62553037Seric 
626*63581Seric 	if (ca == NULL)
6274406Seric 	{
6284406Seric 		struct stat st;
6294174Seric 
6304406Seric 		if (fstat(fileno(fp), &st) < 0)
63158061Seric 		{
63258061Seric 			int ret = errno;
63358061Seric 
63458061Seric 			clrevent(ev);
6354406Seric 			syserr("Cannot fstat %s!", fname);
63658061Seric 			return ret;
63758061Seric 		}
6384406Seric 		ctladdr->q_uid = st.st_uid;
6394406Seric 		ctladdr->q_gid = st.st_gid;
6404406Seric 		ctladdr->q_flags |= QGOODUID;
6414406Seric 	}
6424406Seric 
64353037Seric 	clrevent(ev);
64453037Seric 
64558092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
64658092Seric 	{
64758092Seric 		/* don't do any more now */
64858868Seric 		ctladdr->q_flags |= QVERIFIED;
64958884Seric 		e->e_nrcpts++;
65058680Seric 		xfclose(fp, "include", fname);
65158092Seric 		return 0;
65258092Seric 	}
65358092Seric 
6544174Seric 	/* read the file -- each line is a comma-separated list. */
6559379Seric 	FileName = fname;
6569379Seric 	LineNumber = 0;
65758082Seric 	ctladdr->q_flags &= ~QSELFREF;
65858082Seric 	nincludes = 0;
6594174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6604174Seric 	{
66156795Seric 		register char *p = strchr(buf, '\n');
6624174Seric 
66340963Sbostic 		LineNumber++;
6644174Seric 		if (p != NULL)
6654174Seric 			*p = '\0';
66657186Seric 		if (buf[0] == '#' || buf[0] == '\0')
66757139Seric 			continue;
66858008Seric 		e->e_to = NULL;
66958151Seric 		message("%s to %s",
67053037Seric 			forwarding ? "forwarding" : "sending", buf);
67157977Seric #ifdef LOG
67258020Seric 		if (forwarding && LogLevel > 9)
67357977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
67457977Seric 				e->e_id, oldto, buf);
67557977Seric #endif
67657977Seric 
6774176Seric 		AliasLevel++;
67858082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
6794176Seric 		AliasLevel--;
6804174Seric 	}
68158082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
68258065Seric 	{
68358065Seric 		if (tTd(27, 5))
68458065Seric 		{
68558065Seric 			printf("include: QDONTSEND ");
68658065Seric 			printaddr(ctladdr, FALSE);
68758065Seric 		}
68858065Seric 		ctladdr->q_flags |= QDONTSEND;
68958065Seric 	}
6904174Seric 
69158680Seric 	(void) xfclose(fp, "include", fname);
6929379Seric 	FileName = oldfilename;
6939379Seric 	LineNumber = oldlinenumber;
69457136Seric 	return 0;
6954174Seric }
69653037Seric 
69753037Seric static
69853037Seric includetimeout()
69953037Seric {
70053037Seric 	longjmp(CtxIncludeTimeout, 1);
70153037Seric }
7024324Seric /*
7034324Seric **  SENDTOARGV -- send to an argument vector.
7044324Seric **
7054324Seric **	Parameters:
7064324Seric **		argv -- argument vector to send to.
70758247Seric **		e -- the current envelope.
7084324Seric **
7094324Seric **	Returns:
7104324Seric **		none.
7114324Seric **
7124324Seric **	Side Effects:
7134324Seric **		puts all addresses on the argument vector onto the
7144324Seric **			send queue.
7154324Seric */
7164324Seric 
71755012Seric sendtoargv(argv, e)
7184324Seric 	register char **argv;
71955012Seric 	register ENVELOPE *e;
7204324Seric {
7214324Seric 	register char *p;
7224324Seric 
7234324Seric 	while ((p = *argv++) != NULL)
7244324Seric 	{
72558082Seric 		(void) sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7264324Seric 	}
7274324Seric }
7284399Seric /*
7294399Seric **  GETCTLADDR -- get controlling address from an address header.
7304399Seric **
7314399Seric **	If none, get one corresponding to the effective userid.
7324399Seric **
7334399Seric **	Parameters:
7344399Seric **		a -- the address to find the controller of.
7354399Seric **
7364399Seric **	Returns:
7374399Seric **		the controlling address.
7384399Seric **
7394399Seric **	Side Effects:
7404399Seric **		none.
7414399Seric */
7424399Seric 
7434399Seric ADDRESS *
7444399Seric getctladdr(a)
7454399Seric 	register ADDRESS *a;
7464399Seric {
7474404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7484399Seric 		a = a->q_alias;
7494399Seric 	return (a);
7504399Seric }
751