1 /*- 2 * Copyright (c) 1985, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * - 33 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 34 * 35 * Permission to use, copy, modify, and distribute this software for any 36 * purpose with or without fee is hereby granted, provided that the above 37 * copyright notice and this permission notice appear in all copies, and that 38 * the name of Digital Equipment Corporation not be used in advertising or 39 * publicity pertaining to distribution of the document or software without 40 * specific, written prior permission. 41 * 42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 49 * SOFTWARE. 50 * - 51 * --Copyright-- 52 */ 53 54 #if defined(LIBC_SCCS) && !defined(lint) 55 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93"; 56 #endif /* LIBC_SCCS and not lint */ 57 #include <sys/cdefs.h> 58 __FBSDID("$FreeBSD$"); 59 60 #include <sys/param.h> 61 #include <sys/socket.h> 62 #include <netinet/in.h> 63 #include <arpa/inet.h> 64 #include <netdb.h> 65 #include <stdio.h> 66 #include <ctype.h> 67 #include <string.h> 68 #include <stdarg.h> 69 #include <nsswitch.h> 70 #include <arpa/nameser.h> /* XXX */ 71 #include <resolv.h> /* XXX */ 72 #include "netdb_private.h" 73 74 void 75 _sethosthtent(int f, struct hostent_data *hed) 76 { 77 if (!hed->hostf) 78 hed->hostf = fopen(_PATH_HOSTS, "r"); 79 else 80 rewind(hed->hostf); 81 hed->stayopen = f; 82 } 83 84 void 85 _endhosthtent(struct hostent_data *hed) 86 { 87 if (hed->hostf && !hed->stayopen) { 88 (void) fclose(hed->hostf); 89 hed->hostf = NULL; 90 } 91 } 92 93 static int 94 gethostent_p(struct hostent *he, struct hostent_data *hed, int mapped, 95 res_state statp) 96 { 97 char *p, *bp, *ep; 98 char *cp, **q; 99 int af, len; 100 char hostbuf[BUFSIZ + 1]; 101 102 if (!hed->hostf && !(hed->hostf = fopen(_PATH_HOSTS, "r"))) { 103 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 104 return (-1); 105 } 106 again: 107 if (!(p = fgets(hostbuf, sizeof hostbuf, hed->hostf))) { 108 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); 109 return (-1); 110 } 111 if (*p == '#') 112 goto again; 113 cp = strpbrk(p, "#\n"); 114 if (cp != NULL) 115 *cp = '\0'; 116 if (!(cp = strpbrk(p, " \t"))) 117 goto again; 118 *cp++ = '\0'; 119 if (inet_pton(AF_INET6, p, hed->host_addr) > 0) { 120 af = AF_INET6; 121 len = IN6ADDRSZ; 122 } else if (inet_pton(AF_INET, p, hed->host_addr) > 0) { 123 if (mapped) { 124 _map_v4v6_address((char *)hed->host_addr, 125 (char *)hed->host_addr); 126 af = AF_INET6; 127 len = IN6ADDRSZ; 128 } else { 129 af = AF_INET; 130 len = INADDRSZ; 131 } 132 } else { 133 goto again; 134 } 135 hed->h_addr_ptrs[0] = (char *)hed->host_addr; 136 hed->h_addr_ptrs[1] = NULL; 137 he->h_addr_list = hed->h_addr_ptrs; 138 he->h_length = len; 139 he->h_addrtype = af; 140 while (*cp == ' ' || *cp == '\t') 141 cp++; 142 bp = hed->hostbuf; 143 ep = hed->hostbuf + sizeof hed->hostbuf; 144 he->h_name = bp; 145 q = he->h_aliases = hed->host_aliases; 146 if ((p = strpbrk(cp, " \t")) != NULL) 147 *p++ = '\0'; 148 len = strlen(cp) + 1; 149 if (ep - bp < len) { 150 RES_SET_H_ERRNO(statp, NO_RECOVERY); 151 return (-1); 152 } 153 strlcpy(bp, cp, ep - bp); 154 bp += len; 155 cp = p; 156 while (cp && *cp) { 157 if (*cp == ' ' || *cp == '\t') { 158 cp++; 159 continue; 160 } 161 if (q >= &hed->host_aliases[_MAXALIASES - 1]) 162 break; 163 if ((p = strpbrk(cp, " \t")) != NULL) 164 *p++ = '\0'; 165 len = strlen(cp) + 1; 166 if (ep - bp < len) 167 break; 168 strlcpy(bp, cp, ep - bp); 169 *q++ = bp; 170 bp += len; 171 cp = p; 172 } 173 *q = NULL; 174 RES_SET_H_ERRNO(statp, NETDB_SUCCESS); 175 return (0); 176 } 177 178 int 179 gethostent_r(struct hostent *hptr, char *buffer, size_t buflen, 180 struct hostent **result, int *h_errnop) 181 { 182 struct hostent_data *hed; 183 struct hostent he; 184 res_state statp; 185 186 statp = __res_state(); 187 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) { 188 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 189 *h_errnop = statp->res_h_errno; 190 return (-1); 191 } 192 if ((hed = __hostent_data_init()) == NULL) { 193 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 194 *h_errnop = statp->res_h_errno; 195 return (-1); 196 } 197 if (gethostent_p(&he, hed, statp->options & RES_USE_INET6, statp) != 0) 198 return (-1); 199 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) 200 return (-1); 201 *result = hptr; 202 return (0); 203 } 204 205 struct hostent * 206 gethostent(void) 207 { 208 struct hostdata *hd; 209 struct hostent *rval; 210 int ret_h_errno; 211 212 if ((hd = __hostdata_init()) == NULL) 213 return (NULL); 214 if (gethostent_r(&hd->host, hd->data, sizeof(hd->data), &rval, 215 &ret_h_errno) != 0) 216 return (NULL); 217 return (rval); 218 } 219 220 int 221 _ht_gethostbyname(void *rval, void *cb_data, va_list ap) 222 { 223 const char *name; 224 int af; 225 char *buffer; 226 size_t buflen; 227 int *errnop, *h_errnop; 228 struct hostent *hptr, he; 229 struct hostent_data *hed; 230 char **cp; 231 res_state statp; 232 int error; 233 234 name = va_arg(ap, const char *); 235 af = va_arg(ap, int); 236 hptr = va_arg(ap, struct hostent *); 237 buffer = va_arg(ap, char *); 238 buflen = va_arg(ap, size_t); 239 errnop = va_arg(ap, int *); 240 h_errnop = va_arg(ap, int *); 241 242 *((struct hostent **)rval) = NULL; 243 244 statp = __res_state(); 245 if ((hed = __hostent_data_init()) == NULL) { 246 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 247 *h_errnop = statp->res_h_errno; 248 return (NS_NOTFOUND); 249 } 250 251 _sethosthtent(0, hed); 252 while ((error = gethostent_p(&he, hed, 0, statp)) == 0) { 253 if (he.h_addrtype != af) 254 continue; 255 if (he.h_addrtype == AF_INET && 256 statp->options & RES_USE_INET6) { 257 _map_v4v6_address(he.h_addr, he.h_addr); 258 he.h_length = IN6ADDRSZ; 259 he.h_addrtype = AF_INET6; 260 } 261 if (strcasecmp(he.h_name, name) == 0) 262 break; 263 for (cp = he.h_aliases; *cp != 0; cp++) 264 if (strcasecmp(*cp, name) == 0) 265 goto found; 266 } 267 found: 268 _endhosthtent(hed); 269 270 if (error != 0) { 271 *h_errnop = statp->res_h_errno; 272 return (NS_NOTFOUND); 273 } 274 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) { 275 *h_errnop = statp->res_h_errno; 276 return (NS_NOTFOUND); 277 } 278 *((struct hostent **)rval) = hptr; 279 return (NS_SUCCESS); 280 } 281 282 int 283 _ht_gethostbyaddr(void *rval, void *cb_data, va_list ap) 284 { 285 const void *addr; 286 socklen_t len; 287 int af; 288 char *buffer; 289 size_t buflen; 290 int *errnop, *h_errnop; 291 struct hostent *hptr, he; 292 struct hostent_data *hed; 293 res_state statp; 294 int error; 295 296 addr = va_arg(ap, const void *); 297 len = va_arg(ap, socklen_t); 298 af = va_arg(ap, int); 299 hptr = va_arg(ap, struct hostent *); 300 buffer = va_arg(ap, char *); 301 buflen = va_arg(ap, size_t); 302 errnop = va_arg(ap, int *); 303 h_errnop = va_arg(ap, int *); 304 305 *((struct hostent **)rval) = NULL; 306 307 statp = __res_state(); 308 if ((hed = __hostent_data_init()) == NULL) { 309 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 310 *h_errnop = statp->res_h_errno; 311 return (NS_NOTFOUND); 312 } 313 314 _sethosthtent(0, hed); 315 while ((error = gethostent_p(&he, hed, 0, statp)) == 0) 316 if (he.h_addrtype == af && !bcmp(he.h_addr, addr, len)) { 317 if (he.h_addrtype == AF_INET && 318 statp->options & RES_USE_INET6) { 319 _map_v4v6_address(he.h_addr, he.h_addr); 320 he.h_length = IN6ADDRSZ; 321 he.h_addrtype = AF_INET6; 322 } 323 break; 324 } 325 _endhosthtent(hed); 326 327 if (error != 0) 328 return (NS_NOTFOUND); 329 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) { 330 *h_errnop = statp->res_h_errno; 331 return (NS_NOTFOUND); 332 } 333 *((struct hostent **)rval) = hptr; 334 return (NS_SUCCESS); 335 } 336