125a99e2eSeric # include <signal.h>
254aa2b0fSeric # include <errno.h>
3b20b3270Seric # include "sendmail.h"
425a99e2eSeric # ifdef LOG
5e374fd72Seric # include <syslog.h>
625a99e2eSeric # endif LOG
725a99e2eSeric 
8*f2fec898Seric static char SccsId[] = "@(#)deliver.c	3.32	08/31/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 
178*f2fec898Seric 		/* save statistics.... */
179*f2fec898Seric 		Stat.stat_nt[to->q_mailer]++;
180*f2fec898Seric 		Stat.stat_bt[to->q_mailer] += kbytes(MsgSize);
181*f2fec898Seric 
1823efaed6eSeric 		/*
18325a99e2eSeric 		**  See if this user name is "special".
18425a99e2eSeric 		**	If the user name has a slash in it, assume that this
18525a99e2eSeric 		**	is a file -- send it off without further ado.
18625a99e2eSeric 		**	Note that this means that editfcn's will not
1875dfc646bSeric 		**	be applied to the message.  Also note that
1885dfc646bSeric 		**	this type of addresses is not processed along
1895dfc646bSeric 		**	with the others, so we fudge on the To person.
19025a99e2eSeric 		*/
19125a99e2eSeric 
1926cbfa739Seric 		if (m == Mailer[MN_LOCAL])
19325a99e2eSeric 		{
19425a99e2eSeric 			if (index(user, '/') != NULL)
19525a99e2eSeric 			{
19625a99e2eSeric 				i = mailfile(user);
19725a99e2eSeric 				giveresponse(i, TRUE, m);
1985dfc646bSeric 				continue;
19925a99e2eSeric 			}
20025a99e2eSeric 		}
20125a99e2eSeric 
2025dfc646bSeric 		/* create list of users for error messages */
2035dfc646bSeric 		if (tobuf[0] != '\0')
204db8841e9Seric 			(void) strcat(tobuf, ",");
205db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
2065dfc646bSeric 		define('u', user);		/* to user */
207c2567733Seric 		define('z', to->q_home);	/* user's home */
2085dfc646bSeric 
2095dfc646bSeric 		/* expand out this user */
210db8841e9Seric 		(void) expand(user, buf, &buf[sizeof buf - 1]);
2115dfc646bSeric 		*pvp++ = newstr(buf);
2125dfc646bSeric 		if (pvp >= &pv[MAXPV - 2])
2135dfc646bSeric 		{
2145dfc646bSeric 			/* allow some space for trailing parms */
2155dfc646bSeric 			break;
2165dfc646bSeric 		}
2175dfc646bSeric 	}
2185dfc646bSeric 
219145b49b1Seric 	/* see if any addresses still exist */
220145b49b1Seric 	if (tobuf[0] == '\0')
221145b49b1Seric 		return (0);
222145b49b1Seric 
2235dfc646bSeric 	/* print out messages as full list */
2245dfc646bSeric 	To = tobuf;
2255dfc646bSeric 
2265dfc646bSeric 	/*
2275dfc646bSeric 	**  Fill out any parameters after the $u parameter.
2285dfc646bSeric 	*/
2295dfc646bSeric 
2305dfc646bSeric 	while (*++mvp != NULL)
2315dfc646bSeric 	{
232db8841e9Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
2335dfc646bSeric 		*pvp++ = newstr(buf);
2345dfc646bSeric 		if (pvp >= &pv[MAXPV])
2355dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
2365dfc646bSeric 	}
2375dfc646bSeric 	*pvp++ = NULL;
2385dfc646bSeric 
23925a99e2eSeric 	/*
24025a99e2eSeric 	**  Call the mailer.
2416328bdf7Seric 	**	The argument vector gets built, pipes
24225a99e2eSeric 	**	are created as necessary, and we fork & exec as
2436328bdf7Seric 	**	appropriate.
244c2567733Seric 	**
245c2567733Seric 	**	Notice the tacky hack to handle private mailers.
24625a99e2eSeric 	*/
24725a99e2eSeric 
2485dfc646bSeric 	if (editfcn == NULL)
2495dfc646bSeric 		editfcn = putmessage;
2505dfc646bSeric 	i = sendoff(m, pv, editfcn);
2515dfc646bSeric 
2525dfc646bSeric 	return (i);
25325a99e2eSeric }
2545dfc646bSeric /*
25532d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
25632d19d43Seric **
25732d19d43Seric **	This MUST be a macro, since after a vfork we are running
25832d19d43Seric **	two processes on the same stack!!!
25932d19d43Seric **
26032d19d43Seric **	Parameters:
26132d19d43Seric **		none.
26232d19d43Seric **
26332d19d43Seric **	Returns:
26432d19d43Seric **		From a macro???  You've got to be kidding!
26532d19d43Seric **
26632d19d43Seric **	Side Effects:
26732d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
26832d19d43Seric **			pid of child in parent, zero in child.
26932d19d43Seric **			-1 on unrecoverable error.
27032d19d43Seric **
27132d19d43Seric **	Notes:
27232d19d43Seric **		I'm awfully sorry this looks so awful.  That's
27332d19d43Seric **		vfork for you.....
27432d19d43Seric */
27532d19d43Seric 
27632d19d43Seric # define NFORKTRIES	5
27732d19d43Seric # ifdef VFORK
27832d19d43Seric # define XFORK	vfork
27932d19d43Seric # else VFORK
28032d19d43Seric # define XFORK	fork
28132d19d43Seric # endif VFORK
28232d19d43Seric 
28332d19d43Seric # define DOFORK(fORKfN) \
28432d19d43Seric {\
28532d19d43Seric 	register int i;\
28632d19d43Seric \
28732d19d43Seric 	for (i = NFORKTRIES; i-- > 0; )\
28832d19d43Seric 	{\
28932d19d43Seric 		pid = fORKfN();\
29032d19d43Seric 		if (pid >= 0)\
29132d19d43Seric 			break;\
29232d19d43Seric 		sleep((unsigned) NFORKTRIES - i);\
29332d19d43Seric 	}\
29432d19d43Seric }
29532d19d43Seric /*
2965dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
2975dfc646bSeric **
2985dfc646bSeric **	Parameters:
2995dfc646bSeric **		m -- mailer descriptor.
3005dfc646bSeric **		pvp -- parameter vector to send to it.
3015dfc646bSeric **		editfcn -- function to pipe it through.
3025dfc646bSeric **
3035dfc646bSeric **	Returns:
3045dfc646bSeric **		exit status of mailer.
3055dfc646bSeric **
3065dfc646bSeric **	Side Effects:
3075dfc646bSeric **		none.
3085dfc646bSeric */
3095dfc646bSeric 
3105dfc646bSeric sendoff(m, pvp, editfcn)
3115dfc646bSeric 	struct mailer *m;
3125dfc646bSeric 	char **pvp;
3135dfc646bSeric 	int (*editfcn)();
3145dfc646bSeric {
3155dfc646bSeric 	auto int st;
3165dfc646bSeric 	register int i;
3175dfc646bSeric 	int pid;
3185dfc646bSeric 	int pvect[2];
3195dfc646bSeric 	FILE *mfile;
3205dfc646bSeric 	extern putmessage();
3215dfc646bSeric 	extern FILE *fdopen();
3225dfc646bSeric 
3235dfc646bSeric # ifdef DEBUG
3245dfc646bSeric 	if (Debug)
3255dfc646bSeric 	{
3265dfc646bSeric 		printf("Sendoff:\n");
3275dfc646bSeric 		printav(pvp);
3285dfc646bSeric 	}
3295dfc646bSeric # endif DEBUG
3305dfc646bSeric 
3316328bdf7Seric 	/* create a pipe to shove the mail through */
3326328bdf7Seric 	if (pipe(pvect) < 0)
33325a99e2eSeric 	{
33425a99e2eSeric 		syserr("pipe");
33525a99e2eSeric 		return (-1);
33625a99e2eSeric 	}
33732d19d43Seric 	DOFORK(XFORK);
33825a99e2eSeric 	if (pid < 0)
33925a99e2eSeric 	{
34025a99e2eSeric 		syserr("Cannot fork");
341db8841e9Seric 		(void) close(pvect[0]);
342db8841e9Seric 		(void) close(pvect[1]);
34325a99e2eSeric 		return (-1);
34425a99e2eSeric 	}
34525a99e2eSeric 	else if (pid == 0)
34625a99e2eSeric 	{
34725a99e2eSeric 		/* child -- set up input & exec mailer */
34803ab8e55Seric 		/* make diagnostic output be standard output */
3490984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
3500984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
3510984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
352db8841e9Seric 		(void) close(2);
353db8841e9Seric 		(void) dup(1);
354db8841e9Seric 		(void) close(0);
35525a99e2eSeric 		if (dup(pvect[0]) < 0)
35625a99e2eSeric 		{
35725a99e2eSeric 			syserr("Cannot dup to zero!");
358a590b978Seric 			_exit(EX_OSERR);
35925a99e2eSeric 		}
360db8841e9Seric 		(void) close(pvect[0]);
361db8841e9Seric 		(void) close(pvect[1]);
3622a6e0786Seric 		if (!bitset(M_RESTR, m->m_flags))
3630984da9fSeric 		{
364db8841e9Seric 			(void) setuid(getuid());
3650984da9fSeric 			(void) setgid(getgid());
3660984da9fSeric 		}
367e374fd72Seric # ifndef VFORK
368e374fd72Seric 		/*
369e374fd72Seric 		**  We have to be careful with vfork - we can't mung up the
370e374fd72Seric 		**  memory but we don't want the mailer to inherit any extra
371e374fd72Seric 		**  open files.  Chances are the mailer won't
372e374fd72Seric 		**  care about an extra file, but then again you never know.
373e374fd72Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
374e374fd72Seric 		**  declared static so we can't.  But if we fclose(pwf), which
375e374fd72Seric 		**  is what endpwent does, it closes it in the parent too and
376e374fd72Seric 		**  the next getpwnam will be slower.  If you have a weird
377e374fd72Seric 		**  mailer that chokes on the extra file you should do the
378e374fd72Seric 		**  endpwent().
379e374fd72Seric 		**
380e374fd72Seric 		**  Similar comments apply to log.  However, openlog is
381e374fd72Seric 		**  clever enough to set the FIOCLEX mode on the file,
382e374fd72Seric 		**  so it will be closed automatically on the exec.
383e374fd72Seric 		*/
384e374fd72Seric 
385e374fd72Seric 		endpwent();
38625a99e2eSeric # ifdef LOG
387f9fe028fSeric 		closelog();
38825a99e2eSeric # endif LOG
389e374fd72Seric # endif VFORK
39025a99e2eSeric 		execv(m->m_mailer, pvp);
39125a99e2eSeric 		/* syserr fails because log is closed */
39225a99e2eSeric 		/* syserr("Cannot exec %s", m->m_mailer); */
39332d19d43Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
394db8841e9Seric 		(void) fflush(stdout);
395a590b978Seric 		_exit(EX_UNAVAILABLE);
39625a99e2eSeric 	}
39725a99e2eSeric 
3986328bdf7Seric 	/* write out message to mailer */
399db8841e9Seric 	(void) close(pvect[0]);
40054aa2b0fSeric 	(void) signal(SIGPIPE, SIG_IGN);
40125a99e2eSeric 	mfile = fdopen(pvect[1], "w");
4026328bdf7Seric 	if (editfcn == NULL)
4036328bdf7Seric 		editfcn = putmessage;
4046328bdf7Seric 	(*editfcn)(mfile, m);
405db8841e9Seric 	(void) fclose(mfile);
40625a99e2eSeric 
40725a99e2eSeric 	/*
40825a99e2eSeric 	**  Wait for child to die and report status.
40925a99e2eSeric 	**	We should never get fatal errors (e.g., segmentation
41025a99e2eSeric 	**	violation), so we report those specially.  For other
41125a99e2eSeric 	**	errors, we choose a status message (into statmsg),
41225a99e2eSeric 	**	and if it represents an error, we print it.
41325a99e2eSeric 	*/
41425a99e2eSeric 
41525a99e2eSeric 	while ((i = wait(&st)) > 0 && i != pid)
41625a99e2eSeric 		continue;
41725a99e2eSeric 	if (i < 0)
41825a99e2eSeric 	{
41925a99e2eSeric 		syserr("wait");
42025a99e2eSeric 		return (-1);
42125a99e2eSeric 	}
42225a99e2eSeric 	if ((st & 0377) != 0)
42325a99e2eSeric 	{
42425a99e2eSeric 		syserr("%s: stat %o", pvp[0], st);
425df2792f7Seric 		ExitStat = EX_UNAVAILABLE;
42625a99e2eSeric 		return (-1);
42725a99e2eSeric 	}
42825a99e2eSeric 	i = (st >> 8) & 0377;
4292ac087dbSeric 	giveresponse(i, TRUE, m);
43025a99e2eSeric 	return (i);
43125a99e2eSeric }
43225a99e2eSeric /*
43325a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
43425a99e2eSeric **
43525a99e2eSeric **	Parameters:
43625a99e2eSeric **		stat -- the status code from the mailer (high byte
43725a99e2eSeric **			only; core dumps must have been taken care of
43825a99e2eSeric **			already).
43925a99e2eSeric **		force -- if set, force an error message output, even
44025a99e2eSeric **			if the mailer seems to like to print its own
44125a99e2eSeric **			messages.
44225a99e2eSeric **		m -- the mailer descriptor for this mailer.
44325a99e2eSeric **
44425a99e2eSeric **	Returns:
445db8841e9Seric **		none.
44625a99e2eSeric **
44725a99e2eSeric **	Side Effects:
448c1f9df2cSeric **		Errors may be incremented.
44925a99e2eSeric **		ExitStat may be set.
45025a99e2eSeric **
45125a99e2eSeric **	Called By:
45225a99e2eSeric **		deliver
45325a99e2eSeric */
45425a99e2eSeric 
45525a99e2eSeric giveresponse(stat, force, m)
45625a99e2eSeric 	int stat;
45725a99e2eSeric 	int force;
45825a99e2eSeric 	register struct mailer *m;
45925a99e2eSeric {
46025a99e2eSeric 	register char *statmsg;
46125a99e2eSeric 	extern char *SysExMsg[];
46225a99e2eSeric 	register int i;
46325a99e2eSeric 	extern int N_SysEx;
46429dd97a3Seric 	char buf[30];
46525a99e2eSeric 
46625a99e2eSeric 	i = stat - EX__BASE;
46725a99e2eSeric 	if (i < 0 || i > N_SysEx)
46825a99e2eSeric 		statmsg = NULL;
46925a99e2eSeric 	else
47025a99e2eSeric 		statmsg = SysExMsg[i];
47125a99e2eSeric 	if (stat == 0)
472c38ba59cSeric 	{
4736cbfa739Seric 		if (bitset(M_LOCAL, m->m_flags))
4743efaed6eSeric 			statmsg = "delivered";
4753efaed6eSeric 		else
4763efaed6eSeric 			statmsg = "queued";
477c38ba59cSeric 		if (Verbose)
478544026c5Seric 			message(Arpa_Info, statmsg);
479c38ba59cSeric 	}
48025a99e2eSeric 	else
48125a99e2eSeric 	{
482c1f9df2cSeric 		Errors++;
48325a99e2eSeric 		if (statmsg == NULL && m->m_badstat != 0)
48425a99e2eSeric 		{
48525a99e2eSeric 			stat = m->m_badstat;
48625a99e2eSeric 			i = stat - EX__BASE;
48725a99e2eSeric # ifdef DEBUG
48825a99e2eSeric 			if (i < 0 || i >= N_SysEx)
48925a99e2eSeric 				syserr("Bad m_badstat %d", stat);
49025a99e2eSeric 			else
49125a99e2eSeric # endif DEBUG
49225a99e2eSeric 			statmsg = SysExMsg[i];
49325a99e2eSeric 		}
49425a99e2eSeric 		if (statmsg == NULL)
49525a99e2eSeric 			usrerr("unknown mailer response %d", stat);
496c38ba59cSeric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
49725a99e2eSeric 			usrerr("%s", statmsg);
49825a99e2eSeric 	}
49925a99e2eSeric 
50025a99e2eSeric 	/*
50125a99e2eSeric 	**  Final cleanup.
50225a99e2eSeric 	**	Log a record of the transaction.  Compute the new
50325a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
50425a99e2eSeric 	**	that.
50525a99e2eSeric 	*/
50625a99e2eSeric 
50725a99e2eSeric 	if (statmsg == NULL)
50829dd97a3Seric 	{
509db8841e9Seric 		(void) sprintf(buf, "error %d", stat);
51029dd97a3Seric 		statmsg = buf;
51129dd97a3Seric 	}
51229dd97a3Seric 
51329dd97a3Seric # ifdef LOG
514e374fd72Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
51525a99e2eSeric # endif LOG
516243921eeSeric 	setstat(stat);
51725a99e2eSeric }
51825a99e2eSeric /*
5196328bdf7Seric **  PUTMESSAGE -- output a message to the final mailer.
52025a99e2eSeric **
5216328bdf7Seric **	This routine takes care of recreating the header from the
5226328bdf7Seric **	in-core copy, etc.
52325a99e2eSeric **
52425a99e2eSeric **	Parameters:
5256328bdf7Seric **		fp -- file to output onto.
5266328bdf7Seric **		m -- a mailer descriptor.
52725a99e2eSeric **
52825a99e2eSeric **	Returns:
5296328bdf7Seric **		none.
53025a99e2eSeric **
53125a99e2eSeric **	Side Effects:
5326328bdf7Seric **		The message is written onto fp.
53325a99e2eSeric */
53425a99e2eSeric 
5356328bdf7Seric putmessage(fp, m)
5366328bdf7Seric 	FILE *fp;
5376328bdf7Seric 	struct mailer *m;
53825a99e2eSeric {
5396328bdf7Seric 	char buf[BUFSIZ];
5406328bdf7Seric 	register int i;
5416328bdf7Seric 	HDR *h;
542b07ac16bSeric 	register char *p;
5436328bdf7Seric 	extern char *arpadate();
5446328bdf7Seric 	bool anyheader = FALSE;
545e9ff65b0Seric 	extern char *capitalize();
54625a99e2eSeric 
54740e4ab56Seric 	/* output "From" line unless supressed */
54840e4ab56Seric 	if (!bitset(M_NHDR, m->m_flags))
5491412cc7cSeric 	{
5501412cc7cSeric 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
5511412cc7cSeric 		fprintf(fp, "%s\n", buf);
5521412cc7cSeric 	}
55340e4ab56Seric 
5544ae18d0eSeric 	/* output all header lines */
5556328bdf7Seric 	for (h = Header; h != NULL; h = h->h_link)
5566328bdf7Seric 	{
5579e9163a0Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
5585a0dcb5fSeric 		{
5595a0dcb5fSeric 			p = ")><(";		/* can't happen (I hope) */
5605a0dcb5fSeric 			goto checkfrom;
5615a0dcb5fSeric 		}
5624ae18d0eSeric 		if (bitset(H_DEFAULT, h->h_flags))
5634ae18d0eSeric 		{
564db8841e9Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
5654ae18d0eSeric 			p = buf;
5664ae18d0eSeric 		}
5674ae18d0eSeric 		else
5684ae18d0eSeric 			p = h->h_value;
5699e9163a0Seric 		if (*p == '\0')
5709e9163a0Seric 			continue;
5714ae18d0eSeric 		fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
5726328bdf7Seric 		h->h_flags |= H_USED;
5736328bdf7Seric 		anyheader = TRUE;
5745a0dcb5fSeric 
5755a0dcb5fSeric 		/* hack, hack -- output Original-From field if different */
5765a0dcb5fSeric 	checkfrom:
5775a0dcb5fSeric 		if (strcmp(h->h_field, "from") == 0)
5785a0dcb5fSeric 		{
5795a0dcb5fSeric 			extern char *hvalue();
5805a0dcb5fSeric 			char *ofrom = hvalue("original-from");
5815a0dcb5fSeric 
5825a0dcb5fSeric 			if (ofrom != NULL && strcmp(p, ofrom) != 0)
5835a0dcb5fSeric 				fprintf(fp, "Original-From: %s\n", ofrom);
5845a0dcb5fSeric 		}
5856328bdf7Seric 	}
5866328bdf7Seric 
5876328bdf7Seric 	if (anyheader)
5886328bdf7Seric 		fprintf(fp, "\n");
5896328bdf7Seric 
5906328bdf7Seric 	/* output the body of the message */
591b7902a1dSeric 	rewind(TempFile);
592b7902a1dSeric 	while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0)
593db8841e9Seric 		(void) fwrite(buf, 1, i, fp);
5946328bdf7Seric 
59554aa2b0fSeric 	if (ferror(fp) && errno != EPIPE)
59625a99e2eSeric 	{
5976328bdf7Seric 		syserr("putmessage: write error");
59825a99e2eSeric 		setstat(EX_IOERR);
59925a99e2eSeric 	}
60054aa2b0fSeric 	errno = 0;
60125a99e2eSeric }
60225a99e2eSeric /*
60325a99e2eSeric **  MAILFILE -- Send a message to a file.
60425a99e2eSeric **
60525a99e2eSeric **	Parameters:
60625a99e2eSeric **		filename -- the name of the file to send to.
60725a99e2eSeric **
60825a99e2eSeric **	Returns:
60925a99e2eSeric **		The exit code associated with the operation.
61025a99e2eSeric **
61125a99e2eSeric **	Side Effects:
61225a99e2eSeric **		none.
61325a99e2eSeric */
61425a99e2eSeric 
61525a99e2eSeric mailfile(filename)
61625a99e2eSeric 	char *filename;
61725a99e2eSeric {
61825a99e2eSeric 	register FILE *f;
61932d19d43Seric 	register int pid;
62032d19d43Seric 	register int i;
62125a99e2eSeric 
62232d19d43Seric 	/*
62332d19d43Seric 	**  Fork so we can change permissions here.
62432d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
62532d19d43Seric 	**	the complications of calling subroutines, etc.
62632d19d43Seric 	*/
62732d19d43Seric 
62832d19d43Seric 	DOFORK(fork);
62932d19d43Seric 
63032d19d43Seric 	if (pid < 0)
63132d19d43Seric 		return (EX_OSERR);
63232d19d43Seric 	else if (pid == 0)
63332d19d43Seric 	{
63432d19d43Seric 		/* child -- actually write to file */
63532d19d43Seric 		(void) setuid(getuid());
63632d19d43Seric 		(void) setgid(getgid());
6370984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
6380984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
6390984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
64025a99e2eSeric 		f = fopen(filename, "a");
64125a99e2eSeric 		if (f == NULL)
64232d19d43Seric 			exit(EX_CANTCREAT);
64325a99e2eSeric 
64440e4ab56Seric 		putmessage(f, Mailer[1]);
64525a99e2eSeric 		fputs("\n", f);
646db8841e9Seric 		(void) fclose(f);
64732d19d43Seric 		(void) fflush(stdout);
64832d19d43Seric 		exit(EX_OK);
64932d19d43Seric 	}
65032d19d43Seric 	else
65132d19d43Seric 	{
65232d19d43Seric 		/* parent -- wait for exit status */
65332d19d43Seric 		register int i;
65432d19d43Seric 		auto int stat;
65532d19d43Seric 
65632d19d43Seric 		while ((i = wait(&stat)) != pid)
65732d19d43Seric 		{
65832d19d43Seric 			if (i < 0)
65932d19d43Seric 			{
66032d19d43Seric 				stat = EX_OSERR << 8;
66132d19d43Seric 				break;
66232d19d43Seric 			}
66332d19d43Seric 		}
6640984da9fSeric 		if ((stat & 0377) != 0)
6650984da9fSeric 			stat = EX_UNAVAILABLE << 8;
66632d19d43Seric 		return ((stat >> 8) & 0377);
66732d19d43Seric 	}
66825a99e2eSeric }
669