huge number of changes for 8.2, including IRIX & Solaris portability;
[unix-history] / usr / src / usr.sbin / sendmail / src / envelope.c
index 0ae656a..32716bb 100644 (file)
@@ -1,21 +1,18 @@
 /*
  * 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.11 (Berkeley) %G%";
+static char sccsid[] = "@(#)envelope.c 8.2 (Berkeley) %G%";
 #endif /* not lint */
 
 #endif /* not lint */
 
-#include <sys/types.h>
+#include "sendmail.h"
 #include <sys/time.h>
 #include <sys/time.h>
-#include <sys/stat.h>
 #include <pwd.h>
 #include <pwd.h>
-#include <fcntl.h>
-#include "sendmail.h"
 
 /*
 **  NEWENVELOPE -- allocate a new envelope
 
 /*
 **  NEWENVELOPE -- allocate a new envelope
@@ -24,6 +21,7 @@ static char sccsid[] = "@(#)envelope.c        6.11 (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.
@@ -33,15 +31,14 @@ static char sccsid[] = "@(#)envelope.c      6.11 (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 && e->e_parent != NULL)
+       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)
@@ -73,22 +70,30 @@ newenvelope(e)
 **             Unlocks this queue file.
 */
 
 **             Unlocks this queue file.
 */
 
+void
 dropenvelope(e)
        register ENVELOPE *e;
 {
        bool queueit = FALSE;
        register ADDRESS *q;
        char *id = e->e_id;
 dropenvelope(e)
        register ENVELOPE *e;
 {
        bool queueit = FALSE;
        register ADDRESS *q;
        char *id = e->e_id;
+       char buf[MAXLINE];
 
        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=%o\n", e->e_flags);
+               if (tTd(50, 10))
+               {
+                       printf("sendq=");
+                       printaddr(e->e_sendqueue, TRUE);
+               }
        }
 
        }
 
+       /* we must have an id to remove disk files */
        if (id == NULL)
        if (id == NULL)
-               id = "(none)";
+               return;
 
 #ifdef LOG
        if (LogLevel > 84)
 
 #ifdef LOG
        if (LogLevel > 84)
@@ -96,9 +101,8 @@ dropenvelope(e)
                                  id, e->e_flags, getpid());
 #endif /* LOG */
 
                                  id, e->e_flags, getpid());
 #endif /* LOG */
 
-       /* we must have an id to remove disk files */
-       if (e->e_id == NULL)
-               return;
+       /* post statistics */
+       poststats(StatFile);
 
        /*
        **  Extract state information from dregs of send list.
 
        /*
        **  Extract state information from dregs of send list.
@@ -110,6 +114,49 @@ dropenvelope(e)
                        queueit = TRUE;
        }
 
                        queueit = TRUE;
        }
 
+       /*
+       **  See if the message timed out.
+       */
+
+       if (!queueit)
+               /* nothing to do */ ;
+       else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
+       {
+               if (!bitset(EF_TIMEOUT, e->e_flags))
+               {
+                       (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_TIMEOUT|EF_CLRQUEUE;
+               fprintf(e->e_xfp, "421 Message timed out\n");
+       }
+       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|EF_TIMEOUT;
+               }
+               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));
+       }
+
        /*
        **  Send back return receipts as requested.
        */
        /*
        **  Send back return receipts as requested.
        */
@@ -126,7 +173,8 @@ dropenvelope(e)
        **  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 (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) &&
+           e->e_errormode != EM_QUIET)
                savemail(e);
 
        /*
                savemail(e);
 
        /*
@@ -136,6 +184,8 @@ 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, 2))
+                       printf("Dropping envelope\n");
                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'));
@@ -145,7 +195,7 @@ dropenvelope(e)
 #ifdef QUEUE
                queueup(e, FALSE, FALSE);
 #else /* QUEUE */
 #ifdef QUEUE
                queueup(e, FALSE, FALSE);
 #else /* QUEUE */
-               syserr("dropenvelope: queueup");
+               syserr("554 dropenvelope: queueup");
 #endif /* QUEUE */
        }
 
 #endif /* QUEUE */
        }
 
@@ -154,10 +204,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;
 
 #ifdef LOG
        if (LogLevel > 74)
 
 #ifdef LOG
        if (LogLevel > 74)
@@ -184,6 +234,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 +247,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,17 +284,18 @@ clearenvelope(e, fullclear)
 **             forms is set.
 */
 
 **             forms is set.
 */
 
