improve DSN handling somewhat + some bug fixes
[unix-history] / usr / src / usr.sbin / sendmail / src / savemail.c
index 894ee29..bf76239 100644 (file)
@@ -7,11 +7,11 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)savemail.c 8.3 (Berkeley) %G%";
+static char sccsid[] = "@(#)savemail.c 8.44 (Berkeley) %G%";
 #endif /* not lint */
 
 #endif /* not lint */
 
-# include <pwd.h>
 # include "sendmail.h"
 # include "sendmail.h"
+# include <pwd.h>
 
 /*
 **  SAVEMAIL -- Save mail on error
 
 /*
 **  SAVEMAIL -- Save mail on error
@@ -23,6 +23,8 @@ static char sccsid[] = "@(#)savemail.c        8.3 (Berkeley) %G%";
 **
 **     Parameters:
 **             e -- the envelope containing the message in error.
 **
 **     Parameters:
 **             e -- the envelope containing the message in error.
+**             sendbody -- if TRUE, also send back the body of the
+**                     message; otherwise just send the header.
 **
 **     Returns:
 **             none
 **
 **     Returns:
 **             none
@@ -43,24 +45,32 @@ static char sccsid[] = "@(#)savemail.c      8.3 (Berkeley) %G%";
 # define ESM_PANIC     6       /* leave the locked queue/transcript files */
 # define ESM_DONE      7       /* the message is successfully delivered */
 
 # define ESM_PANIC     6       /* leave the locked queue/transcript files */
 # define ESM_DONE      7       /* the message is successfully delivered */
 
+# ifndef _PATH_VARTMP
+#  define _PATH_VARTMP "/usr/tmp/"
+# endif
+
 
 
-savemail(e)
+savemail(e, sendbody)
        register ENVELOPE *e;
        register ENVELOPE *e;
+       bool sendbody;
 {
        register struct passwd *pw;
        register FILE *fp;
        int state;
        auto ADDRESS *q = NULL;
 {
        register struct passwd *pw;
        register FILE *fp;
        int state;
        auto ADDRESS *q = NULL;
+       register char *p;
+       MCI mcibuf;
        char buf[MAXLINE+1];
        extern struct passwd *getpwnam();
        char buf[MAXLINE+1];
        extern struct passwd *getpwnam();
-       register char *p;
        extern char *ttypath();
        typedef int (*fnptr)();
        extern char *ttypath();
        typedef int (*fnptr)();
+       extern bool writable();
 
        if (tTd(6, 1))
        {
 
        if (tTd(6, 1))
        {
-               printf("\nsavemail, errormode = %c, id = %s\n  e_from=",
-                       e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id);
+               printf("\nsavemail, errormode = %c, id = %s, ExitStat = %d\n  e_from=",
+                       e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id,
+                       ExitStat);
                printaddr(&e->e_from, FALSE);
        }
 
                printaddr(&e->e_from, FALSE);
        }
 
@@ -70,8 +80,6 @@ savemail(e)
                return;
        }
 
                return;
        }
 
-       e->e_flags &= ~EF_FATALERRS;
-
        /*
        **  In the unhappy event we don't know who to return the mail
        **  to, make someone up.
        /*
        **  In the unhappy event we don't know who to return the mail
        **  to, make someone up.
@@ -155,7 +163,7 @@ savemail(e)
                switch (state)
                {
                  case ESM_QUIET:
                switch (state)
                {
                  case ESM_QUIET:
-                       if (e->e_from.q_mailer == LocalMailer)
+                       if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags))
                                state = ESM_DEADLETTER;
                        else
                                state = ESM_MAIL;
                                state = ESM_DEADLETTER;
                        else
                                state = ESM_MAIL;
@@ -212,33 +220,48 @@ savemail(e)
                        **      joe@x, which gives a response, etc.  Also force
                        **      the mail to be delivered even if a version of
                        **      it has already been sent to the sender.
                        **      joe@x, which gives a response, etc.  Also force
                        **      the mail to be delivered even if a version of
                        **      it has already been sent to the sender.
+                       **
+                       **  If this is a configuration or local software
+                       **      error, send to the local postmaster as well,
+                       **      since the originator can't do anything
+                       **      about it anyway.  Note that this is a full
+                       **      copy of the message (intentionally) so that
+                       **      the Postmaster can forward things along.
                        */
 
                        */
 
