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. 3258f0484fSRodney W. Grimes */ 3358f0484fSRodney W. Grimes 3458f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3558f0484fSRodney W. Grimes static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 3658f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 37333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 38333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 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 #include <rpc/rpc.h> 596c58990dSBjoern A. Zeeb #ifdef YP 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 71c05ac53bSDavid E. O'Brien extern int innetgr( const char *, const char *, const char *, const char * ); 7251295a4dSJordan K. Hubbard 73d1f32ba5SGeoff Rehmet #define max(a, b) ((a > b) ? a : b) 74d1f32ba5SGeoff Rehmet 75c05ac53bSDavid E. O'Brien int __ivaliduser(FILE *, u_int32_t, const char *, const char *); 761372519bSDavid E. O'Brien int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int); 771372519bSDavid E. O'Brien int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *, 781372519bSDavid E. O'Brien const char *); 791372519bSDavid E. O'Brien static int __icheckhost(const struct sockaddr *, socklen_t, const char *); 8042b4f28eSYoshinobu Inoue 81eb74b996SHajimu UMEMOTO char paddr[NI_MAXHOST]; 8258f0484fSRodney W. Grimes 8358f0484fSRodney W. Grimes int 8458f0484fSRodney W. Grimes rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 8558f0484fSRodney W. Grimes char **ahost; 8658f0484fSRodney W. Grimes u_short rport; 8758f0484fSRodney W. Grimes const char *locuser, *remuser, *cmd; 8858f0484fSRodney W. Grimes int *fd2p; 8958f0484fSRodney W. Grimes { 900cac72f4SYoshinobu Inoue return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 910cac72f4SYoshinobu Inoue } 920cac72f4SYoshinobu Inoue 930cac72f4SYoshinobu Inoue int 940cac72f4SYoshinobu Inoue rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 950cac72f4SYoshinobu Inoue char **ahost; 960cac72f4SYoshinobu Inoue u_short rport; 970cac72f4SYoshinobu Inoue const char *locuser, *remuser, *cmd; 980cac72f4SYoshinobu Inoue int *fd2p; 990cac72f4SYoshinobu Inoue int af; 1000cac72f4SYoshinobu Inoue { 10142b4f28eSYoshinobu Inoue struct addrinfo hints, *res, *ai; 10242b4f28eSYoshinobu Inoue struct sockaddr_storage from; 10358f0484fSRodney W. Grimes fd_set reads; 104d201fe46SDaniel Eischen sigset_t oldmask, newmask; 10558f0484fSRodney W. Grimes pid_t pid; 10642b4f28eSYoshinobu Inoue int s, aport, lport, timo, error; 107d4567212SWarner Losh char c, *p; 108eb74b996SHajimu UMEMOTO int refused, nres; 109c98e299eSHajimu UMEMOTO char num[8]; 110c98e299eSHajimu UMEMOTO static char canonnamebuf[MAXDNAME]; /* is it proper here? */ 11158f0484fSRodney W. Grimes 112d4567212SWarner Losh /* call rcmdsh() with specified remote shell if appropriate. */ 113d4567212SWarner Losh if (!issetugid() && (p = getenv("RSH"))) { 114d4567212SWarner Losh struct servent *sp = getservbyname("shell", "tcp"); 115d4567212SWarner Losh 116d4567212SWarner Losh if (sp && sp->s_port == rport) 117d4567212SWarner Losh return (rcmdsh(ahost, rport, locuser, remuser, 118d4567212SWarner Losh cmd, p)); 119d4567212SWarner Losh } 120d4567212SWarner Losh 121d4567212SWarner Losh /* use rsh(1) if non-root and remote port is shell. */ 122d4567212SWarner Losh if (geteuid()) { 123d4567212SWarner Losh struct servent *sp = getservbyname("shell", "tcp"); 124d4567212SWarner Losh 125d4567212SWarner Losh if (sp && sp->s_port == rport) 126d4567212SWarner Losh return (rcmdsh(ahost, rport, locuser, remuser, 127d4567212SWarner Losh cmd, NULL)); 128d4567212SWarner Losh } 129d4567212SWarner Losh 13058f0484fSRodney W. Grimes pid = getpid(); 13142b4f28eSYoshinobu Inoue 13242b4f28eSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 13342b4f28eSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 1340cac72f4SYoshinobu Inoue hints.ai_family = af; 13542b4f28eSYoshinobu Inoue hints.ai_socktype = SOCK_STREAM; 13642b4f28eSYoshinobu Inoue hints.ai_protocol = 0; 13742b4f28eSYoshinobu Inoue (void)snprintf(num, sizeof(num), "%d", ntohs(rport)); 13842b4f28eSYoshinobu Inoue error = getaddrinfo(*ahost, num, &hints, &res); 13942b4f28eSYoshinobu Inoue if (error) { 14042b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 14142b4f28eSYoshinobu Inoue gai_strerror(error)); 14242b4f28eSYoshinobu Inoue if (error == EAI_SYSTEM) 14342b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 14442b4f28eSYoshinobu Inoue strerror(errno)); 14558f0484fSRodney W. Grimes return (-1); 14658f0484fSRodney W. Grimes } 147c98e299eSHajimu UMEMOTO 148c98e299eSHajimu UMEMOTO if (res->ai_canonname 149c98e299eSHajimu UMEMOTO && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) { 150c98e299eSHajimu UMEMOTO strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf)); 151c98e299eSHajimu UMEMOTO *ahost = canonnamebuf; 152c98e299eSHajimu UMEMOTO } 153eb74b996SHajimu UMEMOTO nres = 0; 154eb74b996SHajimu UMEMOTO for (ai = res; ai; ai = ai->ai_next) 155eb74b996SHajimu UMEMOTO nres++; 15642b4f28eSYoshinobu Inoue ai = res; 157eb74b996SHajimu UMEMOTO refused = 0; 158d201fe46SDaniel Eischen sigemptyset(&newmask); 159d201fe46SDaniel Eischen sigaddset(&newmask, SIGURG); 160d201fe46SDaniel Eischen _sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask); 16158f0484fSRodney W. Grimes for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 16242b4f28eSYoshinobu Inoue s = rresvport_af(&lport, ai->ai_family); 16358f0484fSRodney W. Grimes if (s < 0) { 164474ce1d1SYoshinobu Inoue if (errno != EAGAIN && ai->ai_next) { 165474ce1d1SYoshinobu Inoue ai = ai->ai_next; 166474ce1d1SYoshinobu Inoue continue; 167474ce1d1SYoshinobu Inoue } 16858f0484fSRodney W. Grimes if (errno == EAGAIN) 16958f0484fSRodney W. Grimes (void)fprintf(stderr, 17058f0484fSRodney W. Grimes "rcmd: socket: All ports in use\n"); 17158f0484fSRodney W. Grimes else 17258f0484fSRodney W. Grimes (void)fprintf(stderr, "rcmd: socket: %s\n", 17358f0484fSRodney W. Grimes strerror(errno)); 17442b4f28eSYoshinobu Inoue freeaddrinfo(res); 175d201fe46SDaniel Eischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 176d201fe46SDaniel Eischen NULL); 17758f0484fSRodney W. Grimes return (-1); 17858f0484fSRodney W. Grimes } 1799233c4d9SJason Evans _fcntl(s, F_SETOWN, pid); 180d201fe46SDaniel Eischen if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 18158f0484fSRodney W. Grimes break; 1829233c4d9SJason Evans (void)_close(s); 18358f0484fSRodney W. Grimes if (errno == EADDRINUSE) { 18458f0484fSRodney W. Grimes lport--; 18558f0484fSRodney W. Grimes continue; 18658f0484fSRodney W. Grimes } 187eb74b996SHajimu UMEMOTO if (errno == ECONNREFUSED) 188eb74b996SHajimu UMEMOTO refused = 1; 189eb74b996SHajimu UMEMOTO if (ai->ai_next == NULL && (!refused || timo > 16)) { 190eb74b996SHajimu UMEMOTO (void)fprintf(stderr, "%s: %s\n", 191eb74b996SHajimu UMEMOTO *ahost, strerror(errno)); 192eb74b996SHajimu UMEMOTO freeaddrinfo(res); 193d201fe46SDaniel Eischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 194d201fe46SDaniel Eischen NULL); 195eb74b996SHajimu UMEMOTO return (-1); 1962368b03bSHajimu UMEMOTO } 197eb74b996SHajimu UMEMOTO if (nres > 1) { 19858f0484fSRodney W. Grimes int oerrno = errno; 19958f0484fSRodney W. Grimes 20042b4f28eSYoshinobu Inoue getnameinfo(ai->ai_addr, ai->ai_addrlen, 20142b4f28eSYoshinobu Inoue paddr, sizeof(paddr), 20242b4f28eSYoshinobu Inoue NULL, 0, 20342b4f28eSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 20458f0484fSRodney W. Grimes (void)fprintf(stderr, "connect to address %s: ", 20542b4f28eSYoshinobu Inoue paddr); 20658f0484fSRodney W. Grimes errno = oerrno; 20758f0484fSRodney W. Grimes perror(0); 208eb74b996SHajimu UMEMOTO } 209eb74b996SHajimu UMEMOTO if ((ai = ai->ai_next) == NULL) { 210eb74b996SHajimu UMEMOTO /* refused && timo <= 16 */ 211eb74b996SHajimu UMEMOTO struct timespec time_to_sleep, time_remaining; 212eb74b996SHajimu UMEMOTO 213eb74b996SHajimu UMEMOTO time_to_sleep.tv_sec = timo; 214eb74b996SHajimu UMEMOTO time_to_sleep.tv_nsec = 0; 215eb74b996SHajimu UMEMOTO (void)_nanosleep(&time_to_sleep, &time_remaining); 216eb74b996SHajimu UMEMOTO timo *= 2; 217eb74b996SHajimu UMEMOTO ai = res; 218eb74b996SHajimu UMEMOTO refused = 0; 219eb74b996SHajimu UMEMOTO } 220eb74b996SHajimu UMEMOTO if (nres > 1) { 22142b4f28eSYoshinobu Inoue getnameinfo(ai->ai_addr, ai->ai_addrlen, 22242b4f28eSYoshinobu Inoue paddr, sizeof(paddr), 22342b4f28eSYoshinobu Inoue NULL, 0, 22442b4f28eSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 22542b4f28eSYoshinobu Inoue fprintf(stderr, "Trying %s...\n", paddr); 22658f0484fSRodney W. Grimes } 22758f0484fSRodney W. Grimes } 22858f0484fSRodney W. Grimes lport--; 22958f0484fSRodney W. Grimes if (fd2p == 0) { 2309233c4d9SJason Evans _write(s, "", 1); 23158f0484fSRodney W. Grimes lport = 0; 23258f0484fSRodney W. Grimes } else { 23358f0484fSRodney W. Grimes char num[8]; 23442b4f28eSYoshinobu Inoue int s2 = rresvport_af(&lport, ai->ai_family), s3; 23542b4f28eSYoshinobu Inoue int len = ai->ai_addrlen; 236d1f32ba5SGeoff Rehmet int nfds; 23758f0484fSRodney W. Grimes 23858f0484fSRodney W. Grimes if (s2 < 0) 23958f0484fSRodney W. Grimes goto bad; 240d201fe46SDaniel Eischen _listen(s2, 1); 24158f0484fSRodney W. Grimes (void)snprintf(num, sizeof(num), "%d", lport); 2429233c4d9SJason Evans if (_write(s, num, strlen(num)+1) != strlen(num)+1) { 24358f0484fSRodney W. Grimes (void)fprintf(stderr, 24458f0484fSRodney W. Grimes "rcmd: write (setting up stderr): %s\n", 24558f0484fSRodney W. Grimes strerror(errno)); 2469233c4d9SJason Evans (void)_close(s2); 24758f0484fSRodney W. Grimes goto bad; 24858f0484fSRodney W. Grimes } 249d1f32ba5SGeoff Rehmet nfds = max(s, s2)+1; 250d1f32ba5SGeoff Rehmet if(nfds > FD_SETSIZE) { 251d1f32ba5SGeoff Rehmet fprintf(stderr, "rcmd: too many files\n"); 2529233c4d9SJason Evans (void)_close(s2); 253d1f32ba5SGeoff Rehmet goto bad; 254d1f32ba5SGeoff Rehmet } 255ce70b6caSPeter Wemm again: 25658f0484fSRodney W. Grimes FD_ZERO(&reads); 25758f0484fSRodney W. Grimes FD_SET(s, &reads); 25858f0484fSRodney W. Grimes FD_SET(s2, &reads); 25958f0484fSRodney W. Grimes errno = 0; 260d201fe46SDaniel Eischen if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 26158f0484fSRodney W. Grimes if (errno != 0) 26258f0484fSRodney W. Grimes (void)fprintf(stderr, 26358f0484fSRodney W. Grimes "rcmd: select (setting up stderr): %s\n", 26458f0484fSRodney W. Grimes strerror(errno)); 26558f0484fSRodney W. Grimes else 26658f0484fSRodney W. Grimes (void)fprintf(stderr, 26758f0484fSRodney W. Grimes "select: protocol failure in circuit setup\n"); 2689233c4d9SJason Evans (void)_close(s2); 26958f0484fSRodney W. Grimes goto bad; 27058f0484fSRodney W. Grimes } 271d201fe46SDaniel Eischen s3 = _accept(s2, (struct sockaddr *)&from, &len); 27242b4f28eSYoshinobu Inoue switch (from.ss_family) { 27342b4f28eSYoshinobu Inoue case AF_INET: 27442b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 27542b4f28eSYoshinobu Inoue break; 27642b4f28eSYoshinobu Inoue #ifdef INET6 27742b4f28eSYoshinobu Inoue case AF_INET6: 27842b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 27942b4f28eSYoshinobu Inoue break; 28042b4f28eSYoshinobu Inoue #endif 28142b4f28eSYoshinobu Inoue default: 28242b4f28eSYoshinobu Inoue aport = 0; /* error */ 28342b4f28eSYoshinobu Inoue break; 28442b4f28eSYoshinobu Inoue } 285ce70b6caSPeter Wemm /* 286ce70b6caSPeter Wemm * XXX careful for ftp bounce attacks. If discovered, shut them 287ce70b6caSPeter Wemm * down and check for the real auxiliary channel to connect. 288ce70b6caSPeter Wemm */ 28942b4f28eSYoshinobu Inoue if (aport == 20) { 2909233c4d9SJason Evans _close(s3); 291ce70b6caSPeter Wemm goto again; 292ce70b6caSPeter Wemm } 2939233c4d9SJason Evans (void)_close(s2); 29458f0484fSRodney W. Grimes if (s3 < 0) { 29558f0484fSRodney W. Grimes (void)fprintf(stderr, 29658f0484fSRodney W. Grimes "rcmd: accept: %s\n", strerror(errno)); 29758f0484fSRodney W. Grimes lport = 0; 29858f0484fSRodney W. Grimes goto bad; 29958f0484fSRodney W. Grimes } 30058f0484fSRodney W. Grimes *fd2p = s3; 30142b4f28eSYoshinobu Inoue if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 30258f0484fSRodney W. Grimes (void)fprintf(stderr, 30358f0484fSRodney W. Grimes "socket: protocol failure in circuit setup.\n"); 30458f0484fSRodney W. Grimes goto bad2; 30558f0484fSRodney W. Grimes } 30658f0484fSRodney W. Grimes } 3079233c4d9SJason Evans (void)_write(s, locuser, strlen(locuser)+1); 3089233c4d9SJason Evans (void)_write(s, remuser, strlen(remuser)+1); 3099233c4d9SJason Evans (void)_write(s, cmd, strlen(cmd)+1); 3109233c4d9SJason Evans if (_read(s, &c, 1) != 1) { 31158f0484fSRodney W. Grimes (void)fprintf(stderr, 31258f0484fSRodney W. Grimes "rcmd: %s: %s\n", *ahost, strerror(errno)); 31358f0484fSRodney W. Grimes goto bad2; 31458f0484fSRodney W. Grimes } 31558f0484fSRodney W. Grimes if (c != 0) { 3169233c4d9SJason Evans while (_read(s, &c, 1) == 1) { 3179233c4d9SJason Evans (void)_write(STDERR_FILENO, &c, 1); 31858f0484fSRodney W. Grimes if (c == '\n') 31958f0484fSRodney W. Grimes break; 32058f0484fSRodney W. Grimes } 32158f0484fSRodney W. Grimes goto bad2; 32258f0484fSRodney W. Grimes } 323d201fe46SDaniel Eischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 32442b4f28eSYoshinobu Inoue freeaddrinfo(res); 32558f0484fSRodney W. Grimes return (s); 32658f0484fSRodney W. Grimes bad2: 32758f0484fSRodney W. Grimes if (lport) 3289233c4d9SJason Evans (void)_close(*fd2p); 32958f0484fSRodney W. Grimes bad: 3309233c4d9SJason Evans (void)_close(s); 331d201fe46SDaniel Eischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 33242b4f28eSYoshinobu Inoue freeaddrinfo(res); 33358f0484fSRodney W. Grimes return (-1); 33458f0484fSRodney W. Grimes } 33558f0484fSRodney W. Grimes 33658f0484fSRodney W. Grimes int 33742b4f28eSYoshinobu Inoue rresvport(port) 33842b4f28eSYoshinobu Inoue int *port; 33958f0484fSRodney W. Grimes { 34042b4f28eSYoshinobu Inoue return rresvport_af(port, AF_INET); 34142b4f28eSYoshinobu Inoue } 34258f0484fSRodney W. Grimes 34342b4f28eSYoshinobu Inoue int 34442b4f28eSYoshinobu Inoue rresvport_af(alport, family) 34542b4f28eSYoshinobu Inoue int *alport, family; 34642b4f28eSYoshinobu Inoue { 3476d7bd75aSJacques Vidrine int s; 34842b4f28eSYoshinobu Inoue struct sockaddr_storage ss; 34942b4f28eSYoshinobu Inoue u_short *sport; 35042b4f28eSYoshinobu Inoue 35142b4f28eSYoshinobu Inoue memset(&ss, 0, sizeof(ss)); 35242b4f28eSYoshinobu Inoue ss.ss_family = family; 35342b4f28eSYoshinobu Inoue switch (family) { 35442b4f28eSYoshinobu Inoue case AF_INET: 35542b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 35642b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in *)&ss)->sin_port; 35742b4f28eSYoshinobu Inoue ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 35842b4f28eSYoshinobu Inoue break; 35942b4f28eSYoshinobu Inoue #ifdef INET6 36042b4f28eSYoshinobu Inoue case AF_INET6: 36142b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 36242b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 36342b4f28eSYoshinobu Inoue ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 36442b4f28eSYoshinobu Inoue break; 36542b4f28eSYoshinobu Inoue #endif 36642b4f28eSYoshinobu Inoue default: 36742b4f28eSYoshinobu Inoue errno = EAFNOSUPPORT; 36842b4f28eSYoshinobu Inoue return -1; 36942b4f28eSYoshinobu Inoue } 37042b4f28eSYoshinobu Inoue 371d201fe46SDaniel Eischen s = _socket(ss.ss_family, SOCK_STREAM, 0); 37258f0484fSRodney W. Grimes if (s < 0) 37358f0484fSRodney W. Grimes return (-1); 374ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */ 375ce70b6caSPeter Wemm sin.sin_port = htons((u_short)*alport); 376d201fe46SDaniel Eischen if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 37758f0484fSRodney W. Grimes return (s); 378ce70b6caSPeter Wemm if (errno != EADDRINUSE) { 3799233c4d9SJason Evans (void)_close(s); 38058f0484fSRodney W. Grimes return (-1); 38158f0484fSRodney W. Grimes } 382ce70b6caSPeter Wemm #endif 38342b4f28eSYoshinobu Inoue *sport = 0; 384ae42b666SYoshinobu Inoue if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { 3859233c4d9SJason Evans (void)_close(s); 386ce70b6caSPeter Wemm return (-1); 387ce70b6caSPeter Wemm } 38842b4f28eSYoshinobu Inoue *alport = (int)ntohs(*sport); 389ce70b6caSPeter Wemm return (s); 390ce70b6caSPeter Wemm } 39158f0484fSRodney W. Grimes 39258f0484fSRodney W. Grimes int __check_rhosts_file = 1; 39358f0484fSRodney W. Grimes char *__rcmd_errstr; 39458f0484fSRodney W. Grimes 39558f0484fSRodney W. Grimes int 39658f0484fSRodney W. Grimes ruserok(rhost, superuser, ruser, luser) 39758f0484fSRodney W. Grimes const char *rhost, *ruser, *luser; 39858f0484fSRodney W. Grimes int superuser; 39958f0484fSRodney W. Grimes { 400e3be4d7bSYoshinobu Inoue struct addrinfo hints, *res, *r; 401e3be4d7bSYoshinobu Inoue int error; 40242b4f28eSYoshinobu Inoue 403e3be4d7bSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 404e3be4d7bSYoshinobu Inoue hints.ai_family = PF_UNSPEC; 405e3be4d7bSYoshinobu Inoue hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 406e3be4d7bSYoshinobu Inoue error = getaddrinfo(rhost, "0", &hints, &res); 407e3be4d7bSYoshinobu Inoue if (error) 40858f0484fSRodney W. Grimes return (-1); 409e3be4d7bSYoshinobu Inoue 410e3be4d7bSYoshinobu Inoue for (r = res; r; r = r->ai_next) { 411e3be4d7bSYoshinobu Inoue if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 412e3be4d7bSYoshinobu Inoue luser) == 0) { 413e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 414e3be4d7bSYoshinobu Inoue return (0); 41542b4f28eSYoshinobu Inoue } 41642b4f28eSYoshinobu Inoue } 417e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 418e3be4d7bSYoshinobu Inoue return (-1); 41958f0484fSRodney W. Grimes } 42058f0484fSRodney W. Grimes 42158f0484fSRodney W. Grimes /* 42258f0484fSRodney W. Grimes * New .rhosts strategy: We are passed an ip address. We spin through 42358f0484fSRodney W. Grimes * hosts.equiv and .rhosts looking for a match. When the .rhosts only 42458f0484fSRodney W. Grimes * has ip addresses, we don't have to trust a nameserver. When it 42558f0484fSRodney W. Grimes * contains hostnames, we spin through the list of addresses the nameserver 42658f0484fSRodney W. Grimes * gives us and look for a match. 42758f0484fSRodney W. Grimes * 42858f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 42958f0484fSRodney W. Grimes */ 43058f0484fSRodney W. Grimes int 43158f0484fSRodney W. Grimes iruserok(raddr, superuser, ruser, luser) 432e0ce825eSDoug Rabson unsigned long raddr; 43358f0484fSRodney W. Grimes int superuser; 43458f0484fSRodney W. Grimes const char *ruser, *luser; 43558f0484fSRodney W. Grimes { 436eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 437eb74b996SHajimu UMEMOTO 438eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 439eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 440eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 441eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 442eb74b996SHajimu UMEMOTO return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser, 443eb74b996SHajimu UMEMOTO ruser, luser); 44442b4f28eSYoshinobu Inoue } 44542b4f28eSYoshinobu Inoue 446eb74b996SHajimu UMEMOTO /* 447eb74b996SHajimu UMEMOTO * AF independent extension of iruserok. 448eb74b996SHajimu UMEMOTO * 449eb74b996SHajimu UMEMOTO * Returns 0 if ok, -1 if not ok. 450eb74b996SHajimu UMEMOTO */ 451eb74b996SHajimu UMEMOTO int 452eb74b996SHajimu UMEMOTO iruserok_sa(ra, rlen, superuser, ruser, luser) 453eb74b996SHajimu UMEMOTO const void *ra; 454eb74b996SHajimu UMEMOTO int rlen; 45542b4f28eSYoshinobu Inoue int superuser; 45642b4f28eSYoshinobu Inoue const char *ruser, *luser; 45742b4f28eSYoshinobu Inoue { 4588fb3f3f6SDavid E. O'Brien char *cp; 45958f0484fSRodney W. Grimes struct stat sbuf; 46058f0484fSRodney W. Grimes struct passwd *pwd; 46158f0484fSRodney W. Grimes FILE *hostf; 46258f0484fSRodney W. Grimes uid_t uid; 46358f0484fSRodney W. Grimes int first; 46458f0484fSRodney W. Grimes char pbuf[MAXPATHLEN]; 465eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 466eb74b996SHajimu UMEMOTO struct sockaddr_storage ss; 46742b4f28eSYoshinobu Inoue 468eb74b996SHajimu UMEMOTO /* avoid alignment issue */ 469eb74b996SHajimu UMEMOTO if (rlen > sizeof(ss)) 470eb74b996SHajimu UMEMOTO return(-1); 471eb74b996SHajimu UMEMOTO memcpy(&ss, ra, rlen); 472eb74b996SHajimu UMEMOTO raddr = (struct sockaddr *)&ss; 47358f0484fSRodney W. Grimes 47458f0484fSRodney W. Grimes first = 1; 47558f0484fSRodney W. Grimes hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 47658f0484fSRodney W. Grimes again: 47758f0484fSRodney W. Grimes if (hostf) { 478eb74b996SHajimu UMEMOTO if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) { 47958f0484fSRodney W. Grimes (void)fclose(hostf); 48058f0484fSRodney W. Grimes return (0); 48158f0484fSRodney W. Grimes } 48258f0484fSRodney W. Grimes (void)fclose(hostf); 48358f0484fSRodney W. Grimes } 48458f0484fSRodney W. Grimes if (first == 1 && (__check_rhosts_file || superuser)) { 48558f0484fSRodney W. Grimes first = 0; 48658f0484fSRodney W. Grimes if ((pwd = getpwnam(luser)) == NULL) 48758f0484fSRodney W. Grimes return (-1); 48858f0484fSRodney W. Grimes (void)strcpy(pbuf, pwd->pw_dir); 48958f0484fSRodney W. Grimes (void)strcat(pbuf, "/.rhosts"); 49058f0484fSRodney W. Grimes 49158f0484fSRodney W. Grimes /* 49258f0484fSRodney W. Grimes * Change effective uid while opening .rhosts. If root and 49358f0484fSRodney W. Grimes * reading an NFS mounted file system, can't read files that 49458f0484fSRodney W. Grimes * are protected read/write owner only. 49558f0484fSRodney W. Grimes */ 49658f0484fSRodney W. Grimes uid = geteuid(); 49758f0484fSRodney W. Grimes (void)seteuid(pwd->pw_uid); 49858f0484fSRodney W. Grimes hostf = fopen(pbuf, "r"); 49958f0484fSRodney W. Grimes (void)seteuid(uid); 50058f0484fSRodney W. Grimes 50158f0484fSRodney W. Grimes if (hostf == NULL) 50258f0484fSRodney W. Grimes return (-1); 50358f0484fSRodney W. Grimes /* 50458f0484fSRodney W. Grimes * If not a regular file, or is owned by someone other than 50558f0484fSRodney W. Grimes * user or root or if writeable by anyone but the owner, quit. 50658f0484fSRodney W. Grimes */ 50758f0484fSRodney W. Grimes cp = NULL; 50858f0484fSRodney W. Grimes if (lstat(pbuf, &sbuf) < 0) 50958f0484fSRodney W. Grimes cp = ".rhosts lstat failed"; 51058f0484fSRodney W. Grimes else if (!S_ISREG(sbuf.st_mode)) 51158f0484fSRodney W. Grimes cp = ".rhosts not regular file"; 512d201fe46SDaniel Eischen else if (_fstat(fileno(hostf), &sbuf) < 0) 51358f0484fSRodney W. Grimes cp = ".rhosts fstat failed"; 51458f0484fSRodney W. Grimes else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 51558f0484fSRodney W. Grimes cp = "bad .rhosts owner"; 51658f0484fSRodney W. Grimes else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 51758f0484fSRodney W. Grimes cp = ".rhosts writeable by other than owner"; 51858f0484fSRodney W. Grimes /* If there were any problems, quit. */ 51958f0484fSRodney W. Grimes if (cp) { 52058f0484fSRodney W. Grimes __rcmd_errstr = cp; 52158f0484fSRodney W. Grimes (void)fclose(hostf); 52258f0484fSRodney W. Grimes return (-1); 52358f0484fSRodney W. Grimes } 52458f0484fSRodney W. Grimes goto again; 52558f0484fSRodney W. Grimes } 52658f0484fSRodney W. Grimes return (-1); 52758f0484fSRodney W. Grimes } 52858f0484fSRodney W. Grimes 52958f0484fSRodney W. Grimes /* 53058f0484fSRodney W. Grimes * XXX 53158f0484fSRodney W. Grimes * Don't make static, used by lpd(8). 53258f0484fSRodney W. Grimes * 53358f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 53458f0484fSRodney W. Grimes */ 53558f0484fSRodney W. Grimes int 53658f0484fSRodney W. Grimes __ivaliduser(hostf, raddr, luser, ruser) 53758f0484fSRodney W. Grimes FILE *hostf; 538e0ce825eSDoug Rabson u_int32_t raddr; 53958f0484fSRodney W. Grimes const char *luser, *ruser; 54058f0484fSRodney W. Grimes { 541eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 542eb74b996SHajimu UMEMOTO 543eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 544eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 545eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 546eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 547eb74b996SHajimu UMEMOTO return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, 548eb74b996SHajimu UMEMOTO luser, ruser); 54942b4f28eSYoshinobu Inoue } 55042b4f28eSYoshinobu Inoue 551eb74b996SHajimu UMEMOTO /* 552eb74b996SHajimu UMEMOTO * Returns 0 if ok, -1 if not ok. 553eb74b996SHajimu UMEMOTO * 554eb74b996SHajimu UMEMOTO * XXX obsolete API. 555eb74b996SHajimu UMEMOTO */ 55642b4f28eSYoshinobu Inoue int 55742b4f28eSYoshinobu Inoue __ivaliduser_af(hostf, raddr, luser, ruser, af, len) 55842b4f28eSYoshinobu Inoue FILE *hostf; 559eb74b996SHajimu UMEMOTO const void *raddr; 56042b4f28eSYoshinobu Inoue const char *luser, *ruser; 56142b4f28eSYoshinobu Inoue int af, len; 56242b4f28eSYoshinobu Inoue { 563eb74b996SHajimu UMEMOTO struct sockaddr *sa = NULL; 564eb74b996SHajimu UMEMOTO struct sockaddr_in *sin = NULL; 565eb74b996SHajimu UMEMOTO #ifdef INET6 566eb74b996SHajimu UMEMOTO struct sockaddr_in6 *sin6 = NULL; 567eb74b996SHajimu UMEMOTO #endif 568eb74b996SHajimu UMEMOTO struct sockaddr_storage ss; 569eb74b996SHajimu UMEMOTO 570eb74b996SHajimu UMEMOTO memset(&ss, 0, sizeof(ss)); 571eb74b996SHajimu UMEMOTO switch (af) { 572eb74b996SHajimu UMEMOTO case AF_INET: 573eb74b996SHajimu UMEMOTO if (len != sizeof(sin->sin_addr)) 574eb74b996SHajimu UMEMOTO return -1; 575eb74b996SHajimu UMEMOTO sin = (struct sockaddr_in *)&ss; 576eb74b996SHajimu UMEMOTO sin->sin_family = AF_INET; 577eb74b996SHajimu UMEMOTO sin->sin_len = sizeof(struct sockaddr_in); 578eb74b996SHajimu UMEMOTO memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr)); 579eb74b996SHajimu UMEMOTO break; 580eb74b996SHajimu UMEMOTO #ifdef INET6 581eb74b996SHajimu UMEMOTO case AF_INET6: 582eb74b996SHajimu UMEMOTO if (len != sizeof(sin6->sin6_addr)) 583eb74b996SHajimu UMEMOTO return -1; 584eb74b996SHajimu UMEMOTO /* you will lose scope info */ 585eb74b996SHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)&ss; 586eb74b996SHajimu UMEMOTO sin6->sin6_family = AF_INET6; 587eb74b996SHajimu UMEMOTO sin6->sin6_len = sizeof(struct sockaddr_in6); 588eb74b996SHajimu UMEMOTO memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr)); 589eb74b996SHajimu UMEMOTO break; 590eb74b996SHajimu UMEMOTO #endif 591eb74b996SHajimu UMEMOTO default: 592eb74b996SHajimu UMEMOTO return -1; 593eb74b996SHajimu UMEMOTO } 594eb74b996SHajimu UMEMOTO 595eb74b996SHajimu UMEMOTO sa = (struct sockaddr *)&ss; 596eb74b996SHajimu UMEMOTO return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser); 597eb74b996SHajimu UMEMOTO } 598eb74b996SHajimu UMEMOTO 599eb74b996SHajimu UMEMOTO int 600eb74b996SHajimu UMEMOTO __ivaliduser_sa(hostf, raddr, salen, luser, ruser) 601eb74b996SHajimu UMEMOTO FILE *hostf; 602eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 603eb74b996SHajimu UMEMOTO socklen_t salen; 604eb74b996SHajimu UMEMOTO const char *luser, *ruser; 605eb74b996SHajimu UMEMOTO { 6068fb3f3f6SDavid E. O'Brien char *user, *p; 60758f0484fSRodney W. Grimes int ch; 60858f0484fSRodney W. Grimes char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 60997cb5094SBill Paul char hname[MAXHOSTNAMELEN]; 6108538335fSBill Paul /* Presumed guilty until proven innocent. */ 6118538335fSBill Paul int userok = 0, hostok = 0; 6121e890b05SBill Paul #ifdef YP 6131e890b05SBill Paul char *ypdomain; 6148538335fSBill Paul 6151e890b05SBill Paul if (yp_get_default_domain(&ypdomain)) 6161e890b05SBill Paul ypdomain = NULL; 6171e890b05SBill Paul #else 6181e890b05SBill Paul #define ypdomain NULL 6191e890b05SBill Paul #endif 6208538335fSBill Paul /* We need to get the damn hostname back for netgroup matching. */ 621eb74b996SHajimu UMEMOTO if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0, 622eb74b996SHajimu UMEMOTO NI_NAMEREQD) != 0) 623c97c8f4aSJohn Polstra hname[0] = '\0'; 62458f0484fSRodney W. Grimes 62558f0484fSRodney W. Grimes while (fgets(buf, sizeof(buf), hostf)) { 62658f0484fSRodney W. Grimes p = buf; 62758f0484fSRodney W. Grimes /* Skip lines that are too long. */ 628a9f9141cSBrian S. Dean if (strchr(p, '\n') == NULL) { 62958f0484fSRodney W. Grimes while ((ch = getc(hostf)) != '\n' && ch != EOF); 63058f0484fSRodney W. Grimes continue; 63158f0484fSRodney W. Grimes } 632acc7e87cSPeter Wemm if (*p == '\n' || *p == '#') { 633acc7e87cSPeter Wemm /* comment... */ 634acc7e87cSPeter Wemm continue; 635acc7e87cSPeter Wemm } 63658f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 6373fb85bffSAndrey A. Chernov *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 63858f0484fSRodney W. Grimes p++; 63958f0484fSRodney W. Grimes } 64058f0484fSRodney W. Grimes if (*p == ' ' || *p == '\t') { 64158f0484fSRodney W. Grimes *p++ = '\0'; 64258f0484fSRodney W. Grimes while (*p == ' ' || *p == '\t') 64358f0484fSRodney W. Grimes p++; 64458f0484fSRodney W. Grimes user = p; 64558f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && 64658f0484fSRodney W. Grimes *p != '\t' && *p != '\0') 64758f0484fSRodney W. Grimes p++; 64858f0484fSRodney W. Grimes } else 64958f0484fSRodney W. Grimes user = p; 65058f0484fSRodney W. Grimes *p = '\0'; 6518538335fSBill Paul /* 6528538335fSBill Paul * Do +/- and +@/-@ checking. This looks really nasty, 6538538335fSBill Paul * but it matches SunOS's behavior so far as I can tell. 6548538335fSBill Paul */ 6558538335fSBill Paul switch(buf[0]) { 6568538335fSBill Paul case '+': 6578538335fSBill Paul if (!buf[1]) { /* '+' matches all hosts */ 6588538335fSBill Paul hostok = 1; 6598538335fSBill Paul break; 66058f0484fSRodney W. Grimes } 6618538335fSBill Paul if (buf[1] == '@') /* match a host by netgroup */ 662c97c8f4aSJohn Polstra hostok = hname[0] != '\0' && 663c97c8f4aSJohn Polstra innetgr(&buf[2], hname, NULL, ypdomain); 6648538335fSBill Paul else /* match a host by addr */ 665eb74b996SHajimu UMEMOTO hostok = __icheckhost(raddr, salen, 666eb74b996SHajimu UMEMOTO (char *)&buf[1]); 6678538335fSBill Paul break; 6688538335fSBill Paul case '-': /* reject '-' hosts and all their users */ 6698538335fSBill Paul if (buf[1] == '@') { 670c97c8f4aSJohn Polstra if (hname[0] == '\0' || 671c97c8f4aSJohn Polstra innetgr(&buf[2], hname, NULL, ypdomain)) 6728538335fSBill Paul return(-1); 6738538335fSBill Paul } else { 674eb74b996SHajimu UMEMOTO if (__icheckhost(raddr, salen, 675eb74b996SHajimu UMEMOTO (char *)&buf[1])) 6768538335fSBill Paul return(-1); 6778538335fSBill Paul } 6788538335fSBill Paul break; 6798538335fSBill Paul default: /* if no '+' or '-', do a simple match */ 680eb74b996SHajimu UMEMOTO hostok = __icheckhost(raddr, salen, buf); 6818538335fSBill Paul break; 6828538335fSBill Paul } 6838538335fSBill Paul switch(*user) { 6848538335fSBill Paul case '+': 6858538335fSBill Paul if (!*(user+1)) { /* '+' matches all users */ 6868538335fSBill Paul userok = 1; 6878538335fSBill Paul break; 6888538335fSBill Paul } 6898538335fSBill Paul if (*(user+1) == '@') /* match a user by netgroup */ 6901e890b05SBill Paul userok = innetgr(user+2, NULL, ruser, ypdomain); 6918538335fSBill Paul else /* match a user by direct specification */ 6928538335fSBill Paul userok = !(strcmp(ruser, user+1)); 6938538335fSBill Paul break; 6948538335fSBill Paul case '-': /* if we matched a hostname, */ 6958538335fSBill Paul if (hostok) { /* check for user field rejections */ 6968538335fSBill Paul if (!*(user+1)) 6978538335fSBill Paul return(-1); 6988538335fSBill Paul if (*(user+1) == '@') { 6998538335fSBill Paul if (innetgr(user+2, NULL, 7001e890b05SBill Paul ruser, ypdomain)) 7018538335fSBill Paul return(-1); 7028538335fSBill Paul } else { 7038538335fSBill Paul if (!strcmp(ruser, user+1)) 7048538335fSBill Paul return(-1); 7058538335fSBill Paul } 7068538335fSBill Paul } 7078538335fSBill Paul break; 7088538335fSBill Paul default: /* no rejections: try to match the user */ 7098538335fSBill Paul if (hostok) 7108538335fSBill Paul userok = !(strcmp(ruser,*user ? user : luser)); 7118538335fSBill Paul break; 7128538335fSBill Paul } 7138538335fSBill Paul if (hostok && userok) 7148538335fSBill Paul return(0); 71558f0484fSRodney W. Grimes } 71658f0484fSRodney W. Grimes return (-1); 71758f0484fSRodney W. Grimes } 71858f0484fSRodney W. Grimes 71958f0484fSRodney W. Grimes /* 72058f0484fSRodney W. Grimes * Returns "true" if match, 0 if no match. 721eb74b996SHajimu UMEMOTO * 722eb74b996SHajimu UMEMOTO * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion 723eb74b996SHajimu UMEMOTO * if af == AF_INET6. 72458f0484fSRodney W. Grimes */ 72558f0484fSRodney W. Grimes static int 726eb74b996SHajimu UMEMOTO __icheckhost(raddr, salen, lhost) 727eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 728eb74b996SHajimu UMEMOTO socklen_t salen; 729eb74b996SHajimu UMEMOTO const char *lhost; 73058f0484fSRodney W. Grimes { 731eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 732eb74b996SHajimu UMEMOTO struct sockaddr_in6 *sin6; 733eb74b996SHajimu UMEMOTO struct addrinfo hints, *res, *r; 734eb74b996SHajimu UMEMOTO int error; 735eb74b996SHajimu UMEMOTO char h1[NI_MAXHOST], h2[NI_MAXHOST]; 73658f0484fSRodney W. Grimes 737eb74b996SHajimu UMEMOTO if (raddr->sa_family == AF_INET6) { 738eb74b996SHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)raddr; 739eb74b996SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 740eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 741eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 742eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 743eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12], 744eb74b996SHajimu UMEMOTO sizeof(sin.sin_addr)); 745eb74b996SHajimu UMEMOTO raddr = (struct sockaddr *)&sin; 746eb74b996SHajimu UMEMOTO salen = sin.sin_len; 747eb74b996SHajimu UMEMOTO } 748eb74b996SHajimu UMEMOTO } 749eb74b996SHajimu UMEMOTO 750eb74b996SHajimu UMEMOTO h1[0] = '\0'; 751eb74b996SHajimu UMEMOTO if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 752eb74b996SHajimu UMEMOTO NI_NUMERICHOST | NI_WITHSCOPEID) != 0) 753eb74b996SHajimu UMEMOTO return (0); 754eb74b996SHajimu UMEMOTO 755eb74b996SHajimu UMEMOTO /* Resolve laddr into sockaddr */ 756eb74b996SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 757eb74b996SHajimu UMEMOTO hints.ai_family = raddr->sa_family; 758eb74b996SHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/ 759eb74b996SHajimu UMEMOTO res = NULL; 760eb74b996SHajimu UMEMOTO error = getaddrinfo(lhost, "0", &hints, &res); 761eb74b996SHajimu UMEMOTO if (error) 762eb74b996SHajimu UMEMOTO return (0); 763eb74b996SHajimu UMEMOTO 764eb74b996SHajimu UMEMOTO for (r = res; r ; r = r->ai_next) { 765eb74b996SHajimu UMEMOTO h2[0] = '\0'; 766eb74b996SHajimu UMEMOTO if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 767eb74b996SHajimu UMEMOTO NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0) 768eb74b996SHajimu UMEMOTO continue; 769eb74b996SHajimu UMEMOTO if (strcmp(h1, h2) == 0) { 770eb74b996SHajimu UMEMOTO freeaddrinfo(res); 77142b4f28eSYoshinobu Inoue return (1); 772eb74b996SHajimu UMEMOTO } 77342b4f28eSYoshinobu Inoue } 77458f0484fSRodney W. Grimes 775eb74b996SHajimu UMEMOTO /* No match. */ 776eb74b996SHajimu UMEMOTO freeaddrinfo(res); 77758f0484fSRodney W. Grimes return (0); 77858f0484fSRodney W. Grimes } 779