Commit | Line | Data |
---|---|---|
d11a0bf7 | 1 | /* -*-c-*- |
2 | * | |
d11a0bf7 | 3 | * Adding new keys to a key file |
4 | * | |
5 | * (c) 1999 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
d11a0bf7 | 9 | * |
10 | * This file is part of Catacomb. | |
11 | * | |
12 | * Catacomb is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU Library General Public License as | |
14 | * published by the Free Software Foundation; either version 2 of the | |
15 | * License, or (at your option) any later version. | |
45c0fd36 | 16 | * |
d11a0bf7 | 17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU Library General Public License for more details. | |
45c0fd36 | 21 | * |
d11a0bf7 | 22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free | |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
d11a0bf7 | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
30 | #include <ctype.h> | |
31 | #include <errno.h> | |
32 | #include <stdio.h> | |
33 | #include <stdlib.h> | |
34 | #include <string.h> | |
35 | #include <time.h> | |
36 | ||
d11a0bf7 | 37 | #include <mLib/bits.h> |
38 | #include <mLib/crc32.h> | |
39 | #include <mLib/dstr.h> | |
40 | #include <mLib/hash.h> | |
141c1284 | 41 | #include <mLib/macros.h> |
d11a0bf7 | 42 | #include <mLib/str.h> |
43 | #include <mLib/sub.h> | |
44 | #include <mLib/sym.h> | |
45 | #include <mLib/url.h> | |
46 | ||
47 | #include "key.h" | |
48 | ||
49 | /*----- Tweakable macros --------------------------------------------------*/ | |
50 | ||
05e4d756 | 51 | #define KEY_INITSZ 16 |
d11a0bf7 | 52 | |
53 | /*----- Low-level functions -----------------------------------------------*/ | |
54 | ||
55 | /* --- @insert@ --- * | |
56 | * | |
57 | * Arguments: @key_file *f@ = pointer to file structure | |
58 | * @key *k@ = pointer to key block to insert | |
59 | * | |
60 | * Returns: Error code (one of the @KERR@ code). | |
61 | * | |
62 | * Use: Links a new key block into the complicated data structure | |
63 | * which is a keyring file. | |
64 | */ | |
65 | ||
66 | static int insert(key_file *f, key *k) | |
67 | { | |
68 | key_ref *kr = 0; | |
69 | unsigned found; | |
70 | ||
71 | /* --- Sanity preservatives --- */ | |
72 | ||
73 | if (key_chkident(k->type)) | |
74 | return (KERR_BADTYPE); | |
75 | else if (k->tag && key_chkident(k->tag)) | |
76 | return (KERR_BADTAG); | |
77 | ||
78 | /* --- Insert into the tag table --- */ | |
79 | ||
80 | if (k->tag) { | |
81 | kr = sym_find(&f->bytag, k->tag, -1, sizeof(*kr), &found); | |
82 | if (found) | |
83 | return (KERR_DUPTAG); | |
84 | kr->k = k; | |
85 | } | |
86 | ||
87 | /* --- Insert into the id table --- */ | |
88 | ||
89 | { | |
90 | hash_base **bin, *b; | |
91 | ||
92 | bin = HASH_BIN(&f->byid, k->id); | |
93 | for (b = *bin; b; b = b->next) { | |
94 | if (b->hash == k->id) { | |
95 | if (kr) | |
96 | sym_remove(&f->bytag, kr); | |
97 | return (KERR_DUPID); | |
98 | } | |
99 | } | |
100 | ||
101 | k->_b.next = *bin; | |
102 | *bin = &k->_b; | |
103 | k->_b.hash = k->id; | |
104 | } | |
105 | ||
106 | /* --- Extend the table --- */ | |
107 | ||
108 | if (f->idload > 0) | |
109 | f->idload--; | |
110 | else if (hash_extend(&f->byid)) | |
05e4d756 | 111 | f->idload = SYM_LIMIT(f->byid.mask / 2); |
d11a0bf7 | 112 | |
113 | /* --- Insert into the type table --- */ | |
114 | ||
115 | kr = sym_find(&f->bytype, k->type, -1, sizeof(*kr), &found); | |
116 | if (!found) { | |
117 | kr->k = k; | |
118 | k->next = 0; | |
119 | } else { | |
120 | key **p = &kr->k; | |
121 | if (k->exp != KEXP_FOREVER) { | |
122 | while (*p && (*p)->exp != KEXP_EXPIRE && (*p)->exp > k->exp) | |
123 | p = &(*p)->next; | |
124 | } | |
125 | k->next = *p; | |
126 | *p = k; | |
127 | } | |
128 | ||
129 | return (KERR_OK); | |
130 | } | |
131 | ||
132 | /*----- Reading and writing keys ------------------------------------------*/ | |
133 | ||
134 | /* --- @exptime@ --- * | |
135 | * | |
136 | * Arguments: @const char *p@ = pointer to string | |
137 | * | |
138 | * Returns: Time value. | |
139 | * | |
140 | * Use: Translates an expiry or deletion time. | |
141 | */ | |
142 | ||
710440c8 | 143 | static time_t exptime(const char *p) |
d11a0bf7 | 144 | { |
145 | size_t sz = strlen(p); | |
141c1284 | 146 | if (STRNCMP(p, ==, "expired", sz)) |
d11a0bf7 | 147 | return (KEXP_EXPIRE); |
141c1284 | 148 | else if (STRNCMP(p, ==, "forever", sz)) |
d11a0bf7 | 149 | return (KEXP_FOREVER); |
150 | else | |
151 | return (atol(p)); | |
152 | } | |
153 | ||
8f2287ef | 154 | /* --- @merge_core@ --- * |
d11a0bf7 | 155 | * |
156 | * Arguments: @key_file *f@ = pointer to file structure | |
157 | * @const char *file@ = name of file (for error messages) | |
8f2287ef MW |
158 | * @int lno@ = line number |
159 | * @char *p@ = pointer into the line buffer (which will be | |
160 | * clobbered) | |
d11a0bf7 | 161 | * @key_reporter *rep@ = error reporting function |
162 | * @void *arg@ = argument for function | |
8f2287ef MW |
163 | * @dstr *n, *v@ = scratch dynamic strings, which must be empty |
164 | * on entry and are left empty on exit | |
d11a0bf7 | 165 | * |
8f2287ef MW |
166 | * Returns: --- |
167 | * | |
168 | * Use: This is the common core of @key_merge@ and @key_mergeline@. | |
d11a0bf7 | 169 | * |
8f2287ef MW |
170 | * It is assumed that the caller has already verified that the |
171 | * keyring is writable. | |
d11a0bf7 | 172 | */ |
173 | ||
8f2287ef MW |
174 | static void merge_core(key_file *f, const char *file, int lno, char *p, |
175 | key_reporter *rep, void *arg, dstr *n, dstr *v) | |
d11a0bf7 | 176 | { |
8f2287ef MW |
177 | char *vf[6]; |
178 | key *k = 0; | |
179 | ||
180 | /* --- Skip blank lines and comments --- * | |
181 | * | |
182 | * Quite what they're doing in what ought to be an automatically- | |
183 | * maintained file I don't know. | |
184 | */ | |
185 | ||
141c1284 | 186 | while (ISSPACE(*p)) |
8f2287ef MW |
187 | p++; |
188 | if (!*p || *p == '#') | |
189 | goto skip; | |
190 | ||
191 | /* --- Break the line into fields --- * | |
192 | * | |
193 | * There are currently six fields of interest: | |
194 | * | |
195 | * * The key's identification (id, tag and type). | |
196 | * * The actual key data itself. | |
197 | * * The key expiry time. | |
198 | * * The key deletion time. | |
199 | * * The attributes field. | |
200 | * * Any further comments. | |
201 | * | |
202 | * All but the last field can contain no spaces. | |
203 | */ | |
d11a0bf7 | 204 | |
8f2287ef MW |
205 | { |
206 | int n = str_split(p, vf, 5, &vf[5]); | |
207 | if (n < 4) { | |
208 | if (rep) | |
209 | rep(file, lno, "too few fields", arg); | |
210 | goto skip; | |
d11a0bf7 | 211 | } |
8f2287ef | 212 | } |
d11a0bf7 | 213 | |
8f2287ef | 214 | /* --- Allocate a new key block --- */ |
d11a0bf7 | 215 | |
8f2287ef MW |
216 | k = CREATE(key); |
217 | k->k = 0; | |
218 | k->tag = 0; | |
219 | k->type = 0; | |
d11a0bf7 | 220 | |
8f2287ef | 221 | /* --- Extract the key data into the block --- */ |
d11a0bf7 | 222 | |
8f2287ef MW |
223 | if ((k->k = key_read(vf[1], 0)) == 0) { |
224 | if (rep) | |
225 | rep(file, lno, "bad key data", arg); | |
226 | goto skip; | |
227 | } | |
228 | ||
229 | /* --- Decode the identification field --- * | |
230 | * | |
231 | * For compatibility, derive a keyid from the key data. This can only be | |
232 | * done if the key encoding is binary (and presumably old-encoding binary | |
233 | * at that). | |
234 | */ | |
235 | ||
236 | { | |
237 | char *q = strchr(vf[0], ':'); | |
238 | char *qq; | |
d11a0bf7 | 239 | |
8f2287ef MW |
240 | if (!q) { |
241 | if (k->k->e != KENC_BINARY) { | |
242 | if (rep) | |
243 | rep(file, lno, "new-style key encoding but no keyid", arg); | |
244 | goto skip; | |
245 | } | |
246 | k->id = crc32(0, k->k->u.k.k, k->k->u.k.sz); | |
247 | k->type = xstrdup(vf[0]); | |
248 | k->tag = 0; | |
249 | } else { | |
250 | *q++ = 0; | |
251 | k->id = strtoul(p, 0, 16); | |
252 | if ((qq = strchr(q, ':')) == 0 || !qq[1]) { | |
253 | if (qq) | |
254 | *qq = 0; | |
d11a0bf7 | 255 | k->tag = 0; |
256 | } else { | |
8f2287ef MW |
257 | *qq++ = 0; |
258 | k->tag = xstrdup(qq); | |
d11a0bf7 | 259 | } |
8f2287ef | 260 | k->type = xstrdup(q); |
d11a0bf7 | 261 | } |
8f2287ef | 262 | } |
d11a0bf7 | 263 | |
8f2287ef | 264 | /* --- Get a key block for the new key --- */ |
d11a0bf7 | 265 | |
8f2287ef MW |
266 | k->exp = exptime(vf[2]); |
267 | k->del = exptime(vf[3]); | |
d11a0bf7 | 268 | |
8f2287ef | 269 | /* --- Insert the key block into the table --- */ |
d11a0bf7 | 270 | |
8f2287ef MW |
271 | { |
272 | int err; | |
d11a0bf7 | 273 | |
8f2287ef MW |
274 | again: |
275 | if ((err = insert(f, k)) < 0) { | |
276 | if (err == KERR_DUPTAG) { | |
d11a0bf7 | 277 | if (rep) |
8f2287ef MW |
278 | rep(file, lno, "duplicate key tag stripped", arg); |
279 | xfree(k->tag); | |
280 | k->tag = 0; | |
281 | goto again; | |
d11a0bf7 | 282 | } |
8f2287ef MW |
283 | if (rep) |
284 | rep(file, lno, key_strerror(err), arg); | |
285 | goto skip; | |
d11a0bf7 | 286 | } |
8f2287ef | 287 | } |
45c0fd36 | 288 | |
8f2287ef | 289 | /* --- Parse up the attributes, if specified --- */ |
d11a0bf7 | 290 | |
141c1284 MW |
291 | sym_create(&k->a); |
292 | if (vf[4] && STRCMP(vf[4], !=, "-")) { | |
293 | url_dctx uc; | |
8f2287ef MW |
294 | for (url_initdec(&uc, vf[4]); url_dec(&uc, n, v); ) { |
295 | key_putattr(f, k, n->buf, v->buf); | |
296 | DRESET(n); DRESET(v); | |
d11a0bf7 | 297 | } |
8f2287ef | 298 | } |
d11a0bf7 | 299 | |
8f2287ef | 300 | /* --- Insert the comment --- */ |
d11a0bf7 | 301 | |
8f2287ef MW |
302 | if (vf[5]) |
303 | k->c = xstrdup(vf[5]); | |
304 | else | |
305 | k->c = 0; | |
d11a0bf7 | 306 | |
8f2287ef | 307 | /* --- Done --- */ |
d11a0bf7 | 308 | |
8f2287ef MW |
309 | f->f |= KF_MODIFIED; |
310 | return; | |
311 | ||
312 | /* --- Tidy up after something going wrong --- */ | |
313 | ||
314 | skip: | |
315 | if (k) { | |
316 | if (k->tag) xfree(k->tag); | |
317 | if (k->type) xfree(k->type); | |
318 | if (k->k) key_drop(k->k); | |
d11a0bf7 | 319 | DESTROY(k); |
d11a0bf7 | 320 | } |
8f2287ef | 321 | } |
d11a0bf7 | 322 | |
8f2287ef MW |
323 | /* --- @key_merge@, @key_mergeline@ --- * |
324 | * | |
325 | * Arguments: @key_file *f@ = pointer to file structure | |
326 | * @const char *file@ = name of file (for error messages) | |
327 | * @int lno@ = line number (for error messages, @key_mergeline@) | |
328 | * @FILE *fp@ = file handle to read from (@key_merge@) | |
329 | * @const char *line@ = line from the input (@key_mergeline@) | |
330 | * @key_reporter *rep@ = error reporting function | |
331 | * @void *arg@ = argument for function | |
332 | * | |
333 | * Returns: Error code (one of the @KERR@ constants). | |
334 | * | |
335 | * Use: The @key_merge@ function reads keys from a file, and inserts | |
336 | * them into the keyring. | |
337 | * | |
338 | * The @key_mergeline@ function reads a key from a single input | |
339 | * line (which may, but need not, have a final newline), and | |
340 | * adds it to the keyring. | |
341 | * | |
342 | * The @key_mergeline@ function is intended to help with | |
343 | * interfacing Catacomb to runtimes which don't use C's @stdio@ | |
344 | * streams, rather than as a general-purpose service, though if | |
345 | * it turns out to be useful in other ways then so much the | |
346 | * better. | |
347 | */ | |
348 | ||
349 | int key_merge(key_file *f, const char *file, FILE *fp, | |
350 | key_reporter *rep, void *arg) | |
351 | { | |
352 | dstr n = DSTR_INIT, v = DSTR_INIT; | |
353 | dstr l = DSTR_INIT; | |
354 | int lno = 1; | |
355 | ||
356 | if (!(f->f & KF_WRITE)) | |
357 | return (KERR_READONLY); | |
358 | ||
359 | for (; dstr_putline(&l, fp) != EOF; DRESET(&l)) | |
360 | merge_core(f, file, lno++, l.buf, rep, arg, &n, &v); | |
d11a0bf7 | 361 | |
362 | dstr_destroy(&l); | |
8f2287ef MW |
363 | dstr_destroy(&n); dstr_destroy(&v); |
364 | return (0); | |
365 | } | |
366 | ||
367 | int key_mergeline(key_file *f, const char *file, int lno, const char *line, | |
368 | key_reporter *rep, void *arg) | |
369 | { | |
370 | dstr n = DSTR_INIT, v = DSTR_INIT; | |
371 | size_t len = strlen(line); | |
372 | char *p; | |
373 | ||
374 | if (!(f->f & KF_WRITE)) return (KERR_READONLY); | |
375 | ||
376 | if (len && line[len - 1] == '\n') len--; | |
377 | p = xmalloc(len); memcpy(p, line, len); p[len] = 0; | |
378 | merge_core(f, file, lno, p, rep, arg, &n, &v); | |
379 | xfree(p); dstr_destroy(&n); dstr_destroy(&v); | |
d11a0bf7 | 380 | return (0); |
381 | } | |
382 | ||
8f2287ef | 383 | /* --- @key_extract@, @key_extractline@ --- * |
d11a0bf7 | 384 | * |
385 | * Arguments: @key_file *f@ = pointer to file structure | |
386 | * @key *k@ = key to extract | |
8f2287ef MW |
387 | * @FILE *fp@ = file to write on (@key_extract@) |
388 | * @dstr *d@ = string to write on (@key_extractline@) | |
d11a0bf7 | 389 | * @const key_filter *kf@ = pointer to key selection block |
390 | * | |
8f2287ef MW |
391 | * Returns: @key_extract@ returns zero if OK, EOF on error. |
392 | * @key_extractline@ does not return a value. | |
d11a0bf7 | 393 | * |
8f2287ef MW |
394 | * Use: Extracts a key to an ouptut file or buffer. |
395 | * | |
396 | * The @key_extractline@ includes a final newline in its output. | |
397 | * | |
398 | * The @key_extractline@ function is intended to help with | |
399 | * interfacing Catacomb to runtimes which don't use C's @stdio@ | |
400 | * streams, rather than as a general-purpose service, though if | |
401 | * it turns out to be useful in other ways then so much the | |
402 | * better. | |
d11a0bf7 | 403 | */ |
404 | ||
8f2287ef | 405 | void key_extractline(key_file *f, key *k, dstr *d, const key_filter *kf) |
d11a0bf7 | 406 | { |
d11a0bf7 | 407 | time_t t = time(0); |
408 | ||
409 | /* --- Skip the key if it's deleted or unselected--- */ | |
410 | ||
ef13e9a4 | 411 | if (KEY_EXPIRED(t, k->del) || !key_match(k->k, kf)) |
8f2287ef | 412 | return; |
d11a0bf7 | 413 | |
414 | /* --- Encode the key and write the easy stuff --- */ | |
415 | ||
8f2287ef MW |
416 | key_fulltag(k, d); |
417 | DPUTC(d, ' '); | |
418 | if (!key_write(k->k, d, kf)) dstr_puts(d, "struct:[]"); | |
419 | DPUTC(d, ' '); | |
d11a0bf7 | 420 | |
421 | /* --- Write out the expiry and deletion times --- */ | |
422 | ||
423 | if (KEY_EXPIRED(t, k->exp)) | |
8f2287ef | 424 | dstr_puts(d, "expired "); |
d11a0bf7 | 425 | else if (k->exp == KEXP_FOREVER) |
8f2287ef | 426 | dstr_puts(d, "forever "); |
d11a0bf7 | 427 | else |
8f2287ef | 428 | dstr_putf(d, "%li ", (long)k->exp); |
d11a0bf7 | 429 | |
430 | if (k->del == KEXP_FOREVER) | |
8f2287ef | 431 | dstr_puts(d, "forever "); |
d11a0bf7 | 432 | else |
8f2287ef | 433 | dstr_putf(d, "%li ", (long)k->del); |
d11a0bf7 | 434 | |
435 | /* --- Output the attributes --- */ | |
436 | ||
437 | { | |
438 | int none = 1; | |
439 | sym_iter i; | |
440 | key_attr *a; | |
441 | url_ectx uc; | |
442 | ||
443 | url_initenc(&uc); | |
444 | for (sym_mkiter(&i, &k->a); (a = sym_next(&i)) != 0; ) { | |
445 | none = 0; | |
8f2287ef | 446 | url_enc(&uc, d, SYM_NAME(a), a->p); |
d11a0bf7 | 447 | } |
448 | if (none) | |
8f2287ef | 449 | DPUTC(d, '-'); |
d11a0bf7 | 450 | } |
451 | ||
8f2287ef MW |
452 | if (k->c) |
453 | dstr_putf(d, " %s", k->c); | |
454 | ||
455 | DPUTC(d, '\n'); DPUTZ(d); | |
456 | } | |
457 | ||
458 | int key_extract(key_file *f, key *k, FILE *fp, const key_filter *kf) | |
459 | { | |
460 | dstr d = DSTR_INIT; | |
461 | ||
462 | key_extractline(f, k, &d, kf); | |
463 | dstr_write(&d, fp); | |
d11a0bf7 | 464 | return (ferror(fp) ? EOF : 0); |
465 | } | |
466 | ||
467 | /*----- Opening and closing files -----------------------------------------*/ | |
468 | ||
469 | /* --- @key_open@ --- * | |
470 | * | |
471 | * Arguments: @key_file *f@ = pointer to file structure to initialize | |
472 | * @const char *file@ = pointer to the file name | |
9f1b58fe | 473 | * @unsigned how@ = opening options (@KOPEN_*@). |
d11a0bf7 | 474 | * @key_reporter *rep@ = error reporting function |
475 | * @void *arg@ = argument for function | |
476 | * | |
477 | * Returns: Zero if it worked, nonzero otherwise. | |
478 | * | |
479 | * Use: Opens a key file, reads its contents, and stores them in a | |
480 | * structure. The file is locked appropriately until closed | |
481 | * using @key_close@. On an error, everything is cleared away | |
482 | * tidily. If the file is opened with @KOPEN_WRITE@, it's | |
483 | * created if necessary, with read and write permissions for its | |
484 | * owner only. | |
485 | */ | |
486 | ||
9f1b58fe | 487 | int key_open(key_file *f, const char *file, unsigned how, |
d11a0bf7 | 488 | key_reporter *rep, void *arg) |
489 | { | |
f9984d11 | 490 | if (key_lockfile(f, file, how)) { |
491 | rep(file, 0, strerror(errno), arg); | |
d11a0bf7 | 492 | return (-1); |
f9984d11 | 493 | } |
d11a0bf7 | 494 | f->f = 0; |
495 | f->name = xstrdup(file); | |
496 | ||
05e4d756 | 497 | hash_create(&f->byid, KEY_INITSZ); |
498 | f->idload = SYM_LIMIT(KEY_INITSZ); | |
d11a0bf7 | 499 | sym_create(&f->bytype); |
500 | sym_create(&f->bytag); | |
501 | f->f |= KF_WRITE; | |
9f1b58fe | 502 | if (f->fp) |
503 | key_merge(f, file, f->fp, rep, arg); | |
504 | f->f &= ~KF_MODIFIED; | |
d11a0bf7 | 505 | |
9f1b58fe | 506 | if ((how & KOPEN_MASK) == KOPEN_READ) { |
507 | f->f &= ~KF_WRITE; | |
692e9621 | 508 | if (f->fp) fclose(f->fp); |
d11a0bf7 | 509 | f->fp = 0; |
510 | } | |
511 | ||
512 | return (0); | |
513 | } | |
514 | ||
d901c888 | 515 | /* --- @key_discard@ --- * |
d11a0bf7 | 516 | * |
517 | * Arguments: @key_file *f@ = pointer to key file block | |
518 | * | |
d901c888 | 519 | * Returns: --- |
d11a0bf7 | 520 | * |
d901c888 | 521 | * Use: Frees all the key data, without writing changes. |
d11a0bf7 | 522 | */ |
523 | ||
01e60364 | 524 | void key_discard(key_file *f) |
d11a0bf7 | 525 | { |
d11a0bf7 | 526 | hash_base *b; |
527 | hash_iter i; | |
528 | ||
d11a0bf7 | 529 | /* --- Free all the individual keys --- */ |
530 | ||
531 | for (hash_mkiter(&i, &f->byid); (b = hash_next(&i)) != 0; ) { | |
532 | sym_iter j; | |
533 | key_attr *a; | |
534 | key *k = (key *)b; | |
535 | ||
ef13e9a4 | 536 | if (k->k) key_drop(k->k); |
d901c888 | 537 | xfree(k->type); |
538 | xfree(k->tag); | |
d11a0bf7 | 539 | if (k->c) |
d901c888 | 540 | xfree(k->c); |
d11a0bf7 | 541 | for (sym_mkiter(&j, &k->a); (a = sym_next(&j)) != 0; ) |
d901c888 | 542 | xfree(a->p); |
d11a0bf7 | 543 | sym_destroy(&k->a); |
544 | DESTROY(k); | |
545 | } | |
546 | hash_destroy(&f->byid); | |
547 | sym_destroy(&f->bytype); | |
548 | sym_destroy(&f->bytag); | |
549 | ||
550 | if (f->fp) | |
551 | fclose(f->fp); | |
d901c888 | 552 | xfree(f->name); |
553 | } | |
554 | ||
555 | /* --- @key_close@ --- * | |
556 | * | |
557 | * Arguments: @key_file *f@ = pointer to key file block | |
558 | * | |
559 | * Returns: A @KWRITE_@ code indicating how it went. | |
560 | * | |
561 | * Use: Frees all the key data, writes any changes. Make sure that | |
562 | * all hell breaks loose if this returns @KWRITE_BROKEN@. | |
563 | */ | |
564 | ||
565 | int key_close(key_file *f) | |
566 | { | |
567 | int e; | |
568 | ||
569 | if (f->fp && (e = key_save(f)) != KWRITE_OK) | |
570 | return (e); | |
571 | key_discard(f); | |
d11a0bf7 | 572 | return (KWRITE_OK); |
573 | } | |
574 | ||
575 | /* --- @key_new@ --- | |
576 | * | |
577 | * Arguments: @key_file *f@ = pointer to key file | |
578 | * @uint32 id@ = keyid to set | |
579 | * @const char *type@ = the type of this key | |
580 | * @time_t exp@ = when the key expires | |
ef13e9a4 | 581 | * @key *kk@ = where to put the key pointer |
d11a0bf7 | 582 | * |
ef13e9a4 | 583 | * Returns: Error code (one of the @KERR@ constants). |
d11a0bf7 | 584 | * |
585 | * Use: Attaches a new key to a key file. You must have a writable | |
586 | * key file for this to work. | |
587 | * | |
588 | * The type is a key type string. This interface doesn't care | |
589 | * about how type strings are formatted: it just treats them as | |
590 | * opaque gobs of text. Clients are advised to choose some | |
591 | * standard for representing key types, though. | |
592 | * | |
593 | * The expiry time should either be a time in the future, or the | |
594 | * magic value @KEXP_FOREVER@ which means `never expire this | |
595 | * key'. Be careful with `forever' keys. If I were you, I'd | |
596 | * use a more sophisticated key management system than this for | |
597 | * them. | |
d11a0bf7 | 598 | */ |
599 | ||
ef13e9a4 | 600 | int key_new(key_file *f, uint32 id, const char *type, time_t exp, key **kk) |
d11a0bf7 | 601 | { |
602 | key *k = 0; | |
603 | time_t t = time(0); | |
604 | int e = KERR_OK; | |
605 | ||
606 | /* --- Make sure the file is writable --- */ | |
607 | ||
608 | if (!(f->f & KF_WRITE)) | |
609 | e = KERR_READONLY; | |
610 | else if (KEY_EXPIRED(t, exp)) | |
611 | e = KERR_EXPIRED; | |
612 | else if (key_chkident(type)) | |
613 | e = KERR_BADTYPE; | |
614 | else { | |
615 | k = CREATE(key); | |
616 | k->id = id; | |
617 | k->tag = 0; | |
d11a0bf7 | 618 | k->exp = k->del = exp; |
619 | k->c = 0; | |
78ec50fa | 620 | k->type = (char *)type; /* temporarily */ |
ef13e9a4 | 621 | if ((e = insert(f, k)) != 0) |
d11a0bf7 | 622 | DESTROY(k); |
ef13e9a4 | 623 | else { |
624 | k->k = key_newstring(KCAT_SHARE, "<unset>"); | |
625 | k->type = xstrdup(type); | |
2b74e280 | 626 | sym_create(&k->a); |
ef13e9a4 | 627 | *kk = k; |
628 | f->f |= KF_MODIFIED; | |
d11a0bf7 | 629 | } |
630 | } | |
45c0fd36 | 631 | |
ef13e9a4 | 632 | return (e); |
d11a0bf7 | 633 | } |
634 | ||
635 | /*----- That's all, folks -------------------------------------------------*/ |