change call to expand() to be more rational (and consistent!)
[unix-history] / usr / src / usr.sbin / sendmail / src / main.c
index d516923..b9c60ec 100644 (file)
@@ -13,15 +13,14 @@ static char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)main.c     8.40 (Berkeley) %G%";
+static char sccsid[] = "@(#)main.c     8.91 (Berkeley) %G%";
 #endif /* not lint */
 
 #define        _DEFINE
 
 #include "sendmail.h"
 #endif /* not lint */
 
 #define        _DEFINE
 
 #include "sendmail.h"
-#include <sgtty.h>
-#ifdef NAMED_BIND
-#include <arpa/nameser.h>
+#include <netdb.h>
+#if NAMED_BIND
 #include <resolv.h>
 #endif
 #include <pwd.h>
 #include <resolv.h>
 #endif
 #include <pwd.h>
@@ -74,16 +73,15 @@ char                *UserEnviron[MAXUSERENVIRON + 2];
 char           RealUserName[256];      /* the actual user id on this host */
 char           *CommandLineArgs;       /* command line args for pid file */
 bool           Warn_Q_option = FALSE;  /* warn about Q option use */
 char           RealUserName[256];      /* the actual user id on this host */
 char           *CommandLineArgs;       /* command line args for pid file */
 bool           Warn_Q_option = FALSE;  /* warn about Q option use */
+char           **SaveArgv;     /* argument vector for re-execing */
 
 /*
 **  Pointers for setproctitle.
 **     This allows "ps" listings to give more useful information.
 */
 
 
 /*
 **  Pointers for setproctitle.
 **     This allows "ps" listings to give more useful information.
 */
 
-# ifdef SETPROCTITLE
 char           **Argv = NULL;          /* pointer to argument vector */
 char           *LastArgv = NULL;       /* end of argv */
 char           **Argv = NULL;          /* pointer to argument vector */
 char           *LastArgv = NULL;       /* end of argv */
-# endif /* SETPROCTITLE */
 
 static void    obsolete();
 
 
 static void    obsolete();
 
@@ -93,7 +91,7 @@ ERROR %%%%   Cannot have daemon mode without SMTP   %%%% ERROR
 #endif /* SMTP */
 #endif /* DAEMON */
 
 #endif /* SMTP */
 #endif /* DAEMON */
 
-#define MAXCONFIGLEVEL 5       /* highest config version level known */
+#define MAXCONFIGLEVEL 6       /* highest config version level known */
 
 main(argc, argv, envp)
        int argc;
 
 main(argc, argv, envp)
        int argc;
@@ -103,7 +101,6 @@ main(argc, argv, envp)
        register char *p;
        char **av;
        char *locname;
        register char *p;
        char **av;
        char *locname;
-       extern int finis();
        extern char Version[];
        char *ep, *from;
        typedef int (*fnptr)();
        extern char Version[];
        char *ep, *from;
        typedef int (*fnptr)();
@@ -118,19 +115,21 @@ main(argc, argv, envp)
        char *argv0 = argv[0];
        struct passwd *pw;
        struct stat stb;
        char *argv0 = argv[0];
        struct passwd *pw;
        struct stat stb;
+       struct hostent *hp;
        char jbuf[MAXHOSTNAMELEN];      /* holds MyHostName */
        extern int DtableSize;
        extern int optind;
        extern time_t convtime();
        extern putheader(), putbody();
        extern void intsig();
        char jbuf[MAXHOSTNAMELEN];      /* holds MyHostName */
        extern int DtableSize;
        extern int optind;
        extern time_t convtime();
        extern putheader(), putbody();
        extern void intsig();
-       extern char **myhostname();
+       extern struct hostent *myhostname();
        extern char *arpadate();
        extern char *getauthinfo();
        extern char *getcfname();
        extern char *optarg;
        extern char **environ;
        extern char *arpadate();
        extern char *getauthinfo();
        extern char *getcfname();
        extern char *optarg;
        extern char **environ;
-       extern void dumpstate();
+       extern void sigusr1();
+       extern void sighup();
 
        /*
        **  Check to see if we reentered.
 
        /*
        **  Check to see if we reentered.
@@ -150,9 +149,9 @@ main(argc, argv, envp)
        /* do machine-dependent initializations */
        init_md(argc, argv);
 
        /* do machine-dependent initializations */
        init_md(argc, argv);
 
-       /* arrange to dump state on signal */
 #ifdef SIGUSR1
 #ifdef SIGUSR1
-       setsignal(SIGUSR1, dumpstate);
+       /* arrange to dump state on user-1 signal */
+       setsignal(SIGUSR1, sigusr1);
 #endif
 
        /* in 4.4BSD, the table can be huge; impose a reasonable limit */
 #endif
 
        /* in 4.4BSD, the table can be huge; impose a reasonable limit */
@@ -165,7 +164,7 @@ main(argc, argv, envp)
        **      But also be sure that 0, 1, & 2 are open.
        */
 
        **      But also be sure that 0, 1, & 2 are open.
        */
 
