BSD 4_4_Lite1 release
[unix-history] / usr / src / usr.sbin / sendmail / src / savemail.c
index f3429d8..6467def 100644 (file)
@@ -3,15 +3,41 @@
  * Copyright (c) 1988, 1993
  *     The Regents of the University of California.  All rights reserved.
  *
  * Copyright (c) 1988, 1993
  *     The Regents of the University of California.  All rights reserved.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)savemail.c 8.6 (Berkeley) %G%";
+static char sccsid[] = "@(#)savemail.c 8.28 (Berkeley) 3/11/94";
 #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
@@ -43,6 +69,10 @@ static char sccsid[] = "@(#)savemail.c       8.6 (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)
        register ENVELOPE *e;
 
 savemail(e)
        register ENVELOPE *e;
@@ -51,16 +81,19 @@ savemail(e)
        register FILE *fp;
        int state;
        auto ADDRESS *q = NULL;
        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,17 +103,16 @@ 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.
        */
 
-       if (CurEnv->e_returnto == NULL)
+       if (e->e_from.q_paddr == NULL)
        {
        {
-               CurEnv->e_returnto = parse("root", (ADDRESS *) NULL, 0);
-               if (CurEnv->e_returnto == NULL)
+               e->e_sender = "Postmaster";
+               if (parseaddr(e->e_sender, &e->e_from,
+                             RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL)
                {
                        syserr("553 Cannot parse Postmaster!");
                        ExitStat = EX_SOFTWARE;
                {
                        syserr("553 Cannot parse Postmaster!");
                        ExitStat = EX_SOFTWARE;
@@ -224,14 +256,12 @@ savemail(e)
                        if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
                        {
                                (void) sendtolist("postmaster",
                        if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
                        {
                                (void) sendtolist("postmaster",
-                                         (ADDRESS *) NULL,
-                                         &e->e_errorqueue, e);
+                                         NULLADDR, &e->e_errorqueue, e);
                        }
                        if (strcmp(e->e_from.q_paddr, "<>") != 0)
                        {
                                (void) sendtolist(e->e_from.q_paddr,
                        }
                        if (strcmp(e->e_from.q_paddr, "<>") != 0)
                        {
                                (void) sendtolist(e->e_from.q_paddr,
-                                         (ADDRESS *) NULL,
-                                         &e->e_errorqueue, e);
+                                         NULLADDR, &e->e_errorqueue, e);
                        }
 
                        /*
                        }
 
                        /*
@@ -244,15 +274,18 @@ savemail(e)
 
                        e->e_flags |= EF_PM_NOTIFY;
 
 
                        e->e_flags |= EF_PM_NOTIFY;
 
-                       q = e->e_errorqueue;
+                       /* 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,
+                                          (e->e_class >= 0), e) == 0)
                        {
                                state = ESM_DONE;
                                break;
                        {
                                state = ESM_DONE;
                                break;
@@ -323,7 +356,9 @@ savemail(e)
                                e->e_to = buf;
                                q = NULL;
                                (void) sendtolist(buf, &e->e_from, &q, e);
                                e->e_to = buf;
                                q = NULL;
                                (void) sendtolist(buf, &e->e_from, &q, e);
-                               if (deliver(e, q) == 0)
+                               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;
@@ -346,19 +381,31 @@ 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);
+                       putline("\n", &mcibuf);
+                       (*e->e_putbody)(&mcibuf, e, NULL);
+                       putline("\n", &mcibuf);
                        (void) fflush(fp);
                        state = ferror(fp) ? ESM_PANIC : ESM_DONE;
                        (void) xfclose(fp, "savemail", "/usr/tmp/dead.letter");
                        (void) fflush(fp);
                        state = ferror(fp) ? ESM_PANIC : ESM_DONE;
                        (void) xfclose(fp, "savemail", "/usr/tmp/dead.letter");
@@ -371,8 +418,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");
                }
        }
 }
                }
        }
 }
@@ -438,6 +484,7 @@ 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);
        ee = newenvelope(&errenvelope, e);
        define('a', "\201b", ee);
        define('r', "internal", ee);
        ee = newenvelope(&errenvelope, e);
        define('a', "\201b", ee);
        define('r', "internal", ee);
@@ -445,12 +492,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 (!NoReturn)
+               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))
@@ -460,7 +509,7 @@ 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)
                        addheader("To", q->q_paddr, ee);
 
                if (q->q_alias == NULL)
                        addheader("To", q->q_paddr, ee);
@@ -487,7 +536,7 @@ returntosender(msg, returnq, sendbody, e)
 
        /* 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;
@@ -503,7 +552,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);
@@ -523,8 +572,7 @@ 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.
 **
 **     Returns:
 **             e -- the envelope we are working in.
 **
 **     Returns:
@@ -534,9 +582,8 @@ 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)
+       register MCI *mci;
        register ENVELOPE *e;
 {
        register FILE *xfile;
        register ENVELOPE *e;
 {
        register FILE *xfile;
@@ -548,7 +595,7 @@ errbody(fp, m, e)
        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;
        }
 
@@ -558,22 +605,39 @@ 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.
        */
 
        }
 
        /*
        **  Output introductory information.
        */
 
-       sprintf(buf, "The original message was received at %s", arpadate(NULL));
-       putline(buf, fp, m);
-       expand("from \201_", buf, &buf[sizeof buf - 1], e);
-       putline(buf, fp, m);
-       putline("", fp, m);
+       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).
@@ -589,17 +653,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);
                }
        }
 
                }
        }
 
