check for "<<>>" etc as equivalent to "<>"
[unix-history] / usr / src / usr.sbin / sendmail / src / envelope.c
index e085418..f8e09e7 100644 (file)
@@ -1,17 +1,16 @@
 /*
  * 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.
  *
  * %sccs.include.redist.c%
  */
 
 #ifndef lint
  *
  * %sccs.include.redist.c%
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)envelope.c 6.34 (Berkeley) %G%";
+static char sccsid[] = "@(#)envelope.c 8.43 (Berkeley) %G%";
 #endif /* not lint */
 
 #include "sendmail.h"
 #endif /* not lint */
 
 #include "sendmail.h"
-#include <sys/time.h>
 #include <pwd.h>
 
 /*
 #include <pwd.h>
 
 /*
@@ -70,18 +69,28 @@ newenvelope(e, parent)
 **             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 failure_return = FALSE;
+       bool success_return = FALSE;
        register ADDRESS *q;
        char *id = e->e_id;
        register ADDRESS *q;
        char *id = e->e_id;
+       bool return_no, return_yes;
+       char buf[MAXLINE];
 
        if (tTd(50, 1))
        {
                printf("dropenvelope %x: id=", e);
                xputs(e->e_id);
 
        if (tTd(50, 1))
        {
                printf("dropenvelope %x: id=", e);
                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);
+               }
        }
 
        /* we must have an id to remove disk files */
        }
 
        /* we must have an id to remove disk files */
@@ -89,41 +98,163 @@ dropenvelope(e)
                return;
 
 #ifdef LOG
                return;
 
 #ifdef LOG
+       if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
+               logsender(e, NULL);
        if (LogLevel > 84)
        if (LogLevel > 84)
-               syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d",
+               syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d",
                                  id, e->e_flags, getpid());
 #endif /* LOG */
                                  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;
+       return_no = return_yes = FALSE;
        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;
+
+               /* see if a notification is needed */
+               if (bitset(QBADADDR, q->q_flags) &&
+                   bitset(QPINGONFAILURE, q->q_flags))
+               {
+                       failure_return = TRUE;
+                       if (q->q_owner == NULL && !emptyaddr(&e->e_from))
+                               (void) sendtolist(e->e_from.q_paddr, NULL,
+                                                 &e->e_errorqueue, e);
+               }
+               else if (bitset(QSENT, q->q_flags) &&
+                   bitnset(M_LOCALMAILER, q->q_mailer->m_flags) &&
+                   bitset(QPINGONSUCCESS, q->q_flags))
+               {
+                       success_return = TRUE;
+               }
+               else
+                       continue;
+
+               /* common code for error returns and return receipts */
+
+               /* test for returning the body */
+               if (!bitset(QHASRETPARAM, q->q_flags))
+               {
+                       if (!bitset(EF_NORETURN, e->e_flags))
+                               return_yes = TRUE;
+               }
+               else if (bitset(QNOBODYRETURN, q->q_flags))
+                       return_no = TRUE;
+               else
+                       return_yes = TRUE;
        }
        }
