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 40d201fe46SDaniel Eischen #include "namespace.h" 4158f0484fSRodney W. Grimes #include <sys/param.h> 4258f0484fSRodney W. Grimes #include <sys/socket.h> 4358f0484fSRodney W. Grimes #include <sys/stat.h> 4458f0484fSRodney W. Grimes 4558f0484fSRodney W. Grimes #include <netinet/in.h> 4658f0484fSRodney W. Grimes #include <arpa/inet.h> 4758f0484fSRodney W. Grimes 4858f0484fSRodney W. Grimes #include <signal.h> 4958f0484fSRodney W. Grimes #include <fcntl.h> 5058f0484fSRodney W. Grimes #include <netdb.h> 51d4567212SWarner Losh #include <stdlib.h> 5258f0484fSRodney W. Grimes #include <unistd.h> 5358f0484fSRodney W. Grimes #include <pwd.h> 5458f0484fSRodney W. Grimes #include <errno.h> 5558f0484fSRodney W. Grimes #include <stdio.h> 5658f0484fSRodney W. Grimes #include <ctype.h> 5758f0484fSRodney W. Grimes #include <string.h> 581e890b05SBill Paul #ifdef YP 591e890b05SBill Paul #include <rpc/rpc.h> 601e890b05SBill Paul #include <rpcsvc/yp_prot.h> 611e890b05SBill Paul #include <rpcsvc/ypclnt.h> 621e890b05SBill Paul #endif 63c98e299eSHajimu UMEMOTO #include <arpa/nameser.h> 64d201fe46SDaniel Eischen #include "un-namespace.h" 6558f0484fSRodney W. Grimes 6642b4f28eSYoshinobu Inoue /* wrapper for KAME-special getnameinfo() */ 6742b4f28eSYoshinobu Inoue #ifndef NI_WITHSCOPEID 6842b4f28eSYoshinobu Inoue #define NI_WITHSCOPEID 0 6942b4f28eSYoshinobu Inoue #endif 7042b4f28eSYoshinobu Inoue 7151295a4dSJordan K. Hubbard extern int innetgr __P(( const char *, const char *, const char *, const char * )); 7251295a4dSJordan K. Hubbard 73d1f32ba5SGeoff Rehmet #define max(a, b) ((a > b) ? a : b) 74d1f32ba5SGeoff Rehmet 75e0ce825eSDoug Rabson int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *)); 76eb74b996SHajimu UMEMOTO int __ivaliduser_af __P((FILE *,const void *, const char *, const char *, 77eb74b996SHajimu UMEMOTO int, int)); 78eb74b996SHajimu UMEMOTO int __ivaliduser_sa __P((FILE *, const struct sockaddr *, socklen_t, 79eb74b996SHajimu UMEMOTO const char *,const char *)); 80eb74b996SHajimu UMEMOTO static int __icheckhost __P((const struct sockaddr *, socklen_t, 81eb74b996SHajimu UMEMOTO const char *)); 8242b4f28eSYoshinobu Inoue 83eb74b996SHajimu UMEMOTO char paddr[NI_MAXHOST]; 8458f0484fSRodney W. Grimes 8558f0484fSRodney W. Grimes int 8658f0484fSRodney W. Grimes rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 8758f0484fSRodney W. Grimes char **ahost; 8858f0484fSRodney W. Grimes u_short rport; 8958f0484fSRodney W. Grimes const char *locuser, *remuser, *cmd; 9058f0484fSRodney W. Grimes int *fd2p; 9158f0484fSRodney W. Grimes { 920cac72f4SYoshinobu Inoue return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 930cac72f4SYoshinobu Inoue } 940cac72f4SYoshinobu Inoue 950cac72f4SYoshinobu Inoue int 960cac72f4SYoshinobu Inoue rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 970cac72f4SYoshinobu Inoue char **ahost; 980cac72f4SYoshinobu Inoue u_short rport; 990cac72f4SYoshinobu Inoue const char *locuser, *remuser, *cmd; 1000cac72f4SYoshinobu Inoue int *fd2p; 1010cac72f4SYoshinobu Inoue int af; 1020cac72f4SYoshinobu Inoue { 10342b4f28eSYoshinobu Inoue struct addrinfo hints, *res, *ai; 10442b4f28eSYoshinobu Inoue struct sockaddr_storage from; 10558f0484fSRodney W. Grimes fd_set reads; 106d201fe46SDaniel Eischen sigset_t oldmask, newmask; 10758f0484fSRodney W. Grimes pid_t pid; 10842b4f28eSYoshinobu Inoue int s, aport, lport, timo, error; 109d4567212SWarner Losh char c, *p; 110eb74b996SHajimu UMEMOTO int refused, nres; 111c98e299eSHajimu UMEMOTO char num[8]; 112c98e299eSHajimu UMEMOTO static char canonnamebuf[MAXDNAME]; /* is it proper here? */ 11358f0484fSRodney W. Grimes 114d4567212SWarner Losh /* call rcmdsh() with specified remote shell if appropriate. */ 115d4567212SWarner Losh if (!issetugid() && (p = getenv("RSH"))) { 116d4567212SWarner Losh struct servent *sp = getservbyname("shell", "tcp"); 117d4567212SWarner Losh 118d4567212SWarner Losh if (sp && sp->s_port == rport) 119d4567212SWarner Losh return (rcmdsh(ahost, rport, locuser, remuser, 120d4567212SWarner Losh cmd, p)); 121d4567212SWarner Losh } 122d4567212SWarner Losh 123d4567212SWarner Losh /* use rsh(1) if non-root and remote port is shell. */ 124d4567212SWarner Losh if (geteuid()) { 125d4567212SWarner Losh struct servent *sp = getservbyname("shell", "tcp"); 126d4567212SWarner Losh 127d4567212SWarner Losh if (sp && sp->s_port == rport) 128d4567212SWarner Losh return (rcmdsh(ahost, rport, locuser, remuser, 129d4567212SWarner Losh cmd, NULL)); 130d4567212SWarner Losh } 131d4567212SWarner Losh 13258f0484fSRodney W. Grimes pid = getpid(); 13342b4f28eSYoshinobu Inoue 13442b4f28eSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 13542b4f28eSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 1360cac72f4SYoshinobu Inoue hints.ai_family = af; 13742b4f28eSYoshinobu Inoue hints.ai_socktype = SOCK_STREAM; 13842b4f28eSYoshinobu Inoue hints.ai_protocol = 0; 13942b4f28eSYoshinobu Inoue (void)snprintf(num, sizeof(num), "%d", ntohs(rport)); 14042b4f28eSYoshinobu Inoue error = getaddrinfo(*ahost, num, &hints, &res); 14142b4f28eSYoshinobu Inoue if (error) { 14242b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 14342b4f28eSYoshinobu Inoue gai_strerror(error)); 14442b4f28eSYoshinobu Inoue if (error == EAI_SYSTEM) 14542b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 14642b4f28eSYoshinobu Inoue strerror(errno)); 14758f0484fSRodney W. Grimes return (-1); 14858f0484fSRodney W. Grimes } 149c98e299eSHajimu UMEMOTO 150c98e299eSHajimu UMEMOTO if (res->ai_canonname 151c98e299eSHajimu UMEMOTO && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) { 152c98e299eSHajimu UMEMOTO strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf)); 153c98e299eSHajimu UMEMOTO *ahost = canonnamebuf; 154c98e299eSHajimu UMEMOTO } 155eb74b996SHajimu UMEMOTO nres = 0; 156eb74b996SHajimu UMEMOTO for (ai = res; ai; ai = ai->ai_next) 157eb74b996SHajimu UMEMOTO nres++; 15842b4f28eSYoshinobu Inoue ai = res; 159eb74b996SHajimu UMEMOTO refused = 0; 160d201fe46SDaniel Eischen sigemptyset(&newmask); 161d201fe46SDaniel Eischen sigaddset(&newmask, SIGURG); 162d201fe46SDaniel Eischen _sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask); 16358f0484fSRodney W. Grimes for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 16442b4f28eSYoshinobu Inoue s = rresvport_af(&lport, ai->ai_family); 16558f0484fSRodney W. Grimes if (s < 0) { 166474ce1d1SYoshinobu Inoue if (errno != EAGAIN && ai->ai_next) { 167474ce1d1SYoshinobu Inoue ai = ai->ai_next; 168474ce1d1SYoshinobu Inoue continue; 169474ce1d1SYoshinobu Inoue } 17058f0484fSRodney W. Grimes if (errno == EAGAIN) 17158f0484fSRodney W. Grimes (void)fprintf(stderr, 17258f0484fSRodney W. Grimes "rcmd: socket: All ports in use\n"); 17358f0484fSRodney W. Grimes else 17458f0484fSRodney W. Grimes (void)fprintf(stderr, "rcmd: socket: %s\n", 17558f0484fSRodney W. Grimes strerror(errno)); 17642b4f28eSYoshinobu Inoue freeaddrinfo(res); 177d201fe46SDaniel Eischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 178d201fe46SDaniel Eischen NULL); 17958f0484fSRodney W. Grimes return (-1); 18058f0484fSRodney W. Grimes } 1819233c4d9SJason Evans _fcntl(s, F_SETOWN, pid); 182d201fe46SDaniel Eischen if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 18358f0484fSRodney W. Grimes break; 1849233c4d9SJason Evans (void)_close(s); 18558f0484fSRodney W. Grimes if (errno == EADDRINUSE) { 18658f0484fSRodney W. Grimes lport--; 18758f0484fSRodney W. Grimes continue; 18858f0484fSRodney W. Grimes } 189eb74b996SHajimu UMEMOTO if (errno == ECONNREFUSED) 190eb74b996SHajimu UMEMOTO refused = 1; 191eb74b996SHajimu UMEMOTO if (ai->ai_next == NULL && (!refused || timo > 16)) { 192eb74b996SHajimu UMEMOTO (void)fprintf(stderr, "%s: %s\n", 193eb74b996SHajimu UMEMOTO *ahost, strerror(errno)); 194eb74b996SHajimu UMEMOTO freeaddrinfo(res); 195d201fe46SDaniel Eischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 196d201fe46SDaniel Eischen NULL); 197eb74b996SHajimu UMEMOTO return (-1); 1982368b03bSHajimu UMEMOTO } 199eb74b996SHajimu UMEMOTO if (nres > 1) { 20058f0484fSRodney W. Grimes int oerrno = errno; 20158f0484fSRodney W. Grimes 20242b4f28eSYoshinobu Inoue getnameinfo(ai->ai_addr, ai->ai_addrlen, 20342b4f28eSYoshinobu Inoue paddr, sizeof(paddr), 20442b4f28eSYoshinobu Inoue NULL, 0, 20542b4f28eSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 20658f0484fSRodney W. Grimes (void)fprintf(stderr, "connect to address %s: ", 20742b4f28eSYoshinobu Inoue paddr); 20858f0484fSRodney W. Grimes errno = oerrno; 20958f0484fSRodney W. Grimes perror(0); 210eb74b996SHajimu UMEMOTO } 211eb74b996SHajimu UMEMOTO if ((ai = ai->ai_next) == NULL) { 212eb74b996SHajimu UMEMOTO /* refused && timo <= 16 */ 213eb74b996SHajimu UMEMOTO struct timespec time_to_sleep, time_remaining; 214eb74b996SHajimu UMEMOTO 215eb74b996SHajimu UMEMOTO time_to_sleep.tv_sec = timo; 216eb74b996SHajimu UMEMOTO time_to_sleep.tv_nsec = 0; 217eb74b996SHajimu UMEMOTO (void)_nanosleep(&time_to_sleep, &time_remaining); 218eb74b996SHajimu UMEMOTO timo *= 2; 219eb74b996SHajimu UMEMOTO ai = res; 220eb74b996SHajimu UMEMOTO refused = 0; 221eb74b996SHajimu UMEMOTO } 222eb74b996SHajimu UMEMOTO if (nres > 1) { 22342b4f28eSYoshinobu Inoue getnameinfo(ai->ai_addr, ai->ai_addrlen, 22442b4f28eSYoshinobu Inoue paddr, sizeof(paddr), 22542b4f28eSYoshinobu Inoue NULL, 0, 22642b4f28eSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 22742b4f28eSYoshinobu Inoue fprintf(stderr, "Trying %s...\n", paddr); 22858f0484fSRodney W. Grimes } 22958f0484fSRodney W. Grimes } 23058f0484fSRodney W. Grimes lport--; 23158f0484fSRodney W. Grimes if (fd2p == 0) { 2329233c4d9SJason Evans _write(s, "", 1); 23358f0484fSRodney W. Grimes lport = 0; 23458f0484fSRodney W. Grimes } else { 23558f0484fSRodney W. Grimes char num[8]; 23642b4f28eSYoshinobu Inoue int s2 = rresvport_af(&lport, ai->ai_family), s3; 23742b4f28eSYoshinobu Inoue int len = ai->ai_addrlen; 238d1f32ba5SGeoff Rehmet int nfds; 23958f0484fSRodney W. Grimes 24058f0484fSRodney W. Grimes if (s2 < 0) 24158f0484fSRodney W. Grimes goto bad; 242d201fe46SDaniel Eischen _listen(s2, 1); 24358f0484fSRodney W. Grimes (void)snprintf(num, sizeof(num), "%d", lport); 2449233c4d9SJason Evans if (_write(s, num, strlen(num)+1) != strlen(num)+1) { 24558f0484fSRodney W. Grimes (void)fprintf(stderr, 24658f0484fSRodney W. Grimes "rcmd: write (setting up stderr): %s\n", 24758f0484fSRodney W. Grimes strerror(errno)); 2489233c4d9SJason Evans (void)_close(s2); 24958f0484fSRodney W. Grimes goto bad; 25058f0484fSRodney W. Grimes } 251d1f32ba5SGeoff Rehmet nfds = max(s, s2)+1; 252d1f32ba5SGeoff Rehmet if(nfds > FD_SETSIZE) { 253d1f32ba5SGeoff Rehmet fprintf(stderr, "rcmd: too many files\n"); 2549233c4d9SJason Evans (void)_close(s2); 255d1f32ba5SGeoff Rehmet goto bad; 256d1f32ba5SGeoff Rehmet } 257ce70b6caSPeter Wemm again: 25858f0484fSRodney W. Grimes FD_ZERO(&reads); 25958f0484fSRodney W. Grimes FD_SET(s, &reads); 26058f0484fSRodney W. Grimes FD_SET(s2, &reads); 26158f0484fSRodney W. Grimes errno = 0; 262d201fe46SDaniel Eischen if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 26358f0484fSRodney W. Grimes if (errno != 0) 26458f0484fSRodney W. Grimes (void)fprintf(stderr, 26558f0484fSRodney W. Grimes "rcmd: select (setting up stderr): %s\n", 26658f0484fSRodney W. Grimes strerror(errno)); 26758f0484fSRodney W. Grimes else 26858f0484fSRodney W. Grimes (void)fprintf(stderr, 26958f0484fSRodney W. Grimes "select: protocol failure in circuit setup\n"); 2709233c4d9SJason Evans (void)_close(s2); 27158f0484fSRodney W. Grimes goto bad; 27258f0484fSRodney W. Grimes } 273d201fe46SDaniel Eischen s3 = _accept(s2, (struct sockaddr *)&from, &len); 27442b4f28eSYoshinobu Inoue switch (from.ss_family) { 27542b4f28eSYoshinobu Inoue case AF_INET: 27642b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 27742b4f28eSYoshinobu Inoue break; 27842b4f28eSYoshinobu Inoue #ifdef INET6 27942b4f28eSYoshinobu Inoue case AF_INET6: 28042b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 28142b4f28eSYoshinobu Inoue break; 28242b4f28eSYoshinobu Inoue #endif 28342b4f28eSYoshinobu Inoue default: 28442b4f28eSYoshinobu Inoue aport = 0; /* error */ 28542b4f28eSYoshinobu Inoue break; 28642b4f28eSYoshinobu Inoue } 287ce70b6caSPeter Wemm /* 288ce70b6caSPeter Wemm * XXX careful for ftp bounce attacks. If discovered, shut them 289ce70b6caSPeter Wemm * down and check for the real auxiliary channel to connect. 290ce70b6caSPeter Wemm */ 29142b4f28eSYoshinobu Inoue if (aport == 20) { 2929233c4d9SJason Evans _close(s3); 293ce70b6caSPeter Wemm goto again; 294ce70b6caSPeter Wemm } 2959233c4d9SJason Evans (void)_close(s2); 29658f0484fSRodney W. Grimes if (s3 < 0) { 29758f0484fSRodney W. Grimes (void)fprintf(stderr, 29858f0484fSRodney W. Grimes "rcmd: accept: %s\n", strerror(errno)); 29958f0484fSRodney W. Grimes lport = 0; 30058f0484fSRodney W. Grimes goto bad; 30158f0484fSRodney W. Grimes } 30258f0484fSRodney W. Grimes *fd2p = s3; 30342b4f28eSYoshinobu Inoue if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 30458f0484fSRodney W. Grimes (void)fprintf(stderr, 30558f0484fSRodney W. Grimes "socket: protocol failure in circuit setup.\n"); 30658f0484fSRodney W. Grimes goto bad2; 30758f0484fSRodney W. Grimes } 30858f0484fSRodney W. Grimes } 3099233c4d9SJason Evans (void)_write(s, locuser, strlen(locuser)+1); 3109233c4d9SJason Evans (void)_write(s, remuser, strlen(remuser)+1); 3119233c4d9SJason Evans (void)_write(s, cmd, strlen(cmd)+1); 3129233c4d9SJason Evans if (_read(s, &c, 1) != 1) { 31358f0484fSRodney W. Grimes (void)fprintf(stderr, 31458f0484fSRodney W. Grimes "rcmd: %s: %s\n", *ahost, strerror(errno)); 31558f0484fSRodney W. Grimes goto bad2; 31658f0484fSRodney W. Grimes } 31758f0484fSRodney W. Grimes if (c != 0) { 3189233c4d9SJason Evans while (_read(s, &c, 1) == 1) { 3199233c4d9SJason Evans (void)_write(STDERR_FILENO, &c, 1); 32058f0484fSRodney W. Grimes if (c == '\n') 32158f0484fSRodney W. Grimes break; 32258f0484fSRodney W. Grimes } 32358f0484fSRodney W. Grimes goto bad2; 32458f0484fSRodney W. Grimes } 325d201fe46SDaniel Eischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 32642b4f28eSYoshinobu Inoue freeaddrinfo(res); 32758f0484fSRodney W. Grimes return (s); 32858f0484fSRodney W. Grimes bad2: 32958f0484fSRodney W. Grimes if (lport) 3309233c4d9SJason Evans (void)_close(*fd2p); 33158f0484fSRodney W. Grimes bad: 3329233c4d9SJason Evans (void)_close(s); 333d201fe46SDaniel Eischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 33442b4f28eSYoshinobu Inoue freeaddrinfo(res); 33558f0484fSRodney W. Grimes return (-1); 33658f0484fSRodney W. Grimes } 33758f0484fSRodney W. Grimes 33858f0484fSRodney W. Grimes int 33942b4f28eSYoshinobu Inoue rresvport(port) 34042b4f28eSYoshinobu Inoue int *port; 34158f0484fSRodney W. Grimes { 34242b4f28eSYoshinobu Inoue return rresvport_af(port, AF_INET); 34342b4f28eSYoshinobu Inoue } 34458f0484fSRodney W. Grimes 34542b4f28eSYoshinobu Inoue int 34642b4f28eSYoshinobu Inoue rresvport_af(alport, family) 34742b4f28eSYoshinobu Inoue int *alport, family; 34842b4f28eSYoshinobu Inoue { 34942b4f28eSYoshinobu Inoue int i, s, len, err; 35042b4f28eSYoshinobu Inoue struct sockaddr_storage ss; 35142b4f28eSYoshinobu Inoue u_short *sport; 35242b4f28eSYoshinobu Inoue 35342b4f28eSYoshinobu Inoue memset(&ss, 0, sizeof(ss)); 35442b4f28eSYoshinobu Inoue ss.ss_family = family; 35542b4f28eSYoshinobu Inoue switch (family) { 35642b4f28eSYoshinobu Inoue case AF_INET: 35742b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 35842b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in *)&ss)->sin_port; 35942b4f28eSYoshinobu Inoue ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 36042b4f28eSYoshinobu Inoue break; 36142b4f28eSYoshinobu Inoue #ifdef INET6 36242b4f28eSYoshinobu Inoue case AF_INET6: 36342b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 36442b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 36542b4f28eSYoshinobu Inoue ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 36642b4f28eSYoshinobu Inoue break; 36742b4f28eSYoshinobu Inoue #endif 36842b4f28eSYoshinobu Inoue default: 36942b4f28eSYoshinobu Inoue errno = EAFNOSUPPORT; 37042b4f28eSYoshinobu Inoue return -1; 37142b4f28eSYoshinobu Inoue } 37242b4f28eSYoshinobu Inoue 373d201fe46SDaniel Eischen s = _socket(ss.ss_family, SOCK_STREAM, 0); 37458f0484fSRodney W. Grimes if (s < 0) 37558f0484fSRodney W. Grimes return (-1); 376ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */ 377ce70b6caSPeter Wemm sin.sin_port = htons((u_short)*alport); 378d201fe46SDaniel Eischen if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 37958f0484fSRodney W. Grimes return (s); 380ce70b6caSPeter Wemm if (errno != EADDRINUSE) { 3819233c4d9SJason Evans (void)_close(s); 38258f0484fSRodney W. Grimes return (-1); 38358f0484fSRodney W. Grimes } 384ce70b6caSPeter Wemm #endif 38542b4f28eSYoshinobu Inoue *sport = 0; 386ae42b666SYoshinobu Inoue if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { 3879233c4d9SJason Evans (void)_close(s); 388ce70b6caSPeter Wemm return (-1); 389ce70b6caSPeter Wemm } 39042b4f28eSYoshinobu Inoue *alport = (int)ntohs(*sport); 391ce70b6caSPeter Wemm return (s); 392ce70b6caSPeter Wemm } 39358f0484fSRodney W. Grimes 39458f0484fSRodney W. Grimes int __check_rhosts_file = 1; 39558f0484fSRodney W. Grimes char *__rcmd_errstr; 39658f0484fSRodney W. Grimes 39758f0484fSRodney W. Grimes int 39858f0484fSRodney W. Grimes ruserok(rhost, superuser, ruser, luser) 39958f0484fSRodney W. Grimes const char *rhost, *ruser, *luser; 40058f0484fSRodney W. Grimes int superuser; 40158f0484fSRodney W. Grimes { 402e3be4d7bSYoshinobu Inoue struct addrinfo hints, *res, *r; 403e3be4d7bSYoshinobu Inoue int error; 40442b4f28eSYoshinobu Inoue 405e3be4d7bSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 406e3be4d7bSYoshinobu Inoue hints.ai_family = PF_UNSPEC; 407e3be4d7bSYoshinobu Inoue hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 408e3be4d7bSYoshinobu Inoue error = getaddrinfo(rhost, "0", &hints, &res); 409e3be4d7bSYoshinobu Inoue if (error) 41058f0484fSRodney W. Grimes return (-1); 411e3be4d7bSYoshinobu Inoue 412e3be4d7bSYoshinobu Inoue for (r = res; r; r = r->ai_next) { 413e3be4d7bSYoshinobu Inoue if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 414e3be4d7bSYoshinobu Inoue luser) == 0) { 415e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 416e3be4d7bSYoshinobu Inoue return (0); 41742b4f28eSYoshinobu Inoue } 41842b4f28eSYoshinobu Inoue } 419e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 420e3be4d7bSYoshinobu Inoue return (-1); 42158f0484fSRodney W. Grimes } 42258f0484fSRodney W. Grimes 42358f0484fSRodney W. Grimes /* 42458f0484fSRodney W. Grimes * New .rhosts strategy: We are passed an ip address. We spin through 42558f0484fSRodney W. Grimes * hosts.equiv and .rhosts looking for a match. When the .rhosts only 42658f0484fSRodney W. Grimes * has ip addresses, we don't have to trust a nameserver. When it 42758f0484fSRodney W. Grimes * contains hostnames, we spin through the list of addresses the nameserver 42858f0484fSRodney W. Grimes * gives us and look for a match. 42958f0484fSRodney W. Grimes * 43058f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 43158f0484fSRodney W. Grimes */ 43258f0484fSRodney W. Grimes int 43358f0484fSRodney W. Grimes iruserok(raddr, superuser, ruser, luser) 434e0ce825eSDoug Rabson unsigned long raddr; 43558f0484fSRodney W. Grimes int superuser; 43658f0484fSRodney W. Grimes const char *ruser, *luser; 43758f0484fSRodney W. Grimes { 438eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 439eb74b996SHajimu UMEMOTO 440eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 441eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 442eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 443eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 444eb74b996SHajimu UMEMOTO return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser, 445eb74b996SHajimu UMEMOTO ruser, luser); 44642b4f28eSYoshinobu Inoue } 44742b4f28eSYoshinobu Inoue 448eb74b996SHajimu UMEMOTO /* 449eb74b996SHajimu UMEMOTO * AF independent extension of iruserok. 450eb74b996SHajimu UMEMOTO * 451eb74b996SHajimu UMEMOTO * Returns 0 if ok, -1 if not ok. 452eb74b996SHajimu UMEMOTO */ 453eb74b996SHajimu UMEMOTO int 454eb74b996SHajimu UMEMOTO iruserok_sa(ra, rlen, superuser, ruser, luser) 455eb74b996SHajimu UMEMOTO const void *ra; 456eb74b996SHajimu UMEMOTO int rlen; 45742b4f28eSYoshinobu Inoue int superuser; 45842b4f28eSYoshinobu Inoue const char *ruser, *luser; 45942b4f28eSYoshinobu Inoue { 4608fb3f3f6SDavid E. O'Brien char *cp; 46158f0484fSRodney W. Grimes struct stat sbuf; 46258f0484fSRodney W. Grimes struct passwd *pwd; 46358f0484fSRodney W. Grimes FILE *hostf; 46458f0484fSRodney W. Grimes uid_t uid; 46558f0484fSRodney W. Grimes int first; 46658f0484fSRodney W. Grimes char pbuf[MAXPATHLEN]; 467eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 468eb74b996SHajimu UMEMOTO struct sockaddr_storage ss; 46942b4f28eSYoshinobu Inoue 470eb74b996SHajimu UMEMOTO /* avoid alignment issue */ 471eb74b996SHajimu UMEMOTO if (rlen > sizeof(ss)) 472eb74b996SHajimu UMEMOTO return(-1); 473eb74b996SHajimu UMEMOTO memcpy(&ss, ra, rlen); 474eb74b996SHajimu UMEMOTO raddr = (struct sockaddr *)&ss; 47558f0484fSRodney W. Grimes 47658f0484fSRodney W. Grimes first = 1; 47758f0484fSRodney W. Grimes hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 47858f0484fSRodney W. Grimes again: 47958f0484fSRodney W. Grimes if (hostf) { 480eb74b996SHajimu UMEMOTO if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) { 48158f0484fSRodney W. Grimes (void)fclose(hostf); 48258f0484fSRodney W. Grimes return (0); 48358f0484fSRodney W. Grimes } 48458f0484fSRodney W. Grimes (void)fclose(hostf); 48558f0484fSRodney W. Grimes } 48658f0484fSRodney W. Grimes if (first == 1 && (__check_rhosts_file || superuser)) { 48758f0484fSRodney W. Grimes first = 0; 48858f0484fSRodney W. Grimes if ((pwd = getpwnam(luser)) == NULL) 48958f0484fSRodney W. Grimes return (-1); 49058f0484fSRodney W. Grimes (void)strcpy(pbuf, pwd->pw_dir); 49158f0484fSRodney W. Grimes (void)strcat(pbuf, "/.rhosts"); 49258f0484fSRodney W. Grimes 49358f0484fSRodney W. Grimes /* 49458f0484fSRodney W. Grimes * Change effective uid while opening .rhosts. If root and 49558f0484fSRodney W. Grimes * reading an NFS mounted file system, can't read files that 49658f0484fSRodney W. Grimes * are protected read/write owner only. 49758f0484fSRodney W. Grimes */ 49858f0484fSRodney W. Grimes uid = geteuid(); 49958f0484fSRodney W. Grimes (void)seteuid(pwd->pw_uid); 50058f0484fSRodney W. Grimes hostf = fopen(pbuf, "r"); 50158f0484fSRodney W. Grimes (void)seteuid(uid); 50258f0484fSRodney W. Grimes 50358f0484fSRodney W. Grimes if (hostf == NULL) 50458f0484fSRodney W. Grimes return (-1); 50558f0484fSRodney W. Grimes /* 50658f0484fSRodney W. Grimes * If not a regular file, or is owned by someone other than 50758f0484fSRodney W. Grimes * user or root or if writeable by anyone but the owner, quit. 50858f0484fSRodney W. Grimes */ 50958f0484fSRodney W. Grimes cp = NULL; 51058f0484fSRodney W. Grimes if (lstat(pbuf, &sbuf) < 0) 51158f0484fSRodney W. Grimes cp = ".rhosts lstat failed"; 51258f0484fSRodney W. Grimes else if (!S_ISREG(sbuf.st_mode)) 51358f0484fSRodney W. Grimes cp = ".rhosts not regular file"; 514d201fe46SDaniel Eischen else if (_fstat(fileno(hostf), &sbuf) < 0) 51558f0484fSRodney W. Grimes cp = ".rhosts fstat failed"; 51658f0484fSRodney W. Grimes else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 51758f0484fSRodney W. Grimes cp = "bad .rhosts owner"; 51858f0484fSRodney W. Grimes else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 51958f0484fSRodney W. Grimes cp = ".rhosts writeable by other than owner"; 52058f0484fSRodney W. Grimes /* If there were any problems, quit. */ 52158f0484fSRodney W. Grimes if (cp) { 52258f0484fSRodney W. Grimes __rcmd_errstr = cp; 52358f0484fSRodney W. Grimes (void)fclose(hostf); 52458f0484fSRodney W. Grimes return (-1); 52558f0484fSRodney W. Grimes } 52658f0484fSRodney W. Grimes goto again; 52758f0484fSRodney W. Grimes } 52858f0484fSRodney W. Grimes return (-1); 52958f0484fSRodney W. Grimes } 53058f0484fSRodney W. Grimes 53158f0484fSRodney W. Grimes /* 53258f0484fSRodney W. Grimes * XXX 53358f0484fSRodney W. Grimes * Don't make static, used by lpd(8). 53458f0484fSRodney W. Grimes * 53558f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 53658f0484fSRodney W. Grimes */ 53758f0484fSRodney W. Grimes int 53858f0484fSRodney W. Grimes __ivaliduser(hostf, raddr, luser, ruser) 53958f0484fSRodney W. Grimes FILE *hostf; 540e0ce825eSDoug Rabson u_int32_t raddr; 54158f0484fSRodney W. Grimes const char *luser, *ruser; 54258f0484fSRodney W. Grimes { 543eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 544eb74b996SHajimu UMEMOTO 545eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 546eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 547eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 548eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 549eb74b996SHajimu UMEMOTO return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, 550eb74b996SHajimu UMEMOTO luser, ruser); 55142b4f28eSYoshinobu Inoue } 55242b4f28eSYoshinobu Inoue 553eb74b996SHajimu UMEMOTO /* 554eb74b996SHajimu UMEMOTO * Returns 0 if ok, -1 if not ok. 555eb74b996SHajimu UMEMOTO * 556eb74b996SHajimu UMEMOTO * XXX obsolete API. 557eb74b996SHajimu UMEMOTO */ 55842b4f28eSYoshinobu Inoue int 55942b4f28eSYoshinobu Inoue __ivaliduser_af(hostf, raddr, luser, ruser, af, len) 56042b4f28eSYoshinobu Inoue FILE *hostf; 561eb74b996SHajimu UMEMOTO const void *raddr; 56242b4f28eSYoshinobu Inoue const char *luser, *ruser; 56342b4f28eSYoshinobu Inoue int af, len; 56442b4f28eSYoshinobu Inoue { 565eb74b996SHajimu UMEMOTO struct sockaddr *sa = NULL; 566eb74b996SHajimu UMEMOTO struct sockaddr_in *sin = NULL; 567eb74b996SHajimu UMEMOTO #ifdef INET6 568eb74b996SHajimu UMEMOTO struct sockaddr_in6 *sin6 = NULL; 569eb74b996SHajimu UMEMOTO #endif 570eb74b996SHajimu UMEMOTO struct sockaddr_storage ss; 571eb74b996SHajimu UMEMOTO 572eb74b996SHajimu UMEMOTO memset(&ss, 0, sizeof(ss)); 573eb74b996SHajimu UMEMOTO switch (af) { 574eb74b996SHajimu UMEMOTO case AF_INET: 575eb74b996SHajimu UMEMOTO if (len != sizeof(sin->sin_addr)) 576eb74b996SHajimu UMEMOTO return -1; 577eb74b996SHajimu UMEMOTO sin = (struct sockaddr_in *)&ss; 578eb74b996SHajimu UMEMOTO sin->sin_family = AF_INET; 579eb74b996SHajimu UMEMOTO sin->sin_len = sizeof(struct sockaddr_in); 580eb74b996SHajimu UMEMOTO memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr)); 581eb74b996SHajimu UMEMOTO break; 582eb74b996SHajimu UMEMOTO #ifdef INET6 583eb74b996SHajimu UMEMOTO case AF_INET6: 584eb74b996SHajimu UMEMOTO if (len != sizeof(sin6->sin6_addr)) 585eb74b996SHajimu UMEMOTO return -1; 586eb74b996SHajimu UMEMOTO /* you will lose scope info */ 587eb74b996SHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)&ss; 588eb74b996SHajimu UMEMOTO sin6->sin6_family = AF_INET6; 589eb74b996SHajimu UMEMOTO sin6->sin6_len = sizeof(struct sockaddr_in6); 590eb74b996SHajimu UMEMOTO memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr)); 591eb74b996SHajimu UMEMOTO break; 592eb74b996SHajimu UMEMOTO #endif 593eb74b996SHajimu UMEMOTO default: 594eb74b996SHajimu UMEMOTO return -1; 595eb74b996SHajimu UMEMOTO } 596eb74b996SHajimu UMEMOTO 597eb74b996SHajimu UMEMOTO sa = (struct sockaddr *)&ss; 598eb74b996SHajimu UMEMOTO return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser); 599eb74b996SHajimu UMEMOTO } 600eb74b996SHajimu UMEMOTO 601eb74b996SHajimu UMEMOTO int 602eb74b996SHajimu UMEMOTO __ivaliduser_sa(hostf, raddr, salen, luser, ruser) 603eb74b996SHajimu UMEMOTO FILE *hostf; 604eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 605eb74b996SHajimu UMEMOTO socklen_t salen; 606eb74b996SHajimu UMEMOTO const char *luser, *ruser; 607eb74b996SHajimu UMEMOTO { 6088fb3f3f6SDavid E. O'Brien char *user, *p; 60958f0484fSRodney W. Grimes int ch; 61058f0484fSRodney W. Grimes char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 61197cb5094SBill Paul char hname[MAXHOSTNAMELEN]; 6128538335fSBill Paul /* Presumed guilty until proven innocent. */ 6138538335fSBill Paul int userok = 0, hostok = 0; 61442b4f28eSYoshinobu Inoue int h_error; 6151e890b05SBill Paul #ifdef YP 6161e890b05SBill Paul char *ypdomain; 6178538335fSBill Paul 6181e890b05SBill Paul if (yp_get_default_domain(&ypdomain)) 6191e890b05SBill Paul ypdomain = NULL; 6201e890b05SBill Paul #else 6211e890b05SBill Paul #define ypdomain NULL 6221e890b05SBill Paul #endif 6238538335fSBill Paul /* We need to get the damn hostname back for netgroup matching. */ 624eb74b996SHajimu UMEMOTO if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0, 625eb74b996SHajimu UMEMOTO NI_NAMEREQD) != 0) 6268538335fSBill Paul return (-1); 62758f0484fSRodney W. Grimes 62858f0484fSRodney W. Grimes while (fgets(buf, sizeof(buf), hostf)) { 62958f0484fSRodney W. Grimes p = buf; 63058f0484fSRodney W. Grimes /* Skip lines that are too long. */ 631a9f9141cSBrian S. Dean if (strchr(p, '\n') == NULL) { 63258f0484fSRodney W. Grimes while ((ch = getc(hostf)) != '\n' && ch != EOF); 63358f0484fSRodney W. Grimes continue; 63458f0484fSRodney W. Grimes } 635acc7e87cSPeter Wemm if (*p == '\n' || *p == '#') { 636acc7e87cSPeter Wemm /* comment... */ 637acc7e87cSPeter Wemm continue; 638acc7e87cSPeter Wemm } 63958f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 6403fb85bffSAndrey A. Chernov *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 64158f0484fSRodney W. Grimes p++; 64258f0484fSRodney W. Grimes } 64358f0484fSRodney W. Grimes if (*p == ' ' || *p == '\t') { 64458f0484fSRodney W. Grimes *p++ = '\0'; 64558f0484fSRodney W. Grimes while (*p == ' ' || *p == '\t') 64658f0484fSRodney W. Grimes p++; 64758f0484fSRodney W. Grimes user = p; 64858f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && 64958f0484fSRodney W. Grimes *p != '\t' && *p != '\0') 65058f0484fSRodney W. Grimes p++; 65158f0484fSRodney W. Grimes } else 65258f0484fSRodney W. Grimes user = p; 65358f0484fSRodney W. Grimes *p = '\0'; 6548538335fSBill Paul /* 6558538335fSBill Paul * Do +/- and +@/-@ checking. This looks really nasty, 6568538335fSBill Paul * but it matches SunOS's behavior so far as I can tell. 6578538335fSBill Paul */ 6588538335fSBill Paul switch(buf[0]) { 6598538335fSBill Paul case '+': 6608538335fSBill Paul if (!buf[1]) { /* '+' matches all hosts */ 6618538335fSBill Paul hostok = 1; 6628538335fSBill Paul break; 66358f0484fSRodney W. Grimes } 6648538335fSBill Paul if (buf[1] == '@') /* match a host by netgroup */ 66597cb5094SBill Paul hostok = innetgr((char *)&buf[2], 66697cb5094SBill Paul (char *)&hname, NULL, ypdomain); 6678538335fSBill Paul else /* match a host by addr */ 668eb74b996SHajimu UMEMOTO hostok = __icheckhost(raddr, salen, 669eb74b996SHajimu UMEMOTO (char *)&buf[1]); 6708538335fSBill Paul break; 6718538335fSBill Paul case '-': /* reject '-' hosts and all their users */ 6728538335fSBill Paul if (buf[1] == '@') { 6738538335fSBill Paul if (innetgr((char *)&buf[2], 67497cb5094SBill Paul (char *)&hname, NULL, ypdomain)) 6758538335fSBill Paul return(-1); 6768538335fSBill Paul } else { 677eb74b996SHajimu UMEMOTO if (__icheckhost(raddr, salen, 678eb74b996SHajimu UMEMOTO (char *)&buf[1])) 6798538335fSBill Paul return(-1); 6808538335fSBill Paul } 6818538335fSBill Paul break; 6828538335fSBill Paul default: /* if no '+' or '-', do a simple match */ 683eb74b996SHajimu UMEMOTO hostok = __icheckhost(raddr, salen, buf); 6848538335fSBill Paul break; 6858538335fSBill Paul } 6868538335fSBill Paul switch(*user) { 6878538335fSBill Paul case '+': 6888538335fSBill Paul if (!*(user+1)) { /* '+' matches all users */ 6898538335fSBill Paul userok = 1; 6908538335fSBill Paul break; 6918538335fSBill Paul } 6928538335fSBill Paul if (*(user+1) == '@') /* match a user by netgroup */ 6931e890b05SBill Paul userok = innetgr(user+2, NULL, ruser, ypdomain); 6948538335fSBill Paul else /* match a user by direct specification */ 6958538335fSBill Paul userok = !(strcmp(ruser, user+1)); 6968538335fSBill Paul break; 6978538335fSBill Paul case '-': /* if we matched a hostname, */ 6988538335fSBill Paul if (hostok) { /* check for user field rejections */ 6998538335fSBill Paul if (!*(user+1)) 7008538335fSBill Paul return(-1); 7018538335fSBill Paul if (*(user+1) == '@') { 7028538335fSBill Paul if (innetgr(user+2, NULL, 7031e890b05SBill Paul ruser, ypdomain)) 7048538335fSBill Paul return(-1); 7058538335fSBill Paul } else { 7068538335fSBill Paul if (!strcmp(ruser, user+1)) 7078538335fSBill Paul return(-1); 7088538335fSBill Paul } 7098538335fSBill Paul } 7108538335fSBill Paul break; 7118538335fSBill Paul default: /* no rejections: try to match the user */ 7128538335fSBill Paul if (hostok) 7138538335fSBill Paul userok = !(strcmp(ruser,*user ? user : luser)); 7148538335fSBill Paul break; 7158538335fSBill Paul } 7168538335fSBill Paul if (hostok && userok) 7178538335fSBill Paul return(0); 71858f0484fSRodney W. Grimes } 71958f0484fSRodney W. Grimes return (-1); 72058f0484fSRodney W. Grimes } 72158f0484fSRodney W. Grimes 72258f0484fSRodney W. Grimes /* 72358f0484fSRodney W. Grimes * Returns "true" if match, 0 if no match. 724eb74b996SHajimu UMEMOTO * 725eb74b996SHajimu UMEMOTO * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion 726eb74b996SHajimu UMEMOTO * if af == AF_INET6. 72758f0484fSRodney W. Grimes */ 72858f0484fSRodney W. Grimes static int 729eb74b996SHajimu UMEMOTO __icheckhost(raddr, salen, lhost) 730eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 731eb74b996SHajimu UMEMOTO socklen_t salen; 732eb74b996SHajimu UMEMOTO const char *lhost; 73358f0484fSRodney W. Grimes { 734eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 735eb74b996SHajimu UMEMOTO struct sockaddr_in6 *sin6; 736eb74b996SHajimu UMEMOTO struct addrinfo hints, *res, *r; 737eb74b996SHajimu UMEMOTO int error; 738eb74b996SHajimu UMEMOTO char h1[NI_MAXHOST], h2[NI_MAXHOST]; 73958f0484fSRodney W. Grimes 740eb74b996SHajimu UMEMOTO if (raddr->sa_family == AF_INET6) { 741eb74b996SHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)raddr; 742eb74b996SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 743eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 744eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 745eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 746eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12], 747eb74b996SHajimu UMEMOTO sizeof(sin.sin_addr)); 748eb74b996SHajimu UMEMOTO raddr = (struct sockaddr *)&sin; 749eb74b996SHajimu UMEMOTO salen = sin.sin_len; 750eb74b996SHajimu UMEMOTO } 751eb74b996SHajimu UMEMOTO } 752eb74b996SHajimu UMEMOTO 753eb74b996SHajimu UMEMOTO h1[0] = '\0'; 754eb74b996SHajimu UMEMOTO if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 755eb74b996SHajimu UMEMOTO NI_NUMERICHOST | NI_WITHSCOPEID) != 0) 756eb74b996SHajimu UMEMOTO return (0); 757eb74b996SHajimu UMEMOTO 758eb74b996SHajimu UMEMOTO /* Resolve laddr into sockaddr */ 759eb74b996SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 760eb74b996SHajimu UMEMOTO hints.ai_family = raddr->sa_family; 761eb74b996SHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/ 762eb74b996SHajimu UMEMOTO res = NULL; 763eb74b996SHajimu UMEMOTO error = getaddrinfo(lhost, "0", &hints, &res); 764eb74b996SHajimu UMEMOTO if (error) 765eb74b996SHajimu UMEMOTO return (0); 766eb74b996SHajimu UMEMOTO 767eb74b996SHajimu UMEMOTO for (r = res; r ; r = r->ai_next) { 768eb74b996SHajimu UMEMOTO h2[0] = '\0'; 769eb74b996SHajimu UMEMOTO if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 770eb74b996SHajimu UMEMOTO NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0) 771eb74b996SHajimu UMEMOTO continue; 772eb74b996SHajimu UMEMOTO if (strcmp(h1, h2) == 0) { 773eb74b996SHajimu UMEMOTO freeaddrinfo(res); 77442b4f28eSYoshinobu Inoue return (1); 775eb74b996SHajimu UMEMOTO } 77642b4f28eSYoshinobu Inoue } 77758f0484fSRodney W. Grimes 778eb74b996SHajimu UMEMOTO /* No match. */ 779eb74b996SHajimu UMEMOTO freeaddrinfo(res); 78058f0484fSRodney W. Grimes return (0); 78158f0484fSRodney W. Grimes } 782