xref: /freebsd/contrib/unbound/daemon/cachedump.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1 /*
2  * daemon/cachedump.c - dump the cache to text format.
3  *
4  * Copyright (c) 2008, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains functions to read and write the cache(s)
40  * to text format.
41  */
42 #include "config.h"
43 #include <openssl/ssl.h>
44 #include "daemon/cachedump.h"
45 #include "daemon/remote.h"
46 #include "daemon/worker.h"
47 #include "services/cache/rrset.h"
48 #include "services/cache/dns.h"
49 #include "services/cache/infra.h"
50 #include "services/outside_network.h"
51 #include "util/data/msgreply.h"
52 #include "util/regional.h"
53 #include "util/net_help.h"
54 #include "util/data/dname.h"
55 #include "util/config_file.h"
56 #include "iterator/iterator.h"
57 #include "iterator/iter_delegpt.h"
58 #include "iterator/iter_utils.h"
59 #include "iterator/iter_fwd.h"
60 #include "iterator/iter_hints.h"
61 #include "sldns/sbuffer.h"
62 #include "sldns/wire2str.h"
63 #include "sldns/str2wire.h"
64 
65 /** dump one rrset zonefile line */
66 static int
67 dump_rrset_line(RES* ssl, struct ub_packed_rrset_key* k, time_t now, size_t i)
68 {
69 	char s[65535];
70 	if(!packed_rr_to_string(k, i, now, s, sizeof(s))) {
71 		return ssl_printf(ssl, "BADRR\n");
72 	}
73 	return ssl_printf(ssl, "%s", s);
74 }
75 
76 /** dump rrset key and data info */
77 static int
78 dump_rrset(RES* ssl, struct ub_packed_rrset_key* k,
79 	struct packed_rrset_data* d, time_t now)
80 {
81 	size_t i;
82 	/* rd lock held by caller */
83 	if(!k || !d) return 1;
84 	if(k->id == 0) return 1; /* deleted */
85 	if(d->ttl < now) return 1; /* expired */
86 
87 	/* meta line */
88 	if(!ssl_printf(ssl, ";rrset%s " ARG_LL "d %u %u %d %d\n",
89 		(k->rk.flags & PACKED_RRSET_NSEC_AT_APEX)?" nsec_apex":"",
90 		(long long)(d->ttl - now),
91 		(unsigned)d->count, (unsigned)d->rrsig_count,
92 		(int)d->trust, (int)d->security
93 		))
94 		return 0;
95 	for(i=0; i<d->count + d->rrsig_count; i++) {
96 		if(!dump_rrset_line(ssl, k, now, i))
97 			return 0;
98 	}
99 	return 1;
100 }
101 
102 /** dump lruhash rrset cache */
103 static int
104 dump_rrset_lruhash(RES* ssl, struct lruhash* h, time_t now)
105 {
106 	struct lruhash_entry* e;
107 	/* lruhash already locked by caller */
108 	/* walk in order of lru; best first */
109 	for(e=h->lru_start; e; e = e->lru_next) {
110 		lock_rw_rdlock(&e->lock);
111 		if(!dump_rrset(ssl, (struct ub_packed_rrset_key*)e->key,
112 			(struct packed_rrset_data*)e->data, now)) {
113 			lock_rw_unlock(&e->lock);
114 			return 0;
115 		}
116 		lock_rw_unlock(&e->lock);
117 	}
118 	return 1;
119 }
120 
121 /** dump rrset cache */
122 static int
123 dump_rrset_cache(RES* ssl, struct worker* worker)
124 {
125 	struct rrset_cache* r = worker->env.rrset_cache;
126 	size_t slab;
127 	if(!ssl_printf(ssl, "START_RRSET_CACHE\n")) return 0;
128 	for(slab=0; slab<r->table.size; slab++) {
129 		lock_quick_lock(&r->table.array[slab]->lock);
130 		if(!dump_rrset_lruhash(ssl, r->table.array[slab],
131 			*worker->env.now)) {
132 			lock_quick_unlock(&r->table.array[slab]->lock);
133 			return 0;
134 		}
135 		lock_quick_unlock(&r->table.array[slab]->lock);
136 	}
137 	return ssl_printf(ssl, "END_RRSET_CACHE\n");
138 }
139 
140 /** dump message to rrset reference */
141 static int
142 dump_msg_ref(RES* ssl, struct ub_packed_rrset_key* k)
143 {
144 	char* nm, *tp, *cl;
145 	nm = sldns_wire2str_dname(k->rk.dname, k->rk.dname_len);
146 	tp = sldns_wire2str_type(ntohs(k->rk.type));
147 	cl = sldns_wire2str_class(ntohs(k->rk.rrset_class));
148 	if(!nm || !cl || !tp) {
149 		free(nm);
150 		free(tp);
151 		free(cl);
152 		return ssl_printf(ssl, "BADREF\n");
153 	}
154 	if(!ssl_printf(ssl, "%s %s %s %d\n", nm, cl, tp, (int)k->rk.flags)) {
155 		free(nm);
156 		free(tp);
157 		free(cl);
158 		return 0;
159 	}
160 	free(nm);
161 	free(tp);
162 	free(cl);
163 
164 	return 1;
165 }
166 
167 /** dump message entry */
168 static int
169 dump_msg(RES* ssl, struct query_info* k, struct reply_info* d,
170 	time_t now)
171 {
172 	size_t i;
173 	char* nm, *tp, *cl;
174 	if(!k || !d) return 1;
175 	if(d->ttl < now) return 1; /* expired */
176 
177 	nm = sldns_wire2str_dname(k->qname, k->qname_len);
178 	tp = sldns_wire2str_type(k->qtype);
179 	cl = sldns_wire2str_class(k->qclass);
180 	if(!nm || !tp || !cl) {
181 		free(nm);
182 		free(tp);
183 		free(cl);
184 		return 1; /* skip this entry */
185 	}
186 	if(!rrset_array_lock(d->ref, d->rrset_count, now)) {
187 		/* rrsets have timed out or do not exist */
188 		free(nm);
189 		free(tp);
190 		free(cl);
191 		return 1; /* skip this entry */
192 	}
193 
194 	/* meta line */
195 	if(!ssl_printf(ssl, "msg %s %s %s %d %d " ARG_LL "d %d %u %u %u\n",
196 			nm, cl, tp,
197 			(int)d->flags, (int)d->qdcount,
198 			(long long)(d->ttl-now), (int)d->security,
199 			(unsigned)d->an_numrrsets,
200 			(unsigned)d->ns_numrrsets,
201 			(unsigned)d->ar_numrrsets)) {
202 		free(nm);
203 		free(tp);
204 		free(cl);
205 		rrset_array_unlock(d->ref, d->rrset_count);
206 		return 0;
207 	}
208 	free(nm);
209 	free(tp);
210 	free(cl);
211 
212 	for(i=0; i<d->rrset_count; i++) {
213 		if(!dump_msg_ref(ssl, d->rrsets[i])) {
214 			rrset_array_unlock(d->ref, d->rrset_count);
215 			return 0;
216 		}
217 	}
218 	rrset_array_unlock(d->ref, d->rrset_count);
219 
220 	return 1;
221 }
222 
223 /** copy msg to worker pad */
224 static int
225 copy_msg(struct regional* region, struct lruhash_entry* e,
226 	struct query_info** k, struct reply_info** d)
227 {
228 	struct reply_info* rep = (struct reply_info*)e->data;
229 	if(rep->rrset_count > RR_COUNT_MAX)
230 		return 0; /* to protect against integer overflow */
231 	*d = (struct reply_info*)regional_alloc_init(region, e->data,
232 		sizeof(struct reply_info) +
233 		sizeof(struct rrset_ref) * (rep->rrset_count-1) +
234 		sizeof(struct ub_packed_rrset_key*) * rep->rrset_count);
235 	if(!*d)
236 		return 0;
237 	(*d)->rrsets = (struct ub_packed_rrset_key**)(void *)(
238 		(uint8_t*)(&((*d)->ref[0])) +
239 		sizeof(struct rrset_ref) * rep->rrset_count);
240 	*k = (struct query_info*)regional_alloc_init(region,
241 		e->key, sizeof(struct query_info));
242 	if(!*k)
243 		return 0;
244 	(*k)->qname = regional_alloc_init(region,
245 		(*k)->qname, (*k)->qname_len);
246 	return (*k)->qname != NULL;
247 }
248 
249 /** dump lruhash msg cache */
250 static int
251 dump_msg_lruhash(RES* ssl, struct worker* worker, struct lruhash* h)
252 {
253 	struct lruhash_entry* e;
254 	struct query_info* k;
255 	struct reply_info* d;
256 
257 	/* lruhash already locked by caller */
258 	/* walk in order of lru; best first */
259 	for(e=h->lru_start; e; e = e->lru_next) {
260 		regional_free_all(worker->scratchpad);
261 		lock_rw_rdlock(&e->lock);
262 		/* make copy of rrset in worker buffer */
263 		if(!copy_msg(worker->scratchpad, e, &k, &d)) {
264 			lock_rw_unlock(&e->lock);
265 			return 0;
266 		}
267 		lock_rw_unlock(&e->lock);
268 		/* release lock so we can lookup the rrset references
269 		 * in the rrset cache */
270 		if(!dump_msg(ssl, k, d, *worker->env.now)) {
271 			return 0;
272 		}
273 	}
274 	return 1;
275 }
276 
277 /** dump msg cache */
278 static int
279 dump_msg_cache(RES* ssl, struct worker* worker)
280 {
281 	struct slabhash* sh = worker->env.msg_cache;
282 	size_t slab;
283 	if(!ssl_printf(ssl, "START_MSG_CACHE\n")) return 0;
284 	for(slab=0; slab<sh->size; slab++) {
285 		lock_quick_lock(&sh->array[slab]->lock);
286 		if(!dump_msg_lruhash(ssl, worker, sh->array[slab])) {
287 			lock_quick_unlock(&sh->array[slab]->lock);
288 			return 0;
289 		}
290 		lock_quick_unlock(&sh->array[slab]->lock);
291 	}
292 	return ssl_printf(ssl, "END_MSG_CACHE\n");
293 }
294 
295 int
296 dump_cache(RES* ssl, struct worker* worker)
297 {
298 	if(!dump_rrset_cache(ssl, worker))
299 		return 0;
300 	if(!dump_msg_cache(ssl, worker))
301 		return 0;
302 	return ssl_printf(ssl, "EOF\n");
303 }
304 
305 /** read a line from ssl into buffer */
306 static int
307 ssl_read_buf(RES* ssl, sldns_buffer* buf)
308 {
309 	return ssl_read_line(ssl, (char*)sldns_buffer_begin(buf),
310 		sldns_buffer_capacity(buf));
311 }
312 
313 /** check fixed text on line */
314 static int
315 read_fixed(RES* ssl, sldns_buffer* buf, const char* str)
316 {
317 	if(!ssl_read_buf(ssl, buf)) return 0;
318 	return (strcmp((char*)sldns_buffer_begin(buf), str) == 0);
319 }
320 
321 /** load an RR into rrset */
322 static int
323 load_rr(RES* ssl, sldns_buffer* buf, struct regional* region,
324 	struct ub_packed_rrset_key* rk, struct packed_rrset_data* d,
325 	unsigned int i, int is_rrsig, int* go_on, time_t now)
326 {
327 	uint8_t rr[LDNS_RR_BUF_SIZE];
328 	size_t rr_len = sizeof(rr), dname_len = 0;
329 	int status;
330 
331 	/* read the line */
332 	if(!ssl_read_buf(ssl, buf))
333 		return 0;
334 	if(strncmp((char*)sldns_buffer_begin(buf), "BADRR\n", 6) == 0) {
335 		*go_on = 0;
336 		return 1;
337 	}
338 	status = sldns_str2wire_rr_buf((char*)sldns_buffer_begin(buf), rr,
339 		&rr_len, &dname_len, 3600, NULL, 0, NULL, 0);
340 	if(status != 0) {
341 		log_warn("error cannot parse rr: %s: %s",
342 			sldns_get_errorstr_parse(status),
343 			(char*)sldns_buffer_begin(buf));
344 		return 0;
345 	}
346 	if(is_rrsig && sldns_wirerr_get_type(rr, rr_len, dname_len)
347 		!= LDNS_RR_TYPE_RRSIG) {
348 		log_warn("error expected rrsig but got %s",
349 			(char*)sldns_buffer_begin(buf));
350 		return 0;
351 	}
352 
353 	/* convert ldns rr into packed_rr */
354 	d->rr_ttl[i] = (time_t)sldns_wirerr_get_ttl(rr, rr_len, dname_len) + now;
355 	sldns_buffer_clear(buf);
356 	d->rr_len[i] = sldns_wirerr_get_rdatalen(rr, rr_len, dname_len)+2;
357 	d->rr_data[i] = (uint8_t*)regional_alloc_init(region,
358 		sldns_wirerr_get_rdatawl(rr, rr_len, dname_len), d->rr_len[i]);
359 	if(!d->rr_data[i]) {
360 		log_warn("error out of memory");
361 		return 0;
362 	}
363 
364 	/* if first entry, fill the key structure */
365 	if(i==0) {
366 		rk->rk.type = htons(sldns_wirerr_get_type(rr, rr_len, dname_len));
367 		rk->rk.rrset_class = htons(sldns_wirerr_get_class(rr, rr_len, dname_len));
368 		rk->rk.dname_len = dname_len;
369 		rk->rk.dname = regional_alloc_init(region, rr, dname_len);
370 		if(!rk->rk.dname) {
371 			log_warn("error out of memory");
372 			return 0;
373 		}
374 	}
375 
376 	return 1;
377 }
378 
379 /** move entry into cache */
380 static int
381 move_into_cache(struct ub_packed_rrset_key* k,
382 	struct packed_rrset_data* d, struct worker* worker)
383 {
384 	struct ub_packed_rrset_key* ak;
385 	struct packed_rrset_data* ad;
386 	size_t s, i, num = d->count + d->rrsig_count;
387 	struct rrset_ref ref;
388 	uint8_t* p;
389 
390 	ak = alloc_special_obtain(worker->alloc);
391 	if(!ak) {
392 		log_warn("error out of memory");
393 		return 0;
394 	}
395 	ak->entry.data = NULL;
396 	ak->rk = k->rk;
397 	ak->entry.hash = rrset_key_hash(&k->rk);
398 	ak->rk.dname = (uint8_t*)memdup(k->rk.dname, k->rk.dname_len);
399 	if(!ak->rk.dname) {
400 		log_warn("error out of memory");
401 		ub_packed_rrset_parsedelete(ak, worker->alloc);
402 		return 0;
403 	}
404 	s = sizeof(*ad) + (sizeof(size_t) + sizeof(uint8_t*) +
405 		sizeof(time_t))* num;
406 	for(i=0; i<num; i++)
407 		s += d->rr_len[i];
408 	ad = (struct packed_rrset_data*)malloc(s);
409 	if(!ad) {
410 		log_warn("error out of memory");
411 		ub_packed_rrset_parsedelete(ak, worker->alloc);
412 		return 0;
413 	}
414 	p = (uint8_t*)ad;
415 	memmove(p, d, sizeof(*ad));
416 	p += sizeof(*ad);
417 	memmove(p, &d->rr_len[0], sizeof(size_t)*num);
418 	p += sizeof(size_t)*num;
419 	memmove(p, &d->rr_data[0], sizeof(uint8_t*)*num);
420 	p += sizeof(uint8_t*)*num;
421 	memmove(p, &d->rr_ttl[0], sizeof(time_t)*num);
422 	p += sizeof(time_t)*num;
423 	for(i=0; i<num; i++) {
424 		memmove(p, d->rr_data[i], d->rr_len[i]);
425 		p += d->rr_len[i];
426 	}
427 	packed_rrset_ptr_fixup(ad);
428 
429 	ak->entry.data = ad;
430 
431 	ref.key = ak;
432 	ref.id = ak->id;
433 	(void)rrset_cache_update(worker->env.rrset_cache, &ref,
434 		worker->alloc, *worker->env.now);
435 
436 	return 1;
437 }
438 
439 /** load an rrset entry */
440 static int
441 load_rrset(RES* ssl, sldns_buffer* buf, struct worker* worker)
442 {
443 	char* s = (char*)sldns_buffer_begin(buf);
444 	struct regional* region = worker->scratchpad;
445 	struct ub_packed_rrset_key* rk;
446 	struct packed_rrset_data* d;
447 	unsigned int rr_count, rrsig_count, trust, security;
448 	long long ttl;
449 	unsigned int i;
450 	int go_on = 1;
451 	regional_free_all(region);
452 
453 	rk = (struct ub_packed_rrset_key*)regional_alloc_zero(region,
454 		sizeof(*rk));
455 	d = (struct packed_rrset_data*)regional_alloc_zero(region, sizeof(*d));
456 	if(!rk || !d) {
457 		log_warn("error out of memory");
458 		return 0;
459 	}
460 
461 	if(strncmp(s, ";rrset", 6) != 0) {
462 		log_warn("error expected ';rrset' but got %s", s);
463 		return 0;
464 	}
465 	s += 6;
466 	if(strncmp(s, " nsec_apex", 10) == 0) {
467 		s += 10;
468 		rk->rk.flags |= PACKED_RRSET_NSEC_AT_APEX;
469 	}
470 	if(sscanf(s, " " ARG_LL "d %u %u %u %u", &ttl, &rr_count, &rrsig_count,
471 		&trust, &security) != 5) {
472 		log_warn("error bad rrset spec %s", s);
473 		return 0;
474 	}
475 	if(rr_count == 0 && rrsig_count == 0) {
476 		log_warn("bad rrset without contents");
477 		return 0;
478 	}
479 	if(rr_count > RR_COUNT_MAX || rrsig_count > RR_COUNT_MAX) {
480 		log_warn("bad rrset with too many rrs");
481 		return 0;
482 	}
483 	d->count = (size_t)rr_count;
484 	d->rrsig_count = (size_t)rrsig_count;
485 	d->security = (enum sec_status)security;
486 	d->trust = (enum rrset_trust)trust;
487 	d->ttl = (time_t)ttl + *worker->env.now;
488 
489 	d->rr_len = regional_alloc_zero(region,
490 		sizeof(size_t)*(d->count+d->rrsig_count));
491 	d->rr_ttl = regional_alloc_zero(region,
492 		sizeof(time_t)*(d->count+d->rrsig_count));
493 	d->rr_data = regional_alloc_zero(region,
494 		sizeof(uint8_t*)*(d->count+d->rrsig_count));
495 	if(!d->rr_len || !d->rr_ttl || !d->rr_data) {
496 		log_warn("error out of memory");
497 		return 0;
498 	}
499 
500 	/* read the rr's themselves */
501 	for(i=0; i<rr_count; i++) {
502 		if(!load_rr(ssl, buf, region, rk, d, i, 0,
503 			&go_on, *worker->env.now)) {
504 			log_warn("could not read rr %u", i);
505 			return 0;
506 		}
507 	}
508 	for(i=0; i<rrsig_count; i++) {
509 		if(!load_rr(ssl, buf, region, rk, d, i+rr_count, 1,
510 			&go_on, *worker->env.now)) {
511 			log_warn("could not read rrsig %u", i);
512 			return 0;
513 		}
514 	}
515 	if(!go_on) {
516 		/* skip this entry */
517 		return 1;
518 	}
519 
520 	return move_into_cache(rk, d, worker);
521 }
522 
523 /** load rrset cache */
524 static int
525 load_rrset_cache(RES* ssl, struct worker* worker)
526 {
527 	sldns_buffer* buf = worker->env.scratch_buffer;
528 	if(!read_fixed(ssl, buf, "START_RRSET_CACHE")) return 0;
529 	while(ssl_read_buf(ssl, buf) &&
530 		strcmp((char*)sldns_buffer_begin(buf), "END_RRSET_CACHE")!=0) {
531 		if(!load_rrset(ssl, buf, worker))
532 			return 0;
533 	}
534 	return 1;
535 }
536 
537 /** read qinfo from next three words */
538 static char*
539 load_qinfo(char* str, struct query_info* qinfo, struct regional* region)
540 {
541 	/* s is part of the buf */
542 	char* s = str;
543 	uint8_t rr[LDNS_RR_BUF_SIZE];
544 	size_t rr_len = sizeof(rr), dname_len = 0;
545 	int status;
546 
547 	/* skip three words */
548 	s = strchr(str, ' ');
549 	if(s) s = strchr(s+1, ' ');
550 	if(s) s = strchr(s+1, ' ');
551 	if(!s) {
552 		log_warn("error line too short, %s", str);
553 		return NULL;
554 	}
555 	s[0] = 0;
556 	s++;
557 
558 	/* parse them */
559 	status = sldns_str2wire_rr_question_buf(str, rr, &rr_len, &dname_len,
560 		NULL, 0, NULL, 0);
561 	if(status != 0) {
562 		log_warn("error cannot parse: %s %s",
563 			sldns_get_errorstr_parse(status), str);
564 		return NULL;
565 	}
566 	qinfo->qtype = sldns_wirerr_get_type(rr, rr_len, dname_len);
567 	qinfo->qclass = sldns_wirerr_get_class(rr, rr_len, dname_len);
568 	qinfo->qname_len = dname_len;
569 	qinfo->qname = (uint8_t*)regional_alloc_init(region, rr, dname_len);
570 	qinfo->local_alias = NULL;
571 	if(!qinfo->qname) {
572 		log_warn("error out of memory");
573 		return NULL;
574 	}
575 
576 	return s;
577 }
578 
579 /** load a msg rrset reference */
580 static int
581 load_ref(RES* ssl, sldns_buffer* buf, struct worker* worker,
582 	struct regional *region, struct ub_packed_rrset_key** rrset,
583 	int* go_on)
584 {
585 	char* s = (char*)sldns_buffer_begin(buf);
586 	struct query_info qinfo;
587 	unsigned int flags;
588 	struct ub_packed_rrset_key* k;
589 
590 	/* read line */
591 	if(!ssl_read_buf(ssl, buf))
592 		return 0;
593 	if(strncmp(s, "BADREF", 6) == 0) {
594 		*go_on = 0; /* its bad, skip it and skip message */
595 		return 1;
596 	}
597 
598 	s = load_qinfo(s, &qinfo, region);
599 	if(!s) {
600 		return 0;
601 	}
602 	if(sscanf(s, " %u", &flags) != 1) {
603 		log_warn("error cannot parse flags: %s", s);
604 		return 0;
605 	}
606 
607 	/* lookup in cache */
608 	k = rrset_cache_lookup(worker->env.rrset_cache, qinfo.qname,
609 		qinfo.qname_len, qinfo.qtype, qinfo.qclass,
610 		(uint32_t)flags, *worker->env.now, 0);
611 	if(!k) {
612 		/* not found or expired */
613 		*go_on = 0;
614 		return 1;
615 	}
616 
617 	/* store in result */
618 	*rrset = packed_rrset_copy_region(k, region, *worker->env.now);
619 	lock_rw_unlock(&k->entry.lock);
620 
621 	return (*rrset != NULL);
622 }
623 
624 /** load a msg entry */
625 static int
626 load_msg(RES* ssl, sldns_buffer* buf, struct worker* worker)
627 {
628 	struct regional* region = worker->scratchpad;
629 	struct query_info qinf;
630 	struct reply_info rep;
631 	char* s = (char*)sldns_buffer_begin(buf);
632 	unsigned int flags, qdcount, security, an, ns, ar;
633 	long long ttl;
634 	size_t i;
635 	int go_on = 1;
636 
637 	regional_free_all(region);
638 
639 	if(strncmp(s, "msg ", 4) != 0) {
640 		log_warn("error expected msg but got %s", s);
641 		return 0;
642 	}
643 	s += 4;
644 	s = load_qinfo(s, &qinf, region);
645 	if(!s) {
646 		return 0;
647 	}
648 
649 	/* read remainder of line */
650 	if(sscanf(s, " %u %u " ARG_LL "d %u %u %u %u", &flags, &qdcount, &ttl,
651 		&security, &an, &ns, &ar) != 7) {
652 		log_warn("error cannot parse numbers: %s", s);
653 		return 0;
654 	}
655 	rep.flags = (uint16_t)flags;
656 	rep.qdcount = (uint16_t)qdcount;
657 	rep.ttl = (time_t)ttl;
658 	rep.prefetch_ttl = PREFETCH_TTL_CALC(rep.ttl);
659 	rep.serve_expired_ttl = rep.ttl + SERVE_EXPIRED_TTL;
660 	rep.security = (enum sec_status)security;
661 	if(an > RR_COUNT_MAX || ns > RR_COUNT_MAX || ar > RR_COUNT_MAX) {
662 		log_warn("error too many rrsets");
663 		return 0; /* protect against integer overflow in alloc */
664 	}
665 	rep.an_numrrsets = (size_t)an;
666 	rep.ns_numrrsets = (size_t)ns;
667 	rep.ar_numrrsets = (size_t)ar;
668 	rep.rrset_count = (size_t)an+(size_t)ns+(size_t)ar;
669 	rep.rrsets = (struct ub_packed_rrset_key**)regional_alloc_zero(
670 		region, sizeof(struct ub_packed_rrset_key*)*rep.rrset_count);
671 
672 	/* fill repinfo with references */
673 	for(i=0; i<rep.rrset_count; i++) {
674 		if(!load_ref(ssl, buf, worker, region, &rep.rrsets[i],
675 			&go_on)) {
676 			return 0;
677 		}
678 	}
679 
680 	if(!go_on)
681 		return 1; /* skip this one, not all references satisfied */
682 
683 	if(!dns_cache_store(&worker->env, &qinf, &rep, 0, 0, 0, NULL, flags,
684 		*worker->env.now)) {
685 		log_warn("error out of memory");
686 		return 0;
687 	}
688 	return 1;
689 }
690 
691 /** load msg cache */
692 static int
693 load_msg_cache(RES* ssl, struct worker* worker)
694 {
695 	sldns_buffer* buf = worker->env.scratch_buffer;
696 	if(!read_fixed(ssl, buf, "START_MSG_CACHE")) return 0;
697 	while(ssl_read_buf(ssl, buf) &&
698 		strcmp((char*)sldns_buffer_begin(buf), "END_MSG_CACHE")!=0) {
699 		if(!load_msg(ssl, buf, worker))
700 			return 0;
701 	}
702 	return 1;
703 }
704 
705 int
706 load_cache(RES* ssl, struct worker* worker)
707 {
708 	if(!load_rrset_cache(ssl, worker))
709 		return 0;
710 	if(!load_msg_cache(ssl, worker))
711 		return 0;
712 	return read_fixed(ssl, worker->env.scratch_buffer, "EOF");
713 }
714 
715 /** print details on a delegation point */
716 static void
717 print_dp_details(RES* ssl, struct worker* worker, struct delegpt* dp)
718 {
719 	char buf[257];
720 	struct delegpt_addr* a;
721 	int lame, dlame, rlame, rto, edns_vs, to, delay,
722 		tA = 0, tAAAA = 0, tother = 0;
723 	long long entry_ttl;
724 	struct rtt_info ri;
725 	uint8_t edns_lame_known;
726 	for(a = dp->target_list; a; a = a->next_target) {
727 		addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf));
728 		if(!ssl_printf(ssl, "%-16s\t", buf))
729 			return;
730 		if(a->bogus) {
731 			if(!ssl_printf(ssl, "Address is BOGUS. "))
732 				return;
733 		}
734 		/* lookup in infra cache */
735 		delay=0;
736 		entry_ttl = infra_get_host_rto(worker->env.infra_cache,
737 			&a->addr, a->addrlen, dp->name, dp->namelen,
738 			&ri, &delay, *worker->env.now, &tA, &tAAAA, &tother);
739 		if(entry_ttl == -2 && ri.rto >= USEFUL_SERVER_TOP_TIMEOUT) {
740 			if(!ssl_printf(ssl, "expired, rto %d msec, tA %d "
741 				"tAAAA %d tother %d.\n", ri.rto, tA, tAAAA,
742 				tother))
743 				return;
744 			continue;
745 		}
746 		if(entry_ttl == -1 || entry_ttl == -2) {
747 			if(!ssl_printf(ssl, "not in infra cache.\n"))
748 				return;
749 			continue; /* skip stuff not in infra cache */
750 		}
751 
752 		/* uses type_A because most often looked up, but other
753 		 * lameness won't be reported then */
754 		if(!infra_get_lame_rtt(worker->env.infra_cache,
755 			&a->addr, a->addrlen, dp->name, dp->namelen,
756 			LDNS_RR_TYPE_A, &lame, &dlame, &rlame, &rto,
757 			*worker->env.now)) {
758 			if(!ssl_printf(ssl, "not in infra cache.\n"))
759 				return;
760 			continue; /* skip stuff not in infra cache */
761 		}
762 		if(!ssl_printf(ssl, "%s%s%s%srto %d msec, ttl " ARG_LL "d, "
763 			"ping %d var %d rtt %d, tA %d, tAAAA %d, tother %d",
764 			lame?"LAME ":"", dlame?"NoDNSSEC ":"",
765 			a->lame?"AddrWasParentSide ":"",
766 			rlame?"NoAuthButRecursive ":"", rto, entry_ttl,
767 			ri.srtt, ri.rttvar, rtt_notimeout(&ri),
768 			tA, tAAAA, tother))
769 			return;
770 		if(delay)
771 			if(!ssl_printf(ssl, ", probedelay %d", delay))
772 				return;
773 		if(infra_host(worker->env.infra_cache, &a->addr, a->addrlen,
774 			dp->name, dp->namelen, *worker->env.now, &edns_vs,
775 			&edns_lame_known, &to)) {
776 			if(edns_vs == -1) {
777 				if(!ssl_printf(ssl, ", noEDNS%s.",
778 					edns_lame_known?" probed":" assumed"))
779 					return;
780 			} else {
781 				if(!ssl_printf(ssl, ", EDNS %d%s.", edns_vs,
782 					edns_lame_known?" probed":" assumed"))
783 					return;
784 			}
785 		}
786 		if(!ssl_printf(ssl, "\n"))
787 			return;
788 	}
789 }
790 
791 /** print main dp info */
792 static void
793 print_dp_main(RES* ssl, struct delegpt* dp, struct dns_msg* msg)
794 {
795 	size_t i, n_ns, n_miss, n_addr, n_res, n_avail;
796 
797 	/* print the dp */
798 	if(msg)
799 	    for(i=0; i<msg->rep->rrset_count; i++) {
800 		struct ub_packed_rrset_key* k = msg->rep->rrsets[i];
801 		struct packed_rrset_data* d =
802 			(struct packed_rrset_data*)k->entry.data;
803 		if(d->security == sec_status_bogus) {
804 			if(!ssl_printf(ssl, "Address is BOGUS:\n"))
805 				return;
806 		}
807 		if(!dump_rrset(ssl, k, d, 0))
808 			return;
809 	    }
810 	delegpt_count_ns(dp, &n_ns, &n_miss);
811 	delegpt_count_addr(dp, &n_addr, &n_res, &n_avail);
812 	/* since dp has not been used by iterator, all are available*/
813 	if(!ssl_printf(ssl, "Delegation with %d names, of which %d "
814 		"can be examined to query further addresses.\n"
815 		"%sIt provides %d IP addresses.\n",
816 		(int)n_ns, (int)n_miss, (dp->bogus?"It is BOGUS. ":""),
817 		(int)n_addr))
818 		return;
819 }
820 
821 int print_deleg_lookup(RES* ssl, struct worker* worker, uint8_t* nm,
822 	size_t nmlen, int ATTR_UNUSED(nmlabs))
823 {
824 	/* deep links into the iterator module */
825 	struct delegpt* dp;
826 	struct dns_msg* msg;
827 	struct regional* region = worker->scratchpad;
828 	char b[260];
829 	struct query_info qinfo;
830 	struct iter_hints_stub* stub;
831 	regional_free_all(region);
832 	qinfo.qname = nm;
833 	qinfo.qname_len = nmlen;
834 	qinfo.qtype = LDNS_RR_TYPE_A;
835 	qinfo.qclass = LDNS_RR_CLASS_IN;
836 	qinfo.local_alias = NULL;
837 
838 	dname_str(nm, b);
839 	if(!ssl_printf(ssl, "The following name servers are used for lookup "
840 		"of %s\n", b))
841 		return 0;
842 
843 	dp = forwards_lookup(worker->env.fwds, nm, qinfo.qclass);
844 	if(dp) {
845 		if(!ssl_printf(ssl, "forwarding request:\n"))
846 			return 0;
847 		print_dp_main(ssl, dp, NULL);
848 		print_dp_details(ssl, worker, dp);
849 		return 1;
850 	}
851 
852 	while(1) {
853 		dp = dns_cache_find_delegation(&worker->env, nm, nmlen,
854 			qinfo.qtype, qinfo.qclass, region, &msg,
855 			*worker->env.now, 0, NULL, 0);
856 		if(!dp) {
857 			return ssl_printf(ssl, "no delegation from "
858 				"cache; goes to configured roots\n");
859 		}
860 		/* go up? */
861 		if(iter_dp_is_useless(&qinfo, BIT_RD, dp,
862 			(worker->env.cfg->do_ip4 && worker->back->num_ip4 != 0),
863 			(worker->env.cfg->do_ip6 && worker->back->num_ip6 != 0))) {
864 			print_dp_main(ssl, dp, msg);
865 			print_dp_details(ssl, worker, dp);
866 			if(!ssl_printf(ssl, "cache delegation was "
867 				"useless (no IP addresses)\n"))
868 				return 0;
869 			if(dname_is_root(nm)) {
870 				/* goes to root config */
871 				return ssl_printf(ssl, "no delegation from "
872 					"cache; goes to configured roots\n");
873 			} else {
874 				/* useless, goes up */
875 				nm = dp->name;
876 				nmlen = dp->namelen;
877 				dname_remove_label(&nm, &nmlen);
878 				dname_str(nm, b);
879 				if(!ssl_printf(ssl, "going up, lookup %s\n", b))
880 					return 0;
881 				continue;
882 			}
883 		}
884 		stub = hints_lookup_stub(worker->env.hints, nm, qinfo.qclass,
885 			dp);
886 		if(stub) {
887 			if(stub->noprime) {
888 				if(!ssl_printf(ssl, "The noprime stub servers "
889 					"are used:\n"))
890 					return 0;
891 			} else {
892 				if(!ssl_printf(ssl, "The stub is primed "
893 						"with servers:\n"))
894 					return 0;
895 			}
896 			print_dp_main(ssl, stub->dp, NULL);
897 			print_dp_details(ssl, worker, stub->dp);
898 		} else {
899 			print_dp_main(ssl, dp, msg);
900 			print_dp_details(ssl, worker, dp);
901 		}
902 		break;
903 	}
904 
905 	return 1;
906 }
907