+void
 initsys(e)
        register ENVELOPE *e;
 {
        static char cbuf[5];                    /* holds hop count */
        static char pbuf[10];                   /* holds pid */
 #ifdef TTYNAME
 initsys(e)
        register ENVELOPE *e;
 {
        static char cbuf[5];                    /* holds hop count */
        static char pbuf[10];                   /* holds pid */
 #ifdef TTYNAME
-       static char ybuf[10];                   /* holds tty id */
+       static char ybuf[60];                   /* holds tty id */
        register char *p;
 #endif /* TTYNAME */
        extern char *ttyname();
        register char *p;
 #endif /* TTYNAME */
        extern char *ttyname();
-       extern char *macvalue();
+       extern void settime();
        extern char Version[];
 
        /*
        extern char Version[];
 
        /*
@@ -258,7 +313,8 @@ initsys(e)
        **      tucked away in the transcript).
        */
 
        **      tucked away in the transcript).
        */
 
-       if (OpMode == MD_DAEMON && QueueRun && e->e_xfp != NULL)
+       if (OpMode == MD_DAEMON && !bitset(EF_QUEUERUN, e->e_flags) &&
+           e->e_xfp != NULL)
                OutChannel = e->e_xfp;
 
        /*
                OutChannel = e->e_xfp;
 
        /*
@@ -304,6 +360,7 @@ 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;
 {
 settime(e)
        register ENVELOPE *e;
 {
@@ -314,7 +371,6 @@ settime(e)
        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);
@@ -347,6 +403,11 @@ settime(e)
 **             Creates the transcript file.
 */
 
 **             Creates the transcript file.
 */
 
+#ifndef O_APPEND
+#define O_APPEND       0
+#endif
+
+void
 openxscript(e)
        register ENVELOPE *e;
 {
 openxscript(e)
        register ENVELOPE *e;
 {
@@ -356,11 +417,15 @@ openxscript(e)
        if (e->e_xfp != NULL)
                return;
        p = queuename(e, 'x');
        if (e->e_xfp != NULL)
                return;
        p = queuename(e, 'x');
-       fd = open(p, O_WRONLY|O_CREAT, 0644);
+       fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
        if (fd < 0)
        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");
 }
 \f/*
 **  CLOSEXSCRIPT -- close the transcript file.
 }
 \f/*
 **  CLOSEXSCRIPT -- close the transcript file.
@@ -375,12 +440,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/*
@@ -408,6 +474,10 @@ closexscript(e)
 **             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.
 **             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.
@@ -416,19 +486,20 @@ closexscript(e)
 **             sets sendmail's notion of who the from person is.
 */
 
 **             sets sendmail's notion of who the from person is.
 */
 
-setsender(from, e)
+void
+setsender(from, e, delimptr, internal)
        char *from;
        register ENVELOPE *e;
        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 delimchar;
        char buf[MAXNAME];
        char pvpbuf[PSBUFSIZE];
        extern struct passwd *getpwnam();
        char buf[MAXNAME];
        char pvpbuf[PSBUFSIZE];
        extern struct passwd *getpwnam();
-       extern char *macvalue();
-       extern char **prescan();
-       extern bool safefile();
        extern char *FullName;
 
        if (tTd(45, 1))
        extern char *FullName;
 
        if (tTd(45, 1))
@@ -439,58 +510,47 @@ setsender(from, e)
        **      Username can return errno != 0 on non-errors.
        */
 
        **      Username can return errno != 0 on non-errors.
        */
 
-       if (QueueRun || OpMode == MD_SMTP)
+       if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP)
                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 (ConfigLevel < 2)
+               SuprErrs = TRUE;
 
 
-       if (from != NULL)
-       {
-               extern bool trusteduser();
-
-               if (!trusteduser(realname) && getuid() != geteuid() &&
-                   strchr(from, '!') == NULL && getuid() != 0)
-               {
-                       /* network sends -r regardless (why why why?) */
-                       /* syserr("%s, you cannot use the -f flag", realname); */
-                       from = NULL;
-               }
-       }
-
-/*
-       SuprErrs = TRUE;
-*/
-       if (from == NULL || parseaddr(from, &e->e_from, 1, '\0', e) == NULL)
+       delimchar = internal ? '\0' : ' ';
+       if (from == NULL ||
+           parseaddr(from, &e->e_from, 1, delimchar, delimptr, e) == NULL)
        {
                /* log garbage addresses for traceback */
 # ifdef LOG
                if (from != NULL && LogLevel > 2)
                {
        {
                /* log garbage addresses for traceback */
 # ifdef LOG
                if (from != NULL && LogLevel > 2)
                {
-                       char *host = RealHostName;
+                       char *p;
+                       char ebuf[MAXNAME * 2 + 2];
 
 
-                       if (host == NULL)
-                               host = MyHostName;
+                       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,
                        syslog(LOG_NOTICE,
-                               "from=%s unparseable, received from %s@%s",
-                               from, realname, host);
+                               "from=%s unparseable, received from %s",
+                               from, p);
                }
 # endif /* LOG */
                if (from != NULL)
                        SuprErrs = TRUE;
                if (from == realname ||
                }
 # endif /* LOG */
                if (from != NULL)
                        SuprErrs = TRUE;
                if (from == realname ||
-                   parseaddr(from = newstr(realname), &e->e_from, 1, '\0', e) == NULL)
+                   parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL)
                {
                        SuprErrs = TRUE;
                {
                        SuprErrs = TRUE;
-                       if (parseaddr("postmaster", &e->e_from, 1, '\0', e) == NULL)
-                               syserr("setsender: can't even parse postmaster!");
+                       if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL)
+                               syserr("553 setsender: can't even parse postmaster!");
                }
        }
        else
                }
        }
        else
