125a99e2eSeric # include <signal.h>
254aa2b0fSeric # include <errno.h>
3b20b3270Seric # include "sendmail.h"
4c77d1c25Seric # include <sys/stat.h>
525a99e2eSeric # ifdef LOG
6e374fd72Seric # include <syslog.h>
725a99e2eSeric # endif LOG
825a99e2eSeric 
9*e77e673fSeric static char SccsId[] = "@(#)deliver.c	3.55	11/21/81";
10259cace7Seric 
1125a99e2eSeric /*
1213bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
1313bbc08cSeric **
1413bbc08cSeric **	This routine delivers to everyone on the same host as the
1513bbc08cSeric **	user on the head of the list.  It is clever about mailers
1613bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
1713bbc08cSeric **	that it will deliver to all these addresses however -- so
1813bbc08cSeric **	deliver should be called once for each address on the
1913bbc08cSeric **	list.
2025a99e2eSeric **
2125a99e2eSeric **	Parameters:
22c77d1c25Seric **		firstto -- head of the address list to deliver to.
2325a99e2eSeric **		editfcn -- if non-NULL, we want to call this function
2425a99e2eSeric **			to output the letter (instead of just out-
2525a99e2eSeric **			putting it raw).
2625a99e2eSeric **
2725a99e2eSeric **	Returns:
2825a99e2eSeric **		zero -- successfully delivered.
2925a99e2eSeric **		else -- some failure, see ExitStat for more info.
3025a99e2eSeric **
3125a99e2eSeric **	Side Effects:
3225a99e2eSeric **		The standard input is passed off to someone.
3325a99e2eSeric */
3425a99e2eSeric 
35c77d1c25Seric deliver(firstto, editfcn)
36c77d1c25Seric 	ADDRESS *firstto;
3725a99e2eSeric 	int (*editfcn)();
3825a99e2eSeric {
3978442df3Seric 	char *host;			/* host being sent to */
4078442df3Seric 	char *user;			/* user being sent to */
4125a99e2eSeric 	char **pvp;
425dfc646bSeric 	register char **mvp;
4325a99e2eSeric 	register char *p;
4478442df3Seric 	register struct mailer *m;	/* mailer for this recipient */
455dfc646bSeric 	register int i;
466328bdf7Seric 	extern putmessage();
472a6e0786Seric 	extern bool checkcompat();
485dfc646bSeric 	char *pv[MAXPV+1];
4978442df3Seric 	char tobuf[MAXLINE];		/* text line of to people */
505dfc646bSeric 	char buf[MAXNAME];
516259796dSeric 	ADDRESS *ctladdr;
526259796dSeric 	extern ADDRESS *getctladdr();
5378442df3Seric 	char tfrombuf[MAXNAME];		/* translated from person */
5478442df3Seric 	extern char **prescan();
55c77d1c25Seric 	register ADDRESS *to = firstto;
56c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
57c579ef51Seric 	bool tempfail = FALSE;
5825a99e2eSeric 
5935490626Seric 	errno = 0;
60*e77e673fSeric 	if (!ForceMail && bitset(QDONTSEND, to->q_flags))
615dfc646bSeric 		return (0);
6225a99e2eSeric 
6325a99e2eSeric # ifdef DEBUG
6425a99e2eSeric 	if (Debug)
655dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
667da1035fSeric 			to->q_mailer->m_mno, to->q_host, to->q_user);
6725a99e2eSeric # endif DEBUG
6825a99e2eSeric 
6925a99e2eSeric 	/*
705dfc646bSeric 	**  Do initial argv setup.
715dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
725dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
735dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
745dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
755dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
765dfc646bSeric 	*/
775dfc646bSeric 
787da1035fSeric 	m = to->q_mailer;
795dfc646bSeric 	host = to->q_host;
8078442df3Seric 
8178442df3Seric 	/* rewrite from address, using rewriting rules */
8278442df3Seric 	(void) expand(m->m_from, buf, &buf[sizeof buf - 1]);
8378442df3Seric 	mvp = prescan(buf, '\0');
8478442df3Seric 	if (mvp == NULL)
8578442df3Seric 	{
8678442df3Seric 		syserr("bad mailer from translate \"%s\"", buf);
8778442df3Seric 		return (EX_SOFTWARE);
8878442df3Seric 	}
8978442df3Seric 	rewrite(mvp, 2);
9078442df3Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
9178442df3Seric 
9278442df3Seric 	define('g', tfrombuf);		/* translated sender address */
935dfc646bSeric 	define('h', host);		/* to host */
945dfc646bSeric 	Errors = 0;
955dfc646bSeric 	pvp = pv;
965dfc646bSeric 	*pvp++ = m->m_argv[0];
975dfc646bSeric 
985dfc646bSeric 	/* insert -f or -r flag as appropriate */
995dfc646bSeric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1005dfc646bSeric 	{
1015dfc646bSeric 		if (bitset(M_FOPT, m->m_flags))
1025dfc646bSeric 			*pvp++ = "-f";
1035dfc646bSeric 		else
1045dfc646bSeric 			*pvp++ = "-r";
105db8841e9Seric 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
1065dfc646bSeric 		*pvp++ = newstr(buf);
1075dfc646bSeric 	}
1085dfc646bSeric 
1095dfc646bSeric 	/*
1105dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1115dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1125dfc646bSeric 	**  be one of these, and there are only a few more slots
1135dfc646bSeric 	**  in the pv after it.
1145dfc646bSeric 	*/
1155dfc646bSeric 
1165dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1175dfc646bSeric 	{
1185dfc646bSeric 		while ((p = index(p, '$')) != NULL)
1195dfc646bSeric 			if (*++p == 'u')
1205dfc646bSeric 				break;
1215dfc646bSeric 		if (p != NULL)
1225dfc646bSeric 			break;
1235dfc646bSeric 
1245dfc646bSeric 		/* this entry is safe -- go ahead and process it */
125db8841e9Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
1265dfc646bSeric 		*pvp++ = newstr(buf);
1275dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1285dfc646bSeric 		{
1295dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1305dfc646bSeric 			return (-1);
1315dfc646bSeric 		}
1325dfc646bSeric 	}
133c579ef51Seric 
1345dfc646bSeric 	if (*mvp == NULL)
135c579ef51Seric 	{
136c579ef51Seric 		/* running SMTP */
137c579ef51Seric 		clever = TRUE;
138c579ef51Seric 		*pvp = NULL;
139c579ef51Seric 		i = smtpinit(m, pv, (ADDRESS *) NULL);
140c579ef51Seric 		giveresponse(i, TRUE, m);
141c579ef51Seric 		if (i == EX_TEMPFAIL)
142c579ef51Seric 		{
143c579ef51Seric 			QueueUp = TRUE;
144c579ef51Seric 			tempfail = TRUE;
145c579ef51Seric 		}
146c579ef51Seric 	}
1475dfc646bSeric 
1485dfc646bSeric 	/*
1495dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
1505dfc646bSeric 	**  run through our address list and append all the addresses
1515dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
1525dfc646bSeric 	**  always send another copy later.
1535dfc646bSeric 	*/
1545dfc646bSeric 
1555dfc646bSeric 	tobuf[0] = '\0';
1565dfc646bSeric 	To = tobuf;
1576259796dSeric 	ctladdr = NULL;
1585dfc646bSeric 	for (; to != NULL; to = to->q_next)
1595dfc646bSeric 	{
1605dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
161bea22b26Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1625dfc646bSeric 			break;
1635dfc646bSeric 
1645dfc646bSeric 		/* if already sent or not for this host, don't send */
165*e77e673fSeric 		if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) ||
166*e77e673fSeric 		    strcmp(to->q_host, host) != 0 || to->q_mailer != firstto->q_mailer)
1675dfc646bSeric 			continue;
1686259796dSeric 
1696259796dSeric 		/* compute effective uid/gid when sending */
1707da1035fSeric 		if (to->q_mailer == ProgMailer)
1716259796dSeric 			ctladdr = getctladdr(to);
1726259796dSeric 
1735dfc646bSeric 		user = to->q_user;
1745dfc646bSeric 		To = to->q_paddr;
1755dfc646bSeric 		to->q_flags |= QDONTSEND;
176c579ef51Seric 		if (tempfail)
177c579ef51Seric 			to->q_flags |= QQUEUEUP;
1785dfc646bSeric # ifdef DEBUG
1795dfc646bSeric 		if (Debug)
1805dfc646bSeric 			printf("   send to `%s'\n", user);
1815dfc646bSeric # endif DEBUG
1825dfc646bSeric 
1835dfc646bSeric 		/*
1845dfc646bSeric 		**  Check to see that these people are allowed to
1855dfc646bSeric 		**  talk to each other.
1862a6e0786Seric 		*/
1872a6e0786Seric 
1882a6e0786Seric 		if (!checkcompat(to))
1895dfc646bSeric 		{
1905dfc646bSeric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
1915dfc646bSeric 			continue;
1925dfc646bSeric 		}
1932a6e0786Seric 
1942a6e0786Seric 		/*
1959ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
1969ec9501bSeric 		**	about them.
19725a99e2eSeric 		*/
19825a99e2eSeric 
1992a6e0786Seric 		if (bitset(M_STRIPQ, m->m_flags))
20025a99e2eSeric 		{
2019ec9501bSeric 			stripquotes(user, TRUE);
2029ec9501bSeric 			stripquotes(host, TRUE);
2039ec9501bSeric 		}
2049ec9501bSeric 		else
2059ec9501bSeric 		{
2069ec9501bSeric 			stripquotes(user, FALSE);
2079ec9501bSeric 			stripquotes(host, FALSE);
20825a99e2eSeric 		}
20925a99e2eSeric 
21025a99e2eSeric 		/*
2113efaed6eSeric 		**  If an error message has already been given, don't
2123efaed6eSeric 		**	bother to send to this address.
2133efaed6eSeric 		**
2143efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2153efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2163efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2173efaed6eSeric 		*/
2183efaed6eSeric 
2193efaed6eSeric 		if (bitset(QBADADDR, to->q_flags))
2203efaed6eSeric 			continue;
2213efaed6eSeric 
222f2fec898Seric 		/* save statistics.... */
2237da1035fSeric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2247da1035fSeric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(MsgSize);
225f2fec898Seric 
2263efaed6eSeric 		/*
22725a99e2eSeric 		**  See if this user name is "special".
22825a99e2eSeric 		**	If the user name has a slash in it, assume that this
22925a99e2eSeric 		**	is a file -- send it off without further ado.
23025a99e2eSeric 		**	Note that this means that editfcn's will not
2315dfc646bSeric 		**	be applied to the message.  Also note that
2325dfc646bSeric 		**	this type of addresses is not processed along
2335dfc646bSeric 		**	with the others, so we fudge on the To person.
23425a99e2eSeric 		*/
23525a99e2eSeric 
2367da1035fSeric 		if (m == LocalMailer)
23725a99e2eSeric 		{
23825a99e2eSeric 			if (index(user, '/') != NULL)
23925a99e2eSeric 			{
2406259796dSeric 				i = mailfile(user, getctladdr(to));
24125a99e2eSeric 				giveresponse(i, TRUE, m);
2425dfc646bSeric 				continue;
24325a99e2eSeric 			}
24425a99e2eSeric 		}
24525a99e2eSeric 
24613bbc08cSeric 		/*
24713bbc08cSeric 		**  Address is verified -- add this user to mailer
24813bbc08cSeric 		**  argv, and add it to the print list of recipients.
24913bbc08cSeric 		*/
25013bbc08cSeric 
2515dfc646bSeric 		/* create list of users for error messages */
2525dfc646bSeric 		if (tobuf[0] != '\0')
253db8841e9Seric 			(void) strcat(tobuf, ",");
254db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
2555dfc646bSeric 		define('u', user);		/* to user */
256c2567733Seric 		define('z', to->q_home);	/* user's home */
2575dfc646bSeric 
258c579ef51Seric 		/*
259c579ef51Seric 		**  Expand out this user into argument list or
260c579ef51Seric 		**  send it to our SMTP server.
261c579ef51Seric 		*/
262c579ef51Seric 
263c579ef51Seric 		if (clever)
264c579ef51Seric 		{
265d2b7d202Seric 			i = smtprcpt(to);
266c579ef51Seric 			if (i == EX_TEMPFAIL)
267c579ef51Seric 			{
268c579ef51Seric 				QueueUp = TRUE;
269c579ef51Seric 				to->q_flags |= QQUEUEUP;
270c579ef51Seric 			}
271c579ef51Seric 			else if (i != EX_OK)
272c579ef51Seric 			{
273c579ef51Seric 				to->q_flags |= QBADADDR;
274c579ef51Seric 				giveresponse(i, TRUE, m);
275c579ef51Seric 			}
276c579ef51Seric 		}
277c579ef51Seric 		else
278c579ef51Seric 		{
279d4ccc802Seric 			(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
2805dfc646bSeric 			*pvp++ = newstr(buf);
2815dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
2825dfc646bSeric 			{
2835dfc646bSeric 				/* allow some space for trailing parms */
2845dfc646bSeric 				break;
2855dfc646bSeric 			}
2865dfc646bSeric 		}
287c579ef51Seric 	}
2885dfc646bSeric 
289145b49b1Seric 	/* see if any addresses still exist */
290145b49b1Seric 	if (tobuf[0] == '\0')
291c579ef51Seric 	{
292c579ef51Seric 		if (clever)
293c579ef51Seric 			smtpquit(pv[0]);
294145b49b1Seric 		return (0);
295c579ef51Seric 	}
296145b49b1Seric 
2975dfc646bSeric 	/* print out messages as full list */
2985dfc646bSeric 	To = tobuf;
2995dfc646bSeric 
3005dfc646bSeric 	/*
3015dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3025dfc646bSeric 	*/
3035dfc646bSeric 
304c579ef51Seric 	while (!clever && *++mvp != NULL)
3055dfc646bSeric 	{
306db8841e9Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
3075dfc646bSeric 		*pvp++ = newstr(buf);
3085dfc646bSeric 		if (pvp >= &pv[MAXPV])
3095dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
3105dfc646bSeric 	}
3115dfc646bSeric 	*pvp++ = NULL;
3125dfc646bSeric 
31325a99e2eSeric 	/*
31425a99e2eSeric 	**  Call the mailer.
3156328bdf7Seric 	**	The argument vector gets built, pipes
31625a99e2eSeric 	**	are created as necessary, and we fork & exec as
3176328bdf7Seric 	**	appropriate.
318c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
31925a99e2eSeric 	*/
32025a99e2eSeric 
3215dfc646bSeric 	if (editfcn == NULL)
3225dfc646bSeric 		editfcn = putmessage;
3236259796dSeric 	if (ctladdr == NULL)
3246259796dSeric 		ctladdr = &From;
325c579ef51Seric 	if (clever)
326c579ef51Seric 	{
327c579ef51Seric 		i = smtpfinish(m, editfcn);
328c579ef51Seric 		smtpquit(pv[0]);
329c579ef51Seric 	}
330c579ef51Seric 	else
3316259796dSeric 		i = sendoff(m, pv, editfcn, ctladdr);
3325dfc646bSeric 
333c77d1c25Seric 	/*
334c77d1c25Seric 	**  If we got a temporary failure, arrange to queue the
335c77d1c25Seric 	**  addressees.
336c77d1c25Seric 	*/
337c77d1c25Seric 
338c77d1c25Seric 	if (i == EX_TEMPFAIL)
339c77d1c25Seric 	{
340c77d1c25Seric 		QueueUp = TRUE;
341c77d1c25Seric 		for (to = firstto; to != NULL; to = to->q_next)
342c77d1c25Seric 		{
343c77d1c25Seric 			if (bitset(QBADADDR, to->q_flags))
344c77d1c25Seric 				continue;
345c77d1c25Seric 			to->q_flags |= QQUEUEUP;
346c77d1c25Seric 		}
347c77d1c25Seric 	}
348c77d1c25Seric 
34935490626Seric 	errno = 0;
3505dfc646bSeric 	return (i);
35125a99e2eSeric }
3525dfc646bSeric /*
35332d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
35432d19d43Seric **
35532d19d43Seric **	This MUST be a macro, since after a vfork we are running
35632d19d43Seric **	two processes on the same stack!!!
35732d19d43Seric **
35832d19d43Seric **	Parameters:
35932d19d43Seric **		none.
36032d19d43Seric **
36132d19d43Seric **	Returns:
36232d19d43Seric **		From a macro???  You've got to be kidding!
36332d19d43Seric **
36432d19d43Seric **	Side Effects:
36532d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
36632d19d43Seric **			pid of child in parent, zero in child.
36732d19d43Seric **			-1 on unrecoverable error.
36832d19d43Seric **
36932d19d43Seric **	Notes:
37032d19d43Seric **		I'm awfully sorry this looks so awful.  That's
37132d19d43Seric **		vfork for you.....
37232d19d43Seric */
37332d19d43Seric 
37432d19d43Seric # define NFORKTRIES	5
37532d19d43Seric # ifdef VFORK
37632d19d43Seric # define XFORK	vfork
37732d19d43Seric # else VFORK
37832d19d43Seric # define XFORK	fork
37932d19d43Seric # endif VFORK
38032d19d43Seric 
38132d19d43Seric # define DOFORK(fORKfN) \
38232d19d43Seric {\
38332d19d43Seric 	register int i;\
38432d19d43Seric \
38532d19d43Seric 	for (i = NFORKTRIES; i-- > 0; )\
38632d19d43Seric 	{\
38732d19d43Seric 		pid = fORKfN();\
38832d19d43Seric 		if (pid >= 0)\
38932d19d43Seric 			break;\
39032d19d43Seric 		sleep((unsigned) NFORKTRIES - i);\
39132d19d43Seric 	}\
39232d19d43Seric }
39332d19d43Seric /*
3942ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
3952ed72599Seric **
3962ed72599Seric **	Parameters:
3972ed72599Seric **		none.
3982ed72599Seric **
3992ed72599Seric **	Returns:
4002ed72599Seric **		pid of child in parent.
4012ed72599Seric **		zero in child.
4022ed72599Seric **		-1 on error.
4032ed72599Seric **
4042ed72599Seric **	Side Effects:
4052ed72599Seric **		returns twice, once in parent and once in child.
4062ed72599Seric */
4072ed72599Seric 
4082ed72599Seric dofork()
4092ed72599Seric {
4102ed72599Seric 	register int pid;
4112ed72599Seric 
4122ed72599Seric 	DOFORK(fork);
4132ed72599Seric 	return (pid);
4142ed72599Seric }
4152ed72599Seric /*
4165dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
4175dfc646bSeric **
4185dfc646bSeric **	Parameters:
4195dfc646bSeric **		m -- mailer descriptor.
4205dfc646bSeric **		pvp -- parameter vector to send to it.
4215dfc646bSeric **		editfcn -- function to pipe it through.
4226259796dSeric **		ctladdr -- an address pointer controlling the
4236259796dSeric **			user/groupid etc. of the mailer.
4245dfc646bSeric **
4255dfc646bSeric **	Returns:
4265dfc646bSeric **		exit status of mailer.
4275dfc646bSeric **
4285dfc646bSeric **	Side Effects:
4295dfc646bSeric **		none.
4305dfc646bSeric */
4315dfc646bSeric 
4326259796dSeric sendoff(m, pvp, editfcn, ctladdr)
4335dfc646bSeric 	struct mailer *m;
4345dfc646bSeric 	char **pvp;
4355dfc646bSeric 	int (*editfcn)();
4366259796dSeric 	ADDRESS *ctladdr;
4375dfc646bSeric {
438c579ef51Seric 	auto FILE *mfile;
439c579ef51Seric 	auto FILE *rfile;
4405dfc646bSeric 	register int i;
441c579ef51Seric 	extern putmessage();
442c579ef51Seric 	int pid;
443c579ef51Seric 
444c579ef51Seric 	/*
445c579ef51Seric 	**  Create connection to mailer.
446c579ef51Seric 	*/
447c579ef51Seric 
448c579ef51Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
449c579ef51Seric 	if (pid < 0)
450c579ef51Seric 		return (-1);
451c579ef51Seric 
452c579ef51Seric 	/*
453c579ef51Seric 	**  Format and send message.
454c579ef51Seric 	*/
455c579ef51Seric 
456c579ef51Seric 	(void) signal(SIGPIPE, SIG_IGN);
457c579ef51Seric 	if (editfcn == NULL)
458c579ef51Seric 		editfcn = putmessage;
459c579ef51Seric 
460c579ef51Seric 	(*editfcn)(mfile, m, FALSE);
461c579ef51Seric 	(void) fclose(mfile);
462c579ef51Seric 
463c579ef51Seric 	i = endmailer(pid, pvp[0]);
464c579ef51Seric 	giveresponse(i, TRUE, m);
465c579ef51Seric 	return (i);
466c579ef51Seric }
467c579ef51Seric /*
468c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
469c579ef51Seric **
470c579ef51Seric **	We should never get fatal errors (e.g., segmentation
471c579ef51Seric **	violation), so we report those specially.  For other
472c579ef51Seric **	errors, we choose a status message (into statmsg),
473c579ef51Seric **	and if it represents an error, we print it.
474c579ef51Seric **
475c579ef51Seric **	Parameters:
476c579ef51Seric **		pid -- pid of mailer.
477c579ef51Seric **		name -- name of mailer (for error messages).
478c579ef51Seric **
479c579ef51Seric **	Returns:
480c579ef51Seric **		exit code of mailer.
481c579ef51Seric **
482c579ef51Seric **	Side Effects:
483c579ef51Seric **		none.
484c579ef51Seric */
485c579ef51Seric 
486c579ef51Seric endmailer(pid, name)
487c579ef51Seric 	int pid;
488c579ef51Seric 	char *name;
489c579ef51Seric {
490c579ef51Seric 	register int i;
491c579ef51Seric 	auto int st;
492c579ef51Seric 
493c579ef51Seric 	while ((i = wait(&st)) > 0 && i != pid)
494c579ef51Seric 		continue;
495c579ef51Seric 	if (i < 0)
496c579ef51Seric 	{
497c579ef51Seric 		syserr("wait");
498c579ef51Seric 		return (-1);
499c579ef51Seric 	}
500c579ef51Seric 	if ((st & 0377) != 0)
501c579ef51Seric 	{
502c579ef51Seric 		syserr("%s: stat %o", name, st);
503c579ef51Seric 		ExitStat = EX_UNAVAILABLE;
504c579ef51Seric 		return (-1);
505c579ef51Seric 	}
506c579ef51Seric 	i = (st >> 8) & 0377;
507c579ef51Seric 	return (i);
508c579ef51Seric }
509c579ef51Seric /*
510c579ef51Seric **  OPENMAILER -- open connection to mailer.
511c579ef51Seric **
512c579ef51Seric **	Parameters:
513c579ef51Seric **		m -- mailer descriptor.
514c579ef51Seric **		pvp -- parameter vector to pass to mailer.
515c579ef51Seric **		ctladdr -- controlling address for user.
516c579ef51Seric **		clever -- create a full duplex connection.
517c579ef51Seric **		pmfile -- pointer to mfile (to mailer) connection.
518c579ef51Seric **		prfile -- pointer to rfile (from mailer) connection.
519c579ef51Seric **
520c579ef51Seric **	Returns:
521c579ef51Seric **		pid of mailer.
522c579ef51Seric **		-1 on error.
523c579ef51Seric **
524c579ef51Seric **	Side Effects:
525c579ef51Seric **		creates a mailer in a subprocess.
526c579ef51Seric */
527c579ef51Seric 
528c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
529c579ef51Seric 	struct mailer *m;
530c579ef51Seric 	char **pvp;
531c579ef51Seric 	ADDRESS *ctladdr;
532c579ef51Seric 	bool clever;
533c579ef51Seric 	FILE **pmfile;
534c579ef51Seric 	FILE **prfile;
535c579ef51Seric {
5365dfc646bSeric 	int pid;
537f8952a83Seric 	int mpvect[2];
538c579ef51Seric 	int rpvect[2];
5395dfc646bSeric 	FILE *mfile;
540c579ef51Seric 	FILE *rfile;
5415dfc646bSeric 	extern FILE *fdopen();
5425dfc646bSeric 
5435dfc646bSeric # ifdef DEBUG
5445dfc646bSeric 	if (Debug)
5455dfc646bSeric 	{
546c579ef51Seric 		printf("openmailer:\n");
5475dfc646bSeric 		printav(pvp);
5485dfc646bSeric 	}
5495dfc646bSeric # endif DEBUG
55035490626Seric 	errno = 0;
5515dfc646bSeric 
5526328bdf7Seric 	/* create a pipe to shove the mail through */
553f8952a83Seric 	if (pipe(mpvect) < 0)
55425a99e2eSeric 	{
555c579ef51Seric 		syserr("pipe (to mailer)");
55625a99e2eSeric 		return (-1);
55725a99e2eSeric 	}
558c579ef51Seric 
559c579ef51Seric 	/* if this mailer speaks smtp, create a return pipe */
560c579ef51Seric 	if (clever && pipe(rpvect) < 0)
561c579ef51Seric 	{
562c579ef51Seric 		syserr("pipe (from mailer)");
563c579ef51Seric 		(void) close(mpvect[0]);
564c579ef51Seric 		(void) close(mpvect[1]);
565c579ef51Seric 		return (-1);
566c579ef51Seric 	}
567c579ef51Seric 
56832d19d43Seric 	DOFORK(XFORK);
569f129ec7dSeric 	/* pid is set by DOFORK */
57025a99e2eSeric 	if (pid < 0)
57125a99e2eSeric 	{
57225a99e2eSeric 		syserr("Cannot fork");
573f8952a83Seric 		(void) close(mpvect[0]);
574f8952a83Seric 		(void) close(mpvect[1]);
575c579ef51Seric 		if (clever)
576c579ef51Seric 		{
577c579ef51Seric 			(void) close(rpvect[0]);
578c579ef51Seric 			(void) close(rpvect[1]);
579c579ef51Seric 		}
58025a99e2eSeric 		return (-1);
58125a99e2eSeric 	}
58225a99e2eSeric 	else if (pid == 0)
58325a99e2eSeric 	{
58425a99e2eSeric 		/* child -- set up input & exec mailer */
58503ab8e55Seric 		/* make diagnostic output be standard output */
5868f0e7860Seric 		(void) signal(SIGINT, SIG_IGN);
5878f0e7860Seric 		(void) signal(SIGHUP, SIG_IGN);
5880984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
589f8952a83Seric 
590f8952a83Seric 		/* arrange to filter standard & diag output of command */
591c579ef51Seric 		if (clever)
592c579ef51Seric 		{
593c579ef51Seric 			(void) close(rpvect[0]);
594c579ef51Seric 			(void) close(1);
595c579ef51Seric 			(void) dup(rpvect[1]);
596c579ef51Seric 			(void) close(rpvect[1]);
597c579ef51Seric 		}
598c579ef51Seric 		else if (OutChannel != stdout)
599f8952a83Seric 		{
600f8952a83Seric 			(void) close(1);
601f8952a83Seric 			(void) dup(fileno(OutChannel));
602f8952a83Seric 		}
603db8841e9Seric 		(void) close(2);
604db8841e9Seric 		(void) dup(1);
605f8952a83Seric 
606f8952a83Seric 		/* arrange to get standard input */
607f8952a83Seric 		(void) close(mpvect[1]);
608db8841e9Seric 		(void) close(0);
609f8952a83Seric 		if (dup(mpvect[0]) < 0)
61025a99e2eSeric 		{
61125a99e2eSeric 			syserr("Cannot dup to zero!");
612a590b978Seric 			_exit(EX_OSERR);
61325a99e2eSeric 		}
614f8952a83Seric 		(void) close(mpvect[0]);
6152a6e0786Seric 		if (!bitset(M_RESTR, m->m_flags))
6160984da9fSeric 		{
617e36b99e2Seric 			if (ctladdr->q_uid == 0)
618e36b99e2Seric 			{
61969f29479Seric 				extern int DefUid, DefGid;
62069f29479Seric 
621e36b99e2Seric 				(void) setgid(DefGid);
622e36b99e2Seric 				(void) setuid(DefUid);
623e36b99e2Seric 			}
624e36b99e2Seric 			else
62569f29479Seric 			{
626e36b99e2Seric 				(void) setgid(ctladdr->q_gid);
627e36b99e2Seric 				(void) setuid(ctladdr->q_uid);
62869f29479Seric 			}
6290984da9fSeric 		}
630e374fd72Seric # ifndef VFORK
631e374fd72Seric 		/*
632e374fd72Seric 		**  We have to be careful with vfork - we can't mung up the
633e374fd72Seric 		**  memory but we don't want the mailer to inherit any extra
634e374fd72Seric 		**  open files.  Chances are the mailer won't
635e374fd72Seric 		**  care about an extra file, but then again you never know.
636e374fd72Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
637e374fd72Seric 		**  declared static so we can't.  But if we fclose(pwf), which
638e374fd72Seric 		**  is what endpwent does, it closes it in the parent too and
639e374fd72Seric 		**  the next getpwnam will be slower.  If you have a weird
640e374fd72Seric 		**  mailer that chokes on the extra file you should do the
641e374fd72Seric 		**  endpwent().
642e374fd72Seric 		**
643e374fd72Seric 		**  Similar comments apply to log.  However, openlog is
644e374fd72Seric 		**  clever enough to set the FIOCLEX mode on the file,
645e374fd72Seric 		**  so it will be closed automatically on the exec.
646e374fd72Seric 		*/
647e374fd72Seric 
648e374fd72Seric 		endpwent();
64925a99e2eSeric # ifdef LOG
650f9fe028fSeric 		closelog();
65125a99e2eSeric # endif LOG
652e374fd72Seric # endif VFORK
65325a99e2eSeric 		execv(m->m_mailer, pvp);
65425a99e2eSeric 		/* syserr fails because log is closed */
65525a99e2eSeric 		/* syserr("Cannot exec %s", m->m_mailer); */
65632d19d43Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
657db8841e9Seric 		(void) fflush(stdout);
658a590b978Seric 		_exit(EX_UNAVAILABLE);
65925a99e2eSeric 	}
66025a99e2eSeric 
661f8952a83Seric 	/*
662c579ef51Seric 	**  Set up return value.
663f8952a83Seric 	*/
664f8952a83Seric 
665f8952a83Seric 	(void) close(mpvect[0]);
666f8952a83Seric 	mfile = fdopen(mpvect[1], "w");
667c579ef51Seric 	if (clever)
66825a99e2eSeric 	{
669c579ef51Seric 		(void) close(rpvect[1]);
670c579ef51Seric 		rfile = fdopen(rpvect[0], "r");
67125a99e2eSeric 	}
672c579ef51Seric 
673c579ef51Seric 	*pmfile = mfile;
674c579ef51Seric 	*prfile = rfile;
675c579ef51Seric 
676c579ef51Seric 	return (pid);
67725a99e2eSeric }
67825a99e2eSeric /*
67925a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
68025a99e2eSeric **
68125a99e2eSeric **	Parameters:
68225a99e2eSeric **		stat -- the status code from the mailer (high byte
68325a99e2eSeric **			only; core dumps must have been taken care of
68425a99e2eSeric **			already).
68525a99e2eSeric **		force -- if set, force an error message output, even
68625a99e2eSeric **			if the mailer seems to like to print its own
68725a99e2eSeric **			messages.
68825a99e2eSeric **		m -- the mailer descriptor for this mailer.
68925a99e2eSeric **
69025a99e2eSeric **	Returns:
691db8841e9Seric **		none.
69225a99e2eSeric **
69325a99e2eSeric **	Side Effects:
694c1f9df2cSeric **		Errors may be incremented.
69525a99e2eSeric **		ExitStat may be set.
69625a99e2eSeric */
69725a99e2eSeric 
69825a99e2eSeric giveresponse(stat, force, m)
69925a99e2eSeric 	int stat;
70025a99e2eSeric 	int force;
70125a99e2eSeric 	register struct mailer *m;
70225a99e2eSeric {
70325a99e2eSeric 	register char *statmsg;
70425a99e2eSeric 	extern char *SysExMsg[];
70525a99e2eSeric 	register int i;
70625a99e2eSeric 	extern int N_SysEx;
70729dd97a3Seric 	char buf[30];
70825a99e2eSeric 
70913bbc08cSeric 	/*
71013bbc08cSeric 	**  Compute status message from code.
71113bbc08cSeric 	*/
71213bbc08cSeric 
71325a99e2eSeric 	i = stat - EX__BASE;
71425a99e2eSeric 	if (i < 0 || i > N_SysEx)
71525a99e2eSeric 		statmsg = NULL;
71625a99e2eSeric 	else
71725a99e2eSeric 		statmsg = SysExMsg[i];
71825a99e2eSeric 	if (stat == 0)
719c38ba59cSeric 	{
7206cbfa739Seric 		if (bitset(M_LOCAL, m->m_flags))
7213efaed6eSeric 			statmsg = "delivered";
7223efaed6eSeric 		else
7233efaed6eSeric 			statmsg = "queued";
724c38ba59cSeric 		if (Verbose)
725544026c5Seric 			message(Arpa_Info, statmsg);
726c38ba59cSeric 	}
727c77d1c25Seric 	else if (stat == EX_TEMPFAIL)
728c77d1c25Seric 	{
729c77d1c25Seric 		if (Verbose)
730c77d1c25Seric 			message(Arpa_Info, "transmission deferred");
731c77d1c25Seric 	}
73225a99e2eSeric 	else
73325a99e2eSeric 	{
734c1f9df2cSeric 		Errors++;
73525a99e2eSeric 		if (statmsg == NULL && m->m_badstat != 0)
73625a99e2eSeric 		{
73725a99e2eSeric 			stat = m->m_badstat;
73825a99e2eSeric 			i = stat - EX__BASE;
73925a99e2eSeric # ifdef DEBUG
74025a99e2eSeric 			if (i < 0 || i >= N_SysEx)
74125a99e2eSeric 				syserr("Bad m_badstat %d", stat);
74225a99e2eSeric 			else
74325a99e2eSeric # endif DEBUG
74425a99e2eSeric 			statmsg = SysExMsg[i];
74525a99e2eSeric 		}
74625a99e2eSeric 		if (statmsg == NULL)
74725a99e2eSeric 			usrerr("unknown mailer response %d", stat);
748c38ba59cSeric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
74925a99e2eSeric 			usrerr("%s", statmsg);
75025a99e2eSeric 	}
75125a99e2eSeric 
75225a99e2eSeric 	/*
75325a99e2eSeric 	**  Final cleanup.
75425a99e2eSeric 	**	Log a record of the transaction.  Compute the new
75525a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
75625a99e2eSeric 	**	that.
75725a99e2eSeric 	*/
75825a99e2eSeric 
75925a99e2eSeric 	if (statmsg == NULL)
76029dd97a3Seric 	{
761db8841e9Seric 		(void) sprintf(buf, "error %d", stat);
76229dd97a3Seric 		statmsg = buf;
76329dd97a3Seric 	}
76429dd97a3Seric 
76529dd97a3Seric # ifdef LOG
766e374fd72Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
76725a99e2eSeric # endif LOG
768c77d1c25Seric 	if (stat != EX_TEMPFAIL)
769243921eeSeric 		setstat(stat);
77025a99e2eSeric }
77125a99e2eSeric /*
7726328bdf7Seric **  PUTMESSAGE -- output a message to the final mailer.
77325a99e2eSeric **
7746328bdf7Seric **	This routine takes care of recreating the header from the
7756328bdf7Seric **	in-core copy, etc.
77625a99e2eSeric **
77725a99e2eSeric **	Parameters:
7786328bdf7Seric **		fp -- file to output onto.
7796328bdf7Seric **		m -- a mailer descriptor.
780c579ef51Seric **		xdot -- if set, hide lines beginning with dot.
78125a99e2eSeric **
78225a99e2eSeric **	Returns:
7836328bdf7Seric **		none.
78425a99e2eSeric **
78525a99e2eSeric **	Side Effects:
7866328bdf7Seric **		The message is written onto fp.
78725a99e2eSeric */
78825a99e2eSeric 
789c579ef51Seric putmessage(fp, m, xdot)
7906328bdf7Seric 	FILE *fp;
7916328bdf7Seric 	struct mailer *m;
792c579ef51Seric 	bool xdot;
79325a99e2eSeric {
7946328bdf7Seric 	char buf[BUFSIZ];
79513bbc08cSeric 	register HDR *h;
7966328bdf7Seric 	extern char *arpadate();
7976328bdf7Seric 	bool anyheader = FALSE;
798e9ff65b0Seric 	extern char *capitalize();
79987bd9a02Seric 	extern char *hvalue();
80087bd9a02Seric 	extern bool samefrom();
801894de7daSeric 	char *of_line;
80225a99e2eSeric 
80313bbc08cSeric 	/*
80413bbc08cSeric 	**  Output "From" line unless supressed
80513bbc08cSeric 	*/
80613bbc08cSeric 
80740e4ab56Seric 	if (!bitset(M_NHDR, m->m_flags))
8081412cc7cSeric 	{
8091412cc7cSeric 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
8101412cc7cSeric 		fprintf(fp, "%s\n", buf);
8111412cc7cSeric 	}
81240e4ab56Seric 
81313bbc08cSeric 	/*
81413bbc08cSeric 	**  Output all header lines
81513bbc08cSeric 	*/
81613bbc08cSeric 
817894de7daSeric 	of_line = hvalue("original-from");
8186328bdf7Seric 	for (h = Header; h != NULL; h = h->h_link)
8196328bdf7Seric 	{
82013bbc08cSeric 		register char *p;
82187bd9a02Seric 		char *origfrom = OrigFrom;
822894de7daSeric 		bool nooutput;
82313bbc08cSeric 
824894de7daSeric 		nooutput = FALSE;
8259e9163a0Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
8265a0dcb5fSeric 		{
8275a0dcb5fSeric 			p = ")><(";		/* can't happen (I hope) */
828894de7daSeric 			nooutput = TRUE;
8295a0dcb5fSeric 		}
830894de7daSeric 
831894de7daSeric 		/* use From: line from message if generated is the same */
83287bd9a02Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
833894de7daSeric 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
83487bd9a02Seric 		{
83587bd9a02Seric 			p = origfrom;
83687bd9a02Seric 			origfrom = NULL;
83787bd9a02Seric 		}
83887bd9a02Seric 		else if (bitset(H_DEFAULT, h->h_flags))
8394ae18d0eSeric 		{
840db8841e9Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
8414ae18d0eSeric 			p = buf;
8424ae18d0eSeric 		}
8434ae18d0eSeric 		else
8444ae18d0eSeric 			p = h->h_value;
845894de7daSeric 		if (p == NULL || *p == '\0')
8469e9163a0Seric 			continue;
8475a0dcb5fSeric 
8485a0dcb5fSeric 		/* hack, hack -- output Original-From field if different */
849894de7daSeric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
850894de7daSeric 		{
851894de7daSeric 			/* output new Original-From line if needed */
852894de7daSeric 			if (of_line == NULL && !samefrom(p, origfrom))
8535a0dcb5fSeric 			{
85487bd9a02Seric 				fprintf(fp, "Original-From: %s\n", origfrom);
85587bd9a02Seric 				anyheader = TRUE;
8565a0dcb5fSeric 			}
857894de7daSeric 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
858894de7daSeric 			{
859894de7daSeric 				/* delete Original-From: line if redundant */
860894de7daSeric 				p = of_line;
861894de7daSeric 				of_line = NULL;
862894de7daSeric 			}
863894de7daSeric 		}
864894de7daSeric 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
865894de7daSeric 			nooutput = TRUE;
866894de7daSeric 
867894de7daSeric 		/* finally, output the header line */
868894de7daSeric 		if (!nooutput)
869894de7daSeric 		{
870894de7daSeric 			fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
871894de7daSeric 			h->h_flags |= H_USED;
872894de7daSeric 			anyheader = TRUE;
873894de7daSeric 		}
8746328bdf7Seric 	}
8756328bdf7Seric 	if (anyheader)
8766328bdf7Seric 		fprintf(fp, "\n");
8776328bdf7Seric 
87813bbc08cSeric 	/*
87913bbc08cSeric 	**  Output the body of the message
88013bbc08cSeric 	*/
88113bbc08cSeric 
88278442df3Seric 	if (TempFile != NULL)
88378442df3Seric 	{
884b7902a1dSeric 		rewind(TempFile);
885c579ef51Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, TempFile) != NULL)
886c579ef51Seric 			fprintf(fp, "%s%s", xdot && buf[0] == '.' ? "." : "", buf);
8876328bdf7Seric 
88878442df3Seric 		if (ferror(TempFile))
88978442df3Seric 		{
89078442df3Seric 			syserr("putmessage: read error");
89178442df3Seric 			setstat(EX_IOERR);
89278442df3Seric 		}
89378442df3Seric 	}
89478442df3Seric 
895c77d1c25Seric 	(void) fflush(fp);
89654aa2b0fSeric 	if (ferror(fp) && errno != EPIPE)
89725a99e2eSeric 	{
8986328bdf7Seric 		syserr("putmessage: write error");
89925a99e2eSeric 		setstat(EX_IOERR);
90025a99e2eSeric 	}
90154aa2b0fSeric 	errno = 0;
90225a99e2eSeric }
90325a99e2eSeric /*
90487bd9a02Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
90587bd9a02Seric **
90687bd9a02Seric **	Parameters:
90787bd9a02Seric **		ifrom -- internally generated form of from address.
90887bd9a02Seric **		efrom -- external form of from address.
90987bd9a02Seric **
91087bd9a02Seric **	Returns:
91187bd9a02Seric **		TRUE -- if they convey the same info.
91287bd9a02Seric **		FALSE -- if any information has been lost.
91387bd9a02Seric **
91487bd9a02Seric **	Side Effects:
91587bd9a02Seric **		none.
91687bd9a02Seric */
91787bd9a02Seric 
91887bd9a02Seric bool
91987bd9a02Seric samefrom(ifrom, efrom)
92087bd9a02Seric 	char *ifrom;
92187bd9a02Seric 	char *efrom;
92287bd9a02Seric {
923894de7daSeric 	register char *p;
924894de7daSeric 	char buf[MAXNAME + 4];
925894de7daSeric 
926894de7daSeric # ifdef DEBUG
927894de7daSeric 	if (Debug > 7)
928894de7daSeric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
929894de7daSeric # endif DEBUG
930894de7daSeric 	if (strcmp(ifrom, efrom) == 0)
931894de7daSeric 		goto success;
932894de7daSeric 	p = index(ifrom, '@');
933894de7daSeric 	if (p == NULL)
934894de7daSeric 		goto failure;
935894de7daSeric 	*p = '\0';
936894de7daSeric 	strcpy(buf, ifrom);
937894de7daSeric 	strcat(buf, " at ");
938894de7daSeric 	*p++ = '@';
939894de7daSeric 	strcat(buf, p);
940894de7daSeric 	if (strcmp(buf, efrom) == 0)
941894de7daSeric 		goto success;
942894de7daSeric 
943894de7daSeric   failure:
944894de7daSeric # ifdef DEBUG
945894de7daSeric 	if (Debug > 7)
946894de7daSeric 		printf("FALSE\n");
947894de7daSeric # endif DEBUG
948894de7daSeric 	return (FALSE);
949894de7daSeric 
950894de7daSeric   success:
951894de7daSeric # ifdef DEBUG
952894de7daSeric 	if (Debug > 7)
953894de7daSeric 		printf("TRUE\n");
954894de7daSeric # endif DEBUG
955894de7daSeric 	return (TRUE);
95687bd9a02Seric }
95787bd9a02Seric /*
95825a99e2eSeric **  MAILFILE -- Send a message to a file.
95925a99e2eSeric **
960f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
961f129ec7dSeric **	bits, sendmail will try to become the owner of that file
962f129ec7dSeric **	rather than the real user.  Obviously, this only works if
963f129ec7dSeric **	sendmail runs as root.
964f129ec7dSeric **
96525a99e2eSeric **	Parameters:
96625a99e2eSeric **		filename -- the name of the file to send to.
9676259796dSeric **		ctladdr -- the controlling address header -- includes
9686259796dSeric **			the userid/groupid to be when sending.
96925a99e2eSeric **
97025a99e2eSeric **	Returns:
97125a99e2eSeric **		The exit code associated with the operation.
97225a99e2eSeric **
97325a99e2eSeric **	Side Effects:
97425a99e2eSeric **		none.
97525a99e2eSeric */
97625a99e2eSeric 
9776259796dSeric mailfile(filename, ctladdr)
97825a99e2eSeric 	char *filename;
9796259796dSeric 	ADDRESS *ctladdr;
98025a99e2eSeric {
98125a99e2eSeric 	register FILE *f;
98232d19d43Seric 	register int pid;
98325a99e2eSeric 
98432d19d43Seric 	/*
98532d19d43Seric 	**  Fork so we can change permissions here.
98632d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
98732d19d43Seric 	**	the complications of calling subroutines, etc.
98832d19d43Seric 	*/
98932d19d43Seric 
99032d19d43Seric 	DOFORK(fork);
99132d19d43Seric 
99232d19d43Seric 	if (pid < 0)
99332d19d43Seric 		return (EX_OSERR);
99432d19d43Seric 	else if (pid == 0)
99532d19d43Seric 	{
99632d19d43Seric 		/* child -- actually write to file */
997f129ec7dSeric 		struct stat stb;
998e36b99e2Seric 		extern int DefUid, DefGid;
999f129ec7dSeric 
10000984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
10010984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
10020984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
1003f129ec7dSeric 		umask(OldUmask);
1004f129ec7dSeric 		if (stat(filename, &stb) < 0)
1005e6e1265fSeric 			stb.st_mode = 0666;
1006f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1007f129ec7dSeric 			exit(EX_CANTCREAT);
100803827b5fSeric 		if (ctladdr == NULL)
100903827b5fSeric 			ctladdr = &From;
1010f129ec7dSeric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
1011e36b99e2Seric 		{
1012e36b99e2Seric 			if (ctladdr->q_uid == 0)
1013e36b99e2Seric 				(void) setgid(DefGid);
1014e36b99e2Seric 			else
10156259796dSeric 				(void) setgid(ctladdr->q_gid);
1016e36b99e2Seric 		}
1017f129ec7dSeric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
1018e36b99e2Seric 		{
1019e36b99e2Seric 			if (ctladdr->q_uid == 0)
1020e36b99e2Seric 				(void) setuid(DefUid);
1021e36b99e2Seric 			else
10226259796dSeric 				(void) setuid(ctladdr->q_uid);
1023e36b99e2Seric 		}
102425a99e2eSeric 		f = fopen(filename, "a");
102525a99e2eSeric 		if (f == NULL)
102632d19d43Seric 			exit(EX_CANTCREAT);
102725a99e2eSeric 
1028c579ef51Seric 		putmessage(f, Mailer[1], FALSE);
102925a99e2eSeric 		fputs("\n", f);
1030db8841e9Seric 		(void) fclose(f);
103132d19d43Seric 		(void) fflush(stdout);
1032e36b99e2Seric 
1033e36b99e2Seric 		/* reset ISUID & ISGID bits */
1034c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
103532d19d43Seric 		exit(EX_OK);
103613bbc08cSeric 		/*NOTREACHED*/
103732d19d43Seric 	}
103832d19d43Seric 	else
103932d19d43Seric 	{
104032d19d43Seric 		/* parent -- wait for exit status */
104132d19d43Seric 		register int i;
104232d19d43Seric 		auto int stat;
104332d19d43Seric 
104432d19d43Seric 		while ((i = wait(&stat)) != pid)
104532d19d43Seric 		{
104632d19d43Seric 			if (i < 0)
104732d19d43Seric 			{
104832d19d43Seric 				stat = EX_OSERR << 8;
104932d19d43Seric 				break;
105032d19d43Seric 			}
105132d19d43Seric 		}
10520984da9fSeric 		if ((stat & 0377) != 0)
10530984da9fSeric 			stat = EX_UNAVAILABLE << 8;
105432d19d43Seric 		return ((stat >> 8) & 0377);
105532d19d43Seric 	}
105625a99e2eSeric }
1057ea4dc939Seric /*
1058ea4dc939Seric **  SENDALL -- actually send all the messages.
1059ea4dc939Seric **
1060ea4dc939Seric **	Parameters:
1061ea4dc939Seric **		verifyonly -- if set, only give verification messages.
1062ea4dc939Seric **
1063ea4dc939Seric **	Returns:
1064ea4dc939Seric **		none.
1065ea4dc939Seric **
1066ea4dc939Seric **	Side Effects:
1067ea4dc939Seric **		Scans the send lists and sends everything it finds.
1068ea4dc939Seric */
1069ea4dc939Seric 
1070ea4dc939Seric sendall(verifyonly)
1071ea4dc939Seric 	bool verifyonly;
1072ea4dc939Seric {
1073*e77e673fSeric 	register ADDRESS *q;
1074ea4dc939Seric 	typedef int (*fnptr)();
1075ea4dc939Seric 
1076ea4dc939Seric 
1077*e77e673fSeric 	for (q = SendQueue; q != NULL; q = q->q_next)
1078ea4dc939Seric 	{
1079ea4dc939Seric 		if (verifyonly)
1080ea4dc939Seric 		{
1081ea4dc939Seric 			To = q->q_paddr;
1082*e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1083ea4dc939Seric 			{
10847da1035fSeric 				if (bitset(M_LOCAL, q->q_mailer->m_flags))
1085ea4dc939Seric 					message(Arpa_Info, "deliverable");
1086ea4dc939Seric 				else
1087ea4dc939Seric 					message(Arpa_Info, "queueable");
1088ea4dc939Seric 			}
1089ea4dc939Seric 		}
1090ea4dc939Seric 		else
1091ea4dc939Seric 			(void) deliver(q, (fnptr) NULL);
1092ea4dc939Seric 	}
1093ea4dc939Seric }
1094