3 * $Id: key.h,v 1.1 1999/09/03 08:41:12 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.1 1999/09/03 08:41:12 mdw
45 /*----- Header files ------------------------------------------------------*/
50 #include <mLib/bits.h>
51 #include <mLib/hash.h>
54 /*----- Data structures ---------------------------------------------------*/
56 /* --- Key attributes --- *
58 * Each attribute is stored as a symbol in a symbol table. The value is
59 * the plain (not url-encoded) text to be written to the the file. If the
60 * value is binary data, then by this point it's base-64 encoded.
63 typedef struct key_attr
{
64 sym_base _b
; /* Symbol table data */
65 char *p
; /* Pointer to attribute value */
68 /* --- Main key structure --- *
70 * Each key is stored in two symbol tables, one indexed by keyid, and the
71 * other indexed by type. Because many keys can have the same type, the type
72 * table contains a list of keys, sorted in descending order of expiry.
76 hash_base _b
; /* Symbol table data */
77 struct key
*next
; /* Next key of the same type */
78 uint32 id
; /* Key id used to name it */
79 char *type
; /* Textual key type */
80 void *k
; /* Actual key data */
81 size_t ksz
; /* Size of the key data */
82 time_t exp
, del
; /* Expiry times for keys */
83 sym_table a
; /* Hashtable of key attributes */
84 char *c
; /* Any additional comments */
87 /* --- The keys-by-type entries --- */
89 typedef struct key_type
{
90 sym_base _b
; /* Symbol table data */
91 key
*k
; /* Pointer to first key in list */
94 /* --- A key file --- */
96 typedef struct key_file
{
97 FILE *fp
; /* File pointer open on file */
98 int fd
; /* File descriptor open on file */
99 char *name
; /* Filename used to create it */
100 unsigned f
; /* Various useful flags */
101 hash_table byid
; /* Table of keys by keyid */
102 sym_table bytype
; /* Table of keys by type */
103 size_t idload
; /* Loading on id table */
106 /* --- Key file flags --- */
109 KF_WRITE
= 1, /* File opened for writing */
110 KF_MODIFIED
= 2 /* File has been modified */
113 /* --- Iterating over keys --- *
115 * Both of these are simple symbol table iterators, but they're made distinct
116 * types for the dubious benefits that type safety brings.
119 typedef struct { hash_iter i
; time_t t
; } key_iter
;
120 typedef struct { sym_iter i
; } key_attriter
;
122 /* --- File opening options --- */
129 /* --- Various other magic numbers --- */
131 #define KEXP_UNUSED ((time_t)0) /* Key has never been used */
132 #define KEXP_FOREVER ((time_t)-1) /* Never expire this key */
133 #define KEXP_EXPIRE ((time_t)-2) /* Expire this key when unused */
135 /* --- Write attempt codes --- */
138 KWRITE_OK
, /* Everything went fine */
139 KWRITE_FAIL
= -1, /* Close attempt failed */
140 KWRITE_BROKEN
= -2 /* Key ring needs manual fixing */
143 /* --- Macros for testing expiry --- */
145 #define KEY_EXPIRED(now, exp) \
146 ((exp) == KEXP_EXPIRE || ((exp) != KEXP_FOREVER && (exp) < (now)))
148 #define KEY_DELETED(now, del) ((del) == KEXP_FOREVER || (del) < (now))
150 /*----- Functions provided ------------------------------------------------*/
152 /* --- @key_chktype@ --- *
154 * Arguments: @const char *type@ = pointer to a type string
156 * Returns: Zero if OK, -1 on error.
158 * Use: Checks whether a type string is OK.
161 extern int key_chktype(const char */
*type*/
);
163 /* --- @key_chkcomment@ --- *
165 * Arguments: @const char *comment@ = pointer to a comment string
167 * Returns: Zero if OK, -1 on error.
169 * Use: Checks whether a comment string is OK.
172 extern int key_chkcomment(const char */
*c*/
);
174 /* --- @key_mkiter@ --- *
176 * Arguments: @key_iter *i@ = pointer to iterator object
177 * @key_file *f@ = pointer to file structure
181 * Use: Initializes a key iterator. The keys are returned by
185 extern void key_mkiter(key_iter */
*i*/
, key_file */
*f*/
);
187 /* --- @key_next@ --- *
189 * Arguments: @key_iter *i@ = pointer to iterator object
191 * Returns: Pointer to next key, or null.
193 * Use: Returns the next key in some arbitrary sequence.
196 extern key
*key_next(key_iter */
*i*/
);
198 /* --- @key_mkattriter@ --- *
200 * Arguments: @key_attriter *i@ = pointer to attribute iterator
201 * @key_file *f@ = pointer to key file
202 * @key *k@ = pointer to key
206 * Use: Initializes an attribute iterator. The attributes are
207 * returned by @key_nextattr@.
210 extern void key_mkattriter(key_attriter */
*i*/
, key_file */
*f*/
, key */
*k*/
);
212 /* --- @key_nextattr@ --- *
214 * Arguments: @key_attriter *i@ = pointer to attribute iterator
215 * @const char **n, **v@ = pointers to name and value
217 * Returns: Zero if no attribute available, or nonzero if returned OK.
219 * Use: Returns the next attribute.
222 extern int key_nextattr(key_attriter */
*i*/
,
223 const char **/
*n*/
, const char **/
*v*/
);
225 /* --- @key_bytype@ --- *
227 * Arguments: @key_file *f@ = key file we want a key from
228 * @const char *type@ = type string for desired key
230 * Returns: Pointer to the best key to use, or null.
232 * Use: Looks up a key by its type. Returns the key with the latest
233 * expiry time. This function will not return an expired key.
236 extern key
*key_bytype(key_file */
*f*/
, const char */
*type*/
);
238 /* --- @key_byid@ --- *
240 * Arguments: @key_file *f@ = key file to find a key from
241 * @uint32 id@ = id to look for
243 * Returns: Key with matching id.
245 * Use: Returns a key given its id. This function will return an
246 * expired key, but not a deleted one.
249 extern key
*key_byid(key_file */
*f*/
, uint32
/*id*/);
251 /* --- @key_getattr@ --- *
253 * Arguments: @key_file *f@ = pointer to file
254 * @key *k@ = pointer to key
255 * @const char *n@ = pointer to attribute name
257 * Returns: Pointer to attribute value, or null if not found.
259 * Use: Returns the value of a key attribute.
262 extern const char *key_getattr(key_file */
*f*/
, key */
*k*/
,
265 /* --- @key_putattr@ --- *
267 * Arguments: @key_file *f@ = pointer to file
268 * @key *k@ = pointer to key
269 * @const char *n@ = pointer to attribute name
270 * @const char *v@ = pointer to attribute value
274 * Use: Inserts an attribute on a key. If an attribute with the same
275 * name already exists, it is deleted.
278 extern void key_putattr(key_file */
*f*/
, key */
*k*/
,
279 const char */
*n*/
, const char */
*v*/
);
281 /* --- @key_setcomment@ --- *
283 * Arguments: @key_file *f@ = pointer to key file block
284 * @key *k@ = pointer to key block
285 * @const char *c@ = pointer to comment to set, or zero
289 * Use: Replaces the key's current comment with a new one.
292 extern void key_setcomment(key_file */
*f*/
, key */
*k*/
, const char */
*c*/
);
294 /* --- @key_merge@ --- *
296 * Arguments: @key_file *f@ = pointer to file structure
297 * @const char *file@ = name of file (for error messages)
298 * @FILE *fp@ = file handle to read from
302 * Use: Reads keys from a file, and inserts them into the file.
305 extern void key_merge(key_file */
*f*/
, const char */
*file*/
, FILE */
*fp*/
);
307 /* --- @key_extract@ --- *
309 * Arguments: @key_file *f@ = pointer to file structure
310 * @key *k@ = key to extract
311 * @FILE *fp@ = file to write on
313 * Returns: Zero if OK, EOF on error.
315 * Use: Extracts a key to an ouptut file.
318 extern int key_extract(key_file */
*f*/
, key */
*k*/
, FILE */
*fp*/
);
320 /* --- @key_write@ --- *
322 * Arguments: @key_file *f@ = pointer to key file block
324 * Returns: A @KWRITE_@ code indicating how well it worked.
326 * Use: Writes a key file's data back to the actual file. This code
327 * is extremely careful about error handling. It should usually
328 * be able to back out somewhere sensible, but it can tell when
329 * it's got itself into a real pickle and starts leaving well
332 * Callers, please make sure that you ring alarm bells when this
333 * function returns @KWRITE_BROKEN@.
336 extern int key_write(key_file */
*f*/
);
338 /* --- @key_open@ --- *
340 * Arguments: @key_file *f@ = pointer to file structure to initialize
341 * @const char *file@ = pointer to the file name
342 * @int how@ = opening options (@KOPEN_*@).
344 * Returns: Zero if it worked, nonzero otherwise.
346 * Use: Opens a key file, reads its contents, and stores them in a
347 * structure. The file is locked appropriately until closed
348 * using @key_close@. On an error, everything is cleared away
349 * tidily. If the file is opened with @KOPEN_WRITE@, it's
350 * created if necessary, with read and write permissions for its
354 extern int key_open(key_file */
*f*/
, const char */
*file*/
, int /*how*/);
356 /* --- @key_close@ --- *
358 * Arguments: @key_file *f@ = pointer to key file block
360 * Returns: A @KWRITE_@ code indicating how it went.
362 * Use: Frees all the key data, writes any changes. Make sure that
363 * all hell breaks loose if this returns @KWRITE_BROKEN@.
366 extern int key_close(key_file */
*f*/
);
370 * Arguments: @key_file *f@ = pointer to key file
371 * @const char *type@ = the type of this key
372 * @const void *k@ = pointer to key data
373 * @size_t ksz@ = size of key data
374 * @time_t exp@ = when the key expires
375 * @const char *c@ = textual comment to attach
377 * Returns: Key block containing new data, or null if it couldn't be
380 * Use: Attaches a new key to a key file. You must have a writable
381 * key file for this to work.
383 * The type is a key type string. This interface doesn't care
384 * about how type strings are formatted: it just treats them as
385 * opaque gobs of text. Clients are advised to choose some
386 * standard for representing key types, though.
388 * The key can be any old binary mess.
390 * The expiry time should either be a time in the future, or the
391 * magic value @KEXP_FOREVER@ which means `never expire this
392 * key'. Be careful with `forever' keys. If I were you, I'd
393 * use a more sophisticated key management system than this for
396 * The comment can be any old text not containing newlines or
397 * nulls. This interface doesn't impose any length restrictions
398 * on comment lengths.
401 extern key
*key_new(key_file */
*f*/
, const char */
*type*/
,
402 const void */
*k*/
, size_t /*ksz*/,
403 time_t /*exp*/, const char */
*c*/
);
405 /* --- @key_delete@ --- *
407 * Arguments: @key_file *f@ = pointer to file block
408 * @key *k@ = key to delete
412 * Use: Removes the given key from the list. The key file must be
413 * writable. (Due to the horridness of the data structures,
414 * deleted keys aren't actually removed, just marked so that
415 * they can't be looked up or iterated over. One upshot of
416 * this is that they don't get written back to the file when
420 extern void key_delete(key_file */
*f*/
, key */
*k*/
);
422 /* --- @key_expire@ --- *
424 * Arguments: @key_file *f@ = pointer to file block
425 * @key *k@ = pointer to key block
429 * Use: Immediately marks the key as expired. It may be removed
430 * immediately, if it is no longer required, and will be removed
431 * by a tidy operation when it is no longer required. The key
432 * file must be writable.
435 extern void key_expire(key_file */
*f*/
, key */
*k*/
);
437 /* --- @key_used@ --- *
439 * Arguments: @key_file *f@ = pointer to key file
440 * @key *k@ = pointer to key block
441 * @time_t t@ = when key can be removed
443 * Returns: Zero if OK, nonzero on failure.
445 * Use: Marks a key as being required until a given time. Even
446 * though the key may expire before then (and won't be returned
447 * by type after that time), it will still be available when
448 * requested explicitly by id. The key file must be writable.
450 * The only (current) reason for failure is attempting to use
451 * a key which can expire for something which can't.
454 extern int key_used(key_file */
*f*/
, key */
*k*/
, time_t /*t*/);
456 /*----- That's all, folks -------------------------------------------------*/