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) { 130474ce1d1SYoshinobu Inoue if (errno != EAGAIN && ai->ai_next) { 131474ce1d1SYoshinobu Inoue ai = ai->ai_next; 132474ce1d1SYoshinobu Inoue continue; 133474ce1d1SYoshinobu Inoue } 13458f0484fSRodney W. Grimes if (errno == EAGAIN) 13558f0484fSRodney W. Grimes (void)fprintf(stderr, 13658f0484fSRodney W. Grimes "rcmd: socket: All ports in use\n"); 13758f0484fSRodney W. Grimes else 13858f0484fSRodney W. Grimes (void)fprintf(stderr, "rcmd: socket: %s\n", 13958f0484fSRodney W. Grimes strerror(errno)); 14042b4f28eSYoshinobu Inoue freeaddrinfo(res); 141474ce1d1SYoshinobu Inoue sigsetmask(oldmask); 14258f0484fSRodney W. Grimes return (-1); 14358f0484fSRodney W. Grimes } 1449233c4d9SJason Evans _fcntl(s, F_SETOWN, pid); 14542b4f28eSYoshinobu Inoue if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 14658f0484fSRodney W. Grimes break; 1479233c4d9SJason Evans (void)_close(s); 14858f0484fSRodney W. Grimes if (errno == EADDRINUSE) { 14958f0484fSRodney W. Grimes lport--; 15058f0484fSRodney W. Grimes continue; 15158f0484fSRodney W. Grimes } 15242b4f28eSYoshinobu Inoue if (errno == ECONNREFUSED) 15342b4f28eSYoshinobu Inoue refused = 1; 15442b4f28eSYoshinobu Inoue if (ai->ai_next != NULL) { 15558f0484fSRodney W. Grimes int oerrno = errno; 15658f0484fSRodney W. Grimes 15742b4f28eSYoshinobu Inoue getnameinfo(ai->ai_addr, ai->ai_addrlen, 15842b4f28eSYoshinobu Inoue paddr, sizeof(paddr), 15942b4f28eSYoshinobu Inoue NULL, 0, 16042b4f28eSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 16158f0484fSRodney W. Grimes (void)fprintf(stderr, "connect to address %s: ", 16242b4f28eSYoshinobu Inoue paddr); 16358f0484fSRodney W. Grimes errno = oerrno; 16458f0484fSRodney W. Grimes perror(0); 16542b4f28eSYoshinobu Inoue ai = ai->ai_next; 16642b4f28eSYoshinobu Inoue getnameinfo(ai->ai_addr, ai->ai_addrlen, 16742b4f28eSYoshinobu Inoue paddr, sizeof(paddr), 16842b4f28eSYoshinobu Inoue NULL, 0, 16942b4f28eSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 17042b4f28eSYoshinobu Inoue fprintf(stderr, "Trying %s...\n", paddr); 17158f0484fSRodney W. Grimes continue; 17258f0484fSRodney W. Grimes } 17342b4f28eSYoshinobu Inoue if (refused && timo <= 16) { 1749233c4d9SJason Evans struct timespec time_to_sleep, time_remaining; 1759233c4d9SJason Evans 1769233c4d9SJason Evans time_to_sleep.tv_sec = timo; 1779233c4d9SJason Evans time_to_sleep.tv_nsec = 0; 1789233c4d9SJason Evans (void)_nanosleep(&time_to_sleep, &time_remaining); 1799233c4d9SJason Evans 18042b4f28eSYoshinobu Inoue timo *= 2; 18142b4f28eSYoshinobu Inoue ai = res; 18242b4f28eSYoshinobu Inoue refused = 0; 18342b4f28eSYoshinobu Inoue continue; 18442b4f28eSYoshinobu Inoue } 18542b4f28eSYoshinobu Inoue freeaddrinfo(res); 18642b4f28eSYoshinobu Inoue (void)fprintf(stderr, "%s: %s\n", *ahost, strerror(errno)); 18758f0484fSRodney W. Grimes sigsetmask(oldmask); 18858f0484fSRodney W. Grimes return (-1); 18958f0484fSRodney W. Grimes } 19058f0484fSRodney W. Grimes lport--; 19158f0484fSRodney W. Grimes if (fd2p == 0) { 1929233c4d9SJason Evans _write(s, "", 1); 19358f0484fSRodney W. Grimes lport = 0; 19458f0484fSRodney W. Grimes } else { 19558f0484fSRodney W. Grimes char num[8]; 19642b4f28eSYoshinobu Inoue int s2 = rresvport_af(&lport, ai->ai_family), s3; 19742b4f28eSYoshinobu Inoue int len = ai->ai_addrlen; 198d1f32ba5SGeoff Rehmet int nfds; 19958f0484fSRodney W. Grimes 20058f0484fSRodney W. Grimes if (s2 < 0) 20158f0484fSRodney W. Grimes goto bad; 20258f0484fSRodney W. Grimes listen(s2, 1); 20358f0484fSRodney W. Grimes (void)snprintf(num, sizeof(num), "%d", lport); 2049233c4d9SJason Evans if (_write(s, num, strlen(num)+1) != strlen(num)+1) { 20558f0484fSRodney W. Grimes (void)fprintf(stderr, 20658f0484fSRodney W. Grimes "rcmd: write (setting up stderr): %s\n", 20758f0484fSRodney W. Grimes strerror(errno)); 2089233c4d9SJason Evans (void)_close(s2); 20958f0484fSRodney W. Grimes goto bad; 21058f0484fSRodney W. Grimes } 211d1f32ba5SGeoff Rehmet nfds = max(s, s2)+1; 212d1f32ba5SGeoff Rehmet if(nfds > FD_SETSIZE) { 213d1f32ba5SGeoff Rehmet fprintf(stderr, "rcmd: too many files\n"); 2149233c4d9SJason Evans (void)_close(s2); 215d1f32ba5SGeoff Rehmet goto bad; 216d1f32ba5SGeoff Rehmet } 217ce70b6caSPeter Wemm again: 21858f0484fSRodney W. Grimes FD_ZERO(&reads); 21958f0484fSRodney W. Grimes FD_SET(s, &reads); 22058f0484fSRodney W. Grimes FD_SET(s2, &reads); 22158f0484fSRodney W. Grimes errno = 0; 222d1f32ba5SGeoff Rehmet if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 22358f0484fSRodney W. Grimes if (errno != 0) 22458f0484fSRodney W. Grimes (void)fprintf(stderr, 22558f0484fSRodney W. Grimes "rcmd: select (setting up stderr): %s\n", 22658f0484fSRodney W. Grimes strerror(errno)); 22758f0484fSRodney W. Grimes else 22858f0484fSRodney W. Grimes (void)fprintf(stderr, 22958f0484fSRodney W. Grimes "select: protocol failure in circuit setup\n"); 2309233c4d9SJason Evans (void)_close(s2); 23158f0484fSRodney W. Grimes goto bad; 23258f0484fSRodney W. Grimes } 23358f0484fSRodney W. Grimes s3 = accept(s2, (struct sockaddr *)&from, &len); 23442b4f28eSYoshinobu Inoue switch (from.ss_family) { 23542b4f28eSYoshinobu Inoue case AF_INET: 23642b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 23742b4f28eSYoshinobu Inoue break; 23842b4f28eSYoshinobu Inoue #ifdef INET6 23942b4f28eSYoshinobu Inoue case AF_INET6: 24042b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 24142b4f28eSYoshinobu Inoue break; 24242b4f28eSYoshinobu Inoue #endif 24342b4f28eSYoshinobu Inoue default: 24442b4f28eSYoshinobu Inoue aport = 0; /* error */ 24542b4f28eSYoshinobu Inoue break; 24642b4f28eSYoshinobu Inoue } 247ce70b6caSPeter Wemm /* 248ce70b6caSPeter Wemm * XXX careful for ftp bounce attacks. If discovered, shut them 249ce70b6caSPeter Wemm * down and check for the real auxiliary channel to connect. 250ce70b6caSPeter Wemm */ 25142b4f28eSYoshinobu Inoue if (aport == 20) { 2529233c4d9SJason Evans _close(s3); 253ce70b6caSPeter Wemm goto again; 254ce70b6caSPeter Wemm } 2559233c4d9SJason Evans (void)_close(s2); 25658f0484fSRodney W. Grimes if (s3 < 0) { 25758f0484fSRodney W. Grimes (void)fprintf(stderr, 25858f0484fSRodney W. Grimes "rcmd: accept: %s\n", strerror(errno)); 25958f0484fSRodney W. Grimes lport = 0; 26058f0484fSRodney W. Grimes goto bad; 26158f0484fSRodney W. Grimes } 26258f0484fSRodney W. Grimes *fd2p = s3; 26342b4f28eSYoshinobu Inoue if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 26458f0484fSRodney W. Grimes (void)fprintf(stderr, 26558f0484fSRodney W. Grimes "socket: protocol failure in circuit setup.\n"); 26658f0484fSRodney W. Grimes goto bad2; 26758f0484fSRodney W. Grimes } 26858f0484fSRodney W. Grimes } 2699233c4d9SJason Evans (void)_write(s, locuser, strlen(locuser)+1); 2709233c4d9SJason Evans (void)_write(s, remuser, strlen(remuser)+1); 2719233c4d9SJason Evans (void)_write(s, cmd, strlen(cmd)+1); 2729233c4d9SJason Evans if (_read(s, &c, 1) != 1) { 27358f0484fSRodney W. Grimes (void)fprintf(stderr, 27458f0484fSRodney W. Grimes "rcmd: %s: %s\n", *ahost, strerror(errno)); 27558f0484fSRodney W. Grimes goto bad2; 27658f0484fSRodney W. Grimes } 27758f0484fSRodney W. Grimes if (c != 0) { 2789233c4d9SJason Evans while (_read(s, &c, 1) == 1) { 2799233c4d9SJason Evans (void)_write(STDERR_FILENO, &c, 1); 28058f0484fSRodney W. Grimes if (c == '\n') 28158f0484fSRodney W. Grimes break; 28258f0484fSRodney W. Grimes } 28358f0484fSRodney W. Grimes goto bad2; 28458f0484fSRodney W. Grimes } 28558f0484fSRodney W. Grimes sigsetmask(oldmask); 28642b4f28eSYoshinobu Inoue freeaddrinfo(res); 28758f0484fSRodney W. Grimes return (s); 28858f0484fSRodney W. Grimes bad2: 28958f0484fSRodney W. Grimes if (lport) 2909233c4d9SJason Evans (void)_close(*fd2p); 29158f0484fSRodney W. Grimes bad: 2929233c4d9SJason Evans (void)_close(s); 29358f0484fSRodney W. Grimes sigsetmask(oldmask); 29442b4f28eSYoshinobu Inoue freeaddrinfo(res); 29558f0484fSRodney W. Grimes return (-1); 29658f0484fSRodney W. Grimes } 29758f0484fSRodney W. Grimes 29858f0484fSRodney W. Grimes int 29942b4f28eSYoshinobu Inoue rresvport(port) 30042b4f28eSYoshinobu Inoue int *port; 30158f0484fSRodney W. Grimes { 30242b4f28eSYoshinobu Inoue return rresvport_af(port, AF_INET); 30342b4f28eSYoshinobu Inoue } 30458f0484fSRodney W. Grimes 30542b4f28eSYoshinobu Inoue int 30642b4f28eSYoshinobu Inoue rresvport_af(alport, family) 30742b4f28eSYoshinobu Inoue int *alport, family; 30842b4f28eSYoshinobu Inoue { 30942b4f28eSYoshinobu Inoue int i, s, len, err; 31042b4f28eSYoshinobu Inoue struct sockaddr_storage ss; 31142b4f28eSYoshinobu Inoue u_short *sport; 31242b4f28eSYoshinobu Inoue 31342b4f28eSYoshinobu Inoue memset(&ss, 0, sizeof(ss)); 31442b4f28eSYoshinobu Inoue ss.ss_family = family; 31542b4f28eSYoshinobu Inoue switch (family) { 31642b4f28eSYoshinobu Inoue case AF_INET: 31742b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 31842b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in *)&ss)->sin_port; 31942b4f28eSYoshinobu Inoue ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 32042b4f28eSYoshinobu Inoue break; 32142b4f28eSYoshinobu Inoue #ifdef INET6 32242b4f28eSYoshinobu Inoue case AF_INET6: 32342b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 32442b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 32542b4f28eSYoshinobu Inoue ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 32642b4f28eSYoshinobu Inoue break; 32742b4f28eSYoshinobu Inoue #endif 32842b4f28eSYoshinobu Inoue default: 32942b4f28eSYoshinobu Inoue errno = EAFNOSUPPORT; 33042b4f28eSYoshinobu Inoue return -1; 33142b4f28eSYoshinobu Inoue } 33242b4f28eSYoshinobu Inoue 33342b4f28eSYoshinobu Inoue s = socket(ss.ss_family, SOCK_STREAM, 0); 33458f0484fSRodney W. Grimes if (s < 0) 33558f0484fSRodney W. Grimes return (-1); 336ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */ 337ce70b6caSPeter Wemm sin.sin_port = htons((u_short)*alport); 338ce70b6caSPeter Wemm if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 33958f0484fSRodney W. Grimes return (s); 340ce70b6caSPeter Wemm if (errno != EADDRINUSE) { 3419233c4d9SJason Evans (void)_close(s); 34258f0484fSRodney W. Grimes return (-1); 34358f0484fSRodney W. Grimes } 344ce70b6caSPeter Wemm #endif 34542b4f28eSYoshinobu Inoue *sport = 0; 346ae42b666SYoshinobu Inoue if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { 3479233c4d9SJason Evans (void)_close(s); 348ce70b6caSPeter Wemm return (-1); 349ce70b6caSPeter Wemm } 35042b4f28eSYoshinobu Inoue *alport = (int)ntohs(*sport); 351ce70b6caSPeter Wemm return (s); 352ce70b6caSPeter Wemm } 35358f0484fSRodney W. Grimes 35458f0484fSRodney W. Grimes int __check_rhosts_file = 1; 35558f0484fSRodney W. Grimes char *__rcmd_errstr; 35658f0484fSRodney W. Grimes 35758f0484fSRodney W. Grimes int 35858f0484fSRodney W. Grimes ruserok(rhost, superuser, ruser, luser) 35958f0484fSRodney W. Grimes const char *rhost, *ruser, *luser; 36058f0484fSRodney W. Grimes int superuser; 36158f0484fSRodney W. Grimes { 362e3be4d7bSYoshinobu Inoue struct addrinfo hints, *res, *r; 363e3be4d7bSYoshinobu Inoue int error; 36442b4f28eSYoshinobu Inoue 365e3be4d7bSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 366e3be4d7bSYoshinobu Inoue hints.ai_family = PF_UNSPEC; 367e3be4d7bSYoshinobu Inoue hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 368e3be4d7bSYoshinobu Inoue error = getaddrinfo(rhost, "0", &hints, &res); 369e3be4d7bSYoshinobu Inoue if (error) 37058f0484fSRodney W. Grimes return (-1); 371e3be4d7bSYoshinobu Inoue 372e3be4d7bSYoshinobu Inoue for (r = res; r; r = r->ai_next) { 373e3be4d7bSYoshinobu Inoue if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 374e3be4d7bSYoshinobu Inoue luser) == 0) { 375e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 376e3be4d7bSYoshinobu Inoue return (0); 37742b4f28eSYoshinobu Inoue } 37842b4f28eSYoshinobu Inoue } 379e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 380e3be4d7bSYoshinobu Inoue return (-1); 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 { 398e3be4d7bSYoshinobu Inoue return __iruserok_af(&raddr, superuser, ruser, luser, AF_INET); 39942b4f28eSYoshinobu Inoue } 40042b4f28eSYoshinobu Inoue 401e3be4d7bSYoshinobu Inoue /* Other AF support extension of iruserok. */ 402e3be4d7bSYoshinobu Inoue static int 403e3be4d7bSYoshinobu Inoue __iruserok_af(raddr, superuser, ruser, luser, af) 40442b4f28eSYoshinobu Inoue void *raddr; 40542b4f28eSYoshinobu Inoue int superuser; 40642b4f28eSYoshinobu Inoue const char *ruser, *luser; 40742b4f28eSYoshinobu Inoue int af; 40842b4f28eSYoshinobu Inoue { 40958f0484fSRodney W. Grimes register char *cp; 41058f0484fSRodney W. Grimes struct stat sbuf; 41158f0484fSRodney W. Grimes struct passwd *pwd; 41258f0484fSRodney W. Grimes FILE *hostf; 41358f0484fSRodney W. Grimes uid_t uid; 41458f0484fSRodney W. Grimes int first; 41558f0484fSRodney W. Grimes char pbuf[MAXPATHLEN]; 41642b4f28eSYoshinobu Inoue int len = 0; 41742b4f28eSYoshinobu Inoue 41842b4f28eSYoshinobu Inoue switch (af) { 41942b4f28eSYoshinobu Inoue case AF_INET: 42042b4f28eSYoshinobu Inoue len = sizeof(struct in_addr); 42142b4f28eSYoshinobu Inoue break; 42242b4f28eSYoshinobu Inoue #ifdef INET6 42342b4f28eSYoshinobu Inoue case AF_INET6: 42442b4f28eSYoshinobu Inoue len = sizeof(struct in6_addr); 42542b4f28eSYoshinobu Inoue break; 42642b4f28eSYoshinobu Inoue #endif 42742b4f28eSYoshinobu Inoue } 42858f0484fSRodney W. Grimes 42958f0484fSRodney W. Grimes first = 1; 43058f0484fSRodney W. Grimes hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 43158f0484fSRodney W. Grimes again: 43258f0484fSRodney W. Grimes if (hostf) { 43342b4f28eSYoshinobu Inoue if (__ivaliduser_af(hostf, raddr, luser, ruser, af, len) 43442b4f28eSYoshinobu Inoue == 0) { 43558f0484fSRodney W. Grimes (void)fclose(hostf); 43658f0484fSRodney W. Grimes return (0); 43758f0484fSRodney W. Grimes } 43858f0484fSRodney W. Grimes (void)fclose(hostf); 43958f0484fSRodney W. Grimes } 44058f0484fSRodney W. Grimes if (first == 1 && (__check_rhosts_file || superuser)) { 44158f0484fSRodney W. Grimes first = 0; 44258f0484fSRodney W. Grimes if ((pwd = getpwnam(luser)) == NULL) 44358f0484fSRodney W. Grimes return (-1); 44458f0484fSRodney W. Grimes (void)strcpy(pbuf, pwd->pw_dir); 44558f0484fSRodney W. Grimes (void)strcat(pbuf, "/.rhosts"); 44658f0484fSRodney W. Grimes 44758f0484fSRodney W. Grimes /* 44858f0484fSRodney W. Grimes * Change effective uid while opening .rhosts. If root and 44958f0484fSRodney W. Grimes * reading an NFS mounted file system, can't read files that 45058f0484fSRodney W. Grimes * are protected read/write owner only. 45158f0484fSRodney W. Grimes */ 45258f0484fSRodney W. Grimes uid = geteuid(); 45358f0484fSRodney W. Grimes (void)seteuid(pwd->pw_uid); 45458f0484fSRodney W. Grimes hostf = fopen(pbuf, "r"); 45558f0484fSRodney W. Grimes (void)seteuid(uid); 45658f0484fSRodney W. Grimes 45758f0484fSRodney W. Grimes if (hostf == NULL) 45858f0484fSRodney W. Grimes return (-1); 45958f0484fSRodney W. Grimes /* 46058f0484fSRodney W. Grimes * If not a regular file, or is owned by someone other than 46158f0484fSRodney W. Grimes * user or root or if writeable by anyone but the owner, quit. 46258f0484fSRodney W. Grimes */ 46358f0484fSRodney W. Grimes cp = NULL; 46458f0484fSRodney W. Grimes if (lstat(pbuf, &sbuf) < 0) 46558f0484fSRodney W. Grimes cp = ".rhosts lstat failed"; 46658f0484fSRodney W. Grimes else if (!S_ISREG(sbuf.st_mode)) 46758f0484fSRodney W. Grimes cp = ".rhosts not regular file"; 46858f0484fSRodney W. Grimes else if (fstat(fileno(hostf), &sbuf) < 0) 46958f0484fSRodney W. Grimes cp = ".rhosts fstat failed"; 47058f0484fSRodney W. Grimes else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 47158f0484fSRodney W. Grimes cp = "bad .rhosts owner"; 47258f0484fSRodney W. Grimes else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 47358f0484fSRodney W. Grimes cp = ".rhosts writeable by other than owner"; 47458f0484fSRodney W. Grimes /* If there were any problems, quit. */ 47558f0484fSRodney W. Grimes if (cp) { 47658f0484fSRodney W. Grimes __rcmd_errstr = cp; 47758f0484fSRodney W. Grimes (void)fclose(hostf); 47858f0484fSRodney W. Grimes return (-1); 47958f0484fSRodney W. Grimes } 48058f0484fSRodney W. Grimes goto again; 48158f0484fSRodney W. Grimes } 48258f0484fSRodney W. Grimes return (-1); 48358f0484fSRodney W. Grimes } 48458f0484fSRodney W. Grimes 48558f0484fSRodney W. Grimes /* 486e3be4d7bSYoshinobu Inoue * AF independent extension of iruserok. We are passed an sockaddr, and 487e3be4d7bSYoshinobu Inoue * then call iruserok_af() as the type of sockaddr. 488e3be4d7bSYoshinobu Inoue * 489e3be4d7bSYoshinobu Inoue * Returns 0 if ok, -1 if not ok. 490e3be4d7bSYoshinobu Inoue */ 491e3be4d7bSYoshinobu Inoue int 492e3be4d7bSYoshinobu Inoue iruserok_sa(addr, addrlen, superuser, ruser, luser) 493e3be4d7bSYoshinobu Inoue const void *addr; 494e3be4d7bSYoshinobu Inoue int addrlen; 495e3be4d7bSYoshinobu Inoue int superuser; 496e3be4d7bSYoshinobu Inoue const char *ruser, *luser; 497e3be4d7bSYoshinobu Inoue { 498e3be4d7bSYoshinobu Inoue struct sockaddr *sa; 499e3be4d7bSYoshinobu Inoue void *raddr = NULL; 500e3be4d7bSYoshinobu Inoue 501e3be4d7bSYoshinobu Inoue sa = (struct sockaddr *)addr; 502e3be4d7bSYoshinobu Inoue switch (sa->sa_family) { 503e3be4d7bSYoshinobu Inoue case AF_INET: 504e3be4d7bSYoshinobu Inoue raddr = &((struct sockaddr_in *)sa)->sin_addr; 505e3be4d7bSYoshinobu Inoue break; 506e3be4d7bSYoshinobu Inoue #ifdef INET6 507e3be4d7bSYoshinobu Inoue case AF_INET6: 508e3be4d7bSYoshinobu Inoue raddr = &((struct sockaddr_in6 *)sa)->sin6_addr; 509e3be4d7bSYoshinobu Inoue break; 510e3be4d7bSYoshinobu Inoue #endif 511e3be4d7bSYoshinobu Inoue } 512e3be4d7bSYoshinobu Inoue 513e3be4d7bSYoshinobu Inoue __iruserok_af(raddr, superuser, ruser, luser, sa->sa_family); 514e3be4d7bSYoshinobu Inoue } 515e3be4d7bSYoshinobu Inoue 516e3be4d7bSYoshinobu Inoue /* 51758f0484fSRodney W. Grimes * XXX 51858f0484fSRodney W. Grimes * Don't make static, used by lpd(8). 51958f0484fSRodney W. Grimes * 52058f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 52158f0484fSRodney W. Grimes */ 52258f0484fSRodney W. Grimes int 52358f0484fSRodney W. Grimes __ivaliduser(hostf, raddr, luser, ruser) 52458f0484fSRodney W. Grimes FILE *hostf; 525e0ce825eSDoug Rabson u_int32_t raddr; 52658f0484fSRodney W. Grimes const char *luser, *ruser; 52758f0484fSRodney W. Grimes { 52842b4f28eSYoshinobu Inoue return __ivaliduser_af(hostf, &raddr, luser, ruser, AF_INET, 52942b4f28eSYoshinobu Inoue sizeof(raddr)); 53042b4f28eSYoshinobu Inoue } 53142b4f28eSYoshinobu Inoue 53242b4f28eSYoshinobu Inoue int 53342b4f28eSYoshinobu Inoue __ivaliduser_af(hostf, raddr, luser, ruser, af, len) 53442b4f28eSYoshinobu Inoue FILE *hostf; 53542b4f28eSYoshinobu Inoue void *raddr; 53642b4f28eSYoshinobu Inoue const char *luser, *ruser; 53742b4f28eSYoshinobu Inoue int af, len; 53842b4f28eSYoshinobu Inoue { 53958f0484fSRodney W. Grimes register char *user, *p; 54058f0484fSRodney W. Grimes int ch; 54158f0484fSRodney W. Grimes char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 54297cb5094SBill Paul char hname[MAXHOSTNAMELEN]; 5438538335fSBill Paul struct hostent *hp; 5448538335fSBill Paul /* Presumed guilty until proven innocent. */ 5458538335fSBill Paul int userok = 0, hostok = 0; 54642b4f28eSYoshinobu Inoue int h_error; 5471e890b05SBill Paul #ifdef YP 5481e890b05SBill Paul char *ypdomain; 5498538335fSBill Paul 5501e890b05SBill Paul if (yp_get_default_domain(&ypdomain)) 5511e890b05SBill Paul ypdomain = NULL; 5521e890b05SBill Paul #else 5531e890b05SBill Paul #define ypdomain NULL 5541e890b05SBill Paul #endif 5558538335fSBill Paul /* We need to get the damn hostname back for netgroup matching. */ 55642b4f28eSYoshinobu Inoue if ((hp = getipnodebyaddr((char *)raddr, len, af, &h_error)) == NULL) 5578538335fSBill Paul return (-1); 5582a62f023SWarner Losh strncpy(hname, hp->h_name, sizeof(hname)); 5592a62f023SWarner Losh hname[sizeof(hname) - 1] = '\0'; 56042b4f28eSYoshinobu Inoue freehostent(hp); 56158f0484fSRodney W. Grimes 56258f0484fSRodney W. Grimes while (fgets(buf, sizeof(buf), hostf)) { 56358f0484fSRodney W. Grimes p = buf; 56458f0484fSRodney W. Grimes /* Skip lines that are too long. */ 56558f0484fSRodney W. Grimes if (strchr(p, '\n') == NULL) { 56658f0484fSRodney W. Grimes while ((ch = getc(hostf)) != '\n' && ch != EOF); 56758f0484fSRodney W. Grimes continue; 56858f0484fSRodney W. Grimes } 569acc7e87cSPeter Wemm if (*p == '\n' || *p == '#') { 570acc7e87cSPeter Wemm /* comment... */ 571acc7e87cSPeter Wemm continue; 572acc7e87cSPeter Wemm } 57358f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 5743fb85bffSAndrey A. Chernov *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 57558f0484fSRodney W. Grimes p++; 57658f0484fSRodney W. Grimes } 57758f0484fSRodney W. Grimes if (*p == ' ' || *p == '\t') { 57858f0484fSRodney W. Grimes *p++ = '\0'; 57958f0484fSRodney W. Grimes while (*p == ' ' || *p == '\t') 58058f0484fSRodney W. Grimes p++; 58158f0484fSRodney W. Grimes user = p; 58258f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && 58358f0484fSRodney W. Grimes *p != '\t' && *p != '\0') 58458f0484fSRodney W. Grimes p++; 58558f0484fSRodney W. Grimes } else 58658f0484fSRodney W. Grimes user = p; 58758f0484fSRodney W. Grimes *p = '\0'; 5888538335fSBill Paul /* 5898538335fSBill Paul * Do +/- and +@/-@ checking. This looks really nasty, 5908538335fSBill Paul * but it matches SunOS's behavior so far as I can tell. 5918538335fSBill Paul */ 5928538335fSBill Paul switch(buf[0]) { 5938538335fSBill Paul case '+': 5948538335fSBill Paul if (!buf[1]) { /* '+' matches all hosts */ 5958538335fSBill Paul hostok = 1; 5968538335fSBill Paul break; 59758f0484fSRodney W. Grimes } 5988538335fSBill Paul if (buf[1] == '@') /* match a host by netgroup */ 59997cb5094SBill Paul hostok = innetgr((char *)&buf[2], 60097cb5094SBill Paul (char *)&hname, NULL, ypdomain); 6018538335fSBill Paul else /* match a host by addr */ 60242b4f28eSYoshinobu Inoue hostok = __icheckhost(raddr,(char *)&buf[1], 60342b4f28eSYoshinobu Inoue af, len); 6048538335fSBill Paul break; 6058538335fSBill Paul case '-': /* reject '-' hosts and all their users */ 6068538335fSBill Paul if (buf[1] == '@') { 6078538335fSBill Paul if (innetgr((char *)&buf[2], 60897cb5094SBill Paul (char *)&hname, NULL, ypdomain)) 6098538335fSBill Paul return(-1); 6108538335fSBill Paul } else { 61142b4f28eSYoshinobu Inoue if (__icheckhost(raddr,(char *)&buf[1],af,len)) 6128538335fSBill Paul return(-1); 6138538335fSBill Paul } 6148538335fSBill Paul break; 6158538335fSBill Paul default: /* if no '+' or '-', do a simple match */ 61642b4f28eSYoshinobu Inoue hostok = __icheckhost(raddr, buf, af, len); 6178538335fSBill Paul break; 6188538335fSBill Paul } 6198538335fSBill Paul switch(*user) { 6208538335fSBill Paul case '+': 6218538335fSBill Paul if (!*(user+1)) { /* '+' matches all users */ 6228538335fSBill Paul userok = 1; 6238538335fSBill Paul break; 6248538335fSBill Paul } 6258538335fSBill Paul if (*(user+1) == '@') /* match a user by netgroup */ 6261e890b05SBill Paul userok = innetgr(user+2, NULL, ruser, ypdomain); 6278538335fSBill Paul else /* match a user by direct specification */ 6288538335fSBill Paul userok = !(strcmp(ruser, user+1)); 6298538335fSBill Paul break; 6308538335fSBill Paul case '-': /* if we matched a hostname, */ 6318538335fSBill Paul if (hostok) { /* check for user field rejections */ 6328538335fSBill Paul if (!*(user+1)) 6338538335fSBill Paul return(-1); 6348538335fSBill Paul if (*(user+1) == '@') { 6358538335fSBill Paul if (innetgr(user+2, NULL, 6361e890b05SBill Paul ruser, ypdomain)) 6378538335fSBill Paul return(-1); 6388538335fSBill Paul } else { 6398538335fSBill Paul if (!strcmp(ruser, user+1)) 6408538335fSBill Paul return(-1); 6418538335fSBill Paul } 6428538335fSBill Paul } 6438538335fSBill Paul break; 6448538335fSBill Paul default: /* no rejections: try to match the user */ 6458538335fSBill Paul if (hostok) 6468538335fSBill Paul userok = !(strcmp(ruser,*user ? user : luser)); 6478538335fSBill Paul break; 6488538335fSBill Paul } 6498538335fSBill Paul if (hostok && userok) 6508538335fSBill Paul return(0); 65158f0484fSRodney W. Grimes } 65258f0484fSRodney W. Grimes return (-1); 65358f0484fSRodney W. Grimes } 65458f0484fSRodney W. Grimes 65558f0484fSRodney W. Grimes /* 65658f0484fSRodney W. Grimes * Returns "true" if match, 0 if no match. 65758f0484fSRodney W. Grimes */ 65858f0484fSRodney W. Grimes static int 65942b4f28eSYoshinobu Inoue __icheckhost(raddr, lhost, af, len) 66042b4f28eSYoshinobu Inoue void *raddr; 66158f0484fSRodney W. Grimes register char *lhost; 66242b4f28eSYoshinobu Inoue int af, len; 66358f0484fSRodney W. Grimes { 66458f0484fSRodney W. Grimes register struct hostent *hp; 66542b4f28eSYoshinobu Inoue char laddr[BUFSIZ]; /* xxx */ 66658f0484fSRodney W. Grimes register char **pp; 66742b4f28eSYoshinobu Inoue int h_error; 66842b4f28eSYoshinobu Inoue int match; 66958f0484fSRodney W. Grimes 67058f0484fSRodney W. Grimes /* Try for raw ip address first. */ 67142b4f28eSYoshinobu Inoue if (inet_pton(af, lhost, laddr) == 1) { 67242b4f28eSYoshinobu Inoue if (memcmp(raddr, laddr, len) == 0) 67342b4f28eSYoshinobu Inoue return (1); 67442b4f28eSYoshinobu Inoue else 67542b4f28eSYoshinobu Inoue return (0); 67642b4f28eSYoshinobu Inoue } 67758f0484fSRodney W. Grimes 67858f0484fSRodney W. Grimes /* Better be a hostname. */ 679e3be4d7bSYoshinobu Inoue if ((hp = getipnodebyname(lhost, af, AI_ALL|AI_DEFAULT, &h_error)) 680e3be4d7bSYoshinobu Inoue == NULL) 68158f0484fSRodney W. Grimes return (0); 68258f0484fSRodney W. Grimes 68358f0484fSRodney W. Grimes /* Spin through ip addresses. */ 68442b4f28eSYoshinobu Inoue match = 0; 68558f0484fSRodney W. Grimes for (pp = hp->h_addr_list; *pp; ++pp) 68642b4f28eSYoshinobu Inoue if (!bcmp(raddr, *pp, len)) { 68742b4f28eSYoshinobu Inoue match = 1; 68842b4f28eSYoshinobu Inoue break; 68942b4f28eSYoshinobu Inoue } 69058f0484fSRodney W. Grimes 69142b4f28eSYoshinobu Inoue freehostent(hp); 69242b4f28eSYoshinobu Inoue return (match); 69358f0484fSRodney W. Grimes } 694