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