1294Seric # include <signal.h>
24123Seric # include <errno.h>
34621Seric # include "sendmail.h"
44327Seric # include <sys/stat.h>
5294Seric # ifdef LOG
62774Seric # include <syslog.h>
7294Seric # endif LOG
8294Seric 
9*5936Seric SCCSID(@(#)deliver.c	3.68		02/22/82);
10405Seric 
11294Seric /*
124315Seric **  DELIVER -- Deliver a message to a list of addresses.
13294Seric **
144315Seric **	This routine delivers to everyone on the same host as the
154315Seric **	user on the head of the list.  It is clever about mailers
164315Seric **	that don't handle multiple users.  It is NOT guaranteed
174315Seric **	that it will deliver to all these addresses however -- so
184315Seric **	deliver should be called once for each address on the
194315Seric **	list.
204315Seric **
21294Seric **	Parameters:
224621Seric **		firstto -- head of the address list to deliver to.
23294Seric **		editfcn -- if non-NULL, we want to call this function
24294Seric **			to output the letter (instead of just out-
25294Seric **			putting it raw).
26294Seric **
27294Seric **	Returns:
28294Seric **		zero -- successfully delivered.
29294Seric **		else -- some failure, see ExitStat for more info.
30294Seric **
31294Seric **	Side Effects:
32294Seric **		The standard input is passed off to someone.
33294Seric */
34294Seric 
354621Seric deliver(firstto, editfcn)
364621Seric 	ADDRESS *firstto;
37294Seric 	int (*editfcn)();
38294Seric {
394452Seric 	char *host;			/* host being sent to */
404452Seric 	char *user;			/* user being sent to */
41294Seric 	char **pvp;
423233Seric 	register char **mvp;
433233Seric 	register char *p;
444452Seric 	register struct mailer *m;	/* mailer for this recipient */
45294Seric 	register int i;
462898Seric 	extern putmessage();
472968Seric 	extern bool checkcompat();
483233Seric 	char *pv[MAXPV+1];
494452Seric 	char tobuf[MAXLINE];		/* text line of to people */
503233Seric 	char buf[MAXNAME];
514397Seric 	ADDRESS *ctladdr;
524397Seric 	extern ADDRESS *getctladdr();
534452Seric 	char tfrombuf[MAXNAME];		/* translated from person */
544452Seric 	extern char **prescan();
554621Seric 	register ADDRESS *to = firstto;
564863Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
574863Seric 	bool tempfail = FALSE;
585032Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
59294Seric 
604488Seric 	errno = 0;
615008Seric 	if (!ForceMail && bitset(QDONTSEND, to->q_flags))
623233Seric 		return (0);
63294Seric 
64294Seric # ifdef DEBUG
65294Seric 	if (Debug)
663233Seric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
674596Seric 			to->q_mailer->m_mno, to->q_host, to->q_user);
68294Seric # endif DEBUG
69294Seric 
705903Seric 	m = to->q_mailer;
715903Seric 	host = to->q_host;
725903Seric 
73294Seric 	/*
745903Seric 	**  If this mailer is expensive, and if we don't want to make
755903Seric 	**  connections now, just mark these addresses and return.
765903Seric 	**	This is useful if we want to batch connections to
775903Seric 	**	reduce load.  This will cause the messages to be
785903Seric 	**	queued up, and a daemon will come along to send the
795903Seric 	**	messages later.
805903Seric 	**		This should be on a per-mailer basis.
815903Seric 	*/
825903Seric 
835903Seric 	if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags))
845903Seric 	{
855903Seric 		QueueUp = TRUE;
865903Seric 		for (; to != NULL; to = to->q_next)
875903Seric 			if (!bitset(QDONTSEND, to->q_flags))
885903Seric 				to->q_flags |= QQUEUEUP|QDONTSEND;
895903Seric 		return (0);
905903Seric 	}
915903Seric 
925903Seric 	/*
933233Seric 	**  Do initial argv setup.
943233Seric 	**	Insert the mailer name.  Notice that $x expansion is
953233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
963233Seric 	**	a picky -f flag, we insert it as appropriate.  This
973233Seric 	**	code does not check for 'pv' overflow; this places a
983233Seric 	**	manifest lower limit of 4 for MAXPV.
995903Seric 	**		We rewrite the from address here, being careful
1005903Seric 	**		to also rewrite it again using ruleset 2 to
1015903Seric 	**		eliminate redundancies.
1022968Seric 	*/
1032968Seric 
1044452Seric 	/* rewrite from address, using rewriting rules */
1054452Seric 	(void) expand(m->m_from, buf, &buf[sizeof buf - 1]);
1064452Seric 	mvp = prescan(buf, '\0');
1074452Seric 	if (mvp == NULL)
1084452Seric 	{
1094452Seric 		syserr("bad mailer from translate \"%s\"", buf);
1104452Seric 		return (EX_SOFTWARE);
1114452Seric 	}
1124452Seric 	rewrite(mvp, 2);
1134452Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
1144452Seric 
1154452Seric 	define('g', tfrombuf);		/* translated sender address */
1163233Seric 	define('h', host);		/* to host */
1173233Seric 	Errors = 0;
1183233Seric 	pvp = pv;
1193233Seric 	*pvp++ = m->m_argv[0];
1202968Seric 
1213233Seric 	/* insert -f or -r flag as appropriate */
1223233Seric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1233233Seric 	{
1243233Seric 		if (bitset(M_FOPT, m->m_flags))
1253233Seric 			*pvp++ = "-f";
1263233Seric 		else
1273233Seric 			*pvp++ = "-r";
1284082Seric 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
1293233Seric 		*pvp++ = newstr(buf);
1303233Seric 	}
131294Seric 
132294Seric 	/*
1333233Seric 	**  Append the other fixed parts of the argv.  These run
1343233Seric 	**  up to the first entry containing "$u".  There can only
1353233Seric 	**  be one of these, and there are only a few more slots
1363233Seric 	**  in the pv after it.
137294Seric 	*/
138294Seric 
1393233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
140294Seric 	{
1413233Seric 		while ((p = index(p, '$')) != NULL)
1423233Seric 			if (*++p == 'u')
1433233Seric 				break;
1443233Seric 		if (p != NULL)
1453233Seric 			break;
1463233Seric 
1473233Seric 		/* this entry is safe -- go ahead and process it */
1484082Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
1493233Seric 		*pvp++ = newstr(buf);
1503233Seric 		if (pvp >= &pv[MAXPV - 3])
1513233Seric 		{
1523233Seric 			syserr("Too many parameters to %s before $u", pv[0]);
1533233Seric 			return (-1);
1543233Seric 		}
155294Seric 	}
1564863Seric 
1573233Seric 	if (*mvp == NULL)
1584863Seric 	{
1594863Seric 		/* running SMTP */
1605179Seric # ifdef SMTP
1614863Seric 		clever = TRUE;
1624863Seric 		*pvp = NULL;
1634863Seric 		i = smtpinit(m, pv, (ADDRESS *) NULL);
1644863Seric 		giveresponse(i, TRUE, m);
1655179Seric # ifdef QUEUE
1664863Seric 		if (i == EX_TEMPFAIL)
1674863Seric 		{
1684863Seric 			QueueUp = TRUE;
1694863Seric 			tempfail = TRUE;
1704863Seric 		}
1715179Seric # endif QUEUE
1725179Seric # else SMTP
1735179Seric 		syserr("SMTP style mailer");
1745179Seric 		return (EX_SOFTWARE);
1755179Seric # endif SMTP
1764863Seric 	}
177294Seric 
178294Seric 	/*
1793233Seric 	**  At this point *mvp points to the argument with $u.  We
1803233Seric 	**  run through our address list and append all the addresses
1813233Seric 	**  we can.  If we run out of space, do not fret!  We can
1823233Seric 	**  always send another copy later.
183294Seric 	*/
184294Seric 
1853233Seric 	tobuf[0] = '\0';
1863233Seric 	To = tobuf;
1874397Seric 	ctladdr = NULL;
1883233Seric 	for (; to != NULL; to = to->q_next)
189294Seric 	{
1903233Seric 		/* avoid sending multiple recipients to dumb mailers */
1914382Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1923233Seric 			break;
1933233Seric 
1943233Seric 		/* if already sent or not for this host, don't send */
1955008Seric 		if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) ||
1965008Seric 		    strcmp(to->q_host, host) != 0 || to->q_mailer != firstto->q_mailer)
1973233Seric 			continue;
1984397Seric 
1995032Seric # ifdef DEBUG
2005032Seric 		if (Debug)
2015032Seric 		{
2025032Seric 			printf("\nsend to ");
2035032Seric 			printaddr(to, FALSE);
2045032Seric 		}
2055032Seric # endif DEBUG
2065032Seric 
2075032Seric 		/* link together the chain of recipients */
2085032Seric 		if (!bitset(QDONTSEND, to->q_flags))
2095032Seric 		{
2105032Seric 			to->q_tchain = tochain;
2115032Seric 			tochain = to;
2125032Seric 		}
2135032Seric 
2144397Seric 		/* compute effective uid/gid when sending */
2154596Seric 		if (to->q_mailer == ProgMailer)
2164397Seric 			ctladdr = getctladdr(to);
2174397Seric 
2183233Seric 		user = to->q_user;
2193233Seric 		To = to->q_paddr;
2203233Seric 		to->q_flags |= QDONTSEND;
2214863Seric 		if (tempfail)
2225032Seric 		{
2234863Seric 			to->q_flags |= QQUEUEUP;
2245032Seric 			continue;
2255032Seric 		}
2263233Seric 
2273233Seric 		/*
2283233Seric 		**  Check to see that these people are allowed to
2293233Seric 		**  talk to each other.
2303233Seric 		*/
2313233Seric 
2323233Seric 		if (!checkcompat(to))
233294Seric 		{
2343233Seric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
2353233Seric 			continue;
236294Seric 		}
2373233Seric 
2383233Seric 		/*
2394099Seric 		**  Strip quote bits from names if the mailer is dumb
2404099Seric 		**	about them.
2413233Seric 		*/
2423233Seric 
2433233Seric 		if (bitset(M_STRIPQ, m->m_flags))
244294Seric 		{
2454099Seric 			stripquotes(user, TRUE);
2464099Seric 			stripquotes(host, TRUE);
2473233Seric 		}
2484099Seric 		else
2494099Seric 		{
2504099Seric 			stripquotes(user, FALSE);
2514099Seric 			stripquotes(host, FALSE);
2524099Seric 		}
2533233Seric 
2543233Seric 		/*
2554161Seric 		**  If an error message has already been given, don't
2564161Seric 		**	bother to send to this address.
2574161Seric 		**
2584161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
2594161Seric 		**	>> NOTE >> cannot do any further aliasing; that
2604161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
2614161Seric 		*/
2624161Seric 
2634161Seric 		if (bitset(QBADADDR, to->q_flags))
2644161Seric 			continue;
2654161Seric 
2664283Seric 		/* save statistics.... */
2674596Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
2684596Seric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(MsgSize);
2694283Seric 
2704161Seric 		/*
2713233Seric 		**  See if this user name is "special".
2723233Seric 		**	If the user name has a slash in it, assume that this
2733233Seric 		**	is a file -- send it off without further ado.
2743233Seric 		**	Note that this means that editfcn's will not
2753233Seric 		**	be applied to the message.  Also note that
2763233Seric 		**	this type of addresses is not processed along
2773233Seric 		**	with the others, so we fudge on the To person.
2783233Seric 		*/
2793233Seric 
2804596Seric 		if (m == LocalMailer)
2813233Seric 		{
2825599Seric 			if (user[0] == '/')
283294Seric 			{
2844397Seric 				i = mailfile(user, getctladdr(to));
285294Seric 				giveresponse(i, TRUE, m);
2863233Seric 				continue;
287294Seric 			}
288294Seric 		}
2893233Seric 
2904315Seric 		/*
2914315Seric 		**  Address is verified -- add this user to mailer
2924315Seric 		**  argv, and add it to the print list of recipients.
2934315Seric 		*/
2944315Seric 
2953233Seric 		/* create list of users for error messages */
2963233Seric 		if (tobuf[0] != '\0')
2974082Seric 			(void) strcat(tobuf, ",");
2984082Seric 		(void) strcat(tobuf, to->q_paddr);
2993233Seric 		define('u', user);		/* to user */
3004078Seric 		define('z', to->q_home);	/* user's home */
3013233Seric 
3024863Seric 		/*
3034863Seric 		**  Expand out this user into argument list or
3044863Seric 		**  send it to our SMTP server.
3054863Seric 		*/
3064863Seric 
3074863Seric 		if (clever)
3083233Seric 		{
3095179Seric # ifdef SMTP
3104975Seric 			i = smtprcpt(to);
3115179Seric 			if (i != EX_OK)
3124863Seric 			{
3135179Seric # ifdef QUEUE
3145179Seric 				if (i == EX_TEMPFAIL)
3155179Seric 				{
3165179Seric 					QueueUp = TRUE;
3175179Seric 					to->q_flags |= QQUEUEUP;
3185179Seric 				}
3195179Seric 				else
3205179Seric # endif QUEUE
3215179Seric 				{
3225179Seric 					to->q_flags |= QBADADDR;
3235179Seric 					giveresponse(i, TRUE, m);
3245179Seric 				}
3254863Seric 			}
3265179Seric # else SMTP
3275179Seric 			syserr("trying to be clever");
3285179Seric # endif SMTP
3293233Seric 		}
3304863Seric 		else
3314863Seric 		{
3324863Seric 			(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
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)
3474863Seric 			smtpquit(pv[0]);
3485179Seric # endif SMTP
3494067Seric 		return (0);
3504863Seric 	}
3514067Seric 
3523233Seric 	/* print out messages as full list */
3533233Seric 	To = tobuf;
3543233Seric 
355294Seric 	/*
3563233Seric 	**  Fill out any parameters after the $u parameter.
357294Seric 	*/
358294Seric 
3594863Seric 	while (!clever && *++mvp != NULL)
360294Seric 	{
3614082Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
3623233Seric 		*pvp++ = newstr(buf);
3633233Seric 		if (pvp >= &pv[MAXPV])
3643233Seric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
365294Seric 	}
3663233Seric 	*pvp++ = NULL;
367294Seric 
368294Seric 	/*
369294Seric 	**  Call the mailer.
3702898Seric 	**	The argument vector gets built, pipes
371294Seric 	**	are created as necessary, and we fork & exec as
3722898Seric 	**	appropriate.
3734863Seric 	**	If we are running SMTP, we just need to clean up.
374294Seric 	*/
375294Seric 
3763233Seric 	if (editfcn == NULL)
3773233Seric 		editfcn = putmessage;
3784397Seric 	if (ctladdr == NULL)
3794397Seric 		ctladdr = &From;
3805179Seric # ifdef SMTP
3814863Seric 	if (clever)
3824863Seric 	{
3834863Seric 		i = smtpfinish(m, editfcn);
3844863Seric 		smtpquit(pv[0]);
3854863Seric 	}
3864863Seric 	else
3875179Seric # endif SMTP
3884863Seric 		i = sendoff(m, pv, editfcn, ctladdr);
3893233Seric 
3904621Seric 	/*
3914621Seric 	**  If we got a temporary failure, arrange to queue the
3924621Seric 	**  addressees.
3934621Seric 	*/
3944621Seric 
3955179Seric # ifdef QUEUE
3964621Seric 	if (i == EX_TEMPFAIL)
3974621Seric 	{
3984621Seric 		QueueUp = TRUE;
3995032Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
4004621Seric 			to->q_flags |= QQUEUEUP;
4014621Seric 	}
4025179Seric # endif QUEUE
4034621Seric 
4044488Seric 	errno = 0;
4053233Seric 	return (i);
4063233Seric }
4073233Seric /*
4084214Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4094214Seric **
4104214Seric **	This MUST be a macro, since after a vfork we are running
4114214Seric **	two processes on the same stack!!!
4124214Seric **
4134214Seric **	Parameters:
4144214Seric **		none.
4154214Seric **
4164214Seric **	Returns:
4174214Seric **		From a macro???  You've got to be kidding!
4184214Seric **
4194214Seric **	Side Effects:
4204214Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4214214Seric **			pid of child in parent, zero in child.
4224214Seric **			-1 on unrecoverable error.
4234214Seric **
4244214Seric **	Notes:
4254214Seric **		I'm awfully sorry this looks so awful.  That's
4264214Seric **		vfork for you.....
4274214Seric */
4284214Seric 
4294214Seric # define NFORKTRIES	5
4304214Seric # ifdef VFORK
4314214Seric # define XFORK	vfork
4324214Seric # else VFORK
4334214Seric # define XFORK	fork
4344214Seric # endif VFORK
4354214Seric 
4364214Seric # define DOFORK(fORKfN) \
4374214Seric {\
4384214Seric 	register int i;\
4394214Seric \
4404214Seric 	for (i = NFORKTRIES; i-- > 0; )\
4414214Seric 	{\
4424214Seric 		pid = fORKfN();\
4434214Seric 		if (pid >= 0)\
4444214Seric 			break;\
4454214Seric 		sleep((unsigned) NFORKTRIES - i);\
4464214Seric 	}\
4474214Seric }
4484214Seric /*
4494637Seric **  DOFORK -- simple fork interface to DOFORK.
4504637Seric **
4514637Seric **	Parameters:
4524637Seric **		none.
4534637Seric **
4544637Seric **	Returns:
4554637Seric **		pid of child in parent.
4564637Seric **		zero in child.
4574637Seric **		-1 on error.
4584637Seric **
4594637Seric **	Side Effects:
4604637Seric **		returns twice, once in parent and once in child.
4614637Seric */
4624637Seric 
4634637Seric dofork()
4644637Seric {
4654637Seric 	register int pid;
4664637Seric 
4674637Seric 	DOFORK(fork);
4684637Seric 	return (pid);
4694637Seric }
4704637Seric /*
4713233Seric **  SENDOFF -- send off call to mailer & collect response.
4723233Seric **
4733233Seric **	Parameters:
4743233Seric **		m -- mailer descriptor.
4753233Seric **		pvp -- parameter vector to send to it.
4763233Seric **		editfcn -- function to pipe it through.
4774397Seric **		ctladdr -- an address pointer controlling the
4784397Seric **			user/groupid etc. of the mailer.
4793233Seric **
4803233Seric **	Returns:
4813233Seric **		exit status of mailer.
4823233Seric **
4833233Seric **	Side Effects:
4843233Seric **		none.
4853233Seric */
4863233Seric 
4874397Seric sendoff(m, pvp, editfcn, ctladdr)
4883233Seric 	struct mailer *m;
4893233Seric 	char **pvp;
4903233Seric 	int (*editfcn)();
4914397Seric 	ADDRESS *ctladdr;
4923233Seric {
4934863Seric 	auto FILE *mfile;
4944863Seric 	auto FILE *rfile;
4953233Seric 	register int i;
4964863Seric 	extern putmessage();
4973233Seric 	int pid;
4984863Seric 
4994863Seric 	/*
5004863Seric 	**  Create connection to mailer.
5014863Seric 	*/
5024863Seric 
5034863Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
5044863Seric 	if (pid < 0)
5054863Seric 		return (-1);
5064863Seric 
5074863Seric 	/*
5084863Seric 	**  Format and send message.
5094863Seric 	*/
5104863Seric 
5114863Seric 	(void) signal(SIGPIPE, SIG_IGN);
5124863Seric 	if (editfcn == NULL)
5134863Seric 		editfcn = putmessage;
5144863Seric 
5154863Seric 	(*editfcn)(mfile, m, FALSE);
5164863Seric 	(void) fclose(mfile);
5174863Seric 
5184863Seric 	i = endmailer(pid, pvp[0]);
5194863Seric 	giveresponse(i, TRUE, m);
5204863Seric 	return (i);
5214863Seric }
5224863Seric /*
5234863Seric **  ENDMAILER -- Wait for mailer to terminate.
5244863Seric **
5254863Seric **	We should never get fatal errors (e.g., segmentation
5264863Seric **	violation), so we report those specially.  For other
5274863Seric **	errors, we choose a status message (into statmsg),
5284863Seric **	and if it represents an error, we print it.
5294863Seric **
5304863Seric **	Parameters:
5314863Seric **		pid -- pid of mailer.
5324863Seric **		name -- name of mailer (for error messages).
5334863Seric **
5344863Seric **	Returns:
5354863Seric **		exit code of mailer.
5364863Seric **
5374863Seric **	Side Effects:
5384863Seric **		none.
5394863Seric */
5404863Seric 
5414863Seric endmailer(pid, name)
5424863Seric 	int pid;
5434863Seric 	char *name;
5444863Seric {
5454863Seric 	register int i;
5464863Seric 	auto int st;
5474863Seric 
5484863Seric 	while ((i = wait(&st)) > 0 && i != pid)
5494863Seric 		continue;
5504863Seric 	if (i < 0)
5514863Seric 	{
5524863Seric 		syserr("wait");
5534863Seric 		return (-1);
5544863Seric 	}
5554863Seric 	if ((st & 0377) != 0)
5564863Seric 	{
5574863Seric 		syserr("%s: stat %o", name, st);
5584863Seric 		ExitStat = EX_UNAVAILABLE;
5594863Seric 		return (-1);
5604863Seric 	}
5614863Seric 	i = (st >> 8) & 0377;
5624863Seric 	return (i);
5634863Seric }
5644863Seric /*
5654863Seric **  OPENMAILER -- open connection to mailer.
5664863Seric **
5674863Seric **	Parameters:
5684863Seric **		m -- mailer descriptor.
5694863Seric **		pvp -- parameter vector to pass to mailer.
5704863Seric **		ctladdr -- controlling address for user.
5714863Seric **		clever -- create a full duplex connection.
5724863Seric **		pmfile -- pointer to mfile (to mailer) connection.
5734863Seric **		prfile -- pointer to rfile (from mailer) connection.
5744863Seric **
5754863Seric **	Returns:
5764863Seric **		pid of mailer.
5774863Seric **		-1 on error.
5784863Seric **
5794863Seric **	Side Effects:
5804863Seric **		creates a mailer in a subprocess.
5814863Seric */
5824863Seric 
5834863Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
5844863Seric 	struct mailer *m;
5854863Seric 	char **pvp;
5864863Seric 	ADDRESS *ctladdr;
5874863Seric 	bool clever;
5884863Seric 	FILE **pmfile;
5894863Seric 	FILE **prfile;
5904863Seric {
5914863Seric 	int pid;
5924709Seric 	int mpvect[2];
5934863Seric 	int rpvect[2];
5943233Seric 	FILE *mfile;
5954863Seric 	FILE *rfile;
5963233Seric 	extern FILE *fdopen();
5973233Seric 
5983233Seric # ifdef DEBUG
5993233Seric 	if (Debug)
600294Seric 	{
6014863Seric 		printf("openmailer:\n");
6023233Seric 		printav(pvp);
603294Seric 	}
6043233Seric # endif DEBUG
6054488Seric 	errno = 0;
6063233Seric 
6072898Seric 	/* create a pipe to shove the mail through */
6084709Seric 	if (pipe(mpvect) < 0)
609294Seric 	{
6104863Seric 		syserr("pipe (to mailer)");
611294Seric 		return (-1);
612294Seric 	}
6134863Seric 
6145179Seric # ifdef SMTP
6154863Seric 	/* if this mailer speaks smtp, create a return pipe */
6164863Seric 	if (clever && pipe(rpvect) < 0)
6174863Seric 	{
6184863Seric 		syserr("pipe (from mailer)");
6194863Seric 		(void) close(mpvect[0]);
6204863Seric 		(void) close(mpvect[1]);
6214863Seric 		return (-1);
6224863Seric 	}
6235179Seric # endif SMTP
6244863Seric 
6254214Seric 	DOFORK(XFORK);
6264327Seric 	/* pid is set by DOFORK */
627294Seric 	if (pid < 0)
628294Seric 	{
629294Seric 		syserr("Cannot fork");
6304709Seric 		(void) close(mpvect[0]);
6314709Seric 		(void) close(mpvect[1]);
6324863Seric 		if (clever)
6334863Seric 		{
6344863Seric 			(void) close(rpvect[0]);
6354863Seric 			(void) close(rpvect[1]);
6364863Seric 		}
637294Seric 		return (-1);
638294Seric 	}
639294Seric 	else if (pid == 0)
640294Seric 	{
641294Seric 		/* child -- set up input & exec mailer */
6421621Seric 		/* make diagnostic output be standard output */
6434477Seric 		(void) signal(SIGINT, SIG_IGN);
6444477Seric 		(void) signal(SIGHUP, SIG_IGN);
6454215Seric 		(void) signal(SIGTERM, SIG_DFL);
6464709Seric 
6474709Seric 		/* arrange to filter standard & diag output of command */
6484863Seric 		if (clever)
6494709Seric 		{
6504863Seric 			(void) close(rpvect[0]);
6514709Seric 			(void) close(1);
6524863Seric 			(void) dup(rpvect[1]);
6534863Seric 			(void) close(rpvect[1]);
6544863Seric 		}
6554863Seric 		else if (OutChannel != stdout)
6564863Seric 		{
6574863Seric 			(void) close(1);
6584709Seric 			(void) dup(fileno(OutChannel));
6594709Seric 		}
6604082Seric 		(void) close(2);
6614082Seric 		(void) dup(1);
6624709Seric 
6634709Seric 		/* arrange to get standard input */
6644709Seric 		(void) close(mpvect[1]);
6654082Seric 		(void) close(0);
6664709Seric 		if (dup(mpvect[0]) < 0)
667294Seric 		{
6682898Seric 			syserr("Cannot dup to zero!");
6692898Seric 			_exit(EX_OSERR);
670294Seric 		}
6714709Seric 		(void) close(mpvect[0]);
6722968Seric 		if (!bitset(M_RESTR, m->m_flags))
6734215Seric 		{
6744417Seric 			if (ctladdr->q_uid == 0)
6754417Seric 			{
6764417Seric 				(void) setgid(DefGid);
6774417Seric 				(void) setuid(DefUid);
6784417Seric 			}
6794417Seric 			else
6804415Seric 			{
6814417Seric 				(void) setgid(ctladdr->q_gid);
6824417Seric 				(void) setuid(ctladdr->q_uid);
6834415Seric 			}
6844215Seric 		}
6852774Seric # ifndef VFORK
6862774Seric 		/*
6872774Seric 		**  We have to be careful with vfork - we can't mung up the
6882774Seric 		**  memory but we don't want the mailer to inherit any extra
6892774Seric 		**  open files.  Chances are the mailer won't
6902774Seric 		**  care about an extra file, but then again you never know.
6912774Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
6922774Seric 		**  declared static so we can't.  But if we fclose(pwf), which
6932774Seric 		**  is what endpwent does, it closes it in the parent too and
6942774Seric 		**  the next getpwnam will be slower.  If you have a weird
6952774Seric 		**  mailer that chokes on the extra file you should do the
6962774Seric 		**  endpwent().
6972774Seric 		**
6982774Seric 		**  Similar comments apply to log.  However, openlog is
6992774Seric 		**  clever enough to set the FIOCLEX mode on the file,
7002774Seric 		**  so it will be closed automatically on the exec.
7012774Seric 		*/
7022774Seric 
7032774Seric 		endpwent();
704294Seric # ifdef LOG
7052089Seric 		closelog();
706294Seric # endif LOG
7072774Seric # endif VFORK
708294Seric 		execv(m->m_mailer, pvp);
709294Seric 		/* syserr fails because log is closed */
710294Seric 		/* syserr("Cannot exec %s", m->m_mailer); */
7114214Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
7124082Seric 		(void) fflush(stdout);
7131619Seric 		_exit(EX_UNAVAILABLE);
714294Seric 	}
715294Seric 
7164709Seric 	/*
7174863Seric 	**  Set up return value.
7184709Seric 	*/
7194709Seric 
7204709Seric 	(void) close(mpvect[0]);
7214709Seric 	mfile = fdopen(mpvect[1], "w");
7224863Seric 	if (clever)
7234863Seric 	{
7244863Seric 		(void) close(rpvect[1]);
7254863Seric 		rfile = fdopen(rpvect[0], "r");
7264863Seric 	}
727294Seric 
7284863Seric 	*pmfile = mfile;
7294863Seric 	*prfile = rfile;
730294Seric 
7314863Seric 	return (pid);
732294Seric }
733294Seric /*
734294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
735294Seric **
736294Seric **	Parameters:
737294Seric **		stat -- the status code from the mailer (high byte
738294Seric **			only; core dumps must have been taken care of
739294Seric **			already).
740294Seric **		force -- if set, force an error message output, even
741294Seric **			if the mailer seems to like to print its own
742294Seric **			messages.
743294Seric **		m -- the mailer descriptor for this mailer.
744294Seric **
745294Seric **	Returns:
7464082Seric **		none.
747294Seric **
748294Seric **	Side Effects:
7491518Seric **		Errors may be incremented.
750294Seric **		ExitStat may be set.
751294Seric */
752294Seric 
753294Seric giveresponse(stat, force, m)
754294Seric 	int stat;
755294Seric 	int force;
756294Seric 	register struct mailer *m;
757294Seric {
758294Seric 	register char *statmsg;
759294Seric 	extern char *SysExMsg[];
760294Seric 	register int i;
761294Seric 	extern int N_SysEx;
7621624Seric 	char buf[30];
763294Seric 
7644315Seric 	/*
7654315Seric 	**  Compute status message from code.
7664315Seric 	*/
7674315Seric 
768294Seric 	i = stat - EX__BASE;
769294Seric 	if (i < 0 || i > N_SysEx)
770294Seric 		statmsg = NULL;
771294Seric 	else
772294Seric 		statmsg = SysExMsg[i];
773294Seric 	if (stat == 0)
7744065Seric 	{
7754194Seric 		if (bitset(M_LOCAL, m->m_flags))
7764161Seric 			statmsg = "delivered";
7774161Seric 		else
7784161Seric 			statmsg = "queued";
7794065Seric 		if (Verbose)
7804166Seric 			message(Arpa_Info, statmsg);
7814065Seric 	}
7825179Seric # ifdef QUEUE
7834621Seric 	else if (stat == EX_TEMPFAIL)
7844621Seric 	{
7854621Seric 		if (Verbose)
7864621Seric 			message(Arpa_Info, "transmission deferred");
7874621Seric 	}
7885179Seric # endif QUEUE
789294Seric 	else
790294Seric 	{
7911518Seric 		Errors++;
792294Seric 		if (statmsg == NULL && m->m_badstat != 0)
793294Seric 		{
794294Seric 			stat = m->m_badstat;
795294Seric 			i = stat - EX__BASE;
796294Seric # ifdef DEBUG
797294Seric 			if (i < 0 || i >= N_SysEx)
798294Seric 				syserr("Bad m_badstat %d", stat);
799294Seric 			else
800294Seric # endif DEBUG
801294Seric 			statmsg = SysExMsg[i];
802294Seric 		}
803294Seric 		if (statmsg == NULL)
804294Seric 			usrerr("unknown mailer response %d", stat);
8054065Seric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
806294Seric 			usrerr("%s", statmsg);
807294Seric 	}
808294Seric 
809294Seric 	/*
810294Seric 	**  Final cleanup.
811294Seric 	**	Log a record of the transaction.  Compute the new
812294Seric 	**	ExitStat -- if we already had an error, stick with
813294Seric 	**	that.
814294Seric 	*/
815294Seric 
8161624Seric 	if (statmsg == NULL)
8171624Seric 	{
8184082Seric 		(void) sprintf(buf, "error %d", stat);
8191624Seric 		statmsg = buf;
8201624Seric 	}
8211624Seric 
822294Seric # ifdef LOG
8232774Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
824294Seric # endif LOG
8255179Seric # ifdef QUEUE
8264621Seric 	if (stat != EX_TEMPFAIL)
8275179Seric # endif QUEUE
8284621Seric 		setstat(stat);
829294Seric }
830294Seric /*
8312898Seric **  PUTMESSAGE -- output a message to the final mailer.
832294Seric **
8332898Seric **	This routine takes care of recreating the header from the
8342898Seric **	in-core copy, etc.
835294Seric **
836294Seric **	Parameters:
8372898Seric **		fp -- file to output onto.
8382898Seric **		m -- a mailer descriptor.
8394863Seric **		xdot -- if set, hide lines beginning with dot.
840294Seric **
841294Seric **	Returns:
8422898Seric **		none.
843294Seric **
844294Seric **	Side Effects:
8452898Seric **		The message is written onto fp.
846294Seric */
847294Seric 
8484863Seric putmessage(fp, m, xdot)
8492898Seric 	FILE *fp;
8502898Seric 	struct mailer *m;
8514863Seric 	bool xdot;
852294Seric {
8532898Seric 	char buf[BUFSIZ];
8544315Seric 	register HDR *h;
8552898Seric 	extern char *arpadate();
8562898Seric 	bool anyheader = FALSE;
8573044Seric 	extern char *capitalize();
8584370Seric 	extern char *hvalue();
8594370Seric 	extern bool samefrom();
8604447Seric 	char *of_line;
861294Seric 
8624315Seric 	/*
8634315Seric 	**  Output "From" line unless supressed
8645099Seric 	**
8655099Seric 	**  >>>>>>>>>>	One of the ugliest hacks seen by human eyes is
8665099Seric 	**  >>>>>>>>>>	contained herein: UUCP wants those stupid
8675330Seric 	**  >>>>>>>>>>	"remote from <host>" lines.  Why oh why does a
8685330Seric 	**  >> NOTE >>	well-meaning programmer such as myself have to
8695099Seric 	**  >>>>>>>>>>	deal with this kind of antique garbage????
8705330Seric 	**  >>>>>>>>>>  This even depends on the local UUCP host name
8715330Seric 	**  >>>>>>>>>>  being in the $U macro!!!!
8724315Seric 	*/
8734315Seric 
8743186Seric 	if (!bitset(M_NHDR, m->m_flags))
8754205Seric 	{
8765179Seric # ifdef UGLYUUCP
8775599Seric 		if (bitset(M_UGLYUUCP, m->m_flags))
8785330Seric 			(void) expand("From $f  $d remote from $U", buf,
8795099Seric 					&buf[sizeof buf - 1]);
8805099Seric 		else
8815179Seric # endif UGLYUUCP
8825099Seric 			(void) expand("$l", buf, &buf[sizeof buf - 1]);
8834205Seric 		fprintf(fp, "%s\n", buf);
8844205Seric 	}
8853186Seric 
8864315Seric 	/*
8874315Seric 	**  Output all header lines
8884315Seric 	*/
8894315Seric 
8904447Seric 	of_line = hvalue("original-from");
8912898Seric 	for (h = Header; h != NULL; h = h->h_link)
8921828Seric 	{
8934315Seric 		register char *p;
8944370Seric 		char *origfrom = OrigFrom;
8954447Seric 		bool nooutput;
8964315Seric 
8974447Seric 		nooutput = FALSE;
8983389Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
8994447Seric 			nooutput = TRUE;
9004447Seric 
9014447Seric 		/* use From: line from message if generated is the same */
9024370Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
9034447Seric 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
9043385Seric 		{
9054370Seric 			p = origfrom;
9064370Seric 			origfrom = NULL;
9074370Seric 		}
9084370Seric 		else if (bitset(H_DEFAULT, h->h_flags))
9094370Seric 		{
9104082Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
9113385Seric 			p = buf;
9123385Seric 		}
9135913Seric 		else if (bitset(H_ADDR, h->h_flags))
9145913Seric 		{
9155913Seric 			register int opos;
9165913Seric 			bool firstone = TRUE;
9175913Seric 
9185913Seric 			/*
9195913Seric 			**  Output the address list translated by the
9205913Seric 			**  mailer and with commas.
9215913Seric 			*/
9225913Seric 
9235913Seric 			p = h->h_value;
9245913Seric 			if (p == NULL || *p == '\0' || nooutput)
9255913Seric 				continue;
9265913Seric 			fprintf(fp, "%s: ", capitalize(h->h_field));
9275913Seric 			opos = strlen(h->h_field) + 2;
9285913Seric 			while (*p != '\0')
9295913Seric 			{
9305913Seric 				register char *name = p;
9315913Seric 				extern char *remotename();
9325913Seric 				char savechar;
9335913Seric 
9345913Seric 				/* find the end of the name */
935*5936Seric 				while (*p != '\0' && *p != ',')
936*5936Seric 				{
937*5936Seric 					extern bool isatword();
938*5936Seric 					char *oldp;
939*5936Seric 
940*5936Seric 					if (!OldStyle || !isspace(*p))
941*5936Seric 					{
942*5936Seric 						p++;
943*5936Seric 						continue;
944*5936Seric 					}
945*5936Seric 					oldp = p;
946*5936Seric 					while (*p != '\0' && isspace(*p))
947*5936Seric 						p++;
948*5936Seric 					if (*p != '@' && !isatword(p))
949*5936Seric 					{
950*5936Seric 						p = oldp;
951*5936Seric 						break;
952*5936Seric 					}
953*5936Seric 					p += *p == '@' ? 1 : 2;
954*5936Seric 					while (*p != '\0' && isspace(*p))
955*5936Seric 						p++;
956*5936Seric 				}
9575913Seric 				savechar = *p;
9585913Seric 				*p = '\0';
9595913Seric 
9605913Seric 				/* translate the name to be relative */
9615916Seric 				name = remotename(name, m);
9625913Seric 				if (*name == '\0')
9635913Seric 					continue;
9645913Seric 
9655913Seric 				/* output the name with nice formatting */
9665913Seric 				opos += strlen(name);
9675913Seric 				if (!firstone)
9685913Seric 					opos += 2;
9695913Seric 				if (opos > 78 && !firstone)
9705913Seric 				{
9715913Seric 					fprintf(fp, ",\n        ");
9725916Seric 					opos = 8 + strlen(name);
9735913Seric 				}
9745913Seric 				else if (!firstone)
9755913Seric 					fprintf(fp, ", ");
9765913Seric 				fprintf(fp, "%s", name);
9775913Seric 				firstone = FALSE;
9785913Seric 
9795913Seric 				/* clean up the source string */
9805913Seric 				*p = savechar;
9815913Seric 				while (*p != '\0' && (isspace(*p) || *p == ','))
9825913Seric 					p++;
9835913Seric 			}
9845913Seric 			fprintf(fp, "\n");
9855913Seric 			nooutput = TRUE;
9865913Seric 		}
9872898Seric 		else
9883385Seric 			p = h->h_value;
9894447Seric 		if (p == NULL || *p == '\0')
9903389Seric 			continue;
9914209Seric 
9924209Seric 		/* hack, hack -- output Original-From field if different */
9934447Seric 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
9944209Seric 		{
9954447Seric 			/* output new Original-From line if needed */
9964447Seric 			if (of_line == NULL && !samefrom(p, origfrom))
9974447Seric 			{
9984447Seric 				fprintf(fp, "Original-From: %s\n", origfrom);
9994447Seric 				anyheader = TRUE;
10004447Seric 			}
10014447Seric 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
10024447Seric 			{
10034447Seric 				/* delete Original-From: line if redundant */
10044447Seric 				p = of_line;
10054447Seric 				of_line = NULL;
10064447Seric 			}
10074447Seric 		}
10084447Seric 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
10094447Seric 			nooutput = TRUE;
10104447Seric 
10114447Seric 		/* finally, output the header line */
10124447Seric 		if (!nooutput)
10134447Seric 		{
10144447Seric 			fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
10154447Seric 			h->h_flags |= H_USED;
10164370Seric 			anyheader = TRUE;
10174209Seric 		}
10182898Seric 	}
10192898Seric 	if (anyheader)
10202898Seric 		fprintf(fp, "\n");
10212898Seric 
10224315Seric 	/*
10234315Seric 	**  Output the body of the message
10244315Seric 	*/
10254315Seric 
10264452Seric 	if (TempFile != NULL)
10274452Seric 	{
10284452Seric 		rewind(TempFile);
10294863Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, TempFile) != NULL)
10304863Seric 			fprintf(fp, "%s%s", xdot && buf[0] == '.' ? "." : "", buf);
10312898Seric 
10324452Seric 		if (ferror(TempFile))
10334452Seric 		{
10344452Seric 			syserr("putmessage: read error");
10354452Seric 			setstat(EX_IOERR);
10364452Seric 		}
10374452Seric 	}
10384452Seric 
10394621Seric 	(void) fflush(fp);
10404123Seric 	if (ferror(fp) && errno != EPIPE)
1041294Seric 	{
10422898Seric 		syserr("putmessage: write error");
1043294Seric 		setstat(EX_IOERR);
1044294Seric 	}
10454123Seric 	errno = 0;
1046294Seric }
1047294Seric /*
1048*5936Seric **  ISATWORD -- tell if the word we are pointing to is "at".
1049*5936Seric **
1050*5936Seric **	Parameters:
1051*5936Seric **		p -- word to check.
1052*5936Seric **
1053*5936Seric **	Returns:
1054*5936Seric **		TRUE -- if p is the word at.
1055*5936Seric **		FALSE -- otherwise.
1056*5936Seric **
1057*5936Seric **	Side Effects:
1058*5936Seric **		none.
1059*5936Seric */
1060*5936Seric 
1061*5936Seric bool
1062*5936Seric isatword(p)
1063*5936Seric 	register char *p;
1064*5936Seric {
1065*5936Seric 	extern char lower();
1066*5936Seric 
1067*5936Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
1068*5936Seric 	    p[2] != '\0' && isspace(p[2]))
1069*5936Seric 		return (TRUE);
1070*5936Seric 	return (FALSE);
1071*5936Seric }
1072*5936Seric /*
10735913Seric **  REMOTENAME -- return the name relative to the current mailer
10745913Seric **
10755913Seric **	Parameters:
10765913Seric **		name -- the name to translate.
10775913Seric **
10785913Seric **	Returns:
10795913Seric **		the text string representing this address relative to
10805913Seric **			the receiving mailer.
10815913Seric **
10825913Seric **	Side Effects:
10835913Seric **		none.
10845913Seric **
10855913Seric **	Warnings:
10865913Seric **		The text string returned is tucked away locally;
10875913Seric **			copy it if you intend to save it.
10885913Seric */
10895913Seric 
10905913Seric char *
10915916Seric remotename(name, m)
10925913Seric 	char *name;
10935916Seric 	struct mailer *m;
10945913Seric {
10955913Seric 	static char buf[MAXNAME];
10965913Seric 	char lbuf[MAXNAME];
10975913Seric 	extern char *macvalue();
10985913Seric 	char *oldf = macvalue('f');
10995916Seric 	char *oldx = macvalue('x');
11005916Seric 	char *oldg = macvalue('g');
11015913Seric 	extern char **prescan();
11025913Seric 	register char **pvp;
11035916Seric 	extern char *getxpart();
11045913Seric 
11055913Seric 	/*
11065913Seric 	**  Do general rewriting of name.
11075913Seric 	**	This will also take care of doing global name translation.
11085913Seric 	*/
11095913Seric 
11105916Seric 	define('x', getxpart(name));
11115913Seric 	pvp = prescan(name, '\0');
11125913Seric 	for (;;)
11135913Seric 	{
11145913Seric 		rewrite(pvp, 1);
11155913Seric 		rewrite(pvp, 3);
11165913Seric 		if (**pvp == CANONNET)
11175913Seric 		{
11185913Seric 			auto ADDRESS a;
11195913Seric 			register char *p;
11205913Seric 			extern char *hostalias();
11215913Seric 
11225913Seric 			/* oops... resolved to something */
11235913Seric 			if (buildaddr(pvp, &a) == NULL)
11245913Seric 				return (name);
11255913Seric 			p = hostalias(&a);
11265913Seric 			if (p == NULL)
11275913Seric 				return (name);
11285913Seric 			pvp = prescan(p, '\0');
11295913Seric 		}
11305913Seric 		else
11315913Seric 		{
11325913Seric 			cataddr(pvp, lbuf, sizeof lbuf);
11335913Seric 			break;
11345913Seric 		}
11355913Seric 	}
11365913Seric 
11375913Seric 	/* make the name relative to the receiving mailer */
11385913Seric 	define('f', lbuf);
11395916Seric 	(void) expand(m->m_from, buf, &buf[sizeof buf - 1]);
11405913Seric 
11415913Seric 	/* rewrite to get rid of garbage we added in the expand above */
11425913Seric 	pvp = prescan(buf, '\0');
11435913Seric 	rewrite(pvp, 2);
11445916Seric 	cataddr(pvp, lbuf, sizeof lbuf);
11455913Seric 
11465916Seric 	/* now add any comment info we had before back */
11475916Seric 	define('g', lbuf);
11485916Seric 	(void) expand("$q", buf, &buf[sizeof buf - 1]);
11495916Seric 
11505913Seric 	define('f', oldf);
11515916Seric 	define('g', oldg);
11525916Seric 	define('x', oldx);
11535913Seric 
11545913Seric # ifdef DEBUG
11555913Seric 	if (Debug > 0)
11565913Seric 		printf("remotename(%s) => `%s'\n", name, buf);
11575913Seric # endif DEBUG
11585913Seric 	return (buf);
11595913Seric }
11605913Seric /*
11614370Seric **  SAMEFROM -- tell if two text addresses represent the same from address.
11624370Seric **
11634370Seric **	Parameters:
11644370Seric **		ifrom -- internally generated form of from address.
11654370Seric **		efrom -- external form of from address.
11664370Seric **
11674370Seric **	Returns:
11684370Seric **		TRUE -- if they convey the same info.
11694370Seric **		FALSE -- if any information has been lost.
11704370Seric **
11714370Seric **	Side Effects:
11724370Seric **		none.
11734370Seric */
11744370Seric 
11754370Seric bool
11764370Seric samefrom(ifrom, efrom)
11774370Seric 	char *ifrom;
11784370Seric 	char *efrom;
11794370Seric {
11804447Seric 	register char *p;
11814447Seric 	char buf[MAXNAME + 4];
11824447Seric 
11834447Seric # ifdef DEBUG
11844447Seric 	if (Debug > 7)
11854447Seric 		printf("samefrom(%s,%s)-->", ifrom, efrom);
11864447Seric # endif DEBUG
11874447Seric 	if (strcmp(ifrom, efrom) == 0)
11884447Seric 		goto success;
11894447Seric 	p = index(ifrom, '@');
11904447Seric 	if (p == NULL)
11914447Seric 		goto failure;
11924447Seric 	*p = '\0';
11934447Seric 	strcpy(buf, ifrom);
11944447Seric 	strcat(buf, " at ");
11954447Seric 	*p++ = '@';
11964447Seric 	strcat(buf, p);
11974447Seric 	if (strcmp(buf, efrom) == 0)
11984447Seric 		goto success;
11994447Seric 
12004447Seric   failure:
12014447Seric # ifdef DEBUG
12024447Seric 	if (Debug > 7)
12034447Seric 		printf("FALSE\n");
12044447Seric # endif DEBUG
12054447Seric 	return (FALSE);
12064447Seric 
12074447Seric   success:
12084447Seric # ifdef DEBUG
12094447Seric 	if (Debug > 7)
12104447Seric 		printf("TRUE\n");
12114447Seric # endif DEBUG
12124447Seric 	return (TRUE);
12134370Seric }
12144370Seric /*
1215294Seric **  MAILFILE -- Send a message to a file.
1216294Seric **
12174327Seric **	If the file has the setuid/setgid bits set, but NO execute
12184327Seric **	bits, sendmail will try to become the owner of that file
12194327Seric **	rather than the real user.  Obviously, this only works if
12204327Seric **	sendmail runs as root.
12214327Seric **
1222294Seric **	Parameters:
1223294Seric **		filename -- the name of the file to send to.
12244397Seric **		ctladdr -- the controlling address header -- includes
12254397Seric **			the userid/groupid to be when sending.
1226294Seric **
1227294Seric **	Returns:
1228294Seric **		The exit code associated with the operation.
1229294Seric **
1230294Seric **	Side Effects:
1231294Seric **		none.
1232294Seric */
1233294Seric 
12344397Seric mailfile(filename, ctladdr)
1235294Seric 	char *filename;
12364397Seric 	ADDRESS *ctladdr;
1237294Seric {
1238294Seric 	register FILE *f;
12394214Seric 	register int pid;
1240294Seric 
12414214Seric 	/*
12424214Seric 	**  Fork so we can change permissions here.
12434214Seric 	**	Note that we MUST use fork, not vfork, because of
12444214Seric 	**	the complications of calling subroutines, etc.
12454214Seric 	*/
12464067Seric 
12474214Seric 	DOFORK(fork);
12484214Seric 
12494214Seric 	if (pid < 0)
12504214Seric 		return (EX_OSERR);
12514214Seric 	else if (pid == 0)
12524214Seric 	{
12534214Seric 		/* child -- actually write to file */
12544327Seric 		struct stat stb;
12554327Seric 
12564215Seric 		(void) signal(SIGINT, SIG_DFL);
12574215Seric 		(void) signal(SIGHUP, SIG_DFL);
12584215Seric 		(void) signal(SIGTERM, SIG_DFL);
12594327Seric 		umask(OldUmask);
12604327Seric 		if (stat(filename, &stb) < 0)
12614431Seric 			stb.st_mode = 0666;
12624327Seric 		if (bitset(0111, stb.st_mode))
12634327Seric 			exit(EX_CANTCREAT);
12644401Seric 		if (ctladdr == NULL)
12654401Seric 			ctladdr = &From;
12664327Seric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
12674417Seric 		{
12684417Seric 			if (ctladdr->q_uid == 0)
12694417Seric 				(void) setgid(DefGid);
12704417Seric 			else
12714417Seric 				(void) setgid(ctladdr->q_gid);
12724417Seric 		}
12734327Seric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
12744417Seric 		{
12754417Seric 			if (ctladdr->q_uid == 0)
12764417Seric 				(void) setuid(DefUid);
12774417Seric 			else
12784417Seric 				(void) setuid(ctladdr->q_uid);
12794417Seric 		}
12804214Seric 		f = fopen(filename, "a");
12814214Seric 		if (f == NULL)
12824214Seric 			exit(EX_CANTCREAT);
12834214Seric 
12844863Seric 		putmessage(f, Mailer[1], FALSE);
12854214Seric 		fputs("\n", f);
12864214Seric 		(void) fclose(f);
12874214Seric 		(void) fflush(stdout);
12884417Seric 
12894417Seric 		/* reset ISUID & ISGID bits */
12904621Seric 		(void) chmod(filename, (int) stb.st_mode);
12914214Seric 		exit(EX_OK);
12924315Seric 		/*NOTREACHED*/
12934214Seric 	}
12944214Seric 	else
12954214Seric 	{
12964214Seric 		/* parent -- wait for exit status */
12974214Seric 		register int i;
12984214Seric 		auto int stat;
12994214Seric 
13004214Seric 		while ((i = wait(&stat)) != pid)
13014214Seric 		{
13024214Seric 			if (i < 0)
13034214Seric 			{
13044214Seric 				stat = EX_OSERR << 8;
13054214Seric 				break;
13064214Seric 			}
13074214Seric 		}
13084215Seric 		if ((stat & 0377) != 0)
13094215Seric 			stat = EX_UNAVAILABLE << 8;
13104214Seric 		return ((stat >> 8) & 0377);
13114214Seric 	}
1312294Seric }
13134550Seric /*
13144550Seric **  SENDALL -- actually send all the messages.
13154550Seric **
13164550Seric **	Parameters:
13174550Seric **		verifyonly -- if set, only give verification messages.
13184550Seric **
13194550Seric **	Returns:
13204550Seric **		none.
13214550Seric **
13224550Seric **	Side Effects:
13234550Seric **		Scans the send lists and sends everything it finds.
13244550Seric */
13254550Seric 
13264550Seric sendall(verifyonly)
13274550Seric 	bool verifyonly;
13284550Seric {
13295008Seric 	register ADDRESS *q;
13304550Seric 	typedef int (*fnptr)();
13314550Seric 
13325032Seric # ifdef DEBUG
13335032Seric 	if (Debug > 1)
13345032Seric 	{
13355032Seric 		printf("\nSendQueue:\n");
13365032Seric 		printaddr(SendQueue, TRUE);
13375032Seric 	}
13385032Seric # endif DEBUG
13395008Seric 
13405008Seric 	for (q = SendQueue; q != NULL; q = q->q_next)
13414550Seric 	{
13425008Seric 		if (verifyonly)
13434550Seric 		{
13445008Seric 			To = q->q_paddr;
13455008Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
13464550Seric 			{
13475008Seric 				if (bitset(M_LOCAL, q->q_mailer->m_flags))
13485008Seric 					message(Arpa_Info, "deliverable");
13495008Seric 				else
13505008Seric 					message(Arpa_Info, "queueable");
13514550Seric 			}
13524550Seric 		}
13535008Seric 		else
13545008Seric 			(void) deliver(q, (fnptr) NULL);
13554550Seric 	}
13564550Seric }
1357