| 1 |
/* dnsmasq is Copyright (c) 2000-2026 Simon Kelley |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License as published by |
| 5 |
the Free Software Foundation; version 2 dated June, 1991, or |
| 6 |
(at your option) version 3 dated 29 June, 2007. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, |
| 9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License |
| 14 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "dnsmasq.h" |
| 18 |
|
| 19 |
static int order(char *qdomain, size_t qlen, struct server *serv); |
| 20 |
static int order_qsort(const void *a, const void *b); |
| 21 |
static int order_servers(struct server *s, struct server *s2); |
| 22 |
|
| 23 |
/* If the server is USE_RESOLV or LITERAL_ADDRESS, it lives on the local_domains chain. */ |
| 24 |
#define SERV_IS_LOCAL (SERV_USE_RESOLV | SERV_LITERAL_ADDRESS) |
| 25 |
|
| 26 |
void build_server_array(void) |
| 27 |
{ |
| 28 |
struct server *serv; |
| 29 |
int count = 0; |
| 30 |
|
| 31 |
for (serv = daemon->servers; serv; serv = serv->next) |
| 32 |
#ifdef HAVE_LOOP |
| 33 |
if (!(serv->flags & SERV_LOOP)) |
| 34 |
#endif |
| 35 |
{ |
| 36 |
count++; |
| 37 |
if (serv->flags & SERV_WILDCARD) |
| 38 |
daemon->server_has_wildcard = 1; |
| 39 |
} |
| 40 |
|
| 41 |
for (serv = daemon->local_domains; serv; serv = serv->next) |
| 42 |
{ |
| 43 |
count++; |
| 44 |
if (serv->flags & SERV_WILDCARD) |
| 45 |
daemon->server_has_wildcard = 1; |
| 46 |
} |
| 47 |
|
| 48 |
daemon->serverarraysz = count; |
| 49 |
|
| 50 |
if (count > daemon->serverarrayhwm) |
| 51 |
{ |
| 52 |
struct server **new; |
| 53 |
|
| 54 |
count += 10; /* A few extra without re-allocating. */ |
| 55 |
|
| 56 |
if ((new = whine_malloc(count * sizeof(struct server *)))) |
| 57 |
{ |
| 58 |
if (daemon->serverarray) |
| 59 |
free(daemon->serverarray); |
| 60 |
|
| 61 |
daemon->serverarray = new; |
| 62 |
daemon->serverarrayhwm = count; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
count = 0; |
| 67 |
|
| 68 |
for (serv = daemon->servers; serv; serv = serv->next) |
| 69 |
#ifdef HAVE_LOOP |
| 70 |
if (!(serv->flags & SERV_LOOP)) |
| 71 |
#endif |
| 72 |
{ |
| 73 |
daemon->serverarray[count] = serv; |
| 74 |
serv->serial = count; |
| 75 |
serv->last_server = -1; |
| 76 |
count++; |
| 77 |
} |
| 78 |
|
| 79 |
for (serv = daemon->local_domains; serv; serv = serv->next, count++) |
| 80 |
daemon->serverarray[count] = serv; |
| 81 |
|
| 82 |
/* serverarray may be unallocated if we have no servers yet. */ |
| 83 |
if (daemon->serverarray) |
| 84 |
qsort(daemon->serverarray, daemon->serverarraysz, sizeof(struct server *), order_qsort); |
| 85 |
|
| 86 |
/* servers need the location in the array to find all the whole |
| 87 |
set of equivalent servers from a pointer to a single one. */ |
| 88 |
for (count = 0; count < daemon->serverarraysz; count++) |
| 89 |
if (!(daemon->serverarray[count]->flags & SERV_IS_LOCAL)) |
| 90 |
daemon->serverarray[count]->arrayposn = count; |
| 91 |
} |
| 92 |
|
| 93 |
/* we're looking for the server whose domain is the longest exact match |
| 94 |
to the RH end of qdomain, or a local address if the flags match. |
| 95 |
Add '.' to the LHS of the query string so |
| 96 |
server=/.example.com/ works. |
| 97 |
|
| 98 |
A flag of F_SERVER returns an upstream server only. |
| 99 |
A flag of F_DNSSECOK disables NODOTS servers from consideration. |
| 100 |
A flag of F_DS returns parent domain server. |
| 101 |
A flag of F_DOMAINSRV returns a domain-specific server only. |
| 102 |
A flag of F_CONFIG returns anything that generates a local |
| 103 |
reply of IPv4 or IPV6. |
| 104 |
return 0 if nothing found, 1 otherwise. |
| 105 |
*/ |
| 106 |
int lookup_domain(char *domain, int flags, int *lowout, int *highout) |
| 107 |
{ |
| 108 |
int rc, crop_query, nodots; |
| 109 |
ssize_t qlen; |
| 110 |
int try, high, low = 0; |
| 111 |
int nlow = 0, nhigh = 0; |
| 112 |
char *cp, *qdomain; |
| 113 |
|
| 114 |
/* may be no configured servers. */ |
| 115 |
if (daemon->serverarraysz == 0) |
| 116 |
return 0; |
| 117 |
|
| 118 |
/* DS records should come from the parent domain. */ |
| 119 |
if (flags & F_DS) |
| 120 |
{ |
| 121 |
if ((cp = strchr(domain, '.'))) |
| 122 |
domain = cp+1; |
| 123 |
else |
| 124 |
domain = ""; |
| 125 |
} |
| 126 |
|
| 127 |
qdomain = domain; |
| 128 |
|
| 129 |
/* find query length and presence of '.' */ |
| 130 |
for (cp = qdomain, nodots = 1, qlen = 0; *cp; qlen++, cp++) |
| 131 |
if (*cp == '.') |
| 132 |
nodots = 0; |
| 133 |
|
| 134 |
/* Handle empty name, and searches for DNSSEC queries without |
| 135 |
diverting to NODOTS servers. */ |
| 136 |
if (qlen == 0 || flags & F_DNSSECOK) |
| 137 |
nodots = 0; |
| 138 |
|
| 139 |
/* Search shorter and shorter RHS substrings for a match */ |
| 140 |
while (qlen >= 0) |
| 141 |
{ |
| 142 |
/* Note that when we chop off a label, all the possible matches |
| 143 |
MUST be at a larger index than the nearest failing match with one more |
| 144 |
character, since the array is sorted longest to smallest. Hence |
| 145 |
we don't reset low to zero here, we can go further below and crop the |
| 146 |
search string to the size of the largest remaining server |
| 147 |
when this match fails. */ |
| 148 |
high = daemon->serverarraysz; |
| 149 |
crop_query = 1; |
| 150 |
|
| 151 |
/* binary search */ |
| 152 |
while (1) |
| 153 |
{ |
| 154 |
try = (low + high)/2; |
| 155 |
|
| 156 |
if ((rc = order(qdomain, qlen, daemon->serverarray[try])) == 0) |
| 157 |
break; |
| 158 |
|
| 159 |
if (rc < 0) |
| 160 |
{ |
| 161 |
if (high == try) |
| 162 |
{ |
| 163 |
/* qdomain is longer or same length as longest domain, and try == 0 |
| 164 |
crop the query to the longest domain. */ |
| 165 |
crop_query = qlen - daemon->serverarray[try]->domain_len; |
| 166 |
break; |
| 167 |
} |
| 168 |
high = try; |
| 169 |
} |
| 170 |
else |
| 171 |
{ |
| 172 |
if (low == try) |
| 173 |
{ |
| 174 |
/* try now points to the last domain that sorts before the query, so |
| 175 |
we know that a substring of the query shorter than it is required to match, so |
| 176 |
find the largest domain that's shorter than try. Note that just going to |
| 177 |
try+1 is not optimal, consider searching bbb in (aaa,ccc,bb). try will point |
| 178 |
to aaa, since ccc sorts after bbb, but the first domain that has a chance to |
| 179 |
match is bb. So find the length of the first domain later than try which is |
| 180 |
is shorter than it. |
| 181 |
There's a nasty edge case when qdomain sorts before _any_ of the |
| 182 |
server domains, where try _doesn't point_ to the last domain that sorts |
| 183 |
before the query, since no such domain exists. In that case, the loop |
| 184 |
exits via the rc < 0 && high == try path above and this code is |
| 185 |
not executed. */ |
| 186 |
ssize_t len, old = daemon->serverarray[try]->domain_len; |
| 187 |
while (++try != daemon->serverarraysz) |
| 188 |
{ |
| 189 |
if (old != (len = daemon->serverarray[try]->domain_len)) |
| 190 |
{ |
| 191 |
crop_query = qlen - len; |
| 192 |
break; |
| 193 |
} |
| 194 |
} |
| 195 |
break; |
| 196 |
} |
| 197 |
low = try; |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
if (rc == 0) |
| 202 |
{ |
| 203 |
int found = 1; |
| 204 |
|
| 205 |
if (daemon->server_has_wildcard) |
| 206 |
{ |
| 207 |
/* if we have example.com and *example.com we need to check against *example.com, |
| 208 |
but the binary search may have found either. Use the fact that example.com is sorted before *example.com |
| 209 |
We favour example.com in the case that both match (ie www.example.com) */ |
| 210 |
while (try != 0 && order(qdomain, qlen, daemon->serverarray[try-1]) == 0) |
| 211 |
try--; |
| 212 |
|
| 213 |
if (!(qdomain == domain || *qdomain == 0 || *(qdomain-1) == '.')) |
| 214 |
{ |
| 215 |
while (try < daemon->serverarraysz-1 && order(qdomain, qlen, daemon->serverarray[try+1]) == 0) |
| 216 |
try++; |
| 217 |
|
| 218 |
if (!(daemon->serverarray[try]->flags & SERV_WILDCARD)) |
| 219 |
found = 0; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
if (found && filter_servers(try, flags, &nlow, &nhigh)) |
| 224 |
/* We have a match, but it may only be (say) an IPv6 address, and |
| 225 |
if the query wasn't for an AAAA record, it's no good, and we need |
| 226 |
to continue generalising */ |
| 227 |
{ |
| 228 |
/* We've matched a setting which says to use servers without a domain. |
| 229 |
Continue the search with empty query. We set the F_SERVER flag |
| 230 |
so that --address=/#/... doesn't match. */ |
| 231 |
if (daemon->serverarray[nlow]->flags & SERV_USE_RESOLV) |
| 232 |
{ |
| 233 |
crop_query = qlen; |
| 234 |
flags |= F_SERVER; |
| 235 |
} |
| 236 |
else |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/* crop_query must be at least one always. */ |
| 242 |
if (crop_query == 0) |
| 243 |
crop_query = 1; |
| 244 |
|
| 245 |
/* strip chars off the query based on the largest possible remaining match, |
| 246 |
then continue to the start of the next label unless we have a wildcard |
| 247 |
domain somewhere, in which case we have to go one at a time. */ |
| 248 |
qlen -= crop_query; |
| 249 |
qdomain += crop_query; |
| 250 |
if (!daemon->server_has_wildcard) |
| 251 |
while (qlen > 0 && (*(qdomain-1) != '.')) |
| 252 |
qlen--, qdomain++; |
| 253 |
} |
| 254 |
|
| 255 |
/* domain has no dots, and we have at least one server configured to handle such, |
| 256 |
These servers always sort to the very end of the array. |
| 257 |
A configured server eg server=/lan/ will take precdence. */ |
| 258 |
if (nodots && |
| 259 |
(daemon->serverarray[daemon->serverarraysz-1]->flags & SERV_FOR_NODOTS) && |
| 260 |
(nlow == nhigh || daemon->serverarray[nlow]->domain_len == 0)) |
| 261 |
{ |
| 262 |
filter_servers(daemon->serverarraysz-1, flags, &nlow, &nhigh); |
| 263 |
qlen = 0; |
| 264 |
} |
| 265 |
|
| 266 |
if (lowout) |
| 267 |
*lowout = nlow; |
| 268 |
|
| 269 |
if (highout) |
| 270 |
*highout = nhigh; |
| 271 |
|
| 272 |
/* qlen == -1 when we failed to match even an empty query, if there are no default servers. */ |
| 273 |
if (nlow == nhigh || qlen == -1) |
| 274 |
return 0; |
| 275 |
|
| 276 |
return 1; |
| 277 |
} |
| 278 |
|
| 279 |
int server_samegroup(struct server *a, struct server *b) |
| 280 |
{ |
| 281 |
return order_servers(a, b) == 0; |
| 282 |
} |
| 283 |
|
| 284 |
int filter_servers(int seed, int flags, int *lowout, int *highout) |
| 285 |
{ |
| 286 |
int nlow = seed, nhigh = seed; |
| 287 |
int i; |
| 288 |
|
| 289 |
/* expand nlow and nhigh to cover all the records with the same domain |
| 290 |
nlow is the first, nhigh - 1 is the last. nlow=nhigh means no servers, |
| 291 |
which can happen below. */ |
| 292 |
while (nlow > 0 && order_servers(daemon->serverarray[nlow-1], daemon->serverarray[nlow]) == 0) |
| 293 |
nlow--; |
| 294 |
|
| 295 |
while (nhigh < daemon->serverarraysz-1 && order_servers(daemon->serverarray[nhigh], daemon->serverarray[nhigh+1]) == 0) |
| 296 |
nhigh++; |
| 297 |
|
| 298 |
nhigh++; |
| 299 |
|
| 300 |
#define SERV_LOCAL_ADDRESS (SERV_6ADDR | SERV_4ADDR | SERV_ALL_ZEROS) |
| 301 |
|
| 302 |
if (flags & F_CONFIG) |
| 303 |
{ |
| 304 |
/* We're just lookin for any matches that return an RR. */ |
| 305 |
for (i = nlow; i < nhigh; i++) |
| 306 |
if (daemon->serverarray[i]->flags & SERV_LOCAL_ADDRESS) |
| 307 |
break; |
| 308 |
|
| 309 |
/* failed, return failure. */ |
| 310 |
if (i == nhigh) |
| 311 |
nhigh = nlow; |
| 312 |
} |
| 313 |
else |
| 314 |
{ |
| 315 |
/* Now the matching server records are all between low and high. |
| 316 |
order_qsort() ensures that they are in the order |
| 317 |
IPv6 addr, IPv4 addr, return zero for both, no-data return, |
| 318 |
"use resolvconf" servers, domain-specific upstream servers. |
| 319 |
|
| 320 |
See which of those match our query in that priority order and narrow (low, high) */ |
| 321 |
|
| 322 |
for (i = nlow; i < nhigh && (daemon->serverarray[i]->flags & SERV_6ADDR); i++); |
| 323 |
|
| 324 |
if (!(flags & F_SERVER) && i != nlow && (flags & F_IPV6)) |
| 325 |
nhigh = i; |
| 326 |
else |
| 327 |
{ |
| 328 |
nlow = i; |
| 329 |
|
| 330 |
for (i = nlow; i < nhigh && (daemon->serverarray[i]->flags & SERV_4ADDR); i++); |
| 331 |
|
| 332 |
if (!(flags & F_SERVER) && i != nlow && (flags & F_IPV4)) |
| 333 |
nhigh = i; |
| 334 |
else |
| 335 |
{ |
| 336 |
nlow = i; |
| 337 |
|
| 338 |
for (i = nlow; i < nhigh && (daemon->serverarray[i]->flags & SERV_ALL_ZEROS); i++); |
| 339 |
|
| 340 |
if (!(flags & F_SERVER) && i != nlow && (flags & (F_IPV4 | F_IPV6))) |
| 341 |
nhigh = i; |
| 342 |
else |
| 343 |
{ |
| 344 |
nlow = i; |
| 345 |
|
| 346 |
/* now look for a NXDOMAIN answer --local=/domain/ */ |
| 347 |
for (i = nlow; i < nhigh && (daemon->serverarray[i]->flags & SERV_LITERAL_ADDRESS); i++); |
| 348 |
|
| 349 |
if (!(flags & (F_DOMAINSRV | F_SERVER)) && i != nlow) |
| 350 |
nhigh = i; |
| 351 |
else |
| 352 |
{ |
| 353 |
nlow = i; |
| 354 |
|
| 355 |
/* return "use resolv.conf servers" if they exist */ |
| 356 |
for (i = nlow; i < nhigh && (daemon->serverarray[i]->flags & SERV_USE_RESOLV); i++); |
| 357 |
|
| 358 |
if (i != nlow) |
| 359 |
nhigh = i; |
| 360 |
else |
| 361 |
{ |
| 362 |
/* If we want a server for a particular domain, and this one isn't, return nothing. */ |
| 363 |
if (nlow < daemon->serverarraysz && nlow != nhigh && (flags & F_DOMAINSRV) && |
| 364 |
daemon->serverarray[nlow]->domain_len == 0 && !(daemon->serverarray[nlow]->flags & SERV_FOR_NODOTS)) |
| 365 |
nlow = nhigh; |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
*lowout = nlow; |
| 374 |
*highout = nhigh; |
| 375 |
|
| 376 |
return (nlow != nhigh); |
| 377 |
} |
| 378 |
|
| 379 |
int is_local_answer(time_t now, int first, char *name) |
| 380 |
{ |
| 381 |
int flags = 0; |
| 382 |
int rc = 0; |
| 383 |
|
| 384 |
if ((flags = daemon->serverarray[first]->flags) & SERV_LITERAL_ADDRESS) |
| 385 |
{ |
| 386 |
if (flags & SERV_4ADDR) |
| 387 |
rc = F_IPV4; |
| 388 |
else if (flags & SERV_6ADDR) |
| 389 |
rc = F_IPV6; |
| 390 |
else if (flags & SERV_ALL_ZEROS) |
| 391 |
rc = F_IPV4 | F_IPV6; |
| 392 |
else |
| 393 |
{ |
| 394 |
/* argument first is the first struct server which matches the query type; |
| 395 |
now roll back to the server which is just the same domain, to check if that |
| 396 |
provides an answer of a different type. */ |
| 397 |
|
| 398 |
for (;first > 0 && order_servers(daemon->serverarray[first-1], daemon->serverarray[first]) == 0; first--); |
| 399 |
|
| 400 |
if ((daemon->serverarray[first]->flags & SERV_LOCAL_ADDRESS) || |
| 401 |
check_for_local_domain(name, now)) |
| 402 |
rc = F_NOERR; |
| 403 |
else |
| 404 |
rc = F_NXDOMAIN; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
return rc; |
| 409 |
} |
| 410 |
|
| 411 |
size_t make_local_answer(int flags, int gotname, size_t size, struct dns_header *header, char *name, size_t limit, int first, int last, int ede) |
| 412 |
{ |
| 413 |
int trunc = 0, anscount = 0; |
| 414 |
unsigned char *p; |
| 415 |
int start; |
| 416 |
union all_addr addr; |
| 417 |
char *out_end = ((char *)header) + limit; |
| 418 |
|
| 419 |
setup_reply(header, flags, ede); |
| 420 |
|
| 421 |
gotname &= ~(F_QUERY | F_DS); |
| 422 |
|
| 423 |
if (flags & (F_NXDOMAIN | F_NOERR)) |
| 424 |
log_query(flags | gotname | F_NEG | F_CONFIG | F_FORWARD, name, NULL, NULL, 0); |
| 425 |
|
| 426 |
if (flags & F_RCODE) |
| 427 |
{ |
| 428 |
union all_addr a; |
| 429 |
a.log.rcode = RCODE(header); |
| 430 |
a.log.ede = ede; |
| 431 |
log_query(F_UPSTREAM | F_RCODE, "opcode", &a, NULL, 0); |
| 432 |
} |
| 433 |
|
| 434 |
if (!(p = skip_questions(header, size))) |
| 435 |
return 0; |
| 436 |
|
| 437 |
if (flags & gotname & F_IPV4) |
| 438 |
for (start = first; start != last; start++) |
| 439 |
{ |
| 440 |
struct serv_addr4 *srv = (struct serv_addr4 *)daemon->serverarray[start]; |
| 441 |
|
| 442 |
if (srv->flags & SERV_ALL_ZEROS) |
| 443 |
memset(&addr, 0, sizeof(addr)); |
| 444 |
else |
| 445 |
addr.addr4 = srv->addr; |
| 446 |
|
| 447 |
if (add_resource_record(header, out_end, &trunc, sizeof(struct dns_header), &p, daemon->local_ttl, NULL, T_A, C_IN, "4", &addr)) |
| 448 |
anscount++; |
| 449 |
log_query((flags | F_CONFIG | F_FORWARD) & ~F_IPV6, name, (union all_addr *)&addr, NULL, 0); |
| 450 |
} |
| 451 |
|
| 452 |
if (flags & gotname & F_IPV6) |
| 453 |
for (start = first; start != last; start++) |
| 454 |
{ |
| 455 |
struct serv_addr6 *srv = (struct serv_addr6 *)daemon->serverarray[start]; |
| 456 |
|
| 457 |
if (srv->flags & SERV_ALL_ZEROS) |
| 458 |
memset(&addr, 0, sizeof(addr)); |
| 459 |
else |
| 460 |
addr.addr6 = srv->addr; |
| 461 |
|
| 462 |
if (add_resource_record(header, out_end, &trunc, sizeof(struct dns_header), &p, daemon->local_ttl, NULL, T_AAAA, C_IN, "6", &addr)) |
| 463 |
anscount++; |
| 464 |
log_query((flags | F_CONFIG | F_FORWARD) & ~F_IPV4, name, (union all_addr *)&addr, NULL, 0); |
| 465 |
} |
| 466 |
|
| 467 |
if (trunc) |
| 468 |
{ |
| 469 |
header->hb3 |= HB3_TC; |
| 470 |
if (!(p = skip_questions(header, size))) |
| 471 |
return 0; /* bad packet */ |
| 472 |
anscount = 0; |
| 473 |
} |
| 474 |
|
| 475 |
header->ancount = htons(anscount); |
| 476 |
|
| 477 |
return p - (unsigned char *)header; |
| 478 |
} |
| 479 |
|
| 480 |
#ifdef HAVE_DNSSEC |
| 481 |
int dnssec_server(struct server *server, char *keyname, int is_ds, int *firstp, int *lastp) |
| 482 |
{ |
| 483 |
int first, last, index; |
| 484 |
|
| 485 |
/* Find server to send DNSSEC query to. This will normally be the |
| 486 |
same as for the original query, but may be another if |
| 487 |
servers for domains are involved. */ |
| 488 |
if (!lookup_domain(keyname, F_SERVER | F_DNSSECOK | (is_ds ? F_DS : 0), &first, &last)) |
| 489 |
return -1; |
| 490 |
|
| 491 |
for (index = first; index != last; index++) |
| 492 |
if (daemon->serverarray[index] == server) |
| 493 |
break; |
| 494 |
|
| 495 |
/* No match to server used for original query. |
| 496 |
Use newly looked up set. */ |
| 497 |
if (index == last) |
| 498 |
index = daemon->serverarray[first]->last_server == -1 ? |
| 499 |
first : daemon->serverarray[first]->last_server; |
| 500 |
|
| 501 |
if (firstp) |
| 502 |
*firstp = first; |
| 503 |
|
| 504 |
if (lastp) |
| 505 |
*lastp = last; |
| 506 |
|
| 507 |
return index; |
| 508 |
} |
| 509 |
#endif |
| 510 |
|
| 511 |
/* order by size, then by dictionary order */ |
| 512 |
static int order(char *qdomain, size_t qlen, struct server *serv) |
| 513 |
{ |
| 514 |
size_t dlen = 0; |
| 515 |
|
| 516 |
/* servers for dotless names always sort last |
| 517 |
searched for name is never dotless. */ |
| 518 |
if (serv->flags & SERV_FOR_NODOTS) |
| 519 |
return -1; |
| 520 |
|
| 521 |
dlen = serv->domain_len; |
| 522 |
|
| 523 |
if (qlen < dlen) |
| 524 |
return 1; |
| 525 |
|
| 526 |
if (qlen > dlen) |
| 527 |
return -1; |
| 528 |
|
| 529 |
return hostname_order(qdomain, serv->domain); |
| 530 |
} |
| 531 |
|
| 532 |
static int order_servers(struct server *s1, struct server *s2) |
| 533 |
{ |
| 534 |
int rc; |
| 535 |
|
| 536 |
/* need full comparison of dotless servers in |
| 537 |
order_qsort() and filter_servers() */ |
| 538 |
|
| 539 |
if (s1->flags & SERV_FOR_NODOTS) |
| 540 |
return (s2->flags & SERV_FOR_NODOTS) ? 0 : 1; |
| 541 |
|
| 542 |
if ((rc = order(s1->domain, s1->domain_len, s2)) != 0) |
| 543 |
return rc; |
| 544 |
|
| 545 |
/* For identical domains, sort wildcard ones first */ |
| 546 |
if (s1->flags & SERV_WILDCARD) |
| 547 |
return (s2->flags & SERV_WILDCARD) ? 0 : 1; |
| 548 |
|
| 549 |
return (s2->flags & SERV_WILDCARD) ? -1 : 0; |
| 550 |
} |
| 551 |
|
| 552 |
static int order_qsort(const void *a, const void *b) |
| 553 |
{ |
| 554 |
int rc; |
| 555 |
|
| 556 |
struct server *s1 = *((struct server **)a); |
| 557 |
struct server *s2 = *((struct server **)b); |
| 558 |
|
| 559 |
rc = order_servers(s1, s2); |
| 560 |
|
| 561 |
/* Sort all literal NODATA and local IPV4 or IPV6 responses together, |
| 562 |
in a very specific order IPv6 literal, IPv4 literal, all-zero literal, |
| 563 |
NXDOMAIN literal. We also include SERV_USE_RESOLV in this, so that |
| 564 |
use-standard servers sort before ordinary servers. (SERV_USR_RESOLV set |
| 565 |
implies that none of SERV_LITERAL_ADDRESS,SERV_4ADDR,SERV_6ADDR,SERV_ALL_ZEROS |
| 566 |
are set) */ |
| 567 |
if (rc == 0) |
| 568 |
rc = ((s2->flags & (SERV_LITERAL_ADDRESS | SERV_4ADDR | SERV_6ADDR | SERV_ALL_ZEROS | SERV_USE_RESOLV))) - |
| 569 |
((s1->flags & (SERV_LITERAL_ADDRESS | SERV_4ADDR | SERV_6ADDR | SERV_ALL_ZEROS | SERV_USE_RESOLV))); |
| 570 |
|
| 571 |
/* Finally, order by appearance in /etc/resolv.conf etc, for --strict-order */ |
| 572 |
if (rc == 0) |
| 573 |
if (!(s1->flags & SERV_IS_LOCAL) && !(s2->flags & SERV_IS_LOCAL)) |
| 574 |
rc = s1->serial - s2->serial; |
| 575 |
|
| 576 |
return rc; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
/* When loading large numbers of server=.... lines during startup, |
| 581 |
there's no possibility that there will be server records that can be reused, but |
| 582 |
searching a long list for each server added grows as O(n^2) and slows things down. |
| 583 |
This flag is set only if is known there may be free server records that can be reused. |
| 584 |
There's a call to mark_servers(0) in read_opts() to reset the flag before |
| 585 |
main config read. */ |
| 586 |
|
| 587 |
static int maybe_free_servers = 0; |
| 588 |
|
| 589 |
/* Must be called before add_update_server() to set daemon->servers_tail */ |
| 590 |
void mark_servers(int flag) |
| 591 |
{ |
| 592 |
struct server *serv, *next, **up; |
| 593 |
|
| 594 |
maybe_free_servers = !!flag; |
| 595 |
|
| 596 |
daemon->servers_tail = NULL; |
| 597 |
|
| 598 |
/* mark everything with argument flag */ |
| 599 |
for (serv = daemon->servers; serv; serv = serv->next) |
| 600 |
{ |
| 601 |
if (serv->flags & flag) |
| 602 |
serv->flags |= SERV_MARK; |
| 603 |
else |
| 604 |
serv->flags &= ~SERV_MARK; |
| 605 |
|
| 606 |
daemon->servers_tail = serv; |
| 607 |
} |
| 608 |
|
| 609 |
/* --address etc is different: since they are expected to be |
| 610 |
1) numerous and 2) not reloaded often. We just delete |
| 611 |
and recreate. */ |
| 612 |
if (flag) |
| 613 |
for (serv = daemon->local_domains, up = &daemon->local_domains; serv; serv = next) |
| 614 |
{ |
| 615 |
next = serv->next; |
| 616 |
|
| 617 |
if (serv->flags & flag) |
| 618 |
{ |
| 619 |
*up = next; |
| 620 |
free(serv->domain); |
| 621 |
free(serv); |
| 622 |
} |
| 623 |
else |
| 624 |
up = &serv->next; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
void cleanup_servers(void) |
| 629 |
{ |
| 630 |
struct server *serv, *tmp, **up; |
| 631 |
|
| 632 |
/* unlink and free anything still marked. */ |
| 633 |
for (serv = daemon->servers, up = &daemon->servers, daemon->servers_tail = NULL; serv; serv = tmp) |
| 634 |
{ |
| 635 |
tmp = serv->next; |
| 636 |
if (serv->flags & SERV_MARK) |
| 637 |
{ |
| 638 |
server_gone(serv); |
| 639 |
*up = serv->next; |
| 640 |
free(serv->domain); |
| 641 |
free(serv); |
| 642 |
} |
| 643 |
else |
| 644 |
{ |
| 645 |
up = &serv->next; |
| 646 |
daemon->servers_tail = serv; |
| 647 |
} |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
int add_update_server(int flags, |
| 652 |
union mysockaddr *addr, |
| 653 |
union mysockaddr *source_addr, |
| 654 |
const char *interface, |
| 655 |
const char *domain, |
| 656 |
union all_addr *local_addr) |
| 657 |
{ |
| 658 |
struct server *serv = NULL; |
| 659 |
char *alloc_domain; |
| 660 |
|
| 661 |
if (!domain) |
| 662 |
domain = ""; |
| 663 |
|
| 664 |
/* .domain == domain, for historical reasons. */ |
| 665 |
if (*domain == '.') |
| 666 |
while (*domain == '.') domain++; |
| 667 |
else if (*domain == '*') |
| 668 |
{ |
| 669 |
domain++; |
| 670 |
if (*domain != 0) |
| 671 |
flags |= SERV_WILDCARD; |
| 672 |
} |
| 673 |
|
| 674 |
if (*domain == 0) |
| 675 |
alloc_domain = whine_malloc(1); |
| 676 |
else |
| 677 |
alloc_domain = canonicalise((char *)domain, NULL); |
| 678 |
|
| 679 |
if (!alloc_domain) |
| 680 |
return 0; |
| 681 |
|
| 682 |
if (flags & SERV_IS_LOCAL) |
| 683 |
{ |
| 684 |
size_t size; |
| 685 |
|
| 686 |
if (flags & SERV_6ADDR) |
| 687 |
size = sizeof(struct serv_addr6); |
| 688 |
else if (flags & SERV_4ADDR) |
| 689 |
size = sizeof(struct serv_addr4); |
| 690 |
else |
| 691 |
size = sizeof(struct serv_local); |
| 692 |
|
| 693 |
if (!(serv = whine_malloc(size))) |
| 694 |
{ |
| 695 |
free(alloc_domain); |
| 696 |
return 0; |
| 697 |
} |
| 698 |
|
| 699 |
serv->next = daemon->local_domains; |
| 700 |
daemon->local_domains = serv; |
| 701 |
|
| 702 |
if (flags & SERV_4ADDR) |
| 703 |
((struct serv_addr4*)serv)->addr = local_addr->addr4; |
| 704 |
|
| 705 |
if (flags & SERV_6ADDR) |
| 706 |
((struct serv_addr6*)serv)->addr = local_addr->addr6; |
| 707 |
} |
| 708 |
else |
| 709 |
{ |
| 710 |
/* Upstream servers. See if there is a suitable candidate, if so unmark |
| 711 |
and move to the end of the list, for order. The entry found may already |
| 712 |
be at the end. */ |
| 713 |
struct server **up, *tmp; |
| 714 |
|
| 715 |
serv = NULL; |
| 716 |
|
| 717 |
if (maybe_free_servers) |
| 718 |
for (serv = daemon->servers, up = &daemon->servers; serv; serv = tmp) |
| 719 |
{ |
| 720 |
tmp = serv->next; |
| 721 |
if ((serv->flags & SERV_MARK) && |
| 722 |
hostname_isequal(alloc_domain, serv->domain)) |
| 723 |
{ |
| 724 |
/* Need to move down? */ |
| 725 |
if (serv->next) |
| 726 |
{ |
| 727 |
*up = serv->next; |
| 728 |
daemon->servers_tail->next = serv; |
| 729 |
daemon->servers_tail = serv; |
| 730 |
serv->next = NULL; |
| 731 |
} |
| 732 |
break; |
| 733 |
} |
| 734 |
else |
| 735 |
up = &serv->next; |
| 736 |
} |
| 737 |
|
| 738 |
if (serv) |
| 739 |
{ |
| 740 |
free(alloc_domain); |
| 741 |
alloc_domain = serv->domain; |
| 742 |
} |
| 743 |
else |
| 744 |
{ |
| 745 |
if (!(serv = whine_malloc(sizeof(struct server)))) |
| 746 |
{ |
| 747 |
free(alloc_domain); |
| 748 |
return 0; |
| 749 |
} |
| 750 |
|
| 751 |
memset(serv, 0, sizeof(struct server)); |
| 752 |
|
| 753 |
/* Add to the end of the chain, for order */ |
| 754 |
if (daemon->servers_tail) |
| 755 |
daemon->servers_tail->next = serv; |
| 756 |
else |
| 757 |
daemon->servers = serv; |
| 758 |
daemon->servers_tail = serv; |
| 759 |
} |
| 760 |
|
| 761 |
#ifdef HAVE_LOOP |
| 762 |
serv->uid = rand32(); |
| 763 |
#endif |
| 764 |
|
| 765 |
if (interface) |
| 766 |
safe_strncpy(serv->interface, interface, sizeof(serv->interface)); |
| 767 |
if (addr) |
| 768 |
serv->addr = *addr; |
| 769 |
if (source_addr) |
| 770 |
serv->source_addr = *source_addr; |
| 771 |
|
| 772 |
serv->tcpfd = -1; |
| 773 |
} |
| 774 |
|
| 775 |
serv->flags = flags; |
| 776 |
serv->domain = alloc_domain; |
| 777 |
serv->domain_len = strlen(alloc_domain); |
| 778 |
|
| 779 |
return 1; |
| 780 |
} |
| 781 |
|