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 71e3be4d7bSYoshinobu Inoue static int __iruserok_af __P((void *, int, const char *, const char *, int)); 72e0ce825eSDoug Rabson int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *)); 7342b4f28eSYoshinobu Inoue static int __icheckhost __P((void *, char *, int, int)); 7442b4f28eSYoshinobu Inoue 7542b4f28eSYoshinobu Inoue char paddr[INET6_ADDRSTRLEN]; 7658f0484fSRodney W. Grimes 7758f0484fSRodney W. Grimes int 7858f0484fSRodney W. Grimes rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 7958f0484fSRodney W. Grimes char **ahost; 8058f0484fSRodney W. Grimes u_short rport; 8158f0484fSRodney W. Grimes const char *locuser, *remuser, *cmd; 8258f0484fSRodney W. Grimes int *fd2p; 8358f0484fSRodney W. Grimes { 840cac72f4SYoshinobu Inoue return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 850cac72f4SYoshinobu Inoue } 860cac72f4SYoshinobu Inoue 870cac72f4SYoshinobu Inoue int 880cac72f4SYoshinobu Inoue rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 890cac72f4SYoshinobu Inoue char **ahost; 900cac72f4SYoshinobu Inoue u_short rport; 910cac72f4SYoshinobu Inoue const char *locuser, *remuser, *cmd; 920cac72f4SYoshinobu Inoue int *fd2p; 930cac72f4SYoshinobu Inoue int af; 940cac72f4SYoshinobu Inoue { 9542b4f28eSYoshinobu Inoue struct addrinfo hints, *res, *ai; 9642b4f28eSYoshinobu Inoue struct sockaddr_storage from; 9758f0484fSRodney W. Grimes fd_set reads; 9858f0484fSRodney W. Grimes long oldmask; 9958f0484fSRodney W. Grimes pid_t pid; 10042b4f28eSYoshinobu Inoue int s, aport, lport, timo, error; 10158f0484fSRodney W. Grimes char c; 10242b4f28eSYoshinobu Inoue int refused; 10342b4f28eSYoshinobu Inoue char num[8]; 10458f0484fSRodney W. Grimes 10558f0484fSRodney W. Grimes pid = getpid(); 10642b4f28eSYoshinobu Inoue 10742b4f28eSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 10842b4f28eSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 1090cac72f4SYoshinobu Inoue hints.ai_family = af; 11042b4f28eSYoshinobu Inoue hints.ai_socktype = SOCK_STREAM; 11142b4f28eSYoshinobu Inoue hints.ai_protocol = 0; 11242b4f28eSYoshinobu Inoue (void)snprintf(num, sizeof(num), "%d", ntohs(rport)); 11342b4f28eSYoshinobu Inoue error = getaddrinfo(*ahost, num, &hints, &res); 11442b4f28eSYoshinobu Inoue if (error) { 11542b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 11642b4f28eSYoshinobu Inoue gai_strerror(error)); 11742b4f28eSYoshinobu Inoue if (error == EAI_SYSTEM) 11842b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 11942b4f28eSYoshinobu Inoue strerror(errno)); 12058f0484fSRodney W. Grimes return (-1); 12158f0484fSRodney W. Grimes } 12242b4f28eSYoshinobu Inoue if (res->ai_canonname) 12342b4f28eSYoshinobu Inoue *ahost = res->ai_canonname; 12442b4f28eSYoshinobu Inoue ai = res; 12542b4f28eSYoshinobu Inoue refused = 0; 12658f0484fSRodney W. Grimes oldmask = sigblock(sigmask(SIGURG)); 12758f0484fSRodney W. Grimes for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 12842b4f28eSYoshinobu Inoue s = rresvport_af(&lport, ai->ai_family); 12958f0484fSRodney W. Grimes if (s < 0) { 13058f0484fSRodney W. Grimes if (errno == EAGAIN) 13158f0484fSRodney W. Grimes (void)fprintf(stderr, 13258f0484fSRodney W. Grimes "rcmd: socket: All ports in use\n"); 13358f0484fSRodney W. Grimes else 13458f0484fSRodney W. Grimes (void)fprintf(stderr, "rcmd: socket: %s\n", 13558f0484fSRodney W. Grimes strerror(errno)); 13658f0484fSRodney W. Grimes sigsetmask(oldmask); 13742b4f28eSYoshinobu Inoue freeaddrinfo(res); 13858f0484fSRodney W. Grimes return (-1); 13958f0484fSRodney W. Grimes } 1409233c4d9SJason Evans _fcntl(s, F_SETOWN, pid); 14142b4f28eSYoshinobu Inoue if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 14258f0484fSRodney W. Grimes break; 1439233c4d9SJason Evans (void)_close(s); 14458f0484fSRodney W. Grimes if (errno == EADDRINUSE) { 14558f0484fSRodney W. Grimes lport--; 14658f0484fSRodney W. Grimes continue; 14758f0484fSRodney W. Grimes } 14842b4f28eSYoshinobu Inoue if (errno == ECONNREFUSED) 14942b4f28eSYoshinobu Inoue refused = 1; 15042b4f28eSYoshinobu Inoue if (ai->ai_next != NULL) { 15158f0484fSRodney W. Grimes int oerrno = errno; 15258f0484fSRodney W. Grimes 15342b4f28eSYoshinobu Inoue getnameinfo(ai->ai_addr, ai->ai_addrlen, 15442b4f28eSYoshinobu Inoue paddr, sizeof(paddr), 15542b4f28eSYoshinobu Inoue NULL, 0, 15642b4f28eSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 15758f0484fSRodney W. Grimes (void)fprintf(stderr, "connect to address %s: ", 15842b4f28eSYoshinobu Inoue paddr); 15958f0484fSRodney W. Grimes errno = oerrno; 16058f0484fSRodney W. Grimes perror(0); 16142b4f28eSYoshinobu Inoue ai = ai->ai_next; 16242b4f28eSYoshinobu Inoue getnameinfo(ai->ai_addr, ai->ai_addrlen, 16342b4f28eSYoshinobu Inoue paddr, sizeof(paddr), 16442b4f28eSYoshinobu Inoue NULL, 0, 16542b4f28eSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 16642b4f28eSYoshinobu Inoue fprintf(stderr, "Trying %s...\n", paddr); 16758f0484fSRodney W. Grimes continue; 16858f0484fSRodney W. Grimes } 16942b4f28eSYoshinobu Inoue if (refused && timo <= 16) { 1709233c4d9SJason Evans struct timespec time_to_sleep, time_remaining; 1719233c4d9SJason Evans 1729233c4d9SJason Evans time_to_sleep.tv_sec = timo; 1739233c4d9SJason Evans time_to_sleep.tv_nsec = 0; 1749233c4d9SJason Evans (void)_nanosleep(&time_to_sleep, &time_remaining); 1759233c4d9SJason Evans 17642b4f28eSYoshinobu Inoue timo *= 2; 17742b4f28eSYoshinobu Inoue ai = res; 17842b4f28eSYoshinobu Inoue refused = 0; 17942b4f28eSYoshinobu Inoue continue; 18042b4f28eSYoshinobu Inoue } 18142b4f28eSYoshinobu Inoue freeaddrinfo(res); 18242b4f28eSYoshinobu Inoue (void)fprintf(stderr, "%s: %s\n", *ahost, strerror(errno)); 18358f0484fSRodney W. Grimes sigsetmask(oldmask); 18458f0484fSRodney W. Grimes return (-1); 18558f0484fSRodney W. Grimes } 18658f0484fSRodney W. Grimes lport--; 18758f0484fSRodney W. Grimes if (fd2p == 0) { 1889233c4d9SJason Evans _write(s, "", 1); 18958f0484fSRodney W. Grimes lport = 0; 19058f0484fSRodney W. Grimes } else { 19158f0484fSRodney W. Grimes char num[8]; 19242b4f28eSYoshinobu Inoue int s2 = rresvport_af(&lport, ai->ai_family), s3; 19342b4f28eSYoshinobu Inoue int len = ai->ai_addrlen; 194d1f32ba5SGeoff Rehmet int nfds; 19558f0484fSRodney W. Grimes 19658f0484fSRodney W. Grimes if (s2 < 0) 19758f0484fSRodney W. Grimes goto bad; 19858f0484fSRodney W. Grimes listen(s2, 1); 19958f0484fSRodney W. Grimes (void)snprintf(num, sizeof(num), "%d", lport); 2009233c4d9SJason Evans if (_write(s, num, strlen(num)+1) != strlen(num)+1) { 20158f0484fSRodney W. Grimes (void)fprintf(stderr, 20258f0484fSRodney W. Grimes "rcmd: write (setting up stderr): %s\n", 20358f0484fSRodney W. Grimes strerror(errno)); 2049233c4d9SJason Evans (void)_close(s2); 20558f0484fSRodney W. Grimes goto bad; 20658f0484fSRodney W. Grimes } 207d1f32ba5SGeoff Rehmet nfds = max(s, s2)+1; 208d1f32ba5SGeoff Rehmet if(nfds > FD_SETSIZE) { 209d1f32ba5SGeoff Rehmet fprintf(stderr, "rcmd: too many files\n"); 2109233c4d9SJason Evans (void)_close(s2); 211d1f32ba5SGeoff Rehmet goto bad; 212d1f32ba5SGeoff Rehmet } 213ce70b6caSPeter Wemm again: 21458f0484fSRodney W. Grimes FD_ZERO(&reads); 21558f0484fSRodney W. Grimes FD_SET(s, &reads); 21658f0484fSRodney W. Grimes FD_SET(s2, &reads); 21758f0484fSRodney W. Grimes errno = 0; 218d1f32ba5SGeoff Rehmet if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 21958f0484fSRodney W. Grimes if (errno != 0) 22058f0484fSRodney W. Grimes (void)fprintf(stderr, 22158f0484fSRodney W. Grimes "rcmd: select (setting up stderr): %s\n", 22258f0484fSRodney W. Grimes strerror(errno)); 22358f0484fSRodney W. Grimes else 22458f0484fSRodney W. Grimes (void)fprintf(stderr, 22558f0484fSRodney W. Grimes "select: protocol failure in circuit setup\n"); 2269233c4d9SJason Evans (void)_close(s2); 22758f0484fSRodney W. Grimes goto bad; 22858f0484fSRodney W. Grimes } 22958f0484fSRodney W. Grimes s3 = accept(s2, (struct sockaddr *)&from, &len); 23042b4f28eSYoshinobu Inoue switch (from.ss_family) { 23142b4f28eSYoshinobu Inoue case AF_INET: 23242b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 23342b4f28eSYoshinobu Inoue break; 23442b4f28eSYoshinobu Inoue #ifdef INET6 23542b4f28eSYoshinobu Inoue case AF_INET6: 23642b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 23742b4f28eSYoshinobu Inoue break; 23842b4f28eSYoshinobu Inoue #endif 23942b4f28eSYoshinobu Inoue default: 24042b4f28eSYoshinobu Inoue aport = 0; /* error */ 24142b4f28eSYoshinobu Inoue break; 24242b4f28eSYoshinobu Inoue } 243ce70b6caSPeter Wemm /* 244ce70b6caSPeter Wemm * XXX careful for ftp bounce attacks. If discovered, shut them 245ce70b6caSPeter Wemm * down and check for the real auxiliary channel to connect. 246ce70b6caSPeter Wemm */ 24742b4f28eSYoshinobu Inoue if (aport == 20) { 2489233c4d9SJason Evans _close(s3); 249ce70b6caSPeter Wemm goto again; 250ce70b6caSPeter Wemm } 2519233c4d9SJason Evans (void)_close(s2); 25258f0484fSRodney W. Grimes if (s3 < 0) { 25358f0484fSRodney W. Grimes (void)fprintf(stderr, 25458f0484fSRodney W. Grimes "rcmd: accept: %s\n", strerror(errno)); 25558f0484fSRodney W. Grimes lport = 0; 25658f0484fSRodney W. Grimes goto bad; 25758f0484fSRodney W. Grimes } 25858f0484fSRodney W. Grimes *fd2p = s3; 25942b4f28eSYoshinobu Inoue if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 26058f0484fSRodney W. Grimes (void)fprintf(stderr, 26158f0484fSRodney W. Grimes "socket: protocol failure in circuit setup.\n"); 26258f0484fSRodney W. Grimes goto bad2; 26358f0484fSRodney W. Grimes } 26458f0484fSRodney W. Grimes } 2659233c4d9SJason Evans (void)_write(s, locuser, strlen(locuser)+1); 2669233c4d9SJason Evans (void)_write(s, remuser, strlen(remuser)+1); 2679233c4d9SJason Evans (void)_write(s, cmd, strlen(cmd)+1); 2689233c4d9SJason Evans if (_read(s, &c, 1) != 1) { 26958f0484fSRodney W. Grimes (void)fprintf(stderr, 27058f0484fSRodney W. Grimes "rcmd: %s: %s\n", *ahost, strerror(errno)); 27158f0484fSRodney W. Grimes goto bad2; 27258f0484fSRodney W. Grimes } 27358f0484fSRodney W. Grimes if (c != 0) { 2749233c4d9SJason Evans while (_read(s, &c, 1) == 1) { 2759233c4d9SJason Evans (void)_write(STDERR_FILENO, &c, 1); 27658f0484fSRodney W. Grimes if (c == '\n') 27758f0484fSRodney W. Grimes break; 27858f0484fSRodney W. Grimes } 27958f0484fSRodney W. Grimes goto bad2; 28058f0484fSRodney W. Grimes } 28158f0484fSRodney W. Grimes sigsetmask(oldmask); 28242b4f28eSYoshinobu Inoue freeaddrinfo(res); 28358f0484fSRodney W. Grimes return (s); 28458f0484fSRodney W. Grimes bad2: 28558f0484fSRodney W. Grimes if (lport) 2869233c4d9SJason Evans (void)_close(*fd2p); 28758f0484fSRodney W. Grimes bad: 2889233c4d9SJason Evans (void)_close(s); 28958f0484fSRodney W. Grimes sigsetmask(oldmask); 29042b4f28eSYoshinobu Inoue freeaddrinfo(res); 29158f0484fSRodney W. Grimes return (-1); 29258f0484fSRodney W. Grimes } 29358f0484fSRodney W. Grimes 29458f0484fSRodney W. Grimes int 29542b4f28eSYoshinobu Inoue rresvport(port) 29642b4f28eSYoshinobu Inoue int *port; 29758f0484fSRodney W. Grimes { 29842b4f28eSYoshinobu Inoue return rresvport_af(port, AF_INET); 29942b4f28eSYoshinobu Inoue } 30058f0484fSRodney W. Grimes 30142b4f28eSYoshinobu Inoue int 30242b4f28eSYoshinobu Inoue rresvport_af(alport, family) 30342b4f28eSYoshinobu Inoue int *alport, family; 30442b4f28eSYoshinobu Inoue { 30542b4f28eSYoshinobu Inoue int i, s, len, err; 30642b4f28eSYoshinobu Inoue struct sockaddr_storage ss; 30742b4f28eSYoshinobu Inoue u_short *sport; 30842b4f28eSYoshinobu Inoue 30942b4f28eSYoshinobu Inoue memset(&ss, 0, sizeof(ss)); 31042b4f28eSYoshinobu Inoue ss.ss_family = family; 31142b4f28eSYoshinobu Inoue switch (family) { 31242b4f28eSYoshinobu Inoue case AF_INET: 31342b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 31442b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in *)&ss)->sin_port; 31542b4f28eSYoshinobu Inoue ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 31642b4f28eSYoshinobu Inoue break; 31742b4f28eSYoshinobu Inoue #ifdef INET6 31842b4f28eSYoshinobu Inoue case AF_INET6: 31942b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 32042b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 32142b4f28eSYoshinobu Inoue ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 32242b4f28eSYoshinobu Inoue break; 32342b4f28eSYoshinobu Inoue #endif 32442b4f28eSYoshinobu Inoue default: 32542b4f28eSYoshinobu Inoue errno = EAFNOSUPPORT; 32642b4f28eSYoshinobu Inoue return -1; 32742b4f28eSYoshinobu Inoue } 32842b4f28eSYoshinobu Inoue 32942b4f28eSYoshinobu Inoue s = socket(ss.ss_family, SOCK_STREAM, 0); 33058f0484fSRodney W. Grimes if (s < 0) 33158f0484fSRodney W. Grimes return (-1); 332ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */ 333ce70b6caSPeter Wemm sin.sin_port = htons((u_short)*alport); 334ce70b6caSPeter Wemm if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 33558f0484fSRodney W. Grimes return (s); 336ce70b6caSPeter Wemm if (errno != EADDRINUSE) { 3379233c4d9SJason Evans (void)_close(s); 33858f0484fSRodney W. Grimes return (-1); 33958f0484fSRodney W. Grimes } 340ce70b6caSPeter Wemm #endif 34142b4f28eSYoshinobu Inoue *sport = 0; 342ae42b666SYoshinobu Inoue if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { 3439233c4d9SJason Evans (void)_close(s); 344ce70b6caSPeter Wemm return (-1); 345ce70b6caSPeter Wemm } 34642b4f28eSYoshinobu Inoue *alport = (int)ntohs(*sport); 347ce70b6caSPeter Wemm return (s); 348ce70b6caSPeter Wemm } 34958f0484fSRodney W. Grimes 35058f0484fSRodney W. Grimes int __check_rhosts_file = 1; 35158f0484fSRodney W. Grimes char *__rcmd_errstr; 35258f0484fSRodney W. Grimes 35358f0484fSRodney W. Grimes int 35458f0484fSRodney W. Grimes ruserok(rhost, superuser, ruser, luser) 35558f0484fSRodney W. Grimes const char *rhost, *ruser, *luser; 35658f0484fSRodney W. Grimes int superuser; 35758f0484fSRodney W. Grimes { 358e3be4d7bSYoshinobu Inoue struct addrinfo hints, *res, *r; 359e3be4d7bSYoshinobu Inoue int error; 36042b4f28eSYoshinobu Inoue 361e3be4d7bSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 362e3be4d7bSYoshinobu Inoue hints.ai_family = PF_UNSPEC; 363e3be4d7bSYoshinobu Inoue hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 364e3be4d7bSYoshinobu Inoue error = getaddrinfo(rhost, "0", &hints, &res); 365e3be4d7bSYoshinobu Inoue if (error) 36658f0484fSRodney W. Grimes return (-1); 367e3be4d7bSYoshinobu Inoue 368e3be4d7bSYoshinobu Inoue for (r = res; r; r = r->ai_next) { 369e3be4d7bSYoshinobu Inoue if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 370e3be4d7bSYoshinobu Inoue luser) == 0) { 371e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 372e3be4d7bSYoshinobu Inoue return (0); 37342b4f28eSYoshinobu Inoue } 37442b4f28eSYoshinobu Inoue } 375e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 376e3be4d7bSYoshinobu Inoue return (-1); 37758f0484fSRodney W. Grimes } 37858f0484fSRodney W. Grimes 37958f0484fSRodney W. Grimes /* 38058f0484fSRodney W. Grimes * New .rhosts strategy: We are passed an ip address. We spin through 38158f0484fSRodney W. Grimes * hosts.equiv and .rhosts looking for a match. When the .rhosts only 38258f0484fSRodney W. Grimes * has ip addresses, we don't have to trust a nameserver. When it 38358f0484fSRodney W. Grimes * contains hostnames, we spin through the list of addresses the nameserver 38458f0484fSRodney W. Grimes * gives us and look for a match. 38558f0484fSRodney W. Grimes * 38658f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 38758f0484fSRodney W. Grimes */ 38858f0484fSRodney W. Grimes int 38958f0484fSRodney W. Grimes iruserok(raddr, superuser, ruser, luser) 390e0ce825eSDoug Rabson unsigned long raddr; 39158f0484fSRodney W. Grimes int superuser; 39258f0484fSRodney W. Grimes const char *ruser, *luser; 39358f0484fSRodney W. Grimes { 394e3be4d7bSYoshinobu Inoue return __iruserok_af(&raddr, superuser, ruser, luser, AF_INET); 39542b4f28eSYoshinobu Inoue } 39642b4f28eSYoshinobu Inoue 397e3be4d7bSYoshinobu Inoue /* Other AF support extension of iruserok. */ 398e3be4d7bSYoshinobu Inoue static int 399e3be4d7bSYoshinobu Inoue __iruserok_af(raddr, superuser, ruser, luser, af) 40042b4f28eSYoshinobu Inoue void *raddr; 40142b4f28eSYoshinobu Inoue int superuser; 40242b4f28eSYoshinobu Inoue const char *ruser, *luser; 40342b4f28eSYoshinobu Inoue int af; 40442b4f28eSYoshinobu Inoue { 40558f0484fSRodney W. Grimes register char *cp; 40658f0484fSRodney W. Grimes struct stat sbuf; 40758f0484fSRodney W. Grimes struct passwd *pwd; 40858f0484fSRodney W. Grimes FILE *hostf; 40958f0484fSRodney W. Grimes uid_t uid; 41058f0484fSRodney W. Grimes int first; 41158f0484fSRodney W. Grimes char pbuf[MAXPATHLEN]; 41242b4f28eSYoshinobu Inoue int len = 0; 41342b4f28eSYoshinobu Inoue 41442b4f28eSYoshinobu Inoue switch (af) { 41542b4f28eSYoshinobu Inoue case AF_INET: 41642b4f28eSYoshinobu Inoue len = sizeof(struct in_addr); 41742b4f28eSYoshinobu Inoue break; 41842b4f28eSYoshinobu Inoue #ifdef INET6 41942b4f28eSYoshinobu Inoue case AF_INET6: 42042b4f28eSYoshinobu Inoue len = sizeof(struct in6_addr); 42142b4f28eSYoshinobu Inoue break; 42242b4f28eSYoshinobu Inoue #endif 42342b4f28eSYoshinobu Inoue } 42458f0484fSRodney W. Grimes 42558f0484fSRodney W. Grimes first = 1; 42658f0484fSRodney W. Grimes hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 42758f0484fSRodney W. Grimes again: 42858f0484fSRodney W. Grimes if (hostf) { 42942b4f28eSYoshinobu Inoue if (__ivaliduser_af(hostf, raddr, luser, ruser, af, len) 43042b4f28eSYoshinobu Inoue == 0) { 43158f0484fSRodney W. Grimes (void)fclose(hostf); 43258f0484fSRodney W. Grimes return (0); 43358f0484fSRodney W. Grimes } 43458f0484fSRodney W. Grimes (void)fclose(hostf); 43558f0484fSRodney W. Grimes } 43658f0484fSRodney W. Grimes if (first == 1 && (__check_rhosts_file || superuser)) { 43758f0484fSRodney W. Grimes first = 0; 43858f0484fSRodney W. Grimes if ((pwd = getpwnam(luser)) == NULL) 43958f0484fSRodney W. Grimes return (-1); 44058f0484fSRodney W. Grimes (void)strcpy(pbuf, pwd->pw_dir); 44158f0484fSRodney W. Grimes (void)strcat(pbuf, "/.rhosts"); 44258f0484fSRodney W. Grimes 44358f0484fSRodney W. Grimes /* 44458f0484fSRodney W. Grimes * Change effective uid while opening .rhosts. If root and 44558f0484fSRodney W. Grimes * reading an NFS mounted file system, can't read files that 44658f0484fSRodney W. Grimes * are protected read/write owner only. 44758f0484fSRodney W. Grimes */ 44858f0484fSRodney W. Grimes uid = geteuid(); 44958f0484fSRodney W. Grimes (void)seteuid(pwd->pw_uid); 45058f0484fSRodney W. Grimes hostf = fopen(pbuf, "r"); 45158f0484fSRodney W. Grimes (void)seteuid(uid); 45258f0484fSRodney W. Grimes 45358f0484fSRodney W. Grimes if (hostf == NULL) 45458f0484fSRodney W. Grimes return (-1); 45558f0484fSRodney W. Grimes /* 45658f0484fSRodney W. Grimes * If not a regular file, or is owned by someone other than 45758f0484fSRodney W. Grimes * user or root or if writeable by anyone but the owner, quit. 45858f0484fSRodney W. Grimes */ 45958f0484fSRodney W. Grimes cp = NULL; 46058f0484fSRodney W. Grimes if (lstat(pbuf, &sbuf) < 0) 46158f0484fSRodney W. Grimes cp = ".rhosts lstat failed"; 46258f0484fSRodney W. Grimes else if (!S_ISREG(sbuf.st_mode)) 46358f0484fSRodney W. Grimes cp = ".rhosts not regular file"; 46458f0484fSRodney W. Grimes else if (fstat(fileno(hostf), &sbuf) < 0) 46558f0484fSRodney W. Grimes cp = ".rhosts fstat failed"; 46658f0484fSRodney W. Grimes else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 46758f0484fSRodney W. Grimes cp = "bad .rhosts owner"; 46858f0484fSRodney W. Grimes else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 46958f0484fSRodney W. Grimes cp = ".rhosts writeable by other than owner"; 47058f0484fSRodney W. Grimes /* If there were any problems, quit. */ 47158f0484fSRodney W. Grimes if (cp) { 47258f0484fSRodney W. Grimes __rcmd_errstr = cp; 47358f0484fSRodney W. Grimes (void)fclose(hostf); 47458f0484fSRodney W. Grimes return (-1); 47558f0484fSRodney W. Grimes } 47658f0484fSRodney W. Grimes goto again; 47758f0484fSRodney W. Grimes } 47858f0484fSRodney W. Grimes return (-1); 47958f0484fSRodney W. Grimes } 48058f0484fSRodney W. Grimes 48158f0484fSRodney W. Grimes /* 482e3be4d7bSYoshinobu Inoue * AF independent extension of iruserok. We are passed an sockaddr, and 483e3be4d7bSYoshinobu Inoue * then call iruserok_af() as the type of sockaddr. 484e3be4d7bSYoshinobu Inoue * 485e3be4d7bSYoshinobu Inoue * Returns 0 if ok, -1 if not ok. 486e3be4d7bSYoshinobu Inoue */ 487e3be4d7bSYoshinobu Inoue int 488e3be4d7bSYoshinobu Inoue iruserok_sa(addr, addrlen, superuser, ruser, luser) 489e3be4d7bSYoshinobu Inoue const void *addr; 490e3be4d7bSYoshinobu Inoue int addrlen; 491e3be4d7bSYoshinobu Inoue int superuser; 492e3be4d7bSYoshinobu Inoue const char *ruser, *luser; 493e3be4d7bSYoshinobu Inoue { 494e3be4d7bSYoshinobu Inoue struct sockaddr *sa; 495e3be4d7bSYoshinobu Inoue void *raddr = NULL; 496e3be4d7bSYoshinobu Inoue 497e3be4d7bSYoshinobu Inoue sa = (struct sockaddr *)addr; 498e3be4d7bSYoshinobu Inoue switch (sa->sa_family) { 499e3be4d7bSYoshinobu Inoue case AF_INET: 500e3be4d7bSYoshinobu Inoue raddr = &((struct sockaddr_in *)sa)->sin_addr; 501e3be4d7bSYoshinobu Inoue break; 502e3be4d7bSYoshinobu Inoue #ifdef INET6 503e3be4d7bSYoshinobu Inoue case AF_INET6: 504e3be4d7bSYoshinobu Inoue raddr = &((struct sockaddr_in6 *)sa)->sin6_addr; 505e3be4d7bSYoshinobu Inoue break; 506e3be4d7bSYoshinobu Inoue #endif 507e3be4d7bSYoshinobu Inoue } 508e3be4d7bSYoshinobu Inoue 509e3be4d7bSYoshinobu Inoue __iruserok_af(raddr, superuser, ruser, luser, sa->sa_family); 510e3be4d7bSYoshinobu Inoue } 511e3be4d7bSYoshinobu Inoue 512e3be4d7bSYoshinobu Inoue /* 51358f0484fSRodney W. Grimes * XXX 51458f0484fSRodney W. Grimes * Don't make static, used by lpd(8). 51558f0484fSRodney W. Grimes * 51658f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 51758f0484fSRodney W. Grimes */ 51858f0484fSRodney W. Grimes int 51958f0484fSRodney W. Grimes __ivaliduser(hostf, raddr, luser, ruser) 52058f0484fSRodney W. Grimes FILE *hostf; 521e0ce825eSDoug Rabson u_int32_t raddr; 52258f0484fSRodney W. Grimes const char *luser, *ruser; 52358f0484fSRodney W. Grimes { 52442b4f28eSYoshinobu Inoue return __ivaliduser_af(hostf, &raddr, luser, ruser, AF_INET, 52542b4f28eSYoshinobu Inoue sizeof(raddr)); 52642b4f28eSYoshinobu Inoue } 52742b4f28eSYoshinobu Inoue 52842b4f28eSYoshinobu Inoue int 52942b4f28eSYoshinobu Inoue __ivaliduser_af(hostf, raddr, luser, ruser, af, len) 53042b4f28eSYoshinobu Inoue FILE *hostf; 53142b4f28eSYoshinobu Inoue void *raddr; 53242b4f28eSYoshinobu Inoue const char *luser, *ruser; 53342b4f28eSYoshinobu Inoue int af, len; 53442b4f28eSYoshinobu Inoue { 53558f0484fSRodney W. Grimes register char *user, *p; 53658f0484fSRodney W. Grimes int ch; 53758f0484fSRodney W. Grimes char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 53897cb5094SBill Paul char hname[MAXHOSTNAMELEN]; 5398538335fSBill Paul struct hostent *hp; 5408538335fSBill Paul /* Presumed guilty until proven innocent. */ 5418538335fSBill Paul int userok = 0, hostok = 0; 54242b4f28eSYoshinobu Inoue int h_error; 5431e890b05SBill Paul #ifdef YP 5441e890b05SBill Paul char *ypdomain; 5458538335fSBill Paul 5461e890b05SBill Paul if (yp_get_default_domain(&ypdomain)) 5471e890b05SBill Paul ypdomain = NULL; 5481e890b05SBill Paul #else 5491e890b05SBill Paul #define ypdomain NULL 5501e890b05SBill Paul #endif 5518538335fSBill Paul /* We need to get the damn hostname back for netgroup matching. */ 55242b4f28eSYoshinobu Inoue if ((hp = getipnodebyaddr((char *)raddr, len, af, &h_error)) == NULL) 5538538335fSBill Paul return (-1); 5542a62f023SWarner Losh strncpy(hname, hp->h_name, sizeof(hname)); 5552a62f023SWarner Losh hname[sizeof(hname) - 1] = '\0'; 55642b4f28eSYoshinobu Inoue freehostent(hp); 55758f0484fSRodney W. Grimes 55858f0484fSRodney W. Grimes while (fgets(buf, sizeof(buf), hostf)) { 55958f0484fSRodney W. Grimes p = buf; 56058f0484fSRodney W. Grimes /* Skip lines that are too long. */ 56158f0484fSRodney W. Grimes if (strchr(p, '\n') == NULL) { 56258f0484fSRodney W. Grimes while ((ch = getc(hostf)) != '\n' && ch != EOF); 56358f0484fSRodney W. Grimes continue; 56458f0484fSRodney W. Grimes } 565acc7e87cSPeter Wemm if (*p == '\n' || *p == '#') { 566acc7e87cSPeter Wemm /* comment... */ 567acc7e87cSPeter Wemm continue; 568acc7e87cSPeter Wemm } 56958f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 5703fb85bffSAndrey A. Chernov *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 57158f0484fSRodney W. Grimes p++; 57258f0484fSRodney W. Grimes } 57358f0484fSRodney W. Grimes if (*p == ' ' || *p == '\t') { 57458f0484fSRodney W. Grimes *p++ = '\0'; 57558f0484fSRodney W. Grimes while (*p == ' ' || *p == '\t') 57658f0484fSRodney W. Grimes p++; 57758f0484fSRodney W. Grimes user = p; 57858f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && 57958f0484fSRodney W. Grimes *p != '\t' && *p != '\0') 58058f0484fSRodney W. Grimes p++; 58158f0484fSRodney W. Grimes } else 58258f0484fSRodney W. Grimes user = p; 58358f0484fSRodney W. Grimes *p = '\0'; 5848538335fSBill Paul /* 5858538335fSBill Paul * Do +/- and +@/-@ checking. This looks really nasty, 5868538335fSBill Paul * but it matches SunOS's behavior so far as I can tell. 5878538335fSBill Paul */ 5888538335fSBill Paul switch(buf[0]) { 5898538335fSBill Paul case '+': 5908538335fSBill Paul if (!buf[1]) { /* '+' matches all hosts */ 5918538335fSBill Paul hostok = 1; 5928538335fSBill Paul break; 59358f0484fSRodney W. Grimes } 5948538335fSBill Paul if (buf[1] == '@') /* match a host by netgroup */ 59597cb5094SBill Paul hostok = innetgr((char *)&buf[2], 59697cb5094SBill Paul (char *)&hname, NULL, ypdomain); 5978538335fSBill Paul else /* match a host by addr */ 59842b4f28eSYoshinobu Inoue hostok = __icheckhost(raddr,(char *)&buf[1], 59942b4f28eSYoshinobu Inoue af, len); 6008538335fSBill Paul break; 6018538335fSBill Paul case '-': /* reject '-' hosts and all their users */ 6028538335fSBill Paul if (buf[1] == '@') { 6038538335fSBill Paul if (innetgr((char *)&buf[2], 60497cb5094SBill Paul (char *)&hname, NULL, ypdomain)) 6058538335fSBill Paul return(-1); 6068538335fSBill Paul } else { 60742b4f28eSYoshinobu Inoue if (__icheckhost(raddr,(char *)&buf[1],af,len)) 6088538335fSBill Paul return(-1); 6098538335fSBill Paul } 6108538335fSBill Paul break; 6118538335fSBill Paul default: /* if no '+' or '-', do a simple match */ 61242b4f28eSYoshinobu Inoue hostok = __icheckhost(raddr, buf, af, len); 6138538335fSBill Paul break; 6148538335fSBill Paul } 6158538335fSBill Paul switch(*user) { 6168538335fSBill Paul case '+': 6178538335fSBill Paul if (!*(user+1)) { /* '+' matches all users */ 6188538335fSBill Paul userok = 1; 6198538335fSBill Paul break; 6208538335fSBill Paul } 6218538335fSBill Paul if (*(user+1) == '@') /* match a user by netgroup */ 6221e890b05SBill Paul userok = innetgr(user+2, NULL, ruser, ypdomain); 6238538335fSBill Paul else /* match a user by direct specification */ 6248538335fSBill Paul userok = !(strcmp(ruser, user+1)); 6258538335fSBill Paul break; 6268538335fSBill Paul case '-': /* if we matched a hostname, */ 6278538335fSBill Paul if (hostok) { /* check for user field rejections */ 6288538335fSBill Paul if (!*(user+1)) 6298538335fSBill Paul return(-1); 6308538335fSBill Paul if (*(user+1) == '@') { 6318538335fSBill Paul if (innetgr(user+2, NULL, 6321e890b05SBill Paul ruser, ypdomain)) 6338538335fSBill Paul return(-1); 6348538335fSBill Paul } else { 6358538335fSBill Paul if (!strcmp(ruser, user+1)) 6368538335fSBill Paul return(-1); 6378538335fSBill Paul } 6388538335fSBill Paul } 6398538335fSBill Paul break; 6408538335fSBill Paul default: /* no rejections: try to match the user */ 6418538335fSBill Paul if (hostok) 6428538335fSBill Paul userok = !(strcmp(ruser,*user ? user : luser)); 6438538335fSBill Paul break; 6448538335fSBill Paul } 6458538335fSBill Paul if (hostok && userok) 6468538335fSBill Paul return(0); 64758f0484fSRodney W. Grimes } 64858f0484fSRodney W. Grimes return (-1); 64958f0484fSRodney W. Grimes } 65058f0484fSRodney W. Grimes 65158f0484fSRodney W. Grimes /* 65258f0484fSRodney W. Grimes * Returns "true" if match, 0 if no match. 65358f0484fSRodney W. Grimes */ 65458f0484fSRodney W. Grimes static int 65542b4f28eSYoshinobu Inoue __icheckhost(raddr, lhost, af, len) 65642b4f28eSYoshinobu Inoue void *raddr; 65758f0484fSRodney W. Grimes register char *lhost; 65842b4f28eSYoshinobu Inoue int af, len; 65958f0484fSRodney W. Grimes { 66058f0484fSRodney W. Grimes register struct hostent *hp; 66142b4f28eSYoshinobu Inoue char laddr[BUFSIZ]; /* xxx */ 66258f0484fSRodney W. Grimes register char **pp; 66342b4f28eSYoshinobu Inoue int h_error; 66442b4f28eSYoshinobu Inoue int match; 66558f0484fSRodney W. Grimes 66658f0484fSRodney W. Grimes /* Try for raw ip address first. */ 66742b4f28eSYoshinobu Inoue if (inet_pton(af, lhost, laddr) == 1) { 66842b4f28eSYoshinobu Inoue if (memcmp(raddr, laddr, len) == 0) 66942b4f28eSYoshinobu Inoue return (1); 67042b4f28eSYoshinobu Inoue else 67142b4f28eSYoshinobu Inoue return (0); 67242b4f28eSYoshinobu Inoue } 67358f0484fSRodney W. Grimes 67458f0484fSRodney W. Grimes /* Better be a hostname. */ 675e3be4d7bSYoshinobu Inoue if ((hp = getipnodebyname(lhost, af, AI_ALL|AI_DEFAULT, &h_error)) 676e3be4d7bSYoshinobu Inoue == NULL) 67758f0484fSRodney W. Grimes return (0); 67858f0484fSRodney W. Grimes 67958f0484fSRodney W. Grimes /* Spin through ip addresses. */ 68042b4f28eSYoshinobu Inoue match = 0; 68158f0484fSRodney W. Grimes for (pp = hp->h_addr_list; *pp; ++pp) 68242b4f28eSYoshinobu Inoue if (!bcmp(raddr, *pp, len)) { 68342b4f28eSYoshinobu Inoue match = 1; 68442b4f28eSYoshinobu Inoue break; 68542b4f28eSYoshinobu Inoue } 68658f0484fSRodney W. Grimes 68742b4f28eSYoshinobu Inoue freehostent(hp); 68842b4f28eSYoshinobu Inoue return (match); 68958f0484fSRodney W. Grimes } 690