3 * some handy function needed in drill and not implemented
7 * See the file LICENSE for the license
12 #include <ldns/ldns.h>
17 read_line(FILE *input
, char *line
, size_t len
)
22 for (i
= 0; i
< (int)len
-1; i
++) {
23 c
= (char)getc(input
);
26 } else if (c
!= '\n') {
36 /* key_list must be initialized with ldns_rr_list_new() */
38 read_key_file(const char *filename
, ldns_rr_list
*key_list
, bool silently
)
43 char line
[LDNS_MAX_LINELEN
];
48 input_file
= fopen(filename
, "r");
51 fprintf(stderr
, "Error opening %s: %s\n",
52 filename
, strerror(errno
));
54 return LDNS_STATUS_ERR
;
56 while (line_len
>= 0) {
57 line_len
= (int) read_line(input_file
, line
, sizeof(line
));
59 if (line_len
> 0 && line
[0] != ';') {
60 status
= ldns_rr_new_frm_str(&rr
, line
, 0, NULL
, NULL
);
61 if (status
!= LDNS_STATUS_OK
) {
64 "Error parsing DNSKEY RR "
65 "in line %d: %s\n", line_nr
,
66 ldns_get_errorstr_by_id(status
)
69 } else if (ldns_rr_get_type(rr
) == LDNS_RR_TYPE_DNSKEY
||
70 ldns_rr_get_type(rr
) == LDNS_RR_TYPE_DS
) {
71 ldns_rr_list_push_rr(key_list
, rr
);
80 return LDNS_STATUS_OK
;
82 /*fprintf(stderr, "No keys read\n");*/
83 return LDNS_STATUS_ERR
;
88 ldns_rdf_new_addr_frm_str(char *str
)
92 a
= ldns_rdf_new_frm_str(LDNS_RDF_TYPE_A
, str
);
95 a
= ldns_rdf_new_frm_str(LDNS_RDF_TYPE_AAAA
, str
);
104 local_print_ds(FILE* out
, const char* pre
, ldns_rr
* ds
)
107 fprintf(out
, "%s", pre
);
108 ldns_rr_print(out
, ds
);
114 * For all keys in a packet print the DS
117 print_ds_of_keys(ldns_pkt
*p
)
123 /* TODO fix the section stuff, here or in ldns */
124 keys
= ldns_pkt_rr_list_by_type(p
, LDNS_RR_TYPE_DNSKEY
,
125 LDNS_SECTION_ANSWER
);
127 /* this also returns the question section rr, which does not
128 * have any data.... and this inturn crashes everything */
131 for (i
= 0; i
< ldns_rr_list_rr_count(keys
); i
++) {
132 fprintf(stdout
, ";\n; equivalent DS records for key %u:\n",
133 (unsigned int)ldns_calc_keytag(ldns_rr_list_rr(keys
, i
)));
135 ds
= ldns_key_rr2ds(ldns_rr_list_rr(keys
, i
), LDNS_SHA1
);
136 local_print_ds(stdout
, "; sha1: ", ds
);
137 ds
= ldns_key_rr2ds(ldns_rr_list_rr(keys
, i
), LDNS_SHA256
);
138 local_print_ds(stdout
, "; sha256: ", ds
);
140 ldns_rr_list_deep_free(keys
);
145 print_class_type(FILE *fp
, ldns_rr
*r
)
147 ldns_lookup_table
*lt
;
148 lt
= ldns_lookup_by_id(ldns_rr_classes
, ldns_rr_get_class(r
));
150 fprintf(fp
, " %s", lt
->name
);
152 fprintf(fp
, " CLASS%d", ldns_rr_get_class(r
));
154 /* okay not THE way - but the quickest */
155 switch (ldns_rr_get_type(r
)) {
156 case LDNS_RR_TYPE_RRSIG
:
157 fprintf(fp
, " RRSIG ");
159 case LDNS_RR_TYPE_DNSKEY
:
160 fprintf(fp
, " DNSKEY ");
162 case LDNS_RR_TYPE_DS
:
172 print_ds_abbr(FILE *fp
, ldns_rr
*ds
)
174 if (!ds
|| (ldns_rr_get_type(ds
) != LDNS_RR_TYPE_DS
)) {
178 ldns_rdf_print(fp
, ldns_rr_owner(ds
));
179 fprintf(fp
, " %d", (int)ldns_rr_ttl(ds
));
180 print_class_type(fp
, ds
);
181 ldns_rdf_print(fp
, ldns_rr_rdf(ds
, 0)); fprintf(fp
, " ");
182 ldns_rdf_print(fp
, ldns_rr_rdf(ds
, 1)); fprintf(fp
, " ");
183 ldns_rdf_print(fp
, ldns_rr_rdf(ds
, 2)); fprintf(fp
, " ");
184 ldns_rdf_print(fp
, ldns_rr_rdf(ds
, 3)); fprintf(fp
, " ");
187 /* print some of the elements of a signature */
189 print_rrsig_abbr(FILE *fp
, ldns_rr
*sig
) {
190 if (!sig
|| (ldns_rr_get_type(sig
) != LDNS_RR_TYPE_RRSIG
)) {
194 ldns_rdf_print(fp
, ldns_rr_owner(sig
));
195 fprintf(fp
, " %d", (int)ldns_rr_ttl(sig
));
196 print_class_type(fp
, sig
);
198 /* print a number of rdf's */
200 ldns_rdf_print(fp
, ldns_rr_rdf(sig
, 0)); fprintf(fp
, " ");
202 ldns_rdf_print(fp
, ldns_rr_rdf(sig
, 1)); fprintf(fp
, " ");
204 ldns_rdf_print(fp
, ldns_rr_rdf(sig
, 2)); fprintf(fp
, " (\n\t\t\t");
206 ldns_rdf_print(fp
, ldns_rr_rdf(sig
, 4)); fprintf(fp
, " ");
208 ldns_rdf_print(fp
, ldns_rr_rdf(sig
, 5)); fprintf(fp
, " ");
210 ldns_rdf_print(fp
, ldns_rr_rdf(sig
, 6)); fprintf(fp
, " ");
212 ldns_rdf_print(fp
, ldns_rr_rdf(sig
, 7)); fprintf(fp
, ")");
216 print_dnskey_abbr(FILE *fp
, ldns_rr
*key
)
218 if (!key
|| (ldns_rr_get_type(key
) != LDNS_RR_TYPE_DNSKEY
)) {
222 ldns_rdf_print(fp
, ldns_rr_owner(key
));
223 fprintf(fp
, " %d", (int)ldns_rr_ttl(key
));
224 print_class_type(fp
, key
);
226 /* print a number of rdf's */
228 ldns_rdf_print(fp
, ldns_rr_rdf(key
, 0)); fprintf(fp
, " ");
230 ldns_rdf_print(fp
, ldns_rr_rdf(key
, 1)); fprintf(fp
, " ");
232 ldns_rdf_print(fp
, ldns_rr_rdf(key
, 2));
234 if (ldns_rdf2native_int16(ldns_rr_rdf(key
, 0)) == 256) {
235 fprintf(fp
, " ;{id = %u (zsk), size = %db}", (unsigned int)ldns_calc_keytag(key
),
236 (int)ldns_rr_dnskey_key_size(key
));
239 if (ldns_rdf2native_int16(ldns_rr_rdf(key
, 0)) == 257) {
240 fprintf(fp
, " ;{id = %u (ksk), size = %db}", (unsigned int)ldns_calc_keytag(key
),
241 (int)ldns_rr_dnskey_key_size(key
));
244 fprintf(fp
, " ;{id = %u, size = %db}", (unsigned int)ldns_calc_keytag(key
),
245 (int)ldns_rr_dnskey_key_size(key
));
249 print_rr_list_abbr(FILE *fp
, ldns_rr_list
*rrlist
, const char *usr
)
254 for(i
= 0; i
< ldns_rr_list_rr_count(rrlist
); i
++) {
255 tp
= ldns_rr_get_type(ldns_rr_list_rr(rrlist
, i
));
256 if (i
== 0 && tp
!= LDNS_RR_TYPE_RRSIG
) {
258 fprintf(fp
, "%s ", usr
);
262 case LDNS_RR_TYPE_DNSKEY
:
263 print_dnskey_abbr(fp
, ldns_rr_list_rr(rrlist
, i
));
265 case LDNS_RR_TYPE_RRSIG
:
266 print_rrsig_abbr(fp
, ldns_rr_list_rr(rrlist
, i
));
268 case LDNS_RR_TYPE_DS
:
269 print_ds_abbr(fp
, ldns_rr_list_rr(rrlist
, i
));
286 printf("Mem failure\n");
293 xrealloc(void *p
, size_t size
)
297 q
= realloc(p
, size
);
299 printf("Mem failure\n");