-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
dname.c
602 lines (531 loc) · 13.1 KB
/
dname.c
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
/*
* dname.c
*
* dname specific rdata implementations
* A dname is a rdf structure with type LDNS_RDF_TYPE_DNAME
* It is not a /real/ type! All function must therefore check
* for LDNS_RDF_TYPE_DNAME.
*
* a Net::DNS like library for C
*
* (c) NLnet Labs, 2004-2006
*
* See the file LICENSE for the license
*/
#include <ldns/config.h>
#include <ldns/ldns.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
/* Returns whether the last label in the name is a root label (a empty label).
* Note that it is not enough to just test the last character to be 0,
* because it may be part of the last label itself.
*/
static bool
ldns_dname_last_label_is_root_label(const ldns_rdf* dname)
{
size_t src_pos;
size_t len = 0;
for (src_pos = 0; src_pos < ldns_rdf_size(dname); src_pos += len + 1) {
len = ldns_rdf_data(dname)[src_pos];
}
assert(src_pos == ldns_rdf_size(dname));
return src_pos > 0 && len == 0;
}
ldns_rdf *
ldns_dname_cat_clone(const ldns_rdf *rd1, const ldns_rdf *rd2)
{
ldns_rdf *new;
uint16_t new_size;
uint8_t *buf;
uint16_t left_size;
if (ldns_rdf_get_type(rd1) != LDNS_RDF_TYPE_DNAME ||
ldns_rdf_get_type(rd2) != LDNS_RDF_TYPE_DNAME) {
return NULL;
}
/* remove root label if it is present at the end of the left
* rd, by reducing the size with 1
*/
left_size = ldns_rdf_size(rd1);
if (ldns_dname_last_label_is_root_label(rd1)) {
left_size--;
}
/* we overwrite the nullbyte of rd1 */
new_size = left_size + ldns_rdf_size(rd2);
buf = LDNS_XMALLOC(uint8_t, new_size);
if (!buf) {
return NULL;
}
/* put the two dname's after each other */
memcpy(buf, ldns_rdf_data(rd1), left_size);
memcpy(buf + left_size, ldns_rdf_data(rd2), ldns_rdf_size(rd2));
new = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_DNAME, new_size, buf);
LDNS_FREE(buf);
return new;
}
ldns_status
ldns_dname_cat(ldns_rdf *rd1, const ldns_rdf *rd2)
{
uint16_t left_size;
uint16_t size;
uint8_t* newd;
if (ldns_rdf_get_type(rd1) != LDNS_RDF_TYPE_DNAME ||
ldns_rdf_get_type(rd2) != LDNS_RDF_TYPE_DNAME) {
return LDNS_STATUS_ERR;
}
/* remove root label if it is present at the end of the left
* rd, by reducing the size with 1
*/
left_size = ldns_rdf_size(rd1);
if (ldns_dname_last_label_is_root_label(rd1)) {
left_size--;
}
size = left_size + ldns_rdf_size(rd2);
newd = LDNS_XREALLOC(ldns_rdf_data(rd1), uint8_t, size);
if(!newd) {
return LDNS_STATUS_MEM_ERR;
}
ldns_rdf_set_data(rd1, newd);
memcpy(ldns_rdf_data(rd1) + left_size, ldns_rdf_data(rd2),
ldns_rdf_size(rd2));
ldns_rdf_set_size(rd1, size);
return LDNS_STATUS_OK;
}
ldns_rdf*
ldns_dname_reverse(const ldns_rdf *dname)
{
size_t rd_size;
uint8_t* buf;
ldns_rdf* new;
size_t src_pos;
size_t len ;
assert(ldns_rdf_get_type(dname) == LDNS_RDF_TYPE_DNAME);
rd_size = ldns_rdf_size(dname);
buf = LDNS_XMALLOC(uint8_t, rd_size);
if (! buf) {
return NULL;
}
new = ldns_rdf_new(LDNS_RDF_TYPE_DNAME, rd_size, buf);
if (! new) {
LDNS_FREE(buf);
return NULL;
}
/* If dname ends in a root label, the reverse should too.
*/
if (ldns_dname_last_label_is_root_label(dname)) {
buf[rd_size - 1] = 0;
rd_size -= 1;
}
for (src_pos = 0; src_pos < rd_size; src_pos += len + 1) {
len = ldns_rdf_data(dname)[src_pos];
memcpy(&buf[rd_size - src_pos - len - 1],
&ldns_rdf_data(dname)[src_pos], len + 1);
}
return new;
}
ldns_rdf *
ldns_dname_clone_from(const ldns_rdf *d, uint16_t n)
{
uint8_t *data;
uint8_t label_size;
size_t data_size;
if (!d ||
ldns_rdf_get_type(d) != LDNS_RDF_TYPE_DNAME ||
ldns_dname_label_count(d) < n) {
return NULL;
}
data = ldns_rdf_data(d);
data_size = ldns_rdf_size(d);
while (n > 0) {
label_size = data[0] + 1;
data += label_size;
if (data_size < label_size) {
/* this label is very broken */
return NULL;
}
data_size -= label_size;
n--;
}
return ldns_dname_new_frm_data(data_size, data);
}
ldns_rdf *
ldns_dname_left_chop(const ldns_rdf *d)
{
uint8_t label_pos;
ldns_rdf *chop;
if (!d) {
return NULL;
}
if (ldns_rdf_get_type(d) != LDNS_RDF_TYPE_DNAME) {
return NULL;
}
if (ldns_dname_label_count(d) == 0) {
/* root label */
return NULL;
}
/* 05blaat02nl00 */
label_pos = ldns_rdf_data(d)[0];
chop = ldns_dname_new_frm_data(ldns_rdf_size(d) - label_pos - 1,
ldns_rdf_data(d) + label_pos + 1);
return chop;
}
uint8_t
ldns_dname_label_count(const ldns_rdf *r)
{
uint16_t src_pos;
uint16_t len;
uint8_t i;
size_t r_size;
if (!r) {
return 0;
}
i = 0;
src_pos = 0;
r_size = ldns_rdf_size(r);
if (ldns_rdf_get_type(r) != LDNS_RDF_TYPE_DNAME) {
return 0;
} else {
len = ldns_rdf_data(r)[src_pos]; /* start of the label */
/* single root label */
if (1 == r_size) {
return 0;
} else {
while ((len > 0) && src_pos < r_size) {
src_pos++;
src_pos += len;
len = ldns_rdf_data(r)[src_pos];
i++;
}
}
}
return i;
}
ldns_rdf *
ldns_dname_new(uint16_t s, void *d)
{
ldns_rdf *rd;
if (!s || !d) {
return NULL;
}
rd = LDNS_MALLOC(ldns_rdf);
if (!rd) {
return NULL;
}
ldns_rdf_set_size(rd, s);
ldns_rdf_set_type(rd, LDNS_RDF_TYPE_DNAME);
ldns_rdf_set_data(rd, d);
return rd;
}
ldns_rdf *
ldns_dname_new_frm_str(const char *str)
{
return ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, str);
}
ldns_rdf *
ldns_dname_new_frm_data(uint16_t size, const void *data)
{
return ldns_rdf_new_frm_data(LDNS_RDF_TYPE_DNAME, size, data);
}
void
ldns_dname2canonical(const ldns_rdf *rd)
{
uint8_t *rdd;
uint16_t i;
if (ldns_rdf_get_type(rd) != LDNS_RDF_TYPE_DNAME) {
return;
}
rdd = (uint8_t*)ldns_rdf_data(rd);
for (i = 0; i < ldns_rdf_size(rd); i++, rdd++) {
*rdd = (uint8_t)LDNS_DNAME_NORMALIZE((int)*rdd);
}
}
bool
ldns_dname_is_subdomain(const ldns_rdf *sub, const ldns_rdf *parent)
{
uint8_t sub_lab;
uint8_t par_lab;
int8_t i, j;
ldns_rdf *tmp_sub = NULL;
ldns_rdf *tmp_par = NULL;
ldns_rdf *sub_clone;
ldns_rdf *parent_clone;
bool result = true;
if (ldns_rdf_get_type(sub) != LDNS_RDF_TYPE_DNAME ||
ldns_rdf_get_type(parent) != LDNS_RDF_TYPE_DNAME ||
ldns_rdf_compare(sub, parent) == 0) {
return false;
}
/* would be nicer if we do not have to clone... */
sub_clone = ldns_dname_clone_from(sub, 0);
parent_clone = ldns_dname_clone_from(parent, 0);
ldns_dname2canonical(sub_clone);
ldns_dname2canonical(parent_clone);
sub_lab = ldns_dname_label_count(sub_clone);
par_lab = ldns_dname_label_count(parent_clone);
/* if sub sits above parent, it cannot be a child/sub domain */
if (sub_lab < par_lab) {
result = false;
} else {
/* check all labels the from the parent labels, from right to left.
* When they /all/ match we have found a subdomain
*/
j = sub_lab - 1; /* we count from zero, thank you */
for (i = par_lab -1; i >= 0; i--) {
tmp_sub = ldns_dname_label(sub_clone, j);
tmp_par = ldns_dname_label(parent_clone, i);
if (!tmp_sub || !tmp_par) {
/* deep free does null check */
ldns_rdf_deep_free(tmp_sub);
ldns_rdf_deep_free(tmp_par);
result = false;
break;
}
if (ldns_rdf_compare(tmp_sub, tmp_par) != 0) {
/* they are not equal */
ldns_rdf_deep_free(tmp_sub);
ldns_rdf_deep_free(tmp_par);
result = false;
break;
}
ldns_rdf_deep_free(tmp_sub);
ldns_rdf_deep_free(tmp_par);
j--;
}
}
ldns_rdf_deep_free(sub_clone);
ldns_rdf_deep_free(parent_clone);
return result;
}
int
ldns_dname_compare(const ldns_rdf *dname1, const ldns_rdf *dname2)
{
size_t lc1, lc2, lc1f, lc2f;
size_t i;
int result = 0;
uint8_t *lp1, *lp2;
/* see RFC4034 for this algorithm */
/* this algorithm assumes the names are normalized to case */
/* only when both are not NULL we can say anything about them */
if (!dname1 && !dname2) {
return 0;
}
if (!dname1 || !dname2) {
return -1;
}
/* asserts must happen later as we are looking in the
* dname, which could be NULL. But this case is handled
* above
*/
assert(ldns_rdf_get_type(dname1) == LDNS_RDF_TYPE_DNAME);
assert(ldns_rdf_get_type(dname2) == LDNS_RDF_TYPE_DNAME);
lc1 = ldns_dname_label_count(dname1);
lc2 = ldns_dname_label_count(dname2);
if (lc1 == 0 && lc2 == 0) {
return 0;
}
if (lc1 == 0) {
return -1;
}
if (lc2 == 0) {
return 1;
}
lc1--;
lc2--;
/* we start at the last label */
while (true) {
/* find the label first */
lc1f = lc1;
lp1 = ldns_rdf_data(dname1);
while (lc1f > 0) {
lp1 += *lp1 + 1;
lc1f--;
}
/* and find the other one */
lc2f = lc2;
lp2 = ldns_rdf_data(dname2);
while (lc2f > 0) {
lp2 += *lp2 + 1;
lc2f--;
}
/* now check the label character for character. */
for (i = 1; i < (size_t)(*lp1 + 1); i++) {
if (i > *lp2) {
/* apparently label 1 is larger */
result = 1;
goto done;
}
if (LDNS_DNAME_NORMALIZE((int) *(lp1 + i)) <
LDNS_DNAME_NORMALIZE((int) *(lp2 + i))) {
result = -1;
goto done;
} else if (LDNS_DNAME_NORMALIZE((int) *(lp1 + i)) >
LDNS_DNAME_NORMALIZE((int) *(lp2 + i))) {
result = 1;
goto done;
}
}
if (*lp1 < *lp2) {
/* apparently label 2 is larger */
result = -1;
goto done;
}
if (lc1 == 0 && lc2 > 0) {
result = -1;
goto done;
} else if (lc1 > 0 && lc2 == 0) {
result = 1;
goto done;
} else if (lc1 == 0 && lc2 == 0) {
result = 0;
goto done;
}
lc1--;
lc2--;
}
done:
return result;
}
int
ldns_dname_is_wildcard(const ldns_rdf* dname)
{
return ( ldns_dname_label_count(dname) > 0 &&
ldns_rdf_data(dname)[0] == 1 &&
ldns_rdf_data(dname)[1] == '*');
}
int
ldns_dname_match_wildcard(const ldns_rdf *dname, const ldns_rdf *wildcard)
{
ldns_rdf *wc_chopped;
int result;
/* check whether it really is a wildcard */
if (ldns_dname_is_wildcard(wildcard)) {
/* ok, so the dname needs to be a subdomain of the wildcard
* without the *
*/
wc_chopped = ldns_dname_left_chop(wildcard);
result = (int) ldns_dname_is_subdomain(dname, wc_chopped);
ldns_rdf_deep_free(wc_chopped);
} else {
result = (ldns_dname_compare(dname, wildcard) == 0);
}
return result;
}
/* nsec test: does prev <= middle < next
* -1 = yes
* 0 = error/can't tell
* 1 = no
*/
int
ldns_dname_interval(const ldns_rdf *prev, const ldns_rdf *middle,
const ldns_rdf *next)
{
int prev_check, next_check;
assert(ldns_rdf_get_type(prev) == LDNS_RDF_TYPE_DNAME);
assert(ldns_rdf_get_type(middle) == LDNS_RDF_TYPE_DNAME);
assert(ldns_rdf_get_type(next) == LDNS_RDF_TYPE_DNAME);
prev_check = ldns_dname_compare(prev, middle);
next_check = ldns_dname_compare(middle, next);
/* <= next. This cannot be the case for nsec, because then we would
* have gotten the nsec of next...
*/
if (next_check == 0) {
return 0;
}
/* <= */
if ((prev_check == -1 || prev_check == 0) &&
/* < */
next_check == -1) {
return -1;
} else {
return 1;
}
}
bool
ldns_dname_str_absolute(const char *dname_str)
{
const char* s;
if(dname_str && strcmp(dname_str, ".") == 0)
return 1;
if(!dname_str || strlen(dname_str) < 2)
return 0;
if(dname_str[strlen(dname_str) - 1] != '.')
return 0;
if(dname_str[strlen(dname_str) - 2] != '\\')
return 1; /* ends in . and no \ before it */
/* so we have the case of ends in . and there is \ before it */
for(s=dname_str; *s; s++) {
if(*s == '\\') {
if(s[1] && s[2] && s[3] /* check length */
&& isdigit((unsigned char)s[1])
&& isdigit((unsigned char)s[2])
&& isdigit((unsigned char)s[3]))
s += 3;
else if(!s[1] || isdigit((unsigned char)s[1])) /* escape of nul,0-9 */
return 0; /* parse error */
else s++; /* another character escaped */
}
else if(!*(s+1) && *s == '.')
return 1; /* trailing dot, unescaped */
}
return 0;
}
bool
ldns_dname_absolute(const ldns_rdf *rdf)
{
char *str = ldns_rdf2str(rdf);
if (str) {
bool r = ldns_dname_str_absolute(str);
LDNS_FREE(str);
return r;
}
return false;
}
ldns_rdf *
ldns_dname_label(const ldns_rdf *rdf, uint8_t labelpos)
{
uint8_t labelcnt;
uint16_t src_pos;
uint16_t len;
ldns_rdf *tmpnew;
size_t s;
uint8_t *data;
if (ldns_rdf_get_type(rdf) != LDNS_RDF_TYPE_DNAME) {
return NULL;
}
labelcnt = 0;
src_pos = 0;
s = ldns_rdf_size(rdf);
len = ldns_rdf_data(rdf)[src_pos]; /* label start */
while ((len > 0) && src_pos < s) {
if (labelcnt == labelpos) {
/* found our label */
data = LDNS_XMALLOC(uint8_t, len + 2);
if (!data) {
return NULL;
}
memcpy(data, ldns_rdf_data(rdf) + src_pos, len + 1);
data[len + 2 - 1] = 0;
tmpnew = ldns_rdf_new( LDNS_RDF_TYPE_DNAME
, len + 2, data);
if (!tmpnew) {
LDNS_FREE(data);
return NULL;
}
return tmpnew;
}
src_pos++;
src_pos += len;
len = ldns_rdf_data(rdf)[src_pos];
labelcnt++;
}
return NULL;
}