xref: /freebsd/contrib/sendmail/src/envelope.c (revision 2fb4f839f3fc72ce2bab12f9ba4760f97f73e97f)
1c2aa98e2SPeter Wemm /*
25dd76dd0SGregory Neil Shapiro  * Copyright (c) 1998-2003, 2006 Proofpoint, Inc. and its suppliers.
306f25ae9SGregory Neil Shapiro  *	All rights reserved.
4c2aa98e2SPeter Wemm  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
5c2aa98e2SPeter Wemm  * Copyright (c) 1988, 1993
6c2aa98e2SPeter Wemm  *	The Regents of the University of California.  All rights reserved.
7c2aa98e2SPeter Wemm  *
8c2aa98e2SPeter Wemm  * By using this file, you agree to the terms and conditions set
9c2aa98e2SPeter Wemm  * forth in the LICENSE file which can be found at the top level of
10c2aa98e2SPeter Wemm  * the sendmail distribution.
11c2aa98e2SPeter Wemm  *
12c2aa98e2SPeter Wemm  */
13c2aa98e2SPeter Wemm 
1406f25ae9SGregory Neil Shapiro #include <sendmail.h>
15*2fb4f839SGregory Neil Shapiro #include <sm/sendmail.h>
1606f25ae9SGregory Neil Shapiro 
174313cc83SGregory Neil Shapiro SM_RCSID("@(#)$Id: envelope.c,v 8.313 2013-11-22 20:51:55 ca Exp $")
1813bd1963SGregory Neil Shapiro 
1913bd1963SGregory Neil Shapiro /*
2013bd1963SGregory Neil Shapiro **  CLRSESSENVELOPE -- clear session oriented data in an envelope
2113bd1963SGregory Neil Shapiro **
2213bd1963SGregory Neil Shapiro **	Parameters:
2313bd1963SGregory Neil Shapiro **		e -- the envelope to clear.
2413bd1963SGregory Neil Shapiro **
2513bd1963SGregory Neil Shapiro **	Returns:
2613bd1963SGregory Neil Shapiro **		none.
2713bd1963SGregory Neil Shapiro */
2813bd1963SGregory Neil Shapiro 
2913bd1963SGregory Neil Shapiro void
3013bd1963SGregory Neil Shapiro clrsessenvelope(e)
3113bd1963SGregory Neil Shapiro 	ENVELOPE *e;
3213bd1963SGregory Neil Shapiro {
3313bd1963SGregory Neil Shapiro #if SASL
3413bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{auth_type}"), "");
3513bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{auth_authen}"), "");
3613bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{auth_author}"), "");
3713bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{auth_ssf}"), "");
3813bd1963SGregory Neil Shapiro #endif /* SASL */
3913bd1963SGregory Neil Shapiro #if STARTTLS
4013bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{cert_issuer}"), "");
4113bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{cert_subject}"), "");
4213bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{cipher_bits}"), "");
4313bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{cipher}"), "");
4413bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{tls_version}"), "");
4513bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{verify}"), "");
4613bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{alg_bits}"), "");
4713bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{cn_issuer}"), "");
4813bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{cn_subject}"), "");
4913bd1963SGregory Neil Shapiro #endif /* STARTTLS */
5013bd1963SGregory Neil Shapiro }
51c2aa98e2SPeter Wemm 
52c2aa98e2SPeter Wemm /*
5340266059SGregory Neil Shapiro **  NEWENVELOPE -- fill in a new envelope
54c2aa98e2SPeter Wemm **
55c2aa98e2SPeter Wemm **	Supports inheritance.
56c2aa98e2SPeter Wemm **
57c2aa98e2SPeter Wemm **	Parameters:
58c2aa98e2SPeter Wemm **		e -- the new envelope to fill in.
59c2aa98e2SPeter Wemm **		parent -- the envelope to be the parent of e.
6040266059SGregory Neil Shapiro **		rpool -- either NULL, or a pointer to a resource pool
6140266059SGregory Neil Shapiro **			from which envelope memory is allocated, and
6240266059SGregory Neil Shapiro **			to which envelope resources are attached.
63c2aa98e2SPeter Wemm **
64c2aa98e2SPeter Wemm **	Returns:
65c2aa98e2SPeter Wemm **		e.
66c2aa98e2SPeter Wemm **
67c2aa98e2SPeter Wemm **	Side Effects:
68c2aa98e2SPeter Wemm **		none.
69c2aa98e2SPeter Wemm */
70c2aa98e2SPeter Wemm 
71c2aa98e2SPeter Wemm ENVELOPE *
newenvelope(e,parent,rpool)7240266059SGregory Neil Shapiro newenvelope(e, parent, rpool)
73c2aa98e2SPeter Wemm 	register ENVELOPE *e;
74c2aa98e2SPeter Wemm 	register ENVELOPE *parent;
7540266059SGregory Neil Shapiro 	SM_RPOOL_T *rpool;
76c2aa98e2SPeter Wemm {
77e3793f76SGregory Neil Shapiro 	int sendmode;
784e4196cbSGregory Neil Shapiro 
7940266059SGregory Neil Shapiro 	/*
8040266059SGregory Neil Shapiro 	**  This code used to read:
8140266059SGregory Neil Shapiro 	**	if (e == parent && e->e_parent != NULL)
8240266059SGregory Neil Shapiro 	**		parent = e->e_parent;
8340266059SGregory Neil Shapiro 	**  So if e == parent && e->e_parent == NULL then we would
8440266059SGregory Neil Shapiro 	**  set e->e_parent = e, which creates a loop in the e_parent chain.
8540266059SGregory Neil Shapiro 	**  This meant macvalue() could go into an infinite loop.
8640266059SGregory Neil Shapiro 	*/
8740266059SGregory Neil Shapiro 
884e4196cbSGregory Neil Shapiro 	if (parent != NULL)
894e4196cbSGregory Neil Shapiro 		sendmode = parent->e_sendmode;
904e4196cbSGregory Neil Shapiro 	else
914e4196cbSGregory Neil Shapiro 		sendmode = DM_NOTSET;
924e4196cbSGregory Neil Shapiro 
9340266059SGregory Neil Shapiro 	if (e == parent)
94c2aa98e2SPeter Wemm 		parent = e->e_parent;
9540266059SGregory Neil Shapiro 	clearenvelope(e, true, rpool);
96c2aa98e2SPeter Wemm 	if (e == CurEnv)
9706f25ae9SGregory Neil Shapiro 		memmove((char *) &e->e_from,
9806f25ae9SGregory Neil Shapiro 			(char *) &NullAddress,
99d0cef73dSGregory Neil Shapiro 			sizeof(e->e_from));
100c2aa98e2SPeter Wemm 	else
10106f25ae9SGregory Neil Shapiro 		memmove((char *) &e->e_from,
10206f25ae9SGregory Neil Shapiro 			(char *) &CurEnv->e_from,
103d0cef73dSGregory Neil Shapiro 			sizeof(e->e_from));
104c2aa98e2SPeter Wemm 	e->e_parent = parent;
10506f25ae9SGregory Neil Shapiro 	assign_queueid(e);
106c2aa98e2SPeter Wemm 	e->e_ctime = curtime();
107ffb83623SGregory Neil Shapiro #if _FFR_SESSID
108ffb83623SGregory Neil Shapiro 	e->e_sessid = e->e_id;
1095b0945b5SGregory Neil Shapiro #endif
110c2aa98e2SPeter Wemm 	if (parent != NULL)
11140266059SGregory Neil Shapiro 	{
112c2aa98e2SPeter Wemm 		e->e_msgpriority = parent->e_msgsize;
113ffb83623SGregory Neil Shapiro #if _FFR_SESSID
114ffb83623SGregory Neil Shapiro 		if (parent->e_sessid != NULL)
115ffb83623SGregory Neil Shapiro 			e->e_sessid = sm_rpool_strdup_x(rpool,
116ffb83623SGregory Neil Shapiro 							parent->e_sessid);
1175b0945b5SGregory Neil Shapiro #endif
118ffb83623SGregory Neil Shapiro 
11940266059SGregory Neil Shapiro 		if (parent->e_quarmsg == NULL)
12040266059SGregory Neil Shapiro 		{
12140266059SGregory Neil Shapiro 			e->e_quarmsg = NULL;
12240266059SGregory Neil Shapiro 			macdefine(&e->e_macro, A_PERM,
12340266059SGregory Neil Shapiro 				  macid("{quarantine}"), "");
12440266059SGregory Neil Shapiro 		}
12540266059SGregory Neil Shapiro 		else
12640266059SGregory Neil Shapiro 		{
12740266059SGregory Neil Shapiro 			e->e_quarmsg = sm_rpool_strdup_x(rpool,
12840266059SGregory Neil Shapiro 							 parent->e_quarmsg);
12940266059SGregory Neil Shapiro 			macdefine(&e->e_macro, A_PERM,
13040266059SGregory Neil Shapiro 				  macid("{quarantine}"), e->e_quarmsg);
13140266059SGregory Neil Shapiro 		}
13240266059SGregory Neil Shapiro 	}
133c2aa98e2SPeter Wemm 	e->e_puthdr = putheader;
134c2aa98e2SPeter Wemm 	e->e_putbody = putbody;
135c2aa98e2SPeter Wemm 	if (CurEnv->e_xfp != NULL)
13640266059SGregory Neil Shapiro 		(void) sm_io_flush(CurEnv->e_xfp, SM_TIME_DEFAULT);
1374e4196cbSGregory Neil Shapiro 	if (sendmode != DM_NOTSET)
138e3793f76SGregory Neil Shapiro 		set_delivery_mode(sendmode, e);
139c2aa98e2SPeter Wemm 
14006f25ae9SGregory Neil Shapiro 	return e;
141c2aa98e2SPeter Wemm }
14240266059SGregory Neil Shapiro 
14340266059SGregory Neil Shapiro /* values for msg_timeout, see also IS_* below for usage (bit layout) */
14440266059SGregory Neil Shapiro #define MSG_T_O		0x01	/* normal timeout */
14540266059SGregory Neil Shapiro #define MSG_T_O_NOW	0x02	/* NOW timeout */
14640266059SGregory Neil Shapiro #define MSG_NOT_BY	0x04	/* Deliver-By time exceeded, mode R */
14740266059SGregory Neil Shapiro #define MSG_WARN	0x10	/* normal queue warning */
14840266059SGregory Neil Shapiro #define MSG_WARN_BY	0x20	/* Deliver-By time exceeded, mode N */
14940266059SGregory Neil Shapiro 
15040266059SGregory Neil Shapiro #define IS_MSG_ERR(x)	(((x) & 0x0f) != 0)	/* return an error */
15140266059SGregory Neil Shapiro 
15240266059SGregory Neil Shapiro /* immediate return */
15340266059SGregory Neil Shapiro #define IS_IMM_RET(x)	(((x) & (MSG_T_O_NOW|MSG_NOT_BY)) != 0)
15440266059SGregory Neil Shapiro #define IS_MSG_WARN(x)	(((x) & 0xf0) != 0)	/* return a warning */
15540266059SGregory Neil Shapiro 
15640266059SGregory Neil Shapiro /*
157c2aa98e2SPeter Wemm **  DROPENVELOPE -- deallocate an envelope.
158c2aa98e2SPeter Wemm **
159c2aa98e2SPeter Wemm **	Parameters:
160c2aa98e2SPeter Wemm **		e -- the envelope to deallocate.
161c2aa98e2SPeter Wemm **		fulldrop -- if set, do return receipts.
16240266059SGregory Neil Shapiro **		split -- if true, split by recipient if message is queued up
163c2aa98e2SPeter Wemm **
164c2aa98e2SPeter Wemm **	Returns:
1659bd497b8SGregory Neil Shapiro **		EX_* status (currently: 0: success, EX_IOERR on panic)
166c2aa98e2SPeter Wemm **
167c2aa98e2SPeter Wemm **	Side Effects:
168c2aa98e2SPeter Wemm **		housekeeping necessary to dispose of an envelope.
169c2aa98e2SPeter Wemm **		Unlocks this queue file.
170c2aa98e2SPeter Wemm */
171c2aa98e2SPeter Wemm 
1729bd497b8SGregory Neil Shapiro int
dropenvelope(e,fulldrop,split)17340266059SGregory Neil Shapiro dropenvelope(e, fulldrop, split)
174c2aa98e2SPeter Wemm 	register ENVELOPE *e;
175c2aa98e2SPeter Wemm 	bool fulldrop;
17640266059SGregory Neil Shapiro 	bool split;
177c2aa98e2SPeter Wemm {
17840266059SGregory Neil Shapiro 	bool panic = false;
17940266059SGregory Neil Shapiro 	bool queueit = false;
18040266059SGregory Neil Shapiro 	int msg_timeout = 0;
18140266059SGregory Neil Shapiro 	bool failure_return = false;
18240266059SGregory Neil Shapiro 	bool delay_return = false;
18340266059SGregory Neil Shapiro 	bool success_return = false;
18406f25ae9SGregory Neil Shapiro 	bool pmnotify = bitset(EF_PM_NOTIFY, e->e_flags);
18540266059SGregory Neil Shapiro 	bool done = false;
186c2aa98e2SPeter Wemm 	register ADDRESS *q;
187c2aa98e2SPeter Wemm 	char *id = e->e_id;
188193538b7SGregory Neil Shapiro 	time_t now;
189c2aa98e2SPeter Wemm 	char buf[MAXLINE];
190c2aa98e2SPeter Wemm 
191c2aa98e2SPeter Wemm 	if (tTd(50, 1))
192c2aa98e2SPeter Wemm 	{
1935b0945b5SGregory Neil Shapiro 		sm_dprintf("dropenvelope %p: id=", (void *)e);
194e92d3f3fSGregory Neil Shapiro 		xputs(sm_debug_file(), e->e_id);
19540266059SGregory Neil Shapiro 		sm_dprintf(", flags=");
196c2aa98e2SPeter Wemm 		printenvflags(e);
197c2aa98e2SPeter Wemm 		if (tTd(50, 10))
198c2aa98e2SPeter Wemm 		{
19940266059SGregory Neil Shapiro 			sm_dprintf("sendq=");
200e92d3f3fSGregory Neil Shapiro 			printaddr(sm_debug_file(), e->e_sendqueue, true);
201c2aa98e2SPeter Wemm 		}
202c2aa98e2SPeter Wemm 	}
203c2aa98e2SPeter Wemm 
204c2aa98e2SPeter Wemm 	if (LogLevel > 84)
205c2aa98e2SPeter Wemm 		sm_syslog(LOG_DEBUG, id,
20606f25ae9SGregory Neil Shapiro 			  "dropenvelope, e_flags=0x%lx, OpMode=%c, pid=%d",
20740266059SGregory Neil Shapiro 			  e->e_flags, OpMode, (int) CurrentPid);
208c2aa98e2SPeter Wemm 
209c2aa98e2SPeter Wemm 	/* we must have an id to remove disk files */
210c2aa98e2SPeter Wemm 	if (id == NULL)
2119bd497b8SGregory Neil Shapiro 		return EX_OK;
212c2aa98e2SPeter Wemm 
213c2aa98e2SPeter Wemm 	/* if verify-only mode, we can skip most of this */
214c2aa98e2SPeter Wemm 	if (OpMode == MD_VERIFY)
215c2aa98e2SPeter Wemm 		goto simpledrop;
216c2aa98e2SPeter Wemm 
2179bd497b8SGregory Neil Shapiro 	if (tTd(92, 2))
2189bd497b8SGregory Neil Shapiro 		sm_dprintf("dropenvelope: e_id=%s, EF_LOGSENDER=%d, LogLevel=%d\n",
2199bd497b8SGregory Neil Shapiro 			e->e_id, bitset(EF_LOGSENDER, e->e_flags), LogLevel);
220c2aa98e2SPeter Wemm 	if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
221c2aa98e2SPeter Wemm 		logsender(e, NULL);
222c2aa98e2SPeter Wemm 	e->e_flags &= ~EF_LOGSENDER;
223c2aa98e2SPeter Wemm 
224c2aa98e2SPeter Wemm 	/* post statistics */
225c2aa98e2SPeter Wemm 	poststats(StatFile);
226c2aa98e2SPeter Wemm 
227c2aa98e2SPeter Wemm 	/*
228c2aa98e2SPeter Wemm 	**  Extract state information from dregs of send list.
229c2aa98e2SPeter Wemm 	*/
230c2aa98e2SPeter Wemm 
231193538b7SGregory Neil Shapiro 	now = curtime();
2328774250cSGregory Neil Shapiro 	if (now >= e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
23340266059SGregory Neil Shapiro 		msg_timeout = MSG_T_O;
23440266059SGregory Neil Shapiro 	if (IS_DLVR_RETURN(e) && e->e_deliver_by > 0 &&
23540266059SGregory Neil Shapiro 	    now >= e->e_ctime + e->e_deliver_by &&
23606f25ae9SGregory Neil Shapiro 	    !bitset(EF_RESPONSE, e->e_flags))
23706f25ae9SGregory Neil Shapiro 	{
23840266059SGregory Neil Shapiro 		msg_timeout = MSG_NOT_BY;
23940266059SGregory Neil Shapiro 		e->e_flags |= EF_FATALERRS|EF_CLRQUEUE;
24040266059SGregory Neil Shapiro 	}
24140266059SGregory Neil Shapiro 	else if (TimeOuts.to_q_return[e->e_timeoutclass] == NOW &&
24240266059SGregory Neil Shapiro 		 !bitset(EF_RESPONSE, e->e_flags))
24340266059SGregory Neil Shapiro 	{
24440266059SGregory Neil Shapiro 		msg_timeout = MSG_T_O_NOW;
24506f25ae9SGregory Neil Shapiro 		e->e_flags |= EF_FATALERRS|EF_CLRQUEUE;
24606f25ae9SGregory Neil Shapiro 	}
24706f25ae9SGregory Neil Shapiro 
248da7d7b9cSGregory Neil Shapiro #if _FFR_PROXY
249da7d7b9cSGregory Neil Shapiro 	if (tTd(87, 2))
250da7d7b9cSGregory Neil Shapiro 	{
251da7d7b9cSGregory Neil Shapiro 		q = e->e_sendqueue;
252da7d7b9cSGregory Neil Shapiro 		sm_dprintf("dropenvelope: mode=%c, e=%p, sibling=%p, nrcpts=%d, sendqueue=%p, next=%p, state=%d\n",
253da7d7b9cSGregory Neil Shapiro 			e->e_sendmode, e, e->e_sibling, e->e_nrcpts, q,
254da7d7b9cSGregory Neil Shapiro 			(q == NULL) ? (void *)0 : q->q_next,
255da7d7b9cSGregory Neil Shapiro 			(q == NULL) ? -1 : q->q_state);
256da7d7b9cSGregory Neil Shapiro 	}
257da7d7b9cSGregory Neil Shapiro #endif /* _FFR_PROXY */
2586f9c8e5bSGregory Neil Shapiro 
259c2aa98e2SPeter Wemm 	e->e_flags &= ~EF_QUEUERUN;
260c2aa98e2SPeter Wemm 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
261c2aa98e2SPeter Wemm 	{
26206f25ae9SGregory Neil Shapiro 		if (QS_IS_UNDELIVERED(q->q_state))
26340266059SGregory Neil Shapiro 			queueit = true;
264c2aa98e2SPeter Wemm 
265da7d7b9cSGregory Neil Shapiro #if _FFR_PROXY
266da7d7b9cSGregory Neil Shapiro 		if (queueit && e->e_sendmode == SM_PROXY)
267da7d7b9cSGregory Neil Shapiro 			queueit = false;
2685b0945b5SGregory Neil Shapiro #endif
2696f9c8e5bSGregory Neil Shapiro 
270c2aa98e2SPeter Wemm 		/* see if a notification is needed */
271c2aa98e2SPeter Wemm 		if (bitset(QPINGONFAILURE, q->q_flags) &&
27240266059SGregory Neil Shapiro 		    ((IS_MSG_ERR(msg_timeout) &&
27340266059SGregory Neil Shapiro 		      QS_IS_UNDELIVERED(q->q_state)) ||
27406f25ae9SGregory Neil Shapiro 		     QS_IS_BADADDR(q->q_state) ||
27540266059SGregory Neil Shapiro 		     IS_IMM_RET(msg_timeout)))
276c2aa98e2SPeter Wemm 		{
27740266059SGregory Neil Shapiro 			failure_return = true;
27806f25ae9SGregory Neil Shapiro 			if (!done && q->q_owner == NULL &&
27906f25ae9SGregory Neil Shapiro 			    !emptyaddr(&e->e_from))
28006f25ae9SGregory Neil Shapiro 			{
281c2aa98e2SPeter Wemm 				(void) sendtolist(e->e_from.q_paddr, NULLADDR,
282c2aa98e2SPeter Wemm 						  &e->e_errorqueue, 0, e);
28340266059SGregory Neil Shapiro 				done = true;
28406f25ae9SGregory Neil Shapiro 			}
285c2aa98e2SPeter Wemm 		}
28640266059SGregory Neil Shapiro 		else if ((bitset(QPINGONSUCCESS, q->q_flags) &&
28706f25ae9SGregory Neil Shapiro 			  ((QS_IS_SENT(q->q_state) &&
288c2aa98e2SPeter Wemm 			    bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) ||
28940266059SGregory Neil Shapiro 			   bitset(QRELAYED|QEXPANDED|QDELIVERED, q->q_flags))) ||
29040266059SGregory Neil Shapiro 			  bitset(QBYTRACE, q->q_flags) ||
29140266059SGregory Neil Shapiro 			  bitset(QBYNRELAY, q->q_flags))
292c2aa98e2SPeter Wemm 		{
29340266059SGregory Neil Shapiro 			success_return = true;
294c2aa98e2SPeter Wemm 		}
295c2aa98e2SPeter Wemm 	}
296c2aa98e2SPeter Wemm 
297c2aa98e2SPeter Wemm 	if (e->e_class < 0)
298c2aa98e2SPeter Wemm 		e->e_flags |= EF_NO_BODY_RETN;
299c2aa98e2SPeter Wemm 
300c2aa98e2SPeter Wemm 	/*
301c2aa98e2SPeter Wemm 	**  See if the message timed out.
302c2aa98e2SPeter Wemm 	*/
303c2aa98e2SPeter Wemm 
304c2aa98e2SPeter Wemm 	if (!queueit)
30506f25ae9SGregory Neil Shapiro 		/* EMPTY */
306c2aa98e2SPeter Wemm 		/* nothing to do */ ;
30740266059SGregory Neil Shapiro 	else if (IS_MSG_ERR(msg_timeout))
308c2aa98e2SPeter Wemm 	{
309c2aa98e2SPeter Wemm 		if (failure_return)
310c2aa98e2SPeter Wemm 		{
31140266059SGregory Neil Shapiro 			if (msg_timeout == MSG_NOT_BY)
31240266059SGregory Neil Shapiro 			{
313d0cef73dSGregory Neil Shapiro 				(void) sm_snprintf(buf, sizeof(buf),
31440266059SGregory Neil Shapiro 					"delivery time expired %lds",
31540266059SGregory Neil Shapiro 					e->e_deliver_by);
31640266059SGregory Neil Shapiro 			}
31740266059SGregory Neil Shapiro 			else
31840266059SGregory Neil Shapiro 			{
319d0cef73dSGregory Neil Shapiro 				(void) sm_snprintf(buf, sizeof(buf),
32006f25ae9SGregory Neil Shapiro 					"Cannot send message for %s",
32140266059SGregory Neil Shapiro 					pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
32240266059SGregory Neil Shapiro 						false));
32340266059SGregory Neil Shapiro 			}
32440266059SGregory Neil Shapiro 
32540266059SGregory Neil Shapiro 			/* don't free, allocated from e_rpool */
32640266059SGregory Neil Shapiro 			e->e_message = sm_rpool_strdup_x(e->e_rpool, buf);
3275b0945b5SGregory Neil Shapiro 			message("%s", buf);
328c2aa98e2SPeter Wemm 			e->e_flags |= EF_CLRQUEUE;
329c2aa98e2SPeter Wemm 		}
33040266059SGregory Neil Shapiro 		if (msg_timeout == MSG_NOT_BY)
33140266059SGregory Neil Shapiro 		{
33240266059SGregory Neil Shapiro 			(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
33340266059SGregory Neil Shapiro 				"Delivery time (%lds) expired\n",
33440266059SGregory Neil Shapiro 				e->e_deliver_by);
33540266059SGregory Neil Shapiro 		}
33640266059SGregory Neil Shapiro 		else
33740266059SGregory Neil Shapiro 			(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
33840266059SGregory Neil Shapiro 				"Message could not be delivered for %s\n",
33940266059SGregory Neil Shapiro 				pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
34040266059SGregory Neil Shapiro 					false));
34140266059SGregory Neil Shapiro 		(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
34240266059SGregory Neil Shapiro 			"Message will be deleted from queue\n");
343c2aa98e2SPeter Wemm 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
344c2aa98e2SPeter Wemm 		{
34506f25ae9SGregory Neil Shapiro 			if (QS_IS_UNDELIVERED(q->q_state))
346c2aa98e2SPeter Wemm 			{
34706f25ae9SGregory Neil Shapiro 				q->q_state = QS_BADADDR;
34840266059SGregory Neil Shapiro 				if (msg_timeout == MSG_NOT_BY)
34940266059SGregory Neil Shapiro 					q->q_status = "5.4.7";
35040266059SGregory Neil Shapiro 				else
351c2aa98e2SPeter Wemm 					q->q_status = "4.4.7";
352c2aa98e2SPeter Wemm 			}
353c2aa98e2SPeter Wemm 		}
354c2aa98e2SPeter Wemm 	}
35540266059SGregory Neil Shapiro 	else
35640266059SGregory Neil Shapiro 	{
35740266059SGregory Neil Shapiro 		if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 &&
35840266059SGregory Neil Shapiro 		    now >= e->e_ctime +
35940266059SGregory Neil Shapiro 				TimeOuts.to_q_warning[e->e_timeoutclass])
36040266059SGregory Neil Shapiro 			msg_timeout = MSG_WARN;
36140266059SGregory Neil Shapiro 		else if (IS_DLVR_NOTIFY(e) &&
36240266059SGregory Neil Shapiro 			 e->e_deliver_by > 0 &&
36340266059SGregory Neil Shapiro 			 now >= e->e_ctime + e->e_deliver_by)
36440266059SGregory Neil Shapiro 			msg_timeout = MSG_WARN_BY;
36540266059SGregory Neil Shapiro 
36640266059SGregory Neil Shapiro 		if (IS_MSG_WARN(msg_timeout))
367c2aa98e2SPeter Wemm 		{
368c2aa98e2SPeter Wemm 			if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
369c2aa98e2SPeter Wemm 			    e->e_class >= 0 &&
370c2aa98e2SPeter Wemm 			    e->e_from.q_paddr != NULL &&
371c2aa98e2SPeter Wemm 			    strcmp(e->e_from.q_paddr, "<>") != 0 &&
37240266059SGregory Neil Shapiro 			    sm_strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
37340266059SGregory Neil Shapiro 			    (strlen(e->e_from.q_paddr) <= 8 ||
374*2fb4f839SGregory Neil Shapiro 			     !SM_STRCASEEQ(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8],
375*2fb4f839SGregory Neil Shapiro 					   "-request")))
376c2aa98e2SPeter Wemm 			{
37740266059SGregory Neil Shapiro 				for (q = e->e_sendqueue; q != NULL;
37840266059SGregory Neil Shapiro 				     q = q->q_next)
379c2aa98e2SPeter Wemm 				{
38040266059SGregory Neil Shapiro 					if (QS_IS_UNDELIVERED(q->q_state)
38106f25ae9SGregory Neil Shapiro #if _FFR_NODELAYDSN_ON_HOLD
38240266059SGregory Neil Shapiro 					    && !bitnset(M_HOLD,
38340266059SGregory Neil Shapiro 							q->q_mailer->m_flags)
3845b0945b5SGregory Neil Shapiro #endif
38540266059SGregory Neil Shapiro 					   )
38640266059SGregory Neil Shapiro 					{
38740266059SGregory Neil Shapiro 						if (msg_timeout ==
38840266059SGregory Neil Shapiro 						    MSG_WARN_BY &&
38940266059SGregory Neil Shapiro 						    (bitset(QPINGONDELAY,
39040266059SGregory Neil Shapiro 							    q->q_flags) ||
39140266059SGregory Neil Shapiro 						    !bitset(QHASNOTIFY,
39240266059SGregory Neil Shapiro 							    q->q_flags))
39340266059SGregory Neil Shapiro 						   )
39440266059SGregory Neil Shapiro 						{
39540266059SGregory Neil Shapiro 							q->q_flags |= QBYNDELAY;
39640266059SGregory Neil Shapiro 							delay_return = true;
39740266059SGregory Neil Shapiro 						}
39840266059SGregory Neil Shapiro 						if (bitset(QPINGONDELAY,
39940266059SGregory Neil Shapiro 							   q->q_flags))
400c2aa98e2SPeter Wemm 						{
401c2aa98e2SPeter Wemm 							q->q_flags |= QDELAYED;
40240266059SGregory Neil Shapiro 							delay_return = true;
40340266059SGregory Neil Shapiro 						}
404c2aa98e2SPeter Wemm 					}
405c2aa98e2SPeter Wemm 				}
406c2aa98e2SPeter Wemm 			}
407c2aa98e2SPeter Wemm 			if (delay_return)
408c2aa98e2SPeter Wemm 			{
40940266059SGregory Neil Shapiro 				if (msg_timeout == MSG_WARN_BY)
41040266059SGregory Neil Shapiro 				{
411d0cef73dSGregory Neil Shapiro 					(void) sm_snprintf(buf, sizeof(buf),
41240266059SGregory Neil Shapiro 						"Warning: Delivery time (%lds) exceeded",
41340266059SGregory Neil Shapiro 						e->e_deliver_by);
41440266059SGregory Neil Shapiro 				}
41540266059SGregory Neil Shapiro 				else
416d0cef73dSGregory Neil Shapiro 					(void) sm_snprintf(buf, sizeof(buf),
417c2aa98e2SPeter Wemm 						"Warning: could not send message for past %s",
41840266059SGregory Neil Shapiro 						pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
41940266059SGregory Neil Shapiro 							false));
42040266059SGregory Neil Shapiro 
42140266059SGregory Neil Shapiro 				/* don't free, allocated from e_rpool */
42240266059SGregory Neil Shapiro 				e->e_message = sm_rpool_strdup_x(e->e_rpool,
42340266059SGregory Neil Shapiro 								 buf);
4245b0945b5SGregory Neil Shapiro 				message("%s", buf);
425c2aa98e2SPeter Wemm 				e->e_flags |= EF_WARNING;
426c2aa98e2SPeter Wemm 			}
42740266059SGregory Neil Shapiro 			if (msg_timeout == MSG_WARN_BY)
42840266059SGregory Neil Shapiro 			{
42940266059SGregory Neil Shapiro 				(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
43040266059SGregory Neil Shapiro 					"Warning: Delivery time (%lds) exceeded\n",
43140266059SGregory Neil Shapiro 					e->e_deliver_by);
43240266059SGregory Neil Shapiro 			}
43340266059SGregory Neil Shapiro 			else
43440266059SGregory Neil Shapiro 				(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
435c2aa98e2SPeter Wemm 					"Warning: message still undelivered after %s\n",
43640266059SGregory Neil Shapiro 					pintvl(TimeOuts.to_q_warning[e->e_timeoutclass],
43740266059SGregory Neil Shapiro 					     false));
43840266059SGregory Neil Shapiro 			(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
43940266059SGregory Neil Shapiro 				      "Will keep trying until message is %s old\n",
44040266059SGregory Neil Shapiro 				      pintvl(TimeOuts.to_q_return[e->e_timeoutclass],
44140266059SGregory Neil Shapiro 					     false));
44240266059SGregory Neil Shapiro 		}
443c2aa98e2SPeter Wemm 	}
444c2aa98e2SPeter Wemm 
445c2aa98e2SPeter Wemm 	if (tTd(50, 2))
44640266059SGregory Neil Shapiro 		sm_dprintf("failure_return=%d delay_return=%d success_return=%d queueit=%d\n",
447c2aa98e2SPeter Wemm 			failure_return, delay_return, success_return, queueit);
448c2aa98e2SPeter Wemm 
449c2aa98e2SPeter Wemm 	/*
4505b0945b5SGregory Neil Shapiro 	**  If we had some fatal error, but no addresses are marked as bad,
4515b0945b5SGregory Neil Shapiro 	**  mark all OK/VERIFIED addresses as bad (if QPINGONFAILURE).
452c2aa98e2SPeter Wemm 	*/
453c2aa98e2SPeter Wemm 
454c2aa98e2SPeter Wemm 	if (bitset(EF_FATALERRS, e->e_flags) && !failure_return)
455c2aa98e2SPeter Wemm 	{
456c2aa98e2SPeter Wemm 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
457c2aa98e2SPeter Wemm 		{
45806f25ae9SGregory Neil Shapiro 			if ((QS_IS_OK(q->q_state) ||
4595b0945b5SGregory Neil Shapiro 			     QS_IS_VERIFIED(q->q_state))
4605b0945b5SGregory Neil Shapiro 			    && bitset(QPINGONFAILURE, q->q_flags)
4615b0945b5SGregory Neil Shapiro 
4625b0945b5SGregory Neil Shapiro 			/*
4635b0945b5SGregory Neil Shapiro 			**  do not mark an address as bad if
4645b0945b5SGregory Neil Shapiro 			**  - the address itself is stored in the queue
4655b0945b5SGregory Neil Shapiro 			**  - the DeliveryMode requires queueing
4665b0945b5SGregory Neil Shapiro 			**  - the envelope is queued
4675b0945b5SGregory Neil Shapiro 			*/
4685b0945b5SGregory Neil Shapiro 
4695b0945b5SGregory Neil Shapiro 			    && !(bitset(QQUEUED, q->q_flags)
4705b0945b5SGregory Neil Shapiro 				 && WILL_BE_QUEUED(e->e_sendmode)
4715b0945b5SGregory Neil Shapiro 				 && bitset(EF_INQUEUE, e->e_flags)
4725b0945b5SGregory Neil Shapiro 				)
4735b0945b5SGregory Neil Shapiro 			   )
474c2aa98e2SPeter Wemm 			{
47540266059SGregory Neil Shapiro 				failure_return = true;
47606f25ae9SGregory Neil Shapiro 				q->q_state = QS_BADADDR;
477c2aa98e2SPeter Wemm 			}
478c2aa98e2SPeter Wemm 		}
479c2aa98e2SPeter Wemm 	}
480c2aa98e2SPeter Wemm 
481c2aa98e2SPeter Wemm 	/*
482c2aa98e2SPeter Wemm 	**  Send back return receipts as requested.
483c2aa98e2SPeter Wemm 	*/
484c2aa98e2SPeter Wemm 
485c2aa98e2SPeter Wemm 	if (success_return && !failure_return && !delay_return && fulldrop &&
486c2aa98e2SPeter Wemm 	    !bitset(PRIV_NORECEIPTS, PrivacyFlags) &&
487c2aa98e2SPeter Wemm 	    strcmp(e->e_from.q_paddr, "<>") != 0)
488c2aa98e2SPeter Wemm 	{
489c2aa98e2SPeter Wemm 		auto ADDRESS *rlist = NULL;
490c2aa98e2SPeter Wemm 
491c2aa98e2SPeter Wemm 		if (tTd(50, 8))
49240266059SGregory Neil Shapiro 			sm_dprintf("dropenvelope(%s): sending return receipt\n",
49306f25ae9SGregory Neil Shapiro 				id);
494c2aa98e2SPeter Wemm 		e->e_flags |= EF_SENDRECEIPT;
495c2aa98e2SPeter Wemm 		(void) sendtolist(e->e_from.q_paddr, NULLADDR, &rlist, 0, e);
496c2aa98e2SPeter Wemm 		(void) returntosender("Return receipt", rlist, RTSF_NO_BODY, e);
497c2aa98e2SPeter Wemm 	}
498c2aa98e2SPeter Wemm 	e->e_flags &= ~EF_SENDRECEIPT;
499c2aa98e2SPeter Wemm 
500c2aa98e2SPeter Wemm 	/*
501c2aa98e2SPeter Wemm 	**  Arrange to send error messages if there are fatal errors.
502c2aa98e2SPeter Wemm 	*/
503c2aa98e2SPeter Wemm 
504c2aa98e2SPeter Wemm 	if ((failure_return || delay_return) && e->e_errormode != EM_QUIET)
505c2aa98e2SPeter Wemm 	{
506c2aa98e2SPeter Wemm 		if (tTd(50, 8))
50740266059SGregory Neil Shapiro 			sm_dprintf("dropenvelope(%s): saving mail\n", id);
50840266059SGregory Neil Shapiro 		panic = savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags));
509c2aa98e2SPeter Wemm 	}
510c2aa98e2SPeter Wemm 
511c2aa98e2SPeter Wemm 	/*
512c2aa98e2SPeter Wemm 	**  Arrange to send warning messages to postmaster as requested.
513c2aa98e2SPeter Wemm 	*/
514c2aa98e2SPeter Wemm 
51506f25ae9SGregory Neil Shapiro 	if ((failure_return || pmnotify) &&
516c2aa98e2SPeter Wemm 	    PostMasterCopy != NULL &&
51706f25ae9SGregory Neil Shapiro 	    !bitset(EF_RESPONSE, e->e_flags) &&
51806f25ae9SGregory Neil Shapiro 	    e->e_class >= 0)
519c2aa98e2SPeter Wemm 	{
520c2aa98e2SPeter Wemm 		auto ADDRESS *rlist = NULL;
521*2fb4f839SGregory Neil Shapiro 		char pcopy[MAXNAME_I];
52206f25ae9SGregory Neil Shapiro 
52306f25ae9SGregory Neil Shapiro 		if (failure_return)
52406f25ae9SGregory Neil Shapiro 		{
525d0cef73dSGregory Neil Shapiro 			expand(PostMasterCopy, pcopy, sizeof(pcopy), e);
526c2aa98e2SPeter Wemm 
527c2aa98e2SPeter Wemm 			if (tTd(50, 8))
52840266059SGregory Neil Shapiro 				sm_dprintf("dropenvelope(%s): sending postmaster copy to %s\n",
52906f25ae9SGregory Neil Shapiro 					id, pcopy);
53006f25ae9SGregory Neil Shapiro 			(void) sendtolist(pcopy, NULLADDR, &rlist, 0, e);
53106f25ae9SGregory Neil Shapiro 		}
53206f25ae9SGregory Neil Shapiro 		if (pmnotify)
53306f25ae9SGregory Neil Shapiro 			(void) sendtolist("postmaster", NULLADDR,
53406f25ae9SGregory Neil Shapiro 					  &rlist, 0, e);
53506f25ae9SGregory Neil Shapiro 		(void) returntosender(e->e_message, rlist,
53606f25ae9SGregory Neil Shapiro 				      RTSF_PM_BOUNCE|RTSF_NO_BODY, e);
537c2aa98e2SPeter Wemm 	}
538c2aa98e2SPeter Wemm 
539c2aa98e2SPeter Wemm 	/*
540c2aa98e2SPeter Wemm 	**  Instantiate or deinstantiate the queue.
541c2aa98e2SPeter Wemm 	*/
542c2aa98e2SPeter Wemm 
543c2aa98e2SPeter Wemm simpledrop:
544c2aa98e2SPeter Wemm 	if (tTd(50, 8))
54540266059SGregory Neil Shapiro 		sm_dprintf("dropenvelope(%s): at simpledrop, queueit=%d\n",
546c2aa98e2SPeter Wemm 			id, queueit);
547c2aa98e2SPeter Wemm 	if (!queueit || bitset(EF_CLRQUEUE, e->e_flags))
548c2aa98e2SPeter Wemm 	{
549c2aa98e2SPeter Wemm 		if (tTd(50, 1))
550c2aa98e2SPeter Wemm 		{
55140266059SGregory Neil Shapiro 			sm_dprintf("\n===== Dropping queue files for %s... queueit=%d, e_flags=",
552c2aa98e2SPeter Wemm 				e->e_id, queueit);
553c2aa98e2SPeter Wemm 			printenvflags(e);
554c2aa98e2SPeter Wemm 		}
55540266059SGregory Neil Shapiro 		if (!panic)
556af9557fdSGregory Neil Shapiro 		{
557*2fb4f839SGregory Neil Shapiro 			SM_CLOSE_FP(e->e_dfp);
55840266059SGregory Neil Shapiro 			(void) xunlink(queuename(e, DATAFL_LETTER));
559af9557fdSGregory Neil Shapiro 		}
56040266059SGregory Neil Shapiro 		if (panic && QueueMode == QM_LOST)
56140266059SGregory Neil Shapiro 		{
56240266059SGregory Neil Shapiro 			/*
56340266059SGregory Neil Shapiro 			**  leave the Qf file behind as
56440266059SGregory Neil Shapiro 			**  the delivery attempt failed.
56540266059SGregory Neil Shapiro 			*/
56640266059SGregory Neil Shapiro 
56740266059SGregory Neil Shapiro 			/* EMPTY */
56840266059SGregory Neil Shapiro 		}
56940266059SGregory Neil Shapiro 		else
57040266059SGregory Neil Shapiro 		if (xunlink(queuename(e, ANYQFL_LETTER)) == 0)
57140266059SGregory Neil Shapiro 		{
57240266059SGregory Neil Shapiro 			/* add to available space in filesystem */
57313d88268SGregory Neil Shapiro 			updfs(e, -1, panic ? 0 : -1, "dropenvelope");
57440266059SGregory Neil Shapiro 		}
575c2aa98e2SPeter Wemm 
57606f25ae9SGregory Neil Shapiro 		if (e->e_ntries > 0 && LogLevel > 9)
57706f25ae9SGregory Neil Shapiro 			sm_syslog(LOG_INFO, id, "done; delay=%s, ntries=%d",
57840266059SGregory Neil Shapiro 				  pintvl(curtime() - e->e_ctime, true),
57906f25ae9SGregory Neil Shapiro 				  e->e_ntries);
580c2aa98e2SPeter Wemm 	}
581c2aa98e2SPeter Wemm 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
582c2aa98e2SPeter Wemm 	{
58340266059SGregory Neil Shapiro 		if (!split)
584*2fb4f839SGregory Neil Shapiro 			queueup(e, QUP_FL_MSYNC);
58540266059SGregory Neil Shapiro 		else
58640266059SGregory Neil Shapiro 		{
58740266059SGregory Neil Shapiro 			ENVELOPE *oldsib;
58840266059SGregory Neil Shapiro 			ENVELOPE *ee;
58940266059SGregory Neil Shapiro 
59040266059SGregory Neil Shapiro 			/*
59140266059SGregory Neil Shapiro 			**  Save old sibling and set it to NULL to avoid
59240266059SGregory Neil Shapiro 			**  queueing up the same envelopes again.
59340266059SGregory Neil Shapiro 			**  This requires that envelopes in that list have
59440266059SGregory Neil Shapiro 			**  been take care of before (or at some other place).
59540266059SGregory Neil Shapiro 			*/
59640266059SGregory Neil Shapiro 
59740266059SGregory Neil Shapiro 			oldsib = e->e_sibling;
59840266059SGregory Neil Shapiro 			e->e_sibling = NULL;
59940266059SGregory Neil Shapiro 			if (!split_by_recipient(e) &&
60040266059SGregory Neil Shapiro 			    bitset(EF_FATALERRS, e->e_flags))
60140266059SGregory Neil Shapiro 			{
602da7d7b9cSGregory Neil Shapiro 				syserr("!dropenvelope(%s): cannot commit data file %s, uid=%ld",
60340266059SGregory Neil Shapiro 					e->e_id, queuename(e, DATAFL_LETTER),
604da7d7b9cSGregory Neil Shapiro 					(long) geteuid());
60540266059SGregory Neil Shapiro 			}
60640266059SGregory Neil Shapiro 			for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
607*2fb4f839SGregory Neil Shapiro 				queueup(ee, QUP_FL_MSYNC);
608*2fb4f839SGregory Neil Shapiro 			queueup(e, QUP_FL_MSYNC);
60940266059SGregory Neil Shapiro 
61040266059SGregory Neil Shapiro 			/* clean up */
61140266059SGregory Neil Shapiro 			for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
61240266059SGregory Neil Shapiro 			{
61340266059SGregory Neil Shapiro 				/* now unlock the job */
61440266059SGregory Neil Shapiro 				if (tTd(50, 8))
61540266059SGregory Neil Shapiro 					sm_dprintf("dropenvelope(%s): unlocking job\n",
61640266059SGregory Neil Shapiro 						   ee->e_id);
61740266059SGregory Neil Shapiro 				closexscript(ee);
61840266059SGregory Neil Shapiro 				unlockqueue(ee);
61940266059SGregory Neil Shapiro 
62040266059SGregory Neil Shapiro 				/* this envelope is marked unused */
621*2fb4f839SGregory Neil Shapiro 				SM_CLOSE_FP(ee->e_dfp);
62240266059SGregory Neil Shapiro 				ee->e_id = NULL;
62340266059SGregory Neil Shapiro 				ee->e_flags &= ~EF_HAS_DF;
62440266059SGregory Neil Shapiro 			}
62540266059SGregory Neil Shapiro 			e->e_sibling = oldsib;
62640266059SGregory Neil Shapiro 		}
627c2aa98e2SPeter Wemm 	}
628c2aa98e2SPeter Wemm 
629c2aa98e2SPeter Wemm 	/* now unlock the job */
630c2aa98e2SPeter Wemm 	if (tTd(50, 8))
63140266059SGregory Neil Shapiro 		sm_dprintf("dropenvelope(%s): unlocking job\n", id);
632c2aa98e2SPeter Wemm 	closexscript(e);
633c2aa98e2SPeter Wemm 	unlockqueue(e);
634c2aa98e2SPeter Wemm 
635c2aa98e2SPeter Wemm 	/* make sure that this envelope is marked unused */
636*2fb4f839SGregory Neil Shapiro 	SM_CLOSE_FP(e->e_dfp);
637c2aa98e2SPeter Wemm 	e->e_id = NULL;
638c2aa98e2SPeter Wemm 	e->e_flags &= ~EF_HAS_DF;
6399bd497b8SGregory Neil Shapiro 	if (panic)
6409bd497b8SGregory Neil Shapiro 		return EX_IOERR;
6419bd497b8SGregory Neil Shapiro 	return EX_OK;
642c2aa98e2SPeter Wemm }
6439bd497b8SGregory Neil Shapiro 
64440266059SGregory Neil Shapiro /*
645c2aa98e2SPeter Wemm **  CLEARENVELOPE -- clear an envelope without unlocking
646c2aa98e2SPeter Wemm **
647c2aa98e2SPeter Wemm **	This is normally used by a child process to get a clean
648c2aa98e2SPeter Wemm **	envelope without disturbing the parent.
649c2aa98e2SPeter Wemm **
650c2aa98e2SPeter Wemm **	Parameters:
651c2aa98e2SPeter Wemm **		e -- the envelope to clear.
652c2aa98e2SPeter Wemm **		fullclear - if set, the current envelope is total
653c2aa98e2SPeter Wemm **			garbage and should be ignored; otherwise,
654c2aa98e2SPeter Wemm **			release any resources it may indicate.
65540266059SGregory Neil Shapiro **		rpool -- either NULL, or a pointer to a resource pool
65640266059SGregory Neil Shapiro **			from which envelope memory is allocated, and
65740266059SGregory Neil Shapiro **			to which envelope resources are attached.
658c2aa98e2SPeter Wemm **
659c2aa98e2SPeter Wemm **	Returns:
660c2aa98e2SPeter Wemm **		none.
661c2aa98e2SPeter Wemm **
662c2aa98e2SPeter Wemm **	Side Effects:
663c2aa98e2SPeter Wemm **		Closes files associated with the envelope.
664c2aa98e2SPeter Wemm **		Marks the envelope as unallocated.
665c2aa98e2SPeter Wemm */
666c2aa98e2SPeter Wemm 
667c2aa98e2SPeter Wemm void
clearenvelope(e,fullclear,rpool)66840266059SGregory Neil Shapiro clearenvelope(e, fullclear, rpool)
669c2aa98e2SPeter Wemm 	register ENVELOPE *e;
670c2aa98e2SPeter Wemm 	bool fullclear;
67140266059SGregory Neil Shapiro 	SM_RPOOL_T *rpool;
672c2aa98e2SPeter Wemm {
673c2aa98e2SPeter Wemm 	register HDR *bh;
674c2aa98e2SPeter Wemm 	register HDR **nhp;
675c2aa98e2SPeter Wemm 	extern ENVELOPE BlankEnvelope;
67640266059SGregory Neil Shapiro 	char **p;
677c2aa98e2SPeter Wemm 
678c2aa98e2SPeter Wemm 	if (!fullclear)
679c2aa98e2SPeter Wemm 	{
680c2aa98e2SPeter Wemm 		/* clear out any file information */
681*2fb4f839SGregory Neil Shapiro 		SM_CLOSE_FP(e->e_xfp);
682*2fb4f839SGregory Neil Shapiro 		SM_CLOSE_FP(e->e_dfp);
683c2aa98e2SPeter Wemm 	}
684c2aa98e2SPeter Wemm 
68540266059SGregory Neil Shapiro 	/*
68640266059SGregory Neil Shapiro 	**  Copy BlankEnvelope into *e.
68740266059SGregory Neil Shapiro 	**  It is not safe to simply copy pointers to strings;
68840266059SGregory Neil Shapiro 	**  the strings themselves must be copied (or set to NULL).
68940266059SGregory Neil Shapiro 	**  The problem is that when we assign a new string value to
69040266059SGregory Neil Shapiro 	**  a member of BlankEnvelope, we free the old string.
69140266059SGregory Neil Shapiro 	**  We did not need to do this copying in sendmail 8.11 :-(
69240266059SGregory Neil Shapiro 	**  and it is a potential performance hit.  Reference counted
69340266059SGregory Neil Shapiro 	**  strings are one way out.
69440266059SGregory Neil Shapiro 	*/
69540266059SGregory Neil Shapiro 
69640266059SGregory Neil Shapiro 	*e = BlankEnvelope;
697c2aa98e2SPeter Wemm 	e->e_message = NULL;
69840266059SGregory Neil Shapiro 	e->e_qfletter = '\0';
69940266059SGregory Neil Shapiro 	e->e_quarmsg = NULL;
70040266059SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), "");
70140266059SGregory Neil Shapiro 
70240266059SGregory Neil Shapiro 	/*
70340266059SGregory Neil Shapiro 	**  Copy the macro table.
70440266059SGregory Neil Shapiro 	**  We might be able to avoid this by zeroing the macro table
70540266059SGregory Neil Shapiro 	**  and always searching BlankEnvelope.e_macro after e->e_macro
70640266059SGregory Neil Shapiro 	**  in macvalue().
70740266059SGregory Neil Shapiro 	*/
70840266059SGregory Neil Shapiro 
70940266059SGregory Neil Shapiro 	for (p = &e->e_macro.mac_table[0];
71040266059SGregory Neil Shapiro 	     p <= &e->e_macro.mac_table[MAXMACROID];
71140266059SGregory Neil Shapiro 	     ++p)
71240266059SGregory Neil Shapiro 	{
71340266059SGregory Neil Shapiro 		if (*p != NULL)
71440266059SGregory Neil Shapiro 			*p = sm_rpool_strdup_x(rpool, *p);
71540266059SGregory Neil Shapiro 	}
71640266059SGregory Neil Shapiro 
71740266059SGregory Neil Shapiro 	/*
71840266059SGregory Neil Shapiro 	**  XXX There are many strings in the envelope structure
71940266059SGregory Neil Shapiro 	**  XXX that we are not attempting to copy here.
72040266059SGregory Neil Shapiro 	**  XXX Investigate this further.
72140266059SGregory Neil Shapiro 	*/
72240266059SGregory Neil Shapiro 
72340266059SGregory Neil Shapiro 	e->e_rpool = rpool;
72440266059SGregory Neil Shapiro 	e->e_macro.mac_rpool = rpool;
725c2aa98e2SPeter Wemm 	if (Verbose)
72606f25ae9SGregory Neil Shapiro 		set_delivery_mode(SM_DELIVER, e);
727c2aa98e2SPeter Wemm 	bh = BlankEnvelope.e_header;
728c2aa98e2SPeter Wemm 	nhp = &e->e_header;
729c2aa98e2SPeter Wemm 	while (bh != NULL)
730c2aa98e2SPeter Wemm 	{
731d0cef73dSGregory Neil Shapiro 		*nhp = (HDR *) sm_rpool_malloc_x(rpool, sizeof(*bh));
732d0cef73dSGregory Neil Shapiro 		memmove((char *) *nhp, (char *) bh, sizeof(*bh));
733c2aa98e2SPeter Wemm 		bh = bh->h_link;
734c2aa98e2SPeter Wemm 		nhp = &(*nhp)->h_link;
735c2aa98e2SPeter Wemm 	}
7369bd497b8SGregory Neil Shapiro #if _FFR_MILTER_ENHSC
7379bd497b8SGregory Neil Shapiro 	e->e_enhsc[0] = '\0';
7385b0945b5SGregory Neil Shapiro #endif
739c2aa98e2SPeter Wemm }
74040266059SGregory Neil Shapiro /*
741c2aa98e2SPeter Wemm **  INITSYS -- initialize instantiation of system
742c2aa98e2SPeter Wemm **
743c2aa98e2SPeter Wemm **	In Daemon mode, this is done in the child.
744c2aa98e2SPeter Wemm **
745c2aa98e2SPeter Wemm **	Parameters:
74606f25ae9SGregory Neil Shapiro **		e -- the envelope to use.
747c2aa98e2SPeter Wemm **
748c2aa98e2SPeter Wemm **	Returns:
749c2aa98e2SPeter Wemm **		none.
750c2aa98e2SPeter Wemm **
751c2aa98e2SPeter Wemm **	Side Effects:
752c2aa98e2SPeter Wemm **		Initializes the system macros, some global variables,
753c2aa98e2SPeter Wemm **		etc.  In particular, the current time in various
754c2aa98e2SPeter Wemm **		forms is set.
755c2aa98e2SPeter Wemm */
756c2aa98e2SPeter Wemm 
757c2aa98e2SPeter Wemm void
initsys(e)758c2aa98e2SPeter Wemm initsys(e)
759c2aa98e2SPeter Wemm 	register ENVELOPE *e;
760c2aa98e2SPeter Wemm {
76140266059SGregory Neil Shapiro 	char buf[10];
762c2aa98e2SPeter Wemm #ifdef TTYNAME
763c2aa98e2SPeter Wemm 	static char ybuf[60];			/* holds tty id */
764c2aa98e2SPeter Wemm 	register char *p;
765c2aa98e2SPeter Wemm 	extern char *ttyname();
766c2aa98e2SPeter Wemm #endif /* TTYNAME */
767c2aa98e2SPeter Wemm 
768c2aa98e2SPeter Wemm 	/*
769c2aa98e2SPeter Wemm 	**  Give this envelope a reality.
770c2aa98e2SPeter Wemm 	**	I.e., an id, a transcript, and a creation time.
77140266059SGregory Neil Shapiro 	**  We don't select the queue until all of the recipients are known.
772c2aa98e2SPeter Wemm 	*/
773c2aa98e2SPeter Wemm 
774c2aa98e2SPeter Wemm 	openxscript(e);
775c2aa98e2SPeter Wemm 	e->e_ctime = curtime();
77640266059SGregory Neil Shapiro 	e->e_qfletter = '\0';
777c2aa98e2SPeter Wemm 
778c2aa98e2SPeter Wemm 	/*
779c2aa98e2SPeter Wemm 	**  Set OutChannel to something useful if stdout isn't it.
780c2aa98e2SPeter Wemm 	**	This arranges that any extra stuff the mailer produces
781c2aa98e2SPeter Wemm 	**	gets sent back to the user on error (because it is
782c2aa98e2SPeter Wemm 	**	tucked away in the transcript).
783c2aa98e2SPeter Wemm 	*/
784c2aa98e2SPeter Wemm 
785c2aa98e2SPeter Wemm 	if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
786c2aa98e2SPeter Wemm 	    e->e_xfp != NULL)
787c2aa98e2SPeter Wemm 		OutChannel = e->e_xfp;
788c2aa98e2SPeter Wemm 
789c2aa98e2SPeter Wemm 	/*
790c2aa98e2SPeter Wemm 	**  Set up some basic system macros.
791c2aa98e2SPeter Wemm 	*/
792c2aa98e2SPeter Wemm 
793c2aa98e2SPeter Wemm 	/* process id */
794d0cef73dSGregory Neil Shapiro 	(void) sm_snprintf(buf, sizeof(buf), "%d", (int) CurrentPid);
79540266059SGregory Neil Shapiro 	macdefine(&e->e_macro, A_TEMP, 'p', buf);
796c2aa98e2SPeter Wemm 
797c2aa98e2SPeter Wemm 	/* hop count */
798d0cef73dSGregory Neil Shapiro 	(void) sm_snprintf(buf, sizeof(buf), "%d", e->e_hopcount);
79940266059SGregory Neil Shapiro 	macdefine(&e->e_macro, A_TEMP, 'c', buf);
800c2aa98e2SPeter Wemm 
801c2aa98e2SPeter Wemm 	/* time as integer, unix time, arpa time */
802c2aa98e2SPeter Wemm 	settime(e);
803c2aa98e2SPeter Wemm 
80406f25ae9SGregory Neil Shapiro 	/* Load average */
80540266059SGregory Neil Shapiro 	sm_getla();
80606f25ae9SGregory Neil Shapiro 
807c2aa98e2SPeter Wemm #ifdef TTYNAME
808c2aa98e2SPeter Wemm 	/* tty name */
809c2aa98e2SPeter Wemm 	if (macvalue('y', e) == NULL)
810c2aa98e2SPeter Wemm 	{
811c2aa98e2SPeter Wemm 		p = ttyname(2);
812c2aa98e2SPeter Wemm 		if (p != NULL)
813c2aa98e2SPeter Wemm 		{
814c2aa98e2SPeter Wemm 			if (strrchr(p, '/') != NULL)
815c2aa98e2SPeter Wemm 				p = strrchr(p, '/') + 1;
816d0cef73dSGregory Neil Shapiro 			(void) sm_strlcpy(ybuf, sizeof(ybuf), p);
81740266059SGregory Neil Shapiro 			macdefine(&e->e_macro, A_PERM, 'y', ybuf);
818c2aa98e2SPeter Wemm 		}
819c2aa98e2SPeter Wemm 	}
820c2aa98e2SPeter Wemm #endif /* TTYNAME */
821c2aa98e2SPeter Wemm }
82240266059SGregory Neil Shapiro /*
823c2aa98e2SPeter Wemm **  SETTIME -- set the current time.
824c2aa98e2SPeter Wemm **
825c2aa98e2SPeter Wemm **	Parameters:
82606f25ae9SGregory Neil Shapiro **		e -- the envelope in which the macros should be set.
827c2aa98e2SPeter Wemm **
828c2aa98e2SPeter Wemm **	Returns:
829c2aa98e2SPeter Wemm **		none.
830c2aa98e2SPeter Wemm **
831c2aa98e2SPeter Wemm **	Side Effects:
832c2aa98e2SPeter Wemm **		Sets the various time macros -- $a, $b, $d, $t.
833c2aa98e2SPeter Wemm */
834c2aa98e2SPeter Wemm 
835c2aa98e2SPeter Wemm void
settime(e)836c2aa98e2SPeter Wemm settime(e)
837c2aa98e2SPeter Wemm 	register ENVELOPE *e;
838c2aa98e2SPeter Wemm {
839c2aa98e2SPeter Wemm 	register char *p;
840c2aa98e2SPeter Wemm 	auto time_t now;
84140266059SGregory Neil Shapiro 	char buf[30];
842c2aa98e2SPeter Wemm 	register struct tm *tm;
843c2aa98e2SPeter Wemm 
844c2aa98e2SPeter Wemm 	now = curtime();
845d0cef73dSGregory Neil Shapiro 	(void) sm_snprintf(buf, sizeof(buf), "%ld", (long) now);
846e92d3f3fSGregory Neil Shapiro 	macdefine(&e->e_macro, A_TEMP, macid("{time}"), buf);
847c2aa98e2SPeter Wemm 	tm = gmtime(&now);
848d0cef73dSGregory Neil Shapiro 	(void) sm_snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d",
84940266059SGregory Neil Shapiro 			   tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
85040266059SGregory Neil Shapiro 			   tm->tm_hour, tm->tm_min);
85140266059SGregory Neil Shapiro 	macdefine(&e->e_macro, A_TEMP, 't', buf);
852d0cef73dSGregory Neil Shapiro 	(void) sm_strlcpy(buf, ctime(&now), sizeof(buf));
85340266059SGregory Neil Shapiro 	p = strchr(buf, '\n');
854c2aa98e2SPeter Wemm 	if (p != NULL)
855c2aa98e2SPeter Wemm 		*p = '\0';
85640266059SGregory Neil Shapiro 	macdefine(&e->e_macro, A_TEMP, 'd', buf);
85740266059SGregory Neil Shapiro 	macdefine(&e->e_macro, A_TEMP, 'b', arpadate(buf));
858c2aa98e2SPeter Wemm 	if (macvalue('a', e) == NULL)
85940266059SGregory Neil Shapiro 		macdefine(&e->e_macro, A_PERM, 'a', macvalue('b', e));
860c2aa98e2SPeter Wemm }
86140266059SGregory Neil Shapiro /*
862c2aa98e2SPeter Wemm **  OPENXSCRIPT -- Open transcript file
863c2aa98e2SPeter Wemm **
864c2aa98e2SPeter Wemm **	Creates a transcript file for possible eventual mailing or
865c2aa98e2SPeter Wemm **	sending back.
866c2aa98e2SPeter Wemm **
867c2aa98e2SPeter Wemm **	Parameters:
868c2aa98e2SPeter Wemm **		e -- the envelope to create the transcript in/for.
869c2aa98e2SPeter Wemm **
870c2aa98e2SPeter Wemm **	Returns:
871c2aa98e2SPeter Wemm **		none
872c2aa98e2SPeter Wemm **
873c2aa98e2SPeter Wemm **	Side Effects:
874c2aa98e2SPeter Wemm **		Creates the transcript file.
875c2aa98e2SPeter Wemm */
876c2aa98e2SPeter Wemm 
877c2aa98e2SPeter Wemm #ifndef O_APPEND
878c2aa98e2SPeter Wemm # define O_APPEND	0
8795b0945b5SGregory Neil Shapiro #endif
880c2aa98e2SPeter Wemm 
881c2aa98e2SPeter Wemm void
openxscript(e)882c2aa98e2SPeter Wemm openxscript(e)
883c2aa98e2SPeter Wemm 	register ENVELOPE *e;
884c2aa98e2SPeter Wemm {
885c2aa98e2SPeter Wemm 	register char *p;
886c2aa98e2SPeter Wemm 
887c2aa98e2SPeter Wemm 	if (e->e_xfp != NULL)
888c2aa98e2SPeter Wemm 		return;
88906f25ae9SGregory Neil Shapiro 
89006f25ae9SGregory Neil Shapiro #if 0
89106f25ae9SGregory Neil Shapiro 	if (e->e_lockfp == NULL && bitset(EF_INQUEUE, e->e_flags))
89206f25ae9SGregory Neil Shapiro 		syserr("openxscript: job not locked");
8935b0945b5SGregory Neil Shapiro #endif
89406f25ae9SGregory Neil Shapiro 
89540266059SGregory Neil Shapiro 	p = queuename(e, XSCRPT_LETTER);
89606f25ae9SGregory Neil Shapiro 	e->e_xfp = bfopen(p, FileMode, XscriptFileBufferSize,
89706f25ae9SGregory Neil Shapiro 			  SFF_NOTEXCL|SFF_OPENASROOT);
89806f25ae9SGregory Neil Shapiro 
89906f25ae9SGregory Neil Shapiro 	if (e->e_xfp == NULL)
900c2aa98e2SPeter Wemm 	{
901c2aa98e2SPeter Wemm 		syserr("Can't create transcript file %s", p);
90240266059SGregory Neil Shapiro 		e->e_xfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
90340266059SGregory Neil Shapiro 				      SM_PATH_DEVNULL, SM_IO_RDWR, NULL);
90406f25ae9SGregory Neil Shapiro 		if (e->e_xfp == NULL)
90540266059SGregory Neil Shapiro 			syserr("!Can't open %s", SM_PATH_DEVNULL);
906c2aa98e2SPeter Wemm 	}
90740266059SGregory Neil Shapiro 	(void) sm_io_setvbuf(e->e_xfp, SM_TIME_DEFAULT, NULL, SM_IO_LBF, 0);
908c2aa98e2SPeter Wemm 	if (tTd(46, 9))
909c2aa98e2SPeter Wemm 	{
91040266059SGregory Neil Shapiro 		sm_dprintf("openxscript(%s):\n  ", p);
91140266059SGregory Neil Shapiro 		dumpfd(sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL), true,
91240266059SGregory Neil Shapiro 		       false);
913c2aa98e2SPeter Wemm 	}
914c2aa98e2SPeter Wemm }
91540266059SGregory Neil Shapiro /*
916c2aa98e2SPeter Wemm **  CLOSEXSCRIPT -- close the transcript file.
917c2aa98e2SPeter Wemm **
918c2aa98e2SPeter Wemm **	Parameters:
919c2aa98e2SPeter Wemm **		e -- the envelope containing the transcript to close.
920c2aa98e2SPeter Wemm **
921c2aa98e2SPeter Wemm **	Returns:
922c2aa98e2SPeter Wemm **		none.
923c2aa98e2SPeter Wemm **
924c2aa98e2SPeter Wemm **	Side Effects:
925c2aa98e2SPeter Wemm **		none.
926c2aa98e2SPeter Wemm */
927c2aa98e2SPeter Wemm 
928c2aa98e2SPeter Wemm void
closexscript(e)929c2aa98e2SPeter Wemm closexscript(e)
930c2aa98e2SPeter Wemm 	register ENVELOPE *e;
931c2aa98e2SPeter Wemm {
932c2aa98e2SPeter Wemm 	if (e->e_xfp == NULL)
933c2aa98e2SPeter Wemm 		return;
93406f25ae9SGregory Neil Shapiro #if 0
93506f25ae9SGregory Neil Shapiro 	if (e->e_lockfp == NULL)
93606f25ae9SGregory Neil Shapiro 		syserr("closexscript: job not locked");
9375b0945b5SGregory Neil Shapiro #endif
938*2fb4f839SGregory Neil Shapiro 	SM_CLOSE_FP(e->e_xfp);
939c2aa98e2SPeter Wemm }
94040266059SGregory Neil Shapiro /*
941c2aa98e2SPeter Wemm **  SETSENDER -- set the person who this message is from
942c2aa98e2SPeter Wemm **
943c2aa98e2SPeter Wemm **	Under certain circumstances allow the user to say who
944c2aa98e2SPeter Wemm **	s/he is (using -f or -r).  These are:
945c2aa98e2SPeter Wemm **	1.  The user's uid is zero (root).
946c2aa98e2SPeter Wemm **	2.  The user's login name is in an approved list (typically
947c2aa98e2SPeter Wemm **	    from a network server).
948c2aa98e2SPeter Wemm **	3.  The address the user is trying to claim has a
949c2aa98e2SPeter Wemm **	    "!" character in it (since #2 doesn't do it for
950c2aa98e2SPeter Wemm **	    us if we are dialing out for UUCP).
951c2aa98e2SPeter Wemm **	A better check to replace #3 would be if the
952c2aa98e2SPeter Wemm **	effective uid is "UUCP" -- this would require me
953c2aa98e2SPeter Wemm **	to rewrite getpwent to "grab" uucp as it went by,
954c2aa98e2SPeter Wemm **	make getname more nasty, do another passwd file
955c2aa98e2SPeter Wemm **	scan, or compile the UID of "UUCP" into the code,
956c2aa98e2SPeter Wemm **	all of which are reprehensible.
957c2aa98e2SPeter Wemm **
958c2aa98e2SPeter Wemm **	Assuming all of these fail, we figure out something
959c2aa98e2SPeter Wemm **	ourselves.
960c2aa98e2SPeter Wemm **
961c2aa98e2SPeter Wemm **	Parameters:
962*2fb4f839SGregory Neil Shapiro **		from -- the person we would like to believe this message [i]
963c2aa98e2SPeter Wemm **			is from, as specified on the command line.
964c2aa98e2SPeter Wemm **		e -- the envelope in which we would like the sender set.
965c2aa98e2SPeter Wemm **		delimptr -- if non-NULL, set to the location of the
966c2aa98e2SPeter Wemm **			trailing delimiter.
967c2aa98e2SPeter Wemm **		delimchar -- the character that will delimit the sender
968c2aa98e2SPeter Wemm **			address.
969c2aa98e2SPeter Wemm **		internal -- set if this address is coming from an internal
970c2aa98e2SPeter Wemm **			source such as an owner alias.
971c2aa98e2SPeter Wemm **
972c2aa98e2SPeter Wemm **	Returns:
973c2aa98e2SPeter Wemm **		none.
974c2aa98e2SPeter Wemm **
975c2aa98e2SPeter Wemm **	Side Effects:
976c2aa98e2SPeter Wemm **		sets sendmail's notion of who the from person is.
977c2aa98e2SPeter Wemm */
978c2aa98e2SPeter Wemm 
979c2aa98e2SPeter Wemm void
setsender(from,e,delimptr,delimchar,internal)980c2aa98e2SPeter Wemm setsender(from, e, delimptr, delimchar, internal)
981c2aa98e2SPeter Wemm 	char *from;
982c2aa98e2SPeter Wemm 	register ENVELOPE *e;
983c2aa98e2SPeter Wemm 	char **delimptr;
984c2aa98e2SPeter Wemm 	int delimchar;
985c2aa98e2SPeter Wemm 	bool internal;
986c2aa98e2SPeter Wemm {
987c2aa98e2SPeter Wemm 	register char **pvp;
988c2aa98e2SPeter Wemm 	char *realname = NULL;
989c2aa98e2SPeter Wemm 	char *bp;
990*2fb4f839SGregory Neil Shapiro 	char buf[MAXNAME_I + 2];
991c2aa98e2SPeter Wemm 	char pvpbuf[PSBUFSIZE];
992c2aa98e2SPeter Wemm 	extern char *FullName;
993c2aa98e2SPeter Wemm 
994c2aa98e2SPeter Wemm 	if (tTd(45, 1))
99540266059SGregory Neil Shapiro 		sm_dprintf("setsender(%s)\n", from == NULL ? "" : from);
996c2aa98e2SPeter Wemm 
99713bd1963SGregory Neil Shapiro 	/* may be set from earlier calls */
99813bd1963SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, 'x', "");
99913bd1963SGregory Neil Shapiro 
1000c2aa98e2SPeter Wemm 	/*
1001c2aa98e2SPeter Wemm 	**  Figure out the real user executing us.
1002c2aa98e2SPeter Wemm 	**	Username can return errno != 0 on non-errors.
1003c2aa98e2SPeter Wemm 	*/
1004c2aa98e2SPeter Wemm 
1005c2aa98e2SPeter Wemm 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
1006c2aa98e2SPeter Wemm 	    OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
1007c2aa98e2SPeter Wemm 		realname = from;
1008*2fb4f839SGregory Neil Shapiro 	if (SM_IS_EMPTY(realname))
1009c2aa98e2SPeter Wemm 		realname = username();
1010c2aa98e2SPeter Wemm 
1011c2aa98e2SPeter Wemm 	if (ConfigLevel < 2)
101240266059SGregory Neil Shapiro 		SuprErrs = true;
1013c2aa98e2SPeter Wemm 
101440266059SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
101540266059SGregory Neil Shapiro 
101606f25ae9SGregory Neil Shapiro 	/* preset state for then clause in case from == NULL */
101706f25ae9SGregory Neil Shapiro 	e->e_from.q_state = QS_BADADDR;
101806f25ae9SGregory Neil Shapiro 	e->e_from.q_flags = 0;
1019c2aa98e2SPeter Wemm 	if (from == NULL ||
1020c2aa98e2SPeter Wemm 	    parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
102140266059SGregory Neil Shapiro 		      delimchar, delimptr, e, false) == NULL ||
102206f25ae9SGregory Neil Shapiro 	    QS_IS_BADADDR(e->e_from.q_state) ||
1023c2aa98e2SPeter Wemm 	    e->e_from.q_mailer == ProgMailer ||
1024c2aa98e2SPeter Wemm 	    e->e_from.q_mailer == FileMailer ||
1025c2aa98e2SPeter Wemm 	    e->e_from.q_mailer == InclMailer)
1026c2aa98e2SPeter Wemm 	{
1027c2aa98e2SPeter Wemm 		/* log garbage addresses for traceback */
1028c2aa98e2SPeter Wemm 		if (from != NULL && LogLevel > 2)
1029c2aa98e2SPeter Wemm 		{
1030c2aa98e2SPeter Wemm 			char *p;
1031*2fb4f839SGregory Neil Shapiro 			char ebuf[MAXNAME * 2 + 2]; /* EAI:ok? */
1032c2aa98e2SPeter Wemm 
1033c2aa98e2SPeter Wemm 			p = macvalue('_', e);
1034c2aa98e2SPeter Wemm 			if (p == NULL)
1035c2aa98e2SPeter Wemm 			{
1036c2aa98e2SPeter Wemm 				char *host = RealHostName;
1037c2aa98e2SPeter Wemm 
1038c2aa98e2SPeter Wemm 				if (host == NULL)
1039c2aa98e2SPeter Wemm 					host = MyHostName;
1040d0cef73dSGregory Neil Shapiro 				(void) sm_snprintf(ebuf, sizeof(ebuf),
1041*2fb4f839SGregory Neil Shapiro 						   "%.*s@%.*s",
1042*2fb4f839SGregory Neil Shapiro 						   MAXNAME, realname, /* EAI: see above */
1043*2fb4f839SGregory Neil Shapiro 						   MAXNAME, host); /* EAI: see above */
1044c2aa98e2SPeter Wemm 				p = ebuf;
1045c2aa98e2SPeter Wemm 			}
1046c2aa98e2SPeter Wemm 			sm_syslog(LOG_NOTICE, e->e_id,
104706f25ae9SGregory Neil Shapiro 				  "setsender: %s: invalid or unparsable, received from %s",
1048c2aa98e2SPeter Wemm 				  shortenstring(from, 83), p);
1049c2aa98e2SPeter Wemm 		}
1050c2aa98e2SPeter Wemm 		if (from != NULL)
1051c2aa98e2SPeter Wemm 		{
105206f25ae9SGregory Neil Shapiro 			if (!QS_IS_BADADDR(e->e_from.q_state))
1053c2aa98e2SPeter Wemm 			{
1054c2aa98e2SPeter Wemm 				/* it was a bogus mailer in the from addr */
1055c2aa98e2SPeter Wemm 				e->e_status = "5.1.7";
105606f25ae9SGregory Neil Shapiro 				usrerrenh(e->e_status,
105706f25ae9SGregory Neil Shapiro 					  "553 Invalid sender address");
1058c2aa98e2SPeter Wemm 			}
105940266059SGregory Neil Shapiro 			SuprErrs = true;
1060c2aa98e2SPeter Wemm 		}
1061c2aa98e2SPeter Wemm 		if (from == realname ||
1062*2fb4f839SGregory Neil Shapiro /* XXX realname must be [i] */
106340266059SGregory Neil Shapiro 		    parseaddr(from = realname,
106440266059SGregory Neil Shapiro 			      &e->e_from, RF_COPYALL|RF_SENDERADDR, ' ',
106540266059SGregory Neil Shapiro 			      NULL, e, false) == NULL)
1066c2aa98e2SPeter Wemm 		{
1067c2aa98e2SPeter Wemm 			char nbuf[100];
1068c2aa98e2SPeter Wemm 
106940266059SGregory Neil Shapiro 			SuprErrs = true;
1070d0cef73dSGregory Neil Shapiro 			expand("\201n", nbuf, sizeof(nbuf), e);
107140266059SGregory Neil Shapiro 			from = sm_rpool_strdup_x(e->e_rpool, nbuf);
1072*2fb4f839SGregory Neil Shapiro /* XXX from must be [i] */
107340266059SGregory Neil Shapiro 			if (parseaddr(from, &e->e_from, RF_COPYALL, ' ',
107440266059SGregory Neil Shapiro 				      NULL, e, false) == NULL &&
1075c2aa98e2SPeter Wemm 			    parseaddr(from = "postmaster", &e->e_from,
107640266059SGregory Neil Shapiro 				      RF_COPYALL, ' ', NULL, e, false) == NULL)
107706f25ae9SGregory Neil Shapiro 				syserr("553 5.3.0 setsender: can't even parse postmaster!");
1078c2aa98e2SPeter Wemm 		}
1079c2aa98e2SPeter Wemm 	}
1080c2aa98e2SPeter Wemm 	else
108140266059SGregory Neil Shapiro 		FromFlag = true;
108206f25ae9SGregory Neil Shapiro 	e->e_from.q_state = QS_SENDER;
1083c2aa98e2SPeter Wemm 	if (tTd(45, 5))
1084c2aa98e2SPeter Wemm 	{
108540266059SGregory Neil Shapiro 		sm_dprintf("setsender: QS_SENDER ");
1086e92d3f3fSGregory Neil Shapiro 		printaddr(sm_debug_file(), &e->e_from, false);
1087c2aa98e2SPeter Wemm 	}
108840266059SGregory Neil Shapiro 	SuprErrs = false;
1089c2aa98e2SPeter Wemm 
1090c2aa98e2SPeter Wemm #if USERDB
1091c2aa98e2SPeter Wemm 	if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
1092c2aa98e2SPeter Wemm 	{
1093c2aa98e2SPeter Wemm 		register char *p;
1094c2aa98e2SPeter Wemm 
109540266059SGregory Neil Shapiro 		p = udbsender(e->e_from.q_user, e->e_rpool);
1096c2aa98e2SPeter Wemm 		if (p != NULL)
1097c2aa98e2SPeter Wemm 			from = p;
1098c2aa98e2SPeter Wemm 	}
1099c2aa98e2SPeter Wemm #endif /* USERDB */
1100c2aa98e2SPeter Wemm 
1101c2aa98e2SPeter Wemm 	if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
1102c2aa98e2SPeter Wemm 	{
110340266059SGregory Neil Shapiro 		SM_MBDB_T user;
110440266059SGregory Neil Shapiro 
1105c2aa98e2SPeter Wemm 		if (!internal)
1106c2aa98e2SPeter Wemm 		{
1107c2aa98e2SPeter Wemm 			/* if the user already given fullname don't redefine */
1108c2aa98e2SPeter Wemm 			if (FullName == NULL)
1109c2aa98e2SPeter Wemm 				FullName = macvalue('x', e);
1110605302a5SGregory Neil Shapiro 			if (FullName != NULL)
1111605302a5SGregory Neil Shapiro 			{
1112605302a5SGregory Neil Shapiro 				if (FullName[0] == '\0')
1113c2aa98e2SPeter Wemm 					FullName = NULL;
1114605302a5SGregory Neil Shapiro 				else
1115605302a5SGregory Neil Shapiro 					FullName = newstr(FullName);
1116605302a5SGregory Neil Shapiro 			}
1117c2aa98e2SPeter Wemm 		}
1118c2aa98e2SPeter Wemm 
1119c2aa98e2SPeter Wemm 		if (e->e_from.q_user[0] != '\0' &&
112040266059SGregory Neil Shapiro 		    sm_mbdb_lookup(e->e_from.q_user, &user) == EX_OK)
1121c2aa98e2SPeter Wemm 		{
1122c2aa98e2SPeter Wemm 			/*
1123c2aa98e2SPeter Wemm 			**  Process passwd file entry.
1124c2aa98e2SPeter Wemm 			*/
1125c2aa98e2SPeter Wemm 
1126c2aa98e2SPeter Wemm 			/* extract home directory */
112740266059SGregory Neil Shapiro 			if (*user.mbdb_homedir == '\0')
112842e5d165SGregory Neil Shapiro 				e->e_from.q_home = NULL;
112940266059SGregory Neil Shapiro 			else if (strcmp(user.mbdb_homedir, "/") == 0)
113040266059SGregory Neil Shapiro 				e->e_from.q_home = "";
1131c2aa98e2SPeter Wemm 			else
113240266059SGregory Neil Shapiro 				e->e_from.q_home = sm_rpool_strdup_x(e->e_rpool,
113340266059SGregory Neil Shapiro 							user.mbdb_homedir);
113440266059SGregory Neil Shapiro 			macdefine(&e->e_macro, A_PERM, 'z', e->e_from.q_home);
1135c2aa98e2SPeter Wemm 
1136c2aa98e2SPeter Wemm 			/* extract user and group id */
113740266059SGregory Neil Shapiro 			if (user.mbdb_uid != SM_NO_UID)
113840266059SGregory Neil Shapiro 			{
113940266059SGregory Neil Shapiro 				e->e_from.q_uid = user.mbdb_uid;
114040266059SGregory Neil Shapiro 				e->e_from.q_gid = user.mbdb_gid;
1141c2aa98e2SPeter Wemm 				e->e_from.q_flags |= QGOODUID;
114240266059SGregory Neil Shapiro 			}
1143c2aa98e2SPeter Wemm 
1144c2aa98e2SPeter Wemm 			/* extract full name from passwd file */
114540266059SGregory Neil Shapiro 			if (FullName == NULL && !internal &&
114640266059SGregory Neil Shapiro 			    user.mbdb_fullname[0] != '\0' &&
114740266059SGregory Neil Shapiro 			    strcmp(user.mbdb_name, e->e_from.q_user) == 0)
1148c2aa98e2SPeter Wemm 			{
114940266059SGregory Neil Shapiro 				FullName = newstr(user.mbdb_fullname);
1150c2aa98e2SPeter Wemm 			}
1151c2aa98e2SPeter Wemm 		}
1152c2aa98e2SPeter Wemm 		else
1153c2aa98e2SPeter Wemm 		{
115406f25ae9SGregory Neil Shapiro 			e->e_from.q_home = NULL;
1155c2aa98e2SPeter Wemm 		}
1156c2aa98e2SPeter Wemm 		if (FullName != NULL && !internal)
115713bd1963SGregory Neil Shapiro 			macdefine(&e->e_macro, A_TEMP, 'x', FullName);
1158c2aa98e2SPeter Wemm 	}
11592e43090eSPeter Wemm 	else if (!internal && OpMode != MD_DAEMON && OpMode != MD_SMTP)
1160c2aa98e2SPeter Wemm 	{
1161c2aa98e2SPeter Wemm 		if (e->e_from.q_home == NULL)
1162c2aa98e2SPeter Wemm 		{
1163c2aa98e2SPeter Wemm 			e->e_from.q_home = getenv("HOME");
116442e5d165SGregory Neil Shapiro 			if (e->e_from.q_home != NULL)
116542e5d165SGregory Neil Shapiro 			{
116642e5d165SGregory Neil Shapiro 				if (*e->e_from.q_home == '\0')
116742e5d165SGregory Neil Shapiro 					e->e_from.q_home = NULL;
116842e5d165SGregory Neil Shapiro 				else if (strcmp(e->e_from.q_home, "/") == 0)
1169c2aa98e2SPeter Wemm 					e->e_from.q_home++;
1170c2aa98e2SPeter Wemm 			}
117142e5d165SGregory Neil Shapiro 		}
1172c2aa98e2SPeter Wemm 		e->e_from.q_uid = RealUid;
1173c2aa98e2SPeter Wemm 		e->e_from.q_gid = RealGid;
1174c2aa98e2SPeter Wemm 		e->e_from.q_flags |= QGOODUID;
1175c2aa98e2SPeter Wemm 	}
1176c2aa98e2SPeter Wemm 
1177c2aa98e2SPeter Wemm 	/*
1178c2aa98e2SPeter Wemm 	**  Rewrite the from person to dispose of possible implicit
1179c2aa98e2SPeter Wemm 	**	links in the net.
1180c2aa98e2SPeter Wemm 	*/
1181c2aa98e2SPeter Wemm 
1182d0cef73dSGregory Neil Shapiro 	pvp = prescan(from, delimchar, pvpbuf, sizeof(pvpbuf), NULL,
1183d0cef73dSGregory Neil Shapiro 			IntTokenTab, false);
1184c2aa98e2SPeter Wemm 	if (pvp == NULL)
1185c2aa98e2SPeter Wemm 	{
1186c2aa98e2SPeter Wemm 		/* don't need to give error -- prescan did that already */
1187c2aa98e2SPeter Wemm 		if (LogLevel > 2)
1188c2aa98e2SPeter Wemm 			sm_syslog(LOG_NOTICE, e->e_id,
1189c2aa98e2SPeter Wemm 				  "cannot prescan from (%s)",
1190c2aa98e2SPeter Wemm 				  shortenstring(from, MAXSHORTSTR));
119140266059SGregory Neil Shapiro 		finis(true, true, ExitStat);
1192c2aa98e2SPeter Wemm 	}
119340266059SGregory Neil Shapiro 	(void) REWRITE(pvp, 3, e);
119440266059SGregory Neil Shapiro 	(void) REWRITE(pvp, 1, e);
119540266059SGregory Neil Shapiro 	(void) REWRITE(pvp, 4, e);
119640266059SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1197c2aa98e2SPeter Wemm 	bp = buf + 1;
1198d0cef73dSGregory Neil Shapiro 	cataddr(pvp, NULL, bp, sizeof(buf) - 2, '\0', false);
1199c2aa98e2SPeter Wemm 	if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
1200c2aa98e2SPeter Wemm 	{
1201c2aa98e2SPeter Wemm 		/* heuristic: route-addr: add angle brackets */
1202d0cef73dSGregory Neil Shapiro 		(void) sm_strlcat(bp, ">", sizeof(buf) - 1);
1203c2aa98e2SPeter Wemm 		*--bp = '<';
1204c2aa98e2SPeter Wemm 	}
120540266059SGregory Neil Shapiro 	e->e_sender = sm_rpool_strdup_x(e->e_rpool, bp);
120640266059SGregory Neil Shapiro 	macdefine(&e->e_macro, A_PERM, 'f', e->e_sender);
1207c2aa98e2SPeter Wemm 
1208c2aa98e2SPeter Wemm 	/* save the domain spec if this mailer wants it */
1209c2aa98e2SPeter Wemm 	if (e->e_from.q_mailer != NULL &&
1210c2aa98e2SPeter Wemm 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
1211c2aa98e2SPeter Wemm 	{
1212c2aa98e2SPeter Wemm 		char **lastat;
1213c2aa98e2SPeter Wemm 
1214c2aa98e2SPeter Wemm 		/* get rid of any pesky angle brackets */
121540266059SGregory Neil Shapiro 		macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s");
121640266059SGregory Neil Shapiro 		(void) REWRITE(pvp, 3, e);
121740266059SGregory Neil Shapiro 		(void) REWRITE(pvp, 1, e);
121840266059SGregory Neil Shapiro 		(void) REWRITE(pvp, 4, e);
121940266059SGregory Neil Shapiro 		macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL);
1220c2aa98e2SPeter Wemm 
1221c2aa98e2SPeter Wemm 		/* strip off to the last "@" sign */
1222c2aa98e2SPeter Wemm 		for (lastat = NULL; *pvp != NULL; pvp++)
1223e92d3f3fSGregory Neil Shapiro 		{
1224c2aa98e2SPeter Wemm 			if (strcmp(*pvp, "@") == 0)
1225c2aa98e2SPeter Wemm 				lastat = pvp;
1226e92d3f3fSGregory Neil Shapiro 		}
1227c2aa98e2SPeter Wemm 		if (lastat != NULL)
1228c2aa98e2SPeter Wemm 		{
122940266059SGregory Neil Shapiro 			e->e_fromdomain = copyplist(lastat, true, e->e_rpool);
1230c2aa98e2SPeter Wemm 			if (tTd(45, 3))
1231c2aa98e2SPeter Wemm 			{
123240266059SGregory Neil Shapiro 				sm_dprintf("Saving from domain: ");
1233e92d3f3fSGregory Neil Shapiro 				printav(sm_debug_file(), e->e_fromdomain);
1234c2aa98e2SPeter Wemm 			}
1235c2aa98e2SPeter Wemm 		}
1236c2aa98e2SPeter Wemm 	}
1237c2aa98e2SPeter Wemm }
123840266059SGregory Neil Shapiro /*
1239c2aa98e2SPeter Wemm **  PRINTENVFLAGS -- print envelope flags for debugging
1240c2aa98e2SPeter Wemm **
1241c2aa98e2SPeter Wemm **	Parameters:
1242c2aa98e2SPeter Wemm **		e -- the envelope with the flags to be printed.
1243c2aa98e2SPeter Wemm **
1244c2aa98e2SPeter Wemm **	Returns:
1245c2aa98e2SPeter Wemm **		none.
1246c2aa98e2SPeter Wemm */
1247c2aa98e2SPeter Wemm 
1248c2aa98e2SPeter Wemm struct eflags
1249c2aa98e2SPeter Wemm {
1250c2aa98e2SPeter Wemm 	char		*ef_name;
125140266059SGregory Neil Shapiro 	unsigned long	ef_bit;
1252c2aa98e2SPeter Wemm };
1253c2aa98e2SPeter Wemm 
125406f25ae9SGregory Neil Shapiro static struct eflags	EnvelopeFlags[] =
1255c2aa98e2SPeter Wemm {
1256c2aa98e2SPeter Wemm 	{ "OLDSTYLE",		EF_OLDSTYLE	},
1257c2aa98e2SPeter Wemm 	{ "INQUEUE",		EF_INQUEUE	},
1258c2aa98e2SPeter Wemm 	{ "NO_BODY_RETN",	EF_NO_BODY_RETN	},
1259c2aa98e2SPeter Wemm 	{ "CLRQUEUE",		EF_CLRQUEUE	},
1260c2aa98e2SPeter Wemm 	{ "SENDRECEIPT",	EF_SENDRECEIPT	},
1261c2aa98e2SPeter Wemm 	{ "FATALERRS",		EF_FATALERRS	},
1262c2aa98e2SPeter Wemm 	{ "DELETE_BCC",		EF_DELETE_BCC	},
1263c2aa98e2SPeter Wemm 	{ "RESPONSE",		EF_RESPONSE	},
1264c2aa98e2SPeter Wemm 	{ "RESENT",		EF_RESENT	},
1265c2aa98e2SPeter Wemm 	{ "VRFYONLY",		EF_VRFYONLY	},
1266c2aa98e2SPeter Wemm 	{ "WARNING",		EF_WARNING	},
1267c2aa98e2SPeter Wemm 	{ "QUEUERUN",		EF_QUEUERUN	},
1268c2aa98e2SPeter Wemm 	{ "GLOBALERRS",		EF_GLOBALERRS	},
1269c2aa98e2SPeter Wemm 	{ "PM_NOTIFY",		EF_PM_NOTIFY	},
1270c2aa98e2SPeter Wemm 	{ "METOO",		EF_METOO	},
1271c2aa98e2SPeter Wemm 	{ "LOGSENDER",		EF_LOGSENDER	},
1272c2aa98e2SPeter Wemm 	{ "NORECEIPT",		EF_NORECEIPT	},
1273c2aa98e2SPeter Wemm 	{ "HAS8BIT",		EF_HAS8BIT	},
1274c2aa98e2SPeter Wemm 	{ "RET_PARAM",		EF_RET_PARAM	},
1275c2aa98e2SPeter Wemm 	{ "HAS_DF",		EF_HAS_DF	},
1276c2aa98e2SPeter Wemm 	{ "IS_MIME",		EF_IS_MIME	},
1277c2aa98e2SPeter Wemm 	{ "DONT_MIME",		EF_DONT_MIME	},
127840266059SGregory Neil Shapiro 	{ "DISCARD",		EF_DISCARD	},
127940266059SGregory Neil Shapiro 	{ "TOOBIG",		EF_TOOBIG	},
128040266059SGregory Neil Shapiro 	{ "SPLIT",		EF_SPLIT	},
128140266059SGregory Neil Shapiro 	{ "UNSAFE",		EF_UNSAFE	},
12825b0945b5SGregory Neil Shapiro 	{ "TOODEEP",		EF_TOODEEP	},
12835b0945b5SGregory Neil Shapiro 	{ "SECURE",		EF_SECURE	},
1284193538b7SGregory Neil Shapiro 	{ NULL,			0		}
1285c2aa98e2SPeter Wemm };
1286c2aa98e2SPeter Wemm 
1287c2aa98e2SPeter Wemm void
printenvflags(e)1288c2aa98e2SPeter Wemm printenvflags(e)
1289c2aa98e2SPeter Wemm 	register ENVELOPE *e;
1290c2aa98e2SPeter Wemm {
1291c2aa98e2SPeter Wemm 	register struct eflags *ef;
129240266059SGregory Neil Shapiro 	bool first = true;
1293c2aa98e2SPeter Wemm 
1294e92d3f3fSGregory Neil Shapiro 	sm_dprintf("%lx", e->e_flags);
1295c2aa98e2SPeter Wemm 	for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++)
1296c2aa98e2SPeter Wemm 	{
1297c2aa98e2SPeter Wemm 		if (!bitset(ef->ef_bit, e->e_flags))
1298c2aa98e2SPeter Wemm 			continue;
1299c2aa98e2SPeter Wemm 		if (first)
1300e92d3f3fSGregory Neil Shapiro 			sm_dprintf("<%s", ef->ef_name);
1301c2aa98e2SPeter Wemm 		else
1302e92d3f3fSGregory Neil Shapiro 			sm_dprintf(",%s", ef->ef_name);
130340266059SGregory Neil Shapiro 		first = false;
1304c2aa98e2SPeter Wemm 	}
1305c2aa98e2SPeter Wemm 	if (!first)
1306e92d3f3fSGregory Neil Shapiro 		sm_dprintf(">\n");
1307c2aa98e2SPeter Wemm }
1308