125a99e2eSeric # include <signal.h>
254aa2b0fSeric # include <errno.h>
3b20b3270Seric # include "sendmail.h"
4c77d1c25Seric # include <sys/stat.h>
525a99e2eSeric 
6*8557d168Seric SCCSID(@(#)deliver.c	3.141		01/04/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 */
402a6e0786Seric 	extern bool checkcompat();
415dfc646bSeric 	char *pv[MAXPV+1];
424b22ea87Seric 	char tobuf[MAXLINE-50];		/* text line of to people */
435dfc646bSeric 	char buf[MAXNAME];
446259796dSeric 	ADDRESS *ctladdr;
456259796dSeric 	extern ADDRESS *getctladdr();
4678442df3Seric 	char tfrombuf[MAXNAME];		/* translated from person */
4778442df3Seric 	extern char **prescan();
48c77d1c25Seric 	register ADDRESS *to = firstto;
49c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
50772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
515826d9d3Seric 	register int rcode;		/* response code */
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 
76317680d6Seric 	if (NoConnect && !QueueRun && bitset(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);
10678442df3Seric 	mvp = prescan(buf, '\0');
107588cad61Seric 	rewrite(mvp, 3);
108588cad61Seric 	rewrite(mvp, 1);
1093bea8136Seric 	rewrite(mvp, m->m_s_rwset);
11078442df3Seric 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
11178442df3Seric 
112588cad61Seric 	define('g', tfrombuf, e);		/* translated sender address */
113588cad61Seric 	define('h', host, e);			/* to host */
1145dfc646bSeric 	Errors = 0;
1155dfc646bSeric 	pvp = pv;
1165dfc646bSeric 	*pvp++ = m->m_argv[0];
1175dfc646bSeric 
1185dfc646bSeric 	/* insert -f or -r flag as appropriate */
1195dfc646bSeric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
1205dfc646bSeric 	{
1215dfc646bSeric 		if (bitset(M_FOPT, m->m_flags))
1225dfc646bSeric 			*pvp++ = "-f";
1235dfc646bSeric 		else
1245dfc646bSeric 			*pvp++ = "-r";
125588cad61Seric 		expand("$g", buf, &buf[sizeof buf - 1], e);
1265dfc646bSeric 		*pvp++ = newstr(buf);
1275dfc646bSeric 	}
1285dfc646bSeric 
1295dfc646bSeric 	/*
1305dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1315dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1325dfc646bSeric 	**  be one of these, and there are only a few more slots
1335dfc646bSeric 	**  in the pv after it.
1345dfc646bSeric 	*/
1355dfc646bSeric 
1365dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1375dfc646bSeric 	{
1385dfc646bSeric 		while ((p = index(p, '$')) != NULL)
1395dfc646bSeric 			if (*++p == 'u')
1405dfc646bSeric 				break;
1415dfc646bSeric 		if (p != NULL)
1425dfc646bSeric 			break;
1435dfc646bSeric 
1445dfc646bSeric 		/* this entry is safe -- go ahead and process it */
145588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
1465dfc646bSeric 		*pvp++ = newstr(buf);
1475dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1485dfc646bSeric 		{
1495dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1505dfc646bSeric 			return (-1);
1515dfc646bSeric 		}
1525dfc646bSeric 	}
153c579ef51Seric 
15433db8731Seric 	/*
15533db8731Seric 	**  If we have no substitution for the user name in the argument
15633db8731Seric 	**  list, we know that we must supply the names otherwise -- and
15733db8731Seric 	**  SMTP is the answer!!
15833db8731Seric 	*/
15933db8731Seric 
1605dfc646bSeric 	if (*mvp == NULL)
161c579ef51Seric 	{
162c579ef51Seric 		/* running SMTP */
1632c7e1b8dSeric # ifdef SMTP
164c579ef51Seric 		clever = TRUE;
165c579ef51Seric 		*pvp = NULL;
1662c7e1b8dSeric # else SMTP
16733db8731Seric 		/* oops!  we don't implement SMTP */
1682c7e1b8dSeric 		syserr("SMTP style mailer");
1692c7e1b8dSeric 		return (EX_SOFTWARE);
1702c7e1b8dSeric # endif SMTP
171c579ef51Seric 	}
1725dfc646bSeric 
1735dfc646bSeric 	/*
1745dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
1755dfc646bSeric 	**  run through our address list and append all the addresses
1765dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
1775dfc646bSeric 	**  always send another copy later.
1785dfc646bSeric 	*/
1795dfc646bSeric 
1805dfc646bSeric 	tobuf[0] = '\0';
181588cad61Seric 	e->e_to = tobuf;
1826259796dSeric 	ctladdr = NULL;
1835dfc646bSeric 	for (; to != NULL; to = to->q_next)
1845dfc646bSeric 	{
1855dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
186bea22b26Seric 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
1875dfc646bSeric 			break;
1885dfc646bSeric 
1895dfc646bSeric 		/* if already sent or not for this host, don't send */
190da2935e1Seric 		if (bitset(QDONTSEND, to->q_flags) ||
191da2935e1Seric 		    strcmp(to->q_host, host) != 0 ||
192da2935e1Seric 		    to->q_mailer != firstto->q_mailer)
1935dfc646bSeric 			continue;
1946259796dSeric 
1954b22ea87Seric 		/* avoid overflowing tobuf */
196588cad61Seric 		if (sizeof tobuf - (strlen(to->q_paddr) + strlen(tobuf) + 2) < 0)
1974b22ea87Seric 			break;
1984b22ea87Seric 
199772e6e50Seric # ifdef DEBUG
2006ef48975Seric 		if (tTd(10, 1))
201772e6e50Seric 		{
202772e6e50Seric 			printf("\nsend to ");
203772e6e50Seric 			printaddr(to, FALSE);
204772e6e50Seric 		}
205772e6e50Seric # endif DEBUG
206772e6e50Seric 
2076259796dSeric 		/* compute effective uid/gid when sending */
2087da1035fSeric 		if (to->q_mailer == ProgMailer)
2096259796dSeric 			ctladdr = getctladdr(to);
2106259796dSeric 
2115dfc646bSeric 		user = to->q_user;
212588cad61Seric 		e->e_to = to->q_paddr;
2135dfc646bSeric 		to->q_flags |= QDONTSEND;
2145dfc646bSeric 
2155dfc646bSeric 		/*
2165dfc646bSeric 		**  Check to see that these people are allowed to
2175dfc646bSeric 		**  talk to each other.
2182a6e0786Seric 		*/
2192a6e0786Seric 
2202a6e0786Seric 		if (!checkcompat(to))
2215dfc646bSeric 		{
222198d9be0Seric 			giveresponse(EX_UNAVAILABLE, m, e);
2235dfc646bSeric 			continue;
2245dfc646bSeric 		}
2252a6e0786Seric 
2262a6e0786Seric 		/*
2279ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2289ec9501bSeric 		**	about them.
22925a99e2eSeric 		*/
23025a99e2eSeric 
2312a6e0786Seric 		if (bitset(M_STRIPQ, m->m_flags))
23225a99e2eSeric 		{
2339ec9501bSeric 			stripquotes(user, TRUE);
2349ec9501bSeric 			stripquotes(host, TRUE);
2359ec9501bSeric 		}
2369ec9501bSeric 		else
2379ec9501bSeric 		{
2389ec9501bSeric 			stripquotes(user, FALSE);
2399ec9501bSeric 			stripquotes(host, FALSE);
24025a99e2eSeric 		}
24125a99e2eSeric 
242cdb828c5Seric 		/* hack attack -- delivermail compatibility */
243cdb828c5Seric 		if (m == ProgMailer && *user == '|')
244cdb828c5Seric 			user++;
245cdb828c5Seric 
24625a99e2eSeric 		/*
2473efaed6eSeric 		**  If an error message has already been given, don't
2483efaed6eSeric 		**	bother to send to this address.
2493efaed6eSeric 		**
2503efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2513efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2523efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2533efaed6eSeric 		*/
2543efaed6eSeric 
2556cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2563efaed6eSeric 			continue;
2573efaed6eSeric 
258f2fec898Seric 		/* save statistics.... */
259588cad61Seric 		markstats(e, to);
260f2fec898Seric 
2613efaed6eSeric 		/*
26225a99e2eSeric 		**  See if this user name is "special".
26325a99e2eSeric 		**	If the user name has a slash in it, assume that this
26451552439Seric 		**	is a file -- send it off without further ado.  Note
26551552439Seric 		**	that this type of addresses is not processed along
26651552439Seric 		**	with the others, so we fudge on the To person.
26725a99e2eSeric 		*/
26825a99e2eSeric 
2697da1035fSeric 		if (m == LocalMailer)
27025a99e2eSeric 		{
271a49f24c0Seric 			if (user[0] == '/')
27225a99e2eSeric 			{
2735826d9d3Seric 				rcode = mailfile(user, getctladdr(to));
274198d9be0Seric 				giveresponse(rcode, m, e);
2755dfc646bSeric 				continue;
27625a99e2eSeric 			}
27725a99e2eSeric 		}
27825a99e2eSeric 
27913bbc08cSeric 		/*
28013bbc08cSeric 		**  Address is verified -- add this user to mailer
28113bbc08cSeric 		**  argv, and add it to the print list of recipients.
28213bbc08cSeric 		*/
28313bbc08cSeric 
284508daeccSeric 		/* link together the chain of recipients */
285508daeccSeric 		to->q_tchain = tochain;
286508daeccSeric 		tochain = to;
287508daeccSeric 
2885dfc646bSeric 		/* create list of users for error messages */
289db8841e9Seric 		(void) strcat(tobuf, ",");
290db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
291588cad61Seric 		define('u', user, e);		/* to user */
292588cad61Seric 		define('z', to->q_home, e);	/* user's home */
2935dfc646bSeric 
294c579ef51Seric 		/*
295508daeccSeric 		**  Expand out this user into argument list.
296c579ef51Seric 		*/
297c579ef51Seric 
298508daeccSeric 		if (!clever)
299c579ef51Seric 		{
300588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
3015dfc646bSeric 			*pvp++ = newstr(buf);
3025dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3035dfc646bSeric 			{
3045dfc646bSeric 				/* allow some space for trailing parms */
3055dfc646bSeric 				break;
3065dfc646bSeric 			}
3075dfc646bSeric 		}
308c579ef51Seric 	}
3095dfc646bSeric 
310145b49b1Seric 	/* see if any addresses still exist */
311145b49b1Seric 	if (tobuf[0] == '\0')
312c579ef51Seric 	{
313588cad61Seric 		define('g', (char *) NULL, e);
314145b49b1Seric 		return (0);
315c579ef51Seric 	}
316145b49b1Seric 
3175dfc646bSeric 	/* print out messages as full list */
31863780dbdSeric 	e->e_to = tobuf + 1;
3195dfc646bSeric 
3205dfc646bSeric 	/*
3215dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3225dfc646bSeric 	*/
3235dfc646bSeric 
324c579ef51Seric 	while (!clever && *++mvp != NULL)
3255dfc646bSeric 	{
326588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
3275dfc646bSeric 		*pvp++ = newstr(buf);
3285dfc646bSeric 		if (pvp >= &pv[MAXPV])
3295dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
3305dfc646bSeric 	}
3315dfc646bSeric 	*pvp++ = NULL;
3325dfc646bSeric 
33325a99e2eSeric 	/*
33425a99e2eSeric 	**  Call the mailer.
3356328bdf7Seric 	**	The argument vector gets built, pipes
33625a99e2eSeric 	**	are created as necessary, and we fork & exec as
3376328bdf7Seric 	**	appropriate.
338c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
33925a99e2eSeric 	*/
34025a99e2eSeric 
341588cad61Seric 	message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
342588cad61Seric 
3436259796dSeric 	if (ctladdr == NULL)
344588cad61Seric 		ctladdr = &e->e_from;
3452c7e1b8dSeric # ifdef SMTP
346c579ef51Seric 	if (clever)
347c579ef51Seric 	{
348588cad61Seric 		/* send the initial SMTP protocol */
349588cad61Seric 		rcode = smtpinit(m, pv, (ADDRESS *) NULL);
350588cad61Seric 
35163780dbdSeric 		if (rcode == EX_OK)
35263780dbdSeric 		{
353588cad61Seric 			/* send the recipient list */
35463780dbdSeric 			tobuf[0] = '\0';
355588cad61Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
356588cad61Seric 			{
357588cad61Seric 				int i;
358588cad61Seric 
35963780dbdSeric 				e->e_to = to->q_paddr;
360588cad61Seric 				i = smtprcpt(to);
361588cad61Seric 				if (i != EX_OK)
362588cad61Seric 				{
36383b7ddc9Seric 					markfailure(e, to, i);
364198d9be0Seric 					giveresponse(i, m, e);
36563780dbdSeric 				}
36663780dbdSeric 				else
36763780dbdSeric 				{
36863780dbdSeric 					strcat(tobuf, ",");
36963780dbdSeric 					strcat(tobuf, to->q_paddr);
370588cad61Seric 				}
371588cad61Seric 			}
372588cad61Seric 
37363780dbdSeric 			/* now send the data */
37463780dbdSeric 			if (tobuf[0] == '\0')
37563780dbdSeric 				e->e_to = NULL;
37663780dbdSeric 			else
37763780dbdSeric 			{
37863780dbdSeric 				e->e_to = tobuf + 1;
379588cad61Seric 				rcode = smtpfinish(m, e);
38063780dbdSeric 			}
38163780dbdSeric 
38263780dbdSeric 			/* now close the connection */
38363780dbdSeric 			smtpquit(pv[0]);
38463780dbdSeric 		}
385c579ef51Seric 	}
386c579ef51Seric 	else
3872c7e1b8dSeric # endif SMTP
388ddf9b743Seric 		rcode = sendoff(e, m, pv, ctladdr, FALSE);
3895dfc646bSeric 
390c77d1c25Seric 	/*
39163780dbdSeric 	**  Do final status disposal.
39263780dbdSeric 	**	We check for something in tobuf for the SMTP case.
393c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
394c77d1c25Seric 	**		addressees.
395c77d1c25Seric 	*/
396c77d1c25Seric 
39763780dbdSeric 	if (tobuf[0] != '\0')
398198d9be0Seric 		giveresponse(rcode, m, e);
39963780dbdSeric 	if (rcode != EX_OK)
400c77d1c25Seric 	{
401772e6e50Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
40283b7ddc9Seric 			markfailure(e, to, rcode);
403c77d1c25Seric 	}
404c77d1c25Seric 
40535490626Seric 	errno = 0;
406588cad61Seric 	define('g', (char *) NULL, e);
4075826d9d3Seric 	return (rcode);
40825a99e2eSeric }
4095dfc646bSeric /*
41083b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
41183b7ddc9Seric **
41283b7ddc9Seric **	Parameters:
41383b7ddc9Seric **		e -- the envelope we are sending.
41483b7ddc9Seric **		q -- the address to mark.
41583b7ddc9Seric **		rcode -- the code signifying the particular failure.
41683b7ddc9Seric **
41783b7ddc9Seric **	Returns:
41883b7ddc9Seric **		none.
41983b7ddc9Seric **
42083b7ddc9Seric **	Side Effects:
42183b7ddc9Seric **		marks the address (and possibly the envelope) with the
42283b7ddc9Seric **			failure so that an error will be returned or
42383b7ddc9Seric **			the message will be queued, as appropriate.
42483b7ddc9Seric */
42583b7ddc9Seric 
42683b7ddc9Seric markfailure(e, q, rcode)
42783b7ddc9Seric 	register ENVELOPE *e;
42883b7ddc9Seric 	register ADDRESS *q;
42983b7ddc9Seric 	int rcode;
43083b7ddc9Seric {
43183b7ddc9Seric 	if (rcode == EX_OK)
43283b7ddc9Seric 		return;
43383b7ddc9Seric 	else if (rcode != EX_TEMPFAIL)
43483b7ddc9Seric 		q->q_flags |= QBADADDR;
43583b7ddc9Seric 	else if (curtime() > e->e_ctime + TimeOut)
43683b7ddc9Seric 	{
43783b7ddc9Seric 		extern char *pintvl();
438198d9be0Seric 		char buf[MAXLINE];
43983b7ddc9Seric 
44083b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
441198d9be0Seric 		{
442198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
44383b7ddc9Seric 				pintvl(TimeOut, FALSE));
444198d9be0Seric 			if (e->e_message != NULL)
445198d9be0Seric 				free(e->e_message);
446198d9be0Seric 			e->e_message = newstr(buf);
447198d9be0Seric 			message(Arpa_Info, buf);
448198d9be0Seric 		}
44983b7ddc9Seric 		q->q_flags |= QBADADDR;
45083b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
45183b7ddc9Seric 	}
45283b7ddc9Seric 	else
45383b7ddc9Seric 		q->q_flags |= QQUEUEUP;
45483b7ddc9Seric }
45583b7ddc9Seric /*
45632d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
45732d19d43Seric **
45832d19d43Seric **	This MUST be a macro, since after a vfork we are running
45932d19d43Seric **	two processes on the same stack!!!
46032d19d43Seric **
46132d19d43Seric **	Parameters:
46232d19d43Seric **		none.
46332d19d43Seric **
46432d19d43Seric **	Returns:
46532d19d43Seric **		From a macro???  You've got to be kidding!
46632d19d43Seric **
46732d19d43Seric **	Side Effects:
46832d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
46932d19d43Seric **			pid of child in parent, zero in child.
47032d19d43Seric **			-1 on unrecoverable error.
47132d19d43Seric **
47232d19d43Seric **	Notes:
47332d19d43Seric **		I'm awfully sorry this looks so awful.  That's
47432d19d43Seric **		vfork for you.....
47532d19d43Seric */
47632d19d43Seric 
47732d19d43Seric # define NFORKTRIES	5
4784300ddf0Seric # ifdef VMUNIX
47932d19d43Seric # define XFORK	vfork
4804300ddf0Seric # else VMUNIX
48132d19d43Seric # define XFORK	fork
4824300ddf0Seric # endif VMUNIX
48332d19d43Seric 
48432d19d43Seric # define DOFORK(fORKfN) \
48532d19d43Seric {\
48632d19d43Seric 	register int i;\
48732d19d43Seric \
48832d19d43Seric 	for (i = NFORKTRIES; i-- > 0; )\
48932d19d43Seric 	{\
49032d19d43Seric 		pid = fORKfN();\
49132d19d43Seric 		if (pid >= 0)\
49232d19d43Seric 			break;\
4935e663df1Seric 		sleep(NFORKTRIES - i);\
49432d19d43Seric 	}\
49532d19d43Seric }
49632d19d43Seric /*
4972ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
4982ed72599Seric **
4992ed72599Seric **	Parameters:
5002ed72599Seric **		none.
5012ed72599Seric **
5022ed72599Seric **	Returns:
5032ed72599Seric **		pid of child in parent.
5042ed72599Seric **		zero in child.
5052ed72599Seric **		-1 on error.
5062ed72599Seric **
5072ed72599Seric **	Side Effects:
5082ed72599Seric **		returns twice, once in parent and once in child.
5092ed72599Seric */
5102ed72599Seric 
5112ed72599Seric dofork()
5122ed72599Seric {
5132ed72599Seric 	register int pid;
5142ed72599Seric 
5152ed72599Seric 	DOFORK(fork);
5162ed72599Seric 	return (pid);
5172ed72599Seric }
5182ed72599Seric /*
5195dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
5205dfc646bSeric **
5215dfc646bSeric **	Parameters:
522588cad61Seric **		e -- the envelope to mail.
5235dfc646bSeric **		m -- mailer descriptor.
5245dfc646bSeric **		pvp -- parameter vector to send to it.
5256259796dSeric **		ctladdr -- an address pointer controlling the
5266259796dSeric **			user/groupid etc. of the mailer.
527ddf9b743Seric **		crlf -- set if we want CRLF on the end of lines.
5285dfc646bSeric **
5295dfc646bSeric **	Returns:
5305dfc646bSeric **		exit status of mailer.
5315dfc646bSeric **
5325dfc646bSeric **	Side Effects:
5335dfc646bSeric **		none.
5345dfc646bSeric */
5355dfc646bSeric 
536ddf9b743Seric sendoff(e, m, pvp, ctladdr, crlf)
537588cad61Seric 	register ENVELOPE *e;
538588cad61Seric 	MAILER *m;
5395dfc646bSeric 	char **pvp;
5406259796dSeric 	ADDRESS *ctladdr;
541ddf9b743Seric 	bool crlf;
5425dfc646bSeric {
543c579ef51Seric 	auto FILE *mfile;
544c579ef51Seric 	auto FILE *rfile;
5455dfc646bSeric 	register int i;
546c579ef51Seric 	int pid;
547c579ef51Seric 
548c579ef51Seric 	/*
549c579ef51Seric 	**  Create connection to mailer.
550c579ef51Seric 	*/
551c579ef51Seric 
552c579ef51Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
553c579ef51Seric 	if (pid < 0)
554c579ef51Seric 		return (-1);
555c579ef51Seric 
556c579ef51Seric 	/*
557c579ef51Seric 	**  Format and send message.
558588cad61Seric 	**	We ignore broken pipes so that the mailer need not read
559588cad61Seric 	**	its input if it is not convenient to do so (e.g., on
560588cad61Seric 	**	some error).
561c579ef51Seric 	*/
562c579ef51Seric 
563c579ef51Seric 	(void) signal(SIGPIPE, SIG_IGN);
564ddf9b743Seric 	putfromline(mfile, m, crlf);
565ddf9b743Seric 	(*e->e_puthdr)(mfile, m, e, crlf);
56651552439Seric 	fprintf(mfile, "\n");
567ddf9b743Seric 	(*e->e_putbody)(mfile, m, FALSE, e, crlf);
568c579ef51Seric 	(void) fclose(mfile);
569c579ef51Seric 
570c579ef51Seric 	i = endmailer(pid, pvp[0]);
571bc6e2962Seric 
572bc6e2962Seric 	/* arrange a return receipt if requested */
573588cad61Seric 	if (e->e_receiptto != NULL && bitset(M_LOCAL, m->m_flags))
574bc6e2962Seric 	{
575588cad61Seric 		e->e_flags |= EF_SENDRECEIPT;
576bc6e2962Seric 		/* do we want to send back more info? */
577bc6e2962Seric 	}
578bc6e2962Seric 
579c579ef51Seric 	return (i);
580c579ef51Seric }
581c579ef51Seric /*
582c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
583c579ef51Seric **
584c579ef51Seric **	We should never get fatal errors (e.g., segmentation
585c579ef51Seric **	violation), so we report those specially.  For other
586c579ef51Seric **	errors, we choose a status message (into statmsg),
587c579ef51Seric **	and if it represents an error, we print it.
588c579ef51Seric **
589c579ef51Seric **	Parameters:
590c579ef51Seric **		pid -- pid of mailer.
591c579ef51Seric **		name -- name of mailer (for error messages).
592c579ef51Seric **
593c579ef51Seric **	Returns:
594c579ef51Seric **		exit code of mailer.
595c579ef51Seric **
596c579ef51Seric **	Side Effects:
597c579ef51Seric **		none.
598c579ef51Seric */
599c579ef51Seric 
600c579ef51Seric endmailer(pid, name)
601c579ef51Seric 	int pid;
602c579ef51Seric 	char *name;
603c579ef51Seric {
604588cad61Seric 	int st;
605c579ef51Seric 
60633db8731Seric 	/* in the IPC case there is nothing to wait for */
60733db8731Seric 	if (pid == 0)
60833db8731Seric 		return (EX_OK);
60933db8731Seric 
61033db8731Seric 	/* wait for the mailer process to die and collect status */
611588cad61Seric 	st = waitfor(pid);
612588cad61Seric 	if (st == -1)
61378de67c1Seric 	{
614588cad61Seric 		syserr("endmailer %s: wait", name);
615588cad61Seric 		return (EX_SOFTWARE);
616c579ef51Seric 	}
61733db8731Seric 
61833db8731Seric 	/* see if it died a horrid death */
619c579ef51Seric 	if ((st & 0377) != 0)
620c579ef51Seric 	{
621588cad61Seric 		syserr("endmailer %s: stat %o", name, st);
622c579ef51Seric 		ExitStat = EX_UNAVAILABLE;
623588cad61Seric 		return (EX_UNAVAILABLE);
624c579ef51Seric 	}
62533db8731Seric 
62633db8731Seric 	/* normal death -- return status */
627588cad61Seric 	st = (st >> 8) & 0377;
628588cad61Seric 	return (st);
629c579ef51Seric }
630c579ef51Seric /*
631c579ef51Seric **  OPENMAILER -- open connection to mailer.
632c579ef51Seric **
633c579ef51Seric **	Parameters:
634c579ef51Seric **		m -- mailer descriptor.
635c579ef51Seric **		pvp -- parameter vector to pass to mailer.
636c579ef51Seric **		ctladdr -- controlling address for user.
637c579ef51Seric **		clever -- create a full duplex connection.
638c579ef51Seric **		pmfile -- pointer to mfile (to mailer) connection.
639c579ef51Seric **		prfile -- pointer to rfile (from mailer) connection.
640c579ef51Seric **
641c579ef51Seric **	Returns:
64233db8731Seric **		pid of mailer ( > 0 ).
643c579ef51Seric **		-1 on error.
64433db8731Seric **		zero on an IPC connection.
645c579ef51Seric **
646c579ef51Seric **	Side Effects:
647c579ef51Seric **		creates a mailer in a subprocess.
648c579ef51Seric */
649c579ef51Seric 
650c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
651588cad61Seric 	MAILER *m;
652c579ef51Seric 	char **pvp;
653c579ef51Seric 	ADDRESS *ctladdr;
654c579ef51Seric 	bool clever;
655c579ef51Seric 	FILE **pmfile;
656c579ef51Seric 	FILE **prfile;
657c579ef51Seric {
6585dfc646bSeric 	int pid;
659f8952a83Seric 	int mpvect[2];
660c579ef51Seric 	int rpvect[2];
6615dfc646bSeric 	FILE *mfile;
662c579ef51Seric 	FILE *rfile;
6635dfc646bSeric 	extern FILE *fdopen();
6645dfc646bSeric 
6655dfc646bSeric # ifdef DEBUG
6666ef48975Seric 	if (tTd(11, 1))
6675dfc646bSeric 	{
6688c57e552Seric 		printf("openmailer:");
6695dfc646bSeric 		printav(pvp);
6705dfc646bSeric 	}
6715dfc646bSeric # endif DEBUG
67235490626Seric 	errno = 0;
6735dfc646bSeric 
67433db8731Seric 	/*
67533db8731Seric 	**  Deal with the special case of mail handled through an IPC
67633db8731Seric 	**  connection.
67733db8731Seric 	**	In this case we don't actually fork.  We must be
67833db8731Seric 	**	running SMTP for this to work.  We will return a
67933db8731Seric 	**	zero pid to indicate that we are running IPC.
68033db8731Seric 	*/
68133db8731Seric 
68233db8731Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
68333db8731Seric 	{
684588cad61Seric #ifdef DAEMON
68533db8731Seric 		register int i;
6861277f9a8Seric 		register u_short port;
68733db8731Seric 
68833db8731Seric 		if (!clever)
68933db8731Seric 			syserr("non-clever IPC");
69093b6e3cfSeric 		if (pvp[2] != NULL)
6911277f9a8Seric 			port = atoi(pvp[2]);
69293b6e3cfSeric 		else
6931277f9a8Seric 			port = 0;
6941277f9a8Seric 		i = makeconnection(pvp[1], port, pmfile, prfile);
69533db8731Seric 		if (i != EX_OK)
696ed854c7bSeric 		{
697ed854c7bSeric 			ExitStat = i;
69833db8731Seric 			return (-1);
699ed854c7bSeric 		}
70033db8731Seric 		else
70133db8731Seric 			return (0);
702588cad61Seric #else DAEMON
703588cad61Seric 		syserr("openmailer: no IPC");
704588cad61Seric 		return (-1);
70533db8731Seric #endif DAEMON
706588cad61Seric 	}
70733db8731Seric 
7086328bdf7Seric 	/* create a pipe to shove the mail through */
709f8952a83Seric 	if (pipe(mpvect) < 0)
71025a99e2eSeric 	{
711588cad61Seric 		syserr("openmailer: pipe (to mailer)");
71225a99e2eSeric 		return (-1);
71325a99e2eSeric 	}
714c579ef51Seric 
7152c7e1b8dSeric #ifdef SMTP
716c579ef51Seric 	/* if this mailer speaks smtp, create a return pipe */
717c579ef51Seric 	if (clever && pipe(rpvect) < 0)
718c579ef51Seric 	{
719588cad61Seric 		syserr("openmailer: pipe (from mailer)");
720c579ef51Seric 		(void) close(mpvect[0]);
721c579ef51Seric 		(void) close(mpvect[1]);
722c579ef51Seric 		return (-1);
723c579ef51Seric 	}
7242c7e1b8dSeric #endif SMTP
725c579ef51Seric 
72633db8731Seric 	/*
72733db8731Seric 	**  Actually fork the mailer process.
72833db8731Seric 	**	DOFORK is clever about retrying.
72933db8731Seric 	*/
73033db8731Seric 
7319a6a5f55Seric 	if (CurEnv->e_xfp != NULL)
7329a6a5f55Seric 		(void) fflush(CurEnv->e_xfp);		/* for debugging */
733588cad61Seric 	(void) fflush(stdout);
73432d19d43Seric 	DOFORK(XFORK);
735f129ec7dSeric 	/* pid is set by DOFORK */
73625a99e2eSeric 	if (pid < 0)
73725a99e2eSeric 	{
73833db8731Seric 		/* failure */
739588cad61Seric 		syserr("openmailer: cannot fork");
740f8952a83Seric 		(void) close(mpvect[0]);
741f8952a83Seric 		(void) close(mpvect[1]);
742588cad61Seric #ifdef SMTP
743c579ef51Seric 		if (clever)
744c579ef51Seric 		{
745c579ef51Seric 			(void) close(rpvect[0]);
746c579ef51Seric 			(void) close(rpvect[1]);
747c579ef51Seric 		}
748588cad61Seric #endif SMTP
74925a99e2eSeric 		return (-1);
75025a99e2eSeric 	}
75125a99e2eSeric 	else if (pid == 0)
75225a99e2eSeric 	{
75325a99e2eSeric 		/* child -- set up input & exec mailer */
75403ab8e55Seric 		/* make diagnostic output be standard output */
7558f0e7860Seric 		(void) signal(SIGINT, SIG_IGN);
7568f0e7860Seric 		(void) signal(SIGHUP, SIG_IGN);
7570984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
758f8952a83Seric 
759f8952a83Seric 		/* arrange to filter standard & diag output of command */
760c579ef51Seric 		if (clever)
761c579ef51Seric 		{
762c579ef51Seric 			(void) close(rpvect[0]);
763c579ef51Seric 			(void) close(1);
764c579ef51Seric 			(void) dup(rpvect[1]);
765c579ef51Seric 			(void) close(rpvect[1]);
766c579ef51Seric 		}
767276723a8Seric 		else if (OpMode == MD_SMTP || HoldErrs)
768f8952a83Seric 		{
769588cad61Seric 			/* put mailer output in transcript */
770f8952a83Seric 			(void) close(1);
7719a6a5f55Seric 			(void) dup(fileno(CurEnv->e_xfp));
772f8952a83Seric 		}
773db8841e9Seric 		(void) close(2);
774db8841e9Seric 		(void) dup(1);
775f8952a83Seric 
776f8952a83Seric 		/* arrange to get standard input */
777f8952a83Seric 		(void) close(mpvect[1]);
778db8841e9Seric 		(void) close(0);
779f8952a83Seric 		if (dup(mpvect[0]) < 0)
78025a99e2eSeric 		{
78125a99e2eSeric 			syserr("Cannot dup to zero!");
782a590b978Seric 			_exit(EX_OSERR);
78325a99e2eSeric 		}
784f8952a83Seric 		(void) close(mpvect[0]);
7852a6e0786Seric 		if (!bitset(M_RESTR, m->m_flags))
7860984da9fSeric 		{
787e36b99e2Seric 			if (ctladdr->q_uid == 0)
788e36b99e2Seric 			{
789e36b99e2Seric 				(void) setgid(DefGid);
790e36b99e2Seric 				(void) setuid(DefUid);
791e36b99e2Seric 			}
792e36b99e2Seric 			else
79369f29479Seric 			{
794e36b99e2Seric 				(void) setgid(ctladdr->q_gid);
795e36b99e2Seric 				(void) setuid(ctladdr->q_uid);
79669f29479Seric 			}
7970984da9fSeric 		}
798588cad61Seric 
799e374fd72Seric 		/*
800e374fd72Seric 		**  We have to be careful with vfork - we can't mung up the
801e374fd72Seric 		**  memory but we don't want the mailer to inherit any extra
802e374fd72Seric 		**  open files.  Chances are the mailer won't
803e374fd72Seric 		**  care about an extra file, but then again you never know.
804e374fd72Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
805e374fd72Seric 		**  declared static so we can't.  But if we fclose(pwf), which
806e374fd72Seric 		**  is what endpwent does, it closes it in the parent too and
807e374fd72Seric 		**  the next getpwnam will be slower.  If you have a weird
808e374fd72Seric 		**  mailer that chokes on the extra file you should do the
8094300ddf0Seric 		**  endpwent().			-MRH
810e374fd72Seric 		**
811e374fd72Seric 		**  Similar comments apply to log.  However, openlog is
812e374fd72Seric 		**  clever enough to set the FIOCLEX mode on the file,
813e374fd72Seric 		**  so it will be closed automatically on the exec.
814e374fd72Seric 		*/
815e374fd72Seric 
816588cad61Seric 		closeall();
81733db8731Seric 
81833db8731Seric 		/* try to execute the mailer */
81925a99e2eSeric 		execv(m->m_mailer, pvp);
82033db8731Seric 
82125a99e2eSeric 		/* syserr fails because log is closed */
82225a99e2eSeric 		/* syserr("Cannot exec %s", m->m_mailer); */
82332d19d43Seric 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
824db8841e9Seric 		(void) fflush(stdout);
825a590b978Seric 		_exit(EX_UNAVAILABLE);
82625a99e2eSeric 	}
82725a99e2eSeric 
828f8952a83Seric 	/*
829c579ef51Seric 	**  Set up return value.
830f8952a83Seric 	*/
831f8952a83Seric 
832f8952a83Seric 	(void) close(mpvect[0]);
833f8952a83Seric 	mfile = fdopen(mpvect[1], "w");
834c579ef51Seric 	if (clever)
83525a99e2eSeric 	{
836c579ef51Seric 		(void) close(rpvect[1]);
837c579ef51Seric 		rfile = fdopen(rpvect[0], "r");
83825a99e2eSeric 	}
839c579ef51Seric 
840c579ef51Seric 	*pmfile = mfile;
841c579ef51Seric 	*prfile = rfile;
842c579ef51Seric 
843c579ef51Seric 	return (pid);
84425a99e2eSeric }
84525a99e2eSeric /*
84625a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
84725a99e2eSeric **
84825a99e2eSeric **	Parameters:
84925a99e2eSeric **		stat -- the status code from the mailer (high byte
85025a99e2eSeric **			only; core dumps must have been taken care of
85125a99e2eSeric **			already).
85225a99e2eSeric **		m -- the mailer descriptor for this mailer.
85325a99e2eSeric **
85425a99e2eSeric **	Returns:
855db8841e9Seric **		none.
85625a99e2eSeric **
85725a99e2eSeric **	Side Effects:
858c1f9df2cSeric **		Errors may be incremented.
85925a99e2eSeric **		ExitStat may be set.
86025a99e2eSeric */
86125a99e2eSeric 
862588cad61Seric /*ARGSUSED*/
863198d9be0Seric giveresponse(stat, m, e)
86425a99e2eSeric 	int stat;
865588cad61Seric 	register MAILER *m;
866198d9be0Seric 	ENVELOPE *e;
86725a99e2eSeric {
86825a99e2eSeric 	register char *statmsg;
86925a99e2eSeric 	extern char *SysExMsg[];
87025a99e2eSeric 	register int i;
87125a99e2eSeric 	extern int N_SysEx;
872198d9be0Seric 	char buf[MAXLINE];
87325a99e2eSeric 
87413bbc08cSeric 	/*
87513bbc08cSeric 	**  Compute status message from code.
87613bbc08cSeric 	*/
87713bbc08cSeric 
87825a99e2eSeric 	i = stat - EX__BASE;
879588cad61Seric 	if (stat == 0)
880588cad61Seric 		statmsg = "250 Sent";
881588cad61Seric 	else if (i < 0 || i > N_SysEx)
882588cad61Seric 	{
883588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
884588cad61Seric 		stat = EX_UNAVAILABLE;
885588cad61Seric 		statmsg = buf;
886588cad61Seric 	}
887198d9be0Seric 	else if (stat == EX_TEMPFAIL)
888198d9be0Seric 	{
889198d9be0Seric 		extern char *sys_errlist[];
890198d9be0Seric 		extern int sys_nerr;
891198d9be0Seric 
892*8557d168Seric 		(void) strcpy(buf, SysExMsg[i]);
893*8557d168Seric 		if (errno != 0)
894198d9be0Seric 		{
895*8557d168Seric 			(void) strcat(buf, ": ");
896*8557d168Seric 			if (errno > 0 && errno < sys_nerr)
897198d9be0Seric 				(void) strcat(buf, sys_errlist[errno]);
898198d9be0Seric 			else
899*8557d168Seric 			{
900*8557d168Seric 				char xbuf[30];
901*8557d168Seric 
902*8557d168Seric 				(void) sprintf(xbuf, "Error %d", errno);
903*8557d168Seric 				(void) strcat(buf, xbuf);
904*8557d168Seric 			}
905*8557d168Seric 		}
906198d9be0Seric 		statmsg = buf;
907198d9be0Seric 	}
90825a99e2eSeric 	else
90925a99e2eSeric 		statmsg = SysExMsg[i];
910588cad61Seric 
911588cad61Seric 	/*
912588cad61Seric 	**  Print the message as appropriate
913588cad61Seric 	*/
914588cad61Seric 
915198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
9165826d9d3Seric 		message(Arpa_Info, &statmsg[4]);
91725a99e2eSeric 	else
91825a99e2eSeric 	{
919c1f9df2cSeric 		Errors++;
9205826d9d3Seric 		usrerr(statmsg);
92125a99e2eSeric 	}
92225a99e2eSeric 
92325a99e2eSeric 	/*
92425a99e2eSeric 	**  Final cleanup.
92525a99e2eSeric 	**	Log a record of the transaction.  Compute the new
92625a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
92725a99e2eSeric 	**	that.
92825a99e2eSeric 	*/
92925a99e2eSeric 
93061f5a1d4Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
931eb238f8cSeric 		logdelivery(&statmsg[4]);
932eb238f8cSeric 
933eb238f8cSeric 	if (stat != EX_TEMPFAIL)
934eb238f8cSeric 		setstat(stat);
935198d9be0Seric 	if (stat != EX_OK)
936198d9be0Seric 	{
937198d9be0Seric 		if (e->e_message != NULL)
938198d9be0Seric 			free(e->e_message);
939198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
940198d9be0Seric 	}
941*8557d168Seric 	errno = 0;
942eb238f8cSeric }
943eb238f8cSeric /*
944eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
945eb238f8cSeric **
946eb238f8cSeric **	Parameters:
947eb238f8cSeric **		stat -- the message to print for the status
948eb238f8cSeric **
949eb238f8cSeric **	Returns:
950eb238f8cSeric **		none
951eb238f8cSeric **
952eb238f8cSeric **	Side Effects:
953eb238f8cSeric **		none
954eb238f8cSeric */
955eb238f8cSeric 
956eb238f8cSeric logdelivery(stat)
957eb238f8cSeric 	char *stat;
9585cf56be3Seric {
9595cf56be3Seric 	extern char *pintvl();
9605cf56be3Seric 
961eb238f8cSeric # ifdef LOG
9625cf56be3Seric 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id,
963eb238f8cSeric 	       CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat);
96425a99e2eSeric # endif LOG
96525a99e2eSeric }
96625a99e2eSeric /*
96751552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
96825a99e2eSeric **
96951552439Seric **	This can be made an arbitrary message separator by changing $l
97051552439Seric **
97151552439Seric **	One of the ugliest hacks seen by human eyes is
97251552439Seric **	contained herein: UUCP wants those stupid
973588cad61Seric **	"emote from <host>" lines.  Why oh why does a
97451552439Seric **	well-meaning programmer such as myself have to
97551552439Seric **	deal with this kind of antique garbage????
97625a99e2eSeric **
97725a99e2eSeric **	Parameters:
97851552439Seric **		fp -- the file to output to.
97951552439Seric **		m -- the mailer describing this entry.
980ddf9b743Seric **		crlf -- set if we want a CRLF at the end of the line.
98125a99e2eSeric **
98225a99e2eSeric **	Returns:
98351552439Seric **		none
98425a99e2eSeric **
98525a99e2eSeric **	Side Effects:
98651552439Seric **		outputs some text to fp.
98725a99e2eSeric */
98825a99e2eSeric 
989ddf9b743Seric putfromline(fp, m, crlf)
99051552439Seric 	register FILE *fp;
99151552439Seric 	register MAILER *m;
99225a99e2eSeric {
99351552439Seric 	char buf[MAXLINE];
99425a99e2eSeric 
99551552439Seric 	if (bitset(M_NHDR, m->m_flags))
99651552439Seric 		return;
99713bbc08cSeric 
9982c7e1b8dSeric # ifdef UGLYUUCP
999a49f24c0Seric 	if (bitset(M_UGLYUUCP, m->m_flags))
100074b6e67bSeric 	{
100174b6e67bSeric 		extern char *macvalue();
10028c57e552Seric 		char *sys = macvalue('g', CurEnv);
100374b6e67bSeric 		char *bang = index(sys, '!');
100474b6e67bSeric 
100574b6e67bSeric 		if (bang == NULL)
100674b6e67bSeric 			syserr("No ! in UUCP! (%s)", sys);
100774b6e67bSeric 		else
1008588cad61Seric 		{
100974b6e67bSeric 			*bang = '\0';
1010518ad6b6Seric 			expand("From $f  $d remote from $g\n", buf,
101151552439Seric 					&buf[sizeof buf - 1], CurEnv);
101274b6e67bSeric 			*bang = '!';
101374b6e67bSeric 		}
1014588cad61Seric 	}
1015a36e30c9Seric 	else
10162c7e1b8dSeric # endif UGLYUUCP
101751552439Seric 		expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv);
1018ddf9b743Seric 	putline(buf, fp, crlf, bitset(M_FULLSMTP, m->m_flags));
1019bc6e2962Seric }
1020bc6e2962Seric /*
102151552439Seric **  PUTBODY -- put the body of a message.
102251552439Seric **
102351552439Seric **	Parameters:
102451552439Seric **		fp -- file to output onto.
102551552439Seric **		m -- a mailer descriptor.
102651552439Seric **		xdot -- if set, use SMTP hidden dot algorithm.
10279a6a5f55Seric **		e -- the envelope to put out.
102851552439Seric **
102951552439Seric **	Returns:
103051552439Seric **		none.
103151552439Seric **
103251552439Seric **	Side Effects:
103351552439Seric **		The message is written onto fp.
103451552439Seric */
103551552439Seric 
1036ddf9b743Seric putbody(fp, m, xdot, e, crlf)
103751552439Seric 	FILE *fp;
1038588cad61Seric 	MAILER *m;
103951552439Seric 	bool xdot;
10409a6a5f55Seric 	register ENVELOPE *e;
104151552439Seric {
104251552439Seric 	char buf[MAXLINE + 1];
1043d90b8d00Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
104451552439Seric 
104551552439Seric 	/*
104651552439Seric 	**  Output the body of the message
104751552439Seric 	*/
104851552439Seric 
10499a6a5f55Seric 	if (e->e_dfp == NULL)
105051552439Seric 	{
10519a6a5f55Seric 		if (e->e_df != NULL)
10529a6a5f55Seric 		{
10539a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
10549a6a5f55Seric 			if (e->e_dfp == NULL)
10559a6a5f55Seric 				syserr("Cannot open %s", e->e_df);
10569a6a5f55Seric 		}
10579a6a5f55Seric 		else
1058ddf9b743Seric 			putline("<<< No Message Collected >>>", fp, crlf, fullsmtp);
10599a6a5f55Seric 	}
10609a6a5f55Seric 	if (e->e_dfp != NULL)
10619a6a5f55Seric 	{
10629a6a5f55Seric 		rewind(e->e_dfp);
106351552439Seric 		buf[0] = '.';
10649a6a5f55Seric 		while (!ferror(fp) &&
10659a6a5f55Seric 		       fgets(&buf[1], sizeof buf - 1, e->e_dfp) != NULL)
10669a6a5f55Seric 		{
1067ddf9b743Seric 			putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, crlf, fullsmtp);
10689a6a5f55Seric 		}
106951552439Seric 
10709a6a5f55Seric 		if (ferror(e->e_dfp))
107151552439Seric 		{
107251552439Seric 			syserr("putbody: read error");
107351552439Seric 			ExitStat = EX_IOERR;
107451552439Seric 		}
107551552439Seric 	}
107651552439Seric 
107751552439Seric 	(void) fflush(fp);
107851552439Seric 	if (ferror(fp) && errno != EPIPE)
107951552439Seric 	{
108051552439Seric 		syserr("putbody: write error");
108151552439Seric 		ExitStat = EX_IOERR;
108251552439Seric 	}
108351552439Seric 	errno = 0;
108425a99e2eSeric }
108525a99e2eSeric /*
108625a99e2eSeric **  MAILFILE -- Send a message to a file.
108725a99e2eSeric **
1088f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1089f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1090f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1091f129ec7dSeric **	sendmail runs as root.
1092f129ec7dSeric **
1093588cad61Seric **	This could be done as a subordinate mailer, except that it
1094588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1095588cad61Seric **	view this as being sufficiently important as to include it
1096588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1097588cad61Seric **	to create another process plus some pipes to save the message.
1098588cad61Seric **
109925a99e2eSeric **	Parameters:
110025a99e2eSeric **		filename -- the name of the file to send to.
11016259796dSeric **		ctladdr -- the controlling address header -- includes
11026259796dSeric **			the userid/groupid to be when sending.
110325a99e2eSeric **
110425a99e2eSeric **	Returns:
110525a99e2eSeric **		The exit code associated with the operation.
110625a99e2eSeric **
110725a99e2eSeric **	Side Effects:
110825a99e2eSeric **		none.
110925a99e2eSeric */
111025a99e2eSeric 
11116259796dSeric mailfile(filename, ctladdr)
111225a99e2eSeric 	char *filename;
11136259796dSeric 	ADDRESS *ctladdr;
111425a99e2eSeric {
111525a99e2eSeric 	register FILE *f;
111632d19d43Seric 	register int pid;
111725a99e2eSeric 
111832d19d43Seric 	/*
111932d19d43Seric 	**  Fork so we can change permissions here.
112032d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
112132d19d43Seric 	**	the complications of calling subroutines, etc.
112232d19d43Seric 	*/
112332d19d43Seric 
112432d19d43Seric 	DOFORK(fork);
112532d19d43Seric 
112632d19d43Seric 	if (pid < 0)
112732d19d43Seric 		return (EX_OSERR);
112832d19d43Seric 	else if (pid == 0)
112932d19d43Seric 	{
113032d19d43Seric 		/* child -- actually write to file */
1131f129ec7dSeric 		struct stat stb;
1132f129ec7dSeric 
11330984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
11340984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
11350984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
1136f129ec7dSeric 		umask(OldUmask);
1137f129ec7dSeric 		if (stat(filename, &stb) < 0)
1138e6e1265fSeric 			stb.st_mode = 0666;
1139f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1140f129ec7dSeric 			exit(EX_CANTCREAT);
114103827b5fSeric 		if (ctladdr == NULL)
11427a941e7aSeric 			ctladdr = &CurEnv->e_from;
1143f129ec7dSeric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
1144e36b99e2Seric 		{
1145e36b99e2Seric 			if (ctladdr->q_uid == 0)
1146e36b99e2Seric 				(void) setgid(DefGid);
1147e36b99e2Seric 			else
11486259796dSeric 				(void) setgid(ctladdr->q_gid);
1149e36b99e2Seric 		}
1150f129ec7dSeric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
1151e36b99e2Seric 		{
1152e36b99e2Seric 			if (ctladdr->q_uid == 0)
1153e36b99e2Seric 				(void) setuid(DefUid);
1154e36b99e2Seric 			else
11556259796dSeric 				(void) setuid(ctladdr->q_uid);
1156e36b99e2Seric 		}
115727628d59Seric 		f = dfopen(filename, "a");
115825a99e2eSeric 		if (f == NULL)
115932d19d43Seric 			exit(EX_CANTCREAT);
116025a99e2eSeric 
1161ddf9b743Seric 		putfromline(f, ProgMailer, FALSE);
1162ddf9b743Seric 		(*CurEnv->e_puthdr)(f, ProgMailer, CurEnv, FALSE);
1163d90b8d00Seric 		fputs("\n", f);
1164ddf9b743Seric 		(*CurEnv->e_putbody)(f, ProgMailer, FALSE, CurEnv, FALSE);
116525a99e2eSeric 		fputs("\n", f);
1166db8841e9Seric 		(void) fclose(f);
116732d19d43Seric 		(void) fflush(stdout);
1168e36b99e2Seric 
116927628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1170c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
117132d19d43Seric 		exit(EX_OK);
117213bbc08cSeric 		/*NOTREACHED*/
117332d19d43Seric 	}
117432d19d43Seric 	else
117532d19d43Seric 	{
117632d19d43Seric 		/* parent -- wait for exit status */
1177588cad61Seric 		int st;
117832d19d43Seric 
1179588cad61Seric 		st = waitfor(pid);
1180588cad61Seric 		if ((st & 0377) != 0)
1181588cad61Seric 			return (EX_UNAVAILABLE);
1182588cad61Seric 		else
1183588cad61Seric 			return ((st >> 8) & 0377);
118432d19d43Seric 	}
118525a99e2eSeric }
1186ea4dc939Seric /*
1187ea4dc939Seric **  SENDALL -- actually send all the messages.
1188ea4dc939Seric **
1189ea4dc939Seric **	Parameters:
11900c52a0b3Seric **		e -- the envelope to send.
1191276723a8Seric **		mode -- the delivery mode to use.
1192ea4dc939Seric **
1193ea4dc939Seric **	Returns:
1194ea4dc939Seric **		none.
1195ea4dc939Seric **
1196ea4dc939Seric **	Side Effects:
1197ea4dc939Seric **		Scans the send lists and sends everything it finds.
11980c52a0b3Seric **		Delivers any appropriate error messages.
1199276723a8Seric **		If we are running in a non-interactive mode, takes the
1200276723a8Seric **			appropriate action.
1201ea4dc939Seric */
1202ea4dc939Seric 
1203276723a8Seric sendall(e, mode)
12040c52a0b3Seric 	ENVELOPE *e;
1205276723a8Seric 	char mode;
1206ea4dc939Seric {
1207e77e673fSeric 	register ADDRESS *q;
120814a8ed7aSeric 	bool oldverbose;
1209276723a8Seric 	int pid;
1210ea4dc939Seric 
1211772e6e50Seric #ifdef DEBUG
1212df864a8fSeric 	if (tTd(13, 1))
1213772e6e50Seric 	{
1214276723a8Seric 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
12150c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1216772e6e50Seric 	}
1217772e6e50Seric #endif DEBUG
1218ea4dc939Seric 
12190c52a0b3Seric 	/*
1220276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1221588cad61Seric 	**	Check to make sure the hop count is reasonable.
1222588cad61Seric 	**	Delete sends to the sender in mailing lists.
1223276723a8Seric 	*/
1224276723a8Seric 
1225588cad61Seric 	CurEnv = e;
1226276723a8Seric 
1227588cad61Seric 	if (e->e_hopcount > MAXHOP)
1228276723a8Seric 	{
1229588cad61Seric 		syserr("sendall: too many hops (%d max)", MAXHOP);
1230588cad61Seric 		return;
1231588cad61Seric 	}
1232588cad61Seric 
1233588cad61Seric 	if (!MeToo)
1234276723a8Seric 	{
1235588cad61Seric 		e->e_from.q_flags |= QDONTSEND;
1236588cad61Seric 		recipient(&e->e_from, &e->e_sendqueue);
1237276723a8Seric 	}
1238588cad61Seric 
1239588cad61Seric # ifdef QUEUE
1240b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1241b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1242b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
1243588cad61Seric 		queueup(e, TRUE, mode == SM_QUEUE);
1244276723a8Seric #endif QUEUE
1245276723a8Seric 
1246276723a8Seric 	oldverbose = Verbose;
1247276723a8Seric 	switch (mode)
1248276723a8Seric 	{
1249276723a8Seric 	  case SM_VERIFY:
1250276723a8Seric 		Verbose = TRUE;
1251276723a8Seric 		break;
1252276723a8Seric 
1253276723a8Seric 	  case SM_QUEUE:
1254b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1255276723a8Seric 		return;
1256276723a8Seric 
1257276723a8Seric 	  case SM_FORK:
12589a6a5f55Seric 		if (e->e_xfp != NULL)
12599a6a5f55Seric 			(void) fflush(e->e_xfp);
1260276723a8Seric 		pid = fork();
1261276723a8Seric 		if (pid < 0)
1262276723a8Seric 		{
1263276723a8Seric 			mode = SM_DELIVER;
1264276723a8Seric 			break;
1265276723a8Seric 		}
1266276723a8Seric 		else if (pid > 0)
1267a6fce3d8Seric 		{
1268a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1269b254bcb6Seric 			e->e_id = e->e_df = NULL;
1270276723a8Seric 			return;
1271a6fce3d8Seric 		}
1272276723a8Seric 
1273276723a8Seric 		/* double fork to avoid zombies */
1274276723a8Seric 		if (fork() > 0)
1275276723a8Seric 			exit(EX_OK);
1276276723a8Seric 
1277a6fce3d8Seric 		/* be sure we are immune from the terminal */
1278e2e30df7Seric 		disconnect();
1279a6fce3d8Seric 
1280276723a8Seric 		break;
1281276723a8Seric 	}
1282276723a8Seric 
1283276723a8Seric 	/*
12840c52a0b3Seric 	**  Run through the list and send everything.
12850c52a0b3Seric 	*/
12860c52a0b3Seric 
12870c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1288ea4dc939Seric 	{
1289276723a8Seric 		if (mode == SM_VERIFY)
1290ea4dc939Seric 		{
1291a6fce3d8Seric 			e->e_to = q->q_paddr;
1292e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1293ea4dc939Seric 				message(Arpa_Info, "deliverable");
1294ea4dc939Seric 		}
1295ea4dc939Seric 		else
1296588cad61Seric 			(void) deliver(e, q);
1297ea4dc939Seric 	}
129814a8ed7aSeric 	Verbose = oldverbose;
12990c52a0b3Seric 
13000c52a0b3Seric 	/*
13010c52a0b3Seric 	**  Now run through and check for errors.
13020c52a0b3Seric 	*/
13030c52a0b3Seric 
1304276723a8Seric 	if (mode == SM_VERIFY)
13050c52a0b3Seric 		return;
13060c52a0b3Seric 
13070c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
13080c52a0b3Seric 	{
13090c52a0b3Seric 		register ADDRESS *qq;
13100c52a0b3Seric 
1311df864a8fSeric # ifdef DEBUG
1312df864a8fSeric 		if (tTd(13, 3))
1313df864a8fSeric 		{
1314df864a8fSeric 			printf("Checking ");
1315df864a8fSeric 			printaddr(q, FALSE);
1316df864a8fSeric 		}
1317df864a8fSeric # endif DEBUG
1318df864a8fSeric 
1319b254bcb6Seric 		/* only send errors if the message failed */
1320b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1321b254bcb6Seric 			continue;
13220c52a0b3Seric 
13230c52a0b3Seric 		/* we have an address that failed -- find the parent */
13240c52a0b3Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
13250c52a0b3Seric 		{
13260c52a0b3Seric 			char obuf[MAXNAME + 6];
13270c52a0b3Seric 			extern char *aliaslookup();
13280c52a0b3Seric 
13290c52a0b3Seric 			/* we can only have owners for local addresses */
13300c52a0b3Seric 			if (!bitset(M_LOCAL, qq->q_mailer->m_flags))
13310c52a0b3Seric 				continue;
13320c52a0b3Seric 
13330c52a0b3Seric 			/* see if the owner list exists */
13340c52a0b3Seric 			(void) strcpy(obuf, "owner-");
1335cec031e3Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1336cec031e3Seric 				(void) strcat(obuf, "owner");
1337cec031e3Seric 			else
13380c52a0b3Seric 				(void) strcat(obuf, qq->q_user);
13390c52a0b3Seric 			if (aliaslookup(obuf) == NULL)
13400c52a0b3Seric 				continue;
13410c52a0b3Seric 
1342df864a8fSeric # ifdef DEBUG
1343df864a8fSeric 			if (tTd(13, 4))
1344df864a8fSeric 				printf("Errors to %s\n", obuf);
1345df864a8fSeric # endif DEBUG
1346df864a8fSeric 
13470c52a0b3Seric 			/* owner list exists -- add it to the error queue */
1348e3e4ed86Seric 			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue);
1349588cad61Seric 			ErrorMode == EM_MAIL;
13500c52a0b3Seric 			break;
13510c52a0b3Seric 		}
13520c52a0b3Seric 
13530c52a0b3Seric 		/* if we did not find an owner, send to the sender */
13547455aa0bSeric 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1355e3e4ed86Seric 			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue);
13560c52a0b3Seric 	}
1357276723a8Seric 
1358276723a8Seric 	if (mode == SM_FORK)
1359276723a8Seric 		finis();
13600c52a0b3Seric }
1361