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