summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/res_init.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/res_init.c')
-rw-r--r--src/lib/libc/net/res_init.c435
1 files changed, 340 insertions, 95 deletions
diff --git a/src/lib/libc/net/res_init.c b/src/lib/libc/net/res_init.c
index 33cc8d39f1..c55c7763a4 100644
--- a/src/lib/libc/net/res_init.c
+++ b/src/lib/libc/net/res_init.c
@@ -1,9 +1,11 @@
1/* $NetBSD: res_init.c,v 1.8 1995/06/03 22:33:36 mycroft Exp $ */ 1/* $OpenBSD: res_init.c,v 1.27 2002/07/25 21:13:45 deraadt Exp $ */
2 2
3/*- 3/*
4 * ++Copyright++ 1985, 1989, 1993
5 * -
4 * Copyright (c) 1985, 1989, 1993 6 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved. 7 * The Regents of the University of California. All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
9 * are met: 11 * are met:
@@ -14,12 +16,12 @@
14 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 17 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 18 * must display the following acknowledgement:
17 * This product includes software developed by the University of 19 * This product includes software developed by the University of
18 * California, Berkeley and its contributors. 20 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors 21 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 22 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 23 * without specific prior written permission.
22 * 24 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -53,80 +55,167 @@
53 * --Copyright-- 55 * --Copyright--
54 */ 56 */
55 57
58#ifndef INET6
59#define INET6
60#endif
61
56#if defined(LIBC_SCCS) && !defined(lint) 62#if defined(LIBC_SCCS) && !defined(lint)
57#if 0 63#if 0
58static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; 64static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
59static char rcsid[] = "$Id: res_init.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; 65static char rcsid[] = "$From: res_init.c,v 8.7 1996/09/28 06:51:07 vixie Exp $";
60#else 66#else
61static char rcsid[] = "$NetBSD: res_init.c,v 1.8 1995/06/03 22:33:36 mycroft Exp $"; 67static char rcsid[] = "$OpenBSD: res_init.c,v 1.27 2002/07/25 21:13:45 deraadt Exp $";
62#endif 68#endif
63#endif /* LIBC_SCCS and not lint */ 69#endif /* LIBC_SCCS and not lint */
64 70
71#include <sys/types.h>
65#include <sys/param.h> 72#include <sys/param.h>
66#include <sys/socket.h> 73#include <sys/socket.h>
74#include <sys/time.h>
67#include <netinet/in.h> 75#include <netinet/in.h>
68#include <arpa/inet.h> 76#include <arpa/inet.h>
69#include <arpa/nameser.h> 77#include <arpa/nameser.h>
78
79#include <stdio.h>
80#include <ctype.h>
70#include <resolv.h> 81#include <resolv.h>
71#include <unistd.h> 82#include <unistd.h>
72#include <stdio.h>
73#include <stdlib.h> 83#include <stdlib.h>
74#include <string.h> 84#include <string.h>
85#ifdef INET6
86#include <netdb.h>
87#endif /* INET6 */
88
89/*-------------------------------------- info about "sortlist" --------------
90 * Marc Majka 1994/04/16
91 * Allan Nathanson 1994/10/29 (BIND 4.9.3.x)
92 *
93 * NetInfo resolver configuration directory support.
94 *
95 * Allow a NetInfo directory to be created in the hierarchy which
96 * contains the same information as the resolver configuration file.
97 *
98 * - The local domain name is stored as the value of the "domain" property.
99 * - The Internet address(es) of the name server(s) are stored as values
100 * of the "nameserver" property.
101 * - The name server addresses are stored as values of the "nameserver"
102 * property.
103 * - The search list for host-name lookup is stored as values of the
104 * "search" property.
105 * - The sortlist comprised of IP address netmask pairs are stored as
106 * values of the "sortlist" property. The IP address and optional netmask
107 * should be separated by a slash (/) or ampersand (&) character.
108 * - Internal resolver variables can be set from the value of the "options"
109 * property.
110 */
111
112static void res_setoptions(char *, char *);
75 113
76static void res_setoptions __P((char *, char *)); 114#ifdef RESOLVSORT
77static u_int32_t net_mask __P((struct in_addr)); 115static const char sort_mask[] = "/&";
116#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
117static u_int32_t net_mask(struct in_addr);
118#endif
78 119
79/* 120/*
80 * Resolver state default settings 121 * Resolver state default settings.
81 */ 122 */
82 123
83struct __res_state _res = { 124struct __res_state _res
84 RES_TIMEOUT, /* retransmition time interval */ 125# if defined(__BIND_RES_TEXT)
85 4, /* number of times to retransmit */ 126 = { RES_TIMEOUT, } /* Motorola, et al. */
86 RES_DEFAULT, /* options flags */ 127# endif
87 1, /* number of name servers */ 128 ;
88}; 129#ifdef INET6
130struct __res_state_ext _res_ext;
131#endif /* INET6 */
89 132
90/* 133/*
91 * Set up default settings. If the configuration file exist, the values 134 * Set up default settings. If the configuration file exist, the values
92 * there will have precedence. Otherwise, the server address is set to 135 * there will have precedence. Otherwise, the server address is set to
93 * INADDR_ANY and the default domain name comes from the gethostname(). 136 * INADDR_ANY and the default domain name comes from the gethostname().
94 * 137 *
95 * The configuration file should only be used if you want to redefine your 138 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
96 * domain or run without a server on your machine. 139 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
140 * since it was noted that INADDR_ANY actually meant ``the first interface
141 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
142 * it had to be "up" in order for you to reach your own name server. It
143 * was later decided that since the recommended practice is to always
144 * install local static routes through 127.0.0.1 for all your network
145 * interfaces, that we could solve this problem without a code change.
146 *
147 * The configuration file should always be used, since it is the only way
148 * to specify a default domain. If you are running a server on your local
149 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
150 * in the configuration file.
97 * 151 *
98 * Return 0 if completes successfully, -1 on error 152 * Return 0 if completes successfully, -1 on error
99 */ 153 */
154int
100res_init() 155res_init()
101{ 156{
102 register FILE *fp; 157 register FILE *fp;
103 register char *cp, **pp, *net; 158 register char *cp, **pp;
104 register int n; 159 register int n;
105 char buf[BUFSIZ], buf2[BUFSIZ]; 160 char buf[BUFSIZ];
106 int nserv = 0; /* number of nameserver records read from file */ 161 int nserv = 0; /* number of nameserver records read from file */
107 int haveenv = 0; 162 int haveenv = 0;
108 int havesearch = 0; 163 int havesearch = 0;
164 size_t len;
165#ifdef RESOLVSORT
109 int nsort = 0; 166 int nsort = 0;
110 u_long mask; 167 char *net;
168#endif
169#ifndef RFC1535
170 int dots;
171#endif
172
173 /*
174 * These three fields used to be statically initialized. This made
175 * it hard to use this code in a shared library. It is necessary,
176 * now that we're doing dynamic initialization here, that we preserve
177 * the old semantics: if an application modifies one of these three
178 * fields of _res before res_init() is called, res_init() will not
179 * alter them. Of course, if an application is setting them to
180 * _zero_ before calling res_init(), hoping to override what used
181 * to be the static default, we can't detect it and unexpected results
182 * will follow. Zero for any of these fields would make no sense,
183 * so one can safely assume that the applications were already getting
184 * unexpected results.
185 *
186 * _res.options is tricky since some apps were known to diddle the bits
187 * before res_init() was first called. We can't replicate that semantic
188 * with dynamic initialization (they may have turned bits off that are
189 * set in RES_DEFAULT). Our solution is to declare such applications
190 * "broken". They could fool us by setting RES_INIT but none do (yet).
191 */
192 if (!_res.retrans)
193 _res.retrans = RES_TIMEOUT;
194 if (!_res.retry)
195 _res.retry = 4;
196 if (!(_res.options & RES_INIT))
197 _res.options = RES_DEFAULT;
111 198
112 _res.nsaddr.sin_len = sizeof(struct sockaddr_in);
113 _res.nsaddr.sin_family = AF_INET;
114 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
115#ifdef USELOOPBACK 199#ifdef USELOOPBACK
116 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); 200 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
117#else 201#else
118 _res.nsaddr.sin_addr.s_addr = INADDR_ANY; 202 _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
119#endif 203#endif
204 _res.nsaddr.sin_family = AF_INET;
205 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
206 _res.nsaddr.sin_len = sizeof(struct sockaddr_in);
207#ifdef INET6
208 if (sizeof(_res_ext.nsaddr) >= _res.nsaddr.sin_len)
209 memcpy(&_res_ext.nsaddr, &_res.nsaddr, _res.nsaddr.sin_len);
210#endif
120 _res.nscount = 1; 211 _res.nscount = 1;
121 _res.ndots = 1; 212 _res.ndots = 1;
122 _res.pfcode = 0; 213 _res.pfcode = 0;
123 strncpy(_res.lookups, "f", sizeof _res.lookups); 214 strlcpy(_res.lookups, "f", sizeof _res.lookups);
124 215
125 /* Allow user to override the local domain definition */ 216 /* Allow user to override the local domain definition */
126 if ((cp = getenv("LOCALDOMAIN")) != NULL) { 217 if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) {
127 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1); 218 strlcpy(_res.defdname, cp, sizeof(_res.defdname));
128 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
129 *cp = '\0';
130 haveenv++; 219 haveenv++;
131 220
132 /* 221 /*
@@ -140,7 +229,7 @@ res_init()
140 pp = _res.dnsrch; 229 pp = _res.dnsrch;
141 *pp++ = cp; 230 *pp++ = cp;
142 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { 231 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
143 if (*cp == '\n') /* silly backwards compat */ 232 if (*cp == '\n') /* silly backwards compat */
144 break; 233 break;
145 else if (*cp == ' ' || *cp == '\t') { 234 else if (*cp == ' ' || *cp == '\t') {
146 *cp = 0; 235 *cp = 0;
@@ -158,16 +247,29 @@ res_init()
158 *pp++ = 0; 247 *pp++ = 0;
159 } 248 }
160 249
250#define MATCH(line, name) \
251 (!strncmp(line, name, sizeof(name) - 1) && \
252 (line[sizeof(name) - 1] == ' ' || \
253 line[sizeof(name) - 1] == '\t'))
254
161 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { 255 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
162 strncpy(_res.lookups, "bf", sizeof _res.lookups); 256 strlcpy(_res.lookups, "bf", sizeof _res.lookups);
163 257
164 /* read the config file */ 258 /* read the config file */
165 while (fgets(buf, sizeof(buf), fp) != NULL) { 259 buf[0] = '\0';
260 while ((cp = fgetln(fp, &len)) != NULL) {
261 /* skip lines that are too long or zero length */
262 if (len >= sizeof(buf) || len == 0)
263 continue;
264 (void)memcpy(buf, cp, len);
265 buf[len] = '\0';
166 /* skip comments */ 266 /* skip comments */
167 if ((*buf == ';') || (*buf == '#')) 267 if ((cp = strpbrk(buf, ";#")) != NULL)
268 *cp = '\0';
269 if (buf[0] == '\0')
168 continue; 270 continue;
169 /* read default domain name */ 271 /* read default domain name */
170 if (!strncmp(buf, "domain", sizeof("domain") - 1)) { 272 if (MATCH(buf, "domain")) {
171 if (haveenv) /* skip if have from environ */ 273 if (haveenv) /* skip if have from environ */
172 continue; 274 continue;
173 cp = buf + sizeof("domain") - 1; 275 cp = buf + sizeof("domain") - 1;
@@ -175,15 +277,14 @@ res_init()
175 cp++; 277 cp++;
176 if ((*cp == '\0') || (*cp == '\n')) 278 if ((*cp == '\0') || (*cp == '\n'))
177 continue; 279 continue;
178 (void)strncpy(_res.defdname, cp, 280 strlcpy(_res.defdname, cp, sizeof(_res.defdname));
179 sizeof(_res.defdname) - 1);
180 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL) 281 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
181 *cp = '\0'; 282 *cp = '\0';
182 havesearch = 0; 283 havesearch = 0;
183 continue; 284 continue;
184 } 285 }
185 /* lookup types */ 286 /* lookup types */
186 if (!strncmp(buf, "lookup", sizeof("lookup") -1)) { 287 if (MATCH(buf, "lookup")) {
187 char *sp = NULL; 288 char *sp = NULL;
188 289
189 bzero(_res.lookups, sizeof _res.lookups); 290 bzero(_res.lookups, sizeof _res.lookups);
@@ -210,7 +311,7 @@ res_init()
210 continue; 311 continue;
211 } 312 }
212 /* set search list */ 313 /* set search list */
213 if (!strncmp(buf, "search", sizeof("search") - 1)) { 314 if (MATCH(buf, "search")) {
214 if (haveenv) /* skip if have from environ */ 315 if (haveenv) /* skip if have from environ */
215 continue; 316 continue;
216 cp = buf + sizeof("search") - 1; 317 cp = buf + sizeof("search") - 1;
@@ -218,8 +319,7 @@ res_init()
218 cp++; 319 cp++;
219 if ((*cp == '\0') || (*cp == '\n')) 320 if ((*cp == '\0') || (*cp == '\n'))
220 continue; 321 continue;
221 (void)strncpy(_res.defdname, cp, 322 strlcpy(_res.defdname, cp, sizeof(_res.defdname));
222 sizeof(_res.defdname) - 1);
223 if ((cp = strchr(_res.defdname, '\n')) != NULL) 323 if ((cp = strchr(_res.defdname, '\n')) != NULL)
224 *cp = '\0'; 324 *cp = '\0';
225 /* 325 /*
@@ -247,122 +347,256 @@ res_init()
247 continue; 347 continue;
248 } 348 }
249 /* read nameservers to query */ 349 /* read nameservers to query */
250 if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) && 350 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
251 nserv < MAXNS) { 351#ifdef INET6
252 struct in_addr a; 352 char *q;
353 struct addrinfo hints, *res;
354 char pbuf[NI_MAXSERV];
355#else
356 struct in_addr a;
357#endif /* INET6 */
253 358
254 cp = buf + sizeof("nameserver") - 1; 359 cp = buf + sizeof("nameserver") - 1;
255 while (*cp == ' ' || *cp == '\t') 360 while (*cp == ' ' || *cp == '\t')
256 cp++; 361 cp++;
362#ifdef INET6
363 if ((*cp == '\0') || (*cp == '\n'))
364 continue;
365 for (q = cp; *q; q++) {
366 if (isspace(*q)) {
367 *q = '\0';
368 break;
369 }
370 }
371 memset(&hints, 0, sizeof(hints));
372 hints.ai_flags = AI_NUMERICHOST;
373 hints.ai_socktype = SOCK_DGRAM;
374 snprintf(pbuf, sizeof(pbuf), "%u", NAMESERVER_PORT);
375 res = NULL;
376 if (getaddrinfo(cp, pbuf, &hints, &res) == 0 &&
377 res->ai_next == NULL) {
378 if (res->ai_addrlen <= sizeof(_res_ext.nsaddr_list[nserv])) {
379 memcpy(&_res_ext.nsaddr_list[nserv], res->ai_addr,
380 res->ai_addrlen);
381 } else {
382 memset(&_res_ext.nsaddr_list[nserv], 0,
383 sizeof(_res_ext.nsaddr_list[nserv]));
384 }
385 if (res->ai_addrlen <= sizeof(_res.nsaddr_list[nserv])) {
386 memcpy(&_res.nsaddr_list[nserv], res->ai_addr,
387 res->ai_addrlen);
388 } else {
389 memset(&_res.nsaddr_list[nserv], 0,
390 sizeof(_res.nsaddr_list[nserv]));
391 }
392 nserv++;
393 }
394 if (res)
395 freeaddrinfo(res);
396#else /* INET6 */
257 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) { 397 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
258 _res.nsaddr_list[nserv].sin_len = sizeof(struct sockaddr_in); 398 _res.nsaddr_list[nserv].sin_addr = a;
259 _res.nsaddr_list[nserv].sin_family = AF_INET; 399 _res.nsaddr_list[nserv].sin_family = AF_INET;
260 _res.nsaddr_list[nserv].sin_port = 400 _res.nsaddr_list[nserv].sin_port =
261 htons(NAMESERVER_PORT); 401 htons(NAMESERVER_PORT);
262 _res.nsaddr_list[nserv].sin_addr = a; 402 _res.nsaddr_list[nserv].sin_len =
403 sizeof(struct sockaddr_in);
263 nserv++; 404 nserv++;
264 } 405 }
406#endif /* INET6 */
265 continue; 407 continue;
266 } 408 }
267 if (!strncmp(buf, "sortlist", sizeof("sortlist") - 1)) { 409#ifdef RESOLVSORT
410 if (MATCH(buf, "sortlist")) {
268 struct in_addr a; 411 struct in_addr a;
412#ifdef INET6
413 struct in6_addr a6;
414 int m, i;
415 u_char *u;
416#endif /* INET6 */
269 417
270 cp = buf + sizeof("sortlist") - 1; 418 cp = buf + sizeof("sortlist") - 1;
271 while (*cp == ' ' || *cp == '\t') 419 while (nsort < MAXRESOLVSORT) {
272 cp++; 420 while (*cp == ' ' || *cp == '\t')
273 while (sscanf(cp,"%[0-9./]s", buf2) && nsort < MAXRESOLVSORT) { 421 cp++;
274 if (net = strchr(buf2, '/')) 422 if (*cp == '\0' || *cp == '\n' || *cp == ';')
275 *net = '\0'; 423 break;
276 if (inet_aton(buf2, &a)) { 424 net = cp;
425 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
426 isascii(*cp) && !isspace(*cp))
427 cp++;
428 n = *cp;
429 *cp = 0;
430 if (inet_aton(net, &a)) {
277 _res.sort_list[nsort].addr = a; 431 _res.sort_list[nsort].addr = a;
278 if (net && inet_aton(net+1, &a)) { 432 if (ISSORTMASK(n)) {
279 _res.sort_list[nsort].mask = a.s_addr; 433 *cp++ = n;
280 } else { 434 net = cp;
281 _res.sort_list[nsort].mask = 435 while (*cp && *cp != ';' &&
436 isascii(*cp) && !isspace(*cp))
437 cp++;
438 n = *cp;
439 *cp = 0;
440 if (inet_aton(net, &a)) {
441 _res.sort_list[nsort].mask = a.s_addr;
442 } else {
443 _res.sort_list[nsort].mask =
282 net_mask(_res.sort_list[nsort].addr); 444 net_mask(_res.sort_list[nsort].addr);
445 }
446 } else {
447 _res.sort_list[nsort].mask =
448 net_mask(_res.sort_list[nsort].addr);
283 } 449 }
450#ifdef INET6
451 _res_ext.sort_list[nsort].af = AF_INET;
452 _res_ext.sort_list[nsort].addr.ina =
453 _res.sort_list[nsort].addr;
454 _res_ext.sort_list[nsort].mask.ina.s_addr =
455 _res.sort_list[nsort].mask;
456#endif /* INET6 */
284 nsort++; 457 nsort++;
285 } 458 }
286 if (net) 459#ifdef INET6
287 *net = '/'; 460 else if (inet_pton(AF_INET6, net, &a6) == 1) {
288 cp += strlen(buf2); 461 _res_ext.sort_list[nsort].af = AF_INET6;
289 while (*cp == ' ' || *cp == '\t') 462 _res_ext.sort_list[nsort].addr.in6a = a6;
290 cp++; 463 u = (u_char *)&_res_ext.sort_list[nsort].mask.in6a;
464 *cp++ = n;
465 net = cp;
466 while (*cp && *cp != ';' &&
467 isascii(*cp) && !isspace(*cp))
468 cp++;
469 m = n;
470 n = *cp;
471 *cp = 0;
472 switch (m) {
473 case '/':
474 m = atoi(net);
475 break;
476 case '&':
477 if (inet_pton(AF_INET6, net, u) == 1) {
478 m = -1;
479 break;
480 }
481 /*FALLTHRU*/
482 default:
483 m = sizeof(struct in6_addr) * NBBY;
484 break;
485 }
486 if (m >= 0) {
487 for (i = 0; i < sizeof(struct in6_addr); i++) {
488 if (m <= 0) {
489 *u = 0;
490 } else {
491 m -= NBBY;
492 *u = (u_char)~0;
493 if (m < 0)
494 *u <<= -m;
495 }
496 u++;
497 }
498 }
499 nsort++;
500 }
501#endif /* INET6 */
502 *cp = n;
291 } 503 }
292 continue; 504 continue;
293 } 505 }
294 if (!strncmp(buf, "options", sizeof("options") -1)) { 506#endif
507 if (MATCH(buf, "options")) {
295 res_setoptions(buf + sizeof("options") - 1, "conf"); 508 res_setoptions(buf + sizeof("options") - 1, "conf");
296 continue; 509 continue;
297 } 510 }
298 } 511 }
299 if (nserv > 1) 512 if (nserv > 1)
300 _res.nscount = nserv; 513 _res.nscount = nserv;
514#ifdef RESOLVSORT
301 _res.nsort = nsort; 515 _res.nsort = nsort;
516#endif
302 (void) fclose(fp); 517 (void) fclose(fp);
303 } 518 }
304 if (_res.defdname[0] == 0) { 519 if (_res.defdname[0] == 0 &&
305 if (gethostname(buf, sizeof(_res.defdname) - 1) == 0 && 520 gethostname(buf, sizeof(_res.defdname) - 1) == 0 &&
306 (cp = strchr(buf, '.'))) 521 (cp = strchr(buf, '.')) != NULL)
307 (void)strcpy(_res.defdname, cp + 1); 522 {
523 strlcpy(_res.defdname, cp + 1,
524 sizeof(_res.defdname));
308 } 525 }
309 526
310 /* find components of local domain that might be searched */ 527 /* find components of local domain that might be searched */
311 if (havesearch == 0) { 528 if (havesearch == 0) {
312 pp = _res.dnsrch; 529 pp = _res.dnsrch;
313 *pp++ = _res.defdname; 530 *pp++ = _res.defdname;
314#ifndef SEARCH_LOCAL_DOMAINS
315 *pp = NULL; 531 *pp = NULL;
316#else 532
317 for (cp = _res.defdname, n = 0; *cp; cp++) 533#ifndef RFC1535
318 if (*cp == '.') 534 dots = 0;
319 n++; 535 for (cp = _res.defdname; *cp; cp++)
536 dots += (*cp == '.');
537
320 cp = _res.defdname; 538 cp = _res.defdname;
321 for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; 539 while (pp < _res.dnsrch + MAXDFLSRCH) {
322 n--) { 540 if (dots < LOCALDOMAINPARTS)
323 cp = strchr(cp, '.'); 541 break;
324 *pp++ = ++cp; 542 cp = strchr(cp, '.') + 1; /* we know there is one */
543 *pp++ = cp;
544 dots--;
325 } 545 }
326 *pp++ = 0; 546 *pp = NULL;
327#endif 547#ifdef DEBUG
548 if (_res.options & RES_DEBUG) {
549 printf(";; res_init()... default dnsrch list:\n");
550 for (pp = _res.dnsrch; *pp; pp++)
551 printf(";;\t%s\n", *pp);
552 printf(";;\t..END..\n");
553 }
554#endif /* DEBUG */
555#endif /* !RFC1535 */
328 } 556 }
329 557
330 if ((cp = getenv("RES_OPTIONS")) != NULL) 558 if (issetugid())
559 _res.options |= RES_NOALIASES;
560 else if ((cp = getenv("RES_OPTIONS")) != NULL)
331 res_setoptions(cp, "env"); 561 res_setoptions(cp, "env");
332 _res.options |= RES_INIT; 562 _res.options |= RES_INIT;
333 return (0); 563 return (0);
334} 564}
335 565
566/* ARGSUSED */
336static void 567static void
337res_setoptions(options, source) 568res_setoptions(options, source)
338 char *options, *source; 569 char *options, *source;
339{ 570{
340 char *cp = options; 571 char *cp = options;
341 int i; 572 char *endp;
573 long l;
342 574
343#ifdef DEBUG 575#ifdef DEBUG
344 if (_res.options & RES_DEBUG) { 576 if (_res.options & RES_DEBUG)
345 printf(";; res_setoptions(\"%s\", \"%s\")...\n", 577 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
346 options, source); 578 options, source);
347 }
348#endif 579#endif
349 while (*cp) { 580 while (*cp) {
350 /* skip leading and inner runs of spaces */ 581 /* skip leading and inner runs of spaces */
351 while (*cp == ' ' || *cp == '\t') 582 while (*cp == ' ' || *cp == '\t')
352 cp++; 583 cp++;
353 /* search for and process individual options */ 584 /* search for and process individual options */
354 if (!strncmp(cp, "ndots:", sizeof("ndots:")-1)) { 585 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
355 i = atoi(cp + sizeof("ndots:") - 1); 586 char *p = cp + sizeof("ndots:") - 1;
356 if (i <= RES_MAXNDOTS) 587 l = strtol(p, &endp, 10);
357 _res.ndots = i; 588 if (l >= 0 && endp != p &&
358 else 589 (*endp = '\0' || isspace(*endp))) {
359 _res.ndots = RES_MAXNDOTS; 590 if (l <= RES_MAXNDOTS)
591 _res.ndots = l;
592 else
593 _res.ndots = RES_MAXNDOTS;
360#ifdef DEBUG 594#ifdef DEBUG
361 if (_res.options & RES_DEBUG) { 595 if (_res.options & RES_DEBUG)
362 printf(";;\tndots=%d\n", _res.ndots); 596 printf(";;\tndots=%u\n", _res.ndots);
363 }
364#endif 597#endif
365 } else if (!strncmp(cp, "debug", sizeof("debug")-1)) { 598 }
599 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
366#ifdef DEBUG 600#ifdef DEBUG
367 if (!(_res.options & RES_DEBUG)) { 601 if (!(_res.options & RES_DEBUG)) {
368 printf(";; res_setoptions(\"%s\", \"%s\")..\n", 602 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
@@ -371,6 +605,14 @@ res_setoptions(options, source)
371 } 605 }
372 printf(";;\tdebug\n"); 606 printf(";;\tdebug\n");
373#endif 607#endif
608 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
609 _res.options |= RES_USE_INET6;
610 } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) {
611 _res.options |= RES_INSECURE1;
612 } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) {
613 _res.options |= RES_INSECURE2;
614 } else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
615 _res.options |= RES_USE_EDNS0;
374 } else { 616 } else {
375 /* XXX - print a warning here? */ 617 /* XXX - print a warning here? */
376 } 618 }
@@ -380,6 +622,8 @@ res_setoptions(options, source)
380 } 622 }
381} 623}
382 624
625#ifdef RESOLVSORT
626/* XXX - should really support CIDR which means explicit masks always. */
383static u_int32_t 627static u_int32_t
384net_mask(in) /* XXX - should really use system's version of this */ 628net_mask(in) /* XXX - should really use system's version of this */
385 struct in_addr in; 629 struct in_addr in;
@@ -388,7 +632,8 @@ net_mask(in) /* XXX - should really use system's version of this */
388 632
389 if (IN_CLASSA(i)) 633 if (IN_CLASSA(i))
390 return (htonl(IN_CLASSA_NET)); 634 return (htonl(IN_CLASSA_NET));
391 if (IN_CLASSB(i)) 635 else if (IN_CLASSB(i))
392 return (htonl(IN_CLASSB_NET)); 636 return (htonl(IN_CLASSB_NET));
393 return (htonl(IN_CLASSC_NET)); 637 return (htonl(IN_CLASSC_NET));
394} 638}
639#endif