1294Seric # include <signal.h>
24123Seric # include <errno.h>
33310Seric # include "sendmail.h"
4294Seric # ifdef LOG
52774Seric # include <syslog.h>
6294Seric # endif LOG
7294Seric 
8*4311Seric static char SccsId[] = "@(#)deliver.c	3.34	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 
45*4311Seric 	if (!ForceMail && 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 */
127*4311Seric 		if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) ||
128*4311Seric 		    strcmp(to->q_host, host) != 0)
1293233Seric 			continue;
1303233Seric 		user = to->q_user;
1313233Seric 		To = to->q_paddr;
1323233Seric 		to->q_flags |= QDONTSEND;
1333233Seric 		firstone = FALSE;
1343233Seric 
1353233Seric # ifdef DEBUG
1363233Seric 		if (Debug)
1373233Seric 			printf("   send to `%s'\n", user);
1383233Seric # endif DEBUG
1393233Seric 
1403233Seric 		/*
1413233Seric 		**  Check to see that these people are allowed to
1423233Seric 		**  talk to each other.
1433233Seric 		*/
1443233Seric 
1453233Seric 		if (!checkcompat(to))
146294Seric 		{
1473233Seric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
1483233Seric 			continue;
149294Seric 		}
1503233Seric 
1513233Seric 		/*
1524099Seric 		**  Strip quote bits from names if the mailer is dumb
1534099Seric 		**	about them.
1543233Seric 		*/
1553233Seric 
1563233Seric 		if (bitset(M_STRIPQ, m->m_flags))
157294Seric 		{
1584099Seric 			stripquotes(user, TRUE);
1594099Seric 			stripquotes(host, TRUE);
1603233Seric 		}
1614099Seric 		else
1624099Seric 		{
1634099Seric 			stripquotes(user, FALSE);
1644099Seric 			stripquotes(host, FALSE);
1654099Seric 		}
1663233Seric 
1673233Seric 		/*
1684161Seric 		**  If an error message has already been given, don't
1694161Seric 		**	bother to send to this address.
1704161Seric 		**
1714161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
1724161Seric 		**	>> NOTE >> cannot do any further aliasing; that
1734161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
1744161Seric 		*/
1754161Seric 
1764161Seric 		if (bitset(QBADADDR, to->q_flags))
1774161Seric 			continue;
1784161Seric 
1794283Seric 		/* save statistics.... */
1804283Seric 		Stat.stat_nt[to->q_mailer]++;
1814283Seric 		Stat.stat_bt[to->q_mailer] += kbytes(MsgSize);
1824283Seric 
1834161Seric 		/*
1843233Seric 		**  See if this user name is "special".
1853233Seric 		**	If the user name has a slash in it, assume that this
1863233Seric 		**	is a file -- send it off without further ado.
1873233Seric 		**	Note that this means that editfcn's will not
1883233Seric 		**	be applied to the message.  Also note that
1893233Seric 		**	this type of addresses is not processed along
1903233Seric 		**	with the others, so we fudge on the To person.
1913233Seric 		*/
1923233Seric 
1934194Seric 		if (m == Mailer[MN_LOCAL])
1943233Seric 		{
195294Seric 			if (index(user, '/') != NULL)
196294Seric 			{
197294Seric 				i = mailfile(user);
198294Seric 				giveresponse(i, TRUE, m);
1993233Seric 				continue;
200294Seric 			}
201294Seric 		}
2023233Seric 
2033233Seric 		/* create list of users for error messages */
2043233Seric 		if (tobuf[0] != '\0')
2054082Seric 			(void) strcat(tobuf, ",");
2064082Seric 		(void) strcat(tobuf, to->q_paddr);
2073233Seric 		define('u', user);		/* to user */
2084078Seric 		define('z', to->q_home);	/* user's home */
2093233Seric 
2103233Seric 		/* expand out this user */
2114305Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
2123233Seric 		*pvp++ = newstr(buf);
2133233Seric 		if (pvp >= &pv[MAXPV - 2])
2143233Seric 		{
2153233Seric 			/* allow some space for trailing parms */
2163233Seric 			break;
2173233Seric 		}
218294Seric 	}
219294Seric 
2204067Seric 	/* see if any addresses still exist */
2214067Seric 	if (tobuf[0] == '\0')
2224067Seric 		return (0);
2234067Seric 
2243233Seric 	/* print out messages as full list */
2253233Seric 	To = tobuf;
2263233Seric 
227294Seric 	/*
2283233Seric 	**  Fill out any parameters after the $u parameter.
229294Seric 	*/
230294Seric 
2313233Seric 	while (*++mvp != NULL)
232294Seric 	{
2334082Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
2343233Seric 		*pvp++ = newstr(buf);
2353233Seric 		if (pvp >= &pv[MAXPV])
2363233Seric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
237294Seric 	}
2383233Seric 	*pvp++ = NULL;
239294Seric 
240294Seric 	/*
241294Seric 	**  Call the mailer.
2422898Seric 	**	The argument vector gets built, pipes
243294Seric 	**	are created as necessary, and we fork & exec as
2442898Seric 	**	appropriate.
2454078Seric 	**
2464078Seric 	**	Notice the tacky hack to handle private mailers.
247294Seric 	*/
248294Seric 
2493233Seric 	if (editfcn == NULL)
2503233Seric 		editfcn = putmessage;
2513233Seric 	i = sendoff(m, pv, editfcn);
2523233Seric 
2533233Seric 	return (i);
2543233Seric }
2553233Seric /*
2564214Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
2574214Seric **
2584214Seric **	This MUST be a macro, since after a vfork we are running
2594214Seric **	two processes on the same stack!!!
2604214Seric **
2614214Seric **	Parameters:
2624214Seric **		none.
2634214Seric **
2644214Seric **	Returns:
2654214Seric **		From a macro???  You've got to be kidding!
2664214Seric **
2674214Seric **	Side Effects:
2684214Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
2694214Seric **			pid of child in parent, zero in child.
2704214Seric **			-1 on unrecoverable error.
2714214Seric **
2724214Seric **	Notes:
2734214Seric **		I'm awfully sorry this looks so awful.  That's
2744214Seric **		vfork for you.....
2754214Seric */
2764214Seric 
2774214Seric # define NFORKTRIES	5
2784214Seric # ifdef VFORK
2794214Seric # define XFORK	vfork
2804214Seric # else VFORK
2814214Seric # define XFORK	fork
2824214Seric # endif VFORK
2834214Seric 
2844214Seric # define DOFORK(fORKfN) \
2854214Seric {\
2864214Seric 	register int i;\
2874214Seric \
2884214Seric 	for (i = NFORKTRIES; i-- > 0; )\
2894214Seric 	{\
2904214Seric 		pid = fORKfN();\
2914214Seric 		if (pid >= 0)\
2924214Seric 			break;\
2934214Seric 		sleep((unsigned) NFORKTRIES - i);\
2944214Seric 	}\
2954214Seric }
2964214Seric /*
2973233Seric **  SENDOFF -- send off call to mailer & collect response.
2983233Seric **
2993233Seric **	Parameters:
3003233Seric **		m -- mailer descriptor.
3013233Seric **		pvp -- parameter vector to send to it.
3023233Seric **		editfcn -- function to pipe it through.
3033233Seric **
3043233Seric **	Returns:
3053233Seric **		exit status of mailer.
3063233Seric **
3073233Seric **	Side Effects:
3083233Seric **		none.
3093233Seric */
3103233Seric 
3113233Seric sendoff(m, pvp, editfcn)
3123233Seric 	struct mailer *m;
3133233Seric 	char **pvp;
3143233Seric 	int (*editfcn)();
3153233Seric {
3163233Seric 	auto int st;
3173233Seric 	register int i;
3183233Seric 	int pid;
3193233Seric 	int pvect[2];
3203233Seric 	FILE *mfile;
3213233Seric 	extern putmessage();
3223233Seric 	extern FILE *fdopen();
3233233Seric 
3243233Seric # ifdef DEBUG
3253233Seric 	if (Debug)
326294Seric 	{
3273233Seric 		printf("Sendoff:\n");
3283233Seric 		printav(pvp);
329294Seric 	}
3303233Seric # endif DEBUG
3313233Seric 
3322898Seric 	/* create a pipe to shove the mail through */
3332898Seric 	if (pipe(pvect) < 0)
334294Seric 	{
335294Seric 		syserr("pipe");
336294Seric 		return (-1);
337294Seric 	}
3384214Seric 	DOFORK(XFORK);
339294Seric 	if (pid < 0)
340294Seric 	{
341294Seric 		syserr("Cannot fork");
3424082Seric 		(void) close(pvect[0]);
3434082Seric 		(void) close(pvect[1]);
344294Seric 		return (-1);
345294Seric 	}
346294Seric 	else if (pid == 0)
347294Seric 	{
348294Seric 		/* child -- set up input & exec mailer */
3491621Seric 		/* make diagnostic output be standard output */
3504215Seric 		(void) signal(SIGINT, SIG_DFL);
3514215Seric 		(void) signal(SIGHUP, SIG_DFL);
3524215Seric 		(void) signal(SIGTERM, SIG_DFL);
3534082Seric 		(void) close(2);
3544082Seric 		(void) dup(1);
3554082Seric 		(void) close(0);
3562898Seric 		if (dup(pvect[0]) < 0)
357294Seric 		{
3582898Seric 			syserr("Cannot dup to zero!");
3592898Seric 			_exit(EX_OSERR);
360294Seric 		}
3614082Seric 		(void) close(pvect[0]);
3624082Seric 		(void) close(pvect[1]);
3632968Seric 		if (!bitset(M_RESTR, m->m_flags))
3644215Seric 		{
3654082Seric 			(void) setuid(getuid());
3664215Seric 			(void) setgid(getgid());
3674215Seric 		}
3682774Seric # ifndef VFORK
3692774Seric 		/*
3702774Seric 		**  We have to be careful with vfork - we can't mung up the
3712774Seric 		**  memory but we don't want the mailer to inherit any extra
3722774Seric 		**  open files.  Chances are the mailer won't
3732774Seric 		**  care about an extra file, but then again you never know.
3742774Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
3752774Seric 		**  declared static so we can't.  But if we fclose(pwf), which
3762774Seric 		**  is what endpwent does, it closes it in the parent too and
3772774Seric 		**  the next getpwnam will be slower.  If you have a weird
3782774Seric 		**  mailer that chokes on the extra file you should do the
3792774Seric 		**  endpwent().
3802774Seric 		**
3812774Seric 		**  Similar comments apply to log.  However, openlog is
3822774Seric 		**  clever enough to set the FIOCLEX mode on the file,
3832774Seric 		**  so it will be closed automatically on the exec.
3842774Seric 		*/
3852774Seric 
3862774Seric 		endpwent();
387294Seric # ifdef LOG
3882089Seric 		closelog();
389294Seric # endif LOG
3902774Seric # endif VFORK
391294Seric 		execv(m->m_mailer, pvp);
392294Seric 		/* syserr fails because log is closed */
393294Seric 		/* syserr("Cannot exec %s", m->m_mailer); */
3944214Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
3954082Seric 		(void) fflush(stdout);
3961619Seric 		_exit(EX_UNAVAILABLE);
397294Seric 	}
398294Seric 
3992898Seric 	/* write out message to mailer */
4004082Seric 	(void) close(pvect[0]);
4014123Seric 	(void) signal(SIGPIPE, SIG_IGN);
4022898Seric 	mfile = fdopen(pvect[1], "w");
4032898Seric 	if (editfcn == NULL)
4042898Seric 		editfcn = putmessage;
4052898Seric 	(*editfcn)(mfile, m);
4064082Seric 	(void) fclose(mfile);
407294Seric 
408294Seric 	/*
409294Seric 	**  Wait for child to die and report status.
410294Seric 	**	We should never get fatal errors (e.g., segmentation
411294Seric 	**	violation), so we report those specially.  For other
412294Seric 	**	errors, we choose a status message (into statmsg),
413294Seric 	**	and if it represents an error, we print it.
414294Seric 	*/
415294Seric 
416294Seric 	while ((i = wait(&st)) > 0 && i != pid)
417294Seric 		continue;
418294Seric 	if (i < 0)
419294Seric 	{
420294Seric 		syserr("wait");
421294Seric 		return (-1);
422294Seric 	}
423294Seric 	if ((st & 0377) != 0)
424294Seric 	{
425294Seric 		syserr("%s: stat %o", pvp[0], st);
4261597Seric 		ExitStat = EX_UNAVAILABLE;
427294Seric 		return (-1);
428294Seric 	}
429294Seric 	i = (st >> 8) & 0377;
4302343Seric 	giveresponse(i, TRUE, m);
431294Seric 	return (i);
432294Seric }
433294Seric /*
434294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
435294Seric **
436294Seric **	Parameters:
437294Seric **		stat -- the status code from the mailer (high byte
438294Seric **			only; core dumps must have been taken care of
439294Seric **			already).
440294Seric **		force -- if set, force an error message output, even
441294Seric **			if the mailer seems to like to print its own
442294Seric **			messages.
443294Seric **		m -- the mailer descriptor for this mailer.
444294Seric **
445294Seric **	Returns:
4464082Seric **		none.
447294Seric **
448294Seric **	Side Effects:
4491518Seric **		Errors may be incremented.
450294Seric **		ExitStat may be set.
451294Seric **
452294Seric **	Called By:
453294Seric **		deliver
454294Seric */
455294Seric 
456294Seric giveresponse(stat, force, m)
457294Seric 	int stat;
458294Seric 	int force;
459294Seric 	register struct mailer *m;
460294Seric {
461294Seric 	register char *statmsg;
462294Seric 	extern char *SysExMsg[];
463294Seric 	register int i;
464294Seric 	extern int N_SysEx;
4651624Seric 	char buf[30];
466294Seric 
467294Seric 	i = stat - EX__BASE;
468294Seric 	if (i < 0 || i > N_SysEx)
469294Seric 		statmsg = NULL;
470294Seric 	else
471294Seric 		statmsg = SysExMsg[i];
472294Seric 	if (stat == 0)
4734065Seric 	{
4744194Seric 		if (bitset(M_LOCAL, m->m_flags))
4754161Seric 			statmsg = "delivered";
4764161Seric 		else
4774161Seric 			statmsg = "queued";
4784065Seric 		if (Verbose)
4794166Seric 			message(Arpa_Info, statmsg);
4804065Seric 	}
481294Seric 	else
482294Seric 	{
4831518Seric 		Errors++;
484294Seric 		if (statmsg == NULL && m->m_badstat != 0)
485294Seric 		{
486294Seric 			stat = m->m_badstat;
487294Seric 			i = stat - EX__BASE;
488294Seric # ifdef DEBUG
489294Seric 			if (i < 0 || i >= N_SysEx)
490294Seric 				syserr("Bad m_badstat %d", stat);
491294Seric 			else
492294Seric # endif DEBUG
493294Seric 			statmsg = SysExMsg[i];
494294Seric 		}
495294Seric 		if (statmsg == NULL)
496294Seric 			usrerr("unknown mailer response %d", stat);
4974065Seric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
498294Seric 			usrerr("%s", statmsg);
499294Seric 	}
500294Seric 
501294Seric 	/*
502294Seric 	**  Final cleanup.
503294Seric 	**	Log a record of the transaction.  Compute the new
504294Seric 	**	ExitStat -- if we already had an error, stick with
505294Seric 	**	that.
506294Seric 	*/
507294Seric 
5081624Seric 	if (statmsg == NULL)
5091624Seric 	{
5104082Seric 		(void) sprintf(buf, "error %d", stat);
5111624Seric 		statmsg = buf;
5121624Seric 	}
5131624Seric 
514294Seric # ifdef LOG
5152774Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
516294Seric # endif LOG
5171389Seric 	setstat(stat);
518294Seric }
519294Seric /*
5202898Seric **  PUTMESSAGE -- output a message to the final mailer.
521294Seric **
5222898Seric **	This routine takes care of recreating the header from the
5232898Seric **	in-core copy, etc.
524294Seric **
525294Seric **	Parameters:
5262898Seric **		fp -- file to output onto.
5272898Seric **		m -- a mailer descriptor.
528294Seric **
529294Seric **	Returns:
5302898Seric **		none.
531294Seric **
532294Seric **	Side Effects:
5332898Seric **		The message is written onto fp.
534294Seric */
535294Seric 
5362898Seric putmessage(fp, m)
5372898Seric 	FILE *fp;
5382898Seric 	struct mailer *m;
539294Seric {
5402898Seric 	char buf[BUFSIZ];
5412898Seric 	register int i;
5422898Seric 	HDR *h;
5431828Seric 	register char *p;
5442898Seric 	extern char *arpadate();
5452898Seric 	bool anyheader = FALSE;
5463044Seric 	extern char *capitalize();
547294Seric 
5483186Seric 	/* output "From" line unless supressed */
5493186Seric 	if (!bitset(M_NHDR, m->m_flags))
5504205Seric 	{
5514205Seric 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
5524205Seric 		fprintf(fp, "%s\n", buf);
5534205Seric 	}
5543186Seric 
5553385Seric 	/* output all header lines */
5562898Seric 	for (h = Header; h != NULL; h = h->h_link)
5571828Seric 	{
5583389Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
5594209Seric 		{
5604209Seric 			p = ")><(";		/* can't happen (I hope) */
5614209Seric 			goto checkfrom;
5624209Seric 		}
5633385Seric 		if (bitset(H_DEFAULT, h->h_flags))
5643385Seric 		{
5654082Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
5663385Seric 			p = buf;
5673385Seric 		}
5682898Seric 		else
5693385Seric 			p = h->h_value;
5703389Seric 		if (*p == '\0')
5713389Seric 			continue;
5723385Seric 		fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
5732898Seric 		h->h_flags |= H_USED;
5742898Seric 		anyheader = TRUE;
5754209Seric 
5764209Seric 		/* hack, hack -- output Original-From field if different */
5774209Seric 	checkfrom:
5784209Seric 		if (strcmp(h->h_field, "from") == 0)
5794209Seric 		{
5804209Seric 			extern char *hvalue();
5814209Seric 			char *ofrom = hvalue("original-from");
5824209Seric 
5834209Seric 			if (ofrom != NULL && strcmp(p, ofrom) != 0)
5844209Seric 				fprintf(fp, "Original-From: %s\n", ofrom);
5854209Seric 		}
5862898Seric 	}
5872898Seric 
5882898Seric 	if (anyheader)
5892898Seric 		fprintf(fp, "\n");
5902898Seric 
5912898Seric 	/* output the body of the message */
5924181Seric 	rewind(TempFile);
5934181Seric 	while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0)
5944082Seric 		(void) fwrite(buf, 1, i, fp);
5952898Seric 
5964123Seric 	if (ferror(fp) && errno != EPIPE)
597294Seric 	{
5982898Seric 		syserr("putmessage: write error");
599294Seric 		setstat(EX_IOERR);
600294Seric 	}
6014123Seric 	errno = 0;
602294Seric }
603294Seric /*
604294Seric **  MAILFILE -- Send a message to a file.
605294Seric **
606294Seric **	Parameters:
607294Seric **		filename -- the name of the file to send to.
608294Seric **
609294Seric **	Returns:
610294Seric **		The exit code associated with the operation.
611294Seric **
612294Seric **	Side Effects:
613294Seric **		none.
614294Seric */
615294Seric 
616294Seric mailfile(filename)
617294Seric 	char *filename;
618294Seric {
619294Seric 	register FILE *f;
6204214Seric 	register int pid;
6214214Seric 	register int i;
622294Seric 
6234214Seric 	/*
6244214Seric 	**  Fork so we can change permissions here.
6254214Seric 	**	Note that we MUST use fork, not vfork, because of
6264214Seric 	**	the complications of calling subroutines, etc.
6274214Seric 	*/
6284067Seric 
6294214Seric 	DOFORK(fork);
6304214Seric 
6314214Seric 	if (pid < 0)
6324214Seric 		return (EX_OSERR);
6334214Seric 	else if (pid == 0)
6344214Seric 	{
6354214Seric 		/* child -- actually write to file */
6364214Seric 		(void) setuid(getuid());
6374214Seric 		(void) setgid(getgid());
6384215Seric 		(void) signal(SIGINT, SIG_DFL);
6394215Seric 		(void) signal(SIGHUP, SIG_DFL);
6404215Seric 		(void) signal(SIGTERM, SIG_DFL);
6414214Seric 		f = fopen(filename, "a");
6424214Seric 		if (f == NULL)
6434214Seric 			exit(EX_CANTCREAT);
6444214Seric 
6454214Seric 		putmessage(f, Mailer[1]);
6464214Seric 		fputs("\n", f);
6474214Seric 		(void) fclose(f);
6484214Seric 		(void) fflush(stdout);
6494214Seric 		exit(EX_OK);
6504214Seric 	}
6514214Seric 	else
6524214Seric 	{
6534214Seric 		/* parent -- wait for exit status */
6544214Seric 		register int i;
6554214Seric 		auto int stat;
6564214Seric 
6574214Seric 		while ((i = wait(&stat)) != pid)
6584214Seric 		{
6594214Seric 			if (i < 0)
6604214Seric 			{
6614214Seric 				stat = EX_OSERR << 8;
6624214Seric 				break;
6634214Seric 			}
6644214Seric 		}
6654215Seric 		if ((stat & 0377) != 0)
6664215Seric 			stat = EX_UNAVAILABLE << 8;
6674214Seric 		return ((stat >> 8) & 0377);
6684214Seric 	}
669294Seric }
670