5 * Simple key management
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/bits.h>
38 #include <mLib/hash.h>
44 /*----- Useful macros -----------------------------------------------------*/
46 #define KEY_WRITE(f) do { \
47 if (!(f)->f & KF_WRITE) \
48 return (KERR_READONLY); \
51 #define KEY_MODIFY(f) do { (f)->f |= KF_MODIFIED; } while (0)
53 #define KEY_LOAD(n) ((n) * 2)
55 /*----- Iteration and iterators -------------------------------------------*/
57 /* --- @key_mkiter@ --- *
59 * Arguments: @key_iter *i@ = pointer to iterator object
60 * @key_file *f@ = pointer to file structure
64 * Use: Initializes a key iterator. The keys are returned by
68 void key_mkiter(key_iter *i, key_file *f)
70 HASH_MKITER(&i->i, &f->byid);
74 /* --- @key_next@ --- *
76 * Arguments: @key_iter *i@ = pointer to iterator object
78 * Returns: Pointer to next key, or null.
80 * Use: Returns the next key in some arbitrary sequence.
83 key *key_next(key_iter *i)
90 } while (k && KEY_EXPIRED(i->t, k->exp) && KEY_EXPIRED(i->t, k->del));
94 /*----- Lookup ------------------------------------------------------------*/
96 /* --- @key_bytype@ --- *
98 * Arguments: @key_file *f@ = key file we want a key from
99 * @const char *type@ = type string for desired key
101 * Returns: Pointer to the best key to use, or null.
103 * Use: Looks up a key by its type. Returns the key with the latest
104 * expiry time. This function will not return an expired key.
107 key *key_bytype(key_file *f, const char *type)
109 time_t now = time(0);
113 if ((kr = sym_find(&f->bytype, type, -1, 0, 0)) == 0)
115 for (k = kr->k; k && KEY_EXPIRED(now, k->exp); k = k->next)
120 /* --- @key_byid@ --- *
122 * Arguments: @key_file *f@ = key file to find a key from
123 * @uint32 id@ = id to look for
125 * Returns: Key with matching id.
127 * Use: Returns a key given its id. This function will return an
128 * expired key, but not a deleted one.
131 key *key_byid(key_file *f, uint32 id)
136 bin = HASH_BIN(&f->byid, id);
137 for (b = *bin; b; b = b->next) {
140 if (KEY_EXPIRED(t, k->exp) && KEY_EXPIRED(t, k->del))
148 /* --- @key_bytag@ --- *
150 * Arguments: @key_file *f@ = key file to find a key from
151 * @const char *tag@ = pointer to tag string
153 * Returns: Key with matching id or tag.
155 * Use: Returns a key given its tag or id. This function will return
156 * an expired key, but not a deleted one.
159 key *key_bytag(key_file *f, const char *tag)
164 key_ref *kr = sym_find(&f->bytag, tag, -1, 0, 0);
166 if (kr && !(KEY_EXPIRED(t, kr->k->exp) && KEY_EXPIRED(t, kr->k->del)))
168 id = strtoul(tag, &p, 16);
170 return (key_byid(f, id));
171 return (key_bytype(f, tag));
174 /* --- @key_qtag@ --- *
176 * Arguments: @key_file *f@ = key file to find a key from
177 * @const char *tag@ = pointer to tag string
178 * @dstr *d@ = pointer to string for full tag name
179 * @key **k@ = where to store the key pointer
180 * @key_data ***kd@ = where to store the key data pointer
182 * Returns: Zero if OK, nonzero if it failed.
184 * Use: Performs a full lookup on a qualified tag name. The tag is
185 * qualified by the names of subkeys, separated by dots. Hence,
186 * a qualified tag is ID|TAG[.TAG...]. The various result
187 * pointers can be null to indicate that the result isn't
191 int key_qtag(key_file *f, const char *tag, dstr *d, key **k, key_data ***kd)
198 /* --- Find the end of the base tag --- */
200 if ((q = strchr(tag, '.')) == 0)
203 DPUTM(&dd, tag, q - tag);
208 /* --- Look up the key tag --- */
210 if ((kk = key_bytag(f, dd.buf)) == 0) {
215 /* --- Set the various initial bits of result up --- */
223 /* --- Now dig through the rest of the tag --- */
229 /* --- Stick on the next bit of the fullqtag --- */
232 while (*q && *q != '.') {
242 /* --- Look up the subkey --- */
244 if ((*kkd)->e != KENC_STRUCT) {
248 if ((ks = sym_find(&(*kkd)->u.s, dd.buf, -1, 0, 0)) == 0) {
256 /* --- Return the results --- */
266 /*----- Miscellaneous functions -------------------------------------------*/
268 /* --- @key_delete@ --- *
270 * Arguments: @key_file *f@ = pointer to file block
271 * @key *k@ = key to delete
273 * Returns: Error code (one of the @KERR@ constants).
275 * Use: Removes the given key from the list. The key file must be
276 * writable. (Due to the horridness of the data structures,
277 * deleted keys aren't actually removed, just marked so that
278 * they can't be looked up or iterated over. One upshot of
279 * this is that they don't get written back to the file when
283 int key_delete(key_file *f, key *k)
286 k->exp = KEXP_EXPIRE;
287 k->del = KEXP_EXPIRE;
292 /* --- @key_expired@ --- *
294 * Arguments: @key *k@ = pointer to key block
296 * Returns: Zero if the key is OK, nonzero if it's expired.
299 int key_expired(key *k)
301 time_t now = time(0);
302 return (KEY_EXPIRED(now, k->exp) || KEY_EXPIRED(now, k->del));
305 /* --- @key_expire@ --- *
307 * Arguments: @key_file *f@ = pointer to file block
308 * @key *k@ = pointer to key block
310 * Returns: Error code (one of the @KERR@ constants).
312 * Use: Immediately marks the key as expired. It may be removed
313 * immediately, if it is no longer required, and will be removed
314 * by a tidy operation when it is no longer required. The key
315 * file must be writable.
318 int key_expire(key_file *f, key *k)
321 k->exp = KEXP_EXPIRE;
322 if (k->del == KEXP_FOREVER)
323 k->del = KEXP_EXPIRE;
328 /* --- @key_used@ --- *
330 * Arguments: @key_file *f@ = pointer to key file
331 * @key *k@ = pointer to key block
332 * @time_t t@ = when key can be removed
334 * Returns: Zero if OK, nonzero on failure.
336 * Use: Marks a key as being required until a given time. Even
337 * though the key may expire before then (and won't be returned
338 * by type after that time), it will still be available when
339 * requested explicitly by id. The key file must be writable.
341 * The only (current) reason for failure is attempting to use
342 * a key which can expire for something which can't.
345 int key_used(key_file *f, key *k, time_t t)
348 if (t == KEXP_FOREVER) {
349 if (k->exp != KEXP_FOREVER)
350 return (KERR_WILLEXPIRE);
351 } else if (k->del >= t)
359 /* --- @key_fingerprint@ --- *
361 * Arguments: @key *k@ = the key to fingerprint
362 * @ghash *h@ = the hash to use
363 * @const key_filter *kf@ = filter to apply
365 * Returns: Nonzero if the key slightly matched the filter.
367 * Use: Updates the hash context with the key contents.
370 static int abyname(const void *a, const void *b) {
371 key_attr *const *x = a, *const *y = b;
372 return (strcmp(SYM_NAME(*x), SYM_NAME(*y)));
375 int key_fingerprint(key *k, ghash *h, const key_filter *kf)
383 if (!key_encode(k->k, &d, kf))
386 GH_HASHSTR(h, "catacomb-key-fingerprint:");
387 GH_HASHU32(h, k->id);
388 GH_HASHSTR8(h, k->type);
389 GH_HASH(h, d.buf, d.len);
390 for (n = 0, sym_mkiter(&ai, &k->a); (a = sym_next(&ai)) != 0; n++);
392 v = xmalloc(n * sizeof(*v));
393 for (i = 0, sym_mkiter(&ai, &k->a); (a = sym_next(&ai)) != 0; i++)
395 qsort(v, n, sizeof(*v), abyname);
396 for (i = 0; i < n; i++) {
397 GH_HASHSTR8(h, SYM_NAME(v[i]));
398 GH_HASHSTR16(h, v[i]->p);
407 /*----- That's all, folks -------------------------------------------------*/