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 61c98e299eSHajimu UMEMOTO #include <arpa/nameser.h> 6258f0484fSRodney W. Grimes 6342b4f28eSYoshinobu Inoue /* wrapper for KAME-special getnameinfo() */ 6442b4f28eSYoshinobu Inoue #ifndef NI_WITHSCOPEID 6542b4f28eSYoshinobu Inoue #define NI_WITHSCOPEID 0 6642b4f28eSYoshinobu Inoue #endif 6742b4f28eSYoshinobu Inoue 6851295a4dSJordan K. Hubbard extern int innetgr __P(( const char *, const char *, const char *, const char * )); 6951295a4dSJordan K. Hubbard 70d1f32ba5SGeoff Rehmet #define max(a, b) ((a > b) ? a : b) 71d1f32ba5SGeoff Rehmet 72e0ce825eSDoug Rabson int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *)); 73eb74b996SHajimu UMEMOTO int __ivaliduser_af __P((FILE *,const void *, const char *, const char *, 74eb74b996SHajimu UMEMOTO int, int)); 75eb74b996SHajimu UMEMOTO int __ivaliduser_sa __P((FILE *, const struct sockaddr *, socklen_t, 76eb74b996SHajimu UMEMOTO const char *,const char *)); 77eb74b996SHajimu UMEMOTO static int __icheckhost __P((const struct sockaddr *, socklen_t, 78eb74b996SHajimu UMEMOTO const char *)); 7942b4f28eSYoshinobu Inoue 80eb74b996SHajimu UMEMOTO char paddr[NI_MAXHOST]; 8158f0484fSRodney W. Grimes 8258f0484fSRodney W. Grimes int 8358f0484fSRodney W. Grimes rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 8458f0484fSRodney W. Grimes char **ahost; 8558f0484fSRodney W. Grimes u_short rport; 8658f0484fSRodney W. Grimes const char *locuser, *remuser, *cmd; 8758f0484fSRodney W. Grimes int *fd2p; 8858f0484fSRodney W. Grimes { 890cac72f4SYoshinobu Inoue return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 900cac72f4SYoshinobu Inoue } 910cac72f4SYoshinobu Inoue 920cac72f4SYoshinobu Inoue int 930cac72f4SYoshinobu Inoue rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 940cac72f4SYoshinobu Inoue char **ahost; 950cac72f4SYoshinobu Inoue u_short rport; 960cac72f4SYoshinobu Inoue const char *locuser, *remuser, *cmd; 970cac72f4SYoshinobu Inoue int *fd2p; 980cac72f4SYoshinobu Inoue int af; 990cac72f4SYoshinobu Inoue { 10042b4f28eSYoshinobu Inoue struct addrinfo hints, *res, *ai; 10142b4f28eSYoshinobu Inoue struct sockaddr_storage from; 10258f0484fSRodney W. Grimes fd_set reads; 10358f0484fSRodney W. Grimes long oldmask; 10458f0484fSRodney W. Grimes pid_t pid; 10542b4f28eSYoshinobu Inoue int s, aport, lport, timo, error; 10658f0484fSRodney W. Grimes char c; 107eb74b996SHajimu UMEMOTO int refused, nres; 108c98e299eSHajimu UMEMOTO char num[8]; 109c98e299eSHajimu UMEMOTO static char canonnamebuf[MAXDNAME]; /* is it proper here? */ 11058f0484fSRodney W. Grimes 11158f0484fSRodney W. Grimes pid = getpid(); 11242b4f28eSYoshinobu Inoue 11342b4f28eSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 11442b4f28eSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 1150cac72f4SYoshinobu Inoue hints.ai_family = af; 11642b4f28eSYoshinobu Inoue hints.ai_socktype = SOCK_STREAM; 11742b4f28eSYoshinobu Inoue hints.ai_protocol = 0; 11842b4f28eSYoshinobu Inoue (void)snprintf(num, sizeof(num), "%d", ntohs(rport)); 11942b4f28eSYoshinobu Inoue error = getaddrinfo(*ahost, num, &hints, &res); 12042b4f28eSYoshinobu Inoue if (error) { 12142b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 12242b4f28eSYoshinobu Inoue gai_strerror(error)); 12342b4f28eSYoshinobu Inoue if (error == EAI_SYSTEM) 12442b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 12542b4f28eSYoshinobu Inoue strerror(errno)); 12658f0484fSRodney W. Grimes return (-1); 12758f0484fSRodney W. Grimes } 128c98e299eSHajimu UMEMOTO 129c98e299eSHajimu UMEMOTO if (res->ai_canonname 130c98e299eSHajimu UMEMOTO && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) { 131c98e299eSHajimu UMEMOTO strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf)); 132c98e299eSHajimu UMEMOTO *ahost = canonnamebuf; 133c98e299eSHajimu UMEMOTO } 134eb74b996SHajimu UMEMOTO nres = 0; 135eb74b996SHajimu UMEMOTO for (ai = res; ai; ai = ai->ai_next) 136eb74b996SHajimu UMEMOTO nres++; 13742b4f28eSYoshinobu Inoue ai = res; 138eb74b996SHajimu UMEMOTO refused = 0; 13958f0484fSRodney W. Grimes oldmask = sigblock(sigmask(SIGURG)); 14058f0484fSRodney W. Grimes for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 14142b4f28eSYoshinobu Inoue s = rresvport_af(&lport, ai->ai_family); 14258f0484fSRodney W. Grimes if (s < 0) { 143474ce1d1SYoshinobu Inoue if (errno != EAGAIN && ai->ai_next) { 144474ce1d1SYoshinobu Inoue ai = ai->ai_next; 145474ce1d1SYoshinobu Inoue continue; 146474ce1d1SYoshinobu Inoue } 14758f0484fSRodney W. Grimes if (errno == EAGAIN) 14858f0484fSRodney W. Grimes (void)fprintf(stderr, 14958f0484fSRodney W. Grimes "rcmd: socket: All ports in use\n"); 15058f0484fSRodney W. Grimes else 15158f0484fSRodney W. Grimes (void)fprintf(stderr, "rcmd: socket: %s\n", 15258f0484fSRodney W. Grimes strerror(errno)); 15342b4f28eSYoshinobu Inoue freeaddrinfo(res); 154474ce1d1SYoshinobu Inoue sigsetmask(oldmask); 15558f0484fSRodney W. Grimes return (-1); 15658f0484fSRodney W. Grimes } 1579233c4d9SJason Evans _fcntl(s, F_SETOWN, pid); 15842b4f28eSYoshinobu Inoue if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 15958f0484fSRodney W. Grimes break; 1609233c4d9SJason Evans (void)_close(s); 16158f0484fSRodney W. Grimes if (errno == EADDRINUSE) { 16258f0484fSRodney W. Grimes lport--; 16358f0484fSRodney W. Grimes continue; 16458f0484fSRodney W. Grimes } 165eb74b996SHajimu UMEMOTO if (errno == ECONNREFUSED) 166eb74b996SHajimu UMEMOTO refused = 1; 167eb74b996SHajimu UMEMOTO if (ai->ai_next == NULL && (!refused || timo > 16)) { 168eb74b996SHajimu UMEMOTO (void)fprintf(stderr, "%s: %s\n", 169eb74b996SHajimu UMEMOTO *ahost, strerror(errno)); 170eb74b996SHajimu UMEMOTO freeaddrinfo(res); 171eb74b996SHajimu UMEMOTO sigsetmask(oldmask); 172eb74b996SHajimu UMEMOTO return (-1); 1732368b03bSHajimu UMEMOTO } 174eb74b996SHajimu UMEMOTO if (nres > 1) { 17558f0484fSRodney W. Grimes int oerrno = errno; 17658f0484fSRodney W. Grimes 17742b4f28eSYoshinobu Inoue getnameinfo(ai->ai_addr, ai->ai_addrlen, 17842b4f28eSYoshinobu Inoue paddr, sizeof(paddr), 17942b4f28eSYoshinobu Inoue NULL, 0, 18042b4f28eSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 18158f0484fSRodney W. Grimes (void)fprintf(stderr, "connect to address %s: ", 18242b4f28eSYoshinobu Inoue paddr); 18358f0484fSRodney W. Grimes errno = oerrno; 18458f0484fSRodney W. Grimes perror(0); 185eb74b996SHajimu UMEMOTO } 186eb74b996SHajimu UMEMOTO if ((ai = ai->ai_next) == NULL) { 187eb74b996SHajimu UMEMOTO /* refused && timo <= 16 */ 188eb74b996SHajimu UMEMOTO struct timespec time_to_sleep, time_remaining; 189eb74b996SHajimu UMEMOTO 190eb74b996SHajimu UMEMOTO time_to_sleep.tv_sec = timo; 191eb74b996SHajimu UMEMOTO time_to_sleep.tv_nsec = 0; 192eb74b996SHajimu UMEMOTO (void)_nanosleep(&time_to_sleep, &time_remaining); 193eb74b996SHajimu UMEMOTO timo *= 2; 194eb74b996SHajimu UMEMOTO ai = res; 195eb74b996SHajimu UMEMOTO refused = 0; 196eb74b996SHajimu UMEMOTO } 197eb74b996SHajimu UMEMOTO if (nres > 1) { 19842b4f28eSYoshinobu Inoue getnameinfo(ai->ai_addr, ai->ai_addrlen, 19942b4f28eSYoshinobu Inoue paddr, sizeof(paddr), 20042b4f28eSYoshinobu Inoue NULL, 0, 20142b4f28eSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 20242b4f28eSYoshinobu Inoue fprintf(stderr, "Trying %s...\n", paddr); 20358f0484fSRodney W. Grimes } 20458f0484fSRodney W. Grimes } 20558f0484fSRodney W. Grimes lport--; 20658f0484fSRodney W. Grimes if (fd2p == 0) { 2079233c4d9SJason Evans _write(s, "", 1); 20858f0484fSRodney W. Grimes lport = 0; 20958f0484fSRodney W. Grimes } else { 21058f0484fSRodney W. Grimes char num[8]; 21142b4f28eSYoshinobu Inoue int s2 = rresvport_af(&lport, ai->ai_family), s3; 21242b4f28eSYoshinobu Inoue int len = ai->ai_addrlen; 213d1f32ba5SGeoff Rehmet int nfds; 21458f0484fSRodney W. Grimes 21558f0484fSRodney W. Grimes if (s2 < 0) 21658f0484fSRodney W. Grimes goto bad; 21758f0484fSRodney W. Grimes listen(s2, 1); 21858f0484fSRodney W. Grimes (void)snprintf(num, sizeof(num), "%d", lport); 2199233c4d9SJason Evans if (_write(s, num, strlen(num)+1) != strlen(num)+1) { 22058f0484fSRodney W. Grimes (void)fprintf(stderr, 22158f0484fSRodney W. Grimes "rcmd: write (setting up stderr): %s\n", 22258f0484fSRodney W. Grimes strerror(errno)); 2239233c4d9SJason Evans (void)_close(s2); 22458f0484fSRodney W. Grimes goto bad; 22558f0484fSRodney W. Grimes } 226d1f32ba5SGeoff Rehmet nfds = max(s, s2)+1; 227d1f32ba5SGeoff Rehmet if(nfds > FD_SETSIZE) { 228d1f32ba5SGeoff Rehmet fprintf(stderr, "rcmd: too many files\n"); 2299233c4d9SJason Evans (void)_close(s2); 230d1f32ba5SGeoff Rehmet goto bad; 231d1f32ba5SGeoff Rehmet } 232ce70b6caSPeter Wemm again: 23358f0484fSRodney W. Grimes FD_ZERO(&reads); 23458f0484fSRodney W. Grimes FD_SET(s, &reads); 23558f0484fSRodney W. Grimes FD_SET(s2, &reads); 23658f0484fSRodney W. Grimes errno = 0; 237d1f32ba5SGeoff Rehmet if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 23858f0484fSRodney W. Grimes if (errno != 0) 23958f0484fSRodney W. Grimes (void)fprintf(stderr, 24058f0484fSRodney W. Grimes "rcmd: select (setting up stderr): %s\n", 24158f0484fSRodney W. Grimes strerror(errno)); 24258f0484fSRodney W. Grimes else 24358f0484fSRodney W. Grimes (void)fprintf(stderr, 24458f0484fSRodney W. Grimes "select: protocol failure in circuit setup\n"); 2459233c4d9SJason Evans (void)_close(s2); 24658f0484fSRodney W. Grimes goto bad; 24758f0484fSRodney W. Grimes } 24858f0484fSRodney W. Grimes s3 = accept(s2, (struct sockaddr *)&from, &len); 24942b4f28eSYoshinobu Inoue switch (from.ss_family) { 25042b4f28eSYoshinobu Inoue case AF_INET: 25142b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 25242b4f28eSYoshinobu Inoue break; 25342b4f28eSYoshinobu Inoue #ifdef INET6 25442b4f28eSYoshinobu Inoue case AF_INET6: 25542b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 25642b4f28eSYoshinobu Inoue break; 25742b4f28eSYoshinobu Inoue #endif 25842b4f28eSYoshinobu Inoue default: 25942b4f28eSYoshinobu Inoue aport = 0; /* error */ 26042b4f28eSYoshinobu Inoue break; 26142b4f28eSYoshinobu Inoue } 262ce70b6caSPeter Wemm /* 263ce70b6caSPeter Wemm * XXX careful for ftp bounce attacks. If discovered, shut them 264ce70b6caSPeter Wemm * down and check for the real auxiliary channel to connect. 265ce70b6caSPeter Wemm */ 26642b4f28eSYoshinobu Inoue if (aport == 20) { 2679233c4d9SJason Evans _close(s3); 268ce70b6caSPeter Wemm goto again; 269ce70b6caSPeter Wemm } 2709233c4d9SJason Evans (void)_close(s2); 27158f0484fSRodney W. Grimes if (s3 < 0) { 27258f0484fSRodney W. Grimes (void)fprintf(stderr, 27358f0484fSRodney W. Grimes "rcmd: accept: %s\n", strerror(errno)); 27458f0484fSRodney W. Grimes lport = 0; 27558f0484fSRodney W. Grimes goto bad; 27658f0484fSRodney W. Grimes } 27758f0484fSRodney W. Grimes *fd2p = s3; 27842b4f28eSYoshinobu Inoue if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 27958f0484fSRodney W. Grimes (void)fprintf(stderr, 28058f0484fSRodney W. Grimes "socket: protocol failure in circuit setup.\n"); 28158f0484fSRodney W. Grimes goto bad2; 28258f0484fSRodney W. Grimes } 28358f0484fSRodney W. Grimes } 2849233c4d9SJason Evans (void)_write(s, locuser, strlen(locuser)+1); 2859233c4d9SJason Evans (void)_write(s, remuser, strlen(remuser)+1); 2869233c4d9SJason Evans (void)_write(s, cmd, strlen(cmd)+1); 2879233c4d9SJason Evans if (_read(s, &c, 1) != 1) { 28858f0484fSRodney W. Grimes (void)fprintf(stderr, 28958f0484fSRodney W. Grimes "rcmd: %s: %s\n", *ahost, strerror(errno)); 29058f0484fSRodney W. Grimes goto bad2; 29158f0484fSRodney W. Grimes } 29258f0484fSRodney W. Grimes if (c != 0) { 2939233c4d9SJason Evans while (_read(s, &c, 1) == 1) { 2949233c4d9SJason Evans (void)_write(STDERR_FILENO, &c, 1); 29558f0484fSRodney W. Grimes if (c == '\n') 29658f0484fSRodney W. Grimes break; 29758f0484fSRodney W. Grimes } 29858f0484fSRodney W. Grimes goto bad2; 29958f0484fSRodney W. Grimes } 30058f0484fSRodney W. Grimes sigsetmask(oldmask); 30142b4f28eSYoshinobu Inoue freeaddrinfo(res); 30258f0484fSRodney W. Grimes return (s); 30358f0484fSRodney W. Grimes bad2: 30458f0484fSRodney W. Grimes if (lport) 3059233c4d9SJason Evans (void)_close(*fd2p); 30658f0484fSRodney W. Grimes bad: 3079233c4d9SJason Evans (void)_close(s); 30858f0484fSRodney W. Grimes sigsetmask(oldmask); 30942b4f28eSYoshinobu Inoue freeaddrinfo(res); 31058f0484fSRodney W. Grimes return (-1); 31158f0484fSRodney W. Grimes } 31258f0484fSRodney W. Grimes 31358f0484fSRodney W. Grimes int 31442b4f28eSYoshinobu Inoue rresvport(port) 31542b4f28eSYoshinobu Inoue int *port; 31658f0484fSRodney W. Grimes { 31742b4f28eSYoshinobu Inoue return rresvport_af(port, AF_INET); 31842b4f28eSYoshinobu Inoue } 31958f0484fSRodney W. Grimes 32042b4f28eSYoshinobu Inoue int 32142b4f28eSYoshinobu Inoue rresvport_af(alport, family) 32242b4f28eSYoshinobu Inoue int *alport, family; 32342b4f28eSYoshinobu Inoue { 32442b4f28eSYoshinobu Inoue int i, s, len, err; 32542b4f28eSYoshinobu Inoue struct sockaddr_storage ss; 32642b4f28eSYoshinobu Inoue u_short *sport; 32742b4f28eSYoshinobu Inoue 32842b4f28eSYoshinobu Inoue memset(&ss, 0, sizeof(ss)); 32942b4f28eSYoshinobu Inoue ss.ss_family = family; 33042b4f28eSYoshinobu Inoue switch (family) { 33142b4f28eSYoshinobu Inoue case AF_INET: 33242b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 33342b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in *)&ss)->sin_port; 33442b4f28eSYoshinobu Inoue ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 33542b4f28eSYoshinobu Inoue break; 33642b4f28eSYoshinobu Inoue #ifdef INET6 33742b4f28eSYoshinobu Inoue case AF_INET6: 33842b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 33942b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 34042b4f28eSYoshinobu Inoue ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 34142b4f28eSYoshinobu Inoue break; 34242b4f28eSYoshinobu Inoue #endif 34342b4f28eSYoshinobu Inoue default: 34442b4f28eSYoshinobu Inoue errno = EAFNOSUPPORT; 34542b4f28eSYoshinobu Inoue return -1; 34642b4f28eSYoshinobu Inoue } 34742b4f28eSYoshinobu Inoue 34842b4f28eSYoshinobu Inoue s = socket(ss.ss_family, SOCK_STREAM, 0); 34958f0484fSRodney W. Grimes if (s < 0) 35058f0484fSRodney W. Grimes return (-1); 351ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */ 352ce70b6caSPeter Wemm sin.sin_port = htons((u_short)*alport); 353ce70b6caSPeter Wemm if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 35458f0484fSRodney W. Grimes return (s); 355ce70b6caSPeter Wemm if (errno != EADDRINUSE) { 3569233c4d9SJason Evans (void)_close(s); 35758f0484fSRodney W. Grimes return (-1); 35858f0484fSRodney W. Grimes } 359ce70b6caSPeter Wemm #endif 36042b4f28eSYoshinobu Inoue *sport = 0; 361ae42b666SYoshinobu Inoue if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { 3629233c4d9SJason Evans (void)_close(s); 363ce70b6caSPeter Wemm return (-1); 364ce70b6caSPeter Wemm } 36542b4f28eSYoshinobu Inoue *alport = (int)ntohs(*sport); 366ce70b6caSPeter Wemm return (s); 367ce70b6caSPeter Wemm } 36858f0484fSRodney W. Grimes 36958f0484fSRodney W. Grimes int __check_rhosts_file = 1; 37058f0484fSRodney W. Grimes char *__rcmd_errstr; 37158f0484fSRodney W. Grimes 37258f0484fSRodney W. Grimes int 37358f0484fSRodney W. Grimes ruserok(rhost, superuser, ruser, luser) 37458f0484fSRodney W. Grimes const char *rhost, *ruser, *luser; 37558f0484fSRodney W. Grimes int superuser; 37658f0484fSRodney W. Grimes { 377e3be4d7bSYoshinobu Inoue struct addrinfo hints, *res, *r; 378e3be4d7bSYoshinobu Inoue int error; 37942b4f28eSYoshinobu Inoue 380e3be4d7bSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 381e3be4d7bSYoshinobu Inoue hints.ai_family = PF_UNSPEC; 382e3be4d7bSYoshinobu Inoue hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 383e3be4d7bSYoshinobu Inoue error = getaddrinfo(rhost, "0", &hints, &res); 384e3be4d7bSYoshinobu Inoue if (error) 38558f0484fSRodney W. Grimes return (-1); 386e3be4d7bSYoshinobu Inoue 387e3be4d7bSYoshinobu Inoue for (r = res; r; r = r->ai_next) { 388e3be4d7bSYoshinobu Inoue if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 389e3be4d7bSYoshinobu Inoue luser) == 0) { 390e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 391e3be4d7bSYoshinobu Inoue return (0); 39242b4f28eSYoshinobu Inoue } 39342b4f28eSYoshinobu Inoue } 394e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 395e3be4d7bSYoshinobu Inoue return (-1); 39658f0484fSRodney W. Grimes } 39758f0484fSRodney W. Grimes 39858f0484fSRodney W. Grimes /* 39958f0484fSRodney W. Grimes * New .rhosts strategy: We are passed an ip address. We spin through 40058f0484fSRodney W. Grimes * hosts.equiv and .rhosts looking for a match. When the .rhosts only 40158f0484fSRodney W. Grimes * has ip addresses, we don't have to trust a nameserver. When it 40258f0484fSRodney W. Grimes * contains hostnames, we spin through the list of addresses the nameserver 40358f0484fSRodney W. Grimes * gives us and look for a match. 40458f0484fSRodney W. Grimes * 40558f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 40658f0484fSRodney W. Grimes */ 40758f0484fSRodney W. Grimes int 40858f0484fSRodney W. Grimes iruserok(raddr, superuser, ruser, luser) 409e0ce825eSDoug Rabson unsigned long raddr; 41058f0484fSRodney W. Grimes int superuser; 41158f0484fSRodney W. Grimes const char *ruser, *luser; 41258f0484fSRodney W. Grimes { 413eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 414eb74b996SHajimu UMEMOTO 415eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 416eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 417eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 418eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 419eb74b996SHajimu UMEMOTO return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser, 420eb74b996SHajimu UMEMOTO ruser, luser); 42142b4f28eSYoshinobu Inoue } 42242b4f28eSYoshinobu Inoue 423eb74b996SHajimu UMEMOTO /* 424eb74b996SHajimu UMEMOTO * AF independent extension of iruserok. 425eb74b996SHajimu UMEMOTO * 426eb74b996SHajimu UMEMOTO * Returns 0 if ok, -1 if not ok. 427eb74b996SHajimu UMEMOTO */ 428eb74b996SHajimu UMEMOTO int 429eb74b996SHajimu UMEMOTO iruserok_sa(ra, rlen, superuser, ruser, luser) 430eb74b996SHajimu UMEMOTO const void *ra; 431eb74b996SHajimu UMEMOTO int rlen; 43242b4f28eSYoshinobu Inoue int superuser; 43342b4f28eSYoshinobu Inoue const char *ruser, *luser; 43442b4f28eSYoshinobu Inoue { 43558f0484fSRodney W. Grimes register char *cp; 43658f0484fSRodney W. Grimes struct stat sbuf; 43758f0484fSRodney W. Grimes struct passwd *pwd; 43858f0484fSRodney W. Grimes FILE *hostf; 43958f0484fSRodney W. Grimes uid_t uid; 44058f0484fSRodney W. Grimes int first; 44158f0484fSRodney W. Grimes char pbuf[MAXPATHLEN]; 442eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 443eb74b996SHajimu UMEMOTO struct sockaddr_storage ss; 44442b4f28eSYoshinobu Inoue 445eb74b996SHajimu UMEMOTO /* avoid alignment issue */ 446eb74b996SHajimu UMEMOTO if (rlen > sizeof(ss)) 447eb74b996SHajimu UMEMOTO return(-1); 448eb74b996SHajimu UMEMOTO memcpy(&ss, ra, rlen); 449eb74b996SHajimu UMEMOTO raddr = (struct sockaddr *)&ss; 45058f0484fSRodney W. Grimes 45158f0484fSRodney W. Grimes first = 1; 45258f0484fSRodney W. Grimes hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 45358f0484fSRodney W. Grimes again: 45458f0484fSRodney W. Grimes if (hostf) { 455eb74b996SHajimu UMEMOTO if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) { 45658f0484fSRodney W. Grimes (void)fclose(hostf); 45758f0484fSRodney W. Grimes return (0); 45858f0484fSRodney W. Grimes } 45958f0484fSRodney W. Grimes (void)fclose(hostf); 46058f0484fSRodney W. Grimes } 46158f0484fSRodney W. Grimes if (first == 1 && (__check_rhosts_file || superuser)) { 46258f0484fSRodney W. Grimes first = 0; 46358f0484fSRodney W. Grimes if ((pwd = getpwnam(luser)) == NULL) 46458f0484fSRodney W. Grimes return (-1); 46558f0484fSRodney W. Grimes (void)strcpy(pbuf, pwd->pw_dir); 46658f0484fSRodney W. Grimes (void)strcat(pbuf, "/.rhosts"); 46758f0484fSRodney W. Grimes 46858f0484fSRodney W. Grimes /* 46958f0484fSRodney W. Grimes * Change effective uid while opening .rhosts. If root and 47058f0484fSRodney W. Grimes * reading an NFS mounted file system, can't read files that 47158f0484fSRodney W. Grimes * are protected read/write owner only. 47258f0484fSRodney W. Grimes */ 47358f0484fSRodney W. Grimes uid = geteuid(); 47458f0484fSRodney W. Grimes (void)seteuid(pwd->pw_uid); 47558f0484fSRodney W. Grimes hostf = fopen(pbuf, "r"); 47658f0484fSRodney W. Grimes (void)seteuid(uid); 47758f0484fSRodney W. Grimes 47858f0484fSRodney W. Grimes if (hostf == NULL) 47958f0484fSRodney W. Grimes return (-1); 48058f0484fSRodney W. Grimes /* 48158f0484fSRodney W. Grimes * If not a regular file, or is owned by someone other than 48258f0484fSRodney W. Grimes * user or root or if writeable by anyone but the owner, quit. 48358f0484fSRodney W. Grimes */ 48458f0484fSRodney W. Grimes cp = NULL; 48558f0484fSRodney W. Grimes if (lstat(pbuf, &sbuf) < 0) 48658f0484fSRodney W. Grimes cp = ".rhosts lstat failed"; 48758f0484fSRodney W. Grimes else if (!S_ISREG(sbuf.st_mode)) 48858f0484fSRodney W. Grimes cp = ".rhosts not regular file"; 48958f0484fSRodney W. Grimes else if (fstat(fileno(hostf), &sbuf) < 0) 49058f0484fSRodney W. Grimes cp = ".rhosts fstat failed"; 49158f0484fSRodney W. Grimes else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 49258f0484fSRodney W. Grimes cp = "bad .rhosts owner"; 49358f0484fSRodney W. Grimes else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 49458f0484fSRodney W. Grimes cp = ".rhosts writeable by other than owner"; 49558f0484fSRodney W. Grimes /* If there were any problems, quit. */ 49658f0484fSRodney W. Grimes if (cp) { 49758f0484fSRodney W. Grimes __rcmd_errstr = cp; 49858f0484fSRodney W. Grimes (void)fclose(hostf); 49958f0484fSRodney W. Grimes return (-1); 50058f0484fSRodney W. Grimes } 50158f0484fSRodney W. Grimes goto again; 50258f0484fSRodney W. Grimes } 50358f0484fSRodney W. Grimes return (-1); 50458f0484fSRodney W. Grimes } 50558f0484fSRodney W. Grimes 50658f0484fSRodney W. Grimes /* 50758f0484fSRodney W. Grimes * XXX 50858f0484fSRodney W. Grimes * Don't make static, used by lpd(8). 50958f0484fSRodney W. Grimes * 51058f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 51158f0484fSRodney W. Grimes */ 51258f0484fSRodney W. Grimes int 51358f0484fSRodney W. Grimes __ivaliduser(hostf, raddr, luser, ruser) 51458f0484fSRodney W. Grimes FILE *hostf; 515e0ce825eSDoug Rabson u_int32_t raddr; 51658f0484fSRodney W. Grimes const char *luser, *ruser; 51758f0484fSRodney W. Grimes { 518eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 519eb74b996SHajimu UMEMOTO 520eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 521eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 522eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 523eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 524eb74b996SHajimu UMEMOTO return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, 525eb74b996SHajimu UMEMOTO luser, ruser); 52642b4f28eSYoshinobu Inoue } 52742b4f28eSYoshinobu Inoue 528eb74b996SHajimu UMEMOTO /* 529eb74b996SHajimu UMEMOTO * Returns 0 if ok, -1 if not ok. 530eb74b996SHajimu UMEMOTO * 531eb74b996SHajimu UMEMOTO * XXX obsolete API. 532eb74b996SHajimu UMEMOTO */ 53342b4f28eSYoshinobu Inoue int 53442b4f28eSYoshinobu Inoue __ivaliduser_af(hostf, raddr, luser, ruser, af, len) 53542b4f28eSYoshinobu Inoue FILE *hostf; 536eb74b996SHajimu UMEMOTO const void *raddr; 53742b4f28eSYoshinobu Inoue const char *luser, *ruser; 53842b4f28eSYoshinobu Inoue int af, len; 53942b4f28eSYoshinobu Inoue { 540eb74b996SHajimu UMEMOTO struct sockaddr *sa = NULL; 541eb74b996SHajimu UMEMOTO struct sockaddr_in *sin = NULL; 542eb74b996SHajimu UMEMOTO #ifdef INET6 543eb74b996SHajimu UMEMOTO struct sockaddr_in6 *sin6 = NULL; 544eb74b996SHajimu UMEMOTO #endif 545eb74b996SHajimu UMEMOTO struct sockaddr_storage ss; 546eb74b996SHajimu UMEMOTO 547eb74b996SHajimu UMEMOTO memset(&ss, 0, sizeof(ss)); 548eb74b996SHajimu UMEMOTO switch (af) { 549eb74b996SHajimu UMEMOTO case AF_INET: 550eb74b996SHajimu UMEMOTO if (len != sizeof(sin->sin_addr)) 551eb74b996SHajimu UMEMOTO return -1; 552eb74b996SHajimu UMEMOTO sin = (struct sockaddr_in *)&ss; 553eb74b996SHajimu UMEMOTO sin->sin_family = AF_INET; 554eb74b996SHajimu UMEMOTO sin->sin_len = sizeof(struct sockaddr_in); 555eb74b996SHajimu UMEMOTO memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr)); 556eb74b996SHajimu UMEMOTO break; 557eb74b996SHajimu UMEMOTO #ifdef INET6 558eb74b996SHajimu UMEMOTO case AF_INET6: 559eb74b996SHajimu UMEMOTO if (len != sizeof(sin6->sin6_addr)) 560eb74b996SHajimu UMEMOTO return -1; 561eb74b996SHajimu UMEMOTO /* you will lose scope info */ 562eb74b996SHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)&ss; 563eb74b996SHajimu UMEMOTO sin6->sin6_family = AF_INET6; 564eb74b996SHajimu UMEMOTO sin6->sin6_len = sizeof(struct sockaddr_in6); 565eb74b996SHajimu UMEMOTO memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr)); 566eb74b996SHajimu UMEMOTO break; 567eb74b996SHajimu UMEMOTO #endif 568eb74b996SHajimu UMEMOTO default: 569eb74b996SHajimu UMEMOTO return -1; 570eb74b996SHajimu UMEMOTO } 571eb74b996SHajimu UMEMOTO 572eb74b996SHajimu UMEMOTO sa = (struct sockaddr *)&ss; 573eb74b996SHajimu UMEMOTO return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser); 574eb74b996SHajimu UMEMOTO } 575eb74b996SHajimu UMEMOTO 576eb74b996SHajimu UMEMOTO /* 577eb74b996SHajimu UMEMOTO * Returns 0 if ok, -1 if not ok. 578eb74b996SHajimu UMEMOTO */ 579eb74b996SHajimu UMEMOTO int 580eb74b996SHajimu UMEMOTO __ivaliduser_sa(hostf, raddr, salen, luser, ruser) 581eb74b996SHajimu UMEMOTO FILE *hostf; 582eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 583eb74b996SHajimu UMEMOTO socklen_t salen; 584eb74b996SHajimu UMEMOTO const char *luser, *ruser; 585eb74b996SHajimu UMEMOTO { 58658f0484fSRodney W. Grimes register char *user, *p; 58758f0484fSRodney W. Grimes int ch; 58858f0484fSRodney W. Grimes char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 58997cb5094SBill Paul char hname[MAXHOSTNAMELEN]; 5908538335fSBill Paul /* Presumed guilty until proven innocent. */ 5918538335fSBill Paul int userok = 0, hostok = 0; 59242b4f28eSYoshinobu Inoue int h_error; 5931e890b05SBill Paul #ifdef YP 5941e890b05SBill Paul char *ypdomain; 5958538335fSBill Paul 5961e890b05SBill Paul if (yp_get_default_domain(&ypdomain)) 5971e890b05SBill Paul ypdomain = NULL; 5981e890b05SBill Paul #else 5991e890b05SBill Paul #define ypdomain NULL 6001e890b05SBill Paul #endif 6018538335fSBill Paul /* We need to get the damn hostname back for netgroup matching. */ 602eb74b996SHajimu UMEMOTO if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0, 603eb74b996SHajimu UMEMOTO NI_NAMEREQD) != 0) 6048538335fSBill Paul return (-1); 60558f0484fSRodney W. Grimes 60658f0484fSRodney W. Grimes while (fgets(buf, sizeof(buf), hostf)) { 60758f0484fSRodney W. Grimes p = buf; 60858f0484fSRodney W. Grimes /* Skip lines that are too long. */ 609a9f9141cSBrian S. Dean if (strchr(p, '\n') == NULL) { 61058f0484fSRodney W. Grimes while ((ch = getc(hostf)) != '\n' && ch != EOF); 61158f0484fSRodney W. Grimes continue; 61258f0484fSRodney W. Grimes } 613acc7e87cSPeter Wemm if (*p == '\n' || *p == '#') { 614acc7e87cSPeter Wemm /* comment... */ 615acc7e87cSPeter Wemm continue; 616acc7e87cSPeter Wemm } 61758f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 6183fb85bffSAndrey A. Chernov *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 61958f0484fSRodney W. Grimes p++; 62058f0484fSRodney W. Grimes } 62158f0484fSRodney W. Grimes if (*p == ' ' || *p == '\t') { 62258f0484fSRodney W. Grimes *p++ = '\0'; 62358f0484fSRodney W. Grimes while (*p == ' ' || *p == '\t') 62458f0484fSRodney W. Grimes p++; 62558f0484fSRodney W. Grimes user = p; 62658f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && 62758f0484fSRodney W. Grimes *p != '\t' && *p != '\0') 62858f0484fSRodney W. Grimes p++; 62958f0484fSRodney W. Grimes } else 63058f0484fSRodney W. Grimes user = p; 63158f0484fSRodney W. Grimes *p = '\0'; 6328538335fSBill Paul /* 6338538335fSBill Paul * Do +/- and +@/-@ checking. This looks really nasty, 6348538335fSBill Paul * but it matches SunOS's behavior so far as I can tell. 6358538335fSBill Paul */ 6368538335fSBill Paul switch(buf[0]) { 6378538335fSBill Paul case '+': 6388538335fSBill Paul if (!buf[1]) { /* '+' matches all hosts */ 6398538335fSBill Paul hostok = 1; 6408538335fSBill Paul break; 64158f0484fSRodney W. Grimes } 6428538335fSBill Paul if (buf[1] == '@') /* match a host by netgroup */ 64397cb5094SBill Paul hostok = innetgr((char *)&buf[2], 64497cb5094SBill Paul (char *)&hname, NULL, ypdomain); 6458538335fSBill Paul else /* match a host by addr */ 646eb74b996SHajimu UMEMOTO hostok = __icheckhost(raddr, salen, 647eb74b996SHajimu UMEMOTO (char *)&buf[1]); 6488538335fSBill Paul break; 6498538335fSBill Paul case '-': /* reject '-' hosts and all their users */ 6508538335fSBill Paul if (buf[1] == '@') { 6518538335fSBill Paul if (innetgr((char *)&buf[2], 65297cb5094SBill Paul (char *)&hname, NULL, ypdomain)) 6538538335fSBill Paul return(-1); 6548538335fSBill Paul } else { 655eb74b996SHajimu UMEMOTO if (__icheckhost(raddr, salen, 656eb74b996SHajimu UMEMOTO (char *)&buf[1])) 6578538335fSBill Paul return(-1); 6588538335fSBill Paul } 6598538335fSBill Paul break; 6608538335fSBill Paul default: /* if no '+' or '-', do a simple match */ 661eb74b996SHajimu UMEMOTO hostok = __icheckhost(raddr, salen, buf); 6628538335fSBill Paul break; 6638538335fSBill Paul } 6648538335fSBill Paul switch(*user) { 6658538335fSBill Paul case '+': 6668538335fSBill Paul if (!*(user+1)) { /* '+' matches all users */ 6678538335fSBill Paul userok = 1; 6688538335fSBill Paul break; 6698538335fSBill Paul } 6708538335fSBill Paul if (*(user+1) == '@') /* match a user by netgroup */ 6711e890b05SBill Paul userok = innetgr(user+2, NULL, ruser, ypdomain); 6728538335fSBill Paul else /* match a user by direct specification */ 6738538335fSBill Paul userok = !(strcmp(ruser, user+1)); 6748538335fSBill Paul break; 6758538335fSBill Paul case '-': /* if we matched a hostname, */ 6768538335fSBill Paul if (hostok) { /* check for user field rejections */ 6778538335fSBill Paul if (!*(user+1)) 6788538335fSBill Paul return(-1); 6798538335fSBill Paul if (*(user+1) == '@') { 6808538335fSBill Paul if (innetgr(user+2, NULL, 6811e890b05SBill Paul ruser, ypdomain)) 6828538335fSBill Paul return(-1); 6838538335fSBill Paul } else { 6848538335fSBill Paul if (!strcmp(ruser, user+1)) 6858538335fSBill Paul return(-1); 6868538335fSBill Paul } 6878538335fSBill Paul } 6888538335fSBill Paul break; 6898538335fSBill Paul default: /* no rejections: try to match the user */ 6908538335fSBill Paul if (hostok) 6918538335fSBill Paul userok = !(strcmp(ruser,*user ? user : luser)); 6928538335fSBill Paul break; 6938538335fSBill Paul } 6948538335fSBill Paul if (hostok && userok) 6958538335fSBill Paul return(0); 69658f0484fSRodney W. Grimes } 69758f0484fSRodney W. Grimes return (-1); 69858f0484fSRodney W. Grimes } 69958f0484fSRodney W. Grimes 70058f0484fSRodney W. Grimes /* 70158f0484fSRodney W. Grimes * Returns "true" if match, 0 if no match. 702eb74b996SHajimu UMEMOTO * 703eb74b996SHajimu UMEMOTO * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion 704eb74b996SHajimu UMEMOTO * if af == AF_INET6. 70558f0484fSRodney W. Grimes */ 70658f0484fSRodney W. Grimes static int 707eb74b996SHajimu UMEMOTO __icheckhost(raddr, salen, lhost) 708eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 709eb74b996SHajimu UMEMOTO socklen_t salen; 710eb74b996SHajimu UMEMOTO const char *lhost; 71158f0484fSRodney W. Grimes { 712eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 713eb74b996SHajimu UMEMOTO struct sockaddr_in6 *sin6; 714eb74b996SHajimu UMEMOTO struct addrinfo hints, *res, *r; 715eb74b996SHajimu UMEMOTO int error; 716eb74b996SHajimu UMEMOTO char h1[NI_MAXHOST], h2[NI_MAXHOST]; 71758f0484fSRodney W. Grimes 718eb74b996SHajimu UMEMOTO if (raddr->sa_family == AF_INET6) { 719eb74b996SHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)raddr; 720eb74b996SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 721eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 722eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 723eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 724eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12], 725eb74b996SHajimu UMEMOTO sizeof(sin.sin_addr)); 726eb74b996SHajimu UMEMOTO raddr = (struct sockaddr *)&sin; 727eb74b996SHajimu UMEMOTO salen = sin.sin_len; 728eb74b996SHajimu UMEMOTO } 729eb74b996SHajimu UMEMOTO } 730eb74b996SHajimu UMEMOTO 731eb74b996SHajimu UMEMOTO h1[0] = '\0'; 732eb74b996SHajimu UMEMOTO if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 733eb74b996SHajimu UMEMOTO NI_NUMERICHOST | NI_WITHSCOPEID) != 0) 734eb74b996SHajimu UMEMOTO return (0); 735eb74b996SHajimu UMEMOTO 736eb74b996SHajimu UMEMOTO /* Resolve laddr into sockaddr */ 737eb74b996SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 738eb74b996SHajimu UMEMOTO hints.ai_family = raddr->sa_family; 739eb74b996SHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/ 740eb74b996SHajimu UMEMOTO res = NULL; 741eb74b996SHajimu UMEMOTO error = getaddrinfo(lhost, "0", &hints, &res); 742eb74b996SHajimu UMEMOTO if (error) 743eb74b996SHajimu UMEMOTO return (0); 744eb74b996SHajimu UMEMOTO 745eb74b996SHajimu UMEMOTO for (r = res; r ; r = r->ai_next) { 746eb74b996SHajimu UMEMOTO h2[0] = '\0'; 747eb74b996SHajimu UMEMOTO if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 748eb74b996SHajimu UMEMOTO NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0) 749eb74b996SHajimu UMEMOTO continue; 750eb74b996SHajimu UMEMOTO if (strcmp(h1, h2) == 0) { 751eb74b996SHajimu UMEMOTO freeaddrinfo(res); 75242b4f28eSYoshinobu Inoue return (1); 753eb74b996SHajimu UMEMOTO } 75442b4f28eSYoshinobu Inoue } 75558f0484fSRodney W. Grimes 756eb74b996SHajimu UMEMOTO /* No match. */ 757eb74b996SHajimu UMEMOTO freeaddrinfo(res); 75858f0484fSRodney W. Grimes return (0); 75958f0484fSRodney W. Grimes } 760