158f0484fSRodney W. Grimes /* 258f0484fSRodney W. Grimes * Copyright (c) 1983, 1993, 1994 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 758f0484fSRodney W. Grimes * are met: 858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1358f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1458f0484fSRodney W. Grimes * must display the following acknowledgement: 1558f0484fSRodney W. Grimes * This product includes software developed by the University of 1658f0484fSRodney W. Grimes * California, Berkeley and its contributors. 1758f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1858f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1958f0484fSRodney W. Grimes * without specific prior written permission. 2058f0484fSRodney W. Grimes * 2158f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2258f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2358f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2458f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2558f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2658f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2758f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2858f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2958f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3058f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3158f0484fSRodney W. Grimes * SUCH DAMAGE. 323fb85bffSAndrey A. Chernov * 333fb85bffSAndrey A. Chernov * $FreeBSD$ 3458f0484fSRodney W. Grimes */ 3558f0484fSRodney W. Grimes 3658f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3758f0484fSRodney W. Grimes static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 3858f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 3958f0484fSRodney W. Grimes 4058f0484fSRodney W. Grimes #include <sys/param.h> 4158f0484fSRodney W. Grimes #include <sys/socket.h> 4258f0484fSRodney W. Grimes #include <sys/stat.h> 4358f0484fSRodney W. Grimes 4458f0484fSRodney W. Grimes #include <netinet/in.h> 4558f0484fSRodney W. Grimes #include <arpa/inet.h> 4658f0484fSRodney W. Grimes 4758f0484fSRodney W. Grimes #include <signal.h> 4858f0484fSRodney W. Grimes #include <fcntl.h> 4958f0484fSRodney W. Grimes #include <netdb.h> 5058f0484fSRodney W. Grimes #include <unistd.h> 5158f0484fSRodney W. Grimes #include <pwd.h> 5258f0484fSRodney W. Grimes #include <errno.h> 5358f0484fSRodney W. Grimes #include <stdio.h> 5458f0484fSRodney W. Grimes #include <ctype.h> 5558f0484fSRodney W. Grimes #include <string.h> 561e890b05SBill Paul #ifdef YP 571e890b05SBill Paul #include <rpc/rpc.h> 581e890b05SBill Paul #include <rpcsvc/yp_prot.h> 591e890b05SBill Paul #include <rpcsvc/ypclnt.h> 601e890b05SBill Paul #endif 6158f0484fSRodney W. Grimes 6242b4f28eSYoshinobu Inoue /* wrapper for KAME-special getnameinfo() */ 6342b4f28eSYoshinobu Inoue #ifndef NI_WITHSCOPEID 6442b4f28eSYoshinobu Inoue #define NI_WITHSCOPEID 0 6542b4f28eSYoshinobu Inoue #endif 6642b4f28eSYoshinobu Inoue 6751295a4dSJordan K. Hubbard extern int innetgr __P(( const char *, const char *, const char *, const char * )); 6851295a4dSJordan K. Hubbard 69d1f32ba5SGeoff Rehmet #define max(a, b) ((a > b) ? a : b) 70d1f32ba5SGeoff Rehmet 71e0ce825eSDoug Rabson int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *)); 7242b4f28eSYoshinobu Inoue static int __icheckhost __P((void *, char *, int, int)); 7342b4f28eSYoshinobu Inoue 7442b4f28eSYoshinobu Inoue char paddr[INET6_ADDRSTRLEN]; 7558f0484fSRodney W. Grimes 7658f0484fSRodney W. Grimes int 7758f0484fSRodney W. Grimes rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 7858f0484fSRodney W. Grimes char **ahost; 7958f0484fSRodney W. Grimes u_short rport; 8058f0484fSRodney W. Grimes const char *locuser, *remuser, *cmd; 8158f0484fSRodney W. Grimes int *fd2p; 8258f0484fSRodney W. Grimes { 830cac72f4SYoshinobu Inoue return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 840cac72f4SYoshinobu Inoue } 850cac72f4SYoshinobu Inoue 860cac72f4SYoshinobu Inoue int 870cac72f4SYoshinobu Inoue rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 880cac72f4SYoshinobu Inoue char **ahost; 890cac72f4SYoshinobu Inoue u_short rport; 900cac72f4SYoshinobu Inoue const char *locuser, *remuser, *cmd; 910cac72f4SYoshinobu Inoue int *fd2p; 920cac72f4SYoshinobu Inoue int af; 930cac72f4SYoshinobu Inoue { 9442b4f28eSYoshinobu Inoue struct addrinfo hints, *res, *ai; 9542b4f28eSYoshinobu Inoue struct sockaddr_storage from; 9658f0484fSRodney W. Grimes fd_set reads; 9758f0484fSRodney W. Grimes long oldmask; 9858f0484fSRodney W. Grimes pid_t pid; 9942b4f28eSYoshinobu Inoue int s, aport, lport, timo, error; 10058f0484fSRodney W. Grimes char c; 10142b4f28eSYoshinobu Inoue int refused; 10242b4f28eSYoshinobu Inoue char num[8]; 10358f0484fSRodney W. Grimes 10458f0484fSRodney W. Grimes pid = getpid(); 10542b4f28eSYoshinobu Inoue 10642b4f28eSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 10742b4f28eSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 1080cac72f4SYoshinobu Inoue hints.ai_family = af; 10942b4f28eSYoshinobu Inoue hints.ai_socktype = SOCK_STREAM; 11042b4f28eSYoshinobu Inoue hints.ai_protocol = 0; 11142b4f28eSYoshinobu Inoue (void)snprintf(num, sizeof(num), "%d", ntohs(rport)); 11242b4f28eSYoshinobu Inoue error = getaddrinfo(*ahost, num, &hints, &res); 11342b4f28eSYoshinobu Inoue if (error) { 11442b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 11542b4f28eSYoshinobu Inoue gai_strerror(error)); 11642b4f28eSYoshinobu Inoue if (error == EAI_SYSTEM) 11742b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 11842b4f28eSYoshinobu Inoue strerror(errno)); 11958f0484fSRodney W. Grimes return (-1); 12058f0484fSRodney W. Grimes } 12142b4f28eSYoshinobu Inoue if (res->ai_canonname) 12242b4f28eSYoshinobu Inoue *ahost = res->ai_canonname; 12342b4f28eSYoshinobu Inoue ai = res; 12442b4f28eSYoshinobu Inoue refused = 0; 12558f0484fSRodney W. Grimes oldmask = sigblock(sigmask(SIGURG)); 12658f0484fSRodney W. Grimes for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 12742b4f28eSYoshinobu Inoue s = rresvport_af(&lport, ai->ai_family); 12858f0484fSRodney W. Grimes if (s < 0) { 12958f0484fSRodney W. Grimes if (errno == EAGAIN) 13058f0484fSRodney W. Grimes (void)fprintf(stderr, 13158f0484fSRodney W. Grimes "rcmd: socket: All ports in use\n"); 13258f0484fSRodney W. Grimes else 13358f0484fSRodney W. Grimes (void)fprintf(stderr, "rcmd: socket: %s\n", 13458f0484fSRodney W. Grimes strerror(errno)); 13558f0484fSRodney W. Grimes sigsetmask(oldmask); 13642b4f28eSYoshinobu Inoue freeaddrinfo(res); 13758f0484fSRodney W. Grimes return (-1); 13858f0484fSRodney W. Grimes } 13992927338SJason Evans _libc_fcntl(s, F_SETOWN, pid); 14042b4f28eSYoshinobu Inoue if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 14158f0484fSRodney W. Grimes break; 14292927338SJason Evans (void)_libc_close(s); 14358f0484fSRodney W. Grimes if (errno == EADDRINUSE) { 14458f0484fSRodney W. Grimes lport--; 14558f0484fSRodney W. Grimes continue; 14658f0484fSRodney W. Grimes } 14742b4f28eSYoshinobu Inoue if (errno == ECONNREFUSED) 14842b4f28eSYoshinobu Inoue refused = 1; 14942b4f28eSYoshinobu Inoue if (ai->ai_next != NULL) { 15058f0484fSRodney W. Grimes int oerrno = errno; 15158f0484fSRodney W. Grimes 15242b4f28eSYoshinobu Inoue getnameinfo(ai->ai_addr, ai->ai_addrlen, 15342b4f28eSYoshinobu Inoue paddr, sizeof(paddr), 15442b4f28eSYoshinobu Inoue NULL, 0, 15542b4f28eSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 15658f0484fSRodney W. Grimes (void)fprintf(stderr, "connect to address %s: ", 15742b4f28eSYoshinobu Inoue paddr); 15858f0484fSRodney W. Grimes errno = oerrno; 15958f0484fSRodney W. Grimes perror(0); 16042b4f28eSYoshinobu Inoue ai = ai->ai_next; 16142b4f28eSYoshinobu Inoue getnameinfo(ai->ai_addr, ai->ai_addrlen, 16242b4f28eSYoshinobu Inoue paddr, sizeof(paddr), 16342b4f28eSYoshinobu Inoue NULL, 0, 16442b4f28eSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 16542b4f28eSYoshinobu Inoue fprintf(stderr, "Trying %s...\n", paddr); 16658f0484fSRodney W. Grimes continue; 16758f0484fSRodney W. Grimes } 16842b4f28eSYoshinobu Inoue if (refused && timo <= 16) { 16942b4f28eSYoshinobu Inoue (void)_libc_sleep(timo); 17042b4f28eSYoshinobu Inoue timo *= 2; 17142b4f28eSYoshinobu Inoue ai = res; 17242b4f28eSYoshinobu Inoue refused = 0; 17342b4f28eSYoshinobu Inoue continue; 17442b4f28eSYoshinobu Inoue } 17542b4f28eSYoshinobu Inoue freeaddrinfo(res); 17642b4f28eSYoshinobu Inoue (void)fprintf(stderr, "%s: %s\n", *ahost, strerror(errno)); 17758f0484fSRodney W. Grimes sigsetmask(oldmask); 17858f0484fSRodney W. Grimes return (-1); 17958f0484fSRodney W. Grimes } 18058f0484fSRodney W. Grimes lport--; 18158f0484fSRodney W. Grimes if (fd2p == 0) { 18292927338SJason Evans _libc_write(s, "", 1); 18358f0484fSRodney W. Grimes lport = 0; 18458f0484fSRodney W. Grimes } else { 18558f0484fSRodney W. Grimes char num[8]; 18642b4f28eSYoshinobu Inoue int s2 = rresvport_af(&lport, ai->ai_family), s3; 18742b4f28eSYoshinobu Inoue int len = ai->ai_addrlen; 188d1f32ba5SGeoff Rehmet int nfds; 18958f0484fSRodney W. Grimes 19058f0484fSRodney W. Grimes if (s2 < 0) 19158f0484fSRodney W. Grimes goto bad; 19258f0484fSRodney W. Grimes listen(s2, 1); 19358f0484fSRodney W. Grimes (void)snprintf(num, sizeof(num), "%d", lport); 19492927338SJason Evans if (_libc_write(s, num, strlen(num)+1) != strlen(num)+1) { 19558f0484fSRodney W. Grimes (void)fprintf(stderr, 19658f0484fSRodney W. Grimes "rcmd: write (setting up stderr): %s\n", 19758f0484fSRodney W. Grimes strerror(errno)); 19892927338SJason Evans (void)_libc_close(s2); 19958f0484fSRodney W. Grimes goto bad; 20058f0484fSRodney W. Grimes } 201d1f32ba5SGeoff Rehmet nfds = max(s, s2)+1; 202d1f32ba5SGeoff Rehmet if(nfds > FD_SETSIZE) { 203d1f32ba5SGeoff Rehmet fprintf(stderr, "rcmd: too many files\n"); 20492927338SJason Evans (void)_libc_close(s2); 205d1f32ba5SGeoff Rehmet goto bad; 206d1f32ba5SGeoff Rehmet } 207ce70b6caSPeter Wemm again: 20858f0484fSRodney W. Grimes FD_ZERO(&reads); 20958f0484fSRodney W. Grimes FD_SET(s, &reads); 21058f0484fSRodney W. Grimes FD_SET(s2, &reads); 21158f0484fSRodney W. Grimes errno = 0; 212d1f32ba5SGeoff Rehmet if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 21358f0484fSRodney W. Grimes if (errno != 0) 21458f0484fSRodney W. Grimes (void)fprintf(stderr, 21558f0484fSRodney W. Grimes "rcmd: select (setting up stderr): %s\n", 21658f0484fSRodney W. Grimes strerror(errno)); 21758f0484fSRodney W. Grimes else 21858f0484fSRodney W. Grimes (void)fprintf(stderr, 21958f0484fSRodney W. Grimes "select: protocol failure in circuit setup\n"); 22092927338SJason Evans (void)_libc_close(s2); 22158f0484fSRodney W. Grimes goto bad; 22258f0484fSRodney W. Grimes } 22358f0484fSRodney W. Grimes s3 = accept(s2, (struct sockaddr *)&from, &len); 22442b4f28eSYoshinobu Inoue switch (from.ss_family) { 22542b4f28eSYoshinobu Inoue case AF_INET: 22642b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 22742b4f28eSYoshinobu Inoue break; 22842b4f28eSYoshinobu Inoue #ifdef INET6 22942b4f28eSYoshinobu Inoue case AF_INET6: 23042b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 23142b4f28eSYoshinobu Inoue break; 23242b4f28eSYoshinobu Inoue #endif 23342b4f28eSYoshinobu Inoue default: 23442b4f28eSYoshinobu Inoue aport = 0; /* error */ 23542b4f28eSYoshinobu Inoue break; 23642b4f28eSYoshinobu Inoue } 237ce70b6caSPeter Wemm /* 238ce70b6caSPeter Wemm * XXX careful for ftp bounce attacks. If discovered, shut them 239ce70b6caSPeter Wemm * down and check for the real auxiliary channel to connect. 240ce70b6caSPeter Wemm */ 24142b4f28eSYoshinobu Inoue if (aport == 20) { 24292927338SJason Evans _libc_close(s3); 243ce70b6caSPeter Wemm goto again; 244ce70b6caSPeter Wemm } 24592927338SJason Evans (void)_libc_close(s2); 24658f0484fSRodney W. Grimes if (s3 < 0) { 24758f0484fSRodney W. Grimes (void)fprintf(stderr, 24858f0484fSRodney W. Grimes "rcmd: accept: %s\n", strerror(errno)); 24958f0484fSRodney W. Grimes lport = 0; 25058f0484fSRodney W. Grimes goto bad; 25158f0484fSRodney W. Grimes } 25258f0484fSRodney W. Grimes *fd2p = s3; 25342b4f28eSYoshinobu Inoue if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 25458f0484fSRodney W. Grimes (void)fprintf(stderr, 25558f0484fSRodney W. Grimes "socket: protocol failure in circuit setup.\n"); 25658f0484fSRodney W. Grimes goto bad2; 25758f0484fSRodney W. Grimes } 25858f0484fSRodney W. Grimes } 25992927338SJason Evans (void)_libc_write(s, locuser, strlen(locuser)+1); 26092927338SJason Evans (void)_libc_write(s, remuser, strlen(remuser)+1); 26192927338SJason Evans (void)_libc_write(s, cmd, strlen(cmd)+1); 26292927338SJason Evans if (_libc_read(s, &c, 1) != 1) { 26358f0484fSRodney W. Grimes (void)fprintf(stderr, 26458f0484fSRodney W. Grimes "rcmd: %s: %s\n", *ahost, strerror(errno)); 26558f0484fSRodney W. Grimes goto bad2; 26658f0484fSRodney W. Grimes } 26758f0484fSRodney W. Grimes if (c != 0) { 26892927338SJason Evans while (_libc_read(s, &c, 1) == 1) { 26992927338SJason Evans (void)_libc_write(STDERR_FILENO, &c, 1); 27058f0484fSRodney W. Grimes if (c == '\n') 27158f0484fSRodney W. Grimes break; 27258f0484fSRodney W. Grimes } 27358f0484fSRodney W. Grimes goto bad2; 27458f0484fSRodney W. Grimes } 27558f0484fSRodney W. Grimes sigsetmask(oldmask); 27642b4f28eSYoshinobu Inoue freeaddrinfo(res); 27758f0484fSRodney W. Grimes return (s); 27858f0484fSRodney W. Grimes bad2: 27958f0484fSRodney W. Grimes if (lport) 28092927338SJason Evans (void)_libc_close(*fd2p); 28158f0484fSRodney W. Grimes bad: 28292927338SJason Evans (void)_libc_close(s); 28358f0484fSRodney W. Grimes sigsetmask(oldmask); 28442b4f28eSYoshinobu Inoue freeaddrinfo(res); 28558f0484fSRodney W. Grimes return (-1); 28658f0484fSRodney W. Grimes } 28758f0484fSRodney W. Grimes 28858f0484fSRodney W. Grimes int 28942b4f28eSYoshinobu Inoue rresvport(port) 29042b4f28eSYoshinobu Inoue int *port; 29158f0484fSRodney W. Grimes { 29242b4f28eSYoshinobu Inoue return rresvport_af(port, AF_INET); 29342b4f28eSYoshinobu Inoue } 29458f0484fSRodney W. Grimes 29542b4f28eSYoshinobu Inoue int 29642b4f28eSYoshinobu Inoue rresvport_af(alport, family) 29742b4f28eSYoshinobu Inoue int *alport, family; 29842b4f28eSYoshinobu Inoue { 29942b4f28eSYoshinobu Inoue int i, s, len, err; 30042b4f28eSYoshinobu Inoue struct sockaddr_storage ss; 30142b4f28eSYoshinobu Inoue u_short *sport; 30242b4f28eSYoshinobu Inoue 30342b4f28eSYoshinobu Inoue memset(&ss, 0, sizeof(ss)); 30442b4f28eSYoshinobu Inoue ss.ss_family = family; 30542b4f28eSYoshinobu Inoue switch (family) { 30642b4f28eSYoshinobu Inoue case AF_INET: 30742b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 30842b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in *)&ss)->sin_port; 30942b4f28eSYoshinobu Inoue ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 31042b4f28eSYoshinobu Inoue break; 31142b4f28eSYoshinobu Inoue #ifdef INET6 31242b4f28eSYoshinobu Inoue case AF_INET6: 31342b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 31442b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 31542b4f28eSYoshinobu Inoue ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 31642b4f28eSYoshinobu Inoue break; 31742b4f28eSYoshinobu Inoue #endif 31842b4f28eSYoshinobu Inoue default: 31942b4f28eSYoshinobu Inoue errno = EAFNOSUPPORT; 32042b4f28eSYoshinobu Inoue return -1; 32142b4f28eSYoshinobu Inoue } 32242b4f28eSYoshinobu Inoue 32342b4f28eSYoshinobu Inoue s = socket(ss.ss_family, SOCK_STREAM, 0); 32458f0484fSRodney W. Grimes if (s < 0) 32558f0484fSRodney W. Grimes return (-1); 326ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */ 327ce70b6caSPeter Wemm sin.sin_port = htons((u_short)*alport); 328ce70b6caSPeter Wemm if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 32958f0484fSRodney W. Grimes return (s); 330ce70b6caSPeter Wemm if (errno != EADDRINUSE) { 33192927338SJason Evans (void)_libc_close(s); 33258f0484fSRodney W. Grimes return (-1); 33358f0484fSRodney W. Grimes } 334ce70b6caSPeter Wemm #endif 33542b4f28eSYoshinobu Inoue *sport = 0; 33642b4f28eSYoshinobu Inoue if (bindresvport2(s, (struct sockaddr *)&ss, 33742b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len) == -1) { 33892927338SJason Evans (void)_libc_close(s); 339ce70b6caSPeter Wemm return (-1); 340ce70b6caSPeter Wemm } 34142b4f28eSYoshinobu Inoue *alport = (int)ntohs(*sport); 342ce70b6caSPeter Wemm return (s); 343ce70b6caSPeter Wemm } 34458f0484fSRodney W. Grimes 34558f0484fSRodney W. Grimes int __check_rhosts_file = 1; 34658f0484fSRodney W. Grimes char *__rcmd_errstr; 34758f0484fSRodney W. Grimes 34858f0484fSRodney W. Grimes int 34958f0484fSRodney W. Grimes ruserok(rhost, superuser, ruser, luser) 35058f0484fSRodney W. Grimes const char *rhost, *ruser, *luser; 35158f0484fSRodney W. Grimes int superuser; 35258f0484fSRodney W. Grimes { 35342b4f28eSYoshinobu Inoue return ruserok_af(rhost, superuser, ruser, luser, AF_INET); 35458f0484fSRodney W. Grimes } 35542b4f28eSYoshinobu Inoue 35642b4f28eSYoshinobu Inoue int 35742b4f28eSYoshinobu Inoue ruserok_af(rhost, superuser, ruser, luser, af) 35842b4f28eSYoshinobu Inoue const char *rhost, *ruser, *luser; 35942b4f28eSYoshinobu Inoue int superuser, af; 36042b4f28eSYoshinobu Inoue { 36142b4f28eSYoshinobu Inoue struct hostent *hp; 36242b4f28eSYoshinobu Inoue union { 36342b4f28eSYoshinobu Inoue struct in_addr addr_in; 36442b4f28eSYoshinobu Inoue struct in6_addr addr_in6; 36542b4f28eSYoshinobu Inoue } addr; 36642b4f28eSYoshinobu Inoue char **ap; 36742b4f28eSYoshinobu Inoue int ret, h_error; 36842b4f28eSYoshinobu Inoue 36942b4f28eSYoshinobu Inoue if ((hp = getipnodebyname(rhost, af, AI_DEFAULT, &h_error)) == NULL) 37058f0484fSRodney W. Grimes return (-1); 37142b4f28eSYoshinobu Inoue ret = -1; 37242b4f28eSYoshinobu Inoue for (ap = hp->h_addr_list; *ap; ++ap) { 37342b4f28eSYoshinobu Inoue bcopy(*ap, &addr, hp->h_length); 37442b4f28eSYoshinobu Inoue if (iruserok_af(&addr, superuser, ruser, luser, af) == 0) { 37542b4f28eSYoshinobu Inoue ret = 0; 37642b4f28eSYoshinobu Inoue break; 37742b4f28eSYoshinobu Inoue } 37842b4f28eSYoshinobu Inoue } 37942b4f28eSYoshinobu Inoue freehostent(hp); 38042b4f28eSYoshinobu Inoue return (ret); 38158f0484fSRodney W. Grimes } 38258f0484fSRodney W. Grimes 38358f0484fSRodney W. Grimes /* 38458f0484fSRodney W. Grimes * New .rhosts strategy: We are passed an ip address. We spin through 38558f0484fSRodney W. Grimes * hosts.equiv and .rhosts looking for a match. When the .rhosts only 38658f0484fSRodney W. Grimes * has ip addresses, we don't have to trust a nameserver. When it 38758f0484fSRodney W. Grimes * contains hostnames, we spin through the list of addresses the nameserver 38858f0484fSRodney W. Grimes * gives us and look for a match. 38958f0484fSRodney W. Grimes * 39058f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 39158f0484fSRodney W. Grimes */ 39258f0484fSRodney W. Grimes int 39358f0484fSRodney W. Grimes iruserok(raddr, superuser, ruser, luser) 394e0ce825eSDoug Rabson unsigned long raddr; 39558f0484fSRodney W. Grimes int superuser; 39658f0484fSRodney W. Grimes const char *ruser, *luser; 39758f0484fSRodney W. Grimes { 39842b4f28eSYoshinobu Inoue return iruserok_af(&raddr, superuser, ruser, luser, AF_INET); 39942b4f28eSYoshinobu Inoue } 40042b4f28eSYoshinobu Inoue 40142b4f28eSYoshinobu Inoue int 40242b4f28eSYoshinobu Inoue iruserok_af(raddr, superuser, ruser, luser, af) 40342b4f28eSYoshinobu Inoue void *raddr; 40442b4f28eSYoshinobu Inoue int superuser; 40542b4f28eSYoshinobu Inoue const char *ruser, *luser; 40642b4f28eSYoshinobu Inoue int af; 40742b4f28eSYoshinobu Inoue { 40858f0484fSRodney W. Grimes register char *cp; 40958f0484fSRodney W. Grimes struct stat sbuf; 41058f0484fSRodney W. Grimes struct passwd *pwd; 41158f0484fSRodney W. Grimes FILE *hostf; 41258f0484fSRodney W. Grimes uid_t uid; 41358f0484fSRodney W. Grimes int first; 41458f0484fSRodney W. Grimes char pbuf[MAXPATHLEN]; 41542b4f28eSYoshinobu Inoue int len = 0; 41642b4f28eSYoshinobu Inoue 41742b4f28eSYoshinobu Inoue switch (af) { 41842b4f28eSYoshinobu Inoue case AF_INET: 41942b4f28eSYoshinobu Inoue len = sizeof(struct in_addr); 42042b4f28eSYoshinobu Inoue break; 42142b4f28eSYoshinobu Inoue #ifdef INET6 42242b4f28eSYoshinobu Inoue case AF_INET6: 42342b4f28eSYoshinobu Inoue len = sizeof(struct in6_addr); 42442b4f28eSYoshinobu Inoue break; 42542b4f28eSYoshinobu Inoue #endif 42642b4f28eSYoshinobu Inoue } 42758f0484fSRodney W. Grimes 42858f0484fSRodney W. Grimes first = 1; 42958f0484fSRodney W. Grimes hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 43058f0484fSRodney W. Grimes again: 43158f0484fSRodney W. Grimes if (hostf) { 43242b4f28eSYoshinobu Inoue if (__ivaliduser_af(hostf, raddr, luser, ruser, af, len) 43342b4f28eSYoshinobu Inoue == 0) { 43458f0484fSRodney W. Grimes (void)fclose(hostf); 43558f0484fSRodney W. Grimes return (0); 43658f0484fSRodney W. Grimes } 43758f0484fSRodney W. Grimes (void)fclose(hostf); 43858f0484fSRodney W. Grimes } 43958f0484fSRodney W. Grimes if (first == 1 && (__check_rhosts_file || superuser)) { 44058f0484fSRodney W. Grimes first = 0; 44158f0484fSRodney W. Grimes if ((pwd = getpwnam(luser)) == NULL) 44258f0484fSRodney W. Grimes return (-1); 44358f0484fSRodney W. Grimes (void)strcpy(pbuf, pwd->pw_dir); 44458f0484fSRodney W. Grimes (void)strcat(pbuf, "/.rhosts"); 44558f0484fSRodney W. Grimes 44658f0484fSRodney W. Grimes /* 44758f0484fSRodney W. Grimes * Change effective uid while opening .rhosts. If root and 44858f0484fSRodney W. Grimes * reading an NFS mounted file system, can't read files that 44958f0484fSRodney W. Grimes * are protected read/write owner only. 45058f0484fSRodney W. Grimes */ 45158f0484fSRodney W. Grimes uid = geteuid(); 45258f0484fSRodney W. Grimes (void)seteuid(pwd->pw_uid); 45358f0484fSRodney W. Grimes hostf = fopen(pbuf, "r"); 45458f0484fSRodney W. Grimes (void)seteuid(uid); 45558f0484fSRodney W. Grimes 45658f0484fSRodney W. Grimes if (hostf == NULL) 45758f0484fSRodney W. Grimes return (-1); 45858f0484fSRodney W. Grimes /* 45958f0484fSRodney W. Grimes * If not a regular file, or is owned by someone other than 46058f0484fSRodney W. Grimes * user or root or if writeable by anyone but the owner, quit. 46158f0484fSRodney W. Grimes */ 46258f0484fSRodney W. Grimes cp = NULL; 46358f0484fSRodney W. Grimes if (lstat(pbuf, &sbuf) < 0) 46458f0484fSRodney W. Grimes cp = ".rhosts lstat failed"; 46558f0484fSRodney W. Grimes else if (!S_ISREG(sbuf.st_mode)) 46658f0484fSRodney W. Grimes cp = ".rhosts not regular file"; 46758f0484fSRodney W. Grimes else if (fstat(fileno(hostf), &sbuf) < 0) 46858f0484fSRodney W. Grimes cp = ".rhosts fstat failed"; 46958f0484fSRodney W. Grimes else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 47058f0484fSRodney W. Grimes cp = "bad .rhosts owner"; 47158f0484fSRodney W. Grimes else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 47258f0484fSRodney W. Grimes cp = ".rhosts writeable by other than owner"; 47358f0484fSRodney W. Grimes /* If there were any problems, quit. */ 47458f0484fSRodney W. Grimes if (cp) { 47558f0484fSRodney W. Grimes __rcmd_errstr = cp; 47658f0484fSRodney W. Grimes (void)fclose(hostf); 47758f0484fSRodney W. Grimes return (-1); 47858f0484fSRodney W. Grimes } 47958f0484fSRodney W. Grimes goto again; 48058f0484fSRodney W. Grimes } 48158f0484fSRodney W. Grimes return (-1); 48258f0484fSRodney W. Grimes } 48358f0484fSRodney W. Grimes 48458f0484fSRodney W. Grimes /* 48558f0484fSRodney W. Grimes * XXX 48658f0484fSRodney W. Grimes * Don't make static, used by lpd(8). 48758f0484fSRodney W. Grimes * 48858f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 48958f0484fSRodney W. Grimes */ 49058f0484fSRodney W. Grimes int 49158f0484fSRodney W. Grimes __ivaliduser(hostf, raddr, luser, ruser) 49258f0484fSRodney W. Grimes FILE *hostf; 493e0ce825eSDoug Rabson u_int32_t raddr; 49458f0484fSRodney W. Grimes const char *luser, *ruser; 49558f0484fSRodney W. Grimes { 49642b4f28eSYoshinobu Inoue return __ivaliduser_af(hostf, &raddr, luser, ruser, AF_INET, 49742b4f28eSYoshinobu Inoue sizeof(raddr)); 49842b4f28eSYoshinobu Inoue } 49942b4f28eSYoshinobu Inoue 50042b4f28eSYoshinobu Inoue int 50142b4f28eSYoshinobu Inoue __ivaliduser_af(hostf, raddr, luser, ruser, af, len) 50242b4f28eSYoshinobu Inoue FILE *hostf; 50342b4f28eSYoshinobu Inoue void *raddr; 50442b4f28eSYoshinobu Inoue const char *luser, *ruser; 50542b4f28eSYoshinobu Inoue int af, len; 50642b4f28eSYoshinobu Inoue { 50758f0484fSRodney W. Grimes register char *user, *p; 50858f0484fSRodney W. Grimes int ch; 50958f0484fSRodney W. Grimes char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 51097cb5094SBill Paul char hname[MAXHOSTNAMELEN]; 5118538335fSBill Paul struct hostent *hp; 5128538335fSBill Paul /* Presumed guilty until proven innocent. */ 5138538335fSBill Paul int userok = 0, hostok = 0; 51442b4f28eSYoshinobu Inoue int h_error; 5151e890b05SBill Paul #ifdef YP 5161e890b05SBill Paul char *ypdomain; 5178538335fSBill Paul 5181e890b05SBill Paul if (yp_get_default_domain(&ypdomain)) 5191e890b05SBill Paul ypdomain = NULL; 5201e890b05SBill Paul #else 5211e890b05SBill Paul #define ypdomain NULL 5221e890b05SBill Paul #endif 5238538335fSBill Paul /* We need to get the damn hostname back for netgroup matching. */ 52442b4f28eSYoshinobu Inoue if ((hp = getipnodebyaddr((char *)raddr, len, af, &h_error)) == NULL) 5258538335fSBill Paul return (-1); 5262a62f023SWarner Losh strncpy(hname, hp->h_name, sizeof(hname)); 5272a62f023SWarner Losh hname[sizeof(hname) - 1] = '\0'; 52842b4f28eSYoshinobu Inoue freehostent(hp); 52958f0484fSRodney W. Grimes 53058f0484fSRodney W. Grimes while (fgets(buf, sizeof(buf), hostf)) { 53158f0484fSRodney W. Grimes p = buf; 53258f0484fSRodney W. Grimes /* Skip lines that are too long. */ 53358f0484fSRodney W. Grimes if (strchr(p, '\n') == NULL) { 53458f0484fSRodney W. Grimes while ((ch = getc(hostf)) != '\n' && ch != EOF); 53558f0484fSRodney W. Grimes continue; 53658f0484fSRodney W. Grimes } 537acc7e87cSPeter Wemm if (*p == '\n' || *p == '#') { 538acc7e87cSPeter Wemm /* comment... */ 539acc7e87cSPeter Wemm continue; 540acc7e87cSPeter Wemm } 54158f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 5423fb85bffSAndrey A. Chernov *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 54358f0484fSRodney W. Grimes p++; 54458f0484fSRodney W. Grimes } 54558f0484fSRodney W. Grimes if (*p == ' ' || *p == '\t') { 54658f0484fSRodney W. Grimes *p++ = '\0'; 54758f0484fSRodney W. Grimes while (*p == ' ' || *p == '\t') 54858f0484fSRodney W. Grimes p++; 54958f0484fSRodney W. Grimes user = p; 55058f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && 55158f0484fSRodney W. Grimes *p != '\t' && *p != '\0') 55258f0484fSRodney W. Grimes p++; 55358f0484fSRodney W. Grimes } else 55458f0484fSRodney W. Grimes user = p; 55558f0484fSRodney W. Grimes *p = '\0'; 5568538335fSBill Paul /* 5578538335fSBill Paul * Do +/- and +@/-@ checking. This looks really nasty, 5588538335fSBill Paul * but it matches SunOS's behavior so far as I can tell. 5598538335fSBill Paul */ 5608538335fSBill Paul switch(buf[0]) { 5618538335fSBill Paul case '+': 5628538335fSBill Paul if (!buf[1]) { /* '+' matches all hosts */ 5638538335fSBill Paul hostok = 1; 5648538335fSBill Paul break; 56558f0484fSRodney W. Grimes } 5668538335fSBill Paul if (buf[1] == '@') /* match a host by netgroup */ 56797cb5094SBill Paul hostok = innetgr((char *)&buf[2], 56897cb5094SBill Paul (char *)&hname, NULL, ypdomain); 5698538335fSBill Paul else /* match a host by addr */ 57042b4f28eSYoshinobu Inoue hostok = __icheckhost(raddr,(char *)&buf[1], 57142b4f28eSYoshinobu Inoue af, len); 5728538335fSBill Paul break; 5738538335fSBill Paul case '-': /* reject '-' hosts and all their users */ 5748538335fSBill Paul if (buf[1] == '@') { 5758538335fSBill Paul if (innetgr((char *)&buf[2], 57697cb5094SBill Paul (char *)&hname, NULL, ypdomain)) 5778538335fSBill Paul return(-1); 5788538335fSBill Paul } else { 57942b4f28eSYoshinobu Inoue if (__icheckhost(raddr,(char *)&buf[1],af,len)) 5808538335fSBill Paul return(-1); 5818538335fSBill Paul } 5828538335fSBill Paul break; 5838538335fSBill Paul default: /* if no '+' or '-', do a simple match */ 58442b4f28eSYoshinobu Inoue hostok = __icheckhost(raddr, buf, af, len); 5858538335fSBill Paul break; 5868538335fSBill Paul } 5878538335fSBill Paul switch(*user) { 5888538335fSBill Paul case '+': 5898538335fSBill Paul if (!*(user+1)) { /* '+' matches all users */ 5908538335fSBill Paul userok = 1; 5918538335fSBill Paul break; 5928538335fSBill Paul } 5938538335fSBill Paul if (*(user+1) == '@') /* match a user by netgroup */ 5941e890b05SBill Paul userok = innetgr(user+2, NULL, ruser, ypdomain); 5958538335fSBill Paul else /* match a user by direct specification */ 5968538335fSBill Paul userok = !(strcmp(ruser, user+1)); 5978538335fSBill Paul break; 5988538335fSBill Paul case '-': /* if we matched a hostname, */ 5998538335fSBill Paul if (hostok) { /* check for user field rejections */ 6008538335fSBill Paul if (!*(user+1)) 6018538335fSBill Paul return(-1); 6028538335fSBill Paul if (*(user+1) == '@') { 6038538335fSBill Paul if (innetgr(user+2, NULL, 6041e890b05SBill Paul ruser, ypdomain)) 6058538335fSBill Paul return(-1); 6068538335fSBill Paul } else { 6078538335fSBill Paul if (!strcmp(ruser, user+1)) 6088538335fSBill Paul return(-1); 6098538335fSBill Paul } 6108538335fSBill Paul } 6118538335fSBill Paul break; 6128538335fSBill Paul default: /* no rejections: try to match the user */ 6138538335fSBill Paul if (hostok) 6148538335fSBill Paul userok = !(strcmp(ruser,*user ? user : luser)); 6158538335fSBill Paul break; 6168538335fSBill Paul } 6178538335fSBill Paul if (hostok && userok) 6188538335fSBill Paul return(0); 61958f0484fSRodney W. Grimes } 62058f0484fSRodney W. Grimes return (-1); 62158f0484fSRodney W. Grimes } 62258f0484fSRodney W. Grimes 62358f0484fSRodney W. Grimes /* 62458f0484fSRodney W. Grimes * Returns "true" if match, 0 if no match. 62558f0484fSRodney W. Grimes */ 62658f0484fSRodney W. Grimes static int 62742b4f28eSYoshinobu Inoue __icheckhost(raddr, lhost, af, len) 62842b4f28eSYoshinobu Inoue void *raddr; 62958f0484fSRodney W. Grimes register char *lhost; 63042b4f28eSYoshinobu Inoue int af, len; 63158f0484fSRodney W. Grimes { 63258f0484fSRodney W. Grimes register struct hostent *hp; 63342b4f28eSYoshinobu Inoue char laddr[BUFSIZ]; /* xxx */ 63458f0484fSRodney W. Grimes register char **pp; 63542b4f28eSYoshinobu Inoue int h_error; 63642b4f28eSYoshinobu Inoue int match; 63758f0484fSRodney W. Grimes 63858f0484fSRodney W. Grimes /* Try for raw ip address first. */ 63942b4f28eSYoshinobu Inoue if (inet_pton(af, lhost, laddr) == 1) { 64042b4f28eSYoshinobu Inoue if (memcmp(raddr, laddr, len) == 0) 64142b4f28eSYoshinobu Inoue return (1); 64242b4f28eSYoshinobu Inoue else 64342b4f28eSYoshinobu Inoue return (0); 64442b4f28eSYoshinobu Inoue } 64558f0484fSRodney W. Grimes 64658f0484fSRodney W. Grimes /* Better be a hostname. */ 64742b4f28eSYoshinobu Inoue if ((hp = getipnodebyname(lhost, af, AI_DEFAULT, &h_error)) == NULL) 64858f0484fSRodney W. Grimes return (0); 64958f0484fSRodney W. Grimes 65058f0484fSRodney W. Grimes /* Spin through ip addresses. */ 65142b4f28eSYoshinobu Inoue match = 0; 65258f0484fSRodney W. Grimes for (pp = hp->h_addr_list; *pp; ++pp) 65342b4f28eSYoshinobu Inoue if (!bcmp(raddr, *pp, len)) { 65442b4f28eSYoshinobu Inoue match = 1; 65542b4f28eSYoshinobu Inoue break; 65642b4f28eSYoshinobu Inoue } 65758f0484fSRodney W. Grimes 65842b4f28eSYoshinobu Inoue freehostent(hp); 65942b4f28eSYoshinobu Inoue return (match); 66058f0484fSRodney W. Grimes } 661