-                       if (strcmp(e->e_from.q_paddr, "<>") != 0)
+                       if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
+                       {
+                               (void) sendtolist("postmaster",
+                                         NULLADDR, &e->e_errorqueue, 0, e);
+                       }
+                       if (!emptyaddr(&e->e_from))
+                       {
                                (void) sendtolist(e->e_from.q_paddr,
                                (void) sendtolist(e->e_from.q_paddr,
-                                         (ADDRESS *) NULL,
-                                         &e->e_errorqueue, e);
+                                         NULLADDR, &e->e_errorqueue, 0, e);
+                       }
 
 
-                       /* deliver a cc: to the postmaster if desired */
-                       if (PostMasterCopy != NULL)
-                       {
-                               auto ADDRESS *rlist = NULL;
+                       /*
+                       **  Deliver a non-delivery report to the
+                       **  Postmaster-designate (not necessarily
+                       **  Postmaster).  This does not include the
+                       **  body of the message, for privacy reasons.
+                       **  You really shouldn't need this.
+                       */
 
 
-                               (void) sendtolist(PostMasterCopy,
-                                                 (ADDRESS *) NULL,
-                                                 &rlist, e);
-                               (void) returntosender(e->e_message,
-                                                     rlist, FALSE, e);
-                       }
-                       q = e->e_errorqueue;
+                       e->e_flags |= EF_PM_NOTIFY;
+
+                       /* check to see if there are any good addresses */
+                       for (q = e->e_errorqueue; q != NULL; q = q->q_next)
+                               if (!bitset(QBADADDR|QDONTSEND, q->q_flags))
+                                       break;
                        if (q == NULL)
                        {
                                /* this is an error-error */
                                state = ESM_POSTMASTER;
                                break;
                        }
                        if (q == NULL)
                        {
                                /* this is an error-error */
                                state = ESM_POSTMASTER;
                                break;
                        }
