3 * $Id: key.h,v 1.2 1999/12/10 23:29:48 mdw Exp $
5 * Simple key management
7 * (c) 1999 Mark Wooding
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 --------------------------------------------------*
33 * Revision 1.2 1999/12/10 23:29:48 mdw
34 * Change header file guard names.
36 * Revision 1.1 1999/09/03 08:41:12 mdw
41 #ifndef CATACOMB_KEY_H
42 #define CATACOMB_KEY_H
48 /*----- Header files ------------------------------------------------------*/
53 #include <mLib/bits.h>
54 #include <mLib/hash.h>
57 /*----- Data structures ---------------------------------------------------*/
59 /* --- Key attributes --- *
61 * Each attribute is stored as a symbol in a symbol table. The value is
62 * the plain (not url-encoded) text to be written to the the file. If the
63 * value is binary data, then by this point it's base-64 encoded.
66 typedef struct key_attr
{
67 sym_base _b
; /* Symbol table data */
68 char *p
; /* Pointer to attribute value */
71 /* --- Main key structure --- *
73 * Each key is stored in two symbol tables, one indexed by keyid, and the
74 * other indexed by type. Because many keys can have the same type, the type
75 * table contains a list of keys, sorted in descending order of expiry.
79 hash_base _b
; /* Symbol table data */
80 struct key
*next
; /* Next key of the same type */
81 uint32 id
; /* Key id used to name it */
82 char *type
; /* Textual key type */
83 void *k
; /* Actual key data */
84 size_t ksz
; /* Size of the key data */
85 time_t exp
, del
; /* Expiry times for keys */
86 sym_table a
; /* Hashtable of key attributes */
87 char *c
; /* Any additional comments */
90 /* --- The keys-by-type entries --- */
92 typedef struct key_type
{
93 sym_base _b
; /* Symbol table data */
94 key
*k
; /* Pointer to first key in list */
97 /* --- A key file --- */
99 typedef struct key_file
{
100 FILE *fp
; /* File pointer open on file */
101 int fd
; /* File descriptor open on file */
102 char *name
; /* Filename used to create it */
103 unsigned f
; /* Various useful flags */
104 hash_table byid
; /* Table of keys by keyid */
105 sym_table bytype
; /* Table of keys by type */
106 size_t idload
; /* Loading on id table */
109 /* --- Key file flags --- */
112 KF_WRITE
= 1, /* File opened for writing */
113 KF_MODIFIED
= 2 /* File has been modified */
116 /* --- Iterating over keys --- *
118 * Both of these are simple symbol table iterators, but they're made distinct
119 * types for the dubious benefits that type safety brings.
122 typedef struct { hash_iter i
; time_t t
; } key_iter
;
123 typedef struct { sym_iter i
; } key_attriter
;
125 /* --- File opening options --- */
132 /* --- Various other magic numbers --- */
134 #define KEXP_UNUSED ((time_t)0) /* Key has never been used */
135 #define KEXP_FOREVER ((time_t)-1) /* Never expire this key */
136 #define KEXP_EXPIRE ((time_t)-2) /* Expire this key when unused */
138 /* --- Write attempt codes --- */
141 KWRITE_OK
, /* Everything went fine */
142 KWRITE_FAIL
= -1, /* Close attempt failed */
143 KWRITE_BROKEN
= -2 /* Key ring needs manual fixing */
146 /* --- Macros for testing expiry --- */
148 #define KEY_EXPIRED(now, exp) \
149 ((exp) == KEXP_EXPIRE || ((exp) != KEXP_FOREVER && (exp) < (now)))
151 #define KEY_DELETED(now, del) ((del) == KEXP_FOREVER || (del) < (now))
153 /*----- Functions provided ------------------------------------------------*/
155 /* --- @key_chktype@ --- *
157 * Arguments: @const char *type@ = pointer to a type string
159 * Returns: Zero if OK, -1 on error.
161 * Use: Checks whether a type string is OK.
164 extern int key_chktype(const char */
*type*/
);
166 /* --- @key_chkcomment@ --- *
168 * Arguments: @const char *comment@ = pointer to a comment string
170 * Returns: Zero if OK, -1 on error.
172 * Use: Checks whether a comment string is OK.
175 extern int key_chkcomment(const char */
*c*/
);
177 /* --- @key_mkiter@ --- *
179 * Arguments: @key_iter *i@ = pointer to iterator object
180 * @key_file *f@ = pointer to file structure
184 * Use: Initializes a key iterator. The keys are returned by
188 extern void key_mkiter(key_iter */
*i*/
, key_file */
*f*/
);
190 /* --- @key_next@ --- *
192 * Arguments: @key_iter *i@ = pointer to iterator object
194 * Returns: Pointer to next key, or null.
196 * Use: Returns the next key in some arbitrary sequence.
199 extern key
*key_next(key_iter */
*i*/
);
201 /* --- @key_mkattriter@ --- *
203 * Arguments: @key_attriter *i@ = pointer to attribute iterator
204 * @key_file *f@ = pointer to key file
205 * @key *k@ = pointer to key
209 * Use: Initializes an attribute iterator. The attributes are
210 * returned by @key_nextattr@.
213 extern void key_mkattriter(key_attriter */
*i*/
, key_file */
*f*/
, key */
*k*/
);
215 /* --- @key_nextattr@ --- *
217 * Arguments: @key_attriter *i@ = pointer to attribute iterator
218 * @const char **n, **v@ = pointers to name and value
220 * Returns: Zero if no attribute available, or nonzero if returned OK.
222 * Use: Returns the next attribute.
225 extern int key_nextattr(key_attriter */
*i*/
,
226 const char **/
*n*/
, const char **/
*v*/
);
228 /* --- @key_bytype@ --- *
230 * Arguments: @key_file *f@ = key file we want a key from
231 * @const char *type@ = type string for desired key
233 * Returns: Pointer to the best key to use, or null.
235 * Use: Looks up a key by its type. Returns the key with the latest
236 * expiry time. This function will not return an expired key.
239 extern key
*key_bytype(key_file */
*f*/
, const char */
*type*/
);
241 /* --- @key_byid@ --- *
243 * Arguments: @key_file *f@ = key file to find a key from
244 * @uint32 id@ = id to look for
246 * Returns: Key with matching id.
248 * Use: Returns a key given its id. This function will return an
249 * expired key, but not a deleted one.
252 extern key
*key_byid(key_file */
*f*/
, uint32
/*id*/);
254 /* --- @key_getattr@ --- *
256 * Arguments: @key_file *f@ = pointer to file
257 * @key *k@ = pointer to key
258 * @const char *n@ = pointer to attribute name
260 * Returns: Pointer to attribute value, or null if not found.
262 * Use: Returns the value of a key attribute.
265 extern const char *key_getattr(key_file */
*f*/
, key */
*k*/
,
268 /* --- @key_putattr@ --- *
270 * Arguments: @key_file *f@ = pointer to file
271 * @key *k@ = pointer to key
272 * @const char *n@ = pointer to attribute name
273 * @const char *v@ = pointer to attribute value
277 * Use: Inserts an attribute on a key. If an attribute with the same
278 * name already exists, it is deleted.
281 extern void key_putattr(key_file */
*f*/
, key */
*k*/
,
282 const char */
*n*/
, const char */
*v*/
);
284 /* --- @key_setcomment@ --- *
286 * Arguments: @key_file *f@ = pointer to key file block
287 * @key *k@ = pointer to key block
288 * @const char *c@ = pointer to comment to set, or zero
292 * Use: Replaces the key's current comment with a new one.
295 extern void key_setcomment(key_file */
*f*/
, key */
*k*/
, const char */
*c*/
);
297 /* --- @key_merge@ --- *
299 * Arguments: @key_file *f@ = pointer to file structure
300 * @const char *file@ = name of file (for error messages)
301 * @FILE *fp@ = file handle to read from
305 * Use: Reads keys from a file, and inserts them into the file.
308 extern void key_merge(key_file */
*f*/
, const char */
*file*/
, FILE */
*fp*/
);
310 /* --- @key_extract@ --- *
312 * Arguments: @key_file *f@ = pointer to file structure
313 * @key *k@ = key to extract
314 * @FILE *fp@ = file to write on
316 * Returns: Zero if OK, EOF on error.
318 * Use: Extracts a key to an ouptut file.
321 extern int key_extract(key_file */
*f*/
, key */
*k*/
, FILE */
*fp*/
);
323 /* --- @key_write@ --- *
325 * Arguments: @key_file *f@ = pointer to key file block
327 * Returns: A @KWRITE_@ code indicating how well it worked.
329 * Use: Writes a key file's data back to the actual file. This code
330 * is extremely careful about error handling. It should usually
331 * be able to back out somewhere sensible, but it can tell when
332 * it's got itself into a real pickle and starts leaving well
335 * Callers, please make sure that you ring alarm bells when this
336 * function returns @KWRITE_BROKEN@.
339 extern int key_write(key_file */
*f*/
);
341 /* --- @key_open@ --- *
343 * Arguments: @key_file *f@ = pointer to file structure to initialize
344 * @const char *file@ = pointer to the file name
345 * @int how@ = opening options (@KOPEN_*@).
347 * Returns: Zero if it worked, nonzero otherwise.
349 * Use: Opens a key file, reads its contents, and stores them in a
350 * structure. The file is locked appropriately until closed
351 * using @key_close@. On an error, everything is cleared away
352 * tidily. If the file is opened with @KOPEN_WRITE@, it's
353 * created if necessary, with read and write permissions for its
357 extern int key_open(key_file */
*f*/
, const char */
*file*/
, int /*how*/);
359 /* --- @key_close@ --- *
361 * Arguments: @key_file *f@ = pointer to key file block
363 * Returns: A @KWRITE_@ code indicating how it went.
365 * Use: Frees all the key data, writes any changes. Make sure that
366 * all hell breaks loose if this returns @KWRITE_BROKEN@.
369 extern int key_close(key_file */
*f*/
);
373 * Arguments: @key_file *f@ = pointer to key file
374 * @const char *type@ = the type of this key
375 * @const void *k@ = pointer to key data
376 * @size_t ksz@ = size of key data
377 * @time_t exp@ = when the key expires
378 * @const char *c@ = textual comment to attach
380 * Returns: Key block containing new data, or null if it couldn't be
383 * Use: Attaches a new key to a key file. You must have a writable
384 * key file for this to work.
386 * The type is a key type string. This interface doesn't care
387 * about how type strings are formatted: it just treats them as
388 * opaque gobs of text. Clients are advised to choose some
389 * standard for representing key types, though.
391 * The key can be any old binary mess.
393 * The expiry time should either be a time in the future, or the
394 * magic value @KEXP_FOREVER@ which means `never expire this
395 * key'. Be careful with `forever' keys. If I were you, I'd
396 * use a more sophisticated key management system than this for
399 * The comment can be any old text not containing newlines or
400 * nulls. This interface doesn't impose any length restrictions
401 * on comment lengths.
404 extern key
*key_new(key_file */
*f*/
, const char */
*type*/
,
405 const void */
*k*/
, size_t /*ksz*/,
406 time_t /*exp*/, const char */
*c*/
);
408 /* --- @key_delete@ --- *
410 * Arguments: @key_file *f@ = pointer to file block
411 * @key *k@ = key to delete
415 * Use: Removes the given key from the list. The key file must be
416 * writable. (Due to the horridness of the data structures,
417 * deleted keys aren't actually removed, just marked so that
418 * they can't be looked up or iterated over. One upshot of
419 * this is that they don't get written back to the file when
423 extern void key_delete(key_file */
*f*/
, key */
*k*/
);
425 /* --- @key_expire@ --- *
427 * Arguments: @key_file *f@ = pointer to file block
428 * @key *k@ = pointer to key block
432 * Use: Immediately marks the key as expired. It may be removed
433 * immediately, if it is no longer required, and will be removed
434 * by a tidy operation when it is no longer required. The key
435 * file must be writable.
438 extern void key_expire(key_file */
*f*/
, key */
*k*/
);
440 /* --- @key_used@ --- *
442 * Arguments: @key_file *f@ = pointer to key file
443 * @key *k@ = pointer to key block
444 * @time_t t@ = when key can be removed
446 * Returns: Zero if OK, nonzero on failure.
448 * Use: Marks a key as being required until a given time. Even
449 * though the key may expire before then (and won't be returned
450 * by type after that time), it will still be available when
451 * requested explicitly by id. The key file must be writable.
453 * The only (current) reason for failure is attempting to use
454 * a key which can expire for something which can't.
457 extern int key_used(key_file */
*f*/
, key */
*k*/
, time_t /*t*/);
459 /*----- That's all, folks -------------------------------------------------*/