dnssectool.c revision 1.3 1 /* $NetBSD: dnssectool.c,v 1.3 2019/01/09 16:54:59 christos Exp $ */
2
3 /*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 /*! \file */
15
16 /*%
17 * DNSSEC Support Routines.
18 */
19
20 #include <config.h>
21
22 #include <inttypes.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25
26 #ifdef _WIN32
27 #include <Winsock2.h>
28 #endif
29
30 #include <isc/base32.h>
31 #include <isc/buffer.h>
32 #include <isc/commandline.h>
33 #include <isc/dir.h>
34 #include <isc/file.h>
35 #include <isc/heap.h>
36 #include <isc/list.h>
37 #include <isc/mem.h>
38 #include <isc/platform.h>
39 #include <isc/print.h>
40 #include <isc/string.h>
41 #include <isc/time.h>
42 #include <isc/util.h>
43
44 #include <dns/db.h>
45 #include <dns/dbiterator.h>
46 #include <dns/dnssec.h>
47 #include <dns/fixedname.h>
48 #include <dns/keyvalues.h>
49 #include <dns/log.h>
50 #include <dns/name.h>
51 #include <dns/nsec.h>
52 #include <dns/nsec3.h>
53 #include <dns/rdatastruct.h>
54 #include <dns/rdataclass.h>
55 #include <dns/rdataset.h>
56 #include <dns/rdatasetiter.h>
57 #include <dns/rdatatype.h>
58 #include <dns/result.h>
59 #include <dns/secalg.h>
60 #include <dns/time.h>
61
62 #include "dnssectool.h"
63
64 extern int verbose;
65 extern const char *program;
66
67 static fatalcallback_t *fatalcallback = NULL;
68
69 void
70 fatal(const char *format, ...) {
71 va_list args;
72
73 fprintf(stderr, "%s: fatal: ", program);
74 va_start(args, format);
75 vfprintf(stderr, format, args);
76 va_end(args);
77 fprintf(stderr, "\n");
78 if (fatalcallback != NULL)
79 (*fatalcallback)();
80 exit(1);
81 }
82
83 void
84 setfatalcallback(fatalcallback_t *callback) {
85 fatalcallback = callback;
86 }
87
88 void
89 check_result(isc_result_t result, const char *message) {
90 if (result != ISC_R_SUCCESS)
91 fatal("%s: %s", message, isc_result_totext(result));
92 }
93
94 void
95 vbprintf(int level, const char *fmt, ...) {
96 va_list ap;
97 if (level > verbose)
98 return;
99 va_start(ap, fmt);
100 fprintf(stderr, "%s: ", program);
101 vfprintf(stderr, fmt, ap);
102 va_end(ap);
103 }
104
105 void
106 version(const char *name) {
107 fprintf(stderr, "%s %s\n", name, VERSION);
108 exit(0);
109 }
110
111 void
112 sig_format(dns_rdata_rrsig_t *sig, char *cp, unsigned int size) {
113 char namestr[DNS_NAME_FORMATSIZE];
114 char algstr[DNS_NAME_FORMATSIZE];
115
116 dns_name_format(&sig->signer, namestr, sizeof(namestr));
117 dns_secalg_format(sig->algorithm, algstr, sizeof(algstr));
118 snprintf(cp, size, "%s/%s/%d", namestr, algstr, sig->keyid);
119 }
120
121 void
122 setup_logging(isc_mem_t *mctx, isc_log_t **logp) {
123 isc_result_t result;
124 isc_logdestination_t destination;
125 isc_logconfig_t *logconfig = NULL;
126 isc_log_t *log = NULL;
127 int level;
128
129 if (verbose < 0)
130 verbose = 0;
131 switch (verbose) {
132 case 0:
133 /*
134 * We want to see warnings about things like out-of-zone
135 * data in the master file even when not verbose.
136 */
137 level = ISC_LOG_WARNING;
138 break;
139 case 1:
140 level = ISC_LOG_INFO;
141 break;
142 default:
143 level = ISC_LOG_DEBUG(verbose - 2 + 1);
144 break;
145 }
146
147 RUNTIME_CHECK(isc_log_create(mctx, &log, &logconfig) == ISC_R_SUCCESS);
148 isc_log_setcontext(log);
149 dns_log_init(log);
150 dns_log_setcontext(log);
151
152 RUNTIME_CHECK(isc_log_settag(logconfig, program) == ISC_R_SUCCESS);
153
154 /*
155 * Set up a channel similar to default_stderr except:
156 * - the logging level is passed in
157 * - the program name and logging level are printed
158 * - no time stamp is printed
159 */
160 destination.file.stream = stderr;
161 destination.file.name = NULL;
162 destination.file.versions = ISC_LOG_ROLLNEVER;
163 destination.file.maximum_size = 0;
164 result = isc_log_createchannel(logconfig, "stderr",
165 ISC_LOG_TOFILEDESC,
166 level,
167 &destination,
168 ISC_LOG_PRINTTAG|ISC_LOG_PRINTLEVEL);
169 check_result(result, "isc_log_createchannel()");
170
171 RUNTIME_CHECK(isc_log_usechannel(logconfig, "stderr",
172 NULL, NULL) == ISC_R_SUCCESS);
173
174 *logp = log;
175 }
176
177 void
178 cleanup_logging(isc_log_t **logp) {
179 isc_log_t *log;
180
181 REQUIRE(logp != NULL);
182
183 log = *logp;
184 *logp = NULL;
185
186 if (log == NULL)
187 return;
188
189 isc_log_destroy(&log);
190 isc_log_setcontext(NULL);
191 dns_log_setcontext(NULL);
192 }
193
194 static isc_stdtime_t
195 time_units(isc_stdtime_t offset, char *suffix, const char *str) {
196 switch (suffix[0]) {
197 case 'Y': case 'y':
198 return (offset * (365 * 24 * 3600));
199 case 'M': case 'm':
200 switch (suffix[1]) {
201 case 'O': case 'o':
202 return (offset * (30 * 24 * 3600));
203 case 'I': case 'i':
204 return (offset * 60);
205 case '\0':
206 fatal("'%s' ambiguous: use 'mi' for minutes "
207 "or 'mo' for months", str);
208 default:
209 fatal("time value %s is invalid", str);
210 }
211 /* NOTREACHED */
212 break;
213 case 'W': case 'w':
214 return (offset * (7 * 24 * 3600));
215 case 'D': case 'd':
216 return (offset * (24 * 3600));
217 case 'H': case 'h':
218 return (offset * 3600);
219 case 'S': case 's': case '\0':
220 return (offset);
221 default:
222 fatal("time value %s is invalid", str);
223 }
224 /* NOTREACHED */
225 return(0); /* silence compiler warning */
226 }
227
228 static inline bool
229 isnone(const char *str) {
230 return ((strcasecmp(str, "none") == 0) ||
231 (strcasecmp(str, "never") == 0));
232 }
233
234 dns_ttl_t
235 strtottl(const char *str) {
236 const char *orig = str;
237 dns_ttl_t ttl;
238 char *endp;
239
240 if (isnone(str))
241 return ((dns_ttl_t) 0);
242
243 ttl = strtol(str, &endp, 0);
244 if (ttl == 0 && endp == str)
245 fatal("TTL must be numeric");
246 ttl = time_units(ttl, endp, orig);
247 return (ttl);
248 }
249
250 isc_stdtime_t
251 strtotime(const char *str, int64_t now, int64_t base,
252 bool *setp)
253 {
254 int64_t val, offset;
255 isc_result_t result;
256 const char *orig = str;
257 char *endp;
258 size_t n;
259
260 if (isnone(str)) {
261 if (setp != NULL)
262 *setp = false;
263 return ((isc_stdtime_t) 0);
264 }
265
266 if (setp != NULL)
267 *setp = true;
268
269 if ((str[0] == '0' || str[0] == '-') && str[1] == '\0')
270 return ((isc_stdtime_t) 0);
271
272 /*
273 * We accept times in the following formats:
274 * now([+-]offset)
275 * YYYYMMDD([+-]offset)
276 * YYYYMMDDhhmmss([+-]offset)
277 * [+-]offset
278 */
279 n = strspn(str, "0123456789");
280 if ((n == 8u || n == 14u) &&
281 (str[n] == '\0' || str[n] == '-' || str[n] == '+'))
282 {
283 char timestr[15];
284
285 strlcpy(timestr, str, sizeof(timestr));
286 timestr[n] = 0;
287 if (n == 8u)
288 strlcat(timestr, "000000", sizeof(timestr));
289 result = dns_time64_fromtext(timestr, &val);
290 if (result != ISC_R_SUCCESS)
291 fatal("time value %s is invalid: %s", orig,
292 isc_result_totext(result));
293 base = val;
294 str += n;
295 } else if (strncmp(str, "now", 3) == 0) {
296 base = now;
297 str += 3;
298 }
299
300 if (str[0] == '\0')
301 return ((isc_stdtime_t) base);
302 else if (str[0] == '+') {
303 offset = strtol(str + 1, &endp, 0);
304 offset = time_units((isc_stdtime_t) offset, endp, orig);
305 val = base + offset;
306 } else if (str[0] == '-') {
307 offset = strtol(str + 1, &endp, 0);
308 offset = time_units((isc_stdtime_t) offset, endp, orig);
309 val = base - offset;
310 } else
311 fatal("time value %s is invalid", orig);
312
313 return ((isc_stdtime_t) val);
314 }
315
316 dns_rdataclass_t
317 strtoclass(const char *str) {
318 isc_textregion_t r;
319 dns_rdataclass_t rdclass;
320 isc_result_t ret;
321
322 if (str == NULL)
323 return dns_rdataclass_in;
324 DE_CONST(str, r.base);
325 r.length = strlen(str);
326 ret = dns_rdataclass_fromtext(&rdclass, &r);
327 if (ret != ISC_R_SUCCESS)
328 fatal("unknown class %s", str);
329 return (rdclass);
330 }
331
332 unsigned int
333 strtodsdigest(const char *algname) {
334 if (strcasecmp(algname, "SHA1") == 0 ||
335 strcasecmp(algname, "SHA-1") == 0)
336 {
337 return (DNS_DSDIGEST_SHA1);
338 } else if (strcasecmp(algname, "SHA256") == 0 ||
339 strcasecmp(algname, "SHA-256") == 0)
340 {
341 return (DNS_DSDIGEST_SHA256);
342 } else if (strcasecmp(algname, "SHA384") == 0 ||
343 strcasecmp(algname, "SHA-384") == 0)
344 {
345 return (DNS_DSDIGEST_SHA384);
346 } else {
347 fatal("unknown algorithm %s", algname);
348 }
349 }
350
351 isc_result_t
352 try_dir(const char *dirname) {
353 isc_result_t result;
354 isc_dir_t d;
355
356 isc_dir_init(&d);
357 result = isc_dir_open(&d, dirname);
358 if (result == ISC_R_SUCCESS) {
359 isc_dir_close(&d);
360 }
361 return (result);
362 }
363
364 /*
365 * Check private key version compatibility.
366 */
367 void
368 check_keyversion(dst_key_t *key, char *keystr) {
369 int major, minor;
370 dst_key_getprivateformat(key, &major, &minor);
371 INSIST(major <= DST_MAJOR_VERSION); /* invalid private key */
372
373 if (major < DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
374 fatal("Key %s has incompatible format version %d.%d, "
375 "use -f to force upgrade to new version.",
376 keystr, major, minor);
377 if (minor > DST_MINOR_VERSION)
378 fatal("Key %s has incompatible format version %d.%d, "
379 "use -f to force downgrade to current version.",
380 keystr, major, minor);
381 }
382
383 void
384 set_keyversion(dst_key_t *key) {
385 int major, minor;
386 dst_key_getprivateformat(key, &major, &minor);
387 INSIST(major <= DST_MAJOR_VERSION);
388
389 if (major != DST_MAJOR_VERSION || minor != DST_MINOR_VERSION)
390 dst_key_setprivateformat(key, DST_MAJOR_VERSION,
391 DST_MINOR_VERSION);
392
393 /*
394 * If the key is from a version older than 1.3, set
395 * set the creation date
396 */
397 if (major < 1 || (major == 1 && minor <= 2)) {
398 isc_stdtime_t now;
399 isc_stdtime_get(&now);
400 dst_key_settime(key, DST_TIME_CREATED, now);
401 }
402 }
403
404 bool
405 key_collision(dst_key_t *dstkey, dns_name_t *name, const char *dir,
406 isc_mem_t *mctx, bool *exact)
407 {
408 isc_result_t result;
409 bool conflict = false;
410 dns_dnsseckeylist_t matchkeys;
411 dns_dnsseckey_t *key = NULL;
412 uint16_t id, oldid;
413 uint32_t rid, roldid;
414 dns_secalg_t alg;
415 char filename[NAME_MAX];
416 isc_buffer_t fileb;
417 isc_stdtime_t now;
418
419 if (exact != NULL)
420 *exact = false;
421
422 id = dst_key_id(dstkey);
423 rid = dst_key_rid(dstkey);
424 alg = dst_key_alg(dstkey);
425
426 /*
427 * For Diffie Hellman just check if there is a direct collision as
428 * they can't be revoked. Additionally dns_dnssec_findmatchingkeys
429 * only handles DNSKEY which is not used for HMAC.
430 */
431 if (alg == DST_ALG_DH) {
432 isc_buffer_init(&fileb, filename, sizeof(filename));
433 result = dst_key_buildfilename(dstkey, DST_TYPE_PRIVATE,
434 dir, &fileb);
435 if (result != ISC_R_SUCCESS)
436 return (true);
437 return (isc_file_exists(filename));
438 }
439
440 ISC_LIST_INIT(matchkeys);
441 isc_stdtime_get(&now);
442 result = dns_dnssec_findmatchingkeys(name, dir, now, mctx, &matchkeys);
443 if (result == ISC_R_NOTFOUND)
444 return (false);
445
446 while (!ISC_LIST_EMPTY(matchkeys) && !conflict) {
447 key = ISC_LIST_HEAD(matchkeys);
448 if (dst_key_alg(key->key) != alg)
449 goto next;
450
451 oldid = dst_key_id(key->key);
452 roldid = dst_key_rid(key->key);
453
454 if (oldid == rid || roldid == id || id == oldid) {
455 conflict = true;
456 if (id != oldid) {
457 if (verbose > 1)
458 fprintf(stderr, "Key ID %d could "
459 "collide with %d\n",
460 id, oldid);
461 } else {
462 if (exact != NULL)
463 *exact = true;
464 if (verbose > 1)
465 fprintf(stderr, "Key ID %d exists\n",
466 id);
467 }
468 }
469
470 next:
471 ISC_LIST_UNLINK(matchkeys, key, link);
472 dns_dnsseckey_destroy(mctx, &key);
473 }
474
475 /* Finish freeing the list */
476 while (!ISC_LIST_EMPTY(matchkeys)) {
477 key = ISC_LIST_HEAD(matchkeys);
478 ISC_LIST_UNLINK(matchkeys, key, link);
479 dns_dnsseckey_destroy(mctx, &key);
480 }
481
482 return (conflict);
483 }
484
485 bool
486 isoptarg(const char *arg, char **argv, void(*usage)(void)) {
487 if (!strcasecmp(isc_commandline_argument, arg)) {
488 if (argv[isc_commandline_index] == NULL) {
489 fprintf(stderr, "%s: missing argument -%c %s\n",
490 program, isc_commandline_option,
491 isc_commandline_argument);
492 usage();
493 }
494 isc_commandline_argument = argv[isc_commandline_index];
495 /* skip to next arguement */
496 isc_commandline_index++;
497 return (true);
498 }
499 return (false);
500 }
501
502 #ifdef _WIN32
503 void
504 InitSockets(void) {
505 WORD wVersionRequested;
506 WSADATA wsaData;
507 int err;
508
509 wVersionRequested = MAKEWORD(2, 0);
510
511 err = WSAStartup( wVersionRequested, &wsaData );
512 if (err != 0) {
513 fprintf(stderr, "WSAStartup() failed: %d\n", err);
514 exit(1);
515 }
516 }
517
518 void
519 DestroySockets(void) {
520 WSACleanup();
521 }
522 #endif
523