@@ -501,7 +561,6 @@ setsender(from, e)
                printf("setsender: QDONTSEND ");
                printaddr(&e->e_from, FALSE);
        }
                printf("setsender: QDONTSEND ");
                printaddr(&e->e_from, FALSE);
        }
-       loweraddr(&e->e_from);
        SuprErrs = FALSE;
 
        pvp = NULL;
        SuprErrs = FALSE;
 
        pvp = NULL;
@@ -512,24 +571,27 @@ setsender(from, e)
                extern char *udbsender();
 # endif
 
                extern char *udbsender();
 # endif
 
-               /* 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;
+               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
 
 # ifdef USERDB
-               p = udbsender(from);
+                       p = udbsender(from);
 
 
-               if (p != NULL)
-               {
-                       /*
-                       **  We have an alternate address for the sender
-                       */
+                       if (p != NULL)
+                       {
+                               /*
+                               **  We have an alternate address for the sender
+                               */
 
 
-                       pvp = prescan(p, '\0', pvpbuf);
-               }
+                               pvp = prescan(p, '\0', pvpbuf, NULL);
+                       }
 # endif /* USERDB */
 # endif /* USERDB */
+               }
 
                if ((pw = getpwnam(e->e_from.q_user)) != NULL)
                {
 
                if ((pw = getpwnam(e->e_from.q_user)) != NULL)
                {
@@ -548,17 +610,18 @@ setsender(from, e)
 
                        /* extract full name from passwd file */
                        if (FullName == NULL && pw->pw_gecos != NULL &&
 
                        /* extract full name from passwd file */
                        if (FullName == NULL && pw->pw_gecos != NULL &&
-                           strcmp(pw->pw_name, e->e_from.q_user) == 0)
+                           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);
                        }
                }
                        {
                                buildfname(pw->pw_gecos, e->e_from.q_user, buf);
                                if (buf[0] != '\0')
                                        FullName = newstr(buf);
                        }
                }
-               if (FullName != NULL)
+               if (FullName != NULL && !internal)
                        define('x', FullName, e);
        }
                        define('x', FullName, e);
        }
-       else
+       else if (!internal)
        {
                if (e->e_from.q_home == NULL)
                        e->e_from.q_home = getenv("HOME");
        {
                if (e->e_from.q_home == NULL)
                        e->e_from.q_home = getenv("HOME");
@@ -572,26 +635,25 @@ setsender(from, e)
        */
 
        if (pvp == NULL)
        */
 
        if (pvp == NULL)
-               pvp = prescan(from, '\0', pvpbuf);
+               pvp = prescan(from, '\0', pvpbuf, NULL);
        if (pvp == NULL)
        {
        if (pvp == NULL)
        {
+               /* don't need to give error -- prescan did that already */
 # ifdef LOG
                if (LogLevel > 2)
                        syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
 # endif
 # ifdef LOG
                if (LogLevel > 2)
                        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, '\0');
-       e->e_sender = e->e_returnpath = newstr(buf);
-
+       (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);
        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 (e->e_from.q_mailer != NULL &&
+       if (!internal && 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();
@@ -602,29 +664,3 @@ setsender(from, e)
                        e->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);
-}