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	6.1 (Berkeley) 12/21/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 <arpa/nameser.h>
21 #include <resolv.h>
22 #endif
23 
24 /*
25 **  DELIVER -- Deliver a message to a list of addresses.
26 **
27 **	This routine delivers to everyone on the same host as the
28 **	user on the head of the list.  It is clever about mailers
29 **	that don't handle multiple users.  It is NOT guaranteed
30 **	that it will deliver to all these addresses however -- so
31 **	deliver should be called once for each address on the
32 **	list.
33 **
34 **	Parameters:
35 **		e -- the envelope to deliver.
36 **		firstto -- head of the address list to deliver to.
37 **
38 **	Returns:
39 **		zero -- successfully delivered.
40 **		else -- some failure, see ExitStat for more info.
41 **
42 **	Side Effects:
43 **		The standard input is passed off to someone.
44 */
45 
46 deliver(e, firstto)
47 	register ENVELOPE *e;
48 	ADDRESS *firstto;
49 {
50 	char *host;			/* host being sent to */
51 	char *user;			/* user being sent to */
52 	char **pvp;
53 	register char **mvp;
54 	register char *p;
55 	register MAILER *m;		/* mailer for this recipient */
56 	ADDRESS *ctladdr;
57 	register MCI *mci;
58 	register ADDRESS *to = firstto;
59 	bool clever = FALSE;		/* running user smtp to this mailer */
60 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
61 	int rcode;			/* response code */
62 	char *from;			/* pointer to from person */
63 	char *pv[MAXPV+1];
64 	char tobuf[MAXLINE-50];		/* text line of to people */
65 	char buf[MAXNAME];
66 	char tfrombuf[MAXNAME];		/* translated from person */
67 	char rpathbuf[MAXNAME];		/* translated return path */
68 	extern bool checkcompat();
69 	extern ADDRESS *getctladdr();
70 	extern char *remotename();
71 	extern MCI *openmailer();
72 
73 	errno = 0;
74 	if (bitset(QDONTSEND, to->q_flags))
75 		return (0);
76 
77 #ifdef NAMED_BIND
78 	/* unless interactive, try twice, over a minute */
79 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
80 		_res.retrans = 30;
81 		_res.retry = 2;
82 	}
83 #endif
84 
85 	m = to->q_mailer;
86 	host = to->q_host;
87 
88 	if (tTd(10, 1))
89 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
90 			m->m_mno, host, to->q_user);
91 
92 	/*
93 	**  If this mailer is expensive, and if we don't want to make
94 	**  connections now, just mark these addresses and return.
95 	**	This is useful if we want to batch connections to
96 	**	reduce load.  This will cause the messages to be
97 	**	queued up, and a daemon will come along to send the
98 	**	messages later.
99 	**		This should be on a per-mailer basis.
100 	*/
101 
102 	if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) &&
103 	    !Verbose)
104 	{
105 		for (; to != NULL; to = to->q_next)
106 		{
107 			if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m)
108 				continue;
109 			to->q_flags |= QQUEUEUP|QDONTSEND;
110 			e->e_to = to->q_paddr;
111 			message(Arpa_Info, "queued");
112 			if (LogLevel > 4)
113 				logdelivery("queued", e);
114 		}
115 		e->e_to = NULL;
116 		return (0);
117 	}
118 
119 	/*
120 	**  Do initial argv setup.
121 	**	Insert the mailer name.  Notice that $x expansion is
122 	**	NOT done on the mailer name.  Then, if the mailer has
123 	**	a picky -f flag, we insert it as appropriate.  This
124 	**	code does not check for 'pv' overflow; this places a
125 	**	manifest lower limit of 4 for MAXPV.
126 	**		The from address rewrite is expected to make
127 	**		the address relative to the other end.
128 	*/
129 
130 	/* rewrite from address, using rewriting rules */
131 	(void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, TRUE, e));
132 	if (e->e_returnpath == e->e_sender)
133 	{
134 		from = rpathbuf;
135 	}
136 	else
137 	{
138 		(void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, TRUE, e));
139 		from = tfrombuf;
140 	}
141 
142 	define('f', e->e_returnpath, e);	/* raw return path */
143 	define('<', rpathbuf, e);		/* translated return path */
144 	define('g', from, e);			/* translated sender */
145 	define('h', host, e);			/* to host */
146 	Errors = 0;
147 	pvp = pv;
148 	*pvp++ = m->m_argv[0];
149 
150 	/* insert -f or -r flag as appropriate */
151 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
152 	{
153 		if (bitnset(M_FOPT, m->m_flags))
154 			*pvp++ = "-f";
155 		else
156 			*pvp++ = "-r";
157 		*pvp++ = newstr(rpathbuf);
158 	}
159 
160 	/*
161 	**  Append the other fixed parts of the argv.  These run
162 	**  up to the first entry containing "$u".  There can only
163 	**  be one of these, and there are only a few more slots
164 	**  in the pv after it.
165 	*/
166 
167 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
168 	{
169 		while ((p = strchr(p, '\001')) != NULL)
170 			if (*++p == 'u')
171 				break;
172 		if (p != NULL)
173 			break;
174 
175 		/* this entry is safe -- go ahead and process it */
176 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
177 		*pvp++ = newstr(buf);
178 		if (pvp >= &pv[MAXPV - 3])
179 		{
180 			syserr("Too many parameters to %s before $u", pv[0]);
181 			return (-1);
182 		}
183 	}
184 
185 	/*
186 	**  If we have no substitution for the user name in the argument
187 	**  list, we know that we must supply the names otherwise -- and
188 	**  SMTP is the answer!!
189 	*/
190 
191 	if (*mvp == NULL)
192 	{
193 		/* running SMTP */
194 # ifdef SMTP
195 		clever = TRUE;
196 		*pvp = NULL;
197 # else /* SMTP */
198 		/* oops!  we don't implement SMTP */
199 		syserr("SMTP style mailer");
200 		return (EX_SOFTWARE);
201 # endif /* SMTP */
202 	}
203 
204 	/*
205 	**  At this point *mvp points to the argument with $u.  We
206 	**  run through our address list and append all the addresses
207 	**  we can.  If we run out of space, do not fret!  We can
208 	**  always send another copy later.
209 	*/
210 
211 	tobuf[0] = '\0';
212 	e->e_to = tobuf;
213 	ctladdr = NULL;
214 	for (; to != NULL; to = to->q_next)
215 	{
216 		/* avoid sending multiple recipients to dumb mailers */
217 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
218 			break;
219 
220 		/* if already sent or not for this host, don't send */
221 		if (bitset(QDONTSEND, to->q_flags) ||
222 		    strcmp(to->q_host, host) != 0 ||
223 		    to->q_mailer != firstto->q_mailer)
224 			continue;
225 
226 		/* avoid overflowing tobuf */
227 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
228 			break;
229 
230 		if (tTd(10, 1))
231 		{
232 			printf("\nsend to ");
233 			printaddr(to, FALSE);
234 		}
235 
236 		/* compute effective uid/gid when sending */
237 		if (to->q_mailer == ProgMailer)
238 			ctladdr = getctladdr(to);
239 
240 		user = to->q_user;
241 		e->e_to = to->q_paddr;
242 		to->q_flags |= QDONTSEND;
243 
244 		/*
245 		**  Check to see that these people are allowed to
246 		**  talk to each other.
247 		*/
248 
249 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
250 		{
251 			NoReturn = TRUE;
252 			usrerr("Message is too large; %ld bytes max", m->m_maxsize);
253 			giveresponse(EX_UNAVAILABLE, m, e);
254 			continue;
255 		}
256 		if (!checkcompat(to, e))
257 		{
258 			giveresponse(EX_UNAVAILABLE, m, e);
259 			continue;
260 		}
261 
262 		/*
263 		**  Strip quote bits from names if the mailer is dumb
264 		**	about them.
265 		*/
266 
267 		if (bitnset(M_STRIPQ, m->m_flags))
268 		{
269 			stripquotes(user);
270 			stripquotes(host);
271 		}
272 
273 		/* hack attack -- delivermail compatibility */
274 		if (m == ProgMailer && *user == '|')
275 			user++;
276 
277 		/*
278 		**  If an error message has already been given, don't
279 		**	bother to send to this address.
280 		**
281 		**	>>>>>>>>>> This clause assumes that the local mailer
282 		**	>> NOTE >> cannot do any further aliasing; that
283 		**	>>>>>>>>>> function is subsumed by sendmail.
284 		*/
285 
286 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
287 			continue;
288 
289 		/* save statistics.... */
290 		markstats(e, to);
291 
292 		/*
293 		**  See if this user name is "special".
294 		**	If the user name has a slash in it, assume that this
295 		**	is a file -- send it off without further ado.  Note
296 		**	that this type of addresses is not processed along
297 		**	with the others, so we fudge on the To person.
298 		*/
299 
300 		if (m == LocalMailer)
301 		{
302 			if (user[0] == '/')
303 			{
304 				rcode = mailfile(user, getctladdr(to), e);
305 				giveresponse(rcode, m, e);
306 				if (rcode == EX_OK)
307 					to->q_flags |= QSENT;
308 				continue;
309 			}
310 		}
311 
312 		/*
313 		**  Address is verified -- add this user to mailer
314 		**  argv, and add it to the print list of recipients.
315 		*/
316 
317 		/* link together the chain of recipients */
318 		to->q_tchain = tochain;
319 		tochain = to;
320 
321 		/* create list of users for error messages */
322 		(void) strcat(tobuf, ",");
323 		(void) strcat(tobuf, to->q_paddr);
324 		define('u', user, e);		/* to user */
325 		define('z', to->q_home, e);	/* user's home */
326 
327 		/*
328 		**  Expand out this user into argument list.
329 		*/
330 
331 		if (!clever)
332 		{
333 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
334 			*pvp++ = newstr(buf);
335 			if (pvp >= &pv[MAXPV - 2])
336 			{
337 				/* allow some space for trailing parms */
338 				break;
339 			}
340 		}
341 	}
342 
343 	/* see if any addresses still exist */
344 	if (tobuf[0] == '\0')
345 	{
346 		define('g', (char *) NULL, e);
347 		define('<', (char *) NULL, e);
348 		return (0);
349 	}
350 
351 	/* print out messages as full list */
352 	e->e_to = tobuf + 1;
353 
354 	/*
355 	**  Fill out any parameters after the $u parameter.
356 	*/
357 
358 	while (!clever && *++mvp != NULL)
359 	{
360 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
361 		*pvp++ = newstr(buf);
362 		if (pvp >= &pv[MAXPV])
363 			syserr("deliver: pv overflow after $u for %s", pv[0]);
364 	}
365 	*pvp++ = NULL;
366 
367 	/*
368 	**  Call the mailer.
369 	**	The argument vector gets built, pipes
370 	**	are created as necessary, and we fork & exec as
371 	**	appropriate.
372 	**	If we are running SMTP, we just need to clean up.
373 	*/
374 
375 	if (ctladdr == NULL)
376 		ctladdr = &e->e_from;
377 #ifdef NAMED_BIND
378 	if (ConfigLevel < 2)
379 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
380 #endif
381 	mci = openmailer(m, pv, ctladdr, clever, e);
382 	if (mci == NULL)
383 	{
384 		/* catastrophic error */
385 		rcode = -1;
386 		goto give_up;
387 	}
388 	else if (mci->mci_state != MCIS_OPEN)
389 	{
390 		/* couldn't open the mailer */
391 		rcode = mci->mci_exitstat;
392 		errno = mci->mci_errno;
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[j], "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 			int saveerrno;
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 			saveerrno = errno;
926 			syserr("Cannot exec %s", m->m_mailer);
927 			if (m == LocalMailer)
928 				_exit(EX_TEMPFAIL);
929 			switch (saveerrno)
930 			{
931 			  case EIO:
932 			  case EAGAIN:
933 			  case ENOMEM:
934 # ifdef EPROCLIM
935 			  case EPROCLIM:
936 # endif
937 				_exit(EX_TEMPFAIL);
938 			}
939 			_exit(EX_UNAVAILABLE);
940 		}
941 
942 		/*
943 		**  Set up return value.
944 		*/
945 
946 		mci = (MCI *) xalloc(sizeof *mci);
947 		bzero((char *) mci, sizeof *mci);
948 		mci->mci_mailer = m;
949 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
950 		mci->mci_pid = pid;
951 		(void) close(mpvect[0]);
952 		mci->mci_out = fdopen(mpvect[1], "w");
953 		if (clever)
954 		{
955 			(void) close(rpvect[1]);
956 			mci->mci_in = fdopen(rpvect[0], "r");
957 		}
958 		else
959 		{
960 			mci->mci_flags |= MCIF_TEMP;
961 			mci->mci_in = NULL;
962 		}
963 	}
964 
965 	/*
966 	**  If we are in SMTP opening state, send initial protocol.
967 	*/
968 
969 	if (clever && mci->mci_state != MCIS_CLOSED)
970 	{
971 		smtpinit(m, mci, e);
972 	}
973 
974 	return mci;
975 }
976 /*
977 **  GIVERESPONSE -- Interpret an error response from a mailer
978 **
979 **	Parameters:
980 **		stat -- the status code from the mailer (high byte
981 **			only; core dumps must have been taken care of
982 **			already).
983 **		m -- the mailer descriptor for this mailer.
984 **
985 **	Returns:
986 **		none.
987 **
988 **	Side Effects:
989 **		Errors may be incremented.
990 **		ExitStat may be set.
991 */
992 
993 giveresponse(stat, m, e)
994 	int stat;
995 	register MAILER *m;
996 	ENVELOPE *e;
997 {
998 	register char *statmsg;
999 	extern char *SysExMsg[];
1000 	register int i;
1001 	extern int N_SysEx;
1002 #ifdef NAMED_BIND
1003 	extern int h_errno;
1004 #endif
1005 	char buf[MAXLINE];
1006 
1007 #ifdef lint
1008 	if (m == NULL)
1009 		return;
1010 #endif lint
1011 
1012 	/*
1013 	**  Compute status message from code.
1014 	*/
1015 
1016 	i = stat - EX__BASE;
1017 	if (stat == 0)
1018 		statmsg = "250 Sent";
1019 	else if (i < 0 || i > N_SysEx)
1020 	{
1021 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1022 		stat = EX_UNAVAILABLE;
1023 		statmsg = buf;
1024 	}
1025 	else if (stat == EX_TEMPFAIL)
1026 	{
1027 		(void) strcpy(buf, SysExMsg[i]);
1028 #ifdef NAMED_BIND
1029 		if (h_errno == TRY_AGAIN)
1030 		{
1031 			extern char *errstring();
1032 
1033 			statmsg = errstring(h_errno+MAX_ERRNO);
1034 		}
1035 		else
1036 #endif
1037 		{
1038 			if (errno != 0)
1039 			{
1040 				extern char *errstring();
1041 
1042 				statmsg = errstring(errno);
1043 			}
1044 			else
1045 			{
1046 #ifdef SMTP
1047 				extern char SmtpError[];
1048 
1049 				statmsg = SmtpError;
1050 #else /* SMTP */
1051 				statmsg = NULL;
1052 #endif /* SMTP */
1053 			}
1054 		}
1055 		if (statmsg != NULL && statmsg[0] != '\0')
1056 		{
1057 			(void) strcat(buf, ": ");
1058 			(void) strcat(buf, statmsg);
1059 		}
1060 		statmsg = buf;
1061 	}
1062 	else
1063 	{
1064 		statmsg = SysExMsg[i];
1065 	}
1066 
1067 	/*
1068 	**  Print the message as appropriate
1069 	*/
1070 
1071 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1072 		message(Arpa_Info, &statmsg[4]);
1073 	else
1074 	{
1075 		Errors++;
1076 		usrerr(statmsg);
1077 	}
1078 
1079 	/*
1080 	**  Final cleanup.
1081 	**	Log a record of the transaction.  Compute the new
1082 	**	ExitStat -- if we already had an error, stick with
1083 	**	that.
1084 	*/
1085 
1086 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
1087 		logdelivery(&statmsg[4], e);
1088 
1089 	if (stat != EX_TEMPFAIL)
1090 		setstat(stat);
1091 	if (stat != EX_OK)
1092 	{
1093 		if (e->e_message != NULL)
1094 			free(e->e_message);
1095 		e->e_message = newstr(&statmsg[4]);
1096 	}
1097 	errno = 0;
1098 #ifdef NAMED_BIND
1099 	h_errno = 0;
1100 #endif
1101 }
1102 /*
1103 **  LOGDELIVERY -- log the delivery in the system log
1104 **
1105 **	Parameters:
1106 **		stat -- the message to print for the status
1107 **
1108 **	Returns:
1109 **		none
1110 **
1111 **	Side Effects:
1112 **		none
1113 */
1114 
1115 logdelivery(stat, e)
1116 	char *stat;
1117 	register ENVELOPE *e;
1118 {
1119 	extern char *pintvl();
1120 
1121 # ifdef LOG
1122 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", e->e_id,
1123 	       e->e_to, pintvl(curtime() - e->e_ctime, TRUE), stat);
1124 # endif /* LOG */
1125 }
1126 /*
1127 **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
1128 **
1129 **	This can be made an arbitrary message separator by changing $l
1130 **
1131 **	One of the ugliest hacks seen by human eyes is contained herein:
1132 **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
1133 **	does a well-meaning programmer such as myself have to deal with
1134 **	this kind of antique garbage????
1135 **
1136 **	Parameters:
1137 **		fp -- the file to output to.
1138 **		m -- the mailer describing this entry.
1139 **
1140 **	Returns:
1141 **		none
1142 **
1143 **	Side Effects:
1144 **		outputs some text to fp.
1145 */
1146 
1147 putfromline(fp, m, e)
1148 	register FILE *fp;
1149 	register MAILER *m;
1150 	ENVELOPE *e;
1151 {
1152 	char *template = "\001l\n";
1153 	char buf[MAXLINE];
1154 
1155 	if (bitnset(M_NHDR, m->m_flags))
1156 		return;
1157 
1158 # ifdef UGLYUUCP
1159 	if (bitnset(M_UGLYUUCP, m->m_flags))
1160 	{
1161 		char *bang;
1162 		char xbuf[MAXLINE];
1163 
1164 		expand("\001<", buf, &buf[sizeof buf - 1], e);
1165 		bang = strchr(buf, '!');
1166 		if (bang == NULL)
1167 			syserr("No ! in UUCP! (%s)", buf);
1168 		else
1169 		{
1170 			*bang++ = '\0';
1171 			(void) sprintf(xbuf, "From %s  \001d remote from %s\n", bang, buf);
1172 			template = xbuf;
1173 		}
1174 	}
1175 # endif /* UGLYUUCP */
1176 	expand(template, buf, &buf[sizeof buf - 1], e);
1177 	putline(buf, fp, m);
1178 }
1179 /*
1180 **  PUTBODY -- put the body of a message.
1181 **
1182 **	Parameters:
1183 **		fp -- file to output onto.
1184 **		m -- a mailer descriptor to control output format.
1185 **		e -- the envelope to put out.
1186 **
1187 **	Returns:
1188 **		none.
1189 **
1190 **	Side Effects:
1191 **		The message is written onto fp.
1192 */
1193 
1194 putbody(fp, m, e)
1195 	FILE *fp;
1196 	MAILER *m;
1197 	register ENVELOPE *e;
1198 {
1199 	char buf[MAXLINE];
1200 
1201 	/*
1202 	**  Output the body of the message
1203 	*/
1204 
1205 	if (e->e_dfp == NULL)
1206 	{
1207 		if (e->e_df != NULL)
1208 		{
1209 			e->e_dfp = fopen(e->e_df, "r");
1210 			if (e->e_dfp == NULL)
1211 				syserr("putbody: Cannot open %s for %s from %s",
1212 				e->e_df, e->e_to, e->e_from);
1213 		}
1214 		else
1215 			putline("<<< No Message Collected >>>", fp, m);
1216 	}
1217 	if (e->e_dfp != NULL)
1218 	{
1219 		rewind(e->e_dfp);
1220 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
1221 		{
1222 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1223 			    strncmp(buf, "From ", 5) == 0)
1224 				(void) putc('>', fp);
1225 			putline(buf, fp, m);
1226 		}
1227 
1228 		if (ferror(e->e_dfp))
1229 		{
1230 			syserr("putbody: read error");
1231 			ExitStat = EX_IOERR;
1232 		}
1233 	}
1234 
1235 	(void) fflush(fp);
1236 	if (ferror(fp) && errno != EPIPE)
1237 	{
1238 		syserr("putbody: write error");
1239 		ExitStat = EX_IOERR;
1240 	}
1241 	errno = 0;
1242 }
1243 /*
1244 **  MAILFILE -- Send a message to a file.
1245 **
1246 **	If the file has the setuid/setgid bits set, but NO execute
1247 **	bits, sendmail will try to become the owner of that file
1248 **	rather than the real user.  Obviously, this only works if
1249 **	sendmail runs as root.
1250 **
1251 **	This could be done as a subordinate mailer, except that it
1252 **	is used implicitly to save messages in ~/dead.letter.  We
1253 **	view this as being sufficiently important as to include it
1254 **	here.  For example, if the system is dying, we shouldn't have
1255 **	to create another process plus some pipes to save the message.
1256 **
1257 **	Parameters:
1258 **		filename -- the name of the file to send to.
1259 **		ctladdr -- the controlling address header -- includes
1260 **			the userid/groupid to be when sending.
1261 **
1262 **	Returns:
1263 **		The exit code associated with the operation.
1264 **
1265 **	Side Effects:
1266 **		none.
1267 */
1268 
1269 mailfile(filename, ctladdr, e)
1270 	char *filename;
1271 	ADDRESS *ctladdr;
1272 	register ENVELOPE *e;
1273 {
1274 	register FILE *f;
1275 	register int pid;
1276 	int mode;
1277 
1278 	/*
1279 	**  Fork so we can change permissions here.
1280 	**	Note that we MUST use fork, not vfork, because of
1281 	**	the complications of calling subroutines, etc.
1282 	*/
1283 
1284 	DOFORK(fork);
1285 
1286 	if (pid < 0)
1287 		return (EX_OSERR);
1288 	else if (pid == 0)
1289 	{
1290 		/* child -- actually write to file */
1291 		struct stat stb;
1292 
1293 		(void) signal(SIGINT, SIG_DFL);
1294 		(void) signal(SIGHUP, SIG_DFL);
1295 		(void) signal(SIGTERM, SIG_DFL);
1296 		(void) umask(OldUmask);
1297 
1298 		if (stat(filename, &stb) < 0)
1299 			stb.st_mode = 0666;
1300 		mode = stb.st_mode;
1301 
1302 		/* limit the errors to those actually caused in the child */
1303 		errno = 0;
1304 		ExitStat = EX_OK;
1305 
1306 		if (bitset(0111, stb.st_mode))
1307 			exit(EX_CANTCREAT);
1308 		if (ctladdr == NULL)
1309 			ctladdr = &e->e_from;
1310 		else
1311 		{
1312 			/* ignore setuid and setgid bits */
1313 			mode &= ~(S_ISGID|S_ISUID);
1314 		}
1315 
1316 		/* we have to open the dfile BEFORE setuid */
1317 		if (e->e_dfp == NULL && e->e_df != NULL)
1318 		{
1319 			e->e_dfp = fopen(e->e_df, "r");
1320 			if (e->e_dfp == NULL)
1321 			{
1322 				syserr("mailfile: Cannot open %s for %s from %s",
1323 					e->e_df, e->e_to, e->e_from);
1324 			}
1325 		}
1326 
1327 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1328 		{
1329 			if (ctladdr->q_uid == 0)
1330 			{
1331 				(void) setgid(DefGid);
1332 				(void) initgroups(DefUser, DefGid);
1333 			}
1334 			else
1335 			{
1336 				(void) setgid(ctladdr->q_gid);
1337 				(void) initgroups(ctladdr->q_ruser ?
1338 					ctladdr->q_ruser : ctladdr->q_user,
1339 					ctladdr->q_gid);
1340 			}
1341 		}
1342 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1343 		{
1344 			if (ctladdr->q_uid == 0)
1345 				(void) setuid(DefUid);
1346 			else
1347 				(void) setuid(ctladdr->q_uid);
1348 		}
1349 		FileName = filename;
1350 		LineNumber = 0;
1351 		f = dfopen(filename, "a");
1352 		if (f == NULL)
1353 		{
1354 			message("cannot open");
1355 			exit(EX_CANTCREAT);
1356 		}
1357 
1358 		putfromline(f, ProgMailer, e);
1359 		(*e->e_puthdr)(f, ProgMailer, e);
1360 		putline("\n", f, ProgMailer);
1361 		(*e->e_putbody)(f, ProgMailer, e);
1362 		putline("\n", f, ProgMailer);
1363 		if (ferror(f))
1364 		{
1365 			message("I/O error");
1366 			setstat(EX_IOERR);
1367 		}
1368 		(void) fclose(f);
1369 		(void) fflush(stdout);
1370 
1371 		/* reset ISUID & ISGID bits for paranoid systems */
1372 		(void) chmod(filename, (int) stb.st_mode);
1373 		exit(ExitStat);
1374 		/*NOTREACHED*/
1375 	}
1376 	else
1377 	{
1378 		/* parent -- wait for exit status */
1379 		int st;
1380 
1381 		st = waitfor(pid);
1382 		if ((st & 0377) != 0)
1383 			return (EX_UNAVAILABLE);
1384 		else
1385 			return ((st >> 8) & 0377);
1386 		/*NOTREACHED*/
1387 	}
1388 }
1389 /*
1390 **  SENDALL -- actually send all the messages.
1391 **
1392 **	Parameters:
1393 **		e -- the envelope to send.
1394 **		mode -- the delivery mode to use.  If SM_DEFAULT, use
1395 **			the current SendMode.
1396 **
1397 **	Returns:
1398 **		none.
1399 **
1400 **	Side Effects:
1401 **		Scans the send lists and sends everything it finds.
1402 **		Delivers any appropriate error messages.
1403 **		If we are running in a non-interactive mode, takes the
1404 **			appropriate action.
1405 */
1406 
1407 sendall(e, mode)
1408 	ENVELOPE *e;
1409 	char mode;
1410 {
1411 	register ADDRESS *q;
1412 	bool oldverbose;
1413 	int pid;
1414 	int nsent;
1415 # ifdef LOCKF
1416 	struct flock lfd;
1417 # endif
1418 
1419 	/* determine actual delivery mode */
1420 	if (mode == SM_DEFAULT)
1421 	{
1422 		extern bool shouldqueue();
1423 
1424 		if (shouldqueue(e->e_msgpriority))
1425 			mode = SM_QUEUE;
1426 		else
1427 			mode = SendMode;
1428 	}
1429 
1430 	if (tTd(13, 1))
1431 	{
1432 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
1433 		printaddr(e->e_sendqueue, TRUE);
1434 	}
1435 
1436 	/*
1437 	**  Do any preprocessing necessary for the mode we are running.
1438 	**	Check to make sure the hop count is reasonable.
1439 	**	Delete sends to the sender in mailing lists.
1440 	*/
1441 
1442 	CurEnv = e;
1443 
1444 	if (e->e_hopcount > MaxHopCount)
1445 	{
1446 		errno = 0;
1447 		syserr("sendall: too many hops %d (%d max): from %s, to %s",
1448 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr, e->e_to);
1449 		return;
1450 	}
1451 
1452 	if (!MeToo)
1453 	{
1454 		extern ADDRESS *recipient();
1455 
1456 		e->e_from.q_flags |= QDONTSEND;
1457 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1458 	}
1459 
1460 # ifdef QUEUE
1461 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1462 	     (mode != SM_VERIFY && SuperSafe)) &&
1463 	    !bitset(EF_INQUEUE, e->e_flags))
1464 		queueup(e, TRUE, mode == SM_QUEUE);
1465 #endif /* QUEUE */
1466 
1467 	oldverbose = Verbose;
1468 	switch (mode)
1469 	{
1470 	  case SM_VERIFY:
1471 		Verbose = TRUE;
1472 		break;
1473 
1474 	  case SM_QUEUE:
1475   queueonly:
1476 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1477 		return;
1478 
1479 	  case SM_FORK:
1480 		if (e->e_xfp != NULL)
1481 			(void) fflush(e->e_xfp);
1482 
1483 # ifdef LOCKF
1484 		/*
1485 		**  Since lockf has the interesting semantic that the
1486 		**  lock is lost when we fork, we have to risk losing
1487 		**  the lock here by closing before the fork, and then
1488 		**  trying to get it back in the child.
1489 		*/
1490 
1491 		if (e->e_lockfp != NULL)
1492 		{
1493 			(void) fclose(e->e_lockfp);
1494 			e->e_lockfp = NULL;
1495 		}
1496 # endif /* LOCKF */
1497 
1498 		pid = fork();
1499 		if (pid < 0)
1500 		{
1501 			goto queueonly;
1502 		}
1503 		else if (pid > 0)
1504 		{
1505 			/* be sure we leave the temp files to our child */
1506 			e->e_id = e->e_df = NULL;
1507 # ifndef LOCKF
1508 			if (e->e_lockfp != NULL)
1509 				(void) fclose(e->e_lockfp);
1510 # endif
1511 			return;
1512 		}
1513 
1514 		/* double fork to avoid zombies */
1515 		if (fork() > 0)
1516 			exit(EX_OK);
1517 
1518 		/* be sure we are immune from the terminal */
1519 		disconnect(FALSE);
1520 
1521 # ifdef LOCKF
1522 		/*
1523 		**  Now try to get our lock back.
1524 		*/
1525 
1526 		lfd.l_type = F_WRLCK;
1527 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1528 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
1529 		if (e->e_lockfp == NULL ||
1530 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
1531 		{
1532 			/* oops....  lost it */
1533 # ifdef LOG
1534 			if (LogLevel > 5)
1535 				syslog(LOG_NOTICE, "%s: lost lock: %m",
1536 					e->e_id);
1537 # endif /* LOG */
1538 			exit(EX_OK);
1539 		}
1540 # endif /* LOCKF */
1541 
1542 		break;
1543 	}
1544 
1545 	/*
1546 	**  Run through the list and send everything.
1547 	*/
1548 
1549 	nsent = 0;
1550 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1551 	{
1552 		if (mode == SM_VERIFY)
1553 		{
1554 			e->e_to = q->q_paddr;
1555 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1556 				message(Arpa_Info, "deliverable");
1557 		}
1558 		else if (!bitset(QDONTSEND, q->q_flags))
1559 		{
1560 # ifdef QUEUE
1561 			/*
1562 			**  Checkpoint the send list every few addresses
1563 			*/
1564 
1565 			if (nsent >= CheckpointInterval)
1566 			{
1567 				queueup(e, TRUE, FALSE);
1568 				nsent = 0;
1569 			}
1570 # endif /* QUEUE */
1571 			if (deliver(e, q) == EX_OK)
1572 				nsent++;
1573 		}
1574 	}
1575 	Verbose = oldverbose;
1576 
1577 	/*
1578 	**  Now run through and check for errors.
1579 	*/
1580 
1581 	if (mode == SM_VERIFY)
1582 		return;
1583 
1584 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1585 	{
1586 		register ADDRESS *qq;
1587 
1588 		if (tTd(13, 3))
1589 		{
1590 			printf("Checking ");
1591 			printaddr(q, FALSE);
1592 		}
1593 
1594 		/* only send errors if the message failed */
1595 		if (!bitset(QBADADDR, q->q_flags))
1596 			continue;
1597 
1598 		/* we have an address that failed -- find the parent */
1599 		for (qq = q; qq != NULL; qq = qq->q_alias)
1600 		{
1601 			char obuf[MAXNAME + 6];
1602 			extern char *aliaslookup();
1603 
1604 			/* we can only have owners for local addresses */
1605 			if (!bitnset(M_LOCAL, qq->q_mailer->m_flags))
1606 				continue;
1607 
1608 			/* see if the owner list exists */
1609 			(void) strcpy(obuf, "owner-");
1610 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1611 				(void) strcat(obuf, "owner");
1612 			else
1613 				(void) strcat(obuf, qq->q_user);
1614 			makelower(obuf);
1615 			if (aliaslookup(obuf) == NULL)
1616 				continue;
1617 
1618 			if (tTd(13, 4))
1619 				printf("Errors to %s\n", obuf);
1620 
1621 			/* owner list exists -- add it to the error queue */
1622 			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue, e);
1623 			ErrorMode = EM_MAIL;
1624 			break;
1625 		}
1626 
1627 		/* if we did not find an owner, send to the sender */
1628 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1629 			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue, e);
1630 	}
1631 
1632 	if (mode == SM_FORK)
1633 		finis();
1634 }
1635