| 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 |
#ifdef HAVE_AUTH |
| 20 |
|
| 21 |
static struct addrlist *find_addrlist(struct addrlist *list, int flag, union all_addr *addr_u) |
| 22 |
{ |
| 23 |
do { |
| 24 |
if (!(list->flags & ADDRLIST_IPV6)) |
| 25 |
{ |
| 26 |
struct in_addr netmask, addr = addr_u->addr4; |
| 27 |
|
| 28 |
if (!(flag & F_IPV4)) |
| 29 |
continue; |
| 30 |
|
| 31 |
netmask.s_addr = htonl(~(in_addr_t)0 << (32 - list->prefixlen)); |
| 32 |
|
| 33 |
if (is_same_net(addr, list->addr.addr4, netmask)) |
| 34 |
return list; |
| 35 |
} |
| 36 |
else if (is_same_net6(&(addr_u->addr6), &list->addr.addr6, list->prefixlen)) |
| 37 |
return list; |
| 38 |
|
| 39 |
} while ((list = list->next)); |
| 40 |
|
| 41 |
return NULL; |
| 42 |
} |
| 43 |
|
| 44 |
static struct addrlist *find_subnet(struct auth_zone *zone, int flag, union all_addr *addr_u) |
| 45 |
{ |
| 46 |
if (!zone->subnet) |
| 47 |
return NULL; |
| 48 |
|
| 49 |
return find_addrlist(zone->subnet, flag, addr_u); |
| 50 |
} |
| 51 |
|
| 52 |
static struct addrlist *find_exclude(struct auth_zone *zone, int flag, union all_addr *addr_u) |
| 53 |
{ |
| 54 |
if (!zone->exclude) |
| 55 |
return NULL; |
| 56 |
|
| 57 |
return find_addrlist(zone->exclude, flag, addr_u); |
| 58 |
} |
| 59 |
|
| 60 |
static int filter_zone(struct auth_zone *zone, int flag, union all_addr *addr_u) |
| 61 |
{ |
| 62 |
if (find_exclude(zone, flag, addr_u)) |
| 63 |
return 0; |
| 64 |
|
| 65 |
/* No subnets specified, no filter */ |
| 66 |
if (!zone->subnet) |
| 67 |
return 1; |
| 68 |
|
| 69 |
return find_subnet(zone, flag, addr_u) != NULL; |
| 70 |
} |
| 71 |
|
| 72 |
int in_zone(struct auth_zone *zone, char *name, char **cut) |
| 73 |
{ |
| 74 |
size_t namelen = strlen(name); |
| 75 |
size_t domainlen = strlen(zone->domain); |
| 76 |
|
| 77 |
if (cut) |
| 78 |
*cut = NULL; |
| 79 |
|
| 80 |
if (namelen >= domainlen && |
| 81 |
hostname_isequal(zone->domain, &name[namelen - domainlen])) |
| 82 |
{ |
| 83 |
|
| 84 |
if (namelen == domainlen) |
| 85 |
return 1; |
| 86 |
|
| 87 |
if (name[namelen - domainlen - 1] == '.') |
| 88 |
{ |
| 89 |
if (cut) |
| 90 |
*cut = &name[namelen - domainlen - 1]; |
| 91 |
return 1; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
size_t answer_auth(struct dns_header *header, char *limit, size_t qlen, time_t now, |
| 100 |
union mysockaddr *peer_addr, int local_query) |
| 101 |
{ |
| 102 |
char *name = daemon->namebuff; |
| 103 |
unsigned char *p, *ansp; |
| 104 |
int qtype, qclass, rc; |
| 105 |
int nameoffset, axfroffset = 0; |
| 106 |
int anscount = 0, authcount = 0; |
| 107 |
struct crec *crecp; |
| 108 |
int auth = !local_query, trunc = 0, nxdomain = 1, soa = 0, ns = 0, axfr = 0, out_of_zone = 0, notimp = 0; |
| 109 |
struct auth_zone *zone = NULL; |
| 110 |
struct addrlist *subnet = NULL; |
| 111 |
char *cut; |
| 112 |
struct mx_srv_record *rec, *move, **up; |
| 113 |
struct txt_record *txt; |
| 114 |
struct interface_name *intr; |
| 115 |
struct naptr *na; |
| 116 |
union all_addr addr; |
| 117 |
struct cname *a, *candidate; |
| 118 |
unsigned int wclen; |
| 119 |
unsigned int log_flags = local_query ? 0 : F_NOERR; |
| 120 |
|
| 121 |
if (ntohs(header->qdcount) != 1) |
| 122 |
return 0; |
| 123 |
|
| 124 |
/* determine end of question section (we put answers there) */ |
| 125 |
if (!(ansp = skip_questions(header, qlen))) |
| 126 |
return 0; /* bad packet */ |
| 127 |
|
| 128 |
p = (unsigned char *)(header+1); |
| 129 |
|
| 130 |
if (OPCODE(header) != QUERY) |
| 131 |
notimp = 1; |
| 132 |
else |
| 133 |
{ |
| 134 |
unsigned int flag = 0; |
| 135 |
int found = 0; |
| 136 |
int cname_wildcard = 0; |
| 137 |
|
| 138 |
/* save pointer to name for copying into answers */ |
| 139 |
nameoffset = p - (unsigned char *)header; |
| 140 |
|
| 141 |
/* now extract name as .-concatenated string into name */ |
| 142 |
if (!extract_name(header, qlen, &p, name, EXTR_NAME_EXTRACT, 4)) |
| 143 |
return 0; /* bad packet */ |
| 144 |
|
| 145 |
GETSHORT(qtype, p); |
| 146 |
GETSHORT(qclass, p); |
| 147 |
|
| 148 |
if (qclass != C_IN) |
| 149 |
{ |
| 150 |
auth = 0; |
| 151 |
out_of_zone = 1; |
| 152 |
goto done; |
| 153 |
} |
| 154 |
|
| 155 |
if ((qtype == T_PTR || qtype == T_SOA || qtype == T_NS) && |
| 156 |
(flag = in_arpa_name_2_addr(name, &addr)) && |
| 157 |
!local_query) |
| 158 |
{ |
| 159 |
for (zone = daemon->auth_zones; zone; zone = zone->next) |
| 160 |
if ((subnet = find_subnet(zone, flag, &addr))) |
| 161 |
break; |
| 162 |
|
| 163 |
if (!zone) |
| 164 |
{ |
| 165 |
out_of_zone = 1; |
| 166 |
auth = 0; |
| 167 |
goto done; |
| 168 |
} |
| 169 |
else if (qtype == T_SOA) |
| 170 |
soa = 1, found = 1; |
| 171 |
else if (qtype == T_NS) |
| 172 |
ns = 1, found = 1; |
| 173 |
} |
| 174 |
|
| 175 |
if (qtype == T_PTR && flag) |
| 176 |
{ |
| 177 |
intr = NULL; |
| 178 |
|
| 179 |
if (flag == F_IPV4) |
| 180 |
for (intr = daemon->int_names; intr; intr = intr->next) |
| 181 |
{ |
| 182 |
struct addrlist *addrlist; |
| 183 |
|
| 184 |
for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
| 185 |
if (!(addrlist->flags & ADDRLIST_IPV6) && addr.addr4.s_addr == addrlist->addr.addr4.s_addr) |
| 186 |
break; |
| 187 |
|
| 188 |
if (addrlist) |
| 189 |
break; |
| 190 |
else |
| 191 |
while (intr->next && strcmp(intr->intr, intr->next->intr) == 0) |
| 192 |
intr = intr->next; |
| 193 |
} |
| 194 |
else if (flag == F_IPV6) |
| 195 |
for (intr = daemon->int_names; intr; intr = intr->next) |
| 196 |
{ |
| 197 |
struct addrlist *addrlist; |
| 198 |
|
| 199 |
for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
| 200 |
if ((addrlist->flags & ADDRLIST_IPV6) && IN6_ARE_ADDR_EQUAL(&addr.addr6, &addrlist->addr.addr6)) |
| 201 |
break; |
| 202 |
|
| 203 |
if (addrlist) |
| 204 |
break; |
| 205 |
else |
| 206 |
while (intr->next && strcmp(intr->intr, intr->next->intr) == 0) |
| 207 |
intr = intr->next; |
| 208 |
} |
| 209 |
|
| 210 |
if (intr) |
| 211 |
{ |
| 212 |
if (local_query || in_zone(zone, intr->name, NULL)) |
| 213 |
{ |
| 214 |
found = 1; |
| 215 |
log_query(log_flags | flag | F_REVERSE | F_CONFIG, intr->name, &addr, NULL, 0); |
| 216 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 217 |
daemon->auth_ttl, NULL, |
| 218 |
T_PTR, C_IN, "d", intr->name)) |
| 219 |
anscount++; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
if ((crecp = cache_find_by_addr(NULL, &addr, now, flag))) |
| 224 |
do { |
| 225 |
strcpy(name, cache_get_name(crecp)); |
| 226 |
|
| 227 |
if (crecp->flags & F_DHCP && !option_bool(OPT_DHCP_FQDN)) |
| 228 |
{ |
| 229 |
char *p = strchr(name, '.'); |
| 230 |
if (p) |
| 231 |
*p = 0; /* must be bare name */ |
| 232 |
|
| 233 |
/* add external domain */ |
| 234 |
if (zone) |
| 235 |
{ |
| 236 |
strcat(name, "."); |
| 237 |
strcat(name, zone->domain); |
| 238 |
} |
| 239 |
log_query(log_flags | flag | F_DHCP | F_REVERSE, name, &addr, record_source(crecp->uid), 0); |
| 240 |
found = 1; |
| 241 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 242 |
daemon->auth_ttl, NULL, |
| 243 |
T_PTR, C_IN, "d", name)) |
| 244 |
anscount++; |
| 245 |
} |
| 246 |
else if (crecp->flags & (F_DHCP | F_HOSTS) && (local_query || in_zone(zone, name, NULL))) |
| 247 |
{ |
| 248 |
log_query(log_flags | (crecp->flags & ~F_FORWARD), name, &addr, record_source(crecp->uid), 0); |
| 249 |
found = 1; |
| 250 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 251 |
daemon->auth_ttl, NULL, |
| 252 |
T_PTR, C_IN, "d", name)) |
| 253 |
anscount++; |
| 254 |
} |
| 255 |
else |
| 256 |
continue; |
| 257 |
|
| 258 |
} while ((crecp = cache_find_by_addr(crecp, &addr, now, flag))); |
| 259 |
|
| 260 |
if (!found && is_rev_synth(flag, &addr, name) && (local_query || in_zone(zone, name, NULL))) |
| 261 |
{ |
| 262 |
log_query(log_flags | F_CONFIG | F_REVERSE | flag, name, &addr, NULL, 0); |
| 263 |
found = 1; |
| 264 |
|
| 265 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 266 |
daemon->auth_ttl, NULL, |
| 267 |
T_PTR, C_IN, "d", name)) |
| 268 |
anscount++; |
| 269 |
} |
| 270 |
|
| 271 |
if (found) |
| 272 |
nxdomain = 0; |
| 273 |
else |
| 274 |
log_query(log_flags | flag | F_NEG | F_NXDOMAIN | F_REVERSE | (auth ? F_AUTH : 0), NULL, &addr, NULL, 0); |
| 275 |
|
| 276 |
goto done; |
| 277 |
} |
| 278 |
|
| 279 |
cname_restart: |
| 280 |
if (found) |
| 281 |
/* NS and SOA .arpa requests have set found above. */ |
| 282 |
cut = NULL; |
| 283 |
else |
| 284 |
{ |
| 285 |
for (zone = daemon->auth_zones; zone; zone = zone->next) |
| 286 |
if (in_zone(zone, name, &cut)) |
| 287 |
break; |
| 288 |
|
| 289 |
if (!zone) |
| 290 |
{ |
| 291 |
out_of_zone = 1; |
| 292 |
auth = 0; |
| 293 |
goto done; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
for (rec = daemon->mxnames; rec; rec = rec->next) |
| 298 |
if (!rec->issrv && (rc = hostname_issubdomain(name, rec->name))) |
| 299 |
{ |
| 300 |
nxdomain = 0; |
| 301 |
|
| 302 |
if (rc == 2 && qtype == T_MX) |
| 303 |
{ |
| 304 |
found = 1; |
| 305 |
log_query(log_flags | F_CONFIG | F_RRNAME, name, NULL, "<MX>", 0); |
| 306 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->auth_ttl, |
| 307 |
NULL, T_MX, C_IN, "sd", rec->weight, rec->target)) |
| 308 |
anscount++; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
for (move = NULL, up = &daemon->mxnames, rec = daemon->mxnames; rec; rec = rec->next) |
| 313 |
if (rec->issrv && (rc = hostname_issubdomain(name, rec->name))) |
| 314 |
{ |
| 315 |
nxdomain = 0; |
| 316 |
|
| 317 |
if (rc == 2 && qtype == T_SRV) |
| 318 |
{ |
| 319 |
found = 1; |
| 320 |
log_query(log_flags | F_CONFIG | F_RRNAME, name, NULL, "<SRV>", 0); |
| 321 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->auth_ttl, |
| 322 |
NULL, T_SRV, C_IN, "sssd", |
| 323 |
rec->priority, rec->weight, rec->srvport, rec->target)) |
| 324 |
|
| 325 |
anscount++; |
| 326 |
} |
| 327 |
|
| 328 |
/* unlink first SRV record found */ |
| 329 |
if (!move) |
| 330 |
{ |
| 331 |
move = rec; |
| 332 |
*up = rec->next; |
| 333 |
} |
| 334 |
else |
| 335 |
up = &rec->next; |
| 336 |
} |
| 337 |
else |
| 338 |
up = &rec->next; |
| 339 |
|
| 340 |
/* put first SRV record back at the end. */ |
| 341 |
if (move) |
| 342 |
{ |
| 343 |
*up = move; |
| 344 |
move->next = NULL; |
| 345 |
} |
| 346 |
|
| 347 |
for (txt = daemon->rr; txt; txt = txt->next) |
| 348 |
if ((rc = hostname_issubdomain(name, txt->name))) |
| 349 |
{ |
| 350 |
nxdomain = 0; |
| 351 |
if (rc == 2 && txt->class == qtype) |
| 352 |
{ |
| 353 |
found = 1; |
| 354 |
log_query(log_flags | F_CONFIG | F_RRNAME, name, NULL, NULL, txt->class); |
| 355 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->auth_ttl, |
| 356 |
NULL, txt->class, C_IN, "t", txt->len, txt->txt)) |
| 357 |
anscount++; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
for (txt = daemon->txt; txt; txt = txt->next) |
| 362 |
if (txt->class == C_IN && (rc = hostname_issubdomain(name, txt->name))) |
| 363 |
{ |
| 364 |
nxdomain = 0; |
| 365 |
if (rc == 2 && qtype == T_TXT) |
| 366 |
{ |
| 367 |
found = 1; |
| 368 |
log_query(log_flags | F_CONFIG | F_RRNAME, name, NULL, "<TXT>", 0); |
| 369 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->auth_ttl, |
| 370 |
NULL, T_TXT, C_IN, "t", txt->len, txt->txt)) |
| 371 |
anscount++; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
for (na = daemon->naptr; na; na = na->next) |
| 376 |
if ((rc = hostname_issubdomain(name, na->name))) |
| 377 |
{ |
| 378 |
nxdomain = 0; |
| 379 |
if (rc == 2 && qtype == T_NAPTR) |
| 380 |
{ |
| 381 |
found = 1; |
| 382 |
log_query(log_flags | F_CONFIG | F_RRNAME, name, NULL, "<NAPTR>", 0); |
| 383 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->auth_ttl, |
| 384 |
NULL, T_NAPTR, C_IN, "sszzzd", |
| 385 |
na->order, na->pref, na->flags, na->services, na->regexp, na->replace)) |
| 386 |
anscount++; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
if (qtype == T_A) |
| 391 |
flag = F_IPV4; |
| 392 |
|
| 393 |
if (qtype == T_AAAA) |
| 394 |
flag = F_IPV6; |
| 395 |
|
| 396 |
for (intr = daemon->int_names; intr; intr = intr->next) |
| 397 |
if ((rc = hostname_issubdomain(name, intr->name))) |
| 398 |
{ |
| 399 |
struct addrlist *addrlist; |
| 400 |
|
| 401 |
nxdomain = 0; |
| 402 |
|
| 403 |
if (rc == 2 && flag) |
| 404 |
for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
| 405 |
if (((addrlist->flags & ADDRLIST_IPV6) ? T_AAAA : T_A) == qtype && |
| 406 |
(local_query || filter_zone(zone, flag, &addrlist->addr))) |
| 407 |
{ |
| 408 |
if (addrlist->flags & ADDRLIST_REVONLY) |
| 409 |
continue; |
| 410 |
|
| 411 |
found = 1; |
| 412 |
log_query(log_flags | F_FORWARD | F_CONFIG | flag, name, &addrlist->addr, NULL, 0); |
| 413 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 414 |
daemon->auth_ttl, NULL, qtype, C_IN, |
| 415 |
qtype == T_A ? "4" : "6", &addrlist->addr)) |
| 416 |
anscount++; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
if (!found && is_name_synthetic(flag, name, &addr) ) |
| 421 |
{ |
| 422 |
nxdomain = 0; |
| 423 |
|
| 424 |
log_query(log_flags | F_FORWARD | F_CONFIG | flag, name, &addr, NULL, 0); |
| 425 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 426 |
daemon->auth_ttl, NULL, qtype, C_IN, qtype == T_A ? "4" : "6", &addr)) |
| 427 |
anscount++; |
| 428 |
} |
| 429 |
|
| 430 |
if (!cut) |
| 431 |
{ |
| 432 |
nxdomain = 0; |
| 433 |
|
| 434 |
if (qtype == T_SOA) |
| 435 |
{ |
| 436 |
auth = soa = 1; /* inhibits auth section */ |
| 437 |
log_query(log_flags | F_RRNAME | F_AUTH, zone->domain, NULL, "<SOA>", 0); |
| 438 |
} |
| 439 |
else if (qtype == T_AXFR) |
| 440 |
{ |
| 441 |
struct iname *peers; |
| 442 |
|
| 443 |
if (peer_addr->sa.sa_family == AF_INET) |
| 444 |
peer_addr->in.sin_port = 0; |
| 445 |
else |
| 446 |
{ |
| 447 |
peer_addr->in6.sin6_port = 0; |
| 448 |
peer_addr->in6.sin6_scope_id = 0; |
| 449 |
} |
| 450 |
|
| 451 |
for (peers = daemon->auth_peers; peers; peers = peers->next) |
| 452 |
if (sockaddr_isequal(peer_addr, &peers->addr)) |
| 453 |
break; |
| 454 |
|
| 455 |
/* Refuse all AXFR unless --auth-sec-servers or auth-peers is set */ |
| 456 |
if ((!daemon->secondary_forward_server && !daemon->auth_peers) || |
| 457 |
(daemon->auth_peers && !peers)) |
| 458 |
{ |
| 459 |
if (peer_addr->sa.sa_family == AF_INET) |
| 460 |
inet_ntop(AF_INET, &peer_addr->in.sin_addr, daemon->addrbuff, ADDRSTRLEN); |
| 461 |
else |
| 462 |
inet_ntop(AF_INET6, &peer_addr->in6.sin6_addr, daemon->addrbuff, ADDRSTRLEN); |
| 463 |
|
| 464 |
my_syslog(LOG_WARNING, _("ignoring zone transfer request from %s"), daemon->addrbuff); |
| 465 |
return 0; |
| 466 |
} |
| 467 |
|
| 468 |
auth = 1; |
| 469 |
soa = 1; /* inhibits auth section */ |
| 470 |
ns = 1; /* ensure we include NS records! */ |
| 471 |
axfr = 1; |
| 472 |
axfroffset = nameoffset; |
| 473 |
log_query(log_flags | F_RRNAME | F_AUTH, zone->domain, NULL, "<AXFR>", 0); |
| 474 |
} |
| 475 |
else if (qtype == T_NS) |
| 476 |
{ |
| 477 |
auth = 1; |
| 478 |
ns = 1; /* inhibits auth section */ |
| 479 |
log_query(log_flags | F_RRNAME | F_AUTH, zone->domain, NULL, "<NS>", 0); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
if (!option_bool(OPT_DHCP_FQDN) && cut) |
| 484 |
{ |
| 485 |
*cut = 0; /* remove domain part */ |
| 486 |
|
| 487 |
if (!strchr(name, '.') && (crecp = cache_find_by_name(NULL, name, now, F_IPV4 | F_IPV6))) |
| 488 |
{ |
| 489 |
if (crecp->flags & F_DHCP) |
| 490 |
do |
| 491 |
{ |
| 492 |
nxdomain = 0; |
| 493 |
if ((crecp->flags & flag) && |
| 494 |
(local_query || filter_zone(zone, flag, &(crecp->addr)))) |
| 495 |
{ |
| 496 |
*cut = '.'; /* restore domain part */ |
| 497 |
log_query(log_flags | crecp->flags, name, &crecp->addr, record_source(crecp->uid), 0); |
| 498 |
*cut = 0; /* remove domain part */ |
| 499 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 500 |
daemon->auth_ttl, NULL, qtype, C_IN, |
| 501 |
qtype == T_A ? "4" : "6", &crecp->addr)) |
| 502 |
anscount++; |
| 503 |
} |
| 504 |
} while ((crecp = cache_find_by_name(crecp, name, now, F_IPV4 | F_IPV6))); |
| 505 |
} |
| 506 |
|
| 507 |
*cut = '.'; /* restore domain part */ |
| 508 |
} |
| 509 |
|
| 510 |
if ((crecp = cache_find_by_name(NULL, name, now, F_IPV4 | F_IPV6))) |
| 511 |
{ |
| 512 |
if ((crecp->flags & F_HOSTS) || (((crecp->flags & F_DHCP) && option_bool(OPT_DHCP_FQDN)))) |
| 513 |
do |
| 514 |
{ |
| 515 |
nxdomain = 0; |
| 516 |
if ((crecp->flags & flag) && (local_query || filter_zone(zone, flag, &(crecp->addr)))) |
| 517 |
{ |
| 518 |
log_query(log_flags | (crecp->flags & ~F_REVERSE), name, &crecp->addr, record_source(crecp->uid), 0); |
| 519 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 520 |
daemon->auth_ttl, NULL, qtype, C_IN, |
| 521 |
qtype == T_A ? "4" : "6", &crecp->addr)) |
| 522 |
anscount++; |
| 523 |
} |
| 524 |
} while ((crecp = cache_find_by_name(crecp, name, now, F_IPV4 | F_IPV6))); |
| 525 |
} |
| 526 |
|
| 527 |
/* Only supply CNAME if no record for any type is known. */ |
| 528 |
if (nxdomain) |
| 529 |
{ |
| 530 |
/* Check for possible wildcard match against *.domain |
| 531 |
return length of match, to get longest. |
| 532 |
Note that if return length of wildcard section, so |
| 533 |
we match b.simon to _both_ *.simon and b.simon |
| 534 |
but return a longer (better) match to b.simon. |
| 535 |
*/ |
| 536 |
for (wclen = 0, candidate = NULL, a = daemon->cnames; a; a = a->next) |
| 537 |
if (a->alias[0] == '*') |
| 538 |
{ |
| 539 |
char *test = name; |
| 540 |
|
| 541 |
while ((test = strchr(test+1, '.'))) |
| 542 |
{ |
| 543 |
if (hostname_isequal(test, &(a->alias[1]))) |
| 544 |
{ |
| 545 |
if (strlen(test) > wclen && !cname_wildcard) |
| 546 |
{ |
| 547 |
wclen = strlen(test); |
| 548 |
candidate = a; |
| 549 |
cname_wildcard = 1; |
| 550 |
} |
| 551 |
break; |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
} |
| 556 |
else if (hostname_isequal(a->alias, name) && strlen(a->alias) > wclen) |
| 557 |
{ |
| 558 |
/* Simple case, no wildcard */ |
| 559 |
wclen = strlen(a->alias); |
| 560 |
candidate = a; |
| 561 |
} |
| 562 |
|
| 563 |
if (candidate) |
| 564 |
{ |
| 565 |
log_query(log_flags | F_CONFIG | F_CNAME, name, NULL, NULL, 0); |
| 566 |
strcpy(name, candidate->target); |
| 567 |
if (!strchr(name, '.')) |
| 568 |
{ |
| 569 |
strcat(name, "."); |
| 570 |
strcat(name, zone->domain); |
| 571 |
} |
| 572 |
found = 1; |
| 573 |
if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 574 |
daemon->auth_ttl, &nameoffset, |
| 575 |
T_CNAME, C_IN, "d", name)) |
| 576 |
anscount++; |
| 577 |
|
| 578 |
goto cname_restart; |
| 579 |
} |
| 580 |
else if (cache_find_non_terminal(name, now)) |
| 581 |
nxdomain = 0; |
| 582 |
|
| 583 |
log_query(log_flags | flag | F_NEG | (nxdomain ? F_NXDOMAIN : 0) | F_FORWARD | F_AUTH, name, NULL, NULL, 0); |
| 584 |
} |
| 585 |
|
| 586 |
} |
| 587 |
|
| 588 |
done: |
| 589 |
|
| 590 |
/* Add auth section */ |
| 591 |
if (auth && zone) |
| 592 |
{ |
| 593 |
char *authname; |
| 594 |
int newoffset = ansp - (unsigned char *)header, offset = 0; |
| 595 |
|
| 596 |
if (!subnet) |
| 597 |
authname = zone->domain; |
| 598 |
else |
| 599 |
{ |
| 600 |
/* handle NS and SOA for PTR records */ |
| 601 |
|
| 602 |
authname = name; |
| 603 |
|
| 604 |
if (!(subnet->flags & ADDRLIST_IPV6)) |
| 605 |
{ |
| 606 |
in_addr_t a = ntohl(subnet->addr.addr4.s_addr) >> 8; |
| 607 |
char *p = name; |
| 608 |
|
| 609 |
if (subnet->prefixlen >= 24) |
| 610 |
p += sprintf(p, "%u.", a & 0xff); |
| 611 |
a = a >> 8; |
| 612 |
if (subnet->prefixlen >= 16 ) |
| 613 |
p += sprintf(p, "%u.", a & 0xff); |
| 614 |
a = a >> 8; |
| 615 |
sprintf(p, "%u.in-addr.arpa", a & 0xff); |
| 616 |
|
| 617 |
} |
| 618 |
else |
| 619 |
{ |
| 620 |
char *p = name; |
| 621 |
int i; |
| 622 |
|
| 623 |
for (i = subnet->prefixlen-1; i >= 0; i -= 4) |
| 624 |
{ |
| 625 |
int dig = ((unsigned char *)&subnet->addr.addr6)[i>>3]; |
| 626 |
p += sprintf(p, "%.1x.", (i>>2) & 1 ? dig & 15 : dig >> 4); |
| 627 |
} |
| 628 |
sprintf(p, "ip6.arpa"); |
| 629 |
|
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
/* handle NS and SOA in auth section or for explicit queries */ |
| 634 |
if (((anscount == 0 && !ns) || soa) && |
| 635 |
add_resource_record(header, limit, &trunc, 0, &ansp, |
| 636 |
daemon->auth_ttl, NULL, T_SOA, C_IN, "ddlllll", |
| 637 |
authname, daemon->authserver, daemon->hostmaster, |
| 638 |
daemon->soa_sn, daemon->soa_refresh, |
| 639 |
daemon->soa_retry, daemon->soa_expiry, |
| 640 |
daemon->auth_ttl)) |
| 641 |
{ |
| 642 |
offset = newoffset; |
| 643 |
if (soa) |
| 644 |
anscount++; |
| 645 |
else |
| 646 |
authcount++; |
| 647 |
} |
| 648 |
|
| 649 |
if (anscount != 0 || ns) |
| 650 |
{ |
| 651 |
struct name_list *secondary; |
| 652 |
|
| 653 |
/* Only include the machine running dnsmasq if it's acting as an auth server */ |
| 654 |
if (daemon->authinterface) |
| 655 |
{ |
| 656 |
if (add_resource_record(header, limit, &trunc, -offset, &ansp, |
| 657 |
daemon->auth_ttl, NULL, T_NS, C_IN, "d", offset == 0 ? authname : NULL, daemon->authserver)) |
| 658 |
{ |
| 659 |
if (offset == 0) |
| 660 |
offset = newoffset; |
| 661 |
if (ns) |
| 662 |
anscount++; |
| 663 |
else |
| 664 |
authcount++; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
if (!subnet) |
| 669 |
for (secondary = daemon->secondary_forward_server; secondary; secondary = secondary->next) |
| 670 |
if (add_resource_record(header, limit, &trunc, -offset, &ansp, |
| 671 |
daemon->auth_ttl, NULL, T_NS, C_IN, "d", offset == 0 ? authname : NULL, secondary->name)) |
| 672 |
{ |
| 673 |
if (offset == 0) |
| 674 |
offset = newoffset; |
| 675 |
if (ns) |
| 676 |
anscount++; |
| 677 |
else |
| 678 |
authcount++; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
if (axfr) |
| 683 |
{ |
| 684 |
for (rec = daemon->mxnames; rec; rec = rec->next) |
| 685 |
if (in_zone(zone, rec->name, &cut)) |
| 686 |
{ |
| 687 |
if (cut) |
| 688 |
*cut = 0; |
| 689 |
|
| 690 |
if (rec->issrv) |
| 691 |
{ |
| 692 |
if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp, daemon->auth_ttl, |
| 693 |
NULL, T_SRV, C_IN, "sssd", cut ? rec->name : NULL, |
| 694 |
rec->priority, rec->weight, rec->srvport, rec->target)) |
| 695 |
|
| 696 |
anscount++; |
| 697 |
} |
| 698 |
else |
| 699 |
{ |
| 700 |
if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp, daemon->auth_ttl, |
| 701 |
NULL, T_MX, C_IN, "sd", cut ? rec->name : NULL, rec->weight, rec->target)) |
| 702 |
anscount++; |
| 703 |
} |
| 704 |
|
| 705 |
/* restore config data */ |
| 706 |
if (cut) |
| 707 |
*cut = '.'; |
| 708 |
} |
| 709 |
|
| 710 |
for (txt = daemon->rr; txt; txt = txt->next) |
| 711 |
if (in_zone(zone, txt->name, &cut)) |
| 712 |
{ |
| 713 |
if (cut) |
| 714 |
*cut = 0; |
| 715 |
|
| 716 |
if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp, daemon->auth_ttl, |
| 717 |
NULL, txt->class, C_IN, "t", cut ? txt->name : NULL, txt->len, txt->txt)) |
| 718 |
anscount++; |
| 719 |
|
| 720 |
/* restore config data */ |
| 721 |
if (cut) |
| 722 |
*cut = '.'; |
| 723 |
} |
| 724 |
|
| 725 |
for (txt = daemon->txt; txt; txt = txt->next) |
| 726 |
if (txt->class == C_IN && in_zone(zone, txt->name, &cut)) |
| 727 |
{ |
| 728 |
if (cut) |
| 729 |
*cut = 0; |
| 730 |
|
| 731 |
if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp, daemon->auth_ttl, |
| 732 |
NULL, T_TXT, C_IN, "t", cut ? txt->name : NULL, txt->len, txt->txt)) |
| 733 |
anscount++; |
| 734 |
|
| 735 |
/* restore config data */ |
| 736 |
if (cut) |
| 737 |
*cut = '.'; |
| 738 |
} |
| 739 |
|
| 740 |
for (na = daemon->naptr; na; na = na->next) |
| 741 |
if (in_zone(zone, na->name, &cut)) |
| 742 |
{ |
| 743 |
if (cut) |
| 744 |
*cut = 0; |
| 745 |
|
| 746 |
if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp, daemon->auth_ttl, |
| 747 |
NULL, T_NAPTR, C_IN, "sszzzd", cut ? na->name : NULL, |
| 748 |
na->order, na->pref, na->flags, na->services, na->regexp, na->replace)) |
| 749 |
anscount++; |
| 750 |
|
| 751 |
/* restore config data */ |
| 752 |
if (cut) |
| 753 |
*cut = '.'; |
| 754 |
} |
| 755 |
|
| 756 |
for (intr = daemon->int_names; intr; intr = intr->next) |
| 757 |
if (in_zone(zone, intr->name, &cut)) |
| 758 |
{ |
| 759 |
struct addrlist *addrlist; |
| 760 |
|
| 761 |
if (cut) |
| 762 |
*cut = 0; |
| 763 |
|
| 764 |
for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
| 765 |
if (!(addrlist->flags & ADDRLIST_IPV6) && |
| 766 |
(local_query || filter_zone(zone, F_IPV4, &addrlist->addr)) && |
| 767 |
add_resource_record(header, limit, &trunc, -axfroffset, &ansp, |
| 768 |
daemon->auth_ttl, NULL, T_A, C_IN, "4", cut ? intr->name : NULL, &addrlist->addr)) |
| 769 |
anscount++; |
| 770 |
|
| 771 |
for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
| 772 |
if ((addrlist->flags & ADDRLIST_IPV6) && |
| 773 |
(local_query || filter_zone(zone, F_IPV6, &addrlist->addr)) && |
| 774 |
add_resource_record(header, limit, &trunc, -axfroffset, &ansp, |
| 775 |
daemon->auth_ttl, NULL, T_AAAA, C_IN, "6", cut ? intr->name : NULL, &addrlist->addr)) |
| 776 |
anscount++; |
| 777 |
|
| 778 |
/* restore config data */ |
| 779 |
if (cut) |
| 780 |
*cut = '.'; |
| 781 |
} |
| 782 |
|
| 783 |
for (a = daemon->cnames; a; a = a->next) |
| 784 |
if (in_zone(zone, a->alias, &cut)) |
| 785 |
{ |
| 786 |
strcpy(name, a->target); |
| 787 |
if (!strchr(name, '.')) |
| 788 |
{ |
| 789 |
strcat(name, "."); |
| 790 |
strcat(name, zone->domain); |
| 791 |
} |
| 792 |
|
| 793 |
if (cut) |
| 794 |
*cut = 0; |
| 795 |
|
| 796 |
if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp, |
| 797 |
daemon->auth_ttl, NULL, |
| 798 |
T_CNAME, C_IN, "d", cut ? a->alias : NULL, name)) |
| 799 |
anscount++; |
| 800 |
} |
| 801 |
|
| 802 |
cache_enumerate(1); |
| 803 |
while ((crecp = cache_enumerate(0))) |
| 804 |
{ |
| 805 |
if ((crecp->flags & (F_IPV4 | F_IPV6)) && |
| 806 |
!(crecp->flags & (F_NEG | F_NXDOMAIN)) && |
| 807 |
(crecp->flags & F_FORWARD)) |
| 808 |
{ |
| 809 |
if ((crecp->flags & F_DHCP) && !option_bool(OPT_DHCP_FQDN)) |
| 810 |
{ |
| 811 |
char *cache_name = cache_get_name(crecp); |
| 812 |
if (!strchr(cache_name, '.') && |
| 813 |
(local_query || filter_zone(zone, (crecp->flags & (F_IPV6 | F_IPV4)), &(crecp->addr))) && |
| 814 |
add_resource_record(header, limit, &trunc, -axfroffset, &ansp, |
| 815 |
daemon->auth_ttl, NULL, (crecp->flags & F_IPV6) ? T_AAAA : T_A, C_IN, |
| 816 |
(crecp->flags & F_IPV4) ? "4" : "6", cache_name, &crecp->addr)) |
| 817 |
anscount++; |
| 818 |
} |
| 819 |
|
| 820 |
if ((crecp->flags & F_HOSTS) || (((crecp->flags & F_DHCP) && option_bool(OPT_DHCP_FQDN)))) |
| 821 |
{ |
| 822 |
strcpy(name, cache_get_name(crecp)); |
| 823 |
if (in_zone(zone, name, &cut) && |
| 824 |
(local_query || filter_zone(zone, (crecp->flags & (F_IPV6 | F_IPV4)), &(crecp->addr)))) |
| 825 |
{ |
| 826 |
if (cut) |
| 827 |
*cut = 0; |
| 828 |
|
| 829 |
if (add_resource_record(header, limit, &trunc, -axfroffset, &ansp, |
| 830 |
daemon->auth_ttl, NULL, (crecp->flags & F_IPV6) ? T_AAAA : T_A, C_IN, |
| 831 |
(crecp->flags & F_IPV4) ? "4" : "6", cut ? name : NULL, &crecp->addr)) |
| 832 |
anscount++; |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
/* repeat SOA as last record */ |
| 839 |
if (add_resource_record(header, limit, &trunc, axfroffset, &ansp, |
| 840 |
daemon->auth_ttl, NULL, T_SOA, C_IN, "ddlllll", |
| 841 |
daemon->authserver, daemon->hostmaster, |
| 842 |
daemon->soa_sn, daemon->soa_refresh, |
| 843 |
daemon->soa_retry, daemon->soa_expiry, |
| 844 |
daemon->auth_ttl)) |
| 845 |
anscount++; |
| 846 |
|
| 847 |
} |
| 848 |
|
| 849 |
} |
| 850 |
|
| 851 |
/* done all questions, set up header and return length of result */ |
| 852 |
/* clear authoritative and truncated flags, set QR flag */ |
| 853 |
header->hb3 = (header->hb3 & ~(HB3_AA | HB3_TC)) | HB3_QR; |
| 854 |
|
| 855 |
if (local_query) |
| 856 |
{ |
| 857 |
/* set RA flag */ |
| 858 |
header->hb4 |= HB4_RA; |
| 859 |
} |
| 860 |
else |
| 861 |
{ |
| 862 |
/* clear RA flag */ |
| 863 |
header->hb4 &= ~HB4_RA; |
| 864 |
} |
| 865 |
|
| 866 |
/* data is never DNSSEC signed. */ |
| 867 |
header->hb4 &= ~HB4_AD; |
| 868 |
|
| 869 |
/* authoritative */ |
| 870 |
if (auth) |
| 871 |
header->hb3 |= HB3_AA; |
| 872 |
|
| 873 |
/* truncation */ |
| 874 |
if (trunc) |
| 875 |
{ |
| 876 |
header->hb3 |= HB3_TC; |
| 877 |
if (!(ansp = skip_questions(header, qlen))) |
| 878 |
return 0; /* bad packet */ |
| 879 |
anscount = authcount = 0; |
| 880 |
log_query(log_flags | F_AUTH, "reply", NULL, "truncated", 0); |
| 881 |
} |
| 882 |
|
| 883 |
if ((auth || local_query) && nxdomain) |
| 884 |
SET_RCODE(header, NXDOMAIN); |
| 885 |
else |
| 886 |
SET_RCODE(header, NOERROR); /* no error */ |
| 887 |
|
| 888 |
header->ancount = htons(anscount); |
| 889 |
header->nscount = htons(authcount); |
| 890 |
header->arcount = htons(0); |
| 891 |
|
| 892 |
if ((!local_query && out_of_zone) || notimp) |
| 893 |
{ |
| 894 |
if (out_of_zone) |
| 895 |
{ |
| 896 |
addr.log.rcode = REFUSED; |
| 897 |
addr.log.ede = EDE_NOT_AUTH; |
| 898 |
} |
| 899 |
else |
| 900 |
{ |
| 901 |
addr.log.rcode = NOTIMP; |
| 902 |
addr.log.ede = EDE_UNSET; |
| 903 |
} |
| 904 |
|
| 905 |
SET_RCODE(header, addr.log.rcode); |
| 906 |
header->ancount = htons(0); |
| 907 |
header->nscount = htons(0); |
| 908 |
log_query(log_flags | F_UPSTREAM | F_RCODE, "error", &addr, NULL, 0); |
| 909 |
return resize_packet(header, ansp - (unsigned char *)header, NULL, 0); |
| 910 |
} |
| 911 |
|
| 912 |
return ansp - (unsigned char *)header; |
| 913 |
} |
| 914 |
|
| 915 |
#endif |