1294Seric # include <signal.h>
24123Seric # include <errno.h>
34621Seric # include "sendmail.h"
44327Seric # include <sys/stat.h>
5294Seric # ifdef LOG
62774Seric # include <syslog.h>
7294Seric # endif LOG
8294Seric 
9*7003Seric SCCSID(@(#)deliver.c	3.81		05/31/82);
10405Seric 
11294Seric /*
124315Seric **  DELIVER -- Deliver a message to a list of addresses.
13294Seric **
144315Seric **	This routine delivers to everyone on the same host as the
154315Seric **	user on the head of the list.  It is clever about mailers
164315Seric **	that don't handle multiple users.  It is NOT guaranteed
174315Seric **	that it will deliver to all these addresses however -- so
184315Seric **	deliver should be called once for each address on the
194315Seric **	list.
204315Seric **
21294Seric **	Parameters:
224621Seric **		firstto -- head of the address list to deliver to.
23294Seric **
24294Seric **	Returns:
25294Seric **		zero -- successfully delivered.
26294Seric **		else -- some failure, see ExitStat for more info.
27294Seric **
28294Seric **	Side Effects:
29294Seric **		The standard input is passed off to someone.
30294Seric */
31294Seric 
326974Seric deliver(firstto)
334621Seric 	ADDRESS *firstto;
34294Seric {
354452Seric 	char *host;			/* host being sent to */
364452Seric 	char *user;			/* user being sent to */
37294Seric 	char **pvp;
383233Seric 	register char **mvp;
393233Seric 	register char *p;
404452Seric 	register struct mailer *m;	/* mailer for this recipient */
41294Seric 	register int i;
422968Seric 	extern bool checkcompat();
433233Seric 	char *pv[MAXPV+1];
444452Seric 	char tobuf[MAXLINE];		/* text line of to people */
453233Seric 	char buf[MAXNAME];
464397Seric 	ADDRESS *ctladdr;
474397Seric 	extern ADDRESS *getctladdr();
484452Seric 	char tfrombuf[MAXNAME];		/* translated from person */
494452Seric 	extern char **prescan();
504621Seric 	register ADDRESS *to = firstto;
514863Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
524863Seric 	bool tempfail = FALSE;
535032Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
54294Seric 
554488Seric 	errno = 0;
565008Seric 	if (!ForceMail && bitset(QDONTSEND, to->q_flags))
573233Seric 		return (0);
58294Seric 
596974Seric 	m = to->q_mailer;
606974Seric 	host = to->q_host;
616974Seric 
62294Seric # ifdef DEBUG
63294Seric 	if (Debug)
643233Seric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
656974Seric 			m->m_mno, host, to->q_user);
66294Seric # endif DEBUG
676974Seric 	if (Verbose)
686974Seric 		message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
69294Seric 
70294Seric 	/*
715903Seric 	**  If this mailer is expensive, and if we don't want to make
725903Seric 	**  connections now, just mark these addresses and return.
735903Seric 	**	This is useful if we want to batch connections to
745903Seric 	**	reduce load.  This will cause the messages to be
755903Seric 	**	queued up, and a daemon will come along to send the
765903Seric 	**	messages later.
775903Seric 	**		This should be on a per-mailer basis.
785903Seric 	*/
795903Seric 
805903Seric 	if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags))
815903Seric 	{
826900Seric 		CurEnv->e_queueup = TRUE;
835903Seric 		for (; to != NULL; to = to->q_next)
845903Seric 			if (!bitset(QDONTSEND, to->q_flags))
855903Seric 				to->q_flags |= QQUEUEUP|QDONTSEND;
865903Seric 		return (0);
875903Seric 	}
885903Seric 
895903Seric 	/*
903233Seric 	**  Do initial argv setup.
913233Seric 	**	Insert the mailer name.  Notice that $x expansion is
923233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
933233Seric 	**	a picky -f flag, we insert it as appropriate.  This
943233Seric 	**	code does not check for 'pv' overflow; this places a
953233Seric 	**	manifest lower limit of 4 for MAXPV.
965903Seric 	**		We rewrite the from address here, being careful
975903Seric 	**		to also rewrite it again using ruleset 2 to
985903Seric 	**		eliminate redundancies.
992968Seric 	*/
1002968Seric 
1014452Seric 	/* rewrite from address, using rewriting rules */
1026974Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
1034452Seric 	mvp = prescan(buf, '\0');
1044452Seric 	if (mvp == NULL)
1054452Seric 	{
1064452Seric 		syserr("bad mailer from translate \"%s\"", buf);
1074452Seric 		return (EX_SOFTWARE);
1084452Seric 	}
1094452Seric 	rewrite(mvp, 2);
1104452Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
1114452Seric 
1124452Seric 	define('g', tfrombuf);		/* translated sender address */
1133233Seric 	define('h', host);		/* to host */
1143233Seric 	Errors = 0;
1153233Seric 	pvp = pv;
1163233Seric 	*pvp++ = m->m_argv[0];
1172968Seric 
1183233Seric 	/* insert -f or -r flag as appropriate */
1193233Seric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1203233Seric 	{
1213233Seric 		if (bitset(M_FOPT, m->m_flags))
1223233Seric 			*pvp++ = "-f";
1233233Seric 		else
1243233Seric 			*pvp++ = "-r";
1256974Seric 		expand("$g", buf, &buf[sizeof buf - 1], CurEnv);
1263233Seric 		*pvp++ = newstr(buf);
1273233Seric 	}
128294Seric 
129294Seric 	/*
1303233Seric 	**  Append the other fixed parts of the argv.  These run
1313233Seric 	**  up to the first entry containing "$u".  There can only
1323233Seric 	**  be one of these, and there are only a few more slots
1333233Seric 	**  in the pv after it.
134294Seric 	*/
135294Seric 
1363233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
137294Seric 	{
1383233Seric 		while ((p = index(p, '$')) != NULL)
1393233Seric 			if (*++p == 'u')
1403233Seric 				break;
1413233Seric 		if (p != NULL)
1423233Seric 			break;
1433233Seric 
1443233Seric 		/* this entry is safe -- go ahead and process it */
1456974Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
1463233Seric 		*pvp++ = newstr(buf);
1473233Seric 		if (pvp >= &pv[MAXPV - 3])
1483233Seric 		{
1493233Seric 			syserr("Too many parameters to %s before $u", pv[0]);
1503233Seric 			return (-1);
1513233Seric 		}
152294Seric 	}
1534863Seric 
1546038Seric 	/*
1556038Seric 	**  If we have no substitution for the user name in the argument
1566038Seric 	**  list, we know that we must supply the names otherwise -- and
1576038Seric 	**  SMTP is the answer!!
1586038Seric 	*/
1596038Seric 
1603233Seric 	if (*mvp == NULL)
1614863Seric 	{
1624863Seric 		/* running SMTP */
1635179Seric # ifdef SMTP
1644863Seric 		clever = TRUE;
1654863Seric 		*pvp = NULL;
1666038Seric 
1676038Seric 		/* send the initial SMTP protocol */
168*7003Seric 		i = smtpinit(m, pv, (ADDRESS *) NULL);
1695179Seric # ifdef QUEUE
1704863Seric 		if (i == EX_TEMPFAIL)
1714863Seric 		{
1726900Seric 			CurEnv->e_queueup = TRUE;
1734863Seric 			tempfail = TRUE;
1744863Seric 		}
1755179Seric # endif QUEUE
1765179Seric # else SMTP
1776038Seric 		/* oops!  we don't implement SMTP */
1785179Seric 		syserr("SMTP style mailer");
1795179Seric 		return (EX_SOFTWARE);
1805179Seric # endif SMTP
1814863Seric 	}
182294Seric 
183294Seric 	/*
1843233Seric 	**  At this point *mvp points to the argument with $u.  We
1853233Seric 	**  run through our address list and append all the addresses
1863233Seric 	**  we can.  If we run out of space, do not fret!  We can
1873233Seric 	**  always send another copy later.
188294Seric 	*/
189294Seric 
1903233Seric 	tobuf[0] = '\0';
1916900Seric 	CurEnv->e_to = tobuf;
1924397Seric 	ctladdr = NULL;
1933233Seric 	for (; to != NULL; to = to->q_next)
194294Seric 	{
1953233Seric 		/* avoid sending multiple recipients to dumb mailers */
1964382Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1973233Seric 			break;
1983233Seric 
1993233Seric 		/* if already sent or not for this host, don't send */
2005008Seric 		if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) ||
2015008Seric 		    strcmp(to->q_host, host) != 0 || to->q_mailer != firstto->q_mailer)
2023233Seric 			continue;
2034397Seric 
2045032Seric # ifdef DEBUG
2055032Seric 		if (Debug)
2065032Seric 		{
2075032Seric 			printf("\nsend to ");
2085032Seric 			printaddr(to, FALSE);
2095032Seric 		}
2105032Seric # endif DEBUG
2115032Seric 
2124397Seric 		/* compute effective uid/gid when sending */
2134596Seric 		if (to->q_mailer == ProgMailer)
2144397Seric 			ctladdr = getctladdr(to);
2154397Seric 
2163233Seric 		user = to->q_user;
2176900Seric 		CurEnv->e_to = to->q_paddr;
2183233Seric 		to->q_flags |= QDONTSEND;
2194863Seric 		if (tempfail)
2205032Seric 		{
2214863Seric 			to->q_flags |= QQUEUEUP;
2225032Seric 			continue;
2235032Seric 		}
2243233Seric 
2253233Seric 		/*
2263233Seric 		**  Check to see that these people are allowed to
2273233Seric 		**  talk to each other.
2283233Seric 		*/
2293233Seric 
2303233Seric 		if (!checkcompat(to))
231294Seric 		{
2323233Seric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
2333233Seric 			continue;
234294Seric 		}
2353233Seric 
2363233Seric 		/*
2374099Seric 		**  Strip quote bits from names if the mailer is dumb
2384099Seric 		**	about them.
2393233Seric 		*/
2403233Seric 
2413233Seric 		if (bitset(M_STRIPQ, m->m_flags))
242294Seric 		{
2434099Seric 			stripquotes(user, TRUE);
2444099Seric 			stripquotes(host, TRUE);
2453233Seric 		}
2464099Seric 		else
2474099Seric 		{
2484099Seric 			stripquotes(user, FALSE);
2494099Seric 			stripquotes(host, FALSE);
2504099Seric 		}
2513233Seric 
2523233Seric 		/*
2536059Seric 		**  Pass it to the other host if we are running SMTP.
2546059Seric 		*/
2556059Seric 
2566059Seric 		if (clever)
2576059Seric 		{
2586059Seric # ifdef SMTP
2596059Seric 			i = smtprcpt(to);
2606059Seric 			if (i != EX_OK)
2616059Seric 			{
2626059Seric # ifdef QUEUE
2636059Seric 				if (i == EX_TEMPFAIL)
2646059Seric 				{
2656900Seric 					CurEnv->e_queueup = TRUE;
2666059Seric 					to->q_flags |= QQUEUEUP;
2676059Seric 				}
2686059Seric 				else
2696059Seric # endif QUEUE
2706059Seric 				{
2716059Seric 					to->q_flags |= QBADADDR;
2726059Seric 					giveresponse(i, TRUE, m);
2736059Seric 				}
2746059Seric 			}
2756059Seric # else SMTP
2766059Seric 			syserr("trying to be clever");
2776059Seric # endif SMTP
2786059Seric 		}
2796059Seric 
2806059Seric 		/*
2814161Seric 		**  If an error message has already been given, don't
2824161Seric 		**	bother to send to this address.
2834161Seric 		**
2844161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
2854161Seric 		**	>> NOTE >> cannot do any further aliasing; that
2864161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
2874161Seric 		*/
2884161Seric 
2894161Seric 		if (bitset(QBADADDR, to->q_flags))
2904161Seric 			continue;
2914161Seric 
2924283Seric 		/* save statistics.... */
2934596Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2946900Seric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
2954283Seric 
2964161Seric 		/*
2973233Seric 		**  See if this user name is "special".
2983233Seric 		**	If the user name has a slash in it, assume that this
2996974Seric 		**	is a file -- send it off without further ado.  Note
3006974Seric 		**	that this type of addresses is not processed along
3016974Seric 		**	with the others, so we fudge on the To person.
3023233Seric 		*/
3033233Seric 
3044596Seric 		if (m == LocalMailer)
3053233Seric 		{
3065599Seric 			if (user[0] == '/')
307294Seric 			{
3084397Seric 				i = mailfile(user, getctladdr(to));
309294Seric 				giveresponse(i, TRUE, m);
3103233Seric 				continue;
311294Seric 			}
312294Seric 		}
3133233Seric 
3144315Seric 		/*
3154315Seric 		**  Address is verified -- add this user to mailer
3164315Seric 		**  argv, and add it to the print list of recipients.
3174315Seric 		*/
3184315Seric 
3196059Seric 		/* link together the chain of recipients */
3206272Seric 		to->q_tchain = tochain;
3216272Seric 		tochain = to;
3226059Seric 
3233233Seric 		/* create list of users for error messages */
3243233Seric 		if (tobuf[0] != '\0')
3254082Seric 			(void) strcat(tobuf, ",");
3264082Seric 		(void) strcat(tobuf, to->q_paddr);
3273233Seric 		define('u', user);		/* to user */
3284078Seric 		define('z', to->q_home);	/* user's home */
3293233Seric 
3304863Seric 		/*
3316059Seric 		**  Expand out this user into argument list.
3324863Seric 		*/
3334863Seric 
3346059Seric 		if (!clever)
3353233Seric 		{
3366974Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3374863Seric 			*pvp++ = newstr(buf);
3384863Seric 			if (pvp >= &pv[MAXPV - 2])
3394863Seric 			{
3404863Seric 				/* allow some space for trailing parms */
3414863Seric 				break;
3424863Seric 			}
3434863Seric 		}
344294Seric 	}
345294Seric 
3464067Seric 	/* see if any addresses still exist */
3474067Seric 	if (tobuf[0] == '\0')
3484863Seric 	{
3495179Seric # ifdef SMTP
3504863Seric 		if (clever)
3514863Seric 			smtpquit(pv[0]);
3525179Seric # endif SMTP
353*7003Seric 		define('g', (char *) NULL);
3544067Seric 		return (0);
3554863Seric 	}
3564067Seric 
3573233Seric 	/* print out messages as full list */
3586900Seric 	CurEnv->e_to = tobuf;
3593233Seric 
360294Seric 	/*
3613233Seric 	**  Fill out any parameters after the $u parameter.
362294Seric 	*/
363294Seric 
3644863Seric 	while (!clever && *++mvp != NULL)
365294Seric 	{
3666974Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3673233Seric 		*pvp++ = newstr(buf);
3683233Seric 		if (pvp >= &pv[MAXPV])
3693233Seric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
370294Seric 	}
3713233Seric 	*pvp++ = NULL;
372294Seric 
373294Seric 	/*
374294Seric 	**  Call the mailer.
3752898Seric 	**	The argument vector gets built, pipes
376294Seric 	**	are created as necessary, and we fork & exec as
3772898Seric 	**	appropriate.
3784863Seric 	**	If we are running SMTP, we just need to clean up.
379294Seric 	*/
380294Seric 
3814397Seric 	if (ctladdr == NULL)
3826900Seric 		ctladdr = &CurEnv->e_from;
3835179Seric # ifdef SMTP
3844863Seric 	if (clever)
3854863Seric 	{
3866974Seric 		i = smtpfinish(m, CurEnv);
3874863Seric 		smtpquit(pv[0]);
3884863Seric 	}
3894863Seric 	else
3905179Seric # endif SMTP
3916974Seric 		i = sendoff(m, pv, ctladdr);
3923233Seric 
3934621Seric 	/*
3944621Seric 	**  If we got a temporary failure, arrange to queue the
3954621Seric 	**  addressees.
3964621Seric 	*/
3974621Seric 
3985179Seric # ifdef QUEUE
3994621Seric 	if (i == EX_TEMPFAIL)
4004621Seric 	{
4016900Seric 		CurEnv->e_queueup = TRUE;
4025032Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
4034621Seric 			to->q_flags |= QQUEUEUP;
4044621Seric 	}
4055179Seric # endif QUEUE
4064621Seric 
4074488Seric 	errno = 0;
408*7003Seric 	define('g', (char *) NULL);
4093233Seric 	return (i);
4103233Seric }
4113233Seric /*
4124214Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4134214Seric **
4144214Seric **	This MUST be a macro, since after a vfork we are running
4154214Seric **	two processes on the same stack!!!
4164214Seric **
4174214Seric **	Parameters:
4184214Seric **		none.
4194214Seric **
4204214Seric **	Returns:
4214214Seric **		From a macro???  You've got to be kidding!
4224214Seric **
4234214Seric **	Side Effects:
4244214Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4254214Seric **			pid of child in parent, zero in child.
4264214Seric **			-1 on unrecoverable error.
4274214Seric **
4284214Seric **	Notes:
4294214Seric **		I'm awfully sorry this looks so awful.  That's
4304214Seric **		vfork for you.....
4314214Seric */
4324214Seric 
4334214Seric # define NFORKTRIES	5
4344214Seric # ifdef VFORK
4354214Seric # define XFORK	vfork
4364214Seric # else VFORK
4374214Seric # define XFORK	fork
4384214Seric # endif VFORK
4394214Seric 
4404214Seric # define DOFORK(fORKfN) \
4414214Seric {\
4424214Seric 	register int i;\
4434214Seric \
4444214Seric 	for (i = NFORKTRIES; i-- > 0; )\
4454214Seric 	{\
4464214Seric 		pid = fORKfN();\
4474214Seric 		if (pid >= 0)\
4484214Seric 			break;\
449*7003Seric 		sleep(NFORKTRIES - i);\
4504214Seric 	}\
4514214Seric }
4524214Seric /*
4534637Seric **  DOFORK -- simple fork interface to DOFORK.
4544637Seric **
4554637Seric **	Parameters:
4564637Seric **		none.
4574637Seric **
4584637Seric **	Returns:
4594637Seric **		pid of child in parent.
4604637Seric **		zero in child.
4614637Seric **		-1 on error.
4624637Seric **
4634637Seric **	Side Effects:
4644637Seric **		returns twice, once in parent and once in child.
4654637Seric */
4664637Seric 
4674637Seric dofork()
4684637Seric {
4694637Seric 	register int pid;
4704637Seric 
4714637Seric 	DOFORK(fork);
4724637Seric 	return (pid);
4734637Seric }
4744637Seric /*
4753233Seric **  SENDOFF -- send off call to mailer & collect response.
4763233Seric **
4773233Seric **	Parameters:
4783233Seric **		m -- mailer descriptor.
4793233Seric **		pvp -- parameter vector to send to it.
4804397Seric **		ctladdr -- an address pointer controlling the
4814397Seric **			user/groupid etc. of the mailer.
4823233Seric **
4833233Seric **	Returns:
4843233Seric **		exit status of mailer.
4853233Seric **
4863233Seric **	Side Effects:
4873233Seric **		none.
4883233Seric */
4893233Seric 
4906974Seric sendoff(m, pvp, ctladdr)
4913233Seric 	struct mailer *m;
4923233Seric 	char **pvp;
4934397Seric 	ADDRESS *ctladdr;
4943233Seric {
4954863Seric 	auto FILE *mfile;
4964863Seric 	auto FILE *rfile;
4973233Seric 	register int i;
4983233Seric 	int pid;
4994863Seric 
5004863Seric 	/*
5014863Seric 	**  Create connection to mailer.
5024863Seric 	*/
5034863Seric 
5044863Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
5054863Seric 	if (pid < 0)
5064863Seric 		return (-1);
5074863Seric 
5084863Seric 	/*
5094863Seric 	**  Format and send message.
5104863Seric 	*/
5114863Seric 
5124863Seric 	(void) signal(SIGPIPE, SIG_IGN);
5136974Seric 	putfromline(mfile, m);
5146974Seric 	(*CurEnv->e_puthdr)(mfile, m, CurEnv);
5156974Seric 	fprintf(mfile, "\n");
5166974Seric 	(*CurEnv->e_putbody)(mfile, m, FALSE);
5174863Seric 	(void) fclose(mfile);
5184863Seric 
5194863Seric 	i = endmailer(pid, pvp[0]);
5204863Seric 	giveresponse(i, TRUE, m);
5215981Seric 
5225981Seric 	/* arrange a return receipt if requested */
5236900Seric 	if (CurEnv->e_retreceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK)
5245981Seric 	{
5256900Seric 		CurEnv->e_sendreceipt = TRUE;
5266900Seric 		fprintf(Xscript, "%s... successfully delivered\n", CurEnv->e_to);
5275981Seric 		/* do we want to send back more info? */
5285981Seric 	}
5295981Seric 
5304863Seric 	return (i);
5314863Seric }
5324863Seric /*
5334863Seric **  ENDMAILER -- Wait for mailer to terminate.
5344863Seric **
5354863Seric **	We should never get fatal errors (e.g., segmentation
5364863Seric **	violation), so we report those specially.  For other
5374863Seric **	errors, we choose a status message (into statmsg),
5384863Seric **	and if it represents an error, we print it.
5394863Seric **
5404863Seric **	Parameters:
5414863Seric **		pid -- pid of mailer.
5424863Seric **		name -- name of mailer (for error messages).
5434863Seric **
5444863Seric **	Returns:
5454863Seric **		exit code of mailer.
5464863Seric **
5474863Seric **	Side Effects:
5484863Seric **		none.
5494863Seric */
5504863Seric 
5514863Seric endmailer(pid, name)
5524863Seric 	int pid;
5534863Seric 	char *name;
5544863Seric {
5554863Seric 	register int i;
5564863Seric 	auto int st;
5574863Seric 
5586038Seric 	/* in the IPC case there is nothing to wait for */
5596038Seric 	if (pid == 0)
5606038Seric 		return (EX_OK);
5616038Seric 
5626038Seric 	/* wait for the mailer process to die and collect status */
5634863Seric 	while ((i = wait(&st)) > 0 && i != pid)
5644863Seric 		continue;
5654863Seric 	if (i < 0)
5664863Seric 	{
5674863Seric 		syserr("wait");
5684863Seric 		return (-1);
5694863Seric 	}
5706038Seric 
5716038Seric 	/* see if it died a horrid death */
5724863Seric 	if ((st & 0377) != 0)
5734863Seric 	{
5744863Seric 		syserr("%s: stat %o", name, st);
5754863Seric 		ExitStat = EX_UNAVAILABLE;
5764863Seric 		return (-1);
5774863Seric 	}
5786038Seric 
5796038Seric 	/* normal death -- return status */
5804863Seric 	i = (st >> 8) & 0377;
5814863Seric 	return (i);
5824863Seric }
5834863Seric /*
5844863Seric **  OPENMAILER -- open connection to mailer.
5854863Seric **
5864863Seric **	Parameters:
5874863Seric **		m -- mailer descriptor.
5884863Seric **		pvp -- parameter vector to pass to mailer.
5894863Seric **		ctladdr -- controlling address for user.
5904863Seric **		clever -- create a full duplex connection.
5914863Seric **		pmfile -- pointer to mfile (to mailer) connection.
5924863Seric **		prfile -- pointer to rfile (from mailer) connection.
5934863Seric **
5944863Seric **	Returns:
5956038Seric **		pid of mailer ( > 0 ).
5964863Seric **		-1 on error.
5976038Seric **		zero on an IPC connection.
5984863Seric **
5994863Seric **	Side Effects:
6004863Seric **		creates a mailer in a subprocess.
6014863Seric */
6024863Seric 
6034863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
6044863Seric 	struct mailer *m;
6054863Seric 	char **pvp;
6064863Seric 	ADDRESS *ctladdr;
6074863Seric 	bool clever;
6084863Seric 	FILE **pmfile;
6094863Seric 	FILE **prfile;
6104863Seric {
6114863Seric 	int pid;
6124709Seric 	int mpvect[2];
6134863Seric 	int rpvect[2];
6143233Seric 	FILE *mfile;
6154863Seric 	FILE *rfile;
6163233Seric 	extern FILE *fdopen();
6173233Seric 
6183233Seric # ifdef DEBUG
6193233Seric 	if (Debug)
620294Seric 	{
6214863Seric 		printf("openmailer:\n");
6223233Seric 		printav(pvp);
623294Seric 	}
6243233Seric # endif DEBUG
6254488Seric 	errno = 0;
6263233Seric 
6276038Seric # ifdef DAEMON
6286038Seric 	/*
6296038Seric 	**  Deal with the special case of mail handled through an IPC
6306038Seric 	**  connection.
6316038Seric 	**	In this case we don't actually fork.  We must be
6326038Seric 	**	running SMTP for this to work.  We will return a
6336038Seric 	**	zero pid to indicate that we are running IPC.
6346038Seric 	*/
6356038Seric 
6366038Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
6376038Seric 	{
6386038Seric 		register int i;
6396038Seric 
6406038Seric 		if (!clever)
6416038Seric 			syserr("non-clever IPC");
6426632Seric 		if (pvp[2] != NULL)
6436632Seric 			i = atoi(pvp[2]);
6446632Seric 		else
6456632Seric 			i = 0;
6466632Seric 		i = makeconnection(pvp[1], i, pmfile, prfile);
6476038Seric 		if (i != EX_OK)
6486047Seric 		{
6496047Seric 			ExitStat = i;
6506038Seric 			return (-1);
6516047Seric 		}
6526038Seric 		else
6536038Seric 			return (0);
6546038Seric 	}
6556038Seric # endif DAEMON
6566038Seric 
6572898Seric 	/* create a pipe to shove the mail through */
6584709Seric 	if (pipe(mpvect) < 0)
659294Seric 	{
6604863Seric 		syserr("pipe (to mailer)");
661294Seric 		return (-1);
662294Seric 	}
6634863Seric 
6645179Seric # ifdef SMTP
6654863Seric 	/* if this mailer speaks smtp, create a return pipe */
6664863Seric 	if (clever && pipe(rpvect) < 0)
6674863Seric 	{
6684863Seric 		syserr("pipe (from mailer)");
6694863Seric 		(void) close(mpvect[0]);
6704863Seric 		(void) close(mpvect[1]);
6714863Seric 		return (-1);
6724863Seric 	}
6735179Seric # endif SMTP
6744863Seric 
6756038Seric 	/*
6766038Seric 	**  Actually fork the mailer process.
6776038Seric 	**	DOFORK is clever about retrying.
6786038Seric 	*/
6796038Seric 
6804214Seric 	DOFORK(XFORK);
6814327Seric 	/* pid is set by DOFORK */
682294Seric 	if (pid < 0)
683294Seric 	{
6846038Seric 		/* failure */
685294Seric 		syserr("Cannot fork");
6864709Seric 		(void) close(mpvect[0]);
6874709Seric 		(void) close(mpvect[1]);
6884863Seric 		if (clever)
6894863Seric 		{
6904863Seric 			(void) close(rpvect[0]);
6914863Seric 			(void) close(rpvect[1]);
6924863Seric 		}
693294Seric 		return (-1);
694294Seric 	}
695294Seric 	else if (pid == 0)
696294Seric 	{
697294Seric 		/* child -- set up input & exec mailer */
6981621Seric 		/* make diagnostic output be standard output */
6994477Seric 		(void) signal(SIGINT, SIG_IGN);
7004477Seric 		(void) signal(SIGHUP, SIG_IGN);
7014215Seric 		(void) signal(SIGTERM, SIG_DFL);
7024709Seric 
7034709Seric 		/* arrange to filter standard & diag output of command */
7044863Seric 		if (clever)
7054709Seric 		{
7064863Seric 			(void) close(rpvect[0]);
7074709Seric 			(void) close(1);
7084863Seric 			(void) dup(rpvect[1]);
7094863Seric 			(void) close(rpvect[1]);
7104863Seric 		}
7114863Seric 		else if (OutChannel != stdout)
7124863Seric 		{
7134863Seric 			(void) close(1);
7144709Seric 			(void) dup(fileno(OutChannel));
7154709Seric 		}
7164082Seric 		(void) close(2);
7174082Seric 		(void) dup(1);
7184709Seric 
7194709Seric 		/* arrange to get standard input */
7204709Seric 		(void) close(mpvect[1]);
7214082Seric 		(void) close(0);
7224709Seric 		if (dup(mpvect[0]) < 0)
723294Seric 		{
7242898Seric 			syserr("Cannot dup to zero!");
7252898Seric 			_exit(EX_OSERR);
726294Seric 		}
7274709Seric 		(void) close(mpvect[0]);
7282968Seric 		if (!bitset(M_RESTR, m->m_flags))
7294215Seric 		{
7304417Seric 			if (ctladdr->q_uid == 0)
7314417Seric 			{
7324417Seric 				(void) setgid(DefGid);
7334417Seric 				(void) setuid(DefUid);
7344417Seric 			}
7354417Seric 			else
7364415Seric 			{
7374417Seric 				(void) setgid(ctladdr->q_gid);
7384417Seric 				(void) setuid(ctladdr->q_uid);
7394415Seric 			}
7404215Seric 		}
7412774Seric # ifndef VFORK
7422774Seric 		/*
7432774Seric 		**  We have to be careful with vfork - we can't mung up the
7442774Seric 		**  memory but we don't want the mailer to inherit any extra
7452774Seric 		**  open files.  Chances are the mailer won't
7462774Seric 		**  care about an extra file, but then again you never know.
7472774Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
7482774Seric 		**  declared static so we can't.  But if we fclose(pwf), which
7492774Seric 		**  is what endpwent does, it closes it in the parent too and
7502774Seric 		**  the next getpwnam will be slower.  If you have a weird
7512774Seric 		**  mailer that chokes on the extra file you should do the
7522774Seric 		**  endpwent().
7532774Seric 		**
7542774Seric 		**  Similar comments apply to log.  However, openlog is
7552774Seric 		**  clever enough to set the FIOCLEX mode on the file,
7562774Seric 		**  so it will be closed automatically on the exec.
7572774Seric 		*/
7582774Seric 
7592774Seric 		endpwent();
760294Seric # ifdef LOG
7612089Seric 		closelog();
762294Seric # endif LOG
7632774Seric # endif VFORK
7646038Seric 
7656038Seric 		/* try to execute the mailer */
766294Seric 		execv(m->m_mailer, pvp);
7676038Seric 
768294Seric 		/* syserr fails because log is closed */
769294Seric 		/* syserr("Cannot exec %s", m->m_mailer); */
7704214Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
7714082Seric 		(void) fflush(stdout);
7721619Seric 		_exit(EX_UNAVAILABLE);
773294Seric 	}
774294Seric 
7754709Seric 	/*
7764863Seric 	**  Set up return value.
7774709Seric 	*/
7784709Seric 
7794709Seric 	(void) close(mpvect[0]);
7804709Seric 	mfile = fdopen(mpvect[1], "w");
7814863Seric 	if (clever)
7824863Seric 	{
7834863Seric 		(void) close(rpvect[1]);
7844863Seric 		rfile = fdopen(rpvect[0], "r");
7854863Seric 	}
786294Seric 
7874863Seric 	*pmfile = mfile;
7884863Seric 	*prfile = rfile;
789294Seric 
7904863Seric 	return (pid);
791294Seric }
792294Seric /*
793294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
794294Seric **
795294Seric **	Parameters:
796294Seric **		stat -- the status code from the mailer (high byte
797294Seric **			only; core dumps must have been taken care of
798294Seric **			already).
799294Seric **		force -- if set, force an error message output, even
800294Seric **			if the mailer seems to like to print its own
801294Seric **			messages.
802294Seric **		m -- the mailer descriptor for this mailer.
803294Seric **
804294Seric **	Returns:
8054082Seric **		none.
806294Seric **
807294Seric **	Side Effects:
8081518Seric **		Errors may be incremented.
809294Seric **		ExitStat may be set.
810294Seric */
811294Seric 
812294Seric giveresponse(stat, force, m)
813294Seric 	int stat;
814294Seric 	int force;
815294Seric 	register struct mailer *m;
816294Seric {
817294Seric 	register char *statmsg;
818294Seric 	extern char *SysExMsg[];
819294Seric 	register int i;
820294Seric 	extern int N_SysEx;
8211624Seric 	char buf[30];
822294Seric 
8234315Seric 	/*
8244315Seric 	**  Compute status message from code.
8254315Seric 	*/
8264315Seric 
827294Seric 	i = stat - EX__BASE;
828294Seric 	if (i < 0 || i > N_SysEx)
829294Seric 		statmsg = NULL;
830294Seric 	else
831294Seric 		statmsg = SysExMsg[i];
832294Seric 	if (stat == 0)
8334065Seric 	{
8344194Seric 		if (bitset(M_LOCAL, m->m_flags))
8354161Seric 			statmsg = "delivered";
8364161Seric 		else
8374161Seric 			statmsg = "queued";
8384065Seric 		if (Verbose)
8394166Seric 			message(Arpa_Info, statmsg);
8404065Seric 	}
8415179Seric # ifdef QUEUE
8424621Seric 	else if (stat == EX_TEMPFAIL)
8434621Seric 	{
8444621Seric 		if (Verbose)
8454621Seric 			message(Arpa_Info, "transmission deferred");
8464621Seric 	}
8475179Seric # endif QUEUE
848294Seric 	else
849294Seric 	{
8501518Seric 		Errors++;
8516047Seric 		FatalErrors = TRUE;
852294Seric 		if (statmsg == NULL && m->m_badstat != 0)
853294Seric 		{
854294Seric 			stat = m->m_badstat;
855294Seric 			i = stat - EX__BASE;
856294Seric # ifdef DEBUG
857294Seric 			if (i < 0 || i >= N_SysEx)
858294Seric 				syserr("Bad m_badstat %d", stat);
859294Seric 			else
860294Seric # endif DEBUG
861294Seric 			statmsg = SysExMsg[i];
862294Seric 		}
863294Seric 		if (statmsg == NULL)
864294Seric 			usrerr("unknown mailer response %d", stat);
8654065Seric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
866294Seric 			usrerr("%s", statmsg);
867294Seric 	}
868294Seric 
869294Seric 	/*
870294Seric 	**  Final cleanup.
871294Seric 	**	Log a record of the transaction.  Compute the new
872294Seric 	**	ExitStat -- if we already had an error, stick with
873294Seric 	**	that.
874294Seric 	*/
875294Seric 
8761624Seric 	if (statmsg == NULL)
8771624Seric 	{
8784082Seric 		(void) sprintf(buf, "error %d", stat);
8791624Seric 		statmsg = buf;
8801624Seric 	}
8811624Seric 
882294Seric # ifdef LOG
8836900Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", CurEnv->e_from.q_paddr, CurEnv->e_to, CurEnv->e_msgsize, statmsg);
884294Seric # endif LOG
8855179Seric # ifdef QUEUE
8864621Seric 	if (stat != EX_TEMPFAIL)
8875179Seric # endif QUEUE
8884621Seric 		setstat(stat);
889294Seric }
890294Seric /*
8916974Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
892294Seric **
8936974Seric **	This can be made an arbitrary message separator by changing $l
894294Seric **
8956974Seric **	One of the ugliest hacks seen by human eyes is
8966974Seric **	contained herein: UUCP wants those stupid
8976974Seric **	"remote from <host>" lines.  Why oh why does a
8986974Seric **	well-meaning programmer such as myself have to
8996974Seric **	deal with this kind of antique garbage????
9006974Seric **
901294Seric **	Parameters:
9026974Seric **		fp -- the file to output to.
9036974Seric **		m -- the mailer describing this entry.
904294Seric **
905294Seric **	Returns:
9066974Seric **		none
907294Seric **
908294Seric **	Side Effects:
9096974Seric **		outputs some text to fp.
910294Seric */
911294Seric 
9126974Seric putfromline(fp, m)
9136974Seric 	register FILE *fp;
9146974Seric 	register MAILER *m;
915294Seric {
9166974Seric 	char buf[MAXLINE];
917294Seric 
9186974Seric 	if (bitset(M_NHDR, m->m_flags))
9196974Seric 		return;
9204315Seric 
9216974Seric # ifdef UGLYUUCP
9226974Seric 	if (bitset(M_UGLYUUCP, m->m_flags))
9234205Seric 	{
9246974Seric 		extern char *macvalue();
9256974Seric 		char *sys = macvalue('g');
9266974Seric 		char *bang = index(sys, '!');
9276041Seric 
9286974Seric 		if (bang == NULL)
9296974Seric 			syserr("No ! in UUCP! (%s)", sys);
9305099Seric 		else
9316974Seric 			*bang = '\0';
9326974Seric 		expand("From $f  $d remote from $g", buf,
9336974Seric 				&buf[sizeof buf - 1], CurEnv);
9346974Seric 		*bang = '!';
9356974Seric 	}
9366974Seric 	else
9375179Seric # endif UGLYUUCP
9386974Seric 		expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv);
9396974Seric 	fputs(buf, fp);
9405981Seric }
9415981Seric /*
9426974Seric **  PUTHEADER -- put the header part of a message from the in-core copy
9435981Seric **
9445981Seric **	Parameters:
9455981Seric **		fp -- file to put it on.
9465981Seric **		m -- mailer to use.
9476974Seric **		e -- envelope to use.
9485981Seric **
9495981Seric **	Returns:
9505981Seric **		none.
9515981Seric **
9525981Seric **	Side Effects:
9535981Seric **		none.
9545981Seric */
9555981Seric 
9566974Seric putheader(fp, m, e)
9575981Seric 	register FILE *fp;
9585981Seric 	register struct mailer *m;
9596974Seric 	register ENVELOPE *e;
9605981Seric {
9615981Seric 	char buf[BUFSIZ];
9625981Seric 	register HDR *h;
9635981Seric 	extern char *arpadate();
9645981Seric 	extern char *capitalize();
9655981Seric 	extern char *hvalue();
9665981Seric 	extern bool samefrom();
9675981Seric 	char *of_line;
9685981Seric 
9694447Seric 	of_line = hvalue("original-from");
9706974Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
9711828Seric 	{
9724315Seric 		register char *p;
9736974Seric 		char *origfrom = e->e_origfrom;
9744447Seric 		bool nooutput;
9754315Seric 
9764447Seric 		nooutput = FALSE;
9773389Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
9784447Seric 			nooutput = TRUE;
9794447Seric 
9804447Seric 		/* use From: line from message if generated is the same */
9814370Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
9824447Seric 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
9833385Seric 		{
9844370Seric 			p = origfrom;
9854370Seric 			origfrom = NULL;
9864370Seric 		}
9874370Seric 		else if (bitset(H_DEFAULT, h->h_flags))
9884370Seric 		{
9896974Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
9903385Seric 			p = buf;
9913385Seric 		}
9925913Seric 		else if (bitset(H_ADDR, h->h_flags))
9935913Seric 		{
9945913Seric 			register int opos;
9955913Seric 			bool firstone = TRUE;
9965913Seric 
9975913Seric 			/*
9985913Seric 			**  Output the address list translated by the
9995913Seric 			**  mailer and with commas.
10005913Seric 			*/
10015913Seric 
10025913Seric 			p = h->h_value;
10035913Seric 			if (p == NULL || *p == '\0' || nooutput)
10045913Seric 				continue;
10055913Seric 			fprintf(fp, "%s: ", capitalize(h->h_field));
10065913Seric 			opos = strlen(h->h_field) + 2;
10075913Seric 			while (*p != '\0')
10085913Seric 			{
10095913Seric 				register char *name = p;
10105913Seric 				extern char *remotename();
10115913Seric 				char savechar;
10125913Seric 
10135913Seric 				/* find the end of the name */
10145936Seric 				while (*p != '\0' && *p != ',')
10155936Seric 				{
10165936Seric 					extern bool isatword();
10175936Seric 					char *oldp;
10185936Seric 
10196974Seric 					if (!e->e_oldstyle || !isspace(*p))
10205936Seric 					{
10215936Seric 						p++;
10225936Seric 						continue;
10235936Seric 					}
10245936Seric 					oldp = p;
10255936Seric 					while (*p != '\0' && isspace(*p))
10265936Seric 						p++;
10275936Seric 					if (*p != '@' && !isatword(p))
10285936Seric 					{
10295936Seric 						p = oldp;
10305936Seric 						break;
10315936Seric 					}
10325936Seric 					p += *p == '@' ? 1 : 2;
10335936Seric 					while (*p != '\0' && isspace(*p))
10345936Seric 						p++;
10355936Seric 				}
10365913Seric 				savechar = *p;
10375913Seric 				*p = '\0';
10385913Seric 
10395913Seric 				/* translate the name to be relative */
10406820Seric 				name = remotename(name, m, FALSE);
10415913Seric 				if (*name == '\0')
10425913Seric 					continue;
10435913Seric 
10445913Seric 				/* output the name with nice formatting */
10455913Seric 				opos += strlen(name);
10465913Seric 				if (!firstone)
10475913Seric 					opos += 2;
10485913Seric 				if (opos > 78 && !firstone)
10495913Seric 				{
10505913Seric 					fprintf(fp, ",\n        ");
10515916Seric 					opos = 8 + strlen(name);
10525913Seric 				}
10535913Seric 				else if (!firstone)
10545913Seric 					fprintf(fp, ", ");
10555913Seric 				fprintf(fp, "%s", name);
10565913Seric 				firstone = FALSE;
10575913Seric 
10585913Seric 				/* clean up the source string */
10595913Seric 				*p = savechar;
10605913Seric 				while (*p != '\0' && (isspace(*p) || *p == ','))
10615913Seric 					p++;
10625913Seric 			}
10635913Seric 			fprintf(fp, "\n");
10645913Seric 			nooutput = TRUE;
10655913Seric 		}
10662898Seric 		else
10673385Seric 			p = h->h_value;
10684447Seric 		if (p == NULL || *p == '\0')
10693389Seric 			continue;
10704209Seric 
10714209Seric 		/* hack, hack -- output Original-From field if different */
10724447Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
10734209Seric 		{
10744447Seric 			/* output new Original-From line if needed */
10754447Seric 			if (of_line == NULL && !samefrom(p, origfrom))
10764447Seric 				fprintf(fp, "Original-From: %s\n", origfrom);
10774447Seric 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
10784447Seric 			{
10794447Seric 				/* delete Original-From: line if redundant */
10804447Seric 				p = of_line;
10814447Seric 				of_line = NULL;
10824447Seric 			}
10834447Seric 		}
10844447Seric 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
10854447Seric 			nooutput = TRUE;
10864447Seric 
10874447Seric 		/* finally, output the header line */
10884447Seric 		if (!nooutput)
10894447Seric 		{
10904447Seric 			fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
10914447Seric 			h->h_flags |= H_USED;
10924209Seric 		}
10932898Seric 	}
1094294Seric }
1095294Seric /*
10966974Seric **  PUTBODY -- put the body of a message.
10976974Seric **
10986974Seric **	Parameters:
10996974Seric **		fp -- file to output onto.
11006974Seric **		m -- a mailer descriptor.
11016974Seric **		xdot -- if set, use SMTP hidden dot algorithm.
11026974Seric **
11036974Seric **	Returns:
11046974Seric **		none.
11056974Seric **
11066974Seric **	Side Effects:
11076974Seric **		The message is written onto fp.
11086974Seric */
11096974Seric 
11106974Seric putbody(fp, m, xdot)
11116974Seric 	FILE *fp;
11126974Seric 	struct mailer *m;
11136974Seric 	bool xdot;
11146974Seric {
11156974Seric 	char buf[MAXLINE + 1];
11166974Seric 
11176974Seric 	/*
11186974Seric 	**  Output the body of the message
11196974Seric 	*/
11206974Seric 
1121*7003Seric #ifdef lint
1122*7003Seric 	/* m will be needed later for complete smtp emulation */
1123*7003Seric 	if (m == NULL)
1124*7003Seric 		return;
1125*7003Seric #endif lint
1126*7003Seric 
11276974Seric 	if (TempFile != NULL)
11286974Seric 	{
11296974Seric 		rewind(TempFile);
11306974Seric 		buf[0] = '.';
11316974Seric 		while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL)
11326974Seric 			fputs((xdot && buf[1] == '.') ? buf : &buf[1], fp);
11336974Seric 
11346974Seric 		if (ferror(TempFile))
11356974Seric 		{
11366974Seric 			syserr("putbody: read error");
11376974Seric 			ExitStat = EX_IOERR;
11386974Seric 		}
11396974Seric 	}
11406974Seric 
11416974Seric 	(void) fflush(fp);
11426974Seric 	if (ferror(fp) && errno != EPIPE)
11436974Seric 	{
11446974Seric 		syserr("putbody: write error");
11456974Seric 		ExitStat = EX_IOERR;
11466974Seric 	}
11476974Seric 	errno = 0;
11486974Seric }
11496974Seric /*
11505936Seric **  ISATWORD -- tell if the word we are pointing to is "at".
11515936Seric **
11525936Seric **	Parameters:
11535936Seric **		p -- word to check.
11545936Seric **
11555936Seric **	Returns:
11565936Seric **		TRUE -- if p is the word at.
11575936Seric **		FALSE -- otherwise.
11585936Seric **
11595936Seric **	Side Effects:
11605936Seric **		none.
11615936Seric */
11625936Seric 
11635936Seric bool
11645936Seric isatword(p)
11655936Seric 	register char *p;
11665936Seric {
11675936Seric 	extern char lower();
11685936Seric 
11695936Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
11705936Seric 	    p[2] != '\0' && isspace(p[2]))
11715936Seric 		return (TRUE);
11725936Seric 	return (FALSE);
11735936Seric }
11745936Seric /*
11755913Seric **  REMOTENAME -- return the name relative to the current mailer
11765913Seric **
11775913Seric **	Parameters:
11785913Seric **		name -- the name to translate.
11796820Seric **		force -- if set, forces rewriting even if the mailer
11806820Seric **			does not request it.  Used for rewriting
11816820Seric **			sender addresses.
11825913Seric **
11835913Seric **	Returns:
11845913Seric **		the text string representing this address relative to
11855913Seric **			the receiving mailer.
11865913Seric **
11875913Seric **	Side Effects:
11885913Seric **		none.
11895913Seric **
11905913Seric **	Warnings:
11915913Seric **		The text string returned is tucked away locally;
11925913Seric **			copy it if you intend to save it.
11935913Seric */
11945913Seric 
11955913Seric char *
11966820Seric remotename(name, m, force)
11975913Seric 	char *name;
11985916Seric 	struct mailer *m;
11996820Seric 	bool force;
12005913Seric {
12015913Seric 	static char buf[MAXNAME];
12025913Seric 	char lbuf[MAXNAME];
12035913Seric 	extern char *macvalue();
12045913Seric 	char *oldf = macvalue('f');
12055916Seric 	char *oldx = macvalue('x');
12065916Seric 	char *oldg = macvalue('g');
12075913Seric 	extern char **prescan();
12085913Seric 	register char **pvp;
12095916Seric 	extern char *getxpart();
1210*7003Seric 	extern ADDRESS *buildaddr();
12115913Seric 
12125913Seric 	/*
12136820Seric 	**  See if this mailer wants the name to be rewritten.  There are
12146820Seric 	**  many problems here, owing to the standards for doing replies.
12156820Seric 	**  In general, these names should only be rewritten if we are
12166820Seric 	**  sending to another host that runs sendmail.
12176820Seric 	*/
12186820Seric 
12196820Seric 	if (!bitset(M_RELRCPT, m->m_flags) && !force)
1220*7003Seric 		return (name);
12216820Seric 
12226820Seric 	/*
12235913Seric 	**  Do general rewriting of name.
12245913Seric 	**	This will also take care of doing global name translation.
12255913Seric 	*/
12265913Seric 
12275916Seric 	define('x', getxpart(name));
12285913Seric 	pvp = prescan(name, '\0');
12295913Seric 	for (;;)
12305913Seric 	{
12315913Seric 		rewrite(pvp, 1);
12325913Seric 		rewrite(pvp, 3);
12335913Seric 		if (**pvp == CANONNET)
12345913Seric 		{
12355913Seric 			auto ADDRESS a;
12365913Seric 			register char *p;
12375913Seric 			extern char *hostalias();
12385913Seric 
12395913Seric 			/* oops... resolved to something */
12405913Seric 			if (buildaddr(pvp, &a) == NULL)
12415913Seric 				return (name);
12425913Seric 			p = hostalias(&a);
12435913Seric 			if (p == NULL)
12445913Seric 				return (name);
12455913Seric 			pvp = prescan(p, '\0');
12465913Seric 		}
12475913Seric 		else
12485913Seric 		{
12495913Seric 			cataddr(pvp, lbuf, sizeof lbuf);
12505913Seric 			break;
12515913Seric 		}
12525913Seric 	}
12535913Seric 
12545913Seric 	/* make the name relative to the receiving mailer */
12555913Seric 	define('f', lbuf);
12566974Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
12575913Seric 
12585913Seric 	/* rewrite to get rid of garbage we added in the expand above */
12595913Seric 	pvp = prescan(buf, '\0');
12605913Seric 	rewrite(pvp, 2);
12615916Seric 	cataddr(pvp, lbuf, sizeof lbuf);
12625913Seric 
12635916Seric 	/* now add any comment info we had before back */
12645916Seric 	define('g', lbuf);
12656974Seric 	expand("$q", buf, &buf[sizeof buf - 1], CurEnv);
12665916Seric 
12675913Seric 	define('f', oldf);
12685916Seric 	define('g', oldg);
12695916Seric 	define('x', oldx);
12705913Seric 
12715913Seric # ifdef DEBUG
12725913Seric 	if (Debug > 0)
12735913Seric 		printf("remotename(%s) => `%s'\n", name, buf);
12745913Seric # endif DEBUG
12755913Seric 	return (buf);
12765913Seric }
12775913Seric /*
12784370Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
12794370Seric **
12804370Seric **	Parameters:
12814370Seric **		ifrom -- internally generated form of from address.
12824370Seric **		efrom -- external form of from address.
12834370Seric **
12844370Seric **	Returns:
12854370Seric **		TRUE -- if they convey the same info.
12864370Seric **		FALSE -- if any information has been lost.
12874370Seric **
12884370Seric **	Side Effects:
12894370Seric **		none.
12904370Seric */
12914370Seric 
12924370Seric bool
12934370Seric samefrom(ifrom, efrom)
12944370Seric 	char *ifrom;
12954370Seric 	char *efrom;
12964370Seric {
12974447Seric 	register char *p;
12984447Seric 	char buf[MAXNAME + 4];
12994447Seric 
13004447Seric # ifdef DEBUG
13014447Seric 	if (Debug > 7)
13024447Seric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
13034447Seric # endif DEBUG
13044447Seric 	if (strcmp(ifrom, efrom) == 0)
13054447Seric 		goto success;
13064447Seric 	p = index(ifrom, '@');
13074447Seric 	if (p == NULL)
13084447Seric 		goto failure;
13094447Seric 	*p = '\0';
1310*7003Seric 	(void) strcpy(buf, ifrom);
1311*7003Seric 	(void) strcat(buf, " at ");
13124447Seric 	*p++ = '@';
1313*7003Seric 	(void) strcat(buf, p);
13144447Seric 	if (strcmp(buf, efrom) == 0)
13154447Seric 		goto success;
13164447Seric 
13174447Seric   failure:
13184447Seric # ifdef DEBUG
13194447Seric 	if (Debug > 7)
13204447Seric 		printf("FALSE\n");
13214447Seric # endif DEBUG
13224447Seric 	return (FALSE);
13234447Seric 
13244447Seric   success:
13254447Seric # ifdef DEBUG
13264447Seric 	if (Debug > 7)
13274447Seric 		printf("TRUE\n");
13284447Seric # endif DEBUG
13294447Seric 	return (TRUE);
13304370Seric }
13314370Seric /*
1332294Seric **  MAILFILE -- Send a message to a file.
1333294Seric **
13344327Seric **	If the file has the setuid/setgid bits set, but NO execute
13354327Seric **	bits, sendmail will try to become the owner of that file
13364327Seric **	rather than the real user.  Obviously, this only works if
13374327Seric **	sendmail runs as root.
13384327Seric **
1339294Seric **	Parameters:
1340294Seric **		filename -- the name of the file to send to.
13414397Seric **		ctladdr -- the controlling address header -- includes
13424397Seric **			the userid/groupid to be when sending.
1343294Seric **
1344294Seric **	Returns:
1345294Seric **		The exit code associated with the operation.
1346294Seric **
1347294Seric **	Side Effects:
1348294Seric **		none.
1349294Seric */
1350294Seric 
13514397Seric mailfile(filename, ctladdr)
1352294Seric 	char *filename;
13534397Seric 	ADDRESS *ctladdr;
1354294Seric {
1355294Seric 	register FILE *f;
13564214Seric 	register int pid;
1357294Seric 
13584214Seric 	/*
13594214Seric 	**  Fork so we can change permissions here.
13604214Seric 	**	Note that we MUST use fork, not vfork, because of
13614214Seric 	**	the complications of calling subroutines, etc.
13624214Seric 	*/
13634067Seric 
13644214Seric 	DOFORK(fork);
13654214Seric 
13664214Seric 	if (pid < 0)
13674214Seric 		return (EX_OSERR);
13684214Seric 	else if (pid == 0)
13694214Seric 	{
13704214Seric 		/* child -- actually write to file */
13714327Seric 		struct stat stb;
13724327Seric 
13734215Seric 		(void) signal(SIGINT, SIG_DFL);
13744215Seric 		(void) signal(SIGHUP, SIG_DFL);
13754215Seric 		(void) signal(SIGTERM, SIG_DFL);
13764327Seric 		umask(OldUmask);
13774327Seric 		if (stat(filename, &stb) < 0)
13784431Seric 			stb.st_mode = 0666;
13794327Seric 		if (bitset(0111, stb.st_mode))
13804327Seric 			exit(EX_CANTCREAT);
13814401Seric 		if (ctladdr == NULL)
13826900Seric 			ctladdr = &CurEnv->e_from;
13834327Seric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
13844417Seric 		{
13854417Seric 			if (ctladdr->q_uid == 0)
13864417Seric 				(void) setgid(DefGid);
13874417Seric 			else
13884417Seric 				(void) setgid(ctladdr->q_gid);
13894417Seric 		}
13904327Seric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
13914417Seric 		{
13924417Seric 			if (ctladdr->q_uid == 0)
13934417Seric 				(void) setuid(DefUid);
13944417Seric 			else
13954417Seric 				(void) setuid(ctladdr->q_uid);
13964417Seric 		}
13976887Seric 		f = dfopen(filename, "a");
13984214Seric 		if (f == NULL)
13994214Seric 			exit(EX_CANTCREAT);
14004214Seric 
14016974Seric 		putfromline(f, Mailer[1]);
14026974Seric 		(*CurEnv->e_puthdr)(f, Mailer[1], CurEnv);
14036974Seric 		fprintf(f, "\n");
14046974Seric 		(*CurEnv->e_putbody)(f, Mailer[1], FALSE);
14054214Seric 		fputs("\n", f);
14064214Seric 		(void) fclose(f);
14074214Seric 		(void) fflush(stdout);
14084417Seric 
14096887Seric 		/* reset ISUID & ISGID bits for paranoid systems */
14104621Seric 		(void) chmod(filename, (int) stb.st_mode);
14114214Seric 		exit(EX_OK);
14124315Seric 		/*NOTREACHED*/
14134214Seric 	}
14144214Seric 	else
14154214Seric 	{
14164214Seric 		/* parent -- wait for exit status */
14174214Seric 		register int i;
14184214Seric 		auto int stat;
14194214Seric 
14204214Seric 		while ((i = wait(&stat)) != pid)
14214214Seric 		{
14224214Seric 			if (i < 0)
14234214Seric 			{
14244214Seric 				stat = EX_OSERR << 8;
14254214Seric 				break;
14264214Seric 			}
14274214Seric 		}
14284215Seric 		if ((stat & 0377) != 0)
14294215Seric 			stat = EX_UNAVAILABLE << 8;
14304214Seric 		return ((stat >> 8) & 0377);
14314214Seric 	}
1432294Seric }
14334550Seric /*
14344550Seric **  SENDALL -- actually send all the messages.
14354550Seric **
14364550Seric **	Parameters:
14374550Seric **		verifyonly -- if set, only give verification messages.
14384550Seric **
14394550Seric **	Returns:
14404550Seric **		none.
14414550Seric **
14424550Seric **	Side Effects:
14434550Seric **		Scans the send lists and sends everything it finds.
14444550Seric */
14454550Seric 
14464550Seric sendall(verifyonly)
14474550Seric 	bool verifyonly;
14484550Seric {
14495008Seric 	register ADDRESS *q;
14504550Seric 	typedef int (*fnptr)();
14514550Seric 
14525032Seric # ifdef DEBUG
14535032Seric 	if (Debug > 1)
14545032Seric 	{
14556900Seric 		printf("\nSend Queue:\n");
14566900Seric 		printaddr(CurEnv->e_sendqueue, TRUE);
14575032Seric 	}
14585032Seric # endif DEBUG
14595008Seric 
14606900Seric 	for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
14614550Seric 	{
14625008Seric 		if (verifyonly)
14634550Seric 		{
14646900Seric 			CurEnv->e_to = q->q_paddr;
14655008Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
14664550Seric 			{
14675008Seric 				if (bitset(M_LOCAL, q->q_mailer->m_flags))
14685008Seric 					message(Arpa_Info, "deliverable");
14695008Seric 				else
14705008Seric 					message(Arpa_Info, "queueable");
14714550Seric 			}
14724550Seric 		}
14735008Seric 		else
14746974Seric 			(void) deliver(q);
14754550Seric 	}
14764550Seric }
1477