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