xref: /freebsd/lib/libc/net/gethostbyht.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1985, 1988, 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  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
32  *
33  * Permission to use, copy, modify, and distribute this software for any
34  * purpose with or without fee is hereby granted, provided that the above
35  * copyright notice and this permission notice appear in all copies, and that
36  * the name of Digital Equipment Corporation not be used in advertising or
37  * publicity pertaining to distribution of the document or software without
38  * specific, written prior permission.
39  *
40  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
43  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47  * SOFTWARE.
48  * -
49  * --Copyright--
50  */
51 
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <netdb.h>
57 #include <stdio.h>
58 #include <ctype.h>
59 #include <errno.h>
60 #include <string.h>
61 #include <stdarg.h>
62 #include <nsswitch.h>
63 #include <arpa/nameser.h>	/* XXX */
64 #include <resolv.h>		/* XXX */
65 #include "netdb_private.h"
66 
67 void
68 _sethosthtent(int f, struct hostent_data *hed)
69 {
70 	if (!hed->hostf)
71 		hed->hostf = fopen(_PATH_HOSTS, "re");
72 	else
73 		rewind(hed->hostf);
74 	hed->stayopen = f;
75 }
76 
77 void
78 _endhosthtent(struct hostent_data *hed)
79 {
80 	if (hed->hostf && !hed->stayopen) {
81 		(void) fclose(hed->hostf);
82 		hed->hostf = NULL;
83 	}
84 }
85 
86 static int
87 gethostent_p(struct hostent *he, struct hostent_data *hed, int mapped,
88     res_state statp)
89 {
90 	char *p, *bp, *ep;
91 	char *cp, **q;
92 	int af, len;
93 	char hostbuf[BUFSIZ + 1];
94 
95 	if (!hed->hostf && !(hed->hostf = fopen(_PATH_HOSTS, "re"))) {
96 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
97 		return (-1);
98 	}
99  again:
100 	if (!(p = fgets(hostbuf, sizeof hostbuf, hed->hostf))) {
101 		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
102 		return (-1);
103 	}
104 	if (*p == '#')
105 		goto again;
106 	cp = strpbrk(p, "#\n");
107 	if (cp != NULL)
108 		*cp = '\0';
109 	if (!(cp = strpbrk(p, " \t")))
110 		goto again;
111 	*cp++ = '\0';
112 	if (inet_pton(AF_INET6, p, hed->host_addr) > 0) {
113 		af = AF_INET6;
114 		len = IN6ADDRSZ;
115 	} else if (inet_pton(AF_INET, p, hed->host_addr) > 0) {
116 		if (mapped) {
117 			_map_v4v6_address((char *)hed->host_addr,
118 			    (char *)hed->host_addr);
119 			af = AF_INET6;
120 			len = IN6ADDRSZ;
121 		} else {
122 			af = AF_INET;
123 			len = INADDRSZ;
124 		}
125 	} else {
126 		goto again;
127 	}
128 	hed->h_addr_ptrs[0] = (char *)hed->host_addr;
129 	hed->h_addr_ptrs[1] = NULL;
130 	he->h_addr_list = hed->h_addr_ptrs;
131 	he->h_length = len;
132 	he->h_addrtype = af;
133 	while (*cp == ' ' || *cp == '\t')
134 		cp++;
135 	bp = hed->hostbuf;
136 	ep = hed->hostbuf + sizeof hed->hostbuf;
137 	he->h_name = bp;
138 	q = he->h_aliases = hed->host_aliases;
139 	if ((p = strpbrk(cp, " \t")) != NULL)
140 		*p++ = '\0';
141 	len = strlen(cp) + 1;
142 	if (ep - bp < len) {
143 		RES_SET_H_ERRNO(statp, NO_RECOVERY);
144 		return (-1);
145 	}
146 	strlcpy(bp, cp, ep - bp);
147 	bp += len;
148 	cp = p;
149 	while (cp && *cp) {
150 		if (*cp == ' ' || *cp == '\t') {
151 			cp++;
152 			continue;
153 		}
154 		if (q >= &hed->host_aliases[_MAXALIASES - 1])
155 			break;
156 		if ((p = strpbrk(cp, " \t")) != NULL)
157 			*p++ = '\0';
158 		len = strlen(cp) + 1;
159 		if (ep - bp < len)
160 			break;
161 		strlcpy(bp, cp, ep - bp);
162 		*q++ = bp;
163 		bp += len;
164 		cp = p;
165 	}
166 	*q = NULL;
167 	RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
168 	return (0);
169 }
170 
171 int
172 gethostent_r(struct hostent *hptr, char *buffer, size_t buflen,
173     struct hostent **result, int *h_errnop)
174 {
175 	struct hostent_data *hed;
176 	struct hostent he;
177 	res_state statp;
178 
179 	statp = __res_state();
180 	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
181 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
182 		*h_errnop = statp->res_h_errno;
183 		return (-1);
184 	}
185 	if ((hed = __hostent_data_init()) == NULL) {
186 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
187 		*h_errnop = statp->res_h_errno;
188 		return (-1);
189 	}
190 	if (gethostent_p(&he, hed, statp->options & RES_USE_INET6, statp) != 0)
191 		return (-1);
192 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
193 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
194 		*h_errnop = statp->res_h_errno;
195 		return ((errno != 0) ? errno : -1);
196 	}
197 	*result = hptr;
198 	return (0);
199 }
200 
201 struct hostent *
202 gethostent(void)
203 {
204 	struct hostdata *hd;
205 	struct hostent *rval;
206 	int ret_h_errno;
207 
208 	if ((hd = __hostdata_init()) == NULL)
209 		return (NULL);
210 	if (gethostent_r(&hd->host, hd->data, sizeof(hd->data), &rval,
211 	    &ret_h_errno) != 0)
212 		return (NULL);
213 	return (rval);
214 }
215 
216 int
217 _ht_gethostbyname(void *rval, void *cb_data, va_list ap)
218 {
219 	const char *name;
220 	int af;
221 	char *buffer;
222 	size_t buflen;
223 	int *errnop, *h_errnop;
224 	struct hostent *hptr, he;
225 	struct hostent_data *hed;
226 	char **cp;
227 	res_state statp;
228 	int error;
229 
230 	name = va_arg(ap, const char *);
231 	af = va_arg(ap, int);
232 	hptr = va_arg(ap, struct hostent *);
233 	buffer = va_arg(ap, char *);
234 	buflen = va_arg(ap, size_t);
235 	errnop = va_arg(ap, int *);
236 	h_errnop = va_arg(ap, int *);
237 
238 	*((struct hostent **)rval) = NULL;
239 
240 	statp = __res_state();
241 	if ((hed = __hostent_data_init()) == NULL) {
242 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
243 		*h_errnop = statp->res_h_errno;
244 		return (NS_NOTFOUND);
245 	}
246 
247 	_sethosthtent(0, hed);
248 	while ((error = gethostent_p(&he, hed, 0, statp)) == 0) {
249 		if (he.h_addrtype != af)
250 			continue;
251 		if (he.h_addrtype == AF_INET &&
252 		    statp->options & RES_USE_INET6) {
253 			_map_v4v6_address(he.h_addr, he.h_addr);
254 			he.h_length = IN6ADDRSZ;
255 			he.h_addrtype = AF_INET6;
256 		}
257 		if (strcasecmp(he.h_name, name) == 0)
258 			break;
259 		for (cp = he.h_aliases; *cp != 0; cp++)
260 			if (strcasecmp(*cp, name) == 0)
261 				goto found;
262 	}
263 found:
264 	_endhosthtent(hed);
265 
266 	if (error != 0) {
267 		*h_errnop = statp->res_h_errno;
268 		return (NS_NOTFOUND);
269 	}
270 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
271 		*errnop = errno;
272 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
273 		*h_errnop = statp->res_h_errno;
274 		return (NS_RETURN);
275 	}
276 	*((struct hostent **)rval) = hptr;
277 	return (NS_SUCCESS);
278 }
279 
280 int
281 _ht_gethostbyaddr(void *rval, void *cb_data, va_list ap)
282 {
283 	const void *addr;
284 	socklen_t len;
285 	int af;
286 	char *buffer;
287 	size_t buflen;
288 	int *errnop, *h_errnop;
289 	struct hostent *hptr, he;
290 	struct hostent_data *hed;
291 	res_state statp;
292 	int error;
293 
294 	addr = va_arg(ap, const void *);
295 	len = va_arg(ap, socklen_t);
296 	af = va_arg(ap, int);
297 	hptr = va_arg(ap, struct hostent *);
298 	buffer = va_arg(ap, char *);
299 	buflen = va_arg(ap, size_t);
300 	errnop = va_arg(ap, int *);
301 	h_errnop = va_arg(ap, int *);
302 
303 	*((struct hostent **)rval) = NULL;
304 
305 	statp = __res_state();
306 	if ((hed = __hostent_data_init()) == NULL) {
307 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
308 		*h_errnop = statp->res_h_errno;
309 		return (NS_NOTFOUND);
310 	}
311 
312 	_sethosthtent(0, hed);
313 	while ((error = gethostent_p(&he, hed, 0, statp)) == 0)
314 		if (he.h_addrtype == af && !bcmp(he.h_addr, addr, len)) {
315 			if (he.h_addrtype == AF_INET &&
316 			    statp->options & RES_USE_INET6) {
317 				_map_v4v6_address(he.h_addr, he.h_addr);
318 				he.h_length = IN6ADDRSZ;
319 				he.h_addrtype = AF_INET6;
320 			}
321 			break;
322 		}
323 	_endhosthtent(hed);
324 
325 	if (error != 0)
326 		return (NS_NOTFOUND);
327 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
328 		*errnop = errno;
329 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
330 		*h_errnop = statp->res_h_errno;
331 		return (NS_RETURN);
332 	}
333 	*((struct hostent **)rval) = hptr;
334 	return (NS_SUCCESS);
335 }
336