-       i = open("/dev/null", O_RDWR);
+       i = open("/dev/null", O_RDWR, 0);
        if (fstat(STDIN_FILENO, &stb) < 0 && errno != EOPNOTSUPP)
                (void) dup2(i, STDIN_FILENO);
        if (fstat(STDOUT_FILENO, &stb) < 0 && errno != EOPNOTSUPP)
        if (fstat(STDIN_FILENO, &stb) < 0 && errno != EOPNOTSUPP)
                (void) dup2(i, STDIN_FILENO);
        if (fstat(STDOUT_FILENO, &stb) < 0 && errno != EOPNOTSUPP)
@@ -183,12 +182,16 @@ main(argc, argv, envp)
        }
        errno = 0;
 
        }
        errno = 0;
 
-#ifdef LOG_MAIL
+#ifdef LOG
+# ifdef LOG_MAIL
        openlog("sendmail", LOG_PID, LOG_MAIL);
        openlog("sendmail", LOG_PID, LOG_MAIL);
-#else 
+# else 
        openlog("sendmail", LOG_PID);
        openlog("sendmail", LOG_PID);
+# endif
 #endif 
 
 #endif 
 
+       tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
+
        /* set up the blank envelope */
        BlankEnvelope.e_puthdr = putheader;
        BlankEnvelope.e_putbody = putbody;
        /* set up the blank envelope */
        BlankEnvelope.e_puthdr = putheader;
        BlankEnvelope.e_putbody = putbody;
@@ -217,15 +220,18 @@ main(argc, argv, envp)
        i = 0;
        for (av = argv; *av != NULL; )
                i += strlen(*av++) + 1;
        i = 0;
        for (av = argv; *av != NULL; )
                i += strlen(*av++) + 1;
+       SaveArgv = (char **) xalloc(sizeof (char *) * (argc + 1));
        CommandLineArgs = xalloc(i);
        p = CommandLineArgs;
        CommandLineArgs = xalloc(i);
        p = CommandLineArgs;
-       for (av = argv; *av != NULL; )
+       for (av = argv, i = 0; *av != NULL; )
        {
        {
+               SaveArgv[i++] = newstr(*av);
                if (av != argv)
                        *p++ = ' ';
                strcpy(p, *av++);
                p += strlen(p);
        }
                if (av != argv)
                        *p++ = ' ';
                strcpy(p, *av++);
                p += strlen(p);
        }
+       SaveArgv[i] = NULL;
 
        /* Handle any non-getoptable constructions. */
        obsolete(argv);
 
        /* Handle any non-getoptable constructions. */
        obsolete(argv);
@@ -235,23 +241,19 @@ main(argc, argv, envp)
        */
 
 #if defined(__osf__) || defined(_AIX3)
        */
 
 #if defined(__osf__) || defined(_AIX3)
-# define OPTIONS       "B:b:C:cd:e:F:f:h:Iimno:p:q:r:sTtvX:x"
+# define OPTIONS       "B:b:C:cd:e:F:f:h:IimnO:o:p:q:r:sTtvX:x"
 #endif
 #if defined(ultrix)
 #endif
 #if defined(ultrix)
-# define OPTIONS       "B:b:C:cd:e:F:f:h:IiM:mno:p:q:r:sTtvX:"
-#endif
-#if defined(NeXT)
-# define OPTIONS       "B:b:C:cd:e:F:f:h:IimnOo:p:q:r:sTtvX:"
+# define OPTIONS       "B:b:C:cd:e:F:f:h:IiM:mnO:o:p:q:r:sTtvX:"
 #endif
 #ifndef OPTIONS
 #endif
 #ifndef OPTIONS
