BSD 4_3_Reno release
[unix-history] / usr / src / usr.sbin / inetd / inetd.c
index 2d325cf..6973d0c 100644 (file)
@@ -2,17 +2,19 @@
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms are permitted provided
+ * that: (1) source distributions retain this entire copyright notice and
+ * comment, and (2) distributions including binaries display the following
+ * acknowledgement:  ``This product includes software developed by the
+ * University of California, Berkeley and its contributors'' in the
+ * documentation or other materials provided with the distribution and in
+ * all advertising materials mentioning features or use of this software.
+ * Neither the name of the University nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -22,7 +24,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)inetd.c    5.13 (Berkeley) %G%";
+static char sccsid[] = "@(#)inetd.c    5.25 (Berkeley) 6/29/90";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -56,7 +58,7 @@ static char sccsid[] = "@(#)inetd.c   5.13 (Berkeley) %G%";
  *     wait/nowait                     single-threaded/multi-threaded
  *     user                            user to run daemon as
  *     server program                  full path name
  *     wait/nowait                     single-threaded/multi-threaded
  *     user                            user to run daemon as
  *     server program                  full path name
- *     server program arguments        maximum of MAXARGS (5)
+ *     server program arguments        maximum of MAXARGS (20)
  *
  * Comment lines are indicated by a `#' in column 1.
  */
  *
  * Comment lines are indicated by a `#' in column 1.
  */
@@ -73,11 +75,13 @@ static char sccsid[] = "@(#)inetd.c 5.13 (Berkeley) %G%";
 #include <arpa/inet.h>
 
 #include <errno.h>
 #include <arpa/inet.h>
 
 #include <errno.h>
-#include <stdio.h>
 #include <signal.h>
 #include <netdb.h>
 #include <syslog.h>
 #include <pwd.h>
 #include <signal.h>
 #include <netdb.h>
 #include <syslog.h>
 #include <pwd.h>
+#include <stdio.h>
+#include <string.h>
+#include "pathnames.h"
 
 #define        TOOMANY         40              /* don't start more than TOOMANY */
 #define        CNT_INTVL       60              /* servers in CNT_INTVL sec. */
 
 #define        TOOMANY         40              /* don't start more than TOOMANY */
 #define        CNT_INTVL       60              /* servers in CNT_INTVL sec. */
@@ -87,7 +91,7 @@ static char sccsid[] = "@(#)inetd.c   5.13 (Berkeley) %G%";
 
 extern int errno;
 
 
 extern int errno;
 
-int    reapchild(), retry();
+void   config(), reapchild(), retry();
 char   *index();
 char   *malloc();
 
 char   *index();
 char   *malloc();
 
@@ -107,7 +111,7 @@ struct      servtab {
        char    *se_user;               /* user name to run as */
        struct  biltin *se_bi;          /* if built-in, description */
        char    *se_server;             /* server program */
        char    *se_user;               /* user name to run as */
        struct  biltin *se_bi;          /* if built-in, description */
        char    *se_server;             /* server program */
-#define MAXARGV 5
+#define        MAXARGV 20
        char    *se_argv[MAXARGV+1];    /* program arguments */
        int     se_fd;                  /* open descriptor */
        struct  sockaddr_in se_ctrladdr;/* bound address */
        char    *se_argv[MAXARGV+1];    /* program arguments */
        int     se_fd;                  /* open descriptor */
        struct  sockaddr_in se_ctrladdr;/* bound address */
@@ -150,7 +154,7 @@ struct biltin {
 };
 
 #define NUMINT (sizeof(intab) / sizeof(struct inent))
 };
 
 #define NUMINT (sizeof(intab) / sizeof(struct inent))
-char   *CONFIG = "/etc/inetd.conf";
+char   *CONFIG = _PATH_INETDCONF;
 char   **Argv;
 char   *LastArg;
 
 char   **Argv;
 char   *LastArg;
 
