14227346bSdist /*
24227346bSdist **  Sendmail
34227346bSdist **  Copyright (c) 1983  Eric P. Allman
44227346bSdist **  Berkeley, California
54227346bSdist **
64227346bSdist **  Copyright (c) 1983 Regents of the University of California.
74227346bSdist **  All rights reserved.  The Berkeley software License Agreement
84227346bSdist **  specifies the terms and conditions for redistribution.
94227346bSdist */
104227346bSdist 
114227346bSdist #ifndef lint
12*3462ad9eSeric static char	SccsId[] = "@(#)deliver.c	5.3 (Berkeley) 06/08/85";
134227346bSdist #endif not lint
144227346bSdist 
1525a99e2eSeric # include <signal.h>
1654aa2b0fSeric # include <errno.h>
17b20b3270Seric # include "sendmail.h"
18c77d1c25Seric # include <sys/stat.h>
1925a99e2eSeric 
2025a99e2eSeric /*
2113bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
2213bbc08cSeric **
2313bbc08cSeric **	This routine delivers to everyone on the same host as the
2413bbc08cSeric **	user on the head of the list.  It is clever about mailers
2513bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
2613bbc08cSeric **	that it will deliver to all these addresses however -- so
2713bbc08cSeric **	deliver should be called once for each address on the
2813bbc08cSeric **	list.
2925a99e2eSeric **
3025a99e2eSeric **	Parameters:
31588cad61Seric **		e -- the envelope to deliver.
32c77d1c25Seric **		firstto -- head of the address list to deliver to.
3325a99e2eSeric **
3425a99e2eSeric **	Returns:
3525a99e2eSeric **		zero -- successfully delivered.
3625a99e2eSeric **		else -- some failure, see ExitStat for more info.
3725a99e2eSeric **
3825a99e2eSeric **	Side Effects:
3925a99e2eSeric **		The standard input is passed off to someone.
4025a99e2eSeric */
4125a99e2eSeric 
42588cad61Seric deliver(e, firstto)
43588cad61Seric 	register ENVELOPE *e;
44c77d1c25Seric 	ADDRESS *firstto;
4525a99e2eSeric {
4678442df3Seric 	char *host;			/* host being sent to */
4778442df3Seric 	char *user;			/* user being sent to */
4825a99e2eSeric 	char **pvp;
495dfc646bSeric 	register char **mvp;
5025a99e2eSeric 	register char *p;
51588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
526259796dSeric 	ADDRESS *ctladdr;
53c77d1c25Seric 	register ADDRESS *to = firstto;
54c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
55772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
565826d9d3Seric 	register int rcode;		/* response code */
57ee6bf8dfSeric 	char *pv[MAXPV+1];
58ee6bf8dfSeric 	char tobuf[MAXLINE-50];		/* text line of to people */
59ee6bf8dfSeric 	char buf[MAXNAME];
60ee6bf8dfSeric 	char tfrombuf[MAXNAME];		/* translated from person */
61ee6bf8dfSeric 	extern bool checkcompat();
62ee6bf8dfSeric 	extern ADDRESS *getctladdr();
63ee6bf8dfSeric 	extern char *remotename();
6425a99e2eSeric 
6535490626Seric 	errno = 0;
66da2935e1Seric 	if (bitset(QDONTSEND, to->q_flags))
675dfc646bSeric 		return (0);
6825a99e2eSeric 
6951552439Seric 	m = to->q_mailer;
7051552439Seric 	host = to->q_host;
7151552439Seric 
7225a99e2eSeric # ifdef DEBUG
736ef48975Seric 	if (tTd(10, 1))
745dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
7551552439Seric 			m->m_mno, host, to->q_user);
7625a99e2eSeric # endif DEBUG
77f3dbc832Seric 
78f3dbc832Seric 	/*
79f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
80f3dbc832Seric 	**  connections now, just mark these addresses and return.
81f3dbc832Seric 	**	This is useful if we want to batch connections to
82f3dbc832Seric 	**	reduce load.  This will cause the messages to be
83f3dbc832Seric 	**	queued up, and a daemon will come along to send the
84f3dbc832Seric 	**	messages later.
85f3dbc832Seric 	**		This should be on a per-mailer basis.
86f3dbc832Seric 	*/
87f3dbc832Seric 
8857fc6f17Seric 	if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) &&
89317680d6Seric 	    !Verbose)
90f3dbc832Seric 	{
91f3dbc832Seric 		for (; to != NULL; to = to->q_next)
92f4560e80Seric 		{
93f4560e80Seric 			if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m)
94f4560e80Seric 				continue;
95f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
96588cad61Seric 			e->e_to = to->q_paddr;
97eb238f8cSeric 			message(Arpa_Info, "queued");
98eb238f8cSeric 			if (LogLevel > 4)
99eb238f8cSeric 				logdelivery("queued");
100f4560e80Seric 		}
101588cad61Seric 		e->e_to = NULL;
102f3dbc832Seric 		return (0);
103f3dbc832Seric 	}
104f3dbc832Seric 
10525a99e2eSeric 	/*
1065dfc646bSeric 	**  Do initial argv setup.
1075dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
1085dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
1095dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
1105dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
1115dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
1123bea8136Seric 	**		The from address rewrite is expected to make
1133bea8136Seric 	**		the address relative to the other end.
1145dfc646bSeric 	*/
1155dfc646bSeric 
11678442df3Seric 	/* rewrite from address, using rewriting rules */
1179b6c17a6Seric 	expand("\001f", buf, &buf[sizeof buf - 1], e);
118ee6bf8dfSeric 	(void) strcpy(tfrombuf, remotename(buf, m, TRUE, TRUE));
11978442df3Seric 
120588cad61Seric 	define('g', tfrombuf, e);		/* translated sender address */
121588cad61Seric 	define('h', host, e);			/* to host */
1225dfc646bSeric 	Errors = 0;
1235dfc646bSeric 	pvp = pv;
1245dfc646bSeric 	*pvp++ = m->m_argv[0];
1255dfc646bSeric 
1265dfc646bSeric 	/* insert -f or -r flag as appropriate */
12757fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
1285dfc646bSeric 	{
12957fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
1305dfc646bSeric 			*pvp++ = "-f";
1315dfc646bSeric 		else
1325dfc646bSeric 			*pvp++ = "-r";
1339b6c17a6Seric 		expand("\001g", buf, &buf[sizeof buf - 1], e);
1345dfc646bSeric 		*pvp++ = newstr(buf);
1355dfc646bSeric 	}
1365dfc646bSeric 
1375dfc646bSeric 	/*
1385dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1395dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1405dfc646bSeric 	**  be one of these, and there are only a few more slots
1415dfc646bSeric 	**  in the pv after it.
1425dfc646bSeric 	*/
1435dfc646bSeric 
1445dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1455dfc646bSeric 	{
1469b6c17a6Seric 		while ((p = index(p, '\001')) != NULL)
1475dfc646bSeric 			if (*++p == 'u')
1485dfc646bSeric 				break;
1495dfc646bSeric 		if (p != NULL)
1505dfc646bSeric 			break;
1515dfc646bSeric 
1525dfc646bSeric 		/* this entry is safe -- go ahead and process it */
153588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
1545dfc646bSeric 		*pvp++ = newstr(buf);
1555dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1565dfc646bSeric 		{
1575dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1585dfc646bSeric 			return (-1);
1595dfc646bSeric 		}
1605dfc646bSeric 	}
161c579ef51Seric 
16233db8731Seric 	/*
16333db8731Seric 	**  If we have no substitution for the user name in the argument
16433db8731Seric 	**  list, we know that we must supply the names otherwise -- and
16533db8731Seric 	**  SMTP is the answer!!
16633db8731Seric 	*/
16733db8731Seric 
1685dfc646bSeric 	if (*mvp == NULL)
169c579ef51Seric 	{
170c579ef51Seric 		/* running SMTP */
1712c7e1b8dSeric # ifdef SMTP
172c579ef51Seric 		clever = TRUE;
173c579ef51Seric 		*pvp = NULL;
1742c7e1b8dSeric # else SMTP
17533db8731Seric 		/* oops!  we don't implement SMTP */
1762c7e1b8dSeric 		syserr("SMTP style mailer");
1772c7e1b8dSeric 		return (EX_SOFTWARE);
1782c7e1b8dSeric # endif SMTP
179c579ef51Seric 	}
1805dfc646bSeric 
1815dfc646bSeric 	/*
1825dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
1835dfc646bSeric 	**  run through our address list and append all the addresses
1845dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
1855dfc646bSeric 	**  always send another copy later.
1865dfc646bSeric 	*/
1875dfc646bSeric 
1885dfc646bSeric 	tobuf[0] = '\0';
189588cad61Seric 	e->e_to = tobuf;
1906259796dSeric 	ctladdr = NULL;
1915dfc646bSeric 	for (; to != NULL; to = to->q_next)
1925dfc646bSeric 	{
1935dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
19457fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
1955dfc646bSeric 			break;
1965dfc646bSeric 
1975dfc646bSeric 		/* if already sent or not for this host, don't send */
198da2935e1Seric 		if (bitset(QDONTSEND, to->q_flags) ||
199da2935e1Seric 		    strcmp(to->q_host, host) != 0 ||
200da2935e1Seric 		    to->q_mailer != firstto->q_mailer)
2015dfc646bSeric 			continue;
2026259796dSeric 
2034b22ea87Seric 		/* avoid overflowing tobuf */
204588cad61Seric 		if (sizeof tobuf - (strlen(to->q_paddr) + strlen(tobuf) + 2) < 0)
2054b22ea87Seric 			break;
2064b22ea87Seric 
207772e6e50Seric # ifdef DEBUG
2086ef48975Seric 		if (tTd(10, 1))
209772e6e50Seric 		{
210772e6e50Seric 			printf("\nsend to ");
211772e6e50Seric 			printaddr(to, FALSE);
212772e6e50Seric 		}
213772e6e50Seric # endif DEBUG
214772e6e50Seric 
2156259796dSeric 		/* compute effective uid/gid when sending */
2167da1035fSeric 		if (to->q_mailer == ProgMailer)
2176259796dSeric 			ctladdr = getctladdr(to);
2186259796dSeric 
2195dfc646bSeric 		user = to->q_user;
220588cad61Seric 		e->e_to = to->q_paddr;
2215dfc646bSeric 		to->q_flags |= QDONTSEND;
2225dfc646bSeric 
2235dfc646bSeric 		/*
2245dfc646bSeric 		**  Check to see that these people are allowed to
2255dfc646bSeric 		**  talk to each other.
2262a6e0786Seric 		*/
2272a6e0786Seric 
22869582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
22969582d2fSeric 		{
23069582d2fSeric 			usrerr("Message is too large; %ld bytes max", m->m_maxsize);
23169582d2fSeric 			NoReturn = TRUE;
23269582d2fSeric 			giveresponse(EX_UNAVAILABLE, m, e);
23369582d2fSeric 			continue;
23469582d2fSeric 		}
2352a6e0786Seric 		if (!checkcompat(to))
2365dfc646bSeric 		{
237198d9be0Seric 			giveresponse(EX_UNAVAILABLE, m, e);
2385dfc646bSeric 			continue;
2395dfc646bSeric 		}
2402a6e0786Seric 
2412a6e0786Seric 		/*
2429ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2439ec9501bSeric 		**	about them.
24425a99e2eSeric 		*/
24525a99e2eSeric 
24657fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
24725a99e2eSeric 		{
2489ec9501bSeric 			stripquotes(user, TRUE);
2499ec9501bSeric 			stripquotes(host, TRUE);
2509ec9501bSeric 		}
2519ec9501bSeric 		else
2529ec9501bSeric 		{
2539ec9501bSeric 			stripquotes(user, FALSE);
2549ec9501bSeric 			stripquotes(host, FALSE);
25525a99e2eSeric 		}
25625a99e2eSeric 
257cdb828c5Seric 		/* hack attack -- delivermail compatibility */
258cdb828c5Seric 		if (m == ProgMailer && *user == '|')
259cdb828c5Seric 			user++;
260cdb828c5Seric 
26125a99e2eSeric 		/*
2623efaed6eSeric 		**  If an error message has already been given, don't
2633efaed6eSeric 		**	bother to send to this address.
2643efaed6eSeric 		**
2653efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2663efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2673efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2683efaed6eSeric 		*/
2693efaed6eSeric 
2706cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2713efaed6eSeric 			continue;
2723efaed6eSeric 
273f2fec898Seric 		/* save statistics.... */
274588cad61Seric 		markstats(e, to);
275f2fec898Seric 
2763efaed6eSeric 		/*
27725a99e2eSeric 		**  See if this user name is "special".
27825a99e2eSeric 		**	If the user name has a slash in it, assume that this
27951552439Seric 		**	is a file -- send it off without further ado.  Note
28051552439Seric 		**	that this type of addresses is not processed along
28151552439Seric 		**	with the others, so we fudge on the To person.
28225a99e2eSeric 		*/
28325a99e2eSeric 
2847da1035fSeric 		if (m == LocalMailer)
28525a99e2eSeric 		{
286a49f24c0Seric 			if (user[0] == '/')
28725a99e2eSeric 			{
2885826d9d3Seric 				rcode = mailfile(user, getctladdr(to));
289198d9be0Seric 				giveresponse(rcode, m, e);
2905dfc646bSeric 				continue;
29125a99e2eSeric 			}
29225a99e2eSeric 		}
29325a99e2eSeric 
29413bbc08cSeric 		/*
29513bbc08cSeric 		**  Address is verified -- add this user to mailer
29613bbc08cSeric 		**  argv, and add it to the print list of recipients.
29713bbc08cSeric 		*/
29813bbc08cSeric 
299508daeccSeric 		/* link together the chain of recipients */
300508daeccSeric 		to->q_tchain = tochain;
301508daeccSeric 		tochain = to;
302508daeccSeric 
3035dfc646bSeric 		/* create list of users for error messages */
304db8841e9Seric 		(void) strcat(tobuf, ",");
305db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
306588cad61Seric 		define('u', user, e);		/* to user */
307588cad61Seric 		define('z', to->q_home, e);	/* user's home */
3085dfc646bSeric 
309c579ef51Seric 		/*
310508daeccSeric 		**  Expand out this user into argument list.
311c579ef51Seric 		*/
312c579ef51Seric 
313508daeccSeric 		if (!clever)
314c579ef51Seric 		{
315588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
3165dfc646bSeric 			*pvp++ = newstr(buf);
3175dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3185dfc646bSeric 			{
3195dfc646bSeric 				/* allow some space for trailing parms */
3205dfc646bSeric 				break;
3215dfc646bSeric 			}
3225dfc646bSeric 		}
323c579ef51Seric 	}
3245dfc646bSeric 
325145b49b1Seric 	/* see if any addresses still exist */
326145b49b1Seric 	if (tobuf[0] == '\0')
327c579ef51Seric 	{
328588cad61Seric 		define('g', (char *) NULL, e);
329145b49b1Seric 		return (0);
330c579ef51Seric 	}
331145b49b1Seric 
3325dfc646bSeric 	/* print out messages as full list */
33363780dbdSeric 	e->e_to = tobuf + 1;
3345dfc646bSeric 
3355dfc646bSeric 	/*
3365dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3375dfc646bSeric 	*/
3385dfc646bSeric 
339c579ef51Seric 	while (!clever && *++mvp != NULL)
3405dfc646bSeric 	{
341588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
3425dfc646bSeric 		*pvp++ = newstr(buf);
3435dfc646bSeric 		if (pvp >= &pv[MAXPV])
3445dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
3455dfc646bSeric 	}
3465dfc646bSeric 	*pvp++ = NULL;
3475dfc646bSeric 
34825a99e2eSeric 	/*
34925a99e2eSeric 	**  Call the mailer.
3506328bdf7Seric 	**	The argument vector gets built, pipes
35125a99e2eSeric 	**	are created as necessary, and we fork & exec as
3526328bdf7Seric 	**	appropriate.
353c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
35425a99e2eSeric 	*/
35525a99e2eSeric 
356588cad61Seric 	message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
357588cad61Seric 
3586259796dSeric 	if (ctladdr == NULL)
359588cad61Seric 		ctladdr = &e->e_from;
3602c7e1b8dSeric # ifdef SMTP
361c579ef51Seric 	if (clever)
362c579ef51Seric 	{
363588cad61Seric 		/* send the initial SMTP protocol */
36477b52738Seric 		rcode = smtpinit(m, pv);
365588cad61Seric 
36663780dbdSeric 		if (rcode == EX_OK)
36763780dbdSeric 		{
368588cad61Seric 			/* send the recipient list */
36963780dbdSeric 			tobuf[0] = '\0';
370588cad61Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
371588cad61Seric 			{
372588cad61Seric 				int i;
373588cad61Seric 
37463780dbdSeric 				e->e_to = to->q_paddr;
37577b52738Seric 				i = smtprcpt(to, m);
376588cad61Seric 				if (i != EX_OK)
377588cad61Seric 				{
37883b7ddc9Seric 					markfailure(e, to, i);
379198d9be0Seric 					giveresponse(i, m, e);
38063780dbdSeric 				}
38163780dbdSeric 				else
38263780dbdSeric 				{
383*3462ad9eSeric 					(void) strcat(tobuf, ",");
384*3462ad9eSeric 					(void) strcat(tobuf, to->q_paddr);
385588cad61Seric 				}
386588cad61Seric 			}
387588cad61Seric 
38863780dbdSeric 			/* now send the data */
38963780dbdSeric 			if (tobuf[0] == '\0')
39063780dbdSeric 				e->e_to = NULL;
39163780dbdSeric 			else
39263780dbdSeric 			{
39363780dbdSeric 				e->e_to = tobuf + 1;
39477b52738Seric 				rcode = smtpdata(m, e);
39563780dbdSeric 			}
39663780dbdSeric 
39763780dbdSeric 			/* now close the connection */
398a294c4b0Seric 			smtpquit(m);
39963780dbdSeric 		}
400c579ef51Seric 	}
401c579ef51Seric 	else
4022c7e1b8dSeric # endif SMTP
40377b52738Seric 		rcode = sendoff(e, m, pv, ctladdr);
4045dfc646bSeric 
405c77d1c25Seric 	/*
40663780dbdSeric 	**  Do final status disposal.
40763780dbdSeric 	**	We check for something in tobuf for the SMTP case.
408c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
409c77d1c25Seric 	**		addressees.
410c77d1c25Seric 	*/
411c77d1c25Seric 
41263780dbdSeric 	if (tobuf[0] != '\0')
413198d9be0Seric 		giveresponse(rcode, m, e);
41463780dbdSeric 	if (rcode != EX_OK)
415c77d1c25Seric 	{
416772e6e50Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
41783b7ddc9Seric 			markfailure(e, to, rcode);
418c77d1c25Seric 	}
419c77d1c25Seric 
42035490626Seric 	errno = 0;
421588cad61Seric 	define('g', (char *) NULL, e);
4225826d9d3Seric 	return (rcode);
42325a99e2eSeric }
4245dfc646bSeric /*
42583b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
42683b7ddc9Seric **
42783b7ddc9Seric **	Parameters:
42883b7ddc9Seric **		e -- the envelope we are sending.
42983b7ddc9Seric **		q -- the address to mark.
43083b7ddc9Seric **		rcode -- the code signifying the particular failure.
43183b7ddc9Seric **
43283b7ddc9Seric **	Returns:
43383b7ddc9Seric **		none.
43483b7ddc9Seric **
43583b7ddc9Seric **	Side Effects:
43683b7ddc9Seric **		marks the address (and possibly the envelope) with the
43783b7ddc9Seric **			failure so that an error will be returned or
43883b7ddc9Seric **			the message will be queued, as appropriate.
43983b7ddc9Seric */
44083b7ddc9Seric 
44183b7ddc9Seric markfailure(e, q, rcode)
44283b7ddc9Seric 	register ENVELOPE *e;
44383b7ddc9Seric 	register ADDRESS *q;
44483b7ddc9Seric 	int rcode;
44583b7ddc9Seric {
44683b7ddc9Seric 	if (rcode == EX_OK)
44783b7ddc9Seric 		return;
44883b7ddc9Seric 	else if (rcode != EX_TEMPFAIL)
44983b7ddc9Seric 		q->q_flags |= QBADADDR;
45083b7ddc9Seric 	else if (curtime() > e->e_ctime + TimeOut)
45183b7ddc9Seric 	{
45283b7ddc9Seric 		extern char *pintvl();
453198d9be0Seric 		char buf[MAXLINE];
45483b7ddc9Seric 
45583b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
456198d9be0Seric 		{
457198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
45883b7ddc9Seric 				pintvl(TimeOut, FALSE));
459198d9be0Seric 			if (e->e_message != NULL)
460198d9be0Seric 				free(e->e_message);
461198d9be0Seric 			e->e_message = newstr(buf);
462198d9be0Seric 			message(Arpa_Info, buf);
463198d9be0Seric 		}
46483b7ddc9Seric 		q->q_flags |= QBADADDR;
46583b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
46683b7ddc9Seric 	}
46783b7ddc9Seric 	else
46883b7ddc9Seric 		q->q_flags |= QQUEUEUP;
46983b7ddc9Seric }
47083b7ddc9Seric /*
47132d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
47232d19d43Seric **
47332d19d43Seric **	This MUST be a macro, since after a vfork we are running
47432d19d43Seric **	two processes on the same stack!!!
47532d19d43Seric **
47632d19d43Seric **	Parameters:
47732d19d43Seric **		none.
47832d19d43Seric **
47932d19d43Seric **	Returns:
48032d19d43Seric **		From a macro???  You've got to be kidding!
48132d19d43Seric **
48232d19d43Seric **	Side Effects:
48332d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
48432d19d43Seric **			pid of child in parent, zero in child.
48532d19d43Seric **			-1 on unrecoverable error.
48632d19d43Seric **
48732d19d43Seric **	Notes:
48832d19d43Seric **		I'm awfully sorry this looks so awful.  That's
48932d19d43Seric **		vfork for you.....
49032d19d43Seric */
49132d19d43Seric 
49232d19d43Seric # define NFORKTRIES	5
4934300ddf0Seric # ifdef VMUNIX
49432d19d43Seric # define XFORK	vfork
4954300ddf0Seric # else VMUNIX
49632d19d43Seric # define XFORK	fork
4974300ddf0Seric # endif VMUNIX
49832d19d43Seric 
49932d19d43Seric # define DOFORK(fORKfN) \
50032d19d43Seric {\
50132d19d43Seric 	register int i;\
50232d19d43Seric \
50332d19d43Seric 	for (i = NFORKTRIES; i-- > 0; )\
50432d19d43Seric 	{\
50532d19d43Seric 		pid = fORKfN();\
50632d19d43Seric 		if (pid >= 0)\
50732d19d43Seric 			break;\
5085e663df1Seric 		sleep(NFORKTRIES - i);\
50932d19d43Seric 	}\
51032d19d43Seric }
51132d19d43Seric /*
5122ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
5132ed72599Seric **
5142ed72599Seric **	Parameters:
5152ed72599Seric **		none.
5162ed72599Seric **
5172ed72599Seric **	Returns:
5182ed72599Seric **		pid of child in parent.
5192ed72599Seric **		zero in child.
5202ed72599Seric **		-1 on error.
5212ed72599Seric **
5222ed72599Seric **	Side Effects:
5232ed72599Seric **		returns twice, once in parent and once in child.
5242ed72599Seric */
5252ed72599Seric 
5262ed72599Seric dofork()
5272ed72599Seric {
5282ed72599Seric 	register int pid;
5292ed72599Seric 
5302ed72599Seric 	DOFORK(fork);
5312ed72599Seric 	return (pid);
5322ed72599Seric }
5332ed72599Seric /*
5345dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
5355dfc646bSeric **
5365dfc646bSeric **	Parameters:
537588cad61Seric **		e -- the envelope to mail.
5385dfc646bSeric **		m -- mailer descriptor.
5395dfc646bSeric **		pvp -- parameter vector to send to it.
5406259796dSeric **		ctladdr -- an address pointer controlling the
5416259796dSeric **			user/groupid etc. of the mailer.
5425dfc646bSeric **
5435dfc646bSeric **	Returns:
5445dfc646bSeric **		exit status of mailer.
5455dfc646bSeric **
5465dfc646bSeric **	Side Effects:
5475dfc646bSeric **		none.
5485dfc646bSeric */
5495dfc646bSeric 
55077b52738Seric sendoff(e, m, pvp, ctladdr)
551588cad61Seric 	register ENVELOPE *e;
552588cad61Seric 	MAILER *m;
5535dfc646bSeric 	char **pvp;
5546259796dSeric 	ADDRESS *ctladdr;
5555dfc646bSeric {
556c579ef51Seric 	auto FILE *mfile;
557c579ef51Seric 	auto FILE *rfile;
5585dfc646bSeric 	register int i;
559c579ef51Seric 	int pid;
560c579ef51Seric 
561c579ef51Seric 	/*
562c579ef51Seric 	**  Create connection to mailer.
563c579ef51Seric 	*/
564c579ef51Seric 
565c579ef51Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
566c579ef51Seric 	if (pid < 0)
567c579ef51Seric 		return (-1);
568c579ef51Seric 
569c579ef51Seric 	/*
570c579ef51Seric 	**  Format and send message.
571c579ef51Seric 	*/
572c579ef51Seric 
57377b52738Seric 	putfromline(mfile, m);
57477b52738Seric 	(*e->e_puthdr)(mfile, m, e);
57577b52738Seric 	putline("\n", mfile, m);
57677b52738Seric 	(*e->e_putbody)(mfile, m, e);
577c579ef51Seric 	(void) fclose(mfile);
578c579ef51Seric 
579c579ef51Seric 	i = endmailer(pid, pvp[0]);
580bc6e2962Seric 
581bc6e2962Seric 	/* arrange a return receipt if requested */
58257fc6f17Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags))
583bc6e2962Seric 	{
584588cad61Seric 		e->e_flags |= EF_SENDRECEIPT;
585bc6e2962Seric 		/* do we want to send back more info? */
586bc6e2962Seric 	}
587bc6e2962Seric 
588c579ef51Seric 	return (i);
589c579ef51Seric }
590c579ef51Seric /*
591c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
592c579ef51Seric **
593c579ef51Seric **	We should never get fatal errors (e.g., segmentation
594c579ef51Seric **	violation), so we report those specially.  For other
595c579ef51Seric **	errors, we choose a status message (into statmsg),
596c579ef51Seric **	and if it represents an error, we print it.
597c579ef51Seric **
598c579ef51Seric **	Parameters:
599c579ef51Seric **		pid -- pid of mailer.
600c579ef51Seric **		name -- name of mailer (for error messages).
601c579ef51Seric **
602c579ef51Seric **	Returns:
603c579ef51Seric **		exit code of mailer.
604c579ef51Seric **
605c579ef51Seric **	Side Effects:
606c579ef51Seric **		none.
607c579ef51Seric */
608c579ef51Seric 
609c579ef51Seric endmailer(pid, name)
610c579ef51Seric 	int pid;
611c579ef51Seric 	char *name;
612c579ef51Seric {
613588cad61Seric 	int st;
614c579ef51Seric 
61533db8731Seric 	/* in the IPC case there is nothing to wait for */
61633db8731Seric 	if (pid == 0)
61733db8731Seric 		return (EX_OK);
61833db8731Seric 
61933db8731Seric 	/* wait for the mailer process to die and collect status */
620588cad61Seric 	st = waitfor(pid);
621588cad61Seric 	if (st == -1)
62278de67c1Seric 	{
623588cad61Seric 		syserr("endmailer %s: wait", name);
624588cad61Seric 		return (EX_SOFTWARE);
625c579ef51Seric 	}
62633db8731Seric 
62733db8731Seric 	/* see if it died a horrid death */
628c579ef51Seric 	if ((st & 0377) != 0)
629c579ef51Seric 	{
630588cad61Seric 		syserr("endmailer %s: stat %o", name, st);
631c579ef51Seric 		ExitStat = EX_UNAVAILABLE;
632588cad61Seric 		return (EX_UNAVAILABLE);
633c579ef51Seric 	}
63433db8731Seric 
63533db8731Seric 	/* normal death -- return status */
636588cad61Seric 	st = (st >> 8) & 0377;
637588cad61Seric 	return (st);
638c579ef51Seric }
639c579ef51Seric /*
640c579ef51Seric **  OPENMAILER -- open connection to mailer.
641c579ef51Seric **
642c579ef51Seric **	Parameters:
643c579ef51Seric **		m -- mailer descriptor.
644c579ef51Seric **		pvp -- parameter vector to pass to mailer.
645c579ef51Seric **		ctladdr -- controlling address for user.
646c579ef51Seric **		clever -- create a full duplex connection.
647c579ef51Seric **		pmfile -- pointer to mfile (to mailer) connection.
648c579ef51Seric **		prfile -- pointer to rfile (from mailer) connection.
649c579ef51Seric **
650c579ef51Seric **	Returns:
65133db8731Seric **		pid of mailer ( > 0 ).
652c579ef51Seric **		-1 on error.
65333db8731Seric **		zero on an IPC connection.
654c579ef51Seric **
655c579ef51Seric **	Side Effects:
656c579ef51Seric **		creates a mailer in a subprocess.
657c579ef51Seric */
658c579ef51Seric 
659c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
660588cad61Seric 	MAILER *m;
661c579ef51Seric 	char **pvp;
662c579ef51Seric 	ADDRESS *ctladdr;
663c579ef51Seric 	bool clever;
664c579ef51Seric 	FILE **pmfile;
665c579ef51Seric 	FILE **prfile;
666c579ef51Seric {
6675dfc646bSeric 	int pid;
668f8952a83Seric 	int mpvect[2];
669c579ef51Seric 	int rpvect[2];
6705dfc646bSeric 	FILE *mfile;
671c579ef51Seric 	FILE *rfile;
6725dfc646bSeric 	extern FILE *fdopen();
6735dfc646bSeric 
6745dfc646bSeric # ifdef DEBUG
6756ef48975Seric 	if (tTd(11, 1))
6765dfc646bSeric 	{
6778c57e552Seric 		printf("openmailer:");
6785dfc646bSeric 		printav(pvp);
6795dfc646bSeric 	}
6805dfc646bSeric # endif DEBUG
68135490626Seric 	errno = 0;
6825dfc646bSeric 
68333db8731Seric 	/*
68433db8731Seric 	**  Deal with the special case of mail handled through an IPC
68533db8731Seric 	**  connection.
68633db8731Seric 	**	In this case we don't actually fork.  We must be
68733db8731Seric 	**	running SMTP for this to work.  We will return a
68833db8731Seric 	**	zero pid to indicate that we are running IPC.
689e7c1bd78Seric 	**  We also handle a debug version that just talks to stdin/out.
69033db8731Seric 	*/
69133db8731Seric 
692e7c1bd78Seric #ifdef DEBUG
693e7c1bd78Seric 	/* check for Local Person Communication -- not for mortals!!! */
694e7c1bd78Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
695e7c1bd78Seric 	{
696e7c1bd78Seric 		*pmfile = stdout;
697e7c1bd78Seric 		*prfile = stdin;
698e7c1bd78Seric 		return (0);
699e7c1bd78Seric 	}
700e7c1bd78Seric #endif DEBUG
701e7c1bd78Seric 
70233db8731Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
70333db8731Seric 	{
704588cad61Seric #ifdef DAEMON
70533db8731Seric 		register int i;
7061277f9a8Seric 		register u_short port;
70733db8731Seric 
70833db8731Seric 		if (!clever)
70933db8731Seric 			syserr("non-clever IPC");
71093b6e3cfSeric 		if (pvp[2] != NULL)
7111277f9a8Seric 			port = atoi(pvp[2]);
71293b6e3cfSeric 		else
7131277f9a8Seric 			port = 0;
7141277f9a8Seric 		i = makeconnection(pvp[1], port, pmfile, prfile);
71533db8731Seric 		if (i != EX_OK)
716ed854c7bSeric 		{
717ed854c7bSeric 			ExitStat = i;
71833db8731Seric 			return (-1);
719ed854c7bSeric 		}
72033db8731Seric 		else
72133db8731Seric 			return (0);
722588cad61Seric #else DAEMON
723588cad61Seric 		syserr("openmailer: no IPC");
724588cad61Seric 		return (-1);
72533db8731Seric #endif DAEMON
726588cad61Seric 	}
72733db8731Seric 
7286328bdf7Seric 	/* create a pipe to shove the mail through */
729f8952a83Seric 	if (pipe(mpvect) < 0)
73025a99e2eSeric 	{
731588cad61Seric 		syserr("openmailer: pipe (to mailer)");
73225a99e2eSeric 		return (-1);
73325a99e2eSeric 	}
734c579ef51Seric 
7352c7e1b8dSeric #ifdef SMTP
736c579ef51Seric 	/* if this mailer speaks smtp, create a return pipe */
737c579ef51Seric 	if (clever && pipe(rpvect) < 0)
738c579ef51Seric 	{
739588cad61Seric 		syserr("openmailer: pipe (from mailer)");
740c579ef51Seric 		(void) close(mpvect[0]);
741c579ef51Seric 		(void) close(mpvect[1]);
742c579ef51Seric 		return (-1);
743c579ef51Seric 	}
7442c7e1b8dSeric #endif SMTP
745c579ef51Seric 
74633db8731Seric 	/*
74733db8731Seric 	**  Actually fork the mailer process.
74833db8731Seric 	**	DOFORK is clever about retrying.
74933db8731Seric 	*/
75033db8731Seric 
7519a6a5f55Seric 	if (CurEnv->e_xfp != NULL)
7529a6a5f55Seric 		(void) fflush(CurEnv->e_xfp);		/* for debugging */
753588cad61Seric 	(void) fflush(stdout);
75432d19d43Seric 	DOFORK(XFORK);
755f129ec7dSeric 	/* pid is set by DOFORK */
75625a99e2eSeric 	if (pid < 0)
75725a99e2eSeric 	{
75833db8731Seric 		/* failure */
759588cad61Seric 		syserr("openmailer: cannot fork");
760f8952a83Seric 		(void) close(mpvect[0]);
761f8952a83Seric 		(void) close(mpvect[1]);
762588cad61Seric #ifdef SMTP
763c579ef51Seric 		if (clever)
764c579ef51Seric 		{
765c579ef51Seric 			(void) close(rpvect[0]);
766c579ef51Seric 			(void) close(rpvect[1]);
767c579ef51Seric 		}
768588cad61Seric #endif SMTP
76925a99e2eSeric 		return (-1);
77025a99e2eSeric 	}
77125a99e2eSeric 	else if (pid == 0)
77225a99e2eSeric 	{
77313088b9fSeric 		int i;
77413088b9fSeric 
77525a99e2eSeric 		/* child -- set up input & exec mailer */
77603ab8e55Seric 		/* make diagnostic output be standard output */
7778f0e7860Seric 		(void) signal(SIGINT, SIG_IGN);
7788f0e7860Seric 		(void) signal(SIGHUP, SIG_IGN);
7790984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
780f8952a83Seric 
781f8952a83Seric 		/* arrange to filter standard & diag output of command */
782c579ef51Seric 		if (clever)
783c579ef51Seric 		{
784c579ef51Seric 			(void) close(rpvect[0]);
785c579ef51Seric 			(void) close(1);
786c579ef51Seric 			(void) dup(rpvect[1]);
787c579ef51Seric 			(void) close(rpvect[1]);
788c579ef51Seric 		}
789276723a8Seric 		else if (OpMode == MD_SMTP || HoldErrs)
790f8952a83Seric 		{
791588cad61Seric 			/* put mailer output in transcript */
792f8952a83Seric 			(void) close(1);
7939a6a5f55Seric 			(void) dup(fileno(CurEnv->e_xfp));
794f8952a83Seric 		}
795db8841e9Seric 		(void) close(2);
796db8841e9Seric 		(void) dup(1);
797f8952a83Seric 
798f8952a83Seric 		/* arrange to get standard input */
799f8952a83Seric 		(void) close(mpvect[1]);
800db8841e9Seric 		(void) close(0);
801f8952a83Seric 		if (dup(mpvect[0]) < 0)
80225a99e2eSeric 		{
80325a99e2eSeric 			syserr("Cannot dup to zero!");
804a590b978Seric 			_exit(EX_OSERR);
80525a99e2eSeric 		}
806f8952a83Seric 		(void) close(mpvect[0]);
80757fc6f17Seric 		if (!bitnset(M_RESTR, m->m_flags))
8080984da9fSeric 		{
80953e3fa05Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
810e36b99e2Seric 			{
811e36b99e2Seric 				(void) setgid(DefGid);
812e36b99e2Seric 				(void) setuid(DefUid);
813e36b99e2Seric 			}
814e36b99e2Seric 			else
81569f29479Seric 			{
816e36b99e2Seric 				(void) setgid(ctladdr->q_gid);
817e36b99e2Seric 				(void) setuid(ctladdr->q_uid);
81869f29479Seric 			}
8190984da9fSeric 		}
820588cad61Seric 
82113088b9fSeric 		/* arrange for all the files to be closed */
82213088b9fSeric 		for (i = 3; i < 50; i++)
82313088b9fSeric #ifdef FIOCLEX
82413088b9fSeric 			(void) ioctl(i, FIOCLEX, 0);
82513088b9fSeric #else FIOCLEX
82613088b9fSeric 			(void) close(i);
82713088b9fSeric #endif FIOCLEX
82833db8731Seric 
82933db8731Seric 		/* try to execute the mailer */
83025a99e2eSeric 		execv(m->m_mailer, pvp);
83133db8731Seric 
83213088b9fSeric #ifdef FIOCLEX
83313088b9fSeric 		syserr("Cannot exec %s", m->m_mailer);
83413088b9fSeric #else FIOCLEX
83532d19d43Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
836db8841e9Seric 		(void) fflush(stdout);
83713088b9fSeric #endif FIOCLEX
83855f33c03Seric 		if (m == LocalMailer)
83955f33c03Seric 			_exit(EX_TEMPFAIL);
84055f33c03Seric 		else
841a590b978Seric 			_exit(EX_UNAVAILABLE);
84225a99e2eSeric 	}
84325a99e2eSeric 
844f8952a83Seric 	/*
845c579ef51Seric 	**  Set up return value.
846f8952a83Seric 	*/
847f8952a83Seric 
848f8952a83Seric 	(void) close(mpvect[0]);
849f8952a83Seric 	mfile = fdopen(mpvect[1], "w");
850c579ef51Seric 	if (clever)
85125a99e2eSeric 	{
852c579ef51Seric 		(void) close(rpvect[1]);
853c579ef51Seric 		rfile = fdopen(rpvect[0], "r");
85425a99e2eSeric 	}
855c579ef51Seric 
856c579ef51Seric 	*pmfile = mfile;
857c579ef51Seric 	*prfile = rfile;
858c579ef51Seric 
859c579ef51Seric 	return (pid);
86025a99e2eSeric }
86125a99e2eSeric /*
86225a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
86325a99e2eSeric **
86425a99e2eSeric **	Parameters:
86525a99e2eSeric **		stat -- the status code from the mailer (high byte
86625a99e2eSeric **			only; core dumps must have been taken care of
86725a99e2eSeric **			already).
86825a99e2eSeric **		m -- the mailer descriptor for this mailer.
86925a99e2eSeric **
87025a99e2eSeric **	Returns:
871db8841e9Seric **		none.
87225a99e2eSeric **
87325a99e2eSeric **	Side Effects:
874c1f9df2cSeric **		Errors may be incremented.
87525a99e2eSeric **		ExitStat may be set.
87625a99e2eSeric */
87725a99e2eSeric 
878198d9be0Seric giveresponse(stat, m, e)
87925a99e2eSeric 	int stat;
880588cad61Seric 	register MAILER *m;
881198d9be0Seric 	ENVELOPE *e;
88225a99e2eSeric {
88325a99e2eSeric 	register char *statmsg;
88425a99e2eSeric 	extern char *SysExMsg[];
88525a99e2eSeric 	register int i;
88625a99e2eSeric 	extern int N_SysEx;
887198d9be0Seric 	char buf[MAXLINE];
88825a99e2eSeric 
8897d1fc79dSeric #ifdef lint
8907d1fc79dSeric 	if (m == NULL)
8917d1fc79dSeric 		return;
8927d1fc79dSeric #endif lint
8937d1fc79dSeric 
89413bbc08cSeric 	/*
89513bbc08cSeric 	**  Compute status message from code.
89613bbc08cSeric 	*/
89713bbc08cSeric 
89825a99e2eSeric 	i = stat - EX__BASE;
899588cad61Seric 	if (stat == 0)
900588cad61Seric 		statmsg = "250 Sent";
901588cad61Seric 	else if (i < 0 || i > N_SysEx)
902588cad61Seric 	{
903588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
904588cad61Seric 		stat = EX_UNAVAILABLE;
905588cad61Seric 		statmsg = buf;
906588cad61Seric 	}
907198d9be0Seric 	else if (stat == EX_TEMPFAIL)
908198d9be0Seric 	{
9098557d168Seric 		(void) strcpy(buf, SysExMsg[i]);
9108557d168Seric 		if (errno != 0)
911198d9be0Seric 		{
91287c9b3e7Seric 			extern char *errstring();
9138557d168Seric 
914d87e85f3Seric 			statmsg = errstring(errno);
915d87e85f3Seric 		}
916d87e85f3Seric 		else
917d87e85f3Seric 		{
918d87e85f3Seric #ifdef SMTP
919d87e85f3Seric 			extern char SmtpError[];
920d87e85f3Seric 
921d87e85f3Seric 			statmsg = SmtpError;
922d87e85f3Seric #else SMTP
923d87e85f3Seric 			statmsg = NULL;
924d87e85f3Seric #endif SMTP
925d87e85f3Seric 		}
926d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
927d87e85f3Seric 		{
92887c9b3e7Seric 			(void) strcat(buf, ": ");
929d87e85f3Seric 			(void) strcat(buf, statmsg);
9308557d168Seric 		}
931198d9be0Seric 		statmsg = buf;
932198d9be0Seric 	}
93325a99e2eSeric 	else
934d87e85f3Seric 	{
93525a99e2eSeric 		statmsg = SysExMsg[i];
936d87e85f3Seric 	}
937588cad61Seric 
938588cad61Seric 	/*
939588cad61Seric 	**  Print the message as appropriate
940588cad61Seric 	*/
941588cad61Seric 
942198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
9435826d9d3Seric 		message(Arpa_Info, &statmsg[4]);
94425a99e2eSeric 	else
94525a99e2eSeric 	{
946c1f9df2cSeric 		Errors++;
9475826d9d3Seric 		usrerr(statmsg);
94825a99e2eSeric 	}
94925a99e2eSeric 
95025a99e2eSeric 	/*
95125a99e2eSeric 	**  Final cleanup.
95225a99e2eSeric 	**	Log a record of the transaction.  Compute the new
95325a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
95425a99e2eSeric 	**	that.
95525a99e2eSeric 	*/
95625a99e2eSeric 
95761f5a1d4Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
958eb238f8cSeric 		logdelivery(&statmsg[4]);
959eb238f8cSeric 
960eb238f8cSeric 	if (stat != EX_TEMPFAIL)
961eb238f8cSeric 		setstat(stat);
962198d9be0Seric 	if (stat != EX_OK)
963198d9be0Seric 	{
964198d9be0Seric 		if (e->e_message != NULL)
965198d9be0Seric 			free(e->e_message);
966198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
967198d9be0Seric 	}
9688557d168Seric 	errno = 0;
969eb238f8cSeric }
970eb238f8cSeric /*
971eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
972eb238f8cSeric **
973eb238f8cSeric **	Parameters:
974eb238f8cSeric **		stat -- the message to print for the status
975eb238f8cSeric **
976eb238f8cSeric **	Returns:
977eb238f8cSeric **		none
978eb238f8cSeric **
979eb238f8cSeric **	Side Effects:
980eb238f8cSeric **		none
981eb238f8cSeric */
982eb238f8cSeric 
983eb238f8cSeric logdelivery(stat)
984eb238f8cSeric 	char *stat;
9855cf56be3Seric {
9865cf56be3Seric 	extern char *pintvl();
9875cf56be3Seric 
988eb238f8cSeric # ifdef LOG
9895cf56be3Seric 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id,
990eb238f8cSeric 	       CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat);
99125a99e2eSeric # endif LOG
99225a99e2eSeric }
99325a99e2eSeric /*
99451552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
99525a99e2eSeric **
99651552439Seric **	This can be made an arbitrary message separator by changing $l
99751552439Seric **
9989b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
9999b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
10009b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
10019b6c17a6Seric **	this kind of antique garbage????
100225a99e2eSeric **
100325a99e2eSeric **	Parameters:
100451552439Seric **		fp -- the file to output to.
100551552439Seric **		m -- the mailer describing this entry.
100625a99e2eSeric **
100725a99e2eSeric **	Returns:
100851552439Seric **		none
100925a99e2eSeric **
101025a99e2eSeric **	Side Effects:
101151552439Seric **		outputs some text to fp.
101225a99e2eSeric */
101325a99e2eSeric 
101477b52738Seric putfromline(fp, m)
101551552439Seric 	register FILE *fp;
101651552439Seric 	register MAILER *m;
101725a99e2eSeric {
10189b6c17a6Seric 	char *template = "\001l\n";
101951552439Seric 	char buf[MAXLINE];
102025a99e2eSeric 
102157fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
102251552439Seric 		return;
102313bbc08cSeric 
10242c7e1b8dSeric # ifdef UGLYUUCP
102557fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
102674b6e67bSeric 	{
1027ea09d6edSeric 		char *bang;
1028ea09d6edSeric 		char xbuf[MAXLINE];
102974b6e67bSeric 
10309b6c17a6Seric 		expand("\001g", buf, &buf[sizeof buf - 1], CurEnv);
1031ea09d6edSeric 		bang = index(buf, '!');
103274b6e67bSeric 		if (bang == NULL)
1033ea09d6edSeric 			syserr("No ! in UUCP! (%s)", buf);
103474b6e67bSeric 		else
1035588cad61Seric 		{
1036ea09d6edSeric 			*bang++ = '\0';
10379b6c17a6Seric 			(void) sprintf(xbuf, "From %s  \001d remote from %s\n", bang, buf);
1038ea09d6edSeric 			template = xbuf;
103974b6e67bSeric 		}
1040588cad61Seric 	}
10412c7e1b8dSeric # endif UGLYUUCP
1042ea09d6edSeric 	expand(template, buf, &buf[sizeof buf - 1], CurEnv);
104377b52738Seric 	putline(buf, fp, m);
1044bc6e2962Seric }
1045bc6e2962Seric /*
104651552439Seric **  PUTBODY -- put the body of a message.
104751552439Seric **
104851552439Seric **	Parameters:
104951552439Seric **		fp -- file to output onto.
105077b52738Seric **		m -- a mailer descriptor to control output format.
10519a6a5f55Seric **		e -- the envelope to put out.
105251552439Seric **
105351552439Seric **	Returns:
105451552439Seric **		none.
105551552439Seric **
105651552439Seric **	Side Effects:
105751552439Seric **		The message is written onto fp.
105851552439Seric */
105951552439Seric 
106077b52738Seric putbody(fp, m, e)
106151552439Seric 	FILE *fp;
1062588cad61Seric 	MAILER *m;
10639a6a5f55Seric 	register ENVELOPE *e;
106451552439Seric {
106577b52738Seric 	char buf[MAXLINE];
106651552439Seric 
106751552439Seric 	/*
106851552439Seric 	**  Output the body of the message
106951552439Seric 	*/
107051552439Seric 
10719a6a5f55Seric 	if (e->e_dfp == NULL)
107251552439Seric 	{
10739a6a5f55Seric 		if (e->e_df != NULL)
10749a6a5f55Seric 		{
10759a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
10769a6a5f55Seric 			if (e->e_dfp == NULL)
10779a6a5f55Seric 				syserr("Cannot open %s", e->e_df);
10789a6a5f55Seric 		}
10799a6a5f55Seric 		else
108077b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
10819a6a5f55Seric 	}
10829a6a5f55Seric 	if (e->e_dfp != NULL)
10839a6a5f55Seric 	{
10849a6a5f55Seric 		rewind(e->e_dfp);
108577b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
108624fc8aeeSeric 		{
108724fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
108824fc8aeeSeric 			    strncmp(buf, "From", 4) == 0)
1089*3462ad9eSeric 				(void) putc('>', fp);
109077b52738Seric 			putline(buf, fp, m);
109124fc8aeeSeric 		}
109251552439Seric 
10939a6a5f55Seric 		if (ferror(e->e_dfp))
109451552439Seric 		{
109551552439Seric 			syserr("putbody: read error");
109651552439Seric 			ExitStat = EX_IOERR;
109751552439Seric 		}
109851552439Seric 	}
109951552439Seric 
110051552439Seric 	(void) fflush(fp);
110151552439Seric 	if (ferror(fp) && errno != EPIPE)
110251552439Seric 	{
110351552439Seric 		syserr("putbody: write error");
110451552439Seric 		ExitStat = EX_IOERR;
110551552439Seric 	}
110651552439Seric 	errno = 0;
110725a99e2eSeric }
110825a99e2eSeric /*
110925a99e2eSeric **  MAILFILE -- Send a message to a file.
111025a99e2eSeric **
1111f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1112f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1113f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1114f129ec7dSeric **	sendmail runs as root.
1115f129ec7dSeric **
1116588cad61Seric **	This could be done as a subordinate mailer, except that it
1117588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1118588cad61Seric **	view this as being sufficiently important as to include it
1119588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1120588cad61Seric **	to create another process plus some pipes to save the message.
1121588cad61Seric **
112225a99e2eSeric **	Parameters:
112325a99e2eSeric **		filename -- the name of the file to send to.
11246259796dSeric **		ctladdr -- the controlling address header -- includes
11256259796dSeric **			the userid/groupid to be when sending.
112625a99e2eSeric **
112725a99e2eSeric **	Returns:
112825a99e2eSeric **		The exit code associated with the operation.
112925a99e2eSeric **
113025a99e2eSeric **	Side Effects:
113125a99e2eSeric **		none.
113225a99e2eSeric */
113325a99e2eSeric 
11346259796dSeric mailfile(filename, ctladdr)
113525a99e2eSeric 	char *filename;
11366259796dSeric 	ADDRESS *ctladdr;
113725a99e2eSeric {
113825a99e2eSeric 	register FILE *f;
113932d19d43Seric 	register int pid;
114025a99e2eSeric 
114132d19d43Seric 	/*
114232d19d43Seric 	**  Fork so we can change permissions here.
114332d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
114432d19d43Seric 	**	the complications of calling subroutines, etc.
114532d19d43Seric 	*/
114632d19d43Seric 
114732d19d43Seric 	DOFORK(fork);
114832d19d43Seric 
114932d19d43Seric 	if (pid < 0)
115032d19d43Seric 		return (EX_OSERR);
115132d19d43Seric 	else if (pid == 0)
115232d19d43Seric 	{
115332d19d43Seric 		/* child -- actually write to file */
1154f129ec7dSeric 		struct stat stb;
1155f129ec7dSeric 
11560984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
11570984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
11580984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
1159*3462ad9eSeric 		(void) umask(OldUmask);
1160f129ec7dSeric 		if (stat(filename, &stb) < 0)
116124447f54Seric 		{
116224447f54Seric 			errno = 0;
1163e6e1265fSeric 			stb.st_mode = 0666;
116424447f54Seric 		}
1165f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1166f129ec7dSeric 			exit(EX_CANTCREAT);
116703827b5fSeric 		if (ctladdr == NULL)
11687a941e7aSeric 			ctladdr = &CurEnv->e_from;
1169f129ec7dSeric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
1170e36b99e2Seric 		{
1171e36b99e2Seric 			if (ctladdr->q_uid == 0)
1172e36b99e2Seric 				(void) setgid(DefGid);
1173e36b99e2Seric 			else
11746259796dSeric 				(void) setgid(ctladdr->q_gid);
1175e36b99e2Seric 		}
1176f129ec7dSeric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
1177e36b99e2Seric 		{
1178e36b99e2Seric 			if (ctladdr->q_uid == 0)
1179e36b99e2Seric 				(void) setuid(DefUid);
1180e36b99e2Seric 			else
11816259796dSeric 				(void) setuid(ctladdr->q_uid);
1182e36b99e2Seric 		}
118327628d59Seric 		f = dfopen(filename, "a");
118425a99e2eSeric 		if (f == NULL)
118532d19d43Seric 			exit(EX_CANTCREAT);
118625a99e2eSeric 
118777b52738Seric 		putfromline(f, ProgMailer);
118877b52738Seric 		(*CurEnv->e_puthdr)(f, ProgMailer, CurEnv);
118977b52738Seric 		putline("\n", f, ProgMailer);
119077b52738Seric 		(*CurEnv->e_putbody)(f, ProgMailer, CurEnv);
119177b52738Seric 		putline("\n", f, ProgMailer);
1192db8841e9Seric 		(void) fclose(f);
119332d19d43Seric 		(void) fflush(stdout);
1194e36b99e2Seric 
119527628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1196c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
119732d19d43Seric 		exit(EX_OK);
119813bbc08cSeric 		/*NOTREACHED*/
119932d19d43Seric 	}
120032d19d43Seric 	else
120132d19d43Seric 	{
120232d19d43Seric 		/* parent -- wait for exit status */
1203588cad61Seric 		int st;
120432d19d43Seric 
1205588cad61Seric 		st = waitfor(pid);
1206588cad61Seric 		if ((st & 0377) != 0)
1207588cad61Seric 			return (EX_UNAVAILABLE);
1208588cad61Seric 		else
1209588cad61Seric 			return ((st >> 8) & 0377);
121032d19d43Seric 	}
121125a99e2eSeric }
1212ea4dc939Seric /*
1213ea4dc939Seric **  SENDALL -- actually send all the messages.
1214ea4dc939Seric **
1215ea4dc939Seric **	Parameters:
12160c52a0b3Seric **		e -- the envelope to send.
12177b95031aSeric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
12187b95031aSeric **			the current SendMode.
1219ea4dc939Seric **
1220ea4dc939Seric **	Returns:
1221ea4dc939Seric **		none.
1222ea4dc939Seric **
1223ea4dc939Seric **	Side Effects:
1224ea4dc939Seric **		Scans the send lists and sends everything it finds.
12250c52a0b3Seric **		Delivers any appropriate error messages.
1226276723a8Seric **		If we are running in a non-interactive mode, takes the
1227276723a8Seric **			appropriate action.
1228ea4dc939Seric */
1229ea4dc939Seric 
1230276723a8Seric sendall(e, mode)
12310c52a0b3Seric 	ENVELOPE *e;
1232276723a8Seric 	char mode;
1233ea4dc939Seric {
1234e77e673fSeric 	register ADDRESS *q;
123514a8ed7aSeric 	bool oldverbose;
1236276723a8Seric 	int pid;
1237ea4dc939Seric 
12387b95031aSeric 	/* determine actual delivery mode */
12397b95031aSeric 	if (mode == SM_DEFAULT)
12407b95031aSeric 	{
12417b95031aSeric 		extern int QueueLA;
12427b95031aSeric 
12437b95031aSeric 		if (getla() > QueueLA)
12447b95031aSeric 			mode = SM_QUEUE;
12457b95031aSeric 		else
12467b95031aSeric 			mode = SendMode;
12477b95031aSeric 	}
12487b95031aSeric 
1249772e6e50Seric #ifdef DEBUG
1250df864a8fSeric 	if (tTd(13, 1))
1251772e6e50Seric 	{
1252276723a8Seric 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
12530c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1254772e6e50Seric 	}
1255772e6e50Seric #endif DEBUG
1256ea4dc939Seric 
12570c52a0b3Seric 	/*
1258276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1259588cad61Seric 	**	Check to make sure the hop count is reasonable.
1260588cad61Seric 	**	Delete sends to the sender in mailing lists.
1261276723a8Seric 	*/
1262276723a8Seric 
1263588cad61Seric 	CurEnv = e;
1264276723a8Seric 
1265588cad61Seric 	if (e->e_hopcount > MAXHOP)
1266276723a8Seric 	{
1267588cad61Seric 		syserr("sendall: too many hops (%d max)", MAXHOP);
1268588cad61Seric 		return;
1269588cad61Seric 	}
1270588cad61Seric 
1271588cad61Seric 	if (!MeToo)
1272276723a8Seric 	{
1273f3d6c55cSeric 		extern ADDRESS *recipient();
1274f3d6c55cSeric 
1275588cad61Seric 		e->e_from.q_flags |= QDONTSEND;
1276f3d6c55cSeric 		(void) recipient(&e->e_from, &e->e_sendqueue);
1277276723a8Seric 	}
1278588cad61Seric 
1279588cad61Seric # ifdef QUEUE
1280b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1281b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1282b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
1283588cad61Seric 		queueup(e, TRUE, mode == SM_QUEUE);
1284276723a8Seric #endif QUEUE
1285276723a8Seric 
1286276723a8Seric 	oldverbose = Verbose;
1287276723a8Seric 	switch (mode)
1288276723a8Seric 	{
1289276723a8Seric 	  case SM_VERIFY:
1290276723a8Seric 		Verbose = TRUE;
1291276723a8Seric 		break;
1292276723a8Seric 
1293276723a8Seric 	  case SM_QUEUE:
1294b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1295276723a8Seric 		return;
1296276723a8Seric 
1297276723a8Seric 	  case SM_FORK:
12989a6a5f55Seric 		if (e->e_xfp != NULL)
12999a6a5f55Seric 			(void) fflush(e->e_xfp);
1300276723a8Seric 		pid = fork();
1301276723a8Seric 		if (pid < 0)
1302276723a8Seric 		{
1303276723a8Seric 			mode = SM_DELIVER;
1304276723a8Seric 			break;
1305276723a8Seric 		}
1306276723a8Seric 		else if (pid > 0)
1307a6fce3d8Seric 		{
1308a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1309b254bcb6Seric 			e->e_id = e->e_df = NULL;
1310276723a8Seric 			return;
1311a6fce3d8Seric 		}
1312276723a8Seric 
1313276723a8Seric 		/* double fork to avoid zombies */
1314276723a8Seric 		if (fork() > 0)
1315276723a8Seric 			exit(EX_OK);
1316276723a8Seric 
1317a6fce3d8Seric 		/* be sure we are immune from the terminal */
1318769e215aSeric 		disconnect(FALSE);
1319a6fce3d8Seric 
1320276723a8Seric 		break;
1321276723a8Seric 	}
1322276723a8Seric 
1323276723a8Seric 	/*
13240c52a0b3Seric 	**  Run through the list and send everything.
13250c52a0b3Seric 	*/
13260c52a0b3Seric 
13270c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1328ea4dc939Seric 	{
1329276723a8Seric 		if (mode == SM_VERIFY)
1330ea4dc939Seric 		{
1331a6fce3d8Seric 			e->e_to = q->q_paddr;
1332e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1333ea4dc939Seric 				message(Arpa_Info, "deliverable");
1334ea4dc939Seric 		}
1335ea4dc939Seric 		else
1336588cad61Seric 			(void) deliver(e, q);
1337ea4dc939Seric 	}
133814a8ed7aSeric 	Verbose = oldverbose;
13390c52a0b3Seric 
13400c52a0b3Seric 	/*
13410c52a0b3Seric 	**  Now run through and check for errors.
13420c52a0b3Seric 	*/
13430c52a0b3Seric 
1344276723a8Seric 	if (mode == SM_VERIFY)
13450c52a0b3Seric 		return;
13460c52a0b3Seric 
13470c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
13480c52a0b3Seric 	{
13490c52a0b3Seric 		register ADDRESS *qq;
13500c52a0b3Seric 
1351df864a8fSeric # ifdef DEBUG
1352df864a8fSeric 		if (tTd(13, 3))
1353df864a8fSeric 		{
1354df864a8fSeric 			printf("Checking ");
1355df864a8fSeric 			printaddr(q, FALSE);
1356df864a8fSeric 		}
1357df864a8fSeric # endif DEBUG
1358df864a8fSeric 
1359b254bcb6Seric 		/* only send errors if the message failed */
1360b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1361b254bcb6Seric 			continue;
13620c52a0b3Seric 
13630c52a0b3Seric 		/* we have an address that failed -- find the parent */
13640c52a0b3Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
13650c52a0b3Seric 		{
13660c52a0b3Seric 			char obuf[MAXNAME + 6];
13670c52a0b3Seric 			extern char *aliaslookup();
13680c52a0b3Seric 
13690c52a0b3Seric 			/* we can only have owners for local addresses */
137057fc6f17Seric 			if (!bitnset(M_LOCAL, qq->q_mailer->m_flags))
13710c52a0b3Seric 				continue;
13720c52a0b3Seric 
13730c52a0b3Seric 			/* see if the owner list exists */
13740c52a0b3Seric 			(void) strcpy(obuf, "owner-");
1375cec031e3Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1376cec031e3Seric 				(void) strcat(obuf, "owner");
1377cec031e3Seric 			else
13780c52a0b3Seric 				(void) strcat(obuf, qq->q_user);
13790c52a0b3Seric 			if (aliaslookup(obuf) == NULL)
13800c52a0b3Seric 				continue;
13810c52a0b3Seric 
1382df864a8fSeric # ifdef DEBUG
1383df864a8fSeric 			if (tTd(13, 4))
1384df864a8fSeric 				printf("Errors to %s\n", obuf);
1385df864a8fSeric # endif DEBUG
1386df864a8fSeric 
13870c52a0b3Seric 			/* owner list exists -- add it to the error queue */
1388e3e4ed86Seric 			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue);
138953e3fa05Seric 			ErrorMode = EM_MAIL;
13900c52a0b3Seric 			break;
13910c52a0b3Seric 		}
13920c52a0b3Seric 
13930c52a0b3Seric 		/* if we did not find an owner, send to the sender */
13947455aa0bSeric 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1395e3e4ed86Seric 			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue);
13960c52a0b3Seric 	}
1397276723a8Seric 
1398276723a8Seric 	if (mode == SM_FORK)
1399276723a8Seric 		finis();
14000c52a0b3Seric }
1401