-# define OPTIONS       "B:b:C:cd:e:F:f:h:Iimno:p:q:r:sTtvX:"
+# define OPTIONS       "B:b:C:cd:e:F:f:h:IimnO:o:p:q:r:sTtvX:"
 #endif
        while ((j = getopt(argc, argv, OPTIONS)) != EOF)
        {
                switch (j)
                {
                  case 'd':
 #endif
        while ((j = getopt(argc, argv, OPTIONS)) != EOF)
        {
                switch (j)
                {
                  case 'd':
-                       tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
                        tTflag(optarg);
                        setbuf(stdout, (char *) NULL);
                        printf("Version %s\n", Version);
                        tTflag(optarg);
                        setbuf(stdout, (char *) NULL);
                        printf("Version %s\n", Version);
@@ -269,14 +271,13 @@ main(argc, argv, envp)
 
        for (i = j = 0; j < MAXUSERENVIRON && (p = envp[i]) != NULL; i++)
        {
 
        for (i = j = 0; j < MAXUSERENVIRON && (p = envp[i]) != NULL; i++)
        {
-               if (strncmp(p, "FS=", 3) == 0 || strncmp(p, "LD_", 3) == 0)
+               if (strncmp(p, "IFS=", 4) == 0 || strncmp(p, "LD_", 3) == 0)
                        continue;
                UserEnviron[j++] = newstr(p);
        }
        UserEnviron[j] = NULL;
        environ = UserEnviron;
 
                        continue;
                UserEnviron[j++] = newstr(p);
        }
        UserEnviron[j] = NULL;
        environ = UserEnviron;
 
-# ifdef SETPROCTITLE
        /*
        **  Save start and extent of argv for setproctitle.
        */
        /*
        **  Save start and extent of argv for setproctitle.
        */
@@ -286,21 +287,24 @@ main(argc, argv, envp)
                LastArgv = envp[i - 1] + strlen(envp[i - 1]);
        else
                LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
                LastArgv = envp[i - 1] + strlen(envp[i - 1]);
        else
                LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
-# endif /* SETPROCTITLE */
 
        if (setsignal(SIGINT, SIG_IGN) != SIG_IGN)
                (void) setsignal(SIGINT, intsig);
 
        if (setsignal(SIGINT, SIG_IGN) != SIG_IGN)
                (void) setsignal(SIGINT, intsig);
-       if (setsignal(SIGHUP, SIG_IGN) != SIG_IGN)
-               (void) setsignal(SIGHUP, intsig);
        (void) setsignal(SIGTERM, intsig);
        (void) setsignal(SIGPIPE, SIG_IGN);
        OldUmask = umask(022);
        OpMode = MD_DELIVER;
        FullName = getenv("NAME");
 
        (void) setsignal(SIGTERM, intsig);
        (void) setsignal(SIGPIPE, SIG_IGN);
        OldUmask = umask(022);
        OpMode = MD_DELIVER;
        FullName = getenv("NAME");
 
-#ifdef NAMED_BIND
+#if NAMED_BIND
        if (tTd(8, 8))
        if (tTd(8, 8))
+       {
+               res_init();
+       {
+               res_init();
                _res.options |= RES_DEBUG;
                _res.options |= RES_DEBUG;
+       }
+       }
 #endif
 
        errno = 0;
 #endif
 
        errno = 0;
@@ -308,12 +312,13 @@ main(argc, argv, envp)
 
        /* initialize some macros, etc. */
        initmacros(CurEnv);
 
        /* initialize some macros, etc. */
        initmacros(CurEnv);
+       init_vendor_macros(CurEnv);
 
        /* version */
        define('v', Version, CurEnv);
 
        /* hostname */
 
        /* version */
        define('v', Version, CurEnv);
 
        /* hostname */
-       av = myhostname(jbuf, sizeof jbuf);
+       hp = myhostname(jbuf, sizeof jbuf);
        if (jbuf[0] != '\0')
        {
                struct  utsname utsname;
        if (jbuf[0] != '\0')
        {
                struct  utsname utsname;
@@ -351,17 +356,35 @@ main(argc, argv, envp)
                        p = jbuf;
                }
                if (tTd(0, 4))
                        p = jbuf;
                }
                if (tTd(0, 4))
-                       printf("UUCP nodename: %s\n", p);
+                       printf(" UUCP nodename: %s\n", p);
                p = newstr(p);
                define('k', p, CurEnv);
                setclass('k', p);
                setclass('w', p);
        }
                p = newstr(p);
                define('k', p, CurEnv);
                setclass('k', p);
                setclass('w', p);
        }
-       while (av != NULL && *av != NULL)
+       if (hp != NULL)
        {
        {
-               if (tTd(0, 4))
-                       printf("\ta.k.a.: %s\n", *av);
-               setclass('w', *av++);
+               for (av = hp->h_aliases; av != NULL && *av != NULL; av++)
+               {
+                       if (tTd(0, 4))
+                               printf("\ta.k.a.: %s\n", *av);
+                       setclass('w', *av);
+               }
+               if (hp->h_addrtype == AF_INET && hp->h_length == INADDRSZ)
+               {
+                       register int i;
+
+                       for (i = 0; hp->h_addr_list[i] != NULL; i++)
+                       {
+                               char ipbuf[100];
+
+                               sprintf(ipbuf, "[%s]",
+                                       inet_ntoa(*((struct in_addr *) hp->h_addr_list[i])));
+                               if (tTd(0, 4))
+                                       printf("\ta.k.a.: %s\n", ipbuf);
+                               setclass('w', ipbuf);
+                       }
+               }
        }
 
        /* current time */
        }
 
        /* current time */
@@ -420,6 +443,7 @@ main(argc, argv, envp)
                          case MD_TEST:
                          case MD_INITALIAS:
                          case MD_PRINT:
                          case MD_TEST:
                          case MD_INITALIAS:
                          case MD_PRINT:
+                         case MD_ARPAFTP:
                                OpMode = j;
                                break;
 
                                OpMode = j;
                                break;
 
@@ -436,7 +460,11 @@ main(argc, argv, envp)
                        break;
 
                  case 'B':     /* body type */
                        break;
 
                  case 'B':     /* body type */
-                       CurEnv->e_bodytype = newstr(optarg);
+                       if (strcasecmp(optarg, "7bit") == 0 ||
+                           strcasecmp(optarg, "8bitmime") == 0)
+                               CurEnv->e_bodytype = newstr(optarg);
+                       else
+                               usrerr("Illegal body type %s", optarg);
                        break;
 
                  case 'C':     /* select configuration file (already done) */
                        break;
 
                  case 'C':     /* select configuration file (already done) */
@@ -459,7 +487,7 @@ main(argc, argv, envp)
                                ExitStat = EX_USAGE;
                                break;
                        }
                                ExitStat = EX_USAGE;
                                break;
                        }
