Make flags be macros rather than enumerations, to ensure that they're
[u/mdw/catacomb] / key.h
1 /* -*-c-*-
2 *
3 * $Id: key.h,v 1.7 2000/12/06 20:33:27 mdw Exp $
4 *
5 * Simple key management
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: key.h,v $
33 * Revision 1.7 2000/12/06 20:33:27 mdw
34 * Make flags be macros rather than enumerations, to ensure that they're
35 * unsigned.
36 *
37 * Revision 1.6 2000/06/17 11:27:43 mdw
38 * Add key fetching interface.
39 *
40 * Revision 1.5 2000/02/12 18:55:40 mdw
41 * Make it all compile properly.
42 *
43 * Revision 1.4 2000/02/12 18:21:02 mdw
44 * Overhaul of key management (again).
45 *
46 * Revision 1.3 1999/12/22 15:47:48 mdw
47 * Major key-management revision.
48 *
49 * Revision 1.2 1999/12/10 23:29:48 mdw
50 * Change header file guard names.
51 *
52 * Revision 1.1 1999/09/03 08:41:12 mdw
53 * Initial import.
54 *
55 */
56
57 #ifndef CATACOMB_KEY_H
58 #define CATACOMB_KEY_H
59
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63
64 /*----- Header files ------------------------------------------------------*/
65
66 #include <stdio.h>
67 #include <time.h>
68
69 #include <mLib/bits.h>
70 #include <mLib/dstr.h>
71 #include <mLib/hash.h>
72 #include <mLib/sym.h>
73
74 #ifndef CATACOMB_KEY_DATA_H
75 # include "key-data.h"
76 #endif
77
78 #ifndef CATACOMB_MP_H
79 # include "mp.h"
80 #endif
81
82 /*----- Data structures ---------------------------------------------------*/
83
84 /* --- Key attributes --- *
85 *
86 * Each attribute is stored as a symbol in a symbol table. The value is
87 * the plain (not url-encoded) text to be written to the the file. If the
88 * value is binary data, then by this point it's base-64 encoded.
89 */
90
91 typedef struct key_attr {
92 sym_base _b; /* Symbol table data */
93 char *p; /* Pointer to attribute value */
94 } key_attr;
95
96 /* --- Main key structure --- *
97 *
98 * Each key is stored in two symbol tables, one indexed by keyid, and the
99 * other indexed by type. Because many keys can have the same type, the type
100 * table contains a list of keys, sorted in descending order of expiry.
101 */
102
103 typedef struct key {
104
105 /* --- Hashtable management --- */
106
107 hash_base _b; /* Symbol table data */
108 struct key *next; /* Next key of the same type */
109
110 /* --- Basic key attributes --- */
111
112 uint32 id; /* Key id used to name it */
113 char *tag; /* Textual tag name */
114 char *type; /* Textual key type */
115 time_t exp, del; /* Expiry times for keys */
116
117 /* --- The key data itself --- */
118
119 key_data k; /* The actual key data */
120
121 /* --- Other attributes and commentary --- */
122
123 sym_table a; /* Hashtable of key attributes */
124 char *c; /* Any additional comments */
125 } key;
126
127 /* --- The keys-by-type entries --- */
128
129 typedef struct key_ref {
130 sym_base _b; /* Symbol table data */
131 key *k; /* Pointer to first key in list */
132 } key_ref;
133
134 /* --- A key file --- */
135
136 typedef struct key_file {
137 FILE *fp; /* File pointer open on file */
138 char *name; /* Filename used to create it */
139 unsigned f; /* Various useful flags */
140 hash_table byid; /* Table of keys by keyid */
141 sym_table bytype; /* Table of keys by type */
142 sym_table bytag; /* Table of keys by tag */
143 size_t idload; /* Loading on id table */
144 } key_file;
145
146 /* --- Key file flags --- */
147
148 #define KF_WRITE 1u /* File opened for writing */
149 #define KF_MODIFIED 2u /* File has been modified */
150
151 /* --- Iterating over keys --- *
152 *
153 * Both of these are simple symbol table iterators, but they're made distinct
154 * types for the dubious benefits that type safety brings.
155 */
156
157 typedef struct { hash_iter i; time_t t; } key_iter;
158 typedef struct { sym_iter i; } key_attriter;
159
160 /* --- Key fetching --- */
161
162 typedef struct key_fetchdef {
163 char *name; /* Name of item */
164 size_t off; /* Offset into target structure */
165 unsigned e; /* Flags for the item */
166 const struct key_fetchdef *kf; /* Substructure pointer */
167 } key_fetchdef;
168
169 /* --- File opening options --- */
170
171 enum {
172 KOPEN_READ,
173 KOPEN_WRITE
174 };
175
176 /* --- Various other magic numbers --- */
177
178 #define KEXP_FOREVER ((time_t)-1) /* Never expire this key */
179 #define KEXP_EXPIRE ((time_t)-2) /* Expire this key when unused */
180
181 /* --- Key error codes --- */
182
183 enum {
184 KERR_OK = 0, /* No error */
185 KERR_BADTAG = -1, /* Malformed tag string */
186 KERR_BADTYPE = -2, /* Malformed type string */
187 KERR_BADCOMMENT = -3, /* Malformed comment string */
188 KERR_DUPID = -4, /* Duplicate keyid */
189 KERR_DUPTAG = -5, /* Duplicate key tag string */
190 KERR_READONLY = -6, /* Key file is read-only */
191 KERR_WILLEXPIRE = -7, /* Key will eventually expire */
192 KERR_EXPIRED = -8, /* Key has already expired */
193 KERR_BADFLAGS = -9, /* Error in flags string */
194 KERR_BADPASS = -10, /* Error decrypting locked key */
195 KERR_WRONGTYPE = -11, /* Key has incorrect type */
196 KERR_NOTFOUND = -12, /* Key couldn't be found */
197 KERR_MAX /* Largest possible error */
198 };
199
200 /* --- Write error codes --- */
201
202 enum {
203 KWRITE_OK, /* Everything went fine */
204 KWRITE_FAIL = -1, /* Close attempt failed */
205 KWRITE_BROKEN = -2 /* Key ring needs manual fixing */
206 };
207
208 /* --- Error reporting functions for @key_merge@ and @key_open@ --- */
209
210 typedef void key_reporter(const char */*file*/, int /*line*/,
211 const char */*err*/, void */*p*/);
212
213 /* --- Macros for testing expiry --- */
214
215 #define KEY_EXPIRED(now, exp) \
216 ((exp) == KEXP_EXPIRE || ((exp) != KEXP_FOREVER && (exp) < (now)))
217
218 /*----- Reading and writing keys and files --------------------------------*/
219
220 /* --- @key_merge@ --- *
221 *
222 * Arguments: @key_file *f@ = pointer to file structure
223 * @const char *file@ = name of file (for error messages)
224 * @FILE *fp@ = file handle to read from
225 * @key_reporter *rep@ = error reporting function
226 * @void *arg@ = argument for function
227 *
228 * Returns: Error code (one of the @KERR@ constants).
229 *
230 * Use: Reads keys from a file, and inserts them into the file.
231 */
232
233 extern int key_merge(key_file */*f*/, const char */*file*/, FILE */*fp*/,
234 key_reporter */*rep*/, void */*arg*/);
235
236 /* --- @key_extract@ --- *
237 *
238 * Arguments: @key_file *f@ = pointer to file structure
239 * @key *k@ = key to extract
240 * @FILE *fp@ = file to write on
241 * @const key_filter *kf@ = pointer to key selection block
242 *
243 * Returns: Zero if OK, EOF on error.
244 *
245 * Use: Extracts a key to an ouptut file.
246 */
247
248 extern int key_extract(key_file */*f*/, key */*k*/, FILE */*fp*/,
249 const key_filter */*kf*/);
250
251 /* --- @key_open@ --- *
252 *
253 * Arguments: @key_file *f@ = pointer to file structure to initialize
254 * @const char *file@ = pointer to the file name
255 * @int how@ = opening options (@KOPEN_*@).
256 * @key_reporter *rep@ = error reporting function
257 * @void *arg@ = argument for function
258 *
259 * Returns: Zero if it worked, nonzero otherwise.
260 *
261 * Use: Opens a key file, reads its contents, and stores them in a
262 * structure. The file is locked appropriately until closed
263 * using @key_close@. On an error, everything is cleared away
264 * tidily. If the file is opened with @KOPEN_WRITE@, it's
265 * created if necessary, with read and write permissions for its
266 * owner only.
267 */
268
269 extern int key_open(key_file */*f*/, const char */*file*/, int /*how*/,
270 key_reporter */*rep*/, void */*arg*/);
271
272 /* --- @key_close@ --- *
273 *
274 * Arguments: @key_file *f@ = pointer to key file block
275 *
276 * Returns: A @KWRITE_@ code indicating how it went.
277 *
278 * Use: Frees all the key data, writes any changes. Make sure that
279 * all hell breaks loose if this returns @KWRITE_BROKEN@.
280 */
281
282 extern int key_close(key_file */*f*/);
283
284 /* --- @key_save@ --- *
285 *
286 * Arguments: @key_file *f@ = pointer to key file block
287 *
288 * Returns: A @KWRITE_@ code indicating how well it worked.
289 *
290 * Use: Writes a key file's data back to the actual file. This code
291 * is extremely careful about error handling. It should usually
292 * be able to back out somewhere sensible, but it can tell when
293 * it's got itself into a real pickle and starts leaving well
294 * alone.
295 *
296 * Callers, please make sure that you ring alarm bells when this
297 * function returns @KWRITE_BROKEN@.
298 */
299
300 extern int key_save(key_file */*f*/);
301
302 /* --- @key_lockfile@ --- *
303 *
304 * Arguments: @key_file *f@ = pointer to file structure to initialize
305 * @const char *file@ = pointer to the file name
306 * @int how@ = opening options (@KOPEN_*@).
307 *
308 * Returns: Zero if it worked, nonzero otherwise.
309 *
310 * Use: Opens a keyfile and stores the information needed for
311 * continued access in the structure.
312 *
313 * If the file is opened with @KOPEN_WRITE@, it's created if
314 * necessary with read and write permissions for owner only, and
315 * locked for update while it's open.
316 *
317 * This is a system-dependent routine, and only really intended
318 * for the private use of @key_open@.
319 */
320
321 extern int key_lockfile(key_file */*f*/, const char */*file*/, int /*how*/);
322
323 /*----- Creating and manipulating keys ------------------------------------*/
324
325 /* --- @key_new@ ---
326 *
327 * Arguments: @key_file *f@ = pointer to key file
328 * @uint32 id@ = keyid to set
329 * @const char *type@ = the type of this key
330 * @time_t exp@ = when the key expires
331 * @int *err@ = where to store the error condition
332 *
333 * Returns: Key block containing new data, or null if it couldn't be
334 * done.
335 *
336 * Use: Attaches a new key to a key file. You must have a writable
337 * key file for this to work.
338 *
339 * The type is a key type string. This interface doesn't care
340 * about how type strings are formatted: it just treats them as
341 * opaque gobs of text. Clients are advised to choose some
342 * standard for representing key types, though.
343 *
344 * The expiry time should either be a time in the future, or the
345 * magic value @KEXP_FOREVER@ which means `never expire this
346 * key'. Be careful with `forever' keys. If I were you, I'd
347 * use a more sophisticated key management system than this for
348 * them.
349 *
350 * You have to set the actual key yourself.
351 */
352
353 extern key *key_new(key_file */*f*/, uint32 /*id*/, const char */*type*/,
354 time_t /*exp*/, int */*err*/);
355
356 /* --- @key_delete@ --- *
357 *
358 * Arguments: @key_file *f@ = pointer to file block
359 * @key *k@ = key to delete
360 *
361 * Returns: Error code (one of the @KERR@ constants).
362 *
363 * Use: Removes the given key from the list. The key file must be
364 * writable. (Due to the horridness of the data structures,
365 * deleted keys aren't actually removed, just marked so that
366 * they can't be looked up or iterated over. One upshot of
367 * this is that they don't get written back to the file when
368 * it's closed.)
369 */
370
371 extern int key_delete(key_file */*f*/, key */*k*/);
372
373 /* --- @key_expire@ --- *
374 *
375 * Arguments: @key_file *f@ = pointer to file block
376 * @key *k@ = pointer to key block
377 *
378 * Returns: Error code (one of the @KERR@ constants).
379 *
380 * Use: Immediately marks the key as expired. It may be removed
381 * immediately, if it is no longer required, and will be removed
382 * by a tidy operation when it is no longer required. The key
383 * file must be writable.
384 */
385
386 extern int key_expire(key_file */*f*/, key */*k*/);
387
388 /* --- @key_used@ --- *
389 *
390 * Arguments: @key_file *f@ = pointer to key file
391 * @key *k@ = pointer to key block
392 * @time_t t@ = when key can be removed
393 *
394 * Returns: Zero if OK, nonzero on failure.
395 *
396 * Use: Marks a key as being required until a given time. Even
397 * though the key may expire before then (and won't be returned
398 * by type after that time), it will still be available when
399 * requested explicitly by id. The key file must be writable.
400 *
401 * The only (current) reason for failure is attempting to use
402 * a key which can expire for something which can't.
403 */
404
405 extern int key_used(key_file */*f*/, key */*k*/, time_t /*t*/);
406
407 /*----- Setting and reading attributes ------------------------------------*/
408
409 /* --- @key_chkident@ --- *
410 *
411 * Arguments: @const char *p@ = pointer to a type string
412 *
413 * Returns: Zero if OK, -1 on error.
414 *
415 * Use: Checks whether an identification component string is OK.
416 */
417
418 extern int key_chkident(const char */*p*/);
419
420 /* --- @key_chkcomment@ --- *
421 *
422 * Arguments: @const char *p@ = pointer to a comment string
423 *
424 * Returns: Zero if OK, -1 on error.
425 *
426 * Use: Checks whether a comment string is OK.
427 */
428
429 extern int key_chkcomment(const char */*p*/);
430
431 /* --- @key_setcomment@ --- *
432 *
433 * Arguments: @key_file *f@ = pointer to key file block
434 * @key *k@ = pointer to key block
435 * @const char *c@ = pointer to comment to set, or zero
436 *
437 * Returns: Error code (one of the @KERR@ constants).
438 *
439 * Use: Replaces the key's current comment with a new one.
440 */
441
442 extern int key_setcomment(key_file */*f*/, key */*k*/, const char */*c*/);
443
444 /* --- @key_settag@ --- *
445 *
446 * Arguments: @key_file *f@ = pointer to key file block
447 * @key *k@ = pointer to key block
448 * @const char *tag@ = pointer to comment to set, or zero
449 *
450 * Returns: Error code (one of the @KERR@ constants).
451 *
452 * Use: Replaces the key's current tag with a new one.
453 */
454
455 extern int key_settag(key_file */*f*/, key */*k*/, const char */*tag*/);
456
457 /* --- @key_fulltag@ --- *
458 *
459 * Arguments: @key *k@ = pointer to key
460 * @dstr *d@ = pointer to destination string
461 *
462 * Returns: ---
463 *
464 * Use: Emits the key's full tag, which has the form
465 * `ID:TYPE[:TAG]'. This is used in the textual file format,
466 * and to identify passphrases for locked keys.
467 */
468
469 extern void key_fulltag(key */*k*/, dstr */*d*/);
470
471 /* --- @key_qtag@ --- *
472 *
473 * Arguments: @key_file *f@ = key file to find a key from
474 * @const char *tag@ = pointer to tag string
475 * @dstr *d@ = pointer to string for full tag name
476 * @key **k@ = where to store the key pointer
477 * @key_data **kd@ = where to store the key data pointer
478 *
479 * Returns: Zero if OK, nonzero if it failed.
480 *
481 * Use: Performs a full lookup on a qualified tag name. The tag is
482 * qualified by the names of subkeys, separated by dots. Hence,
483 * a qualified tag is ID|TAG[.TAG...]. The various result
484 * pointers can be null to indicate that the result isn't
485 * interesting.
486 */
487
488 extern int key_qtag(key_file */*f*/, const char */*tag*/,
489 dstr */*d*/, key **/*k*/, key_data **/*kd*/);
490
491 /* --- @key_getattr@ --- *
492 *
493 * Arguments: @key_file *f@ = pointer to file
494 * @key *k@ = pointer to key
495 * @const char *n@ = pointer to attribute name
496 *
497 * Returns: Pointer to attribute value, or null if not found.
498 *
499 * Use: Returns the value of a key attribute.
500 */
501
502 extern const char *key_getattr(key_file */*f*/, key */*k*/,
503 const char */*n*/);
504
505 /* --- @key_putattr@ --- *
506 *
507 * Arguments: @key_file *f@ = pointer to file
508 * @key *k@ = pointer to key
509 * @const char *n@ = pointer to attribute name
510 * @const char *v@ = pointer to attribute value or null
511 *
512 * Returns: Error code (one of the @KERR@ constants).
513 *
514 * Use: Inserts an attribute on a key. If an attribute with the same
515 * name already exists, it is deleted. Setting a null value
516 * removes the attribute.
517 */
518
519 extern int key_putattr(key_file */*f*/, key */*k*/,
520 const char */*n*/, const char */*v*/);
521
522 /* --- @key_mkattriter@ --- *
523 *
524 * Arguments: @key_attriter *i@ = pointer to attribute iterator
525 * @key *k@ = pointer to key
526 *
527 * Returns: ---
528 *
529 * Use: Initializes an attribute iterator. The attributes are
530 * returned by @key_nextattr@.
531 */
532
533 extern void key_mkattriter(key_attriter */*i*/, key */*k*/);
534
535 /* --- @key_nextattr@ --- *
536 *
537 * Arguments: @key_attriter *i@ = pointer to attribute iterator
538 * @const char **n, **v@ = pointers to name and value
539 *
540 * Returns: Zero if no attribute available, or nonzero if returned OK.
541 *
542 * Use: Returns the next attribute.
543 */
544
545 extern int key_nextattr(key_attriter */*i*/,
546 const char **/*n*/, const char **/*v*/);
547
548 /*----- Searching and iterating -------------------------------------------*/
549
550 /* --- @key_bytype@ --- *
551 *
552 * Arguments: @key_file *f@ = key file we want a key from
553 * @const char *type@ = type string for desired key
554 *
555 * Returns: Pointer to the best key to use, or null.
556 *
557 * Use: Looks up a key by its type. Returns the key with the latest
558 * expiry time. This function will not return an expired key.
559 */
560
561 extern key *key_bytype(key_file */*f*/, const char */*type*/);
562
563 /* --- @key_byid@ --- *
564 *
565 * Arguments: @key_file *f@ = key file to find a key from
566 * @uint32 id@ = id to look for
567 *
568 * Returns: Key with matching id.
569 *
570 * Use: Returns a key given its id. This function will return an
571 * expired key, but not a deleted one.
572 */
573
574 extern key *key_byid(key_file */*f*/, uint32 /*id*/);
575
576 /* --- @key_bytag@ --- *
577 *
578 * Arguments: @key_file *f@ = key file to find a key from
579 * @const char *tag@ = pointer to tag string
580 *
581 * Returns: Key with matching id or tag.
582 *
583 * Use: Returns a key given its tag or id. This function will return
584 * an expired key, but not a deleted one.
585 */
586
587 extern key *key_bytag(key_file */*f*/, const char */*tag*/);
588
589 /* --- @key_mkiter@ --- *
590 *
591 * Arguments: @key_iter *i@ = pointer to iterator object
592 * @key_file *f@ = pointer to file structure
593 *
594 * Returns: ---
595 *
596 * Use: Initializes a key iterator. The keys are returned by
597 * @key_next@.
598 */
599
600 extern void key_mkiter(key_iter */*i*/, key_file */*f*/);
601
602 /* --- @key_next@ --- *
603 *
604 * Arguments: @key_iter *i@ = pointer to iterator object
605 *
606 * Returns: Pointer to next key, or null.
607 *
608 * Use: Returns the next key in some arbitrary sequence.
609 */
610
611 extern key *key_next(key_iter */*i*/);
612
613 /*----- Fetching key data conveniently ------------------------------------*/
614
615 /* --- @key_fetchinit@ --- *
616 *
617 * Arguments: @const key_fetchdef *kf@ = pointer to base definition
618 * @key_packstruct *kps@ = pointer to destination packing def
619 * @void *p@ = pointer to destination block
620 *
621 * Returns: Pointer to packing definition.
622 *
623 * Use: Initializes a packing definition (@key_packdef@ structure).
624 * If @kps@ is null on entry, an appropriately sized block is
625 * allocated automatically. Otherwise it must be large enough.
626 */
627
628 extern key_packdef *key_fetchinit(const key_fetchdef */*kf*/,
629 key_packstruct */*kp*/, void */*p*/);
630
631 /* --- @key_fetch@ --- *
632 *
633 * Arguments: @key_packdef *kp@ = pointer to packing structure
634 * @key *k@ = key file containing desired key
635 *
636 * Returns: Error code, or zero.
637 *
638 * Use: Fetches an unpacked key from a packed one.
639 */
640
641 extern int key_fetch(key_packdef */*kp*/, key */*k*/);
642
643 /* --- @key_fetchbyname@ --- *
644 *
645 * Arguments: @key_packdef *kp@ = pointer to packing structure
646 * @key_file *kf@ = key file containing desired key
647 * @const char *tag@ = user's tag describing the key
648 *
649 * Returns: Error code, or zero.
650 *
651 * Use: Fetches a named key from a key file and unpacks it
652 * conveniently.
653 */
654
655 extern int key_fetchbyname(key_packdef */*kp*/,
656 key_file */*kf*/, const char */*tag*/);
657
658 /* --- @key_fetchdone@ --- *
659 *
660 * Arguments: @key_packdef *kp@ = pointer to packing structure
661 *
662 * Returns: ---
663 *
664 * Use: Frees a packing structure. If the structure was allocated by
665 * @key_fetchinit@ then it is freed.
666 */
667
668 extern void key_fetchdone(key_packdef */*kp*/);
669
670 /*----- Other functions ---------------------------------------------------*/
671
672 /* --- @key_moan@ --- *
673 *
674 * Arguments: @const char *file@ = name of the file
675 * @int line@ = line number in file
676 * @const char *msg@ = error message
677 * @void *p@ = argument pointer
678 *
679 * Returns: ---
680 *
681 * Use: Reports an error message about loading a key file.
682 */
683
684 extern void key_moan(const char */*file*/, int /*line*/,
685 const char */*msg*/, void */*p*/);
686
687 /* --- @key_strerror@ --- *
688 *
689 * Arguments: @int err@ = error code from @key_new@
690 *
691 * Returns: Pointer to error string.
692 *
693 * Use: Translates a @KERR@ error code into a human-readable string.
694 */
695
696 extern const char *key_strerror(int /*err*/);
697
698 /*----- That's all, folks -------------------------------------------------*/
699
700 #ifdef __cplusplus
701 }
702 #endif
703
704 #endif