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