1294Seric # include <signal.h>
24123Seric # include <errno.h>
34621Seric # include "sendmail.h"
44327Seric # include <sys/stat.h>
5294Seric 
6*7672Seric SCCSID(@(#)deliver.c	3.96		08/08/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
60*7672Seric 	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
190*7672Seric 		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
610*7672Seric 	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 
672*7672Seric 	(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
876*7672Seric 	if (LogLevel > 1)
877*7672Seric 		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 */
9784370Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
9794447Seric 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
9803385Seric 		{
9814370Seric 			p = origfrom;
9824370Seric 			origfrom = NULL;
9834370Seric 		}
9844370Seric 		else if (bitset(H_DEFAULT, h->h_flags))
9854370Seric 		{
9867666Seric 			/* macro expand value if generated internally */
9876974Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
9883385Seric 			p = buf;
9893385Seric 		}
9905913Seric 		else if (bitset(H_ADDR, h->h_flags))
9915913Seric 		{
9925913Seric 			register int opos;
9935913Seric 			bool firstone = TRUE;
9945913Seric 
9955913Seric 			/*
9965913Seric 			**  Output the address list translated by the
9975913Seric 			**  mailer and with commas.
9985913Seric 			*/
9995913Seric 
10005913Seric 			p = h->h_value;
10015913Seric 			if (p == NULL || *p == '\0' || nooutput)
10025913Seric 				continue;
10037123Seric 			obp = obuf;
10047285Seric 			(void) sprintf(obp, "%s: ", capitalize(h->h_field));
10055913Seric 			opos = strlen(h->h_field) + 2;
10067123Seric 			obp += opos;
10077666Seric 
10087666Seric 			/*
10097666Seric 			**  Run through the list of values.
10107666Seric 			*/
10117666Seric 
10125913Seric 			while (*p != '\0')
10135913Seric 			{
10147666Seric 				register char *name;
10155913Seric 				extern char *remotename();
10165913Seric 				char savechar;
10175913Seric 
10187666Seric 				/*
10197666Seric 				**  Find the end of the name.  New style names
10207666Seric 				**  end with a comma, old style names end with
10217666Seric 				**  a space character.  However, spaces do not
10227666Seric 				**  necessarily delimit an old-style name -- at
10237666Seric 				**  signs mean keep going.
10247666Seric 				*/
10257666Seric 
10267666Seric 				/* clean up the leading trash in source */
10277666Seric 				while (*p != '\0' && (isspace(*p) || *p == ','))
10287666Seric 					p++;
10297666Seric 				name = p;
10307666Seric 
10317666Seric 				/* find end of name */
10325936Seric 				while (*p != '\0' && *p != ',')
10335936Seric 				{
10345936Seric 					extern bool isatword();
10355936Seric 					char *oldp;
10365936Seric 
10376974Seric 					if (!e->e_oldstyle || !isspace(*p))
10385936Seric 					{
10395936Seric 						p++;
10405936Seric 						continue;
10415936Seric 					}
10427666Seric 
10437666Seric 					/* look to see if we have an at sign */
10445936Seric 					oldp = p;
10455936Seric 					while (*p != '\0' && isspace(*p))
10465936Seric 						p++;
10477666Seric 
10485936Seric 					if (*p != '@' && !isatword(p))
10495936Seric 					{
10505936Seric 						p = oldp;
10515936Seric 						break;
10525936Seric 					}
10535936Seric 					p += *p == '@' ? 1 : 2;
10545936Seric 					while (*p != '\0' && isspace(*p))
10555936Seric 						p++;
10565936Seric 				}
10577666Seric 				/* at the end of one complete name */
10587666Seric 
10597666Seric 				/* strip off trailing white space */
10607666Seric 				while (p >= name && (isspace(*p) || *p == ','))
10617666Seric 					p--;
10627666Seric 				if (++p == name)
10637666Seric 					continue;
10645913Seric 				savechar = *p;
10655913Seric 				*p = '\0';
10665913Seric 
10675913Seric 				/* translate the name to be relative */
10686820Seric 				name = remotename(name, m, FALSE);
10695913Seric 				if (*name == '\0')
10707666Seric 				{
10717666Seric 					*p = savechar;
10725913Seric 					continue;
10737666Seric 				}
10745913Seric 
10755913Seric 				/* output the name with nice formatting */
10765913Seric 				opos += strlen(name);
10775913Seric 				if (!firstone)
10785913Seric 					opos += 2;
10795913Seric 				if (opos > 78 && !firstone)
10805913Seric 				{
10817123Seric 					(void) sprintf(obp, ",\n");
10827123Seric 					putline(obuf, fp, fullsmtp);
10837123Seric 					obp = obuf;
10847123Seric 					(void) sprintf(obp, "        ");
10857123Seric 					obp += strlen(obp);
10865916Seric 					opos = 8 + strlen(name);
10875913Seric 				}
10885913Seric 				else if (!firstone)
10897123Seric 				{
10907123Seric 					(void) sprintf(obp, ", ");
10917123Seric 					obp += 2;
10927123Seric 				}
10937123Seric 				(void) sprintf(obp, "%s", name);
10947123Seric 				obp += strlen(obp);
10955913Seric 				firstone = FALSE;
10965913Seric 				*p = savechar;
10975913Seric 			}
10987285Seric 			(void) strcpy(obp, "\n");
10997202Seric 			putline(obuf, fp, fullsmtp);
11005913Seric 			nooutput = TRUE;
11015913Seric 		}
11022898Seric 		else
11033385Seric 			p = h->h_value;
11044447Seric 		if (p == NULL || *p == '\0')
11053389Seric 			continue;
11064209Seric 
11074209Seric 		/* hack, hack -- output Original-From field if different */
11084447Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
11094209Seric 		{
11104447Seric 			/* output new Original-From line if needed */
11114447Seric 			if (of_line == NULL && !samefrom(p, origfrom))
11127123Seric 			{
11137123Seric 				(void) sprintf(obuf, "Original-From: %s\n", origfrom);
11147123Seric 				putline(obuf, fp, fullsmtp);
11157123Seric 			}
11164447Seric 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
11174447Seric 			{
11184447Seric 				/* delete Original-From: line if redundant */
11194447Seric 				p = of_line;
11204447Seric 				of_line = NULL;
11214447Seric 			}
11224447Seric 		}
11234447Seric 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
11244447Seric 			nooutput = TRUE;
11254447Seric 
11264447Seric 		/* finally, output the header line */
11274447Seric 		if (!nooutput)
11284447Seric 		{
11297123Seric 			(void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p);
11307123Seric 			putline(obuf, fp, fullsmtp);
11314447Seric 			h->h_flags |= H_USED;
11324209Seric 		}
11332898Seric 	}
1134294Seric }
1135294Seric /*
11366974Seric **  PUTBODY -- put the body of a message.
11376974Seric **
11386974Seric **	Parameters:
11396974Seric **		fp -- file to output onto.
11406974Seric **		m -- a mailer descriptor.
11416974Seric **		xdot -- if set, use SMTP hidden dot algorithm.
11426974Seric **
11436974Seric **	Returns:
11446974Seric **		none.
11456974Seric **
11466974Seric **	Side Effects:
11476974Seric **		The message is written onto fp.
11486974Seric */
11496974Seric 
11506974Seric putbody(fp, m, xdot)
11516974Seric 	FILE *fp;
11526974Seric 	struct mailer *m;
11536974Seric 	bool xdot;
11546974Seric {
11556974Seric 	char buf[MAXLINE + 1];
11567123Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
11576974Seric 
11586974Seric 	/*
11596974Seric 	**  Output the body of the message
11606974Seric 	*/
11616974Seric 
11627003Seric #ifdef lint
11637003Seric 	/* m will be needed later for complete smtp emulation */
11647003Seric 	if (m == NULL)
11657003Seric 		return;
11667003Seric #endif lint
11677003Seric 
11686974Seric 	if (TempFile != NULL)
11696974Seric 	{
11706974Seric 		rewind(TempFile);
11716974Seric 		buf[0] = '.';
11726974Seric 		while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL)
11737123Seric 			putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp);
11746974Seric 
11756974Seric 		if (ferror(TempFile))
11766974Seric 		{
11776974Seric 			syserr("putbody: read error");
11786974Seric 			ExitStat = EX_IOERR;
11796974Seric 		}
11806974Seric 	}
11816974Seric 
11826974Seric 	(void) fflush(fp);
11836974Seric 	if (ferror(fp) && errno != EPIPE)
11846974Seric 	{
11856974Seric 		syserr("putbody: write error");
11866974Seric 		ExitStat = EX_IOERR;
11876974Seric 	}
11886974Seric 	errno = 0;
11896974Seric }
11906974Seric /*
11915936Seric **  ISATWORD -- tell if the word we are pointing to is "at".
11925936Seric **
11935936Seric **	Parameters:
11945936Seric **		p -- word to check.
11955936Seric **
11965936Seric **	Returns:
11975936Seric **		TRUE -- if p is the word at.
11985936Seric **		FALSE -- otherwise.
11995936Seric **
12005936Seric **	Side Effects:
12015936Seric **		none.
12025936Seric */
12035936Seric 
12045936Seric bool
12055936Seric isatword(p)
12065936Seric 	register char *p;
12075936Seric {
12085936Seric 	extern char lower();
12095936Seric 
12105936Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
12115936Seric 	    p[2] != '\0' && isspace(p[2]))
12125936Seric 		return (TRUE);
12135936Seric 	return (FALSE);
12145936Seric }
12155936Seric /*
12165913Seric **  REMOTENAME -- return the name relative to the current mailer
12175913Seric **
12185913Seric **	Parameters:
12195913Seric **		name -- the name to translate.
12206820Seric **		force -- if set, forces rewriting even if the mailer
12216820Seric **			does not request it.  Used for rewriting
12226820Seric **			sender addresses.
12235913Seric **
12245913Seric **	Returns:
12255913Seric **		the text string representing this address relative to
12265913Seric **			the receiving mailer.
12275913Seric **
12285913Seric **	Side Effects:
12295913Seric **		none.
12305913Seric **
12315913Seric **	Warnings:
12325913Seric **		The text string returned is tucked away locally;
12335913Seric **			copy it if you intend to save it.
12345913Seric */
12355913Seric 
12365913Seric char *
12376820Seric remotename(name, m, force)
12385913Seric 	char *name;
12395916Seric 	struct mailer *m;
12406820Seric 	bool force;
12415913Seric {
12425913Seric 	static char buf[MAXNAME];
12435913Seric 	char lbuf[MAXNAME];
12445913Seric 	extern char *macvalue();
12455913Seric 	char *oldf = macvalue('f');
12465916Seric 	char *oldx = macvalue('x');
12475916Seric 	char *oldg = macvalue('g');
12485913Seric 	extern char **prescan();
12495913Seric 	register char **pvp;
12505916Seric 	extern char *getxpart();
12517003Seric 	extern ADDRESS *buildaddr();
12525913Seric 
12535913Seric 	/*
12546820Seric 	**  See if this mailer wants the name to be rewritten.  There are
12556820Seric 	**  many problems here, owing to the standards for doing replies.
12566820Seric 	**  In general, these names should only be rewritten if we are
12576820Seric 	**  sending to another host that runs sendmail.
12586820Seric 	*/
12596820Seric 
12606820Seric 	if (!bitset(M_RELRCPT, m->m_flags) && !force)
12617003Seric 		return (name);
12626820Seric 
12636820Seric 	/*
12645913Seric 	**  Do general rewriting of name.
12655913Seric 	**	This will also take care of doing global name translation.
12665913Seric 	*/
12675913Seric 
12685916Seric 	define('x', getxpart(name));
12695913Seric 	pvp = prescan(name, '\0');
12707259Seric 	if (pvp == NULL)
12717259Seric 		return (name);
12727048Seric 	rewrite(pvp, 1);
12737048Seric 	rewrite(pvp, 3);
12747048Seric 	if (**pvp == CANONNET)
12755913Seric 	{
12767048Seric 		/* oops... resolved to something */
12777048Seric 		return (name);
12785913Seric 	}
12797048Seric 	cataddr(pvp, lbuf, sizeof lbuf);
12805913Seric 
12815913Seric 	/* make the name relative to the receiving mailer */
12825913Seric 	define('f', lbuf);
12836974Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
12845913Seric 
12855913Seric 	/* rewrite to get rid of garbage we added in the expand above */
12865913Seric 	pvp = prescan(buf, '\0');
12877259Seric 	if (pvp == NULL)
12887259Seric 		return (name);
12895913Seric 	rewrite(pvp, 2);
12905916Seric 	cataddr(pvp, lbuf, sizeof lbuf);
12915913Seric 
12925916Seric 	/* now add any comment info we had before back */
12935916Seric 	define('g', lbuf);
12946974Seric 	expand("$q", buf, &buf[sizeof buf - 1], CurEnv);
12955916Seric 
12965913Seric 	define('f', oldf);
12975916Seric 	define('g', oldg);
12985916Seric 	define('x', oldx);
12995913Seric 
13005913Seric # ifdef DEBUG
1301*7672Seric 	if (tTd(12, 1))
13025913Seric 		printf("remotename(%s) => `%s'\n", name, buf);
13035913Seric # endif DEBUG
13045913Seric 	return (buf);
13055913Seric }
13065913Seric /*
13074370Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
13084370Seric **
13094370Seric **	Parameters:
13104370Seric **		ifrom -- internally generated form of from address.
13114370Seric **		efrom -- external form of from address.
13124370Seric **
13134370Seric **	Returns:
13144370Seric **		TRUE -- if they convey the same info.
13154370Seric **		FALSE -- if any information has been lost.
13164370Seric **
13174370Seric **	Side Effects:
13184370Seric **		none.
13194370Seric */
13204370Seric 
13214370Seric bool
13224370Seric samefrom(ifrom, efrom)
13234370Seric 	char *ifrom;
13244370Seric 	char *efrom;
13254370Seric {
13264447Seric 	register char *p;
13274447Seric 	char buf[MAXNAME + 4];
13284447Seric 
13294447Seric # ifdef DEBUG
1330*7672Seric 	if (tTd(3, 8))
13314447Seric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
13324447Seric # endif DEBUG
13334447Seric 	if (strcmp(ifrom, efrom) == 0)
13344447Seric 		goto success;
13354447Seric 	p = index(ifrom, '@');
13364447Seric 	if (p == NULL)
13374447Seric 		goto failure;
13384447Seric 	*p = '\0';
13397003Seric 	(void) strcpy(buf, ifrom);
13407003Seric 	(void) strcat(buf, " at ");
13414447Seric 	*p++ = '@';
13427003Seric 	(void) strcat(buf, p);
13434447Seric 	if (strcmp(buf, efrom) == 0)
13444447Seric 		goto success;
13454447Seric 
13464447Seric   failure:
13474447Seric # ifdef DEBUG
1348*7672Seric 	if (tTd(3, 8))
13494447Seric 		printf("FALSE\n");
13504447Seric # endif DEBUG
13514447Seric 	return (FALSE);
13524447Seric 
13534447Seric   success:
13544447Seric # ifdef DEBUG
1355*7672Seric 	if (tTd(3, 8))
13564447Seric 		printf("TRUE\n");
13574447Seric # endif DEBUG
13584447Seric 	return (TRUE);
13594370Seric }
13604370Seric /*
1361294Seric **  MAILFILE -- Send a message to a file.
1362294Seric **
13634327Seric **	If the file has the setuid/setgid bits set, but NO execute
13644327Seric **	bits, sendmail will try to become the owner of that file
13654327Seric **	rather than the real user.  Obviously, this only works if
13664327Seric **	sendmail runs as root.
13674327Seric **
1368294Seric **	Parameters:
1369294Seric **		filename -- the name of the file to send to.
13704397Seric **		ctladdr -- the controlling address header -- includes
13714397Seric **			the userid/groupid to be when sending.
1372294Seric **
1373294Seric **	Returns:
1374294Seric **		The exit code associated with the operation.
1375294Seric **
1376294Seric **	Side Effects:
1377294Seric **		none.
1378294Seric */
1379294Seric 
13804397Seric mailfile(filename, ctladdr)
1381294Seric 	char *filename;
13824397Seric 	ADDRESS *ctladdr;
1383294Seric {
1384294Seric 	register FILE *f;
13854214Seric 	register int pid;
1386294Seric 
13874214Seric 	/*
13884214Seric 	**  Fork so we can change permissions here.
13894214Seric 	**	Note that we MUST use fork, not vfork, because of
13904214Seric 	**	the complications of calling subroutines, etc.
13914214Seric 	*/
13924067Seric 
13934214Seric 	DOFORK(fork);
13944214Seric 
13954214Seric 	if (pid < 0)
13964214Seric 		return (EX_OSERR);
13974214Seric 	else if (pid == 0)
13984214Seric 	{
13994214Seric 		/* child -- actually write to file */
14004327Seric 		struct stat stb;
14014327Seric 
14024215Seric 		(void) signal(SIGINT, SIG_DFL);
14034215Seric 		(void) signal(SIGHUP, SIG_DFL);
14044215Seric 		(void) signal(SIGTERM, SIG_DFL);
14054327Seric 		umask(OldUmask);
14064327Seric 		if (stat(filename, &stb) < 0)
14074431Seric 			stb.st_mode = 0666;
14084327Seric 		if (bitset(0111, stb.st_mode))
14094327Seric 			exit(EX_CANTCREAT);
14104401Seric 		if (ctladdr == NULL)
14116900Seric 			ctladdr = &CurEnv->e_from;
14124327Seric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
14134417Seric 		{
14144417Seric 			if (ctladdr->q_uid == 0)
14154417Seric 				(void) setgid(DefGid);
14164417Seric 			else
14174417Seric 				(void) setgid(ctladdr->q_gid);
14184417Seric 		}
14194327Seric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
14204417Seric 		{
14214417Seric 			if (ctladdr->q_uid == 0)
14224417Seric 				(void) setuid(DefUid);
14234417Seric 			else
14244417Seric 				(void) setuid(ctladdr->q_uid);
14254417Seric 		}
14266887Seric 		f = dfopen(filename, "a");
14274214Seric 		if (f == NULL)
14284214Seric 			exit(EX_CANTCREAT);
14294214Seric 
14306974Seric 		putfromline(f, Mailer[1]);
14316974Seric 		(*CurEnv->e_puthdr)(f, Mailer[1], CurEnv);
14327123Seric 		fputs("\n", f);
14336974Seric 		(*CurEnv->e_putbody)(f, Mailer[1], FALSE);
14344214Seric 		fputs("\n", f);
14354214Seric 		(void) fclose(f);
14364214Seric 		(void) fflush(stdout);
14374417Seric 
14386887Seric 		/* reset ISUID & ISGID bits for paranoid systems */
14394621Seric 		(void) chmod(filename, (int) stb.st_mode);
14404214Seric 		exit(EX_OK);
14414315Seric 		/*NOTREACHED*/
14424214Seric 	}
14434214Seric 	else
14444214Seric 	{
14454214Seric 		/* parent -- wait for exit status */
14464214Seric 		register int i;
14474214Seric 		auto int stat;
14484214Seric 
14494214Seric 		while ((i = wait(&stat)) != pid)
14504214Seric 		{
14514214Seric 			if (i < 0)
14524214Seric 			{
14534214Seric 				stat = EX_OSERR << 8;
14544214Seric 				break;
14554214Seric 			}
14564214Seric 		}
14574215Seric 		if ((stat & 0377) != 0)
14584215Seric 			stat = EX_UNAVAILABLE << 8;
14594214Seric 		return ((stat >> 8) & 0377);
14604214Seric 	}
1461294Seric }
14624550Seric /*
14634550Seric **  SENDALL -- actually send all the messages.
14644550Seric **
14654550Seric **	Parameters:
14667043Seric **		e -- the envelope to send.
14674550Seric **		verifyonly -- if set, only give verification messages.
14684550Seric **
14694550Seric **	Returns:
14704550Seric **		none.
14714550Seric **
14724550Seric **	Side Effects:
14734550Seric **		Scans the send lists and sends everything it finds.
14747043Seric **		Delivers any appropriate error messages.
14754550Seric */
14764550Seric 
14777043Seric sendall(e, verifyonly)
14787043Seric 	ENVELOPE *e;
14794550Seric 	bool verifyonly;
14804550Seric {
14815008Seric 	register ADDRESS *q;
14824550Seric 
14835032Seric # ifdef DEBUG
1484*7672Seric 	if (tTd(13, 2))
14855032Seric 	{
14866900Seric 		printf("\nSend Queue:\n");
14877043Seric 		printaddr(e->e_sendqueue, TRUE);
14885032Seric 	}
14895032Seric # endif DEBUG
14905008Seric 
14917043Seric 	/*
14927043Seric 	**  Run through the list and send everything.
14937043Seric 	*/
14947043Seric 
14957043Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14964550Seric 	{
14975008Seric 		if (verifyonly)
14984550Seric 		{
14996900Seric 			CurEnv->e_to = q->q_paddr;
15005008Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
15017052Seric 				message(Arpa_Info, "deliverable");
15024550Seric 		}
15035008Seric 		else
15046974Seric 			(void) deliver(q);
15054550Seric 	}
15067043Seric 
15077043Seric 	/*
15087043Seric 	**  Now run through and check for errors.
15097043Seric 	*/
15107043Seric 
15117043Seric 	if (verifyonly)
15127043Seric 		return;
15137043Seric 
15147043Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15157043Seric 	{
15167043Seric 		register ADDRESS *qq;
15177043Seric 
15187043Seric 		if (bitset(QQUEUEUP, q->q_flags))
15197043Seric 			e->e_queueup = TRUE;
15207043Seric 		if (!bitset(QBADADDR, q->q_flags))
15217043Seric 			continue;
15227043Seric 
15237043Seric 		/* we have an address that failed -- find the parent */
15247043Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
15257043Seric 		{
15267043Seric 			char obuf[MAXNAME + 6];
15277043Seric 			extern char *aliaslookup();
15287043Seric 
15297043Seric 			/* we can only have owners for local addresses */
15307043Seric 			if (!bitset(M_LOCAL, qq->q_mailer->m_flags))
15317043Seric 				continue;
15327043Seric 
15337043Seric 			/* see if the owner list exists */
15347043Seric 			(void) strcpy(obuf, "owner-");
15357047Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
15367047Seric 				(void) strcat(obuf, "owner");
15377047Seric 			else
15387047Seric 				(void) strcat(obuf, qq->q_user);
15397043Seric 			if (aliaslookup(obuf) == NULL)
15407043Seric 				continue;
15417043Seric 
15427043Seric 			/* owner list exists -- add it to the error queue */
15437043Seric 			qq->q_flags &= ~QPRIMARY;
15447043Seric 			sendto(obuf, 1, qq, &e->e_errorqueue);
15457043Seric 			MailBack = TRUE;
15467043Seric 			break;
15477043Seric 		}
15487043Seric 
15497043Seric 		/* if we did not find an owner, send to the sender */
15507043Seric 		if (qq == NULL)
15517043Seric 			sendto(e->e_from.q_paddr, 1, qq, &e->e_errorqueue);
15527043Seric 	}
15534550Seric }
15547043Seric /*
15557043Seric **  CHECKERRORS -- check a queue of addresses and process errors.
15567043Seric **
15577043Seric **	Parameters:
15587043Seric **		e -- the envelope to check.
15597043Seric **
15607043Seric **	Returns:
15617043Seric **		none.
15627043Seric **
15637043Seric **	Side Effects:
15647043Seric **		Arranges to queue all tempfailed messages in q
15657043Seric **			or deliver error responses.
15667043Seric */
15677043Seric 
15687043Seric checkerrors(e)
15697043Seric 	register ENVELOPE *e;
15707043Seric {
15717043Seric # ifdef DEBUG
1572*7672Seric 	if (tTd(4, 1))
15737043Seric 	{
15747371Seric 		printf("\ncheckerrors: FatalErrors %d, errorqueue:\n");
15757043Seric 		printaddr(e->e_errorqueue, TRUE);
15767043Seric 	}
15777043Seric # endif DEBUG
15787043Seric 
15797043Seric 	/* mail back the transcript on errors */
15807043Seric 	if (FatalErrors)
15817043Seric 		savemail();
15827043Seric 
15837043Seric 	/* queue up anything laying around */
15847043Seric 	if (e->e_queueup)
15857043Seric 	{
15867043Seric # ifdef QUEUE
15877043Seric 		queueup(e, FALSE);
15887043Seric # else QUEUE
15897043Seric 		syserr("finis: trying to queue %s", e->e_df);
15907043Seric # endif QUEUE
15917043Seric 	}
15927043Seric }
1593