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*58154Seric static char sccsid[] = "@(#)recipient.c	6.17 (Berkeley) 02/23/93";
1133731Sbostic #endif /* not lint */
1222710Sdist 
1336928Sbostic # include <sys/types.h>
1436928Sbostic # include <sys/stat.h>
1557737Seric # include <fcntl.h>
164174Seric # include <pwd.h>
174627Seric # include "sendmail.h"
184174Seric 
194174Seric /*
209622Seric **  SENDTOLIST -- Designate a send list.
214174Seric **
224174Seric **	The parameter is a comma-separated list of people to send to.
234174Seric **	This routine arranges to send to all of them.
244174Seric **
254174Seric **	Parameters:
264174Seric **		list -- the send list.
274399Seric **		ctladdr -- the address template for the person to
284399Seric **			send to -- effective uid/gid are important.
295006Seric **			This is typically the alias that caused this
305006Seric **			expansion.
315006Seric **		sendq -- a pointer to the head of a queue to put
325006Seric **			these people into.
334174Seric **
344174Seric **	Returns:
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 	{
768081Seric 		register ADDRESS *a;
778081Seric 		extern char *DelimChar;		/* defined in prescan */
784319Seric 
798081Seric 		/* parse the address */
8058050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
814174Seric 			p++;
8255012Seric 		a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter, e);
839297Seric 		p = DelimChar;
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 */
1534627Seric 	extern bool 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 	{
2099379Seric 		if (!ForceMail && 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 
238*58154Seric 	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 		{
25158151Seric 			message("including file %s", a->q_user);
25258008Seric 			(void) include(a->q_user, FALSE, a, sendq, e);
25350556Seric 		}
2544174Seric 	}
25557642Seric 	else if (m == FileMailer)
2564174Seric 	{
2574329Seric 		struct stat stb;
2584329Seric 		extern bool writable();
2594174Seric 
26056795Seric 		p = strrchr(buf, '/');
26151317Seric 		/* check if writable or creatable */
26257731Seric 		if (a->q_alias == NULL && !QueueRun)
2634174Seric 		{
26451317Seric 			a->q_flags |= QDONTSEND|QBADADDR;
26558151Seric 			usrerr("550 Cannot mail directly to files");
2664174Seric 		}
26751317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
26851317Seric 		    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
26951317Seric 		{
27051317Seric 			a->q_flags |= QBADADDR;
27155012Seric 			giveresponse(EX_CANTCREAT, m, e);
27251317Seric 		}
27351317Seric 	}
27451317Seric 
27557402Seric 	if (m != LocalMailer)
27657642Seric 	{
27757642Seric 		if (!bitset(QDONTSEND, a->q_flags))
27857642Seric 			e->e_nrcpts++;
27957402Seric 		return (a);
28057642Seric 	}
28157402Seric 
28257402Seric 	/* try aliasing */
28357402Seric 	alias(a, sendq, e);
28457402Seric 
28557402Seric # ifdef USERDB
28657402Seric 	/* if not aliased, look it up in the user database */
28757402Seric 	if (!bitset(QDONTSEND|QNOTREMOTE, a->q_flags))
28857402Seric 	{
28957402Seric 		extern int udbexpand();
29057402Seric 
29157402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
29257402Seric 		{
29357402Seric 			a->q_flags |= QQUEUEUP;
29457402Seric 			if (e->e_message == NULL)
29557402Seric 				e->e_message = newstr("Deferred: user database error");
29657402Seric # ifdef LOG
29758020Seric 			if (LogLevel > 8)
29857402Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand",
29957402Seric 					e->e_id);
30057402Seric # endif
30158151Seric 			message("queued (user database error)");
30257642Seric 			e->e_nrcpts++;
30357402Seric 			return (a);
30457402Seric 		}
30557402Seric 	}
30657402Seric # endif
30757402Seric 
30857402Seric 	/* if it was an alias or a UDB expansion, just return now */
309*58154Seric 	if (bitset(QDONTSEND|QVERIFIED, a->q_flags))
31057402Seric 		return (a);
31157402Seric 
31251317Seric 	/*
31351317Seric 	**  If we have a level two config file, then pass the name through
31451317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
31551317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
31651317Seric 	**  after local aliasing has been done.
31751317Seric 	*/
31851317Seric 
31951317Seric 	if (tTd(29, 5))
32051317Seric 	{
32151317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
32251317Seric 			ConfigLevel, RewriteRules[5]);
32351317Seric 		printaddr(a, FALSE);
32451317Seric 	}
32551317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
32651317Seric 	    RewriteRules[5] != NULL)
32751317Seric 	{
32855012Seric 		maplocaluser(a, sendq, e);
32951317Seric 	}
33051317Seric 
33151317Seric 	/*
33251317Seric 	**  If it didn't get rewritten to another mailer, go ahead
33351317Seric 	**  and deliver it.
33451317Seric 	*/
33551317Seric 
33651317Seric 	if (!bitset(QDONTSEND, a->q_flags))
33751317Seric 	{
33855354Seric 		auto bool fuzzy;
33951317Seric 		register struct passwd *pw;
34051317Seric 		extern struct passwd *finduser();
34151317Seric 
34251317Seric 		/* warning -- finduser may trash buf */
34355354Seric 		pw = finduser(buf, &fuzzy);
34451317Seric 		if (pw == NULL)
34551317Seric 		{
34651317Seric 			a->q_flags |= QBADADDR;
34755012Seric 			giveresponse(EX_NOUSER, m, e);
34851317Seric 		}
3494174Seric 		else
3504174Seric 		{
35151317Seric 			char nbuf[MAXNAME];
3524373Seric 
35355354Seric 			if (fuzzy)
3544174Seric 			{
35553735Seric 				/* name was a fuzzy match */
35651317Seric 				a->q_user = newstr(pw->pw_name);
35753735Seric 				if (findusercount++ > 3)
35853735Seric 				{
35958151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
36053735Seric 						pw->pw_name);
36153735Seric 					return (a);
36253735Seric 				}
36353735Seric 
36453735Seric 				/* see if it aliases */
36551317Seric 				(void) strcpy(buf, pw->pw_name);
36653735Seric 				goto trylocaluser;
3674174Seric 			}
36851317Seric 			a->q_home = newstr(pw->pw_dir);
36951317Seric 			a->q_uid = pw->pw_uid;
37051317Seric 			a->q_gid = pw->pw_gid;
37151317Seric 			a->q_flags |= QGOODUID;
37251317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
37351317Seric 			if (nbuf[0] != '\0')
37451317Seric 				a->q_fullname = newstr(nbuf);
37551317Seric 			if (!quoted)
37655012Seric 				forward(a, sendq, e);
3774174Seric 		}
3784174Seric 	}
37957642Seric 	if (!bitset(QDONTSEND, a->q_flags))
38057642Seric 		e->e_nrcpts++;
38112613Seric 	return (a);
3824174Seric }
3834174Seric /*
3844373Seric **  FINDUSER -- find the password entry for a user.
3854373Seric **
3864373Seric **	This looks a lot like getpwnam, except that it may want to
3874373Seric **	do some fancier pattern matching in /etc/passwd.
3884373Seric **
3899379Seric **	This routine contains most of the time of many sendmail runs.
3909379Seric **	It deserves to be optimized.
3919379Seric **
3924373Seric **	Parameters:
3934373Seric **		name -- the name to match against.
39455354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
39555354Seric **			was found using the fuzzy matching algorithm;
39655354Seric **			set to FALSE otherwise.
3974373Seric **
3984373Seric **	Returns:
3994373Seric **		A pointer to a pw struct.
4004373Seric **		NULL if name is unknown or ambiguous.
4014373Seric **
4024373Seric **	Side Effects:
4034407Seric **		may modify name.
4044373Seric */
4054373Seric 
4064373Seric struct passwd *
40755354Seric finduser(name, fuzzyp)
4084373Seric 	char *name;
40955354Seric 	bool *fuzzyp;
4104373Seric {
4114376Seric 	register struct passwd *pw;
4124407Seric 	register char *p;
41315325Seric 	extern struct passwd *getpwent();
41415325Seric 	extern struct passwd *getpwnam();
4154373Seric 
41655354Seric 	if (tTd(29, 4))
41755354Seric 		printf("finduser(%s): ", name);
41855354Seric 
41925777Seric 	/* map upper => lower case */
4204407Seric 	for (p = name; *p != '\0'; p++)
4214407Seric 	{
42225777Seric 		if (isascii(*p) && isupper(*p))
42325568Seric 			*p = tolower(*p);
4244407Seric 	}
42555354Seric 	*fuzzyp = FALSE;
4264407Seric 
42725777Seric 	/* look up this login name using fast path */
42812634Seric 	if ((pw = getpwnam(name)) != NULL)
42955354Seric 	{
43055354Seric 		if (tTd(29, 4))
43155354Seric 			printf("found (non-fuzzy)\n");
43212634Seric 		return (pw);
43355354Seric 	}
43412634Seric 
43553735Seric #ifdef MATCHGECOS
43653735Seric 	/* see if fuzzy matching allowed */
43753735Seric 	if (!MatchGecos)
43855354Seric 	{
43955354Seric 		if (tTd(29, 4))
44055354Seric 			printf("not found (fuzzy disabled)\n");
44153735Seric 		return NULL;
44255354Seric 	}
44353735Seric 
44412634Seric 	/* search for a matching full name instead */
44525777Seric 	for (p = name; *p != '\0'; p++)
44625777Seric 	{
44725777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
44825777Seric 			*p = ' ';
44925777Seric 	}
45023107Seric 	(void) setpwent();
4514376Seric 	while ((pw = getpwent()) != NULL)
4524376Seric 	{
4534998Seric 		char buf[MAXNAME];
4544376Seric 
4554998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
45656795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
4574381Seric 		{
45855354Seric 			if (tTd(29, 4))
45955354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
46058151Seric 			message("sending to login name %s", pw->pw_name);
46155354Seric 			*fuzzyp = TRUE;
4624376Seric 			return (pw);
4634377Seric 		}
4644376Seric 	}
46553735Seric #endif
46655354Seric 	if (tTd(29, 4))
46755354Seric 		printf("no fuzzy match found\n");
4684376Seric 	return (NULL);
4694373Seric }
4704373Seric /*
4714329Seric **  WRITABLE -- predicate returning if the file is writable.
4724329Seric **
4734329Seric **	This routine must duplicate the algorithm in sys/fio.c.
4744329Seric **	Unfortunately, we cannot use the access call since we
4754329Seric **	won't necessarily be the real uid when we try to
4764329Seric **	actually open the file.
4774329Seric **
4784329Seric **	Notice that ANY file with ANY execute bit is automatically
4794329Seric **	not writable.  This is also enforced by mailfile.
4804329Seric **
4814329Seric **	Parameters:
4824329Seric **		s -- pointer to a stat struct for the file.
4834329Seric **
4844329Seric **	Returns:
4854329Seric **		TRUE -- if we will be able to write this file.
4864329Seric **		FALSE -- if we cannot write this file.
4874329Seric **
4884329Seric **	Side Effects:
4894329Seric **		none.
4904329Seric */
4914329Seric 
4924329Seric bool
4934329Seric writable(s)
4944329Seric 	register struct stat *s;
4954329Seric {
49655372Seric 	uid_t euid;
49755372Seric 	gid_t egid;
4984329Seric 	int bits;
4994329Seric 
5004329Seric 	if (bitset(0111, s->st_mode))
5014329Seric 		return (FALSE);
5024329Seric 	euid = getruid();
5034329Seric 	egid = getrgid();
5044329Seric 	if (geteuid() == 0)
5054329Seric 	{
5064329Seric 		if (bitset(S_ISUID, s->st_mode))
5074329Seric 			euid = s->st_uid;
5084329Seric 		if (bitset(S_ISGID, s->st_mode))
5094329Seric 			egid = s->st_gid;
5104329Seric 	}
5114329Seric 
5124329Seric 	if (euid == 0)
5134329Seric 		return (TRUE);
5144329Seric 	bits = S_IWRITE;
5154329Seric 	if (euid != s->st_uid)
5164329Seric 	{
5174329Seric 		bits >>= 3;
5184329Seric 		if (egid != s->st_gid)
5194329Seric 			bits >>= 3;
5204329Seric 	}
5214329Seric 	return ((s->st_mode & bits) != 0);
5224329Seric }
5234329Seric /*
5244174Seric **  INCLUDE -- handle :include: specification.
5254174Seric **
5264174Seric **	Parameters:
5274174Seric **		fname -- filename to include.
52853037Seric **		forwarding -- if TRUE, we are reading a .forward file.
52953037Seric **			if FALSE, it's a :include: file.
5304399Seric **		ctladdr -- address template to use to fill in these
5314399Seric **			addresses -- effective user/group id are
5324399Seric **			the important things.
5335006Seric **		sendq -- a pointer to the head of the send queue
5345006Seric **			to put these addresses in.
5354174Seric **
5364174Seric **	Returns:
53757136Seric **		open error status
5384174Seric **
5394174Seric **	Side Effects:
5404174Seric **		reads the :include: file and sends to everyone
5414174Seric **		listed in that file.
5424174Seric */
5434174Seric 
54453037Seric static jmp_buf	CtxIncludeTimeout;
54553037Seric 
54657136Seric int
54755012Seric include(fname, forwarding, ctladdr, sendq, e)
5484174Seric 	char *fname;
54953037Seric 	bool forwarding;
5504399Seric 	ADDRESS *ctladdr;
5515006Seric 	ADDRESS **sendq;
55255012Seric 	ENVELOPE *e;
5534174Seric {
5544174Seric 	register FILE *fp;
55555012Seric 	char *oldto = e->e_to;
5569379Seric 	char *oldfilename = FileName;
5579379Seric 	int oldlinenumber = LineNumber;
55853037Seric 	register EVENT *ev = NULL;
55958082Seric 	int nincludes;
56053037Seric 	char buf[MAXLINE];
56153037Seric 	static int includetimeout();
5624174Seric 
56357186Seric 	if (tTd(27, 2))
56457186Seric 		printf("include(%s)\n", fname);
56557186Seric 
56653037Seric 	/*
56753037Seric 	**  If home directory is remote mounted but server is down,
56853037Seric 	**  this can hang or give errors; use a timeout to avoid this
56953037Seric 	*/
57053037Seric 
57153037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
57253037Seric 	{
57353037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
57453037Seric 		errno = 0;
57553037Seric 		usrerr("451 open timeout on %s", fname);
57657136Seric 		return ETIMEDOUT;
57753037Seric 	}
57853037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
57953037Seric 
58053037Seric 	/* if forwarding, the input file must be marked safe */
58153037Seric 	if (forwarding && !safefile(fname, ctladdr->q_uid, S_IREAD))
58253037Seric 	{
58353037Seric 		/* don't use this .forward file */
58453037Seric 		clrevent(ev);
58557186Seric 		if (tTd(27, 4))
58657186Seric 			printf("include: not safe (uid=%d)\n", ctladdr->q_uid);
58757136Seric 		return EPERM;
58853037Seric 	}
58953037Seric 
5904174Seric 	fp = fopen(fname, "r");
5914174Seric 	if (fp == NULL)
5924174Seric 	{
59357136Seric 		int ret = errno;
59457136Seric 
59558061Seric 		clrevent(ev);
59658151Seric 		usrerr("550 Cannot open %s", fname);
59757136Seric 		return ret;
5984174Seric 	}
59953037Seric 
6004406Seric 	if (getctladdr(ctladdr) == NULL)
6014406Seric 	{
6024406Seric 		struct stat st;
6034174Seric 
6044406Seric 		if (fstat(fileno(fp), &st) < 0)
60558061Seric 		{
60658061Seric 			int ret = errno;
60758061Seric 
60858061Seric 			clrevent(ev);
6094406Seric 			syserr("Cannot fstat %s!", fname);
61058061Seric 			return ret;
61158061Seric 		}
6124406Seric 		ctladdr->q_uid = st.st_uid;
6134406Seric 		ctladdr->q_gid = st.st_gid;
6144406Seric 		ctladdr->q_flags |= QGOODUID;
6154406Seric 	}
6164406Seric 
61753037Seric 	clrevent(ev);
61853037Seric 
61958092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
62058092Seric 	{
62158092Seric 		/* don't do any more now */
62258092Seric 		fclose(fp);
62358092Seric 		return 0;
62458092Seric 	}
62558092Seric 
6264174Seric 	/* read the file -- each line is a comma-separated list. */
6279379Seric 	FileName = fname;
6289379Seric 	LineNumber = 0;
62958082Seric 	ctladdr->q_flags &= ~QSELFREF;
63058082Seric 	nincludes = 0;
6314174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6324174Seric 	{
63356795Seric 		register char *p = strchr(buf, '\n');
6344174Seric 
63540963Sbostic 		LineNumber++;
6364174Seric 		if (p != NULL)
6374174Seric 			*p = '\0';
63857186Seric 		if (buf[0] == '#' || buf[0] == '\0')
63957139Seric 			continue;
64058008Seric 		e->e_to = NULL;
64158151Seric 		message("%s to %s",
64253037Seric 			forwarding ? "forwarding" : "sending", buf);
64357977Seric #ifdef LOG
64458020Seric 		if (forwarding && LogLevel > 9)
64557977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
64657977Seric 				e->e_id, oldto, buf);
64757977Seric #endif
64857977Seric 
6494176Seric 		AliasLevel++;
65058082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
6514176Seric 		AliasLevel--;
6524174Seric 	}
65358082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
65458065Seric 	{
65558065Seric 		if (tTd(27, 5))
65658065Seric 		{
65758065Seric 			printf("include: QDONTSEND ");
65858065Seric 			printaddr(ctladdr, FALSE);
65958065Seric 		}
66058065Seric 		ctladdr->q_flags |= QDONTSEND;
66158065Seric 	}
6624174Seric 
6634319Seric 	(void) fclose(fp);
6649379Seric 	FileName = oldfilename;
6659379Seric 	LineNumber = oldlinenumber;
66657136Seric 	return 0;
6674174Seric }
66853037Seric 
66953037Seric static
67053037Seric includetimeout()
67153037Seric {
67253037Seric 	longjmp(CtxIncludeTimeout, 1);
67353037Seric }
6744324Seric /*
6754324Seric **  SENDTOARGV -- send to an argument vector.
6764324Seric **
6774324Seric **	Parameters:
6784324Seric **		argv -- argument vector to send to.
6794324Seric **
6804324Seric **	Returns:
6814324Seric **		none.
6824324Seric **
6834324Seric **	Side Effects:
6844324Seric **		puts all addresses on the argument vector onto the
6854324Seric **			send queue.
6864324Seric */
6874324Seric 
68855012Seric sendtoargv(argv, e)
6894324Seric 	register char **argv;
69055012Seric 	register ENVELOPE *e;
6914324Seric {
6924324Seric 	register char *p;
6934324Seric 
6944324Seric 	while ((p = *argv++) != NULL)
6954324Seric 	{
69633725Sbostic 		if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at"))
6974324Seric 		{
6984324Seric 			char nbuf[MAXNAME];
6994324Seric 
7004324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
70158151Seric 				usrerr("554 address overflow");
7024324Seric 			else
7034324Seric 			{
7044324Seric 				(void) strcpy(nbuf, p);
7054324Seric 				(void) strcat(nbuf, "@");
7064324Seric 				(void) strcat(nbuf, argv[1]);
7074324Seric 				p = newstr(nbuf);
7084324Seric 				argv += 2;
7094324Seric 			}
7104324Seric 		}
71158082Seric 		(void) sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7124324Seric 	}
7134324Seric }
7144399Seric /*
7154399Seric **  GETCTLADDR -- get controlling address from an address header.
7164399Seric **
7174399Seric **	If none, get one corresponding to the effective userid.
7184399Seric **
7194399Seric **	Parameters:
7204399Seric **		a -- the address to find the controller of.
7214399Seric **
7224399Seric **	Returns:
7234399Seric **		the controlling address.
7244399Seric **
7254399Seric **	Side Effects:
7264399Seric **		none.
7274399Seric */
7284399Seric 
7294399Seric ADDRESS *
7304399Seric getctladdr(a)
7314399Seric 	register ADDRESS *a;
7324399Seric {
7334404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7344399Seric 		a = a->q_alias;
7354399Seric 	return (a);
7364399Seric }
737