3 * $Id: key-attr.c,v 1.1 1999/12/22 15:47:48 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.1 1999/12/22 15:47:48 mdw
34 * Major key-management revision.
38 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/alloc.h>
51 /*----- Main code ---------------------------------------------------------*/
53 /* --- @key_chkident@ --- *
55 * Arguments: @const char *p@ = pointer to a type string
57 * Returns: Zero if OK, -1 on error.
59 * Use: Checks whether an identification component string is OK.
62 int key_chkident(const char *p
)
67 if (*p
== ':' || *p
== '.' || isspace((unsigned char)*p
))
74 /* --- @key_chkcomment@ --- *
76 * Arguments: @const char *p@ = pointer to a comment string
78 * Returns: Zero if OK, -1 on error.
80 * Use: Checks whether a comment string is OK.
83 int key_chkcomment(const char *p
)
97 /* --- @key_mkattriter@ --- *
99 * Arguments: @key_attriter *i@ = pointer to attribute iterator
100 * @key *k@ = pointer to key
104 * Use: Initializes an attribute iterator. The attributes are
105 * returned by @key_nextattr@.
108 void key_mkattriter(key_attriter
*i
, key
*k
)
110 sym_mkiter(&i
->i
, &k
->a
);
113 /* --- @key_nextattr@ --- *
115 * Arguments: @key_attriter *i@ = pointer to attribute iterator
116 * @const char **n, **v@ = pointers to name and value
118 * Returns: Zero if no attribute available, or nonzero if returned OK.
120 * Use: Returns the next attribute.
123 int key_nextattr(key_attriter
*i
, const char **n
, const char **v
)
125 key_attr
*a
= sym_next(&i
->i
);
133 /* --- @key_getattr@ --- *
135 * Arguments: @key_file *f@ = pointer to file
136 * @key *k@ = pointer to key
137 * @const char *n@ = pointer to attribute name
139 * Returns: Pointer to attribute value, or null if not found.
141 * Use: Returns the value of a key attribute.
144 const char *key_getattr(key_file
*f
, key
*k
, const char *n
)
147 if ((a
= sym_find(&k
->a
, n
, -1, 0, 0)) == 0)
152 /* --- @key_putattr@ --- *
154 * Arguments: @key_file *f@ = pointer to file
155 * @key *k@ = pointer to key
156 * @const char *n@ = pointer to attribute name
157 * @const char *v@ = pointer to attribute value or null
159 * Returns: Error code (one of the @KERR@ constants).
161 * Use: Inserts an attribute on a key. If an attribute with the same
162 * name already exists, it is deleted. Setting a null value
163 * removes the attribute.
166 int key_putattr(key_file
*f
, key
*k
, const char *n
, const char *v
)
171 if (!(f
->f
& KF_WRITE
))
172 return (KERR_READONLY
);
175 a
= sym_find(&k
->a
, n
, -1, sizeof(*a
), &found
);
179 } else if ((a
= sym_find(&k
->a
, n
, -1, 0, 0)) != 0) {
181 sym_remove(&k
->a
, a
);
188 /* --- @key_setcomment@ --- *
190 * Arguments: @key_file *f@ = pointer to key file block
191 * @key *k@ = pointer to key block
192 * @const char *c@ = pointer to comment to set, or zero
194 * Returns: Error code (one of the @KERR@ constants).
196 * Use: Replaces the key's current comment with a new one.
199 int key_setcomment(key_file
*f
, key
*k
, const char *c
)
201 if (!(f
->f
& KF_WRITE
))
202 return (KERR_READONLY
);
203 if (key_chkcomment(c
))
204 return (KERR_BADCOMMENT
);
215 /* --- @key_settag@ --- *
217 * Arguments: @key_file *f@ = pointer to key file block
218 * @key *k@ = pointer to key block
219 * @const char *tag@ = pointer to comment to set, or zero
221 * Returns: Error code (one of the @KERR@ constants).
223 * Use: Replaces the key's current tag with a new one.
226 int key_settag(key_file
*f
, key
*k
, const char *tag
)
231 if (!(f
->f
& KF_WRITE
))
232 return (KERR_READONLY
);
234 /* --- Make sure the tag is OK --- */
236 if (tag
&& key_chkident(tag
))
237 return (KERR_BADTAG
);
239 /* --- See if the new tag is the same as the old one --- */
241 if ((!tag
&& !k
->tag
) ||
242 (tag
&& k
->tag
&& strcmp(tag
, k
->tag
) == 0))
245 /* --- Allocate an entry for the new tag --- */
248 kr
= sym_find(&f
->bytag
, tag
, -1, sizeof(*kr
), &found
);
250 return (KERR_DUPTAG
);
254 /* --- Remove any existing tag --- */
257 kr
= sym_find(&f
->bytag
, k
->tag
, -1, 0, 0);
258 assert(((void)"No bytag link", kr
));
259 sym_remove(&f
->bytag
, kr
);
267 k
->tag
= xstrdup(tag
);
273 /* --- @key_fulltag@ --- *
275 * Arguments: @key *k@ = pointer to key
276 * @dstr *d@ = pointer to destination string
280 * Use: Emits the key's full tag, which has the form
281 * `ID:TYPE[:TAG]'. This is used in the textual file format,
282 * and to identify passphrases for locked keys.
285 void key_fulltag(key
*k
, dstr
*d
)
287 dstr_putf(d
, "%08lx:%s", (unsigned long)k
->id
, k
->type
);
289 dstr_putf(d
, ":%s", k
->tag
);
292 /*----- That's all, folks -------------------------------------------------*/