ddns.c 20.7 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
}

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 167
	switch (rrset->rrs.count) {
	case 0:
168
		return true;
169 170 171 172
	case 1:
		return rrset->rrs.rdata->len == 0;
	default:
		return false;
173
	}
174 175
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
/*< \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;
	}
}

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

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

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

242
/* --------------------------- DDNS processing ------------------------------ */
243

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

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

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

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

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

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

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

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

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

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

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

310 311 312
/*!< \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)
{
313 314
	if (rr->type == KNOT_RRTYPE_SOA &&
	    (rr->rclass == KNOT_CLASS_NONE || rr->rclass == KNOT_CLASS_ANY ||
315
	     (serial_compare(knot_soa_serial(rr->rrs.rdata), sn) != SERIAL_GREATER))) {
316
		return true;
317
	}
Jan Včelák's avatar
Jan Včelák committed
318

319
	return false;
320 321
}

322 323
/* ---------------------- changeset manipulation ---------------------------- */

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

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

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

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

347
	return true;
348
}
349

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

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

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

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

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

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

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

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

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

414 415 416
	return KNOT_EOK;
}

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

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

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

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

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

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

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

475
/* --------------------------- RR deletions --------------------------------- */
476

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

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

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

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

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

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

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

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

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

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

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

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

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

561
	// Remove all RRSets from node
562 563 564 565
	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);
566
		if (ret != KNOT_EOK) {
567
			node_free(node_copy, NULL);
568 569
			return ret;
		}
570 571
	}

572
	node_free(node_copy, NULL);
573

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

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

593
/* --------------------------- validity checks ------------------------------ */
594

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

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

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

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

621
	return true;
622 623
}

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

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

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

666 667 668
	return KNOT_EOK;
}

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

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

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

701
/* ---------------------------------- API ----------------------------------- */
702

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

710 711 712 713 714
	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);
715
	const knot_rrset_t *answer_rr = knot_pkt_rr(answer, 0);
716 717
	for (int i = 0; i < answer->count; ++i) {
		// Check what can be checked, store full RRs into list
718
		ret = process_prereq(&answer_rr[i], knot_pkt_qclass(query),
719
		                     update, rcode, &rrset_list);
720 721 722
		if (ret != KNOT_EOK) {
			rrset_list_clear(&rrset_list);
			return ret;
723 724
		}
	}
725 726

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

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

742
	uint32_t sn_old = knot_soa_serial(zone_update_from(update)->rdata);
743

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

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

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

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