BSD 4_3 release
[unix-history] / usr / src / etc / rexecd.c
index 7f61c9d..a3e8196 100644 (file)
@@ -1,11 +1,23 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)rexecd.c   4.10 (Berkeley) 83/07/02";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
+#ifndef lint
+static char sccsid[] = "@(#)rexecd.c   5.4 (Berkeley) 5/9/86";
+#endif not lint
 
 #include <sys/ioctl.h>
 #include <sys/param.h>
 #include <sys/socket.h>
 
 #include <sys/ioctl.h>
 #include <sys/param.h>
 #include <sys/socket.h>
-#include <sys/wait.h>
+#include <sys/time.h>
 
 #include <netinet/in.h>
 
 
 #include <netinet/in.h>
 
@@ -16,12 +28,11 @@ static char sccsid[] = "@(#)rexecd.c        4.10 (Berkeley) 83/07/02";
 #include <netdb.h>
 
 extern errno;
 #include <netdb.h>
 
 extern errno;
-struct sockaddr_in sin = { AF_INET };
 struct passwd *getpwnam();
 struct passwd *getpwnam();
-char   *crypt(), *rindex(), *sprintf();
-/* VARARGS 1 */
+char   *crypt(), *rindex(), *strncat(), *sprintf();
+/*VARARGS1*/
 int    error();
 int    error();
-int    reapchild();
+
 /*
  * remote execute server:
  *     username\0
 /*
  * remote execute server:
  *     username\0
@@ -29,72 +40,21 @@ int reapchild();
  *     command\0
  *     data
  */
  *     command\0
  *     data
  */
+/*ARGSUSED*/
 main(argc, argv)
        int argc;
        char **argv;
 {
 main(argc, argv)
        int argc;
        char **argv;
 {
-       int f;
        struct sockaddr_in from;
        struct sockaddr_in from;
-       struct servent *sp;
+       int fromlen;
 
 
-       sp = getservbyname("exec", "tcp");
-       if (sp == 0) {
-               fprintf(stderr, "tcp/exec: unknown service\n");
-               exit(1);
-       }
-       sin.sin_port = sp->s_port;
-#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
-       argc--, argv++;
-       f = socket(AF_INET, SOCK_STREAM, 0, 0);
-       if (f < 0) {
-               perror("rexecd: socket");
-               exit(1);
-       }
-       if (bind(f, &sin, sizeof (sin), 0) < 0) {
-               perror("rexecd: bind:");
+       fromlen = sizeof (from);
+       if (getpeername(0, &from, &fromlen) < 0) {
+               fprintf(stderr, "%s: ", argv[0]);
+               perror("getpeername");
                exit(1);
        }
                exit(1);
        }
-       signal(SIGCHLD, reapchild);
-       listen(f, 10);
-       for (;;) {
-               int s, len = sizeof (from);
-
-               s = accept(f, &from, &len, 0);
-               if (s < 0) {
-                       if (errno == EINTR)
-                               continue;
-                       perror("rexecd: accept");
-                       sleep(1);
-                       continue;
-               }
-               if (fork() == 0) {
-                       signal(SIGCHLD, SIG_IGN);
-                       doit(s, &from);
-               }
-               (void) close(s);
-       }
-}
-
-reapchild()
-{
-       union wait status;
-
-       while (wait3(&status, WNOHANG, 0) > 0)
-               ;
+       doit(0, &from);
 }
 
 char   username[20] = "USER=";
 }
 
 char   username[20] = "USER=";
@@ -145,14 +105,14 @@ doit(f, fromp)
        }
        (void) alarm(0);
        if (port != 0) {
        }
        (void) alarm(0);
        if (port != 0) {
-               s = socket(AF_INET, SOCK_STREAM, 0, 0);
+               s = socket(AF_INET, SOCK_STREAM, 0);
                if (s < 0)
                        exit(1);
                if (s < 0)
                        exit(1);
-               if (bind(s, &asin, sizeof (asin), 0) < 0)
+               if (bind(s, &asin, sizeof (asin)) < 0)
                        exit(1);
                (void) alarm(60);
                fromp->sin_port = htons((u_short)port);
                        exit(1);
                (void) alarm(60);
                fromp->sin_port = htons((u_short)port);
-               if (connect(s, fromp, sizeof (*fromp), 0) < 0)
+               if (connect(s, fromp, sizeof (*fromp)) < 0)
                        exit(1);
                (void) alarm(0);
        }
                        exit(1);
                (void) alarm(0);
        }
@@ -193,7 +153,8 @@ doit(f, fromp)
                        /* should set s nbio! */
                        do {
                                ready = readfrom;
                        /* should set s nbio! */
                        do {
                                ready = readfrom;
-                               (void) select(16, &ready, 0, 0, 0);
+                               (void) select(16, &ready, (fd_set *)0,
+                                   (fd_set *)0, (struct timeval *)0);
                                if (ready & (1<<s)) {
                                        if (read(s, &sig, 1) <= 0)
                                                readfrom &= ~(1<<s);
                                if (ready & (1<<s)) {
                                        if (read(s, &sig, 1) <= 0)
                                                readfrom &= ~(1<<s);
@@ -217,10 +178,11 @@ doit(f, fromp)
        }
        if (*pwd->pw_shell == '\0')
                pwd->pw_shell = "/bin/sh";
        }
        if (*pwd->pw_shell == '\0')
                pwd->pw_shell = "/bin/sh";
-       (void) close(f);
+       if (f > 2)
+               (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);
@@ -235,7 +197,7 @@ doit(f, fromp)
        exit(1);
 }
 
        exit(1);
 }
 
-/* VARARGS 1 */
+/*VARARGS1*/
 error(fmt, a1, a2, a3)
        char *fmt;
        int a1, a2, a3;
 error(fmt, a1, a2, a3)
        char *fmt;
        int a1, a2, a3;