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