125a99e2eSeric # include <signal.h>
254aa2b0fSeric # include <errno.h>
3b20b3270Seric # include "sendmail.h"
4c77d1c25Seric # include <sys/stat.h>
525a99e2eSeric # ifdef LOG
6e374fd72Seric # include <syslog.h>
725a99e2eSeric # endif LOG
825a99e2eSeric 
9*b8a19582Seric SCCSID(@(#)deliver.c	3.95		08/07/82);
10259cace7Seric 
1125a99e2eSeric /*
1213bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
1313bbc08cSeric **
1413bbc08cSeric **	This routine delivers to everyone on the same host as the
1513bbc08cSeric **	user on the head of the list.  It is clever about mailers
1613bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
1713bbc08cSeric **	that it will deliver to all these addresses however -- so
1813bbc08cSeric **	deliver should be called once for each address on the
1913bbc08cSeric **	list.
2025a99e2eSeric **
2125a99e2eSeric **	Parameters:
22c77d1c25Seric **		firstto -- head of the address list to deliver to.
2325a99e2eSeric **
2425a99e2eSeric **	Returns:
2525a99e2eSeric **		zero -- successfully delivered.
2625a99e2eSeric **		else -- some failure, see ExitStat for more info.
2725a99e2eSeric **
2825a99e2eSeric **	Side Effects:
2925a99e2eSeric **		The standard input is passed off to someone.
3025a99e2eSeric */
3125a99e2eSeric 
3251552439Seric deliver(firstto)
33c77d1c25Seric 	ADDRESS *firstto;
3425a99e2eSeric {
3578442df3Seric 	char *host;			/* host being sent to */
3678442df3Seric 	char *user;			/* user being sent to */
3725a99e2eSeric 	char **pvp;
385dfc646bSeric 	register char **mvp;
3925a99e2eSeric 	register char *p;
4078442df3Seric 	register struct mailer *m;	/* mailer for this recipient */
415dfc646bSeric 	register int i;
422a6e0786Seric 	extern bool checkcompat();
435dfc646bSeric 	char *pv[MAXPV+1];
4478442df3Seric 	char tobuf[MAXLINE];		/* text line of to people */
455dfc646bSeric 	char buf[MAXNAME];
466259796dSeric 	ADDRESS *ctladdr;
476259796dSeric 	extern ADDRESS *getctladdr();
4878442df3Seric 	char tfrombuf[MAXNAME];		/* translated from person */
4978442df3Seric 	extern char **prescan();
50c77d1c25Seric 	register ADDRESS *to = firstto;
51c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
52c579ef51Seric 	bool tempfail = FALSE;
53772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
54da2935e1Seric 	bool notopen = TRUE;		/* set if connection not quite open */
5525a99e2eSeric 
5635490626Seric 	errno = 0;
57da2935e1Seric 	if (bitset(QDONTSEND, to->q_flags))
585dfc646bSeric 		return (0);
5925a99e2eSeric 
6051552439Seric 	m = to->q_mailer;
6151552439Seric 	host = to->q_host;
6251552439Seric 
6325a99e2eSeric # ifdef DEBUG
6425a99e2eSeric 	if (Debug)
655dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
6651552439Seric 			m->m_mno, host, to->q_user);
6725a99e2eSeric # endif DEBUG
68f3dbc832Seric 
69f3dbc832Seric 	/*
70f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
71f3dbc832Seric 	**  connections now, just mark these addresses and return.
72f3dbc832Seric 	**	This is useful if we want to batch connections to
73f3dbc832Seric 	**	reduce load.  This will cause the messages to be
74f3dbc832Seric 	**	queued up, and a daemon will come along to send the
75f3dbc832Seric 	**	messages later.
76f3dbc832Seric 	**		This should be on a per-mailer basis.
77f3dbc832Seric 	*/
78f3dbc832Seric 
79f3dbc832Seric 	if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags))
80f3dbc832Seric 	{
81f3dbc832Seric 		for (; to != NULL; to = to->q_next)
82f3dbc832Seric 			if (!bitset(QDONTSEND, to->q_flags))
83f3dbc832Seric 				to->q_flags |= QQUEUEUP|QDONTSEND;
84f3dbc832Seric 		return (0);
85f3dbc832Seric 	}
86f3dbc832Seric 
8725a99e2eSeric 	/*
885dfc646bSeric 	**  Do initial argv setup.
895dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
905dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
915dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
925dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
935dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
94f3dbc832Seric 	**		We rewrite the from address here, being careful
95f3dbc832Seric 	**		to also rewrite it again using ruleset 2 to
96f3dbc832Seric 	**		eliminate redundancies.
975dfc646bSeric 	*/
985dfc646bSeric 
9978442df3Seric 	/* rewrite from address, using rewriting rules */
10051552439Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
10178442df3Seric 	mvp = prescan(buf, '\0');
10278442df3Seric 	if (mvp == NULL)
10378442df3Seric 	{
10478442df3Seric 		syserr("bad mailer from translate \"%s\"", buf);
10578442df3Seric 		return (EX_SOFTWARE);
10678442df3Seric 	}
10778442df3Seric 	rewrite(mvp, 2);
10878442df3Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
10978442df3Seric 
11078442df3Seric 	define('g', tfrombuf);		/* translated sender address */
1115dfc646bSeric 	define('h', host);		/* to host */
1125dfc646bSeric 	Errors = 0;
1135dfc646bSeric 	pvp = pv;
1145dfc646bSeric 	*pvp++ = m->m_argv[0];
1155dfc646bSeric 
1165dfc646bSeric 	/* insert -f or -r flag as appropriate */
1175dfc646bSeric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1185dfc646bSeric 	{
1195dfc646bSeric 		if (bitset(M_FOPT, m->m_flags))
1205dfc646bSeric 			*pvp++ = "-f";
1215dfc646bSeric 		else
1225dfc646bSeric 			*pvp++ = "-r";
12351552439Seric 		expand("$g", buf, &buf[sizeof buf - 1], CurEnv);
1245dfc646bSeric 		*pvp++ = newstr(buf);
1255dfc646bSeric 	}
1265dfc646bSeric 
1275dfc646bSeric 	/*
1285dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1295dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1305dfc646bSeric 	**  be one of these, and there are only a few more slots
1315dfc646bSeric 	**  in the pv after it.
1325dfc646bSeric 	*/
1335dfc646bSeric 
1345dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1355dfc646bSeric 	{
1365dfc646bSeric 		while ((p = index(p, '$')) != NULL)
1375dfc646bSeric 			if (*++p == 'u')
1385dfc646bSeric 				break;
1395dfc646bSeric 		if (p != NULL)
1405dfc646bSeric 			break;
1415dfc646bSeric 
1425dfc646bSeric 		/* this entry is safe -- go ahead and process it */
14351552439Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
1445dfc646bSeric 		*pvp++ = newstr(buf);
1455dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1465dfc646bSeric 		{
1475dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1485dfc646bSeric 			return (-1);
1495dfc646bSeric 		}
1505dfc646bSeric 	}
151c579ef51Seric 
15233db8731Seric 	/*
15333db8731Seric 	**  If we have no substitution for the user name in the argument
15433db8731Seric 	**  list, we know that we must supply the names otherwise -- and
15533db8731Seric 	**  SMTP is the answer!!
15633db8731Seric 	*/
15733db8731Seric 
1585dfc646bSeric 	if (*mvp == NULL)
159c579ef51Seric 	{
160c579ef51Seric 		/* running SMTP */
1612c7e1b8dSeric # ifdef SMTP
162c579ef51Seric 		clever = TRUE;
163c579ef51Seric 		*pvp = NULL;
1642c7e1b8dSeric # else SMTP
16533db8731Seric 		/* oops!  we don't implement SMTP */
1662c7e1b8dSeric 		syserr("SMTP style mailer");
1672c7e1b8dSeric 		return (EX_SOFTWARE);
1682c7e1b8dSeric # endif SMTP
169c579ef51Seric 	}
1705dfc646bSeric 
1715dfc646bSeric 	/*
1725dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
1735dfc646bSeric 	**  run through our address list and append all the addresses
1745dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
1755dfc646bSeric 	**  always send another copy later.
1765dfc646bSeric 	*/
1775dfc646bSeric 
1785dfc646bSeric 	tobuf[0] = '\0';
1797a941e7aSeric 	CurEnv->e_to = tobuf;
1806259796dSeric 	ctladdr = NULL;
1815dfc646bSeric 	for (; to != NULL; to = to->q_next)
1825dfc646bSeric 	{
1835dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
184bea22b26Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1855dfc646bSeric 			break;
1865dfc646bSeric 
1875dfc646bSeric 		/* if already sent or not for this host, don't send */
188da2935e1Seric 		if (bitset(QDONTSEND, to->q_flags) ||
189da2935e1Seric 		    strcmp(to->q_host, host) != 0 ||
190da2935e1Seric 		    to->q_mailer != firstto->q_mailer)
1915dfc646bSeric 			continue;
1926259796dSeric 
193772e6e50Seric # ifdef DEBUG
194772e6e50Seric 		if (Debug)
195772e6e50Seric 		{
196772e6e50Seric 			printf("\nsend to ");
197772e6e50Seric 			printaddr(to, FALSE);
198772e6e50Seric 		}
199772e6e50Seric # endif DEBUG
200772e6e50Seric 
2016259796dSeric 		/* compute effective uid/gid when sending */
2027da1035fSeric 		if (to->q_mailer == ProgMailer)
2036259796dSeric 			ctladdr = getctladdr(to);
2046259796dSeric 
2055dfc646bSeric 		user = to->q_user;
2067a941e7aSeric 		CurEnv->e_to = to->q_paddr;
2075dfc646bSeric 		to->q_flags |= QDONTSEND;
2085dfc646bSeric 
2095dfc646bSeric 		/*
2105dfc646bSeric 		**  Check to see that these people are allowed to
2115dfc646bSeric 		**  talk to each other.
2122a6e0786Seric 		*/
2132a6e0786Seric 
2142a6e0786Seric 		if (!checkcompat(to))
2155dfc646bSeric 		{
2165dfc646bSeric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
2175dfc646bSeric 			continue;
2185dfc646bSeric 		}
2192a6e0786Seric 
2202a6e0786Seric 		/*
2219ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2229ec9501bSeric 		**	about them.
22325a99e2eSeric 		*/
22425a99e2eSeric 
2252a6e0786Seric 		if (bitset(M_STRIPQ, m->m_flags))
22625a99e2eSeric 		{
2279ec9501bSeric 			stripquotes(user, TRUE);
2289ec9501bSeric 			stripquotes(host, TRUE);
2299ec9501bSeric 		}
2309ec9501bSeric 		else
2319ec9501bSeric 		{
2329ec9501bSeric 			stripquotes(user, FALSE);
2339ec9501bSeric 			stripquotes(host, FALSE);
23425a99e2eSeric 		}
23525a99e2eSeric 
23625a99e2eSeric 		/*
237da2935e1Seric 		**  Do initial connection setup if needed.
238da2935e1Seric 		*/
239da2935e1Seric 
240da2935e1Seric 		if (notopen)
241da2935e1Seric 		{
242da2935e1Seric 			message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
243da2935e1Seric # ifdef SMTP
244da2935e1Seric 			if (clever)
245da2935e1Seric 			{
246da2935e1Seric 				/* send the initial SMTP protocol */
247da2935e1Seric 				i = smtpinit(m, pv, (ADDRESS *) NULL);
248da2935e1Seric # ifdef QUEUE
249da2935e1Seric 				if (i == EX_TEMPFAIL)
250da2935e1Seric 					tempfail = TRUE;
251da2935e1Seric # endif QUEUE
252da2935e1Seric 			}
253da2935e1Seric # ifdef SMTP
254da2935e1Seric 			notopen = FALSE;
255da2935e1Seric 		}
256da2935e1Seric 
257da2935e1Seric 		/*
258508daeccSeric 		**  Pass it to the other host if we are running SMTP.
259508daeccSeric 		*/
260508daeccSeric 
261508daeccSeric 		if (clever)
262508daeccSeric 		{
263508daeccSeric # ifdef SMTP
264508daeccSeric 			i = smtprcpt(to);
265508daeccSeric 			if (i != EX_OK)
266508daeccSeric 			{
267508daeccSeric # ifdef QUEUE
268508daeccSeric 				if (i == EX_TEMPFAIL)
269508daeccSeric 					to->q_flags |= QQUEUEUP;
270508daeccSeric 				else
271508daeccSeric # endif QUEUE
272508daeccSeric 					to->q_flags |= QBADADDR;
273508daeccSeric 				giveresponse(i, TRUE, m);
274508daeccSeric 			}
275508daeccSeric # else SMTP
276508daeccSeric 			syserr("trying to be clever");
277508daeccSeric # endif SMTP
278508daeccSeric 		}
279508daeccSeric 
280508daeccSeric 		/*
2813efaed6eSeric 		**  If an error message has already been given, don't
2823efaed6eSeric 		**	bother to send to this address.
2833efaed6eSeric 		**
2843efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2853efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2863efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2873efaed6eSeric 		*/
2883efaed6eSeric 
2896cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2903efaed6eSeric 			continue;
2913efaed6eSeric 
292f2fec898Seric 		/* save statistics.... */
2937da1035fSeric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2947a941e7aSeric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
295f2fec898Seric 
2963efaed6eSeric 		/*
29725a99e2eSeric 		**  See if this user name is "special".
29825a99e2eSeric 		**	If the user name has a slash in it, assume that this
29951552439Seric 		**	is a file -- send it off without further ado.  Note
30051552439Seric 		**	that this type of addresses is not processed along
30151552439Seric 		**	with the others, so we fudge on the To person.
30225a99e2eSeric 		*/
30325a99e2eSeric 
3047da1035fSeric 		if (m == LocalMailer)
30525a99e2eSeric 		{
306a49f24c0Seric 			if (user[0] == '/')
30725a99e2eSeric 			{
3086259796dSeric 				i = mailfile(user, getctladdr(to));
30925a99e2eSeric 				giveresponse(i, TRUE, m);
3105dfc646bSeric 				continue;
31125a99e2eSeric 			}
31225a99e2eSeric 		}
31325a99e2eSeric 
31413bbc08cSeric 		/*
31513bbc08cSeric 		**  Address is verified -- add this user to mailer
31613bbc08cSeric 		**  argv, and add it to the print list of recipients.
31713bbc08cSeric 		*/
31813bbc08cSeric 
319508daeccSeric 		/* link together the chain of recipients */
320508daeccSeric 		to->q_tchain = tochain;
321508daeccSeric 		tochain = to;
322508daeccSeric 
3235dfc646bSeric 		/* create list of users for error messages */
3245dfc646bSeric 		if (tobuf[0] != '\0')
325db8841e9Seric 			(void) strcat(tobuf, ",");
326db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
3275dfc646bSeric 		define('u', user);		/* to user */
328c2567733Seric 		define('z', to->q_home);	/* user's home */
3295dfc646bSeric 
330c579ef51Seric 		/*
331508daeccSeric 		**  Expand out this user into argument list.
332c579ef51Seric 		*/
333c579ef51Seric 
334508daeccSeric 		if (!clever)
335c579ef51Seric 		{
33651552439Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3375dfc646bSeric 			*pvp++ = newstr(buf);
3385dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3395dfc646bSeric 			{
3405dfc646bSeric 				/* allow some space for trailing parms */
3415dfc646bSeric 				break;
3425dfc646bSeric 			}
3435dfc646bSeric 		}
344c579ef51Seric 	}
3455dfc646bSeric 
346145b49b1Seric 	/* see if any addresses still exist */
347145b49b1Seric 	if (tobuf[0] == '\0')
348c579ef51Seric 	{
3492c7e1b8dSeric # ifdef SMTP
350c579ef51Seric 		if (clever)
3514a3f06b2Seric 			smtpquit(pv[0], FALSE);
3522c7e1b8dSeric # endif SMTP
3535e663df1Seric 		define('g', (char *) NULL);
354145b49b1Seric 		return (0);
355c579ef51Seric 	}
356145b49b1Seric 
3575dfc646bSeric 	/* print out messages as full list */
3587a941e7aSeric 	CurEnv->e_to = tobuf;
3595dfc646bSeric 
3605dfc646bSeric 	/*
3615dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3625dfc646bSeric 	*/
3635dfc646bSeric 
364c579ef51Seric 	while (!clever && *++mvp != NULL)
3655dfc646bSeric 	{
36651552439Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3675dfc646bSeric 		*pvp++ = newstr(buf);
3685dfc646bSeric 		if (pvp >= &pv[MAXPV])
3695dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
3705dfc646bSeric 	}
3715dfc646bSeric 	*pvp++ = NULL;
3725dfc646bSeric 
37325a99e2eSeric 	/*
37425a99e2eSeric 	**  Call the mailer.
3756328bdf7Seric 	**	The argument vector gets built, pipes
37625a99e2eSeric 	**	are created as necessary, and we fork & exec as
3776328bdf7Seric 	**	appropriate.
378c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
37925a99e2eSeric 	*/
38025a99e2eSeric 
3816259796dSeric 	if (ctladdr == NULL)
3827a941e7aSeric 		ctladdr = &CurEnv->e_from;
3832c7e1b8dSeric # ifdef SMTP
384c579ef51Seric 	if (clever)
385c579ef51Seric 	{
38651552439Seric 		i = smtpfinish(m, CurEnv);
3874a3f06b2Seric 		smtpquit(pv[0], TRUE);
388c579ef51Seric 	}
389c579ef51Seric 	else
3902c7e1b8dSeric # endif SMTP
39151552439Seric 		i = sendoff(m, pv, ctladdr);
3925dfc646bSeric 
393c77d1c25Seric 	/*
394c77d1c25Seric 	**  If we got a temporary failure, arrange to queue the
395c77d1c25Seric 	**  addressees.
396c77d1c25Seric 	*/
397c77d1c25Seric 
3982c7e1b8dSeric # ifdef QUEUE
399c77d1c25Seric 	if (i == EX_TEMPFAIL)
400c77d1c25Seric 	{
401772e6e50Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
402c77d1c25Seric 			to->q_flags |= QQUEUEUP;
403c77d1c25Seric 	}
4042c7e1b8dSeric # endif QUEUE
405c77d1c25Seric 
40635490626Seric 	errno = 0;
4075e663df1Seric 	define('g', (char *) NULL);
4085dfc646bSeric 	return (i);
40925a99e2eSeric }
4105dfc646bSeric /*
41132d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
41232d19d43Seric **
41332d19d43Seric **	This MUST be a macro, since after a vfork we are running
41432d19d43Seric **	two processes on the same stack!!!
41532d19d43Seric **
41632d19d43Seric **	Parameters:
41732d19d43Seric **		none.
41832d19d43Seric **
41932d19d43Seric **	Returns:
42032d19d43Seric **		From a macro???  You've got to be kidding!
42132d19d43Seric **
42232d19d43Seric **	Side Effects:
42332d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
42432d19d43Seric **			pid of child in parent, zero in child.
42532d19d43Seric **			-1 on unrecoverable error.
42632d19d43Seric **
42732d19d43Seric **	Notes:
42832d19d43Seric **		I'm awfully sorry this looks so awful.  That's
42932d19d43Seric **		vfork for you.....
43032d19d43Seric */
43132d19d43Seric 
43232d19d43Seric # define NFORKTRIES	5
43332d19d43Seric # ifdef VFORK
43432d19d43Seric # define XFORK	vfork
43532d19d43Seric # else VFORK
43632d19d43Seric # define XFORK	fork
43732d19d43Seric # endif VFORK
43832d19d43Seric 
43932d19d43Seric # define DOFORK(fORKfN) \
44032d19d43Seric {\
44132d19d43Seric 	register int i;\
44232d19d43Seric \
44332d19d43Seric 	for (i = NFORKTRIES; i-- > 0; )\
44432d19d43Seric 	{\
44532d19d43Seric 		pid = fORKfN();\
44632d19d43Seric 		if (pid >= 0)\
44732d19d43Seric 			break;\
4485e663df1Seric 		sleep(NFORKTRIES - i);\
44932d19d43Seric 	}\
45032d19d43Seric }
45132d19d43Seric /*
4522ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
4532ed72599Seric **
4542ed72599Seric **	Parameters:
4552ed72599Seric **		none.
4562ed72599Seric **
4572ed72599Seric **	Returns:
4582ed72599Seric **		pid of child in parent.
4592ed72599Seric **		zero in child.
4602ed72599Seric **		-1 on error.
4612ed72599Seric **
4622ed72599Seric **	Side Effects:
4632ed72599Seric **		returns twice, once in parent and once in child.
4642ed72599Seric */
4652ed72599Seric 
4662ed72599Seric dofork()
4672ed72599Seric {
4682ed72599Seric 	register int pid;
4692ed72599Seric 
4702ed72599Seric 	DOFORK(fork);
4712ed72599Seric 	return (pid);
4722ed72599Seric }
4732ed72599Seric /*
4745dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
4755dfc646bSeric **
4765dfc646bSeric **	Parameters:
4775dfc646bSeric **		m -- mailer descriptor.
4785dfc646bSeric **		pvp -- parameter vector to send to it.
4796259796dSeric **		ctladdr -- an address pointer controlling the
4806259796dSeric **			user/groupid etc. of the mailer.
4815dfc646bSeric **
4825dfc646bSeric **	Returns:
4835dfc646bSeric **		exit status of mailer.
4845dfc646bSeric **
4855dfc646bSeric **	Side Effects:
4865dfc646bSeric **		none.
4875dfc646bSeric */
4885dfc646bSeric 
48951552439Seric sendoff(m, pvp, ctladdr)
4905dfc646bSeric 	struct mailer *m;
4915dfc646bSeric 	char **pvp;
4926259796dSeric 	ADDRESS *ctladdr;
4935dfc646bSeric {
494c579ef51Seric 	auto FILE *mfile;
495c579ef51Seric 	auto FILE *rfile;
4965dfc646bSeric 	register int i;
497c579ef51Seric 	int pid;
498c579ef51Seric 
499c579ef51Seric 	/*
500c579ef51Seric 	**  Create connection to mailer.
501c579ef51Seric 	*/
502c579ef51Seric 
503c579ef51Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
504c579ef51Seric 	if (pid < 0)
505c579ef51Seric 		return (-1);
506c579ef51Seric 
507c579ef51Seric 	/*
508c579ef51Seric 	**  Format and send message.
509c579ef51Seric 	*/
510c579ef51Seric 
511c579ef51Seric 	(void) signal(SIGPIPE, SIG_IGN);
51251552439Seric 	putfromline(mfile, m);
51351552439Seric 	(*CurEnv->e_puthdr)(mfile, m, CurEnv);
51451552439Seric 	fprintf(mfile, "\n");
51551552439Seric 	(*CurEnv->e_putbody)(mfile, m, FALSE);
516c579ef51Seric 	(void) fclose(mfile);
517c579ef51Seric 
518c579ef51Seric 	i = endmailer(pid, pvp[0]);
519c579ef51Seric 	giveresponse(i, TRUE, m);
520bc6e2962Seric 
521bc6e2962Seric 	/* arrange a return receipt if requested */
5227a941e7aSeric 	if (CurEnv->e_retreceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK)
523bc6e2962Seric 	{
5247a941e7aSeric 		CurEnv->e_sendreceipt = TRUE;
5257a941e7aSeric 		fprintf(Xscript, "%s... successfully delivered\n", CurEnv->e_to);
526bc6e2962Seric 		/* do we want to send back more info? */
527bc6e2962Seric 	}
528bc6e2962Seric 
529c579ef51Seric 	return (i);
530c579ef51Seric }
531c579ef51Seric /*
532c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
533c579ef51Seric **
534c579ef51Seric **	We should never get fatal errors (e.g., segmentation
535c579ef51Seric **	violation), so we report those specially.  For other
536c579ef51Seric **	errors, we choose a status message (into statmsg),
537c579ef51Seric **	and if it represents an error, we print it.
538c579ef51Seric **
539c579ef51Seric **	Parameters:
540c579ef51Seric **		pid -- pid of mailer.
541c579ef51Seric **		name -- name of mailer (for error messages).
542c579ef51Seric **
543c579ef51Seric **	Returns:
544c579ef51Seric **		exit code of mailer.
545c579ef51Seric **
546c579ef51Seric **	Side Effects:
547c579ef51Seric **		none.
548c579ef51Seric */
549c579ef51Seric 
550c579ef51Seric endmailer(pid, name)
551c579ef51Seric 	int pid;
552c579ef51Seric 	char *name;
553c579ef51Seric {
554c579ef51Seric 	register int i;
555c579ef51Seric 	auto int st;
556c579ef51Seric 
55733db8731Seric 	/* in the IPC case there is nothing to wait for */
55833db8731Seric 	if (pid == 0)
55933db8731Seric 		return (EX_OK);
56033db8731Seric 
56133db8731Seric 	/* wait for the mailer process to die and collect status */
562c579ef51Seric 	while ((i = wait(&st)) > 0 && i != pid)
563c579ef51Seric 		continue;
564c579ef51Seric 	if (i < 0)
565c579ef51Seric 	{
566c579ef51Seric 		syserr("wait");
567c579ef51Seric 		return (-1);
568c579ef51Seric 	}
56933db8731Seric 
57033db8731Seric 	/* see if it died a horrid death */
571c579ef51Seric 	if ((st & 0377) != 0)
572c579ef51Seric 	{
573c579ef51Seric 		syserr("%s: stat %o", name, st);
574c579ef51Seric 		ExitStat = EX_UNAVAILABLE;
575c579ef51Seric 		return (-1);
576c579ef51Seric 	}
57733db8731Seric 
57833db8731Seric 	/* normal death -- return status */
579c579ef51Seric 	i = (st >> 8) & 0377;
580c579ef51Seric 	return (i);
581c579ef51Seric }
582c579ef51Seric /*
583c579ef51Seric **  OPENMAILER -- open connection to mailer.
584c579ef51Seric **
585c579ef51Seric **	Parameters:
586c579ef51Seric **		m -- mailer descriptor.
587c579ef51Seric **		pvp -- parameter vector to pass to mailer.
588c579ef51Seric **		ctladdr -- controlling address for user.
589c579ef51Seric **		clever -- create a full duplex connection.
590c579ef51Seric **		pmfile -- pointer to mfile (to mailer) connection.
591c579ef51Seric **		prfile -- pointer to rfile (from mailer) connection.
592c579ef51Seric **
593c579ef51Seric **	Returns:
59433db8731Seric **		pid of mailer ( > 0 ).
595c579ef51Seric **		-1 on error.
59633db8731Seric **		zero on an IPC connection.
597c579ef51Seric **
598c579ef51Seric **	Side Effects:
599c579ef51Seric **		creates a mailer in a subprocess.
600c579ef51Seric */
601c579ef51Seric 
602c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
603c579ef51Seric 	struct mailer *m;
604c579ef51Seric 	char **pvp;
605c579ef51Seric 	ADDRESS *ctladdr;
606c579ef51Seric 	bool clever;
607c579ef51Seric 	FILE **pmfile;
608c579ef51Seric 	FILE **prfile;
609c579ef51Seric {
6105dfc646bSeric 	int pid;
611f8952a83Seric 	int mpvect[2];
612c579ef51Seric 	int rpvect[2];
6135dfc646bSeric 	FILE *mfile;
614c579ef51Seric 	FILE *rfile;
6155dfc646bSeric 	extern FILE *fdopen();
6165dfc646bSeric 
6175dfc646bSeric # ifdef DEBUG
6185dfc646bSeric 	if (Debug)
6195dfc646bSeric 	{
620c579ef51Seric 		printf("openmailer:\n");
6215dfc646bSeric 		printav(pvp);
6225dfc646bSeric 	}
6235dfc646bSeric # endif DEBUG
62435490626Seric 	errno = 0;
6255dfc646bSeric 
62633db8731Seric # ifdef DAEMON
62733db8731Seric 	/*
62833db8731Seric 	**  Deal with the special case of mail handled through an IPC
62933db8731Seric 	**  connection.
63033db8731Seric 	**	In this case we don't actually fork.  We must be
63133db8731Seric 	**	running SMTP for this to work.  We will return a
63233db8731Seric 	**	zero pid to indicate that we are running IPC.
63333db8731Seric 	*/
63433db8731Seric 
63533db8731Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
63633db8731Seric 	{
63733db8731Seric 		register int i;
6381277f9a8Seric 		register u_short port;
63933db8731Seric 
64033db8731Seric 		if (!clever)
64133db8731Seric 			syserr("non-clever IPC");
64293b6e3cfSeric 		if (pvp[2] != NULL)
6431277f9a8Seric 			port = atoi(pvp[2]);
64493b6e3cfSeric 		else
6451277f9a8Seric 			port = 0;
6461277f9a8Seric 		i = makeconnection(pvp[1], port, pmfile, prfile);
64733db8731Seric 		if (i != EX_OK)
648ed854c7bSeric 		{
649ed854c7bSeric 			ExitStat = i;
65033db8731Seric 			return (-1);
651ed854c7bSeric 		}
65233db8731Seric 		else
65333db8731Seric 			return (0);
65433db8731Seric 	}
65533db8731Seric # endif DAEMON
65633db8731Seric 
6576328bdf7Seric 	/* create a pipe to shove the mail through */
658f8952a83Seric 	if (pipe(mpvect) < 0)
65925a99e2eSeric 	{
660c579ef51Seric 		syserr("pipe (to mailer)");
66125a99e2eSeric 		return (-1);
66225a99e2eSeric 	}
663c579ef51Seric 
6642c7e1b8dSeric # ifdef SMTP
665c579ef51Seric 	/* if this mailer speaks smtp, create a return pipe */
666c579ef51Seric 	if (clever && pipe(rpvect) < 0)
667c579ef51Seric 	{
668c579ef51Seric 		syserr("pipe (from mailer)");
669c579ef51Seric 		(void) close(mpvect[0]);
670c579ef51Seric 		(void) close(mpvect[1]);
671c579ef51Seric 		return (-1);
672c579ef51Seric 	}
6732c7e1b8dSeric # endif SMTP
674c579ef51Seric 
67533db8731Seric 	/*
67633db8731Seric 	**  Actually fork the mailer process.
67733db8731Seric 	**	DOFORK is clever about retrying.
67833db8731Seric 	*/
67933db8731Seric 
680e1e0dfbfSeric 	fflush(Xscript);				/* for debugging */
68132d19d43Seric 	DOFORK(XFORK);
682f129ec7dSeric 	/* pid is set by DOFORK */
68325a99e2eSeric 	if (pid < 0)
68425a99e2eSeric 	{
68533db8731Seric 		/* failure */
68625a99e2eSeric 		syserr("Cannot fork");
687f8952a83Seric 		(void) close(mpvect[0]);
688f8952a83Seric 		(void) close(mpvect[1]);
689c579ef51Seric 		if (clever)
690c579ef51Seric 		{
691c579ef51Seric 			(void) close(rpvect[0]);
692c579ef51Seric 			(void) close(rpvect[1]);
693c579ef51Seric 		}
69425a99e2eSeric 		return (-1);
69525a99e2eSeric 	}
69625a99e2eSeric 	else if (pid == 0)
69725a99e2eSeric 	{
69825a99e2eSeric 		/* child -- set up input & exec mailer */
69903ab8e55Seric 		/* make diagnostic output be standard output */
7008f0e7860Seric 		(void) signal(SIGINT, SIG_IGN);
7018f0e7860Seric 		(void) signal(SIGHUP, SIG_IGN);
7020984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
703f8952a83Seric 
704f8952a83Seric 		/* arrange to filter standard & diag output of command */
705c579ef51Seric 		if (clever)
706c579ef51Seric 		{
707c579ef51Seric 			(void) close(rpvect[0]);
708c579ef51Seric 			(void) close(1);
709c579ef51Seric 			(void) dup(rpvect[1]);
710c579ef51Seric 			(void) close(rpvect[1]);
711c579ef51Seric 		}
712c579ef51Seric 		else if (OutChannel != stdout)
713f8952a83Seric 		{
714f8952a83Seric 			(void) close(1);
715f8952a83Seric 			(void) dup(fileno(OutChannel));
716f8952a83Seric 		}
717db8841e9Seric 		(void) close(2);
718db8841e9Seric 		(void) dup(1);
719f8952a83Seric 
720f8952a83Seric 		/* arrange to get standard input */
721f8952a83Seric 		(void) close(mpvect[1]);
722db8841e9Seric 		(void) close(0);
723f8952a83Seric 		if (dup(mpvect[0]) < 0)
72425a99e2eSeric 		{
72525a99e2eSeric 			syserr("Cannot dup to zero!");
726a590b978Seric 			_exit(EX_OSERR);
72725a99e2eSeric 		}
728f8952a83Seric 		(void) close(mpvect[0]);
7292a6e0786Seric 		if (!bitset(M_RESTR, m->m_flags))
7300984da9fSeric 		{
731e36b99e2Seric 			if (ctladdr->q_uid == 0)
732e36b99e2Seric 			{
733e36b99e2Seric 				(void) setgid(DefGid);
734e36b99e2Seric 				(void) setuid(DefUid);
735e36b99e2Seric 			}
736e36b99e2Seric 			else
73769f29479Seric 			{
738e36b99e2Seric 				(void) setgid(ctladdr->q_gid);
739e36b99e2Seric 				(void) setuid(ctladdr->q_uid);
74069f29479Seric 			}
7410984da9fSeric 		}
742e374fd72Seric # ifndef VFORK
743e374fd72Seric 		/*
744e374fd72Seric 		**  We have to be careful with vfork - we can't mung up the
745e374fd72Seric 		**  memory but we don't want the mailer to inherit any extra
746e374fd72Seric 		**  open files.  Chances are the mailer won't
747e374fd72Seric 		**  care about an extra file, but then again you never know.
748e374fd72Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
749e374fd72Seric 		**  declared static so we can't.  But if we fclose(pwf), which
750e374fd72Seric 		**  is what endpwent does, it closes it in the parent too and
751e374fd72Seric 		**  the next getpwnam will be slower.  If you have a weird
752e374fd72Seric 		**  mailer that chokes on the extra file you should do the
753e374fd72Seric 		**  endpwent().
754e374fd72Seric 		**
755e374fd72Seric 		**  Similar comments apply to log.  However, openlog is
756e374fd72Seric 		**  clever enough to set the FIOCLEX mode on the file,
757e374fd72Seric 		**  so it will be closed automatically on the exec.
758e374fd72Seric 		*/
759e374fd72Seric 
760e374fd72Seric 		endpwent();
76125a99e2eSeric # ifdef LOG
762f9fe028fSeric 		closelog();
76325a99e2eSeric # endif LOG
764e374fd72Seric # endif VFORK
76533db8731Seric 
76633db8731Seric 		/* try to execute the mailer */
76725a99e2eSeric 		execv(m->m_mailer, pvp);
76833db8731Seric 
76925a99e2eSeric 		/* syserr fails because log is closed */
77025a99e2eSeric 		/* syserr("Cannot exec %s", m->m_mailer); */
77132d19d43Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
772db8841e9Seric 		(void) fflush(stdout);
773a590b978Seric 		_exit(EX_UNAVAILABLE);
77425a99e2eSeric 	}
77525a99e2eSeric 
776f8952a83Seric 	/*
777c579ef51Seric 	**  Set up return value.
778f8952a83Seric 	*/
779f8952a83Seric 
780f8952a83Seric 	(void) close(mpvect[0]);
781f8952a83Seric 	mfile = fdopen(mpvect[1], "w");
782c579ef51Seric 	if (clever)
78325a99e2eSeric 	{
784c579ef51Seric 		(void) close(rpvect[1]);
785c579ef51Seric 		rfile = fdopen(rpvect[0], "r");
78625a99e2eSeric 	}
787c579ef51Seric 
788c579ef51Seric 	*pmfile = mfile;
789c579ef51Seric 	*prfile = rfile;
790c579ef51Seric 
791c579ef51Seric 	return (pid);
79225a99e2eSeric }
79325a99e2eSeric /*
79425a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
79525a99e2eSeric **
79625a99e2eSeric **	Parameters:
79725a99e2eSeric **		stat -- the status code from the mailer (high byte
79825a99e2eSeric **			only; core dumps must have been taken care of
79925a99e2eSeric **			already).
80025a99e2eSeric **		force -- if set, force an error message output, even
80125a99e2eSeric **			if the mailer seems to like to print its own
80225a99e2eSeric **			messages.
80325a99e2eSeric **		m -- the mailer descriptor for this mailer.
80425a99e2eSeric **
80525a99e2eSeric **	Returns:
806db8841e9Seric **		none.
80725a99e2eSeric **
80825a99e2eSeric **	Side Effects:
809c1f9df2cSeric **		Errors may be incremented.
81025a99e2eSeric **		ExitStat may be set.
81125a99e2eSeric */
81225a99e2eSeric 
81325a99e2eSeric giveresponse(stat, force, m)
81425a99e2eSeric 	int stat;
8154a3f06b2Seric 	bool force;
81625a99e2eSeric 	register struct mailer *m;
81725a99e2eSeric {
81825a99e2eSeric 	register char *statmsg;
81925a99e2eSeric 	extern char *SysExMsg[];
82025a99e2eSeric 	register int i;
82125a99e2eSeric 	extern int N_SysEx;
82229dd97a3Seric 	char buf[30];
82325a99e2eSeric 
82413bbc08cSeric 	/*
82513bbc08cSeric 	**  Compute status message from code.
82613bbc08cSeric 	*/
82713bbc08cSeric 
82825a99e2eSeric 	i = stat - EX__BASE;
82925a99e2eSeric 	if (i < 0 || i > N_SysEx)
83025a99e2eSeric 		statmsg = NULL;
83125a99e2eSeric 	else
83225a99e2eSeric 		statmsg = SysExMsg[i];
83325a99e2eSeric 	if (stat == 0)
834c38ba59cSeric 	{
8356cbfa739Seric 		if (bitset(M_LOCAL, m->m_flags))
8363efaed6eSeric 			statmsg = "delivered";
8373efaed6eSeric 		else
8383efaed6eSeric 			statmsg = "queued";
839544026c5Seric 		message(Arpa_Info, statmsg);
840c38ba59cSeric 	}
8412c7e1b8dSeric # ifdef QUEUE
842c77d1c25Seric 	else if (stat == EX_TEMPFAIL)
843c77d1c25Seric 	{
844c77d1c25Seric 		message(Arpa_Info, "transmission deferred");
845c77d1c25Seric 	}
8462c7e1b8dSeric # endif QUEUE
84725a99e2eSeric 	else
84825a99e2eSeric 	{
849c1f9df2cSeric 		Errors++;
850ed854c7bSeric 		FatalErrors = TRUE;
85125a99e2eSeric 		if (statmsg == NULL && m->m_badstat != 0)
85225a99e2eSeric 		{
85325a99e2eSeric 			stat = m->m_badstat;
85425a99e2eSeric 			i = stat - EX__BASE;
85525a99e2eSeric # ifdef DEBUG
85625a99e2eSeric 			if (i < 0 || i >= N_SysEx)
85725a99e2eSeric 				syserr("Bad m_badstat %d", stat);
85825a99e2eSeric 			else
85925a99e2eSeric # endif DEBUG
86025a99e2eSeric 				statmsg = SysExMsg[i];
86125a99e2eSeric 		}
86225a99e2eSeric 		if (statmsg == NULL)
86325a99e2eSeric 			usrerr("unknown mailer response %d", stat);
864c38ba59cSeric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
86525a99e2eSeric 			usrerr("%s", statmsg);
866793d9f34Seric 		else
867793d9f34Seric 			fprintf(Xscript, "%s\n", statmsg);
86825a99e2eSeric 	}
86925a99e2eSeric 
87025a99e2eSeric 	/*
87125a99e2eSeric 	**  Final cleanup.
87225a99e2eSeric 	**	Log a record of the transaction.  Compute the new
87325a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
87425a99e2eSeric 	**	that.
87525a99e2eSeric 	*/
87625a99e2eSeric 
87725a99e2eSeric 	if (statmsg == NULL)
87829dd97a3Seric 	{
879db8841e9Seric 		(void) sprintf(buf, "error %d", stat);
88029dd97a3Seric 		statmsg = buf;
88129dd97a3Seric 	}
88229dd97a3Seric 
88329dd97a3Seric # ifdef LOG
8847a941e7aSeric 	syslog(LOG_INFO, "%s->%s: %ld: %s", CurEnv->e_from.q_paddr, CurEnv->e_to, CurEnv->e_msgsize, statmsg);
88525a99e2eSeric # endif LOG
8862c7e1b8dSeric # ifdef QUEUE
887c77d1c25Seric 	if (stat != EX_TEMPFAIL)
8882c7e1b8dSeric # endif QUEUE
889243921eeSeric 		setstat(stat);
89025a99e2eSeric }
89125a99e2eSeric /*
89251552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
89325a99e2eSeric **
89451552439Seric **	This can be made an arbitrary message separator by changing $l
89551552439Seric **
89651552439Seric **	One of the ugliest hacks seen by human eyes is
89751552439Seric **	contained herein: UUCP wants those stupid
89851552439Seric **	"remote from <host>" lines.  Why oh why does a
89951552439Seric **	well-meaning programmer such as myself have to
90051552439Seric **	deal with this kind of antique garbage????
90125a99e2eSeric **
90225a99e2eSeric **	Parameters:
90351552439Seric **		fp -- the file to output to.
90451552439Seric **		m -- the mailer describing this entry.
90525a99e2eSeric **
90625a99e2eSeric **	Returns:
90751552439Seric **		none
90825a99e2eSeric **
90925a99e2eSeric **	Side Effects:
91051552439Seric **		outputs some text to fp.
91125a99e2eSeric */
91225a99e2eSeric 
91351552439Seric putfromline(fp, m)
91451552439Seric 	register FILE *fp;
91551552439Seric 	register MAILER *m;
91625a99e2eSeric {
91751552439Seric 	char buf[MAXLINE];
91825a99e2eSeric 
91951552439Seric 	if (bitset(M_NHDR, m->m_flags))
92051552439Seric 		return;
92113bbc08cSeric 
9222c7e1b8dSeric # ifdef UGLYUUCP
923a49f24c0Seric 	if (bitset(M_UGLYUUCP, m->m_flags))
92474b6e67bSeric 	{
92574b6e67bSeric 		extern char *macvalue();
92674b6e67bSeric 		char *sys = macvalue('g');
92774b6e67bSeric 		char *bang = index(sys, '!');
92874b6e67bSeric 
92974b6e67bSeric 		if (bang == NULL)
93074b6e67bSeric 			syserr("No ! in UUCP! (%s)", sys);
93174b6e67bSeric 		else
93274b6e67bSeric 			*bang = '\0';
933518ad6b6Seric 		expand("From $f  $d remote from $g\n", buf,
93451552439Seric 				&buf[sizeof buf - 1], CurEnv);
93574b6e67bSeric 		*bang = '!';
93674b6e67bSeric 	}
937a36e30c9Seric 	else
9382c7e1b8dSeric # endif UGLYUUCP
93951552439Seric 		expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv);
940d90b8d00Seric 	putline(buf, fp, bitset(M_FULLSMTP, m->m_flags));
941bc6e2962Seric }
942bc6e2962Seric /*
94351552439Seric **  PUTHEADER -- put the header part of a message from the in-core copy
944bc6e2962Seric **
945bc6e2962Seric **	Parameters:
946bc6e2962Seric **		fp -- file to put it on.
947bc6e2962Seric **		m -- mailer to use.
94851552439Seric **		e -- envelope to use.
949bc6e2962Seric **
950bc6e2962Seric **	Returns:
951bc6e2962Seric **		none.
952bc6e2962Seric **
953bc6e2962Seric **	Side Effects:
954bc6e2962Seric **		none.
955bc6e2962Seric */
956bc6e2962Seric 
95751552439Seric putheader(fp, m, e)
958bc6e2962Seric 	register FILE *fp;
959bc6e2962Seric 	register struct mailer *m;
96051552439Seric 	register ENVELOPE *e;
961bc6e2962Seric {
962bc6e2962Seric 	char buf[BUFSIZ];
963bc6e2962Seric 	register HDR *h;
964bc6e2962Seric 	extern char *arpadate();
965bc6e2962Seric 	extern char *capitalize();
966bc6e2962Seric 	extern char *hvalue();
967bc6e2962Seric 	extern bool samefrom();
968bc6e2962Seric 	char *of_line;
969d90b8d00Seric 	char obuf[MAXLINE];
970d90b8d00Seric 	register char *obp;
971d90b8d00Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
972bc6e2962Seric 
973894de7daSeric 	of_line = hvalue("original-from");
97451552439Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
9756328bdf7Seric 	{
97613bbc08cSeric 		register char *p;
97751552439Seric 		char *origfrom = e->e_origfrom;
978894de7daSeric 		bool nooutput;
97913bbc08cSeric 
980894de7daSeric 		nooutput = FALSE;
9819e9163a0Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
982894de7daSeric 			nooutput = TRUE;
983894de7daSeric 
984894de7daSeric 		/* use From: line from message if generated is the same */
98587bd9a02Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
986894de7daSeric 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
98787bd9a02Seric 		{
98887bd9a02Seric 			p = origfrom;
98987bd9a02Seric 			origfrom = NULL;
99087bd9a02Seric 		}
99187bd9a02Seric 		else if (bitset(H_DEFAULT, h->h_flags))
9924ae18d0eSeric 		{
993*b8a19582Seric 			/* macro expand value if generated internally */
99451552439Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
9954ae18d0eSeric 			p = buf;
9964ae18d0eSeric 		}
9978c2bf25bSeric 		else if (bitset(H_ADDR, h->h_flags))
9988c2bf25bSeric 		{
9998c2bf25bSeric 			register int opos;
10008c2bf25bSeric 			bool firstone = TRUE;
10018c2bf25bSeric 
10028c2bf25bSeric 			/*
10038c2bf25bSeric 			**  Output the address list translated by the
10048c2bf25bSeric 			**  mailer and with commas.
10058c2bf25bSeric 			*/
10068c2bf25bSeric 
10078c2bf25bSeric 			p = h->h_value;
10088c2bf25bSeric 			if (p == NULL || *p == '\0' || nooutput)
10098c2bf25bSeric 				continue;
1010d90b8d00Seric 			obp = obuf;
10111277f9a8Seric 			(void) sprintf(obp, "%s: ", capitalize(h->h_field));
10128c2bf25bSeric 			opos = strlen(h->h_field) + 2;
1013d90b8d00Seric 			obp += opos;
1014*b8a19582Seric 
1015*b8a19582Seric 			/*
1016*b8a19582Seric 			**  Run through the list of values.
1017*b8a19582Seric 			*/
1018*b8a19582Seric 
10198c2bf25bSeric 			while (*p != '\0')
10208c2bf25bSeric 			{
1021*b8a19582Seric 				register char *name;
10228c2bf25bSeric 				extern char *remotename();
10238c2bf25bSeric 				char savechar;
10248c2bf25bSeric 
1025*b8a19582Seric 				/*
1026*b8a19582Seric 				**  Find the end of the name.  New style names
1027*b8a19582Seric 				**  end with a comma, old style names end with
1028*b8a19582Seric 				**  a space character.  However, spaces do not
1029*b8a19582Seric 				**  necessarily delimit an old-style name -- at
1030*b8a19582Seric 				**  signs mean keep going.
1031*b8a19582Seric 				*/
1032*b8a19582Seric 
1033*b8a19582Seric 				/* clean up the leading trash in source */
1034*b8a19582Seric 				while (*p != '\0' && (isspace(*p) || *p == ','))
1035*b8a19582Seric 					p++;
1036*b8a19582Seric 				name = p;
1037*b8a19582Seric 
1038*b8a19582Seric 				/* find end of name */
1039bc28c57aSeric 				while (*p != '\0' && *p != ',')
1040bc28c57aSeric 				{
1041bc28c57aSeric 					extern bool isatword();
1042bc28c57aSeric 					char *oldp;
1043bc28c57aSeric 
104451552439Seric 					if (!e->e_oldstyle || !isspace(*p))
1045bc28c57aSeric 					{
10468c2bf25bSeric 						p++;
1047bc28c57aSeric 						continue;
1048bc28c57aSeric 					}
1049*b8a19582Seric 
1050*b8a19582Seric 					/* look to see if we have an at sign */
1051bc28c57aSeric 					oldp = p;
1052bc28c57aSeric 					while (*p != '\0' && isspace(*p))
1053bc28c57aSeric 						p++;
1054*b8a19582Seric 
1055bc28c57aSeric 					if (*p != '@' && !isatword(p))
1056bc28c57aSeric 					{
1057bc28c57aSeric 						p = oldp;
1058bc28c57aSeric 						break;
1059bc28c57aSeric 					}
1060bc28c57aSeric 					p += *p == '@' ? 1 : 2;
1061bc28c57aSeric 					while (*p != '\0' && isspace(*p))
1062bc28c57aSeric 						p++;
1063bc28c57aSeric 				}
1064*b8a19582Seric 				/* at the end of one complete name */
1065*b8a19582Seric 
1066*b8a19582Seric 				/* strip off trailing white space */
1067*b8a19582Seric 				while (p >= name && (isspace(*p) || *p == ','))
1068*b8a19582Seric 					p--;
1069*b8a19582Seric 				if (++p == name)
1070*b8a19582Seric 					continue;
10718c2bf25bSeric 				savechar = *p;
10728c2bf25bSeric 				*p = '\0';
10738c2bf25bSeric 
10748c2bf25bSeric 				/* translate the name to be relative */
1075f60462a7Seric 				name = remotename(name, m, FALSE);
10768c2bf25bSeric 				if (*name == '\0')
1077*b8a19582Seric 				{
1078*b8a19582Seric 					*p = savechar;
10798c2bf25bSeric 					continue;
1080*b8a19582Seric 				}
10818c2bf25bSeric 
10828c2bf25bSeric 				/* output the name with nice formatting */
10838c2bf25bSeric 				opos += strlen(name);
10848c2bf25bSeric 				if (!firstone)
10858c2bf25bSeric 					opos += 2;
10868c2bf25bSeric 				if (opos > 78 && !firstone)
10878c2bf25bSeric 				{
1088d90b8d00Seric 					(void) sprintf(obp, ",\n");
1089d90b8d00Seric 					putline(obuf, fp, fullsmtp);
1090d90b8d00Seric 					obp = obuf;
1091d90b8d00Seric 					(void) sprintf(obp, "        ");
1092d90b8d00Seric 					obp += strlen(obp);
1093bc213ac6Seric 					opos = 8 + strlen(name);
10948c2bf25bSeric 				}
10958c2bf25bSeric 				else if (!firstone)
1096d90b8d00Seric 				{
1097d90b8d00Seric 					(void) sprintf(obp, ", ");
1098d90b8d00Seric 					obp += 2;
1099d90b8d00Seric 				}
1100d90b8d00Seric 				(void) sprintf(obp, "%s", name);
1101d90b8d00Seric 				obp += strlen(obp);
11028c2bf25bSeric 				firstone = FALSE;
11038c2bf25bSeric 				*p = savechar;
11048c2bf25bSeric 			}
11051277f9a8Seric 			(void) strcpy(obp, "\n");
1106573fa002Seric 			putline(obuf, fp, fullsmtp);
11078c2bf25bSeric 			nooutput = TRUE;
11088c2bf25bSeric 		}
11094ae18d0eSeric 		else
11104ae18d0eSeric 			p = h->h_value;
1111894de7daSeric 		if (p == NULL || *p == '\0')
11129e9163a0Seric 			continue;
11135a0dcb5fSeric 
11145a0dcb5fSeric 		/* hack, hack -- output Original-From field if different */
1115894de7daSeric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
1116894de7daSeric 		{
1117894de7daSeric 			/* output new Original-From line if needed */
1118894de7daSeric 			if (of_line == NULL && !samefrom(p, origfrom))
1119d90b8d00Seric 			{
1120d90b8d00Seric 				(void) sprintf(obuf, "Original-From: %s\n", origfrom);
1121d90b8d00Seric 				putline(obuf, fp, fullsmtp);
1122d90b8d00Seric 			}
1123894de7daSeric 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
1124894de7daSeric 			{
1125894de7daSeric 				/* delete Original-From: line if redundant */
1126894de7daSeric 				p = of_line;
1127894de7daSeric 				of_line = NULL;
1128894de7daSeric 			}
1129894de7daSeric 		}
1130894de7daSeric 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
1131894de7daSeric 			nooutput = TRUE;
1132894de7daSeric 
1133894de7daSeric 		/* finally, output the header line */
1134894de7daSeric 		if (!nooutput)
1135894de7daSeric 		{
1136d90b8d00Seric 			(void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p);
1137d90b8d00Seric 			putline(obuf, fp, fullsmtp);
1138894de7daSeric 			h->h_flags |= H_USED;
1139894de7daSeric 		}
11406328bdf7Seric 	}
114151552439Seric }
114251552439Seric /*
114351552439Seric **  PUTBODY -- put the body of a message.
114451552439Seric **
114551552439Seric **	Parameters:
114651552439Seric **		fp -- file to output onto.
114751552439Seric **		m -- a mailer descriptor.
114851552439Seric **		xdot -- if set, use SMTP hidden dot algorithm.
114951552439Seric **
115051552439Seric **	Returns:
115151552439Seric **		none.
115251552439Seric **
115351552439Seric **	Side Effects:
115451552439Seric **		The message is written onto fp.
115551552439Seric */
115651552439Seric 
115751552439Seric putbody(fp, m, xdot)
115851552439Seric 	FILE *fp;
115951552439Seric 	struct mailer *m;
116051552439Seric 	bool xdot;
116151552439Seric {
116251552439Seric 	char buf[MAXLINE + 1];
1163d90b8d00Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
116451552439Seric 
116551552439Seric 	/*
116651552439Seric 	**  Output the body of the message
116751552439Seric 	*/
116851552439Seric 
11695e663df1Seric #ifdef lint
11705e663df1Seric 	/* m will be needed later for complete smtp emulation */
11715e663df1Seric 	if (m == NULL)
11725e663df1Seric 		return;
11735e663df1Seric #endif lint
11745e663df1Seric 
117551552439Seric 	if (TempFile != NULL)
117651552439Seric 	{
117751552439Seric 		rewind(TempFile);
117851552439Seric 		buf[0] = '.';
117951552439Seric 		while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL)
1180d90b8d00Seric 			putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp);
118151552439Seric 
118251552439Seric 		if (ferror(TempFile))
118351552439Seric 		{
118451552439Seric 			syserr("putbody: read error");
118551552439Seric 			ExitStat = EX_IOERR;
118651552439Seric 		}
118751552439Seric 	}
118851552439Seric 
118951552439Seric 	(void) fflush(fp);
119051552439Seric 	if (ferror(fp) && errno != EPIPE)
119151552439Seric 	{
119251552439Seric 		syserr("putbody: write error");
119351552439Seric 		ExitStat = EX_IOERR;
119451552439Seric 	}
119551552439Seric 	errno = 0;
119625a99e2eSeric }
119725a99e2eSeric /*
1198bc28c57aSeric **  ISATWORD -- tell if the word we are pointing to is "at".
1199bc28c57aSeric **
1200bc28c57aSeric **	Parameters:
1201bc28c57aSeric **		p -- word to check.
1202bc28c57aSeric **
1203bc28c57aSeric **	Returns:
1204bc28c57aSeric **		TRUE -- if p is the word at.
1205bc28c57aSeric **		FALSE -- otherwise.
1206bc28c57aSeric **
1207bc28c57aSeric **	Side Effects:
1208bc28c57aSeric **		none.
1209bc28c57aSeric */
1210bc28c57aSeric 
1211bc28c57aSeric bool
1212bc28c57aSeric isatword(p)
1213bc28c57aSeric 	register char *p;
1214bc28c57aSeric {
1215bc28c57aSeric 	extern char lower();
1216bc28c57aSeric 
1217bc28c57aSeric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
1218bc28c57aSeric 	    p[2] != '\0' && isspace(p[2]))
1219bc28c57aSeric 		return (TRUE);
1220bc28c57aSeric 	return (FALSE);
1221bc28c57aSeric }
1222bc28c57aSeric /*
12238c2bf25bSeric **  REMOTENAME -- return the name relative to the current mailer
12248c2bf25bSeric **
12258c2bf25bSeric **	Parameters:
12268c2bf25bSeric **		name -- the name to translate.
1227f60462a7Seric **		force -- if set, forces rewriting even if the mailer
1228f60462a7Seric **			does not request it.  Used for rewriting
1229f60462a7Seric **			sender addresses.
12308c2bf25bSeric **
12318c2bf25bSeric **	Returns:
12328c2bf25bSeric **		the text string representing this address relative to
12338c2bf25bSeric **			the receiving mailer.
12348c2bf25bSeric **
12358c2bf25bSeric **	Side Effects:
12368c2bf25bSeric **		none.
12378c2bf25bSeric **
12388c2bf25bSeric **	Warnings:
12398c2bf25bSeric **		The text string returned is tucked away locally;
12408c2bf25bSeric **			copy it if you intend to save it.
12418c2bf25bSeric */
12428c2bf25bSeric 
12438c2bf25bSeric char *
1244f60462a7Seric remotename(name, m, force)
12458c2bf25bSeric 	char *name;
1246bc213ac6Seric 	struct mailer *m;
1247f60462a7Seric 	bool force;
12488c2bf25bSeric {
12498c2bf25bSeric 	static char buf[MAXNAME];
12508c2bf25bSeric 	char lbuf[MAXNAME];
12518c2bf25bSeric 	extern char *macvalue();
12528c2bf25bSeric 	char *oldf = macvalue('f');
1253bc213ac6Seric 	char *oldx = macvalue('x');
1254bc213ac6Seric 	char *oldg = macvalue('g');
12558c2bf25bSeric 	extern char **prescan();
12568c2bf25bSeric 	register char **pvp;
1257bc213ac6Seric 	extern char *getxpart();
12585e663df1Seric 	extern ADDRESS *buildaddr();
12598c2bf25bSeric 
12608c2bf25bSeric 	/*
1261f60462a7Seric 	**  See if this mailer wants the name to be rewritten.  There are
1262f60462a7Seric 	**  many problems here, owing to the standards for doing replies.
1263f60462a7Seric 	**  In general, these names should only be rewritten if we are
1264f60462a7Seric 	**  sending to another host that runs sendmail.
1265f60462a7Seric 	*/
1266f60462a7Seric 
1267f60462a7Seric 	if (!bitset(M_RELRCPT, m->m_flags) && !force)
12685e663df1Seric 		return (name);
1269f60462a7Seric 
1270f60462a7Seric 	/*
12718c2bf25bSeric 	**  Do general rewriting of name.
12728c2bf25bSeric 	**	This will also take care of doing global name translation.
12738c2bf25bSeric 	*/
12748c2bf25bSeric 
1275bc213ac6Seric 	define('x', getxpart(name));
12768c2bf25bSeric 	pvp = prescan(name, '\0');
1277b3069faaSeric 	if (pvp == NULL)
1278b3069faaSeric 		return (name);
12798c2bf25bSeric 	rewrite(pvp, 1);
12808c2bf25bSeric 	rewrite(pvp, 3);
12818c2bf25bSeric 	if (**pvp == CANONNET)
12828c2bf25bSeric 	{
12838c2bf25bSeric 		/* oops... resolved to something */
12848c2bf25bSeric 		return (name);
12858c2bf25bSeric 	}
12868c2bf25bSeric 	cataddr(pvp, lbuf, sizeof lbuf);
12878c2bf25bSeric 
12888c2bf25bSeric 	/* make the name relative to the receiving mailer */
12898c2bf25bSeric 	define('f', lbuf);
129051552439Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
12918c2bf25bSeric 
12928c2bf25bSeric 	/* rewrite to get rid of garbage we added in the expand above */
12938c2bf25bSeric 	pvp = prescan(buf, '\0');
1294b3069faaSeric 	if (pvp == NULL)
1295b3069faaSeric 		return (name);
12968c2bf25bSeric 	rewrite(pvp, 2);
1297bc213ac6Seric 	cataddr(pvp, lbuf, sizeof lbuf);
1298bc213ac6Seric 
1299bc213ac6Seric 	/* now add any comment info we had before back */
1300bc213ac6Seric 	define('g', lbuf);
130151552439Seric 	expand("$q", buf, &buf[sizeof buf - 1], CurEnv);
13028c2bf25bSeric 
13038c2bf25bSeric 	define('f', oldf);
1304bc213ac6Seric 	define('g', oldg);
1305bc213ac6Seric 	define('x', oldx);
13068c2bf25bSeric 
13078c2bf25bSeric # ifdef DEBUG
13088c2bf25bSeric 	if (Debug > 0)
13098c2bf25bSeric 		printf("remotename(%s) => `%s'\n", name, buf);
13108c2bf25bSeric # endif DEBUG
13118c2bf25bSeric 	return (buf);
13128c2bf25bSeric }
13138c2bf25bSeric /*
131487bd9a02Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
131587bd9a02Seric **
131687bd9a02Seric **	Parameters:
131787bd9a02Seric **		ifrom -- internally generated form of from address.
131887bd9a02Seric **		efrom -- external form of from address.
131987bd9a02Seric **
132087bd9a02Seric **	Returns:
132187bd9a02Seric **		TRUE -- if they convey the same info.
132287bd9a02Seric **		FALSE -- if any information has been lost.
132387bd9a02Seric **
132487bd9a02Seric **	Side Effects:
132587bd9a02Seric **		none.
132687bd9a02Seric */
132787bd9a02Seric 
132887bd9a02Seric bool
132987bd9a02Seric samefrom(ifrom, efrom)
133087bd9a02Seric 	char *ifrom;
133187bd9a02Seric 	char *efrom;
133287bd9a02Seric {
1333894de7daSeric 	register char *p;
1334894de7daSeric 	char buf[MAXNAME + 4];
1335894de7daSeric 
1336894de7daSeric # ifdef DEBUG
1337894de7daSeric 	if (Debug > 7)
1338894de7daSeric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
1339894de7daSeric # endif DEBUG
1340894de7daSeric 	if (strcmp(ifrom, efrom) == 0)
1341894de7daSeric 		goto success;
1342894de7daSeric 	p = index(ifrom, '@');
1343894de7daSeric 	if (p == NULL)
1344894de7daSeric 		goto failure;
1345894de7daSeric 	*p = '\0';
13465e663df1Seric 	(void) strcpy(buf, ifrom);
13475e663df1Seric 	(void) strcat(buf, " at ");
1348894de7daSeric 	*p++ = '@';
13495e663df1Seric 	(void) strcat(buf, p);
1350894de7daSeric 	if (strcmp(buf, efrom) == 0)
1351894de7daSeric 		goto success;
1352894de7daSeric 
1353894de7daSeric   failure:
1354894de7daSeric # ifdef DEBUG
1355894de7daSeric 	if (Debug > 7)
1356894de7daSeric 		printf("FALSE\n");
1357894de7daSeric # endif DEBUG
1358894de7daSeric 	return (FALSE);
1359894de7daSeric 
1360894de7daSeric   success:
1361894de7daSeric # ifdef DEBUG
1362894de7daSeric 	if (Debug > 7)
1363894de7daSeric 		printf("TRUE\n");
1364894de7daSeric # endif DEBUG
1365894de7daSeric 	return (TRUE);
136687bd9a02Seric }
136787bd9a02Seric /*
136825a99e2eSeric **  MAILFILE -- Send a message to a file.
136925a99e2eSeric **
1370f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1371f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1372f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1373f129ec7dSeric **	sendmail runs as root.
1374f129ec7dSeric **
137525a99e2eSeric **	Parameters:
137625a99e2eSeric **		filename -- the name of the file to send to.
13776259796dSeric **		ctladdr -- the controlling address header -- includes
13786259796dSeric **			the userid/groupid to be when sending.
137925a99e2eSeric **
138025a99e2eSeric **	Returns:
138125a99e2eSeric **		The exit code associated with the operation.
138225a99e2eSeric **
138325a99e2eSeric **	Side Effects:
138425a99e2eSeric **		none.
138525a99e2eSeric */
138625a99e2eSeric 
13876259796dSeric mailfile(filename, ctladdr)
138825a99e2eSeric 	char *filename;
13896259796dSeric 	ADDRESS *ctladdr;
139025a99e2eSeric {
139125a99e2eSeric 	register FILE *f;
139232d19d43Seric 	register int pid;
139325a99e2eSeric 
139432d19d43Seric 	/*
139532d19d43Seric 	**  Fork so we can change permissions here.
139632d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
139732d19d43Seric 	**	the complications of calling subroutines, etc.
139832d19d43Seric 	*/
139932d19d43Seric 
140032d19d43Seric 	DOFORK(fork);
140132d19d43Seric 
140232d19d43Seric 	if (pid < 0)
140332d19d43Seric 		return (EX_OSERR);
140432d19d43Seric 	else if (pid == 0)
140532d19d43Seric 	{
140632d19d43Seric 		/* child -- actually write to file */
1407f129ec7dSeric 		struct stat stb;
1408f129ec7dSeric 
14090984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
14100984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
14110984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
1412f129ec7dSeric 		umask(OldUmask);
1413f129ec7dSeric 		if (stat(filename, &stb) < 0)
1414e6e1265fSeric 			stb.st_mode = 0666;
1415f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1416f129ec7dSeric 			exit(EX_CANTCREAT);
141703827b5fSeric 		if (ctladdr == NULL)
14187a941e7aSeric 			ctladdr = &CurEnv->e_from;
1419f129ec7dSeric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
1420e36b99e2Seric 		{
1421e36b99e2Seric 			if (ctladdr->q_uid == 0)
1422e36b99e2Seric 				(void) setgid(DefGid);
1423e36b99e2Seric 			else
14246259796dSeric 				(void) setgid(ctladdr->q_gid);
1425e36b99e2Seric 		}
1426f129ec7dSeric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
1427e36b99e2Seric 		{
1428e36b99e2Seric 			if (ctladdr->q_uid == 0)
1429e36b99e2Seric 				(void) setuid(DefUid);
1430e36b99e2Seric 			else
14316259796dSeric 				(void) setuid(ctladdr->q_uid);
1432e36b99e2Seric 		}
143327628d59Seric 		f = dfopen(filename, "a");
143425a99e2eSeric 		if (f == NULL)
143532d19d43Seric 			exit(EX_CANTCREAT);
143625a99e2eSeric 
143751552439Seric 		putfromline(f, Mailer[1]);
143851552439Seric 		(*CurEnv->e_puthdr)(f, Mailer[1], CurEnv);
1439d90b8d00Seric 		fputs("\n", f);
144051552439Seric 		(*CurEnv->e_putbody)(f, Mailer[1], FALSE);
144125a99e2eSeric 		fputs("\n", f);
1442db8841e9Seric 		(void) fclose(f);
144332d19d43Seric 		(void) fflush(stdout);
1444e36b99e2Seric 
144527628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1446c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
144732d19d43Seric 		exit(EX_OK);
144813bbc08cSeric 		/*NOTREACHED*/
144932d19d43Seric 	}
145032d19d43Seric 	else
145132d19d43Seric 	{
145232d19d43Seric 		/* parent -- wait for exit status */
145332d19d43Seric 		register int i;
145432d19d43Seric 		auto int stat;
145532d19d43Seric 
145632d19d43Seric 		while ((i = wait(&stat)) != pid)
145732d19d43Seric 		{
145832d19d43Seric 			if (i < 0)
145932d19d43Seric 			{
146032d19d43Seric 				stat = EX_OSERR << 8;
146132d19d43Seric 				break;
146232d19d43Seric 			}
146332d19d43Seric 		}
14640984da9fSeric 		if ((stat & 0377) != 0)
14650984da9fSeric 			stat = EX_UNAVAILABLE << 8;
146632d19d43Seric 		return ((stat >> 8) & 0377);
146732d19d43Seric 	}
146825a99e2eSeric }
1469ea4dc939Seric /*
1470ea4dc939Seric **  SENDALL -- actually send all the messages.
1471ea4dc939Seric **
1472ea4dc939Seric **	Parameters:
14730c52a0b3Seric **		e -- the envelope to send.
1474ea4dc939Seric **		verifyonly -- if set, only give verification messages.
1475ea4dc939Seric **
1476ea4dc939Seric **	Returns:
1477ea4dc939Seric **		none.
1478ea4dc939Seric **
1479ea4dc939Seric **	Side Effects:
1480ea4dc939Seric **		Scans the send lists and sends everything it finds.
14810c52a0b3Seric **		Delivers any appropriate error messages.
1482ea4dc939Seric */
1483ea4dc939Seric 
14840c52a0b3Seric sendall(e, verifyonly)
14850c52a0b3Seric 	ENVELOPE *e;
1486ea4dc939Seric 	bool verifyonly;
1487ea4dc939Seric {
1488e77e673fSeric 	register ADDRESS *q;
1489ea4dc939Seric 
1490772e6e50Seric # ifdef DEBUG
1491772e6e50Seric 	if (Debug > 1)
1492772e6e50Seric 	{
1493772e6e50Seric 		printf("\nSend Queue:\n");
14940c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1495772e6e50Seric 	}
1496772e6e50Seric # endif DEBUG
1497ea4dc939Seric 
14980c52a0b3Seric 	/*
14990c52a0b3Seric 	**  Run through the list and send everything.
15000c52a0b3Seric 	*/
15010c52a0b3Seric 
15020c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1503ea4dc939Seric 	{
1504ea4dc939Seric 		if (verifyonly)
1505ea4dc939Seric 		{
15067a941e7aSeric 			CurEnv->e_to = q->q_paddr;
1507e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1508ea4dc939Seric 				message(Arpa_Info, "deliverable");
1509ea4dc939Seric 		}
1510ea4dc939Seric 		else
151151552439Seric 			(void) deliver(q);
1512ea4dc939Seric 	}
15130c52a0b3Seric 
15140c52a0b3Seric 	/*
15150c52a0b3Seric 	**  Now run through and check for errors.
15160c52a0b3Seric 	*/
15170c52a0b3Seric 
15180c52a0b3Seric 	if (verifyonly)
15190c52a0b3Seric 		return;
15200c52a0b3Seric 
15210c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15220c52a0b3Seric 	{
15230c52a0b3Seric 		register ADDRESS *qq;
15240c52a0b3Seric 
15250c52a0b3Seric 		if (bitset(QQUEUEUP, q->q_flags))
15260c52a0b3Seric 			e->e_queueup = TRUE;
15270c52a0b3Seric 		if (!bitset(QBADADDR, q->q_flags))
15280c52a0b3Seric 			continue;
15290c52a0b3Seric 
15300c52a0b3Seric 		/* we have an address that failed -- find the parent */
15310c52a0b3Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
15320c52a0b3Seric 		{
15330c52a0b3Seric 			char obuf[MAXNAME + 6];
15340c52a0b3Seric 			extern char *aliaslookup();
15350c52a0b3Seric 
15360c52a0b3Seric 			/* we can only have owners for local addresses */
15370c52a0b3Seric 			if (!bitset(M_LOCAL, qq->q_mailer->m_flags))
15380c52a0b3Seric 				continue;
15390c52a0b3Seric 
15400c52a0b3Seric 			/* see if the owner list exists */
15410c52a0b3Seric 			(void) strcpy(obuf, "owner-");
1542cec031e3Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1543cec031e3Seric 				(void) strcat(obuf, "owner");
1544cec031e3Seric 			else
15450c52a0b3Seric 				(void) strcat(obuf, qq->q_user);
15460c52a0b3Seric 			if (aliaslookup(obuf) == NULL)
15470c52a0b3Seric 				continue;
15480c52a0b3Seric 
15490c52a0b3Seric 			/* owner list exists -- add it to the error queue */
15500c52a0b3Seric 			qq->q_flags &= ~QPRIMARY;
15510c52a0b3Seric 			sendto(obuf, 1, qq, &e->e_errorqueue);
15520c52a0b3Seric 			MailBack = TRUE;
15530c52a0b3Seric 			break;
15540c52a0b3Seric 		}
15550c52a0b3Seric 
15560c52a0b3Seric 		/* if we did not find an owner, send to the sender */
15570c52a0b3Seric 		if (qq == NULL)
15580c52a0b3Seric 			sendto(e->e_from.q_paddr, 1, qq, &e->e_errorqueue);
15590c52a0b3Seric 	}
15600c52a0b3Seric }
15610c52a0b3Seric /*
15620c52a0b3Seric **  CHECKERRORS -- check a queue of addresses and process errors.
15630c52a0b3Seric **
15640c52a0b3Seric **	Parameters:
15650c52a0b3Seric **		e -- the envelope to check.
15660c52a0b3Seric **
15670c52a0b3Seric **	Returns:
15680c52a0b3Seric **		none.
15690c52a0b3Seric **
15700c52a0b3Seric **	Side Effects:
15710c52a0b3Seric **		Arranges to queue all tempfailed messages in q
15720c52a0b3Seric **			or deliver error responses.
15730c52a0b3Seric */
15740c52a0b3Seric 
15750c52a0b3Seric checkerrors(e)
15760c52a0b3Seric 	register ENVELOPE *e;
15770c52a0b3Seric {
15780c52a0b3Seric # ifdef DEBUG
15790c52a0b3Seric 	if (Debug > 0)
15800c52a0b3Seric 	{
1581e1e0dfbfSeric 		printf("\ncheckerrors: FatalErrors %d, errorqueue:\n");
15820c52a0b3Seric 		printaddr(e->e_errorqueue, TRUE);
15830c52a0b3Seric 	}
15840c52a0b3Seric # endif DEBUG
15850c52a0b3Seric 
15860c52a0b3Seric 	/* mail back the transcript on errors */
15870c52a0b3Seric 	if (FatalErrors)
15880c52a0b3Seric 		savemail();
15890c52a0b3Seric 
15900c52a0b3Seric 	/* queue up anything laying around */
15910c52a0b3Seric 	if (e->e_queueup)
15920c52a0b3Seric 	{
15930c52a0b3Seric # ifdef QUEUE
15940c52a0b3Seric 		queueup(e, FALSE);
15950c52a0b3Seric # else QUEUE
15960c52a0b3Seric 		syserr("finis: trying to queue %s", e->e_df);
15970c52a0b3Seric # endif QUEUE
15980c52a0b3Seric 	}
1599ea4dc939Seric }
1600