dnssectool.c revision 3f543c371fff724d1fb05eb564f732476e946b5b
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2000, 2001 Internet Software Consortium.
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * copyright notice and this permission notice appear in all copies.
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews *
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
09b46c39456ea75a414d8fcdb13127ed4917dac2Automatic Updater * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
fb756ba3047770957173ba546257ca43af7ba3e4Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt */
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt/* $Id: dnssectool.c,v 1.33 2001/09/05 23:15:42 bwelling Exp $ */
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews#include <config.h>
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews#include <stdlib.h>
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews#include <isc/buffer.h>
947cf282a721b089c1106780f13ae8e6298bddb1Mark Andrews#include <isc/entropy.h>
1c02dd9dd94c95a9a59bb6c10b669fb790853b10Evan Hunt#include <isc/list.h>
1c02dd9dd94c95a9a59bb6c10b669fb790853b10Evan Hunt#include <isc/mem.h>
1c02dd9dd94c95a9a59bb6c10b669fb790853b10Evan Hunt#include <isc/string.h>
1c02dd9dd94c95a9a59bb6c10b669fb790853b10Evan Hunt#include <isc/time.h>
1c02dd9dd94c95a9a59bb6c10b669fb790853b10Evan Hunt#include <isc/util.h>
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews#include <isc/print.h>
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews#include <dns/log.h>
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews#include <dns/name.h>
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews#include <dns/rdatastruct.h>
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews#include <dns/rdatatype.h>
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews#include <dns/result.h>
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews#include <dns/secalg.h>
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews#include <dns/time.h>
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews#include "dnssectool.h"
d48730a446ffffa8d75462a4abefce030425fa64Mark Andrews
fb604d1cee223986d090763292015309adfcf9c7Mark Andrewsextern int verbose;
d1cacbb37474b0cbee6c1ddd05d27f731b2b43baMark Andrewsextern const char *program;
d1cacbb37474b0cbee6c1ddd05d27f731b2b43baMark Andrews
d1cacbb37474b0cbee6c1ddd05d27f731b2b43baMark Andrewstypedef struct entropysource entropysource_t;
d1cacbb37474b0cbee6c1ddd05d27f731b2b43baMark Andrews
d1cacbb37474b0cbee6c1ddd05d27f731b2b43baMark Andrewsstruct entropysource {
d1cacbb37474b0cbee6c1ddd05d27f731b2b43baMark Andrews isc_entropysource_t *source;
fb604d1cee223986d090763292015309adfcf9c7Mark Andrews isc_mem_t *mctx;
fb604d1cee223986d090763292015309adfcf9c7Mark Andrews ISC_LINK(entropysource_t) link;
fb604d1cee223986d090763292015309adfcf9c7Mark Andrews};
fb604d1cee223986d090763292015309adfcf9c7Mark Andrews
fb604d1cee223986d090763292015309adfcf9c7Mark Andrewsstatic ISC_LIST(entropysource_t) sources;
2786b6c53fb198d742e9738294fa0a51a52e4dceckbstatic fatalcallback_t *fatalcallback = NULL;
2786b6c53fb198d742e9738294fa0a51a52e4dceckb
2786b6c53fb198d742e9738294fa0a51a52e4dceckbvoid
2786b6c53fb198d742e9738294fa0a51a52e4dceckbfatal(const char *format, ...) {
2786b6c53fb198d742e9738294fa0a51a52e4dceckb va_list args;
2786b6c53fb198d742e9738294fa0a51a52e4dceckb
947cf282a721b089c1106780f13ae8e6298bddb1Mark Andrews fprintf(stderr, "%s: ", program);
947cf282a721b089c1106780f13ae8e6298bddb1Mark Andrews va_start(args, format);
947cf282a721b089c1106780f13ae8e6298bddb1Mark Andrews vfprintf(stderr, format, args);
947cf282a721b089c1106780f13ae8e6298bddb1Mark Andrews va_end(args);
947cf282a721b089c1106780f13ae8e6298bddb1Mark Andrews fprintf(stderr, "\n");
65d59a4307d00f6b4e278b730c278502e5449da8Mark Andrews if (fatalcallback != NULL)
65d59a4307d00f6b4e278b730c278502e5449da8Mark Andrews (*fatalcallback)();
65d59a4307d00f6b4e278b730c278502e5449da8Mark Andrews exit(1);
65d59a4307d00f6b4e278b730c278502e5449da8Mark Andrews}
65d59a4307d00f6b4e278b730c278502e5449da8Mark Andrews
void
setfatalcallback(fatalcallback_t *callback) {
fatalcallback = callback;
}
void
check_result(isc_result_t result, const char *message) {
if (result != ISC_R_SUCCESS) {
fprintf(stderr, "%s: %s: %s\n", program, message,
isc_result_totext(result));
exit(1);
}
}
void
vbprintf(int level, const char *fmt, ...) {
va_list ap;
if (level > verbose)
return;
va_start(ap, fmt);
fprintf(stderr, "%s: ", program);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
void
type_format(const dns_rdatatype_t type, char *cp, unsigned int size) {
isc_buffer_t b;
isc_region_t r;
isc_result_t result;
isc_buffer_init(&b, cp, size - 1);
result = dns_rdatatype_totext(type, &b);
check_result(result, "dns_rdatatype_totext()");
isc_buffer_usedregion(&b, &r);
r.base[r.length] = 0;
}
void
alg_format(const dns_secalg_t alg, char *cp, unsigned int size) {
isc_buffer_t b;
isc_region_t r;
isc_result_t result;
isc_buffer_init(&b, cp, size - 1);
result = dns_secalg_totext(alg, &b);
check_result(result, "dns_secalg_totext()");
isc_buffer_usedregion(&b, &r);
r.base[r.length] = 0;
}
void
sig_format(dns_rdata_sig_t *sig, char *cp, unsigned int size) {
char namestr[DNS_NAME_FORMATSIZE];
char algstr[DNS_NAME_FORMATSIZE];
dns_name_format(&sig->signer, namestr, sizeof namestr);
alg_format(sig->algorithm, algstr, sizeof algstr);
snprintf(cp, size, "%s/%s/%d", namestr, algstr, sig->keyid);
}
void
key_format(const dst_key_t *key, char *cp, unsigned int size) {
char namestr[DNS_NAME_FORMATSIZE];
char algstr[DNS_NAME_FORMATSIZE];
dns_name_format(dst_key_name(key), namestr, sizeof namestr);
alg_format((dns_secalg_t) dst_key_alg(key), algstr, sizeof algstr);
snprintf(cp, size, "%s/%s/%d", namestr, algstr, dst_key_id(key));
}
void
setup_logging(int verbose, isc_mem_t *mctx, isc_log_t **logp) {
isc_result_t result;
isc_logdestination_t destination;
isc_logconfig_t *logconfig = NULL;
isc_log_t *log = NULL;
int level;
switch (verbose) {
case 0:
/*
* We want to see warnings about things like out-of-zone
* data in the master file even when not verbose.
*/
level = ISC_LOG_WARNING;
break;
case 1:
level = ISC_LOG_INFO;
break;
default:
level = ISC_LOG_DEBUG(verbose - 2 + 1);
break;
}
RUNTIME_CHECK(isc_log_create(mctx, &log, &logconfig) == ISC_R_SUCCESS);
isc_log_setcontext(log);
dns_log_init(log);
dns_log_setcontext(log);
RUNTIME_CHECK(isc_log_settag(logconfig, program) == ISC_R_SUCCESS);
/*
* Set up a channel similar to default_stderr except:
* - the logging level is passed in
* - the program name and logging level are printed
* - no time stamp is printed
*/
destination.file.stream = stderr;
destination.file.name = NULL;
destination.file.versions = ISC_LOG_ROLLNEVER;
destination.file.maximum_size = 0;
result = isc_log_createchannel(logconfig, "stderr",
ISC_LOG_TOFILEDESC,
level,
&destination,
ISC_LOG_PRINTTAG|ISC_LOG_PRINTLEVEL);
check_result(result, "isc_log_createchannel()");
RUNTIME_CHECK(isc_log_usechannel(logconfig, "stderr",
NULL, NULL) == ISC_R_SUCCESS);
*logp = log;
}
void
cleanup_logging(isc_log_t **logp) {
isc_log_t *log;
REQUIRE(logp != NULL);
log = *logp;
if (log == NULL)
return;
isc_log_destroy(&log);
isc_log_setcontext(NULL);
dns_log_setcontext(NULL);
logp = NULL;
}
void
setup_entropy(isc_mem_t *mctx, const char *randomfile, isc_entropy_t **ectx) {
isc_result_t result;
isc_entropysource_t *source = NULL;
entropysource_t *elt;
int usekeyboard = ISC_ENTROPY_KEYBOARDMAYBE;
REQUIRE(ectx != NULL);
if (*ectx == NULL) {
result = isc_entropy_create(mctx, ectx);
if (result != ISC_R_SUCCESS)
fatal("could not create entropy object");
ISC_LIST_INIT(sources);
}
if (randomfile != NULL && strcmp(randomfile, "keyboard") == 0) {
usekeyboard = ISC_ENTROPY_KEYBOARDYES;
randomfile = NULL;
}
result = isc_entropy_usebestsource(*ectx, &source, randomfile,
usekeyboard);
if (result != ISC_R_SUCCESS)
fatal("could not initialize entropy source: %s",
isc_result_totext(result));
if (source != NULL) {
elt = isc_mem_get(mctx, sizeof *elt);
if (elt == NULL)
fatal("out of memory");
elt->source = source;
elt->mctx = mctx;
ISC_LINK_INIT(elt, link);
ISC_LIST_APPEND(sources, elt, link);
}
}
void
cleanup_entropy(isc_entropy_t **ectx) {
entropysource_t *source;
while (!ISC_LIST_EMPTY(sources)) {
source = ISC_LIST_HEAD(sources);
ISC_LIST_UNLINK(sources, source, link);
isc_entropy_destroysource(&source->source);
isc_mem_put(source->mctx, source, sizeof *source);
}
isc_entropy_detach(ectx);
}
isc_stdtime_t
strtotime(char *str, isc_int64_t now, isc_int64_t base) {
isc_int64_t val, offset;
isc_result_t result;
char *endp;
if (str[0] == '+') {
offset = strtol(str + 1, &endp, 0);
if (*endp != '\0')
fatal("time value %s is invalid", str);
val = base + offset;
} else if (strncmp(str, "now+", 4) == 0) {
offset = strtol(str + 4, &endp, 0);
if (*endp != '\0')
fatal("time value %s is invalid", str);
val = now + offset;
} else if (strlen(str) == 8) {
char timestr[15];
sprintf(timestr, "%s000000", str);
result = dns_time64_fromtext(timestr, &val);
if (result != ISC_R_SUCCESS)
fatal("time value %s is invalid", str);
} else {
result = dns_time64_fromtext(str, &val);
if (result != ISC_R_SUCCESS)
fatal("time value %s is invalid", str);
}
return ((isc_stdtime_t) val);
}