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" 61bd6060a1SKonstantin Belousov #include "libc_private.h" 6258f0484fSRodney W. Grimes 63c05ac53bSDavid E. O'Brien extern int innetgr( const char *, const char *, const char *, const char * ); 6451295a4dSJordan K. Hubbard 65d1f32ba5SGeoff Rehmet #define max(a, b) ((a > b) ? a : b) 66d1f32ba5SGeoff Rehmet 67c05ac53bSDavid E. O'Brien int __ivaliduser(FILE *, u_int32_t, const char *, const char *); 681372519bSDavid E. O'Brien int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int); 691372519bSDavid E. O'Brien int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *, 701372519bSDavid E. O'Brien const char *); 711372519bSDavid E. O'Brien static int __icheckhost(const struct sockaddr *, socklen_t, const char *); 7242b4f28eSYoshinobu Inoue 73eb74b996SHajimu UMEMOTO char paddr[NI_MAXHOST]; 7458f0484fSRodney W. Grimes 7558f0484fSRodney W. Grimes int 763ba5ea24SCraig Rodrigues rcmd(char **ahost, int rport, const char *locuser, const char *remuser, 773ba5ea24SCraig Rodrigues const char *cmd, int *fd2p) 7858f0484fSRodney W. Grimes { 790cac72f4SYoshinobu Inoue return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 800cac72f4SYoshinobu Inoue } 810cac72f4SYoshinobu Inoue 820cac72f4SYoshinobu Inoue int 833ba5ea24SCraig Rodrigues rcmd_af(char **ahost, int rport, const char *locuser, const char *remuser, 843ba5ea24SCraig Rodrigues const char *cmd, int *fd2p, int af) 850cac72f4SYoshinobu Inoue { 8642b4f28eSYoshinobu Inoue struct addrinfo hints, *res, *ai; 8742b4f28eSYoshinobu Inoue struct sockaddr_storage from; 8858f0484fSRodney W. Grimes fd_set reads; 89d201fe46SDaniel Eischen sigset_t oldmask, newmask; 9058f0484fSRodney W. Grimes pid_t pid; 9142b4f28eSYoshinobu Inoue int s, aport, lport, timo, error; 92d4567212SWarner Losh char c, *p; 93eb74b996SHajimu UMEMOTO int refused, nres; 94c98e299eSHajimu UMEMOTO char num[8]; 95c98e299eSHajimu UMEMOTO static char canonnamebuf[MAXDNAME]; /* is it proper here? */ 9658f0484fSRodney W. Grimes 97d4567212SWarner Losh /* call rcmdsh() with specified remote shell if appropriate. */ 98d4567212SWarner Losh if (!issetugid() && (p = getenv("RSH"))) { 99d4567212SWarner Losh struct servent *sp = getservbyname("shell", "tcp"); 100d4567212SWarner Losh 101d4567212SWarner Losh if (sp && sp->s_port == rport) 102d4567212SWarner Losh return (rcmdsh(ahost, rport, locuser, remuser, 103d4567212SWarner Losh cmd, p)); 104d4567212SWarner Losh } 105d4567212SWarner Losh 106d4567212SWarner Losh /* use rsh(1) if non-root and remote port is shell. */ 107d4567212SWarner Losh if (geteuid()) { 108d4567212SWarner Losh struct servent *sp = getservbyname("shell", "tcp"); 109d4567212SWarner Losh 110d4567212SWarner Losh if (sp && sp->s_port == rport) 111d4567212SWarner Losh return (rcmdsh(ahost, rport, locuser, remuser, 112d4567212SWarner Losh cmd, NULL)); 113d4567212SWarner Losh } 114d4567212SWarner Losh 11558f0484fSRodney W. Grimes pid = getpid(); 11642b4f28eSYoshinobu Inoue 11742b4f28eSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 11842b4f28eSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 1190cac72f4SYoshinobu Inoue hints.ai_family = af; 12042b4f28eSYoshinobu Inoue hints.ai_socktype = SOCK_STREAM; 12142b4f28eSYoshinobu Inoue hints.ai_protocol = 0; 12242b4f28eSYoshinobu Inoue (void)snprintf(num, sizeof(num), "%d", ntohs(rport)); 12342b4f28eSYoshinobu Inoue error = getaddrinfo(*ahost, num, &hints, &res); 12442b4f28eSYoshinobu Inoue if (error) { 12542b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 12642b4f28eSYoshinobu Inoue gai_strerror(error)); 12742b4f28eSYoshinobu Inoue if (error == EAI_SYSTEM) 12842b4f28eSYoshinobu Inoue fprintf(stderr, "rcmd: getaddrinfo: %s\n", 12942b4f28eSYoshinobu Inoue strerror(errno)); 13058f0484fSRodney W. Grimes return (-1); 13158f0484fSRodney W. Grimes } 132c98e299eSHajimu UMEMOTO 133c98e299eSHajimu UMEMOTO if (res->ai_canonname 134c98e299eSHajimu UMEMOTO && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) { 135c98e299eSHajimu UMEMOTO strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf)); 136c98e299eSHajimu UMEMOTO *ahost = canonnamebuf; 137c98e299eSHajimu UMEMOTO } 138eb74b996SHajimu UMEMOTO nres = 0; 139eb74b996SHajimu UMEMOTO for (ai = res; ai; ai = ai->ai_next) 140eb74b996SHajimu UMEMOTO nres++; 14142b4f28eSYoshinobu Inoue ai = res; 142eb74b996SHajimu UMEMOTO refused = 0; 143d201fe46SDaniel Eischen sigemptyset(&newmask); 144d201fe46SDaniel Eischen sigaddset(&newmask, SIGURG); 145bd6060a1SKonstantin Belousov __libc_sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask); 14658f0484fSRodney W. Grimes for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 14742b4f28eSYoshinobu Inoue s = rresvport_af(&lport, ai->ai_family); 14858f0484fSRodney W. Grimes if (s < 0) { 149474ce1d1SYoshinobu Inoue if (errno != EAGAIN && ai->ai_next) { 150474ce1d1SYoshinobu Inoue ai = ai->ai_next; 151474ce1d1SYoshinobu Inoue continue; 152474ce1d1SYoshinobu Inoue } 15358f0484fSRodney W. Grimes if (errno == EAGAIN) 15458f0484fSRodney W. Grimes (void)fprintf(stderr, 15558f0484fSRodney W. Grimes "rcmd: socket: All ports in use\n"); 15658f0484fSRodney W. Grimes else 15758f0484fSRodney W. Grimes (void)fprintf(stderr, "rcmd: socket: %s\n", 15858f0484fSRodney W. Grimes strerror(errno)); 15942b4f28eSYoshinobu Inoue freeaddrinfo(res); 160bd6060a1SKonstantin Belousov __libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 161d201fe46SDaniel Eischen NULL); 16258f0484fSRodney W. Grimes return (-1); 16358f0484fSRodney W. Grimes } 1649233c4d9SJason Evans _fcntl(s, F_SETOWN, pid); 165d201fe46SDaniel Eischen if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 16658f0484fSRodney W. Grimes break; 1679233c4d9SJason Evans (void)_close(s); 16858f0484fSRodney W. Grimes if (errno == EADDRINUSE) { 16958f0484fSRodney W. Grimes lport--; 17058f0484fSRodney W. Grimes continue; 17158f0484fSRodney W. Grimes } 172eb74b996SHajimu UMEMOTO if (errno == ECONNREFUSED) 173eb74b996SHajimu UMEMOTO refused = 1; 174eb74b996SHajimu UMEMOTO if (ai->ai_next == NULL && (!refused || timo > 16)) { 175eb74b996SHajimu UMEMOTO (void)fprintf(stderr, "%s: %s\n", 176eb74b996SHajimu UMEMOTO *ahost, strerror(errno)); 177eb74b996SHajimu UMEMOTO freeaddrinfo(res); 178bd6060a1SKonstantin Belousov __libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 179d201fe46SDaniel Eischen NULL); 180eb74b996SHajimu UMEMOTO return (-1); 1812368b03bSHajimu UMEMOTO } 182eb74b996SHajimu UMEMOTO if (nres > 1) { 18358f0484fSRodney W. Grimes int oerrno = errno; 18458f0484fSRodney W. Grimes 1854f101318SHajimu UMEMOTO getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr, 1864f101318SHajimu UMEMOTO sizeof(paddr), NULL, 0, NI_NUMERICHOST); 18758f0484fSRodney W. Grimes (void)fprintf(stderr, "connect to address %s: ", 18842b4f28eSYoshinobu Inoue paddr); 18958f0484fSRodney W. Grimes errno = oerrno; 19058f0484fSRodney W. Grimes perror(0); 191eb74b996SHajimu UMEMOTO } 192eb74b996SHajimu UMEMOTO if ((ai = ai->ai_next) == NULL) { 193eb74b996SHajimu UMEMOTO /* refused && timo <= 16 */ 194eb74b996SHajimu UMEMOTO struct timespec time_to_sleep, time_remaining; 195eb74b996SHajimu UMEMOTO 196eb74b996SHajimu UMEMOTO time_to_sleep.tv_sec = timo; 197eb74b996SHajimu UMEMOTO time_to_sleep.tv_nsec = 0; 198eb74b996SHajimu UMEMOTO (void)_nanosleep(&time_to_sleep, &time_remaining); 199eb74b996SHajimu UMEMOTO timo *= 2; 200eb74b996SHajimu UMEMOTO ai = res; 201eb74b996SHajimu UMEMOTO refused = 0; 202eb74b996SHajimu UMEMOTO } 203eb74b996SHajimu UMEMOTO if (nres > 1) { 2044f101318SHajimu UMEMOTO getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr, 2054f101318SHajimu UMEMOTO sizeof(paddr), NULL, 0, NI_NUMERICHOST); 20642b4f28eSYoshinobu Inoue fprintf(stderr, "Trying %s...\n", paddr); 20758f0484fSRodney W. Grimes } 20858f0484fSRodney W. Grimes } 20958f0484fSRodney W. Grimes lport--; 210*513004a2SPedro F. Giffuni if (fd2p == NULL) { 2119233c4d9SJason Evans _write(s, "", 1); 21258f0484fSRodney W. Grimes lport = 0; 21358f0484fSRodney W. Grimes } else { 21442b4f28eSYoshinobu Inoue int s2 = rresvport_af(&lport, ai->ai_family), s3; 21510248e3aSStefan Farfeleder socklen_t len = ai->ai_addrlen; 216d1f32ba5SGeoff Rehmet int nfds; 21758f0484fSRodney W. Grimes 21858f0484fSRodney W. Grimes if (s2 < 0) 21958f0484fSRodney W. Grimes goto bad; 220d201fe46SDaniel Eischen _listen(s2, 1); 22158f0484fSRodney W. Grimes (void)snprintf(num, sizeof(num), "%d", lport); 2229233c4d9SJason Evans if (_write(s, num, strlen(num)+1) != strlen(num)+1) { 22358f0484fSRodney W. Grimes (void)fprintf(stderr, 22458f0484fSRodney W. Grimes "rcmd: write (setting up stderr): %s\n", 22558f0484fSRodney W. Grimes strerror(errno)); 2269233c4d9SJason Evans (void)_close(s2); 22758f0484fSRodney W. Grimes goto bad; 22858f0484fSRodney W. Grimes } 229d1f32ba5SGeoff Rehmet nfds = max(s, s2)+1; 230d1f32ba5SGeoff Rehmet if(nfds > FD_SETSIZE) { 231d1f32ba5SGeoff Rehmet fprintf(stderr, "rcmd: too many files\n"); 2329233c4d9SJason Evans (void)_close(s2); 233d1f32ba5SGeoff Rehmet goto bad; 234d1f32ba5SGeoff Rehmet } 235ce70b6caSPeter Wemm again: 23658f0484fSRodney W. Grimes FD_ZERO(&reads); 23758f0484fSRodney W. Grimes FD_SET(s, &reads); 23858f0484fSRodney W. Grimes FD_SET(s2, &reads); 23958f0484fSRodney W. Grimes errno = 0; 240d201fe46SDaniel Eischen if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 24158f0484fSRodney W. Grimes if (errno != 0) 24258f0484fSRodney W. Grimes (void)fprintf(stderr, 24358f0484fSRodney W. Grimes "rcmd: select (setting up stderr): %s\n", 24458f0484fSRodney W. Grimes strerror(errno)); 24558f0484fSRodney W. Grimes else 24658f0484fSRodney W. Grimes (void)fprintf(stderr, 24758f0484fSRodney W. Grimes "select: protocol failure in circuit setup\n"); 2489233c4d9SJason Evans (void)_close(s2); 24958f0484fSRodney W. Grimes goto bad; 25058f0484fSRodney W. Grimes } 251d201fe46SDaniel Eischen s3 = _accept(s2, (struct sockaddr *)&from, &len); 25242b4f28eSYoshinobu Inoue switch (from.ss_family) { 25342b4f28eSYoshinobu Inoue case AF_INET: 25442b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 25542b4f28eSYoshinobu Inoue break; 25642b4f28eSYoshinobu Inoue #ifdef INET6 25742b4f28eSYoshinobu Inoue case AF_INET6: 25842b4f28eSYoshinobu Inoue aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 25942b4f28eSYoshinobu Inoue break; 26042b4f28eSYoshinobu Inoue #endif 26142b4f28eSYoshinobu Inoue default: 26242b4f28eSYoshinobu Inoue aport = 0; /* error */ 26342b4f28eSYoshinobu Inoue break; 26442b4f28eSYoshinobu Inoue } 265ce70b6caSPeter Wemm /* 266ce70b6caSPeter Wemm * XXX careful for ftp bounce attacks. If discovered, shut them 267ce70b6caSPeter Wemm * down and check for the real auxiliary channel to connect. 268ce70b6caSPeter Wemm */ 26942b4f28eSYoshinobu Inoue if (aport == 20) { 2709233c4d9SJason Evans _close(s3); 271ce70b6caSPeter Wemm goto again; 272ce70b6caSPeter Wemm } 2739233c4d9SJason Evans (void)_close(s2); 27458f0484fSRodney W. Grimes if (s3 < 0) { 27558f0484fSRodney W. Grimes (void)fprintf(stderr, 27658f0484fSRodney W. Grimes "rcmd: accept: %s\n", strerror(errno)); 27758f0484fSRodney W. Grimes lport = 0; 27858f0484fSRodney W. Grimes goto bad; 27958f0484fSRodney W. Grimes } 28058f0484fSRodney W. Grimes *fd2p = s3; 28142b4f28eSYoshinobu Inoue if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 28258f0484fSRodney W. Grimes (void)fprintf(stderr, 28358f0484fSRodney W. Grimes "socket: protocol failure in circuit setup.\n"); 28458f0484fSRodney W. Grimes goto bad2; 28558f0484fSRodney W. Grimes } 28658f0484fSRodney W. Grimes } 2879233c4d9SJason Evans (void)_write(s, locuser, strlen(locuser)+1); 2889233c4d9SJason Evans (void)_write(s, remuser, strlen(remuser)+1); 2899233c4d9SJason Evans (void)_write(s, cmd, strlen(cmd)+1); 2909233c4d9SJason Evans if (_read(s, &c, 1) != 1) { 29158f0484fSRodney W. Grimes (void)fprintf(stderr, 29258f0484fSRodney W. Grimes "rcmd: %s: %s\n", *ahost, strerror(errno)); 29358f0484fSRodney W. Grimes goto bad2; 29458f0484fSRodney W. Grimes } 29558f0484fSRodney W. Grimes if (c != 0) { 2969233c4d9SJason Evans while (_read(s, &c, 1) == 1) { 2979233c4d9SJason Evans (void)_write(STDERR_FILENO, &c, 1); 29858f0484fSRodney W. Grimes if (c == '\n') 29958f0484fSRodney W. Grimes break; 30058f0484fSRodney W. Grimes } 30158f0484fSRodney W. Grimes goto bad2; 30258f0484fSRodney W. Grimes } 303bd6060a1SKonstantin Belousov __libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 30442b4f28eSYoshinobu Inoue freeaddrinfo(res); 30558f0484fSRodney W. Grimes return (s); 30658f0484fSRodney W. Grimes bad2: 30758f0484fSRodney W. Grimes if (lport) 3089233c4d9SJason Evans (void)_close(*fd2p); 30958f0484fSRodney W. Grimes bad: 3109233c4d9SJason Evans (void)_close(s); 311bd6060a1SKonstantin Belousov __libc_sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 31242b4f28eSYoshinobu Inoue freeaddrinfo(res); 31358f0484fSRodney W. Grimes return (-1); 31458f0484fSRodney W. Grimes } 31558f0484fSRodney W. Grimes 31658f0484fSRodney W. Grimes int 3173ba5ea24SCraig Rodrigues rresvport(int *port) 31858f0484fSRodney W. Grimes { 31942b4f28eSYoshinobu Inoue return rresvport_af(port, AF_INET); 32042b4f28eSYoshinobu Inoue } 32158f0484fSRodney W. Grimes 32242b4f28eSYoshinobu Inoue int 3233ba5ea24SCraig Rodrigues rresvport_af(int *alport, int family) 32442b4f28eSYoshinobu Inoue { 3256d7bd75aSJacques Vidrine int s; 32642b4f28eSYoshinobu Inoue struct sockaddr_storage ss; 32742b4f28eSYoshinobu Inoue u_short *sport; 32842b4f28eSYoshinobu Inoue 32942b4f28eSYoshinobu Inoue memset(&ss, 0, sizeof(ss)); 33042b4f28eSYoshinobu Inoue ss.ss_family = family; 33142b4f28eSYoshinobu Inoue switch (family) { 33242b4f28eSYoshinobu Inoue case AF_INET: 33342b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 33442b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in *)&ss)->sin_port; 33542b4f28eSYoshinobu Inoue ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 33642b4f28eSYoshinobu Inoue break; 33742b4f28eSYoshinobu Inoue #ifdef INET6 33842b4f28eSYoshinobu Inoue case AF_INET6: 33942b4f28eSYoshinobu Inoue ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 34042b4f28eSYoshinobu Inoue sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 34142b4f28eSYoshinobu Inoue ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 34242b4f28eSYoshinobu Inoue break; 34342b4f28eSYoshinobu Inoue #endif 34442b4f28eSYoshinobu Inoue default: 34542b4f28eSYoshinobu Inoue errno = EAFNOSUPPORT; 34642b4f28eSYoshinobu Inoue return -1; 34742b4f28eSYoshinobu Inoue } 34842b4f28eSYoshinobu Inoue 349d201fe46SDaniel Eischen s = _socket(ss.ss_family, SOCK_STREAM, 0); 35058f0484fSRodney W. Grimes if (s < 0) 35158f0484fSRodney W. Grimes return (-1); 352ce70b6caSPeter Wemm #if 0 /* compat_exact_traditional_rresvport_semantics */ 353ce70b6caSPeter Wemm sin.sin_port = htons((u_short)*alport); 354d201fe46SDaniel Eischen if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 35558f0484fSRodney W. Grimes return (s); 356ce70b6caSPeter Wemm if (errno != EADDRINUSE) { 3579233c4d9SJason Evans (void)_close(s); 35858f0484fSRodney W. Grimes return (-1); 35958f0484fSRodney W. Grimes } 360ce70b6caSPeter Wemm #endif 36142b4f28eSYoshinobu Inoue *sport = 0; 362ae42b666SYoshinobu Inoue if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { 3639233c4d9SJason Evans (void)_close(s); 364ce70b6caSPeter Wemm return (-1); 365ce70b6caSPeter Wemm } 36642b4f28eSYoshinobu Inoue *alport = (int)ntohs(*sport); 367ce70b6caSPeter Wemm return (s); 368ce70b6caSPeter Wemm } 36958f0484fSRodney W. Grimes 37058f0484fSRodney W. Grimes int __check_rhosts_file = 1; 37158f0484fSRodney W. Grimes char *__rcmd_errstr; 37258f0484fSRodney W. Grimes 37358f0484fSRodney W. Grimes int 3743ba5ea24SCraig Rodrigues ruserok(const char *rhost, int superuser, const char *ruser, const char *luser) 37558f0484fSRodney W. Grimes { 376e3be4d7bSYoshinobu Inoue struct addrinfo hints, *res, *r; 377e3be4d7bSYoshinobu Inoue int error; 37842b4f28eSYoshinobu Inoue 379e3be4d7bSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 380e3be4d7bSYoshinobu Inoue hints.ai_family = PF_UNSPEC; 381e3be4d7bSYoshinobu Inoue hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 382e3be4d7bSYoshinobu Inoue error = getaddrinfo(rhost, "0", &hints, &res); 383e3be4d7bSYoshinobu Inoue if (error) 38458f0484fSRodney W. Grimes return (-1); 385e3be4d7bSYoshinobu Inoue 386e3be4d7bSYoshinobu Inoue for (r = res; r; r = r->ai_next) { 387e3be4d7bSYoshinobu Inoue if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 388e3be4d7bSYoshinobu Inoue luser) == 0) { 389e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 390e3be4d7bSYoshinobu Inoue return (0); 39142b4f28eSYoshinobu Inoue } 39242b4f28eSYoshinobu Inoue } 393e3be4d7bSYoshinobu Inoue freeaddrinfo(res); 394e3be4d7bSYoshinobu Inoue return (-1); 39558f0484fSRodney W. Grimes } 39658f0484fSRodney W. Grimes 39758f0484fSRodney W. Grimes /* 39858f0484fSRodney W. Grimes * New .rhosts strategy: We are passed an ip address. We spin through 39958f0484fSRodney W. Grimes * hosts.equiv and .rhosts looking for a match. When the .rhosts only 40058f0484fSRodney W. Grimes * has ip addresses, we don't have to trust a nameserver. When it 40158f0484fSRodney W. Grimes * contains hostnames, we spin through the list of addresses the nameserver 40258f0484fSRodney W. Grimes * gives us and look for a match. 40358f0484fSRodney W. Grimes * 40458f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 40558f0484fSRodney W. Grimes */ 40658f0484fSRodney W. Grimes int 4073ba5ea24SCraig Rodrigues iruserok(unsigned long raddr, int superuser, const char *ruser, const char *luser) 40858f0484fSRodney W. Grimes { 409eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 410eb74b996SHajimu UMEMOTO 411eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 412eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 413eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 414eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 415eb74b996SHajimu UMEMOTO return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser, 416eb74b996SHajimu UMEMOTO ruser, luser); 41742b4f28eSYoshinobu Inoue } 41842b4f28eSYoshinobu Inoue 419eb74b996SHajimu UMEMOTO /* 420eb74b996SHajimu UMEMOTO * AF independent extension of iruserok. 421eb74b996SHajimu UMEMOTO * 422eb74b996SHajimu UMEMOTO * Returns 0 if ok, -1 if not ok. 423eb74b996SHajimu UMEMOTO */ 424eb74b996SHajimu UMEMOTO int 4253ba5ea24SCraig Rodrigues iruserok_sa(const void *ra, int rlen, int superuser, const char *ruser, 4263ba5ea24SCraig Rodrigues const char *luser) 42742b4f28eSYoshinobu Inoue { 4288fb3f3f6SDavid E. O'Brien char *cp; 42958f0484fSRodney W. Grimes struct stat sbuf; 43058f0484fSRodney W. Grimes struct passwd *pwd; 43158f0484fSRodney W. Grimes FILE *hostf; 43258f0484fSRodney W. Grimes uid_t uid; 43358f0484fSRodney W. Grimes int first; 43458f0484fSRodney W. Grimes char pbuf[MAXPATHLEN]; 435eb74b996SHajimu UMEMOTO const struct sockaddr *raddr; 436eb74b996SHajimu UMEMOTO struct sockaddr_storage ss; 43742b4f28eSYoshinobu Inoue 438eb74b996SHajimu UMEMOTO /* avoid alignment issue */ 439eb74b996SHajimu UMEMOTO if (rlen > sizeof(ss)) 440eb74b996SHajimu UMEMOTO return(-1); 441eb74b996SHajimu UMEMOTO memcpy(&ss, ra, rlen); 442eb74b996SHajimu UMEMOTO raddr = (struct sockaddr *)&ss; 44358f0484fSRodney W. Grimes 44458f0484fSRodney W. Grimes first = 1; 445a93705b0SJilles Tjoelker hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "re"); 44658f0484fSRodney W. Grimes again: 44758f0484fSRodney W. Grimes if (hostf) { 448eb74b996SHajimu UMEMOTO if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) { 44958f0484fSRodney W. Grimes (void)fclose(hostf); 45058f0484fSRodney W. Grimes return (0); 45158f0484fSRodney W. Grimes } 45258f0484fSRodney W. Grimes (void)fclose(hostf); 45358f0484fSRodney W. Grimes } 45458f0484fSRodney W. Grimes if (first == 1 && (__check_rhosts_file || superuser)) { 45558f0484fSRodney W. Grimes first = 0; 45658f0484fSRodney W. Grimes if ((pwd = getpwnam(luser)) == NULL) 45758f0484fSRodney W. Grimes return (-1); 45858f0484fSRodney W. Grimes (void)strcpy(pbuf, pwd->pw_dir); 45958f0484fSRodney W. Grimes (void)strcat(pbuf, "/.rhosts"); 46058f0484fSRodney W. Grimes 46158f0484fSRodney W. Grimes /* 46258f0484fSRodney W. Grimes * Change effective uid while opening .rhosts. If root and 46358f0484fSRodney W. Grimes * reading an NFS mounted file system, can't read files that 46458f0484fSRodney W. Grimes * are protected read/write owner only. 46558f0484fSRodney W. Grimes */ 46658f0484fSRodney W. Grimes uid = geteuid(); 46758f0484fSRodney W. Grimes (void)seteuid(pwd->pw_uid); 468a93705b0SJilles Tjoelker hostf = fopen(pbuf, "re"); 46958f0484fSRodney W. Grimes (void)seteuid(uid); 47058f0484fSRodney W. Grimes 47158f0484fSRodney W. Grimes if (hostf == NULL) 47258f0484fSRodney W. Grimes return (-1); 47358f0484fSRodney W. Grimes /* 47458f0484fSRodney W. Grimes * If not a regular file, or is owned by someone other than 47558f0484fSRodney W. Grimes * user or root or if writeable by anyone but the owner, quit. 47658f0484fSRodney W. Grimes */ 47758f0484fSRodney W. Grimes cp = NULL; 47858f0484fSRodney W. Grimes if (lstat(pbuf, &sbuf) < 0) 47958f0484fSRodney W. Grimes cp = ".rhosts lstat failed"; 48058f0484fSRodney W. Grimes else if (!S_ISREG(sbuf.st_mode)) 48158f0484fSRodney W. Grimes cp = ".rhosts not regular file"; 482d201fe46SDaniel Eischen else if (_fstat(fileno(hostf), &sbuf) < 0) 48358f0484fSRodney W. Grimes cp = ".rhosts fstat failed"; 48458f0484fSRodney W. Grimes else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 48558f0484fSRodney W. Grimes cp = "bad .rhosts owner"; 48658f0484fSRodney W. Grimes else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 48758f0484fSRodney W. Grimes cp = ".rhosts writeable by other than owner"; 48858f0484fSRodney W. Grimes /* If there were any problems, quit. */ 48958f0484fSRodney W. Grimes if (cp) { 49058f0484fSRodney W. Grimes __rcmd_errstr = cp; 49158f0484fSRodney W. Grimes (void)fclose(hostf); 49258f0484fSRodney W. Grimes return (-1); 49358f0484fSRodney W. Grimes } 49458f0484fSRodney W. Grimes goto again; 49558f0484fSRodney W. Grimes } 49658f0484fSRodney W. Grimes return (-1); 49758f0484fSRodney W. Grimes } 49858f0484fSRodney W. Grimes 49958f0484fSRodney W. Grimes /* 50058f0484fSRodney W. Grimes * XXX 50158f0484fSRodney W. Grimes * Don't make static, used by lpd(8). 50258f0484fSRodney W. Grimes * 50358f0484fSRodney W. Grimes * Returns 0 if ok, -1 if not ok. 50458f0484fSRodney W. Grimes */ 50558f0484fSRodney W. Grimes int 5063ba5ea24SCraig Rodrigues __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser) 50758f0484fSRodney W. Grimes { 508eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 509eb74b996SHajimu UMEMOTO 510eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 511eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 512eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 513eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 514eb74b996SHajimu UMEMOTO return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, 515eb74b996SHajimu UMEMOTO luser, ruser); 51642b4f28eSYoshinobu Inoue } 51742b4f28eSYoshinobu Inoue 518eb74b996SHajimu UMEMOTO /* 519eb74b996SHajimu UMEMOTO * Returns 0 if ok, -1 if not ok. 520eb74b996SHajimu UMEMOTO * 521eb74b996SHajimu UMEMOTO * XXX obsolete API. 522eb74b996SHajimu UMEMOTO */ 52342b4f28eSYoshinobu Inoue int 5243ba5ea24SCraig Rodrigues __ivaliduser_af(FILE *hostf, const void *raddr, const char *luser, 5253ba5ea24SCraig Rodrigues const char *ruser, int af, int len) 52642b4f28eSYoshinobu Inoue { 527eb74b996SHajimu UMEMOTO struct sockaddr *sa = NULL; 528eb74b996SHajimu UMEMOTO struct sockaddr_in *sin = NULL; 529eb74b996SHajimu UMEMOTO #ifdef INET6 530eb74b996SHajimu UMEMOTO struct sockaddr_in6 *sin6 = NULL; 531eb74b996SHajimu UMEMOTO #endif 532eb74b996SHajimu UMEMOTO struct sockaddr_storage ss; 533eb74b996SHajimu UMEMOTO 534eb74b996SHajimu UMEMOTO memset(&ss, 0, sizeof(ss)); 535eb74b996SHajimu UMEMOTO switch (af) { 536eb74b996SHajimu UMEMOTO case AF_INET: 537eb74b996SHajimu UMEMOTO if (len != sizeof(sin->sin_addr)) 538eb74b996SHajimu UMEMOTO return -1; 539eb74b996SHajimu UMEMOTO sin = (struct sockaddr_in *)&ss; 540eb74b996SHajimu UMEMOTO sin->sin_family = AF_INET; 541eb74b996SHajimu UMEMOTO sin->sin_len = sizeof(struct sockaddr_in); 542eb74b996SHajimu UMEMOTO memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr)); 543eb74b996SHajimu UMEMOTO break; 544eb74b996SHajimu UMEMOTO #ifdef INET6 545eb74b996SHajimu UMEMOTO case AF_INET6: 546eb74b996SHajimu UMEMOTO if (len != sizeof(sin6->sin6_addr)) 547eb74b996SHajimu UMEMOTO return -1; 548eb74b996SHajimu UMEMOTO /* you will lose scope info */ 549eb74b996SHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)&ss; 550eb74b996SHajimu UMEMOTO sin6->sin6_family = AF_INET6; 551eb74b996SHajimu UMEMOTO sin6->sin6_len = sizeof(struct sockaddr_in6); 552eb74b996SHajimu UMEMOTO memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr)); 553eb74b996SHajimu UMEMOTO break; 554eb74b996SHajimu UMEMOTO #endif 555eb74b996SHajimu UMEMOTO default: 556eb74b996SHajimu UMEMOTO return -1; 557eb74b996SHajimu UMEMOTO } 558eb74b996SHajimu UMEMOTO 559eb74b996SHajimu UMEMOTO sa = (struct sockaddr *)&ss; 560eb74b996SHajimu UMEMOTO return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser); 561eb74b996SHajimu UMEMOTO } 562eb74b996SHajimu UMEMOTO 563eb74b996SHajimu UMEMOTO int 5643ba5ea24SCraig Rodrigues __ivaliduser_sa(FILE *hostf, const struct sockaddr *raddr, socklen_t salen, 5653ba5ea24SCraig Rodrigues const char *luser, const char *ruser) 566eb74b996SHajimu UMEMOTO { 5678fb3f3f6SDavid E. O'Brien char *user, *p; 56858f0484fSRodney W. Grimes int ch; 56958f0484fSRodney W. Grimes char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 57097cb5094SBill Paul char hname[MAXHOSTNAMELEN]; 5718538335fSBill Paul /* Presumed guilty until proven innocent. */ 5728538335fSBill Paul int userok = 0, hostok = 0; 5731e890b05SBill Paul #ifdef YP 5741e890b05SBill Paul char *ypdomain; 5758538335fSBill Paul 5761e890b05SBill Paul if (yp_get_default_domain(&ypdomain)) 5771e890b05SBill Paul ypdomain = NULL; 5781e890b05SBill Paul #else 5791e890b05SBill Paul #define ypdomain NULL 5801e890b05SBill Paul #endif 5818538335fSBill Paul /* We need to get the damn hostname back for netgroup matching. */ 582eb74b996SHajimu UMEMOTO if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0, 583eb74b996SHajimu UMEMOTO NI_NAMEREQD) != 0) 584c97c8f4aSJohn Polstra hname[0] = '\0'; 58558f0484fSRodney W. Grimes 58658f0484fSRodney W. Grimes while (fgets(buf, sizeof(buf), hostf)) { 58758f0484fSRodney W. Grimes p = buf; 58858f0484fSRodney W. Grimes /* Skip lines that are too long. */ 589a9f9141cSBrian S. Dean if (strchr(p, '\n') == NULL) { 59058f0484fSRodney W. Grimes while ((ch = getc(hostf)) != '\n' && ch != EOF); 59158f0484fSRodney W. Grimes continue; 59258f0484fSRodney W. Grimes } 593acc7e87cSPeter Wemm if (*p == '\n' || *p == '#') { 594acc7e87cSPeter Wemm /* comment... */ 595acc7e87cSPeter Wemm continue; 596acc7e87cSPeter Wemm } 59758f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 5983fb85bffSAndrey A. Chernov *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 59958f0484fSRodney W. Grimes p++; 60058f0484fSRodney W. Grimes } 60158f0484fSRodney W. Grimes if (*p == ' ' || *p == '\t') { 60258f0484fSRodney W. Grimes *p++ = '\0'; 60358f0484fSRodney W. Grimes while (*p == ' ' || *p == '\t') 60458f0484fSRodney W. Grimes p++; 60558f0484fSRodney W. Grimes user = p; 60658f0484fSRodney W. Grimes while (*p != '\n' && *p != ' ' && 60758f0484fSRodney W. Grimes *p != '\t' && *p != '\0') 60858f0484fSRodney W. Grimes p++; 60958f0484fSRodney W. Grimes } else 61058f0484fSRodney W. Grimes user = p; 61158f0484fSRodney W. Grimes *p = '\0'; 6128538335fSBill Paul /* 6138538335fSBill Paul * Do +/- and +@/-@ checking. This looks really nasty, 6148538335fSBill Paul * but it matches SunOS's behavior so far as I can tell. 6158538335fSBill Paul */ 6168538335fSBill Paul switch(buf[0]) { 6178538335fSBill Paul case '+': 6188538335fSBill Paul if (!buf[1]) { /* '+' matches all hosts */ 6198538335fSBill Paul hostok = 1; 6208538335fSBill Paul break; 62158f0484fSRodney W. Grimes } 6228538335fSBill Paul if (buf[1] == '@') /* match a host by netgroup */ 623c97c8f4aSJohn Polstra hostok = hname[0] != '\0' && 624c97c8f4aSJohn Polstra innetgr(&buf[2], hname, NULL, ypdomain); 6258538335fSBill Paul else /* match a host by addr */ 626eb74b996SHajimu UMEMOTO hostok = __icheckhost(raddr, salen, 627eb74b996SHajimu UMEMOTO (char *)&buf[1]); 6288538335fSBill Paul break; 6298538335fSBill Paul case '-': /* reject '-' hosts and all their users */ 6308538335fSBill Paul if (buf[1] == '@') { 631c97c8f4aSJohn Polstra if (hname[0] == '\0' || 632c97c8f4aSJohn Polstra innetgr(&buf[2], hname, NULL, ypdomain)) 6338538335fSBill Paul return(-1); 6348538335fSBill Paul } else { 635eb74b996SHajimu UMEMOTO if (__icheckhost(raddr, salen, 636eb74b996SHajimu UMEMOTO (char *)&buf[1])) 6378538335fSBill Paul return(-1); 6388538335fSBill Paul } 6398538335fSBill Paul break; 6408538335fSBill Paul default: /* if no '+' or '-', do a simple match */ 641eb74b996SHajimu UMEMOTO hostok = __icheckhost(raddr, salen, buf); 6428538335fSBill Paul break; 6438538335fSBill Paul } 6448538335fSBill Paul switch(*user) { 6458538335fSBill Paul case '+': 6468538335fSBill Paul if (!*(user+1)) { /* '+' matches all users */ 6478538335fSBill Paul userok = 1; 6488538335fSBill Paul break; 6498538335fSBill Paul } 6508538335fSBill Paul if (*(user+1) == '@') /* match a user by netgroup */ 6511e890b05SBill Paul userok = innetgr(user+2, NULL, ruser, ypdomain); 6528538335fSBill Paul else /* match a user by direct specification */ 6538538335fSBill Paul userok = !(strcmp(ruser, user+1)); 6548538335fSBill Paul break; 6558538335fSBill Paul case '-': /* if we matched a hostname, */ 6568538335fSBill Paul if (hostok) { /* check for user field rejections */ 6578538335fSBill Paul if (!*(user+1)) 6588538335fSBill Paul return(-1); 6598538335fSBill Paul if (*(user+1) == '@') { 6608538335fSBill Paul if (innetgr(user+2, NULL, 6611e890b05SBill Paul ruser, ypdomain)) 6628538335fSBill Paul return(-1); 6638538335fSBill Paul } else { 6648538335fSBill Paul if (!strcmp(ruser, user+1)) 6658538335fSBill Paul return(-1); 6668538335fSBill Paul } 6678538335fSBill Paul } 6688538335fSBill Paul break; 6698538335fSBill Paul default: /* no rejections: try to match the user */ 6708538335fSBill Paul if (hostok) 6718538335fSBill Paul userok = !(strcmp(ruser,*user ? user : luser)); 6728538335fSBill Paul break; 6738538335fSBill Paul } 6748538335fSBill Paul if (hostok && userok) 6758538335fSBill Paul return(0); 67658f0484fSRodney W. Grimes } 67758f0484fSRodney W. Grimes return (-1); 67858f0484fSRodney W. Grimes } 67958f0484fSRodney W. Grimes 68058f0484fSRodney W. Grimes /* 68158f0484fSRodney W. Grimes * Returns "true" if match, 0 if no match. 68258f0484fSRodney W. Grimes */ 68358f0484fSRodney W. Grimes static int 6843ba5ea24SCraig Rodrigues __icheckhost(const struct sockaddr *raddr, socklen_t salen, const char *lhost) 68558f0484fSRodney W. Grimes { 686eb74b996SHajimu UMEMOTO struct sockaddr_in sin; 687eb74b996SHajimu UMEMOTO struct sockaddr_in6 *sin6; 688eb74b996SHajimu UMEMOTO struct addrinfo hints, *res, *r; 689eb74b996SHajimu UMEMOTO int error; 690eb74b996SHajimu UMEMOTO char h1[NI_MAXHOST], h2[NI_MAXHOST]; 69158f0484fSRodney W. Grimes 692eb74b996SHajimu UMEMOTO if (raddr->sa_family == AF_INET6) { 693eb74b996SHajimu UMEMOTO sin6 = (struct sockaddr_in6 *)raddr; 694eb74b996SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 695eb74b996SHajimu UMEMOTO memset(&sin, 0, sizeof(sin)); 696eb74b996SHajimu UMEMOTO sin.sin_family = AF_INET; 697eb74b996SHajimu UMEMOTO sin.sin_len = sizeof(struct sockaddr_in); 698eb74b996SHajimu UMEMOTO memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12], 699eb74b996SHajimu UMEMOTO sizeof(sin.sin_addr)); 700eb74b996SHajimu UMEMOTO raddr = (struct sockaddr *)&sin; 701eb74b996SHajimu UMEMOTO salen = sin.sin_len; 702eb74b996SHajimu UMEMOTO } 703eb74b996SHajimu UMEMOTO } 704eb74b996SHajimu UMEMOTO 705eb74b996SHajimu UMEMOTO h1[0] = '\0'; 706eb74b996SHajimu UMEMOTO if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 7074f101318SHajimu UMEMOTO NI_NUMERICHOST) != 0) 708eb74b996SHajimu UMEMOTO return (0); 709eb74b996SHajimu UMEMOTO 710eb74b996SHajimu UMEMOTO /* Resolve laddr into sockaddr */ 711eb74b996SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 712eb74b996SHajimu UMEMOTO hints.ai_family = raddr->sa_family; 713eb74b996SHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/ 714eb74b996SHajimu UMEMOTO res = NULL; 715eb74b996SHajimu UMEMOTO error = getaddrinfo(lhost, "0", &hints, &res); 716eb74b996SHajimu UMEMOTO if (error) 717eb74b996SHajimu UMEMOTO return (0); 718eb74b996SHajimu UMEMOTO 719eb74b996SHajimu UMEMOTO for (r = res; r ; r = r->ai_next) { 720eb74b996SHajimu UMEMOTO h2[0] = '\0'; 721eb74b996SHajimu UMEMOTO if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 7224f101318SHajimu UMEMOTO NULL, 0, NI_NUMERICHOST) != 0) 723eb74b996SHajimu UMEMOTO continue; 724eb74b996SHajimu UMEMOTO if (strcmp(h1, h2) == 0) { 725eb74b996SHajimu UMEMOTO freeaddrinfo(res); 72642b4f28eSYoshinobu Inoue return (1); 727eb74b996SHajimu UMEMOTO } 72842b4f28eSYoshinobu Inoue } 72958f0484fSRodney W. Grimes 730eb74b996SHajimu UMEMOTO /* No match. */ 731eb74b996SHajimu UMEMOTO freeaddrinfo(res); 73258f0484fSRodney W. Grimes return (0); 73358f0484fSRodney W. Grimes } 734