xref: /freebsd/lib/libc/net/gethostbyht.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  * -
29  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
30  *
31  * Permission to use, copy, modify, and distribute this software for any
32  * purpose with or without fee is hereby granted, provided that the above
33  * copyright notice and this permission notice appear in all copies, and that
34  * the name of Digital Equipment Corporation not be used in advertising or
35  * publicity pertaining to distribution of the document or software without
36  * specific, written prior permission.
37  *
38  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
39  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
41  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
42  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
43  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
44  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45  * SOFTWARE.
46  * -
47  * --Copyright--
48  */
49 
50 #if defined(LIBC_SCCS) && !defined(lint)
51 static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
52 #endif /* LIBC_SCCS and not lint */
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55 
56 #include <sys/param.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
60 #include <netdb.h>
61 #include <stdio.h>
62 #include <ctype.h>
63 #include <string.h>
64 #include <stdarg.h>
65 #include <nsswitch.h>
66 #include <arpa/nameser.h>	/* XXX */
67 #include <resolv.h>		/* XXX */
68 #include "netdb_private.h"
69 
70 void
71 _sethosthtent(int f, struct hostent_data *hed)
72 {
73 	if (!hed->hostf)
74 		hed->hostf = fopen(_PATH_HOSTS, "r");
75 	else
76 		rewind(hed->hostf);
77 	hed->stayopen = f;
78 }
79 
80 void
81 _endhosthtent(struct hostent_data *hed)
82 {
83 	if (hed->hostf && !hed->stayopen) {
84 		(void) fclose(hed->hostf);
85 		hed->hostf = NULL;
86 	}
87 }
88 
89 static int
90 gethostent_p(struct hostent *he, struct hostent_data *hed, int mapped,
91     res_state statp)
92 {
93 	char *p, *bp, *ep;
94 	char *cp, **q;
95 	int af, len;
96 	char hostbuf[BUFSIZ + 1];
97 
98 	if (!hed->hostf && !(hed->hostf = fopen(_PATH_HOSTS, "r"))) {
99 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
100 		return (-1);
101 	}
102  again:
103 	if (!(p = fgets(hostbuf, sizeof hostbuf, hed->hostf))) {
104 		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
105 		return (-1);
106 	}
107 	if (*p == '#')
108 		goto again;
109 	cp = strpbrk(p, "#\n");
110 	if (cp != NULL)
111 		*cp = '\0';
112 	if (!(cp = strpbrk(p, " \t")))
113 		goto again;
114 	*cp++ = '\0';
115 	if (inet_pton(AF_INET6, p, hed->host_addr) > 0) {
116 		af = AF_INET6;
117 		len = IN6ADDRSZ;
118 	} else if (inet_pton(AF_INET, p, hed->host_addr) > 0) {
119 		if (mapped) {
120 			_map_v4v6_address((char *)hed->host_addr,
121 			    (char *)hed->host_addr);
122 			af = AF_INET6;
123 			len = IN6ADDRSZ;
124 		} else {
125 			af = AF_INET;
126 			len = INADDRSZ;
127 		}
128 	} else {
129 		goto again;
130 	}
131 	hed->h_addr_ptrs[0] = (char *)hed->host_addr;
132 	hed->h_addr_ptrs[1] = NULL;
133 	he->h_addr_list = hed->h_addr_ptrs;
134 	he->h_length = len;
135 	he->h_addrtype = af;
136 	while (*cp == ' ' || *cp == '\t')
137 		cp++;
138 	bp = hed->hostbuf;
139 	ep = hed->hostbuf + sizeof hed->hostbuf;
140 	he->h_name = bp;
141 	q = he->h_aliases = hed->host_aliases;
142 	if ((p = strpbrk(cp, " \t")) != NULL)
143 		*p++ = '\0';
144 	len = strlen(cp) + 1;
145 	if (ep - bp < len) {
146 		RES_SET_H_ERRNO(statp, NO_RECOVERY);
147 		return (-1);
148 	}
149 	strlcpy(bp, cp, ep - bp);
150 	bp += len;
151 	cp = p;
152 	while (cp && *cp) {
153 		if (*cp == ' ' || *cp == '\t') {
154 			cp++;
155 			continue;
156 		}
157 		if (q >= &hed->host_aliases[_MAXALIASES - 1])
158 			break;
159 		if ((p = strpbrk(cp, " \t")) != NULL)
160 			*p++ = '\0';
161 		len = strlen(cp) + 1;
162 		if (ep - bp < len)
163 			break;
164 		strlcpy(bp, cp, ep - bp);
165 		*q++ = bp;
166 		bp += len;
167 		cp = p;
168 	}
169 	*q = NULL;
170 	RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
171 	return (0);
172 }
173 
174 int
175 gethostent_r(struct hostent *hptr, char *buffer, size_t buflen,
176     struct hostent **result, int *h_errnop)
177 {
178 	struct hostent_data *hed;
179 	struct hostent he;
180 	res_state statp;
181 
182 	statp = __res_state();
183 	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
184 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
185 		*h_errnop = statp->res_h_errno;
186 		return (-1);
187 	}
188 	if ((hed = __hostent_data_init()) == NULL) {
189 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
190 		*h_errnop = statp->res_h_errno;
191 		return (-1);
192 	}
193 	if (gethostent_p(&he, hed, statp->options & RES_USE_INET6, statp) != 0)
194 		return (-1);
195 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0)
196 		return (-1);
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 		*h_errnop = statp->res_h_errno;
272 		return (NS_NOTFOUND);
273 	}
274 	*((struct hostent **)rval) = hptr;
275 	return (NS_SUCCESS);
276 }
277 
278 int
279 _ht_gethostbyaddr(void *rval, void *cb_data, va_list ap)
280 {
281 	const void *addr;
282 	socklen_t len;
283 	int af;
284 	char *buffer;
285 	size_t buflen;
286 	int *errnop, *h_errnop;
287 	struct hostent *hptr, he;
288 	struct hostent_data *hed;
289 	res_state statp;
290 	int error;
291 
292 	addr = va_arg(ap, const void *);
293 	len = va_arg(ap, socklen_t);
294 	af = va_arg(ap, int);
295 	hptr = va_arg(ap, struct hostent *);
296 	buffer = va_arg(ap, char *);
297 	buflen = va_arg(ap, size_t);
298 	errnop = va_arg(ap, int *);
299 	h_errnop = va_arg(ap, int *);
300 
301 	*((struct hostent **)rval) = NULL;
302 
303 	statp = __res_state();
304 	if ((hed = __hostent_data_init()) == NULL) {
305 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
306 		*h_errnop = statp->res_h_errno;
307 		return (NS_NOTFOUND);
308 	}
309 
310 	_sethosthtent(0, hed);
311 	while ((error = gethostent_p(&he, hed, 0, statp)) == 0)
312 		if (he.h_addrtype == af && !bcmp(he.h_addr, addr, len)) {
313 			if (he.h_addrtype == AF_INET &&
314 			    statp->options & RES_USE_INET6) {
315 				_map_v4v6_address(he.h_addr, he.h_addr);
316 				he.h_length = IN6ADDRSZ;
317 				he.h_addrtype = AF_INET6;
318 			}
319 			break;
320 		}
321 	_endhosthtent(hed);
322 
323 	if (error != 0)
324 		return (NS_NOTFOUND);
325 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
326 		*h_errnop = statp->res_h_errno;
327 		return (NS_NOTFOUND);
328 	}
329 	*((struct hostent **)rval) = hptr;
330 	return (NS_SUCCESS);
331 }
332