2008-11-07 02:28:49 +00:00
|
|
|
/*
|
2014-01-16 15:19:24 +11:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2008-11-07 02:28:49 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02:00
|
|
|
*
|
2008-11-07 02:28:49 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
2008-11-07 02:28:49 +00:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
2014-06-13 06:27:43 +05:30
|
|
|
* information regarding copyright ownership.
|
2008-11-07 02:28:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2008-11-07 02:28:49 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2018-08-07 16:46:53 +02:00
|
|
|
#include <isc/attributes.h>
|
2008-11-07 02:28:49 +00:00
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/commandline.h>
|
2021-06-10 08:14:57 +02:00
|
|
|
#include <isc/dir.h>
|
2008-11-07 02:28:49 +00:00
|
|
|
#include <isc/hash.h>
|
2025-02-04 13:17:31 +01:00
|
|
|
#include <isc/lib.h>
|
2024-08-14 13:25:50 +02:00
|
|
|
#include <isc/log.h>
|
2008-11-07 02:28:49 +00:00
|
|
|
#include <isc/mem.h>
|
2021-10-04 17:14:53 +02:00
|
|
|
#include <isc/result.h>
|
2008-11-07 02:28:49 +00:00
|
|
|
#include <isc/string.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2011-03-24 02:10:23 +00:00
|
|
|
#include <dns/callbacks.h>
|
2008-11-07 02:28:49 +00:00
|
|
|
#include <dns/db.h>
|
|
|
|
#include <dns/dbiterator.h>
|
|
|
|
#include <dns/ds.h>
|
|
|
|
#include <dns/fixedname.h>
|
2009-07-19 04:18:05 +00:00
|
|
|
#include <dns/keyvalues.h>
|
2025-02-04 13:17:31 +01:00
|
|
|
#include <dns/lib.h>
|
2009-07-19 04:18:05 +00:00
|
|
|
#include <dns/master.h>
|
2008-11-07 02:28:49 +00:00
|
|
|
#include <dns/name.h>
|
|
|
|
#include <dns/rdata.h>
|
|
|
|
#include <dns/rdataclass.h>
|
|
|
|
#include <dns/rdataset.h>
|
|
|
|
#include <dns/rdatasetiter.h>
|
|
|
|
#include <dns/rdatatype.h>
|
|
|
|
|
|
|
|
#include <dst/dst.h>
|
|
|
|
|
|
|
|
#include "dnssectool.h"
|
|
|
|
|
|
|
|
static dns_rdataclass_t rdclass;
|
2009-06-17 06:51:44 +00:00
|
|
|
static dns_fixedname_t fixed;
|
|
|
|
static dns_name_t *name = NULL;
|
2018-03-28 14:19:37 +02:00
|
|
|
static uint32_t ttl;
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool emitttl = false;
|
2024-03-04 10:14:56 +01:00
|
|
|
static unsigned int split_width = 0;
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2009-07-19 04:18:05 +00:00
|
|
|
static isc_result_t
|
|
|
|
initname(char *setname) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_buffer_t buf;
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2018-03-28 14:38:09 +02:00
|
|
|
name = dns_fixedname_initname(&fixed);
|
2009-07-19 23:47:55 +00:00
|
|
|
|
|
|
|
isc_buffer_init(&buf, setname, strlen(setname));
|
|
|
|
isc_buffer_add(&buf, strlen(setname));
|
2025-02-22 00:11:38 -08:00
|
|
|
result = dns_name_fromtext(name, &buf, dns_rootname, 0);
|
2009-07-19 04:18:05 +00:00
|
|
|
return result;
|
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2011-03-24 02:10:23 +00:00
|
|
|
static void
|
|
|
|
db_load_from_stream(dns_db_t *db, FILE *fp) {
|
|
|
|
isc_result_t result;
|
|
|
|
dns_rdatacallbacks_t callbacks;
|
|
|
|
|
|
|
|
dns_rdatacallbacks_init(&callbacks);
|
2012-06-20 14:13:12 -05:00
|
|
|
result = dns_db_beginload(db, &callbacks);
|
2011-03-24 02:10:23 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
fatal("dns_db_beginload failed: %s", isc_result_totext(result));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2011-03-24 02:10:23 +00:00
|
|
|
|
|
|
|
result = dns_master_loadstream(fp, name, name, rdclass, 0, &callbacks,
|
2025-07-15 12:56:04 +02:00
|
|
|
isc_g_mctx);
|
2011-03-24 02:10:23 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
fatal("can't load from input: %s", isc_result_totext(result));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2011-03-24 23:47:48 +00:00
|
|
|
|
2012-06-20 14:13:12 -05:00
|
|
|
result = dns_db_endload(db, &callbacks);
|
2011-03-24 02:10:23 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
fatal("dns_db_endload failed: %s", isc_result_totext(result));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2011-03-24 02:10:23 +00:00
|
|
|
}
|
|
|
|
|
2009-07-19 04:18:05 +00:00
|
|
|
static isc_result_t
|
2011-03-24 02:10:23 +00:00
|
|
|
loadset(const char *filename, dns_rdataset_t *rdataset) {
|
2009-07-19 04:18:05 +00:00
|
|
|
isc_result_t result;
|
|
|
|
dns_db_t *db = NULL;
|
|
|
|
dns_dbnode_t *node = NULL;
|
|
|
|
char setname[DNS_NAME_FORMATSIZE];
|
2009-06-17 06:51:44 +00:00
|
|
|
|
2009-07-19 04:18:05 +00:00
|
|
|
dns_name_format(name, setname, sizeof(setname));
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2025-07-15 12:56:04 +02:00
|
|
|
result = dns_db_create(isc_g_mctx, ZONEDB_DEFAULT, name,
|
|
|
|
dns_dbtype_zone, rdclass, 0, NULL, &db);
|
2008-11-07 02:28:49 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
fatal("can't create database");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2011-03-24 02:10:23 +00:00
|
|
|
if (strcmp(filename, "-") == 0) {
|
|
|
|
db_load_from_stream(db, stdin);
|
|
|
|
filename = "input";
|
|
|
|
} else {
|
2018-04-03 13:22:09 +02:00
|
|
|
result = dns_db_load(db, filename, dns_masterformat_text, 0);
|
2011-03-24 02:10:23 +00:00
|
|
|
if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE) {
|
|
|
|
fatal("can't load %s: %s", filename,
|
|
|
|
isc_result_totext(result));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2011-03-24 02:10:23 +00:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
result = dns_db_findnode(db, name, false, &node);
|
2008-11-07 02:28:49 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
fatal("can't find %s node in %s", setname, filename);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
|
|
|
result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_dnskey, 0, 0,
|
2009-07-19 04:18:05 +00:00
|
|
|
rdataset, NULL);
|
|
|
|
|
2008-11-07 02:28:49 +00:00
|
|
|
if (result == ISC_R_NOTFOUND) {
|
|
|
|
fatal("no DNSKEY RR for %s in %s", setname, filename);
|
|
|
|
} else if (result != ISC_R_SUCCESS) {
|
|
|
|
fatal("dns_db_findrdataset");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
|
|
|
|
if (node != NULL) {
|
Decouple database and node lifetimes by adding node-specific vtables
All databases in the codebase follow the same structure: a database is
an associative container from DNS names to nodes, and each node is an
associative container from RR types to RR data.
Each database implementation (qpzone, qpcache, sdlz, builtin, dyndb) has
its own corresponding node type (qpznode, qpcnode, etc). However, some
code needs to work with nodes generically regardless of their specific
type - for example, to acquire locks, manage references, or
register/unregister slabs from the heap.
Currently, these generic node operations are implemented as methods in
the database vtable, which creates problematic coupling between database
and node lifetimes. If a node outlives its parent database, the node
destructor will destroy all RR data, and each RR data destructor will
try to unregister from heaps by calling a virtual function from the
database vtable. Since the database was already freed, this causes a
crash.
This commit breaks the coupling by standardizing the layout of all
database nodes, adding a dedicated vtable for node operations, and
moving node-specific methods from the database vtable to the node
vtable.
2025-06-05 11:51:29 +02:00
|
|
|
dns_db_detachnode(&node);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
if (db != NULL) {
|
|
|
|
dns_db_detach(&db);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
loadkeyset(char *dirname, dns_rdataset_t *rdataset) {
|
|
|
|
isc_result_t result;
|
|
|
|
char filename[PATH_MAX + 1];
|
|
|
|
isc_buffer_t buf;
|
|
|
|
|
|
|
|
dns_rdataset_init(rdataset);
|
|
|
|
|
|
|
|
isc_buffer_init(&buf, filename, sizeof(filename));
|
|
|
|
if (dirname != NULL) {
|
|
|
|
/* allow room for a trailing slash */
|
|
|
|
if (strlen(dirname) >= isc_buffer_availablelength(&buf)) {
|
|
|
|
return ISC_R_NOSPACE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
isc_buffer_putstr(&buf, dirname);
|
|
|
|
if (dirname[strlen(dirname) - 1] != '/') {
|
|
|
|
isc_buffer_putstr(&buf, "/");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isc_buffer_availablelength(&buf) < 7) {
|
|
|
|
return ISC_R_NOSPACE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
isc_buffer_putstr(&buf, "keyset-");
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
result = dns_name_tofilenametext(name, false, &buf);
|
2009-07-19 04:18:05 +00:00
|
|
|
check_result(result, "dns_name_tofilenametext()");
|
|
|
|
if (isc_buffer_availablelength(&buf) == 0) {
|
|
|
|
return ISC_R_NOSPACE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
isc_buffer_putuint8(&buf, 0);
|
|
|
|
|
2011-03-24 02:10:23 +00:00
|
|
|
return loadset(filename, rdataset);
|
2008-11-07 02:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-17 00:16:45 +00:00
|
|
|
loadkey(char *filename, unsigned char *key_buf, unsigned int key_buf_size,
|
|
|
|
dns_rdata_t *rdata) {
|
2008-11-07 02:28:49 +00:00
|
|
|
isc_result_t result;
|
|
|
|
dst_key_t *key = NULL;
|
|
|
|
isc_buffer_t keyb;
|
|
|
|
isc_region_t r;
|
|
|
|
|
|
|
|
dns_rdata_init(rdata);
|
|
|
|
|
2009-02-17 00:16:45 +00:00
|
|
|
isc_buffer_init(&keyb, key_buf, key_buf_size);
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2025-07-15 12:56:04 +02:00
|
|
|
result = dst_key_fromnamedfile(filename, NULL, DST_TYPE_PUBLIC,
|
|
|
|
isc_g_mctx, &key);
|
2008-11-07 02:28:49 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2017-06-07 09:02:06 +02:00
|
|
|
fatal("can't load %s.key: %s", filename,
|
2008-11-07 02:28:49 +00:00
|
|
|
isc_result_totext(result));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
|
|
|
if (verbose > 2) {
|
2009-10-12 20:48:12 +00:00
|
|
|
char keystr[DST_KEY_FORMATSIZE];
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2009-10-12 20:48:12 +00:00
|
|
|
dst_key_format(key, keystr, sizeof(keystr));
|
2025-05-28 22:43:38 +02:00
|
|
|
fprintf(stderr, "%s: %s\n", isc_commandline_progname, keystr);
|
2008-11-07 02:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result = dst_key_todns(key, &keyb);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
fatal("can't decode key");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
|
|
|
isc_buffer_usedregion(&keyb, &r);
|
|
|
|
dns_rdata_fromregion(rdata, dst_key_class(key), dns_rdatatype_dnskey,
|
|
|
|
&r);
|
|
|
|
|
|
|
|
rdclass = dst_key_class(key);
|
|
|
|
|
2018-03-28 14:38:09 +02:00
|
|
|
name = dns_fixedname_initname(&fixed);
|
2021-05-21 17:20:44 -07:00
|
|
|
dns_name_copy(dst_key_name(key), name);
|
2008-11-07 02:28:49 +00:00
|
|
|
|
|
|
|
dst_key_free(&key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-08-07 12:27:03 -07:00
|
|
|
logkey(dns_rdata_t *rdata) {
|
2008-11-07 02:28:49 +00:00
|
|
|
isc_result_t result;
|
|
|
|
dst_key_t *key = NULL;
|
|
|
|
isc_buffer_t buf;
|
2009-10-12 20:48:12 +00:00
|
|
|
char keystr[DST_KEY_FORMATSIZE];
|
2008-11-07 02:28:49 +00:00
|
|
|
|
|
|
|
isc_buffer_init(&buf, rdata->data, rdata->length);
|
|
|
|
isc_buffer_add(&buf, rdata->length);
|
2025-07-15 12:56:04 +02:00
|
|
|
result = dst_key_fromdns(name, rdclass, &buf, isc_g_mctx, &key);
|
2008-11-07 02:28:49 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2009-10-12 20:48:12 +00:00
|
|
|
dst_key_format(key, keystr, sizeof(keystr));
|
2025-05-28 22:43:38 +02:00
|
|
|
fprintf(stderr, "%s: %s\n", isc_commandline_progname, keystr);
|
2008-11-07 02:28:49 +00:00
|
|
|
|
|
|
|
dst_key_free(&key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-08-07 12:27:03 -07:00
|
|
|
emit(dns_dsdigest_t dt, bool showall, bool cds, dns_rdata_t *rdata) {
|
2009-07-19 04:18:05 +00:00
|
|
|
isc_result_t result;
|
|
|
|
unsigned char buf[DNS_DS_BUFFERSIZE];
|
|
|
|
char text_buf[DST_KEY_MAXTEXTSIZE];
|
|
|
|
char name_buf[DNS_NAME_MAXWIRE];
|
|
|
|
char class_buf[10];
|
|
|
|
isc_buffer_t textb, nameb, classb;
|
|
|
|
isc_region_t r;
|
|
|
|
dns_rdata_t ds;
|
|
|
|
dns_rdata_dnskey_t dnskey;
|
2008-11-07 02:28:49 +00:00
|
|
|
|
|
|
|
isc_buffer_init(&textb, text_buf, sizeof(text_buf));
|
2009-06-17 06:51:44 +00:00
|
|
|
isc_buffer_init(&nameb, name_buf, sizeof(name_buf));
|
2008-11-07 02:28:49 +00:00
|
|
|
isc_buffer_init(&classb, class_buf, sizeof(class_buf));
|
|
|
|
|
|
|
|
dns_rdata_init(&ds);
|
|
|
|
|
2009-07-19 04:18:05 +00:00
|
|
|
result = dns_rdata_tostruct(rdata, &dnskey, NULL);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
fatal("can't convert DNSKEY");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
|
2021-10-05 10:01:54 +11:00
|
|
|
if ((dnskey.flags & DNS_KEYFLAG_REVOKE) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-19 04:18:05 +00:00
|
|
|
if ((dnskey.flags & DNS_KEYFLAG_KSK) == 0 && !showall) {
|
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
|
2025-05-21 10:13:04 +10:00
|
|
|
result = dns_ds_buildrdata(name, rdata, dt, buf, sizeof(buf), &ds);
|
2008-11-07 02:28:49 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2009-06-17 06:51:44 +00:00
|
|
|
fatal("can't build record");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-17 06:51:44 +00:00
|
|
|
|
2023-08-15 18:52:17 -07:00
|
|
|
result = dns_name_totext(name, 0, &nameb);
|
2009-06-17 06:51:44 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
fatal("can't print name");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-17 06:51:44 +00:00
|
|
|
|
2024-03-04 10:14:56 +01:00
|
|
|
result = dns_rdata_tofmttext(&ds, (dns_name_t *)NULL, 0, 0, split_width,
|
|
|
|
"", &textb);
|
2012-11-27 14:22:28 +11:00
|
|
|
|
2008-11-07 02:28:49 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2009-06-17 06:51:44 +00:00
|
|
|
fatal("can't print rdata");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
|
|
|
result = dns_rdataclass_totext(rdclass, &classb);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2009-06-17 06:51:44 +00:00
|
|
|
fatal("can't print class");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2009-06-17 06:51:44 +00:00
|
|
|
isc_buffer_usedregion(&nameb, &r);
|
2011-08-18 04:52:35 +00:00
|
|
|
printf("%.*s ", (int)r.length, r.base);
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2015-02-06 17:01:50 +11:00
|
|
|
if (emitttl) {
|
2011-10-25 01:54:22 +00:00
|
|
|
printf("%u ", ttl);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2011-10-25 01:54:22 +00:00
|
|
|
|
2008-11-07 02:28:49 +00:00
|
|
|
isc_buffer_usedregion(&classb, &r);
|
2011-08-18 04:52:35 +00:00
|
|
|
printf("%.*s", (int)r.length, r.base);
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2019-08-07 12:27:03 -07:00
|
|
|
if (cds) {
|
|
|
|
printf(" CDS ");
|
|
|
|
} else {
|
|
|
|
printf(" DS ");
|
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
|
|
|
isc_buffer_usedregion(&textb, &r);
|
2011-08-18 04:52:35 +00:00
|
|
|
printf("%.*s\n", (int)r.length, r.base);
|
2008-11-07 02:28:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 17:05:57 +00:00
|
|
|
static void
|
2019-08-07 12:27:03 -07:00
|
|
|
emits(bool showall, bool cds, dns_rdata_t *rdata) {
|
2022-09-07 17:22:47 +02:00
|
|
|
unsigned int i, n;
|
2019-01-31 17:05:57 +00:00
|
|
|
|
|
|
|
n = sizeof(dtype) / sizeof(dtype[0]);
|
|
|
|
for (i = 0; i < n; i++) {
|
2025-06-05 14:49:10 +10:00
|
|
|
if (dtype[i] == DNS_DSDIGEST_SHA1) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: DS digest type %u is deprecated\n",
|
|
|
|
i);
|
|
|
|
}
|
2019-01-31 17:05:57 +00:00
|
|
|
if (dtype[i] != 0) {
|
2019-08-07 12:27:03 -07:00
|
|
|
emit(dtype[i], showall, cds, rdata);
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-11 16:15:40 +03:00
|
|
|
ISC_NORETURN static void
|
2018-08-07 16:46:53 +02:00
|
|
|
usage(void);
|
2009-09-29 15:06:07 +00:00
|
|
|
|
2008-11-07 02:28:49 +00:00
|
|
|
static void
|
|
|
|
usage(void) {
|
|
|
|
fprintf(stderr, "Usage:\n");
|
2025-05-28 22:43:38 +02:00
|
|
|
fprintf(stderr, " %s [options] keyfile\n\n",
|
|
|
|
isc_commandline_progname);
|
|
|
|
fprintf(stderr, " %s [options] -f zonefile [zonename]\n\n",
|
|
|
|
isc_commandline_progname);
|
|
|
|
fprintf(stderr, " %s [options] -s dnsname\n\n",
|
|
|
|
isc_commandline_progname);
|
|
|
|
fprintf(stderr, " %s [-h|-V]\n\n", isc_commandline_progname);
|
2020-05-21 10:04:31 +02:00
|
|
|
fprintf(stderr, "Version: %s\n", PACKAGE_VERSION);
|
2019-01-31 16:41:29 +00:00
|
|
|
fprintf(stderr, "Options:\n"
|
2025-06-05 14:49:10 +10:00
|
|
|
" -1: digest algorithm SHA-1 (deprecated)\n"
|
2019-01-31 16:41:29 +00:00
|
|
|
" -2: digest algorithm SHA-256\n"
|
2025-06-05 14:49:10 +10:00
|
|
|
" -a algorithm: digest algorithm (SHA-1 "
|
|
|
|
"(deprecated), SHA-256 or SHA-384)\n"
|
2019-01-31 16:41:29 +00:00
|
|
|
" -A: include all keys in DS set, not just KSKs (-f "
|
|
|
|
"only)\n"
|
|
|
|
" -c class: rdata class for DS set (default IN) (-f "
|
|
|
|
"or -s only)\n"
|
|
|
|
" -C: print CDS records\n"
|
|
|
|
" -f zonefile: read keys from a zone file\n"
|
|
|
|
" -h: print help information\n"
|
|
|
|
" -K directory: where to find key or keyset files\n"
|
2024-03-04 10:14:56 +01:00
|
|
|
" -w split base64 rdata text into chunks\n"
|
2019-01-31 16:41:29 +00:00
|
|
|
" -s: read keys from keyset-<dnsname> file\n"
|
|
|
|
" -T: TTL of output records (omitted by default)\n"
|
|
|
|
" -v level: verbosity\n"
|
|
|
|
" -V: print version information\n");
|
2019-08-07 12:27:03 -07:00
|
|
|
fprintf(stderr, "Output: DS or CDS RRs\n");
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2024-02-07 14:50:38 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-11-07 02:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv) {
|
2017-10-05 01:04:18 -07:00
|
|
|
char *classname = NULL;
|
2009-07-19 04:18:05 +00:00
|
|
|
char *filename = NULL, *dir = NULL, *namestr;
|
2019-10-01 14:06:53 +10:00
|
|
|
char *endp, *arg1;
|
2009-06-17 06:51:44 +00:00
|
|
|
int ch;
|
2019-01-31 17:05:57 +00:00
|
|
|
bool cds = false;
|
|
|
|
bool usekeyset = false;
|
|
|
|
bool showall = false;
|
2009-06-17 06:51:44 +00:00
|
|
|
isc_result_t result;
|
2009-07-19 04:18:05 +00:00
|
|
|
dns_rdataset_t rdataset;
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2019-01-31 17:05:57 +00:00
|
|
|
if (argc == 1) {
|
2008-11-07 02:28:49 +00:00
|
|
|
usage();
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2025-05-28 22:43:38 +02:00
|
|
|
isc_commandline_init(argc, argv);
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
isc_commandline_errprint = false;
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2024-12-10 11:10:02 +01:00
|
|
|
#define OPTIONS "12Aa:Cc:d:Ff:K:sT:v:whV"
|
2015-05-27 15:25:45 +10:00
|
|
|
while ((ch = isc_commandline_parse(argc, argv, OPTIONS)) != -1) {
|
2008-11-07 02:28:49 +00:00
|
|
|
switch (ch) {
|
|
|
|
case '1':
|
2019-01-31 17:05:57 +00:00
|
|
|
add_dtype(DNS_DSDIGEST_SHA1);
|
2008-11-07 02:28:49 +00:00
|
|
|
break;
|
|
|
|
case '2':
|
2019-01-31 17:05:57 +00:00
|
|
|
add_dtype(DNS_DSDIGEST_SHA256);
|
2008-11-07 02:28:49 +00:00
|
|
|
break;
|
2009-07-19 04:18:05 +00:00
|
|
|
case 'A':
|
2018-04-17 08:29:14 -07:00
|
|
|
showall = true;
|
2009-07-19 04:18:05 +00:00
|
|
|
break;
|
2008-11-07 02:28:49 +00:00
|
|
|
case 'a':
|
2019-01-31 17:05:57 +00:00
|
|
|
add_dtype(strtodsdigest(isc_commandline_argument));
|
2008-11-07 02:28:49 +00:00
|
|
|
break;
|
2015-05-27 15:25:45 +10:00
|
|
|
case 'C':
|
2018-04-17 08:29:14 -07:00
|
|
|
cds = true;
|
2015-05-27 15:25:45 +10:00
|
|
|
break;
|
2008-11-07 02:28:49 +00:00
|
|
|
case 'c':
|
|
|
|
classname = isc_commandline_argument;
|
|
|
|
break;
|
|
|
|
case 'd':
|
2009-07-19 04:18:05 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: the -d option is deprecated; "
|
|
|
|
"use -K\n",
|
2025-05-28 22:43:38 +02:00
|
|
|
isc_commandline_progname);
|
2009-07-19 04:18:05 +00:00
|
|
|
/* fall through */
|
|
|
|
case 'K':
|
|
|
|
dir = isc_commandline_argument;
|
2009-08-13 04:13:58 +00:00
|
|
|
if (strlen(dir) == 0U) {
|
2009-07-19 04:18:05 +00:00
|
|
|
fatal("directory must be non-empty string");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
filename = isc_commandline_argument;
|
2009-06-17 06:51:44 +00:00
|
|
|
break;
|
2008-11-07 02:28:49 +00:00
|
|
|
case 's':
|
2018-04-17 08:29:14 -07:00
|
|
|
usekeyset = true;
|
2008-11-07 02:28:49 +00:00
|
|
|
break;
|
2011-10-25 01:54:22 +00:00
|
|
|
case 'T':
|
2018-04-17 08:29:14 -07:00
|
|
|
emitttl = true;
|
2017-10-05 01:04:18 -07:00
|
|
|
ttl = strtottl(isc_commandline_argument);
|
2011-10-25 01:54:22 +00:00
|
|
|
break;
|
2008-11-07 02:28:49 +00:00
|
|
|
case 'v':
|
|
|
|
verbose = strtol(isc_commandline_argument, &endp, 0);
|
|
|
|
if (*endp != '\0') {
|
|
|
|
fatal("-v must be followed by a number");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
break;
|
2024-03-04 10:14:56 +01:00
|
|
|
case 'w':
|
|
|
|
split_width = UINT_MAX;
|
|
|
|
break;
|
2009-05-07 09:33:52 +00:00
|
|
|
case 'F':
|
2021-10-11 12:09:16 +02:00
|
|
|
/* Reserved for FIPS mode */
|
|
|
|
FALLTHROUGH;
|
2008-11-07 02:28:49 +00:00
|
|
|
case '?':
|
|
|
|
if (isc_commandline_option != '?') {
|
|
|
|
fprintf(stderr, "%s: invalid argument -%c\n",
|
2025-05-28 22:43:38 +02:00
|
|
|
isc_commandline_progname,
|
|
|
|
isc_commandline_option);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2021-10-11 12:09:16 +02:00
|
|
|
FALLTHROUGH;
|
2008-11-07 02:28:49 +00:00
|
|
|
case 'h':
|
2014-06-13 06:27:43 +05:30
|
|
|
/* Does not return. */
|
2008-11-07 02:28:49 +00:00
|
|
|
usage();
|
|
|
|
|
2014-06-13 06:27:43 +05:30
|
|
|
case 'V':
|
|
|
|
/* Does not return. */
|
2025-05-28 22:43:38 +02:00
|
|
|
version(isc_commandline_progname);
|
2014-06-13 06:27:43 +05:30
|
|
|
|
2008-11-07 02:28:49 +00:00
|
|
|
default:
|
2025-05-28 22:43:38 +02:00
|
|
|
fprintf(stderr, "%s: unhandled option -%c\n",
|
|
|
|
isc_commandline_progname,
|
2008-11-07 02:28:49 +00:00
|
|
|
isc_commandline_option);
|
2024-02-07 14:50:38 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-11-07 02:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rdclass = strtoclass(classname);
|
|
|
|
|
2019-01-31 17:05:57 +00:00
|
|
|
if (usekeyset && filename != NULL) {
|
2009-07-19 04:18:05 +00:00
|
|
|
fatal("cannot use both -s and -f");
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
|
|
|
|
/* When not using -f, -A is implicit */
|
2019-01-31 17:05:57 +00:00
|
|
|
if (filename == NULL) {
|
2018-04-17 08:29:14 -07:00
|
|
|
showall = true;
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
|
2019-01-31 17:05:57 +00:00
|
|
|
/* Default digest type if none specified. */
|
|
|
|
if (dtype[0] == 0) {
|
|
|
|
dtype[0] = DNS_DSDIGEST_SHA256;
|
|
|
|
}
|
|
|
|
|
2019-10-01 14:06:53 +10:00
|
|
|
/*
|
|
|
|
* Use local variable arg1 so that clang can correctly analyse
|
|
|
|
* reachable paths rather than 'argc < isc_commandline_index + 1'.
|
|
|
|
*/
|
|
|
|
arg1 = argv[isc_commandline_index];
|
|
|
|
if (arg1 == NULL && filename == NULL) {
|
2008-11-07 02:28:49 +00:00
|
|
|
fatal("the key file name was not specified");
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
2019-10-01 14:06:53 +10:00
|
|
|
if (arg1 != NULL && argv[isc_commandline_index + 1] != NULL) {
|
2008-11-07 02:28:49 +00:00
|
|
|
fatal("extraneous arguments");
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2024-08-13 18:20:26 +02:00
|
|
|
setup_logging();
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2009-07-19 04:18:05 +00:00
|
|
|
dns_rdataset_init(&rdataset);
|
|
|
|
|
|
|
|
if (usekeyset || filename != NULL) {
|
2019-10-01 14:06:53 +10:00
|
|
|
if (arg1 == NULL) {
|
|
|
|
/* using file name as the zone name */
|
2009-07-19 04:18:05 +00:00
|
|
|
namestr = filename;
|
2019-01-31 17:05:57 +00:00
|
|
|
} else {
|
2019-10-01 14:06:53 +10:00
|
|
|
namestr = arg1;
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2009-07-19 04:18:05 +00:00
|
|
|
result = initname(namestr);
|
2019-01-31 17:05:57 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2009-07-19 04:18:05 +00:00
|
|
|
fatal("could not initialize name %s", namestr);
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
|
2019-01-31 17:05:57 +00:00
|
|
|
if (usekeyset) {
|
2009-07-19 04:18:05 +00:00
|
|
|
result = loadkeyset(dir, &rdataset);
|
2019-01-31 17:05:57 +00:00
|
|
|
} else {
|
2019-08-08 13:52:44 +10:00
|
|
|
INSIST(filename != NULL);
|
2011-03-24 02:10:23 +00:00
|
|
|
result = loadset(filename, &rdataset);
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
|
2019-01-31 17:05:57 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2009-07-19 04:18:05 +00:00
|
|
|
fatal("could not load DNSKEY set: %s\n",
|
|
|
|
isc_result_totext(result));
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
2009-07-19 04:18:05 +00:00
|
|
|
|
2025-03-21 23:32:27 -07:00
|
|
|
DNS_RDATASET_FOREACH(&rdataset) {
|
|
|
|
dns_rdata_t rdata = DNS_RDATA_INIT;
|
2009-07-19 04:18:05 +00:00
|
|
|
dns_rdataset_current(&rdataset, &rdata);
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2019-01-31 17:05:57 +00:00
|
|
|
if (verbose > 2) {
|
2008-11-07 02:28:49 +00:00
|
|
|
logkey(&rdata);
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2019-08-07 12:27:03 -07:00
|
|
|
emits(showall, cds, &rdata);
|
2008-11-07 02:28:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-02-17 00:16:45 +00:00
|
|
|
unsigned char key_buf[DST_KEY_MAXSIZE];
|
2025-03-21 23:32:27 -07:00
|
|
|
dns_rdata_t rdata = DNS_RDATA_INIT;
|
2009-02-17 00:16:45 +00:00
|
|
|
|
2019-10-01 14:06:53 +10:00
|
|
|
loadkey(arg1, key_buf, DST_KEY_MAXSIZE, &rdata);
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2019-08-07 12:27:03 -07:00
|
|
|
emits(showall, cds, &rdata);
|
2008-11-07 02:28:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 17:05:57 +00:00
|
|
|
if (dns_rdataset_isassociated(&rdataset)) {
|
2009-07-19 04:18:05 +00:00
|
|
|
dns_rdataset_disassociate(&rdataset);
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
|
|
|
if (verbose > 10) {
|
2025-07-15 12:56:04 +02:00
|
|
|
isc_mem_stats(isc_g_mctx, stdout);
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
|
2009-03-02 03:01:04 +00:00
|
|
|
fflush(stdout);
|
|
|
|
if (ferror(stdout)) {
|
|
|
|
fprintf(stderr, "write error\n");
|
|
|
|
return 1;
|
2019-01-31 17:05:57 +00:00
|
|
|
} else {
|
2009-03-02 03:01:04 +00:00
|
|
|
return 0;
|
2019-01-31 17:05:57 +00:00
|
|
|
}
|
2008-11-07 02:28:49 +00:00
|
|
|
}
|