3 * $Id: key-misc.c,v 1.1 1999/12/22 15:47:48 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
32 * $Log: key-misc.c,v $
33 * Revision 1.1 1999/12/22 15:47:48 mdw
34 * Major key-management revision.
38 /*----- Header files ------------------------------------------------------*/
45 #include <mLib/alloc.h>
46 #include <mLib/bits.h>
47 #include <mLib/hash.h>
53 /*----- Useful macros -----------------------------------------------------*/
55 #define KEY_WRITE(f) do { \
56 if (!(f)->f & KF_WRITE) \
57 return (KERR_READONLY); \
60 #define KEY_MODIFY(f) do { (f)->f |= KF_MODIFIED; } while (0)
62 #define KEY_LOAD(n) ((n) * 2)
64 /*----- Error reporting ---------------------------------------------------*/
66 /* --- @key_strerror@ --- *
68 * Arguments: @int err@ = error code from @key_new@
70 * Returns: Pointer to error string.
72 * Use: Translates a @KERR@ error code into a human-readable
76 const char *key_strerror(int err
)
83 "Keyid already exists",
84 "Key tag already exists",
85 "Key file is read-only",
86 "Key will eventually expire",
87 "Bad key flags string",
97 /*----- Iteration and iterators -------------------------------------------*/
99 /* --- @key_mkiter@ --- *
101 * Arguments: @key_iter *i@ = pointer to iterator object
102 * @key_file *f@ = pointer to file structure
106 * Use: Initializes a key iterator. The keys are returned by
110 void key_mkiter(key_iter
*i
, key_file
*f
)
112 HASH_MKITER(&i
->i
, &f
->byid
);
116 /* --- @key_next@ --- *
118 * Arguments: @key_iter *i@ = pointer to iterator object
120 * Returns: Pointer to next key, or null.
122 * Use: Returns the next key in some arbitrary sequence.
125 key
*key_next(key_iter
*i
)
132 } while (k
&& KEY_EXPIRED(i
->t
, k
->exp
) && KEY_EXPIRED(i
->t
, k
->del
));
136 /*----- Lookup ------------------------------------------------------------*/
138 /* --- @key_bytype@ --- *
140 * Arguments: @key_file *f@ = key file we want a key from
141 * @const char *type@ = type string for desired key
143 * Returns: Pointer to the best key to use, or null.
145 * Use: Looks up a key by its type. Returns the key with the latest
146 * expiry time. This function will not return an expired key.
149 key
*key_bytype(key_file
*f
, const char *type
)
151 time_t now
= time(0);
155 if ((kr
= sym_find(&f
->bytype
, type
, -1, 0, 0)) == 0)
157 for (k
= kr
->k
; k
&& KEY_EXPIRED(now
, k
->exp
); k
= k
->next
)
162 /* --- @key_byid@ --- *
164 * Arguments: @key_file *f@ = key file to find a key from
165 * @uint32 id@ = id to look for
167 * Returns: Key with matching id.
169 * Use: Returns a key given its id. This function will return an
170 * expired key, but not a deleted one.
173 key
*key_byid(key_file
*f
, uint32 id
)
178 bin
= HASH_BIN(&f
->byid
, id
);
179 for (b
= *bin
; b
; b
= b
->next
) {
182 if (KEY_EXPIRED(t
, k
->exp
) && KEY_EXPIRED(t
, k
->del
))
190 /* --- @key_bytag@ --- *
192 * Arguments: @key_file *f@ = key file to find a key from
193 * @const char *tag@ = pointer to tag string
195 * Returns: Key with matching id or tag.
197 * Use: Returns a key given its tag or id. This function will return
198 * an expired key, but not a deleted one.
201 key
*key_bytag(key_file
*f
, const char *tag
)
206 key_ref
*kr
= sym_find(&f
->bytag
, tag
, -1, 0, 0);
208 if (kr
&& !(KEY_EXPIRED(t
, kr
->k
->exp
) && KEY_EXPIRED(t
, kr
->k
->exp
)))
210 id
= strtoul(tag
, &p
, 16);
212 return (key_byid(f
, id
));
213 return (key_bytype(f
, tag
));
216 /* --- @key_qtag@ --- *
218 * Arguments: @key_file *f@ = key file to find a key from
219 * @const char *tag@ = pointer to tag string
220 * @dstr *d@ = pointer to string for full tag name
221 * @key **k@ = where to store the key pointer
222 * @key_data **kd@ = where to store the key data pointer
224 * Returns: Zero if OK, nonzero if it failed.
226 * Use: Performs a full lookup on a qualified tag name. The tag is
227 * qualified by the names of subkeys, separated by dots. Hence,
228 * a qualified tag is ID|TAG[.TAG...]. The various result
229 * pointers can be null to indicate that the result isn't
233 int key_qtag(key_file
*f
, const char *tag
, dstr
*d
, key
**k
, key_data
**kd
)
240 /* --- Find the end of the base tag --- */
242 if ((q
= strchr(tag
, '.')) == 0)
245 DPUTM(&dd
, tag
, q
- tag
);
250 /* --- Look up the key tag --- */
252 if ((kk
= key_bytag(f
, dd
.buf
)) == 0) {
257 /* --- Set the various initial bits of result up --- */
265 /* --- Now dig through the rest of the tag --- */
269 /* --- Stick on the next bit of the fullqtag --- */
272 while (*q
&& *q
!= '.') {
282 /* --- Look up the subkey --- */
284 if (kkd
->e
!= KENC_STRUCT
) {
288 if ((kkd
= key_structfind(kkd
, dd
.buf
)) == 0)
292 /* --- Return the results --- */
302 /*----- Miscellaneous functions -------------------------------------------*/
304 /* --- @key_delete@ --- *
306 * Arguments: @key_file *f@ = pointer to file block
307 * @key *k@ = key to delete
309 * Returns: Error code (one of the @KERR@ constants).
311 * Use: Removes the given key from the list. The key file must be
312 * writable. (Due to the horridness of the data structures,
313 * deleted keys aren't actually removed, just marked so that
314 * they can't be looked up or iterated over. One upshot of
315 * this is that they don't get written back to the file when
319 int key_delete(key_file
*f
, key
*k
)
322 k
->exp
= KEXP_EXPIRE
;
323 k
->del
= KEXP_EXPIRE
;
328 /* --- @key_expire@ --- *
330 * Arguments: @key_file *f@ = pointer to file block
331 * @key *k@ = pointer to key block
333 * Returns: Error code (one of the @KERR@ constants).
335 * Use: Immediately marks the key as expired. It may be removed
336 * immediately, if it is no longer required, and will be removed
337 * by a tidy operation when it is no longer required. The key
338 * file must be writable.
341 int key_expire(key_file
*f
, key
*k
)
344 k
->exp
= KEXP_EXPIRE
;
345 if (k
->del
== KEXP_FOREVER
)
346 k
->del
= KEXP_EXPIRE
;
351 /* --- @key_used@ --- *
353 * Arguments: @key_file *f@ = pointer to key file
354 * @key *k@ = pointer to key block
355 * @time_t t@ = when key can be removed
357 * Returns: Zero if OK, nonzero on failure.
359 * Use: Marks a key as being required until a given time. Even
360 * though the key may expire before then (and won't be returned
361 * by type after that time), it will still be available when
362 * requested explicitly by id. The key file must be writable.
364 * The only (current) reason for failure is attempting to use
365 * a key which can expire for something which can't.
368 int key_used(key_file
*f
, key
*k
, time_t t
)
371 if (t
== KEXP_FOREVER
) {
372 if (k
->exp
!= KEXP_FOREVER
)
373 return (KERR_WILLEXPIRE
);
374 } else if (k
->del
>= t
)
382 /*----- That's all, folks -------------------------------------------------*/