forked from tobez/validns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
419 lines (401 loc) · 11.9 KB
/
main.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
/*
* Part of DNS zone file validator `validns`.
*
* Copyright 2011, 2012 Anton Berezin <[email protected]>
* Modified BSD license.
* (See LICENSE file in the distribution.)
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "common.h"
#include "carp.h"
#include "mempool.h"
#include "textparse.h"
#include "rr.h"
struct globals G;
struct file_info *file_info = NULL;
int
read_zone_file(void);
static char *process_directive(char *s)
{
char *d = s+1;
if (*(s+1) == 'O' && strncmp(s, "$ORIGIN", 7) == 0) {
char *o;
s += 7;
if (!isspace(*s)) {
if (isalnum(*s)) goto unrecognized_directive;
return bitch("bad $ORIGIN format");
}
s = skip_white_space(s);
o = extract_name(&s, "$ORIGIN value", 0);
if (!o) {
return NULL;
}
if (*s) {
return bitch("garbage after valid $ORIGIN directive");
}
G.opt.current_origin = o;
if (G.opt.verbose) {
fprintf(stderr, "-> %s:%d: ", file_info->name, file_info->line);
fprintf(stderr, "origin is now %s\n", o);
}
} else if (*(s+1) == 'T' && strncmp(s, "$TTL", 4) == 0) {
s += 4;
if (!isspace(*s)) {
if (isalnum(*s)) goto unrecognized_directive;
return bitch("bad $TTL format");
}
s = skip_white_space(s);
G.default_ttl = extract_timevalue(&s, "$TTL value");
if (G.default_ttl < 0) {
return NULL;
}
if (*s) {
return bitch("garbage after valid $TTL directive");
}
if (G.opt.verbose) {
fprintf(stderr, "-> %s:%d: ", file_info->name, file_info->line);
fprintf(stderr, "default ttl is now %ld\n", G.default_ttl);
}
} else if (*(s+1) == 'I' && strncmp(s, "$INCLUDE", 8) == 0) {
s += 8;
if (!isspace(*s)) {
if (isalnum(*s)) goto unrecognized_directive;
return bitch("bad $INCLUDE format");
}
s = skip_white_space(s);
return bitch("XXX include support is not implemented");
} else {
unrecognized_directive:
s = d-1;
while (isalnum(*d)) d++;
*d = '\0';
return bitch("unrecognized directive: %s", s);
}
return s;
}
int
read_zone_file(void)
{
char *s;
char *name = NULL, *class, *rdtype;
long ttl = -1;
while (file_info) {
while (fgets(file_info->buf, 2048, file_info->file)) {
freeall_temp();
file_info->line++;
file_info->paren_mode = 0;
rdtype = NULL;
if (empty_line_or_comment(file_info->buf))
continue;
s = file_info->buf;
if (!isspace(*s)) {
/* <domain-name>, $INCLUDE, $ORIGIN */
if (*s == '$') {
process_directive(s);
continue;
} else {
/* <domain-name> */
name = extract_name(&s, "record name", 0);
if (!name)
continue;
}
} else {
s = skip_white_space(s);
}
if (!s)
continue;
if (!name) {
bitch("cannot assume previous name for it is not known");
continue;
}
if (G.default_ttl >= 0)
ttl = G.default_ttl;
if (isdigit(*s)) {
ttl = extract_timevalue(&s, "TTL");
if (ttl < 0)
continue;
class = extract_label(&s, "class or type", "temporary");
if (!class)
continue;
if (*class == 'i' && *(class+1) == 'n' && *(class+2) == 0) {
} else if (*class == 'c' && *(class+1) == 's' && *(class+2) == 0) {
bitch("CSNET class is not supported");
continue;
} else if (*class == 'c' && *(class+1) == 'h' && *(class+2) == 0) {
bitch("CHAOS class is not supported");
continue;
} else if (*class == 'h' && *(class+1) == 's' && *(class+2) == 0) {
bitch("HESIOD class is not supported");
continue;
} else {
rdtype = class;
}
} else {
class = extract_label(&s, "class or type", "temporary");
if (!class)
continue;
if (*class == 'i' && *(class+1) == 'n' && *(class+2) == 0) {
if (isdigit(*s)) {
ttl = extract_timevalue(&s, "TTL");
if (ttl < 0)
continue;
}
} else if (*class == 'c' && *(class+1) == 's' && *(class+2) == 0) {
bitch("CSNET class is not supported");
continue;
} else if (*class == 'c' && *(class+1) == 'h' && *(class+2) == 0) {
bitch("CHAOS class is not supported");
continue;
} else if (*class == 'h' && *(class+1) == 's' && *(class+2) == 0) {
bitch("HESIOD class is not supported");
continue;
} else {
rdtype = class;
}
}
if (!rdtype) {
rdtype = extract_label(&s, "type", "temporary");
}
if (!rdtype) {
continue;
}
if (ttl < 0) {
ttl = G.default_ttl;
}
if (ttl < 0) {
bitch("ttl not specified and default is not known");
continue;
}
{
int is_generic;
int type = str2rdtype(rdtype, &is_generic);
if (type <= 0) continue;
if (is_generic)
rr_parse_any(name, ttl, type, s);
else if (type > T_MAX)
rr_parse_any(name, ttl, type, s);
else if (rr_methods[type].rr_parse)
rr_methods[type].rr_parse(name, ttl, type, s);
else
rr_parse_any(name, ttl, type, s);
}
}
if (ferror(file_info->file))
croak(1, "read error for %s", file_info->name);
file_info = file_info->next;
}
return 0;
}
void
open_zone_file(char *fname)
{
FILE *f;
struct file_info *new_file_info;
if (strcmp(fname, "-") == 0) {
f = stdin;
fname = "stdin";
} else {
f = fopen(fname, "r");
}
if (!f)
croak(1, "open %s", fname);
new_file_info = malloc(sizeof(*new_file_info) + strlen(fname) + 1);
if (!new_file_info)
croak(1, "malloc(file_info), %s", fname);
new_file_info->next = file_info;
new_file_info->file = f;
new_file_info->line = 0;
strcpy(new_file_info->name, fname);
file_info = new_file_info;
}
void usage(char *err)
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s -h\n", thisprogname());
fprintf(stderr, " %s [options] zone-file\n", thisprogname());
fprintf(stderr, "Usage parameters:\n");
fprintf(stderr, "\t-h\t\tproduce usage text and quit\n");
fprintf(stderr, "\t-f\t\tquit on first validation error\n");
fprintf(stderr, "\t-p name\tperform policy check <name>\n");
fprintf(stderr, "\t\t\tsingle-ns\n");
fprintf(stderr, "\t\t\tcname-other-data\n");
fprintf(stderr, "\t\t\tdname\n");
fprintf(stderr, "\t\t\tnsec3param-not-apex\n");
fprintf(stderr, "\t\t\tmx-alias\n");
fprintf(stderr, "\t\t\tns-alias\n");
fprintf(stderr, "\t\t\trp-txt-exists\n");
fprintf(stderr, "\t\t\tall\n");
fprintf(stderr, "\t-n N\t\tuse N worker threads\n");
fprintf(stderr, "\t-q\t\tquiet - do not produce any output\n");
fprintf(stderr, "\t-s\t\tprint validation summary/stats\n");
fprintf(stderr, "\t-v\t\tbe extra verbose\n");
fprintf(stderr, "\t-I path\tuse this path for $INCLUDE files\n");
fprintf(stderr, "\t-z origin\tuse this origin as initial $ORIGIN\n");
fprintf(stderr, "\t-t epoch-time\tuse this time instead of \"now\"\n");
exit(1);
}
struct rr_methods rr_methods[T_MAX+1];
static void initialize_globals(void)
{
int i;
bzero(&G, sizeof(G));
bzero(&G.opt, sizeof(G.opt));
bzero(&G.stats, sizeof(G.stats));
G.default_ttl = -1; /* XXX orly? */
G.opt.current_time = time(NULL);
for (i = 0; i <= T_MAX; i++) {
rr_methods[i] = unknown_methods;
}
rr_methods[T_AAAA] = aaaa_methods;
rr_methods[T_A] = a_methods;
rr_methods[T_CERT] = cert_methods;
rr_methods[T_CNAME] = cname_methods;
rr_methods[T_DNAME] = dname_methods;
rr_methods[T_DNSKEY] = dnskey_methods;
rr_methods[T_DS] = ds_methods;
rr_methods[T_HINFO] = hinfo_methods;
rr_methods[T_IPSECKEY] = ipseckey_methods;
rr_methods[T_L32] = l32_methods;
rr_methods[T_L64] = l64_methods;
rr_methods[T_LOC] = loc_methods;
rr_methods[T_LP] = lp_methods;
rr_methods[T_MX] = mx_methods;
rr_methods[T_NAPTR] = naptr_methods;
rr_methods[T_NID] = nid_methods;
rr_methods[T_NSEC3PARAM] = nsec3param_methods;
rr_methods[T_NSEC3] = nsec3_methods;
rr_methods[T_NSEC] = nsec_methods;
rr_methods[T_NS] = ns_methods;
rr_methods[T_PTR] = ptr_methods;
rr_methods[T_RP] = rp_methods;
rr_methods[T_RRSIG] = rrsig_methods;
rr_methods[T_SOA] = soa_methods;
rr_methods[T_SPF] = spf_methods;
rr_methods[T_SRV] = srv_methods;
rr_methods[T_SSHFP] = sshfp_methods;
rr_methods[T_TLSA] = tlsa_methods;
rr_methods[T_TXT] = txt_methods;
}
int
main(int argc, char **argv)
{
int o;
initialize_globals();
struct timeval start, stop;
while ((o = getopt(argc, argv, "fhqsvI:z:t:p:n:")) != -1) {
switch(o) {
case 'h':
usage(NULL);
break;
case 'f':
G.opt.die_on_first_error = 1;
break;
case 'q':
G.opt.no_output = 1;
break;
case 's':
G.opt.summary = 1;
break;
case 'v':
G.opt.verbose = 1;
break;
case 'p':
if (strcmp(optarg, "all") == 0) {
int i;
for (i = 0; i < N_POLICY_CHECKS; i++) {
G.opt.policy_checks[i] = 1;
}
} else if (strcmp(optarg, "single-ns") == 0) {
G.opt.policy_checks[POLICY_SINGLE_NS] = 1;
} else if (strcmp(optarg, "cname-other-data") == 0) {
G.opt.policy_checks[POLICY_CNAME_OTHER_DATA] = 1;
} else if (strcmp(optarg, "dname") == 0) {
G.opt.policy_checks[POLICY_DNAME] = 1;
} else if (strcmp(optarg, "dnskey") == 0) {
G.opt.policy_checks[POLICY_DNSKEY] = 1;
} else if (strcmp(optarg, "nsec3param-not-apex") == 0) {
G.opt.policy_checks[POLICY_NSEC3PARAM_NOT_APEX] = 1;
} else if (strcmp(optarg, "mx-alias") == 0) {
G.opt.policy_checks[POLICY_MX_ALIAS] = 1;
} else if (strcmp(optarg, "ns-alias") == 0) {
G.opt.policy_checks[POLICY_NS_ALIAS] = 1;
} else if (strcmp(optarg, "rp-txt-exists") == 0) {
G.opt.policy_checks[POLICY_RP_TXT_EXISTS] = 1;
} else if (strcmp(optarg, "tlsa-host") == 0) {
G.opt.policy_checks[POLICY_TLSA_HOST] = 1;
} else {
usage("unknown policy name");
}
break;
case 'I':
G.opt.include_path = optarg;
break;
case 'z':
if (strlen(optarg) && *(optarg+strlen(optarg)-1) == '.') {
G.opt.current_origin = optarg;
} else if (strlen(optarg)) {
G.opt.current_origin = getmem(strlen(optarg)+2);
strcpy(stpcpy(G.opt.current_origin, optarg), ".");
} else {
usage("origin must not be empty");
}
break;
case 'n':
G.opt.n_threads = strtol(optarg, NULL, 10);
if (G.opt.n_threads > 256)
usage("non-sensical number of threads requested");
if (G.opt.verbose)
fprintf(stderr, "using %d worker threads\n", G.opt.n_threads);
break;
case 't':
G.opt.current_time = strtol(optarg, NULL, 10);
if (G.opt.verbose)
fprintf(stderr, "using time %d instead of \"now\"\n", G.opt.current_time);
break;
default:
usage(NULL);
}
}
argc -= optind;
argv += optind;
if (argc != 1)
usage(NULL);
gettimeofday(&start, NULL);
open_zone_file(argv[0]);
read_zone_file();
validate_zone();
verify_all_keys();
if (G.nsec3_present) {
if (first_nsec3) nsec3_validate(&first_nsec3->rr);
perform_remaining_nsec3checks();
}
gettimeofday(&stop, NULL);
if (G.opt.summary) {
printf("records found: %d\n", G.stats.rr_count);
printf("skipped dups: %d\n", G.stats.skipped_dup_rr_count);
printf("record sets found: %d\n", G.stats.rrset_count);
printf("unique names found: %d\n", G.stats.names_count);
printf("delegations found: %d\n", G.stats.delegations);
printf(" nsec3 records: %d\n", G.stats.nsec3_count);
/* "not authoritative names" - non-empty terminals without any authoritative records */
/* delegation points count as authoritative, which might or might not be correct */
printf("not authoritative names, not counting delegation points:\n"
" %d\n", G.stats.not_authoritative);
printf("validation errors: %d\n", G.stats.error_count);
printf("signatures verified: %d\n", G.stats.signatures_verified);
printf("time taken: %.3fs\n",
stop.tv_sec - start.tv_sec + (stop.tv_usec - start.tv_usec)/1000000.);
}
return G.exit_code;
}