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