1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /*! \file */
13 
14 /**
15  *    These functions provide hostname-to-address and address-to-hostname
16  *    lookups by means of the lightweight resolver. They are similar to the
17  *    standard gethostent(3) functions provided by most operating systems.
18  *    They use a struct hostent which is usually defined in <namedb.h>.
19  *
20  * \code
21  * struct  hostent {
22  *         char    *h_name;        // official name of host
23  * 	   char    **h_aliases;    // alias list
24  *         int     h_addrtype;     // host address type
25  *         int     h_length;       // length of address
26  *         char    **h_addr_list;  // list of addresses from name server
27  * };
28  * #define h_addr  h_addr_list[0]  // address, for backward compatibility
29  * \endcode
30  *
31  *    The members of this structure are:
32  *
33  * \li   h_name:
34  *           The official (canonical) name of the host.
35  *
36  * \li   h_aliases:
37  *           A NULL-terminated array of alternate names (nicknames) for the
38  *           host.
39  *
40  * \li   h_addrtype:
41  *           The type of address being returned -- PF_INET or PF_INET6.
42  *
43  * \li   h_length:
44  *           The length of the address in bytes.
45  *
46  * \li   h_addr_list:
47  *           A NULL terminated array of network addresses for the host. Host
48  *           addresses are returned in network byte order.
49  *
50  *    For backward compatibility with very old software, h_addr is the first
51  *    address in h_addr_list.
52  *
53  *    lwres_gethostent(), lwres_sethostent(), lwres_endhostent(),
54  *    lwres_gethostent_r(), lwres_sethostent_r() and lwres_endhostent_r()
55  *    provide iteration over the known host entries on systems that provide
56  *    such functionality through facilities like /etc/hosts or NIS. The
57  *    lightweight resolver does not currently implement these functions; it
58  *    only provides them as stub functions that always return failure.
59  *
60  *    lwres_gethostbyname() and lwres_gethostbyname2() look up the hostname
61  *    name. lwres_gethostbyname() always looks for an IPv4 address while
62  *    lwres_gethostbyname2() looks for an address of protocol family af:
63  *    either PF_INET or PF_INET6 -- IPv4 or IPV6 addresses respectively.
64  *    Successful calls of the functions return a struct hostent for the name
65  *    that was looked up. NULL is returned if the lookups by
66  *    lwres_gethostbyname() or lwres_gethostbyname2() fail.
67  *
68  *    Reverse lookups of addresses are performed by lwres_gethostbyaddr().
69  *    addr is an address of length len bytes and protocol family type --
70  *    PF_INET or PF_INET6. lwres_gethostbyname_r() is a thread-safe function
71  *    for forward lookups. If an error occurs, an error code is returned in
72  *    *error. resbuf is a pointer to a struct hostent which is initialised
73  *    by a successful call to lwres_gethostbyname_r() . buf is a buffer of
74  *    length len bytes which is used to store the h_name, h_aliases, and
75  *    h_addr_list elements of the struct hostent returned in resbuf.
76  *    Successful calls to lwres_gethostbyname_r() return resbuf, which is a
77  *    pointer to the struct hostent it created.
78  *
79  *    lwres_gethostbyaddr_r() is a thread-safe function that performs a
80  *    reverse lookup of address addr which is len bytes long and is of
81  *    protocol family type -- PF_INET or PF_INET6. If an error occurs, the
82  *    error code is returned in *error. The other function parameters are
83  *    identical to those in lwres_gethostbyname_r(). resbuf is a pointer to
84  *    a struct hostent which is initialised by a successful call to
85  *    lwres_gethostbyaddr_r(). buf is a buffer of length len bytes which is
86  *    used to store the h_name, h_aliases, and h_addr_list elements of the
87  *    struct hostent returned in resbuf. Successful calls to
88  *    lwres_gethostbyaddr_r() return resbuf, which is a pointer to the
89  *    struct hostent it created.
90  *
91  * \section gethost_return Return Values
92  *
93  *    The functions lwres_gethostbyname(), lwres_gethostbyname2(),
94  *    lwres_gethostbyaddr(), and lwres_gethostent() return NULL to indicate
95  *    an error. In this case the global variable lwres_h_errno will contain
96  *    one of the following error codes defined in \link netdb.h <lwres/netdb.h>:\endlink
97  *
98  * \li #HOST_NOT_FOUND:
99  *           The host or address was not found.
100  *
101  * \li #TRY_AGAIN:
102  *           A recoverable error occurred, e.g., a timeout. Retrying the
103  *           lookup may succeed.
104  *
105  * \li #NO_RECOVERY:
106  *           A non-recoverable error occurred.
107  *
108  * \li #NO_DATA:
109  *           The name exists, but has no address information associated with
110  *           it (or vice versa in the case of a reverse lookup). The code
111  *           NO_ADDRESS is accepted as a synonym for NO_DATA for backwards
112  *           compatibility.
113  *
114  *    lwres_hstrerror() translates these error codes to suitable error
115  *    messages.
116  *
117  *    lwres_gethostent() and lwres_gethostent_r() always return NULL.
118  *
119  *    Successful calls to lwres_gethostbyname_r() and
120  *    lwres_gethostbyaddr_r() return resbuf, a pointer to the struct hostent
121  *    that was initialised by these functions. They return NULL if the
122  *    lookups fail or if buf was too small to hold the list of addresses and
123  *    names referenced by the h_name, h_aliases, and h_addr_list elements of
124  *    the struct hostent. If buf was too small, both lwres_gethostbyname_r()
125  *    and lwres_gethostbyaddr_r() set the global variable errno to ERANGE.
126  *
127  * \section gethost_see See Also
128  *
129  *    gethostent(), \link getipnode.c getipnode\endlink, lwres_hstrerror()
130  *
131  * \section gethost_bugs Bugs
132  *
133  *    lwres_gethostbyname(), lwres_gethostbyname2(), lwres_gethostbyaddr()
134  *    and lwres_endhostent() are not thread safe; they return pointers to
135  *    static data and provide error codes through a global variable.
136  *    Thread-safe versions for name and address lookup are provided by
137  *    lwres_gethostbyname_r(), and lwres_gethostbyaddr_r() respectively.
138  *
139  *    The resolver daemon does not currently support any non-DNS name
140  *    services such as /etc/hosts or NIS, consequently the above functions
141  *    don't, either.
142  */
143 
144 #include <config.h>
145 
146 #include <errno.h>
147 #include <string.h>
148 #ifdef HAVE_INTTYPES_H
149 #include <inttypes.h> /* uintptr_t */
150 #endif
151 
152 #include <lwres/net.h>
153 #include <lwres/netdb.h>
154 
155 #include "assert_p.h"
156 
157 #define LWRES_ALIGNBYTES (sizeof(char *) - 1)
158 #define LWRES_ALIGN(p) \
159 	(((uintptr_t)(p) + LWRES_ALIGNBYTES) &~ LWRES_ALIGNBYTES)
160 
161 static struct hostent *he = NULL;
162 static int copytobuf(struct hostent *, struct hostent *, char *, int);
163 
164 /*% Always looks for an IPv4 address. */
165 struct hostent *
lwres_gethostbyname(const char * name)166 lwres_gethostbyname(const char *name) {
167 
168 	if (he != NULL)
169 		lwres_freehostent(he);
170 
171 	he = lwres_getipnodebyname(name, AF_INET, 0, &lwres_h_errno);
172 	return (he);
173 }
174 
175 /*% Looks for either an IPv4 or IPv6 address. */
176 struct hostent *
lwres_gethostbyname2(const char * name,int af)177 lwres_gethostbyname2(const char *name, int af) {
178 	if (he != NULL)
179 		lwres_freehostent(he);
180 
181 	he = lwres_getipnodebyname(name, af, 0, &lwres_h_errno);
182 	return (he);
183 }
184 
185 /*% Reverse lookup of addresses. */
186 struct hostent *
lwres_gethostbyaddr(const char * addr,int len,int type)187 lwres_gethostbyaddr(const char *addr, int len, int type) {
188 
189 	if (he != NULL)
190 		lwres_freehostent(he);
191 
192 	he = lwres_getipnodebyaddr(addr, len, type, &lwres_h_errno);
193 	return (he);
194 }
195 
196 /*% Stub function.  Always returns failure. */
197 struct hostent *
lwres_gethostent(void)198 lwres_gethostent(void) {
199 	if (he != NULL)
200 		lwres_freehostent(he);
201 
202 	return (NULL);
203 }
204 
205 /*% Stub function.  Always returns failure. */
206 void
lwres_sethostent(int stayopen)207 lwres_sethostent(int stayopen) {
208 	/*
209 	 * Empty.
210 	 */
211 	UNUSED(stayopen);
212 }
213 
214 /*% Stub function.  Always returns failure. */
215 void
lwres_endhostent(void)216 lwres_endhostent(void) {
217 	/*
218 	 * Empty.
219 	 */
220 }
221 
222 /*% Thread-safe function for forward lookups. */
223 struct hostent *
lwres_gethostbyname_r(const char * name,struct hostent * resbuf,char * buf,int buflen,int * error)224 lwres_gethostbyname_r(const char *name, struct hostent *resbuf,
225 		char *buf, int buflen, int *error)
226 {
227 	struct hostent *myhe;
228 	int res;
229 
230 	myhe = lwres_getipnodebyname(name, AF_INET, 0, error);
231 	if (myhe == NULL)
232 		return (NULL);
233 	res = copytobuf(myhe, resbuf, buf, buflen);
234 	lwres_freehostent(myhe);
235 	if (res != 0) {
236 		errno = ERANGE;
237 		return (NULL);
238 	}
239 	return (resbuf);
240 }
241 
242 /*% Thread-safe reverse lookup. */
243 struct hostent  *
lwres_gethostbyaddr_r(const char * addr,int len,int type,struct hostent * resbuf,char * buf,int buflen,int * error)244 lwres_gethostbyaddr_r(const char *addr, int len, int type,
245 		      struct hostent *resbuf, char *buf, int buflen,
246 		      int *error)
247 {
248 	struct hostent *myhe;
249 	int res;
250 
251 	myhe = lwres_getipnodebyaddr(addr, len, type, error);
252 	if (myhe == NULL)
253 		return (NULL);
254 	res = copytobuf(myhe, resbuf, buf, buflen);
255 	lwres_freehostent(myhe);
256 	if (res != 0) {
257 		errno = ERANGE;
258 		return (NULL);
259 	}
260 	return (resbuf);
261 }
262 
263 /*% Stub function.  Always returns failure. */
264 struct hostent  *
lwres_gethostent_r(struct hostent * resbuf,char * buf,int buflen,int * error)265 lwres_gethostent_r(struct hostent *resbuf, char *buf, int buflen, int *error) {
266 	UNUSED(resbuf);
267 	UNUSED(buf);
268 	UNUSED(buflen);
269 	*error = 0;
270 	return (NULL);
271 }
272 
273 /*% Stub function.  Always returns failure. */
274 void
lwres_sethostent_r(int stayopen)275 lwres_sethostent_r(int stayopen) {
276 	/*
277 	 * Empty.
278 	 */
279 	UNUSED(stayopen);
280 }
281 
282 /*% Stub function.  Always returns failure. */
283 void
lwres_endhostent_r(void)284 lwres_endhostent_r(void) {
285 	/*
286 	 * Empty.
287 	 */
288 }
289 
290 static int
copytobuf(struct hostent * src,struct hostent * hptr,char * buf,int buflen)291 copytobuf(struct hostent *src, struct hostent *hptr, char *buf, int buflen) {
292 	char *cp;
293 	char **ptr;
294 	int i, n;
295 	int nptr, len;
296 
297 	/*
298 	 * Find out the amount of space required to store the answer.
299 	 */
300 	nptr = 2; /* NULL ptrs */
301 	len = (int)((char *)LWRES_ALIGN(buf) - buf);
302 	for (i = 0; src->h_addr_list[i]; i++, nptr++) {
303 		len += src->h_length;
304 	}
305 	for (i = 0; src->h_aliases[i]; i++, nptr++) {
306 		len += strlen(src->h_aliases[i]) + 1;
307 	}
308 	len += strlen(src->h_name) + 1;
309 	len += nptr * sizeof(char*);
310 
311 	if (len > buflen) {
312 		return (-1);
313 	}
314 
315 	/*
316 	 * Copy address size and type.
317 	 */
318 	hptr->h_addrtype = src->h_addrtype;
319 	n = hptr->h_length = src->h_length;
320 
321 	ptr = (char **)LWRES_ALIGN(buf);
322 	cp = (char *)LWRES_ALIGN(buf) + nptr * sizeof(char *);
323 
324 	/*
325 	 * Copy address list.
326 	 */
327 	hptr->h_addr_list = ptr;
328 	for (i = 0; src->h_addr_list[i]; i++, ptr++) {
329 		memmove(cp, src->h_addr_list[i], n);
330 		hptr->h_addr_list[i] = cp;
331 		cp += n;
332 	}
333 	hptr->h_addr_list[i] = NULL;
334 	ptr++;
335 
336 	/*
337 	 * Copy official name.
338 	 */
339 	n = strlen(src->h_name) + 1;
340 	strcpy(cp, src->h_name);
341 	hptr->h_name = cp;
342 	cp += n;
343 
344 	/*
345 	 * Copy aliases.
346 	 */
347 	hptr->h_aliases = ptr;
348 	for (i = 0; src->h_aliases[i]; i++) {
349 		n = strlen(src->h_aliases[i]) + 1;
350 		strcpy(cp, src->h_aliases[i]);
351 		hptr->h_aliases[i] = cp;
352 		cp += n;
353 	}
354 	hptr->h_aliases[i] = NULL;
355 
356 	return (0);
357 }
358