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