BSD 4_4_Lite1 release
[unix-history] / usr / src / usr.sbin / sendmail / src / envelope.c
index e5f2ba7..3a2314c 100644 (file)
@@ -1,29 +1,43 @@
 /*
  * Copyright (c) 1983 Eric P. Allman
 /*
  * Copyright (c) 1983 Eric P. Allman
- * Copyright (c) 1988 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1988, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * 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[] = "@(#)envelope.c 5.17 (Berkeley) %G%";
+static char sccsid[] = "@(#)envelope.c 8.34 (Berkeley) 4/14/94";
 #endif /* not lint */
 
 #endif /* not lint */
 
-#include <pwd.h>
-#include <sys/time.h>
 #include "sendmail.h"
 #include "sendmail.h"
-#include <sys/stat.h>
+#include <pwd.h>
 
 /*
 **  NEWENVELOPE -- allocate a new envelope
 
 /*
 **  NEWENVELOPE -- allocate a new envelope
@@ -32,6 +46,7 @@ static char sccsid[] = "@(#)envelope.c        5.17 (Berkeley) %G%";
 **
 **     Parameters:
 **             e -- the new envelope to fill in.
 **
 **     Parameters:
 **             e -- the new envelope to fill in.
+**             parent -- the envelope to be the parent of e.
 **
 **     Returns:
 **             e.
 **
 **     Returns:
 **             e.
@@ -41,15 +56,14 @@ static char sccsid[] = "@(#)envelope.c      5.17 (Berkeley) %G%";
 */
 
 ENVELOPE *
 */
 
 ENVELOPE *
-newenvelope(e)
+newenvelope(e, parent)
        register ENVELOPE *e;
        register ENVELOPE *e;
