| 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 |
/* EXTR_NAME_EXTRACT -> extract name |
| 20 |
EXTR_NAME_COMPARE -> compare name, case insensitive |
| 21 |
EXTR_NAME_NOCASE -> compare name, case sensitive |
| 22 |
EXTR_NAME_FLIP -> flip 0x20 bits in packet. |
| 23 |
|
| 24 |
For flip, name is an array of ints, whose size |
| 25 |
is given in parm, which forms the bitmap. Bits beyond the size |
| 26 |
are assumed to be zero. |
| 27 |
|
| 28 |
return = 0 -> error |
| 29 |
return = 1 -> extract OK, compare OK, flip OK |
| 30 |
return = 2 -> extract OK, compare failed. |
| 31 |
return = 3 -> extract OK, compare failed but only on case. |
| 32 |
|
| 33 |
If pp == NULL, operate on the query name in the packet. |
| 34 |
*/ |
| 35 |
int extract_name(struct dns_header *header, size_t plen, unsigned char **pp, |
| 36 |
char *name, int func, unsigned int parm) |
| 37 |
{ |
| 38 |
unsigned char *cp = (unsigned char *)name, *p1 = NULL; |
| 39 |
unsigned int j, l, namelen = 0, hops = 0; |
| 40 |
unsigned int bigmap_counter = 0, bigmap_posn = 0, bigmap_size = parm, bitmap = 0; |
| 41 |
int retvalue = 1, case_insens = 1, isExtract = 0, flip = 0, extrabytes = (int)parm; |
| 42 |
unsigned int *bigmap = (unsigned int *)name; |
| 43 |
unsigned char *p = pp ? *pp : (unsigned char *)(header+1); |
| 44 |
|
| 45 |
if (func == EXTR_NAME_EXTRACT) |
| 46 |
isExtract = 1, *cp = 0; |
| 47 |
else if (func == EXTR_NAME_NOCASE) |
| 48 |
case_insens = 0; |
| 49 |
else if (func == EXTR_NAME_FLIP) |
| 50 |
{ |
| 51 |
flip = 1, extrabytes = 0; |
| 52 |
name = NULL; |
| 53 |
} |
| 54 |
|
| 55 |
while (1) |
| 56 |
{ |
| 57 |
unsigned int label_type; |
| 58 |
|
| 59 |
if (!CHECK_LEN(header, p, plen, 1)) |
| 60 |
return 0; |
| 61 |
|
| 62 |
if ((l = *p++) == 0) |
| 63 |
/* end marker */ |
| 64 |
{ |
| 65 |
/* check that there are the correct no. of bytes after the name */ |
| 66 |
if (!CHECK_LEN(header, p1 ? p1 : p, plen, extrabytes)) |
| 67 |
return 0; |
| 68 |
|
| 69 |
if (isExtract) |
| 70 |
{ |
| 71 |
if (cp != (unsigned char *)name) |
| 72 |
cp--; |
| 73 |
*cp = 0; /* terminate: lose final period */ |
| 74 |
} |
| 75 |
else if (!flip && *cp != 0) |
| 76 |
retvalue = 2; |
| 77 |
|
| 78 |
if (pp) |
| 79 |
{ |
| 80 |
if (p1) /* we jumped via compression */ |
| 81 |
*pp = p1; |
| 82 |
else |
| 83 |
*pp = p; |
| 84 |
} |
| 85 |
|
| 86 |
return retvalue; |
| 87 |
} |
| 88 |
|
| 89 |
label_type = l & 0xc0; |
| 90 |
|
| 91 |
if (label_type == 0xc0) /* pointer */ |
| 92 |
{ |
| 93 |
if (!CHECK_LEN(header, p, plen, 1)) |
| 94 |
return 0; |
| 95 |
|
| 96 |
/* get offset */ |
| 97 |
l = (l&0x3f) << 8; |
| 98 |
l |= *p++; |
| 99 |
|
| 100 |
if (!p1) /* first jump, save location to go back to */ |
| 101 |
p1 = p; |
| 102 |
|
| 103 |
hops++; /* break malicious infinite loops */ |
| 104 |
if (hops > 255) |
| 105 |
return 0; |
| 106 |
|
| 107 |
p = l + (unsigned char *)header; |
| 108 |
} |
| 109 |
else if (label_type == 0x00) /* label_type = 0 -> label. */ |
| 110 |
{ |
| 111 |
/* reject wire-format names > MAXDNAME bytes |
| 112 |
This also ensures that internal-format |
| 113 |
names don't exceed MAXDNAMESTR characters. */ |
| 114 |
namelen += l + 1; |
| 115 |
/* namelen == MAXDNAME means terminator will overflow. */ |
| 116 |
if (namelen >= MAXDNAME) |
| 117 |
return 0; |
| 118 |
|
| 119 |
if (!CHECK_LEN(header, p, plen, l)) |
| 120 |
return 0; |
| 121 |
|
| 122 |
for (j=0; j<l; j++, p++) |
| 123 |
if (isExtract) |
| 124 |
{ |
| 125 |
unsigned char c = *p; |
| 126 |
|
| 127 |
if (IS_NAME_ESCAPE(c)) |
| 128 |
{ |
| 129 |
*cp++ = NAME_ESCAPE; |
| 130 |
*cp++ = c+1; |
| 131 |
} |
| 132 |
else |
| 133 |
*cp++ = c; |
| 134 |
} |
| 135 |
else if (flip) |
| 136 |
{ |
| 137 |
unsigned char c = *p; |
| 138 |
|
| 139 |
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) |
| 140 |
{ |
| 141 |
/* Get the next int of the bitmap */ |
| 142 |
if (bigmap_posn < bigmap_size && bigmap_counter-- == 0) |
| 143 |
{ |
| 144 |
bitmap = bigmap[bigmap_posn++]; |
| 145 |
bigmap_counter = (sizeof(unsigned int) * 8) - 1; |
| 146 |
} |
| 147 |
|
| 148 |
if (bitmap & 1) |
| 149 |
*p ^= 0x20; |
| 150 |
bitmap >>= 1; |
| 151 |
} |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
unsigned char c1 = *cp, c2 = *p; |
| 156 |
|
| 157 |
if (c1 == 0) |
| 158 |
retvalue = 2; |
| 159 |
else |
| 160 |
{ |
| 161 |
cp++; |
| 162 |
|
| 163 |
if (c1 == NAME_ESCAPE) |
| 164 |
c1 = (*cp++)-1; |
| 165 |
else if (case_insens && c1 >= 'A' && c1 <= 'Z') |
| 166 |
c1 += 'a' - 'A'; |
| 167 |
|
| 168 |
if (case_insens && c2 >= 'A' && c2 <= 'Z') |
| 169 |
c2 += 'a' - 'A'; |
| 170 |
|
| 171 |
if (!case_insens && retvalue != 2 && c1 != c2) |
| 172 |
{ |
| 173 |
if (c1 >= 'A' && c1 <= 'Z') |
| 174 |
c1 += 'a' - 'A'; |
| 175 |
|
| 176 |
if (c2 >= 'A' && c2 <= 'Z') |
| 177 |
c2 += 'a' - 'A'; |
| 178 |
|
| 179 |
if (c1 == c2) |
| 180 |
retvalue = 3; |
| 181 |
} |
| 182 |
|
| 183 |
if (c1 != c2) |
| 184 |
retvalue = 2; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if (isExtract) |
| 189 |
*cp++ = '.'; |
| 190 |
else if (!flip && *cp != 0 && *cp++ != '.') |
| 191 |
retvalue = 2; |
| 192 |
} |
| 193 |
else |
| 194 |
return 0; /* label types 0x40 and 0x80 not supported */ |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/* Max size of input string (for IPv6) is 75 chars.) */ |
| 199 |
#define MAXARPANAME 75 |
| 200 |
int in_arpa_name_2_addr(char *namein, union all_addr *addrp) |
| 201 |
{ |
| 202 |
int j; |
| 203 |
char name[MAXARPANAME+1], *cp1; |
| 204 |
unsigned char *addr = (unsigned char *)addrp; |
| 205 |
char *lastchunk = NULL, *penchunk = NULL; |
| 206 |
|
| 207 |
if (strlen(namein) > MAXARPANAME) |
| 208 |
return 0; |
| 209 |
|
| 210 |
memset(addrp, 0, sizeof(union all_addr)); |
| 211 |
|
| 212 |
/* turn name into a series of asciiz strings */ |
| 213 |
/* j counts no. of labels */ |
| 214 |
for(j = 1,cp1 = name; *namein; cp1++, namein++) |
| 215 |
if (*namein == '.') |
| 216 |
{ |
| 217 |
penchunk = lastchunk; |
| 218 |
lastchunk = cp1 + 1; |
| 219 |
*cp1 = 0; |
| 220 |
j++; |
| 221 |
} |
| 222 |
else |
| 223 |
*cp1 = *namein; |
| 224 |
|
| 225 |
*cp1 = 0; |
| 226 |
|
| 227 |
if (j<3) |
| 228 |
return 0; |
| 229 |
|
| 230 |
if (hostname_isequal(lastchunk, "arpa") && hostname_isequal(penchunk, "in-addr")) |
| 231 |
{ |
| 232 |
/* IP v4 */ |
| 233 |
/* address arrives as a name of the form |
| 234 |
www.xxx.yyy.zzz.in-addr.arpa |
| 235 |
some of the low order address octets might be missing |
| 236 |
and should be set to zero. */ |
| 237 |
for (cp1 = name; cp1 != penchunk; cp1 += strlen(cp1)+1) |
| 238 |
{ |
| 239 |
/* check for digits only (weeds out things like |
| 240 |
50.0/24.67.28.64.in-addr.arpa which are used |
| 241 |
as CNAME targets according to RFC 2317 */ |
| 242 |
char *cp; |
| 243 |
for (cp = cp1; *cp; cp++) |
| 244 |
if (!isdigit((unsigned char)*cp)) |
| 245 |
return 0; |
| 246 |
|
| 247 |
addr[3] = addr[2]; |
| 248 |
addr[2] = addr[1]; |
| 249 |
addr[1] = addr[0]; |
| 250 |
addr[0] = atoi(cp1); |
| 251 |
} |
| 252 |
|
| 253 |
return F_IPV4; |
| 254 |
} |
| 255 |
else if (hostname_isequal(penchunk, "ip6") && |
| 256 |
(hostname_isequal(lastchunk, "int") || hostname_isequal(lastchunk, "arpa"))) |
| 257 |
{ |
| 258 |
/* IP v6: |
| 259 |
Address arrives as 0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f.ip6.[int|arpa] |
| 260 |
or \[xfedcba9876543210fedcba9876543210/128].ip6.[int|arpa] |
| 261 |
|
| 262 |
Note that most of these the various representations are obsolete and |
| 263 |
left-over from the many DNS-for-IPv6 wars. We support all the formats |
| 264 |
that we can since there is no reason not to. |
| 265 |
*/ |
| 266 |
|
| 267 |
if (*name == '\\' && *(name+1) == '[' && |
| 268 |
(*(name+2) == 'x' || *(name+2) == 'X')) |
| 269 |
{ |
| 270 |
for (j = 0, cp1 = name+3; *cp1 && isxdigit((unsigned char) *cp1) && j < 32; cp1++, j++) |
| 271 |
{ |
| 272 |
char xdig[2]; |
| 273 |
xdig[0] = *cp1; |
| 274 |
xdig[1] = 0; |
| 275 |
if (j%2) |
| 276 |
addr[j/2] |= strtol(xdig, NULL, 16); |
| 277 |
else |
| 278 |
addr[j/2] = strtol(xdig, NULL, 16) << 4; |
| 279 |
} |
| 280 |
|
| 281 |
if (*cp1 == '/' && j == 32) |
| 282 |
return F_IPV6; |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
for (cp1 = name; cp1 != penchunk; cp1 += strlen(cp1)+1) |
| 287 |
{ |
| 288 |
if (*(cp1+1) || !isxdigit((unsigned char)*cp1)) |
| 289 |
return 0; |
| 290 |
|
| 291 |
for (j = sizeof(struct in6_addr)-1; j>0; j--) |
| 292 |
addr[j] = (addr[j] >> 4) | (addr[j-1] << 4); |
| 293 |
addr[0] = (addr[0] >> 4) | (strtol(cp1, NULL, 16) << 4); |
| 294 |
} |
| 295 |
|
| 296 |
return F_IPV6; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
unsigned char *skip_name(unsigned char *ansp, struct dns_header *header, size_t plen, int extrabytes) |
| 304 |
{ |
| 305 |
while(1) |
| 306 |
{ |
| 307 |
unsigned int label_type; |
| 308 |
|
| 309 |
if (!CHECK_LEN(header, ansp, plen, 1)) |
| 310 |
return NULL; |
| 311 |
|
| 312 |
label_type = (*ansp) & 0xc0; |
| 313 |
|
| 314 |
if (label_type == 0xc0) |
| 315 |
{ |
| 316 |
/* pointer for compression. */ |
| 317 |
ansp += 2; |
| 318 |
break; |
| 319 |
} |
| 320 |
else if (label_type == 0x80) |
| 321 |
return NULL; /* reserved */ |
| 322 |
else if (label_type == 0x40) |
| 323 |
{ |
| 324 |
/* Extended label type */ |
| 325 |
unsigned int count, llen; |
| 326 |
|
| 327 |
if (!CHECK_LEN(header, ansp, plen, 2)) |
| 328 |
return NULL; |
| 329 |
|
| 330 |
if (((*ansp++) & 0x3f) != 1) |
| 331 |
return NULL; /* we only understand bitstrings */ |
| 332 |
|
| 333 |
count = *(ansp++); /* Bits in bitstring */ |
| 334 |
|
| 335 |
if (count == 0) /* count == 0 means 256 bits */ |
| 336 |
llen = 32; |
| 337 |
else |
| 338 |
llen = ((count-1)>>3)+1; |
| 339 |
|
| 340 |
if (!ADD_RDLEN(header, ansp, plen, llen)) |
| 341 |
return NULL; |
| 342 |
} |
| 343 |
else |
| 344 |
{ /* label type == 0 Bottom six bits is length */ |
| 345 |
unsigned int len = (*ansp++) & 0x3f; |
| 346 |
|
| 347 |
if (!ADD_RDLEN(header, ansp, plen, len)) |
| 348 |
return NULL; |
| 349 |
|
| 350 |
if (len == 0) |
| 351 |
break; /* zero length label marks the end. */ |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
if (!CHECK_LEN(header, ansp, plen, extrabytes)) |
| 356 |
return NULL; |
| 357 |
|
| 358 |
return ansp; |
| 359 |
} |
| 360 |
|
| 361 |
unsigned char *skip_questions(struct dns_header *header, size_t plen) |
| 362 |
{ |
| 363 |
int q; |
| 364 |
unsigned char *ansp = (unsigned char *)(header+1); |
| 365 |
|
| 366 |
for (q = ntohs(header->qdcount); q != 0; q--) |
| 367 |
{ |
| 368 |
if (!(ansp = skip_name(ansp, header, plen, 4))) |
| 369 |
return NULL; |
| 370 |
ansp += 4; /* class and type */ |
| 371 |
} |
| 372 |
|
| 373 |
return ansp; |
| 374 |
} |
| 375 |
|
| 376 |
unsigned char *skip_section(unsigned char *ansp, int count, struct dns_header *header, size_t plen) |
| 377 |
{ |
| 378 |
int i, rdlen; |
| 379 |
|
| 380 |
for (i = 0; i < count; i++) |
| 381 |
{ |
| 382 |
if (!(ansp = skip_name(ansp, header, plen, 10))) |
| 383 |
return NULL; |
| 384 |
ansp += 8; /* type, class, TTL */ |
| 385 |
GETSHORT(rdlen, ansp); |
| 386 |
if (!ADD_RDLEN(header, ansp, plen, rdlen)) |
| 387 |
return NULL; |
| 388 |
} |
| 389 |
|
| 390 |
return ansp; |
| 391 |
} |
| 392 |
|
| 393 |
size_t resize_packet(struct dns_header *header, size_t plen, unsigned char *pheader, size_t hlen) |
| 394 |
{ |
| 395 |
unsigned char *ansp = skip_questions(header, plen); |
| 396 |
|
| 397 |
/* if packet is malformed, just return as-is. */ |
| 398 |
if (!ansp) |
| 399 |
return plen; |
| 400 |
|
| 401 |
if (!(ansp = skip_section(ansp, ntohs(header->ancount) + ntohs(header->nscount) + ntohs(header->arcount), |
| 402 |
header, plen))) |
| 403 |
return plen; |
| 404 |
|
| 405 |
/* restore pseudoheader */ |
| 406 |
if (pheader && ntohs(header->arcount) == 0) |
| 407 |
{ |
| 408 |
/* must use memmove, may overlap */ |
| 409 |
memmove(ansp, pheader, hlen); |
| 410 |
header->arcount = htons(1); |
| 411 |
ansp += hlen; |
| 412 |
} |
| 413 |
|
| 414 |
return ansp - (unsigned char *)header; |
| 415 |
} |
| 416 |
|
| 417 |
/* is addr in the non-globally-routed IP space? */ |
| 418 |
int private_net(struct in_addr addr, int ban_localhost) |
| 419 |
{ |
| 420 |
in_addr_t ip_addr = ntohl(addr.s_addr); |
| 421 |
|
| 422 |
return |
| 423 |
(((ip_addr & 0xFF000000) == 0x7F000000) && ban_localhost) /* 127.0.0.0/8 (loopback) */ || |
| 424 |
(((ip_addr & 0xFF000000) == 0x00000000) && ban_localhost) /* RFC 5735 section 3. "here" network */ || |
| 425 |
((ip_addr & 0xFF000000) == 0x0A000000) /* 10.0.0.0/8 (private) */ || |
| 426 |
((ip_addr & 0xFFC00000) == 0x64400000) /* 100.64.0.0/10 (CG-NAT) RFC6598/RFC7793*/ || |
| 427 |
((ip_addr & 0xFFF00000) == 0xAC100000) /* 172.16.0.0/12 (private) */ || |
| 428 |
((ip_addr & 0xFFFF0000) == 0xC0A80000) /* 192.168.0.0/16 (private) */ || |
| 429 |
((ip_addr & 0xFFFF0000) == 0xA9FE0000) /* 169.254.0.0/16 (zeroconf) */ || |
| 430 |
((ip_addr & 0xFFFFFF00) == 0xC0000200) /* 192.0.2.0/24 (test-net) */ || |
| 431 |
((ip_addr & 0xFFFFFF00) == 0xC6336400) /* 198.51.100.0/24(test-net) */ || |
| 432 |
((ip_addr & 0xFFFFFF00) == 0xCB007100) /* 203.0.113.0/24 (test-net) */ || |
| 433 |
((ip_addr & 0xFFFFFFFF) == 0xFFFFFFFF) /* 255.255.255.255/32 (broadcast)*/ ; |
| 434 |
} |
| 435 |
|
| 436 |
int private_net6(struct in6_addr *a, int ban_localhost) |
| 437 |
{ |
| 438 |
/* Block IPv4-mapped IPv6 addresses in private IPv4 address space */ |
| 439 |
if (IN6_IS_ADDR_V4MAPPED(a)) |
| 440 |
{ |
| 441 |
struct in_addr v4; |
| 442 |
v4.s_addr = ((const uint32_t *) (a))[3]; |
| 443 |
return private_net(v4, ban_localhost); |
| 444 |
} |
| 445 |
|
| 446 |
return |
| 447 |
(IN6_IS_ADDR_UNSPECIFIED(a) && ban_localhost) || /* RFC 6303 4.3 */ |
| 448 |
(IN6_IS_ADDR_LOOPBACK(a) && ban_localhost) || /* RFC 6303 4.3 */ |
| 449 |
IN6_IS_ADDR_LINKLOCAL(a) || /* RFC 6303 4.5 */ |
| 450 |
IN6_IS_ADDR_SITELOCAL(a) || |
| 451 |
(((unsigned char *)a)[0] & 0xfe) == 0xfc || /* RFC 4196 3.1 */ |
| 452 |
((u32 *)a)[0] == htonl(0x20010db8); /* RFC 6303 4.6 */ |
| 453 |
} |
| 454 |
|
| 455 |
int do_doctor(struct dns_header *header, size_t qlen, char *namebuff) |
| 456 |
{ |
| 457 |
unsigned char *p; |
| 458 |
int i, qtype, qclass, rdlen; |
| 459 |
int done = 0; |
| 460 |
|
| 461 |
if (!(p = skip_questions(header, qlen))) |
| 462 |
return done; |
| 463 |
|
| 464 |
for (i = 0; i < ntohs(header->ancount) + ntohs(header->arcount); i++) |
| 465 |
{ |
| 466 |
/* Skip over auth section */ |
| 467 |
if (i == ntohs(header->ancount) && !(p = skip_section(p, ntohs(header->nscount), header, qlen))) |
| 468 |
return done; |
| 469 |
|
| 470 |
if (!extract_name(header, qlen, &p, namebuff, EXTR_NAME_EXTRACT, 10)) |
| 471 |
return done; /* bad packet */ |
| 472 |
|
| 473 |
GETSHORT(qtype, p); |
| 474 |
GETSHORT(qclass, p); |
| 475 |
p += 4; /* ttl */ |
| 476 |
GETSHORT(rdlen, p); |
| 477 |
|
| 478 |
if (qclass == C_IN && qtype == T_A) |
| 479 |
{ |
| 480 |
struct doctor *doctor; |
| 481 |
union all_addr addr; |
| 482 |
|
| 483 |
if (!CHECK_LEN(header, p, qlen, INADDRSZ)) |
| 484 |
return done; |
| 485 |
|
| 486 |
/* alignment */ |
| 487 |
memcpy(&addr.addr4, p, INADDRSZ); |
| 488 |
|
| 489 |
for (doctor = daemon->doctors; doctor; doctor = doctor->next) |
| 490 |
{ |
| 491 |
if (doctor->end.s_addr == 0) |
| 492 |
{ |
| 493 |
if (!is_same_net(doctor->in, addr.addr4, doctor->mask)) |
| 494 |
continue; |
| 495 |
} |
| 496 |
else if (ntohl(doctor->in.s_addr) > ntohl(addr.addr4.s_addr) || |
| 497 |
ntohl(doctor->end.s_addr) < ntohl(addr.addr4.s_addr)) |
| 498 |
continue; |
| 499 |
|
| 500 |
addr.addr4.s_addr &= ~doctor->mask.s_addr; |
| 501 |
addr.addr4.s_addr |= (doctor->out.s_addr & doctor->mask.s_addr); |
| 502 |
/* Since we munged the data, the server it came from is no longer authoritative */ |
| 503 |
header->hb3 &= ~HB3_AA; |
| 504 |
#ifdef HAVE_DNSSEC |
| 505 |
/* remove validated flag from this RR, since we changed it! */ |
| 506 |
if (option_bool(OPT_DNSSEC_VALID) && i < daemon->rr_status_sz && i < ntohs(header->ancount)) |
| 507 |
daemon->rr_status[i] = 0; |
| 508 |
#endif |
| 509 |
done = 1; |
| 510 |
memcpy(p, &addr.addr4, INADDRSZ); |
| 511 |
log_query(F_FORWARD | F_CONFIG | F_IPV4, namebuff, &addr, NULL, 0); |
| 512 |
break; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
if (!ADD_RDLEN(header, p, qlen, rdlen)) |
| 517 |
return done; /* bad packet */ |
| 518 |
} |
| 519 |
|
| 520 |
return done; |
| 521 |
} |
| 522 |
|
| 523 |
/* Find SOA RR in auth section to get TTL for negative caching of name. |
| 524 |
Cache said SOA and return the difference in length between name and the name of the |
| 525 |
SOA RR so we can look it up again. |
| 526 |
*/ |
| 527 |
static int find_soa(struct dns_header *header, size_t qlen, char *name, int *substring, unsigned long *ttlp, int cache, time_t now) |
| 528 |
{ |
| 529 |
unsigned char *p, *psave; |
| 530 |
int qtype, qclass, rdlen; |
| 531 |
unsigned long ttl, minttl; |
| 532 |
int i, j; |
| 533 |
size_t name_len, soa_len, len; |
| 534 |
union all_addr addr; |
| 535 |
|
| 536 |
/* first move to NS section and find TTL from SOA RR */ |
| 537 |
if (!(p = skip_questions(header, qlen)) || |
| 538 |
!(p = skip_section(p, ntohs(header->ancount), header, qlen))) |
| 539 |
return 0; /* bad packet */ |
| 540 |
|
| 541 |
name_len = strlen(name); |
| 542 |
|
| 543 |
if (substring) |
| 544 |
*substring = name_len; |
| 545 |
|
| 546 |
for (i = 0; i < ntohs(header->nscount); i++) |
| 547 |
{ |
| 548 |
if (!extract_name(header, qlen, &p, daemon->workspacename, EXTR_NAME_EXTRACT, 10)) |
| 549 |
return 0; /* bad packet */ |
| 550 |
|
| 551 |
GETSHORT(qtype, p); |
| 552 |
GETSHORT(qclass, p); |
| 553 |
GETLONG(ttl, p); |
| 554 |
GETSHORT(rdlen, p); |
| 555 |
|
| 556 |
psave = p; |
| 557 |
|
| 558 |
if ((qclass == C_IN) && (qtype == T_SOA)) |
| 559 |
{ |
| 560 |
soa_len = strlen(daemon->workspacename); |
| 561 |
|
| 562 |
/* SOA must be for the name we're interested in. */ |
| 563 |
if (soa_len <= name_len && memcmp(daemon->workspacename, name + name_len - soa_len, soa_len) == 0) |
| 564 |
{ |
| 565 |
int prefix = name_len - soa_len; |
| 566 |
|
| 567 |
if (cache) |
| 568 |
{ |
| 569 |
if (!(addr.rrblock.rrdata = blockdata_alloc(NULL, 0))) |
| 570 |
return 0; |
| 571 |
addr.rrblock.rrtype = T_SOA; |
| 572 |
addr.rrblock.datalen = 0; |
| 573 |
} |
| 574 |
|
| 575 |
for (j = 0; j < 2; j++) /* MNAME, RNAME */ |
| 576 |
{ |
| 577 |
if (!extract_name(header, qlen, &p, daemon->workspacename, EXTR_NAME_EXTRACT, 0)) |
| 578 |
{ |
| 579 |
if (cache) |
| 580 |
blockdata_free(addr.rrblock.rrdata); |
| 581 |
return 0; |
| 582 |
} |
| 583 |
|
| 584 |
if (cache) |
| 585 |
{ |
| 586 |
len = to_wire(daemon->workspacename); |
| 587 |
if (!blockdata_expand(addr.rrblock.rrdata, addr.rrblock.datalen, daemon->workspacename, len)) |
| 588 |
{ |
| 589 |
blockdata_free(addr.rrblock.rrdata); |
| 590 |
return 0; |
| 591 |
} |
| 592 |
|
| 593 |
addr.rrblock.datalen += len; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
if (!CHECK_LEN(header, p, qlen, 20)) |
| 598 |
{ |
| 599 |
if (cache) |
| 600 |
blockdata_free(addr.rrblock.rrdata); |
| 601 |
return 0; |
| 602 |
} |
| 603 |
|
| 604 |
/* rest of RR */ |
| 605 |
if (cache) |
| 606 |
{ |
| 607 |
int secflag = 0; |
| 608 |
|
| 609 |
if (!blockdata_expand(addr.rrblock.rrdata, addr.rrblock.datalen, (char *)p, 20)) |
| 610 |
{ |
| 611 |
blockdata_free(addr.rrblock.rrdata); |
| 612 |
return 0; |
| 613 |
} |
| 614 |
|
| 615 |
addr.rrblock.datalen += 20; |
| 616 |
|
| 617 |
#ifdef HAVE_DNSSEC |
| 618 |
if (option_bool(OPT_DNSSEC_VALID) && |
| 619 |
i + ntohs(header->ancount) < daemon->rr_status_sz && |
| 620 |
daemon->rr_status[i + ntohs(header->ancount)] != 0) |
| 621 |
{ |
| 622 |
secflag = F_DNSSECOK; |
| 623 |
|
| 624 |
/* limit TTL based on signature. */ |
| 625 |
if (daemon->rr_status[i + ntohs(header->ancount)] < ttl) |
| 626 |
ttl = daemon->rr_status[i + ntohs(header->ancount)]; |
| 627 |
} |
| 628 |
#endif |
| 629 |
|
| 630 |
if (!cache_insert(name + prefix, &addr, C_IN, now, ttl, F_FORWARD | F_RR | F_KEYTAG | secflag)) |
| 631 |
{ |
| 632 |
blockdata_free(addr.rrblock.rrdata); |
| 633 |
return 0; |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
p += 16; /* SERIAL REFRESH RETRY EXPIRE */ |
| 638 |
|
| 639 |
GETLONG(minttl, p); /* minTTL */ |
| 640 |
if (ttl < minttl) |
| 641 |
minttl = ttl; |
| 642 |
|
| 643 |
if (substring) |
| 644 |
*substring = prefix; |
| 645 |
|
| 646 |
if (ttlp) |
| 647 |
*ttlp = minttl; |
| 648 |
|
| 649 |
return 1; |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
p = psave; |
| 654 |
|
| 655 |
if (!ADD_RDLEN(header, p, qlen, rdlen)) |
| 656 |
return 0; /* bad packet */ |
| 657 |
} |
| 658 |
|
| 659 |
return 0; |
| 660 |
} |
| 661 |
|
| 662 |
/* Print TXT reply to log */ |
| 663 |
static int log_txt(char *name, unsigned char *p, const int ardlen, int flag) |
| 664 |
{ |
| 665 |
unsigned char *p1 = p; |
| 666 |
|
| 667 |
/* Loop over TXT payload */ |
| 668 |
while ((p1 - p) < ardlen) |
| 669 |
{ |
| 670 |
unsigned int i, len = *p1; |
| 671 |
unsigned char *p3 = p1; |
| 672 |
if ((p1 + len - p) >= ardlen) |
| 673 |
return 0; /* bad packet */ |
| 674 |
|
| 675 |
/* make counted string zero-term and sanitise */ |
| 676 |
for (i = 0; i < len; i++) |
| 677 |
{ |
| 678 |
if (!isprint((unsigned char)*(p3+1))) |
| 679 |
break; |
| 680 |
*p3 = *(p3+1); |
| 681 |
p3++; |
| 682 |
} |
| 683 |
|
| 684 |
*p3 = 0; |
| 685 |
log_query(flag, name, NULL, (char*)p1, 0); |
| 686 |
/* restore */ |
| 687 |
memmove(p1 + 1, p1, i); |
| 688 |
*p1 = len; |
| 689 |
p1 += len+1; |
| 690 |
} |
| 691 |
return 1; |
| 692 |
} |
| 693 |
|
| 694 |
/* Note that the following code can create CNAME chains that don't point to a real record, |
| 695 |
either because of lack of memory, or lack of SOA records. These are treated by the cache code as |
| 696 |
expired and cleaned out that way. |
| 697 |
Return 1 if we reject an address because it look like part of dns-rebinding attack. |
| 698 |
Return 2 if the packet is malformed. |
| 699 |
*/ |
| 700 |
int extract_addresses(struct dns_header *header, size_t qlen, char *name, time_t now, |
| 701 |
struct ipsets *ipsets, struct ipsets *nftsets, int check_rebind, |
| 702 |
int no_cache_dnssec, int secure) |
| 703 |
{ |
| 704 |
unsigned char *p, *p1, *endrr, *namep; |
| 705 |
int j, qtype, qclass, aqtype, aqclass, ardlen, res; |
| 706 |
unsigned long ttl; |
| 707 |
union all_addr addr; |
| 708 |
#ifdef HAVE_IPSET |
| 709 |
char **ipsets_cur; |
| 710 |
#else |
| 711 |
(void)ipsets; /* unused */ |
| 712 |
#endif |
| 713 |
#ifdef HAVE_NFTSET |
| 714 |
char **nftsets_cur; |
| 715 |
#else |
| 716 |
(void)nftsets; /* unused */ |
| 717 |
#endif |
| 718 |
int name_encoding, found = 0, ptr = 0; |
| 719 |
int flags = RCODE(header) == NXDOMAIN ? F_NXDOMAIN : 0; |
| 720 |
|
| 721 |
cache_start_insert(); |
| 722 |
|
| 723 |
namep = p = (unsigned char *)(header+1); |
| 724 |
|
| 725 |
if (ntohs(header->qdcount) != 1 || !extract_name(header, qlen, &p, name, EXTR_NAME_EXTRACT, 4)) |
| 726 |
return 2; /* bad packet */ |
| 727 |
|
| 728 |
GETSHORT(qtype, p); |
| 729 |
GETSHORT(qclass, p); |
| 730 |
|
| 731 |
if (qclass != C_IN) |
| 732 |
return 0; |
| 733 |
|
| 734 |
/* If the PTR record encodes an address, store using a name/address record with F_REVERSE set. |
| 735 |
Otherwise, it gets stored as an arbitrary RR below. If the query is answerable with |
| 736 |
a CNAME, also take the arbitrary-RR route, since the cache can't represent a CNAME |
| 737 |
whose target is stored in a F_REVERSE record. */ |
| 738 |
if (qtype == T_PTR && !(flags & F_NXDOMAIN) && (name_encoding = in_arpa_name_2_addr(name, &addr))) |
| 739 |
{ |
| 740 |
ptr = 1; |
| 741 |
|
| 742 |
if (!(p1 = skip_questions(header, qlen))) |
| 743 |
return 2; |
| 744 |
|
| 745 |
for (j = 0; j < ntohs(header->ancount); j++) |
| 746 |
{ |
| 747 |
int secflag = 0; |
| 748 |
if (!(res = extract_name(header, qlen, &p1, name, EXTR_NAME_COMPARE, 10))) |
| 749 |
return 2; /* bad packet */ |
| 750 |
|
| 751 |
GETSHORT(aqtype, p1); |
| 752 |
GETSHORT(aqclass, p1); |
| 753 |
p = p1; |
| 754 |
GETLONG(ttl, p1); |
| 755 |
GETSHORT(ardlen, p1); |
| 756 |
endrr = p1+ardlen; |
| 757 |
|
| 758 |
if (aqclass == C_IN && res == 1 && aqtype == T_PTR) |
| 759 |
{ |
| 760 |
found = 1; |
| 761 |
|
| 762 |
if ((daemon->max_ttl != 0) && (ttl > daemon->max_ttl)) |
| 763 |
ttl = daemon->max_ttl; |
| 764 |
|
| 765 |
#ifdef HAVE_DNSSEC |
| 766 |
if (option_bool(OPT_DNSSEC_VALID) && j < daemon->rr_status_sz && daemon->rr_status[j] != 0) |
| 767 |
{ |
| 768 |
secflag = F_DNSSECOK; |
| 769 |
/* limit TTL based on signature. */ |
| 770 |
if (daemon->rr_status[j] < ttl) |
| 771 |
ttl = daemon->rr_status[j]; |
| 772 |
} |
| 773 |
#endif |
| 774 |
|
| 775 |
PUTLONG(ttl, p); |
| 776 |
|
| 777 |
if (!extract_name(header, qlen, &p1, name, EXTR_NAME_EXTRACT, 0)) |
| 778 |
return 2; |
| 779 |
log_query(name_encoding | secflag | F_REVERSE | F_UPSTREAM, name, &addr, NULL, 0); |
| 780 |
cache_insert(name, &addr, C_IN, now, ttl, name_encoding | secflag | F_REVERSE); |
| 781 |
|
| 782 |
/* restore query into name */ |
| 783 |
p1 = namep; |
| 784 |
if (!extract_name(header, qlen, &p1, name, EXTR_NAME_EXTRACT, 0)) |
| 785 |
return 2; |
| 786 |
} |
| 787 |
|
| 788 |
p1 = endrr; |
| 789 |
if (!CHECK_LEN(header, p1, qlen, 0)) |
| 790 |
return 2; /* bad packet */ |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
if (!ptr || !found) |
| 795 |
{ |
| 796 |
/* everything other than PTR */ |
| 797 |
struct crec *newc, *cpp = NULL; |
| 798 |
int cname_count = CNAME_CHAIN, addrlen = 0, insert = 1; |
| 799 |
|
| 800 |
if (qtype == T_A) |
| 801 |
{ |
| 802 |
addrlen = INADDRSZ; |
| 803 |
flags |= F_IPV4; |
| 804 |
} |
| 805 |
else if (qtype == T_AAAA) |
| 806 |
{ |
| 807 |
addrlen = IN6ADDRSZ; |
| 808 |
flags |= F_IPV6; |
| 809 |
} |
| 810 |
else if (qtype != T_CNAME && |
| 811 |
(qtype == T_SRV || qtype == T_PTR || rr_on_list(daemon->cache_rr, qtype) || rr_on_list(daemon->cache_rr, T_ANY))) |
| 812 |
flags |= F_RR; |
| 813 |
else |
| 814 |
insert = 0; /* NOTE: do not cache data from CNAME queries. */ |
| 815 |
|
| 816 |
cname_loop: |
| 817 |
if (!(p1 = skip_questions(header, qlen))) |
| 818 |
return 2; |
| 819 |
|
| 820 |
for (j = 0; j < ntohs(header->ancount); j++) |
| 821 |
{ |
| 822 |
int secflag = 0; |
| 823 |
|
| 824 |
if (!(res = extract_name(header, qlen, &p1, name, EXTR_NAME_COMPARE, 10))) |
| 825 |
return 2; /* bad packet */ |
| 826 |
|
| 827 |
GETSHORT(aqtype, p1); |
| 828 |
GETSHORT(aqclass, p1); |
| 829 |
p = p1; |
| 830 |
GETLONG(ttl, p1); |
| 831 |
GETSHORT(ardlen, p1); |
| 832 |
endrr = p1+ardlen; |
| 833 |
|
| 834 |
if (!CHECK_LEN(header, endrr, qlen, 0)) |
| 835 |
return 2; /* bad packet */ |
| 836 |
|
| 837 |
/* Not what we're looking for? */ |
| 838 |
if (aqclass != C_IN || res == 2) |
| 839 |
{ |
| 840 |
p1 = endrr; |
| 841 |
continue; |
| 842 |
} |
| 843 |
|
| 844 |
if ((daemon->max_ttl != 0) && (ttl > daemon->max_ttl)) |
| 845 |
ttl = daemon->max_ttl; |
| 846 |
|
| 847 |
#ifdef HAVE_DNSSEC |
| 848 |
if (option_bool(OPT_DNSSEC_VALID) && j < daemon->rr_status_sz && daemon->rr_status[j] != 0) |
| 849 |
{ |
| 850 |
secflag = F_DNSSECOK; |
| 851 |
|
| 852 |
/* limit TTl based on sig. */ |
| 853 |
if (daemon->rr_status[j] < ttl) |
| 854 |
ttl = daemon->rr_status[j]; |
| 855 |
} |
| 856 |
#endif |
| 857 |
|
| 858 |
PUTLONG(ttl, p); |
| 859 |
|
| 860 |
if (aqtype == T_CNAME) |
| 861 |
{ |
| 862 |
if (!cname_count--) |
| 863 |
return 0; /* looped CNAMES */ |
| 864 |
|
| 865 |
log_query(secflag | F_CNAME | F_FORWARD | F_UPSTREAM, name, NULL, NULL, 0); |
| 866 |
|
| 867 |
if (insert) |
| 868 |
{ |
| 869 |
if ((newc = cache_insert(name, NULL, C_IN, now, ttl, F_CNAME | F_FORWARD | secflag))) |
| 870 |
{ |
| 871 |
newc->addr.cname.target.cache = NULL; |
| 872 |
newc->addr.cname.is_name_ptr = 0; |
| 873 |
if (cpp) |
| 874 |
{ |
| 875 |
next_uid(newc); |
| 876 |
cpp->addr.cname.target.cache = newc; |
| 877 |
cpp->addr.cname.uid = newc->uid; |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
cpp = newc; |
| 882 |
} |
| 883 |
|
| 884 |
/* Set the query to the CNAME target and go again unless the query was just for a CNAME. */ |
| 885 |
namep = p1; |
| 886 |
if (!extract_name(header, qlen, &p1, name, EXTR_NAME_EXTRACT, 0)) |
| 887 |
return 2; |
| 888 |
|
| 889 |
if (qtype != T_CNAME) |
| 890 |
goto cname_loop; |
| 891 |
|
| 892 |
found = 1; |
| 893 |
} |
| 894 |
else if (qtype == T_ANY || aqtype != qtype) |
| 895 |
{ |
| 896 |
#ifdef HAVE_DNSSEC |
| 897 |
if (!option_bool(OPT_DNSSEC_VALID) || aqtype != T_RRSIG) |
| 898 |
#endif |
| 899 |
log_query(secflag | F_FORWARD | F_UPSTREAM | F_RRNAME, name, NULL, NULL, aqtype); |
| 900 |
} |
| 901 |
else if (!(flags & F_NXDOMAIN)) |
| 902 |
{ |
| 903 |
found = 1; |
| 904 |
|
| 905 |
if (flags & F_RR) |
| 906 |
{ |
| 907 |
short desc, *rrdesc = rrfilter_desc(aqtype); |
| 908 |
unsigned char *tmp = namep; |
| 909 |
|
| 910 |
if (!CHECK_LEN(header, p1, qlen, ardlen)) |
| 911 |
return 2; /* bad packet */ |
| 912 |
|
| 913 |
/* If the data has no names and is small enough, store it in |
| 914 |
the crec address field rather than allocate a block. */ |
| 915 |
if (*rrdesc == -1 && ardlen <= (int)RR_IMDATALEN) |
| 916 |
{ |
| 917 |
addr.rrdata.rrtype = aqtype; |
| 918 |
addr.rrdata.datalen = (char)ardlen; |
| 919 |
flags &= ~F_KEYTAG; /* in case of >1 answer, not all the same. */ |
| 920 |
if (ardlen != 0) |
| 921 |
memcpy(addr.rrdata.data, p1, ardlen); |
| 922 |
} |
| 923 |
else |
| 924 |
{ |
| 925 |
addr.rrblock.rrtype = aqtype; |
| 926 |
addr.rrblock.datalen = 0; |
| 927 |
flags |= F_KEYTAG; /* discriminates between rrdata and rrblock */ |
| 928 |
|
| 929 |
/* The RR data may include names, and those names may include |
| 930 |
compression, which will be rendered meaningless when |
| 931 |
copied into another packet. |
| 932 |
Here we go through a description of the packet type to |
| 933 |
find the names, and extract them to a c-string and then |
| 934 |
re-encode them to standalone DNS format without compression. */ |
| 935 |
if (!(addr.rrblock.rrdata = blockdata_alloc(NULL, 0))) |
| 936 |
return 0; |
| 937 |
do |
| 938 |
{ |
| 939 |
desc = *rrdesc++; |
| 940 |
|
| 941 |
if (desc == -1) |
| 942 |
{ |
| 943 |
/* Copy the rest of the RR and end. */ |
| 944 |
if (!blockdata_expand(addr.rrblock.rrdata, addr.rrblock.datalen, (char *)p1, endrr - p1)) |
| 945 |
{ |
| 946 |
blockdata_free(addr.rrblock.rrdata); |
| 947 |
return 0; |
| 948 |
} |
| 949 |
addr.rrblock.datalen += endrr - p1; |
| 950 |
} |
| 951 |
else if (desc == 0) |
| 952 |
{ |
| 953 |
/* Name, extract it then re-encode. */ |
| 954 |
int len; |
| 955 |
|
| 956 |
/* rdlen may lie, and extract_name() advances p1 past where it says the record ends. */ |
| 957 |
if (!extract_name(header, qlen, &p1, name, EXTR_NAME_EXTRACT, 0) || (p1 > endrr)) |
| 958 |
{ |
| 959 |
blockdata_free(addr.rrblock.rrdata); |
| 960 |
return 2; |
| 961 |
} |
| 962 |
|
| 963 |
len = to_wire(name); |
| 964 |
if (!blockdata_expand(addr.rrblock.rrdata, addr.rrblock.datalen, name, len)) |
| 965 |
{ |
| 966 |
blockdata_free(addr.rrblock.rrdata); |
| 967 |
return 0; |
| 968 |
} |
| 969 |
|
| 970 |
addr.rrblock.datalen += len; |
| 971 |
} |
| 972 |
else |
| 973 |
{ |
| 974 |
/* desc is length of a block of data to be used as-is */ |
| 975 |
if (desc > endrr - p1) |
| 976 |
desc = endrr - p1; |
| 977 |
|
| 978 |
if (!blockdata_expand(addr.rrblock.rrdata, addr.rrblock.datalen, (char *)p1, desc)) |
| 979 |
{ |
| 980 |
blockdata_free(addr.rrblock.rrdata); |
| 981 |
return 0; |
| 982 |
} |
| 983 |
|
| 984 |
addr.rrblock.datalen += desc; |
| 985 |
p1 += desc; |
| 986 |
} |
| 987 |
} while (desc != -1); |
| 988 |
|
| 989 |
/* we overwrote the original name, so get it back here. */ |
| 990 |
if (!extract_name(header, qlen, &tmp, name, EXTR_NAME_EXTRACT, 0)) |
| 991 |
{ |
| 992 |
blockdata_free(addr.rrblock.rrdata); |
| 993 |
return 2; |
| 994 |
} |
| 995 |
} |
| 996 |
} |
| 997 |
else if (flags & (F_IPV4 | F_IPV6)) |
| 998 |
{ |
| 999 |
/* copy address into aligned storage */ |
| 1000 |
if (!CHECK_LEN(header, p1, qlen, addrlen)) |
| 1001 |
return 2; /* bad packet */ |
| 1002 |
memcpy(&addr, p1, addrlen); |
| 1003 |
|
| 1004 |
/* check for returned address in private space */ |
| 1005 |
if (check_rebind) |
| 1006 |
{ |
| 1007 |
if ((flags & F_IPV4) && |
| 1008 |
private_net(addr.addr4, !option_bool(OPT_LOCAL_REBIND))) |
| 1009 |
return 1; |
| 1010 |
|
| 1011 |
if ((flags & F_IPV6) && |
| 1012 |
private_net6(&addr.addr6, !option_bool(OPT_LOCAL_REBIND))) |
| 1013 |
return 1; |
| 1014 |
} |
| 1015 |
|
| 1016 |
if (flags & (F_IPV4 | F_IPV6)) |
| 1017 |
{ |
| 1018 |
/* If we're a child process, send this to the parent, |
| 1019 |
since the ipset and nfset access is not re-entrant. */ |
| 1020 |
#ifdef HAVE_IPSET |
| 1021 |
if (ipsets) |
| 1022 |
{ |
| 1023 |
if (daemon->pipe_to_parent != -1) |
| 1024 |
cache_send_ipset(PIPE_OP_IPSET, ipsets, flags, &addr); |
| 1025 |
else |
| 1026 |
for (ipsets_cur = ipsets->sets; *ipsets_cur; ipsets_cur++) |
| 1027 |
if (add_to_ipset(*ipsets_cur, &addr, flags, 0) == 0) |
| 1028 |
log_query((flags & (F_IPV4 | F_IPV6)) | F_IPSET, ipsets->domain, &addr, *ipsets_cur, 1); |
| 1029 |
} |
| 1030 |
#endif |
| 1031 |
#ifdef HAVE_NFTSET |
| 1032 |
if (nftsets) |
| 1033 |
{ |
| 1034 |
if (daemon->pipe_to_parent != -1) |
| 1035 |
cache_send_ipset(PIPE_OP_NFTSET, nftsets, flags, &addr); |
| 1036 |
else |
| 1037 |
for (nftsets_cur = nftsets->sets; *nftsets_cur; nftsets_cur++) |
| 1038 |
if (add_to_nftset(*nftsets_cur, &addr, flags, 0) == 0) |
| 1039 |
log_query((flags & (F_IPV4 | F_IPV6)) | F_IPSET, nftsets->domain, &addr, *nftsets_cur, 0); |
| 1040 |
} |
| 1041 |
#endif |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
if (insert) |
| 1046 |
{ |
| 1047 |
newc = cache_insert(name, &addr, C_IN, now, ttl, flags | F_FORWARD | secflag); |
| 1048 |
if (newc && cpp) |
| 1049 |
{ |
| 1050 |
next_uid(newc); |
| 1051 |
cpp->addr.cname.target.cache = newc; |
| 1052 |
cpp->addr.cname.uid = newc->uid; |
| 1053 |
} |
| 1054 |
cpp = NULL; |
| 1055 |
|
| 1056 |
/* cache insert failed, don't leak blockdata. */ |
| 1057 |
if (!newc && (flags & F_RR) && (flags & F_KEYTAG)) |
| 1058 |
blockdata_free(addr.rrblock.rrdata); |
| 1059 |
} |
| 1060 |
|
| 1061 |
/* We're filtering this RRtype. It will be removed from the |
| 1062 |
returned packet in process_reply() but gets cached here anyway |
| 1063 |
and will be filtered again on the way out of the cache. Here, |
| 1064 |
we just need to alter the logging. */ |
| 1065 |
if (qtype != T_ANY && rr_on_list(daemon->filter_rr, qtype)) |
| 1066 |
secflag = F_NEG | F_CONFIG; |
| 1067 |
|
| 1068 |
if (aqtype == T_TXT) |
| 1069 |
log_txt(name, p1, ardlen, flags | F_FORWARD | F_UPSTREAM | secflag); |
| 1070 |
else |
| 1071 |
log_query(flags | F_FORWARD | F_UPSTREAM | secflag, name, &addr, NULL, aqtype); |
| 1072 |
} |
| 1073 |
|
| 1074 |
p1 = endrr; |
| 1075 |
if (!CHECK_LEN(header, p1, qlen, 0)) |
| 1076 |
return 2; /* bad packet */ |
| 1077 |
} |
| 1078 |
|
| 1079 |
if (!found && (qtype != T_ANY || (flags & F_NXDOMAIN))) |
| 1080 |
{ |
| 1081 |
if (flags & F_NXDOMAIN) |
| 1082 |
{ |
| 1083 |
flags &= ~(F_IPV4 | F_IPV6 | F_RR); |
| 1084 |
|
| 1085 |
/* Can store NXDOMAIN reply for any qtype. */ |
| 1086 |
insert = 1; |
| 1087 |
} |
| 1088 |
|
| 1089 |
log_query(F_UPSTREAM | F_FORWARD | F_NEG | flags | (secure ? F_DNSSECOK : 0), name, NULL, NULL, 0); |
| 1090 |
|
| 1091 |
if (insert && !option_bool(OPT_NO_NEG)) |
| 1092 |
{ |
| 1093 |
/* The order of records going into the cache matters (see cache_recv_insert()). |
| 1094 |
The target of a CNAME must immediately follow the CNAME. |
| 1095 |
(CNAME has already gone into the cache at this point) |
| 1096 |
Here we call find_soa to get the ttl and substring, but |
| 1097 |
we DON'T LET IT INSERT the SOA into the cache if our negative record is a CNAME target |
| 1098 |
so that the SOA doesn't come before the CNAME target. |
| 1099 |
|
| 1100 |
We call find_soa() again after inserting the CNAME target to insert the SOA |
| 1101 |
if necessary. */ |
| 1102 |
|
| 1103 |
int substring, have_soa = find_soa(header, qlen, name, &substring, &ttl, cpp == NULL, now); |
| 1104 |
|
| 1105 |
if (have_soa || daemon->neg_ttl) |
| 1106 |
{ |
| 1107 |
if (have_soa) |
| 1108 |
{ |
| 1109 |
addr.rrdata.datalen = substring; |
| 1110 |
addr.rrdata.rrtype = qtype; |
| 1111 |
} |
| 1112 |
else |
| 1113 |
{ |
| 1114 |
/* If daemon->neg_ttl is set, we can cache even without an SOA. */ |
| 1115 |
ttl = daemon->neg_ttl; |
| 1116 |
flags |= F_NO_RR; /* Marks no SOA found. */ |
| 1117 |
} |
| 1118 |
|
| 1119 |
newc = cache_insert(name, &addr, C_IN, now, ttl, F_FORWARD | F_NEG | flags | (secure ? F_DNSSECOK : 0)); |
| 1120 |
if (newc && cpp) |
| 1121 |
{ |
| 1122 |
next_uid(newc); |
| 1123 |
cpp->addr.cname.target.cache = newc; |
| 1124 |
cpp->addr.cname.uid = newc->uid; |
| 1125 |
|
| 1126 |
/* we didn't insert the SOA before a CNAME target above, do it now. */ |
| 1127 |
if (have_soa) |
| 1128 |
find_soa(header, qlen, name, NULL, NULL, 1, now); |
| 1129 |
} |
| 1130 |
} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
} |
| 1134 |
|
| 1135 |
/* Don't cache replies from non-recursive nameservers, since we may get a |
| 1136 |
reply containing a CNAME but not its target, even though the target |
| 1137 |
does exist. */ |
| 1138 |
if (!(header->hb4 & HB4_CD) && |
| 1139 |
(header->hb4 & HB4_RA) && |
| 1140 |
!no_cache_dnssec) |
| 1141 |
cache_end_insert(); |
| 1142 |
|
| 1143 |
return 0; |
| 1144 |
} |
| 1145 |
|
| 1146 |
#if defined(HAVE_CONNTRACK) && defined(HAVE_UBUS) |
| 1147 |
/* Don't pass control chars and weird escapes to UBus. */ |
| 1148 |
static int safe_name(char *name) |
| 1149 |
{ |
| 1150 |
unsigned char *r; |
| 1151 |
|
| 1152 |
for (r = (unsigned char *)name; *r; r++) |
| 1153 |
if (!isprint((int)*r)) |
| 1154 |
return 0; |
| 1155 |
|
| 1156 |
return 1; |
| 1157 |
} |
| 1158 |
|
| 1159 |
void report_addresses(struct dns_header *header, size_t len, u32 mark) |
| 1160 |
{ |
| 1161 |
unsigned char *p, *endrr; |
| 1162 |
int i; |
| 1163 |
unsigned long attl; |
| 1164 |
struct allowlist *allowlists; |
| 1165 |
char **pattern_pos; |
| 1166 |
|
| 1167 |
if (RCODE(header) != NOERROR) |
| 1168 |
return; |
| 1169 |
|
| 1170 |
for (allowlists = daemon->allowlists; allowlists; allowlists = allowlists->next) |
| 1171 |
if (allowlists->mark == (mark & daemon->allowlist_mask & allowlists->mask)) |
| 1172 |
for (pattern_pos = allowlists->patterns; *pattern_pos; pattern_pos++) |
| 1173 |
if (!strcmp(*pattern_pos, "*")) |
| 1174 |
return; |
| 1175 |
|
| 1176 |
if (!(p = skip_questions(header, len))) |
| 1177 |
return; |
| 1178 |
for (i = ntohs(header->ancount); i != 0; i--) |
| 1179 |
{ |
| 1180 |
int aqtype, aqclass, ardlen; |
| 1181 |
|
| 1182 |
if (!extract_name(header, len, &p, daemon->namebuff, EXTR_NAME_EXTRACT, 10)) |
| 1183 |
return; |
| 1184 |
|
| 1185 |
if (!CHECK_LEN(header, p, len, 10)) |
| 1186 |
return; |
| 1187 |
GETSHORT(aqtype, p); |
| 1188 |
GETSHORT(aqclass, p); |
| 1189 |
GETLONG(attl, p); |
| 1190 |
GETSHORT(ardlen, p); |
| 1191 |
|
| 1192 |
if (!CHECK_LEN(header, p, len, ardlen)) |
| 1193 |
return; |
| 1194 |
endrr = p+ardlen; |
| 1195 |
|
| 1196 |
if (aqclass == C_IN) |
| 1197 |
{ |
| 1198 |
if (aqtype == T_CNAME) |
| 1199 |
{ |
| 1200 |
if (!extract_name(header, len, &p, daemon->workspacename, EXTR_NAME_EXTRACT, 0)) |
| 1201 |
return; |
| 1202 |
if (safe_name(daemon->namebuff) && safe_name(daemon->workspacename)) |
| 1203 |
ubus_event_bcast_connmark_allowlist_resolved(mark, daemon->namebuff, daemon->workspacename, attl); |
| 1204 |
} |
| 1205 |
if (aqtype == T_A) |
| 1206 |
{ |
| 1207 |
struct in_addr addr; |
| 1208 |
char ip[INET_ADDRSTRLEN]; |
| 1209 |
if (ardlen != INADDRSZ) |
| 1210 |
return; |
| 1211 |
memcpy(&addr, p, ardlen); |
| 1212 |
if (inet_ntop(AF_INET, &addr, ip, sizeof ip) && safe_name(daemon->namebuff)) |
| 1213 |
ubus_event_bcast_connmark_allowlist_resolved(mark, daemon->namebuff, ip, attl); |
| 1214 |
} |
| 1215 |
else if (aqtype == T_AAAA) |
| 1216 |
{ |
| 1217 |
struct in6_addr addr; |
| 1218 |
char ip[INET6_ADDRSTRLEN]; |
| 1219 |
if (ardlen != IN6ADDRSZ) |
| 1220 |
return; |
| 1221 |
memcpy(&addr, p, ardlen); |
| 1222 |
if (inet_ntop(AF_INET6, &addr, ip, sizeof ip) && safe_name(daemon->namebuff)) |
| 1223 |
ubus_event_bcast_connmark_allowlist_resolved(mark, daemon->namebuff, ip, attl); |
| 1224 |
} |
| 1225 |
} |
| 1226 |
|
| 1227 |
p = endrr; |
| 1228 |
} |
| 1229 |
} |
| 1230 |
#endif |
| 1231 |
|
| 1232 |
/* If the packet holds exactly one query |
| 1233 |
return F_IPV4 or F_IPV6 and leave the name from the query in name */ |
| 1234 |
unsigned int extract_request(struct dns_header *header, size_t qlen, char *name, |
| 1235 |
unsigned short *typep, unsigned short *classp) |
| 1236 |
{ |
| 1237 |
unsigned char *p = (unsigned char *)(header+1); |
| 1238 |
int qtype, qclass; |
| 1239 |
|
| 1240 |
if (typep) |
| 1241 |
*typep = 0; |
| 1242 |
|
| 1243 |
*name = 0; /* return empty name if no query found. */ |
| 1244 |
|
| 1245 |
if (ntohs(header->qdcount) != 1 || OPCODE(header) != QUERY) |
| 1246 |
return 0; /* must be exactly one query. */ |
| 1247 |
|
| 1248 |
if (!(header->hb3 & HB3_QR) && (ntohs(header->ancount) != 0 || ntohs(header->nscount) != 0)) |
| 1249 |
return 0; /* non-standard query. */ |
| 1250 |
|
| 1251 |
if (!extract_name(header, qlen, &p, name, EXTR_NAME_EXTRACT, 4)) |
| 1252 |
return 0; /* bad packet */ |
| 1253 |
|
| 1254 |
GETSHORT(qtype, p); |
| 1255 |
GETSHORT(qclass, p); |
| 1256 |
|
| 1257 |
if (typep) |
| 1258 |
*typep = qtype; |
| 1259 |
|
| 1260 |
if (classp) |
| 1261 |
*classp = qclass; |
| 1262 |
|
| 1263 |
if (qclass == C_IN) |
| 1264 |
{ |
| 1265 |
if (qtype == T_A) |
| 1266 |
return F_IPV4; |
| 1267 |
if (qtype == T_AAAA) |
| 1268 |
return F_IPV6; |
| 1269 |
if (qtype == T_ANY) |
| 1270 |
return F_IPV4 | F_IPV6; |
| 1271 |
} |
| 1272 |
|
| 1273 |
/* Make the behaviour for DS and DNSKEY queries we forward the same |
| 1274 |
as for DS and DNSKEY queries we originate. */ |
| 1275 |
if (qtype == T_DS || qtype == T_DNSKEY) |
| 1276 |
return F_DNSSECOK | (qtype == T_DS ? F_DS : 0); |
| 1277 |
|
| 1278 |
return F_QUERY; |
| 1279 |
} |
| 1280 |
|
| 1281 |
void setup_reply(struct dns_header *header, unsigned int flags, int ede) |
| 1282 |
{ |
| 1283 |
/* clear authoritative and truncated flags, set QR flag */ |
| 1284 |
header->hb3 = (header->hb3 & ~(HB3_AA | HB3_TC )) | HB3_QR; |
| 1285 |
/* clear AD flag, set RA flag */ |
| 1286 |
header->hb4 = (header->hb4 & ~HB4_AD) | HB4_RA; |
| 1287 |
|
| 1288 |
header->nscount = htons(0); |
| 1289 |
header->arcount = htons(0); |
| 1290 |
header->ancount = htons(0); /* no answers unless changed below */ |
| 1291 |
if (flags == F_NOERR) |
| 1292 |
SET_RCODE(header, NOERROR); /* empty domain */ |
| 1293 |
else if (flags == F_NXDOMAIN) |
| 1294 |
SET_RCODE(header, NXDOMAIN); |
| 1295 |
else if (flags == F_RCODE) |
| 1296 |
SET_RCODE(header, NOTIMP); |
| 1297 |
else if (flags & ( F_IPV4 | F_IPV6)) |
| 1298 |
{ |
| 1299 |
SET_RCODE(header, NOERROR); |
| 1300 |
header->hb3 |= HB3_AA; |
| 1301 |
} |
| 1302 |
else /* nowhere to forward to */ |
| 1303 |
{ |
| 1304 |
union all_addr a; |
| 1305 |
a.log.rcode = REFUSED; |
| 1306 |
a.log.ede = ede; |
| 1307 |
log_query(F_CONFIG | F_RCODE, "error", &a, NULL, 0); |
| 1308 |
SET_RCODE(header, REFUSED); |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
/* check if name matches local names ie from /etc/hosts or DHCP or local mx names. */ |
| 1313 |
int check_for_local_domain(char *name, time_t now) |
| 1314 |
{ |
| 1315 |
struct mx_srv_record *mx; |
| 1316 |
struct txt_record *txt; |
| 1317 |
struct interface_name *intr; |
| 1318 |
struct ptr_record *ptr; |
| 1319 |
struct naptr *naptr; |
| 1320 |
|
| 1321 |
for (naptr = daemon->naptr; naptr; naptr = naptr->next) |
| 1322 |
if (hostname_issubdomain(name, naptr->name)) |
| 1323 |
return 1; |
| 1324 |
|
| 1325 |
for (mx = daemon->mxnames; mx; mx = mx->next) |
| 1326 |
if (hostname_issubdomain(name, mx->name)) |
| 1327 |
return 1; |
| 1328 |
|
| 1329 |
for (txt = daemon->txt; txt; txt = txt->next) |
| 1330 |
if (hostname_issubdomain(name, txt->name)) |
| 1331 |
return 1; |
| 1332 |
|
| 1333 |
for (intr = daemon->int_names; intr; intr = intr->next) |
| 1334 |
if (hostname_issubdomain(name, intr->name)) |
| 1335 |
return 1; |
| 1336 |
|
| 1337 |
for (ptr = daemon->ptr; ptr; ptr = ptr->next) |
| 1338 |
if (hostname_issubdomain(name, ptr->name)) |
| 1339 |
return 1; |
| 1340 |
|
| 1341 |
if (cache_find_non_terminal(name, now)) |
| 1342 |
return 1; |
| 1343 |
|
| 1344 |
if (is_name_synthetic(F_IPV4, name, NULL) || |
| 1345 |
is_name_synthetic(F_IPV6, name, NULL)) |
| 1346 |
return 1; |
| 1347 |
|
| 1348 |
return 0; |
| 1349 |
} |
| 1350 |
|
| 1351 |
static int check_bad_address(struct dns_header *header, size_t qlen, struct bogus_addr *baddr, char *name, unsigned long *ttlp) |
| 1352 |
{ |
| 1353 |
unsigned char *p; |
| 1354 |
int i, qtype, qclass, rdlen; |
| 1355 |
unsigned long ttl; |
| 1356 |
struct bogus_addr *baddrp; |
| 1357 |
|
| 1358 |
/* skip over questions */ |
| 1359 |
if (!(p = skip_questions(header, qlen))) |
| 1360 |
return 0; /* bad packet */ |
| 1361 |
|
| 1362 |
for (i = ntohs(header->ancount); i != 0; i--) |
| 1363 |
{ |
| 1364 |
if (name && !extract_name(header, qlen, &p, name, EXTR_NAME_EXTRACT, 10)) |
| 1365 |
return 0; /* bad packet */ |
| 1366 |
|
| 1367 |
if (!name && !(p = skip_name(p, header, qlen, 10))) |
| 1368 |
return 0; |
| 1369 |
|
| 1370 |
GETSHORT(qtype, p); |
| 1371 |
GETSHORT(qclass, p); |
| 1372 |
GETLONG(ttl, p); |
| 1373 |
GETSHORT(rdlen, p); |
| 1374 |
if (ttlp) |
| 1375 |
*ttlp = ttl; |
| 1376 |
|
| 1377 |
if (qclass == C_IN) |
| 1378 |
{ |
| 1379 |
if (qtype == T_A) |
| 1380 |
{ |
| 1381 |
struct in_addr addr; |
| 1382 |
|
| 1383 |
if (!CHECK_LEN(header, p, qlen, INADDRSZ)) |
| 1384 |
return 0; |
| 1385 |
|
| 1386 |
memcpy(&addr, p, INADDRSZ); |
| 1387 |
|
| 1388 |
for (baddrp = baddr; baddrp; baddrp = baddrp->next) |
| 1389 |
if (!baddrp->is6 && is_same_net_prefix(addr, baddrp->addr.addr4, baddrp->prefix)) |
| 1390 |
return 1; |
| 1391 |
} |
| 1392 |
else if (qtype == T_AAAA) |
| 1393 |
{ |
| 1394 |
struct in6_addr addr; |
| 1395 |
|
| 1396 |
if (!CHECK_LEN(header, p, qlen, IN6ADDRSZ)) |
| 1397 |
return 0; |
| 1398 |
|
| 1399 |
memcpy(&addr, p, IN6ADDRSZ); |
| 1400 |
|
| 1401 |
for (baddrp = baddr; baddrp; baddrp = baddrp->next) |
| 1402 |
if (baddrp->is6 && is_same_net6(&addr, &baddrp->addr.addr6, baddrp->prefix)) |
| 1403 |
return 1; |
| 1404 |
} |
| 1405 |
} |
| 1406 |
|
| 1407 |
if (!ADD_RDLEN(header, p, qlen, rdlen)) |
| 1408 |
return 0; |
| 1409 |
} |
| 1410 |
|
| 1411 |
return 0; |
| 1412 |
} |
| 1413 |
|
| 1414 |
/* Is the packet a reply with the answer address equal to addr? |
| 1415 |
If so mung is into an NXDOMAIN reply and also put that information |
| 1416 |
in the cache. */ |
| 1417 |
int check_for_bogus_wildcard(struct dns_header *header, size_t qlen, char *name, time_t now) |
| 1418 |
{ |
| 1419 |
unsigned long ttl; |
| 1420 |
|
| 1421 |
if (check_bad_address(header, qlen, daemon->bogus_addr, name, &ttl)) |
| 1422 |
{ |
| 1423 |
/* Found a bogus address. Insert that info here, since there no SOA record |
| 1424 |
to get the ttl from in the normal processing */ |
| 1425 |
cache_start_insert(); |
| 1426 |
cache_insert(name, NULL, C_IN, now, ttl, F_FORWARD | F_NEG | F_NXDOMAIN); |
| 1427 |
cache_end_insert(); |
| 1428 |
log_query(F_CONFIG | F_FORWARD | F_NEG | F_NXDOMAIN, name, NULL, NULL, 0); |
| 1429 |
|
| 1430 |
return 1; |
| 1431 |
} |
| 1432 |
|
| 1433 |
return 0; |
| 1434 |
} |
| 1435 |
|
| 1436 |
int check_for_ignored_address(struct dns_header *header, size_t qlen) |
| 1437 |
{ |
| 1438 |
return check_bad_address(header, qlen, daemon->ignore_addr, NULL, NULL); |
| 1439 |
} |
| 1440 |
|
| 1441 |
/* Nameoffset > 0 means that the name of the new record already exists at the given offset, |
| 1442 |
so use a "jump" to that. |
| 1443 |
Nameoffset == 0 means use the first variable argument as the name of the new record. |
| 1444 |
nameoffset < 0 means use the first variable argument as the start of the new record name, |
| 1445 |
then "jump" to -nameoffset to complete it. */ |
| 1446 |
int add_resource_record(struct dns_header *header, char *limit, int *truncp, int nameoffset, unsigned char **pp, |
| 1447 |
unsigned long ttl, int *offset, unsigned short type, unsigned short class, char *format, ...) |
| 1448 |
{ |
| 1449 |
va_list ap; |
| 1450 |
unsigned char *sav, *p = *pp; |
| 1451 |
int j; |
| 1452 |
unsigned short usval; |
| 1453 |
long lval; |
| 1454 |
char *sval; |
| 1455 |
|
| 1456 |
#define CHECK_LIMIT(size) \ |
| 1457 |
if (limit && p + (size) > (unsigned char*)limit) goto truncated; |
| 1458 |
|
| 1459 |
va_start(ap, format); /* make ap point to 1st unamed argument */ |
| 1460 |
|
| 1461 |
if (truncp && *truncp) |
| 1462 |
goto truncated; |
| 1463 |
|
| 1464 |
if (nameoffset > 0) |
| 1465 |
{ |
| 1466 |
CHECK_LIMIT(2); |
| 1467 |
PUTSHORT(nameoffset | 0xc000, p); |
| 1468 |
} |
| 1469 |
else |
| 1470 |
{ |
| 1471 |
char *name = va_arg(ap, char *); |
| 1472 |
if (name && !(p = do_rfc1035_name(p, name, (unsigned char *)limit))) |
| 1473 |
goto truncated; |
| 1474 |
|
| 1475 |
if (nameoffset < 0) |
| 1476 |
{ |
| 1477 |
CHECK_LIMIT(2); |
| 1478 |
PUTSHORT(-nameoffset | 0xc000, p); |
| 1479 |
} |
| 1480 |
else |
| 1481 |
{ |
| 1482 |
CHECK_LIMIT(1); |
| 1483 |
*p++ = 0; |
| 1484 |
} |
| 1485 |
} |
| 1486 |
|
| 1487 |
/* type (2) + class (2) + ttl (4) + rdlen (2) */ |
| 1488 |
CHECK_LIMIT(10); |
| 1489 |
|
| 1490 |
PUTSHORT(type, p); |
| 1491 |
PUTSHORT(class, p); |
| 1492 |
PUTLONG(ttl, p); /* TTL */ |
| 1493 |
|
| 1494 |
sav = p; /* Save pointer to RDLength field */ |
| 1495 |
PUTSHORT(0, p); /* Placeholder RDLength */ |
| 1496 |
|
| 1497 |
for (; *format; format++) |
| 1498 |
switch (*format) |
| 1499 |
{ |
| 1500 |
case '6': |
| 1501 |
CHECK_LIMIT(IN6ADDRSZ); |
| 1502 |
sval = va_arg(ap, char *); |
| 1503 |
memcpy(p, sval, IN6ADDRSZ); |
| 1504 |
p += IN6ADDRSZ; |
| 1505 |
break; |
| 1506 |
|
| 1507 |
case '4': |
| 1508 |
CHECK_LIMIT(INADDRSZ); |
| 1509 |
sval = va_arg(ap, char *); |
| 1510 |
memcpy(p, sval, INADDRSZ); |
| 1511 |
p += INADDRSZ; |
| 1512 |
break; |
| 1513 |
|
| 1514 |
case 'b': |
| 1515 |
CHECK_LIMIT(1); |
| 1516 |
usval = va_arg(ap, int); |
| 1517 |
*p++ = usval; |
| 1518 |
break; |
| 1519 |
|
| 1520 |
case 's': |
| 1521 |
CHECK_LIMIT(2); |
| 1522 |
usval = va_arg(ap, int); |
| 1523 |
PUTSHORT(usval, p); |
| 1524 |
break; |
| 1525 |
|
| 1526 |
case 'l': |
| 1527 |
CHECK_LIMIT(4); |
| 1528 |
lval = va_arg(ap, long); |
| 1529 |
PUTLONG(lval, p); |
| 1530 |
break; |
| 1531 |
|
| 1532 |
case 'd': |
| 1533 |
/* get domain-name answer arg and store it in RDATA field */ |
| 1534 |
if (offset) |
| 1535 |
*offset = p - (unsigned char *)header; |
| 1536 |
if (!(p = do_rfc1035_name(p, va_arg(ap, char *), (unsigned char *)limit))) |
| 1537 |
goto truncated; |
| 1538 |
CHECK_LIMIT(1); |
| 1539 |
*p++ = 0; |
| 1540 |
break; |
| 1541 |
|
| 1542 |
case 't': |
| 1543 |
usval = va_arg(ap, int); |
| 1544 |
CHECK_LIMIT(usval); |
| 1545 |
sval = va_arg(ap, char *); |
| 1546 |
if (usval != 0) |
| 1547 |
memcpy(p, sval, usval); |
| 1548 |
p += usval; |
| 1549 |
break; |
| 1550 |
|
| 1551 |
case 'z': |
| 1552 |
sval = va_arg(ap, char *); |
| 1553 |
usval = sval ? strlen(sval) : 0; |
| 1554 |
if (usval > 255) |
| 1555 |
usval = 255; |
| 1556 |
CHECK_LIMIT(usval + 1); |
| 1557 |
*p++ = (unsigned char)usval; |
| 1558 |
memcpy(p, sval, usval); |
| 1559 |
p += usval; |
| 1560 |
break; |
| 1561 |
} |
| 1562 |
|
| 1563 |
va_end(ap); /* clean up variable argument pointer */ |
| 1564 |
|
| 1565 |
/* Now, store real RDLength. sav already checked against limit. */ |
| 1566 |
j = p - sav - 2; |
| 1567 |
PUTSHORT(j, sav); |
| 1568 |
|
| 1569 |
*pp = p; |
| 1570 |
return 1; |
| 1571 |
|
| 1572 |
truncated: |
| 1573 |
va_end(ap); |
| 1574 |
if (truncp) |
| 1575 |
*truncp = 1; |
| 1576 |
return 0; |
| 1577 |
|
| 1578 |
#undef CHECK_LIMIT |
| 1579 |
} |
| 1580 |
|
| 1581 |
static int crec_isstale(struct crec *crecp, time_t now) |
| 1582 |
{ |
| 1583 |
return (!(crecp->flags & F_IMMORTAL)) && difftime(crecp->ttd, now) < 0; |
| 1584 |
} |
| 1585 |
|
| 1586 |
static unsigned long crec_ttl(struct crec *crecp, time_t now) |
| 1587 |
{ |
| 1588 |
signed long ttl = difftime(crecp->ttd, now); |
| 1589 |
|
| 1590 |
/* Return 0 ttl for DHCP entries, which might change |
| 1591 |
before the lease expires, unless configured otherwise. */ |
| 1592 |
|
| 1593 |
if (crecp->flags & F_DHCP) |
| 1594 |
{ |
| 1595 |
int conf_ttl = daemon->use_dhcp_ttl ? daemon->dhcp_ttl : daemon->local_ttl; |
| 1596 |
|
| 1597 |
/* Apply ceiling of actual lease length to configured TTL. */ |
| 1598 |
if (!(crecp->flags & F_IMMORTAL) && ttl < conf_ttl) |
| 1599 |
return ttl; |
| 1600 |
|
| 1601 |
return conf_ttl; |
| 1602 |
} |
| 1603 |
|
| 1604 |
/* Immortal entries other than DHCP are local, and hold TTL in TTD field. */ |
| 1605 |
if (crecp->flags & F_IMMORTAL) |
| 1606 |
return crecp->ttd; |
| 1607 |
|
| 1608 |
/* Stale cache entries. */ |
| 1609 |
if (ttl < 0) |
| 1610 |
return 0; |
| 1611 |
|
| 1612 |
/* Return the Max TTL value if it is lower than the actual TTL */ |
| 1613 |
if (daemon->max_ttl == 0 || ((unsigned)ttl < daemon->max_ttl)) |
| 1614 |
return ttl; |
| 1615 |
else |
| 1616 |
return daemon->max_ttl; |
| 1617 |
} |
| 1618 |
|
| 1619 |
static int cache_not_validated(const struct crec *crecp) |
| 1620 |
{ |
| 1621 |
return (option_bool(OPT_DNSSEC_VALID) && !(crecp->flags & F_DNSSECOK)); |
| 1622 |
} |
| 1623 |
|
| 1624 |
/* return zero if we can't answer from cache, or packet size if we can */ |
| 1625 |
size_t answer_request(struct dns_header *header, char *limit, size_t qlen, |
| 1626 |
struct in_addr local_addr, struct in_addr local_netmask, |
| 1627 |
time_t now, int ad_reqd, int do_bit, int no_cache, int *stale, int *filtered) |
| 1628 |
{ |
| 1629 |
char *name = daemon->namebuff; |
| 1630 |
unsigned char *p, *ansp; |
| 1631 |
unsigned int qtype, qclass; |
| 1632 |
union all_addr addr; |
| 1633 |
int nameoffset; |
| 1634 |
unsigned short flag; |
| 1635 |
int ans, anscount = 0, nscount = 0, addncount = 0; |
| 1636 |
struct crec *crecp, *soa_lookup = NULL; |
| 1637 |
int nxdomain = 0, notimp = 0, auth = 1, trunc = 0, sec_data = 1; |
| 1638 |
struct mx_srv_record *rec; |
| 1639 |
size_t len; |
| 1640 |
int rd_bit = (header->hb3 & HB3_RD); |
| 1641 |
int count = 255; /* catch loops */ |
| 1642 |
|
| 1643 |
/* Suppress cached answers if no_cache set. */ |
| 1644 |
if (no_cache) |
| 1645 |
rd_bit = 0; |
| 1646 |
|
| 1647 |
if (stale) |
| 1648 |
*stale = 0; |
| 1649 |
|
| 1650 |
if (filtered) |
| 1651 |
*filtered = 0; |
| 1652 |
|
| 1653 |
if (ntohs(header->qdcount) != 1 || |
| 1654 |
ntohs(header->ancount) != 0 || |
| 1655 |
ntohs(header->nscount) != 0 || |
| 1656 |
OPCODE(header) != QUERY ) |
| 1657 |
return 0; |
| 1658 |
|
| 1659 |
/* Don't return AD set if checking disabled. */ |
| 1660 |
if (header->hb4 & HB4_CD) |
| 1661 |
sec_data = 0; |
| 1662 |
|
| 1663 |
for (rec = daemon->mxnames; rec; rec = rec->next) |
| 1664 |
rec->offset = 0; |
| 1665 |
|
| 1666 |
/* determine end of question section (we put answers there) */ |
| 1667 |
if (!(ansp = skip_questions(header, qlen))) |
| 1668 |
return 0; /* bad packet */ |
| 1669 |
|
| 1670 |
/* now process each question, answers go in RRs after the question */ |
| 1671 |
p = (unsigned char *)(header+1); |
| 1672 |
|
| 1673 |
/* save pointer to name for copying into answers */ |
| 1674 |
nameoffset = p - (unsigned char *)header; |
| 1675 |
|
| 1676 |
/* now extract name as .-concatenated string into name */ |
| 1677 |
if (!extract_name(header, qlen, &p, name, EXTR_NAME_EXTRACT, 4)) |
| 1678 |
return 0; /* bad packet */ |
| 1679 |
|
| 1680 |
GETSHORT(qtype, p); |
| 1681 |
GETSHORT(qclass, p); |
| 1682 |
|
| 1683 |
ans = 0; /* have we answered this question */ |
| 1684 |
|
| 1685 |
if (qclass == C_IN) |
| 1686 |
while (--count != 0 && (crecp = cache_find_by_name(NULL, name, now, F_CNAME | F_NXDOMAIN))) |
| 1687 |
{ |
| 1688 |
char *cname_target; |
| 1689 |
int stale_flag = 0; |
| 1690 |
|
| 1691 |
if (crec_isstale(crecp, now)) |
| 1692 |
{ |
| 1693 |
if (stale) |
| 1694 |
*stale = 1; |
| 1695 |
|
| 1696 |
stale_flag = F_STALE; |
| 1697 |
} |
| 1698 |
|
| 1699 |
if (crecp->flags & F_NEG) |
| 1700 |
soa_lookup = crecp; |
| 1701 |
|
| 1702 |
if (crecp->flags & F_NXDOMAIN) |
| 1703 |
{ |
| 1704 |
if (qtype == T_CNAME) |
| 1705 |
{ |
| 1706 |
log_query(stale_flag | crecp->flags, name, NULL, record_source(crecp->uid), 0); |
| 1707 |
auth = 0; |
| 1708 |
nxdomain = 1; |
| 1709 |
ans = 1; |
| 1710 |
} |
| 1711 |
break; |
| 1712 |
} |
| 1713 |
|
| 1714 |
cname_target = cache_get_cname_target(crecp); |
| 1715 |
|
| 1716 |
/* If the client asked for DNSSEC don't use cached data. */ |
| 1717 |
if ((crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) || |
| 1718 |
(rd_bit && (!do_bit || cache_not_validated(crecp)))) |
| 1719 |
{ |
| 1720 |
if (crecp->flags & F_CONFIG || qtype == T_CNAME) |
| 1721 |
ans = 1; |
| 1722 |
|
| 1723 |
if (!(crecp->flags & F_DNSSECOK)) |
| 1724 |
sec_data = 0; |
| 1725 |
|
| 1726 |
log_query(stale_flag | crecp->flags, name, NULL, record_source(crecp->uid), 0); |
| 1727 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1728 |
crec_ttl(crecp, now), &nameoffset, |
| 1729 |
T_CNAME, C_IN, "d", cname_target)) |
| 1730 |
anscount++; |
| 1731 |
} |
| 1732 |
else |
| 1733 |
return 0; /* give up if any cached CNAME in chain can't be used for DNSSEC reasons. */ |
| 1734 |
|
| 1735 |
if (qtype == T_CNAME) |
| 1736 |
break; |
| 1737 |
|
| 1738 |
strcpy(name, cname_target); |
| 1739 |
} |
| 1740 |
|
| 1741 |
if (qtype == T_TXT || qtype == T_ANY) |
| 1742 |
{ |
| 1743 |
struct txt_record *t; |
| 1744 |
for(t = daemon->txt; t ; t = t->next) |
| 1745 |
{ |
| 1746 |
if (t->class == qclass && hostname_isequal(name, t->name)) |
| 1747 |
{ |
| 1748 |
unsigned long ttl = daemon->local_ttl; |
| 1749 |
int ok = 1; |
| 1750 |
|
| 1751 |
ans = 1, sec_data = 0; |
| 1752 |
#ifndef NO_ID |
| 1753 |
/* Dynamically generate stat record */ |
| 1754 |
if (t->stat != 0) |
| 1755 |
{ |
| 1756 |
ttl = 0; |
| 1757 |
if (!cache_make_stat(t)) |
| 1758 |
ok = 0; |
| 1759 |
} |
| 1760 |
#endif |
| 1761 |
if (ok) |
| 1762 |
{ |
| 1763 |
log_query(F_CONFIG | F_RRNAME, name, NULL, "<TXT>", 0); |
| 1764 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1765 |
ttl, NULL, |
| 1766 |
T_TXT, t->class, "t", t->len, t->txt)) |
| 1767 |
anscount++; |
| 1768 |
} |
| 1769 |
} |
| 1770 |
} |
| 1771 |
} |
| 1772 |
|
| 1773 |
if (qclass == C_CHAOS) |
| 1774 |
{ |
| 1775 |
/* don't forward *.bind and *.server chaos queries - always reply with NOTIMP */ |
| 1776 |
if (hostname_issubdomain("bind", name) || hostname_issubdomain("server", name)) |
| 1777 |
{ |
| 1778 |
if (!ans) |
| 1779 |
{ |
| 1780 |
notimp = 1, auth = 0; |
| 1781 |
|
| 1782 |
addr.log.rcode = NOTIMP; |
| 1783 |
log_query(F_CONFIG | F_RCODE, name, &addr, NULL, 0); |
| 1784 |
|
| 1785 |
ans = 1, sec_data = 0; |
| 1786 |
} |
| 1787 |
} |
| 1788 |
} |
| 1789 |
|
| 1790 |
if (qclass == C_IN) |
| 1791 |
{ |
| 1792 |
struct txt_record *t; |
| 1793 |
|
| 1794 |
for (t = daemon->rr; t; t = t->next) |
| 1795 |
if ((t->class == qtype || qtype == T_ANY) && hostname_isequal(name, t->name)) |
| 1796 |
{ |
| 1797 |
ans = 1; |
| 1798 |
sec_data = 0; |
| 1799 |
log_query(F_CONFIG | F_RRNAME, name, NULL, NULL, t->class); |
| 1800 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1801 |
daemon->local_ttl, NULL, |
| 1802 |
t->class, C_IN, "t", t->len, t->txt)) |
| 1803 |
anscount++; |
| 1804 |
} |
| 1805 |
|
| 1806 |
if (qtype == T_PTR || qtype == T_ANY) |
| 1807 |
{ |
| 1808 |
/* see if it's w.z.y.z.in-addr.arpa format */ |
| 1809 |
int is_arpa = in_arpa_name_2_addr(name, &addr); |
| 1810 |
struct ptr_record *ptr; |
| 1811 |
struct interface_name* intr = NULL; |
| 1812 |
|
| 1813 |
for (ptr = daemon->ptr; ptr; ptr = ptr->next) |
| 1814 |
if (hostname_isequal(name, ptr->name)) |
| 1815 |
break; |
| 1816 |
|
| 1817 |
if (is_arpa == F_IPV4) |
| 1818 |
for (intr = daemon->int_names; intr; intr = intr->next) |
| 1819 |
{ |
| 1820 |
struct addrlist *addrlist; |
| 1821 |
|
| 1822 |
for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
| 1823 |
if (!(addrlist->flags & ADDRLIST_IPV6) && addr.addr4.s_addr == addrlist->addr.addr4.s_addr) |
| 1824 |
break; |
| 1825 |
|
| 1826 |
if (addrlist) |
| 1827 |
break; |
| 1828 |
else if (!(intr->flags & INP4)) |
| 1829 |
while (intr->next && strcmp(intr->intr, intr->next->intr) == 0) |
| 1830 |
intr = intr->next; |
| 1831 |
} |
| 1832 |
else if (is_arpa == F_IPV6) |
| 1833 |
for (intr = daemon->int_names; intr; intr = intr->next) |
| 1834 |
{ |
| 1835 |
struct addrlist *addrlist; |
| 1836 |
|
| 1837 |
for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
| 1838 |
if ((addrlist->flags & ADDRLIST_IPV6) && IN6_ARE_ADDR_EQUAL(&addr.addr6, &addrlist->addr.addr6)) |
| 1839 |
break; |
| 1840 |
|
| 1841 |
if (addrlist) |
| 1842 |
break; |
| 1843 |
else if (!(intr->flags & INP6)) |
| 1844 |
while (intr->next && strcmp(intr->intr, intr->next->intr) == 0) |
| 1845 |
intr = intr->next; |
| 1846 |
} |
| 1847 |
|
| 1848 |
if (intr) |
| 1849 |
{ |
| 1850 |
sec_data = 0; |
| 1851 |
ans = 1; |
| 1852 |
log_query(is_arpa | F_REVERSE | F_CONFIG, intr->name, &addr, NULL, 0); |
| 1853 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1854 |
daemon->local_ttl, NULL, |
| 1855 |
T_PTR, C_IN, "d", intr->name)) |
| 1856 |
anscount++; |
| 1857 |
} |
| 1858 |
else if (ptr) |
| 1859 |
{ |
| 1860 |
ans = 1; |
| 1861 |
sec_data = 0; |
| 1862 |
log_query(F_CONFIG | F_RRNAME, name, NULL, "<PTR>", 0); |
| 1863 |
for (ptr = daemon->ptr; ptr; ptr = ptr->next) |
| 1864 |
if (hostname_isequal(name, ptr->name) && |
| 1865 |
add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1866 |
daemon->local_ttl, NULL, |
| 1867 |
T_PTR, C_IN, "d", ptr->ptr)) |
| 1868 |
anscount++; |
| 1869 |
|
| 1870 |
} |
| 1871 |
else if (is_arpa && (crecp = cache_find_by_addr(NULL, &addr, now, is_arpa))) |
| 1872 |
{ |
| 1873 |
/* Don't use cache when DNSSEC data required, unless we know that |
| 1874 |
the zone is unsigned, which implies that we're doing |
| 1875 |
validation. */ |
| 1876 |
if ((crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) || |
| 1877 |
(rd_bit && (!do_bit || cache_not_validated(crecp)) )) |
| 1878 |
{ |
| 1879 |
do |
| 1880 |
{ |
| 1881 |
int stale_flag = 0; |
| 1882 |
|
| 1883 |
if (crec_isstale(crecp, now)) |
| 1884 |
{ |
| 1885 |
if (stale) |
| 1886 |
*stale = 1; |
| 1887 |
|
| 1888 |
stale_flag = F_STALE; |
| 1889 |
} |
| 1890 |
|
| 1891 |
/* don't answer wildcard queries with data not from /etc/hosts or dhcp leases */ |
| 1892 |
if (qtype == T_ANY && !(crecp->flags & (F_HOSTS | F_DHCP))) |
| 1893 |
continue; |
| 1894 |
|
| 1895 |
if (!(crecp->flags & F_DNSSECOK)) |
| 1896 |
sec_data = 0; |
| 1897 |
|
| 1898 |
ans = 1; |
| 1899 |
|
| 1900 |
if (crecp->flags & F_NEG) |
| 1901 |
{ |
| 1902 |
auth = 0; |
| 1903 |
if (crecp->flags & F_NXDOMAIN) |
| 1904 |
nxdomain = 1; |
| 1905 |
log_query(stale_flag | (crecp->flags & ~F_FORWARD), name, &addr, NULL, 0); |
| 1906 |
soa_lookup = crecp; |
| 1907 |
} |
| 1908 |
else |
| 1909 |
{ |
| 1910 |
if (!(crecp->flags & (F_HOSTS | F_DHCP))) |
| 1911 |
auth = 0; |
| 1912 |
|
| 1913 |
log_query(stale_flag | (crecp->flags & ~F_FORWARD), cache_get_name(crecp), &addr, |
| 1914 |
record_source(crecp->uid), 0); |
| 1915 |
|
| 1916 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1917 |
crec_ttl(crecp, now), NULL, |
| 1918 |
T_PTR, C_IN, "d", cache_get_name(crecp))) |
| 1919 |
anscount++; |
| 1920 |
} |
| 1921 |
} while ((crecp = cache_find_by_addr(crecp, &addr, now, is_arpa))); |
| 1922 |
} |
| 1923 |
} |
| 1924 |
else if (is_rev_synth(is_arpa, &addr, name)) |
| 1925 |
{ |
| 1926 |
ans = 1; |
| 1927 |
sec_data = 0; |
| 1928 |
log_query(F_CONFIG | F_REVERSE | is_arpa, name, &addr, NULL, 0); |
| 1929 |
|
| 1930 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1931 |
daemon->local_ttl, NULL, |
| 1932 |
T_PTR, C_IN, "d", name)) |
| 1933 |
anscount++; |
| 1934 |
} |
| 1935 |
else if (option_bool(OPT_BOGUSPRIV) && |
| 1936 |
((is_arpa == F_IPV6 && private_net6(&addr.addr6, 1)) || (is_arpa == F_IPV4 && private_net(addr.addr4, 1))) && |
| 1937 |
!lookup_domain(name, F_DOMAINSRV, NULL, NULL)) |
| 1938 |
{ |
| 1939 |
/* if no configured server, not in cache, enabled and private IPV4 address, return NXDOMAIN */ |
| 1940 |
ans = 1; |
| 1941 |
sec_data = 0; |
| 1942 |
nxdomain = 1; |
| 1943 |
log_query(F_CONFIG | F_REVERSE | is_arpa | F_NEG | F_NXDOMAIN, |
| 1944 |
name, &addr, NULL, 0); |
| 1945 |
} |
| 1946 |
} |
| 1947 |
|
| 1948 |
for (flag = F_IPV4; flag; flag = (flag == F_IPV4) ? F_IPV6 : 0) |
| 1949 |
{ |
| 1950 |
unsigned short type = (flag == F_IPV6) ? T_AAAA : T_A; |
| 1951 |
struct interface_name *intr; |
| 1952 |
|
| 1953 |
if (qtype != type && qtype != T_ANY) |
| 1954 |
continue; |
| 1955 |
|
| 1956 |
/* interface name stuff */ |
| 1957 |
for (intr = daemon->int_names; intr; intr = intr->next) |
| 1958 |
if (hostname_isequal(name, intr->name)) |
| 1959 |
break; |
| 1960 |
|
| 1961 |
if (intr) |
| 1962 |
{ |
| 1963 |
struct addrlist *addrlist; |
| 1964 |
int gotit = 0, localise = 0; |
| 1965 |
|
| 1966 |
enumerate_interfaces(0); |
| 1967 |
|
| 1968 |
/* See if a putative address is on the network from which we received |
| 1969 |
the query, is so we'll filter other answers. */ |
| 1970 |
if (local_addr.s_addr != 0 && option_bool(OPT_LOCALISE) && type == T_A) |
| 1971 |
for (intr = daemon->int_names; intr; intr = intr->next) |
| 1972 |
if (hostname_isequal(name, intr->name)) |
| 1973 |
for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
| 1974 |
if (!(addrlist->flags & ADDRLIST_IPV6) && |
| 1975 |
is_same_net(addrlist->addr.addr4, local_addr, local_netmask)) |
| 1976 |
{ |
| 1977 |
localise = 1; |
| 1978 |
break; |
| 1979 |
} |
| 1980 |
|
| 1981 |
for (intr = daemon->int_names; intr; intr = intr->next) |
| 1982 |
if (hostname_isequal(name, intr->name)) |
| 1983 |
{ |
| 1984 |
for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
| 1985 |
if (((addrlist->flags & ADDRLIST_IPV6) ? T_AAAA : T_A) == type) |
| 1986 |
{ |
| 1987 |
if (localise && |
| 1988 |
!is_same_net(addrlist->addr.addr4, local_addr, local_netmask)) |
| 1989 |
continue; |
| 1990 |
|
| 1991 |
if (addrlist->flags & ADDRLIST_REVONLY) |
| 1992 |
continue; |
| 1993 |
|
| 1994 |
ans = 1; |
| 1995 |
sec_data = 0; |
| 1996 |
gotit = 1; |
| 1997 |
log_query(F_FORWARD | F_CONFIG | flag, name, &addrlist->addr, NULL, 0); |
| 1998 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1999 |
daemon->local_ttl, NULL, type, C_IN, |
| 2000 |
type == T_A ? "4" : "6", &addrlist->addr)) |
| 2001 |
anscount++; |
| 2002 |
} |
| 2003 |
} |
| 2004 |
|
| 2005 |
if (!gotit) |
| 2006 |
log_query(F_FORWARD | F_CONFIG | flag | F_NEG, name, NULL, NULL, 0); |
| 2007 |
|
| 2008 |
continue; |
| 2009 |
} |
| 2010 |
|
| 2011 |
if ((crecp = cache_find_by_name(NULL, name, now, flag))) |
| 2012 |
{ |
| 2013 |
int localise = 0; |
| 2014 |
|
| 2015 |
/* See if a putative address is on the network from which we received |
| 2016 |
the query, is so we'll filter other answers. */ |
| 2017 |
if (!(crecp->flags & F_NEG) && local_addr.s_addr != 0 && option_bool(OPT_LOCALISE) && flag == F_IPV4) |
| 2018 |
{ |
| 2019 |
struct crec *save = crecp; |
| 2020 |
do { |
| 2021 |
if ((crecp->flags & F_HOSTS) && |
| 2022 |
is_same_net(crecp->addr.addr4, local_addr, local_netmask)) |
| 2023 |
{ |
| 2024 |
localise = 1; |
| 2025 |
break; |
| 2026 |
} |
| 2027 |
} while ((crecp = cache_find_by_name(crecp, name, now, flag))); |
| 2028 |
crecp = save; |
| 2029 |
} |
| 2030 |
|
| 2031 |
/* If the client asked for DNSSEC don't use cached data. */ |
| 2032 |
if ((crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) || |
| 2033 |
(rd_bit && (!do_bit || cache_not_validated(crecp)) )) |
| 2034 |
do |
| 2035 |
{ |
| 2036 |
int stale_flag = 0; |
| 2037 |
|
| 2038 |
if (crec_isstale(crecp, now)) |
| 2039 |
{ |
| 2040 |
if (stale) |
| 2041 |
*stale = 1; |
| 2042 |
|
| 2043 |
stale_flag = F_STALE; |
| 2044 |
} |
| 2045 |
|
| 2046 |
/* don't answer wildcard queries with data not from /etc/hosts |
| 2047 |
or DHCP leases */ |
| 2048 |
if (qtype == T_ANY && !(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG))) |
| 2049 |
break; |
| 2050 |
|
| 2051 |
if (!(crecp->flags & F_DNSSECOK)) |
| 2052 |
sec_data = 0; |
| 2053 |
|
| 2054 |
if (!(crecp->flags & (F_HOSTS | F_DHCP))) |
| 2055 |
auth = 0; |
| 2056 |
|
| 2057 |
if (qtype != T_ANY && rr_on_list(daemon->filter_rr, qtype) && |
| 2058 |
!(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG | F_NEG))) |
| 2059 |
{ |
| 2060 |
/* We have a cached answer but we're filtering it. */ |
| 2061 |
ans = 1; |
| 2062 |
sec_data = 0; |
| 2063 |
|
| 2064 |
log_query(F_NEG | F_CONFIG | flag, name, NULL, NULL, 0); |
| 2065 |
|
| 2066 |
if (filtered) |
| 2067 |
*filtered = 1; |
| 2068 |
} |
| 2069 |
else if (crecp->flags & F_NEG) |
| 2070 |
{ |
| 2071 |
if (qtype != T_ANY) |
| 2072 |
{ |
| 2073 |
ans = 1; |
| 2074 |
auth = 0; |
| 2075 |
soa_lookup = crecp; |
| 2076 |
if (crecp->flags & F_NXDOMAIN) |
| 2077 |
nxdomain = 1; |
| 2078 |
|
| 2079 |
log_query(stale_flag | crecp->flags, name, NULL, NULL, 0); |
| 2080 |
} |
| 2081 |
} |
| 2082 |
else |
| 2083 |
{ |
| 2084 |
/* If we are returning local answers depending on network, |
| 2085 |
filter here. */ |
| 2086 |
if (localise && |
| 2087 |
(crecp->flags & F_HOSTS) && |
| 2088 |
!is_same_net(crecp->addr.addr4, local_addr, local_netmask)) |
| 2089 |
continue; |
| 2090 |
|
| 2091 |
ans = 1; |
| 2092 |
log_query(stale_flag | (crecp->flags & ~F_REVERSE), name, &crecp->addr, |
| 2093 |
record_source(crecp->uid), 0); |
| 2094 |
|
| 2095 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 2096 |
crec_ttl(crecp, now), NULL, type, C_IN, |
| 2097 |
type == T_A ? "4" : "6", &crecp->addr)) |
| 2098 |
anscount++; |
| 2099 |
} |
| 2100 |
} while ((crecp = cache_find_by_name(crecp, name, now, flag))); |
| 2101 |
} |
| 2102 |
else if (is_name_synthetic(flag, name, &addr)) |
| 2103 |
{ |
| 2104 |
ans = 1, sec_data = 0; |
| 2105 |
log_query(F_FORWARD | F_CONFIG | flag, name, &addr, NULL, 0); |
| 2106 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 2107 |
daemon->local_ttl, NULL, type, C_IN, type == T_A ? "4" : "6", &addr)) |
| 2108 |
anscount++; |
| 2109 |
} |
| 2110 |
} |
| 2111 |
|
| 2112 |
if (qtype == T_MX || qtype == T_ANY) |
| 2113 |
{ |
| 2114 |
int found = 0; |
| 2115 |
for (rec = daemon->mxnames; rec; rec = rec->next) |
| 2116 |
if (!rec->issrv && hostname_isequal(name, rec->name)) |
| 2117 |
{ |
| 2118 |
int offset; |
| 2119 |
|
| 2120 |
ans = found = 1; |
| 2121 |
sec_data = 0; |
| 2122 |
|
| 2123 |
log_query(F_CONFIG | F_RRNAME, name, NULL, "<MX>", 0); |
| 2124 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->local_ttl, |
| 2125 |
&offset, T_MX, C_IN, "sd", rec->weight, rec->target)) |
| 2126 |
{ |
| 2127 |
anscount++; |
| 2128 |
if (rec->target) |
| 2129 |
rec->offset = offset; |
| 2130 |
} |
| 2131 |
} |
| 2132 |
|
| 2133 |
if (!found && (option_bool(OPT_SELFMX) || option_bool(OPT_LOCALMX)) && |
| 2134 |
cache_find_by_name(NULL, name, now, F_HOSTS | F_DHCP | F_NO_RR)) |
| 2135 |
{ |
| 2136 |
ans = 1; |
| 2137 |
sec_data = 0; |
| 2138 |
log_query(F_CONFIG | F_RRNAME, name, NULL, "<MX>", 0); |
| 2139 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->local_ttl, NULL, |
| 2140 |
T_MX, C_IN, "sd", 1, |
| 2141 |
option_bool(OPT_SELFMX) ? name : daemon->mxtarget)) |
| 2142 |
anscount++; |
| 2143 |
} |
| 2144 |
} |
| 2145 |
|
| 2146 |
if (qtype == T_SRV || qtype == T_ANY) |
| 2147 |
{ |
| 2148 |
struct mx_srv_record *move = NULL, **up = &daemon->mxnames; |
| 2149 |
|
| 2150 |
for (rec = daemon->mxnames; rec; rec = rec->next) |
| 2151 |
if (rec->issrv && hostname_isequal(name, rec->name)) |
| 2152 |
{ |
| 2153 |
int offset; |
| 2154 |
|
| 2155 |
ans = 1; |
| 2156 |
sec_data = 0; |
| 2157 |
log_query(F_CONFIG | F_RRNAME, name, NULL, "<SRV>", 0); |
| 2158 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->local_ttl, |
| 2159 |
&offset, T_SRV, C_IN, "sssd", |
| 2160 |
rec->priority, rec->weight, rec->srvport, rec->target)) |
| 2161 |
{ |
| 2162 |
anscount++; |
| 2163 |
if (rec->target) |
| 2164 |
rec->offset = offset; |
| 2165 |
} |
| 2166 |
|
| 2167 |
/* unlink first SRV record found */ |
| 2168 |
if (!move) |
| 2169 |
{ |
| 2170 |
move = rec; |
| 2171 |
*up = rec->next; |
| 2172 |
} |
| 2173 |
else |
| 2174 |
up = &rec->next; |
| 2175 |
} |
| 2176 |
else |
| 2177 |
up = &rec->next; |
| 2178 |
|
| 2179 |
/* put first SRV record back at the end. */ |
| 2180 |
if (move) |
| 2181 |
{ |
| 2182 |
*up = move; |
| 2183 |
move->next = NULL; |
| 2184 |
} |
| 2185 |
} |
| 2186 |
|
| 2187 |
if (qtype == T_NAPTR || qtype == T_ANY) |
| 2188 |
{ |
| 2189 |
struct naptr *na; |
| 2190 |
for (na = daemon->naptr; na; na = na->next) |
| 2191 |
if (hostname_isequal(name, na->name)) |
| 2192 |
{ |
| 2193 |
ans = 1; |
| 2194 |
sec_data = 0; |
| 2195 |
log_query(F_CONFIG | F_RRNAME, name, NULL, "<NAPTR>", 0); |
| 2196 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->local_ttl, |
| 2197 |
NULL, T_NAPTR, C_IN, "sszzzd", |
| 2198 |
na->order, na->pref, na->flags, na->services, na->regexp, na->replace)) |
| 2199 |
anscount++; |
| 2200 |
} |
| 2201 |
} |
| 2202 |
|
| 2203 |
if (qtype == T_MAILB) |
| 2204 |
ans = 1, nxdomain = 1, sec_data = 0; |
| 2205 |
|
| 2206 |
if (qtype == T_SOA && option_bool(OPT_FILTER)) |
| 2207 |
{ |
| 2208 |
ans = 1; |
| 2209 |
sec_data = 0; |
| 2210 |
log_query(F_CONFIG | F_NEG, name, &addr, NULL, 0); |
| 2211 |
} |
| 2212 |
|
| 2213 |
if (!ans) |
| 2214 |
{ |
| 2215 |
if ((crecp = cache_find_by_name(NULL, name, now, F_RR | F_NXDOMAIN)) && rd_bit) |
| 2216 |
do |
| 2217 |
{ |
| 2218 |
int flags = crecp->flags; |
| 2219 |
unsigned short rrtype; |
| 2220 |
|
| 2221 |
if (flags & F_KEYTAG) |
| 2222 |
rrtype = crecp->addr.rrblock.rrtype; |
| 2223 |
else |
| 2224 |
rrtype = crecp->addr.rrdata.rrtype; |
| 2225 |
|
| 2226 |
if (((flags & F_NXDOMAIN) || rrtype == qtype) && |
| 2227 |
(!do_bit || cache_not_validated(crecp))) |
| 2228 |
{ |
| 2229 |
char *rrdata = NULL; |
| 2230 |
unsigned short rrlen = 0; |
| 2231 |
|
| 2232 |
if (crec_isstale(crecp, now)) |
| 2233 |
{ |
| 2234 |
if (stale) |
| 2235 |
*stale = 1; |
| 2236 |
|
| 2237 |
flags |= F_STALE; |
| 2238 |
} |
| 2239 |
|
| 2240 |
if (!(flags & F_DNSSECOK)) |
| 2241 |
sec_data = 0; |
| 2242 |
|
| 2243 |
if (flags & F_NXDOMAIN) |
| 2244 |
nxdomain = 1; |
| 2245 |
else if (qtype != T_ANY && rr_on_list(daemon->filter_rr, qtype)) |
| 2246 |
flags |= F_NEG | F_CONFIG; |
| 2247 |
|
| 2248 |
auth = 0; |
| 2249 |
ans = 1; |
| 2250 |
|
| 2251 |
if (flags & F_NEG) |
| 2252 |
soa_lookup = crecp; |
| 2253 |
|
| 2254 |
if (!(flags & F_NEG)) |
| 2255 |
{ |
| 2256 |
if (flags & F_KEYTAG) |
| 2257 |
{ |
| 2258 |
rrlen = crecp->addr.rrblock.datalen; |
| 2259 |
rrdata = blockdata_retrieve(crecp->addr.rrblock.rrdata, crecp->addr.rrblock.datalen, NULL); |
| 2260 |
} |
| 2261 |
else |
| 2262 |
{ |
| 2263 |
rrlen = crecp->addr.rrdata.datalen; |
| 2264 |
rrdata = crecp->addr.rrdata.data; |
| 2265 |
} |
| 2266 |
} |
| 2267 |
|
| 2268 |
if (!(flags & F_NEG) && add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 2269 |
crec_ttl(crecp, now), NULL, qtype, C_IN, "t", |
| 2270 |
rrlen, rrdata)) |
| 2271 |
anscount++; |
| 2272 |
|
| 2273 |
/* log after cache insertion as log_txt mangles rrdata */ |
| 2274 |
if (qtype == T_TXT && !(flags & F_NEG)) |
| 2275 |
log_txt(name, (unsigned char *)rrdata, rrlen, flags & (F_DNSSECOK | F_STALE)); |
| 2276 |
else |
| 2277 |
log_query(flags, name, &crecp->addr, NULL, 0); |
| 2278 |
} |
| 2279 |
} while ((crecp = cache_find_by_name(crecp, name, now, F_RR))); |
| 2280 |
} |
| 2281 |
|
| 2282 |
if (!ans && option_bool(OPT_FILTER) && (qtype == T_SRV || (qtype == T_ANY && strchr(name, '_')))) |
| 2283 |
{ |
| 2284 |
ans = 1; |
| 2285 |
sec_data = 0; |
| 2286 |
log_query(F_CONFIG | F_NEG, name, NULL, NULL, 0); |
| 2287 |
} |
| 2288 |
|
| 2289 |
|
| 2290 |
if (qtype != T_ANY && !ans && rr_on_list(daemon->filter_rr, qtype) && !do_bit) |
| 2291 |
{ |
| 2292 |
/* We don't have a cached answer and when we get an answer from upstream we're going to |
| 2293 |
filter it anyway. If we have a cached answer for the domain for another RRtype then |
| 2294 |
that may be enough to tell us if the answer should be NODATA and save the round trip. |
| 2295 |
Cached NXDOMAIN has already been handled, so here we look for any record for the domain, |
| 2296 |
since its existence allows us to return a NODATA answer. Note that we never set the AD flag, |
| 2297 |
since we didn't authenticate the record; this doesn't work if we want auth data, so |
| 2298 |
don't use this shortcut in that case. */ |
| 2299 |
|
| 2300 |
if (cache_find_by_name(NULL, name, now, F_IPV4 | F_IPV6 | F_RR | F_CNAME)) |
| 2301 |
{ |
| 2302 |
ans = 1; |
| 2303 |
sec_data = auth = 0; |
| 2304 |
|
| 2305 |
log_query(F_NEG | F_CONFIG | flag, name, NULL, NULL, 0); |
| 2306 |
|
| 2307 |
if (filtered) |
| 2308 |
*filtered = 1; |
| 2309 |
} |
| 2310 |
} |
| 2311 |
} |
| 2312 |
|
| 2313 |
if (!ans) |
| 2314 |
return 0; /* failed to answer a question */ |
| 2315 |
|
| 2316 |
/* We found a negative record. See if we have an SOA record to |
| 2317 |
return in the AUTH section. |
| 2318 |
|
| 2319 |
For FORWARD NEG records, the addr.rrdata.datalen field of the othewise |
| 2320 |
empty addr is used to held an offset in to the name which yields the SOA |
| 2321 |
name. |
| 2322 |
If the F_NO_RR flag is set, there was no SOA record supplied with the RR. */ |
| 2323 |
if (soa_lookup && !(soa_lookup->flags & F_NO_RR)) |
| 2324 |
{ |
| 2325 |
char *soa_name = name + soa_lookup->addr.rrdata.datalen; |
| 2326 |
|
| 2327 |
crecp = NULL; |
| 2328 |
while ((crecp = cache_find_by_name(crecp, soa_name, now, F_RR))) |
| 2329 |
if (crecp->addr.rrblock.rrtype == T_SOA) |
| 2330 |
{ |
| 2331 |
char *rrdata; |
| 2332 |
|
| 2333 |
if (!(crecp->flags & F_NEG) && |
| 2334 |
(rrdata = blockdata_retrieve(crecp->addr.rrblock.rrdata, crecp->addr.rrblock.datalen, NULL)) && |
| 2335 |
add_resource_record(header, limit, &trunc, 0, &ansp, |
| 2336 |
crec_ttl(crecp, now), NULL, T_SOA, C_IN, "t", |
| 2337 |
soa_name, crecp->addr.rrblock.datalen, rrdata)) |
| 2338 |
{ |
| 2339 |
nscount++; |
| 2340 |
|
| 2341 |
if (!(crecp->flags & F_DNSSECOK)) |
| 2342 |
sec_data = 0; |
| 2343 |
} |
| 2344 |
break; |
| 2345 |
} |
| 2346 |
} |
| 2347 |
|
| 2348 |
/* create an additional data section, for stuff in SRV and MX record replies. */ |
| 2349 |
for (rec = daemon->mxnames; rec; rec = rec->next) |
| 2350 |
if (rec->offset != 0) |
| 2351 |
{ |
| 2352 |
/* squash dupes */ |
| 2353 |
struct mx_srv_record *tmp; |
| 2354 |
for (tmp = rec->next; tmp; tmp = tmp->next) |
| 2355 |
if (tmp->offset != 0 && hostname_isequal(rec->target, tmp->target)) |
| 2356 |
tmp->offset = 0; |
| 2357 |
|
| 2358 |
crecp = NULL; |
| 2359 |
while ((crecp = cache_find_by_name(crecp, rec->target, now, F_IPV4 | F_IPV6))) |
| 2360 |
{ |
| 2361 |
int type = crecp->flags & F_IPV4 ? T_A : T_AAAA; |
| 2362 |
|
| 2363 |
if (crecp->flags & F_NEG) |
| 2364 |
continue; |
| 2365 |
|
| 2366 |
if (add_resource_record(header, limit, NULL, rec->offset, &ansp, |
| 2367 |
crec_ttl(crecp, now), NULL, type, C_IN, |
| 2368 |
crecp->flags & F_IPV4 ? "4" : "6", &crecp->addr)) |
| 2369 |
{ |
| 2370 |
addncount++; |
| 2371 |
if (!(crecp->flags & F_DNSSECOK)) |
| 2372 |
sec_data = 0; |
| 2373 |
} |
| 2374 |
} |
| 2375 |
} |
| 2376 |
|
| 2377 |
/* done all questions, set up header and return length of result */ |
| 2378 |
/* clear authoritative and truncated flags, set QR flag */ |
| 2379 |
header->hb3 = (header->hb3 & ~(HB3_AA | HB3_TC)) | HB3_QR; |
| 2380 |
/* set RA flag */ |
| 2381 |
header->hb4 |= HB4_RA; |
| 2382 |
|
| 2383 |
/* authoritative - only hosts and DHCP derived names. */ |
| 2384 |
if (auth) |
| 2385 |
header->hb3 |= HB3_AA; |
| 2386 |
|
| 2387 |
/* truncation */ |
| 2388 |
if (trunc) |
| 2389 |
{ |
| 2390 |
header->hb3 |= HB3_TC; |
| 2391 |
if (!(ansp = skip_questions(header, qlen))) |
| 2392 |
return 0; /* bad packet */ |
| 2393 |
anscount = nscount = addncount = 0; |
| 2394 |
log_query(0, "reply", NULL, "truncated", 0); |
| 2395 |
} |
| 2396 |
|
| 2397 |
if (nxdomain) |
| 2398 |
SET_RCODE(header, NXDOMAIN); |
| 2399 |
else if (notimp) |
| 2400 |
SET_RCODE(header, NOTIMP); |
| 2401 |
else |
| 2402 |
SET_RCODE(header, NOERROR); /* no error */ |
| 2403 |
|
| 2404 |
header->ancount = htons(anscount); |
| 2405 |
header->nscount = htons(nscount); |
| 2406 |
header->arcount = htons(addncount); |
| 2407 |
|
| 2408 |
len = ansp - (unsigned char *)header; |
| 2409 |
|
| 2410 |
if (ad_reqd && sec_data) |
| 2411 |
header->hb4 |= HB4_AD; |
| 2412 |
else |
| 2413 |
header->hb4 &= ~HB4_AD; |
| 2414 |
|
| 2415 |
return len; |
| 2416 |
} |