Currently ta, rrdp and rsync repositories use different functions to build
their base path. This diff changes this so that all can use the same
function.

This is a first step to introduce a common validated repository.
-- 
:wq Claudio

Index: repo.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/repo.c,v
retrieving revision 1.14
diff -u -p -r1.14 repo.c
--- repo.c      25 Nov 2021 14:03:40 -0000      1.14
+++ repo.c      3 Dec 2021 15:20:39 -0000
@@ -212,26 +212,15 @@ RB_GENERATE(filepath_tree, filepath, ent
 
 /*
  * Function to hash a string into a unique directory name.
- * prefixed with dir.
+ * Returned hash needs to be freed.
  */
 static char *
-hash_dir(const char *uri, const char *dir)
+hash_dir(const char *uri)
 {
-       const char hex[] = "0123456789abcdef";
        unsigned char m[SHA256_DIGEST_LENGTH];
-       char hash[SHA256_DIGEST_LENGTH * 2 + 1];
-       char *out;
-       size_t i;
 
        SHA256(uri, strlen(uri), m);
-       for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
-               hash[i * 2] = hex[m[i] >> 4];
-               hash[i * 2 + 1] = hex[m[i] & 0xf];
-       }
-       hash[SHA256_DIGEST_LENGTH * 2] = '\0';
-
-       asprintf(&out, "%s/%s", dir, hash);
-       return out;
+       return hex_encode(m, sizeof(m));
 }
 
 /*
@@ -239,13 +228,24 @@ hash_dir(const char *uri, const char *di
  * as prefix. Skip the proto:// in URI but keep everything else.
  */
 static char *
-rsync_dir(const char *uri, const char *dir)
+repo_dir(const char *uri, const char *dir, int hash)
 {
-       char *local, *out;
+       const char *local;
+       char *out, *hdir = NULL;
 
-       local = strchr(uri, ':') + strlen("://");
+       if (hash) {
+               local = hdir = hash_dir(uri);
+       } else {
+               local = strchr(uri, ':');
+               if (local != NULL)
+                       local += strlen("://");
+               else
+                       local = uri;
+       }
 
-       asprintf(&out, "%s/%s", dir, local);
+       if (asprintf(&out, "%s/%s", dir, local) == -1)
+               err(1, NULL);
+       free(hdir);
        return out;
 }
 
@@ -397,8 +397,7 @@ ta_get(struct tal *tal, int nofetch)
 
        if ((tr->descr = strdup(tal->descr)) == NULL)
                err(1, NULL);
-       if (asprintf(&tr->basedir, "ta/%s", tal->descr) == -1)
-               err(1, NULL);
+       tr->basedir = repo_dir(tal->descr, "ta", 0);
 
        /* steal URI infromation from TAL */
        tr->urisz = tal->urisz;
@@ -469,7 +468,7 @@ rsync_get(const char *uri, int nofetch)
        SLIST_INSERT_HEAD(&rsyncrepos, rr, entry);
 
        rr->repouri = repo;
-       rr->basedir = rsync_dir(repo, "rsync");
+       rr->basedir = repo_dir(repo, "rsync", 0);
 
        if (noop || nofetch) {
                rr->state = REPO_DONE;
@@ -536,7 +535,7 @@ rrdp_get(const char *uri, int nofetch)
 
        if ((rr->notifyuri = strdup(uri)) == NULL)
                err(1, NULL);
-       rr->basedir = hash_dir(uri, "rrdp");
+       rr->basedir = repo_dir(uri, "rrdp", 1);
 
        RB_INIT(&rr->added);
        RB_INIT(&rr->deleted);

Reply via email to