125a99e2eSeric # include <signal.h>
254aa2b0fSeric # include <errno.h>
3b20b3270Seric # include "sendmail.h"
425a99e2eSeric # ifdef LOG
5e374fd72Seric # include <syslog.h>
625a99e2eSeric # endif LOG
725a99e2eSeric 
8*32d19d43Seric static char SccsId[] = "@(#)deliver.c	3.29	08/22/81";
9259cace7Seric 
1025a99e2eSeric /*
1125a99e2eSeric **  DELIVER -- Deliver a message to a particular address.
1225a99e2eSeric **
1325a99e2eSeric **	Parameters:
1425a99e2eSeric **		to -- the address to deliver the message to.
1525a99e2eSeric **		editfcn -- if non-NULL, we want to call this function
1625a99e2eSeric **			to output the letter (instead of just out-
1725a99e2eSeric **			putting it raw).
1825a99e2eSeric **
1925a99e2eSeric **	Returns:
2025a99e2eSeric **		zero -- successfully delivered.
2125a99e2eSeric **		else -- some failure, see ExitStat for more info.
2225a99e2eSeric **
2325a99e2eSeric **	Side Effects:
2425a99e2eSeric **		The standard input is passed off to someone.
2525a99e2eSeric */
2625a99e2eSeric 
2725a99e2eSeric deliver(to, editfcn)
282a6e0786Seric 	ADDRESS *to;
2925a99e2eSeric 	int (*editfcn)();
3025a99e2eSeric {
3125a99e2eSeric 	char *host;
3225a99e2eSeric 	char *user;
3325a99e2eSeric 	char **pvp;
345dfc646bSeric 	register char **mvp;
3525a99e2eSeric 	register char *p;
365dfc646bSeric 	register struct mailer *m;
375dfc646bSeric 	register int i;
386328bdf7Seric 	extern putmessage();
392a6e0786Seric 	extern bool checkcompat();
405dfc646bSeric 	char *pv[MAXPV+1];
415dfc646bSeric 	char tobuf[MAXLINE];
425dfc646bSeric 	char buf[MAXNAME];
435dfc646bSeric 	bool firstone;
4425a99e2eSeric 
455dfc646bSeric 	if (bitset(QDONTSEND, to->q_flags))
465dfc646bSeric 		return (0);
4725a99e2eSeric 
4825a99e2eSeric # ifdef DEBUG
4925a99e2eSeric 	if (Debug)
505dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
515dfc646bSeric 			to->q_mailer, to->q_host, to->q_user);
5225a99e2eSeric # endif DEBUG
5325a99e2eSeric 
5425a99e2eSeric 	/*
555dfc646bSeric 	**  Do initial argv setup.
565dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
575dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
585dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
595dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
605dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
615dfc646bSeric 	*/
625dfc646bSeric 
635dfc646bSeric 	m = Mailer[to->q_mailer];
645dfc646bSeric 	host = to->q_host;
655dfc646bSeric 	define('g', m->m_from);		/* translated from address */
665dfc646bSeric 	define('h', host);		/* to host */
675dfc646bSeric 	Errors = 0;
685dfc646bSeric 	errno = 0;
695dfc646bSeric 	pvp = pv;
705dfc646bSeric 	*pvp++ = m->m_argv[0];
715dfc646bSeric 
725dfc646bSeric 	/* insert -f or -r flag as appropriate */
735dfc646bSeric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
745dfc646bSeric 	{
755dfc646bSeric 		if (bitset(M_FOPT, m->m_flags))
765dfc646bSeric 			*pvp++ = "-f";
775dfc646bSeric 		else
785dfc646bSeric 			*pvp++ = "-r";
79db8841e9Seric 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
805dfc646bSeric 		*pvp++ = newstr(buf);
815dfc646bSeric 	}
825dfc646bSeric 
835dfc646bSeric 	/*
845dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
855dfc646bSeric 	**  up to the first entry containing "$u".  There can only
865dfc646bSeric 	**  be one of these, and there are only a few more slots
875dfc646bSeric 	**  in the pv after it.
885dfc646bSeric 	*/
895dfc646bSeric 
905dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
915dfc646bSeric 	{
925dfc646bSeric 		while ((p = index(p, '$')) != NULL)
935dfc646bSeric 			if (*++p == 'u')
945dfc646bSeric 				break;
955dfc646bSeric 		if (p != NULL)
965dfc646bSeric 			break;
975dfc646bSeric 
985dfc646bSeric 		/* this entry is safe -- go ahead and process it */
99db8841e9Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
1005dfc646bSeric 		*pvp++ = newstr(buf);
1015dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1025dfc646bSeric 		{
1035dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1045dfc646bSeric 			return (-1);
1055dfc646bSeric 		}
1065dfc646bSeric 	}
1075dfc646bSeric 	if (*mvp == NULL)
1085dfc646bSeric 		syserr("No $u in mailer argv for %s", pv[0]);
1095dfc646bSeric 
1105dfc646bSeric 	/*
1115dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
1125dfc646bSeric 	**  run through our address list and append all the addresses
1135dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
1145dfc646bSeric 	**  always send another copy later.
1155dfc646bSeric 	*/
1165dfc646bSeric 
1175dfc646bSeric 	tobuf[0] = '\0';
1185dfc646bSeric 	firstone = TRUE;
1195dfc646bSeric 	To = tobuf;
1205dfc646bSeric 	for (; to != NULL; to = to->q_next)
1215dfc646bSeric 	{
1225dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
1235dfc646bSeric 		if (!firstone && !bitset(M_MUSER, m->m_flags))
1245dfc646bSeric 			break;
1255dfc646bSeric 
1265dfc646bSeric 		/* if already sent or not for this host, don't send */
1275dfc646bSeric 		if (bitset(QDONTSEND, to->q_flags) || strcmp(to->q_host, host) != 0)
1285dfc646bSeric 			continue;
1295dfc646bSeric 		user = to->q_user;
1305dfc646bSeric 		To = to->q_paddr;
1315dfc646bSeric 		to->q_flags |= QDONTSEND;
1325dfc646bSeric 		firstone = FALSE;
1335dfc646bSeric 
1345dfc646bSeric # ifdef DEBUG
1355dfc646bSeric 		if (Debug)
1365dfc646bSeric 			printf("   send to `%s'\n", user);
1375dfc646bSeric # endif DEBUG
1385dfc646bSeric 
1395dfc646bSeric 		/*
1405dfc646bSeric 		**  Check to see that these people are allowed to
1415dfc646bSeric 		**  talk to each other.
1422a6e0786Seric 		*/
1432a6e0786Seric 
1442a6e0786Seric 		if (!checkcompat(to))
1455dfc646bSeric 		{
1465dfc646bSeric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
1475dfc646bSeric 			continue;
1485dfc646bSeric 		}
1492a6e0786Seric 
1502a6e0786Seric 		/*
1519ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
1529ec9501bSeric 		**	about them.
15325a99e2eSeric 		*/
15425a99e2eSeric 
1552a6e0786Seric 		if (bitset(M_STRIPQ, m->m_flags))
15625a99e2eSeric 		{
1579ec9501bSeric 			stripquotes(user, TRUE);
1589ec9501bSeric 			stripquotes(host, TRUE);
1599ec9501bSeric 		}
1609ec9501bSeric 		else
1619ec9501bSeric 		{
1629ec9501bSeric 			stripquotes(user, FALSE);
1639ec9501bSeric 			stripquotes(host, FALSE);
16425a99e2eSeric 		}
16525a99e2eSeric 
16625a99e2eSeric 		/*
1673efaed6eSeric 		**  If an error message has already been given, don't
1683efaed6eSeric 		**	bother to send to this address.
1693efaed6eSeric 		**
1703efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
1713efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
1723efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
1733efaed6eSeric 		*/
1743efaed6eSeric 
1753efaed6eSeric 		if (bitset(QBADADDR, to->q_flags))
1763efaed6eSeric 			continue;
1773efaed6eSeric 
1783efaed6eSeric 		/*
17925a99e2eSeric 		**  See if this user name is "special".
18025a99e2eSeric 		**	If the user name has a slash in it, assume that this
18125a99e2eSeric 		**	is a file -- send it off without further ado.
18225a99e2eSeric 		**	Note that this means that editfcn's will not
1835dfc646bSeric 		**	be applied to the message.  Also note that
1845dfc646bSeric 		**	this type of addresses is not processed along
1855dfc646bSeric 		**	with the others, so we fudge on the To person.
18625a99e2eSeric 		*/
18725a99e2eSeric 
1886cbfa739Seric 		if (m == Mailer[MN_LOCAL])
18925a99e2eSeric 		{
19025a99e2eSeric 			if (index(user, '/') != NULL)
19125a99e2eSeric 			{
19225a99e2eSeric 				i = mailfile(user);
19325a99e2eSeric 				giveresponse(i, TRUE, m);
1945dfc646bSeric 				continue;
19525a99e2eSeric 			}
19625a99e2eSeric 		}
19725a99e2eSeric 
1985dfc646bSeric 		/* create list of users for error messages */
1995dfc646bSeric 		if (tobuf[0] != '\0')
200db8841e9Seric 			(void) strcat(tobuf, ",");
201db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
2025dfc646bSeric 		define('u', user);		/* to user */
203c2567733Seric 		define('z', to->q_home);	/* user's home */
2045dfc646bSeric 
2055dfc646bSeric 		/* expand out this user */
206db8841e9Seric 		(void) expand(user, buf, &buf[sizeof buf - 1]);
2075dfc646bSeric 		*pvp++ = newstr(buf);
2085dfc646bSeric 		if (pvp >= &pv[MAXPV - 2])
2095dfc646bSeric 		{
2105dfc646bSeric 			/* allow some space for trailing parms */
2115dfc646bSeric 			break;
2125dfc646bSeric 		}
2135dfc646bSeric 	}
2145dfc646bSeric 
215145b49b1Seric 	/* see if any addresses still exist */
216145b49b1Seric 	if (tobuf[0] == '\0')
217145b49b1Seric 		return (0);
218145b49b1Seric 
2195dfc646bSeric 	/* print out messages as full list */
2205dfc646bSeric 	To = tobuf;
2215dfc646bSeric 
2225dfc646bSeric 	/*
2235dfc646bSeric 	**  Fill out any parameters after the $u parameter.
2245dfc646bSeric 	*/
2255dfc646bSeric 
2265dfc646bSeric 	while (*++mvp != NULL)
2275dfc646bSeric 	{
228db8841e9Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
2295dfc646bSeric 		*pvp++ = newstr(buf);
2305dfc646bSeric 		if (pvp >= &pv[MAXPV])
2315dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
2325dfc646bSeric 	}
2335dfc646bSeric 	*pvp++ = NULL;
2345dfc646bSeric 
23525a99e2eSeric 	/*
23625a99e2eSeric 	**  Call the mailer.
2376328bdf7Seric 	**	The argument vector gets built, pipes
23825a99e2eSeric 	**	are created as necessary, and we fork & exec as
2396328bdf7Seric 	**	appropriate.
240c2567733Seric 	**
241c2567733Seric 	**	Notice the tacky hack to handle private mailers.
24225a99e2eSeric 	*/
24325a99e2eSeric 
2445dfc646bSeric 	if (editfcn == NULL)
2455dfc646bSeric 		editfcn = putmessage;
2465dfc646bSeric 	i = sendoff(m, pv, editfcn);
2475dfc646bSeric 
2485dfc646bSeric 	return (i);
24925a99e2eSeric }
2505dfc646bSeric /*
251*32d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
252*32d19d43Seric **
253*32d19d43Seric **	This MUST be a macro, since after a vfork we are running
254*32d19d43Seric **	two processes on the same stack!!!
255*32d19d43Seric **
256*32d19d43Seric **	Parameters:
257*32d19d43Seric **		none.
258*32d19d43Seric **
259*32d19d43Seric **	Returns:
260*32d19d43Seric **		From a macro???  You've got to be kidding!
261*32d19d43Seric **
262*32d19d43Seric **	Side Effects:
263*32d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
264*32d19d43Seric **			pid of child in parent, zero in child.
265*32d19d43Seric **			-1 on unrecoverable error.
266*32d19d43Seric **
267*32d19d43Seric **	Notes:
268*32d19d43Seric **		I'm awfully sorry this looks so awful.  That's
269*32d19d43Seric **		vfork for you.....
270*32d19d43Seric */
271*32d19d43Seric 
272*32d19d43Seric # define NFORKTRIES	5
273*32d19d43Seric # ifdef VFORK
274*32d19d43Seric # define XFORK	vfork
275*32d19d43Seric # else VFORK
276*32d19d43Seric # define XFORK	fork
277*32d19d43Seric # endif VFORK
278*32d19d43Seric 
279*32d19d43Seric # define DOFORK(fORKfN) \
280*32d19d43Seric {\
281*32d19d43Seric 	register int i;\
282*32d19d43Seric \
283*32d19d43Seric 	for (i = NFORKTRIES; i-- > 0; )\
284*32d19d43Seric 	{\
285*32d19d43Seric 		pid = fORKfN();\
286*32d19d43Seric 		if (pid >= 0)\
287*32d19d43Seric 			break;\
288*32d19d43Seric 		sleep((unsigned) NFORKTRIES - i);\
289*32d19d43Seric 	}\
290*32d19d43Seric }
291*32d19d43Seric /*
2925dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
2935dfc646bSeric **
2945dfc646bSeric **	Parameters:
2955dfc646bSeric **		m -- mailer descriptor.
2965dfc646bSeric **		pvp -- parameter vector to send to it.
2975dfc646bSeric **		editfcn -- function to pipe it through.
2985dfc646bSeric **
2995dfc646bSeric **	Returns:
3005dfc646bSeric **		exit status of mailer.
3015dfc646bSeric **
3025dfc646bSeric **	Side Effects:
3035dfc646bSeric **		none.
3045dfc646bSeric */
3055dfc646bSeric 
3065dfc646bSeric sendoff(m, pvp, editfcn)
3075dfc646bSeric 	struct mailer *m;
3085dfc646bSeric 	char **pvp;
3095dfc646bSeric 	int (*editfcn)();
3105dfc646bSeric {
3115dfc646bSeric 	auto int st;
3125dfc646bSeric 	register int i;
3135dfc646bSeric 	int pid;
3145dfc646bSeric 	int pvect[2];
3155dfc646bSeric 	FILE *mfile;
3165dfc646bSeric 	extern putmessage();
3175dfc646bSeric 	extern FILE *fdopen();
3185dfc646bSeric 
3195dfc646bSeric # ifdef DEBUG
3205dfc646bSeric 	if (Debug)
3215dfc646bSeric 	{
3225dfc646bSeric 		printf("Sendoff:\n");
3235dfc646bSeric 		printav(pvp);
3245dfc646bSeric 	}
3255dfc646bSeric # endif DEBUG
3265dfc646bSeric 
3276328bdf7Seric 	/* create a pipe to shove the mail through */
3286328bdf7Seric 	if (pipe(pvect) < 0)
32925a99e2eSeric 	{
33025a99e2eSeric 		syserr("pipe");
33125a99e2eSeric 		return (-1);
33225a99e2eSeric 	}
333*32d19d43Seric 	DOFORK(XFORK);
33425a99e2eSeric 	if (pid < 0)
33525a99e2eSeric 	{
33625a99e2eSeric 		syserr("Cannot fork");
337db8841e9Seric 		(void) close(pvect[0]);
338db8841e9Seric 		(void) close(pvect[1]);
33925a99e2eSeric 		return (-1);
34025a99e2eSeric 	}
34125a99e2eSeric 	else if (pid == 0)
34225a99e2eSeric 	{
34325a99e2eSeric 		/* child -- set up input & exec mailer */
34403ab8e55Seric 		/* make diagnostic output be standard output */
345db8841e9Seric 		(void) close(2);
346db8841e9Seric 		(void) dup(1);
347db8841e9Seric 		(void) signal(SIGINT, SIG_IGN);
348db8841e9Seric 		(void) close(0);
34925a99e2eSeric 		if (dup(pvect[0]) < 0)
35025a99e2eSeric 		{
35125a99e2eSeric 			syserr("Cannot dup to zero!");
352a590b978Seric 			_exit(EX_OSERR);
35325a99e2eSeric 		}
354db8841e9Seric 		(void) close(pvect[0]);
355db8841e9Seric 		(void) close(pvect[1]);
3562a6e0786Seric 		if (!bitset(M_RESTR, m->m_flags))
357db8841e9Seric 			(void) setuid(getuid());
358e374fd72Seric # ifndef VFORK
359e374fd72Seric 		/*
360e374fd72Seric 		**  We have to be careful with vfork - we can't mung up the
361e374fd72Seric 		**  memory but we don't want the mailer to inherit any extra
362e374fd72Seric 		**  open files.  Chances are the mailer won't
363e374fd72Seric 		**  care about an extra file, but then again you never know.
364e374fd72Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
365e374fd72Seric 		**  declared static so we can't.  But if we fclose(pwf), which
366e374fd72Seric 		**  is what endpwent does, it closes it in the parent too and
367e374fd72Seric 		**  the next getpwnam will be slower.  If you have a weird
368e374fd72Seric 		**  mailer that chokes on the extra file you should do the
369e374fd72Seric 		**  endpwent().
370e374fd72Seric 		**
371e374fd72Seric 		**  Similar comments apply to log.  However, openlog is
372e374fd72Seric 		**  clever enough to set the FIOCLEX mode on the file,
373e374fd72Seric 		**  so it will be closed automatically on the exec.
374e374fd72Seric 		*/
375e374fd72Seric 
376e374fd72Seric 		endpwent();
37725a99e2eSeric # ifdef LOG
378f9fe028fSeric 		closelog();
37925a99e2eSeric # endif LOG
380e374fd72Seric # endif VFORK
38125a99e2eSeric 		execv(m->m_mailer, pvp);
38225a99e2eSeric 		/* syserr fails because log is closed */
38325a99e2eSeric 		/* syserr("Cannot exec %s", m->m_mailer); */
384*32d19d43Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
385db8841e9Seric 		(void) fflush(stdout);
386a590b978Seric 		_exit(EX_UNAVAILABLE);
38725a99e2eSeric 	}
38825a99e2eSeric 
3896328bdf7Seric 	/* write out message to mailer */
390db8841e9Seric 	(void) close(pvect[0]);
39154aa2b0fSeric 	(void) signal(SIGPIPE, SIG_IGN);
39225a99e2eSeric 	mfile = fdopen(pvect[1], "w");
3936328bdf7Seric 	if (editfcn == NULL)
3946328bdf7Seric 		editfcn = putmessage;
3956328bdf7Seric 	(*editfcn)(mfile, m);
396db8841e9Seric 	(void) fclose(mfile);
39725a99e2eSeric 
39825a99e2eSeric 	/*
39925a99e2eSeric 	**  Wait for child to die and report status.
40025a99e2eSeric 	**	We should never get fatal errors (e.g., segmentation
40125a99e2eSeric 	**	violation), so we report those specially.  For other
40225a99e2eSeric 	**	errors, we choose a status message (into statmsg),
40325a99e2eSeric 	**	and if it represents an error, we print it.
40425a99e2eSeric 	*/
40525a99e2eSeric 
40625a99e2eSeric 	while ((i = wait(&st)) > 0 && i != pid)
40725a99e2eSeric 		continue;
40825a99e2eSeric 	if (i < 0)
40925a99e2eSeric 	{
41025a99e2eSeric 		syserr("wait");
41125a99e2eSeric 		return (-1);
41225a99e2eSeric 	}
41325a99e2eSeric 	if ((st & 0377) != 0)
41425a99e2eSeric 	{
41525a99e2eSeric 		syserr("%s: stat %o", pvp[0], st);
416df2792f7Seric 		ExitStat = EX_UNAVAILABLE;
41725a99e2eSeric 		return (-1);
41825a99e2eSeric 	}
41925a99e2eSeric 	i = (st >> 8) & 0377;
4202ac087dbSeric 	giveresponse(i, TRUE, m);
42125a99e2eSeric 	return (i);
42225a99e2eSeric }
42325a99e2eSeric /*
42425a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
42525a99e2eSeric **
42625a99e2eSeric **	Parameters:
42725a99e2eSeric **		stat -- the status code from the mailer (high byte
42825a99e2eSeric **			only; core dumps must have been taken care of
42925a99e2eSeric **			already).
43025a99e2eSeric **		force -- if set, force an error message output, even
43125a99e2eSeric **			if the mailer seems to like to print its own
43225a99e2eSeric **			messages.
43325a99e2eSeric **		m -- the mailer descriptor for this mailer.
43425a99e2eSeric **
43525a99e2eSeric **	Returns:
436db8841e9Seric **		none.
43725a99e2eSeric **
43825a99e2eSeric **	Side Effects:
439c1f9df2cSeric **		Errors may be incremented.
44025a99e2eSeric **		ExitStat may be set.
44125a99e2eSeric **
44225a99e2eSeric **	Called By:
44325a99e2eSeric **		deliver
44425a99e2eSeric */
44525a99e2eSeric 
44625a99e2eSeric giveresponse(stat, force, m)
44725a99e2eSeric 	int stat;
44825a99e2eSeric 	int force;
44925a99e2eSeric 	register struct mailer *m;
45025a99e2eSeric {
45125a99e2eSeric 	register char *statmsg;
45225a99e2eSeric 	extern char *SysExMsg[];
45325a99e2eSeric 	register int i;
45425a99e2eSeric 	extern int N_SysEx;
45529dd97a3Seric 	extern long MsgSize;
45629dd97a3Seric 	char buf[30];
45725a99e2eSeric 
45825a99e2eSeric 	i = stat - EX__BASE;
45925a99e2eSeric 	if (i < 0 || i > N_SysEx)
46025a99e2eSeric 		statmsg = NULL;
46125a99e2eSeric 	else
46225a99e2eSeric 		statmsg = SysExMsg[i];
46325a99e2eSeric 	if (stat == 0)
464c38ba59cSeric 	{
4656cbfa739Seric 		if (bitset(M_LOCAL, m->m_flags))
4663efaed6eSeric 			statmsg = "delivered";
4673efaed6eSeric 		else
4683efaed6eSeric 			statmsg = "queued";
469c38ba59cSeric 		if (Verbose)
470544026c5Seric 			message(Arpa_Info, statmsg);
471c38ba59cSeric 	}
47225a99e2eSeric 	else
47325a99e2eSeric 	{
474c1f9df2cSeric 		Errors++;
47525a99e2eSeric 		if (statmsg == NULL && m->m_badstat != 0)
47625a99e2eSeric 		{
47725a99e2eSeric 			stat = m->m_badstat;
47825a99e2eSeric 			i = stat - EX__BASE;
47925a99e2eSeric # ifdef DEBUG
48025a99e2eSeric 			if (i < 0 || i >= N_SysEx)
48125a99e2eSeric 				syserr("Bad m_badstat %d", stat);
48225a99e2eSeric 			else
48325a99e2eSeric # endif DEBUG
48425a99e2eSeric 			statmsg = SysExMsg[i];
48525a99e2eSeric 		}
48625a99e2eSeric 		if (statmsg == NULL)
48725a99e2eSeric 			usrerr("unknown mailer response %d", stat);
488c38ba59cSeric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
48925a99e2eSeric 			usrerr("%s", statmsg);
49025a99e2eSeric 	}
49125a99e2eSeric 
49225a99e2eSeric 	/*
49325a99e2eSeric 	**  Final cleanup.
49425a99e2eSeric 	**	Log a record of the transaction.  Compute the new
49525a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
49625a99e2eSeric 	**	that.
49725a99e2eSeric 	*/
49825a99e2eSeric 
49925a99e2eSeric 	if (statmsg == NULL)
50029dd97a3Seric 	{
501db8841e9Seric 		(void) sprintf(buf, "error %d", stat);
50229dd97a3Seric 		statmsg = buf;
50329dd97a3Seric 	}
50429dd97a3Seric 
50529dd97a3Seric # ifdef LOG
506e374fd72Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
50725a99e2eSeric # endif LOG
508243921eeSeric 	setstat(stat);
50925a99e2eSeric }
51025a99e2eSeric /*
5116328bdf7Seric **  PUTMESSAGE -- output a message to the final mailer.
51225a99e2eSeric **
5136328bdf7Seric **	This routine takes care of recreating the header from the
5146328bdf7Seric **	in-core copy, etc.
51525a99e2eSeric **
51625a99e2eSeric **	Parameters:
5176328bdf7Seric **		fp -- file to output onto.
5186328bdf7Seric **		m -- a mailer descriptor.
51925a99e2eSeric **
52025a99e2eSeric **	Returns:
5216328bdf7Seric **		none.
52225a99e2eSeric **
52325a99e2eSeric **	Side Effects:
5246328bdf7Seric **		The message is written onto fp.
52525a99e2eSeric */
52625a99e2eSeric 
5276328bdf7Seric putmessage(fp, m)
5286328bdf7Seric 	FILE *fp;
5296328bdf7Seric 	struct mailer *m;
53025a99e2eSeric {
5316328bdf7Seric 	char buf[BUFSIZ];
5326328bdf7Seric 	register int i;
5336328bdf7Seric 	HDR *h;
534b07ac16bSeric 	register char *p;
5356328bdf7Seric 	extern char *arpadate();
5366328bdf7Seric 	bool anyheader = FALSE;
537e9ff65b0Seric 	extern char *capitalize();
53825a99e2eSeric 
53940e4ab56Seric 	/* output "From" line unless supressed */
54040e4ab56Seric 	if (!bitset(M_NHDR, m->m_flags))
5411412cc7cSeric 	{
5421412cc7cSeric 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
5431412cc7cSeric 		fprintf(fp, "%s\n", buf);
5441412cc7cSeric 	}
54540e4ab56Seric 
5464ae18d0eSeric 	/* output all header lines */
5476328bdf7Seric 	for (h = Header; h != NULL; h = h->h_link)
5486328bdf7Seric 	{
5499e9163a0Seric 		if (bitset(H_DELETE, h->h_flags))
5509e9163a0Seric 			continue;
5519e9163a0Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
5525a0dcb5fSeric 		{
5535a0dcb5fSeric 			p = ")><(";		/* can't happen (I hope) */
5545a0dcb5fSeric 			goto checkfrom;
5555a0dcb5fSeric 		}
5564ae18d0eSeric 		if (bitset(H_DEFAULT, h->h_flags))
5574ae18d0eSeric 		{
558db8841e9Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
5594ae18d0eSeric 			p = buf;
5604ae18d0eSeric 		}
5614ae18d0eSeric 		else
5624ae18d0eSeric 			p = h->h_value;
5639e9163a0Seric 		if (*p == '\0')
5649e9163a0Seric 			continue;
5654ae18d0eSeric 		fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
5666328bdf7Seric 		h->h_flags |= H_USED;
5676328bdf7Seric 		anyheader = TRUE;
5685a0dcb5fSeric 
5695a0dcb5fSeric 		/* hack, hack -- output Original-From field if different */
5705a0dcb5fSeric 	checkfrom:
5715a0dcb5fSeric 		if (strcmp(h->h_field, "from") == 0)
5725a0dcb5fSeric 		{
5735a0dcb5fSeric 			extern char *hvalue();
5745a0dcb5fSeric 			char *ofrom = hvalue("original-from");
5755a0dcb5fSeric 
5765a0dcb5fSeric 			if (ofrom != NULL && strcmp(p, ofrom) != 0)
5775a0dcb5fSeric 				fprintf(fp, "Original-From: %s\n", ofrom);
5785a0dcb5fSeric 		}
5796328bdf7Seric 	}
5806328bdf7Seric 
5816328bdf7Seric 	if (anyheader)
5826328bdf7Seric 		fprintf(fp, "\n");
5836328bdf7Seric 
5846328bdf7Seric 	/* output the body of the message */
585b7902a1dSeric 	rewind(TempFile);
586b7902a1dSeric 	while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0)
587db8841e9Seric 		(void) fwrite(buf, 1, i, fp);
5886328bdf7Seric 
58954aa2b0fSeric 	if (ferror(fp) && errno != EPIPE)
59025a99e2eSeric 	{
5916328bdf7Seric 		syserr("putmessage: write error");
59225a99e2eSeric 		setstat(EX_IOERR);
59325a99e2eSeric 	}
59454aa2b0fSeric 	errno = 0;
59525a99e2eSeric }
59625a99e2eSeric /*
59725a99e2eSeric **  MAILFILE -- Send a message to a file.
59825a99e2eSeric **
59925a99e2eSeric **	Parameters:
60025a99e2eSeric **		filename -- the name of the file to send to.
60125a99e2eSeric **
60225a99e2eSeric **	Returns:
60325a99e2eSeric **		The exit code associated with the operation.
60425a99e2eSeric **
60525a99e2eSeric **	Side Effects:
60625a99e2eSeric **		none.
60725a99e2eSeric */
60825a99e2eSeric 
60925a99e2eSeric mailfile(filename)
61025a99e2eSeric 	char *filename;
61125a99e2eSeric {
61225a99e2eSeric 	register FILE *f;
613*32d19d43Seric 	register int pid;
614*32d19d43Seric 	register int i;
61525a99e2eSeric 
616*32d19d43Seric 	/*
617*32d19d43Seric 	**  Fork so we can change permissions here.
618*32d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
619*32d19d43Seric 	**	the complications of calling subroutines, etc.
620*32d19d43Seric 	*/
621*32d19d43Seric 
622*32d19d43Seric 	DOFORK(fork);
623*32d19d43Seric 
624*32d19d43Seric 	if (pid < 0)
625*32d19d43Seric 		return (EX_OSERR);
626*32d19d43Seric 	else if (pid == 0)
627*32d19d43Seric 	{
628*32d19d43Seric 		/* child -- actually write to file */
629*32d19d43Seric 		(void) setuid(getuid());
630*32d19d43Seric 		(void) setgid(getgid());
63125a99e2eSeric 		f = fopen(filename, "a");
63225a99e2eSeric 		if (f == NULL)
633*32d19d43Seric 			exit(EX_CANTCREAT);
63425a99e2eSeric 
63540e4ab56Seric 		putmessage(f, Mailer[1]);
63625a99e2eSeric 		fputs("\n", f);
637db8841e9Seric 		(void) fclose(f);
638*32d19d43Seric 		(void) fflush(stdout);
639*32d19d43Seric 		exit(EX_OK);
640*32d19d43Seric 	}
641*32d19d43Seric 	else
642*32d19d43Seric 	{
643*32d19d43Seric 		/* parent -- wait for exit status */
644*32d19d43Seric 		register int i;
645*32d19d43Seric 		auto int stat;
646*32d19d43Seric 
647*32d19d43Seric 		while ((i = wait(&stat)) != pid)
648*32d19d43Seric 		{
649*32d19d43Seric 			if (i < 0)
650*32d19d43Seric 			{
651*32d19d43Seric 				stat = EX_OSERR << 8;
652*32d19d43Seric 				break;
653*32d19d43Seric 			}
654*32d19d43Seric 		}
655*32d19d43Seric 		return ((stat >> 8) & 0377);
656*32d19d43Seric 	}
65725a99e2eSeric }
658