75b2f1ed95fba5488cd2743525653fae41ac2a4e
3 * $Id: key-attr.c,v 1.4 2004/04/08 01:02:49 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.4 2004/04/08 01:02:49 mdw
36 * Revision 1.3 2001/06/22 19:39:43 mdw
37 * Allow tagging if the tag is owned by a deleted key.
39 * Revision 1.2 2000/02/12 18:21:02 mdw
40 * Overhaul of key management (again).
42 * Revision 1.1 1999/12/22 15:47:48 mdw
43 * Major key-management revision.
47 /*----- Header files ------------------------------------------------------*/
55 #include <mLib/dstr.h>
60 /*----- Main code ---------------------------------------------------------*/
62 /* --- @key_chkident@ --- *
64 * Arguments: @const char *p@ = pointer to a type string
66 * Returns: Zero if OK, -1 on error.
68 * Use: Checks whether an identification component string is OK.
71 int key_chkident(const char *p
)
73 if (!p
|| !*p
|| strlen(p
) > 255)
76 if (*p
== ':' || *p
== '.' || isspace((unsigned char)*p
))
83 /* --- @key_chkcomment@ --- *
85 * Arguments: @const char *p@ = pointer to a comment string
87 * Returns: Zero if OK, -1 on error.
89 * Use: Checks whether a comment string is OK.
92 int key_chkcomment(const char *p
)
106 /* --- @key_mkattriter@ --- *
108 * Arguments: @key_attriter *i@ = pointer to attribute iterator
109 * @key *k@ = pointer to key
113 * Use: Initializes an attribute iterator. The attributes are
114 * returned by @key_nextattr@.
117 void key_mkattriter(key_attriter
*i
, key
*k
)
119 sym_mkiter(&i
->i
, &k
->a
);
122 /* --- @key_nextattr@ --- *
124 * Arguments: @key_attriter *i@ = pointer to attribute iterator
125 * @const char **n, **v@ = pointers to name and value
127 * Returns: Zero if no attribute available, or nonzero if returned OK.
129 * Use: Returns the next attribute.
132 int key_nextattr(key_attriter
*i
, const char **n
, const char **v
)
134 key_attr
*a
= sym_next(&i
->i
);
142 /* --- @key_getattr@ --- *
144 * Arguments: @key_file *f@ = pointer to file
145 * @key *k@ = pointer to key
146 * @const char *n@ = pointer to attribute name
148 * Returns: Pointer to attribute value, or null if not found.
150 * Use: Returns the value of a key attribute.
153 const char *key_getattr(key_file
*f
, key
*k
, const char *n
)
156 if ((a
= sym_find(&k
->a
, n
, -1, 0, 0)) == 0)
161 /* --- @key_putattr@ --- *
163 * Arguments: @key_file *f@ = pointer to file
164 * @key *k@ = pointer to key
165 * @const char *n@ = pointer to attribute name
166 * @const char *v@ = pointer to attribute value or null
168 * Returns: Error code (one of the @KERR@ constants).
170 * Use: Inserts an attribute on a key. If an attribute with the same
171 * name already exists, it is deleted. Setting a null value
172 * removes the attribute.
175 int key_putattr(key_file
*f
, key
*k
, const char *n
, const char *v
)
180 if (!(f
->f
& KF_WRITE
))
181 return (KERR_READONLY
);
183 return (KERR_BADATTR
);
186 a
= sym_find(&k
->a
, n
, -1, sizeof(*a
), &found
);
190 } else if ((a
= sym_find(&k
->a
, n
, -1, 0, 0)) != 0) {
192 sym_remove(&k
->a
, a
);
199 /* --- @key_setcomment@ --- *
201 * Arguments: @key_file *f@ = pointer to key file block
202 * @key *k@ = pointer to key block
203 * @const char *c@ = pointer to comment to set, or zero
205 * Returns: Error code (one of the @KERR@ constants).
207 * Use: Replaces the key's current comment with a new one.
210 int key_setcomment(key_file
*f
, key
*k
, const char *c
)
212 if (!(f
->f
& KF_WRITE
))
213 return (KERR_READONLY
);
214 if (key_chkcomment(c
))
215 return (KERR_BADCOMMENT
);
226 /* --- @key_settag@ --- *
228 * Arguments: @key_file *f@ = pointer to key file block
229 * @key *k@ = pointer to key block
230 * @const char *tag@ = pointer to comment to set, or zero
232 * Returns: Error code (one of the @KERR@ constants).
234 * Use: Replaces the key's current tag with a new one.
237 int key_settag(key_file
*f
, key
*k
, const char *tag
)
242 if (!(f
->f
& KF_WRITE
))
243 return (KERR_READONLY
);
245 /* --- Make sure the tag is OK --- */
247 if (tag
&& key_chkident(tag
))
248 return (KERR_BADTAG
);
250 /* --- See if the new tag is the same as the old one --- */
252 if ((!tag
&& !k
->tag
) ||
253 (tag
&& k
->tag
&& strcmp(tag
, k
->tag
) == 0))
256 /* --- Allocate an entry for the new tag --- */
259 kr
= sym_find(&f
->bytag
, tag
, -1, sizeof(*kr
), &found
);
260 if (found
&& !KEY_EXPIRED(time(0), kr
->k
->del
))
261 return (KERR_DUPTAG
);
265 /* --- Remove any existing tag --- */
268 kr
= sym_find(&f
->bytag
, k
->tag
, -1, 0, 0);
269 assert(((void)"No bytag link", kr
));
270 sym_remove(&f
->bytag
, kr
);
278 k
->tag
= xstrdup(tag
);
284 /* --- @key_fulltag@ --- *
286 * Arguments: @key *k@ = pointer to key
287 * @dstr *d@ = pointer to destination string
291 * Use: Emits the key's full tag, which has the form
292 * `ID:TYPE[:TAG]'. This is used in the textual file format,
293 * and to identify passphrases for locked keys.
296 void key_fulltag(key
*k
, dstr
*d
)
298 dstr_putf(d
, "%08lx:%s", (unsigned long)k
->id
, k
->type
);
300 dstr_putf(d
, ":%s", k
->tag
);
303 /*----- That's all, folks -------------------------------------------------*/