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.57 (Berkeley) 03/21/95";
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, sizeof buf, 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, sizeof buf, 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 			if (SafeFileEnv != NULL && SafeFileEnv[0] != '\0')
361 			{
362 				state = ESM_PANIC;
363 				break;
364 			}
365 
366 			strcpy(buf, _PATH_VARTMP);
367 			strcat(buf, "dead.letter");
368 			if (!writable(buf, NULLADDR, SFF_NOSLINK|SFF_CREAT))
369 			{
370 				state = ESM_PANIC;
371 				break;
372 			}
373 			fp = safefopen(buf, O_WRONLY|O_CREAT|O_APPEND,
374 					FileMode, SFF_NOSLINK|SFF_REGONLY);
375 			if (fp == NULL)
376 			{
377 				state = ESM_PANIC;
378 				break;
379 			}
380 
381 			bzero(&mcibuf, sizeof mcibuf);
382 			mcibuf.mci_out = fp;
383 			mcibuf.mci_mailer = FileMailer;
384 			if (bitnset(M_7BITS, FileMailer->m_flags))
385 				mcibuf.mci_flags |= MCIF_7BIT;
386 
387 			putfromline(&mcibuf, e);
388 			(*e->e_puthdr)(&mcibuf, e->e_header, e);
389 			(*e->e_putbody)(&mcibuf, e, NULL);
390 			putline("\n", &mcibuf);
391 			(void) fflush(fp);
392 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
393 			(void) xfclose(fp, "savemail", buf);
394 			break;
395 
396 		  default:
397 			syserr("554 savemail: unknown state %d", state);
398 
399 			/* fall through ... */
400 
401 		  case ESM_PANIC:
402 			/* leave the locked queue & transcript files around */
403 			loseqfile(e, "savemail panic");
404 			syserr("!554 savemail: cannot save rejected email anywhere");
405 		}
406 	}
407 }
408 /*
409 **  RETURNTOSENDER -- return a message to the sender with an error.
410 **
411 **	Parameters:
412 **		msg -- the explanatory message.
413 **		returnq -- the queue of people to send the message to.
414 **		sendbody -- if TRUE, also send back the body of the
415 **			message; otherwise just send the header.
416 **		e -- the current envelope.
417 **
418 **	Returns:
419 **		zero -- if everything went ok.
420 **		else -- some error.
421 **
422 **	Side Effects:
423 **		Returns the current message to the sender via
424 **		mail.
425 */
426 
427 #define MAXRETURNS	6	/* max depth of returning messages */
428 #define ERRORFUDGE	100	/* nominal size of error message text */
429 
430 returntosender(msg, returnq, sendbody, e)
431 	char *msg;
432 	ADDRESS *returnq;
433 	bool sendbody;
434 	register ENVELOPE *e;
435 {
436 	char buf[MAXNAME + 1];
437 	extern putheader(), errbody();
438 	register ENVELOPE *ee;
439 	ENVELOPE *oldcur = CurEnv;
440 	ENVELOPE errenvelope;
441 	static int returndepth;
442 	register ADDRESS *q;
443 	char *p;
444 
445 	if (returnq == NULL)
446 		return (-1);
447 
448 	if (msg == NULL)
449 		msg = "Unable to deliver mail";
450 
451 	if (tTd(6, 1))
452 	{
453 		printf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
454 		       msg, returndepth, e);
455 		printaddr(returnq, TRUE);
456 		if (tTd(6, 20))
457 		{
458 			printf("Sendq=");
459 			printaddr(e->e_sendqueue, TRUE);
460 		}
461 	}
462 
463 	if (++returndepth >= MAXRETURNS)
464 	{
465 		if (returndepth != MAXRETURNS)
466 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
467 		/* don't "unrecurse" and fake a clean exit */
468 		/* returndepth--; */
469 		return (0);
470 	}
471 
472 	define('g', e->e_from.q_paddr, e);
473 	define('u', NULL, e);
474 
475 	/* initialize error envelope */
476 	ee = newenvelope(&errenvelope, e);
477 	define('a', "\201b", ee);
478 	define('r', "internal", ee);
479 	define('s', "localhost", ee);
480 	define('_', "localhost", ee);
481 	ee->e_puthdr = putheader;
482 	ee->e_putbody = errbody;
483 	ee->e_flags |= EF_RESPONSE|EF_METOO;
484 	if (!bitset(EF_OLDSTYLE, e->e_flags))
485 		ee->e_flags &= ~EF_OLDSTYLE;
486 	ee->e_sendqueue = returnq;
487 	ee->e_msgsize = ERRORFUDGE;
488 	if (sendbody)
489 		ee->e_msgsize += e->e_msgsize;
490 	initsys(ee);
491 	for (q = returnq; q != NULL; q = q->q_next)
492 	{
493 		if (bitset(QBADADDR, q->q_flags))
494 			continue;
495 
496 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
497 		{
498 			register ADDRESS *p;
499 
500 			parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e);
501 			for (p = returnq; p != NULL; p = p->q_next)
502 			{
503 				if (p != q && sameaddr(p, q))
504 					q->q_flags |= QDONTSEND;
505 			}
506 		}
507 
508 		if (!bitset(QDONTSEND, q->q_flags))
509 			ee->e_nrcpts++;
510 
511 		if (q->q_alias == NULL)
512 			addheader("To", q->q_paddr, &ee->e_header);
513 	}
514 
515 # ifdef LOG
516 	if (LogLevel > 5)
517 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
518 			e->e_id, ee->e_id, msg);
519 # endif
520 
521 	if (SendMIMEErrors)
522 	{
523 		addheader("MIME-Version", "1.0", &ee->e_header);
524 		(void) sprintf(buf, "%s.%ld/%s",
525 			ee->e_id, curtime(), MyHostName);
526 		ee->e_msgboundary = newstr(buf);
527 		(void) sprintf(buf,
528 #ifdef DSN
529 			"multipart/report; report-type=X-delivery-status-1; boundary=\"%s\"",
530 #else
531 			"multipart/mixed; boundary=\"%s\"",
532 #endif
533 			ee->e_msgboundary);
534 		addheader("Content-Type", buf, &ee->e_header);
535 	}
536 	if (strncmp(msg, "Warning:", 8) == 0)
537 	{
538 		addheader("Subject", msg, ee);
539 		p = "warning-timeout";
540 	}
541 	else if (strcmp(msg, "Return receipt") == 0)
542 	{
543 		addheader("Subject", msg, ee);
544 		p = "return-receipt";
545 	}
546 	else
547 	{
548 		sprintf(buf, "Returned mail: %.*s", sizeof buf - 20, msg);
549 		addheader("Subject", buf, &ee->e_header);
550 		p = "failure";
551 	}
552 	(void) sprintf(buf, "auto-generated (%s)", p);
553 	addheader("Auto-Submitted", buf, &ee->e_header);
554 
555 	/* fake up an address header for the from person */
556 	expand("\201n", buf, sizeof buf, e);
557 	if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL)
558 	{
559 		syserr("553 Can't parse myself!");
560 		ExitStat = EX_SOFTWARE;
561 		returndepth--;
562 		return (-1);
563 	}
564 	ee->e_sender = ee->e_from.q_paddr;
565 
566 	/* push state into submessage */
567 	CurEnv = ee;
568 	define('f', "\201n", ee);
569 	define('x', "Mail Delivery Subsystem", ee);
570 	eatheader(ee, TRUE);
571 
572 	/* mark statistics */
573 	markstats(ee, NULLADDR);
574 
575 	/* actually deliver the error message */
576 	sendall(ee, SM_DEFAULT);
577 
578 	/* restore state */
579 	dropenvelope(ee);
580 	CurEnv = oldcur;
581 	returndepth--;
582 
583 	/* should check for delivery errors here */
584 	return (0);
585 }
586 /*
587 **  ERRBODY -- output the body of an error message.
588 **
589 **	Typically this is a copy of the transcript plus a copy of the
590 **	original offending message.
591 **
592 **	Parameters:
593 **		mci -- the mailer connection information.
594 **		e -- the envelope we are working in.
595 **		separator -- any possible MIME separator.
596 **		flags -- to modify the behaviour.
597 **
598 **	Returns:
599 **		none
600 **
601 **	Side Effects:
602 **		Outputs the body of an error message.
603 */
604 
605 errbody(mci, e, separator)
606 	register MCI *mci;
607 	register ENVELOPE *e;
608 	char *separator;
609 {
610 	register FILE *xfile;
611 	char *p;
612 	register ADDRESS *q;
613 	bool printheader;
614 	bool sendbody;
615 	char buf[MAXLINE];
616 	extern char *xtextify();
617 
618 	if (bitset(MCIF_INHEADER, mci->mci_flags))
619 	{
620 		putline("", mci);
621 		mci->mci_flags &= ~MCIF_INHEADER;
622 	}
623 	if (e->e_parent == NULL)
624 	{
625 		syserr("errbody: null parent");
626 		putline("   ----- Original message lost -----\n", mci);
627 		return;
628 	}
629 
630 	/*
631 	**  Output MIME header.
632 	*/
633 
634 	if (e->e_msgboundary != NULL)
635 	{
636 		putline("This is a MIME-encapsulated message", mci);
637 		putline("", mci);
638 		(void) sprintf(buf, "--%s", e->e_msgboundary);
639 		putline(buf, mci);
640 		putline("", mci);
641 	}
642 
643 	/*
644 	**  Output introductory information.
645 	*/
646 
647 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
648 		if (bitset(QBADADDR, q->q_flags))
649 			break;
650 	if (q == NULL &&
651 	    !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
652 	{
653 		putline("    **********************************************",
654 			mci);
655 		putline("    **      THIS IS A WARNING MESSAGE ONLY      **",
656 			mci);
657 		putline("    **  YOU DO NOT NEED TO RESEND YOUR MESSAGE  **",
658 			mci);
659 		putline("    **********************************************",
660 			mci);
661 		putline("", mci);
662 	}
663 	sprintf(buf, "The original message was received at %s",
664 		arpadate(ctime(&e->e_parent->e_ctime)));
665 	putline(buf, mci);
666 	expand("from \201_", buf, sizeof buf, e->e_parent);
667 	putline(buf, mci);
668 	putline("", mci);
669 
670 	/*
671 	**  Output error message header (if specified and available).
672 	*/
673 
674 	if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags))
675 	{
676 		if (*ErrMsgFile == '/')
677 		{
678 			xfile = fopen(ErrMsgFile, "r");
679 			if (xfile != NULL)
680 			{
681 				while (fgets(buf, sizeof buf, xfile) != NULL)
682 				{
683 					expand(buf, buf, sizeof buf, e);
684 					putline(buf, mci);
685 				}
686 				(void) fclose(xfile);
687 				putline("\n", mci);
688 			}
689 		}
690 		else
691 		{
692 			expand(ErrMsgFile, buf, sizeof buf, e);
693 			putline(buf, mci);
694 			putline("", mci);
695 		}
696 	}
697 
698 	/*
699 	**  Output message introduction
700 	*/
701 
702 	printheader = TRUE;
703 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
704 	{
705 		if (bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags))
706 		{
707 			if (printheader)
708 			{
709 				putline("   ----- The following addresses have delivery notifications -----",
710 					mci);
711 				printheader = FALSE;
712 			}
713 			strcpy(buf, q->q_paddr);
714 			if (bitset(QBADADDR, q->q_flags))
715 				strcat(buf, "  (unrecoverable error)");
716 			else if (bitset(QRELAYED, q->q_flags))
717 				strcat(buf, "  (relayed to non-DSN-aware mailer)");
718 			else if (bitset(QSENT, q->q_flags))
719 				strcat(buf, "  (successfully delivered)");
720 			else
721 				strcat(buf, "  (transient failure)");
722 			putline(buf, mci);
723 			if (q->q_alias != NULL)
724 			{
725 				strcpy(buf, "    (expanded from: ");
726 				strcat(buf, q->q_alias->q_paddr);
727 				strcat(buf, ")");
728 				putline(buf, mci);
729 			}
730 		}
731 	}
732 	if (!printheader)
733 		putline("\n", mci);
734 
735 	/*
736 	**  Output transcript of errors
737 	*/
738 
739 	(void) fflush(stdout);
740 	p = queuename(e->e_parent, 'x');
741 	if ((xfile = fopen(p, "r")) == NULL)
742 	{
743 		syserr("Cannot open %s", p);
744 		putline("   ----- Transcript of session is unavailable -----\n", mci);
745 	}
746 	else
747 	{
748 		putline("   ----- Transcript of session follows -----\n", mci);
749 		if (e->e_xfp != NULL)
750 			(void) fflush(e->e_xfp);
751 		while (fgets(buf, sizeof buf, xfile) != NULL)
752 			putline(buf, mci);
753 		(void) xfclose(xfile, "errbody xscript", p);
754 	}
755 	errno = 0;
756 
757 #ifdef DSN
758 	/*
759 	**  Output machine-readable version.
760 	*/
761 
762 	if (e->e_msgboundary != NULL)
763 	{
764 		putline("", mci);
765 		(void) sprintf(buf, "--%s", e->e_msgboundary);
766 		putline(buf, mci);
767 		putline("Content-Type: message/X-delivery-status-2 (Draft of 20 January 1995)", mci);
768 		putline("", mci);
769 
770 		/*
771 		**  Output per-message information.
772 		*/
773 
774 		/* original envelope id from MAIL FROM: line */
775 		if (e->e_parent->e_envid != NULL)
776 		{
777 			(void) sprintf(buf, "Original-Envelope-Id: %s",
778 				xtextify(e->e_parent->e_envid));
779 			putline(buf, mci);
780 		}
781 
782 		/* Reporting-MTA: is us (required) */
783 		p = e->e_parent->e_from.q_mailer->m_mtatype;
784 		if (p == NULL)
785 			p = "dns";
786 		(void) sprintf(buf, "Reporting-MTA: %s; %s", p, MyHostName);
787 		putline(buf, mci);
788 
789 		/* Received-From-MTA: shows where we got this message from */
790 		if (RealHostName != NULL)
791 		{
792 			/* XXX use $s for type? */
793 			p = e->e_parent->e_from.q_mailer->m_mtatype;
794 			if (p == NULL)
795 				p = "dns";
796 			(void) sprintf(buf, "Received-From-MTA: %s; %s",
797 				p, RealHostName);
798 			putline(buf, mci);
799 		}
800 
801 		/* Arrival-Date: -- when it arrived here */
802 		(void) sprintf(buf, "Arrival-Date: %s",
803 			arpadate(ctime(&e->e_parent->e_ctime)));
804 		putline(buf, mci);
805 
806 		/*
807 		**  Output per-address information.
808 		*/
809 
810 		for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
811 		{
812 			register ADDRESS *r;
813 
814 			if (!bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags))
815 				continue;
816 			putline("", mci);
817 
818 			/* Original-Recipient: -- passed from on high */
819 			if (q->q_orcpt != NULL)
820 			{
821 				(void) sprintf(buf, "Original-Recipient: %s",
822 					xtextify(q->q_orcpt));
823 				putline(buf, mci);
824 			}
825 
826 			/* Final-Recipient: -- the name from the RCPT command */
827 			p = e->e_parent->e_from.q_mailer->m_addrtype;
828 			if (p == NULL)
829 				p = "rfc822";
830 			for (r = q; r->q_alias != NULL; r = r->q_alias)
831 				continue;
832 			if (strchr(r->q_user, '@') == NULL)
833 				(void) sprintf(buf, "Final-Recipient: %s; %s@%s",
834 					p, xtextify(r->q_user), MyHostName);
835 			else
836 				(void) sprintf(buf, "Final-Recipient: %s; %s",
837 					p, xtextify(r->q_user));
838 			putline(buf, mci);
839 
840 			/* Action: -- what happened? */
841 			if (bitset(QBADADDR, q->q_flags))
842 				putline("Action: failure", mci);
843 			else if (bitset(QQUEUEUP, q->q_flags))
844 				putline("Action: delayed", mci);
845 			else if (bitset(QRELAYED, q->q_flags))
846 				putline("Action: relayed", mci);
847 			else
848 				putline("Action: delivered", mci);
849 
850 			/* Status: -- what _really_ happened? */
851 			strcpy(buf, "Status: ");
852 			if (q->q_status != NULL)
853 				strcat(buf, q->q_status);
854 			else if (bitset(QBADADDR, q->q_flags))
855 				strcat(buf, "5.0.0");
856 			else if (bitset(QQUEUEUP, q->q_flags))
857 				strcat(buf, "4.0.0");
858 			else if (bitset(QRELAYED, q->q_flags))
859 				strcat(buf, "6.0.1");
860 			else
861 				strcat(buf, "2.0.0");
862 			putline(buf, mci);
863 
864 			/* Remote-MTA: -- who was I talking to? */
865 			p = q->q_mailer->m_mtatype;
866 			if (p == NULL)
867 				p = "dns";
868 			(void) sprintf(buf, "Remote-MTA: %s; ", p);
869 			if (q->q_statmta != NULL)
870 				p = q->q_statmta;
871 			else if (q->q_host != NULL)
872 				p = q->q_host;
873 			else
874 				p = NULL;
875 			if (p != NULL)
876 			{
877 				strcat(buf, p);
878 				p = &buf[strlen(buf) - 1];
879 				if (*p == '.')
880 					*p = '\0';
881 				putline(buf, mci);
882 			}
883 
884 			/* Diagnostic-Code: -- actual result from other end */
885 			if (q->q_rstatus != NULL)
886 			{
887 				p = q->q_mailer->m_diagtype;
888 				if (p == NULL)
889 					p = "smtp";
890 				(void) sprintf(buf, "Diagnostic-Code: %s; %s",
891 					p, q->q_rstatus);
892 				putline(buf, mci);
893 			}
894 
895 			/* Last-Attempt-Date: -- fine granularity */
896 			if (q->q_statdate == (time_t) 0L)
897 				q->q_statdate = curtime();
898 			(void) sprintf(buf, "Last-Attempt-Date: %s",
899 				arpadate(ctime(&q->q_statdate)));
900 			putline(buf, mci);
901 
902 			/* Expiry-Date: -- for delayed messages only */
903 			if (bitset(QQUEUEUP, q->q_flags) &&
904 			    !bitset(QBADADDR, q->q_flags))
905 			{
906 				time_t xdate;
907 
908 				xdate = e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass];
909 				sprintf(buf, "Expiry-Date: %s",
910 					arpadate(ctime(&xdate)));
911 				putline(buf, mci);
912 			}
913 		}
914 	}
915 #endif
916 
917 	/*
918 	**  Output text of original message
919 	*/
920 
921 	putline("", mci);
922 	if (e->e_parent->e_df != NULL)
923 	{
924 		sendbody = !bitset(EF_NO_BODY_RETN, e->e_parent->e_flags);
925 
926 		if (e->e_msgboundary == NULL)
927 		{
928 			if (sendbody)
929 				putline("   ----- Original message follows -----\n", mci);
930 			else
931 				putline("   ----- Message header follows -----\n", mci);
932 			(void) fflush(mci->mci_out);
933 		}
934 		else
935 		{
936 			(void) sprintf(buf, "--%s", e->e_msgboundary);
937 			putline(buf, mci);
938 			(void) sprintf(buf, "Content-Type: message/rfc822%s",
939 				mci, sendbody ? "" : "-headers");
940 			putline(buf, mci);
941 		}
942 		putline("", mci);
943 		putheader(mci, e->e_parent->e_header, e->e_parent);
944 		if (sendbody)
945 			putbody(mci, e->e_parent, e->e_msgboundary);
946 		else if (e->e_msgboundary == NULL)
947 		{
948 			putline("", mci);
949 			putline("   ----- Message body suppressed -----", mci);
950 		}
951 	}
952 	else if (e->e_msgboundary == NULL)
953 	{
954 		putline("  ----- No message was collected -----\n", mci);
955 	}
956 
957 	if (e->e_msgboundary != NULL)
958 	{
959 		putline("", mci);
960 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
961 		putline(buf, mci);
962 	}
963 	putline("", mci);
964 
965 	/*
966 	**  Cleanup and exit
967 	*/
968 
969 	if (errno != 0)
970 		syserr("errbody: I/O error");
971 }
972 /*
973 **  SMTPTODSN -- convert SMTP to DSN status code
974 **
975 **	Parameters:
976 **		smtpstat -- the smtp status code (e.g., 550).
977 **
978 **	Returns:
979 **		The DSN version of the status code.
980 */
981 
982 char *
983 smtptodsn(smtpstat)
984 	int smtpstat;
985 {
986 	switch (smtpstat)
987 	{
988 	  case 450:	/* Req mail action not taken: mailbox unavailable */
989 		return "4.2.0";
990 
991 	  case 451:	/* Req action aborted: local error in processing */
992 		return "4.3.0";
993 
994 	  case 452:	/* Req action not taken: insufficient sys storage */
995 		return "4.3.1";
996 
997 	  case 500:	/* Syntax error, command unrecognized */
998 		return "5.5.2";
999 
1000 	  case 501:	/* Syntax error in parameters or arguments */
1001 		return "5.5.4";
1002 
1003 	  case 502:	/* Command not implemented */
1004 		return "5.5.1";
1005 
1006 	  case 503:	/* Bad sequence of commands */
1007 		return "5.5.1";
1008 
1009 	  case 504:	/* Command parameter not implemented */
1010 		return "5.5.4";
1011 
1012 	  case 550:	/* Req mail action not taken: mailbox unavailable */
1013 		return "5.2.0";
1014 
1015 	  case 551:	/* User not local; please try <...> */
1016 		return "5.1.6";
1017 
1018 	  case 552:	/* Req mail action aborted: exceeded storage alloc */
1019 		return "5.2.2";
1020 
1021 	  case 553:	/* Req action not taken: mailbox name not allowed */
1022 		return "5.1.3";
1023 
1024 	  case 554:	/* Transaction failed */
1025 		return "5.0.0";
1026 	}
1027 
1028 	if ((smtpstat / 100) == 2)
1029 		return "2.0.0";
1030 	if ((smtpstat / 100) == 4)
1031 		return "4.0.0";
1032 	return "5.0.0";
1033 }
1034 /*
1035 **  XTEXTIFY -- take regular text and turn it into DSN-style xtext
1036 **
1037 **	Parameters:
1038 **		t -- the text to convert.
1039 **
1040 **	Returns:
1041 **		The xtext-ified version of the same string.
1042 */
1043 
1044 char *
1045 xtextify(t)
1046 	register char *t;
1047 {
1048 	register char *p;
1049 	int l;
1050 	int nbogus;
1051 	static char *bp = NULL;
1052 	static int bplen = 0;
1053 
1054 	/* figure out how long this xtext will have to be */
1055 	nbogus = l = 0;
1056 	for (p = t; *p != '\0'; p++)
1057 	{
1058 		register int c = (*p & 0xff);
1059 
1060 		/* ASCII dependence here -- this is the way the spec words it */
1061 		if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(')
1062 			nbogus++;
1063 		l++;
1064 	}
1065 	if (nbogus == 0)
1066 		return t;
1067 	l += nbogus * 2 + 1;
1068 
1069 	/* now allocate space if necessary for the new string */
1070 	if (l > bplen)
1071 	{
1072 		if (bp != NULL)
1073 			free(bp);
1074 		bp = xalloc(l);
1075 		bplen = l;
1076 	}
1077 
1078 	/* ok, copy the text with byte expansion */
1079 	for (p = bp; *t != '\0'; )
1080 	{
1081 		register int c = (*t++ & 0xff);
1082 
1083 		/* ASCII dependence here -- this is the way the spec words it */
1084 		if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(')
1085 		{
1086 			*p++ = '+';
1087 			*p++ = "0123456789abcdef"[c >> 4];
1088 			*p++ = "0123456789abcdef"[c & 0xf];
1089 		}
1090 		else
1091 			*p++ = c;
1092 	}
1093 	*p = '\0';
1094 	return bp;
1095 }
1096 /*
1097 **  PRUNEROUTE -- prune an RFC-822 source route
1098 **
1099 **	Trims down a source route to the last internet-registered hop.
1100 **	This is encouraged by RFC 1123 section 5.3.3.
1101 **
1102 **	Parameters:
1103 **		addr -- the address
1104 **
1105 **	Returns:
1106 **		TRUE -- address was modified
1107 **		FALSE -- address could not be pruned
1108 **
1109 **	Side Effects:
1110 **		modifies addr in-place
1111 */
1112 
1113 pruneroute(addr)
1114 	char *addr;
1115 {
1116 #if NAMED_BIND
1117 	char *start, *at, *comma;
1118 	char c;
1119 	int rcode;
1120 	char hostbuf[BUFSIZ];
1121 	char *mxhosts[MAXMXHOSTS + 1];
1122 
1123 	/* check to see if this is really a route-addr */
1124 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
1125 		return FALSE;
1126 	start = strchr(addr, ':');
1127 	at = strrchr(addr, '@');
1128 	if (start == NULL || at == NULL || at < start)
1129 		return FALSE;
1130 
1131 	/* slice off the angle brackets */
1132 	strcpy(hostbuf, at + 1);
1133 	hostbuf[strlen(hostbuf) - 1] = '\0';
1134 
1135 	while (start)
1136 	{
1137 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
1138 		{
1139 			strcpy(addr + 1, start + 1);
1140 			return TRUE;
1141 		}
1142 		c = *start;
1143 		*start = '\0';
1144 		comma = strrchr(addr, ',');
1145 		if (comma && comma[1] == '@')
1146 			strcpy(hostbuf, comma + 2);
1147 		else
1148 			comma = 0;
1149 		*start = c;
1150 		start = comma;
1151 	}
1152 #endif
1153 	return FALSE;
1154 }
1155