1294Seric # include <signal.h>
24123Seric # include <errno.h>
34621Seric # include "sendmail.h"
44327Seric # include <sys/stat.h>
5294Seric 
6*8431Seric SCCSID(@(#)deliver.c	3.120		10/09/82);
7405Seric 
8294Seric /*
94315Seric **  DELIVER -- Deliver a message to a list of addresses.
10294Seric **
114315Seric **	This routine delivers to everyone on the same host as the
124315Seric **	user on the head of the list.  It is clever about mailers
134315Seric **	that don't handle multiple users.  It is NOT guaranteed
144315Seric **	that it will deliver to all these addresses however -- so
154315Seric **	deliver should be called once for each address on the
164315Seric **	list.
174315Seric **
18294Seric **	Parameters:
194621Seric **		firstto -- head of the address list to deliver to.
20294Seric **
21294Seric **	Returns:
22294Seric **		zero -- successfully delivered.
23294Seric **		else -- some failure, see ExitStat for more info.
24294Seric **
25294Seric **	Side Effects:
26294Seric **		The standard input is passed off to someone.
27294Seric */
28294Seric 
296974Seric deliver(firstto)
304621Seric 	ADDRESS *firstto;
31294Seric {
324452Seric 	char *host;			/* host being sent to */
334452Seric 	char *user;			/* user being sent to */
34294Seric 	char **pvp;
353233Seric 	register char **mvp;
363233Seric 	register char *p;
374452Seric 	register struct mailer *m;	/* mailer for this recipient */
382968Seric 	extern bool checkcompat();
393233Seric 	char *pv[MAXPV+1];
408225Seric 	char tobuf[MAXLINE-50];		/* text line of to people */
413233Seric 	char buf[MAXNAME];
424397Seric 	ADDRESS *ctladdr;
434397Seric 	extern ADDRESS *getctladdr();
444452Seric 	char tfrombuf[MAXNAME];		/* translated from person */
454452Seric 	extern char **prescan();
464621Seric 	register ADDRESS *to = firstto;
474863Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
485032Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
497052Seric 	bool notopen = TRUE;		/* set if connection not quite open */
508003Seric 	register int rcode;		/* response code */
51294Seric 
524488Seric 	errno = 0;
537052Seric 	if (bitset(QDONTSEND, to->q_flags))
543233Seric 		return (0);
55294Seric 
566974Seric 	m = to->q_mailer;
576974Seric 	host = to->q_host;
586974Seric 
59294Seric # ifdef DEBUG
607672Seric 	if (tTd(10, 1))
613233Seric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
626974Seric 			m->m_mno, host, to->q_user);
63294Seric # endif DEBUG
64294Seric 
65294Seric 	/*
665903Seric 	**  If this mailer is expensive, and if we don't want to make
675903Seric 	**  connections now, just mark these addresses and return.
685903Seric 	**	This is useful if we want to batch connections to
695903Seric 	**	reduce load.  This will cause the messages to be
705903Seric 	**	queued up, and a daemon will come along to send the
715903Seric 	**	messages later.
725903Seric 	**		This should be on a per-mailer basis.
735903Seric 	*/
745903Seric 
758428Seric 	if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags) &&
768428Seric 	    !Verbose)
775903Seric 	{
785903Seric 		for (; to != NULL; to = to->q_next)
79*8431Seric 		{
80*8431Seric 			if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m)
81*8431Seric 				continue;
82*8431Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
83*8431Seric 			CurEnv->e_to = to->q_paddr;
84*8431Seric 			giveresponse(EX_TEMPFAIL, TRUE, m);
85*8431Seric 		}
86*8431Seric 		CurEnv->e_to = NULL;
875903Seric 		return (0);
885903Seric 	}
895903Seric 
905903Seric 	/*
913233Seric 	**  Do initial argv setup.
923233Seric 	**	Insert the mailer name.  Notice that $x expansion is
933233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
943233Seric 	**	a picky -f flag, we insert it as appropriate.  This
953233Seric 	**	code does not check for 'pv' overflow; this places a
963233Seric 	**	manifest lower limit of 4 for MAXPV.
978062Seric 	**		The from address rewrite is expected to make
988062Seric 	**		the address relative to the other end.
992968Seric 	*/
1002968Seric 
1014452Seric 	/* rewrite from address, using rewriting rules */
1028062Seric 	expand("$f", buf, &buf[sizeof buf - 1], CurEnv);
1034452Seric 	mvp = prescan(buf, '\0');
1048062Seric 	rewrite(mvp, m->m_s_rwset);
1054452Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
1064452Seric 
1074452Seric 	define('g', tfrombuf);		/* translated sender address */
1083233Seric 	define('h', host);		/* to host */
1093233Seric 	Errors = 0;
1103233Seric 	pvp = pv;
1113233Seric 	*pvp++ = m->m_argv[0];
1122968Seric 
1133233Seric 	/* insert -f or -r flag as appropriate */
1143233Seric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1153233Seric 	{
1163233Seric 		if (bitset(M_FOPT, m->m_flags))
1173233Seric 			*pvp++ = "-f";
1183233Seric 		else
1193233Seric 			*pvp++ = "-r";
1206974Seric 		expand("$g", buf, &buf[sizeof buf - 1], CurEnv);
1213233Seric 		*pvp++ = newstr(buf);
1223233Seric 	}
123294Seric 
124294Seric 	/*
1253233Seric 	**  Append the other fixed parts of the argv.  These run
1263233Seric 	**  up to the first entry containing "$u".  There can only
1273233Seric 	**  be one of these, and there are only a few more slots
1283233Seric 	**  in the pv after it.
129294Seric 	*/
130294Seric 
1313233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
132294Seric 	{
1333233Seric 		while ((p = index(p, '$')) != NULL)
1343233Seric 			if (*++p == 'u')
1353233Seric 				break;
1363233Seric 		if (p != NULL)
1373233Seric 			break;
1383233Seric 
1393233Seric 		/* this entry is safe -- go ahead and process it */
1406974Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
1413233Seric 		*pvp++ = newstr(buf);
1423233Seric 		if (pvp >= &pv[MAXPV - 3])
1433233Seric 		{
1443233Seric 			syserr("Too many parameters to %s before $u", pv[0]);
1453233Seric 			return (-1);
1463233Seric 		}
147294Seric 	}
1484863Seric 
1496038Seric 	/*
1506038Seric 	**  If we have no substitution for the user name in the argument
1516038Seric 	**  list, we know that we must supply the names otherwise -- and
1526038Seric 	**  SMTP is the answer!!
1536038Seric 	*/
1546038Seric 
1553233Seric 	if (*mvp == NULL)
1564863Seric 	{
1574863Seric 		/* running SMTP */
1585179Seric # ifdef SMTP
1594863Seric 		clever = TRUE;
1604863Seric 		*pvp = NULL;
1615179Seric # else SMTP
1626038Seric 		/* oops!  we don't implement SMTP */
1635179Seric 		syserr("SMTP style mailer");
1645179Seric 		return (EX_SOFTWARE);
1655179Seric # endif SMTP
1664863Seric 	}
167294Seric 
168294Seric 	/*
1693233Seric 	**  At this point *mvp points to the argument with $u.  We
1703233Seric 	**  run through our address list and append all the addresses
1713233Seric 	**  we can.  If we run out of space, do not fret!  We can
1723233Seric 	**  always send another copy later.
173294Seric 	*/
174294Seric 
1753233Seric 	tobuf[0] = '\0';
1766900Seric 	CurEnv->e_to = tobuf;
1774397Seric 	ctladdr = NULL;
1783233Seric 	for (; to != NULL; to = to->q_next)
179294Seric 	{
1803233Seric 		/* avoid sending multiple recipients to dumb mailers */
1814382Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1823233Seric 			break;
1833233Seric 
1843233Seric 		/* if already sent or not for this host, don't send */
1857052Seric 		if (bitset(QDONTSEND, to->q_flags) ||
1867052Seric 		    strcmp(to->q_host, host) != 0 ||
1877052Seric 		    to->q_mailer != firstto->q_mailer)
1883233Seric 			continue;
1894397Seric 
1908225Seric 		/* avoid overflowing tobuf */
1918225Seric 		if (sizeof tobuf - (strlen(to->q_paddr) + strlen(tobuf) + 1) < 0)
1928225Seric 			break;
1938225Seric 
1945032Seric # ifdef DEBUG
1957672Seric 		if (tTd(10, 1))
1965032Seric 		{
1975032Seric 			printf("\nsend to ");
1985032Seric 			printaddr(to, FALSE);
1995032Seric 		}
2005032Seric # endif DEBUG
2015032Seric 
2024397Seric 		/* compute effective uid/gid when sending */
2034596Seric 		if (to->q_mailer == ProgMailer)
2044397Seric 			ctladdr = getctladdr(to);
2054397Seric 
2063233Seric 		user = to->q_user;
2076900Seric 		CurEnv->e_to = to->q_paddr;
2083233Seric 		to->q_flags |= QDONTSEND;
2093233Seric 
2103233Seric 		/*
2113233Seric 		**  Check to see that these people are allowed to
2123233Seric 		**  talk to each other.
2133233Seric 		*/
2143233Seric 
2153233Seric 		if (!checkcompat(to))
216294Seric 		{
2173233Seric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
2183233Seric 			continue;
219294Seric 		}
2203233Seric 
2213233Seric 		/*
2224099Seric 		**  Strip quote bits from names if the mailer is dumb
2234099Seric 		**	about them.
2243233Seric 		*/
2253233Seric 
2263233Seric 		if (bitset(M_STRIPQ, m->m_flags))
227294Seric 		{
2284099Seric 			stripquotes(user, TRUE);
2294099Seric 			stripquotes(host, TRUE);
2303233Seric 		}
2314099Seric 		else
2324099Seric 		{
2334099Seric 			stripquotes(user, FALSE);
2344099Seric 			stripquotes(host, FALSE);
2354099Seric 		}
2363233Seric 
2373233Seric 		/*
2387052Seric 		**  Do initial connection setup if needed.
2397052Seric 		*/
2407052Seric 
2417052Seric 		if (notopen)
2427052Seric 		{
2437052Seric 			message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
2447052Seric # ifdef SMTP
2457052Seric 			if (clever)
2467052Seric 			{
2477052Seric 				/* send the initial SMTP protocol */
2488003Seric 				rcode = smtpinit(m, pv, (ADDRESS *) NULL);
2497052Seric 			}
2507052Seric # ifdef SMTP
2517052Seric 			notopen = FALSE;
2527052Seric 		}
2537052Seric 
2547052Seric 		/*
2556059Seric 		**  Pass it to the other host if we are running SMTP.
2566059Seric 		*/
2576059Seric 
2586059Seric 		if (clever)
2596059Seric 		{
2606059Seric # ifdef SMTP
2618003Seric 			if (rcode == EX_OK)
2628003Seric 				rcode = smtprcpt(to);
2638003Seric 			if (rcode != EX_OK)
2646059Seric 			{
2658003Seric 				if (rcode == EX_TEMPFAIL)
2666059Seric 					to->q_flags |= QQUEUEUP;
2676059Seric 				else
2686059Seric 					to->q_flags |= QBADADDR;
2698003Seric 				giveresponse(rcode, TRUE, m);
2706059Seric 			}
2716059Seric # else SMTP
2726059Seric 			syserr("trying to be clever");
2736059Seric # endif SMTP
2746059Seric 		}
2756059Seric 
2766059Seric 		/*
2774161Seric 		**  If an error message has already been given, don't
2784161Seric 		**	bother to send to this address.
2794161Seric 		**
2804161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
2814161Seric 		**	>> NOTE >> cannot do any further aliasing; that
2824161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
2834161Seric 		*/
2844161Seric 
2857293Seric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2864161Seric 			continue;
2874161Seric 
2884283Seric 		/* save statistics.... */
2894596Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2906900Seric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
2914283Seric 
2924161Seric 		/*
2933233Seric 		**  See if this user name is "special".
2943233Seric 		**	If the user name has a slash in it, assume that this
2956974Seric 		**	is a file -- send it off without further ado.  Note
2966974Seric 		**	that this type of addresses is not processed along
2976974Seric 		**	with the others, so we fudge on the To person.
2983233Seric 		*/
2993233Seric 
3004596Seric 		if (m == LocalMailer)
3013233Seric 		{
3025599Seric 			if (user[0] == '/')
303294Seric 			{
3048003Seric 				rcode = mailfile(user, getctladdr(to));
3058003Seric 				giveresponse(rcode, TRUE, m);
3063233Seric 				continue;
307294Seric 			}
308294Seric 		}
3093233Seric 
3104315Seric 		/*
3114315Seric 		**  Address is verified -- add this user to mailer
3124315Seric 		**  argv, and add it to the print list of recipients.
3134315Seric 		*/
3144315Seric 
3156059Seric 		/* link together the chain of recipients */
3166272Seric 		to->q_tchain = tochain;
3176272Seric 		tochain = to;
3186059Seric 
3193233Seric 		/* create list of users for error messages */
3203233Seric 		if (tobuf[0] != '\0')
3214082Seric 			(void) strcat(tobuf, ",");
3224082Seric 		(void) strcat(tobuf, to->q_paddr);
3233233Seric 		define('u', user);		/* to user */
3244078Seric 		define('z', to->q_home);	/* user's home */
3253233Seric 
3264863Seric 		/*
3276059Seric 		**  Expand out this user into argument list.
3284863Seric 		*/
3294863Seric 
3306059Seric 		if (!clever)
3313233Seric 		{
3326974Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3334863Seric 			*pvp++ = newstr(buf);
3344863Seric 			if (pvp >= &pv[MAXPV - 2])
3354863Seric 			{
3364863Seric 				/* allow some space for trailing parms */
3374863Seric 				break;
3384863Seric 			}
3394863Seric 		}
340294Seric 	}
341294Seric 
3424067Seric 	/* see if any addresses still exist */
3434067Seric 	if (tobuf[0] == '\0')
3444863Seric 	{
3455179Seric # ifdef SMTP
3464863Seric 		if (clever)
3477228Seric 			smtpquit(pv[0], FALSE);
3485179Seric # endif SMTP
3497003Seric 		define('g', (char *) NULL);
3504067Seric 		return (0);
3514863Seric 	}
3524067Seric 
3533233Seric 	/* print out messages as full list */
3546900Seric 	CurEnv->e_to = tobuf;
3553233Seric 
356294Seric 	/*
3573233Seric 	**  Fill out any parameters after the $u parameter.
358294Seric 	*/
359294Seric 
3604863Seric 	while (!clever && *++mvp != NULL)
361294Seric 	{
3626974Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3633233Seric 		*pvp++ = newstr(buf);
3643233Seric 		if (pvp >= &pv[MAXPV])
3653233Seric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
366294Seric 	}
3673233Seric 	*pvp++ = NULL;
368294Seric 
369294Seric 	/*
370294Seric 	**  Call the mailer.
3712898Seric 	**	The argument vector gets built, pipes
372294Seric 	**	are created as necessary, and we fork & exec as
3732898Seric 	**	appropriate.
3744863Seric 	**	If we are running SMTP, we just need to clean up.
375294Seric 	*/
376294Seric 
3774397Seric 	if (ctladdr == NULL)
3786900Seric 		ctladdr = &CurEnv->e_from;
3795179Seric # ifdef SMTP
3804863Seric 	if (clever)
3814863Seric 	{
3828003Seric 		rcode = smtpfinish(m, CurEnv);
3838003Seric 		if (rcode != EX_OK)
3848003Seric 			giveresponse(rcode, TRUE, m);
3858003Seric 		smtpquit(pv[0], rcode == EX_OK);
3864863Seric 	}
3874863Seric 	else
3885179Seric # endif SMTP
3898003Seric 		rcode = sendoff(m, pv, ctladdr);
3903233Seric 
3914621Seric 	/*
3924621Seric 	**  If we got a temporary failure, arrange to queue the
3934621Seric 	**  addressees.
3944621Seric 	*/
3954621Seric 
3968003Seric 	if (rcode == EX_TEMPFAIL)
3974621Seric 	{
3985032Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
3994621Seric 			to->q_flags |= QQUEUEUP;
4004621Seric 	}
4014621Seric 
4024488Seric 	errno = 0;
4037003Seric 	define('g', (char *) NULL);
4048003Seric 	return (rcode);
4053233Seric }
4063233Seric /*
4074214Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4084214Seric **
4094214Seric **	This MUST be a macro, since after a vfork we are running
4104214Seric **	two processes on the same stack!!!
4114214Seric **
4124214Seric **	Parameters:
4134214Seric **		none.
4144214Seric **
4154214Seric **	Returns:
4164214Seric **		From a macro???  You've got to be kidding!
4174214Seric **
4184214Seric **	Side Effects:
4194214Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4204214Seric **			pid of child in parent, zero in child.
4214214Seric **			-1 on unrecoverable error.
4224214Seric **
4234214Seric **	Notes:
4244214Seric **		I'm awfully sorry this looks so awful.  That's
4254214Seric **		vfork for you.....
4264214Seric */
4274214Seric 
4284214Seric # define NFORKTRIES	5
4294214Seric # ifdef VFORK
4304214Seric # define XFORK	vfork
4314214Seric # else VFORK
4324214Seric # define XFORK	fork
4334214Seric # endif VFORK
4344214Seric 
4354214Seric # define DOFORK(fORKfN) \
4364214Seric {\
4374214Seric 	register int i;\
4384214Seric \
4394214Seric 	for (i = NFORKTRIES; i-- > 0; )\
4404214Seric 	{\
4414214Seric 		pid = fORKfN();\
4424214Seric 		if (pid >= 0)\
4434214Seric 			break;\
4447003Seric 		sleep(NFORKTRIES - i);\
4454214Seric 	}\
4464214Seric }
4474214Seric /*
4484637Seric **  DOFORK -- simple fork interface to DOFORK.
4494637Seric **
4504637Seric **	Parameters:
4514637Seric **		none.
4524637Seric **
4534637Seric **	Returns:
4544637Seric **		pid of child in parent.
4554637Seric **		zero in child.
4564637Seric **		-1 on error.
4574637Seric **
4584637Seric **	Side Effects:
4594637Seric **		returns twice, once in parent and once in child.
4604637Seric */
4614637Seric 
4624637Seric dofork()
4634637Seric {
4644637Seric 	register int pid;
4654637Seric 
4664637Seric 	DOFORK(fork);
4674637Seric 	return (pid);
4684637Seric }
4694637Seric /*
4703233Seric **  SENDOFF -- send off call to mailer & collect response.
4713233Seric **
4723233Seric **	Parameters:
4733233Seric **		m -- mailer descriptor.
4743233Seric **		pvp -- parameter vector to send to it.
4754397Seric **		ctladdr -- an address pointer controlling the
4764397Seric **			user/groupid etc. of the mailer.
4773233Seric **
4783233Seric **	Returns:
4793233Seric **		exit status of mailer.
4803233Seric **
4813233Seric **	Side Effects:
4823233Seric **		none.
4833233Seric */
4843233Seric 
4856974Seric sendoff(m, pvp, ctladdr)
4863233Seric 	struct mailer *m;
4873233Seric 	char **pvp;
4884397Seric 	ADDRESS *ctladdr;
4893233Seric {
4904863Seric 	auto FILE *mfile;
4914863Seric 	auto FILE *rfile;
4923233Seric 	register int i;
4933233Seric 	int pid;
4944863Seric 
4954863Seric 	/*
4964863Seric 	**  Create connection to mailer.
4974863Seric 	*/
4984863Seric 
4994863Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
5004863Seric 	if (pid < 0)
5014863Seric 		return (-1);
5024863Seric 
5034863Seric 	/*
5044863Seric 	**  Format and send message.
5054863Seric 	*/
5064863Seric 
5074863Seric 	(void) signal(SIGPIPE, SIG_IGN);
5086974Seric 	putfromline(mfile, m);
5096974Seric 	(*CurEnv->e_puthdr)(mfile, m, CurEnv);
5106974Seric 	fprintf(mfile, "\n");
5116974Seric 	(*CurEnv->e_putbody)(mfile, m, FALSE);
5124863Seric 	(void) fclose(mfile);
5134863Seric 
5144863Seric 	i = endmailer(pid, pvp[0]);
5154863Seric 	giveresponse(i, TRUE, m);
5165981Seric 
5175981Seric 	/* arrange a return receipt if requested */
5188062Seric 	if (CurEnv->e_receiptto != NULL && bitset(M_LOCAL, m->m_flags))
5195981Seric 	{
5206900Seric 		CurEnv->e_sendreceipt = TRUE;
5218062Seric 		if (ExitStat == EX_OK)
5228062Seric 			fprintf(Xscript, "%s... successfully delivered\n",
5238062Seric 				CurEnv->e_to);
5245981Seric 		/* do we want to send back more info? */
5255981Seric 	}
5265981Seric 
5274863Seric 	return (i);
5284863Seric }
5294863Seric /*
5304863Seric **  ENDMAILER -- Wait for mailer to terminate.
5314863Seric **
5324863Seric **	We should never get fatal errors (e.g., segmentation
5334863Seric **	violation), so we report those specially.  For other
5344863Seric **	errors, we choose a status message (into statmsg),
5354863Seric **	and if it represents an error, we print it.
5364863Seric **
5374863Seric **	Parameters:
5384863Seric **		pid -- pid of mailer.
5394863Seric **		name -- name of mailer (for error messages).
5404863Seric **
5414863Seric **	Returns:
5424863Seric **		exit code of mailer.
5434863Seric **
5444863Seric **	Side Effects:
5454863Seric **		none.
5464863Seric */
5474863Seric 
5484863Seric endmailer(pid, name)
5494863Seric 	int pid;
5504863Seric 	char *name;
5514863Seric {
5524863Seric 	register int i;
5534863Seric 	auto int st;
5544863Seric 
5556038Seric 	/* in the IPC case there is nothing to wait for */
5566038Seric 	if (pid == 0)
5576038Seric 		return (EX_OK);
5586038Seric 
5596038Seric 	/* wait for the mailer process to die and collect status */
5608127Seric 	do
5618127Seric 	{
5628127Seric 		errno = 0;
5638127Seric 		i = wait(&st);
5648133Seric 	} while ((i >= 0 && i != pid) || errno == EINTR);
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
6197672Seric 	if (tTd(11, 1))
620294Seric 	{
6218178Seric 		printf("openmailer:");
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;
6397285Seric 		register u_short port;
6406038Seric 
6416038Seric 		if (!clever)
6426038Seric 			syserr("non-clever IPC");
6436632Seric 		if (pvp[2] != NULL)
6447285Seric 			port = atoi(pvp[2]);
6456632Seric 		else
6467285Seric 			port = 0;
6477285Seric 		i = makeconnection(pvp[1], port, pmfile, prfile);
6486038Seric 		if (i != EX_OK)
6496047Seric 		{
6506047Seric 			ExitStat = i;
6516038Seric 			return (-1);
6526047Seric 		}
6536038Seric 		else
6546038Seric 			return (0);
6556038Seric 	}
6566038Seric # endif DAEMON
6576038Seric 
6582898Seric 	/* create a pipe to shove the mail through */
6594709Seric 	if (pipe(mpvect) < 0)
660294Seric 	{
6614863Seric 		syserr("pipe (to mailer)");
662294Seric 		return (-1);
663294Seric 	}
6644863Seric 
6655179Seric # ifdef SMTP
6664863Seric 	/* if this mailer speaks smtp, create a return pipe */
6674863Seric 	if (clever && pipe(rpvect) < 0)
6684863Seric 	{
6694863Seric 		syserr("pipe (from mailer)");
6704863Seric 		(void) close(mpvect[0]);
6714863Seric 		(void) close(mpvect[1]);
6724863Seric 		return (-1);
6734863Seric 	}
6745179Seric # endif SMTP
6754863Seric 
6766038Seric 	/*
6776038Seric 	**  Actually fork the mailer process.
6786038Seric 	**	DOFORK is clever about retrying.
6796038Seric 	*/
6806038Seric 
6817672Seric 	(void) fflush(Xscript);				/* for debugging */
6824214Seric 	DOFORK(XFORK);
6834327Seric 	/* pid is set by DOFORK */
684294Seric 	if (pid < 0)
685294Seric 	{
6866038Seric 		/* failure */
687294Seric 		syserr("Cannot fork");
6884709Seric 		(void) close(mpvect[0]);
6894709Seric 		(void) close(mpvect[1]);
6904863Seric 		if (clever)
6914863Seric 		{
6924863Seric 			(void) close(rpvect[0]);
6934863Seric 			(void) close(rpvect[1]);
6944863Seric 		}
695294Seric 		return (-1);
696294Seric 	}
697294Seric 	else if (pid == 0)
698294Seric 	{
699294Seric 		/* child -- set up input & exec mailer */
7001621Seric 		/* make diagnostic output be standard output */
7014477Seric 		(void) signal(SIGINT, SIG_IGN);
7024477Seric 		(void) signal(SIGHUP, SIG_IGN);
7034215Seric 		(void) signal(SIGTERM, SIG_DFL);
7044709Seric 
7054709Seric 		/* arrange to filter standard & diag output of command */
7064863Seric 		if (clever)
7074709Seric 		{
7084863Seric 			(void) close(rpvect[0]);
7094709Seric 			(void) close(1);
7104863Seric 			(void) dup(rpvect[1]);
7114863Seric 			(void) close(rpvect[1]);
7124863Seric 		}
7134863Seric 		else if (OutChannel != stdout)
7144863Seric 		{
7154863Seric 			(void) close(1);
7164709Seric 			(void) dup(fileno(OutChannel));
7174709Seric 		}
7184082Seric 		(void) close(2);
7194082Seric 		(void) dup(1);
7204709Seric 
7214709Seric 		/* arrange to get standard input */
7224709Seric 		(void) close(mpvect[1]);
7234082Seric 		(void) close(0);
7244709Seric 		if (dup(mpvect[0]) < 0)
725294Seric 		{
7262898Seric 			syserr("Cannot dup to zero!");
7272898Seric 			_exit(EX_OSERR);
728294Seric 		}
7294709Seric 		(void) close(mpvect[0]);
7302968Seric 		if (!bitset(M_RESTR, m->m_flags))
7314215Seric 		{
7324417Seric 			if (ctladdr->q_uid == 0)
7334417Seric 			{
7344417Seric 				(void) setgid(DefGid);
7354417Seric 				(void) setuid(DefUid);
7364417Seric 			}
7374417Seric 			else
7384415Seric 			{
7394417Seric 				(void) setgid(ctladdr->q_gid);
7404417Seric 				(void) setuid(ctladdr->q_uid);
7414415Seric 			}
7424215Seric 		}
7432774Seric # ifndef VFORK
7442774Seric 		/*
7452774Seric 		**  We have to be careful with vfork - we can't mung up the
7462774Seric 		**  memory but we don't want the mailer to inherit any extra
7472774Seric 		**  open files.  Chances are the mailer won't
7482774Seric 		**  care about an extra file, but then again you never know.
7492774Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
7502774Seric 		**  declared static so we can't.  But if we fclose(pwf), which
7512774Seric 		**  is what endpwent does, it closes it in the parent too and
7522774Seric 		**  the next getpwnam will be slower.  If you have a weird
7532774Seric 		**  mailer that chokes on the extra file you should do the
7542774Seric 		**  endpwent().
7552774Seric 		**
7562774Seric 		**  Similar comments apply to log.  However, openlog is
7572774Seric 		**  clever enough to set the FIOCLEX mode on the file,
7582774Seric 		**  so it will be closed automatically on the exec.
7592774Seric 		*/
7602774Seric 
7612774Seric 		endpwent();
762294Seric # ifdef LOG
7632089Seric 		closelog();
764294Seric # endif LOG
7652774Seric # endif VFORK
7666038Seric 
7676038Seric 		/* try to execute the mailer */
768294Seric 		execv(m->m_mailer, pvp);
7696038Seric 
770294Seric 		/* syserr fails because log is closed */
771294Seric 		/* syserr("Cannot exec %s", m->m_mailer); */
7724214Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
7734082Seric 		(void) fflush(stdout);
7741619Seric 		_exit(EX_UNAVAILABLE);
775294Seric 	}
776294Seric 
7774709Seric 	/*
7784863Seric 	**  Set up return value.
7794709Seric 	*/
7804709Seric 
7814709Seric 	(void) close(mpvect[0]);
7824709Seric 	mfile = fdopen(mpvect[1], "w");
7834863Seric 	if (clever)
7844863Seric 	{
7854863Seric 		(void) close(rpvect[1]);
7864863Seric 		rfile = fdopen(rpvect[0], "r");
7874863Seric 	}
788294Seric 
7894863Seric 	*pmfile = mfile;
7904863Seric 	*prfile = rfile;
791294Seric 
7924863Seric 	return (pid);
793294Seric }
794294Seric /*
795294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
796294Seric **
797294Seric **	Parameters:
798294Seric **		stat -- the status code from the mailer (high byte
799294Seric **			only; core dumps must have been taken care of
800294Seric **			already).
801294Seric **		force -- if set, force an error message output, even
802294Seric **			if the mailer seems to like to print its own
803294Seric **			messages.
804294Seric **		m -- the mailer descriptor for this mailer.
805294Seric **
806294Seric **	Returns:
8074082Seric **		none.
808294Seric **
809294Seric **	Side Effects:
8101518Seric **		Errors may be incremented.
811294Seric **		ExitStat may be set.
812294Seric */
813294Seric 
814294Seric giveresponse(stat, force, m)
815294Seric 	int stat;
8167228Seric 	bool force;
817294Seric 	register struct mailer *m;
818294Seric {
819294Seric 	register char *statmsg;
820294Seric 	extern char *SysExMsg[];
821294Seric 	register int i;
822294Seric 	extern int N_SysEx;
8231624Seric 	char buf[30];
824294Seric 
8254315Seric 	/*
8264315Seric 	**  Compute status message from code.
8274315Seric 	*/
8284315Seric 
829294Seric 	i = stat - EX__BASE;
830294Seric 	if (i < 0 || i > N_SysEx)
831294Seric 		statmsg = NULL;
832294Seric 	else
833294Seric 		statmsg = SysExMsg[i];
834294Seric 	if (stat == 0)
8354065Seric 	{
8368003Seric 		statmsg = "250 sent";
8378003Seric 		message(Arpa_Info, &statmsg[4]);
8384065Seric 	}
8394621Seric 	else if (stat == EX_TEMPFAIL)
8404621Seric 	{
841*8431Seric 		message(Arpa_Info, "queued");
8424621Seric 	}
843294Seric 	else
844294Seric 	{
8451518Seric 		Errors++;
8466047Seric 		FatalErrors = TRUE;
847294Seric 		if (statmsg == NULL && m->m_badstat != 0)
848294Seric 		{
849294Seric 			stat = m->m_badstat;
850294Seric 			i = stat - EX__BASE;
851294Seric # ifdef DEBUG
852294Seric 			if (i < 0 || i >= N_SysEx)
853294Seric 				syserr("Bad m_badstat %d", stat);
854294Seric 			else
855294Seric # endif DEBUG
8567344Seric 				statmsg = SysExMsg[i];
857294Seric 		}
858294Seric 		if (statmsg == NULL)
859294Seric 			usrerr("unknown mailer response %d", stat);
8604065Seric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
8618003Seric 			usrerr(statmsg);
8627344Seric 		else
8638003Seric 			fprintf(Xscript, "%s\n", &statmsg[4]);
864294Seric 	}
865294Seric 
866294Seric 	/*
867294Seric 	**  Final cleanup.
868294Seric 	**	Log a record of the transaction.  Compute the new
869294Seric 	**	ExitStat -- if we already had an error, stick with
870294Seric 	**	that.
871294Seric 	*/
872294Seric 
8731624Seric 	if (statmsg == NULL)
8741624Seric 	{
8758003Seric 		(void) sprintf(buf, "554 error %d", stat);
8761624Seric 		statmsg = buf;
8771624Seric 	}
8781624Seric 
879294Seric # ifdef LOG
8807680Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
8817858Seric 	{
8827858Seric 		extern char *pintvl();
8837858Seric 
8847858Seric 		syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id,
8857883Seric 		       CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE),
8868003Seric 		       &statmsg[4]);
8877858Seric 	}
888294Seric # endif LOG
8894621Seric 	if (stat != EX_TEMPFAIL)
8904621Seric 		setstat(stat);
891294Seric }
892294Seric /*
8936974Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
894294Seric **
8956974Seric **	This can be made an arbitrary message separator by changing $l
896294Seric **
8976974Seric **	One of the ugliest hacks seen by human eyes is
8986974Seric **	contained herein: UUCP wants those stupid
8996974Seric **	"remote from <host>" lines.  Why oh why does a
9006974Seric **	well-meaning programmer such as myself have to
9016974Seric **	deal with this kind of antique garbage????
9026974Seric **
903294Seric **	Parameters:
9046974Seric **		fp -- the file to output to.
9056974Seric **		m -- the mailer describing this entry.
906294Seric **
907294Seric **	Returns:
9086974Seric **		none
909294Seric **
910294Seric **	Side Effects:
9116974Seric **		outputs some text to fp.
912294Seric */
913294Seric 
9146974Seric putfromline(fp, m)
9156974Seric 	register FILE *fp;
9166974Seric 	register MAILER *m;
917294Seric {
9186974Seric 	char buf[MAXLINE];
919294Seric 
9206974Seric 	if (bitset(M_NHDR, m->m_flags))
9216974Seric 		return;
9224315Seric 
9236974Seric # ifdef UGLYUUCP
9246974Seric 	if (bitset(M_UGLYUUCP, m->m_flags))
9254205Seric 	{
9266974Seric 		extern char *macvalue();
9278178Seric 		char *sys = macvalue('g', CurEnv);
9286974Seric 		char *bang = index(sys, '!');
9296041Seric 
9306974Seric 		if (bang == NULL)
9316974Seric 			syserr("No ! in UUCP! (%s)", sys);
9325099Seric 		else
9336974Seric 			*bang = '\0';
9347232Seric 		expand("From $f  $d remote from $g\n", buf,
9356974Seric 				&buf[sizeof buf - 1], CurEnv);
9366974Seric 		*bang = '!';
9376974Seric 	}
9386974Seric 	else
9395179Seric # endif UGLYUUCP
9406974Seric 		expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv);
9417123Seric 	putline(buf, fp, bitset(M_FULLSMTP, m->m_flags));
9425981Seric }
9435981Seric /*
9446974Seric **  PUTHEADER -- put the header part of a message from the in-core copy
9455981Seric **
9465981Seric **	Parameters:
9475981Seric **		fp -- file to put it on.
9485981Seric **		m -- mailer to use.
9496974Seric **		e -- envelope to use.
9505981Seric **
9515981Seric **	Returns:
9525981Seric **		none.
9535981Seric **
9545981Seric **	Side Effects:
9555981Seric **		none.
9565981Seric */
9575981Seric 
9586974Seric putheader(fp, m, e)
9595981Seric 	register FILE *fp;
9605981Seric 	register struct mailer *m;
9616974Seric 	register ENVELOPE *e;
9625981Seric {
9635981Seric 	char buf[BUFSIZ];
9645981Seric 	register HDR *h;
9655981Seric 	extern char *arpadate();
9665981Seric 	extern char *capitalize();
9675981Seric 	extern bool samefrom();
9687123Seric 	char obuf[MAXLINE];
9697123Seric 	register char *obp;
9707123Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
9715981Seric 
9726974Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
9731828Seric 	{
9744315Seric 		register char *p;
9754315Seric 
9763389Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
9778062Seric 			continue;
9784447Seric 
9797756Seric 		p = h->h_value;
9808062Seric 		if (bitset(H_DEFAULT, h->h_flags))
9813385Seric 		{
9827666Seric 			/* macro expand value if generated internally */
9838062Seric 			expand(p, buf, &buf[sizeof buf], e);
9843385Seric 			p = buf;
9853385Seric 		}
9864447Seric 		if (p == NULL || *p == '\0')
9873389Seric 			continue;
9884209Seric 
9898062Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
9904209Seric 		{
9918062Seric 			/* address field */
9928093Seric 			bool oldstyle = e->e_oldstyle;
9938093Seric 
9948093Seric 			if (bitset(H_FROM, h->h_flags))
9958093Seric 				oldstyle = FALSE;
9968093Seric 			commaize(h, p, fp, oldstyle, m);
9974447Seric 		}
9988062Seric 		else
9994447Seric 		{
10008062Seric 			/* vanilla header line */
10017123Seric 			(void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p);
10027123Seric 			putline(obuf, fp, fullsmtp);
10034209Seric 		}
10048062Seric 		h->h_flags |= H_USED;
10052898Seric 	}
1006294Seric }
1007294Seric /*
10087756Seric **  COMMAIZE -- output a header field, making a comma-translated list.
10097756Seric **
10107756Seric **	Parameters:
10118062Seric **		h -- the header field to output.
10128062Seric **		p -- the value to put in it.
10137756Seric **		fp -- file to put it to.
10147756Seric **		oldstyle -- TRUE if this is an old style header.
10158235Seric **		m -- a pointer to the mailer descriptor.  If NULL,
10168235Seric **			don't transform the name at all.
10177756Seric **
10187756Seric **	Returns:
10197756Seric **		none.
10207756Seric **
10217756Seric **	Side Effects:
10227756Seric **		outputs "p" to file "fp".
10237756Seric */
10247756Seric 
10258062Seric commaize(h, p, fp, oldstyle, m)
10268062Seric 	register HDR *h;
10277756Seric 	register char *p;
10287756Seric 	FILE *fp;
10297756Seric 	bool oldstyle;
10308062Seric 	register MAILER *m;
10317756Seric {
10328062Seric 	register char *obp;
10338062Seric 	int opos;
10348235Seric 	bool fullsmtp = FALSE;
10357756Seric 	bool firstone = TRUE;
10367756Seric 	char obuf[MAXLINE];
10377756Seric 
10387756Seric 	/*
10397756Seric 	**  Output the address list translated by the
10407756Seric 	**  mailer and with commas.
10417756Seric 	*/
10427756Seric 
10437756Seric # ifdef DEBUG
10447756Seric 	if (tTd(14, 2))
10458062Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
10467756Seric # endif DEBUG
10477756Seric 
10488235Seric 	if (m != NULL && bitset(M_FULLSMTP, m->m_flags))
10498235Seric 		fullsmtp = TRUE;
10508235Seric 
10517756Seric 	obp = obuf;
10528062Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
10538062Seric 	opos = strlen(h->h_field) + 2;
10547756Seric 	obp += opos;
10557756Seric 
10567756Seric 	/*
10577756Seric 	**  Run through the list of values.
10587756Seric 	*/
10597756Seric 
10607756Seric 	while (*p != '\0')
10617756Seric 	{
10627756Seric 		register char *name;
10637756Seric 		char savechar;
10648062Seric 		extern char *remotename();
10658077Seric 		extern char *DelimChar;		/* defined in prescan */
10667756Seric 
10677756Seric 		/*
10687756Seric 		**  Find the end of the name.  New style names
10697756Seric 		**  end with a comma, old style names end with
10707756Seric 		**  a space character.  However, spaces do not
10717756Seric 		**  necessarily delimit an old-style name -- at
10727756Seric 		**  signs mean keep going.
10737756Seric 		*/
10747756Seric 
10758077Seric 		/* find end of name */
10768077Seric 		while (isspace(*p) || *p == ',')
10777756Seric 			p++;
10787756Seric 		name = p;
10798077Seric 		for (;;)
10807756Seric 		{
10818077Seric 			char *oldp;
10827756Seric 			extern bool isatword();
10837756Seric 
10848091Seric 			(void) prescan(p, oldstyle ? ' ' : ',');
10858077Seric 			p = DelimChar;
10867756Seric 
10877756Seric 			/* look to see if we have an at sign */
10887756Seric 			oldp = p;
10897756Seric 			while (*p != '\0' && isspace(*p))
10907756Seric 				p++;
10917756Seric 
10927756Seric 			if (*p != '@' && !isatword(p))
10937756Seric 			{
10947756Seric 				p = oldp;
10957756Seric 				break;
10967756Seric 			}
10977756Seric 			p += *p == '@' ? 1 : 2;
10987756Seric 			while (*p != '\0' && isspace(*p))
10997756Seric 				p++;
11007756Seric 		}
11017756Seric 		/* at the end of one complete name */
11027756Seric 
11037756Seric 		/* strip off trailing white space */
11047756Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
11057756Seric 			p--;
11067756Seric 		if (++p == name)
11077756Seric 			continue;
11087756Seric 		savechar = *p;
11097756Seric 		*p = '\0';
11107756Seric 
11117756Seric 		/* translate the name to be relative */
11128235Seric 		if (m != NULL)
11138235Seric 			name = remotename(name, m, bitset(H_FROM, h->h_flags));
11147756Seric 		if (*name == '\0')
11157756Seric 		{
11167756Seric 			*p = savechar;
11177756Seric 			continue;
11187756Seric 		}
11197756Seric 
11207756Seric 		/* output the name with nice formatting */
11217756Seric 		opos += strlen(name);
11227756Seric 		if (!firstone)
11237756Seric 			opos += 2;
11247756Seric 		if (opos > 78 && !firstone)
11257756Seric 		{
11267756Seric 			(void) sprintf(obp, ",\n");
11277756Seric 			putline(obuf, fp, fullsmtp);
11287756Seric 			obp = obuf;
11297756Seric 			(void) sprintf(obp, "        ");
11307756Seric 			obp += strlen(obp);
11317756Seric 			opos = 8 + strlen(name);
11327756Seric 		}
11337756Seric 		else if (!firstone)
11347756Seric 		{
11357756Seric 			(void) sprintf(obp, ", ");
11367756Seric 			obp += 2;
11377756Seric 		}
11387756Seric 		(void) sprintf(obp, "%s", name);
11397756Seric 		obp += strlen(obp);
11407756Seric 		firstone = FALSE;
11417756Seric 		*p = savechar;
11427756Seric 	}
11437756Seric 	(void) strcpy(obp, "\n");
11447756Seric 	putline(obuf, fp, fullsmtp);
11457756Seric }
11467756Seric /*
11476974Seric **  PUTBODY -- put the body of a message.
11486974Seric **
11496974Seric **	Parameters:
11506974Seric **		fp -- file to output onto.
11516974Seric **		m -- a mailer descriptor.
11526974Seric **		xdot -- if set, use SMTP hidden dot algorithm.
11536974Seric **
11546974Seric **	Returns:
11556974Seric **		none.
11566974Seric **
11576974Seric **	Side Effects:
11586974Seric **		The message is written onto fp.
11596974Seric */
11606974Seric 
11616974Seric putbody(fp, m, xdot)
11626974Seric 	FILE *fp;
11636974Seric 	struct mailer *m;
11646974Seric 	bool xdot;
11656974Seric {
11666974Seric 	char buf[MAXLINE + 1];
11677123Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
11686974Seric 
11696974Seric 	/*
11706974Seric 	**  Output the body of the message
11716974Seric 	*/
11726974Seric 
11737003Seric #ifdef lint
11747003Seric 	/* m will be needed later for complete smtp emulation */
11757003Seric 	if (m == NULL)
11767003Seric 		return;
11777003Seric #endif lint
11787003Seric 
11796974Seric 	if (TempFile != NULL)
11806974Seric 	{
11816974Seric 		rewind(TempFile);
11826974Seric 		buf[0] = '.';
11836974Seric 		while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL)
11847123Seric 			putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp);
11856974Seric 
11866974Seric 		if (ferror(TempFile))
11876974Seric 		{
11886974Seric 			syserr("putbody: read error");
11896974Seric 			ExitStat = EX_IOERR;
11906974Seric 		}
11916974Seric 	}
11926974Seric 
11936974Seric 	(void) fflush(fp);
11946974Seric 	if (ferror(fp) && errno != EPIPE)
11956974Seric 	{
11966974Seric 		syserr("putbody: write error");
11976974Seric 		ExitStat = EX_IOERR;
11986974Seric 	}
11996974Seric 	errno = 0;
12006974Seric }
12016974Seric /*
12025936Seric **  ISATWORD -- tell if the word we are pointing to is "at".
12035936Seric **
12045936Seric **	Parameters:
12055936Seric **		p -- word to check.
12065936Seric **
12075936Seric **	Returns:
12085936Seric **		TRUE -- if p is the word at.
12095936Seric **		FALSE -- otherwise.
12105936Seric **
12115936Seric **	Side Effects:
12125936Seric **		none.
12135936Seric */
12145936Seric 
12155936Seric bool
12165936Seric isatword(p)
12175936Seric 	register char *p;
12185936Seric {
12195936Seric 	extern char lower();
12205936Seric 
12215936Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
12225936Seric 	    p[2] != '\0' && isspace(p[2]))
12235936Seric 		return (TRUE);
12245936Seric 	return (FALSE);
12255936Seric }
12265936Seric /*
12274370Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
12284370Seric **
12294370Seric **	Parameters:
12304370Seric **		ifrom -- internally generated form of from address.
12314370Seric **		efrom -- external form of from address.
12324370Seric **
12334370Seric **	Returns:
12344370Seric **		TRUE -- if they convey the same info.
12354370Seric **		FALSE -- if any information has been lost.
12364370Seric **
12374370Seric **	Side Effects:
12384370Seric **		none.
12394370Seric */
12404370Seric 
12414370Seric bool
12424370Seric samefrom(ifrom, efrom)
12434370Seric 	char *ifrom;
12444370Seric 	char *efrom;
12454370Seric {
12464447Seric 	register char *p;
12474447Seric 	char buf[MAXNAME + 4];
12484447Seric 
12494447Seric # ifdef DEBUG
12507672Seric 	if (tTd(3, 8))
12514447Seric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
12524447Seric # endif DEBUG
12534447Seric 	if (strcmp(ifrom, efrom) == 0)
12544447Seric 		goto success;
12554447Seric 	p = index(ifrom, '@');
12564447Seric 	if (p == NULL)
12574447Seric 		goto failure;
12584447Seric 	*p = '\0';
12597003Seric 	(void) strcpy(buf, ifrom);
12607003Seric 	(void) strcat(buf, " at ");
12614447Seric 	*p++ = '@';
12627003Seric 	(void) strcat(buf, p);
12634447Seric 	if (strcmp(buf, efrom) == 0)
12644447Seric 		goto success;
12654447Seric 
12664447Seric   failure:
12674447Seric # ifdef DEBUG
12687672Seric 	if (tTd(3, 8))
12694447Seric 		printf("FALSE\n");
12704447Seric # endif DEBUG
12714447Seric 	return (FALSE);
12724447Seric 
12734447Seric   success:
12744447Seric # ifdef DEBUG
12757672Seric 	if (tTd(3, 8))
12764447Seric 		printf("TRUE\n");
12774447Seric # endif DEBUG
12784447Seric 	return (TRUE);
12794370Seric }
12804370Seric /*
1281294Seric **  MAILFILE -- Send a message to a file.
1282294Seric **
12834327Seric **	If the file has the setuid/setgid bits set, but NO execute
12844327Seric **	bits, sendmail will try to become the owner of that file
12854327Seric **	rather than the real user.  Obviously, this only works if
12864327Seric **	sendmail runs as root.
12874327Seric **
1288294Seric **	Parameters:
1289294Seric **		filename -- the name of the file to send to.
12904397Seric **		ctladdr -- the controlling address header -- includes
12914397Seric **			the userid/groupid to be when sending.
1292294Seric **
1293294Seric **	Returns:
1294294Seric **		The exit code associated with the operation.
1295294Seric **
1296294Seric **	Side Effects:
1297294Seric **		none.
1298294Seric */
1299294Seric 
13004397Seric mailfile(filename, ctladdr)
1301294Seric 	char *filename;
13024397Seric 	ADDRESS *ctladdr;
1303294Seric {
1304294Seric 	register FILE *f;
13054214Seric 	register int pid;
1306294Seric 
13074214Seric 	/*
13084214Seric 	**  Fork so we can change permissions here.
13094214Seric 	**	Note that we MUST use fork, not vfork, because of
13104214Seric 	**	the complications of calling subroutines, etc.
13114214Seric 	*/
13124067Seric 
13134214Seric 	DOFORK(fork);
13144214Seric 
13154214Seric 	if (pid < 0)
13164214Seric 		return (EX_OSERR);
13174214Seric 	else if (pid == 0)
13184214Seric 	{
13194214Seric 		/* child -- actually write to file */
13204327Seric 		struct stat stb;
13214327Seric 
13224215Seric 		(void) signal(SIGINT, SIG_DFL);
13234215Seric 		(void) signal(SIGHUP, SIG_DFL);
13244215Seric 		(void) signal(SIGTERM, SIG_DFL);
13254327Seric 		umask(OldUmask);
13264327Seric 		if (stat(filename, &stb) < 0)
13274431Seric 			stb.st_mode = 0666;
13284327Seric 		if (bitset(0111, stb.st_mode))
13294327Seric 			exit(EX_CANTCREAT);
13304401Seric 		if (ctladdr == NULL)
13316900Seric 			ctladdr = &CurEnv->e_from;
13324327Seric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
13334417Seric 		{
13344417Seric 			if (ctladdr->q_uid == 0)
13354417Seric 				(void) setgid(DefGid);
13364417Seric 			else
13374417Seric 				(void) setgid(ctladdr->q_gid);
13384417Seric 		}
13394327Seric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
13404417Seric 		{
13414417Seric 			if (ctladdr->q_uid == 0)
13424417Seric 				(void) setuid(DefUid);
13434417Seric 			else
13444417Seric 				(void) setuid(ctladdr->q_uid);
13454417Seric 		}
13466887Seric 		f = dfopen(filename, "a");
13474214Seric 		if (f == NULL)
13484214Seric 			exit(EX_CANTCREAT);
13494214Seric 
13506974Seric 		putfromline(f, Mailer[1]);
13516974Seric 		(*CurEnv->e_puthdr)(f, Mailer[1], CurEnv);
13527123Seric 		fputs("\n", f);
13536974Seric 		(*CurEnv->e_putbody)(f, Mailer[1], FALSE);
13544214Seric 		fputs("\n", f);
13554214Seric 		(void) fclose(f);
13564214Seric 		(void) fflush(stdout);
13574417Seric 
13586887Seric 		/* reset ISUID & ISGID bits for paranoid systems */
13594621Seric 		(void) chmod(filename, (int) stb.st_mode);
13604214Seric 		exit(EX_OK);
13614315Seric 		/*NOTREACHED*/
13624214Seric 	}
13634214Seric 	else
13644214Seric 	{
13654214Seric 		/* parent -- wait for exit status */
13664214Seric 		register int i;
13674214Seric 		auto int stat;
13684214Seric 
13694214Seric 		while ((i = wait(&stat)) != pid)
13704214Seric 		{
13714214Seric 			if (i < 0)
13724214Seric 			{
13734214Seric 				stat = EX_OSERR << 8;
13744214Seric 				break;
13754214Seric 			}
13764214Seric 		}
13774215Seric 		if ((stat & 0377) != 0)
13784215Seric 			stat = EX_UNAVAILABLE << 8;
13794214Seric 		return ((stat >> 8) & 0377);
13804214Seric 	}
1381294Seric }
13824550Seric /*
13834550Seric **  SENDALL -- actually send all the messages.
13844550Seric **
13854550Seric **	Parameters:
13867043Seric **		e -- the envelope to send.
13874550Seric **		verifyonly -- if set, only give verification messages.
13884550Seric **
13894550Seric **	Returns:
13904550Seric **		none.
13914550Seric **
13924550Seric **	Side Effects:
13934550Seric **		Scans the send lists and sends everything it finds.
13947043Seric **		Delivers any appropriate error messages.
13954550Seric */
13964550Seric 
13977043Seric sendall(e, verifyonly)
13987043Seric 	ENVELOPE *e;
13994550Seric 	bool verifyonly;
14004550Seric {
14015008Seric 	register ADDRESS *q;
14027779Seric 	bool oldverbose;
14034550Seric 
14045032Seric # ifdef DEBUG
14058248Seric 	if (tTd(13, 1))
14065032Seric 	{
14078248Seric 		printf("\nSENDALL: verify %d, sendqueue:\n");
14087043Seric 		printaddr(e->e_sendqueue, TRUE);
14095032Seric 	}
14105032Seric # endif DEBUG
14115008Seric 
14127043Seric 	/*
14137043Seric 	**  Run through the list and send everything.
14147043Seric 	*/
14157043Seric 
14167779Seric 	oldverbose = Verbose;
14177779Seric 	if (verifyonly)
14187779Seric 		Verbose = TRUE;
14197043Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14204550Seric 	{
14215008Seric 		if (verifyonly)
14224550Seric 		{
14236900Seric 			CurEnv->e_to = q->q_paddr;
14245008Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
14257052Seric 				message(Arpa_Info, "deliverable");
14264550Seric 		}
14275008Seric 		else
14286974Seric 			(void) deliver(q);
14294550Seric 	}
14307779Seric 	Verbose = oldverbose;
14317043Seric 
14327043Seric 	/*
14337043Seric 	**  Now run through and check for errors.
14347043Seric 	*/
14357043Seric 
14367043Seric 	if (verifyonly)
14377043Seric 		return;
14387043Seric 
14397043Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14407043Seric 	{
14417043Seric 		register ADDRESS *qq;
14427043Seric 
14438248Seric # ifdef DEBUG
14448248Seric 		if (tTd(13, 3))
14458248Seric 		{
14468248Seric 			printf("Checking ");
14478248Seric 			printaddr(q, FALSE);
14488248Seric 		}
14498248Seric # endif DEBUG
14508248Seric 
14517043Seric 		if (bitset(QQUEUEUP, q->q_flags))
14527043Seric 			e->e_queueup = TRUE;
14537043Seric 
14547043Seric 		/* we have an address that failed -- find the parent */
14557043Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
14567043Seric 		{
14577043Seric 			char obuf[MAXNAME + 6];
14587043Seric 			extern char *aliaslookup();
14597043Seric 
14607043Seric 			/* we can only have owners for local addresses */
14617043Seric 			if (!bitset(M_LOCAL, qq->q_mailer->m_flags))
14627043Seric 				continue;
14637043Seric 
14647043Seric 			/* see if the owner list exists */
14657043Seric 			(void) strcpy(obuf, "owner-");
14667047Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
14677047Seric 				(void) strcat(obuf, "owner");
14687047Seric 			else
14697047Seric 				(void) strcat(obuf, qq->q_user);
14707043Seric 			if (aliaslookup(obuf) == NULL)
14717043Seric 				continue;
14727043Seric 
14738248Seric # ifdef DEBUG
14748248Seric 			if (tTd(13, 4))
14758248Seric 				printf("Errors to %s\n", obuf);
14768248Seric # endif DEBUG
14778248Seric 
14788248Seric 			/* add in an errors-to field */
14798248Seric 				/*   ugh... must happen before delivery.....
14808248Seric 			addheader("errors-to", newstr(obuf), e);
14818248Seric 				.... i guess this should go in sendto */
14828248Seric 
14838248Seric 			/* only send errors if the message failed */
14848248Seric 			if (!bitset(QBADADDR, q->q_flags))
14858248Seric 				break;
14868248Seric 
14877043Seric 			/* owner list exists -- add it to the error queue */
14887043Seric 			qq->q_flags &= ~QPRIMARY;
14898077Seric 			sendto(obuf, qq, &e->e_errorqueue);
14907043Seric 			MailBack = TRUE;
14917043Seric 			break;
14927043Seric 		}
14937043Seric 
14947043Seric 		/* if we did not find an owner, send to the sender */
14958426Seric 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
14968077Seric 			sendto(e->e_from.q_paddr, qq, &e->e_errorqueue);
14977043Seric 	}
14984550Seric }
14997043Seric /*
15007043Seric **  CHECKERRORS -- check a queue of addresses and process errors.
15017043Seric **
15027043Seric **	Parameters:
15037043Seric **		e -- the envelope to check.
15047043Seric **
15057043Seric **	Returns:
15067043Seric **		none.
15077043Seric **
15087043Seric **	Side Effects:
15097043Seric **		Arranges to queue all tempfailed messages in q
15107043Seric **			or deliver error responses.
15117043Seric */
15127043Seric 
15137043Seric checkerrors(e)
15147043Seric 	register ENVELOPE *e;
15157043Seric {
15167808Seric 	register ADDRESS *q;
15177808Seric 
15187043Seric # ifdef DEBUG
15197672Seric 	if (tTd(4, 1))
15207043Seric 	{
15217371Seric 		printf("\ncheckerrors: FatalErrors %d, errorqueue:\n");
15227043Seric 		printaddr(e->e_errorqueue, TRUE);
15237043Seric 	}
15247043Seric # endif DEBUG
15257043Seric 
15267043Seric 	/* mail back the transcript on errors */
15277043Seric 	if (FatalErrors)
15287043Seric 		savemail();
15297043Seric 
15307043Seric 	/* queue up anything laying around */
15317858Seric 	if (e->e_dontqueue)
15327858Seric 		return;
15337808Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15347043Seric 	{
15357808Seric 		if (bitset(QQUEUEUP, q->q_flags))
15367808Seric 		{
15377043Seric # ifdef QUEUE
15387808Seric 			queueup(e, FALSE);
15398262Seric 			e->e_df = e->e_qf = NULL;
15408262Seric 			e->e_dontqueue = TRUE;
15417043Seric # else QUEUE
15427808Seric 			syserr("checkerrors: trying to queue %s", e->e_df);
15437043Seric # endif QUEUE
15447808Seric 			break;
15457808Seric 		}
15467043Seric 	}
15477043Seric }
1548