@@ -158,11 +162,14 @@ main(argc, argv, envp)
        int argc;
        char *argv[], *envp[];
 {
        int argc;
        char *argv[], *envp[];
 {
+       extern char *optarg;
+       extern int optind;
        register struct servtab *sep;
        register struct passwd *pwd;
        register struct servtab *sep;
        register struct passwd *pwd;
-       char *cp, buf[50];
-       int pid, i, dofork;
+       register int tmpint;
        struct sigvec sv;
        struct sigvec sv;
+       int ch, pid, dofork;
+       char buf[50];
 
        Argv = argv;
        if (envp == 0 || *envp == 0)
 
        Argv = argv;
        if (envp == 0 || *envp == 0)
@@ -170,46 +177,25 @@ main(argc, argv, envp)
        while (*envp)
                envp++;
        LastArg = envp[-1] + strlen(envp[-1]);
        while (*envp)
                envp++;
        LastArg = envp[-1] + strlen(envp[-1]);
-       argc--, argv++;
-       while (argc > 0 && *argv[0] == '-') {
-               for (cp = &argv[0][1]; *cp; cp++) switch (*cp) {
 
 
+       while ((ch = getopt(argc, argv, "d")) != EOF)
+               switch(ch) {
                case 'd':
                        debug = 1;
                        options |= SO_DEBUG;
                        break;
                case 'd':
                        debug = 1;
                        options |= SO_DEBUG;
                        break;
-
+               case '?':
                default:
                default:
-                       fprintf(stderr,
-                           "inetd: Unknown flag -%c ignored.\n", *cp);
-                       break;
+                       fprintf(stderr, "usage: inetd [-d]");
+                       exit(1);
                }
                }
-nextopt:
-               argc--, argv++;
-       }
+       argc -= optind;
+       argv += optind;
+
        if (argc > 0)
                CONFIG = argv[0];
        if (argc > 0)
                CONFIG = argv[0];
-       if (debug == 0) {
-               if (fork())
-                       exit(0);
-               { int s;
-               for (s = 0; s < 10; s++)
-                       (void) close(s);
-               }
-               (void) open("/", O_RDONLY);
-               (void) dup2(0, 1);
-               (void) dup2(0, 2);
-               { int tt = open("/dev/tty", O_RDWR);
-                 if (tt > 0) {
-                       ioctl(tt, TIOCNOTTY, (char *)0);
-                       close(tt);
-                 }
-               }
-               (void) setpgrp(0, 0);
-               signal(SIGTSTP, SIG_IGN);
-               signal(SIGTTIN, SIG_IGN);
-               signal(SIGTTOU, SIG_IGN);
-       }
+       if (debug == 0)
+               daemon(0, 0);
        openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
        bzero((char *)&sv, sizeof(sv));
        sv.sv_mask = SIGBLOCK;
        openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
        bzero((char *)&sv, sizeof(sv));
        sv.sv_mask = SIGBLOCK;
@@ -221,8 +207,18 @@ nextopt:
        sv.sv_handler = reapchild;
        sigvec(SIGCHLD, &sv, (struct sigvec *)0);
 
        sv.sv_handler = reapchild;
        sigvec(SIGCHLD, &sv, (struct sigvec *)0);
 
+       {
+               /* space for daemons to overwrite environment for ps */
+#define        DUMMYSIZE       100
+               char dummy[DUMMYSIZE];
+
+               (void)memset(dummy, 'x', sizeof(DUMMYSIZE) - 1);
+               dummy[DUMMYSIZE - 1] = '\0';
+               (void)setenv("inetd_dummy", dummy, 1);
+       }
+
        for (;;) {
        for (;;) {
-           int s, ctrl, n;
+           int n, ctrl;
            fd_set readable;
 
            if (nsock == 0) {
            fd_set readable;
 
            if (nsock == 0) {
@@ -244,7 +240,7 @@ nextopt:
                n--;
                if (debug)
                        fprintf(stderr, "someone wants %s\n", sep->se_service);
                n--;
                if (debug)
                        fprintf(stderr, "someone wants %s\n", sep->se_service);
-               if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
+               if (sep->se_socktype == SOCK_STREAM) {
                        ctrl = accept(sep->se_fd, (struct sockaddr *)0,
                            (int *)0);
                        if (debug)
                        ctrl = accept(sep->se_fd, (struct sockaddr *)0,
                            (int *)0);
                        if (debug)
@@ -252,7 +248,8 @@ nextopt:
                        if (ctrl < 0) {
                                if (errno == EINTR)
                                        continue;
                        if (ctrl < 0) {
                                if (errno == EINTR)
                                        continue;
-                               syslog(LOG_WARNING, "accept: %m");
+                               syslog(LOG_WARNING, "accept (for %s): %m",
+                                       sep->se_service);
                                continue;
                        }
                } else
                                continue;
                        }
                } else
@@ -292,7 +289,7 @@ nextopt:
                        pid = fork();
                }
                if (pid < 0) {
                        pid = fork();
                }
                if (pid < 0) {
-                       if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
+                       if (sep->se_socktype == SOCK_STREAM)
                                close(ctrl);
                        sigsetmask(0L);
                        sleep(1);
                                close(ctrl);
                        sigsetmask(0L);
                        sleep(1);
@@ -305,22 +302,12 @@ nextopt:
                }
                sigsetmask(0L);
                if (pid == 0) {
                }
                sigsetmask(0L);
                if (pid == 0) {
-                       if (debug) {
-                           int tt;
-
-                           if (dofork && (tt = open("/dev/tty", O_RDWR)) > 0) {
-                               ioctl(tt, TIOCNOTTY, 0);
-                               close(tt);
-                           }
-                           (void) setpgrp(0, 0);
-                           signal(SIGTSTP, SIG_IGN);
-                           signal(SIGTTIN, SIG_IGN);
-                           signal(SIGTTOU, SIG_IGN);
-                       }
+                       if (debug && dofork)
+                               setsid();
                        if (dofork)
                        if (dofork)
-                               for (i = getdtablesize(); --i > 2; )
-                                       if (i != ctrl)
-                                               close(i);
+                               for (tmpint = getdtablesize(); --tmpint > 2; )
+                                       if (tmpint != ctrl)
+                                               close(tmpint);
                        if (sep->se_bi)
                                (*sep->se_bi->bi_fn)(ctrl, sep);
                        else {
                        if (sep->se_bi)
                                (*sep->se_bi->bi_fn)(ctrl, sep);
                        else {
@@ -351,12 +338,13 @@ nextopt:
                                _exit(1);
                        }
                }
                                _exit(1);
                        }
                }
-               if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
+               if (sep->se_socktype == SOCK_STREAM)
                        close(ctrl);
            }
        }
 }
 
                        close(ctrl);
            }
        }
 }
 
