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*7666Seric SCCSID(@(#)deliver.c	3.95		08/07/82);
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 **
24294Seric **	Returns:
25294Seric **		zero -- successfully delivered.
26294Seric **		else -- some failure, see ExitStat for more info.
27294Seric **
28294Seric **	Side Effects:
29294Seric **		The standard input is passed off to someone.
30294Seric */
31294Seric 
326974Seric deliver(firstto)
334621Seric 	ADDRESS *firstto;
34294Seric {
354452Seric 	char *host;			/* host being sent to */
364452Seric 	char *user;			/* user being sent to */
37294Seric 	char **pvp;
383233Seric 	register char **mvp;
393233Seric 	register char *p;
404452Seric 	register struct mailer *m;	/* mailer for this recipient */
41294Seric 	register int i;
422968Seric 	extern bool checkcompat();
433233Seric 	char *pv[MAXPV+1];
444452Seric 	char tobuf[MAXLINE];		/* text line of to people */
453233Seric 	char buf[MAXNAME];
464397Seric 	ADDRESS *ctladdr;
474397Seric 	extern ADDRESS *getctladdr();
484452Seric 	char tfrombuf[MAXNAME];		/* translated from person */
494452Seric 	extern char **prescan();
504621Seric 	register ADDRESS *to = firstto;
514863Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
524863Seric 	bool tempfail = FALSE;
535032Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
547052Seric 	bool notopen = TRUE;		/* set if connection not quite open */
55294Seric 
564488Seric 	errno = 0;
577052Seric 	if (bitset(QDONTSEND, to->q_flags))
583233Seric 		return (0);
59294Seric 
606974Seric 	m = to->q_mailer;
616974Seric 	host = to->q_host;
626974Seric 
63294Seric # ifdef DEBUG
64294Seric 	if (Debug)
653233Seric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
666974Seric 			m->m_mno, host, to->q_user);
67294Seric # endif DEBUG
68294Seric 
69294Seric 	/*
705903Seric 	**  If this mailer is expensive, and if we don't want to make
715903Seric 	**  connections now, just mark these addresses and return.
725903Seric 	**	This is useful if we want to batch connections to
735903Seric 	**	reduce load.  This will cause the messages to be
745903Seric 	**	queued up, and a daemon will come along to send the
755903Seric 	**	messages later.
765903Seric 	**		This should be on a per-mailer basis.
775903Seric 	*/
785903Seric 
795903Seric 	if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags))
805903Seric 	{
815903Seric 		for (; to != NULL; to = to->q_next)
825903Seric 			if (!bitset(QDONTSEND, to->q_flags))
835903Seric 				to->q_flags |= QQUEUEUP|QDONTSEND;
845903Seric 		return (0);
855903Seric 	}
865903Seric 
875903Seric 	/*
883233Seric 	**  Do initial argv setup.
893233Seric 	**	Insert the mailer name.  Notice that $x expansion is
903233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
913233Seric 	**	a picky -f flag, we insert it as appropriate.  This
923233Seric 	**	code does not check for 'pv' overflow; this places a
933233Seric 	**	manifest lower limit of 4 for MAXPV.
945903Seric 	**		We rewrite the from address here, being careful
955903Seric 	**		to also rewrite it again using ruleset 2 to
965903Seric 	**		eliminate redundancies.
972968Seric 	*/
982968Seric 
994452Seric 	/* rewrite from address, using rewriting rules */
1006974Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
1014452Seric 	mvp = prescan(buf, '\0');
1024452Seric 	if (mvp == NULL)
1034452Seric 	{
1044452Seric 		syserr("bad mailer from translate \"%s\"", buf);
1054452Seric 		return (EX_SOFTWARE);
1064452Seric 	}
1074452Seric 	rewrite(mvp, 2);
1084452Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
1094452Seric 
1104452Seric 	define('g', tfrombuf);		/* translated sender address */
1113233Seric 	define('h', host);		/* to host */
1123233Seric 	Errors = 0;
1133233Seric 	pvp = pv;
1143233Seric 	*pvp++ = m->m_argv[0];
1152968Seric 
1163233Seric 	/* insert -f or -r flag as appropriate */
1173233Seric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1183233Seric 	{
1193233Seric 		if (bitset(M_FOPT, m->m_flags))
1203233Seric 			*pvp++ = "-f";
1213233Seric 		else
1223233Seric 			*pvp++ = "-r";
1236974Seric 		expand("$g", buf, &buf[sizeof buf - 1], CurEnv);
1243233Seric 		*pvp++ = newstr(buf);
1253233Seric 	}
126294Seric 
127294Seric 	/*
1283233Seric 	**  Append the other fixed parts of the argv.  These run
1293233Seric 	**  up to the first entry containing "$u".  There can only
1303233Seric 	**  be one of these, and there are only a few more slots
1313233Seric 	**  in the pv after it.
132294Seric 	*/
133294Seric 
1343233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
135294Seric 	{
1363233Seric 		while ((p = index(p, '$')) != NULL)
1373233Seric 			if (*++p == 'u')
1383233Seric 				break;
1393233Seric 		if (p != NULL)
1403233Seric 			break;
1413233Seric 
1423233Seric 		/* this entry is safe -- go ahead and process it */
1436974Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
1443233Seric 		*pvp++ = newstr(buf);
1453233Seric 		if (pvp >= &pv[MAXPV - 3])
1463233Seric 		{
1473233Seric 			syserr("Too many parameters to %s before $u", pv[0]);
1483233Seric 			return (-1);
1493233Seric 		}
150294Seric 	}
1514863Seric 
1526038Seric 	/*
1536038Seric 	**  If we have no substitution for the user name in the argument
1546038Seric 	**  list, we know that we must supply the names otherwise -- and
1556038Seric 	**  SMTP is the answer!!
1566038Seric 	*/
1576038Seric 
1583233Seric 	if (*mvp == NULL)
1594863Seric 	{
1604863Seric 		/* running SMTP */
1615179Seric # ifdef SMTP
1624863Seric 		clever = TRUE;
1634863Seric 		*pvp = NULL;
1645179Seric # else SMTP
1656038Seric 		/* oops!  we don't implement SMTP */
1665179Seric 		syserr("SMTP style mailer");
1675179Seric 		return (EX_SOFTWARE);
1685179Seric # endif SMTP
1694863Seric 	}
170294Seric 
171294Seric 	/*
1723233Seric 	**  At this point *mvp points to the argument with $u.  We
1733233Seric 	**  run through our address list and append all the addresses
1743233Seric 	**  we can.  If we run out of space, do not fret!  We can
1753233Seric 	**  always send another copy later.
176294Seric 	*/
177294Seric 
1783233Seric 	tobuf[0] = '\0';
1796900Seric 	CurEnv->e_to = tobuf;
1804397Seric 	ctladdr = NULL;
1813233Seric 	for (; to != NULL; to = to->q_next)
182294Seric 	{
1833233Seric 		/* avoid sending multiple recipients to dumb mailers */
1844382Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1853233Seric 			break;
1863233Seric 
1873233Seric 		/* if already sent or not for this host, don't send */
1887052Seric 		if (bitset(QDONTSEND, to->q_flags) ||
1897052Seric 		    strcmp(to->q_host, host) != 0 ||
1907052Seric 		    to->q_mailer != firstto->q_mailer)
1913233Seric 			continue;
1924397Seric 
1935032Seric # ifdef DEBUG
1945032Seric 		if (Debug)
1955032Seric 		{
1965032Seric 			printf("\nsend to ");
1975032Seric 			printaddr(to, FALSE);
1985032Seric 		}
1995032Seric # endif DEBUG
2005032Seric 
2014397Seric 		/* compute effective uid/gid when sending */
2024596Seric 		if (to->q_mailer == ProgMailer)
2034397Seric 			ctladdr = getctladdr(to);
2044397Seric 
2053233Seric 		user = to->q_user;
2066900Seric 		CurEnv->e_to = to->q_paddr;
2073233Seric 		to->q_flags |= QDONTSEND;
2083233Seric 
2093233Seric 		/*
2103233Seric 		**  Check to see that these people are allowed to
2113233Seric 		**  talk to each other.
2123233Seric 		*/
2133233Seric 
2143233Seric 		if (!checkcompat(to))
215294Seric 		{
2163233Seric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
2173233Seric 			continue;
218294Seric 		}
2193233Seric 
2203233Seric 		/*
2214099Seric 		**  Strip quote bits from names if the mailer is dumb
2224099Seric 		**	about them.
2233233Seric 		*/
2243233Seric 
2253233Seric 		if (bitset(M_STRIPQ, m->m_flags))
226294Seric 		{
2274099Seric 			stripquotes(user, TRUE);
2284099Seric 			stripquotes(host, TRUE);
2293233Seric 		}
2304099Seric 		else
2314099Seric 		{
2324099Seric 			stripquotes(user, FALSE);
2334099Seric 			stripquotes(host, FALSE);
2344099Seric 		}
2353233Seric 
2363233Seric 		/*
2377052Seric 		**  Do initial connection setup if needed.
2387052Seric 		*/
2397052Seric 
2407052Seric 		if (notopen)
2417052Seric 		{
2427052Seric 			message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
2437052Seric # ifdef SMTP
2447052Seric 			if (clever)
2457052Seric 			{
2467052Seric 				/* send the initial SMTP protocol */
2477052Seric 				i = smtpinit(m, pv, (ADDRESS *) NULL);
2487052Seric # ifdef QUEUE
2497052Seric 				if (i == EX_TEMPFAIL)
2507052Seric 					tempfail = TRUE;
2517052Seric # endif QUEUE
2527052Seric 			}
2537052Seric # ifdef SMTP
2547052Seric 			notopen = FALSE;
2557052Seric 		}
2567052Seric 
2577052Seric 		/*
2586059Seric 		**  Pass it to the other host if we are running SMTP.
2596059Seric 		*/
2606059Seric 
2616059Seric 		if (clever)
2626059Seric 		{
2636059Seric # ifdef SMTP
2646059Seric 			i = smtprcpt(to);
2656059Seric 			if (i != EX_OK)
2666059Seric 			{
2676059Seric # ifdef QUEUE
2686059Seric 				if (i == EX_TEMPFAIL)
2696059Seric 					to->q_flags |= QQUEUEUP;
2706059Seric 				else
2716059Seric # endif QUEUE
2726059Seric 					to->q_flags |= QBADADDR;
2737293Seric 				giveresponse(i, TRUE, m);
2746059Seric 			}
2756059Seric # else SMTP
2766059Seric 			syserr("trying to be clever");
2776059Seric # endif SMTP
2786059Seric 		}
2796059Seric 
2806059Seric 		/*
2814161Seric 		**  If an error message has already been given, don't
2824161Seric 		**	bother to send to this address.
2834161Seric 		**
2844161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
2854161Seric 		**	>> NOTE >> cannot do any further aliasing; that
2864161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
2874161Seric 		*/
2884161Seric 
2897293Seric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2904161Seric 			continue;
2914161Seric 
2924283Seric 		/* save statistics.... */
2934596Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2946900Seric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
2954283Seric 
2964161Seric 		/*
2973233Seric 		**  See if this user name is "special".
2983233Seric 		**	If the user name has a slash in it, assume that this
2996974Seric 		**	is a file -- send it off without further ado.  Note
3006974Seric 		**	that this type of addresses is not processed along
3016974Seric 		**	with the others, so we fudge on the To person.
3023233Seric 		*/
3033233Seric 
3044596Seric 		if (m == LocalMailer)
3053233Seric 		{
3065599Seric 			if (user[0] == '/')
307294Seric 			{
3084397Seric 				i = mailfile(user, getctladdr(to));
309294Seric 				giveresponse(i, TRUE, m);
3103233Seric 				continue;
311294Seric 			}
312294Seric 		}
3133233Seric 
3144315Seric 		/*
3154315Seric 		**  Address is verified -- add this user to mailer
3164315Seric 		**  argv, and add it to the print list of recipients.
3174315Seric 		*/
3184315Seric 
3196059Seric 		/* link together the chain of recipients */
3206272Seric 		to->q_tchain = tochain;
3216272Seric 		tochain = to;
3226059Seric 
3233233Seric 		/* create list of users for error messages */
3243233Seric 		if (tobuf[0] != '\0')
3254082Seric 			(void) strcat(tobuf, ",");
3264082Seric 		(void) strcat(tobuf, to->q_paddr);
3273233Seric 		define('u', user);		/* to user */
3284078Seric 		define('z', to->q_home);	/* user's home */
3293233Seric 
3304863Seric 		/*
3316059Seric 		**  Expand out this user into argument list.
3324863Seric 		*/
3334863Seric 
3346059Seric 		if (!clever)
3353233Seric 		{
3366974Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3374863Seric 			*pvp++ = newstr(buf);
3384863Seric 			if (pvp >= &pv[MAXPV - 2])
3394863Seric 			{
3404863Seric 				/* allow some space for trailing parms */
3414863Seric 				break;
3424863Seric 			}
3434863Seric 		}
344294Seric 	}
345294Seric 
3464067Seric 	/* see if any addresses still exist */
3474067Seric 	if (tobuf[0] == '\0')
3484863Seric 	{
3495179Seric # ifdef SMTP
3504863Seric 		if (clever)
3517228Seric 			smtpquit(pv[0], FALSE);
3525179Seric # endif SMTP
3537003Seric 		define('g', (char *) NULL);
3544067Seric 		return (0);
3554863Seric 	}
3564067Seric 
3573233Seric 	/* print out messages as full list */
3586900Seric 	CurEnv->e_to = tobuf;
3593233Seric 
360294Seric 	/*
3613233Seric 	**  Fill out any parameters after the $u parameter.
362294Seric 	*/
363294Seric 
3644863Seric 	while (!clever && *++mvp != NULL)
365294Seric 	{
3666974Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3673233Seric 		*pvp++ = newstr(buf);
3683233Seric 		if (pvp >= &pv[MAXPV])
3693233Seric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
370294Seric 	}
3713233Seric 	*pvp++ = NULL;
372294Seric 
373294Seric 	/*
374294Seric 	**  Call the mailer.
3752898Seric 	**	The argument vector gets built, pipes
376294Seric 	**	are created as necessary, and we fork & exec as
3772898Seric 	**	appropriate.
3784863Seric 	**	If we are running SMTP, we just need to clean up.
379294Seric 	*/
380294Seric 
3814397Seric 	if (ctladdr == NULL)
3826900Seric 		ctladdr = &CurEnv->e_from;
3835179Seric # ifdef SMTP
3844863Seric 	if (clever)
3854863Seric 	{
3866974Seric 		i = smtpfinish(m, CurEnv);
3877228Seric 		smtpquit(pv[0], TRUE);
3884863Seric 	}
3894863Seric 	else
3905179Seric # endif SMTP
3916974Seric 		i = sendoff(m, pv, ctladdr);
3923233Seric 
3934621Seric 	/*
3944621Seric 	**  If we got a temporary failure, arrange to queue the
3954621Seric 	**  addressees.
3964621Seric 	*/
3974621Seric 
3985179Seric # ifdef QUEUE
3994621Seric 	if (i == EX_TEMPFAIL)
4004621Seric 	{
4015032Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
4024621Seric 			to->q_flags |= QQUEUEUP;
4034621Seric 	}
4045179Seric # endif QUEUE
4054621Seric 
4064488Seric 	errno = 0;
4077003Seric 	define('g', (char *) NULL);
4083233Seric 	return (i);
4093233Seric }
4103233Seric /*
4114214Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4124214Seric **
4134214Seric **	This MUST be a macro, since after a vfork we are running
4144214Seric **	two processes on the same stack!!!
4154214Seric **
4164214Seric **	Parameters:
4174214Seric **		none.
4184214Seric **
4194214Seric **	Returns:
4204214Seric **		From a macro???  You've got to be kidding!
4214214Seric **
4224214Seric **	Side Effects:
4234214Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4244214Seric **			pid of child in parent, zero in child.
4254214Seric **			-1 on unrecoverable error.
4264214Seric **
4274214Seric **	Notes:
4284214Seric **		I'm awfully sorry this looks so awful.  That's
4294214Seric **		vfork for you.....
4304214Seric */
4314214Seric 
4324214Seric # define NFORKTRIES	5
4334214Seric # ifdef VFORK
4344214Seric # define XFORK	vfork
4354214Seric # else VFORK
4364214Seric # define XFORK	fork
4374214Seric # endif VFORK
4384214Seric 
4394214Seric # define DOFORK(fORKfN) \
4404214Seric {\
4414214Seric 	register int i;\
4424214Seric \
4434214Seric 	for (i = NFORKTRIES; i-- > 0; )\
4444214Seric 	{\
4454214Seric 		pid = fORKfN();\
4464214Seric 		if (pid >= 0)\
4474214Seric 			break;\
4487003Seric 		sleep(NFORKTRIES - i);\
4494214Seric 	}\
4504214Seric }
4514214Seric /*
4524637Seric **  DOFORK -- simple fork interface to DOFORK.
4534637Seric **
4544637Seric **	Parameters:
4554637Seric **		none.
4564637Seric **
4574637Seric **	Returns:
4584637Seric **		pid of child in parent.
4594637Seric **		zero in child.
4604637Seric **		-1 on error.
4614637Seric **
4624637Seric **	Side Effects:
4634637Seric **		returns twice, once in parent and once in child.
4644637Seric */
4654637Seric 
4664637Seric dofork()
4674637Seric {
4684637Seric 	register int pid;
4694637Seric 
4704637Seric 	DOFORK(fork);
4714637Seric 	return (pid);
4724637Seric }
4734637Seric /*
4743233Seric **  SENDOFF -- send off call to mailer & collect response.
4753233Seric **
4763233Seric **	Parameters:
4773233Seric **		m -- mailer descriptor.
4783233Seric **		pvp -- parameter vector to send to it.
4794397Seric **		ctladdr -- an address pointer controlling the
4804397Seric **			user/groupid etc. of the mailer.
4813233Seric **
4823233Seric **	Returns:
4833233Seric **		exit status of mailer.
4843233Seric **
4853233Seric **	Side Effects:
4863233Seric **		none.
4873233Seric */
4883233Seric 
4896974Seric sendoff(m, pvp, ctladdr)
4903233Seric 	struct mailer *m;
4913233Seric 	char **pvp;
4924397Seric 	ADDRESS *ctladdr;
4933233Seric {
4944863Seric 	auto FILE *mfile;
4954863Seric 	auto FILE *rfile;
4963233Seric 	register int i;
4973233Seric 	int pid;
4984863Seric 
4994863Seric 	/*
5004863Seric 	**  Create connection to mailer.
5014863Seric 	*/
5024863Seric 
5034863Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
5044863Seric 	if (pid < 0)
5054863Seric 		return (-1);
5064863Seric 
5074863Seric 	/*
5084863Seric 	**  Format and send message.
5094863Seric 	*/
5104863Seric 
5114863Seric 	(void) signal(SIGPIPE, SIG_IGN);
5126974Seric 	putfromline(mfile, m);
5136974Seric 	(*CurEnv->e_puthdr)(mfile, m, CurEnv);
5146974Seric 	fprintf(mfile, "\n");
5156974Seric 	(*CurEnv->e_putbody)(mfile, m, FALSE);
5164863Seric 	(void) fclose(mfile);
5174863Seric 
5184863Seric 	i = endmailer(pid, pvp[0]);
5194863Seric 	giveresponse(i, TRUE, m);
5205981Seric 
5215981Seric 	/* arrange a return receipt if requested */
5226900Seric 	if (CurEnv->e_retreceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK)
5235981Seric 	{
5246900Seric 		CurEnv->e_sendreceipt = TRUE;
5256900Seric 		fprintf(Xscript, "%s... successfully delivered\n", CurEnv->e_to);
5265981Seric 		/* do we want to send back more info? */
5275981Seric 	}
5285981Seric 
5294863Seric 	return (i);
5304863Seric }
5314863Seric /*
5324863Seric **  ENDMAILER -- Wait for mailer to terminate.
5334863Seric **
5344863Seric **	We should never get fatal errors (e.g., segmentation
5354863Seric **	violation), so we report those specially.  For other
5364863Seric **	errors, we choose a status message (into statmsg),
5374863Seric **	and if it represents an error, we print it.
5384863Seric **
5394863Seric **	Parameters:
5404863Seric **		pid -- pid of mailer.
5414863Seric **		name -- name of mailer (for error messages).
5424863Seric **
5434863Seric **	Returns:
5444863Seric **		exit code of mailer.
5454863Seric **
5464863Seric **	Side Effects:
5474863Seric **		none.
5484863Seric */
5494863Seric 
5504863Seric endmailer(pid, name)
5514863Seric 	int pid;
5524863Seric 	char *name;
5534863Seric {
5544863Seric 	register int i;
5554863Seric 	auto int st;
5564863Seric 
5576038Seric 	/* in the IPC case there is nothing to wait for */
5586038Seric 	if (pid == 0)
5596038Seric 		return (EX_OK);
5606038Seric 
5616038Seric 	/* wait for the mailer process to die and collect status */
5624863Seric 	while ((i = wait(&st)) > 0 && i != pid)
5634863Seric 		continue;
5644863Seric 	if (i < 0)
5654863Seric 	{
5664863Seric 		syserr("wait");
5674863Seric 		return (-1);
5684863Seric 	}
5696038Seric 
5706038Seric 	/* see if it died a horrid death */
5714863Seric 	if ((st & 0377) != 0)
5724863Seric 	{
5734863Seric 		syserr("%s: stat %o", name, st);
5744863Seric 		ExitStat = EX_UNAVAILABLE;
5754863Seric 		return (-1);
5764863Seric 	}
5776038Seric 
5786038Seric 	/* normal death -- return status */
5794863Seric 	i = (st >> 8) & 0377;
5804863Seric 	return (i);
5814863Seric }
5824863Seric /*
5834863Seric **  OPENMAILER -- open connection to mailer.
5844863Seric **
5854863Seric **	Parameters:
5864863Seric **		m -- mailer descriptor.
5874863Seric **		pvp -- parameter vector to pass to mailer.
5884863Seric **		ctladdr -- controlling address for user.
5894863Seric **		clever -- create a full duplex connection.
5904863Seric **		pmfile -- pointer to mfile (to mailer) connection.
5914863Seric **		prfile -- pointer to rfile (from mailer) connection.
5924863Seric **
5934863Seric **	Returns:
5946038Seric **		pid of mailer ( > 0 ).
5954863Seric **		-1 on error.
5966038Seric **		zero on an IPC connection.
5974863Seric **
5984863Seric **	Side Effects:
5994863Seric **		creates a mailer in a subprocess.
6004863Seric */
6014863Seric 
6024863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
6034863Seric 	struct mailer *m;
6044863Seric 	char **pvp;
6054863Seric 	ADDRESS *ctladdr;
6064863Seric 	bool clever;
6074863Seric 	FILE **pmfile;
6084863Seric 	FILE **prfile;
6094863Seric {
6104863Seric 	int pid;
6114709Seric 	int mpvect[2];
6124863Seric 	int rpvect[2];
6133233Seric 	FILE *mfile;
6144863Seric 	FILE *rfile;
6153233Seric 	extern FILE *fdopen();
6163233Seric 
6173233Seric # ifdef DEBUG
6183233Seric 	if (Debug)
619294Seric 	{
6204863Seric 		printf("openmailer:\n");
6213233Seric 		printav(pvp);
622294Seric 	}
6233233Seric # endif DEBUG
6244488Seric 	errno = 0;
6253233Seric 
6266038Seric # ifdef DAEMON
6276038Seric 	/*
6286038Seric 	**  Deal with the special case of mail handled through an IPC
6296038Seric 	**  connection.
6306038Seric 	**	In this case we don't actually fork.  We must be
6316038Seric 	**	running SMTP for this to work.  We will return a
6326038Seric 	**	zero pid to indicate that we are running IPC.
6336038Seric 	*/
6346038Seric 
6356038Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
6366038Seric 	{
6376038Seric 		register int i;
6387285Seric 		register u_short port;
6396038Seric 
6406038Seric 		if (!clever)
6416038Seric 			syserr("non-clever IPC");
6426632Seric 		if (pvp[2] != NULL)
6437285Seric 			port = atoi(pvp[2]);
6446632Seric 		else
6457285Seric 			port = 0;
6467285Seric 		i = makeconnection(pvp[1], port, pmfile, prfile);
6476038Seric 		if (i != EX_OK)
6486047Seric 		{
6496047Seric 			ExitStat = i;
6506038Seric 			return (-1);
6516047Seric 		}
6526038Seric 		else
6536038Seric 			return (0);
6546038Seric 	}
6556038Seric # endif DAEMON
6566038Seric 
6572898Seric 	/* create a pipe to shove the mail through */
6584709Seric 	if (pipe(mpvect) < 0)
659294Seric 	{
6604863Seric 		syserr("pipe (to mailer)");
661294Seric 		return (-1);
662294Seric 	}
6634863Seric 
6645179Seric # ifdef SMTP
6654863Seric 	/* if this mailer speaks smtp, create a return pipe */
6664863Seric 	if (clever && pipe(rpvect) < 0)
6674863Seric 	{
6684863Seric 		syserr("pipe (from mailer)");
6694863Seric 		(void) close(mpvect[0]);
6704863Seric 		(void) close(mpvect[1]);
6714863Seric 		return (-1);
6724863Seric 	}
6735179Seric # endif SMTP
6744863Seric 
6756038Seric 	/*
6766038Seric 	**  Actually fork the mailer process.
6776038Seric 	**	DOFORK is clever about retrying.
6786038Seric 	*/
6796038Seric 
6807371Seric 	fflush(Xscript);				/* for debugging */
6814214Seric 	DOFORK(XFORK);
6824327Seric 	/* pid is set by DOFORK */
683294Seric 	if (pid < 0)
684294Seric 	{
6856038Seric 		/* failure */
686294Seric 		syserr("Cannot fork");
6874709Seric 		(void) close(mpvect[0]);
6884709Seric 		(void) close(mpvect[1]);
6894863Seric 		if (clever)
6904863Seric 		{
6914863Seric 			(void) close(rpvect[0]);
6924863Seric 			(void) close(rpvect[1]);
6934863Seric 		}
694294Seric 		return (-1);
695294Seric 	}
696294Seric 	else if (pid == 0)
697294Seric 	{
698294Seric 		/* child -- set up input & exec mailer */
6991621Seric 		/* make diagnostic output be standard output */
7004477Seric 		(void) signal(SIGINT, SIG_IGN);
7014477Seric 		(void) signal(SIGHUP, SIG_IGN);
7024215Seric 		(void) signal(SIGTERM, SIG_DFL);
7034709Seric 
7044709Seric 		/* arrange to filter standard & diag output of command */
7054863Seric 		if (clever)
7064709Seric 		{
7074863Seric 			(void) close(rpvect[0]);
7084709Seric 			(void) close(1);
7094863Seric 			(void) dup(rpvect[1]);
7104863Seric 			(void) close(rpvect[1]);
7114863Seric 		}
7124863Seric 		else if (OutChannel != stdout)
7134863Seric 		{
7144863Seric 			(void) close(1);
7154709Seric 			(void) dup(fileno(OutChannel));
7164709Seric 		}
7174082Seric 		(void) close(2);
7184082Seric 		(void) dup(1);
7194709Seric 
7204709Seric 		/* arrange to get standard input */
7214709Seric 		(void) close(mpvect[1]);
7224082Seric 		(void) close(0);
7234709Seric 		if (dup(mpvect[0]) < 0)
724294Seric 		{
7252898Seric 			syserr("Cannot dup to zero!");
7262898Seric 			_exit(EX_OSERR);
727294Seric 		}
7284709Seric 		(void) close(mpvect[0]);
7292968Seric 		if (!bitset(M_RESTR, m->m_flags))
7304215Seric 		{
7314417Seric 			if (ctladdr->q_uid == 0)
7324417Seric 			{
7334417Seric 				(void) setgid(DefGid);
7344417Seric 				(void) setuid(DefUid);
7354417Seric 			}
7364417Seric 			else
7374415Seric 			{
7384417Seric 				(void) setgid(ctladdr->q_gid);
7394417Seric 				(void) setuid(ctladdr->q_uid);
7404415Seric 			}
7414215Seric 		}
7422774Seric # ifndef VFORK
7432774Seric 		/*
7442774Seric 		**  We have to be careful with vfork - we can't mung up the
7452774Seric 		**  memory but we don't want the mailer to inherit any extra
7462774Seric 		**  open files.  Chances are the mailer won't
7472774Seric 		**  care about an extra file, but then again you never know.
7482774Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
7492774Seric 		**  declared static so we can't.  But if we fclose(pwf), which
7502774Seric 		**  is what endpwent does, it closes it in the parent too and
7512774Seric 		**  the next getpwnam will be slower.  If you have a weird
7522774Seric 		**  mailer that chokes on the extra file you should do the
7532774Seric 		**  endpwent().
7542774Seric 		**
7552774Seric 		**  Similar comments apply to log.  However, openlog is
7562774Seric 		**  clever enough to set the FIOCLEX mode on the file,
7572774Seric 		**  so it will be closed automatically on the exec.
7582774Seric 		*/
7592774Seric 
7602774Seric 		endpwent();
761294Seric # ifdef LOG
7622089Seric 		closelog();
763294Seric # endif LOG
7642774Seric # endif VFORK
7656038Seric 
7666038Seric 		/* try to execute the mailer */
767294Seric 		execv(m->m_mailer, pvp);
7686038Seric 
769294Seric 		/* syserr fails because log is closed */
770294Seric 		/* syserr("Cannot exec %s", m->m_mailer); */
7714214Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
7724082Seric 		(void) fflush(stdout);
7731619Seric 		_exit(EX_UNAVAILABLE);
774294Seric 	}
775294Seric 
7764709Seric 	/*
7774863Seric 	**  Set up return value.
7784709Seric 	*/
7794709Seric 
7804709Seric 	(void) close(mpvect[0]);
7814709Seric 	mfile = fdopen(mpvect[1], "w");
7824863Seric 	if (clever)
7834863Seric 	{
7844863Seric 		(void) close(rpvect[1]);
7854863Seric 		rfile = fdopen(rpvect[0], "r");
7864863Seric 	}
787294Seric 
7884863Seric 	*pmfile = mfile;
7894863Seric 	*prfile = rfile;
790294Seric 
7914863Seric 	return (pid);
792294Seric }
793294Seric /*
794294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
795294Seric **
796294Seric **	Parameters:
797294Seric **		stat -- the status code from the mailer (high byte
798294Seric **			only; core dumps must have been taken care of
799294Seric **			already).
800294Seric **		force -- if set, force an error message output, even
801294Seric **			if the mailer seems to like to print its own
802294Seric **			messages.
803294Seric **		m -- the mailer descriptor for this mailer.
804294Seric **
805294Seric **	Returns:
8064082Seric **		none.
807294Seric **
808294Seric **	Side Effects:
8091518Seric **		Errors may be incremented.
810294Seric **		ExitStat may be set.
811294Seric */
812294Seric 
813294Seric giveresponse(stat, force, m)
814294Seric 	int stat;
8157228Seric 	bool force;
816294Seric 	register struct mailer *m;
817294Seric {
818294Seric 	register char *statmsg;
819294Seric 	extern char *SysExMsg[];
820294Seric 	register int i;
821294Seric 	extern int N_SysEx;
8221624Seric 	char buf[30];
823294Seric 
8244315Seric 	/*
8254315Seric 	**  Compute status message from code.
8264315Seric 	*/
8274315Seric 
828294Seric 	i = stat - EX__BASE;
829294Seric 	if (i < 0 || i > N_SysEx)
830294Seric 		statmsg = NULL;
831294Seric 	else
832294Seric 		statmsg = SysExMsg[i];
833294Seric 	if (stat == 0)
8344065Seric 	{
8354194Seric 		if (bitset(M_LOCAL, m->m_flags))
8364161Seric 			statmsg = "delivered";
8374161Seric 		else
8384161Seric 			statmsg = "queued";
8397052Seric 		message(Arpa_Info, statmsg);
8404065Seric 	}
8415179Seric # ifdef QUEUE
8424621Seric 	else if (stat == EX_TEMPFAIL)
8434621Seric 	{
8447052Seric 		message(Arpa_Info, "transmission deferred");
8454621Seric 	}
8465179Seric # endif QUEUE
847294Seric 	else
848294Seric 	{
8491518Seric 		Errors++;
8506047Seric 		FatalErrors = TRUE;
851294Seric 		if (statmsg == NULL && m->m_badstat != 0)
852294Seric 		{
853294Seric 			stat = m->m_badstat;
854294Seric 			i = stat - EX__BASE;
855294Seric # ifdef DEBUG
856294Seric 			if (i < 0 || i >= N_SysEx)
857294Seric 				syserr("Bad m_badstat %d", stat);
858294Seric 			else
859294Seric # endif DEBUG
8607344Seric 				statmsg = SysExMsg[i];
861294Seric 		}
862294Seric 		if (statmsg == NULL)
863294Seric 			usrerr("unknown mailer response %d", stat);
8644065Seric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
865294Seric 			usrerr("%s", statmsg);
8667344Seric 		else
8677344Seric 			fprintf(Xscript, "%s\n", statmsg);
868294Seric 	}
869294Seric 
870294Seric 	/*
871294Seric 	**  Final cleanup.
872294Seric 	**	Log a record of the transaction.  Compute the new
873294Seric 	**	ExitStat -- if we already had an error, stick with
874294Seric 	**	that.
875294Seric 	*/
876294Seric 
8771624Seric 	if (statmsg == NULL)
8781624Seric 	{
8794082Seric 		(void) sprintf(buf, "error %d", stat);
8801624Seric 		statmsg = buf;
8811624Seric 	}
8821624Seric 
883294Seric # ifdef LOG
8846900Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", CurEnv->e_from.q_paddr, CurEnv->e_to, CurEnv->e_msgsize, statmsg);
885294Seric # endif LOG
8865179Seric # ifdef QUEUE
8874621Seric 	if (stat != EX_TEMPFAIL)
8885179Seric # endif QUEUE
8894621Seric 		setstat(stat);
890294Seric }
891294Seric /*
8926974Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
893294Seric **
8946974Seric **	This can be made an arbitrary message separator by changing $l
895294Seric **
8966974Seric **	One of the ugliest hacks seen by human eyes is
8976974Seric **	contained herein: UUCP wants those stupid
8986974Seric **	"remote from <host>" lines.  Why oh why does a
8996974Seric **	well-meaning programmer such as myself have to
9006974Seric **	deal with this kind of antique garbage????
9016974Seric **
902294Seric **	Parameters:
9036974Seric **		fp -- the file to output to.
9046974Seric **		m -- the mailer describing this entry.
905294Seric **
906294Seric **	Returns:
9076974Seric **		none
908294Seric **
909294Seric **	Side Effects:
9106974Seric **		outputs some text to fp.
911294Seric */
912294Seric 
9136974Seric putfromline(fp, m)
9146974Seric 	register FILE *fp;
9156974Seric 	register MAILER *m;
916294Seric {
9176974Seric 	char buf[MAXLINE];
918294Seric 
9196974Seric 	if (bitset(M_NHDR, m->m_flags))
9206974Seric 		return;
9214315Seric 
9226974Seric # ifdef UGLYUUCP
9236974Seric 	if (bitset(M_UGLYUUCP, m->m_flags))
9244205Seric 	{
9256974Seric 		extern char *macvalue();
9266974Seric 		char *sys = macvalue('g');
9276974Seric 		char *bang = index(sys, '!');
9286041Seric 
9296974Seric 		if (bang == NULL)
9306974Seric 			syserr("No ! in UUCP! (%s)", sys);
9315099Seric 		else
9326974Seric 			*bang = '\0';
9337232Seric 		expand("From $f  $d remote from $g\n", buf,
9346974Seric 				&buf[sizeof buf - 1], CurEnv);
9356974Seric 		*bang = '!';
9366974Seric 	}
9376974Seric 	else
9385179Seric # endif UGLYUUCP
9396974Seric 		expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv);
9407123Seric 	putline(buf, fp, bitset(M_FULLSMTP, m->m_flags));
9415981Seric }
9425981Seric /*
9436974Seric **  PUTHEADER -- put the header part of a message from the in-core copy
9445981Seric **
9455981Seric **	Parameters:
9465981Seric **		fp -- file to put it on.
9475981Seric **		m -- mailer to use.
9486974Seric **		e -- envelope to use.
9495981Seric **
9505981Seric **	Returns:
9515981Seric **		none.
9525981Seric **
9535981Seric **	Side Effects:
9545981Seric **		none.
9555981Seric */
9565981Seric 
9576974Seric putheader(fp, m, e)
9585981Seric 	register FILE *fp;
9595981Seric 	register struct mailer *m;
9606974Seric 	register ENVELOPE *e;
9615981Seric {
9625981Seric 	char buf[BUFSIZ];
9635981Seric 	register HDR *h;
9645981Seric 	extern char *arpadate();
9655981Seric 	extern char *capitalize();
9665981Seric 	extern char *hvalue();
9675981Seric 	extern bool samefrom();
9685981Seric 	char *of_line;
9697123Seric 	char obuf[MAXLINE];
9707123Seric 	register char *obp;
9717123Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
9725981Seric 
9734447Seric 	of_line = hvalue("original-from");
9746974Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
9751828Seric 	{
9764315Seric 		register char *p;
9776974Seric 		char *origfrom = e->e_origfrom;
9784447Seric 		bool nooutput;
9794315Seric 
9804447Seric 		nooutput = FALSE;
9813389Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
9824447Seric 			nooutput = TRUE;
9834447Seric 
9844447Seric 		/* use From: line from message if generated is the same */
9854370Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
9864447Seric 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
9873385Seric 		{
9884370Seric 			p = origfrom;
9894370Seric 			origfrom = NULL;
9904370Seric 		}
9914370Seric 		else if (bitset(H_DEFAULT, h->h_flags))
9924370Seric 		{
993*7666Seric 			/* macro expand value if generated internally */
9946974Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
9953385Seric 			p = buf;
9963385Seric 		}
9975913Seric 		else if (bitset(H_ADDR, h->h_flags))
9985913Seric 		{
9995913Seric 			register int opos;
10005913Seric 			bool firstone = TRUE;
10015913Seric 
10025913Seric 			/*
10035913Seric 			**  Output the address list translated by the
10045913Seric 			**  mailer and with commas.
10055913Seric 			*/
10065913Seric 
10075913Seric 			p = h->h_value;
10085913Seric 			if (p == NULL || *p == '\0' || nooutput)
10095913Seric 				continue;
10107123Seric 			obp = obuf;
10117285Seric 			(void) sprintf(obp, "%s: ", capitalize(h->h_field));
10125913Seric 			opos = strlen(h->h_field) + 2;
10137123Seric 			obp += opos;
1014*7666Seric 
1015*7666Seric 			/*
1016*7666Seric 			**  Run through the list of values.
1017*7666Seric 			*/
1018*7666Seric 
10195913Seric 			while (*p != '\0')
10205913Seric 			{
1021*7666Seric 				register char *name;
10225913Seric 				extern char *remotename();
10235913Seric 				char savechar;
10245913Seric 
1025*7666Seric 				/*
1026*7666Seric 				**  Find the end of the name.  New style names
1027*7666Seric 				**  end with a comma, old style names end with
1028*7666Seric 				**  a space character.  However, spaces do not
1029*7666Seric 				**  necessarily delimit an old-style name -- at
1030*7666Seric 				**  signs mean keep going.
1031*7666Seric 				*/
1032*7666Seric 
1033*7666Seric 				/* clean up the leading trash in source */
1034*7666Seric 				while (*p != '\0' && (isspace(*p) || *p == ','))
1035*7666Seric 					p++;
1036*7666Seric 				name = p;
1037*7666Seric 
1038*7666Seric 				/* find end of name */
10395936Seric 				while (*p != '\0' && *p != ',')
10405936Seric 				{
10415936Seric 					extern bool isatword();
10425936Seric 					char *oldp;
10435936Seric 
10446974Seric 					if (!e->e_oldstyle || !isspace(*p))
10455936Seric 					{
10465936Seric 						p++;
10475936Seric 						continue;
10485936Seric 					}
1049*7666Seric 
1050*7666Seric 					/* look to see if we have an at sign */
10515936Seric 					oldp = p;
10525936Seric 					while (*p != '\0' && isspace(*p))
10535936Seric 						p++;
1054*7666Seric 
10555936Seric 					if (*p != '@' && !isatword(p))
10565936Seric 					{
10575936Seric 						p = oldp;
10585936Seric 						break;
10595936Seric 					}
10605936Seric 					p += *p == '@' ? 1 : 2;
10615936Seric 					while (*p != '\0' && isspace(*p))
10625936Seric 						p++;
10635936Seric 				}
1064*7666Seric 				/* at the end of one complete name */
1065*7666Seric 
1066*7666Seric 				/* strip off trailing white space */
1067*7666Seric 				while (p >= name && (isspace(*p) || *p == ','))
1068*7666Seric 					p--;
1069*7666Seric 				if (++p == name)
1070*7666Seric 					continue;
10715913Seric 				savechar = *p;
10725913Seric 				*p = '\0';
10735913Seric 
10745913Seric 				/* translate the name to be relative */
10756820Seric 				name = remotename(name, m, FALSE);
10765913Seric 				if (*name == '\0')
1077*7666Seric 				{
1078*7666Seric 					*p = savechar;
10795913Seric 					continue;
1080*7666Seric 				}
10815913Seric 
10825913Seric 				/* output the name with nice formatting */
10835913Seric 				opos += strlen(name);
10845913Seric 				if (!firstone)
10855913Seric 					opos += 2;
10865913Seric 				if (opos > 78 && !firstone)
10875913Seric 				{
10887123Seric 					(void) sprintf(obp, ",\n");
10897123Seric 					putline(obuf, fp, fullsmtp);
10907123Seric 					obp = obuf;
10917123Seric 					(void) sprintf(obp, "        ");
10927123Seric 					obp += strlen(obp);
10935916Seric 					opos = 8 + strlen(name);
10945913Seric 				}
10955913Seric 				else if (!firstone)
10967123Seric 				{
10977123Seric 					(void) sprintf(obp, ", ");
10987123Seric 					obp += 2;
10997123Seric 				}
11007123Seric 				(void) sprintf(obp, "%s", name);
11017123Seric 				obp += strlen(obp);
11025913Seric 				firstone = FALSE;
11035913Seric 				*p = savechar;
11045913Seric 			}
11057285Seric 			(void) strcpy(obp, "\n");
11067202Seric 			putline(obuf, fp, fullsmtp);
11075913Seric 			nooutput = TRUE;
11085913Seric 		}
11092898Seric 		else
11103385Seric 			p = h->h_value;
11114447Seric 		if (p == NULL || *p == '\0')
11123389Seric 			continue;
11134209Seric 
11144209Seric 		/* hack, hack -- output Original-From field if different */
11154447Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
11164209Seric 		{
11174447Seric 			/* output new Original-From line if needed */
11184447Seric 			if (of_line == NULL && !samefrom(p, origfrom))
11197123Seric 			{
11207123Seric 				(void) sprintf(obuf, "Original-From: %s\n", origfrom);
11217123Seric 				putline(obuf, fp, fullsmtp);
11227123Seric 			}
11234447Seric 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
11244447Seric 			{
11254447Seric 				/* delete Original-From: line if redundant */
11264447Seric 				p = of_line;
11274447Seric 				of_line = NULL;
11284447Seric 			}
11294447Seric 		}
11304447Seric 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
11314447Seric 			nooutput = TRUE;
11324447Seric 
11334447Seric 		/* finally, output the header line */
11344447Seric 		if (!nooutput)
11354447Seric 		{
11367123Seric 			(void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p);
11377123Seric 			putline(obuf, fp, fullsmtp);
11384447Seric 			h->h_flags |= H_USED;
11394209Seric 		}
11402898Seric 	}
1141294Seric }
1142294Seric /*
11436974Seric **  PUTBODY -- put the body of a message.
11446974Seric **
11456974Seric **	Parameters:
11466974Seric **		fp -- file to output onto.
11476974Seric **		m -- a mailer descriptor.
11486974Seric **		xdot -- if set, use SMTP hidden dot algorithm.
11496974Seric **
11506974Seric **	Returns:
11516974Seric **		none.
11526974Seric **
11536974Seric **	Side Effects:
11546974Seric **		The message is written onto fp.
11556974Seric */
11566974Seric 
11576974Seric putbody(fp, m, xdot)
11586974Seric 	FILE *fp;
11596974Seric 	struct mailer *m;
11606974Seric 	bool xdot;
11616974Seric {
11626974Seric 	char buf[MAXLINE + 1];
11637123Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
11646974Seric 
11656974Seric 	/*
11666974Seric 	**  Output the body of the message
11676974Seric 	*/
11686974Seric 
11697003Seric #ifdef lint
11707003Seric 	/* m will be needed later for complete smtp emulation */
11717003Seric 	if (m == NULL)
11727003Seric 		return;
11737003Seric #endif lint
11747003Seric 
11756974Seric 	if (TempFile != NULL)
11766974Seric 	{
11776974Seric 		rewind(TempFile);
11786974Seric 		buf[0] = '.';
11796974Seric 		while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL)
11807123Seric 			putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp);
11816974Seric 
11826974Seric 		if (ferror(TempFile))
11836974Seric 		{
11846974Seric 			syserr("putbody: read error");
11856974Seric 			ExitStat = EX_IOERR;
11866974Seric 		}
11876974Seric 	}
11886974Seric 
11896974Seric 	(void) fflush(fp);
11906974Seric 	if (ferror(fp) && errno != EPIPE)
11916974Seric 	{
11926974Seric 		syserr("putbody: write error");
11936974Seric 		ExitStat = EX_IOERR;
11946974Seric 	}
11956974Seric 	errno = 0;
11966974Seric }
11976974Seric /*
11985936Seric **  ISATWORD -- tell if the word we are pointing to is "at".
11995936Seric **
12005936Seric **	Parameters:
12015936Seric **		p -- word to check.
12025936Seric **
12035936Seric **	Returns:
12045936Seric **		TRUE -- if p is the word at.
12055936Seric **		FALSE -- otherwise.
12065936Seric **
12075936Seric **	Side Effects:
12085936Seric **		none.
12095936Seric */
12105936Seric 
12115936Seric bool
12125936Seric isatword(p)
12135936Seric 	register char *p;
12145936Seric {
12155936Seric 	extern char lower();
12165936Seric 
12175936Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
12185936Seric 	    p[2] != '\0' && isspace(p[2]))
12195936Seric 		return (TRUE);
12205936Seric 	return (FALSE);
12215936Seric }
12225936Seric /*
12235913Seric **  REMOTENAME -- return the name relative to the current mailer
12245913Seric **
12255913Seric **	Parameters:
12265913Seric **		name -- the name to translate.
12276820Seric **		force -- if set, forces rewriting even if the mailer
12286820Seric **			does not request it.  Used for rewriting
12296820Seric **			sender addresses.
12305913Seric **
12315913Seric **	Returns:
12325913Seric **		the text string representing this address relative to
12335913Seric **			the receiving mailer.
12345913Seric **
12355913Seric **	Side Effects:
12365913Seric **		none.
12375913Seric **
12385913Seric **	Warnings:
12395913Seric **		The text string returned is tucked away locally;
12405913Seric **			copy it if you intend to save it.
12415913Seric */
12425913Seric 
12435913Seric char *
12446820Seric remotename(name, m, force)
12455913Seric 	char *name;
12465916Seric 	struct mailer *m;
12476820Seric 	bool force;
12485913Seric {
12495913Seric 	static char buf[MAXNAME];
12505913Seric 	char lbuf[MAXNAME];
12515913Seric 	extern char *macvalue();
12525913Seric 	char *oldf = macvalue('f');
12535916Seric 	char *oldx = macvalue('x');
12545916Seric 	char *oldg = macvalue('g');
12555913Seric 	extern char **prescan();
12565913Seric 	register char **pvp;
12575916Seric 	extern char *getxpart();
12587003Seric 	extern ADDRESS *buildaddr();
12595913Seric 
12605913Seric 	/*
12616820Seric 	**  See if this mailer wants the name to be rewritten.  There are
12626820Seric 	**  many problems here, owing to the standards for doing replies.
12636820Seric 	**  In general, these names should only be rewritten if we are
12646820Seric 	**  sending to another host that runs sendmail.
12656820Seric 	*/
12666820Seric 
12676820Seric 	if (!bitset(M_RELRCPT, m->m_flags) && !force)
12687003Seric 		return (name);
12696820Seric 
12706820Seric 	/*
12715913Seric 	**  Do general rewriting of name.
12725913Seric 	**	This will also take care of doing global name translation.
12735913Seric 	*/
12745913Seric 
12755916Seric 	define('x', getxpart(name));
12765913Seric 	pvp = prescan(name, '\0');
12777259Seric 	if (pvp == NULL)
12787259Seric 		return (name);
12797048Seric 	rewrite(pvp, 1);
12807048Seric 	rewrite(pvp, 3);
12817048Seric 	if (**pvp == CANONNET)
12825913Seric 	{
12837048Seric 		/* oops... resolved to something */
12847048Seric 		return (name);
12855913Seric 	}
12867048Seric 	cataddr(pvp, lbuf, sizeof lbuf);
12875913Seric 
12885913Seric 	/* make the name relative to the receiving mailer */
12895913Seric 	define('f', lbuf);
12906974Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
12915913Seric 
12925913Seric 	/* rewrite to get rid of garbage we added in the expand above */
12935913Seric 	pvp = prescan(buf, '\0');
12947259Seric 	if (pvp == NULL)
12957259Seric 		return (name);
12965913Seric 	rewrite(pvp, 2);
12975916Seric 	cataddr(pvp, lbuf, sizeof lbuf);
12985913Seric 
12995916Seric 	/* now add any comment info we had before back */
13005916Seric 	define('g', lbuf);
13016974Seric 	expand("$q", buf, &buf[sizeof buf - 1], CurEnv);
13025916Seric 
13035913Seric 	define('f', oldf);
13045916Seric 	define('g', oldg);
13055916Seric 	define('x', oldx);
13065913Seric 
13075913Seric # ifdef DEBUG
13085913Seric 	if (Debug > 0)
13095913Seric 		printf("remotename(%s) => `%s'\n", name, buf);
13105913Seric # endif DEBUG
13115913Seric 	return (buf);
13125913Seric }
13135913Seric /*
13144370Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
13154370Seric **
13164370Seric **	Parameters:
13174370Seric **		ifrom -- internally generated form of from address.
13184370Seric **		efrom -- external form of from address.
13194370Seric **
13204370Seric **	Returns:
13214370Seric **		TRUE -- if they convey the same info.
13224370Seric **		FALSE -- if any information has been lost.
13234370Seric **
13244370Seric **	Side Effects:
13254370Seric **		none.
13264370Seric */
13274370Seric 
13284370Seric bool
13294370Seric samefrom(ifrom, efrom)
13304370Seric 	char *ifrom;
13314370Seric 	char *efrom;
13324370Seric {
13334447Seric 	register char *p;
13344447Seric 	char buf[MAXNAME + 4];
13354447Seric 
13364447Seric # ifdef DEBUG
13374447Seric 	if (Debug > 7)
13384447Seric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
13394447Seric # endif DEBUG
13404447Seric 	if (strcmp(ifrom, efrom) == 0)
13414447Seric 		goto success;
13424447Seric 	p = index(ifrom, '@');
13434447Seric 	if (p == NULL)
13444447Seric 		goto failure;
13454447Seric 	*p = '\0';
13467003Seric 	(void) strcpy(buf, ifrom);
13477003Seric 	(void) strcat(buf, " at ");
13484447Seric 	*p++ = '@';
13497003Seric 	(void) strcat(buf, p);
13504447Seric 	if (strcmp(buf, efrom) == 0)
13514447Seric 		goto success;
13524447Seric 
13534447Seric   failure:
13544447Seric # ifdef DEBUG
13554447Seric 	if (Debug > 7)
13564447Seric 		printf("FALSE\n");
13574447Seric # endif DEBUG
13584447Seric 	return (FALSE);
13594447Seric 
13604447Seric   success:
13614447Seric # ifdef DEBUG
13624447Seric 	if (Debug > 7)
13634447Seric 		printf("TRUE\n");
13644447Seric # endif DEBUG
13654447Seric 	return (TRUE);
13664370Seric }
13674370Seric /*
1368294Seric **  MAILFILE -- Send a message to a file.
1369294Seric **
13704327Seric **	If the file has the setuid/setgid bits set, but NO execute
13714327Seric **	bits, sendmail will try to become the owner of that file
13724327Seric **	rather than the real user.  Obviously, this only works if
13734327Seric **	sendmail runs as root.
13744327Seric **
1375294Seric **	Parameters:
1376294Seric **		filename -- the name of the file to send to.
13774397Seric **		ctladdr -- the controlling address header -- includes
13784397Seric **			the userid/groupid to be when sending.
1379294Seric **
1380294Seric **	Returns:
1381294Seric **		The exit code associated with the operation.
1382294Seric **
1383294Seric **	Side Effects:
1384294Seric **		none.
1385294Seric */
1386294Seric 
13874397Seric mailfile(filename, ctladdr)
1388294Seric 	char *filename;
13894397Seric 	ADDRESS *ctladdr;
1390294Seric {
1391294Seric 	register FILE *f;
13924214Seric 	register int pid;
1393294Seric 
13944214Seric 	/*
13954214Seric 	**  Fork so we can change permissions here.
13964214Seric 	**	Note that we MUST use fork, not vfork, because of
13974214Seric 	**	the complications of calling subroutines, etc.
13984214Seric 	*/
13994067Seric 
14004214Seric 	DOFORK(fork);
14014214Seric 
14024214Seric 	if (pid < 0)
14034214Seric 		return (EX_OSERR);
14044214Seric 	else if (pid == 0)
14054214Seric 	{
14064214Seric 		/* child -- actually write to file */
14074327Seric 		struct stat stb;
14084327Seric 
14094215Seric 		(void) signal(SIGINT, SIG_DFL);
14104215Seric 		(void) signal(SIGHUP, SIG_DFL);
14114215Seric 		(void) signal(SIGTERM, SIG_DFL);
14124327Seric 		umask(OldUmask);
14134327Seric 		if (stat(filename, &stb) < 0)
14144431Seric 			stb.st_mode = 0666;
14154327Seric 		if (bitset(0111, stb.st_mode))
14164327Seric 			exit(EX_CANTCREAT);
14174401Seric 		if (ctladdr == NULL)
14186900Seric 			ctladdr = &CurEnv->e_from;
14194327Seric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
14204417Seric 		{
14214417Seric 			if (ctladdr->q_uid == 0)
14224417Seric 				(void) setgid(DefGid);
14234417Seric 			else
14244417Seric 				(void) setgid(ctladdr->q_gid);
14254417Seric 		}
14264327Seric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
14274417Seric 		{
14284417Seric 			if (ctladdr->q_uid == 0)
14294417Seric 				(void) setuid(DefUid);
14304417Seric 			else
14314417Seric 				(void) setuid(ctladdr->q_uid);
14324417Seric 		}
14336887Seric 		f = dfopen(filename, "a");
14344214Seric 		if (f == NULL)
14354214Seric 			exit(EX_CANTCREAT);
14364214Seric 
14376974Seric 		putfromline(f, Mailer[1]);
14386974Seric 		(*CurEnv->e_puthdr)(f, Mailer[1], CurEnv);
14397123Seric 		fputs("\n", f);
14406974Seric 		(*CurEnv->e_putbody)(f, Mailer[1], FALSE);
14414214Seric 		fputs("\n", f);
14424214Seric 		(void) fclose(f);
14434214Seric 		(void) fflush(stdout);
14444417Seric 
14456887Seric 		/* reset ISUID & ISGID bits for paranoid systems */
14464621Seric 		(void) chmod(filename, (int) stb.st_mode);
14474214Seric 		exit(EX_OK);
14484315Seric 		/*NOTREACHED*/
14494214Seric 	}
14504214Seric 	else
14514214Seric 	{
14524214Seric 		/* parent -- wait for exit status */
14534214Seric 		register int i;
14544214Seric 		auto int stat;
14554214Seric 
14564214Seric 		while ((i = wait(&stat)) != pid)
14574214Seric 		{
14584214Seric 			if (i < 0)
14594214Seric 			{
14604214Seric 				stat = EX_OSERR << 8;
14614214Seric 				break;
14624214Seric 			}
14634214Seric 		}
14644215Seric 		if ((stat & 0377) != 0)
14654215Seric 			stat = EX_UNAVAILABLE << 8;
14664214Seric 		return ((stat >> 8) & 0377);
14674214Seric 	}
1468294Seric }
14694550Seric /*
14704550Seric **  SENDALL -- actually send all the messages.
14714550Seric **
14724550Seric **	Parameters:
14737043Seric **		e -- the envelope to send.
14744550Seric **		verifyonly -- if set, only give verification messages.
14754550Seric **
14764550Seric **	Returns:
14774550Seric **		none.
14784550Seric **
14794550Seric **	Side Effects:
14804550Seric **		Scans the send lists and sends everything it finds.
14817043Seric **		Delivers any appropriate error messages.
14824550Seric */
14834550Seric 
14847043Seric sendall(e, verifyonly)
14857043Seric 	ENVELOPE *e;
14864550Seric 	bool verifyonly;
14874550Seric {
14885008Seric 	register ADDRESS *q;
14894550Seric 
14905032Seric # ifdef DEBUG
14915032Seric 	if (Debug > 1)
14925032Seric 	{
14936900Seric 		printf("\nSend Queue:\n");
14947043Seric 		printaddr(e->e_sendqueue, TRUE);
14955032Seric 	}
14965032Seric # endif DEBUG
14975008Seric 
14987043Seric 	/*
14997043Seric 	**  Run through the list and send everything.
15007043Seric 	*/
15017043Seric 
15027043Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15034550Seric 	{
15045008Seric 		if (verifyonly)
15054550Seric 		{
15066900Seric 			CurEnv->e_to = q->q_paddr;
15075008Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
15087052Seric 				message(Arpa_Info, "deliverable");
15094550Seric 		}
15105008Seric 		else
15116974Seric 			(void) deliver(q);
15124550Seric 	}
15137043Seric 
15147043Seric 	/*
15157043Seric 	**  Now run through and check for errors.
15167043Seric 	*/
15177043Seric 
15187043Seric 	if (verifyonly)
15197043Seric 		return;
15207043Seric 
15217043Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15227043Seric 	{
15237043Seric 		register ADDRESS *qq;
15247043Seric 
15257043Seric 		if (bitset(QQUEUEUP, q->q_flags))
15267043Seric 			e->e_queueup = TRUE;
15277043Seric 		if (!bitset(QBADADDR, q->q_flags))
15287043Seric 			continue;
15297043Seric 
15307043Seric 		/* we have an address that failed -- find the parent */
15317043Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
15327043Seric 		{
15337043Seric 			char obuf[MAXNAME + 6];
15347043Seric 			extern char *aliaslookup();
15357043Seric 
15367043Seric 			/* we can only have owners for local addresses */
15377043Seric 			if (!bitset(M_LOCAL, qq->q_mailer->m_flags))
15387043Seric 				continue;
15397043Seric 
15407043Seric 			/* see if the owner list exists */
15417043Seric 			(void) strcpy(obuf, "owner-");
15427047Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
15437047Seric 				(void) strcat(obuf, "owner");
15447047Seric 			else
15457047Seric 				(void) strcat(obuf, qq->q_user);
15467043Seric 			if (aliaslookup(obuf) == NULL)
15477043Seric 				continue;
15487043Seric 
15497043Seric 			/* owner list exists -- add it to the error queue */
15507043Seric 			qq->q_flags &= ~QPRIMARY;
15517043Seric 			sendto(obuf, 1, qq, &e->e_errorqueue);
15527043Seric 			MailBack = TRUE;
15537043Seric 			break;
15547043Seric 		}
15557043Seric 
15567043Seric 		/* if we did not find an owner, send to the sender */
15577043Seric 		if (qq == NULL)
15587043Seric 			sendto(e->e_from.q_paddr, 1, qq, &e->e_errorqueue);
15597043Seric 	}
15604550Seric }
15617043Seric /*
15627043Seric **  CHECKERRORS -- check a queue of addresses and process errors.
15637043Seric **
15647043Seric **	Parameters:
15657043Seric **		e -- the envelope to check.
15667043Seric **
15677043Seric **	Returns:
15687043Seric **		none.
15697043Seric **
15707043Seric **	Side Effects:
15717043Seric **		Arranges to queue all tempfailed messages in q
15727043Seric **			or deliver error responses.
15737043Seric */
15747043Seric 
15757043Seric checkerrors(e)
15767043Seric 	register ENVELOPE *e;
15777043Seric {
15787043Seric # ifdef DEBUG
15797043Seric 	if (Debug > 0)
15807043Seric 	{
15817371Seric 		printf("\ncheckerrors: FatalErrors %d, errorqueue:\n");
15827043Seric 		printaddr(e->e_errorqueue, TRUE);
15837043Seric 	}
15847043Seric # endif DEBUG
15857043Seric 
15867043Seric 	/* mail back the transcript on errors */
15877043Seric 	if (FatalErrors)
15887043Seric 		savemail();
15897043Seric 
15907043Seric 	/* queue up anything laying around */
15917043Seric 	if (e->e_queueup)
15927043Seric 	{
15937043Seric # ifdef QUEUE
15947043Seric 		queueup(e, FALSE);
15957043Seric # else QUEUE
15967043Seric 		syserr("finis: trying to queue %s", e->e_df);
15977043Seric # endif QUEUE
15987043Seric 	}
15997043Seric }
1600