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