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*67711Seric static char sccsid[] = "@(#)recipient.c	8.47 (Berkeley) 08/20/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;
15467264Seric 	int i;
15567264Seric 	char *buf;
15667264Seric 	char buf0[MAXNAME];		/* unquoted image of the user name */
15758247Seric 	extern int safefile();
1584174Seric 
15955012Seric 	e->e_to = a->q_paddr;
1604600Seric 	m = a->q_mailer;
1614174Seric 	errno = 0;
1627676Seric 	if (tTd(26, 1))
1634444Seric 	{
1644444Seric 		printf("\nrecipient: ");
1654444Seric 		printaddr(a, FALSE);
1664444Seric 	}
1674174Seric 
16864146Seric 	/* if this is primary, add it to the original recipient list */
16964146Seric 	if (a->q_alias == NULL)
17064146Seric 	{
17164146Seric 		if (e->e_origrcpt == NULL)
17264146Seric 			e->e_origrcpt = a->q_paddr;
17364146Seric 		else if (e->e_origrcpt != a->q_paddr)
17464146Seric 			e->e_origrcpt = "";
17564146Seric 	}
17664146Seric 
1774174Seric 	/* break aliasing loops */
1784174Seric 	if (AliasLevel > MAXRCRSN)
1794174Seric 	{
18058151Seric 		usrerr("554 aliasing/forwarding loop broken");
18112613Seric 		return (a);
1824174Seric 	}
1834174Seric 
1844174Seric 	/*
1854627Seric 	**  Finish setting up address structure.
1864174Seric 	*/
1874174Seric 
18816160Seric 	/* set the queue timeout */
18958737Seric 	a->q_timeout = TimeOuts.to_q_return;
1904627Seric 
19116160Seric 	/* get unquoted user for file, program or user.name check */
19267264Seric 	i = strlen(a->q_user);
19367264Seric 	if (i >= sizeof buf)
19467264Seric 		buf = xalloc(i + 1);
19567264Seric 	else
19667264Seric 		buf = buf0;
1979210Seric 	(void) strcpy(buf, a->q_user);
1989210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1999210Seric 	{
20054993Seric 		if (*p == '\\')
2019210Seric 			quoted = TRUE;
2029210Seric 	}
20354983Seric 	stripquotes(buf);
2049210Seric 
20557402Seric 	/* check for direct mailing to restricted mailers */
20665496Seric 	if (m == ProgMailer)
2074174Seric 	{
20865496Seric 		if (a->q_alias == NULL)
20965496Seric 		{
21065496Seric 			a->q_flags |= QBADADDR;
21165496Seric 			usrerr("550 Cannot mail directly to programs");
21265496Seric 		}
21365496Seric 		else if (bitset(QBOGUSSHELL, a->q_alias->q_flags))
21465496Seric 		{
21565496Seric 			a->q_flags |= QBADADDR;
21665496Seric 			usrerr("550 User %s@%s doesn't have a valid shell for mailing to programs",
21765496Seric 				a->q_alias->q_ruser, MyHostName);
21865496Seric 		}
21965496Seric 		else if (bitset(QUNSAFEADDR, a->q_alias->q_flags))
22065496Seric 		{
22165496Seric 			a->q_flags |= QBADADDR;
22265496Seric 			usrerr("550 Address %s is unsafe for mailing to programs",
22365496Seric 				a->q_alias->q_paddr);
22465496Seric 		}
2254174Seric 	}
2264174Seric 
2274174Seric 	/*
2284419Seric 	**  Look up this person in the recipient list.
2294419Seric 	**	If they are there already, return, otherwise continue.
2304419Seric 	**	If the list is empty, just add it.  Notice the cute
2314419Seric 	**	hack to make from addresses suppress things correctly:
2324419Seric 	**	the QDONTSEND bit will be set in the send list.
2334419Seric 	**	[Please note: the emphasis is on "hack."]
2344174Seric 	*/
2354174Seric 
2365006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2374174Seric 	{
23858294Seric 		if (sameaddr(q, a))
2394174Seric 		{
2407676Seric 			if (tTd(26, 1))
2414444Seric 			{
2424444Seric 				printf("%s in sendq: ", a->q_paddr);
2434444Seric 				printaddr(q, FALSE);
2444444Seric 			}
24565593Seric 			if (!bitset(QPRIMARY, q->q_flags))
24658065Seric 			{
24765593Seric 				if (!bitset(QDONTSEND, a->q_flags))
24858151Seric 					message("duplicate suppressed");
24965593Seric 				q->q_flags |= a->q_flags;
25065593Seric 			}
25165593Seric 			else if (bitset(QSELFREF, q->q_flags))
25265579Seric 				q->q_flags |= a->q_flags & ~QDONTSEND;
25363847Seric 			a = q;
25463847Seric 			goto testselfdestruct;
2554174Seric 		}
2564319Seric 	}
2574174Seric 
2584319Seric 	/* add address on list */
25958884Seric 	*pq = a;
26058884Seric 	a->q_next = NULL;
2614174Seric 
2624174Seric 	/*
26357402Seric 	**  Alias the name and handle special mailer types.
2644174Seric 	*/
2654174Seric 
26653735Seric   trylocaluser:
26755354Seric 	if (tTd(29, 7))
26855354Seric 		printf("at trylocaluser %s\n", a->q_user);
26955354Seric 
27058680Seric 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
27163847Seric 		goto testselfdestruct;
27257402Seric 
27357402Seric 	if (m == InclMailer)
2744174Seric 	{
27557402Seric 		a->q_flags |= QDONTSEND;
27664761Seric 		if (a->q_alias == NULL)
2774174Seric 		{
27858680Seric 			a->q_flags |= QBADADDR;
27958151Seric 			usrerr("550 Cannot mail directly to :include:s");
2804174Seric 		}
2814174Seric 		else
28250556Seric 		{
28359563Seric 			int ret;
28458247Seric 
28558151Seric 			message("including file %s", a->q_user);
28659563Seric 			ret = include(a->q_user, FALSE, a, sendq, e);
28759563Seric 			if (transienterror(ret))
28859563Seric 			{
28959563Seric #ifdef LOG
29059563Seric 				if (LogLevel > 2)
29166239Seric 					syslog(LOG_ERR, "%s: include %s: transient error: %s",
29266284Seric 						e->e_id == NULL ? "NOQUEUE" : e->e_id,
29366284Seric 						a->q_user, errstring(ret));
29459563Seric #endif
29563853Seric 				a->q_flags |= QQUEUEUP;
29665215Seric 				a->q_flags &= ~QDONTSEND;
29759563Seric 				usrerr("451 Cannot open %s: %s",
29859563Seric 					a->q_user, errstring(ret));
29959563Seric 			}
30059563Seric 			else if (ret != 0)
30159563Seric 			{
30263938Seric 				a->q_flags |= QBADADDR;
30359563Seric 				usrerr("550 Cannot open %s: %s",
30459563Seric 					a->q_user, errstring(ret));
30559563Seric 			}
30650556Seric 		}
3074174Seric 	}
30857642Seric 	else if (m == FileMailer)
3094174Seric 	{
3104329Seric 		extern bool writable();
3114174Seric 
31251317Seric 		/* check if writable or creatable */
31364761Seric 		if (a->q_alias == NULL)
3144174Seric 		{
31558680Seric 			a->q_flags |= QBADADDR;
31658151Seric 			usrerr("550 Cannot mail directly to files");
3174174Seric 		}
31865496Seric 		else if (bitset(QBOGUSSHELL, a->q_alias->q_flags))
31965496Seric 		{
32065496Seric 			a->q_flags |= QBADADDR;
32165496Seric 			usrerr("550 User %s@%s doesn't have a valid shell for mailing to files",
32265496Seric 				a->q_alias->q_ruser, MyHostName);
32365496Seric 		}
32465496Seric 		else if (bitset(QUNSAFEADDR, a->q_alias->q_flags))
32565496Seric 		{
32665496Seric 			a->q_flags |= QBADADDR;
32765496Seric 			usrerr("550 Address %s is unsafe for mailing to files",
32865496Seric 				a->q_alias->q_paddr);
32965496Seric 		}
33065112Seric 		else if (!writable(buf, getctladdr(a), SFF_ANYFILE))
33151317Seric 		{
33258680Seric 			a->q_flags |= QBADADDR;
33364771Seric 			giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e);
33451317Seric 		}
33551317Seric 	}
33651317Seric 
33757402Seric 	/* try aliasing */
33867472Seric 	if (!bitset(QDONTSEND, a->q_flags) && bitnset(M_ALIASABLE, m->m_flags))
33967472Seric 		alias(a, sendq, e);
34057402Seric 
34157402Seric # ifdef USERDB
34257402Seric 	/* if not aliased, look it up in the user database */
34367472Seric 	if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags) &&
34467472Seric 	    bitnset(M_CHECKUDB, m->m_flags))
34557402Seric 	{
34657402Seric 		extern int udbexpand();
34757402Seric 
34857402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
34957402Seric 		{
35063853Seric 			a->q_flags |= QQUEUEUP;
35157402Seric 			if (e->e_message == NULL)
35257402Seric 				e->e_message = newstr("Deferred: user database error");
35357402Seric # ifdef LOG
35458020Seric 			if (LogLevel > 8)
35559623Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand: %s",
35666284Seric 					e->e_id == NULL ? "NOQUEUE" : e->e_id,
35766284Seric 					errstring(errno));
35857402Seric # endif
35959615Seric 			message("queued (user database error): %s",
36059615Seric 				errstring(errno));
36157642Seric 			e->e_nrcpts++;
36263847Seric 			goto testselfdestruct;
36357402Seric 		}
36457402Seric 	}
36557402Seric # endif
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 	}
38067472Seric 	if (!bitset(QNOTREMOTE|QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags) &&
38167472Seric 	    ConfigLevel >= 2 && RewriteRules[5] != NULL &&
38267472Seric 	    bitnset(M_TRYRULESET5, m->m_flags))
38351317Seric 	{
38455012Seric 		maplocaluser(a, sendq, e);
38551317Seric 	}
38651317Seric 
38751317Seric 	/*
38851317Seric 	**  If it didn't get rewritten to another mailer, go ahead
38951317Seric 	**  and deliver it.
39051317Seric 	*/
39151317Seric 
39267472Seric 	if (!bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags) &&
39367472Seric 	    bitnset(M_HASPWENT, m->m_flags))
39451317Seric 	{
39555354Seric 		auto bool fuzzy;
39651317Seric 		register struct passwd *pw;
39751317Seric 		extern struct passwd *finduser();
39851317Seric 
39951317Seric 		/* warning -- finduser may trash buf */
40055354Seric 		pw = finduser(buf, &fuzzy);
40151317Seric 		if (pw == NULL)
40251317Seric 		{
40358680Seric 			a->q_flags |= QBADADDR;
40464771Seric 			giveresponse(EX_NOUSER, m, NULL, a->q_alias, e);
40551317Seric 		}
4064174Seric 		else
4074174Seric 		{
40851317Seric 			char nbuf[MAXNAME];
4094373Seric 
41055354Seric 			if (fuzzy)
4114174Seric 			{
41253735Seric 				/* name was a fuzzy match */
41351317Seric 				a->q_user = newstr(pw->pw_name);
41453735Seric 				if (findusercount++ > 3)
41553735Seric 				{
41658680Seric 					a->q_flags |= QBADADDR;
41758151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
41853735Seric 						pw->pw_name);
41967264Seric 					goto done;
42053735Seric 				}
42153735Seric 
42253735Seric 				/* see if it aliases */
42351317Seric 				(void) strcpy(buf, pw->pw_name);
42453735Seric 				goto trylocaluser;
4254174Seric 			}
42665822Seric 			if (strcmp(pw->pw_dir, "/") == 0)
42765822Seric 				a->q_home = "";
42865822Seric 			else
42965822Seric 				a->q_home = newstr(pw->pw_dir);
43051317Seric 			a->q_uid = pw->pw_uid;
43151317Seric 			a->q_gid = pw->pw_gid;
43259083Seric 			a->q_ruser = newstr(pw->pw_name);
43351317Seric 			a->q_flags |= QGOODUID;
43451317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
43551317Seric 			if (nbuf[0] != '\0')
43651317Seric 				a->q_fullname = newstr(nbuf);
43765211Seric 			if (pw->pw_shell != NULL && pw->pw_shell[0] != '\0' &&
43865211Seric 			    !usershellok(pw->pw_shell))
43965206Seric 			{
44065211Seric 				a->q_flags |= QBOGUSSHELL;
44165206Seric 			}
44251317Seric 			if (!quoted)
44355012Seric 				forward(a, sendq, e);
4444174Seric 		}
4454174Seric 	}
44657642Seric 	if (!bitset(QDONTSEND, a->q_flags))
44757642Seric 		e->e_nrcpts++;
44863847Seric 
44963847Seric   testselfdestruct:
45063978Seric 	if (tTd(26, 8))
45163847Seric 	{
45263978Seric 		printf("testselfdestruct: ");
45363978Seric 		printaddr(a, TRUE);
45463978Seric 	}
45563978Seric 	if (a->q_alias == NULL && a != &e->e_from &&
45663978Seric 	    bitset(QDONTSEND, a->q_flags))
45763978Seric 	{
45863978Seric 		q = *sendq;
45963965Seric 		while (q != NULL && bitset(QDONTSEND, q->q_flags))
46063847Seric 			q = q->q_next;
46163978Seric 		if (q == NULL)
46263847Seric 		{
46363847Seric 			a->q_flags |= QBADADDR;
46463847Seric 			usrerr("554 aliasing/forwarding loop broken");
46563847Seric 		}
46663847Seric 	}
46767264Seric 
46867264Seric   done:
46967264Seric 	if (buf != buf0)
47067264Seric 		free(buf);
47112613Seric 	return (a);
4724174Seric }
4734174Seric /*
4744373Seric **  FINDUSER -- find the password entry for a user.
4754373Seric **
4764373Seric **	This looks a lot like getpwnam, except that it may want to
4774373Seric **	do some fancier pattern matching in /etc/passwd.
4784373Seric **
4799379Seric **	This routine contains most of the time of many sendmail runs.
4809379Seric **	It deserves to be optimized.
4819379Seric **
4824373Seric **	Parameters:
4834373Seric **		name -- the name to match against.
48455354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
48555354Seric **			was found using the fuzzy matching algorithm;
48655354Seric **			set to FALSE otherwise.
4874373Seric **
4884373Seric **	Returns:
4894373Seric **		A pointer to a pw struct.
4904373Seric **		NULL if name is unknown or ambiguous.
4914373Seric **
4924373Seric **	Side Effects:
4934407Seric **		may modify name.
4944373Seric */
4954373Seric 
4964373Seric struct passwd *
49755354Seric finduser(name, fuzzyp)
4984373Seric 	char *name;
49955354Seric 	bool *fuzzyp;
5004373Seric {
5014376Seric 	register struct passwd *pw;
5024407Seric 	register char *p;
50315325Seric 	extern struct passwd *getpwent();
50415325Seric 	extern struct passwd *getpwnam();
5054373Seric 
50655354Seric 	if (tTd(29, 4))
50755354Seric 		printf("finduser(%s): ", name);
50855354Seric 
50955354Seric 	*fuzzyp = FALSE;
5104407Seric 
51164673Seric 	/* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */
51264673Seric 	for (p = name; *p != '\0'; p++)
51364673Seric 		if (!isascii(*p) || !isdigit(*p))
51464673Seric 			break;
51564673Seric 	if (*p == '\0')
51664673Seric 	{
51764673Seric 		if (tTd(29, 4))
51864673Seric 			printf("failed (numeric input)\n");
51964673Seric 		return NULL;
52064673Seric 	}
52164673Seric 
52225777Seric 	/* look up this login name using fast path */
52312634Seric 	if ((pw = getpwnam(name)) != NULL)
52455354Seric 	{
52555354Seric 		if (tTd(29, 4))
52655354Seric 			printf("found (non-fuzzy)\n");
52712634Seric 		return (pw);
52855354Seric 	}
52912634Seric 
53053735Seric #ifdef MATCHGECOS
53153735Seric 	/* see if fuzzy matching allowed */
53253735Seric 	if (!MatchGecos)
53355354Seric 	{
53455354Seric 		if (tTd(29, 4))
53555354Seric 			printf("not found (fuzzy disabled)\n");
53653735Seric 		return NULL;
53755354Seric 	}
53853735Seric 
53912634Seric 	/* search for a matching full name instead */
54025777Seric 	for (p = name; *p != '\0'; p++)
54125777Seric 	{
54225777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
54325777Seric 			*p = ' ';
54425777Seric 	}
54523107Seric 	(void) setpwent();
5464376Seric 	while ((pw = getpwent()) != NULL)
5474376Seric 	{
5484998Seric 		char buf[MAXNAME];
5494376Seric 
5504998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
55156795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
5524381Seric 		{
55355354Seric 			if (tTd(29, 4))
55455354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
55558151Seric 			message("sending to login name %s", pw->pw_name);
55655354Seric 			*fuzzyp = TRUE;
5574376Seric 			return (pw);
5584377Seric 		}
5594376Seric 	}
56055354Seric 	if (tTd(29, 4))
56155354Seric 		printf("no fuzzy match found\n");
56259015Seric #else
56359015Seric 	if (tTd(29, 4))
56459015Seric 		printf("not found (fuzzy disabled)\n");
56559015Seric #endif
5664376Seric 	return (NULL);
5674373Seric }
5684373Seric /*
5694329Seric **  WRITABLE -- predicate returning if the file is writable.
5704329Seric **
5714329Seric **	This routine must duplicate the algorithm in sys/fio.c.
5724329Seric **	Unfortunately, we cannot use the access call since we
5734329Seric **	won't necessarily be the real uid when we try to
5744329Seric **	actually open the file.
5754329Seric **
5764329Seric **	Notice that ANY file with ANY execute bit is automatically
5774329Seric **	not writable.  This is also enforced by mailfile.
5784329Seric **
5794329Seric **	Parameters:
58065064Seric **		filename -- the file name to check.
58165112Seric **		ctladdr -- the controlling address for this file.
58265064Seric **		flags -- SFF_* flags to control the function.
5834329Seric **
5844329Seric **	Returns:
5854329Seric **		TRUE -- if we will be able to write this file.
5864329Seric **		FALSE -- if we cannot write this file.
5874329Seric **
5884329Seric **	Side Effects:
5894329Seric **		none.
5904329Seric */
5914329Seric 
5924329Seric bool
59365112Seric writable(filename, ctladdr, flags)
59464819Seric 	char *filename;
59565112Seric 	ADDRESS *ctladdr;
59665064Seric 	int flags;
5974329Seric {
59855372Seric 	uid_t euid;
59955372Seric 	gid_t egid;
6004329Seric 	int bits;
60164944Seric 	register char *p;
60264944Seric 	char *uname;
60364944Seric 	struct stat stb;
60464944Seric 	extern char RealUserName[];
6054329Seric 
60664819Seric 	if (tTd(29, 5))
60765064Seric 		printf("writable(%s, %x)\n", filename, flags);
60864944Seric 
60964944Seric #ifdef HASLSTAT
61065064Seric 	if ((bitset(SFF_NOSLINK, flags) ? lstat(filename, &stb)
61165064Seric 					: stat(filename, &stb)) < 0)
61264944Seric #else
61364944Seric 	if (stat(filename, &stb) < 0)
61464944Seric #endif
61564944Seric 	{
61664944Seric 		/* file does not exist -- see if directory is safe */
61764944Seric 		p = strrchr(filename, '/');
61864944Seric 		if (p == NULL)
61964944Seric 		{
62065067Seric 			errno = ENOTDIR;
62164944Seric 			return FALSE;
62264944Seric 		}
62365067Seric 		*p = '\0';
62465067Seric 		errno = safefile(filename, RealUid, RealGid, RealUserName,
62565067Seric 				 SFF_MUSTOWN, S_IWRITE|S_IEXEC);
62664944Seric 		*p = '/';
62765067Seric 		return errno == 0;
62864944Seric 	}
62964944Seric 
63065225Seric #ifdef SUID_ROOT_FILES_OK
63165225Seric 	/* really ought to be passed down -- and not a good idea */
63265225Seric 	flags |= SFF_ROOTOK;
63365225Seric #endif
63465225Seric 
63564944Seric 	/*
63664944Seric 	**  File does exist -- check that it is writable.
63764944Seric 	*/
63864944Seric 
63964944Seric 	if (bitset(0111, stb.st_mode))
64065022Seric 	{
64165022Seric 		if (tTd(29, 5))
64265022Seric 			printf("failed (mode %o: x bits)\n", stb.st_mode);
64365067Seric 		errno = EPERM;
6444329Seric 		return (FALSE);
64565022Seric 	}
64664944Seric 
64765112Seric 	if (ctladdr != NULL && geteuid() == 0)
64864944Seric 	{
64965112Seric 		euid = ctladdr->q_uid;
65065112Seric 		egid = ctladdr->q_gid;
65165112Seric 		uname = ctladdr->q_user;
65264944Seric 	}
65365112Seric 	else
65465112Seric 	{
65565112Seric 		euid = RealUid;
65665112Seric 		egid = RealGid;
65765112Seric 		uname = RealUserName;
65865112Seric 	}
65965138Seric 	if (euid == 0)
66065138Seric 	{
66165138Seric 		euid = DefUid;
66265138Seric 		uname = DefUser;
66365138Seric 	}
66465138Seric 	if (egid == 0)
66565138Seric 		egid = DefGid;
6664329Seric 	if (geteuid() == 0)
6674329Seric 	{
66865225Seric 		if (bitset(S_ISUID, stb.st_mode) &&
66965225Seric 		    (stb.st_uid != 0 || bitset(SFF_ROOTOK, flags)))
67064944Seric 		{
67164944Seric 			euid = stb.st_uid;
67264944Seric 			uname = NULL;
67364944Seric 		}
67465225Seric 		if (bitset(S_ISGID, stb.st_mode) &&
67565225Seric 		    (stb.st_gid != 0 || bitset(SFF_ROOTOK, flags)))
67664944Seric 			egid = stb.st_gid;
6774329Seric 	}
6784329Seric 
67964819Seric 	if (tTd(29, 5))
68064819Seric 		printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n",
68164944Seric 			euid, egid, stb.st_uid, stb.st_gid);
68264819Seric 
68365067Seric 	errno = safefile(filename, euid, egid, uname, flags, S_IWRITE);
68465067Seric 	return errno == 0;
6854329Seric }
6864329Seric /*
6874174Seric **  INCLUDE -- handle :include: specification.
6884174Seric **
6894174Seric **	Parameters:
6904174Seric **		fname -- filename to include.
69153037Seric **		forwarding -- if TRUE, we are reading a .forward file.
69253037Seric **			if FALSE, it's a :include: file.
6934399Seric **		ctladdr -- address template to use to fill in these
6944399Seric **			addresses -- effective user/group id are
6954399Seric **			the important things.
6965006Seric **		sendq -- a pointer to the head of the send queue
6975006Seric **			to put these addresses in.
6984174Seric **
6994174Seric **	Returns:
70057136Seric **		open error status
7014174Seric **
7024174Seric **	Side Effects:
7034174Seric **		reads the :include: file and sends to everyone
7044174Seric **		listed in that file.
70565909Seric **
70665909Seric **	Security Note:
70765909Seric **		If you have restricted chown (that is, you can't
70865909Seric **		give a file away), it is reasonable to allow programs
70965909Seric **		and files called from this :include: file to be to be
71065909Seric **		run as the owner of the :include: file.  This is bogus
71165909Seric **		if there is any chance of someone giving away a file.
71265909Seric **		We assume that pre-POSIX systems can give away files.
71365909Seric **
71465909Seric **		There is an additional restriction that if you
71565909Seric **		forward to a :include: file, it will not take on
71665909Seric **		the ownership of the :include: file.  This may not
71765909Seric **		be necessary, but shouldn't hurt.
7184174Seric */
7194174Seric 
72053037Seric static jmp_buf	CtxIncludeTimeout;
72163937Seric static int	includetimeout();
72253037Seric 
72365496Seric #ifndef S_IWOTH
72465496Seric # define S_IWOTH	(S_IWRITE >> 6)
72565496Seric #endif
72665496Seric 
72757136Seric int
72855012Seric include(fname, forwarding, ctladdr, sendq, e)
7294174Seric 	char *fname;
73053037Seric 	bool forwarding;
7314399Seric 	ADDRESS *ctladdr;
7325006Seric 	ADDRESS **sendq;
73355012Seric 	ENVELOPE *e;
7344174Seric {
73564570Seric 	register FILE *fp = NULL;
73655012Seric 	char *oldto = e->e_to;
7379379Seric 	char *oldfilename = FileName;
7389379Seric 	int oldlinenumber = LineNumber;
73953037Seric 	register EVENT *ev = NULL;
74058082Seric 	int nincludes;
74164325Seric 	register ADDRESS *ca;
74264325Seric 	uid_t saveduid, uid;
74364325Seric 	gid_t savedgid, gid;
74464083Seric 	char *uname;
74564325Seric 	int rval = 0;
74665064Seric 	int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE;
74765496Seric 	struct stat st;
74865948Seric 	char buf[MAXLINE];
74965909Seric #ifdef _POSIX_CHOWN_RESTRICTED
75065948Seric # if _POSIX_CHOWN_RESTRICTED == -1
75165948Seric #  define safechown	FALSE
75265948Seric # else
75365948Seric #  define safechown	TRUE
75465948Seric # endif
75565948Seric #else
75665948Seric # ifdef _PC_CHOWN_RESTRICTED
75765909Seric 	bool safechown;
75865948Seric # else
75965948Seric #  ifdef BSD
76065948Seric #   define safechown	TRUE
76165948Seric #  else
76265948Seric #   define safechown	FALSE
76365948Seric #  endif
76465948Seric # endif
76565909Seric #endif
76665948Seric 	extern bool chownsafe();
7674174Seric 
76857186Seric 	if (tTd(27, 2))
76957186Seric 		printf("include(%s)\n", fname);
77063902Seric 	if (tTd(27, 4))
77163902Seric 		printf("   ruid=%d euid=%d\n", getuid(), geteuid());
77263581Seric 	if (tTd(27, 14))
77363581Seric 	{
77463581Seric 		printf("ctladdr ");
77563581Seric 		printaddr(ctladdr, FALSE);
77663581Seric 	}
77757186Seric 
77864325Seric 	if (tTd(27, 9))
77964325Seric 		printf("include: old uid = %d/%d\n", getuid(), geteuid());
78053037Seric 
78163581Seric 	ca = getctladdr(ctladdr);
78263581Seric 	if (ca == NULL)
78364083Seric 	{
78464846Seric 		uid = DefUid;
78564846Seric 		gid = DefGid;
78664846Seric 		uname = DefUser;
78764325Seric 		saveduid = -1;
78864083Seric 	}
78963581Seric 	else
79064083Seric 	{
79163581Seric 		uid = ca->q_uid;
79264083Seric 		gid = ca->q_gid;
79364083Seric 		uname = ca->q_user;
79464325Seric #ifdef HASSETREUID
79564325Seric 		saveduid = geteuid();
79664325Seric 		savedgid = getegid();
79764325Seric 		if (saveduid == 0)
79864325Seric 		{
79964325Seric 			initgroups(uname, gid);
80064325Seric 			if (uid != 0)
80164325Seric 				(void) setreuid(0, uid);
80264325Seric 		}
80364325Seric #endif
80464083Seric 	}
80563581Seric 
80664325Seric 	if (tTd(27, 9))
80764325Seric 		printf("include: new uid = %d/%d\n", getuid(), geteuid());
80864325Seric 
80964325Seric 	/*
81064325Seric 	**  If home directory is remote mounted but server is down,
81164325Seric 	**  this can hang or give errors; use a timeout to avoid this
81264325Seric 	*/
81364325Seric 
81453037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
81553037Seric 	{
81663853Seric 		ctladdr->q_flags |= QQUEUEUP;
81753037Seric 		errno = 0;
81863993Seric 
81963993Seric 		/* return pseudo-error code */
82064325Seric 		rval = EOPENTIMEOUT;
82164325Seric 		goto resetuid;
82253037Seric 	}
823*67711Seric 	if (TimeOuts.to_fileopen > 0)
824*67711Seric 		ev = setevent(TimeOuts.to_fileopen, includetimeout, 0);
825*67711Seric 	else
826*67711Seric 		ev = NULL;
82753037Seric 
82863581Seric 	/* the input file must be marked safe */
82964944Seric 	rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD);
83064329Seric 	if (rval != 0)
83153037Seric 	{
83264325Seric 		/* don't use this :include: file */
83357186Seric 		if (tTd(27, 4))
83458247Seric 			printf("include: not safe (uid=%d): %s\n",
83564329Seric 				uid, errstring(rval));
83653037Seric 	}
83765496Seric 	else
8384174Seric 	{
83965496Seric 		fp = fopen(fname, "r");
84065496Seric 		if (fp == NULL)
84158061Seric 		{
84264329Seric 			rval = errno;
84365496Seric 			if (tTd(27, 4))
84465496Seric 				printf("include: open: %s\n", errstring(rval));
84558061Seric 		}
8464406Seric 	}
847*67711Seric 	if (ev != NULL)
848*67711Seric 		clrevent(ev);
84953037Seric 
85064570Seric resetuid:
85164570Seric 
85264570Seric #ifdef HASSETREUID
85364570Seric 	if (saveduid == 0)
85464570Seric 	{
85564570Seric 		if (uid != 0)
85664570Seric 			if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0)
85764570Seric 				syserr("setreuid(%d, 0) failure (real=%d, eff=%d)",
85864570Seric 					RealUid, getuid(), geteuid());
85964570Seric 		setgid(savedgid);
86064570Seric 	}
86164570Seric #endif
86264570Seric 
86364570Seric 	if (tTd(27, 9))
86464570Seric 		printf("include: reset uid = %d/%d\n", getuid(), geteuid());
86564570Seric 
86665593Seric 	if (rval == EOPENTIMEOUT)
86765593Seric 		usrerr("451 open timeout on %s", fname);
86865593Seric 
86964570Seric 	if (fp == NULL)
87064570Seric 		return rval;
87164570Seric 
87265496Seric 	if (fstat(fileno(fp), &st) < 0)
87365496Seric 	{
87465496Seric 		rval = errno;
87565496Seric 		syserr("Cannot fstat %s!", fname);
87665496Seric 		return rval;
87765496Seric 	}
87865496Seric 
87965948Seric #ifndef safechown
88065948Seric 	safechown = chownsafe(fileno(fp));
88165948Seric #endif
88265909Seric 	if (ca == NULL && safechown)
88365496Seric 	{
88465496Seric 		ctladdr->q_uid = st.st_uid;
88565496Seric 		ctladdr->q_gid = st.st_gid;
88665496Seric 		ctladdr->q_flags |= QGOODUID;
88765496Seric 	}
88865496Seric 	if (ca != NULL && ca->q_uid == st.st_uid)
88965496Seric 	{
89065496Seric 		/* optimization -- avoid getpwuid if we already have info */
89165496Seric 		ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL;
89265496Seric 		ctladdr->q_ruser = ca->q_ruser;
89365496Seric 	}
89465496Seric 	else
89565496Seric 	{
89665909Seric 		char *sh;
89765496Seric 		register struct passwd *pw;
89865496Seric 
89965909Seric 		sh = "/SENDMAIL/ANY/SHELL/";
90065496Seric 		pw = getpwuid(st.st_uid);
90165909Seric 		if (pw != NULL)
90265496Seric 		{
90365496Seric 			ctladdr->q_ruser = newstr(pw->pw_name);
90465909Seric 			if (safechown)
90565909Seric 				sh = pw->pw_shell;
90665909Seric 		}
90765909Seric 		if (pw == NULL)
90865496Seric 			ctladdr->q_flags |= QBOGUSSHELL;
90965909Seric 		else if(!usershellok(sh))
91065909Seric 		{
91165909Seric 			if (safechown)
91265909Seric 				ctladdr->q_flags |= QBOGUSSHELL;
91365909Seric 			else
91465909Seric 				ctladdr->q_flags |= QUNSAFEADDR;
91565496Seric 		}
91665496Seric 	}
91765496Seric 
91858092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
91958092Seric 	{
92058092Seric 		/* don't do any more now */
92158868Seric 		ctladdr->q_flags |= QVERIFIED;
92258884Seric 		e->e_nrcpts++;
92358680Seric 		xfclose(fp, "include", fname);
92464570Seric 		return rval;
92558092Seric 	}
92658092Seric 
92765496Seric 	/*
92865496Seric 	** Check to see if some bad guy can write this file
92965496Seric 	**
93065496Seric 	**	This should really do something clever with group
93165496Seric 	**	permissions; currently we just view world writable
93265496Seric 	**	as unsafe.  Also, we don't check for writable
93365496Seric 	**	directories in the path.  We've got to leave
93465496Seric 	**	something for the local sysad to do.
93565496Seric 	*/
93665496Seric 
93765496Seric 	if (bitset(S_IWOTH, st.st_mode))
93865496Seric 		ctladdr->q_flags |= QUNSAFEADDR;
93965496Seric 
9404174Seric 	/* read the file -- each line is a comma-separated list. */
9419379Seric 	FileName = fname;
9429379Seric 	LineNumber = 0;
94358082Seric 	ctladdr->q_flags &= ~QSELFREF;
94458082Seric 	nincludes = 0;
9454174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
9464174Seric 	{
94756795Seric 		register char *p = strchr(buf, '\n');
9484174Seric 
94940963Sbostic 		LineNumber++;
9504174Seric 		if (p != NULL)
9514174Seric 			*p = '\0';
95257186Seric 		if (buf[0] == '#' || buf[0] == '\0')
95357139Seric 			continue;
95458008Seric 		e->e_to = NULL;
95558151Seric 		message("%s to %s",
95653037Seric 			forwarding ? "forwarding" : "sending", buf);
95757977Seric #ifdef LOG
95858020Seric 		if (forwarding && LogLevel > 9)
95957977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
96066284Seric 				e->e_id == NULL ? "NOQUEUE" : e->e_id,
96166284Seric 				oldto, buf);
96257977Seric #endif
96357977Seric 
9644176Seric 		AliasLevel++;
96558082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
9664176Seric 		AliasLevel--;
9674174Seric 	}
96863902Seric 
96963902Seric 	if (ferror(fp) && tTd(27, 3))
97063902Seric 		printf("include: read error: %s\n", errstring(errno));
97158082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
97258065Seric 	{
97358065Seric 		if (tTd(27, 5))
97458065Seric 		{
97558065Seric 			printf("include: QDONTSEND ");
97658065Seric 			printaddr(ctladdr, FALSE);
97758065Seric 		}
97858065Seric 		ctladdr->q_flags |= QDONTSEND;
97958065Seric 	}
9804174Seric 
98158680Seric 	(void) xfclose(fp, "include", fname);
9829379Seric 	FileName = oldfilename;
9839379Seric 	LineNumber = oldlinenumber;
98463847Seric 	e->e_to = oldto;
98564325Seric 	return rval;
9864174Seric }
98753037Seric 
98853037Seric static
98953037Seric includetimeout()
99053037Seric {
99153037Seric 	longjmp(CtxIncludeTimeout, 1);
99253037Seric }
9934324Seric /*
9944324Seric **  SENDTOARGV -- send to an argument vector.
9954324Seric **
9964324Seric **	Parameters:
9974324Seric **		argv -- argument vector to send to.
99858247Seric **		e -- the current envelope.
9994324Seric **
10004324Seric **	Returns:
10014324Seric **		none.
10024324Seric **
10034324Seric **	Side Effects:
10044324Seric **		puts all addresses on the argument vector onto the
10054324Seric **			send queue.
10064324Seric */
10074324Seric 
100855012Seric sendtoargv(argv, e)
10094324Seric 	register char **argv;
101055012Seric 	register ENVELOPE *e;
10114324Seric {
10124324Seric 	register char *p;
10134324Seric 
10144324Seric 	while ((p = *argv++) != NULL)
10154324Seric 	{
101664284Seric 		(void) sendtolist(p, NULLADDR, &e->e_sendqueue, e);
10174324Seric 	}
10184324Seric }
10194399Seric /*
10204399Seric **  GETCTLADDR -- get controlling address from an address header.
10214399Seric **
10224399Seric **	If none, get one corresponding to the effective userid.
10234399Seric **
10244399Seric **	Parameters:
10254399Seric **		a -- the address to find the controller of.
10264399Seric **
10274399Seric **	Returns:
10284399Seric **		the controlling address.
10294399Seric **
10304399Seric **	Side Effects:
10314399Seric **		none.
10324399Seric */
10334399Seric 
10344399Seric ADDRESS *
10354399Seric getctladdr(a)
10364399Seric 	register ADDRESS *a;
10374399Seric {
10384404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
10394399Seric 		a = a->q_alias;
10404399Seric 	return (a);
10414399Seric }
1042