+void
 reapchild()
 {
        union wait status;
 reapchild()
 {
        union wait status;
@@ -385,6 +373,7 @@ reapchild()
        }
 }
 
        }
 }
 
+void
 config()
 {
        register struct servtab *sep, *cp, **sepp;
 config()
 {
        register struct servtab *sep, *cp, **sepp;
@@ -406,7 +395,14 @@ config()
                        int i;
 
                        omask = sigblock(SIGBLOCK);
                        int i;
 
                        omask = sigblock(SIGBLOCK);
-                       if (cp->se_bi == 0)
+                       /*
+                        * sep->se_wait may be holding the pid of a daemon
+                        * that we're waiting for.  If so, don't overwrite
+                        * it unless the config file explicitly says don't 
+                        * wait.
+                        */
+                       if (cp->se_bi == 0 && 
+                           (sep->se_wait == 1 || cp->se_wait == 0))
                                sep->se_wait = cp->se_wait;
 #define SWAP(a, b) { char *c = a; a = b; b = c; }
                        if (cp->se_user)
                                sep->se_wait = cp->se_wait;
 #define SWAP(a, b) { char *c = a; a = b; b = c; }
                        if (cp->se_user)
@@ -465,6 +461,7 @@ config()
        (void) sigsetmask(omask);
 }
 
        (void) sigsetmask(omask);
 }
 
