chiark / gitweb /
privcache: uncached_get: Introduce `databuf' local
[secnet.git] / privcache.c
index 0ad6a0eb8ca1912dfba9d2dcb41810b47ea54015..f95ee37a6600e0d2b280dc614652b7c31300ad93 100644 (file)
@@ -45,59 +45,68 @@ static struct sigprivkey_if *uncached_get(struct privcache *st,
 
     sprintf(st->path.write_here, SIGKEYID_PR_FMT, SIGKEYID_PR_VAL(id));
 
-    f = fopen(st->path.buffer,"rb");
+    const char *path=st->path.buffer;
+    struct hash_if *defhash=st->defhash;
+    struct buffer_if *databuf=&st->databuf;
+
+    f = fopen(path,"rb");
     if (!f) {
        if (errno == ENOENT) {
-           slilog(log,M_DEBUG,"private key %s not found\n",
-                  st->path.write_here);
+           slilog(log,M_DEBUG,"private key %s not found",
+                  path);
        } else {
-           slilog(log,M_ERR,"failed to open private key file %s\n",
-                  st->path.buffer);
+           slilog(log,M_ERR,"failed to open private key file %s",
+                  path);
        }
        goto out;
     }
 
     setbuf(f,0);
-    buffer_init(&st->databuf,0);
-    ssize_t got=fread(st->databuf.base,1,st->databuf.alloclen,f);
+    buffer_init(databuf,0);
+    ssize_t got=fread(databuf->base,1,databuf->alloclen,f);
     if (ferror(f)) {
-       slilog(log,M_ERR,"failed to read private-key file %s\n",
-              st->path.buffer);
+       slilog(log,M_ERR,"failed to read private-key file %s",
+              path);
        goto out;
     }
     if (!feof(f)) {
-       slilog(log,M_ERR,"private key file %s longer than max %d\n",
-              st->path.buffer, (int)st->databuf.alloclen);
+       slilog(log,M_ERR,"private key file %s longer than max %d",
+              path, (int)databuf->alloclen);
        goto out;
     }
     fclose(f); f=0;
 
     struct sigprivkey_if *sigpriv=0;
-    for (const struct sigscheme_info *scheme=sigschemes;
+    const struct sigscheme_info *scheme;
+    for (scheme=sigschemes;
         scheme->name;
-        scheme++) {
-       st->databuf.start=st->databuf.base;
-       st->databuf.size=got;
-       ok=scheme->loadpriv(scheme, &st->databuf, &sigpriv, log);
-       if (ok) {
-           if (sigpriv->sethash) {
-               if (!st->defhash) {
-                   slilog(log,M_ERR,
+        scheme++)
+       if (scheme->algid == id->b[GRPIDSZ])
+           goto found;
+
+    slilog(log,M_ERR,"private key file %s not loaded (unknown algid)",
+          path);
+    goto out;
+
+ found:
+    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 */
+
+    if (sigpriv->sethash) {
+       if (!defhash) {
+           slilog(log,M_ERR,
  "private key %s requires `hash' config key for privcache to load",
-                          st->path.buffer);
-                   sigpriv->dispose(sigpriv->st);
-                   sigpriv=0;
-                   goto out;
-               }
-               sigpriv->sethash(sigpriv->st,st->defhash);
-           }
+                  path);
+           sigpriv->dispose(sigpriv->st);
+           sigpriv=0;
            goto out;
        }
+       sigpriv->sethash(sigpriv->st,defhash);
     }
 
-    slilog(log,M_ERR,"private key file %s not loaded (not recognised?)\n",
-          st->path.buffer);
-
   out:
     if (f) fclose(f);
     return ok ? sigpriv : 0;
@@ -171,10 +180,7 @@ static list_t *privcache_apply(closure_t *self, struct cloc loc,
     buffer_new(&st->databuf,buflen+1);
 
     const char *path=dict_read_string(dict,"privkeys",True,"privcache",loc);
-    int l=strlen(path);
-    NEW_ARY(st->path.buffer,l+KEYIDSZ*2+1);
-    strcpy(st->path.buffer,path);
-    st->path.write_here=st->path.buffer+l;
+    pathprefix_template_init(&st->path,path,KEYIDSZ*2);
 
     st->defhash=find_cl_if(dict,"hash",CL_HASH,False,"site",loc);