prettiness police
authorJan-Simon Pendry <pendry@ucbvax.Berkeley.EDU>
Thu, 7 Apr 1994 03:06:56 +0000 (19:06 -0800)
committerJan-Simon Pendry <pendry@ucbvax.Berkeley.EDU>
Thu, 7 Apr 1994 03:06:56 +0000 (19:06 -0800)
SCCS-vsn: libexec/rshd/rshd.c 8.2
SCCS-vsn: usr.bin/rsh/rsh.c 8.2

usr/src/libexec/rshd/rshd.c
usr/src/usr.bin/rsh/rsh.c

index 59e76eb..228845b 100644 (file)
@@ -1,5 +1,5 @@
 /*-
 /*-
- * Copyright (c) 1988, 1989, 1992, 1993
+ * Copyright (c) 1988, 1989, 1992, 1993, 1994
  *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
  *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
@@ -7,12 +7,12 @@
 
 #ifndef lint
 static char copyright[] =
 
 #ifndef lint
 static char copyright[] =
-"@(#) Copyright (c) 1988, 1989, 1992, 1993\n\
+"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
        The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
        The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)rshd.c     8.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)rshd.c     8.2 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -32,17 +32,16 @@ static char sccsid[] = "@(#)rshd.c  8.1 (Berkeley) %G%";
 #include <arpa/inet.h>
 #include <netdb.h>
 
 #include <arpa/inet.h>
 #include <netdb.h>
 
+#include <errno.h>
 #include <fcntl.h>
 #include <fcntl.h>
-#include <signal.h>
+#include <paths.h>
 #include <pwd.h>
 #include <pwd.h>
-#include <syslog.h>
-#include "pathnames.h"
-#include <unistd.h>
-#include <errno.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <paths.h>
+#include <syslog.h>
+#include <unistd.h>
 
 int    keepalive = 1;
 int    check_all = 0;
 
 int    keepalive = 1;
 int    check_all = 0;
@@ -84,7 +83,7 @@ main(argc, argv)
                case '?':
                default:
                        usage();
                case '?':
                default:
                        usage();
-                       exit(2);
+                       break;
                }
 
        argc -= optind;
                }
 
        argc -= optind;
@@ -421,7 +420,7 @@ doit(fromp)
        strcat(path, _PATH_DEFPATH);
        strncat(shell, pwd->pw_shell, sizeof(shell)-7);
        strncat(username, pwd->pw_name, sizeof(username)-6);
        strcat(path, _PATH_DEFPATH);
        strncat(shell, pwd->pw_shell, sizeof(shell)-7);
        strncat(username, pwd->pw_name, sizeof(username)-6);
-       cp = rindex(pwd->pw_shell, '/');
+       cp = strrchr(pwd->pw_shell, '/');
        if (cp)
                cp++;
        else
        if (cp)
                cp++;
        else
@@ -557,8 +556,7 @@ char *
 topdomain(h)
        char *h;
 {
 topdomain(h)
        char *h;
 {
-       register char *p;
-       char *maybe = NULL;
+       char *p, *maybe = NULL;
        int dots = 0;
 
        for (p = h + strlen(h); p >= h; p--) {
        int dots = 0;
 
        for (p = h + strlen(h); p >= h; p--) {
@@ -574,5 +572,7 @@ topdomain(h)
 void
 usage()
 {
 void
 usage()
 {
+
        syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
        syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
+       exit(2);
 }
 }
index 37f9626..138d571 100644 (file)
@@ -12,7 +12,7 @@ static char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)rsh.c      8.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)rsh.c      8.2 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -29,11 +29,16 @@ static char sccsid[] = "@(#)rsh.c   8.1 (Berkeley) %G%";
 #include <netinet/in.h>
 #include <netdb.h>
 
 #include <netinet/in.h>
 #include <netdb.h>
 
+#include <err.h>
+#include <errno.h>
 #include <pwd.h>
 #include <pwd.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 #include <string.h>
+#include <unistd.h>
 #include <varargs.h>
 #include <varargs.h>
+
 #include "pathnames.h"
 
 #ifdef KERBEROS
 #include "pathnames.h"
 
 #ifdef KERBEROS
@@ -50,30 +55,33 @@ extern char *krb_realmofhost();
 /*
  * rsh - remote shell
  */
 /*
  * rsh - remote shell
  */
