1294Seric # include <signal.h>
24123Seric # include <errno.h>
34621Seric # include "sendmail.h"
44327Seric # include <sys/stat.h>
5294Seric 
6*7779Seric SCCSID(@(#)deliver.c	3.99		08/17/82);
7405Seric 
8294Seric /*
94315Seric **  DELIVER -- Deliver a message to a list of addresses.
10294Seric **
114315Seric **	This routine delivers to everyone on the same host as the
124315Seric **	user on the head of the list.  It is clever about mailers
134315Seric **	that don't handle multiple users.  It is NOT guaranteed
144315Seric **	that it will deliver to all these addresses however -- so
154315Seric **	deliver should be called once for each address on the
164315Seric **	list.
174315Seric **
18294Seric **	Parameters:
194621Seric **		firstto -- head of the address list to deliver to.
20294Seric **
21294Seric **	Returns:
22294Seric **		zero -- successfully delivered.
23294Seric **		else -- some failure, see ExitStat for more info.
24294Seric **
25294Seric **	Side Effects:
26294Seric **		The standard input is passed off to someone.
27294Seric */
28294Seric 
296974Seric deliver(firstto)
304621Seric 	ADDRESS *firstto;
31294Seric {
324452Seric 	char *host;			/* host being sent to */
334452Seric 	char *user;			/* user being sent to */
34294Seric 	char **pvp;
353233Seric 	register char **mvp;
363233Seric 	register char *p;
374452Seric 	register struct mailer *m;	/* mailer for this recipient */
38294Seric 	register int i;
392968Seric 	extern bool checkcompat();
403233Seric 	char *pv[MAXPV+1];
414452Seric 	char tobuf[MAXLINE];		/* text line of to people */
423233Seric 	char buf[MAXNAME];
434397Seric 	ADDRESS *ctladdr;
444397Seric 	extern ADDRESS *getctladdr();
454452Seric 	char tfrombuf[MAXNAME];		/* translated from person */
464452Seric 	extern char **prescan();
474621Seric 	register ADDRESS *to = firstto;
484863Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
495032Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
507052Seric 	bool notopen = TRUE;		/* set if connection not quite open */
51294Seric 
524488Seric 	errno = 0;
537052Seric 	if (bitset(QDONTSEND, to->q_flags))
543233Seric 		return (0);
55294Seric 
566974Seric 	m = to->q_mailer;
576974Seric 	host = to->q_host;
586974Seric 
59294Seric # ifdef DEBUG
607672Seric 	if (tTd(10, 1))
613233Seric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
626974Seric 			m->m_mno, host, to->q_user);
63294Seric # endif DEBUG
64294Seric 
65294Seric 	/*
665903Seric 	**  If this mailer is expensive, and if we don't want to make
675903Seric 	**  connections now, just mark these addresses and return.
685903Seric 	**	This is useful if we want to batch connections to
695903Seric 	**	reduce load.  This will cause the messages to be
705903Seric 	**	queued up, and a daemon will come along to send the
715903Seric 	**	messages later.
725903Seric 	**		This should be on a per-mailer basis.
735903Seric 	*/
745903Seric 
755903Seric 	if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags))
765903Seric 	{
775903Seric 		for (; to != NULL; to = to->q_next)
785903Seric 			if (!bitset(QDONTSEND, to->q_flags))
795903Seric 				to->q_flags |= QQUEUEUP|QDONTSEND;
805903Seric 		return (0);
815903Seric 	}
825903Seric 
835903Seric 	/*
843233Seric 	**  Do initial argv setup.
853233Seric 	**	Insert the mailer name.  Notice that $x expansion is
863233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
873233Seric 	**	a picky -f flag, we insert it as appropriate.  This
883233Seric 	**	code does not check for 'pv' overflow; this places a
893233Seric 	**	manifest lower limit of 4 for MAXPV.
905903Seric 	**		We rewrite the from address here, being careful
915903Seric 	**		to also rewrite it again using ruleset 2 to
925903Seric 	**		eliminate redundancies.
932968Seric 	*/
942968Seric 
954452Seric 	/* rewrite from address, using rewriting rules */
966974Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
974452Seric 	mvp = prescan(buf, '\0');
984452Seric 	if (mvp == NULL)
994452Seric 	{
1004452Seric 		syserr("bad mailer from translate \"%s\"", buf);
1014452Seric 		return (EX_SOFTWARE);
1024452Seric 	}
1034452Seric 	rewrite(mvp, 2);
1044452Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
1054452Seric 
1064452Seric 	define('g', tfrombuf);		/* translated sender address */
1073233Seric 	define('h', host);		/* to host */
1083233Seric 	Errors = 0;
1093233Seric 	pvp = pv;
1103233Seric 	*pvp++ = m->m_argv[0];
1112968Seric 
1123233Seric 	/* insert -f or -r flag as appropriate */
1133233Seric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1143233Seric 	{
1153233Seric 		if (bitset(M_FOPT, m->m_flags))
1163233Seric 			*pvp++ = "-f";
1173233Seric 		else
1183233Seric 			*pvp++ = "-r";
1196974Seric 		expand("$g", buf, &buf[sizeof buf - 1], CurEnv);
1203233Seric 		*pvp++ = newstr(buf);
1213233Seric 	}
122294Seric 
123294Seric 	/*
1243233Seric 	**  Append the other fixed parts of the argv.  These run
1253233Seric 	**  up to the first entry containing "$u".  There can only
1263233Seric 	**  be one of these, and there are only a few more slots
1273233Seric 	**  in the pv after it.
128294Seric 	*/
129294Seric 
1303233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
131294Seric 	{
1323233Seric 		while ((p = index(p, '$')) != NULL)
1333233Seric 			if (*++p == 'u')
1343233Seric 				break;
1353233Seric 		if (p != NULL)
1363233Seric 			break;
1373233Seric 
1383233Seric 		/* this entry is safe -- go ahead and process it */
1396974Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
1403233Seric 		*pvp++ = newstr(buf);
1413233Seric 		if (pvp >= &pv[MAXPV - 3])
1423233Seric 		{
1433233Seric 			syserr("Too many parameters to %s before $u", pv[0]);
1443233Seric 			return (-1);
1453233Seric 		}
146294Seric 	}
1474863Seric 
1486038Seric 	/*
1496038Seric 	**  If we have no substitution for the user name in the argument
1506038Seric 	**  list, we know that we must supply the names otherwise -- and
1516038Seric 	**  SMTP is the answer!!
1526038Seric 	*/
1536038Seric 
1543233Seric 	if (*mvp == NULL)
1554863Seric 	{
1564863Seric 		/* running SMTP */
1575179Seric # ifdef SMTP
1584863Seric 		clever = TRUE;
1594863Seric 		*pvp = NULL;
1605179Seric # else SMTP
1616038Seric 		/* oops!  we don't implement SMTP */
1625179Seric 		syserr("SMTP style mailer");
1635179Seric 		return (EX_SOFTWARE);
1645179Seric # endif SMTP
1654863Seric 	}
166294Seric 
167294Seric 	/*
1683233Seric 	**  At this point *mvp points to the argument with $u.  We
1693233Seric 	**  run through our address list and append all the addresses
1703233Seric 	**  we can.  If we run out of space, do not fret!  We can
1713233Seric 	**  always send another copy later.
172294Seric 	*/
173294Seric 
1743233Seric 	tobuf[0] = '\0';
1756900Seric 	CurEnv->e_to = tobuf;
1764397Seric 	ctladdr = NULL;
1773233Seric 	for (; to != NULL; to = to->q_next)
178294Seric 	{
1793233Seric 		/* avoid sending multiple recipients to dumb mailers */
1804382Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1813233Seric 			break;
1823233Seric 
1833233Seric 		/* if already sent or not for this host, don't send */
1847052Seric 		if (bitset(QDONTSEND, to->q_flags) ||
1857052Seric 		    strcmp(to->q_host, host) != 0 ||
1867052Seric 		    to->q_mailer != firstto->q_mailer)
1873233Seric 			continue;
1884397Seric 
1895032Seric # ifdef DEBUG
1907672Seric 		if (tTd(10, 1))
1915032Seric 		{
1925032Seric 			printf("\nsend to ");
1935032Seric 			printaddr(to, FALSE);
1945032Seric 		}
1955032Seric # endif DEBUG
1965032Seric 
1974397Seric 		/* compute effective uid/gid when sending */
1984596Seric 		if (to->q_mailer == ProgMailer)
1994397Seric 			ctladdr = getctladdr(to);
2004397Seric 
2013233Seric 		user = to->q_user;
2026900Seric 		CurEnv->e_to = to->q_paddr;
2033233Seric 		to->q_flags |= QDONTSEND;
2043233Seric 
2053233Seric 		/*
2063233Seric 		**  Check to see that these people are allowed to
2073233Seric 		**  talk to each other.
2083233Seric 		*/
2093233Seric 
2103233Seric 		if (!checkcompat(to))
211294Seric 		{
2123233Seric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
2133233Seric 			continue;
214294Seric 		}
2153233Seric 
2163233Seric 		/*
2174099Seric 		**  Strip quote bits from names if the mailer is dumb
2184099Seric 		**	about them.
2193233Seric 		*/
2203233Seric 
2213233Seric 		if (bitset(M_STRIPQ, m->m_flags))
222294Seric 		{
2234099Seric 			stripquotes(user, TRUE);
2244099Seric 			stripquotes(host, TRUE);
2253233Seric 		}
2264099Seric 		else
2274099Seric 		{
2284099Seric 			stripquotes(user, FALSE);
2294099Seric 			stripquotes(host, FALSE);
2304099Seric 		}
2313233Seric 
2323233Seric 		/*
2337052Seric 		**  Do initial connection setup if needed.
2347052Seric 		*/
2357052Seric 
2367052Seric 		if (notopen)
2377052Seric 		{
2387052Seric 			message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
2397052Seric # ifdef SMTP
2407052Seric 			if (clever)
2417052Seric 			{
2427052Seric 				/* send the initial SMTP protocol */
2437052Seric 				i = smtpinit(m, pv, (ADDRESS *) NULL);
2447052Seric 			}
2457052Seric # ifdef SMTP
2467052Seric 			notopen = FALSE;
2477052Seric 		}
2487052Seric 
2497052Seric 		/*
2506059Seric 		**  Pass it to the other host if we are running SMTP.
2516059Seric 		*/
2526059Seric 
2536059Seric 		if (clever)
2546059Seric 		{
2556059Seric # ifdef SMTP
2566059Seric 			i = smtprcpt(to);
2576059Seric 			if (i != EX_OK)
2586059Seric 			{
2596059Seric # ifdef QUEUE
2606059Seric 				if (i == EX_TEMPFAIL)
2616059Seric 					to->q_flags |= QQUEUEUP;
2626059Seric 				else
2636059Seric # endif QUEUE
2646059Seric 					to->q_flags |= QBADADDR;
2657293Seric 				giveresponse(i, TRUE, m);
2666059Seric 			}
2676059Seric # else SMTP
2686059Seric 			syserr("trying to be clever");
2696059Seric # endif SMTP
2706059Seric 		}
2716059Seric 
2726059Seric 		/*
2734161Seric 		**  If an error message has already been given, don't
2744161Seric 		**	bother to send to this address.
2754161Seric 		**
2764161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
2774161Seric 		**	>> NOTE >> cannot do any further aliasing; that
2784161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
2794161Seric 		*/
2804161Seric 
2817293Seric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2824161Seric 			continue;
2834161Seric 
2844283Seric 		/* save statistics.... */
2854596Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2866900Seric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
2874283Seric 
2884161Seric 		/*
2893233Seric 		**  See if this user name is "special".
2903233Seric 		**	If the user name has a slash in it, assume that this
2916974Seric 		**	is a file -- send it off without further ado.  Note
2926974Seric 		**	that this type of addresses is not processed along
2936974Seric 		**	with the others, so we fudge on the To person.
2943233Seric 		*/
2953233Seric 
2964596Seric 		if (m == LocalMailer)
2973233Seric 		{
2985599Seric 			if (user[0] == '/')
299294Seric 			{
3004397Seric 				i = mailfile(user, getctladdr(to));
301294Seric 				giveresponse(i, TRUE, m);
3023233Seric 				continue;
303294Seric 			}
304294Seric 		}
3053233Seric 
3064315Seric 		/*
3074315Seric 		**  Address is verified -- add this user to mailer
3084315Seric 		**  argv, and add it to the print list of recipients.
3094315Seric 		*/
3104315Seric 
3116059Seric 		/* link together the chain of recipients */
3126272Seric 		to->q_tchain = tochain;
3136272Seric 		tochain = to;
3146059Seric 
3153233Seric 		/* create list of users for error messages */
3163233Seric 		if (tobuf[0] != '\0')
3174082Seric 			(void) strcat(tobuf, ",");
3184082Seric 		(void) strcat(tobuf, to->q_paddr);
3193233Seric 		define('u', user);		/* to user */
3204078Seric 		define('z', to->q_home);	/* user's home */
3213233Seric 
3224863Seric 		/*
3236059Seric 		**  Expand out this user into argument list.
3244863Seric 		*/
3254863Seric 
3266059Seric 		if (!clever)
3273233Seric 		{
3286974Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3294863Seric 			*pvp++ = newstr(buf);
3304863Seric 			if (pvp >= &pv[MAXPV - 2])
3314863Seric 			{
3324863Seric 				/* allow some space for trailing parms */
3334863Seric 				break;
3344863Seric 			}
3354863Seric 		}
336294Seric 	}
337294Seric 
3384067Seric 	/* see if any addresses still exist */
3394067Seric 	if (tobuf[0] == '\0')
3404863Seric 	{
3415179Seric # ifdef SMTP
3424863Seric 		if (clever)
3437228Seric 			smtpquit(pv[0], FALSE);
3445179Seric # endif SMTP
3457003Seric 		define('g', (char *) NULL);
3464067Seric 		return (0);
3474863Seric 	}
3484067Seric 
3493233Seric 	/* print out messages as full list */
3506900Seric 	CurEnv->e_to = tobuf;
3513233Seric 
352294Seric 	/*
3533233Seric 	**  Fill out any parameters after the $u parameter.
354294Seric 	*/
355294Seric 
3564863Seric 	while (!clever && *++mvp != NULL)
357294Seric 	{
3586974Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3593233Seric 		*pvp++ = newstr(buf);
3603233Seric 		if (pvp >= &pv[MAXPV])
3613233Seric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
362294Seric 	}
3633233Seric 	*pvp++ = NULL;
364294Seric 
365294Seric 	/*
366294Seric 	**  Call the mailer.
3672898Seric 	**	The argument vector gets built, pipes
368294Seric 	**	are created as necessary, and we fork & exec as
3692898Seric 	**	appropriate.
3704863Seric 	**	If we are running SMTP, we just need to clean up.
371294Seric 	*/
372294Seric 
3734397Seric 	if (ctladdr == NULL)
3746900Seric 		ctladdr = &CurEnv->e_from;
3755179Seric # ifdef SMTP
3764863Seric 	if (clever)
3774863Seric 	{
3786974Seric 		i = smtpfinish(m, CurEnv);
3797228Seric 		smtpquit(pv[0], TRUE);
3804863Seric 	}
3814863Seric 	else
3825179Seric # endif SMTP
3836974Seric 		i = sendoff(m, pv, ctladdr);
3843233Seric 
3854621Seric 	/*
3864621Seric 	**  If we got a temporary failure, arrange to queue the
3874621Seric 	**  addressees.
3884621Seric 	*/
3894621Seric 
3905179Seric # ifdef QUEUE
3914621Seric 	if (i == EX_TEMPFAIL)
3924621Seric 	{
3935032Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
3944621Seric 			to->q_flags |= QQUEUEUP;
3954621Seric 	}
3965179Seric # endif QUEUE
3974621Seric 
3984488Seric 	errno = 0;
3997003Seric 	define('g', (char *) NULL);
4003233Seric 	return (i);
4013233Seric }
4023233Seric /*
4034214Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4044214Seric **
4054214Seric **	This MUST be a macro, since after a vfork we are running
4064214Seric **	two processes on the same stack!!!
4074214Seric **
4084214Seric **	Parameters:
4094214Seric **		none.
4104214Seric **
4114214Seric **	Returns:
4124214Seric **		From a macro???  You've got to be kidding!
4134214Seric **
4144214Seric **	Side Effects:
4154214Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4164214Seric **			pid of child in parent, zero in child.
4174214Seric **			-1 on unrecoverable error.
4184214Seric **
4194214Seric **	Notes:
4204214Seric **		I'm awfully sorry this looks so awful.  That's
4214214Seric **		vfork for you.....
4224214Seric */
4234214Seric 
4244214Seric # define NFORKTRIES	5
4254214Seric # ifdef VFORK
4264214Seric # define XFORK	vfork
4274214Seric # else VFORK
4284214Seric # define XFORK	fork
4294214Seric # endif VFORK
4304214Seric 
4314214Seric # define DOFORK(fORKfN) \
4324214Seric {\
4334214Seric 	register int i;\
4344214Seric \
4354214Seric 	for (i = NFORKTRIES; i-- > 0; )\
4364214Seric 	{\
4374214Seric 		pid = fORKfN();\
4384214Seric 		if (pid >= 0)\
4394214Seric 			break;\
4407003Seric 		sleep(NFORKTRIES - i);\
4414214Seric 	}\
4424214Seric }
4434214Seric /*
4444637Seric **  DOFORK -- simple fork interface to DOFORK.
4454637Seric **
4464637Seric **	Parameters:
4474637Seric **		none.
4484637Seric **
4494637Seric **	Returns:
4504637Seric **		pid of child in parent.
4514637Seric **		zero in child.
4524637Seric **		-1 on error.
4534637Seric **
4544637Seric **	Side Effects:
4554637Seric **		returns twice, once in parent and once in child.
4564637Seric */
4574637Seric 
4584637Seric dofork()
4594637Seric {
4604637Seric 	register int pid;
4614637Seric 
4624637Seric 	DOFORK(fork);
4634637Seric 	return (pid);
4644637Seric }
4654637Seric /*
4663233Seric **  SENDOFF -- send off call to mailer & collect response.
4673233Seric **
4683233Seric **	Parameters:
4693233Seric **		m -- mailer descriptor.
4703233Seric **		pvp -- parameter vector to send to it.
4714397Seric **		ctladdr -- an address pointer controlling the
4724397Seric **			user/groupid etc. of the mailer.
4733233Seric **
4743233Seric **	Returns:
4753233Seric **		exit status of mailer.
4763233Seric **
4773233Seric **	Side Effects:
4783233Seric **		none.
4793233Seric */
4803233Seric 
4816974Seric sendoff(m, pvp, ctladdr)
4823233Seric 	struct mailer *m;
4833233Seric 	char **pvp;
4844397Seric 	ADDRESS *ctladdr;
4853233Seric {
4864863Seric 	auto FILE *mfile;
4874863Seric 	auto FILE *rfile;
4883233Seric 	register int i;
4893233Seric 	int pid;
4904863Seric 
4914863Seric 	/*
4924863Seric 	**  Create connection to mailer.
4934863Seric 	*/
4944863Seric 
4954863Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
4964863Seric 	if (pid < 0)
4974863Seric 		return (-1);
4984863Seric 
4994863Seric 	/*
5004863Seric 	**  Format and send message.
5014863Seric 	*/
5024863Seric 
5034863Seric 	(void) signal(SIGPIPE, SIG_IGN);
5046974Seric 	putfromline(mfile, m);
5056974Seric 	(*CurEnv->e_puthdr)(mfile, m, CurEnv);
5066974Seric 	fprintf(mfile, "\n");
5076974Seric 	(*CurEnv->e_putbody)(mfile, m, FALSE);
5084863Seric 	(void) fclose(mfile);
5094863Seric 
5104863Seric 	i = endmailer(pid, pvp[0]);
5114863Seric 	giveresponse(i, TRUE, m);
5125981Seric 
5135981Seric 	/* arrange a return receipt if requested */
5146900Seric 	if (CurEnv->e_retreceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK)
5155981Seric 	{
5166900Seric 		CurEnv->e_sendreceipt = TRUE;
5176900Seric 		fprintf(Xscript, "%s... successfully delivered\n", CurEnv->e_to);
5185981Seric 		/* do we want to send back more info? */
5195981Seric 	}
5205981Seric 
5214863Seric 	return (i);
5224863Seric }
5234863Seric /*
5244863Seric **  ENDMAILER -- Wait for mailer to terminate.
5254863Seric **
5264863Seric **	We should never get fatal errors (e.g., segmentation
5274863Seric **	violation), so we report those specially.  For other
5284863Seric **	errors, we choose a status message (into statmsg),
5294863Seric **	and if it represents an error, we print it.
5304863Seric **
5314863Seric **	Parameters:
5324863Seric **		pid -- pid of mailer.
5334863Seric **		name -- name of mailer (for error messages).
5344863Seric **
5354863Seric **	Returns:
5364863Seric **		exit code of mailer.
5374863Seric **
5384863Seric **	Side Effects:
5394863Seric **		none.
5404863Seric */
5414863Seric 
5424863Seric endmailer(pid, name)
5434863Seric 	int pid;
5444863Seric 	char *name;
5454863Seric {
5464863Seric 	register int i;
5474863Seric 	auto int st;
5484863Seric 
5496038Seric 	/* in the IPC case there is nothing to wait for */
5506038Seric 	if (pid == 0)
5516038Seric 		return (EX_OK);
5526038Seric 
5536038Seric 	/* wait for the mailer process to die and collect status */
5544863Seric 	while ((i = wait(&st)) > 0 && i != pid)
5554863Seric 		continue;
5564863Seric 	if (i < 0)
5574863Seric 	{
5584863Seric 		syserr("wait");
5594863Seric 		return (-1);
5604863Seric 	}
5616038Seric 
5626038Seric 	/* see if it died a horrid death */
5634863Seric 	if ((st & 0377) != 0)
5644863Seric 	{
5654863Seric 		syserr("%s: stat %o", name, st);
5664863Seric 		ExitStat = EX_UNAVAILABLE;
5674863Seric 		return (-1);
5684863Seric 	}
5696038Seric 
5706038Seric 	/* normal death -- return status */
5714863Seric 	i = (st >> 8) & 0377;
5724863Seric 	return (i);
5734863Seric }
5744863Seric /*
5754863Seric **  OPENMAILER -- open connection to mailer.
5764863Seric **
5774863Seric **	Parameters:
5784863Seric **		m -- mailer descriptor.
5794863Seric **		pvp -- parameter vector to pass to mailer.
5804863Seric **		ctladdr -- controlling address for user.
5814863Seric **		clever -- create a full duplex connection.
5824863Seric **		pmfile -- pointer to mfile (to mailer) connection.
5834863Seric **		prfile -- pointer to rfile (from mailer) connection.
5844863Seric **
5854863Seric **	Returns:
5866038Seric **		pid of mailer ( > 0 ).
5874863Seric **		-1 on error.
5886038Seric **		zero on an IPC connection.
5894863Seric **
5904863Seric **	Side Effects:
5914863Seric **		creates a mailer in a subprocess.
5924863Seric */
5934863Seric 
5944863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
5954863Seric 	struct mailer *m;
5964863Seric 	char **pvp;
5974863Seric 	ADDRESS *ctladdr;
5984863Seric 	bool clever;
5994863Seric 	FILE **pmfile;
6004863Seric 	FILE **prfile;
6014863Seric {
6024863Seric 	int pid;
6034709Seric 	int mpvect[2];
6044863Seric 	int rpvect[2];
6053233Seric 	FILE *mfile;
6064863Seric 	FILE *rfile;
6073233Seric 	extern FILE *fdopen();
6083233Seric 
6093233Seric # ifdef DEBUG
6107672Seric 	if (tTd(11, 1))
611294Seric 	{
6124863Seric 		printf("openmailer:\n");
6133233Seric 		printav(pvp);
614294Seric 	}
6153233Seric # endif DEBUG
6164488Seric 	errno = 0;
6173233Seric 
6186038Seric # ifdef DAEMON
6196038Seric 	/*
6206038Seric 	**  Deal with the special case of mail handled through an IPC
6216038Seric 	**  connection.
6226038Seric 	**	In this case we don't actually fork.  We must be
6236038Seric 	**	running SMTP for this to work.  We will return a
6246038Seric 	**	zero pid to indicate that we are running IPC.
6256038Seric 	*/
6266038Seric 
6276038Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
6286038Seric 	{
6296038Seric 		register int i;
6307285Seric 		register u_short port;
6316038Seric 
6326038Seric 		if (!clever)
6336038Seric 			syserr("non-clever IPC");
6346632Seric 		if (pvp[2] != NULL)
6357285Seric 			port = atoi(pvp[2]);
6366632Seric 		else
6377285Seric 			port = 0;
6387285Seric 		i = makeconnection(pvp[1], port, pmfile, prfile);
6396038Seric 		if (i != EX_OK)
6406047Seric 		{
6416047Seric 			ExitStat = i;
6426038Seric 			return (-1);
6436047Seric 		}
6446038Seric 		else
6456038Seric 			return (0);
6466038Seric 	}
6476038Seric # endif DAEMON
6486038Seric 
6492898Seric 	/* create a pipe to shove the mail through */
6504709Seric 	if (pipe(mpvect) < 0)
651294Seric 	{
6524863Seric 		syserr("pipe (to mailer)");
653294Seric 		return (-1);
654294Seric 	}
6554863Seric 
6565179Seric # ifdef SMTP
6574863Seric 	/* if this mailer speaks smtp, create a return pipe */
6584863Seric 	if (clever && pipe(rpvect) < 0)
6594863Seric 	{
6604863Seric 		syserr("pipe (from mailer)");
6614863Seric 		(void) close(mpvect[0]);
6624863Seric 		(void) close(mpvect[1]);
6634863Seric 		return (-1);
6644863Seric 	}
6655179Seric # endif SMTP
6664863Seric 
6676038Seric 	/*
6686038Seric 	**  Actually fork the mailer process.
6696038Seric 	**	DOFORK is clever about retrying.
6706038Seric 	*/
6716038Seric 
6727672Seric 	(void) fflush(Xscript);				/* for debugging */
6734214Seric 	DOFORK(XFORK);
6744327Seric 	/* pid is set by DOFORK */
675294Seric 	if (pid < 0)
676294Seric 	{
6776038Seric 		/* failure */
678294Seric 		syserr("Cannot fork");
6794709Seric 		(void) close(mpvect[0]);
6804709Seric 		(void) close(mpvect[1]);
6814863Seric 		if (clever)
6824863Seric 		{
6834863Seric 			(void) close(rpvect[0]);
6844863Seric 			(void) close(rpvect[1]);
6854863Seric 		}
686294Seric 		return (-1);
687294Seric 	}
688294Seric 	else if (pid == 0)
689294Seric 	{
690294Seric 		/* child -- set up input & exec mailer */
6911621Seric 		/* make diagnostic output be standard output */
6924477Seric 		(void) signal(SIGINT, SIG_IGN);
6934477Seric 		(void) signal(SIGHUP, SIG_IGN);
6944215Seric 		(void) signal(SIGTERM, SIG_DFL);
6954709Seric 
6964709Seric 		/* arrange to filter standard & diag output of command */
6974863Seric 		if (clever)
6984709Seric 		{
6994863Seric 			(void) close(rpvect[0]);
7004709Seric 			(void) close(1);
7014863Seric 			(void) dup(rpvect[1]);
7024863Seric 			(void) close(rpvect[1]);
7034863Seric 		}
7044863Seric 		else if (OutChannel != stdout)
7054863Seric 		{
7064863Seric 			(void) close(1);
7074709Seric 			(void) dup(fileno(OutChannel));
7084709Seric 		}
7094082Seric 		(void) close(2);
7104082Seric 		(void) dup(1);
7114709Seric 
7124709Seric 		/* arrange to get standard input */
7134709Seric 		(void) close(mpvect[1]);
7144082Seric 		(void) close(0);
7154709Seric 		if (dup(mpvect[0]) < 0)
716294Seric 		{
7172898Seric 			syserr("Cannot dup to zero!");
7182898Seric 			_exit(EX_OSERR);
719294Seric 		}
7204709Seric 		(void) close(mpvect[0]);
7212968Seric 		if (!bitset(M_RESTR, m->m_flags))
7224215Seric 		{
7234417Seric 			if (ctladdr->q_uid == 0)
7244417Seric 			{
7254417Seric 				(void) setgid(DefGid);
7264417Seric 				(void) setuid(DefUid);
7274417Seric 			}
7284417Seric 			else
7294415Seric 			{
7304417Seric 				(void) setgid(ctladdr->q_gid);
7314417Seric 				(void) setuid(ctladdr->q_uid);
7324415Seric 			}
7334215Seric 		}
7342774Seric # ifndef VFORK
7352774Seric 		/*
7362774Seric 		**  We have to be careful with vfork - we can't mung up the
7372774Seric 		**  memory but we don't want the mailer to inherit any extra
7382774Seric 		**  open files.  Chances are the mailer won't
7392774Seric 		**  care about an extra file, but then again you never know.
7402774Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
7412774Seric 		**  declared static so we can't.  But if we fclose(pwf), which
7422774Seric 		**  is what endpwent does, it closes it in the parent too and
7432774Seric 		**  the next getpwnam will be slower.  If you have a weird
7442774Seric 		**  mailer that chokes on the extra file you should do the
7452774Seric 		**  endpwent().
7462774Seric 		**
7472774Seric 		**  Similar comments apply to log.  However, openlog is
7482774Seric 		**  clever enough to set the FIOCLEX mode on the file,
7492774Seric 		**  so it will be closed automatically on the exec.
7502774Seric 		*/
7512774Seric 
7522774Seric 		endpwent();
753294Seric # ifdef LOG
7542089Seric 		closelog();
755294Seric # endif LOG
7562774Seric # endif VFORK
7576038Seric 
7586038Seric 		/* try to execute the mailer */
759294Seric 		execv(m->m_mailer, pvp);
7606038Seric 
761294Seric 		/* syserr fails because log is closed */
762294Seric 		/* syserr("Cannot exec %s", m->m_mailer); */
7634214Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
7644082Seric 		(void) fflush(stdout);
7651619Seric 		_exit(EX_UNAVAILABLE);
766294Seric 	}
767294Seric 
7684709Seric 	/*
7694863Seric 	**  Set up return value.
7704709Seric 	*/
7714709Seric 
7724709Seric 	(void) close(mpvect[0]);
7734709Seric 	mfile = fdopen(mpvect[1], "w");
7744863Seric 	if (clever)
7754863Seric 	{
7764863Seric 		(void) close(rpvect[1]);
7774863Seric 		rfile = fdopen(rpvect[0], "r");
7784863Seric 	}
779294Seric 
7804863Seric 	*pmfile = mfile;
7814863Seric 	*prfile = rfile;
782294Seric 
7834863Seric 	return (pid);
784294Seric }
785294Seric /*
786294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
787294Seric **
788294Seric **	Parameters:
789294Seric **		stat -- the status code from the mailer (high byte
790294Seric **			only; core dumps must have been taken care of
791294Seric **			already).
792294Seric **		force -- if set, force an error message output, even
793294Seric **			if the mailer seems to like to print its own
794294Seric **			messages.
795294Seric **		m -- the mailer descriptor for this mailer.
796294Seric **
797294Seric **	Returns:
7984082Seric **		none.
799294Seric **
800294Seric **	Side Effects:
8011518Seric **		Errors may be incremented.
802294Seric **		ExitStat may be set.
803294Seric */
804294Seric 
805294Seric giveresponse(stat, force, m)
806294Seric 	int stat;
8077228Seric 	bool force;
808294Seric 	register struct mailer *m;
809294Seric {
810294Seric 	register char *statmsg;
811294Seric 	extern char *SysExMsg[];
812294Seric 	register int i;
813294Seric 	extern int N_SysEx;
8141624Seric 	char buf[30];
815294Seric 
8164315Seric 	/*
8174315Seric 	**  Compute status message from code.
8184315Seric 	*/
8194315Seric 
820294Seric 	i = stat - EX__BASE;
821294Seric 	if (i < 0 || i > N_SysEx)
822294Seric 		statmsg = NULL;
823294Seric 	else
824294Seric 		statmsg = SysExMsg[i];
825294Seric 	if (stat == 0)
8264065Seric 	{
8274194Seric 		if (bitset(M_LOCAL, m->m_flags))
8284161Seric 			statmsg = "delivered";
8294161Seric 		else
8304161Seric 			statmsg = "queued";
8317052Seric 		message(Arpa_Info, statmsg);
8324065Seric 	}
8335179Seric # ifdef QUEUE
8344621Seric 	else if (stat == EX_TEMPFAIL)
8354621Seric 	{
8367052Seric 		message(Arpa_Info, "transmission deferred");
8374621Seric 	}
8385179Seric # endif QUEUE
839294Seric 	else
840294Seric 	{
8411518Seric 		Errors++;
8426047Seric 		FatalErrors = TRUE;
843294Seric 		if (statmsg == NULL && m->m_badstat != 0)
844294Seric 		{
845294Seric 			stat = m->m_badstat;
846294Seric 			i = stat - EX__BASE;
847294Seric # ifdef DEBUG
848294Seric 			if (i < 0 || i >= N_SysEx)
849294Seric 				syserr("Bad m_badstat %d", stat);
850294Seric 			else
851294Seric # endif DEBUG
8527344Seric 				statmsg = SysExMsg[i];
853294Seric 		}
854294Seric 		if (statmsg == NULL)
855294Seric 			usrerr("unknown mailer response %d", stat);
8564065Seric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
857294Seric 			usrerr("%s", statmsg);
8587344Seric 		else
8597344Seric 			fprintf(Xscript, "%s\n", statmsg);
860294Seric 	}
861294Seric 
862294Seric 	/*
863294Seric 	**  Final cleanup.
864294Seric 	**	Log a record of the transaction.  Compute the new
865294Seric 	**	ExitStat -- if we already had an error, stick with
866294Seric 	**	that.
867294Seric 	*/
868294Seric 
8691624Seric 	if (statmsg == NULL)
8701624Seric 	{
8714082Seric 		(void) sprintf(buf, "error %d", stat);
8721624Seric 		statmsg = buf;
8731624Seric 	}
8741624Seric 
875294Seric # ifdef LOG
8767680Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
8777672Seric 		syslog(LOG_INFO, "%s: to=%s, stat=%s", MsgId, CurEnv->e_to, statmsg);
878294Seric # endif LOG
8795179Seric # ifdef QUEUE
8804621Seric 	if (stat != EX_TEMPFAIL)
8815179Seric # endif QUEUE
8824621Seric 		setstat(stat);
883294Seric }
884294Seric /*
8856974Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
886294Seric **
8876974Seric **	This can be made an arbitrary message separator by changing $l
888294Seric **
8896974Seric **	One of the ugliest hacks seen by human eyes is
8906974Seric **	contained herein: UUCP wants those stupid
8916974Seric **	"remote from <host>" lines.  Why oh why does a
8926974Seric **	well-meaning programmer such as myself have to
8936974Seric **	deal with this kind of antique garbage????
8946974Seric **
895294Seric **	Parameters:
8966974Seric **		fp -- the file to output to.
8976974Seric **		m -- the mailer describing this entry.
898294Seric **
899294Seric **	Returns:
9006974Seric **		none
901294Seric **
902294Seric **	Side Effects:
9036974Seric **		outputs some text to fp.
904294Seric */
905294Seric 
9066974Seric putfromline(fp, m)
9076974Seric 	register FILE *fp;
9086974Seric 	register MAILER *m;
909294Seric {
9106974Seric 	char buf[MAXLINE];
911294Seric 
9126974Seric 	if (bitset(M_NHDR, m->m_flags))
9136974Seric 		return;
9144315Seric 
9156974Seric # ifdef UGLYUUCP
9166974Seric 	if (bitset(M_UGLYUUCP, m->m_flags))
9174205Seric 	{
9186974Seric 		extern char *macvalue();
9196974Seric 		char *sys = macvalue('g');
9206974Seric 		char *bang = index(sys, '!');
9216041Seric 
9226974Seric 		if (bang == NULL)
9236974Seric 			syserr("No ! in UUCP! (%s)", sys);
9245099Seric 		else
9256974Seric 			*bang = '\0';
9267232Seric 		expand("From $f  $d remote from $g\n", buf,
9276974Seric 				&buf[sizeof buf - 1], CurEnv);
9286974Seric 		*bang = '!';
9296974Seric 	}
9306974Seric 	else
9315179Seric # endif UGLYUUCP
9326974Seric 		expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv);
9337123Seric 	putline(buf, fp, bitset(M_FULLSMTP, m->m_flags));
9345981Seric }
9355981Seric /*
9366974Seric **  PUTHEADER -- put the header part of a message from the in-core copy
9375981Seric **
9385981Seric **	Parameters:
9395981Seric **		fp -- file to put it on.
9405981Seric **		m -- mailer to use.
9416974Seric **		e -- envelope to use.
9425981Seric **
9435981Seric **	Returns:
9445981Seric **		none.
9455981Seric **
9465981Seric **	Side Effects:
9475981Seric **		none.
9485981Seric */
9495981Seric 
9506974Seric putheader(fp, m, e)
9515981Seric 	register FILE *fp;
9525981Seric 	register struct mailer *m;
9536974Seric 	register ENVELOPE *e;
9545981Seric {
9555981Seric 	char buf[BUFSIZ];
9565981Seric 	register HDR *h;
9575981Seric 	extern char *arpadate();
9585981Seric 	extern char *capitalize();
9595981Seric 	extern char *hvalue();
9605981Seric 	extern bool samefrom();
9615981Seric 	char *of_line;
9627123Seric 	char obuf[MAXLINE];
9637123Seric 	register char *obp;
9647123Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
9655981Seric 
9664447Seric 	of_line = hvalue("original-from");
9676974Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
9681828Seric 	{
9694315Seric 		register char *p;
9706974Seric 		char *origfrom = e->e_origfrom;
9714447Seric 		bool nooutput;
9724315Seric 
9734447Seric 		nooutput = FALSE;
9743389Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
9754447Seric 			nooutput = TRUE;
9764447Seric 
9774447Seric 		/* use From: line from message if generated is the same */
9787756Seric 		p = h->h_value;
9794370Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
9804447Seric 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
9813385Seric 		{
9824370Seric 			p = origfrom;
9834370Seric 			origfrom = NULL;
9844370Seric 		}
9854370Seric 		else if (bitset(H_DEFAULT, h->h_flags))
9864370Seric 		{
9877666Seric 			/* macro expand value if generated internally */
9886974Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
9893385Seric 			p = buf;
9903385Seric 		}
9915913Seric 		else if (bitset(H_ADDR, h->h_flags))
9925913Seric 		{
9935913Seric 			if (p == NULL || *p == '\0' || nooutput)
9945913Seric 				continue;
9957756Seric 			commaize(p, capitalize(h->h_field), fp, e->e_oldstyle, m);
9965913Seric 			nooutput = TRUE;
9975913Seric 		}
9984447Seric 		if (p == NULL || *p == '\0')
9993389Seric 			continue;
10004209Seric 
10014209Seric 		/* hack, hack -- output Original-From field if different */
10024447Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
10034209Seric 		{
10044447Seric 			/* output new Original-From line if needed */
10054447Seric 			if (of_line == NULL && !samefrom(p, origfrom))
10067123Seric 			{
10077123Seric 				(void) sprintf(obuf, "Original-From: %s\n", origfrom);
10087123Seric 				putline(obuf, fp, fullsmtp);
10097123Seric 			}
10104447Seric 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
10114447Seric 			{
10124447Seric 				/* delete Original-From: line if redundant */
10134447Seric 				p = of_line;
10144447Seric 				of_line = NULL;
10154447Seric 			}
10164447Seric 		}
10174447Seric 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
10184447Seric 			nooutput = TRUE;
10194447Seric 
10204447Seric 		/* finally, output the header line */
10214447Seric 		if (!nooutput)
10224447Seric 		{
10237123Seric 			(void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p);
10247123Seric 			putline(obuf, fp, fullsmtp);
10254447Seric 			h->h_flags |= H_USED;
10264209Seric 		}
10272898Seric 	}
1028294Seric }
1029294Seric /*
10307756Seric **  COMMAIZE -- output a header field, making a comma-translated list.
10317756Seric **
10327756Seric **	Parameters:
10337756Seric **		p -- the field to output.
10347756Seric **		tag -- the tag to associate with it.
10357756Seric **		fp -- file to put it to.
10367756Seric **		oldstyle -- TRUE if this is an old style header.
10377756Seric **		m -- a pointer to the mailer descriptor.
10387756Seric **
10397756Seric **	Returns:
10407756Seric **		none.
10417756Seric **
10427756Seric **	Side Effects:
10437756Seric **		outputs "p" to file "fp".
10447756Seric */
10457756Seric 
10467756Seric commaize(p, tag, fp, oldstyle, m)
10477756Seric 	register char *p;
10487756Seric 	char *tag;
10497756Seric 	FILE *fp;
10507756Seric 	bool oldstyle;
10517756Seric 	MAILER *m;
10527756Seric {
10537756Seric 	register int opos;
10547756Seric 	bool firstone = TRUE;
10557756Seric 	char obuf[MAXLINE];
10567756Seric 	register char *obp;
10577756Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
10587756Seric 
10597756Seric 	/*
10607756Seric 	**  Output the address list translated by the
10617756Seric 	**  mailer and with commas.
10627756Seric 	*/
10637756Seric 
10647756Seric # ifdef DEBUG
10657756Seric 	if (tTd(14, 2))
10667756Seric 		printf("commaize(%s: %s)\n", tag, p);
10677756Seric # endif DEBUG
10687756Seric 
10697756Seric 	obp = obuf;
10707756Seric 	(void) sprintf(obp, "%s: ", tag);
10717756Seric 	opos = strlen(tag) + 2;
10727756Seric 	obp += opos;
10737756Seric 
10747756Seric 	/*
10757756Seric 	**  Run through the list of values.
10767756Seric 	*/
10777756Seric 
10787756Seric 	while (*p != '\0')
10797756Seric 	{
10807756Seric 		register char *name;
10817756Seric 		extern char *remotename();
10827756Seric 		char savechar;
10837756Seric 
10847756Seric 		/*
10857756Seric 		**  Find the end of the name.  New style names
10867756Seric 		**  end with a comma, old style names end with
10877756Seric 		**  a space character.  However, spaces do not
10887756Seric 		**  necessarily delimit an old-style name -- at
10897756Seric 		**  signs mean keep going.
10907756Seric 		*/
10917756Seric 
10927756Seric 		/* clean up the leading trash in source */
10937756Seric 		while (*p != '\0' && (isspace(*p) || *p == ','))
10947756Seric 			p++;
10957756Seric 		name = p;
10967756Seric 
10977756Seric 		/* find end of name */
10987756Seric 		while (*p != '\0' && *p != ',')
10997756Seric 		{
11007756Seric 			extern bool isatword();
11017756Seric 			char *oldp;
11027756Seric 
11037756Seric 			if (!oldstyle || !isspace(*p))
11047756Seric 			{
11057756Seric 				p++;
11067756Seric 				continue;
11077756Seric 			}
11087756Seric 
11097756Seric 			/* look to see if we have an at sign */
11107756Seric 			oldp = p;
11117756Seric 			while (*p != '\0' && isspace(*p))
11127756Seric 				p++;
11137756Seric 
11147756Seric 			if (*p != '@' && !isatword(p))
11157756Seric 			{
11167756Seric 				p = oldp;
11177756Seric 				break;
11187756Seric 			}
11197756Seric 			p += *p == '@' ? 1 : 2;
11207756Seric 			while (*p != '\0' && isspace(*p))
11217756Seric 				p++;
11227756Seric 		}
11237756Seric 		/* at the end of one complete name */
11247756Seric 
11257756Seric 		/* strip off trailing white space */
11267756Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
11277756Seric 			p--;
11287756Seric 		if (++p == name)
11297756Seric 			continue;
11307756Seric 		savechar = *p;
11317756Seric 		*p = '\0';
11327756Seric 
11337756Seric 		/* translate the name to be relative */
11347756Seric 		name = remotename(name, m, FALSE);
11357756Seric 		if (*name == '\0')
11367756Seric 		{
11377756Seric 			*p = savechar;
11387756Seric 			continue;
11397756Seric 		}
11407756Seric 
11417756Seric 		/* output the name with nice formatting */
11427756Seric 		opos += strlen(name);
11437756Seric 		if (!firstone)
11447756Seric 			opos += 2;
11457756Seric 		if (opos > 78 && !firstone)
11467756Seric 		{
11477756Seric 			(void) sprintf(obp, ",\n");
11487756Seric 			putline(obuf, fp, fullsmtp);
11497756Seric 			obp = obuf;
11507756Seric 			(void) sprintf(obp, "        ");
11517756Seric 			obp += strlen(obp);
11527756Seric 			opos = 8 + strlen(name);
11537756Seric 		}
11547756Seric 		else if (!firstone)
11557756Seric 		{
11567756Seric 			(void) sprintf(obp, ", ");
11577756Seric 			obp += 2;
11587756Seric 		}
11597756Seric 		(void) sprintf(obp, "%s", name);
11607756Seric 		obp += strlen(obp);
11617756Seric 		firstone = FALSE;
11627756Seric 		*p = savechar;
11637756Seric 	}
11647756Seric 	(void) strcpy(obp, "\n");
11657756Seric 	putline(obuf, fp, fullsmtp);
11667756Seric }
11677756Seric /*
11686974Seric **  PUTBODY -- put the body of a message.
11696974Seric **
11706974Seric **	Parameters:
11716974Seric **		fp -- file to output onto.
11726974Seric **		m -- a mailer descriptor.
11736974Seric **		xdot -- if set, use SMTP hidden dot algorithm.
11746974Seric **
11756974Seric **	Returns:
11766974Seric **		none.
11776974Seric **
11786974Seric **	Side Effects:
11796974Seric **		The message is written onto fp.
11806974Seric */
11816974Seric 
11826974Seric putbody(fp, m, xdot)
11836974Seric 	FILE *fp;
11846974Seric 	struct mailer *m;
11856974Seric 	bool xdot;
11866974Seric {
11876974Seric 	char buf[MAXLINE + 1];
11887123Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
11896974Seric 
11906974Seric 	/*
11916974Seric 	**  Output the body of the message
11926974Seric 	*/
11936974Seric 
11947003Seric #ifdef lint
11957003Seric 	/* m will be needed later for complete smtp emulation */
11967003Seric 	if (m == NULL)
11977003Seric 		return;
11987003Seric #endif lint
11997003Seric 
12006974Seric 	if (TempFile != NULL)
12016974Seric 	{
12026974Seric 		rewind(TempFile);
12036974Seric 		buf[0] = '.';
12046974Seric 		while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL)
12057123Seric 			putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp);
12066974Seric 
12076974Seric 		if (ferror(TempFile))
12086974Seric 		{
12096974Seric 			syserr("putbody: read error");
12106974Seric 			ExitStat = EX_IOERR;
12116974Seric 		}
12126974Seric 	}
12136974Seric 
12146974Seric 	(void) fflush(fp);
12156974Seric 	if (ferror(fp) && errno != EPIPE)
12166974Seric 	{
12176974Seric 		syserr("putbody: write error");
12186974Seric 		ExitStat = EX_IOERR;
12196974Seric 	}
12206974Seric 	errno = 0;
12216974Seric }
12226974Seric /*
12235936Seric **  ISATWORD -- tell if the word we are pointing to is "at".
12245936Seric **
12255936Seric **	Parameters:
12265936Seric **		p -- word to check.
12275936Seric **
12285936Seric **	Returns:
12295936Seric **		TRUE -- if p is the word at.
12305936Seric **		FALSE -- otherwise.
12315936Seric **
12325936Seric **	Side Effects:
12335936Seric **		none.
12345936Seric */
12355936Seric 
12365936Seric bool
12375936Seric isatword(p)
12385936Seric 	register char *p;
12395936Seric {
12405936Seric 	extern char lower();
12415936Seric 
12425936Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
12435936Seric 	    p[2] != '\0' && isspace(p[2]))
12445936Seric 		return (TRUE);
12455936Seric 	return (FALSE);
12465936Seric }
12475936Seric /*
12484370Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
12494370Seric **
12504370Seric **	Parameters:
12514370Seric **		ifrom -- internally generated form of from address.
12524370Seric **		efrom -- external form of from address.
12534370Seric **
12544370Seric **	Returns:
12554370Seric **		TRUE -- if they convey the same info.
12564370Seric **		FALSE -- if any information has been lost.
12574370Seric **
12584370Seric **	Side Effects:
12594370Seric **		none.
12604370Seric */
12614370Seric 
12624370Seric bool
12634370Seric samefrom(ifrom, efrom)
12644370Seric 	char *ifrom;
12654370Seric 	char *efrom;
12664370Seric {
12674447Seric 	register char *p;
12684447Seric 	char buf[MAXNAME + 4];
12694447Seric 
12704447Seric # ifdef DEBUG
12717672Seric 	if (tTd(3, 8))
12724447Seric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
12734447Seric # endif DEBUG
12744447Seric 	if (strcmp(ifrom, efrom) == 0)
12754447Seric 		goto success;
12764447Seric 	p = index(ifrom, '@');
12774447Seric 	if (p == NULL)
12784447Seric 		goto failure;
12794447Seric 	*p = '\0';
12807003Seric 	(void) strcpy(buf, ifrom);
12817003Seric 	(void) strcat(buf, " at ");
12824447Seric 	*p++ = '@';
12837003Seric 	(void) strcat(buf, p);
12844447Seric 	if (strcmp(buf, efrom) == 0)
12854447Seric 		goto success;
12864447Seric 
12874447Seric   failure:
12884447Seric # ifdef DEBUG
12897672Seric 	if (tTd(3, 8))
12904447Seric 		printf("FALSE\n");
12914447Seric # endif DEBUG
12924447Seric 	return (FALSE);
12934447Seric 
12944447Seric   success:
12954447Seric # ifdef DEBUG
12967672Seric 	if (tTd(3, 8))
12974447Seric 		printf("TRUE\n");
12984447Seric # endif DEBUG
12994447Seric 	return (TRUE);
13004370Seric }
13014370Seric /*
1302294Seric **  MAILFILE -- Send a message to a file.
1303294Seric **
13044327Seric **	If the file has the setuid/setgid bits set, but NO execute
13054327Seric **	bits, sendmail will try to become the owner of that file
13064327Seric **	rather than the real user.  Obviously, this only works if
13074327Seric **	sendmail runs as root.
13084327Seric **
1309294Seric **	Parameters:
1310294Seric **		filename -- the name of the file to send to.
13114397Seric **		ctladdr -- the controlling address header -- includes
13124397Seric **			the userid/groupid to be when sending.
1313294Seric **
1314294Seric **	Returns:
1315294Seric **		The exit code associated with the operation.
1316294Seric **
1317294Seric **	Side Effects:
1318294Seric **		none.
1319294Seric */
1320294Seric 
13214397Seric mailfile(filename, ctladdr)
1322294Seric 	char *filename;
13234397Seric 	ADDRESS *ctladdr;
1324294Seric {
1325294Seric 	register FILE *f;
13264214Seric 	register int pid;
1327294Seric 
13284214Seric 	/*
13294214Seric 	**  Fork so we can change permissions here.
13304214Seric 	**	Note that we MUST use fork, not vfork, because of
13314214Seric 	**	the complications of calling subroutines, etc.
13324214Seric 	*/
13334067Seric 
13344214Seric 	DOFORK(fork);
13354214Seric 
13364214Seric 	if (pid < 0)
13374214Seric 		return (EX_OSERR);
13384214Seric 	else if (pid == 0)
13394214Seric 	{
13404214Seric 		/* child -- actually write to file */
13414327Seric 		struct stat stb;
13424327Seric 
13434215Seric 		(void) signal(SIGINT, SIG_DFL);
13444215Seric 		(void) signal(SIGHUP, SIG_DFL);
13454215Seric 		(void) signal(SIGTERM, SIG_DFL);
13464327Seric 		umask(OldUmask);
13474327Seric 		if (stat(filename, &stb) < 0)
13484431Seric 			stb.st_mode = 0666;
13494327Seric 		if (bitset(0111, stb.st_mode))
13504327Seric 			exit(EX_CANTCREAT);
13514401Seric 		if (ctladdr == NULL)
13526900Seric 			ctladdr = &CurEnv->e_from;
13534327Seric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
13544417Seric 		{
13554417Seric 			if (ctladdr->q_uid == 0)
13564417Seric 				(void) setgid(DefGid);
13574417Seric 			else
13584417Seric 				(void) setgid(ctladdr->q_gid);
13594417Seric 		}
13604327Seric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
13614417Seric 		{
13624417Seric 			if (ctladdr->q_uid == 0)
13634417Seric 				(void) setuid(DefUid);
13644417Seric 			else
13654417Seric 				(void) setuid(ctladdr->q_uid);
13664417Seric 		}
13676887Seric 		f = dfopen(filename, "a");
13684214Seric 		if (f == NULL)
13694214Seric 			exit(EX_CANTCREAT);
13704214Seric 
13716974Seric 		putfromline(f, Mailer[1]);
13726974Seric 		(*CurEnv->e_puthdr)(f, Mailer[1], CurEnv);
13737123Seric 		fputs("\n", f);
13746974Seric 		(*CurEnv->e_putbody)(f, Mailer[1], FALSE);
13754214Seric 		fputs("\n", f);
13764214Seric 		(void) fclose(f);
13774214Seric 		(void) fflush(stdout);
13784417Seric 
13796887Seric 		/* reset ISUID & ISGID bits for paranoid systems */
13804621Seric 		(void) chmod(filename, (int) stb.st_mode);
13814214Seric 		exit(EX_OK);
13824315Seric 		/*NOTREACHED*/
13834214Seric 	}
13844214Seric 	else
13854214Seric 	{
13864214Seric 		/* parent -- wait for exit status */
13874214Seric 		register int i;
13884214Seric 		auto int stat;
13894214Seric 
13904214Seric 		while ((i = wait(&stat)) != pid)
13914214Seric 		{
13924214Seric 			if (i < 0)
13934214Seric 			{
13944214Seric 				stat = EX_OSERR << 8;
13954214Seric 				break;
13964214Seric 			}
13974214Seric 		}
13984215Seric 		if ((stat & 0377) != 0)
13994215Seric 			stat = EX_UNAVAILABLE << 8;
14004214Seric 		return ((stat >> 8) & 0377);
14014214Seric 	}
1402294Seric }
14034550Seric /*
14044550Seric **  SENDALL -- actually send all the messages.
14054550Seric **
14064550Seric **	Parameters:
14077043Seric **		e -- the envelope to send.
14084550Seric **		verifyonly -- if set, only give verification messages.
14094550Seric **
14104550Seric **	Returns:
14114550Seric **		none.
14124550Seric **
14134550Seric **	Side Effects:
14144550Seric **		Scans the send lists and sends everything it finds.
14157043Seric **		Delivers any appropriate error messages.
14164550Seric */
14174550Seric 
14187043Seric sendall(e, verifyonly)
14197043Seric 	ENVELOPE *e;
14204550Seric 	bool verifyonly;
14214550Seric {
14225008Seric 	register ADDRESS *q;
1423*7779Seric 	bool oldverbose;
14244550Seric 
14255032Seric # ifdef DEBUG
14267672Seric 	if (tTd(13, 2))
14275032Seric 	{
14286900Seric 		printf("\nSend Queue:\n");
14297043Seric 		printaddr(e->e_sendqueue, TRUE);
14305032Seric 	}
14315032Seric # endif DEBUG
14325008Seric 
14337043Seric 	/*
14347043Seric 	**  Run through the list and send everything.
14357043Seric 	*/
14367043Seric 
1437*7779Seric 	oldverbose = Verbose;
1438*7779Seric 	if (verifyonly)
1439*7779Seric 		Verbose = TRUE;
14407043Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14414550Seric 	{
14425008Seric 		if (verifyonly)
14434550Seric 		{
14446900Seric 			CurEnv->e_to = q->q_paddr;
14455008Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
14467052Seric 				message(Arpa_Info, "deliverable");
14474550Seric 		}
14485008Seric 		else
14496974Seric 			(void) deliver(q);
14504550Seric 	}
1451*7779Seric 	Verbose = oldverbose;
14527043Seric 
14537043Seric 	/*
14547043Seric 	**  Now run through and check for errors.
14557043Seric 	*/
14567043Seric 
14577043Seric 	if (verifyonly)
14587043Seric 		return;
14597043Seric 
14607043Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14617043Seric 	{
14627043Seric 		register ADDRESS *qq;
14637043Seric 
14647043Seric 		if (bitset(QQUEUEUP, q->q_flags))
14657043Seric 			e->e_queueup = TRUE;
14667043Seric 		if (!bitset(QBADADDR, q->q_flags))
14677043Seric 			continue;
14687043Seric 
14697043Seric 		/* we have an address that failed -- find the parent */
14707043Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
14717043Seric 		{
14727043Seric 			char obuf[MAXNAME + 6];
14737043Seric 			extern char *aliaslookup();
14747043Seric 
14757043Seric 			/* we can only have owners for local addresses */
14767043Seric 			if (!bitset(M_LOCAL, qq->q_mailer->m_flags))
14777043Seric 				continue;
14787043Seric 
14797043Seric 			/* see if the owner list exists */
14807043Seric 			(void) strcpy(obuf, "owner-");
14817047Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
14827047Seric 				(void) strcat(obuf, "owner");
14837047Seric 			else
14847047Seric 				(void) strcat(obuf, qq->q_user);
14857043Seric 			if (aliaslookup(obuf) == NULL)
14867043Seric 				continue;
14877043Seric 
14887043Seric 			/* owner list exists -- add it to the error queue */
14897043Seric 			qq->q_flags &= ~QPRIMARY;
14907043Seric 			sendto(obuf, 1, qq, &e->e_errorqueue);
14917043Seric 			MailBack = TRUE;
14927043Seric 			break;
14937043Seric 		}
14947043Seric 
14957043Seric 		/* if we did not find an owner, send to the sender */
14967043Seric 		if (qq == NULL)
14977043Seric 			sendto(e->e_from.q_paddr, 1, qq, &e->e_errorqueue);
14987043Seric 	}
14994550Seric }
15007043Seric /*
15017043Seric **  CHECKERRORS -- check a queue of addresses and process errors.
15027043Seric **
15037043Seric **	Parameters:
15047043Seric **		e -- the envelope to check.
15057043Seric **
15067043Seric **	Returns:
15077043Seric **		none.
15087043Seric **
15097043Seric **	Side Effects:
15107043Seric **		Arranges to queue all tempfailed messages in q
15117043Seric **			or deliver error responses.
15127043Seric */
15137043Seric 
15147043Seric checkerrors(e)
15157043Seric 	register ENVELOPE *e;
15167043Seric {
15177043Seric # ifdef DEBUG
15187672Seric 	if (tTd(4, 1))
15197043Seric 	{
15207371Seric 		printf("\ncheckerrors: FatalErrors %d, errorqueue:\n");
15217043Seric 		printaddr(e->e_errorqueue, TRUE);
15227043Seric 	}
15237043Seric # endif DEBUG
15247043Seric 
15257043Seric 	/* mail back the transcript on errors */
15267043Seric 	if (FatalErrors)
15277043Seric 		savemail();
15287043Seric 
15297043Seric 	/* queue up anything laying around */
15307043Seric 	if (e->e_queueup)
15317043Seric 	{
15327043Seric # ifdef QUEUE
15337043Seric 		queueup(e, FALSE);
15347043Seric # else QUEUE
15357043Seric 		syserr("finis: trying to queue %s", e->e_df);
15367043Seric # endif QUEUE
15377043Seric 	}
15387043Seric }
1539