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