-extern int errno;
-int rfd2;
+int    rfd2;
+
+char   *copyargs __P((char **));
+void   sendsig __P((int));
+void   talk __P((int, long, pid_t, int));
+void   usage __P((void));
+void   warning __P(());
 
 
+int
 main(argc, argv)
        int argc;
        char **argv;
 {
 main(argc, argv)
        int argc;
        char **argv;
 {
-       extern char *optarg;
-       extern int optind;
        struct passwd *pw;
        struct servent *sp;
        struct passwd *pw;
        struct servent *sp;
-       struct hostent *hp;
        long omask;
        long omask;
-       int argoff, asrsh, ch, dflag, nflag, one, pid, rem, uid;
-       register char *p;
-       char *args, *host, *user, *copyargs();
-       void sendsig();
+       int argoff, asrsh, ch, dflag, nflag, one, rem;
+       pid_t pid;
+       uid_t uid;
+       char *args, *host, *p, *user;
 
        argoff = asrsh = dflag = nflag = 0;
        one = 1;
        host = user = NULL;
 
        /* if called as something other than "rsh", use it as the host name */
 
        argoff = asrsh = dflag = nflag = 0;
        one = 1;
        host = user = NULL;
 
        /* if called as something other than "rsh", use it as the host name */
-       if (p = rindex(argv[0], '/'))
+       if (p = strrchr(argv[0], '/'))
                ++p;
        else
                p = argv[0];
                ++p;
        else
                p = argv[0];
@@ -137,17 +145,14 @@ main(argc, argv)
                if (asrsh)
                        *argv = "rlogin";
                execv(_PATH_RLOGIN, argv);
                if (asrsh)
                        *argv = "rlogin";
                execv(_PATH_RLOGIN, argv);
-               (void)fprintf(stderr, "rsh: can't exec %s.\n", _PATH_RLOGIN);
-               exit(1);
+               err(1, "can't exec %s", _PATH_RLOGIN);
        }
 
        argc -= optind;
        argv += optind;
 
        }
 
        argc -= optind;
        argv += optind;
 
-       if (!(pw = getpwuid(uid = getuid()))) {
-               (void)fprintf(stderr, "rsh: unknown user id.\n");
-               exit(1);
-       }
+       if (!(pw = getpwuid(uid = getuid())))
+               errx(1, "unknown user id");
        if (!user)
                user = pw->pw_name;
 
        if (!user)
                user = pw->pw_name;
 
@@ -169,20 +174,18 @@ main(argc, argv)
 #endif
        if (sp == NULL)
                sp = getservbyname("shell", "tcp");
 #endif
        if (sp == NULL)
                sp = getservbyname("shell", "tcp");
-       if (sp == NULL) {
-               (void)fprintf(stderr, "rsh: shell/tcp: unknown service.\n");
-               exit(1);
-       }
+       if (sp == NULL)
+               errx(1, "shell/tcp: unknown service");
 
 #ifdef KERBEROS
 try_connect:
        if (use_kerberos) {
 
 #ifdef KERBEROS
 try_connect:
        if (use_kerberos) {
+               struct hostent *hp;
+
                /* fully qualify hostname (needed for krb_realmofhost) */
                hp = gethostbyname(host);
                /* fully qualify hostname (needed for krb_realmofhost) */
                hp = gethostbyname(host);
-               if (hp != NULL && !(host = strdup(hp->h_name))) {
-                       (void)fprintf(stderr, "rsh: %s.\n", strerror(ENOMEM));
-                       exit(1);
-               }
+               if (hp != NULL && !(host = strdup(hp->h_name)))
+                       err(1, NULL);
 
                rem = KSUCCESS;
                errno = 0;
 
                rem = KSUCCESS;
                errno = 0;
@@ -194,11 +197,8 @@ try_connect:
                if (rem < 0) {
                        use_kerberos = 0;
                        sp = getservbyname("shell", "tcp");
                if (rem < 0) {
                        use_kerberos = 0;
                        sp = getservbyname("shell", "tcp");
-                       if (sp == NULL) {
-                               (void)fprintf(stderr,
-                                   "rsh: unknown service shell/tcp.\n");
-                               exit(1);
-                       }
+                       if (sp == NULL)
+                               errx(1, "shell/tcp: unknown service");
                        if (errno == ECONNREFUSED)
                                warning("remote host doesn't support Kerberos");
                        if (errno == ENOENT)
                        if (errno == ECONNREFUSED)
                                warning("remote host doesn't support Kerberos");
                        if (errno == ENOENT)
@@ -206,11 +206,8 @@ try_connect:
                        goto try_connect;
                }
        } else {
                        goto try_connect;
                }
        } else {
-               if (doencrypt) {
-                       (void)fprintf(stderr,
-                           "rsh: the -x flag requires Kerberos authentication.\n");
-                       exit(1);
-               }
+               if (doencrypt)
+                       errx(1, "the -x flag requires Kerberos authentication");
                rem = rcmd(&host, sp->s_port, pw->pw_name, user, args, &rfd2);
        }
 #else
                rem = rcmd(&host, sp->s_port, pw->pw_name, user, args, &rfd2);
        }
 #else
