dnssec-dsfromkey.c revision 1.5.2.3 1 1.5.2.3 martin /* $NetBSD: dnssec-dsfromkey.c,v 1.5.2.3 2020/04/13 08:02:35 martin Exp $ */
2 1.5.2.2 christos
3 1.5.2.2 christos /*
4 1.5.2.2 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 1.5.2.2 christos *
6 1.5.2.2 christos * This Source Code Form is subject to the terms of the Mozilla Public
7 1.5.2.2 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
8 1.5.2.2 christos * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 1.5.2.2 christos *
10 1.5.2.2 christos * See the COPYRIGHT file distributed with this work for additional
11 1.5.2.2 christos * information regarding copyright ownership.
12 1.5.2.2 christos */
13 1.5.2.2 christos
14 1.5.2.2 christos /*! \file */
15 1.5.2.2 christos
16 1.5.2.2 christos #include <config.h>
17 1.5.2.2 christos
18 1.5.2.2 christos #include <inttypes.h>
19 1.5.2.2 christos #include <stdbool.h>
20 1.5.2.2 christos #include <stdlib.h>
21 1.5.2.2 christos
22 1.5.2.2 christos #include <isc/buffer.h>
23 1.5.2.2 christos #include <isc/commandline.h>
24 1.5.2.2 christos #include <isc/hash.h>
25 1.5.2.2 christos #include <isc/mem.h>
26 1.5.2.2 christos #include <isc/print.h>
27 1.5.2.2 christos #include <isc/string.h>
28 1.5.2.2 christos #include <isc/util.h>
29 1.5.2.2 christos
30 1.5.2.2 christos #include <dns/callbacks.h>
31 1.5.2.2 christos #include <dns/db.h>
32 1.5.2.2 christos #include <dns/dbiterator.h>
33 1.5.2.2 christos #include <dns/ds.h>
34 1.5.2.2 christos #include <dns/fixedname.h>
35 1.5.2.2 christos #include <dns/keyvalues.h>
36 1.5.2.2 christos #include <dns/log.h>
37 1.5.2.2 christos #include <dns/master.h>
38 1.5.2.2 christos #include <dns/name.h>
39 1.5.2.2 christos #include <dns/rdata.h>
40 1.5.2.2 christos #include <dns/rdataclass.h>
41 1.5.2.2 christos #include <dns/rdataset.h>
42 1.5.2.2 christos #include <dns/rdatasetiter.h>
43 1.5.2.2 christos #include <dns/rdatatype.h>
44 1.5.2.2 christos #include <dns/result.h>
45 1.5.2.2 christos
46 1.5.2.2 christos #include <dst/dst.h>
47 1.5.2.2 christos
48 1.5.2.2 christos #if USE_PKCS11
49 1.5.2.2 christos #include <pk11/result.h>
50 1.5.2.2 christos #endif
51 1.5.2.2 christos
52 1.5.2.2 christos #include "dnssectool.h"
53 1.5.2.2 christos
54 1.5.2.2 christos #ifndef PATH_MAX
55 1.5.2.2 christos #define PATH_MAX 1024 /* WIN32, and others don't define this. */
56 1.5.2.2 christos #endif
57 1.5.2.2 christos
58 1.5.2.2 christos const char *program = "dnssec-dsfromkey";
59 1.5.2.2 christos int verbose;
60 1.5.2.2 christos
61 1.5.2.2 christos static dns_rdataclass_t rdclass;
62 1.5.2.2 christos static dns_fixedname_t fixed;
63 1.5.2.2 christos static dns_name_t *name = NULL;
64 1.5.2.2 christos static isc_mem_t *mctx = NULL;
65 1.5.2.2 christos static uint32_t ttl;
66 1.5.2.2 christos static bool emitttl = false;
67 1.5.2.2 christos
68 1.5.2.2 christos static isc_result_t
69 1.5.2.2 christos initname(char *setname) {
70 1.5.2.2 christos isc_result_t result;
71 1.5.2.2 christos isc_buffer_t buf;
72 1.5.2.2 christos
73 1.5.2.2 christos name = dns_fixedname_initname(&fixed);
74 1.5.2.2 christos
75 1.5.2.2 christos isc_buffer_init(&buf, setname, strlen(setname));
76 1.5.2.2 christos isc_buffer_add(&buf, strlen(setname));
77 1.5.2.2 christos result = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
78 1.5.2.2 christos return (result);
79 1.5.2.2 christos }
80 1.5.2.2 christos
81 1.5.2.2 christos static void
82 1.5.2.2 christos db_load_from_stream(dns_db_t *db, FILE *fp) {
83 1.5.2.2 christos isc_result_t result;
84 1.5.2.2 christos dns_rdatacallbacks_t callbacks;
85 1.5.2.2 christos
86 1.5.2.2 christos dns_rdatacallbacks_init(&callbacks);
87 1.5.2.2 christos result = dns_db_beginload(db, &callbacks);
88 1.5.2.2 christos if (result != ISC_R_SUCCESS)
89 1.5.2.2 christos fatal("dns_db_beginload failed: %s", isc_result_totext(result));
90 1.5.2.2 christos
91 1.5.2.2 christos result = dns_master_loadstream(fp, name, name, rdclass, 0,
92 1.5.2.2 christos &callbacks, mctx);
93 1.5.2.2 christos if (result != ISC_R_SUCCESS)
94 1.5.2.2 christos fatal("can't load from input: %s", isc_result_totext(result));
95 1.5.2.2 christos
96 1.5.2.2 christos result = dns_db_endload(db, &callbacks);
97 1.5.2.2 christos if (result != ISC_R_SUCCESS)
98 1.5.2.2 christos fatal("dns_db_endload failed: %s", isc_result_totext(result));
99 1.5.2.2 christos }
100 1.5.2.2 christos
101 1.5.2.2 christos static isc_result_t
102 1.5.2.2 christos loadset(const char *filename, dns_rdataset_t *rdataset) {
103 1.5.2.2 christos isc_result_t result;
104 1.5.2.2 christos dns_db_t *db = NULL;
105 1.5.2.2 christos dns_dbnode_t *node = NULL;
106 1.5.2.2 christos char setname[DNS_NAME_FORMATSIZE];
107 1.5.2.2 christos
108 1.5.2.2 christos dns_name_format(name, setname, sizeof(setname));
109 1.5.2.2 christos
110 1.5.2.2 christos result = dns_db_create(mctx, "rbt", name, dns_dbtype_zone,
111 1.5.2.2 christos rdclass, 0, NULL, &db);
112 1.5.2.2 christos if (result != ISC_R_SUCCESS)
113 1.5.2.2 christos fatal("can't create database");
114 1.5.2.2 christos
115 1.5.2.2 christos if (strcmp(filename, "-") == 0) {
116 1.5.2.2 christos db_load_from_stream(db, stdin);
117 1.5.2.2 christos filename = "input";
118 1.5.2.2 christos } else {
119 1.5.2.2 christos result = dns_db_load(db, filename, dns_masterformat_text, 0);
120 1.5.2.2 christos if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE)
121 1.5.2.2 christos fatal("can't load %s: %s", filename,
122 1.5.2.2 christos isc_result_totext(result));
123 1.5.2.2 christos }
124 1.5.2.2 christos
125 1.5.2.2 christos result = dns_db_findnode(db, name, false, &node);
126 1.5.2.2 christos if (result != ISC_R_SUCCESS)
127 1.5.2.2 christos fatal("can't find %s node in %s", setname, filename);
128 1.5.2.2 christos
129 1.5.2.2 christos result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_dnskey,
130 1.5.2.2 christos 0, 0, rdataset, NULL);
131 1.5.2.2 christos
132 1.5.2.2 christos if (result == ISC_R_NOTFOUND)
133 1.5.2.2 christos fatal("no DNSKEY RR for %s in %s", setname, filename);
134 1.5.2.2 christos else if (result != ISC_R_SUCCESS)
135 1.5.2.2 christos fatal("dns_db_findrdataset");
136 1.5.2.2 christos
137 1.5.2.2 christos if (node != NULL)
138 1.5.2.2 christos dns_db_detachnode(db, &node);
139 1.5.2.2 christos if (db != NULL)
140 1.5.2.2 christos dns_db_detach(&db);
141 1.5.2.2 christos return (result);
142 1.5.2.2 christos }
143 1.5.2.2 christos
144 1.5.2.2 christos static isc_result_t
145 1.5.2.2 christos loadkeyset(char *dirname, dns_rdataset_t *rdataset) {
146 1.5.2.2 christos isc_result_t result;
147 1.5.2.2 christos char filename[PATH_MAX + 1];
148 1.5.2.2 christos isc_buffer_t buf;
149 1.5.2.2 christos
150 1.5.2.2 christos dns_rdataset_init(rdataset);
151 1.5.2.2 christos
152 1.5.2.2 christos isc_buffer_init(&buf, filename, sizeof(filename));
153 1.5.2.2 christos if (dirname != NULL) {
154 1.5.2.2 christos /* allow room for a trailing slash */
155 1.5.2.2 christos if (strlen(dirname) >= isc_buffer_availablelength(&buf))
156 1.5.2.2 christos return (ISC_R_NOSPACE);
157 1.5.2.2 christos isc_buffer_putstr(&buf, dirname);
158 1.5.2.2 christos if (dirname[strlen(dirname) - 1] != '/')
159 1.5.2.2 christos isc_buffer_putstr(&buf, "/");
160 1.5.2.2 christos }
161 1.5.2.2 christos
162 1.5.2.2 christos if (isc_buffer_availablelength(&buf) < 7)
163 1.5.2.2 christos return (ISC_R_NOSPACE);
164 1.5.2.2 christos isc_buffer_putstr(&buf, "keyset-");
165 1.5.2.2 christos
166 1.5.2.2 christos result = dns_name_tofilenametext(name, false, &buf);
167 1.5.2.2 christos check_result(result, "dns_name_tofilenametext()");
168 1.5.2.2 christos if (isc_buffer_availablelength(&buf) == 0)
169 1.5.2.2 christos return (ISC_R_NOSPACE);
170 1.5.2.2 christos isc_buffer_putuint8(&buf, 0);
171 1.5.2.2 christos
172 1.5.2.2 christos return (loadset(filename, rdataset));
173 1.5.2.2 christos }
174 1.5.2.2 christos
175 1.5.2.2 christos static void
176 1.5.2.2 christos loadkey(char *filename, unsigned char *key_buf, unsigned int key_buf_size,
177 1.5.2.2 christos dns_rdata_t *rdata)
178 1.5.2.2 christos {
179 1.5.2.2 christos isc_result_t result;
180 1.5.2.2 christos dst_key_t *key = NULL;
181 1.5.2.2 christos isc_buffer_t keyb;
182 1.5.2.2 christos isc_region_t r;
183 1.5.2.2 christos
184 1.5.2.2 christos dns_rdata_init(rdata);
185 1.5.2.2 christos
186 1.5.2.2 christos isc_buffer_init(&keyb, key_buf, key_buf_size);
187 1.5.2.2 christos
188 1.5.2.2 christos result = dst_key_fromnamedfile(filename, NULL, DST_TYPE_PUBLIC,
189 1.5.2.2 christos mctx, &key);
190 1.5.2.2 christos if (result != ISC_R_SUCCESS)
191 1.5.2.2 christos fatal("can't load %s.key: %s",
192 1.5.2.2 christos filename, isc_result_totext(result));
193 1.5.2.2 christos
194 1.5.2.2 christos if (verbose > 2) {
195 1.5.2.2 christos char keystr[DST_KEY_FORMATSIZE];
196 1.5.2.2 christos
197 1.5.2.2 christos dst_key_format(key, keystr, sizeof(keystr));
198 1.5.2.2 christos fprintf(stderr, "%s: %s\n", program, keystr);
199 1.5.2.2 christos }
200 1.5.2.2 christos
201 1.5.2.2 christos result = dst_key_todns(key, &keyb);
202 1.5.2.2 christos if (result != ISC_R_SUCCESS)
203 1.5.2.2 christos fatal("can't decode key");
204 1.5.2.2 christos
205 1.5.2.2 christos isc_buffer_usedregion(&keyb, &r);
206 1.5.2.2 christos dns_rdata_fromregion(rdata, dst_key_class(key),
207 1.5.2.2 christos dns_rdatatype_dnskey, &r);
208 1.5.2.2 christos
209 1.5.2.2 christos rdclass = dst_key_class(key);
210 1.5.2.2 christos
211 1.5.2.2 christos name = dns_fixedname_initname(&fixed);
212 1.5.2.3 martin dns_name_copynf(dst_key_name(key), name);
213 1.5.2.2 christos
214 1.5.2.2 christos dst_key_free(&key);
215 1.5.2.2 christos }
216 1.5.2.2 christos
217 1.5.2.2 christos static void
218 1.5.2.2 christos logkey(dns_rdata_t *rdata)
219 1.5.2.2 christos {
220 1.5.2.2 christos isc_result_t result;
221 1.5.2.2 christos dst_key_t *key = NULL;
222 1.5.2.2 christos isc_buffer_t buf;
223 1.5.2.2 christos char keystr[DST_KEY_FORMATSIZE];
224 1.5.2.2 christos
225 1.5.2.2 christos isc_buffer_init(&buf, rdata->data, rdata->length);
226 1.5.2.2 christos isc_buffer_add(&buf, rdata->length);
227 1.5.2.2 christos result = dst_key_fromdns(name, rdclass, &buf, mctx, &key);
228 1.5.2.2 christos if (result != ISC_R_SUCCESS)
229 1.5.2.2 christos return;
230 1.5.2.2 christos
231 1.5.2.2 christos dst_key_format(key, keystr, sizeof(keystr));
232 1.5.2.2 christos fprintf(stderr, "%s: %s\n", program, keystr);
233 1.5.2.2 christos
234 1.5.2.2 christos dst_key_free(&key);
235 1.5.2.2 christos }
236 1.5.2.2 christos
237 1.5.2.2 christos static void
238 1.5.2.2 christos emit(dns_dsdigest_t dtype, bool showall, char *lookaside,
239 1.5.2.2 christos bool cds, dns_rdata_t *rdata)
240 1.5.2.2 christos {
241 1.5.2.2 christos isc_result_t result;
242 1.5.2.2 christos unsigned char buf[DNS_DS_BUFFERSIZE];
243 1.5.2.2 christos char text_buf[DST_KEY_MAXTEXTSIZE];
244 1.5.2.2 christos char name_buf[DNS_NAME_MAXWIRE];
245 1.5.2.2 christos char class_buf[10];
246 1.5.2.2 christos isc_buffer_t textb, nameb, classb;
247 1.5.2.2 christos isc_region_t r;
248 1.5.2.2 christos dns_rdata_t ds;
249 1.5.2.2 christos dns_rdata_dnskey_t dnskey;
250 1.5.2.2 christos
251 1.5.2.2 christos isc_buffer_init(&textb, text_buf, sizeof(text_buf));
252 1.5.2.2 christos isc_buffer_init(&nameb, name_buf, sizeof(name_buf));
253 1.5.2.2 christos isc_buffer_init(&classb, class_buf, sizeof(class_buf));
254 1.5.2.2 christos
255 1.5.2.2 christos dns_rdata_init(&ds);
256 1.5.2.2 christos
257 1.5.2.2 christos result = dns_rdata_tostruct(rdata, &dnskey, NULL);
258 1.5.2.2 christos if (result != ISC_R_SUCCESS)
259 1.5.2.2 christos fatal("can't convert DNSKEY");
260 1.5.2.2 christos
261 1.5.2.2 christos if ((dnskey.flags & DNS_KEYFLAG_KSK) == 0 && !showall)
262 1.5.2.2 christos return;
263 1.5.2.2 christos
264 1.5.2.2 christos result = dns_ds_buildrdata(name, rdata, dtype, buf, &ds);
265 1.5.2.2 christos if (result != ISC_R_SUCCESS)
266 1.5.2.2 christos fatal("can't build record");
267 1.5.2.2 christos
268 1.5.2.2 christos result = dns_name_totext(name, false, &nameb);
269 1.5.2.2 christos if (result != ISC_R_SUCCESS)
270 1.5.2.2 christos fatal("can't print name");
271 1.5.2.2 christos
272 1.5.2.2 christos /* Add lookaside origin, if set */
273 1.5.2.2 christos if (lookaside != NULL) {
274 1.5.2.2 christos if (isc_buffer_availablelength(&nameb) < strlen(lookaside))
275 1.5.2.2 christos fatal("DLV origin '%s' is too long", lookaside);
276 1.5.2.2 christos isc_buffer_putstr(&nameb, lookaside);
277 1.5.2.2 christos if (lookaside[strlen(lookaside) - 1] != '.') {
278 1.5.2.2 christos if (isc_buffer_availablelength(&nameb) < 1)
279 1.5.2.2 christos fatal("DLV origin '%s' is too long", lookaside);
280 1.5.2.2 christos isc_buffer_putstr(&nameb, ".");
281 1.5.2.2 christos }
282 1.5.2.2 christos }
283 1.5.2.2 christos
284 1.5.2.2 christos result = dns_rdata_tofmttext(&ds, (dns_name_t *) NULL, 0, 0, 0, "",
285 1.5.2.2 christos &textb);
286 1.5.2.2 christos
287 1.5.2.2 christos if (result != ISC_R_SUCCESS)
288 1.5.2.2 christos fatal("can't print rdata");
289 1.5.2.2 christos
290 1.5.2.2 christos result = dns_rdataclass_totext(rdclass, &classb);
291 1.5.2.2 christos if (result != ISC_R_SUCCESS)
292 1.5.2.2 christos fatal("can't print class");
293 1.5.2.2 christos
294 1.5.2.2 christos isc_buffer_usedregion(&nameb, &r);
295 1.5.2.2 christos printf("%.*s ", (int)r.length, r.base);
296 1.5.2.2 christos
297 1.5.2.2 christos if (emitttl)
298 1.5.2.2 christos printf("%u ", ttl);
299 1.5.2.2 christos
300 1.5.2.2 christos isc_buffer_usedregion(&classb, &r);
301 1.5.2.2 christos printf("%.*s", (int)r.length, r.base);
302 1.5.2.2 christos
303 1.5.2.2 christos if (lookaside == NULL) {
304 1.5.2.2 christos if (cds)
305 1.5.2.2 christos printf(" CDS ");
306 1.5.2.2 christos else
307 1.5.2.2 christos printf(" DS ");
308 1.5.2.2 christos } else
309 1.5.2.2 christos printf(" DLV ");
310 1.5.2.2 christos
311 1.5.2.2 christos isc_buffer_usedregion(&textb, &r);
312 1.5.2.2 christos printf("%.*s\n", (int)r.length, r.base);
313 1.5.2.2 christos }
314 1.5.2.2 christos
315 1.5.2.2 christos ISC_PLATFORM_NORETURN_PRE static void
316 1.5.2.2 christos usage(void) ISC_PLATFORM_NORETURN_POST;
317 1.5.2.2 christos
318 1.5.2.2 christos static void
319 1.5.2.2 christos usage(void) {
320 1.5.2.2 christos fprintf(stderr, "Usage:\n");
321 1.5.2.2 christos fprintf(stderr, " %s [options] keyfile\n\n", program);
322 1.5.2.2 christos fprintf(stderr, " %s [options] -f zonefile [zonename]\n\n", program);
323 1.5.2.2 christos fprintf(stderr, " %s [options] -s dnsname\n\n", program);
324 1.5.2.2 christos fprintf(stderr, " %s [-h|-V]\n\n", program);
325 1.5.2.2 christos fprintf(stderr, "Version: %s\n", VERSION);
326 1.5.2.2 christos fprintf(stderr, "Options:\n"
327 1.5.2.2 christos " -1: digest algorithm SHA-1\n"
328 1.5.2.2 christos " -2: digest algorithm SHA-256\n"
329 1.5.2.2 christos " -a algorithm: digest algorithm (SHA-1, SHA-256 or SHA-384)\n"
330 1.5.2.2 christos " -A: include all keys in DS set, not just KSKs (-f only)\n"
331 1.5.2.2 christos " -c class: rdata class for DS set (default IN) (-f or -s only)\n"
332 1.5.2.2 christos " -C: print CDS records\n"
333 1.5.2.2 christos " -f zonefile: read keys from a zone file\n"
334 1.5.2.2 christos " -h: print help information\n"
335 1.5.2.2 christos " -K directory: where to find key or keyset files\n"
336 1.5.2.2 christos " -l zone: print DLV records in the given lookaside zone\n"
337 1.5.2.2 christos " -s: read keys from keyset-<dnsname> file\n"
338 1.5.2.2 christos " -T: TTL of output records (omitted by default)\n"
339 1.5.2.2 christos " -v level: verbosity\n"
340 1.5.2.2 christos " -V: print version information\n");
341 1.5.2.2 christos fprintf(stderr, "Output: DS, DLV, or CDS RRs\n");
342 1.5.2.2 christos
343 1.5.2.2 christos exit (-1);
344 1.5.2.2 christos }
345 1.5.2.2 christos
346 1.5.2.2 christos int
347 1.5.2.2 christos main(int argc, char **argv) {
348 1.5.2.2 christos char *classname = NULL;
349 1.5.2.2 christos char *filename = NULL, *dir = NULL, *namestr;
350 1.5.2.2 christos char *lookaside = NULL;
351 1.5.2.3 martin char *endp, *arg1;
352 1.5.2.2 christos int ch;
353 1.5.2.2 christos dns_dsdigest_t dtype = DNS_DSDIGEST_SHA1;
354 1.5.2.2 christos bool cds = false;
355 1.5.2.2 christos bool both = true;
356 1.5.2.2 christos bool usekeyset = false;
357 1.5.2.2 christos bool showall = false;
358 1.5.2.2 christos isc_result_t result;
359 1.5.2.2 christos isc_log_t *log = NULL;
360 1.5.2.2 christos dns_rdataset_t rdataset;
361 1.5.2.2 christos dns_rdata_t rdata;
362 1.5.2.2 christos
363 1.5.2.2 christos dns_rdata_init(&rdata);
364 1.5.2.2 christos
365 1.5.2.3 martin if (argc == 1) {
366 1.5.2.2 christos usage();
367 1.5.2.3 martin }
368 1.5.2.2 christos
369 1.5.2.2 christos result = isc_mem_create(0, 0, &mctx);
370 1.5.2.3 martin if (result != ISC_R_SUCCESS) {
371 1.5.2.2 christos fatal("out of memory");
372 1.5.2.3 martin }
373 1.5.2.2 christos
374 1.5.2.2 christos #if USE_PKCS11
375 1.5.2.2 christos pk11_result_register();
376 1.5.2.2 christos #endif
377 1.5.2.2 christos dns_result_register();
378 1.5.2.2 christos
379 1.5.2.2 christos isc_commandline_errprint = false;
380 1.5.2.2 christos
381 1.5.2.2 christos #define OPTIONS "12Aa:Cc:d:Ff:K:l:sT:v:hV"
382 1.5.2.2 christos while ((ch = isc_commandline_parse(argc, argv, OPTIONS)) != -1) {
383 1.5.2.2 christos switch (ch) {
384 1.5.2.2 christos case '1':
385 1.5.2.2 christos dtype = DNS_DSDIGEST_SHA1;
386 1.5.2.2 christos both = false;
387 1.5.2.2 christos break;
388 1.5.2.2 christos case '2':
389 1.5.2.2 christos dtype = DNS_DSDIGEST_SHA256;
390 1.5.2.2 christos both = false;
391 1.5.2.2 christos break;
392 1.5.2.2 christos case 'A':
393 1.5.2.2 christos showall = true;
394 1.5.2.2 christos break;
395 1.5.2.2 christos case 'a':
396 1.5.2.2 christos dtype = strtodsdigest(isc_commandline_argument);
397 1.5.2.2 christos both = false;
398 1.5.2.2 christos break;
399 1.5.2.2 christos case 'C':
400 1.5.2.3 martin if (lookaside != NULL) {
401 1.5.2.2 christos fatal("lookaside and CDS are mutually"
402 1.5.2.2 christos " exclusive");
403 1.5.2.3 martin }
404 1.5.2.2 christos cds = true;
405 1.5.2.2 christos break;
406 1.5.2.2 christos case 'c':
407 1.5.2.2 christos classname = isc_commandline_argument;
408 1.5.2.2 christos break;
409 1.5.2.2 christos case 'd':
410 1.5.2.2 christos fprintf(stderr, "%s: the -d option is deprecated; "
411 1.5.2.2 christos "use -K\n", program);
412 1.5.2.2 christos /* fall through */
413 1.5.2.2 christos case 'K':
414 1.5.2.2 christos dir = isc_commandline_argument;
415 1.5.2.3 martin if (strlen(dir) == 0U) {
416 1.5.2.2 christos fatal("directory must be non-empty string");
417 1.5.2.3 martin }
418 1.5.2.2 christos break;
419 1.5.2.2 christos case 'f':
420 1.5.2.2 christos filename = isc_commandline_argument;
421 1.5.2.2 christos break;
422 1.5.2.2 christos case 'l':
423 1.5.2.3 martin if (cds) {
424 1.5.2.2 christos fatal("lookaside and CDS are mutually"
425 1.5.2.2 christos " exclusive");
426 1.5.2.3 martin }
427 1.5.2.2 christos lookaside = isc_commandline_argument;
428 1.5.2.2 christos if (strlen(lookaside) == 0U)
429 1.5.2.2 christos fatal("lookaside must be a non-empty string");
430 1.5.2.2 christos break;
431 1.5.2.2 christos case 's':
432 1.5.2.2 christos usekeyset = true;
433 1.5.2.2 christos break;
434 1.5.2.2 christos case 'T':
435 1.5.2.2 christos emitttl = true;
436 1.5.2.2 christos ttl = strtottl(isc_commandline_argument);
437 1.5.2.2 christos break;
438 1.5.2.2 christos case 'v':
439 1.5.2.2 christos verbose = strtol(isc_commandline_argument, &endp, 0);
440 1.5.2.3 martin if (*endp != '\0') {
441 1.5.2.2 christos fatal("-v must be followed by a number");
442 1.5.2.3 martin }
443 1.5.2.2 christos break;
444 1.5.2.2 christos case 'F':
445 1.5.2.2 christos /* Reserved for FIPS mode */
446 1.5.2.2 christos /* FALLTHROUGH */
447 1.5.2.2 christos case '?':
448 1.5.2.3 martin if (isc_commandline_option != '?') {
449 1.5.2.2 christos fprintf(stderr, "%s: invalid argument -%c\n",
450 1.5.2.2 christos program, isc_commandline_option);
451 1.5.2.3 martin }
452 1.5.2.2 christos /* FALLTHROUGH */
453 1.5.2.2 christos case 'h':
454 1.5.2.2 christos /* Does not return. */
455 1.5.2.2 christos usage();
456 1.5.2.2 christos
457 1.5.2.2 christos case 'V':
458 1.5.2.2 christos /* Does not return. */
459 1.5.2.2 christos version(program);
460 1.5.2.2 christos
461 1.5.2.2 christos default:
462 1.5.2.2 christos fprintf(stderr, "%s: unhandled option -%c\n",
463 1.5.2.2 christos program, isc_commandline_option);
464 1.5.2.2 christos exit(1);
465 1.5.2.2 christos }
466 1.5.2.2 christos }
467 1.5.2.2 christos
468 1.5.2.2 christos rdclass = strtoclass(classname);
469 1.5.2.2 christos
470 1.5.2.3 martin if (usekeyset && filename != NULL) {
471 1.5.2.2 christos fatal("cannot use both -s and -f");
472 1.5.2.3 martin }
473 1.5.2.2 christos
474 1.5.2.2 christos /* When not using -f, -A is implicit */
475 1.5.2.3 martin if (filename == NULL) {
476 1.5.2.2 christos showall = true;
477 1.5.2.3 martin }
478 1.5.2.2 christos
479 1.5.2.3 martin /*
480 1.5.2.3 martin * Use local variable arg1 so that clang can correctly analyse
481 1.5.2.3 martin * reachable paths rather than 'argc < isc_commandline_index + 1'.
482 1.5.2.3 martin */
483 1.5.2.3 martin arg1 = argv[isc_commandline_index];
484 1.5.2.3 martin if (arg1 == NULL && filename == NULL) {
485 1.5.2.2 christos fatal("the key file name was not specified");
486 1.5.2.3 martin }
487 1.5.2.3 martin if (arg1 != NULL && argv[isc_commandline_index + 1] != NULL) {
488 1.5.2.2 christos fatal("extraneous arguments");
489 1.5.2.3 martin }
490 1.5.2.2 christos
491 1.5.2.2 christos result = dst_lib_init(mctx, NULL);
492 1.5.2.3 martin if (result != ISC_R_SUCCESS) {
493 1.5.2.2 christos fatal("could not initialize dst: %s",
494 1.5.2.2 christos isc_result_totext(result));
495 1.5.2.3 martin }
496 1.5.2.2 christos
497 1.5.2.2 christos setup_logging(mctx, &log);
498 1.5.2.2 christos
499 1.5.2.2 christos dns_rdataset_init(&rdataset);
500 1.5.2.2 christos
501 1.5.2.2 christos if (usekeyset || filename != NULL) {
502 1.5.2.3 martin if (arg1 == NULL) {
503 1.5.2.3 martin /* using file name as the zone name */
504 1.5.2.2 christos namestr = filename;
505 1.5.2.3 martin } else {
506 1.5.2.3 martin namestr = arg1;
507 1.5.2.3 martin }
508 1.5.2.2 christos
509 1.5.2.2 christos result = initname(namestr);
510 1.5.2.3 martin if (result != ISC_R_SUCCESS) {
511 1.5.2.2 christos fatal("could not initialize name %s", namestr);
512 1.5.2.3 martin }
513 1.5.2.2 christos
514 1.5.2.3 martin if (usekeyset) {
515 1.5.2.2 christos result = loadkeyset(dir, &rdataset);
516 1.5.2.3 martin } else {
517 1.5.2.3 martin INSIST(filename != NULL);
518 1.5.2.2 christos result = loadset(filename, &rdataset);
519 1.5.2.3 martin }
520 1.5.2.2 christos
521 1.5.2.3 martin if (result != ISC_R_SUCCESS) {
522 1.5.2.2 christos fatal("could not load DNSKEY set: %s\n",
523 1.5.2.2 christos isc_result_totext(result));
524 1.5.2.3 martin }
525 1.5.2.2 christos
526 1.5.2.2 christos for (result = dns_rdataset_first(&rdataset);
527 1.5.2.2 christos result == ISC_R_SUCCESS;
528 1.5.2.2 christos result = dns_rdataset_next(&rdataset)) {
529 1.5.2.2 christos dns_rdata_init(&rdata);
530 1.5.2.2 christos dns_rdataset_current(&rdataset, &rdata);
531 1.5.2.2 christos
532 1.5.2.3 martin if (verbose > 2) {
533 1.5.2.2 christos logkey(&rdata);
534 1.5.2.3 martin }
535 1.5.2.2 christos
536 1.5.2.2 christos if (both) {
537 1.5.2.2 christos emit(DNS_DSDIGEST_SHA1, showall, lookaside,
538 1.5.2.2 christos cds, &rdata);
539 1.5.2.2 christos emit(DNS_DSDIGEST_SHA256, showall, lookaside,
540 1.5.2.2 christos cds, &rdata);
541 1.5.2.3 martin } else {
542 1.5.2.2 christos emit(dtype, showall, lookaside, cds, &rdata);
543 1.5.2.3 martin }
544 1.5.2.2 christos }
545 1.5.2.2 christos } else {
546 1.5.2.2 christos unsigned char key_buf[DST_KEY_MAXSIZE];
547 1.5.2.2 christos
548 1.5.2.3 martin loadkey(arg1, key_buf, DST_KEY_MAXSIZE, &rdata);
549 1.5.2.2 christos
550 1.5.2.2 christos if (both) {
551 1.5.2.2 christos emit(DNS_DSDIGEST_SHA1, showall, lookaside, cds,
552 1.5.2.2 christos &rdata);
553 1.5.2.2 christos emit(DNS_DSDIGEST_SHA256, showall, lookaside, cds,
554 1.5.2.2 christos &rdata);
555 1.5.2.3 martin } else {
556 1.5.2.2 christos emit(dtype, showall, lookaside, cds, &rdata);
557 1.5.2.3 martin }
558 1.5.2.2 christos }
559 1.5.2.2 christos
560 1.5.2.2 christos if (dns_rdataset_isassociated(&rdataset))
561 1.5.2.2 christos dns_rdataset_disassociate(&rdataset);
562 1.5.2.2 christos cleanup_logging(&log);
563 1.5.2.2 christos dst_lib_destroy();
564 1.5.2.2 christos dns_name_destroy();
565 1.5.2.3 martin if (verbose > 10) {
566 1.5.2.2 christos isc_mem_stats(mctx, stdout);
567 1.5.2.3 martin }
568 1.5.2.2 christos isc_mem_destroy(&mctx);
569 1.5.2.2 christos
570 1.5.2.2 christos fflush(stdout);
571 1.5.2.2 christos if (ferror(stdout)) {
572 1.5.2.2 christos fprintf(stderr, "write error\n");
573 1.5.2.2 christos return (1);
574 1.5.2.3 martin } else {
575 1.5.2.2 christos return (0);
576 1.5.2.3 martin }
577 1.5.2.2 christos }
578