branch for network release (no Kerberos), one more time...
[unix-history] / usr / src / libexec / rshd / rshd.c
index db4b592..18123c9 100644 (file)
+/*
+ * Copyright (c) 1983, 1988 The 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.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)rshd.c     4.13 83/02/21";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1983, 1988 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
 
 
-#include <sys/ioctl.h>
+#ifndef lint
+static char sccsid[] = "@(#)rshd.c     5.17.1.1 (Berkeley) %G%";
+#endif /* not lint */
+
+/*
+ * remote shell server:
+ *     [port]\0
+ *     remuser\0
+ *     locuser\0
+ *     command\0
+ *     data
+ */
 #include <sys/param.h>
 #include <sys/param.h>
+#include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/socket.h>
+#include <sys/file.h>
+#include <sys/time.h>
 
 #include <netinet/in.h>
 
 
 #include <netinet/in.h>
 
+#include <arpa/inet.h>
+
 #include <stdio.h>
 #include <errno.h>
 #include <pwd.h>
 #include <stdio.h>
 #include <errno.h>
 #include <pwd.h>
-#include <wait.h>
 #include <signal.h>
 #include <netdb.h>
 #include <signal.h>
 #include <netdb.h>
+#include <syslog.h>
 
 int    errno;
 
 int    errno;
-int    reapchild();
-struct sockaddr_in sin = { AF_INET };
-struct passwd *getpwnam();
-char   *index(), *rindex(), *sprintf();
-int    options;
-/* VARARGS 1 */
+int    keepalive = 1;
+char   *index(), *rindex(), *strncat();
+/*VARARGS1*/
 int    error();
 int    error();
-/*
- * remote execute server:
- *     remuser\0
- *     locuser\0
- *     command\0
- *     data
- */
+
+/*ARGSUSED*/
 main(argc, argv)
        int argc;
        char **argv;
 {
 main(argc, argv)
        int argc;
        char **argv;
 {
-       int f;
+       extern int opterr, optind, _check_rhosts_file;
+       struct linger linger;
+       int ch, on = 1, fromlen;
        struct sockaddr_in from;
        struct sockaddr_in from;
-       struct servent *sp;
 
 
-       sp = getservbyname("shell", "tcp");
-       if (sp == 0) {
-               fprintf(stderr, "rshd: tcp/shell: unknown service\n");
-               exit(1);
-       }
-#ifndef DEBUG
-       if (fork())
-               exit(0);
-       for (f = 0; f < 10; f++)
-               (void) close(f);
-       (void) open("/", 0);
-       (void) dup2(0, 1);
-       (void) dup2(0, 2);
-       { int t = open("/dev/tty", 2);
-         if (t >= 0) {
-               ioctl(t, TIOCNOTTY, (char *)0);
-               (void) close(t);
-         }
-       }
-#endif
-       sin.sin_port = sp->s_port;
-       argc--, argv++;
-       if (argc > 0 && !strcmp(argv[0], "-d")) {
-               options |= SO_DEBUG;
-               argc--, argv++;
-       }
-       if (argc > 0) {
-               int port = atoi(argv[0]);
+       openlog("rsh", LOG_PID | LOG_ODELAY, LOG_DAEMON);
 
 
-               if (port < 0) {
-                       fprintf(stderr, "%s: bad port #\n", argv[0]);
-                       exit(1);
+       opterr = 0;
+       while ((ch = getopt(argc, argv, "ln")) != EOF)
+               switch((char)ch) {
+               case 'l':
+                       _check_rhosts_file = 0;
+                       break;
+               case 'n':
+                       keepalive = 0;
+                       break;
+               case '?':
+               default:
+                       syslog(LOG_ERR, "usage: rshd [-l]");
+                       break;
                }
                }
-               sin.sin_port = htons((u_short)port);
-               argc--, argv++;
-       }
-       f = socket(AF_INET, SOCK_STREAM, 0, 0);
-       if (f < 0) {
-               perror("rshd: socket");
-               exit(1);
-       }
-       if (options & SO_DEBUG && setsockopt(f, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
-               perror("rshd: setsockopt (SO_DEBUG)");
-#ifdef notdef
-       if (setsockopt(f, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0)
-               perror("rshd: setsockopt (SO_KEEPALIVE)");
-#endif
-       if (bind(f, (caddr_t)&sin, sizeof (sin), 0) < 0) {
-               perror("rshd: bind");
-               exit(1);
-       }
-       sigset(SIGCHLD, reapchild);
-       listen(f, 10);
-       for (;;) {
-               int g, len = sizeof (from);
 
 
-               g = accept(f, &from, &len, 0);
-               if (g < 0) {
-                       if (errno == EINTR)
-                               continue;
-                       perror("rshd: accept");
-                       continue;
-               }
-               if (fork() == 0) {
-                       close(f);
-                       signal(SIGCHLD, SIG_IGN);
-                       doit(g, &from);
-               }
-               close(g);
-       }
-}
+       argc -= optind;
+       argv += optind;
 
 
-reapchild()
-{
-       union wait status;
 
 
-       while (wait3(&status, WNOHANG, 0) > 0)
-               ;
+       fromlen = sizeof (from);
+       if (getpeername(0, &from, &fromlen) < 0) {
+               fprintf(stderr, "%s: ", argv[0]);
+               perror("getpeername");
+               _exit(1);
+       }
+       if (keepalive &&
+           setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
+           sizeof(on)) < 0)
+               syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
+       linger.l_onoff = 1;
+       linger.l_linger = 60;                   /* XXX */
+       if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
+           sizeof (linger)) < 0)
+               syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
+       doit(0, &from);
 }
 
 char   username[20] = "USER=";
 char   homedir[64] = "HOME=";
 char   shell[64] = "SHELL=";
 char   *envinit[] =
 }
 
 char   username[20] = "USER=";
 char   homedir[64] = "HOME=";
 char   shell[64] = "SHELL=";
 char   *envinit[] =
