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*68457Seric static char sccsid[] = "@(#)recipient.c	8.44.1.5 (Berkeley) 02/28/95";
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 
39*68457Seric # define MAXRCRSN	10
404174Seric 
41*68457Seric 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;
5268392Seric 	int i;
5363847Seric 	char *oldto = e->e_to;
5468392Seric 	char *bufp;
5568271Seric 	char buf[MAXNAME + 1];
564174Seric 
5764131Seric 	if (list == NULL)
5864131Seric 	{
5964131Seric 		syserr("sendtolist: null list");
6064131Seric 		return 0;
6164131Seric 	}
6264131Seric 
637676Seric 	if (tTd(25, 1))
644444Seric 	{
654444Seric 		printf("sendto: %s\n   ctladdr=", list);
664444Seric 		printaddr(ctladdr, FALSE);
674444Seric 	}
684324Seric 
698223Seric 	/* heuristic to determine old versus new style addresses */
708230Seric 	if (ctladdr == NULL &&
7156795Seric 	    (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
7256795Seric 	     strchr(list, '<') != NULL || strchr(list, '(') != NULL))
7355012Seric 		e->e_flags &= ~EF_OLDSTYLE;
7411446Seric 	delimiter = ' ';
7555012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
7611446Seric 		delimiter = ',';
778223Seric 
784423Seric 	firstone = TRUE;
794324Seric 	al = NULL;
8058082Seric 	naddrs = 0;
818223Seric 
8268392Seric 	/* make sure we have enough space to copy the string */
8368392Seric 	i = strlen(list) + 1;
8468392Seric 	if (i <= sizeof buf)
8568271Seric 		bufp = buf;
8668392Seric 	else
8768392Seric 		bufp = xalloc(i);
88*68457Seric 	strcpy(bufp, denlstring(list, FALSE));
8968271Seric 
9068271Seric 	for (p = bufp; *p != '\0'; )
9168271Seric 	{
9258333Seric 		auto char *delimptr;
938081Seric 		register ADDRESS *a;
944319Seric 
958081Seric 		/* parse the address */
9658050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
974174Seric 			p++;
9864284Seric 		a = parseaddr(p, NULLADDR, RF_COPYALL, delimiter, &delimptr, e);
9958333Seric 		p = delimptr;
1009297Seric 		if (a == NULL)
1014174Seric 			continue;
1024324Seric 		a->q_next = al;
1034399Seric 		a->q_alias = ctladdr;
1044444Seric 
1054444Seric 		/* see if this should be marked as a primary address */
1064423Seric 		if (ctladdr == NULL ||
1078081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
1084423Seric 			a->q_flags |= QPRIMARY;
1094444Seric 
110*68457Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
111*68457Seric 			ctladdr->q_flags |= QSELFREF;
11257731Seric 		al = a;
1134423Seric 		firstone = FALSE;
1144324Seric 	}
1154324Seric 
1164324Seric 	/* arrange to send to everyone on the local send list */
1174324Seric 	while (al != NULL)
1184324Seric 	{
1194324Seric 		register ADDRESS *a = al;
1204324Seric 
1214324Seric 		al = a->q_next;
122*68457Seric 		a = recipient(a, sendq, e);
123*68457Seric 
124*68457Seric 		/* arrange to inherit full name */
125*68457Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
126*68457Seric 			a->q_fullname = ctladdr->q_fullname;
12758082Seric 		naddrs++;
1284174Seric 	}
1294324Seric 
13063847Seric 	e->e_to = oldto;
13168392Seric 	if (bufp != buf)
13268392Seric 		free(bufp);
13358082Seric 	return (naddrs);
1344174Seric }
1354174Seric /*
1364174Seric **  RECIPIENT -- Designate a message recipient
1374174Seric **
1384174Seric **	Saves the named person for future mailing.
1394174Seric **
1404174Seric **	Parameters:
1414174Seric **		a -- the (preparsed) address header for the recipient.
1425006Seric **		sendq -- a pointer to the head of a queue to put the
1435006Seric **			recipient in.  Duplicate supression is done
1445006Seric **			in this queue.
14557731Seric **		e -- the current envelope.
1464174Seric **
1474174Seric **	Returns:
14812613Seric **		The actual address in the queue.  This will be "a" if
14912613Seric **		the address is not a duplicate, else the original address.
1504174Seric **
1514174Seric **	Side Effects:
1524174Seric **		none.
1534174Seric */
1544174Seric 
15512613Seric ADDRESS *
156*68457Seric recipient(a, sendq, e)
1574174Seric 	register ADDRESS *a;
1585006Seric 	register ADDRESS **sendq;
15955012Seric 	register ENVELOPE *e;
1604174Seric {
1614174Seric 	register ADDRESS *q;
1624319Seric 	ADDRESS **pq;
1634174Seric 	register struct mailer *m;
1649210Seric 	register char *p;
1659210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
16653735Seric 	int findusercount = 0;
167*68457Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
16858247Seric 	extern int safefile();
1694174Seric 
17055012Seric 	e->e_to = a->q_paddr;
1714600Seric 	m = a->q_mailer;
1724174Seric 	errno = 0;
1737676Seric 	if (tTd(26, 1))
1744444Seric 	{
1754444Seric 		printf("\nrecipient: ");
1764444Seric 		printaddr(a, FALSE);
1774444Seric 	}
1784174Seric 
17964146Seric 	/* if this is primary, add it to the original recipient list */
18064146Seric 	if (a->q_alias == NULL)
18164146Seric 	{
18264146Seric 		if (e->e_origrcpt == NULL)
18364146Seric 			e->e_origrcpt = a->q_paddr;
18464146Seric 		else if (e->e_origrcpt != a->q_paddr)
18564146Seric 			e->e_origrcpt = "";
18664146Seric 	}
18764146Seric 
1884174Seric 	/* break aliasing loops */
189*68457Seric 	if (AliasLevel > MAXRCRSN)
1904174Seric 	{
191*68457Seric 		usrerr("554 aliasing/forwarding loop broken");
19212613Seric 		return (a);
1934174Seric 	}
1944174Seric 
1954174Seric 	/*
1964627Seric 	**  Finish setting up address structure.
1974174Seric 	*/
1984174Seric 
199*68457Seric 	/* set the queue timeout */
200*68457Seric 	a->q_timeout = TimeOuts.to_q_return;
201*68457Seric 
20216160Seric 	/* get unquoted user for file, program or user.name check */
2039210Seric 	(void) strcpy(buf, a->q_user);
2049210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
2059210Seric 	{
20654993Seric 		if (*p == '\\')
2079210Seric 			quoted = TRUE;
2089210Seric 	}
20954983Seric 	stripquotes(buf);
2109210Seric 
21157402Seric 	/* check for direct mailing to restricted mailers */
21265496Seric 	if (m == ProgMailer)
2134174Seric 	{
21465496Seric 		if (a->q_alias == NULL)
21565496Seric 		{
21665496Seric 			a->q_flags |= QBADADDR;
21765496Seric 			usrerr("550 Cannot mail directly to programs");
21865496Seric 		}
21965496Seric 		else if (bitset(QBOGUSSHELL, a->q_alias->q_flags))
22065496Seric 		{
22165496Seric 			a->q_flags |= QBADADDR;
22265496Seric 			usrerr("550 User %s@%s doesn't have a valid shell for mailing to programs",
22365496Seric 				a->q_alias->q_ruser, MyHostName);
22465496Seric 		}
22565496Seric 		else if (bitset(QUNSAFEADDR, a->q_alias->q_flags))
22665496Seric 		{
22765496Seric 			a->q_flags |= QBADADDR;
22865496Seric 			usrerr("550 Address %s is unsafe for mailing to programs",
22965496Seric 				a->q_alias->q_paddr);
23065496Seric 		}
2314174Seric 	}
2324174Seric 
2334174Seric 	/*
2344419Seric 	**  Look up this person in the recipient list.
2354419Seric 	**	If they are there already, return, otherwise continue.
2364419Seric 	**	If the list is empty, just add it.  Notice the cute
2374419Seric 	**	hack to make from addresses suppress things correctly:
2384419Seric 	**	the QDONTSEND bit will be set in the send list.
2394419Seric 	**	[Please note: the emphasis is on "hack."]
2404174Seric 	*/
2414174Seric 
2425006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2434174Seric 	{
24458294Seric 		if (sameaddr(q, a))
2454174Seric 		{
2467676Seric 			if (tTd(26, 1))
2474444Seric 			{
2484444Seric 				printf("%s in sendq: ", a->q_paddr);
2494444Seric 				printaddr(q, FALSE);
2504444Seric 			}
25165593Seric 			if (!bitset(QPRIMARY, q->q_flags))
25258065Seric 			{
25365593Seric 				if (!bitset(QDONTSEND, a->q_flags))
25458151Seric 					message("duplicate suppressed");
25565593Seric 				q->q_flags |= a->q_flags;
25665593Seric 			}
25765593Seric 			else if (bitset(QSELFREF, q->q_flags))
25865579Seric 				q->q_flags |= a->q_flags & ~QDONTSEND;
25963847Seric 			a = q;
260*68457Seric 			goto testselfdestruct;
2614174Seric 		}
2624319Seric 	}
2634174Seric 
2644319Seric 	/* add address on list */
26558884Seric 	*pq = a;
26658884Seric 	a->q_next = NULL;
2674174Seric 
2684174Seric 	/*
26957402Seric 	**  Alias the name and handle special mailer types.
2704174Seric 	*/
2714174Seric 
27253735Seric   trylocaluser:
27355354Seric 	if (tTd(29, 7))
27455354Seric 		printf("at trylocaluser %s\n", a->q_user);
27555354Seric 
27658680Seric 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
27763847Seric 		goto testselfdestruct;
27857402Seric 
27957402Seric 	if (m == InclMailer)
2804174Seric 	{
28157402Seric 		a->q_flags |= QDONTSEND;
28264761Seric 		if (a->q_alias == NULL)
2834174Seric 		{
28458680Seric 			a->q_flags |= QBADADDR;
28558151Seric 			usrerr("550 Cannot mail directly to :include:s");
2864174Seric 		}
2874174Seric 		else
28850556Seric 		{
28959563Seric 			int ret;
29058247Seric 
29158151Seric 			message("including file %s", a->q_user);
292*68457Seric 			ret = include(a->q_user, FALSE, a, sendq, e);
29359563Seric 			if (transienterror(ret))
29459563Seric 			{
29559563Seric #ifdef LOG
29659563Seric 				if (LogLevel > 2)
29766239Seric 					syslog(LOG_ERR, "%s: include %s: transient error: %s",
29866284Seric 						e->e_id == NULL ? "NOQUEUE" : e->e_id,
29966284Seric 						a->q_user, errstring(ret));
30059563Seric #endif
30163853Seric 				a->q_flags |= QQUEUEUP;
30265215Seric 				a->q_flags &= ~QDONTSEND;
30359563Seric 				usrerr("451 Cannot open %s: %s",
30459563Seric 					a->q_user, errstring(ret));
30559563Seric 			}
30659563Seric 			else if (ret != 0)
30759563Seric 			{
30863938Seric 				a->q_flags |= QBADADDR;
30959563Seric 				usrerr("550 Cannot open %s: %s",
31059563Seric 					a->q_user, errstring(ret));
31159563Seric 			}
31250556Seric 		}
3134174Seric 	}
31457642Seric 	else if (m == FileMailer)
3154174Seric 	{
3164329Seric 		extern bool writable();
3174174Seric 
31851317Seric 		/* check if writable or creatable */
31964761Seric 		if (a->q_alias == NULL)
3204174Seric 		{
32158680Seric 			a->q_flags |= QBADADDR;
32258151Seric 			usrerr("550 Cannot mail directly to files");
3234174Seric 		}
32465496Seric 		else if (bitset(QBOGUSSHELL, a->q_alias->q_flags))
32565496Seric 		{
32665496Seric 			a->q_flags |= QBADADDR;
32765496Seric 			usrerr("550 User %s@%s doesn't have a valid shell for mailing to files",
32865496Seric 				a->q_alias->q_ruser, MyHostName);
32965496Seric 		}
33065496Seric 		else if (bitset(QUNSAFEADDR, a->q_alias->q_flags))
33165496Seric 		{
33265496Seric 			a->q_flags |= QBADADDR;
33365496Seric 			usrerr("550 Address %s is unsafe for mailing to files",
33465496Seric 				a->q_alias->q_paddr);
33565496Seric 		}
33665112Seric 		else if (!writable(buf, getctladdr(a), SFF_ANYFILE))
33751317Seric 		{
33858680Seric 			a->q_flags |= QBADADDR;
339*68457Seric 			giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e);
34051317Seric 		}
34151317Seric 	}
34251317Seric 
343*68457Seric 	if (m != LocalMailer)
344*68457Seric 	{
345*68457Seric 		if (!bitset(QDONTSEND, a->q_flags))
346*68457Seric 			e->e_nrcpts++;
347*68457Seric 		goto testselfdestruct;
348*68457Seric 	}
349*68457Seric 
35057402Seric 	/* try aliasing */
351*68457Seric 	alias(a, sendq, e);
35257402Seric 
35357402Seric # ifdef USERDB
35457402Seric 	/* if not aliased, look it up in the user database */
355*68457Seric 	if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags))
35657402Seric 	{
35757402Seric 		extern int udbexpand();
35857402Seric 
359*68457Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
36057402Seric 		{
36163853Seric 			a->q_flags |= QQUEUEUP;
36257402Seric 			if (e->e_message == NULL)
36357402Seric 				e->e_message = newstr("Deferred: user database error");
36457402Seric # ifdef LOG
36558020Seric 			if (LogLevel > 8)
36659623Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand: %s",
36766284Seric 					e->e_id == NULL ? "NOQUEUE" : e->e_id,
36866284Seric 					errstring(errno));
36957402Seric # endif
37059615Seric 			message("queued (user database error): %s",
37159615Seric 				errstring(errno));
37257642Seric 			e->e_nrcpts++;
37363847Seric 			goto testselfdestruct;
37457402Seric 		}
37557402Seric 	}
37657402Seric # endif
37757402Seric 
378*68457Seric 	/* if it was an alias or a UDB expansion, just return now */
379*68457Seric 	if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags))
380*68457Seric 		goto testselfdestruct;
381*68457Seric 
38251317Seric 	/*
38351317Seric 	**  If we have a level two config file, then pass the name through
38451317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
38551317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
38651317Seric 	**  after local aliasing has been done.
38751317Seric 	*/
38851317Seric 
38951317Seric 	if (tTd(29, 5))
39051317Seric 	{
39151317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
39251317Seric 			ConfigLevel, RewriteRules[5]);
39351317Seric 		printaddr(a, FALSE);
39451317Seric 	}
395*68457Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
396*68457Seric 	    RewriteRules[5] != NULL)
39751317Seric 	{
398*68457Seric 		maplocaluser(a, sendq, e);
39951317Seric 	}
40051317Seric 
40151317Seric 	/*
40251317Seric 	**  If it didn't get rewritten to another mailer, go ahead
40351317Seric 	**  and deliver it.
40451317Seric 	*/
40551317Seric 
406*68457Seric 	if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags))
40751317Seric 	{
40855354Seric 		auto bool fuzzy;
40951317Seric 		register struct passwd *pw;
41051317Seric 		extern struct passwd *finduser();
41151317Seric 
41251317Seric 		/* warning -- finduser may trash buf */
41355354Seric 		pw = finduser(buf, &fuzzy);
41451317Seric 		if (pw == NULL)
41551317Seric 		{
41658680Seric 			a->q_flags |= QBADADDR;
417*68457Seric 			giveresponse(EX_NOUSER, m, NULL, a->q_alias, e);
41851317Seric 		}
4194174Seric 		else
4204174Seric 		{
42151317Seric 			char nbuf[MAXNAME];
4224373Seric 
42355354Seric 			if (fuzzy)
4244174Seric 			{
42553735Seric 				/* name was a fuzzy match */
42651317Seric 				a->q_user = newstr(pw->pw_name);
42753735Seric 				if (findusercount++ > 3)
42853735Seric 				{
42958680Seric 					a->q_flags |= QBADADDR;
43058151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
43153735Seric 						pw->pw_name);
432*68457Seric 					return (a);
43353735Seric 				}
43453735Seric 
43553735Seric 				/* see if it aliases */
43651317Seric 				(void) strcpy(buf, pw->pw_name);
43753735Seric 				goto trylocaluser;
4384174Seric 			}
43965822Seric 			if (strcmp(pw->pw_dir, "/") == 0)
44065822Seric 				a->q_home = "";
44165822Seric 			else
44265822Seric 				a->q_home = newstr(pw->pw_dir);
44351317Seric 			a->q_uid = pw->pw_uid;
44451317Seric 			a->q_gid = pw->pw_gid;
44559083Seric 			a->q_ruser = newstr(pw->pw_name);
44651317Seric 			a->q_flags |= QGOODUID;
44751317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
44851317Seric 			if (nbuf[0] != '\0')
44951317Seric 				a->q_fullname = newstr(nbuf);
45065211Seric 			if (pw->pw_shell != NULL && pw->pw_shell[0] != '\0' &&
45165211Seric 			    !usershellok(pw->pw_shell))
45265206Seric 			{
45365211Seric 				a->q_flags |= QBOGUSSHELL;
45465206Seric 			}
45551317Seric 			if (!quoted)
456*68457Seric 				forward(a, sendq, e);
4574174Seric 		}
4584174Seric 	}
45957642Seric 	if (!bitset(QDONTSEND, a->q_flags))
46057642Seric 		e->e_nrcpts++;
46163847Seric 
46263847Seric   testselfdestruct:
46363978Seric 	if (tTd(26, 8))
46463847Seric 	{
46563978Seric 		printf("testselfdestruct: ");
46663978Seric 		printaddr(a, TRUE);
46763978Seric 	}
46863978Seric 	if (a->q_alias == NULL && a != &e->e_from &&
46963978Seric 	    bitset(QDONTSEND, a->q_flags))
47063978Seric 	{
47163978Seric 		q = *sendq;
47263965Seric 		while (q != NULL && bitset(QDONTSEND, q->q_flags))
47363847Seric 			q = q->q_next;
47463978Seric 		if (q == NULL)
47563847Seric 		{
47663847Seric 			a->q_flags |= QBADADDR;
47763847Seric 			usrerr("554 aliasing/forwarding loop broken");
47863847Seric 		}
47963847Seric 	}
48012613Seric 	return (a);
4814174Seric }
4824174Seric /*
4834373Seric **  FINDUSER -- find the password entry for a user.
4844373Seric **
4854373Seric **	This looks a lot like getpwnam, except that it may want to
4864373Seric **	do some fancier pattern matching in /etc/passwd.
4874373Seric **
4889379Seric **	This routine contains most of the time of many sendmail runs.
4899379Seric **	It deserves to be optimized.
4909379Seric **
4914373Seric **	Parameters:
4924373Seric **		name -- the name to match against.
49355354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
49455354Seric **			was found using the fuzzy matching algorithm;
49555354Seric **			set to FALSE otherwise.
4964373Seric **
4974373Seric **	Returns:
4984373Seric **		A pointer to a pw struct.
4994373Seric **		NULL if name is unknown or ambiguous.
5004373Seric **
5014373Seric **	Side Effects:
5024407Seric **		may modify name.
5034373Seric */
5044373Seric 
5054373Seric struct passwd *
50655354Seric finduser(name, fuzzyp)
5074373Seric 	char *name;
50855354Seric 	bool *fuzzyp;
5094373Seric {
5104376Seric 	register struct passwd *pw;
5114407Seric 	register char *p;
51215325Seric 	extern struct passwd *getpwent();
51315325Seric 	extern struct passwd *getpwnam();
5144373Seric 
51555354Seric 	if (tTd(29, 4))
51655354Seric 		printf("finduser(%s): ", name);
51755354Seric 
51855354Seric 	*fuzzyp = FALSE;
5194407Seric 
52064673Seric 	/* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */
52164673Seric 	for (p = name; *p != '\0'; p++)
52264673Seric 		if (!isascii(*p) || !isdigit(*p))
52364673Seric 			break;
52464673Seric 	if (*p == '\0')
52564673Seric 	{
52664673Seric 		if (tTd(29, 4))
52764673Seric 			printf("failed (numeric input)\n");
52864673Seric 		return NULL;
52964673Seric 	}
53064673Seric 
53125777Seric 	/* look up this login name using fast path */
53212634Seric 	if ((pw = getpwnam(name)) != NULL)
53355354Seric 	{
53455354Seric 		if (tTd(29, 4))
53555354Seric 			printf("found (non-fuzzy)\n");
53612634Seric 		return (pw);
53755354Seric 	}
53812634Seric 
53953735Seric #ifdef MATCHGECOS
54053735Seric 	/* see if fuzzy matching allowed */
54153735Seric 	if (!MatchGecos)
54255354Seric 	{
54355354Seric 		if (tTd(29, 4))
54455354Seric 			printf("not found (fuzzy disabled)\n");
54553735Seric 		return NULL;
54655354Seric 	}
54753735Seric 
54812634Seric 	/* search for a matching full name instead */
54925777Seric 	for (p = name; *p != '\0'; p++)
55025777Seric 	{
55125777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
55225777Seric 			*p = ' ';
55325777Seric 	}
55423107Seric 	(void) setpwent();
5554376Seric 	while ((pw = getpwent()) != NULL)
5564376Seric 	{
5574998Seric 		char buf[MAXNAME];
5584376Seric 
5594998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
56056795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
5614381Seric 		{
56255354Seric 			if (tTd(29, 4))
56355354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
56458151Seric 			message("sending to login name %s", pw->pw_name);
56555354Seric 			*fuzzyp = TRUE;
5664376Seric 			return (pw);
5674377Seric 		}
5684376Seric 	}
56955354Seric 	if (tTd(29, 4))
57055354Seric 		printf("no fuzzy match found\n");
57159015Seric #else
57259015Seric 	if (tTd(29, 4))
57359015Seric 		printf("not found (fuzzy disabled)\n");
57459015Seric #endif
5754376Seric 	return (NULL);
5764373Seric }
5774373Seric /*
5784329Seric **  WRITABLE -- predicate returning if the file is writable.
5794329Seric **
5804329Seric **	This routine must duplicate the algorithm in sys/fio.c.
5814329Seric **	Unfortunately, we cannot use the access call since we
5824329Seric **	won't necessarily be the real uid when we try to
5834329Seric **	actually open the file.
5844329Seric **
5854329Seric **	Notice that ANY file with ANY execute bit is automatically
5864329Seric **	not writable.  This is also enforced by mailfile.
5874329Seric **
5884329Seric **	Parameters:
58965064Seric **		filename -- the file name to check.
59065112Seric **		ctladdr -- the controlling address for this file.
59165064Seric **		flags -- SFF_* flags to control the function.
5924329Seric **
5934329Seric **	Returns:
5944329Seric **		TRUE -- if we will be able to write this file.
5954329Seric **		FALSE -- if we cannot write this file.
5964329Seric **
5974329Seric **	Side Effects:
5984329Seric **		none.
5994329Seric */
6004329Seric 
6014329Seric bool
60265112Seric writable(filename, ctladdr, flags)
60364819Seric 	char *filename;
60465112Seric 	ADDRESS *ctladdr;
60565064Seric 	int flags;
6064329Seric {
60755372Seric 	uid_t euid;
60855372Seric 	gid_t egid;
6094329Seric 	int bits;
61064944Seric 	register char *p;
61164944Seric 	char *uname;
61264944Seric 	struct stat stb;
61364944Seric 	extern char RealUserName[];
6144329Seric 
61564819Seric 	if (tTd(29, 5))
61665064Seric 		printf("writable(%s, %x)\n", filename, flags);
61764944Seric 
61864944Seric #ifdef HASLSTAT
61965064Seric 	if ((bitset(SFF_NOSLINK, flags) ? lstat(filename, &stb)
62065064Seric 					: stat(filename, &stb)) < 0)
62164944Seric #else
62264944Seric 	if (stat(filename, &stb) < 0)
62364944Seric #endif
62464944Seric 	{
62564944Seric 		/* file does not exist -- see if directory is safe */
62664944Seric 		p = strrchr(filename, '/');
62764944Seric 		if (p == NULL)
62864944Seric 		{
62965067Seric 			errno = ENOTDIR;
63064944Seric 			return FALSE;
63164944Seric 		}
63265067Seric 		*p = '\0';
63365067Seric 		errno = safefile(filename, RealUid, RealGid, RealUserName,
63465067Seric 				 SFF_MUSTOWN, S_IWRITE|S_IEXEC);
63564944Seric 		*p = '/';
63665067Seric 		return errno == 0;
63764944Seric 	}
63864944Seric 
63965225Seric #ifdef SUID_ROOT_FILES_OK
64065225Seric 	/* really ought to be passed down -- and not a good idea */
64165225Seric 	flags |= SFF_ROOTOK;
64265225Seric #endif
64365225Seric 
64464944Seric 	/*
64564944Seric 	**  File does exist -- check that it is writable.
64664944Seric 	*/
64764944Seric 
64864944Seric 	if (bitset(0111, stb.st_mode))
64965022Seric 	{
65065022Seric 		if (tTd(29, 5))
65165022Seric 			printf("failed (mode %o: x bits)\n", stb.st_mode);
65265067Seric 		errno = EPERM;
6534329Seric 		return (FALSE);
65465022Seric 	}
65564944Seric 
65665112Seric 	if (ctladdr != NULL && geteuid() == 0)
65764944Seric 	{
65865112Seric 		euid = ctladdr->q_uid;
65965112Seric 		egid = ctladdr->q_gid;
66065112Seric 		uname = ctladdr->q_user;
66164944Seric 	}
66265112Seric 	else
66365112Seric 	{
66465112Seric 		euid = RealUid;
66565112Seric 		egid = RealGid;
66665112Seric 		uname = RealUserName;
66765112Seric 	}
66865138Seric 	if (euid == 0)
66965138Seric 	{
67065138Seric 		euid = DefUid;
67165138Seric 		uname = DefUser;
67265138Seric 	}
67365138Seric 	if (egid == 0)
67465138Seric 		egid = DefGid;
6754329Seric 	if (geteuid() == 0)
6764329Seric 	{
67765225Seric 		if (bitset(S_ISUID, stb.st_mode) &&
67865225Seric 		    (stb.st_uid != 0 || bitset(SFF_ROOTOK, flags)))
67964944Seric 		{
68064944Seric 			euid = stb.st_uid;
68164944Seric 			uname = NULL;
68264944Seric 		}
68365225Seric 		if (bitset(S_ISGID, stb.st_mode) &&
68465225Seric 		    (stb.st_gid != 0 || bitset(SFF_ROOTOK, flags)))
68564944Seric 			egid = stb.st_gid;
6864329Seric 	}
6874329Seric 
68864819Seric 	if (tTd(29, 5))
68964819Seric 		printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n",
69064944Seric 			euid, egid, stb.st_uid, stb.st_gid);
69164819Seric 
69265067Seric 	errno = safefile(filename, euid, egid, uname, flags, S_IWRITE);
69365067Seric 	return errno == 0;
6944329Seric }
6954329Seric /*
6964174Seric **  INCLUDE -- handle :include: specification.
6974174Seric **
6984174Seric **	Parameters:
6994174Seric **		fname -- filename to include.
70053037Seric **		forwarding -- if TRUE, we are reading a .forward file.
70153037Seric **			if FALSE, it's a :include: file.
7024399Seric **		ctladdr -- address template to use to fill in these
7034399Seric **			addresses -- effective user/group id are
7044399Seric **			the important things.
7055006Seric **		sendq -- a pointer to the head of the send queue
7065006Seric **			to put these addresses in.
7074174Seric **
7084174Seric **	Returns:
70957136Seric **		open error status
7104174Seric **
7114174Seric **	Side Effects:
7124174Seric **		reads the :include: file and sends to everyone
7134174Seric **		listed in that file.
71465909Seric **
71565909Seric **	Security Note:
71665909Seric **		If you have restricted chown (that is, you can't
71765909Seric **		give a file away), it is reasonable to allow programs
71865909Seric **		and files called from this :include: file to be to be
71965909Seric **		run as the owner of the :include: file.  This is bogus
72065909Seric **		if there is any chance of someone giving away a file.
72165909Seric **		We assume that pre-POSIX systems can give away files.
72265909Seric **
72365909Seric **		There is an additional restriction that if you
72465909Seric **		forward to a :include: file, it will not take on
72565909Seric **		the ownership of the :include: file.  This may not
72665909Seric **		be necessary, but shouldn't hurt.
7274174Seric */
7284174Seric 
72953037Seric static jmp_buf	CtxIncludeTimeout;
730*68457Seric static int	includetimeout();
73153037Seric 
73265496Seric #ifndef S_IWOTH
73365496Seric # define S_IWOTH	(S_IWRITE >> 6)
73465496Seric #endif
73565496Seric 
73657136Seric int
737*68457Seric include(fname, forwarding, ctladdr, sendq, e)
7384174Seric 	char *fname;
73953037Seric 	bool forwarding;
7404399Seric 	ADDRESS *ctladdr;
7415006Seric 	ADDRESS **sendq;
74255012Seric 	ENVELOPE *e;
7434174Seric {
744*68457Seric 	register FILE *fp = NULL;
74555012Seric 	char *oldto = e->e_to;
7469379Seric 	char *oldfilename = FileName;
7479379Seric 	int oldlinenumber = LineNumber;
74853037Seric 	register EVENT *ev = NULL;
74958082Seric 	int nincludes;
75064325Seric 	register ADDRESS *ca;
75164325Seric 	uid_t saveduid, uid;
75264325Seric 	gid_t savedgid, gid;
75364083Seric 	char *uname;
75464325Seric 	int rval = 0;
75565064Seric 	int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE;
75665496Seric 	struct stat st;
75765948Seric 	char buf[MAXLINE];
75865909Seric #ifdef _POSIX_CHOWN_RESTRICTED
75965948Seric # if _POSIX_CHOWN_RESTRICTED == -1
76065948Seric #  define safechown	FALSE
76165948Seric # else
76265948Seric #  define safechown	TRUE
76365948Seric # endif
76465948Seric #else
76565948Seric # ifdef _PC_CHOWN_RESTRICTED
76665909Seric 	bool safechown;
76765948Seric # else
76865948Seric #  ifdef BSD
76965948Seric #   define safechown	TRUE
77065948Seric #  else
77165948Seric #   define safechown	FALSE
77265948Seric #  endif
77365948Seric # endif
77465909Seric #endif
77565948Seric 	extern bool chownsafe();
7764174Seric 
77757186Seric 	if (tTd(27, 2))
77857186Seric 		printf("include(%s)\n", fname);
77963902Seric 	if (tTd(27, 4))
78063902Seric 		printf("   ruid=%d euid=%d\n", getuid(), geteuid());
78163581Seric 	if (tTd(27, 14))
78263581Seric 	{
78363581Seric 		printf("ctladdr ");
78463581Seric 		printaddr(ctladdr, FALSE);
78563581Seric 	}
78657186Seric 
78764325Seric 	if (tTd(27, 9))
78864325Seric 		printf("include: old uid = %d/%d\n", getuid(), geteuid());
78953037Seric 
79063581Seric 	ca = getctladdr(ctladdr);
79163581Seric 	if (ca == NULL)
79264083Seric 	{
79364846Seric 		uid = DefUid;
79464846Seric 		gid = DefGid;
79564846Seric 		uname = DefUser;
796*68457Seric 		saveduid = -1;
79764083Seric 	}
79863581Seric 	else
79964083Seric 	{
80063581Seric 		uid = ca->q_uid;
80164083Seric 		gid = ca->q_gid;
80264083Seric 		uname = ca->q_user;
80364325Seric #ifdef HASSETREUID
804*68457Seric 		saveduid = geteuid();
805*68457Seric 		savedgid = getegid();
806*68457Seric 		if (saveduid == 0)
80764325Seric 		{
808*68457Seric 			initgroups(uname, gid);
809*68457Seric 			if (uid != 0)
810*68457Seric 				(void) setreuid(0, uid);
81164325Seric 		}
812*68457Seric #endif
81368393Seric 	}
81463581Seric 
81564325Seric 	if (tTd(27, 9))
81664325Seric 		printf("include: new uid = %d/%d\n", getuid(), geteuid());
81764325Seric 
81864325Seric 	/*
81964325Seric 	**  If home directory is remote mounted but server is down,
82064325Seric 	**  this can hang or give errors; use a timeout to avoid this
82164325Seric 	*/
82264325Seric 
82353037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
82453037Seric 	{
82563853Seric 		ctladdr->q_flags |= QQUEUEUP;
82653037Seric 		errno = 0;
82763993Seric 
82863993Seric 		/* return pseudo-error code */
82964325Seric 		rval = EOPENTIMEOUT;
83064325Seric 		goto resetuid;
83153037Seric 	}
832*68457Seric 	ev = setevent((time_t) 60, includetimeout, 0);
83353037Seric 
83463581Seric 	/* the input file must be marked safe */
83564944Seric 	rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD);
83664329Seric 	if (rval != 0)
83753037Seric 	{
83864325Seric 		/* don't use this :include: file */
83957186Seric 		if (tTd(27, 4))
84058247Seric 			printf("include: not safe (uid=%d): %s\n",
84164329Seric 				uid, errstring(rval));
84253037Seric 	}
84365496Seric 	else
8444174Seric 	{
84565496Seric 		fp = fopen(fname, "r");
84665496Seric 		if (fp == NULL)
84758061Seric 		{
84864329Seric 			rval = errno;
84965496Seric 			if (tTd(27, 4))
85065496Seric 				printf("include: open: %s\n", errstring(rval));
85158061Seric 		}
8524406Seric 	}
853*68457Seric 	clrevent(ev);
85453037Seric 
85564570Seric resetuid:
85664570Seric 
85764570Seric #ifdef HASSETREUID
85864570Seric 	if (saveduid == 0)
85964570Seric 	{
86064570Seric 		if (uid != 0)
861*68457Seric 			if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0)
86264570Seric 				syserr("setreuid(%d, 0) failure (real=%d, eff=%d)",
86364570Seric 					RealUid, getuid(), geteuid());
86464570Seric 		setgid(savedgid);
86564570Seric 	}
86664570Seric #endif
86764570Seric 
86864570Seric 	if (tTd(27, 9))
86964570Seric 		printf("include: reset uid = %d/%d\n", getuid(), geteuid());
87064570Seric 
87165593Seric 	if (rval == EOPENTIMEOUT)
87265593Seric 		usrerr("451 open timeout on %s", fname);
87365593Seric 
87464570Seric 	if (fp == NULL)
87564570Seric 		return rval;
87664570Seric 
87765496Seric 	if (fstat(fileno(fp), &st) < 0)
87865496Seric 	{
87965496Seric 		rval = errno;
88065496Seric 		syserr("Cannot fstat %s!", fname);
88165496Seric 		return rval;
88265496Seric 	}
88365496Seric 
88465948Seric #ifndef safechown
88565948Seric 	safechown = chownsafe(fileno(fp));
88665948Seric #endif
88765909Seric 	if (ca == NULL && safechown)
88865496Seric 	{
88965496Seric 		ctladdr->q_uid = st.st_uid;
89065496Seric 		ctladdr->q_gid = st.st_gid;
89165496Seric 		ctladdr->q_flags |= QGOODUID;
89265496Seric 	}
89365496Seric 	if (ca != NULL && ca->q_uid == st.st_uid)
89465496Seric 	{
89565496Seric 		/* optimization -- avoid getpwuid if we already have info */
89665496Seric 		ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL;
89765496Seric 		ctladdr->q_ruser = ca->q_ruser;
89865496Seric 	}
89965496Seric 	else
90065496Seric 	{
901*68457Seric 		char *sh;
90265496Seric 		register struct passwd *pw;
90365496Seric 
904*68457Seric 		sh = "/SENDMAIL/ANY/SHELL/";
90565496Seric 		pw = getpwuid(st.st_uid);
906*68457Seric 		if (pw != NULL)
907*68457Seric 		{
908*68457Seric 			ctladdr->q_ruser = newstr(pw->pw_name);
909*68457Seric 			if (safechown)
910*68457Seric 				sh = pw->pw_shell;
911*68457Seric 		}
91268393Seric 		if (pw == NULL)
91368393Seric 			ctladdr->q_flags |= QBOGUSSHELL;
914*68457Seric 		else if(!usershellok(sh))
91568392Seric 		{
91668392Seric 			if (safechown)
917*68457Seric 				ctladdr->q_flags |= QBOGUSSHELL;
91865909Seric 			else
919*68457Seric 				ctladdr->q_flags |= QUNSAFEADDR;
92065496Seric 		}
92165496Seric 	}
92265496Seric 
92358092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
92458092Seric 	{
92558092Seric 		/* don't do any more now */
92658868Seric 		ctladdr->q_flags |= QVERIFIED;
92758884Seric 		e->e_nrcpts++;
92858680Seric 		xfclose(fp, "include", fname);
92964570Seric 		return rval;
93058092Seric 	}
93158092Seric 
93265496Seric 	/*
93365496Seric 	** Check to see if some bad guy can write this file
93465496Seric 	**
93565496Seric 	**	This should really do something clever with group
93665496Seric 	**	permissions; currently we just view world writable
93765496Seric 	**	as unsafe.  Also, we don't check for writable
93865496Seric 	**	directories in the path.  We've got to leave
93965496Seric 	**	something for the local sysad to do.
94065496Seric 	*/
94165496Seric 
94265496Seric 	if (bitset(S_IWOTH, st.st_mode))
94365496Seric 		ctladdr->q_flags |= QUNSAFEADDR;
94465496Seric 
9454174Seric 	/* read the file -- each line is a comma-separated list. */
9469379Seric 	FileName = fname;
9479379Seric 	LineNumber = 0;
94858082Seric 	ctladdr->q_flags &= ~QSELFREF;
94958082Seric 	nincludes = 0;
9504174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
9514174Seric 	{
95256795Seric 		register char *p = strchr(buf, '\n');
9534174Seric 
95440963Sbostic 		LineNumber++;
9554174Seric 		if (p != NULL)
9564174Seric 			*p = '\0';
95757186Seric 		if (buf[0] == '#' || buf[0] == '\0')
95857139Seric 			continue;
95958008Seric 		e->e_to = NULL;
96058151Seric 		message("%s to %s",
96153037Seric 			forwarding ? "forwarding" : "sending", buf);
96257977Seric #ifdef LOG
96358020Seric 		if (forwarding && LogLevel > 9)
96457977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
96566284Seric 				e->e_id == NULL ? "NOQUEUE" : e->e_id,
96666284Seric 				oldto, buf);
96757977Seric #endif
96857977Seric 
969*68457Seric 		AliasLevel++;
970*68457Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
971*68457Seric 		AliasLevel--;
9724174Seric 	}
97363902Seric 
97463902Seric 	if (ferror(fp) && tTd(27, 3))
97563902Seric 		printf("include: read error: %s\n", errstring(errno));
97658082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
97758065Seric 	{
97858065Seric 		if (tTd(27, 5))
97958065Seric 		{
98058065Seric 			printf("include: QDONTSEND ");
98158065Seric 			printaddr(ctladdr, FALSE);
98258065Seric 		}
98358065Seric 		ctladdr->q_flags |= QDONTSEND;
98458065Seric 	}
9854174Seric 
98658680Seric 	(void) xfclose(fp, "include", fname);
9879379Seric 	FileName = oldfilename;
9889379Seric 	LineNumber = oldlinenumber;
98963847Seric 	e->e_to = oldto;
99064325Seric 	return rval;
9914174Seric }
99253037Seric 
993*68457Seric static
99453037Seric includetimeout()
99553037Seric {
99653037Seric 	longjmp(CtxIncludeTimeout, 1);
99753037Seric }
9984324Seric /*
9994324Seric **  SENDTOARGV -- send to an argument vector.
10004324Seric **
10014324Seric **	Parameters:
10024324Seric **		argv -- argument vector to send to.
100358247Seric **		e -- the current envelope.
10044324Seric **
10054324Seric **	Returns:
10064324Seric **		none.
10074324Seric **
10084324Seric **	Side Effects:
10094324Seric **		puts all addresses on the argument vector onto the
10104324Seric **			send queue.
10114324Seric */
10124324Seric 
101355012Seric sendtoargv(argv, e)
10144324Seric 	register char **argv;
101555012Seric 	register ENVELOPE *e;
10164324Seric {
10174324Seric 	register char *p;
10184324Seric 
10194324Seric 	while ((p = *argv++) != NULL)
10204324Seric 	{
1021*68457Seric 		(void) sendtolist(p, NULLADDR, &e->e_sendqueue, e);
10224324Seric 	}
10234324Seric }
10244399Seric /*
10254399Seric **  GETCTLADDR -- get controlling address from an address header.
10264399Seric **
10274399Seric **	If none, get one corresponding to the effective userid.
10284399Seric **
10294399Seric **	Parameters:
10304399Seric **		a -- the address to find the controller of.
10314399Seric **
10324399Seric **	Returns:
10334399Seric **		the controlling address.
10344399Seric **
10354399Seric **	Side Effects:
10364399Seric **		none.
10374399Seric */
10384399Seric 
10394399Seric ADDRESS *
10404399Seric getctladdr(a)
10414399Seric 	register ADDRESS *a;
10424399Seric {
10434404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
10444399Seric 		a = a->q_alias;
10454399Seric 	return (a);
10464399Seric }
1047