From: A. Schulze Date: Sun, 22 Nov 2020 16:33:13 +0000 (+0100) Subject: added option for symlinks with constant names X-Git-Tag: 1.8.0-rc.1~17^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=31e56867b7b27a477fb76682401b0b4c9894a9df;p=thirdparty%2Fldns.git added option for symlinks with constant names --- diff --git a/examples/ldns-keygen.1 b/examples/ldns-keygen.1 index 57603c7b..4bb9fec2 100644 --- a/examples/ldns-keygen.1 +++ b/examples/ldns-keygen.1 @@ -43,6 +43,16 @@ When given, generate a key signing key. This just sets the flag field to Make ldns-keygen use this file to seed the random generator with. This will default to /dev/random. +.TP +\fB-s\fR +ldns-keygen will create symbolic links named \fB.private\fR to +the new generated private key, \fB.key\fR to the public DNSKEY +and \fB.ds\fR to the file containing DS record data. + +.TP +\fB-f\fR +force symlinks to be overwritten if they exist. + .TP \fB-v\fR Show the version and exit diff --git a/examples/ldns-keygen.c b/examples/ldns-keygen.c index 237016e5..ddd58f43 100644 --- a/examples/ldns-keygen.c +++ b/examples/ldns-keygen.c @@ -14,11 +14,12 @@ #include #include #include +#include #ifdef HAVE_SSL static void usage(FILE *fp, char *prog) { - fprintf(fp, "%s -a [-b bits] [-r /dev/random] [-v] domain\n", + fprintf(fp, "%s -a [-b bits] [-r /dev/random] [-s] [-f] [-v] domain\n", prog); fprintf(fp, " generate a new key pair for domain\n"); fprintf(fp, " -a \tuse the specified algorithm (-a list to"); @@ -27,6 +28,8 @@ usage(FILE *fp, char *prog) { fprintf(fp, " -b \tspecify the keylength\n"); fprintf(fp, " -r \tspecify a random device (defaults to /dev/random)\n"); fprintf(fp, "\t\tto seed the random generator with\n"); + fprintf(fp, " -s\t\tcreate additional symlinks with constant names\n"); + fprintf(fp, " -f\t\tforce override of existing symlinks\n"); fprintf(fp, " -v\t\tshow the version and exit\n"); fprintf(fp, " The following files will be created:\n"); fprintf(fp, " K++.key\tPublic key in RR format\n"); @@ -47,6 +50,37 @@ show_algorithms(FILE *out) } } +static int +remove_symlink(const char *symlink_name) +{ + int result; + + if ((result = unlink(symlink_name)) == -1) { + if (errno == ENOENT) { + /* it's OK if the link simply didn't exist */ + result = 0; + } else { + /* error if unlink fail */ + fprintf(stderr, "Can't delete symlink %s: %s\n", symlink_name, strerror(errno)); + } + } + return result; +} + +static int +create_symlink(const char *symlink_destination, const char *symlink_name) +{ + int result = 0; + + if (!symlink_name) + return result; /* no arg "-s" at all */ + + if ((result = symlink(symlink_destination, symlink_name)) == -1) { + fprintf(stderr, "Unable to create symlink %s -> %s: %s\n", symlink_name, symlink_destination, strerror(errno)); + } + return result; +} + int main(int argc, char *argv[]) { @@ -64,6 +98,8 @@ main(int argc, char *argv[]) FILE *random; char *filename; char *owner; + bool symlink_create; + bool symlink_override; ldns_signing_algorithm algorithm; ldns_rdf *domain; @@ -75,8 +111,10 @@ main(int argc, char *argv[]) algorithm = 0; random = NULL; ksk = false; /* don't create a ksk per default */ + symlink_create = false; + symlink_override = false; - while ((c = getopt(argc, argv, "a:kb:r:v")) != -1) { + while ((c = getopt(argc, argv, "a:kb:r:sfv")) != -1) { switch (c) { case 'a': if (algorithm != 0) { @@ -112,6 +150,12 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } break; + case 's': + symlink_create = true; + break; + case 'f': + symlink_override = true; + break; case 'v': printf("DNSSEC key generator version %s (ldns version %s)\n", LDNS_VERSION, ldns_version()); exit(EXIT_SUCCESS); @@ -305,6 +349,19 @@ main(int argc, char *argv[]) break; } + /* maybe a symlinks should be removed */ + if (symlink_create && symlink_override) { + if (remove_symlink(".key") != 0) { + exit(EXIT_FAILURE); + } + if (remove_symlink(".private") != 0) { + exit(EXIT_FAILURE); + } + if (remove_symlink(".ds") != 0) { + exit(EXIT_FAILURE); + } + } + /* print the public key RR to .key */ filename = LDNS_XMALLOC(char, strlen(owner) + 17); snprintf(filename, strlen(owner) + 16, "K%s+%03u+%05u.key", owner, algorithm, (unsigned int) ldns_key_keytag(key)); @@ -323,6 +380,11 @@ main(int argc, char *argv[]) ldns_rr_print(file, pubkey); ldns_rr_set_question(pubkey, false); fclose(file); + if (symlink_create) { + if (create_symlink(filename, ".key") != 0) { + goto silent_fail; + } + } LDNS_FREE(filename); } @@ -342,6 +404,11 @@ main(int argc, char *argv[]) ldns_key_print(file, key); fclose(file); + if (symlink_create) { + if (create_symlink(filename, ".private") != 0) { + goto silent_fail; + } + } LDNS_FREE(filename); /* print the DS to .ds */ @@ -368,6 +435,11 @@ main(int argc, char *argv[]) ldns_rr_print(file, ds); ldns_rr_set_question(ds, false); fclose(file); + if (symlink_create) { + if (create_symlink(filename, ".ds") != 0) { + goto silent_fail; + } + } LDNS_FREE(filename); } } @@ -381,6 +453,7 @@ main(int argc, char *argv[]) fail: fprintf(stderr, "Unable to open %s: %s\n", filename, strerror(errno)); +silentfail: ldns_key_deep_free(key); free(owner); ldns_rr_free(pubkey);