@@ -615,7 +679,7 @@ errbody(fp, m, e)
                        if (printheader)
                        {
                                putline("   ----- The following addresses had delivery problems -----",
                        if (printheader)
                        {
                                putline("   ----- The following addresses had delivery problems -----",
-                                       fp, m);
+                                       mci);
                                printheader = FALSE;
                        }
                        strcpy(buf, q->q_paddr);
                                printheader = FALSE;
                        }
                        strcpy(buf, q->q_paddr);
@@ -623,18 +687,18 @@ errbody(fp, m, e)
                                strcat(buf, "  (unrecoverable error)");
                        else
                                strcat(buf, "  (transient failure)");
                                strcat(buf, "  (unrecoverable error)");
                        else
                                strcat(buf, "  (transient failure)");
-                       putline(buf, fp, m);
+                       putline(buf, mci);
                        if (q->q_alias != NULL)
                        {
                                strcpy(buf, "    (expanded from: ");
                                strcat(buf, q->q_alias->q_paddr);
                                strcat(buf, ")");
                        if (q->q_alias != NULL)
                        {
                                strcpy(buf, "    (expanded from: ");
                                strcat(buf, q->q_alias->q_paddr);
                                strcat(buf, ")");
-                               putline(buf, fp, m);
+                               putline(buf, mci);
                        }
                }
        }
        if (!printheader)
                        }
                }
        }
        if (!printheader)
-               putline("\n", fp, m);
+               putline("\n", mci);
 
        /*
        **  Output transcript of errors
 
        /*
        **  Output transcript of errors
@@ -645,15 +709,15 @@ 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;
@@ -664,42 +728,42 @@ errbody(fp, m, e)
 
        if (NoReturn)
                SendBody = FALSE;
 
        if (NoReturn)
                SendBody = FALSE;
-       putline("", fp, m);
+       putline("", mci);
        if (e->e_parent->e_df != NULL)
        {
                if (SendBody)
        if (e->e_parent->e_df != NULL)
        {
                if (SendBody)
-                       putline("   ----- Original message follows -----\n", fp, m);
+                       putline("   ----- Original message follows -----\n", mci);
                else
                else
-                       putline("   ----- Message header follows -----\n", fp, m);
-               (void) fflush(fp);
+                       putline("   ----- Message header follows -----\n", mci);
+               (void) fflush(mci->mci_out);
 
                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("Content-Type: message/rfc822", fp, m);
-                       putline("", fp, m);
+                       putline(buf, mci);
+                       putline("Content-Type: message/rfc822", mci);
+                       putline("", mci);
                }
                }
-               putheader(fp, m, e->e_parent);
-               putline("", fp, m);
+               putheader(mci, e->e_parent);
+               putline("", mci);
                if (SendBody)
                if (SendBody)
-                       putbody(fp, m, e->e_parent, e->e_msgboundary);
+                       putbody(mci, e->e_parent, e->e_msgboundary);
                else
                else
-                       putline("   ----- Message body suppressed -----", fp, m);
+                       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
@@ -728,7 +792,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;