125a99e2eSeric # include <signal.h>
254aa2b0fSeric # include <errno.h>
3b20b3270Seric # include "sendmail.h"
4c77d1c25Seric # include <sys/stat.h>
525a99e2eSeric 
6*3bea8136Seric SCCSID(@(#)deliver.c	3.107		09/05/82);
7259cace7Seric 
825a99e2eSeric /*
913bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
1013bbc08cSeric **
1113bbc08cSeric **	This routine delivers to everyone on the same host as the
1213bbc08cSeric **	user on the head of the list.  It is clever about mailers
1313bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
1413bbc08cSeric **	that it will deliver to all these addresses however -- so
1513bbc08cSeric **	deliver should be called once for each address on the
1613bbc08cSeric **	list.
1725a99e2eSeric **
1825a99e2eSeric **	Parameters:
19c77d1c25Seric **		firstto -- head of the address list to deliver to.
2025a99e2eSeric **
2125a99e2eSeric **	Returns:
2225a99e2eSeric **		zero -- successfully delivered.
2325a99e2eSeric **		else -- some failure, see ExitStat for more info.
2425a99e2eSeric **
2525a99e2eSeric **	Side Effects:
2625a99e2eSeric **		The standard input is passed off to someone.
2725a99e2eSeric */
2825a99e2eSeric 
2951552439Seric deliver(firstto)
30c77d1c25Seric 	ADDRESS *firstto;
3125a99e2eSeric {
3278442df3Seric 	char *host;			/* host being sent to */
3378442df3Seric 	char *user;			/* user being sent to */
3425a99e2eSeric 	char **pvp;
355dfc646bSeric 	register char **mvp;
3625a99e2eSeric 	register char *p;
3778442df3Seric 	register struct mailer *m;	/* mailer for this recipient */
382a6e0786Seric 	extern bool checkcompat();
395dfc646bSeric 	char *pv[MAXPV+1];
4078442df3Seric 	char tobuf[MAXLINE];		/* text line of to people */
415dfc646bSeric 	char buf[MAXNAME];
426259796dSeric 	ADDRESS *ctladdr;
436259796dSeric 	extern ADDRESS *getctladdr();
4478442df3Seric 	char tfrombuf[MAXNAME];		/* translated from person */
4578442df3Seric 	extern char **prescan();
46c77d1c25Seric 	register ADDRESS *to = firstto;
47c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
48772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
49da2935e1Seric 	bool notopen = TRUE;		/* set if connection not quite open */
505826d9d3Seric 	register int rcode;		/* response code */
5125a99e2eSeric 
5235490626Seric 	errno = 0;
53da2935e1Seric 	if (bitset(QDONTSEND, to->q_flags))
545dfc646bSeric 		return (0);
5525a99e2eSeric 
5651552439Seric 	m = to->q_mailer;
5751552439Seric 	host = to->q_host;
5851552439Seric 
5925a99e2eSeric # ifdef DEBUG
606ef48975Seric 	if (tTd(10, 1))
615dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
6251552439Seric 			m->m_mno, host, to->q_user);
6325a99e2eSeric # endif DEBUG
64f3dbc832Seric 
65f3dbc832Seric 	/*
66f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
67f3dbc832Seric 	**  connections now, just mark these addresses and return.
68f3dbc832Seric 	**	This is useful if we want to batch connections to
69f3dbc832Seric 	**	reduce load.  This will cause the messages to be
70f3dbc832Seric 	**	queued up, and a daemon will come along to send the
71f3dbc832Seric 	**	messages later.
72f3dbc832Seric 	**		This should be on a per-mailer basis.
73f3dbc832Seric 	*/
74f3dbc832Seric 
75f3dbc832Seric 	if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags))
76f3dbc832Seric 	{
77f3dbc832Seric 		for (; to != NULL; to = to->q_next)
780d4209fdSeric 			if (!bitset(QDONTSEND, to->q_flags) &&
790d4209fdSeric 			    to->q_mailer == firstto->q_mailer)
80f3dbc832Seric 				to->q_flags |= QQUEUEUP|QDONTSEND;
81f3dbc832Seric 		return (0);
82f3dbc832Seric 	}
83f3dbc832Seric 
8425a99e2eSeric 	/*
855dfc646bSeric 	**  Do initial argv setup.
865dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
875dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
885dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
895dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
905dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
91*3bea8136Seric 	**		The from address rewrite is expected to make
92*3bea8136Seric 	**		the address relative to the other end.
935dfc646bSeric 	*/
945dfc646bSeric 
9578442df3Seric 	/* rewrite from address, using rewriting rules */
96*3bea8136Seric 	expand("$f", buf, &buf[sizeof buf - 1], CurEnv);
9778442df3Seric 	mvp = prescan(buf, '\0');
98*3bea8136Seric 	rewrite(mvp, m->m_s_rwset);
9978442df3Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
10078442df3Seric 
10178442df3Seric 	define('g', tfrombuf);		/* translated sender address */
1025dfc646bSeric 	define('h', host);		/* to host */
1035dfc646bSeric 	Errors = 0;
1045dfc646bSeric 	pvp = pv;
1055dfc646bSeric 	*pvp++ = m->m_argv[0];
1065dfc646bSeric 
1075dfc646bSeric 	/* insert -f or -r flag as appropriate */
1085dfc646bSeric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1095dfc646bSeric 	{
1105dfc646bSeric 		if (bitset(M_FOPT, m->m_flags))
1115dfc646bSeric 			*pvp++ = "-f";
1125dfc646bSeric 		else
1135dfc646bSeric 			*pvp++ = "-r";
11451552439Seric 		expand("$g", buf, &buf[sizeof buf - 1], CurEnv);
1155dfc646bSeric 		*pvp++ = newstr(buf);
1165dfc646bSeric 	}
1175dfc646bSeric 
1185dfc646bSeric 	/*
1195dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1205dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1215dfc646bSeric 	**  be one of these, and there are only a few more slots
1225dfc646bSeric 	**  in the pv after it.
1235dfc646bSeric 	*/
1245dfc646bSeric 
1255dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1265dfc646bSeric 	{
1275dfc646bSeric 		while ((p = index(p, '$')) != NULL)
1285dfc646bSeric 			if (*++p == 'u')
1295dfc646bSeric 				break;
1305dfc646bSeric 		if (p != NULL)
1315dfc646bSeric 			break;
1325dfc646bSeric 
1335dfc646bSeric 		/* this entry is safe -- go ahead and process it */
13451552439Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
1355dfc646bSeric 		*pvp++ = newstr(buf);
1365dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1375dfc646bSeric 		{
1385dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1395dfc646bSeric 			return (-1);
1405dfc646bSeric 		}
1415dfc646bSeric 	}
142c579ef51Seric 
14333db8731Seric 	/*
14433db8731Seric 	**  If we have no substitution for the user name in the argument
14533db8731Seric 	**  list, we know that we must supply the names otherwise -- and
14633db8731Seric 	**  SMTP is the answer!!
14733db8731Seric 	*/
14833db8731Seric 
1495dfc646bSeric 	if (*mvp == NULL)
150c579ef51Seric 	{
151c579ef51Seric 		/* running SMTP */
1522c7e1b8dSeric # ifdef SMTP
153c579ef51Seric 		clever = TRUE;
154c579ef51Seric 		*pvp = NULL;
1552c7e1b8dSeric # else SMTP
15633db8731Seric 		/* oops!  we don't implement SMTP */
1572c7e1b8dSeric 		syserr("SMTP style mailer");
1582c7e1b8dSeric 		return (EX_SOFTWARE);
1592c7e1b8dSeric # endif SMTP
160c579ef51Seric 	}
1615dfc646bSeric 
1625dfc646bSeric 	/*
1635dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
1645dfc646bSeric 	**  run through our address list and append all the addresses
1655dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
1665dfc646bSeric 	**  always send another copy later.
1675dfc646bSeric 	*/
1685dfc646bSeric 
1695dfc646bSeric 	tobuf[0] = '\0';
1707a941e7aSeric 	CurEnv->e_to = tobuf;
1716259796dSeric 	ctladdr = NULL;
1725dfc646bSeric 	for (; to != NULL; to = to->q_next)
1735dfc646bSeric 	{
1745dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
175bea22b26Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1765dfc646bSeric 			break;
1775dfc646bSeric 
1785dfc646bSeric 		/* if already sent or not for this host, don't send */
179da2935e1Seric 		if (bitset(QDONTSEND, to->q_flags) ||
180da2935e1Seric 		    strcmp(to->q_host, host) != 0 ||
181da2935e1Seric 		    to->q_mailer != firstto->q_mailer)
1825dfc646bSeric 			continue;
1836259796dSeric 
184772e6e50Seric # ifdef DEBUG
1856ef48975Seric 		if (tTd(10, 1))
186772e6e50Seric 		{
187772e6e50Seric 			printf("\nsend to ");
188772e6e50Seric 			printaddr(to, FALSE);
189772e6e50Seric 		}
190772e6e50Seric # endif DEBUG
191772e6e50Seric 
1926259796dSeric 		/* compute effective uid/gid when sending */
1937da1035fSeric 		if (to->q_mailer == ProgMailer)
1946259796dSeric 			ctladdr = getctladdr(to);
1956259796dSeric 
1965dfc646bSeric 		user = to->q_user;
1977a941e7aSeric 		CurEnv->e_to = to->q_paddr;
1985dfc646bSeric 		to->q_flags |= QDONTSEND;
1995dfc646bSeric 
2005dfc646bSeric 		/*
2015dfc646bSeric 		**  Check to see that these people are allowed to
2025dfc646bSeric 		**  talk to each other.
2032a6e0786Seric 		*/
2042a6e0786Seric 
2052a6e0786Seric 		if (!checkcompat(to))
2065dfc646bSeric 		{
2075dfc646bSeric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
2085dfc646bSeric 			continue;
2095dfc646bSeric 		}
2102a6e0786Seric 
2112a6e0786Seric 		/*
2129ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2139ec9501bSeric 		**	about them.
21425a99e2eSeric 		*/
21525a99e2eSeric 
2162a6e0786Seric 		if (bitset(M_STRIPQ, m->m_flags))
21725a99e2eSeric 		{
2189ec9501bSeric 			stripquotes(user, TRUE);
2199ec9501bSeric 			stripquotes(host, TRUE);
2209ec9501bSeric 		}
2219ec9501bSeric 		else
2229ec9501bSeric 		{
2239ec9501bSeric 			stripquotes(user, FALSE);
2249ec9501bSeric 			stripquotes(host, FALSE);
22525a99e2eSeric 		}
22625a99e2eSeric 
22725a99e2eSeric 		/*
228da2935e1Seric 		**  Do initial connection setup if needed.
229da2935e1Seric 		*/
230da2935e1Seric 
231da2935e1Seric 		if (notopen)
232da2935e1Seric 		{
233da2935e1Seric 			message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
234da2935e1Seric # ifdef SMTP
235da2935e1Seric 			if (clever)
236da2935e1Seric 			{
237da2935e1Seric 				/* send the initial SMTP protocol */
2385826d9d3Seric 				rcode = smtpinit(m, pv, (ADDRESS *) NULL);
239da2935e1Seric 			}
240da2935e1Seric # ifdef SMTP
241da2935e1Seric 			notopen = FALSE;
242da2935e1Seric 		}
243da2935e1Seric 
244da2935e1Seric 		/*
245508daeccSeric 		**  Pass it to the other host if we are running SMTP.
246508daeccSeric 		*/
247508daeccSeric 
248508daeccSeric 		if (clever)
249508daeccSeric 		{
250508daeccSeric # ifdef SMTP
2515826d9d3Seric 			if (rcode == EX_OK)
2525826d9d3Seric 				rcode = smtprcpt(to);
2535826d9d3Seric 			if (rcode != EX_OK)
254508daeccSeric 			{
2555826d9d3Seric 				if (rcode == EX_TEMPFAIL)
256508daeccSeric 					to->q_flags |= QQUEUEUP;
257508daeccSeric 				else
258508daeccSeric 					to->q_flags |= QBADADDR;
2595826d9d3Seric 				giveresponse(rcode, TRUE, m);
260508daeccSeric 			}
261508daeccSeric # else SMTP
262508daeccSeric 			syserr("trying to be clever");
263508daeccSeric # endif SMTP
264508daeccSeric 		}
265508daeccSeric 
266508daeccSeric 		/*
2673efaed6eSeric 		**  If an error message has already been given, don't
2683efaed6eSeric 		**	bother to send to this address.
2693efaed6eSeric 		**
2703efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2713efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2723efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2733efaed6eSeric 		*/
2743efaed6eSeric 
2756cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2763efaed6eSeric 			continue;
2773efaed6eSeric 
278f2fec898Seric 		/* save statistics.... */
2797da1035fSeric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2807a941e7aSeric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
281f2fec898Seric 
2823efaed6eSeric 		/*
28325a99e2eSeric 		**  See if this user name is "special".
28425a99e2eSeric 		**	If the user name has a slash in it, assume that this
28551552439Seric 		**	is a file -- send it off without further ado.  Note
28651552439Seric 		**	that this type of addresses is not processed along
28751552439Seric 		**	with the others, so we fudge on the To person.
28825a99e2eSeric 		*/
28925a99e2eSeric 
2907da1035fSeric 		if (m == LocalMailer)
29125a99e2eSeric 		{
292a49f24c0Seric 			if (user[0] == '/')
29325a99e2eSeric 			{
2945826d9d3Seric 				rcode = mailfile(user, getctladdr(to));
2955826d9d3Seric 				giveresponse(rcode, TRUE, m);
2965dfc646bSeric 				continue;
29725a99e2eSeric 			}
29825a99e2eSeric 		}
29925a99e2eSeric 
30013bbc08cSeric 		/*
30113bbc08cSeric 		**  Address is verified -- add this user to mailer
30213bbc08cSeric 		**  argv, and add it to the print list of recipients.
30313bbc08cSeric 		*/
30413bbc08cSeric 
305508daeccSeric 		/* link together the chain of recipients */
306508daeccSeric 		to->q_tchain = tochain;
307508daeccSeric 		tochain = to;
308508daeccSeric 
3095dfc646bSeric 		/* create list of users for error messages */
3105dfc646bSeric 		if (tobuf[0] != '\0')
311db8841e9Seric 			(void) strcat(tobuf, ",");
312db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
3135dfc646bSeric 		define('u', user);		/* to user */
314c2567733Seric 		define('z', to->q_home);	/* user's home */
3155dfc646bSeric 
316c579ef51Seric 		/*
317508daeccSeric 		**  Expand out this user into argument list.
318c579ef51Seric 		*/
319c579ef51Seric 
320508daeccSeric 		if (!clever)
321c579ef51Seric 		{
32251552439Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3235dfc646bSeric 			*pvp++ = newstr(buf);
3245dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3255dfc646bSeric 			{
3265dfc646bSeric 				/* allow some space for trailing parms */
3275dfc646bSeric 				break;
3285dfc646bSeric 			}
3295dfc646bSeric 		}
330c579ef51Seric 	}
3315dfc646bSeric 
332145b49b1Seric 	/* see if any addresses still exist */
333145b49b1Seric 	if (tobuf[0] == '\0')
334c579ef51Seric 	{
3352c7e1b8dSeric # ifdef SMTP
336c579ef51Seric 		if (clever)
3374a3f06b2Seric 			smtpquit(pv[0], FALSE);
3382c7e1b8dSeric # endif SMTP
3395e663df1Seric 		define('g', (char *) NULL);
340145b49b1Seric 		return (0);
341c579ef51Seric 	}
342145b49b1Seric 
3435dfc646bSeric 	/* print out messages as full list */
3447a941e7aSeric 	CurEnv->e_to = tobuf;
3455dfc646bSeric 
3465dfc646bSeric 	/*
3475dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3485dfc646bSeric 	*/
3495dfc646bSeric 
350c579ef51Seric 	while (!clever && *++mvp != NULL)
3515dfc646bSeric 	{
35251552439Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3535dfc646bSeric 		*pvp++ = newstr(buf);
3545dfc646bSeric 		if (pvp >= &pv[MAXPV])
3555dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
3565dfc646bSeric 	}
3575dfc646bSeric 	*pvp++ = NULL;
3585dfc646bSeric 
35925a99e2eSeric 	/*
36025a99e2eSeric 	**  Call the mailer.
3616328bdf7Seric 	**	The argument vector gets built, pipes
36225a99e2eSeric 	**	are created as necessary, and we fork & exec as
3636328bdf7Seric 	**	appropriate.
364c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
36525a99e2eSeric 	*/
36625a99e2eSeric 
3676259796dSeric 	if (ctladdr == NULL)
3687a941e7aSeric 		ctladdr = &CurEnv->e_from;
3692c7e1b8dSeric # ifdef SMTP
370c579ef51Seric 	if (clever)
371c579ef51Seric 	{
3725826d9d3Seric 		rcode = smtpfinish(m, CurEnv);
3735826d9d3Seric 		if (rcode != EX_OK)
3745826d9d3Seric 			giveresponse(rcode, TRUE, m);
3755826d9d3Seric 		smtpquit(pv[0], rcode == EX_OK);
376c579ef51Seric 	}
377c579ef51Seric 	else
3782c7e1b8dSeric # endif SMTP
3795826d9d3Seric 		rcode = sendoff(m, pv, ctladdr);
3805dfc646bSeric 
381c77d1c25Seric 	/*
382c77d1c25Seric 	**  If we got a temporary failure, arrange to queue the
383c77d1c25Seric 	**  addressees.
384c77d1c25Seric 	*/
385c77d1c25Seric 
3865826d9d3Seric 	if (rcode == EX_TEMPFAIL)
387c77d1c25Seric 	{
388772e6e50Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
389c77d1c25Seric 			to->q_flags |= QQUEUEUP;
390c77d1c25Seric 	}
391c77d1c25Seric 
39235490626Seric 	errno = 0;
3935e663df1Seric 	define('g', (char *) NULL);
3945826d9d3Seric 	return (rcode);
39525a99e2eSeric }
3965dfc646bSeric /*
39732d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
39832d19d43Seric **
39932d19d43Seric **	This MUST be a macro, since after a vfork we are running
40032d19d43Seric **	two processes on the same stack!!!
40132d19d43Seric **
40232d19d43Seric **	Parameters:
40332d19d43Seric **		none.
40432d19d43Seric **
40532d19d43Seric **	Returns:
40632d19d43Seric **		From a macro???  You've got to be kidding!
40732d19d43Seric **
40832d19d43Seric **	Side Effects:
40932d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
41032d19d43Seric **			pid of child in parent, zero in child.
41132d19d43Seric **			-1 on unrecoverable error.
41232d19d43Seric **
41332d19d43Seric **	Notes:
41432d19d43Seric **		I'm awfully sorry this looks so awful.  That's
41532d19d43Seric **		vfork for you.....
41632d19d43Seric */
41732d19d43Seric 
41832d19d43Seric # define NFORKTRIES	5
41932d19d43Seric # ifdef VFORK
42032d19d43Seric # define XFORK	vfork
42132d19d43Seric # else VFORK
42232d19d43Seric # define XFORK	fork
42332d19d43Seric # endif VFORK
42432d19d43Seric 
42532d19d43Seric # define DOFORK(fORKfN) \
42632d19d43Seric {\
42732d19d43Seric 	register int i;\
42832d19d43Seric \
42932d19d43Seric 	for (i = NFORKTRIES; i-- > 0; )\
43032d19d43Seric 	{\
43132d19d43Seric 		pid = fORKfN();\
43232d19d43Seric 		if (pid >= 0)\
43332d19d43Seric 			break;\
4345e663df1Seric 		sleep(NFORKTRIES - i);\
43532d19d43Seric 	}\
43632d19d43Seric }
43732d19d43Seric /*
4382ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
4392ed72599Seric **
4402ed72599Seric **	Parameters:
4412ed72599Seric **		none.
4422ed72599Seric **
4432ed72599Seric **	Returns:
4442ed72599Seric **		pid of child in parent.
4452ed72599Seric **		zero in child.
4462ed72599Seric **		-1 on error.
4472ed72599Seric **
4482ed72599Seric **	Side Effects:
4492ed72599Seric **		returns twice, once in parent and once in child.
4502ed72599Seric */
4512ed72599Seric 
4522ed72599Seric dofork()
4532ed72599Seric {
4542ed72599Seric 	register int pid;
4552ed72599Seric 
4562ed72599Seric 	DOFORK(fork);
4572ed72599Seric 	return (pid);
4582ed72599Seric }
4592ed72599Seric /*
4605dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
4615dfc646bSeric **
4625dfc646bSeric **	Parameters:
4635dfc646bSeric **		m -- mailer descriptor.
4645dfc646bSeric **		pvp -- parameter vector to send to it.
4656259796dSeric **		ctladdr -- an address pointer controlling the
4666259796dSeric **			user/groupid etc. of the mailer.
4675dfc646bSeric **
4685dfc646bSeric **	Returns:
4695dfc646bSeric **		exit status of mailer.
4705dfc646bSeric **
4715dfc646bSeric **	Side Effects:
4725dfc646bSeric **		none.
4735dfc646bSeric */
4745dfc646bSeric 
47551552439Seric sendoff(m, pvp, ctladdr)
4765dfc646bSeric 	struct mailer *m;
4775dfc646bSeric 	char **pvp;
4786259796dSeric 	ADDRESS *ctladdr;
4795dfc646bSeric {
480c579ef51Seric 	auto FILE *mfile;
481c579ef51Seric 	auto FILE *rfile;
4825dfc646bSeric 	register int i;
483c579ef51Seric 	int pid;
484c579ef51Seric 
485c579ef51Seric 	/*
486c579ef51Seric 	**  Create connection to mailer.
487c579ef51Seric 	*/
488c579ef51Seric 
489c579ef51Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
490c579ef51Seric 	if (pid < 0)
491c579ef51Seric 		return (-1);
492c579ef51Seric 
493c579ef51Seric 	/*
494c579ef51Seric 	**  Format and send message.
495c579ef51Seric 	*/
496c579ef51Seric 
497c579ef51Seric 	(void) signal(SIGPIPE, SIG_IGN);
49851552439Seric 	putfromline(mfile, m);
49951552439Seric 	(*CurEnv->e_puthdr)(mfile, m, CurEnv);
50051552439Seric 	fprintf(mfile, "\n");
50151552439Seric 	(*CurEnv->e_putbody)(mfile, m, FALSE);
502c579ef51Seric 	(void) fclose(mfile);
503c579ef51Seric 
504c579ef51Seric 	i = endmailer(pid, pvp[0]);
505c579ef51Seric 	giveresponse(i, TRUE, m);
506bc6e2962Seric 
507bc6e2962Seric 	/* arrange a return receipt if requested */
508*3bea8136Seric 	if (CurEnv->e_receiptto != NULL && bitset(M_LOCAL, m->m_flags))
509bc6e2962Seric 	{
5107a941e7aSeric 		CurEnv->e_sendreceipt = TRUE;
511*3bea8136Seric 		if (ExitStat == EX_OK)
512*3bea8136Seric 			fprintf(Xscript, "%s... successfully delivered\n",
513*3bea8136Seric 				CurEnv->e_to);
514bc6e2962Seric 		/* do we want to send back more info? */
515bc6e2962Seric 	}
516bc6e2962Seric 
517c579ef51Seric 	return (i);
518c579ef51Seric }
519c579ef51Seric /*
520c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
521c579ef51Seric **
522c579ef51Seric **	We should never get fatal errors (e.g., segmentation
523c579ef51Seric **	violation), so we report those specially.  For other
524c579ef51Seric **	errors, we choose a status message (into statmsg),
525c579ef51Seric **	and if it represents an error, we print it.
526c579ef51Seric **
527c579ef51Seric **	Parameters:
528c579ef51Seric **		pid -- pid of mailer.
529c579ef51Seric **		name -- name of mailer (for error messages).
530c579ef51Seric **
531c579ef51Seric **	Returns:
532c579ef51Seric **		exit code of mailer.
533c579ef51Seric **
534c579ef51Seric **	Side Effects:
535c579ef51Seric **		none.
536c579ef51Seric */
537c579ef51Seric 
538c579ef51Seric endmailer(pid, name)
539c579ef51Seric 	int pid;
540c579ef51Seric 	char *name;
541c579ef51Seric {
542c579ef51Seric 	register int i;
543c579ef51Seric 	auto int st;
544c579ef51Seric 
54533db8731Seric 	/* in the IPC case there is nothing to wait for */
54633db8731Seric 	if (pid == 0)
54733db8731Seric 		return (EX_OK);
54833db8731Seric 
54933db8731Seric 	/* wait for the mailer process to die and collect status */
550c579ef51Seric 	while ((i = wait(&st)) > 0 && i != pid)
551c579ef51Seric 		continue;
552c579ef51Seric 	if (i < 0)
553c579ef51Seric 	{
554c579ef51Seric 		syserr("wait");
555c579ef51Seric 		return (-1);
556c579ef51Seric 	}
55733db8731Seric 
55833db8731Seric 	/* see if it died a horrid death */
559c579ef51Seric 	if ((st & 0377) != 0)
560c579ef51Seric 	{
561c579ef51Seric 		syserr("%s: stat %o", name, st);
562c579ef51Seric 		ExitStat = EX_UNAVAILABLE;
563c579ef51Seric 		return (-1);
564c579ef51Seric 	}
56533db8731Seric 
56633db8731Seric 	/* normal death -- return status */
567c579ef51Seric 	i = (st >> 8) & 0377;
568c579ef51Seric 	return (i);
569c579ef51Seric }
570c579ef51Seric /*
571c579ef51Seric **  OPENMAILER -- open connection to mailer.
572c579ef51Seric **
573c579ef51Seric **	Parameters:
574c579ef51Seric **		m -- mailer descriptor.
575c579ef51Seric **		pvp -- parameter vector to pass to mailer.
576c579ef51Seric **		ctladdr -- controlling address for user.
577c579ef51Seric **		clever -- create a full duplex connection.
578c579ef51Seric **		pmfile -- pointer to mfile (to mailer) connection.
579c579ef51Seric **		prfile -- pointer to rfile (from mailer) connection.
580c579ef51Seric **
581c579ef51Seric **	Returns:
58233db8731Seric **		pid of mailer ( > 0 ).
583c579ef51Seric **		-1 on error.
58433db8731Seric **		zero on an IPC connection.
585c579ef51Seric **
586c579ef51Seric **	Side Effects:
587c579ef51Seric **		creates a mailer in a subprocess.
588c579ef51Seric */
589c579ef51Seric 
590c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
591c579ef51Seric 	struct mailer *m;
592c579ef51Seric 	char **pvp;
593c579ef51Seric 	ADDRESS *ctladdr;
594c579ef51Seric 	bool clever;
595c579ef51Seric 	FILE **pmfile;
596c579ef51Seric 	FILE **prfile;
597c579ef51Seric {
5985dfc646bSeric 	int pid;
599f8952a83Seric 	int mpvect[2];
600c579ef51Seric 	int rpvect[2];
6015dfc646bSeric 	FILE *mfile;
602c579ef51Seric 	FILE *rfile;
6035dfc646bSeric 	extern FILE *fdopen();
6045dfc646bSeric 
6055dfc646bSeric # ifdef DEBUG
6066ef48975Seric 	if (tTd(11, 1))
6075dfc646bSeric 	{
608c579ef51Seric 		printf("openmailer:\n");
6095dfc646bSeric 		printav(pvp);
6105dfc646bSeric 	}
6115dfc646bSeric # endif DEBUG
61235490626Seric 	errno = 0;
6135dfc646bSeric 
61433db8731Seric # ifdef DAEMON
61533db8731Seric 	/*
61633db8731Seric 	**  Deal with the special case of mail handled through an IPC
61733db8731Seric 	**  connection.
61833db8731Seric 	**	In this case we don't actually fork.  We must be
61933db8731Seric 	**	running SMTP for this to work.  We will return a
62033db8731Seric 	**	zero pid to indicate that we are running IPC.
62133db8731Seric 	*/
62233db8731Seric 
62333db8731Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
62433db8731Seric 	{
62533db8731Seric 		register int i;
6261277f9a8Seric 		register u_short port;
62733db8731Seric 
62833db8731Seric 		if (!clever)
62933db8731Seric 			syserr("non-clever IPC");
63093b6e3cfSeric 		if (pvp[2] != NULL)
6311277f9a8Seric 			port = atoi(pvp[2]);
63293b6e3cfSeric 		else
6331277f9a8Seric 			port = 0;
6341277f9a8Seric 		i = makeconnection(pvp[1], port, pmfile, prfile);
63533db8731Seric 		if (i != EX_OK)
636ed854c7bSeric 		{
637ed854c7bSeric 			ExitStat = i;
63833db8731Seric 			return (-1);
639ed854c7bSeric 		}
64033db8731Seric 		else
64133db8731Seric 			return (0);
64233db8731Seric 	}
64333db8731Seric # endif DAEMON
64433db8731Seric 
6456328bdf7Seric 	/* create a pipe to shove the mail through */
646f8952a83Seric 	if (pipe(mpvect) < 0)
64725a99e2eSeric 	{
648c579ef51Seric 		syserr("pipe (to mailer)");
64925a99e2eSeric 		return (-1);
65025a99e2eSeric 	}
651c579ef51Seric 
6522c7e1b8dSeric # ifdef SMTP
653c579ef51Seric 	/* if this mailer speaks smtp, create a return pipe */
654c579ef51Seric 	if (clever && pipe(rpvect) < 0)
655c579ef51Seric 	{
656c579ef51Seric 		syserr("pipe (from mailer)");
657c579ef51Seric 		(void) close(mpvect[0]);
658c579ef51Seric 		(void) close(mpvect[1]);
659c579ef51Seric 		return (-1);
660c579ef51Seric 	}
6612c7e1b8dSeric # endif SMTP
662c579ef51Seric 
66333db8731Seric 	/*
66433db8731Seric 	**  Actually fork the mailer process.
66533db8731Seric 	**	DOFORK is clever about retrying.
66633db8731Seric 	*/
66733db8731Seric 
6686ef48975Seric 	(void) fflush(Xscript);				/* for debugging */
66932d19d43Seric 	DOFORK(XFORK);
670f129ec7dSeric 	/* pid is set by DOFORK */
67125a99e2eSeric 	if (pid < 0)
67225a99e2eSeric 	{
67333db8731Seric 		/* failure */
67425a99e2eSeric 		syserr("Cannot fork");
675f8952a83Seric 		(void) close(mpvect[0]);
676f8952a83Seric 		(void) close(mpvect[1]);
677c579ef51Seric 		if (clever)
678c579ef51Seric 		{
679c579ef51Seric 			(void) close(rpvect[0]);
680c579ef51Seric 			(void) close(rpvect[1]);
681c579ef51Seric 		}
68225a99e2eSeric 		return (-1);
68325a99e2eSeric 	}
68425a99e2eSeric 	else if (pid == 0)
68525a99e2eSeric 	{
68625a99e2eSeric 		/* child -- set up input & exec mailer */
68703ab8e55Seric 		/* make diagnostic output be standard output */
6888f0e7860Seric 		(void) signal(SIGINT, SIG_IGN);
6898f0e7860Seric 		(void) signal(SIGHUP, SIG_IGN);
6900984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
691f8952a83Seric 
692f8952a83Seric 		/* arrange to filter standard & diag output of command */
693c579ef51Seric 		if (clever)
694c579ef51Seric 		{
695c579ef51Seric 			(void) close(rpvect[0]);
696c579ef51Seric 			(void) close(1);
697c579ef51Seric 			(void) dup(rpvect[1]);
698c579ef51Seric 			(void) close(rpvect[1]);
699c579ef51Seric 		}
700c579ef51Seric 		else if (OutChannel != stdout)
701f8952a83Seric 		{
702f8952a83Seric 			(void) close(1);
703f8952a83Seric 			(void) dup(fileno(OutChannel));
704f8952a83Seric 		}
705db8841e9Seric 		(void) close(2);
706db8841e9Seric 		(void) dup(1);
707f8952a83Seric 
708f8952a83Seric 		/* arrange to get standard input */
709f8952a83Seric 		(void) close(mpvect[1]);
710db8841e9Seric 		(void) close(0);
711f8952a83Seric 		if (dup(mpvect[0]) < 0)
71225a99e2eSeric 		{
71325a99e2eSeric 			syserr("Cannot dup to zero!");
714a590b978Seric 			_exit(EX_OSERR);
71525a99e2eSeric 		}
716f8952a83Seric 		(void) close(mpvect[0]);
7172a6e0786Seric 		if (!bitset(M_RESTR, m->m_flags))
7180984da9fSeric 		{
719e36b99e2Seric 			if (ctladdr->q_uid == 0)
720e36b99e2Seric 			{
721e36b99e2Seric 				(void) setgid(DefGid);
722e36b99e2Seric 				(void) setuid(DefUid);
723e36b99e2Seric 			}
724e36b99e2Seric 			else
72569f29479Seric 			{
726e36b99e2Seric 				(void) setgid(ctladdr->q_gid);
727e36b99e2Seric 				(void) setuid(ctladdr->q_uid);
72869f29479Seric 			}
7290984da9fSeric 		}
730e374fd72Seric # ifndef VFORK
731e374fd72Seric 		/*
732e374fd72Seric 		**  We have to be careful with vfork - we can't mung up the
733e374fd72Seric 		**  memory but we don't want the mailer to inherit any extra
734e374fd72Seric 		**  open files.  Chances are the mailer won't
735e374fd72Seric 		**  care about an extra file, but then again you never know.
736e374fd72Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
737e374fd72Seric 		**  declared static so we can't.  But if we fclose(pwf), which
738e374fd72Seric 		**  is what endpwent does, it closes it in the parent too and
739e374fd72Seric 		**  the next getpwnam will be slower.  If you have a weird
740e374fd72Seric 		**  mailer that chokes on the extra file you should do the
741e374fd72Seric 		**  endpwent().
742e374fd72Seric 		**
743e374fd72Seric 		**  Similar comments apply to log.  However, openlog is
744e374fd72Seric 		**  clever enough to set the FIOCLEX mode on the file,
745e374fd72Seric 		**  so it will be closed automatically on the exec.
746e374fd72Seric 		*/
747e374fd72Seric 
748e374fd72Seric 		endpwent();
74925a99e2eSeric # ifdef LOG
750f9fe028fSeric 		closelog();
75125a99e2eSeric # endif LOG
752e374fd72Seric # endif VFORK
75333db8731Seric 
75433db8731Seric 		/* try to execute the mailer */
75525a99e2eSeric 		execv(m->m_mailer, pvp);
75633db8731Seric 
75725a99e2eSeric 		/* syserr fails because log is closed */
75825a99e2eSeric 		/* syserr("Cannot exec %s", m->m_mailer); */
75932d19d43Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
760db8841e9Seric 		(void) fflush(stdout);
761a590b978Seric 		_exit(EX_UNAVAILABLE);
76225a99e2eSeric 	}
76325a99e2eSeric 
764f8952a83Seric 	/*
765c579ef51Seric 	**  Set up return value.
766f8952a83Seric 	*/
767f8952a83Seric 
768f8952a83Seric 	(void) close(mpvect[0]);
769f8952a83Seric 	mfile = fdopen(mpvect[1], "w");
770c579ef51Seric 	if (clever)
77125a99e2eSeric 	{
772c579ef51Seric 		(void) close(rpvect[1]);
773c579ef51Seric 		rfile = fdopen(rpvect[0], "r");
77425a99e2eSeric 	}
775c579ef51Seric 
776c579ef51Seric 	*pmfile = mfile;
777c579ef51Seric 	*prfile = rfile;
778c579ef51Seric 
779c579ef51Seric 	return (pid);
78025a99e2eSeric }
78125a99e2eSeric /*
78225a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
78325a99e2eSeric **
78425a99e2eSeric **	Parameters:
78525a99e2eSeric **		stat -- the status code from the mailer (high byte
78625a99e2eSeric **			only; core dumps must have been taken care of
78725a99e2eSeric **			already).
78825a99e2eSeric **		force -- if set, force an error message output, even
78925a99e2eSeric **			if the mailer seems to like to print its own
79025a99e2eSeric **			messages.
79125a99e2eSeric **		m -- the mailer descriptor for this mailer.
79225a99e2eSeric **
79325a99e2eSeric **	Returns:
794db8841e9Seric **		none.
79525a99e2eSeric **
79625a99e2eSeric **	Side Effects:
797c1f9df2cSeric **		Errors may be incremented.
79825a99e2eSeric **		ExitStat may be set.
79925a99e2eSeric */
80025a99e2eSeric 
80125a99e2eSeric giveresponse(stat, force, m)
80225a99e2eSeric 	int stat;
8034a3f06b2Seric 	bool force;
80425a99e2eSeric 	register struct mailer *m;
80525a99e2eSeric {
80625a99e2eSeric 	register char *statmsg;
80725a99e2eSeric 	extern char *SysExMsg[];
80825a99e2eSeric 	register int i;
80925a99e2eSeric 	extern int N_SysEx;
81029dd97a3Seric 	char buf[30];
81125a99e2eSeric 
81213bbc08cSeric 	/*
81313bbc08cSeric 	**  Compute status message from code.
81413bbc08cSeric 	*/
81513bbc08cSeric 
81625a99e2eSeric 	i = stat - EX__BASE;
81725a99e2eSeric 	if (i < 0 || i > N_SysEx)
81825a99e2eSeric 		statmsg = NULL;
81925a99e2eSeric 	else
82025a99e2eSeric 		statmsg = SysExMsg[i];
82125a99e2eSeric 	if (stat == 0)
822c38ba59cSeric 	{
8235826d9d3Seric 		statmsg = "250 sent";
8245826d9d3Seric 		message(Arpa_Info, &statmsg[4]);
825c38ba59cSeric 	}
826c77d1c25Seric 	else if (stat == EX_TEMPFAIL)
827c77d1c25Seric 	{
8280d4209fdSeric 		message(Arpa_Info, "deferred");
829c77d1c25Seric 	}
83025a99e2eSeric 	else
83125a99e2eSeric 	{
832c1f9df2cSeric 		Errors++;
833ed854c7bSeric 		FatalErrors = TRUE;
83425a99e2eSeric 		if (statmsg == NULL && m->m_badstat != 0)
83525a99e2eSeric 		{
83625a99e2eSeric 			stat = m->m_badstat;
83725a99e2eSeric 			i = stat - EX__BASE;
83825a99e2eSeric # ifdef DEBUG
83925a99e2eSeric 			if (i < 0 || i >= N_SysEx)
84025a99e2eSeric 				syserr("Bad m_badstat %d", stat);
84125a99e2eSeric 			else
84225a99e2eSeric # endif DEBUG
84325a99e2eSeric 				statmsg = SysExMsg[i];
84425a99e2eSeric 		}
84525a99e2eSeric 		if (statmsg == NULL)
84625a99e2eSeric 			usrerr("unknown mailer response %d", stat);
847c38ba59cSeric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
8485826d9d3Seric 			usrerr(statmsg);
849793d9f34Seric 		else
8505826d9d3Seric 			fprintf(Xscript, "%s\n", &statmsg[4]);
85125a99e2eSeric 	}
85225a99e2eSeric 
85325a99e2eSeric 	/*
85425a99e2eSeric 	**  Final cleanup.
85525a99e2eSeric 	**	Log a record of the transaction.  Compute the new
85625a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
85725a99e2eSeric 	**	that.
85825a99e2eSeric 	*/
85925a99e2eSeric 
86025a99e2eSeric 	if (statmsg == NULL)
86129dd97a3Seric 	{
8625826d9d3Seric 		(void) sprintf(buf, "554 error %d", stat);
86329dd97a3Seric 		statmsg = buf;
86429dd97a3Seric 	}
86529dd97a3Seric 
86629dd97a3Seric # ifdef LOG
86761f5a1d4Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
8685cf56be3Seric 	{
8695cf56be3Seric 		extern char *pintvl();
8705cf56be3Seric 
8715cf56be3Seric 		syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id,
872b20f17b5Seric 		       CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE),
8735826d9d3Seric 		       &statmsg[4]);
8745cf56be3Seric 	}
87525a99e2eSeric # endif LOG
876c77d1c25Seric 	if (stat != EX_TEMPFAIL)
877243921eeSeric 		setstat(stat);
87825a99e2eSeric }
87925a99e2eSeric /*
88051552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
88125a99e2eSeric **
88251552439Seric **	This can be made an arbitrary message separator by changing $l
88351552439Seric **
88451552439Seric **	One of the ugliest hacks seen by human eyes is
88551552439Seric **	contained herein: UUCP wants those stupid
88651552439Seric **	"remote from <host>" lines.  Why oh why does a
88751552439Seric **	well-meaning programmer such as myself have to
88851552439Seric **	deal with this kind of antique garbage????
88925a99e2eSeric **
89025a99e2eSeric **	Parameters:
89151552439Seric **		fp -- the file to output to.
89251552439Seric **		m -- the mailer describing this entry.
89325a99e2eSeric **
89425a99e2eSeric **	Returns:
89551552439Seric **		none
89625a99e2eSeric **
89725a99e2eSeric **	Side Effects:
89851552439Seric **		outputs some text to fp.
89925a99e2eSeric */
90025a99e2eSeric 
90151552439Seric putfromline(fp, m)
90251552439Seric 	register FILE *fp;
90351552439Seric 	register MAILER *m;
90425a99e2eSeric {
90551552439Seric 	char buf[MAXLINE];
90625a99e2eSeric 
90751552439Seric 	if (bitset(M_NHDR, m->m_flags))
90851552439Seric 		return;
90913bbc08cSeric 
9102c7e1b8dSeric # ifdef UGLYUUCP
911a49f24c0Seric 	if (bitset(M_UGLYUUCP, m->m_flags))
91274b6e67bSeric 	{
91374b6e67bSeric 		extern char *macvalue();
91474b6e67bSeric 		char *sys = macvalue('g');
91574b6e67bSeric 		char *bang = index(sys, '!');
91674b6e67bSeric 
91774b6e67bSeric 		if (bang == NULL)
91874b6e67bSeric 			syserr("No ! in UUCP! (%s)", sys);
91974b6e67bSeric 		else
92074b6e67bSeric 			*bang = '\0';
921518ad6b6Seric 		expand("From $f  $d remote from $g\n", buf,
92251552439Seric 				&buf[sizeof buf - 1], CurEnv);
92374b6e67bSeric 		*bang = '!';
92474b6e67bSeric 	}
925a36e30c9Seric 	else
9262c7e1b8dSeric # endif UGLYUUCP
92751552439Seric 		expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv);
928d90b8d00Seric 	putline(buf, fp, bitset(M_FULLSMTP, m->m_flags));
929bc6e2962Seric }
930bc6e2962Seric /*
93151552439Seric **  PUTHEADER -- put the header part of a message from the in-core copy
932bc6e2962Seric **
933bc6e2962Seric **	Parameters:
934bc6e2962Seric **		fp -- file to put it on.
935bc6e2962Seric **		m -- mailer to use.
93651552439Seric **		e -- envelope to use.
937bc6e2962Seric **
938bc6e2962Seric **	Returns:
939bc6e2962Seric **		none.
940bc6e2962Seric **
941bc6e2962Seric **	Side Effects:
942bc6e2962Seric **		none.
943bc6e2962Seric */
944bc6e2962Seric 
94551552439Seric putheader(fp, m, e)
946bc6e2962Seric 	register FILE *fp;
947bc6e2962Seric 	register struct mailer *m;
94851552439Seric 	register ENVELOPE *e;
949bc6e2962Seric {
950bc6e2962Seric 	char buf[BUFSIZ];
951bc6e2962Seric 	register HDR *h;
952bc6e2962Seric 	extern char *arpadate();
953bc6e2962Seric 	extern char *capitalize();
954bc6e2962Seric 	extern char *hvalue();
955bc6e2962Seric 	extern bool samefrom();
956d90b8d00Seric 	char obuf[MAXLINE];
957d90b8d00Seric 	register char *obp;
958d90b8d00Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
959bc6e2962Seric 
96051552439Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
9616328bdf7Seric 	{
96213bbc08cSeric 		register char *p;
96313bbc08cSeric 
9649e9163a0Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
965*3bea8136Seric 			continue;
966894de7daSeric 
9678a66d3edSeric 		p = h->h_value;
968*3bea8136Seric 		if (bitset(H_DEFAULT, h->h_flags))
9694ae18d0eSeric 		{
970b8a19582Seric 			/* macro expand value if generated internally */
971*3bea8136Seric 			expand(p, buf, &buf[sizeof buf], e);
9724ae18d0eSeric 			p = buf;
9734ae18d0eSeric 		}
9748a66d3edSeric 		if (p == NULL || *p == '\0')
9758a66d3edSeric 			continue;
9768a66d3edSeric 
977*3bea8136Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
9788a66d3edSeric 		{
979*3bea8136Seric 			/* address field */
980*3bea8136Seric 			commaize(h, p, fp, e->e_oldstyle, m);
9818a66d3edSeric 		}
982*3bea8136Seric 		else
9838a66d3edSeric 		{
984*3bea8136Seric 			/* vanilla header line */
9858a66d3edSeric 			(void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p);
9868a66d3edSeric 			putline(obuf, fp, fullsmtp);
9878a66d3edSeric 		}
988*3bea8136Seric 		h->h_flags |= H_USED;
9898a66d3edSeric 	}
9908a66d3edSeric }
9918a66d3edSeric /*
9928a66d3edSeric **  COMMAIZE -- output a header field, making a comma-translated list.
9938a66d3edSeric **
9948a66d3edSeric **	Parameters:
995*3bea8136Seric **		h -- the header field to output.
996*3bea8136Seric **		p -- the value to put in it.
9978a66d3edSeric **		fp -- file to put it to.
9988a66d3edSeric **		oldstyle -- TRUE if this is an old style header.
9998a66d3edSeric **		m -- a pointer to the mailer descriptor.
10008a66d3edSeric **
10018a66d3edSeric **	Returns:
10028a66d3edSeric **		none.
10038a66d3edSeric **
10048a66d3edSeric **	Side Effects:
10058a66d3edSeric **		outputs "p" to file "fp".
10068a66d3edSeric */
10078a66d3edSeric 
1008*3bea8136Seric commaize(h, p, fp, oldstyle, m)
1009*3bea8136Seric 	register HDR *h;
10108a66d3edSeric 	register char *p;
10118a66d3edSeric 	FILE *fp;
10128a66d3edSeric 	bool oldstyle;
1013*3bea8136Seric 	register MAILER *m;
10148a66d3edSeric {
1015*3bea8136Seric 	register char *obp;
1016*3bea8136Seric 	int opos;
1017*3bea8136Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
10188c2bf25bSeric 	bool firstone = TRUE;
10198a66d3edSeric 	char obuf[MAXLINE];
10208c2bf25bSeric 
10218c2bf25bSeric 	/*
10228c2bf25bSeric 	**  Output the address list translated by the
10238c2bf25bSeric 	**  mailer and with commas.
10248c2bf25bSeric 	*/
10258c2bf25bSeric 
10268a66d3edSeric # ifdef DEBUG
10278a66d3edSeric 	if (tTd(14, 2))
1028*3bea8136Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
10298a66d3edSeric # endif DEBUG
10308a66d3edSeric 
1031d90b8d00Seric 	obp = obuf;
1032*3bea8136Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
1033*3bea8136Seric 	opos = strlen(h->h_field) + 2;
1034d90b8d00Seric 	obp += opos;
1035b8a19582Seric 
1036b8a19582Seric 	/*
1037b8a19582Seric 	**  Run through the list of values.
1038b8a19582Seric 	*/
1039b8a19582Seric 
10408c2bf25bSeric 	while (*p != '\0')
10418c2bf25bSeric 	{
1042b8a19582Seric 		register char *name;
10438c2bf25bSeric 		char savechar;
1044003a5a37Seric 		int commentlevel;
1045003a5a37Seric 		bool inquote;
1046*3bea8136Seric 		extern char *remotename();
10478c2bf25bSeric 
1048b8a19582Seric 		/*
1049b8a19582Seric 		**  Find the end of the name.  New style names
1050b8a19582Seric 		**  end with a comma, old style names end with
1051b8a19582Seric 		**  a space character.  However, spaces do not
1052b8a19582Seric 		**  necessarily delimit an old-style name -- at
1053b8a19582Seric 		**  signs mean keep going.
1054b8a19582Seric 		*/
1055b8a19582Seric 
1056b8a19582Seric 		/* clean up the leading trash in source */
1057b8a19582Seric 		while (*p != '\0' && (isspace(*p) || *p == ','))
1058b8a19582Seric 			p++;
1059b8a19582Seric 		name = p;
1060b8a19582Seric 
1061b8a19582Seric 		/* find end of name */
1062003a5a37Seric 		commentlevel = 0;
1063003a5a37Seric 		inquote = FALSE;
1064003a5a37Seric 		while (*p != '\0' && (*p != ',' || commentlevel > 0 || inquote))
1065bc28c57aSeric 		{
1066bc28c57aSeric 			extern bool isatword();
1067bc28c57aSeric 			char *oldp;
1068bc28c57aSeric 
1069003a5a37Seric 			if (*p == '(')
1070003a5a37Seric 				commentlevel++;
1071003a5a37Seric 			else if (*p == ')' && commentlevel > 0)
1072003a5a37Seric 				commentlevel--;
1073003a5a37Seric 			else if (*p == '"')
1074003a5a37Seric 				inquote = !inquote;
10758a66d3edSeric 			if (!oldstyle || !isspace(*p))
1076bc28c57aSeric 			{
10778c2bf25bSeric 				p++;
1078bc28c57aSeric 				continue;
1079bc28c57aSeric 			}
1080b8a19582Seric 
1081b8a19582Seric 			/* look to see if we have an at sign */
1082bc28c57aSeric 			oldp = p;
1083bc28c57aSeric 			while (*p != '\0' && isspace(*p))
1084bc28c57aSeric 				p++;
1085b8a19582Seric 
1086bc28c57aSeric 			if (*p != '@' && !isatword(p))
1087bc28c57aSeric 			{
1088bc28c57aSeric 				p = oldp;
1089bc28c57aSeric 				break;
1090bc28c57aSeric 			}
1091bc28c57aSeric 			p += *p == '@' ? 1 : 2;
1092bc28c57aSeric 			while (*p != '\0' && isspace(*p))
1093bc28c57aSeric 				p++;
1094bc28c57aSeric 		}
1095b8a19582Seric 		/* at the end of one complete name */
1096b8a19582Seric 
1097b8a19582Seric 		/* strip off trailing white space */
10988a66d3edSeric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
1099b8a19582Seric 			p--;
1100b8a19582Seric 		if (++p == name)
1101b8a19582Seric 			continue;
11028c2bf25bSeric 		savechar = *p;
11038c2bf25bSeric 		*p = '\0';
11048c2bf25bSeric 
11058c2bf25bSeric 		/* translate the name to be relative */
1106*3bea8136Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags));
11078c2bf25bSeric 		if (*name == '\0')
1108b8a19582Seric 		{
1109b8a19582Seric 			*p = savechar;
11108c2bf25bSeric 			continue;
1111b8a19582Seric 		}
11128c2bf25bSeric 
11138c2bf25bSeric 		/* output the name with nice formatting */
11148c2bf25bSeric 		opos += strlen(name);
11158c2bf25bSeric 		if (!firstone)
11168c2bf25bSeric 			opos += 2;
11178c2bf25bSeric 		if (opos > 78 && !firstone)
11188c2bf25bSeric 		{
1119d90b8d00Seric 			(void) sprintf(obp, ",\n");
1120d90b8d00Seric 			putline(obuf, fp, fullsmtp);
1121d90b8d00Seric 			obp = obuf;
1122d90b8d00Seric 			(void) sprintf(obp, "        ");
1123d90b8d00Seric 			obp += strlen(obp);
1124bc213ac6Seric 			opos = 8 + strlen(name);
11258c2bf25bSeric 		}
11268c2bf25bSeric 		else if (!firstone)
1127d90b8d00Seric 		{
1128d90b8d00Seric 			(void) sprintf(obp, ", ");
1129d90b8d00Seric 			obp += 2;
1130d90b8d00Seric 		}
1131d90b8d00Seric 		(void) sprintf(obp, "%s", name);
1132d90b8d00Seric 		obp += strlen(obp);
11338c2bf25bSeric 		firstone = FALSE;
11348c2bf25bSeric 		*p = savechar;
11358c2bf25bSeric 	}
11361277f9a8Seric 	(void) strcpy(obp, "\n");
1137573fa002Seric 	putline(obuf, fp, fullsmtp);
113851552439Seric }
113951552439Seric /*
114051552439Seric **  PUTBODY -- put the body of a message.
114151552439Seric **
114251552439Seric **	Parameters:
114351552439Seric **		fp -- file to output onto.
114451552439Seric **		m -- a mailer descriptor.
114551552439Seric **		xdot -- if set, use SMTP hidden dot algorithm.
114651552439Seric **
114751552439Seric **	Returns:
114851552439Seric **		none.
114951552439Seric **
115051552439Seric **	Side Effects:
115151552439Seric **		The message is written onto fp.
115251552439Seric */
115351552439Seric 
115451552439Seric putbody(fp, m, xdot)
115551552439Seric 	FILE *fp;
115651552439Seric 	struct mailer *m;
115751552439Seric 	bool xdot;
115851552439Seric {
115951552439Seric 	char buf[MAXLINE + 1];
1160d90b8d00Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
116151552439Seric 
116251552439Seric 	/*
116351552439Seric 	**  Output the body of the message
116451552439Seric 	*/
116551552439Seric 
11665e663df1Seric #ifdef lint
11675e663df1Seric 	/* m will be needed later for complete smtp emulation */
11685e663df1Seric 	if (m == NULL)
11695e663df1Seric 		return;
11705e663df1Seric #endif lint
11715e663df1Seric 
117251552439Seric 	if (TempFile != NULL)
117351552439Seric 	{
117451552439Seric 		rewind(TempFile);
117551552439Seric 		buf[0] = '.';
117651552439Seric 		while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL)
1177d90b8d00Seric 			putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp);
117851552439Seric 
117951552439Seric 		if (ferror(TempFile))
118051552439Seric 		{
118151552439Seric 			syserr("putbody: read error");
118251552439Seric 			ExitStat = EX_IOERR;
118351552439Seric 		}
118451552439Seric 	}
118551552439Seric 
118651552439Seric 	(void) fflush(fp);
118751552439Seric 	if (ferror(fp) && errno != EPIPE)
118851552439Seric 	{
118951552439Seric 		syserr("putbody: write error");
119051552439Seric 		ExitStat = EX_IOERR;
119151552439Seric 	}
119251552439Seric 	errno = 0;
119325a99e2eSeric }
119425a99e2eSeric /*
1195bc28c57aSeric **  ISATWORD -- tell if the word we are pointing to is "at".
1196bc28c57aSeric **
1197bc28c57aSeric **	Parameters:
1198bc28c57aSeric **		p -- word to check.
1199bc28c57aSeric **
1200bc28c57aSeric **	Returns:
1201bc28c57aSeric **		TRUE -- if p is the word at.
1202bc28c57aSeric **		FALSE -- otherwise.
1203bc28c57aSeric **
1204bc28c57aSeric **	Side Effects:
1205bc28c57aSeric **		none.
1206bc28c57aSeric */
1207bc28c57aSeric 
1208bc28c57aSeric bool
1209bc28c57aSeric isatword(p)
1210bc28c57aSeric 	register char *p;
1211bc28c57aSeric {
1212bc28c57aSeric 	extern char lower();
1213bc28c57aSeric 
1214bc28c57aSeric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
1215bc28c57aSeric 	    p[2] != '\0' && isspace(p[2]))
1216bc28c57aSeric 		return (TRUE);
1217bc28c57aSeric 	return (FALSE);
1218bc28c57aSeric }
1219bc28c57aSeric /*
122087bd9a02Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
122187bd9a02Seric **
122287bd9a02Seric **	Parameters:
122387bd9a02Seric **		ifrom -- internally generated form of from address.
122487bd9a02Seric **		efrom -- external form of from address.
122587bd9a02Seric **
122687bd9a02Seric **	Returns:
122787bd9a02Seric **		TRUE -- if they convey the same info.
122887bd9a02Seric **		FALSE -- if any information has been lost.
122987bd9a02Seric **
123087bd9a02Seric **	Side Effects:
123187bd9a02Seric **		none.
123287bd9a02Seric */
123387bd9a02Seric 
123487bd9a02Seric bool
123587bd9a02Seric samefrom(ifrom, efrom)
123687bd9a02Seric 	char *ifrom;
123787bd9a02Seric 	char *efrom;
123887bd9a02Seric {
1239894de7daSeric 	register char *p;
1240894de7daSeric 	char buf[MAXNAME + 4];
1241894de7daSeric 
1242894de7daSeric # ifdef DEBUG
12436ef48975Seric 	if (tTd(3, 8))
1244894de7daSeric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
1245894de7daSeric # endif DEBUG
1246894de7daSeric 	if (strcmp(ifrom, efrom) == 0)
1247894de7daSeric 		goto success;
1248894de7daSeric 	p = index(ifrom, '@');
1249894de7daSeric 	if (p == NULL)
1250894de7daSeric 		goto failure;
1251894de7daSeric 	*p = '\0';
12525e663df1Seric 	(void) strcpy(buf, ifrom);
12535e663df1Seric 	(void) strcat(buf, " at ");
1254894de7daSeric 	*p++ = '@';
12555e663df1Seric 	(void) strcat(buf, p);
1256894de7daSeric 	if (strcmp(buf, efrom) == 0)
1257894de7daSeric 		goto success;
1258894de7daSeric 
1259894de7daSeric   failure:
1260894de7daSeric # ifdef DEBUG
12616ef48975Seric 	if (tTd(3, 8))
1262894de7daSeric 		printf("FALSE\n");
1263894de7daSeric # endif DEBUG
1264894de7daSeric 	return (FALSE);
1265894de7daSeric 
1266894de7daSeric   success:
1267894de7daSeric # ifdef DEBUG
12686ef48975Seric 	if (tTd(3, 8))
1269894de7daSeric 		printf("TRUE\n");
1270894de7daSeric # endif DEBUG
1271894de7daSeric 	return (TRUE);
127287bd9a02Seric }
127387bd9a02Seric /*
127425a99e2eSeric **  MAILFILE -- Send a message to a file.
127525a99e2eSeric **
1276f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1277f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1278f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1279f129ec7dSeric **	sendmail runs as root.
1280f129ec7dSeric **
128125a99e2eSeric **	Parameters:
128225a99e2eSeric **		filename -- the name of the file to send to.
12836259796dSeric **		ctladdr -- the controlling address header -- includes
12846259796dSeric **			the userid/groupid to be when sending.
128525a99e2eSeric **
128625a99e2eSeric **	Returns:
128725a99e2eSeric **		The exit code associated with the operation.
128825a99e2eSeric **
128925a99e2eSeric **	Side Effects:
129025a99e2eSeric **		none.
129125a99e2eSeric */
129225a99e2eSeric 
12936259796dSeric mailfile(filename, ctladdr)
129425a99e2eSeric 	char *filename;
12956259796dSeric 	ADDRESS *ctladdr;
129625a99e2eSeric {
129725a99e2eSeric 	register FILE *f;
129832d19d43Seric 	register int pid;
129925a99e2eSeric 
130032d19d43Seric 	/*
130132d19d43Seric 	**  Fork so we can change permissions here.
130232d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
130332d19d43Seric 	**	the complications of calling subroutines, etc.
130432d19d43Seric 	*/
130532d19d43Seric 
130632d19d43Seric 	DOFORK(fork);
130732d19d43Seric 
130832d19d43Seric 	if (pid < 0)
130932d19d43Seric 		return (EX_OSERR);
131032d19d43Seric 	else if (pid == 0)
131132d19d43Seric 	{
131232d19d43Seric 		/* child -- actually write to file */
1313f129ec7dSeric 		struct stat stb;
1314f129ec7dSeric 
13150984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
13160984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
13170984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
1318f129ec7dSeric 		umask(OldUmask);
1319f129ec7dSeric 		if (stat(filename, &stb) < 0)
1320e6e1265fSeric 			stb.st_mode = 0666;
1321f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1322f129ec7dSeric 			exit(EX_CANTCREAT);
132303827b5fSeric 		if (ctladdr == NULL)
13247a941e7aSeric 			ctladdr = &CurEnv->e_from;
1325f129ec7dSeric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
1326e36b99e2Seric 		{
1327e36b99e2Seric 			if (ctladdr->q_uid == 0)
1328e36b99e2Seric 				(void) setgid(DefGid);
1329e36b99e2Seric 			else
13306259796dSeric 				(void) setgid(ctladdr->q_gid);
1331e36b99e2Seric 		}
1332f129ec7dSeric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
1333e36b99e2Seric 		{
1334e36b99e2Seric 			if (ctladdr->q_uid == 0)
1335e36b99e2Seric 				(void) setuid(DefUid);
1336e36b99e2Seric 			else
13376259796dSeric 				(void) setuid(ctladdr->q_uid);
1338e36b99e2Seric 		}
133927628d59Seric 		f = dfopen(filename, "a");
134025a99e2eSeric 		if (f == NULL)
134132d19d43Seric 			exit(EX_CANTCREAT);
134225a99e2eSeric 
134351552439Seric 		putfromline(f, Mailer[1]);
134451552439Seric 		(*CurEnv->e_puthdr)(f, Mailer[1], CurEnv);
1345d90b8d00Seric 		fputs("\n", f);
134651552439Seric 		(*CurEnv->e_putbody)(f, Mailer[1], FALSE);
134725a99e2eSeric 		fputs("\n", f);
1348db8841e9Seric 		(void) fclose(f);
134932d19d43Seric 		(void) fflush(stdout);
1350e36b99e2Seric 
135127628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1352c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
135332d19d43Seric 		exit(EX_OK);
135413bbc08cSeric 		/*NOTREACHED*/
135532d19d43Seric 	}
135632d19d43Seric 	else
135732d19d43Seric 	{
135832d19d43Seric 		/* parent -- wait for exit status */
135932d19d43Seric 		register int i;
136032d19d43Seric 		auto int stat;
136132d19d43Seric 
136232d19d43Seric 		while ((i = wait(&stat)) != pid)
136332d19d43Seric 		{
136432d19d43Seric 			if (i < 0)
136532d19d43Seric 			{
136632d19d43Seric 				stat = EX_OSERR << 8;
136732d19d43Seric 				break;
136832d19d43Seric 			}
136932d19d43Seric 		}
13700984da9fSeric 		if ((stat & 0377) != 0)
13710984da9fSeric 			stat = EX_UNAVAILABLE << 8;
137232d19d43Seric 		return ((stat >> 8) & 0377);
137332d19d43Seric 	}
137425a99e2eSeric }
1375ea4dc939Seric /*
1376ea4dc939Seric **  SENDALL -- actually send all the messages.
1377ea4dc939Seric **
1378ea4dc939Seric **	Parameters:
13790c52a0b3Seric **		e -- the envelope to send.
1380ea4dc939Seric **		verifyonly -- if set, only give verification messages.
1381ea4dc939Seric **
1382ea4dc939Seric **	Returns:
1383ea4dc939Seric **		none.
1384ea4dc939Seric **
1385ea4dc939Seric **	Side Effects:
1386ea4dc939Seric **		Scans the send lists and sends everything it finds.
13870c52a0b3Seric **		Delivers any appropriate error messages.
1388ea4dc939Seric */
1389ea4dc939Seric 
13900c52a0b3Seric sendall(e, verifyonly)
13910c52a0b3Seric 	ENVELOPE *e;
1392ea4dc939Seric 	bool verifyonly;
1393ea4dc939Seric {
1394e77e673fSeric 	register ADDRESS *q;
139514a8ed7aSeric 	bool oldverbose;
1396ea4dc939Seric 
1397772e6e50Seric # ifdef DEBUG
13986ef48975Seric 	if (tTd(13, 2))
1399772e6e50Seric 	{
1400772e6e50Seric 		printf("\nSend Queue:\n");
14010c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1402772e6e50Seric 	}
1403772e6e50Seric # endif DEBUG
1404ea4dc939Seric 
14050c52a0b3Seric 	/*
14060c52a0b3Seric 	**  Run through the list and send everything.
14070c52a0b3Seric 	*/
14080c52a0b3Seric 
140914a8ed7aSeric 	oldverbose = Verbose;
141014a8ed7aSeric 	if (verifyonly)
141114a8ed7aSeric 		Verbose = TRUE;
14120c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1413ea4dc939Seric 	{
1414ea4dc939Seric 		if (verifyonly)
1415ea4dc939Seric 		{
14167a941e7aSeric 			CurEnv->e_to = q->q_paddr;
1417e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1418ea4dc939Seric 				message(Arpa_Info, "deliverable");
1419ea4dc939Seric 		}
1420ea4dc939Seric 		else
142151552439Seric 			(void) deliver(q);
1422ea4dc939Seric 	}
142314a8ed7aSeric 	Verbose = oldverbose;
14240c52a0b3Seric 
14250c52a0b3Seric 	/*
14260c52a0b3Seric 	**  Now run through and check for errors.
14270c52a0b3Seric 	*/
14280c52a0b3Seric 
14290c52a0b3Seric 	if (verifyonly)
14300c52a0b3Seric 		return;
14310c52a0b3Seric 
14320c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14330c52a0b3Seric 	{
14340c52a0b3Seric 		register ADDRESS *qq;
14350c52a0b3Seric 
14360c52a0b3Seric 		if (bitset(QQUEUEUP, q->q_flags))
14370c52a0b3Seric 			e->e_queueup = TRUE;
14380c52a0b3Seric 		if (!bitset(QBADADDR, q->q_flags))
14390c52a0b3Seric 			continue;
14400c52a0b3Seric 
14410c52a0b3Seric 		/* we have an address that failed -- find the parent */
14420c52a0b3Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
14430c52a0b3Seric 		{
14440c52a0b3Seric 			char obuf[MAXNAME + 6];
14450c52a0b3Seric 			extern char *aliaslookup();
14460c52a0b3Seric 
14470c52a0b3Seric 			/* we can only have owners for local addresses */
14480c52a0b3Seric 			if (!bitset(M_LOCAL, qq->q_mailer->m_flags))
14490c52a0b3Seric 				continue;
14500c52a0b3Seric 
14510c52a0b3Seric 			/* see if the owner list exists */
14520c52a0b3Seric 			(void) strcpy(obuf, "owner-");
1453cec031e3Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1454cec031e3Seric 				(void) strcat(obuf, "owner");
1455cec031e3Seric 			else
14560c52a0b3Seric 				(void) strcat(obuf, qq->q_user);
14570c52a0b3Seric 			if (aliaslookup(obuf) == NULL)
14580c52a0b3Seric 				continue;
14590c52a0b3Seric 
14600c52a0b3Seric 			/* owner list exists -- add it to the error queue */
14610c52a0b3Seric 			qq->q_flags &= ~QPRIMARY;
14620c52a0b3Seric 			sendto(obuf, 1, qq, &e->e_errorqueue);
14630c52a0b3Seric 			MailBack = TRUE;
14640c52a0b3Seric 			break;
14650c52a0b3Seric 		}
14660c52a0b3Seric 
14670c52a0b3Seric 		/* if we did not find an owner, send to the sender */
14680c52a0b3Seric 		if (qq == NULL)
14690c52a0b3Seric 			sendto(e->e_from.q_paddr, 1, qq, &e->e_errorqueue);
14700c52a0b3Seric 	}
14710c52a0b3Seric }
14720c52a0b3Seric /*
14730c52a0b3Seric **  CHECKERRORS -- check a queue of addresses and process errors.
14740c52a0b3Seric **
14750c52a0b3Seric **	Parameters:
14760c52a0b3Seric **		e -- the envelope to check.
14770c52a0b3Seric **
14780c52a0b3Seric **	Returns:
14790c52a0b3Seric **		none.
14800c52a0b3Seric **
14810c52a0b3Seric **	Side Effects:
14820c52a0b3Seric **		Arranges to queue all tempfailed messages in q
14830c52a0b3Seric **			or deliver error responses.
14840c52a0b3Seric */
14850c52a0b3Seric 
14860c52a0b3Seric checkerrors(e)
14870c52a0b3Seric 	register ENVELOPE *e;
14880c52a0b3Seric {
1489da91004bSeric 	register ADDRESS *q;
1490da91004bSeric 
14910c52a0b3Seric # ifdef DEBUG
14926ef48975Seric 	if (tTd(4, 1))
14930c52a0b3Seric 	{
1494e1e0dfbfSeric 		printf("\ncheckerrors: FatalErrors %d, errorqueue:\n");
14950c52a0b3Seric 		printaddr(e->e_errorqueue, TRUE);
14960c52a0b3Seric 	}
14970c52a0b3Seric # endif DEBUG
14980c52a0b3Seric 
14990c52a0b3Seric 	/* mail back the transcript on errors */
15000c52a0b3Seric 	if (FatalErrors)
15010c52a0b3Seric 		savemail();
15020c52a0b3Seric 
15030c52a0b3Seric 	/* queue up anything laying around */
15045cf56be3Seric 	if (e->e_dontqueue)
15055cf56be3Seric 		return;
1506da91004bSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1507da91004bSeric 	{
1508da91004bSeric 		if (bitset(QQUEUEUP, q->q_flags))
15090c52a0b3Seric 		{
15100c52a0b3Seric # ifdef QUEUE
15110c52a0b3Seric 			queueup(e, FALSE);
15120c52a0b3Seric # else QUEUE
1513da91004bSeric 			syserr("checkerrors: trying to queue %s", e->e_df);
15140c52a0b3Seric # endif QUEUE
1515da91004bSeric 			break;
1516da91004bSeric 		}
15170c52a0b3Seric 	}
1518ea4dc939Seric }
1519