X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/05e4d756d456f903dc7b4a263fc58a13e2bff898..908ebb29ff621f582b34d48fd4278b60b78379b6:/key-io.c diff --git a/key-io.c b/key-io.c index 44bb803..5c70909 100644 --- a/key-io.c +++ b/key-io.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: key-io.c,v 1.3 2001/01/20 11:56:48 mdw Exp $ + * $Id$ * * Adding new keys to a key file * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,32 +15,18 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * Catacomb is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with Catacomb; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: key-io.c,v $ - * Revision 1.3 2001/01/20 11:56:48 mdw - * Use mLib exported tuning parameters for hashtable. - * - * Revision 1.2 2000/02/12 18:21:02 mdw - * Overhaul of key management (again). - * - * Revision 1.1 1999/12/22 15:47:48 mdw - * Major key-management revision. - * - */ - /*----- Header files ------------------------------------------------------*/ #include @@ -210,12 +196,12 @@ int key_merge(key_file *f, const char *file, FILE *fp, * * There are currently six fields of interest: * - * * The key's identification (id, tag and type). - * * The actual key data itself. - * * The key expiry time. - * * The key deletion time. - * * The attributes field. - * * Any further comments. + * * The key's identification (id, tag and type). + * * The actual key data itself. + * * The key expiry time. + * * The key deletion time. + * * The attributes field. + * * Any further comments. * * All but the last field can contain no spaces. */ @@ -235,7 +221,7 @@ int key_merge(key_file *f, const char *file, FILE *fp, /* --- Extract the key data into the block --- */ - if (key_read(vf[1], &k->k, 0)) { + if ((k->k = key_read(vf[1], 0)) == 0) { if (rep) rep(file, line, "bad key data", arg); goto skip_1; @@ -253,12 +239,12 @@ int key_merge(key_file *f, const char *file, FILE *fp, char *qq; if (!q) { - if (k->k.e != KENC_BINARY) { + if (k->k->e != KENC_BINARY) { if (rep) rep(file, line, "new-style key encoding but no keyid", arg); goto skip_2; } - k->id = crc32(0, k->k.u.k.k, k->k.u.k.sz); + k->id = crc32(0, k->k->u.k.k, k->k->u.k.sz); k->type = xstrdup(vf[0]); k->tag = 0; } else { @@ -291,7 +277,7 @@ int key_merge(key_file *f, const char *file, FILE *fp, if (err == KERR_DUPTAG) { if (rep) rep(file, line, "duplicate key tag stripped", arg); - free(k->tag); + xfree(k->tag); k->tag = 0; goto again; } @@ -300,7 +286,7 @@ int key_merge(key_file *f, const char *file, FILE *fp, goto skip_3; } } - + /* --- Parse up the attributes, if specified --- */ sym_create(&k->a); @@ -324,10 +310,10 @@ int key_merge(key_file *f, const char *file, FILE *fp, skip_3: if (k->tag) - free(k->tag); - free(k->type); + xfree(k->tag); + xfree(k->type); skip_2: - key_destroy(&k->k); + key_drop(k->k); skip_1: DESTROY(k); skip_0:; @@ -361,14 +347,14 @@ int key_extract(key_file *f, key *k, FILE *fp, const key_filter *kf) /* --- Skip the key if it's deleted or unselected--- */ - if (KEY_EXPIRED(t, k->del) || !key_match(&k->k, kf)) + if (KEY_EXPIRED(t, k->del) || !key_match(k->k, kf)) return (0); /* --- Encode the key and write the easy stuff --- */ key_fulltag(k, &d); DPUTC(&d, ' '); - key_write(&k->k, &d, kf); + key_write(k->k, &d, kf); DPUTC(&d, ' '); dstr_write(&d, fp); DRESET(&d); @@ -420,7 +406,7 @@ int key_extract(key_file *f, key *k, FILE *fp, const key_filter *kf) * * Arguments: @key_file *f@ = pointer to file structure to initialize * @const char *file@ = pointer to the file name - * @int how@ = opening options (@KOPEN_*@). + * @unsigned how@ = opening options (@KOPEN_*@). * @key_reporter *rep@ = error reporting function * @void *arg@ = argument for function * @@ -434,33 +420,27 @@ int key_extract(key_file *f, key *k, FILE *fp, const key_filter *kf) * owner only. */ -int key_open(key_file *f, const char *file, int how, +int key_open(key_file *f, const char *file, unsigned how, key_reporter *rep, void *arg) { - if (key_lockfile(f, file, how)) + if (key_lockfile(f, file, how)) { + rep(file, 0, strerror(errno), arg); return (-1); - - /* --- Trivial bits of initialization --- */ - + } f->f = 0; f->name = xstrdup(file); - /* --- Read the file of keys into the table --- */ - hash_create(&f->byid, KEY_INITSZ); f->idload = SYM_LIMIT(KEY_INITSZ); sym_create(&f->bytype); sym_create(&f->bytag); f->f |= KF_WRITE; - key_merge(f, file, f->fp, rep, arg); - if (how == KOPEN_READ) - f->f &= ~(KF_WRITE | KF_MODIFIED); - else - f->f &= ~KF_MODIFIED; - - /* --- Close the file if only needed for reading --- */ + if (f->fp) + key_merge(f, file, f->fp, rep, arg); + f->f &= ~KF_MODIFIED; - if (how == KOPEN_READ) { + if ((how & KOPEN_MASK) == KOPEN_READ) { + f->f &= ~KF_WRITE; fclose(f->fp); f->fp = 0; } @@ -468,25 +448,20 @@ int key_open(key_file *f, const char *file, int how, return (0); } -/* --- @key_close@ --- * +/* --- @key_discard@ --- * * * Arguments: @key_file *f@ = pointer to key file block * - * Returns: A @KWRITE_@ code indicating how it went. + * Returns: --- * - * Use: Frees all the key data, writes any changes. Make sure that - * all hell breaks loose if this returns @KWRITE_BROKEN@. + * Use: Frees all the key data, without writing changes. */ -int key_close(key_file *f) +void key_discard(key_file *f) { - int e; hash_base *b; hash_iter i; - if ((e = key_save(f)) != KWRITE_OK) - return (e); - /* --- Free all the individual keys --- */ for (hash_mkiter(&i, &f->byid); (b = hash_next(&i)) != 0; ) { @@ -494,13 +469,13 @@ int key_close(key_file *f) key_attr *a; key *k = (key *)b; - key_destroy(&k->k); - free(k->type); - free(k->tag); + if (k->k) key_drop(k->k); + xfree(k->type); + xfree(k->tag); if (k->c) - free(k->c); + xfree(k->c); for (sym_mkiter(&j, &k->a); (a = sym_next(&j)) != 0; ) - free(a->p); + xfree(a->p); sym_destroy(&k->a); DESTROY(k); } @@ -510,7 +485,26 @@ int key_close(key_file *f) if (f->fp) fclose(f->fp); - free(f->name); + xfree(f->name); +} + +/* --- @key_close@ --- * + * + * Arguments: @key_file *f@ = pointer to key file block + * + * Returns: A @KWRITE_@ code indicating how it went. + * + * Use: Frees all the key data, writes any changes. Make sure that + * all hell breaks loose if this returns @KWRITE_BROKEN@. + */ + +int key_close(key_file *f) +{ + int e; + + if (f->fp && (e = key_save(f)) != KWRITE_OK) + return (e); + key_discard(f); return (KWRITE_OK); } @@ -520,10 +514,9 @@ int key_close(key_file *f) * @uint32 id@ = keyid to set * @const char *type@ = the type of this key * @time_t exp@ = when the key expires - * @int *err@ = where to store the error condition + * @key *kk@ = where to put the key pointer * - * Returns: Key block containing new data, or null if it couldn't be - * done. + * Returns: Error code (one of the @KERR@ constants). * * Use: Attaches a new key to a key file. You must have a writable * key file for this to work. @@ -538,11 +531,9 @@ int key_close(key_file *f) * key'. Be careful with `forever' keys. If I were you, I'd * use a more sophisticated key management system than this for * them. - * - * You have to set the actual key yourself. */ -key *key_new(key_file *f, uint32 id, const char *type, time_t exp, int *err) +int key_new(key_file *f, uint32 id, const char *type, time_t exp, key **kk) { key *k = 0; time_t t = time(0); @@ -560,20 +551,21 @@ key *key_new(key_file *f, uint32 id, const char *type, time_t exp, int *err) k = CREATE(key); k->id = id; k->tag = 0; - k->type = xstrdup(type); k->exp = k->del = exp; k->c = 0; - k->k.e = 0; + k->type = (char *)type; /* temporarily */ sym_create(&k->a); - if ((e = insert(f, k)) == 0) - f->f |= KF_MODIFIED; - else { - free(k->type); + if ((e = insert(f, k)) != 0) DESTROY(k); - k = 0; + else { + k->k = key_newstring(KCAT_SHARE, ""); + k->type = xstrdup(type); + *kk = k; + f->f |= KF_MODIFIED; } } - return (k); + + return (e); } /*----- That's all, folks -------------------------------------------------*/