+       if (return_no && !return_yes)
+               e->e_flags |= EF_NORETURN;
+
+       /*
+       **  See if the message timed out.
+       */
+
+       if (!queueit)
+               /* nothing to do */ ;
+       else if (curtime() > e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
+       {
+               (void) sprintf(buf, "Cannot send message for %s",
+                       pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
+               if (e->e_message != NULL)
+                       free(e->e_message);
+               e->e_message = newstr(buf);
+               message(buf);
+               e->e_flags |= EF_CLRQUEUE;
+               failure_return = TRUE;
+               fprintf(e->e_xfp, "Message could not be delivered for %s\n",
+                       pintvl(TimeOuts.to_q_return[e->e_timeoutclass], 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[e->e_timeoutclass] > 0 &&
+           curtime() > e->e_ctime + TimeOuts.to_q_warning[e->e_timeoutclass])
+       {
+               if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
+                   e->e_class >= 0 &&
+                   strcmp(e->e_from.q_paddr, "<>") != 0 &&
+                   strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
+                   (strlen(e->e_from.q_paddr) <= 8 ||
+                    strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8], "-request") != 0))
+               {
+                       (void) sprintf(buf,
+                               "Warning: cannot send message for %s",
+                               pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE));
+                       if (e->e_message != NULL)
+                               free(e->e_message);
+                       e->e_message = newstr(buf);
+                       message(buf);
+                       e->e_flags |= EF_WARNING;
+                       failure_return = TRUE;
+               }
+               fprintf(e->e_xfp,
+                       "Warning: message still undelivered after %s\n",
+                       pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE));
+               fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
+                       pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
+               for (q = e->e_sendqueue; q != NULL; q = q->q_next)
+               {
+                       if (bitset(QQUEUEUP, q->q_flags))
+                               q->q_flags |= QREPORT;
+               }
+       }
+
+       if (tTd(50, 2))
+               printf("failure_return=%d success_return=%d queueit=%d\n",
+                       failure_return, success_return, queueit);
 
        /*
        **  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))
+*/
+       if (e->e_receiptto == NULL)
+               e->e_receiptto = e->e_from.q_paddr;
+       if (success_return && strcmp(e->e_receiptto, "<>") != 0)
        {
                auto ADDRESS *rlist = NULL;
 
        {
                auto ADDRESS *rlist = NULL;
 
-               (void) sendtolist(e->e_receiptto, (ADDRESS *) NULL, &rlist, e);
+               e->e_flags |= EF_SENDRECEIPT;
+               (void) sendtolist(e->e_receiptto, NULLADDR, &rlist, e);
                (void) returntosender("Return receipt", rlist, FALSE, 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) &&
-           e->e_errormode != EM_QUIET)
+       if (failure_return && 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.
        */
@@ -131,14 +262,22 @@ 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 (queueit=%d, e_flags=%x) =====\n\n",
+                               e->e_id, queueit, e->e_flags);
                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);
+               queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE);
 #else /* QUEUE */
                syserr("554 dropenvelope: queueup");
 #endif /* QUEUE */
 #else /* QUEUE */
                syserr("554 dropenvelope: queueup");
 #endif /* QUEUE */
@@ -153,11 +292,6 @@ dropenvelope(e)
                (void) xfclose(e->e_dfp, "dropenvelope", e->e_df);
        e->e_dfp = NULL;
        e->e_id = e->e_df = NULL;
                (void) xfclose(e->e_dfp, "dropenvelope", e->e_df);
        e->e_dfp = NULL;
        e->e_id = e->e_df = NULL;
-
-#ifdef LOG
-       if (LogLevel > 74)
-               syslog(LOG_INFO, "%s: done", id);
-#endif /* LOG */
 }
 \f/*
 **  CLEARENVELOPE -- clear an envelope without unlocking
 }
 \f/*
 **  CLEARENVELOPE -- clear an envelope without unlocking
@@ -179,6 +313,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;
@@ -228,17 +363,18 @@ clearenvelope(e, fullclear)
 **             forms is set.
 */
 
 **             forms is set.
 */
 
+void
 initsys(e)
        register ENVELOPE *e;
 {
 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
        static char ybuf[60];                   /* holds tty id */
        register char *p;
 #endif /* TTYNAME */
        extern char *ttyname();
 #ifdef TTYNAME
        static char ybuf[60];                   /* holds tty id */
        register char *p;
 #endif /* TTYNAME */
        extern char *ttyname();
-       extern char *macvalue();
+       extern void settime();
        extern char Version[];
 
        /*
        extern char Version[];
 
        /*
@@ -256,7 +392,7 @@ initsys(e)
        **      tucked away in the transcript).
        */
 
        **      tucked away in the transcript).
        */
 
-       if (OpMode == MD_DAEMON && !bitset(EF_QUEUERUN, e->e_flags) &&
+       if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
            e->e_xfp != NULL)
                OutChannel = e->e_xfp;
 
            e->e_xfp != NULL)
                OutChannel = e->e_xfp;
 
@@ -266,11 +402,11 @@ initsys(e)
 
        /* process id */
        (void) sprintf(pbuf, "%d", getpid());
 
        /* process id */
        (void) sprintf(pbuf, "%d", getpid());
-       define('p', pbuf, e);
+       define('p', newstr(pbuf), e);
 
        /* hop count */
        (void) sprintf(cbuf, "%d", e->e_hopcount);
 
        /* hop count */
        (void) sprintf(cbuf, "%d", e->e_hopcount);
-       define('c', cbuf, e);
+       define('c', newstr(cbuf), e);
 
        /* time as integer, unix time, arpa time */
        settime(e);
 
        /* time as integer, unix time, arpa time */
        settime(e);
@@ -303,29 +439,30 @@ initsys(e)
 **             Sets the various time macros -- $a, $b, $d, $t.
 */
 
 **             Sets the various time macros -- $a, $b, $d, $t.
 */
 
