ddns.c 20.8 KB
Newer Older
1
/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

17 18
#include <assert.h>

19
#include "knot/common/log.h"
20 21
#include "knot/updates/ddns.h"
#include "knot/updates/changesets.h"
22
#include "knot/updates/zone-update.h"
23
#include "knot/zone/serial.h"
Daniel Salzman's avatar
Daniel Salzman committed
24
#include "libknot/libknot.h"
25
#include "contrib/ucw/lists.h"
26

27
/* ----------------------------- prereq check ------------------------------- */
28

29
/*!< \brief Clears prereq RRSet list. */
30
static void rrset_list_clear(list_t *l)
31
{
32 33 34 35
	node_t *n, *nxt;
	WALK_LIST_DELSAFE(n, nxt, *l) {
		ptrnode_t *ptr_n = (ptrnode_t *)n;
		knot_rrset_t *rrset = (knot_rrset_t *)ptr_n->d;
36
		knot_rrset_free(rrset, NULL);
37 38
		free(n);
	};
39 40
}

41
/*!< \brief Adds RR to prereq RRSet list, merges RRs into RRSets. */
42
static int add_rr_to_list(list_t *l, const knot_rrset_t *rr)
43
{
44 45 46 47 48
	node_t *n;
	WALK_LIST(n, *l) {
		ptrnode_t *ptr_n = (ptrnode_t *)n;
		knot_rrset_t *rrset = (knot_rrset_t *)ptr_n->d;
		if (knot_rrset_equal(rr, rrset, KNOT_RRSET_COMPARE_HEADER)) {
49
			return knot_rdataset_merge(&rrset->rrs, &rr->rrs, NULL);
50
		}
51
	};
52

53
	knot_rrset_t *rr_copy = knot_rrset_copy(rr, NULL);
54
	if (rr_copy == NULL) {
55 56
		return KNOT_ENOMEM;
	}
57
	return ptrlist_add(l, rr_copy, NULL) != NULL ? KNOT_EOK : KNOT_ENOMEM;
58 59
}

60
/*!< \brief Checks whether RRSet exists in the zone. */
61 62
static int check_rrset_exists(zone_update_t *update, const knot_rrset_t *rrset,
                              uint16_t *rcode)
63
{
64
	assert(rrset->type != KNOT_RRTYPE_ANY);
65

66
	const zone_node_t *node = zone_update_get_node(update, rrset->owner);
67
	if (node == NULL || !node_rrtype_exists(node, rrset->type)) {
68 69 70
		*rcode = KNOT_RCODE_NXRRSET;
		return KNOT_EPREREQ;
	} else {
71
		knot_rrset_t found = node_rrset(node, rrset->type);
72
		assert(!knot_rrset_empty(&found));
73 74 75
		if (knot_rrset_equal(&found, rrset, KNOT_RRSET_COMPARE_WHOLE)) {
			return KNOT_EOK;
		} else {
76 77 78 79
			*rcode = KNOT_RCODE_NXRRSET;
			return KNOT_EPREREQ;
		}
	}
80 81
}

82
/*!< \brief Checks whether RRSets in the list exist in the zone. */
83
static int check_stored_rrsets(list_t *l, zone_update_t *update,
84
                               uint16_t *rcode)
85 86 87 88 89
{
	node_t *n;
	WALK_LIST(n, *l) {
		ptrnode_t *ptr_n = (ptrnode_t *)n;
		knot_rrset_t *rrset = (knot_rrset_t *)ptr_n->d;
90
		int ret = check_rrset_exists(update, rrset, rcode);
91 92 93 94
		if (ret != KNOT_EOK) {
			return ret;
		}
	};
95

96 97
	return KNOT_EOK;
}
98

