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 * 4. Neither the name of the University nor the names of its contributors 1458f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1558f0484fSRodney W. Grimes * without specific prior written permission. 1658f0484fSRodney W. Grimes * 1758f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1858f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1958f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2058f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2158f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2258f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2358f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2458f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2558f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2658f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2758f0484fSRodney W. Grimes * SUCH DAMAGE. 2858f0484fSRodney W. Grimes */ 2958f0484fSRodney W. Grimes 3058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3158f0484fSRodney W. Grimes static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 3258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 33333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 34333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 3558f0484fSRodney W. Grimes 36d201fe46SDaniel Eischen #include "namespace.h" 3758f0484fSRodney W. Grimes #include <sys/param.h> 3858f0484fSRodney W. Grimes #include <sys/socket.h> 3958f0484fSRodney W. Grimes #include <sys/stat.h> 4058f0484fSRodney W. Grimes 4158f0484fSRodney W. Grimes #include <netinet/in.h> 4258f0484fSRodney W. Grimes #include <arpa/inet.h> 4358f0484fSRodney W. Grimes 4458f0484fSRodney W. Grimes #include <signal.h> 4558f0484fSRodney W. Grimes #include <fcntl.h> 4658f0484fSRodney W. Grimes #include <netdb.h> 47d4567212SWarner Losh #include <stdlib.h> 4858f0484fSRodney W. Grimes #include <unistd.h> 4958f0484fSRodney W. Grimes #include <pwd.h> 5058f0484fSRodney W. Grimes #include <errno.h> 5158f0484fSRodney W. Grimes #include <stdio.h> 5258f0484fSRodney W. Grimes #include <ctype.h> 5358f0484fSRodney W. Grimes #include <string.h> 541e890b05SBill Paul #include <rpc/rpc.h> 556c58990dSBjoern A. Zeeb #ifdef YP 561e890b05SBill Paul #include <rpcsvc/yp_prot.h> 571e890b05SBill Paul #include <rpcsvc/ypclnt.h> 581e890b05SBill Paul #endif 59c98e299eSHajimu UMEMOTO #include <arpa/nameser.h> 60d201fe46SDaniel Eischen #include "un-namespace.h" 6158f0484fSRodney W. Grimes 62c05ac53bSDavid E. O'Brien extern int innetgr( const char *, const char *, const char *, const char * ); 6351295a4dSJordan K. Hubbard 64d1f32ba5SGeoff Rehmet #define max(a, b) ((a > b) ? a : b) 65d1f32ba5SGeoff Rehmet 66c05ac53bSDavid E. O'Brien int __ivaliduser(FILE *, u_int32_t, const char *, const char *); 671372519bSDavid E. O'Brien int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int); 681372519bSDavid E. O'Brien int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *, 691372519bSDavid E. O'Brien const char *); 701372519bSDavid E. O'Brien static int __icheckhost(const struct sockaddr *, socklen_t, const char *); 7142b4f28eSYoshinobu Inoue 72eb74b996SHajimu UMEMOTO char paddr[NI_MAXHOST]; 7358f0484fSRodney W. Grimes 7458f0484fSRodney W. Grimes int 7558f0484fSRodney W. Grimes rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 7658f0484fSRodney W. Grimes char **ahost; 7758f0484fSRodney W. Grimes u_short rport; 7858f0484fSRodney W. Grimes const char *locuser, *remuser, *cmd; 7958f0484fSRodney W. Grimes int *fd2p; 8058f0484fSRodney W. Grimes { 810cac72f4SYoshinobu Inoue return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 820cac72f4SYoshinobu Inoue } 830cac72f4SYoshinobu Inoue 840cac72f4SYoshinobu Inoue int 850cac72f4SYoshinobu Inoue rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 860cac72f4SYoshinobu Inoue char **ahost; 870cac72f4SYoshinobu Inoue u_short rport; 880cac72f4SYoshinobu Inoue const char *locuser, *remuser, *cmd; 890cac72f4SYoshinobu Inoue int *fd2p; 900cac72f4SYoshinobu Inoue int af; 910cac72f4SYoshinobu Inoue { 9242b4f28eSYoshinobu Inoue struct addrinfo hints, *res, *ai; 9342b4f28eSYoshinobu Inoue struct sockaddr_storage from; 9458f0484fSRodney W. Grimes fd_set reads; 95d201fe46SDaniel Eischen sigset_t oldmask, newmask; 9658f0484fSRodney W. Grimes pid_t pid; 9742b4f28eSYoshinobu Inoue int s, aport, lport, timo, error; 98d4567212SWarner Losh char c, *p; 99eb74b996SHajimu UMEMOTO int refused, nres; 100c98e299eSHajimu UMEMOTO char num[8]; 101c98e299eSHajimu UMEMOTO static char canonnamebuf[MAXDNAME]; /* is it proper here? */ 10258f0484fSRodney W. Grimes 103d4567212SWarner Losh /* call rcmdsh() with specified remote shell if appropriate. */ 104d4567212SWarner Losh if (!issetugid() && (p = getenv("RSH"))) { 105d4567212SWarner Losh struct servent *sp = getservbyname("shell", "tcp"); 106d4567212SWarner Losh 107d4567212SWarner Losh if (sp && sp->s_port == rport) 108d4567212SWarner Losh return (rcmdsh(ahost, rport, locuser, remuser, 109d4567212SWarner Losh cmd, p)); 110d4567212SWarner Losh } 111d4567212SWarner Losh 112d4567212SWarner Losh /* use rsh(1) if non-root and remote port is shell. */ 113d4567212SWarner Losh if (geteuid()) { 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, NULL)); 119d4567212SWarner Losh } 120d4567212SWarner Losh 12158f0484fSRodney W. Grimes pid = getpid(); 12242b4f28eSYoshinobu Inoue 12342b4f28eSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 12442b4f28eSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 1250cac72f4SYoshinobu Inoue hints.ai_family = af; 12642b4f28eSYoshinobu Inoue hints.ai_socktype = SOCK_STREAM; 12742b4f28eSYoshinobu Inoue hints.ai_protocol = 0; 12842b4f28eSYoshinobu Inoue (void)snprintf(num, sizeof(num), "%d", ntohs(rport)); 12942b4f28eSYoshinobu Inoue error = getaddrinfo(*ahost, num, &hints, &res); 13042b4f28eSYoshinobu Inoue if (error) { 13142b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 13242b4f28eSYoshinobu Inoue gai_strerror(error)); 13342b4f28eSYoshinobu Inoue if (error == EAI_SYSTEM) 13442b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 13542b4f28eSYoshinobu Inoue strerror(errno)); 13658f0484fSRodney W. Grimes return (-1); 13758f0484fSRodney W. Grimes } 138c98e299eSHajimu UMEMOTO 139c98e299eSHajimu UMEMOTO if (res->ai_canonname 140c98e299eSHajimu UMEMOTO && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) { 141c98e299eSHajimu UMEMOTO strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf)); 142c98e299eSHajimu UMEMOTO *ahost = canonnamebuf; 143c98e299eSHajimu UMEMOTO } 144eb74b996SHajimu UMEMOTO nres = 0; 145eb74b996SHajimu UMEMOTO for (ai = res; ai; ai = ai->ai_next) 146eb74b996SHajimu UMEMOTO nres++; 14742b4f28eSYoshinobu Inoue ai = res; 148eb74b996SHajimu UMEMOTO refused = 0; 149d201fe46SDaniel Eischen sigemptyset(&newmask); 150d201fe46SDaniel Eischen sigaddset(&newmask, SIGURG); 151d201fe46SDaniel Eischen _sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask); 15258f0484fSRodney W. Grimes for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 15342b4f28eSYoshinobu Inoue s = rresvport_af(&lport, ai->ai_family); 15458f0484fSRodney W. Grimes if (s < 0) { 155474ce1d1SYoshinobu Inoue if (errno != EAGAIN && ai->ai_next) { 156474ce1d1SYoshinobu Inoue ai = ai->ai_next; 157474ce1d1SYoshinobu Inoue continue; 158474ce1d1SYoshinobu Inoue } 15958f0484fSRodney W. Grimes if (errno == EAGAIN) 16058f0484fSRodney W. Grimes (void)fprintf(stderr, 16158f0484fSRodney W. Grimes "rcmd: socket: All ports in use\n"); 16258f0484fSRodney W. Grimes else 16358f0484fSRodney W. Grimes (void)fprintf(stderr, "rcmd: socket: %s\n", 16458f0484fSRodney W. Grimes strerror(errno)); 16542b4f28eSYoshinobu Inoue freeaddrinfo(res); 166d201fe46SDaniel Eischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 167d201fe46SDaniel Eischen NULL); 16858f0484fSRodney W. Grimes return (-1); 16958f0484fSRodney W. Grimes } 1709233c4d9SJason Evans _fcntl(s, F_SETOWN, pid); 171d201fe46SDaniel Eischen if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 17258f0484fSRodney W. Grimes break; 1739233c4d9SJason Evans (void)_close(s); 17458f0484fSRodney W. Grimes if (errno == EADDRINUSE) { 17558f0484fSRodney W. Grimes lport--; 17658f0484fSRodney W. Grimes continue; 17758f0484fSRodney W. Grimes } 178eb74b996SHajimu UMEMOTO if (errno == ECONNREFUSED) 179eb74b996SHajimu UMEMOTO refused = 1; 180eb74b996SHajimu UMEMOTO if (ai->ai_next == NULL && (!refused || timo > 16)) { 181eb74b996SHajimu UMEMOTO (void)fprintf(stderr, "%s: %s\n", 182eb74b996SHajimu UMEMOTO *ahost, strerror(errno)); 183eb74b996SHajimu UMEMOTO freeaddrinfo(res); 184d201fe46SDaniel Eischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 185d201fe46SDaniel Eischen NULL); 186eb74b996SHajimu UMEMOTO return (-1); 1872368b03bSHajimu UMEMOTO } 188eb74b996SHajimu UMEMOTO if (nres > 1) { 18958f0484fSRodney W. Grimes int oerrno = errno; 19058f0484fSRodney W. Grimes 1914f101318SHajimu UMEMOTO getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr, 1924f101318SHajimu UMEMOTO sizeof(paddr), NULL, 0, NI_NUMERICHOST); 19358f0484fSRodney W. Grimes (void)fprintf(stderr, "connect to address %s: ", 19442b4f28eSYoshinobu Inoue paddr); 19558f0484fSRodney W. Grimes errno = oerrno; 19658f0484fSRodney W. Grimes perror(0); 197eb74b996SHajimu UMEMOTO } 198eb74b996SHajimu UMEMOTO if ((ai = ai->ai_next) == NULL) { 199eb74b996SHajimu UMEMOTO /* refused && timo <= 16 */ 200eb74b996SHajimu UMEMOTO struct timespec time_to_sleep, time_remaining; 201eb74b996SHajimu UMEMOTO 202eb74b996SHajimu UMEMOTO time_to_sleep.tv_sec = timo; 203eb74b996SHajimu UMEMOTO time_to_sleep.tv_nsec = 0; 204eb74b996SHajimu UMEMOTO (void)_nanosleep(&time_to_sleep, &time_remaining); 205eb74b996SHajimu UMEMOTO timo *= 2; 206eb74b996SHajimu UMEMOTO ai = res; 207eb74b996SHajimu UMEMOTO refused = 0; 208eb74b996SHajimu UMEMOTO } 209eb74b996SHajimu UMEMOTO if (nres > 1) { 2104f101318SHajimu UMEMOTO getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr, 2114f101318SHajimu UMEMOTO sizeof(paddr), NULL, 0, NI_NUMERICHOST); 21242b4f28eSYoshinobu Inoue fprintf(stderr, "Trying %s...\n", paddr); 21358f0484fSRodney W. Grimes } 21458f0484fSRodney W. Grimes } 21558f0484fSRodney W. Grimes lport--; 21658f0484fSRodney W. Grimes if (fd2p == 0) { 2179233c4d9SJason Evans _write(s, "", 1); 21858f0484fSRodney W. Grimes lport = 0; 21958f0484fSRodney W. Grimes } else { 22042b4f28eSYoshinobu Inoue int s2 = rresvport_af(&lport, ai->ai_family), s3; 22110248e3aSStefan Farfeleder socklen_t len = ai->ai_addrlen; 222d1f32ba5SGeoff Rehmet int nfds; 22358f0484fSRodney W. Grimes 22458f0484fSRodney W. Grimes if (s2 < 0) 22558f0484fSRodney W. Grimes goto bad; 226d201fe46SDaniel Eischen _listen(s2, 1); 22758f0484fSRodney W. Grimes (void)snprintf(num, sizeof(num), "%d", lport); 2289233c4d9SJason Evans if (_write(s, num, strlen(num)+1) != strlen(num)+1) { 22958f0484fSRodney W. Grimes (void)fprintf(stderr, 23058f0484fSRodney W. Grimes "rcmd: write (setting up stderr): %s\n", 23158f0484fSRodney W. Grimes strerror(errno)); 2329233c4d9SJason Evans (void)_close(s2); 23358f0484fSRodney W. Grimes goto bad; 23458f0484fSRodney W. Grimes } 235d1f32ba5SGeoff Rehmet nfds = max(s, s2)+1; 236d1f32ba5SGeoff Rehmet if(nfds > FD_SETSIZE) { 237d1f32ba5SGeoff Rehmet fprintf(stderr, "rcmd: too many files\n"); 2389233c4d9SJason Evans (void)_close(s2); 239d1f32ba5SGeoff Rehmet goto bad; 240d1f32ba5SGeoff Rehmet } 241ce70b6caSPeter Wemm again: 24258f0484fSRodney W. Grimes FD_ZERO(&reads); 24358f0484fSRodney W. Grimes FD_SET(s, &reads); 24458f0484fSRodney W. Grimes FD_SET(s2, &reads); 24558f0484fSRodney W. Grimes errno = 0; 246d201fe46SDaniel Eischen if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 24758f0484fSRodney W. Grimes if (errno != 0) 24858f0484fSRodney W. Grimes (void)fprintf(stderr, 24958f0484fSRodney W. Grimes "rcmd: select (setting up stderr): %s\n", 25058f0484fSRodney W. Grimes strerror(errno)); 25158f0484fSRodney W. Grimes else 25258f0484fSRodney W. Grimes (void)fprintf(stderr, 25358f0484fSRodney W. Grimes "select: protocol failure in circuit setup\n"); 2549233c4d9SJason Evans (void)_close(s2); 25558f0484fSRodney W. Grimes goto bad; 25658f0484fSRodney W. Grimes } 257d201fe46SDaniel Eischen s3 = _accept(s2, (struct sockaddr *)&from, &len); 25842b4f28eSYoshinobu Inoue switch (from.ss_family) { 25942b4f28eSYoshinobu Inoue case AF_INET: 26042b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 26142b4f28eSYoshinobu Inoue break; 26242b4f28eSYoshinobu Inoue #ifdef INET6 26342b4f28eSYoshinobu Inoue case AF_INET6: 26442b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 26542b4f28eSYoshinobu Inoue break; 26642b4f28eSYoshinobu Inoue #endif 26742b4f28eSYoshinobu Inoue default: 26842b4f28eSYoshinobu Inoue aport = 0; /* error */ 26942b4f28eSYoshinobu Inoue break; 27042b4f28eSYoshinobu Inoue } 271ce70b6caSPeter Wemm /* 272ce70b6caSPeter Wemm * XXX careful for ftp bounce attacks. If discovered, shut them 273ce70b6caSPeter Wemm * down and check for the real auxiliary channel to connect. 274ce70b6caSPeter Wemm */ 27542b4f28eSYoshinobu Inoue if (aport == 20) { 2769233c4d9SJason Evans _close(s3); 277ce70b6caSPeter Wemm goto again; 278ce70b6caSPeter Wemm } 2799233c4d9SJason Evans (void)_close(s2); 28058f0484fSRodney W. Grimes if (s3 < 0) { 28158f0484fSRodney W. Grimes (void)fprintf(stderr, 28258f0484fSRodney W. Grimes "rcmd: accept: %s\n", strerror(errno)); 28358f0484fSRodney W. Grimes lport = 0; 28458f0484fSRodney W. Grimes goto bad; 28558f0484fSRodney W. Grimes } 28658f0484fSRodney W. Grimes *fd2p = s3; 28742b4f28eSYoshinobu Inoue if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 28858f0484fSRodney W. Grimes (void)fprintf(stderr, 28958f0484fSRodney W. Grimes "socket: protocol failure in circuit setup.\n"); 29058f0484fSRodney W. Grimes goto bad2; 29158f0484fSRodney W. Grimes } 29258f0484fSRodney W. Grimes } 2939233c4d9SJason Evans (void)_write(s, locuser, strlen(locuser)+1); 2949233c4d9SJason Evans (void)_write(s, remuser, strlen(remuser)+1); 2959233c4d9SJason Evans (void)_write(s, cmd, strlen(cmd)+1); 2969233c4d9SJason Evans if (_read(s, &c, 1) != 1) { 29758f0484fSRodney W. Grimes (void)fprintf(stderr, 29858f0484fSRodney W. Grimes "rcmd: %s: %s\n", *ahost, strerror(errno)); 29958f0484fSRodney W. Grimes goto bad2; 30058f0484fSRodney W. Grimes } 30158f0484fSRodney W. Grimes if (c != 0) { 3029233c4d9SJason Evans while (_read(s, &c, 1) == 1) { 3039233c4d9SJason Evans (void)_write(STDERR_FILENO, &c, 1); 30458f0484fSRodney W. Grimes if (c == '\n') 30558f0484fSRodney W. Grimes break; 30658f0484fSRodney W. Grimes } 30758f0484fSRodney W. Grimes goto bad2; 30858f0484fSRodney W. Grimes } 309d201fe46SDaniel Eischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 31042b4f28eSYoshinobu Inoue freeaddrinfo(res); 31158f0484fSRodney W. Grimes return (s); 31258f0484fSRodney W. Grimes bad2: 31358f0484fSRodney W. Grimes if (lport) 3149233c4d9SJason Evans (void)_close(*fd2p); 31558f0484fSRodney W. Grimes bad: 3169233c4d9SJason Evans (void)_close(s); 317d201fe46SDaniel Eischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 31842b4f28eSYoshinobu Inoue freeaddrinfo(res); 31958f0484fSRodney W. Grimes return (-1); 32058f0484fSRodney W. Grimes } 32158f0484fSRodney W. Grimes 32258f0484fSRodney W. Grimes int 32342b4f28eSYoshinobu Inoue rresvport(port) 32442b4f28eSYoshinobu Inoue int *port; 32558f0484fSRodney W. Grimes { 32642b4f28eSYoshinobu Inoue return rresvport_af(port, AF_INET); 32742b4f28eSYoshinobu Inoue } 32858f0484fSRodney W. Grimes 32942b4f28eSYoshinobu Inoue int 33042b4f28eSYoshinobu Inoue rresvport_af(alport, family) 33142b4f28eSYoshinobu Inoue int *alport, family; 33242b4f28eSYoshinobu Inoue { 3336d7bd75aSJacques Vidrine int s; 33442b4f28eSYoshinobu Inoue struct sockaddr_storage ss; 33542b4f28eSYoshinobu Inoue u_short *sport; 33642b4f28eSYoshinobu Inoue 33742b4f28eSYoshinobu Inoue memset(&ss, 0, sizeof(ss)); 33842b4f28eSYoshinobu Inoue ss.ss_family = family; 33942b4f28eSYoshinobu Inoue switch (family) { 34042b4f28eSYoshinobu Inoue case AF_INET: 34142b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 34242b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in *)&ss)->sin_port; 34342b4f28eSYoshinobu Inoue ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 34442b4f28eSYoshinobu Inoue break; 34542b4f28eSYoshinobu Inoue #ifdef INET6 34642b4f28eSYoshinobu Inoue case AF_INET6: 34742b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 34842b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 34942b4f28eSYoshinobu Inoue ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 35042b4f28eSYoshinobu Inoue break; 35142b4f28eSYoshinobu Inoue #endif 35242b4f28eSYoshinobu Inoue default: 35342b4f28eSYoshinobu Inoue errno = EAFNOSUPPORT; 35442b4f28eSYoshinobu Inoue return -1; 35542b4f28eSYoshinobu Inoue } 35642b4f28eSYoshinobu Inoue 357d201fe46SDaniel Eischen s = _socket(ss.ss_family, SOCK_STREAM, 0); 35858f0484fSRodney W. Grimes if (s < 0) 35958f0484fSRodney W. Grimes return (-1); 360ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */ 361ce70b6caSPeter Wemm sin.sin_port = htons((u_short)*alport); 362d201fe46SDaniel Eischen if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 36358f0484fSRodney W. Grimes return (s); 364ce70b6caSPeter Wemm if (errno != EADDRINUSE) { 3659233c4d9SJason Evans (void)_close(s); 36658f0484fSRodney W. Grimes return (-1); 36758f0484fSRodney W. Grimes } 368ce70b6caSPeter Wemm #endif 36942b4f28eSYoshinobu Inoue *sport = 0; 370ae42b666SYoshinobu Inoue if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { 3719233c4d9SJason Evans (void)_close(s); 372ce70b6caSPeter Wemm return (-1); 373ce70b6caSPeter Wemm } 37442b4f28eSYoshinobu Inoue *alport = (int)ntohs(*sport); 375ce70b6caSPeter Wemm return (s); 376ce70b6caSPeter Wemm } 37758f0484fSRodney W. Grimes 37858f0484fSRodney W. Grimes int __check_rhosts_file = 1; 37958f0484fSRodney W. Grimes char *__rcmd_errstr; 38058f0484fSRodney W. Grimes 38158f0484fSRodney W. Grimes int 38258f0484fSRodney W. Grimes ruserok(rhost, superuser, ruser, luser) 38358f0484fSRodney W. Grimes const char *rhost, *ruser, *luser; 38458f0484fSRodney W. Grimes int superuser; 38558f0484fSRodney W. Grimes { 386e3be4d7bSYoshinobu Inoue struct addrinfo hints, *res, *r; 387e3be4d7bSYoshinobu Inoue int error; 38842b4f28eSYoshinobu Inoue 389e3be4d7bSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 390e3be4d7bSYoshinobu Inoue hints.ai_family = PF_UNSPEC; 391e3be4d7bSYoshinobu Inoue hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 392e3be4d7bSYoshinobu Inoue error = getaddrinfo(rhost, "0", &hints, &res); 393e3be4d7bSYoshinobu Inoue if (error) 39458f0484fSRodney W. Grimes return (-1); 395e3be4d7bSYoshinobu Inoue 396e3be4d7bSYoshinobu Inoue for (r = res; r; r = r->ai_next) { 397e3be4d7bSYoshinobu Inoue if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 398e3be4d7bSYoshinobu Inoue luser) == 0) { 399e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 400e3be4d7bSYoshinobu Inoue return (0); 40142b4f28eSYoshinobu Inoue } 40242b4f28eSYoshinobu Inoue } 403e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 404e3be4d7bSYoshinobu Inoue return (-1); 40558f0484fSRodney W. Grimes } 40658f0484fSRodney W. Grimes 40758f0484fSRodney W. Grimes /* 40858f0484fSRodney W. Grimes * New .rhosts strategy: We are passed an ip address. We spin through 40958f0484fSRodney W. Grimes * hosts.equiv and .rhosts looking for a match. When the .rhosts only 41058f0484fSRodney W. Grimes * has ip addresses, we don't have to trust a nameserver. When it 41158f0484fSRodney W. Grimes * contains hostnames, we spin through the list of addresses the nameserver 41258f0484fSRodney W. Grimes * gives us and look for a match. 41358f0484fSRodney W. Grimes * 41458f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 41558f0484fSRodney W. Grimes */ 41658f0484fSRodney W. Grimes int 41758f0484fSRodney W. Grimes iruserok(raddr, superuser, ruser, luser) 418e0ce825eSDoug Rabson unsigned long raddr; 41958f0484fSRodney W. Grimes int superuser; 42058f0484fSRodney W. Grimes const char *ruser, *luser; 42158f0484fSRodney W. Grimes { 422eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 423eb74b996SHajimu UMEMOTO 424eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 425eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 426eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 427eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 428eb74b996SHajimu UMEMOTO return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser, 429eb74b996SHajimu UMEMOTO ruser, luser); 43042b4f28eSYoshinobu Inoue } 43142b4f28eSYoshinobu Inoue 432eb74b996SHajimu UMEMOTO /* 433eb74b996SHajimu UMEMOTO * AF independent extension of iruserok. 434eb74b996SHajimu UMEMOTO * 435eb74b996SHajimu UMEMOTO * Returns 0 if ok, -1 if not ok. 436eb74b996SHajimu UMEMOTO */ 437eb74b996SHajimu UMEMOTO int 438eb74b996SHajimu UMEMOTO iruserok_sa(ra, rlen, superuser, ruser, luser) 439eb74b996SHajimu UMEMOTO const void *ra; 440eb74b996SHajimu UMEMOTO int rlen; 44142b4f28eSYoshinobu Inoue int superuser; 44242b4f28eSYoshinobu Inoue const char *ruser, *luser; 44342b4f28eSYoshinobu Inoue { 4448fb3f3f6SDavid E. O'Brien char *cp; 44558f0484fSRodney W. Grimes struct stat sbuf; 44658f0484fSRodney W. Grimes struct passwd *pwd; 44758f0484fSRodney W. Grimes FILE *hostf; 44858f0484fSRodney W. Grimes uid_t uid; 44958f0484fSRodney W. Grimes int first; 45058f0484fSRodney W. Grimes char pbuf[MAXPATHLEN]; 451eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 452eb74b996SHajimu UMEMOTO struct sockaddr_storage ss; 45342b4f28eSYoshinobu Inoue 454eb74b996SHajimu UMEMOTO /* avoid alignment issue */ 455eb74b996SHajimu UMEMOTO if (rlen > sizeof(ss)) 456eb74b996SHajimu UMEMOTO return(-1); 457eb74b996SHajimu UMEMOTO memcpy(&ss, ra, rlen); 458eb74b996SHajimu UMEMOTO raddr = (struct sockaddr *)&ss; 45958f0484fSRodney W. Grimes 46058f0484fSRodney W. Grimes first = 1; 461*a93705b0SJilles Tjoelker hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "re"); 46258f0484fSRodney W. Grimes again: 46358f0484fSRodney W. Grimes if (hostf) { 464eb74b996SHajimu UMEMOTO if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) { 46558f0484fSRodney W. Grimes (void)fclose(hostf); 46658f0484fSRodney W. Grimes return (0); 46758f0484fSRodney W. Grimes } 46858f0484fSRodney W. Grimes (void)fclose(hostf); 46958f0484fSRodney W. Grimes } 47058f0484fSRodney W. Grimes if (first == 1 && (__check_rhosts_file || superuser)) { 47158f0484fSRodney W. Grimes first = 0; 47258f0484fSRodney W. Grimes if ((pwd = getpwnam(luser)) == NULL) 47358f0484fSRodney W. Grimes return (-1); 47458f0484fSRodney W. Grimes (void)strcpy(pbuf, pwd->pw_dir); 47558f0484fSRodney W. Grimes (void)strcat(pbuf, "/.rhosts"); 47658f0484fSRodney W. Grimes 47758f0484fSRodney W. Grimes /* 47858f0484fSRodney W. Grimes * Change effective uid while opening .rhosts. If root and 47958f0484fSRodney W. Grimes * reading an NFS mounted file system, can't read files that 48058f0484fSRodney W. Grimes * are protected read/write owner only. 48158f0484fSRodney W. Grimes */ 48258f0484fSRodney W. Grimes uid = geteuid(); 48358f0484fSRodney W. Grimes (void)seteuid(pwd->pw_uid); 484*a93705b0SJilles Tjoelker hostf = fopen(pbuf, "re"); 48558f0484fSRodney W. Grimes (void)seteuid(uid); 48658f0484fSRodney W. Grimes 48758f0484fSRodney W. Grimes if (hostf == NULL) 48858f0484fSRodney W. Grimes return (-1); 48958f0484fSRodney W. Grimes /* 49058f0484fSRodney W. Grimes * If not a regular file, or is owned by someone other than 49158f0484fSRodney W. Grimes * user or root or if writeable by anyone but the owner, quit. 49258f0484fSRodney W. Grimes */ 49358f0484fSRodney W. Grimes cp = NULL; 49458f0484fSRodney W. Grimes if (lstat(pbuf, &sbuf) < 0) 49558f0484fSRodney W. Grimes cp = ".rhosts lstat failed"; 49658f0484fSRodney W. Grimes else if (!S_ISREG(sbuf.st_mode)) 49758f0484fSRodney W. Grimes cp = ".rhosts not regular file"; 498d201fe46SDaniel Eischen else if (_fstat(fileno(hostf), &sbuf) < 0) 49958f0484fSRodney W. Grimes cp = ".rhosts fstat failed"; 50058f0484fSRodney W. Grimes else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 50158f0484fSRodney W. Grimes cp = "bad .rhosts owner"; 50258f0484fSRodney W. Grimes else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 50358f0484fSRodney W. Grimes cp = ".rhosts writeable by other than owner"; 50458f0484fSRodney W. Grimes /* If there were any problems, quit. */ 50558f0484fSRodney W. Grimes if (cp) { 50658f0484fSRodney W. Grimes __rcmd_errstr = cp; 50758f0484fSRodney W. Grimes (void)fclose(hostf); 50858f0484fSRodney W. Grimes return (-1); 50958f0484fSRodney W. Grimes } 51058f0484fSRodney W. Grimes goto again; 51158f0484fSRodney W. Grimes } 51258f0484fSRodney W. Grimes return (-1); 51358f0484fSRodney W. Grimes } 51458f0484fSRodney W. Grimes 51558f0484fSRodney W. Grimes /* 51658f0484fSRodney W. Grimes * XXX 51758f0484fSRodney W. Grimes * Don't make static, used by lpd(8). 51858f0484fSRodney W. Grimes * 51958f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 52058f0484fSRodney W. Grimes */ 52158f0484fSRodney W. Grimes int 52258f0484fSRodney W. Grimes __ivaliduser(hostf, raddr, luser, ruser) 52358f0484fSRodney W. Grimes FILE *hostf; 524e0ce825eSDoug Rabson u_int32_t raddr; 52558f0484fSRodney W. Grimes const char *luser, *ruser; 52658f0484fSRodney W. Grimes { 527eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 528eb74b996SHajimu UMEMOTO 529eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 530eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 531eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 532eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 533eb74b996SHajimu UMEMOTO return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, 534eb74b996SHajimu UMEMOTO luser, ruser); 53542b4f28eSYoshinobu Inoue } 53642b4f28eSYoshinobu Inoue 537eb74b996SHajimu UMEMOTO /* 538eb74b996SHajimu UMEMOTO * Returns 0 if ok, -1 if not ok. 539eb74b996SHajimu UMEMOTO * 540eb74b996SHajimu UMEMOTO * XXX obsolete API. 541eb74b996SHajimu UMEMOTO */ 54242b4f28eSYoshinobu Inoue int 54342b4f28eSYoshinobu Inoue __ivaliduser_af(hostf, raddr, luser, ruser, af, len) 54442b4f28eSYoshinobu Inoue FILE *hostf; 545eb74b996SHajimu UMEMOTO const void *raddr; 54642b4f28eSYoshinobu Inoue const char *luser, *ruser; 54742b4f28eSYoshinobu Inoue int af, len; 54842b4f28eSYoshinobu Inoue { 549eb74b996SHajimu UMEMOTO struct sockaddr *sa = NULL; 550eb74b996SHajimu UMEMOTO struct sockaddr_in *sin = NULL; 551eb74b996SHajimu UMEMOTO #ifdef INET6 552eb74b996SHajimu UMEMOTO struct sockaddr_in6 *sin6 = NULL; 553eb74b996SHajimu UMEMOTO #endif 554eb74b996SHajimu UMEMOTO struct sockaddr_storage ss; 555eb74b996SHajimu UMEMOTO 556eb74b996SHajimu UMEMOTO memset(&ss, 0, sizeof(ss)); 557eb74b996SHajimu UMEMOTO switch (af) { 558eb74b996SHajimu UMEMOTO case AF_INET: 559eb74b996SHajimu UMEMOTO if (len != sizeof(sin->sin_addr)) 560eb74b996SHajimu UMEMOTO return -1; 561eb74b996SHajimu UMEMOTO sin = (struct sockaddr_in *)&ss; 562eb74b996SHajimu UMEMOTO sin->sin_family = AF_INET; 563eb74b996SHajimu UMEMOTO sin->sin_len = sizeof(struct sockaddr_in); 564eb74b996SHajimu UMEMOTO memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr)); 565eb74b996SHajimu UMEMOTO break; 566eb74b996SHajimu UMEMOTO #ifdef INET6 567eb74b996SHajimu UMEMOTO case AF_INET6: 568eb74b996SHajimu UMEMOTO if (len != sizeof(sin6->sin6_addr)) 569eb74b996SHajimu UMEMOTO return -1; 570eb74b996SHajimu UMEMOTO /* you will lose scope info */ 571eb74b996SHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)&ss; 572eb74b996SHajimu UMEMOTO sin6->sin6_family = AF_INET6; 573eb74b996SHajimu UMEMOTO sin6->sin6_len = sizeof(struct sockaddr_in6); 574eb74b996SHajimu UMEMOTO memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr)); 575eb74b996SHajimu UMEMOTO break; 576eb74b996SHajimu UMEMOTO #endif 577eb74b996SHajimu UMEMOTO default: 578eb74b996SHajimu UMEMOTO return -1; 579eb74b996SHajimu UMEMOTO } 580eb74b996SHajimu UMEMOTO 581eb74b996SHajimu UMEMOTO sa = (struct sockaddr *)&ss; 582eb74b996SHajimu UMEMOTO return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser); 583eb74b996SHajimu UMEMOTO } 584eb74b996SHajimu UMEMOTO 585eb74b996SHajimu UMEMOTO int 586eb74b996SHajimu UMEMOTO __ivaliduser_sa(hostf, raddr, salen, luser, ruser) 587eb74b996SHajimu UMEMOTO FILE *hostf; 588eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 589eb74b996SHajimu UMEMOTO socklen_t salen; 590eb74b996SHajimu UMEMOTO const char *luser, *ruser; 591eb74b996SHajimu UMEMOTO { 5928fb3f3f6SDavid E. O'Brien char *user, *p; 59358f0484fSRodney W. Grimes int ch; 59458f0484fSRodney W. Grimes char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 59597cb5094SBill Paul char hname[MAXHOSTNAMELEN]; 5968538335fSBill Paul /* Presumed guilty until proven innocent. */ 5978538335fSBill Paul int userok = 0, hostok = 0; 5981e890b05SBill Paul #ifdef YP 5991e890b05SBill Paul char *ypdomain; 6008538335fSBill Paul 6011e890b05SBill Paul if (yp_get_default_domain(&ypdomain)) 6021e890b05SBill Paul ypdomain = NULL; 6031e890b05SBill Paul #else 6041e890b05SBill Paul #define ypdomain NULL 6051e890b05SBill Paul #endif 6068538335fSBill Paul /* We need to get the damn hostname back for netgroup matching. */ 607eb74b996SHajimu UMEMOTO if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0, 608eb74b996SHajimu UMEMOTO NI_NAMEREQD) != 0) 609c97c8f4aSJohn Polstra hname[0] = '\0'; 61058f0484fSRodney W. Grimes 61158f0484fSRodney W. Grimes while (fgets(buf, sizeof(buf), hostf)) { 61258f0484fSRodney W. Grimes p = buf; 61358f0484fSRodney W. Grimes /* Skip lines that are too long. */ 614a9f9141cSBrian S. Dean if (strchr(p, '\n') == NULL) { 61558f0484fSRodney W. Grimes while ((ch = getc(hostf)) != '\n' && ch != EOF); 61658f0484fSRodney W. Grimes continue; 61758f0484fSRodney W. Grimes } 618acc7e87cSPeter Wemm if (*p == '\n' || *p == '#') { 619acc7e87cSPeter Wemm /* comment... */ 620acc7e87cSPeter Wemm continue; 621acc7e87cSPeter Wemm } 62258f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 6233fb85bffSAndrey A. Chernov *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 62458f0484fSRodney W. Grimes p++; 62558f0484fSRodney W. Grimes } 62658f0484fSRodney W. Grimes if (*p == ' ' || *p == '\t') { 62758f0484fSRodney W. Grimes *p++ = '\0'; 62858f0484fSRodney W. Grimes while (*p == ' ' || *p == '\t') 62958f0484fSRodney W. Grimes p++; 63058f0484fSRodney W. Grimes user = p; 63158f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && 63258f0484fSRodney W. Grimes *p != '\t' && *p != '\0') 63358f0484fSRodney W. Grimes p++; 63458f0484fSRodney W. Grimes } else 63558f0484fSRodney W. Grimes user = p; 63658f0484fSRodney W. Grimes *p = '\0'; 6378538335fSBill Paul /* 6388538335fSBill Paul * Do +/- and +@/-@ checking. This looks really nasty, 6398538335fSBill Paul * but it matches SunOS's behavior so far as I can tell. 6408538335fSBill Paul */ 6418538335fSBill Paul switch(buf[0]) { 6428538335fSBill Paul case '+': 6438538335fSBill Paul if (!buf[1]) { /* '+' matches all hosts */ 6448538335fSBill Paul hostok = 1; 6458538335fSBill Paul break; 64658f0484fSRodney W. Grimes } 6478538335fSBill Paul if (buf[1] == '@') /* match a host by netgroup */ 648c97c8f4aSJohn Polstra hostok = hname[0] != '\0' && 649c97c8f4aSJohn Polstra innetgr(&buf[2], hname, NULL, ypdomain); 6508538335fSBill Paul else /* match a host by addr */ 651eb74b996SHajimu UMEMOTO hostok = __icheckhost(raddr, salen, 652eb74b996SHajimu UMEMOTO (char *)&buf[1]); 6538538335fSBill Paul break; 6548538335fSBill Paul case '-': /* reject '-' hosts and all their users */ 6558538335fSBill Paul if (buf[1] == '@') { 656c97c8f4aSJohn Polstra if (hname[0] == '\0' || 657c97c8f4aSJohn Polstra innetgr(&buf[2], hname, NULL, ypdomain)) 6588538335fSBill Paul return(-1); 6598538335fSBill Paul } else { 660eb74b996SHajimu UMEMOTO if (__icheckhost(raddr, salen, 661eb74b996SHajimu UMEMOTO (char *)&buf[1])) 6628538335fSBill Paul return(-1); 6638538335fSBill Paul } 6648538335fSBill Paul break; 6658538335fSBill Paul default: /* if no '+' or '-', do a simple match */ 666eb74b996SHajimu UMEMOTO hostok = __icheckhost(raddr, salen, buf); 6678538335fSBill Paul break; 6688538335fSBill Paul } 6698538335fSBill Paul switch(*user) { 6708538335fSBill Paul case '+': 6718538335fSBill Paul if (!*(user+1)) { /* '+' matches all users */ 6728538335fSBill Paul userok = 1; 6738538335fSBill Paul break; 6748538335fSBill Paul } 6758538335fSBill Paul if (*(user+1) == '@') /* match a user by netgroup */ 6761e890b05SBill Paul userok = innetgr(user+2, NULL, ruser, ypdomain); 6778538335fSBill Paul else /* match a user by direct specification */ 6788538335fSBill Paul userok = !(strcmp(ruser, user+1)); 6798538335fSBill Paul break; 6808538335fSBill Paul case '-': /* if we matched a hostname, */ 6818538335fSBill Paul if (hostok) { /* check for user field rejections */ 6828538335fSBill Paul if (!*(user+1)) 6838538335fSBill Paul return(-1); 6848538335fSBill Paul if (*(user+1) == '@') { 6858538335fSBill Paul if (innetgr(user+2, NULL, 6861e890b05SBill Paul ruser, ypdomain)) 6878538335fSBill Paul return(-1); 6888538335fSBill Paul } else { 6898538335fSBill Paul if (!strcmp(ruser, user+1)) 6908538335fSBill Paul return(-1); 6918538335fSBill Paul } 6928538335fSBill Paul } 6938538335fSBill Paul break; 6948538335fSBill Paul default: /* no rejections: try to match the user */ 6958538335fSBill Paul if (hostok) 6968538335fSBill Paul userok = !(strcmp(ruser,*user ? user : luser)); 6978538335fSBill Paul break; 6988538335fSBill Paul } 6998538335fSBill Paul if (hostok && userok) 7008538335fSBill Paul return(0); 70158f0484fSRodney W. Grimes } 70258f0484fSRodney W. Grimes return (-1); 70358f0484fSRodney W. Grimes } 70458f0484fSRodney W. Grimes 70558f0484fSRodney W. Grimes /* 70658f0484fSRodney W. Grimes * Returns "true" if match, 0 if no match. 70758f0484fSRodney W. Grimes */ 70858f0484fSRodney W. Grimes static int 709eb74b996SHajimu UMEMOTO __icheckhost(raddr, salen, lhost) 710eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 711eb74b996SHajimu UMEMOTO socklen_t salen; 712eb74b996SHajimu UMEMOTO const char *lhost; 71358f0484fSRodney W. Grimes { 714eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 715eb74b996SHajimu UMEMOTO struct sockaddr_in6 *sin6; 716eb74b996SHajimu UMEMOTO struct addrinfo hints, *res, *r; 717eb74b996SHajimu UMEMOTO int error; 718eb74b996SHajimu UMEMOTO char h1[NI_MAXHOST], h2[NI_MAXHOST]; 71958f0484fSRodney W. Grimes 720eb74b996SHajimu UMEMOTO if (raddr->sa_family == AF_INET6) { 721eb74b996SHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)raddr; 722eb74b996SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 723eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 724eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 725eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 726eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12], 727eb74b996SHajimu UMEMOTO sizeof(sin.sin_addr)); 728eb74b996SHajimu UMEMOTO raddr = (struct sockaddr *)&sin; 729eb74b996SHajimu UMEMOTO salen = sin.sin_len; 730eb74b996SHajimu UMEMOTO } 731eb74b996SHajimu UMEMOTO } 732eb74b996SHajimu UMEMOTO 733eb74b996SHajimu UMEMOTO h1[0] = '\0'; 734eb74b996SHajimu UMEMOTO if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 7354f101318SHajimu UMEMOTO NI_NUMERICHOST) != 0) 736eb74b996SHajimu UMEMOTO return (0); 737eb74b996SHajimu UMEMOTO 738eb74b996SHajimu UMEMOTO /* Resolve laddr into sockaddr */ 739eb74b996SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 740eb74b996SHajimu UMEMOTO hints.ai_family = raddr->sa_family; 741eb74b996SHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/ 742eb74b996SHajimu UMEMOTO res = NULL; 743eb74b996SHajimu UMEMOTO error = getaddrinfo(lhost, "0", &hints, &res); 744eb74b996SHajimu UMEMOTO if (error) 745eb74b996SHajimu UMEMOTO return (0); 746eb74b996SHajimu UMEMOTO 747eb74b996SHajimu UMEMOTO for (r = res; r ; r = r->ai_next) { 748eb74b996SHajimu UMEMOTO h2[0] = '\0'; 749eb74b996SHajimu UMEMOTO if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 7504f101318SHajimu UMEMOTO NULL, 0, NI_NUMERICHOST) != 0) 751eb74b996SHajimu UMEMOTO continue; 752eb74b996SHajimu UMEMOTO if (strcmp(h1, h2) == 0) { 753eb74b996SHajimu UMEMOTO freeaddrinfo(res); 75442b4f28eSYoshinobu Inoue return (1); 755eb74b996SHajimu UMEMOTO } 75642b4f28eSYoshinobu Inoue } 75758f0484fSRodney W. Grimes 758eb74b996SHajimu UMEMOTO /* No match. */ 759eb74b996SHajimu UMEMOTO freeaddrinfo(res); 76058f0484fSRodney W. Grimes return (0); 76158f0484fSRodney W. Grimes } 762