125a99e2eSeric # include <signal.h>
254aa2b0fSeric # include <errno.h>
3b20b3270Seric # include "sendmail.h"
4c77d1c25Seric # include <sys/stat.h>
525a99e2eSeric 
6*6ef48975Seric SCCSID(@(#)deliver.c	3.96		08/08/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 */
385dfc646bSeric 	register int i;
392a6e0786Seric 	extern bool checkcompat();
405dfc646bSeric 	char *pv[MAXPV+1];
4178442df3Seric 	char tobuf[MAXLINE];		/* text line of to people */
425dfc646bSeric 	char buf[MAXNAME];
436259796dSeric 	ADDRESS *ctladdr;
446259796dSeric 	extern ADDRESS *getctladdr();
4578442df3Seric 	char tfrombuf[MAXNAME];		/* translated from person */
4678442df3Seric 	extern char **prescan();
47c77d1c25Seric 	register ADDRESS *to = firstto;
48c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
49772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
50da2935e1Seric 	bool notopen = TRUE;		/* set if connection not quite open */
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
60*6ef48975Seric 	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)
78f3dbc832Seric 			if (!bitset(QDONTSEND, to->q_flags))
79f3dbc832Seric 				to->q_flags |= QQUEUEUP|QDONTSEND;
80f3dbc832Seric 		return (0);
81f3dbc832Seric 	}
82f3dbc832Seric 
8325a99e2eSeric 	/*
845dfc646bSeric 	**  Do initial argv setup.
855dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
865dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
875dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
885dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
895dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
90f3dbc832Seric 	**		We rewrite the from address here, being careful
91f3dbc832Seric 	**		to also rewrite it again using ruleset 2 to
92f3dbc832Seric 	**		eliminate redundancies.
935dfc646bSeric 	*/
945dfc646bSeric 
9578442df3Seric 	/* rewrite from address, using rewriting rules */
9651552439Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
9778442df3Seric 	mvp = prescan(buf, '\0');
9878442df3Seric 	if (mvp == NULL)
9978442df3Seric 	{
10078442df3Seric 		syserr("bad mailer from translate \"%s\"", buf);
10178442df3Seric 		return (EX_SOFTWARE);
10278442df3Seric 	}
10378442df3Seric 	rewrite(mvp, 2);
10478442df3Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
10578442df3Seric 
10678442df3Seric 	define('g', tfrombuf);		/* translated sender address */
1075dfc646bSeric 	define('h', host);		/* to host */
1085dfc646bSeric 	Errors = 0;
1095dfc646bSeric 	pvp = pv;
1105dfc646bSeric 	*pvp++ = m->m_argv[0];
1115dfc646bSeric 
1125dfc646bSeric 	/* insert -f or -r flag as appropriate */
1135dfc646bSeric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1145dfc646bSeric 	{
1155dfc646bSeric 		if (bitset(M_FOPT, m->m_flags))
1165dfc646bSeric 			*pvp++ = "-f";
1175dfc646bSeric 		else
1185dfc646bSeric 			*pvp++ = "-r";
11951552439Seric 		expand("$g", buf, &buf[sizeof buf - 1], CurEnv);
1205dfc646bSeric 		*pvp++ = newstr(buf);
1215dfc646bSeric 	}
1225dfc646bSeric 
1235dfc646bSeric 	/*
1245dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1255dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1265dfc646bSeric 	**  be one of these, and there are only a few more slots
1275dfc646bSeric 	**  in the pv after it.
1285dfc646bSeric 	*/
1295dfc646bSeric 
1305dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1315dfc646bSeric 	{
1325dfc646bSeric 		while ((p = index(p, '$')) != NULL)
1335dfc646bSeric 			if (*++p == 'u')
1345dfc646bSeric 				break;
1355dfc646bSeric 		if (p != NULL)
1365dfc646bSeric 			break;
1375dfc646bSeric 
1385dfc646bSeric 		/* this entry is safe -- go ahead and process it */
13951552439Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
1405dfc646bSeric 		*pvp++ = newstr(buf);
1415dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1425dfc646bSeric 		{
1435dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1445dfc646bSeric 			return (-1);
1455dfc646bSeric 		}
1465dfc646bSeric 	}
147c579ef51Seric 
14833db8731Seric 	/*
14933db8731Seric 	**  If we have no substitution for the user name in the argument
15033db8731Seric 	**  list, we know that we must supply the names otherwise -- and
15133db8731Seric 	**  SMTP is the answer!!
15233db8731Seric 	*/
15333db8731Seric 
1545dfc646bSeric 	if (*mvp == NULL)
155c579ef51Seric 	{
156c579ef51Seric 		/* running SMTP */
1572c7e1b8dSeric # ifdef SMTP
158c579ef51Seric 		clever = TRUE;
159c579ef51Seric 		*pvp = NULL;
1602c7e1b8dSeric # else SMTP
16133db8731Seric 		/* oops!  we don't implement SMTP */
1622c7e1b8dSeric 		syserr("SMTP style mailer");
1632c7e1b8dSeric 		return (EX_SOFTWARE);
1642c7e1b8dSeric # endif SMTP
165c579ef51Seric 	}
1665dfc646bSeric 
1675dfc646bSeric 	/*
1685dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
1695dfc646bSeric 	**  run through our address list and append all the addresses
1705dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
1715dfc646bSeric 	**  always send another copy later.
1725dfc646bSeric 	*/
1735dfc646bSeric 
1745dfc646bSeric 	tobuf[0] = '\0';
1757a941e7aSeric 	CurEnv->e_to = tobuf;
1766259796dSeric 	ctladdr = NULL;
1775dfc646bSeric 	for (; to != NULL; to = to->q_next)
1785dfc646bSeric 	{
1795dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
180bea22b26Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1815dfc646bSeric 			break;
1825dfc646bSeric 
1835dfc646bSeric 		/* if already sent or not for this host, don't send */
184da2935e1Seric 		if (bitset(QDONTSEND, to->q_flags) ||
185da2935e1Seric 		    strcmp(to->q_host, host) != 0 ||
186da2935e1Seric 		    to->q_mailer != firstto->q_mailer)
1875dfc646bSeric 			continue;
1886259796dSeric 
189772e6e50Seric # ifdef DEBUG
190*6ef48975Seric 		if (tTd(10, 1))
191772e6e50Seric 		{
192772e6e50Seric 			printf("\nsend to ");
193772e6e50Seric 			printaddr(to, FALSE);
194772e6e50Seric 		}
195772e6e50Seric # endif DEBUG
196772e6e50Seric 
1976259796dSeric 		/* compute effective uid/gid when sending */
1987da1035fSeric 		if (to->q_mailer == ProgMailer)
1996259796dSeric 			ctladdr = getctladdr(to);
2006259796dSeric 
2015dfc646bSeric 		user = to->q_user;
2027a941e7aSeric 		CurEnv->e_to = to->q_paddr;
2035dfc646bSeric 		to->q_flags |= QDONTSEND;
2045dfc646bSeric 
2055dfc646bSeric 		/*
2065dfc646bSeric 		**  Check to see that these people are allowed to
2075dfc646bSeric 		**  talk to each other.
2082a6e0786Seric 		*/
2092a6e0786Seric 
2102a6e0786Seric 		if (!checkcompat(to))
2115dfc646bSeric 		{
2125dfc646bSeric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
2135dfc646bSeric 			continue;
2145dfc646bSeric 		}
2152a6e0786Seric 
2162a6e0786Seric 		/*
2179ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2189ec9501bSeric 		**	about them.
21925a99e2eSeric 		*/
22025a99e2eSeric 
2212a6e0786Seric 		if (bitset(M_STRIPQ, m->m_flags))
22225a99e2eSeric 		{
2239ec9501bSeric 			stripquotes(user, TRUE);
2249ec9501bSeric 			stripquotes(host, TRUE);
2259ec9501bSeric 		}
2269ec9501bSeric 		else
2279ec9501bSeric 		{
2289ec9501bSeric 			stripquotes(user, FALSE);
2299ec9501bSeric 			stripquotes(host, FALSE);
23025a99e2eSeric 		}
23125a99e2eSeric 
23225a99e2eSeric 		/*
233da2935e1Seric 		**  Do initial connection setup if needed.
234da2935e1Seric 		*/
235da2935e1Seric 
236da2935e1Seric 		if (notopen)
237da2935e1Seric 		{
238da2935e1Seric 			message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
239da2935e1Seric # ifdef SMTP
240da2935e1Seric 			if (clever)
241da2935e1Seric 			{
242da2935e1Seric 				/* send the initial SMTP protocol */
243da2935e1Seric 				i = smtpinit(m, pv, (ADDRESS *) NULL);
244da2935e1Seric 			}
245da2935e1Seric # ifdef SMTP
246da2935e1Seric 			notopen = FALSE;
247da2935e1Seric 		}
248da2935e1Seric 
249da2935e1Seric 		/*
250508daeccSeric 		**  Pass it to the other host if we are running SMTP.
251508daeccSeric 		*/
252508daeccSeric 
253508daeccSeric 		if (clever)
254508daeccSeric 		{
255508daeccSeric # ifdef SMTP
256508daeccSeric 			i = smtprcpt(to);
257508daeccSeric 			if (i != EX_OK)
258508daeccSeric 			{
259508daeccSeric # ifdef QUEUE
260508daeccSeric 				if (i == EX_TEMPFAIL)
261508daeccSeric 					to->q_flags |= QQUEUEUP;
262508daeccSeric 				else
263508daeccSeric # endif QUEUE
264508daeccSeric 					to->q_flags |= QBADADDR;
265508daeccSeric 				giveresponse(i, TRUE, m);
266508daeccSeric 			}
267508daeccSeric # else SMTP
268508daeccSeric 			syserr("trying to be clever");
269508daeccSeric # endif SMTP
270508daeccSeric 		}
271508daeccSeric 
272508daeccSeric 		/*
2733efaed6eSeric 		**  If an error message has already been given, don't
2743efaed6eSeric 		**	bother to send to this address.
2753efaed6eSeric 		**
2763efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2773efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2783efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2793efaed6eSeric 		*/
2803efaed6eSeric 
2816cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2823efaed6eSeric 			continue;
2833efaed6eSeric 
284f2fec898Seric 		/* save statistics.... */
2857da1035fSeric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2867a941e7aSeric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
287f2fec898Seric 
2883efaed6eSeric 		/*
28925a99e2eSeric 		**  See if this user name is "special".
29025a99e2eSeric 		**	If the user name has a slash in it, assume that this
29151552439Seric 		**	is a file -- send it off without further ado.  Note
29251552439Seric 		**	that this type of addresses is not processed along
29351552439Seric 		**	with the others, so we fudge on the To person.
29425a99e2eSeric 		*/
29525a99e2eSeric 
2967da1035fSeric 		if (m == LocalMailer)
29725a99e2eSeric 		{
298a49f24c0Seric 			if (user[0] == '/')
29925a99e2eSeric 			{
3006259796dSeric 				i = mailfile(user, getctladdr(to));
30125a99e2eSeric 				giveresponse(i, TRUE, m);
3025dfc646bSeric 				continue;
30325a99e2eSeric 			}
30425a99e2eSeric 		}
30525a99e2eSeric 
30613bbc08cSeric 		/*
30713bbc08cSeric 		**  Address is verified -- add this user to mailer
30813bbc08cSeric 		**  argv, and add it to the print list of recipients.
30913bbc08cSeric 		*/
31013bbc08cSeric 
311508daeccSeric 		/* link together the chain of recipients */
312508daeccSeric 		to->q_tchain = tochain;
313508daeccSeric 		tochain = to;
314508daeccSeric 
3155dfc646bSeric 		/* create list of users for error messages */
3165dfc646bSeric 		if (tobuf[0] != '\0')
317db8841e9Seric 			(void) strcat(tobuf, ",");
318db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
3195dfc646bSeric 		define('u', user);		/* to user */
320c2567733Seric 		define('z', to->q_home);	/* user's home */
3215dfc646bSeric 
322c579ef51Seric 		/*
323508daeccSeric 		**  Expand out this user into argument list.
324c579ef51Seric 		*/
325c579ef51Seric 
326508daeccSeric 		if (!clever)
327c579ef51Seric 		{
32851552439Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3295dfc646bSeric 			*pvp++ = newstr(buf);
3305dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3315dfc646bSeric 			{
3325dfc646bSeric 				/* allow some space for trailing parms */
3335dfc646bSeric 				break;
3345dfc646bSeric 			}
3355dfc646bSeric 		}
336c579ef51Seric 	}
3375dfc646bSeric 
338145b49b1Seric 	/* see if any addresses still exist */
339145b49b1Seric 	if (tobuf[0] == '\0')
340c579ef51Seric 	{
3412c7e1b8dSeric # ifdef SMTP
342c579ef51Seric 		if (clever)
3434a3f06b2Seric 			smtpquit(pv[0], FALSE);
3442c7e1b8dSeric # endif SMTP
3455e663df1Seric 		define('g', (char *) NULL);
346145b49b1Seric 		return (0);
347c579ef51Seric 	}
348145b49b1Seric 
3495dfc646bSeric 	/* print out messages as full list */
3507a941e7aSeric 	CurEnv->e_to = tobuf;
3515dfc646bSeric 
3525dfc646bSeric 	/*
3535dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3545dfc646bSeric 	*/
3555dfc646bSeric 
356c579ef51Seric 	while (!clever && *++mvp != NULL)
3575dfc646bSeric 	{
35851552439Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3595dfc646bSeric 		*pvp++ = newstr(buf);
3605dfc646bSeric 		if (pvp >= &pv[MAXPV])
3615dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
3625dfc646bSeric 	}
3635dfc646bSeric 	*pvp++ = NULL;
3645dfc646bSeric 
36525a99e2eSeric 	/*
36625a99e2eSeric 	**  Call the mailer.
3676328bdf7Seric 	**	The argument vector gets built, pipes
36825a99e2eSeric 	**	are created as necessary, and we fork & exec as
3696328bdf7Seric 	**	appropriate.
370c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
37125a99e2eSeric 	*/
37225a99e2eSeric 
3736259796dSeric 	if (ctladdr == NULL)
3747a941e7aSeric 		ctladdr = &CurEnv->e_from;
3752c7e1b8dSeric # ifdef SMTP
376c579ef51Seric 	if (clever)
377c579ef51Seric 	{
37851552439Seric 		i = smtpfinish(m, CurEnv);
3794a3f06b2Seric 		smtpquit(pv[0], TRUE);
380c579ef51Seric 	}
381c579ef51Seric 	else
3822c7e1b8dSeric # endif SMTP
38351552439Seric 		i = sendoff(m, pv, ctladdr);
3845dfc646bSeric 
385c77d1c25Seric 	/*
386c77d1c25Seric 	**  If we got a temporary failure, arrange to queue the
387c77d1c25Seric 	**  addressees.
388c77d1c25Seric 	*/
389c77d1c25Seric 
3902c7e1b8dSeric # ifdef QUEUE
391c77d1c25Seric 	if (i == EX_TEMPFAIL)
392c77d1c25Seric 	{
393772e6e50Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
394c77d1c25Seric 			to->q_flags |= QQUEUEUP;
395c77d1c25Seric 	}
3962c7e1b8dSeric # endif QUEUE
397c77d1c25Seric 
39835490626Seric 	errno = 0;
3995e663df1Seric 	define('g', (char *) NULL);
4005dfc646bSeric 	return (i);
40125a99e2eSeric }
4025dfc646bSeric /*
40332d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
40432d19d43Seric **
40532d19d43Seric **	This MUST be a macro, since after a vfork we are running
40632d19d43Seric **	two processes on the same stack!!!
40732d19d43Seric **
40832d19d43Seric **	Parameters:
40932d19d43Seric **		none.
41032d19d43Seric **
41132d19d43Seric **	Returns:
41232d19d43Seric **		From a macro???  You've got to be kidding!
41332d19d43Seric **
41432d19d43Seric **	Side Effects:
41532d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
41632d19d43Seric **			pid of child in parent, zero in child.
41732d19d43Seric **			-1 on unrecoverable error.
41832d19d43Seric **
41932d19d43Seric **	Notes:
42032d19d43Seric **		I'm awfully sorry this looks so awful.  That's
42132d19d43Seric **		vfork for you.....
42232d19d43Seric */
42332d19d43Seric 
42432d19d43Seric # define NFORKTRIES	5
42532d19d43Seric # ifdef VFORK
42632d19d43Seric # define XFORK	vfork
42732d19d43Seric # else VFORK
42832d19d43Seric # define XFORK	fork
42932d19d43Seric # endif VFORK
43032d19d43Seric 
43132d19d43Seric # define DOFORK(fORKfN) \
43232d19d43Seric {\
43332d19d43Seric 	register int i;\
43432d19d43Seric \
43532d19d43Seric 	for (i = NFORKTRIES; i-- > 0; )\
43632d19d43Seric 	{\
43732d19d43Seric 		pid = fORKfN();\
43832d19d43Seric 		if (pid >= 0)\
43932d19d43Seric 			break;\
4405e663df1Seric 		sleep(NFORKTRIES - i);\
44132d19d43Seric 	}\
44232d19d43Seric }
44332d19d43Seric /*
4442ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
4452ed72599Seric **
4462ed72599Seric **	Parameters:
4472ed72599Seric **		none.
4482ed72599Seric **
4492ed72599Seric **	Returns:
4502ed72599Seric **		pid of child in parent.
4512ed72599Seric **		zero in child.
4522ed72599Seric **		-1 on error.
4532ed72599Seric **
4542ed72599Seric **	Side Effects:
4552ed72599Seric **		returns twice, once in parent and once in child.
4562ed72599Seric */
4572ed72599Seric 
4582ed72599Seric dofork()
4592ed72599Seric {
4602ed72599Seric 	register int pid;
4612ed72599Seric 
4622ed72599Seric 	DOFORK(fork);
4632ed72599Seric 	return (pid);
4642ed72599Seric }
4652ed72599Seric /*
4665dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
4675dfc646bSeric **
4685dfc646bSeric **	Parameters:
4695dfc646bSeric **		m -- mailer descriptor.
4705dfc646bSeric **		pvp -- parameter vector to send to it.
4716259796dSeric **		ctladdr -- an address pointer controlling the
4726259796dSeric **			user/groupid etc. of the mailer.
4735dfc646bSeric **
4745dfc646bSeric **	Returns:
4755dfc646bSeric **		exit status of mailer.
4765dfc646bSeric **
4775dfc646bSeric **	Side Effects:
4785dfc646bSeric **		none.
4795dfc646bSeric */
4805dfc646bSeric 
48151552439Seric sendoff(m, pvp, ctladdr)
4825dfc646bSeric 	struct mailer *m;
4835dfc646bSeric 	char **pvp;
4846259796dSeric 	ADDRESS *ctladdr;
4855dfc646bSeric {
486c579ef51Seric 	auto FILE *mfile;
487c579ef51Seric 	auto FILE *rfile;
4885dfc646bSeric 	register int i;
489c579ef51Seric 	int pid;
490c579ef51Seric 
491c579ef51Seric 	/*
492c579ef51Seric 	**  Create connection to mailer.
493c579ef51Seric 	*/
494c579ef51Seric 
495c579ef51Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
496c579ef51Seric 	if (pid < 0)
497c579ef51Seric 		return (-1);
498c579ef51Seric 
499c579ef51Seric 	/*
500c579ef51Seric 	**  Format and send message.
501c579ef51Seric 	*/
502c579ef51Seric 
503c579ef51Seric 	(void) signal(SIGPIPE, SIG_IGN);
50451552439Seric 	putfromline(mfile, m);
50551552439Seric 	(*CurEnv->e_puthdr)(mfile, m, CurEnv);
50651552439Seric 	fprintf(mfile, "\n");
50751552439Seric 	(*CurEnv->e_putbody)(mfile, m, FALSE);
508c579ef51Seric 	(void) fclose(mfile);
509c579ef51Seric 
510c579ef51Seric 	i = endmailer(pid, pvp[0]);
511c579ef51Seric 	giveresponse(i, TRUE, m);
512bc6e2962Seric 
513bc6e2962Seric 	/* arrange a return receipt if requested */
5147a941e7aSeric 	if (CurEnv->e_retreceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK)
515bc6e2962Seric 	{
5167a941e7aSeric 		CurEnv->e_sendreceipt = TRUE;
5177a941e7aSeric 		fprintf(Xscript, "%s... successfully delivered\n", CurEnv->e_to);
518bc6e2962Seric 		/* do we want to send back more info? */
519bc6e2962Seric 	}
520bc6e2962Seric 
521c579ef51Seric 	return (i);
522c579ef51Seric }
523c579ef51Seric /*
524c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
525c579ef51Seric **
526c579ef51Seric **	We should never get fatal errors (e.g., segmentation
527c579ef51Seric **	violation), so we report those specially.  For other
528c579ef51Seric **	errors, we choose a status message (into statmsg),
529c579ef51Seric **	and if it represents an error, we print it.
530c579ef51Seric **
531c579ef51Seric **	Parameters:
532c579ef51Seric **		pid -- pid of mailer.
533c579ef51Seric **		name -- name of mailer (for error messages).
534c579ef51Seric **
535c579ef51Seric **	Returns:
536c579ef51Seric **		exit code of mailer.
537c579ef51Seric **
538c579ef51Seric **	Side Effects:
539c579ef51Seric **		none.
540c579ef51Seric */
541c579ef51Seric 
542c579ef51Seric endmailer(pid, name)
543c579ef51Seric 	int pid;
544c579ef51Seric 	char *name;
545c579ef51Seric {
546c579ef51Seric 	register int i;
547c579ef51Seric 	auto int st;
548c579ef51Seric 
54933db8731Seric 	/* in the IPC case there is nothing to wait for */
55033db8731Seric 	if (pid == 0)
55133db8731Seric 		return (EX_OK);
55233db8731Seric 
55333db8731Seric 	/* wait for the mailer process to die and collect status */
554c579ef51Seric 	while ((i = wait(&st)) > 0 && i != pid)
555c579ef51Seric 		continue;
556c579ef51Seric 	if (i < 0)
557c579ef51Seric 	{
558c579ef51Seric 		syserr("wait");
559c579ef51Seric 		return (-1);
560c579ef51Seric 	}
56133db8731Seric 
56233db8731Seric 	/* see if it died a horrid death */
563c579ef51Seric 	if ((st & 0377) != 0)
564c579ef51Seric 	{
565c579ef51Seric 		syserr("%s: stat %o", name, st);
566c579ef51Seric 		ExitStat = EX_UNAVAILABLE;
567c579ef51Seric 		return (-1);
568c579ef51Seric 	}
56933db8731Seric 
57033db8731Seric 	/* normal death -- return status */
571c579ef51Seric 	i = (st >> 8) & 0377;
572c579ef51Seric 	return (i);
573c579ef51Seric }
574c579ef51Seric /*
575c579ef51Seric **  OPENMAILER -- open connection to mailer.
576c579ef51Seric **
577c579ef51Seric **	Parameters:
578c579ef51Seric **		m -- mailer descriptor.
579c579ef51Seric **		pvp -- parameter vector to pass to mailer.
580c579ef51Seric **		ctladdr -- controlling address for user.
581c579ef51Seric **		clever -- create a full duplex connection.
582c579ef51Seric **		pmfile -- pointer to mfile (to mailer) connection.
583c579ef51Seric **		prfile -- pointer to rfile (from mailer) connection.
584c579ef51Seric **
585c579ef51Seric **	Returns:
58633db8731Seric **		pid of mailer ( > 0 ).
587c579ef51Seric **		-1 on error.
58833db8731Seric **		zero on an IPC connection.
589c579ef51Seric **
590c579ef51Seric **	Side Effects:
591c579ef51Seric **		creates a mailer in a subprocess.
592c579ef51Seric */
593c579ef51Seric 
594c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
595c579ef51Seric 	struct mailer *m;
596c579ef51Seric 	char **pvp;
597c579ef51Seric 	ADDRESS *ctladdr;
598c579ef51Seric 	bool clever;
599c579ef51Seric 	FILE **pmfile;
600c579ef51Seric 	FILE **prfile;
601c579ef51Seric {
6025dfc646bSeric 	int pid;
603f8952a83Seric 	int mpvect[2];
604c579ef51Seric 	int rpvect[2];
6055dfc646bSeric 	FILE *mfile;
606c579ef51Seric 	FILE *rfile;
6075dfc646bSeric 	extern FILE *fdopen();
6085dfc646bSeric 
6095dfc646bSeric # ifdef DEBUG
610*6ef48975Seric 	if (tTd(11, 1))
6115dfc646bSeric 	{
612c579ef51Seric 		printf("openmailer:\n");
6135dfc646bSeric 		printav(pvp);
6145dfc646bSeric 	}
6155dfc646bSeric # endif DEBUG
61635490626Seric 	errno = 0;
6175dfc646bSeric 
61833db8731Seric # ifdef DAEMON
61933db8731Seric 	/*
62033db8731Seric 	**  Deal with the special case of mail handled through an IPC
62133db8731Seric 	**  connection.
62233db8731Seric 	**	In this case we don't actually fork.  We must be
62333db8731Seric 	**	running SMTP for this to work.  We will return a
62433db8731Seric 	**	zero pid to indicate that we are running IPC.
62533db8731Seric 	*/
62633db8731Seric 
62733db8731Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
62833db8731Seric 	{
62933db8731Seric 		register int i;
6301277f9a8Seric 		register u_short port;
63133db8731Seric 
63233db8731Seric 		if (!clever)
63333db8731Seric 			syserr("non-clever IPC");
63493b6e3cfSeric 		if (pvp[2] != NULL)
6351277f9a8Seric 			port = atoi(pvp[2]);
63693b6e3cfSeric 		else
6371277f9a8Seric 			port = 0;
6381277f9a8Seric 		i = makeconnection(pvp[1], port, pmfile, prfile);
63933db8731Seric 		if (i != EX_OK)
640ed854c7bSeric 		{
641ed854c7bSeric 			ExitStat = i;
64233db8731Seric 			return (-1);
643ed854c7bSeric 		}
64433db8731Seric 		else
64533db8731Seric 			return (0);
64633db8731Seric 	}
64733db8731Seric # endif DAEMON
64833db8731Seric 
6496328bdf7Seric 	/* create a pipe to shove the mail through */
650f8952a83Seric 	if (pipe(mpvect) < 0)
65125a99e2eSeric 	{
652c579ef51Seric 		syserr("pipe (to mailer)");
65325a99e2eSeric 		return (-1);
65425a99e2eSeric 	}
655c579ef51Seric 
6562c7e1b8dSeric # ifdef SMTP
657c579ef51Seric 	/* if this mailer speaks smtp, create a return pipe */
658c579ef51Seric 	if (clever && pipe(rpvect) < 0)
659c579ef51Seric 	{
660c579ef51Seric 		syserr("pipe (from mailer)");
661c579ef51Seric 		(void) close(mpvect[0]);
662c579ef51Seric 		(void) close(mpvect[1]);
663c579ef51Seric 		return (-1);
664c579ef51Seric 	}
6652c7e1b8dSeric # endif SMTP
666c579ef51Seric 
66733db8731Seric 	/*
66833db8731Seric 	**  Actually fork the mailer process.
66933db8731Seric 	**	DOFORK is clever about retrying.
67033db8731Seric 	*/
67133db8731Seric 
672*6ef48975Seric 	(void) fflush(Xscript);				/* for debugging */
67332d19d43Seric 	DOFORK(XFORK);
674f129ec7dSeric 	/* pid is set by DOFORK */
67525a99e2eSeric 	if (pid < 0)
67625a99e2eSeric 	{
67733db8731Seric 		/* failure */
67825a99e2eSeric 		syserr("Cannot fork");
679f8952a83Seric 		(void) close(mpvect[0]);
680f8952a83Seric 		(void) close(mpvect[1]);
681c579ef51Seric 		if (clever)
682c579ef51Seric 		{
683c579ef51Seric 			(void) close(rpvect[0]);
684c579ef51Seric 			(void) close(rpvect[1]);
685c579ef51Seric 		}
68625a99e2eSeric 		return (-1);
68725a99e2eSeric 	}
68825a99e2eSeric 	else if (pid == 0)
68925a99e2eSeric 	{
69025a99e2eSeric 		/* child -- set up input & exec mailer */
69103ab8e55Seric 		/* make diagnostic output be standard output */
6928f0e7860Seric 		(void) signal(SIGINT, SIG_IGN);
6938f0e7860Seric 		(void) signal(SIGHUP, SIG_IGN);
6940984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
695f8952a83Seric 
696f8952a83Seric 		/* arrange to filter standard & diag output of command */
697c579ef51Seric 		if (clever)
698c579ef51Seric 		{
699c579ef51Seric 			(void) close(rpvect[0]);
700c579ef51Seric 			(void) close(1);
701c579ef51Seric 			(void) dup(rpvect[1]);
702c579ef51Seric 			(void) close(rpvect[1]);
703c579ef51Seric 		}
704c579ef51Seric 		else if (OutChannel != stdout)
705f8952a83Seric 		{
706f8952a83Seric 			(void) close(1);
707f8952a83Seric 			(void) dup(fileno(OutChannel));
708f8952a83Seric 		}
709db8841e9Seric 		(void) close(2);
710db8841e9Seric 		(void) dup(1);
711f8952a83Seric 
712f8952a83Seric 		/* arrange to get standard input */
713f8952a83Seric 		(void) close(mpvect[1]);
714db8841e9Seric 		(void) close(0);
715f8952a83Seric 		if (dup(mpvect[0]) < 0)
71625a99e2eSeric 		{
71725a99e2eSeric 			syserr("Cannot dup to zero!");
718a590b978Seric 			_exit(EX_OSERR);
71925a99e2eSeric 		}
720f8952a83Seric 		(void) close(mpvect[0]);
7212a6e0786Seric 		if (!bitset(M_RESTR, m->m_flags))
7220984da9fSeric 		{
723e36b99e2Seric 			if (ctladdr->q_uid == 0)
724e36b99e2Seric 			{
725e36b99e2Seric 				(void) setgid(DefGid);
726e36b99e2Seric 				(void) setuid(DefUid);
727e36b99e2Seric 			}
728e36b99e2Seric 			else
72969f29479Seric 			{
730e36b99e2Seric 				(void) setgid(ctladdr->q_gid);
731e36b99e2Seric 				(void) setuid(ctladdr->q_uid);
73269f29479Seric 			}
7330984da9fSeric 		}
734e374fd72Seric # ifndef VFORK
735e374fd72Seric 		/*
736e374fd72Seric 		**  We have to be careful with vfork - we can't mung up the
737e374fd72Seric 		**  memory but we don't want the mailer to inherit any extra
738e374fd72Seric 		**  open files.  Chances are the mailer won't
739e374fd72Seric 		**  care about an extra file, but then again you never know.
740e374fd72Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
741e374fd72Seric 		**  declared static so we can't.  But if we fclose(pwf), which
742e374fd72Seric 		**  is what endpwent does, it closes it in the parent too and
743e374fd72Seric 		**  the next getpwnam will be slower.  If you have a weird
744e374fd72Seric 		**  mailer that chokes on the extra file you should do the
745e374fd72Seric 		**  endpwent().
746e374fd72Seric 		**
747e374fd72Seric 		**  Similar comments apply to log.  However, openlog is
748e374fd72Seric 		**  clever enough to set the FIOCLEX mode on the file,
749e374fd72Seric 		**  so it will be closed automatically on the exec.
750e374fd72Seric 		*/
751e374fd72Seric 
752e374fd72Seric 		endpwent();
75325a99e2eSeric # ifdef LOG
754f9fe028fSeric 		closelog();
75525a99e2eSeric # endif LOG
756e374fd72Seric # endif VFORK
75733db8731Seric 
75833db8731Seric 		/* try to execute the mailer */
75925a99e2eSeric 		execv(m->m_mailer, pvp);
76033db8731Seric 
76125a99e2eSeric 		/* syserr fails because log is closed */
76225a99e2eSeric 		/* syserr("Cannot exec %s", m->m_mailer); */
76332d19d43Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
764db8841e9Seric 		(void) fflush(stdout);
765a590b978Seric 		_exit(EX_UNAVAILABLE);
76625a99e2eSeric 	}
76725a99e2eSeric 
768f8952a83Seric 	/*
769c579ef51Seric 	**  Set up return value.
770f8952a83Seric 	*/
771f8952a83Seric 
772f8952a83Seric 	(void) close(mpvect[0]);
773f8952a83Seric 	mfile = fdopen(mpvect[1], "w");
774c579ef51Seric 	if (clever)
77525a99e2eSeric 	{
776c579ef51Seric 		(void) close(rpvect[1]);
777c579ef51Seric 		rfile = fdopen(rpvect[0], "r");
77825a99e2eSeric 	}
779c579ef51Seric 
780c579ef51Seric 	*pmfile = mfile;
781c579ef51Seric 	*prfile = rfile;
782c579ef51Seric 
783c579ef51Seric 	return (pid);
78425a99e2eSeric }
78525a99e2eSeric /*
78625a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
78725a99e2eSeric **
78825a99e2eSeric **	Parameters:
78925a99e2eSeric **		stat -- the status code from the mailer (high byte
79025a99e2eSeric **			only; core dumps must have been taken care of
79125a99e2eSeric **			already).
79225a99e2eSeric **		force -- if set, force an error message output, even
79325a99e2eSeric **			if the mailer seems to like to print its own
79425a99e2eSeric **			messages.
79525a99e2eSeric **		m -- the mailer descriptor for this mailer.
79625a99e2eSeric **
79725a99e2eSeric **	Returns:
798db8841e9Seric **		none.
79925a99e2eSeric **
80025a99e2eSeric **	Side Effects:
801c1f9df2cSeric **		Errors may be incremented.
80225a99e2eSeric **		ExitStat may be set.
80325a99e2eSeric */
80425a99e2eSeric 
80525a99e2eSeric giveresponse(stat, force, m)
80625a99e2eSeric 	int stat;
8074a3f06b2Seric 	bool force;
80825a99e2eSeric 	register struct mailer *m;
80925a99e2eSeric {
81025a99e2eSeric 	register char *statmsg;
81125a99e2eSeric 	extern char *SysExMsg[];
81225a99e2eSeric 	register int i;
81325a99e2eSeric 	extern int N_SysEx;
81429dd97a3Seric 	char buf[30];
81525a99e2eSeric 
81613bbc08cSeric 	/*
81713bbc08cSeric 	**  Compute status message from code.
81813bbc08cSeric 	*/
81913bbc08cSeric 
82025a99e2eSeric 	i = stat - EX__BASE;
82125a99e2eSeric 	if (i < 0 || i > N_SysEx)
82225a99e2eSeric 		statmsg = NULL;
82325a99e2eSeric 	else
82425a99e2eSeric 		statmsg = SysExMsg[i];
82525a99e2eSeric 	if (stat == 0)
826c38ba59cSeric 	{
8276cbfa739Seric 		if (bitset(M_LOCAL, m->m_flags))
8283efaed6eSeric 			statmsg = "delivered";
8293efaed6eSeric 		else
8303efaed6eSeric 			statmsg = "queued";
831544026c5Seric 		message(Arpa_Info, statmsg);
832c38ba59cSeric 	}
8332c7e1b8dSeric # ifdef QUEUE
834c77d1c25Seric 	else if (stat == EX_TEMPFAIL)
835c77d1c25Seric 	{
836c77d1c25Seric 		message(Arpa_Info, "transmission deferred");
837c77d1c25Seric 	}
8382c7e1b8dSeric # endif QUEUE
83925a99e2eSeric 	else
84025a99e2eSeric 	{
841c1f9df2cSeric 		Errors++;
842ed854c7bSeric 		FatalErrors = TRUE;
84325a99e2eSeric 		if (statmsg == NULL && m->m_badstat != 0)
84425a99e2eSeric 		{
84525a99e2eSeric 			stat = m->m_badstat;
84625a99e2eSeric 			i = stat - EX__BASE;
84725a99e2eSeric # ifdef DEBUG
84825a99e2eSeric 			if (i < 0 || i >= N_SysEx)
84925a99e2eSeric 				syserr("Bad m_badstat %d", stat);
85025a99e2eSeric 			else
85125a99e2eSeric # endif DEBUG
85225a99e2eSeric 				statmsg = SysExMsg[i];
85325a99e2eSeric 		}
85425a99e2eSeric 		if (statmsg == NULL)
85525a99e2eSeric 			usrerr("unknown mailer response %d", stat);
856c38ba59cSeric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
85725a99e2eSeric 			usrerr("%s", statmsg);
858793d9f34Seric 		else
859793d9f34Seric 			fprintf(Xscript, "%s\n", statmsg);
86025a99e2eSeric 	}
86125a99e2eSeric 
86225a99e2eSeric 	/*
86325a99e2eSeric 	**  Final cleanup.
86425a99e2eSeric 	**	Log a record of the transaction.  Compute the new
86525a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
86625a99e2eSeric 	**	that.
86725a99e2eSeric 	*/
86825a99e2eSeric 
86925a99e2eSeric 	if (statmsg == NULL)
87029dd97a3Seric 	{
871db8841e9Seric 		(void) sprintf(buf, "error %d", stat);
87229dd97a3Seric 		statmsg = buf;
87329dd97a3Seric 	}
87429dd97a3Seric 
87529dd97a3Seric # ifdef LOG
876*6ef48975Seric 	if (LogLevel > 1)
877*6ef48975Seric 		syslog(LOG_INFO, "%s: to=%s, stat=%s", MsgId, CurEnv->e_to, statmsg);
87825a99e2eSeric # endif LOG
8792c7e1b8dSeric # ifdef QUEUE
880c77d1c25Seric 	if (stat != EX_TEMPFAIL)
8812c7e1b8dSeric # endif QUEUE
882243921eeSeric 		setstat(stat);
88325a99e2eSeric }
88425a99e2eSeric /*
88551552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
88625a99e2eSeric **
88751552439Seric **	This can be made an arbitrary message separator by changing $l
88851552439Seric **
88951552439Seric **	One of the ugliest hacks seen by human eyes is
89051552439Seric **	contained herein: UUCP wants those stupid
89151552439Seric **	"remote from <host>" lines.  Why oh why does a
89251552439Seric **	well-meaning programmer such as myself have to
89351552439Seric **	deal with this kind of antique garbage????
89425a99e2eSeric **
89525a99e2eSeric **	Parameters:
89651552439Seric **		fp -- the file to output to.
89751552439Seric **		m -- the mailer describing this entry.
89825a99e2eSeric **
89925a99e2eSeric **	Returns:
90051552439Seric **		none
90125a99e2eSeric **
90225a99e2eSeric **	Side Effects:
90351552439Seric **		outputs some text to fp.
90425a99e2eSeric */
90525a99e2eSeric 
90651552439Seric putfromline(fp, m)
90751552439Seric 	register FILE *fp;
90851552439Seric 	register MAILER *m;
90925a99e2eSeric {
91051552439Seric 	char buf[MAXLINE];
91125a99e2eSeric 
91251552439Seric 	if (bitset(M_NHDR, m->m_flags))
91351552439Seric 		return;
91413bbc08cSeric 
9152c7e1b8dSeric # ifdef UGLYUUCP
916a49f24c0Seric 	if (bitset(M_UGLYUUCP, m->m_flags))
91774b6e67bSeric 	{
91874b6e67bSeric 		extern char *macvalue();
91974b6e67bSeric 		char *sys = macvalue('g');
92074b6e67bSeric 		char *bang = index(sys, '!');
92174b6e67bSeric 
92274b6e67bSeric 		if (bang == NULL)
92374b6e67bSeric 			syserr("No ! in UUCP! (%s)", sys);
92474b6e67bSeric 		else
92574b6e67bSeric 			*bang = '\0';
926518ad6b6Seric 		expand("From $f  $d remote from $g\n", buf,
92751552439Seric 				&buf[sizeof buf - 1], CurEnv);
92874b6e67bSeric 		*bang = '!';
92974b6e67bSeric 	}
930a36e30c9Seric 	else
9312c7e1b8dSeric # endif UGLYUUCP
93251552439Seric 		expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv);
933d90b8d00Seric 	putline(buf, fp, bitset(M_FULLSMTP, m->m_flags));
934bc6e2962Seric }
935bc6e2962Seric /*
93651552439Seric **  PUTHEADER -- put the header part of a message from the in-core copy
937bc6e2962Seric **
938bc6e2962Seric **	Parameters:
939bc6e2962Seric **		fp -- file to put it on.
940bc6e2962Seric **		m -- mailer to use.
94151552439Seric **		e -- envelope to use.
942bc6e2962Seric **
943bc6e2962Seric **	Returns:
944bc6e2962Seric **		none.
945bc6e2962Seric **
946bc6e2962Seric **	Side Effects:
947bc6e2962Seric **		none.
948bc6e2962Seric */
949bc6e2962Seric 
95051552439Seric putheader(fp, m, e)
951bc6e2962Seric 	register FILE *fp;
952bc6e2962Seric 	register struct mailer *m;
95351552439Seric 	register ENVELOPE *e;
954bc6e2962Seric {
955bc6e2962Seric 	char buf[BUFSIZ];
956bc6e2962Seric 	register HDR *h;
957bc6e2962Seric 	extern char *arpadate();
958bc6e2962Seric 	extern char *capitalize();
959bc6e2962Seric 	extern char *hvalue();
960bc6e2962Seric 	extern bool samefrom();
961bc6e2962Seric 	char *of_line;
962d90b8d00Seric 	char obuf[MAXLINE];
963d90b8d00Seric 	register char *obp;
964d90b8d00Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
965bc6e2962Seric 
966894de7daSeric 	of_line = hvalue("original-from");
96751552439Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
9686328bdf7Seric 	{
96913bbc08cSeric 		register char *p;
97051552439Seric 		char *origfrom = e->e_origfrom;
971894de7daSeric 		bool nooutput;
97213bbc08cSeric 
973894de7daSeric 		nooutput = FALSE;
9749e9163a0Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
975894de7daSeric 			nooutput = TRUE;
976894de7daSeric 
977894de7daSeric 		/* use From: line from message if generated is the same */
97887bd9a02Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
979894de7daSeric 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
98087bd9a02Seric 		{
98187bd9a02Seric 			p = origfrom;
98287bd9a02Seric 			origfrom = NULL;
98387bd9a02Seric 		}
98487bd9a02Seric 		else if (bitset(H_DEFAULT, h->h_flags))
9854ae18d0eSeric 		{
986b8a19582Seric 			/* macro expand value if generated internally */
98751552439Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
9884ae18d0eSeric 			p = buf;
9894ae18d0eSeric 		}
9908c2bf25bSeric 		else if (bitset(H_ADDR, h->h_flags))
9918c2bf25bSeric 		{
9928c2bf25bSeric 			register int opos;
9938c2bf25bSeric 			bool firstone = TRUE;
9948c2bf25bSeric 
9958c2bf25bSeric 			/*
9968c2bf25bSeric 			**  Output the address list translated by the
9978c2bf25bSeric 			**  mailer and with commas.
9988c2bf25bSeric 			*/
9998c2bf25bSeric 
10008c2bf25bSeric 			p = h->h_value;
10018c2bf25bSeric 			if (p == NULL || *p == '\0' || nooutput)
10028c2bf25bSeric 				continue;
1003d90b8d00Seric 			obp = obuf;
10041277f9a8Seric 			(void) sprintf(obp, "%s: ", capitalize(h->h_field));
10058c2bf25bSeric 			opos = strlen(h->h_field) + 2;
1006d90b8d00Seric 			obp += opos;
1007b8a19582Seric 
1008b8a19582Seric 			/*
1009b8a19582Seric 			**  Run through the list of values.
1010b8a19582Seric 			*/
1011b8a19582Seric 
10128c2bf25bSeric 			while (*p != '\0')
10138c2bf25bSeric 			{
1014b8a19582Seric 				register char *name;
10158c2bf25bSeric 				extern char *remotename();
10168c2bf25bSeric 				char savechar;
10178c2bf25bSeric 
1018b8a19582Seric 				/*
1019b8a19582Seric 				**  Find the end of the name.  New style names
1020b8a19582Seric 				**  end with a comma, old style names end with
1021b8a19582Seric 				**  a space character.  However, spaces do not
1022b8a19582Seric 				**  necessarily delimit an old-style name -- at
1023b8a19582Seric 				**  signs mean keep going.
1024b8a19582Seric 				*/
1025b8a19582Seric 
1026b8a19582Seric 				/* clean up the leading trash in source */
1027b8a19582Seric 				while (*p != '\0' && (isspace(*p) || *p == ','))
1028b8a19582Seric 					p++;
1029b8a19582Seric 				name = p;
1030b8a19582Seric 
1031b8a19582Seric 				/* find end of name */
1032bc28c57aSeric 				while (*p != '\0' && *p != ',')
1033bc28c57aSeric 				{
1034bc28c57aSeric 					extern bool isatword();
1035bc28c57aSeric 					char *oldp;
1036bc28c57aSeric 
103751552439Seric 					if (!e->e_oldstyle || !isspace(*p))
1038bc28c57aSeric 					{
10398c2bf25bSeric 						p++;
1040bc28c57aSeric 						continue;
1041bc28c57aSeric 					}
1042b8a19582Seric 
1043b8a19582Seric 					/* look to see if we have an at sign */
1044bc28c57aSeric 					oldp = p;
1045bc28c57aSeric 					while (*p != '\0' && isspace(*p))
1046bc28c57aSeric 						p++;
1047b8a19582Seric 
1048bc28c57aSeric 					if (*p != '@' && !isatword(p))
1049bc28c57aSeric 					{
1050bc28c57aSeric 						p = oldp;
1051bc28c57aSeric 						break;
1052bc28c57aSeric 					}
1053bc28c57aSeric 					p += *p == '@' ? 1 : 2;
1054bc28c57aSeric 					while (*p != '\0' && isspace(*p))
1055bc28c57aSeric 						p++;
1056bc28c57aSeric 				}
1057b8a19582Seric 				/* at the end of one complete name */
1058b8a19582Seric 
1059b8a19582Seric 				/* strip off trailing white space */
1060b8a19582Seric 				while (p >= name && (isspace(*p) || *p == ','))
1061b8a19582Seric 					p--;
1062b8a19582Seric 				if (++p == name)
1063b8a19582Seric 					continue;
10648c2bf25bSeric 				savechar = *p;
10658c2bf25bSeric 				*p = '\0';
10668c2bf25bSeric 
10678c2bf25bSeric 				/* translate the name to be relative */
1068f60462a7Seric 				name = remotename(name, m, FALSE);
10698c2bf25bSeric 				if (*name == '\0')
1070b8a19582Seric 				{
1071b8a19582Seric 					*p = savechar;
10728c2bf25bSeric 					continue;
1073b8a19582Seric 				}
10748c2bf25bSeric 
10758c2bf25bSeric 				/* output the name with nice formatting */
10768c2bf25bSeric 				opos += strlen(name);
10778c2bf25bSeric 				if (!firstone)
10788c2bf25bSeric 					opos += 2;
10798c2bf25bSeric 				if (opos > 78 && !firstone)
10808c2bf25bSeric 				{
1081d90b8d00Seric 					(void) sprintf(obp, ",\n");
1082d90b8d00Seric 					putline(obuf, fp, fullsmtp);
1083d90b8d00Seric 					obp = obuf;
1084d90b8d00Seric 					(void) sprintf(obp, "        ");
1085d90b8d00Seric 					obp += strlen(obp);
1086bc213ac6Seric 					opos = 8 + strlen(name);
10878c2bf25bSeric 				}
10888c2bf25bSeric 				else if (!firstone)
1089d90b8d00Seric 				{
1090d90b8d00Seric 					(void) sprintf(obp, ", ");
1091d90b8d00Seric 					obp += 2;
1092d90b8d00Seric 				}
1093d90b8d00Seric 				(void) sprintf(obp, "%s", name);
1094d90b8d00Seric 				obp += strlen(obp);
10958c2bf25bSeric 				firstone = FALSE;
10968c2bf25bSeric 				*p = savechar;
10978c2bf25bSeric 			}
10981277f9a8Seric 			(void) strcpy(obp, "\n");
1099573fa002Seric 			putline(obuf, fp, fullsmtp);
11008c2bf25bSeric 			nooutput = TRUE;
11018c2bf25bSeric 		}
11024ae18d0eSeric 		else
11034ae18d0eSeric 			p = h->h_value;
1104894de7daSeric 		if (p == NULL || *p == '\0')
11059e9163a0Seric 			continue;
11065a0dcb5fSeric 
11075a0dcb5fSeric 		/* hack, hack -- output Original-From field if different */
1108894de7daSeric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
1109894de7daSeric 		{
1110894de7daSeric 			/* output new Original-From line if needed */
1111894de7daSeric 			if (of_line == NULL && !samefrom(p, origfrom))
1112d90b8d00Seric 			{
1113d90b8d00Seric 				(void) sprintf(obuf, "Original-From: %s\n", origfrom);
1114d90b8d00Seric 				putline(obuf, fp, fullsmtp);
1115d90b8d00Seric 			}
1116894de7daSeric 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
1117894de7daSeric 			{
1118894de7daSeric 				/* delete Original-From: line if redundant */
1119894de7daSeric 				p = of_line;
1120894de7daSeric 				of_line = NULL;
1121894de7daSeric 			}
1122894de7daSeric 		}
1123894de7daSeric 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
1124894de7daSeric 			nooutput = TRUE;
1125894de7daSeric 
1126894de7daSeric 		/* finally, output the header line */
1127894de7daSeric 		if (!nooutput)
1128894de7daSeric 		{
1129d90b8d00Seric 			(void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p);
1130d90b8d00Seric 			putline(obuf, fp, fullsmtp);
1131894de7daSeric 			h->h_flags |= H_USED;
1132894de7daSeric 		}
11336328bdf7Seric 	}
113451552439Seric }
113551552439Seric /*
113651552439Seric **  PUTBODY -- put the body of a message.
113751552439Seric **
113851552439Seric **	Parameters:
113951552439Seric **		fp -- file to output onto.
114051552439Seric **		m -- a mailer descriptor.
114151552439Seric **		xdot -- if set, use SMTP hidden dot algorithm.
114251552439Seric **
114351552439Seric **	Returns:
114451552439Seric **		none.
114551552439Seric **
114651552439Seric **	Side Effects:
114751552439Seric **		The message is written onto fp.
114851552439Seric */
114951552439Seric 
115051552439Seric putbody(fp, m, xdot)
115151552439Seric 	FILE *fp;
115251552439Seric 	struct mailer *m;
115351552439Seric 	bool xdot;
115451552439Seric {
115551552439Seric 	char buf[MAXLINE + 1];
1156d90b8d00Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
115751552439Seric 
115851552439Seric 	/*
115951552439Seric 	**  Output the body of the message
116051552439Seric 	*/
116151552439Seric 
11625e663df1Seric #ifdef lint
11635e663df1Seric 	/* m will be needed later for complete smtp emulation */
11645e663df1Seric 	if (m == NULL)
11655e663df1Seric 		return;
11665e663df1Seric #endif lint
11675e663df1Seric 
116851552439Seric 	if (TempFile != NULL)
116951552439Seric 	{
117051552439Seric 		rewind(TempFile);
117151552439Seric 		buf[0] = '.';
117251552439Seric 		while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL)
1173d90b8d00Seric 			putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp);
117451552439Seric 
117551552439Seric 		if (ferror(TempFile))
117651552439Seric 		{
117751552439Seric 			syserr("putbody: read error");
117851552439Seric 			ExitStat = EX_IOERR;
117951552439Seric 		}
118051552439Seric 	}
118151552439Seric 
118251552439Seric 	(void) fflush(fp);
118351552439Seric 	if (ferror(fp) && errno != EPIPE)
118451552439Seric 	{
118551552439Seric 		syserr("putbody: write error");
118651552439Seric 		ExitStat = EX_IOERR;
118751552439Seric 	}
118851552439Seric 	errno = 0;
118925a99e2eSeric }
119025a99e2eSeric /*
1191bc28c57aSeric **  ISATWORD -- tell if the word we are pointing to is "at".
1192bc28c57aSeric **
1193bc28c57aSeric **	Parameters:
1194bc28c57aSeric **		p -- word to check.
1195bc28c57aSeric **
1196bc28c57aSeric **	Returns:
1197bc28c57aSeric **		TRUE -- if p is the word at.
1198bc28c57aSeric **		FALSE -- otherwise.
1199bc28c57aSeric **
1200bc28c57aSeric **	Side Effects:
1201bc28c57aSeric **		none.
1202bc28c57aSeric */
1203bc28c57aSeric 
1204bc28c57aSeric bool
1205bc28c57aSeric isatword(p)
1206bc28c57aSeric 	register char *p;
1207bc28c57aSeric {
1208bc28c57aSeric 	extern char lower();
1209bc28c57aSeric 
1210bc28c57aSeric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
1211bc28c57aSeric 	    p[2] != '\0' && isspace(p[2]))
1212bc28c57aSeric 		return (TRUE);
1213bc28c57aSeric 	return (FALSE);
1214bc28c57aSeric }
1215bc28c57aSeric /*
12168c2bf25bSeric **  REMOTENAME -- return the name relative to the current mailer
12178c2bf25bSeric **
12188c2bf25bSeric **	Parameters:
12198c2bf25bSeric **		name -- the name to translate.
1220f60462a7Seric **		force -- if set, forces rewriting even if the mailer
1221f60462a7Seric **			does not request it.  Used for rewriting
1222f60462a7Seric **			sender addresses.
12238c2bf25bSeric **
12248c2bf25bSeric **	Returns:
12258c2bf25bSeric **		the text string representing this address relative to
12268c2bf25bSeric **			the receiving mailer.
12278c2bf25bSeric **
12288c2bf25bSeric **	Side Effects:
12298c2bf25bSeric **		none.
12308c2bf25bSeric **
12318c2bf25bSeric **	Warnings:
12328c2bf25bSeric **		The text string returned is tucked away locally;
12338c2bf25bSeric **			copy it if you intend to save it.
12348c2bf25bSeric */
12358c2bf25bSeric 
12368c2bf25bSeric char *
1237f60462a7Seric remotename(name, m, force)
12388c2bf25bSeric 	char *name;
1239bc213ac6Seric 	struct mailer *m;
1240f60462a7Seric 	bool force;
12418c2bf25bSeric {
12428c2bf25bSeric 	static char buf[MAXNAME];
12438c2bf25bSeric 	char lbuf[MAXNAME];
12448c2bf25bSeric 	extern char *macvalue();
12458c2bf25bSeric 	char *oldf = macvalue('f');
1246bc213ac6Seric 	char *oldx = macvalue('x');
1247bc213ac6Seric 	char *oldg = macvalue('g');
12488c2bf25bSeric 	extern char **prescan();
12498c2bf25bSeric 	register char **pvp;
1250bc213ac6Seric 	extern char *getxpart();
12515e663df1Seric 	extern ADDRESS *buildaddr();
12528c2bf25bSeric 
12538c2bf25bSeric 	/*
1254f60462a7Seric 	**  See if this mailer wants the name to be rewritten.  There are
1255f60462a7Seric 	**  many problems here, owing to the standards for doing replies.
1256f60462a7Seric 	**  In general, these names should only be rewritten if we are
1257f60462a7Seric 	**  sending to another host that runs sendmail.
1258f60462a7Seric 	*/
1259f60462a7Seric 
1260f60462a7Seric 	if (!bitset(M_RELRCPT, m->m_flags) && !force)
12615e663df1Seric 		return (name);
1262f60462a7Seric 
1263f60462a7Seric 	/*
12648c2bf25bSeric 	**  Do general rewriting of name.
12658c2bf25bSeric 	**	This will also take care of doing global name translation.
12668c2bf25bSeric 	*/
12678c2bf25bSeric 
1268bc213ac6Seric 	define('x', getxpart(name));
12698c2bf25bSeric 	pvp = prescan(name, '\0');
1270b3069faaSeric 	if (pvp == NULL)
1271b3069faaSeric 		return (name);
12728c2bf25bSeric 	rewrite(pvp, 1);
12738c2bf25bSeric 	rewrite(pvp, 3);
12748c2bf25bSeric 	if (**pvp == CANONNET)
12758c2bf25bSeric 	{
12768c2bf25bSeric 		/* oops... resolved to something */
12778c2bf25bSeric 		return (name);
12788c2bf25bSeric 	}
12798c2bf25bSeric 	cataddr(pvp, lbuf, sizeof lbuf);
12808c2bf25bSeric 
12818c2bf25bSeric 	/* make the name relative to the receiving mailer */
12828c2bf25bSeric 	define('f', lbuf);
128351552439Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
12848c2bf25bSeric 
12858c2bf25bSeric 	/* rewrite to get rid of garbage we added in the expand above */
12868c2bf25bSeric 	pvp = prescan(buf, '\0');
1287b3069faaSeric 	if (pvp == NULL)
1288b3069faaSeric 		return (name);
12898c2bf25bSeric 	rewrite(pvp, 2);
1290bc213ac6Seric 	cataddr(pvp, lbuf, sizeof lbuf);
1291bc213ac6Seric 
1292bc213ac6Seric 	/* now add any comment info we had before back */
1293bc213ac6Seric 	define('g', lbuf);
129451552439Seric 	expand("$q", buf, &buf[sizeof buf - 1], CurEnv);
12958c2bf25bSeric 
12968c2bf25bSeric 	define('f', oldf);
1297bc213ac6Seric 	define('g', oldg);
1298bc213ac6Seric 	define('x', oldx);
12998c2bf25bSeric 
13008c2bf25bSeric # ifdef DEBUG
1301*6ef48975Seric 	if (tTd(12, 1))
13028c2bf25bSeric 		printf("remotename(%s) => `%s'\n", name, buf);
13038c2bf25bSeric # endif DEBUG
13048c2bf25bSeric 	return (buf);
13058c2bf25bSeric }
13068c2bf25bSeric /*
130787bd9a02Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
130887bd9a02Seric **
130987bd9a02Seric **	Parameters:
131087bd9a02Seric **		ifrom -- internally generated form of from address.
131187bd9a02Seric **		efrom -- external form of from address.
131287bd9a02Seric **
131387bd9a02Seric **	Returns:
131487bd9a02Seric **		TRUE -- if they convey the same info.
131587bd9a02Seric **		FALSE -- if any information has been lost.
131687bd9a02Seric **
131787bd9a02Seric **	Side Effects:
131887bd9a02Seric **		none.
131987bd9a02Seric */
132087bd9a02Seric 
132187bd9a02Seric bool
132287bd9a02Seric samefrom(ifrom, efrom)
132387bd9a02Seric 	char *ifrom;
132487bd9a02Seric 	char *efrom;
132587bd9a02Seric {
1326894de7daSeric 	register char *p;
1327894de7daSeric 	char buf[MAXNAME + 4];
1328894de7daSeric 
1329894de7daSeric # ifdef DEBUG
1330*6ef48975Seric 	if (tTd(3, 8))
1331894de7daSeric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
1332894de7daSeric # endif DEBUG
1333894de7daSeric 	if (strcmp(ifrom, efrom) == 0)
1334894de7daSeric 		goto success;
1335894de7daSeric 	p = index(ifrom, '@');
1336894de7daSeric 	if (p == NULL)
1337894de7daSeric 		goto failure;
1338894de7daSeric 	*p = '\0';
13395e663df1Seric 	(void) strcpy(buf, ifrom);
13405e663df1Seric 	(void) strcat(buf, " at ");
1341894de7daSeric 	*p++ = '@';
13425e663df1Seric 	(void) strcat(buf, p);
1343894de7daSeric 	if (strcmp(buf, efrom) == 0)
1344894de7daSeric 		goto success;
1345894de7daSeric 
1346894de7daSeric   failure:
1347894de7daSeric # ifdef DEBUG
1348*6ef48975Seric 	if (tTd(3, 8))
1349894de7daSeric 		printf("FALSE\n");
1350894de7daSeric # endif DEBUG
1351894de7daSeric 	return (FALSE);
1352894de7daSeric 
1353894de7daSeric   success:
1354894de7daSeric # ifdef DEBUG
1355*6ef48975Seric 	if (tTd(3, 8))
1356894de7daSeric 		printf("TRUE\n");
1357894de7daSeric # endif DEBUG
1358894de7daSeric 	return (TRUE);
135987bd9a02Seric }
136087bd9a02Seric /*
136125a99e2eSeric **  MAILFILE -- Send a message to a file.
136225a99e2eSeric **
1363f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1364f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1365f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1366f129ec7dSeric **	sendmail runs as root.
1367f129ec7dSeric **
136825a99e2eSeric **	Parameters:
136925a99e2eSeric **		filename -- the name of the file to send to.
13706259796dSeric **		ctladdr -- the controlling address header -- includes
13716259796dSeric **			the userid/groupid to be when sending.
137225a99e2eSeric **
137325a99e2eSeric **	Returns:
137425a99e2eSeric **		The exit code associated with the operation.
137525a99e2eSeric **
137625a99e2eSeric **	Side Effects:
137725a99e2eSeric **		none.
137825a99e2eSeric */
137925a99e2eSeric 
13806259796dSeric mailfile(filename, ctladdr)
138125a99e2eSeric 	char *filename;
13826259796dSeric 	ADDRESS *ctladdr;
138325a99e2eSeric {
138425a99e2eSeric 	register FILE *f;
138532d19d43Seric 	register int pid;
138625a99e2eSeric 
138732d19d43Seric 	/*
138832d19d43Seric 	**  Fork so we can change permissions here.
138932d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
139032d19d43Seric 	**	the complications of calling subroutines, etc.
139132d19d43Seric 	*/
139232d19d43Seric 
139332d19d43Seric 	DOFORK(fork);
139432d19d43Seric 
139532d19d43Seric 	if (pid < 0)
139632d19d43Seric 		return (EX_OSERR);
139732d19d43Seric 	else if (pid == 0)
139832d19d43Seric 	{
139932d19d43Seric 		/* child -- actually write to file */
1400f129ec7dSeric 		struct stat stb;
1401f129ec7dSeric 
14020984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
14030984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
14040984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
1405f129ec7dSeric 		umask(OldUmask);
1406f129ec7dSeric 		if (stat(filename, &stb) < 0)
1407e6e1265fSeric 			stb.st_mode = 0666;
1408f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1409f129ec7dSeric 			exit(EX_CANTCREAT);
141003827b5fSeric 		if (ctladdr == NULL)
14117a941e7aSeric 			ctladdr = &CurEnv->e_from;
1412f129ec7dSeric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
1413e36b99e2Seric 		{
1414e36b99e2Seric 			if (ctladdr->q_uid == 0)
1415e36b99e2Seric 				(void) setgid(DefGid);
1416e36b99e2Seric 			else
14176259796dSeric 				(void) setgid(ctladdr->q_gid);
1418e36b99e2Seric 		}
1419f129ec7dSeric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
1420e36b99e2Seric 		{
1421e36b99e2Seric 			if (ctladdr->q_uid == 0)
1422e36b99e2Seric 				(void) setuid(DefUid);
1423e36b99e2Seric 			else
14246259796dSeric 				(void) setuid(ctladdr->q_uid);
1425e36b99e2Seric 		}
142627628d59Seric 		f = dfopen(filename, "a");
142725a99e2eSeric 		if (f == NULL)
142832d19d43Seric 			exit(EX_CANTCREAT);
142925a99e2eSeric 
143051552439Seric 		putfromline(f, Mailer[1]);
143151552439Seric 		(*CurEnv->e_puthdr)(f, Mailer[1], CurEnv);
1432d90b8d00Seric 		fputs("\n", f);
143351552439Seric 		(*CurEnv->e_putbody)(f, Mailer[1], FALSE);
143425a99e2eSeric 		fputs("\n", f);
1435db8841e9Seric 		(void) fclose(f);
143632d19d43Seric 		(void) fflush(stdout);
1437e36b99e2Seric 
143827628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1439c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
144032d19d43Seric 		exit(EX_OK);
144113bbc08cSeric 		/*NOTREACHED*/
144232d19d43Seric 	}
144332d19d43Seric 	else
144432d19d43Seric 	{
144532d19d43Seric 		/* parent -- wait for exit status */
144632d19d43Seric 		register int i;
144732d19d43Seric 		auto int stat;
144832d19d43Seric 
144932d19d43Seric 		while ((i = wait(&stat)) != pid)
145032d19d43Seric 		{
145132d19d43Seric 			if (i < 0)
145232d19d43Seric 			{
145332d19d43Seric 				stat = EX_OSERR << 8;
145432d19d43Seric 				break;
145532d19d43Seric 			}
145632d19d43Seric 		}
14570984da9fSeric 		if ((stat & 0377) != 0)
14580984da9fSeric 			stat = EX_UNAVAILABLE << 8;
145932d19d43Seric 		return ((stat >> 8) & 0377);
146032d19d43Seric 	}
146125a99e2eSeric }
1462ea4dc939Seric /*
1463ea4dc939Seric **  SENDALL -- actually send all the messages.
1464ea4dc939Seric **
1465ea4dc939Seric **	Parameters:
14660c52a0b3Seric **		e -- the envelope to send.
1467ea4dc939Seric **		verifyonly -- if set, only give verification messages.
1468ea4dc939Seric **
1469ea4dc939Seric **	Returns:
1470ea4dc939Seric **		none.
1471ea4dc939Seric **
1472ea4dc939Seric **	Side Effects:
1473ea4dc939Seric **		Scans the send lists and sends everything it finds.
14740c52a0b3Seric **		Delivers any appropriate error messages.
1475ea4dc939Seric */
1476ea4dc939Seric 
14770c52a0b3Seric sendall(e, verifyonly)
14780c52a0b3Seric 	ENVELOPE *e;
1479ea4dc939Seric 	bool verifyonly;
1480ea4dc939Seric {
1481e77e673fSeric 	register ADDRESS *q;
1482ea4dc939Seric 
1483772e6e50Seric # ifdef DEBUG
1484*6ef48975Seric 	if (tTd(13, 2))
1485772e6e50Seric 	{
1486772e6e50Seric 		printf("\nSend Queue:\n");
14870c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1488772e6e50Seric 	}
1489772e6e50Seric # endif DEBUG
1490ea4dc939Seric 
14910c52a0b3Seric 	/*
14920c52a0b3Seric 	**  Run through the list and send everything.
14930c52a0b3Seric 	*/
14940c52a0b3Seric 
14950c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1496ea4dc939Seric 	{
1497ea4dc939Seric 		if (verifyonly)
1498ea4dc939Seric 		{
14997a941e7aSeric 			CurEnv->e_to = q->q_paddr;
1500e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1501ea4dc939Seric 				message(Arpa_Info, "deliverable");
1502ea4dc939Seric 		}
1503ea4dc939Seric 		else
150451552439Seric 			(void) deliver(q);
1505ea4dc939Seric 	}
15060c52a0b3Seric 
15070c52a0b3Seric 	/*
15080c52a0b3Seric 	**  Now run through and check for errors.
15090c52a0b3Seric 	*/
15100c52a0b3Seric 
15110c52a0b3Seric 	if (verifyonly)
15120c52a0b3Seric 		return;
15130c52a0b3Seric 
15140c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15150c52a0b3Seric 	{
15160c52a0b3Seric 		register ADDRESS *qq;
15170c52a0b3Seric 
15180c52a0b3Seric 		if (bitset(QQUEUEUP, q->q_flags))
15190c52a0b3Seric 			e->e_queueup = TRUE;
15200c52a0b3Seric 		if (!bitset(QBADADDR, q->q_flags))
15210c52a0b3Seric 			continue;
15220c52a0b3Seric 
15230c52a0b3Seric 		/* we have an address that failed -- find the parent */
15240c52a0b3Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
15250c52a0b3Seric 		{
15260c52a0b3Seric 			char obuf[MAXNAME + 6];
15270c52a0b3Seric 			extern char *aliaslookup();
15280c52a0b3Seric 
15290c52a0b3Seric 			/* we can only have owners for local addresses */
15300c52a0b3Seric 			if (!bitset(M_LOCAL, qq->q_mailer->m_flags))
15310c52a0b3Seric 				continue;
15320c52a0b3Seric 
15330c52a0b3Seric 			/* see if the owner list exists */
15340c52a0b3Seric 			(void) strcpy(obuf, "owner-");
1535cec031e3Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1536cec031e3Seric 				(void) strcat(obuf, "owner");
1537cec031e3Seric 			else
15380c52a0b3Seric 				(void) strcat(obuf, qq->q_user);
15390c52a0b3Seric 			if (aliaslookup(obuf) == NULL)
15400c52a0b3Seric 				continue;
15410c52a0b3Seric 
15420c52a0b3Seric 			/* owner list exists -- add it to the error queue */
15430c52a0b3Seric 			qq->q_flags &= ~QPRIMARY;
15440c52a0b3Seric 			sendto(obuf, 1, qq, &e->e_errorqueue);
15450c52a0b3Seric 			MailBack = TRUE;
15460c52a0b3Seric 			break;
15470c52a0b3Seric 		}
15480c52a0b3Seric 
15490c52a0b3Seric 		/* if we did not find an owner, send to the sender */
15500c52a0b3Seric 		if (qq == NULL)
15510c52a0b3Seric 			sendto(e->e_from.q_paddr, 1, qq, &e->e_errorqueue);
15520c52a0b3Seric 	}
15530c52a0b3Seric }
15540c52a0b3Seric /*
15550c52a0b3Seric **  CHECKERRORS -- check a queue of addresses and process errors.
15560c52a0b3Seric **
15570c52a0b3Seric **	Parameters:
15580c52a0b3Seric **		e -- the envelope to check.
15590c52a0b3Seric **
15600c52a0b3Seric **	Returns:
15610c52a0b3Seric **		none.
15620c52a0b3Seric **
15630c52a0b3Seric **	Side Effects:
15640c52a0b3Seric **		Arranges to queue all tempfailed messages in q
15650c52a0b3Seric **			or deliver error responses.
15660c52a0b3Seric */
15670c52a0b3Seric 
15680c52a0b3Seric checkerrors(e)
15690c52a0b3Seric 	register ENVELOPE *e;
15700c52a0b3Seric {
15710c52a0b3Seric # ifdef DEBUG
1572*6ef48975Seric 	if (tTd(4, 1))
15730c52a0b3Seric 	{
1574e1e0dfbfSeric 		printf("\ncheckerrors: FatalErrors %d, errorqueue:\n");
15750c52a0b3Seric 		printaddr(e->e_errorqueue, TRUE);
15760c52a0b3Seric 	}
15770c52a0b3Seric # endif DEBUG
15780c52a0b3Seric 
15790c52a0b3Seric 	/* mail back the transcript on errors */
15800c52a0b3Seric 	if (FatalErrors)
15810c52a0b3Seric 		savemail();
15820c52a0b3Seric 
15830c52a0b3Seric 	/* queue up anything laying around */
15840c52a0b3Seric 	if (e->e_queueup)
15850c52a0b3Seric 	{
15860c52a0b3Seric # ifdef QUEUE
15870c52a0b3Seric 		queueup(e, FALSE);
15880c52a0b3Seric # else QUEUE
15890c52a0b3Seric 		syserr("finis: trying to queue %s", e->e_df);
15900c52a0b3Seric # endif QUEUE
15910c52a0b3Seric 	}
1592ea4dc939Seric }
1593