-{
        register ENVELOPE *parent;
        register ENVELOPE *parent;
+{
        extern putheader(), putbody();
        extern ENVELOPE BlankEnvelope;
 
        extern putheader(), putbody();
        extern ENVELOPE BlankEnvelope;
 
-       parent = CurEnv;
-       if (e == CurEnv)
+       if (e == parent && e->e_parent != NULL)
                parent = e->e_parent;
        clearenvelope(e, TRUE);
        if (e == CurEnv)
                parent = e->e_parent;
        clearenvelope(e, TRUE);
        if (e == CurEnv)
@@ -58,7 +72,8 @@ newenvelope(e)
                bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
        e->e_parent = parent;
        e->e_ctime = curtime();
                bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
        e->e_parent = parent;
        e->e_ctime = curtime();
-       e->e_msgpriority = parent->e_msgsize;
+       if (parent != NULL)
+               e->e_msgpriority = parent->e_msgsize;
        e->e_puthdr = putheader;
        e->e_putbody = putbody;
        if (CurEnv->e_xfp != NULL)
        e->e_puthdr = putheader;
        e->e_putbody = putbody;
        if (CurEnv->e_xfp != NULL)
@@ -80,60 +95,151 @@ newenvelope(e)
 **             Unlocks this queue file.
 */
 
 **             Unlocks this queue file.
 */
 
+void
 dropenvelope(e)
        register ENVELOPE *e;
 {
        bool queueit = FALSE;
 dropenvelope(e)
        register ENVELOPE *e;
 {
        bool queueit = FALSE;
+       bool saveit = bitset(EF_FATALERRS, e->e_flags);
        register ADDRESS *q;
        register ADDRESS *q;
+       char *id = e->e_id;
+       char buf[MAXLINE];
 
 
-#ifdef DEBUG
        if (tTd(50, 1))
        {
        if (tTd(50, 1))
        {
-               printf("dropenvelope %x id=", e);
+               printf("dropenvelope %x: id=", e);
                xputs(e->e_id);
                xputs(e->e_id);
-               printf(" flags=%o\n", e->e_flags);
+               printf(", flags=0x%x\n", e->e_flags);
+               if (tTd(50, 10))
+               {
+                       printf("sendq=");
+                       printaddr(e->e_sendqueue, TRUE);
+               }
        }
        }
-#endif DEBUG
-#ifdef LOG
-       if (LogLevel > 10)
-               syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d",
-                                 e->e_id == NULL ? "(none)" : e->e_id,
-                                 e->e_flags, getpid());
-#endif LOG
 
        /* we must have an id to remove disk files */
 
        /* we must have an id to remove disk files */
-       if (e->e_id == NULL)
+       if (id == NULL)
                return;
 
                return;
 
+#ifdef LOG
+       if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
+               logsender(e, NULL);
+       if (LogLevel > 84)
+               syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d",
+                                 id, e->e_flags, getpid());
+#endif /* LOG */
+       e->e_flags &= ~EF_LOGSENDER;
+
+       /* post statistics */
+       poststats(StatFile);
+
        /*
        **  Extract state information from dregs of send list.
        */
 
        /*
        **  Extract state information from dregs of send list.
        */
 
+       e->e_flags &= ~EF_QUEUERUN;
        for (q = e->e_sendqueue; q != NULL; q = q->q_next)
        {
                if (bitset(QQUEUEUP, q->q_flags))
                        queueit = TRUE;
        for (q = e->e_sendqueue; q != NULL; q = q->q_next)
        {
                if (bitset(QQUEUEUP, q->q_flags))
                        queueit = TRUE;
+               if (!bitset(QDONTSEND, q->q_flags) &&
+                   bitset(QBADADDR, q->q_flags))
+               {
+                       if (q->q_owner == NULL &&
+                           strcmp(e->e_from.q_paddr, "<>") != 0)
+                               (void) sendtolist(e->e_from.q_paddr, NULL,
+                                                 &e->e_errorqueue, e);
+               }
+       }
+
+       /*
+       **  See if the message timed out.
+       */
+
+       if (!queueit)
+               /* nothing to do */ ;
+       else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
+       {
+               (void) sprintf(buf, "Cannot send message for %s",
+                       pintvl(TimeOuts.to_q_return, FALSE));
+               if (e->e_message != NULL)
+                       free(e->e_message);
+               e->e_message = newstr(buf);
+               message(buf);
+               e->e_flags |= EF_CLRQUEUE;
+               saveit = TRUE;
+               fprintf(e->e_xfp, "Message could not be delivered for %s\n",
+                       pintvl(TimeOuts.to_q_return, FALSE));
+               fprintf(e->e_xfp, "Message will be deleted from queue\n");
+               for (q = e->e_sendqueue; q != NULL; q = q->q_next)
+               {
+                       if (bitset(QQUEUEUP, q->q_flags))
+                               q->q_flags |= QBADADDR;
+               }
+       }
+       else if (TimeOuts.to_q_warning > 0 &&
+           curtime() > e->e_ctime + TimeOuts.to_q_warning)
+       {
+               if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
+                   e->e_class >= 0 &&
+                   strcmp(e->e_from.q_paddr, "<>") != 0)
+               {
+                       (void) sprintf(buf,
+                               "warning: cannot send message for %s",
+                               pintvl(TimeOuts.to_q_warning, FALSE));
+                       if (e->e_message != NULL)
+                               free(e->e_message);
+                       e->e_message = newstr(buf);
+                       message(buf);
+                       e->e_flags |= EF_WARNING;
+                       saveit = TRUE;
+               }
+               fprintf(e->e_xfp,
+                       "Warning: message still undelivered after %s\n",
+                       pintvl(TimeOuts.to_q_warning, FALSE));
+               fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
+                       pintvl(TimeOuts.to_q_return, FALSE));
+               for (q = e->e_sendqueue; q != NULL; q = q->q_next)
+               {
+                       if (bitset(QQUEUEUP, q->q_flags))
+                               q->q_flags |= QREPORT;
+               }
        }
 
        /*
        **  Send back return receipts as requested.
        */
 
        }
 
        /*
        **  Send back return receipts as requested.
        */
 
-       if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags))
+       if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)
+           && !bitset(PRIV_NORECEIPTS, PrivacyFlags))
        {
                auto ADDRESS *rlist = NULL;
 
        {
                auto ADDRESS *rlist = NULL;
 
-               sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist);
-               (void) returntosender("Return receipt", rlist, FALSE);
+               (void) sendtolist(e->e_receiptto, NULLADDR, &rlist, e);
+               (void) returntosender("Return receipt", rlist, FALSE, e);
+               e->e_flags &= ~EF_SENDRECEIPT;
        }
 
        /*
        **  Arrange to send error messages if there are fatal errors.
        */
 
        }
 
        /*
        **  Arrange to send error messages if there are fatal errors.
        */
 
