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