1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)deliver.c	8.59 (Berkeley) 01/05/94";
11 #endif /* not lint */
12 
13 #include "sendmail.h"
14 #include <netdb.h>
15 #include <errno.h>
16 #ifdef NAMED_BIND
17 #include <arpa/nameser.h>
18 #include <resolv.h>
19 
20 extern int	h_errno;
21 #endif
22 
23 extern char	SmtpError[];
24 
25 /*
26 **  SENDALL -- actually send all the messages.
27 **
28 **	Parameters:
29 **		e -- the envelope to send.
30 **		mode -- the delivery mode to use.  If SM_DEFAULT, use
31 **			the current e->e_sendmode.
32 **
33 **	Returns:
34 **		none.
35 **
36 **	Side Effects:
37 **		Scans the send lists and sends everything it finds.
38 **		Delivers any appropriate error messages.
39 **		If we are running in a non-interactive mode, takes the
40 **			appropriate action.
41 */
42 
43 sendall(e, mode)
44 	ENVELOPE *e;
45 	char mode;
46 {
47 	register ADDRESS *q;
48 	char *owner;
49 	int otherowners;
50 	register ENVELOPE *ee;
51 	ENVELOPE *splitenv = NULL;
52 	bool announcequeueup;
53 
54 	/*
55 	**  If we have had global, fatal errors, don't bother sending
56 	**  the message at all if we are in SMTP mode.  Local errors
57 	**  (e.g., a single address failing) will still cause the other
58 	**  addresses to be sent.
59 	*/
60 
61 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
62 	{
63 		e->e_flags |= EF_CLRQUEUE;
64 		return;
65 	}
66 
67 	/* determine actual delivery mode */
68 	CurrentLA = getla();
69 	if (mode == SM_DEFAULT)
70 	{
71 		mode = e->e_sendmode;
72 		if (mode != SM_VERIFY &&
73 		    shouldqueue(e->e_msgpriority, e->e_ctime))
74 			mode = SM_QUEUE;
75 		announcequeueup = mode == SM_QUEUE;
76 	}
77 	else
78 		announcequeueup = FALSE;
79 
80 	if (tTd(13, 1))
81 	{
82 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
83 			mode, e->e_id);
84 		printaddr(&e->e_from, FALSE);
85 		printf("sendqueue:\n");
86 		printaddr(e->e_sendqueue, TRUE);
87 	}
88 
89 	/*
90 	**  Do any preprocessing necessary for the mode we are running.
91 	**	Check to make sure the hop count is reasonable.
92 	**	Delete sends to the sender in mailing lists.
93 	*/
94 
95 	CurEnv = e;
96 
97 	if (e->e_hopcount > MaxHopCount)
98 	{
99 		errno = 0;
100 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
101 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
102 			RealHostName, e->e_sendqueue->q_paddr);
103 		return;
104 	}
105 
106 	/*
107 	**  Do sender deletion.
108 	**
109 	**	If the sender has the QQUEUEUP flag set, skip this.
110 	**	This can happen if the name server is hosed when you
111 	**	are trying to send mail.  The result is that the sender
112 	**	is instantiated in the queue as a recipient.
113 	*/
114 
115 	if (!bitset(EF_METOO, e->e_flags) &&
116 	    !bitset(QQUEUEUP, e->e_from.q_flags))
117 	{
118 		if (tTd(13, 5))
119 		{
120 			printf("sendall: QDONTSEND ");
121 			printaddr(&e->e_from, FALSE);
122 		}
123 		e->e_from.q_flags |= QDONTSEND;
124 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
125 	}
126 
127 	/*
128 	**  Handle alias owners.
129 	**
130 	**	We scan up the q_alias chain looking for owners.
131 	**	We discard owners that are the same as the return path.
132 	*/
133 
134 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
135 	{
136 		register struct address *a;
137 
138 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
139 			continue;
140 		if (a != NULL)
141 			q->q_owner = a->q_owner;
142 
143 		if (q->q_owner != NULL &&
144 		    !bitset(QDONTSEND, q->q_flags) &&
145 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
146 			q->q_owner = NULL;
147 	}
148 
149 	owner = "";
150 	otherowners = 1;
151 	while (owner != NULL && otherowners > 0)
152 	{
153 		owner = NULL;
154 		otherowners = 0;
155 
156 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
157 		{
158 			if (bitset(QDONTSEND, q->q_flags))
159 				continue;
160 
161 			if (q->q_owner != NULL)
162 			{
163 				if (owner == NULL)
164 					owner = q->q_owner;
165 				else if (owner != q->q_owner)
166 				{
167 					if (strcmp(owner, q->q_owner) == 0)
168 					{
169 						/* make future comparisons cheap */
170 						q->q_owner = owner;
171 					}
172 					else
173 					{
174 						otherowners++;
175 					}
176 					owner = q->q_owner;
177 				}
178 			}
179 			else
180 			{
181 				otherowners++;
182 			}
183 		}
184 
185 		if (owner != NULL && otherowners > 0)
186 		{
187 			extern HDR *copyheader();
188 			extern ADDRESS *copyqueue();
189 
190 			/*
191 			**  Split this envelope into two.
192 			*/
193 
194 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
195 			*ee = *e;
196 			ee->e_id = NULL;
197 			(void) queuename(ee, '\0');
198 
199 			if (tTd(13, 1))
200 				printf("sendall: split %s into %s\n",
201 					e->e_id, ee->e_id);
202 
203 			ee->e_header = copyheader(e->e_header);
204 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
205 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
206 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
207 			setsender(owner, ee, NULL, TRUE);
208 			if (tTd(13, 5))
209 			{
210 				printf("sendall(split): QDONTSEND ");
211 				printaddr(&ee->e_from, FALSE);
212 			}
213 			ee->e_from.q_flags |= QDONTSEND;
214 			ee->e_dfp = NULL;
215 			ee->e_xfp = NULL;
216 			ee->e_df = NULL;
217 			ee->e_errormode = EM_MAIL;
218 			ee->e_sibling = splitenv;
219 			splitenv = ee;
220 
221 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
222 				if (q->q_owner == owner)
223 					q->q_flags |= QDONTSEND;
224 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
225 				if (q->q_owner != owner)
226 					q->q_flags |= QDONTSEND;
227 
228 			if (e->e_df != NULL && mode != SM_VERIFY)
229 			{
230 				ee->e_dfp = NULL;
231 				ee->e_df = queuename(ee, 'd');
232 				ee->e_df = newstr(ee->e_df);
233 				if (link(e->e_df, ee->e_df) < 0)
234 				{
235 					syserr("sendall: link(%s, %s)",
236 						e->e_df, ee->e_df);
237 				}
238 			}
239 
240 			if (mode != SM_VERIFY)
241 				openxscript(ee);
242 #ifdef LOG
243 			if (LogLevel > 4)
244 				syslog(LOG_INFO, "%s: clone %s",
245 					ee->e_id, e->e_id);
246 #endif
247 		}
248 	}
249 
250 	if (owner != NULL)
251 	{
252 		setsender(owner, e, NULL, TRUE);
253 		if (tTd(13, 5))
254 		{
255 			printf("sendall(owner): QDONTSEND ");
256 			printaddr(&e->e_from, FALSE);
257 		}
258 		e->e_from.q_flags |= QDONTSEND;
259 		e->e_errormode = EM_MAIL;
260 	}
261 
262 # ifdef QUEUE
263 	if ((mode == SM_QUEUE || mode == SM_FORK ||
264 	     (mode != SM_VERIFY && SuperSafe)) &&
265 	    !bitset(EF_INQUEUE, e->e_flags))
266 	{
267 		/* be sure everything is instantiated in the queue */
268 		queueup(e, TRUE, announcequeueup);
269 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
270 			queueup(ee, TRUE, announcequeueup);
271 	}
272 #endif /* QUEUE */
273 
274 	if (splitenv != NULL)
275 	{
276 		if (tTd(13, 1))
277 		{
278 			printf("\nsendall: Split queue; remaining queue:\n");
279 			printaddr(e->e_sendqueue, TRUE);
280 		}
281 
282 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
283 		{
284 			CurEnv = ee;
285 			sendenvelope(ee, mode);
286 		}
287 
288 		CurEnv = e;
289 	}
290 	sendenvelope(e, mode);
291 
292 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
293 		dropenvelope(splitenv);
294 }
295 
296 sendenvelope(e, mode)
297 	register ENVELOPE *e;
298 	char mode;
299 {
300 	bool oldverbose;
301 	int pid;
302 	register ADDRESS *q;
303 	char *qf;
304 	char *id;
305 
306 	/*
307 	**  If we have had global, fatal errors, don't bother sending
308 	**  the message at all if we are in SMTP mode.  Local errors
309 	**  (e.g., a single address failing) will still cause the other
310 	**  addresses to be sent.
311 	*/
312 
313 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
314 	{
315 		e->e_flags |= EF_CLRQUEUE;
316 		return;
317 	}
318 
319 	oldverbose = Verbose;
320 	switch (mode)
321 	{
322 	  case SM_VERIFY:
323 		Verbose = TRUE;
324 		break;
325 
326 	  case SM_QUEUE:
327   queueonly:
328 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
329 		return;
330 
331 	  case SM_FORK:
332 		if (e->e_xfp != NULL)
333 			(void) fflush(e->e_xfp);
334 
335 # ifndef HASFLOCK
336 		/*
337 		**  Since fcntl locking has the interesting semantic that
338 		**  the lock is owned by a process, not by an open file
339 		**  descriptor, we have to flush this to the queue, and
340 		**  then restart from scratch in the child.
341 		*/
342 
343 		/* save id for future use */
344 		id = e->e_id;
345 
346 		/* now drop the envelope in the parent */
347 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
348 		dropenvelope(e);
349 
350 		/* and reacquire in the child */
351 		(void) dowork(id, TRUE, FALSE, e);
352 
353 		return;
354 
355 # else /* HASFLOCK */
356 
357 		pid = fork();
358 		if (pid < 0)
359 		{
360 			goto queueonly;
361 		}
362 		else if (pid > 0)
363 		{
364 			/* be sure we leave the temp files to our child */
365 			/* can't call unlockqueue to avoid unlink of xfp */
366 			if (e->e_lockfp != NULL)
367 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
368 			e->e_lockfp = NULL;
369 
370 			/* close any random open files in the envelope */
371 			closexscript(e);
372 			if (e->e_dfp != NULL)
373 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
374 			e->e_dfp = NULL;
375 			e->e_id = e->e_df = NULL;
376 			return;
377 		}
378 
379 		/* double fork to avoid zombies */
380 		if (fork() > 0)
381 			exit(EX_OK);
382 
383 		/* be sure we are immune from the terminal */
384 		disconnect(1, e);
385 
386 		/*
387 		**  Close any cached connections.
388 		**
389 		**	We don't send the QUIT protocol because the parent
390 		**	still knows about the connection.
391 		**
392 		**	This should only happen when delivering an error
393 		**	message.
394 		*/
395 
396 		mci_flush(FALSE, NULL);
397 
398 # endif /* HASFLOCK */
399 
400 		break;
401 	}
402 
403 	/*
404 	**  Run through the list and send everything.
405 	**
406 	**	Set EF_GLOBALERRS so that error messages during delivery
407 	**	result in returned mail.
408 	*/
409 
410 	e->e_nsent = 0;
411 	e->e_flags |= EF_GLOBALERRS;
412 
413 	/* now run through the queue */
414 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
415 	{
416 #ifdef XDEBUG
417 		char wbuf[MAXNAME + 20];
418 
419 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
420 		checkfd012(wbuf);
421 #endif
422 		if (mode == SM_VERIFY)
423 		{
424 			e->e_to = q->q_paddr;
425 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
426 			{
427 				if (q->q_host != NULL && q->q_host[0] != '\0')
428 					message("deliverable: mailer %s, host %s, user %s",
429 						q->q_mailer->m_name,
430 						q->q_host,
431 						q->q_user);
432 				else
433 					message("deliverable: mailer %s, user %s",
434 						q->q_mailer->m_name,
435 						q->q_user);
436 			}
437 		}
438 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
439 		{
440 # ifdef QUEUE
441 			/*
442 			**  Checkpoint the send list every few addresses
443 			*/
444 
445 			if (e->e_nsent >= CheckpointInterval)
446 			{
447 				queueup(e, TRUE, FALSE);
448 				e->e_nsent = 0;
449 			}
450 # endif /* QUEUE */
451 			(void) deliver(e, q);
452 		}
453 	}
454 	Verbose = oldverbose;
455 
456 #ifdef XDEBUG
457 	checkfd012("end of sendenvelope");
458 #endif
459 
460 	if (mode == SM_FORK)
461 		finis();
462 }
463 /*
464 **  DOFORK -- do a fork, retrying a couple of times on failure.
465 **
466 **	This MUST be a macro, since after a vfork we are running
467 **	two processes on the same stack!!!
468 **
469 **	Parameters:
470 **		none.
471 **
472 **	Returns:
473 **		From a macro???  You've got to be kidding!
474 **
475 **	Side Effects:
476 **		Modifies the ==> LOCAL <== variable 'pid', leaving:
477 **			pid of child in parent, zero in child.
478 **			-1 on unrecoverable error.
479 **
480 **	Notes:
481 **		I'm awfully sorry this looks so awful.  That's
482 **		vfork for you.....
483 */
484 
485 # define NFORKTRIES	5
486 
487 # ifndef FORK
488 # define FORK	fork
489 # endif
490 
491 # define DOFORK(fORKfN) \
492 {\
493 	register int i;\
494 \
495 	for (i = NFORKTRIES; --i >= 0; )\
496 	{\
497 		pid = fORKfN();\
498 		if (pid >= 0)\
499 			break;\
500 		if (i > 0)\
501 			sleep((unsigned) NFORKTRIES - i);\
502 	}\
503 }
504 /*
505 **  DOFORK -- simple fork interface to DOFORK.
506 **
507 **	Parameters:
508 **		none.
509 **
510 **	Returns:
511 **		pid of child in parent.
512 **		zero in child.
513 **		-1 on error.
514 **
515 **	Side Effects:
516 **		returns twice, once in parent and once in child.
517 */
518 
519 dofork()
520 {
521 	register int pid;
522 
523 	DOFORK(fork);
524 	return (pid);
525 }
526 /*
527 **  DELIVER -- Deliver a message to a list of addresses.
528 **
529 **	This routine delivers to everyone on the same host as the
530 **	user on the head of the list.  It is clever about mailers
531 **	that don't handle multiple users.  It is NOT guaranteed
532 **	that it will deliver to all these addresses however -- so
533 **	deliver should be called once for each address on the
534 **	list.
535 **
536 **	Parameters:
537 **		e -- the envelope to deliver.
538 **		firstto -- head of the address list to deliver to.
539 **
540 **	Returns:
541 **		zero -- successfully delivered.
542 **		else -- some failure, see ExitStat for more info.
543 **
544 **	Side Effects:
545 **		The standard input is passed off to someone.
546 */
547 
548 deliver(e, firstto)
549 	register ENVELOPE *e;
550 	ADDRESS *firstto;
551 {
552 	char *host;			/* host being sent to */
553 	char *user;			/* user being sent to */
554 	char **pvp;
555 	register char **mvp;
556 	register char *p;
557 	register MAILER *m;		/* mailer for this recipient */
558 	ADDRESS *ctladdr;
559 	register MCI *mci;
560 	register ADDRESS *to = firstto;
561 	bool clever = FALSE;		/* running user smtp to this mailer */
562 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
563 	int rcode;			/* response code */
564 	char *firstsig;			/* signature of firstto */
565 	int pid;
566 	char *curhost;
567 	int mpvect[2];
568 	int rpvect[2];
569 	char *pv[MAXPV+1];
570 	char tobuf[TOBUFSIZE];		/* text line of to people */
571 	char buf[MAXNAME];
572 	char rpathbuf[MAXNAME];		/* translated return path */
573 	extern int checkcompat();
574 
575 	errno = 0;
576 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
577 		return (0);
578 
579 #ifdef NAMED_BIND
580 	/* unless interactive, try twice, over a minute */
581 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
582 		_res.retrans = 30;
583 		_res.retry = 2;
584 	}
585 #endif
586 
587 	m = to->q_mailer;
588 	host = to->q_host;
589 	CurEnv = e;			/* just in case */
590 	e->e_statmsg = NULL;
591 	SmtpError[0] = '\0';
592 
593 	if (tTd(10, 1))
594 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
595 			m->m_mno, host, to->q_user);
596 	if (tTd(10, 100))
597 		printopenfds(FALSE);
598 
599 	/*
600 	**  If this mailer is expensive, and if we don't want to make
601 	**  connections now, just mark these addresses and return.
602 	**	This is useful if we want to batch connections to
603 	**	reduce load.  This will cause the messages to be
604 	**	queued up, and a daemon will come along to send the
605 	**	messages later.
606 	**		This should be on a per-mailer basis.
607 	*/
608 
609 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
610 	{
611 		for (; to != NULL; to = to->q_next)
612 		{
613 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
614 			    to->q_mailer != m)
615 				continue;
616 			to->q_flags |= QQUEUEUP;
617 			e->e_to = to->q_paddr;
618 			message("queued");
619 			if (LogLevel > 8)
620 				logdelivery(m, NULL, "queued", NULL, e);
621 		}
622 		e->e_to = NULL;
623 		return (0);
624 	}
625 
626 	/*
627 	**  Do initial argv setup.
628 	**	Insert the mailer name.  Notice that $x expansion is
629 	**	NOT done on the mailer name.  Then, if the mailer has
630 	**	a picky -f flag, we insert it as appropriate.  This
631 	**	code does not check for 'pv' overflow; this places a
632 	**	manifest lower limit of 4 for MAXPV.
633 	**		The from address rewrite is expected to make
634 	**		the address relative to the other end.
635 	*/
636 
637 	/* rewrite from address, using rewriting rules */
638 	rcode = EX_OK;
639 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
640 					   RF_SENDERADDR|RF_CANONICAL,
641 					   &rcode, e));
642 	define('g', rpathbuf, e);		/* translated return path */
643 	define('h', host, e);			/* to host */
644 	Errors = 0;
645 	pvp = pv;
646 	*pvp++ = m->m_argv[0];
647 
648 	/* insert -f or -r flag as appropriate */
649 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
650 	{
651 		if (bitnset(M_FOPT, m->m_flags))
652 			*pvp++ = "-f";
653 		else
654 			*pvp++ = "-r";
655 		*pvp++ = newstr(rpathbuf);
656 	}
657 
658 	/*
659 	**  Append the other fixed parts of the argv.  These run
660 	**  up to the first entry containing "$u".  There can only
661 	**  be one of these, and there are only a few more slots
662 	**  in the pv after it.
663 	*/
664 
665 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
666 	{
667 		/* can't use strchr here because of sign extension problems */
668 		while (*p != '\0')
669 		{
670 			if ((*p++ & 0377) == MACROEXPAND)
671 			{
672 				if (*p == 'u')
673 					break;
674 			}
675 		}
676 
677 		if (*p != '\0')
678 			break;
679 
680 		/* this entry is safe -- go ahead and process it */
681 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
682 		*pvp++ = newstr(buf);
683 		if (pvp >= &pv[MAXPV - 3])
684 		{
685 			syserr("554 Too many parameters to %s before $u", pv[0]);
686 			return (-1);
687 		}
688 	}
689 
690 	/*
691 	**  If we have no substitution for the user name in the argument
692 	**  list, we know that we must supply the names otherwise -- and
693 	**  SMTP is the answer!!
694 	*/
695 
696 	if (*mvp == NULL)
697 	{
698 		/* running SMTP */
699 # ifdef SMTP
700 		clever = TRUE;
701 		*pvp = NULL;
702 # else /* SMTP */
703 		/* oops!  we don't implement SMTP */
704 		syserr("554 SMTP style mailer not implemented");
705 		return (EX_SOFTWARE);
706 # endif /* SMTP */
707 	}
708 
709 	/*
710 	**  At this point *mvp points to the argument with $u.  We
711 	**  run through our address list and append all the addresses
712 	**  we can.  If we run out of space, do not fret!  We can
713 	**  always send another copy later.
714 	*/
715 
716 	tobuf[0] = '\0';
717 	e->e_to = tobuf;
718 	ctladdr = NULL;
719 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
720 	for (; to != NULL; to = to->q_next)
721 	{
722 		/* avoid sending multiple recipients to dumb mailers */
723 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
724 			break;
725 
726 		/* if already sent or not for this host, don't send */
727 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
728 		    to->q_mailer != firstto->q_mailer ||
729 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
730 			continue;
731 
732 		/* avoid overflowing tobuf */
733 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
734 			break;
735 
736 		if (tTd(10, 1))
737 		{
738 			printf("\nsend to ");
739 			printaddr(to, FALSE);
740 		}
741 
742 		/* compute effective uid/gid when sending */
743 		/* XXX perhaps this should be to->q_mailer != LocalMailer ?? */
744 		/* XXX perhaps it should be a mailer flag? */
745 		if (to->q_mailer == ProgMailer || to->q_mailer == FileMailer)
746 			ctladdr = getctladdr(to);
747 
748 		user = to->q_user;
749 		e->e_to = to->q_paddr;
750 		if (tTd(10, 5))
751 		{
752 			printf("deliver: QDONTSEND ");
753 			printaddr(to, FALSE);
754 		}
755 		to->q_flags |= QDONTSEND;
756 
757 		/*
758 		**  Check to see that these people are allowed to
759 		**  talk to each other.
760 		*/
761 
762 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
763 		{
764 			NoReturn = TRUE;
765 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
766 			giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e);
767 			continue;
768 		}
769 		rcode = checkcompat(to, e);
770 		if (rcode != EX_OK)
771 		{
772 			markfailure(e, to, rcode);
773 			giveresponse(rcode, m, NULL, ctladdr, e);
774 			continue;
775 		}
776 
777 		/*
778 		**  Strip quote bits from names if the mailer is dumb
779 		**	about them.
780 		*/
781 
782 		if (bitnset(M_STRIPQ, m->m_flags))
783 		{
784 			stripquotes(user);
785 			stripquotes(host);
786 		}
787 
788 		/* hack attack -- delivermail compatibility */
789 		if (m == ProgMailer && *user == '|')
790 			user++;
791 
792 		/*
793 		**  If an error message has already been given, don't
794 		**	bother to send to this address.
795 		**
796 		**	>>>>>>>>>> This clause assumes that the local mailer
797 		**	>> NOTE >> cannot do any further aliasing; that
798 		**	>>>>>>>>>> function is subsumed by sendmail.
799 		*/
800 
801 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
802 			continue;
803 
804 		/* save statistics.... */
805 		markstats(e, to);
806 
807 		/*
808 		**  See if this user name is "special".
809 		**	If the user name has a slash in it, assume that this
810 		**	is a file -- send it off without further ado.  Note
811 		**	that this type of addresses is not processed along
812 		**	with the others, so we fudge on the To person.
813 		*/
814 
815 		if (m == FileMailer)
816 		{
817 			rcode = mailfile(user, ctladdr, e);
818 			giveresponse(rcode, m, NULL, ctladdr, e);
819 			if (rcode == EX_OK)
820 				to->q_flags |= QSENT;
821 			continue;
822 		}
823 
824 		/*
825 		**  Address is verified -- add this user to mailer
826 		**  argv, and add it to the print list of recipients.
827 		*/
828 
829 		/* link together the chain of recipients */
830 		to->q_tchain = tochain;
831 		tochain = to;
832 
833 		/* create list of users for error messages */
834 		(void) strcat(tobuf, ",");
835 		(void) strcat(tobuf, to->q_paddr);
836 		define('u', user, e);		/* to user */
837 		p = to->q_home;
838 		if (p == NULL && ctladdr != NULL)
839 			p = ctladdr->q_home;
840 		define('z', p, e);	/* user's home */
841 
842 		/*
843 		**  Expand out this user into argument list.
844 		*/
845 
846 		if (!clever)
847 		{
848 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
849 			*pvp++ = newstr(buf);
850 			if (pvp >= &pv[MAXPV - 2])
851 			{
852 				/* allow some space for trailing parms */
853 				break;
854 			}
855 		}
856 	}
857 
858 	/* see if any addresses still exist */
859 	if (tobuf[0] == '\0')
860 	{
861 		define('g', (char *) NULL, e);
862 		return (0);
863 	}
864 
865 	/* print out messages as full list */
866 	e->e_to = tobuf + 1;
867 
868 	/*
869 	**  Fill out any parameters after the $u parameter.
870 	*/
871 
872 	while (!clever && *++mvp != NULL)
873 	{
874 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
875 		*pvp++ = newstr(buf);
876 		if (pvp >= &pv[MAXPV])
877 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
878 	}
879 	*pvp++ = NULL;
880 
881 	/*
882 	**  Call the mailer.
883 	**	The argument vector gets built, pipes
884 	**	are created as necessary, and we fork & exec as
885 	**	appropriate.
886 	**	If we are running SMTP, we just need to clean up.
887 	*/
888 
889 	/*XXX this seems a bit wierd */
890 	if (ctladdr == NULL && bitset(QGOODUID, e->e_from.q_flags))
891 		ctladdr = &e->e_from;
892 
893 #ifdef NAMED_BIND
894 	if (ConfigLevel < 2)
895 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
896 #endif
897 
898 	if (tTd(11, 1))
899 	{
900 		printf("openmailer:");
901 		printav(pv);
902 	}
903 	errno = 0;
904 
905 	CurHostName = m->m_mailer;
906 
907 	/*
908 	**  Deal with the special case of mail handled through an IPC
909 	**  connection.
910 	**	In this case we don't actually fork.  We must be
911 	**	running SMTP for this to work.  We will return a
912 	**	zero pid to indicate that we are running IPC.
913 	**  We also handle a debug version that just talks to stdin/out.
914 	*/
915 
916 	curhost = NULL;
917 	SmtpPhase = NULL;
918 	mci = NULL;
919 
920 #ifdef XDEBUG
921 	{
922 		char wbuf[MAXLINE];
923 
924 		/* make absolutely certain 0, 1, and 2 are in use */
925 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
926 		checkfd012(wbuf);
927 	}
928 #endif
929 
930 	/* check for Local Person Communication -- not for mortals!!! */
931 	if (strcmp(m->m_mailer, "[LPC]") == 0)
932 	{
933 		mci = (MCI *) xalloc(sizeof *mci);
934 		bzero((char *) mci, sizeof *mci);
935 		mci->mci_in = stdin;
936 		mci->mci_out = stdout;
937 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
938 		mci->mci_mailer = m;
939 	}
940 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
941 		 strcmp(m->m_mailer, "[TCP]") == 0)
942 	{
943 #ifdef DAEMON
944 		register int i;
945 		register u_short port;
946 
947 		if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0')
948 		{
949 			syserr("null host name for %s mailer", m->m_mailer);
950 			rcode = EX_CONFIG;
951 			goto give_up;
952 		}
953 
954 		CurHostName = pv[1];
955 		curhost = hostsignature(m, pv[1], e);
956 
957 		if (curhost == NULL || curhost[0] == '\0')
958 		{
959 			syserr("null host signature for %s", pv[1]);
960 			rcode = EX_OSERR;
961 			goto give_up;
962 		}
963 
964 		if (!clever)
965 		{
966 			syserr("554 non-clever IPC");
967 			rcode = EX_CONFIG;
968 			goto give_up;
969 		}
970 		if (pv[2] != NULL)
971 			port = atoi(pv[2]);
972 		else
973 			port = 0;
974 tryhost:
975 		while (*curhost != '\0')
976 		{
977 			register char *p;
978 			static char hostbuf[MAXNAME];
979 
980 			/* pull the next host from the signature */
981 			p = strchr(curhost, ':');
982 			if (p == NULL)
983 				p = &curhost[strlen(curhost)];
984 			if (p == curhost)
985 			{
986 				syserr("deliver: null host name in signature");
987 				curhost++;
988 				continue;
989 			}
990 			strncpy(hostbuf, curhost, p - curhost);
991 			hostbuf[p - curhost] = '\0';
992 			if (*p != '\0')
993 				p++;
994 			curhost = p;
995 
996 			/* see if we already know that this host is fried */
997 			CurHostName = hostbuf;
998 			mci = mci_get(hostbuf, m);
999 			if (mci->mci_state != MCIS_CLOSED)
1000 			{
1001 				if (tTd(11, 1))
1002 				{
1003 					printf("openmailer: ");
1004 					mci_dump(mci, FALSE);
1005 				}
1006 				CurHostName = mci->mci_host;
1007 				break;
1008 			}
1009 			mci->mci_mailer = m;
1010 			if (mci->mci_exitstat != EX_OK)
1011 				continue;
1012 
1013 			/* try the connection */
1014 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
1015 			message("Connecting to %s (%s)...",
1016 				hostbuf, m->m_name);
1017 			i = makeconnection(hostbuf, port, mci,
1018 				bitnset(M_SECURE_PORT, m->m_flags));
1019 			mci->mci_exitstat = i;
1020 			mci->mci_errno = errno;
1021 #ifdef NAMED_BIND
1022 			mci->mci_herrno = h_errno;
1023 #endif
1024 			if (i == EX_OK)
1025 			{
1026 				mci->mci_state = MCIS_OPENING;
1027 				mci_cache(mci);
1028 				if (TrafficLogFile != NULL)
1029 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1030 						getpid(), hostbuf);
1031 				break;
1032 			}
1033 			else if (tTd(11, 1))
1034 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
1035 					i, errno);
1036 
1037 			/* enter status of this host */
1038 			setstat(i);
1039 
1040 			/* should print some message here for -v mode */
1041 		}
1042 		if (mci == NULL)
1043 		{
1044 			syserr("deliver: no host name");
1045 			rcode = EX_OSERR;
1046 			goto give_up;
1047 		}
1048 		mci->mci_pid = 0;
1049 #else /* no DAEMON */
1050 		syserr("554 openmailer: no IPC");
1051 		if (tTd(11, 1))
1052 			printf("openmailer: NULL\n");
1053 		rcode = EX_UNAVAILABLE;
1054 		goto give_up;
1055 #endif /* DAEMON */
1056 	}
1057 	else
1058 	{
1059 		if (TrafficLogFile != NULL)
1060 		{
1061 			char **av;
1062 
1063 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1064 			for (av = pv; *av != NULL; av++)
1065 				fprintf(TrafficLogFile, " %s", *av);
1066 			fprintf(TrafficLogFile, "\n");
1067 		}
1068 
1069 		/* create a pipe to shove the mail through */
1070 		if (pipe(mpvect) < 0)
1071 		{
1072 			syserr("%s... openmailer(%s): pipe (to mailer)",
1073 				e->e_to, m->m_name);
1074 			if (tTd(11, 1))
1075 				printf("openmailer: NULL\n");
1076 			rcode = EX_OSERR;
1077 			goto give_up;
1078 		}
1079 
1080 		/* if this mailer speaks smtp, create a return pipe */
1081 		if (clever && pipe(rpvect) < 0)
1082 		{
1083 			syserr("%s... openmailer(%s): pipe (from mailer)",
1084 				e->e_to, m->m_name);
1085 			(void) close(mpvect[0]);
1086 			(void) close(mpvect[1]);
1087 			if (tTd(11, 1))
1088 				printf("openmailer: NULL\n");
1089 			rcode = EX_OSERR;
1090 			goto give_up;
1091 		}
1092 
1093 		/*
1094 		**  Actually fork the mailer process.
1095 		**	DOFORK is clever about retrying.
1096 		**
1097 		**	Dispose of SIGCHLD signal catchers that may be laying
1098 		**	around so that endmail will get it.
1099 		*/
1100 
1101 		if (e->e_xfp != NULL)
1102 			(void) fflush(e->e_xfp);		/* for debugging */
1103 		(void) fflush(stdout);
1104 # ifdef SIGCHLD
1105 		(void) setsignal(SIGCHLD, SIG_DFL);
1106 # endif /* SIGCHLD */
1107 		DOFORK(FORK);
1108 		/* pid is set by DOFORK */
1109 		if (pid < 0)
1110 		{
1111 			/* failure */
1112 			syserr("%s... openmailer(%s): cannot fork",
1113 				e->e_to, m->m_name);
1114 			(void) close(mpvect[0]);
1115 			(void) close(mpvect[1]);
1116 			if (clever)
1117 			{
1118 				(void) close(rpvect[0]);
1119 				(void) close(rpvect[1]);
1120 			}
1121 			if (tTd(11, 1))
1122 				printf("openmailer: NULL\n");
1123 			rcode = EX_OSERR;
1124 			goto give_up;
1125 		}
1126 		else if (pid == 0)
1127 		{
1128 			int i;
1129 			int saveerrno;
1130 			char **ep;
1131 			char *env[MAXUSERENVIRON];
1132 			extern char **environ;
1133 			extern int DtableSize;
1134 
1135 			/* child -- set up input & exec mailer */
1136 			(void) setsignal(SIGINT, SIG_IGN);
1137 			(void) setsignal(SIGHUP, SIG_IGN);
1138 			(void) setsignal(SIGTERM, SIG_DFL);
1139 
1140 			/* reset user and group */
1141 			if (!bitnset(M_RESTR, m->m_flags))
1142 			{
1143 				if (ctladdr == NULL || ctladdr->q_uid == 0)
1144 				{
1145 					(void) initgroups(DefUser, DefGid);
1146 					(void) setgid(DefGid);
1147 					(void) setuid(DefUid);
1148 				}
1149 				else
1150 				{
1151 					(void) initgroups(ctladdr->q_ruser?
1152 						ctladdr->q_ruser: ctladdr->q_user,
1153 						ctladdr->q_gid);
1154 					(void) setgid(ctladdr->q_gid);
1155 					(void) setuid(ctladdr->q_uid);
1156 				}
1157 			}
1158 
1159 			if (tTd(11, 2))
1160 				printf("openmailer: running as r/euid=%d/%d\n",
1161 					getuid(), geteuid());
1162 
1163 			/* move into some "safe" directory */
1164 			if (m->m_execdir != NULL)
1165 			{
1166 				char *p, *q;
1167 				char buf[MAXLINE];
1168 
1169 				for (p = m->m_execdir; p != NULL; p = q)
1170 				{
1171 					q = strchr(p, ':');
1172 					if (q != NULL)
1173 						*q = '\0';
1174 					expand(p, buf, &buf[sizeof buf] - 1, e);
1175 					if (q != NULL)
1176 						*q++ = ':';
1177 					if (tTd(11, 20))
1178 						printf("openmailer: trydir %s\n",
1179 							buf);
1180 					if (buf[0] != '\0' && chdir(buf) >= 0)
1181 						break;
1182 				}
1183 			}
1184 
1185 			/* arrange to filter std & diag output of command */
1186 			if (clever)
1187 			{
1188 				(void) close(rpvect[0]);
1189 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
1190 				{
1191 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
1192 						e->e_to, m->m_name, rpvect[1]);
1193 					_exit(EX_OSERR);
1194 				}
1195 				(void) close(rpvect[1]);
1196 			}
1197 			else if (OpMode == MD_SMTP || HoldErrs)
1198 			{
1199 				/* put mailer output in transcript */
1200 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
1201 				{
1202 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
1203 						e->e_to, m->m_name,
1204 						fileno(e->e_xfp));
1205 					_exit(EX_OSERR);
1206 				}
1207 			}
1208 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
1209 			{
1210 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
1211 					e->e_to, m->m_name);
1212 				_exit(EX_OSERR);
1213 			}
1214 
1215 			/* arrange to get standard input */
1216 			(void) close(mpvect[1]);
1217 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
1218 			{
1219 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
1220 					e->e_to, m->m_name, mpvect[0]);
1221 				_exit(EX_OSERR);
1222 			}
1223 			(void) close(mpvect[0]);
1224 
1225 			/* arrange for all the files to be closed */
1226 			for (i = 3; i < DtableSize; i++)
1227 			{
1228 				register int j;
1229 
1230 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
1231 					(void) fcntl(i, F_SETFD, j | 1);
1232 			}
1233 
1234 			/* set up the mailer environment */
1235 			i = 0;
1236 			env[i++] = "AGENT=sendmail";
1237 			for (ep = environ; *ep != NULL; ep++)
1238 			{
1239 				if (strncmp(*ep, "TZ=", 3) == 0)
1240 					env[i++] = *ep;
1241 			}
1242 			env[i++] = NULL;
1243 
1244 			/* try to execute the mailer */
1245 			execve(m->m_mailer, pv, env);
1246 			saveerrno = errno;
1247 			syserr("Cannot exec %s", m->m_mailer);
1248 			if (m == LocalMailer || transienterror(saveerrno))
1249 				_exit(EX_OSERR);
1250 			_exit(EX_UNAVAILABLE);
1251 		}
1252 
1253 		/*
1254 		**  Set up return value.
1255 		*/
1256 
1257 		mci = (MCI *) xalloc(sizeof *mci);
1258 		bzero((char *) mci, sizeof *mci);
1259 		mci->mci_mailer = m;
1260 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
1261 		mci->mci_pid = pid;
1262 		(void) close(mpvect[0]);
1263 		mci->mci_out = fdopen(mpvect[1], "w");
1264 		if (mci->mci_out == NULL)
1265 		{
1266 			syserr("deliver: cannot create mailer output channel, fd=%d",
1267 				mpvect[1]);
1268 			(void) close(mpvect[1]);
1269 			if (clever)
1270 			{
1271 				(void) close(rpvect[0]);
1272 				(void) close(rpvect[1]);
1273 			}
1274 			rcode = EX_OSERR;
1275 			goto give_up;
1276 		}
1277 		if (clever)
1278 		{
1279 			(void) close(rpvect[1]);
1280 			mci->mci_in = fdopen(rpvect[0], "r");
1281 			if (mci->mci_in == NULL)
1282 			{
1283 				syserr("deliver: cannot create mailer input channel, fd=%d",
1284 					mpvect[1]);
1285 				(void) close(rpvect[0]);
1286 				fclose(mci->mci_out);
1287 				mci->mci_out = NULL;
1288 				rcode = EX_OSERR;
1289 				goto give_up;
1290 			}
1291 		}
1292 		else
1293 		{
1294 			mci->mci_flags |= MCIF_TEMP;
1295 			mci->mci_in = NULL;
1296 		}
1297 	}
1298 
1299 	/*
1300 	**  If we are in SMTP opening state, send initial protocol.
1301 	*/
1302 
1303 	if (clever && mci->mci_state != MCIS_CLOSED)
1304 	{
1305 		smtpinit(m, mci, e);
1306 	}
1307 	if (tTd(11, 1))
1308 	{
1309 		printf("openmailer: ");
1310 		mci_dump(mci, FALSE);
1311 	}
1312 
1313 	if (mci->mci_state != MCIS_OPEN)
1314 	{
1315 		/* couldn't open the mailer */
1316 		rcode = mci->mci_exitstat;
1317 		errno = mci->mci_errno;
1318 #ifdef NAMED_BIND
1319 		h_errno = mci->mci_herrno;
1320 #endif
1321 		if (rcode == EX_OK)
1322 		{
1323 			/* shouldn't happen */
1324 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
1325 				rcode, mci->mci_state, firstsig);
1326 			rcode = EX_SOFTWARE;
1327 		}
1328 		else if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0')
1329 		{
1330 			/* try next MX site */
1331 			goto tryhost;
1332 		}
1333 	}
1334 	else if (!clever)
1335 	{
1336 		/*
1337 		**  Format and send message.
1338 		*/
1339 
1340 		putfromline(mci->mci_out, m, e);
1341 		(*e->e_puthdr)(mci->mci_out, m, e);
1342 		putline("\n", mci->mci_out, m);
1343 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1344 
1345 		/* get the exit status */
1346 		rcode = endmailer(mci, e, pv);
1347 	}
1348 	else
1349 #ifdef SMTP
1350 	{
1351 		/*
1352 		**  Send the MAIL FROM: protocol
1353 		*/
1354 
1355 		rcode = smtpmailfrom(m, mci, e);
1356 		if (rcode == EX_OK)
1357 		{
1358 			register char *t = tobuf;
1359 			register int i;
1360 
1361 			/* send the recipient list */
1362 			tobuf[0] = '\0';
1363 			for (to = tochain; to != NULL; to = to->q_tchain)
1364 			{
1365 				e->e_to = to->q_paddr;
1366 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
1367 				{
1368 					markfailure(e, to, i);
1369 					giveresponse(i, m, mci, ctladdr, e);
1370 				}
1371 				else
1372 				{
1373 					*t++ = ',';
1374 					for (p = to->q_paddr; *p; *t++ = *p++)
1375 						continue;
1376 					*t = '\0';
1377 				}
1378 			}
1379 
1380 			/* now send the data */
1381 			if (tobuf[0] == '\0')
1382 			{
1383 				rcode = EX_OK;
1384 				e->e_to = NULL;
1385 				if (bitset(MCIF_CACHED, mci->mci_flags))
1386 					smtprset(m, mci, e);
1387 			}
1388 			else
1389 			{
1390 				e->e_to = tobuf + 1;
1391 				rcode = smtpdata(m, mci, e);
1392 			}
1393 
1394 			/* now close the connection */
1395 			if (!bitset(MCIF_CACHED, mci->mci_flags))
1396 				smtpquit(m, mci, e);
1397 		}
1398 		if (rcode != EX_OK && curhost != NULL && *curhost != '\0')
1399 		{
1400 			/* try next MX site */
1401 			goto tryhost;
1402 		}
1403 	}
1404 #else /* not SMTP */
1405 	{
1406 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1407 		rcode = EX_CONFIG;
1408 		goto give_up;
1409 	}
1410 #endif /* SMTP */
1411 #ifdef NAMED_BIND
1412 	if (ConfigLevel < 2)
1413 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1414 #endif
1415 
1416 	/* arrange a return receipt if requested */
1417 	if (rcode == EX_OK && e->e_receiptto != NULL &&
1418 	    bitnset(M_LOCALMAILER, m->m_flags))
1419 	{
1420 		e->e_flags |= EF_SENDRECEIPT;
1421 		/* do we want to send back more info? */
1422 	}
1423 
1424 	/*
1425 	**  Do final status disposal.
1426 	**	We check for something in tobuf for the SMTP case.
1427 	**	If we got a temporary failure, arrange to queue the
1428 	**		addressees.
1429 	*/
1430 
1431   give_up:
1432 	if (tobuf[0] != '\0')
1433 		giveresponse(rcode, m, mci, ctladdr, e);
1434 	for (to = tochain; to != NULL; to = to->q_tchain)
1435 	{
1436 		if (rcode != EX_OK)
1437 			markfailure(e, to, rcode);
1438 		else
1439 		{
1440 			to->q_flags |= QSENT;
1441 			e->e_nsent++;
1442 			if (e->e_receiptto != NULL &&
1443 			    bitnset(M_LOCALMAILER, m->m_flags))
1444 			{
1445 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
1446 					to->q_paddr);
1447 			}
1448 		}
1449 	}
1450 
1451 	/*
1452 	**  Restore state and return.
1453 	*/
1454 
1455 #ifdef XDEBUG
1456 	{
1457 		char wbuf[MAXLINE];
1458 
1459 		/* make absolutely certain 0, 1, and 2 are in use */
1460 		sprintf(wbuf, "%s... end of deliver(%s)",
1461 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
1462 			m->m_name);
1463 		checkfd012(wbuf);
1464 	}
1465 #endif
1466 
1467 	errno = 0;
1468 	define('g', (char *) NULL, e);
1469 	return (rcode);
1470 }
1471 /*
1472 **  MARKFAILURE -- mark a failure on a specific address.
1473 **
1474 **	Parameters:
1475 **		e -- the envelope we are sending.
1476 **		q -- the address to mark.
1477 **		rcode -- the code signifying the particular failure.
1478 **
1479 **	Returns:
1480 **		none.
1481 **
1482 **	Side Effects:
1483 **		marks the address (and possibly the envelope) with the
1484 **			failure so that an error will be returned or
1485 **			the message will be queued, as appropriate.
1486 */
1487 
1488 markfailure(e, q, rcode)
1489 	register ENVELOPE *e;
1490 	register ADDRESS *q;
1491 	int rcode;
1492 {
1493 	char buf[MAXLINE];
1494 
1495 	switch (rcode)
1496 	{
1497 	  case EX_OK:
1498 		break;
1499 
1500 	  case EX_TEMPFAIL:
1501 	  case EX_IOERR:
1502 	  case EX_OSERR:
1503 		q->q_flags |= QQUEUEUP;
1504 		break;
1505 
1506 	  default:
1507 		q->q_flags |= QBADADDR;
1508 		break;
1509 	}
1510 }
1511 /*
1512 **  ENDMAILER -- Wait for mailer to terminate.
1513 **
1514 **	We should never get fatal errors (e.g., segmentation
1515 **	violation), so we report those specially.  For other
1516 **	errors, we choose a status message (into statmsg),
1517 **	and if it represents an error, we print it.
1518 **
1519 **	Parameters:
1520 **		pid -- pid of mailer.
1521 **		e -- the current envelope.
1522 **		pv -- the parameter vector that invoked the mailer
1523 **			(for error messages).
1524 **
1525 **	Returns:
1526 **		exit code of mailer.
1527 **
1528 **	Side Effects:
1529 **		none.
1530 */
1531 
1532 endmailer(mci, e, pv)
1533 	register MCI *mci;
1534 	register ENVELOPE *e;
1535 	char **pv;
1536 {
1537 	int st;
1538 
1539 	/* close any connections */
1540 	if (mci->mci_in != NULL)
1541 		(void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in");
1542 	if (mci->mci_out != NULL)
1543 		(void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out");
1544 	mci->mci_in = mci->mci_out = NULL;
1545 	mci->mci_state = MCIS_CLOSED;
1546 
1547 	/* in the IPC case there is nothing to wait for */
1548 	if (mci->mci_pid == 0)
1549 		return (EX_OK);
1550 
1551 	/* wait for the mailer process to die and collect status */
1552 	st = waitfor(mci->mci_pid);
1553 	if (st == -1)
1554 	{
1555 		syserr("endmailer %s: wait", pv[0]);
1556 		return (EX_SOFTWARE);
1557 	}
1558 
1559 	if (WIFEXITED(st))
1560 	{
1561 		/* normal death -- return status */
1562 		return (WEXITSTATUS(st));
1563 	}
1564 
1565 	/* it died a horrid death */
1566 	syserr("mailer %s died with signal %o", mci->mci_mailer->m_name, st);
1567 
1568 	/* log the arguments */
1569 	if (pv != NULL && e->e_xfp != NULL)
1570 	{
1571 		register char **av;
1572 
1573 		fprintf(e->e_xfp, "Arguments:");
1574 		for (av = pv; *av != NULL; av++)
1575 			fprintf(e->e_xfp, " %s", *av);
1576 		fprintf(e->e_xfp, "\n");
1577 	}
1578 
1579 	ExitStat = EX_TEMPFAIL;
1580 	return (EX_TEMPFAIL);
1581 }
1582 /*
1583 **  GIVERESPONSE -- Interpret an error response from a mailer
1584 **
1585 **	Parameters:
1586 **		stat -- the status code from the mailer (high byte
1587 **			only; core dumps must have been taken care of
1588 **			already).
1589 **		m -- the mailer info for this mailer.
1590 **		mci -- the mailer connection info -- can be NULL if the
1591 **			response is given before the connection is made.
1592 **		ctladdr -- the controlling address for the recipient
1593 **			address(es).
1594 **		e -- the current envelope.
1595 **
1596 **	Returns:
1597 **		none.
1598 **
1599 **	Side Effects:
1600 **		Errors may be incremented.
1601 **		ExitStat may be set.
1602 */
1603 
1604 giveresponse(stat, m, mci, ctladdr, e)
1605 	int stat;
1606 	register MAILER *m;
1607 	register MCI *mci;
1608 	ADDRESS *ctladdr;
1609 	ENVELOPE *e;
1610 {
1611 	register const char *statmsg;
1612 	extern char *SysExMsg[];
1613 	register int i;
1614 	extern int N_SysEx;
1615 	char buf[MAXLINE];
1616 
1617 	/*
1618 	**  Compute status message from code.
1619 	*/
1620 
1621 	i = stat - EX__BASE;
1622 	if (stat == 0)
1623 	{
1624 		statmsg = "250 Sent";
1625 		if (e->e_statmsg != NULL)
1626 		{
1627 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
1628 			statmsg = buf;
1629 		}
1630 	}
1631 	else if (i < 0 || i > N_SysEx)
1632 	{
1633 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1634 		stat = EX_UNAVAILABLE;
1635 		statmsg = buf;
1636 	}
1637 	else if (stat == EX_TEMPFAIL)
1638 	{
1639 		(void) strcpy(buf, SysExMsg[i] + 1);
1640 #ifdef NAMED_BIND
1641 		if (h_errno == TRY_AGAIN)
1642 			statmsg = errstring(h_errno+E_DNSBASE);
1643 		else
1644 #endif
1645 		{
1646 			if (errno != 0)
1647 				statmsg = errstring(errno);
1648 			else
1649 			{
1650 #ifdef SMTP
1651 				statmsg = SmtpError;
1652 #else /* SMTP */
1653 				statmsg = NULL;
1654 #endif /* SMTP */
1655 			}
1656 		}
1657 		if (statmsg != NULL && statmsg[0] != '\0')
1658 		{
1659 			(void) strcat(buf, ": ");
1660 			(void) strcat(buf, statmsg);
1661 		}
1662 		statmsg = buf;
1663 	}
1664 #ifdef NAMED_BIND
1665 	else if (stat == EX_NOHOST && h_errno != 0)
1666 	{
1667 		statmsg = errstring(h_errno + E_DNSBASE);
1668 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1669 		statmsg = buf;
1670 	}
1671 #endif
1672 	else
1673 	{
1674 		statmsg = SysExMsg[i];
1675 		if (*statmsg++ == ':')
1676 		{
1677 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
1678 			statmsg = buf;
1679 		}
1680 	}
1681 
1682 	/*
1683 	**  Print the message as appropriate
1684 	*/
1685 
1686 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1687 	{
1688 		extern char MsgBuf[];
1689 
1690 		message(&statmsg[4], errstring(errno));
1691 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
1692 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
1693 	}
1694 	else
1695 	{
1696 		Errors++;
1697 		usrerr(statmsg, errstring(errno));
1698 	}
1699 
1700 	/*
1701 	**  Final cleanup.
1702 	**	Log a record of the transaction.  Compute the new
1703 	**	ExitStat -- if we already had an error, stick with
1704 	**	that.
1705 	*/
1706 
1707 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
1708 		logdelivery(m, mci, &statmsg[4], ctladdr, e);
1709 
1710 	if (stat != EX_TEMPFAIL)
1711 		setstat(stat);
1712 	if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL))
1713 	{
1714 		if (e->e_message != NULL)
1715 			free(e->e_message);
1716 		e->e_message = newstr(&statmsg[4]);
1717 	}
1718 	errno = 0;
1719 #ifdef NAMED_BIND
1720 	h_errno = 0;
1721 #endif
1722 }
1723 /*
1724 **  LOGDELIVERY -- log the delivery in the system log
1725 **
1726 **	Care is taken to avoid logging lines that are too long, because
1727 **	some versions of syslog have an unfortunate proclivity for core
1728 **	dumping.  This is a hack, to be sure, that is at best empirical.
1729 **
1730 **	Parameters:
1731 **		m -- the mailer info.  Can be NULL for initial queue.
1732 **		mci -- the mailer connection info -- can be NULL if the
1733 **			log is occuring when no connection is active.
1734 **		stat -- the message to print for the status.
1735 **		ctladdr -- the controlling address for the to list.
1736 **		e -- the current envelope.
1737 **
1738 **	Returns:
1739 **		none
1740 **
1741 **	Side Effects:
1742 **		none
1743 */
1744 
1745 logdelivery(m, mci, stat, ctladdr, e)
1746 	MAILER *m;
1747 	register MCI *mci;
1748 	char *stat;
1749 	ADDRESS *ctladdr;
1750 	register ENVELOPE *e;
1751 {
1752 # ifdef LOG
1753 	register char *bp;
1754 	register char *p;
1755 	int l;
1756 	char buf[512];
1757 
1758 #  if (SYSLOG_BUFSIZE) >= 256
1759 	bp = buf;
1760 	if (ctladdr != NULL)
1761 	{
1762 		strcpy(bp, ", ctladdr=");
1763 		strcat(bp, shortenstring(ctladdr->q_paddr, 83));
1764 		bp += strlen(bp);
1765 		if (bitset(QGOODUID, ctladdr->q_flags))
1766 		{
1767 			(void) sprintf(bp, " (%d/%d)",
1768 					ctladdr->q_uid, ctladdr->q_gid);
1769 			bp += strlen(bp);
1770 		}
1771 	}
1772 
1773 	(void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
1774 	bp += strlen(bp);
1775 
1776 	if (m != NULL)
1777 	{
1778 		(void) strcpy(bp, ", mailer=");
1779 		(void) strcat(bp, m->m_name);
1780 		bp += strlen(bp);
1781 	}
1782 
1783 	if (mci != NULL && mci->mci_host != NULL)
1784 	{
1785 # ifdef DAEMON
1786 		extern SOCKADDR CurHostAddr;
1787 # endif
1788 
1789 		(void) strcpy(bp, ", relay=");
1790 		(void) strcat(bp, mci->mci_host);
1791 
1792 # ifdef DAEMON
1793 		(void) strcat(bp, " (");
1794 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
1795 		(void) strcat(bp, ")");
1796 # endif
1797 	}
1798 	else
1799 	{
1800 		char *p = macvalue('h', e);
1801 
1802 		if (p != NULL && p[0] != '\0')
1803 		{
1804 			(void) strcpy(bp, ", relay=");
1805 			(void) strcat(bp, p);
1806 		}
1807 	}
1808 	bp += strlen(bp);
1809 
1810 #define STATLEN		(((SYSLOG_BUFSIZE) - 100) / 4)
1811 #if (STATLEN) < 63
1812 # undef STATLEN
1813 # define STATLEN	63
1814 #endif
1815 #if (STATLEN) > 203
1816 # undef STATLEN
1817 # define STATLEN	203
1818 #endif
1819 
1820 	if ((bp - buf) > (sizeof buf - ((STATLEN) + 20)))
1821 	{
1822 		/* desperation move -- truncate data */
1823 		bp = buf + sizeof buf - ((STATLEN) + 17);
1824 		strcpy(bp, "...");
1825 		bp += 3;
1826 	}
1827 
1828 	(void) strcpy(bp, ", stat=");
1829 	bp += strlen(bp);
1830 
1831 	(void) strcpy(bp, shortenstring(stat, (STATLEN)));
1832 
1833 	l = SYSLOG_BUFSIZE - 100 - strlen(buf);
1834 	p = e->e_to;
1835 	while (strlen(p) >= l)
1836 	{
1837 		register char *q = strchr(p + l, ',');
1838 
1839 		if (q == NULL)
1840 			break;
1841 		syslog(LOG_INFO, "%s: to=%.*s [more]%s",
1842 			e->e_id, ++q - p, p, buf);
1843 		p = q;
1844 	}
1845 	syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf);
1846 
1847 #  else		/* we have a very short log buffer size */
1848 
1849 	l = SYSLOG_BUFSIZE - 40;
1850 	p = e->e_to;
1851 	while (strlen(p) >= l)
1852 	{
1853 		register char *q = strchr(p + l, ',');
1854 
1855 		if (q == NULL)
1856 			break;
1857 		syslog(LOG_INFO, "%s: to=%.*s [more]",
1858 			e->e_id, ++q - p, p);
1859 		p = q;
1860 	}
1861 	syslog(LOG_INFO, "%s: to=%s", e->e_id, p);
1862 
1863 	if (ctladdr != NULL)
1864 	{
1865 		bp = buf;
1866 		strcpy(buf, "ctladdr=");
1867 		bp += strlen(buf);
1868 		strcpy(bp, shortenstring(ctladdr->q_paddr, 83));
1869 		bp += strlen(buf);
1870 		if (bitset(QGOODUID, ctladdr->q_flags))
1871 		{
1872 			(void) sprintf(bp, " (%d/%d)",
1873 					ctladdr->q_uid, ctladdr->q_gid);
1874 			bp += strlen(bp);
1875 		}
1876 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
1877 	}
1878 	syslog(LOG_INFO, "%s: delay=%s",
1879 		e->e_id, pintvl(curtime() - e->e_ctime, TRUE));
1880 
1881 	if (m != NULL)
1882 		syslog(LOG_INFO, "%s: mailer=%s", e->e_id, m->m_name);
1883 
1884 	if (mci != NULL && mci->mci_host != NULL)
1885 	{
1886 # ifdef DAEMON
1887 		extern SOCKADDR CurHostAddr;
1888 # endif
1889 
1890 		(void) strcpy(buf, mci->mci_host);
1891 
1892 # ifdef DAEMON
1893 		(void) strcat(buf, " (");
1894 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
1895 		(void) strcat(buf, ")");
1896 # endif
1897 		syslog(LOG_INFO, "%s: relay=%s", e->e_id, buf);
1898 	}
1899 	else
1900 	{
1901 		char *p = macvalue('h', e);
1902 
1903 		if (p != NULL && p[0] != '\0')
1904 			syslog(LOG_INFO, "%s: relay=%s", e->e_id, p);
1905 	}
1906 
1907 	syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63));
1908 #  endif /* short log buffer */
1909 # endif /* LOG */
1910 }
1911 /*
1912 **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
1913 **
1914 **	This can be made an arbitrary message separator by changing $l
1915 **
1916 **	One of the ugliest hacks seen by human eyes is contained herein:
1917 **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
1918 **	does a well-meaning programmer such as myself have to deal with
1919 **	this kind of antique garbage????
1920 **
1921 **	Parameters:
1922 **		fp -- the file to output to.
1923 **		m -- the mailer describing this entry.
1924 **
1925 **	Returns:
1926 **		none
1927 **
1928 **	Side Effects:
1929 **		outputs some text to fp.
1930 */
1931 
1932 putfromline(fp, m, e)
1933 	register FILE *fp;
1934 	register MAILER *m;
1935 	ENVELOPE *e;
1936 {
1937 	char *template = "\201l\n";
1938 	char buf[MAXLINE];
1939 
1940 	if (bitnset(M_NHDR, m->m_flags))
1941 		return;
1942 
1943 # ifdef UGLYUUCP
1944 	if (bitnset(M_UGLYUUCP, m->m_flags))
1945 	{
1946 		char *bang;
1947 		char xbuf[MAXLINE];
1948 
1949 		expand("\201g", buf, &buf[sizeof buf - 1], e);
1950 		bang = strchr(buf, '!');
1951 		if (bang == NULL)
1952 		{
1953 			errno = 0;
1954 			syserr("554 No ! in UUCP From address! (%s given)", buf);
1955 		}
1956 		else
1957 		{
1958 			*bang++ = '\0';
1959 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1960 			template = xbuf;
1961 		}
1962 	}
1963 # endif /* UGLYUUCP */
1964 	expand(template, buf, &buf[sizeof buf - 1], e);
1965 	putline(buf, fp, m);
1966 }
1967 /*
1968 **  PUTBODY -- put the body of a message.
1969 **
1970 **	Parameters:
1971 **		fp -- file to output onto.
1972 **		m -- a mailer descriptor to control output format.
1973 **		e -- the envelope to put out.
1974 **		separator -- if non-NULL, a message separator that must
1975 **			not be permitted in the resulting message.
1976 **
1977 **	Returns:
1978 **		none.
1979 **
1980 **	Side Effects:
1981 **		The message is written onto fp.
1982 */
1983 
1984 putbody(fp, m, e, separator)
1985 	FILE *fp;
1986 	MAILER *m;
1987 	register ENVELOPE *e;
1988 	char *separator;
1989 {
1990 	char buf[MAXLINE];
1991 
1992 	/*
1993 	**  Output the body of the message
1994 	*/
1995 
1996 	if (e->e_dfp == NULL)
1997 	{
1998 		if (e->e_df != NULL)
1999 		{
2000 			e->e_dfp = fopen(e->e_df, "r");
2001 			if (e->e_dfp == NULL)
2002 				syserr("putbody: Cannot open %s for %s from %s",
2003 				e->e_df, e->e_to, e->e_from.q_paddr);
2004 		}
2005 		else
2006 			putline("<<< No Message Collected >>>", fp, m);
2007 	}
2008 	if (e->e_dfp != NULL)
2009 	{
2010 		rewind(e->e_dfp);
2011 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
2012 		{
2013 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
2014 			    strncmp(buf, "From ", 5) == 0)
2015 				(void) putc('>', fp);
2016 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
2017 			{
2018 				/* possible separator */
2019 				int sl = strlen(separator);
2020 
2021 				if (strncmp(&buf[2], separator, sl) == 0)
2022 					(void) putc(' ', fp);
2023 			}
2024 			putline(buf, fp, m);
2025 		}
2026 
2027 		if (ferror(e->e_dfp))
2028 		{
2029 			syserr("putbody: %s: read error", e->e_df);
2030 			ExitStat = EX_IOERR;
2031 		}
2032 	}
2033 
2034 	/* some mailers want extra blank line at end of message */
2035 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
2036 		putline("", fp, m);
2037 
2038 	(void) fflush(fp);
2039 	if (ferror(fp) && errno != EPIPE)
2040 	{
2041 		syserr("putbody: write error");
2042 		ExitStat = EX_IOERR;
2043 	}
2044 	errno = 0;
2045 }
2046 /*
2047 **  MAILFILE -- Send a message to a file.
2048 **
2049 **	If the file has the setuid/setgid bits set, but NO execute
2050 **	bits, sendmail will try to become the owner of that file
2051 **	rather than the real user.  Obviously, this only works if
2052 **	sendmail runs as root.
2053 **
2054 **	This could be done as a subordinate mailer, except that it
2055 **	is used implicitly to save messages in ~/dead.letter.  We
2056 **	view this as being sufficiently important as to include it
2057 **	here.  For example, if the system is dying, we shouldn't have
2058 **	to create another process plus some pipes to save the message.
2059 **
2060 **	Parameters:
2061 **		filename -- the name of the file to send to.
2062 **		ctladdr -- the controlling address header -- includes
2063 **			the userid/groupid to be when sending.
2064 **
2065 **	Returns:
2066 **		The exit code associated with the operation.
2067 **
2068 **	Side Effects:
2069 **		none.
2070 */
2071 
2072 mailfile(filename, ctladdr, e)
2073 	char *filename;
2074 	ADDRESS *ctladdr;
2075 	register ENVELOPE *e;
2076 {
2077 	register FILE *f;
2078 	register int pid;
2079 	int mode;
2080 
2081 	if (tTd(11, 1))
2082 	{
2083 		printf("mailfile %s\n  ctladdr=", filename);
2084 		printaddr(ctladdr, FALSE);
2085 	}
2086 
2087 	if (e->e_xfp != NULL)
2088 		fflush(e->e_xfp);
2089 
2090 	/*
2091 	**  Fork so we can change permissions here.
2092 	**	Note that we MUST use fork, not vfork, because of
2093 	**	the complications of calling subroutines, etc.
2094 	*/
2095 
2096 	DOFORK(fork);
2097 
2098 	if (pid < 0)
2099 		return (EX_OSERR);
2100 	else if (pid == 0)
2101 	{
2102 		/* child -- actually write to file */
2103 		struct stat stb;
2104 
2105 		(void) setsignal(SIGINT, SIG_DFL);
2106 		(void) setsignal(SIGHUP, SIG_DFL);
2107 		(void) setsignal(SIGTERM, SIG_DFL);
2108 		(void) umask(OldUmask);
2109 
2110 		if (stat(filename, &stb) < 0)
2111 			stb.st_mode = FileMode;
2112 		mode = stb.st_mode;
2113 
2114 		/* limit the errors to those actually caused in the child */
2115 		errno = 0;
2116 		ExitStat = EX_OK;
2117 
2118 		if (bitset(0111, stb.st_mode))
2119 			exit(EX_CANTCREAT);
2120 		if (ctladdr != NULL)
2121 		{
2122 			/* ignore setuid and setgid bits */
2123 			mode &= ~(S_ISGID|S_ISUID);
2124 		}
2125 
2126 		/* we have to open the dfile BEFORE setuid */
2127 		if (e->e_dfp == NULL && e->e_df != NULL)
2128 		{
2129 			e->e_dfp = fopen(e->e_df, "r");
2130 			if (e->e_dfp == NULL)
2131 			{
2132 				syserr("mailfile: Cannot open %s for %s from %s",
2133 					e->e_df, e->e_to, e->e_from.q_paddr);
2134 			}
2135 		}
2136 
2137 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
2138 		{
2139 			if (ctladdr == NULL || ctladdr->q_uid == 0)
2140 			{
2141 				(void) initgroups(DefUser, DefGid);
2142 			}
2143 			else
2144 			{
2145 				(void) initgroups(ctladdr->q_ruser ?
2146 					ctladdr->q_ruser : ctladdr->q_user,
2147 					ctladdr->q_gid);
2148 			}
2149 		}
2150 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
2151 		{
2152 			if (ctladdr == NULL || ctladdr->q_uid == 0)
2153 				(void) setuid(DefUid);
2154 			else
2155 				(void) setuid(ctladdr->q_uid);
2156 		}
2157 		FileName = filename;
2158 		LineNumber = 0;
2159 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
2160 		if (f == NULL)
2161 		{
2162 			message("554 cannot open: %s", errstring(errno));
2163 			exit(EX_CANTCREAT);
2164 		}
2165 
2166 		putfromline(f, FileMailer, e);
2167 		(*e->e_puthdr)(f, FileMailer, e);
2168 		putline("\n", f, FileMailer);
2169 		(*e->e_putbody)(f, FileMailer, e, NULL);
2170 		putline("\n", f, FileMailer);
2171 		if (ferror(f))
2172 		{
2173 			message("451 I/O error: %s", errstring(errno));
2174 			setstat(EX_IOERR);
2175 		}
2176 		(void) xfclose(f, "mailfile", filename);
2177 		(void) fflush(stdout);
2178 
2179 		/* reset ISUID & ISGID bits for paranoid systems */
2180 		(void) chmod(filename, (int) stb.st_mode);
2181 		exit(ExitStat);
2182 		/*NOTREACHED*/
2183 	}
2184 	else
2185 	{
2186 		/* parent -- wait for exit status */
2187 		int st;
2188 
2189 		st = waitfor(pid);
2190 		if (WIFEXITED(st))
2191 			return (WEXITSTATUS(st));
2192 		else
2193 		{
2194 			syserr("child died on signal %d", st);
2195 			return (EX_UNAVAILABLE);
2196 		}
2197 		/*NOTREACHED*/
2198 	}
2199 }
2200 /*
2201 **  HOSTSIGNATURE -- return the "signature" for a host.
2202 **
2203 **	The signature describes how we are going to send this -- it
2204 **	can be just the hostname (for non-Internet hosts) or can be
2205 **	an ordered list of MX hosts.
2206 **
2207 **	Parameters:
2208 **		m -- the mailer describing this host.
2209 **		host -- the host name.
2210 **		e -- the current envelope.
2211 **
2212 **	Returns:
2213 **		The signature for this host.
2214 **
2215 **	Side Effects:
2216 **		Can tweak the symbol table.
2217 */
2218 
2219 char *
2220 hostsignature(m, host, e)
2221 	register MAILER *m;
2222 	char *host;
2223 	ENVELOPE *e;
2224 {
2225 	register char *p;
2226 	register STAB *s;
2227 	int i;
2228 	int len;
2229 #ifdef NAMED_BIND
2230 	int nmx;
2231 	auto int rcode;
2232 	char *hp;
2233 	char *endp;
2234 	int oldoptions;
2235 	char *mxhosts[MAXMXHOSTS + 1];
2236 #endif
2237 
2238 	/*
2239 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2240 	*/
2241 
2242 	p = m->m_mailer;
2243 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2244 	{
2245 		/* just an ordinary mailer */
2246 		return host;
2247 	}
2248 
2249 	/*
2250 	**  Look it up in the symbol table.
2251 	*/
2252 
2253 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2254 	if (s->s_hostsig != NULL)
2255 		return s->s_hostsig;
2256 
2257 	/*
2258 	**  Not already there -- create a signature.
2259 	*/
2260 
2261 #ifdef NAMED_BIND
2262 	if (ConfigLevel < 2)
2263 	{
2264 		oldoptions = _res.options;
2265 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2266 	}
2267 
2268 	for (hp = host; hp != NULL; hp = endp)
2269 	{
2270 		endp = strchr(hp, ':');
2271 		if (endp != NULL)
2272 			*endp = '\0';
2273 
2274 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
2275 
2276 		if (nmx <= 0)
2277 		{
2278 			register MCI *mci;
2279 
2280 			/* update the connection info for this host */
2281 			mci = mci_get(hp, m);
2282 			mci->mci_exitstat = rcode;
2283 			mci->mci_errno = errno;
2284 #ifdef NAMED_BIND
2285 			mci->mci_herrno = h_errno;
2286 #endif
2287 
2288 			/* and return the original host name as the signature */
2289 			nmx = 1;
2290 			mxhosts[0] = hp;
2291 		}
2292 
2293 		len = 0;
2294 		for (i = 0; i < nmx; i++)
2295 		{
2296 			len += strlen(mxhosts[i]) + 1;
2297 		}
2298 		if (s->s_hostsig != NULL)
2299 			len += strlen(s->s_hostsig) + 1;
2300 		p = xalloc(len);
2301 		if (s->s_hostsig != NULL)
2302 		{
2303 			(void) strcpy(p, s->s_hostsig);
2304 			free(s->s_hostsig);
2305 			s->s_hostsig = p;
2306 			p += strlen(p);
2307 			*p++ = ':';
2308 		}
2309 		else
2310 			s->s_hostsig = p;
2311 		for (i = 0; i < nmx; i++)
2312 		{
2313 			if (i != 0)
2314 				*p++ = ':';
2315 			strcpy(p, mxhosts[i]);
2316 			p += strlen(p);
2317 		}
2318 		if (endp != NULL)
2319 			*endp++ = ':';
2320 	}
2321 	makelower(s->s_hostsig);
2322 	if (ConfigLevel < 2)
2323 		_res.options = oldoptions;
2324 #else
2325 	/* not using BIND -- the signature is just the host name */
2326 	s->s_hostsig = host;
2327 #endif
2328 	if (tTd(17, 1))
2329 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2330 	return s->s_hostsig;
2331 }
2332