Search for primitive elements using prime-search equipment.
[u/mdw/catacomb] / key-misc.c
CommitLineData
d11a0bf7 1/* -*-c-*-
2 *
3 * $Id: key-misc.c,v 1.1 1999/12/22 15:47:48 mdw Exp $
4 *
5 * Simple key management
6 *
7 * (c) 1999 Straylight/Edgeware
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
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: key-misc.c,v $
33 * Revision 1.1 1999/12/22 15:47:48 mdw
34 * Major key-management revision.
35 *
36 */
37
38/*----- Header files ------------------------------------------------------*/
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <time.h>
44
45#include <mLib/alloc.h>
46#include <mLib/bits.h>
47#include <mLib/hash.h>
48#include <mLib/sub.h>
49#include <mLib/sym.h>
50
51#include "key.h"
52
53/*----- Useful macros -----------------------------------------------------*/
54
55#define KEY_WRITE(f) do { \
56 if (!(f)->f & KF_WRITE) \
57 return (KERR_READONLY); \
58 } while (0)
59
60#define KEY_MODIFY(f) do { (f)->f |= KF_MODIFIED; } while (0)
61
62#define KEY_LOAD(n) ((n) * 2)
63
64/*----- Error reporting ---------------------------------------------------*/
65
66/* --- @key_strerror@ --- *
67 *
68 * Arguments: @int err@ = error code from @key_new@
69 *
70 * Returns: Pointer to error string.
71 *
72 * Use: Translates a @KERR@ error code into a human-readable
73 * string.
74 */
75
76const char *key_strerror(int err)
77{
78 char *tab[] = {
79 "No error",
80 "Bad tag string",
81 "Bad type string",
82 "Bad comment string",
83 "Keyid already exists",
84 "Key tag already exists",
85 "Key file is read-only",
86 "Key will eventually expire",
87 "Bad key flags string",
88 "Unknown error code"
89 };
90
91 unsigned e = -err;
92 if (e >= KERR_MAX)
93 e = KERR_MAX;
94 return (tab[e]);
95}
96
97/*----- Iteration and iterators -------------------------------------------*/
98
99/* --- @key_mkiter@ --- *
100 *
101 * Arguments: @key_iter *i@ = pointer to iterator object
102 * @key_file *f@ = pointer to file structure
103 *
104 * Returns: ---
105 *
106 * Use: Initializes a key iterator. The keys are returned by
107 * @key_next@.
108 */
109
110void key_mkiter(key_iter *i, key_file *f)
111{
112 HASH_MKITER(&i->i, &f->byid);
113 i->t = time(0);
114}
115
116/* --- @key_next@ --- *
117 *
118 * Arguments: @key_iter *i@ = pointer to iterator object
119 *
120 * Returns: Pointer to next key, or null.
121 *
122 * Use: Returns the next key in some arbitrary sequence.
123 */
124
125key *key_next(key_iter *i)
126{
127 hash_base *b;
128 key *k;
129 do {
130 HASH_NEXT(&i->i, b);
131 k = (key *)b;
132 } while (k && KEY_EXPIRED(i->t, k->exp) && KEY_EXPIRED(i->t, k->del));
133 return (k);
134}
135
136/*----- Lookup ------------------------------------------------------------*/
137
138/* --- @key_bytype@ --- *
139 *
140 * Arguments: @key_file *f@ = key file we want a key from
141 * @const char *type@ = type string for desired key
142 *
143 * Returns: Pointer to the best key to use, or null.
144 *
145 * Use: Looks up a key by its type. Returns the key with the latest
146 * expiry time. This function will not return an expired key.
147 */
148
149key *key_bytype(key_file *f, const char *type)
150{
151 time_t now = time(0);
152 key *k;
153 key_ref *kr;
154
155 if ((kr = sym_find(&f->bytype, type, -1, 0, 0)) == 0)
156 return (0);
157 for (k = kr->k; k && KEY_EXPIRED(now, k->exp); k = k->next)
158 ;
159 return (k);
160}
161
162/* --- @key_byid@ --- *
163 *
164 * Arguments: @key_file *f@ = key file to find a key from
165 * @uint32 id@ = id to look for
166 *
167 * Returns: Key with matching id.
168 *
169 * Use: Returns a key given its id. This function will return an
170 * expired key, but not a deleted one.
171 */
172
173key *key_byid(key_file *f, uint32 id)
174{
175 time_t t = time(0);
176 hash_base **bin, *b;
177
178 bin = HASH_BIN(&f->byid, id);
179 for (b = *bin; b; b = b->next) {
180 if (b->hash == id) {
181 key *k = (key *)b;
182 if (KEY_EXPIRED(t, k->exp) && KEY_EXPIRED(t, k->del))
183 return (0);
184 return (k);
185 }
186 }
187 return (0);
188}
189
190/* --- @key_bytag@ --- *
191 *
192 * Arguments: @key_file *f@ = key file to find a key from
193 * @const char *tag@ = pointer to tag string
194 *
195 * Returns: Key with matching id or tag.
196 *
197 * Use: Returns a key given its tag or id. This function will return
198 * an expired key, but not a deleted one.
199 */
200
201key *key_bytag(key_file *f, const char *tag)
202{
203 time_t t = time(0);
204 char *p;
205 uint32 id;
206 key_ref *kr = sym_find(&f->bytag, tag, -1, 0, 0);
207
208 if (kr && !(KEY_EXPIRED(t, kr->k->exp) && KEY_EXPIRED(t, kr->k->exp)))
209 return (kr->k);
210 id = strtoul(tag, &p, 16);
211 if (!*p)
212 return (key_byid(f, id));
213 return (key_bytype(f, tag));
214}
215
216/* --- @key_qtag@ --- *
217 *
218 * Arguments: @key_file *f@ = key file to find a key from
219 * @const char *tag@ = pointer to tag string
220 * @dstr *d@ = pointer to string for full tag name
221 * @key **k@ = where to store the key pointer
222 * @key_data **kd@ = where to store the key data pointer
223 *
224 * Returns: Zero if OK, nonzero if it failed.
225 *
226 * Use: Performs a full lookup on a qualified tag name. The tag is
227 * qualified by the names of subkeys, separated by dots. Hence,
228 * a qualified tag is ID|TAG[.TAG...]. The various result
229 * pointers can be null to indicate that the result isn't
230 * interesting.
231 */
232
233int key_qtag(key_file *f, const char *tag, dstr *d, key **k, key_data **kd)
234{
235 dstr dd = DSTR_INIT;
236 const char *q;
237 key *kk;
238 key_data *kkd;
239
240 /* --- Find the end of the base tag --- */
241
242 if ((q = strchr(tag, '.')) == 0)
243 DPUTS(&dd, tag);
244 else {
245 DPUTM(&dd, tag, q - tag);
246 DPUTZ(&dd);
247 q++;
248 }
249
250 /* --- Look up the key tag --- */
251
252 if ((kk = key_bytag(f, dd.buf)) == 0) {
253 dstr_destroy(&dd);
254 return (-1);
255 }
256
257 /* --- Set the various initial bits of result up --- */
258
259 if (d)
260 key_fulltag(kk, d);
261 if (k)
262 *k = kk;
263 kkd = &kk->k;
264
265 /* --- Now dig through the rest of the tag --- */
266
267 while (q && *q) {
268
269 /* --- Stick on the next bit of the fullqtag --- */
270
271 DRESET(&dd);
272 while (*q && *q != '.') {
273 DPUTC(&dd, *q);
274 q++;
275 }
276 DPUTZ(&dd);
277 if (d) {
278 DPUTC(d, '.');
279 DPUTD(d, &dd);
280 }
281
282 /* --- Look up the subkey --- */
283
284 if (kkd->e != KENC_STRUCT) {
285 kkd = 0;
286 break;
287 }
288 if ((kkd = key_structfind(kkd, dd.buf)) == 0)
289 break;
290 }
291
292 /* --- Return the results --- */
293
294 if (!kkd)
295 return (-1);
296 dstr_destroy(&dd);
297 if (kd)
298 *kd = kkd;
299 return (0);
300}
301
302/*----- Miscellaneous functions -------------------------------------------*/
303
304/* --- @key_delete@ --- *
305 *
306 * Arguments: @key_file *f@ = pointer to file block
307 * @key *k@ = key to delete
308 *
309 * Returns: Error code (one of the @KERR@ constants).
310 *
311 * Use: Removes the given key from the list. The key file must be
312 * writable. (Due to the horridness of the data structures,
313 * deleted keys aren't actually removed, just marked so that
314 * they can't be looked up or iterated over. One upshot of
315 * this is that they don't get written back to the file when
316 * it's closed.)
317 */
318
319int key_delete(key_file *f, key *k)
320{
321 KEY_WRITE(f);
322 k->exp = KEXP_EXPIRE;
323 k->del = KEXP_EXPIRE;
324 KEY_MODIFY(f);
325 return (0);
326}
327
328/* --- @key_expire@ --- *
329 *
330 * Arguments: @key_file *f@ = pointer to file block
331 * @key *k@ = pointer to key block
332 *
333 * Returns: Error code (one of the @KERR@ constants).
334 *
335 * Use: Immediately marks the key as expired. It may be removed
336 * immediately, if it is no longer required, and will be removed
337 * by a tidy operation when it is no longer required. The key
338 * file must be writable.
339 */
340
341int key_expire(key_file *f, key *k)
342{
343 KEY_WRITE(f);
344 k->exp = KEXP_EXPIRE;
345 if (k->del == KEXP_FOREVER)
346 k->del = KEXP_EXPIRE;
347 KEY_MODIFY(f);
348 return (0);
349}
350
351/* --- @key_used@ --- *
352 *
353 * Arguments: @key_file *f@ = pointer to key file
354 * @key *k@ = pointer to key block
355 * @time_t t@ = when key can be removed
356 *
357 * Returns: Zero if OK, nonzero on failure.
358 *
359 * Use: Marks a key as being required until a given time. Even
360 * though the key may expire before then (and won't be returned
361 * by type after that time), it will still be available when
362 * requested explicitly by id. The key file must be writable.
363 *
364 * The only (current) reason for failure is attempting to use
365 * a key which can expire for something which can't.
366 */
367
368int key_used(key_file *f, key *k, time_t t)
369{
370 KEY_WRITE(f);
371 if (t == KEXP_FOREVER) {
372 if (k->exp != KEXP_FOREVER)
373 return (KERR_WILLEXPIRE);
374 } else if (k->del >= t)
375 return (0);
376
377 k->del = t;
378 KEY_MODIFY(f);
379 return (0);
380}
381
382/*----- That's all, folks -------------------------------------------------*/