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*58737Seric static char sccsid[] = "@(#)recipient.c	6.30 (Berkeley) 03/19/93";
1133731Sbostic #endif /* not lint */
1222710Sdist 
1358332Seric # include "sendmail.h"
1436928Sbostic # include <sys/stat.h>
154174Seric # include <pwd.h>
164174Seric 
174174Seric /*
189622Seric **  SENDTOLIST -- Designate a send list.
194174Seric **
204174Seric **	The parameter is a comma-separated list of people to send to.
214174Seric **	This routine arranges to send to all of them.
224174Seric **
234174Seric **	Parameters:
244174Seric **		list -- the send list.
254399Seric **		ctladdr -- the address template for the person to
264399Seric **			send to -- effective uid/gid are important.
275006Seric **			This is typically the alias that caused this
285006Seric **			expansion.
295006Seric **		sendq -- a pointer to the head of a queue to put
305006Seric **			these people into.
3158247Seric **		e -- the envelope in which to add these recipients.
324174Seric **
334174Seric **	Returns:
3458082Seric **		The number of addresses actually on the list.
354174Seric **
364174Seric **	Side Effects:
374174Seric **		none.
384174Seric */
394174Seric 
404174Seric # define MAXRCRSN	10
414174Seric 
4255012Seric sendtolist(list, ctladdr, sendq, e)
434174Seric 	char *list;
444399Seric 	ADDRESS *ctladdr;
455198Seric 	ADDRESS **sendq;
4655012Seric 	register ENVELOPE *e;
474174Seric {
484174Seric 	register char *p;
498223Seric 	register ADDRESS *al;	/* list of addresses to send to */
504423Seric 	bool firstone;		/* set on first address sent */
5111446Seric 	char delimiter;		/* the address delimiter */
5258082Seric 	int naddrs;
534174Seric 
547676Seric 	if (tTd(25, 1))
554444Seric 	{
564444Seric 		printf("sendto: %s\n   ctladdr=", list);
574444Seric 		printaddr(ctladdr, FALSE);
584444Seric 	}
594324Seric 
608223Seric 	/* heuristic to determine old versus new style addresses */
618230Seric 	if (ctladdr == NULL &&
6256795Seric 	    (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
6356795Seric 	     strchr(list, '<') != NULL || strchr(list, '(') != NULL))
6455012Seric 		e->e_flags &= ~EF_OLDSTYLE;
6511446Seric 	delimiter = ' ';
6655012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
6711446Seric 		delimiter = ',';
688223Seric 
694423Seric 	firstone = TRUE;
704324Seric 	al = NULL;
7158082Seric 	naddrs = 0;
728223Seric 
738081Seric 	for (p = list; *p != '\0'; )
744174Seric 	{
7558333Seric 		auto char *delimptr;
768081Seric 		register ADDRESS *a;
774319Seric 
788081Seric 		/* parse the address */
7958050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
804174Seric 			p++;
8158333Seric 		a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter, &delimptr, e);
8258333Seric 		p = delimptr;
839297Seric 		if (a == NULL)
844174Seric 			continue;
854324Seric 		a->q_next = al;
864399Seric 		a->q_alias = ctladdr;
874444Seric 
884444Seric 		/* see if this should be marked as a primary address */
894423Seric 		if (ctladdr == NULL ||
908081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
914423Seric 			a->q_flags |= QPRIMARY;
924444Seric 
939379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
9458061Seric 			ctladdr->q_flags |= QSELFREF;
9557731Seric 		al = a;
964423Seric 		firstone = FALSE;
974324Seric 	}
984324Seric 
994324Seric 	/* arrange to send to everyone on the local send list */
1004324Seric 	while (al != NULL)
1014324Seric 	{
1024324Seric 		register ADDRESS *a = al;
10312613Seric 		extern ADDRESS *recipient();
1044324Seric 
1054324Seric 		al = a->q_next;
10655012Seric 		a = recipient(a, sendq, e);
1074993Seric 
1084998Seric 		/* arrange to inherit full name */
1094998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1104998Seric 			a->q_fullname = ctladdr->q_fullname;
11158082Seric 		naddrs++;
1124174Seric 	}
1134324Seric 
11455012Seric 	e->e_to = NULL;
11558082Seric 	return (naddrs);
1164174Seric }
1174174Seric /*
1184174Seric **  RECIPIENT -- Designate a message recipient
1194174Seric **
1204174Seric **	Saves the named person for future mailing.
1214174Seric **
1224174Seric **	Parameters:
1234174Seric **		a -- the (preparsed) address header for the recipient.
1245006Seric **		sendq -- a pointer to the head of a queue to put the
1255006Seric **			recipient in.  Duplicate supression is done
1265006Seric **			in this queue.
12757731Seric **		e -- the current envelope.
1284174Seric **
1294174Seric **	Returns:
13012613Seric **		The actual address in the queue.  This will be "a" if
13112613Seric **		the address is not a duplicate, else the original address.
1324174Seric **
1334174Seric **	Side Effects:
1344174Seric **		none.
1354174Seric */
1364174Seric 
13746928Sbostic extern ADDRESS *getctladdr();
13846928Sbostic 
13912613Seric ADDRESS *
14055012Seric recipient(a, sendq, e)
1414174Seric 	register ADDRESS *a;
1425006Seric 	register ADDRESS **sendq;
14355012Seric 	register ENVELOPE *e;
1444174Seric {
1454174Seric 	register ADDRESS *q;
1464319Seric 	ADDRESS **pq;
1474174Seric 	register struct mailer *m;
1489210Seric 	register char *p;
1499210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
15053735Seric 	int findusercount = 0;
1519210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
15258247Seric 	extern int safefile();
1534174Seric 
15455012Seric 	e->e_to = a->q_paddr;
1554600Seric 	m = a->q_mailer;
1564174Seric 	errno = 0;
1577676Seric 	if (tTd(26, 1))
1584444Seric 	{
1594444Seric 		printf("\nrecipient: ");
1604444Seric 		printaddr(a, FALSE);
1614444Seric 	}
1624174Seric 
1634174Seric 	/* break aliasing loops */
1644174Seric 	if (AliasLevel > MAXRCRSN)
1654174Seric 	{
16658151Seric 		usrerr("554 aliasing/forwarding loop broken");
16712613Seric 		return (a);
1684174Seric 	}
1694174Seric 
1704174Seric 	/*
1714627Seric 	**  Finish setting up address structure.
1724174Seric 	*/
1734174Seric 
17416160Seric 	/* set the queue timeout */
175*58737Seric 	a->q_timeout = TimeOuts.to_q_return;
1764627Seric 
17716160Seric 	/* get unquoted user for file, program or user.name check */
1789210Seric 	(void) strcpy(buf, a->q_user);
1799210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1809210Seric 	{
18154993Seric 		if (*p == '\\')
1829210Seric 			quoted = TRUE;
1839210Seric 	}
18454983Seric 	stripquotes(buf);
1859210Seric 
18657402Seric 	/* check for direct mailing to restricted mailers */
187*58737Seric 	if (a->q_alias == NULL && m == ProgMailer &&
188*58737Seric 	    !bitset(EF_QUEUERUN, e->e_flags))
1894174Seric 	{
19058680Seric 		a->q_flags |= QBADADDR;
19158151Seric 		usrerr("550 Cannot mail directly to programs", m->m_name);
1924174Seric 	}
1934174Seric 
1944174Seric 	/*
1954419Seric 	**  Look up this person in the recipient list.
1964419Seric 	**	If they are there already, return, otherwise continue.
1974419Seric 	**	If the list is empty, just add it.  Notice the cute
1984419Seric 	**	hack to make from addresses suppress things correctly:
1994419Seric 	**	the QDONTSEND bit will be set in the send list.
2004419Seric 	**	[Please note: the emphasis is on "hack."]
2014174Seric 	*/
2024174Seric 
2035006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2044174Seric 	{
20558294Seric 		if (sameaddr(q, a))
2064174Seric 		{
2077676Seric 			if (tTd(26, 1))
2084444Seric 			{
2094444Seric 				printf("%s in sendq: ", a->q_paddr);
2104444Seric 				printaddr(q, FALSE);
2114444Seric 			}
2124423Seric 			if (!bitset(QPRIMARY, q->q_flags))
21358065Seric 			{
21458065Seric 				if (!bitset(QDONTSEND, a->q_flags))
21558151Seric 					message("duplicate suppressed");
2164423Seric 				q->q_flags |= a->q_flags;
21758065Seric 			}
21812613Seric 			return (q);
2194174Seric 		}
2204319Seric 	}
2214174Seric 
2224319Seric 	/* add address on list */
2234319Seric 	*pq = a;
2244174Seric 	a->q_next = NULL;
2254174Seric 
2264174Seric 	/*
22757402Seric 	**  Alias the name and handle special mailer types.
2284174Seric 	*/
2294174Seric 
23053735Seric   trylocaluser:
23155354Seric 	if (tTd(29, 7))
23255354Seric 		printf("at trylocaluser %s\n", a->q_user);
23355354Seric 
23458680Seric 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
23557402Seric 		return (a);
23657402Seric 
23757402Seric 	if (m == InclMailer)
2384174Seric 	{
23957402Seric 		a->q_flags |= QDONTSEND;
240*58737Seric 		if (a->q_alias == NULL && !bitset(EF_QUEUERUN, e->e_flags))
2414174Seric 		{
24258680Seric 			a->q_flags |= QBADADDR;
24358151Seric 			usrerr("550 Cannot mail directly to :include:s");
2444174Seric 		}
2454174Seric 		else
24650556Seric 		{
24758247Seric 			int err;
24858247Seric 
24958151Seric 			message("including file %s", a->q_user);
25058247Seric 			err = include(a->q_user, FALSE, a, sendq, e);
25158247Seric 			if (transienterror(err))
25258247Seric 				a->q_flags |= QQUEUEUP|QDONTSEND;
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 */
262*58737Seric 		if (a->q_alias == NULL && !bitset(EF_QUEUERUN, e->e_flags))
2634174Seric 		{
26458680Seric 			a->q_flags |= QBADADDR;
26558151Seric 			usrerr("550 Cannot mail directly to files");
2664174Seric 		}
26751317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
26858247Seric 		    (*p = '\0', safefile(buf, getruid(), S_IWRITE|S_IEXEC) != 0))
26951317Seric 		{
27058680Seric 			a->q_flags |= QBADADDR;
27158337Seric 			giveresponse(EX_CANTCREAT, m, NULL, 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 		{
29358247Seric 			a->q_flags |= QQUEUEUP|QDONTSEND;
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 */
30958247Seric 	if (bitset(QDONTSEND|QQUEUEUP|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 
33658247Seric 	if (!bitset(QDONTSEND|QQUEUEUP, 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 		{
34658680Seric 			a->q_flags |= QBADADDR;
34758337Seric 			giveresponse(EX_NOUSER, m, NULL, 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 				{
35958680Seric 					a->q_flags |= QBADADDR;
36058151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
36153735Seric 						pw->pw_name);
36253735Seric 					return (a);
36353735Seric 				}
36453735Seric 
36553735Seric 				/* see if it aliases */
36651317Seric 				(void) strcpy(buf, pw->pw_name);
36753735Seric 				goto trylocaluser;
3684174Seric 			}
36951317Seric 			a->q_home = newstr(pw->pw_dir);
37051317Seric 			a->q_uid = pw->pw_uid;
37151317Seric 			a->q_gid = pw->pw_gid;
37251317Seric 			a->q_flags |= QGOODUID;
37351317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
37451317Seric 			if (nbuf[0] != '\0')
37551317Seric 				a->q_fullname = newstr(nbuf);
37651317Seric 			if (!quoted)
37755012Seric 				forward(a, sendq, e);
3784174Seric 		}
3794174Seric 	}
38057642Seric 	if (!bitset(QDONTSEND, a->q_flags))
38157642Seric 		e->e_nrcpts++;
38212613Seric 	return (a);
3834174Seric }
3844174Seric /*
3854373Seric **  FINDUSER -- find the password entry for a user.
3864373Seric **
3874373Seric **	This looks a lot like getpwnam, except that it may want to
3884373Seric **	do some fancier pattern matching in /etc/passwd.
3894373Seric **
3909379Seric **	This routine contains most of the time of many sendmail runs.
3919379Seric **	It deserves to be optimized.
3929379Seric **
3934373Seric **	Parameters:
3944373Seric **		name -- the name to match against.
39555354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
39655354Seric **			was found using the fuzzy matching algorithm;
39755354Seric **			set to FALSE otherwise.
3984373Seric **
3994373Seric **	Returns:
4004373Seric **		A pointer to a pw struct.
4014373Seric **		NULL if name is unknown or ambiguous.
4024373Seric **
4034373Seric **	Side Effects:
4044407Seric **		may modify name.
4054373Seric */
4064373Seric 
4074373Seric struct passwd *
40855354Seric finduser(name, fuzzyp)
4094373Seric 	char *name;
41055354Seric 	bool *fuzzyp;
4114373Seric {
4124376Seric 	register struct passwd *pw;
4134407Seric 	register char *p;
41415325Seric 	extern struct passwd *getpwent();
41515325Seric 	extern struct passwd *getpwnam();
4164373Seric 
41755354Seric 	if (tTd(29, 4))
41855354Seric 		printf("finduser(%s): ", name);
41955354Seric 
42025777Seric 	/* map upper => lower case */
4214407Seric 	for (p = name; *p != '\0'; p++)
4224407Seric 	{
42325777Seric 		if (isascii(*p) && isupper(*p))
42425568Seric 			*p = tolower(*p);
4254407Seric 	}
42655354Seric 	*fuzzyp = FALSE;
4274407Seric 
42825777Seric 	/* look up this login name using fast path */
42912634Seric 	if ((pw = getpwnam(name)) != NULL)
43055354Seric 	{
43155354Seric 		if (tTd(29, 4))
43255354Seric 			printf("found (non-fuzzy)\n");
43312634Seric 		return (pw);
43455354Seric 	}
43512634Seric 
43653735Seric #ifdef MATCHGECOS
43753735Seric 	/* see if fuzzy matching allowed */
43853735Seric 	if (!MatchGecos)
43955354Seric 	{
44055354Seric 		if (tTd(29, 4))
44155354Seric 			printf("not found (fuzzy disabled)\n");
44253735Seric 		return NULL;
44355354Seric 	}
44453735Seric 
44512634Seric 	/* search for a matching full name instead */
44625777Seric 	for (p = name; *p != '\0'; p++)
44725777Seric 	{
44825777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
44925777Seric 			*p = ' ';
45025777Seric 	}
45123107Seric 	(void) setpwent();
4524376Seric 	while ((pw = getpwent()) != NULL)
4534376Seric 	{
4544998Seric 		char buf[MAXNAME];
4554376Seric 
4564998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
45756795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
4584381Seric 		{
45955354Seric 			if (tTd(29, 4))
46055354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
46158151Seric 			message("sending to login name %s", pw->pw_name);
46255354Seric 			*fuzzyp = TRUE;
4634376Seric 			return (pw);
4644377Seric 		}
4654376Seric 	}
46653735Seric #endif
46755354Seric 	if (tTd(29, 4))
46855354Seric 		printf("no fuzzy match found\n");
4694376Seric 	return (NULL);
4704373Seric }
4714373Seric /*
4724329Seric **  WRITABLE -- predicate returning if the file is writable.
4734329Seric **
4744329Seric **	This routine must duplicate the algorithm in sys/fio.c.
4754329Seric **	Unfortunately, we cannot use the access call since we
4764329Seric **	won't necessarily be the real uid when we try to
4774329Seric **	actually open the file.
4784329Seric **
4794329Seric **	Notice that ANY file with ANY execute bit is automatically
4804329Seric **	not writable.  This is also enforced by mailfile.
4814329Seric **
4824329Seric **	Parameters:
4834329Seric **		s -- pointer to a stat struct for the file.
4844329Seric **
4854329Seric **	Returns:
4864329Seric **		TRUE -- if we will be able to write this file.
4874329Seric **		FALSE -- if we cannot write this file.
4884329Seric **
4894329Seric **	Side Effects:
4904329Seric **		none.
4914329Seric */
4924329Seric 
4934329Seric bool
4944329Seric writable(s)
4954329Seric 	register struct stat *s;
4964329Seric {
49755372Seric 	uid_t euid;
49855372Seric 	gid_t egid;
4994329Seric 	int bits;
5004329Seric 
5014329Seric 	if (bitset(0111, s->st_mode))
5024329Seric 		return (FALSE);
5034329Seric 	euid = getruid();
5044329Seric 	egid = getrgid();
5054329Seric 	if (geteuid() == 0)
5064329Seric 	{
5074329Seric 		if (bitset(S_ISUID, s->st_mode))
5084329Seric 			euid = s->st_uid;
5094329Seric 		if (bitset(S_ISGID, s->st_mode))
5104329Seric 			egid = s->st_gid;
5114329Seric 	}
5124329Seric 
5134329Seric 	if (euid == 0)
5144329Seric 		return (TRUE);
5154329Seric 	bits = S_IWRITE;
5164329Seric 	if (euid != s->st_uid)
5174329Seric 	{
5184329Seric 		bits >>= 3;
5194329Seric 		if (egid != s->st_gid)
5204329Seric 			bits >>= 3;
5214329Seric 	}
5224329Seric 	return ((s->st_mode & bits) != 0);
5234329Seric }
5244329Seric /*
5254174Seric **  INCLUDE -- handle :include: specification.
5264174Seric **
5274174Seric **	Parameters:
5284174Seric **		fname -- filename to include.
52953037Seric **		forwarding -- if TRUE, we are reading a .forward file.
53053037Seric **			if FALSE, it's a :include: file.
5314399Seric **		ctladdr -- address template to use to fill in these
5324399Seric **			addresses -- effective user/group id are
5334399Seric **			the important things.
5345006Seric **		sendq -- a pointer to the head of the send queue
5355006Seric **			to put these addresses in.
5364174Seric **
5374174Seric **	Returns:
53857136Seric **		open error status
5394174Seric **
5404174Seric **	Side Effects:
5414174Seric **		reads the :include: file and sends to everyone
5424174Seric **		listed in that file.
5434174Seric */
5444174Seric 
54553037Seric static jmp_buf	CtxIncludeTimeout;
54653037Seric 
54757136Seric int
54855012Seric include(fname, forwarding, ctladdr, sendq, e)
5494174Seric 	char *fname;
55053037Seric 	bool forwarding;
5514399Seric 	ADDRESS *ctladdr;
5525006Seric 	ADDRESS **sendq;
55355012Seric 	ENVELOPE *e;
5544174Seric {
5554174Seric 	register FILE *fp;
55655012Seric 	char *oldto = e->e_to;
5579379Seric 	char *oldfilename = FileName;
5589379Seric 	int oldlinenumber = LineNumber;
55953037Seric 	register EVENT *ev = NULL;
56058082Seric 	int nincludes;
56158247Seric 	int ret;
56253037Seric 	char buf[MAXLINE];
56353037Seric 	static int includetimeout();
5644174Seric 
56557186Seric 	if (tTd(27, 2))
56657186Seric 		printf("include(%s)\n", fname);
56757186Seric 
56853037Seric 	/*
56953037Seric 	**  If home directory is remote mounted but server is down,
57053037Seric 	**  this can hang or give errors; use a timeout to avoid this
57153037Seric 	*/
57253037Seric 
57353037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
57453037Seric 	{
57553037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
57653037Seric 		errno = 0;
57753037Seric 		usrerr("451 open timeout on %s", fname);
57857136Seric 		return ETIMEDOUT;
57953037Seric 	}
58053037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
58153037Seric 
58253037Seric 	/* if forwarding, the input file must be marked safe */
58358247Seric 	if (forwarding && (ret = safefile(fname, ctladdr->q_uid, S_IREAD)) != 0)
58453037Seric 	{
58553037Seric 		/* don't use this .forward file */
58653037Seric 		clrevent(ev);
58757186Seric 		if (tTd(27, 4))
58858247Seric 			printf("include: not safe (uid=%d): %s\n",
58958247Seric 				ctladdr->q_uid, errstring(ret));
59058247Seric 		return ret;
59153037Seric 	}
59253037Seric 
5934174Seric 	fp = fopen(fname, "r");
5944174Seric 	if (fp == NULL)
5954174Seric 	{
59657136Seric 		int ret = errno;
59757136Seric 
59858061Seric 		clrevent(ev);
59958690Seric 		if (transienterror(ret))
60058690Seric 		{
60158690Seric 			ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
60258690Seric 			errno = 0;
60358690Seric 			usrerr("451 Cannot open %s: %s", fname, errstring(ret));
60458690Seric 		}
60558690Seric 		else
60658690Seric 		{
60758690Seric 			usrerr("550 Cannot open %s: %s", fname, errstring(ret));
60858690Seric 		}
60957136Seric 		return ret;
6104174Seric 	}
61153037Seric 
6124406Seric 	if (getctladdr(ctladdr) == NULL)
6134406Seric 	{
6144406Seric 		struct stat st;
6154174Seric 
6164406Seric 		if (fstat(fileno(fp), &st) < 0)
61758061Seric 		{
61858061Seric 			int ret = errno;
61958061Seric 
62058061Seric 			clrevent(ev);
6214406Seric 			syserr("Cannot fstat %s!", fname);
62258061Seric 			return ret;
62358061Seric 		}
6244406Seric 		ctladdr->q_uid = st.st_uid;
6254406Seric 		ctladdr->q_gid = st.st_gid;
6264406Seric 		ctladdr->q_flags |= QGOODUID;
6274406Seric 	}
6284406Seric 
62953037Seric 	clrevent(ev);
63053037Seric 
63158092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
63258092Seric 	{
63358092Seric 		/* don't do any more now */
63458680Seric 		xfclose(fp, "include", fname);
63558092Seric 		return 0;
63658092Seric 	}
63758092Seric 
6384174Seric 	/* read the file -- each line is a comma-separated list. */
6399379Seric 	FileName = fname;
6409379Seric 	LineNumber = 0;
64158082Seric 	ctladdr->q_flags &= ~QSELFREF;
64258082Seric 	nincludes = 0;
6434174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6444174Seric 	{
64556795Seric 		register char *p = strchr(buf, '\n');
6464174Seric 
64740963Sbostic 		LineNumber++;
6484174Seric 		if (p != NULL)
6494174Seric 			*p = '\0';
65057186Seric 		if (buf[0] == '#' || buf[0] == '\0')
65157139Seric 			continue;
65258008Seric 		e->e_to = NULL;
65358151Seric 		message("%s to %s",
65453037Seric 			forwarding ? "forwarding" : "sending", buf);
65557977Seric #ifdef LOG
65658020Seric 		if (forwarding && LogLevel > 9)
65757977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
65857977Seric 				e->e_id, oldto, buf);
65957977Seric #endif
66057977Seric 
6614176Seric 		AliasLevel++;
66258082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
6634176Seric 		AliasLevel--;
6644174Seric 	}
66558082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
66658065Seric 	{
66758065Seric 		if (tTd(27, 5))
66858065Seric 		{
66958065Seric 			printf("include: QDONTSEND ");
67058065Seric 			printaddr(ctladdr, FALSE);
67158065Seric 		}
67258065Seric 		ctladdr->q_flags |= QDONTSEND;
67358065Seric 	}
6744174Seric 
67558680Seric 	(void) xfclose(fp, "include", fname);
6769379Seric 	FileName = oldfilename;
6779379Seric 	LineNumber = oldlinenumber;
67857136Seric 	return 0;
6794174Seric }
68053037Seric 
68153037Seric static
68253037Seric includetimeout()
68353037Seric {
68453037Seric 	longjmp(CtxIncludeTimeout, 1);
68553037Seric }
6864324Seric /*
6874324Seric **  SENDTOARGV -- send to an argument vector.
6884324Seric **
6894324Seric **	Parameters:
6904324Seric **		argv -- argument vector to send to.
69158247Seric **		e -- the current envelope.
6924324Seric **
6934324Seric **	Returns:
6944324Seric **		none.
6954324Seric **
6964324Seric **	Side Effects:
6974324Seric **		puts all addresses on the argument vector onto the
6984324Seric **			send queue.
6994324Seric */
7004324Seric 
70155012Seric sendtoargv(argv, e)
7024324Seric 	register char **argv;
70355012Seric 	register ENVELOPE *e;
7044324Seric {
7054324Seric 	register char *p;
7064324Seric 
7074324Seric 	while ((p = *argv++) != NULL)
7084324Seric 	{
70958082Seric 		(void) sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7104324Seric 	}
7114324Seric }
7124399Seric /*
7134399Seric **  GETCTLADDR -- get controlling address from an address header.
7144399Seric **
7154399Seric **	If none, get one corresponding to the effective userid.
7164399Seric **
7174399Seric **	Parameters:
7184399Seric **		a -- the address to find the controller of.
7194399Seric **
7204399Seric **	Returns:
7214399Seric **		the controlling address.
7224399Seric **
7234399Seric **	Side Effects:
7244399Seric **		none.
7254399Seric */
7264399Seric 
7274399Seric ADDRESS *
7284399Seric getctladdr(a)
7294399Seric 	register ADDRESS *a;
7304399Seric {
7314404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7324399Seric 		a = a->q_alias;
7334399Seric 	return (a);
7344399Seric }
735