-                       from = newstr(optarg);
+                       from = newstr(denlstring(optarg, TRUE, TRUE));
                        if (strcmp(RealUserName, from) != 0)
                                warn_f_flag = j;
                        break;
                        if (strcmp(RealUserName, from) != 0)
                                warn_f_flag = j;
                        break;
@@ -486,14 +514,28 @@ main(argc, argv, envp)
                        setoption(*optarg, optarg + 1, FALSE, TRUE, CurEnv);
                        break;
 
                        setoption(*optarg, optarg + 1, FALSE, TRUE, CurEnv);
                        break;
 
+                 case 'O':     /* set option (long form) */
+                       setoption(' ', optarg, FALSE, TRUE, CurEnv);
+                       break;
+
                  case 'p':     /* set protocol */
                        p = strchr(optarg, ':');
                        if (p != NULL)
                  case 'p':     /* set protocol */
                        p = strchr(optarg, ':');
                        if (p != NULL)
+                       {
                                *p++ = '\0';
                                *p++ = '\0';
+                               if (*p != '\0')
+                               {
+                                       ep = xalloc(strlen(p) + 1);
+                                       cleanstrcpy(ep, p, MAXNAME);
+                                       define('s', ep, CurEnv);
+                               }
+                       }
                        if (*optarg != '\0')
                        if (*optarg != '\0')
-                               define('r', newstr(optarg), CurEnv);
-                       if (p != NULL && *p != '\0')
-                               define('s', newstr(p), CurEnv);
+                       {
+                               ep = xalloc(strlen(optarg) + 1);
+                               cleanstrcpy(ep, optarg, MAXNAME);
+                               define('r', ep, CurEnv);
+                       }
                        break;
 
                  case 'q':     /* run queue files at intervals */
                        break;
 
                  case 'q':     /* run queue files at intervals */
@@ -530,6 +572,7 @@ main(argc, argv, envp)
                        break;
 
                  case 'X':     /* traffic log file */
                        break;
 
                  case 'X':     /* traffic log file */
+                       setgid(RealGid);
                        setuid(RealUid);
                        TrafficLogFile = fopen(optarg, "a");
                        if (TrafficLogFile == NULL)
                        setuid(RealUid);
                        TrafficLogFile = fopen(optarg, "a");
                        if (TrafficLogFile == NULL)
@@ -554,9 +597,7 @@ main(argc, argv, envp)
                        break;
 
                  case 'e':     /* error message disposition */
                        break;
 
                  case 'e':     /* error message disposition */
-# if defined(ultrix)
                  case 'M':     /* define macro */
                  case 'M':     /* define macro */
-# endif
                        setoption(j, optarg, FALSE, TRUE, CurEnv);
                        break;
 
                        setoption(j, optarg, FALSE, TRUE, CurEnv);
                        break;
 
@@ -574,10 +615,6 @@ main(argc, argv, envp)
                  case 'x':     /* random flag that OSF/1 & AIX mailx passes */
                        break;
 # endif
                  case 'x':     /* random flag that OSF/1 & AIX mailx passes */
                        break;
 # endif
-# if defined(NeXT)
-                 case 'O':     /* random flag that NeXT Mail.app passes */
-                       break;
-# endif
 
                  default:
                        ExitStat = EX_USAGE;
 
                  default:
                        ExitStat = EX_USAGE;
@@ -612,6 +649,24 @@ main(argc, argv, envp)
                printf("\n");
        }
 
                printf("\n");
        }
 
+       /*
+       **  Initialize name server if it is going to be used.
+       */
+
+#if NAMED_BIND
+       if (!bitset(RES_INIT, _res.options))
+               res_init();
+#endif
+
+       /*
+       **  Initialize name server if it is going to be used.
+       */
+
+#if NAMED_BIND
+       if (UseNameServer && !bitset(RES_INIT, _res.options))
+               res_init();
+#endif
+
        /*
        **  Process authorization warnings from command line.
        */
        /*
        **  Process authorization warnings from command line.
        */
@@ -619,14 +674,18 @@ main(argc, argv, envp)
        if (warn_C_flag)
                auth_warning(CurEnv, "Processed by %s with -C %s",
                        RealUserName, ConfFile);
        if (warn_C_flag)
                auth_warning(CurEnv, "Processed by %s with -C %s",
                        RealUserName, ConfFile);
-/*
-       if (warn_f_flag != '\0')
+       if (warn_f_flag != '\0' &&
+           ((st = stab(RealUserName, ST_CLASS, ST_FIND)) == NULL ||
+            !bitnset('t', st->s_class)))
                auth_warning(CurEnv, "%s set sender to %s using -%c",
                        RealUserName, from, warn_f_flag);
                auth_warning(CurEnv, "%s set sender to %s using -%c",
                        RealUserName, from, warn_f_flag);
-*/
        if (Warn_Q_option)
                auth_warning(CurEnv, "Processed from queue %s", QueueDir);
 
        if (Warn_Q_option)
                auth_warning(CurEnv, "Processed from queue %s", QueueDir);
 
