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 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 32 | #include <arpa/inet.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 33 | #include <arpa/nameser.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 34 | #include <assert.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 35 | #include <errno.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 36 | #include <netdb.h> |
| 37 | #include <netinet/in.h> |
| 38 | #include <nsswitch.h> |
| 39 | #include <resolv.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 40 | #include <stdlib.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 41 | #include <string.h> |
| 42 | #include <sys/param.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 43 | |
| 44 | #include "hostent.h" |
| 45 | #include "resolv_private.h" |
| 46 | |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame^] | 47 | // NetBSD uses _DIAGASSERT to null-check arguments and the like, |
| 48 | // but it's clear from the number of mistakes in their assertions |
| 49 | // that they don't actually test or ship with this. |
| 50 | #define _DIAGASSERT(e) /* nothing */ |
| 51 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 52 | #define ALIGNBYTES (sizeof(uintptr_t) - 1) |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 53 | #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 54 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 55 | static struct hostent* _hf_gethtbyname2(const char*, int, struct getnamaddr*); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 56 | |
| 57 | void |
| 58 | /*ARGSUSED*/ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 59 | sethostent(int stayopen) { |
| 60 | res_static rs = __res_get_static(); |
| 61 | if (rs) sethostent_r(&rs->hostf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 62 | } |
| 63 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 64 | void endhostent(void) { |
| 65 | res_static rs = __res_get_static(); |
| 66 | if (rs) endhostent_r(&rs->hostf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 67 | } |
| 68 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 69 | void sethostent_r(FILE** hf) { |
| 70 | if (!*hf) |
| 71 | *hf = fopen(_PATH_HOSTS, "re"); |
| 72 | else |
| 73 | rewind(*hf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 74 | } |
| 75 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 76 | void endhostent_r(FILE** hf) { |
| 77 | if (*hf) { |
| 78 | (void) fclose(*hf); |
| 79 | *hf = NULL; |
| 80 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /*ARGSUSED*/ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 84 | int _hf_gethtbyname(void* rv, void* cb_data, va_list ap) { |
| 85 | struct hostent* hp; |
| 86 | const char* name; |
| 87 | int af; |
| 88 | struct getnamaddr* info = rv; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 89 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 90 | _DIAGASSERT(rv != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 91 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 92 | name = va_arg(ap, char*); |
| 93 | /* NOSTRICT skip string len */ (void) va_arg(ap, int); |
| 94 | af = va_arg(ap, int); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 95 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 96 | hp = _hf_gethtbyname2(name, af, info); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 97 | if (hp == NULL) { |
| 98 | if (*info->he == NETDB_INTERNAL && errno == ENOSPC) { |
| 99 | return NS_UNAVAIL; // glibc compatibility. |
| 100 | } |
| 101 | *info->he = HOST_NOT_FOUND; |
| 102 | return NS_NOTFOUND; |
| 103 | } |
| 104 | return NS_SUCCESS; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 105 | } |
| 106 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 107 | struct hostent* _hf_gethtbyname2(const char* name, int af, struct getnamaddr* info) { |
| 108 | struct hostent *hp, hent; |
| 109 | char *buf, *ptr; |
| 110 | size_t len, anum, num, i; |
| 111 | FILE* hf; |
| 112 | char* aliases[MAXALIASES]; |
| 113 | char* addr_ptrs[MAXADDRS]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 114 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 115 | _DIAGASSERT(name != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 116 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 117 | hf = NULL; |
| 118 | sethostent_r(&hf); |
| 119 | if (hf == NULL) { |
| 120 | errno = EINVAL; |
| 121 | *info->he = NETDB_INTERNAL; |
| 122 | return NULL; |
| 123 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 124 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 125 | if ((ptr = buf = malloc(len = info->buflen)) == NULL) { |
| 126 | *info->he = NETDB_INTERNAL; |
| 127 | return NULL; |
| 128 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 129 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 130 | anum = 0; /* XXX: gcc */ |
| 131 | hent.h_name = NULL; /* XXX: gcc */ |
| 132 | hent.h_addrtype = 0; /* XXX: gcc */ |
| 133 | hent.h_length = 0; /* XXX: gcc */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 134 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 135 | for (num = 0; num < MAXADDRS;) { |
| 136 | info->hp->h_addrtype = af; |
| 137 | info->hp->h_length = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 138 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 139 | hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he); |
| 140 | if (hp == NULL) { |
| 141 | if (*info->he == NETDB_INTERNAL && errno == ENOSPC) { |
| 142 | goto nospc; // glibc compatibility. |
| 143 | } |
| 144 | break; |
| 145 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 146 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 147 | if (strcasecmp(hp->h_name, name) != 0) { |
| 148 | char** cp; |
| 149 | for (cp = hp->h_aliases; *cp != NULL; cp++) |
| 150 | if (strcasecmp(*cp, name) == 0) break; |
| 151 | if (*cp == NULL) continue; |
| 152 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 153 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 154 | if (num == 0) { |
| 155 | hent.h_addrtype = af = hp->h_addrtype; |
| 156 | hent.h_length = hp->h_length; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 157 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 158 | HENT_SCOPY(hent.h_name, hp->h_name, ptr, len); |
| 159 | for (anum = 0; hp->h_aliases[anum]; anum++) { |
| 160 | if (anum >= MAXALIASES) goto nospc; |
| 161 | HENT_SCOPY(aliases[anum], hp->h_aliases[anum], ptr, len); |
| 162 | } |
| 163 | ptr = (void*) ALIGN(ptr); |
| 164 | if ((size_t)(ptr - buf) >= info->buflen) goto nospc; |
| 165 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 166 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 167 | if (num >= MAXADDRS) goto nospc; |
| 168 | HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr, len); |
| 169 | num++; |
| 170 | } |
| 171 | endhostent_r(&hf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 172 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 173 | if (num == 0) { |
| 174 | *info->he = HOST_NOT_FOUND; |
| 175 | free(buf); |
| 176 | return NULL; |
| 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 | hp = info->hp; |
| 180 | ptr = info->buf; |
| 181 | len = info->buflen; |
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 | hp->h_addrtype = hent.h_addrtype; |
| 184 | hp->h_length = hent.h_length; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 185 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 186 | HENT_ARRAY(hp->h_aliases, anum, ptr, len); |
| 187 | HENT_ARRAY(hp->h_addr_list, num, ptr, len); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 188 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 189 | for (i = 0; i < num; i++) HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr, len); |
| 190 | hp->h_addr_list[num] = NULL; |
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 | HENT_SCOPY(hp->h_name, hent.h_name, ptr, len); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 193 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 194 | for (i = 0; i < anum; i++) HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len); |
| 195 | hp->h_aliases[anum] = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 196 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 197 | free(buf); |
| 198 | return hp; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 199 | nospc: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 200 | *info->he = NETDB_INTERNAL; |
| 201 | free(buf); |
| 202 | errno = ENOSPC; |
| 203 | return NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /*ARGSUSED*/ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 207 | int _hf_gethtbyaddr(void* rv, void* cb_data, va_list ap) { |
| 208 | struct hostent* hp; |
| 209 | const unsigned char* addr; |
| 210 | struct getnamaddr* info = rv; |
| 211 | FILE* hf; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 212 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 213 | _DIAGASSERT(rv != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 214 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 215 | addr = va_arg(ap, unsigned char*); |
| 216 | info->hp->h_length = va_arg(ap, int); |
| 217 | info->hp->h_addrtype = va_arg(ap, int); |
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 | hf = NULL; |
| 220 | sethostent_r(&hf); |
| 221 | if (hf == NULL) { |
| 222 | *info->he = NETDB_INTERNAL; |
| 223 | return NS_UNAVAIL; |
| 224 | } |
| 225 | while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he)) != NULL) |
| 226 | if (!memcmp(hp->h_addr_list[0], addr, (size_t) hp->h_length)) break; |
| 227 | endhostent_r(&hf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 228 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 229 | if (hp == NULL) { |
| 230 | if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility. |
| 231 | *info->he = HOST_NOT_FOUND; |
| 232 | return NS_NOTFOUND; |
| 233 | } |
| 234 | return NS_SUCCESS; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 235 | } |