blob: 2a4248f3ae4f28f88b9569b7a55834967e30be96 [file] [log] [blame]
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001/* $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 Innocenti318ed2d2018-08-30 04:05:20 +090032#include <arpa/inet.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090033#include <arpa/nameser.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090034#include <assert.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090035#include <errno.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090036#include <netdb.h>
37#include <netinet/in.h>
38#include <nsswitch.h>
39#include <resolv.h>
Bernie Innocentic939de02018-09-12 17:59:17 +090040#include <stdio.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090041#include <stdlib.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090042#include <string.h>
43#include <sys/param.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090044
45#include "hostent.h"
46#include "resolv_private.h"
47
Bernie Innocentiafaacf72018-08-30 07:34:37 +090048// NetBSD uses _DIAGASSERT to null-check arguments and the like,
49// but it's clear from the number of mistakes in their assertions
50// that they don't actually test or ship with this.
51#define _DIAGASSERT(e) /* nothing */
52
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090053#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090054#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090055
Bernie Innocentic939de02018-09-12 17:59:17 +090056static void sethostent_r(FILE** hf) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090057 if (!*hf)
58 *hf = fopen(_PATH_HOSTS, "re");
59 else
60 rewind(*hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090061}
62
Bernie Innocentic939de02018-09-12 17:59:17 +090063static void endhostent_r(FILE** hf) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090064 if (*hf) {
65 (void) fclose(*hf);
66 *hf = NULL;
67 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090068}
69
Bernie Innocentic939de02018-09-12 17:59:17 +090070hostent* _hf_gethtbyname2(const char* name, int af, getnamaddr* info) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090071 struct hostent *hp, hent;
72 char *buf, *ptr;
73 size_t len, anum, num, i;
74 FILE* hf;
75 char* aliases[MAXALIASES];
76 char* addr_ptrs[MAXADDRS];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090077
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090078 _DIAGASSERT(name != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090079
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090080 hf = NULL;
81 sethostent_r(&hf);
82 if (hf == NULL) {
83 errno = EINVAL;
84 *info->he = NETDB_INTERNAL;
85 return NULL;
86 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090087
Bernie Innocenti9c575932018-09-07 21:10:25 +090088 if ((ptr = buf = (char*) malloc(len = info->buflen)) == NULL) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090089 *info->he = NETDB_INTERNAL;
90 return NULL;
91 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090092
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090093 anum = 0; /* XXX: gcc */
94 hent.h_name = NULL; /* XXX: gcc */
95 hent.h_addrtype = 0; /* XXX: gcc */
96 hent.h_length = 0; /* XXX: gcc */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090097
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090098 for (num = 0; num < MAXADDRS;) {
99 info->hp->h_addrtype = af;
100 info->hp->h_length = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900101
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900102 hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he);
103 if (hp == NULL) {
104 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
105 goto nospc; // glibc compatibility.
106 }
107 break;
108 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900109
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900110 if (strcasecmp(hp->h_name, name) != 0) {
111 char** cp;
112 for (cp = hp->h_aliases; *cp != NULL; cp++)
113 if (strcasecmp(*cp, name) == 0) break;
114 if (*cp == NULL) continue;
115 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900116
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900117 if (num == 0) {
Bernie Innocentic939de02018-09-12 17:59:17 +0900118 hent.h_addrtype = hp->h_addrtype;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900119 hent.h_length = hp->h_length;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900120
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900121 HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
122 for (anum = 0; hp->h_aliases[anum]; anum++) {
123 if (anum >= MAXALIASES) goto nospc;
124 HENT_SCOPY(aliases[anum], hp->h_aliases[anum], ptr, len);
125 }
Bernie Innocenti9c575932018-09-07 21:10:25 +0900126 ptr = (char*) ALIGN(ptr);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900127 if ((size_t)(ptr - buf) >= info->buflen) goto nospc;
128 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900129
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900130 if (num >= MAXADDRS) goto nospc;
131 HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr, len);
132 num++;
133 }
134 endhostent_r(&hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900135
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900136 if (num == 0) {
137 *info->he = HOST_NOT_FOUND;
138 free(buf);
139 return NULL;
140 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900141
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900142 hp = info->hp;
143 ptr = info->buf;
144 len = info->buflen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900145
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900146 hp->h_addrtype = hent.h_addrtype;
147 hp->h_length = hent.h_length;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900148
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900149 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
150 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900151
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900152 for (i = 0; i < num; i++) HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr, len);
153 hp->h_addr_list[num] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900154
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900155 HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900156
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900157 for (i = 0; i < anum; i++) HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
158 hp->h_aliases[anum] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900159
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900160 free(buf);
161 return hp;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900162nospc:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900163 *info->he = NETDB_INTERNAL;
164 free(buf);
165 errno = ENOSPC;
166 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900167}
168
Bernie Innocenti9c575932018-09-07 21:10:25 +0900169int _hf_gethtbyaddr(void* rv, void* /*cb_data*/, va_list ap) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900170 struct hostent* hp;
171 const unsigned char* addr;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900172 struct getnamaddr* info = (struct getnamaddr*) rv;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900173 FILE* hf;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900174
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900175 _DIAGASSERT(rv != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900176
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900177 addr = va_arg(ap, unsigned char*);
178 info->hp->h_length = va_arg(ap, int);
179 info->hp->h_addrtype = va_arg(ap, int);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900180
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900181 hf = NULL;
182 sethostent_r(&hf);
183 if (hf == NULL) {
184 *info->he = NETDB_INTERNAL;
185 return NS_UNAVAIL;
186 }
187 while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he)) != NULL)
188 if (!memcmp(hp->h_addr_list[0], addr, (size_t) hp->h_length)) break;
189 endhostent_r(&hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900190
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900191 if (hp == NULL) {
192 if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility.
193 *info->he = HOST_NOT_FOUND;
194 return NS_NOTFOUND;
195 }
196 return NS_SUCCESS;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900197}