1294Seric # include <signal.h>
24123Seric # include <errno.h>
34621Seric # include "sendmail.h"
44327Seric # include <sys/stat.h>
5294Seric # ifdef LOG
62774Seric # include <syslog.h>
7294Seric # endif LOG
8294Seric 
9*4637Seric static char SccsId[] = "@(#)deliver.c	3.51	10/27/81";
10405Seric 
11294Seric /*
124315Seric **  DELIVER -- Deliver a message to a list of addresses.
13294Seric **
144315Seric **	This routine delivers to everyone on the same host as the
154315Seric **	user on the head of the list.  It is clever about mailers
164315Seric **	that don't handle multiple users.  It is NOT guaranteed
174315Seric **	that it will deliver to all these addresses however -- so
184315Seric **	deliver should be called once for each address on the
194315Seric **	list.
204315Seric **
21294Seric **	Parameters:
224621Seric **		firstto -- head of the address list to deliver to.
23294Seric **		editfcn -- if non-NULL, we want to call this function
24294Seric **			to output the letter (instead of just out-
25294Seric **			putting it raw).
26294Seric **
27294Seric **	Returns:
28294Seric **		zero -- successfully delivered.
29294Seric **		else -- some failure, see ExitStat for more info.
30294Seric **
31294Seric **	Side Effects:
32294Seric **		The standard input is passed off to someone.
33294Seric */
34294Seric 
354621Seric deliver(firstto, editfcn)
364621Seric 	ADDRESS *firstto;
37294Seric 	int (*editfcn)();
38294Seric {
394452Seric 	char *host;			/* host being sent to */
404452Seric 	char *user;			/* user being sent to */
41294Seric 	char **pvp;
423233Seric 	register char **mvp;
433233Seric 	register char *p;
444452Seric 	register struct mailer *m;	/* mailer for this recipient */
45294Seric 	register int i;
462898Seric 	extern putmessage();
472968Seric 	extern bool checkcompat();
483233Seric 	char *pv[MAXPV+1];
494452Seric 	char tobuf[MAXLINE];		/* text line of to people */
503233Seric 	char buf[MAXNAME];
514397Seric 	ADDRESS *ctladdr;
524397Seric 	extern ADDRESS *getctladdr();
534452Seric 	char tfrombuf[MAXNAME];		/* translated from person */
544452Seric 	extern char **prescan();
554621Seric 	register ADDRESS *to = firstto;
56294Seric 
574488Seric 	errno = 0;
584311Seric 	if (!ForceMail && bitset(QDONTSEND, to->q_flags))
593233Seric 		return (0);
60294Seric 
61294Seric # ifdef DEBUG
62294Seric 	if (Debug)
633233Seric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
644596Seric 			to->q_mailer->m_mno, to->q_host, to->q_user);
65294Seric # endif DEBUG
66294Seric 
67294Seric 	/*
683233Seric 	**  Do initial argv setup.
693233Seric 	**	Insert the mailer name.  Notice that $x expansion is
703233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
713233Seric 	**	a picky -f flag, we insert it as appropriate.  This
723233Seric 	**	code does not check for 'pv' overflow; this places a
733233Seric 	**	manifest lower limit of 4 for MAXPV.
742968Seric 	*/
752968Seric 
764596Seric 	m = to->q_mailer;
773233Seric 	host = to->q_host;
784452Seric 
794452Seric 	/* rewrite from address, using rewriting rules */
804452Seric 	(void) expand(m->m_from, buf, &buf[sizeof buf - 1]);
814452Seric 	mvp = prescan(buf, '\0');
824452Seric 	if (mvp == NULL)
834452Seric 	{
844452Seric 		syserr("bad mailer from translate \"%s\"", buf);
854452Seric 		return (EX_SOFTWARE);
864452Seric 	}
874452Seric 	rewrite(mvp, 2);
884452Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
894452Seric 
904452Seric 	define('g', tfrombuf);		/* translated sender address */
913233Seric 	define('h', host);		/* to host */
923233Seric 	Errors = 0;
933233Seric 	pvp = pv;
943233Seric 	*pvp++ = m->m_argv[0];
952968Seric 
963233Seric 	/* insert -f or -r flag as appropriate */
973233Seric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
983233Seric 	{
993233Seric 		if (bitset(M_FOPT, m->m_flags))
1003233Seric 			*pvp++ = "-f";
1013233Seric 		else
1023233Seric 			*pvp++ = "-r";
1034082Seric 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
1043233Seric 		*pvp++ = newstr(buf);
1053233Seric 	}
106294Seric 
107294Seric 	/*
1083233Seric 	**  Append the other fixed parts of the argv.  These run
1093233Seric 	**  up to the first entry containing "$u".  There can only
1103233Seric 	**  be one of these, and there are only a few more slots
1113233Seric 	**  in the pv after it.
112294Seric 	*/
113294Seric 
1143233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
115294Seric 	{
1163233Seric 		while ((p = index(p, '$')) != NULL)
1173233Seric 			if (*++p == 'u')
1183233Seric 				break;
1193233Seric 		if (p != NULL)
1203233Seric 			break;
1213233Seric 
1223233Seric 		/* this entry is safe -- go ahead and process it */
1234082Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
1243233Seric 		*pvp++ = newstr(buf);
1253233Seric 		if (pvp >= &pv[MAXPV - 3])
1263233Seric 		{
1273233Seric 			syserr("Too many parameters to %s before $u", pv[0]);
1283233Seric 			return (-1);
1293233Seric 		}
130294Seric 	}
1313233Seric 	if (*mvp == NULL)
1323233Seric 		syserr("No $u in mailer argv for %s", pv[0]);
133294Seric 
134294Seric 	/*
1353233Seric 	**  At this point *mvp points to the argument with $u.  We
1363233Seric 	**  run through our address list and append all the addresses
1373233Seric 	**  we can.  If we run out of space, do not fret!  We can
1383233Seric 	**  always send another copy later.
139294Seric 	*/
140294Seric 
1413233Seric 	tobuf[0] = '\0';
1423233Seric 	To = tobuf;
1434397Seric 	ctladdr = NULL;
1443233Seric 	for (; to != NULL; to = to->q_next)
145294Seric 	{
1463233Seric 		/* avoid sending multiple recipients to dumb mailers */
1474382Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1483233Seric 			break;
1493233Seric 
1503233Seric 		/* if already sent or not for this host, don't send */
1514311Seric 		if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) ||
1524311Seric 		    strcmp(to->q_host, host) != 0)
1533233Seric 			continue;
1544397Seric 
1554397Seric 		/* compute effective uid/gid when sending */
1564596Seric 		if (to->q_mailer == ProgMailer)
1574397Seric 			ctladdr = getctladdr(to);
1584397Seric 
1593233Seric 		user = to->q_user;
1603233Seric 		To = to->q_paddr;
1613233Seric 		to->q_flags |= QDONTSEND;
1623233Seric # ifdef DEBUG
1633233Seric 		if (Debug)
1643233Seric 			printf("   send to `%s'\n", user);
1653233Seric # endif DEBUG
1663233Seric 
1673233Seric 		/*
1683233Seric 		**  Check to see that these people are allowed to
1693233Seric 		**  talk to each other.
1703233Seric 		*/
1713233Seric 
1723233Seric 		if (!checkcompat(to))
173294Seric 		{
1743233Seric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
1753233Seric 			continue;
176294Seric 		}
1773233Seric 
1783233Seric 		/*
1794099Seric 		**  Strip quote bits from names if the mailer is dumb
1804099Seric 		**	about them.
1813233Seric 		*/
1823233Seric 
1833233Seric 		if (bitset(M_STRIPQ, m->m_flags))
184294Seric 		{
1854099Seric 			stripquotes(user, TRUE);
1864099Seric 			stripquotes(host, TRUE);
1873233Seric 		}
1884099Seric 		else
1894099Seric 		{
1904099Seric 			stripquotes(user, FALSE);
1914099Seric 			stripquotes(host, FALSE);
1924099Seric 		}
1933233Seric 
1943233Seric 		/*
1954161Seric 		**  If an error message has already been given, don't
1964161Seric 		**	bother to send to this address.
1974161Seric 		**
1984161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
1994161Seric 		**	>> NOTE >> cannot do any further aliasing; that
2004161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
2014161Seric 		*/
2024161Seric 
2034161Seric 		if (bitset(QBADADDR, to->q_flags))
2044161Seric 			continue;
2054161Seric 
2064283Seric 		/* save statistics.... */
2074596Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2084596Seric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(MsgSize);
2094283Seric 
2104161Seric 		/*
2113233Seric 		**  See if this user name is "special".
2123233Seric 		**	If the user name has a slash in it, assume that this
2133233Seric 		**	is a file -- send it off without further ado.
2143233Seric 		**	Note that this means that editfcn's will not
2153233Seric 		**	be applied to the message.  Also note that
2163233Seric 		**	this type of addresses is not processed along
2173233Seric 		**	with the others, so we fudge on the To person.
2183233Seric 		*/
2193233Seric 
2204596Seric 		if (m == LocalMailer)
2213233Seric 		{
222294Seric 			if (index(user, '/') != NULL)
223294Seric 			{
2244397Seric 				i = mailfile(user, getctladdr(to));
225294Seric 				giveresponse(i, TRUE, m);
2263233Seric 				continue;
227294Seric 			}
228294Seric 		}
2293233Seric 
2304315Seric 		/*
2314315Seric 		**  Address is verified -- add this user to mailer
2324315Seric 		**  argv, and add it to the print list of recipients.
2334315Seric 		*/
2344315Seric 
2353233Seric 		/* create list of users for error messages */
2363233Seric 		if (tobuf[0] != '\0')
2374082Seric 			(void) strcat(tobuf, ",");
2384082Seric 		(void) strcat(tobuf, to->q_paddr);
2393233Seric 		define('u', user);		/* to user */
2404078Seric 		define('z', to->q_home);	/* user's home */
2413233Seric 
2423233Seric 		/* expand out this user */
2434305Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
2443233Seric 		*pvp++ = newstr(buf);
2453233Seric 		if (pvp >= &pv[MAXPV - 2])
2463233Seric 		{
2473233Seric 			/* allow some space for trailing parms */
2483233Seric 			break;
2493233Seric 		}
250294Seric 	}
251294Seric 
2524067Seric 	/* see if any addresses still exist */
2534067Seric 	if (tobuf[0] == '\0')
2544067Seric 		return (0);
2554067Seric 
2563233Seric 	/* print out messages as full list */
2573233Seric 	To = tobuf;
2583233Seric 
259294Seric 	/*
2603233Seric 	**  Fill out any parameters after the $u parameter.
261294Seric 	*/
262294Seric 
2633233Seric 	while (*++mvp != NULL)
264294Seric 	{
2654082Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
2663233Seric 		*pvp++ = newstr(buf);
2673233Seric 		if (pvp >= &pv[MAXPV])
2683233Seric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
269294Seric 	}
2703233Seric 	*pvp++ = NULL;
271294Seric 
272294Seric 	/*
273294Seric 	**  Call the mailer.
2742898Seric 	**	The argument vector gets built, pipes
275294Seric 	**	are created as necessary, and we fork & exec as
2762898Seric 	**	appropriate.
277294Seric 	*/
278294Seric 
2793233Seric 	if (editfcn == NULL)
2803233Seric 		editfcn = putmessage;
2814397Seric 	if (ctladdr == NULL)
2824397Seric 		ctladdr = &From;
2834397Seric 	i = sendoff(m, pv, editfcn, ctladdr);
2843233Seric 
2854621Seric 	/*
2864621Seric 	**  If we got a temporary failure, arrange to queue the
2874621Seric 	**  addressees.
2884621Seric 	*/
2894621Seric 
2904621Seric 	if (i == EX_TEMPFAIL)
2914621Seric 	{
2924621Seric 		QueueUp = TRUE;
2934621Seric 		for (to = firstto; to != NULL; to = to->q_next)
2944621Seric 		{
2954621Seric 			if (bitset(QBADADDR, to->q_flags))
2964621Seric 				continue;
2974621Seric 			to->q_flags |= QQUEUEUP;
2984621Seric 		}
2994621Seric 	}
3004621Seric 
3014488Seric 	errno = 0;
3023233Seric 	return (i);
3033233Seric }
3043233Seric /*
3054214Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
3064214Seric **
3074214Seric **	This MUST be a macro, since after a vfork we are running
3084214Seric **	two processes on the same stack!!!
3094214Seric **
3104214Seric **	Parameters:
3114214Seric **		none.
3124214Seric **
3134214Seric **	Returns:
3144214Seric **		From a macro???  You've got to be kidding!
3154214Seric **
3164214Seric **	Side Effects:
3174214Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
3184214Seric **			pid of child in parent, zero in child.
3194214Seric **			-1 on unrecoverable error.
3204214Seric **
3214214Seric **	Notes:
3224214Seric **		I'm awfully sorry this looks so awful.  That's
3234214Seric **		vfork for you.....
3244214Seric */
3254214Seric 
3264214Seric # define NFORKTRIES	5
3274214Seric # ifdef VFORK
3284214Seric # define XFORK	vfork
3294214Seric # else VFORK
3304214Seric # define XFORK	fork
3314214Seric # endif VFORK
3324214Seric 
3334214Seric # define DOFORK(fORKfN) \
3344214Seric {\
3354214Seric 	register int i;\
3364214Seric \
3374214Seric 	for (i = NFORKTRIES; i-- > 0; )\
3384214Seric 	{\
3394214Seric 		pid = fORKfN();\
3404214Seric 		if (pid >= 0)\
3414214Seric 			break;\
3424214Seric 		sleep((unsigned) NFORKTRIES - i);\
3434214Seric 	}\
3444214Seric }
3454214Seric /*
346*4637Seric **  DOFORK -- simple fork interface to DOFORK.
347*4637Seric **
348*4637Seric **	Parameters:
349*4637Seric **		none.
350*4637Seric **
351*4637Seric **	Returns:
352*4637Seric **		pid of child in parent.
353*4637Seric **		zero in child.
354*4637Seric **		-1 on error.
355*4637Seric **
356*4637Seric **	Side Effects:
357*4637Seric **		returns twice, once in parent and once in child.
358*4637Seric */
359*4637Seric 
360*4637Seric dofork()
361*4637Seric {
362*4637Seric 	register int pid;
363*4637Seric 
364*4637Seric 	DOFORK(fork);
365*4637Seric 	return (pid);
366*4637Seric }
367*4637Seric /*
3683233Seric **  SENDOFF -- send off call to mailer & collect response.
3693233Seric **
3703233Seric **	Parameters:
3713233Seric **		m -- mailer descriptor.
3723233Seric **		pvp -- parameter vector to send to it.
3733233Seric **		editfcn -- function to pipe it through.
3744397Seric **		ctladdr -- an address pointer controlling the
3754397Seric **			user/groupid etc. of the mailer.
3763233Seric **
3773233Seric **	Returns:
3783233Seric **		exit status of mailer.
3793233Seric **
3803233Seric **	Side Effects:
3813233Seric **		none.
3823233Seric */
3833233Seric 
3844397Seric sendoff(m, pvp, editfcn, ctladdr)
3853233Seric 	struct mailer *m;
3863233Seric 	char **pvp;
3873233Seric 	int (*editfcn)();
3884397Seric 	ADDRESS *ctladdr;
3893233Seric {
3903233Seric 	auto int st;
3913233Seric 	register int i;
3923233Seric 	int pid;
3933233Seric 	int pvect[2];
3943233Seric 	FILE *mfile;
3953233Seric 	extern putmessage();
3963233Seric 	extern FILE *fdopen();
3973233Seric 
3983233Seric # ifdef DEBUG
3993233Seric 	if (Debug)
400294Seric 	{
4013233Seric 		printf("Sendoff:\n");
4023233Seric 		printav(pvp);
403294Seric 	}
4043233Seric # endif DEBUG
4054488Seric 	errno = 0;
4063233Seric 
4072898Seric 	/* create a pipe to shove the mail through */
4082898Seric 	if (pipe(pvect) < 0)
409294Seric 	{
410294Seric 		syserr("pipe");
411294Seric 		return (-1);
412294Seric 	}
4134214Seric 	DOFORK(XFORK);
4144327Seric 	/* pid is set by DOFORK */
415294Seric 	if (pid < 0)
416294Seric 	{
417294Seric 		syserr("Cannot fork");
4184082Seric 		(void) close(pvect[0]);
4194082Seric 		(void) close(pvect[1]);
420294Seric 		return (-1);
421294Seric 	}
422294Seric 	else if (pid == 0)
423294Seric 	{
424294Seric 		/* child -- set up input & exec mailer */
4251621Seric 		/* make diagnostic output be standard output */
4264477Seric 		(void) signal(SIGINT, SIG_IGN);
4274477Seric 		(void) signal(SIGHUP, SIG_IGN);
4284215Seric 		(void) signal(SIGTERM, SIG_DFL);
4294082Seric 		(void) close(2);
4304082Seric 		(void) dup(1);
4314082Seric 		(void) close(0);
4322898Seric 		if (dup(pvect[0]) < 0)
433294Seric 		{
4342898Seric 			syserr("Cannot dup to zero!");
4352898Seric 			_exit(EX_OSERR);
436294Seric 		}
4374082Seric 		(void) close(pvect[0]);
4384082Seric 		(void) close(pvect[1]);
4392968Seric 		if (!bitset(M_RESTR, m->m_flags))
4404215Seric 		{
4414417Seric 			if (ctladdr->q_uid == 0)
4424417Seric 			{
4434417Seric 				extern int DefUid, DefGid;
4444415Seric 
4454417Seric 				(void) setgid(DefGid);
4464417Seric 				(void) setuid(DefUid);
4474417Seric 			}
4484417Seric 			else
4494415Seric 			{
4504417Seric 				(void) setgid(ctladdr->q_gid);
4514417Seric 				(void) setuid(ctladdr->q_uid);
4524415Seric 			}
4534215Seric 		}
4542774Seric # ifndef VFORK
4552774Seric 		/*
4562774Seric 		**  We have to be careful with vfork - we can't mung up the
4572774Seric 		**  memory but we don't want the mailer to inherit any extra
4582774Seric 		**  open files.  Chances are the mailer won't
4592774Seric 		**  care about an extra file, but then again you never know.
4602774Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
4612774Seric 		**  declared static so we can't.  But if we fclose(pwf), which
4622774Seric 		**  is what endpwent does, it closes it in the parent too and
4632774Seric 		**  the next getpwnam will be slower.  If you have a weird
4642774Seric 		**  mailer that chokes on the extra file you should do the
4652774Seric 		**  endpwent().
4662774Seric 		**
4672774Seric 		**  Similar comments apply to log.  However, openlog is
4682774Seric 		**  clever enough to set the FIOCLEX mode on the file,
4692774Seric 		**  so it will be closed automatically on the exec.
4702774Seric 		*/
4712774Seric 
4722774Seric 		endpwent();
473294Seric # ifdef LOG
4742089Seric 		closelog();
475294Seric # endif LOG
4762774Seric # endif VFORK
477294Seric 		execv(m->m_mailer, pvp);
478294Seric 		/* syserr fails because log is closed */
479294Seric 		/* syserr("Cannot exec %s", m->m_mailer); */
4804214Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
4814082Seric 		(void) fflush(stdout);
4821619Seric 		_exit(EX_UNAVAILABLE);
483294Seric 	}
484294Seric 
4852898Seric 	/* write out message to mailer */
4864082Seric 	(void) close(pvect[0]);
4874123Seric 	(void) signal(SIGPIPE, SIG_IGN);
4882898Seric 	mfile = fdopen(pvect[1], "w");
4892898Seric 	if (editfcn == NULL)
4902898Seric 		editfcn = putmessage;
4912898Seric 	(*editfcn)(mfile, m);
4924082Seric 	(void) fclose(mfile);
493294Seric 
494294Seric 	/*
495294Seric 	**  Wait for child to die and report status.
496294Seric 	**	We should never get fatal errors (e.g., segmentation
497294Seric 	**	violation), so we report those specially.  For other
498294Seric 	**	errors, we choose a status message (into statmsg),
499294Seric 	**	and if it represents an error, we print it.
500294Seric 	*/
501294Seric 
502294Seric 	while ((i = wait(&st)) > 0 && i != pid)
503294Seric 		continue;
504294Seric 	if (i < 0)
505294Seric 	{
506294Seric 		syserr("wait");
507294Seric 		return (-1);
508294Seric 	}
509294Seric 	if ((st & 0377) != 0)
510294Seric 	{
511294Seric 		syserr("%s: stat %o", pvp[0], st);
5121597Seric 		ExitStat = EX_UNAVAILABLE;
513294Seric 		return (-1);
514294Seric 	}
515294Seric 	i = (st >> 8) & 0377;
5162343Seric 	giveresponse(i, TRUE, m);
517294Seric 	return (i);
518294Seric }
519294Seric /*
520294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
521294Seric **
522294Seric **	Parameters:
523294Seric **		stat -- the status code from the mailer (high byte
524294Seric **			only; core dumps must have been taken care of
525294Seric **			already).
526294Seric **		force -- if set, force an error message output, even
527294Seric **			if the mailer seems to like to print its own
528294Seric **			messages.
529294Seric **		m -- the mailer descriptor for this mailer.
530294Seric **
531294Seric **	Returns:
5324082Seric **		none.
533294Seric **
534294Seric **	Side Effects:
5351518Seric **		Errors may be incremented.
536294Seric **		ExitStat may be set.
537294Seric */
538294Seric 
539294Seric giveresponse(stat, force, m)
540294Seric 	int stat;
541294Seric 	int force;
542294Seric 	register struct mailer *m;
543294Seric {
544294Seric 	register char *statmsg;
545294Seric 	extern char *SysExMsg[];
546294Seric 	register int i;
547294Seric 	extern int N_SysEx;
5481624Seric 	char buf[30];
549294Seric 
5504315Seric 	/*
5514315Seric 	**  Compute status message from code.
5524315Seric 	*/
5534315Seric 
554294Seric 	i = stat - EX__BASE;
555294Seric 	if (i < 0 || i > N_SysEx)
556294Seric 		statmsg = NULL;
557294Seric 	else
558294Seric 		statmsg = SysExMsg[i];
559294Seric 	if (stat == 0)
5604065Seric 	{
5614194Seric 		if (bitset(M_LOCAL, m->m_flags))
5624161Seric 			statmsg = "delivered";
5634161Seric 		else
5644161Seric 			statmsg = "queued";
5654065Seric 		if (Verbose)
5664166Seric 			message(Arpa_Info, statmsg);
5674065Seric 	}
5684621Seric 	else if (stat == EX_TEMPFAIL)
5694621Seric 	{
5704621Seric 		if (Verbose)
5714621Seric 			message(Arpa_Info, "transmission deferred");
5724621Seric 	}
573294Seric 	else
574294Seric 	{
5751518Seric 		Errors++;
576294Seric 		if (statmsg == NULL && m->m_badstat != 0)
577294Seric 		{
578294Seric 			stat = m->m_badstat;
579294Seric 			i = stat - EX__BASE;
580294Seric # ifdef DEBUG
581294Seric 			if (i < 0 || i >= N_SysEx)
582294Seric 				syserr("Bad m_badstat %d", stat);
583294Seric 			else
584294Seric # endif DEBUG
585294Seric 			statmsg = SysExMsg[i];
586294Seric 		}
587294Seric 		if (statmsg == NULL)
588294Seric 			usrerr("unknown mailer response %d", stat);
5894065Seric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
590294Seric 			usrerr("%s", statmsg);
591294Seric 	}
592294Seric 
593294Seric 	/*
594294Seric 	**  Final cleanup.
595294Seric 	**	Log a record of the transaction.  Compute the new
596294Seric 	**	ExitStat -- if we already had an error, stick with
597294Seric 	**	that.
598294Seric 	*/
599294Seric 
6001624Seric 	if (statmsg == NULL)
6011624Seric 	{
6024082Seric 		(void) sprintf(buf, "error %d", stat);
6031624Seric 		statmsg = buf;
6041624Seric 	}
6051624Seric 
606294Seric # ifdef LOG
6072774Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
608294Seric # endif LOG
6094621Seric 	if (stat != EX_TEMPFAIL)
6104621Seric 		setstat(stat);
611294Seric }
612294Seric /*
6132898Seric **  PUTMESSAGE -- output a message to the final mailer.
614294Seric **
6152898Seric **	This routine takes care of recreating the header from the
6162898Seric **	in-core copy, etc.
617294Seric **
618294Seric **	Parameters:
6192898Seric **		fp -- file to output onto.
6202898Seric **		m -- a mailer descriptor.
621294Seric **
622294Seric **	Returns:
6232898Seric **		none.
624294Seric **
625294Seric **	Side Effects:
6262898Seric **		The message is written onto fp.
627294Seric */
628294Seric 
6292898Seric putmessage(fp, m)
6302898Seric 	FILE *fp;
6312898Seric 	struct mailer *m;
632294Seric {
6332898Seric 	char buf[BUFSIZ];
6342898Seric 	register int i;
6354315Seric 	register HDR *h;
6362898Seric 	extern char *arpadate();
6372898Seric 	bool anyheader = FALSE;
6383044Seric 	extern char *capitalize();
6394370Seric 	extern char *hvalue();
6404370Seric 	extern bool samefrom();
6414447Seric 	char *of_line;
642294Seric 
6434315Seric 	/*
6444315Seric 	**  Output "From" line unless supressed
6454315Seric 	*/
6464315Seric 
6473186Seric 	if (!bitset(M_NHDR, m->m_flags))
6484205Seric 	{
6494205Seric 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
6504205Seric 		fprintf(fp, "%s\n", buf);
6514205Seric 	}
6523186Seric 
6534315Seric 	/*
6544315Seric 	**  Output all header lines
6554315Seric 	*/
6564315Seric 
6574447Seric 	of_line = hvalue("original-from");
6582898Seric 	for (h = Header; h != NULL; h = h->h_link)
6591828Seric 	{
6604315Seric 		register char *p;
6614370Seric 		char *origfrom = OrigFrom;
6624447Seric 		bool nooutput;
6634315Seric 
6644447Seric 		nooutput = FALSE;
6653389Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
6664209Seric 		{
6674209Seric 			p = ")><(";		/* can't happen (I hope) */
6684447Seric 			nooutput = TRUE;
6694209Seric 		}
6704447Seric 
6714447Seric 		/* use From: line from message if generated is the same */
6724370Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
6734447Seric 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
6743385Seric 		{
6754370Seric 			p = origfrom;
6764370Seric 			origfrom = NULL;
6774370Seric 		}
6784370Seric 		else if (bitset(H_DEFAULT, h->h_flags))
6794370Seric 		{
6804082Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
6813385Seric 			p = buf;
6823385Seric 		}
6832898Seric 		else
6843385Seric 			p = h->h_value;
6854447Seric 		if (p == NULL || *p == '\0')
6863389Seric 			continue;
6874209Seric 
6884209Seric 		/* hack, hack -- output Original-From field if different */
6894447Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
6904209Seric 		{
6914447Seric 			/* output new Original-From line if needed */
6924447Seric 			if (of_line == NULL && !samefrom(p, origfrom))
6934447Seric 			{
6944447Seric 				fprintf(fp, "Original-From: %s\n", origfrom);
6954447Seric 				anyheader = TRUE;
6964447Seric 			}
6974447Seric 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
6984447Seric 			{
6994447Seric 				/* delete Original-From: line if redundant */
7004447Seric 				p = of_line;
7014447Seric 				of_line = NULL;
7024447Seric 			}
7034447Seric 		}
7044447Seric 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
7054447Seric 			nooutput = TRUE;
7064447Seric 
7074447Seric 		/* finally, output the header line */
7084447Seric 		if (!nooutput)
7094447Seric 		{
7104447Seric 			fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
7114447Seric 			h->h_flags |= H_USED;
7124370Seric 			anyheader = TRUE;
7134209Seric 		}
7142898Seric 	}
7152898Seric 	if (anyheader)
7162898Seric 		fprintf(fp, "\n");
7172898Seric 
7184315Seric 	/*
7194315Seric 	**  Output the body of the message
7204315Seric 	*/
7214315Seric 
7224452Seric 	if (TempFile != NULL)
7234452Seric 	{
7244452Seric 		rewind(TempFile);
7254452Seric 		while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0)
7264452Seric 			(void) fwrite(buf, 1, i, fp);
7272898Seric 
7284452Seric 		if (ferror(TempFile))
7294452Seric 		{
7304452Seric 			syserr("putmessage: read error");
7314452Seric 			setstat(EX_IOERR);
7324452Seric 		}
7334452Seric 	}
7344452Seric 
7354621Seric 	(void) fflush(fp);
7364123Seric 	if (ferror(fp) && errno != EPIPE)
737294Seric 	{
7382898Seric 		syserr("putmessage: write error");
739294Seric 		setstat(EX_IOERR);
740294Seric 	}
7414123Seric 	errno = 0;
742294Seric }
743294Seric /*
7444370Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
7454370Seric **
7464370Seric **	Parameters:
7474370Seric **		ifrom -- internally generated form of from address.
7484370Seric **		efrom -- external form of from address.
7494370Seric **
7504370Seric **	Returns:
7514370Seric **		TRUE -- if they convey the same info.
7524370Seric **		FALSE -- if any information has been lost.
7534370Seric **
7544370Seric **	Side Effects:
7554370Seric **		none.
7564370Seric */
7574370Seric 
7584370Seric bool
7594370Seric samefrom(ifrom, efrom)
7604370Seric 	char *ifrom;
7614370Seric 	char *efrom;
7624370Seric {
7634447Seric 	register char *p;
7644447Seric 	char buf[MAXNAME + 4];
7654447Seric 
7664447Seric # ifdef DEBUG
7674447Seric 	if (Debug > 7)
7684447Seric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
7694447Seric # endif DEBUG
7704447Seric 	if (strcmp(ifrom, efrom) == 0)
7714447Seric 		goto success;
7724447Seric 	p = index(ifrom, '@');
7734447Seric 	if (p == NULL)
7744447Seric 		goto failure;
7754447Seric 	*p = '\0';
7764447Seric 	strcpy(buf, ifrom);
7774447Seric 	strcat(buf, " at ");
7784447Seric 	*p++ = '@';
7794447Seric 	strcat(buf, p);
7804447Seric 	if (strcmp(buf, efrom) == 0)
7814447Seric 		goto success;
7824447Seric 
7834447Seric   failure:
7844447Seric # ifdef DEBUG
7854447Seric 	if (Debug > 7)
7864447Seric 		printf("FALSE\n");
7874447Seric # endif DEBUG
7884447Seric 	return (FALSE);
7894447Seric 
7904447Seric   success:
7914447Seric # ifdef DEBUG
7924447Seric 	if (Debug > 7)
7934447Seric 		printf("TRUE\n");
7944447Seric # endif DEBUG
7954447Seric 	return (TRUE);
7964370Seric }
7974370Seric /*
798294Seric **  MAILFILE -- Send a message to a file.
799294Seric **
8004327Seric **	If the file has the setuid/setgid bits set, but NO execute
8014327Seric **	bits, sendmail will try to become the owner of that file
8024327Seric **	rather than the real user.  Obviously, this only works if
8034327Seric **	sendmail runs as root.
8044327Seric **
805294Seric **	Parameters:
806294Seric **		filename -- the name of the file to send to.
8074397Seric **		ctladdr -- the controlling address header -- includes
8084397Seric **			the userid/groupid to be when sending.
809294Seric **
810294Seric **	Returns:
811294Seric **		The exit code associated with the operation.
812294Seric **
813294Seric **	Side Effects:
814294Seric **		none.
815294Seric */
816294Seric 
8174397Seric mailfile(filename, ctladdr)
818294Seric 	char *filename;
8194397Seric 	ADDRESS *ctladdr;
820294Seric {
821294Seric 	register FILE *f;
8224214Seric 	register int pid;
823294Seric 
8244214Seric 	/*
8254214Seric 	**  Fork so we can change permissions here.
8264214Seric 	**	Note that we MUST use fork, not vfork, because of
8274214Seric 	**	the complications of calling subroutines, etc.
8284214Seric 	*/
8294067Seric 
8304214Seric 	DOFORK(fork);
8314214Seric 
8324214Seric 	if (pid < 0)
8334214Seric 		return (EX_OSERR);
8344214Seric 	else if (pid == 0)
8354214Seric 	{
8364214Seric 		/* child -- actually write to file */
8374327Seric 		struct stat stb;
8384417Seric 		extern int DefUid, DefGid;
8394327Seric 
8404215Seric 		(void) signal(SIGINT, SIG_DFL);
8414215Seric 		(void) signal(SIGHUP, SIG_DFL);
8424215Seric 		(void) signal(SIGTERM, SIG_DFL);
8434327Seric 		umask(OldUmask);
8444327Seric 		if (stat(filename, &stb) < 0)
8454431Seric 			stb.st_mode = 0666;
8464327Seric 		if (bitset(0111, stb.st_mode))
8474327Seric 			exit(EX_CANTCREAT);
8484401Seric 		if (ctladdr == NULL)
8494401Seric 			ctladdr = &From;
8504327Seric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
8514417Seric 		{
8524417Seric 			if (ctladdr->q_uid == 0)
8534417Seric 				(void) setgid(DefGid);
8544417Seric 			else
8554417Seric 				(void) setgid(ctladdr->q_gid);
8564417Seric 		}
8574327Seric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
8584417Seric 		{
8594417Seric 			if (ctladdr->q_uid == 0)
8604417Seric 				(void) setuid(DefUid);
8614417Seric 			else
8624417Seric 				(void) setuid(ctladdr->q_uid);
8634417Seric 		}
8644214Seric 		f = fopen(filename, "a");
8654214Seric 		if (f == NULL)
8664214Seric 			exit(EX_CANTCREAT);
8674214Seric 
8684214Seric 		putmessage(f, Mailer[1]);
8694214Seric 		fputs("\n", f);
8704214Seric 		(void) fclose(f);
8714214Seric 		(void) fflush(stdout);
8724417Seric 
8734417Seric 		/* reset ISUID & ISGID bits */
8744621Seric 		(void) chmod(filename, (int) stb.st_mode);
8754214Seric 		exit(EX_OK);
8764315Seric 		/*NOTREACHED*/
8774214Seric 	}
8784214Seric 	else
8794214Seric 	{
8804214Seric 		/* parent -- wait for exit status */
8814214Seric 		register int i;
8824214Seric 		auto int stat;
8834214Seric 
8844214Seric 		while ((i = wait(&stat)) != pid)
8854214Seric 		{
8864214Seric 			if (i < 0)
8874214Seric 			{
8884214Seric 				stat = EX_OSERR << 8;
8894214Seric 				break;
8904214Seric 			}
8914214Seric 		}
8924215Seric 		if ((stat & 0377) != 0)
8934215Seric 			stat = EX_UNAVAILABLE << 8;
8944214Seric 		return ((stat >> 8) & 0377);
8954214Seric 	}
896294Seric }
8974550Seric /*
8984550Seric **  SENDALL -- actually send all the messages.
8994550Seric **
9004550Seric **	Parameters:
9014550Seric **		verifyonly -- if set, only give verification messages.
9024550Seric **
9034550Seric **	Returns:
9044550Seric **		none.
9054550Seric **
9064550Seric **	Side Effects:
9074550Seric **		Scans the send lists and sends everything it finds.
9084550Seric */
9094550Seric 
9104550Seric sendall(verifyonly)
9114550Seric 	bool verifyonly;
9124550Seric {
9134550Seric 	register int i;
9144550Seric 	typedef int (*fnptr)();
9154550Seric 
9164550Seric 	for (i = 0; Mailer[i] != NULL; i++)
9174550Seric 	{
9184550Seric 		ADDRESS *q;
9194550Seric 
9204550Seric 		for (q = Mailer[i]->m_sendq; q != NULL; q = q->q_next)
9214550Seric 		{
9224550Seric 			if (verifyonly)
9234550Seric 			{
9244550Seric 				To = q->q_paddr;
9254550Seric 				if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
9264550Seric 				{
9274596Seric 					if (bitset(M_LOCAL, q->q_mailer->m_flags))
9284550Seric 						message(Arpa_Info, "deliverable");
9294550Seric 					else
9304550Seric 						message(Arpa_Info, "queueable");
9314550Seric 				}
9324550Seric 			}
9334550Seric 			else
9344550Seric 				(void) deliver(q, (fnptr) NULL);
9354550Seric 		}
9364550Seric 	}
9374550Seric }
938