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*65593Seric static char sccsid[] = "@(#)recipient.c	8.39 (Berkeley) 01/10/94";
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 */
19965496Seric 	if (m == ProgMailer)
2004174Seric 	{
20165496Seric 		if (a->q_alias == NULL)
20265496Seric 		{
20365496Seric 			a->q_flags |= QBADADDR;
20465496Seric 			usrerr("550 Cannot mail directly to programs");
20565496Seric 		}
20665496Seric 		else if (bitset(QBOGUSSHELL, a->q_alias->q_flags))
20765496Seric 		{
20865496Seric 			a->q_flags |= QBADADDR;
20965496Seric 			usrerr("550 User %s@%s doesn't have a valid shell for mailing to programs",
21065496Seric 				a->q_alias->q_ruser, MyHostName);
21165496Seric 		}
21265496Seric 		else if (bitset(QUNSAFEADDR, a->q_alias->q_flags))
21365496Seric 		{
21465496Seric 			a->q_flags |= QBADADDR;
21565496Seric 			usrerr("550 Address %s is unsafe for mailing to programs",
21665496Seric 				a->q_alias->q_paddr);
21765496Seric 		}
2184174Seric 	}
2194174Seric 
2204174Seric 	/*
2214419Seric 	**  Look up this person in the recipient list.
2224419Seric 	**	If they are there already, return, otherwise continue.
2234419Seric 	**	If the list is empty, just add it.  Notice the cute
2244419Seric 	**	hack to make from addresses suppress things correctly:
2254419Seric 	**	the QDONTSEND bit will be set in the send list.
2264419Seric 	**	[Please note: the emphasis is on "hack."]
2274174Seric 	*/
2284174Seric 
2295006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2304174Seric 	{
23158294Seric 		if (sameaddr(q, a))
2324174Seric 		{
2337676Seric 			if (tTd(26, 1))
2344444Seric 			{
2354444Seric 				printf("%s in sendq: ", a->q_paddr);
2364444Seric 				printaddr(q, FALSE);
2374444Seric 			}
238*65593Seric 			if (!bitset(QPRIMARY, q->q_flags))
23958065Seric 			{
240*65593Seric 				if (!bitset(QDONTSEND, a->q_flags))
24158151Seric 					message("duplicate suppressed");
242*65593Seric 				q->q_flags |= a->q_flags;
243*65593Seric 			}
244*65593Seric 			else if (bitset(QSELFREF, q->q_flags))
24565579Seric 				q->q_flags |= a->q_flags & ~QDONTSEND;
24663847Seric 			a = q;
24763847Seric 			goto testselfdestruct;
2484174Seric 		}
2494319Seric 	}
2504174Seric 
2514319Seric 	/* add address on list */
25258884Seric 	*pq = a;
25358884Seric 	a->q_next = NULL;
2544174Seric 
2554174Seric 	/*
25657402Seric 	**  Alias the name and handle special mailer types.
2574174Seric 	*/
2584174Seric 
25953735Seric   trylocaluser:
26055354Seric 	if (tTd(29, 7))
26155354Seric 		printf("at trylocaluser %s\n", a->q_user);
26255354Seric 
26358680Seric 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
26463847Seric 		goto testselfdestruct;
26557402Seric 
26657402Seric 	if (m == InclMailer)
2674174Seric 	{
26857402Seric 		a->q_flags |= QDONTSEND;
26964761Seric 		if (a->q_alias == NULL)
2704174Seric 		{
27158680Seric 			a->q_flags |= QBADADDR;
27258151Seric 			usrerr("550 Cannot mail directly to :include:s");
2734174Seric 		}
2744174Seric 		else
27550556Seric 		{
27659563Seric 			int ret;
27758247Seric 
27858151Seric 			message("including file %s", a->q_user);
27959563Seric 			ret = include(a->q_user, FALSE, a, sendq, e);
28059563Seric 			if (transienterror(ret))
28159563Seric 			{
28259563Seric #ifdef LOG
28359563Seric 				if (LogLevel > 2)
28459615Seric 					syslog(LOG_ERR, "%s: include %s: transient error: %e",
28559623Seric 						e->e_id, a->q_user, errstring(ret));
28659563Seric #endif
28763853Seric 				a->q_flags |= QQUEUEUP;
28865215Seric 				a->q_flags &= ~QDONTSEND;
28959563Seric 				usrerr("451 Cannot open %s: %s",
29059563Seric 					a->q_user, errstring(ret));
29159563Seric 			}
29259563Seric 			else if (ret != 0)
29359563Seric 			{
29463938Seric 				a->q_flags |= QBADADDR;
29559563Seric 				usrerr("550 Cannot open %s: %s",
29659563Seric 					a->q_user, errstring(ret));
29759563Seric 			}
29850556Seric 		}
2994174Seric 	}
30057642Seric 	else if (m == FileMailer)
3014174Seric 	{
3024329Seric 		extern bool writable();
3034174Seric 
30451317Seric 		/* check if writable or creatable */
30564761Seric 		if (a->q_alias == NULL)
3064174Seric 		{
30758680Seric 			a->q_flags |= QBADADDR;
30858151Seric 			usrerr("550 Cannot mail directly to files");
3094174Seric 		}
31065496Seric 		else if (bitset(QBOGUSSHELL, a->q_alias->q_flags))
31165496Seric 		{
31265496Seric 			a->q_flags |= QBADADDR;
31365496Seric 			usrerr("550 User %s@%s doesn't have a valid shell for mailing to files",
31465496Seric 				a->q_alias->q_ruser, MyHostName);
31565496Seric 		}
31665496Seric 		else if (bitset(QUNSAFEADDR, a->q_alias->q_flags))
31765496Seric 		{
31865496Seric 			a->q_flags |= QBADADDR;
31965496Seric 			usrerr("550 Address %s is unsafe for mailing to files",
32065496Seric 				a->q_alias->q_paddr);
32165496Seric 		}
32265112Seric 		else if (!writable(buf, getctladdr(a), SFF_ANYFILE))
32351317Seric 		{
32458680Seric 			a->q_flags |= QBADADDR;
32564771Seric 			giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e);
32651317Seric 		}
32751317Seric 	}
32851317Seric 
32957402Seric 	if (m != LocalMailer)
33057642Seric 	{
33157642Seric 		if (!bitset(QDONTSEND, a->q_flags))
33257642Seric 			e->e_nrcpts++;
33363847Seric 		goto testselfdestruct;
33457642Seric 	}
33557402Seric 
33657402Seric 	/* try aliasing */
33757402Seric 	alias(a, sendq, e);
33857402Seric 
33957402Seric # ifdef USERDB
34057402Seric 	/* if not aliased, look it up in the user database */
34158918Seric 	if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags))
34257402Seric 	{
34357402Seric 		extern int udbexpand();
34457402Seric 
34557402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
34657402Seric 		{
34763853Seric 			a->q_flags |= QQUEUEUP;
34857402Seric 			if (e->e_message == NULL)
34957402Seric 				e->e_message = newstr("Deferred: user database error");
35057402Seric # ifdef LOG
35158020Seric 			if (LogLevel > 8)
35259623Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand: %s",
35359623Seric 					e->e_id, errstring(errno));
35457402Seric # endif
35559615Seric 			message("queued (user database error): %s",
35659615Seric 				errstring(errno));
35757642Seric 			e->e_nrcpts++;
35863847Seric 			goto testselfdestruct;
35957402Seric 		}
36057402Seric 	}
36157402Seric # endif
36257402Seric 
36357402Seric 	/* if it was an alias or a UDB expansion, just return now */
36458247Seric 	if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags))
36563847Seric 		goto testselfdestruct;
36657402Seric 
36751317Seric 	/*
36851317Seric 	**  If we have a level two config file, then pass the name through
36951317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
37051317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
37151317Seric 	**  after local aliasing has been done.
37251317Seric 	*/
37351317Seric 
37451317Seric 	if (tTd(29, 5))
37551317Seric 	{
37651317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
37751317Seric 			ConfigLevel, RewriteRules[5]);
37851317Seric 		printaddr(a, FALSE);
37951317Seric 	}
38051317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
38151317Seric 	    RewriteRules[5] != NULL)
38251317Seric 	{
38355012Seric 		maplocaluser(a, sendq, e);
38451317Seric 	}
38551317Seric 
38651317Seric 	/*
38751317Seric 	**  If it didn't get rewritten to another mailer, go ahead
38851317Seric 	**  and deliver it.
38951317Seric 	*/
39051317Seric 
39158247Seric 	if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags))
39251317Seric 	{
39355354Seric 		auto bool fuzzy;
39451317Seric 		register struct passwd *pw;
39551317Seric 		extern struct passwd *finduser();
39651317Seric 
39751317Seric 		/* warning -- finduser may trash buf */
39855354Seric 		pw = finduser(buf, &fuzzy);
39951317Seric 		if (pw == NULL)
40051317Seric 		{
40158680Seric 			a->q_flags |= QBADADDR;
40264771Seric 			giveresponse(EX_NOUSER, m, NULL, a->q_alias, e);
40351317Seric 		}
4044174Seric 		else
4054174Seric 		{
40651317Seric 			char nbuf[MAXNAME];
4074373Seric 
40855354Seric 			if (fuzzy)
4094174Seric 			{
41053735Seric 				/* name was a fuzzy match */
41151317Seric 				a->q_user = newstr(pw->pw_name);
41253735Seric 				if (findusercount++ > 3)
41353735Seric 				{
41458680Seric 					a->q_flags |= QBADADDR;
41558151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
41653735Seric 						pw->pw_name);
41753735Seric 					return (a);
41853735Seric 				}
41953735Seric 
42053735Seric 				/* see if it aliases */
42151317Seric 				(void) strcpy(buf, pw->pw_name);
42253735Seric 				goto trylocaluser;
4234174Seric 			}
42451317Seric 			a->q_home = newstr(pw->pw_dir);
42551317Seric 			a->q_uid = pw->pw_uid;
42651317Seric 			a->q_gid = pw->pw_gid;
42759083Seric 			a->q_ruser = newstr(pw->pw_name);
42851317Seric 			a->q_flags |= QGOODUID;
42951317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
43051317Seric 			if (nbuf[0] != '\0')
43151317Seric 				a->q_fullname = newstr(nbuf);
43265211Seric 			if (pw->pw_shell != NULL && pw->pw_shell[0] != '\0' &&
43365211Seric 			    !usershellok(pw->pw_shell))
43465206Seric 			{
43565211Seric 				a->q_flags |= QBOGUSSHELL;
43665206Seric 			}
43751317Seric 			if (!quoted)
43855012Seric 				forward(a, sendq, e);
4394174Seric 		}
4404174Seric 	}
44157642Seric 	if (!bitset(QDONTSEND, a->q_flags))
44257642Seric 		e->e_nrcpts++;
44363847Seric 
44463847Seric   testselfdestruct:
44563978Seric 	if (tTd(26, 8))
44663847Seric 	{
44763978Seric 		printf("testselfdestruct: ");
44863978Seric 		printaddr(a, TRUE);
44963978Seric 	}
45063978Seric 	if (a->q_alias == NULL && a != &e->e_from &&
45163978Seric 	    bitset(QDONTSEND, a->q_flags))
45263978Seric 	{
45363978Seric 		q = *sendq;
45463965Seric 		while (q != NULL && bitset(QDONTSEND, q->q_flags))
45563847Seric 			q = q->q_next;
45663978Seric 		if (q == NULL)
45763847Seric 		{
45863847Seric 			a->q_flags |= QBADADDR;
45963847Seric 			usrerr("554 aliasing/forwarding loop broken");
46063847Seric 		}
46163847Seric 	}
46212613Seric 	return (a);
4634174Seric }
4644174Seric /*
4654373Seric **  FINDUSER -- find the password entry for a user.
4664373Seric **
4674373Seric **	This looks a lot like getpwnam, except that it may want to
4684373Seric **	do some fancier pattern matching in /etc/passwd.
4694373Seric **
4709379Seric **	This routine contains most of the time of many sendmail runs.
4719379Seric **	It deserves to be optimized.
4729379Seric **
4734373Seric **	Parameters:
4744373Seric **		name -- the name to match against.
47555354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
47655354Seric **			was found using the fuzzy matching algorithm;
47755354Seric **			set to FALSE otherwise.
4784373Seric **
4794373Seric **	Returns:
4804373Seric **		A pointer to a pw struct.
4814373Seric **		NULL if name is unknown or ambiguous.
4824373Seric **
4834373Seric **	Side Effects:
4844407Seric **		may modify name.
4854373Seric */
4864373Seric 
4874373Seric struct passwd *
48855354Seric finduser(name, fuzzyp)
4894373Seric 	char *name;
49055354Seric 	bool *fuzzyp;
4914373Seric {
4924376Seric 	register struct passwd *pw;
4934407Seric 	register char *p;
49415325Seric 	extern struct passwd *getpwent();
49515325Seric 	extern struct passwd *getpwnam();
4964373Seric 
49755354Seric 	if (tTd(29, 4))
49855354Seric 		printf("finduser(%s): ", name);
49955354Seric 
50055354Seric 	*fuzzyp = FALSE;
5014407Seric 
50264673Seric 	/* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */
50364673Seric 	for (p = name; *p != '\0'; p++)
50464673Seric 		if (!isascii(*p) || !isdigit(*p))
50564673Seric 			break;
50664673Seric 	if (*p == '\0')
50764673Seric 	{
50864673Seric 		if (tTd(29, 4))
50964673Seric 			printf("failed (numeric input)\n");
51064673Seric 		return NULL;
51164673Seric 	}
51264673Seric 
51325777Seric 	/* look up this login name using fast path */
51412634Seric 	if ((pw = getpwnam(name)) != NULL)
51555354Seric 	{
51655354Seric 		if (tTd(29, 4))
51755354Seric 			printf("found (non-fuzzy)\n");
51812634Seric 		return (pw);
51955354Seric 	}
52012634Seric 
52153735Seric #ifdef MATCHGECOS
52253735Seric 	/* see if fuzzy matching allowed */
52353735Seric 	if (!MatchGecos)
52455354Seric 	{
52555354Seric 		if (tTd(29, 4))
52655354Seric 			printf("not found (fuzzy disabled)\n");
52753735Seric 		return NULL;
52855354Seric 	}
52953735Seric 
53012634Seric 	/* search for a matching full name instead */
53125777Seric 	for (p = name; *p != '\0'; p++)
53225777Seric 	{
53325777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
53425777Seric 			*p = ' ';
53525777Seric 	}
53623107Seric 	(void) setpwent();
5374376Seric 	while ((pw = getpwent()) != NULL)
5384376Seric 	{
5394998Seric 		char buf[MAXNAME];
5404376Seric 
5414998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
54256795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
5434381Seric 		{
54455354Seric 			if (tTd(29, 4))
54555354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
54658151Seric 			message("sending to login name %s", pw->pw_name);
54755354Seric 			*fuzzyp = TRUE;
5484376Seric 			return (pw);
5494377Seric 		}
5504376Seric 	}
55155354Seric 	if (tTd(29, 4))
55255354Seric 		printf("no fuzzy match found\n");
55359015Seric #else
55459015Seric 	if (tTd(29, 4))
55559015Seric 		printf("not found (fuzzy disabled)\n");
55659015Seric #endif
5574376Seric 	return (NULL);
5584373Seric }
5594373Seric /*
5604329Seric **  WRITABLE -- predicate returning if the file is writable.
5614329Seric **
5624329Seric **	This routine must duplicate the algorithm in sys/fio.c.
5634329Seric **	Unfortunately, we cannot use the access call since we
5644329Seric **	won't necessarily be the real uid when we try to
5654329Seric **	actually open the file.
5664329Seric **
5674329Seric **	Notice that ANY file with ANY execute bit is automatically
5684329Seric **	not writable.  This is also enforced by mailfile.
5694329Seric **
5704329Seric **	Parameters:
57165064Seric **		filename -- the file name to check.
57265112Seric **		ctladdr -- the controlling address for this file.
57365064Seric **		flags -- SFF_* flags to control the function.
5744329Seric **
5754329Seric **	Returns:
5764329Seric **		TRUE -- if we will be able to write this file.
5774329Seric **		FALSE -- if we cannot write this file.
5784329Seric **
5794329Seric **	Side Effects:
5804329Seric **		none.
5814329Seric */
5824329Seric 
5834329Seric bool
58465112Seric writable(filename, ctladdr, flags)
58564819Seric 	char *filename;
58665112Seric 	ADDRESS *ctladdr;
58765064Seric 	int flags;
5884329Seric {
58955372Seric 	uid_t euid;
59055372Seric 	gid_t egid;
5914329Seric 	int bits;
59264944Seric 	register char *p;
59364944Seric 	char *uname;
59464944Seric 	struct stat stb;
59564944Seric 	extern char RealUserName[];
5964329Seric 
59764819Seric 	if (tTd(29, 5))
59865064Seric 		printf("writable(%s, %x)\n", filename, flags);
59964944Seric 
60064944Seric #ifdef HASLSTAT
60165064Seric 	if ((bitset(SFF_NOSLINK, flags) ? lstat(filename, &stb)
60265064Seric 					: stat(filename, &stb)) < 0)
60364944Seric #else
60464944Seric 	if (stat(filename, &stb) < 0)
60564944Seric #endif
60664944Seric 	{
60764944Seric 		/* file does not exist -- see if directory is safe */
60864944Seric 		p = strrchr(filename, '/');
60964944Seric 		if (p == NULL)
61064944Seric 		{
61165067Seric 			errno = ENOTDIR;
61264944Seric 			return FALSE;
61364944Seric 		}
61465067Seric 		*p = '\0';
61565067Seric 		errno = safefile(filename, RealUid, RealGid, RealUserName,
61665067Seric 				 SFF_MUSTOWN, S_IWRITE|S_IEXEC);
61764944Seric 		*p = '/';
61865067Seric 		return errno == 0;
61964944Seric 	}
62064944Seric 
62165225Seric #ifdef SUID_ROOT_FILES_OK
62265225Seric 	/* really ought to be passed down -- and not a good idea */
62365225Seric 	flags |= SFF_ROOTOK;
62465225Seric #endif
62565225Seric 
62664944Seric 	/*
62764944Seric 	**  File does exist -- check that it is writable.
62864944Seric 	*/
62964944Seric 
63064944Seric 	if (bitset(0111, stb.st_mode))
63165022Seric 	{
63265022Seric 		if (tTd(29, 5))
63365022Seric 			printf("failed (mode %o: x bits)\n", stb.st_mode);
63465067Seric 		errno = EPERM;
6354329Seric 		return (FALSE);
63665022Seric 	}
63764944Seric 
63865112Seric 	if (ctladdr != NULL && geteuid() == 0)
63964944Seric 	{
64065112Seric 		euid = ctladdr->q_uid;
64165112Seric 		egid = ctladdr->q_gid;
64265112Seric 		uname = ctladdr->q_user;
64364944Seric 	}
64465112Seric 	else
64565112Seric 	{
64665112Seric 		euid = RealUid;
64765112Seric 		egid = RealGid;
64865112Seric 		uname = RealUserName;
64965112Seric 	}
65065138Seric 	if (euid == 0)
65165138Seric 	{
65265138Seric 		euid = DefUid;
65365138Seric 		uname = DefUser;
65465138Seric 	}
65565138Seric 	if (egid == 0)
65665138Seric 		egid = DefGid;
6574329Seric 	if (geteuid() == 0)
6584329Seric 	{
65965225Seric 		if (bitset(S_ISUID, stb.st_mode) &&
66065225Seric 		    (stb.st_uid != 0 || bitset(SFF_ROOTOK, flags)))
66164944Seric 		{
66264944Seric 			euid = stb.st_uid;
66364944Seric 			uname = NULL;
66464944Seric 		}
66565225Seric 		if (bitset(S_ISGID, stb.st_mode) &&
66665225Seric 		    (stb.st_gid != 0 || bitset(SFF_ROOTOK, flags)))
66764944Seric 			egid = stb.st_gid;
6684329Seric 	}
6694329Seric 
67064819Seric 	if (tTd(29, 5))
67164819Seric 		printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n",
67264944Seric 			euid, egid, stb.st_uid, stb.st_gid);
67364819Seric 
67465067Seric 	errno = safefile(filename, euid, egid, uname, flags, S_IWRITE);
67565067Seric 	return errno == 0;
6764329Seric }
6774329Seric /*
6784174Seric **  INCLUDE -- handle :include: specification.
6794174Seric **
6804174Seric **	Parameters:
6814174Seric **		fname -- filename to include.
68253037Seric **		forwarding -- if TRUE, we are reading a .forward file.
68353037Seric **			if FALSE, it's a :include: file.
6844399Seric **		ctladdr -- address template to use to fill in these
6854399Seric **			addresses -- effective user/group id are
6864399Seric **			the important things.
6875006Seric **		sendq -- a pointer to the head of the send queue
6885006Seric **			to put these addresses in.
6894174Seric **
6904174Seric **	Returns:
69157136Seric **		open error status
6924174Seric **
6934174Seric **	Side Effects:
6944174Seric **		reads the :include: file and sends to everyone
6954174Seric **		listed in that file.
6964174Seric */
6974174Seric 
69853037Seric static jmp_buf	CtxIncludeTimeout;
69963937Seric static int	includetimeout();
70053037Seric 
70165496Seric #ifndef S_IWOTH
70265496Seric # define S_IWOTH	(S_IWRITE >> 6)
70365496Seric #endif
70465496Seric 
70557136Seric int
70655012Seric include(fname, forwarding, ctladdr, sendq, e)
7074174Seric 	char *fname;
70853037Seric 	bool forwarding;
7094399Seric 	ADDRESS *ctladdr;
7105006Seric 	ADDRESS **sendq;
71155012Seric 	ENVELOPE *e;
7124174Seric {
71364570Seric 	register FILE *fp = NULL;
71455012Seric 	char *oldto = e->e_to;
7159379Seric 	char *oldfilename = FileName;
7169379Seric 	int oldlinenumber = LineNumber;
71753037Seric 	register EVENT *ev = NULL;
71858082Seric 	int nincludes;
71964325Seric 	register ADDRESS *ca;
72064325Seric 	uid_t saveduid, uid;
72164325Seric 	gid_t savedgid, gid;
72264083Seric 	char *uname;
72364325Seric 	int rval = 0;
72465064Seric 	int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE;
72565496Seric 	struct stat st;
72653037Seric 	char buf[MAXLINE];
7274174Seric 
72857186Seric 	if (tTd(27, 2))
72957186Seric 		printf("include(%s)\n", fname);
73063902Seric 	if (tTd(27, 4))
73163902Seric 		printf("   ruid=%d euid=%d\n", getuid(), geteuid());
73263581Seric 	if (tTd(27, 14))
73363581Seric 	{
73463581Seric 		printf("ctladdr ");
73563581Seric 		printaddr(ctladdr, FALSE);
73663581Seric 	}
73757186Seric 
73864325Seric 	if (tTd(27, 9))
73964325Seric 		printf("include: old uid = %d/%d\n", getuid(), geteuid());
74053037Seric 
74163581Seric 	ca = getctladdr(ctladdr);
74263581Seric 	if (ca == NULL)
74364083Seric 	{
74464846Seric 		uid = DefUid;
74564846Seric 		gid = DefGid;
74664846Seric 		uname = DefUser;
74764325Seric 		saveduid = -1;
74864083Seric 	}
74963581Seric 	else
75064083Seric 	{
75163581Seric 		uid = ca->q_uid;
75264083Seric 		gid = ca->q_gid;
75364083Seric 		uname = ca->q_user;
75464325Seric #ifdef HASSETREUID
75564325Seric 		saveduid = geteuid();
75664325Seric 		savedgid = getegid();
75764325Seric 		if (saveduid == 0)
75864325Seric 		{
75964325Seric 			initgroups(uname, gid);
76064325Seric 			if (uid != 0)
76164325Seric 				(void) setreuid(0, uid);
76264325Seric 		}
76364325Seric #endif
76464083Seric 	}
76563581Seric 
76664325Seric 	if (tTd(27, 9))
76764325Seric 		printf("include: new uid = %d/%d\n", getuid(), geteuid());
76864325Seric 
76964325Seric 	/*
77064325Seric 	**  If home directory is remote mounted but server is down,
77164325Seric 	**  this can hang or give errors; use a timeout to avoid this
77264325Seric 	*/
77364325Seric 
77453037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
77553037Seric 	{
77663853Seric 		ctladdr->q_flags |= QQUEUEUP;
77753037Seric 		errno = 0;
77863993Seric 
77963993Seric 		/* return pseudo-error code */
78064325Seric 		rval = EOPENTIMEOUT;
78164325Seric 		goto resetuid;
78253037Seric 	}
78353037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
78453037Seric 
78563581Seric 	/* the input file must be marked safe */
78664944Seric 	rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD);
78764329Seric 	if (rval != 0)
78853037Seric 	{
78964325Seric 		/* don't use this :include: file */
79057186Seric 		if (tTd(27, 4))
79158247Seric 			printf("include: not safe (uid=%d): %s\n",
79264329Seric 				uid, errstring(rval));
79353037Seric 	}
79465496Seric 	else
7954174Seric 	{
79665496Seric 		fp = fopen(fname, "r");
79765496Seric 		if (fp == NULL)
79858061Seric 		{
79964329Seric 			rval = errno;
80065496Seric 			if (tTd(27, 4))
80165496Seric 				printf("include: open: %s\n", errstring(rval));
80258061Seric 		}
8034406Seric 	}
80453037Seric 	clrevent(ev);
80553037Seric 
80664570Seric resetuid:
80764570Seric 
80864570Seric #ifdef HASSETREUID
80964570Seric 	if (saveduid == 0)
81064570Seric 	{
81164570Seric 		if (uid != 0)
81264570Seric 			if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0)
81364570Seric 				syserr("setreuid(%d, 0) failure (real=%d, eff=%d)",
81464570Seric 					RealUid, getuid(), geteuid());
81564570Seric 		setgid(savedgid);
81664570Seric 	}
81764570Seric #endif
81864570Seric 
81964570Seric 	if (tTd(27, 9))
82064570Seric 		printf("include: reset uid = %d/%d\n", getuid(), geteuid());
82164570Seric 
822*65593Seric 	if (rval == EOPENTIMEOUT)
823*65593Seric 		usrerr("451 open timeout on %s", fname);
824*65593Seric 
82564570Seric 	if (fp == NULL)
82664570Seric 		return rval;
82764570Seric 
82865496Seric 	if (fstat(fileno(fp), &st) < 0)
82965496Seric 	{
83065496Seric 		rval = errno;
83165496Seric 		syserr("Cannot fstat %s!", fname);
83265496Seric 		return rval;
83365496Seric 	}
83465496Seric 
83565496Seric 	if (ca == NULL)
83665496Seric 	{
83765496Seric 		ctladdr->q_uid = st.st_uid;
83865496Seric 		ctladdr->q_gid = st.st_gid;
83965496Seric 		ctladdr->q_flags |= QGOODUID;
84065496Seric 	}
84165496Seric 	if (ca != NULL && ca->q_uid == st.st_uid)
84265496Seric 	{
84365496Seric 		/* optimization -- avoid getpwuid if we already have info */
84465496Seric 		ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL;
84565496Seric 		ctladdr->q_ruser = ca->q_ruser;
84665496Seric 	}
84765496Seric 	else
84865496Seric 	{
84965496Seric 		register struct passwd *pw;
85065496Seric 
85165496Seric 		pw = getpwuid(st.st_uid);
85265496Seric 		if (pw == NULL || !usershellok(pw->pw_shell))
85365496Seric 		{
85465496Seric 			ctladdr->q_ruser = newstr(pw->pw_name);
85565496Seric 			ctladdr->q_flags |= QBOGUSSHELL;
85665496Seric 		}
85765496Seric 	}
85865496Seric 
85958092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
86058092Seric 	{
86158092Seric 		/* don't do any more now */
86258868Seric 		ctladdr->q_flags |= QVERIFIED;
86358884Seric 		e->e_nrcpts++;
86458680Seric 		xfclose(fp, "include", fname);
86564570Seric 		return rval;
86658092Seric 	}
86758092Seric 
86865496Seric 	/*
86965496Seric 	** Check to see if some bad guy can write this file
87065496Seric 	**
87165496Seric 	**	This should really do something clever with group
87265496Seric 	**	permissions; currently we just view world writable
87365496Seric 	**	as unsafe.  Also, we don't check for writable
87465496Seric 	**	directories in the path.  We've got to leave
87565496Seric 	**	something for the local sysad to do.
87665496Seric 	*/
87765496Seric 
87865496Seric 	if (bitset(S_IWOTH, st.st_mode))
87965496Seric 		ctladdr->q_flags |= QUNSAFEADDR;
88065496Seric 
8814174Seric 	/* read the file -- each line is a comma-separated list. */
8829379Seric 	FileName = fname;
8839379Seric 	LineNumber = 0;
88458082Seric 	ctladdr->q_flags &= ~QSELFREF;
88558082Seric 	nincludes = 0;
8864174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
8874174Seric 	{
88856795Seric 		register char *p = strchr(buf, '\n');
8894174Seric 
89040963Sbostic 		LineNumber++;
8914174Seric 		if (p != NULL)
8924174Seric 			*p = '\0';
89357186Seric 		if (buf[0] == '#' || buf[0] == '\0')
89457139Seric 			continue;
89558008Seric 		e->e_to = NULL;
89658151Seric 		message("%s to %s",
89753037Seric 			forwarding ? "forwarding" : "sending", buf);
89857977Seric #ifdef LOG
89958020Seric 		if (forwarding && LogLevel > 9)
90057977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
90157977Seric 				e->e_id, oldto, buf);
90257977Seric #endif
90357977Seric 
9044176Seric 		AliasLevel++;
90558082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
9064176Seric 		AliasLevel--;
9074174Seric 	}
90863902Seric 
90963902Seric 	if (ferror(fp) && tTd(27, 3))
91063902Seric 		printf("include: read error: %s\n", errstring(errno));
91158082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
91258065Seric 	{
91358065Seric 		if (tTd(27, 5))
91458065Seric 		{
91558065Seric 			printf("include: QDONTSEND ");
91658065Seric 			printaddr(ctladdr, FALSE);
91758065Seric 		}
91858065Seric 		ctladdr->q_flags |= QDONTSEND;
91958065Seric 	}
9204174Seric 
92158680Seric 	(void) xfclose(fp, "include", fname);
9229379Seric 	FileName = oldfilename;
9239379Seric 	LineNumber = oldlinenumber;
92463847Seric 	e->e_to = oldto;
92564325Seric 	return rval;
9264174Seric }
92753037Seric 
92853037Seric static
92953037Seric includetimeout()
93053037Seric {
93153037Seric 	longjmp(CtxIncludeTimeout, 1);
93253037Seric }
9334324Seric /*
9344324Seric **  SENDTOARGV -- send to an argument vector.
9354324Seric **
9364324Seric **	Parameters:
9374324Seric **		argv -- argument vector to send to.
93858247Seric **		e -- the current envelope.
9394324Seric **
9404324Seric **	Returns:
9414324Seric **		none.
9424324Seric **
9434324Seric **	Side Effects:
9444324Seric **		puts all addresses on the argument vector onto the
9454324Seric **			send queue.
9464324Seric */
9474324Seric 
94855012Seric sendtoargv(argv, e)
9494324Seric 	register char **argv;
95055012Seric 	register ENVELOPE *e;
9514324Seric {
9524324Seric 	register char *p;
9534324Seric 
9544324Seric 	while ((p = *argv++) != NULL)
9554324Seric 	{
95664284Seric 		(void) sendtolist(p, NULLADDR, &e->e_sendqueue, e);
9574324Seric 	}
9584324Seric }
9594399Seric /*
9604399Seric **  GETCTLADDR -- get controlling address from an address header.
9614399Seric **
9624399Seric **	If none, get one corresponding to the effective userid.
9634399Seric **
9644399Seric **	Parameters:
9654399Seric **		a -- the address to find the controller of.
9664399Seric **
9674399Seric **	Returns:
9684399Seric **		the controlling address.
9694399Seric **
9704399Seric **	Side Effects:
9714399Seric **		none.
9724399Seric */
9734399Seric 
9744399Seric ADDRESS *
9754399Seric getctladdr(a)
9764399Seric 	register ADDRESS *a;
9774399Seric {
9784404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
9794399Seric 		a = a->q_alias;
9804399Seric 	return (a);
9814399Seric }
982