chiark / gitweb /
secnet: loadpriv interface: Return a closure_t too
[secnet.git] / privcache.c
index 298626f2e19c4e416e3b8b7c6949684875f571dc..ba5ddc903a0ee4e5fdb4be83d18236bb45048938 100644 (file)
@@ -37,11 +37,13 @@ struct privcache {
     struct hash_if *defhash;
 };
 
-static struct sigprivkey_if *uncached_load_file(
+static bool_t uncached_load_file(
                           const struct sigscheme_info *scheme,
                           const char *path,
                           struct buffer_if *databuf,
                           struct hash_if *defhash,
+                          struct sigprivkey_if **sigpriv_r,
+                          closure_t **closure_r,
                           struct log_if *log);
 
 static struct sigprivkey_if *uncached_get(struct privcache *st,
@@ -61,26 +63,33 @@ static struct sigprivkey_if *uncached_get(struct privcache *st,
           path);
     return 0;
 
- found:
-    return uncached_load_file(scheme,
+ found:;
+    struct sigprivkey_if *sigpriv;
+    closure_t *cl;
+    bool_t ok=uncached_load_file(scheme,
                              path,
                              &st->databuf,
                              st->defhash,
+                             &sigpriv,
+                             &cl,
                              log);
+    return ok ? sigpriv : 0;
 }
 
-static struct sigprivkey_if *uncached_load_file(
+static bool_t uncached_load_file(
                           const struct sigscheme_info *scheme,
                           const char *path,
                           struct buffer_if *databuf,
                           struct hash_if *defhash,
+                          struct sigprivkey_if **sigpriv_r,
+                          closure_t **closure_r,
                           struct log_if *log)
 {
     bool_t ok=False;
     FILE *f=0;
     struct sigprivkey_if *sigpriv=0;
 
-    f = fopen(path,"rb");
+    f=fopen(path,"rb");
     if (!f) {
        if (errno == ENOENT) {
            slilog(log,M_DEBUG,"private key %s not found",
@@ -89,7 +98,7 @@ static struct sigprivkey_if *uncached_load_file(
            slilog(log,M_ERR,"failed to open private key file %s",
                   path);
        }
-       goto out;
+       goto error_out;
     }
 
     setbuf(f,0);
@@ -98,42 +107,46 @@ static struct sigprivkey_if *uncached_load_file(
     if (ferror(f)) {
        slilog(log,M_ERR,"failed to read private-key file %s",
               path);
-       goto out;
+       goto error_out;
     }
     if (!feof(f)) {
        slilog(log,M_ERR,"private key file %s longer than max %d",
               path, (int)databuf->alloclen);
-       goto out;
+       goto error_out;
     }
     fclose(f); f=0;
 
     databuf->start=databuf->base;
     databuf->size=got;
     struct cloc loc = { .file=path, .line=0 };
-    ok=scheme->loadpriv(scheme, databuf, &sigpriv, log, loc);
-    if (!ok) goto out; /* loadpriv will have logged */
+    ok=scheme->loadpriv(scheme, databuf, &sigpriv, closure_r, log, loc);
+    if (!ok) goto error_out; /* loadpriv will have logged */
 
     if (sigpriv->sethash) {
        if (!defhash) {
            slilog(log,M_ERR,
  "private key %s requires `hash' config key for privcache to load",
                   path);
-           sigpriv->dispose(sigpriv->st);
-           sigpriv=0;
-           goto out;
+           goto error_out;
        }
        sigpriv->sethash(sigpriv->st,defhash);
     }
+    *sigpriv_r=sigpriv;
 
   out:
     if (f) fclose(f);
-    return ok ? sigpriv : 0;
+    return ok;
+
+ error_out:
+    if (sigpriv) sigpriv->dispose(sigpriv->st);
+    ok=False;
+    goto out;
 }
 
 static struct sigprivkey_if *privcache_lookup(void *sst,
                                              const struct sigkeyid *id,
                                              struct log_if *log) {
-    struct privcache *st = sst;
+    struct privcache *st=sst;
     int was;
     struct ent result;
 
@@ -145,10 +158,10 @@ static struct sigprivkey_if *privcache_lookup(void *sst,
     }
 
     if (st->used < st->alloc) {
-       was = st->used;
+       was=st->used;
        st->used++;
     } else {
-       was = st->used-1;
+       was=st->used-1;
        if (st->ents[was].sigpriv) {
            st->ents[was].sigpriv->dispose(st->ents[was].sigpriv->st);
        }
@@ -159,7 +172,7 @@ static struct sigprivkey_if *privcache_lookup(void *sst,
 
  found:
     memmove(&st->ents[1], &st->ents[0], sizeof(st->ents[0]) * was);
-    st->ents[0] = result;
+    st->ents[0]=result;
     return result.sigpriv;
 }