+       /* supress error printing if errors mailed back or whatever */
+       if (CurEnv->e_errormode != EM_PRINT)
+               HoldErrs = TRUE;
+
        /* Enforce use of local time (null string overrides this) */
        if (TimeZoneSpec == NULL)
                unsetenv("TZ");
        /* Enforce use of local time (null string overrides this) */
        if (TimeZoneSpec == NULL)
                unsetenv("TZ");
@@ -658,42 +717,30 @@ main(argc, argv, envp)
        if (MeToo)
                BlankEnvelope.e_flags |= EF_METOO;
 
        if (MeToo)
                BlankEnvelope.e_flags |= EF_METOO;
 
-# ifdef QUEUE
-       if (queuemode && RealUid != 0 && bitset(PRIV_RESTRICTQRUN, PrivacyFlags))
-       {
-               struct stat stbuf;
-
-               /* check to see if we own the queue directory */
-               if (stat(QueueDir, &stbuf) < 0)
-                       syserr("main: cannot stat %s", QueueDir);
-               if (stbuf.st_uid != RealUid)
-               {
-                       /* nope, really a botch */
-                       usrerr("You do not have permission to process the queue");
-                       exit (EX_NOPERM);
-               }
-       }
-# endif /* QUEUE */
-
        switch (OpMode)
        {
        switch (OpMode)
        {
-         case MD_INITALIAS:
-               Verbose = TRUE;
-               break;
-
          case MD_DAEMON:
                /* remove things that don't make sense in daemon mode */
                FullName = NULL;
          case MD_DAEMON:
                /* remove things that don't make sense in daemon mode */
                FullName = NULL;
+
+               /* arrange to restart on hangup signal */
+               setsignal(SIGHUP, sighup);
                break;
 
                break;
 
-         case MD_SMTP:
-               if (RealUid != 0)
-                       auth_warning(CurEnv,
-                               "%s owned process doing -bs",
-                               RealUserName);
+         case MD_INITALIAS:
+               Verbose = TRUE;
+               /* fall through... */
+
+         default:
+               /* arrange to exit cleanly on hangup signal */
+               setsignal(SIGHUP, intsig);
                break;
        }
 
                break;
        }
 
+       /* full names can't have newlines */
+       if (FullName != NULL && strchr(FullName, '\n') != NULL)
+               FullName = newstr(denlstring(FullName, TRUE, TRUE));
+
        /* do heuristic mode adjustment */
        if (Verbose)
        {
        /* do heuristic mode adjustment */
        if (Verbose)
        {
@@ -710,8 +757,11 @@ main(argc, argv, envp)
        }
 
        /* our name for SMTP codes */
        }
 
        /* our name for SMTP codes */
-       expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv);
+       expand("\201j", jbuf, sizeof jbuf, CurEnv);
        MyHostName = jbuf;
        MyHostName = jbuf;
+       if (strchr(jbuf, '.') == NULL)
+               message("WARNING: local host name (%s) is not qualified; fix $j in config file",
+                       jbuf);
 
        /* make certain that this name is part of the $=w class */
        setclass('w', MyHostName);
 
        /* make certain that this name is part of the $=w class */
        setclass('w', MyHostName);
@@ -741,6 +791,30 @@ main(argc, argv, envp)
        else
                InclMailer = st->s_mailer;
 
        else
                InclMailer = st->s_mailer;
 
+       /* heuristic tweaking of local mailer for back compat */
+       if (ConfigLevel < 6)
+       {
+               if (LocalMailer != NULL)
+               {
+                       setbitn(M_ALIASABLE, LocalMailer->m_flags);
+                       setbitn(M_HASPWENT, LocalMailer->m_flags);
+                       setbitn(M_TRYRULESET5, LocalMailer->m_flags);
+                       setbitn(M_CHECKINCLUDE, LocalMailer->m_flags);
+                       setbitn(M_CHECKPROG, LocalMailer->m_flags);
+                       setbitn(M_CHECKFILE, LocalMailer->m_flags);
+                       setbitn(M_CHECKUDB, LocalMailer->m_flags);
+               }
+               if (ProgMailer != NULL)
+                       setbitn(M_RUNASRCPT, ProgMailer->m_flags);
+               if (FileMailer != NULL)
+                       setbitn(M_RUNASRCPT, FileMailer->m_flags);
+       }
+
+       /* initialize standard MIME classes */
+       setclass('n', "message/rfc822");
+       setclass('n', "message/partial");
+       setclass('n', "message/external-body");
+       setclass('n', "multipart/signed");
 
        /* operate in queue directory */
        if (OpMode != MD_TEST && chdir(QueueDir) < 0)
 
        /* operate in queue directory */
        if (OpMode != MD_TEST && chdir(QueueDir) < 0)
@@ -749,6 +823,23 @@ main(argc, argv, envp)
                ExitStat = EX_SOFTWARE;
        }
 
                ExitStat = EX_SOFTWARE;
        }
 
