d11a0bf7 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: key-attr.c,v 1.5 2004/04/08 01:36:15 mdw Exp $ |
d11a0bf7 |
4 | * |
5 | * Key attribute manipulation |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
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. |
18 | * |
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. |
23 | * |
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, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
d11a0bf7 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include <ctype.h> |
33 | #include <stdio.h> |
34 | #include <stdlib.h> |
35 | #include <string.h> |
36 | #include <time.h> |
37 | |
052b36d0 |
38 | #include <mLib/dstr.h> |
d11a0bf7 |
39 | #include <mLib/sym.h> |
40 | |
41 | #include "key.h" |
42 | |
43 | /*----- Main code ---------------------------------------------------------*/ |
44 | |
45 | /* --- @key_chkident@ --- * |
46 | * |
47 | * Arguments: @const char *p@ = pointer to a type string |
48 | * |
49 | * Returns: Zero if OK, -1 on error. |
50 | * |
51 | * Use: Checks whether an identification component string is OK. |
52 | */ |
53 | |
54 | int key_chkident(const char *p) |
55 | { |
981bf127 |
56 | if (!p || !*p || strlen(p) > 255) |
d11a0bf7 |
57 | return (-1); |
58 | while (*p) { |
59 | if (*p == ':' || *p == '.' || isspace((unsigned char)*p)) |
60 | return (-1); |
61 | p++; |
62 | } |
63 | return (0); |
64 | } |
65 | |
66 | /* --- @key_chkcomment@ --- * |
67 | * |
68 | * Arguments: @const char *p@ = pointer to a comment string |
69 | * |
70 | * Returns: Zero if OK, -1 on error. |
71 | * |
72 | * Use: Checks whether a comment string is OK. |
73 | */ |
74 | |
75 | int key_chkcomment(const char *p) |
76 | { |
77 | if (!p) |
78 | return (0); |
79 | if (!*p) |
80 | return (-1); |
81 | while (*p) { |
82 | if (*p == '\n') |
83 | return (-1); |
84 | p++; |
85 | } |
86 | return (0); |
87 | } |
88 | |
89 | /* --- @key_mkattriter@ --- * |
90 | * |
91 | * Arguments: @key_attriter *i@ = pointer to attribute iterator |
92 | * @key *k@ = pointer to key |
93 | * |
94 | * Returns: --- |
95 | * |
96 | * Use: Initializes an attribute iterator. The attributes are |
97 | * returned by @key_nextattr@. |
98 | */ |
99 | |
100 | void key_mkattriter(key_attriter *i, key *k) |
101 | { |
102 | sym_mkiter(&i->i, &k->a); |
103 | } |
104 | |
105 | /* --- @key_nextattr@ --- * |
106 | * |
107 | * Arguments: @key_attriter *i@ = pointer to attribute iterator |
108 | * @const char **n, **v@ = pointers to name and value |
109 | * |
110 | * Returns: Zero if no attribute available, or nonzero if returned OK. |
111 | * |
112 | * Use: Returns the next attribute. |
113 | */ |
114 | |
115 | int key_nextattr(key_attriter *i, const char **n, const char **v) |
116 | { |
117 | key_attr *a = sym_next(&i->i); |
118 | if (!a) |
119 | return (0); |
120 | *n = SYM_NAME(a); |
121 | *v = a->p; |
122 | return (1); |
123 | } |
124 | |
125 | /* --- @key_getattr@ --- * |
126 | * |
127 | * Arguments: @key_file *f@ = pointer to file |
128 | * @key *k@ = pointer to key |
129 | * @const char *n@ = pointer to attribute name |
130 | * |
131 | * Returns: Pointer to attribute value, or null if not found. |
132 | * |
133 | * Use: Returns the value of a key attribute. |
134 | */ |
135 | |
136 | const char *key_getattr(key_file *f, key *k, const char *n) |
137 | { |
138 | key_attr *a; |
139 | if ((a = sym_find(&k->a, n, -1, 0, 0)) == 0) |
140 | return (0); |
141 | return (a->p); |
142 | } |
143 | |
144 | /* --- @key_putattr@ --- * |
145 | * |
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 |
150 | * |
151 | * Returns: Error code (one of the @KERR@ constants). |
152 | * |
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. |
156 | */ |
157 | |
158 | int key_putattr(key_file *f, key *k, const char *n, const char *v) |
159 | { |
160 | key_attr *a; |
161 | unsigned found; |
162 | |
163 | if (!(f->f & KF_WRITE)) |
164 | return (KERR_READONLY); |
981bf127 |
165 | if (strlen(n) > 255) |
166 | return (KERR_BADATTR); |
d11a0bf7 |
167 | |
168 | if (v) { |
169 | a = sym_find(&k->a, n, -1, sizeof(*a), &found); |
170 | if (found) |
171 | free(a->p); |
172 | a->p = xstrdup(v); |
173 | } else if ((a = sym_find(&k->a, n, -1, 0, 0)) != 0) { |
174 | free(a->p); |
175 | sym_remove(&k->a, a); |
176 | } |
177 | |
178 | f->f |= KF_MODIFIED; |
179 | return (0); |
180 | } |
181 | |
182 | /* --- @key_setcomment@ --- * |
183 | * |
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 |
187 | * |
188 | * Returns: Error code (one of the @KERR@ constants). |
189 | * |
190 | * Use: Replaces the key's current comment with a new one. |
191 | */ |
192 | |
193 | int key_setcomment(key_file *f, key *k, const char *c) |
194 | { |
195 | if (!(f->f & KF_WRITE)) |
196 | return (KERR_READONLY); |
197 | if (key_chkcomment(c)) |
198 | return (KERR_BADCOMMENT); |
199 | if (k->c) |
200 | free(k->c); |
201 | if (c) |
202 | k->c = xstrdup(c); |
203 | else |
204 | k->c = 0; |
205 | f->f |= KF_MODIFIED; |
206 | return (0); |
207 | } |
208 | |
209 | /* --- @key_settag@ --- * |
210 | * |
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 |
214 | * |
215 | * Returns: Error code (one of the @KERR@ constants). |
216 | * |
217 | * Use: Replaces the key's current tag with a new one. |
218 | */ |
219 | |
220 | int key_settag(key_file *f, key *k, const char *tag) |
221 | { |
222 | key_ref *kr; |
223 | unsigned found; |
224 | |
225 | if (!(f->f & KF_WRITE)) |
226 | return (KERR_READONLY); |
227 | |
228 | /* --- Make sure the tag is OK --- */ |
229 | |
230 | if (tag && key_chkident(tag)) |
231 | return (KERR_BADTAG); |
232 | |
233 | /* --- See if the new tag is the same as the old one --- */ |
234 | |
235 | if ((!tag && !k->tag) || |
236 | (tag && k->tag && strcmp(tag, k->tag) == 0)) |
237 | return (0); |
238 | |
239 | /* --- Allocate an entry for the new tag --- */ |
240 | |
241 | if (tag) { |
242 | kr = sym_find(&f->bytag, tag, -1, sizeof(*kr), &found); |
8c0e800f |
243 | if (found && !KEY_EXPIRED(time(0), kr->k->del)) |
d11a0bf7 |
244 | return (KERR_DUPTAG); |
245 | kr->k = k; |
246 | } |
247 | |
248 | /* --- Remove any existing tag --- */ |
249 | |
250 | if (k->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); |
254 | free(k->tag); |
255 | } |
256 | |
257 | /* --- Done --- */ |
258 | |
259 | f->f |= KF_MODIFIED; |
260 | if (tag) |
261 | k->tag = xstrdup(tag); |
262 | else |
263 | k->tag = 0; |
264 | return (0); |
265 | } |
266 | |
267 | /* --- @key_fulltag@ --- * |
268 | * |
269 | * Arguments: @key *k@ = pointer to key |
270 | * @dstr *d@ = pointer to destination string |
271 | * |
272 | * Returns: --- |
273 | * |
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. |
277 | */ |
278 | |
279 | void key_fulltag(key *k, dstr *d) |
280 | { |
281 | dstr_putf(d, "%08lx:%s", (unsigned long)k->id, k->type); |
282 | if (k->tag) |
283 | dstr_putf(d, ":%s", k->tag); |
284 | } |
285 | |
286 | /*----- That's all, folks -------------------------------------------------*/ |