ldns-keygen: add flag for generating BIND / LDNS filenames

This commit is contained in:
Lucas 2021-12-11 09:28:38 +00:00
parent bfc403b266
commit bea2ef361b

View File

@ -29,11 +29,46 @@ usage(void)
fprintf(stderr, "Usage:\n" fprintf(stderr, "Usage:\n"
"\t%s [-b bits] algorithm\n" "\t%s [-b bits] algorithm\n"
"\t%s -n [-d domain]\n"
"\t%s -r [-a algorithm] [-d domain] [-k] ds|dnskey\n", "\t%s -r [-a algorithm] [-d domain] [-k] ds|dnskey\n",
p, p); p, p, p);
exit(1); exit(1);
} }
static int
print_filename_main(const char *domain, int argc, char *argv[])
{
ldns_key *key;
ldns_rdf *rdf_dname;
ldns_status s;
char *str;
int line_nr;
if (argc != 0)
usage();
s = ldns_key_new_frm_fp_l(&key, stdin, &line_nr);
if (s != LDNS_STATUS_OK)
errx(1, "ldns_key_new_frm_fp_l: (stdin) line %d: %s",
line_nr, ldns_get_errorstr_by_id(s));
s = ldns_str2rdf_dname(&rdf_dname, domain);
if (s != LDNS_STATUS_OK)
errx(1, "ldns_str2rdf_dname: %s", ldns_get_errorstr_by_id(s));
ldns_key_set_pubkey_owner(key, rdf_dname);
str = ldns_key_get_file_base_name(key);
if (str == NULL)
errx(1, "ldns_key_get_file_base_name");
printf("%s\n", str);
free(str);
ldns_key_deep_free(key);
return 0;
}
static int static int
print_record_main(ldns_hash hash, int ksk, const char *domain, int argc, print_record_main(ldns_hash hash, int ksk, const char *domain, int argc,
char *argv[]) char *argv[])
@ -139,14 +174,14 @@ main(int argc, char *argv[])
const char *errstr, *domain; const char *errstr, *domain;
long long n; long long n;
int ch, rc; int ch, rc;
int aflag, bflag, dflag, kflag, rflag; int aflag, bflag, dflag, kflag, nflag, rflag;
uint16_t bits; uint16_t bits;
aflag = bflag = dflag = kflag = rflag = 0; aflag = bflag = dflag = kflag = nflag = rflag = 0;
hash_alg = LDNS_SHA256; hash_alg = LDNS_SHA256;
bits = 0; bits = 0;
domain = "."; domain = ".";
while ((ch = getopt(argc, argv, "a:b:d:kr")) != -1) { while ((ch = getopt(argc, argv, "a:b:d:knr")) != -1) {
switch (ch) { switch (ch) {
case 'a': case 'a':
aflag = 1; aflag = 1;
@ -169,6 +204,9 @@ main(int argc, char *argv[])
case 'k': case 'k':
kflag = 1; kflag = 1;
break; break;
case 'n':
nflag = 1;
break;
case 'r': case 'r':
rflag = 1; rflag = 1;
break; break;
@ -179,17 +217,25 @@ main(int argc, char *argv[])
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (bflag && (aflag || dflag || rflag || kflag)) if (bflag && (aflag || dflag || kflag || nflag || rflag))
errx(1, "-b is mutually exclusive with -d, -k and -r"); errx(1, "-b is mutually exclusive with -d, -k, -n and -r");
if (aflag && !rflag) if (aflag && !rflag)
errx(1, "can't use -a without -r"); errx(1, "can't use -a without -r");
if (dflag && !rflag) if (aflag && nflag)
errx(1, "can't use -d without -r"); errx(1, "-a and -n are mutually exclusive");
if (dflag && !(nflag || rflag))
errx(1, "can't use -d without -n or -r");
if (kflag && !rflag) if (kflag && !rflag)
errx(1, "can't use -k without -r"); errx(1, "can't use -k without -r");
if (kflag && nflag)
errx(1, "-k and -n are mutually exclusive");
if (nflag && rflag)
errx(1, "-n and -r are mutually exclusive");
rc = 1; rc = 1;
if (rflag) if (nflag)
rc = print_filename_main(domain, argc, argv);
else if (rflag)
rc = print_record_main(hash_alg, kflag, domain, argc, argv); rc = print_record_main(hash_alg, kflag, domain, argc, argv);
else else
rc = keygen_main(bits, argc, argv); rc = keygen_main(bits, argc, argv);