1 /*-
2 * SPDX-License-Identifier: (BSD-3-Clause AND ISC)
3 *
4 * Copyright (c) 1985, 1989, 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
32 /*
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 */
51
52 /*
53 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
54 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
55 *
56 * Permission to use, copy, modify, and distribute this software for any
57 * purpose with or without fee is hereby granted, provided that the above
58 * copyright notice and this permission notice appear in all copies.
59 *
60 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
61 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
62 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
63 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
64 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
65 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
66 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
67 */
68
69 #if defined(LIBC_SCCS) && !defined(lint)
70 static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
71 static const char rcsid[] = "$Id: res_init.c,v 1.26 2008/12/11 09:59:00 marka Exp $";
72 #endif /* LIBC_SCCS and not lint */
73 #include <sys/cdefs.h>
74 __FBSDID("$FreeBSD$");
75
76 #include "port_before.h"
77
78 #include "namespace.h"
79
80 #include <sys/param.h>
81 #include <sys/socket.h>
82 #include <sys/stat.h>
83 #include <sys/time.h>
84
85 #include <netinet/in.h>
86 #include <arpa/inet.h>
87 #include <arpa/nameser.h>
88
89 #include <ctype.h>
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <string.h>
93 #include <unistd.h>
94 #include <netdb.h>
95
96 #ifndef HAVE_MD5
97 # include "../dst/md5.h"
98 #else
99 # ifdef SOLARIS2
100 # include <sys/md5.h>
101 # elif _LIBC
102 # include <md5.h>
103 # endif
104 #endif
105 #ifndef _MD5_H_
106 # define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */
107 #endif
108
109 #include "un-namespace.h"
110
111 #include "port_after.h"
112
113 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
114 #include <resolv.h>
115
116 #include "res_private.h"
117
118 /*% Options. Should all be left alone. */
119 #define RESOLVSORT
120 #ifndef DEBUG
121 #define DEBUG
122 #endif
123
124 #ifdef SOLARIS2
125 #include <sys/systeminfo.h>
126 #endif
127
128 static void res_setoptions(res_state, const char *, const char *);
129
130 #ifdef RESOLVSORT
131 static const char sort_mask[] = "/&";
132 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
133 static u_int32_t net_mask(struct in_addr);
134 #endif
135
136 #if !defined(isascii) /*%< XXX - could be a function */
137 # define isascii(c) (!(c & 0200))
138 #endif
139
140 /*
141 * Resolver state default settings.
142 */
143
144 /*%
145 * Set up default settings. If the configuration file exist, the values
146 * there will have precedence. Otherwise, the server address is set to
147 * INADDR_ANY and the default domain name comes from the gethostname().
148 *
149 * An interim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
150 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
151 * since it was noted that INADDR_ANY actually meant ``the first interface
152 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
153 * it had to be "up" in order for you to reach your own name server. It
154 * was later decided that since the recommended practice is to always
155 * install local static routes through 127.0.0.1 for all your network
156 * interfaces, that we could solve this problem without a code change.
157 *
158 * The configuration file should always be used, since it is the only way
159 * to specify a default domain. If you are running a server on your local
160 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
161 * in the configuration file.
162 *
163 * Return 0 if completes successfully, -1 on error
164 */
165 int
res_ninit(res_state statp)166 res_ninit(res_state statp) {
167 extern int __res_vinit(res_state, int);
168
169 return (__res_vinit(statp, 0));
170 }
171
172 /*% This function has to be reachable by res_data.c but not publicly. */
173 int
__res_vinit(res_state statp,int preinit)174 __res_vinit(res_state statp, int preinit) {
175 FILE *fp;
176 char *cp, **pp;
177 int n;
178 char buf[BUFSIZ];
179 int nserv = 0; /*%< number of nameserver records read from file */
180 int haveenv = 0;
181 int havesearch = 0;
182 #ifdef RESOLVSORT
183 int nsort = 0;
184 char *net;
185 #endif
186 int dots;
187 union res_sockaddr_union u[2];
188 int maxns = MAXNS;
189
190 RES_SET_H_ERRNO(statp, 0);
191 if (statp->_u._ext.ext != NULL)
192 res_ndestroy(statp);
193
194 if (!preinit) {
195 statp->retrans = RES_TIMEOUT;
196 statp->retry = RES_DFLRETRY;
197 statp->options = RES_DEFAULT;
198 }
199
200 statp->_rnd = malloc(16);
201 res_rndinit(statp);
202 statp->id = res_nrandomid(statp);
203
204 memset(u, 0, sizeof(u));
205 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
206 u[nserv].sin.sin_family = AF_INET;
207 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
208 #ifdef HAVE_SA_LEN
209 u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
210 #endif
211 nserv++;
212 #ifdef HAS_INET6_STRUCTS
213 u[nserv].sin6.sin6_addr = in6addr_any;
214 u[nserv].sin6.sin6_family = AF_INET6;
215 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
216 #ifdef HAVE_SA_LEN
217 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
218 #endif
219 nserv++;
220 #endif
221 statp->nscount = 0;
222 statp->ndots = 1;
223 statp->pfcode = 0;
224 statp->_vcsock = -1;
225 statp->_flags = 0;
226 statp->qhook = NULL;
227 statp->rhook = NULL;
228 statp->_u._ext.nscount = 0;
229 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
230 if (statp->_u._ext.ext != NULL) {
231 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
232 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
233 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
234 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
235 statp->_u._ext.ext->reload_period = 2;
236 } else {
237 /*
238 * Historically res_init() rarely, if at all, failed.
239 * Examples and applications exist which do not check
240 * our return code. Furthermore several applications
241 * simply call us to get the systems domainname. So
242 * rather then immediately fail here we store the
243 * failure, which is returned later, in h_errno. And
244 * prevent the collection of 'nameserver' information
245 * by setting maxns to 0. Thus applications that fail
246 * to check our return code wont be able to make
247 * queries anyhow.
248 */
249 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
250 maxns = 0;
251 }
252 #ifdef RESOLVSORT
253 statp->nsort = 0;
254 #endif
255 res_setservers(statp, u, nserv);
256
257 #ifdef SOLARIS2
258 /*
259 * The old libresolv derived the defaultdomain from NIS/NIS+.
260 * We want to keep this behaviour
261 */
262 {
263 char buf[sizeof(statp->defdname)], *cp;
264 int ret;
265
266 if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 &&
267 (unsigned int)ret <= sizeof(buf)) {
268 if (buf[0] == '+')
269 buf[0] = '.';
270 cp = strchr(buf, '.');
271 cp = (cp == NULL) ? buf : (cp + 1);
272 strncpy(statp->defdname, cp,
273 sizeof(statp->defdname) - 1);
274 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
275 }
276 }
277 #endif /* SOLARIS2 */
278
279 /* Allow user to override the local domain definition */
280 if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) {
281 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
282 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
283 haveenv++;
284
285 /*
286 * Set search list to be blank-separated strings
287 * from rest of env value. Permits users of LOCALDOMAIN
288 * to still have a search list, and anyone to set the
289 * one that they want to use as an individual (even more
290 * important now that the rfc1535 stuff restricts searches)
291 */
292 cp = statp->defdname;
293 pp = statp->dnsrch;
294 *pp++ = cp;
295 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
296 if (*cp == '\n') /*%< silly backwards compat */
297 break;
298 else if (*cp == ' ' || *cp == '\t') {
299 *cp = 0;
300 n = 1;
301 } else if (n) {
302 *pp++ = cp;
303 n = 0;
304 havesearch = 1;
305 }
306 }
307 /* null terminate last domain if there are excess */
308 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
309 cp++;
310 *cp = '\0';
311 *pp++ = NULL;
312 }
313
314 #define MATCH(line, name) \
315 (!strncmp(line, name, sizeof(name) - 1) && \
316 (line[sizeof(name) - 1] == ' ' || \
317 line[sizeof(name) - 1] == '\t'))
318
319 nserv = 0;
320 if ((fp = fopen(_PATH_RESCONF, "re")) != NULL) {
321 struct stat sb;
322 struct timespec now;
323
324 if (statp->_u._ext.ext != NULL) {
325 if (_fstat(fileno(fp), &sb) == 0) {
326 statp->_u._ext.ext->conf_mtim = sb.st_mtim;
327 if (clock_gettime(CLOCK_MONOTONIC_FAST, &now) == 0) {
328 statp->_u._ext.ext->conf_stat = now.tv_sec;
329 }
330 }
331 }
332
333 /* read the config file */
334 while (fgets(buf, sizeof(buf), fp) != NULL) {
335 /* skip comments */
336 if (*buf == ';' || *buf == '#')
337 continue;
338 /* read default domain name */
339 if (MATCH(buf, "domain")) {
340 if (haveenv) /*%< skip if have from environ */
341 continue;
342 cp = buf + sizeof("domain") - 1;
343 while (*cp == ' ' || *cp == '\t')
344 cp++;
345 if ((*cp == '\0') || (*cp == '\n'))
346 continue;
347 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
348 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
349 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
350 *cp = '\0';
351 havesearch = 0;
352 continue;
353 }
354 /* set search list */
355 if (MATCH(buf, "search")) {
356 if (haveenv) /*%< skip if have from environ */
357 continue;
358 cp = buf + sizeof("search") - 1;
359 while (*cp == ' ' || *cp == '\t')
360 cp++;
361 if ((*cp == '\0') || (*cp == '\n'))
362 continue;
363 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
364 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
365 if ((cp = strchr(statp->defdname, '\n')) != NULL)
366 *cp = '\0';
367 /*
368 * Set search list to be blank-separated strings
369 * on rest of line.
370 */
371 cp = statp->defdname;
372 pp = statp->dnsrch;
373 *pp++ = cp;
374 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
375 if (*cp == ' ' || *cp == '\t') {
376 *cp = 0;
377 n = 1;
378 } else if (n) {
379 *pp++ = cp;
380 n = 0;
381 }
382 }
383 /* null terminate last domain if there are excess */
384 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
385 cp++;
386 *cp = '\0';
387 *pp++ = NULL;
388 havesearch = 1;
389 continue;
390 }
391 /* read nameservers to query */
392 if (MATCH(buf, "nameserver") && nserv < maxns) {
393 struct addrinfo hints, *ai;
394 char sbuf[NI_MAXSERV];
395 const size_t minsiz =
396 sizeof(statp->_u._ext.ext->nsaddrs[0]);
397
398 cp = buf + sizeof("nameserver") - 1;
399 while (*cp == ' ' || *cp == '\t')
400 cp++;
401 cp[strcspn(cp, ";# \t\n")] = '\0';
402 if ((*cp != '\0') && (*cp != '\n')) {
403 memset(&hints, 0, sizeof(hints));
404 hints.ai_family = PF_UNSPEC;
405 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
406 hints.ai_flags = AI_NUMERICHOST;
407 sprintf(sbuf, "%u", NAMESERVER_PORT);
408 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0) {
409 if (ai->ai_addrlen <= minsiz) {
410 if (statp->_u._ext.ext != NULL) {
411 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
412 ai->ai_addr, ai->ai_addrlen);
413 }
414 if (ai->ai_addrlen <=
415 sizeof(statp->nsaddr_list[nserv])) {
416 memcpy(&statp->nsaddr_list[nserv],
417 ai->ai_addr, ai->ai_addrlen);
418 } else
419 statp->nsaddr_list[nserv].sin_family = 0;
420 nserv++;
421 }
422 freeaddrinfo(ai);
423 }
424 }
425 continue;
426 }
427 #ifdef RESOLVSORT
428 if (MATCH(buf, "sortlist")) {
429 struct in_addr a;
430 struct in6_addr a6;
431 int m, i;
432 u_char *u;
433 struct __res_state_ext *ext = statp->_u._ext.ext;
434
435 cp = buf + sizeof("sortlist") - 1;
436 while (nsort < MAXRESOLVSORT) {
437 while (*cp == ' ' || *cp == '\t')
438 cp++;
439 if (*cp == '\0' || *cp == '\n' || *cp == ';')
440 break;
441 net = cp;
442 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
443 isascii(*cp) && !isspace((unsigned char)*cp))
444 cp++;
445 n = *cp;
446 *cp = 0;
447 if (inet_aton(net, &a)) {
448 statp->sort_list[nsort].addr = a;
449 if (ISSORTMASK(n)) {
450 *cp++ = n;
451 net = cp;
452 while (*cp && *cp != ';' &&
453 isascii(*cp) &&
454 !isspace((unsigned char)*cp))
455 cp++;
456 n = *cp;
457 *cp = 0;
458 if (inet_aton(net, &a)) {
459 statp->sort_list[nsort].mask = a.s_addr;
460 } else {
461 statp->sort_list[nsort].mask =
462 net_mask(statp->sort_list[nsort].addr);
463 }
464 } else {
465 statp->sort_list[nsort].mask =
466 net_mask(statp->sort_list[nsort].addr);
467 }
468 ext->sort_list[nsort].af = AF_INET;
469 ext->sort_list[nsort].addr.ina =
470 statp->sort_list[nsort].addr;
471 ext->sort_list[nsort].mask.ina.s_addr =
472 statp->sort_list[nsort].mask;
473 nsort++;
474 }
475 else if (inet_pton(AF_INET6, net, &a6) == 1) {
476
477 ext->sort_list[nsort].af = AF_INET6;
478 ext->sort_list[nsort].addr.in6a = a6;
479 u = (u_char *)&ext->sort_list[nsort].mask.in6a;
480 *cp++ = n;
481 net = cp;
482 while (*cp && *cp != ';' &&
483 isascii(*cp) && !isspace(*cp))
484 cp++;
485 m = n;
486 n = *cp;
487 *cp = 0;
488 switch (m) {
489 case '/':
490 m = atoi(net);
491 break;
492 case '&':
493 if (inet_pton(AF_INET6, net, u) == 1) {
494 m = -1;
495 break;
496 }
497 /*FALLTHROUGH*/
498 default:
499 m = sizeof(struct in6_addr) * CHAR_BIT;
500 break;
501 }
502 if (m >= 0) {
503 for (i = 0; i < sizeof(struct in6_addr); i++) {
504 if (m <= 0) {
505 *u = 0;
506 } else {
507 m -= CHAR_BIT;
508 *u = (u_char)~0;
509 if (m < 0)
510 *u <<= -m;
511 }
512 u++;
513 }
514 }
515 statp->sort_list[nsort].addr.s_addr =
516 (u_int32_t)0xffffffff;
517 statp->sort_list[nsort].mask =
518 (u_int32_t)0xffffffff;
519 nsort++;
520 }
521 *cp = n;
522 }
523 continue;
524 }
525 #endif
526 if (MATCH(buf, "options")) {
527 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
528 continue;
529 }
530 }
531 if (nserv > 0)
532 statp->nscount = nserv;
533 #ifdef RESOLVSORT
534 statp->nsort = nsort;
535 #endif
536 (void) fclose(fp);
537 }
538 /*
539 * Last chance to get a nameserver. This should not normally
540 * be necessary
541 */
542 #ifdef NO_RESOLV_CONF
543 if(nserv == 0)
544 nserv = get_nameservers(statp);
545 #endif
546
547 if (statp->defdname[0] == 0 &&
548 gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
549 (cp = strchr(buf, '.')) != NULL)
550 strcpy(statp->defdname, cp + 1);
551
552 /* find components of local domain that might be searched */
553 if (havesearch == 0) {
554 pp = statp->dnsrch;
555 *pp++ = statp->defdname;
556 *pp = NULL;
557
558 dots = 0;
559 for (cp = statp->defdname; *cp; cp++)
560 dots += (*cp == '.');
561
562 cp = statp->defdname;
563 while (pp < statp->dnsrch + MAXDFLSRCH) {
564 if (dots < LOCALDOMAINPARTS)
565 break;
566 cp = strchr(cp, '.') + 1; /*%< we know there is one */
567 *pp++ = cp;
568 dots--;
569 }
570 *pp = NULL;
571 #ifdef DEBUG
572 if (statp->options & RES_DEBUG) {
573 printf(";; res_init()... default dnsrch list:\n");
574 for (pp = statp->dnsrch; *pp; pp++)
575 printf(";;\t%s\n", *pp);
576 printf(";;\t..END..\n");
577 }
578 #endif
579 }
580
581 if (issetugid())
582 statp->options |= RES_NOALIASES;
583 else if ((cp = getenv("RES_OPTIONS")) != NULL)
584 res_setoptions(statp, cp, "env");
585 statp->options |= RES_INIT;
586 return (statp->res_h_errno);
587 }
588
589 static void
res_setoptions(res_state statp,const char * options,const char * source)590 res_setoptions(res_state statp, const char *options, const char *source)
591 {
592 const char *cp = options;
593 int i;
594 struct __res_state_ext *ext = statp->_u._ext.ext;
595
596 #ifdef DEBUG
597 if (statp->options & RES_DEBUG)
598 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
599 options, source);
600 #endif
601 while (*cp) {
602 /* skip leading and inner runs of spaces */
603 while (*cp == ' ' || *cp == '\t')
604 cp++;
605 /* search for and process individual options */
606 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
607 i = atoi(cp + sizeof("ndots:") - 1);
608 if (i <= RES_MAXNDOTS)
609 statp->ndots = i;
610 else
611 statp->ndots = RES_MAXNDOTS;
612 #ifdef DEBUG
613 if (statp->options & RES_DEBUG)
614 printf(";;\tndots=%d\n", statp->ndots);
615 #endif
616 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
617 i = atoi(cp + sizeof("timeout:") - 1);
618 if (i <= RES_MAXRETRANS)
619 statp->retrans = i;
620 else
621 statp->retrans = RES_MAXRETRANS;
622 #ifdef DEBUG
623 if (statp->options & RES_DEBUG)
624 printf(";;\ttimeout=%d\n", statp->retrans);
625 #endif
626 #ifdef SOLARIS2
627 } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) {
628 /*
629 * For backward compatibility, 'retrans' is
630 * supported as an alias for 'timeout', though
631 * without an imposed maximum.
632 */
633 statp->retrans = atoi(cp + sizeof("retrans:") - 1);
634 } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){
635 /*
636 * For backward compatibility, 'retry' is
637 * supported as an alias for 'attempts', though
638 * without an imposed maximum.
639 */
640 statp->retry = atoi(cp + sizeof("retry:") - 1);
641 #endif /* SOLARIS2 */
642 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
643 i = atoi(cp + sizeof("attempts:") - 1);
644 if (i <= RES_MAXRETRY)
645 statp->retry = i;
646 else
647 statp->retry = RES_MAXRETRY;
648 #ifdef DEBUG
649 if (statp->options & RES_DEBUG)
650 printf(";;\tattempts=%d\n", statp->retry);
651 #endif
652 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
653 #ifdef DEBUG
654 if (!(statp->options & RES_DEBUG)) {
655 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
656 options, source);
657 statp->options |= RES_DEBUG;
658 }
659 printf(";;\tdebug\n");
660 #endif
661 } else if (!strncmp(cp, "no_tld_query",
662 sizeof("no_tld_query") - 1) ||
663 !strncmp(cp, "no-tld-query",
664 sizeof("no-tld-query") - 1)) {
665 statp->options |= RES_NOTLDQUERY;
666 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
667 statp->options |= RES_USE_INET6;
668 } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) {
669 statp->options |= RES_INSECURE1;
670 } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) {
671 statp->options |= RES_INSECURE2;
672 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
673 statp->options |= RES_ROTATE;
674 } else if (!strncmp(cp, "usevc", sizeof("usevc") - 1)) {
675 statp->options |= RES_USEVC;
676 } else if (!strncmp(cp, "no-check-names",
677 sizeof("no-check-names") - 1)) {
678 statp->options |= RES_NOCHECKNAME;
679 } else if (!strncmp(cp, "reload-period:",
680 sizeof("reload-period:") - 1)) {
681 if (ext != NULL) {
682 ext->reload_period = (u_short)
683 atoi(cp + sizeof("reload-period:") - 1);
684 }
685 }
686 #ifdef RES_USE_EDNS0
687 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
688 statp->options |= RES_USE_EDNS0;
689 }
690 #endif
691 #ifndef _LIBC
692 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
693 statp->options |= RES_USE_DNAME;
694 }
695 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
696 if (ext == NULL)
697 goto skip;
698 cp += sizeof("nibble:") - 1;
699 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
700 strncpy(ext->nsuffix, cp, i);
701 ext->nsuffix[i] = '\0';
702 }
703 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
704 if (ext == NULL)
705 goto skip;
706 cp += sizeof("nibble2:") - 1;
707 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
708 strncpy(ext->nsuffix2, cp, i);
709 ext->nsuffix2[i] = '\0';
710 }
711 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
712 cp += sizeof("v6revmode:") - 1;
713 /* "nibble" and "bitstring" used to be valid */
714 if (!strncmp(cp, "single", sizeof("single") - 1)) {
715 statp->options |= RES_NO_NIBBLE2;
716 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
717 statp->options &=
718 ~RES_NO_NIBBLE2;
719 }
720 }
721 #endif
722 else {
723 /* XXX - print a warning here? */
724 }
725 #ifndef _LIBC
726 skip:
727 #endif
728 /* skip to next run of spaces */
729 while (*cp && *cp != ' ' && *cp != '\t')
730 cp++;
731 }
732 }
733
734 #ifdef RESOLVSORT
735 /* XXX - should really support CIDR which means explicit masks always. */
736 static u_int32_t
net_mask(struct in_addr in)737 net_mask(struct in_addr in) /*!< XXX - should really use system's version of this */
738 {
739 u_int32_t i = ntohl(in.s_addr);
740
741 if (IN_CLASSA(i))
742 return (htonl(IN_CLASSA_NET));
743 else if (IN_CLASSB(i))
744 return (htonl(IN_CLASSB_NET));
745 return (htonl(IN_CLASSC_NET));
746 }
747 #endif
748
749 static u_char srnd[16];
750
751 void
res_rndinit(res_state statp)752 res_rndinit(res_state statp)
753 {
754 struct timeval now;
755 u_int32_t u32;
756 u_int16_t u16;
757 u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd;
758
759 gettimeofday(&now, NULL);
760 u32 = now.tv_sec;
761 memcpy(rnd, &u32, 4);
762 u32 = now.tv_usec;
763 memcpy(rnd + 4, &u32, 4);
764 u32 += now.tv_sec;
765 memcpy(rnd + 8, &u32, 4);
766 u16 = getpid();
767 memcpy(rnd + 12, &u16, 2);
768 }
769
770 u_int
res_nrandomid(res_state statp)771 res_nrandomid(res_state statp) {
772 struct timeval now;
773 u_int16_t u16;
774 MD5_CTX ctx;
775 u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd;
776
777 gettimeofday(&now, NULL);
778 u16 = (u_int16_t) (now.tv_sec ^ now.tv_usec);
779 memcpy(rnd + 14, &u16, 2);
780 #ifndef HAVE_MD5
781 MD5_Init(&ctx);
782 MD5_Update(&ctx, rnd, 16);
783 MD5_Final(rnd, &ctx);
784 #else
785 MD5Init(&ctx);
786 MD5Update(&ctx, rnd, 16);
787 MD5Final(rnd, &ctx);
788 #endif
789 memcpy(&u16, rnd + 14, 2);
790 return ((u_int) u16);
791 }
792
793 /*%
794 * This routine is for closing the socket if a virtual circuit is used and
795 * the program wants to close it. This provides support for endhostent()
796 * which expects to close the socket.
797 *
798 * This routine is not expected to be user visible.
799 */
800 void
res_nclose(res_state statp)801 res_nclose(res_state statp) {
802 int ns;
803
804 if (statp->_vcsock >= 0) {
805 (void) _close(statp->_vcsock);
806 statp->_vcsock = -1;
807 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
808 }
809 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
810 if (statp->_u._ext.nssocks[ns] != -1) {
811 (void) _close(statp->_u._ext.nssocks[ns]);
812 statp->_u._ext.nssocks[ns] = -1;
813 }
814 }
815 }
816
817 void
res_ndestroy(res_state statp)818 res_ndestroy(res_state statp) {
819 res_nclose(statp);
820 if (statp->_u._ext.ext != NULL) {
821 free(statp->_u._ext.ext);
822 statp->_u._ext.ext = NULL;
823 }
824 if (statp->_rnd != NULL) {
825 free(statp->_rnd);
826 statp->_rnd = NULL;
827 }
828 statp->options &= ~RES_INIT;
829 }
830
831 #ifndef _LIBC
832 const char *
res_get_nibblesuffix(res_state statp)833 res_get_nibblesuffix(res_state statp) {
834 if (statp->_u._ext.ext)
835 return (statp->_u._ext.ext->nsuffix);
836 return ("ip6.arpa");
837 }
838
839 const char *
res_get_nibblesuffix2(res_state statp)840 res_get_nibblesuffix2(res_state statp) {
841 if (statp->_u._ext.ext)
842 return (statp->_u._ext.ext->nsuffix2);
843 return ("ip6.int");
844 }
845 #endif
846
847 void
res_setservers(res_state statp,const union res_sockaddr_union * set,int cnt)848 res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
849 int i, nserv;
850 size_t size;
851
852 /* close open servers */
853 res_nclose(statp);
854
855 /* cause rtt times to be forgotten */
856 statp->_u._ext.nscount = 0;
857
858 nserv = 0;
859 for (i = 0; i < cnt && nserv < MAXNS; i++) {
860 switch (set->sin.sin_family) {
861 case AF_INET:
862 size = sizeof(set->sin);
863 if (statp->_u._ext.ext)
864 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
865 &set->sin, size);
866 if (size <= sizeof(statp->nsaddr_list[nserv]))
867 memcpy(&statp->nsaddr_list[nserv],
868 &set->sin, size);
869 else
870 statp->nsaddr_list[nserv].sin_family = 0;
871 nserv++;
872 break;
873
874 #ifdef HAS_INET6_STRUCTS
875 case AF_INET6:
876 size = sizeof(set->sin6);
877 if (statp->_u._ext.ext)
878 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
879 &set->sin6, size);
880 if (size <= sizeof(statp->nsaddr_list[nserv]))
881 memcpy(&statp->nsaddr_list[nserv],
882 &set->sin6, size);
883 else
884 statp->nsaddr_list[nserv].sin_family = 0;
885 nserv++;
886 break;
887 #endif
888
889 default:
890 break;
891 }
892 set++;
893 }
894 statp->nscount = nserv;
895
896 }
897
898 int
res_getservers(res_state statp,union res_sockaddr_union * set,int cnt)899 res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
900 int i;
901 size_t size;
902 u_int16_t family;
903
904 for (i = 0; i < statp->nscount && i < cnt; i++) {
905 if (statp->_u._ext.ext)
906 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
907 else
908 family = statp->nsaddr_list[i].sin_family;
909
910 switch (family) {
911 case AF_INET:
912 size = sizeof(set->sin);
913 if (statp->_u._ext.ext)
914 memcpy(&set->sin,
915 &statp->_u._ext.ext->nsaddrs[i],
916 size);
917 else
918 memcpy(&set->sin, &statp->nsaddr_list[i],
919 size);
920 break;
921
922 #ifdef HAS_INET6_STRUCTS
923 case AF_INET6:
924 size = sizeof(set->sin6);
925 if (statp->_u._ext.ext)
926 memcpy(&set->sin6,
927 &statp->_u._ext.ext->nsaddrs[i],
928 size);
929 else
930 memcpy(&set->sin6, &statp->nsaddr_list[i],
931 size);
932 break;
933 #endif
934
935 default:
936 set->sin.sin_family = 0;
937 break;
938 }
939 set++;
940 }
941 return (statp->nscount);
942 }
943
944 /*! \file */
945