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