Set the $w macro to the hostname if at all possible to allow generic
[unix-history] / usr / src / usr.sbin / sendmail / src / main.c
index 29a93fd..cd9c12f 100644 (file)
@@ -2,8 +2,9 @@
 # include <signal.h>
 # include <sys/ioctl.h>
 # include "sendmail.h"
 # include <signal.h>
 # include <sys/ioctl.h>
 # include "sendmail.h"
+# include <sys/file.h>
 
 
-SCCSID(@(#)main.c      3.146           %G%);
+SCCSID(@(#)main.c      3.152           %G%);
 
 /*
 **  SENDMAIL -- Post mail to a set of destinations.
 
 /*
 **  SENDMAIL -- Post mail to a set of destinations.
@@ -61,7 +62,6 @@ main(argc, argv)
        char **argv;
 {
        register char *p;
        char **argv;
 {
        register char *p;
-       int ac;
        char **av;
        char *locname;
        extern int finis();
        char **av;
        char *locname;
        extern int finis();
@@ -80,6 +80,7 @@ main(argc, argv)
        extern putheader(), putbody();
        extern ENVELOPE *newenvelope();
        extern intsig();
        extern putheader(), putbody();
        extern ENVELOPE *newenvelope();
        extern intsig();
+       extern char *myhostname();
 
        /*
        **  Check to see if we reentered.
 
        /*
        **  Check to see if we reentered.
@@ -96,6 +97,14 @@ main(argc, argv)
        extern ADDRESS *recipient();
        bool canrename;
 
        extern ADDRESS *recipient();
        bool canrename;
 
+       /*
+       **  Be sure we have enough file descriptors.
+       */
+
+       for (i = 3; i < 20; i++)
+               (void) close(i);
+       errno = 0;
+
        /*
        **  Do a quick prescan of the argument list.
        **      We do this to find out if we can potentially thaw the
        /*
        **  Do a quick prescan of the argument list.
        **      We do this to find out if we can potentially thaw the
@@ -105,14 +114,13 @@ main(argc, argv)
        */
 
        argv[argc] = NULL;
        */
 
        argv[argc] = NULL;
-       ac = argc;
        av = argv;
        av = argv;
-       while (--ac > 0)
+       while (*++av != NULL)
        {
        {
-               if (strncmp(*++av, "-C", 2) == 0 || strncmp(*av, "-bz", 3) == 0)
+               if (strncmp(*av, "-C", 2) == 0 || strncmp(*av, "-bz", 3) == 0)
                        break;
        }
                        break;
        }
-       if (ac <= 0)
+       if (*av == NULL)
                readconfig = !thaw(FreezeFile);
 
        /*
                readconfig = !thaw(FreezeFile);
 
        /*
@@ -126,6 +134,7 @@ main(argc, argv)
        if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
                (void) signal(SIGHUP, intsig);
        (void) signal(SIGTERM, intsig);
        if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
                (void) signal(SIGHUP, intsig);
        (void) signal(SIGTERM, intsig);
+       (void) signal(SIGPIPE, SIG_IGN);
        OldUmask = umask(0);
        OpMode = MD_DELIVER;
        MotherPid = getpid();
        OldUmask = umask(0);
        OpMode = MD_DELIVER;
        MotherPid = getpid();
@@ -148,12 +157,14 @@ main(argc, argv)
        errno = 0;
        from = NULL;
        initmacros();
        errno = 0;
        from = NULL;
        initmacros();
+       p = myhostname();
+       if (p != NULL && *p != '\0')
+               define('w', p, CurEnv);
 
        /*
        ** Crack argv.
        */
 
 
        /*
        ** Crack argv.
        */
 
-       ac = argc;
        av = argv;
        p = rindex(*av, '/');
        if (p++ == NULL)
        av = argv;
        p = rindex(*av, '/');
        if (p++ == NULL)
@@ -162,7 +173,7 @@ main(argc, argv)
                OpMode = MD_INITALIAS;
        else if (strcmp(p, "mailq") == 0)
                OpMode = MD_PRINT;
                OpMode = MD_INITALIAS;
        else if (strcmp(p, "mailq") == 0)
                OpMode = MD_PRINT;
-       while (--ac > 0 && (p = *++av)[0] == '-')
+       while ((p = *++av) != NULL && p[0] == '-')
        {
                switch (p[1])
                {
        {
                switch (p[1])
                {
@@ -214,13 +225,12 @@ main(argc, argv)
                  case 'f':     /* from address */
                  case 'r':     /* obsolete -f flag */
                        p += 2;
                  case 'f':     /* from address */
                  case 'r':     /* obsolete -f flag */
                        p += 2;
-                       if (*p == '\0')
+                       if (*p == '\0' && ((p = *++av) == NULL || *p == '-'))
                        {
                                p = *++av;
                        {
                                p = *++av;
-                               if (--ac <= 0 || *p == '-')
+                               if (p == NULL || *p == '-')
                                {
                                        syserr("No \"from\" person");
                                {
                                        syserr("No \"from\" person");
-                                       ac++;
                                        av--;
                                        break;
                                }
                                        av--;
                                        break;
                                }
@@ -235,32 +245,22 @@ main(argc, argv)
 
                  case 'F':     /* set full name */
                        p += 2;
 
                  case 'F':     /* set full name */
                        p += 2;
-                       if (*p == '\0')
+                       if (*p == '\0' && ((p = *++av) == NULL || *p == '-'))
                        {
                        {
-                               p = *++av;
-                               if (--ac <= 0 || *p == '-')
-                               {
-                                       syserr("Bad -F flag");
-                                       ac++;
-                                       av--;
-                                       break;
-                               }
+                               syserr("Bad -F flag");
+                               av--;
+                               break;
                        }
                        FullName = p;
                        break;
 
                  case 'h':     /* hop count */
                        p += 2;
                        }
                        FullName = p;
                        break;
 
                  case 'h':     /* hop count */
                        p += 2;
-                       if (*p == '\0')
+                       if (*p == '\0' && ((p = *++av) == NULL || !isdigit(*p)))
                        {
                        {
-                               p = *++av;
-                               if (--ac <= 0 || *p < '0' || *p > '9')
-                               {
-                                       syserr("Bad hop count (%s)", p);
-                                       ac++;
-                                       av--;
-                                       break;
-                               }
+                               syserr("Bad hop count (%s)", p);
+                               av--;
+                               break;
                        }
                        CurEnv->e_hopcount = atoi(p);
                        break;
                        }
                        CurEnv->e_hopcount = atoi(p);
                        break;
@@ -391,8 +391,11 @@ main(argc, argv)
 
                        if (m == NULL)
                                continue;
 
                        if (m == NULL)
                                continue;
-                       printf("mailer %d: %s %s %lo %d %d\n", i, m->m_name,
-                              m->m_mailer, m->m_flags, m->m_s_rwset, m->m_r_rwset);
+                       printf("mailer %d (%s): P=%s F=%lo S=%d R=%d E=", i,
+                              m->m_name, m->m_mailer, m->m_flags, m->m_s_rwset,
+                              m->m_r_rwset);
+                       xputs(m->m_eol);
+                       printf("\n");
                }
        }
 # endif DEBUG
                }
        }
 # endif DEBUG
@@ -485,7 +488,7 @@ main(argc, argv)
                        MotherPid = getpid();
 
                        /* disconnect from our controlling tty */
                        MotherPid = getpid();
 
                        /* disconnect from our controlling tty */
-                       disconnect();
+                       disconnect(TRUE);
                }
 
 # ifdef QUEUE
                }
 
 # ifdef QUEUE
@@ -526,7 +529,7 @@ main(argc, argv)
        initsys();
        setsender(from);
 
        initsys();
        setsender(from);
 
-       if (OpMode != MD_ARPAFTP && ac <= 0 && !GrabTo)
+       if (OpMode != MD_ARPAFTP && *av == NULL && !GrabTo)
        {
                usrerr("Usage: /etc/sendmail [flags] addr...");
                finis();
        {
                usrerr("Usage: /etc/sendmail [flags] addr...");
                finis();
@@ -818,7 +821,11 @@ thaw(freezefile)
 **  DISCONNECT -- remove our connection with any foreground process
 **
 **     Parameters:
 **  DISCONNECT -- remove our connection with any foreground process
 **
 **     Parameters:
-**             none.
+**             fulldrop -- if set, we should also drop the controlling
+**                     TTY if possible -- this should only be done when
+**                     setting up the daemon since otherwise UUCP can
+**                     leave us trying to open a dialin, and we will
+**                     wait for the carrier.
 **
 **     Returns:
 **             none
 **
 **     Returns:
 **             none
@@ -828,7 +835,8 @@ thaw(freezefile)
 **             the controlling tty.
 */
 
 **             the controlling tty.
 */
 
-disconnect()
+disconnect(fulldrop)
+       bool fulldrop;
 {
        int fd;
 
 {
        int fd;
 
@@ -877,11 +885,14 @@ disconnect()
 
 #ifdef TIOCNOTTY
        /* drop our controlling TTY completely if possible */
 
 #ifdef TIOCNOTTY
        /* drop our controlling TTY completely if possible */
-       fd = open("/dev/tty", 2);
-       if (fd >= 0)
+       if (fulldrop)
        {
        {
-               (void) ioctl(fd, TIOCNOTTY, 0);
-               (void) close(fd);
+               fd = open("/dev/tty", 2);
+               if (fd >= 0)
+               {
+                       (void) ioctl(fd, TIOCNOTTY, 0);
+                       (void) close(fd);
+               }
        }
 #endif TIOCNOTTY
 
        }
 #endif TIOCNOTTY