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 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
52 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
53 *
54 * Permission to use, copy, modify, and distribute this software for any
55 * purpose with or without fee is hereby granted, provided that the above
56 * copyright notice and this permission notice appear in all copies.
57 */
58
59 #if defined(LIBC_SCCS) && !defined(lint)
60 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
61 #endif /* LIBC_SCCS and not lint */
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD: stable/12/lib/libc/net/getnetbydns.c 326025 2017-11-20 19:49:47Z pfg $");
64
65 #include <sys/param.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <arpa/nameser.h>
70
71 #include <errno.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <netdb.h>
75 #include <resolv.h>
76 #include <ctype.h>
77 #include <string.h>
78 #include <unistd.h>
79 #include <syslog.h>
80 #include <stdarg.h>
81 #include <nsswitch.h>
82
83 #include "netdb_private.h"
84 #include "res_config.h"
85
86 #define BYADDR 0
87 #define BYNAME 1
88
89 #define MAXPACKET (64*1024)
90
91 typedef union {
92 HEADER hdr;
93 u_char buf[MAXPACKET];
94 } querybuf;
95
96 typedef union {
97 long al;
98 char ac;
99 } align;
100
101 /*
102 * Reverse the order of first four dotted entries of in.
103 * Out must contain space for at least strlen(in) characters.
104 * The result does not include any leading 0s of in.
105 */
106 static void
ipreverse(char * in,char * out)107 ipreverse(char *in, char *out)
108 {
109 char *pos[4];
110 int len[4];
111 char *p, *start;
112 int i = 0;
113 int leading = 1;
114
115 /* Fill-in element positions and lengths: pos[], len[]. */
116 start = p = in;
117 for (;;) {
118 if (*p == '.' || *p == '\0') {
119 /* Leading 0? */
120 if (leading && p - start == 1 && *start == '0')
121 len[i] = 0;
122 else {
123 len[i] = p - start;
124 leading = 0;
125 }
126 pos[i] = start;
127 start = p + 1;
128 i++;
129 }
130 if (i == 4)
131 break;
132 if (*p == 0) {
133 for (; i < 4; i++) {
134 pos[i] = p;
135 len[i] = 0;
136 }
137 break;
138 }
139 p++;
140 }
141
142 /* Copy the entries in reverse order */
143 p = out;
144 leading = 1;
145 for (i = 3; i >= 0; i--) {
146 memcpy(p, pos[i], len[i]);
147 if (len[i])
148 leading = 0;
149 p += len[i];
150 /* Need a . separator? */
151 if (!leading && i > 0 && len[i - 1])
152 *p++ = '.';
153 }
154 *p = '\0';
155 }
156
157 static int
getnetanswer(querybuf * answer,int anslen,int net_i,struct netent * ne,struct netent_data * ned,res_state statp)158 getnetanswer(querybuf *answer, int anslen, int net_i, struct netent *ne,
159 struct netent_data *ned, res_state statp)
160 {
161
162 HEADER *hp;
163 u_char *cp;
164 int n;
165 u_char *eom;
166 int type, class, ancount, qdcount, haveanswer;
167 char aux[MAXHOSTNAMELEN];
168 char ans[MAXHOSTNAMELEN];
169 char *in, *bp, *ep, **ap;
170
171 /*
172 * find first satisfactory answer
173 *
174 * answer --> +------------+ ( MESSAGE )
175 * | Header |
176 * +------------+
177 * | Question | the question for the name server
178 * +------------+
179 * | Answer | RRs answering the question
180 * +------------+
181 * | Authority | RRs pointing toward an authority
182 * | Additional | RRs holding additional information
183 * +------------+
184 */
185 eom = answer->buf + anslen;
186 hp = &answer->hdr;
187 ancount = ntohs(hp->ancount); /* #/records in the answer section */
188 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
189 bp = ned->netbuf;
190 ep = ned->netbuf + sizeof(ned->netbuf);
191 cp = answer->buf + HFIXEDSZ;
192 if (!qdcount) {
193 if (hp->aa)
194 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
195 else
196 RES_SET_H_ERRNO(statp, TRY_AGAIN);
197 return (-1);
198 }
199 while (qdcount-- > 0)
200 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
201 ap = ned->net_aliases;
202 *ap = NULL;
203 ne->n_aliases = ned->net_aliases;
204 haveanswer = 0;
205 while (--ancount >= 0 && cp < eom) {
206 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
207 if ((n < 0) || !res_dnok(bp))
208 break;
209 cp += n;
210 ans[0] = '\0';
211 (void)strncpy(&ans[0], bp, sizeof(ans) - 1);
212 ans[sizeof(ans) - 1] = '\0';
213 GETSHORT(type, cp);
214 GETSHORT(class, cp);
215 cp += INT32SZ; /* TTL */
216 GETSHORT(n, cp);
217 if (class == C_IN && type == T_PTR) {
218 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
219 if ((n < 0) || !res_hnok(bp)) {
220 cp += n;
221 return (-1);
222 }
223 cp += n;
224 *ap++ = bp;
225 n = strlen(bp) + 1;
226 bp += n;
227 ne->n_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
228 haveanswer++;
229 }
230 }
231 if (haveanswer) {
232 *ap = NULL;
233 switch (net_i) {
234 case BYADDR:
235 ne->n_name = *ne->n_aliases;
236 ne->n_net = 0L;
237 break;
238 case BYNAME:
239 in = *ne->n_aliases;
240 n = strlen(ans) + 1;
241 if (ep - bp < n) {
242 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
243 errno = ENOBUFS;
244 return (-1);
245 }
246 strlcpy(bp, ans, ep - bp);
247 ne->n_name = bp;
248 if (strlen(in) + 1 > sizeof(aux)) {
249 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
250 errno = ENOBUFS;
251 return (-1);
252 }
253 ipreverse(in, aux);
254 ne->n_net = inet_network(aux);
255 break;
256 }
257 ne->n_aliases++;
258 return (0);
259 }
260 RES_SET_H_ERRNO(statp, TRY_AGAIN);
261 return (-1);
262 }
263
264 int
_dns_getnetbyaddr(void * rval,void * cb_data,va_list ap)265 _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap)
266 {
267 uint32_t net;
268 int net_type;
269 char *buffer;
270 size_t buflen;
271 int *errnop, *h_errnop;
272 struct netent *nptr, ne;
273 struct netent_data *ned;
274 unsigned int netbr[4];
275 int nn, anslen, error;
276 querybuf *buf;
277 char qbuf[MAXDNAME];
278 uint32_t net2;
279 res_state statp;
280
281 net = va_arg(ap, uint32_t);
282 net_type = va_arg(ap, int);
283 nptr = va_arg(ap, struct netent *);
284 buffer = va_arg(ap, char *);
285 buflen = va_arg(ap, size_t);
286 errnop = va_arg(ap, int *);
287 h_errnop = va_arg(ap, int *);
288
289 statp = __res_state();
290 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
291 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
292 *h_errnop = statp->res_h_errno;
293 return (NS_UNAVAIL);
294 }
295
296 if ((ned = __netent_data_init()) == NULL) {
297 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
298 *h_errnop = statp->res_h_errno;
299 return (NS_UNAVAIL);
300 }
301
302 *((struct netent **)rval) = NULL;
303
304 if (net_type != AF_INET) {
305 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
306 *h_errnop = statp->res_h_errno;
307 return (NS_UNAVAIL);
308 }
309
310 for (nn = 4, net2 = net; net2; net2 >>= 8)
311 netbr[--nn] = net2 & 0xff;
312 switch (nn) {
313 case 3: /* Class A */
314 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
315 break;
316 case 2: /* Class B */
317 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
318 break;
319 case 1: /* Class C */
320 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
321 netbr[1]);
322 break;
323 case 0: /* Class D - E */
324 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
325 netbr[1], netbr[0]);
326 break;
327 }
328 if ((buf = malloc(sizeof(*buf))) == NULL) {
329 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
330 *h_errnop = statp->res_h_errno;
331 return (NS_NOTFOUND);
332 }
333 anslen = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
334 sizeof(*buf));
335 if (anslen < 0) {
336 free(buf);
337 #ifdef DEBUG
338 if (statp->options & RES_DEBUG)
339 printf("res_nsearch failed\n");
340 #endif
341 *h_errnop = statp->res_h_errno;
342 return (NS_UNAVAIL);
343 } else if (anslen > sizeof(*buf)) {
344 free(buf);
345 #ifdef DEBUG
346 if (statp->options & RES_DEBUG)
347 printf("res_nsearch static buffer too small\n");
348 #endif
349 *h_errnop = statp->res_h_errno;
350 return (NS_UNAVAIL);
351 }
352 error = getnetanswer(buf, anslen, BYADDR, &ne, ned, statp);
353 free(buf);
354 if (error == 0) {
355 /* Strip trailing zeros */
356 while ((net & 0xff) == 0 && net != 0)
357 net >>= 8;
358 ne.n_net = net;
359 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
360 *errnop = errno;
361 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
362 *h_errnop = statp->res_h_errno;
363 return (NS_RETURN);
364 }
365 *((struct netent **)rval) = nptr;
366 return (NS_SUCCESS);
367 }
368 *h_errnop = statp->res_h_errno;
369 return (NS_NOTFOUND);
370 }
371
372 int
_dns_getnetbyname(void * rval,void * cb_data,va_list ap)373 _dns_getnetbyname(void *rval, void *cb_data, va_list ap)
374 {
375 const char *net;
376 char *buffer;
377 size_t buflen;
378 int *errnop, *h_errnop;
379 struct netent *nptr, ne;
380 struct netent_data *ned;
381 int anslen, error;
382 querybuf *buf;
383 char qbuf[MAXDNAME];
384 res_state statp;
385
386 net = va_arg(ap, const char *);
387 nptr = va_arg(ap, struct netent *);
388 buffer = va_arg(ap, char *);
389 buflen = va_arg(ap, size_t);
390 errnop = va_arg(ap, int *);
391 h_errnop = va_arg(ap, int *);
392
393 statp = __res_state();
394 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
395 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
396 *h_errnop = statp->res_h_errno;
397 return (NS_UNAVAIL);
398 }
399 if ((ned = __netent_data_init()) == NULL) {
400 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
401 *h_errnop = statp->res_h_errno;
402 return (NS_UNAVAIL);
403 }
404 if ((buf = malloc(sizeof(*buf))) == NULL) {
405 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
406 *h_errnop = statp->res_h_errno;
407 return (NS_NOTFOUND);
408 }
409
410 *((struct netent **)rval) = NULL;
411
412 strncpy(qbuf, net, sizeof(qbuf) - 1);
413 qbuf[sizeof(qbuf) - 1] = '\0';
414 anslen = res_nsearch(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
415 sizeof(*buf));
416 if (anslen < 0) {
417 free(buf);
418 #ifdef DEBUG
419 if (statp->options & RES_DEBUG)
420 printf("res_nsearch failed\n");
421 #endif
422 return (NS_UNAVAIL);
423 } else if (anslen > sizeof(*buf)) {
424 free(buf);
425 #ifdef DEBUG
426 if (statp->options & RES_DEBUG)
427 printf("res_search static buffer too small\n");
428 #endif
429 return (NS_UNAVAIL);
430 }
431 error = getnetanswer(buf, anslen, BYNAME, &ne, ned, statp);
432 free(buf);
433 if (error != 0) {
434 *h_errnop = statp->res_h_errno;
435 return (NS_NOTFOUND);
436 }
437 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
438 *errnop = errno;
439 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
440 *h_errnop = statp->res_h_errno;
441 return (NS_RETURN);
442 }
443 *((struct netent **)rval) = nptr;
444 return (NS_SUCCESS);
445 }
446
447 void
_setnetdnsent(int stayopen)448 _setnetdnsent(int stayopen)
449 {
450 res_state statp;
451
452 statp = __res_state();
453 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
454 return;
455 if (stayopen)
456 statp->options |= RES_STAYOPEN | RES_USEVC;
457 }
458
459 void
_endnetdnsent(void)460 _endnetdnsent(void)
461 {
462 res_state statp;
463
464 statp = __res_state();
465 statp->options &= ~(RES_STAYOPEN | RES_USEVC);
466 res_nclose(statp);
467 }
468