1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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/>.
*/
#include <assert.h>
#include "updates/ddns.h"
#include "updates/changesets.h"
#include "util/debug.h"
#include "packet/packet.h"
#include "consts.h"
/*----------------------------------------------------------------------------*/
// Copied from XFR - maybe extract somewhere else
static int knot_ddns_prereq_check_rrsets(knot_rrset_t ***rrsets,
size_t *count, size_t *allocated)
{
int new_count = 0;
if (*count == *allocated) {
new_count = *allocated * 2;
}
knot_rrset_t **rrsets_new =
(knot_rrset_t **)calloc(new_count, sizeof(knot_rrset_t *));
if (rrsets_new == NULL) {
return KNOT_ENOMEM;
}
memcpy(rrsets_new, *rrsets, (*count) * sizeof(knot_rrset_t *));
*rrsets = rrsets_new;
*allocated = new_count;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_prereq_check_dnames(knot_dname_t ***dnames,
size_t *count, size_t *allocated)
{
int new_count = 0;
if (*count == *allocated) {
new_count = *allocated * 2;
}
knot_dname_t **dnames_new =
(knot_dname_t **)calloc(new_count, sizeof(knot_dname_t *));
if (dnames_new == NULL) {
return KNOT_ENOMEM;
}
memcpy(dnames_new, *dnames, (*count) * sizeof(knot_dname_t *));
*dnames = dnames_new;
*allocated = new_count;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_prereq_rrset(const knot_rrset_t *rrset,
knot_rrset_t ***rrsets,
size_t *count, size_t *allocd)
{
// check if such RRSet is not already there and merge if needed
int ret;
for (int i = 0; i < *count; ++i) {
if (knot_rrset_compare(rrset, (*rrsets)[i],
KNOT_RRSET_COMPARE_HEADER) == 0) {
ret = knot_rrset_merge((void **)&((*rrsets)[i]),
(void **)&rrset);
if (ret != KNOT_EOK) {
return ret;
} else {
return KNOT_EOK;
}
}
}
// if we are here, the RRSet was not found
ret = knot_ddns_prereq_check_rrsets(rrsets, count, allocd);
if (ret != KNOT_EOK) {
return ret;
}
knot_rrset_t *new_rrset = NULL;
ret = knot_rrset_deep_copy(rrset, &new_rrset, 0);
if (ret != KNOT_EOK) {
return ret;
}
(*rrsets)[(*count)++] = new_rrset;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_prereq_dname(const knot_dname_t *dname,
knot_dname_t ***dnames,
size_t *count, size_t *allocd)
{
// we do not have to check if the name is not already there
// if it is, we will just check it twice in the zone
int ret = knot_ddns_prereq_check_dnames(dnames, count, allocd);
if (ret != KNOT_EOK) {
return ret;
}
knot_dname_t *dname_new = knot_dname_deep_copy(dname);
if (dname_new == NULL) {
return KNOT_ENOMEM;
}
(*dnames)[(*count)++] = dname_new;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_prereq(knot_ddns_prereq_t *prereqs,
const knot_rrset_t *rrset, uint16_t qclass)
{
assert(prereqs != NULL);
assert(rrset != NULL);
if (knot_rrset_ttl(rrset) != 0) {
return KNOT_EMALF;
}
int ret;
if (knot_rrset_class(rrset) == KNOT_CLASS_ANY) {
if (knot_rrset_rdata(rrset) != NULL) {
return KNOT_EMALF;
}
if (knot_rrset_type(rrset) == KNOT_RRTYPE_ANY) {
ret = knot_ddns_add_prereq_dname(
knot_rrset_owner(rrset), &prereqs->in_use,
&prereqs->in_use_count,
&prereqs->in_use_allocd);
} else {
ret = knot_ddns_add_prereq_rrset(rrset,
&prereqs->exist,
&prereqs->exist_count,
&prereqs->exist_allocd);
}
} else if (knot_rrset_class(rrset) == KNOT_CLASS_NONE) {
if (knot_rrset_rdata(rrset) != NULL) {
return KNOT_EMALF;
}
if (knot_rrset_type(rrset) == KNOT_RRTYPE_ANY) {
ret = knot_ddns_add_prereq_dname(
knot_rrset_owner(rrset), &prereqs->not_in_use,
&prereqs->not_in_use_count,
&prereqs->not_in_use_allocd);
} else {
ret = knot_ddns_add_prereq_rrset(rrset,
&prereqs->not_exist,
&prereqs->not_exist_count,
&prereqs->not_exist_allocd);
}
} else if (knot_rrset_class(rrset) == qclass) {
ret = knot_ddns_add_prereq_rrset(rrset,
&prereqs->exist_full,
&prereqs->exist_full_count,
&prereqs->exist_full_allocd);
} else {
return KNOT_EMALF;
}
return ret;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_update(knot_changeset_t *changeset,
const knot_rrset_t *rrset, uint16_t qclass)
{
assert(changeset != NULL);
assert(rrset != NULL);
int ret;
// create a copy of the RRSet
/*! \todo If the packet was not parsed all at once, we could save this
* copy.
*/
knot_rrset_t *rrset_copy;
ret = knot_rrset_deep_copy(rrset, &rrset_copy, 0);
if (ret != KNOT_EOK) {
return ret;
}
/*! \todo What about the SOAs? */
if (knot_rrset_class(rrset) == qclass) {
// this RRSet should be added to the zone
ret = knot_changeset_add_rr(&changeset->add,
&changeset->add_count,
&changeset->add_allocated,
rrset_copy);
} else {
// this RRSet marks removal of something from zone
// what should be removed is distinguished when applying
ret = knot_changeset_add_rr(&changeset->remove,
&changeset->remove_count,
&changeset->remove_allocated,
rrset_copy);
}
return ret;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_exist(const knot_zone_contents_t *zone,
const knot_rrset_t *rrset, uint8_t *rcode)
{
assert(zone != NULL);
assert(rrset != NULL);
assert(rcode != NULL);
assert(knot_rrset_rdata(rrset) == NULL);
assert(knot_rrset_type(rrset) != KNOT_RRTYPE_ANY);
assert(knot_rrset_ttl(rrset) == 0);
assert(knot_rrset_class(rrset) == KNOT_CLASS_ANY);
if (!knot_dname_is_subdomain(knot_rrset_owner(rrset),
knot_node_owner(knot_zone_contents_apex(zone)))) {
*rcode = KNOT_RCODE_NOTZONE;
return KNOT_EBADZONE;
}
const knot_node_t *node;
node = knot_zone_contents_find_node(zone, knot_rrset_owner(rrset));
if (node == NULL) {
*rcode = KNOT_RCODE_NXRRSET;
return KNOT_ENONODE;
} else if (knot_node_rrset(node, knot_rrset_type(rrset)) == NULL) {
*rcode = KNOT_RCODE_NXRRSET;
return KNOT_ENORRSET;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_exist_full(const knot_zone_contents_t *zone,
const knot_rrset_t *rrset, uint8_t *rcode)
{
assert(zone != NULL);
assert(rrset != NULL);
assert(rcode != NULL);
assert(knot_rrset_rdata(rrset) == NULL);
assert(knot_rrset_type(rrset) != KNOT_RRTYPE_ANY);
assert(knot_rrset_ttl(rrset) == 0);
assert(knot_rrset_class(rrset) == KNOT_CLASS_ANY);
if (!knot_dname_is_subdomain(knot_rrset_owner(rrset),
knot_node_owner(knot_zone_contents_apex(zone)))) {
*rcode = KNOT_RCODE_NOTZONE;
return KNOT_EBADZONE;
}
const knot_node_t *node;
const knot_rrset_t *found;
node = knot_zone_contents_find_node(zone, knot_rrset_owner(rrset));
if (node == NULL) {
*rcode = KNOT_RCODE_NXRRSET;
return KNOT_EPREREQ;
} else if ((found = knot_node_rrset(node, knot_rrset_type(rrset)))
== NULL) {
*rcode = KNOT_RCODE_NXRRSET;
return KNOT_EPREREQ;
} else {
// do not have to compare the header, it is already done
assert(knot_rrset_type(found) == knot_rrset_type(rrset));
assert(knot_dname_compare(knot_rrset_owner(found),
knot_rrset_owner(rrset)) == 0);
if (knot_rrset_compare_rdata(found, rrset) <= 0) {
*rcode = KNOT_RCODE_NXRRSET;
return KNOT_EPREREQ;
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_not_exist(const knot_zone_contents_t *zone,
const knot_rrset_t *rrset, uint8_t *rcode)
{
assert(zone != NULL);
assert(rrset != NULL);
assert(rcode != NULL);
assert(knot_rrset_rdata(rrset) == NULL);
assert(knot_rrset_type(rrset) != KNOT_RRTYPE_ANY);
assert(knot_rrset_ttl(rrset) == 0);
assert(knot_rrset_class(rrset) == KNOT_CLASS_NONE);
if (!knot_dname_is_subdomain(knot_rrset_owner(rrset),
knot_node_owner(knot_zone_contents_apex(zone)))) {
*rcode = KNOT_RCODE_NOTZONE;
return KNOT_EBADZONE;
}
const knot_node_t *node;
const knot_rrset_t *found;
node = knot_zone_contents_find_node(zone, knot_rrset_owner(rrset));
if (node == NULL) {
return KNOT_EOK;
} else if ((found = knot_node_rrset(node, knot_rrset_type(rrset)))
== NULL) {
return KNOT_EOK;
} else {
// do not have to compare the header, it is already done
assert(knot_rrset_type(found) == knot_rrset_type(rrset));
assert(knot_dname_compare(knot_rrset_owner(found),
knot_rrset_owner(rrset)) == 0);
if (knot_rrset_compare_rdata(found, rrset) <= 0) {
return KNOT_EOK;
}
}
*rcode = KNOT_RCODE_YXRRSET;
return KNOT_EPREREQ;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_in_use(const knot_zone_contents_t *zone,
const knot_dname_t *dname, uint8_t *rcode)
{
assert(zone != NULL);
assert(dname != NULL);
assert(rcode != NULL);
if (!knot_dname_is_subdomain(dname,
knot_node_owner(knot_zone_contents_apex(zone)))) {
*rcode = KNOT_RCODE_NOTZONE;
return KNOT_EBADZONE;
}
const knot_node_t *node;
node = knot_zone_contents_find_node(zone, dname);
if (node == NULL) {
*rcode = KNOT_RCODE_NXDOMAIN;
return KNOT_EPREREQ;
} else if (knot_node_rrset_count(node) == 0) {
*rcode = KNOT_RCODE_NXDOMAIN;
return KNOT_EPREREQ;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_not_in_use(const knot_zone_contents_t *zone,
const knot_dname_t *dname, uint8_t *rcode)
{
assert(zone != NULL);
assert(dname != NULL);
assert(rcode != NULL);
if (!knot_dname_is_subdomain(dname,
knot_node_owner(knot_zone_contents_apex(zone)))) {
*rcode = KNOT_RCODE_NOTZONE;
return KNOT_EBADZONE;
}
const knot_node_t *node;
node = knot_zone_contents_find_node(zone, dname);
if (node == NULL) {
return KNOT_EOK;
} else if (knot_node_rrset_count(node) == 0) {
return KNOT_EOK;
}
*rcode = KNOT_RCODE_YXDOMAIN;
return KNOT_EPREREQ;
}
/*----------------------------------------------------------------------------*/
/* API functions */
/*----------------------------------------------------------------------------*/
int knot_ddns_check_zone(const knot_zone_t *zone, knot_packet_t *query,
uint8_t *rcode)
{
if (zone == NULL || query == NULL || rcode == NULL) {
return KNOT_EINVAL;
}
if (knot_packet_qtype(query) != KNOT_RRTYPE_SOA) {
*rcode = KNOT_RCODE_FORMERR;
return KNOT_EMALF;
}
if(!knot_zone_contents(zone)) {
*rcode = KNOT_RCODE_REFUSED;
return KNOT_ENOZONE;
}
// 1) check if the zone is master or slave
if (!knot_zone_is_master(zone)) {
return KNOT_EBADZONE;
}
// 2) check zone CLASS
if (knot_zone_contents_class(knot_zone_contents(zone)) !=
knot_packet_qclass(query)) {
*rcode = KNOT_RCODE_NOTAUTH;
return KNOT_ENOZONE;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
int knot_ddns_process_prereqs(knot_packet_t *query,
knot_ddns_prereq_t **prereqs, uint8_t *rcode)
{
/*! \todo Consider not parsing the whole packet at once, but
* parsing one RR at a time - could save some memory and time.
*/
if (query == NULL || prereqs == NULL || rcode == NULL) {
return KNOT_EINVAL;
}
// allocate space for the prerequisities
*prereqs = (knot_ddns_prereq_t *)calloc(1, sizeof(knot_ddns_prereq_t));
CHECK_ALLOC_LOG(*prereqs, KNOT_ENOMEM);
int ret;
for (int i = 0; i < knot_packet_answer_rrset_count(query); ++i) {
// we must copy the RRSets, because all those stored in the
// packet will be destroyed
ret = knot_ddns_add_prereq(*prereqs,
knot_packet_answer_rrset(query, i),
knot_packet_qclass(query));
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add prerequisity RRSet:%s\n",
knot_strerror(ret));
*rcode = (ret == KNOT_EMALF) ? KNOT_RCODE_FORMERR
: KNOT_RCODE_SERVFAIL;
knot_ddns_prereqs_free(prereqs);
return ret;
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
int knot_ddns_check_prereqs(const knot_zone_contents_t *zone,
knot_ddns_prereq_t **prereqs, uint8_t *rcode)
{
int i, ret;
for (i = 0; i < (*prereqs)->exist_count; ++i) {
ret = knot_ddns_check_exist(zone, (*prereqs)->exist[i], rcode);
if (ret != KNOT_EOK) {
return ret;
}
}
for (i = 0; i < (*prereqs)->exist_full_count; ++i) {
ret = knot_ddns_check_exist_full(zone,
(*prereqs)->exist_full[i],
rcode);
if (ret != KNOT_EOK) {
return ret;
}
}
for (i = 0; i < (*prereqs)->not_exist_count; ++i) {
ret = knot_ddns_check_not_exist(zone, (*prereqs)->not_exist[i],
rcode);
if (ret != KNOT_EOK) {
return ret;
}
}
for (i = 0; i < (*prereqs)->in_use_count; ++i) {
ret = knot_ddns_check_in_use(zone, (*prereqs)->in_use[i],
rcode);
if (ret != KNOT_EOK) {
return ret;
}
}
for (i = 0; i < (*prereqs)->not_in_use_count; ++i) {
ret = knot_ddns_check_not_in_use(zone,
(*prereqs)->not_in_use[i],
rcode);
if (ret != KNOT_EOK) {
return ret;
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_update(const knot_rrset_t *rrset,
const knot_packet_t *query, uint8_t *rcode)
{
if (!knot_dname_is_subdomain(knot_rrset_owner(rrset),
knot_packet_qname(query))) {
*rcode = KNOT_RCODE_NOTZONE;
return KNOT_EBADZONE;
}
if (knot_rrset_class(rrset) == knot_packet_qclass(query)) {
if (knot_rrtype_is_metatype(knot_rrset_type(rrset))) {
*rcode = KNOT_RCODE_FORMERR;
return KNOT_EMALF;
}
} else if (knot_rrset_class(rrset) == KNOT_CLASS_ANY) {
if (knot_rrset_rdata(rrset) != NULL
|| (knot_rrtype_is_metatype(knot_rrset_type(rrset))
&& knot_rrset_type(rrset) != KNOT_RRTYPE_ANY)) {
*rcode = KNOT_RCODE_FORMERR;
return KNOT_EMALF;
}
} else if (knot_rrset_class(rrset) == KNOT_CLASS_NONE) {
if (knot_rrset_ttl(rrset) != 0
|| knot_rrtype_is_metatype(knot_rrset_type(rrset))) {
*rcode = KNOT_RCODE_FORMERR;
return KNOT_EMALF;
}
} else {
*rcode = KNOT_RCODE_FORMERR;
return KNOT_EMALF;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
int knot_ddns_process_update(knot_packet_t *query,
knot_changeset_t **changeset, uint8_t *rcode)
{
// just put all RRSets from query's Authority section
// it will be distinguished when applying to the zone
if (query == NULL || changeset == NULL || rcode == NULL) {
return KNOT_EINVAL;
}
*changeset = (knot_changeset_t *)calloc(1, sizeof(knot_changeset_t));
CHECK_ALLOC_LOG(*changeset, KNOT_ENOMEM);
int ret;
for (int i = 0; i < knot_packet_authority_rrset_count(query); ++i) {
const knot_rrset_t *rrset =
knot_packet_authority_rrset(query, i);
ret = knot_ddns_check_update(rrset, query, rcode);
if (ret != KNOT_EOK) {
return ret;
}
ret = knot_ddns_add_update(*changeset, rrset,
knot_packet_qclass(query));
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add update RRSet:%s\n",
knot_strerror(ret));
*rcode = (ret == KNOT_EMALF) ? KNOT_RCODE_FORMERR
: KNOT_RCODE_SERVFAIL;
knot_free_changeset(changeset);
return ret;
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
void knot_ddns_prereqs_free(knot_ddns_prereq_t **prereq)
{
int i;
for (i = 0; i < (*prereq)->exist_count; ++i) {
knot_rrset_deep_free(&(*prereq)->exist[i], 1, 1, 1);
}
for (i = 0; i < (*prereq)->exist_full_count; ++i) {
knot_rrset_deep_free(&(*prereq)->exist_full[i], 1, 1, 1);
}
for (i = 0; i < (*prereq)->not_exist_count; ++i) {
knot_rrset_deep_free(&(*prereq)->not_exist[i], 1, 1, 1);
}
for (i = 0; i < (*prereq)->in_use_count; ++i) {
knot_dname_free(&(*prereq)->in_use[i]);
}
for (i = 0; i < (*prereq)->not_in_use_count; ++i) {
knot_dname_free(&(*prereq)->not_in_use[i]);
}
free(*prereq);
*prereq = NULL;
}
|