3 * $Id: key-attr.c,v 1.2 2000/02/12 18:21:02 mdw Exp $
5 * Key attribute manipulation
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 --------------------------------------------------*
32 * $Log: key-attr.c,v $
33 * Revision 1.2 2000/02/12 18:21:02 mdw
34 * Overhaul of key management (again).
36 * Revision 1.1 1999/12/22 15:47:48 mdw
37 * Major key-management revision.
41 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/dstr.h>
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @key_chkident@ --- *
58 * Arguments: @const char *p@ = pointer to a type string
60 * Returns: Zero if OK, -1 on error.
62 * Use: Checks whether an identification component string is OK.
65 int key_chkident(const char *p)
70 if (*p == ':' || *p == '.' || isspace((unsigned char)*p))
77 /* --- @key_chkcomment@ --- *
79 * Arguments: @const char *p@ = pointer to a comment string
81 * Returns: Zero if OK, -1 on error.
83 * Use: Checks whether a comment string is OK.
86 int key_chkcomment(const char *p)
100 /* --- @key_mkattriter@ --- *
102 * Arguments: @key_attriter *i@ = pointer to attribute iterator
103 * @key *k@ = pointer to key
107 * Use: Initializes an attribute iterator. The attributes are
108 * returned by @key_nextattr@.
111 void key_mkattriter(key_attriter *i, key *k)
113 sym_mkiter(&i->i, &k->a);
116 /* --- @key_nextattr@ --- *
118 * Arguments: @key_attriter *i@ = pointer to attribute iterator
119 * @const char **n, **v@ = pointers to name and value
121 * Returns: Zero if no attribute available, or nonzero if returned OK.
123 * Use: Returns the next attribute.
126 int key_nextattr(key_attriter *i, const char **n, const char **v)
128 key_attr *a = sym_next(&i->i);
136 /* --- @key_getattr@ --- *
138 * Arguments: @key_file *f@ = pointer to file
139 * @key *k@ = pointer to key
140 * @const char *n@ = pointer to attribute name
142 * Returns: Pointer to attribute value, or null if not found.
144 * Use: Returns the value of a key attribute.
147 const char *key_getattr(key_file *f, key *k, const char *n)
150 if ((a = sym_find(&k->a, n, -1, 0, 0)) == 0)
155 /* --- @key_putattr@ --- *
157 * Arguments: @key_file *f@ = pointer to file
158 * @key *k@ = pointer to key
159 * @const char *n@ = pointer to attribute name
160 * @const char *v@ = pointer to attribute value or null
162 * Returns: Error code (one of the @KERR@ constants).
164 * Use: Inserts an attribute on a key. If an attribute with the same
165 * name already exists, it is deleted. Setting a null value
166 * removes the attribute.
169 int key_putattr(key_file *f, key *k, const char *n, const char *v)
174 if (!(f->f & KF_WRITE))
175 return (KERR_READONLY);
178 a = sym_find(&k->a, n, -1, sizeof(*a), &found);
182 } else if ((a = sym_find(&k->a, n, -1, 0, 0)) != 0) {
184 sym_remove(&k->a, a);
191 /* --- @key_setcomment@ --- *
193 * Arguments: @key_file *f@ = pointer to key file block
194 * @key *k@ = pointer to key block
195 * @const char *c@ = pointer to comment to set, or zero
197 * Returns: Error code (one of the @KERR@ constants).
199 * Use: Replaces the key's current comment with a new one.
202 int key_setcomment(key_file *f, key *k, const char *c)
204 if (!(f->f & KF_WRITE))
205 return (KERR_READONLY);
206 if (key_chkcomment(c))
207 return (KERR_BADCOMMENT);
218 /* --- @key_settag@ --- *
220 * Arguments: @key_file *f@ = pointer to key file block
221 * @key *k@ = pointer to key block
222 * @const char *tag@ = pointer to comment to set, or zero
224 * Returns: Error code (one of the @KERR@ constants).
226 * Use: Replaces the key's current tag with a new one.
229 int key_settag(key_file *f, key *k, const char *tag)
234 if (!(f->f & KF_WRITE))
235 return (KERR_READONLY);
237 /* --- Make sure the tag is OK --- */
239 if (tag && key_chkident(tag))
240 return (KERR_BADTAG);
242 /* --- See if the new tag is the same as the old one --- */
244 if ((!tag && !k->tag) ||
245 (tag && k->tag && strcmp(tag, k->tag) == 0))
248 /* --- Allocate an entry for the new tag --- */
251 kr = sym_find(&f->bytag, tag, -1, sizeof(*kr), &found);
253 return (KERR_DUPTAG);
257 /* --- Remove any existing tag --- */
260 kr = sym_find(&f->bytag, k->tag, -1, 0, 0);
261 assert(((void)"No bytag link", kr));
262 sym_remove(&f->bytag, kr);
270 k->tag = xstrdup(tag);
276 /* --- @key_fulltag@ --- *
278 * Arguments: @key *k@ = pointer to key
279 * @dstr *d@ = pointer to destination string
283 * Use: Emits the key's full tag, which has the form
284 * `ID:TYPE[:TAG]'. This is used in the textual file format,
285 * and to identify passphrases for locked keys.
288 void key_fulltag(key *k, dstr *d)
290 dstr_putf(d, "%08lx:%s", (unsigned long)k->id, k->type);
292 dstr_putf(d, ":%s", k->tag);
295 /*----- That's all, folks -------------------------------------------------*/