-           {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", username, 0};
+           {homedir, shell, "PATH=/usr/ucb:/bin:/usr/bin:", username, 0};
 char   **environ;
 
 doit(f, fromp)
 char   **environ;
 
 doit(f, fromp)
@@ -132,12 +119,16 @@ doit(f, fromp)
        char cmdbuf[NCARGS+1], *cp;
        char locuser[16], remuser[16];
        struct passwd *pwd;
        char cmdbuf[NCARGS+1], *cp;
        char locuser[16], remuser[16];
        struct passwd *pwd;
-       int s, backoff;
+       int s;
        struct hostent *hp;
        struct hostent *hp;
+       char *hostname;
        short port;
        short port;
-       int pv[2], pid, ready, readfrom, cc;
+       int pv[2], pid, cc;
+       int nfd;
+       fd_set ready, readfrom;
        char buf[BUFSIZ], sig;
        int one = 1;
        char buf[BUFSIZ], sig;
        int one = 1;
+       char remotehost[2 * MAXHOSTNAMELEN + 1];
 
        (void) signal(SIGINT, SIG_DFL);
        (void) signal(SIGQUIT, SIG_DFL);
 
        (void) signal(SIGINT, SIG_DFL);
        (void) signal(SIGQUIT, SIG_DFL);
@@ -151,53 +142,96 @@ doit(f, fromp)
        }
 #endif
        fromp->sin_port = ntohs((u_short)fromp->sin_port);
        }
 #endif
        fromp->sin_port = ntohs((u_short)fromp->sin_port);
-       if (fromp->sin_family != AF_INET ||
-           fromp->sin_port >= IPPORT_RESERVED) {
-               fprintf(stderr, "rshd: malformed from address\n");
+       if (fromp->sin_family != AF_INET) {
+               syslog(LOG_ERR, "malformed from address\n");
                exit(1);
        }
                exit(1);
        }
+
+       if (fromp->sin_port >= IPPORT_RESERVED ||
+           fromp->sin_port < IPPORT_RESERVED/2) {
+               syslog(LOG_NOTICE, "Connection from %s on illegal port",
+                       inet_ntoa(fromp->sin_addr));
+               exit(1);
+       }
+
        (void) alarm(60);
        port = 0;
        for (;;) {
                char c;
        (void) alarm(60);
        port = 0;
        for (;;) {
                char c;
-               if (read(f, &c, 1) != 1) {
-                       int how = 1+1;
-
-                       perror("rshd: read");
-                       shutdown(f, &how);
+               if ((cc = read(f, &c, 1)) != 1) {
+                       if (cc < 0)
+                               syslog(LOG_NOTICE, "read: %m");
+                       shutdown(f, 1+1);
                        exit(1);
                }
                if (c == 0)
                        break;
                port = port * 10 + c - '0';
        }
                        exit(1);
                }
                if (c == 0)
                        break;
                port = port * 10 + c - '0';
        }
