Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $NetBSD: sethostent.c,v 1.20 2014/03/17 13:24:23 christos Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1985, 1993 |
| 5 | * The Regents of the University of California. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. Neither the name of the University nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software |
| 17 | * without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 | * SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include <sys/cdefs.h> |
| 33 | #if defined(LIBC_SCCS) && !defined(lint) |
| 34 | #if 0 |
| 35 | static char sccsid[] = "@(#)sethostent.c 8.1 (Berkeley) 6/4/93"; |
| 36 | static char rcsid[] = "Id: sethostent.c,v 8.5 1996/09/28 06:51:07 vixie Exp "; |
| 37 | #else |
| 38 | __RCSID("$NetBSD: sethostent.c,v 1.20 2014/03/17 13:24:23 christos Exp $"); |
| 39 | #endif |
| 40 | #endif /* LIBC_SCCS and not lint */ |
| 41 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 42 | #include <arpa/inet.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 43 | #include <arpa/nameser.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 44 | #include <assert.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 45 | #include <errno.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 46 | #include <netdb.h> |
| 47 | #include <netinet/in.h> |
| 48 | #include <nsswitch.h> |
| 49 | #include <resolv.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 50 | #include <stdlib.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 51 | #include <string.h> |
| 52 | #include <sys/param.h> |
| 53 | #include "namespace.h" |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 54 | |
| 55 | #include "hostent.h" |
| 56 | #include "resolv_private.h" |
| 57 | |
| 58 | #define ALIGNBYTES (sizeof(uintptr_t) - 1) |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 59 | #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 60 | |
| 61 | #ifndef _REENTRANT |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 62 | void res_close(void); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 63 | #endif |
| 64 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 65 | static struct hostent* _hf_gethtbyname2(const char*, int, struct getnamaddr*); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 66 | |
| 67 | void |
| 68 | /*ARGSUSED*/ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 69 | sethostent(int stayopen) { |
| 70 | res_static rs = __res_get_static(); |
| 71 | if (rs) sethostent_r(&rs->hostf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 72 | } |
| 73 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 74 | void endhostent(void) { |
| 75 | res_static rs = __res_get_static(); |
| 76 | if (rs) endhostent_r(&rs->hostf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 77 | } |
| 78 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 79 | void sethostent_r(FILE** hf) { |
| 80 | if (!*hf) |
| 81 | *hf = fopen(_PATH_HOSTS, "re"); |
| 82 | else |
| 83 | rewind(*hf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 84 | } |
| 85 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 86 | void endhostent_r(FILE** hf) { |
| 87 | if (*hf) { |
| 88 | (void) fclose(*hf); |
| 89 | *hf = NULL; |
| 90 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /*ARGSUSED*/ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 94 | int _hf_gethtbyname(void* rv, void* cb_data, va_list ap) { |
| 95 | struct hostent* hp; |
| 96 | const char* name; |
| 97 | int af; |
| 98 | struct getnamaddr* info = rv; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 99 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 100 | _DIAGASSERT(rv != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 101 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 102 | name = va_arg(ap, char*); |
| 103 | /* NOSTRICT skip string len */ (void) va_arg(ap, int); |
| 104 | af = va_arg(ap, int); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 105 | |
| 106 | #if 0 |
| 107 | { |
| 108 | res_state res = __res_get_state(); |
| 109 | if (res == NULL) |
| 110 | return NS_NOTFOUND; |
| 111 | if (res->options & RES_USE_INET6) |
| 112 | hp = _hf_gethtbyname2(name, AF_INET6, info); |
| 113 | else |
| 114 | hp = NULL; |
| 115 | if (hp == NULL) |
| 116 | hp = _hf_gethtbyname2(name, AF_INET, info); |
| 117 | __res_put_state(res); |
| 118 | } |
| 119 | #else |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 120 | hp = _hf_gethtbyname2(name, af, info); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 121 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 122 | if (hp == NULL) { |
| 123 | if (*info->he == NETDB_INTERNAL && errno == ENOSPC) { |
| 124 | return NS_UNAVAIL; // glibc compatibility. |
| 125 | } |
| 126 | *info->he = HOST_NOT_FOUND; |
| 127 | return NS_NOTFOUND; |
| 128 | } |
| 129 | return NS_SUCCESS; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 130 | } |
| 131 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 132 | struct hostent* _hf_gethtbyname2(const char* name, int af, struct getnamaddr* info) { |
| 133 | struct hostent *hp, hent; |
| 134 | char *buf, *ptr; |
| 135 | size_t len, anum, num, i; |
| 136 | FILE* hf; |
| 137 | char* aliases[MAXALIASES]; |
| 138 | char* addr_ptrs[MAXADDRS]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 139 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 140 | _DIAGASSERT(name != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 141 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 142 | hf = NULL; |
| 143 | sethostent_r(&hf); |
| 144 | if (hf == NULL) { |
| 145 | errno = EINVAL; |
| 146 | *info->he = NETDB_INTERNAL; |
| 147 | return NULL; |
| 148 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 149 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 150 | if ((ptr = buf = malloc(len = info->buflen)) == NULL) { |
| 151 | *info->he = NETDB_INTERNAL; |
| 152 | return NULL; |
| 153 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 154 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 155 | anum = 0; /* XXX: gcc */ |
| 156 | hent.h_name = NULL; /* XXX: gcc */ |
| 157 | hent.h_addrtype = 0; /* XXX: gcc */ |
| 158 | hent.h_length = 0; /* XXX: gcc */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 159 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 160 | for (num = 0; num < MAXADDRS;) { |
| 161 | info->hp->h_addrtype = af; |
| 162 | info->hp->h_length = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 163 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 164 | hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he); |
| 165 | if (hp == NULL) { |
| 166 | if (*info->he == NETDB_INTERNAL && errno == ENOSPC) { |
| 167 | goto nospc; // glibc compatibility. |
| 168 | } |
| 169 | break; |
| 170 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 171 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 172 | if (strcasecmp(hp->h_name, name) != 0) { |
| 173 | char** cp; |
| 174 | for (cp = hp->h_aliases; *cp != NULL; cp++) |
| 175 | if (strcasecmp(*cp, name) == 0) break; |
| 176 | if (*cp == NULL) continue; |
| 177 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 178 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 179 | if (num == 0) { |
| 180 | hent.h_addrtype = af = hp->h_addrtype; |
| 181 | hent.h_length = hp->h_length; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 182 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 183 | HENT_SCOPY(hent.h_name, hp->h_name, ptr, len); |
| 184 | for (anum = 0; hp->h_aliases[anum]; anum++) { |
| 185 | if (anum >= MAXALIASES) goto nospc; |
| 186 | HENT_SCOPY(aliases[anum], hp->h_aliases[anum], ptr, len); |
| 187 | } |
| 188 | ptr = (void*) ALIGN(ptr); |
| 189 | if ((size_t)(ptr - buf) >= info->buflen) goto nospc; |
| 190 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 191 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 192 | if (num >= MAXADDRS) goto nospc; |
| 193 | HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr, len); |
| 194 | num++; |
| 195 | } |
| 196 | endhostent_r(&hf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 197 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 198 | if (num == 0) { |
| 199 | *info->he = HOST_NOT_FOUND; |
| 200 | free(buf); |
| 201 | return NULL; |
| 202 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 203 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 204 | hp = info->hp; |
| 205 | ptr = info->buf; |
| 206 | len = info->buflen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 207 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 208 | hp->h_addrtype = hent.h_addrtype; |
| 209 | hp->h_length = hent.h_length; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 210 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 211 | HENT_ARRAY(hp->h_aliases, anum, ptr, len); |
| 212 | HENT_ARRAY(hp->h_addr_list, num, ptr, len); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 213 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 214 | for (i = 0; i < num; i++) HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr, len); |
| 215 | hp->h_addr_list[num] = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 216 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 217 | HENT_SCOPY(hp->h_name, hent.h_name, ptr, len); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 218 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 219 | for (i = 0; i < anum; i++) HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len); |
| 220 | hp->h_aliases[anum] = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 221 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 222 | free(buf); |
| 223 | return hp; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 224 | nospc: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 225 | *info->he = NETDB_INTERNAL; |
| 226 | free(buf); |
| 227 | errno = ENOSPC; |
| 228 | return NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /*ARGSUSED*/ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 232 | int _hf_gethtbyaddr(void* rv, void* cb_data, va_list ap) { |
| 233 | struct hostent* hp; |
| 234 | const unsigned char* addr; |
| 235 | struct getnamaddr* info = rv; |
| 236 | FILE* hf; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 237 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 238 | _DIAGASSERT(rv != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 239 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 240 | addr = va_arg(ap, unsigned char*); |
| 241 | info->hp->h_length = va_arg(ap, int); |
| 242 | info->hp->h_addrtype = va_arg(ap, int); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 243 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 244 | hf = NULL; |
| 245 | sethostent_r(&hf); |
| 246 | if (hf == NULL) { |
| 247 | *info->he = NETDB_INTERNAL; |
| 248 | return NS_UNAVAIL; |
| 249 | } |
| 250 | while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he)) != NULL) |
| 251 | if (!memcmp(hp->h_addr_list[0], addr, (size_t) hp->h_length)) break; |
| 252 | endhostent_r(&hf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 253 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 254 | if (hp == NULL) { |
| 255 | if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility. |
| 256 | *info->he = HOST_NOT_FOUND; |
| 257 | return NS_NOTFOUND; |
| 258 | } |
| 259 | return NS_SUCCESS; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 260 | } |