+void
 settime(e)
        register ENVELOPE *e;
 {
        register char *p;
        auto time_t now;
 settime(e)
        register ENVELOPE *e;
 {
        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);
        (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);
 
        now = curtime();
        tm = gmtime(&now);
        (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', tbuf, e);
+       define('t', newstr(tbuf), e);
        (void) strcpy(dbuf, ctime(&now));
        p = strchr(dbuf, '\n');
        if (p != NULL)
                *p = '\0';
        (void) strcpy(dbuf, ctime(&now));
        p = strchr(dbuf, '\n');
        if (p != NULL)
                *p = '\0';
-       define('d', dbuf, e);
-       p = newstr(arpadate(dbuf));
+       define('d', newstr(dbuf), e);
+       p = arpadate(dbuf);
+       p = newstr(p);
        if (macvalue('a', e) == NULL)
                define('a', p, e);
        define('b', p, e);
        if (macvalue('a', e) == NULL)
                define('a', p, e);
        define('b', p, e);
@@ -350,6 +487,7 @@ settime(e)
 #define O_APPEND       0
 #endif
 
 #define O_APPEND       0
 #endif
 
+void
 openxscript(e)
        register ENVELOPE *e;
 {
 openxscript(e)
        register ENVELOPE *e;
 {
@@ -361,9 +499,22 @@ openxscript(e)
        p = queuename(e, 'x');
        fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
        if (fd < 0)
        p = queuename(e, 'x');
        fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
        if (fd < 0)
-               syserr("Can't create %s", p);
-       else
-               e->e_xfp = fdopen(fd, "w");
+       {
+               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)
+       {
+               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.
@@ -378,6 +529,7 @@ openxscript(e)
 **             none.
 */
 
 **             none.
 */
 
+void
 closexscript(e)
        register ENVELOPE *e;
 {
 closexscript(e)
        register ENVELOPE *e;
 {
@@ -423,6 +575,7 @@ closexscript(e)
 **             sets sendmail's notion of who the from person is.
 */
 
 **             sets sendmail's notion of who the from person is.
 */
 
+void
 setsender(from, e, delimptr, internal)
        char *from;
        register ENVELOPE *e;
 setsender(from, e, delimptr, internal)
        char *from;
        register ENVELOPE *e;
@@ -433,11 +586,10 @@ setsender(from, e, delimptr, internal)
        char *realname = NULL;
        register struct passwd *pw;
        char delimchar;
        char *realname = NULL;
        register struct passwd *pw;
        char delimchar;
-       char buf[MAXNAME];
+       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 char *FullName;
 
        if (tTd(45, 1))
        extern char *FullName;
 
        if (tTd(45, 1))
@@ -448,21 +600,24 @@ setsender(from, e, delimptr, internal)
        **      Username can return errno != 0 on non-errors.
        */
 
        **      Username can return errno != 0 on non-errors.
        */
 
-       if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP)
+       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();
-       }
 
        if (ConfigLevel < 2)
                SuprErrs = TRUE;
 
        delimchar = internal ? '\0' : ' ';
 
        if (ConfigLevel < 2)
                SuprErrs = TRUE;
 
        delimchar = internal ? '\0' : ' ';
+       e->e_from.q_flags = QBADADDR;
        if (from == NULL ||
        if (from == NULL ||
-           parseaddr(from, &e->e_from, 1, delimchar, delimptr, e) == 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)
        {
                /* log garbage addresses for traceback */
 # ifdef LOG
        {
                /* log garbage addresses for traceback */
 # ifdef LOG
@@ -481,17 +636,31 @@ setsender(from, e, delimptr, internal)
                                p = ebuf;
                        }
                        syslog(LOG_NOTICE,
                                p = ebuf;
                        }
                        syslog(LOG_NOTICE,
-                               "from=%s unparseable, received from %s",
-                               from, p);
+                               "setsender: %s: invalid or unparseable, received from %s",
+                               shortenstring(from, 83), p);
                }
 # endif /* LOG */
                if (from != NULL)
                }
 # endif /* LOG */
                if (from != NULL)
+               {
+                       if (!bitset(QBADADDR, e->e_from.q_flags))
+                       {
+                               /* it was a bogus mailer in the from addr */
+                               usrerr("553 Invalid sender address");
+                       }
                        SuprErrs = TRUE;
                        SuprErrs = TRUE;
+               }
                if (from == realname ||
                if (from == realname ||
-                   parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL)
+                   parseaddr(from = newstr(realname), &e->e_from,
+                             RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL)
                {
                {
+                       char nbuf[100];
+
                        SuprErrs = TRUE;
                        SuprErrs = TRUE;
-                       if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL)
+                       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!");
                }
        }
                                syserr("553 setsender: can't even parse postmaster!");
                }
        }
@@ -505,34 +674,27 @@ setsender(from, e, delimptr, internal)
        }
        SuprErrs = FALSE;
 
        }
        SuprErrs = FALSE;
 
