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.61 (Berkeley) 07/12/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 		if (rcode == EX_OK)
394 		{
395 			/* shouldn't happen */
396 			rcode = EX_SOFTWARE;
397 		}
398 	}
399 	else if (!clever)
400 	{
401 		/*
402 		**  Format and send message.
403 		*/
404 
405 		putfromline(mci->mci_out, m, e);
406 		(*e->e_puthdr)(mci->mci_out, m, e);
407 		putline("\n", mci->mci_out, m);
408 		(*e->e_putbody)(mci->mci_out, m, e);
409 
410 		/* get the exit status */
411 		rcode = endmailer(mci, pv[0]);
412 	}
413 	else
414 #ifdef SMTP
415 	{
416 		/*
417 		**  Send the MAIL FROM: protocol
418 		*/
419 
420 		rcode = smtpmailfrom(m, mci, e);
421 		if (rcode == EX_OK)
422 		{
423 			register char *t = tobuf;
424 			register int i;
425 
426 			/* send the recipient list */
427 			tobuf[0] = '\0';
428 			for (to = tochain; to != NULL; to = to->q_tchain)
429 			{
430 				e->e_to = to->q_paddr;
431 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
432 				{
433 					markfailure(e, to, i);
434 					giveresponse(i, m, e);
435 				}
436 				else
437 				{
438 					*t++ = ',';
439 					for (p = to->q_paddr; *p; *t++ = *p++)
440 						continue;
441 				}
442 			}
443 
444 			/* now send the data */
445 			if (tobuf[0] == '\0')
446 			{
447 				e->e_to = NULL;
448 				if (bitset(MCIF_CACHED, mci->mci_flags))
449 					smtprset(m, mci, e);
450 			}
451 			else
452 			{
453 				e->e_to = tobuf + 1;
454 				rcode = smtpdata(m, mci, e);
455 			}
456 
457 			/* now close the connection */
458 			if (!bitset(MCIF_CACHED, mci->mci_flags))
459 				smtpquit(m, mci, e);
460 		}
461 	}
462 #else /* not SMTP */
463 	{
464 		syserr("deliver: need SMTP compiled to use clever mailer");
465 		rcode = -1;
466 		goto give_up;
467 	}
468 #endif /* SMTP */
469 #ifdef NAMED_BIND
470 	if (ConfigLevel < 2)
471 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
472 #endif
473 
474 	/* arrange a return receipt if requested */
475 	if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags))
476 	{
477 		e->e_flags |= EF_SENDRECEIPT;
478 		/* do we want to send back more info? */
479 	}
480 
481 	/*
482 	**  Do final status disposal.
483 	**	We check for something in tobuf for the SMTP case.
484 	**	If we got a temporary failure, arrange to queue the
485 	**		addressees.
486 	*/
487 
488   give_up:
489 	if (tobuf[0] != '\0')
490 		giveresponse(rcode, m, e);
491 	for (to = tochain; to != NULL; to = to->q_tchain)
492 	{
493 		if (rcode != EX_OK)
494 			markfailure(e, to, rcode);
495 		else
496 			to->q_flags |= QSENT;
497 	}
498 
499 	/*
500 	**  Restore state and return.
501 	*/
502 
503 	errno = 0;
504 	define('g', (char *) NULL, e);
505 	define('<', (char *) NULL, e);
506 	return (rcode);
507 }
508 /*
509 **  MARKFAILURE -- mark a failure on a specific address.
510 **
511 **	Parameters:
512 **		e -- the envelope we are sending.
513 **		q -- the address to mark.
514 **		rcode -- the code signifying the particular failure.
515 **
516 **	Returns:
517 **		none.
518 **
519 **	Side Effects:
520 **		marks the address (and possibly the envelope) with the
521 **			failure so that an error will be returned or
522 **			the message will be queued, as appropriate.
523 */
524 
525 markfailure(e, q, rcode)
526 	register ENVELOPE *e;
527 	register ADDRESS *q;
528 	int rcode;
529 {
530 	if (rcode == EX_OK)
531 		return;
532 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
533 		q->q_flags |= QBADADDR;
534 	else if (curtime() > e->e_ctime + TimeOut)
535 	{
536 		extern char *pintvl();
537 		char buf[MAXLINE];
538 
539 		if (!bitset(EF_TIMEOUT, e->e_flags))
540 		{
541 			(void) sprintf(buf, "Cannot send message for %s",
542 				pintvl(TimeOut, FALSE));
543 			if (e->e_message != NULL)
544 				free(e->e_message);
545 			e->e_message = newstr(buf);
546 			message(Arpa_Info, buf);
547 		}
548 		q->q_flags |= QBADADDR;
549 		e->e_flags |= EF_TIMEOUT;
550 	}
551 	else
552 		q->q_flags |= QQUEUEUP;
553 }
554 /*
555 **  DOFORK -- do a fork, retrying a couple of times on failure.
556 **
557 **	This MUST be a macro, since after a vfork we are running
558 **	two processes on the same stack!!!
559 **
560 **	Parameters:
561 **		none.
562 **
563 **	Returns:
564 **		From a macro???  You've got to be kidding!
565 **
566 **	Side Effects:
567 **		Modifies the ==> LOCAL <== variable 'pid', leaving:
568 **			pid of child in parent, zero in child.
569 **			-1 on unrecoverable error.
570 **
571 **	Notes:
572 **		I'm awfully sorry this looks so awful.  That's
573 **		vfork for you.....
574 */
575 
576 # define NFORKTRIES	5
577 
578 # ifndef FORK
579 # define FORK	fork
580 # endif
581 
582 # define DOFORK(fORKfN) \
583 {\
584 	register int i;\
585 \
586 	for (i = NFORKTRIES; --i >= 0; )\
587 	{\
588 		pid = fORKfN();\
589 		if (pid >= 0)\
590 			break;\
591 		if (i > 0)\
592 			sleep((unsigned) NFORKTRIES - i);\
593 	}\
594 }
595 /*
596 **  DOFORK -- simple fork interface to DOFORK.
597 **
598 **	Parameters:
599 **		none.
600 **
601 **	Returns:
602 **		pid of child in parent.
603 **		zero in child.
604 **		-1 on error.
605 **
606 **	Side Effects:
607 **		returns twice, once in parent and once in child.
608 */
609 
610 dofork()
611 {
612 	register int pid;
613 
614 	DOFORK(fork);
615 	return (pid);
616 }
617 /*
618 **  ENDMAILER -- Wait for mailer to terminate.
619 **
620 **	We should never get fatal errors (e.g., segmentation
621 **	violation), so we report those specially.  For other
622 **	errors, we choose a status message (into statmsg),
623 **	and if it represents an error, we print it.
624 **
625 **	Parameters:
626 **		pid -- pid of mailer.
627 **		name -- name of mailer (for error messages).
628 **
629 **	Returns:
630 **		exit code of mailer.
631 **
632 **	Side Effects:
633 **		none.
634 */
635 
636 endmailer(mci, name)
637 	register MCI *mci;
638 	char *name;
639 {
640 	int st;
641 
642 	/* close any connections */
643 	if (mci->mci_in != NULL)
644 		(void) fclose(mci->mci_in);
645 	if (mci->mci_out != NULL)
646 		(void) fclose(mci->mci_out);
647 	mci->mci_in = mci->mci_out = NULL;
648 	mci->mci_state = MCIS_CLOSED;
649 
650 	/* in the IPC case there is nothing to wait for */
651 	if (mci->mci_pid == 0)
652 		return (EX_OK);
653 
654 	/* wait for the mailer process to die and collect status */
655 	st = waitfor(mci->mci_pid);
656 	if (st == -1)
657 	{
658 		syserr("endmailer %s: wait", name);
659 		return (EX_SOFTWARE);
660 	}
661 
662 	/* see if it died a horrid death */
663 	if ((st & 0377) != 0)
664 	{
665 		syserr("mailer %s died with signal %o", name, st);
666 		ExitStat = EX_TEMPFAIL;
667 		return (EX_TEMPFAIL);
668 	}
669 
670 	/* normal death -- return status */
671 	st = (st >> 8) & 0377;
672 	return (st);
673 }
674 /*
675 **  OPENMAILER -- open connection to mailer.
676 **
677 **	Parameters:
678 **		m -- mailer descriptor.
679 **		pvp -- parameter vector to pass to mailer.
680 **		ctladdr -- controlling address for user.
681 **		clever -- create a full duplex connection.
682 **
683 **	Returns:
684 **		The mail connection info struct for this connection.
685 **		NULL on failure.
686 **
687 **	Side Effects:
688 **		creates a mailer in a subprocess.
689 */
690 
691 MCI *
692 openmailer(m, pvp, ctladdr, clever, e)
693 	MAILER *m;
694 	char **pvp;
695 	ADDRESS *ctladdr;
696 	bool clever;
697 	ENVELOPE *e;
698 {
699 	int pid;
700 	register MCI *mci;
701 	int mpvect[2];
702 	int rpvect[2];
703 	extern FILE *fdopen();
704 
705 	if (tTd(11, 1))
706 	{
707 		printf("openmailer:");
708 		printav(pvp);
709 	}
710 	errno = 0;
711 
712 	CurHostName = m->m_mailer;
713 
714 	/*
715 	**  Deal with the special case of mail handled through an IPC
716 	**  connection.
717 	**	In this case we don't actually fork.  We must be
718 	**	running SMTP for this to work.  We will return a
719 	**	zero pid to indicate that we are running IPC.
720 	**  We also handle a debug version that just talks to stdin/out.
721 	*/
722 
723 	/* check for Local Person Communication -- not for mortals!!! */
724 	if (strcmp(m->m_mailer, "[LPC]") == 0)
725 	{
726 		mci = (MCI *) xalloc(sizeof *mci);
727 		bzero((char *) mci, sizeof *mci);
728 		mci->mci_in = stdin;
729 		mci->mci_out = stdout;
730 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
731 		mci->mci_mailer = m;
732 	}
733 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
734 		 strcmp(m->m_mailer, "[TCP]") == 0)
735 	{
736 #ifdef DAEMON
737 		register int i, j;
738 		register u_short port;
739 		int nmx;
740 		char *mxhosts[MAXMXHOSTS + 1];
741 		extern MCI *mci_get();
742 
743 		CurHostName = pvp[1];
744 #ifdef NAMED_BIND
745 		if (CurHostName != NULL && CurHostName[0] != '\0' &&
746 		    CurHostName[0] != '[')
747 		{
748 			int rcode;
749 			char buf[MAXNAME];
750 
751 			expand("\001j", buf, &buf[sizeof(buf) - 1], e);
752 			nmx = getmxrr(CurHostName, mxhosts, buf, &rcode);
753 			if (nmx < 0)
754 			{
755 				mci = mci_get(CurHostName, m);
756 				mci->mci_exitstat = rcode;
757 				mci->mci_errno = errno;
758 			}
759 		}
760 		else
761 #endif
762 		{
763 			nmx = 1;
764 			mxhosts[0] = CurHostName;
765 		}
766 
767 		if (!clever)
768 			syserr("non-clever IPC");
769 		if (pvp[2] != NULL)
770 			port = atoi(pvp[2]);
771 		else
772 			port = 0;
773 		for (j = 0; j < nmx; j++)
774 		{
775 			/* see if we already know that this host is fried */
776 			CurHostName = mxhosts[j];
777 			mci = mci_get(CurHostName, m);
778 			if (mci->mci_state != MCIS_CLOSED)
779 				return mci;
780 			mci->mci_mailer = m;
781 			if (mci->mci_exitstat != EX_OK)
782 				continue;
783 
784 			/* try the connection */
785 			setproctitle("%s %s: %s", e->e_id, mxhosts[1], "user open");
786 			message(Arpa_Info, "Connecting to %s (%s)...",
787 				mxhosts[j], m->m_name);
788 			i = makeconnection(mxhosts[j], port, mci,
789 				bitnset(M_SECURE_PORT, m->m_flags));
790 			mci->mci_exitstat = i;
791 			mci->mci_errno = errno;
792 			if (i == EX_OK)
793 			{
794 				mci->mci_state = MCIS_OPENING;
795 				mci_cache(mci);
796 				break;
797 			}
798 			else if (tTd(11, 1))
799 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
800 					i, errno);
801 
802 
803 			/* enter status of this host */
804 			setstat(i);
805 		}
806 		mci->mci_pid = 0;
807 #else /* no DAEMON */
808 		syserr("openmailer: no IPC");
809 		return NULL;
810 #endif /* DAEMON */
811 	}
812 	else
813 	{
814 		/* create a pipe to shove the mail through */
815 		if (pipe(mpvect) < 0)
816 		{
817 			syserr("openmailer: pipe (to mailer)");
818 			return NULL;
819 		}
820 
821 		/* if this mailer speaks smtp, create a return pipe */
822 		if (clever && pipe(rpvect) < 0)
823 		{
824 			syserr("openmailer: pipe (from mailer)");
825 			(void) close(mpvect[0]);
826 			(void) close(mpvect[1]);
827 			return NULL;
828 		}
829 
830 		/*
831 		**  Actually fork the mailer process.
832 		**	DOFORK is clever about retrying.
833 		**
834 		**	Dispose of SIGCHLD signal catchers that may be laying
835 		**	around so that endmail will get it.
836 		*/
837 
838 		if (e->e_xfp != NULL)
839 			(void) fflush(e->e_xfp);		/* for debugging */
840 		(void) fflush(stdout);
841 # ifdef SIGCHLD
842 		(void) signal(SIGCHLD, SIG_DFL);
843 # endif SIGCHLD
844 		DOFORK(FORK);
845 		/* pid is set by DOFORK */
846 		if (pid < 0)
847 		{
848 			/* failure */
849 			syserr("openmailer: cannot fork");
850 			(void) close(mpvect[0]);
851 			(void) close(mpvect[1]);
852 			if (clever)
853 			{
854 				(void) close(rpvect[0]);
855 				(void) close(rpvect[1]);
856 			}
857 			return NULL;
858 		}
859 		else if (pid == 0)
860 		{
861 			int i;
862 			extern int DtableSize;
863 
864 			/* child -- set up input & exec mailer */
865 			/* make diagnostic output be standard output */
866 			(void) signal(SIGINT, SIG_IGN);
867 			(void) signal(SIGHUP, SIG_IGN);
868 			(void) signal(SIGTERM, SIG_DFL);
869 
870 			/* arrange to filter std & diag output of command */
871 			if (clever)
872 			{
873 				(void) close(rpvect[0]);
874 				(void) close(1);
875 				(void) dup(rpvect[1]);
876 				(void) close(rpvect[1]);
877 			}
878 			else if (OpMode == MD_SMTP || HoldErrs)
879 			{
880 				/* put mailer output in transcript */
881 				(void) close(1);
882 				(void) dup(fileno(e->e_xfp));
883 			}
884 			(void) close(2);
885 			(void) dup(1);
886 
887 			/* arrange to get standard input */
888 			(void) close(mpvect[1]);
889 			(void) close(0);
890 			if (dup(mpvect[0]) < 0)
891 			{
892 				syserr("Cannot dup to zero!");
893 				_exit(EX_OSERR);
894 			}
895 			(void) close(mpvect[0]);
896 			if (!bitnset(M_RESTR, m->m_flags))
897 			{
898 				if (ctladdr == NULL || ctladdr->q_uid == 0)
899 				{
900 					(void) setgid(DefGid);
901 					(void) initgroups(DefUser, DefGid);
902 					(void) setuid(DefUid);
903 				}
904 				else
905 				{
906 					(void) setgid(ctladdr->q_gid);
907 					(void) initgroups(ctladdr->q_ruser?
908 						ctladdr->q_ruser: ctladdr->q_user,
909 						ctladdr->q_gid);
910 					(void) setuid(ctladdr->q_uid);
911 				}
912 			}
913 
914 			/* arrange for all the files to be closed */
915 			for (i = 3; i < DtableSize; i++)
916 			{
917 				register int j;
918 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
919 					(void)fcntl(i, F_SETFD, j|1);
920 			}
921 
922 			/* try to execute the mailer */
923 			execve(m->m_mailer, pvp, UserEnviron);
924 			syserr("Cannot exec %s", m->m_mailer);
925 			if (m == LocalMailer)
926 				_exit(EX_TEMPFAIL);
927 			switch (errno)
928 			{
929 			  case EIO:
930 			  case EAGAIN:
931 			  case ENOMEM:
932 # ifdef EPROCLIM
933 			  case EPROCLIM:
934 # endif
935 				_exit(EX_TEMPFAIL);
936 			}
937 			_exit(EX_UNAVAILABLE);
938 		}
939 
940 		/*
941 		**  Set up return value.
942 		*/
943 
944 		mci = (MCI *) xalloc(sizeof *mci);
945 		bzero((char *) mci, sizeof *mci);
946 		mci->mci_mailer = m;
947 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
948 		mci->mci_pid = pid;
949 		(void) close(mpvect[0]);
950 		mci->mci_out = fdopen(mpvect[1], "w");
951 		if (clever)
952 		{
953 			(void) close(rpvect[1]);
954 			mci->mci_in = fdopen(rpvect[0], "r");
955 		}
956 		else
957 		{
958 			mci->mci_flags |= MCIF_TEMP;
959 			mci->mci_in = NULL;
960 		}
961 	}
962 
963 	/*
964 	**  If we are in SMTP opening state, send initial protocol.
965 	*/
966 
967 	if (clever && mci->mci_state != MCIS_CLOSED)
968 	{
969 		smtpinit(m, mci, e);
970 	}
971 
972 	return mci;
973 }
974 /*
975 **  GIVERESPONSE -- Interpret an error response from a mailer
976 **
977 **	Parameters:
978 **		stat -- the status code from the mailer (high byte
979 **			only; core dumps must have been taken care of
980 **			already).
981 **		m -- the mailer descriptor for this mailer.
982 **
983 **	Returns:
984 **		none.
985 **
986 **	Side Effects:
987 **		Errors may be incremented.
988 **		ExitStat may be set.
989 */
990 
991 giveresponse(stat, m, e)
992 	int stat;
993 	register MAILER *m;
994 	ENVELOPE *e;
995 {
996 	register char *statmsg;
997 	extern char *SysExMsg[];
998 	register int i;
999 	extern int N_SysEx;
1000 #ifdef NAMED_BIND
1001 	extern int h_errno;
1002 #endif
1003 	char buf[MAXLINE];
1004 
1005 #ifdef lint
1006 	if (m == NULL)
1007 		return;
1008 #endif lint
1009 
1010 	/*
1011 	**  Compute status message from code.
1012 	*/
1013 
1014 	i = stat - EX__BASE;
1015 	if (stat == 0)
1016 		statmsg = "250 Sent";
1017 	else if (i < 0 || i > N_SysEx)
1018 	{
1019 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1020 		stat = EX_UNAVAILABLE;
1021 		statmsg = buf;
1022 	}
1023 	else if (stat == EX_TEMPFAIL)
1024 	{
1025 		(void) strcpy(buf, SysExMsg[i]);
1026 #ifdef NAMED_BIND
1027 		if (h_errno == TRY_AGAIN)
1028 		{
1029 			extern char *errstring();
1030 
1031 			statmsg = errstring(h_errno+MAX_ERRNO);
1032 		}
1033 		else
1034 #endif
1035 		{
1036 			if (errno != 0)
1037 			{
1038 				extern char *errstring();
1039 
1040 				statmsg = errstring(errno);
1041 			}
1042 			else
1043 			{
1044 #ifdef SMTP
1045 				extern char SmtpError[];
1046 
1047 				statmsg = SmtpError;
1048 #else SMTP
1049 				statmsg = NULL;
1050 #endif SMTP
1051 			}
1052 		}
1053 		if (statmsg != NULL && statmsg[0] != '\0')
1054 		{
1055 			(void) strcat(buf, ": ");
1056 			(void) strcat(buf, statmsg);
1057 		}
1058 		statmsg = buf;
1059 	}
1060 	else
1061 	{
1062 		statmsg = SysExMsg[i];
1063 	}
1064 
1065 	/*
1066 	**  Print the message as appropriate
1067 	*/
1068 
1069 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1070 		message(Arpa_Info, &statmsg[4]);
1071 	else
1072 	{
1073 		Errors++;
1074 		usrerr(statmsg);
1075 	}
1076 
1077 	/*
1078 	**  Final cleanup.
1079 	**	Log a record of the transaction.  Compute the new
1080 	**	ExitStat -- if we already had an error, stick with
1081 	**	that.
1082 	*/
1083 
1084 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
1085 		logdelivery(&statmsg[4], e);
1086 
1087 	if (stat != EX_TEMPFAIL)
1088 		setstat(stat);
1089 	if (stat != EX_OK)
1090 	{
1091 		if (e->e_message != NULL)
1092 			free(e->e_message);
1093 		e->e_message = newstr(&statmsg[4]);
1094 	}
1095 	errno = 0;
1096 #ifdef NAMED_BIND
1097 	h_errno = 0;
1098 #endif
1099 }
1100 /*
1101 **  LOGDELIVERY -- log the delivery in the system log
1102 **
1103 **	Parameters:
1104 **		stat -- the message to print for the status
1105 **
1106 **	Returns:
1107 **		none
1108 **
1109 **	Side Effects:
1110 **		none
1111 */
1112 
1113 logdelivery(stat, e)
1114 	char *stat;
1115 	register ENVELOPE *e;
1116 {
1117 	extern char *pintvl();
1118 
1119 # ifdef LOG
1120 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", e->e_id,
1121 	       e->e_to, pintvl(curtime() - e->e_ctime, TRUE), stat);
1122 # endif LOG
1123 }
1124 /*
1125 **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
1126 **
1127 **	This can be made an arbitrary message separator by changing $l
1128 **
1129 **	One of the ugliest hacks seen by human eyes is contained herein:
1130 **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
1131 **	does a well-meaning programmer such as myself have to deal with
1132 **	this kind of antique garbage????
1133 **
1134 **	Parameters:
1135 **		fp -- the file to output to.
1136 **		m -- the mailer describing this entry.
1137 **
1138 **	Returns:
1139 **		none
1140 **
1141 **	Side Effects:
1142 **		outputs some text to fp.
1143 */
1144 
1145 putfromline(fp, m, e)
1146 	register FILE *fp;
1147 	register MAILER *m;
1148 	ENVELOPE *e;
1149 {
1150 	char *template = "\001l\n";
1151 	char buf[MAXLINE];
1152 
1153 	if (bitnset(M_NHDR, m->m_flags))
1154 		return;
1155 
1156 # ifdef UGLYUUCP
1157 	if (bitnset(M_UGLYUUCP, m->m_flags))
1158 	{
1159 		char *bang;
1160 		char xbuf[MAXLINE];
1161 
1162 		expand("\001<", buf, &buf[sizeof buf - 1], e);
1163 		bang = index(buf, '!');
1164 		if (bang == NULL)
1165 			syserr("No ! in UUCP! (%s)", buf);
1166 		else
1167 		{
1168 			*bang++ = '\0';
1169 			(void) sprintf(xbuf, "From %s  \001d remote from %s\n", bang, buf);
1170 			template = xbuf;
1171 		}
1172 	}
1173 # endif UGLYUUCP
1174 	expand(template, buf, &buf[sizeof buf - 1], e);
1175 	putline(buf, fp, m);
1176 }
1177 /*
1178 **  PUTBODY -- put the body of a message.
1179 **
1180 **	Parameters:
1181 **		fp -- file to output onto.
1182 **		m -- a mailer descriptor to control output format.
1183 **		e -- the envelope to put out.
1184 **
1185 **	Returns:
1186 **		none.
1187 **
1188 **	Side Effects:
1189 **		The message is written onto fp.
1190 */
1191 
1192 putbody(fp, m, e)
1193 	FILE *fp;
1194 	MAILER *m;
1195 	register ENVELOPE *e;
1196 {
1197 	char buf[MAXLINE];
1198 
1199 	/*
1200 	**  Output the body of the message
1201 	*/
1202 
1203 	if (e->e_dfp == NULL)
1204 	{
1205 		if (e->e_df != NULL)
1206 		{
1207 			e->e_dfp = fopen(e->e_df, "r");
1208 			if (e->e_dfp == NULL)
1209 				syserr("putbody: Cannot open %s for %s from %s",
1210 				e->e_df, e->e_to, e->e_from);
1211 		}
1212 		else
1213 			putline("<<< No Message Collected >>>", fp, m);
1214 	}
1215 	if (e->e_dfp != NULL)
1216 	{
1217 		rewind(e->e_dfp);
1218 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
1219 		{
1220 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1221 			    strncmp(buf, "From ", 5) == 0)
1222 				(void) putc('>', fp);
1223 			putline(buf, fp, m);
1224 		}
1225 
1226 		if (ferror(e->e_dfp))
1227 		{
1228 			syserr("putbody: read error");
1229 			ExitStat = EX_IOERR;
1230 		}
1231 	}
1232 
1233 	(void) fflush(fp);
1234 	if (ferror(fp) && errno != EPIPE)
1235 	{
1236 		syserr("putbody: write error");
1237 		ExitStat = EX_IOERR;
1238 	}
1239 	errno = 0;
1240 }
1241 /*
1242 **  MAILFILE -- Send a message to a file.
1243 **
1244 **	If the file has the setuid/setgid bits set, but NO execute
1245 **	bits, sendmail will try to become the owner of that file
1246 **	rather than the real user.  Obviously, this only works if
1247 **	sendmail runs as root.
1248 **
1249 **	This could be done as a subordinate mailer, except that it
1250 **	is used implicitly to save messages in ~/dead.letter.  We
1251 **	view this as being sufficiently important as to include it
1252 **	here.  For example, if the system is dying, we shouldn't have
1253 **	to create another process plus some pipes to save the message.
1254 **
1255 **	Parameters:
1256 **		filename -- the name of the file to send to.
1257 **		ctladdr -- the controlling address header -- includes
1258 **			the userid/groupid to be when sending.
1259 **
1260 **	Returns:
1261 **		The exit code associated with the operation.
1262 **
1263 **	Side Effects:
1264 **		none.
1265 */
1266 
1267 mailfile(filename, ctladdr, e)
1268 	char *filename;
1269 	ADDRESS *ctladdr;
1270 	register ENVELOPE *e;
1271 {
1272 	register FILE *f;
1273 	register int pid;
1274 	int mode;
1275 
1276 	/*
1277 	**  Fork so we can change permissions here.
1278 	**	Note that we MUST use fork, not vfork, because of
1279 	**	the complications of calling subroutines, etc.
1280 	*/
1281 
1282 	DOFORK(fork);
1283 
1284 	if (pid < 0)
1285 		return (EX_OSERR);
1286 	else if (pid == 0)
1287 	{
1288 		/* child -- actually write to file */
1289 		struct stat stb;
1290 
1291 		(void) signal(SIGINT, SIG_DFL);
1292 		(void) signal(SIGHUP, SIG_DFL);
1293 		(void) signal(SIGTERM, SIG_DFL);
1294 		(void) umask(OldUmask);
1295 
1296 		if (stat(filename, &stb) < 0)
1297 			stb.st_mode = 0666;
1298 		mode = stb.st_mode;
1299 
1300 		/* limit the errors to those actually caused in the child */
1301 		errno = 0;
1302 		ExitStat = EX_OK;
1303 
1304 		if (bitset(0111, stb.st_mode))
1305 			exit(EX_CANTCREAT);
1306 		if (ctladdr == NULL)
1307 			ctladdr = &e->e_from;
1308 		else
1309 		{
1310 			/* ignore setuid and setgid bits */
1311 			mode &= ~(S_ISGID|S_ISUID);
1312 		}
1313 
1314 		/* we have to open the dfile BEFORE setuid */
1315 		if (e->e_dfp == NULL && e->e_df != NULL)
1316 		{
1317 			e->e_dfp = fopen(e->e_df, "r");
1318 			if (e->e_dfp == NULL)
1319 			{
1320 				syserr("mailfile: Cannot open %s for %s from %s",
1321 					e->e_df, e->e_to, e->e_from);
1322 			}
1323 		}
1324 
1325 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1326 		{
1327 			if (ctladdr->q_uid == 0)
1328 			{
1329 				(void) setgid(DefGid);
1330 				(void) initgroups(DefUser, DefGid);
1331 			}
1332 			else
1333 			{
1334 				(void) setgid(ctladdr->q_gid);
1335 				(void) initgroups(ctladdr->q_ruser ?
1336 					ctladdr->q_ruser : ctladdr->q_user,
1337 					ctladdr->q_gid);
1338 			}
1339 		}
1340 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1341 		{
1342 			if (ctladdr->q_uid == 0)
1343 				(void) setuid(DefUid);
1344 			else
1345 				(void) setuid(ctladdr->q_uid);
1346 		}
1347 		FileName = filename;
1348 		LineNumber = 0;
1349 		f = dfopen(filename, "a");
1350 		if (f == NULL)
1351 		{
1352 			message("cannot open");
1353 			exit(EX_CANTCREAT);
1354 		}
1355 
1356 		putfromline(f, ProgMailer, e);
1357 		(*e->e_puthdr)(f, ProgMailer, e);
1358 		putline("\n", f, ProgMailer);
1359 		(*e->e_putbody)(f, ProgMailer, e);
1360 		putline("\n", f, ProgMailer);
1361 		if (ferror(f))
1362 		{
1363 			message("I/O error");
1364 			setstat(EX_IOERR);
1365 		}
1366 		(void) fclose(f);
1367 		(void) fflush(stdout);
1368 
1369 		/* reset ISUID & ISGID bits for paranoid systems */
1370 		(void) chmod(filename, (int) stb.st_mode);
1371 		exit(ExitStat);
1372 		/*NOTREACHED*/
1373 	}
1374 	else
1375 	{
1376 		/* parent -- wait for exit status */
1377 		int st;
1378 
1379 		st = waitfor(pid);
1380 		if ((st & 0377) != 0)
1381 			return (EX_UNAVAILABLE);
1382 		else
1383 			return ((st >> 8) & 0377);
1384 		/*NOTREACHED*/
1385 	}
1386 }
1387 /*
1388 **  SENDALL -- actually send all the messages.
1389 **
1390 **	Parameters:
1391 **		e -- the envelope to send.
1392 **		mode -- the delivery mode to use.  If SM_DEFAULT, use
1393 **			the current SendMode.
1394 **
1395 **	Returns:
1396 **		none.
1397 **
1398 **	Side Effects:
1399 **		Scans the send lists and sends everything it finds.
1400 **		Delivers any appropriate error messages.
1401 **		If we are running in a non-interactive mode, takes the
1402 **			appropriate action.
1403 */
1404 
1405 sendall(e, mode)
1406 	ENVELOPE *e;
1407 	char mode;
1408 {
1409 	register ADDRESS *q;
1410 	bool oldverbose;
1411 	int pid;
1412 	int nsent;
1413 # ifdef LOCKF
1414 	struct flock lfd;
1415 # endif
1416 
1417 	/* determine actual delivery mode */
1418 	if (mode == SM_DEFAULT)
1419 	{
1420 		extern bool shouldqueue();
1421 
1422 		if (shouldqueue(e->e_msgpriority))
1423 			mode = SM_QUEUE;
1424 		else
1425 			mode = SendMode;
1426 	}
1427 
1428 	if (tTd(13, 1))
1429 	{
1430 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
1431 		printaddr(e->e_sendqueue, TRUE);
1432 	}
1433 
1434 	/*
1435 	**  Do any preprocessing necessary for the mode we are running.
1436 	**	Check to make sure the hop count is reasonable.
1437 	**	Delete sends to the sender in mailing lists.
1438 	*/
1439 
1440 	CurEnv = e;
1441 
1442 	if (e->e_hopcount > MaxHopCount)
1443 	{
1444 		errno = 0;
1445 		syserr("sendall: too many hops %d (%d max): from %s, to %s",
1446 			e->e_hopcount, MaxHopCount, e->e_from, e->e_to);
1447 		return;
1448 	}
1449 
1450 	if (!MeToo)
1451 	{
1452 		extern ADDRESS *recipient();
1453 
1454 		e->e_from.q_flags |= QDONTSEND;
1455 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1456 	}
1457 
1458 # ifdef QUEUE
1459 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1460 	     (mode != SM_VERIFY && SuperSafe)) &&
1461 	    !bitset(EF_INQUEUE, e->e_flags))
1462 		queueup(e, TRUE, mode == SM_QUEUE);
1463 #endif QUEUE
1464 
1465 	oldverbose = Verbose;
1466 	switch (mode)
1467 	{
1468 	  case SM_VERIFY:
1469 		Verbose = TRUE;
1470 		break;
1471 
1472 	  case SM_QUEUE:
1473   queueonly:
1474 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1475 		return;
1476 
1477 	  case SM_FORK:
1478 		if (e->e_xfp != NULL)
1479 			(void) fflush(e->e_xfp);
1480 
1481 # ifdef LOCKF
1482 		/*
1483 		**  Since lockf has the interesting semantic that the
1484 		**  lock is lost when we fork, we have to risk losing
1485 		**  the lock here by closing before the fork, and then
1486 		**  trying to get it back in the child.
1487 		*/
1488 
1489 		if (e->e_lockfp != NULL)
1490 		{
1491 			(void) fclose(e->e_lockfp);
1492 			e->e_lockfp = NULL;
1493 		}
1494 # endif /* LOCKF */
1495 
1496 		pid = fork();
1497 		if (pid < 0)
1498 		{
1499 			goto queueonly;
1500 		}
1501 		else if (pid > 0)
1502 		{
1503 			/* be sure we leave the temp files to our child */
1504 			e->e_id = e->e_df = NULL;
1505 # ifndef LOCKF
1506 			if (e->e_lockfp != NULL)
1507 				(void) fclose(e->e_lockfp);
1508 # endif
1509 			return;
1510 		}
1511 
1512 		/* double fork to avoid zombies */
1513 		if (fork() > 0)
1514 			exit(EX_OK);
1515 
1516 		/* be sure we are immune from the terminal */
1517 		disconnect(FALSE);
1518 
1519 # ifdef LOCKF
1520 		/*
1521 		**  Now try to get our lock back.
1522 		*/
1523 
1524 		lfd.l_type = F_WRLCK;
1525 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1526 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
1527 		if (e->e_lockfp == NULL ||
1528 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
1529 		{
1530 			/* oops....  lost it */
1531 # ifdef LOG
1532 			if (LogLevel > 5)
1533 				syslog(LOG_NOTICE, "%s: lost lock: %m",
1534 					e->e_id);
1535 # endif /* LOG */
1536 			exit(EX_OK);
1537 		}
1538 # endif /* LOCKF */
1539 
1540 		break;
1541 	}
1542 
1543 	/*
1544 	**  Run through the list and send everything.
1545 	*/
1546 
1547 	nsent = 0;
1548 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1549 	{
1550 		if (mode == SM_VERIFY)
1551 		{
1552 			e->e_to = q->q_paddr;
1553 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1554 				message(Arpa_Info, "deliverable");
1555 		}
1556 		else if (!bitset(QDONTSEND, q->q_flags))
1557 		{
1558 # ifdef QUEUE
1559 			/*
1560 			**  Checkpoint the send list every few addresses
1561 			*/
1562 
1563 			if (nsent >= CheckpointInterval)
1564 			{
1565 				queueup(e, TRUE, FALSE);
1566 				nsent = 0;
1567 			}
1568 # endif /* QUEUE */
1569 			if (deliver(e, q) == EX_OK)
1570 				nsent++;
1571 		}
1572 	}
1573 	Verbose = oldverbose;
1574 
1575 	/*
1576 	**  Now run through and check for errors.
1577 	*/
1578 
1579 	if (mode == SM_VERIFY)
1580 		return;
1581 
1582 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1583 	{
1584 		register ADDRESS *qq;
1585 
1586 		if (tTd(13, 3))
1587 		{
1588 			printf("Checking ");
1589 			printaddr(q, FALSE);
1590 		}
1591 
1592 		/* only send errors if the message failed */
1593 		if (!bitset(QBADADDR, q->q_flags))
1594 			continue;
1595 
1596 		/* we have an address that failed -- find the parent */
1597 		for (qq = q; qq != NULL; qq = qq->q_alias)
1598 		{
1599 			char obuf[MAXNAME + 6];
1600 			extern char *aliaslookup();
1601 
1602 			/* we can only have owners for local addresses */
1603 			if (!bitnset(M_LOCAL, qq->q_mailer->m_flags))
1604 				continue;
1605 
1606 			/* see if the owner list exists */
1607 			(void) strcpy(obuf, "owner-");
1608 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1609 				(void) strcat(obuf, "owner");
1610 			else
1611 				(void) strcat(obuf, qq->q_user);
1612 			makelower(obuf);
1613 			if (aliaslookup(obuf) == NULL)
1614 				continue;
1615 
1616 			if (tTd(13, 4))
1617 				printf("Errors to %s\n", obuf);
1618 
1619 			/* owner list exists -- add it to the error queue */
1620 			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue, e);
1621 			ErrorMode = EM_MAIL;
1622 			break;
1623 		}
1624 
1625 		/* if we did not find an owner, send to the sender */
1626 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1627 			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue, e);
1628 	}
1629 
1630 	if (mode == SM_FORK)
1631 		finis();
1632 }
1633