-                       if (returntosender(e->e_message,
-                                          q, (e->e_class >= 0), e) == 0)
+                       if (returntosender(e->e_message, e->e_errorqueue,
+                                          sendbody, e) == 0)
                        {
                                state = ESM_DONE;
                                break;
                        {
                                state = ESM_DONE;
                                break;
@@ -254,15 +277,14 @@ savemail(e)
                        */
 
                        q = NULL;
                        */
 
                        q = NULL;
-                       if (sendtolist("postmaster", NULL, &q, e) <= 0)
+                       if (sendtolist("postmaster", NULL, &q, 0, e) <= 0)
                        {
                                syserr("553 cannot parse postmaster!");
                                ExitStat = EX_SOFTWARE;
                                state = ESM_USRTMP;
                                break;
                        }
                        {
                                syserr("553 cannot parse postmaster!");
                                ExitStat = EX_SOFTWARE;
                                state = ESM_USRTMP;
                                break;
                        }
-                       if (returntosender(e->e_message,
-                                          q, (e->e_class >= 0), e) == 0)
+                       if (returntosender(e->e_message, q, sendbody, e) == 0)
                        {
                                state = ESM_DONE;
                                break;
                        {
                                state = ESM_DONE;
                                break;
@@ -283,7 +305,7 @@ savemail(e)
                        */
 
                        p = NULL;
                        */
 
                        p = NULL;
-                       if (e->e_from.q_mailer == LocalMailer)
+                       if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
                        {
                                if (e->e_from.q_home != NULL)
                                        p = e->e_from.q_home;
                        {
                                if (e->e_from.q_home != NULL)
                                        p = e->e_from.q_home;
@@ -308,8 +330,10 @@ savemail(e)
                                Verbose = oldverb;
                                e->e_to = buf;
                                q = NULL;
                                Verbose = oldverb;
                                e->e_to = buf;
                                q = NULL;
-                               (void) sendtolist(buf, &e->e_from, &q, e);
-                               if (deliver(e, q) == 0)
+                               (void) sendtolist(buf, &e->e_from, &q, 0, e);
+                               if (q != NULL &&
+                                   !bitset(QBADADDR, q->q_flags) &&
+                                   deliver(e, q) == 0)
                                        state = ESM_DONE;
                                else
                                        state = ESM_MAIL;
                                        state = ESM_DONE;
                                else
                                        state = ESM_MAIL;
@@ -332,22 +356,33 @@ savemail(e)
                                break;
                        }
 
                                break;
                        }
 
-                       fp = dfopen("/usr/tmp/dead.letter",
-                                   O_WRONLY|O_CREAT|O_APPEND, FileMode);
+                       strcpy(buf, _PATH_VARTMP);
+                       strcat(buf, "dead.letter");
+                       if (!writable(buf, NULLADDR, SFF_NOSLINK))
+                       {
+                               state = ESM_PANIC;
+                               break;
+                       }
+                       fp = dfopen(buf, O_WRONLY|O_CREAT|O_APPEND, FileMode);
                        if (fp == NULL)
                        {
                                state = ESM_PANIC;
                                break;
                        }
 
                        if (fp == NULL)
                        {
                                state = ESM_PANIC;
                                break;
                        }
 
-                       putfromline(fp, FileMailer, e);
-                       (*e->e_puthdr)(fp, FileMailer, e);
-                       putline("\n", fp, FileMailer);
-                       (*e->e_putbody)(fp, FileMailer, e, NULL);
-                       putline("\n", fp, FileMailer);
+                       bzero(&mcibuf, sizeof mcibuf);
+                       mcibuf.mci_out = fp;
+                       mcibuf.mci_mailer = FileMailer;
+                       if (bitnset(M_7BITS, FileMailer->m_flags))
+                               mcibuf.mci_flags |= MCIF_7BIT;
+
+                       putfromline(&mcibuf, e);
+                       (*e->e_puthdr)(&mcibuf, e->e_header, e, 0);
+                       (*e->e_putbody)(&mcibuf, e, NULL, 0);
+                       putline("\n", &mcibuf);
                        (void) fflush(fp);
                        state = ferror(fp) ? ESM_PANIC : ESM_DONE;
                        (void) fflush(fp);
                        state = ferror(fp) ? ESM_PANIC : ESM_DONE;
-                       (void) xfclose(fp, "savemail", "/usr/tmp/dead.letter");
+                       (void) xfclose(fp, "savemail", buf);
                        break;
 
                  default:
                        break;
 
                  default:
@@ -357,8 +392,7 @@ savemail(e)
 
                  case ESM_PANIC:
                        /* leave the locked queue & transcript files around */
 
                  case ESM_PANIC:
                        /* leave the locked queue & transcript files around */
-                       syserr("554 savemail: cannot save rejected email anywhere");
-                       exit(EX_SOFTWARE);
+                       syserr("!554 savemail: cannot save rejected email anywhere");
                }
        }
 }
                }
        }
 }
@@ -408,9 +442,14 @@ returntosender(msg, returnq, sendbody, e)
 
        if (tTd(6, 1))
        {
 
        if (tTd(6, 1))
        {
-               printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
+               printf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
                       msg, returndepth, e);
                printaddr(returnq, TRUE);
                       msg, returndepth, e);
                printaddr(returnq, TRUE);
+               if (tTd(6, 20))
+               {
+                       printf("Sendq=");
+                       printaddr(e->e_sendqueue, TRUE);
+               }
        }
 
        if (++returndepth >= MAXRETURNS)
        }
 
        if (++returndepth >= MAXRETURNS)
@@ -424,6 +463,9 @@ returntosender(msg, returnq, sendbody, e)
 
        SendBody = sendbody;
        define('g', e->e_from.q_paddr, e);
 
        SendBody = sendbody;
        define('g', e->e_from.q_paddr, e);
+       define('u', NULL, e);
+
+       /* initialize error envelope */
        ee = newenvelope(&errenvelope, e);
        define('a', "\201b", ee);
        define('r', "internal", ee);
        ee = newenvelope(&errenvelope, e);
        define('a', "\201b", ee);
        define('r', "internal", ee);
@@ -431,12 +473,14 @@ returntosender(msg, returnq, sendbody, e)
        define('_', "localhost", ee);
        ee->e_puthdr = putheader;
        ee->e_putbody = errbody;
        define('_', "localhost", ee);
        ee->e_puthdr = putheader;
        ee->e_putbody = errbody;