+void
 retry()
 {
        register struct servtab *sep;
 retry()
 {
        register struct servtab *sep;
@@ -552,11 +549,10 @@ setconfig()
 
 endconfig()
 {
 
 endconfig()
 {
-
-       if (fconfig == NULL)
-               return;
-       fclose(fconfig);
-       fconfig = NULL;
+       if (fconfig) {
+               (void) fclose(fconfig);
+               fconfig = NULL;
+       }
 }
 
 struct servtab *
 }
 
 struct servtab *
@@ -644,10 +640,10 @@ again:
        while (*cp == ' ' || *cp == '\t')
                cp++;
        if (*cp == '\0') {
        while (*cp == ' ' || *cp == '\t')
                cp++;
        if (*cp == '\0') {
-               char c;
+               int c;
 
                c = getc(fconfig);
 
                c = getc(fconfig);
-               ungetc(c, fconfig);
+               (void) ungetc(c, fconfig);
                if (c == ' ' || c == '\t')
                        if (cp = nextline(fconfig))
                                goto again;
                if (c == ' ' || c == '\t')
                        if (cp = nextline(fconfig))
                                goto again;
@@ -690,7 +686,7 @@ strdup(cp)
                syslog(LOG_ERR, "Out of memory.");
                exit(-1);
        }
                syslog(LOG_ERR, "Out of memory.");
                exit(-1);
        }
-       strcpy(new, cp);
+       (void)strcpy(new, cp);
        return (new);
 }
 
        return (new);
 }
 
@@ -718,13 +714,14 @@ setproctitle(a, s)
 /*
  * Internet services provided internally by inetd:
  */
 /*
  * Internet services provided internally by inetd:
  */
+#define        BUFSIZE 4096
 
 /* ARGSUSED */
 echo_stream(s, sep)            /* Echo service -- echo data back */
        int s;
        struct servtab *sep;
 {
 
 /* ARGSUSED */
 echo_stream(s, sep)            /* Echo service -- echo data back */
        int s;
        struct servtab *sep;
 {
-       char buffer[BUFSIZ];
+       char buffer[BUFSIZE];
        int i;
 
        setproctitle(sep->se_service, s);
        int i;
 
        setproctitle(sep->se_service, s);
@@ -739,7 +736,7 @@ echo_dg(s, sep)                     /* Echo service -- echo data back */
        int s;
        struct servtab *sep;
 {
        int s;
        struct servtab *sep;
 {
-       char buffer[BUFSIZ];
+       char buffer[BUFSIZE];
        int i, size;
        struct sockaddr sa;
 
        int i, size;
        struct sockaddr sa;
 
@@ -754,7 +751,7 @@ discard_stream(s, sep)              /* Discard service -- ignore data */
        int s;
        struct servtab *sep;
 {
        int s;
        struct servtab *sep;
 {
-       char buffer[BUFSIZ];
+       char buffer[BUFSIZE];
 
        setproctitle(sep->se_service, s);
        while (1) {
 
        setproctitle(sep->se_service, s);
        while (1) {
@@ -771,7 +768,7 @@ discard_dg(s, sep)          /* Discard service -- ignore data */
        int s;
        struct servtab *sep;
 {
        int s;
        struct servtab *sep;
 {
-       char buffer[BUFSIZ];
+       char buffer[BUFSIZE];
 
        (void) read(s, buffer, sizeof(buffer));
 }
 
        (void) read(s, buffer, sizeof(buffer));
 }
@@ -950,5 +947,5 @@ print_service(action, sep)
        fprintf(stderr,
            "%s: %s proto=%s, wait=%d, user=%s builtin=%x server=%s\n",
            action, sep->se_service, sep->se_proto,
        fprintf(stderr,
            "%s: %s proto=%s, wait=%d, user=%s builtin=%x server=%s\n",
            action, sep->se_service, sep->se_proto,
-           sep->se_wait, sep->se_user, sep->se_bi, sep->se_server);
+           sep->se_wait, sep->se_user, (int)sep->se_bi, sep->se_server);
 }
 }