]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
added option for symlinks with constant names
authorA. Schulze <git@andeasschulze.de>
Sun, 22 Nov 2020 16:33:13 +0000 (17:33 +0100)
committerA. Schulze <git@andeasschulze.de>
Sun, 22 Nov 2020 16:33:13 +0000 (17:33 +0100)
examples/ldns-keygen.1
examples/ldns-keygen.c

index 57603c7b3e8ba3e28245a928a6f6f7deb747f696..4bb9fec2dd7075b5af6f38491d438a230c2159fb 100644 (file)
@@ -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
index 237016e5944cd5542ceb7c275cbe0ec837e65835..ddd58f43a47452f18c47baef863420a9a600ca83 100644 (file)
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <unistd.h>
 
 #ifdef HAVE_SSL
 static void
 usage(FILE *fp, char *prog) {
-       fprintf(fp, "%s -a <algorithm> [-b bits] [-r /dev/random] [-v] domain\n",
+       fprintf(fp, "%s -a <algorithm> [-b bits] [-r /dev/random] [-s] [-f] [-v] domain\n",
                   prog);
        fprintf(fp, "  generate a new key pair for domain\n");
        fprintf(fp, "  -a <alg>\tuse the specified algorithm (-a list to");
@@ -27,6 +28,8 @@ usage(FILE *fp, char *prog) {
        fprintf(fp, "  -b <bits>\tspecify the keylength\n");
        fprintf(fp, "  -r <random>\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<name>+<alg>+<id>.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);