-       ee->e_flags |= EF_RESPONSE;
+       ee->e_flags |= EF_RESPONSE|EF_METOO;
        if (!bitset(EF_OLDSTYLE, e->e_flags))
                ee->e_flags &= ~EF_OLDSTYLE;
        ee->e_sendqueue = returnq;
        if (!bitset(EF_OLDSTYLE, e->e_flags))
                ee->e_flags &= ~EF_OLDSTYLE;
        ee->e_sendqueue = returnq;
-       ee->e_msgsize = e->e_msgsize + ERRORFUDGE;
-       openxscript(ee);
+       ee->e_msgsize = ERRORFUDGE;
+       if (!bitset(EF_NORETURN, e->e_flags))
+               ee->e_msgsize += e->e_msgsize;
+       initsys(ee);
        for (q = returnq; q != NULL; q = q->q_next)
        {
                if (bitset(QBADADDR, q->q_flags))
        for (q = returnq; q != NULL; q = q->q_next)
        {
                if (bitset(QBADADDR, q->q_flags))
@@ -446,34 +490,39 @@ returntosender(msg, returnq, sendbody, e)
                        ee->e_nrcpts++;
 
                if (!DontPruneRoutes && pruneroute(q->q_paddr))
                        ee->e_nrcpts++;
 
                if (!DontPruneRoutes && pruneroute(q->q_paddr))
-                       parseaddr(q->q_paddr, q, 0, '\0', NULL, e);
+                       parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e);
 
                if (q->q_alias == NULL)
 
                if (q->q_alias == NULL)
-                       addheader("To", q->q_paddr, ee);
+                       addheader("To", q->q_paddr, &ee->e_header);
        }
 
 # ifdef LOG
        if (LogLevel > 5)
        }
 
 # ifdef LOG
        if (LogLevel > 5)
-               syslog(LOG_INFO, "%s: %s: return to sender: %s",
+               syslog(LOG_INFO, "%s: %s: returntosender: %s",
                        e->e_id, ee->e_id, msg);
 # endif
 
                        e->e_id, ee->e_id, msg);
 # endif
 
-       (void) sprintf(buf, "Returned mail: %s", msg);
-       addheader("Subject", buf, ee);
+       if (strncasecmp(msg, "warning:", 8) != 0)
+       {
+               (void) sprintf(buf, "Returned mail: %s", msg);
+               msg = buf;
+       }
+       addheader("Subject", msg, ee);
        if (SendMIMEErrors)
        {
        if (SendMIMEErrors)
        {
-               addheader("MIME-Version", "1.0", ee);
+               addheader("MIME-Version", "1.0", &ee->e_header);
                (void) sprintf(buf, "%s.%ld/%s",
                        ee->e_id, curtime(), MyHostName);
                ee->e_msgboundary = newstr(buf);
                (void) sprintf(buf, "%s.%ld/%s",
                        ee->e_id, curtime(), MyHostName);
                ee->e_msgboundary = newstr(buf);
-               (void) sprintf(buf, "multipart/mixed; boundary=\"%s\"",
-                                       ee->e_msgboundary);
-               addheader("Content-Type", buf, ee);
+               (void) sprintf(buf,
+                       "multipart/report; report-type=delivery-status; boundary=\"%s\"",
+                       ee->e_msgboundary);
+               addheader("Content-Type", buf, &ee->e_header);
        }
 
        /* fake up an address header for the from person */
        expand("\201n", buf, &buf[sizeof buf - 1], e);
        }
 
        /* fake up an address header for the from person */
        expand("\201n", buf, &buf[sizeof buf - 1], e);
