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*58337Seric static char sccsid[] = "@(#)recipient.c	6.22 (Berkeley) 03/01/93";
1133731Sbostic #endif /* not lint */
1222710Sdist 
1358332Seric # include "sendmail.h"
1436928Sbostic # include <sys/stat.h>
1557737Seric # include <fcntl.h>
164174Seric # include <pwd.h>
174174Seric 
184174Seric /*
199622Seric **  SENDTOLIST -- Designate a send list.
204174Seric **
214174Seric **	The parameter is a comma-separated list of people to send to.
224174Seric **	This routine arranges to send to all of them.
234174Seric **
244174Seric **	Parameters:
254174Seric **		list -- the send list.
264399Seric **		ctladdr -- the address template for the person to
274399Seric **			send to -- effective uid/gid are important.
285006Seric **			This is typically the alias that caused this
295006Seric **			expansion.
305006Seric **		sendq -- a pointer to the head of a queue to put
315006Seric **			these people into.
3258247Seric **		e -- the envelope in which to add these recipients.
334174Seric **
344174Seric **	Returns:
3558082Seric **		The number of addresses actually on the list.
364174Seric **
374174Seric **	Side Effects:
384174Seric **		none.
394174Seric */
404174Seric 
414174Seric # define MAXRCRSN	10
424174Seric 
4355012Seric sendtolist(list, ctladdr, sendq, e)
444174Seric 	char *list;
454399Seric 	ADDRESS *ctladdr;
465198Seric 	ADDRESS **sendq;
4755012Seric 	register ENVELOPE *e;
484174Seric {
494174Seric 	register char *p;
508223Seric 	register ADDRESS *al;	/* list of addresses to send to */
514423Seric 	bool firstone;		/* set on first address sent */
5211446Seric 	char delimiter;		/* the address delimiter */
5358082Seric 	int naddrs;
544174Seric 
557676Seric 	if (tTd(25, 1))
564444Seric 	{
574444Seric 		printf("sendto: %s\n   ctladdr=", list);
584444Seric 		printaddr(ctladdr, FALSE);
594444Seric 	}
604324Seric 
618223Seric 	/* heuristic to determine old versus new style addresses */
628230Seric 	if (ctladdr == NULL &&
6356795Seric 	    (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
6456795Seric 	     strchr(list, '<') != NULL || strchr(list, '(') != NULL))
6555012Seric 		e->e_flags &= ~EF_OLDSTYLE;
6611446Seric 	delimiter = ' ';
6755012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
6811446Seric 		delimiter = ',';
698223Seric 
704423Seric 	firstone = TRUE;
714324Seric 	al = NULL;
7258082Seric 	naddrs = 0;
738223Seric 
748081Seric 	for (p = list; *p != '\0'; )
754174Seric 	{
7658333Seric 		auto char *delimptr;
778081Seric 		register ADDRESS *a;
784319Seric 
798081Seric 		/* parse the address */
8058050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
814174Seric 			p++;
8258333Seric 		a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter, &delimptr, e);
8358333Seric 		p = delimptr;
849297Seric 		if (a == NULL)
854174Seric 			continue;
864324Seric 		a->q_next = al;
874399Seric 		a->q_alias = ctladdr;
884444Seric 
894444Seric 		/* see if this should be marked as a primary address */
904423Seric 		if (ctladdr == NULL ||
918081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
924423Seric 			a->q_flags |= QPRIMARY;
934444Seric 
949379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
9558061Seric 			ctladdr->q_flags |= QSELFREF;
9657731Seric 		al = a;
974423Seric 		firstone = FALSE;
984324Seric 	}
994324Seric 
1004324Seric 	/* arrange to send to everyone on the local send list */
1014324Seric 	while (al != NULL)
1024324Seric 	{
1034324Seric 		register ADDRESS *a = al;
10412613Seric 		extern ADDRESS *recipient();
1054324Seric 
1064324Seric 		al = a->q_next;
10755012Seric 		a = recipient(a, sendq, e);
1084993Seric 
1094998Seric 		/* arrange to inherit full name */
1104998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1114998Seric 			a->q_fullname = ctladdr->q_fullname;
11258082Seric 		naddrs++;
1134174Seric 	}
1144324Seric 
11555012Seric 	e->e_to = NULL;
11658082Seric 	return (naddrs);
1174174Seric }
1184174Seric /*
1194174Seric **  RECIPIENT -- Designate a message recipient
1204174Seric **
1214174Seric **	Saves the named person for future mailing.
1224174Seric **
1234174Seric **	Parameters:
1244174Seric **		a -- the (preparsed) address header for the recipient.
1255006Seric **		sendq -- a pointer to the head of a queue to put the
1265006Seric **			recipient in.  Duplicate supression is done
1275006Seric **			in this queue.
12857731Seric **		e -- the current envelope.
1294174Seric **
1304174Seric **	Returns:
13112613Seric **		The actual address in the queue.  This will be "a" if
13212613Seric **		the address is not a duplicate, else the original address.
1334174Seric **
1344174Seric **	Side Effects:
1354174Seric **		none.
1364174Seric */
1374174Seric 
13846928Sbostic extern ADDRESS *getctladdr();
13946928Sbostic 
14012613Seric ADDRESS *
14155012Seric recipient(a, sendq, e)
1424174Seric 	register ADDRESS *a;
1435006Seric 	register ADDRESS **sendq;
14455012Seric 	register ENVELOPE *e;
1454174Seric {
1464174Seric 	register ADDRESS *q;
1474319Seric 	ADDRESS **pq;
1484174Seric 	register struct mailer *m;
1499210Seric 	register char *p;
1509210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
15153735Seric 	int findusercount = 0;
1529210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
15358247Seric 	extern int safefile();
1544174Seric 
15555012Seric 	e->e_to = a->q_paddr;
1564600Seric 	m = a->q_mailer;
1574174Seric 	errno = 0;
1587676Seric 	if (tTd(26, 1))
1594444Seric 	{
1604444Seric 		printf("\nrecipient: ");
1614444Seric 		printaddr(a, FALSE);
1624444Seric 	}
1634174Seric 
1644174Seric 	/* break aliasing loops */
1654174Seric 	if (AliasLevel > MAXRCRSN)
1664174Seric 	{
16758151Seric 		usrerr("554 aliasing/forwarding loop broken");
16812613Seric 		return (a);
1694174Seric 	}
1704174Seric 
1714174Seric 	/*
1724627Seric 	**  Finish setting up address structure.
1734174Seric 	*/
1744174Seric 
17516160Seric 	/* set the queue timeout */
1764627Seric 	a->q_timeout = TimeOut;
1774627Seric 
17816160Seric 	/* map user & host to lower case if requested on non-aliases */
17916160Seric 	if (a->q_alias == NULL)
18016160Seric 		loweraddr(a);
18116160Seric 
18216160Seric 	/* get unquoted user for file, program or user.name check */
1839210Seric 	(void) strcpy(buf, a->q_user);
1849210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1859210Seric 	{
18654993Seric 		if (*p == '\\')
1879210Seric 			quoted = TRUE;
1889210Seric 	}
18954983Seric 	stripquotes(buf);
1909210Seric 
19157402Seric 	/* check for direct mailing to restricted mailers */
19257731Seric 	if (a->q_alias == NULL && m == ProgMailer)
1934174Seric 	{
19457402Seric 		a->q_flags |= QDONTSEND|QBADADDR;
19558151Seric 		usrerr("550 Cannot mail directly to programs", m->m_name);
1964174Seric 	}
1974174Seric 
1984174Seric 	/*
1994419Seric 	**  Look up this person in the recipient list.
2004419Seric 	**	If they are there already, return, otherwise continue.
2014419Seric 	**	If the list is empty, just add it.  Notice the cute
2024419Seric 	**	hack to make from addresses suppress things correctly:
2034419Seric 	**	the QDONTSEND bit will be set in the send list.
2044419Seric 	**	[Please note: the emphasis is on "hack."]
2054174Seric 	*/
2064174Seric 
2075006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2084174Seric 	{
20958294Seric 		if (sameaddr(q, a))
2104174Seric 		{
2117676Seric 			if (tTd(26, 1))
2124444Seric 			{
2134444Seric 				printf("%s in sendq: ", a->q_paddr);
2144444Seric 				printaddr(q, FALSE);
2154444Seric 			}
2164423Seric 			if (!bitset(QPRIMARY, q->q_flags))
21758065Seric 			{
21858065Seric 				if (!bitset(QDONTSEND, a->q_flags))
21958151Seric 					message("duplicate suppressed");
2204423Seric 				q->q_flags |= a->q_flags;
22158065Seric 			}
22212613Seric 			return (q);
2234174Seric 		}
2244319Seric 	}
2254174Seric 
2264319Seric 	/* add address on list */
2274319Seric 	*pq = a;
2284174Seric 	a->q_next = NULL;
2294174Seric 
2304174Seric 	/*
23157402Seric 	**  Alias the name and handle special mailer types.
2324174Seric 	*/
2334174Seric 
23453735Seric   trylocaluser:
23555354Seric 	if (tTd(29, 7))
23655354Seric 		printf("at trylocaluser %s\n", a->q_user);
23755354Seric 
23858154Seric 	if (bitset(QDONTSEND|QVERIFIED, a->q_flags))
23957402Seric 		return (a);
24057402Seric 
24157402Seric 	if (m == InclMailer)
2424174Seric 	{
24357402Seric 		a->q_flags |= QDONTSEND;
24457731Seric 		if (a->q_alias == NULL)
2454174Seric 		{
24657402Seric 			a->q_flags |= QBADADDR;
24758151Seric 			usrerr("550 Cannot mail directly to :include:s");
2484174Seric 		}
2494174Seric 		else
25050556Seric 		{
25158247Seric 			int err;
25258247Seric 
25358151Seric 			message("including file %s", a->q_user);
25458247Seric 			err = include(a->q_user, FALSE, a, sendq, e);
25558247Seric 			if (transienterror(err))
25658247Seric 				a->q_flags |= QQUEUEUP|QDONTSEND;
25750556Seric 		}
2584174Seric 	}
25957642Seric 	else if (m == FileMailer)
2604174Seric 	{
2614329Seric 		struct stat stb;
2624329Seric 		extern bool writable();
2634174Seric 
26456795Seric 		p = strrchr(buf, '/');
26551317Seric 		/* check if writable or creatable */
26657731Seric 		if (a->q_alias == NULL && !QueueRun)
2674174Seric 		{
26851317Seric 			a->q_flags |= QDONTSEND|QBADADDR;
26958151Seric 			usrerr("550 Cannot mail directly to files");
2704174Seric 		}
27151317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
27258247Seric 		    (*p = '\0', safefile(buf, getruid(), S_IWRITE|S_IEXEC) != 0))
27351317Seric 		{
27451317Seric 			a->q_flags |= QBADADDR;
275*58337Seric 			giveresponse(EX_CANTCREAT, m, NULL, e);
27651317Seric 		}
27751317Seric 	}
27851317Seric 
27957402Seric 	if (m != LocalMailer)
28057642Seric 	{
28157642Seric 		if (!bitset(QDONTSEND, a->q_flags))
28257642Seric 			e->e_nrcpts++;
28357402Seric 		return (a);
28457642Seric 	}
28557402Seric 
28657402Seric 	/* try aliasing */
28757402Seric 	alias(a, sendq, e);
28857402Seric 
28957402Seric # ifdef USERDB
29057402Seric 	/* if not aliased, look it up in the user database */
29157402Seric 	if (!bitset(QDONTSEND|QNOTREMOTE, a->q_flags))
29257402Seric 	{
29357402Seric 		extern int udbexpand();
29457402Seric 
29557402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
29657402Seric 		{
29758247Seric 			a->q_flags |= QQUEUEUP|QDONTSEND;
29857402Seric 			if (e->e_message == NULL)
29957402Seric 				e->e_message = newstr("Deferred: user database error");
30057402Seric # ifdef LOG
30158020Seric 			if (LogLevel > 8)
30257402Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand",
30357402Seric 					e->e_id);
30457402Seric # endif
30558151Seric 			message("queued (user database error)");
30657642Seric 			e->e_nrcpts++;
30757402Seric 			return (a);
30857402Seric 		}
30957402Seric 	}
31057402Seric # endif
31157402Seric 
31257402Seric 	/* if it was an alias or a UDB expansion, just return now */
31358247Seric 	if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags))
31457402Seric 		return (a);
31557402Seric 
31651317Seric 	/*
31751317Seric 	**  If we have a level two config file, then pass the name through
31851317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
31951317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
32051317Seric 	**  after local aliasing has been done.
32151317Seric 	*/
32251317Seric 
32351317Seric 	if (tTd(29, 5))
32451317Seric 	{
32551317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
32651317Seric 			ConfigLevel, RewriteRules[5]);
32751317Seric 		printaddr(a, FALSE);
32851317Seric 	}
32951317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
33051317Seric 	    RewriteRules[5] != NULL)
33151317Seric 	{
33255012Seric 		maplocaluser(a, sendq, e);
33351317Seric 	}
33451317Seric 
33551317Seric 	/*
33651317Seric 	**  If it didn't get rewritten to another mailer, go ahead
33751317Seric 	**  and deliver it.
33851317Seric 	*/
33951317Seric 
34058247Seric 	if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags))
34151317Seric 	{
34255354Seric 		auto bool fuzzy;
34351317Seric 		register struct passwd *pw;
34451317Seric 		extern struct passwd *finduser();
34551317Seric 
34651317Seric 		/* warning -- finduser may trash buf */
34755354Seric 		pw = finduser(buf, &fuzzy);
34851317Seric 		if (pw == NULL)
34951317Seric 		{
35051317Seric 			a->q_flags |= QBADADDR;
351*58337Seric 			giveresponse(EX_NOUSER, m, NULL, e);
35251317Seric 		}
3534174Seric 		else
3544174Seric 		{
35551317Seric 			char nbuf[MAXNAME];
3564373Seric 
35755354Seric 			if (fuzzy)
3584174Seric 			{
35953735Seric 				/* name was a fuzzy match */
36051317Seric 				a->q_user = newstr(pw->pw_name);
36153735Seric 				if (findusercount++ > 3)
36253735Seric 				{
36358151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
36453735Seric 						pw->pw_name);
36553735Seric 					return (a);
36653735Seric 				}
36753735Seric 
36853735Seric 				/* see if it aliases */
36951317Seric 				(void) strcpy(buf, pw->pw_name);
37053735Seric 				goto trylocaluser;
3714174Seric 			}
37251317Seric 			a->q_home = newstr(pw->pw_dir);
37351317Seric 			a->q_uid = pw->pw_uid;
37451317Seric 			a->q_gid = pw->pw_gid;
37551317Seric 			a->q_flags |= QGOODUID;
37651317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
37751317Seric 			if (nbuf[0] != '\0')
37851317Seric 				a->q_fullname = newstr(nbuf);
37951317Seric 			if (!quoted)
38055012Seric 				forward(a, sendq, e);
3814174Seric 		}
3824174Seric 	}
38357642Seric 	if (!bitset(QDONTSEND, a->q_flags))
38457642Seric 		e->e_nrcpts++;
38512613Seric 	return (a);
3864174Seric }
3874174Seric /*
3884373Seric **  FINDUSER -- find the password entry for a user.
3894373Seric **
3904373Seric **	This looks a lot like getpwnam, except that it may want to
3914373Seric **	do some fancier pattern matching in /etc/passwd.
3924373Seric **
3939379Seric **	This routine contains most of the time of many sendmail runs.
3949379Seric **	It deserves to be optimized.
3959379Seric **
3964373Seric **	Parameters:
3974373Seric **		name -- the name to match against.
39855354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
39955354Seric **			was found using the fuzzy matching algorithm;
40055354Seric **			set to FALSE otherwise.
4014373Seric **
4024373Seric **	Returns:
4034373Seric **		A pointer to a pw struct.
4044373Seric **		NULL if name is unknown or ambiguous.
4054373Seric **
4064373Seric **	Side Effects:
4074407Seric **		may modify name.
4084373Seric */
4094373Seric 
4104373Seric struct passwd *
41155354Seric finduser(name, fuzzyp)
4124373Seric 	char *name;
41355354Seric 	bool *fuzzyp;
4144373Seric {
4154376Seric 	register struct passwd *pw;
4164407Seric 	register char *p;
41715325Seric 	extern struct passwd *getpwent();
41815325Seric 	extern struct passwd *getpwnam();
4194373Seric 
42055354Seric 	if (tTd(29, 4))
42155354Seric 		printf("finduser(%s): ", name);
42255354Seric 
42325777Seric 	/* map upper => lower case */
4244407Seric 	for (p = name; *p != '\0'; p++)
4254407Seric 	{
42625777Seric 		if (isascii(*p) && isupper(*p))
42725568Seric 			*p = tolower(*p);
4284407Seric 	}
42955354Seric 	*fuzzyp = FALSE;
4304407Seric 
43125777Seric 	/* look up this login name using fast path */
43212634Seric 	if ((pw = getpwnam(name)) != NULL)
43355354Seric 	{
43455354Seric 		if (tTd(29, 4))
43555354Seric 			printf("found (non-fuzzy)\n");
43612634Seric 		return (pw);
43755354Seric 	}
43812634Seric 
43953735Seric #ifdef MATCHGECOS
44053735Seric 	/* see if fuzzy matching allowed */
44153735Seric 	if (!MatchGecos)
44255354Seric 	{
44355354Seric 		if (tTd(29, 4))
44455354Seric 			printf("not found (fuzzy disabled)\n");
44553735Seric 		return NULL;
44655354Seric 	}
44753735Seric 
44812634Seric 	/* search for a matching full name instead */
44925777Seric 	for (p = name; *p != '\0'; p++)
45025777Seric 	{
45125777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
45225777Seric 			*p = ' ';
45325777Seric 	}
45423107Seric 	(void) setpwent();
4554376Seric 	while ((pw = getpwent()) != NULL)
4564376Seric 	{
4574998Seric 		char buf[MAXNAME];
4584376Seric 
4594998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
46056795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
4614381Seric 		{
46255354Seric 			if (tTd(29, 4))
46355354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
46458151Seric 			message("sending to login name %s", pw->pw_name);
46555354Seric 			*fuzzyp = TRUE;
4664376Seric 			return (pw);
4674377Seric 		}
4684376Seric 	}
46953735Seric #endif
47055354Seric 	if (tTd(29, 4))
47155354Seric 		printf("no fuzzy match found\n");
4724376Seric 	return (NULL);
4734373Seric }
4744373Seric /*
4754329Seric **  WRITABLE -- predicate returning if the file is writable.
4764329Seric **
4774329Seric **	This routine must duplicate the algorithm in sys/fio.c.
4784329Seric **	Unfortunately, we cannot use the access call since we
4794329Seric **	won't necessarily be the real uid when we try to
4804329Seric **	actually open the file.
4814329Seric **
4824329Seric **	Notice that ANY file with ANY execute bit is automatically
4834329Seric **	not writable.  This is also enforced by mailfile.
4844329Seric **
4854329Seric **	Parameters:
4864329Seric **		s -- pointer to a stat struct for the file.
4874329Seric **
4884329Seric **	Returns:
4894329Seric **		TRUE -- if we will be able to write this file.
4904329Seric **		FALSE -- if we cannot write this file.
4914329Seric **
4924329Seric **	Side Effects:
4934329Seric **		none.
4944329Seric */
4954329Seric 
4964329Seric bool
4974329Seric writable(s)
4984329Seric 	register struct stat *s;
4994329Seric {
50055372Seric 	uid_t euid;
50155372Seric 	gid_t egid;
5024329Seric 	int bits;
5034329Seric 
5044329Seric 	if (bitset(0111, s->st_mode))
5054329Seric 		return (FALSE);
5064329Seric 	euid = getruid();
5074329Seric 	egid = getrgid();
5084329Seric 	if (geteuid() == 0)
5094329Seric 	{
5104329Seric 		if (bitset(S_ISUID, s->st_mode))
5114329Seric 			euid = s->st_uid;
5124329Seric 		if (bitset(S_ISGID, s->st_mode))
5134329Seric 			egid = s->st_gid;
5144329Seric 	}
5154329Seric 
5164329Seric 	if (euid == 0)
5174329Seric 		return (TRUE);
5184329Seric 	bits = S_IWRITE;
5194329Seric 	if (euid != s->st_uid)
5204329Seric 	{
5214329Seric 		bits >>= 3;
5224329Seric 		if (egid != s->st_gid)
5234329Seric 			bits >>= 3;
5244329Seric 	}
5254329Seric 	return ((s->st_mode & bits) != 0);
5264329Seric }
5274329Seric /*
5284174Seric **  INCLUDE -- handle :include: specification.
5294174Seric **
5304174Seric **	Parameters:
5314174Seric **		fname -- filename to include.
53253037Seric **		forwarding -- if TRUE, we are reading a .forward file.
53353037Seric **			if FALSE, it's a :include: file.
5344399Seric **		ctladdr -- address template to use to fill in these
5354399Seric **			addresses -- effective user/group id are
5364399Seric **			the important things.
5375006Seric **		sendq -- a pointer to the head of the send queue
5385006Seric **			to put these addresses in.
5394174Seric **
5404174Seric **	Returns:
54157136Seric **		open error status
5424174Seric **
5434174Seric **	Side Effects:
5444174Seric **		reads the :include: file and sends to everyone
5454174Seric **		listed in that file.
5464174Seric */
5474174Seric 
54853037Seric static jmp_buf	CtxIncludeTimeout;
54953037Seric 
55057136Seric int
55155012Seric include(fname, forwarding, ctladdr, sendq, e)
5524174Seric 	char *fname;
55353037Seric 	bool forwarding;
5544399Seric 	ADDRESS *ctladdr;
5555006Seric 	ADDRESS **sendq;
55655012Seric 	ENVELOPE *e;
5574174Seric {
5584174Seric 	register FILE *fp;
55955012Seric 	char *oldto = e->e_to;
5609379Seric 	char *oldfilename = FileName;
5619379Seric 	int oldlinenumber = LineNumber;
56253037Seric 	register EVENT *ev = NULL;
56358082Seric 	int nincludes;
56458247Seric 	int ret;
56553037Seric 	char buf[MAXLINE];
56653037Seric 	static int includetimeout();
5674174Seric 
56857186Seric 	if (tTd(27, 2))
56957186Seric 		printf("include(%s)\n", fname);
57057186Seric 
57153037Seric 	/*
57253037Seric 	**  If home directory is remote mounted but server is down,
57353037Seric 	**  this can hang or give errors; use a timeout to avoid this
57453037Seric 	*/
57553037Seric 
57653037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
57753037Seric 	{
57853037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
57953037Seric 		errno = 0;
58053037Seric 		usrerr("451 open timeout on %s", fname);
58157136Seric 		return ETIMEDOUT;
58253037Seric 	}
58353037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
58453037Seric 
58553037Seric 	/* if forwarding, the input file must be marked safe */
58658247Seric 	if (forwarding && (ret = safefile(fname, ctladdr->q_uid, S_IREAD)) != 0)
58753037Seric 	{
58853037Seric 		/* don't use this .forward file */
58953037Seric 		clrevent(ev);
59057186Seric 		if (tTd(27, 4))
59158247Seric 			printf("include: not safe (uid=%d): %s\n",
59258247Seric 				ctladdr->q_uid, errstring(ret));
59358247Seric 		return ret;
59453037Seric 	}
59553037Seric 
5964174Seric 	fp = fopen(fname, "r");
5974174Seric 	if (fp == NULL)
5984174Seric 	{
59957136Seric 		int ret = errno;
60057136Seric 
60158061Seric 		clrevent(ev);
60258151Seric 		usrerr("550 Cannot open %s", fname);
60357136Seric 		return ret;
6044174Seric 	}
60553037Seric 
6064406Seric 	if (getctladdr(ctladdr) == NULL)
6074406Seric 	{
6084406Seric 		struct stat st;
6094174Seric 
6104406Seric 		if (fstat(fileno(fp), &st) < 0)
61158061Seric 		{
61258061Seric 			int ret = errno;
61358061Seric 
61458061Seric 			clrevent(ev);
6154406Seric 			syserr("Cannot fstat %s!", fname);
61658061Seric 			return ret;
61758061Seric 		}
6184406Seric 		ctladdr->q_uid = st.st_uid;
6194406Seric 		ctladdr->q_gid = st.st_gid;
6204406Seric 		ctladdr->q_flags |= QGOODUID;
6214406Seric 	}
6224406Seric 
62353037Seric 	clrevent(ev);
62453037Seric 
62558092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
62658092Seric 	{
62758092Seric 		/* don't do any more now */
62858092Seric 		fclose(fp);
62958092Seric 		return 0;
63058092Seric 	}
63158092Seric 
6324174Seric 	/* read the file -- each line is a comma-separated list. */
6339379Seric 	FileName = fname;
6349379Seric 	LineNumber = 0;
63558082Seric 	ctladdr->q_flags &= ~QSELFREF;
63658082Seric 	nincludes = 0;
6374174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6384174Seric 	{
63956795Seric 		register char *p = strchr(buf, '\n');
6404174Seric 
64140963Sbostic 		LineNumber++;
6424174Seric 		if (p != NULL)
6434174Seric 			*p = '\0';
64457186Seric 		if (buf[0] == '#' || buf[0] == '\0')
64557139Seric 			continue;
64658008Seric 		e->e_to = NULL;
64758151Seric 		message("%s to %s",
64853037Seric 			forwarding ? "forwarding" : "sending", buf);
64957977Seric #ifdef LOG
65058020Seric 		if (forwarding && LogLevel > 9)
65157977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
65257977Seric 				e->e_id, oldto, buf);
65357977Seric #endif
65457977Seric 
6554176Seric 		AliasLevel++;
65658082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
6574176Seric 		AliasLevel--;
6584174Seric 	}
65958082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
66058065Seric 	{
66158065Seric 		if (tTd(27, 5))
66258065Seric 		{
66358065Seric 			printf("include: QDONTSEND ");
66458065Seric 			printaddr(ctladdr, FALSE);
66558065Seric 		}
66658065Seric 		ctladdr->q_flags |= QDONTSEND;
66758065Seric 	}
6684174Seric 
6694319Seric 	(void) fclose(fp);
6709379Seric 	FileName = oldfilename;
6719379Seric 	LineNumber = oldlinenumber;
67257136Seric 	return 0;
6734174Seric }
67453037Seric 
67553037Seric static
67653037Seric includetimeout()
67753037Seric {
67853037Seric 	longjmp(CtxIncludeTimeout, 1);
67953037Seric }
6804324Seric /*
6814324Seric **  SENDTOARGV -- send to an argument vector.
6824324Seric **
6834324Seric **	Parameters:
6844324Seric **		argv -- argument vector to send to.
68558247Seric **		e -- the current envelope.
6864324Seric **
6874324Seric **	Returns:
6884324Seric **		none.
6894324Seric **
6904324Seric **	Side Effects:
6914324Seric **		puts all addresses on the argument vector onto the
6924324Seric **			send queue.
6934324Seric */
6944324Seric 
69555012Seric sendtoargv(argv, e)
6964324Seric 	register char **argv;
69755012Seric 	register ENVELOPE *e;
6984324Seric {
6994324Seric 	register char *p;
7004324Seric 
7014324Seric 	while ((p = *argv++) != NULL)
7024324Seric 	{
70333725Sbostic 		if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at"))
7044324Seric 		{
7054324Seric 			char nbuf[MAXNAME];
7064324Seric 
7074324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
70858151Seric 				usrerr("554 address overflow");
7094324Seric 			else
7104324Seric 			{
7114324Seric 				(void) strcpy(nbuf, p);
7124324Seric 				(void) strcat(nbuf, "@");
7134324Seric 				(void) strcat(nbuf, argv[1]);
7144324Seric 				p = newstr(nbuf);
7154324Seric 				argv += 2;
7164324Seric 			}
7174324Seric 		}
71858082Seric 		(void) sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7194324Seric 	}
7204324Seric }
7214399Seric /*
7224399Seric **  GETCTLADDR -- get controlling address from an address header.
7234399Seric **
7244399Seric **	If none, get one corresponding to the effective userid.
7254399Seric **
7264399Seric **	Parameters:
7274399Seric **		a -- the address to find the controller of.
7284399Seric **
7294399Seric **	Returns:
7304399Seric **		the controlling address.
7314399Seric **
7324399Seric **	Side Effects:
7334399Seric **		none.
7344399Seric */
7354399Seric 
7364399Seric ADDRESS *
7374399Seric getctladdr(a)
7384399Seric 	register ADDRESS *a;
7394399Seric {
7404404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7414399Seric 		a = a->q_alias;
7424399Seric 	return (a);
7434399Seric }
744