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*67827Seric static char sccsid[] = "@(#)recipient.c	8.49 (Berkeley) 10/17/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 	/* get unquoted user for file, program or user.name check */
18967264Seric 	i = strlen(a->q_user);
19067264Seric 	if (i >= sizeof buf)
19167264Seric 		buf = xalloc(i + 1);
19267264Seric 	else
19367264Seric 		buf = buf0;
1949210Seric 	(void) strcpy(buf, a->q_user);
1959210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1969210Seric 	{
19754993Seric 		if (*p == '\\')
1989210Seric 			quoted = TRUE;
1999210Seric 	}
20054983Seric 	stripquotes(buf);
2019210Seric 
20257402Seric 	/* check for direct mailing to restricted mailers */
20365496Seric 	if (m == ProgMailer)
2044174Seric 	{
20565496Seric 		if (a->q_alias == NULL)
20665496Seric 		{
20765496Seric 			a->q_flags |= QBADADDR;
20865496Seric 			usrerr("550 Cannot mail directly to programs");
20965496Seric 		}
21065496Seric 		else if (bitset(QBOGUSSHELL, a->q_alias->q_flags))
21165496Seric 		{
21265496Seric 			a->q_flags |= QBADADDR;
21365496Seric 			usrerr("550 User %s@%s doesn't have a valid shell for mailing to programs",
21465496Seric 				a->q_alias->q_ruser, MyHostName);
21565496Seric 		}
21665496Seric 		else if (bitset(QUNSAFEADDR, a->q_alias->q_flags))
21765496Seric 		{
21865496Seric 			a->q_flags |= QBADADDR;
21965496Seric 			usrerr("550 Address %s is unsafe for mailing to programs",
22065496Seric 				a->q_alias->q_paddr);
22165496Seric 		}
2224174Seric 	}
2234174Seric 
2244174Seric 	/*
2254419Seric 	**  Look up this person in the recipient list.
2264419Seric 	**	If they are there already, return, otherwise continue.
2274419Seric 	**	If the list is empty, just add it.  Notice the cute
2284419Seric 	**	hack to make from addresses suppress things correctly:
2294419Seric 	**	the QDONTSEND bit will be set in the send list.
2304419Seric 	**	[Please note: the emphasis is on "hack."]
2314174Seric 	*/
2324174Seric 
2335006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2344174Seric 	{
23558294Seric 		if (sameaddr(q, a))
2364174Seric 		{
2377676Seric 			if (tTd(26, 1))
2384444Seric 			{
2394444Seric 				printf("%s in sendq: ", a->q_paddr);
2404444Seric 				printaddr(q, FALSE);
2414444Seric 			}
24265593Seric 			if (!bitset(QPRIMARY, q->q_flags))
24358065Seric 			{
24465593Seric 				if (!bitset(QDONTSEND, a->q_flags))
24558151Seric 					message("duplicate suppressed");
24665593Seric 				q->q_flags |= a->q_flags;
24765593Seric 			}
24865593Seric 			else if (bitset(QSELFREF, q->q_flags))
24965579Seric 				q->q_flags |= a->q_flags & ~QDONTSEND;
25063847Seric 			a = q;
25163847Seric 			goto testselfdestruct;
2524174Seric 		}
2534319Seric 	}
2544174Seric 
2554319Seric 	/* add address on list */
25658884Seric 	*pq = a;
25758884Seric 	a->q_next = NULL;
2584174Seric 
2594174Seric 	/*
26057402Seric 	**  Alias the name and handle special mailer types.
2614174Seric 	*/
2624174Seric 
26353735Seric   trylocaluser:
26455354Seric 	if (tTd(29, 7))
26555354Seric 		printf("at trylocaluser %s\n", a->q_user);
26655354Seric 
26758680Seric 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
26863847Seric 		goto testselfdestruct;
26957402Seric 
27057402Seric 	if (m == InclMailer)
2714174Seric 	{
27257402Seric 		a->q_flags |= QDONTSEND;
27364761Seric 		if (a->q_alias == NULL)
2744174Seric 		{
27558680Seric 			a->q_flags |= QBADADDR;
27658151Seric 			usrerr("550 Cannot mail directly to :include:s");
2774174Seric 		}
2784174Seric 		else
27950556Seric 		{
28059563Seric 			int ret;
28158247Seric 
28258151Seric 			message("including file %s", a->q_user);
28359563Seric 			ret = include(a->q_user, FALSE, a, sendq, e);
28459563Seric 			if (transienterror(ret))
28559563Seric 			{
28659563Seric #ifdef LOG
28759563Seric 				if (LogLevel > 2)
28866239Seric 					syslog(LOG_ERR, "%s: include %s: transient error: %s",
28966284Seric 						e->e_id == NULL ? "NOQUEUE" : e->e_id,
29066284Seric 						a->q_user, errstring(ret));
29159563Seric #endif
29263853Seric 				a->q_flags |= QQUEUEUP;
29365215Seric 				a->q_flags &= ~QDONTSEND;
29459563Seric 				usrerr("451 Cannot open %s: %s",
29559563Seric 					a->q_user, errstring(ret));
29659563Seric 			}
29759563Seric 			else if (ret != 0)
29859563Seric 			{
29963938Seric 				a->q_flags |= QBADADDR;
30059563Seric 				usrerr("550 Cannot open %s: %s",
30159563Seric 					a->q_user, errstring(ret));
30259563Seric 			}
30350556Seric 		}
3044174Seric 	}
30557642Seric 	else if (m == FileMailer)
3064174Seric 	{
3074329Seric 		extern bool writable();
3084174Seric 
30951317Seric 		/* check if writable or creatable */
31064761Seric 		if (a->q_alias == NULL)
3114174Seric 		{
31258680Seric 			a->q_flags |= QBADADDR;
31358151Seric 			usrerr("550 Cannot mail directly to files");
3144174Seric 		}
31565496Seric 		else if (bitset(QBOGUSSHELL, a->q_alias->q_flags))
31665496Seric 		{
31765496Seric 			a->q_flags |= QBADADDR;
31865496Seric 			usrerr("550 User %s@%s doesn't have a valid shell for mailing to files",
31965496Seric 				a->q_alias->q_ruser, MyHostName);
32065496Seric 		}
32165496Seric 		else if (bitset(QUNSAFEADDR, a->q_alias->q_flags))
32265496Seric 		{
32365496Seric 			a->q_flags |= QBADADDR;
32465496Seric 			usrerr("550 Address %s is unsafe for mailing to files",
32565496Seric 				a->q_alias->q_paddr);
32665496Seric 		}
32765112Seric 		else if (!writable(buf, getctladdr(a), SFF_ANYFILE))
32851317Seric 		{
32958680Seric 			a->q_flags |= QBADADDR;
33064771Seric 			giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e);
33151317Seric 		}
33251317Seric 	}
33351317Seric 
33457402Seric 	/* try aliasing */
33567472Seric 	if (!bitset(QDONTSEND, a->q_flags) && bitnset(M_ALIASABLE, m->m_flags))
33667472Seric 		alias(a, sendq, e);
33757402Seric 
33857402Seric # ifdef USERDB
33957402Seric 	/* if not aliased, look it up in the user database */
34067472Seric 	if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags) &&
34167472Seric 	    bitnset(M_CHECKUDB, m->m_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",
35366284Seric 					e->e_id == NULL ? "NOQUEUE" : e->e_id,
35466284Seric 					errstring(errno));
35557402Seric # endif
35659615Seric 			message("queued (user database error): %s",
35759615Seric 				errstring(errno));
35857642Seric 			e->e_nrcpts++;
35963847Seric 			goto testselfdestruct;
36057402Seric 		}
36157402Seric 	}
36257402Seric # endif
36357402Seric 
36451317Seric 	/*
36551317Seric 	**  If we have a level two config file, then pass the name through
36651317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
36751317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
36851317Seric 	**  after local aliasing has been done.
36951317Seric 	*/
37051317Seric 
37151317Seric 	if (tTd(29, 5))
37251317Seric 	{
37351317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
37451317Seric 			ConfigLevel, RewriteRules[5]);
37551317Seric 		printaddr(a, FALSE);
37651317Seric 	}
37767472Seric 	if (!bitset(QNOTREMOTE|QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags) &&
37867472Seric 	    ConfigLevel >= 2 && RewriteRules[5] != NULL &&
37967472Seric 	    bitnset(M_TRYRULESET5, m->m_flags))
38051317Seric 	{
38155012Seric 		maplocaluser(a, sendq, e);
38251317Seric 	}
38351317Seric 
38451317Seric 	/*
38551317Seric 	**  If it didn't get rewritten to another mailer, go ahead
38651317Seric 	**  and deliver it.
38751317Seric 	*/
38851317Seric 
38967472Seric 	if (!bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags) &&
39067472Seric 	    bitnset(M_HASPWENT, m->m_flags))
39151317Seric 	{
39255354Seric 		auto bool fuzzy;
39351317Seric 		register struct passwd *pw;
39451317Seric 		extern struct passwd *finduser();
39551317Seric 
39651317Seric 		/* warning -- finduser may trash buf */
39755354Seric 		pw = finduser(buf, &fuzzy);
39851317Seric 		if (pw == NULL)
39951317Seric 		{
40058680Seric 			a->q_flags |= QBADADDR;
40164771Seric 			giveresponse(EX_NOUSER, m, NULL, a->q_alias, e);
40251317Seric 		}
4034174Seric 		else
4044174Seric 		{
40551317Seric 			char nbuf[MAXNAME];
4064373Seric 
40755354Seric 			if (fuzzy)
4084174Seric 			{
40953735Seric 				/* name was a fuzzy match */
41051317Seric 				a->q_user = newstr(pw->pw_name);
41153735Seric 				if (findusercount++ > 3)
41253735Seric 				{
41358680Seric 					a->q_flags |= QBADADDR;
41458151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
41553735Seric 						pw->pw_name);
41667264Seric 					goto done;
41753735Seric 				}
41853735Seric 
41953735Seric 				/* see if it aliases */
42051317Seric 				(void) strcpy(buf, pw->pw_name);
42153735Seric 				goto trylocaluser;
4224174Seric 			}
42365822Seric 			if (strcmp(pw->pw_dir, "/") == 0)
42465822Seric 				a->q_home = "";
42565822Seric 			else
42665822Seric 				a->q_home = newstr(pw->pw_dir);
42751317Seric 			a->q_uid = pw->pw_uid;
42851317Seric 			a->q_gid = pw->pw_gid;
42959083Seric 			a->q_ruser = newstr(pw->pw_name);
43051317Seric 			a->q_flags |= QGOODUID;
43151317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
43251317Seric 			if (nbuf[0] != '\0')
43351317Seric 				a->q_fullname = newstr(nbuf);
43465211Seric 			if (pw->pw_shell != NULL && pw->pw_shell[0] != '\0' &&
43565211Seric 			    !usershellok(pw->pw_shell))
43665206Seric 			{
43765211Seric 				a->q_flags |= QBOGUSSHELL;
43865206Seric 			}
43951317Seric 			if (!quoted)
44055012Seric 				forward(a, sendq, e);
4414174Seric 		}
4424174Seric 	}
44357642Seric 	if (!bitset(QDONTSEND, a->q_flags))
44457642Seric 		e->e_nrcpts++;
44563847Seric 
44663847Seric   testselfdestruct:
44763978Seric 	if (tTd(26, 8))
44863847Seric 	{
44963978Seric 		printf("testselfdestruct: ");
45063978Seric 		printaddr(a, TRUE);
45163978Seric 	}
45263978Seric 	if (a->q_alias == NULL && a != &e->e_from &&
45363978Seric 	    bitset(QDONTSEND, a->q_flags))
45463978Seric 	{
45563978Seric 		q = *sendq;
45663965Seric 		while (q != NULL && bitset(QDONTSEND, q->q_flags))
45763847Seric 			q = q->q_next;
45863978Seric 		if (q == NULL)
45963847Seric 		{
46063847Seric 			a->q_flags |= QBADADDR;
46163847Seric 			usrerr("554 aliasing/forwarding loop broken");
46263847Seric 		}
46363847Seric 	}
46467264Seric 
46567264Seric   done:
46667264Seric 	if (buf != buf0)
46767264Seric 		free(buf);
46812613Seric 	return (a);
4694174Seric }
4704174Seric /*
4714373Seric **  FINDUSER -- find the password entry for a user.
4724373Seric **
4734373Seric **	This looks a lot like getpwnam, except that it may want to
4744373Seric **	do some fancier pattern matching in /etc/passwd.
4754373Seric **
4769379Seric **	This routine contains most of the time of many sendmail runs.
4779379Seric **	It deserves to be optimized.
4789379Seric **
4794373Seric **	Parameters:
4804373Seric **		name -- the name to match against.
48155354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
48255354Seric **			was found using the fuzzy matching algorithm;
48355354Seric **			set to FALSE otherwise.
4844373Seric **
4854373Seric **	Returns:
4864373Seric **		A pointer to a pw struct.
4874373Seric **		NULL if name is unknown or ambiguous.
4884373Seric **
4894373Seric **	Side Effects:
4904407Seric **		may modify name.
4914373Seric */
4924373Seric 
4934373Seric struct passwd *
49455354Seric finduser(name, fuzzyp)
4954373Seric 	char *name;
49655354Seric 	bool *fuzzyp;
4974373Seric {
4984376Seric 	register struct passwd *pw;
4994407Seric 	register char *p;
50015325Seric 	extern struct passwd *getpwent();
50115325Seric 	extern struct passwd *getpwnam();
5024373Seric 
50355354Seric 	if (tTd(29, 4))
50455354Seric 		printf("finduser(%s): ", name);
50555354Seric 
50655354Seric 	*fuzzyp = FALSE;
5074407Seric 
50864673Seric 	/* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */
50964673Seric 	for (p = name; *p != '\0'; p++)
51064673Seric 		if (!isascii(*p) || !isdigit(*p))
51164673Seric 			break;
51264673Seric 	if (*p == '\0')
51364673Seric 	{
51464673Seric 		if (tTd(29, 4))
51564673Seric 			printf("failed (numeric input)\n");
51664673Seric 		return NULL;
51764673Seric 	}
51864673Seric 
51925777Seric 	/* look up this login name using fast path */
52012634Seric 	if ((pw = getpwnam(name)) != NULL)
52155354Seric 	{
52255354Seric 		if (tTd(29, 4))
52355354Seric 			printf("found (non-fuzzy)\n");
52412634Seric 		return (pw);
52555354Seric 	}
52612634Seric 
52753735Seric #ifdef MATCHGECOS
52853735Seric 	/* see if fuzzy matching allowed */
52953735Seric 	if (!MatchGecos)
53055354Seric 	{
53155354Seric 		if (tTd(29, 4))
53255354Seric 			printf("not found (fuzzy disabled)\n");
53353735Seric 		return NULL;
53455354Seric 	}
53553735Seric 
53612634Seric 	/* search for a matching full name instead */
53725777Seric 	for (p = name; *p != '\0'; p++)
53825777Seric 	{
53925777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
54025777Seric 			*p = ' ';
54125777Seric 	}
54223107Seric 	(void) setpwent();
5434376Seric 	while ((pw = getpwent()) != NULL)
5444376Seric 	{
5454998Seric 		char buf[MAXNAME];
5464376Seric 
5474998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
54856795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
5494381Seric 		{
55055354Seric 			if (tTd(29, 4))
55155354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
55258151Seric 			message("sending to login name %s", pw->pw_name);
55355354Seric 			*fuzzyp = TRUE;
5544376Seric 			return (pw);
5554377Seric 		}
5564376Seric 	}
55755354Seric 	if (tTd(29, 4))
55855354Seric 		printf("no fuzzy match found\n");
55959015Seric #else
56059015Seric 	if (tTd(29, 4))
56159015Seric 		printf("not found (fuzzy disabled)\n");
56259015Seric #endif
5634376Seric 	return (NULL);
5644373Seric }
5654373Seric /*
5664329Seric **  WRITABLE -- predicate returning if the file is writable.
5674329Seric **
5684329Seric **	This routine must duplicate the algorithm in sys/fio.c.
5694329Seric **	Unfortunately, we cannot use the access call since we
5704329Seric **	won't necessarily be the real uid when we try to
5714329Seric **	actually open the file.
5724329Seric **
5734329Seric **	Notice that ANY file with ANY execute bit is automatically
5744329Seric **	not writable.  This is also enforced by mailfile.
5754329Seric **
5764329Seric **	Parameters:
57765064Seric **		filename -- the file name to check.
57865112Seric **		ctladdr -- the controlling address for this file.
57965064Seric **		flags -- SFF_* flags to control the function.
5804329Seric **
5814329Seric **	Returns:
5824329Seric **		TRUE -- if we will be able to write this file.
5834329Seric **		FALSE -- if we cannot write this file.
5844329Seric **
5854329Seric **	Side Effects:
5864329Seric **		none.
5874329Seric */
5884329Seric 
5894329Seric bool
59065112Seric writable(filename, ctladdr, flags)
59164819Seric 	char *filename;
59265112Seric 	ADDRESS *ctladdr;
59365064Seric 	int flags;
5944329Seric {
59555372Seric 	uid_t euid;
59655372Seric 	gid_t egid;
5974329Seric 	int bits;
59864944Seric 	register char *p;
59964944Seric 	char *uname;
60064944Seric 	struct stat stb;
60164944Seric 	extern char RealUserName[];
6024329Seric 
60364819Seric 	if (tTd(29, 5))
60465064Seric 		printf("writable(%s, %x)\n", filename, flags);
60564944Seric 
60664944Seric #ifdef HASLSTAT
60765064Seric 	if ((bitset(SFF_NOSLINK, flags) ? lstat(filename, &stb)
60865064Seric 					: stat(filename, &stb)) < 0)
60964944Seric #else
61064944Seric 	if (stat(filename, &stb) < 0)
61164944Seric #endif
61264944Seric 	{
61364944Seric 		/* file does not exist -- see if directory is safe */
61464944Seric 		p = strrchr(filename, '/');
61564944Seric 		if (p == NULL)
61664944Seric 		{
61765067Seric 			errno = ENOTDIR;
61864944Seric 			return FALSE;
61964944Seric 		}
62065067Seric 		*p = '\0';
62165067Seric 		errno = safefile(filename, RealUid, RealGid, RealUserName,
62265067Seric 				 SFF_MUSTOWN, S_IWRITE|S_IEXEC);
62364944Seric 		*p = '/';
62465067Seric 		return errno == 0;
62564944Seric 	}
62664944Seric 
62765225Seric #ifdef SUID_ROOT_FILES_OK
62865225Seric 	/* really ought to be passed down -- and not a good idea */
62965225Seric 	flags |= SFF_ROOTOK;
63065225Seric #endif
63165225Seric 
63264944Seric 	/*
63364944Seric 	**  File does exist -- check that it is writable.
63464944Seric 	*/
63564944Seric 
63664944Seric 	if (bitset(0111, stb.st_mode))
63765022Seric 	{
63865022Seric 		if (tTd(29, 5))
63965022Seric 			printf("failed (mode %o: x bits)\n", stb.st_mode);
64065067Seric 		errno = EPERM;
6414329Seric 		return (FALSE);
64265022Seric 	}
64364944Seric 
64465112Seric 	if (ctladdr != NULL && geteuid() == 0)
64564944Seric 	{
64665112Seric 		euid = ctladdr->q_uid;
64765112Seric 		egid = ctladdr->q_gid;
64865112Seric 		uname = ctladdr->q_user;
64964944Seric 	}
65065112Seric 	else
65165112Seric 	{
65265112Seric 		euid = RealUid;
65365112Seric 		egid = RealGid;
65465112Seric 		uname = RealUserName;
65565112Seric 	}
65665138Seric 	if (euid == 0)
65765138Seric 	{
65865138Seric 		euid = DefUid;
65965138Seric 		uname = DefUser;
66065138Seric 	}
66165138Seric 	if (egid == 0)
66265138Seric 		egid = DefGid;
6634329Seric 	if (geteuid() == 0)
6644329Seric 	{
66565225Seric 		if (bitset(S_ISUID, stb.st_mode) &&
66665225Seric 		    (stb.st_uid != 0 || bitset(SFF_ROOTOK, flags)))
66764944Seric 		{
66864944Seric 			euid = stb.st_uid;
66964944Seric 			uname = NULL;
67064944Seric 		}
67165225Seric 		if (bitset(S_ISGID, stb.st_mode) &&
67265225Seric 		    (stb.st_gid != 0 || bitset(SFF_ROOTOK, flags)))
67364944Seric 			egid = stb.st_gid;
6744329Seric 	}
6754329Seric 
67664819Seric 	if (tTd(29, 5))
67764819Seric 		printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n",
67864944Seric 			euid, egid, stb.st_uid, stb.st_gid);
67964819Seric 
68065067Seric 	errno = safefile(filename, euid, egid, uname, flags, S_IWRITE);
68165067Seric 	return errno == 0;
6824329Seric }
6834329Seric /*
6844174Seric **  INCLUDE -- handle :include: specification.
6854174Seric **
6864174Seric **	Parameters:
6874174Seric **		fname -- filename to include.
68853037Seric **		forwarding -- if TRUE, we are reading a .forward file.
68953037Seric **			if FALSE, it's a :include: file.
6904399Seric **		ctladdr -- address template to use to fill in these
6914399Seric **			addresses -- effective user/group id are
6924399Seric **			the important things.
6935006Seric **		sendq -- a pointer to the head of the send queue
6945006Seric **			to put these addresses in.
6954174Seric **
6964174Seric **	Returns:
69757136Seric **		open error status
6984174Seric **
6994174Seric **	Side Effects:
7004174Seric **		reads the :include: file and sends to everyone
7014174Seric **		listed in that file.
70265909Seric **
70365909Seric **	Security Note:
70465909Seric **		If you have restricted chown (that is, you can't
70565909Seric **		give a file away), it is reasonable to allow programs
70665909Seric **		and files called from this :include: file to be to be
70765909Seric **		run as the owner of the :include: file.  This is bogus
70865909Seric **		if there is any chance of someone giving away a file.
70965909Seric **		We assume that pre-POSIX systems can give away files.
71065909Seric **
71165909Seric **		There is an additional restriction that if you
71265909Seric **		forward to a :include: file, it will not take on
71365909Seric **		the ownership of the :include: file.  This may not
71465909Seric **		be necessary, but shouldn't hurt.
7154174Seric */
7164174Seric 
71753037Seric static jmp_buf	CtxIncludeTimeout;
71863937Seric static int	includetimeout();
71953037Seric 
72065496Seric #ifndef S_IWOTH
72165496Seric # define S_IWOTH	(S_IWRITE >> 6)
72265496Seric #endif
72365496Seric 
72457136Seric int
72555012Seric include(fname, forwarding, ctladdr, sendq, e)
7264174Seric 	char *fname;
72753037Seric 	bool forwarding;
7284399Seric 	ADDRESS *ctladdr;
7295006Seric 	ADDRESS **sendq;
73055012Seric 	ENVELOPE *e;
7314174Seric {
73264570Seric 	register FILE *fp = NULL;
73355012Seric 	char *oldto = e->e_to;
7349379Seric 	char *oldfilename = FileName;
7359379Seric 	int oldlinenumber = LineNumber;
73653037Seric 	register EVENT *ev = NULL;
73758082Seric 	int nincludes;
73864325Seric 	register ADDRESS *ca;
73964325Seric 	uid_t saveduid, uid;
74064325Seric 	gid_t savedgid, gid;
74164083Seric 	char *uname;
74264325Seric 	int rval = 0;
74365064Seric 	int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE;
74465496Seric 	struct stat st;
74565948Seric 	char buf[MAXLINE];
74665909Seric #ifdef _POSIX_CHOWN_RESTRICTED
74765948Seric # if _POSIX_CHOWN_RESTRICTED == -1
74865948Seric #  define safechown	FALSE
74965948Seric # else
75065948Seric #  define safechown	TRUE
75165948Seric # endif
75265948Seric #else
75365948Seric # ifdef _PC_CHOWN_RESTRICTED
75465909Seric 	bool safechown;
75565948Seric # else
75665948Seric #  ifdef BSD
75765948Seric #   define safechown	TRUE
75865948Seric #  else
75965948Seric #   define safechown	FALSE
76065948Seric #  endif
76165948Seric # endif
76265909Seric #endif
76365948Seric 	extern bool chownsafe();
7644174Seric 
76557186Seric 	if (tTd(27, 2))
76657186Seric 		printf("include(%s)\n", fname);
76763902Seric 	if (tTd(27, 4))
76863902Seric 		printf("   ruid=%d euid=%d\n", getuid(), geteuid());
76963581Seric 	if (tTd(27, 14))
77063581Seric 	{
77163581Seric 		printf("ctladdr ");
77263581Seric 		printaddr(ctladdr, FALSE);
77363581Seric 	}
77457186Seric 
77564325Seric 	if (tTd(27, 9))
77664325Seric 		printf("include: old uid = %d/%d\n", getuid(), geteuid());
77753037Seric 
77863581Seric 	ca = getctladdr(ctladdr);
77963581Seric 	if (ca == NULL)
78064083Seric 	{
78164846Seric 		uid = DefUid;
78264846Seric 		gid = DefGid;
78364846Seric 		uname = DefUser;
78464325Seric 		saveduid = -1;
78564083Seric 	}
78663581Seric 	else
78764083Seric 	{
78863581Seric 		uid = ca->q_uid;
78964083Seric 		gid = ca->q_gid;
79064083Seric 		uname = ca->q_user;
79164325Seric #ifdef HASSETREUID
79264325Seric 		saveduid = geteuid();
79364325Seric 		savedgid = getegid();
79464325Seric 		if (saveduid == 0)
79564325Seric 		{
79664325Seric 			initgroups(uname, gid);
79764325Seric 			if (uid != 0)
798*67827Seric 			{
799*67827Seric 				if (setreuid(0, uid) < 0)
800*67827Seric 					syserr("setreuid(0, %d) failure (real=%d, eff=%d)",
801*67827Seric 						uid, getuid(), geteuid());
802*67827Seric 			}
80364325Seric 		}
80464325Seric #endif
80564083Seric 	}
80663581Seric 
80764325Seric 	if (tTd(27, 9))
80864325Seric 		printf("include: new uid = %d/%d\n", getuid(), geteuid());
80964325Seric 
81064325Seric 	/*
81164325Seric 	**  If home directory is remote mounted but server is down,
81264325Seric 	**  this can hang or give errors; use a timeout to avoid this
81364325Seric 	*/
81464325Seric 
81553037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
81653037Seric 	{
81763853Seric 		ctladdr->q_flags |= QQUEUEUP;
81853037Seric 		errno = 0;
81963993Seric 
82063993Seric 		/* return pseudo-error code */
82164325Seric 		rval = EOPENTIMEOUT;
82264325Seric 		goto resetuid;
82353037Seric 	}
82467711Seric 	if (TimeOuts.to_fileopen > 0)
82567711Seric 		ev = setevent(TimeOuts.to_fileopen, includetimeout, 0);
82667711Seric 	else
82767711Seric 		ev = NULL;
82853037Seric 
82963581Seric 	/* the input file must be marked safe */
83064944Seric 	rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD);
83164329Seric 	if (rval != 0)
83253037Seric 	{
83364325Seric 		/* don't use this :include: file */
83457186Seric 		if (tTd(27, 4))
83558247Seric 			printf("include: not safe (uid=%d): %s\n",
83664329Seric 				uid, errstring(rval));
83753037Seric 	}
83865496Seric 	else
8394174Seric 	{
84065496Seric 		fp = fopen(fname, "r");
84165496Seric 		if (fp == NULL)
84258061Seric 		{
84364329Seric 			rval = errno;
84465496Seric 			if (tTd(27, 4))
84565496Seric 				printf("include: open: %s\n", errstring(rval));
84658061Seric 		}
8474406Seric 	}
84867711Seric 	if (ev != NULL)
84967711Seric 		clrevent(ev);
85053037Seric 
85164570Seric resetuid:
85264570Seric 
85364570Seric #ifdef HASSETREUID
85464570Seric 	if (saveduid == 0)
85564570Seric 	{
85664570Seric 		if (uid != 0)
857*67827Seric 		{
858*67827Seric 			if (setreuid(-1, 0) < 0)
859*67827Seric 				syserr("setreuid(-1, 0) failure (real=%d, eff=%d)",
860*67827Seric 					getuid(), geteuid());
861*67827Seric 			if (setreuid(RealUid, 0) < 0)
86264570Seric 				syserr("setreuid(%d, 0) failure (real=%d, eff=%d)",
86364570Seric 					RealUid, getuid(), geteuid());
864*67827Seric 		}
86564570Seric 		setgid(savedgid);
86664570Seric 	}
86764570Seric #endif
86864570Seric 
86964570Seric 	if (tTd(27, 9))
87064570Seric 		printf("include: reset uid = %d/%d\n", getuid(), geteuid());
87164570Seric 
87265593Seric 	if (rval == EOPENTIMEOUT)
87365593Seric 		usrerr("451 open timeout on %s", fname);
87465593Seric 
87564570Seric 	if (fp == NULL)
87664570Seric 		return rval;
87764570Seric 
87865496Seric 	if (fstat(fileno(fp), &st) < 0)
87965496Seric 	{
88065496Seric 		rval = errno;
88165496Seric 		syserr("Cannot fstat %s!", fname);
88265496Seric 		return rval;
88365496Seric 	}
88465496Seric 
88565948Seric #ifndef safechown
88665948Seric 	safechown = chownsafe(fileno(fp));
88765948Seric #endif
88865909Seric 	if (ca == NULL && safechown)
88965496Seric 	{
89065496Seric 		ctladdr->q_uid = st.st_uid;
89165496Seric 		ctladdr->q_gid = st.st_gid;
89265496Seric 		ctladdr->q_flags |= QGOODUID;
89365496Seric 	}
89465496Seric 	if (ca != NULL && ca->q_uid == st.st_uid)
89565496Seric 	{
89665496Seric 		/* optimization -- avoid getpwuid if we already have info */
89765496Seric 		ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL;
89865496Seric 		ctladdr->q_ruser = ca->q_ruser;
89965496Seric 	}
90065496Seric 	else
90165496Seric 	{
90265909Seric 		char *sh;
90365496Seric 		register struct passwd *pw;
90465496Seric 
90565909Seric 		sh = "/SENDMAIL/ANY/SHELL/";
90665496Seric 		pw = getpwuid(st.st_uid);
90765909Seric 		if (pw != NULL)
90865496Seric 		{
90965496Seric 			ctladdr->q_ruser = newstr(pw->pw_name);
91065909Seric 			if (safechown)
91165909Seric 				sh = pw->pw_shell;
91265909Seric 		}
91365909Seric 		if (pw == NULL)
91465496Seric 			ctladdr->q_flags |= QBOGUSSHELL;
91565909Seric 		else if(!usershellok(sh))
91665909Seric 		{
91765909Seric 			if (safechown)
91865909Seric 				ctladdr->q_flags |= QBOGUSSHELL;
91965909Seric 			else
92065909Seric 				ctladdr->q_flags |= QUNSAFEADDR;
92165496Seric 		}
92265496Seric 	}
92365496Seric 
92458092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
92558092Seric 	{
92658092Seric 		/* don't do any more now */
92758868Seric 		ctladdr->q_flags |= QVERIFIED;
92858884Seric 		e->e_nrcpts++;
92958680Seric 		xfclose(fp, "include", fname);
93064570Seric 		return rval;
93158092Seric 	}
93258092Seric 
93365496Seric 	/*
93465496Seric 	** Check to see if some bad guy can write this file
93565496Seric 	**
93665496Seric 	**	This should really do something clever with group
93765496Seric 	**	permissions; currently we just view world writable
93865496Seric 	**	as unsafe.  Also, we don't check for writable
93965496Seric 	**	directories in the path.  We've got to leave
94065496Seric 	**	something for the local sysad to do.
94165496Seric 	*/
94265496Seric 
94365496Seric 	if (bitset(S_IWOTH, st.st_mode))
94465496Seric 		ctladdr->q_flags |= QUNSAFEADDR;
94565496Seric 
9464174Seric 	/* read the file -- each line is a comma-separated list. */
9479379Seric 	FileName = fname;
9489379Seric 	LineNumber = 0;
94958082Seric 	ctladdr->q_flags &= ~QSELFREF;
95058082Seric 	nincludes = 0;
9514174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
9524174Seric 	{
95356795Seric 		register char *p = strchr(buf, '\n');
9544174Seric 
95540963Sbostic 		LineNumber++;
9564174Seric 		if (p != NULL)
9574174Seric 			*p = '\0';
95857186Seric 		if (buf[0] == '#' || buf[0] == '\0')
95957139Seric 			continue;
96058008Seric 		e->e_to = NULL;
96158151Seric 		message("%s to %s",
96253037Seric 			forwarding ? "forwarding" : "sending", buf);
96357977Seric #ifdef LOG
96458020Seric 		if (forwarding && LogLevel > 9)
96557977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
96666284Seric 				e->e_id == NULL ? "NOQUEUE" : e->e_id,
96766284Seric 				oldto, buf);
96857977Seric #endif
96957977Seric 
9704176Seric 		AliasLevel++;
97158082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
9724176Seric 		AliasLevel--;
9734174Seric 	}
97463902Seric 
97563902Seric 	if (ferror(fp) && tTd(27, 3))
97663902Seric 		printf("include: read error: %s\n", errstring(errno));
97758082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
97858065Seric 	{
97958065Seric 		if (tTd(27, 5))
98058065Seric 		{
98158065Seric 			printf("include: QDONTSEND ");
98258065Seric 			printaddr(ctladdr, FALSE);
98358065Seric 		}
98458065Seric 		ctladdr->q_flags |= QDONTSEND;
98558065Seric 	}
9864174Seric 
98758680Seric 	(void) xfclose(fp, "include", fname);
9889379Seric 	FileName = oldfilename;
9899379Seric 	LineNumber = oldlinenumber;
99063847Seric 	e->e_to = oldto;
99164325Seric 	return rval;
9924174Seric }
99353037Seric 
99453037Seric static
99553037Seric includetimeout()
99653037Seric {
99753037Seric 	longjmp(CtxIncludeTimeout, 1);
99853037Seric }
9994324Seric /*
10004324Seric **  SENDTOARGV -- send to an argument vector.
10014324Seric **
10024324Seric **	Parameters:
10034324Seric **		argv -- argument vector to send to.
100458247Seric **		e -- the current envelope.
10054324Seric **
10064324Seric **	Returns:
10074324Seric **		none.
10084324Seric **
10094324Seric **	Side Effects:
10104324Seric **		puts all addresses on the argument vector onto the
10114324Seric **			send queue.
10124324Seric */
10134324Seric 
101455012Seric sendtoargv(argv, e)
10154324Seric 	register char **argv;
101655012Seric 	register ENVELOPE *e;
10174324Seric {
10184324Seric 	register char *p;
10194324Seric 
10204324Seric 	while ((p = *argv++) != NULL)
10214324Seric 	{
102264284Seric 		(void) sendtolist(p, NULLADDR, &e->e_sendqueue, e);
10234324Seric 	}
10244324Seric }
10254399Seric /*
10264399Seric **  GETCTLADDR -- get controlling address from an address header.
10274399Seric **
10284399Seric **	If none, get one corresponding to the effective userid.
10294399Seric **
10304399Seric **	Parameters:
10314399Seric **		a -- the address to find the controller of.
10324399Seric **
10334399Seric **	Returns:
10344399Seric **		the controlling address.
10354399Seric **
10364399Seric **	Side Effects:
10374399Seric **		none.
10384399Seric */
10394399Seric 
10404399Seric ADDRESS *
10414399Seric getctladdr(a)
10424399Seric 	register ADDRESS *a;
10434399Seric {
10444404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
10454399Seric 		a = a->q_alias;
10464399Seric 	return (a);
10474399Seric }
1048