+# ifdef QUEUE
+       if (queuemode && RealUid != 0 && bitset(PRIV_RESTRICTQRUN, PrivacyFlags))
+       {
+               struct stat stbuf;
+
+               /* check to see if we own the queue directory */
+               if (stat(".", &stbuf) < 0)
+                       syserr("main: cannot stat %s", QueueDir);
+               if (stbuf.st_uid != RealUid)
+               {
+                       /* nope, really a botch */
+                       usrerr("You do not have permission to process the queue");
+                       exit (EX_NOPERM);
+               }
+       }
+# endif /* QUEUE */
+
        /* if we've had errors so far, exit now */
        if (ExitStat != EX_OK && OpMode != MD_TEST)
        {
        /* if we've had errors so far, exit now */
        if (ExitStat != EX_OK && OpMode != MD_TEST)
        {
@@ -805,27 +896,7 @@ main(argc, argv, envp)
 
                        if (m == NULL)
                                continue;
 
                        if (m == NULL)
                                continue;
-                       printf("mailer %d (%s): P=%s S=%d/%d R=%d/%d M=%ld F=", i, m->m_name,
-                               m->m_mailer, m->m_se_rwset, m->m_sh_rwset,
-                               m->m_re_rwset, m->m_rh_rwset, m->m_maxsize);
-                       for (j = '\0'; j <= '\177'; j++)
-                               if (bitnset(j, m->m_flags))
-                                       (void) putchar(j);
-                       printf(" E=");
-                       xputs(m->m_eol);
-                       if (m->m_argv != NULL)
-                       {
-                               char **a = m->m_argv;
-
-                               printf(" A=");
-                               while (*a != NULL)
-                               {
-                                       if (a != m->m_argv)
-                                               printf(" ");
-                                       xputs(*a++);
-                               }
-                       }
-                       printf("\n");
+                       printmailer(m);
                }
        }
 
                }
        }
 
@@ -872,13 +943,91 @@ main(argc, argv, envp)
                          case '#':
                                continue;
 
                          case '#':
                                continue;
 
-#ifdef MAYBENEXTRELEASE
-                         case 'C':             /* try crackaddr */
+                         case '?':             /* try crackaddr */
                                q = crackaddr(&buf[1]);
                                xputs(q);
                                printf("\n");
                                continue;
                                q = crackaddr(&buf[1]);
                                xputs(q);
                                printf("\n");
                                continue;
-#endif
+
+                         case '.':             /* config-style settings */
+                               switch (buf[1])
+                               {
+                                 case 'D':
+                                       define(buf[2], newstr(&buf[3]), CurEnv);
+                                       break;
+
+                                 case 'C':
+                                       setclass(buf[2], &buf[3]);
+                                       break;
+
+                                 case 'S':             /* dump rule set */
+                                       {
+                                               int rs;
+                                               struct rewrite *rw;
+                                               char *cp;
+                                               STAB *s;
+
+                                               if ((cp = strchr(buf, '\n')) != NULL)
+                                                       *cp = '\0';
+                                               if (cp == buf+2)
+                                                       continue;
+                                               s = stab(buf+2, ST_RULESET, ST_FIND);
+                                               if (s == NULL)
+                                               {
+                                                       if (!isdigit(buf[2]))
+                                                               continue;
+                                                       rs = atoi(buf+2);
+                                               }
+                                               else
+                                                       rs = s->s_ruleset;
+                                               if (rs < 0 || rs > MAXRWSETS)
+                                                       continue;
+                                               if ((rw = RewriteRules[rs]) == NULL)
+                                                       continue;
+                                               do
+                                               {
+                                                       char **s;
+                                                       putchar('R');
+                                                       s = rw->r_lhs;
+                                                       while (*s != NULL)
+                                                       {
+                                                               xputs(*s++);
+                                                               putchar(' ');
+                                                       }
+                                                       putchar('\t');
+                                                       putchar('\t');
+                                                       s = rw->r_rhs;
+                                                       while (*s != NULL)
+                                                       {
+                                                               xputs(*s++);
+                                                               putchar(' ');
+                                                       }
+                                                       putchar('\n');
+                                               } while (rw = rw->r_next);
+                                       }
+                                       break;
+
+                                 default:
+                                       printf("Unknown config command %s", buf);
+                                       break;
+                               }
+                               continue;
+
+                         case '-':             /* set command-line-like opts */
+                               switch (buf[1])
+                               {
+                                 case 'd':
+                                       if (buf[2] == '\n')
+                                               tTflag("");
+                                       else
+                                               tTflag(&buf[2]);
+                                       break;
+
+                                 default:
+                                       printf("Unknown \"-\" command %s", buf);
+                                       break;
+                               }
+                               continue;
                        }
 
                        for (p = buf; isascii(*p) && isspace(*p); p++)
                        }
 
                        for (p = buf; isascii(*p) && isspace(*p); p++)