-       if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET)
+       if (saveit && e->e_errormode != EM_QUIET)
                savemail(e);
 
                savemail(e);
 
+       /*
+       **  Arrange to send warning messages to postmaster as requested.
+       */
+
+       if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL &&
+           !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0)
+       {
+               auto ADDRESS *rlist = NULL;
+
+               (void) sendtolist(PostMasterCopy, NULLADDR, &rlist, e);
+               (void) returntosender(e->e_message, rlist, FALSE, e);
+       }
+
        /*
        **  Instantiate or deinstantiate the queue.
        */
        /*
        **  Instantiate or deinstantiate the queue.
        */
@@ -141,17 +247,24 @@ dropenvelope(e)
        if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
            bitset(EF_CLRQUEUE, e->e_flags))
        {
        if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
            bitset(EF_CLRQUEUE, e->e_flags))
        {
+               if (tTd(50, 1))
+                       printf("\n===== Dropping [dq]f%s =====\n\n", e->e_id);
                if (e->e_df != NULL)
                        xunlink(e->e_df);
                xunlink(queuename(e, 'q'));
                if (e->e_df != NULL)
                        xunlink(e->e_df);
                xunlink(queuename(e, 'q'));
+
+#ifdef LOG
+               if (LogLevel > 10)
+                       syslog(LOG_INFO, "%s: done", id);
+#endif
        }
        else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
        {
 #ifdef QUEUE
        }
        else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
        {
 #ifdef QUEUE
-               queueup(e, FALSE, FALSE);
-#else QUEUE
-               syserr("dropenvelope: queueup");
-#endif QUEUE
+               queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE);
+#else /* QUEUE */
+               syserr("554 dropenvelope: queueup");
+#endif /* QUEUE */
        }
 
        /* now unlock the job */
        }
 
        /* now unlock the job */
@@ -159,10 +272,10 @@ dropenvelope(e)
        unlockqueue(e);
 
        /* make sure that this envelope is marked unused */
        unlockqueue(e);
 
        /* make sure that this envelope is marked unused */
-       e->e_id = e->e_df = NULL;
        if (e->e_dfp != NULL)
        if (e->e_dfp != NULL)
-               (void) fclose(e->e_dfp);
+               (void) xfclose(e->e_dfp, "dropenvelope", e->e_df);
        e->e_dfp = NULL;
        e->e_dfp = NULL;
+       e->e_id = e->e_df = NULL;
 }
 \f/*
 **  CLEARENVELOPE -- clear an envelope without unlocking
 }
 \f/*
 **  CLEARENVELOPE -- clear an envelope without unlocking
@@ -184,6 +297,7 @@ dropenvelope(e)
 **             Marks the envelope as unallocated.
 */
 
 **             Marks the envelope as unallocated.
 */
 
+void
 clearenvelope(e, fullclear)
        register ENVELOPE *e;
        bool fullclear;
 clearenvelope(e, fullclear)
        register ENVELOPE *e;
        bool fullclear;
@@ -196,13 +310,16 @@ clearenvelope(e, fullclear)
        {
                /* clear out any file information */
                if (e->e_xfp != NULL)
        {
                /* clear out any file information */
                if (e->e_xfp != NULL)
-                       (void) fclose(e->e_xfp);
+                       (void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id);
                if (e->e_dfp != NULL)
                if (e->e_dfp != NULL)
-                       (void) fclose(e->e_dfp);
+                       (void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df);
+               e->e_xfp = e->e_dfp = NULL;
        }
 
        /* now clear out the data */
        STRUCTCOPY(BlankEnvelope, *e);
        }
 
        /* now clear out the data */
        STRUCTCOPY(BlankEnvelope, *e);
+       if (Verbose)
+               e->e_sendmode = SM_DELIVER;
        bh = BlankEnvelope.e_header;
        nhp = &e->e_header;
        while (bh != NULL)
        bh = BlankEnvelope.e_header;
        nhp = &e->e_header;
        while (bh != NULL)
