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.42 (Berkeley) 11/22/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("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
447 		       msg, returndepth, e);
448 		printaddr(returnq, TRUE);
449 	}
450 
451 	if (++returndepth >= MAXRETURNS)
452 	{
453 		if (returndepth != MAXRETURNS)
454 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
455 		/* don't "unrecurse" and fake a clean exit */
456 		/* returndepth--; */
457 		return (0);
458 	}
459 
460 	SendBody = sendbody;
461 	define('g', e->e_from.q_paddr, e);
462 	define('u', NULL, e);
463 
464 	/* initialize error envelope */
465 	ee = newenvelope(&errenvelope, e);
466 	define('a', "\201b", ee);
467 	define('r', "internal", ee);
468 	define('s', "localhost", ee);
469 	define('_', "localhost", ee);
470 	ee->e_puthdr = putheader;
471 	ee->e_putbody = errbody;
472 	ee->e_flags |= EF_RESPONSE|EF_METOO;
473 	if (!bitset(EF_OLDSTYLE, e->e_flags))
474 		ee->e_flags &= ~EF_OLDSTYLE;
475 	ee->e_sendqueue = returnq;
476 	ee->e_msgsize = ERRORFUDGE;
477 	if (!bitset(EF_NORETURN, e->e_flags))
478 		ee->e_msgsize += e->e_msgsize;
479 	initsys(ee);
480 	for (q = returnq; q != NULL; q = q->q_next)
481 	{
482 		if (bitset(QBADADDR, q->q_flags))
483 			continue;
484 
485 		if (!bitset(QDONTSEND, q->q_flags))
486 			ee->e_nrcpts++;
487 
488 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
489 			parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e);
490 
491 		if (q->q_alias == NULL)
492 			addheader("To", q->q_paddr, &ee->e_header);
493 	}
494 
495 # ifdef LOG
496 	if (LogLevel > 5)
497 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
498 			e->e_id, ee->e_id, msg);
499 # endif
500 
501 	if (strncmp(msg, "Warning:", 8) == 0)
502 	{
503 		addheader("Subject", msg, ee);
504 		addheader("Precedence", "autoreply warning-timeout", &ee->e_header);
505 	}
506 	else if (strcmp(msg, "Return receipt") == 0)
507 	{
508 		addheader("Subject", msg, ee);
509 		addheader("Precedence", "autoreply return-receipt", &ee->e_header);
510 	}
511 	else
512 	{
513 		sprintf(buf, "Returned mail: %.*s", sizeof buf - 20, msg);
514 		addheader("Subject", buf, &ee->e_header);
515 		addheader("Precedence", "autoreply failure-message", &ee->e_header);
516 	}
517 	if (SendMIMEErrors)
518 	{
519 		addheader("MIME-Version", "1.0", &ee->e_header);
520 		(void) sprintf(buf, "%s.%ld/%s",
521 			ee->e_id, curtime(), MyHostName);
522 		ee->e_msgboundary = newstr(buf);
523 		(void) sprintf(buf,
524 			"multipart/report; report-type=delivery-status; boundary=\"%s\"",
525 			ee->e_msgboundary);
526 		addheader("Content-Type", buf, &ee->e_header);
527 	}
528 
529 	/* fake up an address header for the from person */
530 	expand("\201n", buf, &buf[sizeof buf - 1], e);
531 	if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL)
532 	{
533 		syserr("553 Can't parse myself!");
534 		ExitStat = EX_SOFTWARE;
535 		returndepth--;
536 		return (-1);
537 	}
538 	ee->e_sender = ee->e_from.q_paddr;
539 
540 	/* push state into submessage */
541 	CurEnv = ee;
542 	define('f', "\201n", ee);
543 	define('x', "Mail Delivery Subsystem", ee);
544 	eatheader(ee, TRUE);
545 
546 	/* mark statistics */
547 	markstats(ee, NULLADDR);
548 
549 	/* actually deliver the error message */
550 	sendall(ee, SM_DEFAULT);
551 
552 	/* restore state */
553 	dropenvelope(ee);
554 	CurEnv = oldcur;
555 	returndepth--;
556 
557 	/* should check for delivery errors here */
558 	return (0);
559 }
560 /*
561 **  ERRBODY -- output the body of an error message.
562 **
563 **	Typically this is a copy of the transcript plus a copy of the
564 **	original offending message.
565 **
566 **	Parameters:
567 **		mci -- the mailer connection information.
568 **		e -- the envelope we are working in.
569 **		separator -- any possible MIME separator.
570 **		flags -- to modify the behaviour.
571 **
572 **	Returns:
573 **		none
574 **
575 **	Side Effects:
576 **		Outputs the body of an error message.
577 */
578 
579 errbody(mci, e, separator, flags)
580 	register MCI *mci;
581 	register ENVELOPE *e;
582 	char *separator;
583 {
584 	register FILE *xfile;
585 	char *p;
586 	register ADDRESS *q;
587 	bool printheader;
588 	int pflags = flags;
589 	char buf[MAXLINE];
590 
591 	if (bitset(MCIF_INHEADER, mci->mci_flags))
592 	{
593 		putline("", mci);
594 		mci->mci_flags &= ~MCIF_INHEADER;
595 	}
596 	if (e->e_parent == NULL)
597 	{
598 		syserr("errbody: null parent");
599 		putline("   ----- Original message lost -----\n", mci);
600 		return;
601 	}
602 
603 	/*
604 	**  Output MIME header.
605 	*/
606 
607 	if (e->e_msgboundary != NULL)
608 	{
609 		putline("This is a MIME-encapsulated message", mci);
610 		putline("", mci);
611 		(void) sprintf(buf, "--%s", e->e_msgboundary);
612 		putline(buf, mci);
613 		putline("", mci);
614 	}
615 
616 	/*
617 	**  Output introductory information.
618 	*/
619 
620 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
621 		if (bitset(QBADADDR, q->q_flags))
622 			break;
623 	if (q == NULL &&
624 	    !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
625 	{
626 		putline("    **********************************************",
627 			mci);
628 		putline("    **      THIS IS A WARNING MESSAGE ONLY      **",
629 			mci);
630 		putline("    **  YOU DO NOT NEED TO RESEND YOUR MESSAGE  **",
631 			mci);
632 		putline("    **********************************************",
633 			mci);
634 		putline("", mci);
635 	}
636 	sprintf(buf, "The original message was received at %s",
637 		arpadate(ctime(&e->e_parent->e_ctime)));
638 	putline(buf, mci);
639 	expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent);
640 	putline(buf, mci);
641 	putline("", mci);
642 
643 	/*
644 	**  Output error message header (if specified and available).
645 	*/
646 
647 	if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags))
648 	{
649 		if (*ErrMsgFile == '/')
650 		{
651 			xfile = fopen(ErrMsgFile, "r");
652 			if (xfile != NULL)
653 			{
654 				while (fgets(buf, sizeof buf, xfile) != NULL)
655 				{
656 					expand(buf, buf, &buf[sizeof buf - 1], e);
657 					putline(buf, mci);
658 				}
659 				(void) fclose(xfile);
660 				putline("\n", mci);
661 			}
662 		}
663 		else
664 		{
665 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
666 			putline(buf, mci);
667 			putline("", mci);
668 		}
669 	}
670 
671 	/*
672 	**  Output message introduction
673 	*/
674 
675 	printheader = TRUE;
676 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
677 	{
678 		if (bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags))
679 		{
680 			if (printheader)
681 			{
682 				putline("   ----- The following addresses have delivery notifications -----",
683 					mci);
684 				printheader = FALSE;
685 			}
686 			strcpy(buf, q->q_paddr);
687 			if (bitset(QBADADDR, q->q_flags))
688 				strcat(buf, "  (unrecoverable error)");
689 			else if (bitset(QRELAYED, q->q_flags))
690 				strcat(buf, "  (relayed to non-DSN-aware mailer)");
691 			else if (bitset(QSENT, q->q_flags))
692 				strcat(buf, "  (successfully delivered)");
693 			else
694 				strcat(buf, "  (transient failure)");
695 			putline(buf, mci);
696 			if (q->q_alias != NULL)
697 			{
698 				strcpy(buf, "    (expanded from: ");
699 				strcat(buf, q->q_alias->q_paddr);
700 				strcat(buf, ")");
701 				putline(buf, mci);
702 			}
703 		}
704 	}
705 	if (!printheader)
706 		putline("\n", mci);
707 
708 	/*
709 	**  Output transcript of errors
710 	*/
711 
712 	(void) fflush(stdout);
713 	p = queuename(e->e_parent, 'x');
714 	if ((xfile = fopen(p, "r")) == NULL)
715 	{
716 		syserr("Cannot open %s", p);
717 		putline("   ----- Transcript of session is unavailable -----\n", mci);
718 	}
719 	else
720 	{
721 		putline("   ----- Transcript of session follows -----\n", mci);
722 		if (e->e_xfp != NULL)
723 			(void) fflush(e->e_xfp);
724 		while (fgets(buf, sizeof buf, xfile) != NULL)
725 			putline(buf, mci);
726 		(void) xfclose(xfile, "errbody xscript", p);
727 	}
728 	errno = 0;
729 
730 	/*
731 	**  Output machine-readable version.
732 	*/
733 
734 	if (e->e_msgboundary != NULL)
735 	{
736 		putline("", mci);
737 		(void) sprintf(buf, "--%s", e->e_msgboundary);
738 		putline(buf, mci);
739 		putline("Content-Type: message/delivery-status", mci);
740 		putline("", mci);
741 
742 		/*
743 		**  Output per-message information.
744 		*/
745 
746 		/* OMTS from MAIL FROM: line */
747 		if (e->e_parent->e_omts != NULL)
748 		{
749 			(void) sprintf(buf, "Original-MTS-Type: %s",
750 				e->e_parent->e_omts);
751 			putline(buf, mci);
752 		}
753 
754 		/* original envelope id from MAIL FROM: line */
755 		if (e->e_parent->e_envid != NULL)
756 		{
757 			(void) sprintf(buf, "Original-Envelope-Id: %s",
758 				e->e_parent->e_envid);
759 			putline(buf, mci);
760 		}
761 
762 		/* Final-MTS-Type: is required -- our type */
763 		putline("Final-MTS-Type: Internet", mci);
764 
765 		/* Final-MTA: seems silly -- this is in the From: line */
766 		(void) sprintf(buf, "Final-MTA: %s", MyHostName);
767 		putline(buf, mci);
768 
769 		/* Received-From: shows where we got this message from */
770 		expand("Received-From: \201_", buf, &buf[sizeof buf - 1],
771 				e->e_parent);
772 		putline(buf, mci);
773 
774 		/* Arrival-Date: -- when it arrived here */
775 		(void) sprintf(buf, "Arrival-Date: %s",
776 			arpadate(ctime(&e->e_parent->e_ctime)));
777 		putline(buf, mci);
778 
779 		/*
780 		**  Output per-address information.
781 		*/
782 
783 		for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
784 		{
785 			register ADDRESS *r;
786 
787 			if (!bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags))
788 				continue;
789 			putline("", mci);
790 
791 			/* Recipient: -- the name from the RCPT command */
792 			for (r = q; r->q_alias != NULL; r = r->q_alias)
793 				continue;
794 			(void) sprintf(buf, "Recipient: %s", r->q_paddr);
795 			putline(buf, mci);
796 
797 			/* Action: -- what happened? */
798 			if (bitset(QBADADDR, q->q_flags))
799 				putline("Action: failed", mci);
800 			else if (bitset(QQUEUEUP, q->q_flags))
801 				putline("Action: delayed", mci);
802 			else if (bitset(QRELAYED, q->q_flags))
803 				putline("Action: relayed", mci);
804 			else
805 				putline("Action: delivered", mci);
806 
807 			/* Status: -- what _really_ happened? */
808 			strcpy(buf, "Status: ");
809 			if (q->q_status != NULL)
810 				strcat(buf, q->q_status);
811 			else if (bitset(QBADADDR, q->q_flags))
812 				strcat(buf, "500");
813 			else if (bitset(QQUEUEUP, q->q_flags))
814 				strcat(buf, "400");
815 			else if (bitset(QRELAYED, q->q_flags))
816 				strcat(buf, "601");
817 			else
818 				strcat(buf, "200");
819 			putline(buf, mci);
820 
821 			/* Date: -- fine granularity */
822 			if (q->q_statdate == (time_t) 0L)
823 				q->q_statdate = curtime();
824 			(void) sprintf(buf, "Date: %s",
825 				arpadate(ctime(&q->q_statdate)));
826 			putline(buf, mci);
827 
828 			/* Final-Log-Id: -- why isn't this per-message? */
829 			(void) sprintf(buf, "Final-Log-Id: %s", e->e_id);
830 			putline(buf, mci);
831 
832 			/* Expiry-Date: -- for delayed messages only */
833 			if (bitset(QQUEUEUP, q->q_flags) &&
834 			    !bitset(QBADADDR, q->q_flags))
835 			{
836 				time_t xdate;
837 
838 				xdate = e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass];
839 				sprintf(buf, "Expiry-Date: %s",
840 					arpadate(ctime(&xdate)));
841 				putline(buf, mci);
842 			}
843 
844 			/* Original-Rcpt: -- passed from on high */
845 			if (q->q_orcpt != NULL)
846 			{
847 				(void) sprintf(buf, "Original-Rcpt: %s",
848 					q->q_orcpt);
849 				putline(buf, mci);
850 			}
851 
852 			/* Final-Rcpt: -- if through alias */
853 			if (q->q_alias != NULL)
854 			{
855 				(void) sprintf(buf, "Final-Rcpt: %s",
856 					q->q_paddr);
857 				putline(buf, mci);
858 			}
859 
860 			/* Final-Status: -- same as Status?  XXX */
861 
862 			/* Remote-MTS-Type: -- always INET?  XXX */
863 
864 			/* Remote-MTA: -- who was I talking to? */
865 			if (q->q_statmta != NULL)
866 			{
867 				(void) sprintf(buf, "Remote-MTA: %s",
868 					q->q_statmta);
869 				putline(buf, mci);
870 			}
871 
872 			/* Remote-Rcpt: -- same as Final-Rcpt?  XXX */
873 
874 			/* Remote-Status: -- same as Final-Status?  XXX */
875 		}
876 	}
877 
878 	/*
879 	**  Output text of original message
880 	*/
881 
882 	if (bitset(EF_NORETURN, e->e_parent->e_flags))
883 		SendBody = FALSE;
884 	if (!SendBody && e->e_msgboundary != NULL)
885 		pflags |= PF_DELETEMIMEHDRS;
886 	putline("", mci);
887 	if (e->e_parent->e_df != NULL)
888 	{
889 		if (e->e_msgboundary == NULL)
890 		{
891 			if (SendBody)
892 				putline("   ----- Original message follows -----\n", mci);
893 			else
894 				putline("   ----- Message header follows -----\n", mci);
895 			(void) fflush(mci->mci_out);
896 		}
897 		else
898 		{
899 			(void) sprintf(buf, "--%s", e->e_msgboundary);
900 			putline(buf, mci);
901 			putline("Content-Type: message/rfc822", mci);
902 		}
903 		putline("", mci);
904 		putheader(mci, e->e_parent->e_header, e->e_parent, pflags);
905 		if (SendBody)
906 			putbody(mci, e->e_parent, e->e_msgboundary, pflags);
907 		else
908 		{
909 			putline("", mci);
910 			putline("   ----- Message body suppressed -----", mci);
911 		}
912 	}
913 	else
914 	{
915 		putline("  ----- No message was collected -----\n", mci);
916 	}
917 
918 	if (e->e_msgboundary != NULL)
919 	{
920 		putline("", mci);
921 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
922 		putline(buf, mci);
923 	}
924 	putline("", mci);
925 
926 	/*
927 	**  Cleanup and exit
928 	*/
929 
930 	if (errno != 0)
931 		syserr("errbody: I/O error");
932 }
933 /*
934 **  PRUNEROUTE -- prune an RFC-822 source route
935 **
936 **	Trims down a source route to the last internet-registered hop.
937 **	This is encouraged by RFC 1123 section 5.3.3.
938 **
939 **	Parameters:
940 **		addr -- the address
941 **
942 **	Returns:
943 **		TRUE -- address was modified
944 **		FALSE -- address could not be pruned
945 **
946 **	Side Effects:
947 **		modifies addr in-place
948 */
949 
950 pruneroute(addr)
951 	char *addr;
952 {
953 #if NAMED_BIND
954 	char *start, *at, *comma;
955 	char c;
956 	int rcode;
957 	char hostbuf[BUFSIZ];
958 	char *mxhosts[MAXMXHOSTS + 1];
959 
960 	/* check to see if this is really a route-addr */
961 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
962 		return FALSE;
963 	start = strchr(addr, ':');
964 	at = strrchr(addr, '@');
965 	if (start == NULL || at == NULL || at < start)
966 		return FALSE;
967 
968 	/* slice off the angle brackets */
969 	strcpy(hostbuf, at + 1);
970 	hostbuf[strlen(hostbuf) - 1] = '\0';
971 
972 	while (start)
973 	{
974 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
975 		{
976 			strcpy(addr + 1, start + 1);
977 			return TRUE;
978 		}
979 		c = *start;
980 		*start = '\0';
981 		comma = strrchr(addr, ',');
982 		if (comma && comma[1] == '@')
983 			strcpy(hostbuf, comma + 2);
984 		else
985 			comma = 0;
986 		*start = c;
987 		start = comma;
988 	}
989 #endif
990 	return FALSE;
991 }
992