1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)savemail.c	8.43 (Berkeley) 11/23/94";
11 #endif /* not lint */
12 
13 # include "sendmail.h"
14 # include <pwd.h>
15 
16 /*
17 **  SAVEMAIL -- Save mail on error
18 **
19 **	If mailing back errors, mail it back to the originator
20 **	together with an error message; otherwise, just put it in
21 **	dead.letter in the user's home directory (if he exists on
22 **	this machine).
23 **
24 **	Parameters:
25 **		e -- the envelope containing the message in error.
26 **		sendbody -- if TRUE, also send back the body of the
27 **			message; otherwise just send the header.
28 **
29 **	Returns:
30 **		none
31 **
32 **	Side Effects:
33 **		Saves the letter, by writing or mailing it back to the
34 **		sender, or by putting it in dead.letter in her home
35 **		directory.
36 */
37 
38 /* defines for state machine */
39 # define ESM_REPORT	0	/* report to sender's terminal */
40 # define ESM_MAIL	1	/* mail back to sender */
41 # define ESM_QUIET	2	/* messages have already been returned */
42 # define ESM_DEADLETTER	3	/* save in ~/dead.letter */
43 # define ESM_POSTMASTER	4	/* return to postmaster */
44 # define ESM_USRTMP	5	/* save in /usr/tmp/dead.letter */
45 # define ESM_PANIC	6	/* leave the locked queue/transcript files */
46 # define ESM_DONE	7	/* the message is successfully delivered */
47 
48 # ifndef _PATH_VARTMP
49 #  define _PATH_VARTMP	"/usr/tmp/"
50 # endif
51 
52 
53 savemail(e, sendbody)
54 	register ENVELOPE *e;
55 	bool sendbody;
56 {
57 	register struct passwd *pw;
58 	register FILE *fp;
59 	int state;
60 	auto ADDRESS *q = NULL;
61 	register char *p;
62 	MCI mcibuf;
63 	char buf[MAXLINE+1];
64 	extern struct passwd *getpwnam();
65 	extern char *ttypath();
66 	typedef int (*fnptr)();
67 	extern bool writable();
68 
69 	if (tTd(6, 1))
70 	{
71 		printf("\nsavemail, errormode = %c, id = %s, ExitStat = %d\n  e_from=",
72 			e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id,
73 			ExitStat);
74 		printaddr(&e->e_from, FALSE);
75 	}
76 
77 	if (e->e_id == NULL)
78 	{
79 		/* can't return a message with no id */
80 		return;
81 	}
82 
83 	/*
84 	**  In the unhappy event we don't know who to return the mail
85 	**  to, make someone up.
86 	*/
87 
88 	if (e->e_from.q_paddr == NULL)
89 	{
90 		e->e_sender = "Postmaster";
91 		if (parseaddr(e->e_sender, &e->e_from,
92 			      RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL)
93 		{
94 			syserr("553 Cannot parse Postmaster!");
95 			ExitStat = EX_SOFTWARE;
96 			finis();
97 		}
98 	}
99 	e->e_to = NULL;
100 
101 	/*
102 	**  Basic state machine.
103 	**
104 	**	This machine runs through the following states:
105 	**
106 	**	ESM_QUIET	Errors have already been printed iff the
107 	**			sender is local.
108 	**	ESM_REPORT	Report directly to the sender's terminal.
109 	**	ESM_MAIL	Mail response to the sender.
110 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
111 	**	ESM_POSTMASTER	Mail response to the postmaster.
112 	**	ESM_PANIC	Save response anywhere possible.
113 	*/
114 
115 	/* determine starting state */
116 	switch (e->e_errormode)
117 	{
118 	  case EM_WRITE:
119 		state = ESM_REPORT;
120 		break;
121 
122 	  case EM_BERKNET:
123 		/* mail back, but return o.k. exit status */
124 		ExitStat = EX_OK;
125 
126 		/* fall through.... */
127 
128 	  case EM_MAIL:
129 		state = ESM_MAIL;
130 		break;
131 
132 	  case EM_PRINT:
133 	  case '\0':
134 		state = ESM_QUIET;
135 		break;
136 
137 	  case EM_QUIET:
138 		/* no need to return anything at all */
139 		return;
140 
141 	  default:
142 		syserr("554 savemail: bogus errormode x%x\n", e->e_errormode);
143 		state = ESM_MAIL;
144 		break;
145 	}
146 
147 	/* if this is already an error response, send to postmaster */
148 	if (bitset(EF_RESPONSE, e->e_flags))
149 	{
150 		if (e->e_parent != NULL &&
151 		    bitset(EF_RESPONSE, e->e_parent->e_flags))
152 		{
153 			/* got an error sending a response -- can it */
154 			return;
155 		}
156 		state = ESM_POSTMASTER;
157 	}
158 
159 	while (state != ESM_DONE)
160 	{
161 		if (tTd(6, 5))
162 			printf("  state %d\n", state);
163 
164 		switch (state)
165 		{
166 		  case ESM_QUIET:
167 			if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags))
168 				state = ESM_DEADLETTER;
169 			else
170 				state = ESM_MAIL;
171 			break;
172 
173 		  case ESM_REPORT:
174 
175 			/*
176 			**  If the user is still logged in on the same terminal,
177 			**  then write the error messages back to hir (sic).
178 			*/
179 
180 			p = ttypath();
181 			if (p == NULL || freopen(p, "w", stdout) == NULL)
182 			{
183 				state = ESM_MAIL;
184 				break;
185 			}
186 
187 			expand("\201n", buf, &buf[sizeof buf - 1], e);
188 			printf("\r\nMessage from %s...\r\n", buf);
189 			printf("Errors occurred while sending mail.\r\n");
190 			if (e->e_xfp != NULL)
191 			{
192 				(void) fflush(e->e_xfp);
193 				fp = fopen(queuename(e, 'x'), "r");
194 			}
195 			else
196 				fp = NULL;
197 			if (fp == NULL)
198 			{
199 				syserr("Cannot open %s", queuename(e, 'x'));
200 				printf("Transcript of session is unavailable.\r\n");
201 			}
202 			else
203 			{
204 				printf("Transcript follows:\r\n");
205 				while (fgets(buf, sizeof buf, fp) != NULL &&
206 				       !ferror(stdout))
207 					fputs(buf, stdout);
208 				(void) xfclose(fp, "savemail transcript", e->e_id);
209 			}
210 			printf("Original message will be saved in dead.letter.\r\n");
211 			state = ESM_DEADLETTER;
212 			break;
213 
214 		  case ESM_MAIL:
215 			/*
216 			**  If mailing back, do it.
217 			**	Throw away all further output.  Don't alias,
218 			**	since this could cause loops, e.g., if joe
219 			**	mails to joe@x, and for some reason the network
220 			**	for @x is down, then the response gets sent to
221 			**	joe@x, which gives a response, etc.  Also force
222 			**	the mail to be delivered even if a version of
223 			**	it has already been sent to the sender.
224 			**
225 			**  If this is a configuration or local software
226 			**	error, send to the local postmaster as well,
227 			**	since the originator can't do anything
228 			**	about it anyway.  Note that this is a full
229 			**	copy of the message (intentionally) so that
230 			**	the Postmaster can forward things along.
231 			*/
232 
233 			if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
234 			{
235 				(void) sendtolist("postmaster",
236 					  NULLADDR, &e->e_errorqueue, 0, e);
237 			}
238 			if (!emptyaddr(&e->e_from))
239 			{
240 				(void) sendtolist(e->e_from.q_paddr,
241 					  NULLADDR, &e->e_errorqueue, 0, e);
242 			}
243 
244 			/*
245 			**  Deliver a non-delivery report to the
246 			**  Postmaster-designate (not necessarily
247 			**  Postmaster).  This does not include the
248 			**  body of the message, for privacy reasons.
249 			**  You really shouldn't need this.
250 			*/
251 
252 			e->e_flags |= EF_PM_NOTIFY;
253 
254 			/* check to see if there are any good addresses */
255 			for (q = e->e_errorqueue; q != NULL; q = q->q_next)
256 				if (!bitset(QBADADDR|QDONTSEND, q->q_flags))
257 					break;
258 			if (q == NULL)
259 			{
260 				/* this is an error-error */
261 				state = ESM_POSTMASTER;
262 				break;
263 			}
264 			if (returntosender(e->e_message, e->e_errorqueue,
265 					   sendbody, e) == 0)
266 			{
267 				state = ESM_DONE;
268 				break;
269 			}
270 
271 			/* didn't work -- return to postmaster */
272 			state = ESM_POSTMASTER;
273 			break;
274 
275 		  case ESM_POSTMASTER:
276 			/*
277 			**  Similar to previous case, but to system postmaster.
278 			*/
279 
280 			q = NULL;
281 			if (sendtolist("postmaster", NULL, &q, 0, e) <= 0)
282 			{
283 				syserr("553 cannot parse postmaster!");
284 				ExitStat = EX_SOFTWARE;
285 				state = ESM_USRTMP;
286 				break;
287 			}
288 			if (returntosender(e->e_message, q, sendbody, e) == 0)
289 			{
290 				state = ESM_DONE;
291 				break;
292 			}
293 
294 			/* didn't work -- last resort */
295 			state = ESM_USRTMP;
296 			break;
297 
298 		  case ESM_DEADLETTER:
299 			/*
300 			**  Save the message in dead.letter.
301 			**	If we weren't mailing back, and the user is
302 			**	local, we should save the message in
303 			**	~/dead.letter so that the poor person doesn't
304 			**	have to type it over again -- and we all know
305 			**	what poor typists UNIX users are.
306 			*/
307 
308 			p = NULL;
309 			if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
310 			{
311 				if (e->e_from.q_home != NULL)
312 					p = e->e_from.q_home;
313 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
314 					p = pw->pw_dir;
315 			}
316 			if (p == NULL)
317 			{
318 				/* no local directory */
319 				state = ESM_MAIL;
320 				break;
321 			}
322 			if (e->e_dfp != NULL)
323 			{
324 				bool oldverb = Verbose;
325 
326 				/* we have a home directory; open dead.letter */
327 				define('z', p, e);
328 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
329 				Verbose = TRUE;
330 				message("Saving message in %s", buf);
331 				Verbose = oldverb;
332 				e->e_to = buf;
333 				q = NULL;
334 				(void) sendtolist(buf, &e->e_from, &q, 0, e);
335 				if (q != NULL &&
336 				    !bitset(QBADADDR, q->q_flags) &&
337 				    deliver(e, q) == 0)
338 					state = ESM_DONE;
339 				else
340 					state = ESM_MAIL;
341 			}
342 			else
343 			{
344 				/* no data file -- try mailing back */
345 				state = ESM_MAIL;
346 			}
347 			break;
348 
349 		  case ESM_USRTMP:
350 			/*
351 			**  Log the mail in /usr/tmp/dead.letter.
352 			*/
353 
354 			if (e->e_class < 0)
355 			{
356 				state = ESM_DONE;
357 				break;
358 			}
359 
360 			strcpy(buf, _PATH_VARTMP);
361 			strcat(buf, "dead.letter");
362 			if (!writable(buf, NULLADDR, SFF_NOSLINK))
363 			{
364 				state = ESM_PANIC;
365 				break;
366 			}
367 			fp = dfopen(buf, O_WRONLY|O_CREAT|O_APPEND, FileMode);
368 			if (fp == NULL)
369 			{
370 				state = ESM_PANIC;
371 				break;
372 			}
373 
374 			bzero(&mcibuf, sizeof mcibuf);
375 			mcibuf.mci_out = fp;
376 			mcibuf.mci_mailer = FileMailer;
377 			if (bitnset(M_7BITS, FileMailer->m_flags))
378 				mcibuf.mci_flags |= MCIF_7BIT;
379 
380 			putfromline(&mcibuf, e);
381 			(*e->e_puthdr)(&mcibuf, e->e_header, e, 0);
382 			(*e->e_putbody)(&mcibuf, e, NULL, 0);
383 			putline("\n", &mcibuf);
384 			(void) fflush(fp);
385 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
386 			(void) xfclose(fp, "savemail", buf);
387 			break;
388 
389 		  default:
390 			syserr("554 savemail: unknown state %d", state);
391 
392 			/* fall through ... */
393 
394 		  case ESM_PANIC:
395 			/* leave the locked queue & transcript files around */
396 			syserr("!554 savemail: cannot save rejected email anywhere");
397 		}
398 	}
399 }
400 /*
401 **  RETURNTOSENDER -- return a message to the sender with an error.
402 **
403 **	Parameters:
404 **		msg -- the explanatory message.
405 **		returnq -- the queue of people to send the message to.
406 **		sendbody -- if TRUE, also send back the body of the
407 **			message; otherwise just send the header.
408 **		e -- the current envelope.
409 **
410 **	Returns:
411 **		zero -- if everything went ok.
412 **		else -- some error.
413 **
414 **	Side Effects:
415 **		Returns the current message to the sender via
416 **		mail.
417 */
418 
419 static bool	SendBody;
420 
421 #define MAXRETURNS	6	/* max depth of returning messages */
422 #define ERRORFUDGE	100	/* nominal size of error message text */
423 
424 returntosender(msg, returnq, sendbody, e)
425 	char *msg;
426 	ADDRESS *returnq;
427 	bool sendbody;
428 	register ENVELOPE *e;
429 {
430 	char buf[MAXNAME];
431 	extern putheader(), errbody();
432 	register ENVELOPE *ee;
433 	ENVELOPE *oldcur = CurEnv;
434 	ENVELOPE errenvelope;
435 	static int returndepth;
436 	register ADDRESS *q;
437 
438 	if (returnq == NULL)
439 		return (-1);
440 
441 	if (msg == NULL)
442 		msg = "Unable to deliver mail";
443 
444 	if (tTd(6, 1))
445 	{
446 		printf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
447 		       msg, returndepth, e);
448 		printaddr(returnq, TRUE);
449 		if (tTd(6, 20))
450 		{
451 			printf("Sendq=");
452 			printaddr(e->e_sendqueue, TRUE);
453 		}
454 	}
455 
456 	if (++returndepth >= MAXRETURNS)
457 	{
458 		if (returndepth != MAXRETURNS)
459 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
460 		/* don't "unrecurse" and fake a clean exit */
461 		/* returndepth--; */
462 		return (0);
463 	}
464 
465 	SendBody = sendbody;
466 	define('g', e->e_from.q_paddr, e);
467 	define('u', NULL, e);
468 
469 	/* initialize error envelope */
470 	ee = newenvelope(&errenvelope, e);
471 	define('a', "\201b", ee);
472 	define('r', "internal", ee);
473 	define('s', "localhost", ee);
474 	define('_', "localhost", ee);
475 	ee->e_puthdr = putheader;
476 	ee->e_putbody = errbody;
477 	ee->e_flags |= EF_RESPONSE|EF_METOO;
478 	if (!bitset(EF_OLDSTYLE, e->e_flags))
479 		ee->e_flags &= ~EF_OLDSTYLE;
480 	ee->e_sendqueue = returnq;
481 	ee->e_msgsize = ERRORFUDGE;
482 	if (!bitset(EF_NORETURN, e->e_flags))
483 		ee->e_msgsize += e->e_msgsize;
484 	initsys(ee);
485 	for (q = returnq; q != NULL; q = q->q_next)
486 	{
487 		if (bitset(QBADADDR, q->q_flags))
488 			continue;
489 
490 		if (!bitset(QDONTSEND, q->q_flags))
491 			ee->e_nrcpts++;
492 
493 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
494 			parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e);
495 
496 		if (q->q_alias == NULL)
497 			addheader("To", q->q_paddr, &ee->e_header);
498 	}
499 
500 # ifdef LOG
501 	if (LogLevel > 5)
502 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
503 			e->e_id, ee->e_id, msg);
504 # endif
505 
506 	if (strncmp(msg, "Warning:", 8) == 0)
507 	{
508 		addheader("Subject", msg, ee);
509 		addheader("Precedence", "autoreply warning-timeout", &ee->e_header);
510 	}
511 	else if (strcmp(msg, "Return receipt") == 0)
512 	{
513 		addheader("Subject", msg, ee);
514 		addheader("Precedence", "autoreply return-receipt", &ee->e_header);
515 	}
516 	else
517 	{
518 		sprintf(buf, "Returned mail: %.*s", sizeof buf - 20, msg);
519 		addheader("Subject", buf, &ee->e_header);
520 		addheader("Precedence", "autoreply failure-message", &ee->e_header);
521 	}
522 	if (SendMIMEErrors)
523 	{
524 		addheader("MIME-Version", "1.0", &ee->e_header);
525 		(void) sprintf(buf, "%s.%ld/%s",
526 			ee->e_id, curtime(), MyHostName);
527 		ee->e_msgboundary = newstr(buf);
528 		(void) sprintf(buf,
529 			"multipart/report; report-type=delivery-status; boundary=\"%s\"",
530 			ee->e_msgboundary);
531 		addheader("Content-Type", buf, &ee->e_header);
532 	}
533 
534 	/* fake up an address header for the from person */
535 	expand("\201n", buf, &buf[sizeof buf - 1], e);
536 	if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL)
537 	{
538 		syserr("553 Can't parse myself!");
539 		ExitStat = EX_SOFTWARE;
540 		returndepth--;
541 		return (-1);
542 	}
543 	ee->e_sender = ee->e_from.q_paddr;
544 
545 	/* push state into submessage */
546 	CurEnv = ee;
547 	define('f', "\201n", ee);
548 	define('x', "Mail Delivery Subsystem", ee);
549 	eatheader(ee, TRUE);
550 
551 	/* mark statistics */
552 	markstats(ee, NULLADDR);
553 
554 	/* actually deliver the error message */
555 	sendall(ee, SM_DEFAULT);
556 
557 	/* restore state */
558 	dropenvelope(ee);
559 	CurEnv = oldcur;
560 	returndepth--;
561 
562 	/* should check for delivery errors here */
563 	return (0);
564 }
565 /*
566 **  ERRBODY -- output the body of an error message.
567 **
568 **	Typically this is a copy of the transcript plus a copy of the
569 **	original offending message.
570 **
571 **	Parameters:
572 **		mci -- the mailer connection information.
573 **		e -- the envelope we are working in.
574 **		separator -- any possible MIME separator.
575 **		flags -- to modify the behaviour.
576 **
577 **	Returns:
578 **		none
579 **
580 **	Side Effects:
581 **		Outputs the body of an error message.
582 */
583 
584 errbody(mci, e, separator, flags)
585 	register MCI *mci;
586 	register ENVELOPE *e;
587 	char *separator;
588 {
589 	register FILE *xfile;
590 	char *p;
591 	register ADDRESS *q;
592 	bool printheader;
593 	int pflags = flags;
594 	char buf[MAXLINE];
595 
596 	if (bitset(MCIF_INHEADER, mci->mci_flags))
597 	{
598 		putline("", mci);
599 		mci->mci_flags &= ~MCIF_INHEADER;
600 	}
601 	if (e->e_parent == NULL)
602 	{
603 		syserr("errbody: null parent");
604 		putline("   ----- Original message lost -----\n", mci);
605 		return;
606 	}
607 
608 	/*
609 	**  Output MIME header.
610 	*/
611 
612 	if (e->e_msgboundary != NULL)
613 	{
614 		putline("This is a MIME-encapsulated message", mci);
615 		putline("", mci);
616 		(void) sprintf(buf, "--%s", e->e_msgboundary);
617 		putline(buf, mci);
618 		putline("", mci);
619 	}
620 
621 	/*
622 	**  Output introductory information.
623 	*/
624 
625 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
626 		if (bitset(QBADADDR, q->q_flags))
627 			break;
628 	if (q == NULL &&
629 	    !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
630 	{
631 		putline("    **********************************************",
632 			mci);
633 		putline("    **      THIS IS A WARNING MESSAGE ONLY      **",
634 			mci);
635 		putline("    **  YOU DO NOT NEED TO RESEND YOUR MESSAGE  **",
636 			mci);
637 		putline("    **********************************************",
638 			mci);
639 		putline("", mci);
640 	}
641 	sprintf(buf, "The original message was received at %s",
642 		arpadate(ctime(&e->e_parent->e_ctime)));
643 	putline(buf, mci);
644 	expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent);
645 	putline(buf, mci);
646 	putline("", mci);
647 
648 	/*
649 	**  Output error message header (if specified and available).
650 	*/
651 
652 	if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags))
653 	{
654 		if (*ErrMsgFile == '/')
655 		{
656 			xfile = fopen(ErrMsgFile, "r");
657 			if (xfile != NULL)
658 			{
659 				while (fgets(buf, sizeof buf, xfile) != NULL)
660 				{
661 					expand(buf, buf, &buf[sizeof buf - 1], e);
662 					putline(buf, mci);
663 				}
664 				(void) fclose(xfile);
665 				putline("\n", mci);
666 			}
667 		}
668 		else
669 		{
670 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
671 			putline(buf, mci);
672 			putline("", mci);
673 		}
674 	}
675 
676 	/*
677 	**  Output message introduction
678 	*/
679 
680 	printheader = TRUE;
681 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
682 	{
683 		if (bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags))
684 		{
685 			if (printheader)
686 			{
687 				putline("   ----- The following addresses have delivery notifications -----",
688 					mci);
689 				printheader = FALSE;
690 			}
691 			strcpy(buf, q->q_paddr);
692 			if (bitset(QBADADDR, q->q_flags))
693 				strcat(buf, "  (unrecoverable error)");
694 			else if (bitset(QRELAYED, q->q_flags))
695 				strcat(buf, "  (relayed to non-DSN-aware mailer)");
696 			else if (bitset(QSENT, q->q_flags))
697 				strcat(buf, "  (successfully delivered)");
698 			else
699 				strcat(buf, "  (transient failure)");
700 			putline(buf, mci);
701 			if (q->q_alias != NULL)
702 			{
703 				strcpy(buf, "    (expanded from: ");
704 				strcat(buf, q->q_alias->q_paddr);
705 				strcat(buf, ")");
706 				putline(buf, mci);
707 			}
708 		}
709 	}
710 	if (!printheader)
711 		putline("\n", mci);
712 
713 	/*
714 	**  Output transcript of errors
715 	*/
716 
717 	(void) fflush(stdout);
718 	p = queuename(e->e_parent, 'x');
719 	if ((xfile = fopen(p, "r")) == NULL)
720 	{
721 		syserr("Cannot open %s", p);
722 		putline("   ----- Transcript of session is unavailable -----\n", mci);
723 	}
724 	else
725 	{
726 		putline("   ----- Transcript of session follows -----\n", mci);
727 		if (e->e_xfp != NULL)
728 			(void) fflush(e->e_xfp);
729 		while (fgets(buf, sizeof buf, xfile) != NULL)
730 			putline(buf, mci);
731 		(void) xfclose(xfile, "errbody xscript", p);
732 	}
733 	errno = 0;
734 
735 	/*
736 	**  Output machine-readable version.
737 	*/
738 
739 	if (e->e_msgboundary != NULL)
740 	{
741 		putline("", mci);
742 		(void) sprintf(buf, "--%s", e->e_msgboundary);
743 		putline(buf, mci);
744 		putline("Content-Type: message/delivery-status", mci);
745 		putline("", mci);
746 
747 		/*
748 		**  Output per-message information.
749 		*/
750 
751 		/* OMTS from MAIL FROM: line */
752 		if (e->e_parent->e_omts != NULL)
753 		{
754 			(void) sprintf(buf, "Original-MTS-Type: %s",
755 				e->e_parent->e_omts);
756 			putline(buf, mci);
757 		}
758 
759 		/* original envelope id from MAIL FROM: line */
760 		if (e->e_parent->e_envid != NULL)
761 		{
762 			(void) sprintf(buf, "Original-Envelope-Id: %s",
763 				e->e_parent->e_envid);
764 			putline(buf, mci);
765 		}
766 
767 		/* Final-MTS-Type: is required -- our type */
768 		putline("Final-MTS-Type: Internet", mci);
769 
770 		/* Final-MTA: seems silly -- this is in the From: line */
771 		(void) sprintf(buf, "Final-MTA: %s", MyHostName);
772 		putline(buf, mci);
773 
774 		/* Received-From: shows where we got this message from */
775 		expand("Received-From: \201_", buf, &buf[sizeof buf - 1],
776 				e->e_parent);
777 		putline(buf, mci);
778 
779 		/* Arrival-Date: -- when it arrived here */
780 		(void) sprintf(buf, "Arrival-Date: %s",
781 			arpadate(ctime(&e->e_parent->e_ctime)));
782 		putline(buf, mci);
783 
784 		/*
785 		**  Output per-address information.
786 		*/
787 
788 		for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
789 		{
790 			register ADDRESS *r;
791 
792 			if (!bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags))
793 				continue;
794 			putline("", mci);
795 
796 			/* Recipient: -- the name from the RCPT command */
797 			for (r = q; r->q_alias != NULL; r = r->q_alias)
798 				continue;
799 			(void) sprintf(buf, "Recipient: %s", r->q_paddr);
800 			putline(buf, mci);
801 
802 			/* Action: -- what happened? */
803 			if (bitset(QBADADDR, q->q_flags))
804 				putline("Action: failed", mci);
805 			else if (bitset(QQUEUEUP, q->q_flags))
806 				putline("Action: delayed", mci);
807 			else if (bitset(QRELAYED, q->q_flags))
808 				putline("Action: relayed", mci);
809 			else
810 				putline("Action: delivered", mci);
811 
812 			/* Status: -- what _really_ happened? */
813 			strcpy(buf, "Status: ");
814 			if (q->q_status != NULL)
815 				strcat(buf, q->q_status);
816 			else if (bitset(QBADADDR, q->q_flags))
817 				strcat(buf, "500");
818 			else if (bitset(QQUEUEUP, q->q_flags))
819 				strcat(buf, "400");
820 			else if (bitset(QRELAYED, q->q_flags))
821 				strcat(buf, "601");
822 			else
823 				strcat(buf, "200");
824 			putline(buf, mci);
825 
826 			/* Date: -- fine granularity */
827 			if (q->q_statdate == (time_t) 0L)
828 				q->q_statdate = curtime();
829 			(void) sprintf(buf, "Date: %s",
830 				arpadate(ctime(&q->q_statdate)));
831 			putline(buf, mci);
832 
833 			/* Final-Log-Id: -- why isn't this per-message? */
834 			(void) sprintf(buf, "Final-Log-Id: %s", e->e_id);
835 			putline(buf, mci);
836 
837 			/* Expiry-Date: -- for delayed messages only */
838 			if (bitset(QQUEUEUP, q->q_flags) &&
839 			    !bitset(QBADADDR, q->q_flags))
840 			{
841 				time_t xdate;
842 
843 				xdate = e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass];
844 				sprintf(buf, "Expiry-Date: %s",
845 					arpadate(ctime(&xdate)));
846 				putline(buf, mci);
847 			}
848 
849 			/* Original-Rcpt: -- passed from on high */
850 			if (q->q_orcpt != NULL)
851 			{
852 				(void) sprintf(buf, "Original-Rcpt: %s",
853 					q->q_orcpt);
854 				putline(buf, mci);
855 			}
856 
857 			/* Final-Rcpt: -- if through alias */
858 			if (q->q_alias != NULL)
859 			{
860 				(void) sprintf(buf, "Final-Rcpt: %s",
861 					q->q_paddr);
862 				putline(buf, mci);
863 			}
864 
865 			/* Final-Status: -- same as Status?  XXX */
866 
867 			/* Remote-MTS-Type: -- always INET?  XXX */
868 
869 			/* Remote-MTA: -- who was I talking to? */
870 			if (q->q_statmta != NULL)
871 			{
872 				(void) sprintf(buf, "Remote-MTA: %s",
873 					q->q_statmta);
874 				putline(buf, mci);
875 			}
876 
877 			/* Remote-Rcpt: -- same as Final-Rcpt?  XXX */
878 
879 			/* Remote-Status: -- same as Final-Status?  XXX */
880 		}
881 	}
882 
883 	/*
884 	**  Output text of original message
885 	*/
886 
887 	if (bitset(EF_NORETURN, e->e_parent->e_flags))
888 		SendBody = FALSE;
889 	if (!SendBody && e->e_msgboundary != NULL)
890 		pflags |= PF_DELETEMIMEHDRS;
891 	putline("", mci);
892 	if (e->e_parent->e_df != NULL)
893 	{
894 		if (e->e_msgboundary == NULL)
895 		{
896 			if (SendBody)
897 				putline("   ----- Original message follows -----\n", mci);
898 			else
899 				putline("   ----- Message header follows -----\n", mci);
900 			(void) fflush(mci->mci_out);
901 		}
902 		else
903 		{
904 			(void) sprintf(buf, "--%s", e->e_msgboundary);
905 			putline(buf, mci);
906 			putline("Content-Type: message/rfc822", mci);
907 		}
908 		putline("", mci);
909 		putheader(mci, e->e_parent->e_header, e->e_parent, pflags);
910 		if (SendBody)
911 			putbody(mci, e->e_parent, e->e_msgboundary, pflags);
912 		else
913 		{
914 			putline("", mci);
915 			putline("   ----- Message body suppressed -----", mci);
916 		}
917 	}
918 	else
919 	{
920 		putline("  ----- No message was collected -----\n", mci);
921 	}
922 
923 	if (e->e_msgboundary != NULL)
924 	{
925 		putline("", mci);
926 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
927 		putline(buf, mci);
928 	}
929 	putline("", mci);
930 
931 	/*
932 	**  Cleanup and exit
933 	*/
934 
935 	if (errno != 0)
936 		syserr("errbody: I/O error");
937 }
938 /*
939 **  PRUNEROUTE -- prune an RFC-822 source route
940 **
941 **	Trims down a source route to the last internet-registered hop.
942 **	This is encouraged by RFC 1123 section 5.3.3.
943 **
944 **	Parameters:
945 **		addr -- the address
946 **
947 **	Returns:
948 **		TRUE -- address was modified
949 **		FALSE -- address could not be pruned
950 **
951 **	Side Effects:
952 **		modifies addr in-place
953 */
954 
955 pruneroute(addr)
956 	char *addr;
957 {
958 #if NAMED_BIND
959 	char *start, *at, *comma;
960 	char c;
961 	int rcode;
962 	char hostbuf[BUFSIZ];
963 	char *mxhosts[MAXMXHOSTS + 1];
964 
965 	/* check to see if this is really a route-addr */
966 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
967 		return FALSE;
968 	start = strchr(addr, ':');
969 	at = strrchr(addr, '@');
970 	if (start == NULL || at == NULL || at < start)
971 		return FALSE;
972 
973 	/* slice off the angle brackets */
974 	strcpy(hostbuf, at + 1);
975 	hostbuf[strlen(hostbuf) - 1] = '\0';
976 
977 	while (start)
978 	{
979 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
980 		{
981 			strcpy(addr + 1, start + 1);
982 			return TRUE;
983 		}
984 		c = *start;
985 		*start = '\0';
986 		comma = strrchr(addr, ',');
987 		if (comma && comma[1] == '@')
988 			strcpy(hostbuf, comma + 2);
989 		else
990 			comma = 0;
991 		*start = c;
992 		start = comma;
993 	}
994 #endif
995 	return FALSE;
996 }
997