1294Seric # include <signal.h>
24123Seric # include <errno.h>
34327Seric # include <sys/types.h>
44327Seric # include <sys/stat.h>
53310Seric # include "sendmail.h"
6294Seric # ifdef LOG
72774Seric # include <syslog.h>
8294Seric # endif LOG
9294Seric 
10*4382Seric static char SccsId[] = "@(#)deliver.c	3.38	09/14/81";
11405Seric 
12294Seric /*
134315Seric **  DELIVER -- Deliver a message to a list of addresses.
14294Seric **
154315Seric **	This routine delivers to everyone on the same host as the
164315Seric **	user on the head of the list.  It is clever about mailers
174315Seric **	that don't handle multiple users.  It is NOT guaranteed
184315Seric **	that it will deliver to all these addresses however -- so
194315Seric **	deliver should be called once for each address on the
204315Seric **	list.
214315Seric **
22294Seric **	Parameters:
234315Seric **		to -- head of the address list to deliver to.
24294Seric **		editfcn -- if non-NULL, we want to call this function
25294Seric **			to output the letter (instead of just out-
26294Seric **			putting it raw).
27294Seric **
28294Seric **	Returns:
29294Seric **		zero -- successfully delivered.
30294Seric **		else -- some failure, see ExitStat for more info.
31294Seric **
32294Seric **	Side Effects:
33294Seric **		The standard input is passed off to someone.
34294Seric */
35294Seric 
36294Seric deliver(to, editfcn)
372968Seric 	ADDRESS *to;
38294Seric 	int (*editfcn)();
39294Seric {
40294Seric 	char *host;
41294Seric 	char *user;
42294Seric 	char **pvp;
433233Seric 	register char **mvp;
443233Seric 	register char *p;
453233Seric 	register struct mailer *m;
46294Seric 	register int i;
472898Seric 	extern putmessage();
482968Seric 	extern bool checkcompat();
493233Seric 	char *pv[MAXPV+1];
503233Seric 	char tobuf[MAXLINE];
513233Seric 	char buf[MAXNAME];
52294Seric 
534311Seric 	if (!ForceMail && bitset(QDONTSEND, to->q_flags))
543233Seric 		return (0);
55294Seric 
56294Seric # ifdef DEBUG
57294Seric 	if (Debug)
583233Seric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
593233Seric 			to->q_mailer, to->q_host, to->q_user);
60294Seric # endif DEBUG
61294Seric 
62294Seric 	/*
633233Seric 	**  Do initial argv setup.
643233Seric 	**	Insert the mailer name.  Notice that $x expansion is
653233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
663233Seric 	**	a picky -f flag, we insert it as appropriate.  This
673233Seric 	**	code does not check for 'pv' overflow; this places a
683233Seric 	**	manifest lower limit of 4 for MAXPV.
692968Seric 	*/
702968Seric 
713233Seric 	m = Mailer[to->q_mailer];
723233Seric 	host = to->q_host;
733233Seric 	define('g', m->m_from);		/* translated from address */
743233Seric 	define('h', host);		/* to host */
753233Seric 	Errors = 0;
763233Seric 	errno = 0;
773233Seric 	pvp = pv;
783233Seric 	*pvp++ = m->m_argv[0];
792968Seric 
803233Seric 	/* insert -f or -r flag as appropriate */
813233Seric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
823233Seric 	{
833233Seric 		if (bitset(M_FOPT, m->m_flags))
843233Seric 			*pvp++ = "-f";
853233Seric 		else
863233Seric 			*pvp++ = "-r";
874082Seric 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
883233Seric 		*pvp++ = newstr(buf);
893233Seric 	}
90294Seric 
91294Seric 	/*
923233Seric 	**  Append the other fixed parts of the argv.  These run
933233Seric 	**  up to the first entry containing "$u".  There can only
943233Seric 	**  be one of these, and there are only a few more slots
953233Seric 	**  in the pv after it.
96294Seric 	*/
97294Seric 
983233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
99294Seric 	{
1003233Seric 		while ((p = index(p, '$')) != NULL)
1013233Seric 			if (*++p == 'u')
1023233Seric 				break;
1033233Seric 		if (p != NULL)
1043233Seric 			break;
1053233Seric 
1063233Seric 		/* this entry is safe -- go ahead and process it */
1074082Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
1083233Seric 		*pvp++ = newstr(buf);
1093233Seric 		if (pvp >= &pv[MAXPV - 3])
1103233Seric 		{
1113233Seric 			syserr("Too many parameters to %s before $u", pv[0]);
1123233Seric 			return (-1);
1133233Seric 		}
114294Seric 	}
1153233Seric 	if (*mvp == NULL)
1163233Seric 		syserr("No $u in mailer argv for %s", pv[0]);
117294Seric 
118294Seric 	/*
1193233Seric 	**  At this point *mvp points to the argument with $u.  We
1203233Seric 	**  run through our address list and append all the addresses
1213233Seric 	**  we can.  If we run out of space, do not fret!  We can
1223233Seric 	**  always send another copy later.
123294Seric 	*/
124294Seric 
1253233Seric 	tobuf[0] = '\0';
1263233Seric 	To = tobuf;
1273233Seric 	for (; to != NULL; to = to->q_next)
128294Seric 	{
1293233Seric 		/* avoid sending multiple recipients to dumb mailers */
130*4382Seric 		if (tobuf[0] != '\0' && !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 
2084315Seric 		/*
2094315Seric 		**  Address is verified -- add this user to mailer
2104315Seric 		**  argv, and add it to the print list of recipients.
2114315Seric 		*/
2124315Seric 
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);
3474327Seric 	/* pid is set by DOFORK */
348294Seric 	if (pid < 0)
349294Seric 	{
350294Seric 		syserr("Cannot fork");
3514082Seric 		(void) close(pvect[0]);
3524082Seric 		(void) close(pvect[1]);
353294Seric 		return (-1);
354294Seric 	}
355294Seric 	else if (pid == 0)
356294Seric 	{
357294Seric 		/* child -- set up input & exec mailer */
3581621Seric 		/* make diagnostic output be standard output */
3594215Seric 		(void) signal(SIGINT, SIG_DFL);
3604215Seric 		(void) signal(SIGHUP, SIG_DFL);
3614215Seric 		(void) signal(SIGTERM, SIG_DFL);
3624082Seric 		(void) close(2);
3634082Seric 		(void) dup(1);
3644082Seric 		(void) close(0);
3652898Seric 		if (dup(pvect[0]) < 0)
366294Seric 		{
3672898Seric 			syserr("Cannot dup to zero!");
3682898Seric 			_exit(EX_OSERR);
369294Seric 		}
3704082Seric 		(void) close(pvect[0]);
3714082Seric 		(void) close(pvect[1]);
3722968Seric 		if (!bitset(M_RESTR, m->m_flags))
3734215Seric 		{
3744082Seric 			(void) setuid(getuid());
3754215Seric 			(void) setgid(getgid());
3764215Seric 		}
3772774Seric # ifndef VFORK
3782774Seric 		/*
3792774Seric 		**  We have to be careful with vfork - we can't mung up the
3802774Seric 		**  memory but we don't want the mailer to inherit any extra
3812774Seric 		**  open files.  Chances are the mailer won't
3822774Seric 		**  care about an extra file, but then again you never know.
3832774Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
3842774Seric 		**  declared static so we can't.  But if we fclose(pwf), which
3852774Seric 		**  is what endpwent does, it closes it in the parent too and
3862774Seric 		**  the next getpwnam will be slower.  If you have a weird
3872774Seric 		**  mailer that chokes on the extra file you should do the
3882774Seric 		**  endpwent().
3892774Seric 		**
3902774Seric 		**  Similar comments apply to log.  However, openlog is
3912774Seric 		**  clever enough to set the FIOCLEX mode on the file,
3922774Seric 		**  so it will be closed automatically on the exec.
3932774Seric 		*/
3942774Seric 
3952774Seric 		endpwent();
396294Seric # ifdef LOG
3972089Seric 		closelog();
398294Seric # endif LOG
3992774Seric # endif VFORK
400294Seric 		execv(m->m_mailer, pvp);
401294Seric 		/* syserr fails because log is closed */
402294Seric 		/* syserr("Cannot exec %s", m->m_mailer); */
4034214Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
4044082Seric 		(void) fflush(stdout);
4051619Seric 		_exit(EX_UNAVAILABLE);
406294Seric 	}
407294Seric 
4082898Seric 	/* write out message to mailer */
4094082Seric 	(void) close(pvect[0]);
4104123Seric 	(void) signal(SIGPIPE, SIG_IGN);
4112898Seric 	mfile = fdopen(pvect[1], "w");
4122898Seric 	if (editfcn == NULL)
4132898Seric 		editfcn = putmessage;
4142898Seric 	(*editfcn)(mfile, m);
4154082Seric 	(void) fclose(mfile);
416294Seric 
417294Seric 	/*
418294Seric 	**  Wait for child to die and report status.
419294Seric 	**	We should never get fatal errors (e.g., segmentation
420294Seric 	**	violation), so we report those specially.  For other
421294Seric 	**	errors, we choose a status message (into statmsg),
422294Seric 	**	and if it represents an error, we print it.
423294Seric 	*/
424294Seric 
425294Seric 	while ((i = wait(&st)) > 0 && i != pid)
426294Seric 		continue;
427294Seric 	if (i < 0)
428294Seric 	{
429294Seric 		syserr("wait");
430294Seric 		return (-1);
431294Seric 	}
432294Seric 	if ((st & 0377) != 0)
433294Seric 	{
434294Seric 		syserr("%s: stat %o", pvp[0], st);
4351597Seric 		ExitStat = EX_UNAVAILABLE;
436294Seric 		return (-1);
437294Seric 	}
438294Seric 	i = (st >> 8) & 0377;
4392343Seric 	giveresponse(i, TRUE, m);
440294Seric 	return (i);
441294Seric }
442294Seric /*
443294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
444294Seric **
445294Seric **	Parameters:
446294Seric **		stat -- the status code from the mailer (high byte
447294Seric **			only; core dumps must have been taken care of
448294Seric **			already).
449294Seric **		force -- if set, force an error message output, even
450294Seric **			if the mailer seems to like to print its own
451294Seric **			messages.
452294Seric **		m -- the mailer descriptor for this mailer.
453294Seric **
454294Seric **	Returns:
4554082Seric **		none.
456294Seric **
457294Seric **	Side Effects:
4581518Seric **		Errors may be incremented.
459294Seric **		ExitStat may be set.
460294Seric */
461294Seric 
462294Seric giveresponse(stat, force, m)
463294Seric 	int stat;
464294Seric 	int force;
465294Seric 	register struct mailer *m;
466294Seric {
467294Seric 	register char *statmsg;
468294Seric 	extern char *SysExMsg[];
469294Seric 	register int i;
470294Seric 	extern int N_SysEx;
4711624Seric 	char buf[30];
472294Seric 
4734315Seric 	/*
4744315Seric 	**  Compute status message from code.
4754315Seric 	*/
4764315Seric 
477294Seric 	i = stat - EX__BASE;
478294Seric 	if (i < 0 || i > N_SysEx)
479294Seric 		statmsg = NULL;
480294Seric 	else
481294Seric 		statmsg = SysExMsg[i];
482294Seric 	if (stat == 0)
4834065Seric 	{
4844194Seric 		if (bitset(M_LOCAL, m->m_flags))
4854161Seric 			statmsg = "delivered";
4864161Seric 		else
4874161Seric 			statmsg = "queued";
4884065Seric 		if (Verbose)
4894166Seric 			message(Arpa_Info, statmsg);
4904065Seric 	}
491294Seric 	else
492294Seric 	{
4931518Seric 		Errors++;
494294Seric 		if (statmsg == NULL && m->m_badstat != 0)
495294Seric 		{
496294Seric 			stat = m->m_badstat;
497294Seric 			i = stat - EX__BASE;
498294Seric # ifdef DEBUG
499294Seric 			if (i < 0 || i >= N_SysEx)
500294Seric 				syserr("Bad m_badstat %d", stat);
501294Seric 			else
502294Seric # endif DEBUG
503294Seric 			statmsg = SysExMsg[i];
504294Seric 		}
505294Seric 		if (statmsg == NULL)
506294Seric 			usrerr("unknown mailer response %d", stat);
5074065Seric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
508294Seric 			usrerr("%s", statmsg);
509294Seric 	}
510294Seric 
511294Seric 	/*
512294Seric 	**  Final cleanup.
513294Seric 	**	Log a record of the transaction.  Compute the new
514294Seric 	**	ExitStat -- if we already had an error, stick with
515294Seric 	**	that.
516294Seric 	*/
517294Seric 
5181624Seric 	if (statmsg == NULL)
5191624Seric 	{
5204082Seric 		(void) sprintf(buf, "error %d", stat);
5211624Seric 		statmsg = buf;
5221624Seric 	}
5231624Seric 
524294Seric # ifdef LOG
5252774Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
526294Seric # endif LOG
5271389Seric 	setstat(stat);
528294Seric }
529294Seric /*
5302898Seric **  PUTMESSAGE -- output a message to the final mailer.
531294Seric **
5322898Seric **	This routine takes care of recreating the header from the
5332898Seric **	in-core copy, etc.
534294Seric **
535294Seric **	Parameters:
5362898Seric **		fp -- file to output onto.
5372898Seric **		m -- a mailer descriptor.
538294Seric **
539294Seric **	Returns:
5402898Seric **		none.
541294Seric **
542294Seric **	Side Effects:
5432898Seric **		The message is written onto fp.
544294Seric */
545294Seric 
5462898Seric putmessage(fp, m)
5472898Seric 	FILE *fp;
5482898Seric 	struct mailer *m;
549294Seric {
5502898Seric 	char buf[BUFSIZ];
5512898Seric 	register int i;
5524315Seric 	register HDR *h;
5532898Seric 	extern char *arpadate();
5542898Seric 	bool anyheader = FALSE;
5553044Seric 	extern char *capitalize();
5564370Seric 	extern char *hvalue();
5574370Seric 	extern bool samefrom();
558294Seric 
5594315Seric 	/*
5604315Seric 	**  Output "From" line unless supressed
5614315Seric 	*/
5624315Seric 
5633186Seric 	if (!bitset(M_NHDR, m->m_flags))
5644205Seric 	{
5654205Seric 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
5664205Seric 		fprintf(fp, "%s\n", buf);
5674205Seric 	}
5683186Seric 
5694315Seric 	/*
5704315Seric 	**  Output all header lines
5714315Seric 	*/
5724315Seric 
5732898Seric 	for (h = Header; h != NULL; h = h->h_link)
5741828Seric 	{
5754315Seric 		register char *p;
5764370Seric 		char *origfrom = OrigFrom;
5774315Seric 
5783389Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
5794209Seric 		{
5804209Seric 			p = ")><(";		/* can't happen (I hope) */
5814209Seric 			goto checkfrom;
5824209Seric 		}
5834370Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
5844370Seric 		    strcmp(m->m_from, "$f") == 0)
5853385Seric 		{
5864370Seric 			p = origfrom;
5874370Seric 			origfrom = NULL;
5884370Seric 		}
5894370Seric 		else if (bitset(H_DEFAULT, h->h_flags))
5904370Seric 		{
5914082Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
5923385Seric 			p = buf;
5933385Seric 		}
5942898Seric 		else
5953385Seric 			p = h->h_value;
5963389Seric 		if (*p == '\0')
5973389Seric 			continue;
5983385Seric 		fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
5992898Seric 		h->h_flags |= H_USED;
6002898Seric 		anyheader = TRUE;
6014209Seric 
6024209Seric 		/* hack, hack -- output Original-From field if different */
6034209Seric 	checkfrom:
6044370Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
6054370Seric 		    !samefrom(p, origfrom) && hvalue("original-from") == NULL)
6064209Seric 		{
6074370Seric 			fprintf(fp, "Original-From: %s\n", origfrom);
6084370Seric 			anyheader = TRUE;
6094209Seric 		}
6102898Seric 	}
6112898Seric 	if (anyheader)
6122898Seric 		fprintf(fp, "\n");
6132898Seric 
6144315Seric 	/*
6154315Seric 	**  Output the body of the message
6164315Seric 	*/
6174315Seric 
6184181Seric 	rewind(TempFile);
6194181Seric 	while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0)
6204082Seric 		(void) fwrite(buf, 1, i, fp);
6212898Seric 
6224123Seric 	if (ferror(fp) && errno != EPIPE)
623294Seric 	{
6242898Seric 		syserr("putmessage: write error");
625294Seric 		setstat(EX_IOERR);
626294Seric 	}
6274123Seric 	errno = 0;
628294Seric }
629294Seric /*
6304370Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
6314370Seric **
6324370Seric **	Parameters:
6334370Seric **		ifrom -- internally generated form of from address.
6344370Seric **		efrom -- external form of from address.
6354370Seric **
6364370Seric **	Returns:
6374370Seric **		TRUE -- if they convey the same info.
6384370Seric **		FALSE -- if any information has been lost.
6394370Seric **
6404370Seric **	Side Effects:
6414370Seric **		none.
6424370Seric */
6434370Seric 
6444370Seric bool
6454370Seric samefrom(ifrom, efrom)
6464370Seric 	char *ifrom;
6474370Seric 	char *efrom;
6484370Seric {
6494370Seric 	return (strcmp(ifrom, efrom) == 0);
6504370Seric }
6514370Seric /*
652294Seric **  MAILFILE -- Send a message to a file.
653294Seric **
6544327Seric **	If the file has the setuid/setgid bits set, but NO execute
6554327Seric **	bits, sendmail will try to become the owner of that file
6564327Seric **	rather than the real user.  Obviously, this only works if
6574327Seric **	sendmail runs as root.
6584327Seric **
659294Seric **	Parameters:
660294Seric **		filename -- the name of the file to send to.
661294Seric **
662294Seric **	Returns:
663294Seric **		The exit code associated with the operation.
664294Seric **
665294Seric **	Side Effects:
666294Seric **		none.
667294Seric */
668294Seric 
669294Seric mailfile(filename)
670294Seric 	char *filename;
671294Seric {
672294Seric 	register FILE *f;
6734214Seric 	register int pid;
674294Seric 
6754214Seric 	/*
6764214Seric 	**  Fork so we can change permissions here.
6774214Seric 	**	Note that we MUST use fork, not vfork, because of
6784214Seric 	**	the complications of calling subroutines, etc.
6794214Seric 	*/
6804067Seric 
6814214Seric 	DOFORK(fork);
6824214Seric 
6834214Seric 	if (pid < 0)
6844214Seric 		return (EX_OSERR);
6854214Seric 	else if (pid == 0)
6864214Seric 	{
6874214Seric 		/* child -- actually write to file */
6884327Seric 		struct stat stb;
6894327Seric 
6904215Seric 		(void) signal(SIGINT, SIG_DFL);
6914215Seric 		(void) signal(SIGHUP, SIG_DFL);
6924215Seric 		(void) signal(SIGTERM, SIG_DFL);
6934327Seric 		umask(OldUmask);
6944327Seric 		if (stat(filename, &stb) < 0)
6954327Seric 			stb.st_mode = 0;
6964327Seric 		if (bitset(0111, stb.st_mode))
6974327Seric 			exit(EX_CANTCREAT);
6984327Seric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
6994327Seric 			(void) setgid(getgid());
7004327Seric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
7014327Seric 			(void) setuid(getuid());
7024214Seric 		f = fopen(filename, "a");
7034214Seric 		if (f == NULL)
7044214Seric 			exit(EX_CANTCREAT);
7054214Seric 
7064214Seric 		putmessage(f, Mailer[1]);
7074214Seric 		fputs("\n", f);
7084214Seric 		(void) fclose(f);
7094214Seric 		(void) fflush(stdout);
7104214Seric 		exit(EX_OK);
7114315Seric 		/*NOTREACHED*/
7124214Seric 	}
7134214Seric 	else
7144214Seric 	{
7154214Seric 		/* parent -- wait for exit status */
7164214Seric 		register int i;
7174214Seric 		auto int stat;
7184214Seric 
7194214Seric 		while ((i = wait(&stat)) != pid)
7204214Seric 		{
7214214Seric 			if (i < 0)
7224214Seric 			{
7234214Seric 				stat = EX_OSERR << 8;
7244214Seric 				break;
7254214Seric 			}
7264214Seric 		}
7274215Seric 		if ((stat & 0377) != 0)
7284215Seric 			stat = EX_UNAVAILABLE << 8;
7294214Seric 		return ((stat >> 8) & 0377);
7304214Seric 	}
731294Seric }
732