1 /*- 2 * Copyright (c) 1994, Garrett Wollman 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/socket.h> 31 #include <netinet/in.h> 32 #include <arpa/inet.h> 33 #include <arpa/nameser.h> 34 #include <netdb.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <ctype.h> 38 #include <errno.h> 39 #include <string.h> 40 #include <stdarg.h> 41 #include <nsswitch.h> 42 #ifdef YP 43 #include <rpc/rpc.h> 44 #include <rpcsvc/yp_prot.h> 45 #include <rpcsvc/ypclnt.h> 46 #endif 47 48 #define MAXALIASES 35 49 #define MAXADDRS 35 50 51 #ifdef YP 52 static char *host_aliases[MAXALIASES]; 53 static char hostaddr[MAXADDRS]; 54 static char *host_addrs[2]; 55 56 static struct hostent * 57 _gethostbynis(name, map, af) 58 const char *name; 59 char *map; 60 int af; 61 { 62 char *cp, **q; 63 char *result; 64 int resultlen,size; 65 static struct hostent h; 66 static char *domain = (char *)NULL; 67 static char ypbuf[YPMAXRECORD + 2]; 68 in_addr_t addr; 69 70 switch(af) { 71 case AF_INET: 72 size = NS_INADDRSZ; 73 break; 74 default: 75 case AF_INET6: 76 size = NS_IN6ADDRSZ; 77 errno = EAFNOSUPPORT; 78 h_errno = NETDB_INTERNAL; 79 return NULL; 80 } 81 82 if (domain == (char *)NULL) 83 if (yp_get_default_domain (&domain)) { 84 h_errno = NETDB_INTERNAL; 85 return ((struct hostent *)NULL); 86 } 87 88 if (yp_match(domain, map, name, strlen(name), &result, &resultlen)) { 89 h_errno = HOST_NOT_FOUND; 90 return ((struct hostent *)NULL); 91 } 92 93 /* avoid potential memory leak */ 94 bcopy((char *)result, (char *)&ypbuf, resultlen); 95 ypbuf[resultlen] = '\0'; 96 free(result); 97 result = (char *)&ypbuf; 98 99 if ((cp = index(result, '\n'))) 100 *cp = '\0'; 101 102 cp = strpbrk(result, " \t"); 103 *cp++ = '\0'; 104 h.h_addr_list = host_addrs; 105 h.h_addr = hostaddr; 106 addr = inet_addr(result); 107 bcopy((char *)&addr, h.h_addr, size); 108 h.h_length = size; 109 h.h_addrtype = AF_INET; 110 while (*cp == ' ' || *cp == '\t') 111 cp++; 112 h.h_name = cp; 113 q = h.h_aliases = host_aliases; 114 cp = strpbrk(cp, " \t"); 115 if (cp != NULL) 116 *cp++ = '\0'; 117 while (cp && *cp) { 118 if (*cp == ' ' || *cp == '\t') { 119 cp++; 120 continue; 121 } 122 if (q < &host_aliases[MAXALIASES - 1]) 123 *q++ = cp; 124 cp = strpbrk(cp, " \t"); 125 if (cp != NULL) 126 *cp++ = '\0'; 127 } 128 *q = NULL; 129 return (&h); 130 } 131 #endif /* YP */ 132 133 /* XXX _gethostbynisname/_gethostbynisaddr only used by getaddrinfo */ 134 struct hostent * 135 _gethostbynisname(const char *name, int af) 136 { 137 #ifdef YP 138 return _gethostbynis(name, "hosts.byname", af); 139 #else 140 return NULL; 141 #endif 142 } 143 144 struct hostent * 145 _gethostbynisaddr(const char *addr, int len, int af) 146 { 147 #ifdef YP 148 return _gethostbynis(inet_ntoa(*(struct in_addr *)addr), 149 "hosts.byaddr", af); 150 #else 151 return NULL; 152 #endif 153 } 154 155 156 int 157 _nis_gethostbyname(void *rval, void *cb_data, va_list ap) 158 { 159 #ifdef YP 160 const char *name; 161 int af; 162 163 name = va_arg(ap, const char *); 164 af = va_arg(ap, int); 165 166 *(struct hostent **)rval = _gethostbynis(name, "hosts.byname", af); 167 return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND; 168 #else 169 return NS_UNAVAIL; 170 #endif 171 } 172 173 int 174 _nis_gethostbyaddr(void *rval, void *cb_data, va_list ap) 175 { 176 #ifdef YP 177 const char *addr; 178 int len; 179 int af; 180 181 addr = va_arg(ap, const char *); 182 len = va_arg(ap, int); 183 af = va_arg(ap, int); 184 185 *(struct hostent **)rval =_gethostbynis(inet_ntoa(*(struct in_addr *)addr),"hosts.byaddr", af); 186 return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND; 187 #else 188 return NS_UNAVAIL; 189 #endif 190 } 191