@@ -220,19 +217,15 @@ try_connect:
        if (rem < 0)
                exit(1);
 
        if (rem < 0)
                exit(1);
 
-       if (rfd2 < 0) {
-               (void)fprintf(stderr, "rsh: can't establish stderr.\n");
-               exit(1);
-       }
+       if (rfd2 < 0)
+               errx(1, "can't establish stderr");
        if (dflag) {
                if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one,
                    sizeof(one)) < 0)
        if (dflag) {
                if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one,
                    sizeof(one)) < 0)
-                       (void)fprintf(stderr, "rsh: setsockopt: %s.\n",
-                           strerror(errno));
+                       warn("setsockopt");
                if (setsockopt(rfd2, SOL_SOCKET, SO_DEBUG, &one,
                    sizeof(one)) < 0)
                if (setsockopt(rfd2, SOL_SOCKET, SO_DEBUG, &one,
                    sizeof(one)) < 0)
-                       (void)fprintf(stderr, "rsh: setsockopt: %s.\n",
-                           strerror(errno));
+                       warn("setsockopt");
        }
 
        (void)setuid(uid);
        }
 
        (void)setuid(uid);
@@ -246,11 +239,8 @@ try_connect:
 
        if (!nflag) {
                pid = fork();
 
        if (!nflag) {
                pid = fork();
-               if (pid < 0) {
-                       (void)fprintf(stderr,
-                           "rsh: fork: %s.\n", strerror(errno));
-                       exit(1);
-               }
+               if (pid < 0)
+                       err(1, "fork");
        }
 
 #ifdef KERBEROS
        }
 
 #ifdef KERBEROS
@@ -267,15 +257,16 @@ try_connect:
        exit(0);
 }
 
        exit(0);
 }
 
+void
 talk(nflag, omask, pid, rem)
 talk(nflag, omask, pid, rem)
-       int nflag, pid;
+       int nflag;
        long omask;
        long omask;