@@ -230,16 +347,18 @@ clearenvelope(e, fullclear)
 **             forms is set.
 */
 
 **             forms is set.
 */
 
-initsys()
+void
+initsys(e)
+       register ENVELOPE *e;
 {
 {
-       static char cbuf[5];                    /* holds hop count */
-       static char pbuf[10];                   /* holds pid */
+       char cbuf[5];                           /* holds hop count */
+       char pbuf[10];                          /* holds pid */
 #ifdef TTYNAME
 #ifdef TTYNAME
-       static char ybuf[10];                   /* holds tty id */
+       static char ybuf[60];                   /* holds tty id */
        register char *p;
        register char *p;
-#endif TTYNAME
+#endif /* TTYNAME */
        extern char *ttyname();
        extern char *ttyname();
-       extern char *macvalue();
+       extern void settime();
        extern char Version[];
 
        /*
        extern char Version[];
 
        /*
@@ -247,8 +366,8 @@ initsys()
        **      I.e., an id, a transcript, and a creation time.
        */
 
        **      I.e., an id, a transcript, and a creation time.
        */
 
-       openxscript(CurEnv);
-       CurEnv->e_ctime = curtime();
+       openxscript(e);
+       e->e_ctime = curtime();
 
        /*
        **  Set OutChannel to something useful if stdout isn't it.
 
        /*
        **  Set OutChannel to something useful if stdout isn't it.
@@ -257,8 +376,9 @@ initsys()
        **      tucked away in the transcript).
        */
 
        **      tucked away in the transcript).
        */
 
-       if (OpMode == MD_DAEMON && QueueRun)
-               OutChannel = CurEnv->e_xfp;
+       if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
+           e->e_xfp != NULL)
+               OutChannel = e->e_xfp;
 
        /*
        **  Set up some basic system macros.
 
        /*
        **  Set up some basic system macros.
@@ -266,29 +386,29 @@ initsys()
 
        /* process id */
        (void) sprintf(pbuf, "%d", getpid());
 
        /* process id */
        (void) sprintf(pbuf, "%d", getpid());
-       define('p', pbuf, CurEnv);
+       define('p', newstr(pbuf), e);
 
        /* hop count */
 
        /* hop count */
-       (void) sprintf(cbuf, "%d", CurEnv->e_hopcount);
-       define('c', cbuf, CurEnv);
+       (void) sprintf(cbuf, "%d", e->e_hopcount);
+       define('c', newstr(cbuf), e);
 
        /* time as integer, unix time, arpa time */
 
        /* time as integer, unix time, arpa time */
-       settime();
+       settime(e);
 
 #ifdef TTYNAME
        /* tty name */
 
 #ifdef TTYNAME
        /* tty name */
-       if (macvalue('y', CurEnv) == NULL)
+       if (macvalue('y', e) == NULL)
        {
                p = ttyname(2);
                if (p != NULL)
                {
        {
                p = ttyname(2);
                if (p != NULL)
                {
-                       if (rindex(p, '/') != NULL)
-                               p = rindex(p, '/') + 1;
+                       if (strrchr(p, '/') != NULL)
+                               p = strrchr(p, '/') + 1;
                        (void) strcpy(ybuf, p);
                        (void) strcpy(ybuf, p);
-                       define('y', ybuf, CurEnv);
+                       define('y', ybuf, e);
                }
        }
                }
        }
-#endif TTYNAME
+#endif /* TTYNAME */
 }
 \f/*
 **  SETTIME -- set the current time.
 }
 \f/*
 **  SETTIME -- set the current time.
@@ -303,30 +423,33 @@ initsys()
 **             Sets the various time macros -- $a, $b, $d, $t.
 */
 
 **             Sets the various time macros -- $a, $b, $d, $t.
 */
 
