1294Seric # include <signal.h>
24123Seric # include <errno.h>
33310Seric # include "sendmail.h"
4294Seric # ifdef LOG
52774Seric # include <syslog.h>
6294Seric # endif LOG
7294Seric 
8*4315Seric static char SccsId[] = "@(#)deliver.c	3.35	09/06/81";
9405Seric 
10294Seric /*
11*4315Seric **  DELIVER -- Deliver a message to a list of addresses.
12294Seric **
13*4315Seric **	This routine delivers to everyone on the same host as the
14*4315Seric **	user on the head of the list.  It is clever about mailers
15*4315Seric **	that don't handle multiple users.  It is NOT guaranteed
16*4315Seric **	that it will deliver to all these addresses however -- so
17*4315Seric **	deliver should be called once for each address on the
18*4315Seric **	list.
19*4315Seric **
20294Seric **	Parameters:
21*4315Seric **		to -- head of the address list to deliver to.
22294Seric **		editfcn -- if non-NULL, we want to call this function
23294Seric **			to output the letter (instead of just out-
24294Seric **			putting it raw).
25294Seric **
26294Seric **	Returns:
27294Seric **		zero -- successfully delivered.
28294Seric **		else -- some failure, see ExitStat for more info.
29294Seric **
30294Seric **	Side Effects:
31294Seric **		The standard input is passed off to someone.
32294Seric */
33294Seric 
34294Seric deliver(to, editfcn)
352968Seric 	ADDRESS *to;
36294Seric 	int (*editfcn)();
37294Seric {
38294Seric 	char *host;
39294Seric 	char *user;
40294Seric 	char **pvp;
413233Seric 	register char **mvp;
423233Seric 	register char *p;
433233Seric 	register struct mailer *m;
44294Seric 	register int i;
452898Seric 	extern putmessage();
462968Seric 	extern bool checkcompat();
473233Seric 	char *pv[MAXPV+1];
483233Seric 	char tobuf[MAXLINE];
493233Seric 	char buf[MAXNAME];
503233Seric 	bool firstone;
51294Seric 
524311Seric 	if (!ForceMail && bitset(QDONTSEND, to->q_flags))
533233Seric 		return (0);
54294Seric 
55294Seric # ifdef DEBUG
56294Seric 	if (Debug)
573233Seric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
583233Seric 			to->q_mailer, to->q_host, to->q_user);
59294Seric # endif DEBUG
60294Seric 
61294Seric 	/*
623233Seric 	**  Do initial argv setup.
633233Seric 	**	Insert the mailer name.  Notice that $x expansion is
643233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
653233Seric 	**	a picky -f flag, we insert it as appropriate.  This
663233Seric 	**	code does not check for 'pv' overflow; this places a
673233Seric 	**	manifest lower limit of 4 for MAXPV.
682968Seric 	*/
692968Seric 
703233Seric 	m = Mailer[to->q_mailer];
713233Seric 	host = to->q_host;
723233Seric 	define('g', m->m_from);		/* translated from address */
733233Seric 	define('h', host);		/* to host */
743233Seric 	Errors = 0;
753233Seric 	errno = 0;
763233Seric 	pvp = pv;
773233Seric 	*pvp++ = m->m_argv[0];
782968Seric 
793233Seric 	/* insert -f or -r flag as appropriate */
803233Seric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
813233Seric 	{
823233Seric 		if (bitset(M_FOPT, m->m_flags))
833233Seric 			*pvp++ = "-f";
843233Seric 		else
853233Seric 			*pvp++ = "-r";
864082Seric 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
873233Seric 		*pvp++ = newstr(buf);
883233Seric 	}
89294Seric 
90294Seric 	/*
913233Seric 	**  Append the other fixed parts of the argv.  These run
923233Seric 	**  up to the first entry containing "$u".  There can only
933233Seric 	**  be one of these, and there are only a few more slots
943233Seric 	**  in the pv after it.
95294Seric 	*/
96294Seric 
973233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
98294Seric 	{
993233Seric 		while ((p = index(p, '$')) != NULL)
1003233Seric 			if (*++p == 'u')
1013233Seric 				break;
1023233Seric 		if (p != NULL)
1033233Seric 			break;
1043233Seric 
1053233Seric 		/* this entry is safe -- go ahead and process it */
1064082Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
1073233Seric 		*pvp++ = newstr(buf);
1083233Seric 		if (pvp >= &pv[MAXPV - 3])
1093233Seric 		{
1103233Seric 			syserr("Too many parameters to %s before $u", pv[0]);
1113233Seric 			return (-1);
1123233Seric 		}
113294Seric 	}
1143233Seric 	if (*mvp == NULL)
1153233Seric 		syserr("No $u in mailer argv for %s", pv[0]);
116294Seric 
117294Seric 	/*
1183233Seric 	**  At this point *mvp points to the argument with $u.  We
1193233Seric 	**  run through our address list and append all the addresses
1203233Seric 	**  we can.  If we run out of space, do not fret!  We can
1213233Seric 	**  always send another copy later.
122294Seric 	*/
123294Seric 
1243233Seric 	tobuf[0] = '\0';
1253233Seric 	firstone = TRUE;
1263233Seric 	To = tobuf;
1273233Seric 	for (; to != NULL; to = to->q_next)
128294Seric 	{
1293233Seric 		/* avoid sending multiple recipients to dumb mailers */
1303233Seric 		if (!firstone && !bitset(M_MUSER, m->m_flags))
1313233Seric 			break;
1323233Seric 
1333233Seric 		/* if already sent or not for this host, don't send */
1344311Seric 		if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) ||
1354311Seric 		    strcmp(to->q_host, host) != 0)
1363233Seric 			continue;
1373233Seric 		user = to->q_user;
1383233Seric 		To = to->q_paddr;
1393233Seric 		to->q_flags |= QDONTSEND;
1403233Seric # ifdef DEBUG
1413233Seric 		if (Debug)
1423233Seric 			printf("   send to `%s'\n", user);
1433233Seric # endif DEBUG
1443233Seric 
1453233Seric 		/*
1463233Seric 		**  Check to see that these people are allowed to
1473233Seric 		**  talk to each other.
1483233Seric 		*/
1493233Seric 
1503233Seric 		if (!checkcompat(to))
151294Seric 		{
1523233Seric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
1533233Seric 			continue;
154294Seric 		}
1553233Seric 
1563233Seric 		/*
1574099Seric 		**  Strip quote bits from names if the mailer is dumb
1584099Seric 		**	about them.
1593233Seric 		*/
1603233Seric 
1613233Seric 		if (bitset(M_STRIPQ, m->m_flags))
162294Seric 		{
1634099Seric 			stripquotes(user, TRUE);
1644099Seric 			stripquotes(host, TRUE);
1653233Seric 		}
1664099Seric 		else
1674099Seric 		{
1684099Seric 			stripquotes(user, FALSE);
1694099Seric 			stripquotes(host, FALSE);
1704099Seric 		}
1713233Seric 
1723233Seric 		/*
1734161Seric 		**  If an error message has already been given, don't
1744161Seric 		**	bother to send to this address.
1754161Seric 		**
1764161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
1774161Seric 		**	>> NOTE >> cannot do any further aliasing; that
1784161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
1794161Seric 		*/
1804161Seric 
1814161Seric 		if (bitset(QBADADDR, to->q_flags))
1824161Seric 			continue;
1834161Seric 
1844283Seric 		/* save statistics.... */
1854283Seric 		Stat.stat_nt[to->q_mailer]++;
1864283Seric 		Stat.stat_bt[to->q_mailer] += kbytes(MsgSize);
1874283Seric 
1884161Seric 		/*
1893233Seric 		**  See if this user name is "special".
1903233Seric 		**	If the user name has a slash in it, assume that this
1913233Seric 		**	is a file -- send it off without further ado.
1923233Seric 		**	Note that this means that editfcn's will not
1933233Seric 		**	be applied to the message.  Also note that
1943233Seric 		**	this type of addresses is not processed along
1953233Seric 		**	with the others, so we fudge on the To person.
1963233Seric 		*/
1973233Seric 
1984194Seric 		if (m == Mailer[MN_LOCAL])
1993233Seric 		{
200294Seric 			if (index(user, '/') != NULL)
201294Seric 			{
202294Seric 				i = mailfile(user);
203294Seric 				giveresponse(i, TRUE, m);
2043233Seric 				continue;
205294Seric 			}
206294Seric 		}
2073233Seric 
208*4315Seric 		/*
209*4315Seric 		**  Address is verified -- add this user to mailer
210*4315Seric 		**  argv, and add it to the print list of recipients.
211*4315Seric 		*/
212*4315Seric 
2133233Seric 		/* create list of users for error messages */
2143233Seric 		if (tobuf[0] != '\0')
2154082Seric 			(void) strcat(tobuf, ",");
2164082Seric 		(void) strcat(tobuf, to->q_paddr);
2173233Seric 		define('u', user);		/* to user */
2184078Seric 		define('z', to->q_home);	/* user's home */
2193233Seric 
2203233Seric 		/* expand out this user */
2214305Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
2223233Seric 		*pvp++ = newstr(buf);
2233233Seric 		if (pvp >= &pv[MAXPV - 2])
2243233Seric 		{
2253233Seric 			/* allow some space for trailing parms */
2263233Seric 			break;
2273233Seric 		}
228294Seric 	}
229294Seric 
2304067Seric 	/* see if any addresses still exist */
2314067Seric 	if (tobuf[0] == '\0')
2324067Seric 		return (0);
2334067Seric 
2343233Seric 	/* print out messages as full list */
2353233Seric 	To = tobuf;
2363233Seric 
237294Seric 	/*
2383233Seric 	**  Fill out any parameters after the $u parameter.
239294Seric 	*/
240294Seric 
2413233Seric 	while (*++mvp != NULL)
242294Seric 	{
2434082Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
2443233Seric 		*pvp++ = newstr(buf);
2453233Seric 		if (pvp >= &pv[MAXPV])
2463233Seric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
247294Seric 	}
2483233Seric 	*pvp++ = NULL;
249294Seric 
250294Seric 	/*
251294Seric 	**  Call the mailer.
2522898Seric 	**	The argument vector gets built, pipes
253294Seric 	**	are created as necessary, and we fork & exec as
2542898Seric 	**	appropriate.
255294Seric 	*/
256294Seric 
2573233Seric 	if (editfcn == NULL)
2583233Seric 		editfcn = putmessage;
2593233Seric 	i = sendoff(m, pv, editfcn);
2603233Seric 
2613233Seric 	return (i);
2623233Seric }
2633233Seric /*
2644214Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
2654214Seric **
2664214Seric **	This MUST be a macro, since after a vfork we are running
2674214Seric **	two processes on the same stack!!!
2684214Seric **
2694214Seric **	Parameters:
2704214Seric **		none.
2714214Seric **
2724214Seric **	Returns:
2734214Seric **		From a macro???  You've got to be kidding!
2744214Seric **
2754214Seric **	Side Effects:
2764214Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
2774214Seric **			pid of child in parent, zero in child.
2784214Seric **			-1 on unrecoverable error.
2794214Seric **
2804214Seric **	Notes:
2814214Seric **		I'm awfully sorry this looks so awful.  That's
2824214Seric **		vfork for you.....
2834214Seric */
2844214Seric 
2854214Seric # define NFORKTRIES	5
2864214Seric # ifdef VFORK
2874214Seric # define XFORK	vfork
2884214Seric # else VFORK
2894214Seric # define XFORK	fork
2904214Seric # endif VFORK
2914214Seric 
2924214Seric # define DOFORK(fORKfN) \
2934214Seric {\
2944214Seric 	register int i;\
2954214Seric \
2964214Seric 	for (i = NFORKTRIES; i-- > 0; )\
2974214Seric 	{\
2984214Seric 		pid = fORKfN();\
2994214Seric 		if (pid >= 0)\
3004214Seric 			break;\
3014214Seric 		sleep((unsigned) NFORKTRIES - i);\
3024214Seric 	}\
3034214Seric }
3044214Seric /*
3053233Seric **  SENDOFF -- send off call to mailer & collect response.
3063233Seric **
3073233Seric **	Parameters:
3083233Seric **		m -- mailer descriptor.
3093233Seric **		pvp -- parameter vector to send to it.
3103233Seric **		editfcn -- function to pipe it through.
3113233Seric **
3123233Seric **	Returns:
3133233Seric **		exit status of mailer.
3143233Seric **
3153233Seric **	Side Effects:
3163233Seric **		none.
3173233Seric */
3183233Seric 
3193233Seric sendoff(m, pvp, editfcn)
3203233Seric 	struct mailer *m;
3213233Seric 	char **pvp;
3223233Seric 	int (*editfcn)();
3233233Seric {
3243233Seric 	auto int st;
3253233Seric 	register int i;
3263233Seric 	int pid;
3273233Seric 	int pvect[2];
3283233Seric 	FILE *mfile;
3293233Seric 	extern putmessage();
3303233Seric 	extern FILE *fdopen();
3313233Seric 
3323233Seric # ifdef DEBUG
3333233Seric 	if (Debug)
334294Seric 	{
3353233Seric 		printf("Sendoff:\n");
3363233Seric 		printav(pvp);
337294Seric 	}
3383233Seric # endif DEBUG
3393233Seric 
3402898Seric 	/* create a pipe to shove the mail through */
3412898Seric 	if (pipe(pvect) < 0)
342294Seric 	{
343294Seric 		syserr("pipe");
344294Seric 		return (-1);
345294Seric 	}
3464214Seric 	DOFORK(XFORK);
347294Seric 	if (pid < 0)
348294Seric 	{
349294Seric 		syserr("Cannot fork");
3504082Seric 		(void) close(pvect[0]);
3514082Seric 		(void) close(pvect[1]);
352294Seric 		return (-1);
353294Seric 	}
354294Seric 	else if (pid == 0)
355294Seric 	{
356294Seric 		/* child -- set up input & exec mailer */
3571621Seric 		/* make diagnostic output be standard output */
3584215Seric 		(void) signal(SIGINT, SIG_DFL);
3594215Seric 		(void) signal(SIGHUP, SIG_DFL);
3604215Seric 		(void) signal(SIGTERM, SIG_DFL);
3614082Seric 		(void) close(2);
3624082Seric 		(void) dup(1);
3634082Seric 		(void) close(0);
3642898Seric 		if (dup(pvect[0]) < 0)
365294Seric 		{
3662898Seric 			syserr("Cannot dup to zero!");
3672898Seric 			_exit(EX_OSERR);
368294Seric 		}
3694082Seric 		(void) close(pvect[0]);
3704082Seric 		(void) close(pvect[1]);
3712968Seric 		if (!bitset(M_RESTR, m->m_flags))
3724215Seric 		{
3734082Seric 			(void) setuid(getuid());
3744215Seric 			(void) setgid(getgid());
3754215Seric 		}
3762774Seric # ifndef VFORK
3772774Seric 		/*
3782774Seric 		**  We have to be careful with vfork - we can't mung up the
3792774Seric 		**  memory but we don't want the mailer to inherit any extra
3802774Seric 		**  open files.  Chances are the mailer won't
3812774Seric 		**  care about an extra file, but then again you never know.
3822774Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
3832774Seric 		**  declared static so we can't.  But if we fclose(pwf), which
3842774Seric 		**  is what endpwent does, it closes it in the parent too and
3852774Seric 		**  the next getpwnam will be slower.  If you have a weird
3862774Seric 		**  mailer that chokes on the extra file you should do the
3872774Seric 		**  endpwent().
3882774Seric 		**
3892774Seric 		**  Similar comments apply to log.  However, openlog is
3902774Seric 		**  clever enough to set the FIOCLEX mode on the file,
3912774Seric 		**  so it will be closed automatically on the exec.
3922774Seric 		*/
3932774Seric 
3942774Seric 		endpwent();
395294Seric # ifdef LOG
3962089Seric 		closelog();
397294Seric # endif LOG
3982774Seric # endif VFORK
399294Seric 		execv(m->m_mailer, pvp);
400294Seric 		/* syserr fails because log is closed */
401294Seric 		/* syserr("Cannot exec %s", m->m_mailer); */
4024214Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
4034082Seric 		(void) fflush(stdout);
4041619Seric 		_exit(EX_UNAVAILABLE);
405294Seric 	}
406294Seric 
4072898Seric 	/* write out message to mailer */
4084082Seric 	(void) close(pvect[0]);
4094123Seric 	(void) signal(SIGPIPE, SIG_IGN);
4102898Seric 	mfile = fdopen(pvect[1], "w");
4112898Seric 	if (editfcn == NULL)
4122898Seric 		editfcn = putmessage;
4132898Seric 	(*editfcn)(mfile, m);
4144082Seric 	(void) fclose(mfile);
415294Seric 
416294Seric 	/*
417294Seric 	**  Wait for child to die and report status.
418294Seric 	**	We should never get fatal errors (e.g., segmentation
419294Seric 	**	violation), so we report those specially.  For other
420294Seric 	**	errors, we choose a status message (into statmsg),
421294Seric 	**	and if it represents an error, we print it.
422294Seric 	*/
423294Seric 
424294Seric 	while ((i = wait(&st)) > 0 && i != pid)
425294Seric 		continue;
426294Seric 	if (i < 0)
427294Seric 	{
428294Seric 		syserr("wait");
429294Seric 		return (-1);
430294Seric 	}
431294Seric 	if ((st & 0377) != 0)
432294Seric 	{
433294Seric 		syserr("%s: stat %o", pvp[0], st);
4341597Seric 		ExitStat = EX_UNAVAILABLE;
435294Seric 		return (-1);
436294Seric 	}
437294Seric 	i = (st >> 8) & 0377;
4382343Seric 	giveresponse(i, TRUE, m);
439294Seric 	return (i);
440294Seric }
441294Seric /*
442294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
443294Seric **
444294Seric **	Parameters:
445294Seric **		stat -- the status code from the mailer (high byte
446294Seric **			only; core dumps must have been taken care of
447294Seric **			already).
448294Seric **		force -- if set, force an error message output, even
449294Seric **			if the mailer seems to like to print its own
450294Seric **			messages.
451294Seric **		m -- the mailer descriptor for this mailer.
452294Seric **
453294Seric **	Returns:
4544082Seric **		none.
455294Seric **
456294Seric **	Side Effects:
4571518Seric **		Errors may be incremented.
458294Seric **		ExitStat may be set.
459294Seric */
460294Seric 
461294Seric giveresponse(stat, force, m)
462294Seric 	int stat;
463294Seric 	int force;
464294Seric 	register struct mailer *m;
465294Seric {
466294Seric 	register char *statmsg;
467294Seric 	extern char *SysExMsg[];
468294Seric 	register int i;
469294Seric 	extern int N_SysEx;
4701624Seric 	char buf[30];
471294Seric 
472*4315Seric 	/*
473*4315Seric 	**  Compute status message from code.
474*4315Seric 	*/
475*4315Seric 
476294Seric 	i = stat - EX__BASE;
477294Seric 	if (i < 0 || i > N_SysEx)
478294Seric 		statmsg = NULL;
479294Seric 	else
480294Seric 		statmsg = SysExMsg[i];
481294Seric 	if (stat == 0)
4824065Seric 	{
4834194Seric 		if (bitset(M_LOCAL, m->m_flags))
4844161Seric 			statmsg = "delivered";
4854161Seric 		else
4864161Seric 			statmsg = "queued";
4874065Seric 		if (Verbose)
4884166Seric 			message(Arpa_Info, statmsg);
4894065Seric 	}
490294Seric 	else
491294Seric 	{
4921518Seric 		Errors++;
493294Seric 		if (statmsg == NULL && m->m_badstat != 0)
494294Seric 		{
495294Seric 			stat = m->m_badstat;
496294Seric 			i = stat - EX__BASE;
497294Seric # ifdef DEBUG
498294Seric 			if (i < 0 || i >= N_SysEx)
499294Seric 				syserr("Bad m_badstat %d", stat);
500294Seric 			else
501294Seric # endif DEBUG
502294Seric 			statmsg = SysExMsg[i];
503294Seric 		}
504294Seric 		if (statmsg == NULL)
505294Seric 			usrerr("unknown mailer response %d", stat);
5064065Seric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
507294Seric 			usrerr("%s", statmsg);
508294Seric 	}
509294Seric 
510294Seric 	/*
511294Seric 	**  Final cleanup.
512294Seric 	**	Log a record of the transaction.  Compute the new
513294Seric 	**	ExitStat -- if we already had an error, stick with
514294Seric 	**	that.
515294Seric 	*/
516294Seric 
5171624Seric 	if (statmsg == NULL)
5181624Seric 	{
5194082Seric 		(void) sprintf(buf, "error %d", stat);
5201624Seric 		statmsg = buf;
5211624Seric 	}
5221624Seric 
523294Seric # ifdef LOG
5242774Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
525294Seric # endif LOG
5261389Seric 	setstat(stat);
527294Seric }
528294Seric /*
5292898Seric **  PUTMESSAGE -- output a message to the final mailer.
530294Seric **
5312898Seric **	This routine takes care of recreating the header from the
5322898Seric **	in-core copy, etc.
533294Seric **
534294Seric **	Parameters:
5352898Seric **		fp -- file to output onto.
5362898Seric **		m -- a mailer descriptor.
537294Seric **
538294Seric **	Returns:
5392898Seric **		none.
540294Seric **
541294Seric **	Side Effects:
5422898Seric **		The message is written onto fp.
543294Seric */
544294Seric 
5452898Seric putmessage(fp, m)
5462898Seric 	FILE *fp;
5472898Seric 	struct mailer *m;
548294Seric {
5492898Seric 	char buf[BUFSIZ];
5502898Seric 	register int i;
551*4315Seric 	register HDR *h;
5522898Seric 	extern char *arpadate();
5532898Seric 	bool anyheader = FALSE;
5543044Seric 	extern char *capitalize();
555294Seric 
556*4315Seric 	/*
557*4315Seric 	**  Output "From" line unless supressed
558*4315Seric 	*/
559*4315Seric 
5603186Seric 	if (!bitset(M_NHDR, m->m_flags))
5614205Seric 	{
5624205Seric 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
5634205Seric 		fprintf(fp, "%s\n", buf);
5644205Seric 	}
5653186Seric 
566*4315Seric 	/*
567*4315Seric 	**  Output all header lines
568*4315Seric 	*/
569*4315Seric 
5702898Seric 	for (h = Header; h != NULL; h = h->h_link)
5711828Seric 	{
572*4315Seric 		register char *p;
573*4315Seric 
5743389Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
5754209Seric 		{
5764209Seric 			p = ")><(";		/* can't happen (I hope) */
5774209Seric 			goto checkfrom;
5784209Seric 		}
5793385Seric 		if (bitset(H_DEFAULT, h->h_flags))
5803385Seric 		{
5814082Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
5823385Seric 			p = buf;
5833385Seric 		}
5842898Seric 		else
5853385Seric 			p = h->h_value;
5863389Seric 		if (*p == '\0')
5873389Seric 			continue;
5883385Seric 		fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
5892898Seric 		h->h_flags |= H_USED;
5902898Seric 		anyheader = TRUE;
5914209Seric 
5924209Seric 		/* hack, hack -- output Original-From field if different */
5934209Seric 	checkfrom:
5944209Seric 		if (strcmp(h->h_field, "from") == 0)
5954209Seric 		{
5964209Seric 			extern char *hvalue();
5974209Seric 			char *ofrom = hvalue("original-from");
5984209Seric 
5994209Seric 			if (ofrom != NULL && strcmp(p, ofrom) != 0)
6004209Seric 				fprintf(fp, "Original-From: %s\n", ofrom);
6014209Seric 		}
6022898Seric 	}
6032898Seric 	if (anyheader)
6042898Seric 		fprintf(fp, "\n");
6052898Seric 
606*4315Seric 	/*
607*4315Seric 	**  Output the body of the message
608*4315Seric 	*/
609*4315Seric 
6104181Seric 	rewind(TempFile);
6114181Seric 	while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0)
6124082Seric 		(void) fwrite(buf, 1, i, fp);
6132898Seric 
6144123Seric 	if (ferror(fp) && errno != EPIPE)
615294Seric 	{
6162898Seric 		syserr("putmessage: write error");
617294Seric 		setstat(EX_IOERR);
618294Seric 	}
6194123Seric 	errno = 0;
620294Seric }
621294Seric /*
622294Seric **  MAILFILE -- Send a message to a file.
623294Seric **
624294Seric **	Parameters:
625294Seric **		filename -- the name of the file to send to.
626294Seric **
627294Seric **	Returns:
628294Seric **		The exit code associated with the operation.
629294Seric **
630294Seric **	Side Effects:
631294Seric **		none.
632294Seric */
633294Seric 
634294Seric mailfile(filename)
635294Seric 	char *filename;
636294Seric {
637294Seric 	register FILE *f;
6384214Seric 	register int pid;
639294Seric 
6404214Seric 	/*
6414214Seric 	**  Fork so we can change permissions here.
6424214Seric 	**	Note that we MUST use fork, not vfork, because of
6434214Seric 	**	the complications of calling subroutines, etc.
6444214Seric 	*/
6454067Seric 
6464214Seric 	DOFORK(fork);
6474214Seric 
6484214Seric 	if (pid < 0)
6494214Seric 		return (EX_OSERR);
6504214Seric 	else if (pid == 0)
6514214Seric 	{
6524214Seric 		/* child -- actually write to file */
6534214Seric 		(void) setuid(getuid());
6544214Seric 		(void) setgid(getgid());
6554215Seric 		(void) signal(SIGINT, SIG_DFL);
6564215Seric 		(void) signal(SIGHUP, SIG_DFL);
6574215Seric 		(void) signal(SIGTERM, SIG_DFL);
6584214Seric 		f = fopen(filename, "a");
6594214Seric 		if (f == NULL)
6604214Seric 			exit(EX_CANTCREAT);
6614214Seric 
6624214Seric 		putmessage(f, Mailer[1]);
6634214Seric 		fputs("\n", f);
6644214Seric 		(void) fclose(f);
6654214Seric 		(void) fflush(stdout);
6664214Seric 		exit(EX_OK);
667*4315Seric 		/*NOTREACHED*/
6684214Seric 	}
6694214Seric 	else
6704214Seric 	{
6714214Seric 		/* parent -- wait for exit status */
6724214Seric 		register int i;
6734214Seric 		auto int stat;
6744214Seric 
6754214Seric 		while ((i = wait(&stat)) != pid)
6764214Seric 		{
6774214Seric 			if (i < 0)
6784214Seric 			{
6794214Seric 				stat = EX_OSERR << 8;
6804214Seric 				break;
6814214Seric 			}
6824214Seric 		}
6834215Seric 		if ((stat & 0377) != 0)
6844215Seric 			stat = EX_UNAVAILABLE << 8;
6854214Seric 		return ((stat >> 8) & 0377);
6864214Seric 	}
687294Seric }
688