-       if (parseaddr(buf, &ee->e_from, 1, '\0', NULL, e) == NULL)
+       if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL)
        {
                syserr("553 Can't parse myself!");
                ExitStat = EX_SOFTWARE;
        {
                syserr("553 Can't parse myself!");
                ExitStat = EX_SOFTWARE;
@@ -489,7 +538,7 @@ returntosender(msg, returnq, sendbody, e)
        eatheader(ee, TRUE);
 
        /* mark statistics */
        eatheader(ee, TRUE);
 
        /* mark statistics */
-       markstats(ee, (ADDRESS *) NULL);
+       markstats(ee, NULLADDR);
 
        /* actually deliver the error message */
        sendall(ee, SM_DEFAULT);
 
        /* actually deliver the error message */
        sendall(ee, SM_DEFAULT);
@@ -509,9 +558,10 @@ returntosender(msg, returnq, sendbody, e)
 **     original offending message.
 **
 **     Parameters:
 **     original offending message.
 **
 **     Parameters:
-**             fp -- the output file.
-**             m -- the mailer to output to.
+**             mci -- the mailer connection information.
 **             e -- the envelope we are working in.
 **             e -- the envelope we are working in.
+**             separator -- any possible MIME separator.
+**             flags -- to modify the behaviour.
 **
 **     Returns:
 **             none
 **
 **     Returns:
 **             none
@@ -520,21 +570,27 @@ returntosender(msg, returnq, sendbody, e)
 **             Outputs the body of an error message.
 */
 
 **             Outputs the body of an error message.
 */
 
-errbody(fp, m, e)
-       register FILE *fp;
-       register struct mailer *m;
+errbody(mci, e, separator, flags)
+       register MCI *mci;
        register ENVELOPE *e;
        register ENVELOPE *e;
+       char *separator;
 {
        register FILE *xfile;
        char *p;
        register ADDRESS *q;
        bool printheader;
 {
        register FILE *xfile;
        char *p;
        register ADDRESS *q;
        bool printheader;
+       int pflags = flags;
        char buf[MAXLINE];
 
        char buf[MAXLINE];
 
+       if (bitset(MCIF_INHEADER, mci->mci_flags))
+       {
+               putline("", mci);
+               mci->mci_flags &= ~MCIF_INHEADER;
+       }
        if (e->e_parent == NULL)
        {
                syserr("errbody: null parent");
        if (e->e_parent == NULL)
        {
                syserr("errbody: null parent");
-               putline("   ----- Original message lost -----\n", fp, m);
+               putline("   ----- Original message lost -----\n", mci);
                return;
        }
 
                return;
        }
 
@@ -544,18 +600,45 @@ errbody(fp, m, e)
 
        if (e->e_msgboundary != NULL)
        {
 
        if (e->e_msgboundary != NULL)
        {
-               putline("This is a MIME-encapsulated message", fp, m);
-               putline("", fp, m);
+               putline("This is a MIME-encapsulated message", mci);
+               putline("", mci);
                (void) sprintf(buf, "--%s", e->e_msgboundary);
                (void) sprintf(buf, "--%s", e->e_msgboundary);
-               putline(buf, fp, m);
-               putline("", fp, m);
+               putline(buf, mci);
+               putline("", mci);
+       }
+
+       /*
+       **  Output introductory information.
+       */
+
+       for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
+               if (bitset(QBADADDR, q->q_flags))
+                       break;
+       if (q == NULL &&
+           !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
+       {
+               putline("    **********************************************",
+                       mci);
+               putline("    **      THIS IS A WARNING MESSAGE ONLY      **",
+                       mci);
+               putline("    **  YOU DO NOT NEED TO RESEND YOUR MESSAGE  **",
+                       mci);
+               putline("    **********************************************",
+                       mci);
+               putline("", mci);
        }
        }
+       sprintf(buf, "The original message was received at %s",
+               arpadate(ctime(&e->e_parent->e_ctime)));
+       putline(buf, mci);
+       expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent);
+       putline(buf, mci);
+       putline("", mci);
 
        /*
        **  Output error message header (if specified and available).
        */
 
 
        /*
        **  Output error message header (if specified and available).
        */
 
-       if (ErrMsgFile != NULL)
+       if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags))
        {
                if (*ErrMsgFile == '/')
                {
        {
                if (*ErrMsgFile == '/')
                {
@@ -565,17 +648,17 @@ errbody(fp, m, e)
                                while (fgets(buf, sizeof buf, xfile) != NULL)
                                {
                                        expand(buf, buf, &buf[sizeof buf - 1], e);
                                while (fgets(buf, sizeof buf, xfile) != NULL)
                                {
                                        expand(buf, buf, &buf[sizeof buf - 1], e);
-                                       putline(buf, fp, m);
+                                       putline(buf, mci);
                                }
                                (void) fclose(xfile);
                                }
                                (void) fclose(xfile);
-                               putline("\n", fp, m);
+                               putline("\n", mci);
                        }
                }
                else
                {
                        expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
                        }
                }
                else
                {
                        expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
-                       putline(buf, fp, m);
-                       putline("", fp, m);
+                       putline(buf, mci);
+                       putline("", mci);
                }
        }
 
                }
        }
 
