Major and incompatible overhaul of key_data representation. Fix leaks.
[u/mdw/catacomb] / key-io.c
index 7ed0c2c..9d9b6ee 100644 (file)
--- a/key-io.c
+++ b/key-io.c
@@ -221,7 +221,7 @@ int key_merge(key_file *f, const char *file, FILE *fp,
 
     /* --- Extract the key data into the block --- */
 
-    if (key_read(vf[1], &k->k, 0)) {
+    if ((k->k = key_read(vf[1], 0)) == 0) {
       if (rep)
        rep(file, line, "bad key data", arg);
       goto skip_1;
@@ -239,12 +239,12 @@ int key_merge(key_file *f, const char *file, FILE *fp,
       char *qq;
 
       if (!q) {
-       if (k->k.e != KENC_BINARY) {
+       if (k->k->e != KENC_BINARY) {
          if (rep)
            rep(file, line, "new-style key encoding but no keyid", arg);
          goto skip_2;
        }
-       k->id = crc32(0, k->k.u.k.k, k->k.u.k.sz);
+       k->id = crc32(0, k->k->u.k.k, k->k->u.k.sz);
        k->type = xstrdup(vf[0]);
        k->tag = 0;
       } else {
@@ -313,7 +313,7 @@ int key_merge(key_file *f, const char *file, FILE *fp,
       xfree(k->tag);
     xfree(k->type);
   skip_2:
-    key_destroy(&k->k);
+    key_drop(k->k);
   skip_1:
     DESTROY(k);
   skip_0:;
@@ -347,14 +347,14 @@ int key_extract(key_file *f, key *k, FILE *fp, const key_filter *kf)
 
   /* --- Skip the key if it's deleted or unselected--- */
 
-  if (KEY_EXPIRED(t, k->del) || !key_match(&k->k, kf))
+  if (KEY_EXPIRED(t, k->del) || !key_match(k->k, kf))
     return (0);
 
   /* --- Encode the key and write the easy stuff --- */
 
   key_fulltag(k, &d);
   DPUTC(&d, ' ');
-  key_write(&k->k, &d, kf);
+  key_write(k->k, &d, kf);
   DPUTC(&d, ' ');
   dstr_write(&d, fp);
   DRESET(&d);
@@ -469,7 +469,7 @@ void key_discard(key_file *f)
     key_attr *a;
     key *k = (key *)b;
 
-    key_destroy(&k->k);
+    if (k->k) key_drop(k->k);
     xfree(k->type);
     xfree(k->tag);
     if (k->c)
@@ -514,10 +514,9 @@ int key_close(key_file *f)
  *             @uint32 id@ = keyid to set
  *             @const char *type@ = the type of this key
  *             @time_t exp@ = when the key expires
- *             @int *err@ = where to store the error condition
+ *             @key *kk@ = where to put the key pointer
  *
- * Returns:    Key block containing new data, or null if it couldn't be
- *             done.
+ * Returns:    Error code (one of the @KERR@ constants).
  *
  * Use:                Attaches a new key to a key file.  You must have a writable
  *             key file for this to work.
@@ -532,11 +531,9 @@ int key_close(key_file *f)
  *             key'.  Be careful with `forever' keys.  If I were you, I'd
  *             use a more sophisticated key management system than this for
  *             them.
- *
- *             You have to set the actual key yourself.
  */
 
-key *key_new(key_file *f, uint32 id, const char *type, time_t exp, int *err)
+int key_new(key_file *f, uint32 id, const char *type, time_t exp, key **kk)
 {
   key *k = 0;
   time_t t = time(0);
@@ -554,21 +551,21 @@ key *key_new(key_file *f, uint32 id, const char *type, time_t exp, int *err)
     k = CREATE(key);
     k->id = id;
     k->tag = 0;
-    k->type = xstrdup(type);
     k->exp = k->del = exp;
     k->c = 0;
-    k->k.e = 0;
+    k->type = type; /* temporarily */
     sym_create(&k->a);
-    if ((e = insert(f, k)) == 0)
-      f->f |= KF_MODIFIED;
-    else {
-      xfree(k->type);
+    if ((e = insert(f, k)) != 0)
       DESTROY(k);
-      k = 0;
+    else {
+      k->k = key_newstring(KCAT_SHARE, "<unset>");
+      k->type = xstrdup(type);
+      *kk = k;
+      f->f |= KF_MODIFIED;
     }
   }
-  *err = e;
-  return (k);
+  
+  return (e);
 }
 
 /*----- That's all, folks -------------------------------------------------*/