122710Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
363589Sbostic  * Copyright (c) 1988, 1993
463589Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822710Sdist 
922710Sdist #ifndef lint
10*63993Seric static char sccsid[] = "@(#)recipient.c	8.11 (Berkeley) 07/22/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;
5263847Seric 	char *oldto = e->e_to;
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;
1034324Seric 
1044324Seric 		al = a->q_next;
10555012Seric 		a = recipient(a, sendq, e);
1064993Seric 
1074998Seric 		/* arrange to inherit full name */
1084998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1094998Seric 			a->q_fullname = ctladdr->q_fullname;
11058082Seric 		naddrs++;
1114174Seric 	}
1124324Seric 
11363847Seric 	e->e_to = oldto;
11458082Seric 	return (naddrs);
1154174Seric }
1164174Seric /*
1174174Seric **  RECIPIENT -- Designate a message recipient
1184174Seric **
1194174Seric **	Saves the named person for future mailing.
1204174Seric **
1214174Seric **	Parameters:
1224174Seric **		a -- the (preparsed) address header for the recipient.
1235006Seric **		sendq -- a pointer to the head of a queue to put the
1245006Seric **			recipient in.  Duplicate supression is done
1255006Seric **			in this queue.
12657731Seric **		e -- the current envelope.
1274174Seric **
1284174Seric **	Returns:
12912613Seric **		The actual address in the queue.  This will be "a" if
13012613Seric **		the address is not a duplicate, else the original address.
1314174Seric **
1324174Seric **	Side Effects:
1334174Seric **		none.
1344174Seric */
1354174Seric 
13612613Seric ADDRESS *
13755012Seric recipient(a, sendq, e)
1384174Seric 	register ADDRESS *a;
1395006Seric 	register ADDRESS **sendq;
14055012Seric 	register ENVELOPE *e;
1414174Seric {
1424174Seric 	register ADDRESS *q;
1434319Seric 	ADDRESS **pq;
1444174Seric 	register struct mailer *m;
1459210Seric 	register char *p;
1469210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
14753735Seric 	int findusercount = 0;
1489210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
14958247Seric 	extern int safefile();
1504174Seric 
15155012Seric 	e->e_to = a->q_paddr;
1524600Seric 	m = a->q_mailer;
1534174Seric 	errno = 0;
1547676Seric 	if (tTd(26, 1))
1554444Seric 	{
1564444Seric 		printf("\nrecipient: ");
1574444Seric 		printaddr(a, FALSE);
1584444Seric 	}
1594174Seric 
1604174Seric 	/* break aliasing loops */
1614174Seric 	if (AliasLevel > MAXRCRSN)
1624174Seric 	{
16358151Seric 		usrerr("554 aliasing/forwarding loop broken");
16412613Seric 		return (a);
1654174Seric 	}
1664174Seric 
1674174Seric 	/*
1684627Seric 	**  Finish setting up address structure.
1694174Seric 	*/
1704174Seric 
17116160Seric 	/* set the queue timeout */
17258737Seric 	a->q_timeout = TimeOuts.to_q_return;
1734627Seric 
17416160Seric 	/* get unquoted user for file, program or user.name check */
1759210Seric 	(void) strcpy(buf, a->q_user);
1769210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1779210Seric 	{
17854993Seric 		if (*p == '\\')
1799210Seric 			quoted = TRUE;
1809210Seric 	}
18154983Seric 	stripquotes(buf);
1829210Seric 
18357402Seric 	/* check for direct mailing to restricted mailers */
18458737Seric 	if (a->q_alias == NULL && m == ProgMailer &&
18558737Seric 	    !bitset(EF_QUEUERUN, e->e_flags))
1864174Seric 	{
18758680Seric 		a->q_flags |= QBADADDR;
18863847Seric 		usrerr("550 Cannot mail directly to programs");
1894174Seric 	}
1904174Seric 
1914174Seric 	/*
1924419Seric 	**  Look up this person in the recipient list.
1934419Seric 	**	If they are there already, return, otherwise continue.
1944419Seric 	**	If the list is empty, just add it.  Notice the cute
1954419Seric 	**	hack to make from addresses suppress things correctly:
1964419Seric 	**	the QDONTSEND bit will be set in the send list.
1974419Seric 	**	[Please note: the emphasis is on "hack."]
1984174Seric 	*/
1994174Seric 
2005006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2014174Seric 	{
20258294Seric 		if (sameaddr(q, a))
2034174Seric 		{
2047676Seric 			if (tTd(26, 1))
2054444Seric 			{
2064444Seric 				printf("%s in sendq: ", a->q_paddr);
2074444Seric 				printaddr(q, FALSE);
2084444Seric 			}
2094423Seric 			if (!bitset(QPRIMARY, q->q_flags))
21058065Seric 			{
21158065Seric 				if (!bitset(QDONTSEND, a->q_flags))
21258151Seric 					message("duplicate suppressed");
2134423Seric 				q->q_flags |= a->q_flags;
21458065Seric 			}
21563847Seric 			a = q;
21663847Seric 			goto testselfdestruct;
2174174Seric 		}
2184319Seric 	}
2194174Seric 
2204319Seric 	/* add address on list */
22158884Seric 	*pq = a;
22258884Seric 	a->q_next = NULL;
2234174Seric 
2244174Seric 	/*
22557402Seric 	**  Alias the name and handle special mailer types.
2264174Seric 	*/
2274174Seric 
22853735Seric   trylocaluser:
22955354Seric 	if (tTd(29, 7))
23055354Seric 		printf("at trylocaluser %s\n", a->q_user);
23155354Seric 
23258680Seric 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
23363847Seric 		goto testselfdestruct;
23457402Seric 
23557402Seric 	if (m == InclMailer)
2364174Seric 	{
23757402Seric 		a->q_flags |= QDONTSEND;
23858737Seric 		if (a->q_alias == NULL && !bitset(EF_QUEUERUN, e->e_flags))
2394174Seric 		{
24058680Seric 			a->q_flags |= QBADADDR;
24158151Seric 			usrerr("550 Cannot mail directly to :include:s");
2424174Seric 		}
2434174Seric 		else
24450556Seric 		{
24559563Seric 			int ret;
24658247Seric 
24758151Seric 			message("including file %s", a->q_user);
24859563Seric 			ret = include(a->q_user, FALSE, a, sendq, e);
24959563Seric 			if (transienterror(ret))
25059563Seric 			{
25159563Seric #ifdef LOG
25259563Seric 				if (LogLevel > 2)
25359615Seric 					syslog(LOG_ERR, "%s: include %s: transient error: %e",
25459623Seric 						e->e_id, a->q_user, errstring(ret));
25559563Seric #endif
25663853Seric 				a->q_flags |= QQUEUEUP;
25759563Seric 				usrerr("451 Cannot open %s: %s",
25859563Seric 					a->q_user, errstring(ret));
25959563Seric 			}
26059563Seric 			else if (ret != 0)
26159563Seric 			{
26263938Seric 				a->q_flags |= QBADADDR;
26359563Seric 				usrerr("550 Cannot open %s: %s",
26459563Seric 					a->q_user, errstring(ret));
26559563Seric 			}
26650556Seric 		}
2674174Seric 	}
26857642Seric 	else if (m == FileMailer)
2694174Seric 	{
2704329Seric 		struct stat stb;
2714329Seric 		extern bool writable();
2724174Seric 
27356795Seric 		p = strrchr(buf, '/');
27451317Seric 		/* check if writable or creatable */
27558737Seric 		if (a->q_alias == NULL && !bitset(EF_QUEUERUN, e->e_flags))
2764174Seric 		{
27758680Seric 			a->q_flags |= QBADADDR;
27858151Seric 			usrerr("550 Cannot mail directly to files");
2794174Seric 		}
28051317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
28163787Seric 		    (*p = '\0', safefile(buf, RealUid, TRUE, S_IWRITE|S_IEXEC) != 0))
28251317Seric 		{
28358680Seric 			a->q_flags |= QBADADDR;
28458337Seric 			giveresponse(EX_CANTCREAT, m, NULL, e);
28551317Seric 		}
28651317Seric 	}
28751317Seric 
28857402Seric 	if (m != LocalMailer)
28957642Seric 	{
29057642Seric 		if (!bitset(QDONTSEND, a->q_flags))
29157642Seric 			e->e_nrcpts++;
29263847Seric 		goto testselfdestruct;
29357642Seric 	}
29457402Seric 
29557402Seric 	/* try aliasing */
29657402Seric 	alias(a, sendq, e);
29757402Seric 
29857402Seric # ifdef USERDB
29957402Seric 	/* if not aliased, look it up in the user database */
30058918Seric 	if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags))
30157402Seric 	{
30257402Seric 		extern int udbexpand();
30359615Seric 		extern int errno;
30457402Seric 
30557402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
30657402Seric 		{
30763853Seric 			a->q_flags |= QQUEUEUP;
30857402Seric 			if (e->e_message == NULL)
30957402Seric 				e->e_message = newstr("Deferred: user database error");
31057402Seric # ifdef LOG
31158020Seric 			if (LogLevel > 8)
31259623Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand: %s",
31359623Seric 					e->e_id, errstring(errno));
31457402Seric # endif
31559615Seric 			message("queued (user database error): %s",
31659615Seric 				errstring(errno));
31757642Seric 			e->e_nrcpts++;
31863847Seric 			goto testselfdestruct;
31957402Seric 		}
32057402Seric 	}
32157402Seric # endif
32257402Seric 
32357402Seric 	/* if it was an alias or a UDB expansion, just return now */
32458247Seric 	if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags))
32563847Seric 		goto testselfdestruct;
32657402Seric 
32751317Seric 	/*
32851317Seric 	**  If we have a level two config file, then pass the name through
32951317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
33051317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
33151317Seric 	**  after local aliasing has been done.
33251317Seric 	*/
33351317Seric 
33451317Seric 	if (tTd(29, 5))
33551317Seric 	{
33651317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
33751317Seric 			ConfigLevel, RewriteRules[5]);
33851317Seric 		printaddr(a, FALSE);
33951317Seric 	}
34051317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
34151317Seric 	    RewriteRules[5] != NULL)
34251317Seric 	{
34355012Seric 		maplocaluser(a, sendq, e);
34451317Seric 	}
34551317Seric 
34651317Seric 	/*
34751317Seric 	**  If it didn't get rewritten to another mailer, go ahead
34851317Seric 	**  and deliver it.
34951317Seric 	*/
35051317Seric 
35158247Seric 	if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags))
35251317Seric 	{
35355354Seric 		auto bool fuzzy;
35451317Seric 		register struct passwd *pw;
35551317Seric 		extern struct passwd *finduser();
35651317Seric 
35751317Seric 		/* warning -- finduser may trash buf */
35855354Seric 		pw = finduser(buf, &fuzzy);
35951317Seric 		if (pw == NULL)
36051317Seric 		{
36158680Seric 			a->q_flags |= QBADADDR;
36258337Seric 			giveresponse(EX_NOUSER, m, NULL, e);
36351317Seric 		}
3644174Seric 		else
3654174Seric 		{
36651317Seric 			char nbuf[MAXNAME];
3674373Seric 
36855354Seric 			if (fuzzy)
3694174Seric 			{
37053735Seric 				/* name was a fuzzy match */
37151317Seric 				a->q_user = newstr(pw->pw_name);
37253735Seric 				if (findusercount++ > 3)
37353735Seric 				{
37458680Seric 					a->q_flags |= QBADADDR;
37558151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
37653735Seric 						pw->pw_name);
37753735Seric 					return (a);
37853735Seric 				}
37953735Seric 
38053735Seric 				/* see if it aliases */
38151317Seric 				(void) strcpy(buf, pw->pw_name);
38253735Seric 				goto trylocaluser;
3834174Seric 			}
38451317Seric 			a->q_home = newstr(pw->pw_dir);
38551317Seric 			a->q_uid = pw->pw_uid;
38651317Seric 			a->q_gid = pw->pw_gid;
38759083Seric 			a->q_ruser = newstr(pw->pw_name);
38851317Seric 			a->q_flags |= QGOODUID;
38951317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
39051317Seric 			if (nbuf[0] != '\0')
39151317Seric 				a->q_fullname = newstr(nbuf);
39251317Seric 			if (!quoted)
39355012Seric 				forward(a, sendq, e);
3944174Seric 		}
3954174Seric 	}
39657642Seric 	if (!bitset(QDONTSEND, a->q_flags))
39757642Seric 		e->e_nrcpts++;
39863847Seric 
39963847Seric   testselfdestruct:
40063978Seric 	if (tTd(26, 8))
40163847Seric 	{
40263978Seric 		printf("testselfdestruct: ");
40363978Seric 		printaddr(a, TRUE);
40463978Seric 	}
40563978Seric 	if (a->q_alias == NULL && a != &e->e_from &&
40663978Seric 	    bitset(QDONTSEND, a->q_flags))
40763978Seric 	{
40863978Seric 		q = *sendq;
40963965Seric 		while (q != NULL && bitset(QDONTSEND, q->q_flags))
41063847Seric 			q = q->q_next;
41163978Seric 		if (q == NULL)
41263847Seric 		{
41363847Seric 			a->q_flags |= QBADADDR;
41463847Seric 			usrerr("554 aliasing/forwarding loop broken");
41563847Seric 		}
41663847Seric 	}
41712613Seric 	return (a);
4184174Seric }
4194174Seric /*
4204373Seric **  FINDUSER -- find the password entry for a user.
4214373Seric **
4224373Seric **	This looks a lot like getpwnam, except that it may want to
4234373Seric **	do some fancier pattern matching in /etc/passwd.
4244373Seric **
4259379Seric **	This routine contains most of the time of many sendmail runs.
4269379Seric **	It deserves to be optimized.
4279379Seric **
4284373Seric **	Parameters:
4294373Seric **		name -- the name to match against.
43055354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
43155354Seric **			was found using the fuzzy matching algorithm;
43255354Seric **			set to FALSE otherwise.
4334373Seric **
4344373Seric **	Returns:
4354373Seric **		A pointer to a pw struct.
4364373Seric **		NULL if name is unknown or ambiguous.
4374373Seric **
4384373Seric **	Side Effects:
4394407Seric **		may modify name.
4404373Seric */
4414373Seric 
4424373Seric struct passwd *
44355354Seric finduser(name, fuzzyp)
4444373Seric 	char *name;
44555354Seric 	bool *fuzzyp;
4464373Seric {
4474376Seric 	register struct passwd *pw;
4484407Seric 	register char *p;
44915325Seric 	extern struct passwd *getpwent();
45015325Seric 	extern struct passwd *getpwnam();
4514373Seric 
45255354Seric 	if (tTd(29, 4))
45355354Seric 		printf("finduser(%s): ", name);
45455354Seric 
45555354Seric 	*fuzzyp = FALSE;
4564407Seric 
45725777Seric 	/* look up this login name using fast path */
45812634Seric 	if ((pw = getpwnam(name)) != NULL)
45955354Seric 	{
46055354Seric 		if (tTd(29, 4))
46155354Seric 			printf("found (non-fuzzy)\n");
46212634Seric 		return (pw);
46355354Seric 	}
46412634Seric 
46553735Seric #ifdef MATCHGECOS
46653735Seric 	/* see if fuzzy matching allowed */
46753735Seric 	if (!MatchGecos)
46855354Seric 	{
46955354Seric 		if (tTd(29, 4))
47055354Seric 			printf("not found (fuzzy disabled)\n");
47153735Seric 		return NULL;
47255354Seric 	}
47353735Seric 
47412634Seric 	/* search for a matching full name instead */
47525777Seric 	for (p = name; *p != '\0'; p++)
47625777Seric 	{
47725777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
47825777Seric 			*p = ' ';
47925777Seric 	}
48023107Seric 	(void) setpwent();
4814376Seric 	while ((pw = getpwent()) != NULL)
4824376Seric 	{
4834998Seric 		char buf[MAXNAME];
4844376Seric 
4854998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
48656795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
4874381Seric 		{
48855354Seric 			if (tTd(29, 4))
48955354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
49058151Seric 			message("sending to login name %s", pw->pw_name);
49155354Seric 			*fuzzyp = TRUE;
4924376Seric 			return (pw);
4934377Seric 		}
4944376Seric 	}
49555354Seric 	if (tTd(29, 4))
49655354Seric 		printf("no fuzzy match found\n");
49759015Seric #else
49859015Seric 	if (tTd(29, 4))
49959015Seric 		printf("not found (fuzzy disabled)\n");
50059015Seric #endif
5014376Seric 	return (NULL);
5024373Seric }
5034373Seric /*
5044329Seric **  WRITABLE -- predicate returning if the file is writable.
5054329Seric **
5064329Seric **	This routine must duplicate the algorithm in sys/fio.c.
5074329Seric **	Unfortunately, we cannot use the access call since we
5084329Seric **	won't necessarily be the real uid when we try to
5094329Seric **	actually open the file.
5104329Seric **
5114329Seric **	Notice that ANY file with ANY execute bit is automatically
5124329Seric **	not writable.  This is also enforced by mailfile.
5134329Seric **
5144329Seric **	Parameters:
5154329Seric **		s -- pointer to a stat struct for the file.
5164329Seric **
5174329Seric **	Returns:
5184329Seric **		TRUE -- if we will be able to write this file.
5194329Seric **		FALSE -- if we cannot write this file.
5204329Seric **
5214329Seric **	Side Effects:
5224329Seric **		none.
5234329Seric */
5244329Seric 
5254329Seric bool
5264329Seric writable(s)
5274329Seric 	register struct stat *s;
5284329Seric {
52955372Seric 	uid_t euid;
53055372Seric 	gid_t egid;
5314329Seric 	int bits;
5324329Seric 
5334329Seric 	if (bitset(0111, s->st_mode))
5344329Seric 		return (FALSE);
53563787Seric 	euid = RealUid;
53663787Seric 	egid = RealGid;
5374329Seric 	if (geteuid() == 0)
5384329Seric 	{
5394329Seric 		if (bitset(S_ISUID, s->st_mode))
5404329Seric 			euid = s->st_uid;
5414329Seric 		if (bitset(S_ISGID, s->st_mode))
5424329Seric 			egid = s->st_gid;
5434329Seric 	}
5444329Seric 
5454329Seric 	if (euid == 0)
5464329Seric 		return (TRUE);
5474329Seric 	bits = S_IWRITE;
5484329Seric 	if (euid != s->st_uid)
5494329Seric 	{
5504329Seric 		bits >>= 3;
5514329Seric 		if (egid != s->st_gid)
5524329Seric 			bits >>= 3;
5534329Seric 	}
5544329Seric 	return ((s->st_mode & bits) != 0);
5554329Seric }
5564329Seric /*
5574174Seric **  INCLUDE -- handle :include: specification.
5584174Seric **
5594174Seric **	Parameters:
5604174Seric **		fname -- filename to include.
56153037Seric **		forwarding -- if TRUE, we are reading a .forward file.
56253037Seric **			if FALSE, it's a :include: file.
5634399Seric **		ctladdr -- address template to use to fill in these
5644399Seric **			addresses -- effective user/group id are
5654399Seric **			the important things.
5665006Seric **		sendq -- a pointer to the head of the send queue
5675006Seric **			to put these addresses in.
5684174Seric **
5694174Seric **	Returns:
57057136Seric **		open error status
5714174Seric **
5724174Seric **	Side Effects:
5734174Seric **		reads the :include: file and sends to everyone
5744174Seric **		listed in that file.
5754174Seric */
5764174Seric 
57753037Seric static jmp_buf	CtxIncludeTimeout;
57863937Seric static int	includetimeout();
57953037Seric 
58057136Seric int
58155012Seric include(fname, forwarding, ctladdr, sendq, e)
5824174Seric 	char *fname;
58353037Seric 	bool forwarding;
5844399Seric 	ADDRESS *ctladdr;
5855006Seric 	ADDRESS **sendq;
58655012Seric 	ENVELOPE *e;
5874174Seric {
5884174Seric 	register FILE *fp;
58955012Seric 	char *oldto = e->e_to;
5909379Seric 	char *oldfilename = FileName;
5919379Seric 	int oldlinenumber = LineNumber;
59253037Seric 	register EVENT *ev = NULL;
59358082Seric 	int nincludes;
59458247Seric 	int ret;
59563581Seric 	ADDRESS *ca;
59663581Seric 	uid_t uid;
59753037Seric 	char buf[MAXLINE];
5984174Seric 
59957186Seric 	if (tTd(27, 2))
60057186Seric 		printf("include(%s)\n", fname);
60163902Seric 	if (tTd(27, 4))
60263902Seric 		printf("   ruid=%d euid=%d\n", getuid(), geteuid());
60363581Seric 	if (tTd(27, 14))
60463581Seric 	{
60563581Seric 		printf("ctladdr ");
60663581Seric 		printaddr(ctladdr, FALSE);
60763581Seric 	}
60857186Seric 
60953037Seric 	/*
61053037Seric 	**  If home directory is remote mounted but server is down,
61153037Seric 	**  this can hang or give errors; use a timeout to avoid this
61253037Seric 	*/
61353037Seric 
61463581Seric 	ca = getctladdr(ctladdr);
61563581Seric 	if (ca == NULL)
61663581Seric 		uid = 0;
61763581Seric 	else
61863581Seric 		uid = ca->q_uid;
61963581Seric 
62053037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
62153037Seric 	{
62263853Seric 		ctladdr->q_flags |= QQUEUEUP;
62353037Seric 		errno = 0;
62453037Seric 		usrerr("451 open timeout on %s", fname);
625*63993Seric 
626*63993Seric 		/* return pseudo-error code */
627*63993Seric 		return EOPENTIMEOUT;
62853037Seric 	}
62953037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
63053037Seric 
63163581Seric 	/* the input file must be marked safe */
63263753Seric 	if ((ret = safefile(fname, uid, forwarding, S_IREAD)) != 0)
63353037Seric 	{
63453037Seric 		/* don't use this .forward file */
63553037Seric 		clrevent(ev);
63657186Seric 		if (tTd(27, 4))
63758247Seric 			printf("include: not safe (uid=%d): %s\n",
63863581Seric 				uid, errstring(ret));
63958247Seric 		return ret;
64053037Seric 	}
64153037Seric 
6424174Seric 	fp = fopen(fname, "r");
6434174Seric 	if (fp == NULL)
6444174Seric 	{
64557136Seric 		int ret = errno;
64657136Seric 
64758061Seric 		clrevent(ev);
64863902Seric 		if (tTd(27, 4))
64963902Seric 			printf("include: open: %s\n", errstring(ret));
65057136Seric 		return ret;
6514174Seric 	}
65253037Seric 
65363581Seric 	if (ca == NULL)
6544406Seric 	{
6554406Seric 		struct stat st;
6564174Seric 
6574406Seric 		if (fstat(fileno(fp), &st) < 0)
65858061Seric 		{
65958061Seric 			int ret = errno;
66058061Seric 
66158061Seric 			clrevent(ev);
6624406Seric 			syserr("Cannot fstat %s!", fname);
66358061Seric 			return ret;
66458061Seric 		}
6654406Seric 		ctladdr->q_uid = st.st_uid;
6664406Seric 		ctladdr->q_gid = st.st_gid;
6674406Seric 		ctladdr->q_flags |= QGOODUID;
6684406Seric 	}
6694406Seric 
67053037Seric 	clrevent(ev);
67153037Seric 
67258092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
67358092Seric 	{
67458092Seric 		/* don't do any more now */
67558868Seric 		ctladdr->q_flags |= QVERIFIED;
67658884Seric 		e->e_nrcpts++;
67758680Seric 		xfclose(fp, "include", fname);
67858092Seric 		return 0;
67958092Seric 	}
68058092Seric 
6814174Seric 	/* read the file -- each line is a comma-separated list. */
6829379Seric 	FileName = fname;
6839379Seric 	LineNumber = 0;
68458082Seric 	ctladdr->q_flags &= ~QSELFREF;
68558082Seric 	nincludes = 0;
6864174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6874174Seric 	{
68856795Seric 		register char *p = strchr(buf, '\n');
6894174Seric 
69040963Sbostic 		LineNumber++;
6914174Seric 		if (p != NULL)
6924174Seric 			*p = '\0';
69357186Seric 		if (buf[0] == '#' || buf[0] == '\0')
69457139Seric 			continue;
69558008Seric 		e->e_to = NULL;
69658151Seric 		message("%s to %s",
69753037Seric 			forwarding ? "forwarding" : "sending", buf);
69857977Seric #ifdef LOG
69958020Seric 		if (forwarding && LogLevel > 9)
70057977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
70157977Seric 				e->e_id, oldto, buf);
70257977Seric #endif
70357977Seric 
7044176Seric 		AliasLevel++;
70558082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
7064176Seric 		AliasLevel--;
7074174Seric 	}
70863902Seric 
70963902Seric 	if (ferror(fp) && tTd(27, 3))
71063902Seric 		printf("include: read error: %s\n", errstring(errno));
71158082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
71258065Seric 	{
71358065Seric 		if (tTd(27, 5))
71458065Seric 		{
71558065Seric 			printf("include: QDONTSEND ");
71658065Seric 			printaddr(ctladdr, FALSE);
71758065Seric 		}
71858065Seric 		ctladdr->q_flags |= QDONTSEND;
71958065Seric 	}
7204174Seric 
72158680Seric 	(void) xfclose(fp, "include", fname);
7229379Seric 	FileName = oldfilename;
7239379Seric 	LineNumber = oldlinenumber;
72463847Seric 	e->e_to = oldto;
72557136Seric 	return 0;
7264174Seric }
72753037Seric 
72853037Seric static
72953037Seric includetimeout()
73053037Seric {
73153037Seric 	longjmp(CtxIncludeTimeout, 1);
73253037Seric }
7334324Seric /*
7344324Seric **  SENDTOARGV -- send to an argument vector.
7354324Seric **
7364324Seric **	Parameters:
7374324Seric **		argv -- argument vector to send to.
73858247Seric **		e -- the current envelope.
7394324Seric **
7404324Seric **	Returns:
7414324Seric **		none.
7424324Seric **
7434324Seric **	Side Effects:
7444324Seric **		puts all addresses on the argument vector onto the
7454324Seric **			send queue.
7464324Seric */
7474324Seric 
74855012Seric sendtoargv(argv, e)
7494324Seric 	register char **argv;
75055012Seric 	register ENVELOPE *e;
7514324Seric {
7524324Seric 	register char *p;
7534324Seric 
7544324Seric 	while ((p = *argv++) != NULL)
7554324Seric 	{
75658082Seric 		(void) sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7574324Seric 	}
7584324Seric }
7594399Seric /*
7604399Seric **  GETCTLADDR -- get controlling address from an address header.
7614399Seric **
7624399Seric **	If none, get one corresponding to the effective userid.
7634399Seric **
7644399Seric **	Parameters:
7654399Seric **		a -- the address to find the controller of.
7664399Seric **
7674399Seric **	Returns:
7684399Seric **		the controlling address.
7694399Seric **
7704399Seric **	Side Effects:
7714399Seric **		none.
7724399Seric */
7734399Seric 
7744399Seric ADDRESS *
7754399Seric getctladdr(a)
7764399Seric 	register ADDRESS *a;
7774399Seric {
7784404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7794399Seric 		a = a->q_alias;
7804399Seric 	return (a);
7814399Seric }
782