2 * Copyright (c) 1998-2003, 2006 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.315 2012/02/27 17:43:03 gshapiro Exp $")
18 static bool 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 if (!putfromline(&mcibuf, e) ||
436 !(*e->e_puthdr)(&mcibuf, e->e_header, e,
438 !(*e->e_putbody)(&mcibuf, e, NULL) ||
439 !putline("\n", &mcibuf) ||
440 sm_io_flush(fp, SM_TIME_DEFAULT) == SM_IO_EOF ||
442 sm_io_close(fp, SM_TIME_DEFAULT) < 0)
446 int oldverb = Verbose;
448 if (OpMode != MD_DAEMON && OpMode != MD_SMTP)
451 message("Saved message in %s",
455 sm_syslog(LOG_NOTICE, e->e_id,
456 "Saved message in %s",
460 macdefine(&e->e_macro, A_PERM, 'g', p);
464 syserr("554 5.3.5 savemail: unknown state %d", state);
468 /* leave the locked queue & transcript files around */
469 loseqfile(e, "savemail panic");
472 syserr("554 savemail: cannot save rejected email anywhere");
480 ** RETURNTOSENDER -- return a message to the sender with an error.
483 ** msg -- the explanatory message.
484 ** returnq -- the queue of people to send the message to.
485 ** flags -- flags tweaking the operation:
486 ** RTSF_SENDBODY -- include body of message (otherwise
487 ** just send the header).
488 ** RTSF_PMBOUNCE -- this is a postmaster bounce.
489 ** e -- the current envelope.
492 ** zero -- if everything went ok.
493 ** else -- some error.
496 ** Returns the current message to the sender via mail.
499 #define MAXRETURNS 6 /* max depth of returning messages */
500 #define ERRORFUDGE 1024 /* nominal size of error message text */
503 returntosender(msg, returnq, flags, e)
507 register ENVELOPE *e;
510 register ENVELOPE *ee;
511 ENVELOPE *oldcur = CurEnv;
512 ENVELOPE errenvelope;
513 static int returndepth = 0;
516 char buf[MAXNAME + 1];
522 msg = "Unable to deliver mail";
526 sm_dprintf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%p, returnq=",
527 msg, returndepth, e);
528 printaddr(sm_debug_file(), returnq, true);
531 sm_dprintf("Sendq=");
532 printaddr(sm_debug_file(), e->e_sendqueue, true);
536 if (++returndepth >= MAXRETURNS)
538 if (returndepth != MAXRETURNS)
539 syserr("554 5.3.0 returntosender: infinite recursion on %s",
541 /* don't "unrecurse" and fake a clean exit */
546 macdefine(&e->e_macro, A_PERM, 'g', e->e_sender);
547 macdefine(&e->e_macro, A_PERM, 'u', NULL);
549 /* initialize error envelope */
550 ee = newenvelope(&errenvelope, e, sm_rpool_new_x(NULL));
551 macdefine(&ee->e_macro, A_PERM, 'a', "\201b");
552 macdefine(&ee->e_macro, A_PERM, 'r', "");
553 macdefine(&ee->e_macro, A_PERM, 's', "localhost");
554 macdefine(&ee->e_macro, A_PERM, '_', "localhost");
557 ee->e_puthdr = putheader;
558 ee->e_putbody = errbody;
559 ee->e_flags |= EF_RESPONSE|EF_METOO;
560 if (!bitset(EF_OLDSTYLE, e->e_flags))
561 ee->e_flags &= ~EF_OLDSTYLE;
562 if (bitset(EF_DONT_MIME, e->e_flags))
564 ee->e_flags |= EF_DONT_MIME;
567 ** If we can't convert to MIME and we don't pass
568 ** 8-bit, we can't send the body.
571 if (bitset(EF_HAS8BIT, e->e_flags) &&
572 !bitset(MM_PASS8BIT, MimeMode))
573 flags &= ~RTSF_SEND_BODY;
576 ee->e_sendqueue = returnq;
578 if (bitset(RTSF_SEND_BODY, flags) &&
579 !bitset(PRIV_NOBODYRETN, PrivacyFlags))
580 ee->e_msgsize = ERRORFUDGE + e->e_msgsize;
582 ee->e_flags |= EF_NO_BODY_RETN;
584 if (!setnewqueue(ee))
586 syserr("554 5.3.0 returntosender: cannot select queue for %s",
588 ExitStat = EX_UNAVAILABLE;
595 _res.retry = TimeOuts.res_retry[RES_TO_FIRST];
596 _res.retrans = TimeOuts.res_retrans[RES_TO_FIRST];
597 #endif /* NAMED_BIND */
598 for (q = returnq; q != NULL; q = q->q_next)
600 if (QS_IS_BADADDR(q->q_state))
603 q->q_flags &= ~(QHASNOTIFY|Q_PINGFLAGS);
604 q->q_flags |= QPINGONFAILURE;
606 if (!QS_IS_DEAD(q->q_state))
609 if (q->q_alias == NULL)
610 addheader("To", q->q_paddr, 0, ee, true);
615 if (bitset(EF_RESPONSE, e->e_flags))
616 p = "return to sender";
617 else if (bitset(EF_WARNING, e->e_flags))
619 else if (bitset(RTSF_PM_BOUNCE, flags))
620 p = "postmaster notify";
623 sm_syslog(LOG_INFO, e->e_id, "%s: %s: %s",
624 ee->e_id, p, shortenstring(msg, MAXSHORTSTR));
629 addheader("MIME-Version", "1.0", 0, ee, true);
630 (void) sm_snprintf(buf, sizeof(buf), "%s.%ld/%.100s",
631 ee->e_id, (long)curtime(), MyHostName);
632 ee->e_msgboundary = sm_rpool_strdup_x(ee->e_rpool, buf);
633 (void) sm_snprintf(buf, sizeof(buf),
635 "multipart/report; report-type=delivery-status;\n\tboundary=\"%s\"",
637 "multipart/mixed; boundary=\"%s\"",
640 addheader("Content-Type", buf, 0, ee, true);
642 p = hvalue("Content-Transfer-Encoding", e->e_header);
643 if (p != NULL && sm_strcasecmp(p, "binary") != 0)
645 if (p == NULL && bitset(EF_HAS8BIT, e->e_flags))
648 addheader("Content-Transfer-Encoding", p, 0, ee, true);
650 if (strncmp(msg, "Warning:", 8) == 0)
652 addheader("Subject", msg, 0, ee, true);
653 p = "warning-timeout";
655 else if (strncmp(msg, "Postmaster warning:", 19) == 0)
657 addheader("Subject", msg, 0, ee, true);
658 p = "postmaster-warning";
660 else if (strcmp(msg, "Return receipt") == 0)
662 addheader("Subject", msg, 0, ee, true);
663 p = "return-receipt";
665 else if (bitset(RTSF_PM_BOUNCE, flags))
667 (void) sm_snprintf(buf, sizeof(buf),
668 "Postmaster notify: see transcript for details");
669 addheader("Subject", buf, 0, ee, true);
670 p = "postmaster-notification";
674 (void) sm_snprintf(buf, sizeof(buf),
675 "Returned mail: see transcript for details");
676 addheader("Subject", buf, 0, ee, true);
679 (void) sm_snprintf(buf, sizeof(buf), "auto-generated (%s)", p);
680 addheader("Auto-Submitted", buf, 0, ee, true);
682 /* fake up an address header for the from person */
683 expand("\201n", buf, sizeof(buf), e);
684 if (parseaddr(buf, &ee->e_from,
685 RF_COPYALL|RF_SENDERADDR, '\0', NULL, e, false) == NULL)
687 syserr("553 5.3.5 Can't parse myself!");
688 ExitStat = EX_SOFTWARE;
692 ee->e_from.q_flags &= ~(QHASNOTIFY|Q_PINGFLAGS);
693 ee->e_from.q_flags |= QPINGONFAILURE;
694 ee->e_sender = ee->e_from.q_paddr;
696 /* push state into submessage */
698 macdefine(&ee->e_macro, A_PERM, 'f', "\201n");
699 macdefine(&ee->e_macro, A_PERM, 'x', "Mail Delivery Subsystem");
700 eatheader(ee, true, true);
702 /* mark statistics */
703 markstats(ee, NULLADDR, STATS_NORMAL);
705 /* actually deliver the error message */
706 sendall(ee, SM_DELIVER);
707 (void) dropenvelope(ee, true, false);
709 /* check for delivery errors */
711 if (ee->e_parent == NULL ||
712 !bitset(EF_RESPONSE, ee->e_parent->e_flags))
718 for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
720 if (QS_IS_ATTEMPTED(q->q_state))
729 sm_rpool_free(ee->e_rpool);
737 ** ERRBODY -- output the body of an error message.
739 ** Typically this is a copy of the transcript plus a copy of the
740 ** original offending message.
743 ** mci -- the mailer connection information.
744 ** e -- the envelope we are working in.
745 ** separator -- any possible MIME separator (unused).
748 ** true iff body was written successfully
751 ** Outputs the body of an error message.
756 errbody(mci, e, separator)
758 register ENVELOPE *e;
765 register SM_FILE_T *xfile;
767 register ADDRESS *q = NULL;
768 char actual[MAXLINE];
771 if (bitset(MCIF_INHEADER, mci->mci_flags))
773 if (!putline("", mci))
775 mci->mci_flags &= ~MCIF_INHEADER;
777 if (e->e_parent == NULL)
779 syserr("errbody: null parent");
780 if (!putline(" ----- Original message lost -----\n", mci))
786 ** Output MIME header.
789 if (e->e_msgboundary != NULL)
791 (void) sm_strlcpyn(buf, sizeof(buf), 2, "--", e->e_msgboundary);
792 if (!putline("This is a MIME-encapsulated message", mci) ||
794 !putline(buf, mci) ||
800 ** Output introductory information.
804 p = hvalue("subject", e->e_header);
805 if (p != NULL && strncmp(p, "Postmaster ", 11) == 0)
809 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
811 if (QS_IS_BADADDR(q->q_state))
815 if (!pm_notify && q == NULL &&
816 !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
818 if (!putline(" **********************************************",
820 !putline(" ** THIS IS A WARNING MESSAGE ONLY **",
822 !putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **",
824 !putline(" **********************************************",
829 (void) sm_snprintf(buf, sizeof(buf),
830 "The original message was received at %s",
831 arpadate(ctime(&e->e_parent->e_ctime)));
832 if (!putline(buf, mci))
834 expand("from \201_", buf, sizeof(buf), e->e_parent);
835 if (!putline(buf, mci))
838 /* include id in postmaster copies */
839 if (pm_notify && e->e_parent->e_id != NULL)
841 (void) sm_strlcpyn(buf, sizeof(buf), 2, "with id ",
843 if (!putline(buf, mci))
846 if (!putline("", mci))
850 ** Output error message header (if specified and available).
853 if (ErrMsgFile != NULL &&
854 !bitset(EF_SENDRECEIPT, e->e_parent->e_flags))
856 if (*ErrMsgFile == '/')
858 long sff = SFF_ROOTOK|SFF_REGONLY;
860 if (DontLockReadFiles)
862 if (!bitnset(DBS_ERRORHEADERINUNSAFEDIRPATH,
864 sff |= SFF_SAFEDIRPATH;
865 xfile = safefopen(ErrMsgFile, O_RDONLY, 0444, sff);
868 while (sm_io_fgets(xfile, SM_TIME_DEFAULT, buf,
869 sizeof(buf)) != NULL)
876 lbp = translate_dollars(buf, buf, &lbs);
877 expand(lbp, lbp, lbs, e);
878 putok = putline(lbp, mci);
884 (void) sm_io_close(xfile, SM_TIME_DEFAULT);
885 if (!putline("\n", mci))
891 expand(ErrMsgFile, buf, sizeof(buf), e);
892 if (!putline(buf, mci) || !putline("", mci))
898 ** Output message introduction
901 /* permanent fatal errors */
903 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
905 if (!QS_IS_BADADDR(q->q_state) ||
906 !bitset(QPINGONFAILURE, q->q_flags))
911 if (!putline(" ----- The following addresses had permanent fatal errors -----",
917 (void) sm_strlcpy(buf, shortenstring(q->q_paddr, MAXSHORTSTR),
919 if (!putline(buf, mci))
921 if (q->q_rstatus != NULL)
923 (void) sm_snprintf(buf, sizeof(buf),
925 shortenstring(exitstat(q->q_rstatus),
927 if (!putline(buf, mci))
930 if (q->q_alias != NULL)
932 (void) sm_snprintf(buf, sizeof(buf),
933 " (expanded from: %s)",
934 shortenstring(q->q_alias->q_paddr,
936 if (!putline(buf, mci))
940 if (!printheader && !putline("", mci))
943 /* transient non-fatal errors */
945 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
947 if (QS_IS_BADADDR(q->q_state) ||
948 !bitset(QPRIMARY, q->q_flags) ||
949 !bitset(QBYNDELAY, q->q_flags) ||
950 !bitset(QDELAYED, q->q_flags))
955 if (!putline(" ----- The following addresses had transient non-fatal errors -----",
961 (void) sm_strlcpy(buf, shortenstring(q->q_paddr, MAXSHORTSTR),
963 if (!putline(buf, mci))
965 if (q->q_alias != NULL)
967 (void) sm_snprintf(buf, sizeof(buf),
968 " (expanded from: %s)",
969 shortenstring(q->q_alias->q_paddr,
971 if (!putline(buf, mci))
975 if (!printheader && !putline("", mci))
978 /* successful delivery notifications */
980 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
982 if (QS_IS_BADADDR(q->q_state) ||
983 !bitset(QPRIMARY, q->q_flags) ||
984 bitset(QBYNDELAY, q->q_flags) ||
985 bitset(QDELAYED, q->q_flags))
987 else if (bitset(QBYNRELAY, q->q_flags))
988 p = "Deliver-By notify: relayed";
989 else if (bitset(QBYTRACE, q->q_flags))
990 p = "Deliver-By trace: relayed";
991 else if (!bitset(QPINGONSUCCESS, q->q_flags))
993 else if (bitset(QRELAYED, q->q_flags))
994 p = "relayed to non-DSN-aware mailer";
995 else if (bitset(QDELIVERED, q->q_flags))
997 if (bitset(QEXPANDED, q->q_flags))
998 p = "successfully delivered to mailing list";
1000 p = "successfully delivered to mailbox";
1002 else if (bitset(QEXPANDED, q->q_flags))
1003 p = "expanded by alias";
1009 if (!putline(" ----- The following addresses had successful delivery notifications -----",
1012 printheader = false;
1015 (void) sm_snprintf(buf, sizeof(buf), "%s (%s)",
1016 shortenstring(q->q_paddr, MAXSHORTSTR), p);
1017 if (!putline(buf, mci))
1019 if (q->q_alias != NULL)
1021 (void) sm_snprintf(buf, sizeof(buf),
1022 " (expanded from: %s)",
1023 shortenstring(q->q_alias->q_paddr,
1025 if (!putline(buf, mci))
1029 if (!printheader && !putline("", mci))
1033 ** Output transcript of errors
1036 (void) sm_io_flush(smioout, SM_TIME_DEFAULT);
1037 if (e->e_parent->e_xfp == NULL)
1039 if (!putline(" ----- Transcript of session is unavailable -----\n",
1046 (void) bfrewind(e->e_parent->e_xfp);
1047 if (e->e_xfp != NULL)
1048 (void) sm_io_flush(e->e_xfp, SM_TIME_DEFAULT);
1049 while (sm_io_fgets(e->e_parent->e_xfp, SM_TIME_DEFAULT, buf,
1050 sizeof(buf)) != NULL)
1052 if (printheader && !putline(" ----- Transcript of session follows -----\n",
1055 printheader = false;
1056 if (!putline(buf, mci))
1064 ** Output machine-readable version.
1067 if (e->e_msgboundary != NULL)
1069 (void) sm_strlcpyn(buf, sizeof(buf), 2, "--", e->e_msgboundary);
1070 if (!putline("", mci) ||
1071 !putline(buf, mci) ||
1072 !putline("Content-Type: message/delivery-status", mci) ||
1077 ** Output per-message information.
1080 /* original envelope id from MAIL FROM: line */
1081 if (e->e_parent->e_envid != NULL)
1083 (void) sm_snprintf(buf, sizeof(buf),
1084 "Original-Envelope-Id: %.800s",
1085 xuntextify(e->e_parent->e_envid));
1086 if (!putline(buf, mci))
1090 /* Reporting-MTA: is us (required) */
1091 (void) sm_snprintf(buf, sizeof(buf),
1092 "Reporting-MTA: dns; %.800s", MyHostName);
1093 if (!putline(buf, mci))
1096 /* DSN-Gateway: not relevant since we are not translating */
1098 /* Received-From-MTA: shows where we got this message from */
1099 if (RealHostName != NULL)
1101 /* XXX use $s for type? */
1102 if (e->e_parent->e_from.q_mailer == NULL ||
1103 (p = e->e_parent->e_from.q_mailer->m_mtatype) == NULL)
1105 (void) sm_snprintf(buf, sizeof(buf),
1106 "Received-From-MTA: %s; %.800s",
1108 if (!putline(buf, mci))
1112 /* Arrival-Date: -- when it arrived here */
1113 (void) sm_strlcpyn(buf, sizeof(buf), 2, "Arrival-Date: ",
1114 arpadate(ctime(&e->e_parent->e_ctime)));
1115 if (!putline(buf, mci))
1118 /* Deliver-By-Date: -- when it should have been delivered */
1119 if (IS_DLVR_BY(e->e_parent))
1123 dbyd = e->e_parent->e_ctime + e->e_parent->e_deliver_by;
1124 (void) sm_strlcpyn(buf, sizeof(buf), 2,
1125 "Deliver-By-Date: ",
1126 arpadate(ctime(&dbyd)));
1127 if (!putline(buf, mci))
1132 ** Output per-address information.
1135 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
1139 if (QS_IS_BADADDR(q->q_state))
1141 /* RFC 1891, 6.2.6 (b) */
1142 if (bitset(QHASNOTIFY, q->q_flags) &&
1143 !bitset(QPINGONFAILURE, q->q_flags))
1147 else if (!bitset(QPRIMARY, q->q_flags))
1149 else if (bitset(QDELIVERED, q->q_flags))
1151 if (bitset(QEXPANDED, q->q_flags))
1152 action = "delivered (to mailing list)";
1154 action = "delivered (to mailbox)";
1156 else if (bitset(QRELAYED, q->q_flags))
1157 action = "relayed (to non-DSN-aware mailer)";
1158 else if (bitset(QEXPANDED, q->q_flags))
1159 action = "expanded (to multi-recipient alias)";
1160 else if (bitset(QDELAYED, q->q_flags))
1162 else if (bitset(QBYTRACE, q->q_flags))
1163 action = "relayed (Deliver-By trace mode)";
1164 else if (bitset(QBYNDELAY, q->q_flags))
1165 action = "delayed (Deliver-By notify mode)";
1166 else if (bitset(QBYNRELAY, q->q_flags))
1167 action = "relayed (Deliver-By notify mode)";
1171 if (!putline("", mci))
1174 /* Original-Recipient: -- passed from on high */
1175 if (q->q_orcpt != NULL)
1177 (void) sm_snprintf(buf, sizeof(buf),
1178 "Original-Recipient: %.800s",
1180 if (!putline(buf, mci))
1184 /* Figure out actual recipient */
1186 if (q->q_user[0] != '\0')
1188 if (q->q_mailer != NULL &&
1189 q->q_mailer->m_addrtype != NULL)
1190 p = q->q_mailer->m_addrtype;
1194 if (sm_strcasecmp(p, "rfc822") == 0 &&
1195 strchr(q->q_user, '@') == NULL)
1197 (void) sm_snprintf(actual,
1199 "%s; %.700s@%.100s",
1205 (void) sm_snprintf(actual,
1212 /* Final-Recipient: -- the name from the RCPT command */
1213 if (q->q_finalrcpt == NULL)
1215 /* should never happen */
1216 sm_syslog(LOG_ERR, e->e_id,
1217 "returntosender: q_finalrcpt is NULL");
1219 /* try to fall back to the actual recipient */
1220 if (actual[0] != '\0')
1221 q->q_finalrcpt = sm_rpool_strdup_x(e->e_rpool,
1225 if (q->q_finalrcpt != NULL)
1227 (void) sm_snprintf(buf, sizeof(buf),
1228 "Final-Recipient: %s",
1230 if (!putline(buf, mci))
1234 /* X-Actual-Recipient: -- the real problem address */
1235 if (actual[0] != '\0' &&
1236 q->q_finalrcpt != NULL &&
1237 !bitset(PRIV_NOACTUALRECIPIENT, PrivacyFlags) &&
1238 strcmp(actual, q->q_finalrcpt) != 0)
1240 (void) sm_snprintf(buf, sizeof(buf),
1241 "X-Actual-Recipient: %s",
1243 if (!putline(buf, mci))
1247 /* Action: -- what happened? */
1248 (void) sm_strlcpyn(buf, sizeof(buf), 2, "Action: ",
1250 if (!putline(buf, mci))
1253 /* Status: -- what _really_ happened? */
1254 if (q->q_status != NULL)
1256 else if (QS_IS_BADADDR(q->q_state))
1258 else if (QS_IS_QUEUEUP(q->q_state))
1262 (void) sm_strlcpyn(buf, sizeof(buf), 2, "Status: ", p);
1263 if (!putline(buf, mci))
1266 /* Remote-MTA: -- who was I talking to? */
1267 if (q->q_statmta != NULL)
1269 if (q->q_mailer == NULL ||
1270 (p = q->q_mailer->m_mtatype) == NULL)
1272 (void) sm_snprintf(buf, sizeof(buf),
1273 "Remote-MTA: %s; %.800s",
1275 p = &buf[strlen(buf) - 1];
1278 if (!putline(buf, mci))
1282 /* Diagnostic-Code: -- actual result from other end */
1283 if (q->q_rstatus != NULL)
1285 if (q->q_mailer == NULL ||
1286 (p = q->q_mailer->m_diagtype) == NULL)
1288 (void) sm_snprintf(buf, sizeof(buf),
1289 "Diagnostic-Code: %s; %.800s",
1291 if (!putline(buf, mci))
1295 /* Last-Attempt-Date: -- fine granularity */
1296 if (q->q_statdate == (time_t) 0L)
1297 q->q_statdate = curtime();
1298 (void) sm_strlcpyn(buf, sizeof(buf), 2,
1299 "Last-Attempt-Date: ",
1300 arpadate(ctime(&q->q_statdate)));
1301 if (!putline(buf, mci))
1304 /* Will-Retry-Until: -- for delayed messages only */
1305 if (QS_IS_QUEUEUP(q->q_state))
1309 xdate = e->e_parent->e_ctime +
1310 TimeOuts.to_q_return[e->e_parent->e_timeoutclass];
1311 (void) sm_strlcpyn(buf, sizeof(buf), 2,
1312 "Will-Retry-Until: ",
1313 arpadate(ctime(&xdate)));
1314 if (!putline(buf, mci))
1322 ** Output text of original message
1325 if (!putline("", mci))
1327 if (bitset(EF_HAS_DF, e->e_parent->e_flags))
1329 sendbody = !bitset(EF_NO_BODY_RETN, e->e_parent->e_flags) &&
1330 !bitset(EF_NO_BODY_RETN, e->e_flags);
1332 if (e->e_msgboundary == NULL)
1336 ? " ----- Original message follows -----\n"
1337 : " ----- Message header follows -----\n",
1345 (void) sm_strlcpyn(buf, sizeof(buf), 2, "--",
1348 if (!putline(buf, mci))
1350 (void) sm_strlcpyn(buf, sizeof(buf), 2, "Content-Type: ",
1351 sendbody ? "message/rfc822"
1352 : "text/rfc822-headers");
1353 if (!putline(buf, mci))
1356 p = hvalue("Content-Transfer-Encoding",
1357 e->e_parent->e_header);
1358 if (p != NULL && sm_strcasecmp(p, "binary") != 0)
1361 bitset(EF_HAS8BIT, e->e_parent->e_flags))
1365 (void) sm_snprintf(buf, sizeof(buf),
1366 "Content-Transfer-Encoding: %s",
1368 if (!putline(buf, mci))
1372 if (!putline("", mci))
1375 if (!putheader(mci, e->e_parent->e_header, e->e_parent,
1381 if (!putbody(mci, e->e_parent, e->e_msgboundary))
1384 else if (e->e_msgboundary == NULL)
1386 if (!putline("", mci) ||
1387 !putline(" ----- Message body suppressed -----",
1394 else if (e->e_msgboundary == NULL)
1396 if (!putline(" ----- No message was collected -----\n", mci))
1400 if (e->e_msgboundary != NULL)
1402 (void) sm_strlcpyn(buf, sizeof(buf), 3, "--", e->e_msgboundary,
1404 if (!putline("", mci) || !putline(buf, mci))
1407 if (!putline("", mci) ||
1408 sm_io_flush(mci->mci_out, SM_TIME_DEFAULT) == SM_IO_EOF)
1418 syserr("errbody: I/O error");
1425 ** SMTPTODSN -- convert SMTP to DSN status code
1428 ** smtpstat -- the smtp status code (e.g., 550).
1431 ** The DSN version of the status code.
1433 ** Storage Management:
1434 ** smtptodsn() returns a pointer to a character string literal,
1435 ** which will remain valid forever, and thus does not need to
1436 ** be copied. Current code relies on this property.
1448 case 450: /* Req mail action not taken: mailbox unavailable */
1451 case 451: /* Req action aborted: local error in processing */
1454 case 452: /* Req action not taken: insufficient sys storage */
1457 case 500: /* Syntax error, command unrecognized */
1460 case 501: /* Syntax error in parameters or arguments */
1463 case 502: /* Command not implemented */
1466 case 503: /* Bad sequence of commands */
1469 case 504: /* Command parameter not implemented */
1472 case 550: /* Req mail action not taken: mailbox unavailable */
1475 case 551: /* User not local; please try <...> */
1478 case 552: /* Req mail action aborted: exceeded storage alloc */
1481 case 553: /* Req action not taken: mailbox name not allowed */
1484 case 554: /* Transaction failed */
1488 if (REPLYTYPE(smtpstat) == 2)
1490 if (REPLYTYPE(smtpstat) == 4)
1495 ** XTEXTIFY -- take regular text and turn it into DSN-style xtext
1498 ** t -- the text to convert.
1499 ** taboo -- additional characters that must be encoded.
1502 ** The xtext-ified version of the same string.
1513 static char *bp = NULL;
1514 static int bplen = 0;
1519 /* figure out how long this xtext will have to be */
1521 for (p = t; *p != '\0'; p++)
1523 register int c = (*p & 0xff);
1525 /* ASCII dependence here -- this is the way the spec words it */
1526 if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(' ||
1527 strchr(taboo, c) != NULL)
1533 /* since nbogus is ssize_t and wrapped, 2 * size_t would wrap */
1534 syserr("!xtextify string too long");
1538 l += nbogus * 2 + 1;
1540 /* now allocate space if necessary for the new string */
1544 sm_free(bp); /* XXX */
1545 bp = sm_pmalloc_x(l);
1549 /* ok, copy the text with byte expansion */
1550 for (p = bp; *t != '\0'; )
1552 register int c = (*t++ & 0xff);
1554 /* ASCII dependence here -- this is the way the spec words it */
1555 if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(' ||
1556 strchr(taboo, c) != NULL)
1559 *p++ = "0123456789ABCDEF"[c >> 4];
1560 *p++ = "0123456789ABCDEF"[c & 0xf];
1569 ** XUNTEXTIFY -- take xtext and turn it into plain text
1572 ** t -- the xtextified text.
1575 ** The decoded text. No attempt is made to deal with
1576 ** null strings in the resulting text.
1585 static char *bp = NULL;
1586 static int bplen = 0;
1588 /* heuristic -- if no plus sign, just return the input */
1589 if (strchr(t, '+') == NULL)
1592 /* xtext is always longer than decoded text */
1597 sm_free(bp); /* XXX */
1602 /* ok, copy the text with byte compression */
1603 for (p = bp; *t != '\0'; t++)
1605 register int c = *t & 0xff;
1614 if (!isascii(c) || !isxdigit(c))
1616 /* error -- first digit is not hex */
1617 usrerr("bogus xtext: +%c", c);
1623 else if (isupper(c))
1630 if (!isascii(c) || !isxdigit(c))
1632 /* error -- second digit is not hex */
1633 usrerr("bogus xtext: +%x%c", *p >> 4, c);
1639 else if (isupper(c))
1649 ** XTEXTOK -- check if a string is legal xtext
1651 ** Xtext is used in Delivery Status Notifications. The spec was
1652 ** taken from RFC 1891, ``SMTP Service Extension for Delivery
1653 ** Status Notifications''.
1656 ** s -- the string to check.
1659 ** true -- if 's' is legal xtext.
1660 ** false -- if it has any illegal characters in it.
1669 while ((c = *s++) != '\0')
1674 if (!isascii(c) || !isxdigit(c))
1677 if (!isascii(c) || !isxdigit(c))
1680 else if (c < '!' || c > '~' || c == '=')
1686 ** PRUNEROUTE -- prune an RFC-822 source route
1688 ** Trims down a source route to the last internet-registered hop.
1689 ** This is encouraged by RFC 1123 section 5.3.3.
1692 ** addr -- the address
1695 ** true -- address was modified
1696 ** false -- address could not be pruned
1699 ** modifies addr in-place
1707 char *start, *at, *comma;
1712 char hostbuf[BUFSIZ];
1713 char *mxhosts[MAXMXHOSTS + 1];
1715 /* check to see if this is really a route-addr */
1716 if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
1720 ** Can't simply find the first ':' is the address might be in the
1721 ** form: "<@[IPv6:::1]:user@host>" and the first ':' in inside
1722 ** the IPv6 address.
1727 while (*start != '\0')
1729 if (*start == ':' && braclev <= 0)
1731 else if (*start == '[')
1733 else if (*start == ']' && braclev > 0)
1737 if (braclev > 0 || *start != ':')
1740 at = strrchr(addr, '@');
1741 if (at == NULL || at < start)
1744 /* slice off the angle brackets */
1746 if (i >= sizeof(hostbuf))
1748 (void) sm_strlcpy(hostbuf, at + 1, sizeof(hostbuf));
1749 hostbuf[i - 1] = '\0';
1751 while (start != NULL)
1753 if (getmxrr(hostbuf, mxhosts, NULL, false,
1754 &rcode, true, NULL) > 0)
1756 (void) sm_strlcpy(addr + 1, start + 1,
1762 comma = strrchr(addr, ',');
1763 if (comma != NULL && comma[1] == '@' &&
1764 strlen(comma + 2) < sizeof(hostbuf))
1765 (void) sm_strlcpy(hostbuf, comma + 2, sizeof(hostbuf));
1771 #endif /* NAMED_BIND */