125a99e2eSeric # include <signal.h>
254aa2b0fSeric # include <errno.h>
3b20b3270Seric # include "sendmail.h"
4c77d1c25Seric # include <sys/stat.h>
525a99e2eSeric 
6*87c9b3e7Seric SCCSID(@(#)deliver.c	4.3		10/01/83);
7259cace7Seric 
825a99e2eSeric /*
913bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
1013bbc08cSeric **
1113bbc08cSeric **	This routine delivers to everyone on the same host as the
1213bbc08cSeric **	user on the head of the list.  It is clever about mailers
1313bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
1413bbc08cSeric **	that it will deliver to all these addresses however -- so
1513bbc08cSeric **	deliver should be called once for each address on the
1613bbc08cSeric **	list.
1725a99e2eSeric **
1825a99e2eSeric **	Parameters:
19588cad61Seric **		e -- the envelope to deliver.
20c77d1c25Seric **		firstto -- head of the address list to deliver to.
2125a99e2eSeric **
2225a99e2eSeric **	Returns:
2325a99e2eSeric **		zero -- successfully delivered.
2425a99e2eSeric **		else -- some failure, see ExitStat for more info.
2525a99e2eSeric **
2625a99e2eSeric **	Side Effects:
2725a99e2eSeric **		The standard input is passed off to someone.
2825a99e2eSeric */
2925a99e2eSeric 
30588cad61Seric deliver(e, firstto)
31588cad61Seric 	register ENVELOPE *e;
32c77d1c25Seric 	ADDRESS *firstto;
3325a99e2eSeric {
3478442df3Seric 	char *host;			/* host being sent to */
3578442df3Seric 	char *user;			/* user being sent to */
3625a99e2eSeric 	char **pvp;
375dfc646bSeric 	register char **mvp;
3825a99e2eSeric 	register char *p;
39588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
406259796dSeric 	ADDRESS *ctladdr;
41c77d1c25Seric 	register ADDRESS *to = firstto;
42c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
43772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
445826d9d3Seric 	register int rcode;		/* response code */
45ee6bf8dfSeric 	char *pv[MAXPV+1];
46ee6bf8dfSeric 	char tobuf[MAXLINE-50];		/* text line of to people */
47ee6bf8dfSeric 	char buf[MAXNAME];
48ee6bf8dfSeric 	char tfrombuf[MAXNAME];		/* translated from person */
49ee6bf8dfSeric 	extern bool checkcompat();
50ee6bf8dfSeric 	extern ADDRESS *getctladdr();
51ee6bf8dfSeric 	extern char *remotename();
5225a99e2eSeric 
5335490626Seric 	errno = 0;
54da2935e1Seric 	if (bitset(QDONTSEND, to->q_flags))
555dfc646bSeric 		return (0);
5625a99e2eSeric 
5751552439Seric 	m = to->q_mailer;
5851552439Seric 	host = to->q_host;
5951552439Seric 
6025a99e2eSeric # ifdef DEBUG
616ef48975Seric 	if (tTd(10, 1))
625dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
6351552439Seric 			m->m_mno, host, to->q_user);
6425a99e2eSeric # endif DEBUG
65f3dbc832Seric 
66f3dbc832Seric 	/*
67f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
68f3dbc832Seric 	**  connections now, just mark these addresses and return.
69f3dbc832Seric 	**	This is useful if we want to batch connections to
70f3dbc832Seric 	**	reduce load.  This will cause the messages to be
71f3dbc832Seric 	**	queued up, and a daemon will come along to send the
72f3dbc832Seric 	**	messages later.
73f3dbc832Seric 	**		This should be on a per-mailer basis.
74f3dbc832Seric 	*/
75f3dbc832Seric 
7657fc6f17Seric 	if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) &&
77317680d6Seric 	    !Verbose)
78f3dbc832Seric 	{
79f3dbc832Seric 		for (; to != NULL; to = to->q_next)
80f4560e80Seric 		{
81f4560e80Seric 			if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m)
82f4560e80Seric 				continue;
83f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
84588cad61Seric 			e->e_to = to->q_paddr;
85eb238f8cSeric 			message(Arpa_Info, "queued");
86eb238f8cSeric 			if (LogLevel > 4)
87eb238f8cSeric 				logdelivery("queued");
88f4560e80Seric 		}
89588cad61Seric 		e->e_to = NULL;
90f3dbc832Seric 		return (0);
91f3dbc832Seric 	}
92f3dbc832Seric 
9325a99e2eSeric 	/*
945dfc646bSeric 	**  Do initial argv setup.
955dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
965dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
975dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
985dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
995dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
1003bea8136Seric 	**		The from address rewrite is expected to make
1013bea8136Seric 	**		the address relative to the other end.
1025dfc646bSeric 	*/
1035dfc646bSeric 
10478442df3Seric 	/* rewrite from address, using rewriting rules */
105588cad61Seric 	expand("$f", buf, &buf[sizeof buf - 1], e);
106ee6bf8dfSeric 	(void) strcpy(tfrombuf, remotename(buf, m, TRUE, TRUE));
10778442df3Seric 
108588cad61Seric 	define('g', tfrombuf, e);		/* translated sender address */
109588cad61Seric 	define('h', host, e);			/* to host */
1105dfc646bSeric 	Errors = 0;
1115dfc646bSeric 	pvp = pv;
1125dfc646bSeric 	*pvp++ = m->m_argv[0];
1135dfc646bSeric 
1145dfc646bSeric 	/* insert -f or -r flag as appropriate */
11557fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
1165dfc646bSeric 	{
11757fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
1185dfc646bSeric 			*pvp++ = "-f";
1195dfc646bSeric 		else
1205dfc646bSeric 			*pvp++ = "-r";
121588cad61Seric 		expand("$g", buf, &buf[sizeof buf - 1], e);
1225dfc646bSeric 		*pvp++ = newstr(buf);
1235dfc646bSeric 	}
1245dfc646bSeric 
1255dfc646bSeric 	/*
1265dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1275dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1285dfc646bSeric 	**  be one of these, and there are only a few more slots
1295dfc646bSeric 	**  in the pv after it.
1305dfc646bSeric 	*/
1315dfc646bSeric 
1325dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1335dfc646bSeric 	{
1345dfc646bSeric 		while ((p = index(p, '$')) != NULL)
1355dfc646bSeric 			if (*++p == 'u')
1365dfc646bSeric 				break;
1375dfc646bSeric 		if (p != NULL)
1385dfc646bSeric 			break;
1395dfc646bSeric 
1405dfc646bSeric 		/* this entry is safe -- go ahead and process it */
141588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
1425dfc646bSeric 		*pvp++ = newstr(buf);
1435dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1445dfc646bSeric 		{
1455dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1465dfc646bSeric 			return (-1);
1475dfc646bSeric 		}
1485dfc646bSeric 	}
149c579ef51Seric 
15033db8731Seric 	/*
15133db8731Seric 	**  If we have no substitution for the user name in the argument
15233db8731Seric 	**  list, we know that we must supply the names otherwise -- and
15333db8731Seric 	**  SMTP is the answer!!
15433db8731Seric 	*/
15533db8731Seric 
1565dfc646bSeric 	if (*mvp == NULL)
157c579ef51Seric 	{
158c579ef51Seric 		/* running SMTP */
1592c7e1b8dSeric # ifdef SMTP
160c579ef51Seric 		clever = TRUE;
161c579ef51Seric 		*pvp = NULL;
1622c7e1b8dSeric # else SMTP
16333db8731Seric 		/* oops!  we don't implement SMTP */
1642c7e1b8dSeric 		syserr("SMTP style mailer");
1652c7e1b8dSeric 		return (EX_SOFTWARE);
1662c7e1b8dSeric # endif SMTP
167c579ef51Seric 	}
1685dfc646bSeric 
1695dfc646bSeric 	/*
1705dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
1715dfc646bSeric 	**  run through our address list and append all the addresses
1725dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
1735dfc646bSeric 	**  always send another copy later.
1745dfc646bSeric 	*/
1755dfc646bSeric 
1765dfc646bSeric 	tobuf[0] = '\0';
177588cad61Seric 	e->e_to = tobuf;
1786259796dSeric 	ctladdr = NULL;
1795dfc646bSeric 	for (; to != NULL; to = to->q_next)
1805dfc646bSeric 	{
1815dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
18257fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
1835dfc646bSeric 			break;
1845dfc646bSeric 
1855dfc646bSeric 		/* if already sent or not for this host, don't send */
186da2935e1Seric 		if (bitset(QDONTSEND, to->q_flags) ||
187da2935e1Seric 		    strcmp(to->q_host, host) != 0 ||
188da2935e1Seric 		    to->q_mailer != firstto->q_mailer)
1895dfc646bSeric 			continue;
1906259796dSeric 
1914b22ea87Seric 		/* avoid overflowing tobuf */
192588cad61Seric 		if (sizeof tobuf - (strlen(to->q_paddr) + strlen(tobuf) + 2) < 0)
1934b22ea87Seric 			break;
1944b22ea87Seric 
195772e6e50Seric # ifdef DEBUG
1966ef48975Seric 		if (tTd(10, 1))
197772e6e50Seric 		{
198772e6e50Seric 			printf("\nsend to ");
199772e6e50Seric 			printaddr(to, FALSE);
200772e6e50Seric 		}
201772e6e50Seric # endif DEBUG
202772e6e50Seric 
2036259796dSeric 		/* compute effective uid/gid when sending */
2047da1035fSeric 		if (to->q_mailer == ProgMailer)
2056259796dSeric 			ctladdr = getctladdr(to);
2066259796dSeric 
2075dfc646bSeric 		user = to->q_user;
208588cad61Seric 		e->e_to = to->q_paddr;
2095dfc646bSeric 		to->q_flags |= QDONTSEND;
2105dfc646bSeric 
2115dfc646bSeric 		/*
2125dfc646bSeric 		**  Check to see that these people are allowed to
2135dfc646bSeric 		**  talk to each other.
2142a6e0786Seric 		*/
2152a6e0786Seric 
21669582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
21769582d2fSeric 		{
21869582d2fSeric 			usrerr("Message is too large; %ld bytes max", m->m_maxsize);
21969582d2fSeric 			NoReturn = TRUE;
22069582d2fSeric 			giveresponse(EX_UNAVAILABLE, m, e);
22169582d2fSeric 			continue;
22269582d2fSeric 		}
2232a6e0786Seric 		if (!checkcompat(to))
2245dfc646bSeric 		{
225198d9be0Seric 			giveresponse(EX_UNAVAILABLE, m, e);
2265dfc646bSeric 			continue;
2275dfc646bSeric 		}
2282a6e0786Seric 
2292a6e0786Seric 		/*
2309ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2319ec9501bSeric 		**	about them.
23225a99e2eSeric 		*/
23325a99e2eSeric 
23457fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
23525a99e2eSeric 		{
2369ec9501bSeric 			stripquotes(user, TRUE);
2379ec9501bSeric 			stripquotes(host, TRUE);
2389ec9501bSeric 		}
2399ec9501bSeric 		else
2409ec9501bSeric 		{
2419ec9501bSeric 			stripquotes(user, FALSE);
2429ec9501bSeric 			stripquotes(host, FALSE);
24325a99e2eSeric 		}
24425a99e2eSeric 
245cdb828c5Seric 		/* hack attack -- delivermail compatibility */
246cdb828c5Seric 		if (m == ProgMailer && *user == '|')
247cdb828c5Seric 			user++;
248cdb828c5Seric 
24925a99e2eSeric 		/*
2503efaed6eSeric 		**  If an error message has already been given, don't
2513efaed6eSeric 		**	bother to send to this address.
2523efaed6eSeric 		**
2533efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2543efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2553efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2563efaed6eSeric 		*/
2573efaed6eSeric 
2586cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2593efaed6eSeric 			continue;
2603efaed6eSeric 
261f2fec898Seric 		/* save statistics.... */
262588cad61Seric 		markstats(e, to);
263f2fec898Seric 
2643efaed6eSeric 		/*
26525a99e2eSeric 		**  See if this user name is "special".
26625a99e2eSeric 		**	If the user name has a slash in it, assume that this
26751552439Seric 		**	is a file -- send it off without further ado.  Note
26851552439Seric 		**	that this type of addresses is not processed along
26951552439Seric 		**	with the others, so we fudge on the To person.
27025a99e2eSeric 		*/
27125a99e2eSeric 
2727da1035fSeric 		if (m == LocalMailer)
27325a99e2eSeric 		{
274a49f24c0Seric 			if (user[0] == '/')
27525a99e2eSeric 			{
2765826d9d3Seric 				rcode = mailfile(user, getctladdr(to));
277198d9be0Seric 				giveresponse(rcode, m, e);
2785dfc646bSeric 				continue;
27925a99e2eSeric 			}
28025a99e2eSeric 		}
28125a99e2eSeric 
28213bbc08cSeric 		/*
28313bbc08cSeric 		**  Address is verified -- add this user to mailer
28413bbc08cSeric 		**  argv, and add it to the print list of recipients.
28513bbc08cSeric 		*/
28613bbc08cSeric 
287508daeccSeric 		/* link together the chain of recipients */
288508daeccSeric 		to->q_tchain = tochain;
289508daeccSeric 		tochain = to;
290508daeccSeric 
2915dfc646bSeric 		/* create list of users for error messages */
292db8841e9Seric 		(void) strcat(tobuf, ",");
293db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
294588cad61Seric 		define('u', user, e);		/* to user */
295588cad61Seric 		define('z', to->q_home, e);	/* user's home */
2965dfc646bSeric 
297c579ef51Seric 		/*
298508daeccSeric 		**  Expand out this user into argument list.
299c579ef51Seric 		*/
300c579ef51Seric 
301508daeccSeric 		if (!clever)
302c579ef51Seric 		{
303588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
3045dfc646bSeric 			*pvp++ = newstr(buf);
3055dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3065dfc646bSeric 			{
3075dfc646bSeric 				/* allow some space for trailing parms */
3085dfc646bSeric 				break;
3095dfc646bSeric 			}
3105dfc646bSeric 		}
311c579ef51Seric 	}
3125dfc646bSeric 
313145b49b1Seric 	/* see if any addresses still exist */
314145b49b1Seric 	if (tobuf[0] == '\0')
315c579ef51Seric 	{
316588cad61Seric 		define('g', (char *) NULL, e);
317145b49b1Seric 		return (0);
318c579ef51Seric 	}
319145b49b1Seric 
3205dfc646bSeric 	/* print out messages as full list */
32163780dbdSeric 	e->e_to = tobuf + 1;
3225dfc646bSeric 
3235dfc646bSeric 	/*
3245dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3255dfc646bSeric 	*/
3265dfc646bSeric 
327c579ef51Seric 	while (!clever && *++mvp != NULL)
3285dfc646bSeric 	{
329588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
3305dfc646bSeric 		*pvp++ = newstr(buf);
3315dfc646bSeric 		if (pvp >= &pv[MAXPV])
3325dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
3335dfc646bSeric 	}
3345dfc646bSeric 	*pvp++ = NULL;
3355dfc646bSeric 
33625a99e2eSeric 	/*
33725a99e2eSeric 	**  Call the mailer.
3386328bdf7Seric 	**	The argument vector gets built, pipes
33925a99e2eSeric 	**	are created as necessary, and we fork & exec as
3406328bdf7Seric 	**	appropriate.
341c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
34225a99e2eSeric 	*/
34325a99e2eSeric 
344588cad61Seric 	message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
345588cad61Seric 
3466259796dSeric 	if (ctladdr == NULL)
347588cad61Seric 		ctladdr = &e->e_from;
3482c7e1b8dSeric # ifdef SMTP
349c579ef51Seric 	if (clever)
350c579ef51Seric 	{
351588cad61Seric 		/* send the initial SMTP protocol */
35277b52738Seric 		rcode = smtpinit(m, pv);
353588cad61Seric 
35463780dbdSeric 		if (rcode == EX_OK)
35563780dbdSeric 		{
356588cad61Seric 			/* send the recipient list */
35763780dbdSeric 			tobuf[0] = '\0';
358588cad61Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
359588cad61Seric 			{
360588cad61Seric 				int i;
361588cad61Seric 
36263780dbdSeric 				e->e_to = to->q_paddr;
36377b52738Seric 				i = smtprcpt(to, m);
364588cad61Seric 				if (i != EX_OK)
365588cad61Seric 				{
36683b7ddc9Seric 					markfailure(e, to, i);
367198d9be0Seric 					giveresponse(i, m, e);
36863780dbdSeric 				}
36963780dbdSeric 				else
37063780dbdSeric 				{
37163780dbdSeric 					strcat(tobuf, ",");
37263780dbdSeric 					strcat(tobuf, to->q_paddr);
373588cad61Seric 				}
374588cad61Seric 			}
375588cad61Seric 
37663780dbdSeric 			/* now send the data */
37763780dbdSeric 			if (tobuf[0] == '\0')
37863780dbdSeric 				e->e_to = NULL;
37963780dbdSeric 			else
38063780dbdSeric 			{
38163780dbdSeric 				e->e_to = tobuf + 1;
38277b52738Seric 				rcode = smtpdata(m, e);
38363780dbdSeric 			}
38463780dbdSeric 
38563780dbdSeric 			/* now close the connection */
38677b52738Seric 			smtpquit(pv[0], m);
38763780dbdSeric 		}
388c579ef51Seric 	}
389c579ef51Seric 	else
3902c7e1b8dSeric # endif SMTP
39177b52738Seric 		rcode = sendoff(e, m, pv, ctladdr);
3925dfc646bSeric 
393c77d1c25Seric 	/*
39463780dbdSeric 	**  Do final status disposal.
39563780dbdSeric 	**	We check for something in tobuf for the SMTP case.
396c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
397c77d1c25Seric 	**		addressees.
398c77d1c25Seric 	*/
399c77d1c25Seric 
40063780dbdSeric 	if (tobuf[0] != '\0')
401198d9be0Seric 		giveresponse(rcode, m, e);
40263780dbdSeric 	if (rcode != EX_OK)
403c77d1c25Seric 	{
404772e6e50Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
40583b7ddc9Seric 			markfailure(e, to, rcode);
406c77d1c25Seric 	}
407c77d1c25Seric 
40835490626Seric 	errno = 0;
409588cad61Seric 	define('g', (char *) NULL, e);
4105826d9d3Seric 	return (rcode);
41125a99e2eSeric }
4125dfc646bSeric /*
41383b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
41483b7ddc9Seric **
41583b7ddc9Seric **	Parameters:
41683b7ddc9Seric **		e -- the envelope we are sending.
41783b7ddc9Seric **		q -- the address to mark.
41883b7ddc9Seric **		rcode -- the code signifying the particular failure.
41983b7ddc9Seric **
42083b7ddc9Seric **	Returns:
42183b7ddc9Seric **		none.
42283b7ddc9Seric **
42383b7ddc9Seric **	Side Effects:
42483b7ddc9Seric **		marks the address (and possibly the envelope) with the
42583b7ddc9Seric **			failure so that an error will be returned or
42683b7ddc9Seric **			the message will be queued, as appropriate.
42783b7ddc9Seric */
42883b7ddc9Seric 
42983b7ddc9Seric markfailure(e, q, rcode)
43083b7ddc9Seric 	register ENVELOPE *e;
43183b7ddc9Seric 	register ADDRESS *q;
43283b7ddc9Seric 	int rcode;
43383b7ddc9Seric {
43483b7ddc9Seric 	if (rcode == EX_OK)
43583b7ddc9Seric 		return;
43683b7ddc9Seric 	else if (rcode != EX_TEMPFAIL)
43783b7ddc9Seric 		q->q_flags |= QBADADDR;
43883b7ddc9Seric 	else if (curtime() > e->e_ctime + TimeOut)
43983b7ddc9Seric 	{
44083b7ddc9Seric 		extern char *pintvl();
441198d9be0Seric 		char buf[MAXLINE];
44283b7ddc9Seric 
44383b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
444198d9be0Seric 		{
445198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
44683b7ddc9Seric 				pintvl(TimeOut, FALSE));
447198d9be0Seric 			if (e->e_message != NULL)
448198d9be0Seric 				free(e->e_message);
449198d9be0Seric 			e->e_message = newstr(buf);
450198d9be0Seric 			message(Arpa_Info, buf);
451198d9be0Seric 		}
45283b7ddc9Seric 		q->q_flags |= QBADADDR;
45383b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
45483b7ddc9Seric 	}
45583b7ddc9Seric 	else
45683b7ddc9Seric 		q->q_flags |= QQUEUEUP;
45783b7ddc9Seric }
45883b7ddc9Seric /*
45932d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
46032d19d43Seric **
46132d19d43Seric **	This MUST be a macro, since after a vfork we are running
46232d19d43Seric **	two processes on the same stack!!!
46332d19d43Seric **
46432d19d43Seric **	Parameters:
46532d19d43Seric **		none.
46632d19d43Seric **
46732d19d43Seric **	Returns:
46832d19d43Seric **		From a macro???  You've got to be kidding!
46932d19d43Seric **
47032d19d43Seric **	Side Effects:
47132d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
47232d19d43Seric **			pid of child in parent, zero in child.
47332d19d43Seric **			-1 on unrecoverable error.
47432d19d43Seric **
47532d19d43Seric **	Notes:
47632d19d43Seric **		I'm awfully sorry this looks so awful.  That's
47732d19d43Seric **		vfork for you.....
47832d19d43Seric */
47932d19d43Seric 
48032d19d43Seric # define NFORKTRIES	5
4814300ddf0Seric # ifdef VMUNIX
48232d19d43Seric # define XFORK	vfork
4834300ddf0Seric # else VMUNIX
48432d19d43Seric # define XFORK	fork
4854300ddf0Seric # endif VMUNIX
48632d19d43Seric 
48732d19d43Seric # define DOFORK(fORKfN) \
48832d19d43Seric {\
48932d19d43Seric 	register int i;\
49032d19d43Seric \
49132d19d43Seric 	for (i = NFORKTRIES; i-- > 0; )\
49232d19d43Seric 	{\
49332d19d43Seric 		pid = fORKfN();\
49432d19d43Seric 		if (pid >= 0)\
49532d19d43Seric 			break;\
4965e663df1Seric 		sleep(NFORKTRIES - i);\
49732d19d43Seric 	}\
49832d19d43Seric }
49932d19d43Seric /*
5002ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
5012ed72599Seric **
5022ed72599Seric **	Parameters:
5032ed72599Seric **		none.
5042ed72599Seric **
5052ed72599Seric **	Returns:
5062ed72599Seric **		pid of child in parent.
5072ed72599Seric **		zero in child.
5082ed72599Seric **		-1 on error.
5092ed72599Seric **
5102ed72599Seric **	Side Effects:
5112ed72599Seric **		returns twice, once in parent and once in child.
5122ed72599Seric */
5132ed72599Seric 
5142ed72599Seric dofork()
5152ed72599Seric {
5162ed72599Seric 	register int pid;
5172ed72599Seric 
5182ed72599Seric 	DOFORK(fork);
5192ed72599Seric 	return (pid);
5202ed72599Seric }
5212ed72599Seric /*
5225dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
5235dfc646bSeric **
5245dfc646bSeric **	Parameters:
525588cad61Seric **		e -- the envelope to mail.
5265dfc646bSeric **		m -- mailer descriptor.
5275dfc646bSeric **		pvp -- parameter vector to send to it.
5286259796dSeric **		ctladdr -- an address pointer controlling the
5296259796dSeric **			user/groupid etc. of the mailer.
5305dfc646bSeric **
5315dfc646bSeric **	Returns:
5325dfc646bSeric **		exit status of mailer.
5335dfc646bSeric **
5345dfc646bSeric **	Side Effects:
5355dfc646bSeric **		none.
5365dfc646bSeric */
5375dfc646bSeric 
53877b52738Seric sendoff(e, m, pvp, ctladdr)
539588cad61Seric 	register ENVELOPE *e;
540588cad61Seric 	MAILER *m;
5415dfc646bSeric 	char **pvp;
5426259796dSeric 	ADDRESS *ctladdr;
5435dfc646bSeric {
544c579ef51Seric 	auto FILE *mfile;
545c579ef51Seric 	auto FILE *rfile;
5465dfc646bSeric 	register int i;
547c579ef51Seric 	int pid;
548c579ef51Seric 
549c579ef51Seric 	/*
550c579ef51Seric 	**  Create connection to mailer.
551c579ef51Seric 	*/
552c579ef51Seric 
553c579ef51Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
554c579ef51Seric 	if (pid < 0)
555c579ef51Seric 		return (-1);
556c579ef51Seric 
557c579ef51Seric 	/*
558c579ef51Seric 	**  Format and send message.
559c579ef51Seric 	*/
560c579ef51Seric 
56177b52738Seric 	putfromline(mfile, m);
56277b52738Seric 	(*e->e_puthdr)(mfile, m, e);
56377b52738Seric 	putline("\n", mfile, m);
56477b52738Seric 	(*e->e_putbody)(mfile, m, e);
565c579ef51Seric 	(void) fclose(mfile);
566c579ef51Seric 
567c579ef51Seric 	i = endmailer(pid, pvp[0]);
568bc6e2962Seric 
569bc6e2962Seric 	/* arrange a return receipt if requested */
57057fc6f17Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags))
571bc6e2962Seric 	{
572588cad61Seric 		e->e_flags |= EF_SENDRECEIPT;
573bc6e2962Seric 		/* do we want to send back more info? */
574bc6e2962Seric 	}
575bc6e2962Seric 
576c579ef51Seric 	return (i);
577c579ef51Seric }
578c579ef51Seric /*
579c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
580c579ef51Seric **
581c579ef51Seric **	We should never get fatal errors (e.g., segmentation
582c579ef51Seric **	violation), so we report those specially.  For other
583c579ef51Seric **	errors, we choose a status message (into statmsg),
584c579ef51Seric **	and if it represents an error, we print it.
585c579ef51Seric **
586c579ef51Seric **	Parameters:
587c579ef51Seric **		pid -- pid of mailer.
588c579ef51Seric **		name -- name of mailer (for error messages).
589c579ef51Seric **
590c579ef51Seric **	Returns:
591c579ef51Seric **		exit code of mailer.
592c579ef51Seric **
593c579ef51Seric **	Side Effects:
594c579ef51Seric **		none.
595c579ef51Seric */
596c579ef51Seric 
597c579ef51Seric endmailer(pid, name)
598c579ef51Seric 	int pid;
599c579ef51Seric 	char *name;
600c579ef51Seric {
601588cad61Seric 	int st;
602c579ef51Seric 
60333db8731Seric 	/* in the IPC case there is nothing to wait for */
60433db8731Seric 	if (pid == 0)
60533db8731Seric 		return (EX_OK);
60633db8731Seric 
60733db8731Seric 	/* wait for the mailer process to die and collect status */
608588cad61Seric 	st = waitfor(pid);
609588cad61Seric 	if (st == -1)
61078de67c1Seric 	{
611588cad61Seric 		syserr("endmailer %s: wait", name);
612588cad61Seric 		return (EX_SOFTWARE);
613c579ef51Seric 	}
61433db8731Seric 
61533db8731Seric 	/* see if it died a horrid death */
616c579ef51Seric 	if ((st & 0377) != 0)
617c579ef51Seric 	{
618588cad61Seric 		syserr("endmailer %s: stat %o", name, st);
619c579ef51Seric 		ExitStat = EX_UNAVAILABLE;
620588cad61Seric 		return (EX_UNAVAILABLE);
621c579ef51Seric 	}
62233db8731Seric 
62333db8731Seric 	/* normal death -- return status */
624588cad61Seric 	st = (st >> 8) & 0377;
625588cad61Seric 	return (st);
626c579ef51Seric }
627c579ef51Seric /*
628c579ef51Seric **  OPENMAILER -- open connection to mailer.
629c579ef51Seric **
630c579ef51Seric **	Parameters:
631c579ef51Seric **		m -- mailer descriptor.
632c579ef51Seric **		pvp -- parameter vector to pass to mailer.
633c579ef51Seric **		ctladdr -- controlling address for user.
634c579ef51Seric **		clever -- create a full duplex connection.
635c579ef51Seric **		pmfile -- pointer to mfile (to mailer) connection.
636c579ef51Seric **		prfile -- pointer to rfile (from mailer) connection.
637c579ef51Seric **
638c579ef51Seric **	Returns:
63933db8731Seric **		pid of mailer ( > 0 ).
640c579ef51Seric **		-1 on error.
64133db8731Seric **		zero on an IPC connection.
642c579ef51Seric **
643c579ef51Seric **	Side Effects:
644c579ef51Seric **		creates a mailer in a subprocess.
645c579ef51Seric */
646c579ef51Seric 
647c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
648588cad61Seric 	MAILER *m;
649c579ef51Seric 	char **pvp;
650c579ef51Seric 	ADDRESS *ctladdr;
651c579ef51Seric 	bool clever;
652c579ef51Seric 	FILE **pmfile;
653c579ef51Seric 	FILE **prfile;
654c579ef51Seric {
6555dfc646bSeric 	int pid;
656f8952a83Seric 	int mpvect[2];
657c579ef51Seric 	int rpvect[2];
6585dfc646bSeric 	FILE *mfile;
659c579ef51Seric 	FILE *rfile;
6605dfc646bSeric 	extern FILE *fdopen();
6615dfc646bSeric 
6625dfc646bSeric # ifdef DEBUG
6636ef48975Seric 	if (tTd(11, 1))
6645dfc646bSeric 	{
6658c57e552Seric 		printf("openmailer:");
6665dfc646bSeric 		printav(pvp);
6675dfc646bSeric 	}
6685dfc646bSeric # endif DEBUG
66935490626Seric 	errno = 0;
6705dfc646bSeric 
67133db8731Seric 	/*
67233db8731Seric 	**  Deal with the special case of mail handled through an IPC
67333db8731Seric 	**  connection.
67433db8731Seric 	**	In this case we don't actually fork.  We must be
67533db8731Seric 	**	running SMTP for this to work.  We will return a
67633db8731Seric 	**	zero pid to indicate that we are running IPC.
677e7c1bd78Seric 	**  We also handle a debug version that just talks to stdin/out.
67833db8731Seric 	*/
67933db8731Seric 
680e7c1bd78Seric #ifdef DEBUG
681e7c1bd78Seric 	/* check for Local Person Communication -- not for mortals!!! */
682e7c1bd78Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
683e7c1bd78Seric 	{
684e7c1bd78Seric 		*pmfile = stdout;
685e7c1bd78Seric 		*prfile = stdin;
686e7c1bd78Seric 		return (0);
687e7c1bd78Seric 	}
688e7c1bd78Seric #endif DEBUG
689e7c1bd78Seric 
69033db8731Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
69133db8731Seric 	{
692588cad61Seric #ifdef DAEMON
69333db8731Seric 		register int i;
6941277f9a8Seric 		register u_short port;
69533db8731Seric 
69633db8731Seric 		if (!clever)
69733db8731Seric 			syserr("non-clever IPC");
69893b6e3cfSeric 		if (pvp[2] != NULL)
6991277f9a8Seric 			port = atoi(pvp[2]);
70093b6e3cfSeric 		else
7011277f9a8Seric 			port = 0;
7021277f9a8Seric 		i = makeconnection(pvp[1], port, pmfile, prfile);
70333db8731Seric 		if (i != EX_OK)
704ed854c7bSeric 		{
705ed854c7bSeric 			ExitStat = i;
70633db8731Seric 			return (-1);
707ed854c7bSeric 		}
70833db8731Seric 		else
70933db8731Seric 			return (0);
710588cad61Seric #else DAEMON
711588cad61Seric 		syserr("openmailer: no IPC");
712588cad61Seric 		return (-1);
71333db8731Seric #endif DAEMON
714588cad61Seric 	}
71533db8731Seric 
7166328bdf7Seric 	/* create a pipe to shove the mail through */
717f8952a83Seric 	if (pipe(mpvect) < 0)
71825a99e2eSeric 	{
719588cad61Seric 		syserr("openmailer: pipe (to mailer)");
72025a99e2eSeric 		return (-1);
72125a99e2eSeric 	}
722c579ef51Seric 
7232c7e1b8dSeric #ifdef SMTP
724c579ef51Seric 	/* if this mailer speaks smtp, create a return pipe */
725c579ef51Seric 	if (clever && pipe(rpvect) < 0)
726c579ef51Seric 	{
727588cad61Seric 		syserr("openmailer: pipe (from mailer)");
728c579ef51Seric 		(void) close(mpvect[0]);
729c579ef51Seric 		(void) close(mpvect[1]);
730c579ef51Seric 		return (-1);
731c579ef51Seric 	}
7322c7e1b8dSeric #endif SMTP
733c579ef51Seric 
73433db8731Seric 	/*
73533db8731Seric 	**  Actually fork the mailer process.
73633db8731Seric 	**	DOFORK is clever about retrying.
73733db8731Seric 	*/
73833db8731Seric 
7399a6a5f55Seric 	if (CurEnv->e_xfp != NULL)
7409a6a5f55Seric 		(void) fflush(CurEnv->e_xfp);		/* for debugging */
741588cad61Seric 	(void) fflush(stdout);
74232d19d43Seric 	DOFORK(XFORK);
743f129ec7dSeric 	/* pid is set by DOFORK */
74425a99e2eSeric 	if (pid < 0)
74525a99e2eSeric 	{
74633db8731Seric 		/* failure */
747588cad61Seric 		syserr("openmailer: cannot fork");
748f8952a83Seric 		(void) close(mpvect[0]);
749f8952a83Seric 		(void) close(mpvect[1]);
750588cad61Seric #ifdef SMTP
751c579ef51Seric 		if (clever)
752c579ef51Seric 		{
753c579ef51Seric 			(void) close(rpvect[0]);
754c579ef51Seric 			(void) close(rpvect[1]);
755c579ef51Seric 		}
756588cad61Seric #endif SMTP
75725a99e2eSeric 		return (-1);
75825a99e2eSeric 	}
75925a99e2eSeric 	else if (pid == 0)
76025a99e2eSeric 	{
76125a99e2eSeric 		/* child -- set up input & exec mailer */
76203ab8e55Seric 		/* make diagnostic output be standard output */
7638f0e7860Seric 		(void) signal(SIGINT, SIG_IGN);
7648f0e7860Seric 		(void) signal(SIGHUP, SIG_IGN);
7650984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
766f8952a83Seric 
767f8952a83Seric 		/* arrange to filter standard & diag output of command */
768c579ef51Seric 		if (clever)
769c579ef51Seric 		{
770c579ef51Seric 			(void) close(rpvect[0]);
771c579ef51Seric 			(void) close(1);
772c579ef51Seric 			(void) dup(rpvect[1]);
773c579ef51Seric 			(void) close(rpvect[1]);
774c579ef51Seric 		}
775276723a8Seric 		else if (OpMode == MD_SMTP || HoldErrs)
776f8952a83Seric 		{
777588cad61Seric 			/* put mailer output in transcript */
778f8952a83Seric 			(void) close(1);
7799a6a5f55Seric 			(void) dup(fileno(CurEnv->e_xfp));
780f8952a83Seric 		}
781db8841e9Seric 		(void) close(2);
782db8841e9Seric 		(void) dup(1);
783f8952a83Seric 
784f8952a83Seric 		/* arrange to get standard input */
785f8952a83Seric 		(void) close(mpvect[1]);
786db8841e9Seric 		(void) close(0);
787f8952a83Seric 		if (dup(mpvect[0]) < 0)
78825a99e2eSeric 		{
78925a99e2eSeric 			syserr("Cannot dup to zero!");
790a590b978Seric 			_exit(EX_OSERR);
79125a99e2eSeric 		}
792f8952a83Seric 		(void) close(mpvect[0]);
79357fc6f17Seric 		if (!bitnset(M_RESTR, m->m_flags))
7940984da9fSeric 		{
795e36b99e2Seric 			if (ctladdr->q_uid == 0)
796e36b99e2Seric 			{
797e36b99e2Seric 				(void) setgid(DefGid);
798e36b99e2Seric 				(void) setuid(DefUid);
799e36b99e2Seric 			}
800e36b99e2Seric 			else
80169f29479Seric 			{
802e36b99e2Seric 				(void) setgid(ctladdr->q_gid);
803e36b99e2Seric 				(void) setuid(ctladdr->q_uid);
80469f29479Seric 			}
8050984da9fSeric 		}
806588cad61Seric 
807e374fd72Seric 		/*
808e374fd72Seric 		**  We have to be careful with vfork - we can't mung up the
809e374fd72Seric 		**  memory but we don't want the mailer to inherit any extra
810e374fd72Seric 		**  open files.  Chances are the mailer won't
811e374fd72Seric 		**  care about an extra file, but then again you never know.
812e374fd72Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
813e374fd72Seric 		**  declared static so we can't.  But if we fclose(pwf), which
814e374fd72Seric 		**  is what endpwent does, it closes it in the parent too and
815e374fd72Seric 		**  the next getpwnam will be slower.  If you have a weird
816e374fd72Seric 		**  mailer that chokes on the extra file you should do the
8174300ddf0Seric 		**  endpwent().			-MRH
818e374fd72Seric 		**
819e374fd72Seric 		**  Similar comments apply to log.  However, openlog is
820e374fd72Seric 		**  clever enough to set the FIOCLEX mode on the file,
821e374fd72Seric 		**  so it will be closed automatically on the exec.
822e374fd72Seric 		*/
823e374fd72Seric 
824588cad61Seric 		closeall();
82533db8731Seric 
82633db8731Seric 		/* try to execute the mailer */
82725a99e2eSeric 		execv(m->m_mailer, pvp);
82833db8731Seric 
82925a99e2eSeric 		/* syserr fails because log is closed */
83025a99e2eSeric 		/* syserr("Cannot exec %s", m->m_mailer); */
83132d19d43Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
832db8841e9Seric 		(void) fflush(stdout);
833a590b978Seric 		_exit(EX_UNAVAILABLE);
83425a99e2eSeric 	}
83525a99e2eSeric 
836f8952a83Seric 	/*
837c579ef51Seric 	**  Set up return value.
838f8952a83Seric 	*/
839f8952a83Seric 
840f8952a83Seric 	(void) close(mpvect[0]);
841f8952a83Seric 	mfile = fdopen(mpvect[1], "w");
842c579ef51Seric 	if (clever)
84325a99e2eSeric 	{
844c579ef51Seric 		(void) close(rpvect[1]);
845c579ef51Seric 		rfile = fdopen(rpvect[0], "r");
84625a99e2eSeric 	}
847c579ef51Seric 
848c579ef51Seric 	*pmfile = mfile;
849c579ef51Seric 	*prfile = rfile;
850c579ef51Seric 
851c579ef51Seric 	return (pid);
85225a99e2eSeric }
85325a99e2eSeric /*
85425a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
85525a99e2eSeric **
85625a99e2eSeric **	Parameters:
85725a99e2eSeric **		stat -- the status code from the mailer (high byte
85825a99e2eSeric **			only; core dumps must have been taken care of
85925a99e2eSeric **			already).
86025a99e2eSeric **		m -- the mailer descriptor for this mailer.
86125a99e2eSeric **
86225a99e2eSeric **	Returns:
863db8841e9Seric **		none.
86425a99e2eSeric **
86525a99e2eSeric **	Side Effects:
866c1f9df2cSeric **		Errors may be incremented.
86725a99e2eSeric **		ExitStat may be set.
86825a99e2eSeric */
86925a99e2eSeric 
870198d9be0Seric giveresponse(stat, m, e)
87125a99e2eSeric 	int stat;
872588cad61Seric 	register MAILER *m;
873198d9be0Seric 	ENVELOPE *e;
87425a99e2eSeric {
87525a99e2eSeric 	register char *statmsg;
87625a99e2eSeric 	extern char *SysExMsg[];
87725a99e2eSeric 	register int i;
87825a99e2eSeric 	extern int N_SysEx;
879198d9be0Seric 	char buf[MAXLINE];
88025a99e2eSeric 
8817d1fc79dSeric #ifdef lint
8827d1fc79dSeric 	if (m == NULL)
8837d1fc79dSeric 		return;
8847d1fc79dSeric #endif lint
8857d1fc79dSeric 
88613bbc08cSeric 	/*
88713bbc08cSeric 	**  Compute status message from code.
88813bbc08cSeric 	*/
88913bbc08cSeric 
89025a99e2eSeric 	i = stat - EX__BASE;
891588cad61Seric 	if (stat == 0)
892588cad61Seric 		statmsg = "250 Sent";
893588cad61Seric 	else if (i < 0 || i > N_SysEx)
894588cad61Seric 	{
895588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
896588cad61Seric 		stat = EX_UNAVAILABLE;
897588cad61Seric 		statmsg = buf;
898588cad61Seric 	}
899198d9be0Seric 	else if (stat == EX_TEMPFAIL)
900198d9be0Seric 	{
9018557d168Seric 		(void) strcpy(buf, SysExMsg[i]);
9028557d168Seric 		if (errno != 0)
903198d9be0Seric 		{
904*87c9b3e7Seric 			extern char *errstring();
9058557d168Seric 
906*87c9b3e7Seric 			(void) strcat(buf, ": ");
907*87c9b3e7Seric 			(void) strcat(buf, errstring(errno));
9088557d168Seric 		}
909198d9be0Seric 		statmsg = buf;
910198d9be0Seric 	}
91125a99e2eSeric 	else
91225a99e2eSeric 		statmsg = SysExMsg[i];
913588cad61Seric 
914588cad61Seric 	/*
915588cad61Seric 	**  Print the message as appropriate
916588cad61Seric 	*/
917588cad61Seric 
918198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
9195826d9d3Seric 		message(Arpa_Info, &statmsg[4]);
92025a99e2eSeric 	else
92125a99e2eSeric 	{
922c1f9df2cSeric 		Errors++;
9235826d9d3Seric 		usrerr(statmsg);
92425a99e2eSeric 	}
92525a99e2eSeric 
92625a99e2eSeric 	/*
92725a99e2eSeric 	**  Final cleanup.
92825a99e2eSeric 	**	Log a record of the transaction.  Compute the new
92925a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
93025a99e2eSeric 	**	that.
93125a99e2eSeric 	*/
93225a99e2eSeric 
93361f5a1d4Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
934eb238f8cSeric 		logdelivery(&statmsg[4]);
935eb238f8cSeric 
936eb238f8cSeric 	if (stat != EX_TEMPFAIL)
937eb238f8cSeric 		setstat(stat);
938198d9be0Seric 	if (stat != EX_OK)
939198d9be0Seric 	{
940198d9be0Seric 		if (e->e_message != NULL)
941198d9be0Seric 			free(e->e_message);
942198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
943198d9be0Seric 	}
9448557d168Seric 	errno = 0;
945eb238f8cSeric }
946eb238f8cSeric /*
947eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
948eb238f8cSeric **
949eb238f8cSeric **	Parameters:
950eb238f8cSeric **		stat -- the message to print for the status
951eb238f8cSeric **
952eb238f8cSeric **	Returns:
953eb238f8cSeric **		none
954eb238f8cSeric **
955eb238f8cSeric **	Side Effects:
956eb238f8cSeric **		none
957eb238f8cSeric */
958eb238f8cSeric 
959eb238f8cSeric logdelivery(stat)
960eb238f8cSeric 	char *stat;
9615cf56be3Seric {
9625cf56be3Seric 	extern char *pintvl();
9635cf56be3Seric 
964eb238f8cSeric # ifdef LOG
9655cf56be3Seric 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id,
966eb238f8cSeric 	       CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat);
96725a99e2eSeric # endif LOG
96825a99e2eSeric }
96925a99e2eSeric /*
97051552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
97125a99e2eSeric **
97251552439Seric **	This can be made an arbitrary message separator by changing $l
97351552439Seric **
97451552439Seric **	One of the ugliest hacks seen by human eyes is
97551552439Seric **	contained herein: UUCP wants those stupid
976588cad61Seric **	"emote from <host>" lines.  Why oh why does a
97751552439Seric **	well-meaning programmer such as myself have to
97851552439Seric **	deal with this kind of antique garbage????
97925a99e2eSeric **
98025a99e2eSeric **	Parameters:
98151552439Seric **		fp -- the file to output to.
98251552439Seric **		m -- the mailer describing this entry.
98325a99e2eSeric **
98425a99e2eSeric **	Returns:
98551552439Seric **		none
98625a99e2eSeric **
98725a99e2eSeric **	Side Effects:
98851552439Seric **		outputs some text to fp.
98925a99e2eSeric */
99025a99e2eSeric 
99177b52738Seric putfromline(fp, m)
99251552439Seric 	register FILE *fp;
99351552439Seric 	register MAILER *m;
99425a99e2eSeric {
995ea09d6edSeric 	char *template = "$l\n";
99651552439Seric 	char buf[MAXLINE];
99725a99e2eSeric 
99857fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
99951552439Seric 		return;
100013bbc08cSeric 
10012c7e1b8dSeric # ifdef UGLYUUCP
100257fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
100374b6e67bSeric 	{
1004ea09d6edSeric 		char *bang;
1005ea09d6edSeric 		char xbuf[MAXLINE];
100674b6e67bSeric 
1007ea09d6edSeric 		expand("$g", buf, &buf[sizeof buf - 1], CurEnv);
1008ea09d6edSeric 		bang = index(buf, '!');
100974b6e67bSeric 		if (bang == NULL)
1010ea09d6edSeric 			syserr("No ! in UUCP! (%s)", buf);
101174b6e67bSeric 		else
1012588cad61Seric 		{
1013ea09d6edSeric 			*bang++ = '\0';
1014d0caca29Seric 			(void) sprintf(xbuf, "From %s  $d remote from %s\n", bang, buf);
1015ea09d6edSeric 			template = xbuf;
101674b6e67bSeric 		}
1017588cad61Seric 	}
10182c7e1b8dSeric # endif UGLYUUCP
1019ea09d6edSeric 	expand(template, buf, &buf[sizeof buf - 1], CurEnv);
102077b52738Seric 	putline(buf, fp, m);
1021bc6e2962Seric }
1022bc6e2962Seric /*
102351552439Seric **  PUTBODY -- put the body of a message.
102451552439Seric **
102551552439Seric **	Parameters:
102651552439Seric **		fp -- file to output onto.
102777b52738Seric **		m -- a mailer descriptor to control output format.
10289a6a5f55Seric **		e -- the envelope to put out.
102951552439Seric **
103051552439Seric **	Returns:
103151552439Seric **		none.
103251552439Seric **
103351552439Seric **	Side Effects:
103451552439Seric **		The message is written onto fp.
103551552439Seric */
103651552439Seric 
103777b52738Seric putbody(fp, m, e)
103851552439Seric 	FILE *fp;
1039588cad61Seric 	MAILER *m;
10409a6a5f55Seric 	register ENVELOPE *e;
104151552439Seric {
104277b52738Seric 	char buf[MAXLINE];
104351552439Seric 
104451552439Seric 	/*
104551552439Seric 	**  Output the body of the message
104651552439Seric 	*/
104751552439Seric 
10489a6a5f55Seric 	if (e->e_dfp == NULL)
104951552439Seric 	{
10509a6a5f55Seric 		if (e->e_df != NULL)
10519a6a5f55Seric 		{
10529a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
10539a6a5f55Seric 			if (e->e_dfp == NULL)
10549a6a5f55Seric 				syserr("Cannot open %s", e->e_df);
10559a6a5f55Seric 		}
10569a6a5f55Seric 		else
105777b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
10589a6a5f55Seric 	}
10599a6a5f55Seric 	if (e->e_dfp != NULL)
10609a6a5f55Seric 	{
10619a6a5f55Seric 		rewind(e->e_dfp);
106277b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
106377b52738Seric 			putline(buf, fp, m);
106451552439Seric 
10659a6a5f55Seric 		if (ferror(e->e_dfp))
106651552439Seric 		{
106751552439Seric 			syserr("putbody: read error");
106851552439Seric 			ExitStat = EX_IOERR;
106951552439Seric 		}
107051552439Seric 	}
107151552439Seric 
107251552439Seric 	(void) fflush(fp);
107351552439Seric 	if (ferror(fp) && errno != EPIPE)
107451552439Seric 	{
107551552439Seric 		syserr("putbody: write error");
107651552439Seric 		ExitStat = EX_IOERR;
107751552439Seric 	}
107851552439Seric 	errno = 0;
107925a99e2eSeric }
108025a99e2eSeric /*
108125a99e2eSeric **  MAILFILE -- Send a message to a file.
108225a99e2eSeric **
1083f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1084f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1085f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1086f129ec7dSeric **	sendmail runs as root.
1087f129ec7dSeric **
1088588cad61Seric **	This could be done as a subordinate mailer, except that it
1089588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1090588cad61Seric **	view this as being sufficiently important as to include it
1091588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1092588cad61Seric **	to create another process plus some pipes to save the message.
1093588cad61Seric **
109425a99e2eSeric **	Parameters:
109525a99e2eSeric **		filename -- the name of the file to send to.
10966259796dSeric **		ctladdr -- the controlling address header -- includes
10976259796dSeric **			the userid/groupid to be when sending.
109825a99e2eSeric **
109925a99e2eSeric **	Returns:
110025a99e2eSeric **		The exit code associated with the operation.
110125a99e2eSeric **
110225a99e2eSeric **	Side Effects:
110325a99e2eSeric **		none.
110425a99e2eSeric */
110525a99e2eSeric 
11066259796dSeric mailfile(filename, ctladdr)
110725a99e2eSeric 	char *filename;
11086259796dSeric 	ADDRESS *ctladdr;
110925a99e2eSeric {
111025a99e2eSeric 	register FILE *f;
111132d19d43Seric 	register int pid;
111225a99e2eSeric 
111332d19d43Seric 	/*
111432d19d43Seric 	**  Fork so we can change permissions here.
111532d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
111632d19d43Seric 	**	the complications of calling subroutines, etc.
111732d19d43Seric 	*/
111832d19d43Seric 
111932d19d43Seric 	DOFORK(fork);
112032d19d43Seric 
112132d19d43Seric 	if (pid < 0)
112232d19d43Seric 		return (EX_OSERR);
112332d19d43Seric 	else if (pid == 0)
112432d19d43Seric 	{
112532d19d43Seric 		/* child -- actually write to file */
1126f129ec7dSeric 		struct stat stb;
1127f129ec7dSeric 
11280984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
11290984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
11300984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
1131f129ec7dSeric 		umask(OldUmask);
1132f129ec7dSeric 		if (stat(filename, &stb) < 0)
113324447f54Seric 		{
113424447f54Seric 			errno = 0;
1135e6e1265fSeric 			stb.st_mode = 0666;
113624447f54Seric 		}
1137f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1138f129ec7dSeric 			exit(EX_CANTCREAT);
113903827b5fSeric 		if (ctladdr == NULL)
11407a941e7aSeric 			ctladdr = &CurEnv->e_from;
1141f129ec7dSeric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
1142e36b99e2Seric 		{
1143e36b99e2Seric 			if (ctladdr->q_uid == 0)
1144e36b99e2Seric 				(void) setgid(DefGid);
1145e36b99e2Seric 			else
11466259796dSeric 				(void) setgid(ctladdr->q_gid);
1147e36b99e2Seric 		}
1148f129ec7dSeric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
1149e36b99e2Seric 		{
1150e36b99e2Seric 			if (ctladdr->q_uid == 0)
1151e36b99e2Seric 				(void) setuid(DefUid);
1152e36b99e2Seric 			else
11536259796dSeric 				(void) setuid(ctladdr->q_uid);
1154e36b99e2Seric 		}
115527628d59Seric 		f = dfopen(filename, "a");
115625a99e2eSeric 		if (f == NULL)
115732d19d43Seric 			exit(EX_CANTCREAT);
115825a99e2eSeric 
115977b52738Seric 		putfromline(f, ProgMailer);
116077b52738Seric 		(*CurEnv->e_puthdr)(f, ProgMailer, CurEnv);
116177b52738Seric 		putline("\n", f, ProgMailer);
116277b52738Seric 		(*CurEnv->e_putbody)(f, ProgMailer, CurEnv);
116377b52738Seric 		putline("\n", f, ProgMailer);
1164db8841e9Seric 		(void) fclose(f);
116532d19d43Seric 		(void) fflush(stdout);
1166e36b99e2Seric 
116727628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1168c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
116932d19d43Seric 		exit(EX_OK);
117013bbc08cSeric 		/*NOTREACHED*/
117132d19d43Seric 	}
117232d19d43Seric 	else
117332d19d43Seric 	{
117432d19d43Seric 		/* parent -- wait for exit status */
1175588cad61Seric 		int st;
117632d19d43Seric 
1177588cad61Seric 		st = waitfor(pid);
1178588cad61Seric 		if ((st & 0377) != 0)
1179588cad61Seric 			return (EX_UNAVAILABLE);
1180588cad61Seric 		else
1181588cad61Seric 			return ((st >> 8) & 0377);
118232d19d43Seric 	}
118325a99e2eSeric }
1184ea4dc939Seric /*
1185ea4dc939Seric **  SENDALL -- actually send all the messages.
1186ea4dc939Seric **
1187ea4dc939Seric **	Parameters:
11880c52a0b3Seric **		e -- the envelope to send.
11897b95031aSeric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
11907b95031aSeric **			the current SendMode.
1191ea4dc939Seric **
1192ea4dc939Seric **	Returns:
1193ea4dc939Seric **		none.
1194ea4dc939Seric **
1195ea4dc939Seric **	Side Effects:
1196ea4dc939Seric **		Scans the send lists and sends everything it finds.
11970c52a0b3Seric **		Delivers any appropriate error messages.
1198276723a8Seric **		If we are running in a non-interactive mode, takes the
1199276723a8Seric **			appropriate action.
1200ea4dc939Seric */
1201ea4dc939Seric 
1202276723a8Seric sendall(e, mode)
12030c52a0b3Seric 	ENVELOPE *e;
1204276723a8Seric 	char mode;
1205ea4dc939Seric {
1206e77e673fSeric 	register ADDRESS *q;
120714a8ed7aSeric 	bool oldverbose;
1208276723a8Seric 	int pid;
1209ea4dc939Seric 
12107b95031aSeric 	/* determine actual delivery mode */
12117b95031aSeric 	if (mode == SM_DEFAULT)
12127b95031aSeric 	{
12137b95031aSeric 		extern int QueueLA;
12147b95031aSeric 
12157b95031aSeric 		if (getla() > QueueLA)
12167b95031aSeric 			mode = SM_QUEUE;
12177b95031aSeric 		else
12187b95031aSeric 			mode = SendMode;
12197b95031aSeric 	}
12207b95031aSeric 
1221772e6e50Seric #ifdef DEBUG
1222df864a8fSeric 	if (tTd(13, 1))
1223772e6e50Seric 	{
1224276723a8Seric 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
12250c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1226772e6e50Seric 	}
1227772e6e50Seric #endif DEBUG
1228ea4dc939Seric 
12290c52a0b3Seric 	/*
1230276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1231588cad61Seric 	**	Check to make sure the hop count is reasonable.
1232588cad61Seric 	**	Delete sends to the sender in mailing lists.
1233276723a8Seric 	*/
1234276723a8Seric 
1235588cad61Seric 	CurEnv = e;
1236276723a8Seric 
1237588cad61Seric 	if (e->e_hopcount > MAXHOP)
1238276723a8Seric 	{
1239588cad61Seric 		syserr("sendall: too many hops (%d max)", MAXHOP);
1240588cad61Seric 		return;
1241588cad61Seric 	}
1242588cad61Seric 
1243588cad61Seric 	if (!MeToo)
1244276723a8Seric 	{
1245f3d6c55cSeric 		extern ADDRESS *recipient();
1246f3d6c55cSeric 
1247588cad61Seric 		e->e_from.q_flags |= QDONTSEND;
1248f3d6c55cSeric 		(void) recipient(&e->e_from, &e->e_sendqueue);
1249276723a8Seric 	}
1250588cad61Seric 
1251588cad61Seric # ifdef QUEUE
1252b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1253b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1254b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
1255588cad61Seric 		queueup(e, TRUE, mode == SM_QUEUE);
1256276723a8Seric #endif QUEUE
1257276723a8Seric 
1258276723a8Seric 	oldverbose = Verbose;
1259276723a8Seric 	switch (mode)
1260276723a8Seric 	{
1261276723a8Seric 	  case SM_VERIFY:
1262276723a8Seric 		Verbose = TRUE;
1263276723a8Seric 		break;
1264276723a8Seric 
1265276723a8Seric 	  case SM_QUEUE:
1266b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1267276723a8Seric 		return;
1268276723a8Seric 
1269276723a8Seric 	  case SM_FORK:
12709a6a5f55Seric 		if (e->e_xfp != NULL)
12719a6a5f55Seric 			(void) fflush(e->e_xfp);
1272276723a8Seric 		pid = fork();
1273276723a8Seric 		if (pid < 0)
1274276723a8Seric 		{
1275276723a8Seric 			mode = SM_DELIVER;
1276276723a8Seric 			break;
1277276723a8Seric 		}
1278276723a8Seric 		else if (pid > 0)
1279a6fce3d8Seric 		{
1280a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1281b254bcb6Seric 			e->e_id = e->e_df = NULL;
1282276723a8Seric 			return;
1283a6fce3d8Seric 		}
1284276723a8Seric 
1285276723a8Seric 		/* double fork to avoid zombies */
1286276723a8Seric 		if (fork() > 0)
1287276723a8Seric 			exit(EX_OK);
1288276723a8Seric 
1289a6fce3d8Seric 		/* be sure we are immune from the terminal */
1290769e215aSeric 		disconnect(FALSE);
1291a6fce3d8Seric 
1292276723a8Seric 		break;
1293276723a8Seric 	}
1294276723a8Seric 
1295276723a8Seric 	/*
12960c52a0b3Seric 	**  Run through the list and send everything.
12970c52a0b3Seric 	*/
12980c52a0b3Seric 
12990c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1300ea4dc939Seric 	{
1301276723a8Seric 		if (mode == SM_VERIFY)
1302ea4dc939Seric 		{
1303a6fce3d8Seric 			e->e_to = q->q_paddr;
1304e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1305ea4dc939Seric 				message(Arpa_Info, "deliverable");
1306ea4dc939Seric 		}
1307ea4dc939Seric 		else
1308588cad61Seric 			(void) deliver(e, q);
1309ea4dc939Seric 	}
131014a8ed7aSeric 	Verbose = oldverbose;
13110c52a0b3Seric 
13120c52a0b3Seric 	/*
13130c52a0b3Seric 	**  Now run through and check for errors.
13140c52a0b3Seric 	*/
13150c52a0b3Seric 
1316276723a8Seric 	if (mode == SM_VERIFY)
13170c52a0b3Seric 		return;
13180c52a0b3Seric 
13190c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
13200c52a0b3Seric 	{
13210c52a0b3Seric 		register ADDRESS *qq;
13220c52a0b3Seric 
1323df864a8fSeric # ifdef DEBUG
1324df864a8fSeric 		if (tTd(13, 3))
1325df864a8fSeric 		{
1326df864a8fSeric 			printf("Checking ");
1327df864a8fSeric 			printaddr(q, FALSE);
1328df864a8fSeric 		}
1329df864a8fSeric # endif DEBUG
1330df864a8fSeric 
1331b254bcb6Seric 		/* only send errors if the message failed */
1332b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1333b254bcb6Seric 			continue;
13340c52a0b3Seric 
13350c52a0b3Seric 		/* we have an address that failed -- find the parent */
13360c52a0b3Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
13370c52a0b3Seric 		{
13380c52a0b3Seric 			char obuf[MAXNAME + 6];
13390c52a0b3Seric 			extern char *aliaslookup();
13400c52a0b3Seric 
13410c52a0b3Seric 			/* we can only have owners for local addresses */
134257fc6f17Seric 			if (!bitnset(M_LOCAL, qq->q_mailer->m_flags))
13430c52a0b3Seric 				continue;
13440c52a0b3Seric 
13450c52a0b3Seric 			/* see if the owner list exists */
13460c52a0b3Seric 			(void) strcpy(obuf, "owner-");
1347cec031e3Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1348cec031e3Seric 				(void) strcat(obuf, "owner");
1349cec031e3Seric 			else
13500c52a0b3Seric 				(void) strcat(obuf, qq->q_user);
13510c52a0b3Seric 			if (aliaslookup(obuf) == NULL)
13520c52a0b3Seric 				continue;
13530c52a0b3Seric 
1354df864a8fSeric # ifdef DEBUG
1355df864a8fSeric 			if (tTd(13, 4))
1356df864a8fSeric 				printf("Errors to %s\n", obuf);
1357df864a8fSeric # endif DEBUG
1358df864a8fSeric 
13590c52a0b3Seric 			/* owner list exists -- add it to the error queue */
1360e3e4ed86Seric 			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue);
1361588cad61Seric 			ErrorMode == EM_MAIL;
13620c52a0b3Seric 			break;
13630c52a0b3Seric 		}
13640c52a0b3Seric 
13650c52a0b3Seric 		/* if we did not find an owner, send to the sender */
13667455aa0bSeric 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1367e3e4ed86Seric 			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue);
13680c52a0b3Seric 	}
1369276723a8Seric 
1370276723a8Seric 	if (mode == SM_FORK)
1371276723a8Seric 		finis();
13720c52a0b3Seric }
1373