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*63938Seric static char sccsid[] = "@(#)recipient.c	8.8 (Berkeley) 07/19/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 			{
262*63938Seric 				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:
40063847Seric 	if (a->q_alias == NULL && a != &e->e_from)
40163847Seric 	{
40263847Seric 		q = a->q_next;
40363847Seric 		while (q != NULL && bitset(QDONTSEND|QBADADDR, q->q_flags))
40463847Seric 			q = q->q_next;
405*63938Seric 		if (bitset(QDONTSEND, a->q_flags) && q == NULL)
40663847Seric 		{
40763847Seric 			a->q_flags |= QBADADDR;
40863847Seric 			usrerr("554 aliasing/forwarding loop broken");
40963847Seric 		}
41063847Seric 	}
41112613Seric 	return (a);
4124174Seric }
4134174Seric /*
4144373Seric **  FINDUSER -- find the password entry for a user.
4154373Seric **
4164373Seric **	This looks a lot like getpwnam, except that it may want to
4174373Seric **	do some fancier pattern matching in /etc/passwd.
4184373Seric **
4199379Seric **	This routine contains most of the time of many sendmail runs.
4209379Seric **	It deserves to be optimized.
4219379Seric **
4224373Seric **	Parameters:
4234373Seric **		name -- the name to match against.
42455354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
42555354Seric **			was found using the fuzzy matching algorithm;
42655354Seric **			set to FALSE otherwise.
4274373Seric **
4284373Seric **	Returns:
4294373Seric **		A pointer to a pw struct.
4304373Seric **		NULL if name is unknown or ambiguous.
4314373Seric **
4324373Seric **	Side Effects:
4334407Seric **		may modify name.
4344373Seric */
4354373Seric 
4364373Seric struct passwd *
43755354Seric finduser(name, fuzzyp)
4384373Seric 	char *name;
43955354Seric 	bool *fuzzyp;
4404373Seric {
4414376Seric 	register struct passwd *pw;
4424407Seric 	register char *p;
44315325Seric 	extern struct passwd *getpwent();
44415325Seric 	extern struct passwd *getpwnam();
4454373Seric 
44655354Seric 	if (tTd(29, 4))
44755354Seric 		printf("finduser(%s): ", name);
44855354Seric 
44955354Seric 	*fuzzyp = FALSE;
4504407Seric 
45125777Seric 	/* look up this login name using fast path */
45212634Seric 	if ((pw = getpwnam(name)) != NULL)
45355354Seric 	{
45455354Seric 		if (tTd(29, 4))
45555354Seric 			printf("found (non-fuzzy)\n");
45612634Seric 		return (pw);
45755354Seric 	}
45812634Seric 
45953735Seric #ifdef MATCHGECOS
46053735Seric 	/* see if fuzzy matching allowed */
46153735Seric 	if (!MatchGecos)
46255354Seric 	{
46355354Seric 		if (tTd(29, 4))
46455354Seric 			printf("not found (fuzzy disabled)\n");
46553735Seric 		return NULL;
46655354Seric 	}
46753735Seric 
46812634Seric 	/* search for a matching full name instead */
46925777Seric 	for (p = name; *p != '\0'; p++)
47025777Seric 	{
47125777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
47225777Seric 			*p = ' ';
47325777Seric 	}
47423107Seric 	(void) setpwent();
4754376Seric 	while ((pw = getpwent()) != NULL)
4764376Seric 	{
4774998Seric 		char buf[MAXNAME];
4784376Seric 
4794998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
48056795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
4814381Seric 		{
48255354Seric 			if (tTd(29, 4))
48355354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
48458151Seric 			message("sending to login name %s", pw->pw_name);
48555354Seric 			*fuzzyp = TRUE;
4864376Seric 			return (pw);
4874377Seric 		}
4884376Seric 	}
48955354Seric 	if (tTd(29, 4))
49055354Seric 		printf("no fuzzy match found\n");
49159015Seric #else
49259015Seric 	if (tTd(29, 4))
49359015Seric 		printf("not found (fuzzy disabled)\n");
49459015Seric #endif
4954376Seric 	return (NULL);
4964373Seric }
4974373Seric /*
4984329Seric **  WRITABLE -- predicate returning if the file is writable.
4994329Seric **
5004329Seric **	This routine must duplicate the algorithm in sys/fio.c.
5014329Seric **	Unfortunately, we cannot use the access call since we
5024329Seric **	won't necessarily be the real uid when we try to
5034329Seric **	actually open the file.
5044329Seric **
5054329Seric **	Notice that ANY file with ANY execute bit is automatically
5064329Seric **	not writable.  This is also enforced by mailfile.
5074329Seric **
5084329Seric **	Parameters:
5094329Seric **		s -- pointer to a stat struct for the file.
5104329Seric **
5114329Seric **	Returns:
5124329Seric **		TRUE -- if we will be able to write this file.
5134329Seric **		FALSE -- if we cannot write this file.
5144329Seric **
5154329Seric **	Side Effects:
5164329Seric **		none.
5174329Seric */
5184329Seric 
5194329Seric bool
5204329Seric writable(s)
5214329Seric 	register struct stat *s;
5224329Seric {
52355372Seric 	uid_t euid;
52455372Seric 	gid_t egid;
5254329Seric 	int bits;
5264329Seric 
5274329Seric 	if (bitset(0111, s->st_mode))
5284329Seric 		return (FALSE);
52963787Seric 	euid = RealUid;
53063787Seric 	egid = RealGid;
5314329Seric 	if (geteuid() == 0)
5324329Seric 	{
5334329Seric 		if (bitset(S_ISUID, s->st_mode))
5344329Seric 			euid = s->st_uid;
5354329Seric 		if (bitset(S_ISGID, s->st_mode))
5364329Seric 			egid = s->st_gid;
5374329Seric 	}
5384329Seric 
5394329Seric 	if (euid == 0)
5404329Seric 		return (TRUE);
5414329Seric 	bits = S_IWRITE;
5424329Seric 	if (euid != s->st_uid)
5434329Seric 	{
5444329Seric 		bits >>= 3;
5454329Seric 		if (egid != s->st_gid)
5464329Seric 			bits >>= 3;
5474329Seric 	}
5484329Seric 	return ((s->st_mode & bits) != 0);
5494329Seric }
5504329Seric /*
5514174Seric **  INCLUDE -- handle :include: specification.
5524174Seric **
5534174Seric **	Parameters:
5544174Seric **		fname -- filename to include.
55553037Seric **		forwarding -- if TRUE, we are reading a .forward file.
55653037Seric **			if FALSE, it's a :include: file.
5574399Seric **		ctladdr -- address template to use to fill in these
5584399Seric **			addresses -- effective user/group id are
5594399Seric **			the important things.
5605006Seric **		sendq -- a pointer to the head of the send queue
5615006Seric **			to put these addresses in.
5624174Seric **
5634174Seric **	Returns:
56457136Seric **		open error status
5654174Seric **
5664174Seric **	Side Effects:
5674174Seric **		reads the :include: file and sends to everyone
5684174Seric **		listed in that file.
5694174Seric */
5704174Seric 
57153037Seric static jmp_buf	CtxIncludeTimeout;
57263937Seric static int	includetimeout();
57353037Seric 
57457136Seric int
57555012Seric include(fname, forwarding, ctladdr, sendq, e)
5764174Seric 	char *fname;
57753037Seric 	bool forwarding;
5784399Seric 	ADDRESS *ctladdr;
5795006Seric 	ADDRESS **sendq;
58055012Seric 	ENVELOPE *e;
5814174Seric {
5824174Seric 	register FILE *fp;
58355012Seric 	char *oldto = e->e_to;
5849379Seric 	char *oldfilename = FileName;
5859379Seric 	int oldlinenumber = LineNumber;
58653037Seric 	register EVENT *ev = NULL;
58758082Seric 	int nincludes;
58858247Seric 	int ret;
58963581Seric 	ADDRESS *ca;
59063581Seric 	uid_t uid;
59153037Seric 	char buf[MAXLINE];
5924174Seric 
59357186Seric 	if (tTd(27, 2))
59457186Seric 		printf("include(%s)\n", fname);
59563902Seric 	if (tTd(27, 4))
59663902Seric 		printf("   ruid=%d euid=%d\n", getuid(), geteuid());
59763581Seric 	if (tTd(27, 14))
59863581Seric 	{
59963581Seric 		printf("ctladdr ");
60063581Seric 		printaddr(ctladdr, FALSE);
60163581Seric 	}
60257186Seric 
60353037Seric 	/*
60453037Seric 	**  If home directory is remote mounted but server is down,
60553037Seric 	**  this can hang or give errors; use a timeout to avoid this
60653037Seric 	*/
60753037Seric 
60863581Seric 	ca = getctladdr(ctladdr);
60963581Seric 	if (ca == NULL)
61063581Seric 		uid = 0;
61163581Seric 	else
61263581Seric 		uid = ca->q_uid;
61363581Seric 
61453037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
61553037Seric 	{
61663853Seric 		ctladdr->q_flags |= QQUEUEUP;
61753037Seric 		errno = 0;
61853037Seric 		usrerr("451 open timeout on %s", fname);
61957136Seric 		return ETIMEDOUT;
62053037Seric 	}
62153037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
62253037Seric 
62363581Seric 	/* the input file must be marked safe */
62463753Seric 	if ((ret = safefile(fname, uid, forwarding, S_IREAD)) != 0)
62553037Seric 	{
62653037Seric 		/* don't use this .forward file */
62753037Seric 		clrevent(ev);
62857186Seric 		if (tTd(27, 4))
62958247Seric 			printf("include: not safe (uid=%d): %s\n",
63063581Seric 				uid, errstring(ret));
63158247Seric 		return ret;
63253037Seric 	}
63353037Seric 
6344174Seric 	fp = fopen(fname, "r");
6354174Seric 	if (fp == NULL)
6364174Seric 	{
63757136Seric 		int ret = errno;
63857136Seric 
63958061Seric 		clrevent(ev);
64063902Seric 		if (tTd(27, 4))
64163902Seric 			printf("include: open: %s\n", errstring(ret));
64257136Seric 		return ret;
6434174Seric 	}
64453037Seric 
64563581Seric 	if (ca == NULL)
6464406Seric 	{
6474406Seric 		struct stat st;
6484174Seric 
6494406Seric 		if (fstat(fileno(fp), &st) < 0)
65058061Seric 		{
65158061Seric 			int ret = errno;
65258061Seric 
65358061Seric 			clrevent(ev);
6544406Seric 			syserr("Cannot fstat %s!", fname);
65558061Seric 			return ret;
65658061Seric 		}
6574406Seric 		ctladdr->q_uid = st.st_uid;
6584406Seric 		ctladdr->q_gid = st.st_gid;
6594406Seric 		ctladdr->q_flags |= QGOODUID;
6604406Seric 	}
6614406Seric 
66253037Seric 	clrevent(ev);
66353037Seric 
66458092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
66558092Seric 	{
66658092Seric 		/* don't do any more now */
66758868Seric 		ctladdr->q_flags |= QVERIFIED;
66858884Seric 		e->e_nrcpts++;
66958680Seric 		xfclose(fp, "include", fname);
67058092Seric 		return 0;
67158092Seric 	}
67258092Seric 
6734174Seric 	/* read the file -- each line is a comma-separated list. */
6749379Seric 	FileName = fname;
6759379Seric 	LineNumber = 0;
67658082Seric 	ctladdr->q_flags &= ~QSELFREF;
67758082Seric 	nincludes = 0;
6784174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6794174Seric 	{
68056795Seric 		register char *p = strchr(buf, '\n');
6814174Seric 
68240963Sbostic 		LineNumber++;
6834174Seric 		if (p != NULL)
6844174Seric 			*p = '\0';
68557186Seric 		if (buf[0] == '#' || buf[0] == '\0')
68657139Seric 			continue;
68758008Seric 		e->e_to = NULL;
68858151Seric 		message("%s to %s",
68953037Seric 			forwarding ? "forwarding" : "sending", buf);
69057977Seric #ifdef LOG
69158020Seric 		if (forwarding && LogLevel > 9)
69257977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
69357977Seric 				e->e_id, oldto, buf);
69457977Seric #endif
69557977Seric 
6964176Seric 		AliasLevel++;
69758082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
6984176Seric 		AliasLevel--;
6994174Seric 	}
70063902Seric 
70163902Seric 	if (ferror(fp) && tTd(27, 3))
70263902Seric 		printf("include: read error: %s\n", errstring(errno));
70358082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
70458065Seric 	{
70558065Seric 		if (tTd(27, 5))
70658065Seric 		{
70758065Seric 			printf("include: QDONTSEND ");
70858065Seric 			printaddr(ctladdr, FALSE);
70958065Seric 		}
71058065Seric 		ctladdr->q_flags |= QDONTSEND;
71158065Seric 	}
7124174Seric 
71358680Seric 	(void) xfclose(fp, "include", fname);
7149379Seric 	FileName = oldfilename;
7159379Seric 	LineNumber = oldlinenumber;
71663847Seric 	e->e_to = oldto;
71757136Seric 	return 0;
7184174Seric }
71953037Seric 
72053037Seric static
72153037Seric includetimeout()
72253037Seric {
72353037Seric 	longjmp(CtxIncludeTimeout, 1);
72453037Seric }
7254324Seric /*
7264324Seric **  SENDTOARGV -- send to an argument vector.
7274324Seric **
7284324Seric **	Parameters:
7294324Seric **		argv -- argument vector to send to.
73058247Seric **		e -- the current envelope.
7314324Seric **
7324324Seric **	Returns:
7334324Seric **		none.
7344324Seric **
7354324Seric **	Side Effects:
7364324Seric **		puts all addresses on the argument vector onto the
7374324Seric **			send queue.
7384324Seric */
7394324Seric 
74055012Seric sendtoargv(argv, e)
7414324Seric 	register char **argv;
74255012Seric 	register ENVELOPE *e;
7434324Seric {
7444324Seric 	register char *p;
7454324Seric 
7464324Seric 	while ((p = *argv++) != NULL)
7474324Seric 	{
74858082Seric 		(void) sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7494324Seric 	}
7504324Seric }
7514399Seric /*
7524399Seric **  GETCTLADDR -- get controlling address from an address header.
7534399Seric **
7544399Seric **	If none, get one corresponding to the effective userid.
7554399Seric **
7564399Seric **	Parameters:
7574399Seric **		a -- the address to find the controller of.
7584399Seric **
7594399Seric **	Returns:
7604399Seric **		the controlling address.
7614399Seric **
7624399Seric **	Side Effects:
7634399Seric **		none.
7644399Seric */
7654399Seric 
7664399Seric ADDRESS *
7674399Seric getctladdr(a)
7684399Seric 	register ADDRESS *a;
7694399Seric {
7704404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7714399Seric 		a = a->q_alias;
7724399Seric 	return (a);
7734399Seric }
774