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.
18 SM_RCSID("@(#)$Id: savemail.c,v 8.306 2006/02/25 02:16:53 ca Exp $")
20 static bool errbody __P((MCI *, ENVELOPE *, char *));
21 static bool pruneroute __P((char *));
24 ** SAVEMAIL -- Save mail on error
26 ** If mailing back errors, mail it back to the originator
27 ** together with an error message; otherwise, just put it in
28 ** dead.letter in the user's home directory (if he exists on
32 ** e -- the envelope containing the message in error.
33 ** sendbody -- if true, also send back the body of the
34 ** message; otherwise just send the header.
37 ** true if savemail panic'ed, (i.e., the data file should
38 ** be preserved by dropenvelope())
41 ** Saves the letter, by writing or mailing it back to the
42 ** sender, or by putting it in dead.letter in her home
46 /* defines for state machine */
47 #define ESM_REPORT 0 /* report to sender's terminal */
48 #define ESM_MAIL 1 /* mail back to sender */
49 #define ESM_QUIET 2 /* mail has already been returned */
50 #define ESM_DEADLETTER 3 /* save in ~/dead.letter */
51 #define ESM_POSTMASTER 4 /* return to postmaster */
52 #define ESM_DEADLETTERDROP 5 /* save in DeadLetterDrop */
53 #define ESM_PANIC 6 /* call loseqfile() */
54 #define ESM_DONE 7 /* message is successfully delivered */
61 register SM_FILE_T *fp;
64 auto ADDRESS *q = NULL;
69 char buf[MAXLINE + 1];
70 char dlbuf[MAXPATHLEN];
76 sm_dprintf("\nsavemail, errormode = %c, id = %s, ExitStat = %d\n e_from=",
77 e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id,
79 printaddr(sm_debug_file(), &e->e_from, false);
84 /* can't return a message with no id */
89 ** In the unhappy event we don't know who to return the mail
90 ** to, make someone up.
93 if (e->e_from.q_paddr == NULL)
95 e->e_sender = "Postmaster";
96 if (parseaddr(e->e_sender, &e->e_from,
97 RF_COPYPARSE|RF_SENDERADDR,
98 '\0', NULL, e, false) == NULL)
100 syserr("553 5.3.5 Cannot parse Postmaster!");
101 finis(true, true, EX_SOFTWARE);
107 ** Basic state machine.
109 ** This machine runs through the following states:
111 ** ESM_QUIET Errors have already been printed iff the
113 ** ESM_REPORT Report directly to the sender's terminal.
114 ** ESM_MAIL Mail response to the sender.
115 ** ESM_DEADLETTER Save response in ~/dead.letter.
116 ** ESM_POSTMASTER Mail response to the postmaster.
117 ** ESM_DEADLETTERDROP
118 ** If DeadLetterDrop set, save it there.
119 ** ESM_PANIC Save response anywhere possible.
122 /* determine starting state */
123 switch (e->e_errormode)
140 /* no need to return anything at all */
144 syserr("554 5.3.0 savemail: bogus errormode x%x",
150 /* if this is already an error response, send to postmaster */
151 if (bitset(EF_RESPONSE, e->e_flags))
153 if (e->e_parent != NULL &&
154 bitset(EF_RESPONSE, e->e_parent->e_flags))
156 /* got an error sending a response -- can it */
159 state = ESM_POSTMASTER;
162 while (state != ESM_DONE)
165 sm_dprintf(" state %d\n", state);
170 if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags))
171 state = ESM_DEADLETTER;
179 ** If the user is still logged in on the same terminal,
180 ** then write the error messages back to hir (sic).
185 #else /* USE_TTYPATH */
187 #endif /* USE_TTYPATH */
189 if (p == NULL || sm_io_reopen(SmFtStdio,
191 p, SM_IO_WRONLY, NULL,
198 expand("\201n", buf, sizeof buf, e);
199 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
200 "\r\nMessage from %s...\r\n", buf);
201 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
202 "Errors occurred while sending mail.\r\n");
203 if (e->e_xfp != NULL)
205 (void) bfrewind(e->e_xfp);
206 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
207 "Transcript follows:\r\n");
208 while (sm_io_fgets(e->e_xfp, SM_TIME_DEFAULT,
209 buf, sizeof buf) != NULL &&
210 !sm_io_error(smioout))
211 (void) sm_io_fputs(smioout,
217 syserr("Cannot open %s",
218 queuename(e, XSCRPT_LETTER));
219 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
220 "Transcript of session is unavailable.\r\n");
222 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
223 "Original message will be saved in dead.letter.\r\n");
224 state = ESM_DEADLETTER;
229 ** If mailing back, do it.
230 ** Throw away all further output. Don't alias,
231 ** since this could cause loops, e.g., if joe
232 ** mails to joe@x, and for some reason the network
233 ** for @x is down, then the response gets sent to
234 ** joe@x, which gives a response, etc. Also force
235 ** the mail to be delivered even if a version of
236 ** it has already been sent to the sender.
238 ** If this is a configuration or local software
239 ** error, send to the local postmaster as well,
240 ** since the originator can't do anything
241 ** about it anyway. Note that this is a full
242 ** copy of the message (intentionally) so that
243 ** the Postmaster can forward things along.
246 if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
248 (void) sendtolist("postmaster", NULLADDR,
249 &e->e_errorqueue, 0, e);
251 if (!emptyaddr(&e->e_from))
253 char from[TOBUFSIZE];
255 if (sm_strlcpy(from, e->e_from.q_paddr,
256 sizeof from) >= sizeof from)
258 state = ESM_POSTMASTER;
262 if (!DontPruneRoutes)
263 (void) pruneroute(from);
265 (void) sendtolist(from, NULLADDR,
266 &e->e_errorqueue, 0, e);
270 ** Deliver a non-delivery report to the
271 ** Postmaster-designate (not necessarily
272 ** Postmaster). This does not include the
273 ** body of the message, for privacy reasons.
274 ** You really shouldn't need this.
277 e->e_flags |= EF_PM_NOTIFY;
279 /* check to see if there are any good addresses */
280 for (q = e->e_errorqueue; q != NULL; q = q->q_next)
282 if (QS_IS_SENDABLE(q->q_state))
287 /* this is an error-error */
288 state = ESM_POSTMASTER;
291 if (returntosender(e->e_message, e->e_errorqueue,
292 sendbody ? RTSF_SEND_BODY
300 /* didn't work -- return to postmaster */
301 state = ESM_POSTMASTER;
306 ** Similar to previous case, but to system postmaster.
310 expand(DoubleBounceAddr, buf, sizeof buf, e);
313 ** Just drop it on the floor if DoubleBounceAddr
314 ** expands to an empty string.
322 if (sendtolist(buf, NULLADDR, &q, 0, e) <= 0)
324 syserr("553 5.3.0 cannot parse %s!", buf);
325 ExitStat = EX_SOFTWARE;
326 state = ESM_DEADLETTERDROP;
329 flags = RTSF_PM_BOUNCE;
331 flags |= RTSF_SEND_BODY;
332 if (returntosender(e->e_message, q, flags, e) == 0)
338 /* didn't work -- last resort */
339 state = ESM_DEADLETTERDROP;
344 ** Save the message in dead.letter.
345 ** If we weren't mailing back, and the user is
346 ** local, we should save the message in
347 ** ~/dead.letter so that the poor person doesn't
348 ** have to type it over again -- and we all know
349 ** what poor typists UNIX users are.
353 if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
355 if (e->e_from.q_home != NULL)
356 p = e->e_from.q_home;
357 else if (sm_mbdb_lookup(e->e_from.q_user, &user)
359 *user.mbdb_homedir != '\0')
360 p = user.mbdb_homedir;
362 if (p == NULL || e->e_dfp == NULL)
364 /* no local directory or no data file */
369 /* we have a home directory; write dead.letter */
370 macdefine(&e->e_macro, A_TEMP, 'z', p);
372 /* get the sender for the UnixFromLine */
373 p = macvalue('g', e);
374 macdefine(&e->e_macro, A_PERM, 'g', e->e_sender);
376 expand("\201z/dead.letter", dlbuf, sizeof dlbuf, e);
377 sff = SFF_CREAT|SFF_REGONLY|SFF_RUNASREALUID;
381 if (writable(dlbuf, NULL, sff) &&
382 mailfile(dlbuf, FileMailer, NULL, sff, e) == EX_OK)
384 int oldverb = Verbose;
386 if (OpMode != MD_DAEMON && OpMode != MD_SMTP)
389 message("Saved message in %s", dlbuf);
391 macdefine(&e->e_macro, A_PERM, 'g', p);
395 macdefine(&e->e_macro, A_PERM, 'g', p);
399 case ESM_DEADLETTERDROP:
401 ** Log the mail in DeadLetterDrop file.
410 if ((SafeFileEnv != NULL && SafeFileEnv[0] != '\0') ||
411 DeadLetterDrop == NULL ||
412 DeadLetterDrop[0] == '\0')
418 sff = SFF_CREAT|SFF_REGONLY|SFF_ROOTOK|SFF_OPENASROOT|SFF_MUSTOWN;
419 if (!writable(DeadLetterDrop, NULL, sff) ||
420 (fp = safefopen(DeadLetterDrop, O_WRONLY|O_APPEND,
421 FileMode, sff)) == NULL)
427 memset(&mcibuf, '\0', sizeof mcibuf);
429 mcibuf.mci_mailer = FileMailer;
430 if (bitnset(M_7BITS, FileMailer->m_flags))
431 mcibuf.mci_flags |= MCIF_7BIT;
433 /* get the sender for the UnixFromLine */
434 p = macvalue('g', e);
435 macdefine(&e->e_macro, A_PERM, 'g', e->e_sender);
437 if (!putfromline(&mcibuf, e) ||
438 !(*e->e_puthdr)(&mcibuf, e->e_header, e,
440 !(*e->e_putbody)(&mcibuf, e, NULL) ||
441 !putline("\n", &mcibuf) ||
442 sm_io_flush(fp, SM_TIME_DEFAULT) == SM_IO_EOF ||
444 sm_io_close(fp, SM_TIME_DEFAULT) < 0)
448 int oldverb = Verbose;
450 if (OpMode != MD_DAEMON && OpMode != MD_SMTP)
453 message("Saved message in %s",
457 sm_syslog(LOG_NOTICE, e->e_id,
458 "Saved message in %s",
462 macdefine(&e->e_macro, A_PERM, 'g', p);
466 syserr("554 5.3.5 savemail: unknown state %d", state);
470 /* leave the locked queue & transcript files around */
471 loseqfile(e, "savemail panic");
474 syserr("554 savemail: cannot save rejected email anywhere");
482 ** RETURNTOSENDER -- return a message to the sender with an error.
485 ** msg -- the explanatory message.
486 ** returnq -- the queue of people to send the message to.
487 ** flags -- flags tweaking the operation:
488 ** RTSF_SENDBODY -- include body of message (otherwise
489 ** just send the header).
490 ** RTSF_PMBOUNCE -- this is a postmaster bounce.
491 ** e -- the current envelope.
494 ** zero -- if everything went ok.
495 ** else -- some error.
498 ** Returns the current message to the sender via mail.
501 #define MAXRETURNS 6 /* max depth of returning messages */
502 #define ERRORFUDGE 1024 /* nominal size of error message text */
505 returntosender(msg, returnq, flags, e)
509 register ENVELOPE *e;
511 register ENVELOPE *ee;
512 ENVELOPE *oldcur = CurEnv;
513 ENVELOPE errenvelope;
514 static int returndepth = 0;
517 char buf[MAXNAME + 1];
523 msg = "Unable to deliver mail";
527 sm_dprintf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%p, returnq=",
528 msg, returndepth, e);
529 printaddr(sm_debug_file(), returnq, true);
532 sm_dprintf("Sendq=");
533 printaddr(sm_debug_file(), e->e_sendqueue, true);
537 if (++returndepth >= MAXRETURNS)
539 if (returndepth != MAXRETURNS)
540 syserr("554 5.3.0 returntosender: infinite recursion on %s",
542 /* don't "unrecurse" and fake a clean exit */
547 macdefine(&e->e_macro, A_PERM, 'g', e->e_sender);
548 macdefine(&e->e_macro, A_PERM, 'u', NULL);
550 /* initialize error envelope */
551 ee = newenvelope(&errenvelope, e, sm_rpool_new_x(NULL));
552 macdefine(&ee->e_macro, A_PERM, 'a', "\201b");
553 macdefine(&ee->e_macro, A_PERM, 'r', "");
554 macdefine(&ee->e_macro, A_PERM, 's', "localhost");
555 macdefine(&ee->e_macro, A_PERM, '_', "localhost");
558 ee->e_puthdr = putheader;
559 ee->e_putbody = errbody;
560 ee->e_flags |= EF_RESPONSE|EF_METOO;
561 if (!bitset(EF_OLDSTYLE, e->e_flags))
562 ee->e_flags &= ~EF_OLDSTYLE;
563 if (bitset(EF_DONT_MIME, e->e_flags))
565 ee->e_flags |= EF_DONT_MIME;
568 ** If we can't convert to MIME and we don't pass
569 ** 8-bit, we can't send the body.
572 if (bitset(EF_HAS8BIT, e->e_flags) &&
573 !bitset(MM_PASS8BIT, MimeMode))
574 flags &= ~RTSF_SEND_BODY;
577 ee->e_sendqueue = returnq;
579 if (bitset(RTSF_SEND_BODY, flags) &&
580 !bitset(PRIV_NOBODYRETN, PrivacyFlags))
581 ee->e_msgsize = ERRORFUDGE + e->e_msgsize;
583 ee->e_flags |= EF_NO_BODY_RETN;
585 if (!setnewqueue(ee))
587 syserr("554 5.3.0 returntosender: cannot select queue for %s",
589 ExitStat = EX_UNAVAILABLE;
596 _res.retry = TimeOuts.res_retry[RES_TO_FIRST];
597 _res.retrans = TimeOuts.res_retrans[RES_TO_FIRST];
598 #endif /* NAMED_BIND */
599 for (q = returnq; q != NULL; q = q->q_next)
601 if (QS_IS_BADADDR(q->q_state))
604 q->q_flags &= ~(QHASNOTIFY|Q_PINGFLAGS);
605 q->q_flags |= QPINGONFAILURE;
607 if (!QS_IS_DEAD(q->q_state))
610 if (q->q_alias == NULL)
611 addheader("To", q->q_paddr, 0, ee);
616 if (bitset(EF_RESPONSE, e->e_flags))
617 p = "return to sender";
618 else if (bitset(EF_WARNING, e->e_flags))
620 else if (bitset(RTSF_PM_BOUNCE, flags))
621 p = "postmaster notify";
624 sm_syslog(LOG_INFO, e->e_id, "%s: %s: %s",
625 ee->e_id, p, shortenstring(msg, MAXSHORTSTR));
630 addheader("MIME-Version", "1.0", 0, ee);
631 (void) sm_snprintf(buf, sizeof buf, "%s.%ld/%.100s",
632 ee->e_id, (long)curtime(), MyHostName);
633 ee->e_msgboundary = sm_rpool_strdup_x(ee->e_rpool, buf);
634 (void) sm_snprintf(buf, sizeof buf,
636 "multipart/report; report-type=delivery-status;\n\tboundary=\"%s\"",
638 "multipart/mixed; boundary=\"%s\"",
641 addheader("Content-Type", buf, 0, ee);
643 p = hvalue("Content-Transfer-Encoding", e->e_header);
644 if (p != NULL && sm_strcasecmp(p, "binary") != 0)
646 if (p == NULL && bitset(EF_HAS8BIT, e->e_flags))
649 addheader("Content-Transfer-Encoding", p, 0, ee);
651 if (strncmp(msg, "Warning:", 8) == 0)
653 addheader("Subject", msg, 0, ee);
654 p = "warning-timeout";
656 else if (strncmp(msg, "Postmaster warning:", 19) == 0)
658 addheader("Subject", msg, 0, ee);
659 p = "postmaster-warning";
661 else if (strcmp(msg, "Return receipt") == 0)
663 addheader("Subject", msg, 0, ee);
664 p = "return-receipt";
666 else if (bitset(RTSF_PM_BOUNCE, flags))
668 (void) sm_snprintf(buf, sizeof buf,
669 "Postmaster notify: see transcript for details");
670 addheader("Subject", buf, 0, ee);
671 p = "postmaster-notification";
675 (void) sm_snprintf(buf, sizeof buf,
676 "Returned mail: see transcript for details");
677 addheader("Subject", buf, 0, ee);
680 (void) sm_snprintf(buf, sizeof buf, "auto-generated (%s)", p);
681 addheader("Auto-Submitted", buf, 0, ee);
683 /* fake up an address header for the from person */
684 expand("\201n", buf, sizeof buf, e);
685 if (parseaddr(buf, &ee->e_from,
686 RF_COPYALL|RF_SENDERADDR, '\0', NULL, e, false) == NULL)
688 syserr("553 5.3.5 Can't parse myself!");
689 ExitStat = EX_SOFTWARE;
693 ee->e_from.q_flags &= ~(QHASNOTIFY|Q_PINGFLAGS);
694 ee->e_from.q_flags |= QPINGONFAILURE;
695 ee->e_sender = ee->e_from.q_paddr;
697 /* push state into submessage */
699 macdefine(&ee->e_macro, A_PERM, 'f', "\201n");
700 macdefine(&ee->e_macro, A_PERM, 'x', "Mail Delivery Subsystem");
701 eatheader(ee, true, true);
703 /* mark statistics */
704 markstats(ee, NULLADDR, STATS_NORMAL);
706 /* actually deliver the error message */
707 sendall(ee, SM_DELIVER);
710 dropenvelope(ee, true, false);
711 sm_rpool_free(ee->e_rpool);
715 /* check for delivery errors */
716 if (ee->e_parent == NULL ||
717 !bitset(EF_RESPONSE, ee->e_parent->e_flags))
719 for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
721 if (QS_IS_ATTEMPTED(q->q_state))
727 ** ERRBODY -- output the body of an error message.
729 ** Typically this is a copy of the transcript plus a copy of the
730 ** original offending message.
733 ** mci -- the mailer connection information.
734 ** e -- the envelope we are working in.
735 ** separator -- any possible MIME separator (unused).
741 ** Outputs the body of an error message.
746 errbody(mci, e, separator)
748 register ENVELOPE *e;
755 register SM_FILE_T *xfile;
757 register ADDRESS *q = NULL;
758 char actual[MAXLINE];
761 if (bitset(MCIF_INHEADER, mci->mci_flags))
763 if (!putline("", mci))
765 mci->mci_flags &= ~MCIF_INHEADER;
767 if (e->e_parent == NULL)
769 syserr("errbody: null parent");
770 if (!putline(" ----- Original message lost -----\n", mci))
776 ** Output MIME header.
779 if (e->e_msgboundary != NULL)
781 (void) sm_strlcpyn(buf, sizeof buf, 2, "--", e->e_msgboundary);
782 if (!putline("This is a MIME-encapsulated message", mci) ||
784 !putline(buf, mci) ||
790 ** Output introductory information.
794 p = hvalue("subject", e->e_header);
795 if (p != NULL && strncmp(p, "Postmaster ", 11) == 0)
799 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
801 if (QS_IS_BADADDR(q->q_state))
805 if (!pm_notify && q == NULL &&
806 !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
808 if (!putline(" **********************************************",
810 !putline(" ** THIS IS A WARNING MESSAGE ONLY **",
812 !putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **",
814 !putline(" **********************************************",
819 (void) sm_snprintf(buf, sizeof buf,
820 "The original message was received at %s",
821 arpadate(ctime(&e->e_parent->e_ctime)));
822 if (!putline(buf, mci))
824 expand("from \201_", buf, sizeof buf, e->e_parent);
825 if (!putline(buf, mci))
828 /* include id in postmaster copies */
829 if (pm_notify && e->e_parent->e_id != NULL)
831 (void) sm_strlcpyn(buf, sizeof buf, 2, "with id ",
833 if (!putline(buf, mci))
836 if (!putline("", mci))
840 ** Output error message header (if specified and available).
843 if (ErrMsgFile != NULL &&
844 !bitset(EF_SENDRECEIPT, e->e_parent->e_flags))
846 if (*ErrMsgFile == '/')
848 long sff = SFF_ROOTOK|SFF_REGONLY;
850 if (DontLockReadFiles)
852 if (!bitnset(DBS_ERRORHEADERINUNSAFEDIRPATH,
854 sff |= SFF_SAFEDIRPATH;
855 xfile = safefopen(ErrMsgFile, O_RDONLY, 0444, sff);
858 while (sm_io_fgets(xfile, SM_TIME_DEFAULT, buf,
861 translate_dollars(buf);
862 expand(buf, buf, sizeof buf, e);
863 if (!putline(buf, mci))
866 (void) sm_io_close(xfile, SM_TIME_DEFAULT);
867 if (!putline("\n", mci))
873 expand(ErrMsgFile, buf, sizeof buf, e);
874 if (!putline(buf, mci) || !putline("", mci))
880 ** Output message introduction
883 /* permanent fatal errors */
885 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
887 if (!QS_IS_BADADDR(q->q_state) ||
888 !bitset(QPINGONFAILURE, q->q_flags))
893 if (!putline(" ----- The following addresses had permanent fatal errors -----",
899 (void) sm_strlcpy(buf, shortenstring(q->q_paddr, MAXSHORTSTR),
901 if (!putline(buf, mci))
903 if (q->q_rstatus != NULL)
905 (void) sm_snprintf(buf, sizeof buf,
907 shortenstring(exitstat(q->q_rstatus),
909 if (!putline(buf, mci))
912 if (q->q_alias != NULL)
914 (void) sm_snprintf(buf, sizeof buf,
915 " (expanded from: %s)",
916 shortenstring(q->q_alias->q_paddr,
918 if (!putline(buf, mci))
922 if (!printheader && !putline("", mci))
925 /* transient non-fatal errors */
927 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
929 if (QS_IS_BADADDR(q->q_state) ||
930 !bitset(QPRIMARY, q->q_flags) ||
931 !bitset(QBYNDELAY, q->q_flags) ||
932 !bitset(QDELAYED, q->q_flags))
937 if (!putline(" ----- The following addresses had transient non-fatal errors -----",
943 (void) sm_strlcpy(buf, shortenstring(q->q_paddr, MAXSHORTSTR),
945 if (!putline(buf, mci))
947 if (q->q_alias != NULL)
949 (void) sm_snprintf(buf, sizeof buf,
950 " (expanded from: %s)",
951 shortenstring(q->q_alias->q_paddr,
953 if (!putline(buf, mci))
957 if (!printheader && !putline("", mci))
960 /* successful delivery notifications */
962 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
964 if (QS_IS_BADADDR(q->q_state) ||
965 !bitset(QPRIMARY, q->q_flags) ||
966 bitset(QBYNDELAY, q->q_flags) ||
967 bitset(QDELAYED, q->q_flags))
969 else if (bitset(QBYNRELAY, q->q_flags))
970 p = "Deliver-By notify: relayed";
971 else if (bitset(QBYTRACE, q->q_flags))
972 p = "Deliver-By trace: relayed";
973 else if (!bitset(QPINGONSUCCESS, q->q_flags))
975 else if (bitset(QRELAYED, q->q_flags))
976 p = "relayed to non-DSN-aware mailer";
977 else if (bitset(QDELIVERED, q->q_flags))
979 if (bitset(QEXPANDED, q->q_flags))
980 p = "successfully delivered to mailing list";
982 p = "successfully delivered to mailbox";
984 else if (bitset(QEXPANDED, q->q_flags))
985 p = "expanded by alias";
991 if (!putline(" ----- The following addresses had successful delivery notifications -----",
997 (void) sm_snprintf(buf, sizeof buf, "%s (%s)",
998 shortenstring(q->q_paddr, MAXSHORTSTR), p);
999 if (!putline(buf, mci))
1001 if (q->q_alias != NULL)
1003 (void) sm_snprintf(buf, sizeof buf,
1004 " (expanded from: %s)",
1005 shortenstring(q->q_alias->q_paddr,
1007 if (!putline(buf, mci))
1011 if (!printheader && !putline("", mci))
1015 ** Output transcript of errors
1018 (void) sm_io_flush(smioout, SM_TIME_DEFAULT);
1019 if (e->e_parent->e_xfp == NULL)
1021 if (!putline(" ----- Transcript of session is unavailable -----\n",
1028 (void) bfrewind(e->e_parent->e_xfp);
1029 if (e->e_xfp != NULL)
1030 (void) sm_io_flush(e->e_xfp, SM_TIME_DEFAULT);
1031 while (sm_io_fgets(e->e_parent->e_xfp, SM_TIME_DEFAULT, buf,
1032 sizeof buf) != NULL)
1034 if (printheader && !putline(" ----- Transcript of session follows -----\n",
1037 printheader = false;
1038 if (!putline(buf, mci))
1046 ** Output machine-readable version.
1049 if (e->e_msgboundary != NULL)
1051 (void) sm_strlcpyn(buf, sizeof buf, 2, "--", e->e_msgboundary);
1052 if (!putline("", mci) ||
1053 !putline(buf, mci) ||
1054 !putline("Content-Type: message/delivery-status", mci) ||
1059 ** Output per-message information.
1062 /* original envelope id from MAIL FROM: line */
1063 if (e->e_parent->e_envid != NULL)
1065 (void) sm_snprintf(buf, sizeof buf,
1066 "Original-Envelope-Id: %.800s",
1067 xuntextify(e->e_parent->e_envid));
1068 if (!putline(buf, mci))
1072 /* Reporting-MTA: is us (required) */
1073 (void) sm_snprintf(buf, sizeof buf,
1074 "Reporting-MTA: dns; %.800s", MyHostName);
1075 if (!putline(buf, mci))
1078 /* DSN-Gateway: not relevant since we are not translating */
1080 /* Received-From-MTA: shows where we got this message from */
1081 if (RealHostName != NULL)
1083 /* XXX use $s for type? */
1084 if (e->e_parent->e_from.q_mailer == NULL ||
1085 (p = e->e_parent->e_from.q_mailer->m_mtatype) == NULL)
1087 (void) sm_snprintf(buf, sizeof buf,
1088 "Received-From-MTA: %s; %.800s",
1090 if (!putline(buf, mci))
1094 /* Arrival-Date: -- when it arrived here */
1095 (void) sm_strlcpyn(buf, sizeof buf, 2, "Arrival-Date: ",
1096 arpadate(ctime(&e->e_parent->e_ctime)));
1097 if (!putline(buf, mci))
1100 /* Deliver-By-Date: -- when it should have been delivered */
1101 if (IS_DLVR_BY(e->e_parent))
1105 dbyd = e->e_parent->e_ctime + e->e_parent->e_deliver_by;
1106 (void) sm_strlcpyn(buf, sizeof buf, 2,
1107 "Deliver-By-Date: ",
1108 arpadate(ctime(&dbyd)));
1109 if (!putline(buf, mci))
1114 ** Output per-address information.
1117 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
1121 if (QS_IS_BADADDR(q->q_state))
1123 /* RFC 1891, 6.2.6 (b) */
1124 if (bitset(QHASNOTIFY, q->q_flags) &&
1125 !bitset(QPINGONFAILURE, q->q_flags))
1129 else if (!bitset(QPRIMARY, q->q_flags))
1131 else if (bitset(QDELIVERED, q->q_flags))
1133 if (bitset(QEXPANDED, q->q_flags))
1134 action = "delivered (to mailing list)";
1136 action = "delivered (to mailbox)";
1138 else if (bitset(QRELAYED, q->q_flags))
1139 action = "relayed (to non-DSN-aware mailer)";
1140 else if (bitset(QEXPANDED, q->q_flags))
1141 action = "expanded (to multi-recipient alias)";
1142 else if (bitset(QDELAYED, q->q_flags))
1144 else if (bitset(QBYTRACE, q->q_flags))
1145 action = "relayed (Deliver-By trace mode)";
1146 else if (bitset(QBYNDELAY, q->q_flags))
1147 action = "delayed (Deliver-By notify mode)";
1148 else if (bitset(QBYNRELAY, q->q_flags))
1149 action = "relayed (Deliver-By notify mode)";
1153 if (!putline("", mci))
1156 /* Original-Recipient: -- passed from on high */
1157 if (q->q_orcpt != NULL)
1159 (void) sm_snprintf(buf, sizeof buf,
1160 "Original-Recipient: %.800s",
1162 if (!putline(buf, mci))
1166 /* Figure out actual recipient */
1168 if (q->q_user[0] != '\0')
1170 if (q->q_mailer != NULL &&
1171 q->q_mailer->m_addrtype != NULL)
1172 p = q->q_mailer->m_addrtype;
1176 if (sm_strcasecmp(p, "rfc822") == 0 &&
1177 strchr(q->q_user, '@') == NULL)
1179 (void) sm_snprintf(actual,
1181 "%s; %.700s@%.100s",
1187 (void) sm_snprintf(actual,
1194 /* Final-Recipient: -- the name from the RCPT command */
1195 if (q->q_finalrcpt == NULL)
1197 /* should never happen */
1198 sm_syslog(LOG_ERR, e->e_id,
1199 "returntosender: q_finalrcpt is NULL");
1201 /* try to fall back to the actual recipient */
1202 if (actual[0] != '\0')
1203 q->q_finalrcpt = sm_rpool_strdup_x(e->e_rpool,
1207 if (q->q_finalrcpt != NULL)
1209 (void) sm_snprintf(buf, sizeof buf,
1210 "Final-Recipient: %s",
1212 if (!putline(buf, mci))
1216 /* X-Actual-Recipient: -- the real problem address */
1217 if (actual[0] != '\0' &&
1218 q->q_finalrcpt != NULL &&
1219 #if _FFR_PRIV_NOACTUALRECIPIENT
1220 !bitset(PRIV_NOACTUALRECIPIENT, PrivacyFlags) &&
1221 #endif /* _FFR_PRIV_NOACTUALRECIPIENT */
1222 strcmp(actual, q->q_finalrcpt) != 0)
1224 (void) sm_snprintf(buf, sizeof buf,
1225 "X-Actual-Recipient: %s",
1227 if (!putline(buf, mci))
1231 /* Action: -- what happened? */
1232 (void) sm_strlcpyn(buf, sizeof buf, 2, "Action: ",
1234 if (!putline(buf, mci))
1237 /* Status: -- what _really_ happened? */
1238 if (q->q_status != NULL)
1240 else if (QS_IS_BADADDR(q->q_state))
1242 else if (QS_IS_QUEUEUP(q->q_state))
1246 (void) sm_strlcpyn(buf, sizeof buf, 2, "Status: ", p);
1247 if (!putline(buf, mci))
1250 /* Remote-MTA: -- who was I talking to? */
1251 if (q->q_statmta != NULL)
1253 if (q->q_mailer == NULL ||
1254 (p = q->q_mailer->m_mtatype) == NULL)
1256 (void) sm_snprintf(buf, sizeof buf,
1257 "Remote-MTA: %s; %.800s",
1259 p = &buf[strlen(buf) - 1];
1262 if (!putline(buf, mci))
1266 /* Diagnostic-Code: -- actual result from other end */
1267 if (q->q_rstatus != NULL)
1269 p = q->q_mailer->m_diagtype;
1272 (void) sm_snprintf(buf, sizeof buf,
1273 "Diagnostic-Code: %s; %.800s",
1275 if (!putline(buf, mci))
1279 /* Last-Attempt-Date: -- fine granularity */
1280 if (q->q_statdate == (time_t) 0L)
1281 q->q_statdate = curtime();
1282 (void) sm_strlcpyn(buf, sizeof buf, 2,
1283 "Last-Attempt-Date: ",
1284 arpadate(ctime(&q->q_statdate)));
1285 if (!putline(buf, mci))
1288 /* Will-Retry-Until: -- for delayed messages only */
1289 if (QS_IS_QUEUEUP(q->q_state))
1293 xdate = e->e_parent->e_ctime +
1294 TimeOuts.to_q_return[e->e_parent->e_timeoutclass];
1295 (void) sm_strlcpyn(buf, sizeof buf, 2,
1296 "Will-Retry-Until: ",
1297 arpadate(ctime(&xdate)));
1298 if (!putline(buf, mci))
1306 ** Output text of original message
1309 if (!putline("", mci))
1311 if (bitset(EF_HAS_DF, e->e_parent->e_flags))
1313 sendbody = !bitset(EF_NO_BODY_RETN, e->e_parent->e_flags) &&
1314 !bitset(EF_NO_BODY_RETN, e->e_flags);
1316 if (e->e_msgboundary == NULL)
1320 ? " ----- Original message follows -----\n"
1321 : " ----- Message header follows -----\n",
1329 (void) sm_strlcpyn(buf, sizeof buf, 2, "--",
1332 if (!putline(buf, mci))
1334 (void) sm_strlcpyn(buf, sizeof buf, 2, "Content-Type: ",
1335 sendbody ? "message/rfc822"
1336 : "text/rfc822-headers");
1337 if (!putline(buf, mci))
1340 p = hvalue("Content-Transfer-Encoding",
1341 e->e_parent->e_header);
1342 if (p != NULL && sm_strcasecmp(p, "binary") != 0)
1345 bitset(EF_HAS8BIT, e->e_parent->e_flags))
1349 (void) sm_snprintf(buf, sizeof buf,
1350 "Content-Transfer-Encoding: %s",
1352 if (!putline(buf, mci))
1356 if (!putline("", mci))
1359 if (!putheader(mci, e->e_parent->e_header, e->e_parent,
1365 if (!putbody(mci, e->e_parent, e->e_msgboundary))
1368 else if (e->e_msgboundary == NULL)
1370 if (!putline("", mci) ||
1371 !putline(" ----- Message body suppressed -----",
1378 else if (e->e_msgboundary == NULL)
1380 if (!putline(" ----- No message was collected -----\n", mci))
1384 if (e->e_msgboundary != NULL)
1386 (void) sm_strlcpyn(buf, sizeof buf, 3, "--", e->e_msgboundary,
1388 if (!putline("", mci) || !putline(buf, mci))
1391 if (!putline("", mci) ||
1392 sm_io_flush(mci->mci_out, SM_TIME_DEFAULT) == SM_IO_EOF)
1402 syserr("errbody: I/O error");
1409 ** SMTPTODSN -- convert SMTP to DSN status code
1412 ** smtpstat -- the smtp status code (e.g., 550).
1415 ** The DSN version of the status code.
1417 ** Storage Management:
1418 ** smtptodsn() returns a pointer to a character string literal,
1419 ** which will remain valid forever, and thus does not need to
1420 ** be copied. Current code relies on this property.
1432 case 450: /* Req mail action not taken: mailbox unavailable */
1435 case 451: /* Req action aborted: local error in processing */
1438 case 452: /* Req action not taken: insufficient sys storage */
1441 case 500: /* Syntax error, command unrecognized */
1444 case 501: /* Syntax error in parameters or arguments */
1447 case 502: /* Command not implemented */
1450 case 503: /* Bad sequence of commands */
1453 case 504: /* Command parameter not implemented */
1456 case 550: /* Req mail action not taken: mailbox unavailable */
1459 case 551: /* User not local; please try <...> */
1462 case 552: /* Req mail action aborted: exceeded storage alloc */
1465 case 553: /* Req action not taken: mailbox name not allowed */
1468 case 554: /* Transaction failed */
1472 if (REPLYTYPE(smtpstat) == 2)
1474 if (REPLYTYPE(smtpstat) == 4)
1479 ** XTEXTIFY -- take regular text and turn it into DSN-style xtext
1482 ** t -- the text to convert.
1483 ** taboo -- additional characters that must be encoded.
1486 ** The xtext-ified version of the same string.
1497 static char *bp = NULL;
1498 static int bplen = 0;
1503 /* figure out how long this xtext will have to be */
1505 for (p = t; *p != '\0'; p++)
1507 register int c = (*p & 0xff);
1509 /* ASCII dependence here -- this is the way the spec words it */
1510 if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(' ||
1511 strchr(taboo, c) != NULL)
1517 /* since nbogus is ssize_t and wrapped, 2 * size_t would wrap */
1518 syserr("!xtextify string too long");
1522 l += nbogus * 2 + 1;
1524 /* now allocate space if necessary for the new string */
1528 sm_free(bp); /* XXX */
1529 bp = sm_pmalloc_x(l);
1533 /* ok, copy the text with byte expansion */
1534 for (p = bp; *t != '\0'; )
1536 register int c = (*t++ & 0xff);
1538 /* ASCII dependence here -- this is the way the spec words it */
1539 if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(' ||
1540 strchr(taboo, c) != NULL)
1543 *p++ = "0123456789ABCDEF"[c >> 4];
1544 *p++ = "0123456789ABCDEF"[c & 0xf];
1553 ** XUNTEXTIFY -- take xtext and turn it into plain text
1556 ** t -- the xtextified text.
1559 ** The decoded text. No attempt is made to deal with
1560 ** null strings in the resulting text.
1569 static char *bp = NULL;
1570 static int bplen = 0;
1572 /* heuristic -- if no plus sign, just return the input */
1573 if (strchr(t, '+') == NULL)
1576 /* xtext is always longer than decoded text */
1581 sm_free(bp); /* XXX */
1586 /* ok, copy the text with byte compression */
1587 for (p = bp; *t != '\0'; t++)
1589 register int c = *t & 0xff;
1598 if (!isascii(c) || !isxdigit(c))
1600 /* error -- first digit is not hex */
1601 usrerr("bogus xtext: +%c", c);
1607 else if (isupper(c))
1614 if (!isascii(c) || !isxdigit(c))
1616 /* error -- second digit is not hex */
1617 usrerr("bogus xtext: +%x%c", *p >> 4, c);
1623 else if (isupper(c))
1633 ** XTEXTOK -- check if a string is legal xtext
1635 ** Xtext is used in Delivery Status Notifications. The spec was
1636 ** taken from RFC 1891, ``SMTP Service Extension for Delivery
1637 ** Status Notifications''.
1640 ** s -- the string to check.
1643 ** true -- if 's' is legal xtext.
1644 ** false -- if it has any illegal characters in it.
1653 while ((c = *s++) != '\0')
1658 if (!isascii(c) || !isxdigit(c))
1661 if (!isascii(c) || !isxdigit(c))
1664 else if (c < '!' || c > '~' || c == '=')
1670 ** PRUNEROUTE -- prune an RFC-822 source route
1672 ** Trims down a source route to the last internet-registered hop.
1673 ** This is encouraged by RFC 1123 section 5.3.3.
1676 ** addr -- the address
1679 ** true -- address was modified
1680 ** false -- address could not be pruned
1683 ** modifies addr in-place
1691 char *start, *at, *comma;
1696 char hostbuf[BUFSIZ];
1697 char *mxhosts[MAXMXHOSTS + 1];
1699 /* check to see if this is really a route-addr */
1700 if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
1704 ** Can't simply find the first ':' is the address might be in the
1705 ** form: "<@[IPv6:::1]:user@host>" and the first ':' in inside
1706 ** the IPv6 address.
1711 while (*start != '\0')
1713 if (*start == ':' && braclev <= 0)
1715 else if (*start == '[')
1717 else if (*start == ']' && braclev > 0)
1721 if (braclev > 0 || *start != ':')
1724 at = strrchr(addr, '@');
1725 if (at == NULL || at < start)
1728 /* slice off the angle brackets */
1730 if (i >= sizeof hostbuf)
1732 (void) sm_strlcpy(hostbuf, at + 1, sizeof hostbuf);
1733 hostbuf[i - 1] = '\0';
1735 while (start != NULL)
1737 if (getmxrr(hostbuf, mxhosts, NULL, false,
1738 &rcode, true, NULL) > 0)
1740 (void) sm_strlcpy(addr + 1, start + 1,
1746 comma = strrchr(addr, ',');
1747 if (comma != NULL && comma[1] == '@' &&
1748 strlen(comma + 2) < sizeof hostbuf)
1749 (void) sm_strlcpy(hostbuf, comma + 2, sizeof hostbuf);
1755 #endif /* NAMED_BIND */