Simon Kelley | d1ced3a | 2018-01-01 22:18:03 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2018 Simon Kelley |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 19 | static struct crec *cache_head = NULL, *cache_tail = NULL, **hash_table = NULL; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 20 | #ifdef HAVE_DHCP |
| 21 | static struct crec *dhcp_spare = NULL; |
| 22 | #endif |
| 23 | static struct crec *new_chain = NULL; |
Julian Kornberger | aba8bbb | 2018-07-21 21:55:08 +0100 | [diff] [blame] | 24 | static int insert_error; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 25 | static union bigname *big_free = NULL; |
| 26 | static int bignames_left, hash_size; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 27 | |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 28 | static void make_non_terminals(struct crec *source); |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 29 | static struct crec *really_insert(char *name, union all_addr *addr, unsigned short class, |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 30 | time_t now, unsigned long ttl, unsigned int flags); |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 31 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 32 | /* type->string mapping: this is also used by the name-hash function as a mixing table. */ |
| 33 | static const struct { |
| 34 | unsigned int type; |
| 35 | const char * const name; |
| 36 | } typestr[] = { |
| 37 | { 1, "A" }, |
| 38 | { 2, "NS" }, |
| 39 | { 5, "CNAME" }, |
| 40 | { 6, "SOA" }, |
| 41 | { 10, "NULL" }, |
| 42 | { 11, "WKS" }, |
| 43 | { 12, "PTR" }, |
| 44 | { 13, "HINFO" }, |
| 45 | { 15, "MX" }, |
| 46 | { 16, "TXT" }, |
| 47 | { 22, "NSAP" }, |
| 48 | { 23, "NSAP_PTR" }, |
| 49 | { 24, "SIG" }, |
| 50 | { 25, "KEY" }, |
| 51 | { 28, "AAAA" }, |
Simon Kelley | aa6f832 | 2017-10-27 22:52:26 +0100 | [diff] [blame] | 52 | { 29, "LOC" }, |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 53 | { 33, "SRV" }, |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 54 | { 35, "NAPTR" }, |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 55 | { 36, "KX" }, |
| 56 | { 37, "CERT" }, |
| 57 | { 38, "A6" }, |
| 58 | { 39, "DNAME" }, |
| 59 | { 41, "OPT" }, |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 60 | { 43, "DS" }, |
| 61 | { 46, "RRSIG" }, |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 62 | { 47, "NSEC" }, |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 63 | { 48, "DNSKEY" }, |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 64 | { 50, "NSEC3" }, |
Simon Kelley | aa6f832 | 2017-10-27 22:52:26 +0100 | [diff] [blame] | 65 | { 51, "NSEC3PARAM" }, |
| 66 | { 52, "TLSA" }, |
| 67 | { 53, "SMIMEA" }, |
| 68 | { 55, "HIP" }, |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 69 | { 249, "TKEY" }, |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 70 | { 250, "TSIG" }, |
| 71 | { 251, "IXFR" }, |
| 72 | { 252, "AXFR" }, |
| 73 | { 253, "MAILB" }, |
| 74 | { 254, "MAILA" }, |
Simon Kelley | b758b67 | 2018-08-23 21:41:23 +0100 | [diff] [blame] | 75 | { 255, "ANY" }, |
| 76 | { 257, "CAA" } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 79 | static void cache_free(struct crec *crecp); |
| 80 | static void cache_unlink(struct crec *crecp); |
| 81 | static void cache_link(struct crec *crecp); |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 82 | static void rehash(int size); |
| 83 | static void cache_hash(struct crec *crecp); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 84 | |
Simon Kelley | 45d8a24 | 2018-07-17 21:01:14 +0100 | [diff] [blame] | 85 | void next_uid(struct crec *crecp) |
Simon Kelley | 3f7483e | 2014-03-16 22:56:58 +0000 | [diff] [blame] | 86 | { |
Andy | d508215 | 2014-03-17 19:50:29 +0000 | [diff] [blame] | 87 | static unsigned int uid = 0; |
Simon Kelley | 3f7483e | 2014-03-16 22:56:58 +0000 | [diff] [blame] | 88 | |
Simon Kelley | 45d8a24 | 2018-07-17 21:01:14 +0100 | [diff] [blame] | 89 | if (crecp->uid == UID_NONE) |
| 90 | { |
| 91 | uid++; |
Andy | d508215 | 2014-03-17 19:50:29 +0000 | [diff] [blame] | 92 | |
Simon Kelley | 45d8a24 | 2018-07-17 21:01:14 +0100 | [diff] [blame] | 93 | /* uid == 0 used to indicate CNAME to interface name. */ |
| 94 | if (uid == UID_NONE) |
| 95 | uid++; |
| 96 | |
| 97 | crecp->uid = uid; |
| 98 | } |
Simon Kelley | 3f7483e | 2014-03-16 22:56:58 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 101 | void cache_init(void) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 102 | { |
| 103 | struct crec *crecp; |
| 104 | int i; |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 105 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 106 | bignames_left = daemon->cachesize/10; |
| 107 | |
| 108 | if (daemon->cachesize > 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 109 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 110 | crecp = safe_malloc(daemon->cachesize*sizeof(struct crec)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 111 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 112 | for (i=0; i < daemon->cachesize; i++, crecp++) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 113 | { |
| 114 | cache_link(crecp); |
| 115 | crecp->flags = 0; |
Simon Kelley | 45d8a24 | 2018-07-17 21:01:14 +0100 | [diff] [blame] | 116 | crecp->uid = UID_NONE; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 120 | /* create initial hash table*/ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 121 | rehash(daemon->cachesize); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 124 | /* In most cases, we create the hash table once here by calling this with (hash_table == NULL) |
| 125 | but if the hosts file(s) are big (some people have 50000 ad-block entries), the table |
| 126 | will be much too small, so the hosts reading code calls rehash every 1000 addresses, to |
| 127 | expand the table. */ |
| 128 | static void rehash(int size) |
| 129 | { |
| 130 | struct crec **new, **old, *p, *tmp; |
| 131 | int i, new_size, old_size; |
| 132 | |
| 133 | /* hash_size is a power of two. */ |
| 134 | for (new_size = 64; new_size < size/10; new_size = new_size << 1); |
| 135 | |
| 136 | /* must succeed in getting first instance, failure later is non-fatal */ |
| 137 | if (!hash_table) |
| 138 | new = safe_malloc(new_size * sizeof(struct crec *)); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 139 | else if (new_size <= hash_size || !(new = whine_malloc(new_size * sizeof(struct crec *)))) |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 140 | return; |
| 141 | |
| 142 | for(i = 0; i < new_size; i++) |
| 143 | new[i] = NULL; |
| 144 | |
| 145 | old = hash_table; |
| 146 | old_size = hash_size; |
| 147 | hash_table = new; |
| 148 | hash_size = new_size; |
| 149 | |
| 150 | if (old) |
| 151 | { |
| 152 | for (i = 0; i < old_size; i++) |
| 153 | for (p = old[i]; p ; p = tmp) |
| 154 | { |
| 155 | tmp = p->hash_next; |
| 156 | cache_hash(p); |
| 157 | } |
| 158 | free(old); |
| 159 | } |
| 160 | } |
| 161 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 162 | static struct crec **hash_bucket(char *name) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 163 | { |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 164 | unsigned int c, val = 017465; /* Barker code - minimum self-correlation in cyclic shift */ |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 165 | const unsigned char *mix_tab = (const unsigned char*)typestr; |
| 166 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 167 | while((c = (unsigned char) *name++)) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 168 | { |
| 169 | /* don't use tolower and friends here - they may be messed up by LOCALE */ |
| 170 | if (c >= 'A' && c <= 'Z') |
| 171 | c += 'a' - 'A'; |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 172 | val = ((val << 7) | (val >> (32 - 7))) + (mix_tab[(val + c) & 0x3F] ^ c); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 173 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 174 | |
| 175 | /* hash_size is a power of two */ |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 176 | return hash_table + ((val ^ (val >> 16)) & (hash_size - 1)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static void cache_hash(struct crec *crecp) |
| 180 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 181 | /* maintain an invariant that all entries with F_REVERSE set |
| 182 | are at the start of the hash-chain and all non-reverse |
| 183 | immortal entries are at the end of the hash-chain. |
| 184 | This allows reverse searches and garbage collection to be optimised */ |
| 185 | |
| 186 | struct crec **up = hash_bucket(cache_get_name(crecp)); |
| 187 | |
| 188 | if (!(crecp->flags & F_REVERSE)) |
| 189 | { |
| 190 | while (*up && ((*up)->flags & F_REVERSE)) |
| 191 | up = &((*up)->hash_next); |
| 192 | |
| 193 | if (crecp->flags & F_IMMORTAL) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 194 | while (*up && !((*up)->flags & F_IMMORTAL)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 195 | up = &((*up)->hash_next); |
| 196 | } |
| 197 | crecp->hash_next = *up; |
| 198 | *up = crecp; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 199 | } |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 200 | |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 201 | static void cache_blockdata_free(struct crec *crecp) |
| 202 | { |
Simon Kelley | 4bf62f6 | 2019-01-10 21:54:22 +0000 | [diff] [blame] | 203 | if (!(crecp->flags & F_NEG)) |
| 204 | { |
| 205 | if (crecp->flags & F_SRV) |
| 206 | blockdata_free(crecp->addr.srv.target); |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 207 | #ifdef HAVE_DNSSEC |
Simon Kelley | 4bf62f6 | 2019-01-10 21:54:22 +0000 | [diff] [blame] | 208 | else if (crecp->flags & F_DNSKEY) |
| 209 | blockdata_free(crecp->addr.key.keydata); |
| 210 | else if (crecp->flags & F_DS) |
| 211 | blockdata_free(crecp->addr.ds.keydata); |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 212 | #endif |
Simon Kelley | 4bf62f6 | 2019-01-10 21:54:22 +0000 | [diff] [blame] | 213 | } |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 214 | } |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 215 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 216 | static void cache_free(struct crec *crecp) |
| 217 | { |
| 218 | crecp->flags &= ~F_FORWARD; |
| 219 | crecp->flags &= ~F_REVERSE; |
Simon Kelley | 45d8a24 | 2018-07-17 21:01:14 +0100 | [diff] [blame] | 220 | crecp->uid = UID_NONE; /* invalidate CNAMES pointing to this. */ |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 221 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 222 | if (cache_tail) |
| 223 | cache_tail->next = crecp; |
| 224 | else |
| 225 | cache_head = crecp; |
| 226 | crecp->prev = cache_tail; |
| 227 | crecp->next = NULL; |
| 228 | cache_tail = crecp; |
| 229 | |
| 230 | /* retrieve big name for further use. */ |
| 231 | if (crecp->flags & F_BIGNAME) |
| 232 | { |
| 233 | crecp->name.bname->next = big_free; |
| 234 | big_free = crecp->name.bname; |
| 235 | crecp->flags &= ~F_BIGNAME; |
| 236 | } |
Simon Kelley | 072e81b | 2014-01-31 12:42:54 +0000 | [diff] [blame] | 237 | |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 238 | cache_blockdata_free(crecp); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* insert a new cache entry at the head of the list (youngest entry) */ |
| 242 | static void cache_link(struct crec *crecp) |
| 243 | { |
| 244 | if (cache_head) /* check needed for init code */ |
| 245 | cache_head->prev = crecp; |
| 246 | crecp->next = cache_head; |
| 247 | crecp->prev = NULL; |
| 248 | cache_head = crecp; |
| 249 | if (!cache_tail) |
| 250 | cache_tail = crecp; |
| 251 | } |
| 252 | |
| 253 | /* remove an arbitrary cache entry for promotion */ |
| 254 | static void cache_unlink (struct crec *crecp) |
| 255 | { |
| 256 | if (crecp->prev) |
| 257 | crecp->prev->next = crecp->next; |
| 258 | else |
| 259 | cache_head = crecp->next; |
| 260 | |
| 261 | if (crecp->next) |
| 262 | crecp->next->prev = crecp->prev; |
| 263 | else |
| 264 | cache_tail = crecp->prev; |
| 265 | } |
| 266 | |
| 267 | char *cache_get_name(struct crec *crecp) |
| 268 | { |
| 269 | if (crecp->flags & F_BIGNAME) |
| 270 | return crecp->name.bname->name; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 271 | else if (crecp->flags & F_NAMEP) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 272 | return crecp->name.namep; |
| 273 | |
| 274 | return crecp->name.sname; |
| 275 | } |
| 276 | |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 277 | char *cache_get_cname_target(struct crec *crecp) |
| 278 | { |
Simon Kelley | 19c51cf | 2014-03-18 22:38:30 +0000 | [diff] [blame] | 279 | if (crecp->addr.cname.uid != SRC_INTERFACE) |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 280 | return cache_get_name(crecp->addr.cname.target.cache); |
| 281 | |
| 282 | return crecp->addr.cname.target.int_name->name; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | |
Simon Kelley | b75e936 | 2012-12-07 11:50:41 +0000 | [diff] [blame] | 287 | struct crec *cache_enumerate(int init) |
| 288 | { |
| 289 | static int bucket; |
| 290 | static struct crec *cache; |
| 291 | |
| 292 | if (init) |
| 293 | { |
| 294 | bucket = 0; |
| 295 | cache = NULL; |
| 296 | } |
| 297 | else if (cache && cache->hash_next) |
| 298 | cache = cache->hash_next; |
| 299 | else |
| 300 | { |
| 301 | cache = NULL; |
| 302 | while (bucket < hash_size) |
| 303 | if ((cache = hash_table[bucket++])) |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | return cache; |
| 308 | } |
| 309 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 310 | static int is_outdated_cname_pointer(struct crec *crecp) |
| 311 | { |
Andy | 3e21a1a | 2014-03-22 19:10:07 +0000 | [diff] [blame] | 312 | if (!(crecp->flags & F_CNAME) || crecp->addr.cname.uid == SRC_INTERFACE) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 313 | return 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 314 | |
Simon Kelley | cdbee9a | 2012-04-04 21:55:59 +0100 | [diff] [blame] | 315 | /* NB. record may be reused as DS or DNSKEY, where uid is |
| 316 | overloaded for something completely different */ |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 317 | if (crecp->addr.cname.target.cache && |
Simon Kelley | 2896e24 | 2019-01-09 15:12:34 +0000 | [diff] [blame] | 318 | !(crecp->addr.cname.target.cache->flags & (F_DNSKEY | F_DS)) && |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 319 | crecp->addr.cname.uid == crecp->addr.cname.target.cache->uid) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 320 | return 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 321 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 322 | return 1; |
| 323 | } |
| 324 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 325 | static int is_expired(time_t now, struct crec *crecp) |
| 326 | { |
| 327 | if (crecp->flags & F_IMMORTAL) |
| 328 | return 0; |
| 329 | |
| 330 | if (difftime(now, crecp->ttd) < 0) |
| 331 | return 0; |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 332 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 333 | return 1; |
| 334 | } |
| 335 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 336 | static struct crec *cache_scan_free(char *name, union all_addr *addr, unsigned short class, time_t now, |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 337 | unsigned int flags, struct crec **target_crec, unsigned int *target_uid) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 338 | { |
| 339 | /* Scan and remove old entries. |
| 340 | If (flags & F_FORWARD) then remove any forward entries for name and any expired |
| 341 | entries but only in the same hash bucket as name. |
| 342 | If (flags & F_REVERSE) then remove any reverse entries for addr and any expired |
| 343 | entries in the whole cache. |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 344 | If (flags == 0) remove any expired entries in the whole cache. |
| 345 | |
Simon Kelley | cbc6524 | 2014-12-21 21:21:53 +0000 | [diff] [blame] | 346 | In the flags & F_FORWARD case, the return code is valid, and returns a non-NULL pointer |
| 347 | to a cache entry if the name exists in the cache as a HOSTS or DHCP entry (these are never deleted) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 348 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 349 | We take advantage of the fact that hash chains have stuff in the order <reverse>,<other>,<immortal> |
Simon Kelley | eb1fe15 | 2018-07-18 20:59:52 +0100 | [diff] [blame] | 350 | so that when we hit an entry which isn't reverse and is immortal, we're done. |
| 351 | |
| 352 | If we free a crec which is a CNAME target, return the entry and uid in target_crec and target_uid. |
| 353 | This entry will get re-used with the same name, to preserve CNAMEs. */ |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 354 | |
| 355 | struct crec *crecp, **up; |
Simon Kelley | 65a01b7 | 2018-12-31 23:56:33 +0000 | [diff] [blame] | 356 | |
| 357 | (void)class; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 358 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 359 | if (flags & F_FORWARD) |
| 360 | { |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 361 | for (up = hash_bucket(name), crecp = *up; crecp; crecp = crecp->hash_next) |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 362 | { |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 363 | if ((crecp->flags & F_FORWARD) && hostname_isequal(cache_get_name(crecp), name)) |
| 364 | { |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 365 | /* Don't delete DNSSEC in favour of a CNAME, they can co-exist */ |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 366 | if ((flags & crecp->flags & (F_IPV4 | F_IPV6 | F_SRV)) || |
Simon Kelley | 6429e42 | 2014-01-23 12:09:36 +0000 | [diff] [blame] | 367 | (((crecp->flags | flags) & F_CNAME) && !(crecp->flags & (F_DNSKEY | F_DS)))) |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 368 | { |
| 369 | if (crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) |
Simon Kelley | cbc6524 | 2014-12-21 21:21:53 +0000 | [diff] [blame] | 370 | return crecp; |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 371 | *up = crecp->hash_next; |
Simon Kelley | eb1fe15 | 2018-07-18 20:59:52 +0100 | [diff] [blame] | 372 | /* If this record is for the name we're inserting and is the target |
| 373 | of a CNAME record. Make the new record for the same name, in the same |
| 374 | crec, with the same uid to avoid breaking the existing CNAME. */ |
| 375 | if (crecp->uid != UID_NONE) |
| 376 | { |
| 377 | if (target_crec) |
| 378 | *target_crec = crecp; |
| 379 | if (target_uid) |
| 380 | *target_uid = crecp->uid; |
| 381 | } |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 382 | cache_unlink(crecp); |
| 383 | cache_free(crecp); |
| 384 | continue; |
| 385 | } |
| 386 | |
| 387 | #ifdef HAVE_DNSSEC |
Simon Kelley | 93be5b1 | 2015-12-15 12:04:40 +0000 | [diff] [blame] | 388 | /* Deletion has to be class-sensitive for DS and DNSKEY */ |
Simon Kelley | 65a01b7 | 2018-12-31 23:56:33 +0000 | [diff] [blame] | 389 | if ((flags & crecp->flags & (F_DNSKEY | F_DS)) && crecp->uid == class) |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 390 | { |
Simon Kelley | 824202e | 2014-01-23 20:59:46 +0000 | [diff] [blame] | 391 | if (crecp->flags & F_CONFIG) |
Simon Kelley | cbc6524 | 2014-12-21 21:21:53 +0000 | [diff] [blame] | 392 | return crecp; |
Simon Kelley | 824202e | 2014-01-23 20:59:46 +0000 | [diff] [blame] | 393 | *up = crecp->hash_next; |
| 394 | cache_unlink(crecp); |
| 395 | cache_free(crecp); |
| 396 | continue; |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 397 | } |
| 398 | #endif |
| 399 | } |
Simon Kelley | eb1fe15 | 2018-07-18 20:59:52 +0100 | [diff] [blame] | 400 | |
| 401 | if (is_expired(now, crecp) || is_outdated_cname_pointer(crecp)) |
| 402 | { |
| 403 | *up = crecp->hash_next; |
| 404 | if (!(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG))) |
| 405 | { |
| 406 | cache_unlink(crecp); |
| 407 | cache_free(crecp); |
| 408 | } |
| 409 | continue; |
| 410 | } |
| 411 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 412 | up = &crecp->hash_next; |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 413 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 414 | } |
| 415 | else |
| 416 | { |
| 417 | int i; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 418 | int addrlen = (flags & F_IPV6) ? IN6ADDRSZ : INADDRSZ; |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 419 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 420 | for (i = 0; i < hash_size; i++) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 421 | for (crecp = hash_table[i], up = &hash_table[i]; |
| 422 | crecp && ((crecp->flags & F_REVERSE) || !(crecp->flags & F_IMMORTAL)); |
| 423 | crecp = crecp->hash_next) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 424 | if (is_expired(now, crecp)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 425 | { |
| 426 | *up = crecp->hash_next; |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 427 | if (!(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 428 | { |
| 429 | cache_unlink(crecp); |
| 430 | cache_free(crecp); |
| 431 | } |
| 432 | } |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 433 | else if (!(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) && |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 434 | (flags & crecp->flags & F_REVERSE) && |
| 435 | (flags & crecp->flags & (F_IPV4 | F_IPV6)) && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 436 | memcmp(&crecp->addr, addr, addrlen) == 0) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 437 | { |
| 438 | *up = crecp->hash_next; |
| 439 | cache_unlink(crecp); |
| 440 | cache_free(crecp); |
| 441 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 442 | else |
| 443 | up = &crecp->hash_next; |
| 444 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 445 | |
Simon Kelley | cbc6524 | 2014-12-21 21:21:53 +0000 | [diff] [blame] | 446 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* Note: The normal calling sequence is |
| 450 | cache_start_insert |
| 451 | cache_insert * n |
| 452 | cache_end_insert |
| 453 | |
| 454 | but an abort can cause the cache_end_insert to be missed |
| 455 | in which can the next cache_start_insert cleans things up. */ |
| 456 | |
| 457 | void cache_start_insert(void) |
| 458 | { |
| 459 | /* Free any entries which didn't get committed during the last |
| 460 | insert due to error. |
| 461 | */ |
| 462 | while (new_chain) |
| 463 | { |
| 464 | struct crec *tmp = new_chain->next; |
| 465 | cache_free(new_chain); |
| 466 | new_chain = tmp; |
| 467 | } |
| 468 | new_chain = NULL; |
| 469 | insert_error = 0; |
| 470 | } |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 471 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 472 | struct crec *cache_insert(char *name, union all_addr *addr, unsigned short class, |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 473 | time_t now, unsigned long ttl, unsigned int flags) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 474 | { |
Simon Kelley | a0ab18f | 2014-02-13 16:38:23 +0000 | [diff] [blame] | 475 | /* Don't log DNSSEC records here, done elsewhere */ |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 476 | if (flags & (F_IPV4 | F_IPV6 | F_CNAME | F_SRV)) |
Simon Kelley | a0ab18f | 2014-02-13 16:38:23 +0000 | [diff] [blame] | 477 | { |
| 478 | log_query(flags | F_UPSTREAM, name, addr, NULL); |
RinSatsuki | 28de387 | 2015-01-10 15:22:21 +0000 | [diff] [blame] | 479 | /* Don't mess with TTL for DNSSEC records. */ |
Simon Kelley | a0ab18f | 2014-02-13 16:38:23 +0000 | [diff] [blame] | 480 | if (daemon->max_cache_ttl != 0 && daemon->max_cache_ttl < ttl) |
| 481 | ttl = daemon->max_cache_ttl; |
RinSatsuki | 28de387 | 2015-01-10 15:22:21 +0000 | [diff] [blame] | 482 | if (daemon->min_cache_ttl != 0 && daemon->min_cache_ttl > ttl) |
| 483 | ttl = daemon->min_cache_ttl; |
Simon Kelley | a0ab18f | 2014-02-13 16:38:23 +0000 | [diff] [blame] | 484 | } |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 485 | |
Simon Kelley | 65a01b7 | 2018-12-31 23:56:33 +0000 | [diff] [blame] | 486 | return really_insert(name, addr, class, now, ttl, flags); |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 487 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 488 | |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 489 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 490 | static struct crec *really_insert(char *name, union all_addr *addr, unsigned short class, |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 491 | time_t now, unsigned long ttl, unsigned int flags) |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 492 | { |
| 493 | struct crec *new, *target_crec = NULL; |
| 494 | union bigname *big_name = NULL; |
| 495 | int freed_all = flags & F_REVERSE; |
| 496 | int free_avail = 0; |
| 497 | unsigned int target_uid; |
| 498 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 499 | /* if previous insertion failed give up now. */ |
| 500 | if (insert_error) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 501 | return NULL; |
Simon Kelley | 1292e1a | 2019-10-25 17:31:53 +0100 | [diff] [blame^] | 502 | |
| 503 | /* we don't cache zero-TTL records. */ |
| 504 | if (ttl == 0) |
| 505 | { |
| 506 | insert_error = 1; |
| 507 | return NULL; |
| 508 | } |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 509 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 510 | /* First remove any expired entries and entries for the name/address we |
Simon Kelley | cbc6524 | 2014-12-21 21:21:53 +0000 | [diff] [blame] | 511 | are currently inserting. */ |
Simon Kelley | 65a01b7 | 2018-12-31 23:56:33 +0000 | [diff] [blame] | 512 | if ((new = cache_scan_free(name, addr, class, now, flags, &target_crec, &target_uid))) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 513 | { |
Simon Kelley | cbc6524 | 2014-12-21 21:21:53 +0000 | [diff] [blame] | 514 | /* We're trying to insert a record over one from |
| 515 | /etc/hosts or DHCP, or other config. If the |
| 516 | existing record is for an A or AAAA and |
| 517 | the record we're trying to insert is the same, |
| 518 | just drop the insert, but don't error the whole process. */ |
Edwin Török | 41a8d9e | 2015-11-14 17:45:48 +0000 | [diff] [blame] | 519 | if ((flags & (F_IPV4 | F_IPV6)) && (flags & F_FORWARD) && addr) |
Simon Kelley | cbc6524 | 2014-12-21 21:21:53 +0000 | [diff] [blame] | 520 | { |
| 521 | if ((flags & F_IPV4) && (new->flags & F_IPV4) && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 522 | new->addr.addr4.s_addr == addr->addr4.s_addr) |
Simon Kelley | cbc6524 | 2014-12-21 21:21:53 +0000 | [diff] [blame] | 523 | return new; |
Simon Kelley | cbc6524 | 2014-12-21 21:21:53 +0000 | [diff] [blame] | 524 | else if ((flags & F_IPV6) && (new->flags & F_IPV6) && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 525 | IN6_ARE_ADDR_EQUAL(&new->addr.addr6, &addr->addr6)) |
Simon Kelley | cbc6524 | 2014-12-21 21:21:53 +0000 | [diff] [blame] | 526 | return new; |
Simon Kelley | cbc6524 | 2014-12-21 21:21:53 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 529 | insert_error = 1; |
| 530 | return NULL; |
| 531 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 532 | |
| 533 | /* Now get a cache entry from the end of the LRU list */ |
Simon Kelley | eb1fe15 | 2018-07-18 20:59:52 +0100 | [diff] [blame] | 534 | if (!target_crec) |
| 535 | while (1) { |
| 536 | if (!(new = cache_tail)) /* no entries left - cache is too small, bail */ |
| 537 | { |
| 538 | insert_error = 1; |
| 539 | return NULL; |
| 540 | } |
| 541 | |
| 542 | /* Free entry at end of LRU list, use it. */ |
| 543 | if (!(new->flags & (F_FORWARD | F_REVERSE))) |
| 544 | break; |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 545 | |
Simon Kelley | eb1fe15 | 2018-07-18 20:59:52 +0100 | [diff] [blame] | 546 | /* End of LRU list is still in use: if we didn't scan all the hash |
| 547 | chains for expired entries do that now. If we already tried that |
| 548 | then it's time to start spilling things. */ |
| 549 | |
| 550 | /* If free_avail set, we believe that an entry has been freed. |
| 551 | Bugs have been known to make this not true, resulting in |
| 552 | a tight loop here. If that happens, abandon the |
| 553 | insert. Once in this state, all inserts will probably fail. */ |
| 554 | if (free_avail) |
| 555 | { |
| 556 | static int warned = 0; |
| 557 | if (!warned) |
| 558 | { |
| 559 | my_syslog(LOG_ERR, _("Internal error in cache.")); |
| 560 | warned = 1; |
| 561 | } |
| 562 | insert_error = 1; |
| 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | if (freed_all) |
| 567 | { |
Simon Kelley | 65a01b7 | 2018-12-31 23:56:33 +0000 | [diff] [blame] | 568 | /* For DNSSEC records, uid holds class. */ |
Simon Kelley | eb1fe15 | 2018-07-18 20:59:52 +0100 | [diff] [blame] | 569 | free_avail = 1; /* Must be free space now. */ |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 570 | cache_scan_free(cache_get_name(new), &new->addr, new->uid, now, new->flags, NULL, NULL); |
Julian Kornberger | aba8bbb | 2018-07-21 21:55:08 +0100 | [diff] [blame] | 571 | daemon->metrics[METRIC_DNS_CACHE_LIVE_FREED]++; |
Simon Kelley | eb1fe15 | 2018-07-18 20:59:52 +0100 | [diff] [blame] | 572 | } |
| 573 | else |
| 574 | { |
Simon Kelley | 65a01b7 | 2018-12-31 23:56:33 +0000 | [diff] [blame] | 575 | cache_scan_free(NULL, NULL, class, now, 0, NULL, NULL); |
Simon Kelley | eb1fe15 | 2018-07-18 20:59:52 +0100 | [diff] [blame] | 576 | freed_all = 1; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /* Check if we need to and can allocate extra memory for a long name. |
| 581 | If that fails, give up now, always succeed for DNSSEC records. */ |
| 582 | if (name && (strlen(name) > SMALLDNAME-1)) |
| 583 | { |
| 584 | if (big_free) |
| 585 | { |
| 586 | big_name = big_free; |
| 587 | big_free = big_free->next; |
| 588 | } |
| 589 | else if ((bignames_left == 0 && !(flags & (F_DS | F_DNSKEY))) || |
| 590 | !(big_name = (union bigname *)whine_malloc(sizeof(union bigname)))) |
| 591 | { |
| 592 | insert_error = 1; |
| 593 | return NULL; |
| 594 | } |
| 595 | else if (bignames_left != 0) |
| 596 | bignames_left--; |
| 597 | |
| 598 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 599 | |
Simon Kelley | eb1fe15 | 2018-07-18 20:59:52 +0100 | [diff] [blame] | 600 | /* If we freed a cache entry for our name which was a CNAME target, use that. |
| 601 | and preserve the uid, so that existing CNAMES are not broken. */ |
| 602 | if (target_crec) |
| 603 | { |
| 604 | new = target_crec; |
| 605 | new->uid = target_uid; |
| 606 | } |
| 607 | |
| 608 | /* Got the rest: finally grab entry. */ |
| 609 | cache_unlink(new); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 610 | |
| 611 | new->flags = flags; |
| 612 | if (big_name) |
| 613 | { |
| 614 | new->name.bname = big_name; |
| 615 | new->flags |= F_BIGNAME; |
| 616 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 617 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 618 | if (name) |
| 619 | strcpy(cache_get_name(new), name); |
| 620 | else |
| 621 | *cache_get_name(new) = 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 622 | |
Simon Kelley | b8eac19 | 2014-02-27 14:30:03 +0000 | [diff] [blame] | 623 | #ifdef HAVE_DNSSEC |
Simon Kelley | 65a01b7 | 2018-12-31 23:56:33 +0000 | [diff] [blame] | 624 | if (flags & (F_DS | F_DNSKEY)) |
| 625 | new->uid = class; |
Simon Kelley | b8eac19 | 2014-02-27 14:30:03 +0000 | [diff] [blame] | 626 | #endif |
Simon Kelley | 65a01b7 | 2018-12-31 23:56:33 +0000 | [diff] [blame] | 627 | |
| 628 | if (addr) |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 629 | new->addr = *addr; |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 630 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 631 | new->ttd = now + (time_t)ttl; |
| 632 | new->next = new_chain; |
| 633 | new_chain = new; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 634 | |
| 635 | return new; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* after end of insertion, commit the new entries */ |
| 639 | void cache_end_insert(void) |
| 640 | { |
| 641 | if (insert_error) |
| 642 | return; |
| 643 | |
| 644 | while (new_chain) |
| 645 | { |
| 646 | struct crec *tmp = new_chain->next; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 647 | /* drop CNAMEs which didn't find a target. */ |
| 648 | if (is_outdated_cname_pointer(new_chain)) |
| 649 | cache_free(new_chain); |
| 650 | else |
| 651 | { |
| 652 | cache_hash(new_chain); |
| 653 | cache_link(new_chain); |
Julian Kornberger | aba8bbb | 2018-07-21 21:55:08 +0100 | [diff] [blame] | 654 | daemon->metrics[METRIC_DNS_CACHE_INSERTED]++; |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 655 | |
| 656 | /* If we're a child process, send this cache entry up the pipe to the master. |
| 657 | The marshalling process is rather nasty. */ |
| 658 | if (daemon->pipe_to_parent != -1) |
| 659 | { |
| 660 | char *name = cache_get_name(new_chain); |
| 661 | ssize_t m = strlen(name); |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 662 | unsigned int flags = new_chain->flags; |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 663 | #ifdef HAVE_DNSSEC |
| 664 | u16 class = new_chain->uid; |
| 665 | #endif |
| 666 | |
| 667 | read_write(daemon->pipe_to_parent, (unsigned char *)&m, sizeof(m), 0); |
| 668 | read_write(daemon->pipe_to_parent, (unsigned char *)name, m, 0); |
| 669 | read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->ttd, sizeof(new_chain->ttd), 0); |
| 670 | read_write(daemon->pipe_to_parent, (unsigned char *)&flags, sizeof(flags), 0); |
| 671 | |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 672 | if (flags & (F_IPV4 | F_IPV6 | F_DNSKEY | F_DS | F_SRV)) |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 673 | read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr, sizeof(new_chain->addr), 0); |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 674 | if (flags & F_SRV) |
Alin Nastac | e710c34 | 2019-09-30 15:30:26 +0100 | [diff] [blame] | 675 | { |
| 676 | /* A negative SRV entry is possible and has no data, obviously. */ |
| 677 | if (!(flags & F_NEG)) |
| 678 | blockdata_write(new_chain->addr.srv.target, new_chain->addr.srv.targetlen, daemon->pipe_to_parent); |
| 679 | } |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 680 | #ifdef HAVE_DNSSEC |
Simon Kelley | ab194ed | 2019-01-01 01:35:30 +0000 | [diff] [blame] | 681 | if (flags & F_DNSKEY) |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 682 | { |
| 683 | read_write(daemon->pipe_to_parent, (unsigned char *)&class, sizeof(class), 0); |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 684 | blockdata_write(new_chain->addr.key.keydata, new_chain->addr.key.keylen, daemon->pipe_to_parent); |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 685 | } |
| 686 | else if (flags & F_DS) |
| 687 | { |
| 688 | read_write(daemon->pipe_to_parent, (unsigned char *)&class, sizeof(class), 0); |
| 689 | /* A negative DS entry is possible and has no data, obviously. */ |
| 690 | if (!(flags & F_NEG)) |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 691 | blockdata_write(new_chain->addr.ds.keydata, new_chain->addr.ds.keylen, daemon->pipe_to_parent); |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 692 | } |
| 693 | #endif |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 694 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 695 | } |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 696 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 697 | new_chain = tmp; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 698 | } |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 699 | |
| 700 | /* signal end of cache insert in master process */ |
| 701 | if (daemon->pipe_to_parent != -1) |
| 702 | { |
| 703 | ssize_t m = -1; |
| 704 | read_write(daemon->pipe_to_parent, (unsigned char *)&m, sizeof(m), 0); |
| 705 | } |
| 706 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 707 | new_chain = NULL; |
| 708 | } |
| 709 | |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 710 | |
| 711 | /* A marshalled cache entry arrives on fd, read, unmarshall and insert into cache of master process. */ |
| 712 | int cache_recv_insert(time_t now, int fd) |
| 713 | { |
| 714 | ssize_t m; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 715 | union all_addr addr; |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 716 | unsigned long ttl; |
| 717 | time_t ttd; |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 718 | unsigned int flags; |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 719 | struct crec *crecp = NULL; |
| 720 | |
| 721 | cache_start_insert(); |
| 722 | |
| 723 | while(1) |
| 724 | { |
| 725 | |
| 726 | if (!read_write(fd, (unsigned char *)&m, sizeof(m), 1)) |
| 727 | return 0; |
| 728 | |
| 729 | if (m == -1) |
| 730 | { |
| 731 | cache_end_insert(); |
| 732 | return 1; |
| 733 | } |
| 734 | |
| 735 | if (!read_write(fd, (unsigned char *)daemon->namebuff, m, 1) || |
| 736 | !read_write(fd, (unsigned char *)&ttd, sizeof(ttd), 1) || |
| 737 | !read_write(fd, (unsigned char *)&flags, sizeof(flags), 1)) |
| 738 | return 0; |
| 739 | |
| 740 | daemon->namebuff[m] = 0; |
| 741 | |
| 742 | ttl = difftime(ttd, now); |
| 743 | |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 744 | if (flags & (F_IPV4 | F_IPV6 | F_DNSKEY | F_DS | F_SRV)) |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 745 | { |
Simon Kelley | ab194ed | 2019-01-01 01:35:30 +0000 | [diff] [blame] | 746 | unsigned short class = C_IN; |
| 747 | |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 748 | if (!read_write(fd, (unsigned char *)&addr, sizeof(addr), 1)) |
| 749 | return 0; |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 750 | |
Alin Nastac | e710c34 | 2019-09-30 15:30:26 +0100 | [diff] [blame] | 751 | if ((flags & F_SRV) && !(flags & F_NEG) && !(addr.srv.target = blockdata_read(fd, addr.srv.targetlen))) |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 752 | return 0; |
| 753 | |
Simon Kelley | ab194ed | 2019-01-01 01:35:30 +0000 | [diff] [blame] | 754 | #ifdef HAVE_DNSSEC |
| 755 | if (flags & F_DNSKEY) |
| 756 | { |
| 757 | if (!read_write(fd, (unsigned char *)&class, sizeof(class), 1) || |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 758 | !(addr.key.keydata = blockdata_read(fd, addr.key.keylen))) |
Simon Kelley | ab194ed | 2019-01-01 01:35:30 +0000 | [diff] [blame] | 759 | return 0; |
| 760 | } |
| 761 | else if (flags & F_DS) |
| 762 | { |
| 763 | if (!read_write(fd, (unsigned char *)&class, sizeof(class), 1) || |
Simon Kelley | 2c59473 | 2019-01-03 13:42:03 +0000 | [diff] [blame] | 764 | (!(flags & F_NEG) && !(addr.key.keydata = blockdata_read(fd, addr.key.keylen)))) |
Simon Kelley | ab194ed | 2019-01-01 01:35:30 +0000 | [diff] [blame] | 765 | return 0; |
| 766 | } |
| 767 | #endif |
| 768 | |
| 769 | crecp = really_insert(daemon->namebuff, &addr, class, now, ttl, flags); |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 770 | } |
| 771 | else if (flags & F_CNAME) |
| 772 | { |
Simon Kelley | 65a01b7 | 2018-12-31 23:56:33 +0000 | [diff] [blame] | 773 | struct crec *newc = really_insert(daemon->namebuff, NULL, C_IN, now, ttl, flags); |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 774 | /* This relies on the fact the the target of a CNAME immediately preceeds |
| 775 | it because of the order of extraction in extract_addresses, and |
| 776 | the order reversal on the new_chain. */ |
| 777 | if (newc) |
| 778 | { |
| 779 | if (!crecp) |
| 780 | { |
| 781 | newc->addr.cname.target.cache = NULL; |
| 782 | /* anything other than zero, to avoid being mistaken for CNAME to interface-name */ |
| 783 | newc->addr.cname.uid = 1; |
| 784 | } |
| 785 | else |
| 786 | { |
| 787 | next_uid(crecp); |
| 788 | newc->addr.cname.target.cache = crecp; |
| 789 | newc->addr.cname.uid = crecp->uid; |
| 790 | } |
| 791 | } |
| 792 | } |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 796 | int cache_find_non_terminal(char *name, time_t now) |
| 797 | { |
| 798 | struct crec *crecp; |
| 799 | |
| 800 | for (crecp = *hash_bucket(name); crecp; crecp = crecp->hash_next) |
| 801 | if (!is_outdated_cname_pointer(crecp) && |
| 802 | !is_expired(now, crecp) && |
| 803 | (crecp->flags & F_FORWARD) && |
Sven Mueller | 162e5e0 | 2019-02-27 21:17:37 +0000 | [diff] [blame] | 804 | !(crecp->flags & F_NXDOMAIN) && |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 805 | hostname_isequal(name, cache_get_name(crecp))) |
| 806 | return 1; |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
Simon Kelley | 12fae49 | 2014-02-04 22:03:06 +0000 | [diff] [blame] | 811 | struct crec *cache_find_by_name(struct crec *crecp, char *name, time_t now, unsigned int prot) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 812 | { |
| 813 | struct crec *ans; |
Simon Kelley | 12fae49 | 2014-02-04 22:03:06 +0000 | [diff] [blame] | 814 | int no_rr = prot & F_NO_RR; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 815 | |
Simon Kelley | 12fae49 | 2014-02-04 22:03:06 +0000 | [diff] [blame] | 816 | prot &= ~F_NO_RR; |
| 817 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 818 | if (crecp) /* iterating */ |
| 819 | ans = crecp->next; |
| 820 | else |
| 821 | { |
| 822 | /* first search, look for relevant entries and push to top of list |
| 823 | also free anything which has expired */ |
| 824 | struct crec *next, **up, **insert = NULL, **chainp = &ans; |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 825 | unsigned int ins_flags = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 826 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 827 | for (up = hash_bucket(name), crecp = *up; crecp; crecp = next) |
| 828 | { |
| 829 | next = crecp->hash_next; |
| 830 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 831 | if (!is_expired(now, crecp) && !is_outdated_cname_pointer(crecp)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 832 | { |
| 833 | if ((crecp->flags & F_FORWARD) && |
| 834 | (crecp->flags & prot) && |
| 835 | hostname_isequal(cache_get_name(crecp), name)) |
| 836 | { |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 837 | if (crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 838 | { |
| 839 | *chainp = crecp; |
| 840 | chainp = &crecp->next; |
| 841 | } |
| 842 | else |
| 843 | { |
| 844 | cache_unlink(crecp); |
| 845 | cache_link(crecp); |
| 846 | } |
| 847 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 848 | /* Move all but the first entry up the hash chain |
| 849 | this implements round-robin. |
| 850 | Make sure that re-ordering doesn't break the hash-chain |
| 851 | order invariants. |
| 852 | */ |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 853 | if (insert && (crecp->flags & (F_REVERSE | F_IMMORTAL)) == ins_flags) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 854 | { |
| 855 | *up = crecp->hash_next; |
| 856 | crecp->hash_next = *insert; |
| 857 | *insert = crecp; |
| 858 | insert = &crecp->hash_next; |
| 859 | } |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 860 | else |
| 861 | { |
Simon Kelley | 12fae49 | 2014-02-04 22:03:06 +0000 | [diff] [blame] | 862 | if (!insert && !no_rr) |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 863 | { |
| 864 | insert = up; |
| 865 | ins_flags = crecp->flags & (F_REVERSE | F_IMMORTAL); |
| 866 | } |
| 867 | up = &crecp->hash_next; |
| 868 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 869 | } |
| 870 | else |
| 871 | /* case : not expired, incorrect entry. */ |
| 872 | up = &crecp->hash_next; |
| 873 | } |
| 874 | else |
| 875 | { |
| 876 | /* expired entry, free it */ |
| 877 | *up = crecp->hash_next; |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 878 | if (!(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 879 | { |
| 880 | cache_unlink(crecp); |
| 881 | cache_free(crecp); |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | *chainp = cache_head; |
| 887 | } |
| 888 | |
| 889 | if (ans && |
| 890 | (ans->flags & F_FORWARD) && |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 891 | (ans->flags & prot) && |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 892 | hostname_isequal(cache_get_name(ans), name)) |
| 893 | return ans; |
| 894 | |
| 895 | return NULL; |
| 896 | } |
| 897 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 898 | struct crec *cache_find_by_addr(struct crec *crecp, union all_addr *addr, |
Simon Kelley | 12fae49 | 2014-02-04 22:03:06 +0000 | [diff] [blame] | 899 | time_t now, unsigned int prot) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 900 | { |
| 901 | struct crec *ans; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 902 | int addrlen = (prot == F_IPV6) ? IN6ADDRSZ : INADDRSZ; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 903 | |
| 904 | if (crecp) /* iterating */ |
| 905 | ans = crecp->next; |
| 906 | else |
| 907 | { |
| 908 | /* first search, look for relevant entries and push to top of list |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 909 | also free anything which has expired. All the reverse entries are at the |
| 910 | start of the hash chain, so we can give up when we find the first |
| 911 | non-REVERSE one. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 912 | int i; |
| 913 | struct crec **up, **chainp = &ans; |
| 914 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 915 | for (i=0; i<hash_size; i++) |
| 916 | for (crecp = hash_table[i], up = &hash_table[i]; |
| 917 | crecp && (crecp->flags & F_REVERSE); |
| 918 | crecp = crecp->hash_next) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 919 | if (!is_expired(now, crecp)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 920 | { |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 921 | if ((crecp->flags & prot) && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 922 | memcmp(&crecp->addr, addr, addrlen) == 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 923 | { |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 924 | if (crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 925 | { |
| 926 | *chainp = crecp; |
| 927 | chainp = &crecp->next; |
| 928 | } |
| 929 | else |
| 930 | { |
| 931 | cache_unlink(crecp); |
| 932 | cache_link(crecp); |
| 933 | } |
| 934 | } |
| 935 | up = &crecp->hash_next; |
| 936 | } |
| 937 | else |
| 938 | { |
| 939 | *up = crecp->hash_next; |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 940 | if (!(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 941 | { |
| 942 | cache_unlink(crecp); |
| 943 | cache_free(crecp); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | *chainp = cache_head; |
| 948 | } |
| 949 | |
| 950 | if (ans && |
| 951 | (ans->flags & F_REVERSE) && |
| 952 | (ans->flags & prot) && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 953 | memcmp(&ans->addr, addr, addrlen) == 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 954 | return ans; |
| 955 | |
| 956 | return NULL; |
| 957 | } |
| 958 | |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 959 | static void add_hosts_cname(struct crec *target) |
| 960 | { |
| 961 | struct crec *crec; |
| 962 | struct cname *a; |
| 963 | |
| 964 | for (a = daemon->cnames; a; a = a->next) |
Simon Kelley | b637d78 | 2016-12-13 16:44:11 +0000 | [diff] [blame] | 965 | if (a->alias[1] != '*' && |
| 966 | hostname_isequal(cache_get_name(target), a->target) && |
Simon Kelley | 3a610a0 | 2018-09-26 16:50:35 +0100 | [diff] [blame] | 967 | (crec = whine_malloc(SIZEOF_POINTER_CREC))) |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 968 | { |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 969 | crec->flags = F_FORWARD | F_IMMORTAL | F_NAMEP | F_CONFIG | F_CNAME; |
Simon Kelley | df3d54f | 2016-02-24 21:03:38 +0000 | [diff] [blame] | 970 | crec->ttd = a->ttl; |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 971 | crec->name.namep = a->alias; |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 972 | crec->addr.cname.target.cache = target; |
Simon Kelley | 45d8a24 | 2018-07-17 21:01:14 +0100 | [diff] [blame] | 973 | next_uid(target); |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 974 | crec->addr.cname.uid = target->uid; |
Simon Kelley | 45d8a24 | 2018-07-17 21:01:14 +0100 | [diff] [blame] | 975 | crec->uid = UID_NONE; |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 976 | cache_hash(crec); |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 977 | make_non_terminals(crec); |
| 978 | |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 979 | add_hosts_cname(crec); /* handle chains */ |
| 980 | } |
| 981 | } |
| 982 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 983 | static void add_hosts_entry(struct crec *cache, union all_addr *addr, int addrlen, |
Simon Kelley | 19c51cf | 2014-03-18 22:38:30 +0000 | [diff] [blame] | 984 | unsigned int index, struct crec **rhash, int hashsz) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 985 | { |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 986 | struct crec *lookup = cache_find_by_name(NULL, cache_get_name(cache), 0, cache->flags & (F_IPV4 | F_IPV6)); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 987 | int i, nameexists = 0; |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 988 | unsigned int j; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 989 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 990 | /* Remove duplicates in hosts files. */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 991 | if (lookup && (lookup->flags & F_HOSTS)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 992 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 993 | nameexists = 1; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 994 | if (memcmp(&lookup->addr, addr, addrlen) == 0) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 995 | { |
| 996 | free(cache); |
| 997 | return; |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | /* Ensure there is only one address -> name mapping (first one trumps) |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 1002 | We do this by steam here, The entries are kept in hash chains, linked |
| 1003 | by ->next (which is unused at this point) held in hash buckets in |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 1004 | the array rhash, hashed on address. Note that rhash and the values |
| 1005 | in ->next are only valid whilst reading hosts files: the buckets are |
| 1006 | then freed, and the ->next pointer used for other things. |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 1007 | |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 1008 | Only insert each unique address once into this hashing structure. |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 1009 | |
| 1010 | This complexity avoids O(n^2) divergent CPU use whilst reading |
Simon Kelley | 70d1873 | 2015-01-31 19:59:29 +0000 | [diff] [blame] | 1011 | large (10000 entry) hosts files. |
| 1012 | |
| 1013 | Note that we only do this process when bulk-reading hosts files, |
| 1014 | for incremental reads, rhash is NULL, and we use cache lookups |
| 1015 | instead. |
| 1016 | */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1017 | |
Simon Kelley | 70d1873 | 2015-01-31 19:59:29 +0000 | [diff] [blame] | 1018 | if (rhash) |
Simon Kelley | 915363f | 2012-01-11 22:00:48 +0000 | [diff] [blame] | 1019 | { |
Simon Kelley | 70d1873 | 2015-01-31 19:59:29 +0000 | [diff] [blame] | 1020 | /* hash address */ |
| 1021 | for (j = 0, i = 0; i < addrlen; i++) |
| 1022 | j = (j*2 +((unsigned char *)addr)[i]) % hashsz; |
| 1023 | |
| 1024 | for (lookup = rhash[j]; lookup; lookup = lookup->next) |
| 1025 | if ((lookup->flags & cache->flags & (F_IPV4 | F_IPV6)) && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1026 | memcmp(&lookup->addr, addr, addrlen) == 0) |
Simon Kelley | 70d1873 | 2015-01-31 19:59:29 +0000 | [diff] [blame] | 1027 | { |
| 1028 | cache->flags &= ~F_REVERSE; |
| 1029 | break; |
| 1030 | } |
| 1031 | |
| 1032 | /* maintain address hash chain, insert new unique address */ |
| 1033 | if (!lookup) |
| 1034 | { |
| 1035 | cache->next = rhash[j]; |
| 1036 | rhash[j] = cache; |
| 1037 | } |
Simon Kelley | 915363f | 2012-01-11 22:00:48 +0000 | [diff] [blame] | 1038 | } |
Simon Kelley | 70d1873 | 2015-01-31 19:59:29 +0000 | [diff] [blame] | 1039 | else |
| 1040 | { |
| 1041 | /* incremental read, lookup in cache */ |
| 1042 | lookup = cache_find_by_addr(NULL, addr, 0, cache->flags & (F_IPV4 | F_IPV6)); |
| 1043 | if (lookup && lookup->flags & F_HOSTS) |
| 1044 | cache->flags &= ~F_REVERSE; |
| 1045 | } |
| 1046 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1047 | cache->uid = index; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1048 | memcpy(&cache->addr, addr, addrlen); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1049 | cache_hash(cache); |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 1050 | make_non_terminals(cache); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1051 | |
| 1052 | /* don't need to do alias stuff for second and subsequent addresses. */ |
| 1053 | if (!nameexists) |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1054 | add_hosts_cname(cache); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | static int eatspace(FILE *f) |
| 1058 | { |
| 1059 | int c, nl = 0; |
| 1060 | |
| 1061 | while (1) |
| 1062 | { |
| 1063 | if ((c = getc(f)) == '#') |
| 1064 | while (c != '\n' && c != EOF) |
| 1065 | c = getc(f); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1066 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1067 | if (c == EOF) |
| 1068 | return 1; |
| 1069 | |
| 1070 | if (!isspace(c)) |
| 1071 | { |
| 1072 | ungetc(c, f); |
| 1073 | return nl; |
| 1074 | } |
| 1075 | |
| 1076 | if (c == '\n') |
Simon Kelley | 4219ade | 2019-02-27 20:30:21 +0000 | [diff] [blame] | 1077 | nl++; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | static int gettok(FILE *f, char *token) |
| 1082 | { |
| 1083 | int c, count = 0; |
| 1084 | |
| 1085 | while (1) |
| 1086 | { |
| 1087 | if ((c = getc(f)) == EOF) |
Simon Kelley | 4219ade | 2019-02-27 20:30:21 +0000 | [diff] [blame] | 1088 | return (count == 0) ? -1 : 1; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1089 | |
| 1090 | if (isspace(c) || c == '#') |
| 1091 | { |
| 1092 | ungetc(c, f); |
| 1093 | return eatspace(f); |
| 1094 | } |
| 1095 | |
| 1096 | if (count < (MAXDNAME - 1)) |
| 1097 | { |
| 1098 | token[count++] = c; |
| 1099 | token[count] = 0; |
| 1100 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
Simon Kelley | 70d1873 | 2015-01-31 19:59:29 +0000 | [diff] [blame] | 1104 | int read_hostsfile(char *filename, unsigned int index, int cache_size, struct crec **rhash, int hashsz) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1105 | { |
| 1106 | FILE *f = fopen(filename, "r"); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1107 | char *token = daemon->namebuff, *domain_suffix = NULL; |
Simon Kelley | 4219ade | 2019-02-27 20:30:21 +0000 | [diff] [blame] | 1108 | int addr_count = 0, name_count = cache_size, lineno = 1; |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 1109 | unsigned int flags = 0; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1110 | union all_addr addr; |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 1111 | int atnl, addrlen = 0; |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 1112 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1113 | if (!f) |
| 1114 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1115 | my_syslog(LOG_ERR, _("failed to load names from %s: %s"), filename, strerror(errno)); |
André Glüpker | eddf365 | 2016-01-12 12:54:17 +0000 | [diff] [blame] | 1116 | return cache_size; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1117 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1118 | |
Simon Kelley | 4219ade | 2019-02-27 20:30:21 +0000 | [diff] [blame] | 1119 | lineno += eatspace(f); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1120 | |
Simon Kelley | 4219ade | 2019-02-27 20:30:21 +0000 | [diff] [blame] | 1121 | while ((atnl = gettok(f, token)) != -1) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1122 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1123 | if (inet_pton(AF_INET, token, &addr) > 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1124 | { |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 1125 | flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV4; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1126 | addrlen = INADDRSZ; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1127 | domain_suffix = get_domain(addr.addr4); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1128 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1129 | else if (inet_pton(AF_INET6, token, &addr) > 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1130 | { |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 1131 | flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV6; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1132 | addrlen = IN6ADDRSZ; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1133 | domain_suffix = get_domain6(&addr.addr6); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1134 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1135 | else |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1136 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1137 | my_syslog(LOG_ERR, _("bad address at %s line %d"), filename, lineno); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1138 | while (atnl == 0) |
| 1139 | atnl = gettok(f, token); |
Simon Kelley | 065e5bb | 2019-03-01 14:38:51 +0000 | [diff] [blame] | 1140 | lineno += atnl; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1141 | continue; |
| 1142 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1143 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1144 | addr_count++; |
| 1145 | |
| 1146 | /* rehash every 1000 names. */ |
Simon Kelley | 70d1873 | 2015-01-31 19:59:29 +0000 | [diff] [blame] | 1147 | if (rhash && ((name_count - cache_size) > 1000)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1148 | { |
| 1149 | rehash(name_count); |
| 1150 | cache_size = name_count; |
| 1151 | } |
| 1152 | |
| 1153 | while (atnl == 0) |
| 1154 | { |
| 1155 | struct crec *cache; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1156 | int fqdn, nomem; |
| 1157 | char *canon; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1158 | |
Simon Kelley | 4219ade | 2019-02-27 20:30:21 +0000 | [diff] [blame] | 1159 | if ((atnl = gettok(f, token)) == -1) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1160 | break; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1161 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1162 | fqdn = !!strchr(token, '.'); |
| 1163 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1164 | if ((canon = canonicalise(token, &nomem))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1165 | { |
| 1166 | /* If set, add a version of the name with a default domain appended */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1167 | if (option_bool(OPT_EXPAND) && domain_suffix && !fqdn && |
Simon Kelley | 3a610a0 | 2018-09-26 16:50:35 +0100 | [diff] [blame] | 1168 | (cache = whine_malloc(SIZEOF_BARE_CREC + strlen(canon) + 2 + strlen(domain_suffix)))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1169 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1170 | strcpy(cache->name.sname, canon); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1171 | strcat(cache->name.sname, "."); |
| 1172 | strcat(cache->name.sname, domain_suffix); |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1173 | cache->flags = flags; |
Simon Kelley | df3d54f | 2016-02-24 21:03:38 +0000 | [diff] [blame] | 1174 | cache->ttd = daemon->local_ttl; |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1175 | add_hosts_entry(cache, &addr, addrlen, index, rhash, hashsz); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1176 | name_count++; |
| 1177 | } |
Simon Kelley | 3a610a0 | 2018-09-26 16:50:35 +0100 | [diff] [blame] | 1178 | if ((cache = whine_malloc(SIZEOF_BARE_CREC + strlen(canon) + 1))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1179 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1180 | strcpy(cache->name.sname, canon); |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1181 | cache->flags = flags; |
Simon Kelley | df3d54f | 2016-02-24 21:03:38 +0000 | [diff] [blame] | 1182 | cache->ttd = daemon->local_ttl; |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1183 | add_hosts_entry(cache, &addr, addrlen, index, rhash, hashsz); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1184 | name_count++; |
| 1185 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1186 | free(canon); |
| 1187 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1188 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1189 | else if (!nomem) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1190 | my_syslog(LOG_ERR, _("bad name at %s line %d"), filename, lineno); |
| 1191 | } |
Simon Kelley | 4219ade | 2019-02-27 20:30:21 +0000 | [diff] [blame] | 1192 | |
| 1193 | lineno += atnl; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1196 | fclose(f); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1197 | |
Simon Kelley | 70d1873 | 2015-01-31 19:59:29 +0000 | [diff] [blame] | 1198 | if (rhash) |
Simon Kelley | f9c8637 | 2015-02-03 21:52:48 +0000 | [diff] [blame] | 1199 | rehash(name_count); |
| 1200 | |
| 1201 | my_syslog(LOG_INFO, _("read %s - %d addresses"), filename, addr_count); |
| 1202 | |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 1203 | return name_count; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1206 | void cache_reload(void) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1207 | { |
| 1208 | struct crec *cache, **up, *tmp; |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 1209 | int revhashsz, i, total_size = daemon->cachesize; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1210 | struct hostsfile *ah; |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1211 | struct host_record *hr; |
| 1212 | struct name_list *nl; |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1213 | struct cname *a; |
| 1214 | struct interface_name *intr; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1215 | #ifdef HAVE_DNSSEC |
Simon Kelley | ee41586 | 2014-02-11 11:07:22 +0000 | [diff] [blame] | 1216 | struct ds_config *ds; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1217 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1218 | |
Julian Kornberger | aba8bbb | 2018-07-21 21:55:08 +0100 | [diff] [blame] | 1219 | daemon->metrics[METRIC_DNS_CACHE_INSERTED] = 0; |
| 1220 | daemon->metrics[METRIC_DNS_CACHE_LIVE_FREED] = 0; |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 1221 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1222 | for (i=0; i<hash_size; i++) |
| 1223 | for (cache = hash_table[i], up = &hash_table[i]; cache; cache = tmp) |
| 1224 | { |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 1225 | cache_blockdata_free(cache); |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 1226 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1227 | tmp = cache->hash_next; |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 1228 | if (cache->flags & (F_HOSTS | F_CONFIG)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1229 | { |
| 1230 | *up = cache->hash_next; |
| 1231 | free(cache); |
| 1232 | } |
| 1233 | else if (!(cache->flags & F_DHCP)) |
| 1234 | { |
| 1235 | *up = cache->hash_next; |
| 1236 | if (cache->flags & F_BIGNAME) |
| 1237 | { |
| 1238 | cache->name.bname->next = big_free; |
| 1239 | big_free = cache->name.bname; |
| 1240 | } |
| 1241 | cache->flags = 0; |
| 1242 | } |
| 1243 | else |
| 1244 | up = &cache->hash_next; |
| 1245 | } |
| 1246 | |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1247 | /* Add CNAMEs to interface_names to the cache */ |
| 1248 | for (a = daemon->cnames; a; a = a->next) |
| 1249 | for (intr = daemon->int_names; intr; intr = intr->next) |
Simon Kelley | b637d78 | 2016-12-13 16:44:11 +0000 | [diff] [blame] | 1250 | if (a->alias[1] != '*' && |
| 1251 | hostname_isequal(a->target, intr->name) && |
Simon Kelley | 3a610a0 | 2018-09-26 16:50:35 +0100 | [diff] [blame] | 1252 | ((cache = whine_malloc(SIZEOF_POINTER_CREC)))) |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1253 | { |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 1254 | cache->flags = F_FORWARD | F_NAMEP | F_CNAME | F_IMMORTAL | F_CONFIG; |
Simon Kelley | df3d54f | 2016-02-24 21:03:38 +0000 | [diff] [blame] | 1255 | cache->ttd = a->ttl; |
Simon Kelley | 532066e | 2013-11-26 10:14:47 +0000 | [diff] [blame] | 1256 | cache->name.namep = a->alias; |
| 1257 | cache->addr.cname.target.int_name = intr; |
Simon Kelley | 19c51cf | 2014-03-18 22:38:30 +0000 | [diff] [blame] | 1258 | cache->addr.cname.uid = SRC_INTERFACE; |
Simon Kelley | 45d8a24 | 2018-07-17 21:01:14 +0100 | [diff] [blame] | 1259 | cache->uid = UID_NONE; |
Simon Kelley | 532066e | 2013-11-26 10:14:47 +0000 | [diff] [blame] | 1260 | cache_hash(cache); |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 1261 | make_non_terminals(cache); |
Simon Kelley | 532066e | 2013-11-26 10:14:47 +0000 | [diff] [blame] | 1262 | add_hosts_cname(cache); /* handle chains */ |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1263 | } |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1264 | |
| 1265 | #ifdef HAVE_DNSSEC |
Simon Kelley | ee41586 | 2014-02-11 11:07:22 +0000 | [diff] [blame] | 1266 | for (ds = daemon->ds; ds; ds = ds->next) |
Simon Kelley | 3a610a0 | 2018-09-26 16:50:35 +0100 | [diff] [blame] | 1267 | if ((cache = whine_malloc(SIZEOF_POINTER_CREC)) && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1268 | (cache->addr.ds.keydata = blockdata_alloc(ds->digest, ds->digestlen))) |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1269 | { |
Simon Kelley | ee41586 | 2014-02-11 11:07:22 +0000 | [diff] [blame] | 1270 | cache->flags = F_FORWARD | F_IMMORTAL | F_DS | F_CONFIG | F_NAMEP; |
Simon Kelley | df3d54f | 2016-02-24 21:03:38 +0000 | [diff] [blame] | 1271 | cache->ttd = daemon->local_ttl; |
Simon Kelley | ee41586 | 2014-02-11 11:07:22 +0000 | [diff] [blame] | 1272 | cache->name.namep = ds->name; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1273 | cache->addr.ds.keylen = ds->digestlen; |
| 1274 | cache->addr.ds.algo = ds->algo; |
| 1275 | cache->addr.ds.keytag = ds->keytag; |
| 1276 | cache->addr.ds.digest = ds->digest_type; |
Simon Kelley | ee41586 | 2014-02-11 11:07:22 +0000 | [diff] [blame] | 1277 | cache->uid = ds->class; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1278 | cache_hash(cache); |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 1279 | make_non_terminals(cache); |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1280 | } |
| 1281 | #endif |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1282 | |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1283 | /* borrow the packet buffer for a temporary by-address hash */ |
| 1284 | memset(daemon->packet, 0, daemon->packet_buff_sz); |
| 1285 | revhashsz = daemon->packet_buff_sz / sizeof(struct crec *); |
| 1286 | /* we overwrote the buffer... */ |
| 1287 | daemon->srv_save = NULL; |
| 1288 | |
| 1289 | /* Do host_records in config. */ |
| 1290 | for (hr = daemon->host_records; hr; hr = hr->next) |
| 1291 | for (nl = hr->names; nl; nl = nl->next) |
| 1292 | { |
| 1293 | if (hr->addr.s_addr != 0 && |
Simon Kelley | 3a610a0 | 2018-09-26 16:50:35 +0100 | [diff] [blame] | 1294 | (cache = whine_malloc(SIZEOF_POINTER_CREC))) |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1295 | { |
| 1296 | cache->name.namep = nl->name; |
Simon Kelley | df3d54f | 2016-02-24 21:03:38 +0000 | [diff] [blame] | 1297 | cache->ttd = hr->ttl; |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 1298 | cache->flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV4 | F_NAMEP | F_CONFIG; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1299 | add_hosts_entry(cache, (union all_addr *)&hr->addr, INADDRSZ, SRC_CONFIG, (struct crec **)daemon->packet, revhashsz); |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1300 | } |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 1301 | |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1302 | if (!IN6_IS_ADDR_UNSPECIFIED(&hr->addr6) && |
Simon Kelley | 3a610a0 | 2018-09-26 16:50:35 +0100 | [diff] [blame] | 1303 | (cache = whine_malloc(SIZEOF_POINTER_CREC))) |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1304 | { |
| 1305 | cache->name.namep = nl->name; |
Simon Kelley | df3d54f | 2016-02-24 21:03:38 +0000 | [diff] [blame] | 1306 | cache->ttd = hr->ttl; |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 1307 | cache->flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV6 | F_NAMEP | F_CONFIG; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1308 | add_hosts_entry(cache, (union all_addr *)&hr->addr6, IN6ADDRSZ, SRC_CONFIG, (struct crec **)daemon->packet, revhashsz); |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1309 | } |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1312 | if (option_bool(OPT_NO_HOSTS) && !daemon->addn_hosts) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1313 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1314 | if (daemon->cachesize > 0) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1315 | my_syslog(LOG_INFO, _("cleared cache")); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1316 | } |
Simon Kelley | caeea19 | 2015-02-14 20:08:56 +0000 | [diff] [blame] | 1317 | else |
| 1318 | { |
| 1319 | if (!option_bool(OPT_NO_HOSTS)) |
| 1320 | total_size = read_hostsfile(HOSTSFILE, SRC_HOSTS, total_size, (struct crec **)daemon->packet, revhashsz); |
| 1321 | |
| 1322 | daemon->addn_hosts = expand_filelist(daemon->addn_hosts); |
| 1323 | for (ah = daemon->addn_hosts; ah; ah = ah->next) |
| 1324 | if (!(ah->flags & AH_INACTIVE)) |
| 1325 | total_size = read_hostsfile(ah->fname, ah->index, total_size, (struct crec **)daemon->packet, revhashsz); |
| 1326 | } |
| 1327 | |
Simon Kelley | 70d1873 | 2015-01-31 19:59:29 +0000 | [diff] [blame] | 1328 | #ifdef HAVE_INOTIFY |
| 1329 | set_dynamic_inotify(AH_HOSTS, total_size, (struct crec **)daemon->packet, revhashsz); |
| 1330 | #endif |
| 1331 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1334 | #ifdef HAVE_DHCP |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1335 | struct in_addr a_record_from_hosts(char *name, time_t now) |
| 1336 | { |
| 1337 | struct crec *crecp = NULL; |
| 1338 | struct in_addr ret; |
| 1339 | |
| 1340 | while ((crecp = cache_find_by_name(crecp, name, now, F_IPV4))) |
| 1341 | if (crecp->flags & F_HOSTS) |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1342 | return crecp->addr.addr4; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1343 | |
| 1344 | my_syslog(MS_DHCP | LOG_WARNING, _("No IPv4 address found for %s"), name); |
| 1345 | |
| 1346 | ret.s_addr = 0; |
| 1347 | return ret; |
| 1348 | } |
| 1349 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1350 | void cache_unhash_dhcp(void) |
| 1351 | { |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1352 | struct crec *cache, **up; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1353 | int i; |
| 1354 | |
| 1355 | for (i=0; i<hash_size; i++) |
| 1356 | for (cache = hash_table[i], up = &hash_table[i]; cache; cache = cache->hash_next) |
| 1357 | if (cache->flags & F_DHCP) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1358 | { |
| 1359 | *up = cache->hash_next; |
| 1360 | cache->next = dhcp_spare; |
| 1361 | dhcp_spare = cache; |
| 1362 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1363 | else |
| 1364 | up = &cache->hash_next; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1367 | static void add_dhcp_cname(struct crec *target, time_t ttd) |
| 1368 | { |
| 1369 | struct crec *aliasc; |
| 1370 | struct cname *a; |
| 1371 | |
| 1372 | for (a = daemon->cnames; a; a = a->next) |
Simon Kelley | b637d78 | 2016-12-13 16:44:11 +0000 | [diff] [blame] | 1373 | if (a->alias[1] != '*' && |
| 1374 | hostname_isequal(cache_get_name(target), a->target)) |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1375 | { |
| 1376 | if ((aliasc = dhcp_spare)) |
| 1377 | dhcp_spare = dhcp_spare->next; |
| 1378 | else /* need new one */ |
Simon Kelley | 3a610a0 | 2018-09-26 16:50:35 +0100 | [diff] [blame] | 1379 | aliasc = whine_malloc(SIZEOF_POINTER_CREC); |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1380 | |
| 1381 | if (aliasc) |
| 1382 | { |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 1383 | aliasc->flags = F_FORWARD | F_NAMEP | F_DHCP | F_CNAME | F_CONFIG; |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1384 | if (ttd == 0) |
| 1385 | aliasc->flags |= F_IMMORTAL; |
| 1386 | else |
| 1387 | aliasc->ttd = ttd; |
| 1388 | aliasc->name.namep = a->alias; |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1389 | aliasc->addr.cname.target.cache = target; |
Simon Kelley | 45d8a24 | 2018-07-17 21:01:14 +0100 | [diff] [blame] | 1390 | next_uid(target); |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1391 | aliasc->addr.cname.uid = target->uid; |
Simon Kelley | 45d8a24 | 2018-07-17 21:01:14 +0100 | [diff] [blame] | 1392 | aliasc->uid = UID_NONE; |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1393 | cache_hash(aliasc); |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 1394 | make_non_terminals(aliasc); |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1395 | add_dhcp_cname(aliasc, ttd); |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1400 | void cache_add_dhcp_entry(char *host_name, int prot, |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1401 | union all_addr *host_address, time_t ttd) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1402 | { |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1403 | struct crec *crec = NULL, *fail_crec = NULL; |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 1404 | unsigned int flags = F_IPV4; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1405 | int in_hosts = 0; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1406 | size_t addrlen = sizeof(struct in_addr); |
| 1407 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1408 | if (prot == AF_INET6) |
| 1409 | { |
| 1410 | flags = F_IPV6; |
| 1411 | addrlen = sizeof(struct in6_addr); |
| 1412 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1413 | |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1414 | inet_ntop(prot, host_address, daemon->addrbuff, ADDRSTRLEN); |
| 1415 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1416 | while ((crec = cache_find_by_name(crec, host_name, 0, flags | F_CNAME))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1417 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1418 | /* check all addresses associated with name */ |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 1419 | if (crec->flags & (F_HOSTS | F_CONFIG)) |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 1420 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1421 | if (crec->flags & F_CNAME) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1422 | my_syslog(MS_DHCP | LOG_WARNING, |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1423 | _("%s is a CNAME, not giving it to the DHCP lease of %s"), |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1424 | host_name, daemon->addrbuff); |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1425 | else if (memcmp(&crec->addr, host_address, addrlen) == 0) |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1426 | in_hosts = 1; |
| 1427 | else |
| 1428 | fail_crec = crec; |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 1429 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1430 | else if (!(crec->flags & F_DHCP)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1431 | { |
Simon Kelley | 65a01b7 | 2018-12-31 23:56:33 +0000 | [diff] [blame] | 1432 | cache_scan_free(host_name, NULL, C_IN, 0, crec->flags & (flags | F_CNAME | F_FORWARD), NULL, NULL); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1433 | /* scan_free deletes all addresses associated with name */ |
| 1434 | break; |
| 1435 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1436 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1437 | |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1438 | /* if in hosts, don't need DHCP record */ |
| 1439 | if (in_hosts) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1440 | return; |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1441 | |
| 1442 | /* Name in hosts, address doesn't match */ |
| 1443 | if (fail_crec) |
| 1444 | { |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1445 | inet_ntop(prot, &fail_crec->addr, daemon->namebuff, MAXDNAME); |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1446 | my_syslog(MS_DHCP | LOG_WARNING, |
| 1447 | _("not giving name %s to the DHCP lease of %s because " |
| 1448 | "the name exists in %s with address %s"), |
| 1449 | host_name, daemon->addrbuff, |
| 1450 | record_source(fail_crec->uid), daemon->namebuff); |
| 1451 | return; |
| 1452 | } |
| 1453 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1454 | if ((crec = cache_find_by_addr(NULL, (union all_addr *)host_address, 0, flags))) |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1455 | { |
| 1456 | if (crec->flags & F_NEG) |
| 1457 | { |
| 1458 | flags |= F_REVERSE; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1459 | cache_scan_free(NULL, (union all_addr *)host_address, C_IN, 0, flags, NULL, NULL); |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | else |
| 1463 | flags |= F_REVERSE; |
| 1464 | |
| 1465 | if ((crec = dhcp_spare)) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1466 | dhcp_spare = dhcp_spare->next; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1467 | else /* need new one */ |
Simon Kelley | 3a610a0 | 2018-09-26 16:50:35 +0100 | [diff] [blame] | 1468 | crec = whine_malloc(SIZEOF_POINTER_CREC); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1469 | |
| 1470 | if (crec) /* malloc may fail */ |
| 1471 | { |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 1472 | crec->flags = flags | F_NAMEP | F_DHCP | F_FORWARD; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1473 | if (ttd == 0) |
| 1474 | crec->flags |= F_IMMORTAL; |
| 1475 | else |
| 1476 | crec->ttd = ttd; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1477 | crec->addr = *host_address; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1478 | crec->name.namep = host_name; |
Simon Kelley | 45d8a24 | 2018-07-17 21:01:14 +0100 | [diff] [blame] | 1479 | crec->uid = UID_NONE; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1480 | cache_hash(crec); |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 1481 | make_non_terminals(crec); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1482 | |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1483 | add_dhcp_cname(crec, ttd); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1484 | } |
| 1485 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1486 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1487 | |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 1488 | /* Called when we put a local or DHCP name into the cache. |
| 1489 | Creates empty cache entries for subnames (ie, |
| 1490 | for three.two.one, for two.one and one), without |
| 1491 | F_IPV4 or F_IPV6 or F_CNAME set. These convert |
| 1492 | NXDOMAIN answers to NoData ones. */ |
| 1493 | static void make_non_terminals(struct crec *source) |
| 1494 | { |
| 1495 | char *name = cache_get_name(source); |
Simon Kelley | ea6cc33 | 2018-09-18 23:21:17 +0100 | [diff] [blame] | 1496 | struct crec *crecp, *tmp, **up; |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 1497 | int type = F_HOSTS | F_CONFIG; |
| 1498 | #ifdef HAVE_DHCP |
| 1499 | if (source->flags & F_DHCP) |
| 1500 | type = F_DHCP; |
| 1501 | #endif |
| 1502 | |
| 1503 | /* First delete any empty entries for our new real name. Note that |
| 1504 | we only delete empty entries deriving from DHCP for a new DHCP-derived |
| 1505 | entry and vice-versa for HOSTS and CONFIG. This ensures that |
| 1506 | non-terminals from DHCP go when we reload DHCP and |
| 1507 | for HOSTS/CONFIG when we re-read. */ |
| 1508 | for (up = hash_bucket(name), crecp = *up; crecp; crecp = tmp) |
| 1509 | { |
| 1510 | tmp = crecp->hash_next; |
| 1511 | |
| 1512 | if (!is_outdated_cname_pointer(crecp) && |
| 1513 | (crecp->flags & F_FORWARD) && |
| 1514 | (crecp->flags & type) && |
Simon Kelley | cbfbd17 | 2018-08-21 18:25:18 +0100 | [diff] [blame] | 1515 | !(crecp->flags & (F_IPV4 | F_IPV6 | F_CNAME | F_DNSKEY | F_DS)) && |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 1516 | hostname_isequal(name, cache_get_name(crecp))) |
| 1517 | { |
| 1518 | *up = crecp->hash_next; |
| 1519 | #ifdef HAVE_DHCP |
| 1520 | if (type & F_DHCP) |
| 1521 | { |
| 1522 | crecp->next = dhcp_spare; |
| 1523 | dhcp_spare = crecp; |
| 1524 | } |
| 1525 | else |
| 1526 | #endif |
| 1527 | free(crecp); |
| 1528 | break; |
| 1529 | } |
| 1530 | else |
| 1531 | up = &crecp->hash_next; |
| 1532 | } |
| 1533 | |
| 1534 | while ((name = strchr(name, '.'))) |
| 1535 | { |
| 1536 | name++; |
| 1537 | |
| 1538 | /* Look for one existing, don't need another */ |
| 1539 | for (crecp = *hash_bucket(name); crecp; crecp = crecp->hash_next) |
| 1540 | if (!is_outdated_cname_pointer(crecp) && |
| 1541 | (crecp->flags & F_FORWARD) && |
| 1542 | (crecp->flags & type) && |
| 1543 | hostname_isequal(name, cache_get_name(crecp))) |
| 1544 | break; |
| 1545 | |
| 1546 | if (crecp) |
| 1547 | { |
| 1548 | /* If the new name expires later, transfer that time to |
| 1549 | empty non-terminal entry. */ |
| 1550 | if (!(crecp->flags & F_IMMORTAL)) |
| 1551 | { |
| 1552 | if (source->flags & F_IMMORTAL) |
| 1553 | crecp->flags |= F_IMMORTAL; |
| 1554 | else if (difftime(crecp->ttd, source->ttd) < 0) |
| 1555 | crecp->ttd = source->ttd; |
| 1556 | } |
| 1557 | continue; |
| 1558 | } |
| 1559 | |
| 1560 | #ifdef HAVE_DHCP |
| 1561 | if ((source->flags & F_DHCP) && dhcp_spare) |
| 1562 | { |
| 1563 | crecp = dhcp_spare; |
| 1564 | dhcp_spare = dhcp_spare->next; |
| 1565 | } |
| 1566 | else |
| 1567 | #endif |
Simon Kelley | 3a610a0 | 2018-09-26 16:50:35 +0100 | [diff] [blame] | 1568 | crecp = whine_malloc(SIZEOF_POINTER_CREC); |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 1569 | |
Simon Kelley | ea6cc33 | 2018-09-18 23:21:17 +0100 | [diff] [blame] | 1570 | if (crecp) |
| 1571 | { |
Simon Kelley | 48b090c | 2018-09-26 12:53:59 +0100 | [diff] [blame] | 1572 | crecp->flags = (source->flags | F_NAMEP) & ~(F_IPV4 | F_IPV6 | F_CNAME | F_DNSKEY | F_DS | F_REVERSE); |
| 1573 | crecp->ttd = source->ttd; |
Simon Kelley | ea6cc33 | 2018-09-18 23:21:17 +0100 | [diff] [blame] | 1574 | crecp->name.namep = name; |
| 1575 | |
| 1576 | cache_hash(crecp); |
| 1577 | } |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 1578 | } |
| 1579 | } |
| 1580 | |
Kevin Darbyshire-Bryant | 7ac9ae1 | 2016-09-09 20:52:08 +0100 | [diff] [blame] | 1581 | #ifndef NO_ID |
Simon Kelley | fec216d | 2014-03-27 20:54:34 +0000 | [diff] [blame] | 1582 | int cache_make_stat(struct txt_record *t) |
| 1583 | { |
| 1584 | static char *buff = NULL; |
| 1585 | static int bufflen = 60; |
| 1586 | int len; |
| 1587 | struct server *serv, *serv1; |
| 1588 | char *p; |
| 1589 | |
| 1590 | if (!buff && !(buff = whine_malloc(60))) |
| 1591 | return 0; |
| 1592 | |
| 1593 | p = buff; |
| 1594 | |
| 1595 | switch (t->stat) |
| 1596 | { |
| 1597 | case TXT_STAT_CACHESIZE: |
| 1598 | sprintf(buff+1, "%d", daemon->cachesize); |
| 1599 | break; |
| 1600 | |
| 1601 | case TXT_STAT_INSERTS: |
Julian Kornberger | aba8bbb | 2018-07-21 21:55:08 +0100 | [diff] [blame] | 1602 | sprintf(buff+1, "%d", daemon->metrics[METRIC_DNS_CACHE_INSERTED]); |
Simon Kelley | fec216d | 2014-03-27 20:54:34 +0000 | [diff] [blame] | 1603 | break; |
| 1604 | |
| 1605 | case TXT_STAT_EVICTIONS: |
Julian Kornberger | aba8bbb | 2018-07-21 21:55:08 +0100 | [diff] [blame] | 1606 | sprintf(buff+1, "%d", daemon->metrics[METRIC_DNS_CACHE_LIVE_FREED]); |
Simon Kelley | fec216d | 2014-03-27 20:54:34 +0000 | [diff] [blame] | 1607 | break; |
| 1608 | |
| 1609 | case TXT_STAT_MISSES: |
Julian Kornberger | aba8bbb | 2018-07-21 21:55:08 +0100 | [diff] [blame] | 1610 | sprintf(buff+1, "%u", daemon->metrics[METRIC_DNS_QUERIES_FORWARDED]); |
Simon Kelley | fec216d | 2014-03-27 20:54:34 +0000 | [diff] [blame] | 1611 | break; |
| 1612 | |
| 1613 | case TXT_STAT_HITS: |
Julian Kornberger | aba8bbb | 2018-07-21 21:55:08 +0100 | [diff] [blame] | 1614 | sprintf(buff+1, "%u", daemon->metrics[METRIC_DNS_LOCAL_ANSWERED]); |
Simon Kelley | fec216d | 2014-03-27 20:54:34 +0000 | [diff] [blame] | 1615 | break; |
| 1616 | |
| 1617 | #ifdef HAVE_AUTH |
| 1618 | case TXT_STAT_AUTH: |
Julian Kornberger | aba8bbb | 2018-07-21 21:55:08 +0100 | [diff] [blame] | 1619 | sprintf(buff+1, "%u", daemon->metrics[METRIC_DNS_AUTH_ANSWERED]); |
Simon Kelley | fec216d | 2014-03-27 20:54:34 +0000 | [diff] [blame] | 1620 | break; |
| 1621 | #endif |
| 1622 | |
| 1623 | case TXT_STAT_SERVERS: |
| 1624 | /* sum counts from different records for same server */ |
| 1625 | for (serv = daemon->servers; serv; serv = serv->next) |
| 1626 | serv->flags &= ~SERV_COUNTED; |
| 1627 | |
| 1628 | for (serv = daemon->servers; serv; serv = serv->next) |
| 1629 | if (!(serv->flags & |
| 1630 | (SERV_NO_ADDR | SERV_LITERAL_ADDRESS | SERV_COUNTED | SERV_USE_RESOLV | SERV_NO_REBIND))) |
| 1631 | { |
| 1632 | char *new, *lenp; |
| 1633 | int port, newlen, bytes_avail, bytes_needed; |
| 1634 | unsigned int queries = 0, failed_queries = 0; |
| 1635 | for (serv1 = serv; serv1; serv1 = serv1->next) |
| 1636 | if (!(serv1->flags & |
| 1637 | (SERV_NO_ADDR | SERV_LITERAL_ADDRESS | SERV_COUNTED | SERV_USE_RESOLV | SERV_NO_REBIND)) && |
| 1638 | sockaddr_isequal(&serv->addr, &serv1->addr)) |
| 1639 | { |
| 1640 | serv1->flags |= SERV_COUNTED; |
| 1641 | queries += serv1->queries; |
| 1642 | failed_queries += serv1->failed_queries; |
| 1643 | } |
| 1644 | port = prettyprint_addr(&serv->addr, daemon->addrbuff); |
| 1645 | lenp = p++; /* length */ |
Simon Kelley | 04b0ac0 | 2015-04-06 17:19:13 +0100 | [diff] [blame] | 1646 | bytes_avail = bufflen - (p - buff ); |
Simon Kelley | fec216d | 2014-03-27 20:54:34 +0000 | [diff] [blame] | 1647 | bytes_needed = snprintf(p, bytes_avail, "%s#%d %u %u", daemon->addrbuff, port, queries, failed_queries); |
| 1648 | if (bytes_needed >= bytes_avail) |
| 1649 | { |
| 1650 | /* expand buffer if necessary */ |
| 1651 | newlen = bytes_needed + 1 + bufflen - bytes_avail; |
| 1652 | if (!(new = whine_malloc(newlen))) |
| 1653 | return 0; |
| 1654 | memcpy(new, buff, bufflen); |
| 1655 | free(buff); |
| 1656 | p = new + (p - buff); |
| 1657 | lenp = p - 1; |
| 1658 | buff = new; |
| 1659 | bufflen = newlen; |
Simon Kelley | 04b0ac0 | 2015-04-06 17:19:13 +0100 | [diff] [blame] | 1660 | bytes_avail = bufflen - (p - buff ); |
Simon Kelley | fec216d | 2014-03-27 20:54:34 +0000 | [diff] [blame] | 1661 | bytes_needed = snprintf(p, bytes_avail, "%s#%d %u %u", daemon->addrbuff, port, queries, failed_queries); |
| 1662 | } |
| 1663 | *lenp = bytes_needed; |
| 1664 | p += bytes_needed; |
| 1665 | } |
| 1666 | t->txt = (unsigned char *)buff; |
| 1667 | t->len = p - buff; |
| 1668 | return 1; |
| 1669 | } |
| 1670 | |
| 1671 | len = strlen(buff+1); |
| 1672 | t->txt = (unsigned char *)buff; |
| 1673 | t->len = len + 1; |
| 1674 | *buff = len; |
| 1675 | return 1; |
| 1676 | } |
Kevin Darbyshire-Bryant | 7ac9ae1 | 2016-09-09 20:52:08 +0100 | [diff] [blame] | 1677 | #endif |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1678 | |
Simon Kelley | 394ff49 | 2015-03-29 22:17:14 +0100 | [diff] [blame] | 1679 | /* There can be names in the cache containing control chars, don't |
| 1680 | mess up logging or open security holes. */ |
| 1681 | static char *sanitise(char *name) |
| 1682 | { |
| 1683 | unsigned char *r; |
Simon Kelley | 794fccc | 2015-03-29 22:35:44 +0100 | [diff] [blame] | 1684 | if (name) |
| 1685 | for (r = (unsigned char *)name; *r; r++) |
| 1686 | if (!isprint((int)*r)) |
| 1687 | return "<name unprintable>"; |
Simon Kelley | 394ff49 | 2015-03-29 22:17:14 +0100 | [diff] [blame] | 1688 | |
| 1689 | return name; |
| 1690 | } |
| 1691 | |
| 1692 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1693 | void dump_cache(time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1694 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1695 | struct server *serv, *serv1; |
| 1696 | |
| 1697 | my_syslog(LOG_INFO, _("time %lu"), (unsigned long)now); |
| 1698 | my_syslog(LOG_INFO, _("cache size %d, %d/%d cache insertions re-used unexpired cache entries."), |
Julian Kornberger | aba8bbb | 2018-07-21 21:55:08 +0100 | [diff] [blame] | 1699 | daemon->cachesize, daemon->metrics[METRIC_DNS_CACHE_LIVE_FREED], daemon->metrics[METRIC_DNS_CACHE_INSERTED]); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1700 | my_syslog(LOG_INFO, _("queries forwarded %u, queries answered locally %u"), |
Julian Kornberger | aba8bbb | 2018-07-21 21:55:08 +0100 | [diff] [blame] | 1701 | daemon->metrics[METRIC_DNS_QUERIES_FORWARDED], daemon->metrics[METRIC_DNS_LOCAL_ANSWERED]); |
Simon Kelley | b485ed9 | 2013-10-18 22:00:39 +0100 | [diff] [blame] | 1702 | #ifdef HAVE_AUTH |
Julian Kornberger | aba8bbb | 2018-07-21 21:55:08 +0100 | [diff] [blame] | 1703 | my_syslog(LOG_INFO, _("queries for authoritative zones %u"), daemon->metrics[METRIC_DNS_AUTH_ANSWERED]); |
Simon Kelley | b485ed9 | 2013-10-18 22:00:39 +0100 | [diff] [blame] | 1704 | #endif |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 1705 | |
Simon Kelley | c220768 | 2014-01-08 18:04:20 +0000 | [diff] [blame] | 1706 | blockdata_report(); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1707 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1708 | /* sum counts from different records for same server */ |
| 1709 | for (serv = daemon->servers; serv; serv = serv->next) |
| 1710 | serv->flags &= ~SERV_COUNTED; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1711 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1712 | for (serv = daemon->servers; serv; serv = serv->next) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1713 | if (!(serv->flags & |
| 1714 | (SERV_NO_ADDR | SERV_LITERAL_ADDRESS | SERV_COUNTED | SERV_USE_RESOLV | SERV_NO_REBIND))) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1715 | { |
| 1716 | int port; |
| 1717 | unsigned int queries = 0, failed_queries = 0; |
| 1718 | for (serv1 = serv; serv1; serv1 = serv1->next) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1719 | if (!(serv1->flags & |
| 1720 | (SERV_NO_ADDR | SERV_LITERAL_ADDRESS | SERV_COUNTED | SERV_USE_RESOLV | SERV_NO_REBIND)) && |
| 1721 | sockaddr_isequal(&serv->addr, &serv1->addr)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1722 | { |
| 1723 | serv1->flags |= SERV_COUNTED; |
| 1724 | queries += serv1->queries; |
| 1725 | failed_queries += serv1->failed_queries; |
| 1726 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1727 | port = prettyprint_addr(&serv->addr, daemon->addrbuff); |
| 1728 | my_syslog(LOG_INFO, _("server %s#%d: queries sent %u, retried or failed %u"), daemon->addrbuff, port, queries, failed_queries); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1731 | if (option_bool(OPT_DEBUG) || option_bool(OPT_LOG)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1732 | { |
| 1733 | struct crec *cache ; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1734 | int i; |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 1735 | my_syslog(LOG_INFO, "Host Address Flags Expires"); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1736 | |
| 1737 | for (i=0; i<hash_size; i++) |
| 1738 | for (cache = hash_table[i]; cache; cache = cache->hash_next) |
| 1739 | { |
Simon Kelley | b6f926f | 2018-08-21 17:46:52 +0100 | [diff] [blame] | 1740 | char *t = " "; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1741 | char *a = daemon->addrbuff, *p = daemon->namebuff, *n = cache_get_name(cache); |
| 1742 | *a = 0; |
Simon Kelley | 2d33bda | 2014-01-24 22:37:25 +0000 | [diff] [blame] | 1743 | if (strlen(n) == 0 && !(cache->flags & F_REVERSE)) |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1744 | n = "<Root>"; |
Simon Kelley | 394ff49 | 2015-03-29 22:17:14 +0100 | [diff] [blame] | 1745 | p += sprintf(p, "%-30.30s ", sanitise(n)); |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1746 | if ((cache->flags & F_CNAME) && !is_outdated_cname_pointer(cache)) |
Simon Kelley | 394ff49 | 2015-03-29 22:17:14 +0100 | [diff] [blame] | 1747 | a = sanitise(cache_get_cname_target(cache)); |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 1748 | else if ((cache->flags & F_SRV) && !(cache->flags & F_NEG)) |
| 1749 | { |
| 1750 | int targetlen = cache->addr.srv.targetlen; |
| 1751 | ssize_t len = sprintf(a, "%u %u %u ", cache->addr.srv.priority, |
| 1752 | cache->addr.srv.weight, cache->addr.srv.srvport); |
| 1753 | |
| 1754 | if (targetlen > (40 - len)) |
| 1755 | targetlen = 40 - len; |
| 1756 | blockdata_retrieve(cache->addr.srv.target, targetlen, a + len); |
| 1757 | a[len + targetlen] = 0; |
| 1758 | } |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1759 | #ifdef HAVE_DNSSEC |
Simon Kelley | cdbee9a | 2012-04-04 21:55:59 +0100 | [diff] [blame] | 1760 | else if (cache->flags & F_DS) |
| 1761 | { |
Simon Kelley | 93be5b1 | 2015-12-15 12:04:40 +0000 | [diff] [blame] | 1762 | if (!(cache->flags & F_NEG)) |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1763 | sprintf(a, "%5u %3u %3u", cache->addr.ds.keytag, |
| 1764 | cache->addr.ds.algo, cache->addr.ds.digest); |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 1765 | } |
| 1766 | else if (cache->flags & F_DNSKEY) |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1767 | sprintf(a, "%5u %3u %3u", cache->addr.key.keytag, |
| 1768 | cache->addr.key.algo, cache->addr.key.flags); |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1769 | #endif |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1770 | else if (!(cache->flags & F_NEG) || !(cache->flags & F_FORWARD)) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1771 | { |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1772 | a = daemon->addrbuff; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1773 | if (cache->flags & F_IPV4) |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1774 | inet_ntop(AF_INET, &cache->addr, a, ADDRSTRLEN); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1775 | else if (cache->flags & F_IPV6) |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1776 | inet_ntop(AF_INET6, &cache->addr, a, ADDRSTRLEN); |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1777 | } |
| 1778 | |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 1779 | if (cache->flags & F_IPV4) |
| 1780 | t = "4"; |
| 1781 | else if (cache->flags & F_IPV6) |
| 1782 | t = "6"; |
| 1783 | else if (cache->flags & F_CNAME) |
| 1784 | t = "C"; |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 1785 | else if (cache->flags & F_SRV) |
| 1786 | t = "V"; |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 1787 | #ifdef HAVE_DNSSEC |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 1788 | else if (cache->flags & F_DS) |
| 1789 | t = "S"; |
| 1790 | else if (cache->flags & F_DNSKEY) |
| 1791 | t = "K"; |
| 1792 | #endif |
Simon Kelley | 3267804 | 2014-12-17 20:38:20 +0000 | [diff] [blame] | 1793 | p += sprintf(p, "%-40.40s %s%s%s%s%s%s%s%s%s ", a, t, |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1794 | cache->flags & F_FORWARD ? "F" : " ", |
| 1795 | cache->flags & F_REVERSE ? "R" : " ", |
| 1796 | cache->flags & F_IMMORTAL ? "I" : " ", |
| 1797 | cache->flags & F_DHCP ? "D" : " ", |
| 1798 | cache->flags & F_NEG ? "N" : " ", |
| 1799 | cache->flags & F_NXDOMAIN ? "X" : " ", |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1800 | cache->flags & F_HOSTS ? "H" : " ", |
| 1801 | cache->flags & F_DNSSECOK ? "V" : " "); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1802 | #ifdef HAVE_BROKEN_RTC |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1803 | p += sprintf(p, "%lu", cache->flags & F_IMMORTAL ? 0: (unsigned long)(cache->ttd - now)); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1804 | #else |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1805 | p += sprintf(p, "%s", cache->flags & F_IMMORTAL ? "\n" : ctime(&(cache->ttd))); |
| 1806 | /* ctime includes trailing \n - eat it */ |
| 1807 | *(p-1) = 0; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1808 | #endif |
Rosen Penev | cbd29e5 | 2017-06-27 22:29:51 +0100 | [diff] [blame] | 1809 | my_syslog(LOG_INFO, "%s", daemon->namebuff); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1810 | } |
| 1811 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Simon Kelley | 19c51cf | 2014-03-18 22:38:30 +0000 | [diff] [blame] | 1814 | char *record_source(unsigned int index) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1815 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1816 | struct hostsfile *ah; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1817 | |
Simon Kelley | 19c51cf | 2014-03-18 22:38:30 +0000 | [diff] [blame] | 1818 | if (index == SRC_CONFIG) |
| 1819 | return "config"; |
| 1820 | else if (index == SRC_HOSTS) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1821 | return HOSTSFILE; |
| 1822 | |
| 1823 | for (ah = daemon->addn_hosts; ah; ah = ah->next) |
| 1824 | if (ah->index == index) |
| 1825 | return ah->fname; |
Simon Kelley | 70d1873 | 2015-01-31 19:59:29 +0000 | [diff] [blame] | 1826 | |
| 1827 | #ifdef HAVE_INOTIFY |
| 1828 | for (ah = daemon->dynamic_dirs; ah; ah = ah->next) |
| 1829 | if (ah->index == index) |
| 1830 | return ah->fname; |
| 1831 | #endif |
| 1832 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1833 | return "<unknown>"; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1834 | } |
Simon Kelley | c1bb850 | 2004-08-11 18:40:17 +0100 | [diff] [blame] | 1835 | |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 1836 | char *querystr(char *desc, unsigned short type) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1837 | { |
| 1838 | unsigned int i; |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 1839 | int len = 10; /* strlen("type=xxxxx") */ |
| 1840 | const char *types = NULL; |
| 1841 | static char *buff = NULL; |
| 1842 | static int bufflen = 0; |
| 1843 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1844 | for (i = 0; i < (sizeof(typestr)/sizeof(typestr[0])); i++) |
| 1845 | if (typestr[i].type == type) |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 1846 | { |
| 1847 | types = typestr[i].name; |
| 1848 | len = strlen(types); |
| 1849 | break; |
| 1850 | } |
| 1851 | |
Simon Kelley | b758b67 | 2018-08-23 21:41:23 +0100 | [diff] [blame] | 1852 | if (desc) |
| 1853 | { |
| 1854 | len += 2; /* braces */ |
| 1855 | len += strlen(desc); |
| 1856 | } |
| 1857 | len++; /* terminator */ |
| 1858 | |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 1859 | if (!buff || bufflen < len) |
| 1860 | { |
| 1861 | if (buff) |
| 1862 | free(buff); |
| 1863 | else if (len < 20) |
| 1864 | len = 20; |
| 1865 | |
| 1866 | buff = whine_malloc(len); |
| 1867 | bufflen = len; |
| 1868 | } |
| 1869 | |
| 1870 | if (buff) |
| 1871 | { |
Simon Kelley | b758b67 | 2018-08-23 21:41:23 +0100 | [diff] [blame] | 1872 | if (desc) |
| 1873 | { |
| 1874 | if (types) |
| 1875 | sprintf(buff, "%s[%s]", desc, types); |
| 1876 | else |
| 1877 | sprintf(buff, "%s[type=%d]", desc, type); |
| 1878 | } |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 1879 | else |
Simon Kelley | b758b67 | 2018-08-23 21:41:23 +0100 | [diff] [blame] | 1880 | { |
| 1881 | if (types) |
| 1882 | sprintf(buff, "<%s>", types); |
| 1883 | else |
| 1884 | sprintf(buff, "type=%d", type); |
| 1885 | } |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 1886 | } |
Simon Kelley | b758b67 | 2018-08-23 21:41:23 +0100 | [diff] [blame] | 1887 | |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 1888 | return buff ? buff : ""; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1889 | } |
| 1890 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1891 | void log_query(unsigned int flags, char *name, union all_addr *addr, char *arg) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1892 | { |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1893 | char *source, *dest = daemon->addrbuff; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1894 | char *verb = "is"; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1895 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1896 | if (!option_bool(OPT_LOG)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1897 | return; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1898 | |
Simon Kelley | 394ff49 | 2015-03-29 22:17:14 +0100 | [diff] [blame] | 1899 | name = sanitise(name); |
| 1900 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1901 | if (addr) |
| 1902 | { |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1903 | if (flags & F_KEYTAG) |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1904 | sprintf(daemon->addrbuff, arg, addr->log.keytag, addr->log.algo, addr->log.digest); |
Simon Kelley | 07ed585 | 2018-05-04 21:52:22 +0100 | [diff] [blame] | 1905 | else if (flags & F_RCODE) |
| 1906 | { |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 1907 | unsigned int rcode = addr->log.rcode; |
Simon Kelley | 07ed585 | 2018-05-04 21:52:22 +0100 | [diff] [blame] | 1908 | |
| 1909 | if (rcode == SERVFAIL) |
| 1910 | dest = "SERVFAIL"; |
| 1911 | else if (rcode == REFUSED) |
| 1912 | dest = "REFUSED"; |
| 1913 | else if (rcode == NOTIMP) |
| 1914 | dest = "not implemented"; |
| 1915 | else |
| 1916 | sprintf(daemon->addrbuff, "%u", rcode); |
| 1917 | } |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1918 | else |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 1919 | inet_ntop(flags & F_IPV4 ? AF_INET : AF_INET6, |
| 1920 | addr, daemon->addrbuff, ADDRSTRLEN); |
| 1921 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1922 | } |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1923 | else |
| 1924 | dest = arg; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1925 | |
| 1926 | if (flags & F_REVERSE) |
| 1927 | { |
| 1928 | dest = name; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1929 | name = daemon->addrbuff; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1930 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1931 | |
| 1932 | if (flags & F_NEG) |
| 1933 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1934 | if (flags & F_NXDOMAIN) |
Simon Kelley | 40b695c | 2014-02-03 17:07:51 +0000 | [diff] [blame] | 1935 | dest = "NXDOMAIN"; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1936 | else |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1937 | { |
| 1938 | if (flags & F_IPV4) |
| 1939 | dest = "NODATA-IPv4"; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1940 | else if (flags & F_IPV6) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1941 | dest = "NODATA-IPv6"; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1942 | else |
| 1943 | dest = "NODATA"; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1944 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1945 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1946 | else if (flags & F_CNAME) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1947 | dest = "<CNAME>"; |
Simon Kelley | 5b99eae | 2019-01-06 23:09:50 +0000 | [diff] [blame] | 1948 | else if (flags & F_SRV) |
| 1949 | dest = "<SRV>"; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1950 | else if (flags & F_RRNAME) |
| 1951 | dest = arg; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1952 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1953 | if (flags & F_CONFIG) |
| 1954 | source = "config"; |
| 1955 | else if (flags & F_DHCP) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1956 | source = "DHCP"; |
| 1957 | else if (flags & F_HOSTS) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1958 | source = arg; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1959 | else if (flags & F_UPSTREAM) |
| 1960 | source = "reply"; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1961 | else if (flags & F_SECSTAT) |
| 1962 | source = "validation"; |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1963 | else if (flags & F_AUTH) |
| 1964 | source = "auth"; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1965 | else if (flags & F_SERVER) |
| 1966 | { |
| 1967 | source = "forwarded"; |
| 1968 | verb = "to"; |
| 1969 | } |
| 1970 | else if (flags & F_QUERY) |
| 1971 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1972 | source = arg; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1973 | verb = "from"; |
| 1974 | } |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1975 | else if (flags & F_DNSSEC) |
| 1976 | { |
| 1977 | source = arg; |
| 1978 | verb = "to"; |
| 1979 | } |
Wang Jian | 49752b9 | 2014-03-28 20:52:47 +0000 | [diff] [blame] | 1980 | else if (flags & F_IPSET) |
| 1981 | { |
| 1982 | source = "ipset add"; |
| 1983 | dest = name; |
| 1984 | name = arg; |
| 1985 | verb = daemon->addrbuff; |
| 1986 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1987 | else |
| 1988 | source = "cached"; |
| 1989 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1990 | if (strlen(name) == 0) |
| 1991 | name = "."; |
| 1992 | |
Simon Kelley | 25cf5e3 | 2015-01-09 15:53:03 +0000 | [diff] [blame] | 1993 | if (option_bool(OPT_EXTRALOG)) |
| 1994 | { |
Simon Kelley | 9f79ee4 | 2015-01-12 20:18:18 +0000 | [diff] [blame] | 1995 | int port = prettyprint_addr(daemon->log_source_addr, daemon->addrbuff2); |
Simon Kelley | 25cf5e3 | 2015-01-09 15:53:03 +0000 | [diff] [blame] | 1996 | if (flags & F_NOEXTRA) |
Simon Kelley | 9f79ee4 | 2015-01-12 20:18:18 +0000 | [diff] [blame] | 1997 | my_syslog(LOG_INFO, "* %s/%u %s %s %s %s", daemon->addrbuff2, port, source, name, verb, dest); |
Simon Kelley | 25cf5e3 | 2015-01-09 15:53:03 +0000 | [diff] [blame] | 1998 | else |
Simon Kelley | 9f79ee4 | 2015-01-12 20:18:18 +0000 | [diff] [blame] | 1999 | my_syslog(LOG_INFO, "%u %s/%u %s %s %s %s", daemon->log_display_id, daemon->addrbuff2, port, source, name, verb, dest); |
Simon Kelley | 25cf5e3 | 2015-01-09 15:53:03 +0000 | [diff] [blame] | 2000 | } |
| 2001 | else |
| 2002 | my_syslog(LOG_INFO, "%s %s %s %s", source, name, verb, dest); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 2005 | |