@@ -898,7 +1047,8 @@ main(argc, argv, envp)
                        {
                                char pvpbuf[PSBUFSIZE];
 
                        {
                                char pvpbuf[PSBUFSIZE];
 
-                               pvp = prescan(++p, ',', pvpbuf, &delimptr);
+                               pvp = prescan(++p, ',', pvpbuf, sizeof pvpbuf,
+                                             &delimptr);
                                if (pvp == NULL)
                                        continue;
                                p = q;
                                if (pvp == NULL)
                                        continue;
                                p = q;
@@ -906,7 +1056,7 @@ main(argc, argv, envp)
                                {
                                        int stat;
 
                                {
                                        int stat;
 
-                                       stat = rewrite(pvp, atoi(p), CurEnv);
+                                       stat = rewrite(pvp, atoi(p), 0, CurEnv);
                                        if (stat != EX_OK)
                                                printf("== Ruleset %s status %d\n",
                                                        p, stat);
                                        if (stat != EX_OK)
                                                printf("== Ruleset %s status %d\n",
                                                        p, stat);
@@ -966,7 +1116,9 @@ main(argc, argv, envp)
                if (tTd(0, 1))
                        strcat(dtype, "+debugging");
 
                if (tTd(0, 1))
                        strcat(dtype, "+debugging");
 
+#ifdef LOG
                syslog(LOG_INFO, "starting daemon (%s): %s", Version, dtype + 1);
                syslog(LOG_INFO, "starting daemon (%s): %s", Version, dtype + 1);
+#endif
 #ifdef XLA
                xla_create_file();
 #endif
 #ifdef XLA
                xla_create_file();
 #endif
@@ -1004,7 +1156,7 @@ main(argc, argv, envp)
        **  commands.  This will never return.
        */
 
        **  commands.  This will never return.
        */
 
-       if (OpMode == MD_SMTP)
+       if (OpMode == MD_SMTP || OpMode == MD_DAEMON)
                smtp(CurEnv);
 # endif /* SMTP */
 
                smtp(CurEnv);
 # endif /* SMTP */
 
@@ -1016,7 +1168,7 @@ main(argc, argv, envp)
        else
        {
                /* interactive -- all errors are global */
        else
        {
                /* interactive -- all errors are global */
-               CurEnv->e_flags |= EF_GLOBALERRS;
+               CurEnv->e_flags |= EF_GLOBALERRS|EF_LOGSENDER;
        }
 
        /*
        }
 
        /*
@@ -1035,7 +1187,7 @@ main(argc, argv, envp)
 
                /* collect body for UUCP return */
                if (OpMode != MD_VERIFY)
 
                /* collect body for UUCP return */
                if (OpMode != MD_VERIFY)
-                       collect(FALSE, FALSE, CurEnv);
+                       collect(InChannel, FALSE, FALSE, NULL, CurEnv);
                finis();
        }
 
                finis();
        }
 
@@ -1057,7 +1209,7 @@ main(argc, argv, envp)
        if (OpMode != MD_VERIFY || GrabTo)
        {
                CurEnv->e_flags |= EF_GLOBALERRS;
        if (OpMode != MD_VERIFY || GrabTo)
        {
                CurEnv->e_flags |= EF_GLOBALERRS;
-               collect(FALSE, FALSE, CurEnv);
+               collect(InChannel, FALSE, FALSE, NULL, CurEnv);
        }
        errno = 0;
 
        }
        errno = 0;
 
@@ -1098,10 +1250,11 @@ main(argc, argv, envp)
 **             exits sendmail
 */
 
 **             exits sendmail
 */
 
+void
 finis()
 {
        if (tTd(2, 1))
 finis()
 {
        if (tTd(2, 1))
-               printf("\n====finis: stat %d e_flags %o, e_id=%s\n",
+               printf("\n====finis: stat %d e_flags %x, e_id=%s\n",
                        ExitStat, CurEnv->e_flags,
                        CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id);
        if (tTd(2, 9))
                        ExitStat, CurEnv->e_flags,
                        CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id);
        if (tTd(2, 9))
@@ -1266,7 +1419,6 @@ disconnect(droplev, e)
        }
 
        /* be sure we don't get nasty signals */
        }
 
        /* be sure we don't get nasty signals */
-       (void) setsignal(SIGHUP, SIG_IGN);
        (void) setsignal(SIGINT, SIG_IGN);
        (void) setsignal(SIGQUIT, SIG_IGN);
 
        (void) setsignal(SIGINT, SIG_IGN);
        (void) setsignal(SIGQUIT, SIG_IGN);
 
@@ -1274,6 +1426,7 @@ disconnect(droplev, e)
        HoldErrs = TRUE;
        CurEnv->e_errormode = EM_MAIL;
        Verbose = FALSE;
        HoldErrs = TRUE;
        CurEnv->e_errormode = EM_MAIL;
        Verbose = FALSE;
+       DisConnected = TRUE;
 
        /* all input from /dev/null */
        if (InChannel != stdin)
 
        /* all input from /dev/null */
        if (InChannel != stdin)
@@ -1325,7 +1478,8 @@ static void
 obsolete(argv)
        char *argv[];
 {
 obsolete(argv)
        char *argv[];
 {
-       char *ap;
+       register char *ap;
+       register char *op;
 
        while ((ap = *++argv) != NULL)
        {
 
        while ((ap = *++argv) != NULL)
        {
@@ -1333,10 +1487,18 @@ obsolete(argv)
                if (ap[0] != '-' || ap[1] == '-')
                        return;
 
                if (ap[0] != '-' || ap[1] == '-')
                        return;
 
+               /* skip over options that do have a value */
+               op = strchr(OPTIONS, ap[1]);
+               if (op != NULL && *++op == ':' && ap[2] == '\0' &&
+                   ap[1] != 'd' && argv[1] != NULL && argv[1][0] != '-')
+               {
+                       argv++;
+                       continue;
+               }
+
                /* If -C doesn't have an argument, use sendmail.cf. */
 #define        __DEFPATH       "sendmail.cf"
                /* If -C doesn't have an argument, use sendmail.cf. */
 #define        __DEFPATH       "sendmail.cf"
-               if (ap[1] == 'C' && ap[2] == '\0' &&
-                   (argv[1] == NULL || argv[1][0] == '-'))
+               if (ap[1] == 'C' && ap[2] == '\0')
                {
                        *argv = xalloc(sizeof(__DEFPATH) + 2);
                        argv[0][0] = '-';
                {
                        *argv = xalloc(sizeof(__DEFPATH) + 2);
                        argv[0][0] = '-';
@@ -1345,13 +1507,11 @@ obsolete(argv)
                }
 
                /* If -q doesn't have an argument, run it once. */
                }
 
                /* If -q doesn't have an argument, run it once. */
-               if (ap[1] == 'q' && ap[2] == '\0' &&
-                   (argv[1] == NULL || argv[1][0] == '-'))
+               if (ap[1] == 'q' && ap[2] == '\0')
                        *argv = "-q0";
 
                /* if -d doesn't have an argument, use 0-99.1 */
                        *argv = "-q0";
 
                /* if -d doesn't have an argument, use 0-99.1 */
-               if (ap[1] == 'd' && ap[2] == '\0' &&
-                   (argv[1] == NULL || !isdigit(argv[1][0])))
+               if (ap[1] == 'd' && ap[2] == '\0')
                        *argv = "-d0-99.1";
        }
 }
                        *argv = "-d0-99.1";
        }
 }
@@ -1384,7 +1544,7 @@ auth_warning(e, msg, va_alist)
        {
                register char *p;
                static char hostbuf[48];
        {
                register char *p;
                static char hostbuf[48];
-               extern char **myhostname();
+               extern struct hostent *myhostname();
 
                if (hostbuf[0] == '\0')
                        (void) myhostname(hostbuf, sizeof hostbuf);
 
                if (hostbuf[0] == '\0')
                        (void) myhostname(hostbuf, sizeof hostbuf);
@@ -1394,23 +1554,70 @@ auth_warning(e, msg, va_alist)
                VA_START(msg);
                vsprintf(p, msg, ap);
                VA_END;
                VA_START(msg);
                vsprintf(p, msg, ap);
                VA_END;
-               addheader("X-Authentication-Warning", buf, e);
+               addheader("X-Authentication-Warning", buf, &e->e_header);
        }
 }
 \f/*
        }
 }
 \f/*
-**  DUMPSTATE -- dump state on user signal
+**  DUMPSTATE -- dump state
 **
 **     For debugging.
 */
 
 void
 **
 **     For debugging.
 */
 
 void
-dumpstate()
+dumpstate(when)
+       char *when;
 {
 #ifdef LOG
 {
 #ifdef LOG
-       syslog(LOG_DEBUG, "--- dumping state on user signal: open file descriptors: ---");
+       register char *j = macvalue('j', CurEnv);
+
+       syslog(LOG_DEBUG, "--- dumping state on %s: $j = %s ---",
+               when,
+               j == NULL ? "<NULL>" : j);
+       if (j != NULL)
+       {
+               if (!wordinclass(j, 'w'))
+                       syslog(LOG_DEBUG, "*** $j not in $=w ***");
+       }
+       syslog(LOG_DEBUG, "--- open file descriptors: ---");
        printopenfds(TRUE);
        syslog(LOG_DEBUG, "--- connection cache: ---");
        mci_dump_all(TRUE);
        printopenfds(TRUE);
        syslog(LOG_DEBUG, "--- connection cache: ---");
        mci_dump_all(TRUE);
+       if (RewriteRules[89] != NULL)
+       {
+               int stat;
+               register char **pvp;
+               char *pv[MAXATOM + 1];
+
+               pv[0] = NULL;
+               stat = rewrite(pv, 89, 0, CurEnv);
+               syslog(LOG_DEBUG, "--- ruleset 89 returns stat %d, pv: ---",
+                       stat);
+               for (pvp = pv; *pvp != NULL; pvp++)
+                       syslog(LOG_DEBUG, "%s", *pvp);
+       }
        syslog(LOG_DEBUG, "--- end of state dump ---");
 #endif
 }
        syslog(LOG_DEBUG, "--- end of state dump ---");
 #endif
 }
+
+
+void
+sigusr1()
+{
+       dumpstate("user signal");
+}
+
+
+void
+sighup()
+{
+#ifdef LOG
+       if (LogLevel > 3)
+               syslog(LOG_INFO, "restarting %s on signal", SaveArgv[0]);
+#endif
+       execv(SaveArgv[0], SaveArgv);
+#ifdef LOG
+       if (LogLevel > 0)
+               syslog(LOG_ALERT, "could not exec %s: %m", SaveArgv[0]);
+#endif
+       exit(EX_OSFILE);
+}