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*db8841e9Seric static char SccsId[] = "@(#)deliver.c	3.17	08/09/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;
416328bdf7Seric 	extern putmessage();
422a6e0786Seric 	extern bool checkcompat();
435dfc646bSeric 	char *pv[MAXPV+1];
445dfc646bSeric 	char tobuf[MAXLINE];
455dfc646bSeric 	char buf[MAXNAME];
465dfc646bSeric 	bool firstone;
4725a99e2eSeric 
485dfc646bSeric 	if (bitset(QDONTSEND, to->q_flags))
495dfc646bSeric 		return (0);
5025a99e2eSeric 
5125a99e2eSeric # ifdef DEBUG
5225a99e2eSeric 	if (Debug)
535dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
545dfc646bSeric 			to->q_mailer, to->q_host, to->q_user);
5525a99e2eSeric # endif DEBUG
5625a99e2eSeric 
5725a99e2eSeric 	/*
585dfc646bSeric 	**  Do initial argv setup.
595dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
605dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
615dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
625dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
635dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
645dfc646bSeric 	*/
655dfc646bSeric 
665dfc646bSeric 	m = Mailer[to->q_mailer];
675dfc646bSeric 	host = to->q_host;
685dfc646bSeric 	define('g', m->m_from);		/* translated from address */
695dfc646bSeric 	define('h', host);		/* to host */
705dfc646bSeric 	Errors = 0;
715dfc646bSeric 	errno = 0;
725dfc646bSeric 	pvp = pv;
735dfc646bSeric 	*pvp++ = m->m_argv[0];
745dfc646bSeric 
755dfc646bSeric 	/* insert -f or -r flag as appropriate */
765dfc646bSeric 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
775dfc646bSeric 	{
785dfc646bSeric 		if (bitset(M_FOPT, m->m_flags))
795dfc646bSeric 			*pvp++ = "-f";
805dfc646bSeric 		else
815dfc646bSeric 			*pvp++ = "-r";
82*db8841e9Seric 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
835dfc646bSeric 		*pvp++ = newstr(buf);
845dfc646bSeric 	}
855dfc646bSeric 
865dfc646bSeric 	/*
875dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
885dfc646bSeric 	**  up to the first entry containing "$u".  There can only
895dfc646bSeric 	**  be one of these, and there are only a few more slots
905dfc646bSeric 	**  in the pv after it.
915dfc646bSeric 	*/
925dfc646bSeric 
935dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
945dfc646bSeric 	{
955dfc646bSeric 		while ((p = index(p, '$')) != NULL)
965dfc646bSeric 			if (*++p == 'u')
975dfc646bSeric 				break;
985dfc646bSeric 		if (p != NULL)
995dfc646bSeric 			break;
1005dfc646bSeric 
1015dfc646bSeric 		/* this entry is safe -- go ahead and process it */
102*db8841e9Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
1035dfc646bSeric 		*pvp++ = newstr(buf);
1045dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1055dfc646bSeric 		{
1065dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1075dfc646bSeric 			return (-1);
1085dfc646bSeric 		}
1095dfc646bSeric 	}
1105dfc646bSeric 	if (*mvp == NULL)
1115dfc646bSeric 		syserr("No $u in mailer argv for %s", pv[0]);
1125dfc646bSeric 
1135dfc646bSeric 	/*
1145dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
1155dfc646bSeric 	**  run through our address list and append all the addresses
1165dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
1175dfc646bSeric 	**  always send another copy later.
1185dfc646bSeric 	*/
1195dfc646bSeric 
1205dfc646bSeric 	tobuf[0] = '\0';
1215dfc646bSeric 	firstone = TRUE;
1225dfc646bSeric 	To = tobuf;
1235dfc646bSeric 	for (; to != NULL; to = to->q_next)
1245dfc646bSeric 	{
1255dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
1265dfc646bSeric 		if (!firstone && !bitset(M_MUSER, m->m_flags))
1275dfc646bSeric 			break;
1285dfc646bSeric 
1295dfc646bSeric 		/* if already sent or not for this host, don't send */
1305dfc646bSeric 		if (bitset(QDONTSEND, to->q_flags) || strcmp(to->q_host, host) != 0)
1315dfc646bSeric 			continue;
1325dfc646bSeric 		user = to->q_user;
1335dfc646bSeric 		To = to->q_paddr;
1345dfc646bSeric 		to->q_flags |= QDONTSEND;
1355dfc646bSeric 		firstone = FALSE;
1365dfc646bSeric 
1375dfc646bSeric # ifdef DEBUG
1385dfc646bSeric 		if (Debug)
1395dfc646bSeric 			printf("   send to `%s'\n", user);
1405dfc646bSeric # endif DEBUG
1415dfc646bSeric 
1425dfc646bSeric 		/*
1435dfc646bSeric 		**  Check to see that these people are allowed to
1445dfc646bSeric 		**  talk to each other.
1452a6e0786Seric 		*/
1462a6e0786Seric 
1472a6e0786Seric 		if (!checkcompat(to))
1485dfc646bSeric 		{
1495dfc646bSeric 			giveresponse(EX_UNAVAILABLE, TRUE, m);
1505dfc646bSeric 			continue;
1515dfc646bSeric 		}
1522a6e0786Seric 
1532a6e0786Seric 		/*
15425a99e2eSeric 		**  Remove quote bits from user/host.
15525a99e2eSeric 		*/
15625a99e2eSeric 
15725a99e2eSeric 		for (p = user; (*p++ &= 0177) != '\0'; )
15825a99e2eSeric 			continue;
15925a99e2eSeric 		if (host != NULL)
16025a99e2eSeric 			for (p = host; (*p++ &= 0177) != '\0'; )
16125a99e2eSeric 				continue;
16225a99e2eSeric 
16325a99e2eSeric 		/*
16425a99e2eSeric 		**  Strip quote bits from names if the mailer wants it.
16525a99e2eSeric 		*/
16625a99e2eSeric 
1672a6e0786Seric 		if (bitset(M_STRIPQ, m->m_flags))
16825a99e2eSeric 		{
16925a99e2eSeric 			stripquotes(user);
17025a99e2eSeric 			stripquotes(host);
17125a99e2eSeric 		}
17225a99e2eSeric 
17325a99e2eSeric 		/*
17425a99e2eSeric 		**  See if this user name is "special".
17525a99e2eSeric 		**	If the user name has a slash in it, assume that this
17625a99e2eSeric 		**	is a file -- send it off without further ado.
17725a99e2eSeric 		**	Note that this means that editfcn's will not
1785dfc646bSeric 		**	be applied to the message.  Also note that
1795dfc646bSeric 		**	this type of addresses is not processed along
1805dfc646bSeric 		**	with the others, so we fudge on the To person.
18125a99e2eSeric 		*/
18225a99e2eSeric 
183c2567733Seric 		if (m == Mailer[M_LOCAL])
18425a99e2eSeric 		{
18525a99e2eSeric 			if (index(user, '/') != NULL)
18625a99e2eSeric 			{
18725a99e2eSeric 				i = mailfile(user);
18825a99e2eSeric 				giveresponse(i, TRUE, m);
1895dfc646bSeric 				continue;
19025a99e2eSeric 			}
19125a99e2eSeric 		}
19225a99e2eSeric 
19325a99e2eSeric 		/*
194243921eeSeric 		**  See if the user exists.
195243921eeSeric 		**	Strictly, this is only needed to print a pretty
19625a99e2eSeric 		**	error message.
197243921eeSeric 		**
198243921eeSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
199243921eeSeric 		**	>> NOTE >> cannot do any further aliasing; that
200b20b3270Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
20125a99e2eSeric 		*/
20225a99e2eSeric 
203c2567733Seric 		if (bitset(QBADADDR, to->q_flags))
20425a99e2eSeric 		{
20525a99e2eSeric 			giveresponse(EX_NOUSER, TRUE, m);
2065dfc646bSeric 			continue;
20725a99e2eSeric 		}
20825a99e2eSeric 
2095dfc646bSeric 		/* create list of users for error messages */
2105dfc646bSeric 		if (tobuf[0] != '\0')
211*db8841e9Seric 			(void) strcat(tobuf, ",");
212*db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
2135dfc646bSeric 		define('u', user);		/* to user */
214c2567733Seric 		define('z', to->q_home);	/* user's home */
2155dfc646bSeric 
2165dfc646bSeric 		/* expand out this user */
217*db8841e9Seric 		(void) expand(user, buf, &buf[sizeof buf - 1]);
2185dfc646bSeric 		*pvp++ = newstr(buf);
2195dfc646bSeric 		if (pvp >= &pv[MAXPV - 2])
2205dfc646bSeric 		{
2215dfc646bSeric 			/* allow some space for trailing parms */
2225dfc646bSeric 			break;
2235dfc646bSeric 		}
2245dfc646bSeric 	}
2255dfc646bSeric 
226145b49b1Seric 	/* see if any addresses still exist */
227145b49b1Seric 	if (tobuf[0] == '\0')
228145b49b1Seric 		return (0);
229145b49b1Seric 
2305dfc646bSeric 	/* print out messages as full list */
2315dfc646bSeric 	To = tobuf;
2325dfc646bSeric 
2335dfc646bSeric 	/*
2345dfc646bSeric 	**  Fill out any parameters after the $u parameter.
2355dfc646bSeric 	*/
2365dfc646bSeric 
2375dfc646bSeric 	while (*++mvp != NULL)
2385dfc646bSeric 	{
239*db8841e9Seric 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
2405dfc646bSeric 		*pvp++ = newstr(buf);
2415dfc646bSeric 		if (pvp >= &pv[MAXPV])
2425dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
2435dfc646bSeric 	}
2445dfc646bSeric 	*pvp++ = NULL;
2455dfc646bSeric 
24625a99e2eSeric 	/*
24725a99e2eSeric 	**  Call the mailer.
2486328bdf7Seric 	**	The argument vector gets built, pipes
24925a99e2eSeric 	**	are created as necessary, and we fork & exec as
2506328bdf7Seric 	**	appropriate.
251c2567733Seric 	**
252c2567733Seric 	**	Notice the tacky hack to handle private mailers.
25325a99e2eSeric 	*/
25425a99e2eSeric 
2555dfc646bSeric 	if (editfcn == NULL)
2565dfc646bSeric 		editfcn = putmessage;
257c2567733Seric 	if (m == Mailer[M_PRIVATE])
258c2567733Seric 	{
259*db8841e9Seric 		(void) expand("$z/.mailer", buf, &buf[sizeof buf - 1]);
260c2567733Seric 		m->m_mailer = buf;
261c2567733Seric 	}
2625dfc646bSeric 	i = sendoff(m, pv, editfcn);
2635dfc646bSeric 
2645dfc646bSeric 	return (i);
26525a99e2eSeric }
2665dfc646bSeric /*
2675dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
2685dfc646bSeric **
2695dfc646bSeric **	Parameters:
2705dfc646bSeric **		m -- mailer descriptor.
2715dfc646bSeric **		pvp -- parameter vector to send to it.
2725dfc646bSeric **		editfcn -- function to pipe it through.
2735dfc646bSeric **
2745dfc646bSeric **	Returns:
2755dfc646bSeric **		exit status of mailer.
2765dfc646bSeric **
2775dfc646bSeric **	Side Effects:
2785dfc646bSeric **		none.
2795dfc646bSeric */
2805dfc646bSeric 
281750c6fe8Seric #define NFORKTRIES	5
282750c6fe8Seric 
2835dfc646bSeric sendoff(m, pvp, editfcn)
2845dfc646bSeric 	struct mailer *m;
2855dfc646bSeric 	char **pvp;
2865dfc646bSeric 	int (*editfcn)();
2875dfc646bSeric {
2885dfc646bSeric 	auto int st;
2895dfc646bSeric 	register int i;
2905dfc646bSeric 	int pid;
2915dfc646bSeric 	int pvect[2];
2925dfc646bSeric 	FILE *mfile;
2935dfc646bSeric 	extern putmessage();
2945dfc646bSeric 	extern pipesig();
2955dfc646bSeric 	extern FILE *fdopen();
2965dfc646bSeric 
2975dfc646bSeric # ifdef DEBUG
2985dfc646bSeric 	if (Debug)
2995dfc646bSeric 	{
3005dfc646bSeric 		printf("Sendoff:\n");
3015dfc646bSeric 		printav(pvp);
3025dfc646bSeric 	}
3035dfc646bSeric # endif DEBUG
3045dfc646bSeric 
3056328bdf7Seric 	/* create a pipe to shove the mail through */
3066328bdf7Seric 	if (pipe(pvect) < 0)
30725a99e2eSeric 	{
30825a99e2eSeric 		syserr("pipe");
30925a99e2eSeric 		return (-1);
31025a99e2eSeric 	}
311750c6fe8Seric 	for (i = NFORKTRIES; i-- > 0; )
312750c6fe8Seric 	{
313ea235328Smark # ifdef VFORK
314ea235328Smark 		pid = vfork();
315ea235328Smark # else
31625a99e2eSeric 		pid = fork();
317ea235328Smark # endif
318750c6fe8Seric 		if (pid >= 0)
319750c6fe8Seric 			break;
320*db8841e9Seric 		sleep((unsigned) NFORKTRIES - i);
321750c6fe8Seric 	}
32225a99e2eSeric 	if (pid < 0)
32325a99e2eSeric 	{
32425a99e2eSeric 		syserr("Cannot fork");
325*db8841e9Seric 		(void) close(pvect[0]);
326*db8841e9Seric 		(void) close(pvect[1]);
32725a99e2eSeric 		return (-1);
32825a99e2eSeric 	}
32925a99e2eSeric 	else if (pid == 0)
33025a99e2eSeric 	{
33125a99e2eSeric 		/* child -- set up input & exec mailer */
33203ab8e55Seric 		/* make diagnostic output be standard output */
333*db8841e9Seric 		(void) close(2);
334*db8841e9Seric 		(void) dup(1);
335*db8841e9Seric 		(void) signal(SIGINT, SIG_IGN);
336*db8841e9Seric 		(void) close(0);
33725a99e2eSeric 		if (dup(pvect[0]) < 0)
33825a99e2eSeric 		{
33925a99e2eSeric 			syserr("Cannot dup to zero!");
340a590b978Seric 			_exit(EX_OSERR);
34125a99e2eSeric 		}
342*db8841e9Seric 		(void) close(pvect[0]);
343*db8841e9Seric 		(void) close(pvect[1]);
3442a6e0786Seric 		if (!bitset(M_RESTR, m->m_flags))
345*db8841e9Seric 			(void) setuid(getuid());
346e374fd72Seric # ifndef VFORK
347e374fd72Seric 		/*
348e374fd72Seric 		**  We have to be careful with vfork - we can't mung up the
349e374fd72Seric 		**  memory but we don't want the mailer to inherit any extra
350e374fd72Seric 		**  open files.  Chances are the mailer won't
351e374fd72Seric 		**  care about an extra file, but then again you never know.
352e374fd72Seric 		**  Actually, we would like to close(fileno(pwf)), but it's
353e374fd72Seric 		**  declared static so we can't.  But if we fclose(pwf), which
354e374fd72Seric 		**  is what endpwent does, it closes it in the parent too and
355e374fd72Seric 		**  the next getpwnam will be slower.  If you have a weird
356e374fd72Seric 		**  mailer that chokes on the extra file you should do the
357e374fd72Seric 		**  endpwent().
358e374fd72Seric 		**
359e374fd72Seric 		**  Similar comments apply to log.  However, openlog is
360e374fd72Seric 		**  clever enough to set the FIOCLEX mode on the file,
361e374fd72Seric 		**  so it will be closed automatically on the exec.
362e374fd72Seric 		*/
363e374fd72Seric 
364e374fd72Seric 		endpwent();
36525a99e2eSeric # ifdef LOG
366f9fe028fSeric 		closelog();
36725a99e2eSeric # endif LOG
368e374fd72Seric # endif VFORK
36925a99e2eSeric 		execv(m->m_mailer, pvp);
37025a99e2eSeric 		/* syserr fails because log is closed */
37125a99e2eSeric 		/* syserr("Cannot exec %s", m->m_mailer); */
3722ac087dbSeric 		printf("Cannot exec %s\n", m->m_mailer);
373*db8841e9Seric 		(void) fflush(stdout);
374a590b978Seric 		_exit(EX_UNAVAILABLE);
37525a99e2eSeric 	}
37625a99e2eSeric 
3776328bdf7Seric 	/* write out message to mailer */
378*db8841e9Seric 	(void) close(pvect[0]);
379*db8841e9Seric 	(void) signal(SIGPIPE, pipesig);
38025a99e2eSeric 	mfile = fdopen(pvect[1], "w");
3816328bdf7Seric 	if (editfcn == NULL)
3826328bdf7Seric 		editfcn = putmessage;
3836328bdf7Seric 	(*editfcn)(mfile, m);
384*db8841e9Seric 	(void) fclose(mfile);
38525a99e2eSeric 
38625a99e2eSeric 	/*
38725a99e2eSeric 	**  Wait for child to die and report status.
38825a99e2eSeric 	**	We should never get fatal errors (e.g., segmentation
38925a99e2eSeric 	**	violation), so we report those specially.  For other
39025a99e2eSeric 	**	errors, we choose a status message (into statmsg),
39125a99e2eSeric 	**	and if it represents an error, we print it.
39225a99e2eSeric 	*/
39325a99e2eSeric 
39425a99e2eSeric 	while ((i = wait(&st)) > 0 && i != pid)
39525a99e2eSeric 		continue;
39625a99e2eSeric 	if (i < 0)
39725a99e2eSeric 	{
39825a99e2eSeric 		syserr("wait");
39925a99e2eSeric 		return (-1);
40025a99e2eSeric 	}
40125a99e2eSeric 	if ((st & 0377) != 0)
40225a99e2eSeric 	{
40325a99e2eSeric 		syserr("%s: stat %o", pvp[0], st);
404df2792f7Seric 		ExitStat = EX_UNAVAILABLE;
40525a99e2eSeric 		return (-1);
40625a99e2eSeric 	}
40725a99e2eSeric 	i = (st >> 8) & 0377;
4082ac087dbSeric 	giveresponse(i, TRUE, m);
40925a99e2eSeric 	return (i);
41025a99e2eSeric }
41125a99e2eSeric /*
41225a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
41325a99e2eSeric **
41425a99e2eSeric **	Parameters:
41525a99e2eSeric **		stat -- the status code from the mailer (high byte
41625a99e2eSeric **			only; core dumps must have been taken care of
41725a99e2eSeric **			already).
41825a99e2eSeric **		force -- if set, force an error message output, even
41925a99e2eSeric **			if the mailer seems to like to print its own
42025a99e2eSeric **			messages.
42125a99e2eSeric **		m -- the mailer descriptor for this mailer.
42225a99e2eSeric **
42325a99e2eSeric **	Returns:
424*db8841e9Seric **		none.
42525a99e2eSeric **
42625a99e2eSeric **	Side Effects:
427c1f9df2cSeric **		Errors may be incremented.
42825a99e2eSeric **		ExitStat may be set.
42925a99e2eSeric **
43025a99e2eSeric **	Called By:
43125a99e2eSeric **		deliver
43225a99e2eSeric */
43325a99e2eSeric 
43425a99e2eSeric giveresponse(stat, force, m)
43525a99e2eSeric 	int stat;
43625a99e2eSeric 	int force;
43725a99e2eSeric 	register struct mailer *m;
43825a99e2eSeric {
43925a99e2eSeric 	register char *statmsg;
44025a99e2eSeric 	extern char *SysExMsg[];
44125a99e2eSeric 	register int i;
44225a99e2eSeric 	extern int N_SysEx;
44329dd97a3Seric 	extern long MsgSize;
44429dd97a3Seric 	char buf[30];
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 	{
486*db8841e9Seric 		(void) 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 }
49525a99e2eSeric /*
4966328bdf7Seric **  PUTMESSAGE -- output a message to the final mailer.
49725a99e2eSeric **
4986328bdf7Seric **	This routine takes care of recreating the header from the
4996328bdf7Seric **	in-core copy, etc.
50025a99e2eSeric **
50125a99e2eSeric **	Parameters:
5026328bdf7Seric **		fp -- file to output onto.
5036328bdf7Seric **		m -- a mailer descriptor.
50425a99e2eSeric **
50525a99e2eSeric **	Returns:
5066328bdf7Seric **		none.
50725a99e2eSeric **
50825a99e2eSeric **	Side Effects:
5096328bdf7Seric **		The message is written onto fp.
51025a99e2eSeric */
51125a99e2eSeric 
5126328bdf7Seric putmessage(fp, m)
5136328bdf7Seric 	FILE *fp;
5146328bdf7Seric 	struct mailer *m;
51525a99e2eSeric {
5166328bdf7Seric 	char buf[BUFSIZ];
5176328bdf7Seric 	register int i;
5186328bdf7Seric 	HDR *h;
519b07ac16bSeric 	register char *p;
5206328bdf7Seric 	extern char *arpadate();
5216328bdf7Seric 	bool anyheader = FALSE;
522e9ff65b0Seric 	extern char *capitalize();
52325a99e2eSeric 
52440e4ab56Seric 	/* output "From" line unless supressed */
52540e4ab56Seric 	if (!bitset(M_NHDR, m->m_flags))
52640e4ab56Seric 		fprintf(fp, "%s\n", FromLine);
52740e4ab56Seric 
5284ae18d0eSeric 	/* output all header lines */
5296328bdf7Seric 	for (h = Header; h != NULL; h = h->h_link)
5306328bdf7Seric 	{
5319e9163a0Seric 		if (bitset(H_DELETE, h->h_flags))
5329e9163a0Seric 			continue;
5339e9163a0Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
5346328bdf7Seric 			continue;
5354ae18d0eSeric 		if (bitset(H_DEFAULT, h->h_flags))
5364ae18d0eSeric 		{
537*db8841e9Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
5384ae18d0eSeric 			p = buf;
5394ae18d0eSeric 		}
5404ae18d0eSeric 		else
5414ae18d0eSeric 			p = h->h_value;
5429e9163a0Seric 		if (*p == '\0')
5439e9163a0Seric 			continue;
5444ae18d0eSeric 		fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
5456328bdf7Seric 		h->h_flags |= H_USED;
5466328bdf7Seric 		anyheader = TRUE;
5476328bdf7Seric 	}
5486328bdf7Seric 
5496328bdf7Seric 	if (anyheader)
5506328bdf7Seric 		fprintf(fp, "\n");
5516328bdf7Seric 
5526328bdf7Seric 	/* output the body of the message */
55385751117Seric 	rewind(stdin);
55485751117Seric 	while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, stdin)) > 0)
555*db8841e9Seric 		(void) fwrite(buf, 1, i, fp);
5566328bdf7Seric 
55725a99e2eSeric 	if (ferror(fp))
55825a99e2eSeric 	{
5596328bdf7Seric 		syserr("putmessage: write error");
56025a99e2eSeric 		setstat(EX_IOERR);
56125a99e2eSeric 	}
56225a99e2eSeric }
56325a99e2eSeric /*
56425a99e2eSeric **  PIPESIG -- Handle broken pipe signals
56525a99e2eSeric **
56625a99e2eSeric **	This just logs an error.
56725a99e2eSeric **
56825a99e2eSeric **	Parameters:
56925a99e2eSeric **		none
57025a99e2eSeric **
57125a99e2eSeric **	Returns:
57225a99e2eSeric **		none
57325a99e2eSeric **
57425a99e2eSeric **	Side Effects:
57525a99e2eSeric **		logs an error message.
57625a99e2eSeric */
57725a99e2eSeric 
57825a99e2eSeric pipesig()
57925a99e2eSeric {
58025a99e2eSeric 	syserr("Broken pipe");
581*db8841e9Seric 	(void) signal(SIGPIPE, SIG_IGN);
58225a99e2eSeric }
58325a99e2eSeric /*
58425a99e2eSeric **  SENDTO -- Designate a send list.
58525a99e2eSeric **
58625a99e2eSeric **	The parameter is a comma-separated list of people to send to.
58725a99e2eSeric **	This routine arranges to send to all of them.
58825a99e2eSeric **
58925a99e2eSeric **	Parameters:
59025a99e2eSeric **		list -- the send list.
59125a99e2eSeric **		copyf -- the copy flag; passed to parse.
59225a99e2eSeric **
59325a99e2eSeric **	Returns:
59425a99e2eSeric **		none
59525a99e2eSeric **
59625a99e2eSeric **	Side Effects:
59725a99e2eSeric **		none.
59825a99e2eSeric */
59925a99e2eSeric 
60025a99e2eSeric sendto(list, copyf)
60125a99e2eSeric 	char *list;
60225a99e2eSeric 	int copyf;
60325a99e2eSeric {
60425a99e2eSeric 	register char *p;
60525a99e2eSeric 	register char *q;
60625a99e2eSeric 	register char c;
6072a6e0786Seric 	ADDRESS *a;
60825a99e2eSeric 	bool more;
60925a99e2eSeric 
61025a99e2eSeric 	/* more keeps track of what the previous delimiter was */
61125a99e2eSeric 	more = TRUE;
61225a99e2eSeric 	for (p = list; more; )
61325a99e2eSeric 	{
61425a99e2eSeric 		/* find the end of this address */
61525a99e2eSeric 		q = p;
61625a99e2eSeric 		while ((c = *p++) != '\0' && c != ',' && c != '\n')
61725a99e2eSeric 			continue;
61825a99e2eSeric 		more = c != '\0';
61925a99e2eSeric 		*--p = '\0';
62025a99e2eSeric 		if (more)
62125a99e2eSeric 			p++;
62225a99e2eSeric 
62325a99e2eSeric 		/* parse the address */
6242a6e0786Seric 		if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL)
62525a99e2eSeric 			continue;
62625a99e2eSeric 
62725a99e2eSeric 		/* arrange to send to this person */
62840e4ab56Seric 		recipient(a);
62925a99e2eSeric 	}
63025a99e2eSeric 	To = NULL;
63125a99e2eSeric }
63225a99e2eSeric /*
63325a99e2eSeric **  RECIPIENT -- Designate a message recipient
63425a99e2eSeric **
63525a99e2eSeric **	Saves the named person for future mailing.
63625a99e2eSeric **
63725a99e2eSeric **	Parameters:
63825a99e2eSeric **		a -- the (preparsed) address header for the recipient.
63925a99e2eSeric **
64025a99e2eSeric **	Returns:
64125a99e2eSeric **		none.
64225a99e2eSeric **
64325a99e2eSeric **	Side Effects:
64425a99e2eSeric **		none.
64525a99e2eSeric */
64625a99e2eSeric 
64740e4ab56Seric recipient(a)
6482a6e0786Seric 	register ADDRESS *a;
64925a99e2eSeric {
6502a6e0786Seric 	register ADDRESS *q;
65125a99e2eSeric 	register struct mailer *m;
65225a99e2eSeric 	extern bool forward();
65325a99e2eSeric 
65425a99e2eSeric 	To = a->q_paddr;
65586f2b423Seric 	m = Mailer[a->q_mailer];
65625a99e2eSeric 	errno = 0;
65725a99e2eSeric # ifdef DEBUG
65825a99e2eSeric 	if (Debug)
65925a99e2eSeric 		printf("recipient(%s)\n", To);
66025a99e2eSeric # endif DEBUG
66125a99e2eSeric 
66225a99e2eSeric 	/*
66340e4ab56Seric 	**  Do sickly crude mapping for program mailing, etc.
66425a99e2eSeric 	*/
66525a99e2eSeric 
666c2567733Seric 	if (a->q_mailer == M_LOCAL)
66725a99e2eSeric 	{
668c2567733Seric 		if (a->q_user[0] == '|')
669c2567733Seric 		{
670c2567733Seric 			a->q_mailer = M_PROG;
671c2567733Seric 			m = Mailer[M_PROG];
67240e4ab56Seric 			a->q_user++;
67325a99e2eSeric 		}
674c2567733Seric 		else
675c2567733Seric 		{
676c2567733Seric 			register struct passwd *pw;
677c2567733Seric 
678c2567733Seric 			pw = getpwnam(a->q_user);
679c2567733Seric 			if (pw == NULL)
680c2567733Seric 				a->q_flags |= QBADADDR;
681c2567733Seric 			else
682c2567733Seric 			{
683c2567733Seric 				char xbuf[60];
684c2567733Seric 
685c2567733Seric 				a->q_home = newstr(pw->pw_dir);
686c2567733Seric 				define('z', a->q_home);
687*db8841e9Seric 				(void) expand("$z/.mailer", xbuf, &xbuf[sizeof xbuf - 1]);
688c2567733Seric 				if (access(xbuf, 1) == 0)
689c2567733Seric 				{
690c2567733Seric 					a->q_mailer = M_PRIVATE;
691c2567733Seric 					m = Mailer[M_PROG];
692c2567733Seric 				}
693c2567733Seric 			}
694c2567733Seric 		}
695c2567733Seric 	}
69640e4ab56Seric 
69740e4ab56Seric 	/*
69840e4ab56Seric 	**  Look up this person in the recipient list.  If they
69940e4ab56Seric 	**  are there already, return, otherwise continue.
70040e4ab56Seric 	**  If the list is empty, just add it.
70140e4ab56Seric 	*/
70240e4ab56Seric 
70340e4ab56Seric 	if (m->m_sendq == NULL)
70440e4ab56Seric 	{
70540e4ab56Seric 		m->m_sendq = a;
70640e4ab56Seric 	}
70740e4ab56Seric 	else
70840e4ab56Seric 	{
70940e4ab56Seric 		ADDRESS *pq;
71040e4ab56Seric 
71140e4ab56Seric 		for (q = m->m_sendq; q != NULL; pq = q, q = q->q_next)
71240e4ab56Seric 		{
71340e4ab56Seric 			if (!ForceMail && sameaddr(q, a, FALSE))
71425a99e2eSeric 			{
71525a99e2eSeric # ifdef DEBUG
71625a99e2eSeric 				if (Debug)
71740e4ab56Seric 					printf("(%s in sendq)\n", a->q_paddr);
71825a99e2eSeric # endif DEBUG
719145b49b1Seric 				if (Verbose && !bitset(QDONTSEND, a->q_flags))
720c38ba59cSeric 					message("050", "duplicate supressed");
72125a99e2eSeric 				return;
72225a99e2eSeric 			}
72325a99e2eSeric 		}
72425a99e2eSeric 
72540e4ab56Seric 		/* add address on list */
72640e4ab56Seric 		q = pq;
7275dfc646bSeric 		q->q_next = a;
72840e4ab56Seric 	}
72940e4ab56Seric 	a->q_next = NULL;
73040e4ab56Seric 
73125a99e2eSeric 	/*
73225a99e2eSeric 	**  See if the user wants hir mail forwarded.
73325a99e2eSeric 	**	`Forward' must do the forwarding recursively.
73425a99e2eSeric 	*/
73525a99e2eSeric 
736c2567733Seric 	if (m == Mailer[M_LOCAL] && !NoAlias && forward(a))
737145b49b1Seric 		a->q_flags |= QDONTSEND;
73825a99e2eSeric 
73925a99e2eSeric 	return;
74025a99e2eSeric }
74125a99e2eSeric /*
74225a99e2eSeric **  MAILFILE -- Send a message to a file.
74325a99e2eSeric **
74425a99e2eSeric **	Parameters:
74525a99e2eSeric **		filename -- the name of the file to send to.
74625a99e2eSeric **
74725a99e2eSeric **	Returns:
74825a99e2eSeric **		The exit code associated with the operation.
74925a99e2eSeric **
75025a99e2eSeric **	Side Effects:
75125a99e2eSeric **		none.
75225a99e2eSeric **
75325a99e2eSeric **	Called By:
75425a99e2eSeric **		deliver
75525a99e2eSeric */
75625a99e2eSeric 
75725a99e2eSeric mailfile(filename)
75825a99e2eSeric 	char *filename;
75925a99e2eSeric {
76025a99e2eSeric 	register FILE *f;
76125a99e2eSeric 
76225a99e2eSeric 	f = fopen(filename, "a");
76325a99e2eSeric 	if (f == NULL)
76425a99e2eSeric 		return (EX_CANTCREAT);
76525a99e2eSeric 
76640e4ab56Seric 	putmessage(f, Mailer[1]);
76725a99e2eSeric 	fputs("\n", f);
768*db8841e9Seric 	(void) fclose(f);
76925a99e2eSeric 	return (EX_OK);
77025a99e2eSeric }
771