2 * Copyright (c) 1998-2003 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: savemail.c,v 8.304 2004/10/06 21:36:06 ca Exp $")
18 static void errbody
__P((MCI
*, ENVELOPE
*, char *));
19 static bool pruneroute
__P((char *));
22 ** SAVEMAIL -- Save mail on error
24 ** If mailing back errors, mail it back to the originator
25 ** together with an error message; otherwise, just put it in
26 ** dead.letter in the user's home directory (if he exists on
30 ** e -- the envelope containing the message in error.
31 ** sendbody -- if true, also send back the body of the
32 ** message; otherwise just send the header.
35 ** true if savemail panic'ed, (i.e., the data file should
36 ** be preserved by dropenvelope())
39 ** Saves the letter, by writing or mailing it back to the
40 ** sender, or by putting it in dead.letter in her home
44 /* defines for state machine */
45 #define ESM_REPORT 0 /* report to sender's terminal */
46 #define ESM_MAIL 1 /* mail back to sender */
47 #define ESM_QUIET 2 /* mail has already been returned */
48 #define ESM_DEADLETTER 3 /* save in ~/dead.letter */
49 #define ESM_POSTMASTER 4 /* return to postmaster */
50 #define ESM_DEADLETTERDROP 5 /* save in DeadLetterDrop */
51 #define ESM_PANIC 6 /* call loseqfile() */
52 #define ESM_DONE 7 /* message is successfully delivered */
59 register SM_FILE_T
*fp
;
62 auto ADDRESS
*q
= NULL
;
67 char buf
[MAXLINE
+ 1];
68 char dlbuf
[MAXPATHLEN
];
74 sm_dprintf("\nsavemail, errormode = %c, id = %s, ExitStat = %d\n e_from=",
75 e
->e_errormode
, e
->e_id
== NULL
? "NONE" : e
->e_id
,
77 printaddr(sm_debug_file(), &e
->e_from
, false);
82 /* can't return a message with no id */
87 ** In the unhappy event we don't know who to return the mail
88 ** to, make someone up.
91 if (e
->e_from
.q_paddr
== NULL
)
93 e
->e_sender
= "Postmaster";
94 if (parseaddr(e
->e_sender
, &e
->e_from
,
95 RF_COPYPARSE
|RF_SENDERADDR
,
96 '\0', NULL
, e
, false) == NULL
)
98 syserr("553 5.3.5 Cannot parse Postmaster!");
99 finis(true, true, EX_SOFTWARE
);
105 ** Basic state machine.
107 ** This machine runs through the following states:
109 ** ESM_QUIET Errors have already been printed iff the
111 ** ESM_REPORT Report directly to the sender's terminal.
112 ** ESM_MAIL Mail response to the sender.
113 ** ESM_DEADLETTER Save response in ~/dead.letter.
114 ** ESM_POSTMASTER Mail response to the postmaster.
115 ** ESM_DEADLETTERDROP
116 ** If DeadLetterDrop set, save it there.
117 ** ESM_PANIC Save response anywhere possible.
120 /* determine starting state */
121 switch (e
->e_errormode
)
138 /* no need to return anything at all */
142 syserr("554 5.3.0 savemail: bogus errormode x%x",
148 /* if this is already an error response, send to postmaster */
149 if (bitset(EF_RESPONSE
, e
->e_flags
))
151 if (e
->e_parent
!= NULL
&&
152 bitset(EF_RESPONSE
, e
->e_parent
->e_flags
))
154 /* got an error sending a response -- can it */
157 state
= ESM_POSTMASTER
;
160 while (state
!= ESM_DONE
)
163 sm_dprintf(" state %d\n", state
);
168 if (bitnset(M_LOCALMAILER
, e
->e_from
.q_mailer
->m_flags
))
169 state
= ESM_DEADLETTER
;
177 ** If the user is still logged in on the same terminal,
178 ** then write the error messages back to hir (sic).
183 #else /* USE_TTYPATH */
185 #endif /* USE_TTYPATH */
187 if (p
== NULL
|| sm_io_reopen(SmFtStdio
,
189 p
, SM_IO_WRONLY
, NULL
,
196 expand("\201n", buf
, sizeof buf
, e
);
197 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
198 "\r\nMessage from %s...\r\n", buf
);
199 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
200 "Errors occurred while sending mail.\r\n");
201 if (e
->e_xfp
!= NULL
)
203 (void) bfrewind(e
->e_xfp
);
204 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
205 "Transcript follows:\r\n");
206 while (sm_io_fgets(e
->e_xfp
, SM_TIME_DEFAULT
,
207 buf
, sizeof buf
) != NULL
&&
208 !sm_io_error(smioout
))
209 (void) sm_io_fputs(smioout
,
215 syserr("Cannot open %s",
216 queuename(e
, XSCRPT_LETTER
));
217 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
218 "Transcript of session is unavailable.\r\n");
220 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
221 "Original message will be saved in dead.letter.\r\n");
222 state
= ESM_DEADLETTER
;
227 ** If mailing back, do it.
228 ** Throw away all further output. Don't alias,
229 ** since this could cause loops, e.g., if joe
230 ** mails to joe@x, and for some reason the network
231 ** for @x is down, then the response gets sent to
232 ** joe@x, which gives a response, etc. Also force
233 ** the mail to be delivered even if a version of
234 ** it has already been sent to the sender.
236 ** If this is a configuration or local software
237 ** error, send to the local postmaster as well,
238 ** since the originator can't do anything
239 ** about it anyway. Note that this is a full
240 ** copy of the message (intentionally) so that
241 ** the Postmaster can forward things along.
244 if (ExitStat
== EX_CONFIG
|| ExitStat
== EX_SOFTWARE
)
246 (void) sendtolist("postmaster", NULLADDR
,
247 &e
->e_errorqueue
, 0, e
);
249 if (!emptyaddr(&e
->e_from
))
251 char from
[TOBUFSIZE
];
253 if (sm_strlcpy(from
, e
->e_from
.q_paddr
,
254 sizeof from
) >= sizeof from
)
256 state
= ESM_POSTMASTER
;
260 if (!DontPruneRoutes
)
261 (void) pruneroute(from
);
263 (void) sendtolist(from
, NULLADDR
,
264 &e
->e_errorqueue
, 0, e
);
268 ** Deliver a non-delivery report to the
269 ** Postmaster-designate (not necessarily
270 ** Postmaster). This does not include the
271 ** body of the message, for privacy reasons.
272 ** You really shouldn't need this.
275 e
->e_flags
|= EF_PM_NOTIFY
;
277 /* check to see if there are any good addresses */
278 for (q
= e
->e_errorqueue
; q
!= NULL
; q
= q
->q_next
)
280 if (QS_IS_SENDABLE(q
->q_state
))
285 /* this is an error-error */
286 state
= ESM_POSTMASTER
;
289 if (returntosender(e
->e_message
, e
->e_errorqueue
,
290 sendbody
? RTSF_SEND_BODY
298 /* didn't work -- return to postmaster */
299 state
= ESM_POSTMASTER
;
304 ** Similar to previous case, but to system postmaster.
308 expand(DoubleBounceAddr
, buf
, sizeof buf
, e
);
311 ** Just drop it on the floor if DoubleBounceAddr
312 ** expands to an empty string.
320 if (sendtolist(buf
, NULLADDR
, &q
, 0, e
) <= 0)
322 syserr("553 5.3.0 cannot parse %s!", buf
);
323 ExitStat
= EX_SOFTWARE
;
324 state
= ESM_DEADLETTERDROP
;
327 flags
= RTSF_PM_BOUNCE
;
329 flags
|= RTSF_SEND_BODY
;
330 if (returntosender(e
->e_message
, q
, flags
, e
) == 0)
336 /* didn't work -- last resort */
337 state
= ESM_DEADLETTERDROP
;
342 ** Save the message in dead.letter.
343 ** If we weren't mailing back, and the user is
344 ** local, we should save the message in
345 ** ~/dead.letter so that the poor person doesn't
346 ** have to type it over again -- and we all know
347 ** what poor typists UNIX users are.
351 if (bitnset(M_HASPWENT
, e
->e_from
.q_mailer
->m_flags
))
353 if (e
->e_from
.q_home
!= NULL
)
354 p
= e
->e_from
.q_home
;
355 else if (sm_mbdb_lookup(e
->e_from
.q_user
, &user
)
357 *user
.mbdb_homedir
!= '\0')
358 p
= user
.mbdb_homedir
;
360 if (p
== NULL
|| e
->e_dfp
== NULL
)
362 /* no local directory or no data file */
367 /* we have a home directory; write dead.letter */
368 macdefine(&e
->e_macro
, A_TEMP
, 'z', p
);
370 /* get the sender for the UnixFromLine */
371 p
= macvalue('g', e
);
372 macdefine(&e
->e_macro
, A_PERM
, 'g', e
->e_sender
);
374 expand("\201z/dead.letter", dlbuf
, sizeof dlbuf
, e
);
375 sff
= SFF_CREAT
|SFF_REGONLY
|SFF_RUNASREALUID
;
379 if (writable(dlbuf
, NULL
, sff
) &&
380 mailfile(dlbuf
, FileMailer
, NULL
, sff
, e
) == EX_OK
)
382 int oldverb
= Verbose
;
384 if (OpMode
!= MD_DAEMON
&& OpMode
!= MD_SMTP
)
387 message("Saved message in %s", dlbuf
);
389 macdefine(&e
->e_macro
, A_PERM
, 'g', p
);
393 macdefine(&e
->e_macro
, A_PERM
, 'g', p
);
397 case ESM_DEADLETTERDROP
:
399 ** Log the mail in DeadLetterDrop file.
408 if ((SafeFileEnv
!= NULL
&& SafeFileEnv
[0] != '\0') ||
409 DeadLetterDrop
== NULL
||
410 DeadLetterDrop
[0] == '\0')
416 sff
= SFF_CREAT
|SFF_REGONLY
|SFF_ROOTOK
|SFF_OPENASROOT
|SFF_MUSTOWN
;
417 if (!writable(DeadLetterDrop
, NULL
, sff
) ||
418 (fp
= safefopen(DeadLetterDrop
, O_WRONLY
|O_APPEND
,
419 FileMode
, sff
)) == NULL
)
425 memset(&mcibuf
, '\0', sizeof mcibuf
);
427 mcibuf
.mci_mailer
= FileMailer
;
428 if (bitnset(M_7BITS
, FileMailer
->m_flags
))
429 mcibuf
.mci_flags
|= MCIF_7BIT
;
431 /* get the sender for the UnixFromLine */
432 p
= macvalue('g', e
);
433 macdefine(&e
->e_macro
, A_PERM
, 'g', e
->e_sender
);
435 putfromline(&mcibuf
, e
);
436 (*e
->e_puthdr
)(&mcibuf
, e
->e_header
, e
, M87F_OUTER
);
437 (*e
->e_putbody
)(&mcibuf
, e
, NULL
);
438 putline("\n", &mcibuf
); /* XXX EOL from FileMailer? */
439 (void) sm_io_flush(fp
, SM_TIME_DEFAULT
);
440 if (sm_io_error(fp
) ||
441 sm_io_close(fp
, SM_TIME_DEFAULT
) < 0)
445 int oldverb
= Verbose
;
447 if (OpMode
!= MD_DAEMON
&& OpMode
!= MD_SMTP
)
450 message("Saved message in %s",
454 sm_syslog(LOG_NOTICE
, e
->e_id
,
455 "Saved message in %s",
459 macdefine(&e
->e_macro
, A_PERM
, 'g', p
);
463 syserr("554 5.3.5 savemail: unknown state %d", state
);
467 /* leave the locked queue & transcript files around */
468 loseqfile(e
, "savemail panic");
471 syserr("554 savemail: cannot save rejected email anywhere");
479 ** RETURNTOSENDER -- return a message to the sender with an error.
482 ** msg -- the explanatory message.
483 ** returnq -- the queue of people to send the message to.
484 ** flags -- flags tweaking the operation:
485 ** RTSF_SENDBODY -- include body of message (otherwise
486 ** just send the header).
487 ** RTSF_PMBOUNCE -- this is a postmaster bounce.
488 ** e -- the current envelope.
491 ** zero -- if everything went ok.
492 ** else -- some error.
495 ** Returns the current message to the sender via mail.
498 #define MAXRETURNS 6 /* max depth of returning messages */
499 #define ERRORFUDGE 1024 /* nominal size of error message text */
502 returntosender(msg
, returnq
, flags
, e
)
506 register ENVELOPE
*e
;
508 register ENVELOPE
*ee
;
509 ENVELOPE
*oldcur
= CurEnv
;
510 ENVELOPE errenvelope
;
511 static int returndepth
= 0;
514 char buf
[MAXNAME
+ 1];
520 msg
= "Unable to deliver mail";
524 sm_dprintf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%p, returnq=",
525 msg
, returndepth
, e
);
526 printaddr(sm_debug_file(), returnq
, true);
529 sm_dprintf("Sendq=");
530 printaddr(sm_debug_file(), e
->e_sendqueue
, true);
534 if (++returndepth
>= MAXRETURNS
)
536 if (returndepth
!= MAXRETURNS
)
537 syserr("554 5.3.0 returntosender: infinite recursion on %s",
539 /* don't "unrecurse" and fake a clean exit */
544 macdefine(&e
->e_macro
, A_PERM
, 'g', e
->e_sender
);
545 macdefine(&e
->e_macro
, A_PERM
, 'u', NULL
);
547 /* initialize error envelope */
548 ee
= newenvelope(&errenvelope
, e
, sm_rpool_new_x(NULL
));
549 macdefine(&ee
->e_macro
, A_PERM
, 'a', "\201b");
550 macdefine(&ee
->e_macro
, A_PERM
, 'r', "");
551 macdefine(&ee
->e_macro
, A_PERM
, 's', "localhost");
552 macdefine(&ee
->e_macro
, A_PERM
, '_', "localhost");
555 ee
->e_puthdr
= putheader
;
556 ee
->e_putbody
= errbody
;
557 ee
->e_flags
|= EF_RESPONSE
|EF_METOO
;
558 if (!bitset(EF_OLDSTYLE
, e
->e_flags
))
559 ee
->e_flags
&= ~EF_OLDSTYLE
;
560 if (bitset(EF_DONT_MIME
, e
->e_flags
))
562 ee
->e_flags
|= EF_DONT_MIME
;
565 ** If we can't convert to MIME and we don't pass
566 ** 8-bit, we can't send the body.
569 if (bitset(EF_HAS8BIT
, e
->e_flags
) &&
570 !bitset(MM_PASS8BIT
, MimeMode
))
571 flags
&= ~RTSF_SEND_BODY
;
574 ee
->e_sendqueue
= returnq
;
576 if (bitset(RTSF_SEND_BODY
, flags
) &&
577 !bitset(PRIV_NOBODYRETN
, PrivacyFlags
))
578 ee
->e_msgsize
= ERRORFUDGE
+ e
->e_msgsize
;
580 ee
->e_flags
|= EF_NO_BODY_RETN
;
582 if (!setnewqueue(ee
))
584 syserr("554 5.3.0 returntosender: cannot select queue for %s",
586 ExitStat
= EX_UNAVAILABLE
;
593 _res
.retry
= TimeOuts
.res_retry
[RES_TO_FIRST
];
594 _res
.retrans
= TimeOuts
.res_retrans
[RES_TO_FIRST
];
595 #endif /* NAMED_BIND */
596 for (q
= returnq
; q
!= NULL
; q
= q
->q_next
)
598 if (QS_IS_BADADDR(q
->q_state
))
601 q
->q_flags
&= ~(QHASNOTIFY
|Q_PINGFLAGS
);
602 q
->q_flags
|= QPINGONFAILURE
;
604 if (!QS_IS_DEAD(q
->q_state
))
607 if (q
->q_alias
== NULL
)
608 addheader("To", q
->q_paddr
, 0, ee
);
613 if (bitset(EF_RESPONSE
, e
->e_flags
))
614 p
= "return to sender";
615 else if (bitset(EF_WARNING
, e
->e_flags
))
617 else if (bitset(RTSF_PM_BOUNCE
, flags
))
618 p
= "postmaster notify";
621 sm_syslog(LOG_INFO
, e
->e_id
, "%s: %s: %s",
622 ee
->e_id
, p
, shortenstring(msg
, MAXSHORTSTR
));
627 addheader("MIME-Version", "1.0", 0, ee
);
628 (void) sm_snprintf(buf
, sizeof buf
, "%s.%ld/%.100s",
629 ee
->e_id
, (long)curtime(), MyHostName
);
630 ee
->e_msgboundary
= sm_rpool_strdup_x(ee
->e_rpool
, buf
);
631 (void) sm_snprintf(buf
, sizeof buf
,
633 "multipart/report; report-type=delivery-status;\n\tboundary=\"%s\"",
635 "multipart/mixed; boundary=\"%s\"",
638 addheader("Content-Type", buf
, 0, ee
);
640 p
= hvalue("Content-Transfer-Encoding", e
->e_header
);
641 if (p
!= NULL
&& sm_strcasecmp(p
, "binary") != 0)
643 if (p
== NULL
&& bitset(EF_HAS8BIT
, e
->e_flags
))
646 addheader("Content-Transfer-Encoding", p
, 0, ee
);
648 if (strncmp(msg
, "Warning:", 8) == 0)
650 addheader("Subject", msg
, 0, ee
);
651 p
= "warning-timeout";
653 else if (strncmp(msg
, "Postmaster warning:", 19) == 0)
655 addheader("Subject", msg
, 0, ee
);
656 p
= "postmaster-warning";
658 else if (strcmp(msg
, "Return receipt") == 0)
660 addheader("Subject", msg
, 0, ee
);
661 p
= "return-receipt";
663 else if (bitset(RTSF_PM_BOUNCE
, flags
))
665 (void) sm_snprintf(buf
, sizeof buf
,
666 "Postmaster notify: see transcript for details");
667 addheader("Subject", buf
, 0, ee
);
668 p
= "postmaster-notification";
672 (void) sm_snprintf(buf
, sizeof buf
,
673 "Returned mail: see transcript for details");
674 addheader("Subject", buf
, 0, ee
);
677 (void) sm_snprintf(buf
, sizeof buf
, "auto-generated (%s)", p
);
678 addheader("Auto-Submitted", buf
, 0, ee
);
680 /* fake up an address header for the from person */
681 expand("\201n", buf
, sizeof buf
, e
);
682 if (parseaddr(buf
, &ee
->e_from
,
683 RF_COPYALL
|RF_SENDERADDR
, '\0', NULL
, e
, false) == NULL
)
685 syserr("553 5.3.5 Can't parse myself!");
686 ExitStat
= EX_SOFTWARE
;
690 ee
->e_from
.q_flags
&= ~(QHASNOTIFY
|Q_PINGFLAGS
);
691 ee
->e_from
.q_flags
|= QPINGONFAILURE
;
692 ee
->e_sender
= ee
->e_from
.q_paddr
;
694 /* push state into submessage */
696 macdefine(&ee
->e_macro
, A_PERM
, 'f', "\201n");
697 macdefine(&ee
->e_macro
, A_PERM
, 'x', "Mail Delivery Subsystem");
698 eatheader(ee
, true, true);
700 /* mark statistics */
701 markstats(ee
, NULLADDR
, STATS_NORMAL
);
703 /* actually deliver the error message */
704 sendall(ee
, SM_DELIVER
);
707 dropenvelope(ee
, true, false);
708 sm_rpool_free(ee
->e_rpool
);
712 /* check for delivery errors */
713 if (ee
->e_parent
== NULL
||
714 !bitset(EF_RESPONSE
, ee
->e_parent
->e_flags
))
716 for (q
= ee
->e_sendqueue
; q
!= NULL
; q
= q
->q_next
)
718 if (QS_IS_ATTEMPTED(q
->q_state
))
724 ** ERRBODY -- output the body of an error message.
726 ** Typically this is a copy of the transcript plus a copy of the
727 ** original offending message.
730 ** mci -- the mailer connection information.
731 ** e -- the envelope we are working in.
732 ** separator -- any possible MIME separator (unused).
738 ** Outputs the body of an error message.
743 errbody(mci
, e
, separator
)
745 register ENVELOPE
*e
;
752 register SM_FILE_T
*xfile
;
754 register ADDRESS
*q
= NULL
;
755 char actual
[MAXLINE
];
758 if (bitset(MCIF_INHEADER
, mci
->mci_flags
))
761 mci
->mci_flags
&= ~MCIF_INHEADER
;
763 if (e
->e_parent
== NULL
)
765 syserr("errbody: null parent");
766 putline(" ----- Original message lost -----\n", mci
);
771 ** Output MIME header.
774 if (e
->e_msgboundary
!= NULL
)
776 putline("This is a MIME-encapsulated message", mci
);
778 (void) sm_strlcpyn(buf
, sizeof buf
, 2, "--", e
->e_msgboundary
);
784 ** Output introductory information.
788 p
= hvalue("subject", e
->e_header
);
789 if (p
!= NULL
&& strncmp(p
, "Postmaster ", 11) == 0)
793 for (q
= e
->e_parent
->e_sendqueue
; q
!= NULL
; q
= q
->q_next
)
795 if (QS_IS_BADADDR(q
->q_state
))
799 if (!pm_notify
&& q
== NULL
&&
800 !bitset(EF_FATALERRS
|EF_SENDRECEIPT
, e
->e_parent
->e_flags
))
802 putline(" **********************************************",
804 putline(" ** THIS IS A WARNING MESSAGE ONLY **",
806 putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **",
808 putline(" **********************************************",
812 (void) sm_snprintf(buf
, sizeof buf
,
813 "The original message was received at %s",
814 arpadate(ctime(&e
->e_parent
->e_ctime
)));
816 expand("from \201_", buf
, sizeof buf
, e
->e_parent
);
819 /* include id in postmaster copies */
820 if (pm_notify
&& e
->e_parent
->e_id
!= NULL
)
822 (void) sm_strlcpyn(buf
, sizeof buf
, 2, "with id ",
829 ** Output error message header (if specified and available).
832 if (ErrMsgFile
!= NULL
&&
833 !bitset(EF_SENDRECEIPT
, e
->e_parent
->e_flags
))
835 if (*ErrMsgFile
== '/')
837 long sff
= SFF_ROOTOK
|SFF_REGONLY
;
839 if (DontLockReadFiles
)
841 if (!bitnset(DBS_ERRORHEADERINUNSAFEDIRPATH
,
843 sff
|= SFF_SAFEDIRPATH
;
844 xfile
= safefopen(ErrMsgFile
, O_RDONLY
, 0444, sff
);
847 while (sm_io_fgets(xfile
, SM_TIME_DEFAULT
, buf
,
850 translate_dollars(buf
);
851 expand(buf
, buf
, sizeof buf
, e
);
854 (void) sm_io_close(xfile
, SM_TIME_DEFAULT
);
860 expand(ErrMsgFile
, buf
, sizeof buf
, e
);
867 ** Output message introduction
870 /* permanent fatal errors */
872 for (q
= e
->e_parent
->e_sendqueue
; q
!= NULL
; q
= q
->q_next
)
874 if (!QS_IS_BADADDR(q
->q_state
) ||
875 !bitset(QPINGONFAILURE
, q
->q_flags
))
880 putline(" ----- The following addresses had permanent fatal errors -----",
885 (void) sm_strlcpy(buf
, shortenstring(q
->q_paddr
, MAXSHORTSTR
),
888 if (q
->q_rstatus
!= NULL
)
890 (void) sm_snprintf(buf
, sizeof buf
,
892 shortenstring(exitstat(q
->q_rstatus
),
896 if (q
->q_alias
!= NULL
)
898 (void) sm_snprintf(buf
, sizeof buf
,
899 " (expanded from: %s)",
900 shortenstring(q
->q_alias
->q_paddr
,
908 /* transient non-fatal errors */
910 for (q
= e
->e_parent
->e_sendqueue
; q
!= NULL
; q
= q
->q_next
)
912 if (QS_IS_BADADDR(q
->q_state
) ||
913 !bitset(QPRIMARY
, q
->q_flags
) ||
914 !bitset(QBYNDELAY
, q
->q_flags
) ||
915 !bitset(QDELAYED
, q
->q_flags
))
920 putline(" ----- The following addresses had transient non-fatal errors -----",
925 (void) sm_strlcpy(buf
, shortenstring(q
->q_paddr
, MAXSHORTSTR
),
928 if (q
->q_alias
!= NULL
)
930 (void) sm_snprintf(buf
, sizeof buf
,
931 " (expanded from: %s)",
932 shortenstring(q
->q_alias
->q_paddr
,
940 /* successful delivery notifications */
942 for (q
= e
->e_parent
->e_sendqueue
; q
!= NULL
; q
= q
->q_next
)
944 if (QS_IS_BADADDR(q
->q_state
) ||
945 !bitset(QPRIMARY
, q
->q_flags
) ||
946 bitset(QBYNDELAY
, q
->q_flags
) ||
947 bitset(QDELAYED
, q
->q_flags
))
949 else if (bitset(QBYNRELAY
, q
->q_flags
))
950 p
= "Deliver-By notify: relayed";
951 else if (bitset(QBYTRACE
, q
->q_flags
))
952 p
= "Deliver-By trace: relayed";
953 else if (!bitset(QPINGONSUCCESS
, q
->q_flags
))
955 else if (bitset(QRELAYED
, q
->q_flags
))
956 p
= "relayed to non-DSN-aware mailer";
957 else if (bitset(QDELIVERED
, q
->q_flags
))
959 if (bitset(QEXPANDED
, q
->q_flags
))
960 p
= "successfully delivered to mailing list";
962 p
= "successfully delivered to mailbox";
964 else if (bitset(QEXPANDED
, q
->q_flags
))
965 p
= "expanded by alias";
971 putline(" ----- The following addresses had successful delivery notifications -----",
976 (void) sm_snprintf(buf
, sizeof buf
, "%s (%s)",
977 shortenstring(q
->q_paddr
, MAXSHORTSTR
), p
);
979 if (q
->q_alias
!= NULL
)
981 (void) sm_snprintf(buf
, sizeof buf
,
982 " (expanded from: %s)",
983 shortenstring(q
->q_alias
->q_paddr
,
992 ** Output transcript of errors
995 (void) sm_io_flush(smioout
, SM_TIME_DEFAULT
);
996 if (e
->e_parent
->e_xfp
== NULL
)
998 putline(" ----- Transcript of session is unavailable -----\n",
1004 (void) bfrewind(e
->e_parent
->e_xfp
);
1005 if (e
->e_xfp
!= NULL
)
1006 (void) sm_io_flush(e
->e_xfp
, SM_TIME_DEFAULT
);
1007 while (sm_io_fgets(e
->e_parent
->e_xfp
, SM_TIME_DEFAULT
, buf
,
1008 sizeof buf
) != NULL
)
1011 putline(" ----- Transcript of session follows -----\n",
1013 printheader
= false;
1021 ** Output machine-readable version.
1024 if (e
->e_msgboundary
!= NULL
)
1027 (void) sm_strlcpyn(buf
, sizeof buf
, 2, "--", e
->e_msgboundary
);
1029 putline("Content-Type: message/delivery-status", mci
);
1033 ** Output per-message information.
1036 /* original envelope id from MAIL FROM: line */
1037 if (e
->e_parent
->e_envid
!= NULL
)
1039 (void) sm_snprintf(buf
, sizeof buf
,
1040 "Original-Envelope-Id: %.800s",
1041 xuntextify(e
->e_parent
->e_envid
));
1045 /* Reporting-MTA: is us (required) */
1046 (void) sm_snprintf(buf
, sizeof buf
,
1047 "Reporting-MTA: dns; %.800s", MyHostName
);
1050 /* DSN-Gateway: not relevant since we are not translating */
1052 /* Received-From-MTA: shows where we got this message from */
1053 if (RealHostName
!= NULL
)
1055 /* XXX use $s for type? */
1056 if (e
->e_parent
->e_from
.q_mailer
== NULL
||
1057 (p
= e
->e_parent
->e_from
.q_mailer
->m_mtatype
) == NULL
)
1059 (void) sm_snprintf(buf
, sizeof buf
,
1060 "Received-From-MTA: %s; %.800s",
1065 /* Arrival-Date: -- when it arrived here */
1066 (void) sm_strlcpyn(buf
, sizeof buf
, 2, "Arrival-Date: ",
1067 arpadate(ctime(&e
->e_parent
->e_ctime
)));
1070 /* Deliver-By-Date: -- when it should have been delivered */
1071 if (IS_DLVR_BY(e
->e_parent
))
1075 dbyd
= e
->e_parent
->e_ctime
+ e
->e_parent
->e_deliver_by
;
1076 (void) sm_strlcpyn(buf
, sizeof buf
, 2,
1077 "Deliver-By-Date: ",
1078 arpadate(ctime(&dbyd
)));
1083 ** Output per-address information.
1086 for (q
= e
->e_parent
->e_sendqueue
; q
!= NULL
; q
= q
->q_next
)
1090 if (QS_IS_BADADDR(q
->q_state
))
1092 /* RFC 1891, 6.2.6 (b) */
1093 if (bitset(QHASNOTIFY
, q
->q_flags
) &&
1094 !bitset(QPINGONFAILURE
, q
->q_flags
))
1098 else if (!bitset(QPRIMARY
, q
->q_flags
))
1100 else if (bitset(QDELIVERED
, q
->q_flags
))
1102 if (bitset(QEXPANDED
, q
->q_flags
))
1103 action
= "delivered (to mailing list)";
1105 action
= "delivered (to mailbox)";
1107 else if (bitset(QRELAYED
, q
->q_flags
))
1108 action
= "relayed (to non-DSN-aware mailer)";
1109 else if (bitset(QEXPANDED
, q
->q_flags
))
1110 action
= "expanded (to multi-recipient alias)";
1111 else if (bitset(QDELAYED
, q
->q_flags
))
1113 else if (bitset(QBYTRACE
, q
->q_flags
))
1114 action
= "relayed (Deliver-By trace mode)";
1115 else if (bitset(QBYNDELAY
, q
->q_flags
))
1116 action
= "delayed (Deliver-By notify mode)";
1117 else if (bitset(QBYNRELAY
, q
->q_flags
))
1118 action
= "relayed (Deliver-By notify mode)";
1124 /* Original-Recipient: -- passed from on high */
1125 if (q
->q_orcpt
!= NULL
)
1127 (void) sm_snprintf(buf
, sizeof buf
,
1128 "Original-Recipient: %.800s",
1133 /* Figure out actual recipient */
1135 if (q
->q_user
[0] != '\0')
1137 if (q
->q_mailer
!= NULL
&&
1138 q
->q_mailer
->m_addrtype
!= NULL
)
1139 p
= q
->q_mailer
->m_addrtype
;
1143 if (sm_strcasecmp(p
, "rfc822") == 0 &&
1144 strchr(q
->q_user
, '@') == NULL
)
1146 (void) sm_snprintf(actual
,
1148 "%s; %.700s@%.100s",
1154 (void) sm_snprintf(actual
,
1161 /* Final-Recipient: -- the name from the RCPT command */
1162 if (q
->q_finalrcpt
== NULL
)
1164 /* should never happen */
1165 sm_syslog(LOG_ERR
, e
->e_id
,
1166 "returntosender: q_finalrcpt is NULL");
1168 /* try to fall back to the actual recipient */
1169 if (actual
[0] != '\0')
1170 q
->q_finalrcpt
= sm_rpool_strdup_x(e
->e_rpool
,
1174 if (q
->q_finalrcpt
!= NULL
)
1176 (void) sm_snprintf(buf
, sizeof buf
,
1177 "Final-Recipient: %s",
1182 /* X-Actual-Recipient: -- the real problem address */
1183 if (actual
[0] != '\0' &&
1184 q
->q_finalrcpt
!= NULL
&&
1185 #if _FFR_PRIV_NOACTUALRECIPIENT
1186 !bitset(PRIV_NOACTUALRECIPIENT
, PrivacyFlags
) &&
1187 #endif /* _FFR_PRIV_NOACTUALRECIPIENT */
1188 strcmp(actual
, q
->q_finalrcpt
) != 0)
1190 (void) sm_snprintf(buf
, sizeof buf
,
1191 "X-Actual-Recipient: %s",
1196 /* Action: -- what happened? */
1197 (void) sm_strlcpyn(buf
, sizeof buf
, 2, "Action: ",
1201 /* Status: -- what _really_ happened? */
1202 if (q
->q_status
!= NULL
)
1204 else if (QS_IS_BADADDR(q
->q_state
))
1206 else if (QS_IS_QUEUEUP(q
->q_state
))
1210 (void) sm_strlcpyn(buf
, sizeof buf
, 2, "Status: ", p
);
1213 /* Remote-MTA: -- who was I talking to? */
1214 if (q
->q_statmta
!= NULL
)
1216 if (q
->q_mailer
== NULL
||
1217 (p
= q
->q_mailer
->m_mtatype
) == NULL
)
1219 (void) sm_snprintf(buf
, sizeof buf
,
1220 "Remote-MTA: %s; %.800s",
1222 p
= &buf
[strlen(buf
) - 1];
1228 /* Diagnostic-Code: -- actual result from other end */
1229 if (q
->q_rstatus
!= NULL
)
1231 p
= q
->q_mailer
->m_diagtype
;
1234 (void) sm_snprintf(buf
, sizeof buf
,
1235 "Diagnostic-Code: %s; %.800s",
1240 /* Last-Attempt-Date: -- fine granularity */
1241 if (q
->q_statdate
== (time_t) 0L)
1242 q
->q_statdate
= curtime();
1243 (void) sm_strlcpyn(buf
, sizeof buf
, 2,
1244 "Last-Attempt-Date: ",
1245 arpadate(ctime(&q
->q_statdate
)));
1248 /* Will-Retry-Until: -- for delayed messages only */
1249 if (QS_IS_QUEUEUP(q
->q_state
))
1253 xdate
= e
->e_parent
->e_ctime
+
1254 TimeOuts
.to_q_return
[e
->e_parent
->e_timeoutclass
];
1255 (void) sm_strlcpyn(buf
, sizeof buf
, 2,
1256 "Will-Retry-Until: ",
1257 arpadate(ctime(&xdate
)));
1265 ** Output text of original message
1269 if (bitset(EF_HAS_DF
, e
->e_parent
->e_flags
))
1271 sendbody
= !bitset(EF_NO_BODY_RETN
, e
->e_parent
->e_flags
) &&
1272 !bitset(EF_NO_BODY_RETN
, e
->e_flags
);
1274 if (e
->e_msgboundary
== NULL
)
1277 putline(" ----- Original message follows -----\n", mci
);
1279 putline(" ----- Message header follows -----\n", mci
);
1283 (void) sm_strlcpyn(buf
, sizeof buf
, 2, "--",
1287 (void) sm_strlcpyn(buf
, sizeof buf
, 2, "Content-Type: ",
1288 sendbody
? "message/rfc822"
1289 : "text/rfc822-headers");
1292 p
= hvalue("Content-Transfer-Encoding",
1293 e
->e_parent
->e_header
);
1294 if (p
!= NULL
&& sm_strcasecmp(p
, "binary") != 0)
1297 bitset(EF_HAS8BIT
, e
->e_parent
->e_flags
))
1301 (void) sm_snprintf(buf
, sizeof buf
,
1302 "Content-Transfer-Encoding: %s",
1309 putheader(mci
, e
->e_parent
->e_header
, e
->e_parent
, M87F_OUTER
);
1312 putbody(mci
, e
->e_parent
, e
->e_msgboundary
);
1313 else if (e
->e_msgboundary
== NULL
)
1316 putline(" ----- Message body suppressed -----", mci
);
1319 else if (e
->e_msgboundary
== NULL
)
1321 putline(" ----- No message was collected -----\n", mci
);
1324 if (e
->e_msgboundary
!= NULL
)
1327 (void) sm_strlcpyn(buf
, sizeof buf
, 3, "--", e
->e_msgboundary
,
1332 (void) sm_io_flush(mci
->mci_out
, SM_TIME_DEFAULT
);
1339 syserr("errbody: I/O error");
1342 ** SMTPTODSN -- convert SMTP to DSN status code
1345 ** smtpstat -- the smtp status code (e.g., 550).
1348 ** The DSN version of the status code.
1350 ** Storage Management:
1351 ** smtptodsn() returns a pointer to a character string literal,
1352 ** which will remain valid forever, and thus does not need to
1353 ** be copied. Current code relies on this property.
1365 case 450: /* Req mail action not taken: mailbox unavailable */
1368 case 451: /* Req action aborted: local error in processing */
1371 case 452: /* Req action not taken: insufficient sys storage */
1374 case 500: /* Syntax error, command unrecognized */
1377 case 501: /* Syntax error in parameters or arguments */
1380 case 502: /* Command not implemented */
1383 case 503: /* Bad sequence of commands */
1386 case 504: /* Command parameter not implemented */
1389 case 550: /* Req mail action not taken: mailbox unavailable */
1392 case 551: /* User not local; please try <...> */
1395 case 552: /* Req mail action aborted: exceeded storage alloc */
1398 case 553: /* Req action not taken: mailbox name not allowed */
1401 case 554: /* Transaction failed */
1405 if ((smtpstat
/ 100) == 2)
1407 if ((smtpstat
/ 100) == 4)
1412 ** XTEXTIFY -- take regular text and turn it into DSN-style xtext
1415 ** t -- the text to convert.
1416 ** taboo -- additional characters that must be encoded.
1419 ** The xtext-ified version of the same string.
1430 static char *bp
= NULL
;
1431 static int bplen
= 0;
1436 /* figure out how long this xtext will have to be */
1438 for (p
= t
; *p
!= '\0'; p
++)
1440 register int c
= (*p
& 0xff);
1442 /* ASCII dependence here -- this is the way the spec words it */
1443 if (c
< '!' || c
> '~' || c
== '+' || c
== '\\' || c
== '(' ||
1444 strchr(taboo
, c
) != NULL
)
1450 /* since nbogus is ssize_t and wrapped, 2 * size_t would wrap */
1451 syserr("!xtextify string too long");
1455 l
+= nbogus
* 2 + 1;
1457 /* now allocate space if necessary for the new string */
1461 sm_free(bp
); /* XXX */
1462 bp
= sm_pmalloc_x(l
);
1466 /* ok, copy the text with byte expansion */
1467 for (p
= bp
; *t
!= '\0'; )
1469 register int c
= (*t
++ & 0xff);
1471 /* ASCII dependence here -- this is the way the spec words it */
1472 if (c
< '!' || c
> '~' || c
== '+' || c
== '\\' || c
== '(' ||
1473 strchr(taboo
, c
) != NULL
)
1476 *p
++ = "0123456789ABCDEF"[c
>> 4];
1477 *p
++ = "0123456789ABCDEF"[c
& 0xf];
1486 ** XUNTEXTIFY -- take xtext and turn it into plain text
1489 ** t -- the xtextified text.
1492 ** The decoded text. No attempt is made to deal with
1493 ** null strings in the resulting text.
1502 static char *bp
= NULL
;
1503 static int bplen
= 0;
1505 /* heuristic -- if no plus sign, just return the input */
1506 if (strchr(t
, '+') == NULL
)
1509 /* xtext is always longer than decoded text */
1514 sm_free(bp
); /* XXX */
1519 /* ok, copy the text with byte compression */
1520 for (p
= bp
; *t
!= '\0'; t
++)
1522 register int c
= *t
& 0xff;
1531 if (!isascii(c
) || !isxdigit(c
))
1533 /* error -- first digit is not hex */
1534 usrerr("bogus xtext: +%c", c
);
1540 else if (isupper(c
))
1547 if (!isascii(c
) || !isxdigit(c
))
1549 /* error -- second digit is not hex */
1550 usrerr("bogus xtext: +%x%c", *p
>> 4, c
);
1556 else if (isupper(c
))
1566 ** XTEXTOK -- check if a string is legal xtext
1568 ** Xtext is used in Delivery Status Notifications. The spec was
1569 ** taken from RFC 1891, ``SMTP Service Extension for Delivery
1570 ** Status Notifications''.
1573 ** s -- the string to check.
1576 ** true -- if 's' is legal xtext.
1577 ** false -- if it has any illegal characters in it.
1586 while ((c
= *s
++) != '\0')
1591 if (!isascii(c
) || !isxdigit(c
))
1594 if (!isascii(c
) || !isxdigit(c
))
1597 else if (c
< '!' || c
> '~' || c
== '=')
1603 ** PRUNEROUTE -- prune an RFC-822 source route
1605 ** Trims down a source route to the last internet-registered hop.
1606 ** This is encouraged by RFC 1123 section 5.3.3.
1609 ** addr -- the address
1612 ** true -- address was modified
1613 ** false -- address could not be pruned
1616 ** modifies addr in-place
1624 char *start
, *at
, *comma
;
1629 char hostbuf
[BUFSIZ
];
1630 char *mxhosts
[MAXMXHOSTS
+ 1];
1632 /* check to see if this is really a route-addr */
1633 if (*addr
!= '<' || addr
[1] != '@' || addr
[strlen(addr
) - 1] != '>')
1637 ** Can't simply find the first ':' is the address might be in the
1638 ** form: "<@[IPv6:::1]:user@host>" and the first ':' in inside
1639 ** the IPv6 address.
1644 while (*start
!= '\0')
1646 if (*start
== ':' && braclev
<= 0)
1648 else if (*start
== '[')
1650 else if (*start
== ']' && braclev
> 0)
1654 if (braclev
> 0 || *start
!= ':')
1657 at
= strrchr(addr
, '@');
1658 if (at
== NULL
|| at
< start
)
1661 /* slice off the angle brackets */
1663 if (i
>= sizeof hostbuf
)
1665 (void) sm_strlcpy(hostbuf
, at
+ 1, sizeof hostbuf
);
1666 hostbuf
[i
- 1] = '\0';
1668 while (start
!= NULL
)
1670 if (getmxrr(hostbuf
, mxhosts
, NULL
, false,
1671 &rcode
, true, NULL
) > 0)
1673 (void) sm_strlcpy(addr
+ 1, start
+ 1,
1679 comma
= strrchr(addr
, ',');
1680 if (comma
!= NULL
&& comma
[1] == '@' &&
1681 strlen(comma
+ 2) < sizeof hostbuf
)
1682 (void) sm_strlcpy(hostbuf
, comma
+ 2, sizeof hostbuf
);
1688 #endif /* NAMED_BIND */