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
);
120 if (n
) *n
= SYM_NAME(a
);
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_setkeydata@ --- *
184 * Arguments: @key_file *kf@ = pointer to key file
185 * @key *k@ = pointer to key
186 * @key_data *kd@ = new key data
188 * Returns: Zero on success, or a @KERR_@ error code on failure.
190 * Use: Sets the key data for a key.
193 int key_setkeydata(key_file
*kf
, key
*k
, key_data
*kd
)
195 if (!(kf
->f
& KF_WRITE
))
196 return (KERR_READONLY
);
200 kf
->f
|= KF_MODIFIED
;
204 /* --- @key_setcomment@ --- *
206 * Arguments: @key_file *f@ = pointer to key file block
207 * @key *k@ = pointer to key block
208 * @const char *c@ = pointer to comment to set, or zero
210 * Returns: Error code (one of the @KERR@ constants).
212 * Use: Replaces the key's current comment with a new one.
215 int key_setcomment(key_file
*f
, key
*k
, const char *c
)
217 if (!(f
->f
& KF_WRITE
))
218 return (KERR_READONLY
);
219 if (key_chkcomment(c
))
220 return (KERR_BADCOMMENT
);
231 /* --- @key_settag@ --- *
233 * Arguments: @key_file *f@ = pointer to key file block
234 * @key *k@ = pointer to key block
235 * @const char *tag@ = pointer to comment to set, or zero
237 * Returns: Error code (one of the @KERR@ constants).
239 * Use: Replaces the key's current tag with a new one.
242 int key_settag(key_file
*f
, key
*k
, const char *tag
)
247 if (!(f
->f
& KF_WRITE
))
248 return (KERR_READONLY
);
250 /* --- Make sure the tag is OK --- */
252 if (tag
&& key_chkident(tag
))
253 return (KERR_BADTAG
);
255 /* --- See if the new tag is the same as the old one --- */
257 if ((!tag
&& !k
->tag
) ||
258 (tag
&& k
->tag
&& strcmp(tag
, k
->tag
) == 0))
261 /* --- Allocate an entry for the new tag --- */
264 kr
= sym_find(&f
->bytag
, tag
, -1, sizeof(*kr
), &found
);
265 if (found
&& !KEY_EXPIRED(time(0), kr
->k
->del
))
266 return (KERR_DUPTAG
);
270 /* --- Remove any existing tag --- */
273 kr
= sym_find(&f
->bytag
, k
->tag
, -1, 0, 0);
274 assert(((void)"No bytag link", kr
));
275 sym_remove(&f
->bytag
, kr
);
283 k
->tag
= xstrdup(tag
);
289 /* --- @key_fulltag@ --- *
291 * Arguments: @key *k@ = pointer to key
292 * @dstr *d@ = pointer to destination string
296 * Use: Emits the key's full tag, which has the form
297 * `ID:TYPE[:TAG]'. This is used in the textual file format,
298 * and to identify passphrases for locked keys.
301 void key_fulltag(key
*k
, dstr
*d
)
303 dstr_putf(d
, "%08lx:%s", (unsigned long)k
->id
, k
->type
);
305 dstr_putf(d
, ":%s", k
->tag
);
308 /*----- That's all, folks -------------------------------------------------*/