@@ -586,27 +669,35 @@ errbody(fp, m, e)
        printheader = TRUE;
        for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
        {
        printheader = TRUE;
        for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
        {
-               if (bitset(QBADADDR|QREPORT, q->q_flags))
+               if (bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags))
                {
                        if (printheader)
                        {
                {
                        if (printheader)
                        {
-                               putline("   ----- The following addresses had delivery problems -----",
-                                       fp, m);
+                               putline("   ----- The following addresses have delivery notifications -----",
+                                       mci);
                                printheader = FALSE;
                        }
                                printheader = FALSE;
                        }
-                       if (q->q_alias != NULL)
-                               strcpy(buf, q->q_alias->q_paddr);
-                       else
-                               strcpy(buf, q->q_paddr);
+                       strcpy(buf, q->q_paddr);
                        if (bitset(QBADADDR, q->q_flags))
                        if (bitset(QBADADDR, q->q_flags))
-                               strcat(buf, "  (hard error -- address deleted)");
+                               strcat(buf, "  (unrecoverable error)");
+                       else if (bitset(QRELAYED, q->q_flags))
+                               strcat(buf, "  (relayed to non-DSN-aware mailer)");
+                       else if (bitset(QSENT, q->q_flags))
+                               strcat(buf, "  (successfully delivered)");
                        else
                        else
-                               strcat(buf, "  (temporary failure -- will retry)");
-                       putline(buf, fp, m);
+                               strcat(buf, "  (transient failure)");
+                       putline(buf, mci);
+                       if (q->q_alias != NULL)
+                       {
+                               strcpy(buf, "    (expanded from: ");
+                               strcat(buf, q->q_alias->q_paddr);
+                               strcat(buf, ")");
+                               putline(buf, mci);
+                       }
                }
        }
        if (!printheader)
                }
        }
        if (!printheader)
