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