1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)deliver.c	5.64 (Berkeley) 07/14/92";
11 #endif /* not lint */
12 
13 #include "sendmail.h"
14 #include <sys/signal.h>
15 #include <sys/stat.h>
16 #include <netdb.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #ifdef NAMED_BIND
20 #include <sys/param.h>
21 #include <arpa/nameser.h>
22 #include <resolv.h>
23 #endif
24 
25 /*
26 **  DELIVER -- Deliver a message to a list of addresses.
27 **
28 **	This routine delivers to everyone on the same host as the
29 **	user on the head of the list.  It is clever about mailers
30 **	that don't handle multiple users.  It is NOT guaranteed
31 **	that it will deliver to all these addresses however -- so
32 **	deliver should be called once for each address on the
33 **	list.
34 **
35 **	Parameters:
36 **		e -- the envelope to deliver.
37 **		firstto -- head of the address list to deliver to.
38 **
39 **	Returns:
40 **		zero -- successfully delivered.
41 **		else -- some failure, see ExitStat for more info.
42 **
43 **	Side Effects:
44 **		The standard input is passed off to someone.
45 */
46 
47 deliver(e, firstto)
48 	register ENVELOPE *e;
49 	ADDRESS *firstto;
50 {
51 	char *host;			/* host being sent to */
52 	char *user;			/* user being sent to */
53 	char **pvp;
54 	register char **mvp;
55 	register char *p;
56 	register MAILER *m;		/* mailer for this recipient */
57 	ADDRESS *ctladdr;
58 	register MCI *mci;
59 	register ADDRESS *to = firstto;
60 	bool clever = FALSE;		/* running user smtp to this mailer */
61 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
62 	int rcode;			/* response code */
63 	char *from;			/* pointer to from person */
64 	char *pv[MAXPV+1];
65 	char tobuf[MAXLINE-50];		/* text line of to people */
66 	char buf[MAXNAME];
67 	char tfrombuf[MAXNAME];		/* translated from person */
68 	char rpathbuf[MAXNAME];		/* translated return path */
69 	extern bool checkcompat();
70 	extern ADDRESS *getctladdr();
71 	extern char *remotename();
72 	extern MCI *openmailer();
73 
74 	errno = 0;
75 	if (bitset(QDONTSEND, to->q_flags))
76 		return (0);
77 
78 #ifdef NAMED_BIND
79 	/* unless interactive, try twice, over a minute */
80 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
81 		_res.retrans = 30;
82 		_res.retry = 2;
83 	}
84 #endif
85 
86 	m = to->q_mailer;
87 	host = to->q_host;
88 
89 	if (tTd(10, 1))
90 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
91 			m->m_mno, host, to->q_user);
92 
93 	/*
94 	**  If this mailer is expensive, and if we don't want to make
95 	**  connections now, just mark these addresses and return.
96 	**	This is useful if we want to batch connections to
97 	**	reduce load.  This will cause the messages to be
98 	**	queued up, and a daemon will come along to send the
99 	**	messages later.
100 	**		This should be on a per-mailer basis.
101 	*/
102 
103 	if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) &&
104 	    !Verbose)
105 	{
106 		for (; to != NULL; to = to->q_next)
107 		{
108 			if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m)
109 				continue;
110 			to->q_flags |= QQUEUEUP|QDONTSEND;
111 			e->e_to = to->q_paddr;
112 			message(Arpa_Info, "queued");
113 			if (LogLevel > 4)
114 				logdelivery("queued", e);
115 		}
116 		e->e_to = NULL;
117 		return (0);
118 	}
119 
120 	/*
121 	**  Do initial argv setup.
122 	**	Insert the mailer name.  Notice that $x expansion is
123 	**	NOT done on the mailer name.  Then, if the mailer has
124 	**	a picky -f flag, we insert it as appropriate.  This
125 	**	code does not check for 'pv' overflow; this places a
126 	**	manifest lower limit of 4 for MAXPV.
127 	**		The from address rewrite is expected to make
128 	**		the address relative to the other end.
129 	*/
130 
131 	/* rewrite from address, using rewriting rules */
132 	(void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, TRUE, e));
133 	if (e->e_returnpath == e->e_sender)
134 	{
135 		from = rpathbuf;
136 	}
137 	else
138 	{
139 		(void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, TRUE, e));
140 		from = tfrombuf;
141 	}
142 
143 	define('f', e->e_returnpath, e);	/* raw return path */
144 	define('<', rpathbuf, e);		/* translated return path */
145 	define('g', from, e);			/* translated sender */
146 	define('h', host, e);			/* to host */
147 	Errors = 0;
148 	pvp = pv;
149 	*pvp++ = m->m_argv[0];
150 
151 	/* insert -f or -r flag as appropriate */
152 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
153 	{
154 		if (bitnset(M_FOPT, m->m_flags))
155 			*pvp++ = "-f";
156 		else
157 			*pvp++ = "-r";
158 		*pvp++ = newstr(rpathbuf);
159 	}
160 
161 	/*
162 	**  Append the other fixed parts of the argv.  These run
163 	**  up to the first entry containing "$u".  There can only
164 	**  be one of these, and there are only a few more slots
165 	**  in the pv after it.
166 	*/
167 
168 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
169 	{
170 		while ((p = index(p, '\001')) != NULL)
171 			if (*++p == 'u')
172 				break;
173 		if (p != NULL)
174 			break;
175 
176 		/* this entry is safe -- go ahead and process it */
177 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
178 		*pvp++ = newstr(buf);
179 		if (pvp >= &pv[MAXPV - 3])
180 		{
181 			syserr("Too many parameters to %s before $u", pv[0]);
182 			return (-1);
183 		}
184 	}
185 
186 	/*
187 	**  If we have no substitution for the user name in the argument
188 	**  list, we know that we must supply the names otherwise -- and
189 	**  SMTP is the answer!!
190 	*/
191 
192 	if (*mvp == NULL)
193 	{
194 		/* running SMTP */
195 # ifdef SMTP
196 		clever = TRUE;
197 		*pvp = NULL;
198 # else SMTP
199 		/* oops!  we don't implement SMTP */
200 		syserr("SMTP style mailer");
201 		return (EX_SOFTWARE);
202 # endif SMTP
203 	}
204 
205 	/*
206 	**  At this point *mvp points to the argument with $u.  We
207 	**  run through our address list and append all the addresses
208 	**  we can.  If we run out of space, do not fret!  We can
209 	**  always send another copy later.
210 	*/
211 
212 	tobuf[0] = '\0';
213 	e->e_to = tobuf;
214 	ctladdr = NULL;
215 	for (; to != NULL; to = to->q_next)
216 	{
217 		/* avoid sending multiple recipients to dumb mailers */
218 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
219 			break;
220 
221 		/* if already sent or not for this host, don't send */
222 		if (bitset(QDONTSEND, to->q_flags) ||
223 		    strcmp(to->q_host, host) != 0 ||
224 		    to->q_mailer != firstto->q_mailer)
225 			continue;
226 
227 		/* avoid overflowing tobuf */
228 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
229 			break;
230 
231 		if (tTd(10, 1))
232 		{
233 			printf("\nsend to ");
234 			printaddr(to, FALSE);
235 		}
236 
237 		/* compute effective uid/gid when sending */
238 		if (to->q_mailer == ProgMailer)
239 			ctladdr = getctladdr(to);
240 
241 		user = to->q_user;
242 		e->e_to = to->q_paddr;
243 		to->q_flags |= QDONTSEND;
244 
245 		/*
246 		**  Check to see that these people are allowed to
247 		**  talk to each other.
248 		*/
249 
250 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
251 		{
252 			NoReturn = TRUE;
253 			usrerr("Message is too large; %ld bytes max", m->m_maxsize);
254 			giveresponse(EX_UNAVAILABLE, m, e);
255 			continue;
256 		}
257 		if (!checkcompat(to, e))
258 		{
259 			giveresponse(EX_UNAVAILABLE, m, e);
260 			continue;
261 		}
262 
263 		/*
264 		**  Strip quote bits from names if the mailer is dumb
265 		**	about them.
266 		*/
267 
268 		if (bitnset(M_STRIPQ, m->m_flags))
269 		{
270 			stripquotes(user);
271 			stripquotes(host);
272 		}
273 
274 		/* hack attack -- delivermail compatibility */
275 		if (m == ProgMailer && *user == '|')
276 			user++;
277 
278 		/*
279 		**  If an error message has already been given, don't
280 		**	bother to send to this address.
281 		**
282 		**	>>>>>>>>>> This clause assumes that the local mailer
283 		**	>> NOTE >> cannot do any further aliasing; that
284 		**	>>>>>>>>>> function is subsumed by sendmail.
285 		*/
286 
287 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
288 			continue;
289 
290 		/* save statistics.... */
291 		markstats(e, to);
292 
293 		/*
294 		**  See if this user name is "special".
295 		**	If the user name has a slash in it, assume that this
296 		**	is a file -- send it off without further ado.  Note
297 		**	that this type of addresses is not processed along
298 		**	with the others, so we fudge on the To person.
299 		*/
300 
301 		if (m == LocalMailer)
302 		{
303 			if (user[0] == '/')
304 			{
305 				rcode = mailfile(user, getctladdr(to), e);
306 				giveresponse(rcode, m, e);
307 				if (rcode == EX_OK)
308 					to->q_flags |= QSENT;
309 				continue;
310 			}
311 		}
312 
313 		/*
314 		**  Address is verified -- add this user to mailer
315 		**  argv, and add it to the print list of recipients.
316 		*/
317 
318 		/* link together the chain of recipients */
319 		to->q_tchain = tochain;
320 		tochain = to;
321 
322 		/* create list of users for error messages */
323 		(void) strcat(tobuf, ",");
324 		(void) strcat(tobuf, to->q_paddr);
325 		define('u', user, e);		/* to user */
326 		define('z', to->q_home, e);	/* user's home */
327 
328 		/*
329 		**  Expand out this user into argument list.
330 		*/
331 
332 		if (!clever)
333 		{
334 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
335 			*pvp++ = newstr(buf);
336 			if (pvp >= &pv[MAXPV - 2])
337 			{
338 				/* allow some space for trailing parms */
339 				break;
340 			}
341 		}
342 	}
343 
344 	/* see if any addresses still exist */
345 	if (tobuf[0] == '\0')
346 	{
347 		define('g', (char *) NULL, e);
348 		define('<', (char *) NULL, e);
349 		return (0);
350 	}
351 
352 	/* print out messages as full list */
353 	e->e_to = tobuf + 1;
354 
355 	/*
356 	**  Fill out any parameters after the $u parameter.
357 	*/
358 
359 	while (!clever && *++mvp != NULL)
360 	{
361 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
362 		*pvp++ = newstr(buf);
363 		if (pvp >= &pv[MAXPV])
364 			syserr("deliver: pv overflow after $u for %s", pv[0]);
365 	}
366 	*pvp++ = NULL;
367 
368 	/*
369 	**  Call the mailer.
370 	**	The argument vector gets built, pipes
371 	**	are created as necessary, and we fork & exec as
372 	**	appropriate.
373 	**	If we are running SMTP, we just need to clean up.
374 	*/
375 
376 	if (ctladdr == NULL)
377 		ctladdr = &e->e_from;
378 #ifdef NAMED_BIND
379 	if (ConfigLevel < 2)
380 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
381 #endif
382 	mci = openmailer(m, pv, ctladdr, clever, e);
383 	if (mci == NULL)
384 	{
385 		/* catastrophic error */
386 		rcode = -1;
387 		goto give_up;
388 	}
389 	else if (mci->mci_state != MCIS_OPEN)
390 	{
391 		/* couldn't open the mailer */
392 		rcode = mci->mci_exitstat;
393 		errno = mci->mci_errno;
394 		if (rcode == EX_OK)
395 		{
396 			/* shouldn't happen */
397 			rcode = EX_SOFTWARE;
398 		}
399 	}
400 	else if (!clever)
401 	{
402 		/*
403 		**  Format and send message.
404 		*/
405 
406 		putfromline(mci->mci_out, m, e);
407 		(*e->e_puthdr)(mci->mci_out, m, e);
408 		putline("\n", mci->mci_out, m);
409 		(*e->e_putbody)(mci->mci_out, m, e);
410 
411 		/* get the exit status */
412 		rcode = endmailer(mci, pv[0]);
413 	}
414 	else
415 #ifdef SMTP
416 	{
417 		/*
418 		**  Send the MAIL FROM: protocol
419 		*/
420 
421 		rcode = smtpmailfrom(m, mci, e);
422 		if (rcode == EX_OK)
423 		{
424 			register char *t = tobuf;
425 			register int i;
426 
427 			/* send the recipient list */
428 			tobuf[0] = '\0';
429 			for (to = tochain; to != NULL; to = to->q_tchain)
430 			{
431 				e->e_to = to->q_paddr;
432 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
433 				{
434 					markfailure(e, to, i);
435 					giveresponse(i, m, e);
436 				}
437 				else
438 				{
439 					*t++ = ',';
440 					for (p = to->q_paddr; *p; *t++ = *p++)
441 						continue;
442 				}
443 			}
444 
445 			/* now send the data */
446 			if (tobuf[0] == '\0')
447 			{
448 				e->e_to = NULL;
449 				if (bitset(MCIF_CACHED, mci->mci_flags))
450 					smtprset(m, mci, e);
451 			}
452 			else
453 			{
454 				e->e_to = tobuf + 1;
455 				rcode = smtpdata(m, mci, e);
456 			}
457 
458 			/* now close the connection */
459 			if (!bitset(MCIF_CACHED, mci->mci_flags))
460 				smtpquit(m, mci, e);
461 		}
462 	}
463 #else /* not SMTP */
464 	{
465 		syserr("deliver: need SMTP compiled to use clever mailer");
466 		rcode = -1;
467 		goto give_up;
468 	}
469 #endif /* SMTP */
470 #ifdef NAMED_BIND
471 	if (ConfigLevel < 2)
472 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
473 #endif
474 
475 	/* arrange a return receipt if requested */
476 	if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags))
477 	{
478 		e->e_flags |= EF_SENDRECEIPT;
479 		/* do we want to send back more info? */
480 	}
481 
482 	/*
483 	**  Do final status disposal.
484 	**	We check for something in tobuf for the SMTP case.
485 	**	If we got a temporary failure, arrange to queue the
486 	**		addressees.
487 	*/
488 
489   give_up:
490 	if (tobuf[0] != '\0')
491 		giveresponse(rcode, m, e);
492 	for (to = tochain; to != NULL; to = to->q_tchain)
493 	{
494 		if (rcode != EX_OK)
495 			markfailure(e, to, rcode);
496 		else
497 			to->q_flags |= QSENT;
498 	}
499 
500 	/*
501 	**  Restore state and return.
502 	*/
503 
504 	errno = 0;
505 	define('g', (char *) NULL, e);
506 	define('<', (char *) NULL, e);
507 	return (rcode);
508 }
509 /*
510 **  MARKFAILURE -- mark a failure on a specific address.
511 **
512 **	Parameters:
513 **		e -- the envelope we are sending.
514 **		q -- the address to mark.
515 **		rcode -- the code signifying the particular failure.
516 **
517 **	Returns:
518 **		none.
519 **
520 **	Side Effects:
521 **		marks the address (and possibly the envelope) with the
522 **			failure so that an error will be returned or
523 **			the message will be queued, as appropriate.
524 */
525 
526 markfailure(e, q, rcode)
527 	register ENVELOPE *e;
528 	register ADDRESS *q;
529 	int rcode;
530 {
531 	if (rcode == EX_OK)
532 		return;
533 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
534 		q->q_flags |= QBADADDR;
535 	else if (curtime() > e->e_ctime + TimeOut)
536 	{
537 		extern char *pintvl();
538 		char buf[MAXLINE];
539 
540 		if (!bitset(EF_TIMEOUT, e->e_flags))
541 		{
542 			(void) sprintf(buf, "Cannot send message for %s",
543 				pintvl(TimeOut, FALSE));
544 			if (e->e_message != NULL)
545 				free(e->e_message);
546 			e->e_message = newstr(buf);
547 			message(Arpa_Info, buf);
548 		}
549 		q->q_flags |= QBADADDR;
550 		e->e_flags |= EF_TIMEOUT;
551 	}
552 	else
553 		q->q_flags |= QQUEUEUP;
554 }
555 /*
556 **  DOFORK -- do a fork, retrying a couple of times on failure.
557 **
558 **	This MUST be a macro, since after a vfork we are running
559 **	two processes on the same stack!!!
560 **
561 **	Parameters:
562 **		none.
563 **
564 **	Returns:
565 **		From a macro???  You've got to be kidding!
566 **
567 **	Side Effects:
568 **		Modifies the ==> LOCAL <== variable 'pid', leaving:
569 **			pid of child in parent, zero in child.
570 **			-1 on unrecoverable error.
571 **
572 **	Notes:
573 **		I'm awfully sorry this looks so awful.  That's
574 **		vfork for you.....
575 */
576 
577 # define NFORKTRIES	5
578 
579 # ifndef FORK
580 # define FORK	fork
581 # endif
582 
583 # define DOFORK(fORKfN) \
584 {\
585 	register int i;\
586 \
587 	for (i = NFORKTRIES; --i >= 0; )\
588 	{\
589 		pid = fORKfN();\
590 		if (pid >= 0)\
591 			break;\
592 		if (i > 0)\
593 			sleep((unsigned) NFORKTRIES - i);\
594 	}\
595 }
596 /*
597 **  DOFORK -- simple fork interface to DOFORK.
598 **
599 **	Parameters:
600 **		none.
601 **
602 **	Returns:
603 **		pid of child in parent.
604 **		zero in child.
605 **		-1 on error.
606 **
607 **	Side Effects:
608 **		returns twice, once in parent and once in child.
609 */
610 
611 dofork()
612 {
613 	register int pid;
614 
615 	DOFORK(fork);
616 	return (pid);
617 }
618 /*
619 **  ENDMAILER -- Wait for mailer to terminate.
620 **
621 **	We should never get fatal errors (e.g., segmentation
622 **	violation), so we report those specially.  For other
623 **	errors, we choose a status message (into statmsg),
624 **	and if it represents an error, we print it.
625 **
626 **	Parameters:
627 **		pid -- pid of mailer.
628 **		name -- name of mailer (for error messages).
629 **
630 **	Returns:
631 **		exit code of mailer.
632 **
633 **	Side Effects:
634 **		none.
635 */
636 
637 endmailer(mci, name)
638 	register MCI *mci;
639 	char *name;
640 {
641 	int st;
642 
643 	/* close any connections */
644 	if (mci->mci_in != NULL)
645 		(void) fclose(mci->mci_in);
646 	if (mci->mci_out != NULL)
647 		(void) fclose(mci->mci_out);
648 	mci->mci_in = mci->mci_out = NULL;
649 	mci->mci_state = MCIS_CLOSED;
650 
651 	/* in the IPC case there is nothing to wait for */
652 	if (mci->mci_pid == 0)
653 		return (EX_OK);
654 
655 	/* wait for the mailer process to die and collect status */
656 	st = waitfor(mci->mci_pid);
657 	if (st == -1)
658 	{
659 		syserr("endmailer %s: wait", name);
660 		return (EX_SOFTWARE);
661 	}
662 
663 	/* see if it died a horrid death */
664 	if ((st & 0377) != 0)
665 	{
666 		syserr("mailer %s died with signal %o", name, st);
667 		ExitStat = EX_TEMPFAIL;
668 		return (EX_TEMPFAIL);
669 	}
670 
671 	/* normal death -- return status */
672 	st = (st >> 8) & 0377;
673 	return (st);
674 }
675 /*
676 **  OPENMAILER -- open connection to mailer.
677 **
678 **	Parameters:
679 **		m -- mailer descriptor.
680 **		pvp -- parameter vector to pass to mailer.
681 **		ctladdr -- controlling address for user.
682 **		clever -- create a full duplex connection.
683 **
684 **	Returns:
685 **		The mail connection info struct for this connection.
686 **		NULL on failure.
687 **
688 **	Side Effects:
689 **		creates a mailer in a subprocess.
690 */
691 
692 MCI *
693 openmailer(m, pvp, ctladdr, clever, e)
694 	MAILER *m;
695 	char **pvp;
696 	ADDRESS *ctladdr;
697 	bool clever;
698 	ENVELOPE *e;
699 {
700 	int pid;
701 	register MCI *mci;
702 	int mpvect[2];
703 	int rpvect[2];
704 	extern FILE *fdopen();
705 
706 	if (tTd(11, 1))
707 	{
708 		printf("openmailer:");
709 		printav(pvp);
710 	}
711 	errno = 0;
712 
713 	CurHostName = m->m_mailer;
714 
715 	/*
716 	**  Deal with the special case of mail handled through an IPC
717 	**  connection.
718 	**	In this case we don't actually fork.  We must be
719 	**	running SMTP for this to work.  We will return a
720 	**	zero pid to indicate that we are running IPC.
721 	**  We also handle a debug version that just talks to stdin/out.
722 	*/
723 
724 	/* check for Local Person Communication -- not for mortals!!! */
725 	if (strcmp(m->m_mailer, "[LPC]") == 0)
726 	{
727 		mci = (MCI *) xalloc(sizeof *mci);
728 		bzero((char *) mci, sizeof *mci);
729 		mci->mci_in = stdin;
730 		mci->mci_out = stdout;
731 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
732 		mci->mci_mailer = m;
733 	}
734 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
735 		 strcmp(m->m_mailer, "[TCP]") == 0)
736 	{
737 #ifdef DAEMON
738 		register int i, j;
739 		register u_short port;
740 		int nmx;
741 		char *mxhosts[MAXMXHOSTS + 1];
742 		extern MCI *mci_get();
743 
744 		CurHostName = pvp[1];
745 #ifdef NAMED_BIND
746 		if (CurHostName != NULL && CurHostName[0] != '\0' &&
747 		    CurHostName[0] != '[')
748 		{
749 			int rcode;
750 			char buf[MAXNAME];
751 
752 			expand("\001j", buf, &buf[sizeof(buf) - 1], e);
753 			nmx = getmxrr(CurHostName, mxhosts, buf, &rcode);
754 			if (nmx < 0)
755 			{
756 				mci = mci_get(CurHostName, m);
757 				mci->mci_exitstat = rcode;
758 				mci->mci_errno = errno;
759 			}
760 		}
761 		else
762 #endif
763 		{
764 			nmx = 1;
765 			mxhosts[0] = CurHostName;
766 		}
767 
768 		if (!clever)
769 			syserr("non-clever IPC");
770 		if (pvp[2] != NULL)
771 			port = atoi(pvp[2]);
772 		else
773 			port = 0;
774 		for (j = 0; j < nmx; j++)
775 		{
776 			/* see if we already know that this host is fried */
777 			CurHostName = mxhosts[j];
778 			mci = mci_get(CurHostName, m);
779 			if (mci->mci_state != MCIS_CLOSED)
780 				return mci;
781 			mci->mci_mailer = m;
782 			if (mci->mci_exitstat != EX_OK)
783 				continue;
784 
785 			/* try the connection */
786 			setproctitle("%s %s: %s", e->e_id, mxhosts[j], "user open");
787 			message(Arpa_Info, "Connecting to %s (%s)...",
788 				mxhosts[j], m->m_name);
789 			i = makeconnection(mxhosts[j], port, mci,
790 				bitnset(M_SECURE_PORT, m->m_flags));
791 			mci->mci_exitstat = i;
792 			mci->mci_errno = errno;
793 			if (i == EX_OK)
794 			{
795 				mci->mci_state = MCIS_OPENING;
796 				mci_cache(mci);
797 				break;
798 			}
799 			else if (tTd(11, 1))
800 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
801 					i, errno);
802 
803 
804 			/* enter status of this host */
805 			setstat(i);
806 		}
807 		mci->mci_pid = 0;
808 #else /* no DAEMON */
809 		syserr("openmailer: no IPC");
810 		return NULL;
811 #endif /* DAEMON */
812 	}
813 	else
814 	{
815 		/* create a pipe to shove the mail through */
816 		if (pipe(mpvect) < 0)
817 		{
818 			syserr("openmailer: pipe (to mailer)");
819 			return NULL;
820 		}
821 
822 		/* if this mailer speaks smtp, create a return pipe */
823 		if (clever && pipe(rpvect) < 0)
824 		{
825 			syserr("openmailer: pipe (from mailer)");
826 			(void) close(mpvect[0]);
827 			(void) close(mpvect[1]);
828 			return NULL;
829 		}
830 
831 		/*
832 		**  Actually fork the mailer process.
833 		**	DOFORK is clever about retrying.
834 		**
835 		**	Dispose of SIGCHLD signal catchers that may be laying
836 		**	around so that endmail will get it.
837 		*/
838 
839 		if (e->e_xfp != NULL)
840 			(void) fflush(e->e_xfp);		/* for debugging */
841 		(void) fflush(stdout);
842 # ifdef SIGCHLD
843 		(void) signal(SIGCHLD, SIG_DFL);
844 # endif SIGCHLD
845 		DOFORK(FORK);
846 		/* pid is set by DOFORK */
847 		if (pid < 0)
848 		{
849 			/* failure */
850 			syserr("openmailer: cannot fork");
851 			(void) close(mpvect[0]);
852 			(void) close(mpvect[1]);
853 			if (clever)
854 			{
855 				(void) close(rpvect[0]);
856 				(void) close(rpvect[1]);
857 			}
858 			return NULL;
859 		}
860 		else if (pid == 0)
861 		{
862 			int i;
863 			extern int DtableSize;
864 
865 			/* child -- set up input & exec mailer */
866 			/* make diagnostic output be standard output */
867 			(void) signal(SIGINT, SIG_IGN);
868 			(void) signal(SIGHUP, SIG_IGN);
869 			(void) signal(SIGTERM, SIG_DFL);
870 
871 			/* arrange to filter std & diag output of command */
872 			if (clever)
873 			{
874 				(void) close(rpvect[0]);
875 				(void) close(1);
876 				(void) dup(rpvect[1]);
877 				(void) close(rpvect[1]);
878 			}
879 			else if (OpMode == MD_SMTP || HoldErrs)
880 			{
881 				/* put mailer output in transcript */
882 				(void) close(1);
883 				(void) dup(fileno(e->e_xfp));
884 			}
885 			(void) close(2);
886 			(void) dup(1);
887 
888 			/* arrange to get standard input */
889 			(void) close(mpvect[1]);
890 			(void) close(0);
891 			if (dup(mpvect[0]) < 0)
892 			{
893 				syserr("Cannot dup to zero!");
894 				_exit(EX_OSERR);
895 			}
896 			(void) close(mpvect[0]);
897 			if (!bitnset(M_RESTR, m->m_flags))
898 			{
899 				if (ctladdr == NULL || ctladdr->q_uid == 0)
900 				{
901 					(void) setgid(DefGid);
902 					(void) initgroups(DefUser, DefGid);
903 					(void) setuid(DefUid);
904 				}
905 				else
906 				{
907 					(void) setgid(ctladdr->q_gid);
908 					(void) initgroups(ctladdr->q_ruser?
909 						ctladdr->q_ruser: ctladdr->q_user,
910 						ctladdr->q_gid);
911 					(void) setuid(ctladdr->q_uid);
912 				}
913 			}
914 
915 			/* arrange for all the files to be closed */
916 			for (i = 3; i < DtableSize; i++)
917 			{
918 				register int j;
919 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
920 					(void)fcntl(i, F_SETFD, j|1);
921 			}
922 
923 			/* try to execute the mailer */
924 			execve(m->m_mailer, pvp, UserEnviron);
925 			syserr("Cannot exec %s", m->m_mailer);
926 			if (m == LocalMailer)
927 				_exit(EX_TEMPFAIL);
928 			switch (errno)
929 			{
930 			  case EIO:
931 			  case EAGAIN:
932 			  case ENOMEM:
933 # ifdef EPROCLIM
934 			  case EPROCLIM:
935 # endif
936 				_exit(EX_TEMPFAIL);
937 			}
938 			_exit(EX_UNAVAILABLE);
939 		}
940 
941 		/*
942 		**  Set up return value.
943 		*/
944 
945 		mci = (MCI *) xalloc(sizeof *mci);
946 		bzero((char *) mci, sizeof *mci);
947 		mci->mci_mailer = m;
948 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
949 		mci->mci_pid = pid;
950 		(void) close(mpvect[0]);
951 		mci->mci_out = fdopen(mpvect[1], "w");
952 		if (clever)
953 		{
954 			(void) close(rpvect[1]);
955 			mci->mci_in = fdopen(rpvect[0], "r");
956 		}
957 		else
958 		{
959 			mci->mci_flags |= MCIF_TEMP;
960 			mci->mci_in = NULL;
961 		}
962 	}
963 
964 	/*
965 	**  If we are in SMTP opening state, send initial protocol.
966 	*/
967 
968 	if (clever && mci->mci_state != MCIS_CLOSED)
969 	{
970 		smtpinit(m, mci, e);
971 	}
972 
973 	return mci;
974 }
975 /*
976 **  GIVERESPONSE -- Interpret an error response from a mailer
977 **
978 **	Parameters:
979 **		stat -- the status code from the mailer (high byte
980 **			only; core dumps must have been taken care of
981 **			already).
982 **		m -- the mailer descriptor for this mailer.
983 **
984 **	Returns:
985 **		none.
986 **
987 **	Side Effects:
988 **		Errors may be incremented.
989 **		ExitStat may be set.
990 */
991 
992 giveresponse(stat, m, e)
993 	int stat;
994 	register MAILER *m;
995 	ENVELOPE *e;
996 {
997 	register char *statmsg;
998 	extern char *SysExMsg[];
999 	register int i;
1000 	extern int N_SysEx;
1001 #ifdef NAMED_BIND
1002 	extern int h_errno;
1003 #endif
1004 	char buf[MAXLINE];
1005 
1006 #ifdef lint
1007 	if (m == NULL)
1008 		return;
1009 #endif lint
1010 
1011 	/*
1012 	**  Compute status message from code.
1013 	*/
1014 
1015 	i = stat - EX__BASE;
1016 	if (stat == 0)
1017 		statmsg = "250 Sent";
1018 	else if (i < 0 || i > N_SysEx)
1019 	{
1020 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1021 		stat = EX_UNAVAILABLE;
1022 		statmsg = buf;
1023 	}
1024 	else if (stat == EX_TEMPFAIL)
1025 	{
1026 		(void) strcpy(buf, SysExMsg[i]);
1027 #ifdef NAMED_BIND
1028 		if (h_errno == TRY_AGAIN)
1029 		{
1030 			extern char *errstring();
1031 
1032 			statmsg = errstring(h_errno+MAX_ERRNO);
1033 		}
1034 		else
1035 #endif
1036 		{
1037 			if (errno != 0)
1038 			{
1039 				extern char *errstring();
1040 
1041 				statmsg = errstring(errno);
1042 			}
1043 			else
1044 			{
1045 #ifdef SMTP
1046 				extern char SmtpError[];
1047 
1048 				statmsg = SmtpError;
1049 #else SMTP
1050 				statmsg = NULL;
1051 #endif SMTP
1052 			}
1053 		}
1054 		if (statmsg != NULL && statmsg[0] != '\0')
1055 		{
1056 			(void) strcat(buf, ": ");
1057 			(void) strcat(buf, statmsg);
1058 		}
1059 		statmsg = buf;
1060 	}
1061 	else
1062 	{
1063 		statmsg = SysExMsg[i];
1064 	}
1065 
1066 	/*
1067 	**  Print the message as appropriate
1068 	*/
1069 
1070 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1071 		message(Arpa_Info, &statmsg[4]);
1072 	else
1073 	{
1074 		Errors++;
1075 		usrerr(statmsg);
1076 	}
1077 
1078 	/*
1079 	**  Final cleanup.
1080 	**	Log a record of the transaction.  Compute the new
1081 	**	ExitStat -- if we already had an error, stick with
1082 	**	that.
1083 	*/
1084 
1085 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
1086 		logdelivery(&statmsg[4], e);
1087 
1088 	if (stat != EX_TEMPFAIL)
1089 		setstat(stat);
1090 	if (stat != EX_OK)
1091 	{
1092 		if (e->e_message != NULL)
1093 			free(e->e_message);
1094 		e->e_message = newstr(&statmsg[4]);
1095 	}
1096 	errno = 0;
1097 #ifdef NAMED_BIND
1098 	h_errno = 0;
1099 #endif
1100 }
1101 /*
1102 **  LOGDELIVERY -- log the delivery in the system log
1103 **
1104 **	Parameters:
1105 **		stat -- the message to print for the status
1106 **
1107 **	Returns:
1108 **		none
1109 **
1110 **	Side Effects:
1111 **		none
1112 */
1113 
1114 logdelivery(stat, e)
1115 	char *stat;
1116 	register ENVELOPE *e;
1117 {
1118 	extern char *pintvl();
1119 
1120 # ifdef LOG
1121 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", e->e_id,
1122 	       e->e_to, pintvl(curtime() - e->e_ctime, TRUE), stat);
1123 # endif LOG
1124 }
1125 /*
1126 **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
1127 **
1128 **	This can be made an arbitrary message separator by changing $l
1129 **
1130 **	One of the ugliest hacks seen by human eyes is contained herein:
1131 **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
1132 **	does a well-meaning programmer such as myself have to deal with
1133 **	this kind of antique garbage????
1134 **
1135 **	Parameters:
1136 **		fp -- the file to output to.
1137 **		m -- the mailer describing this entry.
1138 **
1139 **	Returns:
1140 **		none
1141 **
1142 **	Side Effects:
1143 **		outputs some text to fp.
1144 */
1145 
1146 putfromline(fp, m, e)
1147 	register FILE *fp;
1148 	register MAILER *m;
1149 	ENVELOPE *e;
1150 {
1151 	char *template = "\001l\n";
1152 	char buf[MAXLINE];
1153 
1154 	if (bitnset(M_NHDR, m->m_flags))
1155 		return;
1156 
1157 # ifdef UGLYUUCP
1158 	if (bitnset(M_UGLYUUCP, m->m_flags))
1159 	{
1160 		char *bang;
1161 		char xbuf[MAXLINE];
1162 
1163 		expand("\001<", buf, &buf[sizeof buf - 1], e);
1164 		bang = index(buf, '!');
1165 		if (bang == NULL)
1166 			syserr("No ! in UUCP! (%s)", buf);
1167 		else
1168 		{
1169 			*bang++ = '\0';
1170 			(void) sprintf(xbuf, "From %s  \001d remote from %s\n", bang, buf);
1171 			template = xbuf;
1172 		}
1173 	}
1174 # endif UGLYUUCP
1175 	expand(template, buf, &buf[sizeof buf - 1], e);
1176 	putline(buf, fp, m);
1177 }
1178 /*
1179 **  PUTBODY -- put the body of a message.
1180 **
1181 **	Parameters:
1182 **		fp -- file to output onto.
1183 **		m -- a mailer descriptor to control output format.
1184 **		e -- the envelope to put out.
1185 **
1186 **	Returns:
1187 **		none.
1188 **
1189 **	Side Effects:
1190 **		The message is written onto fp.
1191 */
1192 
1193 putbody(fp, m, e)
1194 	FILE *fp;
1195 	MAILER *m;
1196 	register ENVELOPE *e;
1197 {
1198 	char buf[MAXLINE];
1199 
1200 	/*
1201 	**  Output the body of the message
1202 	*/
1203 
1204 	if (e->e_dfp == NULL)
1205 	{
1206 		if (e->e_df != NULL)
1207 		{
1208 			e->e_dfp = fopen(e->e_df, "r");
1209 			if (e->e_dfp == NULL)
1210 				syserr("putbody: Cannot open %s for %s from %s",
1211 				e->e_df, e->e_to, e->e_from);
1212 		}
1213 		else
1214 			putline("<<< No Message Collected >>>", fp, m);
1215 	}
1216 	if (e->e_dfp != NULL)
1217 	{
1218 		rewind(e->e_dfp);
1219 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
1220 		{
1221 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1222 			    strncmp(buf, "From ", 5) == 0)
1223 				(void) putc('>', fp);
1224 			putline(buf, fp, m);
1225 		}
1226 
1227 		if (ferror(e->e_dfp))
1228 		{
1229 			syserr("putbody: read error");
1230 			ExitStat = EX_IOERR;
1231 		}
1232 	}
1233 
1234 	(void) fflush(fp);
1235 	if (ferror(fp) && errno != EPIPE)
1236 	{
1237 		syserr("putbody: write error");
1238 		ExitStat = EX_IOERR;
1239 	}
1240 	errno = 0;
1241 }
1242 /*
1243 **  MAILFILE -- Send a message to a file.
1244 **
1245 **	If the file has the setuid/setgid bits set, but NO execute
1246 **	bits, sendmail will try to become the owner of that file
1247 **	rather than the real user.  Obviously, this only works if
1248 **	sendmail runs as root.
1249 **
1250 **	This could be done as a subordinate mailer, except that it
1251 **	is used implicitly to save messages in ~/dead.letter.  We
1252 **	view this as being sufficiently important as to include it
1253 **	here.  For example, if the system is dying, we shouldn't have
1254 **	to create another process plus some pipes to save the message.
1255 **
1256 **	Parameters:
1257 **		filename -- the name of the file to send to.
1258 **		ctladdr -- the controlling address header -- includes
1259 **			the userid/groupid to be when sending.
1260 **
1261 **	Returns:
1262 **		The exit code associated with the operation.
1263 **
1264 **	Side Effects:
1265 **		none.
1266 */
1267 
1268 mailfile(filename, ctladdr, e)
1269 	char *filename;
1270 	ADDRESS *ctladdr;
1271 	register ENVELOPE *e;
1272 {
1273 	register FILE *f;
1274 	register int pid;
1275 	int mode;
1276 
1277 	/*
1278 	**  Fork so we can change permissions here.
1279 	**	Note that we MUST use fork, not vfork, because of
1280 	**	the complications of calling subroutines, etc.
1281 	*/
1282 
1283 	DOFORK(fork);
1284 
1285 	if (pid < 0)
1286 		return (EX_OSERR);
1287 	else if (pid == 0)
1288 	{
1289 		/* child -- actually write to file */
1290 		struct stat stb;
1291 
1292 		(void) signal(SIGINT, SIG_DFL);
1293 		(void) signal(SIGHUP, SIG_DFL);
1294 		(void) signal(SIGTERM, SIG_DFL);
1295 		(void) umask(OldUmask);
1296 
1297 		if (stat(filename, &stb) < 0)
1298 			stb.st_mode = 0666;
1299 		mode = stb.st_mode;
1300 
1301 		/* limit the errors to those actually caused in the child */
1302 		errno = 0;
1303 		ExitStat = EX_OK;
1304 
1305 		if (bitset(0111, stb.st_mode))
1306 			exit(EX_CANTCREAT);
1307 		if (ctladdr == NULL)
1308 			ctladdr = &e->e_from;
1309 		else
1310 		{
1311 			/* ignore setuid and setgid bits */
1312 			mode &= ~(S_ISGID|S_ISUID);
1313 		}
1314 
1315 		/* we have to open the dfile BEFORE setuid */
1316 		if (e->e_dfp == NULL && e->e_df != NULL)
1317 		{
1318 			e->e_dfp = fopen(e->e_df, "r");
1319 			if (e->e_dfp == NULL)
1320 			{
1321 				syserr("mailfile: Cannot open %s for %s from %s",
1322 					e->e_df, e->e_to, e->e_from);
1323 			}
1324 		}
1325 
1326 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1327 		{
1328 			if (ctladdr->q_uid == 0)
1329 			{
1330 				(void) setgid(DefGid);
1331 				(void) initgroups(DefUser, DefGid);
1332 			}
1333 			else
1334 			{
1335 				(void) setgid(ctladdr->q_gid);
1336 				(void) initgroups(ctladdr->q_ruser ?
1337 					ctladdr->q_ruser : ctladdr->q_user,
1338 					ctladdr->q_gid);
1339 			}
1340 		}
1341 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1342 		{
1343 			if (ctladdr->q_uid == 0)
1344 				(void) setuid(DefUid);
1345 			else
1346 				(void) setuid(ctladdr->q_uid);
1347 		}
1348 		FileName = filename;
1349 		LineNumber = 0;
1350 		f = dfopen(filename, "a");
1351 		if (f == NULL)
1352 		{
1353 			message("cannot open");
1354 			exit(EX_CANTCREAT);
1355 		}
1356 
1357 		putfromline(f, ProgMailer, e);
1358 		(*e->e_puthdr)(f, ProgMailer, e);
1359 		putline("\n", f, ProgMailer);
1360 		(*e->e_putbody)(f, ProgMailer, e);
1361 		putline("\n", f, ProgMailer);
1362 		if (ferror(f))
1363 		{
1364 			message("I/O error");
1365 			setstat(EX_IOERR);
1366 		}
1367 		(void) fclose(f);
1368 		(void) fflush(stdout);
1369 
1370 		/* reset ISUID & ISGID bits for paranoid systems */
1371 		(void) chmod(filename, (int) stb.st_mode);
1372 		exit(ExitStat);
1373 		/*NOTREACHED*/
1374 	}
1375 	else
1376 	{
1377 		/* parent -- wait for exit status */
1378 		int st;
1379 
1380 		st = waitfor(pid);
1381 		if ((st & 0377) != 0)
1382 			return (EX_UNAVAILABLE);
1383 		else
1384 			return ((st >> 8) & 0377);
1385 		/*NOTREACHED*/
1386 	}
1387 }
1388 /*
1389 **  SENDALL -- actually send all the messages.
1390 **
1391 **	Parameters:
1392 **		e -- the envelope to send.
1393 **		mode -- the delivery mode to use.  If SM_DEFAULT, use
1394 **			the current SendMode.
1395 **
1396 **	Returns:
1397 **		none.
1398 **
1399 **	Side Effects:
1400 **		Scans the send lists and sends everything it finds.
1401 **		Delivers any appropriate error messages.
1402 **		If we are running in a non-interactive mode, takes the
1403 **			appropriate action.
1404 */
1405 
1406 sendall(e, mode)
1407 	ENVELOPE *e;
1408 	char mode;
1409 {
1410 	register ADDRESS *q;
1411 	bool oldverbose;
1412 	int pid;
1413 	int nsent;
1414 # ifdef LOCKF
1415 	struct flock lfd;
1416 # endif
1417 
1418 	/* determine actual delivery mode */
1419 	if (mode == SM_DEFAULT)
1420 	{
1421 		extern bool shouldqueue();
1422 
1423 		if (shouldqueue(e->e_msgpriority))
1424 			mode = SM_QUEUE;
1425 		else
1426 			mode = SendMode;
1427 	}
1428 
1429 	if (tTd(13, 1))
1430 	{
1431 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
1432 		printaddr(e->e_sendqueue, TRUE);
1433 	}
1434 
1435 	/*
1436 	**  Do any preprocessing necessary for the mode we are running.
1437 	**	Check to make sure the hop count is reasonable.
1438 	**	Delete sends to the sender in mailing lists.
1439 	*/
1440 
1441 	CurEnv = e;
1442 
1443 	if (e->e_hopcount > MaxHopCount)
1444 	{
1445 		errno = 0;
1446 		syserr("sendall: too many hops %d (%d max): from %s, to %s",
1447 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr, e->e_to);
1448 		return;
1449 	}
1450 
1451 	if (!MeToo)
1452 	{
1453 		extern ADDRESS *recipient();
1454 
1455 		e->e_from.q_flags |= QDONTSEND;
1456 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1457 	}
1458 
1459 # ifdef QUEUE
1460 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1461 	     (mode != SM_VERIFY && SuperSafe)) &&
1462 	    !bitset(EF_INQUEUE, e->e_flags))
1463 		queueup(e, TRUE, mode == SM_QUEUE);
1464 #endif QUEUE
1465 
1466 	oldverbose = Verbose;
1467 	switch (mode)
1468 	{
1469 	  case SM_VERIFY:
1470 		Verbose = TRUE;
1471 		break;
1472 
1473 	  case SM_QUEUE:
1474   queueonly:
1475 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1476 		return;
1477 
1478 	  case SM_FORK:
1479 		if (e->e_xfp != NULL)
1480 			(void) fflush(e->e_xfp);
1481 
1482 # ifdef LOCKF
1483 		/*
1484 		**  Since lockf has the interesting semantic that the
1485 		**  lock is lost when we fork, we have to risk losing
1486 		**  the lock here by closing before the fork, and then
1487 		**  trying to get it back in the child.
1488 		*/
1489 
1490 		if (e->e_lockfp != NULL)
1491 		{
1492 			(void) fclose(e->e_lockfp);
1493 			e->e_lockfp = NULL;
1494 		}
1495 # endif /* LOCKF */
1496 
1497 		pid = fork();
1498 		if (pid < 0)
1499 		{
1500 			goto queueonly;
1501 		}
1502 		else if (pid > 0)
1503 		{
1504 			/* be sure we leave the temp files to our child */
1505 			e->e_id = e->e_df = NULL;
1506 # ifndef LOCKF
1507 			if (e->e_lockfp != NULL)
1508 				(void) fclose(e->e_lockfp);
1509 # endif
1510 			return;
1511 		}
1512 
1513 		/* double fork to avoid zombies */
1514 		if (fork() > 0)
1515 			exit(EX_OK);
1516 
1517 		/* be sure we are immune from the terminal */
1518 		disconnect(FALSE);
1519 
1520 # ifdef LOCKF
1521 		/*
1522 		**  Now try to get our lock back.
1523 		*/
1524 
1525 		lfd.l_type = F_WRLCK;
1526 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1527 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
1528 		if (e->e_lockfp == NULL ||
1529 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
1530 		{
1531 			/* oops....  lost it */
1532 # ifdef LOG
1533 			if (LogLevel > 5)
1534 				syslog(LOG_NOTICE, "%s: lost lock: %m",
1535 					e->e_id);
1536 # endif /* LOG */
1537 			exit(EX_OK);
1538 		}
1539 # endif /* LOCKF */
1540 
1541 		break;
1542 	}
1543 
1544 	/*
1545 	**  Run through the list and send everything.
1546 	*/
1547 
1548 	nsent = 0;
1549 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1550 	{
1551 		if (mode == SM_VERIFY)
1552 		{
1553 			e->e_to = q->q_paddr;
1554 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1555 				message(Arpa_Info, "deliverable");
1556 		}
1557 		else if (!bitset(QDONTSEND, q->q_flags))
1558 		{
1559 # ifdef QUEUE
1560 			/*
1561 			**  Checkpoint the send list every few addresses
1562 			*/
1563 
1564 			if (nsent >= CheckpointInterval)
1565 			{
1566 				queueup(e, TRUE, FALSE);
1567 				nsent = 0;
1568 			}
1569 # endif /* QUEUE */
1570 			if (deliver(e, q) == EX_OK)
1571 				nsent++;
1572 		}
1573 	}
1574 	Verbose = oldverbose;
1575 
1576 	/*
1577 	**  Now run through and check for errors.
1578 	*/
1579 
1580 	if (mode == SM_VERIFY)
1581 		return;
1582 
1583 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1584 	{
1585 		register ADDRESS *qq;
1586 
1587 		if (tTd(13, 3))
1588 		{
1589 			printf("Checking ");
1590 			printaddr(q, FALSE);
1591 		}
1592 
1593 		/* only send errors if the message failed */
1594 		if (!bitset(QBADADDR, q->q_flags))
1595 			continue;
1596 
1597 		/* we have an address that failed -- find the parent */
1598 		for (qq = q; qq != NULL; qq = qq->q_alias)
1599 		{
1600 			char obuf[MAXNAME + 6];
1601 			extern char *aliaslookup();
1602 
1603 			/* we can only have owners for local addresses */
1604 			if (!bitnset(M_LOCAL, qq->q_mailer->m_flags))
1605 				continue;
1606 
1607 			/* see if the owner list exists */
1608 			(void) strcpy(obuf, "owner-");
1609 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1610 				(void) strcat(obuf, "owner");
1611 			else
1612 				(void) strcat(obuf, qq->q_user);
1613 			makelower(obuf);
1614 			if (aliaslookup(obuf) == NULL)
1615 				continue;
1616 
1617 			if (tTd(13, 4))
1618 				printf("Errors to %s\n", obuf);
1619 
1620 			/* owner list exists -- add it to the error queue */
1621 			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue, e);
1622 			ErrorMode = EM_MAIL;
1623 			break;
1624 		}
1625 
1626 		/* if we did not find an owner, send to the sender */
1627 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1628 			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue, e);
1629 	}
1630 
1631 	if (mode == SM_FORK)
1632 		finis();
1633 }
1634