-               putline("\n", fp, m);
+               putline("\n", mci);
 
        /*
        **  Output transcript of errors
 
        /*
        **  Output transcript of errors
@@ -617,61 +708,242 @@ errbody(fp, m, e)
        if ((xfile = fopen(p, "r")) == NULL)
        {
                syserr("Cannot open %s", p);
        if ((xfile = fopen(p, "r")) == NULL)
        {
                syserr("Cannot open %s", p);
-               putline("   ----- Transcript of session is unavailable -----\n", fp, m);
+               putline("   ----- Transcript of session is unavailable -----\n", mci);
        }
        else
        {
        }
        else
        {
-               putline("   ----- Transcript of session follows -----\n", fp, m);
+               putline("   ----- Transcript of session follows -----\n", mci);
                if (e->e_xfp != NULL)
                        (void) fflush(e->e_xfp);
                while (fgets(buf, sizeof buf, xfile) != NULL)
                if (e->e_xfp != NULL)
                        (void) fflush(e->e_xfp);
                while (fgets(buf, sizeof buf, xfile) != NULL)
-                       putline(buf, fp, m);
+                       putline(buf, mci);
                (void) xfclose(xfile, "errbody xscript", p);
        }
        errno = 0;
 
                (void) xfclose(xfile, "errbody xscript", p);
        }
        errno = 0;
 
+       /*
+       **  Output machine-readable version.
+       */
+
+       if (e->e_msgboundary != NULL)
+       {
+               putline("", mci);
+               (void) sprintf(buf, "--%s", e->e_msgboundary);
+               putline(buf, mci);
+               putline("Content-Type: message/delivery-status", mci);
+               putline("", mci);
+
+               /*
+               **  Output per-message information.
+               */
+
+               /* OMTS from MAIL FROM: line */
+               if (e->e_parent->e_omts != NULL)
+               {
+                       (void) sprintf(buf, "Original-MTS-Type: %s",
+                               e->e_parent->e_omts);
+                       putline(buf, mci);
+               }
+
+               /* original envelope id from MAIL FROM: line */
+               if (e->e_parent->e_envid != NULL)
+               {
+                       (void) sprintf(buf, "Original-Envelope-Id: %s",
+                               e->e_parent->e_envid);
+                       putline(buf, mci);
+               }
+
+               /* Final-MTS-Type: is required -- our type */
+               putline("Final-MTS-Type: Internet", mci);
+
+               /* Final-MTA: seems silly -- this is in the From: line */
+               (void) sprintf(buf, "Final-MTA: %s", MyHostName);
+               putline(buf, mci);
+
+               /* Received-From: shows where we got this message from */
+               if (RealHostName != NULL)
+               {
+                       (void) sprintf(buf, "Received-From: %s", RealHostName);
+                       putline(buf, mci);
+               }
+
+               /* Arrival-Date: -- when it arrived here */
+               (void) sprintf(buf, "Arrival-Date: %s",
+                       arpadate(ctime(&e->e_parent->e_ctime)));
+               putline(buf, mci);
+
+               /*
+               **  Output per-address information.
+               */
+
+               for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
+               {
+                       register ADDRESS *r;
+
+                       if (!bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags))
+                               continue;
+                       putline("", mci);
+
+                       /* Recipient: -- the name from the RCPT command */
+                       for (r = q; r->q_alias != NULL; r = r->q_alias)
+                               continue;
+                       (void) sprintf(buf, "Recipient: %s", r->q_paddr);
+                       putline(buf, mci);
+
+                       /* Action: -- what happened? */
+                       if (bitset(QBADADDR, q->q_flags))
+                               putline("Action: failed", mci);
+                       else if (bitset(QQUEUEUP, q->q_flags))
+                               putline("Action: delayed", mci);
+                       else if (bitset(QRELAYED, q->q_flags))
+                               putline("Action: relayed", mci);
+                       else
+                               putline("Action: delivered", mci);
+
+                       /* Status: -- what _really_ happened? */
+                       strcpy(buf, "Status: ");
+                       if (q->q_status != NULL)
+                               strcat(buf, q->q_status);
+                       else if (q->q_fstatus != NULL)
+                               strcat(buf, q->q_fstatus);
+                       else if (bitset(QBADADDR, q->q_flags))
+                               strcat(buf, "500");
+                       else if (bitset(QQUEUEUP, q->q_flags))
+                               strcat(buf, "400");
+                       else if (bitset(QRELAYED, q->q_flags))
+                               strcat(buf, "601");
+                       else
+                               strcat(buf, "200");
+                       putline(buf, mci);
+
+                       /* Date: -- fine granularity */
+                       if (q->q_statdate == (time_t) 0L)
+                               q->q_statdate = curtime();
+                       (void) sprintf(buf, "Date: %s",
+                               arpadate(ctime(&q->q_statdate)));
+                       putline(buf, mci);
+
+                       /* Final-Log-Id: -- why isn't this per-message? */
+                       (void) sprintf(buf, "Final-Log-Id: %s", e->e_id);
+                       putline(buf, mci);
+
+                       /* Expiry-Date: -- for delayed messages only */
+                       if (bitset(QQUEUEUP, q->q_flags) &&
+                           !bitset(QBADADDR, q->q_flags))
+                       {
+                               time_t xdate;
+
+                               xdate = e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass];
+                               sprintf(buf, "Expiry-Date: %s",
+                                       arpadate(ctime(&xdate)));
+                               putline(buf, mci);
+                       }
+
+                       /* Original-Recipient: -- passed from on high */
+                       if (q->q_orcpt != NULL)
+                       {
+                               (void) sprintf(buf, "Original-Recipient: %s",
+                                       q->q_orcpt);
+                               putline(buf, mci);
+                       }
+
+                       /* Final-Recipient: -- if through alias */
+                       if (q->q_alias != NULL)
+                       {
+                               (void) sprintf(buf, "Final-Recipient: %s",
+                                       q->q_paddr);
+                               putline(buf, mci);
+                       }
+
+                       /* Final-Status: -- same as Status?  XXX */
+                       if (q->q_fstatus != NULL && q->q_status != NULL)
+                       {
+                               (void) sprintf(buf, "Final-Status: %s",
+                                       q->q_fstatus);
+                               putline(buf, mci);
+                       }
+
+                       /* Remote-MTS-Type: -- always INET?  XXX */
+                       if (q->q_mailer->m_mtstype != NULL)
+                       {
+                               (void) sprintf(buf, "Remote-MTS-Type: %s",
+                                       q->q_mailer->m_mtstype);
+                               putline(buf, mci);
+                       }
+
+                       /* Remote-MTA: -- who was I talking to? */
+                       if (q->q_statmta != NULL)
+                       {
+                               (void) sprintf(buf, "Remote-MTA: %s",
+                                       q->q_statmta);
+                               putline(buf, mci);
+                       }
+
+                       /* Remote-Recipient: -- same as Final-Recipient?  XXX */
+                       if (strcmp(q->q_user, q->q_paddr) != 0)
+                       {
+                               (void) sprintf(buf, "Remote-Recipient: %s",
+                                       q->q_user);
+                               putline(buf, mci);
+                       }
+
+                       /* Remote-Status: -- return code from remote mailer */
+                       if (q->q_rstatus != NULL)
+                       {
+                               (void) sprintf(buf, "Remote-Status: %s",
+                                       q->q_rstatus);
+                               putline(buf, mci);
+                       }
+               }
+       }
+
        /*
        **  Output text of original message
        */
 
        /*
        **  Output text of original message
        */
 