+
        (void) alarm(0);
        if (port != 0) {
        (void) alarm(0);
        if (port != 0) {
-               int lport = IPPORT_RESERVED - 1, retryshift;
+               int lport = IPPORT_RESERVED - 1;
                s = rresvport(&lport);
                if (s < 0) {
                s = rresvport(&lport);
                if (s < 0) {
-                       perror("rshd: can't get stderr port");
+                       syslog(LOG_ERR, "can't get stderr port: %m");
                        exit(1);
                }
                if (port >= IPPORT_RESERVED) {
                        exit(1);
                }
                if (port >= IPPORT_RESERVED) {
-                       fprintf(stderr, "rshd: 2nd port not reserved\n");
+                       syslog(LOG_ERR, "2nd port not reserved\n");
                        exit(1);
                }
                fromp->sin_port = htons((u_short)port);
                        exit(1);
                }
                fromp->sin_port = htons((u_short)port);
-               if (connect(s, fromp, sizeof (*fromp), 0) < 0) {
-                       perror("rshd: connect");
+               if (connect(s, fromp, sizeof (*fromp)) < 0) {
+                       syslog(LOG_INFO, "connect second port: %m");
                        exit(1);
                }
        }
                        exit(1);
                }
        }
+
+#ifdef notdef
+       /* from inetd, f is already on 0, 1, 2 */
        dup2(f, 0);
        dup2(f, 1);
        dup2(f, 2);
        dup2(f, 0);
        dup2(f, 1);
        dup2(f, 2);
-       hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr),
+#endif
+       hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr),
                fromp->sin_family);
                fromp->sin_family);
-       if (hp == 0) {
-               error("Host name for your address unknown\n");
-               exit(1);
-       }
+       if (hp) {
+               /*
+                * If name returned by gethostbyaddr is in our domain,
+                * attempt to verify that we haven't been fooled by someone
+                * in a remote net; look up the name and check that this
+                * address corresponds to the name.
+                */
+               if (local_domain(hp->h_name)) {
+                       strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
+                       remotehost[sizeof(remotehost) - 1] = 0;
+                       hp = gethostbyname(remotehost);
+                       if (hp == NULL) {
+                               syslog(LOG_INFO,
+                                   "Couldn't look up address for %s",
+                                   remotehost);
+                               error("Couldn't look up address for your host");
+                               exit(1);
+                       } else for (; ; hp->h_addr_list++) {
+                               if (!bcmp(hp->h_addr_list[0],
+                                   (caddr_t)&fromp->sin_addr,
+                                   sizeof(fromp->sin_addr)))
+                                       break;
+                               if (hp->h_addr_list[0] == NULL) {
+                                       syslog(LOG_NOTICE,
+                                         "Host addr %s not listed for host %s",
+                                           inet_ntoa(fromp->sin_addr),
+                                           hp->h_name);
+                                       error("Host address mismatch");
+                                       exit(1);
+                               }
+                       }
+               }
+               hostname = hp->h_name;
+       } else
+               hostname = inet_ntoa(fromp->sin_addr);
+
        getstr(remuser, sizeof(remuser), "remuser");
        getstr(locuser, sizeof(locuser), "locuser");
        getstr(cmdbuf, sizeof(cmdbuf), "command");
        getstr(remuser, sizeof(remuser), "remuser");
        getstr(locuser, sizeof(locuser), "locuser");
        getstr(cmdbuf, sizeof(cmdbuf), "command");
@@ -209,14 +243,25 @@ doit(f, fromp)
        }
        endpwent();
        if (chdir(pwd->pw_dir) < 0) {
        }
        endpwent();
        if (chdir(pwd->pw_dir) < 0) {
+               (void) chdir("/");
+#ifdef notdef
                error("No remote directory.\n");
                exit(1);
                error("No remote directory.\n");
                exit(1);
+#endif
        }
        }
-       if (ruserok(hp->h_name, remuser, locuser) < 0) {
+
+       if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
+           ruserok(hostname, pwd->pw_uid == 0, remuser, locuser) < 0) {
                error("Permission denied.\n");
                exit(1);
        }
                error("Permission denied.\n");
                exit(1);
        }
+
+       if (pwd->pw_uid && !access("/etc/nologin", F_OK)) {
+               error("Logins currently disabled.\n");
+               exit(1);
+       }
        (void) write(2, "\0", 1);
        (void) write(2, "\0", 1);
