1294Seric # include <signal.h>
24123Seric # include <errno.h>
33310Seric # include "sendmail.h"
4294Seric # ifdef LOG
52774Seric # include <syslog.h>
6294Seric # endif LOG
7294Seric 
8*4305Seric static char SccsId[] = "@(#)deliver.c	3.33	09/06/81";
9405Seric 
10294Seric /*
11294Seric **  DELIVER -- Deliver a message to a particular address.
12294Seric **
13294Seric **	Parameters:
14294Seric **		to -- the address to deliver the message to.
15294Seric **		editfcn -- if non-NULL, we want to call this function
16294Seric **			to output the letter (instead of just out-
17294Seric **			putting it raw).
18294Seric **
19294Seric **	Returns:
20294Seric **		zero -- successfully delivered.
21294Seric **		else -- some failure, see ExitStat for more info.
22294Seric **
23294Seric **	Side Effects:
24294Seric **		The standard input is passed off to someone.
25294Seric */
26294Seric 
27294Seric deliver(to, editfcn)
282968Seric 	ADDRESS *to;
29294Seric 	int (*editfcn)();
30294Seric {
31294Seric 	char *host;
32294Seric 	char *user;
33294Seric 	char **pvp;
343233Seric 	register char **mvp;
353233Seric 	register char *p;
363233Seric 	register struct mailer *m;
37294Seric 	register int i;
382898Seric 	extern putmessage();
392968Seric 	extern bool checkcompat();
403233Seric 	char *pv[MAXPV+1];
413233Seric 	char tobuf[MAXLINE];
423233Seric 	char buf[MAXNAME];
433233Seric 	bool firstone;
44294Seric 
453233Seric 	if (bitset(QDONTSEND, to->q_flags))
463233Seric 		return (0);
47294Seric 
48294Seric # ifdef DEBUG
49294Seric 	if (Debug)
503233Seric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
513233Seric 			to->q_mailer, to->q_host, to->q_user);
52294Seric # endif DEBUG
53294Seric 
54294Seric 	/*
553233Seric 	**  Do initial argv setup.
563233Seric 	**	Insert the mailer name.  Notice that $x expansion is
573233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
583233Seric 	**	a picky -f flag, we insert it as appropriate.  This
593233Seric 	**	code does not check for 'pv' overflow; this places a
603233Seric 	**	manifest lower limit of 4 for MAXPV.
612968Seric 	*/
622968Seric 
633233Seric 	m = Mailer[to->q_mailer];
643233Seric 	host = to->q_host;
653233Seric 	define('g', m->m_from);		/* translated from address */
663233Seric 	define('h', host);		/* to host */
673233Seric 	Errors = 0;
683233Seric 	errno = 0;
693233Seric 	pvp = pv;
703233Seric 	*pvp++ = m->m_argv[0];
712968Seric 
723233Seric 	/* insert -f or -r flag as appropriate */
733233Seric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
743233Seric 	{
753233Seric 		if (bitset(M_FOPT, m->m_flags))
763233Seric 			*pvp++ = "-f";
773233Seric 		else
783233Seric 			*pvp++ = "-r";
794082Seric 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
803233Seric 		*pvp++ = newstr(buf);
813233Seric 	}
82294Seric 
83294Seric 	/*
843233Seric 	**  Append the other fixed parts of the argv.  These run
853233Seric 	**  up to the first entry containing "$u".  There can only
863233Seric 	**  be one of these, and there are only a few more slots
873233Seric 	**  in the pv after it.
88294Seric 	*/
89294Seric 
903233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
91294Seric 	{
923233Seric 		while ((p = index(p, '$')) != NULL)
933233Seric 			if (*++p == 'u')
943233Seric 				break;
953233Seric 		if (p != NULL)
963233Seric 			break;
973233Seric 
983233Seric 		/* this entry is safe -- go ahead and process it */
994082Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
1003233Seric 		*pvp++ = newstr(buf);
1013233Seric 		if (pvp >= &pv[MAXPV - 3])
1023233Seric 		{
1033233Seric 			syserr("Too many parameters to %s before $u", pv[0]);
1043233Seric 			return (-1);
1053233Seric 		}
106294Seric 	}
1073233Seric 	if (*mvp == NULL)
1083233Seric 		syserr("No $u in mailer argv for %s", pv[0]);
109294Seric 
110294Seric 	/*
1113233Seric 	**  At this point *mvp points to the argument with $u.  We
1123233Seric 	**  run through our address list and append all the addresses
1133233Seric 	**  we can.  If we run out of space, do not fret!  We can
1143233Seric 	**  always send another copy later.
115294Seric 	*/
116294Seric 
1173233Seric 	tobuf[0] = '\0';
1183233Seric 	firstone = TRUE;
1193233Seric 	To = tobuf;
1203233Seric 	for (; to != NULL; to = to->q_next)
121294Seric 	{
1223233Seric 		/* avoid sending multiple recipients to dumb mailers */
1233233Seric 		if (!firstone && !bitset(M_MUSER, m->m_flags))
1243233Seric 			break;
1253233Seric 
1263233Seric 		/* if already sent or not for this host, don't send */
1273233Seric 		if (bitset(QDONTSEND, to->q_flags) || strcmp(to->q_host, host) != 0)
1283233Seric 			continue;
1293233Seric 		user = to->q_user;
1303233Seric 		To = to->q_paddr;
1313233Seric 		to->q_flags |= QDONTSEND;
1323233Seric 		firstone = FALSE;
1333233Seric 
1343233Seric # ifdef DEBUG
1353233Seric 		if (Debug)
1363233Seric 			printf("   send to `%s'\n", user);
1373233Seric # endif DEBUG
1383233Seric 
1393233Seric 		/*
1403233Seric 		**  Check to see that these people are allowed to
1413233Seric 		**  talk to each other.
1423233Seric 		*/
1433233Seric 
1443233Seric 		if (!checkcompat(to))
145294Seric 		{
1463233Seric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
1473233Seric 			continue;
148294Seric 		}
1493233Seric 
1503233Seric 		/*
1514099Seric 		**  Strip quote bits from names if the mailer is dumb
1524099Seric 		**	about them.
1533233Seric 		*/
1543233Seric 
1553233Seric 		if (bitset(M_STRIPQ, m->m_flags))
156294Seric 		{
1574099Seric 			stripquotes(user, TRUE);
1584099Seric 			stripquotes(host, TRUE);
1593233Seric 		}
1604099Seric 		else
1614099Seric 		{
1624099Seric 			stripquotes(user, FALSE);
1634099Seric 			stripquotes(host, FALSE);
1644099Seric 		}
1653233Seric 
1663233Seric 		/*
1674161Seric 		**  If an error message has already been given, don't
1684161Seric 		**	bother to send to this address.
1694161Seric 		**
1704161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
1714161Seric 		**	>> NOTE >> cannot do any further aliasing; that
1724161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
1734161Seric 		*/
1744161Seric 
1754161Seric 		if (bitset(QBADADDR, to->q_flags))
1764161Seric 			continue;
1774161Seric 
1784283Seric 		/* save statistics.... */
1794283Seric 		Stat.stat_nt[to->q_mailer]++;
1804283Seric 		Stat.stat_bt[to->q_mailer] += kbytes(MsgSize);
1814283Seric 
1824161Seric 		/*
1833233Seric 		**  See if this user name is "special".
1843233Seric 		**	If the user name has a slash in it, assume that this
1853233Seric 		**	is a file -- send it off without further ado.
1863233Seric 		**	Note that this means that editfcn's will not
1873233Seric 		**	be applied to the message.  Also note that
1883233Seric 		**	this type of addresses is not processed along
1893233Seric 		**	with the others, so we fudge on the To person.
1903233Seric 		*/
1913233Seric 
1924194Seric 		if (m == Mailer[MN_LOCAL])
1933233Seric 		{
194294Seric 			if (index(user, '/') != NULL)
195294Seric 			{
196294Seric 				i = mailfile(user);
197294Seric 				giveresponse(i, TRUE, m);
1983233Seric 				continue;
199294Seric 			}
200294Seric 		}
2013233Seric 
2023233Seric 		/* create list of users for error messages */
2033233Seric 		if (tobuf[0] != '\0')
2044082Seric 			(void) strcat(tobuf, ",");
2054082Seric 		(void) strcat(tobuf, to->q_paddr);
2063233Seric 		define('u', user);		/* to user */
2074078Seric 		define('z', to->q_home);	/* user's home */
2083233Seric 
2093233Seric 		/* expand out this user */
210*4305Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
2113233Seric 		*pvp++ = newstr(buf);
2123233Seric 		if (pvp >= &pv[MAXPV - 2])
2133233Seric 		{
2143233Seric 			/* allow some space for trailing parms */
2153233Seric 			break;
2163233Seric 		}
217294Seric 	}
218294Seric 
2194067Seric 	/* see if any addresses still exist */
2204067Seric 	if (tobuf[0] == '\0')
2214067Seric 		return (0);
2224067Seric 
2233233Seric 	/* print out messages as full list */
2243233Seric 	To = tobuf;
2253233Seric 
226294Seric 	/*
2273233Seric 	**  Fill out any parameters after the $u parameter.
228294Seric 	*/
229294Seric 
2303233Seric 	while (*++mvp != NULL)
231294Seric 	{
2324082Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
2333233Seric 		*pvp++ = newstr(buf);
2343233Seric 		if (pvp >= &pv[MAXPV])
2353233Seric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
236294Seric 	}
2373233Seric 	*pvp++ = NULL;
238294Seric 
239294Seric 	/*
240294Seric 	**  Call the mailer.
2412898Seric 	**	The argument vector gets built, pipes
242294Seric 	**	are created as necessary, and we fork & exec as
2432898Seric 	**	appropriate.
2444078Seric 	**
2454078Seric 	**	Notice the tacky hack to handle private mailers.
246294Seric 	*/
247294Seric 
2483233Seric 	if (editfcn == NULL)
2493233Seric 		editfcn = putmessage;
2503233Seric 	i = sendoff(m, pv, editfcn);
2513233Seric 
2523233Seric 	return (i);
2533233Seric }
2543233Seric /*
2554214Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
2564214Seric **
2574214Seric **	This MUST be a macro, since after a vfork we are running
2584214Seric **	two processes on the same stack!!!
2594214Seric **
2604214Seric **	Parameters:
2614214Seric **		none.
2624214Seric **
2634214Seric **	Returns:
2644214Seric **		From a macro???  You've got to be kidding!
2654214Seric **
2664214Seric **	Side Effects:
2674214Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
2684214Seric **			pid of child in parent, zero in child.
2694214Seric **			-1 on unrecoverable error.
2704214Seric **
2714214Seric **	Notes:
2724214Seric **		I'm awfully sorry this looks so awful.  That's
2734214Seric **		vfork for you.....
2744214Seric */
2754214Seric 
2764214Seric # define NFORKTRIES	5
2774214Seric # ifdef VFORK
2784214Seric # define XFORK	vfork
2794214Seric # else VFORK
2804214Seric # define XFORK	fork
2814214Seric # endif VFORK
2824214Seric 
2834214Seric # define DOFORK(fORKfN) \
2844214Seric {\
2854214Seric 	register int i;\
2864214Seric \
2874214Seric 	for (i = NFORKTRIES; i-- > 0; )\
2884214Seric 	{\
2894214Seric 		pid = fORKfN();\
2904214Seric 		if (pid >= 0)\
2914214Seric 			break;\
2924214Seric 		sleep((unsigned) NFORKTRIES - i);\
2934214Seric 	}\
2944214Seric }
2954214Seric /*
2963233Seric **  SENDOFF -- send off call to mailer & collect response.
2973233Seric **
2983233Seric **	Parameters:
2993233Seric **		m -- mailer descriptor.
3003233Seric **		pvp -- parameter vector to send to it.
3013233Seric **		editfcn -- function to pipe it through.
3023233Seric **
3033233Seric **	Returns:
3043233Seric **		exit status of mailer.
3053233Seric **
3063233Seric **	Side Effects:
3073233Seric **		none.
3083233Seric */
3093233Seric 
3103233Seric sendoff(m, pvp, editfcn)
3113233Seric 	struct mailer *m;
3123233Seric 	char **pvp;
3133233Seric 	int (*editfcn)();
3143233Seric {
3153233Seric 	auto int st;
3163233Seric 	register int i;
3173233Seric 	int pid;
3183233Seric 	int pvect[2];
3193233Seric 	FILE *mfile;
3203233Seric 	extern putmessage();
3213233Seric 	extern FILE *fdopen();
3223233Seric 
3233233Seric # ifdef DEBUG
3243233Seric 	if (Debug)
325294Seric 	{
3263233Seric 		printf("Sendoff:\n");
3273233Seric 		printav(pvp);
328294Seric 	}
3293233Seric # endif DEBUG
3303233Seric 
3312898Seric 	/* create a pipe to shove the mail through */
3322898Seric 	if (pipe(pvect) < 0)
333294Seric 	{
334294Seric 		syserr("pipe");
335294Seric 		return (-1);
336294Seric 	}
3374214Seric 	DOFORK(XFORK);
338294Seric 	if (pid < 0)
339294Seric 	{
340294Seric 		syserr("Cannot fork");
3414082Seric 		(void) close(pvect[0]);
3424082Seric 		(void) close(pvect[1]);
343294Seric 		return (-1);
344294Seric 	}
345294Seric 	else if (pid == 0)
346294Seric 	{
347294Seric 		/* child -- set up input & exec mailer */
3481621Seric 		/* make diagnostic output be standard output */
3494215Seric 		(void) signal(SIGINT, SIG_DFL);
3504215Seric 		(void) signal(SIGHUP, SIG_DFL);
3514215Seric 		(void) signal(SIGTERM, SIG_DFL);
3524082Seric 		(void) close(2);
3534082Seric 		(void) dup(1);
3544082Seric 		(void) close(0);
3552898Seric 		if (dup(pvect[0]) < 0)
356294Seric 		{
3572898Seric 			syserr("Cannot dup to zero!");
3582898Seric 			_exit(EX_OSERR);
359294Seric 		}
3604082Seric 		(void) close(pvect[0]);
3614082Seric 		(void) close(pvect[1]);
3622968Seric 		if (!bitset(M_RESTR, m->m_flags))
3634215Seric 		{
3644082Seric 			(void) setuid(getuid());
3654215Seric 			(void) setgid(getgid());
3664215Seric 		}
3672774Seric # ifndef VFORK
3682774Seric 		/*
3692774Seric 		**  We have to be careful with vfork - we can't mung up the
3702774Seric 		**  memory but we don't want the mailer to inherit any extra
3712774Seric 		**  open files.  Chances are the mailer won't
3722774Seric 		**  care about an extra file, but then again you never know.
3732774Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
3742774Seric 		**  declared static so we can't.  But if we fclose(pwf), which
3752774Seric 		**  is what endpwent does, it closes it in the parent too and
3762774Seric 		**  the next getpwnam will be slower.  If you have a weird
3772774Seric 		**  mailer that chokes on the extra file you should do the
3782774Seric 		**  endpwent().
3792774Seric 		**
3802774Seric 		**  Similar comments apply to log.  However, openlog is
3812774Seric 		**  clever enough to set the FIOCLEX mode on the file,
3822774Seric 		**  so it will be closed automatically on the exec.
3832774Seric 		*/
3842774Seric 
3852774Seric 		endpwent();
386294Seric # ifdef LOG
3872089Seric 		closelog();
388294Seric # endif LOG
3892774Seric # endif VFORK
390294Seric 		execv(m->m_mailer, pvp);
391294Seric 		/* syserr fails because log is closed */
392294Seric 		/* syserr("Cannot exec %s", m->m_mailer); */
3934214Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
3944082Seric 		(void) fflush(stdout);
3951619Seric 		_exit(EX_UNAVAILABLE);
396294Seric 	}
397294Seric 
3982898Seric 	/* write out message to mailer */
3994082Seric 	(void) close(pvect[0]);
4004123Seric 	(void) signal(SIGPIPE, SIG_IGN);
4012898Seric 	mfile = fdopen(pvect[1], "w");
4022898Seric 	if (editfcn == NULL)
4032898Seric 		editfcn = putmessage;
4042898Seric 	(*editfcn)(mfile, m);
4054082Seric 	(void) fclose(mfile);
406294Seric 
407294Seric 	/*
408294Seric 	**  Wait for child to die and report status.
409294Seric 	**	We should never get fatal errors (e.g., segmentation
410294Seric 	**	violation), so we report those specially.  For other
411294Seric 	**	errors, we choose a status message (into statmsg),
412294Seric 	**	and if it represents an error, we print it.
413294Seric 	*/
414294Seric 
415294Seric 	while ((i = wait(&st)) > 0 && i != pid)
416294Seric 		continue;
417294Seric 	if (i < 0)
418294Seric 	{
419294Seric 		syserr("wait");
420294Seric 		return (-1);
421294Seric 	}
422294Seric 	if ((st & 0377) != 0)
423294Seric 	{
424294Seric 		syserr("%s: stat %o", pvp[0], st);
4251597Seric 		ExitStat = EX_UNAVAILABLE;
426294Seric 		return (-1);
427294Seric 	}
428294Seric 	i = (st >> 8) & 0377;
4292343Seric 	giveresponse(i, TRUE, m);
430294Seric 	return (i);
431294Seric }
432294Seric /*
433294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
434294Seric **
435294Seric **	Parameters:
436294Seric **		stat -- the status code from the mailer (high byte
437294Seric **			only; core dumps must have been taken care of
438294Seric **			already).
439294Seric **		force -- if set, force an error message output, even
440294Seric **			if the mailer seems to like to print its own
441294Seric **			messages.
442294Seric **		m -- the mailer descriptor for this mailer.
443294Seric **
444294Seric **	Returns:
4454082Seric **		none.
446294Seric **
447294Seric **	Side Effects:
4481518Seric **		Errors may be incremented.
449294Seric **		ExitStat may be set.
450294Seric **
451294Seric **	Called By:
452294Seric **		deliver
453294Seric */
454294Seric 
455294Seric giveresponse(stat, force, m)
456294Seric 	int stat;
457294Seric 	int force;
458294Seric 	register struct mailer *m;
459294Seric {
460294Seric 	register char *statmsg;
461294Seric 	extern char *SysExMsg[];
462294Seric 	register int i;
463294Seric 	extern int N_SysEx;
4641624Seric 	char buf[30];
465294Seric 
466294Seric 	i = stat - EX__BASE;
467294Seric 	if (i < 0 || i > N_SysEx)
468294Seric 		statmsg = NULL;
469294Seric 	else
470294Seric 		statmsg = SysExMsg[i];
471294Seric 	if (stat == 0)
4724065Seric 	{
4734194Seric 		if (bitset(M_LOCAL, m->m_flags))
4744161Seric 			statmsg = "delivered";
4754161Seric 		else
4764161Seric 			statmsg = "queued";
4774065Seric 		if (Verbose)
4784166Seric 			message(Arpa_Info, statmsg);
4794065Seric 	}
480294Seric 	else
481294Seric 	{
4821518Seric 		Errors++;
483294Seric 		if (statmsg == NULL && m->m_badstat != 0)
484294Seric 		{
485294Seric 			stat = m->m_badstat;
486294Seric 			i = stat - EX__BASE;
487294Seric # ifdef DEBUG
488294Seric 			if (i < 0 || i >= N_SysEx)
489294Seric 				syserr("Bad m_badstat %d", stat);
490294Seric 			else
491294Seric # endif DEBUG
492294Seric 			statmsg = SysExMsg[i];
493294Seric 		}
494294Seric 		if (statmsg == NULL)
495294Seric 			usrerr("unknown mailer response %d", stat);
4964065Seric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
497294Seric 			usrerr("%s", statmsg);
498294Seric 	}
499294Seric 
500294Seric 	/*
501294Seric 	**  Final cleanup.
502294Seric 	**	Log a record of the transaction.  Compute the new
503294Seric 	**	ExitStat -- if we already had an error, stick with
504294Seric 	**	that.
505294Seric 	*/
506294Seric 
5071624Seric 	if (statmsg == NULL)
5081624Seric 	{
5094082Seric 		(void) sprintf(buf, "error %d", stat);
5101624Seric 		statmsg = buf;
5111624Seric 	}
5121624Seric 
513294Seric # ifdef LOG
5142774Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
515294Seric # endif LOG
5161389Seric 	setstat(stat);
517294Seric }
518294Seric /*
5192898Seric **  PUTMESSAGE -- output a message to the final mailer.
520294Seric **
5212898Seric **	This routine takes care of recreating the header from the
5222898Seric **	in-core copy, etc.
523294Seric **
524294Seric **	Parameters:
5252898Seric **		fp -- file to output onto.
5262898Seric **		m -- a mailer descriptor.
527294Seric **
528294Seric **	Returns:
5292898Seric **		none.
530294Seric **
531294Seric **	Side Effects:
5322898Seric **		The message is written onto fp.
533294Seric */
534294Seric 
5352898Seric putmessage(fp, m)
5362898Seric 	FILE *fp;
5372898Seric 	struct mailer *m;
538294Seric {
5392898Seric 	char buf[BUFSIZ];
5402898Seric 	register int i;
5412898Seric 	HDR *h;
5421828Seric 	register char *p;
5432898Seric 	extern char *arpadate();
5442898Seric 	bool anyheader = FALSE;
5453044Seric 	extern char *capitalize();
546294Seric 
5473186Seric 	/* output "From" line unless supressed */
5483186Seric 	if (!bitset(M_NHDR, m->m_flags))
5494205Seric 	{
5504205Seric 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
5514205Seric 		fprintf(fp, "%s\n", buf);
5524205Seric 	}
5533186Seric 
5543385Seric 	/* output all header lines */
5552898Seric 	for (h = Header; h != NULL; h = h->h_link)
5561828Seric 	{
5573389Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
5584209Seric 		{
5594209Seric 			p = ")><(";		/* can't happen (I hope) */
5604209Seric 			goto checkfrom;
5614209Seric 		}
5623385Seric 		if (bitset(H_DEFAULT, h->h_flags))
5633385Seric 		{
5644082Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
5653385Seric 			p = buf;
5663385Seric 		}
5672898Seric 		else
5683385Seric 			p = h->h_value;
5693389Seric 		if (*p == '\0')
5703389Seric 			continue;
5713385Seric 		fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
5722898Seric 		h->h_flags |= H_USED;
5732898Seric 		anyheader = TRUE;
5744209Seric 
5754209Seric 		/* hack, hack -- output Original-From field if different */
5764209Seric 	checkfrom:
5774209Seric 		if (strcmp(h->h_field, "from") == 0)
5784209Seric 		{
5794209Seric 			extern char *hvalue();
5804209Seric 			char *ofrom = hvalue("original-from");
5814209Seric 
5824209Seric 			if (ofrom != NULL && strcmp(p, ofrom) != 0)
5834209Seric 				fprintf(fp, "Original-From: %s\n", ofrom);
5844209Seric 		}
5852898Seric 	}
5862898Seric 
5872898Seric 	if (anyheader)
5882898Seric 		fprintf(fp, "\n");
5892898Seric 
5902898Seric 	/* output the body of the message */
5914181Seric 	rewind(TempFile);
5924181Seric 	while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0)
5934082Seric 		(void) fwrite(buf, 1, i, fp);
5942898Seric 
5954123Seric 	if (ferror(fp) && errno != EPIPE)
596294Seric 	{
5972898Seric 		syserr("putmessage: write error");
598294Seric 		setstat(EX_IOERR);
599294Seric 	}
6004123Seric 	errno = 0;
601294Seric }
602294Seric /*
603294Seric **  MAILFILE -- Send a message to a file.
604294Seric **
605294Seric **	Parameters:
606294Seric **		filename -- the name of the file to send to.
607294Seric **
608294Seric **	Returns:
609294Seric **		The exit code associated with the operation.
610294Seric **
611294Seric **	Side Effects:
612294Seric **		none.
613294Seric */
614294Seric 
615294Seric mailfile(filename)
616294Seric 	char *filename;
617294Seric {
618294Seric 	register FILE *f;
6194214Seric 	register int pid;
6204214Seric 	register int i;
621294Seric 
6224214Seric 	/*
6234214Seric 	**  Fork so we can change permissions here.
6244214Seric 	**	Note that we MUST use fork, not vfork, because of
6254214Seric 	**	the complications of calling subroutines, etc.
6264214Seric 	*/
6274067Seric 
6284214Seric 	DOFORK(fork);
6294214Seric 
6304214Seric 	if (pid < 0)
6314214Seric 		return (EX_OSERR);
6324214Seric 	else if (pid == 0)
6334214Seric 	{
6344214Seric 		/* child -- actually write to file */
6354214Seric 		(void) setuid(getuid());
6364214Seric 		(void) setgid(getgid());
6374215Seric 		(void) signal(SIGINT, SIG_DFL);
6384215Seric 		(void) signal(SIGHUP, SIG_DFL);
6394215Seric 		(void) signal(SIGTERM, SIG_DFL);
6404214Seric 		f = fopen(filename, "a");
6414214Seric 		if (f == NULL)
6424214Seric 			exit(EX_CANTCREAT);
6434214Seric 
6444214Seric 		putmessage(f, Mailer[1]);
6454214Seric 		fputs("\n", f);
6464214Seric 		(void) fclose(f);
6474214Seric 		(void) fflush(stdout);
6484214Seric 		exit(EX_OK);
6494214Seric 	}
6504214Seric 	else
6514214Seric 	{
6524214Seric 		/* parent -- wait for exit status */
6534214Seric 		register int i;
6544214Seric 		auto int stat;
6554214Seric 
6564214Seric 		while ((i = wait(&stat)) != pid)
6574214Seric 		{
6584214Seric 			if (i < 0)
6594214Seric 			{
6604214Seric 				stat = EX_OSERR << 8;
6614214Seric 				break;
6624214Seric 			}
6634214Seric 		}
6644215Seric 		if ((stat & 0377) != 0)
6654215Seric 			stat = EX_UNAVAILABLE << 8;
6664214Seric 		return ((stat >> 8) & 0377);
6674214Seric 	}
668294Seric }
669