xref: /openbsd-src/usr.sbin/unbound/validator/autotrust.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*
2  * validator/autotrust.c - RFC5011 trust anchor management for unbound.
3  *
4  * Copyright (c) 2009, 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 LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * Contains autotrust implementation. The implementation was taken from
40  * the autotrust daemon (BSD licensed), written by Matthijs Mekking.
41  * It was modified to fit into unbound. The state table process is the same.
42  */
43 #include "config.h"
44 #include <ldns/ldns.h>
45 #include "validator/autotrust.h"
46 #include "validator/val_anchor.h"
47 #include "validator/val_utils.h"
48 #include "validator/val_sigcrypt.h"
49 #include "util/data/dname.h"
50 #include "util/data/packed_rrset.h"
51 #include "util/log.h"
52 #include "util/module.h"
53 #include "util/net_help.h"
54 #include "util/config_file.h"
55 #include "util/regional.h"
56 #include "util/random.h"
57 #include "util/data/msgparse.h"
58 #include "services/mesh.h"
59 #include "services/cache/rrset.h"
60 #include "validator/val_kcache.h"
61 
62 /** number of times a key must be seen before it can become valid */
63 #define MIN_PENDINGCOUNT 2
64 
65 /** Event: Revoked */
66 static void do_revoked(struct module_env* env, struct autr_ta* anchor, int* c);
67 
68 struct autr_global_data* autr_global_create(void)
69 {
70 	struct autr_global_data* global;
71 	global = (struct autr_global_data*)malloc(sizeof(*global));
72 	if(!global)
73 		return NULL;
74 	rbtree_init(&global->probe, &probetree_cmp);
75 	return global;
76 }
77 
78 void autr_global_delete(struct autr_global_data* global)
79 {
80 	if(!global)
81 		return;
82 	/* elements deleted by parent */
83 	memset(global, 0, sizeof(*global));
84 	free(global);
85 }
86 
87 int probetree_cmp(const void* x, const void* y)
88 {
89 	struct trust_anchor* a = (struct trust_anchor*)x;
90 	struct trust_anchor* b = (struct trust_anchor*)y;
91 	log_assert(a->autr && b->autr);
92 	if(a->autr->next_probe_time < b->autr->next_probe_time)
93 		return -1;
94 	if(a->autr->next_probe_time > b->autr->next_probe_time)
95 		return 1;
96 	/* time is equal, sort on trust point identity */
97 	return anchor_cmp(x, y);
98 }
99 
100 size_t
101 autr_get_num_anchors(struct val_anchors* anchors)
102 {
103 	size_t res = 0;
104 	if(!anchors)
105 		return 0;
106 	lock_basic_lock(&anchors->lock);
107 	if(anchors->autr)
108 		res = anchors->autr->probe.count;
109 	lock_basic_unlock(&anchors->lock);
110 	return res;
111 }
112 
113 /** Position in string */
114 static int
115 position_in_string(char *str, const char* sub)
116 {
117 	char* pos = strstr(str, sub);
118 	if(pos)
119 		return (int)(pos-str)+(int)strlen(sub);
120 	return -1;
121 }
122 
123 /** Debug routine to print pretty key information */
124 static void
125 verbose_key(struct autr_ta* ta, enum verbosity_value level,
126 	const char* format, ...) ATTR_FORMAT(printf, 3, 4);
127 
128 /**
129  * Implementation of debug pretty key print
130  * @param ta: trust anchor key with DNSKEY data.
131  * @param level: verbosity level to print at.
132  * @param format: printf style format string.
133  */
134 static void
135 verbose_key(struct autr_ta* ta, enum verbosity_value level,
136 	const char* format, ...)
137 {
138 	va_list args;
139 	va_start(args, format);
140 	if(verbosity >= level) {
141 		char* str = ldns_rdf2str(ldns_rr_owner(ta->rr));
142 		int keytag = (int)ldns_calc_keytag(ta->rr);
143 		char msg[MAXSYSLOGMSGLEN];
144 		vsnprintf(msg, sizeof(msg), format, args);
145 		verbose(level, "%s key %d %s", str?str:"??", keytag, msg);
146 		free(str);
147 	}
148 	va_end(args);
149 }
150 
151 /**
152  * Parse comments
153  * @param str: to parse
154  * @param ta: trust key autotrust metadata
155  * @return false on failure.
156  */
157 static int
158 parse_comments(char* str, struct autr_ta* ta)
159 {
160         int len = (int)strlen(str), pos = 0, timestamp = 0;
161         char* comment = (char*) malloc(sizeof(char)*len+1);
162         char* comments = comment;
163 	if(!comment) {
164 		log_err("malloc failure in parse");
165                 return 0;
166 	}
167 	/* skip over whitespace and data at start of line */
168         while (*str != '\0' && *str != ';')
169                 str++;
170         if (*str == ';')
171                 str++;
172         /* copy comments */
173         while (*str != '\0')
174         {
175                 *comments = *str;
176                 comments++;
177                 str++;
178         }
179         *comments = '\0';
180 
181         comments = comment;
182 
183         /* read state */
184         pos = position_in_string(comments, "state=");
185         if (pos >= (int) strlen(comments))
186         {
187 		log_err("parse error");
188                 free(comment);
189                 return 0;
190         }
191         if (pos <= 0)
192                 ta->s = AUTR_STATE_VALID;
193         else
194         {
195                 int s = (int) comments[pos] - '0';
196                 switch(s)
197                 {
198                         case AUTR_STATE_START:
199                         case AUTR_STATE_ADDPEND:
200                         case AUTR_STATE_VALID:
201                         case AUTR_STATE_MISSING:
202                         case AUTR_STATE_REVOKED:
203                         case AUTR_STATE_REMOVED:
204                                 ta->s = s;
205                                 break;
206                         default:
207 				verbose_key(ta, VERB_OPS, "has undefined "
208 					"state, considered NewKey");
209                                 ta->s = AUTR_STATE_START;
210                                 break;
211                 }
212         }
213         /* read pending count */
214         pos = position_in_string(comments, "count=");
215         if (pos >= (int) strlen(comments))
216         {
217 		log_err("parse error");
218                 free(comment);
219                 return 0;
220         }
221         if (pos <= 0)
222                 ta->pending_count = 0;
223         else
224         {
225                 comments += pos;
226                 ta->pending_count = (uint8_t)atoi(comments);
227         }
228 
229         /* read last change */
230         pos = position_in_string(comments, "lastchange=");
231         if (pos >= (int) strlen(comments))
232         {
233 		log_err("parse error");
234                 free(comment);
235                 return 0;
236         }
237         if (pos >= 0)
238         {
239                 comments += pos;
240                 timestamp = atoi(comments);
241         }
242         if (pos < 0 || !timestamp)
243 		ta->last_change = 0;
244         else
245                 ta->last_change = (uint32_t)timestamp;
246 
247         free(comment);
248         return 1;
249 }
250 
251 /** Check if a line contains data (besides comments) */
252 static int
253 str_contains_data(char* str, char comment)
254 {
255         while (*str != '\0') {
256                 if (*str == comment || *str == '\n')
257                         return 0;
258                 if (*str != ' ' && *str != '\t')
259                         return 1;
260                 str++;
261         }
262         return 0;
263 }
264 
265 /** Get DNSKEY flags */
266 static int
267 dnskey_flags(ldns_rr* rr)
268 {
269 	if(ldns_rr_get_type(rr) != LDNS_RR_TYPE_DNSKEY)
270 		return 0;
271 	return (int)ldns_read_uint16(ldns_rdf_data(ldns_rr_dnskey_flags(rr)));
272 }
273 
274 
275 /** Check if KSK DNSKEY */
276 static int
277 rr_is_dnskey_sep(ldns_rr* rr)
278 {
279 	return (dnskey_flags(rr)&DNSKEY_BIT_SEP);
280 }
281 
282 /** Check if REVOKED DNSKEY */
283 static int
284 rr_is_dnskey_revoked(ldns_rr* rr)
285 {
286 	return (dnskey_flags(rr)&LDNS_KEY_REVOKE_KEY);
287 }
288 
289 /** create ta */
290 static struct autr_ta*
291 autr_ta_create(ldns_rr* rr)
292 {
293 	struct autr_ta* ta = (struct autr_ta*)calloc(1, sizeof(*ta));
294 	if(!ta) {
295 		ldns_rr_free(rr);
296 		return NULL;
297 	}
298 	ta->rr = rr;
299 	return ta;
300 }
301 
302 /** create tp */
303 static struct trust_anchor*
304 autr_tp_create(struct val_anchors* anchors, ldns_rdf* own, uint16_t dc)
305 {
306 	struct trust_anchor* tp = (struct trust_anchor*)calloc(1, sizeof(*tp));
307 	if(!tp) return NULL;
308 	tp->name = memdup(ldns_rdf_data(own), ldns_rdf_size(own));
309 	if(!tp->name) {
310 		free(tp);
311 		return NULL;
312 	}
313 	tp->namelen = ldns_rdf_size(own);
314 	tp->namelabs = dname_count_labels(tp->name);
315 	tp->node.key = tp;
316 	tp->dclass = dc;
317 	tp->autr = (struct autr_point_data*)calloc(1, sizeof(*tp->autr));
318 	if(!tp->autr) {
319 		free(tp->name);
320 		free(tp);
321 		return NULL;
322 	}
323 	tp->autr->pnode.key = tp;
324 
325 	lock_basic_lock(&anchors->lock);
326 	if(!rbtree_insert(anchors->tree, &tp->node)) {
327 		lock_basic_unlock(&anchors->lock);
328 		log_err("trust anchor presented twice");
329 		free(tp->name);
330 		free(tp->autr);
331 		free(tp);
332 		return NULL;
333 	}
334 	if(!rbtree_insert(&anchors->autr->probe, &tp->autr->pnode)) {
335 		(void)rbtree_delete(anchors->tree, tp);
336 		lock_basic_unlock(&anchors->lock);
337 		log_err("trust anchor in probetree twice");
338 		free(tp->name);
339 		free(tp->autr);
340 		free(tp);
341 		return NULL;
342 	}
343 	lock_basic_unlock(&anchors->lock);
344 	lock_basic_init(&tp->lock);
345 	lock_protect(&tp->lock, tp, sizeof(*tp));
346 	lock_protect(&tp->lock, tp->autr, sizeof(*tp->autr));
347 	return tp;
348 }
349 
350 /** delete assembled rrsets */
351 static void
352 autr_rrset_delete(struct ub_packed_rrset_key* r)
353 {
354 	if(r) {
355 		free(r->rk.dname);
356 		free(r->entry.data);
357 		free(r);
358 	}
359 }
360 
361 void autr_point_delete(struct trust_anchor* tp)
362 {
363 	if(!tp)
364 		return;
365 	lock_unprotect(&tp->lock, tp);
366 	lock_unprotect(&tp->lock, tp->autr);
367 	lock_basic_destroy(&tp->lock);
368 	autr_rrset_delete(tp->ds_rrset);
369 	autr_rrset_delete(tp->dnskey_rrset);
370 	if(tp->autr) {
371 		struct autr_ta* p = tp->autr->keys, *np;
372 		while(p) {
373 			np = p->next;
374 			ldns_rr_free(p->rr);
375 			free(p);
376 			p = np;
377 		}
378 		free(tp->autr->file);
379 		free(tp->autr);
380 	}
381 	free(tp->name);
382 	free(tp);
383 }
384 
385 /** find or add a new trust point for autotrust */
386 static struct trust_anchor*
387 find_add_tp(struct val_anchors* anchors, ldns_rr* rr)
388 {
389 	struct trust_anchor* tp;
390 	ldns_rdf* own = ldns_rr_owner(rr);
391 	tp = anchor_find(anchors, ldns_rdf_data(own),
392 		dname_count_labels(ldns_rdf_data(own)),
393 		ldns_rdf_size(own), ldns_rr_get_class(rr));
394 	if(tp) {
395 		if(!tp->autr) {
396 			log_err("anchor cannot be with and without autotrust");
397 			lock_basic_unlock(&tp->lock);
398 			return NULL;
399 		}
400 		return tp;
401 	}
402 	tp = autr_tp_create(anchors, ldns_rr_owner(rr), ldns_rr_get_class(rr));
403 	lock_basic_lock(&tp->lock);
404 	return tp;
405 }
406 
407 /** Add trust anchor from RR */
408 static struct autr_ta*
409 add_trustanchor_frm_rr(struct val_anchors* anchors, ldns_rr* rr,
410 	struct trust_anchor** tp)
411 {
412 	struct autr_ta* ta = autr_ta_create(rr);
413 	if(!ta)
414 		return NULL;
415 	*tp = find_add_tp(anchors, rr);
416 	if(!*tp) {
417 		ldns_rr_free(ta->rr);
418 		free(ta);
419 		return NULL;
420 	}
421 	/* add ta to tp */
422 	ta->next = (*tp)->autr->keys;
423 	(*tp)->autr->keys = ta;
424 	lock_basic_unlock(&(*tp)->lock);
425 	return ta;
426 }
427 
428 /**
429  * Add new trust anchor from a string in file.
430  * @param anchors: all anchors
431  * @param str: string with anchor and comments, if any comments.
432  * @param tp: trust point returned.
433  * @param origin: what to use for @
434  * @param prev: previous rr name
435  * @param skip: if true, the result is NULL, but not an error, skip it.
436  * @return new key in trust point.
437  */
438 static struct autr_ta*
439 add_trustanchor_frm_str(struct val_anchors* anchors, char* str,
440 	struct trust_anchor** tp, ldns_rdf* origin, ldns_rdf** prev, int* skip)
441 {
442         ldns_rr* rr;
443 	ldns_status lstatus;
444         if (!str_contains_data(str, ';')) {
445 		*skip = 1;
446                 return NULL; /* empty line */
447 	}
448         if (LDNS_STATUS_OK !=
449                 (lstatus = ldns_rr_new_frm_str(&rr, str, 0, origin, prev)))
450         {
451         	log_err("ldns error while converting string to RR: %s",
452 			ldns_get_errorstr_by_id(lstatus));
453                 return NULL;
454         }
455 	if(ldns_rr_get_type(rr) != LDNS_RR_TYPE_DNSKEY &&
456 		ldns_rr_get_type(rr) != LDNS_RR_TYPE_DS) {
457 		ldns_rr_free(rr);
458 		*skip = 1;
459 		return NULL; /* only DS and DNSKEY allowed */
460 	}
461         return add_trustanchor_frm_rr(anchors, rr, tp);
462 }
463 
464 /**
465  * Load single anchor
466  * @param anchors: all points.
467  * @param str: comments line
468  * @param fname: filename
469  * @param origin: $ORIGIN.
470  * @param prev: passed to ldns.
471  * @param skip: if true, the result is NULL, but not an error, skip it.
472  * @return false on failure, otherwise the tp read.
473  */
474 static struct trust_anchor*
475 load_trustanchor(struct val_anchors* anchors, char* str, const char* fname,
476 	ldns_rdf* origin, ldns_rdf** prev, int* skip)
477 {
478         struct autr_ta* ta = NULL;
479         struct trust_anchor* tp = NULL;
480 
481         ta = add_trustanchor_frm_str(anchors, str, &tp, origin, prev, skip);
482 	if(!ta)
483 		return NULL;
484 	lock_basic_lock(&tp->lock);
485 	if(!parse_comments(str, ta)) {
486 		lock_basic_unlock(&tp->lock);
487 		return NULL;
488 	}
489 	if(!tp->autr->file) {
490 		tp->autr->file = strdup(fname);
491 		if(!tp->autr->file) {
492 			lock_basic_unlock(&tp->lock);
493 			log_err("malloc failure");
494 			return NULL;
495 		}
496 	}
497 	lock_basic_unlock(&tp->lock);
498         return tp;
499 }
500 
501 /**
502  * Assemble the trust anchors into DS and DNSKEY packed rrsets.
503  * Uses only VALID and MISSING DNSKEYs.
504  * Read the ldns_rrs and builds packed rrsets
505  * @param tp: the trust point. Must be locked.
506  * @return false on malloc failure.
507  */
508 static int
509 autr_assemble(struct trust_anchor* tp)
510 {
511 	ldns_rr_list* ds, *dnskey;
512 	struct autr_ta* ta;
513 	struct ub_packed_rrset_key* ubds=NULL, *ubdnskey=NULL;
514 
515 	ds = ldns_rr_list_new();
516 	dnskey = ldns_rr_list_new();
517 	if(!ds || !dnskey) {
518 		ldns_rr_list_free(ds);
519 		ldns_rr_list_free(dnskey);
520 		return 0;
521 	}
522 	for(ta = tp->autr->keys; ta; ta = ta->next) {
523 		if(ldns_rr_get_type(ta->rr) == LDNS_RR_TYPE_DS) {
524 			if(!ldns_rr_list_push_rr(ds, ta->rr)) {
525 				ldns_rr_list_free(ds);
526 				ldns_rr_list_free(dnskey);
527 				return 0;
528 			}
529 		} else if(ta->s == AUTR_STATE_VALID ||
530 			ta->s == AUTR_STATE_MISSING) {
531 			if(!ldns_rr_list_push_rr(dnskey, ta->rr)) {
532 				ldns_rr_list_free(ds);
533 				ldns_rr_list_free(dnskey);
534 				return 0;
535 			}
536 		}
537 	}
538 
539 	/* make packed rrset keys - malloced with no ID number, they
540 	 * are not in the cache */
541 	/* make packed rrset data (if there is a key) */
542 
543 	if(ldns_rr_list_rr_count(ds) > 0) {
544 		ubds = ub_packed_rrset_heap_key(ds);
545 		if(!ubds)
546 			goto error_cleanup;
547 		ubds->entry.data = packed_rrset_heap_data(ds);
548 		if(!ubds->entry.data)
549 			goto error_cleanup;
550 	}
551 	if(ldns_rr_list_rr_count(dnskey) > 0) {
552 		ubdnskey = ub_packed_rrset_heap_key(dnskey);
553 		if(!ubdnskey)
554 			goto error_cleanup;
555 		ubdnskey->entry.data = packed_rrset_heap_data(dnskey);
556 		if(!ubdnskey->entry.data) {
557 		error_cleanup:
558 			autr_rrset_delete(ubds);
559 			autr_rrset_delete(ubdnskey);
560 			ldns_rr_list_free(ds);
561 			ldns_rr_list_free(dnskey);
562 			return 0;
563 		}
564 	}
565 	/* we have prepared the new keys so nothing can go wrong any more.
566 	 * And we are sure we cannot be left without trustanchor after
567 	 * any errors. Put in the new keys and remove old ones. */
568 
569 	/* free the old data */
570 	autr_rrset_delete(tp->ds_rrset);
571 	autr_rrset_delete(tp->dnskey_rrset);
572 
573 	/* assign the data to replace the old */
574 	tp->ds_rrset = ubds;
575 	tp->dnskey_rrset = ubdnskey;
576 	tp->numDS = ldns_rr_list_rr_count(ds);
577 	tp->numDNSKEY = ldns_rr_list_rr_count(dnskey);
578 
579 	ldns_rr_list_free(ds);
580 	ldns_rr_list_free(dnskey);
581 	return 1;
582 }
583 
584 /** parse integer */
585 static unsigned int
586 parse_int(char* line, int* ret)
587 {
588 	char *e;
589 	unsigned int x = (unsigned int)strtol(line, &e, 10);
590 	if(line == e) {
591 		*ret = -1; /* parse error */
592 		return 0;
593 	}
594 	*ret = 1; /* matched */
595 	return x;
596 }
597 
598 /** parse id sequence for anchor */
599 static struct trust_anchor*
600 parse_id(struct val_anchors* anchors, char* line)
601 {
602 	struct trust_anchor *tp;
603 	int r;
604 	ldns_rdf* rdf;
605 	uint16_t dclass;
606 	/* read the owner name */
607 	char* next = strchr(line, ' ');
608 	if(!next)
609 		return NULL;
610 	next[0] = 0;
611 	rdf = ldns_dname_new_frm_str(line);
612 	if(!rdf)
613 		return NULL;
614 
615 	/* read the class */
616 	dclass = parse_int(next+1, &r);
617 	if(r == -1) {
618 		ldns_rdf_deep_free(rdf);
619 		return NULL;
620 	}
621 
622 	/* find the trust point */
623 	tp = autr_tp_create(anchors, rdf, dclass);
624 	ldns_rdf_deep_free(rdf);
625 	return tp;
626 }
627 
628 /**
629  * Parse variable from trustanchor header
630  * @param line: to parse
631  * @param anchors: the anchor is added to this, if "id:" is seen.
632  * @param anchor: the anchor as result value or previously returned anchor
633  * 	value to read the variable lines into.
634  * @return: 0 no match, -1 failed syntax error, +1 success line read.
635  * 	+2 revoked trust anchor file.
636  */
637 static int
638 parse_var_line(char* line, struct val_anchors* anchors,
639 	struct trust_anchor** anchor)
640 {
641 	struct trust_anchor* tp = *anchor;
642 	int r = 0;
643 	if(strncmp(line, ";;id: ", 6) == 0) {
644 		*anchor = parse_id(anchors, line+6);
645 		if(!*anchor) return -1;
646 		else return 1;
647 	} else if(strncmp(line, ";;REVOKED", 9) == 0) {
648 		if(tp) {
649 			log_err("REVOKED statement must be at start of file");
650 			return -1;
651 		}
652 		return 2;
653 	} else if(strncmp(line, ";;last_queried: ", 16) == 0) {
654 		if(!tp) return -1;
655 		lock_basic_lock(&tp->lock);
656 		tp->autr->last_queried = (time_t)parse_int(line+16, &r);
657 		lock_basic_unlock(&tp->lock);
658 	} else if(strncmp(line, ";;last_success: ", 16) == 0) {
659 		if(!tp) return -1;
660 		lock_basic_lock(&tp->lock);
661 		tp->autr->last_success = (time_t)parse_int(line+16, &r);
662 		lock_basic_unlock(&tp->lock);
663 	} else if(strncmp(line, ";;next_probe_time: ", 19) == 0) {
664 		if(!tp) return -1;
665 		lock_basic_lock(&anchors->lock);
666 		lock_basic_lock(&tp->lock);
667 		(void)rbtree_delete(&anchors->autr->probe, tp);
668 		tp->autr->next_probe_time = (time_t)parse_int(line+19, &r);
669 		(void)rbtree_insert(&anchors->autr->probe, &tp->autr->pnode);
670 		lock_basic_unlock(&tp->lock);
671 		lock_basic_unlock(&anchors->lock);
672 	} else if(strncmp(line, ";;query_failed: ", 16) == 0) {
673 		if(!tp) return -1;
674 		lock_basic_lock(&tp->lock);
675 		tp->autr->query_failed = (uint8_t)parse_int(line+16, &r);
676 		lock_basic_unlock(&tp->lock);
677 	} else if(strncmp(line, ";;query_interval: ", 18) == 0) {
678 		if(!tp) return -1;
679 		lock_basic_lock(&tp->lock);
680 		tp->autr->query_interval = (uint32_t)parse_int(line+18, &r);
681 		lock_basic_unlock(&tp->lock);
682 	} else if(strncmp(line, ";;retry_time: ", 14) == 0) {
683 		if(!tp) return -1;
684 		lock_basic_lock(&tp->lock);
685 		tp->autr->retry_time = (uint32_t)parse_int(line+14, &r);
686 		lock_basic_unlock(&tp->lock);
687 	}
688 	return r;
689 }
690 
691 /** handle origin lines */
692 static int
693 handle_origin(char* line, ldns_rdf** origin)
694 {
695 	while(isspace((int)*line))
696 		line++;
697 	if(strncmp(line, "$ORIGIN", 7) != 0)
698 		return 0;
699 	ldns_rdf_deep_free(*origin);
700 	line += 7;
701 	while(isspace((int)*line))
702 		line++;
703 	*origin = ldns_dname_new_frm_str(line);
704 	if(!*origin)
705 		log_warn("malloc failure or parse error in $ORIGIN");
706 	return 1;
707 }
708 
709 /** Read one line and put multiline RRs onto one line string */
710 static int
711 read_multiline(char* buf, size_t len, FILE* in, int* linenr)
712 {
713 	char* pos = buf;
714 	size_t left = len;
715 	int depth = 0;
716 	buf[len-1] = 0;
717 	while(left > 0 && fgets(pos, (int)left, in) != NULL) {
718 		size_t i, poslen = strlen(pos);
719 		(*linenr)++;
720 
721 		/* check what the new depth is after the line */
722 		/* this routine cannot handle braces inside quotes,
723 		   say for TXT records, but this routine only has to read keys */
724 		for(i=0; i<poslen; i++) {
725 			if(pos[i] == '(') {
726 				depth++;
727 			} else if(pos[i] == ')') {
728 				if(depth == 0) {
729 					log_err("mismatch: too many ')'");
730 					return -1;
731 				}
732 				depth--;
733 			} else if(pos[i] == ';') {
734 				break;
735 			}
736 		}
737 
738 		/* normal oneline or last line: keeps newline and comments */
739 		if(depth == 0) {
740 			return 1;
741 		}
742 
743 		/* more lines expected, snip off comments and newline */
744 		if(poslen>0)
745 			pos[poslen-1] = 0; /* strip newline */
746 		if(strchr(pos, ';'))
747 			strchr(pos, ';')[0] = 0; /* strip comments */
748 
749 		/* move to paste other lines behind this one */
750 		poslen = strlen(pos);
751 		pos += poslen;
752 		left -= poslen;
753 		/* the newline is changed into a space */
754 		if(left <= 2 /* space and eos */) {
755 			log_err("line too long");
756 			return -1;
757 		}
758 		pos[0] = ' ';
759 		pos[1] = 0;
760 		pos += 1;
761 		left -= 1;
762 	}
763 	if(depth != 0) {
764 		log_err("mismatch: too many '('");
765 		return -1;
766 	}
767 	if(pos != buf)
768 		return 1;
769 	return 0;
770 }
771 
772 int autr_read_file(struct val_anchors* anchors, const char* nm)
773 {
774         /* the file descriptor */
775         FILE* fd;
776         /* keep track of line numbers */
777         int line_nr = 0;
778         /* single line */
779         char line[10240];
780 	/* trust point being read */
781 	struct trust_anchor *tp = NULL, *tp2;
782 	int r;
783 	/* for $ORIGIN parsing */
784 	ldns_rdf *origin=NULL, *prev=NULL;
785 
786         if (!(fd = fopen(nm, "r"))) {
787                 log_err("unable to open %s for reading: %s",
788 			nm, strerror(errno));
789                 return 0;
790         }
791         verbose(VERB_ALGO, "reading autotrust anchor file %s", nm);
792         while ( (r=read_multiline(line, sizeof(line), fd, &line_nr)) != 0) {
793 		if(r == -1 || (r = parse_var_line(line, anchors, &tp)) == -1) {
794 			log_err("could not parse auto-trust-anchor-file "
795 				"%s line %d", nm, line_nr);
796 			fclose(fd);
797 			ldns_rdf_deep_free(origin);
798 			ldns_rdf_deep_free(prev);
799 			return 0;
800 		} else if(r == 1) {
801 			continue;
802 		} else if(r == 2) {
803 			log_warn("trust anchor %s has been revoked", nm);
804 			fclose(fd);
805 			ldns_rdf_deep_free(origin);
806 			ldns_rdf_deep_free(prev);
807 			return 1;
808 		}
809         	if (!str_contains_data(line, ';'))
810                 	continue; /* empty lines allowed */
811  		if(handle_origin(line, &origin))
812 			continue;
813 		r = 0;
814                 if(!(tp2=load_trustanchor(anchors, line, nm, origin, &prev,
815 			&r))) {
816 			if(!r) log_err("failed to load trust anchor from %s "
817 				"at line %i, skipping", nm, line_nr);
818                         /* try to do the rest */
819 			continue;
820                 }
821 		if(tp && tp != tp2) {
822 			log_err("file %s has mismatching data inside: "
823 				"the file may only contain keys for one name, "
824 				"remove keys for other domain names", nm);
825         		fclose(fd);
826 			ldns_rdf_deep_free(origin);
827 			ldns_rdf_deep_free(prev);
828 			return 0;
829 		}
830 		tp = tp2;
831         }
832         fclose(fd);
833 	ldns_rdf_deep_free(origin);
834 	ldns_rdf_deep_free(prev);
835 	if(!tp) {
836 		log_err("failed to read %s", nm);
837 		return 0;
838 	}
839 
840 	/* now assemble the data into DNSKEY and DS packed rrsets */
841 	lock_basic_lock(&tp->lock);
842 	if(!autr_assemble(tp)) {
843 		lock_basic_unlock(&tp->lock);
844 		log_err("malloc failure assembling %s", nm);
845 		return 0;
846 	}
847 	lock_basic_unlock(&tp->lock);
848 	return 1;
849 }
850 
851 /** string for a trustanchor state */
852 static const char*
853 trustanchor_state2str(autr_state_t s)
854 {
855         switch (s) {
856                 case AUTR_STATE_START:       return "  START  ";
857                 case AUTR_STATE_ADDPEND:     return " ADDPEND ";
858                 case AUTR_STATE_VALID:       return "  VALID  ";
859                 case AUTR_STATE_MISSING:     return " MISSING ";
860                 case AUTR_STATE_REVOKED:     return " REVOKED ";
861                 case AUTR_STATE_REMOVED:     return " REMOVED ";
862         }
863         return " UNKNOWN ";
864 }
865 
866 /** print ID to file */
867 static int
868 print_id(FILE* out, char* fname, struct module_env* env,
869 	uint8_t* nm, size_t nmlen, uint16_t dclass)
870 {
871 	ldns_rdf rdf;
872 #ifdef UNBOUND_DEBUG
873 	ldns_status s;
874 #endif
875 
876 	memset(&rdf, 0, sizeof(rdf));
877 	ldns_rdf_set_data(&rdf, nm);
878 	ldns_rdf_set_size(&rdf, nmlen);
879 	ldns_rdf_set_type(&rdf, LDNS_RDF_TYPE_DNAME);
880 
881 	ldns_buffer_clear(env->scratch_buffer);
882 #ifdef UNBOUND_DEBUG
883 	s =
884 #endif
885 	ldns_rdf2buffer_str_dname(env->scratch_buffer, &rdf);
886 	log_assert(s == LDNS_STATUS_OK);
887 	ldns_buffer_write_u8(env->scratch_buffer, 0);
888 	ldns_buffer_flip(env->scratch_buffer);
889 	if(fprintf(out, ";;id: %s %d\n",
890 		(char*)ldns_buffer_begin(env->scratch_buffer),
891 		(int)dclass) < 0) {
892 		log_err("could not write to %s: %s", fname, strerror(errno));
893 		return 0;
894 	}
895 	return 1;
896 }
897 
898 static int
899 autr_write_contents(FILE* out, char* fn, struct module_env* env,
900 	struct trust_anchor* tp)
901 {
902 	char tmi[32];
903 	struct autr_ta* ta;
904 	char* str;
905 
906 	/* write pretty header */
907 	if(fprintf(out, "; autotrust trust anchor file\n") < 0) {
908 		log_err("could not write to %s: %s", fn, strerror(errno));
909 		return 0;
910 	}
911 	if(tp->autr->revoked) {
912 		if(fprintf(out, ";;REVOKED\n") < 0 ||
913 		   fprintf(out, "; The zone has all keys revoked, and is\n"
914 			"; considered as if it has no trust anchors.\n"
915 			"; the remainder of the file is the last probe.\n"
916 			"; to restart the trust anchor, overwrite this file.\n"
917 			"; with one containing valid DNSKEYs or DSes.\n") < 0) {
918 		   log_err("could not write to %s: %s", fn, strerror(errno));
919 		   return 0;
920 		}
921 	}
922 	if(!print_id(out, fn, env, tp->name, tp->namelen, tp->dclass)) {
923 		return 0;
924 	}
925 	if(fprintf(out, ";;last_queried: %u ;;%s",
926 		(unsigned int)tp->autr->last_queried,
927 		ctime_r(&(tp->autr->last_queried), tmi)) < 0 ||
928 	   fprintf(out, ";;last_success: %u ;;%s",
929 		(unsigned int)tp->autr->last_success,
930 		ctime_r(&(tp->autr->last_success), tmi)) < 0 ||
931 	   fprintf(out, ";;next_probe_time: %u ;;%s",
932 		(unsigned int)tp->autr->next_probe_time,
933 		ctime_r(&(tp->autr->next_probe_time), tmi)) < 0 ||
934 	   fprintf(out, ";;query_failed: %d\n", (int)tp->autr->query_failed)<0
935 	   || fprintf(out, ";;query_interval: %d\n",
936 	   (int)tp->autr->query_interval) < 0 ||
937 	   fprintf(out, ";;retry_time: %d\n", (int)tp->autr->retry_time) < 0) {
938 		log_err("could not write to %s: %s", fn, strerror(errno));
939 		return 0;
940 	}
941 
942 	/* write anchors */
943 	for(ta=tp->autr->keys; ta; ta=ta->next) {
944 		/* by default do not store START and REMOVED keys */
945 		if(ta->s == AUTR_STATE_START)
946 			continue;
947 		if(ta->s == AUTR_STATE_REMOVED)
948 			continue;
949 		/* only store keys */
950 		if(ldns_rr_get_type(ta->rr) != LDNS_RR_TYPE_DNSKEY)
951 			continue;
952 		str = ldns_rr2str(ta->rr);
953 		if(!str || !str[0]) {
954 			free(str);
955 			log_err("malloc failure writing %s", fn);
956 			return 0;
957 		}
958 		str[strlen(str)-1] = 0; /* remove newline */
959 		if(fprintf(out, "%s ;;state=%d [%s] ;;count=%d "
960 			";;lastchange=%u ;;%s", str, (int)ta->s,
961 			trustanchor_state2str(ta->s), (int)ta->pending_count,
962 			(unsigned int)ta->last_change,
963 			ctime_r(&(ta->last_change), tmi)) < 0) {
964 		   log_err("could not write to %s: %s", fn, strerror(errno));
965 		   free(str);
966 		   return 0;
967 		}
968 		free(str);
969 	}
970 	return 1;
971 }
972 
973 void autr_write_file(struct module_env* env, struct trust_anchor* tp)
974 {
975 	FILE* out;
976 	char* fname = tp->autr->file;
977 	char tempf[2048];
978 	log_assert(tp->autr);
979 	/* unique name with pid number and thread number */
980 	snprintf(tempf, sizeof(tempf), "%s.%d-%d", fname, (int)getpid(),
981 		env&&env->worker?*(int*)env->worker:0);
982 	verbose(VERB_ALGO, "autotrust: write to disk: %s", tempf);
983 	out = fopen(tempf, "w");
984 	if(!out) {
985 		log_err("could not open autotrust file for writing, %s: %s",
986 			tempf, strerror(errno));
987 		return;
988 	}
989 	if(!autr_write_contents(out, tempf, env, tp)) {
990 		/* failed to write contents (completely) */
991 		fclose(out);
992 		unlink(tempf);
993 		log_err("could not completely write: %s", fname);
994 		return;
995 	}
996 	/* success; overwrite actual file */
997 	fclose(out);
998 	verbose(VERB_ALGO, "autotrust: replaced %s", fname);
999 	if(rename(tempf, fname) < 0) {
1000 		log_err("rename(%s to %s): %s", tempf, fname, strerror(errno));
1001 	}
1002 }
1003 
1004 /**
1005  * Verify if dnskey works for trust point
1006  * @param env: environment (with time) for verification
1007  * @param ve: validator environment (with options) for verification.
1008  * @param tp: trust point to verify with
1009  * @param rrset: DNSKEY rrset to verify.
1010  * @return false on failure, true if verification successful.
1011  */
1012 static int
1013 verify_dnskey(struct module_env* env, struct val_env* ve,
1014         struct trust_anchor* tp, struct ub_packed_rrset_key* rrset)
1015 {
1016 	char* reason = NULL;
1017 	uint8_t sigalg[ALGO_NEEDS_MAX+1];
1018 	int downprot = 1;
1019 	enum sec_status sec = val_verify_DNSKEY_with_TA(env, ve, rrset,
1020 		tp->ds_rrset, tp->dnskey_rrset, downprot?sigalg:NULL, &reason);
1021 	/* sigalg is ignored, it returns algorithms signalled to exist, but
1022 	 * in 5011 there are no other rrsets to check.  if downprot is
1023 	 * enabled, then it checks that the DNSKEY is signed with all
1024 	 * algorithms available in the trust store. */
1025 	verbose(VERB_ALGO, "autotrust: validate DNSKEY with anchor: %s",
1026 		sec_status_to_string(sec));
1027 	return sec == sec_status_secure;
1028 }
1029 
1030 /** Find minimum expiration interval from signatures */
1031 static uint32_t
1032 min_expiry(struct module_env* env, ldns_rr_list* rrset)
1033 {
1034 	size_t i;
1035 	uint32_t t, r = 15 * 24 * 3600; /* 15 days max */
1036 	for(i=0; i<ldns_rr_list_rr_count(rrset); i++) {
1037 		ldns_rr* rr = ldns_rr_list_rr(rrset, i);
1038 		if(ldns_rr_get_type(rr) != LDNS_RR_TYPE_RRSIG)
1039 			continue;
1040 		t = ldns_rdf2native_int32(ldns_rr_rrsig_expiration(rr));
1041 		if(t - *env->now > 0) {
1042 			t -= *env->now;
1043 			if(t < r)
1044 				r = t;
1045 		}
1046 	}
1047 	return r;
1048 }
1049 
1050 /** Is rr self-signed revoked key */
1051 static int
1052 rr_is_selfsigned_revoked(struct module_env* env, struct val_env* ve,
1053 	struct ub_packed_rrset_key* dnskey_rrset, size_t i)
1054 {
1055 	enum sec_status sec;
1056 	char* reason = NULL;
1057 	verbose(VERB_ALGO, "seen REVOKE flag, check self-signed, rr %d",
1058 		(int)i);
1059 	/* no algorithm downgrade protection necessary, if it is selfsigned
1060 	 * revoked it can be removed. */
1061 	sec = dnskey_verify_rrset(env, ve, dnskey_rrset, dnskey_rrset, i,
1062 		&reason);
1063 	return (sec == sec_status_secure);
1064 }
1065 
1066 /** Set fetched value */
1067 static void
1068 seen_trustanchor(struct autr_ta* ta, uint8_t seen)
1069 {
1070 	ta->fetched = seen;
1071 	if(ta->pending_count < 250) /* no numerical overflow, please */
1072 		ta->pending_count++;
1073 }
1074 
1075 /** set revoked value */
1076 static void
1077 seen_revoked_trustanchor(struct autr_ta* ta, uint8_t revoked)
1078 {
1079 	ta->revoked = revoked;
1080 }
1081 
1082 /** revoke a trust anchor */
1083 static void
1084 revoke_dnskey(struct autr_ta* ta, int off)
1085 {
1086         ldns_rdf* rdf;
1087         uint16_t flags;
1088 	log_assert(ta && ta->rr);
1089 	if(ldns_rr_get_type(ta->rr) != LDNS_RR_TYPE_DNSKEY)
1090 		return;
1091 	rdf = ldns_rr_dnskey_flags(ta->rr);
1092 	flags = ldns_read_uint16(ldns_rdf_data(rdf));
1093 
1094 	if (off && (flags&LDNS_KEY_REVOKE_KEY))
1095 		flags ^= LDNS_KEY_REVOKE_KEY; /* flip */
1096 	else
1097 		flags |= LDNS_KEY_REVOKE_KEY;
1098 	ldns_write_uint16(ldns_rdf_data(rdf), flags);
1099 }
1100 
1101 /** Compare two RR buffers skipping the REVOKED bit */
1102 static int
1103 ldns_rr_compare_wire_skip_revbit(ldns_buffer* rr1_buf, ldns_buffer* rr2_buf)
1104 {
1105 	size_t rr1_len, rr2_len, min_len, i, offset;
1106 	rr1_len = ldns_buffer_capacity(rr1_buf);
1107 	rr2_len = ldns_buffer_capacity(rr2_buf);
1108 	/* jump past dname (checked in earlier part) and especially past TTL */
1109 	offset = 0;
1110 	while (offset < rr1_len && *ldns_buffer_at(rr1_buf, offset) != 0)
1111 		offset += *ldns_buffer_at(rr1_buf, offset) + 1;
1112 	/* jump to rdata section (PAST the rdata length field) */
1113 	offset += 11; /* 0-dname-end + type + class + ttl + rdatalen */
1114 	min_len = (rr1_len < rr2_len) ? rr1_len : rr2_len;
1115 	/* compare RRs RDATA byte for byte. */
1116 	for(i = offset; i < min_len; i++)
1117 	{
1118 		uint8_t *rdf1, *rdf2;
1119 		rdf1 = ldns_buffer_at(rr1_buf, i);
1120 		rdf2 = ldns_buffer_at(rr2_buf, i);
1121 		if (i==(offset+1))
1122 		{
1123 			/* this is the second part of the flags field */
1124 			*rdf1 = *rdf1 | LDNS_KEY_REVOKE_KEY;
1125 			*rdf2 = *rdf2 | LDNS_KEY_REVOKE_KEY;
1126 		}
1127 		if (*rdf1 < *rdf2)	return -1;
1128 		else if (*rdf1 > *rdf2)	return 1;
1129         }
1130 	return 0;
1131 }
1132 
1133 /** Compare two RRs skipping the REVOKED bit */
1134 static int
1135 ldns_rr_compare_skip_revbit(const ldns_rr* rr1, const ldns_rr* rr2, int* result)
1136 {
1137 	size_t rr1_len, rr2_len;
1138 	ldns_buffer* rr1_buf;
1139 	ldns_buffer* rr2_buf;
1140 
1141 	*result = ldns_rr_compare_no_rdata(rr1, rr2);
1142 	if (*result == 0)
1143 	{
1144 		rr1_len = ldns_rr_uncompressed_size(rr1);
1145 		rr2_len = ldns_rr_uncompressed_size(rr2);
1146 		rr1_buf = ldns_buffer_new(rr1_len);
1147 		rr2_buf = ldns_buffer_new(rr2_len);
1148 		if(!rr1_buf || !rr2_buf) {
1149 			ldns_buffer_free(rr1_buf);
1150 			ldns_buffer_free(rr2_buf);
1151 			return 0;
1152 		}
1153 		if (ldns_rr2buffer_wire_canonical(rr1_buf, rr1,
1154 			LDNS_SECTION_ANY) != LDNS_STATUS_OK)
1155 		{
1156 			ldns_buffer_free(rr1_buf);
1157 			ldns_buffer_free(rr2_buf);
1158 			return 0;
1159 		}
1160 		if (ldns_rr2buffer_wire_canonical(rr2_buf, rr2,
1161 			LDNS_SECTION_ANY) != LDNS_STATUS_OK) {
1162 			ldns_buffer_free(rr1_buf);
1163 			ldns_buffer_free(rr2_buf);
1164 			return 0;
1165 		}
1166 		*result = ldns_rr_compare_wire_skip_revbit(rr1_buf, rr2_buf);
1167 		ldns_buffer_free(rr1_buf);
1168 		ldns_buffer_free(rr2_buf);
1169 	}
1170 	return 1;
1171 }
1172 
1173 
1174 /** compare two trust anchors */
1175 static int
1176 ta_compare(ldns_rr* a, ldns_rr* b, int* result)
1177 {
1178 	if (!a && !b)	*result = 0;
1179 	else if (!a)	*result = -1;
1180 	else if (!b)	*result = 1;
1181 	else if (ldns_rr_get_type(a) != ldns_rr_get_type(b))
1182 		*result = (int)ldns_rr_get_type(a) - (int)ldns_rr_get_type(b);
1183 	else if (ldns_rr_get_type(a) == LDNS_RR_TYPE_DNSKEY) {
1184 		if(!ldns_rr_compare_skip_revbit(a, b, result))
1185 			return 0;
1186 	}
1187 	else if (ldns_rr_get_type(a) == LDNS_RR_TYPE_DS)
1188 		*result = ldns_rr_compare(a, b);
1189 	else    *result = -1;
1190 	return 1;
1191 }
1192 
1193 /**
1194  * Find key
1195  * @param tp: to search in
1196  * @param rr: to look for
1197  * @param result: returns NULL or the ta key looked for.
1198  * @return false on malloc failure during search. if true examine result.
1199  */
1200 static int
1201 find_key(struct trust_anchor* tp, ldns_rr* rr, struct autr_ta** result)
1202 {
1203 	struct autr_ta* ta;
1204 	int ret;
1205 	if(!tp || !rr)
1206 		return 0;
1207 	for(ta=tp->autr->keys; ta; ta=ta->next) {
1208 		if(!ta_compare(ta->rr, rr, &ret))
1209 			return 0;
1210 		if(ret == 0) {
1211 			*result = ta;
1212 			return 1;
1213 		}
1214 	}
1215 	*result = NULL;
1216 	return 1;
1217 }
1218 
1219 /** add key and clone RR and tp already locked */
1220 static struct autr_ta*
1221 add_key(struct trust_anchor* tp, ldns_rr* rr)
1222 {
1223 	ldns_rr* c;
1224 	struct autr_ta* ta;
1225 	c = ldns_rr_clone(rr);
1226 	if(!c) return NULL;
1227 	ta = autr_ta_create(c);
1228 	if(!ta) {
1229 		ldns_rr_free(c);
1230 		return NULL;
1231 	}
1232 	/* link in, tp already locked */
1233 	ta->next = tp->autr->keys;
1234 	tp->autr->keys = ta;
1235 	return ta;
1236 }
1237 
1238 /** get TTL from DNSKEY rrset */
1239 static uint32_t
1240 key_ttl(struct ub_packed_rrset_key* k)
1241 {
1242 	struct packed_rrset_data* d = (struct packed_rrset_data*)k->entry.data;
1243 	return d->ttl;
1244 }
1245 
1246 /** update the time values for the trustpoint */
1247 static void
1248 set_tp_times(struct trust_anchor* tp, uint32_t rrsig_exp_interval,
1249 	uint32_t origttl, int* changed)
1250 {
1251 	uint32_t x, qi = tp->autr->query_interval, rt = tp->autr->retry_time;
1252 
1253 	/* x = MIN(15days, ttl/2, expire/2) */
1254 	x = 15 * 24 * 3600;
1255 	if(origttl/2 < x)
1256 		x = origttl/2;
1257 	if(rrsig_exp_interval/2 < x)
1258 		x = rrsig_exp_interval/2;
1259 	/* MAX(1hr, x) */
1260 	if(x < 3600)
1261 		tp->autr->query_interval = 3600;
1262 	else	tp->autr->query_interval = x;
1263 
1264 	/* x= MIN(1day, ttl/10, expire/10) */
1265 	x = 24 * 3600;
1266 	if(origttl/10 < x)
1267 		x = origttl/10;
1268 	if(rrsig_exp_interval/10 < x)
1269 		x = rrsig_exp_interval/10;
1270 	/* MAX(1hr, x) */
1271 	if(x < 3600)
1272 		tp->autr->retry_time = 3600;
1273 	else	tp->autr->retry_time = x;
1274 
1275 	if(qi != tp->autr->query_interval || rt != tp->autr->retry_time) {
1276 		*changed = 1;
1277 		verbose(VERB_ALGO, "orig_ttl is %d", (int)origttl);
1278 		verbose(VERB_ALGO, "rrsig_exp_interval is %d",
1279 			(int)rrsig_exp_interval);
1280 		verbose(VERB_ALGO, "query_interval: %d, retry_time: %d",
1281 			(int)tp->autr->query_interval,
1282 			(int)tp->autr->retry_time);
1283 	}
1284 }
1285 
1286 /** init events to zero */
1287 static void
1288 init_events(struct trust_anchor* tp)
1289 {
1290 	struct autr_ta* ta;
1291 	for(ta=tp->autr->keys; ta; ta=ta->next) {
1292 		ta->fetched = 0;
1293 	}
1294 }
1295 
1296 /** check for revoked keys without trusting any other information */
1297 static void
1298 check_contains_revoked(struct module_env* env, struct val_env* ve,
1299 	struct trust_anchor* tp, struct ub_packed_rrset_key* dnskey_rrset,
1300 	int* changed)
1301 {
1302 	ldns_rr_list* r = packed_rrset_to_rr_list(dnskey_rrset,
1303 		env->scratch_buffer);
1304 	size_t i;
1305 	if(!r) {
1306 		log_err("malloc failure");
1307 		return;
1308 	}
1309 	for(i=0; i<ldns_rr_list_rr_count(r); i++) {
1310 		ldns_rr* rr = ldns_rr_list_rr(r, i);
1311 		struct autr_ta* ta = NULL;
1312 		if(ldns_rr_get_type(rr) != LDNS_RR_TYPE_DNSKEY)
1313 			continue;
1314 		if(!rr_is_dnskey_sep(rr) || !rr_is_dnskey_revoked(rr))
1315 			continue; /* not a revoked KSK */
1316 		if(!find_key(tp, rr, &ta)) {
1317 			log_err("malloc failure");
1318 			continue; /* malloc fail in compare*/
1319 		}
1320 		if(!ta)
1321 			continue; /* key not found */
1322 		if(rr_is_selfsigned_revoked(env, ve, dnskey_rrset, i)) {
1323 			/* checked if there is an rrsig signed by this key. */
1324 			log_assert(dnskey_calc_keytag(dnskey_rrset, i) ==
1325 				ldns_calc_keytag(rr)); /* checks conversion*/
1326 			verbose_key(ta, VERB_ALGO, "is self-signed revoked");
1327 			if(!ta->revoked)
1328 				*changed = 1;
1329 			seen_revoked_trustanchor(ta, 1);
1330 			do_revoked(env, ta, changed);
1331 		}
1332 	}
1333 	ldns_rr_list_deep_free(r);
1334 }
1335 
1336 /** See if a DNSKEY is verified by one of the DSes */
1337 static int
1338 key_matches_a_ds(struct module_env* env, struct val_env* ve,
1339 	struct ub_packed_rrset_key* dnskey_rrset, size_t key_idx,
1340 	struct ub_packed_rrset_key* ds_rrset)
1341 {
1342 	struct packed_rrset_data* dd = (struct packed_rrset_data*)
1343 	                ds_rrset->entry.data;
1344 	size_t ds_idx, num = dd->count;
1345 	int d = val_favorite_ds_algo(ds_rrset);
1346 	char* reason = "";
1347 	for(ds_idx=0; ds_idx<num; ds_idx++) {
1348 		if(!ds_digest_algo_is_supported(ds_rrset, ds_idx) ||
1349 			!ds_key_algo_is_supported(ds_rrset, ds_idx) ||
1350 			ds_get_digest_algo(ds_rrset, ds_idx) != d)
1351 			continue;
1352 		if(ds_get_key_algo(ds_rrset, ds_idx)
1353 		   != dnskey_get_algo(dnskey_rrset, key_idx)
1354 		   || dnskey_calc_keytag(dnskey_rrset, key_idx)
1355 		   != ds_get_keytag(ds_rrset, ds_idx)) {
1356 			continue;
1357 		}
1358 		if(!ds_digest_match_dnskey(env, dnskey_rrset, key_idx,
1359 			ds_rrset, ds_idx)) {
1360 			verbose(VERB_ALGO, "DS match attempt failed");
1361 			continue;
1362 		}
1363 		if(dnskey_verify_rrset(env, ve, dnskey_rrset,
1364 			dnskey_rrset, key_idx, &reason) == sec_status_secure) {
1365 			return 1;
1366 		} else {
1367 			verbose(VERB_ALGO, "DS match failed because the key "
1368 				"does not verify the keyset: %s", reason);
1369 		}
1370 	}
1371 	return 0;
1372 }
1373 
1374 /** Set update events */
1375 static int
1376 update_events(struct module_env* env, struct val_env* ve,
1377 	struct trust_anchor* tp, struct ub_packed_rrset_key* dnskey_rrset,
1378 	int* changed)
1379 {
1380 	ldns_rr_list* r = packed_rrset_to_rr_list(dnskey_rrset,
1381 		env->scratch_buffer);
1382 	size_t i;
1383 	if(!r)
1384 		return 0;
1385 	init_events(tp);
1386 	for(i=0; i<ldns_rr_list_rr_count(r); i++) {
1387 		ldns_rr* rr = ldns_rr_list_rr(r, i);
1388 		struct autr_ta* ta = NULL;
1389 		if(ldns_rr_get_type(rr) != LDNS_RR_TYPE_DNSKEY)
1390 			continue;
1391 		if(!rr_is_dnskey_sep(rr))
1392 			continue;
1393 		if(rr_is_dnskey_revoked(rr)) {
1394 			/* self-signed revoked keys already detected before,
1395 			 * other revoked keys are not 'added' again */
1396 			continue;
1397 		}
1398 		/* is a key of this type supported?. Note rr_list and
1399 		 * packed_rrset are in the same order. */
1400 		if(!dnskey_algo_is_supported(dnskey_rrset, i)) {
1401 			/* skip unknown algorithm key, it is useless to us */
1402 			log_nametypeclass(VERB_DETAIL, "trust point has "
1403 				"unsupported algorithm at",
1404 				tp->name, LDNS_RR_TYPE_DNSKEY, tp->dclass);
1405 			continue;
1406 		}
1407 
1408 		/* is it new? if revocation bit set, find the unrevoked key */
1409 		if(!find_key(tp, rr, &ta)) {
1410 			ldns_rr_list_deep_free(r); /* malloc fail in compare*/
1411 			return 0;
1412 		}
1413 		if(!ta) {
1414 			ta = add_key(tp, rr);
1415 			*changed = 1;
1416 			/* first time seen, do we have DSes? if match: VALID */
1417 			if(ta && tp->ds_rrset && key_matches_a_ds(env, ve,
1418 				dnskey_rrset, i, tp->ds_rrset)) {
1419 				verbose_key(ta, VERB_ALGO, "verified by DS");
1420 				ta->s = AUTR_STATE_VALID;
1421 			}
1422 		}
1423 		if(!ta) {
1424 			ldns_rr_list_deep_free(r);
1425 			return 0;
1426 		}
1427 		seen_trustanchor(ta, 1);
1428 		verbose_key(ta, VERB_ALGO, "in DNS response");
1429 	}
1430 	set_tp_times(tp, min_expiry(env, r), key_ttl(dnskey_rrset), changed);
1431 	ldns_rr_list_deep_free(r);
1432 	return 1;
1433 }
1434 
1435 /**
1436  * Check if the holddown time has already exceeded
1437  * setting: add-holddown: add holddown timer
1438  * setting: del-holddown: del holddown timer
1439  * @param env: environment with current time
1440  * @param ta: trust anchor to check for.
1441  * @param holddown: the timer value
1442  * @return number of seconds the holddown has passed.
1443  */
1444 static int
1445 check_holddown(struct module_env* env, struct autr_ta* ta,
1446 	unsigned int holddown)
1447 {
1448         unsigned int elapsed;
1449 	if((unsigned)*env->now < (unsigned)ta->last_change) {
1450 		log_warn("time goes backwards. delaying key holddown");
1451 		return 0;
1452 	}
1453 	elapsed = (unsigned)*env->now - (unsigned)ta->last_change;
1454         if (elapsed > holddown) {
1455                 return (int) (elapsed-holddown);
1456         }
1457 	verbose_key(ta, VERB_ALGO, "holddown time %d seconds to go",
1458 		(int) (holddown-elapsed));
1459         return 0;
1460 }
1461 
1462 
1463 /** Set last_change to now */
1464 static void
1465 reset_holddown(struct module_env* env, struct autr_ta* ta, int* changed)
1466 {
1467 	ta->last_change = *env->now;
1468 	*changed = 1;
1469 }
1470 
1471 /** Set the state for this trust anchor */
1472 static void
1473 set_trustanchor_state(struct module_env* env, struct autr_ta* ta, int* changed,
1474 	autr_state_t s)
1475 {
1476 	verbose_key(ta, VERB_ALGO, "update: %s to %s",
1477 		trustanchor_state2str(ta->s), trustanchor_state2str(s));
1478 	ta->s = s;
1479 	reset_holddown(env, ta, changed);
1480 }
1481 
1482 
1483 /** Event: NewKey */
1484 static void
1485 do_newkey(struct module_env* env, struct autr_ta* anchor, int* c)
1486 {
1487 	if (anchor->s == AUTR_STATE_START)
1488 		set_trustanchor_state(env, anchor, c, AUTR_STATE_ADDPEND);
1489 }
1490 
1491 /** Event: AddTime */
1492 static void
1493 do_addtime(struct module_env* env, struct autr_ta* anchor, int* c)
1494 {
1495 	/* This not according to RFC, this is 30 days, but the RFC demands
1496 	 * MAX(30days, TTL expire time of first DNSKEY set with this key),
1497 	 * The value may be too small if a very large TTL was used. */
1498 	int exceeded = check_holddown(env, anchor, env->cfg->add_holddown);
1499 	if (exceeded && anchor->s == AUTR_STATE_ADDPEND) {
1500 		verbose_key(anchor, VERB_ALGO, "add-holddown time exceeded "
1501 			"%d seconds ago, and pending-count %d", exceeded,
1502 			anchor->pending_count);
1503 		if(anchor->pending_count >= MIN_PENDINGCOUNT) {
1504 			set_trustanchor_state(env, anchor, c, AUTR_STATE_VALID);
1505 			anchor->pending_count = 0;
1506 			return;
1507 		}
1508 		verbose_key(anchor, VERB_ALGO, "add-holddown time sanity check "
1509 			"failed (pending count: %d)", anchor->pending_count);
1510 	}
1511 }
1512 
1513 /** Event: RemTime */
1514 static void
1515 do_remtime(struct module_env* env, struct autr_ta* anchor, int* c)
1516 {
1517 	int exceeded = check_holddown(env, anchor, env->cfg->del_holddown);
1518 	if(exceeded && anchor->s == AUTR_STATE_REVOKED) {
1519 		verbose_key(anchor, VERB_ALGO, "del-holddown time exceeded "
1520 			"%d seconds ago", exceeded);
1521 		set_trustanchor_state(env, anchor, c, AUTR_STATE_REMOVED);
1522 	}
1523 }
1524 
1525 /** Event: KeyRem */
1526 static void
1527 do_keyrem(struct module_env* env, struct autr_ta* anchor, int* c)
1528 {
1529 	if(anchor->s == AUTR_STATE_ADDPEND) {
1530 		set_trustanchor_state(env, anchor, c, AUTR_STATE_START);
1531 		anchor->pending_count = 0;
1532 	} else if(anchor->s == AUTR_STATE_VALID)
1533 		set_trustanchor_state(env, anchor, c, AUTR_STATE_MISSING);
1534 }
1535 
1536 /** Event: KeyPres */
1537 static void
1538 do_keypres(struct module_env* env, struct autr_ta* anchor, int* c)
1539 {
1540 	if(anchor->s == AUTR_STATE_MISSING)
1541 		set_trustanchor_state(env, anchor, c, AUTR_STATE_VALID);
1542 }
1543 
1544 /* Event: Revoked */
1545 static void
1546 do_revoked(struct module_env* env, struct autr_ta* anchor, int* c)
1547 {
1548 	if(anchor->s == AUTR_STATE_VALID || anchor->s == AUTR_STATE_MISSING) {
1549                 set_trustanchor_state(env, anchor, c, AUTR_STATE_REVOKED);
1550 		verbose_key(anchor, VERB_ALGO, "old id, prior to revocation");
1551                 revoke_dnskey(anchor, 0);
1552 		verbose_key(anchor, VERB_ALGO, "new id, after revocation");
1553 	}
1554 }
1555 
1556 /** Do statestable transition matrix for anchor */
1557 static void
1558 anchor_state_update(struct module_env* env, struct autr_ta* anchor, int* c)
1559 {
1560 	log_assert(anchor);
1561 	switch(anchor->s) {
1562 	/* START */
1563 	case AUTR_STATE_START:
1564 		/* NewKey: ADDPEND */
1565 		if (anchor->fetched)
1566 			do_newkey(env, anchor, c);
1567 		break;
1568 	/* ADDPEND */
1569 	case AUTR_STATE_ADDPEND:
1570 		/* KeyRem: START */
1571 		if (!anchor->fetched)
1572 			do_keyrem(env, anchor, c);
1573 		/* AddTime: VALID */
1574 		else	do_addtime(env, anchor, c);
1575 		break;
1576 	/* VALID */
1577 	case AUTR_STATE_VALID:
1578 		/* RevBit: REVOKED */
1579 		if (anchor->revoked)
1580 			do_revoked(env, anchor, c);
1581 		/* KeyRem: MISSING */
1582 		else if (!anchor->fetched)
1583 			do_keyrem(env, anchor, c);
1584 		else if(!anchor->last_change) {
1585 			verbose_key(anchor, VERB_ALGO, "first seen");
1586 			reset_holddown(env, anchor, c);
1587 		}
1588 		break;
1589 	/* MISSING */
1590 	case AUTR_STATE_MISSING:
1591 		/* RevBit: REVOKED */
1592 		if (anchor->revoked)
1593 			do_revoked(env, anchor, c);
1594 		/* KeyPres */
1595 		else if (anchor->fetched)
1596 			do_keypres(env, anchor, c);
1597 		break;
1598 	/* REVOKED */
1599 	case AUTR_STATE_REVOKED:
1600 		if (anchor->fetched)
1601 			reset_holddown(env, anchor, c);
1602 		/* RemTime: REMOVED */
1603 		else	do_remtime(env, anchor, c);
1604 		break;
1605 	/* REMOVED */
1606 	case AUTR_STATE_REMOVED:
1607 	default:
1608 		break;
1609 	}
1610 }
1611 
1612 /** if ZSK init then trust KSKs */
1613 static int
1614 init_zsk_to_ksk(struct module_env* env, struct trust_anchor* tp, int* changed)
1615 {
1616 	/* search for VALID ZSKs */
1617 	struct autr_ta* anchor;
1618 	int validzsk = 0;
1619 	int validksk = 0;
1620 	for(anchor = tp->autr->keys; anchor; anchor = anchor->next) {
1621 		/* last_change test makes sure it was manually configured */
1622                 if (ldns_rr_get_type(anchor->rr) == LDNS_RR_TYPE_DNSKEY &&
1623 			anchor->last_change == 0 &&
1624 			!rr_is_dnskey_sep(anchor->rr) &&
1625 			anchor->s == AUTR_STATE_VALID)
1626                         validzsk++;
1627 	}
1628 	if(validzsk == 0)
1629 		return 0;
1630 	for(anchor = tp->autr->keys; anchor; anchor = anchor->next) {
1631                 if (rr_is_dnskey_sep(anchor->rr) &&
1632 			anchor->s == AUTR_STATE_ADDPEND) {
1633 			verbose_key(anchor, VERB_ALGO, "trust KSK from "
1634 				"ZSK(config)");
1635 			set_trustanchor_state(env, anchor, changed,
1636 				AUTR_STATE_VALID);
1637 			validksk++;
1638 		}
1639 	}
1640 	return validksk;
1641 }
1642 
1643 /** Remove missing trustanchors so the list does not grow forever */
1644 static void
1645 remove_missing_trustanchors(struct module_env* env, struct trust_anchor* tp,
1646 	int* changed)
1647 {
1648 	struct autr_ta* anchor;
1649 	int exceeded;
1650 	int valid = 0;
1651 	/* see if we have anchors that are valid */
1652 	for(anchor = tp->autr->keys; anchor; anchor = anchor->next) {
1653 		/* Only do KSKs */
1654                 if (!rr_is_dnskey_sep(anchor->rr))
1655                         continue;
1656                 if (anchor->s == AUTR_STATE_VALID)
1657                         valid++;
1658 	}
1659 	/* if there are no SEP Valid anchors, see if we started out with
1660 	 * a ZSK (last-change=0) anchor, which is VALID and there are KSKs
1661 	 * now that can be made valid.  Do this immediately because there
1662 	 * is no guarantee that the ZSKs get announced long enough.  Usually
1663 	 * this is immediately after init with a ZSK trusted, unless the domain
1664 	 * was not advertising any KSKs at all.  In which case we perfectly
1665 	 * track the zero number of KSKs. */
1666 	if(valid == 0) {
1667 		valid = init_zsk_to_ksk(env, tp, changed);
1668 		if(valid == 0)
1669 			return;
1670 	}
1671 
1672 	for(anchor = tp->autr->keys; anchor; anchor = anchor->next) {
1673 		/* ignore ZSKs if newly added */
1674 		if(anchor->s == AUTR_STATE_START)
1675 			continue;
1676 		/* remove ZSKs if a KSK is present */
1677                 if (!rr_is_dnskey_sep(anchor->rr)) {
1678 			if(valid > 0) {
1679 				verbose_key(anchor, VERB_ALGO, "remove ZSK "
1680 					"[%d key(s) VALID]", valid);
1681 				set_trustanchor_state(env, anchor, changed,
1682 					AUTR_STATE_REMOVED);
1683 			}
1684                         continue;
1685 		}
1686                 /* Only do MISSING keys */
1687                 if (anchor->s != AUTR_STATE_MISSING)
1688                         continue;
1689 		if(env->cfg->keep_missing == 0)
1690 			continue; /* keep forever */
1691 
1692 		exceeded = check_holddown(env, anchor, env->cfg->keep_missing);
1693 		/* If keep_missing has exceeded and we still have more than
1694 		 * one valid KSK: remove missing trust anchor */
1695                 if (exceeded && valid > 0) {
1696 			verbose_key(anchor, VERB_ALGO, "keep-missing time "
1697 				"exceeded %d seconds ago, [%d key(s) VALID]",
1698 				exceeded, valid);
1699 			set_trustanchor_state(env, anchor, changed,
1700 				AUTR_STATE_REMOVED);
1701 		}
1702 	}
1703 }
1704 
1705 /** Do the statetable from RFC5011 transition matrix */
1706 static int
1707 do_statetable(struct module_env* env, struct trust_anchor* tp, int* changed)
1708 {
1709 	struct autr_ta* anchor;
1710 	for(anchor = tp->autr->keys; anchor; anchor = anchor->next) {
1711 		/* Only do KSKs */
1712 		if(!rr_is_dnskey_sep(anchor->rr))
1713 			continue;
1714 		anchor_state_update(env, anchor, changed);
1715 	}
1716 	remove_missing_trustanchors(env, tp, changed);
1717 	return 1;
1718 }
1719 
1720 /** See if time alone makes ADDPEND to VALID transition */
1721 static void
1722 autr_holddown_exceed(struct module_env* env, struct trust_anchor* tp, int* c)
1723 {
1724 	struct autr_ta* anchor;
1725 	for(anchor = tp->autr->keys; anchor; anchor = anchor->next) {
1726 		if(rr_is_dnskey_sep(anchor->rr) &&
1727 			anchor->s == AUTR_STATE_ADDPEND)
1728 			do_addtime(env, anchor, c);
1729 	}
1730 }
1731 
1732 /** cleanup key list */
1733 static void
1734 autr_cleanup_keys(struct trust_anchor* tp)
1735 {
1736 	struct autr_ta* p, **prevp;
1737 	prevp = &tp->autr->keys;
1738 	p = tp->autr->keys;
1739 	while(p) {
1740 		/* do we want to remove this key? */
1741 		if(p->s == AUTR_STATE_START || p->s == AUTR_STATE_REMOVED ||
1742 			ldns_rr_get_type(p->rr) != LDNS_RR_TYPE_DNSKEY) {
1743 			struct autr_ta* np = p->next;
1744 			/* remove */
1745 			ldns_rr_free(p->rr);
1746 			free(p);
1747 			/* snip and go to next item */
1748 			*prevp = np;
1749 			p = np;
1750 			continue;
1751 		}
1752 		/* remove pending counts if no longer pending */
1753 		if(p->s != AUTR_STATE_ADDPEND)
1754 			p->pending_count = 0;
1755 		prevp = &p->next;
1756 		p = p->next;
1757 	}
1758 }
1759 
1760 /** calculate next probe time */
1761 static time_t
1762 calc_next_probe(struct module_env* env, uint32_t wait)
1763 {
1764 	/* make it random, 90-100% */
1765 	uint32_t rnd, rest;
1766 	if(wait < 3600)
1767 		wait = 3600;
1768 	rnd = wait/10;
1769 	rest = wait-rnd;
1770 	rnd = (uint32_t)ub_random_max(env->rnd, (long int)rnd);
1771 	return (time_t)(*env->now + rest + rnd);
1772 }
1773 
1774 /** what is first probe time (anchors must be locked) */
1775 static time_t
1776 wait_probe_time(struct val_anchors* anchors)
1777 {
1778 	rbnode_t* t = rbtree_first(&anchors->autr->probe);
1779 	if(t != RBTREE_NULL)
1780 		return ((struct trust_anchor*)t->key)->autr->next_probe_time;
1781 	return 0;
1782 }
1783 
1784 /** reset worker timer */
1785 static void
1786 reset_worker_timer(struct module_env* env)
1787 {
1788 	struct timeval tv;
1789 #ifndef S_SPLINT_S
1790 	uint32_t next = (uint32_t)wait_probe_time(env->anchors);
1791 	/* in case this is libunbound, no timer */
1792 	if(!env->probe_timer)
1793 		return;
1794 	if(next > *env->now)
1795 		tv.tv_sec = (time_t)(next - *env->now);
1796 	else	tv.tv_sec = 0;
1797 #endif
1798 	tv.tv_usec = 0;
1799 	comm_timer_set(env->probe_timer, &tv);
1800 	verbose(VERB_ALGO, "scheduled next probe in %d sec", (int)tv.tv_sec);
1801 }
1802 
1803 /** set next probe for trust anchor */
1804 static int
1805 set_next_probe(struct module_env* env, struct trust_anchor* tp,
1806 	struct ub_packed_rrset_key* dnskey_rrset)
1807 {
1808 	struct trust_anchor key, *tp2;
1809 	time_t mold, mnew;
1810 	/* use memory allocated in rrset for temporary name storage */
1811 	key.node.key = &key;
1812 	key.name = dnskey_rrset->rk.dname;
1813 	key.namelen = dnskey_rrset->rk.dname_len;
1814 	key.namelabs = dname_count_labels(key.name);
1815 	key.dclass = tp->dclass;
1816 	lock_basic_unlock(&tp->lock);
1817 
1818 	/* fetch tp again and lock anchors, so that we can modify the trees */
1819 	lock_basic_lock(&env->anchors->lock);
1820 	tp2 = (struct trust_anchor*)rbtree_search(env->anchors->tree, &key);
1821 	if(!tp2) {
1822 		verbose(VERB_ALGO, "trustpoint was deleted in set_next_probe");
1823 		lock_basic_unlock(&env->anchors->lock);
1824 		return 0;
1825 	}
1826 	log_assert(tp == tp2);
1827 	lock_basic_lock(&tp->lock);
1828 
1829 	/* schedule */
1830 	mold = wait_probe_time(env->anchors);
1831 	(void)rbtree_delete(&env->anchors->autr->probe, tp);
1832 	tp->autr->next_probe_time = calc_next_probe(env,
1833 		tp->autr->query_interval);
1834 	(void)rbtree_insert(&env->anchors->autr->probe, &tp->autr->pnode);
1835 	mnew = wait_probe_time(env->anchors);
1836 
1837 	lock_basic_unlock(&env->anchors->lock);
1838 	verbose(VERB_ALGO, "next probe set in %d seconds",
1839 		(int)tp->autr->next_probe_time - (int)*env->now);
1840 	if(mold != mnew) {
1841 		reset_worker_timer(env);
1842 	}
1843 	return 1;
1844 }
1845 
1846 /** Revoke and Delete a trust point */
1847 static void
1848 autr_tp_remove(struct module_env* env, struct trust_anchor* tp,
1849 	struct ub_packed_rrset_key* dnskey_rrset)
1850 {
1851 	struct trust_anchor key;
1852 	struct autr_point_data pd;
1853 	time_t mold, mnew;
1854 
1855 	log_nametypeclass(VERB_OPS, "trust point was revoked",
1856 		tp->name, LDNS_RR_TYPE_DNSKEY, tp->dclass);
1857 	tp->autr->revoked = 1;
1858 
1859 	/* use space allocated for dnskey_rrset to save name of anchor */
1860 	memset(&key, 0, sizeof(key));
1861 	memset(&pd, 0, sizeof(pd));
1862 	key.autr = &pd;
1863 	key.node.key = &key;
1864 	pd.pnode.key = &key;
1865 	pd.next_probe_time = tp->autr->next_probe_time;
1866 	key.name = dnskey_rrset->rk.dname;
1867 	key.namelen = tp->namelen;
1868 	key.namelabs = tp->namelabs;
1869 	key.dclass = tp->dclass;
1870 
1871 	/* unlock */
1872 	lock_basic_unlock(&tp->lock);
1873 
1874 	/* take from tree. It could be deleted by someone else,hence (void). */
1875 	lock_basic_lock(&env->anchors->lock);
1876 	(void)rbtree_delete(env->anchors->tree, &key);
1877 	mold = wait_probe_time(env->anchors);
1878 	(void)rbtree_delete(&env->anchors->autr->probe, &key);
1879 	mnew = wait_probe_time(env->anchors);
1880 	anchors_init_parents_locked(env->anchors);
1881 	lock_basic_unlock(&env->anchors->lock);
1882 
1883 	/* save on disk */
1884 	tp->autr->next_probe_time = 0; /* no more probing for it */
1885 	autr_write_file(env, tp);
1886 
1887 	/* delete */
1888 	autr_point_delete(tp);
1889 	if(mold != mnew) {
1890 		reset_worker_timer(env);
1891 	}
1892 }
1893 
1894 int autr_process_prime(struct module_env* env, struct val_env* ve,
1895 	struct trust_anchor* tp, struct ub_packed_rrset_key* dnskey_rrset)
1896 {
1897 	int changed = 0;
1898 	log_assert(tp && tp->autr);
1899 	/* autotrust update trust anchors */
1900 	/* the tp is locked, and stays locked unless it is deleted */
1901 
1902 	/* we could just catch the anchor here while another thread
1903 	 * is busy deleting it. Just unlock and let the other do its job */
1904 	if(tp->autr->revoked) {
1905 		log_nametypeclass(VERB_ALGO, "autotrust not processed, "
1906 			"trust point revoked", tp->name,
1907 			LDNS_RR_TYPE_DNSKEY, tp->dclass);
1908 		lock_basic_unlock(&tp->lock);
1909 		return 0; /* it is revoked */
1910 	}
1911 
1912 	/* query_dnskeys(): */
1913 	tp->autr->last_queried = *env->now;
1914 
1915 	log_nametypeclass(VERB_ALGO, "autotrust process for",
1916 		tp->name, LDNS_RR_TYPE_DNSKEY, tp->dclass);
1917 	/* see if time alone makes some keys valid */
1918 	autr_holddown_exceed(env, tp, &changed);
1919 	if(changed) {
1920 		verbose(VERB_ALGO, "autotrust: morekeys, reassemble");
1921 		if(!autr_assemble(tp)) {
1922 			log_err("malloc failure assembling autotrust keys");
1923 			return 1; /* unchanged */
1924 		}
1925 	}
1926 	/* did we get any data? */
1927 	if(!dnskey_rrset) {
1928 		verbose(VERB_ALGO, "autotrust: no dnskey rrset");
1929 		/* no update of query_failed, because then we would have
1930 		 * to write to disk. But we cannot because we maybe are
1931 		 * still 'initialising' with DS records, that we cannot write
1932 		 * in the full format (which only contains KSKs). */
1933 		return 1; /* trust point exists */
1934 	}
1935 	/* check for revoked keys to remove immediately */
1936 	check_contains_revoked(env, ve, tp, dnskey_rrset, &changed);
1937 	if(changed) {
1938 		verbose(VERB_ALGO, "autotrust: revokedkeys, reassemble");
1939 		if(!autr_assemble(tp)) {
1940 			log_err("malloc failure assembling autotrust keys");
1941 			return 1; /* unchanged */
1942 		}
1943 		if(!tp->ds_rrset && !tp->dnskey_rrset) {
1944 			/* no more keys, all are revoked */
1945 			/* this is a success for this probe attempt */
1946 			tp->autr->last_success = *env->now;
1947 			autr_tp_remove(env, tp, dnskey_rrset);
1948 			return 0; /* trust point removed */
1949 		}
1950 	}
1951 	/* verify the dnskey rrset and see if it is valid. */
1952 	if(!verify_dnskey(env, ve, tp, dnskey_rrset)) {
1953 		verbose(VERB_ALGO, "autotrust: dnskey did not verify.");
1954 		/* only increase failure count if this is not the first prime,
1955 		 * this means there was a previous succesful probe */
1956 		if(tp->autr->last_success) {
1957 			tp->autr->query_failed += 1;
1958 			autr_write_file(env, tp);
1959 		}
1960 		return 1; /* trust point exists */
1961 	}
1962 
1963 	tp->autr->last_success = *env->now;
1964 	tp->autr->query_failed = 0;
1965 
1966 	/* Add new trust anchors to the data structure
1967 	 * - note which trust anchors are seen this probe.
1968 	 * Set trustpoint query_interval and retry_time.
1969 	 * - find minimum rrsig expiration interval
1970 	 */
1971 	if(!update_events(env, ve, tp, dnskey_rrset, &changed)) {
1972 		log_err("malloc failure in autotrust update_events. "
1973 			"trust point unchanged.");
1974 		return 1; /* trust point unchanged, so exists */
1975 	}
1976 
1977 	/* - for every SEP key do the 5011 statetable.
1978 	 * - remove missing trustanchors (if veryold and we have new anchors).
1979 	 */
1980 	if(!do_statetable(env, tp, &changed)) {
1981 		log_err("malloc failure in autotrust do_statetable. "
1982 			"trust point unchanged.");
1983 		return 1; /* trust point unchanged, so exists */
1984 	}
1985 
1986 	autr_cleanup_keys(tp);
1987 	if(!set_next_probe(env, tp, dnskey_rrset))
1988 		return 0; /* trust point does not exist */
1989 	autr_write_file(env, tp);
1990 	if(changed) {
1991 		verbose(VERB_ALGO, "autotrust: changed, reassemble");
1992 		if(!autr_assemble(tp)) {
1993 			log_err("malloc failure assembling autotrust keys");
1994 			return 1; /* unchanged */
1995 		}
1996 		if(!tp->ds_rrset && !tp->dnskey_rrset) {
1997 			/* no more keys, all are revoked */
1998 			autr_tp_remove(env, tp, dnskey_rrset);
1999 			return 0; /* trust point removed */
2000 		}
2001 	} else verbose(VERB_ALGO, "autotrust: no changes");
2002 
2003 	return 1; /* trust point exists */
2004 }
2005 
2006 /** debug print a trust anchor key */
2007 static void
2008 autr_debug_print_ta(struct autr_ta* ta)
2009 {
2010 	char buf[32];
2011 	char* str = ldns_rr2str(ta->rr);
2012 	if(!str) {
2013 		log_info("out of memory in debug_print_ta");
2014 		return;
2015 	}
2016 	if(str && str[0]) str[strlen(str)-1]=0; /* remove newline */
2017 	ctime_r(&ta->last_change, buf);
2018 	if(buf[0]) buf[strlen(buf)-1]=0; /* remove newline */
2019 	log_info("[%s] %s ;;state:%d ;;pending_count:%d%s%s last:%s",
2020 		trustanchor_state2str(ta->s), str, ta->s, ta->pending_count,
2021 		ta->fetched?" fetched":"", ta->revoked?" revoked":"", buf);
2022 	free(str);
2023 }
2024 
2025 /** debug print a trust point */
2026 static void
2027 autr_debug_print_tp(struct trust_anchor* tp)
2028 {
2029 	struct autr_ta* ta;
2030 	char buf[257];
2031 	if(!tp->autr)
2032 		return;
2033 	dname_str(tp->name, buf);
2034 	log_info("trust point %s : %d", buf, (int)tp->dclass);
2035 	log_info("assembled %d DS and %d DNSKEYs",
2036 		(int)tp->numDS, (int)tp->numDNSKEY);
2037 	if(0) { /* turned off because it prints to stderr */
2038 		ldns_buffer* bf = ldns_buffer_new(70000);
2039 		ldns_rr_list* list;
2040 		if(tp->ds_rrset) {
2041 			list = packed_rrset_to_rr_list(tp->ds_rrset, bf);
2042 			ldns_rr_list_print(stderr, list);
2043 			ldns_rr_list_deep_free(list);
2044 		}
2045 		if(tp->dnskey_rrset) {
2046 			list = packed_rrset_to_rr_list(tp->dnskey_rrset, bf);
2047 			ldns_rr_list_print(stderr, list);
2048 			ldns_rr_list_deep_free(list);
2049 		}
2050 		ldns_buffer_free(bf);
2051 	}
2052 	log_info("file %s", tp->autr->file);
2053 	ctime_r(&tp->autr->last_queried, buf);
2054 	if(buf[0]) buf[strlen(buf)-1]=0; /* remove newline */
2055 	log_info("last_queried: %u %s", (unsigned)tp->autr->last_queried, buf);
2056 	ctime_r(&tp->autr->last_success, buf);
2057 	if(buf[0]) buf[strlen(buf)-1]=0; /* remove newline */
2058 	log_info("last_success: %u %s", (unsigned)tp->autr->last_success, buf);
2059 	ctime_r(&tp->autr->next_probe_time, buf);
2060 	if(buf[0]) buf[strlen(buf)-1]=0; /* remove newline */
2061 	log_info("next_probe_time: %u %s", (unsigned)tp->autr->next_probe_time,
2062 		buf);
2063 	log_info("query_interval: %u", (unsigned)tp->autr->query_interval);
2064 	log_info("retry_time: %u", (unsigned)tp->autr->retry_time);
2065 	log_info("query_failed: %u", (unsigned)tp->autr->query_failed);
2066 
2067 	for(ta=tp->autr->keys; ta; ta=ta->next) {
2068 		autr_debug_print_ta(ta);
2069 	}
2070 }
2071 
2072 void
2073 autr_debug_print(struct val_anchors* anchors)
2074 {
2075 	struct trust_anchor* tp;
2076 	lock_basic_lock(&anchors->lock);
2077 	RBTREE_FOR(tp, struct trust_anchor*, anchors->tree) {
2078 		lock_basic_lock(&tp->lock);
2079 		autr_debug_print_tp(tp);
2080 		lock_basic_unlock(&tp->lock);
2081 	}
2082 	lock_basic_unlock(&anchors->lock);
2083 }
2084 
2085 void probe_answer_cb(void* arg, int ATTR_UNUSED(rcode),
2086 	ldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(sec),
2087 	char* ATTR_UNUSED(why_bogus))
2088 {
2089 	/* retry was set before the query was done,
2090 	 * re-querytime is set when query succeeded, but that may not
2091 	 * have reset this timer because the query could have been
2092 	 * handled by another thread. In that case, this callback would
2093 	 * get called after the original timeout is done.
2094 	 * By not resetting the timer, it may probe more often, but not
2095 	 * less often.
2096 	 * Unless the new lookup resulted in smaller TTLs and thus smaller
2097 	 * timeout values. In that case one old TTL could be mistakenly done.
2098 	 */
2099 	struct module_env* env = (struct module_env*)arg;
2100 	verbose(VERB_ALGO, "autotrust probe answer cb");
2101 	reset_worker_timer(env);
2102 }
2103 
2104 /** probe a trust anchor DNSKEY and unlocks tp */
2105 static void
2106 probe_anchor(struct module_env* env, struct trust_anchor* tp)
2107 {
2108 	struct query_info qinfo;
2109 	uint16_t qflags = BIT_RD;
2110 	struct edns_data edns;
2111 	ldns_buffer* buf = env->scratch_buffer;
2112 	qinfo.qname = regional_alloc_init(env->scratch, tp->name, tp->namelen);
2113 	if(!qinfo.qname) {
2114 		log_err("out of memory making 5011 probe");
2115 		return;
2116 	}
2117 	qinfo.qname_len = tp->namelen;
2118 	qinfo.qtype = LDNS_RR_TYPE_DNSKEY;
2119 	qinfo.qclass = tp->dclass;
2120 	log_query_info(VERB_ALGO, "autotrust probe", &qinfo);
2121 	verbose(VERB_ALGO, "retry probe set in %d seconds",
2122 		(int)tp->autr->next_probe_time - (int)*env->now);
2123 	edns.edns_present = 1;
2124 	edns.ext_rcode = 0;
2125 	edns.edns_version = 0;
2126 	edns.bits = EDNS_DO;
2127 	if(ldns_buffer_capacity(buf) < 65535)
2128 		edns.udp_size = (uint16_t)ldns_buffer_capacity(buf);
2129 	else	edns.udp_size = 65535;
2130 
2131 	/* can't hold the lock while mesh_run is processing */
2132 	lock_basic_unlock(&tp->lock);
2133 
2134 	/* delete the DNSKEY from rrset and key cache so an active probe
2135 	 * is done. First the rrset so another thread does not use it
2136 	 * to recreate the key entry in a race condition. */
2137 	rrset_cache_remove(env->rrset_cache, qinfo.qname, qinfo.qname_len,
2138 		qinfo.qtype, qinfo.qclass, 0);
2139 	key_cache_remove(env->key_cache, qinfo.qname, qinfo.qname_len,
2140 		qinfo.qclass);
2141 
2142 	if(!mesh_new_callback(env->mesh, &qinfo, qflags, &edns, buf, 0,
2143 		&probe_answer_cb, env)) {
2144 		log_err("out of memory making 5011 probe");
2145 	}
2146 }
2147 
2148 /** fetch first to-probe trust-anchor and lock it and set retrytime */
2149 static struct trust_anchor*
2150 todo_probe(struct module_env* env, uint32_t* next)
2151 {
2152 	struct trust_anchor* tp;
2153 	rbnode_t* el;
2154 	/* get first one */
2155 	lock_basic_lock(&env->anchors->lock);
2156 	if( (el=rbtree_first(&env->anchors->autr->probe)) == RBTREE_NULL) {
2157 		/* in case of revoked anchors */
2158 		lock_basic_unlock(&env->anchors->lock);
2159 		return NULL;
2160 	}
2161 	tp = (struct trust_anchor*)el->key;
2162 	lock_basic_lock(&tp->lock);
2163 
2164 	/* is it eligible? */
2165 	if((uint32_t)tp->autr->next_probe_time > *env->now) {
2166 		/* no more to probe */
2167 		*next = (uint32_t)tp->autr->next_probe_time - *env->now;
2168 		lock_basic_unlock(&tp->lock);
2169 		lock_basic_unlock(&env->anchors->lock);
2170 		return NULL;
2171 	}
2172 
2173 	/* reset its next probe time */
2174 	(void)rbtree_delete(&env->anchors->autr->probe, tp);
2175 	tp->autr->next_probe_time = calc_next_probe(env, tp->autr->retry_time);
2176 	(void)rbtree_insert(&env->anchors->autr->probe, &tp->autr->pnode);
2177 	lock_basic_unlock(&env->anchors->lock);
2178 
2179 	return tp;
2180 }
2181 
2182 uint32_t
2183 autr_probe_timer(struct module_env* env)
2184 {
2185 	struct trust_anchor* tp;
2186 	uint32_t next_probe = 3600;
2187 	int num = 0;
2188 	verbose(VERB_ALGO, "autotrust probe timer callback");
2189 	/* while there are still anchors to probe */
2190 	while( (tp = todo_probe(env, &next_probe)) ) {
2191 		/* make a probe for this anchor */
2192 		probe_anchor(env, tp);
2193 		num++;
2194 	}
2195 	regional_free_all(env->scratch);
2196 	if(num == 0)
2197 		return 0; /* no trust points to probe */
2198 	verbose(VERB_ALGO, "autotrust probe timer %d callbacks done", num);
2199 	return next_probe;
2200 }
2201