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