Simon Kelley | c47e3ba | 2014-01-08 17:07:54 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2014 Simon Kelley |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 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; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 24 | static int cache_inserted = 0, cache_live_freed = 0, insert_error; |
| 25 | static union bigname *big_free = NULL; |
| 26 | static int bignames_left, hash_size; |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 27 | static int uid = 1; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 28 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 29 | /* type->string mapping: this is also used by the name-hash function as a mixing table. */ |
| 30 | static const struct { |
| 31 | unsigned int type; |
| 32 | const char * const name; |
| 33 | } typestr[] = { |
| 34 | { 1, "A" }, |
| 35 | { 2, "NS" }, |
| 36 | { 5, "CNAME" }, |
| 37 | { 6, "SOA" }, |
| 38 | { 10, "NULL" }, |
| 39 | { 11, "WKS" }, |
| 40 | { 12, "PTR" }, |
| 41 | { 13, "HINFO" }, |
| 42 | { 15, "MX" }, |
| 43 | { 16, "TXT" }, |
| 44 | { 22, "NSAP" }, |
| 45 | { 23, "NSAP_PTR" }, |
| 46 | { 24, "SIG" }, |
| 47 | { 25, "KEY" }, |
| 48 | { 28, "AAAA" }, |
| 49 | { 33, "SRV" }, |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 50 | { 35, "NAPTR" }, |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 51 | { 36, "KX" }, |
| 52 | { 37, "CERT" }, |
| 53 | { 38, "A6" }, |
| 54 | { 39, "DNAME" }, |
| 55 | { 41, "OPT" }, |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 56 | { 43, "DS" }, |
| 57 | { 46, "RRSIG" }, |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 58 | { 47, "NSEC" }, |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 59 | { 48, "DNSKEY" }, |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 60 | { 50, "NSEC3" }, |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 61 | { 249, "TKEY" }, |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 62 | { 250, "TSIG" }, |
| 63 | { 251, "IXFR" }, |
| 64 | { 252, "AXFR" }, |
| 65 | { 253, "MAILB" }, |
| 66 | { 254, "MAILA" }, |
| 67 | { 255, "ANY" } |
| 68 | }; |
| 69 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 70 | static void cache_free(struct crec *crecp); |
| 71 | static void cache_unlink(struct crec *crecp); |
| 72 | static void cache_link(struct crec *crecp); |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 73 | static void rehash(int size); |
| 74 | static void cache_hash(struct crec *crecp); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 75 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 76 | void cache_init(void) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 77 | { |
| 78 | struct crec *crecp; |
| 79 | int i; |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 80 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 81 | bignames_left = daemon->cachesize/10; |
| 82 | |
| 83 | if (daemon->cachesize > 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 84 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 85 | crecp = safe_malloc(daemon->cachesize*sizeof(struct crec)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 86 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 87 | for (i=0; i < daemon->cachesize; i++, crecp++) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 88 | { |
| 89 | cache_link(crecp); |
| 90 | crecp->flags = 0; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 91 | crecp->uid = uid++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 95 | /* create initial hash table*/ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 96 | rehash(daemon->cachesize); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 99 | /* In most cases, we create the hash table once here by calling this with (hash_table == NULL) |
| 100 | but if the hosts file(s) are big (some people have 50000 ad-block entries), the table |
| 101 | will be much too small, so the hosts reading code calls rehash every 1000 addresses, to |
| 102 | expand the table. */ |
| 103 | static void rehash(int size) |
| 104 | { |
| 105 | struct crec **new, **old, *p, *tmp; |
| 106 | int i, new_size, old_size; |
| 107 | |
| 108 | /* hash_size is a power of two. */ |
| 109 | for (new_size = 64; new_size < size/10; new_size = new_size << 1); |
| 110 | |
| 111 | /* must succeed in getting first instance, failure later is non-fatal */ |
| 112 | if (!hash_table) |
| 113 | new = safe_malloc(new_size * sizeof(struct crec *)); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 114 | 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] | 115 | return; |
| 116 | |
| 117 | for(i = 0; i < new_size; i++) |
| 118 | new[i] = NULL; |
| 119 | |
| 120 | old = hash_table; |
| 121 | old_size = hash_size; |
| 122 | hash_table = new; |
| 123 | hash_size = new_size; |
| 124 | |
| 125 | if (old) |
| 126 | { |
| 127 | for (i = 0; i < old_size; i++) |
| 128 | for (p = old[i]; p ; p = tmp) |
| 129 | { |
| 130 | tmp = p->hash_next; |
| 131 | cache_hash(p); |
| 132 | } |
| 133 | free(old); |
| 134 | } |
| 135 | } |
| 136 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 137 | static struct crec **hash_bucket(char *name) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 138 | { |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 139 | 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] | 140 | const unsigned char *mix_tab = (const unsigned char*)typestr; |
| 141 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 142 | while((c = (unsigned char) *name++)) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 143 | { |
| 144 | /* don't use tolower and friends here - they may be messed up by LOCALE */ |
| 145 | if (c >= 'A' && c <= 'Z') |
| 146 | c += 'a' - 'A'; |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 147 | val = ((val << 7) | (val >> (32 - 7))) + (mix_tab[(val + c) & 0x3F] ^ c); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 148 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 149 | |
| 150 | /* hash_size is a power of two */ |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 151 | return hash_table + ((val ^ (val >> 16)) & (hash_size - 1)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void cache_hash(struct crec *crecp) |
| 155 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 156 | /* maintain an invariant that all entries with F_REVERSE set |
| 157 | are at the start of the hash-chain and all non-reverse |
| 158 | immortal entries are at the end of the hash-chain. |
| 159 | This allows reverse searches and garbage collection to be optimised */ |
| 160 | |
| 161 | struct crec **up = hash_bucket(cache_get_name(crecp)); |
| 162 | |
| 163 | if (!(crecp->flags & F_REVERSE)) |
| 164 | { |
| 165 | while (*up && ((*up)->flags & F_REVERSE)) |
| 166 | up = &((*up)->hash_next); |
| 167 | |
| 168 | if (crecp->flags & F_IMMORTAL) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 169 | while (*up && !((*up)->flags & F_IMMORTAL)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 170 | up = &((*up)->hash_next); |
| 171 | } |
| 172 | crecp->hash_next = *up; |
| 173 | *up = crecp; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 174 | } |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 175 | |
| 176 | #ifdef HAVE_DNSSEC |
| 177 | static void cache_blockdata_free(struct crec *crecp) |
| 178 | { |
| 179 | if (crecp->flags & F_DNSKEY) |
| 180 | { |
| 181 | if (crecp->flags & F_DS) |
| 182 | blockdata_free(crecp->addr.sig.keydata); |
| 183 | else |
| 184 | blockdata_free(crecp->addr.key.keydata); |
| 185 | } |
| 186 | else if (crecp->flags & F_DS) |
| 187 | blockdata_free(crecp->addr.ds.keydata); |
| 188 | } |
| 189 | #endif |
| 190 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 191 | static void cache_free(struct crec *crecp) |
| 192 | { |
| 193 | crecp->flags &= ~F_FORWARD; |
| 194 | crecp->flags &= ~F_REVERSE; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 195 | crecp->uid = uid++; /* invalidate CNAMES pointing to this. */ |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 196 | |
| 197 | if (uid == -1) |
| 198 | uid++; |
| 199 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 200 | if (cache_tail) |
| 201 | cache_tail->next = crecp; |
| 202 | else |
| 203 | cache_head = crecp; |
| 204 | crecp->prev = cache_tail; |
| 205 | crecp->next = NULL; |
| 206 | cache_tail = crecp; |
| 207 | |
| 208 | /* retrieve big name for further use. */ |
| 209 | if (crecp->flags & F_BIGNAME) |
| 210 | { |
| 211 | crecp->name.bname->next = big_free; |
| 212 | big_free = crecp->name.bname; |
| 213 | crecp->flags &= ~F_BIGNAME; |
| 214 | } |
Simon Kelley | 072e81b | 2014-01-31 12:42:54 +0000 | [diff] [blame] | 215 | |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 216 | #ifdef HAVE_DNSSEC |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 217 | cache_blockdata_free(crecp); |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 218 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* insert a new cache entry at the head of the list (youngest entry) */ |
| 222 | static void cache_link(struct crec *crecp) |
| 223 | { |
| 224 | if (cache_head) /* check needed for init code */ |
| 225 | cache_head->prev = crecp; |
| 226 | crecp->next = cache_head; |
| 227 | crecp->prev = NULL; |
| 228 | cache_head = crecp; |
| 229 | if (!cache_tail) |
| 230 | cache_tail = crecp; |
| 231 | } |
| 232 | |
| 233 | /* remove an arbitrary cache entry for promotion */ |
| 234 | static void cache_unlink (struct crec *crecp) |
| 235 | { |
| 236 | if (crecp->prev) |
| 237 | crecp->prev->next = crecp->next; |
| 238 | else |
| 239 | cache_head = crecp->next; |
| 240 | |
| 241 | if (crecp->next) |
| 242 | crecp->next->prev = crecp->prev; |
| 243 | else |
| 244 | cache_tail = crecp->prev; |
| 245 | } |
| 246 | |
| 247 | char *cache_get_name(struct crec *crecp) |
| 248 | { |
| 249 | if (crecp->flags & F_BIGNAME) |
| 250 | return crecp->name.bname->name; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 251 | else if (crecp->flags & F_NAMEP) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 252 | return crecp->name.namep; |
| 253 | |
| 254 | return crecp->name.sname; |
| 255 | } |
| 256 | |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 257 | char *cache_get_cname_target(struct crec *crecp) |
| 258 | { |
| 259 | if (crecp->addr.cname.uid != -1) |
| 260 | return cache_get_name(crecp->addr.cname.target.cache); |
| 261 | |
| 262 | return crecp->addr.cname.target.int_name->name; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | |
Simon Kelley | b75e936 | 2012-12-07 11:50:41 +0000 | [diff] [blame] | 267 | struct crec *cache_enumerate(int init) |
| 268 | { |
| 269 | static int bucket; |
| 270 | static struct crec *cache; |
| 271 | |
| 272 | if (init) |
| 273 | { |
| 274 | bucket = 0; |
| 275 | cache = NULL; |
| 276 | } |
| 277 | else if (cache && cache->hash_next) |
| 278 | cache = cache->hash_next; |
| 279 | else |
| 280 | { |
| 281 | cache = NULL; |
| 282 | while (bucket < hash_size) |
| 283 | if ((cache = hash_table[bucket++])) |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | return cache; |
| 288 | } |
| 289 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 290 | static int is_outdated_cname_pointer(struct crec *crecp) |
| 291 | { |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 292 | if (!(crecp->flags & F_CNAME) || crecp->addr.cname.uid == -1) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 293 | return 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 294 | |
Simon Kelley | cdbee9a | 2012-04-04 21:55:59 +0100 | [diff] [blame] | 295 | /* NB. record may be reused as DS or DNSKEY, where uid is |
| 296 | overloaded for something completely different */ |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 297 | if (crecp->addr.cname.target.cache && |
| 298 | (crecp->addr.cname.target.cache->flags & (F_IPV4 | F_IPV6 | F_CNAME)) && |
| 299 | crecp->addr.cname.uid == crecp->addr.cname.target.cache->uid) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 300 | return 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 301 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 302 | return 1; |
| 303 | } |
| 304 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 305 | static int is_expired(time_t now, struct crec *crecp) |
| 306 | { |
| 307 | if (crecp->flags & F_IMMORTAL) |
| 308 | return 0; |
| 309 | |
| 310 | if (difftime(now, crecp->ttd) < 0) |
| 311 | return 0; |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 312 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 313 | return 1; |
| 314 | } |
| 315 | |
| 316 | static int cache_scan_free(char *name, struct all_addr *addr, time_t now, unsigned short flags) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 317 | { |
| 318 | /* Scan and remove old entries. |
| 319 | If (flags & F_FORWARD) then remove any forward entries for name and any expired |
| 320 | entries but only in the same hash bucket as name. |
| 321 | If (flags & F_REVERSE) then remove any reverse entries for addr and any expired |
| 322 | entries in the whole cache. |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 323 | If (flags == 0) remove any expired entries in the whole cache. |
| 324 | |
| 325 | In the flags & F_FORWARD case, the return code is valid, and returns zero if the |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 326 | 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] | 327 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 328 | We take advantage of the fact that hash chains have stuff in the order <reverse>,<other>,<immortal> |
| 329 | so that when we hit an entry which isn't reverse and is immortal, we're done. */ |
| 330 | |
| 331 | struct crec *crecp, **up; |
| 332 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 333 | if (flags & F_FORWARD) |
| 334 | { |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 335 | for (up = hash_bucket(name), crecp = *up; crecp; crecp = crecp->hash_next) |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 336 | { |
| 337 | if (is_expired(now, crecp) || is_outdated_cname_pointer(crecp)) |
| 338 | { |
| 339 | *up = crecp->hash_next; |
| 340 | if (!(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG))) |
| 341 | { |
| 342 | cache_unlink(crecp); |
| 343 | cache_free(crecp); |
| 344 | } |
| 345 | continue; |
| 346 | } |
| 347 | |
| 348 | if ((crecp->flags & F_FORWARD) && hostname_isequal(cache_get_name(crecp), name)) |
| 349 | { |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 350 | /* Don't delete DNSSEC in favour of a CNAME, they can co-exist */ |
| 351 | if ((flags & crecp->flags & (F_IPV4 | F_IPV6)) || |
Simon Kelley | 6429e42 | 2014-01-23 12:09:36 +0000 | [diff] [blame] | 352 | (((crecp->flags | flags) & F_CNAME) && !(crecp->flags & (F_DNSKEY | F_DS)))) |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 353 | { |
| 354 | if (crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) |
| 355 | return 0; |
| 356 | *up = crecp->hash_next; |
| 357 | cache_unlink(crecp); |
| 358 | cache_free(crecp); |
| 359 | continue; |
| 360 | } |
| 361 | |
| 362 | #ifdef HAVE_DNSSEC |
| 363 | /* Deletion has to be class-sensitive for DS, DNSKEY, RRSIG, also |
| 364 | type-covered sensitive for RRSIG */ |
Simon Kelley | 583043f | 2014-01-28 14:54:46 +0000 | [diff] [blame] | 365 | if ((flags & (F_DNSKEY | F_DS)) && |
| 366 | (flags & (F_DNSKEY | F_DS)) == (crecp->flags & (F_DNSKEY | F_DS)) && |
Simon Kelley | 824202e | 2014-01-23 20:59:46 +0000 | [diff] [blame] | 367 | crecp->uid == addr->addr.dnssec.class && |
| 368 | (!((flags & (F_DS | F_DNSKEY)) == (F_DS | F_DNSKEY)) || |
| 369 | crecp->addr.sig.type_covered == addr->addr.dnssec.type)) |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 370 | { |
Simon Kelley | 824202e | 2014-01-23 20:59:46 +0000 | [diff] [blame] | 371 | if (crecp->flags & F_CONFIG) |
| 372 | return 0; |
| 373 | *up = crecp->hash_next; |
| 374 | cache_unlink(crecp); |
| 375 | cache_free(crecp); |
| 376 | continue; |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 377 | } |
| 378 | #endif |
| 379 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 380 | up = &crecp->hash_next; |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 381 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 382 | } |
| 383 | else |
| 384 | { |
| 385 | int i; |
| 386 | #ifdef HAVE_IPV6 |
| 387 | int addrlen = (flags & F_IPV6) ? IN6ADDRSZ : INADDRSZ; |
| 388 | #else |
| 389 | int addrlen = INADDRSZ; |
| 390 | #endif |
| 391 | for (i = 0; i < hash_size; i++) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 392 | for (crecp = hash_table[i], up = &hash_table[i]; |
| 393 | crecp && ((crecp->flags & F_REVERSE) || !(crecp->flags & F_IMMORTAL)); |
| 394 | crecp = crecp->hash_next) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 395 | if (is_expired(now, crecp)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 396 | { |
| 397 | *up = crecp->hash_next; |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 398 | if (!(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 399 | { |
| 400 | cache_unlink(crecp); |
| 401 | cache_free(crecp); |
| 402 | } |
| 403 | } |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 404 | else if (!(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) && |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 405 | (flags & crecp->flags & F_REVERSE) && |
| 406 | (flags & crecp->flags & (F_IPV4 | F_IPV6)) && |
| 407 | memcmp(&crecp->addr.addr, addr, addrlen) == 0) |
| 408 | { |
| 409 | *up = crecp->hash_next; |
| 410 | cache_unlink(crecp); |
| 411 | cache_free(crecp); |
| 412 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 413 | else |
| 414 | up = &crecp->hash_next; |
| 415 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 416 | |
| 417 | return 1; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* Note: The normal calling sequence is |
| 421 | cache_start_insert |
| 422 | cache_insert * n |
| 423 | cache_end_insert |
| 424 | |
| 425 | but an abort can cause the cache_end_insert to be missed |
| 426 | in which can the next cache_start_insert cleans things up. */ |
| 427 | |
| 428 | void cache_start_insert(void) |
| 429 | { |
| 430 | /* Free any entries which didn't get committed during the last |
| 431 | insert due to error. |
| 432 | */ |
| 433 | while (new_chain) |
| 434 | { |
| 435 | struct crec *tmp = new_chain->next; |
| 436 | cache_free(new_chain); |
| 437 | new_chain = tmp; |
| 438 | } |
| 439 | new_chain = NULL; |
| 440 | insert_error = 0; |
| 441 | } |
| 442 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 443 | struct crec *cache_insert(char *name, struct all_addr *addr, |
| 444 | time_t now, unsigned long ttl, unsigned short flags) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 445 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 446 | struct crec *new; |
| 447 | union bigname *big_name = NULL; |
| 448 | int freed_all = flags & F_REVERSE; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 449 | int free_avail = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 450 | |
Simon Kelley | a0ab18f | 2014-02-13 16:38:23 +0000 | [diff] [blame] | 451 | /* Don't log DNSSEC records here, done elsewhere */ |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 452 | if (flags & (F_IPV4 | F_IPV6 | F_CNAME)) |
Simon Kelley | a0ab18f | 2014-02-13 16:38:23 +0000 | [diff] [blame] | 453 | { |
| 454 | log_query(flags | F_UPSTREAM, name, addr, NULL); |
| 455 | /* Don;t mess with TTL for DNSSEC records. */ |
| 456 | if (daemon->max_cache_ttl != 0 && daemon->max_cache_ttl < ttl) |
| 457 | ttl = daemon->max_cache_ttl; |
| 458 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 459 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 460 | /* if previous insertion failed give up now. */ |
| 461 | if (insert_error) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 462 | return NULL; |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 463 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 464 | /* First remove any expired entries and entries for the name/address we |
Simon Kelley | c8ca33f | 2014-02-10 10:35:42 +0000 | [diff] [blame] | 465 | are currently inserting. Fail if we attempt to delete a name from |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 466 | /etc/hosts or DHCP. */ |
| 467 | if (!cache_scan_free(name, addr, now, flags)) |
| 468 | { |
| 469 | insert_error = 1; |
| 470 | return NULL; |
| 471 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 472 | |
| 473 | /* Now get a cache entry from the end of the LRU list */ |
| 474 | while (1) { |
| 475 | if (!(new = cache_tail)) /* no entries left - cache is too small, bail */ |
| 476 | { |
| 477 | insert_error = 1; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 478 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* End of LRU list is still in use: if we didn't scan all the hash |
| 482 | chains for expired entries do that now. If we already tried that |
| 483 | then it's time to start spilling things. */ |
| 484 | |
| 485 | if (new->flags & (F_FORWARD | F_REVERSE)) |
| 486 | { |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 487 | /* If free_avail set, we believe that an entry has been freed. |
| 488 | Bugs have been known to make this not true, resulting in |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 489 | a tight loop here. If that happens, abandon the |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 490 | insert. Once in this state, all inserts will probably fail. */ |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 491 | if (free_avail) |
| 492 | { |
Simon Kelley | 5f93853 | 2014-02-03 16:44:32 +0000 | [diff] [blame] | 493 | static int warned = 0; |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 494 | if (!warned) |
| 495 | { |
| 496 | my_syslog(LOG_ERR, _("Internal error in cache.")); |
| 497 | warned = 1; |
| 498 | } |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 499 | insert_error = 1; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 500 | return NULL; |
| 501 | } |
| 502 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 503 | if (freed_all) |
| 504 | { |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 505 | struct all_addr free_addr = new->addr.addr;; |
| 506 | |
| 507 | #ifdef HAVE_DNSSEC |
| 508 | /* For DNSSEC records, addr holds class and type_covered for RRSIG */ |
| 509 | if (new->flags & (F_DS | F_DNSKEY)) |
| 510 | { |
| 511 | free_addr.addr.dnssec.class = new->uid; |
| 512 | if ((new->flags & (F_DS | F_DNSKEY)) == (F_DS | F_DNSKEY)) |
| 513 | free_addr.addr.dnssec.type = new->addr.sig.type_covered; |
| 514 | } |
| 515 | #endif |
| 516 | |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 517 | free_avail = 1; /* Must be free space now. */ |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 518 | cache_scan_free(cache_get_name(new), &free_addr, now, new->flags); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 519 | cache_live_freed++; |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | cache_scan_free(NULL, NULL, now, 0); |
| 524 | freed_all = 1; |
| 525 | } |
| 526 | continue; |
| 527 | } |
| 528 | |
| 529 | /* Check if we need to and can allocate extra memory for a long name. |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 530 | If that fails, give up now, always succeed for DNSSEC records. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 531 | if (name && (strlen(name) > SMALLDNAME-1)) |
| 532 | { |
| 533 | if (big_free) |
| 534 | { |
| 535 | big_name = big_free; |
| 536 | big_free = big_free->next; |
| 537 | } |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 538 | else if ((bignames_left == 0 && !(flags & (F_DS | F_DNSKEY))) || |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 539 | !(big_name = (union bigname *)whine_malloc(sizeof(union bigname)))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 540 | { |
| 541 | insert_error = 1; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 542 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 543 | } |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 544 | else if (bignames_left != 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 545 | bignames_left--; |
| 546 | |
| 547 | } |
| 548 | |
| 549 | /* Got the rest: finally grab entry. */ |
| 550 | cache_unlink(new); |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | new->flags = flags; |
| 555 | if (big_name) |
| 556 | { |
| 557 | new->name.bname = big_name; |
| 558 | new->flags |= F_BIGNAME; |
| 559 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 560 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 561 | if (name) |
| 562 | strcpy(cache_get_name(new), name); |
| 563 | else |
| 564 | *cache_get_name(new) = 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 565 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 566 | if (addr) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 567 | new->addr.addr = *addr; |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 568 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 569 | new->ttd = now + (time_t)ttl; |
| 570 | new->next = new_chain; |
| 571 | new_chain = new; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 572 | |
| 573 | return new; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* after end of insertion, commit the new entries */ |
| 577 | void cache_end_insert(void) |
| 578 | { |
| 579 | if (insert_error) |
| 580 | return; |
| 581 | |
| 582 | while (new_chain) |
| 583 | { |
| 584 | struct crec *tmp = new_chain->next; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 585 | /* drop CNAMEs which didn't find a target. */ |
| 586 | if (is_outdated_cname_pointer(new_chain)) |
| 587 | cache_free(new_chain); |
| 588 | else |
| 589 | { |
| 590 | cache_hash(new_chain); |
| 591 | cache_link(new_chain); |
| 592 | cache_inserted++; |
| 593 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 594 | new_chain = tmp; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 595 | } |
| 596 | new_chain = NULL; |
| 597 | } |
| 598 | |
Simon Kelley | 12fae49 | 2014-02-04 22:03:06 +0000 | [diff] [blame] | 599 | 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] | 600 | { |
| 601 | struct crec *ans; |
Simon Kelley | 12fae49 | 2014-02-04 22:03:06 +0000 | [diff] [blame] | 602 | int no_rr = prot & F_NO_RR; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 603 | |
Simon Kelley | 12fae49 | 2014-02-04 22:03:06 +0000 | [diff] [blame] | 604 | prot &= ~F_NO_RR; |
| 605 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 606 | if (crecp) /* iterating */ |
| 607 | ans = crecp->next; |
| 608 | else |
| 609 | { |
| 610 | /* first search, look for relevant entries and push to top of list |
| 611 | also free anything which has expired */ |
| 612 | struct crec *next, **up, **insert = NULL, **chainp = &ans; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 613 | unsigned short ins_flags = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 614 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 615 | for (up = hash_bucket(name), crecp = *up; crecp; crecp = next) |
| 616 | { |
| 617 | next = crecp->hash_next; |
| 618 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 619 | if (!is_expired(now, crecp) && !is_outdated_cname_pointer(crecp)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 620 | { |
| 621 | if ((crecp->flags & F_FORWARD) && |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 622 | #ifdef HAVE_DNSSEC |
| 623 | ((crecp->flags & (F_DNSKEY | F_DS)) == (prot & (F_DNSKEY | F_DS))) && |
| 624 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 625 | (crecp->flags & prot) && |
| 626 | hostname_isequal(cache_get_name(crecp), name)) |
| 627 | { |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 628 | if (crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 629 | { |
| 630 | *chainp = crecp; |
| 631 | chainp = &crecp->next; |
| 632 | } |
| 633 | else |
| 634 | { |
| 635 | cache_unlink(crecp); |
| 636 | cache_link(crecp); |
| 637 | } |
| 638 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 639 | /* Move all but the first entry up the hash chain |
| 640 | this implements round-robin. |
| 641 | Make sure that re-ordering doesn't break the hash-chain |
| 642 | order invariants. |
| 643 | */ |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 644 | if (insert && (crecp->flags & (F_REVERSE | F_IMMORTAL)) == ins_flags) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 645 | { |
| 646 | *up = crecp->hash_next; |
| 647 | crecp->hash_next = *insert; |
| 648 | *insert = crecp; |
| 649 | insert = &crecp->hash_next; |
| 650 | } |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 651 | else |
| 652 | { |
Simon Kelley | 12fae49 | 2014-02-04 22:03:06 +0000 | [diff] [blame] | 653 | if (!insert && !no_rr) |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 654 | { |
| 655 | insert = up; |
| 656 | ins_flags = crecp->flags & (F_REVERSE | F_IMMORTAL); |
| 657 | } |
| 658 | up = &crecp->hash_next; |
| 659 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 660 | } |
| 661 | else |
| 662 | /* case : not expired, incorrect entry. */ |
| 663 | up = &crecp->hash_next; |
| 664 | } |
| 665 | else |
| 666 | { |
| 667 | /* expired entry, free it */ |
| 668 | *up = crecp->hash_next; |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 669 | if (!(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 670 | { |
| 671 | cache_unlink(crecp); |
| 672 | cache_free(crecp); |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | *chainp = cache_head; |
| 678 | } |
| 679 | |
| 680 | if (ans && |
| 681 | (ans->flags & F_FORWARD) && |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 682 | #ifdef HAVE_DNSSEC |
| 683 | ((ans->flags & (F_DNSKEY | F_DS)) == (prot & (F_DNSKEY | F_DS))) && |
| 684 | #endif |
| 685 | (ans->flags & prot) && |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 686 | hostname_isequal(cache_get_name(ans), name)) |
| 687 | return ans; |
| 688 | |
| 689 | return NULL; |
| 690 | } |
| 691 | |
| 692 | 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] | 693 | time_t now, unsigned int prot) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 694 | { |
| 695 | struct crec *ans; |
| 696 | #ifdef HAVE_IPV6 |
| 697 | int addrlen = (prot == F_IPV6) ? IN6ADDRSZ : INADDRSZ; |
| 698 | #else |
| 699 | int addrlen = INADDRSZ; |
| 700 | #endif |
| 701 | |
| 702 | if (crecp) /* iterating */ |
| 703 | ans = crecp->next; |
| 704 | else |
| 705 | { |
| 706 | /* first search, look for relevant entries and push to top of list |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 707 | also free anything which has expired. All the reverse entries are at the |
| 708 | start of the hash chain, so we can give up when we find the first |
| 709 | non-REVERSE one. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 710 | int i; |
| 711 | struct crec **up, **chainp = &ans; |
| 712 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 713 | for (i=0; i<hash_size; i++) |
| 714 | for (crecp = hash_table[i], up = &hash_table[i]; |
| 715 | crecp && (crecp->flags & F_REVERSE); |
| 716 | crecp = crecp->hash_next) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 717 | if (!is_expired(now, crecp)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 718 | { |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 719 | if ((crecp->flags & prot) && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 720 | memcmp(&crecp->addr.addr, addr, addrlen) == 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 721 | { |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 722 | if (crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 723 | { |
| 724 | *chainp = crecp; |
| 725 | chainp = &crecp->next; |
| 726 | } |
| 727 | else |
| 728 | { |
| 729 | cache_unlink(crecp); |
| 730 | cache_link(crecp); |
| 731 | } |
| 732 | } |
| 733 | up = &crecp->hash_next; |
| 734 | } |
| 735 | else |
| 736 | { |
| 737 | *up = crecp->hash_next; |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 738 | if (!(crecp->flags & (F_HOSTS | F_DHCP | F_CONFIG))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 739 | { |
| 740 | cache_unlink(crecp); |
| 741 | cache_free(crecp); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | *chainp = cache_head; |
| 746 | } |
| 747 | |
| 748 | if (ans && |
| 749 | (ans->flags & F_REVERSE) && |
| 750 | (ans->flags & prot) && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 751 | memcmp(&ans->addr.addr, addr, addrlen) == 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 752 | return ans; |
| 753 | |
| 754 | return NULL; |
| 755 | } |
| 756 | |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 757 | static void add_hosts_cname(struct crec *target) |
| 758 | { |
| 759 | struct crec *crec; |
| 760 | struct cname *a; |
| 761 | |
| 762 | for (a = daemon->cnames; a; a = a->next) |
| 763 | if (hostname_isequal(cache_get_name(target), a->target) && |
| 764 | (crec = whine_malloc(sizeof(struct crec)))) |
| 765 | { |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 766 | crec->flags = F_FORWARD | F_IMMORTAL | F_NAMEP | F_CONFIG | F_CNAME; |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 767 | crec->name.namep = a->alias; |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 768 | crec->addr.cname.target.cache = target; |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 769 | crec->addr.cname.uid = target->uid; |
| 770 | cache_hash(crec); |
| 771 | add_hosts_cname(crec); /* handle chains */ |
| 772 | } |
| 773 | } |
| 774 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 775 | static void add_hosts_entry(struct crec *cache, struct all_addr *addr, int addrlen, |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 776 | int index, struct crec **rhash, int hashsz) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 777 | { |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 778 | 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] | 779 | int i, nameexists = 0; |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 780 | unsigned int j; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 781 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 782 | /* Remove duplicates in hosts files. */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 783 | if (lookup && (lookup->flags & F_HOSTS)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 784 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 785 | nameexists = 1; |
| 786 | if (memcmp(&lookup->addr.addr, addr, addrlen) == 0) |
| 787 | { |
| 788 | free(cache); |
| 789 | return; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | /* Ensure there is only one address -> name mapping (first one trumps) |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 794 | We do this by steam here, The entries are kept in hash chains, linked |
| 795 | 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] | 796 | the array rhash, hashed on address. Note that rhash and the values |
| 797 | in ->next are only valid whilst reading hosts files: the buckets are |
| 798 | then freed, and the ->next pointer used for other things. |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 799 | |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 800 | Only insert each unique address once into this hashing structure. |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 801 | |
| 802 | This complexity avoids O(n^2) divergent CPU use whilst reading |
| 803 | large (10000 entry) hosts files. */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 804 | |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 805 | /* hash address */ |
| 806 | for (j = 0, i = 0; i < addrlen; i++) |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 807 | j = (j*2 +((unsigned char *)addr)[i]) % hashsz; |
Simon Kelley | 915363f | 2012-01-11 22:00:48 +0000 | [diff] [blame] | 808 | |
| 809 | for (lookup = rhash[j]; lookup; lookup = lookup->next) |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 810 | if ((lookup->flags & cache->flags & (F_IPV4 | F_IPV6)) && |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 811 | memcmp(&lookup->addr.addr, addr, addrlen) == 0) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 812 | { |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 813 | cache->flags &= ~F_REVERSE; |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 814 | break; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Simon Kelley | 915363f | 2012-01-11 22:00:48 +0000 | [diff] [blame] | 817 | /* maintain address hash chain, insert new unique address */ |
| 818 | if (!lookup) |
| 819 | { |
| 820 | cache->next = rhash[j]; |
| 821 | rhash[j] = cache; |
| 822 | } |
| 823 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 824 | cache->uid = index; |
Simon Kelley | 915363f | 2012-01-11 22:00:48 +0000 | [diff] [blame] | 825 | memcpy(&cache->addr.addr, addr, addrlen); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 826 | cache_hash(cache); |
| 827 | |
| 828 | /* don't need to do alias stuff for second and subsequent addresses. */ |
| 829 | if (!nameexists) |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 830 | add_hosts_cname(cache); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | static int eatspace(FILE *f) |
| 834 | { |
| 835 | int c, nl = 0; |
| 836 | |
| 837 | while (1) |
| 838 | { |
| 839 | if ((c = getc(f)) == '#') |
| 840 | while (c != '\n' && c != EOF) |
| 841 | c = getc(f); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 842 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 843 | if (c == EOF) |
| 844 | return 1; |
| 845 | |
| 846 | if (!isspace(c)) |
| 847 | { |
| 848 | ungetc(c, f); |
| 849 | return nl; |
| 850 | } |
| 851 | |
| 852 | if (c == '\n') |
| 853 | nl = 1; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | static int gettok(FILE *f, char *token) |
| 858 | { |
| 859 | int c, count = 0; |
| 860 | |
| 861 | while (1) |
| 862 | { |
| 863 | if ((c = getc(f)) == EOF) |
| 864 | return (count == 0) ? EOF : 1; |
| 865 | |
| 866 | if (isspace(c) || c == '#') |
| 867 | { |
| 868 | ungetc(c, f); |
| 869 | return eatspace(f); |
| 870 | } |
| 871 | |
| 872 | if (count < (MAXDNAME - 1)) |
| 873 | { |
| 874 | token[count++] = c; |
| 875 | token[count] = 0; |
| 876 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 880 | static int read_hostsfile(char *filename, int index, int cache_size, struct crec **rhash, int hashsz) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 881 | { |
| 882 | FILE *f = fopen(filename, "r"); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 883 | char *token = daemon->namebuff, *domain_suffix = NULL; |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 884 | int addr_count = 0, name_count = cache_size, lineno = 0; |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 885 | unsigned short flags = 0; |
| 886 | struct all_addr addr; |
| 887 | int atnl, addrlen = 0; |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 888 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 889 | if (!f) |
| 890 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 891 | my_syslog(LOG_ERR, _("failed to load names from %s: %s"), filename, strerror(errno)); |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 892 | return 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 893 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 894 | |
| 895 | eatspace(f); |
| 896 | |
| 897 | while ((atnl = gettok(f, token)) != EOF) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 898 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 899 | lineno++; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 900 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 901 | if (inet_pton(AF_INET, token, &addr) > 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 902 | { |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 903 | flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV4; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 904 | addrlen = INADDRSZ; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 905 | domain_suffix = get_domain(addr.addr.addr4); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 906 | } |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 907 | #ifdef HAVE_IPV6 |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 908 | else if (inet_pton(AF_INET6, token, &addr) > 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 909 | { |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 910 | flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV6; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 911 | addrlen = IN6ADDRSZ; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 912 | domain_suffix = get_domain6(&addr.addr.addr6); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 913 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 914 | #endif |
| 915 | else |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 916 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 917 | my_syslog(LOG_ERR, _("bad address at %s line %d"), filename, lineno); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 918 | while (atnl == 0) |
| 919 | atnl = gettok(f, token); |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 920 | continue; |
| 921 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 922 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 923 | addr_count++; |
| 924 | |
| 925 | /* rehash every 1000 names. */ |
| 926 | if ((name_count - cache_size) > 1000) |
| 927 | { |
| 928 | rehash(name_count); |
| 929 | cache_size = name_count; |
| 930 | } |
| 931 | |
| 932 | while (atnl == 0) |
| 933 | { |
| 934 | struct crec *cache; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 935 | int fqdn, nomem; |
| 936 | char *canon; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 937 | |
| 938 | if ((atnl = gettok(f, token)) == EOF) |
| 939 | break; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 940 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 941 | fqdn = !!strchr(token, '.'); |
| 942 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 943 | if ((canon = canonicalise(token, &nomem))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 944 | { |
| 945 | /* 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] | 946 | if (option_bool(OPT_EXPAND) && domain_suffix && !fqdn && |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 947 | (cache = whine_malloc(sizeof(struct crec) + |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 948 | strlen(canon)+2+strlen(domain_suffix)-SMALLDNAME))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 949 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 950 | strcpy(cache->name.sname, canon); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 951 | strcat(cache->name.sname, "."); |
| 952 | strcat(cache->name.sname, domain_suffix); |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 953 | cache->flags = flags; |
| 954 | add_hosts_entry(cache, &addr, addrlen, index, rhash, hashsz); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 955 | name_count++; |
| 956 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 957 | if ((cache = whine_malloc(sizeof(struct crec) + strlen(canon)+1-SMALLDNAME))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 958 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 959 | strcpy(cache->name.sname, canon); |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 960 | cache->flags = flags; |
| 961 | add_hosts_entry(cache, &addr, addrlen, index, rhash, hashsz); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 962 | name_count++; |
| 963 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 964 | free(canon); |
| 965 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 966 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 967 | else if (!nomem) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 968 | my_syslog(LOG_ERR, _("bad name at %s line %d"), filename, lineno); |
| 969 | } |
| 970 | } |
| 971 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 972 | fclose(f); |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 973 | rehash(name_count); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 974 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 975 | my_syslog(LOG_INFO, _("read %s - %d addresses"), filename, addr_count); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 976 | |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 977 | return name_count; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 980 | void cache_reload(void) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 981 | { |
| 982 | struct crec *cache, **up, *tmp; |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 983 | int revhashsz, i, total_size = daemon->cachesize; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 984 | struct hostsfile *ah; |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 985 | struct host_record *hr; |
| 986 | struct name_list *nl; |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 987 | struct cname *a; |
| 988 | struct interface_name *intr; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 989 | #ifdef HAVE_DNSSEC |
Simon Kelley | ee41586 | 2014-02-11 11:07:22 +0000 | [diff] [blame] | 990 | struct ds_config *ds; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 991 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 992 | |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 993 | cache_inserted = cache_live_freed = 0; |
| 994 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 995 | for (i=0; i<hash_size; i++) |
| 996 | for (cache = hash_table[i], up = &hash_table[i]; cache; cache = tmp) |
| 997 | { |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 998 | #ifdef HAVE_DNSSEC |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 999 | cache_blockdata_free(cache); |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1000 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1001 | tmp = cache->hash_next; |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 1002 | if (cache->flags & (F_HOSTS | F_CONFIG)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1003 | { |
| 1004 | *up = cache->hash_next; |
| 1005 | free(cache); |
| 1006 | } |
| 1007 | else if (!(cache->flags & F_DHCP)) |
| 1008 | { |
| 1009 | *up = cache->hash_next; |
| 1010 | if (cache->flags & F_BIGNAME) |
| 1011 | { |
| 1012 | cache->name.bname->next = big_free; |
| 1013 | big_free = cache->name.bname; |
| 1014 | } |
| 1015 | cache->flags = 0; |
| 1016 | } |
| 1017 | else |
| 1018 | up = &cache->hash_next; |
| 1019 | } |
| 1020 | |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1021 | /* Add CNAMEs to interface_names to the cache */ |
| 1022 | for (a = daemon->cnames; a; a = a->next) |
| 1023 | for (intr = daemon->int_names; intr; intr = intr->next) |
Simon Kelley | 532066e | 2013-11-26 10:14:47 +0000 | [diff] [blame] | 1024 | if (hostname_isequal(a->target, intr->name) && |
| 1025 | ((cache = whine_malloc(sizeof(struct crec))))) |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1026 | { |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 1027 | cache->flags = F_FORWARD | F_NAMEP | F_CNAME | F_IMMORTAL | F_CONFIG; |
Simon Kelley | 532066e | 2013-11-26 10:14:47 +0000 | [diff] [blame] | 1028 | cache->name.namep = a->alias; |
| 1029 | cache->addr.cname.target.int_name = intr; |
| 1030 | cache->addr.cname.uid = -1; |
| 1031 | cache_hash(cache); |
| 1032 | add_hosts_cname(cache); /* handle chains */ |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1033 | } |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1034 | |
| 1035 | #ifdef HAVE_DNSSEC |
Simon Kelley | ee41586 | 2014-02-11 11:07:22 +0000 | [diff] [blame] | 1036 | for (ds = daemon->ds; ds; ds = ds->next) |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1037 | if ((cache = whine_malloc(sizeof(struct crec))) && |
Simon Kelley | ee41586 | 2014-02-11 11:07:22 +0000 | [diff] [blame] | 1038 | (cache->addr.ds.keydata = blockdata_alloc(ds->digest, ds->digestlen))) |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1039 | { |
Simon Kelley | ee41586 | 2014-02-11 11:07:22 +0000 | [diff] [blame] | 1040 | cache->flags = F_FORWARD | F_IMMORTAL | F_DS | F_CONFIG | F_NAMEP; |
| 1041 | cache->name.namep = ds->name; |
| 1042 | cache->addr.ds.keylen = ds->digestlen; |
| 1043 | cache->addr.ds.algo = ds->algo; |
| 1044 | cache->addr.ds.keytag = ds->keytag; |
| 1045 | cache->addr.ds.digest = ds->digest_type; |
| 1046 | cache->uid = ds->class; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1047 | cache_hash(cache); |
| 1048 | } |
| 1049 | #endif |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1050 | |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1051 | /* borrow the packet buffer for a temporary by-address hash */ |
| 1052 | memset(daemon->packet, 0, daemon->packet_buff_sz); |
| 1053 | revhashsz = daemon->packet_buff_sz / sizeof(struct crec *); |
| 1054 | /* we overwrote the buffer... */ |
| 1055 | daemon->srv_save = NULL; |
| 1056 | |
| 1057 | /* Do host_records in config. */ |
| 1058 | for (hr = daemon->host_records; hr; hr = hr->next) |
| 1059 | for (nl = hr->names; nl; nl = nl->next) |
| 1060 | { |
| 1061 | if (hr->addr.s_addr != 0 && |
| 1062 | (cache = whine_malloc(sizeof(struct crec)))) |
| 1063 | { |
| 1064 | cache->name.namep = nl->name; |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 1065 | cache->flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV4 | F_NAMEP | F_CONFIG; |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1066 | add_hosts_entry(cache, (struct all_addr *)&hr->addr, INADDRSZ, 0, (struct crec **)daemon->packet, revhashsz); |
| 1067 | } |
| 1068 | #ifdef HAVE_IPV6 |
| 1069 | if (!IN6_IS_ADDR_UNSPECIFIED(&hr->addr6) && |
| 1070 | (cache = whine_malloc(sizeof(struct crec)))) |
| 1071 | { |
| 1072 | cache->name.namep = nl->name; |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 1073 | cache->flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV6 | F_NAMEP | F_CONFIG; |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 1074 | add_hosts_entry(cache, (struct all_addr *)&hr->addr6, IN6ADDRSZ, 0, (struct crec **)daemon->packet, revhashsz); |
| 1075 | } |
| 1076 | #endif |
| 1077 | } |
| 1078 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1079 | if (option_bool(OPT_NO_HOSTS) && !daemon->addn_hosts) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1080 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1081 | if (daemon->cachesize > 0) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1082 | my_syslog(LOG_INFO, _("cleared cache")); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1083 | return; |
| 1084 | } |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 1085 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1086 | if (!option_bool(OPT_NO_HOSTS)) |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 1087 | total_size = read_hostsfile(HOSTSFILE, 0, total_size, (struct crec **)daemon->packet, revhashsz); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1088 | |
| 1089 | daemon->addn_hosts = expand_filelist(daemon->addn_hosts); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1090 | for (ah = daemon->addn_hosts; ah; ah = ah->next) |
| 1091 | if (!(ah->flags & AH_INACTIVE)) |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 1092 | total_size = read_hostsfile(ah->fname, ah->index, total_size, (struct crec **)daemon->packet, revhashsz); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1095 | #ifdef HAVE_DHCP |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1096 | struct in_addr a_record_from_hosts(char *name, time_t now) |
| 1097 | { |
| 1098 | struct crec *crecp = NULL; |
| 1099 | struct in_addr ret; |
| 1100 | |
| 1101 | while ((crecp = cache_find_by_name(crecp, name, now, F_IPV4))) |
| 1102 | if (crecp->flags & F_HOSTS) |
| 1103 | return *(struct in_addr *)&crecp->addr; |
| 1104 | |
| 1105 | my_syslog(MS_DHCP | LOG_WARNING, _("No IPv4 address found for %s"), name); |
| 1106 | |
| 1107 | ret.s_addr = 0; |
| 1108 | return ret; |
| 1109 | } |
| 1110 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1111 | void cache_unhash_dhcp(void) |
| 1112 | { |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1113 | struct crec *cache, **up; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1114 | int i; |
| 1115 | |
| 1116 | for (i=0; i<hash_size; i++) |
| 1117 | for (cache = hash_table[i], up = &hash_table[i]; cache; cache = cache->hash_next) |
| 1118 | if (cache->flags & F_DHCP) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1119 | { |
| 1120 | *up = cache->hash_next; |
| 1121 | cache->next = dhcp_spare; |
| 1122 | dhcp_spare = cache; |
| 1123 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1124 | else |
| 1125 | up = &cache->hash_next; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1128 | static void add_dhcp_cname(struct crec *target, time_t ttd) |
| 1129 | { |
| 1130 | struct crec *aliasc; |
| 1131 | struct cname *a; |
| 1132 | |
| 1133 | for (a = daemon->cnames; a; a = a->next) |
| 1134 | if (hostname_isequal(cache_get_name(target), a->target)) |
| 1135 | { |
| 1136 | if ((aliasc = dhcp_spare)) |
| 1137 | dhcp_spare = dhcp_spare->next; |
| 1138 | else /* need new one */ |
| 1139 | aliasc = whine_malloc(sizeof(struct crec)); |
| 1140 | |
| 1141 | if (aliasc) |
| 1142 | { |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 1143 | aliasc->flags = F_FORWARD | F_NAMEP | F_DHCP | F_CNAME | F_CONFIG; |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1144 | if (ttd == 0) |
| 1145 | aliasc->flags |= F_IMMORTAL; |
| 1146 | else |
| 1147 | aliasc->ttd = ttd; |
| 1148 | aliasc->name.namep = a->alias; |
Simon Kelley | d56a604 | 2013-10-11 14:39:03 +0100 | [diff] [blame] | 1149 | aliasc->addr.cname.target.cache = target; |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1150 | aliasc->addr.cname.uid = target->uid; |
| 1151 | cache_hash(aliasc); |
| 1152 | add_dhcp_cname(aliasc, ttd); |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1157 | void cache_add_dhcp_entry(char *host_name, int prot, |
| 1158 | struct all_addr *host_address, time_t ttd) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1159 | { |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1160 | struct crec *crec = NULL, *fail_crec = NULL; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1161 | unsigned short flags = F_IPV4; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1162 | int in_hosts = 0; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1163 | size_t addrlen = sizeof(struct in_addr); |
| 1164 | |
| 1165 | #ifdef HAVE_IPV6 |
| 1166 | if (prot == AF_INET6) |
| 1167 | { |
| 1168 | flags = F_IPV6; |
| 1169 | addrlen = sizeof(struct in6_addr); |
| 1170 | } |
| 1171 | #endif |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1172 | |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1173 | inet_ntop(prot, host_address, daemon->addrbuff, ADDRSTRLEN); |
| 1174 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1175 | 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] | 1176 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1177 | /* check all addresses associated with name */ |
Simon Kelley | 2543906 | 2013-11-25 21:14:51 +0000 | [diff] [blame] | 1178 | if (crec->flags & (F_HOSTS | F_CONFIG)) |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 1179 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1180 | if (crec->flags & F_CNAME) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1181 | my_syslog(MS_DHCP | LOG_WARNING, |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1182 | _("%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] | 1183 | host_name, daemon->addrbuff); |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1184 | else if (memcmp(&crec->addr.addr, host_address, addrlen) == 0) |
| 1185 | in_hosts = 1; |
| 1186 | else |
| 1187 | fail_crec = crec; |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 1188 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1189 | else if (!(crec->flags & F_DHCP)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1190 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1191 | cache_scan_free(host_name, NULL, 0, crec->flags & (flags | F_CNAME | F_FORWARD)); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1192 | /* scan_free deletes all addresses associated with name */ |
| 1193 | break; |
| 1194 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1195 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1196 | |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1197 | /* if in hosts, don't need DHCP record */ |
| 1198 | if (in_hosts) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1199 | return; |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1200 | |
| 1201 | /* Name in hosts, address doesn't match */ |
| 1202 | if (fail_crec) |
| 1203 | { |
| 1204 | inet_ntop(prot, &fail_crec->addr.addr, daemon->namebuff, MAXDNAME); |
| 1205 | my_syslog(MS_DHCP | LOG_WARNING, |
| 1206 | _("not giving name %s to the DHCP lease of %s because " |
| 1207 | "the name exists in %s with address %s"), |
| 1208 | host_name, daemon->addrbuff, |
| 1209 | record_source(fail_crec->uid), daemon->namebuff); |
| 1210 | return; |
| 1211 | } |
| 1212 | |
| 1213 | if ((crec = cache_find_by_addr(NULL, (struct all_addr *)host_address, 0, flags))) |
| 1214 | { |
| 1215 | if (crec->flags & F_NEG) |
| 1216 | { |
| 1217 | flags |= F_REVERSE; |
| 1218 | cache_scan_free(NULL, (struct all_addr *)host_address, 0, flags); |
| 1219 | } |
| 1220 | } |
| 1221 | else |
| 1222 | flags |= F_REVERSE; |
| 1223 | |
| 1224 | if ((crec = dhcp_spare)) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1225 | dhcp_spare = dhcp_spare->next; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1226 | else /* need new one */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1227 | crec = whine_malloc(sizeof(struct crec)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1228 | |
| 1229 | if (crec) /* malloc may fail */ |
| 1230 | { |
Simon Kelley | bce6e1b | 2014-01-23 22:02:19 +0000 | [diff] [blame] | 1231 | crec->flags = flags | F_NAMEP | F_DHCP | F_FORWARD; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1232 | if (ttd == 0) |
| 1233 | crec->flags |= F_IMMORTAL; |
| 1234 | else |
| 1235 | crec->ttd = ttd; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1236 | crec->addr.addr = *host_address; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1237 | crec->name.namep = host_name; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1238 | crec->uid = uid++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1239 | cache_hash(crec); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1240 | |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1241 | add_dhcp_cname(crec, ttd); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1242 | } |
| 1243 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1244 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1245 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1246 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1247 | void dump_cache(time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1248 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1249 | struct server *serv, *serv1; |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 1250 | char *t = ""; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1251 | |
| 1252 | my_syslog(LOG_INFO, _("time %lu"), (unsigned long)now); |
| 1253 | my_syslog(LOG_INFO, _("cache size %d, %d/%d cache insertions re-used unexpired cache entries."), |
| 1254 | daemon->cachesize, cache_live_freed, cache_inserted); |
| 1255 | my_syslog(LOG_INFO, _("queries forwarded %u, queries answered locally %u"), |
| 1256 | daemon->queries_forwarded, daemon->local_answer); |
Simon Kelley | b485ed9 | 2013-10-18 22:00:39 +0100 | [diff] [blame] | 1257 | #ifdef HAVE_AUTH |
| 1258 | my_syslog(LOG_INFO, _("queries for authoritative zones %u"), daemon->auth_answer); |
| 1259 | #endif |
Simon Kelley | c220768 | 2014-01-08 18:04:20 +0000 | [diff] [blame] | 1260 | #ifdef HAVE_DNSSEC |
| 1261 | blockdata_report(); |
| 1262 | #endif |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1263 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1264 | /* sum counts from different records for same server */ |
| 1265 | for (serv = daemon->servers; serv; serv = serv->next) |
| 1266 | serv->flags &= ~SERV_COUNTED; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1267 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1268 | for (serv = daemon->servers; serv; serv = serv->next) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1269 | if (!(serv->flags & |
| 1270 | (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] | 1271 | { |
| 1272 | int port; |
| 1273 | unsigned int queries = 0, failed_queries = 0; |
| 1274 | for (serv1 = serv; serv1; serv1 = serv1->next) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1275 | if (!(serv1->flags & |
| 1276 | (SERV_NO_ADDR | SERV_LITERAL_ADDRESS | SERV_COUNTED | SERV_USE_RESOLV | SERV_NO_REBIND)) && |
| 1277 | sockaddr_isequal(&serv->addr, &serv1->addr)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1278 | { |
| 1279 | serv1->flags |= SERV_COUNTED; |
| 1280 | queries += serv1->queries; |
| 1281 | failed_queries += serv1->failed_queries; |
| 1282 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1283 | port = prettyprint_addr(&serv->addr, daemon->addrbuff); |
| 1284 | 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] | 1285 | } |
| 1286 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1287 | if (option_bool(OPT_DEBUG) || option_bool(OPT_LOG)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1288 | { |
| 1289 | struct crec *cache ; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1290 | int i; |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 1291 | my_syslog(LOG_INFO, "Host Address Flags Expires"); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1292 | |
| 1293 | for (i=0; i<hash_size; i++) |
| 1294 | for (cache = hash_table[i]; cache; cache = cache->hash_next) |
| 1295 | { |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1296 | char *a = daemon->addrbuff, *p = daemon->namebuff, *n = cache_get_name(cache); |
| 1297 | *a = 0; |
Simon Kelley | 2d33bda | 2014-01-24 22:37:25 +0000 | [diff] [blame] | 1298 | if (strlen(n) == 0 && !(cache->flags & F_REVERSE)) |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1299 | n = "<Root>"; |
| 1300 | p += sprintf(p, "%-40.40s ", n); |
| 1301 | if ((cache->flags & F_CNAME) && !is_outdated_cname_pointer(cache)) |
| 1302 | a = cache_get_cname_target(cache); |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1303 | #ifdef HAVE_DNSSEC |
Simon Kelley | cdbee9a | 2012-04-04 21:55:59 +0100 | [diff] [blame] | 1304 | else if (cache->flags & F_DS) |
| 1305 | { |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 1306 | if (cache->flags & F_DNSKEY) |
| 1307 | { |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 1308 | /* RRSIG */ |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 1309 | a = daemon->addrbuff; |
| 1310 | sprintf(a, "%5u %3u %s", cache->addr.sig.keytag, |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 1311 | cache->addr.sig.algo, querystr("", cache->addr.sig.type_covered)); |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 1312 | } |
| 1313 | else |
| 1314 | { |
| 1315 | a = daemon->addrbuff; |
| 1316 | sprintf(a, "%5u %3u %3u", cache->addr.ds.keytag, |
| 1317 | cache->addr.ds.algo, cache->addr.ds.digest); |
| 1318 | } |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 1319 | } |
| 1320 | else if (cache->flags & F_DNSKEY) |
| 1321 | { |
| 1322 | a = daemon->addrbuff; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1323 | sprintf(a, "%5u %3u %3u", cache->addr.key.keytag, |
Simon Kelley | 51ea3ca | 2014-01-22 19:31:38 +0000 | [diff] [blame] | 1324 | cache->addr.key.algo, cache->addr.key.flags); |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1325 | } |
| 1326 | #endif |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1327 | else if (!(cache->flags & F_NEG) || !(cache->flags & F_FORWARD)) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1328 | { |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1329 | a = daemon->addrbuff; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1330 | if (cache->flags & F_IPV4) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1331 | inet_ntop(AF_INET, &cache->addr.addr, a, ADDRSTRLEN); |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1332 | #ifdef HAVE_IPV6 |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1333 | else if (cache->flags & F_IPV6) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1334 | inet_ntop(AF_INET6, &cache->addr.addr, a, ADDRSTRLEN); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1335 | #endif |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1336 | } |
| 1337 | |
Simon Kelley | e7829ae | 2014-01-22 22:21:51 +0000 | [diff] [blame] | 1338 | if (cache->flags & F_IPV4) |
| 1339 | t = "4"; |
| 1340 | else if (cache->flags & F_IPV6) |
| 1341 | t = "6"; |
| 1342 | else if (cache->flags & F_CNAME) |
| 1343 | t = "C"; |
| 1344 | #ifdef HAVE_DNSSEC |
| 1345 | else if ((cache->flags & (F_DS | F_DNSKEY)) == (F_DS | F_DNSKEY)) |
| 1346 | t = "G"; /* DNSKEY and DS set -> RRISG */ |
| 1347 | else if (cache->flags & F_DS) |
| 1348 | t = "S"; |
| 1349 | else if (cache->flags & F_DNSKEY) |
| 1350 | t = "K"; |
| 1351 | #endif |
| 1352 | p += sprintf(p, "%-30.30s %s%s%s%s%s%s%s%s%s ", a, t, |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1353 | cache->flags & F_FORWARD ? "F" : " ", |
| 1354 | cache->flags & F_REVERSE ? "R" : " ", |
| 1355 | cache->flags & F_IMMORTAL ? "I" : " ", |
| 1356 | cache->flags & F_DHCP ? "D" : " ", |
| 1357 | cache->flags & F_NEG ? "N" : " ", |
| 1358 | cache->flags & F_NXDOMAIN ? "X" : " ", |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1359 | cache->flags & F_HOSTS ? "H" : " ", |
| 1360 | cache->flags & F_DNSSECOK ? "V" : " "); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1361 | #ifdef HAVE_BROKEN_RTC |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1362 | 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] | 1363 | #else |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1364 | p += sprintf(p, "%s", cache->flags & F_IMMORTAL ? "\n" : ctime(&(cache->ttd))); |
| 1365 | /* ctime includes trailing \n - eat it */ |
| 1366 | *(p-1) = 0; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1367 | #endif |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1368 | my_syslog(LOG_INFO, daemon->namebuff); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1369 | } |
| 1370 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1373 | char *record_source(int index) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1374 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1375 | struct hostsfile *ah; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1376 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1377 | if (index == 0) |
| 1378 | return HOSTSFILE; |
| 1379 | |
| 1380 | for (ah = daemon->addn_hosts; ah; ah = ah->next) |
| 1381 | if (ah->index == index) |
| 1382 | return ah->fname; |
| 1383 | |
| 1384 | return "<unknown>"; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1385 | } |
Simon Kelley | c1bb850 | 2004-08-11 18:40:17 +0100 | [diff] [blame] | 1386 | |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 1387 | char *querystr(char *desc, unsigned short type) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1388 | { |
| 1389 | unsigned int i; |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 1390 | int len = 10; /* strlen("type=xxxxx") */ |
| 1391 | const char *types = NULL; |
| 1392 | static char *buff = NULL; |
| 1393 | static int bufflen = 0; |
| 1394 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1395 | for (i = 0; i < (sizeof(typestr)/sizeof(typestr[0])); i++) |
| 1396 | if (typestr[i].type == type) |
Simon Kelley | 610e782 | 2014-02-06 14:45:17 +0000 | [diff] [blame] | 1397 | { |
| 1398 | types = typestr[i].name; |
| 1399 | len = strlen(types); |
| 1400 | break; |
| 1401 | } |
| 1402 | |
| 1403 | len += 3; /* braces, terminator */ |
| 1404 | len += strlen(desc); |
| 1405 | |
| 1406 | if (!buff || bufflen < len) |
| 1407 | { |
| 1408 | if (buff) |
| 1409 | free(buff); |
| 1410 | else if (len < 20) |
| 1411 | len = 20; |
| 1412 | |
| 1413 | buff = whine_malloc(len); |
| 1414 | bufflen = len; |
| 1415 | } |
| 1416 | |
| 1417 | if (buff) |
| 1418 | { |
| 1419 | if (types) |
| 1420 | sprintf(buff, "%s[%s]", desc, types); |
| 1421 | else |
| 1422 | sprintf(buff, "%s[type=%d]", desc, type); |
| 1423 | } |
| 1424 | |
| 1425 | return buff ? buff : ""; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1426 | } |
| 1427 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1428 | 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] | 1429 | { |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1430 | char *source, *dest = daemon->addrbuff; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1431 | char *verb = "is"; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1432 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1433 | if (!option_bool(OPT_LOG)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1434 | return; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1435 | |
| 1436 | if (addr) |
| 1437 | { |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1438 | if (flags & F_KEYTAG) |
| 1439 | sprintf(daemon->addrbuff, arg, addr->addr.keytag); |
| 1440 | else |
| 1441 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1442 | #ifdef HAVE_IPV6 |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1443 | inet_ntop(flags & F_IPV4 ? AF_INET : AF_INET6, |
| 1444 | addr, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1445 | #else |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1446 | strncpy(daemon->addrbuff, inet_ntoa(addr->addr.addr4), ADDRSTRLEN); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1447 | #endif |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1448 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1449 | } |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1450 | else |
| 1451 | dest = arg; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1452 | |
| 1453 | if (flags & F_REVERSE) |
| 1454 | { |
| 1455 | dest = name; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1456 | name = daemon->addrbuff; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1457 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1458 | |
| 1459 | if (flags & F_NEG) |
| 1460 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1461 | if (flags & F_NXDOMAIN) |
Simon Kelley | 40b695c | 2014-02-03 17:07:51 +0000 | [diff] [blame] | 1462 | dest = "NXDOMAIN"; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1463 | else |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1464 | { |
| 1465 | if (flags & F_IPV4) |
| 1466 | dest = "NODATA-IPv4"; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1467 | else if (flags & F_IPV6) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1468 | dest = "NODATA-IPv6"; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1469 | else |
| 1470 | dest = "NODATA"; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1471 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1472 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1473 | else if (flags & F_CNAME) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1474 | dest = "<CNAME>"; |
| 1475 | else if (flags & F_RRNAME) |
| 1476 | dest = arg; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1477 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1478 | if (flags & F_CONFIG) |
| 1479 | source = "config"; |
| 1480 | else if (flags & F_DHCP) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1481 | source = "DHCP"; |
| 1482 | else if (flags & F_HOSTS) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1483 | source = arg; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1484 | else if (flags & F_UPSTREAM) |
| 1485 | source = "reply"; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1486 | else if (flags & F_SECSTAT) |
| 1487 | source = "validation"; |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1488 | else if (flags & F_AUTH) |
| 1489 | source = "auth"; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1490 | else if (flags & F_SERVER) |
| 1491 | { |
| 1492 | source = "forwarded"; |
| 1493 | verb = "to"; |
| 1494 | } |
| 1495 | else if (flags & F_QUERY) |
| 1496 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1497 | source = arg; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1498 | verb = "from"; |
| 1499 | } |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 1500 | else if (flags & F_DNSSEC) |
| 1501 | { |
| 1502 | source = arg; |
| 1503 | verb = "to"; |
| 1504 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1505 | else |
| 1506 | source = "cached"; |
| 1507 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1508 | if (strlen(name) == 0) |
| 1509 | name = "."; |
| 1510 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1511 | my_syslog(LOG_INFO, "%s %s %s %s", source, name, verb, dest); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 1514 | |