forked from NLnetLabs/ldns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldns-zcat.c
134 lines (117 loc) · 2.64 KB
/
ldns-zcat.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
/*
* read a zone from disk and split it up:
*
* zone: SOA a b c d e f g h i j k l
* becomes:
* zone1: SOA a b c d e f
* zone2: SOA f g h i k l
*
* ldns-catzone removes the last name and put
* the zone back together.
*
* This way you can incremental sign a zone
*
* See the file LICENSE for the license
*/
#include "config.h"
#include <errno.h>
#include <ldns/dns.h>
#define FIRST_ZONE 0
#define MIDDLE_ZONE 1
#define LAST_ZONE 2
void
usage(FILE *f, char *progname)
{
fprintf(f, "Usage: %s [OPTIONS] <zonefile>\n", progname);
fprintf(f, "\tThe generate zone file is printed to stdout\n");
fprintf(f, "\tDNSKEYs found in subsequent zones are removed.\n");
fprintf(f, "-o ORIGIN\tUse this as initial origin. For zones starting with @\n");
}
int
main(int argc, char **argv)
{
char *progname;
FILE *fp;
int c;
ldns_rdf *origin;
size_t i, j;
int where;
ldns_zone *z;
ldns_rr_list *zrr;
ldns_rr *current_rr;
ldns_rr_list *lastname;
progname = strdup(argv[0]);
origin = NULL;
while ((c = getopt(argc, argv, "n:o:")) != -1) {
switch(c) {
case 'o':
origin = ldns_dname_new_frm_str(strdup(optarg));
if (!origin) {
printf("cannot convert to dname\n");
exit(EXIT_FAILURE);
}
break;
default:
printf("Unrecognized option\n");
usage(stdout, progname);
exit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
if (argc < 1) {
usage(stdout, progname);
exit(EXIT_SUCCESS);
}
for (i = 0; i < argc; i++) {
if (0 == i) {
where = FIRST_ZONE;
} else if ((argc - 1) == i) {
where = LAST_ZONE;
} else {
where = MIDDLE_ZONE;
}
if (!(fp = fopen(argv[i], "r"))) {
printf("Cannot open file\n");
exit(EXIT_FAILURE);
}
if (!(z = ldns_zone_new_frm_fp(fp, origin, 0, 0))) {
printf("cannot parse the zone\n");
exit(EXIT_FAILURE);
}
zrr = ldns_zone_rrs(z);
printf("** READING %s\n", argv[i]);
for (j = 0; j < ldns_rr_list_rr_count(zrr); j++) {
current_rr = ldns_rr_list_rr(zrr, j);
switch(where) {
case FIRST_ZONE:
/* remove the last RRs with the same name */
break;
case MIDDLE_ZONE:
if (ldns_rr_get_type(current_rr) ==
LDNS_RR_TYPE_SOA) {
/* skip this */
continue;
}
/* remove
* SOA + SOA sig
* KEY + sig KEYs
* remove the last RRs with the same name */
break;
case LAST_ZONE:
if (ldns_rr_get_type(current_rr) ==
LDNS_RR_TYPE_SOA) {
/* skip this */
continue;
}
/* remove
* SOA + SOA sig
* KEY + sig KEYS
* DONT remove the last record */
break;
}
ldns_rr_print(stdout, current_rr);
}
}
exit(EXIT_SUCCESS);
}