-settime()
+void
+settime(e)
+       register ENVELOPE *e;
 {
        register char *p;
        auto time_t now;
 {
        register char *p;
        auto time_t now;
-       static char tbuf[20];                   /* holds "current" time */
-       static char dbuf[30];                   /* holds ctime(tbuf) */
+       char tbuf[20];                          /* holds "current" time */
+       char dbuf[30];                          /* holds ctime(tbuf) */
        register struct tm *tm;
        extern char *arpadate();
        extern struct tm *gmtime();
        register struct tm *tm;
        extern char *arpadate();
        extern struct tm *gmtime();
-       extern char *macvalue();
 
        now = curtime();
        tm = gmtime(&now);
 
        now = curtime();
        tm = gmtime(&now);
-       (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1,
-                       tm->tm_mday, tm->tm_hour, tm->tm_min);
-       define('t', tbuf, CurEnv);
+       (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
+                       tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
+       define('t', newstr(tbuf), e);
        (void) strcpy(dbuf, ctime(&now));
        (void) strcpy(dbuf, ctime(&now));
-       *index(dbuf, '\n') = '\0';
-       if (macvalue('d', CurEnv) == NULL)
-               define('d', dbuf, CurEnv);
-       p = newstr(arpadate(dbuf));
-       if (macvalue('a', CurEnv) == NULL)
-               define('a', p, CurEnv);
-       define('b', p, CurEnv);
+       p = strchr(dbuf, '\n');
+       if (p != NULL)
+               *p = '\0';
+       define('d', newstr(dbuf), e);
+       p = arpadate(dbuf);
+       p = newstr(p);
+       if (macvalue('a', e) == NULL)
+               define('a', p, e);
+       define('b', p, e);
 }
 \f/*
 **  OPENXSCRIPT -- Open transcript file
 }
 \f/*
 **  OPENXSCRIPT -- Open transcript file
@@ -344,23 +467,38 @@ settime()
 **             Creates the transcript file.
 */
 
 **             Creates the transcript file.
 */
 
+#ifndef O_APPEND
+#define O_APPEND       0
+#endif
+
+void
 openxscript(e)
        register ENVELOPE *e;
 {
        register char *p;
 openxscript(e)
        register ENVELOPE *e;
 {
        register char *p;
+       int fd;
 
 
-# ifdef LOG
-       if (LogLevel > 19)
-               syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)");
-# endif LOG
        if (e->e_xfp != NULL)
                return;
        p = queuename(e, 'x');
        if (e->e_xfp != NULL)
                return;
        p = queuename(e, 'x');
-       e->e_xfp = fopen(p, "w");
+       fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
+       if (fd < 0)
+       {
+               syserr("Can't create transcript file %s", p);
+               fd = open("/dev/null", O_WRONLY, 0644);
+               if (fd < 0)
+                       syserr("!Can't open /dev/null");
+       }
+       e->e_xfp = fdopen(fd, "w");
        if (e->e_xfp == NULL)
        if (e->e_xfp == NULL)
-               syserr("Can't create %s", p);
-       else
-               (void) chmod(p, 0644);
+       {
+               syserr("!Can't create transcript stream %s", p);
+       }
+       if (tTd(46, 9))
+       {
+               printf("openxscript(%s):\n  ", p);
+               dumpfd(fileno(e->e_xfp), TRUE, FALSE);
+       }
 }
 \f/*
 **  CLOSEXSCRIPT -- close the transcript file.
 }
 \f/*
 **  CLOSEXSCRIPT -- close the transcript file.
@@ -375,12 +513,13 @@ openxscript(e)
 **             none.
 */
 
 **             none.
 */
 
+void
 closexscript(e)
        register ENVELOPE *e;
 {
        if (e->e_xfp == NULL)
                return;
 closexscript(e)
        register ENVELOPE *e;
 {
        if (e->e_xfp == NULL)
                return;
-       (void) fclose(e->e_xfp);
+       (void) xfclose(e->e_xfp, "closexscript", e->e_id);
        e->e_xfp = NULL;
 }
 \f/*
        e->e_xfp = NULL;
 }
 \f/*
@@ -407,6 +546,11 @@ closexscript(e)
 **     Parameters:
 **             from -- the person we would like to believe this message
 **                     is from, as specified on the command line.
 **     Parameters:
 **             from -- the person we would like to believe this message
 **                     is from, as specified on the command line.
+**             e -- the envelope in which we would like the sender set.
+**             delimptr -- if non-NULL, set to the location of the
+**                     trailing delimiter.
+**             internal -- set if this address is coming from an internal
+**                     source such as an owner alias.
 **
 **     Returns:
 **             none.
 **
 **     Returns:
 **             none.
@@ -415,132 +559,178 @@ closexscript(e)
 **             sets sendmail's notion of who the from person is.
 */
 
 **             sets sendmail's notion of who the from person is.
 */
 
-setsender(from)
+void
+setsender(from, e, delimptr, internal)
        char *from;
        char *from;
+       register ENVELOPE *e;
+       char **delimptr;
+       bool internal;
 {
        register char **pvp;
        char *realname = NULL;
        register struct passwd *pw;
 {
        register char **pvp;
        char *realname = NULL;
        register struct passwd *pw;
-       char buf[MAXNAME];
+       char delimchar;
+       char *bp;
+       char buf[MAXNAME + 2];
        char pvpbuf[PSBUFSIZE];
        extern struct passwd *getpwnam();
        char pvpbuf[PSBUFSIZE];
        extern struct passwd *getpwnam();
-       extern char *macvalue();
-       extern char **prescan();
-       extern bool safefile();
        extern char *FullName;
 
        extern char *FullName;
 
-# ifdef DEBUG
        if (tTd(45, 1))
                printf("setsender(%s)\n", from == NULL ? "" : from);
        if (tTd(45, 1))
                printf("setsender(%s)\n", from == NULL ? "" : from);
-# endif DEBUG
 
        /*
        **  Figure out the real user executing us.
        **      Username can return errno != 0 on non-errors.
        */
 
 
        /*
        **  Figure out the real user executing us.
        **      Username can return errno != 0 on non-errors.
        */
 
-       if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP)
+       if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
+           OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
                realname = from;
        if (realname == NULL || realname[0] == '\0')
                realname = from;
        if (realname == NULL || realname[0] == '\0')
-       {
-               extern char *username();
-
                realname = username();
                realname = username();
-       }
-
-       /*
-       **  Determine if this real person is allowed to alias themselves.
-       */
 
 
-       if (from != NULL)
+       if (ConfigLevel < 2)
+               SuprErrs = TRUE;
+
+       delimchar = internal ? '\0' : ' ';
+       e->e_from.q_flags = QBADADDR;
+       if (from == NULL ||
+           parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
+                     delimchar, delimptr, e) == NULL ||
+           bitset(QBADADDR, e->e_from.q_flags) ||
+           e->e_from.q_mailer == ProgMailer ||
+           e->e_from.q_mailer == FileMailer ||
+           e->e_from.q_mailer == InclMailer)
        {
        {
-               extern bool trusteduser();
-
-               if (!trusteduser(realname) && getuid() != geteuid() &&
-                   index(from, '!') == NULL && getuid() != 0)
+               /* log garbage addresses for traceback */
+# ifdef LOG
+               if (from != NULL && LogLevel > 2)
                {
                {
-                       /* network sends -r regardless (why why why?) */
-                       /* syserr("%s, you cannot use the -f flag", realname); */
-                       from = NULL;
+                       char *p;
+                       char ebuf[MAXNAME * 2 + 2];
+
+                       p = macvalue('_', e);
+                       if (p == NULL)
+                       {
+                               char *host = RealHostName;
+                               if (host == NULL)
+                                       host = MyHostName;
+                               (void) sprintf(ebuf, "%s@%s", realname, host);
+                               p = ebuf;
+                       }
+                       syslog(LOG_NOTICE,
+                               "setsender: %s: invalid or unparseable, received from %s",
+                               shortenstring(from, 83), p);
                }
                }
-       }
-
-       SuprErrs = TRUE;
-       if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL)
-       {
-               /* log garbage addresses for traceback */
+# endif /* LOG */
                if (from != NULL)
                {
                if (from != NULL)
                {
-# ifdef LOG
-                       if (LogLevel >= 1)
-                           if (realname == from && RealHostName != NULL)
-                               syslog(LOG_NOTICE,
-                                   "from=%s unparseable, received from %s",
-                                   from, RealHostName);
-                           else
-                               syslog(LOG_NOTICE,
-                                   "Unparseable username %s wants from=%s",
-                                   realname, from);
-# endif LOG
+                       if (!bitset(QBADADDR, e->e_from.q_flags))
+                       {
+                               /* it was a bogus mailer in the from addr */
+                               usrerr("553 Invalid sender address");
+                       }
+                       SuprErrs = TRUE;
                }
                }
-               from = newstr(realname);
-               if (parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL &&
-                   parseaddr("postmaster", &CurEnv->e_from, 1, '\0') == NULL)
+               if (from == realname ||
+                   parseaddr(from = newstr(realname), &e->e_from,
+                             RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL)
                {
                {
-                       syserr("setsender: can't even parse postmaster!");
+                       char nbuf[100];
+
+                       SuprErrs = TRUE;
+                       expand("\201n", nbuf, &nbuf[sizeof nbuf], e);
+                       if (parseaddr(from = newstr(nbuf), &e->e_from,
+                                     RF_COPYALL, ' ', NULL, e) == NULL &&
+                           parseaddr(from = "postmaster", &e->e_from,
+                                     RF_COPYALL, ' ', NULL, e) == NULL)
+                               syserr("553 setsender: can't even parse postmaster!");
                }
        }
        else
                FromFlag = TRUE;
                }
        }
        else
                FromFlag = TRUE;
-       CurEnv->e_from.q_flags |= QDONTSEND;
-       loweraddr(&CurEnv->e_from);
+       e->e_from.q_flags |= QDONTSEND;
+       if (tTd(45, 5))
+       {
+               printf("setsender: QDONTSEND ");
+               printaddr(&e->e_from, FALSE);
+       }
        SuprErrs = FALSE;
 
        SuprErrs = FALSE;
 
-       if (CurEnv->e_from.q_mailer == LocalMailer &&
-           (pw = getpwnam(CurEnv->e_from.q_user)) != NULL)
+       pvp = NULL;
+       if (e->e_from.q_mailer == LocalMailer)
        {
        {
-               /*
-               **  Process passwd file entry.
-               */
-
-
-               /* extract home directory */
-               CurEnv->e_from.q_home = newstr(pw->pw_dir);
-               define('z', CurEnv->e_from.q_home, CurEnv);
-
-               /* extract user and group id */
-               CurEnv->e_from.q_uid = pw->pw_uid;
-               CurEnv->e_from.q_gid = pw->pw_gid;
+# ifdef USERDB
+               register char *p;
+               extern char *udbsender();
+# endif
 
 
-               /* if the user has given fullname already, don't redefine */
-               if (FullName == NULL)
-                       FullName = macvalue('x', CurEnv);
-               if (FullName != NULL && FullName[0] == '\0')
-                       FullName = NULL;
+               if (!internal)
+               {
+                       /* if the user has given fullname already, don't redefine */
+                       if (FullName == NULL)
+                               FullName = macvalue('x', e);
+                       if (FullName != NULL && FullName[0] == '\0')
+                               FullName = NULL;
+
+# ifdef USERDB
+                       p = udbsender(e->e_from.q_user);
+
+                       if (p != NULL)
+                       {
+                               /*
+                               **  We have an alternate address for the sender
+                               */
+
+                               pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL);
+                       }
+# endif /* USERDB */
+               }
 
 
-               /* extract full name from passwd file */
-               if (FullName == NULL && pw->pw_gecos != NULL &&
-                   strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0)
+               if ((pw = getpwnam(e->e_from.q_user)) != NULL)
                {
                {
-                       buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf);
-                       if (buf[0] != '\0')
-                               FullName = newstr(buf);
+                       /*
+                       **  Process passwd file entry.
+                       */
+
+                       /* extract home directory */
+                       if (strcmp(pw->pw_dir, "/") == 0)
+                               e->e_from.q_home = newstr("");
+                       else
+                               e->e_from.q_home = newstr(pw->pw_dir);
+                       define('z', e->e_from.q_home, e);
+
+                       /* extract user and group id */
+                       e->e_from.q_uid = pw->pw_uid;
+                       e->e_from.q_gid = pw->pw_gid;
+                       e->e_from.q_flags |= QGOODUID;
+
+                       /* extract full name from passwd file */
+                       if (FullName == NULL && pw->pw_gecos != NULL &&
+                           strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
+                           !internal)
+                       {
+                               buildfname(pw->pw_gecos, e->e_from.q_user, buf);
+                               if (buf[0] != '\0')
+                                       FullName = newstr(buf);
+                       }
                }
                }
-               if (FullName != NULL)
-                       define('x', FullName, CurEnv);
-       }
-       else
-       {
-               if (CurEnv->e_from.q_home == NULL)
-                       CurEnv->e_from.q_home = getenv("HOME");
-               CurEnv->e_from.q_uid = getuid();
-               CurEnv->e_from.q_gid = getgid();
+               if (FullName != NULL && !internal)
+                       define('x', FullName, e);
        }
        }
-
-       if (CurEnv->e_from.q_uid != 0)
+       else if (!internal && OpMode != MD_DAEMON)
        {
        {
-               DefUid = CurEnv->e_from.q_uid;
-               DefGid = CurEnv->e_from.q_gid;
+               if (e->e_from.q_home == NULL)
+               {
+                       e->e_from.q_home = getenv("HOME");
+                       if (e->e_from.q_home != NULL &&
+                           strcmp(e->e_from.q_home, "/") == 0)
+                               e->e_from.q_home++;
+               }
+               e->e_from.q_uid = RealUid;
+               e->e_from.q_gid = RealGid;
+               e->e_from.q_flags |= QGOODUID;
        }
 
        /*
        }
 
        /*
@@ -548,57 +738,40 @@ setsender(from)
        **      links in the net.
        */
 
        **      links in the net.
        */
 
-       pvp = prescan(from, '\0', pvpbuf);
+       if (pvp == NULL)
+               pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL);
        if (pvp == NULL)
        {
        if (pvp == NULL)
        {
+               /* don't need to give error -- prescan did that already */
 # ifdef LOG
 # ifdef LOG
-               if (LogLevel >= 1)
+               if (LogLevel > 2)
                        syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
 # endif
                        syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
 # endif
-               usrerr("cannot prescan from (%s)", from);
                finis();
        }
                finis();
        }
-       rewrite(pvp, 3);
-       rewrite(pvp, 1);
-       rewrite(pvp, 4);
-       cataddr(pvp, buf, sizeof buf);
-       define('f', newstr(buf), CurEnv);
+       (void) rewrite(pvp, 3, 0, e);
+       (void) rewrite(pvp, 1, 0, e);
+       (void) rewrite(pvp, 4, 0, e);
+       bp = buf + 1;
+       cataddr(pvp, NULL, bp, sizeof buf - 2, '\0');
+       if (*bp == '@')
+       {
+               /* heuristic: route-addr: add angle brackets */
+               strcat(bp, ">");
+               *--bp = '<';
+       }
+       e->e_sender = newstr(bp);
+       define('f', e->e_sender, e);
 
        /* save the domain spec if this mailer wants it */
 
        /* save the domain spec if this mailer wants it */
-       if (CurEnv->e_from.q_mailer != NULL &&
-           bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags))
+       if (e->e_from.q_mailer != NULL &&
+           bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
        {
                extern char **copyplist();
 
                while (*pvp != NULL && strcmp(*pvp, "@") != 0)
                        pvp++;
                if (*pvp != NULL)
        {
                extern char **copyplist();
 
                while (*pvp != NULL && strcmp(*pvp, "@") != 0)
                        pvp++;
                if (*pvp != NULL)
-                       CurEnv->e_fromdomain = copyplist(pvp, TRUE);
+                       e->e_fromdomain = copyplist(pvp, TRUE);
        }
 }
        }
 }
-\f/*
-**  TRUSTEDUSER -- tell us if this user is to be trusted.
-**
-**     Parameters:
-**             user -- the user to be checked.
-**
-**     Returns:
-**             TRUE if the user is in an approved list.
-**             FALSE otherwise.
-**
-**     Side Effects:
-**             none.
-*/
-
-bool
-trusteduser(user)
-       char *user;
-{
-       register char **ulist;
-       extern char *TrustedUsers[];
-
-       for (ulist = TrustedUsers; *ulist != NULL; ulist++)
-               if (strcmp(*ulist, user) == 0)
-                       return (TRUE);
-       return (FALSE);
-}