-       register int rem;
+       pid_t pid;
+       int rem;
 {
 {
-       register int cc, wc;
-       register char *bp;
-       int readfrom, ready, rembits;
-       char buf[BUFSIZ];
+       int cc, wc;
+       fd_set readfrom, ready, rembits;
+       char *bp, buf[BUFSIZ];
 
        if (!nflag && pid == 0) {
                (void)close(rfd2);
 
        if (!nflag && pid == 0) {
                (void)close(rfd2);
@@ -285,16 +276,15 @@ reread:           errno = 0;
                        goto done;
                bp = buf;
 
                        goto done;
                bp = buf;
 
-rewrite:       rembits = 1 << rem;
+rewrite:       
+               FD_ZERO(&rembits);
+               FD_SET(rem, &rembits);
                if (select(16, 0, &rembits, 0, 0) < 0) {
                if (select(16, 0, &rembits, 0, 0) < 0) {
-                       if (errno != EINTR) {
-                               (void)fprintf(stderr,
-                                   "rsh: select: %s.\n", strerror(errno));
-                               exit(1);
-                       }
+                       if (errno != EINTR)
+                               err(1, "select");
                        goto rewrite;
                }
                        goto rewrite;
                }
-               if ((rembits & (1 << rem)) == 0)
+               if (!FD_ISSET(rem, &rembits))
                        goto rewrite;
 #ifdef KERBEROS
 #endif
                        goto rewrite;
 #ifdef KERBEROS
 #endif
@@ -315,46 +305,48 @@ done:
        }
 
        (void)sigsetmask(omask);
        }
 
        (void)sigsetmask(omask);
-       readfrom = (1 << rfd2) | (1 << rem);
+       FD_ZERO(&readfrom);
+       FD_SET(rfd2, &readfrom);
+       FD_SET(rem, &readfrom);
        do {
                ready = readfrom;
                if (select(16, &ready, 0, 0, 0) < 0) {
        do {
                ready = readfrom;
                if (select(16, &ready, 0, 0, 0) < 0) {
-                       if (errno != EINTR) {
-                               (void)fprintf(stderr,
-                                   "rsh: select: %s.\n", strerror(errno));
-                               exit(1);
-                       }
+                       if (errno != EINTR)
+                               err(1, "select");
                        continue;
                }
                        continue;
                }
-               if (ready & (1 << rfd2)) {
+               if (FD_ISSET(rfd2, &ready)) {
                        errno = 0;
 #ifdef KERBEROS
 #endif
                                cc = read(rfd2, buf, sizeof buf);
                        if (cc <= 0) {
                                if (errno != EWOULDBLOCK)
                        errno = 0;
 #ifdef KERBEROS
 #endif
                                cc = read(rfd2, buf, sizeof buf);
                        if (cc <= 0) {
                                if (errno != EWOULDBLOCK)
-                                       readfrom &= ~(1 << rfd2);
+                                       FD_CLR(rfd2, &readfrom);
                        } else
                                (void)write(2, buf, cc);
                }
                        } else
                                (void)write(2, buf, cc);
                }
-               if (ready & (1 << rem)) {
+               if (FD_ISSET(rem, &ready)) {
                        errno = 0;
 #ifdef KERBEROS
 #endif
                                cc = read(rem, buf, sizeof buf);
                        if (cc <= 0) {
                                if (errno != EWOULDBLOCK)
                        errno = 0;
 #ifdef KERBEROS
 #endif
                                cc = read(rem, buf, sizeof buf);
                        if (cc <= 0) {
                                if (errno != EWOULDBLOCK)
-                                       readfrom &= ~(1 << rem);
+                                       FD_CLR(rem, &readfrom);
                        } else
                                (void)write(1, buf, cc);
                }
                        } else
                                (void)write(1, buf, cc);
                }
-       } while (readfrom);
+       } while (FD_ISSET(rfd2, &readfrom) || FD_ISSET(rem, &readfrom));
 }
 
 void
 }
 
 void
-sendsig(signo)
-       char signo;
+sendsig(sig)
+       int sig;
 {
 {
+       char signo;
+
+       signo = sig;
 #ifdef KERBEROS
 #endif
                (void)write(rfd2, &signo, 1);
 #ifdef KERBEROS
 #endif
                (void)write(rfd2, &signo, 1);
@@ -362,6 +354,7 @@ sendsig(signo)
 
 #ifdef KERBEROS
 /* VARARGS */
 
 #ifdef KERBEROS
 /* VARARGS */
+void
 warning(va_alist)
 va_dcl
 {
 warning(va_alist)
 va_dcl
 {
@@ -381,28 +374,27 @@ char *
 copyargs(argv)
        char **argv;
 {
 copyargs(argv)
        char **argv;
 {
-       register int cc;
-       register char **ap, *p;
-       char *args, *malloc();
+       int cc;
+       char **ap, *args, *p;
 
        cc = 0;
        for (ap = argv; *ap; ++ap)
                cc += strlen(*ap) + 1;
 
        cc = 0;
        for (ap = argv; *ap; ++ap)
                cc += strlen(*ap) + 1;
-       if (!(args = malloc((u_int)cc))) {
-               (void)fprintf(stderr, "rsh: %s.\n", strerror(ENOMEM));
-               exit(1);
-       }
+       if (!(args = malloc((u_int)cc)))
+               err(1, NULL);
        for (p = args, ap = argv; *ap; ++ap) {
                (void)strcpy(p, *ap);
                for (p = strcpy(p, *ap); *p; ++p);
                if (ap[1])
                        *p++ = ' ';
        }
        for (p = args, ap = argv; *ap; ++ap) {
                (void)strcpy(p, *ap);
                for (p = strcpy(p, *ap); *p; ++p);
                if (ap[1])
                        *p++ = ' ';
        }
-       return(args);
+       return (args);
 }
 
 }
 
+void
 usage()
 {
 usage()
 {
+
        (void)fprintf(stderr,
            "usage: rsh [-nd%s]%s[-l login] host [command]\n",
 #ifdef KERBEROS
        (void)fprintf(stderr,
            "usage: rsh [-nd%s]%s[-l login] host [command]\n",
 #ifdef KERBEROS