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