3 * $Id: key-io.c,v 1.4 2001/02/03 11:57:38 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.4 2001/02/03 11:57:38 mdw
34 * Allow creating keyfiles with no file attached.
36 * Revision 1.3 2001/01/20 11:56:48 mdw
37 * Use mLib exported tuning parameters for hashtable.
39 * Revision 1.2 2000/02/12 18:21:02 mdw
40 * Overhaul of key management (again).
42 * Revision 1.1 1999/12/22 15:47:48 mdw
43 * Major key-management revision.
47 /*----- Header files ------------------------------------------------------*/
56 #include <mLib/bits.h>
57 #include <mLib/crc32.h>
58 #include <mLib/dstr.h>
59 #include <mLib/hash.h>
67 /*----- Tweakable macros --------------------------------------------------*/
71 /*----- Low-level functions -----------------------------------------------*/
75 * Arguments: @key_file *f@ = pointer to file structure
76 * @key *k@ = pointer to key block to insert
78 * Returns: Error code (one of the @KERR@ code).
80 * Use: Links a new key block into the complicated data structure
81 * which is a keyring file.
84 static int insert(key_file
*f
, key
*k
)
89 /* --- Sanity preservatives --- */
91 if (key_chkident(k
->type
))
92 return (KERR_BADTYPE
);
93 else if (k
->tag
&& key_chkident(k
->tag
))
96 /* --- Insert into the tag table --- */
99 kr
= sym_find(&f
->bytag
, k
->tag
, -1, sizeof(*kr
), &found
);
101 return (KERR_DUPTAG
);
105 /* --- Insert into the id table --- */
110 bin
= HASH_BIN(&f
->byid
, k
->id
);
111 for (b
= *bin
; b
; b
= b
->next
) {
112 if (b
->hash
== k
->id
) {
114 sym_remove(&f
->bytag
, kr
);
124 /* --- Extend the table --- */
128 else if (hash_extend(&f
->byid
))
129 f
->idload
= SYM_LIMIT(f
->byid
.mask
/ 2);
131 /* --- Insert into the type table --- */
133 kr
= sym_find(&f
->bytype
, k
->type
, -1, sizeof(*kr
), &found
);
139 if (k
->exp
!= KEXP_FOREVER
) {
140 while (*p
&& (*p
)->exp
!= KEXP_EXPIRE
&& (*p
)->exp
> k
->exp
)
150 /*----- Reading and writing keys ------------------------------------------*/
152 /* --- @exptime@ --- *
154 * Arguments: @const char *p@ = pointer to string
156 * Returns: Time value.
158 * Use: Translates an expiry or deletion time.
161 time_t exptime(const char *p
)
163 size_t sz
= strlen(p
);
164 if (strncmp(p
, "expired", sz
) == 0)
165 return (KEXP_EXPIRE
);
166 else if (strncmp(p
, "forever", sz
) == 0)
167 return (KEXP_FOREVER
);
172 /* --- @key_merge@ --- *
174 * Arguments: @key_file *f@ = pointer to file structure
175 * @const char *file@ = name of file (for error messages)
176 * @FILE *fp@ = file handle to read from
177 * @key_reporter *rep@ = error reporting function
178 * @void *arg@ = argument for function
180 * Returns: Error code (one of the @KERR@ constants).
182 * Use: Reads keys from a file, and inserts them into the file.
185 int key_merge(key_file
*f
, const char *file
, FILE *fp
,
186 key_reporter
*rep
, void *arg
)
190 dstr n
= DSTR_INIT
, v
= DSTR_INIT
;
192 if (!(f
->f
& KF_WRITE
))
193 return (KERR_READONLY
);
195 for (; dstr_putline(&l
, fp
) != EOF
; DRESET(&l
)) {
200 /* --- Skip blank lines and comments --- *
202 * Quite what they're doing in what ought to be an automatically-
203 * maintained file I don't know.
207 while (isspace((unsigned char)*p
))
209 if (!*p
|| *p
== '#')
212 /* --- Break the line into fields --- *
214 * There are currently six fields of interest:
216 * * The key's identification (id, tag and type).
217 * * The actual key data itself.
218 * * The key expiry time.
219 * * The key deletion time.
220 * * The attributes field.
221 * * Any further comments.
223 * All but the last field can contain no spaces.
227 int n
= str_split(p
, vf
, 5, &vf
[5]);
230 rep(file
, line
, "too few fields", arg
);
235 /* --- Allocate a new key block --- */
239 /* --- Extract the key data into the block --- */
241 if (key_read(vf
[1], &k
->k
, 0)) {
243 rep(file
, line
, "bad key data", arg
);
247 /* --- Decode the identification field --- *
249 * For compatibility, derive a keyid from the key data. This can only be
250 * done if the key encoding is binary (and presumably old-encoding binary
255 char *q
= strchr(vf
[0], ':');
259 if (k
->k
.e
!= KENC_BINARY
) {
261 rep(file
, line
, "new-style key encoding but no keyid", arg
);
264 k
->id
= crc32(0, k
->k
.u
.k
.k
, k
->k
.u
.k
.sz
);
265 k
->type
= xstrdup(vf
[0]);
269 k
->id
= strtoul(p
, 0, 16);
270 if ((qq
= strchr(q
, ':')) == 0 || !qq
[1]) {
276 k
->tag
= xstrdup(qq
);
278 k
->type
= xstrdup(q
);
282 /* --- Get a key block for the new key --- */
284 k
->exp
= exptime(vf
[2]);
285 k
->del
= exptime(vf
[3]);
287 /* --- Insert the key block into the table --- */
293 if ((err
= insert(f
, k
)) < 0) {
294 if (err
== KERR_DUPTAG
) {
296 rep(file
, line
, "duplicate key tag stripped", arg
);
302 rep(file
, line
, key_strerror(err
), arg
);
307 /* --- Parse up the attributes, if specified --- */
310 if (vf
[4] && strcmp(vf
[4], "-") != 0) {
312 for (url_initdec(&uc
, vf
[4]); url_dec(&uc
, &n
, &v
); ) {
313 key_putattr(f
, k
, n
.buf
, v
.buf
);
314 DRESET(&n
); DRESET(&v
);
318 /* --- Insert the comment --- */
321 k
->c
= xstrdup(vf
[5]);
326 /* --- Tidy up after something going wrong --- */
339 /* --- Extensive tidying up now required --- */
348 /* --- @key_extract@ --- *
350 * Arguments: @key_file *f@ = pointer to file structure
351 * @key *k@ = key to extract
352 * @FILE *fp@ = file to write on
353 * @const key_filter *kf@ = pointer to key selection block
355 * Returns: Zero if OK, EOF on error.
357 * Use: Extracts a key to an ouptut file.
360 int key_extract(key_file
*f
, key
*k
, FILE *fp
, const key_filter
*kf
)
365 /* --- Skip the key if it's deleted or unselected--- */
367 if (KEY_EXPIRED(t
, k
->del
) || !key_match(&k
->k
, kf
))
370 /* --- Encode the key and write the easy stuff --- */
374 key_write(&k
->k
, &d
, kf
);
379 /* --- Write out the expiry and deletion times --- */
381 if (KEY_EXPIRED(t
, k
->exp
))
382 fputs("expired ", fp
);
383 else if (k
->exp
== KEXP_FOREVER
)
384 fputs("forever ", fp
);
386 fprintf(fp
, "%li ", (long)k
->exp
);
388 if (k
->del
== KEXP_FOREVER
)
389 fputs("forever ", fp
);
391 fprintf(fp
, "%li ", (long)k
->del
);
393 /* --- Output the attributes --- */
402 for (sym_mkiter(&i
, &k
->a
); (a
= sym_next(&i
)) != 0; ) {
404 url_enc(&uc
, &d
, SYM_NAME(a
), a
->p
);
417 return (ferror(fp
) ? EOF
: 0);
420 /*----- Opening and closing files -----------------------------------------*/
422 /* --- @key_open@ --- *
424 * Arguments: @key_file *f@ = pointer to file structure to initialize
425 * @const char *file@ = pointer to the file name
426 * @unsigned how@ = opening options (@KOPEN_*@).
427 * @key_reporter *rep@ = error reporting function
428 * @void *arg@ = argument for function
430 * Returns: Zero if it worked, nonzero otherwise.
432 * Use: Opens a key file, reads its contents, and stores them in a
433 * structure. The file is locked appropriately until closed
434 * using @key_close@. On an error, everything is cleared away
435 * tidily. If the file is opened with @KOPEN_WRITE@, it's
436 * created if necessary, with read and write permissions for its
440 int key_open(key_file
*f
, const char *file
, unsigned how
,
441 key_reporter
*rep
, void *arg
)
443 if (key_lockfile(f
, file
, how
))
447 f
->name
= xstrdup(file
);
449 hash_create(&f
->byid
, KEY_INITSZ
);
450 f
->idload
= SYM_LIMIT(KEY_INITSZ
);
451 sym_create(&f
->bytype
);
452 sym_create(&f
->bytag
);
455 key_merge(f
, file
, f
->fp
, rep
, arg
);
456 f
->f
&= ~KF_MODIFIED
;
458 if ((how
& KOPEN_MASK
) == KOPEN_READ
) {
467 /* --- @key_close@ --- *
469 * Arguments: @key_file *f@ = pointer to key file block
471 * Returns: A @KWRITE_@ code indicating how it went.
473 * Use: Frees all the key data, writes any changes. Make sure that
474 * all hell breaks loose if this returns @KWRITE_BROKEN@.
477 int key_close(key_file
*f
)
483 if (f
->fp
&& (e
= key_save(f
)) != KWRITE_OK
)
486 /* --- Free all the individual keys --- */
488 for (hash_mkiter(&i
, &f
->byid
); (b
= hash_next(&i
)) != 0; ) {
498 for (sym_mkiter(&j
, &k
->a
); (a
= sym_next(&j
)) != 0; )
503 hash_destroy(&f
->byid
);
504 sym_destroy(&f
->bytype
);
505 sym_destroy(&f
->bytag
);
515 * Arguments: @key_file *f@ = pointer to key file
516 * @uint32 id@ = keyid to set
517 * @const char *type@ = the type of this key
518 * @time_t exp@ = when the key expires
519 * @int *err@ = where to store the error condition
521 * Returns: Key block containing new data, or null if it couldn't be
524 * Use: Attaches a new key to a key file. You must have a writable
525 * key file for this to work.
527 * The type is a key type string. This interface doesn't care
528 * about how type strings are formatted: it just treats them as
529 * opaque gobs of text. Clients are advised to choose some
530 * standard for representing key types, though.
532 * The expiry time should either be a time in the future, or the
533 * magic value @KEXP_FOREVER@ which means `never expire this
534 * key'. Be careful with `forever' keys. If I were you, I'd
535 * use a more sophisticated key management system than this for
538 * You have to set the actual key yourself.
541 key
*key_new(key_file
*f
, uint32 id
, const char *type
, time_t exp
, int *err
)
547 /* --- Make sure the file is writable --- */
549 if (!(f
->f
& KF_WRITE
))
551 else if (KEY_EXPIRED(t
, exp
))
553 else if (key_chkident(type
))
559 k
->type
= xstrdup(type
);
560 k
->exp
= k
->del
= exp
;
564 if ((e
= insert(f
, k
)) == 0)
575 /*----- That's all, folks -------------------------------------------------*/