chiark / gitweb /
privcache; uncached_load_file: Introduce error_out
[secnet.git] / privcache.c
index ba756ffda8d38117ff766935103e86221b501c7d..c01af9dbc412cbc218256ac01e3919fb8066a1a5 100644 (file)
@@ -37,19 +37,19 @@ struct privcache {
     struct hash_if *defhash;
 };
 
+static struct sigprivkey_if *uncached_load_file(
+                          const struct sigscheme_info *scheme,
+                          const char *path,
+                          struct buffer_if *databuf,
+                          struct hash_if *defhash,
+                          struct log_if *log);
+
 static struct sigprivkey_if *uncached_get(struct privcache *st,
                           const struct sigkeyid *id, struct log_if *log)
 {
-    bool_t ok=False;
-    FILE *f=0;
-
     sprintf(st->path.write_here, SIGKEYID_PR_FMT, SIGKEYID_PR_VAL(id));
 
     const char *path=st->path.buffer;
-    struct hash_if *defhash=st->defhash;
-    struct buffer_if *databuf=&st->databuf;
-
-    struct sigprivkey_if *sigpriv=0;
     const struct sigscheme_info *scheme;
     for (scheme=sigschemes;
         scheme->name;
@@ -59,9 +59,27 @@ static struct sigprivkey_if *uncached_get(struct privcache *st,
 
     slilog(log,M_ERR,"private key file %s not loaded (unknown algid)",
           path);
-    goto out;
+    return 0;
 
  found:
+    return uncached_load_file(scheme,
+                             path,
+                             &st->databuf,
+                             st->defhash,
+                             log);
+}
+
+static struct sigprivkey_if *uncached_load_file(
+                          const struct sigscheme_info *scheme,
+                          const char *path,
+                          struct buffer_if *databuf,
+                          struct hash_if *defhash,
+                          struct log_if *log)
+{
+    bool_t ok=False;
+    FILE *f=0;
+    struct sigprivkey_if *sigpriv=0;
+
     f = fopen(path,"rb");
     if (!f) {
        if (errno == ENOENT) {
@@ -71,7 +89,7 @@ static struct sigprivkey_if *uncached_get(struct privcache *st,
            slilog(log,M_ERR,"failed to open private key file %s",
                   path);
        }
-       goto out;
+       goto error_out;
     }
 
     setbuf(f,0);
@@ -80,12 +98,12 @@ static struct sigprivkey_if *uncached_get(struct privcache *st,
     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;
 
@@ -93,7 +111,7 @@ static struct sigprivkey_if *uncached_get(struct privcache *st,
     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 */
+    if (!ok) goto error_out; /* loadpriv will have logged */
 
     if (sigpriv->sethash) {
        if (!defhash) {
@@ -102,7 +120,7 @@ static struct sigprivkey_if *uncached_get(struct privcache *st,
                   path);
            sigpriv->dispose(sigpriv->st);
            sigpriv=0;
-           goto out;
+           goto error_out;
        }
        sigpriv->sethash(sigpriv->st,defhash);
     }
@@ -110,6 +128,10 @@ static struct sigprivkey_if *uncached_get(struct privcache *st,
   out:
     if (f) fclose(f);
     return ok ? sigpriv : 0;
+
+ error_out:
+    ok=False;
+    goto out;
 }
 
 static struct sigprivkey_if *privcache_lookup(void *sst,