3 * $Id: key-io.c,v 1.1 1999/12/22 15:47:48 mdw Exp $
5 * Adding new keys to a key file
7 * (c) 1999 Straylight/Edgeware
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/12/22 15:47:48 mdw
34 * Major key-management revision.
38 /*----- Header files ------------------------------------------------------*/
47 #include <mLib/alloc.h>
48 #include <mLib/bits.h>
49 #include <mLib/crc32.h>
50 #include <mLib/dstr.h>
51 #include <mLib/hash.h>
59 /*----- Tweakable macros --------------------------------------------------*/
61 #define KEY_LOAD(n) ((n) * 2)
63 /*----- Low-level functions -----------------------------------------------*/
67 * Arguments: @key_file *f@ = pointer to file structure
68 * @key *k@ = pointer to key block to insert
70 * Returns: Error code (one of the @KERR@ code).
72 * Use: Links a new key block into the complicated data structure
73 * which is a keyring file.
76 static int insert(key_file
*f
, key
*k
)
81 /* --- Sanity preservatives --- */
83 if (key_chkident(k
->type
))
84 return (KERR_BADTYPE
);
85 else if (k
->tag
&& key_chkident(k
->tag
))
88 /* --- Insert into the tag table --- */
91 kr
= sym_find(&f
->bytag
, k
->tag
, -1, sizeof(*kr
), &found
);
97 /* --- Insert into the id table --- */
102 bin
= HASH_BIN(&f
->byid
, k
->id
);
103 for (b
= *bin
; b
; b
= b
->next
) {
104 if (b
->hash
== k
->id
) {
106 sym_remove(&f
->bytag
, kr
);
116 /* --- Extend the table --- */
120 else if (hash_extend(&f
->byid
))
121 f
->idload
= KEY_LOAD(f
->byid
.mask
/ 2);
123 /* --- Insert into the type table --- */
125 kr
= sym_find(&f
->bytype
, k
->type
, -1, sizeof(*kr
), &found
);
131 if (k
->exp
!= KEXP_FOREVER
) {
132 while (*p
&& (*p
)->exp
!= KEXP_EXPIRE
&& (*p
)->exp
> k
->exp
)
142 /*----- Reading and writing keys ------------------------------------------*/
144 /* --- @exptime@ --- *
146 * Arguments: @const char *p@ = pointer to string
148 * Returns: Time value.
150 * Use: Translates an expiry or deletion time.
153 time_t exptime(const char *p
)
155 size_t sz
= strlen(p
);
156 if (strncmp(p
, "expired", sz
) == 0)
157 return (KEXP_EXPIRE
);
158 else if (strncmp(p
, "forever", sz
) == 0)
159 return (KEXP_FOREVER
);
164 /* --- @key_merge@ --- *
166 * Arguments: @key_file *f@ = pointer to file structure
167 * @const char *file@ = name of file (for error messages)
168 * @FILE *fp@ = file handle to read from
169 * @key_reporter *rep@ = error reporting function
170 * @void *arg@ = argument for function
172 * Returns: Error code (one of the @KERR@ constants).
174 * Use: Reads keys from a file, and inserts them into the file.
177 int key_merge(key_file
*f
, const char *file
, FILE *fp
,
178 key_reporter
*rep
, void *arg
)
182 dstr n
= DSTR_INIT
, v
= DSTR_INIT
;
184 if (!(f
->f
& KF_WRITE
))
185 return (KERR_READONLY
);
187 for (; dstr_putline(&l
, fp
) != EOF
; DRESET(&l
)) {
192 /* --- Skip blank lines and comments --- *
194 * Quite what they're doing in what ought to be an automatically-
195 * maintained file I don't know.
199 while (isspace((unsigned char)*p
))
201 if (!*p
|| *p
== '#')
204 /* --- Break the line into fields --- *
206 * There are currently six fields of interest:
208 * * The key's identification (id, tag and type).
209 * * The actual key data itself.
210 * * The key expiry time.
211 * * The key deletion time.
212 * * The attributes field.
213 * * Any further comments.
215 * All but the last field can contain no spaces.
219 int n
= str_split(p
, vf
, 5, &vf
[5]);
222 rep(file
, line
, "too few fields", arg
);
227 /* --- Allocate a new key block --- */
231 /* --- Extract the key data into the block --- */
233 if (key_read(vf
[1], &k
->k
, 0)) {
235 rep(file
, line
, "bad key data", arg
);
239 /* --- Decode the identification field --- *
241 * For compatibility, derive a keyid from the key data. This can only be
242 * done if the key encoding is binary (and presumably old-encoding binary
247 char *q
= strchr(vf
[0], ':');
251 if (k
->k
.e
!= KENC_BINARY
) {
253 rep(file
, line
, "new-style key encoding but no keyid", arg
);
256 k
->id
= crc32(0, k
->k
.u
.k
.k
, k
->k
.u
.k
.sz
);
257 k
->type
= xstrdup(vf
[0]);
261 k
->id
= strtoul(p
, 0, 16);
262 if ((qq
= strchr(q
, ':')) == 0 || !qq
[1]) {
268 k
->tag
= xstrdup(qq
);
270 k
->type
= xstrdup(q
);
274 /* --- Get a key block for the new key --- */
276 k
->exp
= exptime(vf
[2]);
277 k
->del
= exptime(vf
[3]);
279 /* --- Insert the key block into the table --- */
285 if ((err
= insert(f
, k
)) < 0) {
286 if (err
== KERR_DUPTAG
) {
288 rep(file
, line
, "duplicate key tag stripped", arg
);
294 rep(file
, line
, key_strerror(err
), arg
);
299 /* --- Parse up the attributes, if specified --- */
302 if (vf
[4] && strcmp(vf
[4], "-") != 0) {
304 for (url_initdec(&uc
, vf
[4]); url_dec(&uc
, &n
, &v
); ) {
305 key_putattr(f
, k
, n
.buf
, v
.buf
);
306 DRESET(&n
); DRESET(&v
);
310 /* --- Insert the comment --- */
313 k
->c
= xstrdup(vf
[5]);
318 /* --- Tidy up after something going wrong --- */
331 /* --- Extensive tidying up now required --- */
340 /* --- @key_extract@ --- *
342 * Arguments: @key_file *f@ = pointer to file structure
343 * @key *k@ = key to extract
344 * @FILE *fp@ = file to write on
345 * @const key_filter *kf@ = pointer to key selection block
347 * Returns: Zero if OK, EOF on error.
349 * Use: Extracts a key to an ouptut file.
352 int key_extract(key_file
*f
, key
*k
, FILE *fp
, const key_filter
*kf
)
357 /* --- Skip the key if it's deleted or unselected--- */
359 if (KEY_EXPIRED(t
, k
->del
) || !key_match(&k
->k
, kf
))
362 /* --- Encode the key and write the easy stuff --- */
366 key_write(&k
->k
, &d
, kf
);
371 /* --- Write out the expiry and deletion times --- */
373 if (KEY_EXPIRED(t
, k
->exp
))
374 fputs("expired ", fp
);
375 else if (k
->exp
== KEXP_FOREVER
)
376 fputs("forever ", fp
);
378 fprintf(fp
, "%li ", (long)k
->exp
);
380 if (k
->del
== KEXP_FOREVER
)
381 fputs("forever ", fp
);
383 fprintf(fp
, "%li ", (long)k
->del
);
385 /* --- Output the attributes --- */
394 for (sym_mkiter(&i
, &k
->a
); (a
= sym_next(&i
)) != 0; ) {
396 url_enc(&uc
, &d
, SYM_NAME(a
), a
->p
);
409 return (ferror(fp
) ? EOF
: 0);
412 /*----- Opening and closing files -----------------------------------------*/
414 /* --- @key_open@ --- *
416 * Arguments: @key_file *f@ = pointer to file structure to initialize
417 * @const char *file@ = pointer to the file name
418 * @int how@ = opening options (@KOPEN_*@).
419 * @key_reporter *rep@ = error reporting function
420 * @void *arg@ = argument for function
422 * Returns: Zero if it worked, nonzero otherwise.
424 * Use: Opens a key file, reads its contents, and stores them in a
425 * structure. The file is locked appropriately until closed
426 * using @key_close@. On an error, everything is cleared away
427 * tidily. If the file is opened with @KOPEN_WRITE@, it's
428 * created if necessary, with read and write permissions for its
432 int key_open(key_file
*f
, const char *file
, int how
,
433 key_reporter
*rep
, void *arg
)
435 if (key_lockfile(f
, file
, how
))
438 /* --- Trivial bits of initialization --- */
441 f
->name
= xstrdup(file
);
443 /* --- Read the file of keys into the table --- */
445 hash_create(&f
->byid
, 16);
446 f
->idload
= KEY_LOAD(16);
447 sym_create(&f
->bytype
);
448 sym_create(&f
->bytag
);
450 key_merge(f
, file
, f
->fp
, rep
, arg
);
451 if (how
== KOPEN_READ
)
452 f
->f
&= ~(KF_WRITE
| KF_MODIFIED
);
454 f
->f
&= ~KF_MODIFIED
;
456 /* --- Close the file if only needed for reading --- */
458 if (how
== KOPEN_READ
) {
466 /* --- @key_close@ --- *
468 * Arguments: @key_file *f@ = pointer to key file block
470 * Returns: A @KWRITE_@ code indicating how it went.
472 * Use: Frees all the key data, writes any changes. Make sure that
473 * all hell breaks loose if this returns @KWRITE_BROKEN@.
476 int key_close(key_file
*f
)
482 if ((e
= key_save(f
)) != KWRITE_OK
)
485 /* --- Free all the individual keys --- */
487 for (hash_mkiter(&i
, &f
->byid
); (b
= hash_next(&i
)) != 0; ) {
497 for (sym_mkiter(&j
, &k
->a
); (a
= sym_next(&j
)) != 0; )
502 hash_destroy(&f
->byid
);
503 sym_destroy(&f
->bytype
);
504 sym_destroy(&f
->bytag
);
514 * Arguments: @key_file *f@ = pointer to key file
515 * @uint32 id@ = keyid to set
516 * @const char *type@ = the type of this key
517 * @time_t exp@ = when the key expires
518 * @int *err@ = where to store the error condition
520 * Returns: Key block containing new data, or null if it couldn't be
523 * Use: Attaches a new key to a key file. You must have a writable
524 * key file for this to work.
526 * The type is a key type string. This interface doesn't care
527 * about how type strings are formatted: it just treats them as
528 * opaque gobs of text. Clients are advised to choose some
529 * standard for representing key types, though.
531 * The expiry time should either be a time in the future, or the
532 * magic value @KEXP_FOREVER@ which means `never expire this
533 * key'. Be careful with `forever' keys. If I were you, I'd
534 * use a more sophisticated key management system than this for
537 * You have to set the actual key yourself.
540 key
*key_new(key_file
*f
, uint32 id
, const char *type
, time_t exp
, int *err
)
546 /* --- Make sure the file is writable --- */
548 if (!(f
->f
& KF_WRITE
))
550 else if (KEY_EXPIRED(t
, exp
))
552 else if (key_chkident(type
))
558 k
->type
= xstrdup(type
);
559 k
->exp
= k
->del
= exp
;
563 if ((e
= insert(f
, k
)) == 0)
574 /*----- That's all, folks -------------------------------------------------*/