+
        if (port) {
                if (pipe(pv) < 0) {
                        error("Can't make pipe.\n");
        if (port) {
                if (pipe(pv) < 0) {
                        error("Can't make pipe.\n");
@@ -227,31 +272,41 @@ doit(f, fromp)
                        error("Try again.\n");
                        exit(1);
                }
                        error("Try again.\n");
                        exit(1);
                }
+               if (pv[0] > s)
+                       nfd = pv[0];
+               else
+                       nfd = s;
+               nfd++;
                if (pid) {
                        (void) close(0); (void) close(1); (void) close(2);
                        (void) close(f); (void) close(pv[1]);
                if (pid) {
                        (void) close(0); (void) close(1); (void) close(2);
                        (void) close(f); (void) close(pv[1]);
-                       readfrom = (1<<s) | (1<<pv[0]);
-                       ioctl(pv[1], FIONBIO, (char *)&one);
+                       FD_ZERO(&readfrom);
+                       FD_SET(s, &readfrom);
+                       FD_SET(pv[0], &readfrom);
+                       ioctl(pv[0], FIONBIO, (char *)&one);
                        /* should set s nbio! */
                        do {
                                ready = readfrom;
                        /* should set s nbio! */
                        do {
                                ready = readfrom;
-                               if (select(16, &ready, 0, 0, 0) < 0)
+                               if (select(nfd, &ready, (fd_set *)0,
+                                   (fd_set *)0, (struct timeval *)0) < 0)
                                        break;
                                        break;
-                               if (ready & (1<<s)) {
+                               if (FD_ISSET(s, &ready)) {
                                        if (read(s, &sig, 1) <= 0)
                                        if (read(s, &sig, 1) <= 0)
-                                               readfrom &= ~(1<<s);
+                                               FD_CLR(s, &readfrom);
                                        else
                                                killpg(pid, sig);
                                }
                                        else
                                                killpg(pid, sig);
                                }
-                               if (ready & (1<<pv[0])) {
+                               if (FD_ISSET(pv[0], &ready)) {
+                                       errno = 0;
                                        cc = read(pv[0], buf, sizeof (buf));
                                        if (cc <= 0) {
                                                shutdown(s, 1+1);
                                        cc = read(pv[0], buf, sizeof (buf));
                                        if (cc <= 0) {
                                                shutdown(s, 1+1);
-                                               readfrom &= ~(1<<pv[0]);
+                                               FD_CLR(pv[0], &readfrom);
                                        } else
                                                (void) write(s, buf, cc);
                                }
                                        } else
                                                (void) write(s, buf, cc);
                                }
-                       } while (readfrom);
+                       } while (FD_ISSET(s, &readfrom) ||
+                           FD_ISSET(pv[0], &readfrom));
                        exit(0);
                }
                setpgrp(0, getpid());
                        exit(0);
                }
                setpgrp(0, getpid());
@@ -261,9 +316,9 @@ doit(f, fromp)
        if (*pwd->pw_shell == '\0')
                pwd->pw_shell = "/bin/sh";
        (void) close(f);
        if (*pwd->pw_shell == '\0')
                pwd->pw_shell = "/bin/sh";
        (void) close(f);
+       (void) setgid((gid_t)pwd->pw_gid);
        initgroups(pwd->pw_name, pwd->pw_gid);
        initgroups(pwd->pw_name, pwd->pw_gid);
-       (void) setuid(pwd->pw_uid);
-       (void) setgid(pwd->pw_gid);
+       (void) setuid((uid_t)pwd->pw_uid);
        environ = envinit;
        strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
        strncat(shell, pwd->pw_shell, sizeof(shell)-7);
        environ = envinit;
        strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
        strncat(shell, pwd->pw_shell, sizeof(shell)-7);
@@ -276,19 +331,17 @@ doit(f, fromp)
        execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
        perror(pwd->pw_shell);
        exit(1);
        execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
        perror(pwd->pw_shell);
        exit(1);
-protofail:
-       error("rsh: protocol failure detected by remote\n");
-       exit(1);
 }
 
 }
 
-/* VARARGS 1 */
-error(fmt)
+/*VARARGS1*/
+error(fmt, a1, a2, a3)
        char *fmt;
        char *fmt;
+       int a1, a2, a3;
 {
        char buf[BUFSIZ];
 
        buf[0] = 1;
 {
        char buf[BUFSIZ];
 
        buf[0] = 1;
-       (void) sprintf(buf+1, fmt);
+       (void) sprintf(buf+1, fmt, a1, a2, a3);
        (void) write(2, buf, strlen(buf));
 }
 
        (void) write(2, buf, strlen(buf));
 }
 
@@ -309,3 +362,24 @@ getstr(buf, cnt, err)
                }
        } while (c != 0);
 }
                }
        } while (c != 0);
 }
+
+/*
+ * Check whether host h is in our local domain,
+ * as determined by the part of the name following
+ * the first '.' in its name and in ours.
+ * If either name is unqualified (contains no '.'),
+ * assume that the host is local, as it will be
+ * interpreted as such.
+ */
+local_domain(h)
+       char *h;
+{
+       char localhost[MAXHOSTNAMELEN];
+       char *p1, *p2 = index(h, '.');
+
+       (void) gethostname(localhost, sizeof(localhost));
+       p1 = index(localhost, '.');
+       if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
+               return(1);
+       return(0);
+}