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*65022Seric static char sccsid[] = "@(#)recipient.c	8.26 (Berkeley) 12/04/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 
5464131Seric 	if (list == NULL)
5564131Seric 	{
5664131Seric 		syserr("sendtolist: null list");
5764131Seric 		return 0;
5864131Seric 	}
5964131Seric 
607676Seric 	if (tTd(25, 1))
614444Seric 	{
624444Seric 		printf("sendto: %s\n   ctladdr=", list);
634444Seric 		printaddr(ctladdr, FALSE);
644444Seric 	}
654324Seric 
668223Seric 	/* heuristic to determine old versus new style addresses */
678230Seric 	if (ctladdr == NULL &&
6856795Seric 	    (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
6956795Seric 	     strchr(list, '<') != NULL || strchr(list, '(') != NULL))
7055012Seric 		e->e_flags &= ~EF_OLDSTYLE;
7111446Seric 	delimiter = ' ';
7255012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
7311446Seric 		delimiter = ',';
748223Seric 
754423Seric 	firstone = TRUE;
764324Seric 	al = NULL;
7758082Seric 	naddrs = 0;
788223Seric 
798081Seric 	for (p = list; *p != '\0'; )
804174Seric 	{
8158333Seric 		auto char *delimptr;
828081Seric 		register ADDRESS *a;
834319Seric 
848081Seric 		/* parse the address */
8558050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
864174Seric 			p++;
8764284Seric 		a = parseaddr(p, NULLADDR, RF_COPYALL, delimiter, &delimptr, e);
8858333Seric 		p = delimptr;
899297Seric 		if (a == NULL)
904174Seric 			continue;
914324Seric 		a->q_next = al;
924399Seric 		a->q_alias = ctladdr;
934444Seric 
944444Seric 		/* see if this should be marked as a primary address */
954423Seric 		if (ctladdr == NULL ||
968081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
974423Seric 			a->q_flags |= QPRIMARY;
984444Seric 
999379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
10058061Seric 			ctladdr->q_flags |= QSELFREF;
10157731Seric 		al = a;
1024423Seric 		firstone = FALSE;
1034324Seric 	}
1044324Seric 
1054324Seric 	/* arrange to send to everyone on the local send list */
1064324Seric 	while (al != NULL)
1074324Seric 	{
1084324Seric 		register ADDRESS *a = al;
1094324Seric 
1104324Seric 		al = a->q_next;
11155012Seric 		a = recipient(a, sendq, e);
1124993Seric 
1134998Seric 		/* arrange to inherit full name */
1144998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1154998Seric 			a->q_fullname = ctladdr->q_fullname;
11658082Seric 		naddrs++;
1174174Seric 	}
1184324Seric 
11963847Seric 	e->e_to = oldto;
12058082Seric 	return (naddrs);
1214174Seric }
1224174Seric /*
1234174Seric **  RECIPIENT -- Designate a message recipient
1244174Seric **
1254174Seric **	Saves the named person for future mailing.
1264174Seric **
1274174Seric **	Parameters:
1284174Seric **		a -- the (preparsed) address header for the recipient.
1295006Seric **		sendq -- a pointer to the head of a queue to put the
1305006Seric **			recipient in.  Duplicate supression is done
1315006Seric **			in this queue.
13257731Seric **		e -- the current envelope.
1334174Seric **
1344174Seric **	Returns:
13512613Seric **		The actual address in the queue.  This will be "a" if
13612613Seric **		the address is not a duplicate, else the original address.
1374174Seric **
1384174Seric **	Side Effects:
1394174Seric **		none.
1404174Seric */
1414174Seric 
14212613Seric ADDRESS *
14355012Seric recipient(a, sendq, e)
1444174Seric 	register ADDRESS *a;
1455006Seric 	register ADDRESS **sendq;
14655012Seric 	register ENVELOPE *e;
1474174Seric {
1484174Seric 	register ADDRESS *q;
1494319Seric 	ADDRESS **pq;
1504174Seric 	register struct mailer *m;
1519210Seric 	register char *p;
1529210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
15353735Seric 	int findusercount = 0;
1549210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
15558247Seric 	extern int safefile();
1564174Seric 
15755012Seric 	e->e_to = a->q_paddr;
1584600Seric 	m = a->q_mailer;
1594174Seric 	errno = 0;
1607676Seric 	if (tTd(26, 1))
1614444Seric 	{
1624444Seric 		printf("\nrecipient: ");
1634444Seric 		printaddr(a, FALSE);
1644444Seric 	}
1654174Seric 
16664146Seric 	/* if this is primary, add it to the original recipient list */
16764146Seric 	if (a->q_alias == NULL)
16864146Seric 	{
16964146Seric 		if (e->e_origrcpt == NULL)
17064146Seric 			e->e_origrcpt = a->q_paddr;
17164146Seric 		else if (e->e_origrcpt != a->q_paddr)
17264146Seric 			e->e_origrcpt = "";
17364146Seric 	}
17464146Seric 
1754174Seric 	/* break aliasing loops */
1764174Seric 	if (AliasLevel > MAXRCRSN)
1774174Seric 	{
17858151Seric 		usrerr("554 aliasing/forwarding loop broken");
17912613Seric 		return (a);
1804174Seric 	}
1814174Seric 
1824174Seric 	/*
1834627Seric 	**  Finish setting up address structure.
1844174Seric 	*/
1854174Seric 
18616160Seric 	/* set the queue timeout */
18758737Seric 	a->q_timeout = TimeOuts.to_q_return;
1884627Seric 
18916160Seric 	/* get unquoted user for file, program or user.name check */
1909210Seric 	(void) strcpy(buf, a->q_user);
1919210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1929210Seric 	{
19354993Seric 		if (*p == '\\')
1949210Seric 			quoted = TRUE;
1959210Seric 	}
19654983Seric 	stripquotes(buf);
1979210Seric 
19857402Seric 	/* check for direct mailing to restricted mailers */
19964761Seric 	if (a->q_alias == NULL && m == ProgMailer)
2004174Seric 	{
20158680Seric 		a->q_flags |= QBADADDR;
20263847Seric 		usrerr("550 Cannot mail directly to programs");
2034174Seric 	}
2044174Seric 
2054174Seric 	/*
2064419Seric 	**  Look up this person in the recipient list.
2074419Seric 	**	If they are there already, return, otherwise continue.
2084419Seric 	**	If the list is empty, just add it.  Notice the cute
2094419Seric 	**	hack to make from addresses suppress things correctly:
2104419Seric 	**	the QDONTSEND bit will be set in the send list.
2114419Seric 	**	[Please note: the emphasis is on "hack."]
2124174Seric 	*/
2134174Seric 
2145006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2154174Seric 	{
21658294Seric 		if (sameaddr(q, a))
2174174Seric 		{
2187676Seric 			if (tTd(26, 1))
2194444Seric 			{
2204444Seric 				printf("%s in sendq: ", a->q_paddr);
2214444Seric 				printaddr(q, FALSE);
2224444Seric 			}
2234423Seric 			if (!bitset(QPRIMARY, q->q_flags))
22458065Seric 			{
22558065Seric 				if (!bitset(QDONTSEND, a->q_flags))
22658151Seric 					message("duplicate suppressed");
2274423Seric 				q->q_flags |= a->q_flags;
22858065Seric 			}
22963847Seric 			a = q;
23063847Seric 			goto testselfdestruct;
2314174Seric 		}
2324319Seric 	}
2334174Seric 
2344319Seric 	/* add address on list */
23558884Seric 	*pq = a;
23658884Seric 	a->q_next = NULL;
2374174Seric 
2384174Seric 	/*
23957402Seric 	**  Alias the name and handle special mailer types.
2404174Seric 	*/
2414174Seric 
24253735Seric   trylocaluser:
24355354Seric 	if (tTd(29, 7))
24455354Seric 		printf("at trylocaluser %s\n", a->q_user);
24555354Seric 
24658680Seric 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
24763847Seric 		goto testselfdestruct;
24857402Seric 
24957402Seric 	if (m == InclMailer)
2504174Seric 	{
25157402Seric 		a->q_flags |= QDONTSEND;
25264761Seric 		if (a->q_alias == NULL)
2534174Seric 		{
25458680Seric 			a->q_flags |= QBADADDR;
25558151Seric 			usrerr("550 Cannot mail directly to :include:s");
2564174Seric 		}
2574174Seric 		else
25850556Seric 		{
25959563Seric 			int ret;
26058247Seric 
26158151Seric 			message("including file %s", a->q_user);
26259563Seric 			ret = include(a->q_user, FALSE, a, sendq, e);
26359563Seric 			if (transienterror(ret))
26459563Seric 			{
26559563Seric #ifdef LOG
26659563Seric 				if (LogLevel > 2)
26759615Seric 					syslog(LOG_ERR, "%s: include %s: transient error: %e",
26859623Seric 						e->e_id, a->q_user, errstring(ret));
26959563Seric #endif
27063853Seric 				a->q_flags |= QQUEUEUP;
27159563Seric 				usrerr("451 Cannot open %s: %s",
27259563Seric 					a->q_user, errstring(ret));
27359563Seric 			}
27459563Seric 			else if (ret != 0)
27559563Seric 			{
27663938Seric 				a->q_flags |= QBADADDR;
27759563Seric 				usrerr("550 Cannot open %s: %s",
27859563Seric 					a->q_user, errstring(ret));
27959563Seric 			}
28050556Seric 		}
2814174Seric 	}
28257642Seric 	else if (m == FileMailer)
2834174Seric 	{
2844329Seric 		extern bool writable();
2854174Seric 
28651317Seric 		/* check if writable or creatable */
28764761Seric 		if (a->q_alias == NULL)
2884174Seric 		{
28958680Seric 			a->q_flags |= QBADADDR;
29058151Seric 			usrerr("550 Cannot mail directly to files");
2914174Seric 		}
29264944Seric 		else if (!writable(buf))
29351317Seric 		{
29458680Seric 			a->q_flags |= QBADADDR;
29564771Seric 			giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e);
29651317Seric 		}
29751317Seric 	}
29851317Seric 
29957402Seric 	if (m != LocalMailer)
30057642Seric 	{
30157642Seric 		if (!bitset(QDONTSEND, a->q_flags))
30257642Seric 			e->e_nrcpts++;
30363847Seric 		goto testselfdestruct;
30457642Seric 	}
30557402Seric 
30657402Seric 	/* try aliasing */
30757402Seric 	alias(a, sendq, e);
30857402Seric 
30957402Seric # ifdef USERDB
31057402Seric 	/* if not aliased, look it up in the user database */
31158918Seric 	if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags))
31257402Seric 	{
31357402Seric 		extern int udbexpand();
31457402Seric 
31557402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
31657402Seric 		{
31763853Seric 			a->q_flags |= QQUEUEUP;
31857402Seric 			if (e->e_message == NULL)
31957402Seric 				e->e_message = newstr("Deferred: user database error");
32057402Seric # ifdef LOG
32158020Seric 			if (LogLevel > 8)
32259623Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand: %s",
32359623Seric 					e->e_id, errstring(errno));
32457402Seric # endif
32559615Seric 			message("queued (user database error): %s",
32659615Seric 				errstring(errno));
32757642Seric 			e->e_nrcpts++;
32863847Seric 			goto testselfdestruct;
32957402Seric 		}
33057402Seric 	}
33157402Seric # endif
33257402Seric 
33357402Seric 	/* if it was an alias or a UDB expansion, just return now */
33458247Seric 	if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags))
33563847Seric 		goto testselfdestruct;
33657402Seric 
33751317Seric 	/*
33851317Seric 	**  If we have a level two config file, then pass the name through
33951317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
34051317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
34151317Seric 	**  after local aliasing has been done.
34251317Seric 	*/
34351317Seric 
34451317Seric 	if (tTd(29, 5))
34551317Seric 	{
34651317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
34751317Seric 			ConfigLevel, RewriteRules[5]);
34851317Seric 		printaddr(a, FALSE);
34951317Seric 	}
35051317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
35151317Seric 	    RewriteRules[5] != NULL)
35251317Seric 	{
35355012Seric 		maplocaluser(a, sendq, e);
35451317Seric 	}
35551317Seric 
35651317Seric 	/*
35751317Seric 	**  If it didn't get rewritten to another mailer, go ahead
35851317Seric 	**  and deliver it.
35951317Seric 	*/
36051317Seric 
36158247Seric 	if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags))
36251317Seric 	{
36355354Seric 		auto bool fuzzy;
36451317Seric 		register struct passwd *pw;
36551317Seric 		extern struct passwd *finduser();
36651317Seric 
36751317Seric 		/* warning -- finduser may trash buf */
36855354Seric 		pw = finduser(buf, &fuzzy);
36951317Seric 		if (pw == NULL)
37051317Seric 		{
37158680Seric 			a->q_flags |= QBADADDR;
37264771Seric 			giveresponse(EX_NOUSER, m, NULL, a->q_alias, e);
37351317Seric 		}
3744174Seric 		else
3754174Seric 		{
37651317Seric 			char nbuf[MAXNAME];
3774373Seric 
37855354Seric 			if (fuzzy)
3794174Seric 			{
38053735Seric 				/* name was a fuzzy match */
38151317Seric 				a->q_user = newstr(pw->pw_name);
38253735Seric 				if (findusercount++ > 3)
38353735Seric 				{
38458680Seric 					a->q_flags |= QBADADDR;
38558151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
38653735Seric 						pw->pw_name);
38753735Seric 					return (a);
38853735Seric 				}
38953735Seric 
39053735Seric 				/* see if it aliases */
39151317Seric 				(void) strcpy(buf, pw->pw_name);
39253735Seric 				goto trylocaluser;
3934174Seric 			}
39451317Seric 			a->q_home = newstr(pw->pw_dir);
39551317Seric 			a->q_uid = pw->pw_uid;
39651317Seric 			a->q_gid = pw->pw_gid;
39759083Seric 			a->q_ruser = newstr(pw->pw_name);
39851317Seric 			a->q_flags |= QGOODUID;
39951317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
40051317Seric 			if (nbuf[0] != '\0')
40151317Seric 				a->q_fullname = newstr(nbuf);
40251317Seric 			if (!quoted)
40355012Seric 				forward(a, sendq, e);
4044174Seric 		}
4054174Seric 	}
40657642Seric 	if (!bitset(QDONTSEND, a->q_flags))
40757642Seric 		e->e_nrcpts++;
40863847Seric 
40963847Seric   testselfdestruct:
41063978Seric 	if (tTd(26, 8))
41163847Seric 	{
41263978Seric 		printf("testselfdestruct: ");
41363978Seric 		printaddr(a, TRUE);
41463978Seric 	}
41563978Seric 	if (a->q_alias == NULL && a != &e->e_from &&
41663978Seric 	    bitset(QDONTSEND, a->q_flags))
41763978Seric 	{
41863978Seric 		q = *sendq;
41963965Seric 		while (q != NULL && bitset(QDONTSEND, q->q_flags))
42063847Seric 			q = q->q_next;
42163978Seric 		if (q == NULL)
42263847Seric 		{
42363847Seric 			a->q_flags |= QBADADDR;
42463847Seric 			usrerr("554 aliasing/forwarding loop broken");
42563847Seric 		}
42663847Seric 	}
42712613Seric 	return (a);
4284174Seric }
4294174Seric /*
4304373Seric **  FINDUSER -- find the password entry for a user.
4314373Seric **
4324373Seric **	This looks a lot like getpwnam, except that it may want to
4334373Seric **	do some fancier pattern matching in /etc/passwd.
4344373Seric **
4359379Seric **	This routine contains most of the time of many sendmail runs.
4369379Seric **	It deserves to be optimized.
4379379Seric **
4384373Seric **	Parameters:
4394373Seric **		name -- the name to match against.
44055354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
44155354Seric **			was found using the fuzzy matching algorithm;
44255354Seric **			set to FALSE otherwise.
4434373Seric **
4444373Seric **	Returns:
4454373Seric **		A pointer to a pw struct.
4464373Seric **		NULL if name is unknown or ambiguous.
4474373Seric **
4484373Seric **	Side Effects:
4494407Seric **		may modify name.
4504373Seric */
4514373Seric 
4524373Seric struct passwd *
45355354Seric finduser(name, fuzzyp)
4544373Seric 	char *name;
45555354Seric 	bool *fuzzyp;
4564373Seric {
4574376Seric 	register struct passwd *pw;
4584407Seric 	register char *p;
45915325Seric 	extern struct passwd *getpwent();
46015325Seric 	extern struct passwd *getpwnam();
4614373Seric 
46255354Seric 	if (tTd(29, 4))
46355354Seric 		printf("finduser(%s): ", name);
46455354Seric 
46555354Seric 	*fuzzyp = FALSE;
4664407Seric 
46764673Seric 	/* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */
46864673Seric 	for (p = name; *p != '\0'; p++)
46964673Seric 		if (!isascii(*p) || !isdigit(*p))
47064673Seric 			break;
47164673Seric 	if (*p == '\0')
47264673Seric 	{
47364673Seric 		if (tTd(29, 4))
47464673Seric 			printf("failed (numeric input)\n");
47564673Seric 		return NULL;
47664673Seric 	}
47764673Seric 
47825777Seric 	/* look up this login name using fast path */
47912634Seric 	if ((pw = getpwnam(name)) != NULL)
48055354Seric 	{
48155354Seric 		if (tTd(29, 4))
48255354Seric 			printf("found (non-fuzzy)\n");
48312634Seric 		return (pw);
48455354Seric 	}
48512634Seric 
48653735Seric #ifdef MATCHGECOS
48753735Seric 	/* see if fuzzy matching allowed */
48853735Seric 	if (!MatchGecos)
48955354Seric 	{
49055354Seric 		if (tTd(29, 4))
49155354Seric 			printf("not found (fuzzy disabled)\n");
49253735Seric 		return NULL;
49355354Seric 	}
49453735Seric 
49512634Seric 	/* search for a matching full name instead */
49625777Seric 	for (p = name; *p != '\0'; p++)
49725777Seric 	{
49825777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
49925777Seric 			*p = ' ';
50025777Seric 	}
50123107Seric 	(void) setpwent();
5024376Seric 	while ((pw = getpwent()) != NULL)
5034376Seric 	{
5044998Seric 		char buf[MAXNAME];
5054376Seric 
5064998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
50756795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
5084381Seric 		{
50955354Seric 			if (tTd(29, 4))
51055354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
51158151Seric 			message("sending to login name %s", pw->pw_name);
51255354Seric 			*fuzzyp = TRUE;
5134376Seric 			return (pw);
5144377Seric 		}
5154376Seric 	}
51655354Seric 	if (tTd(29, 4))
51755354Seric 		printf("no fuzzy match found\n");
51859015Seric #else
51959015Seric 	if (tTd(29, 4))
52059015Seric 		printf("not found (fuzzy disabled)\n");
52159015Seric #endif
5224376Seric 	return (NULL);
5234373Seric }
5244373Seric /*
5254329Seric **  WRITABLE -- predicate returning if the file is writable.
5264329Seric **
5274329Seric **	This routine must duplicate the algorithm in sys/fio.c.
5284329Seric **	Unfortunately, we cannot use the access call since we
5294329Seric **	won't necessarily be the real uid when we try to
5304329Seric **	actually open the file.
5314329Seric **
5324329Seric **	Notice that ANY file with ANY execute bit is automatically
5334329Seric **	not writable.  This is also enforced by mailfile.
5344329Seric **
5354329Seric **	Parameters:
5364329Seric **		s -- pointer to a stat struct for the file.
5374329Seric **
5384329Seric **	Returns:
5394329Seric **		TRUE -- if we will be able to write this file.
5404329Seric **		FALSE -- if we cannot write this file.
5414329Seric **
5424329Seric **	Side Effects:
5434329Seric **		none.
5444329Seric */
5454329Seric 
5464329Seric bool
54764944Seric writable(filename)
54864819Seric 	char *filename;
5494329Seric {
55055372Seric 	uid_t euid;
55155372Seric 	gid_t egid;
5524329Seric 	int bits;
55364944Seric 	register char *p;
55464944Seric 	char *uname;
55564944Seric 	struct stat stb;
55664944Seric 	extern char RealUserName[];
5574329Seric 
55864819Seric 	if (tTd(29, 5))
55964944Seric 		printf("writable(%s)\n", filename);
56064944Seric 
56164944Seric #ifdef HASLSTAT
56264944Seric 	if (lstat(filename, &stb) < 0)
56364944Seric #else
56464944Seric 	if (stat(filename, &stb) < 0)
56564944Seric #endif
56664944Seric 	{
56764944Seric 		/* file does not exist -- see if directory is safe */
56864944Seric 		p = strrchr(filename, '/');
56964944Seric 		if (p == NULL)
57064944Seric 			return FALSE;
57164944Seric 		*p = '\0';
57264944Seric 		if (safefile(filename, RealUid, RealGid, RealUserName,
57364944Seric 			     SF_MUSTOWN, S_IWRITE|S_IEXEC) != 0)
57464944Seric 		{
57564944Seric 			*p = '/';
57664944Seric 			return FALSE;
57764944Seric 		}
57864944Seric 		*p = '/';
579*65022Seric 		return TRUE;
58064944Seric 	}
58164944Seric 
58264944Seric 	/*
58364944Seric 	**  File does exist -- check that it is writable.
58464944Seric 	*/
58564944Seric 
58664944Seric 	if (bitset(0111, stb.st_mode))
587*65022Seric 	{
588*65022Seric 		if (tTd(29, 5))
589*65022Seric 			printf("failed (mode %o: x bits)\n", stb.st_mode);
5904329Seric 		return (FALSE);
591*65022Seric 	}
59264944Seric 
59363787Seric 	euid = RealUid;
59464944Seric 	uname = RealUserName;
59564944Seric 	if (euid == 0)
59664944Seric 	{
59764944Seric 		euid = DefUid;
59864944Seric 		uname = DefUser;
59964944Seric 	}
60063787Seric 	egid = RealGid;
60164944Seric 	if (egid == 0)
60264944Seric 		egid = DefGid;
6034329Seric 	if (geteuid() == 0)
6044329Seric 	{
60564944Seric 		if (bitset(S_ISUID, stb.st_mode))
60664944Seric 		{
60764944Seric 			euid = stb.st_uid;
60864944Seric 			uname = NULL;
60964944Seric 		}
61064944Seric 		if (bitset(S_ISGID, stb.st_mode))
61164944Seric 			egid = stb.st_gid;
6124329Seric 	}
6134329Seric 
61464819Seric 	if (tTd(29, 5))
61564819Seric 		printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n",
61664944Seric 			euid, egid, stb.st_uid, stb.st_gid);
61764819Seric 
61865014Seric 	return safefile(filename, euid, egid, uname, SF_NOSLINK, S_IWRITE) == 0;
6194329Seric }
6204329Seric /*
6214174Seric **  INCLUDE -- handle :include: specification.
6224174Seric **
6234174Seric **	Parameters:
6244174Seric **		fname -- filename to include.
62553037Seric **		forwarding -- if TRUE, we are reading a .forward file.
62653037Seric **			if FALSE, it's a :include: file.
6274399Seric **		ctladdr -- address template to use to fill in these
6284399Seric **			addresses -- effective user/group id are
6294399Seric **			the important things.
6305006Seric **		sendq -- a pointer to the head of the send queue
6315006Seric **			to put these addresses in.
6324174Seric **
6334174Seric **	Returns:
63457136Seric **		open error status
6354174Seric **
6364174Seric **	Side Effects:
6374174Seric **		reads the :include: file and sends to everyone
6384174Seric **		listed in that file.
6394174Seric */
6404174Seric 
64153037Seric static jmp_buf	CtxIncludeTimeout;
64263937Seric static int	includetimeout();
64353037Seric 
64457136Seric int
64555012Seric include(fname, forwarding, ctladdr, sendq, e)
6464174Seric 	char *fname;
64753037Seric 	bool forwarding;
6484399Seric 	ADDRESS *ctladdr;
6495006Seric 	ADDRESS **sendq;
65055012Seric 	ENVELOPE *e;
6514174Seric {
65264570Seric 	register FILE *fp = NULL;
65355012Seric 	char *oldto = e->e_to;
6549379Seric 	char *oldfilename = FileName;
6559379Seric 	int oldlinenumber = LineNumber;
65653037Seric 	register EVENT *ev = NULL;
65758082Seric 	int nincludes;
65864325Seric 	register ADDRESS *ca;
65964325Seric 	uid_t saveduid, uid;
66064325Seric 	gid_t savedgid, gid;
66164083Seric 	char *uname;
66264325Seric 	int rval = 0;
66364944Seric 	int sfflags = forwarding ? SF_MUSTOWN : 0;
66453037Seric 	char buf[MAXLINE];
6654174Seric 
66657186Seric 	if (tTd(27, 2))
66757186Seric 		printf("include(%s)\n", fname);
66863902Seric 	if (tTd(27, 4))
66963902Seric 		printf("   ruid=%d euid=%d\n", getuid(), geteuid());
67063581Seric 	if (tTd(27, 14))
67163581Seric 	{
67263581Seric 		printf("ctladdr ");
67363581Seric 		printaddr(ctladdr, FALSE);
67463581Seric 	}
67557186Seric 
67664325Seric 	if (tTd(27, 9))
67764325Seric 		printf("include: old uid = %d/%d\n", getuid(), geteuid());
67853037Seric 
67963581Seric 	ca = getctladdr(ctladdr);
68063581Seric 	if (ca == NULL)
68164083Seric 	{
68264846Seric 		uid = DefUid;
68364846Seric 		gid = DefGid;
68464846Seric 		uname = DefUser;
68564325Seric 		saveduid = -1;
68664083Seric 	}
68763581Seric 	else
68864083Seric 	{
68963581Seric 		uid = ca->q_uid;
69064083Seric 		gid = ca->q_gid;
69164083Seric 		uname = ca->q_user;
69264325Seric #ifdef HASSETREUID
69364325Seric 		saveduid = geteuid();
69464325Seric 		savedgid = getegid();
69564325Seric 		if (saveduid == 0)
69664325Seric 		{
69764325Seric 			initgroups(uname, gid);
69864325Seric 			if (uid != 0)
69964325Seric 				(void) setreuid(0, uid);
70064325Seric 		}
70164325Seric #endif
70264083Seric 	}
70363581Seric 
70464325Seric 	if (tTd(27, 9))
70564325Seric 		printf("include: new uid = %d/%d\n", getuid(), geteuid());
70664325Seric 
70764325Seric 	/*
70864325Seric 	**  If home directory is remote mounted but server is down,
70964325Seric 	**  this can hang or give errors; use a timeout to avoid this
71064325Seric 	*/
71164325Seric 
71253037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
71353037Seric 	{
71463853Seric 		ctladdr->q_flags |= QQUEUEUP;
71553037Seric 		errno = 0;
71653037Seric 		usrerr("451 open timeout on %s", fname);
71763993Seric 
71863993Seric 		/* return pseudo-error code */
71964325Seric 		rval = EOPENTIMEOUT;
72064325Seric 		goto resetuid;
72153037Seric 	}
72253037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
72353037Seric 
72463581Seric 	/* the input file must be marked safe */
72564944Seric 	rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD);
72664329Seric 	if (rval != 0)
72753037Seric 	{
72864325Seric 		/* don't use this :include: file */
72953037Seric 		clrevent(ev);
73057186Seric 		if (tTd(27, 4))
73158247Seric 			printf("include: not safe (uid=%d): %s\n",
73264329Seric 				uid, errstring(rval));
73364329Seric 		goto resetuid;
73453037Seric 	}
73553037Seric 
7364174Seric 	fp = fopen(fname, "r");
7374174Seric 	if (fp == NULL)
7384174Seric 	{
73964329Seric 		rval = errno;
74063902Seric 		if (tTd(27, 4))
74164329Seric 			printf("include: open: %s\n", errstring(rval));
7424174Seric 	}
74364570Seric 	else if (ca == NULL)
7444406Seric 	{
7454406Seric 		struct stat st;
7464174Seric 
7474406Seric 		if (fstat(fileno(fp), &st) < 0)
74858061Seric 		{
74964329Seric 			rval = errno;
7504406Seric 			syserr("Cannot fstat %s!", fname);
75158061Seric 		}
75264570Seric 		else
75364570Seric 		{
75464570Seric 			ctladdr->q_uid = st.st_uid;
75564570Seric 			ctladdr->q_gid = st.st_gid;
75664570Seric 			ctladdr->q_flags |= QGOODUID;
75764570Seric 		}
7584406Seric 	}
7594406Seric 
76053037Seric 	clrevent(ev);
76153037Seric 
76264570Seric resetuid:
76364570Seric 
76464570Seric #ifdef HASSETREUID
76564570Seric 	if (saveduid == 0)
76664570Seric 	{
76764570Seric 		if (uid != 0)
76864570Seric 			if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0)
76964570Seric 				syserr("setreuid(%d, 0) failure (real=%d, eff=%d)",
77064570Seric 					RealUid, getuid(), geteuid());
77164570Seric 		setgid(savedgid);
77264570Seric 	}
77364570Seric #endif
77464570Seric 
77564570Seric 	if (tTd(27, 9))
77664570Seric 		printf("include: reset uid = %d/%d\n", getuid(), geteuid());
77764570Seric 
77864570Seric 	if (fp == NULL)
77964570Seric 		return rval;
78064570Seric 
78158092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
78258092Seric 	{
78358092Seric 		/* don't do any more now */
78458868Seric 		ctladdr->q_flags |= QVERIFIED;
78558884Seric 		e->e_nrcpts++;
78658680Seric 		xfclose(fp, "include", fname);
78764570Seric 		return rval;
78858092Seric 	}
78958092Seric 
7904174Seric 	/* read the file -- each line is a comma-separated list. */
7919379Seric 	FileName = fname;
7929379Seric 	LineNumber = 0;
79358082Seric 	ctladdr->q_flags &= ~QSELFREF;
79458082Seric 	nincludes = 0;
7954174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
7964174Seric 	{
79756795Seric 		register char *p = strchr(buf, '\n');
7984174Seric 
79940963Sbostic 		LineNumber++;
8004174Seric 		if (p != NULL)
8014174Seric 			*p = '\0';
80257186Seric 		if (buf[0] == '#' || buf[0] == '\0')
80357139Seric 			continue;
80458008Seric 		e->e_to = NULL;
80558151Seric 		message("%s to %s",
80653037Seric 			forwarding ? "forwarding" : "sending", buf);
80757977Seric #ifdef LOG
80858020Seric 		if (forwarding && LogLevel > 9)
80957977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
81057977Seric 				e->e_id, oldto, buf);
81157977Seric #endif
81257977Seric 
8134176Seric 		AliasLevel++;
81458082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
8154176Seric 		AliasLevel--;
8164174Seric 	}
81763902Seric 
81863902Seric 	if (ferror(fp) && tTd(27, 3))
81963902Seric 		printf("include: read error: %s\n", errstring(errno));
82058082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
82158065Seric 	{
82258065Seric 		if (tTd(27, 5))
82358065Seric 		{
82458065Seric 			printf("include: QDONTSEND ");
82558065Seric 			printaddr(ctladdr, FALSE);
82658065Seric 		}
82758065Seric 		ctladdr->q_flags |= QDONTSEND;
82858065Seric 	}
8294174Seric 
83058680Seric 	(void) xfclose(fp, "include", fname);
8319379Seric 	FileName = oldfilename;
8329379Seric 	LineNumber = oldlinenumber;
83363847Seric 	e->e_to = oldto;
83464325Seric 	return rval;
8354174Seric }
83653037Seric 
83753037Seric static
83853037Seric includetimeout()
83953037Seric {
84053037Seric 	longjmp(CtxIncludeTimeout, 1);
84153037Seric }
8424324Seric /*
8434324Seric **  SENDTOARGV -- send to an argument vector.
8444324Seric **
8454324Seric **	Parameters:
8464324Seric **		argv -- argument vector to send to.
84758247Seric **		e -- the current envelope.
8484324Seric **
8494324Seric **	Returns:
8504324Seric **		none.
8514324Seric **
8524324Seric **	Side Effects:
8534324Seric **		puts all addresses on the argument vector onto the
8544324Seric **			send queue.
8554324Seric */
8564324Seric 
85755012Seric sendtoargv(argv, e)
8584324Seric 	register char **argv;
85955012Seric 	register ENVELOPE *e;
8604324Seric {
8614324Seric 	register char *p;
8624324Seric 
8634324Seric 	while ((p = *argv++) != NULL)
8644324Seric 	{
86564284Seric 		(void) sendtolist(p, NULLADDR, &e->e_sendqueue, e);
8664324Seric 	}
8674324Seric }
8684399Seric /*
8694399Seric **  GETCTLADDR -- get controlling address from an address header.
8704399Seric **
8714399Seric **	If none, get one corresponding to the effective userid.
8724399Seric **
8734399Seric **	Parameters:
8744399Seric **		a -- the address to find the controller of.
8754399Seric **
8764399Seric **	Returns:
8774399Seric **		the controlling address.
8784399Seric **
8794399Seric **	Side Effects:
8804399Seric **		none.
8814399Seric */
8824399Seric 
8834399Seric ADDRESS *
8844399Seric getctladdr(a)
8854399Seric 	register ADDRESS *a;
8864399Seric {
8874404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
8884399Seric 		a = a->q_alias;
8894399Seric 	return (a);
8904399Seric }
891