Sendmail source for the 3B1/7300 (part 8/8)

David H. Brierley dave at galaxia.Newport.RI.US
Sat Feb 25 12:30:03 AEST 1989


----- cut here and feed to /bin/sh -----
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 8 (of 8)."
# Contents:  src/deliver.c
# Wrapped by dave at galaxia on Fri Feb 24 20:24:22 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'src/deliver.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'src/deliver.c'\"
else
echo shar: Extracting \"'src/deliver.c'\" \(31247 characters\)
sed "s/^X//" >'src/deliver.c' <<'END_OF_FILE'
X/*
X**  Sendmail
X**  Copyright (c) 1983  Eric P. Allman
X**  Berkeley, California
X**
X**  Copyright (c) 1983 Regents of the University of California.
X**  All rights reserved.  The Berkeley software License Agreement
X**  specifies the terms and conditions for redistribution.
X*/
X
X#ifndef lint
Xstatic char	SccsId[] = "@(#)deliver.c	5.15 (Berkeley) 12/12/87";
X#endif not lint
X
X# include <signal.h>
X# include <errno.h>
X# include "sendmail.h"
X# include <sys/stat.h>
X
X#ifdef	SMTP
X# include <netdb.h>
X#endif
X
X/*
X**  DELIVER -- Deliver a message to a list of addresses.
X**
X**	This routine delivers to everyone on the same host as the
X**	user on the head of the list.  It is clever about mailers
X**	that don't handle multiple users.  It is NOT guaranteed
X**	that it will deliver to all these addresses however -- so
X**	deliver should be called once for each address on the
X**	list.
X**
X**	Parameters:
X**		e -- the envelope to deliver.
X**		firstto -- head of the address list to deliver to.
X**
X**	Returns:
X**		zero -- successfully delivered.
X**		else -- some failure, see ExitStat for more info.
X**
X**	Side Effects:
X**		The standard input is passed off to someone.
X*/
X
Xdeliver(e, firstto)
X	register ENVELOPE *e;
X	ADDRESS *firstto;
X{
X	char *host;			/* host being sent to */
X	char *user;			/* user being sent to */
X	char **pvp;
X	register char **mvp;
X	register char *p;
X	register MAILER *m;		/* mailer for this recipient */
X	ADDRESS *ctladdr;
X	register ADDRESS *to = firstto;
X	bool clever = FALSE;		/* running user smtp to this mailer */
X	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
X	register int rcode;		/* response code */
X	char *pv[MAXPV+1];
X	char tobuf[MAXLINE-50];		/* text line of to people */
X	char buf[MAXNAME];
X	char tfrombuf[MAXNAME];		/* translated from person */
X	extern bool checkcompat();
X	extern ADDRESS *getctladdr();
X	extern char *remotename();
X
X	errno = 0;
X	if (bitset(QDONTSEND, to->q_flags))
X		return (0);
X
X	m = to->q_mailer;
X	host = to->q_host;
X
X# ifdef DEBUG
X	if (tTd(10, 1))
X		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
X			m->m_mno, host, to->q_user);
X# endif DEBUG
X
X	/*
X	**  If this mailer is expensive, and if we don't want to make
X	**  connections now, just mark these addresses and return.
X	**	This is useful if we want to batch connections to
X	**	reduce load.  This will cause the messages to be
X	**	queued up, and a daemon will come along to send the
X	**	messages later.
X	**		This should be on a per-mailer basis.
X	*/
X
X	if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) &&
X	    !Verbose)
X	{
X		for (; to != NULL; to = to->q_next)
X		{
X			if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m)
X				continue;
X			to->q_flags |= QQUEUEUP|QDONTSEND;
X			e->e_to = to->q_paddr;
X			message(Arpa_Info, "queued");
X			if (LogLevel > 4)
X				logdelivery("queued");
X		}
X		e->e_to = NULL;
X		return (0);
X	}
X
X	/*
X	**  Do initial argv setup.
X	**	Insert the mailer name.  Notice that $x expansion is
X	**	NOT done on the mailer name.  Then, if the mailer has
X	**	a picky -f flag, we insert it as appropriate.  This
X	**	code does not check for 'pv' overflow; this places a
X	**	manifest lower limit of 4 for MAXPV.
X	**		The from address rewrite is expected to make
X	**		the address relative to the other end.
X	*/
X
X	/* rewrite from address, using rewriting rules */
X	expand("\001f", buf, &buf[sizeof buf - 1], e);
X	(void) strcpy(tfrombuf, remotename(buf, m, TRUE, TRUE));
X
X	define('g', tfrombuf, e);		/* translated sender address */
X	define('h', host, e);			/* to host */
X	Errors = 0;
X	pvp = pv;
X	*pvp++ = m->m_argv[0];
X
X	/* insert -f or -r flag as appropriate */
X	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
X	{
X		if (bitnset(M_FOPT, m->m_flags))
X			*pvp++ = "-f";
X		else
X			*pvp++ = "-r";
X		expand("\001g", buf, &buf[sizeof buf - 1], e);
X		*pvp++ = newstr(buf);
X	}
X
X	/*
X	**  Append the other fixed parts of the argv.  These run
X	**  up to the first entry containing "$u".  There can only
X	**  be one of these, and there are only a few more slots
X	**  in the pv after it.
X	*/
X
X	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
X	{
X		while ((p = index(p, '\001')) != NULL)
X			if (*++p == 'u')
X				break;
X		if (p != NULL)
X			break;
X
X		/* this entry is safe -- go ahead and process it */
X		expand(*mvp, buf, &buf[sizeof buf - 1], e);
X		*pvp++ = newstr(buf);
X		if (pvp >= &pv[MAXPV - 3])
X		{
X			syserr("Too many parameters to %s before $u", pv[0]);
X			return (-1);
X		}
X	}
X
X	/*
X	**  If we have no substitution for the user name in the argument
X	**  list, we know that we must supply the names otherwise -- and
X	**  SMTP is the answer!!
X	*/
X
X	if (*mvp == NULL)
X	{
X		/* running SMTP */
X# ifdef SMTP
X		clever = TRUE;
X		*pvp = NULL;
X# else SMTP
X		/* oops!  we don't implement SMTP */
X		syserr("SMTP style mailer");
X		return (EX_SOFTWARE);
X# endif SMTP
X	}
X
X	/*
X	**  At this point *mvp points to the argument with $u.  We
X	**  run through our address list and append all the addresses
X	**  we can.  If we run out of space, do not fret!  We can
X	**  always send another copy later.
X	*/
X
X	tobuf[0] = '\0';
X	e->e_to = tobuf;
X	ctladdr = NULL;
X	for (; to != NULL; to = to->q_next)
X	{
X		/* avoid sending multiple recipients to dumb mailers */
X		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
X			break;
X
X		/* if already sent or not for this host, don't send */
X		if (bitset(QDONTSEND, to->q_flags) ||
X		    strcmp(to->q_host, host) != 0 ||
X		    to->q_mailer != firstto->q_mailer)
X			continue;
X
X		/* avoid overflowing tobuf */
X		if (sizeof tobuf - (strlen(to->q_paddr) + strlen(tobuf) + 2) < 0)
X			break;
X
X# ifdef DEBUG
X		if (tTd(10, 1))
X		{
X			printf("\nsend to ");
X			printaddr(to, FALSE);
X		}
X# endif DEBUG
X
X		/* compute effective uid/gid when sending */
X		if (to->q_mailer == ProgMailer)
X			ctladdr = getctladdr(to);
X
X		user = to->q_user;
X		e->e_to = to->q_paddr;
X		to->q_flags |= QDONTSEND;
X
X		/*
X		**  Check to see that these people are allowed to
X		**  talk to each other.
X		*/
X
X		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
X		{
X			NoReturn = TRUE;
X			usrerr("Message is too large; %ld bytes max", m->m_maxsize);
X			giveresponse(EX_UNAVAILABLE, m, e);
X			continue;
X		}
X		if (!checkcompat(to))
X		{
X			giveresponse(EX_UNAVAILABLE, m, e);
X			continue;
X		}
X
X		/*
X		**  Strip quote bits from names if the mailer is dumb
X		**	about them.
X		*/
X
X		if (bitnset(M_STRIPQ, m->m_flags))
X		{
X			stripquotes(user, TRUE);
X			stripquotes(host, TRUE);
X		}
X		else
X		{
X			stripquotes(user, FALSE);
X			stripquotes(host, FALSE);
X		}
X
X		/* hack attack -- delivermail compatibility */
X		if (m == ProgMailer && *user == '|')
X			user++;
X
X		/*
X		**  If an error message has already been given, don't
X		**	bother to send to this address.
X		**
X		**	>>>>>>>>>> This clause assumes that the local mailer
X		**	>> NOTE >> cannot do any further aliasing; that
X		**	>>>>>>>>>> function is subsumed by sendmail.
X		*/
X
X		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
X			continue;
X
X		/* save statistics.... */
X		markstats(e, to);
X
X		/*
X		**  See if this user name is "special".
X		**	If the user name has a slash in it, assume that this
X		**	is a file -- send it off without further ado.  Note
X		**	that this type of addresses is not processed along
X		**	with the others, so we fudge on the To person.
X		*/
X
X		if (m == LocalMailer)
X		{
X			if (user[0] == '/')
X			{
X				rcode = mailfile(user, getctladdr(to));
X				giveresponse(rcode, m, e);
X				continue;
X			}
X		}
X
X		/*
X		**  Address is verified -- add this user to mailer
X		**  argv, and add it to the print list of recipients.
X		*/
X
X		/* link together the chain of recipients */
X		to->q_tchain = tochain;
X		tochain = to;
X
X		/* create list of users for error messages */
X		(void) strcat(tobuf, ",");
X		(void) strcat(tobuf, to->q_paddr);
X		define('u', user, e);		/* to user */
X		define('z', to->q_home, e);	/* user's home */
X
X		/*
X		**  Expand out this user into argument list.
X		*/
X
X		if (!clever)
X		{
X			expand(*mvp, buf, &buf[sizeof buf - 1], e);
X			*pvp++ = newstr(buf);
X			if (pvp >= &pv[MAXPV - 2])
X			{
X				/* allow some space for trailing parms */
X				break;
X			}
X		}
X	}
X
X	/* see if any addresses still exist */
X	if (tobuf[0] == '\0')
X	{
X		define('g', (char *) NULL, e);
X		return (0);
X	}
X
X	/* print out messages as full list */
X	e->e_to = tobuf + 1;
X
X	/*
X	**  Fill out any parameters after the $u parameter.
X	*/
X
X	while (!clever && *++mvp != NULL)
X	{
X		expand(*mvp, buf, &buf[sizeof buf - 1], e);
X		*pvp++ = newstr(buf);
X		if (pvp >= &pv[MAXPV])
X			syserr("deliver: pv overflow after $u for %s", pv[0]);
X	}
X	*pvp++ = NULL;
X
X	/*
X	**  Call the mailer.
X	**	The argument vector gets built, pipes
X	**	are created as necessary, and we fork & exec as
X	**	appropriate.
X	**	If we are running SMTP, we just need to clean up.
X	*/
X
X	message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
X
X	if (ctladdr == NULL)
X		ctladdr = &e->e_from;
X# ifdef SMTP
X	if (clever)
X	{
X# ifdef MXDOMAIN
X		expand("\001w", buf, &buf[sizeof buf - 1], e);
X		if (host[0] == '[')
X		{
X			Nmx = 1;
X			MxHosts[0] = host;
X			rcode = EX_OK;
X		}
X		else if ((Nmx = getmxrr(host, MxHosts, MAXMXHOSTS, buf)) < 0)
X		{
X			/*
X			 * Map errors into standard values
X			 */
X			if (Nmx == -1)
X				rcode = EX_TEMPFAIL;
X			else if (Nmx == -3)
X				rcode = EX_NOHOST;
X			else
X				rcode = EX_UNAVAILABLE;
X		}
X		else
X			rcode = EX_OK;
X#else MXDOMAIN
X		Nmx = 1;
X		MxHosts[0] = host;
X		rcode = EX_OK;
X#endif
X		/* send the initial SMTP protocol */
X		if (rcode == EX_OK)
X			rcode = smtpinit(m, pv);
X
X		if (rcode == EX_OK)
X		{
X			/* send the recipient list */
X			tobuf[0] = '\0';
X			for (to = tochain; to != NULL; to = to->q_tchain)
X			{
X				int i;
X
X				e->e_to = to->q_paddr;
X				i = smtprcpt(to, m);
X				if (i != EX_OK)
X				{
X					markfailure(e, to, i);
X					giveresponse(i, m, e);
X				}
X				else
X				{
X					(void) strcat(tobuf, ",");
X					(void) strcat(tobuf, to->q_paddr);
X				}
X			}
X
X			/* now send the data */
X			if (tobuf[0] == '\0')
X				e->e_to = NULL;
X			else
X			{
X				e->e_to = tobuf + 1;
X				rcode = smtpdata(m, e);
X			}
X
X			/* now close the connection */
X			smtpquit(m);
X		}
X	}
X	else
X# endif SMTP
X		rcode = sendoff(e, m, pv, ctladdr);
X
X	/*
X	**  Do final status disposal.
X	**	We check for something in tobuf for the SMTP case.
X	**	If we got a temporary failure, arrange to queue the
X	**		addressees.
X	*/
X
X	if (tobuf[0] != '\0')
X		giveresponse(rcode, m, e);
X	if (rcode != EX_OK)
X	{
X		for (to = tochain; to != NULL; to = to->q_tchain)
X			markfailure(e, to, rcode);
X	}
X
X	errno = 0;
X	define('g', (char *) NULL, e);
X	return (rcode);
X}
X/*
X**  MARKFAILURE -- mark a failure on a specific address.
X**
X**	Parameters:
X**		e -- the envelope we are sending.
X**		q -- the address to mark.
X**		rcode -- the code signifying the particular failure.
X**
X**	Returns:
X**		none.
X**
X**	Side Effects:
X**		marks the address (and possibly the envelope) with the
X**			failure so that an error will be returned or
X**			the message will be queued, as appropriate.
X*/
X
Xmarkfailure(e, q, rcode)
X	register ENVELOPE *e;
X	register ADDRESS *q;
X	int rcode;
X{
X	if (rcode == EX_OK)
X		return;
X	else if (rcode != EX_TEMPFAIL)
X		q->q_flags |= QBADADDR;
X	else if (curtime() > e->e_ctime + TimeOut)
X	{
X		extern char *pintvl();
X		char buf[MAXLINE];
X
X		if (!bitset(EF_TIMEOUT, e->e_flags))
X		{
X			(void) sprintf(buf, "Cannot send message for %s",
X				pintvl(TimeOut, FALSE));
X			if (e->e_message != NULL)
X				free(e->e_message);
X			e->e_message = newstr(buf);
X			message(Arpa_Info, buf);
X		}
X		q->q_flags |= QBADADDR;
X		e->e_flags |= EF_TIMEOUT;
X	}
X	else
X		q->q_flags |= QQUEUEUP;
X}
X/*
X**  DOFORK -- do a fork, retrying a couple of times on failure.
X**
X**	This MUST be a macro, since after a vfork we are running
X**	two processes on the same stack!!!
X**
X**	Parameters:
X**		none.
X**
X**	Returns:
X**		From a macro???  You've got to be kidding!
X**
X**	Side Effects:
X**		Modifies the ==> LOCAL <== variable 'pid', leaving:
X**			pid of child in parent, zero in child.
X**			-1 on unrecoverable error.
X**
X**	Notes:
X**		I'm awfully sorry this looks so awful.  That's
X**		vfork for you.....
X*/
X
X# define NFORKTRIES	5
X# ifdef VMUNIX
X# define XFORK	vfork
X# else VMUNIX
X# define XFORK	fork
X# endif VMUNIX
X
X# define DOFORK(fORKfN) \
X{\
X	register int i;\
X\
X	for (i = NFORKTRIES; --i >= 0; )\
X	{\
X		pid = fORKfN();\
X		if (pid >= 0)\
X			break;\
X		if (i > 0)\
X			sleep((unsigned) NFORKTRIES - i);\
X	}\
X}
X/*
X**  DOFORK -- simple fork interface to DOFORK.
X**
X**	Parameters:
X**		none.
X**
X**	Returns:
X**		pid of child in parent.
X**		zero in child.
X**		-1 on error.
X**
X**	Side Effects:
X**		returns twice, once in parent and once in child.
X*/
X
Xdofork()
X{
X	register int pid;
X
X	DOFORK(fork);
X	return (pid);
X}
X/*
X**  SENDOFF -- send off call to mailer & collect response.
X**
X**	Parameters:
X**		e -- the envelope to mail.
X**		m -- mailer descriptor.
X**		pvp -- parameter vector to send to it.
X**		ctladdr -- an address pointer controlling the
X**			user/groupid etc. of the mailer.
X**
X**	Returns:
X**		exit status of mailer.
X**
X**	Side Effects:
X**		none.
X*/
X
Xsendoff(e, m, pvp, ctladdr)
X	register ENVELOPE *e;
X	MAILER *m;
X	char **pvp;
X	ADDRESS *ctladdr;
X{
X	auto FILE *mfile;
X	auto FILE *rfile;
X	register int i;
X	int pid;
X
X	/*
X	**  Create connection to mailer.
X	*/
X
X	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
X	if (pid < 0)
X		return (-1);
X
X	/*
X	**  Format and send message.
X	*/
X
X	putfromline(mfile, m);
X	(*e->e_puthdr)(mfile, m, e);
X	putline("\n", mfile, m);
X	(*e->e_putbody)(mfile, m, e);
X	(void) fclose(mfile);
X
X	i = endmailer(pid, pvp[0]);
X
X	/* arrange a return receipt if requested */
X	if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags))
X	{
X		e->e_flags |= EF_SENDRECEIPT;
X		/* do we want to send back more info? */
X	}
X
X	return (i);
X}
X/*
X**  ENDMAILER -- Wait for mailer to terminate.
X**
X**	We should never get fatal errors (e.g., segmentation
X**	violation), so we report those specially.  For other
X**	errors, we choose a status message (into statmsg),
X**	and if it represents an error, we print it.
X**
X**	Parameters:
X**		pid -- pid of mailer.
X**		name -- name of mailer (for error messages).
X**
X**	Returns:
X**		exit code of mailer.
X**
X**	Side Effects:
X**		none.
X*/
X
Xendmailer(pid, name)
X	int pid;
X	char *name;
X{
X	int st;
X
X	/* in the IPC case there is nothing to wait for */
X	if (pid == 0)
X		return (EX_OK);
X
X	/* wait for the mailer process to die and collect status */
X	st = waitfor(pid);
X	if (st == -1)
X	{
X		syserr("endmailer %s: wait", name);
X		return (EX_SOFTWARE);
X	}
X
X	/* see if it died a horrid death */
X	if ((st & 0377) != 0)
X	{
X		syserr("mailer %s died with signal %o", name, st);
X		ExitStat = EX_TEMPFAIL;
X		return (EX_TEMPFAIL);
X	}
X
X	/* normal death -- return status */
X	st = (st >> 8) & 0377;
X	return (st);
X}
X/*
X**  OPENMAILER -- open connection to mailer.
X**
X**	Parameters:
X**		m -- mailer descriptor.
X**		pvp -- parameter vector to pass to mailer.
X**		ctladdr -- controlling address for user.
X**		clever -- create a full duplex connection.
X**		pmfile -- pointer to mfile (to mailer) connection.
X**		prfile -- pointer to rfile (from mailer) connection.
X**
X**	Returns:
X**		pid of mailer ( > 0 ).
X**		-1 on error.
X**		zero on an IPC connection.
X**
X**	Side Effects:
X**		creates a mailer in a subprocess.
X*/
X
Xopenmailer(m, pvp, ctladdr, clever, pmfile, prfile)
X	MAILER *m;
X	char **pvp;
X	ADDRESS *ctladdr;
X	bool clever;
X	FILE **pmfile;
X	FILE **prfile;
X{
X	int pid;
X	int mpvect[2];
X	int rpvect[2];
X	FILE *mfile;
X	FILE *rfile;
X	extern FILE *fdopen();
X
X# ifdef DEBUG
X	if (tTd(11, 1))
X	{
X		printf("openmailer:");
X		printav(pvp);
X	}
X# endif DEBUG
X	errno = 0;
X
X	CurHostName = m->m_mailer;
X
X	/*
X	**  Deal with the special case of mail handled through an IPC
X	**  connection.
X	**	In this case we don't actually fork.  We must be
X	**	running SMTP for this to work.  We will return a
X	**	zero pid to indicate that we are running IPC.
X	**  We also handle a debug version that just talks to stdin/out.
X	*/
X
X#ifdef DEBUG
X	/* check for Local Person Communication -- not for mortals!!! */
X	if (strcmp(m->m_mailer, "[LPC]") == 0)
X	{
X		*pmfile = stdout;
X		*prfile = stdin;
X		return (0);
X	}
X#endif DEBUG
X
X	if (strcmp(m->m_mailer, "[IPC]") == 0)
X	{
X#ifdef HOSTINFO
X		register STAB *st;
X		extern STAB *stab();
X#endif HOSTINFO
X#ifdef DAEMON
X		register int i, j;
X		register u_short port;
X
X		CurHostName = pvp[1];
X		if (!clever)
X			syserr("non-clever IPC");
X		if (pvp[2] != NULL)
X			port = atoi(pvp[2]);
X		else
X			port = 0;
X		for (j = 0; j < Nmx; j++)
X		{
X			CurHostName = MxHosts[j];
X#ifdef HOSTINFO
X		/* see if we have already determined that this host is fried */
X			st = stab(MxHosts[j], ST_HOST, ST_FIND);
X			if (st == NULL || st->s_host.ho_exitstat == EX_OK)
X				i = makeconnection(MxHosts[j], port, pmfile, prfile);
X			else
X			{
X				i = st->s_host.ho_exitstat;
X				errno = st->s_host.ho_errno;
X			}
X#else HOSTINFO
X			i = makeconnection(MxHosts[j], port, pmfile, prfile);
X#endif HOSTINFO
X			if (i != EX_OK)
X			{
X#ifdef HOSTINFO
X				/* enter status of this host */
X				if (st == NULL)
X					st = stab(MxHosts[j], ST_HOST, ST_ENTER);
X				st->s_host.ho_exitstat = i;
X				st->s_host.ho_errno = errno;
X#endif HOSTINFO
X				ExitStat = i;
X				continue;
X			}
X			else
X				return (0);
X		}
X		return (-1);
X#else DAEMON
X		syserr("openmailer: no IPC");
X		return (-1);
X#endif DAEMON
X	}
X
X	/* create a pipe to shove the mail through */
X	if (pipe(mpvect) < 0)
X	{
X		syserr("openmailer: pipe (to mailer)");
X		return (-1);
X	}
X
X#ifdef SMTP
X	/* if this mailer speaks smtp, create a return pipe */
X	if (clever && pipe(rpvect) < 0)
X	{
X		syserr("openmailer: pipe (from mailer)");
X		(void) close(mpvect[0]);
X		(void) close(mpvect[1]);
X		return (-1);
X	}
X#endif SMTP
X
X	/*
X	**  Actually fork the mailer process.
X	**	DOFORK is clever about retrying.
X	**
X	**	Dispose of SIGCHLD signal catchers that may be laying
X	**	around so that endmail will get it.
X	*/
X
X	if (CurEnv->e_xfp != NULL)
X		(void) fflush(CurEnv->e_xfp);		/* for debugging */
X	(void) fflush(stdout);
X# ifdef SIGCHLD
X	(void) signal(SIGCHLD, SIG_DFL);
X# endif SIGCHLD
X	DOFORK(XFORK);
X	/* pid is set by DOFORK */
X	if (pid < 0)
X	{
X		/* failure */
X		syserr("openmailer: cannot fork");
X		(void) close(mpvect[0]);
X		(void) close(mpvect[1]);
X#ifdef SMTP
X		if (clever)
X		{
X			(void) close(rpvect[0]);
X			(void) close(rpvect[1]);
X		}
X#endif SMTP
X		return (-1);
X	}
X	else if (pid == 0)
X	{
X		int i;
X		extern int DtableSize;
X
X		/* child -- set up input & exec mailer */
X		/* make diagnostic output be standard output */
X		(void) signal(SIGINT, SIG_IGN);
X		(void) signal(SIGHUP, SIG_IGN);
X		(void) signal(SIGTERM, SIG_DFL);
X
X		/* arrange to filter standard & diag output of command */
X		if (clever)
X		{
X			(void) close(rpvect[0]);
X			(void) close(1);
X			(void) dup(rpvect[1]);
X			(void) close(rpvect[1]);
X		}
X		else if (OpMode == MD_SMTP || HoldErrs)
X		{
X			/* put mailer output in transcript */
X			(void) close(1);
X			(void) dup(fileno(CurEnv->e_xfp));
X		}
X		(void) close(2);
X		(void) dup(1);
X
X		/* arrange to get standard input */
X		(void) close(mpvect[1]);
X		(void) close(0);
X		if (dup(mpvect[0]) < 0)
X		{
X			syserr("Cannot dup to zero!");
X			_exit(EX_OSERR);
X		}
X		(void) close(mpvect[0]);
X		if (!bitnset(M_RESTR, m->m_flags))
X		{
X			if (ctladdr == NULL || ctladdr->q_uid == 0)
X			{
X				(void) setgid(DefGid);
X				(void) setuid(DefUid);
X			}
X			else
X			{
X				(void) setgid(ctladdr->q_gid);
X				(void) setuid(ctladdr->q_uid);
X			}
X		}
X
X		/* arrange for all the files to be closed */
X		for (i = 3; i < DtableSize; i++)
X#ifdef FIOCLEX
X			(void) ioctl(i, FIOCLEX, 0);
X#else FIOCLEX
X			(void) close(i);
X#endif FIOCLEX
X
X		/* try to execute the mailer */
X		execve(m->m_mailer, pvp, UserEnviron);
X
X#ifdef FIOCLEX
X		syserr("Cannot exec %s", m->m_mailer);
X#else FIOCLEX
X		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
X		(void) fflush(stdout);
X#endif FIOCLEX
X#ifdef EPROCLIM
X		if (m == LocalMailer || errno == EIO || errno == EAGAIN ||
X		    errno == ENOMEM || errno == EPROCLIM)
X#else
X		if (m == LocalMailer || errno == EIO || errno == EAGAIN ||
X		    errno == ENOMEM)
X#endif
X			_exit(EX_TEMPFAIL);
X		else
X			_exit(EX_UNAVAILABLE);
X	}
X
X	/*
X	**  Set up return value.
X	*/
X
X	(void) close(mpvect[0]);
X	mfile = fdopen(mpvect[1], "w");
X	if (clever)
X	{
X		(void) close(rpvect[1]);
X		rfile = fdopen(rpvect[0], "r");
X	}
X
X	*pmfile = mfile;
X	*prfile = rfile;
X
X	return (pid);
X}
X/*
X**  GIVERESPONSE -- Interpret an error response from a mailer
X**
X**	Parameters:
X**		stat -- the status code from the mailer (high byte
X**			only; core dumps must have been taken care of
X**			already).
X**		m -- the mailer descriptor for this mailer.
X**
X**	Returns:
X**		none.
X**
X**	Side Effects:
X**		Errors may be incremented.
X**		ExitStat may be set.
X*/
X
Xgiveresponse(stat, m, e)
X	int stat;
X	register MAILER *m;
X	ENVELOPE *e;
X{
X	register char *statmsg;
X	extern char *SysExMsg[];
X	register int i;
X	extern int N_SysEx;
X#if	defined(DAEMON) || defined(MXDOMAIN)
X	extern int h_errno;
X#endif
X	char buf[MAXLINE];
X
X#ifdef lint
X	if (m == NULL)
X		return;
X#endif lint
X
X	/*
X	**  Compute status message from code.
X	*/
X
X	i = stat - EX__BASE;
X	if (stat == 0)
X		statmsg = "250 Sent";
X	else if (i < 0 || i > N_SysEx)
X	{
X		(void) sprintf(buf, "554 unknown mailer error %d", stat);
X		stat = EX_UNAVAILABLE;
X		statmsg = buf;
X	}
X	else if (stat == EX_TEMPFAIL)
X	{
X		(void) strcpy(buf, SysExMsg[i]);
X#if	defined(TRY_AGAIN) && (defined(DAEMON) || defined(MXDOMAIN))
X		if (h_errno == TRY_AGAIN)
X		{
X			extern char *errstring();
X
X			statmsg = errstring(h_errno+MAX_ERRNO);
X		}
X		else
X#endif
X		{
X			if (errno != 0)
X			{
X				extern char *errstring();
X
X				statmsg = errstring(errno);
X			}
X			else
X			{
X#ifdef SMTP
X				extern char SmtpError[];
X
X				statmsg = SmtpError;
X#else SMTP
X				statmsg = NULL;
X#endif SMTP
X			}
X		}
X		if (statmsg != NULL && statmsg[0] != '\0')
X		{
X			(void) strcat(buf, ": ");
X			(void) strcat(buf, statmsg);
X		}
X		statmsg = buf;
X	}
X	else
X	{
X		statmsg = SysExMsg[i];
X	}
X
X	/*
X	**  Print the message as appropriate
X	*/
X
X	if (stat == EX_OK || stat == EX_TEMPFAIL)
X		message(Arpa_Info, &statmsg[4]);
X	else
X	{
X		Errors++;
X		usrerr(statmsg);
X	}
X
X	/*
X	**  Final cleanup.
X	**	Log a record of the transaction.  Compute the new
X	**	ExitStat -- if we already had an error, stick with
X	**	that.
X	*/
X
X	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
X		logdelivery(&statmsg[4]);
X
X	if (stat != EX_TEMPFAIL)
X		setstat(stat);
X	if (stat != EX_OK)
X	{
X		if (e->e_message != NULL)
X			free(e->e_message);
X		e->e_message = newstr(&statmsg[4]);
X	}
X	errno = 0;
X#if	defined(DAEMON) || defined(MXDOMAIN)
X	h_errno = 0;
X#endif
X}
X/*
X**  LOGDELIVERY -- log the delivery in the system log
X**
X**	Parameters:
X**		stat -- the message to print for the status
X**
X**	Returns:
X**		none
X**
X**	Side Effects:
X**		none
X*/
X
Xlogdelivery(stat)
X	char *stat;
X{
X	extern char *pintvl();
X
X# ifdef LOG
X	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id,
X	       CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat);
X# endif LOG
X}
X/*
X**  PUTFROMLINE -- output a UNIX-style from line (or whatever)
X**
X**	This can be made an arbitrary message separator by changing $l
X**
X**	One of the ugliest hacks seen by human eyes is contained herein:
X**	UUCP wants those stupid "remote from <host>" lines.  Why oh why
X**	does a well-meaning programmer such as myself have to deal with
X**	this kind of antique garbage????
X**
X**	Parameters:
X**		fp -- the file to output to.
X**		m -- the mailer describing this entry.
X**
X**	Returns:
X**		none
X**
X**	Side Effects:
X**		outputs some text to fp.
X*/
X
Xputfromline(fp, m)
X	register FILE *fp;
X	register MAILER *m;
X{
X	char *template = "\001l\n";
X	char buf[MAXLINE];
X
X	if (bitnset(M_NHDR, m->m_flags))
X		return;
X
X# ifdef UGLYUUCP
X	if (bitnset(M_UGLYUUCP, m->m_flags))
X	{
X		char *bang;
X		char xbuf[MAXLINE];
X
X		expand("\001g", buf, &buf[sizeof buf - 1], CurEnv);
X		bang = index(buf, '!');
X		if (bang == NULL)
X			syserr("No ! in UUCP! (%s)", buf);
X		else
X		{
X			*bang++ = '\0';
X			(void) sprintf(xbuf, "From %s  \001d remote from %s\n", bang, buf);
X			template = xbuf;
X		}
X	}
X# endif UGLYUUCP
X	expand(template, buf, &buf[sizeof buf - 1], CurEnv);
X	putline(buf, fp, m);
X}
X/*
X**  PUTBODY -- put the body of a message.
X**
X**	Parameters:
X**		fp -- file to output onto.
X**		m -- a mailer descriptor to control output format.
X**		e -- the envelope to put out.
X**
X**	Returns:
X**		none.
X**
X**	Side Effects:
X**		The message is written onto fp.
X*/
X
Xputbody(fp, m, e)
X	FILE *fp;
X	MAILER *m;
X	register ENVELOPE *e;
X{
X	char buf[MAXLINE];
X
X	/*
X	**  Output the body of the message
X	*/
X
X	if (e->e_dfp == NULL)
X	{
X		if (e->e_df != NULL)
X		{
X			e->e_dfp = fopen(e->e_df, "r");
X			if (e->e_dfp == NULL)
X				syserr("Cannot open %s", e->e_df);
X		}
X		else
X			putline("<<< No Message Collected >>>", fp, m);
X	}
X	if (e->e_dfp != NULL)
X	{
X		rewind(e->e_dfp);
X		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
X		{
X			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
X			    strncmp(buf, "From", 4) == 0)
X				(void) putc('>', fp);
X			putline(buf, fp, m);
X		}
X
X		if (ferror(e->e_dfp))
X		{
X			syserr("putbody: read error");
X			ExitStat = EX_IOERR;
X		}
X	}
X
X	(void) fflush(fp);
X	if (ferror(fp) && errno != EPIPE)
X	{
X		syserr("putbody: write error");
X		ExitStat = EX_IOERR;
X	}
X	errno = 0;
X}
X/*
X**  MAILFILE -- Send a message to a file.
X**
X**	If the file has the setuid/setgid bits set, but NO execute
X**	bits, sendmail will try to become the owner of that file
X**	rather than the real user.  Obviously, this only works if
X**	sendmail runs as root.
X**
X**	This could be done as a subordinate mailer, except that it
X**	is used implicitly to save messages in ~/dead.letter.  We
X**	view this as being sufficiently important as to include it
X**	here.  For example, if the system is dying, we shouldn't have
X**	to create another process plus some pipes to save the message.
X**
X**	Parameters:
X**		filename -- the name of the file to send to.
X**		ctladdr -- the controlling address header -- includes
X**			the userid/groupid to be when sending.
X**
X**	Returns:
X**		The exit code associated with the operation.
X**
X**	Side Effects:
X**		none.
X*/
X
Xmailfile(filename, ctladdr)
X	char *filename;
X	ADDRESS *ctladdr;
X{
X	register FILE *f;
X	register int pid;
X
X	/*
X	**  Fork so we can change permissions here.
X	**	Note that we MUST use fork, not vfork, because of
X	**	the complications of calling subroutines, etc.
X	*/
X
X	DOFORK(fork);
X
X	if (pid < 0)
X		return (EX_OSERR);
X	else if (pid == 0)
X	{
X		/* child -- actually write to file */
X		struct stat stb;
X
X		(void) signal(SIGINT, SIG_DFL);
X		(void) signal(SIGHUP, SIG_DFL);
X		(void) signal(SIGTERM, SIG_DFL);
X		(void) umask(OldUmask);
X		if (stat(filename, &stb) < 0)
X		{
X			errno = 0;
X			stb.st_mode = 0666;
X		}
X		if (bitset(0111, stb.st_mode))
X			exit(EX_CANTCREAT);
X		if (ctladdr == NULL)
X			ctladdr = &CurEnv->e_from;
X		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
X		{
X			if (ctladdr->q_uid == 0)
X				(void) setgid(DefGid);
X			else
X				(void) setgid(ctladdr->q_gid);
X		}
X		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
X		{
X			if (ctladdr->q_uid == 0)
X				(void) setuid(DefUid);
X			else
X				(void) setuid(ctladdr->q_uid);
X		}
X		f = dfopen(filename, "a");
X		if (f == NULL)
X			exit(EX_CANTCREAT);
X
X		putfromline(f, ProgMailer);
X		(*CurEnv->e_puthdr)(f, ProgMailer, CurEnv);
X		putline("\n", f, ProgMailer);
X		(*CurEnv->e_putbody)(f, ProgMailer, CurEnv);
X		putline("\n", f, ProgMailer);
X		(void) fclose(f);
X		(void) fflush(stdout);
X
X		/* reset ISUID & ISGID bits for paranoid systems */
X		(void) chmod(filename, (int) stb.st_mode);
X		exit(EX_OK);
X		/*NOTREACHED*/
X	}
X	else
X	{
X		/* parent -- wait for exit status */
X		int st;
X
X		st = waitfor(pid);
X		if ((st & 0377) != 0)
X			return (EX_UNAVAILABLE);
X		else
X			return ((st >> 8) & 0377);
X	}
X}
X/*
X**  SENDALL -- actually send all the messages.
X**
X**	Parameters:
X**		e -- the envelope to send.
X**		mode -- the delivery mode to use.  If SM_DEFAULT, use
X**			the current SendMode.
X**
X**	Returns:
X**		none.
X**
X**	Side Effects:
X**		Scans the send lists and sends everything it finds.
X**		Delivers any appropriate error messages.
X**		If we are running in a non-interactive mode, takes the
X**			appropriate action.
X*/
X
Xsendall(e, mode)
X	ENVELOPE *e;
X	char mode;
X{
X	register ADDRESS *q;
X	bool oldverbose;
X	int pid;
X
X	/* determine actual delivery mode */
X	if (mode == SM_DEFAULT)
X	{
X		extern bool shouldqueue();
X
X		if (shouldqueue(e->e_msgpriority))
X			mode = SM_QUEUE;
X		else
X			mode = SendMode;
X	}
X
X#ifdef DEBUG
X	if (tTd(13, 1))
X	{
X		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
X		printaddr(e->e_sendqueue, TRUE);
X	}
X#endif DEBUG
X
X	/*
X	**  Do any preprocessing necessary for the mode we are running.
X	**	Check to make sure the hop count is reasonable.
X	**	Delete sends to the sender in mailing lists.
X	*/
X
X	CurEnv = e;
X
X	if (e->e_hopcount > MAXHOP)
X	{
X		syserr("sendall: too many hops (%d max)", MAXHOP);
X		return;
X	}
X
X	if (!MeToo)
X	{
X		extern ADDRESS *recipient();
X
X		e->e_from.q_flags |= QDONTSEND;
X		(void) recipient(&e->e_from, &e->e_sendqueue);
X	}
X
X# ifdef QUEUE
X	if ((mode == SM_QUEUE || mode == SM_FORK ||
X	     (mode != SM_VERIFY && SuperSafe)) &&
X	    !bitset(EF_INQUEUE, e->e_flags))
X		queueup(e, TRUE, mode == SM_QUEUE);
X#endif QUEUE
X
X	oldverbose = Verbose;
X	switch (mode)
X	{
X	  case SM_VERIFY:
X		Verbose = TRUE;
X		break;
X
X	  case SM_QUEUE:
X		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
X		return;
X
X	  case SM_FORK:
X		if (e->e_xfp != NULL)
X			(void) fflush(e->e_xfp);
X		pid = fork();
X		if (pid < 0)
X		{
X			mode = SM_DELIVER;
X			break;
X		}
X		else if (pid > 0)
X		{
X			/* be sure we leave the temp files to our child */
X			e->e_id = e->e_df = NULL;
X			return;
X		}
X
X		/* double fork to avoid zombies */
X		if (fork() > 0)
X			exit(EX_OK);
X
X		/* be sure we are immune from the terminal */
X		disconnect(FALSE);
X
X		break;
X	}
X
X	/*
X	**  Run through the list and send everything.
X	*/
X
X	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
X	{
X		if (mode == SM_VERIFY)
X		{
X			e->e_to = q->q_paddr;
X			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
X				message(Arpa_Info, "deliverable");
X		}
X		else
X			(void) deliver(e, q);
X	}
X	Verbose = oldverbose;
X
X	/*
X	**  Now run through and check for errors.
X	*/
X
X	if (mode == SM_VERIFY)
X		return;
X
X	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
X	{
X		register ADDRESS *qq;
X
X# ifdef DEBUG
X		if (tTd(13, 3))
X		{
X			printf("Checking ");
X			printaddr(q, FALSE);
X		}
X# endif DEBUG
X
X		/* only send errors if the message failed */
X		if (!bitset(QBADADDR, q->q_flags))
X			continue;
X
X		/* we have an address that failed -- find the parent */
X		for (qq = q; qq != NULL; qq = qq->q_alias)
X		{
X			char obuf[MAXNAME + 6];
X			extern char *aliaslookup();
X
X			/* we can only have owners for local addresses */
X			if (!bitnset(M_LOCAL, qq->q_mailer->m_flags))
X				continue;
X
X			/* see if the owner list exists */
X			(void) strcpy(obuf, "owner-");
X			if (strncmp(qq->q_user, "owner-", 6) == 0)
X				(void) strcat(obuf, "owner");
X			else
X				(void) strcat(obuf, qq->q_user);
X			if (aliaslookup(obuf) == NULL)
X				continue;
X
X# ifdef DEBUG
X			if (tTd(13, 4))
X				printf("Errors to %s\n", obuf);
X# endif DEBUG
X
X			/* owner list exists -- add it to the error queue */
X			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue);
X			ErrorMode = EM_MAIL;
X			break;
X		}
X
X		/* if we did not find an owner, send to the sender */
X		if (qq == NULL && bitset(QBADADDR, q->q_flags))
X			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue);
X	}
X
X	if (mode == SM_FORK)
X		finis();
X}
END_OF_FILE
if test 31247 -ne `wc -c <'src/deliver.c'`; then
    echo shar: \"'src/deliver.c'\" unpacked with wrong size!
fi
# end of 'src/deliver.c'
fi
echo shar: End of archive 8 \(of 8\).
cp /dev/null ark8isdone
MISSING=""
for I in 1 2 3 4 5 6 7 8 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have unpacked all 8 archives.
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0
-- 
David H. Brierley
Home: dave at galaxia.Newport.RI.US   {rayssd,xanth,lazlo,jclyde}!galaxia!dave
Work: dhb at rayssd.ray.com           {sun,decuac,gatech,necntc,ukma}!rayssd!dhb



More information about the Unix-pc.sources mailing list