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