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 } 1399233c4d9SJason Evans _fcntl(s, F_SETOWN, pid); 14042b4f28eSYoshinobu Inoue if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 14158f0484fSRodney W. Grimes break; 1429233c4d9SJason Evans (void)_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) { 1699233c4d9SJason Evans struct timespec time_to_sleep, time_remaining; 1709233c4d9SJason Evans 1719233c4d9SJason Evans time_to_sleep.tv_sec = timo; 1729233c4d9SJason Evans time_to_sleep.tv_nsec = 0; 1739233c4d9SJason Evans (void)_nanosleep(&time_to_sleep, &time_remaining); 1749233c4d9SJason Evans 17542b4f28eSYoshinobu Inoue timo *= 2; 17642b4f28eSYoshinobu Inoue ai = res; 17742b4f28eSYoshinobu Inoue refused = 0; 17842b4f28eSYoshinobu Inoue continue; 17942b4f28eSYoshinobu Inoue } 18042b4f28eSYoshinobu Inoue freeaddrinfo(res); 18142b4f28eSYoshinobu Inoue (void)fprintf(stderr, "%s: %s\n", *ahost, strerror(errno)); 18258f0484fSRodney W. Grimes sigsetmask(oldmask); 18358f0484fSRodney W. Grimes return (-1); 18458f0484fSRodney W. Grimes } 18558f0484fSRodney W. Grimes lport--; 18658f0484fSRodney W. Grimes if (fd2p == 0) { 1879233c4d9SJason Evans _write(s, "", 1); 18858f0484fSRodney W. Grimes lport = 0; 18958f0484fSRodney W. Grimes } else { 19058f0484fSRodney W. Grimes char num[8]; 19142b4f28eSYoshinobu Inoue int s2 = rresvport_af(&lport, ai->ai_family), s3; 19242b4f28eSYoshinobu Inoue int len = ai->ai_addrlen; 193d1f32ba5SGeoff Rehmet int nfds; 19458f0484fSRodney W. Grimes 19558f0484fSRodney W. Grimes if (s2 < 0) 19658f0484fSRodney W. Grimes goto bad; 19758f0484fSRodney W. Grimes listen(s2, 1); 19858f0484fSRodney W. Grimes (void)snprintf(num, sizeof(num), "%d", lport); 1999233c4d9SJason Evans if (_write(s, num, strlen(num)+1) != strlen(num)+1) { 20058f0484fSRodney W. Grimes (void)fprintf(stderr, 20158f0484fSRodney W. Grimes "rcmd: write (setting up stderr): %s\n", 20258f0484fSRodney W. Grimes strerror(errno)); 2039233c4d9SJason Evans (void)_close(s2); 20458f0484fSRodney W. Grimes goto bad; 20558f0484fSRodney W. Grimes } 206d1f32ba5SGeoff Rehmet nfds = max(s, s2)+1; 207d1f32ba5SGeoff Rehmet if(nfds > FD_SETSIZE) { 208d1f32ba5SGeoff Rehmet fprintf(stderr, "rcmd: too many files\n"); 2099233c4d9SJason Evans (void)_close(s2); 210d1f32ba5SGeoff Rehmet goto bad; 211d1f32ba5SGeoff Rehmet } 212ce70b6caSPeter Wemm again: 21358f0484fSRodney W. Grimes FD_ZERO(&reads); 21458f0484fSRodney W. Grimes FD_SET(s, &reads); 21558f0484fSRodney W. Grimes FD_SET(s2, &reads); 21658f0484fSRodney W. Grimes errno = 0; 217d1f32ba5SGeoff Rehmet if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 21858f0484fSRodney W. Grimes if (errno != 0) 21958f0484fSRodney W. Grimes (void)fprintf(stderr, 22058f0484fSRodney W. Grimes "rcmd: select (setting up stderr): %s\n", 22158f0484fSRodney W. Grimes strerror(errno)); 22258f0484fSRodney W. Grimes else 22358f0484fSRodney W. Grimes (void)fprintf(stderr, 22458f0484fSRodney W. Grimes "select: protocol failure in circuit setup\n"); 2259233c4d9SJason Evans (void)_close(s2); 22658f0484fSRodney W. Grimes goto bad; 22758f0484fSRodney W. Grimes } 22858f0484fSRodney W. Grimes s3 = accept(s2, (struct sockaddr *)&from, &len); 22942b4f28eSYoshinobu Inoue switch (from.ss_family) { 23042b4f28eSYoshinobu Inoue case AF_INET: 23142b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 23242b4f28eSYoshinobu Inoue break; 23342b4f28eSYoshinobu Inoue #ifdef INET6 23442b4f28eSYoshinobu Inoue case AF_INET6: 23542b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 23642b4f28eSYoshinobu Inoue break; 23742b4f28eSYoshinobu Inoue #endif 23842b4f28eSYoshinobu Inoue default: 23942b4f28eSYoshinobu Inoue aport = 0; /* error */ 24042b4f28eSYoshinobu Inoue break; 24142b4f28eSYoshinobu Inoue } 242ce70b6caSPeter Wemm /* 243ce70b6caSPeter Wemm * XXX careful for ftp bounce attacks. If discovered, shut them 244ce70b6caSPeter Wemm * down and check for the real auxiliary channel to connect. 245ce70b6caSPeter Wemm */ 24642b4f28eSYoshinobu Inoue if (aport == 20) { 2479233c4d9SJason Evans _close(s3); 248ce70b6caSPeter Wemm goto again; 249ce70b6caSPeter Wemm } 2509233c4d9SJason Evans (void)_close(s2); 25158f0484fSRodney W. Grimes if (s3 < 0) { 25258f0484fSRodney W. Grimes (void)fprintf(stderr, 25358f0484fSRodney W. Grimes "rcmd: accept: %s\n", strerror(errno)); 25458f0484fSRodney W. Grimes lport = 0; 25558f0484fSRodney W. Grimes goto bad; 25658f0484fSRodney W. Grimes } 25758f0484fSRodney W. Grimes *fd2p = s3; 25842b4f28eSYoshinobu Inoue if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 25958f0484fSRodney W. Grimes (void)fprintf(stderr, 26058f0484fSRodney W. Grimes "socket: protocol failure in circuit setup.\n"); 26158f0484fSRodney W. Grimes goto bad2; 26258f0484fSRodney W. Grimes } 26358f0484fSRodney W. Grimes } 2649233c4d9SJason Evans (void)_write(s, locuser, strlen(locuser)+1); 2659233c4d9SJason Evans (void)_write(s, remuser, strlen(remuser)+1); 2669233c4d9SJason Evans (void)_write(s, cmd, strlen(cmd)+1); 2679233c4d9SJason Evans if (_read(s, &c, 1) != 1) { 26858f0484fSRodney W. Grimes (void)fprintf(stderr, 26958f0484fSRodney W. Grimes "rcmd: %s: %s\n", *ahost, strerror(errno)); 27058f0484fSRodney W. Grimes goto bad2; 27158f0484fSRodney W. Grimes } 27258f0484fSRodney W. Grimes if (c != 0) { 2739233c4d9SJason Evans while (_read(s, &c, 1) == 1) { 2749233c4d9SJason Evans (void)_write(STDERR_FILENO, &c, 1); 27558f0484fSRodney W. Grimes if (c == '\n') 27658f0484fSRodney W. Grimes break; 27758f0484fSRodney W. Grimes } 27858f0484fSRodney W. Grimes goto bad2; 27958f0484fSRodney W. Grimes } 28058f0484fSRodney W. Grimes sigsetmask(oldmask); 28142b4f28eSYoshinobu Inoue freeaddrinfo(res); 28258f0484fSRodney W. Grimes return (s); 28358f0484fSRodney W. Grimes bad2: 28458f0484fSRodney W. Grimes if (lport) 2859233c4d9SJason Evans (void)_close(*fd2p); 28658f0484fSRodney W. Grimes bad: 2879233c4d9SJason Evans (void)_close(s); 28858f0484fSRodney W. Grimes sigsetmask(oldmask); 28942b4f28eSYoshinobu Inoue freeaddrinfo(res); 29058f0484fSRodney W. Grimes return (-1); 29158f0484fSRodney W. Grimes } 29258f0484fSRodney W. Grimes 29358f0484fSRodney W. Grimes int 29442b4f28eSYoshinobu Inoue rresvport(port) 29542b4f28eSYoshinobu Inoue int *port; 29658f0484fSRodney W. Grimes { 29742b4f28eSYoshinobu Inoue return rresvport_af(port, AF_INET); 29842b4f28eSYoshinobu Inoue } 29958f0484fSRodney W. Grimes 30042b4f28eSYoshinobu Inoue int 30142b4f28eSYoshinobu Inoue rresvport_af(alport, family) 30242b4f28eSYoshinobu Inoue int *alport, family; 30342b4f28eSYoshinobu Inoue { 30442b4f28eSYoshinobu Inoue int i, s, len, err; 30542b4f28eSYoshinobu Inoue struct sockaddr_storage ss; 30642b4f28eSYoshinobu Inoue u_short *sport; 30742b4f28eSYoshinobu Inoue 30842b4f28eSYoshinobu Inoue memset(&ss, 0, sizeof(ss)); 30942b4f28eSYoshinobu Inoue ss.ss_family = family; 31042b4f28eSYoshinobu Inoue switch (family) { 31142b4f28eSYoshinobu Inoue case AF_INET: 31242b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 31342b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in *)&ss)->sin_port; 31442b4f28eSYoshinobu Inoue ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 31542b4f28eSYoshinobu Inoue break; 31642b4f28eSYoshinobu Inoue #ifdef INET6 31742b4f28eSYoshinobu Inoue case AF_INET6: 31842b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 31942b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 32042b4f28eSYoshinobu Inoue ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 32142b4f28eSYoshinobu Inoue break; 32242b4f28eSYoshinobu Inoue #endif 32342b4f28eSYoshinobu Inoue default: 32442b4f28eSYoshinobu Inoue errno = EAFNOSUPPORT; 32542b4f28eSYoshinobu Inoue return -1; 32642b4f28eSYoshinobu Inoue } 32742b4f28eSYoshinobu Inoue 32842b4f28eSYoshinobu Inoue s = socket(ss.ss_family, SOCK_STREAM, 0); 32958f0484fSRodney W. Grimes if (s < 0) 33058f0484fSRodney W. Grimes return (-1); 331ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */ 332ce70b6caSPeter Wemm sin.sin_port = htons((u_short)*alport); 333ce70b6caSPeter Wemm if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 33458f0484fSRodney W. Grimes return (s); 335ce70b6caSPeter Wemm if (errno != EADDRINUSE) { 3369233c4d9SJason Evans (void)_close(s); 33758f0484fSRodney W. Grimes return (-1); 33858f0484fSRodney W. Grimes } 339ce70b6caSPeter Wemm #endif 34042b4f28eSYoshinobu Inoue *sport = 0; 341ae42b666SYoshinobu Inoue if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { 3429233c4d9SJason Evans (void)_close(s); 343ce70b6caSPeter Wemm return (-1); 344ce70b6caSPeter Wemm } 34542b4f28eSYoshinobu Inoue *alport = (int)ntohs(*sport); 346ce70b6caSPeter Wemm return (s); 347ce70b6caSPeter Wemm } 34858f0484fSRodney W. Grimes 34958f0484fSRodney W. Grimes int __check_rhosts_file = 1; 35058f0484fSRodney W. Grimes char *__rcmd_errstr; 35158f0484fSRodney W. Grimes 35258f0484fSRodney W. Grimes int 35358f0484fSRodney W. Grimes ruserok(rhost, superuser, ruser, luser) 35458f0484fSRodney W. Grimes const char *rhost, *ruser, *luser; 35558f0484fSRodney W. Grimes int superuser; 35658f0484fSRodney W. Grimes { 35742b4f28eSYoshinobu Inoue return ruserok_af(rhost, superuser, ruser, luser, AF_INET); 35858f0484fSRodney W. Grimes } 35942b4f28eSYoshinobu Inoue 36042b4f28eSYoshinobu Inoue int 36142b4f28eSYoshinobu Inoue ruserok_af(rhost, superuser, ruser, luser, af) 36242b4f28eSYoshinobu Inoue const char *rhost, *ruser, *luser; 36342b4f28eSYoshinobu Inoue int superuser, af; 36442b4f28eSYoshinobu Inoue { 36542b4f28eSYoshinobu Inoue struct hostent *hp; 36642b4f28eSYoshinobu Inoue union { 36742b4f28eSYoshinobu Inoue struct in_addr addr_in; 36842b4f28eSYoshinobu Inoue struct in6_addr addr_in6; 36942b4f28eSYoshinobu Inoue } addr; 37042b4f28eSYoshinobu Inoue char **ap; 37142b4f28eSYoshinobu Inoue int ret, h_error; 37242b4f28eSYoshinobu Inoue 37342b4f28eSYoshinobu Inoue if ((hp = getipnodebyname(rhost, af, AI_DEFAULT, &h_error)) == NULL) 37458f0484fSRodney W. Grimes return (-1); 37542b4f28eSYoshinobu Inoue ret = -1; 37642b4f28eSYoshinobu Inoue for (ap = hp->h_addr_list; *ap; ++ap) { 37742b4f28eSYoshinobu Inoue bcopy(*ap, &addr, hp->h_length); 37842b4f28eSYoshinobu Inoue if (iruserok_af(&addr, superuser, ruser, luser, af) == 0) { 37942b4f28eSYoshinobu Inoue ret = 0; 38042b4f28eSYoshinobu Inoue break; 38142b4f28eSYoshinobu Inoue } 38242b4f28eSYoshinobu Inoue } 38342b4f28eSYoshinobu Inoue freehostent(hp); 38442b4f28eSYoshinobu Inoue return (ret); 38558f0484fSRodney W. Grimes } 38658f0484fSRodney W. Grimes 38758f0484fSRodney W. Grimes /* 38858f0484fSRodney W. Grimes * New .rhosts strategy: We are passed an ip address. We spin through 38958f0484fSRodney W. Grimes * hosts.equiv and .rhosts looking for a match. When the .rhosts only 39058f0484fSRodney W. Grimes * has ip addresses, we don't have to trust a nameserver. When it 39158f0484fSRodney W. Grimes * contains hostnames, we spin through the list of addresses the nameserver 39258f0484fSRodney W. Grimes * gives us and look for a match. 39358f0484fSRodney W. Grimes * 39458f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 39558f0484fSRodney W. Grimes */ 39658f0484fSRodney W. Grimes int 39758f0484fSRodney W. Grimes iruserok(raddr, superuser, ruser, luser) 398e0ce825eSDoug Rabson unsigned long raddr; 39958f0484fSRodney W. Grimes int superuser; 40058f0484fSRodney W. Grimes const char *ruser, *luser; 40158f0484fSRodney W. Grimes { 40242b4f28eSYoshinobu Inoue return iruserok_af(&raddr, superuser, ruser, luser, AF_INET); 40342b4f28eSYoshinobu Inoue } 40442b4f28eSYoshinobu Inoue 40542b4f28eSYoshinobu Inoue int 40642b4f28eSYoshinobu Inoue iruserok_af(raddr, superuser, ruser, luser, af) 40742b4f28eSYoshinobu Inoue void *raddr; 40842b4f28eSYoshinobu Inoue int superuser; 40942b4f28eSYoshinobu Inoue const char *ruser, *luser; 41042b4f28eSYoshinobu Inoue int af; 41142b4f28eSYoshinobu Inoue { 41258f0484fSRodney W. Grimes register char *cp; 41358f0484fSRodney W. Grimes struct stat sbuf; 41458f0484fSRodney W. Grimes struct passwd *pwd; 41558f0484fSRodney W. Grimes FILE *hostf; 41658f0484fSRodney W. Grimes uid_t uid; 41758f0484fSRodney W. Grimes int first; 41858f0484fSRodney W. Grimes char pbuf[MAXPATHLEN]; 41942b4f28eSYoshinobu Inoue int len = 0; 42042b4f28eSYoshinobu Inoue 42142b4f28eSYoshinobu Inoue switch (af) { 42242b4f28eSYoshinobu Inoue case AF_INET: 42342b4f28eSYoshinobu Inoue len = sizeof(struct in_addr); 42442b4f28eSYoshinobu Inoue break; 42542b4f28eSYoshinobu Inoue #ifdef INET6 42642b4f28eSYoshinobu Inoue case AF_INET6: 42742b4f28eSYoshinobu Inoue len = sizeof(struct in6_addr); 42842b4f28eSYoshinobu Inoue break; 42942b4f28eSYoshinobu Inoue #endif 43042b4f28eSYoshinobu Inoue } 43158f0484fSRodney W. Grimes 43258f0484fSRodney W. Grimes first = 1; 43358f0484fSRodney W. Grimes hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 43458f0484fSRodney W. Grimes again: 43558f0484fSRodney W. Grimes if (hostf) { 43642b4f28eSYoshinobu Inoue if (__ivaliduser_af(hostf, raddr, luser, ruser, af, len) 43742b4f28eSYoshinobu Inoue == 0) { 43858f0484fSRodney W. Grimes (void)fclose(hostf); 43958f0484fSRodney W. Grimes return (0); 44058f0484fSRodney W. Grimes } 44158f0484fSRodney W. Grimes (void)fclose(hostf); 44258f0484fSRodney W. Grimes } 44358f0484fSRodney W. Grimes if (first == 1 && (__check_rhosts_file || superuser)) { 44458f0484fSRodney W. Grimes first = 0; 44558f0484fSRodney W. Grimes if ((pwd = getpwnam(luser)) == NULL) 44658f0484fSRodney W. Grimes return (-1); 44758f0484fSRodney W. Grimes (void)strcpy(pbuf, pwd->pw_dir); 44858f0484fSRodney W. Grimes (void)strcat(pbuf, "/.rhosts"); 44958f0484fSRodney W. Grimes 45058f0484fSRodney W. Grimes /* 45158f0484fSRodney W. Grimes * Change effective uid while opening .rhosts. If root and 45258f0484fSRodney W. Grimes * reading an NFS mounted file system, can't read files that 45358f0484fSRodney W. Grimes * are protected read/write owner only. 45458f0484fSRodney W. Grimes */ 45558f0484fSRodney W. Grimes uid = geteuid(); 45658f0484fSRodney W. Grimes (void)seteuid(pwd->pw_uid); 45758f0484fSRodney W. Grimes hostf = fopen(pbuf, "r"); 45858f0484fSRodney W. Grimes (void)seteuid(uid); 45958f0484fSRodney W. Grimes 46058f0484fSRodney W. Grimes if (hostf == NULL) 46158f0484fSRodney W. Grimes return (-1); 46258f0484fSRodney W. Grimes /* 46358f0484fSRodney W. Grimes * If not a regular file, or is owned by someone other than 46458f0484fSRodney W. Grimes * user or root or if writeable by anyone but the owner, quit. 46558f0484fSRodney W. Grimes */ 46658f0484fSRodney W. Grimes cp = NULL; 46758f0484fSRodney W. Grimes if (lstat(pbuf, &sbuf) < 0) 46858f0484fSRodney W. Grimes cp = ".rhosts lstat failed"; 46958f0484fSRodney W. Grimes else if (!S_ISREG(sbuf.st_mode)) 47058f0484fSRodney W. Grimes cp = ".rhosts not regular file"; 47158f0484fSRodney W. Grimes else if (fstat(fileno(hostf), &sbuf) < 0) 47258f0484fSRodney W. Grimes cp = ".rhosts fstat failed"; 47358f0484fSRodney W. Grimes else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 47458f0484fSRodney W. Grimes cp = "bad .rhosts owner"; 47558f0484fSRodney W. Grimes else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 47658f0484fSRodney W. Grimes cp = ".rhosts writeable by other than owner"; 47758f0484fSRodney W. Grimes /* If there were any problems, quit. */ 47858f0484fSRodney W. Grimes if (cp) { 47958f0484fSRodney W. Grimes __rcmd_errstr = cp; 48058f0484fSRodney W. Grimes (void)fclose(hostf); 48158f0484fSRodney W. Grimes return (-1); 48258f0484fSRodney W. Grimes } 48358f0484fSRodney W. Grimes goto again; 48458f0484fSRodney W. Grimes } 48558f0484fSRodney W. Grimes return (-1); 48658f0484fSRodney W. Grimes } 48758f0484fSRodney W. Grimes 48858f0484fSRodney W. Grimes /* 48958f0484fSRodney W. Grimes * XXX 49058f0484fSRodney W. Grimes * Don't make static, used by lpd(8). 49158f0484fSRodney W. Grimes * 49258f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 49358f0484fSRodney W. Grimes */ 49458f0484fSRodney W. Grimes int 49558f0484fSRodney W. Grimes __ivaliduser(hostf, raddr, luser, ruser) 49658f0484fSRodney W. Grimes FILE *hostf; 497e0ce825eSDoug Rabson u_int32_t raddr; 49858f0484fSRodney W. Grimes const char *luser, *ruser; 49958f0484fSRodney W. Grimes { 50042b4f28eSYoshinobu Inoue return __ivaliduser_af(hostf, &raddr, luser, ruser, AF_INET, 50142b4f28eSYoshinobu Inoue sizeof(raddr)); 50242b4f28eSYoshinobu Inoue } 50342b4f28eSYoshinobu Inoue 50442b4f28eSYoshinobu Inoue int 50542b4f28eSYoshinobu Inoue __ivaliduser_af(hostf, raddr, luser, ruser, af, len) 50642b4f28eSYoshinobu Inoue FILE *hostf; 50742b4f28eSYoshinobu Inoue void *raddr; 50842b4f28eSYoshinobu Inoue const char *luser, *ruser; 50942b4f28eSYoshinobu Inoue int af, len; 51042b4f28eSYoshinobu Inoue { 51158f0484fSRodney W. Grimes register char *user, *p; 51258f0484fSRodney W. Grimes int ch; 51358f0484fSRodney W. Grimes char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 51497cb5094SBill Paul char hname[MAXHOSTNAMELEN]; 5158538335fSBill Paul struct hostent *hp; 5168538335fSBill Paul /* Presumed guilty until proven innocent. */ 5178538335fSBill Paul int userok = 0, hostok = 0; 51842b4f28eSYoshinobu Inoue int h_error; 5191e890b05SBill Paul #ifdef YP 5201e890b05SBill Paul char *ypdomain; 5218538335fSBill Paul 5221e890b05SBill Paul if (yp_get_default_domain(&ypdomain)) 5231e890b05SBill Paul ypdomain = NULL; 5241e890b05SBill Paul #else 5251e890b05SBill Paul #define ypdomain NULL 5261e890b05SBill Paul #endif 5278538335fSBill Paul /* We need to get the damn hostname back for netgroup matching. */ 52842b4f28eSYoshinobu Inoue if ((hp = getipnodebyaddr((char *)raddr, len, af, &h_error)) == NULL) 5298538335fSBill Paul return (-1); 5302a62f023SWarner Losh strncpy(hname, hp->h_name, sizeof(hname)); 5312a62f023SWarner Losh hname[sizeof(hname) - 1] = '\0'; 53242b4f28eSYoshinobu Inoue freehostent(hp); 53358f0484fSRodney W. Grimes 53458f0484fSRodney W. Grimes while (fgets(buf, sizeof(buf), hostf)) { 53558f0484fSRodney W. Grimes p = buf; 53658f0484fSRodney W. Grimes /* Skip lines that are too long. */ 53758f0484fSRodney W. Grimes if (strchr(p, '\n') == NULL) { 53858f0484fSRodney W. Grimes while ((ch = getc(hostf)) != '\n' && ch != EOF); 53958f0484fSRodney W. Grimes continue; 54058f0484fSRodney W. Grimes } 541acc7e87cSPeter Wemm if (*p == '\n' || *p == '#') { 542acc7e87cSPeter Wemm /* comment... */ 543acc7e87cSPeter Wemm continue; 544acc7e87cSPeter Wemm } 54558f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 5463fb85bffSAndrey A. Chernov *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 54758f0484fSRodney W. Grimes p++; 54858f0484fSRodney W. Grimes } 54958f0484fSRodney W. Grimes if (*p == ' ' || *p == '\t') { 55058f0484fSRodney W. Grimes *p++ = '\0'; 55158f0484fSRodney W. Grimes while (*p == ' ' || *p == '\t') 55258f0484fSRodney W. Grimes p++; 55358f0484fSRodney W. Grimes user = p; 55458f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && 55558f0484fSRodney W. Grimes *p != '\t' && *p != '\0') 55658f0484fSRodney W. Grimes p++; 55758f0484fSRodney W. Grimes } else 55858f0484fSRodney W. Grimes user = p; 55958f0484fSRodney W. Grimes *p = '\0'; 5608538335fSBill Paul /* 5618538335fSBill Paul * Do +/- and +@/-@ checking. This looks really nasty, 5628538335fSBill Paul * but it matches SunOS's behavior so far as I can tell. 5638538335fSBill Paul */ 5648538335fSBill Paul switch(buf[0]) { 5658538335fSBill Paul case '+': 5668538335fSBill Paul if (!buf[1]) { /* '+' matches all hosts */ 5678538335fSBill Paul hostok = 1; 5688538335fSBill Paul break; 56958f0484fSRodney W. Grimes } 5708538335fSBill Paul if (buf[1] == '@') /* match a host by netgroup */ 57197cb5094SBill Paul hostok = innetgr((char *)&buf[2], 57297cb5094SBill Paul (char *)&hname, NULL, ypdomain); 5738538335fSBill Paul else /* match a host by addr */ 57442b4f28eSYoshinobu Inoue hostok = __icheckhost(raddr,(char *)&buf[1], 57542b4f28eSYoshinobu Inoue af, len); 5768538335fSBill Paul break; 5778538335fSBill Paul case '-': /* reject '-' hosts and all their users */ 5788538335fSBill Paul if (buf[1] == '@') { 5798538335fSBill Paul if (innetgr((char *)&buf[2], 58097cb5094SBill Paul (char *)&hname, NULL, ypdomain)) 5818538335fSBill Paul return(-1); 5828538335fSBill Paul } else { 58342b4f28eSYoshinobu Inoue if (__icheckhost(raddr,(char *)&buf[1],af,len)) 5848538335fSBill Paul return(-1); 5858538335fSBill Paul } 5868538335fSBill Paul break; 5878538335fSBill Paul default: /* if no '+' or '-', do a simple match */ 58842b4f28eSYoshinobu Inoue hostok = __icheckhost(raddr, buf, af, len); 5898538335fSBill Paul break; 5908538335fSBill Paul } 5918538335fSBill Paul switch(*user) { 5928538335fSBill Paul case '+': 5938538335fSBill Paul if (!*(user+1)) { /* '+' matches all users */ 5948538335fSBill Paul userok = 1; 5958538335fSBill Paul break; 5968538335fSBill Paul } 5978538335fSBill Paul if (*(user+1) == '@') /* match a user by netgroup */ 5981e890b05SBill Paul userok = innetgr(user+2, NULL, ruser, ypdomain); 5998538335fSBill Paul else /* match a user by direct specification */ 6008538335fSBill Paul userok = !(strcmp(ruser, user+1)); 6018538335fSBill Paul break; 6028538335fSBill Paul case '-': /* if we matched a hostname, */ 6038538335fSBill Paul if (hostok) { /* check for user field rejections */ 6048538335fSBill Paul if (!*(user+1)) 6058538335fSBill Paul return(-1); 6068538335fSBill Paul if (*(user+1) == '@') { 6078538335fSBill Paul if (innetgr(user+2, NULL, 6081e890b05SBill Paul ruser, ypdomain)) 6098538335fSBill Paul return(-1); 6108538335fSBill Paul } else { 6118538335fSBill Paul if (!strcmp(ruser, user+1)) 6128538335fSBill Paul return(-1); 6138538335fSBill Paul } 6148538335fSBill Paul } 6158538335fSBill Paul break; 6168538335fSBill Paul default: /* no rejections: try to match the user */ 6178538335fSBill Paul if (hostok) 6188538335fSBill Paul userok = !(strcmp(ruser,*user ? user : luser)); 6198538335fSBill Paul break; 6208538335fSBill Paul } 6218538335fSBill Paul if (hostok && userok) 6228538335fSBill Paul return(0); 62358f0484fSRodney W. Grimes } 62458f0484fSRodney W. Grimes return (-1); 62558f0484fSRodney W. Grimes } 62658f0484fSRodney W. Grimes 62758f0484fSRodney W. Grimes /* 62858f0484fSRodney W. Grimes * Returns "true" if match, 0 if no match. 62958f0484fSRodney W. Grimes */ 63058f0484fSRodney W. Grimes static int 63142b4f28eSYoshinobu Inoue __icheckhost(raddr, lhost, af, len) 63242b4f28eSYoshinobu Inoue void *raddr; 63358f0484fSRodney W. Grimes register char *lhost; 63442b4f28eSYoshinobu Inoue int af, len; 63558f0484fSRodney W. Grimes { 63658f0484fSRodney W. Grimes register struct hostent *hp; 63742b4f28eSYoshinobu Inoue char laddr[BUFSIZ]; /* xxx */ 63858f0484fSRodney W. Grimes register char **pp; 63942b4f28eSYoshinobu Inoue int h_error; 64042b4f28eSYoshinobu Inoue int match; 64158f0484fSRodney W. Grimes 64258f0484fSRodney W. Grimes /* Try for raw ip address first. */ 64342b4f28eSYoshinobu Inoue if (inet_pton(af, lhost, laddr) == 1) { 64442b4f28eSYoshinobu Inoue if (memcmp(raddr, laddr, len) == 0) 64542b4f28eSYoshinobu Inoue return (1); 64642b4f28eSYoshinobu Inoue else 64742b4f28eSYoshinobu Inoue return (0); 64842b4f28eSYoshinobu Inoue } 64958f0484fSRodney W. Grimes 65058f0484fSRodney W. Grimes /* Better be a hostname. */ 65142b4f28eSYoshinobu Inoue if ((hp = getipnodebyname(lhost, af, AI_DEFAULT, &h_error)) == NULL) 65258f0484fSRodney W. Grimes return (0); 65358f0484fSRodney W. Grimes 65458f0484fSRodney W. Grimes /* Spin through ip addresses. */ 65542b4f28eSYoshinobu Inoue match = 0; 65658f0484fSRodney W. Grimes for (pp = hp->h_addr_list; *pp; ++pp) 65742b4f28eSYoshinobu Inoue if (!bcmp(raddr, *pp, len)) { 65842b4f28eSYoshinobu Inoue match = 1; 65942b4f28eSYoshinobu Inoue break; 66042b4f28eSYoshinobu Inoue } 66158f0484fSRodney W. Grimes 66242b4f28eSYoshinobu Inoue freehostent(hp); 66342b4f28eSYoshinobu Inoue return (match); 66458f0484fSRodney W. Grimes } 665