99
/*!< \brief Checks whether node of given owner, with given type exists. */
100
static bool check_type(zone_update_t *update, const knot_rrset_t *rrset)
101
{
102
	assert(rrset->type != KNOT_RRTYPE_ANY);
103
	const zone_node_t *node = zone_update_get_node(update, rrset->owner);
Lubos Slovak's avatar
Lubos Slovak committed
104
	if (node == NULL || !node_rrtype_exists(node, rrset->type)) {
105
		return false;
Lubos Slovak's avatar
Lubos Slovak committed
106
	}
107

108
	return true;
Lubos Slovak's avatar
Lubos Slovak committed
109 110 111
}

/*!< \brief Checks whether RR type exists in the zone. */
112
static int check_type_exist(zone_update_t *update,
Lubos Slovak's avatar
Lubos Slovak committed
113 114 115
                            const knot_rrset_t *rrset, uint16_t *rcode)
{
	assert(rrset->rclass == KNOT_CLASS_ANY);
116
	if (check_type(update, rrset)) {
117 118
		return KNOT_EOK;
	} else {
Lubos Slovak's avatar
Lubos Slovak committed
119
		*rcode = KNOT_RCODE_NXRRSET;
120
		return KNOT_EPREREQ;
121
	}
Lubos Slovak's avatar
Lubos Slovak committed
122 123 124
}

/*!< \brief Checks whether RR type is not in the zone. */
125
static int check_type_not_exist(zone_update_t *update,
Lubos Slovak's avatar
Lubos Slovak committed
126 127 128
                                const knot_rrset_t *rrset, uint16_t *rcode)
{
	assert(rrset->rclass == KNOT_CLASS_NONE);
129
	if (check_type(update, rrset)) {
Lubos Slovak's avatar
Lubos Slovak committed
130 131 132 133 134
		*rcode = KNOT_RCODE_YXRRSET;
		return KNOT_EPREREQ;
	} else {
		return KNOT_EOK;
	}
135 136
}

137
/*!< \brief Checks whether DNAME is in the zone. */
138
static int check_in_use(zone_update_t *update,
139
                        const knot_dname_t *dname, uint16_t *rcode)
140
{
141
	const zone_node_t *node = zone_update_get_node(update, dname);
142
	if (node == NULL || node->rrset_count == 0) {
143 144
		*rcode = KNOT_RCODE_NXDOMAIN;
		return KNOT_EPREREQ;
145 146
	} else {
		return KNOT_EOK;
147
	}
148 149
}

150
/*!< \brief Checks whether DNAME is not in the zone. */
151
static int check_not_in_use(zone_update_t *update,
152
                            const knot_dname_t *dname, uint16_t *rcode)
153
{
154
	const zone_node_t *node = zone_update_get_node(update, dname);
155
	if (node == NULL || node->rrset_count == 0) {
156
		return KNOT_EOK;
157 158 159
	} else {
		*rcode = KNOT_RCODE_YXDOMAIN;
		return KNOT_EPREREQ;
160
	}
161 162
}

