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*33db8731Seric SCCSID(@(#)deliver.c	3.70		03/05/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 **		editfcn -- if non-NULL, we want to call this function
2425a99e2eSeric **			to output the letter (instead of just out-
2525a99e2eSeric **			putting it raw).
2625a99e2eSeric **
2725a99e2eSeric **	Returns:
2825a99e2eSeric **		zero -- successfully delivered.
2925a99e2eSeric **		else -- some failure, see ExitStat for more info.
3025a99e2eSeric **
3125a99e2eSeric **	Side Effects:
3225a99e2eSeric **		The standard input is passed off to someone.
3325a99e2eSeric */
3425a99e2eSeric 
35c77d1c25Seric deliver(firstto, editfcn)
36c77d1c25Seric 	ADDRESS *firstto;
3725a99e2eSeric 	int (*editfcn)();
3825a99e2eSeric {
3978442df3Seric 	char *host;			/* host being sent to */
4078442df3Seric 	char *user;			/* user being sent to */
4125a99e2eSeric 	char **pvp;
425dfc646bSeric 	register char **mvp;
4325a99e2eSeric 	register char *p;
4478442df3Seric 	register struct mailer *m;	/* mailer for this recipient */
455dfc646bSeric 	register int i;
466328bdf7Seric 	extern putmessage();
472a6e0786Seric 	extern bool checkcompat();
485dfc646bSeric 	char *pv[MAXPV+1];
4978442df3Seric 	char tobuf[MAXLINE];		/* text line of to people */
505dfc646bSeric 	char buf[MAXNAME];
516259796dSeric 	ADDRESS *ctladdr;
526259796dSeric 	extern ADDRESS *getctladdr();
5378442df3Seric 	char tfrombuf[MAXNAME];		/* translated from person */
5478442df3Seric 	extern char **prescan();
55c77d1c25Seric 	register ADDRESS *to = firstto;
56c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
57c579ef51Seric 	bool tempfail = FALSE;
58772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
5925a99e2eSeric 
6035490626Seric 	errno = 0;
61e77e673fSeric 	if (!ForceMail && bitset(QDONTSEND, to->q_flags))
625dfc646bSeric 		return (0);
6325a99e2eSeric 
6425a99e2eSeric # ifdef DEBUG
6525a99e2eSeric 	if (Debug)
665dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
677da1035fSeric 			to->q_mailer->m_mno, to->q_host, to->q_user);
6825a99e2eSeric # endif DEBUG
6925a99e2eSeric 
70f3dbc832Seric 	m = to->q_mailer;
71f3dbc832Seric 	host = to->q_host;
72f3dbc832Seric 
73f3dbc832Seric 	/*
74f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
75f3dbc832Seric 	**  connections now, just mark these addresses and return.
76f3dbc832Seric 	**	This is useful if we want to batch connections to
77f3dbc832Seric 	**	reduce load.  This will cause the messages to be
78f3dbc832Seric 	**	queued up, and a daemon will come along to send the
79f3dbc832Seric 	**	messages later.
80f3dbc832Seric 	**		This should be on a per-mailer basis.
81f3dbc832Seric 	*/
82f3dbc832Seric 
83f3dbc832Seric 	if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags))
84f3dbc832Seric 	{
85f3dbc832Seric 		QueueUp = TRUE;
86f3dbc832Seric 		for (; to != NULL; to = to->q_next)
87f3dbc832Seric 			if (!bitset(QDONTSEND, to->q_flags))
88f3dbc832Seric 				to->q_flags |= QQUEUEUP|QDONTSEND;
89f3dbc832Seric 		return (0);
90f3dbc832Seric 	}
91f3dbc832Seric 
9225a99e2eSeric 	/*
935dfc646bSeric 	**  Do initial argv setup.
945dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
955dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
965dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
975dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
985dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
99f3dbc832Seric 	**		We rewrite the from address here, being careful
100f3dbc832Seric 	**		to also rewrite it again using ruleset 2 to
101f3dbc832Seric 	**		eliminate redundancies.
1025dfc646bSeric 	*/
1035dfc646bSeric 
10478442df3Seric 	/* rewrite from address, using rewriting rules */
10578442df3Seric 	(void) expand(m->m_from, buf, &buf[sizeof buf - 1]);
10678442df3Seric 	mvp = prescan(buf, '\0');
10778442df3Seric 	if (mvp == NULL)
10878442df3Seric 	{
10978442df3Seric 		syserr("bad mailer from translate \"%s\"", buf);
11078442df3Seric 		return (EX_SOFTWARE);
11178442df3Seric 	}
11278442df3Seric 	rewrite(mvp, 2);
11378442df3Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
11478442df3Seric 
11578442df3Seric 	define('g', tfrombuf);		/* translated sender address */
1165dfc646bSeric 	define('h', host);		/* to host */
1175dfc646bSeric 	Errors = 0;
1185dfc646bSeric 	pvp = pv;
1195dfc646bSeric 	*pvp++ = m->m_argv[0];
1205dfc646bSeric 
1215dfc646bSeric 	/* insert -f or -r flag as appropriate */
1225dfc646bSeric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1235dfc646bSeric 	{
1245dfc646bSeric 		if (bitset(M_FOPT, m->m_flags))
1255dfc646bSeric 			*pvp++ = "-f";
1265dfc646bSeric 		else
1275dfc646bSeric 			*pvp++ = "-r";
128db8841e9Seric 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
1295dfc646bSeric 		*pvp++ = newstr(buf);
1305dfc646bSeric 	}
1315dfc646bSeric 
1325dfc646bSeric 	/*
1335dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1345dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1355dfc646bSeric 	**  be one of these, and there are only a few more slots
1365dfc646bSeric 	**  in the pv after it.
1375dfc646bSeric 	*/
1385dfc646bSeric 
1395dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1405dfc646bSeric 	{
1415dfc646bSeric 		while ((p = index(p, '$')) != NULL)
1425dfc646bSeric 			if (*++p == 'u')
1435dfc646bSeric 				break;
1445dfc646bSeric 		if (p != NULL)
1455dfc646bSeric 			break;
1465dfc646bSeric 
1475dfc646bSeric 		/* this entry is safe -- go ahead and process it */
148db8841e9Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
1495dfc646bSeric 		*pvp++ = newstr(buf);
1505dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1515dfc646bSeric 		{
1525dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1535dfc646bSeric 			return (-1);
1545dfc646bSeric 		}
1555dfc646bSeric 	}
156c579ef51Seric 
157*33db8731Seric 	/*
158*33db8731Seric 	**  If we have no substitution for the user name in the argument
159*33db8731Seric 	**  list, we know that we must supply the names otherwise -- and
160*33db8731Seric 	**  SMTP is the answer!!
161*33db8731Seric 	*/
162*33db8731Seric 
1635dfc646bSeric 	if (*mvp == NULL)
164c579ef51Seric 	{
165c579ef51Seric 		/* running SMTP */
1662c7e1b8dSeric # ifdef SMTP
167c579ef51Seric 		clever = TRUE;
168c579ef51Seric 		*pvp = NULL;
169*33db8731Seric 
170*33db8731Seric 		/* send the initial SMTP protocol */
171c579ef51Seric 		i = smtpinit(m, pv, (ADDRESS *) NULL);
172c579ef51Seric 		giveresponse(i, TRUE, m);
1732c7e1b8dSeric # ifdef QUEUE
174c579ef51Seric 		if (i == EX_TEMPFAIL)
175c579ef51Seric 		{
176c579ef51Seric 			QueueUp = TRUE;
177c579ef51Seric 			tempfail = TRUE;
178c579ef51Seric 		}
1792c7e1b8dSeric # endif QUEUE
1802c7e1b8dSeric # else SMTP
181*33db8731Seric 		/* oops!  we don't implement SMTP */
1822c7e1b8dSeric 		syserr("SMTP style mailer");
1832c7e1b8dSeric 		return (EX_SOFTWARE);
1842c7e1b8dSeric # endif SMTP
185c579ef51Seric 	}
1865dfc646bSeric 
1875dfc646bSeric 	/*
1885dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
1895dfc646bSeric 	**  run through our address list and append all the addresses
1905dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
1915dfc646bSeric 	**  always send another copy later.
1925dfc646bSeric 	*/
1935dfc646bSeric 
1945dfc646bSeric 	tobuf[0] = '\0';
1955dfc646bSeric 	To = tobuf;
1966259796dSeric 	ctladdr = NULL;
1975dfc646bSeric 	for (; to != NULL; to = to->q_next)
1985dfc646bSeric 	{
1995dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
200bea22b26Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
2015dfc646bSeric 			break;
2025dfc646bSeric 
2035dfc646bSeric 		/* if already sent or not for this host, don't send */
204e77e673fSeric 		if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) ||
205e77e673fSeric 		    strcmp(to->q_host, host) != 0 || to->q_mailer != firstto->q_mailer)
2065dfc646bSeric 			continue;
2076259796dSeric 
208772e6e50Seric # ifdef DEBUG
209772e6e50Seric 		if (Debug)
210772e6e50Seric 		{
211772e6e50Seric 			printf("\nsend to ");
212772e6e50Seric 			printaddr(to, FALSE);
213772e6e50Seric 		}
214772e6e50Seric # endif DEBUG
215772e6e50Seric 
216772e6e50Seric 		/* link together the chain of recipients */
217772e6e50Seric 		if (!bitset(QDONTSEND, to->q_flags))
218772e6e50Seric 		{
219772e6e50Seric 			to->q_tchain = tochain;
220772e6e50Seric 			tochain = to;
221772e6e50Seric 		}
222772e6e50Seric 
2236259796dSeric 		/* compute effective uid/gid when sending */
2247da1035fSeric 		if (to->q_mailer == ProgMailer)
2256259796dSeric 			ctladdr = getctladdr(to);
2266259796dSeric 
2275dfc646bSeric 		user = to->q_user;
2285dfc646bSeric 		To = to->q_paddr;
2295dfc646bSeric 		to->q_flags |= QDONTSEND;
230c579ef51Seric 		if (tempfail)
231772e6e50Seric 		{
232c579ef51Seric 			to->q_flags |= QQUEUEUP;
233772e6e50Seric 			continue;
234772e6e50Seric 		}
2355dfc646bSeric 
2365dfc646bSeric 		/*
2375dfc646bSeric 		**  Check to see that these people are allowed to
2385dfc646bSeric 		**  talk to each other.
2392a6e0786Seric 		*/
2402a6e0786Seric 
2412a6e0786Seric 		if (!checkcompat(to))
2425dfc646bSeric 		{
2435dfc646bSeric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
2445dfc646bSeric 			continue;
2455dfc646bSeric 		}
2462a6e0786Seric 
2472a6e0786Seric 		/*
2489ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2499ec9501bSeric 		**	about them.
25025a99e2eSeric 		*/
25125a99e2eSeric 
2522a6e0786Seric 		if (bitset(M_STRIPQ, m->m_flags))
25325a99e2eSeric 		{
2549ec9501bSeric 			stripquotes(user, TRUE);
2559ec9501bSeric 			stripquotes(host, TRUE);
2569ec9501bSeric 		}
2579ec9501bSeric 		else
2589ec9501bSeric 		{
2599ec9501bSeric 			stripquotes(user, FALSE);
2609ec9501bSeric 			stripquotes(host, FALSE);
26125a99e2eSeric 		}
26225a99e2eSeric 
26325a99e2eSeric 		/*
2643efaed6eSeric 		**  If an error message has already been given, don't
2653efaed6eSeric 		**	bother to send to this address.
2663efaed6eSeric 		**
2673efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2683efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2693efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2703efaed6eSeric 		*/
2713efaed6eSeric 
2723efaed6eSeric 		if (bitset(QBADADDR, to->q_flags))
2733efaed6eSeric 			continue;
2743efaed6eSeric 
275f2fec898Seric 		/* save statistics.... */
2767da1035fSeric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2777da1035fSeric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(MsgSize);
278f2fec898Seric 
2793efaed6eSeric 		/*
28025a99e2eSeric 		**  See if this user name is "special".
28125a99e2eSeric 		**	If the user name has a slash in it, assume that this
28225a99e2eSeric 		**	is a file -- send it off without further ado.
28325a99e2eSeric 		**	Note that this means that editfcn's will not
2845dfc646bSeric 		**	be applied to the message.  Also note that
2855dfc646bSeric 		**	this type of addresses is not processed along
2865dfc646bSeric 		**	with the others, so we fudge on the To person.
28725a99e2eSeric 		*/
28825a99e2eSeric 
2897da1035fSeric 		if (m == LocalMailer)
29025a99e2eSeric 		{
291a49f24c0Seric 			if (user[0] == '/')
29225a99e2eSeric 			{
2936259796dSeric 				i = mailfile(user, getctladdr(to));
29425a99e2eSeric 				giveresponse(i, TRUE, m);
2955dfc646bSeric 				continue;
29625a99e2eSeric 			}
29725a99e2eSeric 		}
29825a99e2eSeric 
29913bbc08cSeric 		/*
30013bbc08cSeric 		**  Address is verified -- add this user to mailer
30113bbc08cSeric 		**  argv, and add it to the print list of recipients.
30213bbc08cSeric 		*/
30313bbc08cSeric 
3045dfc646bSeric 		/* create list of users for error messages */
3055dfc646bSeric 		if (tobuf[0] != '\0')
306db8841e9Seric 			(void) strcat(tobuf, ",");
307db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
3085dfc646bSeric 		define('u', user);		/* to user */
309c2567733Seric 		define('z', to->q_home);	/* user's home */
3105dfc646bSeric 
311c579ef51Seric 		/*
312c579ef51Seric 		**  Expand out this user into argument list or
313c579ef51Seric 		**  send it to our SMTP server.
314c579ef51Seric 		*/
315c579ef51Seric 
316c579ef51Seric 		if (clever)
317c579ef51Seric 		{
3182c7e1b8dSeric # ifdef SMTP
319d2b7d202Seric 			i = smtprcpt(to);
3202c7e1b8dSeric 			if (i != EX_OK)
3212c7e1b8dSeric 			{
3222c7e1b8dSeric # ifdef QUEUE
323c579ef51Seric 				if (i == EX_TEMPFAIL)
324c579ef51Seric 				{
325c579ef51Seric 					QueueUp = TRUE;
326c579ef51Seric 					to->q_flags |= QQUEUEUP;
327c579ef51Seric 				}
3282c7e1b8dSeric 				else
3292c7e1b8dSeric # endif QUEUE
330c579ef51Seric 				{
331c579ef51Seric 					to->q_flags |= QBADADDR;
332c579ef51Seric 					giveresponse(i, TRUE, m);
333c579ef51Seric 				}
334c579ef51Seric 			}
3352c7e1b8dSeric # else SMTP
3362c7e1b8dSeric 			syserr("trying to be clever");
3372c7e1b8dSeric # endif SMTP
3382c7e1b8dSeric 		}
339c579ef51Seric 		else
340c579ef51Seric 		{
341d4ccc802Seric 			(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
3425dfc646bSeric 			*pvp++ = newstr(buf);
3435dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3445dfc646bSeric 			{
3455dfc646bSeric 				/* allow some space for trailing parms */
3465dfc646bSeric 				break;
3475dfc646bSeric 			}
3485dfc646bSeric 		}
349c579ef51Seric 	}
3505dfc646bSeric 
351145b49b1Seric 	/* see if any addresses still exist */
352145b49b1Seric 	if (tobuf[0] == '\0')
353c579ef51Seric 	{
3542c7e1b8dSeric # ifdef SMTP
355c579ef51Seric 		if (clever)
356c579ef51Seric 			smtpquit(pv[0]);
3572c7e1b8dSeric # endif SMTP
358145b49b1Seric 		return (0);
359c579ef51Seric 	}
360145b49b1Seric 
3615dfc646bSeric 	/* print out messages as full list */
3625dfc646bSeric 	To = tobuf;
3635dfc646bSeric 
3645dfc646bSeric 	/*
3655dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3665dfc646bSeric 	*/
3675dfc646bSeric 
368c579ef51Seric 	while (!clever && *++mvp != NULL)
3695dfc646bSeric 	{
370db8841e9Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
3715dfc646bSeric 		*pvp++ = newstr(buf);
3725dfc646bSeric 		if (pvp >= &pv[MAXPV])
3735dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
3745dfc646bSeric 	}
3755dfc646bSeric 	*pvp++ = NULL;
3765dfc646bSeric 
37725a99e2eSeric 	/*
37825a99e2eSeric 	**  Call the mailer.
3796328bdf7Seric 	**	The argument vector gets built, pipes
38025a99e2eSeric 	**	are created as necessary, and we fork & exec as
3816328bdf7Seric 	**	appropriate.
382c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
38325a99e2eSeric 	*/
38425a99e2eSeric 
3855dfc646bSeric 	if (editfcn == NULL)
3865dfc646bSeric 		editfcn = putmessage;
3876259796dSeric 	if (ctladdr == NULL)
3886259796dSeric 		ctladdr = &From;
3892c7e1b8dSeric # ifdef SMTP
390c579ef51Seric 	if (clever)
391c579ef51Seric 	{
392c579ef51Seric 		i = smtpfinish(m, editfcn);
393c579ef51Seric 		smtpquit(pv[0]);
394c579ef51Seric 	}
395c579ef51Seric 	else
3962c7e1b8dSeric # endif SMTP
3976259796dSeric 		i = sendoff(m, pv, editfcn, ctladdr);
3985dfc646bSeric 
399c77d1c25Seric 	/*
400c77d1c25Seric 	**  If we got a temporary failure, arrange to queue the
401c77d1c25Seric 	**  addressees.
402c77d1c25Seric 	*/
403c77d1c25Seric 
4042c7e1b8dSeric # ifdef QUEUE
405c77d1c25Seric 	if (i == EX_TEMPFAIL)
406c77d1c25Seric 	{
407c77d1c25Seric 		QueueUp = TRUE;
408772e6e50Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
409c77d1c25Seric 			to->q_flags |= QQUEUEUP;
410c77d1c25Seric 	}
4112c7e1b8dSeric # endif QUEUE
412c77d1c25Seric 
41335490626Seric 	errno = 0;
4145dfc646bSeric 	return (i);
41525a99e2eSeric }
4165dfc646bSeric /*
41732d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
41832d19d43Seric **
41932d19d43Seric **	This MUST be a macro, since after a vfork we are running
42032d19d43Seric **	two processes on the same stack!!!
42132d19d43Seric **
42232d19d43Seric **	Parameters:
42332d19d43Seric **		none.
42432d19d43Seric **
42532d19d43Seric **	Returns:
42632d19d43Seric **		From a macro???  You've got to be kidding!
42732d19d43Seric **
42832d19d43Seric **	Side Effects:
42932d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
43032d19d43Seric **			pid of child in parent, zero in child.
43132d19d43Seric **			-1 on unrecoverable error.
43232d19d43Seric **
43332d19d43Seric **	Notes:
43432d19d43Seric **		I'm awfully sorry this looks so awful.  That's
43532d19d43Seric **		vfork for you.....
43632d19d43Seric */
43732d19d43Seric 
43832d19d43Seric # define NFORKTRIES	5
43932d19d43Seric # ifdef VFORK
44032d19d43Seric # define XFORK	vfork
44132d19d43Seric # else VFORK
44232d19d43Seric # define XFORK	fork
44332d19d43Seric # endif VFORK
44432d19d43Seric 
44532d19d43Seric # define DOFORK(fORKfN) \
44632d19d43Seric {\
44732d19d43Seric 	register int i;\
44832d19d43Seric \
44932d19d43Seric 	for (i = NFORKTRIES; i-- > 0; )\
45032d19d43Seric 	{\
45132d19d43Seric 		pid = fORKfN();\
45232d19d43Seric 		if (pid >= 0)\
45332d19d43Seric 			break;\
45432d19d43Seric 		sleep((unsigned) NFORKTRIES - i);\
45532d19d43Seric 	}\
45632d19d43Seric }
45732d19d43Seric /*
4582ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
4592ed72599Seric **
4602ed72599Seric **	Parameters:
4612ed72599Seric **		none.
4622ed72599Seric **
4632ed72599Seric **	Returns:
4642ed72599Seric **		pid of child in parent.
4652ed72599Seric **		zero in child.
4662ed72599Seric **		-1 on error.
4672ed72599Seric **
4682ed72599Seric **	Side Effects:
4692ed72599Seric **		returns twice, once in parent and once in child.
4702ed72599Seric */
4712ed72599Seric 
4722ed72599Seric dofork()
4732ed72599Seric {
4742ed72599Seric 	register int pid;
4752ed72599Seric 
4762ed72599Seric 	DOFORK(fork);
4772ed72599Seric 	return (pid);
4782ed72599Seric }
4792ed72599Seric /*
4805dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
4815dfc646bSeric **
4825dfc646bSeric **	Parameters:
4835dfc646bSeric **		m -- mailer descriptor.
4845dfc646bSeric **		pvp -- parameter vector to send to it.
4855dfc646bSeric **		editfcn -- function to pipe it through.
4866259796dSeric **		ctladdr -- an address pointer controlling the
4876259796dSeric **			user/groupid etc. of the mailer.
4885dfc646bSeric **
4895dfc646bSeric **	Returns:
4905dfc646bSeric **		exit status of mailer.
4915dfc646bSeric **
4925dfc646bSeric **	Side Effects:
4935dfc646bSeric **		none.
4945dfc646bSeric */
4955dfc646bSeric 
4966259796dSeric sendoff(m, pvp, editfcn, ctladdr)
4975dfc646bSeric 	struct mailer *m;
4985dfc646bSeric 	char **pvp;
4995dfc646bSeric 	int (*editfcn)();
5006259796dSeric 	ADDRESS *ctladdr;
5015dfc646bSeric {
502c579ef51Seric 	auto FILE *mfile;
503c579ef51Seric 	auto FILE *rfile;
5045dfc646bSeric 	register int i;
505c579ef51Seric 	extern putmessage();
506c579ef51Seric 	int pid;
507c579ef51Seric 
508c579ef51Seric 	/*
509c579ef51Seric 	**  Create connection to mailer.
510c579ef51Seric 	*/
511c579ef51Seric 
512c579ef51Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
513c579ef51Seric 	if (pid < 0)
514c579ef51Seric 		return (-1);
515c579ef51Seric 
516c579ef51Seric 	/*
517c579ef51Seric 	**  Format and send message.
518c579ef51Seric 	*/
519c579ef51Seric 
520c579ef51Seric 	(void) signal(SIGPIPE, SIG_IGN);
521c579ef51Seric 	if (editfcn == NULL)
522c579ef51Seric 		editfcn = putmessage;
523c579ef51Seric 
524c579ef51Seric 	(*editfcn)(mfile, m, FALSE);
525c579ef51Seric 	(void) fclose(mfile);
526c579ef51Seric 
527c579ef51Seric 	i = endmailer(pid, pvp[0]);
528c579ef51Seric 	giveresponse(i, TRUE, m);
529bc6e2962Seric 
530bc6e2962Seric 	/* arrange a return receipt if requested */
531bc6e2962Seric 	if (RetReceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK)
532bc6e2962Seric 	{
533bc6e2962Seric 		SendReceipt = TRUE;
534bc6e2962Seric 		fprintf(Xscript, "%s... successfully delivered\n", To);
535bc6e2962Seric 		/* do we want to send back more info? */
536bc6e2962Seric 	}
537bc6e2962Seric 
538c579ef51Seric 	return (i);
539c579ef51Seric }
540c579ef51Seric /*
541c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
542c579ef51Seric **
543c579ef51Seric **	We should never get fatal errors (e.g., segmentation
544c579ef51Seric **	violation), so we report those specially.  For other
545c579ef51Seric **	errors, we choose a status message (into statmsg),
546c579ef51Seric **	and if it represents an error, we print it.
547c579ef51Seric **
548c579ef51Seric **	Parameters:
549c579ef51Seric **		pid -- pid of mailer.
550c579ef51Seric **		name -- name of mailer (for error messages).
551c579ef51Seric **
552c579ef51Seric **	Returns:
553c579ef51Seric **		exit code of mailer.
554c579ef51Seric **
555c579ef51Seric **	Side Effects:
556c579ef51Seric **		none.
557c579ef51Seric */
558c579ef51Seric 
559c579ef51Seric endmailer(pid, name)
560c579ef51Seric 	int pid;
561c579ef51Seric 	char *name;
562c579ef51Seric {
563c579ef51Seric 	register int i;
564c579ef51Seric 	auto int st;
565c579ef51Seric 
566*33db8731Seric 	/* in the IPC case there is nothing to wait for */
567*33db8731Seric 	if (pid == 0)
568*33db8731Seric 		return (EX_OK);
569*33db8731Seric 
570*33db8731Seric 	/* wait for the mailer process to die and collect status */
571c579ef51Seric 	while ((i = wait(&st)) > 0 && i != pid)
572c579ef51Seric 		continue;
573c579ef51Seric 	if (i < 0)
574c579ef51Seric 	{
575c579ef51Seric 		syserr("wait");
576c579ef51Seric 		return (-1);
577c579ef51Seric 	}
578*33db8731Seric 
579*33db8731Seric 	/* see if it died a horrid death */
580c579ef51Seric 	if ((st & 0377) != 0)
581c579ef51Seric 	{
582c579ef51Seric 		syserr("%s: stat %o", name, st);
583c579ef51Seric 		ExitStat = EX_UNAVAILABLE;
584c579ef51Seric 		return (-1);
585c579ef51Seric 	}
586*33db8731Seric 
587*33db8731Seric 	/* normal death -- return status */
588c579ef51Seric 	i = (st >> 8) & 0377;
589c579ef51Seric 	return (i);
590c579ef51Seric }
591c579ef51Seric /*
592c579ef51Seric **  OPENMAILER -- open connection to mailer.
593c579ef51Seric **
594c579ef51Seric **	Parameters:
595c579ef51Seric **		m -- mailer descriptor.
596c579ef51Seric **		pvp -- parameter vector to pass to mailer.
597c579ef51Seric **		ctladdr -- controlling address for user.
598c579ef51Seric **		clever -- create a full duplex connection.
599c579ef51Seric **		pmfile -- pointer to mfile (to mailer) connection.
600c579ef51Seric **		prfile -- pointer to rfile (from mailer) connection.
601c579ef51Seric **
602c579ef51Seric **	Returns:
603*33db8731Seric **		pid of mailer ( > 0 ).
604c579ef51Seric **		-1 on error.
605*33db8731Seric **		zero on an IPC connection.
606c579ef51Seric **
607c579ef51Seric **	Side Effects:
608c579ef51Seric **		creates a mailer in a subprocess.
609c579ef51Seric */
610c579ef51Seric 
611c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
612c579ef51Seric 	struct mailer *m;
613c579ef51Seric 	char **pvp;
614c579ef51Seric 	ADDRESS *ctladdr;
615c579ef51Seric 	bool clever;
616c579ef51Seric 	FILE **pmfile;
617c579ef51Seric 	FILE **prfile;
618c579ef51Seric {
6195dfc646bSeric 	int pid;
620f8952a83Seric 	int mpvect[2];
621c579ef51Seric 	int rpvect[2];
6225dfc646bSeric 	FILE *mfile;
623c579ef51Seric 	FILE *rfile;
6245dfc646bSeric 	extern FILE *fdopen();
6255dfc646bSeric 
6265dfc646bSeric # ifdef DEBUG
6275dfc646bSeric 	if (Debug)
6285dfc646bSeric 	{
629c579ef51Seric 		printf("openmailer:\n");
6305dfc646bSeric 		printav(pvp);
6315dfc646bSeric 	}
6325dfc646bSeric # endif DEBUG
63335490626Seric 	errno = 0;
6345dfc646bSeric 
635*33db8731Seric # ifdef DAEMON
636*33db8731Seric 	/*
637*33db8731Seric 	**  Deal with the special case of mail handled through an IPC
638*33db8731Seric 	**  connection.
639*33db8731Seric 	**	In this case we don't actually fork.  We must be
640*33db8731Seric 	**	running SMTP for this to work.  We will return a
641*33db8731Seric 	**	zero pid to indicate that we are running IPC.
642*33db8731Seric 	*/
643*33db8731Seric 
644*33db8731Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
645*33db8731Seric 	{
646*33db8731Seric 		register int i;
647*33db8731Seric 
648*33db8731Seric 		if (!clever)
649*33db8731Seric 			syserr("non-clever IPC");
650*33db8731Seric 		i = makeconnection(pvp[1], pmfile, prfile);
651*33db8731Seric 		if (i != EX_OK)
652*33db8731Seric 			return (-1);
653*33db8731Seric 		else
654*33db8731Seric 			return (0);
655*33db8731Seric 	}
656*33db8731Seric # endif DAEMON
657*33db8731Seric 
6586328bdf7Seric 	/* create a pipe to shove the mail through */
659f8952a83Seric 	if (pipe(mpvect) < 0)
66025a99e2eSeric 	{
661c579ef51Seric 		syserr("pipe (to mailer)");
66225a99e2eSeric 		return (-1);
66325a99e2eSeric 	}
664c579ef51Seric 
6652c7e1b8dSeric # ifdef SMTP
666c579ef51Seric 	/* if this mailer speaks smtp, create a return pipe */
667c579ef51Seric 	if (clever && pipe(rpvect) < 0)
668c579ef51Seric 	{
669c579ef51Seric 		syserr("pipe (from mailer)");
670c579ef51Seric 		(void) close(mpvect[0]);
671c579ef51Seric 		(void) close(mpvect[1]);
672c579ef51Seric 		return (-1);
673c579ef51Seric 	}
6742c7e1b8dSeric # endif SMTP
675c579ef51Seric 
676*33db8731Seric 	/*
677*33db8731Seric 	**  Actually fork the mailer process.
678*33db8731Seric 	**	DOFORK is clever about retrying.
679*33db8731Seric 	*/
680*33db8731Seric 
68132d19d43Seric 	DOFORK(XFORK);
682f129ec7dSeric 	/* pid is set by DOFORK */
68325a99e2eSeric 	if (pid < 0)
68425a99e2eSeric 	{
685*33db8731Seric 		/* 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
765*33db8731Seric 
766*33db8731Seric 		/* try to execute the mailer */
76725a99e2eSeric 		execv(m->m_mailer, pvp);
768*33db8731Seric 
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;
81525a99e2eSeric 	int 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";
839c38ba59cSeric 		if (Verbose)
840544026c5Seric 			message(Arpa_Info, statmsg);
841c38ba59cSeric 	}
8422c7e1b8dSeric # ifdef QUEUE
843c77d1c25Seric 	else if (stat == EX_TEMPFAIL)
844c77d1c25Seric 	{
845c77d1c25Seric 		if (Verbose)
846c77d1c25Seric 			message(Arpa_Info, "transmission deferred");
847c77d1c25Seric 	}
8482c7e1b8dSeric # endif QUEUE
84925a99e2eSeric 	else
85025a99e2eSeric 	{
851c1f9df2cSeric 		Errors++;
85225a99e2eSeric 		if (statmsg == NULL && m->m_badstat != 0)
85325a99e2eSeric 		{
85425a99e2eSeric 			stat = m->m_badstat;
85525a99e2eSeric 			i = stat - EX__BASE;
85625a99e2eSeric # ifdef DEBUG
85725a99e2eSeric 			if (i < 0 || i >= N_SysEx)
85825a99e2eSeric 				syserr("Bad m_badstat %d", stat);
85925a99e2eSeric 			else
86025a99e2eSeric # endif DEBUG
86125a99e2eSeric 			statmsg = SysExMsg[i];
86225a99e2eSeric 		}
86325a99e2eSeric 		if (statmsg == NULL)
86425a99e2eSeric 			usrerr("unknown mailer response %d", stat);
865c38ba59cSeric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
86625a99e2eSeric 			usrerr("%s", statmsg);
86725a99e2eSeric 	}
86825a99e2eSeric 
86925a99e2eSeric 	/*
87025a99e2eSeric 	**  Final cleanup.
87125a99e2eSeric 	**	Log a record of the transaction.  Compute the new
87225a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
87325a99e2eSeric 	**	that.
87425a99e2eSeric 	*/
87525a99e2eSeric 
87625a99e2eSeric 	if (statmsg == NULL)
87729dd97a3Seric 	{
878db8841e9Seric 		(void) sprintf(buf, "error %d", stat);
87929dd97a3Seric 		statmsg = buf;
88029dd97a3Seric 	}
88129dd97a3Seric 
88229dd97a3Seric # ifdef LOG
883e374fd72Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
88425a99e2eSeric # endif LOG
8852c7e1b8dSeric # ifdef QUEUE
886c77d1c25Seric 	if (stat != EX_TEMPFAIL)
8872c7e1b8dSeric # endif QUEUE
888243921eeSeric 		setstat(stat);
88925a99e2eSeric }
89025a99e2eSeric /*
8916328bdf7Seric **  PUTMESSAGE -- output a message to the final mailer.
89225a99e2eSeric **
8936328bdf7Seric **	This routine takes care of recreating the header from the
8946328bdf7Seric **	in-core copy, etc.
89525a99e2eSeric **
89625a99e2eSeric **	Parameters:
8976328bdf7Seric **		fp -- file to output onto.
8986328bdf7Seric **		m -- a mailer descriptor.
899c579ef51Seric **		xdot -- if set, hide lines beginning with dot.
90025a99e2eSeric **
90125a99e2eSeric **	Returns:
9026328bdf7Seric **		none.
90325a99e2eSeric **
90425a99e2eSeric **	Side Effects:
9056328bdf7Seric **		The message is written onto fp.
90625a99e2eSeric */
90725a99e2eSeric 
908c579ef51Seric putmessage(fp, m, xdot)
9096328bdf7Seric 	FILE *fp;
9106328bdf7Seric 	struct mailer *m;
911c579ef51Seric 	bool xdot;
91225a99e2eSeric {
9136328bdf7Seric 	char buf[BUFSIZ];
91425a99e2eSeric 
91513bbc08cSeric 	/*
91613bbc08cSeric 	**  Output "From" line unless supressed
917a36e30c9Seric 	**
918a36e30c9Seric 	**  >>>>>>>>>>	One of the ugliest hacks seen by human eyes is
919a36e30c9Seric 	**  >>>>>>>>>>	contained herein: UUCP wants those stupid
920bba2edb3Seric 	**  >>>>>>>>>>	"remote from <host>" lines.  Why oh why does a
921bba2edb3Seric 	**  >> NOTE >>	well-meaning programmer such as myself have to
922a36e30c9Seric 	**  >>>>>>>>>>	deal with this kind of antique garbage????
923bba2edb3Seric 	**  >>>>>>>>>>  This even depends on the local UUCP host name
924bba2edb3Seric 	**  >>>>>>>>>>  being in the $U macro!!!!
92513bbc08cSeric 	*/
92613bbc08cSeric 
92740e4ab56Seric 	if (!bitset(M_NHDR, m->m_flags))
9281412cc7cSeric 	{
9292c7e1b8dSeric # ifdef UGLYUUCP
930a49f24c0Seric 		if (bitset(M_UGLYUUCP, m->m_flags))
931bba2edb3Seric 			(void) expand("From $f  $d remote from $U", buf,
932a36e30c9Seric 					&buf[sizeof buf - 1]);
933a36e30c9Seric 		else
9342c7e1b8dSeric # endif UGLYUUCP
9351412cc7cSeric 			(void) expand("$l", buf, &buf[sizeof buf - 1]);
9361412cc7cSeric 		fprintf(fp, "%s\n", buf);
9371412cc7cSeric 	}
93840e4ab56Seric 
93913bbc08cSeric 	/*
94013bbc08cSeric 	**  Output all header lines
94113bbc08cSeric 	*/
94213bbc08cSeric 
943bc6e2962Seric 	putheader(fp, m);
944bc6e2962Seric 
945bc6e2962Seric 	/*
946bc6e2962Seric 	**  Output the body of the message
947bc6e2962Seric 	*/
948bc6e2962Seric 
949bc6e2962Seric 	if (TempFile != NULL)
950bc6e2962Seric 	{
951bc6e2962Seric 		rewind(TempFile);
952bc6e2962Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, TempFile) != NULL)
953bc6e2962Seric 			fprintf(fp, "%s%s", xdot && buf[0] == '.' ? "." : "", buf);
954bc6e2962Seric 
955bc6e2962Seric 		if (ferror(TempFile))
956bc6e2962Seric 		{
957bc6e2962Seric 			syserr("putmessage: read error");
958bc6e2962Seric 			setstat(EX_IOERR);
959bc6e2962Seric 		}
960bc6e2962Seric 	}
961bc6e2962Seric 
962bc6e2962Seric 	(void) fflush(fp);
963bc6e2962Seric 	if (ferror(fp) && errno != EPIPE)
964bc6e2962Seric 	{
965bc6e2962Seric 		syserr("putmessage: write error");
966bc6e2962Seric 		setstat(EX_IOERR);
967bc6e2962Seric 	}
968bc6e2962Seric 	errno = 0;
969bc6e2962Seric }
970bc6e2962Seric /*
971bc6e2962Seric **  PUTHEADER -- put the header part of a message
972bc6e2962Seric **
973bc6e2962Seric **	Parameters:
974bc6e2962Seric **		fp -- file to put it on.
975bc6e2962Seric **		m -- mailer to use.
976bc6e2962Seric **
977bc6e2962Seric **	Returns:
978bc6e2962Seric **		none.
979bc6e2962Seric **
980bc6e2962Seric **	Side Effects:
981bc6e2962Seric **		none.
982bc6e2962Seric */
983bc6e2962Seric 
984bc6e2962Seric putheader(fp, m)
985bc6e2962Seric 	register FILE *fp;
986bc6e2962Seric 	register struct mailer *m;
987bc6e2962Seric {
988bc6e2962Seric 	char buf[BUFSIZ];
989bc6e2962Seric 	register HDR *h;
990bc6e2962Seric 	extern char *arpadate();
991bc6e2962Seric 	bool anyheader = FALSE;
992bc6e2962Seric 	extern char *capitalize();
993bc6e2962Seric 	extern char *hvalue();
994bc6e2962Seric 	extern bool samefrom();
995bc6e2962Seric 	char *of_line;
996bc6e2962Seric 
997894de7daSeric 	of_line = hvalue("original-from");
9986328bdf7Seric 	for (h = Header; h != NULL; h = h->h_link)
9996328bdf7Seric 	{
100013bbc08cSeric 		register char *p;
100187bd9a02Seric 		char *origfrom = OrigFrom;
1002894de7daSeric 		bool nooutput;
100313bbc08cSeric 
1004894de7daSeric 		nooutput = FALSE;
10059e9163a0Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
1006894de7daSeric 			nooutput = TRUE;
1007894de7daSeric 
1008894de7daSeric 		/* use From: line from message if generated is the same */
100987bd9a02Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
1010894de7daSeric 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
101187bd9a02Seric 		{
101287bd9a02Seric 			p = origfrom;
101387bd9a02Seric 			origfrom = NULL;
101487bd9a02Seric 		}
101587bd9a02Seric 		else if (bitset(H_DEFAULT, h->h_flags))
10164ae18d0eSeric 		{
1017db8841e9Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
10184ae18d0eSeric 			p = buf;
10194ae18d0eSeric 		}
10208c2bf25bSeric 		else if (bitset(H_ADDR, h->h_flags))
10218c2bf25bSeric 		{
10228c2bf25bSeric 			register int opos;
10238c2bf25bSeric 			bool firstone = TRUE;
10248c2bf25bSeric 
10258c2bf25bSeric 			/*
10268c2bf25bSeric 			**  Output the address list translated by the
10278c2bf25bSeric 			**  mailer and with commas.
10288c2bf25bSeric 			*/
10298c2bf25bSeric 
10308c2bf25bSeric 			p = h->h_value;
10318c2bf25bSeric 			if (p == NULL || *p == '\0' || nooutput)
10328c2bf25bSeric 				continue;
10338c2bf25bSeric 			fprintf(fp, "%s: ", capitalize(h->h_field));
10348c2bf25bSeric 			opos = strlen(h->h_field) + 2;
10358c2bf25bSeric 			while (*p != '\0')
10368c2bf25bSeric 			{
10378c2bf25bSeric 				register char *name = p;
10388c2bf25bSeric 				extern char *remotename();
10398c2bf25bSeric 				char savechar;
10408c2bf25bSeric 
10418c2bf25bSeric 				/* find the end of the name */
1042bc28c57aSeric 				while (*p != '\0' && *p != ',')
1043bc28c57aSeric 				{
1044bc28c57aSeric 					extern bool isatword();
1045bc28c57aSeric 					char *oldp;
1046bc28c57aSeric 
1047bc28c57aSeric 					if (!OldStyle || !isspace(*p))
1048bc28c57aSeric 					{
10498c2bf25bSeric 						p++;
1050bc28c57aSeric 						continue;
1051bc28c57aSeric 					}
1052bc28c57aSeric 					oldp = p;
1053bc28c57aSeric 					while (*p != '\0' && isspace(*p))
1054bc28c57aSeric 						p++;
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 				}
10648c2bf25bSeric 				savechar = *p;
10658c2bf25bSeric 				*p = '\0';
10668c2bf25bSeric 
10678c2bf25bSeric 				/* translate the name to be relative */
1068bc213ac6Seric 				name = remotename(name, m);
10698c2bf25bSeric 				if (*name == '\0')
10708c2bf25bSeric 					continue;
10718c2bf25bSeric 
10728c2bf25bSeric 				/* output the name with nice formatting */
10738c2bf25bSeric 				opos += strlen(name);
10748c2bf25bSeric 				if (!firstone)
10758c2bf25bSeric 					opos += 2;
10768c2bf25bSeric 				if (opos > 78 && !firstone)
10778c2bf25bSeric 				{
10788c2bf25bSeric 					fprintf(fp, ",\n        ");
1079bc213ac6Seric 					opos = 8 + strlen(name);
10808c2bf25bSeric 				}
10818c2bf25bSeric 				else if (!firstone)
10828c2bf25bSeric 					fprintf(fp, ", ");
10838c2bf25bSeric 				fprintf(fp, "%s", name);
10848c2bf25bSeric 				firstone = FALSE;
10858c2bf25bSeric 
10868c2bf25bSeric 				/* clean up the source string */
10878c2bf25bSeric 				*p = savechar;
10888c2bf25bSeric 				while (*p != '\0' && (isspace(*p) || *p == ','))
10898c2bf25bSeric 					p++;
10908c2bf25bSeric 			}
10918c2bf25bSeric 			fprintf(fp, "\n");
10928c2bf25bSeric 			nooutput = TRUE;
10938c2bf25bSeric 		}
10944ae18d0eSeric 		else
10954ae18d0eSeric 			p = h->h_value;
1096894de7daSeric 		if (p == NULL || *p == '\0')
10979e9163a0Seric 			continue;
10985a0dcb5fSeric 
10995a0dcb5fSeric 		/* hack, hack -- output Original-From field if different */
1100894de7daSeric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
1101894de7daSeric 		{
1102894de7daSeric 			/* output new Original-From line if needed */
1103894de7daSeric 			if (of_line == NULL && !samefrom(p, origfrom))
11045a0dcb5fSeric 			{
110587bd9a02Seric 				fprintf(fp, "Original-From: %s\n", origfrom);
110687bd9a02Seric 				anyheader = TRUE;
11075a0dcb5fSeric 			}
1108894de7daSeric 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
1109894de7daSeric 			{
1110894de7daSeric 				/* delete Original-From: line if redundant */
1111894de7daSeric 				p = of_line;
1112894de7daSeric 				of_line = NULL;
1113894de7daSeric 			}
1114894de7daSeric 		}
1115894de7daSeric 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
1116894de7daSeric 			nooutput = TRUE;
1117894de7daSeric 
1118894de7daSeric 		/* finally, output the header line */
1119894de7daSeric 		if (!nooutput)
1120894de7daSeric 		{
1121894de7daSeric 			fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
1122894de7daSeric 			h->h_flags |= H_USED;
1123894de7daSeric 			anyheader = TRUE;
1124894de7daSeric 		}
11256328bdf7Seric 	}
11266328bdf7Seric 	if (anyheader)
11276328bdf7Seric 		fprintf(fp, "\n");
112825a99e2eSeric }
112925a99e2eSeric /*
1130bc28c57aSeric **  ISATWORD -- tell if the word we are pointing to is "at".
1131bc28c57aSeric **
1132bc28c57aSeric **	Parameters:
1133bc28c57aSeric **		p -- word to check.
1134bc28c57aSeric **
1135bc28c57aSeric **	Returns:
1136bc28c57aSeric **		TRUE -- if p is the word at.
1137bc28c57aSeric **		FALSE -- otherwise.
1138bc28c57aSeric **
1139bc28c57aSeric **	Side Effects:
1140bc28c57aSeric **		none.
1141bc28c57aSeric */
1142bc28c57aSeric 
1143bc28c57aSeric bool
1144bc28c57aSeric isatword(p)
1145bc28c57aSeric 	register char *p;
1146bc28c57aSeric {
1147bc28c57aSeric 	extern char lower();
1148bc28c57aSeric 
1149bc28c57aSeric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
1150bc28c57aSeric 	    p[2] != '\0' && isspace(p[2]))
1151bc28c57aSeric 		return (TRUE);
1152bc28c57aSeric 	return (FALSE);
1153bc28c57aSeric }
1154bc28c57aSeric /*
11558c2bf25bSeric **  REMOTENAME -- return the name relative to the current mailer
11568c2bf25bSeric **
11578c2bf25bSeric **	Parameters:
11588c2bf25bSeric **		name -- the name to translate.
11598c2bf25bSeric **
11608c2bf25bSeric **	Returns:
11618c2bf25bSeric **		the text string representing this address relative to
11628c2bf25bSeric **			the receiving mailer.
11638c2bf25bSeric **
11648c2bf25bSeric **	Side Effects:
11658c2bf25bSeric **		none.
11668c2bf25bSeric **
11678c2bf25bSeric **	Warnings:
11688c2bf25bSeric **		The text string returned is tucked away locally;
11698c2bf25bSeric **			copy it if you intend to save it.
11708c2bf25bSeric */
11718c2bf25bSeric 
11728c2bf25bSeric char *
1173bc213ac6Seric remotename(name, m)
11748c2bf25bSeric 	char *name;
1175bc213ac6Seric 	struct mailer *m;
11768c2bf25bSeric {
11778c2bf25bSeric 	static char buf[MAXNAME];
11788c2bf25bSeric 	char lbuf[MAXNAME];
11798c2bf25bSeric 	extern char *macvalue();
11808c2bf25bSeric 	char *oldf = macvalue('f');
1181bc213ac6Seric 	char *oldx = macvalue('x');
1182bc213ac6Seric 	char *oldg = macvalue('g');
11838c2bf25bSeric 	extern char **prescan();
11848c2bf25bSeric 	register char **pvp;
1185bc213ac6Seric 	extern char *getxpart();
11868c2bf25bSeric 
11878c2bf25bSeric 	/*
11888c2bf25bSeric 	**  Do general rewriting of name.
11898c2bf25bSeric 	**	This will also take care of doing global name translation.
11908c2bf25bSeric 	*/
11918c2bf25bSeric 
1192bc213ac6Seric 	define('x', getxpart(name));
11938c2bf25bSeric 	pvp = prescan(name, '\0');
11948c2bf25bSeric 	for (;;)
11958c2bf25bSeric 	{
11968c2bf25bSeric 		rewrite(pvp, 1);
11978c2bf25bSeric 		rewrite(pvp, 3);
11988c2bf25bSeric 		if (**pvp == CANONNET)
11998c2bf25bSeric 		{
12008c2bf25bSeric 			auto ADDRESS a;
12018c2bf25bSeric 			register char *p;
12028c2bf25bSeric 			extern char *hostalias();
12038c2bf25bSeric 
12048c2bf25bSeric 			/* oops... resolved to something */
12058c2bf25bSeric 			if (buildaddr(pvp, &a) == NULL)
12068c2bf25bSeric 				return (name);
12078c2bf25bSeric 			p = hostalias(&a);
12088c2bf25bSeric 			if (p == NULL)
12098c2bf25bSeric 				return (name);
12108c2bf25bSeric 			pvp = prescan(p, '\0');
12118c2bf25bSeric 		}
12128c2bf25bSeric 		else
12138c2bf25bSeric 		{
12148c2bf25bSeric 			cataddr(pvp, lbuf, sizeof lbuf);
12158c2bf25bSeric 			break;
12168c2bf25bSeric 		}
12178c2bf25bSeric 	}
12188c2bf25bSeric 
12198c2bf25bSeric 	/* make the name relative to the receiving mailer */
12208c2bf25bSeric 	define('f', lbuf);
1221bc213ac6Seric 	(void) expand(m->m_from, buf, &buf[sizeof buf - 1]);
12228c2bf25bSeric 
12238c2bf25bSeric 	/* rewrite to get rid of garbage we added in the expand above */
12248c2bf25bSeric 	pvp = prescan(buf, '\0');
12258c2bf25bSeric 	rewrite(pvp, 2);
1226bc213ac6Seric 	cataddr(pvp, lbuf, sizeof lbuf);
1227bc213ac6Seric 
1228bc213ac6Seric 	/* now add any comment info we had before back */
1229bc213ac6Seric 	define('g', lbuf);
1230bc213ac6Seric 	(void) expand("$q", buf, &buf[sizeof buf - 1]);
12318c2bf25bSeric 
12328c2bf25bSeric 	define('f', oldf);
1233bc213ac6Seric 	define('g', oldg);
1234bc213ac6Seric 	define('x', oldx);
12358c2bf25bSeric 
12368c2bf25bSeric # ifdef DEBUG
12378c2bf25bSeric 	if (Debug > 0)
12388c2bf25bSeric 		printf("remotename(%s) => `%s'\n", name, buf);
12398c2bf25bSeric # endif DEBUG
12408c2bf25bSeric 	return (buf);
12418c2bf25bSeric }
12428c2bf25bSeric /*
124387bd9a02Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
124487bd9a02Seric **
124587bd9a02Seric **	Parameters:
124687bd9a02Seric **		ifrom -- internally generated form of from address.
124787bd9a02Seric **		efrom -- external form of from address.
124887bd9a02Seric **
124987bd9a02Seric **	Returns:
125087bd9a02Seric **		TRUE -- if they convey the same info.
125187bd9a02Seric **		FALSE -- if any information has been lost.
125287bd9a02Seric **
125387bd9a02Seric **	Side Effects:
125487bd9a02Seric **		none.
125587bd9a02Seric */
125687bd9a02Seric 
125787bd9a02Seric bool
125887bd9a02Seric samefrom(ifrom, efrom)
125987bd9a02Seric 	char *ifrom;
126087bd9a02Seric 	char *efrom;
126187bd9a02Seric {
1262894de7daSeric 	register char *p;
1263894de7daSeric 	char buf[MAXNAME + 4];
1264894de7daSeric 
1265894de7daSeric # ifdef DEBUG
1266894de7daSeric 	if (Debug > 7)
1267894de7daSeric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
1268894de7daSeric # endif DEBUG
1269894de7daSeric 	if (strcmp(ifrom, efrom) == 0)
1270894de7daSeric 		goto success;
1271894de7daSeric 	p = index(ifrom, '@');
1272894de7daSeric 	if (p == NULL)
1273894de7daSeric 		goto failure;
1274894de7daSeric 	*p = '\0';
1275894de7daSeric 	strcpy(buf, ifrom);
1276894de7daSeric 	strcat(buf, " at ");
1277894de7daSeric 	*p++ = '@';
1278894de7daSeric 	strcat(buf, p);
1279894de7daSeric 	if (strcmp(buf, efrom) == 0)
1280894de7daSeric 		goto success;
1281894de7daSeric 
1282894de7daSeric   failure:
1283894de7daSeric # ifdef DEBUG
1284894de7daSeric 	if (Debug > 7)
1285894de7daSeric 		printf("FALSE\n");
1286894de7daSeric # endif DEBUG
1287894de7daSeric 	return (FALSE);
1288894de7daSeric 
1289894de7daSeric   success:
1290894de7daSeric # ifdef DEBUG
1291894de7daSeric 	if (Debug > 7)
1292894de7daSeric 		printf("TRUE\n");
1293894de7daSeric # endif DEBUG
1294894de7daSeric 	return (TRUE);
129587bd9a02Seric }
129687bd9a02Seric /*
129725a99e2eSeric **  MAILFILE -- Send a message to a file.
129825a99e2eSeric **
1299f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1300f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1301f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1302f129ec7dSeric **	sendmail runs as root.
1303f129ec7dSeric **
130425a99e2eSeric **	Parameters:
130525a99e2eSeric **		filename -- the name of the file to send to.
13066259796dSeric **		ctladdr -- the controlling address header -- includes
13076259796dSeric **			the userid/groupid to be when sending.
130825a99e2eSeric **
130925a99e2eSeric **	Returns:
131025a99e2eSeric **		The exit code associated with the operation.
131125a99e2eSeric **
131225a99e2eSeric **	Side Effects:
131325a99e2eSeric **		none.
131425a99e2eSeric */
131525a99e2eSeric 
13166259796dSeric mailfile(filename, ctladdr)
131725a99e2eSeric 	char *filename;
13186259796dSeric 	ADDRESS *ctladdr;
131925a99e2eSeric {
132025a99e2eSeric 	register FILE *f;
132132d19d43Seric 	register int pid;
132225a99e2eSeric 
132332d19d43Seric 	/*
132432d19d43Seric 	**  Fork so we can change permissions here.
132532d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
132632d19d43Seric 	**	the complications of calling subroutines, etc.
132732d19d43Seric 	*/
132832d19d43Seric 
132932d19d43Seric 	DOFORK(fork);
133032d19d43Seric 
133132d19d43Seric 	if (pid < 0)
133232d19d43Seric 		return (EX_OSERR);
133332d19d43Seric 	else if (pid == 0)
133432d19d43Seric 	{
133532d19d43Seric 		/* child -- actually write to file */
1336f129ec7dSeric 		struct stat stb;
1337f129ec7dSeric 
13380984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
13390984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
13400984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
1341f129ec7dSeric 		umask(OldUmask);
1342f129ec7dSeric 		if (stat(filename, &stb) < 0)
1343e6e1265fSeric 			stb.st_mode = 0666;
1344f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1345f129ec7dSeric 			exit(EX_CANTCREAT);
134603827b5fSeric 		if (ctladdr == NULL)
134703827b5fSeric 			ctladdr = &From;
1348f129ec7dSeric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
1349e36b99e2Seric 		{
1350e36b99e2Seric 			if (ctladdr->q_uid == 0)
1351e36b99e2Seric 				(void) setgid(DefGid);
1352e36b99e2Seric 			else
13536259796dSeric 				(void) setgid(ctladdr->q_gid);
1354e36b99e2Seric 		}
1355f129ec7dSeric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
1356e36b99e2Seric 		{
1357e36b99e2Seric 			if (ctladdr->q_uid == 0)
1358e36b99e2Seric 				(void) setuid(DefUid);
1359e36b99e2Seric 			else
13606259796dSeric 				(void) setuid(ctladdr->q_uid);
1361e36b99e2Seric 		}
136225a99e2eSeric 		f = fopen(filename, "a");
136325a99e2eSeric 		if (f == NULL)
136432d19d43Seric 			exit(EX_CANTCREAT);
136525a99e2eSeric 
1366c579ef51Seric 		putmessage(f, Mailer[1], FALSE);
136725a99e2eSeric 		fputs("\n", f);
1368db8841e9Seric 		(void) fclose(f);
136932d19d43Seric 		(void) fflush(stdout);
1370e36b99e2Seric 
1371e36b99e2Seric 		/* reset ISUID & ISGID bits */
1372c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
137332d19d43Seric 		exit(EX_OK);
137413bbc08cSeric 		/*NOTREACHED*/
137532d19d43Seric 	}
137632d19d43Seric 	else
137732d19d43Seric 	{
137832d19d43Seric 		/* parent -- wait for exit status */
137932d19d43Seric 		register int i;
138032d19d43Seric 		auto int stat;
138132d19d43Seric 
138232d19d43Seric 		while ((i = wait(&stat)) != pid)
138332d19d43Seric 		{
138432d19d43Seric 			if (i < 0)
138532d19d43Seric 			{
138632d19d43Seric 				stat = EX_OSERR << 8;
138732d19d43Seric 				break;
138832d19d43Seric 			}
138932d19d43Seric 		}
13900984da9fSeric 		if ((stat & 0377) != 0)
13910984da9fSeric 			stat = EX_UNAVAILABLE << 8;
139232d19d43Seric 		return ((stat >> 8) & 0377);
139332d19d43Seric 	}
139425a99e2eSeric }
1395ea4dc939Seric /*
1396ea4dc939Seric **  SENDALL -- actually send all the messages.
1397ea4dc939Seric **
1398ea4dc939Seric **	Parameters:
1399ea4dc939Seric **		verifyonly -- if set, only give verification messages.
1400ea4dc939Seric **
1401ea4dc939Seric **	Returns:
1402ea4dc939Seric **		none.
1403ea4dc939Seric **
1404ea4dc939Seric **	Side Effects:
1405ea4dc939Seric **		Scans the send lists and sends everything it finds.
1406ea4dc939Seric */
1407ea4dc939Seric 
1408ea4dc939Seric sendall(verifyonly)
1409ea4dc939Seric 	bool verifyonly;
1410ea4dc939Seric {
1411e77e673fSeric 	register ADDRESS *q;
1412ea4dc939Seric 	typedef int (*fnptr)();
1413ea4dc939Seric 
1414772e6e50Seric # ifdef DEBUG
1415772e6e50Seric 	if (Debug > 1)
1416772e6e50Seric 	{
1417772e6e50Seric 		printf("\nSendQueue:\n");
1418772e6e50Seric 		printaddr(SendQueue, TRUE);
1419772e6e50Seric 	}
1420772e6e50Seric # endif DEBUG
1421ea4dc939Seric 
1422e77e673fSeric 	for (q = SendQueue; q != NULL; q = q->q_next)
1423ea4dc939Seric 	{
1424ea4dc939Seric 		if (verifyonly)
1425ea4dc939Seric 		{
1426ea4dc939Seric 			To = q->q_paddr;
1427e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1428ea4dc939Seric 			{
14297da1035fSeric 				if (bitset(M_LOCAL, q->q_mailer->m_flags))
1430ea4dc939Seric 					message(Arpa_Info, "deliverable");
1431ea4dc939Seric 				else
1432ea4dc939Seric 					message(Arpa_Info, "queueable");
1433ea4dc939Seric 			}
1434ea4dc939Seric 		}
1435ea4dc939Seric 		else
1436ea4dc939Seric 			(void) deliver(q, (fnptr) NULL);
1437ea4dc939Seric 	}
1438ea4dc939Seric }
1439