1294Seric # include <signal.h>
24123Seric # include <errno.h>
34621Seric # include "sendmail.h"
44327Seric # include <sys/stat.h>
5294Seric 
6*8127Seric SCCSID(@(#)deliver.c	3.111		09/08/82);
7405Seric 
8294Seric /*
94315Seric **  DELIVER -- Deliver a message to a list of addresses.
10294Seric **
114315Seric **	This routine delivers to everyone on the same host as the
124315Seric **	user on the head of the list.  It is clever about mailers
134315Seric **	that don't handle multiple users.  It is NOT guaranteed
144315Seric **	that it will deliver to all these addresses however -- so
154315Seric **	deliver should be called once for each address on the
164315Seric **	list.
174315Seric **
18294Seric **	Parameters:
194621Seric **		firstto -- head of the address list to deliver to.
20294Seric **
21294Seric **	Returns:
22294Seric **		zero -- successfully delivered.
23294Seric **		else -- some failure, see ExitStat for more info.
24294Seric **
25294Seric **	Side Effects:
26294Seric **		The standard input is passed off to someone.
27294Seric */
28294Seric 
296974Seric deliver(firstto)
304621Seric 	ADDRESS *firstto;
31294Seric {
324452Seric 	char *host;			/* host being sent to */
334452Seric 	char *user;			/* user being sent to */
34294Seric 	char **pvp;
353233Seric 	register char **mvp;
363233Seric 	register char *p;
374452Seric 	register struct mailer *m;	/* mailer for this recipient */
382968Seric 	extern bool checkcompat();
393233Seric 	char *pv[MAXPV+1];
404452Seric 	char tobuf[MAXLINE];		/* 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 
755903Seric 	if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags))
765903Seric 	{
775903Seric 		for (; to != NULL; to = to->q_next)
787875Seric 			if (!bitset(QDONTSEND, to->q_flags) &&
797875Seric 			    to->q_mailer == firstto->q_mailer)
805903Seric 				to->q_flags |= QQUEUEUP|QDONTSEND;
815903Seric 		return (0);
825903Seric 	}
835903Seric 
845903Seric 	/*
853233Seric 	**  Do initial argv setup.
863233Seric 	**	Insert the mailer name.  Notice that $x expansion is
873233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
883233Seric 	**	a picky -f flag, we insert it as appropriate.  This
893233Seric 	**	code does not check for 'pv' overflow; this places a
903233Seric 	**	manifest lower limit of 4 for MAXPV.
918062Seric 	**		The from address rewrite is expected to make
928062Seric 	**		the address relative to the other end.
932968Seric 	*/
942968Seric 
954452Seric 	/* rewrite from address, using rewriting rules */
968062Seric 	expand("$f", buf, &buf[sizeof buf - 1], CurEnv);
974452Seric 	mvp = prescan(buf, '\0');
988062Seric 	rewrite(mvp, m->m_s_rwset);
994452Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
1004452Seric 
1014452Seric 	define('g', tfrombuf);		/* translated sender address */
1023233Seric 	define('h', host);		/* to host */
1033233Seric 	Errors = 0;
1043233Seric 	pvp = pv;
1053233Seric 	*pvp++ = m->m_argv[0];
1062968Seric 
1073233Seric 	/* insert -f or -r flag as appropriate */
1083233Seric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1093233Seric 	{
1103233Seric 		if (bitset(M_FOPT, m->m_flags))
1113233Seric 			*pvp++ = "-f";
1123233Seric 		else
1133233Seric 			*pvp++ = "-r";
1146974Seric 		expand("$g", buf, &buf[sizeof buf - 1], CurEnv);
1153233Seric 		*pvp++ = newstr(buf);
1163233Seric 	}
117294Seric 
118294Seric 	/*
1193233Seric 	**  Append the other fixed parts of the argv.  These run
1203233Seric 	**  up to the first entry containing "$u".  There can only
1213233Seric 	**  be one of these, and there are only a few more slots
1223233Seric 	**  in the pv after it.
123294Seric 	*/
124294Seric 
1253233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
126294Seric 	{
1273233Seric 		while ((p = index(p, '$')) != NULL)
1283233Seric 			if (*++p == 'u')
1293233Seric 				break;
1303233Seric 		if (p != NULL)
1313233Seric 			break;
1323233Seric 
1333233Seric 		/* this entry is safe -- go ahead and process it */
1346974Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
1353233Seric 		*pvp++ = newstr(buf);
1363233Seric 		if (pvp >= &pv[MAXPV - 3])
1373233Seric 		{
1383233Seric 			syserr("Too many parameters to %s before $u", pv[0]);
1393233Seric 			return (-1);
1403233Seric 		}
141294Seric 	}
1424863Seric 
1436038Seric 	/*
1446038Seric 	**  If we have no substitution for the user name in the argument
1456038Seric 	**  list, we know that we must supply the names otherwise -- and
1466038Seric 	**  SMTP is the answer!!
1476038Seric 	*/
1486038Seric 
1493233Seric 	if (*mvp == NULL)
1504863Seric 	{
1514863Seric 		/* running SMTP */
1525179Seric # ifdef SMTP
1534863Seric 		clever = TRUE;
1544863Seric 		*pvp = NULL;
1555179Seric # else SMTP
1566038Seric 		/* oops!  we don't implement SMTP */
1575179Seric 		syserr("SMTP style mailer");
1585179Seric 		return (EX_SOFTWARE);
1595179Seric # endif SMTP
1604863Seric 	}
161294Seric 
162294Seric 	/*
1633233Seric 	**  At this point *mvp points to the argument with $u.  We
1643233Seric 	**  run through our address list and append all the addresses
1653233Seric 	**  we can.  If we run out of space, do not fret!  We can
1663233Seric 	**  always send another copy later.
167294Seric 	*/
168294Seric 
1693233Seric 	tobuf[0] = '\0';
1706900Seric 	CurEnv->e_to = tobuf;
1714397Seric 	ctladdr = NULL;
1723233Seric 	for (; to != NULL; to = to->q_next)
173294Seric 	{
1743233Seric 		/* avoid sending multiple recipients to dumb mailers */
1754382Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1763233Seric 			break;
1773233Seric 
1783233Seric 		/* if already sent or not for this host, don't send */
1797052Seric 		if (bitset(QDONTSEND, to->q_flags) ||
1807052Seric 		    strcmp(to->q_host, host) != 0 ||
1817052Seric 		    to->q_mailer != firstto->q_mailer)
1823233Seric 			continue;
1834397Seric 
1845032Seric # ifdef DEBUG
1857672Seric 		if (tTd(10, 1))
1865032Seric 		{
1875032Seric 			printf("\nsend to ");
1885032Seric 			printaddr(to, FALSE);
1895032Seric 		}
1905032Seric # endif DEBUG
1915032Seric 
1924397Seric 		/* compute effective uid/gid when sending */
1934596Seric 		if (to->q_mailer == ProgMailer)
1944397Seric 			ctladdr = getctladdr(to);
1954397Seric 
1963233Seric 		user = to->q_user;
1976900Seric 		CurEnv->e_to = to->q_paddr;
1983233Seric 		to->q_flags |= QDONTSEND;
1993233Seric 
2003233Seric 		/*
2013233Seric 		**  Check to see that these people are allowed to
2023233Seric 		**  talk to each other.
2033233Seric 		*/
2043233Seric 
2053233Seric 		if (!checkcompat(to))
206294Seric 		{
2073233Seric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
2083233Seric 			continue;
209294Seric 		}
2103233Seric 
2113233Seric 		/*
2124099Seric 		**  Strip quote bits from names if the mailer is dumb
2134099Seric 		**	about them.
2143233Seric 		*/
2153233Seric 
2163233Seric 		if (bitset(M_STRIPQ, m->m_flags))
217294Seric 		{
2184099Seric 			stripquotes(user, TRUE);
2194099Seric 			stripquotes(host, TRUE);
2203233Seric 		}
2214099Seric 		else
2224099Seric 		{
2234099Seric 			stripquotes(user, FALSE);
2244099Seric 			stripquotes(host, FALSE);
2254099Seric 		}
2263233Seric 
2273233Seric 		/*
2287052Seric 		**  Do initial connection setup if needed.
2297052Seric 		*/
2307052Seric 
2317052Seric 		if (notopen)
2327052Seric 		{
2337052Seric 			message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
2347052Seric # ifdef SMTP
2357052Seric 			if (clever)
2367052Seric 			{
2377052Seric 				/* send the initial SMTP protocol */
2388003Seric 				rcode = smtpinit(m, pv, (ADDRESS *) NULL);
2397052Seric 			}
2407052Seric # ifdef SMTP
2417052Seric 			notopen = FALSE;
2427052Seric 		}
2437052Seric 
2447052Seric 		/*
2456059Seric 		**  Pass it to the other host if we are running SMTP.
2466059Seric 		*/
2476059Seric 
2486059Seric 		if (clever)
2496059Seric 		{
2506059Seric # ifdef SMTP
2518003Seric 			if (rcode == EX_OK)
2528003Seric 				rcode = smtprcpt(to);
2538003Seric 			if (rcode != EX_OK)
2546059Seric 			{
2558003Seric 				if (rcode == EX_TEMPFAIL)
2566059Seric 					to->q_flags |= QQUEUEUP;
2576059Seric 				else
2586059Seric 					to->q_flags |= QBADADDR;
2598003Seric 				giveresponse(rcode, TRUE, m);
2606059Seric 			}
2616059Seric # else SMTP
2626059Seric 			syserr("trying to be clever");
2636059Seric # endif SMTP
2646059Seric 		}
2656059Seric 
2666059Seric 		/*
2674161Seric 		**  If an error message has already been given, don't
2684161Seric 		**	bother to send to this address.
2694161Seric 		**
2704161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
2714161Seric 		**	>> NOTE >> cannot do any further aliasing; that
2724161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
2734161Seric 		*/
2744161Seric 
2757293Seric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2764161Seric 			continue;
2774161Seric 
2784283Seric 		/* save statistics.... */
2794596Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2806900Seric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
2814283Seric 
2824161Seric 		/*
2833233Seric 		**  See if this user name is "special".
2843233Seric 		**	If the user name has a slash in it, assume that this
2856974Seric 		**	is a file -- send it off without further ado.  Note
2866974Seric 		**	that this type of addresses is not processed along
2876974Seric 		**	with the others, so we fudge on the To person.
2883233Seric 		*/
2893233Seric 
2904596Seric 		if (m == LocalMailer)
2913233Seric 		{
2925599Seric 			if (user[0] == '/')
293294Seric 			{
2948003Seric 				rcode = mailfile(user, getctladdr(to));
2958003Seric 				giveresponse(rcode, TRUE, m);
2963233Seric 				continue;
297294Seric 			}
298294Seric 		}
2993233Seric 
3004315Seric 		/*
3014315Seric 		**  Address is verified -- add this user to mailer
3024315Seric 		**  argv, and add it to the print list of recipients.
3034315Seric 		*/
3044315Seric 
3056059Seric 		/* link together the chain of recipients */
3066272Seric 		to->q_tchain = tochain;
3076272Seric 		tochain = to;
3086059Seric 
3093233Seric 		/* create list of users for error messages */
3103233Seric 		if (tobuf[0] != '\0')
3114082Seric 			(void) strcat(tobuf, ",");
3124082Seric 		(void) strcat(tobuf, to->q_paddr);
3133233Seric 		define('u', user);		/* to user */
3144078Seric 		define('z', to->q_home);	/* user's home */
3153233Seric 
3164863Seric 		/*
3176059Seric 		**  Expand out this user into argument list.
3184863Seric 		*/
3194863Seric 
3206059Seric 		if (!clever)
3213233Seric 		{
3226974Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3234863Seric 			*pvp++ = newstr(buf);
3244863Seric 			if (pvp >= &pv[MAXPV - 2])
3254863Seric 			{
3264863Seric 				/* allow some space for trailing parms */
3274863Seric 				break;
3284863Seric 			}
3294863Seric 		}
330294Seric 	}
331294Seric 
3324067Seric 	/* see if any addresses still exist */
3334067Seric 	if (tobuf[0] == '\0')
3344863Seric 	{
3355179Seric # ifdef SMTP
3364863Seric 		if (clever)
3377228Seric 			smtpquit(pv[0], FALSE);
3385179Seric # endif SMTP
3397003Seric 		define('g', (char *) NULL);
3404067Seric 		return (0);
3414863Seric 	}
3424067Seric 
3433233Seric 	/* print out messages as full list */
3446900Seric 	CurEnv->e_to = tobuf;
3453233Seric 
346294Seric 	/*
3473233Seric 	**  Fill out any parameters after the $u parameter.
348294Seric 	*/
349294Seric 
3504863Seric 	while (!clever && *++mvp != NULL)
351294Seric 	{
3526974Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
3533233Seric 		*pvp++ = newstr(buf);
3543233Seric 		if (pvp >= &pv[MAXPV])
3553233Seric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
356294Seric 	}
3573233Seric 	*pvp++ = NULL;
358294Seric 
359294Seric 	/*
360294Seric 	**  Call the mailer.
3612898Seric 	**	The argument vector gets built, pipes
362294Seric 	**	are created as necessary, and we fork & exec as
3632898Seric 	**	appropriate.
3644863Seric 	**	If we are running SMTP, we just need to clean up.
365294Seric 	*/
366294Seric 
3674397Seric 	if (ctladdr == NULL)
3686900Seric 		ctladdr = &CurEnv->e_from;
3695179Seric # ifdef SMTP
3704863Seric 	if (clever)
3714863Seric 	{
3728003Seric 		rcode = smtpfinish(m, CurEnv);
3738003Seric 		if (rcode != EX_OK)
3748003Seric 			giveresponse(rcode, TRUE, m);
3758003Seric 		smtpquit(pv[0], rcode == EX_OK);
3764863Seric 	}
3774863Seric 	else
3785179Seric # endif SMTP
3798003Seric 		rcode = sendoff(m, pv, ctladdr);
3803233Seric 
3814621Seric 	/*
3824621Seric 	**  If we got a temporary failure, arrange to queue the
3834621Seric 	**  addressees.
3844621Seric 	*/
3854621Seric 
3868003Seric 	if (rcode == EX_TEMPFAIL)
3874621Seric 	{
3885032Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
3894621Seric 			to->q_flags |= QQUEUEUP;
3904621Seric 	}
3914621Seric 
3924488Seric 	errno = 0;
3937003Seric 	define('g', (char *) NULL);
3948003Seric 	return (rcode);
3953233Seric }
3963233Seric /*
3974214Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
3984214Seric **
3994214Seric **	This MUST be a macro, since after a vfork we are running
4004214Seric **	two processes on the same stack!!!
4014214Seric **
4024214Seric **	Parameters:
4034214Seric **		none.
4044214Seric **
4054214Seric **	Returns:
4064214Seric **		From a macro???  You've got to be kidding!
4074214Seric **
4084214Seric **	Side Effects:
4094214Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4104214Seric **			pid of child in parent, zero in child.
4114214Seric **			-1 on unrecoverable error.
4124214Seric **
4134214Seric **	Notes:
4144214Seric **		I'm awfully sorry this looks so awful.  That's
4154214Seric **		vfork for you.....
4164214Seric */
4174214Seric 
4184214Seric # define NFORKTRIES	5
4194214Seric # ifdef VFORK
4204214Seric # define XFORK	vfork
4214214Seric # else VFORK
4224214Seric # define XFORK	fork
4234214Seric # endif VFORK
4244214Seric 
4254214Seric # define DOFORK(fORKfN) \
4264214Seric {\
4274214Seric 	register int i;\
4284214Seric \
4294214Seric 	for (i = NFORKTRIES; i-- > 0; )\
4304214Seric 	{\
4314214Seric 		pid = fORKfN();\
4324214Seric 		if (pid >= 0)\
4334214Seric 			break;\
4347003Seric 		sleep(NFORKTRIES - i);\
4354214Seric 	}\
4364214Seric }
4374214Seric /*
4384637Seric **  DOFORK -- simple fork interface to DOFORK.
4394637Seric **
4404637Seric **	Parameters:
4414637Seric **		none.
4424637Seric **
4434637Seric **	Returns:
4444637Seric **		pid of child in parent.
4454637Seric **		zero in child.
4464637Seric **		-1 on error.
4474637Seric **
4484637Seric **	Side Effects:
4494637Seric **		returns twice, once in parent and once in child.
4504637Seric */
4514637Seric 
4524637Seric dofork()
4534637Seric {
4544637Seric 	register int pid;
4554637Seric 
4564637Seric 	DOFORK(fork);
4574637Seric 	return (pid);
4584637Seric }
4594637Seric /*
4603233Seric **  SENDOFF -- send off call to mailer & collect response.
4613233Seric **
4623233Seric **	Parameters:
4633233Seric **		m -- mailer descriptor.
4643233Seric **		pvp -- parameter vector to send to it.
4654397Seric **		ctladdr -- an address pointer controlling the
4664397Seric **			user/groupid etc. of the mailer.
4673233Seric **
4683233Seric **	Returns:
4693233Seric **		exit status of mailer.
4703233Seric **
4713233Seric **	Side Effects:
4723233Seric **		none.
4733233Seric */
4743233Seric 
4756974Seric sendoff(m, pvp, ctladdr)
4763233Seric 	struct mailer *m;
4773233Seric 	char **pvp;
4784397Seric 	ADDRESS *ctladdr;
4793233Seric {
4804863Seric 	auto FILE *mfile;
4814863Seric 	auto FILE *rfile;
4823233Seric 	register int i;
4833233Seric 	int pid;
4844863Seric 
4854863Seric 	/*
4864863Seric 	**  Create connection to mailer.
4874863Seric 	*/
4884863Seric 
4894863Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
4904863Seric 	if (pid < 0)
4914863Seric 		return (-1);
4924863Seric 
4934863Seric 	/*
4944863Seric 	**  Format and send message.
4954863Seric 	*/
4964863Seric 
4974863Seric 	(void) signal(SIGPIPE, SIG_IGN);
4986974Seric 	putfromline(mfile, m);
4996974Seric 	(*CurEnv->e_puthdr)(mfile, m, CurEnv);
5006974Seric 	fprintf(mfile, "\n");
5016974Seric 	(*CurEnv->e_putbody)(mfile, m, FALSE);
5024863Seric 	(void) fclose(mfile);
5034863Seric 
5044863Seric 	i = endmailer(pid, pvp[0]);
5054863Seric 	giveresponse(i, TRUE, m);
5065981Seric 
5075981Seric 	/* arrange a return receipt if requested */
5088062Seric 	if (CurEnv->e_receiptto != NULL && bitset(M_LOCAL, m->m_flags))
5095981Seric 	{
5106900Seric 		CurEnv->e_sendreceipt = TRUE;
5118062Seric 		if (ExitStat == EX_OK)
5128062Seric 			fprintf(Xscript, "%s... successfully delivered\n",
5138062Seric 				CurEnv->e_to);
5145981Seric 		/* do we want to send back more info? */
5155981Seric 	}
5165981Seric 
5174863Seric 	return (i);
5184863Seric }
5194863Seric /*
5204863Seric **  ENDMAILER -- Wait for mailer to terminate.
5214863Seric **
5224863Seric **	We should never get fatal errors (e.g., segmentation
5234863Seric **	violation), so we report those specially.  For other
5244863Seric **	errors, we choose a status message (into statmsg),
5254863Seric **	and if it represents an error, we print it.
5264863Seric **
5274863Seric **	Parameters:
5284863Seric **		pid -- pid of mailer.
5294863Seric **		name -- name of mailer (for error messages).
5304863Seric **
5314863Seric **	Returns:
5324863Seric **		exit code of mailer.
5334863Seric **
5344863Seric **	Side Effects:
5354863Seric **		none.
5364863Seric */
5374863Seric 
5384863Seric endmailer(pid, name)
5394863Seric 	int pid;
5404863Seric 	char *name;
5414863Seric {
5424863Seric 	register int i;
5434863Seric 	auto int st;
5444863Seric 
5456038Seric 	/* in the IPC case there is nothing to wait for */
5466038Seric 	if (pid == 0)
5476038Seric 		return (EX_OK);
5486038Seric 
5496038Seric 	/* wait for the mailer process to die and collect status */
550*8127Seric 	do
551*8127Seric 	{
552*8127Seric 		errno = 0;
553*8127Seric 		i = wait(&st);
554*8127Seric 		if (i < 0 && errno == EINTR)
555*8127Seric 			continue;
556*8127Seric 	} while (i >= 0 && i != pid);
5574863Seric 	if (i < 0)
5584863Seric 	{
5594863Seric 		syserr("wait");
5604863Seric 		return (-1);
5614863Seric 	}
5626038Seric 
5636038Seric 	/* see if it died a horrid death */
5644863Seric 	if ((st & 0377) != 0)
5654863Seric 	{
5664863Seric 		syserr("%s: stat %o", name, st);
5674863Seric 		ExitStat = EX_UNAVAILABLE;
5684863Seric 		return (-1);
5694863Seric 	}
5706038Seric 
5716038Seric 	/* normal death -- return status */
5724863Seric 	i = (st >> 8) & 0377;
5734863Seric 	return (i);
5744863Seric }
5754863Seric /*
5764863Seric **  OPENMAILER -- open connection to mailer.
5774863Seric **
5784863Seric **	Parameters:
5794863Seric **		m -- mailer descriptor.
5804863Seric **		pvp -- parameter vector to pass to mailer.
5814863Seric **		ctladdr -- controlling address for user.
5824863Seric **		clever -- create a full duplex connection.
5834863Seric **		pmfile -- pointer to mfile (to mailer) connection.
5844863Seric **		prfile -- pointer to rfile (from mailer) connection.
5854863Seric **
5864863Seric **	Returns:
5876038Seric **		pid of mailer ( > 0 ).
5884863Seric **		-1 on error.
5896038Seric **		zero on an IPC connection.
5904863Seric **
5914863Seric **	Side Effects:
5924863Seric **		creates a mailer in a subprocess.
5934863Seric */
5944863Seric 
5954863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
5964863Seric 	struct mailer *m;
5974863Seric 	char **pvp;
5984863Seric 	ADDRESS *ctladdr;
5994863Seric 	bool clever;
6004863Seric 	FILE **pmfile;
6014863Seric 	FILE **prfile;
6024863Seric {
6034863Seric 	int pid;
6044709Seric 	int mpvect[2];
6054863Seric 	int rpvect[2];
6063233Seric 	FILE *mfile;
6074863Seric 	FILE *rfile;
6083233Seric 	extern FILE *fdopen();
6093233Seric 
6103233Seric # ifdef DEBUG
6117672Seric 	if (tTd(11, 1))
612294Seric 	{
6134863Seric 		printf("openmailer:\n");
6143233Seric 		printav(pvp);
615294Seric 	}
6163233Seric # endif DEBUG
6174488Seric 	errno = 0;
6183233Seric 
6196038Seric # ifdef DAEMON
6206038Seric 	/*
6216038Seric 	**  Deal with the special case of mail handled through an IPC
6226038Seric 	**  connection.
6236038Seric 	**	In this case we don't actually fork.  We must be
6246038Seric 	**	running SMTP for this to work.  We will return a
6256038Seric 	**	zero pid to indicate that we are running IPC.
6266038Seric 	*/
6276038Seric 
6286038Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
6296038Seric 	{
6306038Seric 		register int i;
6317285Seric 		register u_short port;
6326038Seric 
6336038Seric 		if (!clever)
6346038Seric 			syserr("non-clever IPC");
6356632Seric 		if (pvp[2] != NULL)
6367285Seric 			port = atoi(pvp[2]);
6376632Seric 		else
6387285Seric 			port = 0;
6397285Seric 		i = makeconnection(pvp[1], port, pmfile, prfile);
6406038Seric 		if (i != EX_OK)
6416047Seric 		{
6426047Seric 			ExitStat = i;
6436038Seric 			return (-1);
6446047Seric 		}
6456038Seric 		else
6466038Seric 			return (0);
6476038Seric 	}
6486038Seric # endif DAEMON
6496038Seric 
6502898Seric 	/* create a pipe to shove the mail through */
6514709Seric 	if (pipe(mpvect) < 0)
652294Seric 	{
6534863Seric 		syserr("pipe (to mailer)");
654294Seric 		return (-1);
655294Seric 	}
6564863Seric 
6575179Seric # ifdef SMTP
6584863Seric 	/* if this mailer speaks smtp, create a return pipe */
6594863Seric 	if (clever && pipe(rpvect) < 0)
6604863Seric 	{
6614863Seric 		syserr("pipe (from mailer)");
6624863Seric 		(void) close(mpvect[0]);
6634863Seric 		(void) close(mpvect[1]);
6644863Seric 		return (-1);
6654863Seric 	}
6665179Seric # endif SMTP
6674863Seric 
6686038Seric 	/*
6696038Seric 	**  Actually fork the mailer process.
6706038Seric 	**	DOFORK is clever about retrying.
6716038Seric 	*/
6726038Seric 
6737672Seric 	(void) fflush(Xscript);				/* for debugging */
6744214Seric 	DOFORK(XFORK);
6754327Seric 	/* pid is set by DOFORK */
676294Seric 	if (pid < 0)
677294Seric 	{
6786038Seric 		/* failure */
679294Seric 		syserr("Cannot fork");
6804709Seric 		(void) close(mpvect[0]);
6814709Seric 		(void) close(mpvect[1]);
6824863Seric 		if (clever)
6834863Seric 		{
6844863Seric 			(void) close(rpvect[0]);
6854863Seric 			(void) close(rpvect[1]);
6864863Seric 		}
687294Seric 		return (-1);
688294Seric 	}
689294Seric 	else if (pid == 0)
690294Seric 	{
691294Seric 		/* child -- set up input & exec mailer */
6921621Seric 		/* make diagnostic output be standard output */
6934477Seric 		(void) signal(SIGINT, SIG_IGN);
6944477Seric 		(void) signal(SIGHUP, SIG_IGN);
6954215Seric 		(void) signal(SIGTERM, SIG_DFL);
6964709Seric 
6974709Seric 		/* arrange to filter standard & diag output of command */
6984863Seric 		if (clever)
6994709Seric 		{
7004863Seric 			(void) close(rpvect[0]);
7014709Seric 			(void) close(1);
7024863Seric 			(void) dup(rpvect[1]);
7034863Seric 			(void) close(rpvect[1]);
7044863Seric 		}
7054863Seric 		else if (OutChannel != stdout)
7064863Seric 		{
7074863Seric 			(void) close(1);
7084709Seric 			(void) dup(fileno(OutChannel));
7094709Seric 		}
7104082Seric 		(void) close(2);
7114082Seric 		(void) dup(1);
7124709Seric 
7134709Seric 		/* arrange to get standard input */
7144709Seric 		(void) close(mpvect[1]);
7154082Seric 		(void) close(0);
7164709Seric 		if (dup(mpvect[0]) < 0)
717294Seric 		{
7182898Seric 			syserr("Cannot dup to zero!");
7192898Seric 			_exit(EX_OSERR);
720294Seric 		}
7214709Seric 		(void) close(mpvect[0]);
7222968Seric 		if (!bitset(M_RESTR, m->m_flags))
7234215Seric 		{
7244417Seric 			if (ctladdr->q_uid == 0)
7254417Seric 			{
7264417Seric 				(void) setgid(DefGid);
7274417Seric 				(void) setuid(DefUid);
7284417Seric 			}
7294417Seric 			else
7304415Seric 			{
7314417Seric 				(void) setgid(ctladdr->q_gid);
7324417Seric 				(void) setuid(ctladdr->q_uid);
7334415Seric 			}
7344215Seric 		}
7352774Seric # ifndef VFORK
7362774Seric 		/*
7372774Seric 		**  We have to be careful with vfork - we can't mung up the
7382774Seric 		**  memory but we don't want the mailer to inherit any extra
7392774Seric 		**  open files.  Chances are the mailer won't
7402774Seric 		**  care about an extra file, but then again you never know.
7412774Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
7422774Seric 		**  declared static so we can't.  But if we fclose(pwf), which
7432774Seric 		**  is what endpwent does, it closes it in the parent too and
7442774Seric 		**  the next getpwnam will be slower.  If you have a weird
7452774Seric 		**  mailer that chokes on the extra file you should do the
7462774Seric 		**  endpwent().
7472774Seric 		**
7482774Seric 		**  Similar comments apply to log.  However, openlog is
7492774Seric 		**  clever enough to set the FIOCLEX mode on the file,
7502774Seric 		**  so it will be closed automatically on the exec.
7512774Seric 		*/
7522774Seric 
7532774Seric 		endpwent();
754294Seric # ifdef LOG
7552089Seric 		closelog();
756294Seric # endif LOG
7572774Seric # endif VFORK
7586038Seric 
7596038Seric 		/* try to execute the mailer */
760294Seric 		execv(m->m_mailer, pvp);
7616038Seric 
762294Seric 		/* syserr fails because log is closed */
763294Seric 		/* syserr("Cannot exec %s", m->m_mailer); */
7644214Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
7654082Seric 		(void) fflush(stdout);
7661619Seric 		_exit(EX_UNAVAILABLE);
767294Seric 	}
768294Seric 
7694709Seric 	/*
7704863Seric 	**  Set up return value.
7714709Seric 	*/
7724709Seric 
7734709Seric 	(void) close(mpvect[0]);
7744709Seric 	mfile = fdopen(mpvect[1], "w");
7754863Seric 	if (clever)
7764863Seric 	{
7774863Seric 		(void) close(rpvect[1]);
7784863Seric 		rfile = fdopen(rpvect[0], "r");
7794863Seric 	}
780294Seric 
7814863Seric 	*pmfile = mfile;
7824863Seric 	*prfile = rfile;
783294Seric 
7844863Seric 	return (pid);
785294Seric }
786294Seric /*
787294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
788294Seric **
789294Seric **	Parameters:
790294Seric **		stat -- the status code from the mailer (high byte
791294Seric **			only; core dumps must have been taken care of
792294Seric **			already).
793294Seric **		force -- if set, force an error message output, even
794294Seric **			if the mailer seems to like to print its own
795294Seric **			messages.
796294Seric **		m -- the mailer descriptor for this mailer.
797294Seric **
798294Seric **	Returns:
7994082Seric **		none.
800294Seric **
801294Seric **	Side Effects:
8021518Seric **		Errors may be incremented.
803294Seric **		ExitStat may be set.
804294Seric */
805294Seric 
806294Seric giveresponse(stat, force, m)
807294Seric 	int stat;
8087228Seric 	bool force;
809294Seric 	register struct mailer *m;
810294Seric {
811294Seric 	register char *statmsg;
812294Seric 	extern char *SysExMsg[];
813294Seric 	register int i;
814294Seric 	extern int N_SysEx;
8151624Seric 	char buf[30];
816294Seric 
8174315Seric 	/*
8184315Seric 	**  Compute status message from code.
8194315Seric 	*/
8204315Seric 
821294Seric 	i = stat - EX__BASE;
822294Seric 	if (i < 0 || i > N_SysEx)
823294Seric 		statmsg = NULL;
824294Seric 	else
825294Seric 		statmsg = SysExMsg[i];
826294Seric 	if (stat == 0)
8274065Seric 	{
8288003Seric 		statmsg = "250 sent";
8298003Seric 		message(Arpa_Info, &statmsg[4]);
8304065Seric 	}
8314621Seric 	else if (stat == EX_TEMPFAIL)
8324621Seric 	{
8337875Seric 		message(Arpa_Info, "deferred");
8344621Seric 	}
835294Seric 	else
836294Seric 	{
8371518Seric 		Errors++;
8386047Seric 		FatalErrors = TRUE;
839294Seric 		if (statmsg == NULL && m->m_badstat != 0)
840294Seric 		{
841294Seric 			stat = m->m_badstat;
842294Seric 			i = stat - EX__BASE;
843294Seric # ifdef DEBUG
844294Seric 			if (i < 0 || i >= N_SysEx)
845294Seric 				syserr("Bad m_badstat %d", stat);
846294Seric 			else
847294Seric # endif DEBUG
8487344Seric 				statmsg = SysExMsg[i];
849294Seric 		}
850294Seric 		if (statmsg == NULL)
851294Seric 			usrerr("unknown mailer response %d", stat);
8524065Seric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
8538003Seric 			usrerr(statmsg);
8547344Seric 		else
8558003Seric 			fprintf(Xscript, "%s\n", &statmsg[4]);
856294Seric 	}
857294Seric 
858294Seric 	/*
859294Seric 	**  Final cleanup.
860294Seric 	**	Log a record of the transaction.  Compute the new
861294Seric 	**	ExitStat -- if we already had an error, stick with
862294Seric 	**	that.
863294Seric 	*/
864294Seric 
8651624Seric 	if (statmsg == NULL)
8661624Seric 	{
8678003Seric 		(void) sprintf(buf, "554 error %d", stat);
8681624Seric 		statmsg = buf;
8691624Seric 	}
8701624Seric 
871294Seric # ifdef LOG
8727680Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
8737858Seric 	{
8747858Seric 		extern char *pintvl();
8757858Seric 
8767858Seric 		syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id,
8777883Seric 		       CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE),
8788003Seric 		       &statmsg[4]);
8797858Seric 	}
880294Seric # endif LOG
8814621Seric 	if (stat != EX_TEMPFAIL)
8824621Seric 		setstat(stat);
883294Seric }
884294Seric /*
8856974Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
886294Seric **
8876974Seric **	This can be made an arbitrary message separator by changing $l
888294Seric **
8896974Seric **	One of the ugliest hacks seen by human eyes is
8906974Seric **	contained herein: UUCP wants those stupid
8916974Seric **	"remote from <host>" lines.  Why oh why does a
8926974Seric **	well-meaning programmer such as myself have to
8936974Seric **	deal with this kind of antique garbage????
8946974Seric **
895294Seric **	Parameters:
8966974Seric **		fp -- the file to output to.
8976974Seric **		m -- the mailer describing this entry.
898294Seric **
899294Seric **	Returns:
9006974Seric **		none
901294Seric **
902294Seric **	Side Effects:
9036974Seric **		outputs some text to fp.
904294Seric */
905294Seric 
9066974Seric putfromline(fp, m)
9076974Seric 	register FILE *fp;
9086974Seric 	register MAILER *m;
909294Seric {
9106974Seric 	char buf[MAXLINE];
911294Seric 
9126974Seric 	if (bitset(M_NHDR, m->m_flags))
9136974Seric 		return;
9144315Seric 
9156974Seric # ifdef UGLYUUCP
9166974Seric 	if (bitset(M_UGLYUUCP, m->m_flags))
9174205Seric 	{
9186974Seric 		extern char *macvalue();
9196974Seric 		char *sys = macvalue('g');
9206974Seric 		char *bang = index(sys, '!');
9216041Seric 
9226974Seric 		if (bang == NULL)
9236974Seric 			syserr("No ! in UUCP! (%s)", sys);
9245099Seric 		else
9256974Seric 			*bang = '\0';
9267232Seric 		expand("From $f  $d remote from $g\n", buf,
9276974Seric 				&buf[sizeof buf - 1], CurEnv);
9286974Seric 		*bang = '!';
9296974Seric 	}
9306974Seric 	else
9315179Seric # endif UGLYUUCP
9326974Seric 		expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv);
9337123Seric 	putline(buf, fp, bitset(M_FULLSMTP, m->m_flags));
9345981Seric }
9355981Seric /*
9366974Seric **  PUTHEADER -- put the header part of a message from the in-core copy
9375981Seric **
9385981Seric **	Parameters:
9395981Seric **		fp -- file to put it on.
9405981Seric **		m -- mailer to use.
9416974Seric **		e -- envelope to use.
9425981Seric **
9435981Seric **	Returns:
9445981Seric **		none.
9455981Seric **
9465981Seric **	Side Effects:
9475981Seric **		none.
9485981Seric */
9495981Seric 
9506974Seric putheader(fp, m, e)
9515981Seric 	register FILE *fp;
9525981Seric 	register struct mailer *m;
9536974Seric 	register ENVELOPE *e;
9545981Seric {
9555981Seric 	char buf[BUFSIZ];
9565981Seric 	register HDR *h;
9575981Seric 	extern char *arpadate();
9585981Seric 	extern char *capitalize();
9595981Seric 	extern char *hvalue();
9605981Seric 	extern bool samefrom();
9617123Seric 	char obuf[MAXLINE];
9627123Seric 	register char *obp;
9637123Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
9645981Seric 
9656974Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
9661828Seric 	{
9674315Seric 		register char *p;
9684315Seric 
9693389Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
9708062Seric 			continue;
9714447Seric 
9727756Seric 		p = h->h_value;
9738062Seric 		if (bitset(H_DEFAULT, h->h_flags))
9743385Seric 		{
9757666Seric 			/* macro expand value if generated internally */
9768062Seric 			expand(p, buf, &buf[sizeof buf], e);
9773385Seric 			p = buf;
9783385Seric 		}
9794447Seric 		if (p == NULL || *p == '\0')
9803389Seric 			continue;
9814209Seric 
9828062Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
9834209Seric 		{
9848062Seric 			/* address field */
9858093Seric 			bool oldstyle = e->e_oldstyle;
9868093Seric 
9878093Seric 			if (bitset(H_FROM, h->h_flags))
9888093Seric 				oldstyle = FALSE;
9898093Seric 			commaize(h, p, fp, oldstyle, m);
9904447Seric 		}
9918062Seric 		else
9924447Seric 		{
9938062Seric 			/* vanilla header line */
9947123Seric 			(void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p);
9957123Seric 			putline(obuf, fp, fullsmtp);
9964209Seric 		}
9978062Seric 		h->h_flags |= H_USED;
9982898Seric 	}
999294Seric }
1000294Seric /*
10017756Seric **  COMMAIZE -- output a header field, making a comma-translated list.
10027756Seric **
10037756Seric **	Parameters:
10048062Seric **		h -- the header field to output.
10058062Seric **		p -- the value to put in it.
10067756Seric **		fp -- file to put it to.
10077756Seric **		oldstyle -- TRUE if this is an old style header.
10087756Seric **		m -- a pointer to the mailer descriptor.
10097756Seric **
10107756Seric **	Returns:
10117756Seric **		none.
10127756Seric **
10137756Seric **	Side Effects:
10147756Seric **		outputs "p" to file "fp".
10157756Seric */
10167756Seric 
10178062Seric commaize(h, p, fp, oldstyle, m)
10188062Seric 	register HDR *h;
10197756Seric 	register char *p;
10207756Seric 	FILE *fp;
10217756Seric 	bool oldstyle;
10228062Seric 	register MAILER *m;
10237756Seric {
10248062Seric 	register char *obp;
10258062Seric 	int opos;
10268062Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
10277756Seric 	bool firstone = TRUE;
10287756Seric 	char obuf[MAXLINE];
10297756Seric 
10307756Seric 	/*
10317756Seric 	**  Output the address list translated by the
10327756Seric 	**  mailer and with commas.
10337756Seric 	*/
10347756Seric 
10357756Seric # ifdef DEBUG
10367756Seric 	if (tTd(14, 2))
10378062Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
10387756Seric # endif DEBUG
10397756Seric 
10407756Seric 	obp = obuf;
10418062Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
10428062Seric 	opos = strlen(h->h_field) + 2;
10437756Seric 	obp += opos;
10447756Seric 
10457756Seric 	/*
10467756Seric 	**  Run through the list of values.
10477756Seric 	*/
10487756Seric 
10497756Seric 	while (*p != '\0')
10507756Seric 	{
10517756Seric 		register char *name;
10527756Seric 		char savechar;
10538062Seric 		extern char *remotename();
10548077Seric 		extern char *DelimChar;		/* defined in prescan */
10557756Seric 
10567756Seric 		/*
10577756Seric 		**  Find the end of the name.  New style names
10587756Seric 		**  end with a comma, old style names end with
10597756Seric 		**  a space character.  However, spaces do not
10607756Seric 		**  necessarily delimit an old-style name -- at
10617756Seric 		**  signs mean keep going.
10627756Seric 		*/
10637756Seric 
10648077Seric 		/* find end of name */
10658077Seric 		while (isspace(*p) || *p == ',')
10667756Seric 			p++;
10677756Seric 		name = p;
10688077Seric 		for (;;)
10697756Seric 		{
10708077Seric 			char *oldp;
10717756Seric 			extern bool isatword();
10727756Seric 
10738091Seric 			(void) prescan(p, oldstyle ? ' ' : ',');
10748077Seric 			p = DelimChar;
10757756Seric 
10767756Seric 			/* look to see if we have an at sign */
10777756Seric 			oldp = p;
10787756Seric 			while (*p != '\0' && isspace(*p))
10797756Seric 				p++;
10807756Seric 
10817756Seric 			if (*p != '@' && !isatword(p))
10827756Seric 			{
10837756Seric 				p = oldp;
10847756Seric 				break;
10857756Seric 			}
10867756Seric 			p += *p == '@' ? 1 : 2;
10877756Seric 			while (*p != '\0' && isspace(*p))
10887756Seric 				p++;
10897756Seric 		}
10907756Seric 		/* at the end of one complete name */
10917756Seric 
10927756Seric 		/* strip off trailing white space */
10937756Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
10947756Seric 			p--;
10957756Seric 		if (++p == name)
10967756Seric 			continue;
10977756Seric 		savechar = *p;
10987756Seric 		*p = '\0';
10997756Seric 
11007756Seric 		/* translate the name to be relative */
11018062Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags));
11027756Seric 		if (*name == '\0')
11037756Seric 		{
11047756Seric 			*p = savechar;
11057756Seric 			continue;
11067756Seric 		}
11077756Seric 
11087756Seric 		/* output the name with nice formatting */
11097756Seric 		opos += strlen(name);
11107756Seric 		if (!firstone)
11117756Seric 			opos += 2;
11127756Seric 		if (opos > 78 && !firstone)
11137756Seric 		{
11147756Seric 			(void) sprintf(obp, ",\n");
11157756Seric 			putline(obuf, fp, fullsmtp);
11167756Seric 			obp = obuf;
11177756Seric 			(void) sprintf(obp, "        ");
11187756Seric 			obp += strlen(obp);
11197756Seric 			opos = 8 + strlen(name);
11207756Seric 		}
11217756Seric 		else if (!firstone)
11227756Seric 		{
11237756Seric 			(void) sprintf(obp, ", ");
11247756Seric 			obp += 2;
11257756Seric 		}
11267756Seric 		(void) sprintf(obp, "%s", name);
11277756Seric 		obp += strlen(obp);
11287756Seric 		firstone = FALSE;
11297756Seric 		*p = savechar;
11307756Seric 	}
11317756Seric 	(void) strcpy(obp, "\n");
11327756Seric 	putline(obuf, fp, fullsmtp);
11337756Seric }
11347756Seric /*
11356974Seric **  PUTBODY -- put the body of a message.
11366974Seric **
11376974Seric **	Parameters:
11386974Seric **		fp -- file to output onto.
11396974Seric **		m -- a mailer descriptor.
11406974Seric **		xdot -- if set, use SMTP hidden dot algorithm.
11416974Seric **
11426974Seric **	Returns:
11436974Seric **		none.
11446974Seric **
11456974Seric **	Side Effects:
11466974Seric **		The message is written onto fp.
11476974Seric */
11486974Seric 
11496974Seric putbody(fp, m, xdot)
11506974Seric 	FILE *fp;
11516974Seric 	struct mailer *m;
11526974Seric 	bool xdot;
11536974Seric {
11546974Seric 	char buf[MAXLINE + 1];
11557123Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
11566974Seric 
11576974Seric 	/*
11586974Seric 	**  Output the body of the message
11596974Seric 	*/
11606974Seric 
11617003Seric #ifdef lint
11627003Seric 	/* m will be needed later for complete smtp emulation */
11637003Seric 	if (m == NULL)
11647003Seric 		return;
11657003Seric #endif lint
11667003Seric 
11676974Seric 	if (TempFile != NULL)
11686974Seric 	{
11696974Seric 		rewind(TempFile);
11706974Seric 		buf[0] = '.';
11716974Seric 		while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL)
11727123Seric 			putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp);
11736974Seric 
11746974Seric 		if (ferror(TempFile))
11756974Seric 		{
11766974Seric 			syserr("putbody: read error");
11776974Seric 			ExitStat = EX_IOERR;
11786974Seric 		}
11796974Seric 	}
11806974Seric 
11816974Seric 	(void) fflush(fp);
11826974Seric 	if (ferror(fp) && errno != EPIPE)
11836974Seric 	{
11846974Seric 		syserr("putbody: write error");
11856974Seric 		ExitStat = EX_IOERR;
11866974Seric 	}
11876974Seric 	errno = 0;
11886974Seric }
11896974Seric /*
11905936Seric **  ISATWORD -- tell if the word we are pointing to is "at".
11915936Seric **
11925936Seric **	Parameters:
11935936Seric **		p -- word to check.
11945936Seric **
11955936Seric **	Returns:
11965936Seric **		TRUE -- if p is the word at.
11975936Seric **		FALSE -- otherwise.
11985936Seric **
11995936Seric **	Side Effects:
12005936Seric **		none.
12015936Seric */
12025936Seric 
12035936Seric bool
12045936Seric isatword(p)
12055936Seric 	register char *p;
12065936Seric {
12075936Seric 	extern char lower();
12085936Seric 
12095936Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
12105936Seric 	    p[2] != '\0' && isspace(p[2]))
12115936Seric 		return (TRUE);
12125936Seric 	return (FALSE);
12135936Seric }
12145936Seric /*
12154370Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
12164370Seric **
12174370Seric **	Parameters:
12184370Seric **		ifrom -- internally generated form of from address.
12194370Seric **		efrom -- external form of from address.
12204370Seric **
12214370Seric **	Returns:
12224370Seric **		TRUE -- if they convey the same info.
12234370Seric **		FALSE -- if any information has been lost.
12244370Seric **
12254370Seric **	Side Effects:
12264370Seric **		none.
12274370Seric */
12284370Seric 
12294370Seric bool
12304370Seric samefrom(ifrom, efrom)
12314370Seric 	char *ifrom;
12324370Seric 	char *efrom;
12334370Seric {
12344447Seric 	register char *p;
12354447Seric 	char buf[MAXNAME + 4];
12364447Seric 
12374447Seric # ifdef DEBUG
12387672Seric 	if (tTd(3, 8))
12394447Seric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
12404447Seric # endif DEBUG
12414447Seric 	if (strcmp(ifrom, efrom) == 0)
12424447Seric 		goto success;
12434447Seric 	p = index(ifrom, '@');
12444447Seric 	if (p == NULL)
12454447Seric 		goto failure;
12464447Seric 	*p = '\0';
12477003Seric 	(void) strcpy(buf, ifrom);
12487003Seric 	(void) strcat(buf, " at ");
12494447Seric 	*p++ = '@';
12507003Seric 	(void) strcat(buf, p);
12514447Seric 	if (strcmp(buf, efrom) == 0)
12524447Seric 		goto success;
12534447Seric 
12544447Seric   failure:
12554447Seric # ifdef DEBUG
12567672Seric 	if (tTd(3, 8))
12574447Seric 		printf("FALSE\n");
12584447Seric # endif DEBUG
12594447Seric 	return (FALSE);
12604447Seric 
12614447Seric   success:
12624447Seric # ifdef DEBUG
12637672Seric 	if (tTd(3, 8))
12644447Seric 		printf("TRUE\n");
12654447Seric # endif DEBUG
12664447Seric 	return (TRUE);
12674370Seric }
12684370Seric /*
1269294Seric **  MAILFILE -- Send a message to a file.
1270294Seric **
12714327Seric **	If the file has the setuid/setgid bits set, but NO execute
12724327Seric **	bits, sendmail will try to become the owner of that file
12734327Seric **	rather than the real user.  Obviously, this only works if
12744327Seric **	sendmail runs as root.
12754327Seric **
1276294Seric **	Parameters:
1277294Seric **		filename -- the name of the file to send to.
12784397Seric **		ctladdr -- the controlling address header -- includes
12794397Seric **			the userid/groupid to be when sending.
1280294Seric **
1281294Seric **	Returns:
1282294Seric **		The exit code associated with the operation.
1283294Seric **
1284294Seric **	Side Effects:
1285294Seric **		none.
1286294Seric */
1287294Seric 
12884397Seric mailfile(filename, ctladdr)
1289294Seric 	char *filename;
12904397Seric 	ADDRESS *ctladdr;
1291294Seric {
1292294Seric 	register FILE *f;
12934214Seric 	register int pid;
1294294Seric 
12954214Seric 	/*
12964214Seric 	**  Fork so we can change permissions here.
12974214Seric 	**	Note that we MUST use fork, not vfork, because of
12984214Seric 	**	the complications of calling subroutines, etc.
12994214Seric 	*/
13004067Seric 
13014214Seric 	DOFORK(fork);
13024214Seric 
13034214Seric 	if (pid < 0)
13044214Seric 		return (EX_OSERR);
13054214Seric 	else if (pid == 0)
13064214Seric 	{
13074214Seric 		/* child -- actually write to file */
13084327Seric 		struct stat stb;
13094327Seric 
13104215Seric 		(void) signal(SIGINT, SIG_DFL);
13114215Seric 		(void) signal(SIGHUP, SIG_DFL);
13124215Seric 		(void) signal(SIGTERM, SIG_DFL);
13134327Seric 		umask(OldUmask);
13144327Seric 		if (stat(filename, &stb) < 0)
13154431Seric 			stb.st_mode = 0666;
13164327Seric 		if (bitset(0111, stb.st_mode))
13174327Seric 			exit(EX_CANTCREAT);
13184401Seric 		if (ctladdr == NULL)
13196900Seric 			ctladdr = &CurEnv->e_from;
13204327Seric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
13214417Seric 		{
13224417Seric 			if (ctladdr->q_uid == 0)
13234417Seric 				(void) setgid(DefGid);
13244417Seric 			else
13254417Seric 				(void) setgid(ctladdr->q_gid);
13264417Seric 		}
13274327Seric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
13284417Seric 		{
13294417Seric 			if (ctladdr->q_uid == 0)
13304417Seric 				(void) setuid(DefUid);
13314417Seric 			else
13324417Seric 				(void) setuid(ctladdr->q_uid);
13334417Seric 		}
13346887Seric 		f = dfopen(filename, "a");
13354214Seric 		if (f == NULL)
13364214Seric 			exit(EX_CANTCREAT);
13374214Seric 
13386974Seric 		putfromline(f, Mailer[1]);
13396974Seric 		(*CurEnv->e_puthdr)(f, Mailer[1], CurEnv);
13407123Seric 		fputs("\n", f);
13416974Seric 		(*CurEnv->e_putbody)(f, Mailer[1], FALSE);
13424214Seric 		fputs("\n", f);
13434214Seric 		(void) fclose(f);
13444214Seric 		(void) fflush(stdout);
13454417Seric 
13466887Seric 		/* reset ISUID & ISGID bits for paranoid systems */
13474621Seric 		(void) chmod(filename, (int) stb.st_mode);
13484214Seric 		exit(EX_OK);
13494315Seric 		/*NOTREACHED*/
13504214Seric 	}
13514214Seric 	else
13524214Seric 	{
13534214Seric 		/* parent -- wait for exit status */
13544214Seric 		register int i;
13554214Seric 		auto int stat;
13564214Seric 
13574214Seric 		while ((i = wait(&stat)) != pid)
13584214Seric 		{
13594214Seric 			if (i < 0)
13604214Seric 			{
13614214Seric 				stat = EX_OSERR << 8;
13624214Seric 				break;
13634214Seric 			}
13644214Seric 		}
13654215Seric 		if ((stat & 0377) != 0)
13664215Seric 			stat = EX_UNAVAILABLE << 8;
13674214Seric 		return ((stat >> 8) & 0377);
13684214Seric 	}
1369294Seric }
13704550Seric /*
13714550Seric **  SENDALL -- actually send all the messages.
13724550Seric **
13734550Seric **	Parameters:
13747043Seric **		e -- the envelope to send.
13754550Seric **		verifyonly -- if set, only give verification messages.
13764550Seric **
13774550Seric **	Returns:
13784550Seric **		none.
13794550Seric **
13804550Seric **	Side Effects:
13814550Seric **		Scans the send lists and sends everything it finds.
13827043Seric **		Delivers any appropriate error messages.
13834550Seric */
13844550Seric 
13857043Seric sendall(e, verifyonly)
13867043Seric 	ENVELOPE *e;
13874550Seric 	bool verifyonly;
13884550Seric {
13895008Seric 	register ADDRESS *q;
13907779Seric 	bool oldverbose;
13914550Seric 
13925032Seric # ifdef DEBUG
13937672Seric 	if (tTd(13, 2))
13945032Seric 	{
13956900Seric 		printf("\nSend Queue:\n");
13967043Seric 		printaddr(e->e_sendqueue, TRUE);
13975032Seric 	}
13985032Seric # endif DEBUG
13995008Seric 
14007043Seric 	/*
14017043Seric 	**  Run through the list and send everything.
14027043Seric 	*/
14037043Seric 
14047779Seric 	oldverbose = Verbose;
14057779Seric 	if (verifyonly)
14067779Seric 		Verbose = TRUE;
14077043Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14084550Seric 	{
14095008Seric 		if (verifyonly)
14104550Seric 		{
14116900Seric 			CurEnv->e_to = q->q_paddr;
14125008Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
14137052Seric 				message(Arpa_Info, "deliverable");
14144550Seric 		}
14155008Seric 		else
14166974Seric 			(void) deliver(q);
14174550Seric 	}
14187779Seric 	Verbose = oldverbose;
14197043Seric 
14207043Seric 	/*
14217043Seric 	**  Now run through and check for errors.
14227043Seric 	*/
14237043Seric 
14247043Seric 	if (verifyonly)
14257043Seric 		return;
14267043Seric 
14277043Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14287043Seric 	{
14297043Seric 		register ADDRESS *qq;
14307043Seric 
14317043Seric 		if (bitset(QQUEUEUP, q->q_flags))
14327043Seric 			e->e_queueup = TRUE;
14337043Seric 		if (!bitset(QBADADDR, q->q_flags))
14347043Seric 			continue;
14357043Seric 
14367043Seric 		/* we have an address that failed -- find the parent */
14377043Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
14387043Seric 		{
14397043Seric 			char obuf[MAXNAME + 6];
14407043Seric 			extern char *aliaslookup();
14417043Seric 
14427043Seric 			/* we can only have owners for local addresses */
14437043Seric 			if (!bitset(M_LOCAL, qq->q_mailer->m_flags))
14447043Seric 				continue;
14457043Seric 
14467043Seric 			/* see if the owner list exists */
14477043Seric 			(void) strcpy(obuf, "owner-");
14487047Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
14497047Seric 				(void) strcat(obuf, "owner");
14507047Seric 			else
14517047Seric 				(void) strcat(obuf, qq->q_user);
14527043Seric 			if (aliaslookup(obuf) == NULL)
14537043Seric 				continue;
14547043Seric 
14557043Seric 			/* owner list exists -- add it to the error queue */
14567043Seric 			qq->q_flags &= ~QPRIMARY;
14578077Seric 			sendto(obuf, qq, &e->e_errorqueue);
14587043Seric 			MailBack = TRUE;
14597043Seric 			break;
14607043Seric 		}
14617043Seric 
14627043Seric 		/* if we did not find an owner, send to the sender */
14637043Seric 		if (qq == NULL)
14648077Seric 			sendto(e->e_from.q_paddr, qq, &e->e_errorqueue);
14657043Seric 	}
14664550Seric }
14677043Seric /*
14687043Seric **  CHECKERRORS -- check a queue of addresses and process errors.
14697043Seric **
14707043Seric **	Parameters:
14717043Seric **		e -- the envelope to check.
14727043Seric **
14737043Seric **	Returns:
14747043Seric **		none.
14757043Seric **
14767043Seric **	Side Effects:
14777043Seric **		Arranges to queue all tempfailed messages in q
14787043Seric **			or deliver error responses.
14797043Seric */
14807043Seric 
14817043Seric checkerrors(e)
14827043Seric 	register ENVELOPE *e;
14837043Seric {
14847808Seric 	register ADDRESS *q;
14857808Seric 
14867043Seric # ifdef DEBUG
14877672Seric 	if (tTd(4, 1))
14887043Seric 	{
14897371Seric 		printf("\ncheckerrors: FatalErrors %d, errorqueue:\n");
14907043Seric 		printaddr(e->e_errorqueue, TRUE);
14917043Seric 	}
14927043Seric # endif DEBUG
14937043Seric 
14947043Seric 	/* mail back the transcript on errors */
14957043Seric 	if (FatalErrors)
14967043Seric 		savemail();
14977043Seric 
14987043Seric 	/* queue up anything laying around */
14997858Seric 	if (e->e_dontqueue)
15007858Seric 		return;
15017808Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15027043Seric 	{
15037808Seric 		if (bitset(QQUEUEUP, q->q_flags))
15047808Seric 		{
15057043Seric # ifdef QUEUE
15067808Seric 			queueup(e, FALSE);
15077043Seric # else QUEUE
15087808Seric 			syserr("checkerrors: trying to queue %s", e->e_df);
15097043Seric # endif QUEUE
15107808Seric 			break;
15117808Seric 		}
15127043Seric 	}
15137043Seric }
1514