125a99e2eSeric # include <stdio.h>
225a99e2eSeric # include <pwd.h>
325a99e2eSeric # include <signal.h>
46328bdf7Seric # include <ctype.h>
5b20b3270Seric # include "sendmail.h"
625a99e2eSeric # ifdef LOG
7e374fd72Seric # include <syslog.h>
825a99e2eSeric # endif LOG
925a99e2eSeric 
10*145b49b1Seric static char SccsId[] = "@(#)deliver.c	3.15	08/08/81";
11259cace7Seric 
1225a99e2eSeric /*
1325a99e2eSeric **  DELIVER -- Deliver a message to a particular address.
1425a99e2eSeric **
1525a99e2eSeric **	Parameters:
1625a99e2eSeric **		to -- the address to deliver the message to.
1725a99e2eSeric **		editfcn -- if non-NULL, we want to call this function
1825a99e2eSeric **			to output the letter (instead of just out-
1925a99e2eSeric **			putting it raw).
2025a99e2eSeric **
2125a99e2eSeric **	Returns:
2225a99e2eSeric **		zero -- successfully delivered.
2325a99e2eSeric **		else -- some failure, see ExitStat for more info.
2425a99e2eSeric **
2525a99e2eSeric **	Side Effects:
2625a99e2eSeric **		The standard input is passed off to someone.
2725a99e2eSeric */
2825a99e2eSeric 
2925a99e2eSeric deliver(to, editfcn)
302a6e0786Seric 	ADDRESS *to;
3125a99e2eSeric 	int (*editfcn)();
3225a99e2eSeric {
3325a99e2eSeric 	char *host;
3425a99e2eSeric 	char *user;
3525a99e2eSeric 	extern struct passwd *getpwnam();
3625a99e2eSeric 	char **pvp;
375dfc646bSeric 	register char **mvp;
3825a99e2eSeric 	register char *p;
395dfc646bSeric 	register struct mailer *m;
405dfc646bSeric 	register int i;
4125a99e2eSeric 	extern int errno;
426328bdf7Seric 	extern putmessage();
43b07ac16bSeric 	extern char *index();
442a6e0786Seric 	extern bool checkcompat();
455dfc646bSeric 	char *pv[MAXPV+1];
465dfc646bSeric 	extern char *newstr();
475dfc646bSeric 	char tobuf[MAXLINE];
485dfc646bSeric 	char buf[MAXNAME];
495dfc646bSeric 	extern char *expand();
505dfc646bSeric 	bool firstone;
5125a99e2eSeric 
525dfc646bSeric 	if (bitset(QDONTSEND, to->q_flags))
535dfc646bSeric 		return (0);
5425a99e2eSeric 
5525a99e2eSeric # ifdef DEBUG
5625a99e2eSeric 	if (Debug)
575dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
585dfc646bSeric 			to->q_mailer, to->q_host, to->q_user);
5925a99e2eSeric # endif DEBUG
6025a99e2eSeric 
6125a99e2eSeric 	/*
625dfc646bSeric 	**  Do initial argv setup.
635dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
645dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
655dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
665dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
675dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
685dfc646bSeric 	*/
695dfc646bSeric 
705dfc646bSeric 	m = Mailer[to->q_mailer];
715dfc646bSeric 	host = to->q_host;
725dfc646bSeric 	define('g', m->m_from);		/* translated from address */
735dfc646bSeric 	define('h', host);		/* to host */
745dfc646bSeric 	Errors = 0;
755dfc646bSeric 	errno = 0;
765dfc646bSeric 	pvp = pv;
775dfc646bSeric 	*pvp++ = m->m_argv[0];
785dfc646bSeric 
795dfc646bSeric 	/* insert -f or -r flag as appropriate */
805dfc646bSeric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
815dfc646bSeric 	{
825dfc646bSeric 		if (bitset(M_FOPT, m->m_flags))
835dfc646bSeric 			*pvp++ = "-f";
845dfc646bSeric 		else
855dfc646bSeric 			*pvp++ = "-r";
865dfc646bSeric 		expand("$g", buf, &buf[sizeof buf - 1]);
875dfc646bSeric 		*pvp++ = newstr(buf);
885dfc646bSeric 	}
895dfc646bSeric 
905dfc646bSeric 	/*
915dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
925dfc646bSeric 	**  up to the first entry containing "$u".  There can only
935dfc646bSeric 	**  be one of these, and there are only a few more slots
945dfc646bSeric 	**  in the pv after it.
955dfc646bSeric 	*/
965dfc646bSeric 
975dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
985dfc646bSeric 	{
995dfc646bSeric 		while ((p = index(p, '$')) != NULL)
1005dfc646bSeric 			if (*++p == 'u')
1015dfc646bSeric 				break;
1025dfc646bSeric 		if (p != NULL)
1035dfc646bSeric 			break;
1045dfc646bSeric 
1055dfc646bSeric 		/* this entry is safe -- go ahead and process it */
1065dfc646bSeric 		expand(*mvp, buf, &buf[sizeof buf - 1]);
1075dfc646bSeric 		*pvp++ = newstr(buf);
1085dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1095dfc646bSeric 		{
1105dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1115dfc646bSeric 			return (-1);
1125dfc646bSeric 		}
1135dfc646bSeric 	}
1145dfc646bSeric 	if (*mvp == NULL)
1155dfc646bSeric 		syserr("No $u in mailer argv for %s", pv[0]);
1165dfc646bSeric 
1175dfc646bSeric 	/*
1185dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
1195dfc646bSeric 	**  run through our address list and append all the addresses
1205dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
1215dfc646bSeric 	**  always send another copy later.
1225dfc646bSeric 	*/
1235dfc646bSeric 
1245dfc646bSeric 	tobuf[0] = '\0';
1255dfc646bSeric 	firstone = TRUE;
1265dfc646bSeric 	To = tobuf;
1275dfc646bSeric 	for (; to != NULL; to = to->q_next)
1285dfc646bSeric 	{
1295dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
1305dfc646bSeric 		if (!firstone && !bitset(M_MUSER, m->m_flags))
1315dfc646bSeric 			break;
1325dfc646bSeric 
1335dfc646bSeric 		/* if already sent or not for this host, don't send */
1345dfc646bSeric 		if (bitset(QDONTSEND, to->q_flags) || strcmp(to->q_host, host) != 0)
1355dfc646bSeric 			continue;
1365dfc646bSeric 		user = to->q_user;
1375dfc646bSeric 		To = to->q_paddr;
1385dfc646bSeric 		to->q_flags |= QDONTSEND;
1395dfc646bSeric 		firstone = FALSE;
1405dfc646bSeric 
1415dfc646bSeric # ifdef DEBUG
1425dfc646bSeric 		if (Debug)
1435dfc646bSeric 			printf("   send to `%s'\n", user);
1445dfc646bSeric # endif DEBUG
1455dfc646bSeric 
1465dfc646bSeric 		/*
1475dfc646bSeric 		**  Check to see that these people are allowed to
1485dfc646bSeric 		**  talk to each other.
1492a6e0786Seric 		*/
1502a6e0786Seric 
1512a6e0786Seric 		if (!checkcompat(to))
1525dfc646bSeric 		{
1535dfc646bSeric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
1545dfc646bSeric 			continue;
1555dfc646bSeric 		}
1562a6e0786Seric 
1572a6e0786Seric 		/*
15825a99e2eSeric 		**  Remove quote bits from user/host.
15925a99e2eSeric 		*/
16025a99e2eSeric 
16125a99e2eSeric 		for (p = user; (*p++ &= 0177) != '\0'; )
16225a99e2eSeric 			continue;
16325a99e2eSeric 		if (host != NULL)
16425a99e2eSeric 			for (p = host; (*p++ &= 0177) != '\0'; )
16525a99e2eSeric 				continue;
16625a99e2eSeric 
16725a99e2eSeric 		/*
16825a99e2eSeric 		**  Strip quote bits from names if the mailer wants it.
16925a99e2eSeric 		*/
17025a99e2eSeric 
1712a6e0786Seric 		if (bitset(M_STRIPQ, m->m_flags))
17225a99e2eSeric 		{
17325a99e2eSeric 			stripquotes(user);
17425a99e2eSeric 			stripquotes(host);
17525a99e2eSeric 		}
17625a99e2eSeric 
17725a99e2eSeric 		/*
17825a99e2eSeric 		**  See if this user name is "special".
17925a99e2eSeric 		**	If the user name has a slash in it, assume that this
18025a99e2eSeric 		**	is a file -- send it off without further ado.
18125a99e2eSeric 		**	Note that this means that editfcn's will not
1825dfc646bSeric 		**	be applied to the message.  Also note that
1835dfc646bSeric 		**	this type of addresses is not processed along
1845dfc646bSeric 		**	with the others, so we fudge on the To person.
18525a99e2eSeric 		*/
18625a99e2eSeric 
18786f2b423Seric 		if (m == Mailer[0])
18825a99e2eSeric 		{
18925a99e2eSeric 			if (index(user, '/') != NULL)
19025a99e2eSeric 			{
19125a99e2eSeric 				i = mailfile(user);
19225a99e2eSeric 				giveresponse(i, TRUE, m);
1935dfc646bSeric 				continue;
19425a99e2eSeric 			}
19525a99e2eSeric 		}
19625a99e2eSeric 
19725a99e2eSeric 		/*
198243921eeSeric 		**  See if the user exists.
199243921eeSeric 		**	Strictly, this is only needed to print a pretty
20025a99e2eSeric 		**	error message.
201243921eeSeric 		**
202243921eeSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
203243921eeSeric 		**	>> NOTE >> cannot do any further aliasing; that
204b20b3270Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
20525a99e2eSeric 		*/
20625a99e2eSeric 
207*145b49b1Seric 		if (m == Mailer[0] && !bitset(QGOODADDR, to->q_flags))
20825a99e2eSeric 		{
209*145b49b1Seric 			if (bitset(QBADADDR, to->q_flags) || getpwnam(user) == NULL)
21025a99e2eSeric 			{
21125a99e2eSeric 				giveresponse(EX_NOUSER, TRUE, m);
2125dfc646bSeric 				continue;
21325a99e2eSeric 			}
21425a99e2eSeric 		}
21525a99e2eSeric 
2165dfc646bSeric 		/* create list of users for error messages */
2175dfc646bSeric 		if (tobuf[0] != '\0')
2185dfc646bSeric 			strcat(tobuf, ",");
2195dfc646bSeric 		strcat(tobuf, to->q_paddr);
2205dfc646bSeric 		define('u', user);		/* to user */
2215dfc646bSeric 
2225dfc646bSeric 		/* expand out this user */
2235dfc646bSeric 		expand(user, buf, &buf[sizeof buf - 1]);
2245dfc646bSeric 		*pvp++ = newstr(buf);
2255dfc646bSeric 		if (pvp >= &pv[MAXPV - 2])
2265dfc646bSeric 		{
2275dfc646bSeric 			/* allow some space for trailing parms */
2285dfc646bSeric 			break;
2295dfc646bSeric 		}
2305dfc646bSeric 	}
2315dfc646bSeric 
232*145b49b1Seric 	/* see if any addresses still exist */
233*145b49b1Seric 	if (tobuf[0] == '\0')
234*145b49b1Seric 		return (0);
235*145b49b1Seric 
2365dfc646bSeric 	/* print out messages as full list */
2375dfc646bSeric 	To = tobuf;
2385dfc646bSeric 
2395dfc646bSeric 	/*
2405dfc646bSeric 	**  Fill out any parameters after the $u parameter.
2415dfc646bSeric 	*/
2425dfc646bSeric 
2435dfc646bSeric 	while (*++mvp != NULL)
2445dfc646bSeric 	{
2455dfc646bSeric 		expand(*mvp, buf, &buf[sizeof buf - 1]);
2465dfc646bSeric 		*pvp++ = newstr(buf);
2475dfc646bSeric 		if (pvp >= &pv[MAXPV])
2485dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
2495dfc646bSeric 	}
2505dfc646bSeric 	*pvp++ = NULL;
2515dfc646bSeric 
25225a99e2eSeric 	/*
25325a99e2eSeric 	**  Call the mailer.
2546328bdf7Seric 	**	The argument vector gets built, pipes
25525a99e2eSeric 	**	are created as necessary, and we fork & exec as
2566328bdf7Seric 	**	appropriate.
25725a99e2eSeric 	*/
25825a99e2eSeric 
2595dfc646bSeric 	if (editfcn == NULL)
2605dfc646bSeric 		editfcn = putmessage;
2615dfc646bSeric 	i = sendoff(m, pv, editfcn);
2625dfc646bSeric 
2635dfc646bSeric 	return (i);
26425a99e2eSeric }
2655dfc646bSeric /*
2665dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
2675dfc646bSeric **
2685dfc646bSeric **	Parameters:
2695dfc646bSeric **		m -- mailer descriptor.
2705dfc646bSeric **		pvp -- parameter vector to send to it.
2715dfc646bSeric **		editfcn -- function to pipe it through.
2725dfc646bSeric **
2735dfc646bSeric **	Returns:
2745dfc646bSeric **		exit status of mailer.
2755dfc646bSeric **
2765dfc646bSeric **	Side Effects:
2775dfc646bSeric **		none.
2785dfc646bSeric */
2795dfc646bSeric 
280750c6fe8Seric #define NFORKTRIES	5
281750c6fe8Seric 
2825dfc646bSeric sendoff(m, pvp, editfcn)
2835dfc646bSeric 	struct mailer *m;
2845dfc646bSeric 	char **pvp;
2855dfc646bSeric 	int (*editfcn)();
2865dfc646bSeric {
2875dfc646bSeric 	auto int st;
2885dfc646bSeric 	register int i;
2895dfc646bSeric 	int pid;
2905dfc646bSeric 	int pvect[2];
2915dfc646bSeric 	FILE *mfile;
2925dfc646bSeric 	extern putmessage();
2935dfc646bSeric 	extern pipesig();
2945dfc646bSeric 	extern FILE *fdopen();
2955dfc646bSeric 
2965dfc646bSeric # ifdef DEBUG
2975dfc646bSeric 	if (Debug)
2985dfc646bSeric 	{
2995dfc646bSeric 		printf("Sendoff:\n");
3005dfc646bSeric 		printav(pvp);
3015dfc646bSeric 	}
3025dfc646bSeric # endif DEBUG
3035dfc646bSeric 
3046328bdf7Seric 	/* create a pipe to shove the mail through */
3056328bdf7Seric 	if (pipe(pvect) < 0)
30625a99e2eSeric 	{
30725a99e2eSeric 		syserr("pipe");
30825a99e2eSeric 		return (-1);
30925a99e2eSeric 	}
310750c6fe8Seric 	for (i = NFORKTRIES; i-- > 0; )
311750c6fe8Seric 	{
312ea235328Smark # ifdef VFORK
313ea235328Smark 		pid = vfork();
314ea235328Smark # else
31525a99e2eSeric 		pid = fork();
316ea235328Smark # endif
317750c6fe8Seric 		if (pid >= 0)
318750c6fe8Seric 			break;
319750c6fe8Seric 		sleep(NFORKTRIES - i);
320750c6fe8Seric 	}
32125a99e2eSeric 	if (pid < 0)
32225a99e2eSeric 	{
32325a99e2eSeric 		syserr("Cannot fork");
32425a99e2eSeric 		close(pvect[0]);
32525a99e2eSeric 		close(pvect[1]);
32625a99e2eSeric 		return (-1);
32725a99e2eSeric 	}
32825a99e2eSeric 	else if (pid == 0)
32925a99e2eSeric 	{
33025a99e2eSeric 		/* child -- set up input & exec mailer */
33103ab8e55Seric 		/* make diagnostic output be standard output */
33203ab8e55Seric 		close(2);
33303ab8e55Seric 		dup(1);
33425a99e2eSeric 		signal(SIGINT, SIG_IGN);
33525a99e2eSeric 		close(0);
33625a99e2eSeric 		if (dup(pvect[0]) < 0)
33725a99e2eSeric 		{
33825a99e2eSeric 			syserr("Cannot dup to zero!");
339a590b978Seric 			_exit(EX_OSERR);
34025a99e2eSeric 		}
34125a99e2eSeric 		close(pvect[0]);
34225a99e2eSeric 		close(pvect[1]);
3432a6e0786Seric 		if (!bitset(M_RESTR, m->m_flags))
34425a99e2eSeric 			setuid(getuid());
345e374fd72Seric # ifndef VFORK
346e374fd72Seric 		/*
347e374fd72Seric 		**  We have to be careful with vfork - we can't mung up the
348e374fd72Seric 		**  memory but we don't want the mailer to inherit any extra
349e374fd72Seric 		**  open files.  Chances are the mailer won't
350e374fd72Seric 		**  care about an extra file, but then again you never know.
351e374fd72Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
352e374fd72Seric 		**  declared static so we can't.  But if we fclose(pwf), which
353e374fd72Seric 		**  is what endpwent does, it closes it in the parent too and
354e374fd72Seric 		**  the next getpwnam will be slower.  If you have a weird
355e374fd72Seric 		**  mailer that chokes on the extra file you should do the
356e374fd72Seric 		**  endpwent().
357e374fd72Seric 		**
358e374fd72Seric 		**  Similar comments apply to log.  However, openlog is
359e374fd72Seric 		**  clever enough to set the FIOCLEX mode on the file,
360e374fd72Seric 		**  so it will be closed automatically on the exec.
361e374fd72Seric 		*/
362e374fd72Seric 
363e374fd72Seric 		endpwent();
36425a99e2eSeric # ifdef LOG
365f9fe028fSeric 		closelog();
36625a99e2eSeric # endif LOG
367e374fd72Seric # endif VFORK
36825a99e2eSeric 		execv(m->m_mailer, pvp);
36925a99e2eSeric 		/* syserr fails because log is closed */
37025a99e2eSeric 		/* syserr("Cannot exec %s", m->m_mailer); */
3712ac087dbSeric 		printf("Cannot exec %s\n", m->m_mailer);
3722ac087dbSeric 		fflush(stdout);
373a590b978Seric 		_exit(EX_UNAVAILABLE);
37425a99e2eSeric 	}
37525a99e2eSeric 
3766328bdf7Seric 	/* write out message to mailer */
37725a99e2eSeric 	close(pvect[0]);
37825a99e2eSeric 	signal(SIGPIPE, pipesig);
37925a99e2eSeric 	mfile = fdopen(pvect[1], "w");
3806328bdf7Seric 	if (editfcn == NULL)
3816328bdf7Seric 		editfcn = putmessage;
3826328bdf7Seric 	(*editfcn)(mfile, m);
38325a99e2eSeric 	fclose(mfile);
38425a99e2eSeric 
38525a99e2eSeric 	/*
38625a99e2eSeric 	**  Wait for child to die and report status.
38725a99e2eSeric 	**	We should never get fatal errors (e.g., segmentation
38825a99e2eSeric 	**	violation), so we report those specially.  For other
38925a99e2eSeric 	**	errors, we choose a status message (into statmsg),
39025a99e2eSeric 	**	and if it represents an error, we print it.
39125a99e2eSeric 	*/
39225a99e2eSeric 
39325a99e2eSeric 	while ((i = wait(&st)) > 0 && i != pid)
39425a99e2eSeric 		continue;
39525a99e2eSeric 	if (i < 0)
39625a99e2eSeric 	{
39725a99e2eSeric 		syserr("wait");
39825a99e2eSeric 		return (-1);
39925a99e2eSeric 	}
40025a99e2eSeric 	if ((st & 0377) != 0)
40125a99e2eSeric 	{
40225a99e2eSeric 		syserr("%s: stat %o", pvp[0], st);
403df2792f7Seric 		ExitStat = EX_UNAVAILABLE;
40425a99e2eSeric 		return (-1);
40525a99e2eSeric 	}
40625a99e2eSeric 	i = (st >> 8) & 0377;
4072ac087dbSeric 	giveresponse(i, TRUE, m);
40825a99e2eSeric 	return (i);
40925a99e2eSeric }
41025a99e2eSeric /*
41125a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
41225a99e2eSeric **
41325a99e2eSeric **	Parameters:
41425a99e2eSeric **		stat -- the status code from the mailer (high byte
41525a99e2eSeric **			only; core dumps must have been taken care of
41625a99e2eSeric **			already).
41725a99e2eSeric **		force -- if set, force an error message output, even
41825a99e2eSeric **			if the mailer seems to like to print its own
41925a99e2eSeric **			messages.
42025a99e2eSeric **		m -- the mailer descriptor for this mailer.
42125a99e2eSeric **
42225a99e2eSeric **	Returns:
4232a6e0786Seric **		stat.
42425a99e2eSeric **
42525a99e2eSeric **	Side Effects:
426c1f9df2cSeric **		Errors may be incremented.
42725a99e2eSeric **		ExitStat may be set.
42825a99e2eSeric **
42925a99e2eSeric **	Called By:
43025a99e2eSeric **		deliver
43125a99e2eSeric */
43225a99e2eSeric 
43325a99e2eSeric giveresponse(stat, force, m)
43425a99e2eSeric 	int stat;
43525a99e2eSeric 	int force;
43625a99e2eSeric 	register struct mailer *m;
43725a99e2eSeric {
43825a99e2eSeric 	register char *statmsg;
43925a99e2eSeric 	extern char *SysExMsg[];
44025a99e2eSeric 	register int i;
44125a99e2eSeric 	extern int N_SysEx;
44229dd97a3Seric 	extern long MsgSize;
44329dd97a3Seric 	char buf[30];
444e9ff65b0Seric 	extern char *sprintf();
44525a99e2eSeric 
44625a99e2eSeric 	i = stat - EX__BASE;
44725a99e2eSeric 	if (i < 0 || i > N_SysEx)
44825a99e2eSeric 		statmsg = NULL;
44925a99e2eSeric 	else
45025a99e2eSeric 		statmsg = SysExMsg[i];
45125a99e2eSeric 	if (stat == 0)
452c38ba59cSeric 	{
45325a99e2eSeric 		statmsg = "ok";
454c38ba59cSeric 		if (Verbose)
455c38ba59cSeric 			message("050", "ok");
456c38ba59cSeric 	}
45725a99e2eSeric 	else
45825a99e2eSeric 	{
459c1f9df2cSeric 		Errors++;
46025a99e2eSeric 		if (statmsg == NULL && m->m_badstat != 0)
46125a99e2eSeric 		{
46225a99e2eSeric 			stat = m->m_badstat;
46325a99e2eSeric 			i = stat - EX__BASE;
46425a99e2eSeric # ifdef DEBUG
46525a99e2eSeric 			if (i < 0 || i >= N_SysEx)
46625a99e2eSeric 				syserr("Bad m_badstat %d", stat);
46725a99e2eSeric 			else
46825a99e2eSeric # endif DEBUG
46925a99e2eSeric 			statmsg = SysExMsg[i];
47025a99e2eSeric 		}
47125a99e2eSeric 		if (statmsg == NULL)
47225a99e2eSeric 			usrerr("unknown mailer response %d", stat);
473c38ba59cSeric 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
47425a99e2eSeric 			usrerr("%s", statmsg);
47525a99e2eSeric 	}
47625a99e2eSeric 
47725a99e2eSeric 	/*
47825a99e2eSeric 	**  Final cleanup.
47925a99e2eSeric 	**	Log a record of the transaction.  Compute the new
48025a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
48125a99e2eSeric 	**	that.
48225a99e2eSeric 	*/
48325a99e2eSeric 
48425a99e2eSeric 	if (statmsg == NULL)
48529dd97a3Seric 	{
48629dd97a3Seric 		sprintf(buf, "error %d", stat);
48729dd97a3Seric 		statmsg = buf;
48829dd97a3Seric 	}
48929dd97a3Seric 
49029dd97a3Seric # ifdef LOG
491e374fd72Seric 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
49225a99e2eSeric # endif LOG
493243921eeSeric 	setstat(stat);
49425a99e2eSeric 	return (stat);
49525a99e2eSeric }
49625a99e2eSeric /*
4976328bdf7Seric **  PUTMESSAGE -- output a message to the final mailer.
49825a99e2eSeric **
4996328bdf7Seric **	This routine takes care of recreating the header from the
5006328bdf7Seric **	in-core copy, etc.
50125a99e2eSeric **
50225a99e2eSeric **	Parameters:
5036328bdf7Seric **		fp -- file to output onto.
5046328bdf7Seric **		m -- a mailer descriptor.
50525a99e2eSeric **
50625a99e2eSeric **	Returns:
5076328bdf7Seric **		none.
50825a99e2eSeric **
50925a99e2eSeric **	Side Effects:
5106328bdf7Seric **		The message is written onto fp.
51125a99e2eSeric */
51225a99e2eSeric 
5136328bdf7Seric putmessage(fp, m)
5146328bdf7Seric 	FILE *fp;
5156328bdf7Seric 	struct mailer *m;
51625a99e2eSeric {
5176328bdf7Seric 	char buf[BUFSIZ];
5186328bdf7Seric 	register int i;
5196328bdf7Seric 	HDR *h;
520b07ac16bSeric 	register char *p;
5216328bdf7Seric 	extern char *arpadate();
5226328bdf7Seric 	bool anyheader = FALSE;
52340e4ab56Seric 	extern char *expand();
524e9ff65b0Seric 	extern char *capitalize();
52525a99e2eSeric 
52640e4ab56Seric 	/* output "From" line unless supressed */
52740e4ab56Seric 	if (!bitset(M_NHDR, m->m_flags))
52840e4ab56Seric 		fprintf(fp, "%s\n", FromLine);
52940e4ab56Seric 
5304ae18d0eSeric 	/* output all header lines */
5316328bdf7Seric 	for (h = Header; h != NULL; h = h->h_link)
5326328bdf7Seric 	{
5339e9163a0Seric 		if (bitset(H_DELETE, h->h_flags))
5349e9163a0Seric 			continue;
5359e9163a0Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
5366328bdf7Seric 			continue;
5374ae18d0eSeric 		if (bitset(H_DEFAULT, h->h_flags))
5384ae18d0eSeric 		{
5394ae18d0eSeric 			expand(h->h_value, buf, &buf[sizeof buf]);
5404ae18d0eSeric 			p = buf;
5414ae18d0eSeric 		}
5424ae18d0eSeric 		else
5434ae18d0eSeric 			p = h->h_value;
5449e9163a0Seric 		if (*p == '\0')
5459e9163a0Seric 			continue;
5464ae18d0eSeric 		fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
5476328bdf7Seric 		h->h_flags |= H_USED;
5486328bdf7Seric 		anyheader = TRUE;
5496328bdf7Seric 	}
5506328bdf7Seric 
5516328bdf7Seric 	if (anyheader)
5526328bdf7Seric 		fprintf(fp, "\n");
5536328bdf7Seric 
5546328bdf7Seric 	/* output the body of the message */
55585751117Seric 	rewind(stdin);
55685751117Seric 	while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, stdin)) > 0)
5576328bdf7Seric 		fwrite(buf, 1, i, fp);
5586328bdf7Seric 
55925a99e2eSeric 	if (ferror(fp))
56025a99e2eSeric 	{
5616328bdf7Seric 		syserr("putmessage: write error");
56225a99e2eSeric 		setstat(EX_IOERR);
56325a99e2eSeric 	}
56425a99e2eSeric }
56525a99e2eSeric /*
56625a99e2eSeric **  PIPESIG -- Handle broken pipe signals
56725a99e2eSeric **
56825a99e2eSeric **	This just logs an error.
56925a99e2eSeric **
57025a99e2eSeric **	Parameters:
57125a99e2eSeric **		none
57225a99e2eSeric **
57325a99e2eSeric **	Returns:
57425a99e2eSeric **		none
57525a99e2eSeric **
57625a99e2eSeric **	Side Effects:
57725a99e2eSeric **		logs an error message.
57825a99e2eSeric */
57925a99e2eSeric 
58025a99e2eSeric pipesig()
58125a99e2eSeric {
58225a99e2eSeric 	syserr("Broken pipe");
58303ab8e55Seric 	signal(SIGPIPE, SIG_IGN);
58425a99e2eSeric }
58525a99e2eSeric /*
58625a99e2eSeric **  SENDTO -- Designate a send list.
58725a99e2eSeric **
58825a99e2eSeric **	The parameter is a comma-separated list of people to send to.
58925a99e2eSeric **	This routine arranges to send to all of them.
59025a99e2eSeric **
59125a99e2eSeric **	Parameters:
59225a99e2eSeric **		list -- the send list.
59325a99e2eSeric **		copyf -- the copy flag; passed to parse.
59425a99e2eSeric **
59525a99e2eSeric **	Returns:
59625a99e2eSeric **		none
59725a99e2eSeric **
59825a99e2eSeric **	Side Effects:
59925a99e2eSeric **		none.
60025a99e2eSeric */
60125a99e2eSeric 
60225a99e2eSeric sendto(list, copyf)
60325a99e2eSeric 	char *list;
60425a99e2eSeric 	int copyf;
60525a99e2eSeric {
60625a99e2eSeric 	register char *p;
60725a99e2eSeric 	register char *q;
60825a99e2eSeric 	register char c;
6092a6e0786Seric 	ADDRESS *a;
6102a6e0786Seric 	extern ADDRESS *parse();
61125a99e2eSeric 	bool more;
61225a99e2eSeric 
61325a99e2eSeric 	/* more keeps track of what the previous delimiter was */
61425a99e2eSeric 	more = TRUE;
61525a99e2eSeric 	for (p = list; more; )
61625a99e2eSeric 	{
61725a99e2eSeric 		/* find the end of this address */
61825a99e2eSeric 		q = p;
61925a99e2eSeric 		while ((c = *p++) != '\0' && c != ',' && c != '\n')
62025a99e2eSeric 			continue;
62125a99e2eSeric 		more = c != '\0';
62225a99e2eSeric 		*--p = '\0';
62325a99e2eSeric 		if (more)
62425a99e2eSeric 			p++;
62525a99e2eSeric 
62625a99e2eSeric 		/* parse the address */
6272a6e0786Seric 		if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL)
62825a99e2eSeric 			continue;
62925a99e2eSeric 
63025a99e2eSeric 		/* arrange to send to this person */
63140e4ab56Seric 		recipient(a);
63225a99e2eSeric 	}
63325a99e2eSeric 	To = NULL;
63425a99e2eSeric }
63525a99e2eSeric /*
63625a99e2eSeric **  RECIPIENT -- Designate a message recipient
63725a99e2eSeric **
63825a99e2eSeric **	Saves the named person for future mailing.
63925a99e2eSeric **
64025a99e2eSeric **	Parameters:
64125a99e2eSeric **		a -- the (preparsed) address header for the recipient.
64225a99e2eSeric **
64325a99e2eSeric **	Returns:
64425a99e2eSeric **		none.
64525a99e2eSeric **
64625a99e2eSeric **	Side Effects:
64725a99e2eSeric **		none.
64825a99e2eSeric */
64925a99e2eSeric 
65040e4ab56Seric recipient(a)
6512a6e0786Seric 	register ADDRESS *a;
65225a99e2eSeric {
6532a6e0786Seric 	register ADDRESS *q;
65425a99e2eSeric 	register struct mailer *m;
65525a99e2eSeric 	extern bool forward();
65625a99e2eSeric 	extern int errno;
65725a99e2eSeric 	extern bool sameaddr();
65825a99e2eSeric 
65925a99e2eSeric 	To = a->q_paddr;
66086f2b423Seric 	m = Mailer[a->q_mailer];
66125a99e2eSeric 	errno = 0;
66225a99e2eSeric # ifdef DEBUG
66325a99e2eSeric 	if (Debug)
66425a99e2eSeric 		printf("recipient(%s)\n", To);
66525a99e2eSeric # endif DEBUG
66625a99e2eSeric 
66725a99e2eSeric 	/*
66840e4ab56Seric 	**  Do sickly crude mapping for program mailing, etc.
66925a99e2eSeric 	*/
67025a99e2eSeric 
67140e4ab56Seric 	if (a->q_mailer == 0 && a->q_user[0] == '|')
67225a99e2eSeric 	{
67340e4ab56Seric 		a->q_mailer = 1;
674fc194317Seric 		m = Mailer[1];
67540e4ab56Seric 		a->q_user++;
67625a99e2eSeric 	}
67740e4ab56Seric 
67840e4ab56Seric 	/*
67940e4ab56Seric 	**  Look up this person in the recipient list.  If they
68040e4ab56Seric 	**  are there already, return, otherwise continue.
68140e4ab56Seric 	**  If the list is empty, just add it.
68240e4ab56Seric 	*/
68340e4ab56Seric 
68440e4ab56Seric 	if (m->m_sendq == NULL)
68540e4ab56Seric 	{
68640e4ab56Seric 		m->m_sendq = a;
68740e4ab56Seric 	}
68840e4ab56Seric 	else
68940e4ab56Seric 	{
69040e4ab56Seric 		ADDRESS *pq;
69140e4ab56Seric 
69240e4ab56Seric 		for (q = m->m_sendq; q != NULL; pq = q, q = q->q_next)
69340e4ab56Seric 		{
69440e4ab56Seric 			if (!ForceMail && sameaddr(q, a, FALSE))
69525a99e2eSeric 			{
69625a99e2eSeric # ifdef DEBUG
69725a99e2eSeric 				if (Debug)
69840e4ab56Seric 					printf("(%s in sendq)\n", a->q_paddr);
69925a99e2eSeric # endif DEBUG
700*145b49b1Seric 				if (Verbose && !bitset(QDONTSEND, a->q_flags))
701c38ba59cSeric 					message("050", "duplicate supressed");
70225a99e2eSeric 				return;
70325a99e2eSeric 			}
70425a99e2eSeric 		}
70525a99e2eSeric 
70640e4ab56Seric 		/* add address on list */
70740e4ab56Seric 		q = pq;
7085dfc646bSeric 		q->q_next = a;
70940e4ab56Seric 	}
71040e4ab56Seric 	a->q_next = NULL;
71140e4ab56Seric 
71225a99e2eSeric 	/*
71325a99e2eSeric 	**  See if the user wants hir mail forwarded.
71425a99e2eSeric 	**	`Forward' must do the forwarding recursively.
71525a99e2eSeric 	*/
71625a99e2eSeric 
71740e4ab56Seric 	if (m == Mailer[0] && !NoAlias && forward(a))
718*145b49b1Seric 		a->q_flags |= QDONTSEND;
71925a99e2eSeric 
72025a99e2eSeric 	return;
72125a99e2eSeric }
72225a99e2eSeric /*
72325a99e2eSeric **  MAILFILE -- Send a message to a file.
72425a99e2eSeric **
72525a99e2eSeric **	Parameters:
72625a99e2eSeric **		filename -- the name of the file to send to.
72725a99e2eSeric **
72825a99e2eSeric **	Returns:
72925a99e2eSeric **		The exit code associated with the operation.
73025a99e2eSeric **
73125a99e2eSeric **	Side Effects:
73225a99e2eSeric **		none.
73325a99e2eSeric **
73425a99e2eSeric **	Called By:
73525a99e2eSeric **		deliver
73625a99e2eSeric */
73725a99e2eSeric 
73825a99e2eSeric mailfile(filename)
73925a99e2eSeric 	char *filename;
74025a99e2eSeric {
74125a99e2eSeric 	register FILE *f;
74225a99e2eSeric 
74325a99e2eSeric 	f = fopen(filename, "a");
74425a99e2eSeric 	if (f == NULL)
74525a99e2eSeric 		return (EX_CANTCREAT);
74625a99e2eSeric 
74740e4ab56Seric 	putmessage(f, Mailer[1]);
74825a99e2eSeric 	fputs("\n", f);
74925a99e2eSeric 	fclose(f);
75025a99e2eSeric 	return (EX_OK);
75125a99e2eSeric }
752