key/key-misc.c (key_bytag): Accept `tag:', `id:' and `type:' prefixes.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 26 Oct 2019 14:40:19 +0000 (15:40 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 9 May 2020 19:57:33 +0000 (20:57 +0100)
Further to the fix 079836cc4d21b355c8b58a4624ef85df0ac6c21a, allow the
user to attach a disambiguating prefix to the label.  Using the colon is
safe here, since it's obviously not acceptable in a hex id, and
`key_chkident' rejects colons in tag and type strings.

key/key-misc.c

index b10879a..0afad76 100644 (file)
@@ -160,14 +160,29 @@ key *key_bytag(key_file *f, const char *tag)
   time_t t = time(0);
   char *p;
   uint32 id;
   time_t t = time(0);
   char *p;
   uint32 id;
-  key_ref *kr = sym_find(&f->bytag, tag, -1, 0, 0);
+  enum { GUESS = -1, TAG, ID, TYPE }; int lookup;
+  key_ref *kr;
   key *k;
 
   key *k;
 
-  if (kr && !(KEY_EXPIRED(t, kr->k->exp) && KEY_EXPIRED(t, kr->k->del)))
+  if (STRNCMP(tag, ==, "tag:", 4)) { lookup = TAG; tag += 4; }
+  else if (STRNCMP(tag, ==, "id:", 3)) { lookup = ID; tag += 3; }
+  else if (STRNCMP(tag, ==, "type:", 5)) { lookup = TYPE; tag += 5; }
+  else lookup = GUESS;
+
+  if ((lookup == GUESS || lookup == TAG) &&
+      (kr = sym_find(&f->bytag, tag, -1, 0, 0)) != 0 &&
+      !(KEY_EXPIRED(t, kr->k->exp) && KEY_EXPIRED(t, kr->k->del)))
     return (kr->k);
     return (kr->k);
-  id = strtoul(tag, &p, 16);
-  if (!*p && (k = key_byid(f, id)) != 0) return (k);
-  return (key_bytype(f, tag));
+
+  if (lookup == GUESS || lookup == ID) {
+    id = strtoul(tag, &p, 16);
+    if (!*p && (k = key_byid(f, id)) != 0) return (k);
+  }
+
+  if ((lookup == GUESS || lookup == TYPE) && (k = key_bytype(f, tag)) != 0)
+    return (k);
+
+  return (0);
 }
 
 /* --- @key_qtag@ --- *
 }
 
 /* --- @key_qtag@ --- *