-       if (NoReturn)
+       if (bitset(EF_NORETURN, e->e_parent->e_flags))
                SendBody = FALSE;
                SendBody = FALSE;
-       putline("", fp, m);
+       if (!SendBody && e->e_msgboundary != NULL)
+               pflags |= PF_DELETEMIMEHDRS;
+       putline("", mci);
        if (e->e_parent->e_df != NULL)
        {
        if (e->e_parent->e_df != NULL)
        {
-               if (SendBody)
-                       putline("   ----- Unsent message follows -----\n", fp, m);
+               if (e->e_msgboundary == NULL)
+               {
+                       if (SendBody)
+                               putline("   ----- Original message follows -----\n", mci);
+                       else
+                               putline("   ----- Message header follows -----\n", mci);
+                       (void) fflush(mci->mci_out);
+               }
                else
                else
-                       putline("   ----- Message header follows -----\n", fp, m);
-               (void) fflush(fp);
-
-               if (e->e_msgboundary != NULL)
                {
                {
-                       putline("", fp, m);
                        (void) sprintf(buf, "--%s", e->e_msgboundary);
                        (void) sprintf(buf, "--%s", e->e_msgboundary);
-                       putline(buf, fp, m);
-                       putline("Content-Type: message/rfc822", fp, m);
-                       putline("", fp, m);
+                       putline(buf, mci);
+                       putline("Content-Type: message/rfc822", mci);
                }
                }
-               putheader(fp, m, e->e_parent);
-               putline("", fp, m);
+               putline("", mci);
+               putheader(mci, e->e_parent->e_header, e->e_parent, pflags);
                if (SendBody)
                if (SendBody)
-                       putbody(fp, m, e->e_parent, e->e_msgboundary);
+                       putbody(mci, e->e_parent, e->e_msgboundary, pflags);
                else
                else
-                       putline("   ----- Message body suppressed -----", fp, m);
+               {
+                       putline("", mci);
+                       putline("   ----- Message body suppressed -----", mci);
+               }
        }
        else
        {
        }
        else
        {
-               putline("  ----- No message was collected -----\n", fp, m);
+               putline("  ----- No message was collected -----\n", mci);
        }
 
        if (e->e_msgboundary != NULL)
        {
        }
 
        if (e->e_msgboundary != NULL)
        {
-               putline("", fp, m);
+               putline("", mci);
                (void) sprintf(buf, "--%s--", e->e_msgboundary);
                (void) sprintf(buf, "--%s--", e->e_msgboundary);
-               putline(buf, fp, m);
+               putline(buf, mci);
        }
        }
-       putline("", fp, m);
+       putline("", mci);
 
        /*
        **  Cleanup and exit
 
        /*
        **  Cleanup and exit
@@ -700,7 +972,7 @@ errbody(fp, m, e)
 pruneroute(addr)
        char *addr;
 {
 pruneroute(addr)
        char *addr;
 {
-#ifdef NAMED_BIND
+#if NAMED_BIND
        char *start, *at, *comma;
        char c;
        int rcode;
        char *start, *at, *comma;
        char c;
        int rcode;