2 * Copyright (c) 1998-2002 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.299.2.1 2002/10/23 15:08:47 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(&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).
182 if (p == NULL || sm_io_reopen(SmFtStdio,
184 p, SM_IO_WRONLY, NULL,
191 expand("\201n", buf, sizeof buf, e);
192 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
193 "\r\nMessage from %s...\r\n", buf);
194 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
195 "Errors occurred while sending mail.\r\n");
196 if (e->e_xfp != NULL)
198 (void) bfrewind(e->e_xfp);
199 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
200 "Transcript follows:\r\n");
201 while (sm_io_fgets(e->e_xfp, SM_TIME_DEFAULT,
202 buf, sizeof buf) != NULL &&
203 !sm_io_error(smioout))
204 (void) sm_io_fputs(smioout,
210 syserr("Cannot open %s",
211 queuename(e, XSCRPT_LETTER));
212 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
213 "Transcript of session is unavailable.\r\n");
215 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
216 "Original message will be saved in dead.letter.\r\n");
217 state = ESM_DEADLETTER;
222 ** If mailing back, do it.
223 ** Throw away all further output. Don't alias,
224 ** since this could cause loops, e.g., if joe
225 ** mails to joe@x, and for some reason the network
226 ** for @x is down, then the response gets sent to
227 ** joe@x, which gives a response, etc. Also force
228 ** the mail to be delivered even if a version of
229 ** it has already been sent to the sender.
231 ** If this is a configuration or local software
232 ** error, send to the local postmaster as well,
233 ** since the originator can't do anything
234 ** about it anyway. Note that this is a full
235 ** copy of the message (intentionally) so that
236 ** the Postmaster can forward things along.
239 if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
241 (void) sendtolist("postmaster", NULLADDR,
242 &e->e_errorqueue, 0, e);
244 if (!emptyaddr(&e->e_from))
246 char from[TOBUFSIZE];
248 if (sm_strlcpy(from, e->e_from.q_paddr,
249 sizeof from) >= sizeof from)
251 state = ESM_POSTMASTER;
255 if (!DontPruneRoutes)
256 (void) pruneroute(from);
258 (void) sendtolist(from, NULLADDR,
259 &e->e_errorqueue, 0, e);
263 ** Deliver a non-delivery report to the
264 ** Postmaster-designate (not necessarily
265 ** Postmaster). This does not include the
266 ** body of the message, for privacy reasons.
267 ** You really shouldn't need this.
270 e->e_flags |= EF_PM_NOTIFY;
272 /* check to see if there are any good addresses */
273 for (q = e->e_errorqueue; q != NULL; q = q->q_next)
275 if (QS_IS_SENDABLE(q->q_state))
280 /* this is an error-error */
281 state = ESM_POSTMASTER;
284 if (returntosender(e->e_message, e->e_errorqueue,
285 sendbody ? RTSF_SEND_BODY
293 /* didn't work -- return to postmaster */
294 state = ESM_POSTMASTER;
299 ** Similar to previous case, but to system postmaster.
303 expand(DoubleBounceAddr, buf, sizeof buf, e);
306 ** Just drop it on the floor if DoubleBounceAddr
307 ** expands to an empty string.
315 if (sendtolist(buf, NULLADDR, &q, 0, e) <= 0)
317 syserr("553 5.3.0 cannot parse %s!", buf);
318 ExitStat = EX_SOFTWARE;
319 state = ESM_DEADLETTERDROP;
322 flags = RTSF_PM_BOUNCE;
324 flags |= RTSF_SEND_BODY;
325 if (returntosender(e->e_message, q, flags, e) == 0)
331 /* didn't work -- last resort */
332 state = ESM_DEADLETTERDROP;
337 ** Save the message in dead.letter.
338 ** If we weren't mailing back, and the user is
339 ** local, we should save the message in
340 ** ~/dead.letter so that the poor person doesn't
341 ** have to type it over again -- and we all know
342 ** what poor typists UNIX users are.
346 if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
348 if (e->e_from.q_home != NULL)
349 p = e->e_from.q_home;
350 else if (sm_mbdb_lookup(e->e_from.q_user, &user)
352 *user.mbdb_homedir != '\0')
353 p = user.mbdb_homedir;
355 if (p == NULL || e->e_dfp == NULL)
357 /* no local directory or no data file */
362 /* we have a home directory; write dead.letter */
363 macdefine(&e->e_macro, A_TEMP, 'z', p);
365 /* get the sender for the UnixFromLine */
366 p = macvalue('g', e);
367 macdefine(&e->e_macro, A_PERM, 'g', e->e_sender);
369 expand("\201z/dead.letter", dlbuf, sizeof dlbuf, e);
370 sff = SFF_CREAT|SFF_REGONLY|SFF_RUNASREALUID;
374 if (writable(dlbuf, NULL, sff) &&
375 mailfile(dlbuf, FileMailer, NULL, sff, e) == EX_OK)
377 int oldverb = Verbose;
379 if (OpMode != MD_DAEMON && OpMode != MD_SMTP)
382 message("Saved message in %s", dlbuf);
384 macdefine(&e->e_macro, A_PERM, 'g', p);
388 macdefine(&e->e_macro, A_PERM, 'g', p);
392 case ESM_DEADLETTERDROP:
394 ** Log the mail in DeadLetterDrop file.
403 if ((SafeFileEnv != NULL && SafeFileEnv[0] != '\0') ||
404 DeadLetterDrop == NULL ||
405 DeadLetterDrop[0] == '\0')
411 sff = SFF_CREAT|SFF_REGONLY|SFF_ROOTOK|SFF_OPENASROOT|SFF_MUSTOWN;
412 if (!writable(DeadLetterDrop, NULL, sff) ||
413 (fp = safefopen(DeadLetterDrop, O_WRONLY|O_APPEND,
414 FileMode, sff)) == NULL)
420 memset(&mcibuf, '\0', sizeof mcibuf);
422 mcibuf.mci_mailer = FileMailer;
423 if (bitnset(M_7BITS, FileMailer->m_flags))
424 mcibuf.mci_flags |= MCIF_7BIT;
426 /* get the sender for the UnixFromLine */
427 p = macvalue('g', e);
428 macdefine(&e->e_macro, A_PERM, 'g', e->e_sender);
430 putfromline(&mcibuf, e);
431 (*e->e_puthdr)(&mcibuf, e->e_header, e, M87F_OUTER);
432 (*e->e_putbody)(&mcibuf, e, NULL);
433 putline("\n", &mcibuf); /* XXX EOL from FileMailer? */
434 (void) sm_io_flush(fp, SM_TIME_DEFAULT);
435 if (sm_io_error(fp) ||
436 sm_io_close(fp, SM_TIME_DEFAULT) < 0)
440 int oldverb = Verbose;
442 if (OpMode != MD_DAEMON && OpMode != MD_SMTP)
445 message("Saved message in %s",
449 sm_syslog(LOG_NOTICE, e->e_id,
450 "Saved message in %s",
454 macdefine(&e->e_macro, A_PERM, 'g', p);
458 syserr("554 5.3.5 savemail: unknown state %d", state);
462 /* leave the locked queue & transcript files around */
463 loseqfile(e, "savemail panic");
466 syserr("554 savemail: cannot save rejected email anywhere");
474 ** RETURNTOSENDER -- return a message to the sender with an error.
477 ** msg -- the explanatory message.
478 ** returnq -- the queue of people to send the message to.
479 ** flags -- flags tweaking the operation:
480 ** RTSF_SENDBODY -- include body of message (otherwise
481 ** just send the header).
482 ** RTSF_PMBOUNCE -- this is a postmaster bounce.
483 ** e -- the current envelope.
486 ** zero -- if everything went ok.
487 ** else -- some error.
490 ** Returns the current message to the sender via mail.
493 #define MAXRETURNS 6 /* max depth of returning messages */
494 #define ERRORFUDGE 1024 /* nominal size of error message text */
497 returntosender(msg, returnq, flags, e)
501 register ENVELOPE *e;
503 register ENVELOPE *ee;
504 ENVELOPE *oldcur = CurEnv;
505 ENVELOPE errenvelope;
506 static int returndepth = 0;
509 char buf[MAXNAME + 1];
515 msg = "Unable to deliver mail";
519 sm_dprintf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%p, returnq=",
520 msg, returndepth, e);
521 printaddr(returnq, true);
524 sm_dprintf("Sendq=");
525 printaddr(e->e_sendqueue, true);
529 if (++returndepth >= MAXRETURNS)
531 if (returndepth != MAXRETURNS)
532 syserr("554 5.3.0 returntosender: infinite recursion on %s",
534 /* don't "unrecurse" and fake a clean exit */
539 macdefine(&e->e_macro, A_PERM, 'g', e->e_sender);
540 macdefine(&e->e_macro, A_PERM, 'u', NULL);
542 /* initialize error envelope */
543 ee = newenvelope(&errenvelope, e, sm_rpool_new_x(NULL));
544 macdefine(&ee->e_macro, A_PERM, 'a', "\201b");
545 macdefine(&ee->e_macro, A_PERM, 'r', "");
546 macdefine(&ee->e_macro, A_PERM, 's', "localhost");
547 macdefine(&ee->e_macro, A_PERM, '_', "localhost");
550 ee->e_puthdr = putheader;
551 ee->e_putbody = errbody;
552 ee->e_flags |= EF_RESPONSE|EF_METOO;
553 if (!bitset(EF_OLDSTYLE, e->e_flags))
554 ee->e_flags &= ~EF_OLDSTYLE;
555 if (bitset(EF_DONT_MIME, e->e_flags))
557 ee->e_flags |= EF_DONT_MIME;
560 ** If we can't convert to MIME and we don't pass
561 ** 8-bit, we can't send the body.
564 if (bitset(EF_HAS8BIT, e->e_flags) &&
565 !bitset(MM_PASS8BIT, MimeMode))
566 flags &= ~RTSF_SEND_BODY;
569 ee->e_sendqueue = returnq;
571 if (bitset(RTSF_SEND_BODY, flags) &&
572 !bitset(PRIV_NOBODYRETN, PrivacyFlags))
573 ee->e_msgsize = ERRORFUDGE + e->e_msgsize;
575 ee->e_flags |= EF_NO_BODY_RETN;
577 if (!setnewqueue(ee))
579 syserr("554 5.3.0 returntosender: cannot select queue for %s",
581 ExitStat = EX_UNAVAILABLE;
588 _res.retry = TimeOuts.res_retry[RES_TO_FIRST];
589 _res.retrans = TimeOuts.res_retrans[RES_TO_FIRST];
590 #endif /* NAMED_BIND */
591 for (q = returnq; q != NULL; q = q->q_next)
593 if (QS_IS_BADADDR(q->q_state))
596 q->q_flags &= ~(QHASNOTIFY|Q_PINGFLAGS);
597 q->q_flags |= QPINGONFAILURE;
599 if (!QS_IS_DEAD(q->q_state))
602 if (q->q_alias == NULL)
603 addheader("To", q->q_paddr, 0, ee);
608 if (bitset(EF_RESPONSE, e->e_flags))
609 p = "return to sender";
610 else if (bitset(EF_WARNING, e->e_flags))
612 else if (bitset(RTSF_PM_BOUNCE, flags))
613 p = "postmaster notify";
616 sm_syslog(LOG_INFO, e->e_id, "%s: %s: %s",
617 ee->e_id, p, shortenstring(msg, MAXSHORTSTR));
622 addheader("MIME-Version", "1.0", 0, ee);
623 (void) sm_snprintf(buf, sizeof buf, "%s.%ld/%.100s",
624 ee->e_id, (long)curtime(), MyHostName);
625 ee->e_msgboundary = sm_rpool_strdup_x(ee->e_rpool, buf);
626 (void) sm_snprintf(buf, sizeof buf,
628 "multipart/report; report-type=delivery-status;\n\tboundary=\"%s\"",
630 "multipart/mixed; boundary=\"%s\"",
633 addheader("Content-Type", buf, 0, ee);
635 p = hvalue("Content-Transfer-Encoding", e->e_header);
636 if (p != NULL && sm_strcasecmp(p, "binary") != 0)
638 if (p == NULL && bitset(EF_HAS8BIT, e->e_flags))
641 addheader("Content-Transfer-Encoding", p, 0, ee);
643 if (strncmp(msg, "Warning:", 8) == 0)
645 addheader("Subject", msg, 0, ee);
646 p = "warning-timeout";
648 else if (strncmp(msg, "Postmaster warning:", 19) == 0)
650 addheader("Subject", msg, 0, ee);
651 p = "postmaster-warning";
653 else if (strcmp(msg, "Return receipt") == 0)
655 addheader("Subject", msg, 0, ee);
656 p = "return-receipt";
658 else if (bitset(RTSF_PM_BOUNCE, flags))
660 (void) sm_snprintf(buf, sizeof buf,
661 "Postmaster notify: see transcript for details");
662 addheader("Subject", buf, 0, ee);
663 p = "postmaster-notification";
667 (void) sm_snprintf(buf, sizeof buf,
668 "Returned mail: see transcript for details");
669 addheader("Subject", buf, 0, ee);
672 (void) sm_snprintf(buf, sizeof buf, "auto-generated (%s)", p);
673 addheader("Auto-Submitted", buf, 0, ee);
675 /* fake up an address header for the from person */
676 expand("\201n", buf, sizeof buf, e);
677 if (parseaddr(buf, &ee->e_from,
678 RF_COPYALL|RF_SENDERADDR, '\0', NULL, e, false) == NULL)
680 syserr("553 5.3.5 Can't parse myself!");
681 ExitStat = EX_SOFTWARE;
685 ee->e_from.q_flags &= ~(QHASNOTIFY|Q_PINGFLAGS);
686 ee->e_from.q_flags |= QPINGONFAILURE;
687 ee->e_sender = ee->e_from.q_paddr;
689 /* push state into submessage */
691 macdefine(&ee->e_macro, A_PERM, 'f', "\201n");
692 macdefine(&ee->e_macro, A_PERM, 'x', "Mail Delivery Subsystem");
693 eatheader(ee, true, true);
695 /* mark statistics */
696 markstats(ee, NULLADDR, STATS_NORMAL);
698 /* actually deliver the error message */
699 sendall(ee, SM_DELIVER);
702 dropenvelope(ee, true, false);
703 sm_rpool_free(ee->e_rpool);
707 /* check for delivery errors */
708 if (ee->e_parent == NULL ||
709 !bitset(EF_RESPONSE, ee->e_parent->e_flags))
711 for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
713 if (QS_IS_ATTEMPTED(q->q_state))
719 ** ERRBODY -- output the body of an error message.
721 ** Typically this is a copy of the transcript plus a copy of the
722 ** original offending message.
725 ** mci -- the mailer connection information.
726 ** e -- the envelope we are working in.
727 ** separator -- any possible MIME separator (unused).
733 ** Outputs the body of an error message.
738 errbody(mci, e, separator)
740 register ENVELOPE *e;
747 register SM_FILE_T *xfile;
749 register ADDRESS *q = NULL;
750 char actual[MAXLINE];
753 if (bitset(MCIF_INHEADER, mci->mci_flags))
756 mci->mci_flags &= ~MCIF_INHEADER;
758 if (e->e_parent == NULL)
760 syserr("errbody: null parent");
761 putline(" ----- Original message lost -----\n", mci);
766 ** Output MIME header.
769 if (e->e_msgboundary != NULL)
771 putline("This is a MIME-encapsulated message", mci);
773 (void) sm_strlcpyn(buf, sizeof buf, 2, "--", e->e_msgboundary);
779 ** Output introductory information.
783 p = hvalue("subject", e->e_header);
784 if (p != NULL && strncmp(p, "Postmaster ", 11) == 0)
788 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
790 if (QS_IS_BADADDR(q->q_state))
794 if (!pm_notify && q == NULL &&
795 !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
797 putline(" **********************************************",
799 putline(" ** THIS IS A WARNING MESSAGE ONLY **",
801 putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **",
803 putline(" **********************************************",
807 (void) sm_snprintf(buf, sizeof buf,
808 "The original message was received at %s",
809 arpadate(ctime(&e->e_parent->e_ctime)));
811 expand("from \201_", buf, sizeof buf, e->e_parent);
814 /* include id in postmaster copies */
815 if (pm_notify && e->e_parent->e_id != NULL)
817 (void) sm_strlcpyn(buf, sizeof buf, 2, "with id ",
824 ** Output error message header (if specified and available).
827 if (ErrMsgFile != NULL &&
828 !bitset(EF_SENDRECEIPT, e->e_parent->e_flags))
830 if (*ErrMsgFile == '/')
832 long sff = SFF_ROOTOK|SFF_REGONLY;
834 if (DontLockReadFiles)
836 if (!bitnset(DBS_ERRORHEADERINUNSAFEDIRPATH,
838 sff |= SFF_SAFEDIRPATH;
839 xfile = safefopen(ErrMsgFile, O_RDONLY, 0444, sff);
842 while (sm_io_fgets(xfile, SM_TIME_DEFAULT, buf,
845 translate_dollars(buf);
846 expand(buf, buf, sizeof buf, e);
849 (void) sm_io_close(xfile, SM_TIME_DEFAULT);
855 expand(ErrMsgFile, buf, sizeof buf, e);
862 ** Output message introduction
865 /* permanent fatal errors */
867 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
869 if (!QS_IS_BADADDR(q->q_state) ||
870 !bitset(QPINGONFAILURE, q->q_flags))
875 putline(" ----- The following addresses had permanent fatal errors -----",
880 (void) sm_strlcpy(buf, shortenstring(q->q_paddr, MAXSHORTSTR),
883 if (q->q_rstatus != NULL)
885 (void) sm_snprintf(buf, sizeof buf,
887 shortenstring(exitstat(q->q_rstatus),
891 if (q->q_alias != NULL)
893 (void) sm_snprintf(buf, sizeof buf,
894 " (expanded from: %s)",
895 shortenstring(q->q_alias->q_paddr,
903 /* transient non-fatal errors */
905 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
907 if (QS_IS_BADADDR(q->q_state) ||
908 !bitset(QPRIMARY, q->q_flags) ||
909 !bitset(QBYNDELAY, q->q_flags) ||
910 !bitset(QDELAYED, q->q_flags))
915 putline(" ----- The following addresses had transient non-fatal errors -----",
920 (void) sm_strlcpy(buf, shortenstring(q->q_paddr, MAXSHORTSTR),
923 if (q->q_alias != NULL)
925 (void) sm_snprintf(buf, sizeof buf,
926 " (expanded from: %s)",
927 shortenstring(q->q_alias->q_paddr,
935 /* successful delivery notifications */
937 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
939 if (QS_IS_BADADDR(q->q_state) ||
940 !bitset(QPRIMARY, q->q_flags) ||
941 bitset(QBYNDELAY, q->q_flags) ||
942 bitset(QDELAYED, q->q_flags))
944 else if (bitset(QBYNRELAY, q->q_flags))
945 p = "Deliver-By notify: relayed";
946 else if (bitset(QBYTRACE, q->q_flags))
947 p = "Deliver-By trace: relayed";
948 else if (!bitset(QPINGONSUCCESS, q->q_flags))
950 else if (bitset(QRELAYED, q->q_flags))
951 p = "relayed to non-DSN-aware mailer";
952 else if (bitset(QDELIVERED, q->q_flags))
954 if (bitset(QEXPANDED, q->q_flags))
955 p = "successfully delivered to mailing list";
957 p = "successfully delivered to mailbox";
959 else if (bitset(QEXPANDED, q->q_flags))
960 p = "expanded by alias";
966 putline(" ----- The following addresses had successful delivery notifications -----",
971 (void) sm_snprintf(buf, sizeof buf, "%s (%s)",
972 shortenstring(q->q_paddr, MAXSHORTSTR), p);
974 if (q->q_alias != NULL)
976 (void) sm_snprintf(buf, sizeof buf,
977 " (expanded from: %s)",
978 shortenstring(q->q_alias->q_paddr,
987 ** Output transcript of errors
990 (void) sm_io_flush(smioout, SM_TIME_DEFAULT);
991 if (e->e_parent->e_xfp == NULL)
993 putline(" ----- Transcript of session is unavailable -----\n",
999 (void) bfrewind(e->e_parent->e_xfp);
1000 if (e->e_xfp != NULL)
1001 (void) sm_io_flush(e->e_xfp, SM_TIME_DEFAULT);
1002 while (sm_io_fgets(e->e_parent->e_xfp, SM_TIME_DEFAULT, buf,
1003 sizeof buf) != NULL)
1006 putline(" ----- Transcript of session follows -----\n",
1008 printheader = false;
1016 ** Output machine-readable version.
1019 if (e->e_msgboundary != NULL)
1022 (void) sm_strlcpyn(buf, sizeof buf, 2, "--", e->e_msgboundary);
1024 putline("Content-Type: message/delivery-status", mci);
1028 ** Output per-message information.
1031 /* original envelope id from MAIL FROM: line */
1032 if (e->e_parent->e_envid != NULL)
1034 (void) sm_snprintf(buf, sizeof buf,
1035 "Original-Envelope-Id: %.800s",
1036 xuntextify(e->e_parent->e_envid));
1040 /* Reporting-MTA: is us (required) */
1041 (void) sm_snprintf(buf, sizeof buf,
1042 "Reporting-MTA: dns; %.800s", MyHostName);
1045 /* DSN-Gateway: not relevant since we are not translating */
1047 /* Received-From-MTA: shows where we got this message from */
1048 if (RealHostName != NULL)
1050 /* XXX use $s for type? */
1051 if (e->e_parent->e_from.q_mailer == NULL ||
1052 (p = e->e_parent->e_from.q_mailer->m_mtatype) == NULL)
1054 (void) sm_snprintf(buf, sizeof buf,
1055 "Received-From-MTA: %s; %.800s",
1060 /* Arrival-Date: -- when it arrived here */
1061 (void) sm_strlcpyn(buf, sizeof buf, 2, "Arrival-Date: ",
1062 arpadate(ctime(&e->e_parent->e_ctime)));
1065 /* Deliver-By-Date: -- when it should have been delivered */
1066 if (IS_DLVR_BY(e->e_parent))
1070 dbyd = e->e_parent->e_ctime + e->e_parent->e_deliver_by;
1071 (void) sm_strlcpyn(buf, sizeof buf, 2,
1072 "Deliver-By-Date: ",
1073 arpadate(ctime(&dbyd)));
1078 ** Output per-address information.
1081 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
1085 if (QS_IS_BADADDR(q->q_state))
1087 /* RFC 1891, 6.2.6 (b) */
1088 if (bitset(QHASNOTIFY, q->q_flags) &&
1089 !bitset(QPINGONFAILURE, q->q_flags))
1093 else if (!bitset(QPRIMARY, q->q_flags))
1095 else if (bitset(QDELIVERED, q->q_flags))
1097 if (bitset(QEXPANDED, q->q_flags))
1098 action = "delivered (to mailing list)";
1100 action = "delivered (to mailbox)";
1102 else if (bitset(QRELAYED, q->q_flags))
1103 action = "relayed (to non-DSN-aware mailer)";
1104 else if (bitset(QEXPANDED, q->q_flags))
1105 action = "expanded (to multi-recipient alias)";
1106 else if (bitset(QDELAYED, q->q_flags))
1108 else if (bitset(QBYTRACE, q->q_flags))
1109 action = "relayed (Deliver-By trace mode)";
1110 else if (bitset(QBYNDELAY, q->q_flags))
1111 action = "delayed (Deliver-By notify mode)";
1112 else if (bitset(QBYNRELAY, q->q_flags))
1113 action = "relayed (Deliver-By notify mode)";
1119 /* Original-Recipient: -- passed from on high */
1120 if (q->q_orcpt != NULL)
1122 (void) sm_snprintf(buf, sizeof buf,
1123 "Original-Recipient: %.800s",
1128 /* Figure out actual recipient */
1130 if (q->q_user[0] != '\0')
1132 if (q->q_mailer != NULL &&
1133 q->q_mailer->m_addrtype != NULL)
1134 p = q->q_mailer->m_addrtype;
1138 if (sm_strcasecmp(p, "rfc822") == 0 &&
1139 strchr(q->q_user, '@') == NULL)
1141 (void) sm_snprintf(actual,
1143 "%s; %.700s@%.100s",
1149 (void) sm_snprintf(actual,
1156 /* Final-Recipient: -- the name from the RCPT command */
1157 if (q->q_finalrcpt == NULL)
1159 /* should never happen */
1160 sm_syslog(LOG_ERR, e->e_id,
1161 "returntosender: q_finalrcpt is NULL");
1163 /* try to fall back to the actual recipient */
1164 if (actual[0] != '\0')
1165 q->q_finalrcpt = sm_rpool_strdup_x(e->e_rpool,
1169 if (q->q_finalrcpt != NULL)
1171 (void) sm_snprintf(buf, sizeof buf,
1172 "Final-Recipient: %s",
1177 /* X-Actual-Recipient: -- the real problem address */
1178 if (actual[0] != '\0' &&
1179 q->q_finalrcpt != NULL &&
1180 strcmp(actual, q->q_finalrcpt) != 0)
1182 (void) sm_snprintf(buf, sizeof buf,
1183 "X-Actual-Recipient: %s",
1188 /* Action: -- what happened? */
1189 (void) sm_strlcpyn(buf, sizeof buf, 2, "Action: ",
1193 /* Status: -- what _really_ happened? */
1194 if (q->q_status != NULL)
1196 else if (QS_IS_BADADDR(q->q_state))
1198 else if (QS_IS_QUEUEUP(q->q_state))
1202 (void) sm_strlcpyn(buf, sizeof buf, 2, "Status: ", p);
1205 /* Remote-MTA: -- who was I talking to? */
1206 if (q->q_statmta != NULL)
1208 if (q->q_mailer == NULL ||
1209 (p = q->q_mailer->m_mtatype) == NULL)
1211 (void) sm_snprintf(buf, sizeof buf,
1212 "Remote-MTA: %s; %.800s",
1214 p = &buf[strlen(buf) - 1];
1220 /* Diagnostic-Code: -- actual result from other end */
1221 if (q->q_rstatus != NULL)
1223 p = q->q_mailer->m_diagtype;
1226 (void) sm_snprintf(buf, sizeof buf,
1227 "Diagnostic-Code: %s; %.800s",
1232 /* Last-Attempt-Date: -- fine granularity */
1233 if (q->q_statdate == (time_t) 0L)
1234 q->q_statdate = curtime();
1235 (void) sm_strlcpyn(buf, sizeof buf, 2,
1236 "Last-Attempt-Date: ",
1237 arpadate(ctime(&q->q_statdate)));
1240 /* Will-Retry-Until: -- for delayed messages only */
1241 if (QS_IS_QUEUEUP(q->q_state))
1245 xdate = e->e_parent->e_ctime +
1246 TimeOuts.to_q_return[e->e_parent->e_timeoutclass];
1247 (void) sm_strlcpyn(buf, sizeof buf, 2,
1248 "Will-Retry-Until: ",
1249 arpadate(ctime(&xdate)));
1257 ** Output text of original message
1261 if (bitset(EF_HAS_DF, e->e_parent->e_flags))
1263 sendbody = !bitset(EF_NO_BODY_RETN, e->e_parent->e_flags) &&
1264 !bitset(EF_NO_BODY_RETN, e->e_flags);
1266 if (e->e_msgboundary == NULL)
1269 putline(" ----- Original message follows -----\n", mci);
1271 putline(" ----- Message header follows -----\n", mci);
1275 (void) sm_strlcpyn(buf, sizeof buf, 2, "--",
1279 (void) sm_strlcpyn(buf, sizeof buf, 2, "Content-Type: ",
1280 sendbody ? "message/rfc822"
1281 : "text/rfc822-headers");
1284 p = hvalue("Content-Transfer-Encoding",
1285 e->e_parent->e_header);
1286 if (p != NULL && sm_strcasecmp(p, "binary") != 0)
1289 bitset(EF_HAS8BIT, e->e_parent->e_flags))
1293 (void) sm_snprintf(buf, sizeof buf,
1294 "Content-Transfer-Encoding: %s",
1301 putheader(mci, e->e_parent->e_header, e->e_parent, M87F_OUTER);
1304 putbody(mci, e->e_parent, e->e_msgboundary);
1305 else if (e->e_msgboundary == NULL)
1308 putline(" ----- Message body suppressed -----", mci);
1311 else if (e->e_msgboundary == NULL)
1313 putline(" ----- No message was collected -----\n", mci);
1316 if (e->e_msgboundary != NULL)
1319 (void) sm_strlcpyn(buf, sizeof buf, 3, "--", e->e_msgboundary,
1324 (void) sm_io_flush(mci->mci_out, SM_TIME_DEFAULT);
1331 syserr("errbody: I/O error");
1334 ** SMTPTODSN -- convert SMTP to DSN status code
1337 ** smtpstat -- the smtp status code (e.g., 550).
1340 ** The DSN version of the status code.
1342 ** Storage Management:
1343 ** smtptodsn() returns a pointer to a character string literal,
1344 ** which will remain valid forever, and thus does not need to
1345 ** be copied. Current code relies on this property.
1357 case 450: /* Req mail action not taken: mailbox unavailable */
1360 case 451: /* Req action aborted: local error in processing */
1363 case 452: /* Req action not taken: insufficient sys storage */
1366 case 500: /* Syntax error, command unrecognized */
1369 case 501: /* Syntax error in parameters or arguments */
1372 case 502: /* Command not implemented */
1375 case 503: /* Bad sequence of commands */
1378 case 504: /* Command parameter not implemented */
1381 case 550: /* Req mail action not taken: mailbox unavailable */
1384 case 551: /* User not local; please try <...> */
1387 case 552: /* Req mail action aborted: exceeded storage alloc */
1390 case 553: /* Req action not taken: mailbox name not allowed */
1393 case 554: /* Transaction failed */
1397 if ((smtpstat / 100) == 2)
1399 if ((smtpstat / 100) == 4)
1404 ** XTEXTIFY -- take regular text and turn it into DSN-style xtext
1407 ** t -- the text to convert.
1408 ** taboo -- additional characters that must be encoded.
1411 ** The xtext-ified version of the same string.
1422 static char *bp = NULL;
1423 static int bplen = 0;
1428 /* figure out how long this xtext will have to be */
1430 for (p = t; *p != '\0'; p++)
1432 register int c = (*p & 0xff);
1434 /* ASCII dependence here -- this is the way the spec words it */
1435 if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(' ||
1436 strchr(taboo, c) != NULL)
1442 /* since nbogus is ssize_t and wrapped, 2 * size_t would wrap */
1443 syserr("!xtextify string too long");
1447 l += nbogus * 2 + 1;
1449 /* now allocate space if necessary for the new string */
1453 sm_free(bp); /* XXX */
1454 bp = sm_pmalloc_x(l);
1458 /* ok, copy the text with byte expansion */
1459 for (p = bp; *t != '\0'; )
1461 register int c = (*t++ & 0xff);
1463 /* ASCII dependence here -- this is the way the spec words it */
1464 if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(' ||
1465 strchr(taboo, c) != NULL)
1468 *p++ = "0123456789ABCDEF"[c >> 4];
1469 *p++ = "0123456789ABCDEF"[c & 0xf];
1478 ** XUNTEXTIFY -- take xtext and turn it into plain text
1481 ** t -- the xtextified text.
1484 ** The decoded text. No attempt is made to deal with
1485 ** null strings in the resulting text.
1494 static char *bp = NULL;
1495 static int bplen = 0;
1497 /* heuristic -- if no plus sign, just return the input */
1498 if (strchr(t, '+') == NULL)
1501 /* xtext is always longer than decoded text */
1506 sm_free(bp); /* XXX */
1511 /* ok, copy the text with byte compression */
1512 for (p = bp; *t != '\0'; t++)
1514 register int c = *t & 0xff;
1523 if (!isascii(c) || !isxdigit(c))
1525 /* error -- first digit is not hex */
1526 usrerr("bogus xtext: +%c", c);
1532 else if (isupper(c))
1539 if (!isascii(c) || !isxdigit(c))
1541 /* error -- second digit is not hex */
1542 usrerr("bogus xtext: +%x%c", *p >> 4, c);
1548 else if (isupper(c))
1558 ** XTEXTOK -- check if a string is legal xtext
1560 ** Xtext is used in Delivery Status Notifications. The spec was
1561 ** taken from RFC 1891, ``SMTP Service Extension for Delivery
1562 ** Status Notifications''.
1565 ** s -- the string to check.
1568 ** true -- if 's' is legal xtext.
1569 ** false -- if it has any illegal characters in it.
1578 while ((c = *s++) != '\0')
1583 if (!isascii(c) || !isxdigit(c))
1586 if (!isascii(c) || !isxdigit(c))
1589 else if (c < '!' || c > '~' || c == '=')
1595 ** PRUNEROUTE -- prune an RFC-822 source route
1597 ** Trims down a source route to the last internet-registered hop.
1598 ** This is encouraged by RFC 1123 section 5.3.3.
1601 ** addr -- the address
1604 ** true -- address was modified
1605 ** false -- address could not be pruned
1608 ** modifies addr in-place
1616 char *start, *at, *comma;
1621 char hostbuf[BUFSIZ];
1622 char *mxhosts[MAXMXHOSTS + 1];
1624 /* check to see if this is really a route-addr */
1625 if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
1629 ** Can't simply find the first ':' is the address might be in the
1630 ** form: "<@[IPv6:::1]:user@host>" and the first ':' in inside
1631 ** the IPv6 address.
1636 while (*start != '\0')
1638 if (*start == ':' && braclev <= 0)
1640 else if (*start == '[')
1642 else if (*start == ']' && braclev > 0)
1646 if (braclev > 0 || *start != ':')
1649 at = strrchr(addr, '@');
1650 if (at == NULL || at < start)
1653 /* slice off the angle brackets */
1655 if (i >= sizeof hostbuf)
1657 (void) sm_strlcpy(hostbuf, at + 1, sizeof hostbuf);
1658 hostbuf[i - 1] = '\0';
1660 while (start != NULL)
1662 if (getmxrr(hostbuf, mxhosts, NULL, false,
1663 &rcode, true, NULL) > 0)
1665 (void) sm_strlcpy(addr + 1, start + 1,
1671 comma = strrchr(addr, ',');
1672 if (comma != NULL && comma[1] == '@' &&
1673 strlen(comma + 2) < sizeof hostbuf)
1674 (void) sm_strlcpy(hostbuf, comma + 2, sizeof hostbuf);
1680 #endif /* NAMED_BIND */