3 * $Id: key-attr.c,v 1.5 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
38 #include <mLib/dstr.h>
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @key_chkident@ --- *
47 * Arguments: @const char *p@ = pointer to a type string
49 * Returns: Zero if OK, -1 on error.
51 * Use: Checks whether an identification component string is OK.
54 int key_chkident(const char *p
)
56 if (!p
|| !*p
|| strlen(p
) > 255)
59 if (*p
== ':' || *p
== '.' || isspace((unsigned char)*p
))
66 /* --- @key_chkcomment@ --- *
68 * Arguments: @const char *p@ = pointer to a comment string
70 * Returns: Zero if OK, -1 on error.
72 * Use: Checks whether a comment string is OK.
75 int key_chkcomment(const char *p
)
89 /* --- @key_mkattriter@ --- *
91 * Arguments: @key_attriter *i@ = pointer to attribute iterator
92 * @key *k@ = pointer to key
96 * Use: Initializes an attribute iterator. The attributes are
97 * returned by @key_nextattr@.
100 void key_mkattriter(key_attriter
*i
, key
*k
)
102 sym_mkiter(&i
->i
, &k
->a
);
105 /* --- @key_nextattr@ --- *
107 * Arguments: @key_attriter *i@ = pointer to attribute iterator
108 * @const char **n, **v@ = pointers to name and value
110 * Returns: Zero if no attribute available, or nonzero if returned OK.
112 * Use: Returns the next attribute.
115 int key_nextattr(key_attriter
*i
, const char **n
, const char **v
)
117 key_attr
*a
= sym_next(&i
->i
);
125 /* --- @key_getattr@ --- *
127 * Arguments: @key_file *f@ = pointer to file
128 * @key *k@ = pointer to key
129 * @const char *n@ = pointer to attribute name
131 * Returns: Pointer to attribute value, or null if not found.
133 * Use: Returns the value of a key attribute.
136 const char *key_getattr(key_file
*f
, key
*k
, const char *n
)
139 if ((a
= sym_find(&k
->a
, n
, -1, 0, 0)) == 0)
144 /* --- @key_putattr@ --- *
146 * Arguments: @key_file *f@ = pointer to file
147 * @key *k@ = pointer to key
148 * @const char *n@ = pointer to attribute name
149 * @const char *v@ = pointer to attribute value or null
151 * Returns: Error code (one of the @KERR@ constants).
153 * Use: Inserts an attribute on a key. If an attribute with the same
154 * name already exists, it is deleted. Setting a null value
155 * removes the attribute.
158 int key_putattr(key_file
*f
, key
*k
, const char *n
, const char *v
)
163 if (!(f
->f
& KF_WRITE
))
164 return (KERR_READONLY
);
166 return (KERR_BADATTR
);
169 a
= sym_find(&k
->a
, n
, -1, sizeof(*a
), &found
);
173 } else if ((a
= sym_find(&k
->a
, n
, -1, 0, 0)) != 0) {
175 sym_remove(&k
->a
, a
);
182 /* --- @key_setcomment@ --- *
184 * Arguments: @key_file *f@ = pointer to key file block
185 * @key *k@ = pointer to key block
186 * @const char *c@ = pointer to comment to set, or zero
188 * Returns: Error code (one of the @KERR@ constants).
190 * Use: Replaces the key's current comment with a new one.
193 int key_setcomment(key_file
*f
, key
*k
, const char *c
)
195 if (!(f
->f
& KF_WRITE
))
196 return (KERR_READONLY
);
197 if (key_chkcomment(c
))
198 return (KERR_BADCOMMENT
);
209 /* --- @key_settag@ --- *
211 * Arguments: @key_file *f@ = pointer to key file block
212 * @key *k@ = pointer to key block
213 * @const char *tag@ = pointer to comment to set, or zero
215 * Returns: Error code (one of the @KERR@ constants).
217 * Use: Replaces the key's current tag with a new one.
220 int key_settag(key_file
*f
, key
*k
, const char *tag
)
225 if (!(f
->f
& KF_WRITE
))
226 return (KERR_READONLY
);
228 /* --- Make sure the tag is OK --- */
230 if (tag
&& key_chkident(tag
))
231 return (KERR_BADTAG
);
233 /* --- See if the new tag is the same as the old one --- */
235 if ((!tag
&& !k
->tag
) ||
236 (tag
&& k
->tag
&& strcmp(tag
, k
->tag
) == 0))
239 /* --- Allocate an entry for the new tag --- */
242 kr
= sym_find(&f
->bytag
, tag
, -1, sizeof(*kr
), &found
);
243 if (found
&& !KEY_EXPIRED(time(0), kr
->k
->del
))
244 return (KERR_DUPTAG
);
248 /* --- Remove any existing tag --- */
251 kr
= sym_find(&f
->bytag
, k
->tag
, -1, 0, 0);
252 assert(((void)"No bytag link", kr
));
253 sym_remove(&f
->bytag
, kr
);
261 k
->tag
= xstrdup(tag
);
267 /* --- @key_fulltag@ --- *
269 * Arguments: @key *k@ = pointer to key
270 * @dstr *d@ = pointer to destination string
274 * Use: Emits the key's full tag, which has the form
275 * `ID:TYPE[:TAG]'. This is used in the textual file format,
276 * and to identify passphrases for locked keys.
279 void key_fulltag(key
*k
, dstr
*d
)
281 dstr_putf(d
, "%08lx:%s", (unsigned long)k
->id
, k
->type
);
283 dstr_putf(d
, ":%s", k
->tag
);
286 /*----- That's all, folks -------------------------------------------------*/