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*7344Seric SCCSID(@(#)deliver.c	3.93		07/02/82);
10405Seric 
11294Seric /*
124315Seric **  DELIVER -- Deliver a message to a list of addresses.
13294Seric **
144315Seric **	This routine delivers to everyone on the same host as the
154315Seric **	user on the head of the list.  It is clever about mailers
164315Seric **	that don't handle multiple users.  It is NOT guaranteed
174315Seric **	that it will deliver to all these addresses however -- so
184315Seric **	deliver should be called once for each address on the
194315Seric **	list.
204315Seric **
21294Seric **	Parameters:
224621Seric **		firstto -- head of the address list to deliver to.
23294Seric **
24294Seric **	Returns:
25294Seric **		zero -- successfully delivered.
26294Seric **		else -- some failure, see ExitStat for more info.
27294Seric **
28294Seric **	Side Effects:
29294Seric **		The standard input is passed off to someone.
30294Seric */
31294Seric 
326974Seric deliver(firstto)
334621Seric 	ADDRESS *firstto;
34294Seric {
354452Seric 	char *host;			/* host being sent to */
364452Seric 	char *user;			/* user being sent to */
37294Seric 	char **pvp;
383233Seric 	register char **mvp;
393233Seric 	register char *p;
404452Seric 	register struct mailer *m;	/* mailer for this recipient */
41294Seric 	register int i;
422968Seric 	extern bool checkcompat();
433233Seric 	char *pv[MAXPV+1];
444452Seric 	char tobuf[MAXLINE];		/* text line of to people */
453233Seric 	char buf[MAXNAME];
464397Seric 	ADDRESS *ctladdr;
474397Seric 	extern ADDRESS *getctladdr();
484452Seric 	char tfrombuf[MAXNAME];		/* translated from person */
494452Seric 	extern char **prescan();
504621Seric 	register ADDRESS *to = firstto;
514863Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
524863Seric 	bool tempfail = FALSE;
535032Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
547052Seric 	bool notopen = TRUE;		/* set if connection not quite open */
55294Seric 
564488Seric 	errno = 0;
577052Seric 	if (bitset(QDONTSEND, to->q_flags))
583233Seric 		return (0);
59294Seric 
606974Seric 	m = to->q_mailer;
616974Seric 	host = to->q_host;
626974Seric 
63294Seric # ifdef DEBUG
64294Seric 	if (Debug)
653233Seric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
666974Seric 			m->m_mno, host, to->q_user);
67294Seric # endif DEBUG
68294Seric 
69294Seric 	/*
705903Seric 	**  If this mailer is expensive, and if we don't want to make
715903Seric 	**  connections now, just mark these addresses and return.
725903Seric 	**	This is useful if we want to batch connections to
735903Seric 	**	reduce load.  This will cause the messages to be
745903Seric 	**	queued up, and a daemon will come along to send the
755903Seric 	**	messages later.
765903Seric 	**		This should be on a per-mailer basis.
775903Seric 	*/
785903Seric 
795903Seric 	if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags))
805903Seric 	{
815903Seric 		for (; to != NULL; to = to->q_next)
825903Seric 			if (!bitset(QDONTSEND, to->q_flags))
835903Seric 				to->q_flags |= QQUEUEUP|QDONTSEND;
845903Seric 		return (0);
855903Seric 	}
865903Seric 
875903Seric 	/*
883233Seric 	**  Do initial argv setup.
893233Seric 	**	Insert the mailer name.  Notice that $x expansion is
903233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
913233Seric 	**	a picky -f flag, we insert it as appropriate.  This
923233Seric 	**	code does not check for 'pv' overflow; this places a
933233Seric 	**	manifest lower limit of 4 for MAXPV.
945903Seric 	**		We rewrite the from address here, being careful
955903Seric 	**		to also rewrite it again using ruleset 2 to
965903Seric 	**		eliminate redundancies.
972968Seric 	*/
982968Seric 
994452Seric 	/* rewrite from address, using rewriting rules */
1006974Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
1014452Seric 	mvp = prescan(buf, '\0');
1024452Seric 	if (mvp == NULL)
1034452Seric 	{
1044452Seric 		syserr("bad mailer from translate \"%s\"", buf);
1054452Seric 		return (EX_SOFTWARE);
1064452Seric 	}
1074452Seric 	rewrite(mvp, 2);
1084452Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
1094452Seric 
1104452Seric 	define('g', tfrombuf);		/* translated sender address */
1113233Seric 	define('h', host);		/* to host */
1123233Seric 	Errors = 0;
1133233Seric 	pvp = pv;
1143233Seric 	*pvp++ = m->m_argv[0];
1152968Seric 
1163233Seric 	/* insert -f or -r flag as appropriate */
1173233Seric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1183233Seric 	{
1193233Seric 		if (bitset(M_FOPT, m->m_flags))
1203233Seric 			*pvp++ = "-f";
1213233Seric 		else
1223233Seric 			*pvp++ = "-r";
1236974Seric 		expand("$g", buf, &buf[sizeof buf - 1], CurEnv);
1243233Seric 		*pvp++ = newstr(buf);
1253233Seric 	}
126294Seric 
127294Seric 	/*
1283233Seric 	**  Append the other fixed parts of the argv.  These run
1293233Seric 	**  up to the first entry containing "$u".  There can only
1303233Seric 	**  be one of these, and there are only a few more slots
1313233Seric 	**  in the pv after it.
132294Seric 	*/
133294Seric 
1343233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
135294Seric 	{
1363233Seric 		while ((p = index(p, '$')) != NULL)
1373233Seric 			if (*++p == 'u')
1383233Seric 				break;
1393233Seric 		if (p != NULL)
1403233Seric 			break;
1413233Seric 
1423233Seric 		/* this entry is safe -- go ahead and process it */
1436974Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
1443233Seric 		*pvp++ = newstr(buf);
1453233Seric 		if (pvp >= &pv[MAXPV - 3])
1463233Seric 		{
1473233Seric 			syserr("Too many parameters to %s before $u", pv[0]);
1483233Seric 			return (-1);
1493233Seric 		}
150294Seric 	}
1514863Seric 
1526038Seric 	/*
1536038Seric 	**  If we have no substitution for the user name in the argument
1546038Seric 	**  list, we know that we must supply the names otherwise -- and
1556038Seric 	**  SMTP is the answer!!
1566038Seric 	*/
1576038Seric 
1583233Seric 	if (*mvp == NULL)
1594863Seric 	{
1604863Seric 		/* running SMTP */
1615179Seric # ifdef SMTP
1624863Seric 		clever = TRUE;
1634863Seric 		*pvp = NULL;
1645179Seric # else SMTP
1656038Seric 		/* oops!  we don't implement SMTP */
1665179Seric 		syserr("SMTP style mailer");
1675179Seric 		return (EX_SOFTWARE);
1685179Seric # endif SMTP
1694863Seric 	}
170294Seric 
171294Seric 	/*
1723233Seric 	**  At this point *mvp points to the argument with $u.  We
1733233Seric 	**  run through our address list and append all the addresses
1743233Seric 	**  we can.  If we run out of space, do not fret!  We can
1753233Seric 	**  always send another copy later.
176294Seric 	*/
177294Seric 
1783233Seric 	tobuf[0] = '\0';
1796900Seric 	CurEnv->e_to = tobuf;
1804397Seric 	ctladdr = NULL;
1813233Seric 	for (; to != NULL; to = to->q_next)
182294Seric 	{
1833233Seric 		/* avoid sending multiple recipients to dumb mailers */
1844382Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1853233Seric 			break;
1863233Seric 
1873233Seric 		/* if already sent or not for this host, don't send */
1887052Seric 		if (bitset(QDONTSEND, to->q_flags) ||
1897052Seric 		    strcmp(to->q_host, host) != 0 ||
1907052Seric 		    to->q_mailer != firstto->q_mailer)
1913233Seric 			continue;
1924397Seric 
1935032Seric # ifdef DEBUG
1945032Seric 		if (Debug)
1955032Seric 		{
1965032Seric 			printf("\nsend to ");
1975032Seric 			printaddr(to, FALSE);
1985032Seric 		}
1995032Seric # endif DEBUG
2005032Seric 
2014397Seric 		/* compute effective uid/gid when sending */
2024596Seric 		if (to->q_mailer == ProgMailer)
2034397Seric 			ctladdr = getctladdr(to);
2044397Seric 
2053233Seric 		user = to->q_user;
2066900Seric 		CurEnv->e_to = to->q_paddr;
2073233Seric 		to->q_flags |= QDONTSEND;
2083233Seric 
2093233Seric 		/*
2103233Seric 		**  Check to see that these people are allowed to
2113233Seric 		**  talk to each other.
2123233Seric 		*/
2133233Seric 
2143233Seric 		if (!checkcompat(to))
215294Seric 		{
2163233Seric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
2173233Seric 			continue;
218294Seric 		}
2193233Seric 
2203233Seric 		/*
2214099Seric 		**  Strip quote bits from names if the mailer is dumb
2224099Seric 		**	about them.
2233233Seric 		*/
2243233Seric 
2253233Seric 		if (bitset(M_STRIPQ, m->m_flags))
226294Seric 		{
2274099Seric 			stripquotes(user, TRUE);
2284099Seric 			stripquotes(host, TRUE);
2293233Seric 		}
2304099Seric 		else
2314099Seric 		{
2324099Seric 			stripquotes(user, FALSE);
2334099Seric 			stripquotes(host, FALSE);
2344099Seric 		}
2353233Seric 
2363233Seric 		/*
2377052Seric 		**  Do initial connection setup if needed.
2387052Seric 		*/
2397052Seric 
2407052Seric 		if (notopen)
2417052Seric 		{
2427052Seric 			message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
2437052Seric # ifdef SMTP
2447052Seric 			if (clever)
2457052Seric 			{
2467052Seric 				/* send the initial SMTP protocol */
2477052Seric 				i = smtpinit(m, pv, (ADDRESS *) NULL);
2487052Seric # ifdef QUEUE
2497052Seric 				if (i == EX_TEMPFAIL)
2507052Seric 					tempfail = TRUE;
2517052Seric # endif QUEUE
2527052Seric 			}
2537052Seric # ifdef SMTP
2547052Seric 			notopen = FALSE;
2557052Seric 		}
2567052Seric 
2577052Seric 		/*
2586059Seric 		**  Pass it to the other host if we are running SMTP.
2596059Seric 		*/
2606059Seric 
2616059Seric 		if (clever)
2626059Seric 		{
2636059Seric # ifdef SMTP
2646059Seric 			i = smtprcpt(to);
2656059Seric 			if (i != EX_OK)
2666059Seric 			{
2676059Seric # ifdef QUEUE
2686059Seric 				if (i == EX_TEMPFAIL)
2696059Seric 					to->q_flags |= QQUEUEUP;
2706059Seric 				else
2716059Seric # endif QUEUE
2726059Seric 					to->q_flags |= QBADADDR;
2737293Seric 				giveresponse(i, TRUE, m);
2746059Seric 			}
2756059Seric # else SMTP
2766059Seric 			syserr("trying to be clever");
2776059Seric # endif SMTP
2786059Seric 		}
2796059Seric 
2806059Seric 		/*
2814161Seric 		**  If an error message has already been given, don't
2824161Seric 		**	bother to send to this address.
2834161Seric 		**
2844161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
2854161Seric 		**	>> NOTE >> cannot do any further aliasing; that
2864161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
2874161Seric 		*/
2884161Seric 
2897293Seric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2904161Seric 			continue;
2914161Seric 
2924283Seric 		/* save statistics.... */
2934596Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2946900Seric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
2954283Seric 
2964161Seric 		/*
2973233Seric 		**  See if this user name is "special".
2983233Seric 		**	If the user name has a slash in it, assume that this
2996974Seric 		**	is a file -- send it off without further ado.  Note
3006974Seric 		**	that this type of addresses is not processed along
3016974Seric 		**	with the others, so we fudge on the To person.
3023233Seric 		*/
3033233Seric 
3044596Seric 		if (m == LocalMailer)
3053233Seric 		{
3065599Seric 			if (user[0] == '/')
307294Seric 			{
3084397Seric 				i = mailfile(user, getctladdr(to));
309294Seric 				giveresponse(i, TRUE, m);
3103233Seric 				continue;
311294Seric 			}
312294Seric 		}
3133233Seric 
3144315Seric 		/*
3154315Seric 		**  Address is verified -- add this user to mailer
3164315Seric 		**  argv, and add it to the print list of recipients.
3174315Seric 		*/
3184315Seric 
3196059Seric 		/* link together the chain of recipients */
3206272Seric 		to->q_tchain = tochain;
3216272Seric 		tochain = to;
3226059Seric 
3233233Seric 		/* create list of users for error messages */
3243233Seric 		if (tobuf[0] != '\0')
3254082Seric 			(void) strcat(tobuf, ",");
3264082Seric 		(void) strcat(tobuf, to->q_paddr);
3273233Seric 		define('u', user);		/* to user */
3284078Seric 		define('z', to->q_home);	/* user's home */
3293233Seric 
3304863Seric 		/*
3316059Seric 		**  Expand out this user into argument list.
3324863Seric 		*/
3334863Seric 
3346059Seric 		if (!clever)
3353233Seric 		{
3366974Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3374863Seric 			*pvp++ = newstr(buf);
3384863Seric 			if (pvp >= &pv[MAXPV - 2])
3394863Seric 			{
3404863Seric 				/* allow some space for trailing parms */
3414863Seric 				break;
3424863Seric 			}
3434863Seric 		}
344294Seric 	}
345294Seric 
3464067Seric 	/* see if any addresses still exist */
3474067Seric 	if (tobuf[0] == '\0')
3484863Seric 	{
3495179Seric # ifdef SMTP
3504863Seric 		if (clever)
3517228Seric 			smtpquit(pv[0], FALSE);
3525179Seric # endif SMTP
3537003Seric 		define('g', (char *) NULL);
3544067Seric 		return (0);
3554863Seric 	}
3564067Seric 
3573233Seric 	/* print out messages as full list */
3586900Seric 	CurEnv->e_to = tobuf;
3593233Seric 
360294Seric 	/*
3613233Seric 	**  Fill out any parameters after the $u parameter.
362294Seric 	*/
363294Seric 
3644863Seric 	while (!clever && *++mvp != NULL)
365294Seric 	{
3666974Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3673233Seric 		*pvp++ = newstr(buf);
3683233Seric 		if (pvp >= &pv[MAXPV])
3693233Seric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
370294Seric 	}
3713233Seric 	*pvp++ = NULL;
372294Seric 
373294Seric 	/*
374294Seric 	**  Call the mailer.
3752898Seric 	**	The argument vector gets built, pipes
376294Seric 	**	are created as necessary, and we fork & exec as
3772898Seric 	**	appropriate.
3784863Seric 	**	If we are running SMTP, we just need to clean up.
379294Seric 	*/
380294Seric 
3814397Seric 	if (ctladdr == NULL)
3826900Seric 		ctladdr = &CurEnv->e_from;
3835179Seric # ifdef SMTP
3844863Seric 	if (clever)
3854863Seric 	{
3866974Seric 		i = smtpfinish(m, CurEnv);
3877228Seric 		smtpquit(pv[0], TRUE);
3884863Seric 	}
3894863Seric 	else
3905179Seric # endif SMTP
3916974Seric 		i = sendoff(m, pv, ctladdr);
3923233Seric 
3934621Seric 	/*
3944621Seric 	**  If we got a temporary failure, arrange to queue the
3954621Seric 	**  addressees.
3964621Seric 	*/
3974621Seric 
3985179Seric # ifdef QUEUE
3994621Seric 	if (i == EX_TEMPFAIL)
4004621Seric 	{
4015032Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
4024621Seric 			to->q_flags |= QQUEUEUP;
4034621Seric 	}
4045179Seric # endif QUEUE
4054621Seric 
4064488Seric 	errno = 0;
4077003Seric 	define('g', (char *) NULL);
4083233Seric 	return (i);
4093233Seric }
4103233Seric /*
4114214Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4124214Seric **
4134214Seric **	This MUST be a macro, since after a vfork we are running
4144214Seric **	two processes on the same stack!!!
4154214Seric **
4164214Seric **	Parameters:
4174214Seric **		none.
4184214Seric **
4194214Seric **	Returns:
4204214Seric **		From a macro???  You've got to be kidding!
4214214Seric **
4224214Seric **	Side Effects:
4234214Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4244214Seric **			pid of child in parent, zero in child.
4254214Seric **			-1 on unrecoverable error.
4264214Seric **
4274214Seric **	Notes:
4284214Seric **		I'm awfully sorry this looks so awful.  That's
4294214Seric **		vfork for you.....
4304214Seric */
4314214Seric 
4324214Seric # define NFORKTRIES	5
4334214Seric # ifdef VFORK
4344214Seric # define XFORK	vfork
4354214Seric # else VFORK
4364214Seric # define XFORK	fork
4374214Seric # endif VFORK
4384214Seric 
4394214Seric # define DOFORK(fORKfN) \
4404214Seric {\
4414214Seric 	register int i;\
4424214Seric \
4434214Seric 	for (i = NFORKTRIES; i-- > 0; )\
4444214Seric 	{\
4454214Seric 		pid = fORKfN();\
4464214Seric 		if (pid >= 0)\
4474214Seric 			break;\
4487003Seric 		sleep(NFORKTRIES - i);\
4494214Seric 	}\
4504214Seric }
4514214Seric /*
4524637Seric **  DOFORK -- simple fork interface to DOFORK.
4534637Seric **
4544637Seric **	Parameters:
4554637Seric **		none.
4564637Seric **
4574637Seric **	Returns:
4584637Seric **		pid of child in parent.
4594637Seric **		zero in child.
4604637Seric **		-1 on error.
4614637Seric **
4624637Seric **	Side Effects:
4634637Seric **		returns twice, once in parent and once in child.
4644637Seric */
4654637Seric 
4664637Seric dofork()
4674637Seric {
4684637Seric 	register int pid;
4694637Seric 
4704637Seric 	DOFORK(fork);
4714637Seric 	return (pid);
4724637Seric }
4734637Seric /*
4743233Seric **  SENDOFF -- send off call to mailer & collect response.
4753233Seric **
4763233Seric **	Parameters:
4773233Seric **		m -- mailer descriptor.
4783233Seric **		pvp -- parameter vector to send to it.
4794397Seric **		ctladdr -- an address pointer controlling the
4804397Seric **			user/groupid etc. of the mailer.
4813233Seric **
4823233Seric **	Returns:
4833233Seric **		exit status of mailer.
4843233Seric **
4853233Seric **	Side Effects:
4863233Seric **		none.
4873233Seric */
4883233Seric 
4896974Seric sendoff(m, pvp, ctladdr)
4903233Seric 	struct mailer *m;
4913233Seric 	char **pvp;
4924397Seric 	ADDRESS *ctladdr;
4933233Seric {
4944863Seric 	auto FILE *mfile;
4954863Seric 	auto FILE *rfile;
4963233Seric 	register int i;
4973233Seric 	int pid;
4984863Seric 
4994863Seric 	/*
5004863Seric 	**  Create connection to mailer.
5014863Seric 	*/
5024863Seric 
5034863Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
5044863Seric 	if (pid < 0)
5054863Seric 		return (-1);
5064863Seric 
5074863Seric 	/*
5084863Seric 	**  Format and send message.
5094863Seric 	*/
5104863Seric 
5114863Seric 	(void) signal(SIGPIPE, SIG_IGN);
5126974Seric 	putfromline(mfile, m);
5136974Seric 	(*CurEnv->e_puthdr)(mfile, m, CurEnv);
5146974Seric 	fprintf(mfile, "\n");
5156974Seric 	(*CurEnv->e_putbody)(mfile, m, FALSE);
5164863Seric 	(void) fclose(mfile);
5174863Seric 
5184863Seric 	i = endmailer(pid, pvp[0]);
5194863Seric 	giveresponse(i, TRUE, m);
5205981Seric 
5215981Seric 	/* arrange a return receipt if requested */
5226900Seric 	if (CurEnv->e_retreceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK)
5235981Seric 	{
5246900Seric 		CurEnv->e_sendreceipt = TRUE;
5256900Seric 		fprintf(Xscript, "%s... successfully delivered\n", CurEnv->e_to);
5265981Seric 		/* do we want to send back more info? */
5275981Seric 	}
5285981Seric 
5294863Seric 	return (i);
5304863Seric }
5314863Seric /*
5324863Seric **  ENDMAILER -- Wait for mailer to terminate.
5334863Seric **
5344863Seric **	We should never get fatal errors (e.g., segmentation
5354863Seric **	violation), so we report those specially.  For other
5364863Seric **	errors, we choose a status message (into statmsg),
5374863Seric **	and if it represents an error, we print it.
5384863Seric **
5394863Seric **	Parameters:
5404863Seric **		pid -- pid of mailer.
5414863Seric **		name -- name of mailer (for error messages).
5424863Seric **
5434863Seric **	Returns:
5444863Seric **		exit code of mailer.
5454863Seric **
5464863Seric **	Side Effects:
5474863Seric **		none.
5484863Seric */
5494863Seric 
5504863Seric endmailer(pid, name)
5514863Seric 	int pid;
5524863Seric 	char *name;
5534863Seric {
5544863Seric 	register int i;
5554863Seric 	auto int st;
5564863Seric 
5576038Seric 	/* in the IPC case there is nothing to wait for */
5586038Seric 	if (pid == 0)
5596038Seric 		return (EX_OK);
5606038Seric 
5616038Seric 	/* wait for the mailer process to die and collect status */
5624863Seric 	while ((i = wait(&st)) > 0 && i != pid)
5634863Seric 		continue;
5644863Seric 	if (i < 0)
5654863Seric 	{
5664863Seric 		syserr("wait");
5674863Seric 		return (-1);
5684863Seric 	}
5696038Seric 
5706038Seric 	/* see if it died a horrid death */
5714863Seric 	if ((st & 0377) != 0)
5724863Seric 	{
5734863Seric 		syserr("%s: stat %o", name, st);
5744863Seric 		ExitStat = EX_UNAVAILABLE;
5754863Seric 		return (-1);
5764863Seric 	}
5776038Seric 
5786038Seric 	/* normal death -- return status */
5794863Seric 	i = (st >> 8) & 0377;
5804863Seric 	return (i);
5814863Seric }
5824863Seric /*
5834863Seric **  OPENMAILER -- open connection to mailer.
5844863Seric **
5854863Seric **	Parameters:
5864863Seric **		m -- mailer descriptor.
5874863Seric **		pvp -- parameter vector to pass to mailer.
5884863Seric **		ctladdr -- controlling address for user.
5894863Seric **		clever -- create a full duplex connection.
5904863Seric **		pmfile -- pointer to mfile (to mailer) connection.
5914863Seric **		prfile -- pointer to rfile (from mailer) connection.
5924863Seric **
5934863Seric **	Returns:
5946038Seric **		pid of mailer ( > 0 ).
5954863Seric **		-1 on error.
5966038Seric **		zero on an IPC connection.
5974863Seric **
5984863Seric **	Side Effects:
5994863Seric **		creates a mailer in a subprocess.
6004863Seric */
6014863Seric 
6024863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
6034863Seric 	struct mailer *m;
6044863Seric 	char **pvp;
6054863Seric 	ADDRESS *ctladdr;
6064863Seric 	bool clever;
6074863Seric 	FILE **pmfile;
6084863Seric 	FILE **prfile;
6094863Seric {
6104863Seric 	int pid;
6114709Seric 	int mpvect[2];
6124863Seric 	int rpvect[2];
6133233Seric 	FILE *mfile;
6144863Seric 	FILE *rfile;
6153233Seric 	extern FILE *fdopen();
6163233Seric 
6173233Seric # ifdef DEBUG
6183233Seric 	if (Debug)
619294Seric 	{
6204863Seric 		printf("openmailer:\n");
6213233Seric 		printav(pvp);
622294Seric 	}
6233233Seric # endif DEBUG
6244488Seric 	errno = 0;
6253233Seric 
6266038Seric # ifdef DAEMON
6276038Seric 	/*
6286038Seric 	**  Deal with the special case of mail handled through an IPC
6296038Seric 	**  connection.
6306038Seric 	**	In this case we don't actually fork.  We must be
6316038Seric 	**	running SMTP for this to work.  We will return a
6326038Seric 	**	zero pid to indicate that we are running IPC.
6336038Seric 	*/
6346038Seric 
6356038Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
6366038Seric 	{
6376038Seric 		register int i;
6387285Seric 		register u_short port;
6396038Seric 
6406038Seric 		if (!clever)
6416038Seric 			syserr("non-clever IPC");
6426632Seric 		if (pvp[2] != NULL)
6437285Seric 			port = atoi(pvp[2]);
6446632Seric 		else
6457285Seric 			port = 0;
6467285Seric 		i = makeconnection(pvp[1], port, pmfile, prfile);
6476038Seric 		if (i != EX_OK)
6486047Seric 		{
6496047Seric 			ExitStat = i;
6506038Seric 			return (-1);
6516047Seric 		}
6526038Seric 		else
6536038Seric 			return (0);
6546038Seric 	}
6556038Seric # endif DAEMON
6566038Seric 
6572898Seric 	/* create a pipe to shove the mail through */
6584709Seric 	if (pipe(mpvect) < 0)
659294Seric 	{
6604863Seric 		syserr("pipe (to mailer)");
661294Seric 		return (-1);
662294Seric 	}
6634863Seric 
6645179Seric # ifdef SMTP
6654863Seric 	/* if this mailer speaks smtp, create a return pipe */
6664863Seric 	if (clever && pipe(rpvect) < 0)
6674863Seric 	{
6684863Seric 		syserr("pipe (from mailer)");
6694863Seric 		(void) close(mpvect[0]);
6704863Seric 		(void) close(mpvect[1]);
6714863Seric 		return (-1);
6724863Seric 	}
6735179Seric # endif SMTP
6744863Seric 
6756038Seric 	/*
6766038Seric 	**  Actually fork the mailer process.
6776038Seric 	**	DOFORK is clever about retrying.
6786038Seric 	*/
6796038Seric 
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;
8147228Seric 	bool 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";
8387052Seric 		message(Arpa_Info, statmsg);
8394065Seric 	}
8405179Seric # ifdef QUEUE
8414621Seric 	else if (stat == EX_TEMPFAIL)
8424621Seric 	{
8437052Seric 		message(Arpa_Info, "transmission deferred");
8444621Seric 	}
8455179Seric # endif QUEUE
846294Seric 	else
847294Seric 	{
8481518Seric 		Errors++;
8496047Seric 		FatalErrors = TRUE;
850294Seric 		if (statmsg == NULL && m->m_badstat != 0)
851294Seric 		{
852294Seric 			stat = m->m_badstat;
853294Seric 			i = stat - EX__BASE;
854294Seric # ifdef DEBUG
855294Seric 			if (i < 0 || i >= N_SysEx)
856294Seric 				syserr("Bad m_badstat %d", stat);
857294Seric 			else
858294Seric # endif DEBUG
859*7344Seric 				statmsg = SysExMsg[i];
860294Seric 		}
861294Seric 		if (statmsg == NULL)
862294Seric 			usrerr("unknown mailer response %d", stat);
8634065Seric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
864294Seric 			usrerr("%s", statmsg);
865*7344Seric 		else
866*7344Seric 			fprintf(Xscript, "%s\n", 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';
9327232Seric 		expand("From $f  $d remote from $g\n", 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);
9397123Seric 	putline(buf, fp, bitset(M_FULLSMTP, m->m_flags));
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;
9687123Seric 	char obuf[MAXLINE];
9697123Seric 	register char *obp;
9707123Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
9715981Seric 
9724447Seric 	of_line = hvalue("original-from");
9736974Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
9741828Seric 	{
9754315Seric 		register char *p;
9766974Seric 		char *origfrom = e->e_origfrom;
9774447Seric 		bool nooutput;
9784315Seric 
9794447Seric 		nooutput = FALSE;
9803389Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
9814447Seric 			nooutput = TRUE;
9824447Seric 
9834447Seric 		/* use From: line from message if generated is the same */
9844370Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
9854447Seric 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
9863385Seric 		{
9874370Seric 			p = origfrom;
9884370Seric 			origfrom = NULL;
9894370Seric 		}
9904370Seric 		else if (bitset(H_DEFAULT, h->h_flags))
9914370Seric 		{
9926974Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
9933385Seric 			p = buf;
9943385Seric 		}
9955913Seric 		else if (bitset(H_ADDR, h->h_flags))
9965913Seric 		{
9975913Seric 			register int opos;
9985913Seric 			bool firstone = TRUE;
9995913Seric 
10005913Seric 			/*
10015913Seric 			**  Output the address list translated by the
10025913Seric 			**  mailer and with commas.
10035913Seric 			*/
10045913Seric 
10055913Seric 			p = h->h_value;
10065913Seric 			if (p == NULL || *p == '\0' || nooutput)
10075913Seric 				continue;
10087123Seric 			obp = obuf;
10097285Seric 			(void) sprintf(obp, "%s: ", capitalize(h->h_field));
10105913Seric 			opos = strlen(h->h_field) + 2;
10117123Seric 			obp += opos;
10125913Seric 			while (*p != '\0')
10135913Seric 			{
10145913Seric 				register char *name = p;
10155913Seric 				extern char *remotename();
10165913Seric 				char savechar;
10175913Seric 
10185913Seric 				/* find the end of the name */
10195936Seric 				while (*p != '\0' && *p != ',')
10205936Seric 				{
10215936Seric 					extern bool isatword();
10225936Seric 					char *oldp;
10235936Seric 
10246974Seric 					if (!e->e_oldstyle || !isspace(*p))
10255936Seric 					{
10265936Seric 						p++;
10275936Seric 						continue;
10285936Seric 					}
10295936Seric 					oldp = p;
10305936Seric 					while (*p != '\0' && isspace(*p))
10315936Seric 						p++;
10325936Seric 					if (*p != '@' && !isatword(p))
10335936Seric 					{
10345936Seric 						p = oldp;
10355936Seric 						break;
10365936Seric 					}
10375936Seric 					p += *p == '@' ? 1 : 2;
10385936Seric 					while (*p != '\0' && isspace(*p))
10395936Seric 						p++;
10405936Seric 				}
10415913Seric 				savechar = *p;
10425913Seric 				*p = '\0';
10435913Seric 
10445913Seric 				/* translate the name to be relative */
10456820Seric 				name = remotename(name, m, FALSE);
10465913Seric 				if (*name == '\0')
10475913Seric 					continue;
10485913Seric 
10495913Seric 				/* output the name with nice formatting */
10505913Seric 				opos += strlen(name);
10515913Seric 				if (!firstone)
10525913Seric 					opos += 2;
10535913Seric 				if (opos > 78 && !firstone)
10545913Seric 				{
10557123Seric 					(void) sprintf(obp, ",\n");
10567123Seric 					putline(obuf, fp, fullsmtp);
10577123Seric 					obp = obuf;
10587123Seric 					(void) sprintf(obp, "        ");
10597123Seric 					obp += strlen(obp);
10605916Seric 					opos = 8 + strlen(name);
10615913Seric 				}
10625913Seric 				else if (!firstone)
10637123Seric 				{
10647123Seric 					(void) sprintf(obp, ", ");
10657123Seric 					obp += 2;
10667123Seric 				}
10677123Seric 				(void) sprintf(obp, "%s", name);
10687123Seric 				obp += strlen(obp);
10695913Seric 				firstone = FALSE;
10705913Seric 
10715913Seric 				/* clean up the source string */
10725913Seric 				*p = savechar;
10735913Seric 				while (*p != '\0' && (isspace(*p) || *p == ','))
10745913Seric 					p++;
10755913Seric 			}
10767285Seric 			(void) strcpy(obp, "\n");
10777202Seric 			putline(obuf, fp, fullsmtp);
10785913Seric 			nooutput = TRUE;
10795913Seric 		}
10802898Seric 		else
10813385Seric 			p = h->h_value;
10824447Seric 		if (p == NULL || *p == '\0')
10833389Seric 			continue;
10844209Seric 
10854209Seric 		/* hack, hack -- output Original-From field if different */
10864447Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
10874209Seric 		{
10884447Seric 			/* output new Original-From line if needed */
10894447Seric 			if (of_line == NULL && !samefrom(p, origfrom))
10907123Seric 			{
10917123Seric 				(void) sprintf(obuf, "Original-From: %s\n", origfrom);
10927123Seric 				putline(obuf, fp, fullsmtp);
10937123Seric 			}
10944447Seric 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
10954447Seric 			{
10964447Seric 				/* delete Original-From: line if redundant */
10974447Seric 				p = of_line;
10984447Seric 				of_line = NULL;
10994447Seric 			}
11004447Seric 		}
11014447Seric 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
11024447Seric 			nooutput = TRUE;
11034447Seric 
11044447Seric 		/* finally, output the header line */
11054447Seric 		if (!nooutput)
11064447Seric 		{
11077123Seric 			(void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p);
11087123Seric 			putline(obuf, fp, fullsmtp);
11094447Seric 			h->h_flags |= H_USED;
11104209Seric 		}
11112898Seric 	}
1112294Seric }
1113294Seric /*
11146974Seric **  PUTBODY -- put the body of a message.
11156974Seric **
11166974Seric **	Parameters:
11176974Seric **		fp -- file to output onto.
11186974Seric **		m -- a mailer descriptor.
11196974Seric **		xdot -- if set, use SMTP hidden dot algorithm.
11206974Seric **
11216974Seric **	Returns:
11226974Seric **		none.
11236974Seric **
11246974Seric **	Side Effects:
11256974Seric **		The message is written onto fp.
11266974Seric */
11276974Seric 
11286974Seric putbody(fp, m, xdot)
11296974Seric 	FILE *fp;
11306974Seric 	struct mailer *m;
11316974Seric 	bool xdot;
11326974Seric {
11336974Seric 	char buf[MAXLINE + 1];
11347123Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
11356974Seric 
11366974Seric 	/*
11376974Seric 	**  Output the body of the message
11386974Seric 	*/
11396974Seric 
11407003Seric #ifdef lint
11417003Seric 	/* m will be needed later for complete smtp emulation */
11427003Seric 	if (m == NULL)
11437003Seric 		return;
11447003Seric #endif lint
11457003Seric 
11466974Seric 	if (TempFile != NULL)
11476974Seric 	{
11486974Seric 		rewind(TempFile);
11496974Seric 		buf[0] = '.';
11506974Seric 		while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL)
11517123Seric 			putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp);
11526974Seric 
11536974Seric 		if (ferror(TempFile))
11546974Seric 		{
11556974Seric 			syserr("putbody: read error");
11566974Seric 			ExitStat = EX_IOERR;
11576974Seric 		}
11586974Seric 	}
11596974Seric 
11606974Seric 	(void) fflush(fp);
11616974Seric 	if (ferror(fp) && errno != EPIPE)
11626974Seric 	{
11636974Seric 		syserr("putbody: write error");
11646974Seric 		ExitStat = EX_IOERR;
11656974Seric 	}
11666974Seric 	errno = 0;
11676974Seric }
11686974Seric /*
11695936Seric **  ISATWORD -- tell if the word we are pointing to is "at".
11705936Seric **
11715936Seric **	Parameters:
11725936Seric **		p -- word to check.
11735936Seric **
11745936Seric **	Returns:
11755936Seric **		TRUE -- if p is the word at.
11765936Seric **		FALSE -- otherwise.
11775936Seric **
11785936Seric **	Side Effects:
11795936Seric **		none.
11805936Seric */
11815936Seric 
11825936Seric bool
11835936Seric isatword(p)
11845936Seric 	register char *p;
11855936Seric {
11865936Seric 	extern char lower();
11875936Seric 
11885936Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
11895936Seric 	    p[2] != '\0' && isspace(p[2]))
11905936Seric 		return (TRUE);
11915936Seric 	return (FALSE);
11925936Seric }
11935936Seric /*
11945913Seric **  REMOTENAME -- return the name relative to the current mailer
11955913Seric **
11965913Seric **	Parameters:
11975913Seric **		name -- the name to translate.
11986820Seric **		force -- if set, forces rewriting even if the mailer
11996820Seric **			does not request it.  Used for rewriting
12006820Seric **			sender addresses.
12015913Seric **
12025913Seric **	Returns:
12035913Seric **		the text string representing this address relative to
12045913Seric **			the receiving mailer.
12055913Seric **
12065913Seric **	Side Effects:
12075913Seric **		none.
12085913Seric **
12095913Seric **	Warnings:
12105913Seric **		The text string returned is tucked away locally;
12115913Seric **			copy it if you intend to save it.
12125913Seric */
12135913Seric 
12145913Seric char *
12156820Seric remotename(name, m, force)
12165913Seric 	char *name;
12175916Seric 	struct mailer *m;
12186820Seric 	bool force;
12195913Seric {
12205913Seric 	static char buf[MAXNAME];
12215913Seric 	char lbuf[MAXNAME];
12225913Seric 	extern char *macvalue();
12235913Seric 	char *oldf = macvalue('f');
12245916Seric 	char *oldx = macvalue('x');
12255916Seric 	char *oldg = macvalue('g');
12265913Seric 	extern char **prescan();
12275913Seric 	register char **pvp;
12285916Seric 	extern char *getxpart();
12297003Seric 	extern ADDRESS *buildaddr();
12305913Seric 
12315913Seric 	/*
12326820Seric 	**  See if this mailer wants the name to be rewritten.  There are
12336820Seric 	**  many problems here, owing to the standards for doing replies.
12346820Seric 	**  In general, these names should only be rewritten if we are
12356820Seric 	**  sending to another host that runs sendmail.
12366820Seric 	*/
12376820Seric 
12386820Seric 	if (!bitset(M_RELRCPT, m->m_flags) && !force)
12397003Seric 		return (name);
12406820Seric 
12416820Seric 	/*
12425913Seric 	**  Do general rewriting of name.
12435913Seric 	**	This will also take care of doing global name translation.
12445913Seric 	*/
12455913Seric 
12465916Seric 	define('x', getxpart(name));
12475913Seric 	pvp = prescan(name, '\0');
12487259Seric 	if (pvp == NULL)
12497259Seric 		return (name);
12507048Seric 	rewrite(pvp, 1);
12517048Seric 	rewrite(pvp, 3);
12527048Seric 	if (**pvp == CANONNET)
12535913Seric 	{
12547048Seric 		/* oops... resolved to something */
12557048Seric 		return (name);
12565913Seric 	}
12577048Seric 	cataddr(pvp, lbuf, sizeof lbuf);
12585913Seric 
12595913Seric 	/* make the name relative to the receiving mailer */
12605913Seric 	define('f', lbuf);
12616974Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
12625913Seric 
12635913Seric 	/* rewrite to get rid of garbage we added in the expand above */
12645913Seric 	pvp = prescan(buf, '\0');
12657259Seric 	if (pvp == NULL)
12667259Seric 		return (name);
12675913Seric 	rewrite(pvp, 2);
12685916Seric 	cataddr(pvp, lbuf, sizeof lbuf);
12695913Seric 
12705916Seric 	/* now add any comment info we had before back */
12715916Seric 	define('g', lbuf);
12726974Seric 	expand("$q", buf, &buf[sizeof buf - 1], CurEnv);
12735916Seric 
12745913Seric 	define('f', oldf);
12755916Seric 	define('g', oldg);
12765916Seric 	define('x', oldx);
12775913Seric 
12785913Seric # ifdef DEBUG
12795913Seric 	if (Debug > 0)
12805913Seric 		printf("remotename(%s) => `%s'\n", name, buf);
12815913Seric # endif DEBUG
12825913Seric 	return (buf);
12835913Seric }
12845913Seric /*
12854370Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
12864370Seric **
12874370Seric **	Parameters:
12884370Seric **		ifrom -- internally generated form of from address.
12894370Seric **		efrom -- external form of from address.
12904370Seric **
12914370Seric **	Returns:
12924370Seric **		TRUE -- if they convey the same info.
12934370Seric **		FALSE -- if any information has been lost.
12944370Seric **
12954370Seric **	Side Effects:
12964370Seric **		none.
12974370Seric */
12984370Seric 
12994370Seric bool
13004370Seric samefrom(ifrom, efrom)
13014370Seric 	char *ifrom;
13024370Seric 	char *efrom;
13034370Seric {
13044447Seric 	register char *p;
13054447Seric 	char buf[MAXNAME + 4];
13064447Seric 
13074447Seric # ifdef DEBUG
13084447Seric 	if (Debug > 7)
13094447Seric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
13104447Seric # endif DEBUG
13114447Seric 	if (strcmp(ifrom, efrom) == 0)
13124447Seric 		goto success;
13134447Seric 	p = index(ifrom, '@');
13144447Seric 	if (p == NULL)
13154447Seric 		goto failure;
13164447Seric 	*p = '\0';
13177003Seric 	(void) strcpy(buf, ifrom);
13187003Seric 	(void) strcat(buf, " at ");
13194447Seric 	*p++ = '@';
13207003Seric 	(void) strcat(buf, p);
13214447Seric 	if (strcmp(buf, efrom) == 0)
13224447Seric 		goto success;
13234447Seric 
13244447Seric   failure:
13254447Seric # ifdef DEBUG
13264447Seric 	if (Debug > 7)
13274447Seric 		printf("FALSE\n");
13284447Seric # endif DEBUG
13294447Seric 	return (FALSE);
13304447Seric 
13314447Seric   success:
13324447Seric # ifdef DEBUG
13334447Seric 	if (Debug > 7)
13344447Seric 		printf("TRUE\n");
13354447Seric # endif DEBUG
13364447Seric 	return (TRUE);
13374370Seric }
13384370Seric /*
1339294Seric **  MAILFILE -- Send a message to a file.
1340294Seric **
13414327Seric **	If the file has the setuid/setgid bits set, but NO execute
13424327Seric **	bits, sendmail will try to become the owner of that file
13434327Seric **	rather than the real user.  Obviously, this only works if
13444327Seric **	sendmail runs as root.
13454327Seric **
1346294Seric **	Parameters:
1347294Seric **		filename -- the name of the file to send to.
13484397Seric **		ctladdr -- the controlling address header -- includes
13494397Seric **			the userid/groupid to be when sending.
1350294Seric **
1351294Seric **	Returns:
1352294Seric **		The exit code associated with the operation.
1353294Seric **
1354294Seric **	Side Effects:
1355294Seric **		none.
1356294Seric */
1357294Seric 
13584397Seric mailfile(filename, ctladdr)
1359294Seric 	char *filename;
13604397Seric 	ADDRESS *ctladdr;
1361294Seric {
1362294Seric 	register FILE *f;
13634214Seric 	register int pid;
1364294Seric 
13654214Seric 	/*
13664214Seric 	**  Fork so we can change permissions here.
13674214Seric 	**	Note that we MUST use fork, not vfork, because of
13684214Seric 	**	the complications of calling subroutines, etc.
13694214Seric 	*/
13704067Seric 
13714214Seric 	DOFORK(fork);
13724214Seric 
13734214Seric 	if (pid < 0)
13744214Seric 		return (EX_OSERR);
13754214Seric 	else if (pid == 0)
13764214Seric 	{
13774214Seric 		/* child -- actually write to file */
13784327Seric 		struct stat stb;
13794327Seric 
13804215Seric 		(void) signal(SIGINT, SIG_DFL);
13814215Seric 		(void) signal(SIGHUP, SIG_DFL);
13824215Seric 		(void) signal(SIGTERM, SIG_DFL);
13834327Seric 		umask(OldUmask);
13844327Seric 		if (stat(filename, &stb) < 0)
13854431Seric 			stb.st_mode = 0666;
13864327Seric 		if (bitset(0111, stb.st_mode))
13874327Seric 			exit(EX_CANTCREAT);
13884401Seric 		if (ctladdr == NULL)
13896900Seric 			ctladdr = &CurEnv->e_from;
13904327Seric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
13914417Seric 		{
13924417Seric 			if (ctladdr->q_uid == 0)
13934417Seric 				(void) setgid(DefGid);
13944417Seric 			else
13954417Seric 				(void) setgid(ctladdr->q_gid);
13964417Seric 		}
13974327Seric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
13984417Seric 		{
13994417Seric 			if (ctladdr->q_uid == 0)
14004417Seric 				(void) setuid(DefUid);
14014417Seric 			else
14024417Seric 				(void) setuid(ctladdr->q_uid);
14034417Seric 		}
14046887Seric 		f = dfopen(filename, "a");
14054214Seric 		if (f == NULL)
14064214Seric 			exit(EX_CANTCREAT);
14074214Seric 
14086974Seric 		putfromline(f, Mailer[1]);
14096974Seric 		(*CurEnv->e_puthdr)(f, Mailer[1], CurEnv);
14107123Seric 		fputs("\n", f);
14116974Seric 		(*CurEnv->e_putbody)(f, Mailer[1], FALSE);
14124214Seric 		fputs("\n", f);
14134214Seric 		(void) fclose(f);
14144214Seric 		(void) fflush(stdout);
14154417Seric 
14166887Seric 		/* reset ISUID & ISGID bits for paranoid systems */
14174621Seric 		(void) chmod(filename, (int) stb.st_mode);
14184214Seric 		exit(EX_OK);
14194315Seric 		/*NOTREACHED*/
14204214Seric 	}
14214214Seric 	else
14224214Seric 	{
14234214Seric 		/* parent -- wait for exit status */
14244214Seric 		register int i;
14254214Seric 		auto int stat;
14264214Seric 
14274214Seric 		while ((i = wait(&stat)) != pid)
14284214Seric 		{
14294214Seric 			if (i < 0)
14304214Seric 			{
14314214Seric 				stat = EX_OSERR << 8;
14324214Seric 				break;
14334214Seric 			}
14344214Seric 		}
14354215Seric 		if ((stat & 0377) != 0)
14364215Seric 			stat = EX_UNAVAILABLE << 8;
14374214Seric 		return ((stat >> 8) & 0377);
14384214Seric 	}
1439294Seric }
14404550Seric /*
14414550Seric **  SENDALL -- actually send all the messages.
14424550Seric **
14434550Seric **	Parameters:
14447043Seric **		e -- the envelope to send.
14454550Seric **		verifyonly -- if set, only give verification messages.
14464550Seric **
14474550Seric **	Returns:
14484550Seric **		none.
14494550Seric **
14504550Seric **	Side Effects:
14514550Seric **		Scans the send lists and sends everything it finds.
14527043Seric **		Delivers any appropriate error messages.
14534550Seric */
14544550Seric 
14557043Seric sendall(e, verifyonly)
14567043Seric 	ENVELOPE *e;
14574550Seric 	bool verifyonly;
14584550Seric {
14595008Seric 	register ADDRESS *q;
14604550Seric 
14615032Seric # ifdef DEBUG
14625032Seric 	if (Debug > 1)
14635032Seric 	{
14646900Seric 		printf("\nSend Queue:\n");
14657043Seric 		printaddr(e->e_sendqueue, TRUE);
14665032Seric 	}
14675032Seric # endif DEBUG
14685008Seric 
14697043Seric 	/*
14707043Seric 	**  Run through the list and send everything.
14717043Seric 	*/
14727043Seric 
14737043Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14744550Seric 	{
14755008Seric 		if (verifyonly)
14764550Seric 		{
14776900Seric 			CurEnv->e_to = q->q_paddr;
14785008Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
14797052Seric 				message(Arpa_Info, "deliverable");
14804550Seric 		}
14815008Seric 		else
14826974Seric 			(void) deliver(q);
14834550Seric 	}
14847043Seric 
14857043Seric 	/*
14867043Seric 	**  Now run through and check for errors.
14877043Seric 	*/
14887043Seric 
14897043Seric 	if (verifyonly)
14907043Seric 		return;
14917043Seric 
14927043Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14937043Seric 	{
14947043Seric 		register ADDRESS *qq;
14957043Seric 
14967043Seric 		if (bitset(QQUEUEUP, q->q_flags))
14977043Seric 			e->e_queueup = TRUE;
14987043Seric 		if (!bitset(QBADADDR, q->q_flags))
14997043Seric 			continue;
15007043Seric 
15017043Seric 		/* we have an address that failed -- find the parent */
15027043Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
15037043Seric 		{
15047043Seric 			char obuf[MAXNAME + 6];
15057043Seric 			extern char *aliaslookup();
15067043Seric 
15077043Seric 			/* we can only have owners for local addresses */
15087043Seric 			if (!bitset(M_LOCAL, qq->q_mailer->m_flags))
15097043Seric 				continue;
15107043Seric 
15117043Seric 			/* see if the owner list exists */
15127043Seric 			(void) strcpy(obuf, "owner-");
15137047Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
15147047Seric 				(void) strcat(obuf, "owner");
15157047Seric 			else
15167047Seric 				(void) strcat(obuf, qq->q_user);
15177043Seric 			if (aliaslookup(obuf) == NULL)
15187043Seric 				continue;
15197043Seric 
15207043Seric 			/* owner list exists -- add it to the error queue */
15217043Seric 			qq->q_flags &= ~QPRIMARY;
15227043Seric 			sendto(obuf, 1, qq, &e->e_errorqueue);
15237043Seric 			MailBack = TRUE;
15247043Seric 			break;
15257043Seric 		}
15267043Seric 
15277043Seric 		/* if we did not find an owner, send to the sender */
15287043Seric 		if (qq == NULL)
15297043Seric 			sendto(e->e_from.q_paddr, 1, qq, &e->e_errorqueue);
15307043Seric 	}
15314550Seric }
15327043Seric /*
15337043Seric **  CHECKERRORS -- check a queue of addresses and process errors.
15347043Seric **
15357043Seric **	Parameters:
15367043Seric **		e -- the envelope to check.
15377043Seric **
15387043Seric **	Returns:
15397043Seric **		none.
15407043Seric **
15417043Seric **	Side Effects:
15427043Seric **		Arranges to queue all tempfailed messages in q
15437043Seric **			or deliver error responses.
15447043Seric */
15457043Seric 
15467043Seric checkerrors(e)
15477043Seric 	register ENVELOPE *e;
15487043Seric {
15497043Seric # ifdef DEBUG
15507043Seric 	if (Debug > 0)
15517043Seric 	{
15527043Seric 		printf("\ncheckerrors: errorqueue:\n");
15537043Seric 		printaddr(e->e_errorqueue, TRUE);
15547043Seric 	}
15557043Seric # endif DEBUG
15567043Seric 
15577043Seric 	/* mail back the transcript on errors */
15587043Seric 	if (FatalErrors)
15597043Seric 		savemail();
15607043Seric 
15617043Seric 	/* queue up anything laying around */
15627043Seric 	if (e->e_queueup)
15637043Seric 	{
15647043Seric # ifdef QUEUE
15657043Seric 		queueup(e, FALSE);
15667043Seric # else QUEUE
15677043Seric 		syserr("finis: trying to queue %s", e->e_df);
15687043Seric # endif QUEUE
15697043Seric 	}
15707043Seric }
1571