Simon Kelley | d1ced3a | 2018-01-01 22:18:03 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2018 Simon Kelley |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 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 |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 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. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 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/>. |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 19 | int extract_name(struct dns_header *header, size_t plen, unsigned char **pp, |
| 20 | char *name, int isExtract, int extrabytes) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 21 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 22 | unsigned char *cp = (unsigned char *)name, *p = *pp, *p1 = NULL; |
Simon Kelley | b8f1655 | 2015-04-22 21:14:31 +0100 | [diff] [blame] | 23 | unsigned int j, l, namelen = 0, hops = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 24 | int retvalue = 1; |
| 25 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 26 | if (isExtract) |
| 27 | *cp = 0; |
| 28 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 29 | while (1) |
| 30 | { |
| 31 | unsigned int label_type; |
| 32 | |
| 33 | if (!CHECK_LEN(header, p, plen, 1)) |
| 34 | return 0; |
| 35 | |
| 36 | if ((l = *p++) == 0) |
| 37 | /* end marker */ |
| 38 | { |
Josh Soref | 74f0f9a | 2017-12-01 21:38:27 +0000 | [diff] [blame] | 39 | /* check that there are the correct no. of bytes after the name */ |
Simon Kelley | 6a0b00f | 2017-09-25 20:19:55 +0100 | [diff] [blame] | 40 | if (!CHECK_LEN(header, p1 ? p1 : p, plen, extrabytes)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 41 | return 0; |
| 42 | |
| 43 | if (isExtract) |
| 44 | { |
| 45 | if (cp != (unsigned char *)name) |
| 46 | cp--; |
| 47 | *cp = 0; /* terminate: lose final period */ |
| 48 | } |
| 49 | else if (*cp != 0) |
| 50 | retvalue = 2; |
| 51 | |
| 52 | if (p1) /* we jumped via compression */ |
| 53 | *pp = p1; |
| 54 | else |
| 55 | *pp = p; |
| 56 | |
| 57 | return retvalue; |
| 58 | } |
| 59 | |
| 60 | label_type = l & 0xc0; |
| 61 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 62 | if (label_type == 0xc0) /* pointer */ |
| 63 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 64 | if (!CHECK_LEN(header, p, plen, 1)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 65 | return 0; |
| 66 | |
| 67 | /* get offset */ |
| 68 | l = (l&0x3f) << 8; |
| 69 | l |= *p++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 70 | |
| 71 | if (!p1) /* first jump, save location to go back to */ |
| 72 | p1 = p; |
| 73 | |
| 74 | hops++; /* break malicious infinite loops */ |
| 75 | if (hops > 255) |
| 76 | return 0; |
| 77 | |
| 78 | p = l + (unsigned char *)header; |
| 79 | } |
Simon Kelley | 06568c6 | 2015-05-15 20:43:48 +0100 | [diff] [blame] | 80 | else if (label_type == 0x00) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 81 | { /* label_type = 0 -> label. */ |
Simon Kelley | 5d07d77 | 2015-05-15 18:13:06 +0100 | [diff] [blame] | 82 | namelen += l + 1; /* include period */ |
| 83 | if (namelen >= MAXDNAME) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 84 | return 0; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 85 | if (!CHECK_LEN(header, p, plen, l)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 86 | return 0; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 87 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 88 | for(j=0; j<l; j++, p++) |
| 89 | if (isExtract) |
| 90 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 91 | unsigned char c = *p; |
Simon Kelley | cbe379a | 2015-04-21 22:57:06 +0100 | [diff] [blame] | 92 | #ifdef HAVE_DNSSEC |
| 93 | if (option_bool(OPT_DNSSEC_VALID)) |
| 94 | { |
| 95 | if (c == 0 || c == '.' || c == NAME_ESCAPE) |
Simon Kelley | b8f1655 | 2015-04-22 21:14:31 +0100 | [diff] [blame] | 96 | { |
| 97 | *cp++ = NAME_ESCAPE; |
| 98 | *cp++ = c+1; |
| 99 | } |
| 100 | else |
| 101 | *cp++ = c; |
Simon Kelley | cbe379a | 2015-04-21 22:57:06 +0100 | [diff] [blame] | 102 | } |
| 103 | else |
| 104 | #endif |
Simon Kelley | 394ff49 | 2015-03-29 22:17:14 +0100 | [diff] [blame] | 105 | if (c != 0 && c != '.') |
| 106 | *cp++ = c; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 107 | else |
| 108 | return 0; |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | unsigned char c1 = *cp, c2 = *p; |
| 113 | |
| 114 | if (c1 == 0) |
| 115 | retvalue = 2; |
| 116 | else |
| 117 | { |
| 118 | cp++; |
| 119 | if (c1 >= 'A' && c1 <= 'Z') |
| 120 | c1 += 'a' - 'A'; |
Simon Kelley | cbe379a | 2015-04-21 22:57:06 +0100 | [diff] [blame] | 121 | #ifdef HAVE_DNSSEC |
| 122 | if (option_bool(OPT_DNSSEC_VALID) && c1 == NAME_ESCAPE) |
Simon Kelley | b8f1655 | 2015-04-22 21:14:31 +0100 | [diff] [blame] | 123 | c1 = (*cp++)-1; |
Simon Kelley | cbe379a | 2015-04-21 22:57:06 +0100 | [diff] [blame] | 124 | #endif |
| 125 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 126 | if (c2 >= 'A' && c2 <= 'Z') |
| 127 | c2 += 'a' - 'A'; |
Simon Kelley | cbe379a | 2015-04-21 22:57:06 +0100 | [diff] [blame] | 128 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 129 | if (c1 != c2) |
| 130 | retvalue = 2; |
| 131 | } |
| 132 | } |
Simon Kelley | 06568c6 | 2015-05-15 20:43:48 +0100 | [diff] [blame] | 133 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 134 | if (isExtract) |
| 135 | *cp++ = '.'; |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 136 | else if (*cp != 0 && *cp++ != '.') |
| 137 | retvalue = 2; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 138 | } |
Simon Kelley | 06568c6 | 2015-05-15 20:43:48 +0100 | [diff] [blame] | 139 | else |
| 140 | return 0; /* label types 0x40 and 0x80 not supported */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 141 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* Max size of input string (for IPv6) is 75 chars.) */ |
| 145 | #define MAXARPANAME 75 |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 146 | int in_arpa_name_2_addr(char *namein, struct all_addr *addrp) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 147 | { |
| 148 | int j; |
| 149 | char name[MAXARPANAME+1], *cp1; |
| 150 | unsigned char *addr = (unsigned char *)addrp; |
| 151 | char *lastchunk = NULL, *penchunk = NULL; |
| 152 | |
| 153 | if (strlen(namein) > MAXARPANAME) |
| 154 | return 0; |
| 155 | |
| 156 | memset(addrp, 0, sizeof(struct all_addr)); |
| 157 | |
| 158 | /* turn name into a series of asciiz strings */ |
Josh Soref | 74f0f9a | 2017-12-01 21:38:27 +0000 | [diff] [blame] | 159 | /* j counts no. of labels */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 160 | for(j = 1,cp1 = name; *namein; cp1++, namein++) |
| 161 | if (*namein == '.') |
| 162 | { |
| 163 | penchunk = lastchunk; |
| 164 | lastchunk = cp1 + 1; |
| 165 | *cp1 = 0; |
| 166 | j++; |
| 167 | } |
| 168 | else |
| 169 | *cp1 = *namein; |
| 170 | |
| 171 | *cp1 = 0; |
| 172 | |
| 173 | if (j<3) |
| 174 | return 0; |
| 175 | |
| 176 | if (hostname_isequal(lastchunk, "arpa") && hostname_isequal(penchunk, "in-addr")) |
| 177 | { |
| 178 | /* IP v4 */ |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 179 | /* address arrives as a name of the form |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 180 | www.xxx.yyy.zzz.in-addr.arpa |
| 181 | some of the low order address octets might be missing |
| 182 | and should be set to zero. */ |
| 183 | for (cp1 = name; cp1 != penchunk; cp1 += strlen(cp1)+1) |
| 184 | { |
| 185 | /* check for digits only (weeds out things like |
| 186 | 50.0/24.67.28.64.in-addr.arpa which are used |
| 187 | as CNAME targets according to RFC 2317 */ |
| 188 | char *cp; |
| 189 | for (cp = cp1; *cp; cp++) |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 190 | if (!isdigit((unsigned char)*cp)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 191 | return 0; |
| 192 | |
| 193 | addr[3] = addr[2]; |
| 194 | addr[2] = addr[1]; |
| 195 | addr[1] = addr[0]; |
| 196 | addr[0] = atoi(cp1); |
| 197 | } |
| 198 | |
| 199 | return F_IPV4; |
| 200 | } |
| 201 | #ifdef HAVE_IPV6 |
| 202 | else if (hostname_isequal(penchunk, "ip6") && |
| 203 | (hostname_isequal(lastchunk, "int") || hostname_isequal(lastchunk, "arpa"))) |
| 204 | { |
| 205 | /* IP v6: |
| 206 | Address arrives as 0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f.ip6.[int|arpa] |
| 207 | or \[xfedcba9876543210fedcba9876543210/128].ip6.[int|arpa] |
| 208 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 209 | Note that most of these the various representations are obsolete and |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 210 | left-over from the many DNS-for-IPv6 wars. We support all the formats |
| 211 | that we can since there is no reason not to. |
| 212 | */ |
| 213 | |
| 214 | if (*name == '\\' && *(name+1) == '[' && |
| 215 | (*(name+2) == 'x' || *(name+2) == 'X')) |
| 216 | { |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 217 | for (j = 0, cp1 = name+3; *cp1 && isxdigit((unsigned char) *cp1) && j < 32; cp1++, j++) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 218 | { |
| 219 | char xdig[2]; |
| 220 | xdig[0] = *cp1; |
| 221 | xdig[1] = 0; |
| 222 | if (j%2) |
| 223 | addr[j/2] |= strtol(xdig, NULL, 16); |
| 224 | else |
| 225 | addr[j/2] = strtol(xdig, NULL, 16) << 4; |
| 226 | } |
| 227 | |
| 228 | if (*cp1 == '/' && j == 32) |
| 229 | return F_IPV6; |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | for (cp1 = name; cp1 != penchunk; cp1 += strlen(cp1)+1) |
| 234 | { |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 235 | if (*(cp1+1) || !isxdigit((unsigned char)*cp1)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 236 | return 0; |
| 237 | |
| 238 | for (j = sizeof(struct all_addr)-1; j>0; j--) |
| 239 | addr[j] = (addr[j] >> 4) | (addr[j-1] << 4); |
| 240 | addr[0] = (addr[0] >> 4) | (strtol(cp1, NULL, 16) << 4); |
| 241 | } |
| 242 | |
| 243 | return F_IPV6; |
| 244 | } |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Giovanni Bajo | 32f82c6 | 2012-04-28 01:01:16 +0200 | [diff] [blame] | 251 | unsigned char *skip_name(unsigned char *ansp, struct dns_header *header, size_t plen, int extrabytes) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 252 | { |
| 253 | while(1) |
| 254 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 255 | unsigned int label_type; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 256 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 257 | if (!CHECK_LEN(header, ansp, plen, 1)) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 258 | return NULL; |
| 259 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 260 | label_type = (*ansp) & 0xc0; |
| 261 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 262 | if (label_type == 0xc0) |
| 263 | { |
| 264 | /* pointer for compression. */ |
| 265 | ansp += 2; |
| 266 | break; |
| 267 | } |
| 268 | else if (label_type == 0x80) |
| 269 | return NULL; /* reserved */ |
| 270 | else if (label_type == 0x40) |
| 271 | { |
| 272 | /* Extended label type */ |
| 273 | unsigned int count; |
| 274 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 275 | if (!CHECK_LEN(header, ansp, plen, 2)) |
| 276 | return NULL; |
| 277 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 278 | if (((*ansp++) & 0x3f) != 1) |
| 279 | return NULL; /* we only understand bitstrings */ |
| 280 | |
| 281 | count = *(ansp++); /* Bits in bitstring */ |
| 282 | |
| 283 | if (count == 0) /* count == 0 means 256 bits */ |
| 284 | ansp += 32; |
| 285 | else |
| 286 | ansp += ((count-1)>>3)+1; |
| 287 | } |
| 288 | else |
| 289 | { /* label type == 0 Bottom six bits is length */ |
| 290 | unsigned int len = (*ansp++) & 0x3f; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 291 | |
| 292 | if (!ADD_RDLEN(header, ansp, plen, len)) |
| 293 | return NULL; |
| 294 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 295 | if (len == 0) |
| 296 | break; /* zero length label marks the end. */ |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 297 | } |
| 298 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 299 | |
| 300 | if (!CHECK_LEN(header, ansp, plen, extrabytes)) |
| 301 | return NULL; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 302 | |
| 303 | return ansp; |
| 304 | } |
| 305 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 306 | unsigned char *skip_questions(struct dns_header *header, size_t plen) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 307 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 308 | int q; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 309 | unsigned char *ansp = (unsigned char *)(header+1); |
| 310 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 311 | for (q = ntohs(header->qdcount); q != 0; q--) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 312 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 313 | if (!(ansp = skip_name(ansp, header, plen, 4))) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 314 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 315 | ansp += 4; /* class and type */ |
| 316 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 317 | |
| 318 | return ansp; |
| 319 | } |
| 320 | |
Simon Kelley | 5107ace | 2014-02-23 10:48:32 +0000 | [diff] [blame] | 321 | unsigned char *skip_section(unsigned char *ansp, int count, struct dns_header *header, size_t plen) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 322 | { |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 323 | int i, rdlen; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 324 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 325 | for (i = 0; i < count; i++) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 326 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 327 | if (!(ansp = skip_name(ansp, header, plen, 10))) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 328 | return NULL; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 329 | ansp += 8; /* type, class, TTL */ |
| 330 | GETSHORT(rdlen, ansp); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 331 | if (!ADD_RDLEN(header, ansp, plen, rdlen)) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 332 | return NULL; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 335 | return ansp; |
| 336 | } |
| 337 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 338 | /* CRC the question section. This is used to safely detect query |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 339 | retransmission and to detect answers to questions we didn't ask, which |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 340 | might be poisoning attacks. Note that we decode the name rather |
| 341 | than CRC the raw bytes, since replies might be compressed differently. |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 342 | We ignore case in the names for the same reason. Return all-ones |
| 343 | if there is not question section. */ |
Simon Kelley | 17fb9ea | 2014-01-26 09:36:54 +0000 | [diff] [blame] | 344 | #ifndef HAVE_DNSSEC |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 345 | unsigned int questions_crc(struct dns_header *header, size_t plen, char *name) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 346 | { |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 347 | int q; |
| 348 | unsigned int crc = 0xffffffff; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 349 | unsigned char *p1, *p = (unsigned char *)(header+1); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 350 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 351 | for (q = ntohs(header->qdcount); q != 0; q--) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 352 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 353 | if (!extract_name(header, plen, &p, name, 1, 4)) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 354 | return crc; /* bad packet */ |
| 355 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 356 | for (p1 = (unsigned char *)name; *p1; p1++) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 357 | { |
| 358 | int i = 8; |
| 359 | char c = *p1; |
| 360 | |
| 361 | if (c >= 'A' && c <= 'Z') |
| 362 | c += 'a' - 'A'; |
| 363 | |
| 364 | crc ^= c << 24; |
| 365 | while (i--) |
| 366 | crc = crc & 0x80000000 ? (crc << 1) ^ 0x04c11db7 : crc << 1; |
| 367 | } |
| 368 | |
| 369 | /* CRC the class and type as well */ |
| 370 | for (p1 = p; p1 < p+4; p1++) |
| 371 | { |
| 372 | int i = 8; |
| 373 | crc ^= *p1 << 24; |
| 374 | while (i--) |
| 375 | crc = crc & 0x80000000 ? (crc << 1) ^ 0x04c11db7 : crc << 1; |
| 376 | } |
| 377 | |
| 378 | p += 4; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 379 | if (!CHECK_LEN(header, p, plen, 0)) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 380 | return crc; /* bad packet */ |
| 381 | } |
| 382 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 383 | return crc; |
| 384 | } |
Simon Kelley | 17fb9ea | 2014-01-26 09:36:54 +0000 | [diff] [blame] | 385 | #endif |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 386 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 387 | size_t resize_packet(struct dns_header *header, size_t plen, unsigned char *pheader, size_t hlen) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 388 | { |
| 389 | unsigned char *ansp = skip_questions(header, plen); |
| 390 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 391 | /* if packet is malformed, just return as-is. */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 392 | if (!ansp) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 393 | return plen; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 394 | |
| 395 | if (!(ansp = skip_section(ansp, ntohs(header->ancount) + ntohs(header->nscount) + ntohs(header->arcount), |
| 396 | header, plen))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 397 | return plen; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 398 | |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 399 | /* restore pseudoheader */ |
| 400 | if (pheader && ntohs(header->arcount) == 0) |
| 401 | { |
| 402 | /* must use memmove, may overlap */ |
| 403 | memmove(ansp, pheader, hlen); |
| 404 | header->arcount = htons(1); |
| 405 | ansp += hlen; |
| 406 | } |
| 407 | |
| 408 | return ansp - (unsigned char *)header; |
| 409 | } |
| 410 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 411 | /* is addr in the non-globally-routed IP space? */ |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 412 | int private_net(struct in_addr addr, int ban_localhost) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 413 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 414 | in_addr_t ip_addr = ntohl(addr.s_addr); |
| 415 | |
| 416 | return |
Simon Kelley | d2aa7df | 2015-08-03 21:52:12 +0100 | [diff] [blame] | 417 | (((ip_addr & 0xFF000000) == 0x7F000000) && ban_localhost) /* 127.0.0.0/8 (loopback) */ || |
| 418 | ((ip_addr & 0xFF000000) == 0x00000000) /* RFC 5735 section 3. "here" network */ || |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 419 | ((ip_addr & 0xFF000000) == 0x0A000000) /* 10.0.0.0/8 (private) */ || |
| 420 | ((ip_addr & 0xFFF00000) == 0xAC100000) /* 172.16.0.0/12 (private) */ || |
Simon Kelley | 90477fb | 2015-10-20 21:21:32 +0100 | [diff] [blame] | 421 | ((ip_addr & 0xFFFF0000) == 0xC0A80000) /* 192.168.0.0/16 (private) */ || |
| 422 | ((ip_addr & 0xFFFF0000) == 0xA9FE0000) /* 169.254.0.0/16 (zeroconf) */ || |
| 423 | ((ip_addr & 0xFFFFFF00) == 0xC0000200) /* 192.0.2.0/24 (test-net) */ || |
| 424 | ((ip_addr & 0xFFFFFF00) == 0xC6336400) /* 198.51.100.0/24(test-net) */ || |
| 425 | ((ip_addr & 0xFFFFFF00) == 0xCB007100) /* 203.0.113.0/24 (test-net) */ || |
| 426 | ((ip_addr & 0xFFFFFFFF) == 0xFFFFFFFF) /* 255.255.255.255/32 (broadcast)*/ ; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 427 | } |
Simon Kelley | 1cff166 | 2004-03-12 08:12:58 +0000 | [diff] [blame] | 428 | |
Simon Kelley | fca008d | 2017-02-19 18:50:41 +0000 | [diff] [blame] | 429 | #ifdef HAVE_IPV6 |
| 430 | static int private_net6(struct in6_addr *a) |
| 431 | { |
| 432 | return |
| 433 | IN6_IS_ADDR_UNSPECIFIED(a) || /* RFC 6303 4.3 */ |
| 434 | IN6_IS_ADDR_LOOPBACK(a) || /* RFC 6303 4.3 */ |
| 435 | IN6_IS_ADDR_LINKLOCAL(a) || /* RFC 6303 4.5 */ |
| 436 | ((unsigned char *)a)[0] == 0xfd || /* RFC 6303 4.4 */ |
| 437 | ((u32 *)a)[0] == htonl(0x20010db8); /* RFC 6303 4.6 */ |
| 438 | } |
| 439 | #endif |
| 440 | |
| 441 | |
Simon Kelley | 6938f34 | 2014-01-26 22:47:39 +0000 | [diff] [blame] | 442 | static unsigned char *do_doctor(unsigned char *p, int count, struct dns_header *header, size_t qlen, char *name, int *doctored) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 443 | { |
| 444 | int i, qtype, qclass, rdlen; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 445 | |
| 446 | for (i = count; i != 0; i--) |
| 447 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 448 | if (name && option_bool(OPT_LOG)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 449 | { |
| 450 | if (!extract_name(header, qlen, &p, name, 1, 10)) |
| 451 | return 0; |
| 452 | } |
| 453 | else if (!(p = skip_name(p, header, qlen, 10))) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 454 | return 0; /* bad packet */ |
| 455 | |
| 456 | GETSHORT(qtype, p); |
| 457 | GETSHORT(qclass, p); |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 458 | p += 4; /* ttl */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 459 | GETSHORT(rdlen, p); |
| 460 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 461 | if (qclass == C_IN && qtype == T_A) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 462 | { |
| 463 | struct doctor *doctor; |
| 464 | struct in_addr addr; |
| 465 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 466 | if (!CHECK_LEN(header, p, qlen, INADDRSZ)) |
| 467 | return 0; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 468 | |
| 469 | /* alignment */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 470 | memcpy(&addr, p, INADDRSZ); |
| 471 | |
| 472 | for (doctor = daemon->doctors; doctor; doctor = doctor->next) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 473 | { |
| 474 | if (doctor->end.s_addr == 0) |
| 475 | { |
| 476 | if (!is_same_net(doctor->in, addr, doctor->mask)) |
| 477 | continue; |
| 478 | } |
| 479 | else if (ntohl(doctor->in.s_addr) > ntohl(addr.s_addr) || |
| 480 | ntohl(doctor->end.s_addr) < ntohl(addr.s_addr)) |
| 481 | continue; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 482 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 483 | addr.s_addr &= ~doctor->mask.s_addr; |
| 484 | addr.s_addr |= (doctor->out.s_addr & doctor->mask.s_addr); |
| 485 | /* Since we munged the data, the server it came from is no longer authoritative */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 486 | header->hb3 &= ~HB3_AA; |
Simon Kelley | 6938f34 | 2014-01-26 22:47:39 +0000 | [diff] [blame] | 487 | *doctored = 1; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 488 | memcpy(p, &addr, INADDRSZ); |
| 489 | break; |
| 490 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 491 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 492 | else if (qtype == T_TXT && name && option_bool(OPT_LOG)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 493 | { |
| 494 | unsigned char *p1 = p; |
| 495 | if (!CHECK_LEN(header, p1, qlen, rdlen)) |
| 496 | return 0; |
| 497 | while ((p1 - p) < rdlen) |
| 498 | { |
| 499 | unsigned int i, len = *p1; |
| 500 | unsigned char *p2 = p1; |
Simon Kelley | 6a0b00f | 2017-09-25 20:19:55 +0100 | [diff] [blame] | 501 | if ((p1 + len - p) >= rdlen) |
| 502 | return 0; /* bad packet */ |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 503 | /* make counted string zero-term and sanitise */ |
| 504 | for (i = 0; i < len; i++) |
Simon Kelley | 231d061 | 2012-04-27 13:50:45 +0100 | [diff] [blame] | 505 | { |
| 506 | if (!isprint((int)*(p2+1))) |
| 507 | break; |
| 508 | |
| 509 | *p2 = *(p2+1); |
| 510 | p2++; |
| 511 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 512 | *p2 = 0; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 513 | my_syslog(LOG_INFO, "reply %s is %s", name, p1); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 514 | /* restore */ |
Simon Kelley | 231d061 | 2012-04-27 13:50:45 +0100 | [diff] [blame] | 515 | memmove(p1 + 1, p1, i); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 516 | *p1 = len; |
| 517 | p1 += len+1; |
| 518 | } |
| 519 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 520 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 521 | if (!ADD_RDLEN(header, p, qlen, rdlen)) |
| 522 | return 0; /* bad packet */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | return p; |
| 526 | } |
| 527 | |
Simon Kelley | 6938f34 | 2014-01-26 22:47:39 +0000 | [diff] [blame] | 528 | static int find_soa(struct dns_header *header, size_t qlen, char *name, int *doctored) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 529 | { |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 530 | unsigned char *p; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 531 | int qtype, qclass, rdlen; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 532 | unsigned long ttl, minttl = ULONG_MAX; |
| 533 | int i, found_soa = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 534 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 535 | /* first move to NS section and find TTL from any SOA section */ |
| 536 | if (!(p = skip_questions(header, qlen)) || |
Simon Kelley | 6938f34 | 2014-01-26 22:47:39 +0000 | [diff] [blame] | 537 | !(p = do_doctor(p, ntohs(header->ancount), header, qlen, name, doctored))) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 538 | return 0; /* bad packet */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 539 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 540 | for (i = ntohs(header->nscount); i != 0; i--) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 541 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 542 | if (!(p = skip_name(p, header, qlen, 10))) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 543 | return 0; /* bad packet */ |
| 544 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 545 | GETSHORT(qtype, p); |
| 546 | GETSHORT(qclass, p); |
| 547 | GETLONG(ttl, p); |
| 548 | GETSHORT(rdlen, p); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 549 | |
| 550 | if ((qclass == C_IN) && (qtype == T_SOA)) |
| 551 | { |
| 552 | found_soa = 1; |
| 553 | if (ttl < minttl) |
| 554 | minttl = ttl; |
| 555 | |
| 556 | /* MNAME */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 557 | if (!(p = skip_name(p, header, qlen, 0))) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 558 | return 0; |
| 559 | /* RNAME */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 560 | if (!(p = skip_name(p, header, qlen, 20))) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 561 | return 0; |
| 562 | p += 16; /* SERIAL REFRESH RETRY EXPIRE */ |
| 563 | |
| 564 | GETLONG(ttl, p); /* minTTL */ |
| 565 | if (ttl < minttl) |
| 566 | minttl = ttl; |
| 567 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 568 | else if (!ADD_RDLEN(header, p, qlen, rdlen)) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 569 | return 0; /* bad packet */ |
| 570 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 571 | |
Simon Kelley | 6938f34 | 2014-01-26 22:47:39 +0000 | [diff] [blame] | 572 | /* rewrite addresses in additional section too */ |
| 573 | if (!do_doctor(p, ntohs(header->arcount), header, qlen, NULL, doctored)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 574 | return 0; |
| 575 | |
| 576 | if (!found_soa) |
| 577 | minttl = daemon->neg_ttl; |
| 578 | |
| 579 | return minttl; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* Note that the following code can create CNAME chains that don't point to a real record, |
| 583 | either because of lack of memory, or lack of SOA records. These are treated by the cache code as |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 584 | expired and cleaned out that way. |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 585 | Return 1 if we reject an address because it look like part of dns-rebinding attack. */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 586 | int extract_addresses(struct dns_header *header, size_t qlen, char *name, time_t now, |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 587 | char **ipsets, int is_sign, int check_rebind, int no_cache_dnssec, |
Simon Kelley | 373e917 | 2017-12-01 22:40:56 +0000 | [diff] [blame] | 588 | int secure, int *doctored) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 589 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 590 | unsigned char *p, *p1, *endrr, *namep; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 591 | int i, j, qtype, qclass, aqtype, aqclass, ardlen, res, searched_soa = 0; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 592 | unsigned long ttl = 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 593 | struct all_addr addr; |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 594 | #ifdef HAVE_IPSET |
| 595 | char **ipsets_cur; |
| 596 | #else |
| 597 | (void)ipsets; /* unused */ |
| 598 | #endif |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 599 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 600 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 601 | cache_start_insert(); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 602 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 603 | /* find_soa is needed for dns_doctor and logging side-effects, so don't call it lazily if there are any. */ |
Simon Kelley | 6938f34 | 2014-01-26 22:47:39 +0000 | [diff] [blame] | 604 | if (daemon->doctors || option_bool(OPT_LOG) || option_bool(OPT_DNSSEC_VALID)) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 605 | { |
| 606 | searched_soa = 1; |
Simon Kelley | 6938f34 | 2014-01-26 22:47:39 +0000 | [diff] [blame] | 607 | ttl = find_soa(header, qlen, name, doctored); |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 608 | |
| 609 | if (*doctored) |
| 610 | { |
| 611 | if (secure) |
| 612 | return 0; |
Simon Kelley | 8c707e1 | 2017-12-05 22:28:10 +0000 | [diff] [blame] | 613 | #ifdef HAVE_DNSSEC |
Simon Kelley | 373e917 | 2017-12-01 22:40:56 +0000 | [diff] [blame] | 614 | if (option_bool(OPT_DNSSEC_VALID)) |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 615 | for (i = 0; i < ntohs(header->ancount); i++) |
Simon Kelley | 373e917 | 2017-12-01 22:40:56 +0000 | [diff] [blame] | 616 | if (daemon->rr_status[i]) |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 617 | return 0; |
Simon Kelley | 8c707e1 | 2017-12-05 22:28:10 +0000 | [diff] [blame] | 618 | #endif |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 619 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 620 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 621 | |
| 622 | /* go through the questions. */ |
| 623 | p = (unsigned char *)(header+1); |
| 624 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 625 | for (i = ntohs(header->qdcount); i != 0; i--) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 626 | { |
Simon Kelley | 1fbe4d2 | 2014-03-01 20:03:47 +0000 | [diff] [blame] | 627 | int found = 0, cname_count = CNAME_CHAIN; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 628 | struct crec *cpp = NULL; |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 629 | int flags = RCODE(header) == NXDOMAIN ? F_NXDOMAIN : 0; |
Simon Kelley | 8c707e1 | 2017-12-05 22:28:10 +0000 | [diff] [blame] | 630 | #ifdef HAVE_DNSSEC |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 631 | int cname_short = 0; |
Simon Kelley | 8c707e1 | 2017-12-05 22:28:10 +0000 | [diff] [blame] | 632 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 633 | unsigned long cttl = ULONG_MAX, attl; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 634 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 635 | namep = p; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 636 | if (!extract_name(header, qlen, &p, name, 1, 4)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 637 | return 0; /* bad packet */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 638 | |
| 639 | GETSHORT(qtype, p); |
| 640 | GETSHORT(qclass, p); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 641 | |
| 642 | if (qclass != C_IN) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 643 | continue; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 644 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 645 | /* PTRs: we chase CNAMEs here, since we have no way to |
| 646 | represent them in the cache. */ |
| 647 | if (qtype == T_PTR) |
| 648 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 649 | int name_encoding = in_arpa_name_2_addr(name, &addr); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 650 | |
| 651 | if (!name_encoding) |
| 652 | continue; |
| 653 | |
| 654 | if (!(flags & F_NXDOMAIN)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 655 | { |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 656 | cname_loop: |
| 657 | if (!(p1 = skip_questions(header, qlen))) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 658 | return 0; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 659 | |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 660 | for (j = 0; j < ntohs(header->ancount); j++) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 661 | { |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 662 | int secflag = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 663 | unsigned char *tmp = namep; |
| 664 | /* the loop body overwrites the original name, so get it back here. */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 665 | if (!extract_name(header, qlen, &tmp, name, 1, 0) || |
| 666 | !(res = extract_name(header, qlen, &p1, name, 0, 10))) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 667 | return 0; /* bad packet */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 668 | |
| 669 | GETSHORT(aqtype, p1); |
| 670 | GETSHORT(aqclass, p1); |
| 671 | GETLONG(attl, p1); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 672 | if ((daemon->max_ttl != 0) && (attl > daemon->max_ttl) && !is_sign) |
| 673 | { |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 674 | (p1) -= 4; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 675 | PUTLONG(daemon->max_ttl, p1); |
| 676 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 677 | GETSHORT(ardlen, p1); |
| 678 | endrr = p1+ardlen; |
| 679 | |
| 680 | /* TTL of record is minimum of CNAMES and PTR */ |
| 681 | if (attl < cttl) |
| 682 | cttl = attl; |
| 683 | |
| 684 | if (aqclass == C_IN && res != 2 && (aqtype == T_CNAME || aqtype == T_PTR)) |
| 685 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 686 | if (!extract_name(header, qlen, &p1, name, 1, 0)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 687 | return 0; |
Simon Kelley | 8c707e1 | 2017-12-05 22:28:10 +0000 | [diff] [blame] | 688 | #ifdef HAVE_DNSSEC |
Simon Kelley | 373e917 | 2017-12-01 22:40:56 +0000 | [diff] [blame] | 689 | if (option_bool(OPT_DNSSEC_VALID) && daemon->rr_status[j]) |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 690 | { |
| 691 | /* validated RR anywhere in CNAME chain, don't cache. */ |
| 692 | if (cname_short || aqtype == T_CNAME) |
| 693 | return 0; |
| 694 | |
| 695 | secflag = F_DNSSECOK; |
| 696 | } |
Simon Kelley | 8c707e1 | 2017-12-05 22:28:10 +0000 | [diff] [blame] | 697 | #endif |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 698 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 699 | if (aqtype == T_CNAME) |
| 700 | { |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 701 | if (!cname_count--) |
| 702 | return 0; /* looped CNAMES, we can't cache. */ |
Simon Kelley | 8c707e1 | 2017-12-05 22:28:10 +0000 | [diff] [blame] | 703 | #ifdef HAVE_DNSSEC |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 704 | cname_short = 1; |
Simon Kelley | 8c707e1 | 2017-12-05 22:28:10 +0000 | [diff] [blame] | 705 | #endif |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 706 | goto cname_loop; |
| 707 | } |
| 708 | |
Simon Kelley | 0435d04 | 2014-01-08 18:22:37 +0000 | [diff] [blame] | 709 | cache_insert(name, &addr, now, cttl, name_encoding | secflag | F_REVERSE); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 710 | found = 1; |
| 711 | } |
| 712 | |
| 713 | p1 = endrr; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 714 | if (!CHECK_LEN(header, p1, qlen, 0)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 715 | return 0; /* bad packet */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 719 | if (!found && !option_bool(OPT_NO_NEG)) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 720 | { |
| 721 | if (!searched_soa) |
| 722 | { |
| 723 | searched_soa = 1; |
Simon Kelley | 6938f34 | 2014-01-26 22:47:39 +0000 | [diff] [blame] | 724 | ttl = find_soa(header, qlen, NULL, doctored); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 725 | } |
| 726 | if (ttl) |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 727 | cache_insert(NULL, &addr, now, ttl, name_encoding | F_REVERSE | F_NEG | flags | (secure ? F_DNSSECOK : 0)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 730 | else |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 731 | { |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 732 | /* everything other than PTR */ |
| 733 | struct crec *newc; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 734 | int addrlen; |
| 735 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 736 | if (qtype == T_A) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 737 | { |
| 738 | addrlen = INADDRSZ; |
| 739 | flags |= F_IPV4; |
| 740 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 741 | #ifdef HAVE_IPV6 |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 742 | else if (qtype == T_AAAA) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 743 | { |
| 744 | addrlen = IN6ADDRSZ; |
| 745 | flags |= F_IPV6; |
| 746 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 747 | #endif |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 748 | else |
| 749 | continue; |
| 750 | |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 751 | cname_loop1: |
| 752 | if (!(p1 = skip_questions(header, qlen))) |
| 753 | return 0; |
| 754 | |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 755 | for (j = 0; j < ntohs(header->ancount); j++) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 756 | { |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 757 | int secflag = 0; |
| 758 | |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 759 | if (!(res = extract_name(header, qlen, &p1, name, 0, 10))) |
| 760 | return 0; /* bad packet */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 761 | |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 762 | GETSHORT(aqtype, p1); |
| 763 | GETSHORT(aqclass, p1); |
| 764 | GETLONG(attl, p1); |
| 765 | if ((daemon->max_ttl != 0) && (attl > daemon->max_ttl) && !is_sign) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 766 | { |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 767 | (p1) -= 4; |
| 768 | PUTLONG(daemon->max_ttl, p1); |
| 769 | } |
| 770 | GETSHORT(ardlen, p1); |
| 771 | endrr = p1+ardlen; |
| 772 | |
| 773 | if (aqclass == C_IN && res != 2 && (aqtype == T_CNAME || aqtype == qtype)) |
| 774 | { |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 775 | #ifdef HAVE_DNSSEC |
Simon Kelley | 373e917 | 2017-12-01 22:40:56 +0000 | [diff] [blame] | 776 | if (option_bool(OPT_DNSSEC_VALID) && daemon->rr_status[j]) |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 777 | secflag = F_DNSSECOK; |
| 778 | #endif |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 779 | if (aqtype == T_CNAME) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 780 | { |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 781 | if (!cname_count--) |
| 782 | return 0; /* looped CNAMES */ |
Simon Kelley | 0435d04 | 2014-01-08 18:22:37 +0000 | [diff] [blame] | 783 | newc = cache_insert(name, NULL, now, attl, F_CNAME | F_FORWARD | secflag); |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 784 | if (newc) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 785 | { |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 786 | newc->addr.cname.target.cache = NULL; |
Simon Kelley | 03431d6 | 2014-03-20 16:25:43 +0000 | [diff] [blame] | 787 | /* anything other than zero, to avoid being mistaken for CNAME to interface-name */ |
| 788 | newc->addr.cname.uid = 1; |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 789 | if (cpp) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 790 | { |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 791 | cpp->addr.cname.target.cache = newc; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 792 | cpp->addr.cname.uid = newc->uid; |
| 793 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 794 | } |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 795 | |
| 796 | cpp = newc; |
| 797 | if (attl < cttl) |
| 798 | cttl = attl; |
| 799 | |
| 800 | if (!extract_name(header, qlen, &p1, name, 1, 0)) |
| 801 | return 0; |
| 802 | goto cname_loop1; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 803 | } |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 804 | else if (!(flags & F_NXDOMAIN)) |
| 805 | { |
| 806 | found = 1; |
| 807 | |
| 808 | /* copy address into aligned storage */ |
| 809 | if (!CHECK_LEN(header, p1, qlen, addrlen)) |
| 810 | return 0; /* bad packet */ |
| 811 | memcpy(&addr, p1, addrlen); |
| 812 | |
| 813 | /* check for returned address in private space */ |
Simon Kelley | b059c96 | 2015-05-08 20:25:51 +0100 | [diff] [blame] | 814 | if (check_rebind) |
| 815 | { |
| 816 | if ((flags & F_IPV4) && |
| 817 | private_net(addr.addr.addr4, !option_bool(OPT_LOCAL_REBIND))) |
| 818 | return 1; |
| 819 | |
| 820 | #ifdef HAVE_IPV6 |
| 821 | if ((flags & F_IPV6) && |
| 822 | IN6_IS_ADDR_V4MAPPED(&addr.addr.addr6)) |
| 823 | { |
| 824 | struct in_addr v4; |
| 825 | v4.s_addr = ((const uint32_t *) (&addr.addr.addr6))[3]; |
| 826 | if (private_net(v4, !option_bool(OPT_LOCAL_REBIND))) |
| 827 | return 1; |
| 828 | } |
| 829 | #endif |
| 830 | } |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 831 | |
| 832 | #ifdef HAVE_IPSET |
| 833 | if (ipsets && (flags & (F_IPV4 | F_IPV6))) |
| 834 | { |
| 835 | ipsets_cur = ipsets; |
| 836 | while (*ipsets_cur) |
Wang Jian | 49752b9 | 2014-03-28 20:52:47 +0000 | [diff] [blame] | 837 | { |
Simon Kelley | b7639d5 | 2014-03-29 09:20:07 +0000 | [diff] [blame] | 838 | log_query((flags & (F_IPV4 | F_IPV6)) | F_IPSET, name, &addr, *ipsets_cur); |
Wang Jian | 49752b9 | 2014-03-28 20:52:47 +0000 | [diff] [blame] | 839 | add_to_ipset(*ipsets_cur++, &addr, flags, 0); |
| 840 | } |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 841 | } |
| 842 | #endif |
| 843 | |
Simon Kelley | 0435d04 | 2014-01-08 18:22:37 +0000 | [diff] [blame] | 844 | newc = cache_insert(name, &addr, now, attl, flags | F_FORWARD | secflag); |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 845 | if (newc && cpp) |
| 846 | { |
| 847 | cpp->addr.cname.target.cache = newc; |
| 848 | cpp->addr.cname.uid = newc->uid; |
| 849 | } |
| 850 | cpp = NULL; |
| 851 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 852 | } |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 853 | |
| 854 | p1 = endrr; |
| 855 | if (!CHECK_LEN(header, p1, qlen, 0)) |
| 856 | return 0; /* bad packet */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 859 | if (!found && !option_bool(OPT_NO_NEG)) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 860 | { |
| 861 | if (!searched_soa) |
| 862 | { |
| 863 | searched_soa = 1; |
Simon Kelley | 6938f34 | 2014-01-26 22:47:39 +0000 | [diff] [blame] | 864 | ttl = find_soa(header, qlen, NULL, doctored); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 865 | } |
| 866 | /* If there's no SOA to get the TTL from, but there is a CNAME |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 867 | pointing at this, inherit its TTL */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 868 | if (ttl || cpp) |
| 869 | { |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 870 | newc = cache_insert(name, NULL, now, ttl ? ttl : cttl, F_FORWARD | F_NEG | flags | (secure ? F_DNSSECOK : 0)); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 871 | if (newc && cpp) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 872 | { |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 873 | cpp->addr.cname.target.cache = newc; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 874 | cpp->addr.cname.uid = newc->uid; |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
Simon Kelley | 1023dcb | 2012-04-09 18:00:08 +0100 | [diff] [blame] | 881 | /* Don't put stuff from a truncated packet into the cache. |
Simon Kelley | 1023dcb | 2012-04-09 18:00:08 +0100 | [diff] [blame] | 882 | Don't cache replies from non-recursive nameservers, since we may get a |
| 883 | reply containing a CNAME but not its target, even though the target |
| 884 | does exist. */ |
| 885 | if (!(header->hb3 & HB3_TC) && |
| 886 | !(header->hb4 & HB4_CD) && |
| 887 | (header->hb4 & HB4_RA) && |
Simon Kelley | 3a23715 | 2013-12-12 12:15:50 +0000 | [diff] [blame] | 888 | !no_cache_dnssec) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 889 | cache_end_insert(); |
| 890 | |
| 891 | return 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | /* If the packet holds exactly one query |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 895 | return F_IPV4 or F_IPV6 and leave the name from the query in name */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 896 | unsigned int extract_request(struct dns_header *header, size_t qlen, char *name, unsigned short *typep) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 897 | { |
| 898 | unsigned char *p = (unsigned char *)(header+1); |
| 899 | int qtype, qclass; |
| 900 | |
Simon Kelley | c1bb850 | 2004-08-11 18:40:17 +0100 | [diff] [blame] | 901 | if (typep) |
| 902 | *typep = 0; |
| 903 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 904 | if (ntohs(header->qdcount) != 1 || OPCODE(header) != QUERY) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 905 | return 0; /* must be exactly one query. */ |
| 906 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 907 | if (!extract_name(header, qlen, &p, name, 1, 4)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 908 | return 0; /* bad packet */ |
| 909 | |
| 910 | GETSHORT(qtype, p); |
| 911 | GETSHORT(qclass, p); |
| 912 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 913 | if (typep) |
| 914 | *typep = qtype; |
| 915 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 916 | if (qclass == C_IN) |
| 917 | { |
| 918 | if (qtype == T_A) |
| 919 | return F_IPV4; |
| 920 | if (qtype == T_AAAA) |
| 921 | return F_IPV6; |
| 922 | if (qtype == T_ANY) |
| 923 | return F_IPV4 | F_IPV6; |
| 924 | } |
| 925 | |
| 926 | return F_QUERY; |
| 927 | } |
| 928 | |
| 929 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 930 | size_t setup_reply(struct dns_header *header, size_t qlen, |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 931 | struct all_addr *addrp, unsigned int flags, unsigned long ttl) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 932 | { |
Simon Kelley | ad4a8ff | 2015-04-09 21:48:00 +0100 | [diff] [blame] | 933 | unsigned char *p; |
| 934 | |
| 935 | if (!(p = skip_questions(header, qlen))) |
| 936 | return 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 937 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 938 | /* clear authoritative and truncated flags, set QR flag */ |
| 939 | header->hb3 = (header->hb3 & ~(HB3_AA | HB3_TC)) | HB3_QR; |
| 940 | /* set RA flag */ |
| 941 | header->hb4 |= HB4_RA; |
| 942 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 943 | header->nscount = htons(0); |
| 944 | header->arcount = htons(0); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 945 | header->ancount = htons(0); /* no answers unless changed below */ |
Simon Kelley | d05dd58 | 2016-01-19 21:23:30 +0000 | [diff] [blame] | 946 | if (flags == F_NOERR) |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 947 | SET_RCODE(header, NOERROR); /* empty domain */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 948 | else if (flags == F_NXDOMAIN) |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 949 | SET_RCODE(header, NXDOMAIN); |
Simon Kelley | 087eb76 | 2017-10-30 23:16:54 +0000 | [diff] [blame] | 950 | else if (flags == F_SERVFAIL) |
| 951 | SET_RCODE(header, SERVFAIL); |
Simon Kelley | ad4a8ff | 2015-04-09 21:48:00 +0100 | [diff] [blame] | 952 | else if (flags == F_IPV4) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 953 | { /* we know the address */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 954 | SET_RCODE(header, NOERROR); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 955 | header->ancount = htons(1); |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 956 | header->hb3 |= HB3_AA; |
| 957 | add_resource_record(header, NULL, NULL, sizeof(struct dns_header), &p, ttl, NULL, T_A, C_IN, "4", addrp); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 958 | } |
| 959 | #ifdef HAVE_IPV6 |
Simon Kelley | ad4a8ff | 2015-04-09 21:48:00 +0100 | [diff] [blame] | 960 | else if (flags == F_IPV6) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 961 | { |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 962 | SET_RCODE(header, NOERROR); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 963 | header->ancount = htons(1); |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 964 | header->hb3 |= HB3_AA; |
| 965 | add_resource_record(header, NULL, NULL, sizeof(struct dns_header), &p, ttl, NULL, T_AAAA, C_IN, "6", addrp); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 966 | } |
| 967 | #endif |
| 968 | else /* nowhere to forward to */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 969 | SET_RCODE(header, REFUSED); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 970 | |
| 971 | return p - (unsigned char *)header; |
| 972 | } |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 973 | |
| 974 | /* check if name matches local names ie from /etc/hosts or DHCP or local mx names. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 975 | int check_for_local_domain(char *name, time_t now) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 976 | { |
| 977 | struct crec *crecp; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 978 | struct mx_srv_record *mx; |
| 979 | struct txt_record *txt; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 980 | struct interface_name *intr; |
| 981 | struct ptr_record *ptr; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 982 | struct naptr *naptr; |
| 983 | |
Simon Kelley | 288df49 | 2014-09-18 21:48:51 +0100 | [diff] [blame] | 984 | /* Note: the call to cache_find_by_name is intended to find any record which matches |
Simon Kelley | 93be5b1 | 2015-12-15 12:04:40 +0000 | [diff] [blame] | 985 | ie A, AAAA, CNAME. */ |
Simon Kelley | 288df49 | 2014-09-18 21:48:51 +0100 | [diff] [blame] | 986 | |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 987 | if ((crecp = cache_find_by_name(NULL, name, now, F_IPV4 | F_IPV6 | F_CNAME | F_NO_RR)) && |
Simon Kelley | 7b174c2 | 2013-10-28 13:14:03 +0000 | [diff] [blame] | 988 | (crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG))) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 989 | return 1; |
| 990 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 991 | for (naptr = daemon->naptr; naptr; naptr = naptr->next) |
| 992 | if (hostname_isequal(name, naptr->name)) |
| 993 | return 1; |
| 994 | |
| 995 | for (mx = daemon->mxnames; mx; mx = mx->next) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 996 | if (hostname_isequal(name, mx->name)) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 997 | return 1; |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 998 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 999 | for (txt = daemon->txt; txt; txt = txt->next) |
| 1000 | if (hostname_isequal(name, txt->name)) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1001 | return 1; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1002 | |
| 1003 | for (intr = daemon->int_names; intr; intr = intr->next) |
| 1004 | if (hostname_isequal(name, intr->name)) |
| 1005 | return 1; |
| 1006 | |
| 1007 | for (ptr = daemon->ptr; ptr; ptr = ptr->next) |
| 1008 | if (hostname_isequal(name, ptr->name)) |
| 1009 | return 1; |
| 1010 | |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 1011 | return 0; |
| 1012 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1013 | |
| 1014 | /* Is the packet a reply with the answer address equal to addr? |
| 1015 | If so mung is into an NXDOMAIN reply and also put that information |
| 1016 | in the cache. */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 1017 | int check_for_bogus_wildcard(struct dns_header *header, size_t qlen, char *name, |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1018 | struct bogus_addr *baddr, time_t now) |
| 1019 | { |
| 1020 | unsigned char *p; |
| 1021 | int i, qtype, qclass, rdlen; |
| 1022 | unsigned long ttl; |
| 1023 | struct bogus_addr *baddrp; |
| 1024 | |
| 1025 | /* skip over questions */ |
| 1026 | if (!(p = skip_questions(header, qlen))) |
| 1027 | return 0; /* bad packet */ |
| 1028 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1029 | for (i = ntohs(header->ancount); i != 0; i--) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1030 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1031 | if (!extract_name(header, qlen, &p, name, 1, 10)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1032 | return 0; /* bad packet */ |
| 1033 | |
| 1034 | GETSHORT(qtype, p); |
| 1035 | GETSHORT(qclass, p); |
| 1036 | GETLONG(ttl, p); |
| 1037 | GETSHORT(rdlen, p); |
| 1038 | |
| 1039 | if (qclass == C_IN && qtype == T_A) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1040 | { |
| 1041 | if (!CHECK_LEN(header, p, qlen, INADDRSZ)) |
| 1042 | return 0; |
| 1043 | |
| 1044 | for (baddrp = baddr; baddrp; baddrp = baddrp->next) |
| 1045 | if (memcmp(&baddrp->addr, p, INADDRSZ) == 0) |
| 1046 | { |
| 1047 | /* Found a bogus address. Insert that info here, since there no SOA record |
| 1048 | to get the ttl from in the normal processing */ |
| 1049 | cache_start_insert(); |
Simon Kelley | 8db957d | 2013-12-17 15:47:10 +0000 | [diff] [blame] | 1050 | cache_insert(name, NULL, now, ttl, F_IPV4 | F_FORWARD | F_NEG | F_NXDOMAIN); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1051 | cache_end_insert(); |
| 1052 | |
| 1053 | return 1; |
| 1054 | } |
| 1055 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1056 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1057 | if (!ADD_RDLEN(header, p, qlen, rdlen)) |
| 1058 | return 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
Glen Huang | 32fc6db | 2014-12-27 15:28:12 +0000 | [diff] [blame] | 1064 | int check_for_ignored_address(struct dns_header *header, size_t qlen, struct bogus_addr *baddr) |
| 1065 | { |
| 1066 | unsigned char *p; |
| 1067 | int i, qtype, qclass, rdlen; |
| 1068 | struct bogus_addr *baddrp; |
| 1069 | |
| 1070 | /* skip over questions */ |
| 1071 | if (!(p = skip_questions(header, qlen))) |
| 1072 | return 0; /* bad packet */ |
| 1073 | |
| 1074 | for (i = ntohs(header->ancount); i != 0; i--) |
| 1075 | { |
| 1076 | if (!(p = skip_name(p, header, qlen, 10))) |
| 1077 | return 0; /* bad packet */ |
| 1078 | |
| 1079 | GETSHORT(qtype, p); |
| 1080 | GETSHORT(qclass, p); |
| 1081 | p += 4; /* TTL */ |
| 1082 | GETSHORT(rdlen, p); |
| 1083 | |
| 1084 | if (qclass == C_IN && qtype == T_A) |
| 1085 | { |
| 1086 | if (!CHECK_LEN(header, p, qlen, INADDRSZ)) |
| 1087 | return 0; |
| 1088 | |
| 1089 | for (baddrp = baddr; baddrp; baddrp = baddrp->next) |
| 1090 | if (memcmp(&baddrp->addr, p, INADDRSZ) == 0) |
| 1091 | return 1; |
| 1092 | } |
| 1093 | |
| 1094 | if (!ADD_RDLEN(header, p, qlen, rdlen)) |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | return 0; |
| 1099 | } |
| 1100 | |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 1101 | |
Simon Kelley | b75e936 | 2012-12-07 11:50:41 +0000 | [diff] [blame] | 1102 | int add_resource_record(struct dns_header *header, char *limit, int *truncp, int nameoffset, unsigned char **pp, |
Simon Kelley | e1ff419 | 2012-12-09 17:08:47 +0000 | [diff] [blame] | 1103 | unsigned long ttl, int *offset, unsigned short type, unsigned short class, char *format, ...) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1104 | { |
| 1105 | va_list ap; |
| 1106 | unsigned char *sav, *p = *pp; |
| 1107 | int j; |
| 1108 | unsigned short usval; |
| 1109 | long lval; |
| 1110 | char *sval; |
Simon Kelley | c366717 | 2017-10-13 23:26:29 +0100 | [diff] [blame] | 1111 | |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 1112 | #define CHECK_LIMIT(size) \ |
Simon Kelley | c366717 | 2017-10-13 23:26:29 +0100 | [diff] [blame] | 1113 | if (limit && p + (size) > (unsigned char*)limit) goto truncated; |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 1114 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1115 | va_start(ap, format); /* make ap point to 1st unamed argument */ |
Simon Kelley | c366717 | 2017-10-13 23:26:29 +0100 | [diff] [blame] | 1116 | |
| 1117 | if (truncp && *truncp) |
| 1118 | goto truncated; |
| 1119 | |
Simon Kelley | b75e936 | 2012-12-07 11:50:41 +0000 | [diff] [blame] | 1120 | if (nameoffset > 0) |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1121 | { |
Simon Kelley | 62cb936 | 2017-09-26 22:00:11 +0100 | [diff] [blame] | 1122 | CHECK_LIMIT(2); |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1123 | PUTSHORT(nameoffset | 0xc000, p); |
| 1124 | } |
| 1125 | else |
| 1126 | { |
Simon Kelley | e1ff419 | 2012-12-09 17:08:47 +0000 | [diff] [blame] | 1127 | char *name = va_arg(ap, char *); |
Simon Kelley | 62cb936 | 2017-09-26 22:00:11 +0100 | [diff] [blame] | 1128 | if (name && !(p = do_rfc1035_name(p, name, limit))) |
Simon Kelley | c366717 | 2017-10-13 23:26:29 +0100 | [diff] [blame] | 1129 | goto truncated; |
Simon Kelley | 62cb936 | 2017-09-26 22:00:11 +0100 | [diff] [blame] | 1130 | |
Simon Kelley | b75e936 | 2012-12-07 11:50:41 +0000 | [diff] [blame] | 1131 | if (nameoffset < 0) |
| 1132 | { |
Simon Kelley | 62cb936 | 2017-09-26 22:00:11 +0100 | [diff] [blame] | 1133 | CHECK_LIMIT(2); |
Simon Kelley | b75e936 | 2012-12-07 11:50:41 +0000 | [diff] [blame] | 1134 | PUTSHORT(-nameoffset | 0xc000, p); |
| 1135 | } |
| 1136 | else |
Simon Kelley | 62cb936 | 2017-09-26 22:00:11 +0100 | [diff] [blame] | 1137 | { |
| 1138 | CHECK_LIMIT(1); |
| 1139 | *p++ = 0; |
| 1140 | } |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1141 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1142 | |
Simon Kelley | 62cb936 | 2017-09-26 22:00:11 +0100 | [diff] [blame] | 1143 | /* type (2) + class (2) + ttl (4) + rdlen (2) */ |
| 1144 | CHECK_LIMIT(10); |
| 1145 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1146 | PUTSHORT(type, p); |
| 1147 | PUTSHORT(class, p); |
| 1148 | PUTLONG(ttl, p); /* TTL */ |
| 1149 | |
| 1150 | sav = p; /* Save pointer to RDLength field */ |
| 1151 | PUTSHORT(0, p); /* Placeholder RDLength */ |
| 1152 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1153 | for (; *format; format++) |
| 1154 | switch (*format) |
| 1155 | { |
| 1156 | #ifdef HAVE_IPV6 |
| 1157 | case '6': |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 1158 | CHECK_LIMIT(IN6ADDRSZ); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1159 | sval = va_arg(ap, char *); |
| 1160 | memcpy(p, sval, IN6ADDRSZ); |
| 1161 | p += IN6ADDRSZ; |
| 1162 | break; |
| 1163 | #endif |
| 1164 | |
| 1165 | case '4': |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 1166 | CHECK_LIMIT(INADDRSZ); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1167 | sval = va_arg(ap, char *); |
| 1168 | memcpy(p, sval, INADDRSZ); |
| 1169 | p += INADDRSZ; |
| 1170 | break; |
| 1171 | |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 1172 | case 'b': |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 1173 | CHECK_LIMIT(1); |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 1174 | usval = va_arg(ap, int); |
| 1175 | *p++ = usval; |
| 1176 | break; |
| 1177 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1178 | case 's': |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 1179 | CHECK_LIMIT(2); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1180 | usval = va_arg(ap, int); |
| 1181 | PUTSHORT(usval, p); |
| 1182 | break; |
| 1183 | |
| 1184 | case 'l': |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 1185 | CHECK_LIMIT(4); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1186 | lval = va_arg(ap, long); |
| 1187 | PUTLONG(lval, p); |
| 1188 | break; |
| 1189 | |
| 1190 | case 'd': |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 1191 | /* get domain-name answer arg and store it in RDATA field */ |
| 1192 | if (offset) |
| 1193 | *offset = p - (unsigned char *)header; |
Simon Kelley | c366717 | 2017-10-13 23:26:29 +0100 | [diff] [blame] | 1194 | if (!(p = do_rfc1035_name(p, va_arg(ap, char *), limit))) |
| 1195 | goto truncated; |
| 1196 | CHECK_LIMIT(1); |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 1197 | *p++ = 0; |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1198 | break; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1199 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1200 | case 't': |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1201 | usval = va_arg(ap, int); |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 1202 | CHECK_LIMIT(usval); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1203 | sval = va_arg(ap, char *); |
Simon Kelley | 9f7f3b1 | 2012-05-28 21:39:57 +0100 | [diff] [blame] | 1204 | if (usval != 0) |
| 1205 | memcpy(p, sval, usval); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1206 | p += usval; |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1207 | break; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1208 | |
| 1209 | case 'z': |
| 1210 | sval = va_arg(ap, char *); |
| 1211 | usval = sval ? strlen(sval) : 0; |
| 1212 | if (usval > 255) |
| 1213 | usval = 255; |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 1214 | CHECK_LIMIT(usval + 1); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1215 | *p++ = (unsigned char)usval; |
| 1216 | memcpy(p, sval, usval); |
| 1217 | p += usval; |
| 1218 | break; |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | va_end(ap); /* clean up variable argument pointer */ |
| 1222 | |
Simon Kelley | c366717 | 2017-10-13 23:26:29 +0100 | [diff] [blame] | 1223 | /* Now, store real RDLength. sav already checked against limit. */ |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1224 | j = p - sav - 2; |
Simon Kelley | c366717 | 2017-10-13 23:26:29 +0100 | [diff] [blame] | 1225 | PUTSHORT(j, sav); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1226 | |
| 1227 | *pp = p; |
| 1228 | return 1; |
Simon Kelley | c366717 | 2017-10-13 23:26:29 +0100 | [diff] [blame] | 1229 | |
| 1230 | truncated: |
| 1231 | va_end(ap); |
| 1232 | if (truncp) |
| 1233 | *truncp = 1; |
| 1234 | return 0; |
| 1235 | |
| 1236 | #undef CHECK_LIMIT |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1239 | static unsigned long crec_ttl(struct crec *crecp, time_t now) |
| 1240 | { |
| 1241 | /* Return 0 ttl for DHCP entries, which might change |
Simon Kelley | 7480aef | 2016-02-26 21:58:20 +0000 | [diff] [blame] | 1242 | before the lease expires, unless configured otherwise. */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1243 | |
Simon Kelley | df3d54f | 2016-02-24 21:03:38 +0000 | [diff] [blame] | 1244 | if (crecp->flags & F_DHCP) |
Simon Kelley | 7480aef | 2016-02-26 21:58:20 +0000 | [diff] [blame] | 1245 | { |
| 1246 | int conf_ttl = daemon->use_dhcp_ttl ? daemon->dhcp_ttl : daemon->local_ttl; |
| 1247 | |
| 1248 | /* Apply ceiling of actual lease length to configured TTL. */ |
| 1249 | if (!(crecp->flags & F_IMMORTAL) && (crecp->ttd - now) < conf_ttl) |
| 1250 | return crecp->ttd - now; |
| 1251 | |
| 1252 | return conf_ttl; |
| 1253 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1254 | |
Simon Kelley | df3d54f | 2016-02-24 21:03:38 +0000 | [diff] [blame] | 1255 | /* Immortal entries other than DHCP are local, and hold TTL in TTD field. */ |
| 1256 | if (crecp->flags & F_IMMORTAL) |
| 1257 | return crecp->ttd; |
| 1258 | |
klemens | 43517fc | 2017-02-19 15:53:37 +0000 | [diff] [blame] | 1259 | /* Return the Max TTL value if it is lower than the actual TTL */ |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1260 | if (daemon->max_ttl == 0 || ((unsigned)(crecp->ttd - now) < daemon->max_ttl)) |
| 1261 | return crecp->ttd - now; |
| 1262 | else |
| 1263 | return daemon->max_ttl; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1267 | /* return zero if we can't answer from cache, or packet size if we can */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 1268 | size_t answer_request(struct dns_header *header, char *limit, size_t qlen, |
Simon Kelley | 83349b8 | 2014-02-10 21:02:01 +0000 | [diff] [blame] | 1269 | struct in_addr local_addr, struct in_addr local_netmask, |
Simon Kelley | fa14bec | 2015-12-20 17:12:16 +0000 | [diff] [blame] | 1270 | time_t now, int ad_reqd, int do_bit, int have_pseudoheader) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1271 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1272 | char *name = daemon->namebuff; |
Simon Kelley | fa14bec | 2015-12-20 17:12:16 +0000 | [diff] [blame] | 1273 | unsigned char *p, *ansp; |
Simon Kelley | 3f7483e | 2014-03-16 22:56:58 +0000 | [diff] [blame] | 1274 | unsigned int qtype, qclass; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1275 | struct all_addr addr; |
Simon Kelley | b75e936 | 2012-12-07 11:50:41 +0000 | [diff] [blame] | 1276 | int nameoffset; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1277 | unsigned short flag; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1278 | int q, ans, anscount = 0, addncount = 0; |
Simon Kelley | fa14bec | 2015-12-20 17:12:16 +0000 | [diff] [blame] | 1279 | int dryrun = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1280 | struct crec *crecp; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1281 | int nxdomain = 0, auth = 1, trunc = 0, sec_data = 1; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1282 | struct mx_srv_record *rec; |
Simon Kelley | a25720a | 2014-01-14 23:13:55 +0000 | [diff] [blame] | 1283 | size_t len; |
Simon Kelley | fa78573 | 2016-07-22 20:56:01 +0100 | [diff] [blame] | 1284 | |
Simon Kelley | dd4ad9a | 2015-12-17 10:44:58 +0000 | [diff] [blame] | 1285 | if (ntohs(header->ancount) != 0 || |
| 1286 | ntohs(header->nscount) != 0 || |
| 1287 | ntohs(header->qdcount) == 0 || |
| 1288 | OPCODE(header) != QUERY ) |
| 1289 | return 0; |
Simon Kelley | 087eb76 | 2017-10-30 23:16:54 +0000 | [diff] [blame] | 1290 | |
| 1291 | /* always servfail queries with RD unset, to avoid cache snooping. */ |
| 1292 | if (!(header->hb3 & HB3_RD)) |
| 1293 | return setup_reply(header, qlen, NULL, F_SERVFAIL, 0); |
Simon Kelley | dd4ad9a | 2015-12-17 10:44:58 +0000 | [diff] [blame] | 1294 | |
Simon Kelley | e243c07 | 2014-02-06 18:14:09 +0000 | [diff] [blame] | 1295 | /* Don't return AD set if checking disabled. */ |
Simon Kelley | a25720a | 2014-01-14 23:13:55 +0000 | [diff] [blame] | 1296 | if (header->hb4 & HB4_CD) |
| 1297 | sec_data = 0; |
Simon Kelley | 83349b8 | 2014-02-10 21:02:01 +0000 | [diff] [blame] | 1298 | |
Simon Kelley | dd4ad9a | 2015-12-17 10:44:58 +0000 | [diff] [blame] | 1299 | /* If there is an additional data section then it will be overwritten by |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1300 | partial replies, so we have to do a dry run to see if we can answer |
Simon Kelley | dd4ad9a | 2015-12-17 10:44:58 +0000 | [diff] [blame] | 1301 | the query. */ |
Simon Kelley | dd4ad9a | 2015-12-17 10:44:58 +0000 | [diff] [blame] | 1302 | if (ntohs(header->arcount) != 0) |
Simon Kelley | fa14bec | 2015-12-20 17:12:16 +0000 | [diff] [blame] | 1303 | dryrun = 1; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1304 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1305 | for (rec = daemon->mxnames; rec; rec = rec->next) |
| 1306 | rec->offset = 0; |
| 1307 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1308 | rerun: |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1309 | /* determine end of question section (we put answers there) */ |
| 1310 | if (!(ansp = skip_questions(header, qlen))) |
| 1311 | return 0; /* bad packet */ |
| 1312 | |
| 1313 | /* now process each question, answers go in RRs after the question */ |
| 1314 | p = (unsigned char *)(header+1); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1315 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1316 | for (q = ntohs(header->qdcount); q != 0; q--) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1317 | { |
| 1318 | /* save pointer to name for copying into answers */ |
| 1319 | nameoffset = p - (unsigned char *)header; |
| 1320 | |
| 1321 | /* now extract name as .-concatenated string into name */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1322 | if (!extract_name(header, qlen, &p, name, 1, 4)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1323 | return 0; /* bad packet */ |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1324 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1325 | GETSHORT(qtype, p); |
| 1326 | GETSHORT(qclass, p); |
| 1327 | |
| 1328 | ans = 0; /* have we answered this question */ |
| 1329 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1330 | if (qtype == T_TXT || qtype == T_ANY) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1331 | { |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1332 | struct txt_record *t; |
| 1333 | for(t = daemon->txt; t ; t = t->next) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1334 | { |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1335 | if (t->class == qclass && hostname_isequal(name, t->name)) |
| 1336 | { |
| 1337 | ans = 1; |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 1338 | if (!dryrun) |
| 1339 | { |
Simon Kelley | fec216d | 2014-03-27 20:54:34 +0000 | [diff] [blame] | 1340 | unsigned long ttl = daemon->local_ttl; |
| 1341 | int ok = 1; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1342 | log_query(F_CONFIG | F_RRNAME, name, NULL, "<TXT>"); |
Kevin Darbyshire-Bryant | 7ac9ae1 | 2016-09-09 20:52:08 +0100 | [diff] [blame] | 1343 | #ifndef NO_ID |
Simon Kelley | fec216d | 2014-03-27 20:54:34 +0000 | [diff] [blame] | 1344 | /* Dynamically generate stat record */ |
| 1345 | if (t->stat != 0) |
| 1346 | { |
| 1347 | ttl = 0; |
| 1348 | if (!cache_make_stat(t)) |
| 1349 | ok = 0; |
| 1350 | } |
Kevin Darbyshire-Bryant | 7ac9ae1 | 2016-09-09 20:52:08 +0100 | [diff] [blame] | 1351 | #endif |
Simon Kelley | fec216d | 2014-03-27 20:54:34 +0000 | [diff] [blame] | 1352 | if (ok && add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1353 | ttl, NULL, |
| 1354 | T_TXT, t->class, "t", t->len, t->txt)) |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 1355 | anscount++; |
| 1356 | |
| 1357 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1358 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1359 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1360 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1361 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1362 | if (qclass == C_IN) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1363 | { |
Simon Kelley | 9f7f3b1 | 2012-05-28 21:39:57 +0100 | [diff] [blame] | 1364 | struct txt_record *t; |
| 1365 | |
| 1366 | for (t = daemon->rr; t; t = t->next) |
| 1367 | if ((t->class == qtype || qtype == T_ANY) && hostname_isequal(name, t->name)) |
| 1368 | { |
| 1369 | ans = 1; |
Simon Kelley | 93be5b1 | 2015-12-15 12:04:40 +0000 | [diff] [blame] | 1370 | sec_data = 0; |
Simon Kelley | 9f7f3b1 | 2012-05-28 21:39:57 +0100 | [diff] [blame] | 1371 | if (!dryrun) |
| 1372 | { |
| 1373 | log_query(F_CONFIG | F_RRNAME, name, NULL, "<RR>"); |
| 1374 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1375 | daemon->local_ttl, NULL, |
| 1376 | t->class, C_IN, "t", t->len, t->txt)) |
| 1377 | anscount ++; |
| 1378 | } |
| 1379 | } |
| 1380 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1381 | if (qtype == T_PTR || qtype == T_ANY) |
Simon Kelley | c1bb850 | 2004-08-11 18:40:17 +0100 | [diff] [blame] | 1382 | { |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1383 | /* see if it's w.z.y.z.in-addr.arpa format */ |
| 1384 | int is_arpa = in_arpa_name_2_addr(name, &addr); |
| 1385 | struct ptr_record *ptr; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1386 | struct interface_name* intr = NULL; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1387 | |
| 1388 | for (ptr = daemon->ptr; ptr; ptr = ptr->next) |
| 1389 | if (hostname_isequal(name, ptr->name)) |
| 1390 | break; |
| 1391 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1392 | if (is_arpa == F_IPV4) |
| 1393 | for (intr = daemon->int_names; intr; intr = intr->next) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1394 | { |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 1395 | struct addrlist *addrlist; |
| 1396 | |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 1397 | for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
| 1398 | if (!(addrlist->flags & ADDRLIST_IPV6) && addr.addr.addr4.s_addr == addrlist->addr.addr.addr4.s_addr) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 1399 | break; |
| 1400 | |
| 1401 | if (addrlist) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1402 | break; |
| 1403 | else |
| 1404 | while (intr->next && strcmp(intr->intr, intr->next->intr) == 0) |
| 1405 | intr = intr->next; |
| 1406 | } |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 1407 | #ifdef HAVE_IPV6 |
| 1408 | else if (is_arpa == F_IPV6) |
| 1409 | for (intr = daemon->int_names; intr; intr = intr->next) |
| 1410 | { |
| 1411 | struct addrlist *addrlist; |
| 1412 | |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 1413 | for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
| 1414 | if ((addrlist->flags & ADDRLIST_IPV6) && IN6_ARE_ADDR_EQUAL(&addr.addr.addr6, &addrlist->addr.addr.addr6)) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 1415 | break; |
| 1416 | |
| 1417 | if (addrlist) |
| 1418 | break; |
| 1419 | else |
| 1420 | while (intr->next && strcmp(intr->intr, intr->next->intr) == 0) |
| 1421 | intr = intr->next; |
| 1422 | } |
| 1423 | #endif |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1424 | |
| 1425 | if (intr) |
| 1426 | { |
Simon Kelley | 93be5b1 | 2015-12-15 12:04:40 +0000 | [diff] [blame] | 1427 | sec_data = 0; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1428 | ans = 1; |
| 1429 | if (!dryrun) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1430 | { |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 1431 | log_query(is_arpa | F_REVERSE | F_CONFIG, intr->name, &addr, NULL); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1432 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1433 | daemon->local_ttl, NULL, |
| 1434 | T_PTR, C_IN, "d", intr->name)) |
| 1435 | anscount++; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1436 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1437 | } |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1438 | else if (ptr) |
| 1439 | { |
| 1440 | ans = 1; |
Simon Kelley | 93be5b1 | 2015-12-15 12:04:40 +0000 | [diff] [blame] | 1441 | sec_data = 0; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1442 | if (!dryrun) |
| 1443 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1444 | log_query(F_CONFIG | F_RRNAME, name, NULL, "<PTR>"); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1445 | for (ptr = daemon->ptr; ptr; ptr = ptr->next) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1446 | if (hostname_isequal(name, ptr->name) && |
| 1447 | add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1448 | daemon->local_ttl, NULL, |
| 1449 | T_PTR, C_IN, "d", ptr->ptr)) |
| 1450 | anscount++; |
| 1451 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1452 | } |
| 1453 | } |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1454 | else if ((crecp = cache_find_by_addr(NULL, &addr, now, is_arpa))) |
Simon Kelley | 2d33bda | 2014-01-24 22:37:25 +0000 | [diff] [blame] | 1455 | { |
Simon Kelley | dd4ad9a | 2015-12-17 10:44:58 +0000 | [diff] [blame] | 1456 | /* Don't use cache when DNSSEC data required, unless we know that |
| 1457 | the zone is unsigned, which implies that we're doing |
| 1458 | validation. */ |
| 1459 | if ((crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) || |
Simon Kelley | fa14bec | 2015-12-20 17:12:16 +0000 | [diff] [blame] | 1460 | !do_bit || |
Simon Kelley | dd4ad9a | 2015-12-17 10:44:58 +0000 | [diff] [blame] | 1461 | (option_bool(OPT_DNSSEC_VALID) && !(crecp->flags & F_DNSSECOK))) |
Simon Kelley | 2d33bda | 2014-01-24 22:37:25 +0000 | [diff] [blame] | 1462 | { |
| 1463 | do |
| 1464 | { |
| 1465 | /* don't answer wildcard queries with data not from /etc/hosts or dhcp leases */ |
| 1466 | if (qtype == T_ANY && !(crecp->flags & (F_HOSTS | F_DHCP))) |
| 1467 | continue; |
| 1468 | |
| 1469 | if (!(crecp->flags & F_DNSSECOK)) |
| 1470 | sec_data = 0; |
Simon Kelley | 93be5b1 | 2015-12-15 12:04:40 +0000 | [diff] [blame] | 1471 | |
| 1472 | ans = 1; |
| 1473 | |
Simon Kelley | 2d33bda | 2014-01-24 22:37:25 +0000 | [diff] [blame] | 1474 | if (crecp->flags & F_NEG) |
| 1475 | { |
Simon Kelley | 2d33bda | 2014-01-24 22:37:25 +0000 | [diff] [blame] | 1476 | auth = 0; |
| 1477 | if (crecp->flags & F_NXDOMAIN) |
| 1478 | nxdomain = 1; |
| 1479 | if (!dryrun) |
| 1480 | log_query(crecp->flags & ~F_FORWARD, name, &addr, NULL); |
| 1481 | } |
Simon Kelley | 93be5b1 | 2015-12-15 12:04:40 +0000 | [diff] [blame] | 1482 | else |
Simon Kelley | 2d33bda | 2014-01-24 22:37:25 +0000 | [diff] [blame] | 1483 | { |
Simon Kelley | 2d33bda | 2014-01-24 22:37:25 +0000 | [diff] [blame] | 1484 | if (!(crecp->flags & (F_HOSTS | F_DHCP))) |
| 1485 | auth = 0; |
| 1486 | if (!dryrun) |
| 1487 | { |
| 1488 | log_query(crecp->flags & ~F_FORWARD, cache_get_name(crecp), &addr, |
| 1489 | record_source(crecp->uid)); |
| 1490 | |
| 1491 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1492 | crec_ttl(crecp, now), NULL, |
| 1493 | T_PTR, C_IN, "d", cache_get_name(crecp))) |
| 1494 | anscount++; |
| 1495 | } |
| 1496 | } |
| 1497 | } while ((crecp = cache_find_by_addr(crecp, &addr, now, is_arpa))); |
| 1498 | } |
| 1499 | } |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 1500 | else if (is_rev_synth(is_arpa, &addr, name)) |
| 1501 | { |
| 1502 | ans = 1; |
Simon Kelley | 93be5b1 | 2015-12-15 12:04:40 +0000 | [diff] [blame] | 1503 | sec_data = 0; |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 1504 | if (!dryrun) |
| 1505 | { |
| 1506 | log_query(F_CONFIG | F_REVERSE | is_arpa, name, &addr, NULL); |
| 1507 | |
| 1508 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1509 | daemon->local_ttl, NULL, |
| 1510 | T_PTR, C_IN, "d", name)) |
| 1511 | anscount++; |
| 1512 | } |
| 1513 | } |
Simon Kelley | fca008d | 2017-02-19 18:50:41 +0000 | [diff] [blame] | 1514 | else if (option_bool(OPT_BOGUSPRIV) && ( |
| 1515 | #ifdef HAVE_IPV6 |
| 1516 | (is_arpa == F_IPV6 && private_net6(&addr.addr.addr6)) || |
| 1517 | #endif |
| 1518 | (is_arpa == F_IPV4 && private_net(addr.addr.addr4, 1)))) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1519 | { |
Vladislav Grishenko | 5a7212c | 2017-04-24 22:19:57 +0100 | [diff] [blame] | 1520 | struct server *serv; |
| 1521 | unsigned int namelen = strlen(name); |
| 1522 | char *nameend = name + namelen; |
| 1523 | |
| 1524 | /* see if have rev-server set */ |
| 1525 | for (serv = daemon->servers; serv; serv = serv->next) |
| 1526 | { |
| 1527 | unsigned int domainlen; |
| 1528 | char *matchstart; |
| 1529 | |
| 1530 | if ((serv->flags & (SERV_HAS_DOMAIN | SERV_NO_ADDR)) != SERV_HAS_DOMAIN) |
| 1531 | continue; |
| 1532 | |
| 1533 | domainlen = strlen(serv->domain); |
| 1534 | if (domainlen == 0 || domainlen > namelen) |
| 1535 | continue; |
| 1536 | |
| 1537 | matchstart = nameend - domainlen; |
| 1538 | if (hostname_isequal(matchstart, serv->domain) && |
| 1539 | (namelen == domainlen || *(matchstart-1) == '.' )) |
| 1540 | break; |
| 1541 | } |
| 1542 | |
| 1543 | /* if no configured server, not in cache, enabled and private IPV4 address, return NXDOMAIN */ |
| 1544 | if (!serv) |
| 1545 | { |
| 1546 | ans = 1; |
| 1547 | sec_data = 0; |
| 1548 | nxdomain = 1; |
| 1549 | if (!dryrun) |
| 1550 | log_query(F_CONFIG | F_REVERSE | is_arpa | F_NEG | F_NXDOMAIN, |
| 1551 | name, &addr, NULL); |
| 1552 | } |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1553 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1554 | } |
Simon Kelley | fca008d | 2017-02-19 18:50:41 +0000 | [diff] [blame] | 1555 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1556 | for (flag = F_IPV4; flag; flag = (flag == F_IPV4) ? F_IPV6 : 0) |
| 1557 | { |
| 1558 | unsigned short type = T_A; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 1559 | struct interface_name *intr; |
| 1560 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1561 | if (flag == F_IPV6) |
| 1562 | #ifdef HAVE_IPV6 |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1563 | type = T_AAAA; |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1564 | #else |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1565 | break; |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1566 | #endif |
| 1567 | |
| 1568 | if (qtype != type && qtype != T_ANY) |
| 1569 | continue; |
| 1570 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1571 | /* interface name stuff */ |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1572 | intname_restart: |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 1573 | for (intr = daemon->int_names; intr; intr = intr->next) |
| 1574 | if (hostname_isequal(name, intr->name)) |
| 1575 | break; |
| 1576 | |
| 1577 | if (intr) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1578 | { |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 1579 | struct addrlist *addrlist; |
Simon Kelley | d42d470 | 2017-02-02 16:52:06 +0000 | [diff] [blame] | 1580 | int gotit = 0, localise = 0; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1581 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 1582 | enumerate_interfaces(0); |
Simon Kelley | d42d470 | 2017-02-02 16:52:06 +0000 | [diff] [blame] | 1583 | |
klemens | 43517fc | 2017-02-19 15:53:37 +0000 | [diff] [blame] | 1584 | /* See if a putative address is on the network from which we received |
Simon Kelley | d42d470 | 2017-02-02 16:52:06 +0000 | [diff] [blame] | 1585 | the query, is so we'll filter other answers. */ |
| 1586 | if (local_addr.s_addr != 0 && option_bool(OPT_LOCALISE) && type == T_A) |
| 1587 | for (intr = daemon->int_names; intr; intr = intr->next) |
| 1588 | if (hostname_isequal(name, intr->name)) |
| 1589 | for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
| 1590 | #ifdef HAVE_IPV6 |
| 1591 | if (!(addrlist->flags & ADDRLIST_IPV6)) |
| 1592 | #endif |
| 1593 | if (is_same_net(*((struct in_addr *)&addrlist->addr), local_addr, local_netmask)) |
| 1594 | { |
| 1595 | localise = 1; |
| 1596 | break; |
| 1597 | } |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1598 | |
Simon Kelley | fb63dd1 | 2013-10-21 18:19:35 +0100 | [diff] [blame] | 1599 | for (intr = daemon->int_names; intr; intr = intr->next) |
| 1600 | if (hostname_isequal(name, intr->name)) |
| 1601 | { |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 1602 | for (addrlist = intr->addr; addrlist; addrlist = addrlist->next) |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 1603 | #ifdef HAVE_IPV6 |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 1604 | if (((addrlist->flags & ADDRLIST_IPV6) ? T_AAAA : T_A) == type) |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 1605 | #endif |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 1606 | { |
Simon Kelley | d42d470 | 2017-02-02 16:52:06 +0000 | [diff] [blame] | 1607 | if (localise && |
| 1608 | !is_same_net(*((struct in_addr *)&addrlist->addr), local_addr, local_netmask)) |
| 1609 | continue; |
| 1610 | |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 1611 | #ifdef HAVE_IPV6 |
| 1612 | if (addrlist->flags & ADDRLIST_REVONLY) |
| 1613 | continue; |
| 1614 | #endif |
| 1615 | ans = 1; |
Simon Kelley | 93be5b1 | 2015-12-15 12:04:40 +0000 | [diff] [blame] | 1616 | sec_data = 0; |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 1617 | if (!dryrun) |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 1618 | { |
| 1619 | gotit = 1; |
| 1620 | log_query(F_FORWARD | F_CONFIG | flag, name, &addrlist->addr, NULL); |
| 1621 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1622 | daemon->local_ttl, NULL, type, C_IN, |
| 1623 | type == T_A ? "4" : "6", &addrlist->addr)) |
| 1624 | anscount++; |
| 1625 | } |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 1626 | } |
Simon Kelley | fb63dd1 | 2013-10-21 18:19:35 +0100 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | if (!dryrun && !gotit) |
| 1630 | log_query(F_FORWARD | F_CONFIG | flag | F_NEG, name, NULL, NULL); |
| 1631 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 1632 | continue; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1633 | } |
| 1634 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1635 | cname_restart: |
Simon Kelley | 12fae49 | 2014-02-04 22:03:06 +0000 | [diff] [blame] | 1636 | if ((crecp = cache_find_by_name(NULL, name, now, flag | F_CNAME | (dryrun ? F_NO_RR : 0)))) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1637 | { |
| 1638 | int localise = 0; |
| 1639 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 1640 | /* See if a putative address is on the network from which we received |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1641 | the query, is so we'll filter other answers. */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1642 | if (local_addr.s_addr != 0 && option_bool(OPT_LOCALISE) && flag == F_IPV4) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1643 | { |
| 1644 | struct crec *save = crecp; |
| 1645 | do { |
| 1646 | if ((crecp->flags & F_HOSTS) && |
| 1647 | is_same_net(*((struct in_addr *)&crecp->addr), local_addr, local_netmask)) |
| 1648 | { |
| 1649 | localise = 1; |
| 1650 | break; |
| 1651 | } |
| 1652 | } while ((crecp = cache_find_by_name(crecp, name, now, flag | F_CNAME))); |
| 1653 | crecp = save; |
| 1654 | } |
Simon Kelley | 824202e | 2014-01-23 20:59:46 +0000 | [diff] [blame] | 1655 | |
Simon Kelley | 93be5b1 | 2015-12-15 12:04:40 +0000 | [diff] [blame] | 1656 | /* If the client asked for DNSSEC don't use cached data. */ |
Simon Kelley | fa14bec | 2015-12-20 17:12:16 +0000 | [diff] [blame] | 1657 | if ((crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) || !do_bit || !(crecp->flags & F_DNSSECOK)) |
Simon Kelley | 824202e | 2014-01-23 20:59:46 +0000 | [diff] [blame] | 1658 | do |
| 1659 | { |
| 1660 | /* don't answer wildcard queries with data not from /etc/hosts |
| 1661 | or DHCP leases */ |
| 1662 | if (qtype == T_ANY && !(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG))) |
| 1663 | break; |
| 1664 | |
| 1665 | if (!(crecp->flags & F_DNSSECOK)) |
| 1666 | sec_data = 0; |
| 1667 | |
| 1668 | if (crecp->flags & F_CNAME) |
| 1669 | { |
| 1670 | char *cname_target = cache_get_cname_target(crecp); |
| 1671 | |
| 1672 | if (!dryrun) |
| 1673 | { |
| 1674 | log_query(crecp->flags, name, NULL, record_source(crecp->uid)); |
| 1675 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1676 | crec_ttl(crecp, now), &nameoffset, |
| 1677 | T_CNAME, C_IN, "d", cname_target)) |
| 1678 | anscount++; |
| 1679 | } |
| 1680 | |
| 1681 | strcpy(name, cname_target); |
| 1682 | /* check if target interface_name */ |
Andy | 3e21a1a | 2014-03-22 19:10:07 +0000 | [diff] [blame] | 1683 | if (crecp->addr.cname.uid == SRC_INTERFACE) |
Simon Kelley | 824202e | 2014-01-23 20:59:46 +0000 | [diff] [blame] | 1684 | goto intname_restart; |
| 1685 | else |
| 1686 | goto cname_restart; |
| 1687 | } |
| 1688 | |
| 1689 | if (crecp->flags & F_NEG) |
| 1690 | { |
Simon Kelley | fa14bec | 2015-12-20 17:12:16 +0000 | [diff] [blame] | 1691 | ans = 1; |
| 1692 | auth = 0; |
| 1693 | if (crecp->flags & F_NXDOMAIN) |
| 1694 | nxdomain = 1; |
| 1695 | if (!dryrun) |
| 1696 | log_query(crecp->flags, name, NULL, NULL); |
Simon Kelley | 824202e | 2014-01-23 20:59:46 +0000 | [diff] [blame] | 1697 | } |
| 1698 | else |
| 1699 | { |
| 1700 | /* If we are returning local answers depending on network, |
| 1701 | filter here. */ |
| 1702 | if (localise && |
| 1703 | (crecp->flags & F_HOSTS) && |
| 1704 | !is_same_net(*((struct in_addr *)&crecp->addr), local_addr, local_netmask)) |
| 1705 | continue; |
| 1706 | |
| 1707 | if (!(crecp->flags & (F_HOSTS | F_DHCP))) |
| 1708 | auth = 0; |
| 1709 | |
| 1710 | ans = 1; |
| 1711 | if (!dryrun) |
| 1712 | { |
| 1713 | log_query(crecp->flags & ~F_REVERSE, name, &crecp->addr.addr, |
| 1714 | record_source(crecp->uid)); |
| 1715 | |
| 1716 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1717 | crec_ttl(crecp, now), NULL, type, C_IN, |
| 1718 | type == T_A ? "4" : "6", &crecp->addr)) |
| 1719 | anscount++; |
| 1720 | } |
| 1721 | } |
| 1722 | } while ((crecp = cache_find_by_name(crecp, name, now, flag | F_CNAME))); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1723 | } |
Simon Kelley | 2bb73af | 2013-04-24 17:38:19 +0100 | [diff] [blame] | 1724 | else if (is_name_synthetic(flag, name, &addr)) |
| 1725 | { |
| 1726 | ans = 1; |
| 1727 | if (!dryrun) |
| 1728 | { |
| 1729 | log_query(F_FORWARD | F_CONFIG | flag, name, &addr, NULL); |
| 1730 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1731 | daemon->local_ttl, NULL, type, C_IN, type == T_A ? "4" : "6", &addr)) |
| 1732 | anscount++; |
| 1733 | } |
| 1734 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1735 | } |
Simon Kelley | d1c759c | 2012-04-16 17:26:19 +0100 | [diff] [blame] | 1736 | |
| 1737 | if (qtype == T_CNAME || qtype == T_ANY) |
| 1738 | { |
Simon Kelley | a6004d7 | 2017-10-25 17:48:19 +0100 | [diff] [blame] | 1739 | if ((crecp = cache_find_by_name(NULL, name, now, F_CNAME | (dryrun ? F_NO_RR : 0))) && |
| 1740 | (qtype == T_CNAME || (crecp->flags & F_CONFIG)) && |
| 1741 | ((crecp->flags & F_CONFIG) || !do_bit || !(crecp->flags & F_DNSSECOK))) |
Simon Kelley | d1c759c | 2012-04-16 17:26:19 +0100 | [diff] [blame] | 1742 | { |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1743 | if (!(crecp->flags & F_DNSSECOK)) |
| 1744 | sec_data = 0; |
| 1745 | |
Simon Kelley | d1c759c | 2012-04-16 17:26:19 +0100 | [diff] [blame] | 1746 | ans = 1; |
| 1747 | if (!dryrun) |
| 1748 | { |
| 1749 | log_query(crecp->flags, name, NULL, record_source(crecp->uid)); |
| 1750 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, |
| 1751 | crec_ttl(crecp, now), &nameoffset, |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1752 | T_CNAME, C_IN, "d", cache_get_cname_target(crecp))) |
Simon Kelley | d1c759c | 2012-04-16 17:26:19 +0100 | [diff] [blame] | 1753 | anscount++; |
| 1754 | } |
| 1755 | } |
| 1756 | } |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 1757 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1758 | if (qtype == T_MX || qtype == T_ANY) |
| 1759 | { |
| 1760 | int found = 0; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1761 | for (rec = daemon->mxnames; rec; rec = rec->next) |
| 1762 | if (!rec->issrv && hostname_isequal(name, rec->name)) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1763 | { |
| 1764 | ans = found = 1; |
| 1765 | if (!dryrun) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1766 | { |
Simon Kelley | e1ff419 | 2012-12-09 17:08:47 +0000 | [diff] [blame] | 1767 | int offset; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1768 | log_query(F_CONFIG | F_RRNAME, name, NULL, "<MX>"); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1769 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->local_ttl, |
| 1770 | &offset, T_MX, C_IN, "sd", rec->weight, rec->target)) |
| 1771 | { |
| 1772 | anscount++; |
| 1773 | if (rec->target) |
| 1774 | rec->offset = offset; |
| 1775 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1776 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1779 | if (!found && (option_bool(OPT_SELFMX) || option_bool(OPT_LOCALMX)) && |
Simon Kelley | 12fae49 | 2014-02-04 22:03:06 +0000 | [diff] [blame] | 1780 | cache_find_by_name(NULL, name, now, F_HOSTS | F_DHCP | F_NO_RR)) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1781 | { |
| 1782 | ans = 1; |
| 1783 | if (!dryrun) |
| 1784 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1785 | log_query(F_CONFIG | F_RRNAME, name, NULL, "<MX>"); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1786 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->local_ttl, NULL, |
| 1787 | T_MX, C_IN, "sd", 1, |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1788 | option_bool(OPT_SELFMX) ? name : daemon->mxtarget)) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1789 | anscount++; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1790 | } |
| 1791 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | if (qtype == T_SRV || qtype == T_ANY) |
| 1795 | { |
| 1796 | int found = 0; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1797 | struct mx_srv_record *move = NULL, **up = &daemon->mxnames; |
| 1798 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1799 | for (rec = daemon->mxnames; rec; rec = rec->next) |
| 1800 | if (rec->issrv && hostname_isequal(name, rec->name)) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1801 | { |
| 1802 | found = ans = 1; |
| 1803 | if (!dryrun) |
| 1804 | { |
Simon Kelley | e1ff419 | 2012-12-09 17:08:47 +0000 | [diff] [blame] | 1805 | int offset; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1806 | log_query(F_CONFIG | F_RRNAME, name, NULL, "<SRV>"); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1807 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->local_ttl, |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1808 | &offset, T_SRV, C_IN, "sssd", |
| 1809 | rec->priority, rec->weight, rec->srvport, rec->target)) |
| 1810 | { |
| 1811 | anscount++; |
| 1812 | if (rec->target) |
| 1813 | rec->offset = offset; |
| 1814 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1815 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1816 | |
| 1817 | /* unlink first SRV record found */ |
| 1818 | if (!move) |
| 1819 | { |
| 1820 | move = rec; |
| 1821 | *up = rec->next; |
| 1822 | } |
| 1823 | else |
| 1824 | up = &rec->next; |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1825 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1826 | else |
| 1827 | up = &rec->next; |
| 1828 | |
| 1829 | /* put first SRV record back at the end. */ |
| 1830 | if (move) |
| 1831 | { |
| 1832 | *up = move; |
| 1833 | move->next = NULL; |
| 1834 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1835 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1836 | if (!found && option_bool(OPT_FILTER) && (qtype == T_SRV || (qtype == T_ANY && strchr(name, '_')))) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1837 | { |
| 1838 | ans = 1; |
| 1839 | if (!dryrun) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1840 | log_query(F_CONFIG | F_NEG, name, NULL, NULL); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1841 | } |
| 1842 | } |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1843 | |
| 1844 | if (qtype == T_NAPTR || qtype == T_ANY) |
| 1845 | { |
| 1846 | struct naptr *na; |
| 1847 | for (na = daemon->naptr; na; na = na->next) |
| 1848 | if (hostname_isequal(name, na->name)) |
| 1849 | { |
| 1850 | ans = 1; |
| 1851 | if (!dryrun) |
| 1852 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1853 | log_query(F_CONFIG | F_RRNAME, name, NULL, "<NAPTR>"); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1854 | if (add_resource_record(header, limit, &trunc, nameoffset, &ansp, daemon->local_ttl, |
| 1855 | NULL, T_NAPTR, C_IN, "sszzzd", |
| 1856 | na->order, na->pref, na->flags, na->services, na->regexp, na->replace)) |
| 1857 | anscount++; |
| 1858 | } |
| 1859 | } |
| 1860 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1861 | |
| 1862 | if (qtype == T_MAILB) |
| 1863 | ans = 1, nxdomain = 1; |
| 1864 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1865 | if (qtype == T_SOA && option_bool(OPT_FILTER)) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1866 | { |
| 1867 | ans = 1; |
| 1868 | if (!dryrun) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1869 | log_query(F_CONFIG | F_NEG, name, &addr, NULL); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1870 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1871 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1872 | |
| 1873 | if (!ans) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1874 | return 0; /* failed to answer a question */ |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1875 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 1876 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1877 | if (dryrun) |
| 1878 | { |
| 1879 | dryrun = 0; |
| 1880 | goto rerun; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1883 | /* create an additional data section, for stuff in SRV and MX record replies. */ |
| 1884 | for (rec = daemon->mxnames; rec; rec = rec->next) |
| 1885 | if (rec->offset != 0) |
| 1886 | { |
| 1887 | /* squash dupes */ |
| 1888 | struct mx_srv_record *tmp; |
| 1889 | for (tmp = rec->next; tmp; tmp = tmp->next) |
| 1890 | if (tmp->offset != 0 && hostname_isequal(rec->target, tmp->target)) |
| 1891 | tmp->offset = 0; |
| 1892 | |
| 1893 | crecp = NULL; |
| 1894 | while ((crecp = cache_find_by_name(crecp, rec->target, now, F_IPV4 | F_IPV6))) |
| 1895 | { |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1896 | #ifdef HAVE_IPV6 |
| 1897 | int type = crecp->flags & F_IPV4 ? T_A : T_AAAA; |
| 1898 | #else |
| 1899 | int type = T_A; |
| 1900 | #endif |
| 1901 | if (crecp->flags & F_NEG) |
| 1902 | continue; |
| 1903 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1904 | if (add_resource_record(header, limit, NULL, rec->offset, &ansp, |
| 1905 | crec_ttl(crecp, now), NULL, type, C_IN, |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1906 | crecp->flags & F_IPV4 ? "4" : "6", &crecp->addr)) |
| 1907 | addncount++; |
| 1908 | } |
| 1909 | } |
| 1910 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1911 | /* done all questions, set up header and return length of result */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 1912 | /* clear authoritative and truncated flags, set QR flag */ |
| 1913 | header->hb3 = (header->hb3 & ~(HB3_AA | HB3_TC)) | HB3_QR; |
| 1914 | /* set RA flag */ |
| 1915 | header->hb4 |= HB4_RA; |
| 1916 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 1917 | /* authoritative - only hosts and DHCP derived names. */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 1918 | if (auth) |
| 1919 | header->hb3 |= HB3_AA; |
| 1920 | |
| 1921 | /* truncation */ |
| 1922 | if (trunc) |
| 1923 | header->hb3 |= HB3_TC; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1924 | |
Simon Kelley | 45cca58 | 2013-10-15 10:20:13 +0100 | [diff] [blame] | 1925 | if (nxdomain) |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 1926 | SET_RCODE(header, NXDOMAIN); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1927 | else |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 1928 | SET_RCODE(header, NOERROR); /* no error */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1929 | header->ancount = htons(anscount); |
| 1930 | header->nscount = htons(0); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1931 | header->arcount = htons(addncount); |
Simon Kelley | e243c07 | 2014-02-06 18:14:09 +0000 | [diff] [blame] | 1932 | |
Simon Kelley | a25720a | 2014-01-14 23:13:55 +0000 | [diff] [blame] | 1933 | len = ansp - (unsigned char *)header; |
| 1934 | |
Simon Kelley | fa14bec | 2015-12-20 17:12:16 +0000 | [diff] [blame] | 1935 | /* Advertise our packet size limit in our reply */ |
Simon Kelley | a25720a | 2014-01-14 23:13:55 +0000 | [diff] [blame] | 1936 | if (have_pseudoheader) |
Simon Kelley | c7f3bd2 | 2016-02-28 21:48:34 +0000 | [diff] [blame] | 1937 | len = add_pseudoheader(header, len, (unsigned char *)limit, daemon->edns_pktsz, 0, NULL, 0, do_bit, 0); |
Simon Kelley | e243c07 | 2014-02-06 18:14:09 +0000 | [diff] [blame] | 1938 | |
Simon Kelley | fa14bec | 2015-12-20 17:12:16 +0000 | [diff] [blame] | 1939 | if (ad_reqd && sec_data) |
Simon Kelley | e243c07 | 2014-02-06 18:14:09 +0000 | [diff] [blame] | 1940 | header->hb4 |= HB4_AD; |
Simon Kelley | 83349b8 | 2014-02-10 21:02:01 +0000 | [diff] [blame] | 1941 | else |
| 1942 | header->hb4 &= ~HB4_AD; |
Simon Kelley | a25720a | 2014-01-14 23:13:55 +0000 | [diff] [blame] | 1943 | |
Simon Kelley | 7c28612 | 2014-01-27 21:38:11 +0000 | [diff] [blame] | 1944 | return len; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1945 | } |