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*65225Seric static char sccsid[] = "@(#)recipient.c	8.35 (Berkeley) 12/30/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;
27165215Seric 				a->q_flags &= ~QDONTSEND;
27259563Seric 				usrerr("451 Cannot open %s: %s",
27359563Seric 					a->q_user, errstring(ret));
27459563Seric 			}
27559563Seric 			else if (ret != 0)
27659563Seric 			{
27763938Seric 				a->q_flags |= QBADADDR;
27859563Seric 				usrerr("550 Cannot open %s: %s",
27959563Seric 					a->q_user, errstring(ret));
28059563Seric 			}
28150556Seric 		}
2824174Seric 	}
28357642Seric 	else if (m == FileMailer)
2844174Seric 	{
2854329Seric 		extern bool writable();
2864174Seric 
28751317Seric 		/* check if writable or creatable */
28864761Seric 		if (a->q_alias == NULL)
2894174Seric 		{
29058680Seric 			a->q_flags |= QBADADDR;
29158151Seric 			usrerr("550 Cannot mail directly to files");
2924174Seric 		}
29365112Seric 		else if (!writable(buf, getctladdr(a), SFF_ANYFILE))
29451317Seric 		{
29558680Seric 			a->q_flags |= QBADADDR;
29664771Seric 			giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e);
29751317Seric 		}
29851317Seric 	}
29951317Seric 
30057402Seric 	if (m != LocalMailer)
30157642Seric 	{
30257642Seric 		if (!bitset(QDONTSEND, a->q_flags))
30357642Seric 			e->e_nrcpts++;
30463847Seric 		goto testselfdestruct;
30557642Seric 	}
30657402Seric 
30757402Seric 	/* try aliasing */
30857402Seric 	alias(a, sendq, e);
30957402Seric 
31057402Seric # ifdef USERDB
31157402Seric 	/* if not aliased, look it up in the user database */
31258918Seric 	if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags))
31357402Seric 	{
31457402Seric 		extern int udbexpand();
31557402Seric 
31657402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
31757402Seric 		{
31863853Seric 			a->q_flags |= QQUEUEUP;
31957402Seric 			if (e->e_message == NULL)
32057402Seric 				e->e_message = newstr("Deferred: user database error");
32157402Seric # ifdef LOG
32258020Seric 			if (LogLevel > 8)
32359623Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand: %s",
32459623Seric 					e->e_id, errstring(errno));
32557402Seric # endif
32659615Seric 			message("queued (user database error): %s",
32759615Seric 				errstring(errno));
32857642Seric 			e->e_nrcpts++;
32963847Seric 			goto testselfdestruct;
33057402Seric 		}
33157402Seric 	}
33257402Seric # endif
33357402Seric 
33457402Seric 	/* if it was an alias or a UDB expansion, just return now */
33558247Seric 	if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags))
33663847Seric 		goto testselfdestruct;
33757402Seric 
33851317Seric 	/*
33951317Seric 	**  If we have a level two config file, then pass the name through
34051317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
34151317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
34251317Seric 	**  after local aliasing has been done.
34351317Seric 	*/
34451317Seric 
34551317Seric 	if (tTd(29, 5))
34651317Seric 	{
34751317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
34851317Seric 			ConfigLevel, RewriteRules[5]);
34951317Seric 		printaddr(a, FALSE);
35051317Seric 	}
35151317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
35251317Seric 	    RewriteRules[5] != NULL)
35351317Seric 	{
35455012Seric 		maplocaluser(a, sendq, e);
35551317Seric 	}
35651317Seric 
35751317Seric 	/*
35851317Seric 	**  If it didn't get rewritten to another mailer, go ahead
35951317Seric 	**  and deliver it.
36051317Seric 	*/
36151317Seric 
36258247Seric 	if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags))
36351317Seric 	{
36455354Seric 		auto bool fuzzy;
36551317Seric 		register struct passwd *pw;
36651317Seric 		extern struct passwd *finduser();
36751317Seric 
36851317Seric 		/* warning -- finduser may trash buf */
36955354Seric 		pw = finduser(buf, &fuzzy);
37051317Seric 		if (pw == NULL)
37151317Seric 		{
37258680Seric 			a->q_flags |= QBADADDR;
37364771Seric 			giveresponse(EX_NOUSER, m, NULL, a->q_alias, e);
37451317Seric 		}
3754174Seric 		else
3764174Seric 		{
37751317Seric 			char nbuf[MAXNAME];
3784373Seric 
37955354Seric 			if (fuzzy)
3804174Seric 			{
38153735Seric 				/* name was a fuzzy match */
38251317Seric 				a->q_user = newstr(pw->pw_name);
38353735Seric 				if (findusercount++ > 3)
38453735Seric 				{
38558680Seric 					a->q_flags |= QBADADDR;
38658151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
38753735Seric 						pw->pw_name);
38853735Seric 					return (a);
38953735Seric 				}
39053735Seric 
39153735Seric 				/* see if it aliases */
39251317Seric 				(void) strcpy(buf, pw->pw_name);
39353735Seric 				goto trylocaluser;
3944174Seric 			}
39551317Seric 			a->q_home = newstr(pw->pw_dir);
39651317Seric 			a->q_uid = pw->pw_uid;
39751317Seric 			a->q_gid = pw->pw_gid;
39859083Seric 			a->q_ruser = newstr(pw->pw_name);
39951317Seric 			a->q_flags |= QGOODUID;
40051317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
40151317Seric 			if (nbuf[0] != '\0')
40251317Seric 				a->q_fullname = newstr(nbuf);
40365211Seric 			if (pw->pw_shell != NULL && pw->pw_shell[0] != '\0' &&
40465211Seric 			    !usershellok(pw->pw_shell))
40565206Seric 			{
40665211Seric 				a->q_flags |= QBOGUSSHELL;
40765206Seric 			}
40851317Seric 			if (!quoted)
40955012Seric 				forward(a, sendq, e);
4104174Seric 		}
4114174Seric 	}
41257642Seric 	if (!bitset(QDONTSEND, a->q_flags))
41357642Seric 		e->e_nrcpts++;
41463847Seric 
41563847Seric   testselfdestruct:
41663978Seric 	if (tTd(26, 8))
41763847Seric 	{
41863978Seric 		printf("testselfdestruct: ");
41963978Seric 		printaddr(a, TRUE);
42063978Seric 	}
42163978Seric 	if (a->q_alias == NULL && a != &e->e_from &&
42263978Seric 	    bitset(QDONTSEND, a->q_flags))
42363978Seric 	{
42463978Seric 		q = *sendq;
42563965Seric 		while (q != NULL && bitset(QDONTSEND, q->q_flags))
42663847Seric 			q = q->q_next;
42763978Seric 		if (q == NULL)
42863847Seric 		{
42963847Seric 			a->q_flags |= QBADADDR;
43063847Seric 			usrerr("554 aliasing/forwarding loop broken");
43163847Seric 		}
43263847Seric 	}
43312613Seric 	return (a);
4344174Seric }
4354174Seric /*
4364373Seric **  FINDUSER -- find the password entry for a user.
4374373Seric **
4384373Seric **	This looks a lot like getpwnam, except that it may want to
4394373Seric **	do some fancier pattern matching in /etc/passwd.
4404373Seric **
4419379Seric **	This routine contains most of the time of many sendmail runs.
4429379Seric **	It deserves to be optimized.
4439379Seric **
4444373Seric **	Parameters:
4454373Seric **		name -- the name to match against.
44655354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
44755354Seric **			was found using the fuzzy matching algorithm;
44855354Seric **			set to FALSE otherwise.
4494373Seric **
4504373Seric **	Returns:
4514373Seric **		A pointer to a pw struct.
4524373Seric **		NULL if name is unknown or ambiguous.
4534373Seric **
4544373Seric **	Side Effects:
4554407Seric **		may modify name.
4564373Seric */
4574373Seric 
4584373Seric struct passwd *
45955354Seric finduser(name, fuzzyp)
4604373Seric 	char *name;
46155354Seric 	bool *fuzzyp;
4624373Seric {
4634376Seric 	register struct passwd *pw;
4644407Seric 	register char *p;
46515325Seric 	extern struct passwd *getpwent();
46615325Seric 	extern struct passwd *getpwnam();
4674373Seric 
46855354Seric 	if (tTd(29, 4))
46955354Seric 		printf("finduser(%s): ", name);
47055354Seric 
47155354Seric 	*fuzzyp = FALSE;
4724407Seric 
47364673Seric 	/* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */
47464673Seric 	for (p = name; *p != '\0'; p++)
47564673Seric 		if (!isascii(*p) || !isdigit(*p))
47664673Seric 			break;
47764673Seric 	if (*p == '\0')
47864673Seric 	{
47964673Seric 		if (tTd(29, 4))
48064673Seric 			printf("failed (numeric input)\n");
48164673Seric 		return NULL;
48264673Seric 	}
48364673Seric 
48425777Seric 	/* look up this login name using fast path */
48512634Seric 	if ((pw = getpwnam(name)) != NULL)
48655354Seric 	{
48755354Seric 		if (tTd(29, 4))
48855354Seric 			printf("found (non-fuzzy)\n");
48912634Seric 		return (pw);
49055354Seric 	}
49112634Seric 
49253735Seric #ifdef MATCHGECOS
49353735Seric 	/* see if fuzzy matching allowed */
49453735Seric 	if (!MatchGecos)
49555354Seric 	{
49655354Seric 		if (tTd(29, 4))
49755354Seric 			printf("not found (fuzzy disabled)\n");
49853735Seric 		return NULL;
49955354Seric 	}
50053735Seric 
50112634Seric 	/* search for a matching full name instead */
50225777Seric 	for (p = name; *p != '\0'; p++)
50325777Seric 	{
50425777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
50525777Seric 			*p = ' ';
50625777Seric 	}
50723107Seric 	(void) setpwent();
5084376Seric 	while ((pw = getpwent()) != NULL)
5094376Seric 	{
5104998Seric 		char buf[MAXNAME];
5114376Seric 
5124998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
51356795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
5144381Seric 		{
51555354Seric 			if (tTd(29, 4))
51655354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
51758151Seric 			message("sending to login name %s", pw->pw_name);
51855354Seric 			*fuzzyp = TRUE;
5194376Seric 			return (pw);
5204377Seric 		}
5214376Seric 	}
52255354Seric 	if (tTd(29, 4))
52355354Seric 		printf("no fuzzy match found\n");
52459015Seric #else
52559015Seric 	if (tTd(29, 4))
52659015Seric 		printf("not found (fuzzy disabled)\n");
52759015Seric #endif
5284376Seric 	return (NULL);
5294373Seric }
5304373Seric /*
5314329Seric **  WRITABLE -- predicate returning if the file is writable.
5324329Seric **
5334329Seric **	This routine must duplicate the algorithm in sys/fio.c.
5344329Seric **	Unfortunately, we cannot use the access call since we
5354329Seric **	won't necessarily be the real uid when we try to
5364329Seric **	actually open the file.
5374329Seric **
5384329Seric **	Notice that ANY file with ANY execute bit is automatically
5394329Seric **	not writable.  This is also enforced by mailfile.
5404329Seric **
5414329Seric **	Parameters:
54265064Seric **		filename -- the file name to check.
54365112Seric **		ctladdr -- the controlling address for this file.
54465064Seric **		flags -- SFF_* flags to control the function.
5454329Seric **
5464329Seric **	Returns:
5474329Seric **		TRUE -- if we will be able to write this file.
5484329Seric **		FALSE -- if we cannot write this file.
5494329Seric **
5504329Seric **	Side Effects:
5514329Seric **		none.
5524329Seric */
5534329Seric 
5544329Seric bool
55565112Seric writable(filename, ctladdr, flags)
55664819Seric 	char *filename;
55765112Seric 	ADDRESS *ctladdr;
55865064Seric 	int flags;
5594329Seric {
56055372Seric 	uid_t euid;
56155372Seric 	gid_t egid;
5624329Seric 	int bits;
56364944Seric 	register char *p;
56464944Seric 	char *uname;
56564944Seric 	struct stat stb;
56664944Seric 	extern char RealUserName[];
5674329Seric 
56864819Seric 	if (tTd(29, 5))
56965064Seric 		printf("writable(%s, %x)\n", filename, flags);
57064944Seric 
57164944Seric #ifdef HASLSTAT
57265064Seric 	if ((bitset(SFF_NOSLINK, flags) ? lstat(filename, &stb)
57365064Seric 					: stat(filename, &stb)) < 0)
57464944Seric #else
57564944Seric 	if (stat(filename, &stb) < 0)
57664944Seric #endif
57764944Seric 	{
57864944Seric 		/* file does not exist -- see if directory is safe */
57964944Seric 		p = strrchr(filename, '/');
58064944Seric 		if (p == NULL)
58164944Seric 		{
58265067Seric 			errno = ENOTDIR;
58364944Seric 			return FALSE;
58464944Seric 		}
58565067Seric 		*p = '\0';
58665067Seric 		errno = safefile(filename, RealUid, RealGid, RealUserName,
58765067Seric 				 SFF_MUSTOWN, S_IWRITE|S_IEXEC);
58864944Seric 		*p = '/';
58965067Seric 		return errno == 0;
59064944Seric 	}
59164944Seric 
592*65225Seric #ifdef SUID_ROOT_FILES_OK
593*65225Seric 	/* really ought to be passed down -- and not a good idea */
594*65225Seric 	flags |= SFF_ROOTOK;
595*65225Seric #endif
596*65225Seric 
59764944Seric 	/*
59864944Seric 	**  File does exist -- check that it is writable.
59964944Seric 	*/
60064944Seric 
60164944Seric 	if (bitset(0111, stb.st_mode))
60265022Seric 	{
60365022Seric 		if (tTd(29, 5))
60465022Seric 			printf("failed (mode %o: x bits)\n", stb.st_mode);
60565067Seric 		errno = EPERM;
6064329Seric 		return (FALSE);
60765022Seric 	}
60864944Seric 
60965112Seric 	if (ctladdr != NULL && geteuid() == 0)
61064944Seric 	{
61165112Seric 		euid = ctladdr->q_uid;
61265112Seric 		egid = ctladdr->q_gid;
61365112Seric 		uname = ctladdr->q_user;
61464944Seric 	}
61565112Seric 	else
61665112Seric 	{
61765112Seric 		euid = RealUid;
61865112Seric 		egid = RealGid;
61965112Seric 		uname = RealUserName;
62065112Seric 	}
62165138Seric 	if (euid == 0)
62265138Seric 	{
62365138Seric 		euid = DefUid;
62465138Seric 		uname = DefUser;
62565138Seric 	}
62665138Seric 	if (egid == 0)
62765138Seric 		egid = DefGid;
6284329Seric 	if (geteuid() == 0)
6294329Seric 	{
630*65225Seric 		if (bitset(S_ISUID, stb.st_mode) &&
631*65225Seric 		    (stb.st_uid != 0 || bitset(SFF_ROOTOK, flags)))
63264944Seric 		{
63364944Seric 			euid = stb.st_uid;
63464944Seric 			uname = NULL;
63564944Seric 		}
636*65225Seric 		if (bitset(S_ISGID, stb.st_mode) &&
637*65225Seric 		    (stb.st_gid != 0 || bitset(SFF_ROOTOK, flags)))
63864944Seric 			egid = stb.st_gid;
6394329Seric 	}
6404329Seric 
64164819Seric 	if (tTd(29, 5))
64264819Seric 		printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n",
64364944Seric 			euid, egid, stb.st_uid, stb.st_gid);
64464819Seric 
64565067Seric 	errno = safefile(filename, euid, egid, uname, flags, S_IWRITE);
64665067Seric 	return errno == 0;
6474329Seric }
6484329Seric /*
6494174Seric **  INCLUDE -- handle :include: specification.
6504174Seric **
6514174Seric **	Parameters:
6524174Seric **		fname -- filename to include.
65353037Seric **		forwarding -- if TRUE, we are reading a .forward file.
65453037Seric **			if FALSE, it's a :include: file.
6554399Seric **		ctladdr -- address template to use to fill in these
6564399Seric **			addresses -- effective user/group id are
6574399Seric **			the important things.
6585006Seric **		sendq -- a pointer to the head of the send queue
6595006Seric **			to put these addresses in.
6604174Seric **
6614174Seric **	Returns:
66257136Seric **		open error status
6634174Seric **
6644174Seric **	Side Effects:
6654174Seric **		reads the :include: file and sends to everyone
6664174Seric **		listed in that file.
6674174Seric */
6684174Seric 
66953037Seric static jmp_buf	CtxIncludeTimeout;
67063937Seric static int	includetimeout();
67153037Seric 
67257136Seric int
67355012Seric include(fname, forwarding, ctladdr, sendq, e)
6744174Seric 	char *fname;
67553037Seric 	bool forwarding;
6764399Seric 	ADDRESS *ctladdr;
6775006Seric 	ADDRESS **sendq;
67855012Seric 	ENVELOPE *e;
6794174Seric {
68064570Seric 	register FILE *fp = NULL;
68155012Seric 	char *oldto = e->e_to;
6829379Seric 	char *oldfilename = FileName;
6839379Seric 	int oldlinenumber = LineNumber;
68453037Seric 	register EVENT *ev = NULL;
68558082Seric 	int nincludes;
68664325Seric 	register ADDRESS *ca;
68764325Seric 	uid_t saveduid, uid;
68864325Seric 	gid_t savedgid, gid;
68964083Seric 	char *uname;
69064325Seric 	int rval = 0;
69165064Seric 	int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE;
69253037Seric 	char buf[MAXLINE];
6934174Seric 
69457186Seric 	if (tTd(27, 2))
69557186Seric 		printf("include(%s)\n", fname);
69663902Seric 	if (tTd(27, 4))
69763902Seric 		printf("   ruid=%d euid=%d\n", getuid(), geteuid());
69863581Seric 	if (tTd(27, 14))
69963581Seric 	{
70063581Seric 		printf("ctladdr ");
70163581Seric 		printaddr(ctladdr, FALSE);
70263581Seric 	}
70357186Seric 
70464325Seric 	if (tTd(27, 9))
70564325Seric 		printf("include: old uid = %d/%d\n", getuid(), geteuid());
70653037Seric 
70763581Seric 	ca = getctladdr(ctladdr);
70863581Seric 	if (ca == NULL)
70964083Seric 	{
71064846Seric 		uid = DefUid;
71164846Seric 		gid = DefGid;
71264846Seric 		uname = DefUser;
71364325Seric 		saveduid = -1;
71464083Seric 	}
71563581Seric 	else
71664083Seric 	{
71763581Seric 		uid = ca->q_uid;
71864083Seric 		gid = ca->q_gid;
71964083Seric 		uname = ca->q_user;
72064325Seric #ifdef HASSETREUID
72164325Seric 		saveduid = geteuid();
72264325Seric 		savedgid = getegid();
72364325Seric 		if (saveduid == 0)
72464325Seric 		{
72564325Seric 			initgroups(uname, gid);
72664325Seric 			if (uid != 0)
72764325Seric 				(void) setreuid(0, uid);
72864325Seric 		}
72964325Seric #endif
73064083Seric 	}
73163581Seric 
73264325Seric 	if (tTd(27, 9))
73364325Seric 		printf("include: new uid = %d/%d\n", getuid(), geteuid());
73464325Seric 
73564325Seric 	/*
73664325Seric 	**  If home directory is remote mounted but server is down,
73764325Seric 	**  this can hang or give errors; use a timeout to avoid this
73864325Seric 	*/
73964325Seric 
74053037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
74153037Seric 	{
74263853Seric 		ctladdr->q_flags |= QQUEUEUP;
74353037Seric 		errno = 0;
74453037Seric 		usrerr("451 open timeout on %s", fname);
74563993Seric 
74663993Seric 		/* return pseudo-error code */
74764325Seric 		rval = EOPENTIMEOUT;
74864325Seric 		goto resetuid;
74953037Seric 	}
75053037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
75153037Seric 
75263581Seric 	/* the input file must be marked safe */
75364944Seric 	rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD);
75464329Seric 	if (rval != 0)
75553037Seric 	{
75664325Seric 		/* don't use this :include: file */
75753037Seric 		clrevent(ev);
75857186Seric 		if (tTd(27, 4))
75958247Seric 			printf("include: not safe (uid=%d): %s\n",
76064329Seric 				uid, errstring(rval));
76164329Seric 		goto resetuid;
76253037Seric 	}
76353037Seric 
7644174Seric 	fp = fopen(fname, "r");
7654174Seric 	if (fp == NULL)
7664174Seric 	{
76764329Seric 		rval = errno;
76863902Seric 		if (tTd(27, 4))
76964329Seric 			printf("include: open: %s\n", errstring(rval));
7704174Seric 	}
77164570Seric 	else if (ca == NULL)
7724406Seric 	{
7734406Seric 		struct stat st;
7744174Seric 
7754406Seric 		if (fstat(fileno(fp), &st) < 0)
77658061Seric 		{
77764329Seric 			rval = errno;
7784406Seric 			syserr("Cannot fstat %s!", fname);
77958061Seric 		}
78064570Seric 		else
78164570Seric 		{
78264570Seric 			ctladdr->q_uid = st.st_uid;
78364570Seric 			ctladdr->q_gid = st.st_gid;
78464570Seric 			ctladdr->q_flags |= QGOODUID;
78564570Seric 		}
7864406Seric 	}
7874406Seric 
78853037Seric 	clrevent(ev);
78953037Seric 
79064570Seric resetuid:
79164570Seric 
79264570Seric #ifdef HASSETREUID
79364570Seric 	if (saveduid == 0)
79464570Seric 	{
79564570Seric 		if (uid != 0)
79664570Seric 			if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0)
79764570Seric 				syserr("setreuid(%d, 0) failure (real=%d, eff=%d)",
79864570Seric 					RealUid, getuid(), geteuid());
79964570Seric 		setgid(savedgid);
80064570Seric 	}
80164570Seric #endif
80264570Seric 
80364570Seric 	if (tTd(27, 9))
80464570Seric 		printf("include: reset uid = %d/%d\n", getuid(), geteuid());
80564570Seric 
80664570Seric 	if (fp == NULL)
80764570Seric 		return rval;
80864570Seric 
80958092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
81058092Seric 	{
81158092Seric 		/* don't do any more now */
81258868Seric 		ctladdr->q_flags |= QVERIFIED;
81358884Seric 		e->e_nrcpts++;
81458680Seric 		xfclose(fp, "include", fname);
81564570Seric 		return rval;
81658092Seric 	}
81758092Seric 
8184174Seric 	/* read the file -- each line is a comma-separated list. */
8199379Seric 	FileName = fname;
8209379Seric 	LineNumber = 0;
82158082Seric 	ctladdr->q_flags &= ~QSELFREF;
82258082Seric 	nincludes = 0;
8234174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
8244174Seric 	{
82556795Seric 		register char *p = strchr(buf, '\n');
8264174Seric 
82740963Sbostic 		LineNumber++;
8284174Seric 		if (p != NULL)
8294174Seric 			*p = '\0';
83057186Seric 		if (buf[0] == '#' || buf[0] == '\0')
83157139Seric 			continue;
83258008Seric 		e->e_to = NULL;
83358151Seric 		message("%s to %s",
83453037Seric 			forwarding ? "forwarding" : "sending", buf);
83557977Seric #ifdef LOG
83658020Seric 		if (forwarding && LogLevel > 9)
83757977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
83857977Seric 				e->e_id, oldto, buf);
83957977Seric #endif
84057977Seric 
8414176Seric 		AliasLevel++;
84258082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
8434176Seric 		AliasLevel--;
8444174Seric 	}
84563902Seric 
84663902Seric 	if (ferror(fp) && tTd(27, 3))
84763902Seric 		printf("include: read error: %s\n", errstring(errno));
84858082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
84958065Seric 	{
85058065Seric 		if (tTd(27, 5))
85158065Seric 		{
85258065Seric 			printf("include: QDONTSEND ");
85358065Seric 			printaddr(ctladdr, FALSE);
85458065Seric 		}
85558065Seric 		ctladdr->q_flags |= QDONTSEND;
85658065Seric 	}
8574174Seric 
85858680Seric 	(void) xfclose(fp, "include", fname);
8599379Seric 	FileName = oldfilename;
8609379Seric 	LineNumber = oldlinenumber;
86163847Seric 	e->e_to = oldto;
86264325Seric 	return rval;
8634174Seric }
86453037Seric 
86553037Seric static
86653037Seric includetimeout()
86753037Seric {
86853037Seric 	longjmp(CtxIncludeTimeout, 1);
86953037Seric }
8704324Seric /*
8714324Seric **  SENDTOARGV -- send to an argument vector.
8724324Seric **
8734324Seric **	Parameters:
8744324Seric **		argv -- argument vector to send to.
87558247Seric **		e -- the current envelope.
8764324Seric **
8774324Seric **	Returns:
8784324Seric **		none.
8794324Seric **
8804324Seric **	Side Effects:
8814324Seric **		puts all addresses on the argument vector onto the
8824324Seric **			send queue.
8834324Seric */
8844324Seric 
88555012Seric sendtoargv(argv, e)
8864324Seric 	register char **argv;
88755012Seric 	register ENVELOPE *e;
8884324Seric {
8894324Seric 	register char *p;
8904324Seric 
8914324Seric 	while ((p = *argv++) != NULL)
8924324Seric 	{
89364284Seric 		(void) sendtolist(p, NULLADDR, &e->e_sendqueue, e);
8944324Seric 	}
8954324Seric }
8964399Seric /*
8974399Seric **  GETCTLADDR -- get controlling address from an address header.
8984399Seric **
8994399Seric **	If none, get one corresponding to the effective userid.
9004399Seric **
9014399Seric **	Parameters:
9024399Seric **		a -- the address to find the controller of.
9034399Seric **
9044399Seric **	Returns:
9054399Seric **		the controlling address.
9064399Seric **
9074399Seric **	Side Effects:
9084399Seric **		none.
9094399Seric */
9104399Seric 
9114399Seric ADDRESS *
9124399Seric getctladdr(a)
9134399Seric 	register ADDRESS *a;
9144399Seric {
9154404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
9164399Seric 		a = a->q_alias;
9174399Seric 	return (a);
9184399Seric }
919