17c478bd9Sstevel@tonic-gate /*
2058561cbSjbeck  * Copyright (c) 1998-2003, 2006 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1993
67c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
97c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
107c478bd9Sstevel@tonic-gate  * the sendmail distribution.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #include <sendmail.h>
157c478bd9Sstevel@tonic-gate 
16*7800901eSjbeck SM_RCSID("@(#)$Id: recipient.c,v 8.349 2007/07/10 17:01:22 ca Exp $")
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate static void	includetimeout __P((int));
197c478bd9Sstevel@tonic-gate static ADDRESS	*self_reference __P((ADDRESS *));
207c478bd9Sstevel@tonic-gate static int	sortexpensive __P((ADDRESS *, ADDRESS *));
217c478bd9Sstevel@tonic-gate static int	sortbysignature __P((ADDRESS *, ADDRESS *));
227c478bd9Sstevel@tonic-gate static int	sorthost __P((ADDRESS *, ADDRESS *));
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate typedef int	sortfn_t __P((ADDRESS *, ADDRESS *));
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate **  SORTHOST -- strcmp()-like func for host portion of an ADDRESS
287c478bd9Sstevel@tonic-gate **
297c478bd9Sstevel@tonic-gate **	Parameters:
307c478bd9Sstevel@tonic-gate **		xx -- first ADDRESS
317c478bd9Sstevel@tonic-gate **		yy -- second ADDRESS
327c478bd9Sstevel@tonic-gate **
337c478bd9Sstevel@tonic-gate **	Returns:
347c478bd9Sstevel@tonic-gate **		<0 when xx->q_host is less than yy->q_host
357c478bd9Sstevel@tonic-gate **		>0 when xx->q_host is greater than yy->q_host
367c478bd9Sstevel@tonic-gate **		0 when equal
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static int
sorthost(xx,yy)407c478bd9Sstevel@tonic-gate sorthost(xx, yy)
417c478bd9Sstevel@tonic-gate 	register ADDRESS *xx;
427c478bd9Sstevel@tonic-gate 	register ADDRESS *yy;
437c478bd9Sstevel@tonic-gate {
447c478bd9Sstevel@tonic-gate #if _FFR_HOST_SORT_REVERSE
457c478bd9Sstevel@tonic-gate 	/* XXX maybe compare hostnames from the end? */
467c478bd9Sstevel@tonic-gate 	return sm_strrevcasecmp(xx->q_host, yy->q_host);
477c478bd9Sstevel@tonic-gate #else /* _FFR_HOST_SORT_REVERSE */
487c478bd9Sstevel@tonic-gate 	return sm_strcasecmp(xx->q_host, yy->q_host);
497c478bd9Sstevel@tonic-gate #endif /* _FFR_HOST_SORT_REVERSE */
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate **  SORTEXPENSIVE -- strcmp()-like func for expensive mailers
547c478bd9Sstevel@tonic-gate **
557c478bd9Sstevel@tonic-gate **  The mailer has been noted already as "expensive" for 'xx'. This
567c478bd9Sstevel@tonic-gate **  will give a result relative to 'yy'. Expensive mailers get rated
577c478bd9Sstevel@tonic-gate **  "greater than" non-expensive mailers because during the delivery phase
587c478bd9Sstevel@tonic-gate **  it will get queued -- no use it getting in the way of less expensive
597c478bd9Sstevel@tonic-gate **  recipients. We avoid an MX RR lookup when both 'xx' and 'yy' are
607c478bd9Sstevel@tonic-gate **  expensive since an MX RR lookup happens when extracted from the queue
617c478bd9Sstevel@tonic-gate **  later.
627c478bd9Sstevel@tonic-gate **
637c478bd9Sstevel@tonic-gate **	Parameters:
647c478bd9Sstevel@tonic-gate **		xx -- first ADDRESS
657c478bd9Sstevel@tonic-gate **		yy -- second ADDRESS
667c478bd9Sstevel@tonic-gate **
677c478bd9Sstevel@tonic-gate **	Returns:
687c478bd9Sstevel@tonic-gate **		<0 when xx->q_host is less than yy->q_host and both are
697c478bd9Sstevel@tonic-gate **			expensive
707c478bd9Sstevel@tonic-gate **		>0 when xx->q_host is greater than yy->q_host, or when
717c478bd9Sstevel@tonic-gate **			'yy' is non-expensive
727c478bd9Sstevel@tonic-gate **		0 when equal (by expense and q_host)
737c478bd9Sstevel@tonic-gate */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static int
sortexpensive(xx,yy)767c478bd9Sstevel@tonic-gate sortexpensive(xx, yy)
777c478bd9Sstevel@tonic-gate 	ADDRESS *xx;
787c478bd9Sstevel@tonic-gate 	ADDRESS *yy;
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	if (!bitnset(M_EXPENSIVE, yy->q_mailer->m_flags))
817c478bd9Sstevel@tonic-gate 		return 1; /* xx should go later */
827c478bd9Sstevel@tonic-gate #if _FFR_HOST_SORT_REVERSE
837c478bd9Sstevel@tonic-gate 	/* XXX maybe compare hostnames from the end? */
847c478bd9Sstevel@tonic-gate 	return sm_strrevcasecmp(xx->q_host, yy->q_host);
857c478bd9Sstevel@tonic-gate #else /* _FFR_HOST_SORT_REVERSE */
867c478bd9Sstevel@tonic-gate 	return sm_strcasecmp(xx->q_host, yy->q_host);
877c478bd9Sstevel@tonic-gate #endif /* _FFR_HOST_SORT_REVERSE */
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate **  SORTBYSIGNATURE -- a strcmp()-like func for q_mailer and q_host in ADDRESS
927c478bd9Sstevel@tonic-gate **
937c478bd9Sstevel@tonic-gate **	Parameters:
947c478bd9Sstevel@tonic-gate **		xx -- first ADDRESS
957c478bd9Sstevel@tonic-gate **		yy -- second ADDRESS
967c478bd9Sstevel@tonic-gate **
977c478bd9Sstevel@tonic-gate **	Returns:
987c478bd9Sstevel@tonic-gate **		0 when the "signature"'s are same
997c478bd9Sstevel@tonic-gate **		<0 when xx->q_signature is less than yy->q_signature
1007c478bd9Sstevel@tonic-gate **		>0 when xx->q_signature is greater than yy->q_signature
1017c478bd9Sstevel@tonic-gate **
1027c478bd9Sstevel@tonic-gate **	Side Effect:
1037c478bd9Sstevel@tonic-gate **		May set ADDRESS pointer for q_signature if not already set.
1047c478bd9Sstevel@tonic-gate */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static int
sortbysignature(xx,yy)1077c478bd9Sstevel@tonic-gate sortbysignature(xx, yy)
1087c478bd9Sstevel@tonic-gate 	ADDRESS *xx;
1097c478bd9Sstevel@tonic-gate 	ADDRESS *yy;
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	register int ret;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/* Let's avoid redoing the signature over and over again */
1147c478bd9Sstevel@tonic-gate 	if (xx->q_signature == NULL)
1157c478bd9Sstevel@tonic-gate 		xx->q_signature = hostsignature(xx->q_mailer, xx->q_host);
1167c478bd9Sstevel@tonic-gate 	if (yy->q_signature == NULL)
1177c478bd9Sstevel@tonic-gate 		yy->q_signature = hostsignature(yy->q_mailer, yy->q_host);
1187c478bd9Sstevel@tonic-gate 	ret = strcmp(xx->q_signature, yy->q_signature);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	**  If the two signatures are the same then we will return a sort
1227c478bd9Sstevel@tonic-gate 	**  value based on 'q_user'. But note that we have reversed xx and yy
1237c478bd9Sstevel@tonic-gate 	**  on purpose. This additional compare helps reduce the number of
1247c478bd9Sstevel@tonic-gate 	**  sameaddr() calls and loops in recipient() for the case when
1257c478bd9Sstevel@tonic-gate 	**  the rcpt list has been provided already in-order.
1267c478bd9Sstevel@tonic-gate 	*/
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (ret == 0)
1297c478bd9Sstevel@tonic-gate 		return strcmp(yy->q_user, xx->q_user);
1307c478bd9Sstevel@tonic-gate 	else
1317c478bd9Sstevel@tonic-gate 		return ret;
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate **  SENDTOLIST -- Designate a send list.
1367c478bd9Sstevel@tonic-gate **
1377c478bd9Sstevel@tonic-gate **	The parameter is a comma-separated list of people to send to.
1387c478bd9Sstevel@tonic-gate **	This routine arranges to send to all of them.
1397c478bd9Sstevel@tonic-gate **
1407c478bd9Sstevel@tonic-gate **	Parameters:
1417c478bd9Sstevel@tonic-gate **		list -- the send list.
1427c478bd9Sstevel@tonic-gate **		ctladdr -- the address template for the person to
1437c478bd9Sstevel@tonic-gate **			send to -- effective uid/gid are important.
1447c478bd9Sstevel@tonic-gate **			This is typically the alias that caused this
1457c478bd9Sstevel@tonic-gate **			expansion.
1467c478bd9Sstevel@tonic-gate **		sendq -- a pointer to the head of a queue to put
1477c478bd9Sstevel@tonic-gate **			these people into.
1487c478bd9Sstevel@tonic-gate **		aliaslevel -- the current alias nesting depth -- to
1497c478bd9Sstevel@tonic-gate **			diagnose loops.
1507c478bd9Sstevel@tonic-gate **		e -- the envelope in which to add these recipients.
1517c478bd9Sstevel@tonic-gate **
1527c478bd9Sstevel@tonic-gate **	Returns:
1537c478bd9Sstevel@tonic-gate **		The number of addresses actually on the list.
1547c478bd9Sstevel@tonic-gate */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /* q_flags bits inherited from ctladdr */
1577c478bd9Sstevel@tonic-gate #define QINHERITEDBITS	(QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY|QHASNOTIFY)
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate int
sendtolist(list,ctladdr,sendq,aliaslevel,e)1607c478bd9Sstevel@tonic-gate sendtolist(list, ctladdr, sendq, aliaslevel, e)
1617c478bd9Sstevel@tonic-gate 	char *list;
1627c478bd9Sstevel@tonic-gate 	ADDRESS *ctladdr;
1637c478bd9Sstevel@tonic-gate 	ADDRESS **sendq;
1647c478bd9Sstevel@tonic-gate 	int aliaslevel;
1657c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	register char *p;
1687c478bd9Sstevel@tonic-gate 	register ADDRESS *SM_NONVOLATILE al; /* list of addresses to send to */
1697c478bd9Sstevel@tonic-gate 	SM_NONVOLATILE char delimiter;		/* the address delimiter */
1707c478bd9Sstevel@tonic-gate 	SM_NONVOLATILE int naddrs;
1717c478bd9Sstevel@tonic-gate 	SM_NONVOLATILE int i;
1727c478bd9Sstevel@tonic-gate 	char *endp;
1737c478bd9Sstevel@tonic-gate 	char *oldto = e->e_to;
1747c478bd9Sstevel@tonic-gate 	char *SM_NONVOLATILE bufp;
1757c478bd9Sstevel@tonic-gate 	char buf[MAXNAME + 1];
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (list == NULL)
1787c478bd9Sstevel@tonic-gate 	{
1797c478bd9Sstevel@tonic-gate 		syserr("sendtolist: null list");
1807c478bd9Sstevel@tonic-gate 		return 0;
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (tTd(25, 1))
1847c478bd9Sstevel@tonic-gate 	{
1857c478bd9Sstevel@tonic-gate 		sm_dprintf("sendto: %s\n   ctladdr=", list);
1867c478bd9Sstevel@tonic-gate 		printaddr(sm_debug_file(), ctladdr, false);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/* heuristic to determine old versus new style addresses */
1907c478bd9Sstevel@tonic-gate 	if (ctladdr == NULL &&
1917c478bd9Sstevel@tonic-gate 	    (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
1927c478bd9Sstevel@tonic-gate 	     strchr(list, '<') != NULL || strchr(list, '(') != NULL))
1937c478bd9Sstevel@tonic-gate 		e->e_flags &= ~EF_OLDSTYLE;
1947c478bd9Sstevel@tonic-gate 	delimiter = ' ';
1957c478bd9Sstevel@tonic-gate 	if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
1967c478bd9Sstevel@tonic-gate 		delimiter = ',';
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	al = NULL;
1997c478bd9Sstevel@tonic-gate 	naddrs = 0;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/* make sure we have enough space to copy the string */
2027c478bd9Sstevel@tonic-gate 	i = strlen(list) + 1;
203058561cbSjbeck 	if (i <= sizeof(buf))
2047c478bd9Sstevel@tonic-gate 	{
2057c478bd9Sstevel@tonic-gate 		bufp = buf;
206058561cbSjbeck 		i = sizeof(buf);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	else
2097c478bd9Sstevel@tonic-gate 		bufp = sm_malloc_x(i);
2107c478bd9Sstevel@tonic-gate 	endp = bufp + i;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	SM_TRY
2137c478bd9Sstevel@tonic-gate 	{
2147c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(bufp, denlstring(list, false, true), i);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e r");
2177c478bd9Sstevel@tonic-gate 		for (p = bufp; *p != '\0'; )
2187c478bd9Sstevel@tonic-gate 		{
2197c478bd9Sstevel@tonic-gate 			auto char *delimptr;
2207c478bd9Sstevel@tonic-gate 			register ADDRESS *a;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 			SM_ASSERT(p < endp);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 			/* parse the address */
2257c478bd9Sstevel@tonic-gate 			while ((isascii(*p) && isspace(*p)) || *p == ',')
2267c478bd9Sstevel@tonic-gate 				p++;
2277c478bd9Sstevel@tonic-gate 			SM_ASSERT(p < endp);
2287c478bd9Sstevel@tonic-gate 			a = parseaddr(p, NULLADDR, RF_COPYALL, delimiter,
2297c478bd9Sstevel@tonic-gate 				      &delimptr, e, true);
2307c478bd9Sstevel@tonic-gate 			p = delimptr;
2317c478bd9Sstevel@tonic-gate 			SM_ASSERT(p < endp);
2327c478bd9Sstevel@tonic-gate 			if (a == NULL)
2337c478bd9Sstevel@tonic-gate 				continue;
2347c478bd9Sstevel@tonic-gate 			a->q_next = al;
2357c478bd9Sstevel@tonic-gate 			a->q_alias = ctladdr;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 			/* arrange to inherit attributes from parent */
2387c478bd9Sstevel@tonic-gate 			if (ctladdr != NULL)
2397c478bd9Sstevel@tonic-gate 			{
2407c478bd9Sstevel@tonic-gate 				ADDRESS *b;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 				/* self reference test */
2437c478bd9Sstevel@tonic-gate 				if (sameaddr(ctladdr, a))
2447c478bd9Sstevel@tonic-gate 				{
2457c478bd9Sstevel@tonic-gate 					if (tTd(27, 5))
2467c478bd9Sstevel@tonic-gate 					{
2477c478bd9Sstevel@tonic-gate 						sm_dprintf("sendtolist: QSELFREF ");
2487c478bd9Sstevel@tonic-gate 						printaddr(sm_debug_file(), ctladdr, false);
2497c478bd9Sstevel@tonic-gate 					}
2507c478bd9Sstevel@tonic-gate 					ctladdr->q_flags |= QSELFREF;
2517c478bd9Sstevel@tonic-gate 				}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 				/* check for address loops */
2547c478bd9Sstevel@tonic-gate 				b = self_reference(a);
2557c478bd9Sstevel@tonic-gate 				if (b != NULL)
2567c478bd9Sstevel@tonic-gate 				{
2577c478bd9Sstevel@tonic-gate 					b->q_flags |= QSELFREF;
2587c478bd9Sstevel@tonic-gate 					if (tTd(27, 5))
2597c478bd9Sstevel@tonic-gate 					{
2607c478bd9Sstevel@tonic-gate 						sm_dprintf("sendtolist: QSELFREF ");
2617c478bd9Sstevel@tonic-gate 						printaddr(sm_debug_file(), b, false);
2627c478bd9Sstevel@tonic-gate 					}
2637c478bd9Sstevel@tonic-gate 					if (a != b)
2647c478bd9Sstevel@tonic-gate 					{
2657c478bd9Sstevel@tonic-gate 						if (tTd(27, 5))
2667c478bd9Sstevel@tonic-gate 						{
2677c478bd9Sstevel@tonic-gate 							sm_dprintf("sendtolist: QS_DONTSEND ");
2687c478bd9Sstevel@tonic-gate 							printaddr(sm_debug_file(), a, false);
2697c478bd9Sstevel@tonic-gate 						}
2707c478bd9Sstevel@tonic-gate 						a->q_state = QS_DONTSEND;
2717c478bd9Sstevel@tonic-gate 						b->q_flags |= a->q_flags & QNOTREMOTE;
2727c478bd9Sstevel@tonic-gate 						continue;
2737c478bd9Sstevel@tonic-gate 					}
2747c478bd9Sstevel@tonic-gate 				}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 				/* full name */
2777c478bd9Sstevel@tonic-gate 				if (a->q_fullname == NULL)
2787c478bd9Sstevel@tonic-gate 					a->q_fullname = ctladdr->q_fullname;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 				/* various flag bits */
2817c478bd9Sstevel@tonic-gate 				a->q_flags &= ~QINHERITEDBITS;
2827c478bd9Sstevel@tonic-gate 				a->q_flags |= ctladdr->q_flags & QINHERITEDBITS;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 				/* DSN recipient information */
2857c478bd9Sstevel@tonic-gate 				a->q_finalrcpt = ctladdr->q_finalrcpt;
2867c478bd9Sstevel@tonic-gate 				a->q_orcpt = ctladdr->q_orcpt;
2877c478bd9Sstevel@tonic-gate 			}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 			al = a;
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		/* arrange to send to everyone on the local send list */
2937c478bd9Sstevel@tonic-gate 		while (al != NULL)
2947c478bd9Sstevel@tonic-gate 		{
2957c478bd9Sstevel@tonic-gate 			register ADDRESS *a = al;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 			al = a->q_next;
2987c478bd9Sstevel@tonic-gate 			a = recipient(a, sendq, aliaslevel, e);
2997c478bd9Sstevel@tonic-gate 			naddrs++;
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	SM_FINALLY
3037c478bd9Sstevel@tonic-gate 	{
3047c478bd9Sstevel@tonic-gate 		e->e_to = oldto;
3057c478bd9Sstevel@tonic-gate 		if (bufp != buf)
3067c478bd9Sstevel@tonic-gate 			sm_free(bufp);
3077c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	SM_END_TRY
3107c478bd9Sstevel@tonic-gate 	return naddrs;
3117c478bd9Sstevel@tonic-gate }
312058561cbSjbeck 
3137c478bd9Sstevel@tonic-gate #if MILTER
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate **  REMOVEFROMLIST -- Remove addresses from a send list.
3167c478bd9Sstevel@tonic-gate **
3177c478bd9Sstevel@tonic-gate **	The parameter is a comma-separated list of recipients to remove.
3187c478bd9Sstevel@tonic-gate **	Note that it only deletes matching addresses.  If those addresses
3197c478bd9Sstevel@tonic-gate **	have been expanded already in the sendq, it won't mark the
3207c478bd9Sstevel@tonic-gate **	expanded recipients as QS_REMOVED.
3217c478bd9Sstevel@tonic-gate **
3227c478bd9Sstevel@tonic-gate **	Parameters:
3237c478bd9Sstevel@tonic-gate **		list -- the list to remove.
3247c478bd9Sstevel@tonic-gate **		sendq -- a pointer to the head of a queue to remove
3257c478bd9Sstevel@tonic-gate **			these addresses from.
3267c478bd9Sstevel@tonic-gate **		e -- the envelope in which to remove these recipients.
3277c478bd9Sstevel@tonic-gate **
3287c478bd9Sstevel@tonic-gate **	Returns:
3297c478bd9Sstevel@tonic-gate **		The number of addresses removed from the list.
3307c478bd9Sstevel@tonic-gate **
3317c478bd9Sstevel@tonic-gate */
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate int
removefromlist(list,sendq,e)3347c478bd9Sstevel@tonic-gate removefromlist(list, sendq, e)
3357c478bd9Sstevel@tonic-gate 	char *list;
3367c478bd9Sstevel@tonic-gate 	ADDRESS **sendq;
3377c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	SM_NONVOLATILE char delimiter;		/* the address delimiter */
3407c478bd9Sstevel@tonic-gate 	SM_NONVOLATILE int naddrs;
3417c478bd9Sstevel@tonic-gate 	SM_NONVOLATILE int i;
3427c478bd9Sstevel@tonic-gate 	char *p;
3437c478bd9Sstevel@tonic-gate 	char *oldto = e->e_to;
3447c478bd9Sstevel@tonic-gate 	char *SM_NONVOLATILE bufp;
3457c478bd9Sstevel@tonic-gate 	char buf[MAXNAME + 1];
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (list == NULL)
3487c478bd9Sstevel@tonic-gate 	{
3497c478bd9Sstevel@tonic-gate 		syserr("removefromlist: null list");
3507c478bd9Sstevel@tonic-gate 		return 0;
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (tTd(25, 1))
3547c478bd9Sstevel@tonic-gate 		sm_dprintf("removefromlist: %s\n", list);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/* heuristic to determine old versus new style addresses */
3577c478bd9Sstevel@tonic-gate 	if (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
3587c478bd9Sstevel@tonic-gate 	    strchr(list, '<') != NULL || strchr(list, '(') != NULL)
3597c478bd9Sstevel@tonic-gate 		e->e_flags &= ~EF_OLDSTYLE;
3607c478bd9Sstevel@tonic-gate 	delimiter = ' ';
3617c478bd9Sstevel@tonic-gate 	if (!bitset(EF_OLDSTYLE, e->e_flags))
3627c478bd9Sstevel@tonic-gate 		delimiter = ',';
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	naddrs = 0;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/* make sure we have enough space to copy the string */
3677c478bd9Sstevel@tonic-gate 	i = strlen(list) + 1;
368058561cbSjbeck 	if (i <= sizeof(buf))
3697c478bd9Sstevel@tonic-gate 	{
3707c478bd9Sstevel@tonic-gate 		bufp = buf;
371058561cbSjbeck 		i = sizeof(buf);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 	else
3747c478bd9Sstevel@tonic-gate 		bufp = sm_malloc_x(i);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	SM_TRY
3777c478bd9Sstevel@tonic-gate 	{
3787c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(bufp, denlstring(list, false, true), i);
3797c478bd9Sstevel@tonic-gate 
380*7800901eSjbeck #if _FFR_ADDR_TYPE_MODES
381*7800901eSjbeck 		if (AddrTypeModes)
382*7800901eSjbeck 			macdefine(&e->e_macro, A_PERM, macid("{addr_type}"),
383*7800901eSjbeck 				  "e r d");
384*7800901eSjbeck 		else
385*7800901eSjbeck #endif /* _FFR_ADDR_TYPE_MODES */
3867c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e r");
3877c478bd9Sstevel@tonic-gate 		for (p = bufp; *p != '\0'; )
3887c478bd9Sstevel@tonic-gate 		{
3897c478bd9Sstevel@tonic-gate 			ADDRESS a;	/* parsed address to be removed */
3907c478bd9Sstevel@tonic-gate 			ADDRESS *q;
3917c478bd9Sstevel@tonic-gate 			ADDRESS **pq;
3927c478bd9Sstevel@tonic-gate 			char *delimptr;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 			/* parse the address */
3957c478bd9Sstevel@tonic-gate 			while ((isascii(*p) && isspace(*p)) || *p == ',')
3967c478bd9Sstevel@tonic-gate 				p++;
397058561cbSjbeck 			if (parseaddr(p, &a, RF_COPYALL|RF_RM_ADDR,
3987c478bd9Sstevel@tonic-gate 				      delimiter, &delimptr, e, true) == NULL)
3997c478bd9Sstevel@tonic-gate 			{
4007c478bd9Sstevel@tonic-gate 				p = delimptr;
4017c478bd9Sstevel@tonic-gate 				continue;
4027c478bd9Sstevel@tonic-gate 			}
4037c478bd9Sstevel@tonic-gate 			p = delimptr;
4047c478bd9Sstevel@tonic-gate 			for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
4057c478bd9Sstevel@tonic-gate 			{
4067c478bd9Sstevel@tonic-gate 				if (!QS_IS_DEAD(q->q_state) &&
4077c478bd9Sstevel@tonic-gate 				    (sameaddr(q, &a) ||
4087c478bd9Sstevel@tonic-gate 				     strcmp(q->q_paddr, a.q_paddr) == 0))
4097c478bd9Sstevel@tonic-gate 				{
4107c478bd9Sstevel@tonic-gate 					if (tTd(25, 5))
4117c478bd9Sstevel@tonic-gate 					{
4127c478bd9Sstevel@tonic-gate 						sm_dprintf("removefromlist: QS_REMOVED ");
4137c478bd9Sstevel@tonic-gate 						printaddr(sm_debug_file(), &a, false);
4147c478bd9Sstevel@tonic-gate 					}
4157c478bd9Sstevel@tonic-gate 					q->q_state = QS_REMOVED;
4167c478bd9Sstevel@tonic-gate 					naddrs++;
4177c478bd9Sstevel@tonic-gate 					break;
4187c478bd9Sstevel@tonic-gate 				}
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 	SM_FINALLY
4237c478bd9Sstevel@tonic-gate 	{
4247c478bd9Sstevel@tonic-gate 		e->e_to = oldto;
4257c478bd9Sstevel@tonic-gate 		if (bufp != buf)
4267c478bd9Sstevel@tonic-gate 			sm_free(bufp);
4277c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	SM_END_TRY
4307c478bd9Sstevel@tonic-gate 	return naddrs;
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate #endif /* MILTER */
433058561cbSjbeck 
4347c478bd9Sstevel@tonic-gate /*
4357c478bd9Sstevel@tonic-gate **  RECIPIENT -- Designate a message recipient
4364aac33d3Sjbeck **	Saves the named person for future mailing (after some checks).
4377c478bd9Sstevel@tonic-gate **
4387c478bd9Sstevel@tonic-gate **	Parameters:
4397c478bd9Sstevel@tonic-gate **		new -- the (preparsed) address header for the recipient.
4407c478bd9Sstevel@tonic-gate **		sendq -- a pointer to the head of a queue to put the
4417c478bd9Sstevel@tonic-gate **			recipient in.  Duplicate suppression is done
4427c478bd9Sstevel@tonic-gate **			in this queue.
4437c478bd9Sstevel@tonic-gate **		aliaslevel -- the current alias nesting depth.
4447c478bd9Sstevel@tonic-gate **		e -- the current envelope.
4457c478bd9Sstevel@tonic-gate **
4467c478bd9Sstevel@tonic-gate **	Returns:
4477c478bd9Sstevel@tonic-gate **		The actual address in the queue.  This will be "a" if
4487c478bd9Sstevel@tonic-gate **		the address is not a duplicate, else the original address.
4497c478bd9Sstevel@tonic-gate **
4507c478bd9Sstevel@tonic-gate */
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate ADDRESS *
recipient(new,sendq,aliaslevel,e)4537c478bd9Sstevel@tonic-gate recipient(new, sendq, aliaslevel, e)
4547c478bd9Sstevel@tonic-gate 	register ADDRESS *new;
4557c478bd9Sstevel@tonic-gate 	register ADDRESS **sendq;
4567c478bd9Sstevel@tonic-gate 	int aliaslevel;
4577c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	register ADDRESS *q;
4607c478bd9Sstevel@tonic-gate 	ADDRESS **pq;
4617c478bd9Sstevel@tonic-gate 	ADDRESS **prev;
4627c478bd9Sstevel@tonic-gate 	register struct mailer *m;
4637c478bd9Sstevel@tonic-gate 	register char *p;
4647c478bd9Sstevel@tonic-gate 	int i, buflen;
4657c478bd9Sstevel@tonic-gate 	bool quoted;		/* set if the addr has a quote bit */
4667c478bd9Sstevel@tonic-gate 	bool insert;
4677c478bd9Sstevel@tonic-gate 	int findusercount;
4687c478bd9Sstevel@tonic-gate 	bool initialdontsend;
4697c478bd9Sstevel@tonic-gate 	char *buf;
4707c478bd9Sstevel@tonic-gate 	char buf0[MAXNAME + 1];		/* unquoted image of the user name */
4717c478bd9Sstevel@tonic-gate 	sortfn_t *sortfn;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	p = NULL;
4747c478bd9Sstevel@tonic-gate 	quoted = false;
4757c478bd9Sstevel@tonic-gate 	insert = false;
4767c478bd9Sstevel@tonic-gate 	findusercount = 0;
4777c478bd9Sstevel@tonic-gate 	initialdontsend = QS_IS_DEAD(new->q_state);
4787c478bd9Sstevel@tonic-gate 	e->e_to = new->q_paddr;
4797c478bd9Sstevel@tonic-gate 	m = new->q_mailer;
4807c478bd9Sstevel@tonic-gate 	errno = 0;
4817c478bd9Sstevel@tonic-gate 	if (aliaslevel == 0)
4827c478bd9Sstevel@tonic-gate 		new->q_flags |= QPRIMARY;
4837c478bd9Sstevel@tonic-gate 	if (tTd(26, 1))
4847c478bd9Sstevel@tonic-gate 	{
4857c478bd9Sstevel@tonic-gate 		sm_dprintf("\nrecipient (%d): ", aliaslevel);
4867c478bd9Sstevel@tonic-gate 		printaddr(sm_debug_file(), new, false);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	/* if this is primary, use it as original recipient */
4907c478bd9Sstevel@tonic-gate 	if (new->q_alias == NULL)
4917c478bd9Sstevel@tonic-gate 	{
4927c478bd9Sstevel@tonic-gate 		if (e->e_origrcpt == NULL)
4937c478bd9Sstevel@tonic-gate 			e->e_origrcpt = new->q_paddr;
4947c478bd9Sstevel@tonic-gate 		else if (e->e_origrcpt != new->q_paddr)
4957c478bd9Sstevel@tonic-gate 			e->e_origrcpt = "";
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	/* find parent recipient for finalrcpt and orcpt */
4997c478bd9Sstevel@tonic-gate 	for (q = new; q->q_alias != NULL; q = q->q_alias)
5007c478bd9Sstevel@tonic-gate 		continue;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	/* find final recipient DSN address */
5037c478bd9Sstevel@tonic-gate 	if (new->q_finalrcpt == NULL &&
5047c478bd9Sstevel@tonic-gate 	    e->e_from.q_mailer != NULL)
5057c478bd9Sstevel@tonic-gate 	{
5067c478bd9Sstevel@tonic-gate 		char frbuf[MAXLINE];
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		p = e->e_from.q_mailer->m_addrtype;
5097c478bd9Sstevel@tonic-gate 		if (p == NULL)
5107c478bd9Sstevel@tonic-gate 			p = "rfc822";
5117c478bd9Sstevel@tonic-gate 		if (sm_strcasecmp(p, "rfc822") != 0)
5127c478bd9Sstevel@tonic-gate 		{
513058561cbSjbeck 			(void) sm_snprintf(frbuf, sizeof(frbuf), "%s; %.800s",
5147c478bd9Sstevel@tonic-gate 					   q->q_mailer->m_addrtype,
5157c478bd9Sstevel@tonic-gate 					   q->q_user);
5167c478bd9Sstevel@tonic-gate 		}
5177c478bd9Sstevel@tonic-gate 		else if (strchr(q->q_user, '@') != NULL)
5187c478bd9Sstevel@tonic-gate 		{
519058561cbSjbeck 			(void) sm_snprintf(frbuf, sizeof(frbuf), "%s; %.800s",
5207c478bd9Sstevel@tonic-gate 					   p, q->q_user);
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 		else if (strchr(q->q_paddr, '@') != NULL)
5237c478bd9Sstevel@tonic-gate 		{
5247c478bd9Sstevel@tonic-gate 			char *qp;
5257c478bd9Sstevel@tonic-gate 			bool b;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 			qp = q->q_paddr;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 			/* strip brackets from address */
5307c478bd9Sstevel@tonic-gate 			b = false;
5317c478bd9Sstevel@tonic-gate 			if (*qp == '<')
5327c478bd9Sstevel@tonic-gate 			{
5337c478bd9Sstevel@tonic-gate 				b = qp[strlen(qp) - 1] == '>';
5347c478bd9Sstevel@tonic-gate 				if (b)
5357c478bd9Sstevel@tonic-gate 					qp[strlen(qp) - 1] = '\0';
5367c478bd9Sstevel@tonic-gate 				qp++;
5377c478bd9Sstevel@tonic-gate 			}
538058561cbSjbeck 			(void) sm_snprintf(frbuf, sizeof(frbuf), "%s; %.800s",
5397c478bd9Sstevel@tonic-gate 					   p, qp);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 			/* undo damage */
5427c478bd9Sstevel@tonic-gate 			if (b)
5437c478bd9Sstevel@tonic-gate 				qp[strlen(qp)] = '>';
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 		else
5467c478bd9Sstevel@tonic-gate 		{
547058561cbSjbeck 			(void) sm_snprintf(frbuf, sizeof(frbuf),
5487c478bd9Sstevel@tonic-gate 					   "%s; %.700s@%.100s",
5497c478bd9Sstevel@tonic-gate 					   p, q->q_user, MyHostName);
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 		new->q_finalrcpt = sm_rpool_strdup_x(e->e_rpool, frbuf);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate #if _FFR_GEN_ORCPT
5557c478bd9Sstevel@tonic-gate 	/* set ORCPT DSN arg if not already set */
5567c478bd9Sstevel@tonic-gate 	if (new->q_orcpt == NULL)
5577c478bd9Sstevel@tonic-gate 	{
5587c478bd9Sstevel@tonic-gate 		/* check for an existing ORCPT */
5597c478bd9Sstevel@tonic-gate 		if (q->q_orcpt != NULL)
5607c478bd9Sstevel@tonic-gate 			new->q_orcpt = q->q_orcpt;
5617c478bd9Sstevel@tonic-gate 		else
5627c478bd9Sstevel@tonic-gate 		{
5637c478bd9Sstevel@tonic-gate 			/* make our own */
5647c478bd9Sstevel@tonic-gate 			bool b = false;
5657c478bd9Sstevel@tonic-gate 			char *qp;
5667c478bd9Sstevel@tonic-gate 			char obuf[MAXLINE];
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 			if (e->e_from.q_mailer != NULL)
5697c478bd9Sstevel@tonic-gate 				p = e->e_from.q_mailer->m_addrtype;
5707c478bd9Sstevel@tonic-gate 			if (p == NULL)
5717c478bd9Sstevel@tonic-gate 				p = "rfc822";
572058561cbSjbeck 			(void) sm_strlcpyn(obuf, sizeof(obuf), 2, p, ";");
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 			qp = q->q_paddr;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 			/* FFR: Needs to strip comments from stdin addrs */
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 			/* strip brackets from address */
5797c478bd9Sstevel@tonic-gate 			if (*qp == '<')
5807c478bd9Sstevel@tonic-gate 			{
5817c478bd9Sstevel@tonic-gate 				b = qp[strlen(qp) - 1] == '>';
5827c478bd9Sstevel@tonic-gate 				if (b)
5837c478bd9Sstevel@tonic-gate 					qp[strlen(qp) - 1] = '\0';
5847c478bd9Sstevel@tonic-gate 				qp++;
5857c478bd9Sstevel@tonic-gate 			}
5867c478bd9Sstevel@tonic-gate 
587058561cbSjbeck 			p = xtextify(denlstring(qp, true, false), "=");
5887c478bd9Sstevel@tonic-gate 
589058561cbSjbeck 			if (sm_strlcat(obuf, p, sizeof(obuf)) >= sizeof(obuf))
5907c478bd9Sstevel@tonic-gate 			{
5917c478bd9Sstevel@tonic-gate 				/* if too big, don't use it */
5927c478bd9Sstevel@tonic-gate 				obuf[0] = '\0';
5937c478bd9Sstevel@tonic-gate 			}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 			/* undo damage */
5967c478bd9Sstevel@tonic-gate 			if (b)
5977c478bd9Sstevel@tonic-gate 				qp[strlen(qp)] = '>';
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 			if (obuf[0] != '\0')
6007c478bd9Sstevel@tonic-gate 				new->q_orcpt =
6017c478bd9Sstevel@tonic-gate 					sm_rpool_strdup_x(e->e_rpool, obuf);
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate #endif /* _FFR_GEN_ORCPT */
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	/* break aliasing loops */
6077c478bd9Sstevel@tonic-gate 	if (aliaslevel > MaxAliasRecursion)
6087c478bd9Sstevel@tonic-gate 	{
6097c478bd9Sstevel@tonic-gate 		new->q_state = QS_BADADDR;
6107c478bd9Sstevel@tonic-gate 		new->q_status = "5.4.6";
611058561cbSjbeck 		if (new->q_alias != NULL)
612058561cbSjbeck 		{
613058561cbSjbeck 			new->q_alias->q_state = QS_BADADDR;
614058561cbSjbeck 			new->q_alias->q_status = "5.4.6";
615058561cbSjbeck 		}
616058561cbSjbeck 		if ((SuprErrs || !LogUsrErrs) && LogLevel > 0)
617058561cbSjbeck 		{
618058561cbSjbeck 			sm_syslog(LOG_ERR, e->e_id,
619058561cbSjbeck 				"aliasing/forwarding loop broken: %s (%d aliases deep; %d max)",
620058561cbSjbeck 				FileName != NULL ? FileName : "", aliaslevel,
621058561cbSjbeck 				MaxAliasRecursion);
622058561cbSjbeck 		}
6237c478bd9Sstevel@tonic-gate 		usrerrenh(new->q_status,
6247c478bd9Sstevel@tonic-gate 			  "554 aliasing/forwarding loop broken (%d aliases deep; %d max)",
6257c478bd9Sstevel@tonic-gate 			  aliaslevel, MaxAliasRecursion);
6267c478bd9Sstevel@tonic-gate 		return new;
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	**  Finish setting up address structure.
6317c478bd9Sstevel@tonic-gate 	*/
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	/* get unquoted user for file, program or user.name check */
6347c478bd9Sstevel@tonic-gate 	i = strlen(new->q_user);
635058561cbSjbeck 	if (i >= sizeof(buf0))
6367c478bd9Sstevel@tonic-gate 	{
6377c478bd9Sstevel@tonic-gate 		buflen = i + 1;
6387c478bd9Sstevel@tonic-gate 		buf = xalloc(buflen);
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 	else
6417c478bd9Sstevel@tonic-gate 	{
6427c478bd9Sstevel@tonic-gate 		buf = buf0;
643058561cbSjbeck 		buflen = sizeof(buf0);
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	(void) sm_strlcpy(buf, new->q_user, buflen);
6467c478bd9Sstevel@tonic-gate 	for (p = buf; *p != '\0' && !quoted; p++)
6477c478bd9Sstevel@tonic-gate 	{
6487c478bd9Sstevel@tonic-gate 		if (*p == '\\')
6497c478bd9Sstevel@tonic-gate 			quoted = true;
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 	stripquotes(buf);
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/* check for direct mailing to restricted mailers */
6547c478bd9Sstevel@tonic-gate 	if (m == ProgMailer)
6557c478bd9Sstevel@tonic-gate 	{
6567c478bd9Sstevel@tonic-gate 		if (new->q_alias == NULL || UseMSP ||
6577c478bd9Sstevel@tonic-gate 		    bitset(EF_UNSAFE, e->e_flags))
6587c478bd9Sstevel@tonic-gate 		{
6597c478bd9Sstevel@tonic-gate 			new->q_state = QS_BADADDR;
6607c478bd9Sstevel@tonic-gate 			new->q_status = "5.7.1";
6617c478bd9Sstevel@tonic-gate 			usrerrenh(new->q_status,
6627c478bd9Sstevel@tonic-gate 				  "550 Cannot mail directly to programs");
6637c478bd9Sstevel@tonic-gate 		}
6647c478bd9Sstevel@tonic-gate 		else if (bitset(QBOGUSSHELL, new->q_alias->q_flags))
6657c478bd9Sstevel@tonic-gate 		{
6667c478bd9Sstevel@tonic-gate 			new->q_state = QS_BADADDR;
6677c478bd9Sstevel@tonic-gate 			new->q_status = "5.7.1";
6687c478bd9Sstevel@tonic-gate 			if (new->q_alias->q_ruser == NULL)
6697c478bd9Sstevel@tonic-gate 				usrerrenh(new->q_status,
6707c478bd9Sstevel@tonic-gate 					  "550 UID %d is an unknown user: cannot mail to programs",
6717c478bd9Sstevel@tonic-gate 					  new->q_alias->q_uid);
6727c478bd9Sstevel@tonic-gate 			else
6737c478bd9Sstevel@tonic-gate 				usrerrenh(new->q_status,
6747c478bd9Sstevel@tonic-gate 					  "550 User %s@%s doesn't have a valid shell for mailing to programs",
6757c478bd9Sstevel@tonic-gate 					  new->q_alias->q_ruser, MyHostName);
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 		else if (bitset(QUNSAFEADDR, new->q_alias->q_flags))
6787c478bd9Sstevel@tonic-gate 		{
6797c478bd9Sstevel@tonic-gate 			new->q_state = QS_BADADDR;
6807c478bd9Sstevel@tonic-gate 			new->q_status = "5.7.1";
6817c478bd9Sstevel@tonic-gate 			new->q_rstatus = "550 Unsafe for mailing to programs";
6827c478bd9Sstevel@tonic-gate 			usrerrenh(new->q_status,
6837c478bd9Sstevel@tonic-gate 				  "550 Address %s is unsafe for mailing to programs",
6847c478bd9Sstevel@tonic-gate 				  new->q_alias->q_paddr);
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/*
6897c478bd9Sstevel@tonic-gate 	**  Look up this person in the recipient list.
6907c478bd9Sstevel@tonic-gate 	**	If they are there already, return, otherwise continue.
6917c478bd9Sstevel@tonic-gate 	**	If the list is empty, just add it.  Notice the cute
6927c478bd9Sstevel@tonic-gate 	**	hack to make from addresses suppress things correctly:
6937c478bd9Sstevel@tonic-gate 	**	the QS_DUPLICATE state will be set in the send list.
6947c478bd9Sstevel@tonic-gate 	**	[Please note: the emphasis is on "hack."]
6957c478bd9Sstevel@tonic-gate 	*/
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	prev = NULL;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	/*
7007c478bd9Sstevel@tonic-gate 	**  If this message is going to the queue or FastSplit is set
7017c478bd9Sstevel@tonic-gate 	**  and it is the first try and the envelope hasn't split, then we
7027c478bd9Sstevel@tonic-gate 	**  avoid doing an MX RR lookup now because one will be done when the
7037c478bd9Sstevel@tonic-gate 	**  message is extracted from the queue later. It can go to the queue
7047c478bd9Sstevel@tonic-gate 	**  because all messages are going to the queue or this mailer of
7057c478bd9Sstevel@tonic-gate 	**  the current recipient is marked expensive.
7067c478bd9Sstevel@tonic-gate 	*/
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if (UseMSP || WILL_BE_QUEUED(e->e_sendmode) ||
7097c478bd9Sstevel@tonic-gate 	    (!bitset(EF_SPLIT, e->e_flags) && e->e_ntries == 0 &&
7107c478bd9Sstevel@tonic-gate 	     FastSplit > 0))
7117c478bd9Sstevel@tonic-gate 		sortfn = sorthost;
7127c478bd9Sstevel@tonic-gate 	else if (NoConnect && bitnset(M_EXPENSIVE, new->q_mailer->m_flags))
7137c478bd9Sstevel@tonic-gate 		sortfn = sortexpensive;
7147c478bd9Sstevel@tonic-gate 	else
7157c478bd9Sstevel@tonic-gate 		sortfn = sortbysignature;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
7187c478bd9Sstevel@tonic-gate 	{
7197c478bd9Sstevel@tonic-gate 		/*
7207c478bd9Sstevel@tonic-gate 		**  If address is "less than" it should be inserted now.
7217c478bd9Sstevel@tonic-gate 		**  If address is "greater than" current comparison it'll
7227c478bd9Sstevel@tonic-gate 		**  insert later in the list; so loop again (if possible).
7237c478bd9Sstevel@tonic-gate 		**  If address is "equal" (different equal than sameaddr()
7247c478bd9Sstevel@tonic-gate 		**  call) then check if sameaddr() will be true.
7257c478bd9Sstevel@tonic-gate 		**  Because this list is now sorted, it'll mean fewer
7267c478bd9Sstevel@tonic-gate 		**  comparisons and fewer loops which is important for more
7277c478bd9Sstevel@tonic-gate 		**  recipients.
7287c478bd9Sstevel@tonic-gate 		*/
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		i = (*sortfn)(new, q);
7317c478bd9Sstevel@tonic-gate 		if (i == 0) /* equal */
7327c478bd9Sstevel@tonic-gate 		{
7337c478bd9Sstevel@tonic-gate 			/*
7347c478bd9Sstevel@tonic-gate 			**  Sortbysignature() has said that the two have
7357c478bd9Sstevel@tonic-gate 			**  equal MX RR's and the same user. Calling sameaddr()
7367c478bd9Sstevel@tonic-gate 			**  now checks if the two hosts are as identical as the
7377c478bd9Sstevel@tonic-gate 			**  MX RR's are (which might not be the case)
7387c478bd9Sstevel@tonic-gate 			**  before saying these are the identical addresses.
7397c478bd9Sstevel@tonic-gate 			*/
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 			if (sameaddr(q, new) &&
7427c478bd9Sstevel@tonic-gate 			    (bitset(QRCPTOK, q->q_flags) ||
7437c478bd9Sstevel@tonic-gate 			     !bitset(QPRIMARY, q->q_flags)))
7447c478bd9Sstevel@tonic-gate 			{
7457c478bd9Sstevel@tonic-gate 				if (tTd(26, 1))
7467c478bd9Sstevel@tonic-gate 				{
7477c478bd9Sstevel@tonic-gate 					sm_dprintf("%s in sendq: ",
7487c478bd9Sstevel@tonic-gate 						   new->q_paddr);
7497c478bd9Sstevel@tonic-gate 					printaddr(sm_debug_file(), q, false);
7507c478bd9Sstevel@tonic-gate 				}
7517c478bd9Sstevel@tonic-gate 				if (!bitset(QPRIMARY, q->q_flags))
7527c478bd9Sstevel@tonic-gate 				{
7537c478bd9Sstevel@tonic-gate 					if (!QS_IS_DEAD(new->q_state))
7547c478bd9Sstevel@tonic-gate 						message("duplicate suppressed");
7557c478bd9Sstevel@tonic-gate 					else
7567c478bd9Sstevel@tonic-gate 						q->q_state = QS_DUPLICATE;
7577c478bd9Sstevel@tonic-gate 					q->q_flags |= new->q_flags;
7587c478bd9Sstevel@tonic-gate 				}
7597c478bd9Sstevel@tonic-gate 				else if (bitset(QSELFREF, q->q_flags)
7607c478bd9Sstevel@tonic-gate 					 || q->q_state == QS_REMOVED)
7617c478bd9Sstevel@tonic-gate 				{
7627c478bd9Sstevel@tonic-gate 					/*
7637c478bd9Sstevel@tonic-gate 					**  If an earlier milter removed the
7647c478bd9Sstevel@tonic-gate 					**  address, a later one can still add
7657c478bd9Sstevel@tonic-gate 					**  it back.
7667c478bd9Sstevel@tonic-gate 					*/
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 					q->q_state = new->q_state;
7697c478bd9Sstevel@tonic-gate 					q->q_flags |= new->q_flags;
7707c478bd9Sstevel@tonic-gate 				}
7717c478bd9Sstevel@tonic-gate 				new = q;
7727c478bd9Sstevel@tonic-gate 				goto done;
7737c478bd9Sstevel@tonic-gate 			}
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 		else if (i < 0) /* less than */
7767c478bd9Sstevel@tonic-gate 		{
7777c478bd9Sstevel@tonic-gate 			insert = true;
7787c478bd9Sstevel@tonic-gate 			break;
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 		prev = pq;
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	/* pq should point to an address, never NULL */
7847c478bd9Sstevel@tonic-gate 	SM_ASSERT(pq != NULL);
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	/* add address on list */
7877c478bd9Sstevel@tonic-gate 	if (insert)
7887c478bd9Sstevel@tonic-gate 	{
7897c478bd9Sstevel@tonic-gate 		/*
7907c478bd9Sstevel@tonic-gate 		**  insert before 'pq'. Only possible when at least 1
7917c478bd9Sstevel@tonic-gate 		**  ADDRESS is in the list already.
7927c478bd9Sstevel@tonic-gate 		*/
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 		new->q_next = *pq;
7957c478bd9Sstevel@tonic-gate 		if (prev == NULL)
7967c478bd9Sstevel@tonic-gate 			*sendq = new; /* To be the first ADDRESS */
7977c478bd9Sstevel@tonic-gate 		else
7987c478bd9Sstevel@tonic-gate 			(*prev)->q_next = new;
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 	else
8017c478bd9Sstevel@tonic-gate 	{
8027c478bd9Sstevel@tonic-gate 		/*
8037c478bd9Sstevel@tonic-gate 		**  Place in list at current 'pq' position. Possible
8047c478bd9Sstevel@tonic-gate 		**  when there are 0 or more ADDRESS's in the list.
8057c478bd9Sstevel@tonic-gate 		*/
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		new->q_next = NULL;
8087c478bd9Sstevel@tonic-gate 		*pq = new;
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/* added a new address: clear split flag */
8127c478bd9Sstevel@tonic-gate 	e->e_flags &= ~EF_SPLIT;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/*
8157c478bd9Sstevel@tonic-gate 	**  Alias the name and handle special mailer types.
8167c478bd9Sstevel@tonic-gate 	*/
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate   trylocaluser:
8197c478bd9Sstevel@tonic-gate 	if (tTd(29, 7))
8207c478bd9Sstevel@tonic-gate 	{
8217c478bd9Sstevel@tonic-gate 		sm_dprintf("at trylocaluser: ");
8227c478bd9Sstevel@tonic-gate 		printaddr(sm_debug_file(), new, false);
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	if (!QS_IS_OK(new->q_state))
8267c478bd9Sstevel@tonic-gate 	{
8277c478bd9Sstevel@tonic-gate 		if (QS_IS_UNDELIVERED(new->q_state))
8287c478bd9Sstevel@tonic-gate 			e->e_nrcpts++;
8297c478bd9Sstevel@tonic-gate 		goto testselfdestruct;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if (m == InclMailer)
8337c478bd9Sstevel@tonic-gate 	{
8347c478bd9Sstevel@tonic-gate 		new->q_state = QS_INCLUDED;
8357c478bd9Sstevel@tonic-gate 		if (new->q_alias == NULL || UseMSP ||
8367c478bd9Sstevel@tonic-gate 		    bitset(EF_UNSAFE, e->e_flags))
8377c478bd9Sstevel@tonic-gate 		{
8387c478bd9Sstevel@tonic-gate 			new->q_state = QS_BADADDR;
8397c478bd9Sstevel@tonic-gate 			new->q_status = "5.7.1";
8407c478bd9Sstevel@tonic-gate 			usrerrenh(new->q_status,
8417c478bd9Sstevel@tonic-gate 				  "550 Cannot mail directly to :include:s");
8427c478bd9Sstevel@tonic-gate 		}
8437c478bd9Sstevel@tonic-gate 		else
8447c478bd9Sstevel@tonic-gate 		{
8457c478bd9Sstevel@tonic-gate 			int ret;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 			message("including file %s", new->q_user);
8487c478bd9Sstevel@tonic-gate 			ret = include(new->q_user, false, new,
8497c478bd9Sstevel@tonic-gate 				      sendq, aliaslevel, e);
8507c478bd9Sstevel@tonic-gate 			if (transienterror(ret))
8517c478bd9Sstevel@tonic-gate 			{
8527c478bd9Sstevel@tonic-gate 				if (LogLevel > 2)
8537c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_ERR, e->e_id,
8547c478bd9Sstevel@tonic-gate 						  "include %s: transient error: %s",
8557c478bd9Sstevel@tonic-gate 						  shortenstring(new->q_user,
8567c478bd9Sstevel@tonic-gate 								MAXSHORTSTR),
8577c478bd9Sstevel@tonic-gate 								sm_errstring(ret));
8587c478bd9Sstevel@tonic-gate 				new->q_state = QS_QUEUEUP;
8597c478bd9Sstevel@tonic-gate 				usrerr("451 4.2.4 Cannot open %s: %s",
8607c478bd9Sstevel@tonic-gate 					shortenstring(new->q_user,
8617c478bd9Sstevel@tonic-gate 						      MAXSHORTSTR),
8627c478bd9Sstevel@tonic-gate 					sm_errstring(ret));
8637c478bd9Sstevel@tonic-gate 			}
8647c478bd9Sstevel@tonic-gate 			else if (ret != 0)
8657c478bd9Sstevel@tonic-gate 			{
8667c478bd9Sstevel@tonic-gate 				new->q_state = QS_BADADDR;
8677c478bd9Sstevel@tonic-gate 				new->q_status = "5.2.4";
8687c478bd9Sstevel@tonic-gate 				usrerrenh(new->q_status,
8697c478bd9Sstevel@tonic-gate 					  "550 Cannot open %s: %s",
8707c478bd9Sstevel@tonic-gate 					  shortenstring(new->q_user,
8717c478bd9Sstevel@tonic-gate 							MAXSHORTSTR),
8727c478bd9Sstevel@tonic-gate 					  sm_errstring(ret));
8737c478bd9Sstevel@tonic-gate 			}
8747c478bd9Sstevel@tonic-gate 		}
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 	else if (m == FileMailer)
8777c478bd9Sstevel@tonic-gate 	{
8787c478bd9Sstevel@tonic-gate 		/* check if allowed */
8797c478bd9Sstevel@tonic-gate 		if (new->q_alias == NULL || UseMSP ||
8807c478bd9Sstevel@tonic-gate 		    bitset(EF_UNSAFE, e->e_flags))
8817c478bd9Sstevel@tonic-gate 		{
8827c478bd9Sstevel@tonic-gate 			new->q_state = QS_BADADDR;
8837c478bd9Sstevel@tonic-gate 			new->q_status = "5.7.1";
8847c478bd9Sstevel@tonic-gate 			usrerrenh(new->q_status,
8857c478bd9Sstevel@tonic-gate 				  "550 Cannot mail directly to files");
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 		else if (bitset(QBOGUSSHELL, new->q_alias->q_flags))
8887c478bd9Sstevel@tonic-gate 		{
8897c478bd9Sstevel@tonic-gate 			new->q_state = QS_BADADDR;
8907c478bd9Sstevel@tonic-gate 			new->q_status = "5.7.1";
8917c478bd9Sstevel@tonic-gate 			if (new->q_alias->q_ruser == NULL)
8927c478bd9Sstevel@tonic-gate 				usrerrenh(new->q_status,
8937c478bd9Sstevel@tonic-gate 					  "550 UID %d is an unknown user: cannot mail to files",
8947c478bd9Sstevel@tonic-gate 					  new->q_alias->q_uid);
8957c478bd9Sstevel@tonic-gate 			else
8967c478bd9Sstevel@tonic-gate 				usrerrenh(new->q_status,
8977c478bd9Sstevel@tonic-gate 					  "550 User %s@%s doesn't have a valid shell for mailing to files",
8987c478bd9Sstevel@tonic-gate 					  new->q_alias->q_ruser, MyHostName);
8997c478bd9Sstevel@tonic-gate 		}
9007c478bd9Sstevel@tonic-gate 		else if (bitset(QUNSAFEADDR, new->q_alias->q_flags))
9017c478bd9Sstevel@tonic-gate 		{
9027c478bd9Sstevel@tonic-gate 			new->q_state = QS_BADADDR;
9037c478bd9Sstevel@tonic-gate 			new->q_status = "5.7.1";
9047c478bd9Sstevel@tonic-gate 			new->q_rstatus = "550 Unsafe for mailing to files";
9057c478bd9Sstevel@tonic-gate 			usrerrenh(new->q_status,
9067c478bd9Sstevel@tonic-gate 				  "550 Address %s is unsafe for mailing to files",
9077c478bd9Sstevel@tonic-gate 				  new->q_alias->q_paddr);
9087c478bd9Sstevel@tonic-gate 		}
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	/* try aliasing */
9127c478bd9Sstevel@tonic-gate 	if (!quoted && QS_IS_OK(new->q_state) &&
9137c478bd9Sstevel@tonic-gate 	    bitnset(M_ALIASABLE, m->m_flags))
9147c478bd9Sstevel@tonic-gate 		alias(new, sendq, aliaslevel, e);
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate #if USERDB
9177c478bd9Sstevel@tonic-gate 	/* if not aliased, look it up in the user database */
9187c478bd9Sstevel@tonic-gate 	if (!bitset(QNOTREMOTE, new->q_flags) &&
9197c478bd9Sstevel@tonic-gate 	    QS_IS_SENDABLE(new->q_state) &&
9207c478bd9Sstevel@tonic-gate 	    bitnset(M_CHECKUDB, m->m_flags))
9217c478bd9Sstevel@tonic-gate 	{
9227c478bd9Sstevel@tonic-gate 		if (udbexpand(new, sendq, aliaslevel, e) == EX_TEMPFAIL)
9237c478bd9Sstevel@tonic-gate 		{
9247c478bd9Sstevel@tonic-gate 			new->q_state = QS_QUEUEUP;
9257c478bd9Sstevel@tonic-gate 			if (e->e_message == NULL)
926058561cbSjbeck 				e->e_message = sm_rpool_strdup_x(e->e_rpool,
927058561cbSjbeck 						"Deferred: user database error");
9287c478bd9Sstevel@tonic-gate 			if (new->q_message == NULL)
9297c478bd9Sstevel@tonic-gate 				new->q_message = "Deferred: user database error";
9307c478bd9Sstevel@tonic-gate 			if (LogLevel > 8)
9317c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
9327c478bd9Sstevel@tonic-gate 					  "deferred: udbexpand: %s",
9337c478bd9Sstevel@tonic-gate 					  sm_errstring(errno));
9347c478bd9Sstevel@tonic-gate 			message("queued (user database error): %s",
9357c478bd9Sstevel@tonic-gate 				sm_errstring(errno));
9367c478bd9Sstevel@tonic-gate 			e->e_nrcpts++;
9377c478bd9Sstevel@tonic-gate 			goto testselfdestruct;
9387c478bd9Sstevel@tonic-gate 		}
9397c478bd9Sstevel@tonic-gate 	}
9407c478bd9Sstevel@tonic-gate #endif /* USERDB */
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	/*
9437c478bd9Sstevel@tonic-gate 	**  If we have a level two config file, then pass the name through
9447c478bd9Sstevel@tonic-gate 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
9457c478bd9Sstevel@tonic-gate 	**  to rewrite it to another mailer.  This gives us a hook
9467c478bd9Sstevel@tonic-gate 	**  after local aliasing has been done.
9477c478bd9Sstevel@tonic-gate 	*/
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	if (tTd(29, 5))
9507c478bd9Sstevel@tonic-gate 	{
9517c478bd9Sstevel@tonic-gate 		sm_dprintf("recipient: testing local?  cl=%d, rr5=%p\n\t",
9527c478bd9Sstevel@tonic-gate 			   ConfigLevel, RewriteRules[5]);
9537c478bd9Sstevel@tonic-gate 		printaddr(sm_debug_file(), new, false);
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 	if (ConfigLevel >= 2 && RewriteRules[5] != NULL &&
9567c478bd9Sstevel@tonic-gate 	    bitnset(M_TRYRULESET5, m->m_flags) &&
9577c478bd9Sstevel@tonic-gate 	    !bitset(QNOTREMOTE, new->q_flags) &&
9587c478bd9Sstevel@tonic-gate 	    QS_IS_OK(new->q_state))
9597c478bd9Sstevel@tonic-gate 	{
9607c478bd9Sstevel@tonic-gate 		maplocaluser(new, sendq, aliaslevel + 1, e);
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	/*
9647c478bd9Sstevel@tonic-gate 	**  If it didn't get rewritten to another mailer, go ahead
9657c478bd9Sstevel@tonic-gate 	**  and deliver it.
9667c478bd9Sstevel@tonic-gate 	*/
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (QS_IS_OK(new->q_state) &&
9697c478bd9Sstevel@tonic-gate 	    bitnset(M_HASPWENT, m->m_flags))
9707c478bd9Sstevel@tonic-gate 	{
9717c478bd9Sstevel@tonic-gate 		auto bool fuzzy;
9727c478bd9Sstevel@tonic-gate 		SM_MBDB_T user;
9737c478bd9Sstevel@tonic-gate 		int status;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 		/* warning -- finduser may trash buf */
9767c478bd9Sstevel@tonic-gate 		status = finduser(buf, &fuzzy, &user);
9777c478bd9Sstevel@tonic-gate 		switch (status)
9787c478bd9Sstevel@tonic-gate 		{
9797c478bd9Sstevel@tonic-gate 		  case EX_TEMPFAIL:
9807c478bd9Sstevel@tonic-gate 			new->q_state = QS_QUEUEUP;
9817c478bd9Sstevel@tonic-gate 			new->q_status = "4.5.2";
9827c478bd9Sstevel@tonic-gate 			giveresponse(EX_TEMPFAIL, new->q_status, m, NULL,
9837c478bd9Sstevel@tonic-gate 				     new->q_alias, (time_t) 0, e, new);
9847c478bd9Sstevel@tonic-gate 			break;
9857c478bd9Sstevel@tonic-gate 		  default:
9867c478bd9Sstevel@tonic-gate 			new->q_state = QS_BADADDR;
9877c478bd9Sstevel@tonic-gate 			new->q_status = "5.1.1";
9887c478bd9Sstevel@tonic-gate 			new->q_rstatus = "550 5.1.1 User unknown";
9897c478bd9Sstevel@tonic-gate 			giveresponse(EX_NOUSER, new->q_status, m, NULL,
9907c478bd9Sstevel@tonic-gate 				     new->q_alias, (time_t) 0, e, new);
9917c478bd9Sstevel@tonic-gate 			break;
9927c478bd9Sstevel@tonic-gate 		  case EX_OK:
9937c478bd9Sstevel@tonic-gate 			if (fuzzy)
9947c478bd9Sstevel@tonic-gate 			{
9957c478bd9Sstevel@tonic-gate 				/* name was a fuzzy match */
9967c478bd9Sstevel@tonic-gate 				new->q_user = sm_rpool_strdup_x(e->e_rpool,
9977c478bd9Sstevel@tonic-gate 								user.mbdb_name);
9987c478bd9Sstevel@tonic-gate 				if (findusercount++ > 3)
9997c478bd9Sstevel@tonic-gate 				{
10007c478bd9Sstevel@tonic-gate 					new->q_state = QS_BADADDR;
10017c478bd9Sstevel@tonic-gate 					new->q_status = "5.4.6";
10027c478bd9Sstevel@tonic-gate 					usrerrenh(new->q_status,
10037c478bd9Sstevel@tonic-gate 						  "554 aliasing/forwarding loop for %s broken",
10047c478bd9Sstevel@tonic-gate 						  user.mbdb_name);
10057c478bd9Sstevel@tonic-gate 					goto done;
10067c478bd9Sstevel@tonic-gate 				}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 				/* see if it aliases */
10097c478bd9Sstevel@tonic-gate 				(void) sm_strlcpy(buf, user.mbdb_name, buflen);
10107c478bd9Sstevel@tonic-gate 				goto trylocaluser;
10117c478bd9Sstevel@tonic-gate 			}
10127c478bd9Sstevel@tonic-gate 			if (*user.mbdb_homedir == '\0')
10137c478bd9Sstevel@tonic-gate 				new->q_home = NULL;
10147c478bd9Sstevel@tonic-gate 			else if (strcmp(user.mbdb_homedir, "/") == 0)
10157c478bd9Sstevel@tonic-gate 				new->q_home = "";
10167c478bd9Sstevel@tonic-gate 			else
10177c478bd9Sstevel@tonic-gate 				new->q_home = sm_rpool_strdup_x(e->e_rpool,
10187c478bd9Sstevel@tonic-gate 							user.mbdb_homedir);
10197c478bd9Sstevel@tonic-gate 			if (user.mbdb_uid != SM_NO_UID)
10207c478bd9Sstevel@tonic-gate 			{
10217c478bd9Sstevel@tonic-gate 				new->q_uid = user.mbdb_uid;
10227c478bd9Sstevel@tonic-gate 				new->q_gid = user.mbdb_gid;
10237c478bd9Sstevel@tonic-gate 				new->q_flags |= QGOODUID;
10247c478bd9Sstevel@tonic-gate 			}
10257c478bd9Sstevel@tonic-gate 			new->q_ruser = sm_rpool_strdup_x(e->e_rpool,
10267c478bd9Sstevel@tonic-gate 							 user.mbdb_name);
10277c478bd9Sstevel@tonic-gate 			if (user.mbdb_fullname[0] != '\0')
10287c478bd9Sstevel@tonic-gate 				new->q_fullname = sm_rpool_strdup_x(e->e_rpool,
10297c478bd9Sstevel@tonic-gate 							user.mbdb_fullname);
10307c478bd9Sstevel@tonic-gate 			if (!usershellok(user.mbdb_name, user.mbdb_shell))
10317c478bd9Sstevel@tonic-gate 			{
10327c478bd9Sstevel@tonic-gate 				new->q_flags |= QBOGUSSHELL;
10337c478bd9Sstevel@tonic-gate 			}
10347c478bd9Sstevel@tonic-gate 			if (bitset(EF_VRFYONLY, e->e_flags))
10357c478bd9Sstevel@tonic-gate 			{
10367c478bd9Sstevel@tonic-gate 				/* don't do any more now */
10377c478bd9Sstevel@tonic-gate 				new->q_state = QS_VERIFIED;
10387c478bd9Sstevel@tonic-gate 			}
10397c478bd9Sstevel@tonic-gate 			else if (!quoted)
10407c478bd9Sstevel@tonic-gate 				forward(new, sendq, aliaslevel, e);
10417c478bd9Sstevel@tonic-gate 		}
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 	if (!QS_IS_DEAD(new->q_state))
10447c478bd9Sstevel@tonic-gate 		e->e_nrcpts++;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate   testselfdestruct:
10477c478bd9Sstevel@tonic-gate 	new->q_flags |= QTHISPASS;
10487c478bd9Sstevel@tonic-gate 	if (tTd(26, 8))
10497c478bd9Sstevel@tonic-gate 	{
10507c478bd9Sstevel@tonic-gate 		sm_dprintf("testselfdestruct: ");
10517c478bd9Sstevel@tonic-gate 		printaddr(sm_debug_file(), new, false);
10527c478bd9Sstevel@tonic-gate 		if (tTd(26, 10))
10537c478bd9Sstevel@tonic-gate 		{
10547c478bd9Sstevel@tonic-gate 			sm_dprintf("SENDQ:\n");
10557c478bd9Sstevel@tonic-gate 			printaddr(sm_debug_file(), *sendq, true);
10567c478bd9Sstevel@tonic-gate 			sm_dprintf("----\n");
10577c478bd9Sstevel@tonic-gate 		}
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 	if (new->q_alias == NULL && new != &e->e_from &&
10607c478bd9Sstevel@tonic-gate 	    QS_IS_DEAD(new->q_state))
10617c478bd9Sstevel@tonic-gate 	{
10627c478bd9Sstevel@tonic-gate 		for (q = *sendq; q != NULL; q = q->q_next)
10637c478bd9Sstevel@tonic-gate 		{
10647c478bd9Sstevel@tonic-gate 			if (!QS_IS_DEAD(q->q_state))
10657c478bd9Sstevel@tonic-gate 				break;
10667c478bd9Sstevel@tonic-gate 		}
10677c478bd9Sstevel@tonic-gate 		if (q == NULL)
10687c478bd9Sstevel@tonic-gate 		{
10697c478bd9Sstevel@tonic-gate 			new->q_state = QS_BADADDR;
10707c478bd9Sstevel@tonic-gate 			new->q_status = "5.4.6";
10717c478bd9Sstevel@tonic-gate 			usrerrenh(new->q_status,
10727c478bd9Sstevel@tonic-gate 				  "554 aliasing/forwarding loop broken");
10737c478bd9Sstevel@tonic-gate 		}
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate   done:
10777c478bd9Sstevel@tonic-gate 	new->q_flags |= QTHISPASS;
10787c478bd9Sstevel@tonic-gate 	if (buf != buf0)
10797c478bd9Sstevel@tonic-gate 		sm_free(buf); /* XXX leak if above code raises exception */
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	/*
10827c478bd9Sstevel@tonic-gate 	**  If we are at the top level, check to see if this has
10837c478bd9Sstevel@tonic-gate 	**  expanded to exactly one address.  If so, it can inherit
10847c478bd9Sstevel@tonic-gate 	**  the primaryness of the address.
10857c478bd9Sstevel@tonic-gate 	**
10867c478bd9Sstevel@tonic-gate 	**  While we're at it, clear the QTHISPASS bits.
10877c478bd9Sstevel@tonic-gate 	*/
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	if (aliaslevel == 0)
10907c478bd9Sstevel@tonic-gate 	{
10917c478bd9Sstevel@tonic-gate 		int nrcpts = 0;
10927c478bd9Sstevel@tonic-gate 		ADDRESS *only = NULL;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 		for (q = *sendq; q != NULL; q = q->q_next)
10957c478bd9Sstevel@tonic-gate 		{
10967c478bd9Sstevel@tonic-gate 			if (bitset(QTHISPASS, q->q_flags) &&
10977c478bd9Sstevel@tonic-gate 			    QS_IS_SENDABLE(q->q_state))
10987c478bd9Sstevel@tonic-gate 			{
10997c478bd9Sstevel@tonic-gate 				nrcpts++;
11007c478bd9Sstevel@tonic-gate 				only = q;
11017c478bd9Sstevel@tonic-gate 			}
11027c478bd9Sstevel@tonic-gate 			q->q_flags &= ~QTHISPASS;
11037c478bd9Sstevel@tonic-gate 		}
11047c478bd9Sstevel@tonic-gate 		if (nrcpts == 1)
11057c478bd9Sstevel@tonic-gate 		{
11067c478bd9Sstevel@tonic-gate 			/* check to see if this actually got a new owner */
11077c478bd9Sstevel@tonic-gate 			q = only;
11087c478bd9Sstevel@tonic-gate 			while ((q = q->q_alias) != NULL)
11097c478bd9Sstevel@tonic-gate 			{
11107c478bd9Sstevel@tonic-gate 				if (q->q_owner != NULL)
11117c478bd9Sstevel@tonic-gate 					break;
11127c478bd9Sstevel@tonic-gate 			}
11137c478bd9Sstevel@tonic-gate 			if (q == NULL)
11147c478bd9Sstevel@tonic-gate 				only->q_flags |= QPRIMARY;
11157c478bd9Sstevel@tonic-gate 		}
11167c478bd9Sstevel@tonic-gate 		else if (!initialdontsend && nrcpts > 0)
11177c478bd9Sstevel@tonic-gate 		{
11187c478bd9Sstevel@tonic-gate 			/* arrange for return receipt */
11197c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_SENDRECEIPT;
11207c478bd9Sstevel@tonic-gate 			new->q_flags |= QEXPANDED;
11217c478bd9Sstevel@tonic-gate 			if (e->e_xfp != NULL &&
11227c478bd9Sstevel@tonic-gate 			    bitset(QPINGONSUCCESS, new->q_flags))
11237c478bd9Sstevel@tonic-gate 				(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
11247c478bd9Sstevel@tonic-gate 						     "%s... expanded to multiple addresses\n",
11257c478bd9Sstevel@tonic-gate 						     new->q_paddr);
11267c478bd9Sstevel@tonic-gate 		}
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate 	new->q_flags |= QRCPTOK;
1129058561cbSjbeck 	(void) sm_snprintf(buf0, sizeof(buf0), "%d", e->e_nrcpts);
11307c478bd9Sstevel@tonic-gate 	macdefine(&e->e_macro, A_TEMP, macid("{nrcpts}"), buf0);
11317c478bd9Sstevel@tonic-gate 	return new;
11327c478bd9Sstevel@tonic-gate }
1133058561cbSjbeck 
11347c478bd9Sstevel@tonic-gate /*
11357c478bd9Sstevel@tonic-gate **  FINDUSER -- find the password entry for a user.
11367c478bd9Sstevel@tonic-gate **
11377c478bd9Sstevel@tonic-gate **	This looks a lot like getpwnam, except that it may want to
11387c478bd9Sstevel@tonic-gate **	do some fancier pattern matching in /etc/passwd.
11397c478bd9Sstevel@tonic-gate **
11407c478bd9Sstevel@tonic-gate **	This routine contains most of the time of many sendmail runs.
11417c478bd9Sstevel@tonic-gate **	It deserves to be optimized.
11427c478bd9Sstevel@tonic-gate **
11437c478bd9Sstevel@tonic-gate **	Parameters:
11447c478bd9Sstevel@tonic-gate **		name -- the name to match against.
11457c478bd9Sstevel@tonic-gate **		fuzzyp -- an outarg that is set to true if this entry
11467c478bd9Sstevel@tonic-gate **			was found using the fuzzy matching algorithm;
11477c478bd9Sstevel@tonic-gate **			set to false otherwise.
11487c478bd9Sstevel@tonic-gate **		user -- structure to fill in if user is found
11497c478bd9Sstevel@tonic-gate **
11507c478bd9Sstevel@tonic-gate **	Returns:
11517c478bd9Sstevel@tonic-gate **		On success, fill in *user, set *fuzzyp and return EX_OK.
11527c478bd9Sstevel@tonic-gate **		If the user was not found, return EX_NOUSER.
11537c478bd9Sstevel@tonic-gate **		On error, return EX_TEMPFAIL or EX_OSERR.
11547c478bd9Sstevel@tonic-gate **
11557c478bd9Sstevel@tonic-gate **	Side Effects:
11567c478bd9Sstevel@tonic-gate **		may modify name.
11577c478bd9Sstevel@tonic-gate */
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate int
finduser(name,fuzzyp,user)11607c478bd9Sstevel@tonic-gate finduser(name, fuzzyp, user)
11617c478bd9Sstevel@tonic-gate 	char *name;
11627c478bd9Sstevel@tonic-gate 	bool *fuzzyp;
11637c478bd9Sstevel@tonic-gate 	SM_MBDB_T *user;
11647c478bd9Sstevel@tonic-gate {
11657c478bd9Sstevel@tonic-gate #if MATCHGECOS
11667c478bd9Sstevel@tonic-gate 	register struct passwd *pw;
11677c478bd9Sstevel@tonic-gate #endif /* MATCHGECOS */
11687c478bd9Sstevel@tonic-gate 	register char *p;
11697c478bd9Sstevel@tonic-gate 	bool tryagain;
11707c478bd9Sstevel@tonic-gate 	int status;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	if (tTd(29, 4))
11737c478bd9Sstevel@tonic-gate 		sm_dprintf("finduser(%s): ", name);
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	*fuzzyp = false;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate #if HESIOD
11787c478bd9Sstevel@tonic-gate 	/* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */
11797c478bd9Sstevel@tonic-gate 	for (p = name; *p != '\0'; p++)
11807c478bd9Sstevel@tonic-gate 		if (!isascii(*p) || !isdigit(*p))
11817c478bd9Sstevel@tonic-gate 			break;
11827c478bd9Sstevel@tonic-gate 	if (*p == '\0')
11837c478bd9Sstevel@tonic-gate 	{
11847c478bd9Sstevel@tonic-gate 		if (tTd(29, 4))
11857c478bd9Sstevel@tonic-gate 			sm_dprintf("failed (numeric input)\n");
11867c478bd9Sstevel@tonic-gate 		return EX_NOUSER;
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate #endif /* HESIOD */
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	/* look up this login name using fast path */
11917c478bd9Sstevel@tonic-gate 	status = sm_mbdb_lookup(name, user);
11927c478bd9Sstevel@tonic-gate 	if (status != EX_NOUSER)
11937c478bd9Sstevel@tonic-gate 	{
11947c478bd9Sstevel@tonic-gate 		if (tTd(29, 4))
11957c478bd9Sstevel@tonic-gate 			sm_dprintf("%s (non-fuzzy)\n", sm_strexit(status));
11967c478bd9Sstevel@tonic-gate 		return status;
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	/* try mapping it to lower case */
12007c478bd9Sstevel@tonic-gate 	tryagain = false;
12017c478bd9Sstevel@tonic-gate 	for (p = name; *p != '\0'; p++)
12027c478bd9Sstevel@tonic-gate 	{
12037c478bd9Sstevel@tonic-gate 		if (isascii(*p) && isupper(*p))
12047c478bd9Sstevel@tonic-gate 		{
12057c478bd9Sstevel@tonic-gate 			*p = tolower(*p);
12067c478bd9Sstevel@tonic-gate 			tryagain = true;
12077c478bd9Sstevel@tonic-gate 		}
12087c478bd9Sstevel@tonic-gate 	}
12097c478bd9Sstevel@tonic-gate 	if (tryagain && (status = sm_mbdb_lookup(name, user)) != EX_NOUSER)
12107c478bd9Sstevel@tonic-gate 	{
12117c478bd9Sstevel@tonic-gate 		if (tTd(29, 4))
12127c478bd9Sstevel@tonic-gate 			sm_dprintf("%s (lower case)\n", sm_strexit(status));
12137c478bd9Sstevel@tonic-gate 		*fuzzyp = true;
12147c478bd9Sstevel@tonic-gate 		return status;
12157c478bd9Sstevel@tonic-gate 	}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate #if MATCHGECOS
12187c478bd9Sstevel@tonic-gate 	/* see if fuzzy matching allowed */
12197c478bd9Sstevel@tonic-gate 	if (!MatchGecos)
12207c478bd9Sstevel@tonic-gate 	{
12217c478bd9Sstevel@tonic-gate 		if (tTd(29, 4))
12227c478bd9Sstevel@tonic-gate 			sm_dprintf("not found (fuzzy disabled)\n");
12237c478bd9Sstevel@tonic-gate 		return EX_NOUSER;
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	/* search for a matching full name instead */
12277c478bd9Sstevel@tonic-gate 	for (p = name; *p != '\0'; p++)
12287c478bd9Sstevel@tonic-gate 	{
12297c478bd9Sstevel@tonic-gate 		if (*p == (SpaceSub & 0177) || *p == '_')
12307c478bd9Sstevel@tonic-gate 			*p = ' ';
12317c478bd9Sstevel@tonic-gate 	}
12327c478bd9Sstevel@tonic-gate 	(void) setpwent();
12337c478bd9Sstevel@tonic-gate 	while ((pw = getpwent()) != NULL)
12347c478bd9Sstevel@tonic-gate 	{
12357c478bd9Sstevel@tonic-gate 		char buf[MAXNAME + 1];
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate # if 0
12387c478bd9Sstevel@tonic-gate 		if (sm_strcasecmp(pw->pw_name, name) == 0)
12397c478bd9Sstevel@tonic-gate 		{
12407c478bd9Sstevel@tonic-gate 			if (tTd(29, 4))
12417c478bd9Sstevel@tonic-gate 				sm_dprintf("found (case wrapped)\n");
12427c478bd9Sstevel@tonic-gate 			break;
12437c478bd9Sstevel@tonic-gate 		}
12447c478bd9Sstevel@tonic-gate # endif /* 0 */
12457c478bd9Sstevel@tonic-gate 
1246058561cbSjbeck 		sm_pwfullname(pw->pw_gecos, pw->pw_name, buf, sizeof(buf));
12477c478bd9Sstevel@tonic-gate 		if (strchr(buf, ' ') != NULL && sm_strcasecmp(buf, name) == 0)
12487c478bd9Sstevel@tonic-gate 		{
12497c478bd9Sstevel@tonic-gate 			if (tTd(29, 4))
12507c478bd9Sstevel@tonic-gate 				sm_dprintf("fuzzy matches %s\n", pw->pw_name);
12517c478bd9Sstevel@tonic-gate 			message("sending to login name %s", pw->pw_name);
12527c478bd9Sstevel@tonic-gate 			break;
12537c478bd9Sstevel@tonic-gate 		}
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate 	if (pw != NULL)
12567c478bd9Sstevel@tonic-gate 		*fuzzyp = true;
12577c478bd9Sstevel@tonic-gate 	else if (tTd(29, 4))
12587c478bd9Sstevel@tonic-gate 		sm_dprintf("no fuzzy match found\n");
12597c478bd9Sstevel@tonic-gate # if DEC_OSF_BROKEN_GETPWENT	/* DEC OSF/1 3.2 or earlier */
12607c478bd9Sstevel@tonic-gate 	endpwent();
12617c478bd9Sstevel@tonic-gate # endif /* DEC_OSF_BROKEN_GETPWENT */
12627c478bd9Sstevel@tonic-gate 	if (pw == NULL)
12637c478bd9Sstevel@tonic-gate 		return EX_NOUSER;
12647c478bd9Sstevel@tonic-gate 	sm_mbdb_frompw(user, pw);
12657c478bd9Sstevel@tonic-gate 	return EX_OK;
12667c478bd9Sstevel@tonic-gate #else /* MATCHGECOS */
12677c478bd9Sstevel@tonic-gate 	if (tTd(29, 4))
12687c478bd9Sstevel@tonic-gate 		sm_dprintf("not found (fuzzy disabled)\n");
12697c478bd9Sstevel@tonic-gate 	return EX_NOUSER;
12707c478bd9Sstevel@tonic-gate #endif /* MATCHGECOS */
12717c478bd9Sstevel@tonic-gate }
1272058561cbSjbeck 
12737c478bd9Sstevel@tonic-gate /*
12747c478bd9Sstevel@tonic-gate **  WRITABLE -- predicate returning if the file is writable.
12757c478bd9Sstevel@tonic-gate **
12767c478bd9Sstevel@tonic-gate **	This routine must duplicate the algorithm in sys/fio.c.
12777c478bd9Sstevel@tonic-gate **	Unfortunately, we cannot use the access call since we
12787c478bd9Sstevel@tonic-gate **	won't necessarily be the real uid when we try to
12797c478bd9Sstevel@tonic-gate **	actually open the file.
12807c478bd9Sstevel@tonic-gate **
12817c478bd9Sstevel@tonic-gate **	Notice that ANY file with ANY execute bit is automatically
12827c478bd9Sstevel@tonic-gate **	not writable.  This is also enforced by mailfile.
12837c478bd9Sstevel@tonic-gate **
12847c478bd9Sstevel@tonic-gate **	Parameters:
12857c478bd9Sstevel@tonic-gate **		filename -- the file name to check.
12867c478bd9Sstevel@tonic-gate **		ctladdr -- the controlling address for this file.
12877c478bd9Sstevel@tonic-gate **		flags -- SFF_* flags to control the function.
12887c478bd9Sstevel@tonic-gate **
12897c478bd9Sstevel@tonic-gate **	Returns:
12907c478bd9Sstevel@tonic-gate **		true -- if we will be able to write this file.
12917c478bd9Sstevel@tonic-gate **		false -- if we cannot write this file.
12927c478bd9Sstevel@tonic-gate **
12937c478bd9Sstevel@tonic-gate **	Side Effects:
12947c478bd9Sstevel@tonic-gate **		none.
12957c478bd9Sstevel@tonic-gate */
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate bool
writable(filename,ctladdr,flags)12987c478bd9Sstevel@tonic-gate writable(filename, ctladdr, flags)
12997c478bd9Sstevel@tonic-gate 	char *filename;
13007c478bd9Sstevel@tonic-gate 	ADDRESS *ctladdr;
13017c478bd9Sstevel@tonic-gate 	long flags;
13027c478bd9Sstevel@tonic-gate {
13037c478bd9Sstevel@tonic-gate 	uid_t euid = 0;
13047c478bd9Sstevel@tonic-gate 	gid_t egid = 0;
13057c478bd9Sstevel@tonic-gate 	char *user = NULL;
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	if (tTd(44, 5))
13087c478bd9Sstevel@tonic-gate 		sm_dprintf("writable(%s, 0x%lx)\n", filename, flags);
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	/*
13117c478bd9Sstevel@tonic-gate 	**  File does exist -- check that it is writable.
13127c478bd9Sstevel@tonic-gate 	*/
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	if (geteuid() != 0)
13157c478bd9Sstevel@tonic-gate 	{
13167c478bd9Sstevel@tonic-gate 		euid = geteuid();
13177c478bd9Sstevel@tonic-gate 		egid = getegid();
13187c478bd9Sstevel@tonic-gate 		user = NULL;
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 	else if (ctladdr != NULL)
13217c478bd9Sstevel@tonic-gate 	{
13227c478bd9Sstevel@tonic-gate 		euid = ctladdr->q_uid;
13237c478bd9Sstevel@tonic-gate 		egid = ctladdr->q_gid;
13247c478bd9Sstevel@tonic-gate 		user = ctladdr->q_user;
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate 	else if (bitset(SFF_RUNASREALUID, flags))
13277c478bd9Sstevel@tonic-gate 	{
13287c478bd9Sstevel@tonic-gate 		euid = RealUid;
13297c478bd9Sstevel@tonic-gate 		egid = RealGid;
13307c478bd9Sstevel@tonic-gate 		user = RealUserName;
13317c478bd9Sstevel@tonic-gate 	}
13327c478bd9Sstevel@tonic-gate 	else if (FileMailer != NULL && !bitset(SFF_ROOTOK, flags))
13337c478bd9Sstevel@tonic-gate 	{
13347c478bd9Sstevel@tonic-gate 		if (FileMailer->m_uid == NO_UID)
13357c478bd9Sstevel@tonic-gate 		{
13367c478bd9Sstevel@tonic-gate 			euid = DefUid;
13377c478bd9Sstevel@tonic-gate 			user = DefUser;
13387c478bd9Sstevel@tonic-gate 		}
13397c478bd9Sstevel@tonic-gate 		else
13407c478bd9Sstevel@tonic-gate 		{
13417c478bd9Sstevel@tonic-gate 			euid = FileMailer->m_uid;
13427c478bd9Sstevel@tonic-gate 			user = NULL;
13437c478bd9Sstevel@tonic-gate 		}
13447c478bd9Sstevel@tonic-gate 		if (FileMailer->m_gid == NO_GID)
13457c478bd9Sstevel@tonic-gate 			egid = DefGid;
13467c478bd9Sstevel@tonic-gate 		else
13477c478bd9Sstevel@tonic-gate 			egid = FileMailer->m_gid;
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate 	else
13507c478bd9Sstevel@tonic-gate 	{
13517c478bd9Sstevel@tonic-gate 		euid = egid = 0;
13527c478bd9Sstevel@tonic-gate 		user = NULL;
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 	if (!bitset(SFF_ROOTOK, flags))
13557c478bd9Sstevel@tonic-gate 	{
13567c478bd9Sstevel@tonic-gate 		if (euid == 0)
13577c478bd9Sstevel@tonic-gate 		{
13587c478bd9Sstevel@tonic-gate 			euid = DefUid;
13597c478bd9Sstevel@tonic-gate 			user = DefUser;
13607c478bd9Sstevel@tonic-gate 		}
13617c478bd9Sstevel@tonic-gate 		if (egid == 0)
13627c478bd9Sstevel@tonic-gate 			egid = DefGid;
13637c478bd9Sstevel@tonic-gate 	}
13647c478bd9Sstevel@tonic-gate 	if (geteuid() == 0 &&
13657c478bd9Sstevel@tonic-gate 	    (ctladdr == NULL || !bitset(QGOODUID, ctladdr->q_flags)))
13667c478bd9Sstevel@tonic-gate 		flags |= SFF_SETUIDOK;
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	if (!bitnset(DBS_FILEDELIVERYTOSYMLINK, DontBlameSendmail))
13697c478bd9Sstevel@tonic-gate 		flags |= SFF_NOSLINK;
13707c478bd9Sstevel@tonic-gate 	if (!bitnset(DBS_FILEDELIVERYTOHARDLINK, DontBlameSendmail))
13717c478bd9Sstevel@tonic-gate 		flags |= SFF_NOHLINK;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	errno = safefile(filename, euid, egid, user, flags, S_IWRITE, NULL);
13747c478bd9Sstevel@tonic-gate 	return errno == 0;
13757c478bd9Sstevel@tonic-gate }
1376058561cbSjbeck 
13777c478bd9Sstevel@tonic-gate /*
13787c478bd9Sstevel@tonic-gate **  INCLUDE -- handle :include: specification.
13797c478bd9Sstevel@tonic-gate **
13807c478bd9Sstevel@tonic-gate **	Parameters:
13817c478bd9Sstevel@tonic-gate **		fname -- filename to include.
13827c478bd9Sstevel@tonic-gate **		forwarding -- if true, we are reading a .forward file.
13837c478bd9Sstevel@tonic-gate **			if false, it's a :include: file.
13847c478bd9Sstevel@tonic-gate **		ctladdr -- address template to use to fill in these
13857c478bd9Sstevel@tonic-gate **			addresses -- effective user/group id are
13867c478bd9Sstevel@tonic-gate **			the important things.
13877c478bd9Sstevel@tonic-gate **		sendq -- a pointer to the head of the send queue
13887c478bd9Sstevel@tonic-gate **			to put these addresses in.
13897c478bd9Sstevel@tonic-gate **		aliaslevel -- the alias nesting depth.
13907c478bd9Sstevel@tonic-gate **		e -- the current envelope.
13917c478bd9Sstevel@tonic-gate **
13927c478bd9Sstevel@tonic-gate **	Returns:
13937c478bd9Sstevel@tonic-gate **		open error status
13947c478bd9Sstevel@tonic-gate **
13957c478bd9Sstevel@tonic-gate **	Side Effects:
13967c478bd9Sstevel@tonic-gate **		reads the :include: file and sends to everyone
13977c478bd9Sstevel@tonic-gate **		listed in that file.
13987c478bd9Sstevel@tonic-gate **
13997c478bd9Sstevel@tonic-gate **	Security Note:
14007c478bd9Sstevel@tonic-gate **		If you have restricted chown (that is, you can't
14017c478bd9Sstevel@tonic-gate **		give a file away), it is reasonable to allow programs
14027c478bd9Sstevel@tonic-gate **		and files called from this :include: file to be to be
14037c478bd9Sstevel@tonic-gate **		run as the owner of the :include: file.  This is bogus
14047c478bd9Sstevel@tonic-gate **		if there is any chance of someone giving away a file.
14057c478bd9Sstevel@tonic-gate **		We assume that pre-POSIX systems can give away files.
14067c478bd9Sstevel@tonic-gate **
14077c478bd9Sstevel@tonic-gate **		There is an additional restriction that if you
14087c478bd9Sstevel@tonic-gate **		forward to a :include: file, it will not take on
14097c478bd9Sstevel@tonic-gate **		the ownership of the :include: file.  This may not
14107c478bd9Sstevel@tonic-gate **		be necessary, but shouldn't hurt.
14117c478bd9Sstevel@tonic-gate */
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate static jmp_buf	CtxIncludeTimeout;
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate int
include(fname,forwarding,ctladdr,sendq,aliaslevel,e)14167c478bd9Sstevel@tonic-gate include(fname, forwarding, ctladdr, sendq, aliaslevel, e)
14177c478bd9Sstevel@tonic-gate 	char *fname;
14187c478bd9Sstevel@tonic-gate 	bool forwarding;
14197c478bd9Sstevel@tonic-gate 	ADDRESS *ctladdr;
14207c478bd9Sstevel@tonic-gate 	ADDRESS **sendq;
14217c478bd9Sstevel@tonic-gate 	int aliaslevel;
14227c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
14237c478bd9Sstevel@tonic-gate {
14247c478bd9Sstevel@tonic-gate 	SM_FILE_T *volatile fp = NULL;
14257c478bd9Sstevel@tonic-gate 	char *oldto = e->e_to;
14267c478bd9Sstevel@tonic-gate 	char *oldfilename = FileName;
14277c478bd9Sstevel@tonic-gate 	int oldlinenumber = LineNumber;
14287c478bd9Sstevel@tonic-gate 	register SM_EVENT *ev = NULL;
14297c478bd9Sstevel@tonic-gate 	int nincludes;
14307c478bd9Sstevel@tonic-gate 	int mode;
14317c478bd9Sstevel@tonic-gate 	volatile bool maxreached = false;
14327c478bd9Sstevel@tonic-gate 	register ADDRESS *ca;
14337c478bd9Sstevel@tonic-gate 	volatile uid_t saveduid;
14347c478bd9Sstevel@tonic-gate 	volatile gid_t savedgid;
14357c478bd9Sstevel@tonic-gate 	volatile uid_t uid;
14367c478bd9Sstevel@tonic-gate 	volatile gid_t gid;
14377c478bd9Sstevel@tonic-gate 	char *volatile user;
14387c478bd9Sstevel@tonic-gate 	int rval = 0;
14397c478bd9Sstevel@tonic-gate 	volatile long sfflags = SFF_REGONLY;
14407c478bd9Sstevel@tonic-gate 	register char *p;
14417c478bd9Sstevel@tonic-gate 	bool safechown = false;
14427c478bd9Sstevel@tonic-gate 	volatile bool safedir = false;
14437c478bd9Sstevel@tonic-gate 	struct stat st;
14447c478bd9Sstevel@tonic-gate 	char buf[MAXLINE];
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	if (tTd(27, 2))
14477c478bd9Sstevel@tonic-gate 		sm_dprintf("include(%s)\n", fname);
14487c478bd9Sstevel@tonic-gate 	if (tTd(27, 4))
14497c478bd9Sstevel@tonic-gate 		sm_dprintf("   ruid=%d euid=%d\n",
14507c478bd9Sstevel@tonic-gate 			(int) getuid(), (int) geteuid());
14517c478bd9Sstevel@tonic-gate 	if (tTd(27, 14))
14527c478bd9Sstevel@tonic-gate 	{
14537c478bd9Sstevel@tonic-gate 		sm_dprintf("ctladdr ");
14547c478bd9Sstevel@tonic-gate 		printaddr(sm_debug_file(), ctladdr, false);
14557c478bd9Sstevel@tonic-gate 	}
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	if (tTd(27, 9))
14587c478bd9Sstevel@tonic-gate 		sm_dprintf("include: old uid = %d/%d\n",
14597c478bd9Sstevel@tonic-gate 			   (int) getuid(), (int) geteuid());
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	if (forwarding)
14627c478bd9Sstevel@tonic-gate 	{
14637c478bd9Sstevel@tonic-gate 		sfflags |= SFF_MUSTOWN|SFF_ROOTOK;
14647c478bd9Sstevel@tonic-gate 		if (!bitnset(DBS_GROUPWRITABLEFORWARDFILE, DontBlameSendmail))
14657c478bd9Sstevel@tonic-gate 			sfflags |= SFF_NOGWFILES;
14667c478bd9Sstevel@tonic-gate 		if (!bitnset(DBS_WORLDWRITABLEFORWARDFILE, DontBlameSendmail))
14677c478bd9Sstevel@tonic-gate 			sfflags |= SFF_NOWWFILES;
14687c478bd9Sstevel@tonic-gate 	}
14697c478bd9Sstevel@tonic-gate 	else
14707c478bd9Sstevel@tonic-gate 	{
14717c478bd9Sstevel@tonic-gate 		if (!bitnset(DBS_GROUPWRITABLEINCLUDEFILE, DontBlameSendmail))
14727c478bd9Sstevel@tonic-gate 			sfflags |= SFF_NOGWFILES;
14737c478bd9Sstevel@tonic-gate 		if (!bitnset(DBS_WORLDWRITABLEINCLUDEFILE, DontBlameSendmail))
14747c478bd9Sstevel@tonic-gate 			sfflags |= SFF_NOWWFILES;
14757c478bd9Sstevel@tonic-gate 	}
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	/*
14787c478bd9Sstevel@tonic-gate 	**  If RunAsUser set, won't be able to run programs as user
14797c478bd9Sstevel@tonic-gate 	**  so mark them as unsafe unless the administrator knows better.
14807c478bd9Sstevel@tonic-gate 	*/
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	if ((geteuid() != 0 || RunAsUid != 0) &&
14837c478bd9Sstevel@tonic-gate 	    !bitnset(DBS_NONROOTSAFEADDR, DontBlameSendmail))
14847c478bd9Sstevel@tonic-gate 	{
14857c478bd9Sstevel@tonic-gate 		if (tTd(27, 4))
14867c478bd9Sstevel@tonic-gate 			sm_dprintf("include: not safe (euid=%d, RunAsUid=%d)\n",
14877c478bd9Sstevel@tonic-gate 				   (int) geteuid(), (int) RunAsUid);
14887c478bd9Sstevel@tonic-gate 		ctladdr->q_flags |= QUNSAFEADDR;
14897c478bd9Sstevel@tonic-gate 	}
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	ca = getctladdr(ctladdr);
14927c478bd9Sstevel@tonic-gate 	if (ca == NULL ||
14937c478bd9Sstevel@tonic-gate 	    (ca->q_uid == DefUid && ca->q_gid == 0))
14947c478bd9Sstevel@tonic-gate 	{
14957c478bd9Sstevel@tonic-gate 		uid = DefUid;
14967c478bd9Sstevel@tonic-gate 		gid = DefGid;
14977c478bd9Sstevel@tonic-gate 		user = DefUser;
14987c478bd9Sstevel@tonic-gate 	}
14997c478bd9Sstevel@tonic-gate 	else
15007c478bd9Sstevel@tonic-gate 	{
15017c478bd9Sstevel@tonic-gate 		uid = ca->q_uid;
15027c478bd9Sstevel@tonic-gate 		gid = ca->q_gid;
15037c478bd9Sstevel@tonic-gate 		user = ca->q_user;
15047c478bd9Sstevel@tonic-gate 	}
15057c478bd9Sstevel@tonic-gate #if MAILER_SETUID_METHOD != USE_SETUID
15067c478bd9Sstevel@tonic-gate 	saveduid = geteuid();
15077c478bd9Sstevel@tonic-gate 	savedgid = getegid();
15087c478bd9Sstevel@tonic-gate 	if (saveduid == 0)
15097c478bd9Sstevel@tonic-gate 	{
15107c478bd9Sstevel@tonic-gate 		if (!DontInitGroups)
15117c478bd9Sstevel@tonic-gate 		{
15127c478bd9Sstevel@tonic-gate 			if (initgroups(user, gid) == -1)
15137c478bd9Sstevel@tonic-gate 			{
15147c478bd9Sstevel@tonic-gate 				rval = EAGAIN;
15157c478bd9Sstevel@tonic-gate 				syserr("include: initgroups(%s, %d) failed",
15167c478bd9Sstevel@tonic-gate 					user, gid);
15177c478bd9Sstevel@tonic-gate 				goto resetuid;
15187c478bd9Sstevel@tonic-gate 			}
15197c478bd9Sstevel@tonic-gate 		}
15207c478bd9Sstevel@tonic-gate 		else
15217c478bd9Sstevel@tonic-gate 		{
15227c478bd9Sstevel@tonic-gate 			GIDSET_T gidset[1];
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 			gidset[0] = gid;
15257c478bd9Sstevel@tonic-gate 			if (setgroups(1, gidset) == -1)
15267c478bd9Sstevel@tonic-gate 			{
15277c478bd9Sstevel@tonic-gate 				rval = EAGAIN;
15287c478bd9Sstevel@tonic-gate 				syserr("include: setgroups() failed");
15297c478bd9Sstevel@tonic-gate 				goto resetuid;
15307c478bd9Sstevel@tonic-gate 			}
15317c478bd9Sstevel@tonic-gate 		}
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 		if (gid != 0 && setgid(gid) < -1)
15347c478bd9Sstevel@tonic-gate 		{
15357c478bd9Sstevel@tonic-gate 			rval = EAGAIN;
15367c478bd9Sstevel@tonic-gate 			syserr("setgid(%d) failure", gid);
15377c478bd9Sstevel@tonic-gate 			goto resetuid;
15387c478bd9Sstevel@tonic-gate 		}
15397c478bd9Sstevel@tonic-gate 		if (uid != 0)
15407c478bd9Sstevel@tonic-gate 		{
15417c478bd9Sstevel@tonic-gate # if MAILER_SETUID_METHOD == USE_SETEUID
15427c478bd9Sstevel@tonic-gate 			if (seteuid(uid) < 0)
15437c478bd9Sstevel@tonic-gate 			{
15447c478bd9Sstevel@tonic-gate 				rval = EAGAIN;
15457c478bd9Sstevel@tonic-gate 				syserr("seteuid(%d) failure (real=%d, eff=%d)",
15467c478bd9Sstevel@tonic-gate 					uid, (int) getuid(), (int) geteuid());
15477c478bd9Sstevel@tonic-gate 				goto resetuid;
15487c478bd9Sstevel@tonic-gate 			}
15497c478bd9Sstevel@tonic-gate # endif /* MAILER_SETUID_METHOD == USE_SETEUID */
15507c478bd9Sstevel@tonic-gate # if MAILER_SETUID_METHOD == USE_SETREUID
15517c478bd9Sstevel@tonic-gate 			if (setreuid(0, uid) < 0)
15527c478bd9Sstevel@tonic-gate 			{
15537c478bd9Sstevel@tonic-gate 				rval = EAGAIN;
15547c478bd9Sstevel@tonic-gate 				syserr("setreuid(0, %d) failure (real=%d, eff=%d)",
15557c478bd9Sstevel@tonic-gate 					uid, (int) getuid(), (int) geteuid());
15567c478bd9Sstevel@tonic-gate 				goto resetuid;
15577c478bd9Sstevel@tonic-gate 			}
15587c478bd9Sstevel@tonic-gate # endif /* MAILER_SETUID_METHOD == USE_SETREUID */
15597c478bd9Sstevel@tonic-gate 		}
15607c478bd9Sstevel@tonic-gate 	}
15617c478bd9Sstevel@tonic-gate #endif /* MAILER_SETUID_METHOD != USE_SETUID */
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	if (tTd(27, 9))
15647c478bd9Sstevel@tonic-gate 		sm_dprintf("include: new uid = %d/%d\n",
15657c478bd9Sstevel@tonic-gate 			   (int) getuid(), (int) geteuid());
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	/*
15687c478bd9Sstevel@tonic-gate 	**  If home directory is remote mounted but server is down,
15697c478bd9Sstevel@tonic-gate 	**  this can hang or give errors; use a timeout to avoid this
15707c478bd9Sstevel@tonic-gate 	*/
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	if (setjmp(CtxIncludeTimeout) != 0)
15737c478bd9Sstevel@tonic-gate 	{
15747c478bd9Sstevel@tonic-gate 		ctladdr->q_state = QS_QUEUEUP;
15757c478bd9Sstevel@tonic-gate 		errno = 0;
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 		/* return pseudo-error code */
15787c478bd9Sstevel@tonic-gate 		rval = E_SM_OPENTIMEOUT;
15797c478bd9Sstevel@tonic-gate 		goto resetuid;
15807c478bd9Sstevel@tonic-gate 	}
15817c478bd9Sstevel@tonic-gate 	if (TimeOuts.to_fileopen > 0)
15827c478bd9Sstevel@tonic-gate 		ev = sm_setevent(TimeOuts.to_fileopen, includetimeout, 0);
15837c478bd9Sstevel@tonic-gate 	else
15847c478bd9Sstevel@tonic-gate 		ev = NULL;
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	/* check for writable parent directory */
15887c478bd9Sstevel@tonic-gate 	p = strrchr(fname, '/');
15897c478bd9Sstevel@tonic-gate 	if (p != NULL)
15907c478bd9Sstevel@tonic-gate 	{
15917c478bd9Sstevel@tonic-gate 		int ret;
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 		*p = '\0';
15947c478bd9Sstevel@tonic-gate 		ret = safedirpath(fname, uid, gid, user,
15957c478bd9Sstevel@tonic-gate 				  sfflags|SFF_SAFEDIRPATH, 0, 0);
15967c478bd9Sstevel@tonic-gate 		if (ret == 0)
15977c478bd9Sstevel@tonic-gate 		{
15987c478bd9Sstevel@tonic-gate 			/* in safe directory: relax chown & link rules */
15997c478bd9Sstevel@tonic-gate 			safedir = true;
16007c478bd9Sstevel@tonic-gate 			sfflags |= SFF_NOPATHCHECK;
16017c478bd9Sstevel@tonic-gate 		}
16027c478bd9Sstevel@tonic-gate 		else
16037c478bd9Sstevel@tonic-gate 		{
16047c478bd9Sstevel@tonic-gate 			if (bitnset((forwarding ?
16057c478bd9Sstevel@tonic-gate 				     DBS_FORWARDFILEINUNSAFEDIRPATH :
16067c478bd9Sstevel@tonic-gate 				     DBS_INCLUDEFILEINUNSAFEDIRPATH),
16077c478bd9Sstevel@tonic-gate 				    DontBlameSendmail))
16087c478bd9Sstevel@tonic-gate 				sfflags |= SFF_NOPATHCHECK;
16097c478bd9Sstevel@tonic-gate 			else if (bitnset((forwarding ?
16107c478bd9Sstevel@tonic-gate 					  DBS_FORWARDFILEINGROUPWRITABLEDIRPATH :
16117c478bd9Sstevel@tonic-gate 					  DBS_INCLUDEFILEINGROUPWRITABLEDIRPATH),
16127c478bd9Sstevel@tonic-gate 					 DontBlameSendmail) &&
16137c478bd9Sstevel@tonic-gate 				 ret == E_SM_GWDIR)
16147c478bd9Sstevel@tonic-gate 			{
16157c478bd9Sstevel@tonic-gate 				setbitn(DBS_GROUPWRITABLEDIRPATHSAFE,
16167c478bd9Sstevel@tonic-gate 					DontBlameSendmail);
16177c478bd9Sstevel@tonic-gate 				ret = safedirpath(fname, uid, gid, user,
16187c478bd9Sstevel@tonic-gate 						  sfflags|SFF_SAFEDIRPATH,
16197c478bd9Sstevel@tonic-gate 						  0, 0);
16207c478bd9Sstevel@tonic-gate 				clrbitn(DBS_GROUPWRITABLEDIRPATHSAFE,
16217c478bd9Sstevel@tonic-gate 					DontBlameSendmail);
16227c478bd9Sstevel@tonic-gate 				if (ret == 0)
16237c478bd9Sstevel@tonic-gate 					sfflags |= SFF_NOPATHCHECK;
16247c478bd9Sstevel@tonic-gate 				else
16257c478bd9Sstevel@tonic-gate 					sfflags |= SFF_SAFEDIRPATH;
16267c478bd9Sstevel@tonic-gate 			}
16277c478bd9Sstevel@tonic-gate 			else
16287c478bd9Sstevel@tonic-gate 				sfflags |= SFF_SAFEDIRPATH;
16297c478bd9Sstevel@tonic-gate 			if (ret > E_PSEUDOBASE &&
16307c478bd9Sstevel@tonic-gate 			    !bitnset((forwarding ?
16317c478bd9Sstevel@tonic-gate 				      DBS_FORWARDFILEINUNSAFEDIRPATHSAFE :
16327c478bd9Sstevel@tonic-gate 				      DBS_INCLUDEFILEINUNSAFEDIRPATHSAFE),
16337c478bd9Sstevel@tonic-gate 				     DontBlameSendmail))
16347c478bd9Sstevel@tonic-gate 			{
16357c478bd9Sstevel@tonic-gate 				if (LogLevel > 11)
16367c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
16377c478bd9Sstevel@tonic-gate 						  "%s: unsafe directory path, marked unsafe",
16387c478bd9Sstevel@tonic-gate 						  shortenstring(fname, MAXSHORTSTR));
16397c478bd9Sstevel@tonic-gate 				ctladdr->q_flags |= QUNSAFEADDR;
16407c478bd9Sstevel@tonic-gate 			}
16417c478bd9Sstevel@tonic-gate 		}
16427c478bd9Sstevel@tonic-gate 		*p = '/';
16437c478bd9Sstevel@tonic-gate 	}
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	/* allow links only in unwritable directories */
16467c478bd9Sstevel@tonic-gate 	if (!safedir &&
16477c478bd9Sstevel@tonic-gate 	    !bitnset((forwarding ?
16487c478bd9Sstevel@tonic-gate 		      DBS_LINKEDFORWARDFILEINWRITABLEDIR :
16497c478bd9Sstevel@tonic-gate 		      DBS_LINKEDINCLUDEFILEINWRITABLEDIR),
16507c478bd9Sstevel@tonic-gate 		     DontBlameSendmail))
16517c478bd9Sstevel@tonic-gate 		sfflags |= SFF_NOLINK;
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	rval = safefile(fname, uid, gid, user, sfflags, S_IREAD, &st);
16547c478bd9Sstevel@tonic-gate 	if (rval != 0)
16557c478bd9Sstevel@tonic-gate 	{
16567c478bd9Sstevel@tonic-gate 		/* don't use this :include: file */
16577c478bd9Sstevel@tonic-gate 		if (tTd(27, 4))
16587c478bd9Sstevel@tonic-gate 			sm_dprintf("include: not safe (uid=%d): %s\n",
16597c478bd9Sstevel@tonic-gate 				   (int) uid, sm_errstring(rval));
16607c478bd9Sstevel@tonic-gate 	}
16617c478bd9Sstevel@tonic-gate 	else if ((fp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, fname,
16627c478bd9Sstevel@tonic-gate 				  SM_IO_RDONLY, NULL)) == NULL)
16637c478bd9Sstevel@tonic-gate 	{
16647c478bd9Sstevel@tonic-gate 		rval = errno;
16657c478bd9Sstevel@tonic-gate 		if (tTd(27, 4))
16667c478bd9Sstevel@tonic-gate 			sm_dprintf("include: open: %s\n", sm_errstring(rval));
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 	else if (filechanged(fname, sm_io_getinfo(fp,SM_IO_WHAT_FD, NULL), &st))
16697c478bd9Sstevel@tonic-gate 	{
16707c478bd9Sstevel@tonic-gate 		rval = E_SM_FILECHANGE;
16717c478bd9Sstevel@tonic-gate 		if (tTd(27, 4))
16727c478bd9Sstevel@tonic-gate 			sm_dprintf("include: file changed after open\n");
16737c478bd9Sstevel@tonic-gate 	}
16747c478bd9Sstevel@tonic-gate 	if (ev != NULL)
16757c478bd9Sstevel@tonic-gate 		sm_clrevent(ev);
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate resetuid:
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate #if HASSETREUID || USESETEUID
16807c478bd9Sstevel@tonic-gate 	if (saveduid == 0)
16817c478bd9Sstevel@tonic-gate 	{
16827c478bd9Sstevel@tonic-gate 		if (uid != 0)
16837c478bd9Sstevel@tonic-gate 		{
16847c478bd9Sstevel@tonic-gate # if USESETEUID
16857c478bd9Sstevel@tonic-gate 			if (seteuid(0) < 0)
16867c478bd9Sstevel@tonic-gate 				syserr("!seteuid(0) failure (real=%d, eff=%d)",
16877c478bd9Sstevel@tonic-gate 				       (int) getuid(), (int) geteuid());
16887c478bd9Sstevel@tonic-gate # else /* USESETEUID */
16897c478bd9Sstevel@tonic-gate 			if (setreuid(-1, 0) < 0)
16907c478bd9Sstevel@tonic-gate 				syserr("!setreuid(-1, 0) failure (real=%d, eff=%d)",
16917c478bd9Sstevel@tonic-gate 				       (int) getuid(), (int) geteuid());
16927c478bd9Sstevel@tonic-gate 			if (setreuid(RealUid, 0) < 0)
16937c478bd9Sstevel@tonic-gate 				syserr("!setreuid(%d, 0) failure (real=%d, eff=%d)",
16947c478bd9Sstevel@tonic-gate 				       (int) RealUid, (int) getuid(),
16957c478bd9Sstevel@tonic-gate 				       (int) geteuid());
16967c478bd9Sstevel@tonic-gate # endif /* USESETEUID */
16977c478bd9Sstevel@tonic-gate 		}
16987c478bd9Sstevel@tonic-gate 		if (setgid(savedgid) < 0)
16997c478bd9Sstevel@tonic-gate 			syserr("!setgid(%d) failure (real=%d eff=%d)",
17007c478bd9Sstevel@tonic-gate 			       (int) savedgid, (int) getgid(),
17017c478bd9Sstevel@tonic-gate 			       (int) getegid());
17027c478bd9Sstevel@tonic-gate 	}
17037c478bd9Sstevel@tonic-gate #endif /* HASSETREUID || USESETEUID */
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	if (tTd(27, 9))
17067c478bd9Sstevel@tonic-gate 		sm_dprintf("include: reset uid = %d/%d\n",
17077c478bd9Sstevel@tonic-gate 			   (int) getuid(), (int) geteuid());
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	if (rval == E_SM_OPENTIMEOUT)
17107c478bd9Sstevel@tonic-gate 		usrerr("451 4.4.1 open timeout on %s", fname);
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	if (fp == NULL)
17137c478bd9Sstevel@tonic-gate 		return rval;
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	if (fstat(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), &st) < 0)
17167c478bd9Sstevel@tonic-gate 	{
17177c478bd9Sstevel@tonic-gate 		rval = errno;
17187c478bd9Sstevel@tonic-gate 		syserr("Cannot fstat %s!", fname);
17197c478bd9Sstevel@tonic-gate 		(void) sm_io_close(fp, SM_TIME_DEFAULT);
17207c478bd9Sstevel@tonic-gate 		return rval;
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	/* if path was writable, check to avoid file giveaway tricks */
17247c478bd9Sstevel@tonic-gate 	safechown = chownsafe(sm_io_getinfo(fp, SM_IO_WHAT_FD, NULL), safedir);
17257c478bd9Sstevel@tonic-gate 	if (tTd(27, 6))
17267c478bd9Sstevel@tonic-gate 		sm_dprintf("include: parent of %s is %s, chown is %ssafe\n",
17277c478bd9Sstevel@tonic-gate 			   fname, safedir ? "safe" : "dangerous",
17287c478bd9Sstevel@tonic-gate 			   safechown ? "" : "un");
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	/* if no controlling user or coming from an alias delivery */
17317c478bd9Sstevel@tonic-gate 	if (safechown &&
17327c478bd9Sstevel@tonic-gate 	    (ca == NULL ||
17337c478bd9Sstevel@tonic-gate 	     (ca->q_uid == DefUid && ca->q_gid == 0)))
17347c478bd9Sstevel@tonic-gate 	{
17357c478bd9Sstevel@tonic-gate 		ctladdr->q_uid = st.st_uid;
17367c478bd9Sstevel@tonic-gate 		ctladdr->q_gid = st.st_gid;
17377c478bd9Sstevel@tonic-gate 		ctladdr->q_flags |= QGOODUID;
17387c478bd9Sstevel@tonic-gate 	}
17397c478bd9Sstevel@tonic-gate 	if (ca != NULL && ca->q_uid == st.st_uid)
17407c478bd9Sstevel@tonic-gate 	{
17417c478bd9Sstevel@tonic-gate 		/* optimization -- avoid getpwuid if we already have info */
17427c478bd9Sstevel@tonic-gate 		ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL;
17437c478bd9Sstevel@tonic-gate 		ctladdr->q_ruser = ca->q_ruser;
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 	else if (!forwarding)
17467c478bd9Sstevel@tonic-gate 	{
17477c478bd9Sstevel@tonic-gate 		register struct passwd *pw;
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 		pw = sm_getpwuid(st.st_uid);
17507c478bd9Sstevel@tonic-gate 		if (pw == NULL)
17517c478bd9Sstevel@tonic-gate 		{
17527c478bd9Sstevel@tonic-gate 			ctladdr->q_uid = st.st_uid;
17537c478bd9Sstevel@tonic-gate 			ctladdr->q_flags |= QBOGUSSHELL;
17547c478bd9Sstevel@tonic-gate 		}
17557c478bd9Sstevel@tonic-gate 		else
17567c478bd9Sstevel@tonic-gate 		{
17577c478bd9Sstevel@tonic-gate 			char *sh;
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 			ctladdr->q_ruser = sm_rpool_strdup_x(e->e_rpool,
17607c478bd9Sstevel@tonic-gate 							     pw->pw_name);
17617c478bd9Sstevel@tonic-gate 			if (safechown)
17627c478bd9Sstevel@tonic-gate 				sh = pw->pw_shell;
17637c478bd9Sstevel@tonic-gate 			else
17647c478bd9Sstevel@tonic-gate 				sh = "/SENDMAIL/ANY/SHELL/";
17657c478bd9Sstevel@tonic-gate 			if (!usershellok(pw->pw_name, sh))
17667c478bd9Sstevel@tonic-gate 			{
17677c478bd9Sstevel@tonic-gate 				if (LogLevel > 11)
17687c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
17697c478bd9Sstevel@tonic-gate 						  "%s: user %s has bad shell %s, marked %s",
17707c478bd9Sstevel@tonic-gate 						  shortenstring(fname,
17717c478bd9Sstevel@tonic-gate 								MAXSHORTSTR),
17727c478bd9Sstevel@tonic-gate 						  pw->pw_name, sh,
17737c478bd9Sstevel@tonic-gate 						  safechown ? "bogus" : "unsafe");
17747c478bd9Sstevel@tonic-gate 				if (safechown)
17757c478bd9Sstevel@tonic-gate 					ctladdr->q_flags |= QBOGUSSHELL;
17767c478bd9Sstevel@tonic-gate 				else
17777c478bd9Sstevel@tonic-gate 					ctladdr->q_flags |= QUNSAFEADDR;
17787c478bd9Sstevel@tonic-gate 			}
17797c478bd9Sstevel@tonic-gate 		}
17807c478bd9Sstevel@tonic-gate 	}
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	if (bitset(EF_VRFYONLY, e->e_flags))
17837c478bd9Sstevel@tonic-gate 	{
17847c478bd9Sstevel@tonic-gate 		/* don't do any more now */
17857c478bd9Sstevel@tonic-gate 		ctladdr->q_state = QS_VERIFIED;
17867c478bd9Sstevel@tonic-gate 		e->e_nrcpts++;
17877c478bd9Sstevel@tonic-gate 		(void) sm_io_close(fp, SM_TIME_DEFAULT);
17887c478bd9Sstevel@tonic-gate 		return rval;
17897c478bd9Sstevel@tonic-gate 	}
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	/*
17927c478bd9Sstevel@tonic-gate 	**  Check to see if some bad guy can write this file
17937c478bd9Sstevel@tonic-gate 	**
17947c478bd9Sstevel@tonic-gate 	**	Group write checking could be more clever, e.g.,
17957c478bd9Sstevel@tonic-gate 	**	guessing as to which groups are actually safe ("sys"
17967c478bd9Sstevel@tonic-gate 	**	may be; "user" probably is not).
17977c478bd9Sstevel@tonic-gate 	*/
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	mode = S_IWOTH;
18007c478bd9Sstevel@tonic-gate 	if (!bitnset((forwarding ?
18017c478bd9Sstevel@tonic-gate 		      DBS_GROUPWRITABLEFORWARDFILESAFE :
18027c478bd9Sstevel@tonic-gate 		      DBS_GROUPWRITABLEINCLUDEFILESAFE),
18037c478bd9Sstevel@tonic-gate 		     DontBlameSendmail))
18047c478bd9Sstevel@tonic-gate 		mode |= S_IWGRP;
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	if (bitset(mode, st.st_mode))
18077c478bd9Sstevel@tonic-gate 	{
18087c478bd9Sstevel@tonic-gate 		if (tTd(27, 6))
18097c478bd9Sstevel@tonic-gate 			sm_dprintf("include: %s is %s writable, marked unsafe\n",
18107c478bd9Sstevel@tonic-gate 				   shortenstring(fname, MAXSHORTSTR),
18117c478bd9Sstevel@tonic-gate 				   bitset(S_IWOTH, st.st_mode) ? "world"
18127c478bd9Sstevel@tonic-gate 							       : "group");
18137c478bd9Sstevel@tonic-gate 		if (LogLevel > 11)
18147c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_INFO, e->e_id,
18157c478bd9Sstevel@tonic-gate 				  "%s: %s writable %s file, marked unsafe",
18167c478bd9Sstevel@tonic-gate 				  shortenstring(fname, MAXSHORTSTR),
18177c478bd9Sstevel@tonic-gate 				  bitset(S_IWOTH, st.st_mode) ? "world" : "group",
18187c478bd9Sstevel@tonic-gate 				  forwarding ? "forward" : ":include:");
18197c478bd9Sstevel@tonic-gate 		ctladdr->q_flags |= QUNSAFEADDR;
18207c478bd9Sstevel@tonic-gate 	}
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	/* read the file -- each line is a comma-separated list. */
18237c478bd9Sstevel@tonic-gate 	FileName = fname;
18247c478bd9Sstevel@tonic-gate 	LineNumber = 0;
18257c478bd9Sstevel@tonic-gate 	ctladdr->q_flags &= ~QSELFREF;
18267c478bd9Sstevel@tonic-gate 	nincludes = 0;
1827058561cbSjbeck 	while (sm_io_fgets(fp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL &&
18287c478bd9Sstevel@tonic-gate 	       !maxreached)
18297c478bd9Sstevel@tonic-gate 	{
18307c478bd9Sstevel@tonic-gate 		fixcrlf(buf, true);
18317c478bd9Sstevel@tonic-gate 		LineNumber++;
18327c478bd9Sstevel@tonic-gate 		if (buf[0] == '#' || buf[0] == '\0')
18337c478bd9Sstevel@tonic-gate 			continue;
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 		/* <sp>#@# introduces a comment anywhere */
18367c478bd9Sstevel@tonic-gate 		/* for Japanese character sets */
18377c478bd9Sstevel@tonic-gate 		for (p = buf; (p = strchr(++p, '#')) != NULL; )
18387c478bd9Sstevel@tonic-gate 		{
18397c478bd9Sstevel@tonic-gate 			if (p[1] == '@' && p[2] == '#' &&
18407c478bd9Sstevel@tonic-gate 			    isascii(p[-1]) && isspace(p[-1]) &&
18417c478bd9Sstevel@tonic-gate 			    (p[3] == '\0' || (isascii(p[3]) && isspace(p[3]))))
18427c478bd9Sstevel@tonic-gate 			{
18437c478bd9Sstevel@tonic-gate 				--p;
18447c478bd9Sstevel@tonic-gate 				while (p > buf && isascii(p[-1]) &&
18457c478bd9Sstevel@tonic-gate 				       isspace(p[-1]))
18467c478bd9Sstevel@tonic-gate 					--p;
18477c478bd9Sstevel@tonic-gate 				p[0] = '\0';
18487c478bd9Sstevel@tonic-gate 				break;
18497c478bd9Sstevel@tonic-gate 			}
18507c478bd9Sstevel@tonic-gate 		}
18517c478bd9Sstevel@tonic-gate 		if (buf[0] == '\0')
18527c478bd9Sstevel@tonic-gate 			continue;
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 		e->e_to = NULL;
18557c478bd9Sstevel@tonic-gate 		message("%s to %s",
18567c478bd9Sstevel@tonic-gate 			forwarding ? "forwarding" : "sending", buf);
18577c478bd9Sstevel@tonic-gate 		if (forwarding && LogLevel > 10)
18587c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_INFO, e->e_id,
18597c478bd9Sstevel@tonic-gate 				  "forward %.200s => %s",
18607c478bd9Sstevel@tonic-gate 				  oldto, shortenstring(buf, MAXSHORTSTR));
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 		nincludes += sendtolist(buf, ctladdr, sendq, aliaslevel + 1, e);
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 		if (forwarding &&
18657c478bd9Sstevel@tonic-gate 		    MaxForwardEntries > 0 &&
18667c478bd9Sstevel@tonic-gate 		    nincludes >= MaxForwardEntries)
18677c478bd9Sstevel@tonic-gate 		{
18687c478bd9Sstevel@tonic-gate 			/* just stop reading and processing further entries */
18697c478bd9Sstevel@tonic-gate #if 0
18707c478bd9Sstevel@tonic-gate 			/* additional: (?) */
18717c478bd9Sstevel@tonic-gate 			ctladdr->q_state = QS_DONTSEND;
18727c478bd9Sstevel@tonic-gate #endif /* 0 */
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 			syserr("Attempt to forward to more than %d addresses (in %s)!",
18757c478bd9Sstevel@tonic-gate 				MaxForwardEntries, fname);
18767c478bd9Sstevel@tonic-gate 			maxreached = true;
18777c478bd9Sstevel@tonic-gate 		}
18787c478bd9Sstevel@tonic-gate 	}
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	if (sm_io_error(fp) && tTd(27, 3))
18817c478bd9Sstevel@tonic-gate 		sm_dprintf("include: read error: %s\n", sm_errstring(errno));
18827c478bd9Sstevel@tonic-gate 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
18837c478bd9Sstevel@tonic-gate 	{
18844aac33d3Sjbeck 		if (aliaslevel <= MaxAliasRecursion ||
18854aac33d3Sjbeck 		    ctladdr->q_state != QS_BADADDR)
18867c478bd9Sstevel@tonic-gate 		{
18874aac33d3Sjbeck 			ctladdr->q_state = QS_DONTSEND;
18884aac33d3Sjbeck 			if (tTd(27, 5))
18894aac33d3Sjbeck 			{
18904aac33d3Sjbeck 				sm_dprintf("include: QS_DONTSEND ");
18914aac33d3Sjbeck 				printaddr(sm_debug_file(), ctladdr, false);
18924aac33d3Sjbeck 			}
18937c478bd9Sstevel@tonic-gate 		}
18947c478bd9Sstevel@tonic-gate 	}
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	(void) sm_io_close(fp, SM_TIME_DEFAULT);
18977c478bd9Sstevel@tonic-gate 	FileName = oldfilename;
18987c478bd9Sstevel@tonic-gate 	LineNumber = oldlinenumber;
18997c478bd9Sstevel@tonic-gate 	e->e_to = oldto;
19007c478bd9Sstevel@tonic-gate 	return rval;
19017c478bd9Sstevel@tonic-gate }
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate static void
includetimeout(ignore)19047c478bd9Sstevel@tonic-gate includetimeout(ignore)
19057c478bd9Sstevel@tonic-gate 	int ignore;
19067c478bd9Sstevel@tonic-gate {
19077c478bd9Sstevel@tonic-gate 	/*
19087c478bd9Sstevel@tonic-gate 	**  NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
19097c478bd9Sstevel@tonic-gate 	**	ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
19107c478bd9Sstevel@tonic-gate 	**	DOING.
19117c478bd9Sstevel@tonic-gate 	*/
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	errno = ETIMEDOUT;
19147c478bd9Sstevel@tonic-gate 	longjmp(CtxIncludeTimeout, 1);
19157c478bd9Sstevel@tonic-gate }
1916058561cbSjbeck 
19177c478bd9Sstevel@tonic-gate /*
19187c478bd9Sstevel@tonic-gate **  SENDTOARGV -- send to an argument vector.
19197c478bd9Sstevel@tonic-gate **
19207c478bd9Sstevel@tonic-gate **	Parameters:
19217c478bd9Sstevel@tonic-gate **		argv -- argument vector to send to.
19227c478bd9Sstevel@tonic-gate **		e -- the current envelope.
19237c478bd9Sstevel@tonic-gate **
19247c478bd9Sstevel@tonic-gate **	Returns:
19257c478bd9Sstevel@tonic-gate **		none.
19267c478bd9Sstevel@tonic-gate **
19277c478bd9Sstevel@tonic-gate **	Side Effects:
19287c478bd9Sstevel@tonic-gate **		puts all addresses on the argument vector onto the
19297c478bd9Sstevel@tonic-gate **			send queue.
19307c478bd9Sstevel@tonic-gate */
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate void
sendtoargv(argv,e)19337c478bd9Sstevel@tonic-gate sendtoargv(argv, e)
19347c478bd9Sstevel@tonic-gate 	register char **argv;
19357c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
19367c478bd9Sstevel@tonic-gate {
19377c478bd9Sstevel@tonic-gate 	register char *p;
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	while ((p = *argv++) != NULL)
19407c478bd9Sstevel@tonic-gate 		(void) sendtolist(p, NULLADDR, &e->e_sendqueue, 0, e);
19417c478bd9Sstevel@tonic-gate }
1942058561cbSjbeck 
19437c478bd9Sstevel@tonic-gate /*
19447c478bd9Sstevel@tonic-gate **  GETCTLADDR -- get controlling address from an address header.
19457c478bd9Sstevel@tonic-gate **
19467c478bd9Sstevel@tonic-gate **	If none, get one corresponding to the effective userid.
19477c478bd9Sstevel@tonic-gate **
19487c478bd9Sstevel@tonic-gate **	Parameters:
19497c478bd9Sstevel@tonic-gate **		a -- the address to find the controller of.
19507c478bd9Sstevel@tonic-gate **
19517c478bd9Sstevel@tonic-gate **	Returns:
19527c478bd9Sstevel@tonic-gate **		the controlling address.
19537c478bd9Sstevel@tonic-gate */
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate ADDRESS *
getctladdr(a)19567c478bd9Sstevel@tonic-gate getctladdr(a)
19577c478bd9Sstevel@tonic-gate 	register ADDRESS *a;
19587c478bd9Sstevel@tonic-gate {
19597c478bd9Sstevel@tonic-gate 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
19607c478bd9Sstevel@tonic-gate 		a = a->q_alias;
19617c478bd9Sstevel@tonic-gate 	return a;
19627c478bd9Sstevel@tonic-gate }
1963058561cbSjbeck 
19647c478bd9Sstevel@tonic-gate /*
19657c478bd9Sstevel@tonic-gate **  SELF_REFERENCE -- check to see if an address references itself
19667c478bd9Sstevel@tonic-gate **
19677c478bd9Sstevel@tonic-gate **	The check is done through a chain of aliases.  If it is part of
19687c478bd9Sstevel@tonic-gate **	a loop, break the loop at the "best" address, that is, the one
19697c478bd9Sstevel@tonic-gate **	that exists as a real user.
19707c478bd9Sstevel@tonic-gate **
19717c478bd9Sstevel@tonic-gate **	This is to handle the case of:
19727c478bd9Sstevel@tonic-gate **		awc:		Andrew.Chang
19737c478bd9Sstevel@tonic-gate **		Andrew.Chang:	awc@mail.server
19747c478bd9Sstevel@tonic-gate **	which is a problem only on mail.server.
19757c478bd9Sstevel@tonic-gate **
19767c478bd9Sstevel@tonic-gate **	Parameters:
19777c478bd9Sstevel@tonic-gate **		a -- the address to check.
19787c478bd9Sstevel@tonic-gate **
19797c478bd9Sstevel@tonic-gate **	Returns:
19807c478bd9Sstevel@tonic-gate **		The address that should be retained.
19817c478bd9Sstevel@tonic-gate */
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate static ADDRESS *
self_reference(a)19847c478bd9Sstevel@tonic-gate self_reference(a)
19857c478bd9Sstevel@tonic-gate 	ADDRESS *a;
19867c478bd9Sstevel@tonic-gate {
19877c478bd9Sstevel@tonic-gate 	ADDRESS *b;		/* top entry in self ref loop */
19887c478bd9Sstevel@tonic-gate 	ADDRESS *c;		/* entry that point to a real mail box */
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	if (tTd(27, 1))
19917c478bd9Sstevel@tonic-gate 		sm_dprintf("self_reference(%s)\n", a->q_paddr);
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	for (b = a->q_alias; b != NULL; b = b->q_alias)
19947c478bd9Sstevel@tonic-gate 	{
19957c478bd9Sstevel@tonic-gate 		if (sameaddr(a, b))
19967c478bd9Sstevel@tonic-gate 			break;
19977c478bd9Sstevel@tonic-gate 	}
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	if (b == NULL)
20007c478bd9Sstevel@tonic-gate 	{
20017c478bd9Sstevel@tonic-gate 		if (tTd(27, 1))
20027c478bd9Sstevel@tonic-gate 			sm_dprintf("\t... no self ref\n");
20037c478bd9Sstevel@tonic-gate 		return NULL;
20047c478bd9Sstevel@tonic-gate 	}
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	/*
20077c478bd9Sstevel@tonic-gate 	**  Pick the first address that resolved to a real mail box
20087c478bd9Sstevel@tonic-gate 	**  i.e has a mbdb entry.  The returned value will be marked
20097c478bd9Sstevel@tonic-gate 	**  QSELFREF in recipient(), which in turn will disable alias()
20107c478bd9Sstevel@tonic-gate 	**  from marking it as QS_IS_DEAD(), which mean it will be used
20117c478bd9Sstevel@tonic-gate 	**  as a deliverable address.
20127c478bd9Sstevel@tonic-gate 	**
20137c478bd9Sstevel@tonic-gate 	**  The 2 key thing to note here are:
20147c478bd9Sstevel@tonic-gate 	**	1) we are in a recursive call sequence:
20157c478bd9Sstevel@tonic-gate 	**		alias->sendtolist->recipient->alias
20167c478bd9Sstevel@tonic-gate 	**	2) normally, when we return back to alias(), the address
20177c478bd9Sstevel@tonic-gate 	**	   will be marked QS_EXPANDED, since alias() assumes the
20187c478bd9Sstevel@tonic-gate 	**	   expanded form will be used instead of the current address.
20197c478bd9Sstevel@tonic-gate 	**	   This behaviour is turned off if the address is marked
20207c478bd9Sstevel@tonic-gate 	**	   QSELFREF.  We set QSELFREF when we return to recipient().
20217c478bd9Sstevel@tonic-gate 	*/
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 	c = a;
20247c478bd9Sstevel@tonic-gate 	while (c != NULL)
20257c478bd9Sstevel@tonic-gate 	{
20267c478bd9Sstevel@tonic-gate 		if (tTd(27, 10))
20277c478bd9Sstevel@tonic-gate 			sm_dprintf("  %s", c->q_user);
20287c478bd9Sstevel@tonic-gate 		if (bitnset(M_HASPWENT, c->q_mailer->m_flags))
20297c478bd9Sstevel@tonic-gate 		{
20307c478bd9Sstevel@tonic-gate 			SM_MBDB_T user;
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 			if (tTd(27, 2))
20337c478bd9Sstevel@tonic-gate 				sm_dprintf("\t... getpwnam(%s)... ", c->q_user);
20347c478bd9Sstevel@tonic-gate 			if (sm_mbdb_lookup(c->q_user, &user) == EX_OK)
20357c478bd9Sstevel@tonic-gate 			{
20367c478bd9Sstevel@tonic-gate 				if (tTd(27, 2))
20377c478bd9Sstevel@tonic-gate 					sm_dprintf("found\n");
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 				/* ought to cache results here */
20407c478bd9Sstevel@tonic-gate 				if (sameaddr(b, c))
20417c478bd9Sstevel@tonic-gate 					return b;
20427c478bd9Sstevel@tonic-gate 				else
20437c478bd9Sstevel@tonic-gate 					return c;
20447c478bd9Sstevel@tonic-gate 			}
20457c478bd9Sstevel@tonic-gate 			if (tTd(27, 2))
20467c478bd9Sstevel@tonic-gate 				sm_dprintf("failed\n");
20477c478bd9Sstevel@tonic-gate 		}
20487c478bd9Sstevel@tonic-gate 		else
20497c478bd9Sstevel@tonic-gate 		{
20507c478bd9Sstevel@tonic-gate 			/* if local delivery, compare usernames */
20517c478bd9Sstevel@tonic-gate 			if (bitnset(M_LOCALMAILER, c->q_mailer->m_flags) &&
20527c478bd9Sstevel@tonic-gate 			    b->q_mailer == c->q_mailer)
20537c478bd9Sstevel@tonic-gate 			{
20547c478bd9Sstevel@tonic-gate 				if (tTd(27, 2))
20557c478bd9Sstevel@tonic-gate 					sm_dprintf("\t... local match (%s)\n",
20567c478bd9Sstevel@tonic-gate 						c->q_user);
20577c478bd9Sstevel@tonic-gate 				if (sameaddr(b, c))
20587c478bd9Sstevel@tonic-gate 					return b;
20597c478bd9Sstevel@tonic-gate 				else
20607c478bd9Sstevel@tonic-gate 					return c;
20617c478bd9Sstevel@tonic-gate 			}
20627c478bd9Sstevel@tonic-gate 		}
20637c478bd9Sstevel@tonic-gate 		if (tTd(27, 10))
20647c478bd9Sstevel@tonic-gate 			sm_dprintf("\n");
20657c478bd9Sstevel@tonic-gate 		c = c->q_alias;
20667c478bd9Sstevel@tonic-gate 	}
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	if (tTd(27, 1))
20697c478bd9Sstevel@tonic-gate 		sm_dprintf("\t... cannot break loop for \"%s\"\n", a->q_paddr);
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	return NULL;
20727c478bd9Sstevel@tonic-gate }
2073