Lubos Slovak's avatar
Lubos Slovak committed
163
/*!< \brief Returns true if rrset has 0 data or RDATA of size 0 (we need TTL).*/
164
static bool rrset_empty(const knot_rrset_t *rrset)
165
{
166
	uint16_t rr_count = rrset->rrs.rr_count;
167 168
	if (rr_count == 0) {
		return true;
169
	}
170
	if (rr_count == 1) {
171
		const knot_rdata_t *rr = knot_rdataset_at(&rrset->rrs, 0);
172
		return rr->len == 0;
173
	}
174
	return false;
175 176
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
/*< \brief Returns true if DDNS should deny updating DNSSEC-related record. */
static bool is_dnssec_protected(uint16_t type, bool is_apex)
{
	switch (type) {
	case KNOT_RRTYPE_RRSIG:
	case KNOT_RRTYPE_NSEC:
	case KNOT_RRTYPE_NSEC3:
	case KNOT_RRTYPE_CDNSKEY:
	case KNOT_RRTYPE_CDS:
		return true;
	case KNOT_RRTYPE_DNSKEY:
	case KNOT_RRTYPE_NSEC3PARAM:
		return is_apex;
	default:
		return false;
	}
}

195
/*!< \brief Checks prereq for given packet RR. */
196
static int process_prereq(const knot_rrset_t *rrset, uint16_t qclass,
197
                          zone_update_t *update, uint16_t *rcode,
198
                          list_t *rrset_list)
199
{
200
	if (rrset->ttl != 0) {
201
		*rcode = KNOT_RCODE_FORMERR;
202
		return KNOT_EMALF;
203 204
	}

205
	if (!knot_dname_in(update->zone->name, rrset->owner)) {
206
		*rcode = KNOT_RCODE_NOTZONE;
207
		return KNOT_EOUTOFZONE;
208
	}
209

210 211
	if (rrset->rclass == KNOT_CLASS_ANY) {
		if (!rrset_empty(rrset)) {
212
			*rcode = KNOT_RCODE_FORMERR;
213 214
			return KNOT_EMALF;
		}
215
		if (rrset->type == KNOT_RRTYPE_ANY) {
216
			return check_in_use(update, rrset->owner, rcode);
217
		} else {
218
			return check_type_exist(update, rrset, rcode);
219
		}
220 221
	} else if (rrset->rclass == KNOT_CLASS_NONE) {
		if (!rrset_empty(rrset)) {
222
			*rcode = KNOT_RCODE_FORMERR;
223 224
			return KNOT_EMALF;
		}
225
		if (rrset->type == KNOT_RRTYPE_ANY) {
226
			return check_not_in_use(update, rrset->owner, rcode);
227
		} else {
228
			return check_type_not_exist(update, rrset, rcode);
229 230
		}
	} else if (rrset->rclass == qclass) {
231
		// Store RRs for full check into list
232 233 234 235 236
		int ret = add_rr_to_list(rrset_list, rrset);
		if (ret != KNOT_EOK) {
			*rcode = KNOT_RCODE_SERVFAIL;
		}
		return ret;
237
	} else {
238
		*rcode = KNOT_RCODE_FORMERR;
239 240 241 242
		return KNOT_EMALF;
	}
}

243
/* --------------------------- DDNS processing ------------------------------ */
244

245
/* --------------------- true/false helper functions ------------------------ */
246

247
static inline bool is_addition(const knot_rrset_t *rr)
248
{
249 250
	return rr->rclass == KNOT_CLASS_IN;
}
Jan Včelák's avatar
Jan Včelák committed
251

252
static inline bool is_removal(const knot_rrset_t *rr)
253 254
{
	return rr->rclass == KNOT_CLASS_NONE || rr->rclass == KNOT_CLASS_ANY;
255 256
}

257 258 259 260
static inline bool is_rr_removal(const knot_rrset_t *rr)
{
	return rr->rclass == KNOT_CLASS_NONE;
}
261

262
static inline bool is_rrset_removal(const knot_rrset_t *rr)
263
{
264 265
	return rr->rclass == KNOT_CLASS_ANY && rr->type != KNOT_RRTYPE_ANY;
}
266

267 268 269 270
static inline bool is_node_removal(const knot_rrset_t *rr)
{
	return rr->rclass == KNOT_CLASS_ANY && rr->type == KNOT_RRTYPE_ANY;
}
271

272
/*!< \brief Returns true if last addition of certain types is to be replaced. */
273
static bool should_replace(const knot_rrset_t *rrset)
274
{
275 276
	return rrset->type == KNOT_RRTYPE_CNAME ||
	       rrset->type == KNOT_RRTYPE_NSEC3PARAM;
277 278
}

279
/*!< \brief Returns true if node contains given RR in its RRSets. */
280
static bool node_contains_rr(const zone_node_t *node,
281
                             const knot_rrset_t *rr)
282
{
283
	const knot_rdataset_t *zone_rrs = node_rdataset(node, rr->type);
284 285
	if (zone_rrs) {
		assert(rr->rrs.rr_count == 1);
286
		return knot_rdataset_member(zone_rrs, knot_rdataset_at(&rr->rrs, 0));
287
	} else {
288
		return false;
289 290 291
	}
}

292
/*!< \brief Returns true if CNAME is in this node. */
293
static bool adding_to_cname(const knot_dname_t *owner,
294
                            const zone_node_t *node)
295
{
296 297 298
	if (node == NULL) {
		// Node did not exist before update.
		return false;
299
	}
Jan Včelák's avatar
Jan Včelák committed
300

301
	knot_rrset_t cname = node_rrset(node, KNOT_RRTYPE_CNAME);
302 303 304 305
	if (knot_rrset_empty(&cname)) {
		// Node did not contain CNAME before update.
		return false;
	}
306

307 308
	// CNAME present
	return true;
309
}
Jan Včelák's avatar
Jan Včelák committed
310

311 312 313
/*!< \brief Used to ignore SOA deletions and SOAs with lower serial than zone. */
static bool skip_soa(const knot_rrset_t *rr, int64_t sn)
{
314 315
	if (rr->type == KNOT_RRTYPE_SOA &&
	    (rr->rclass == KNOT_CLASS_NONE || rr->rclass == KNOT_CLASS_ANY ||
316
	     (serial_compare(knot_soa_serial(&rr->rrs), sn) != SERIAL_GREATER))) {
317
		return true;
318
	}
Jan Včelák's avatar
Jan Včelák committed
319

320
	return false;
321 322
}

323 324
/* ---------------------- changeset manipulation ---------------------------- */

325 326 327
/*!< \brief Replaces possible singleton RR type in changeset. */
static bool singleton_replaced(changeset_t *changeset,
                               const knot_rrset_t *rr)
328
{
329 330 331
	if (!should_replace(rr)) {
		return false;
	}
332

333 334 335
	zone_node_t *n = zone_contents_find_node_for_rr(changeset->add, rr);
	if (n == NULL) {
		return false;
336
	}
337

338 339 340 341
	knot_rdataset_t *rrs = node_rdataset(n, rr->type);
	if (rrs == NULL) {
		return false;
	}
342

343 344 345
	// Replace singleton RR.
	knot_rdataset_clear(rrs, NULL);
	node_remove_rdataset(n, rr->type);
346
	node_add_rrset(n, rr, NULL);
347

348
	return true;
349
}
350

351
/*!< \brief Adds RR into add section of changeset if it is deemed worthy. */
352
static int add_rr_to_chgset(const knot_rrset_t *rr,
353
                            zone_update_t *update)
354
{
355
	if (singleton_replaced(&update->change, rr)) {
356
		return KNOT_EOK;
357
	}
Jan Včelák's avatar
Jan Včelák committed
358

359
	return zone_update_add(update, rr);
360
}
Jan Včelák's avatar
Jan Včelák committed
361

362
/* ------------------------ RR processing logic ----------------------------- */
Lubos Slovak's avatar
Lubos Slovak committed
363

364
/* --------------------------- RR additions --------------------------------- */
Jan Včelák's avatar
Jan Včelák committed
365

366
/*!< \brief Processes CNAME addition (replace or ignore) */
367
static int process_add_cname(const zone_node_t *node,
368
                             const knot_rrset_t *rr,
369
                             zone_update_t *update)
370
{
371
	knot_rrset_t cname = node_rrset(node, KNOT_RRTYPE_CNAME);
372
	if (!knot_rrset_empty(&cname)) {
373
		// If they are identical, ignore.
374
		if (knot_rrset_equal(&cname, rr, KNOT_RRSET_COMPARE_WHOLE)) {
375
			return KNOT_EOK;
Lubos Slovak's avatar
Lubos Slovak committed
376
		}
377

378
		int ret = zone_update_remove(update, &cname);
379
		if (ret != KNOT_EOK) {
380
			return ret;
381
		}
Jan Včelák's avatar
Jan Včelák committed
382

383
		return add_rr_to_chgset(rr, update);
384
	} else if (!node_empty(node)) {
385
		// Other occupied node => ignore.
Lubos Slovak's avatar
Lubos Slovak committed
386
		return KNOT_EOK;
387
	} else {
388
		// Can add.
389
		return add_rr_to_chgset(rr, update);
Lubos Slovak's avatar
Lubos Slovak committed
390 391
	}
}
Jan Včelák's avatar
Jan Včelák committed
392

393
/*!< \brief Processes NSEC3PARAM addition (ignore when not removed, or non-apex) */
394
static int process_add_nsec3param(const zone_node_t *node,
395
                                  const knot_rrset_t *rr,
396
                                  zone_update_t *update)
397
{
398
	if (node == NULL || !node_rrtype_exists(node, KNOT_RRTYPE_SOA)) {
399
		// Ignore non-apex additions
400
		char *owner = knot_dname_to_str_alloc(rr->owner);
Jan Včelák's avatar
Jan Včelák committed
401 402
		log_warning("DDNS, refusing to add NSEC3PARAM to non-apex "
		            "node '%s'", owner);
403
		free(owner);
404
		return KNOT_EDENIED;
405
	}
406
	knot_rrset_t param = node_rrset(node, KNOT_RRTYPE_NSEC3PARAM);
407
	if (knot_rrset_empty(&param)) {
408
		return add_rr_to_chgset(rr, update);
409
	}
Jan Včelák's avatar
Jan Včelák committed
410

411
	char *owner = knot_dname_to_str_alloc(rr->owner);
Jan Včelák's avatar
Jan Včelák committed
412
	log_warning("DDNS, refusing to add second NSEC3PARAM to node '%s'", owner);
413
	free(owner);
414

415 416 417
	return KNOT_EOK;
}

418
/*!
419 420
 * \brief Processes SOA addition (ignore when non-apex), lower serials
 *        dropped before.
421
 */
422
static int process_add_soa(const zone_node_t *node,
423
                           const knot_rrset_t *rr,
424
                           zone_update_t *update)
425
{
426
	if (node == NULL || !node_rrtype_exists(node, KNOT_RRTYPE_SOA)) {
427
		// Adding SOA to non-apex node, ignore.
Lubos Slovak's avatar
Lubos Slovak committed
428 429
		return KNOT_EOK;
	}
430

431
	// Get current SOA RR.
432
	knot_rrset_t removed = node_rrset(node, KNOT_RRTYPE_SOA);
433
	if (knot_rrset_equal(&removed, rr, KNOT_RRSET_COMPARE_WHOLE)) {
434
		// If they are identical, ignore.
435 436
		return KNOT_EOK;
	}
Jan Včelák's avatar
Jan Včelák committed
437

438
	return add_rr_to_chgset(rr, update);
439
}
440

441
/*!< \brief Adds normal RR, ignores when CNAME exists in node. */
442
static int process_add_normal(const zone_node_t *node,
443
                              const knot_rrset_t *rr,
444
                              zone_update_t *update)
445
{
446
	if (adding_to_cname(rr->owner, node)) {
447 448
		// Adding RR to CNAME node, ignore.
		return KNOT_EOK;
449
	}
450

451
	if (node && node_contains_rr(node, rr)) {
452
		// Adding existing RR, ignore.
453 454 455
		return KNOT_EOK;
	}

456
	return add_rr_to_chgset(rr, update);
457
}
458

459 460
/*!< \brief Decides what to do with RR addition. */
static int process_add(const knot_rrset_t *rr,
461
                       const zone_node_t *node,
462
                       zone_update_t *update)
463 464 465
{
	switch(rr->type) {
	case KNOT_RRTYPE_CNAME:
466
		return process_add_cname(node, rr, update);
467
	case KNOT_RRTYPE_SOA:
468
		return process_add_soa(node, rr, update);
469
	case KNOT_RRTYPE_NSEC3PARAM:
470
		return process_add_nsec3param(node, rr, update);
471
	default:
472
		return process_add_normal(node, rr, update);
473
	}
474
}
475

476
/* --------------------------- RR deletions --------------------------------- */
477

478
/*!< \brief Removes single RR from zone. */
479
static int process_rem_rr(const knot_rrset_t *rr,
480
                          const zone_node_t *node,
481
                          zone_update_t *update)
482
{
483 484 485 486 487
	if (node == NULL) {
		// Removing from node that does not exist
		return KNOT_EOK;
	}

488
	const bool apex_ns = node_rrtype_exists(node, KNOT_RRTYPE_SOA) &&
489
	                     rr->type == KNOT_RRTYPE_NS;
490
	if (apex_ns) {
491 492
		const knot_rdataset_t *ns_rrs =
			node_rdataset(node, KNOT_RRTYPE_NS);
493 494 495 496
		if (ns_rrs == NULL) {
			// Zone without apex NS.
			return KNOT_EOK;
		}
497
		if (ns_rrs->rr_count == 1) {
498
			// Cannot remove last apex NS RR.
499 500
			return KNOT_EOK;
		}
501 502
	}

503
	knot_rrset_t to_modify = node_rrset(node, rr->type);
504
	if (knot_rrset_empty(&to_modify)) {
505
		// No such RRSet
506
		return KNOT_EOK;
507
	}
508

509
	knot_rdataset_t *rrs = node_rdataset(node, rr->type);
510
	if (!knot_rdataset_member(rrs, rr->rrs.data)) {
511
		// Node does not contain this RR
512
		return KNOT_EOK;
513
	}
514

515
	return zone_update_remove(update, rr);
516 517
}

518
/*!< \brief Removes RRSet from zone. */
519
static int process_rem_rrset(const knot_rrset_t *rrset,
520
                             const zone_node_t *node,
521
                             zone_update_t *update)
522
{
523 524 525
	bool is_apex = node_rrtype_exists(node, KNOT_RRTYPE_SOA);

	if (rrset->type == KNOT_RRTYPE_SOA || is_dnssec_protected(rrset->type, is_apex)) {
526
		// Ignore SOA and DNSSEC removals.
527 528
		return KNOT_EOK;
	}
Jan Včelák's avatar
Jan Včelák committed
529

530
	if (is_apex && rrset->type == KNOT_RRTYPE_NS) {
531
		// Ignore NS apex RRSet removals.
532
		return KNOT_EOK;
533
	}
534

535
	if (node == NULL) {
536
		// no such node in zone, ignore
537 538
		return KNOT_EOK;
	}
Jan Včelák's avatar
Jan Včelák committed
539

540
	if (!node_rrtype_exists(node, rrset->type)) {
541
		// no such RR, ignore
542
		return KNOT_EOK;
543
	}
544

545
	knot_rrset_t to_remove = node_rrset(node, rrset->type);
546
	return zone_update_remove(update, &to_remove);
547 548
}

549
/*!< \brief Removes node from zone. */
550
static int process_rem_node(const knot_rrset_t *rr,
551
                            const zone_node_t *node, zone_update_t *update)
552
{
553
	if (node == NULL) {
554 555 556
		return KNOT_EOK;
	}

557
	zone_node_t *node_copy = node_shallow_copy(node, NULL);
558 559 560
	if (node_copy == NULL) {
		return KNOT_ENOMEM;
	}
561

562
	// Remove all RRSets from node
563 564 565 566
	size_t rrset_count = node_copy->rrset_count;
	for (int i = 0; i < rrset_count; ++i) {
		knot_rrset_t rrset = node_rrset_at(node_copy, rrset_count - i - 1);
		int ret = process_rem_rrset(&rrset, node_copy, update);
567
		if (ret != KNOT_EOK) {
568
			node_free(node_copy, NULL);
569 570
			return ret;
		}
571 572
	}

573
	node_free(node_copy, NULL);
574

575 576
	return KNOT_EOK;
}
Jan Včelák's avatar
Jan Včelák committed
577

578
/*!< \brief Decides what to with removal. */
579
static int process_remove(const knot_rrset_t *rr,
580
                          const zone_node_t *node,
581
                          zone_update_t *update)
582 583
{
	if (is_rr_removal(rr)) {
584
		return process_rem_rr(rr, node, update);
585
	} else if (is_rrset_removal(rr)) {
586
		return process_rem_rrset(rr, node, update);
587
	} else if (is_node_removal(rr)) {
588
		return process_rem_node(rr, node, update);
589 590
	} else {
		return KNOT_EINVAL;
591
	}
592
}
Jan Včelák's avatar
Jan Včelák committed
593

594
/* --------------------------- validity checks ------------------------------ */
595

596
/*!< \brief Checks whether addition has not violated DNAME rules. */
597 598
static bool sem_check(const knot_rrset_t *rr, const zone_node_t *zone_node,
                      zone_update_t *update)
599
{
600
	// Check that we have not added DNAME child
601
	const knot_dname_t *parent_dname = knot_wire_next_label(rr->owner, NULL);
602
	const zone_node_t *parent = zone_update_get_node(update, parent_dname);
603 604
	if (parent == NULL) {
		return true;
605
	}
Jan Včelák's avatar
Jan Včelák committed
606

607
	if (node_rrtype_exists(parent, KNOT_RRTYPE_DNAME)) {
608
		// Parent has DNAME RRSet, refuse update
609
		return false;
610
	}
611

612
	if (rr->type != KNOT_RRTYPE_DNAME || zone_node == NULL) {
613
		return true;
614
	}
Jan Včelák's avatar
Jan Včelák committed
615

616
	// Check that we have not created node with DNAME children.
617
	if (zone_node->children > 0) {
618 619
		// Updated node has children and DNAME was added, refuse update
		return false;
620
	}
621

622
	return true;
623 624
}

625
/*!< \brief Checks whether we can accept this RR. */
626 627
static int check_update(const knot_rrset_t *rrset, const knot_pkt_t *query,
                        uint16_t *rcode)
628
{
629 630 631
	/* Accept both subdomain and dname match. */
	const knot_dname_t *owner = rrset->owner;
	const knot_dname_t *qname = knot_pkt_qname(query);
632
	const bool is_sub = knot_dname_is_sub(owner, qname);
633 634
	const bool is_apex = knot_dname_is_equal(owner, qname);
	if (!is_sub && !is_apex) {
635 636
		*rcode = KNOT_RCODE_NOTZONE;
		return KNOT_EOUTOFZONE;
637 638
	}

639
	if (is_dnssec_protected(rrset->type, is_apex)) {
640
		*rcode = KNOT_RCODE_REFUSED;
Jan Včelák's avatar
Jan Včelák committed
641
		log_warning("DDNS, refusing to update DNSSEC-related record");
642 643
		return KNOT_EDENIED;
	}
644

645 646 647 648
	if (rrset->rclass == knot_pkt_qclass(query)) {
		if (knot_rrtype_is_metatype(rrset->type)) {
			*rcode = KNOT_RCODE_FORMERR;
			return KNOT_EMALF;
649
		}
650
	} else if (rrset->rclass == KNOT_CLASS_ANY) {
651 652 653
		if (!rrset_empty(rrset) ||
		    (knot_rrtype_is_metatype(rrset->type) &&
		     rrset->type != KNOT_RRTYPE_ANY)) {
654 655
			*rcode = KNOT_RCODE_FORMERR;
			return KNOT_EMALF;
656
		}
657
	} else if (rrset->rclass == KNOT_CLASS_NONE) {
658
		if (rrset->ttl != 0 || knot_rrtype_is_metatype(rrset->type)) {
659 660
			*rcode = KNOT_RCODE_FORMERR;
			return KNOT_EMALF;
661
		}
662 663 664
	} else {
		*rcode = KNOT_RCODE_FORMERR;
		return KNOT_EMALF;
665
	}
Jan Včelák's avatar
Jan Včelák committed
666

667 668 669
	return KNOT_EOK;
}

670
/*!< \brief Checks RR and decides what to do with it. */
671
static int process_rr(const knot_rrset_t *rr, zone_update_t *update)
672
{
673
	const zone_node_t *node = zone_update_get_node(update, rr->owner);
674

675
	if (is_addition(rr)) {
676
		int ret = process_add(rr, node, update);
677
		if (ret == KNOT_EOK) {
678
			if (!sem_check(rr, node, update)) {
679 680 681 682
				return KNOT_EDENIED;
			}
		}
		return ret;
683
	} else if (is_removal(rr)) {
684
		return process_remove(rr, node, update);
685
	} else {
686
		return KNOT_EMALF;
687 688 689
	}
}

690
/*!< \brief Maps Knot return code to RCODE. */
691
static uint16_t ret_to_rcode(int ret)
692
{
693 694
	if (ret == KNOT_EMALF) {
		return KNOT_RCODE_FORMERR;
695
	} else if (ret == KNOT_EDENIED) {
696 697 698 699 700
		return KNOT_RCODE_REFUSED;
	} else {
		return KNOT_RCODE_SERVFAIL;
	}
}
701

702
/* ---------------------------------- API ----------------------------------- */
703

704
int ddns_process_prereqs(const knot_pkt_t *query, zone_update_t *update,
705
                         uint16_t *rcode)
706
{
707
	if (query == NULL || rcode == NULL || update == NULL) {
708 709
		return KNOT_EINVAL;
	}
710

711 712 713 714 715
	int ret = KNOT_EOK;
	list_t rrset_list; // List used to store merged RRSets
	init_list(&rrset_list);

	const knot_pktsection_t *answer = knot_pkt_section(query, KNOT_ANSWER);
716
	const knot_rrset_t *answer_rr = knot_pkt_rr(answer, 0);
717 718
	for (int i = 0; i < answer->count; ++i) {
		// Check what can be checked, store full RRs into list
719
		ret = process_prereq(&answer_rr[i], knot_pkt_qclass(query),
720
		                     update, rcode, &rrset_list);
721 722 723
		if (ret != KNOT_EOK) {
			rrset_list_clear(&rrset_list);
			return ret;
724 725
		}
	}
726 727

	// Check stored RRSets
728
	ret = check_stored_rrsets(&rrset_list, update, rcode);
729 730
	rrset_list_clear(&rrset_list);
	return ret;
731 732
}

733
int ddns_process_update(const zone_t *zone, const knot_pkt_t *query,
734
                        zone_update_t *update, uint16_t *rcode)
735
{
736
	if (zone == NULL || query == NULL || update == NULL || rcode == NULL) {
737 738 739
		if (rcode) {
			*rcode = ret_to_rcode(KNOT_EINVAL);
		}
740 741
		return KNOT_EINVAL;
	}
742

743
	uint32_t sn_old = knot_soa_serial(zone_update_from(update));
744

745
	// Process all RRs in the authority section.
746 747
	const knot_pktsection_t *authority = knot_pkt_section(query, KNOT_AUTHORITY);
	const knot_rrset_t *authority_rr = knot_pkt_rr(authority, 0);
748
	for (uint16_t i = 0; i < authority->count; ++i) {
749
		const knot_rrset_t *rr = &authority_rr[i];
750
		// Check if RR is correct.
751
		int ret = check_update(rr, query, rcode);
752
		if (ret != KNOT_EOK) {
753
			assert(*rcode != KNOT_RCODE_NOERROR);
754 755 756
			return ret;
		}

757
		if (skip_soa(rr, sn_old)) {
758 759 760
			continue;
		}

761
		ret = process_rr(rr, update);
762
		if (ret != KNOT_EOK) {
763
			*rcode = ret_to_rcode(ret);
764 765 766 767
			return ret;
		}
	}

768
	*rcode = KNOT_RCODE_NOERROR;
769
	return KNOT_EOK;
770
}