-       pvp = NULL;
-       if (e->e_from.q_mailer == LocalMailer)
-       {
 # ifdef USERDB
 # ifdef USERDB
+       if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
+       {
                register char *p;
                extern char *udbsender();
                register char *p;
                extern char *udbsender();
-# endif
 
 
+               p = udbsender(e->e_from.q_user);
+               if (p != NULL)
+                       from = p;
+       }
+# endif /* USERDB */
+
+       if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
+       {
                if (!internal)
                {
                if (!internal)
                {
-                       /* if the user has given fullname already, don't redefine */
+                       /* if the user already given fullname don't redefine */
                        if (FullName == NULL)
                                FullName = macvalue('x', e);
                        if (FullName != NULL && FullName[0] == '\0')
                                FullName = NULL;
                        if (FullName == NULL)
                                FullName = macvalue('x', e);
                        if (FullName != NULL && FullName[0] == '\0')
                                FullName = NULL;
-
-# ifdef USERDB
-                       p = udbsender(from);
-
-                       if (p != NULL)
-                       {
-                               /*
-                               **  We have an alternate address for the sender
-                               */
-
-                               pvp = prescan(p, '\0', pvpbuf, NULL);
-                       }
-# endif /* USERDB */
                }
 
                if ((pw = getpwnam(e->e_from.q_user)) != NULL)
                }
 
                if ((pw = getpwnam(e->e_from.q_user)) != NULL)
@@ -541,21 +703,26 @@ setsender(from, e, delimptr, internal)
                        **  Process passwd file entry.
                        */
 
                        **  Process passwd file entry.
                        */
 
-
                        /* extract home directory */
                        /* extract home directory */
-                       e->e_from.q_home = newstr(pw->pw_dir);
+                       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;
                        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)
                        {
 
                        /* 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 (buildfname(pw->pw_gecos, e->e_from.q_user, buf) &&
+                                       hvalue("MIME-Version", e) == NULL)
+                                               addheader("MIME-Version", "1.0", e);
                                if (buf[0] != '\0')
                                        FullName = newstr(buf);
                        }
                                if (buf[0] != '\0')
                                        FullName = newstr(buf);
                        }
@@ -563,12 +730,18 @@ setsender(from, e, delimptr, internal)
                if (FullName != NULL && !internal)
                        define('x', FullName, e);
        }
                if (FullName != NULL && !internal)
                        define('x', FullName, e);
        }
-       else if (!internal)
+       else if (!internal && OpMode != MD_DAEMON)
        {
                if (e->e_from.q_home == NULL)
        {
                if (e->e_from.q_home == NULL)
+               {
                        e->e_from.q_home = getenv("HOME");
                        e->e_from.q_home = getenv("HOME");
-               e->e_from.q_uid = getuid();
-               e->e_from.q_gid = getgid();
+                       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;
        }
 
        /*
        }
 
        /*
@@ -576,8 +749,7 @@ setsender(from, e, delimptr, internal)
        **      links in the net.
        */
 
        **      links in the net.
        */
 
-       if (pvp == NULL)
-               pvp = prescan(from, '\0', pvpbuf, NULL);
+       pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL);
        if (pvp == NULL)
        {
                /* don't need to give error -- prescan did that already */
        if (pvp == NULL)
        {
                /* don't need to give error -- prescan did that already */
@@ -587,15 +759,24 @@ setsender(from, e, delimptr, internal)
 # endif
                finis();
        }
 # endif
                finis();
        }
-       (void) rewrite(pvp, 3, e);
-       (void) rewrite(pvp, 1, e);
-       (void) rewrite(pvp, 4, e);
-       cataddr(pvp, NULL, buf, sizeof buf, '\0');
-       e->e_sender = newstr(buf);
+/*
+       (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 == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
+       {
+               /* 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 */
        define('f', e->e_sender, e);
 
        /* save the domain spec if this mailer wants it */
-       if (!internal && e->e_from.q_mailer != NULL &&
+       if (e->e_from.q_mailer != NULL &&
            bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
        {
                extern char **copyplist();
            bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
        {
                extern char **copyplist();