Debian Codegraph @

lib/hdb/common.c

raw
1 /*
2 * Copyright (c) 1997-2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "hdb_locl.h"
35
36 int
37 hdb_principal2key(krb5_context context, krb5_const_principal p, krb5_data *key)
38 {
39 Principal new;
40 size_t len = 0;
41 int ret;
42
43 ret = copy_Principal(p, &new);
44 if(ret)
45 return ret;
46 new.name.name_type = 0;
47
48 ASN1_MALLOC_ENCODE(Principal, key->data, key->length, &new, &len, ret);
49 if (ret == 0 && key->length != len)
50 krb5_abortx(context, "internal asn.1 encoder error");
51 free_Principal(&new);
52 return ret;
53 }
54
55 int
56 hdb_key2principal(krb5_context context, krb5_data *key, krb5_principal p)
57 {
58 return decode_Principal(key->data, key->length, p, NULL);
59 }
60
61 int
62 hdb_entry2value(krb5_context context, const hdb_entry *ent, krb5_data *value)
63 {
64 size_t len = 0;
65 int ret;
66
67 ASN1_MALLOC_ENCODE(hdb_entry, value->data, value->length, ent, &len, ret);
68 if (ret == 0 && value->length != len)
69 krb5_abortx(context, "internal asn.1 encoder error");
70 return ret;
71 }
72
73 int
74 hdb_value2entry(krb5_context context, krb5_data *value, hdb_entry *ent)
75 {
76 return decode_hdb_entry(value->data, value->length, ent, NULL);
77 }
78
79 int
80 hdb_entry_alias2value(krb5_context context,
81 const hdb_entry_alias *alias,
82 krb5_data *value)
83 {
84 size_t len = 0;
85 int ret;
86
87 ASN1_MALLOC_ENCODE(hdb_entry_alias, value->data, value->length,
88 alias, &len, ret);
89 if (ret == 0 && value->length != len)
90 krb5_abortx(context, "internal asn.1 encoder error");
91 return ret;
92 }
93
94 int
95 hdb_value2entry_alias(krb5_context context, krb5_data *value,
96 hdb_entry_alias *ent)
97 {
98 return decode_hdb_entry_alias(value->data, value->length, ent, NULL);
99 }
100
101 krb5_error_code
102 _hdb_fetch_kvno(krb5_context context, HDB *db, krb5_const_principal principal,
103 unsigned flags, krb5_kvno kvno, hdb_entry_ex *entry)
104 {
105 krb5_principal enterprise_principal = NULL;
106 krb5_data key, value;
107 krb5_error_code ret;
108
109 if (principal->name.name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
110 if (principal->name.name_string.len != 1) {
111 ret = KRB5_PARSE_MALFORMED;
112 krb5_set_error_message(context, ret, "malformed principal: "
113 "enterprise name with %d name components",
114 principal->name.name_string.len);
115 return ret;
116 }
117 ret = krb5_parse_name(context, principal->name.name_string.val[0],
118 &enterprise_principal);
119 if (ret)
120 return ret;
121 principal = enterprise_principal;
122 }
123
124 hdb_principal2key(context, principal, &key);
125 if (enterprise_principal)
126 krb5_free_principal(context, enterprise_principal);
127 ret = db->hdb__get(context, db, key, &value);
128 krb5_data_free(&key);
129 if(ret)
130 return ret;
131 ret = hdb_value2entry(context, &value, &entry->entry);
132 if (ret == ASN1_BAD_ID && (flags & HDB_F_CANON) == 0) {
133 krb5_data_free(&value);
134 return HDB_ERR_NOENTRY;
135 } else if (ret == ASN1_BAD_ID) {
136 hdb_entry_alias alias;
137
138 ret = hdb_value2entry_alias(context, &value, &alias);
139 if (ret) {
140 krb5_data_free(&value);
141 return ret;
142 }
143 hdb_principal2key(context, alias.principal, &key);
144 krb5_data_free(&value);
145 free_hdb_entry_alias(&alias);
146
147 ret = db->hdb__get(context, db, key, &value);
148 krb5_data_free(&key);
149 if (ret)
150 return ret;
151 ret = hdb_value2entry(context, &value, &entry->entry);
152 if (ret) {
153 krb5_data_free(&value);
154 return ret;
155 }
156 }
157 krb5_data_free(&value);
158 if ((flags & HDB_F_DECRYPT) && (flags & HDB_F_ALL_KVNOS)) {
159 /* Decrypt the current keys */
160 ret = hdb_unseal_keys(context, db, &entry->entry);
161 if (ret) {
162 hdb_free_entry(context, entry);
163 return ret;
164 }
165 /* Decrypt the key history too */
166 ret = hdb_unseal_keys_kvno(context, db, 0, flags, &entry->entry);
167 if (ret) {
168 hdb_free_entry(context, entry);
169 return ret;
170 }
171 } else if ((flags & HDB_F_DECRYPT)) {
172 if ((flags & HDB_F_KVNO_SPECIFIED) == 0 || kvno == entry->entry.kvno) {
173 /* Decrypt the current keys */
174 ret = hdb_unseal_keys(context, db, &entry->entry);
175 if (ret) {
176 hdb_free_entry(context, entry);
177 return ret;
178 }
179 } else {
180 if ((flags & HDB_F_ALL_KVNOS))
181 kvno = 0;
182 /*
183 * Find and decrypt the keys from the history that we want,
184 * and swap them with the current keys
185 */
186 ret = hdb_unseal_keys_kvno(context, db, kvno, flags, &entry->entry);
187 if (ret) {
188 hdb_free_entry(context, entry);
189 return ret;
190 }
191 }
192 }
193
194 return 0;
195 }
196
197 static krb5_error_code
198 hdb_remove_aliases(krb5_context context, HDB *db, krb5_data *key)
199 {
200 const HDB_Ext_Aliases *aliases;
201 krb5_error_code code;
202 hdb_entry oldentry;
203 krb5_data value;
204 size_t i;
205
206 code = db->hdb__get(context, db, *key, &value);
207 if (code == HDB_ERR_NOENTRY)
208 return 0;
209 else if (code)
210 return code;
211
212 code = hdb_value2entry(context, &value, &oldentry);
213 krb5_data_free(&value);
214 if (code)
215 return code;
216
217 code = hdb_entry_get_aliases(&oldentry, &aliases);
218 if (code || aliases == NULL) {
219 free_hdb_entry(&oldentry);
220 return code;
221 }
222 for (i = 0; i < aliases->aliases.len; i++) {
223 krb5_data akey;
224
225 code = hdb_principal2key(context, &aliases->aliases.val[i], &akey);
226 if (code == 0) {
227 code = db->hdb__del(context, db, akey);
228 krb5_data_free(&akey);
229 }
230 if (code) {
231 free_hdb_entry(&oldentry);
232 return code;
233 }
234 }
235 free_hdb_entry(&oldentry);
236 return 0;
237 }
238
239 static krb5_error_code
240 hdb_add_aliases(krb5_context context, HDB *db,
241 unsigned flags, hdb_entry_ex *entry)
242 {
243 const HDB_Ext_Aliases *aliases;
244 krb5_error_code code;
245 krb5_data key, value;
246 size_t i;
247
248 code = hdb_entry_get_aliases(&entry->entry, &aliases);
249 if (code || aliases == NULL)
250 return code;
251
252 for (i = 0; i < aliases->aliases.len; i++) {
253 hdb_entry_alias entryalias;
254 entryalias.principal = entry->entry.principal;
255
256 code = hdb_entry_alias2value(context, &entryalias, &value);
257 if (code)
258 return code;
259
260 code = hdb_principal2key(context, &aliases->aliases.val[i], &key);
261 if (code == 0) {
262 code = db->hdb__put(context, db, flags, key, value);
263 krb5_data_free(&key);
264 }
265 krb5_data_free(&value);
266 if (code)
267 return code;
268 }
269 return 0;
270 }
271
272 static krb5_error_code
273 hdb_check_aliases(krb5_context context, HDB *db, hdb_entry_ex *entry)
274 {
275 const HDB_Ext_Aliases *aliases;
276 int code;
277 size_t i;
278
279 /* check if new aliases already is used */
280
281 code = hdb_entry_get_aliases(&entry->entry, &aliases);
282 if (code)
283 return code;
284
285 for (i = 0; aliases && i < aliases->aliases.len; i++) {
286 hdb_entry_alias alias;
287 krb5_data akey, value;
288
289 code = hdb_principal2key(context, &aliases->aliases.val[i], &akey);
290 if (code == 0) {
291 code = db->hdb__get(context, db, akey, &value);
292 krb5_data_free(&akey);
293 }
294 if (code == HDB_ERR_NOENTRY)
295 continue;
296 else if (code)
297 return code;
298
299 code = hdb_value2entry_alias(context, &value, &alias);
300 krb5_data_free(&value);
301
302 if (code == ASN1_BAD_ID)
303 return HDB_ERR_EXISTS;
304 else if (code)
305 return code;
306
307 code = krb5_principal_compare(context, alias.principal,
308 entry->entry.principal);
309 free_hdb_entry_alias(&alias);
310 if (code == 0)
311 return HDB_ERR_EXISTS;
312 }
313 return 0;
314 }
315
316 krb5_error_code
317 _hdb_store(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
318 {
319 krb5_data key, value;
320 int code;
321
322 if (entry->entry.flags.do_not_store)
323 return HDB_ERR_MISUSE;
324 /* check if new aliases already is used */
325 code = hdb_check_aliases(context, db, entry);
326 if (code)
327 return code;
328
329 if ((flags & HDB_F_PRECHECK) && (flags & HDB_F_REPLACE))
330 return 0;
331
332 if ((flags & HDB_F_PRECHECK)) {
333 code = hdb_principal2key(context, entry->entry.principal, &key);
334 if (code)
335 return code;
336 code = db->hdb__get(context, db, key, &value);
337 krb5_data_free(&key);
338 if (code == 0)
339 krb5_data_free(&value);
340 if (code == HDB_ERR_NOENTRY)
341 return 0;
342 return code ? code : HDB_ERR_EXISTS;
343 }
344
345 if(entry->entry.generation == NULL) {
346 struct timeval t;
347 entry->entry.generation = malloc(sizeof(*entry->entry.generation));
348 if(entry->entry.generation == NULL) {
349 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
350 return ENOMEM;
351 }
352 gettimeofday(&t, NULL);
353 entry->entry.generation->time = t.tv_sec;
354 entry->entry.generation->usec = t.tv_usec;
355 entry->entry.generation->gen = 0;
356 } else
357 entry->entry.generation->gen++;
358
359 code = hdb_seal_keys(context, db, &entry->entry);
360 if (code)
361 return code;
362
363 hdb_principal2key(context, entry->entry.principal, &key);
364
365 /* remove aliases */
366 code = hdb_remove_aliases(context, db, &key);
367 if (code) {
368 krb5_data_free(&key);
369 return code;
370 }
371 hdb_entry2value(context, &entry->entry, &value);
372 code = db->hdb__put(context, db, flags & HDB_F_REPLACE, key, value);
373 krb5_data_free(&value);
374 krb5_data_free(&key);
375 if (code)
376 return code;
377
378 code = hdb_add_aliases(context, db, flags, entry);
379
380 return code;
381 }
382
383 krb5_error_code
384 _hdb_remove(krb5_context context, HDB *db,
385 unsigned flags, krb5_const_principal principal)
386 {
387 krb5_data key, value;
388 int code;
389
390 hdb_principal2key(context, principal, &key);
391
392 if ((flags & HDB_F_PRECHECK)) {
393 /*
394 * We don't check that we can delete the aliases because we
395 * assume that the DB is consistent. If we did check for alias
396 * consistency we'd also have to provide a way to fsck the DB,
397 * otherwise admins would have no way to recover -- papering
398 * over this here is less work, but we really ought to provide
399 * an HDB fsck.
400 */
401 code = db->hdb__get(context, db, key, &value);
402 krb5_data_free(&key);
403 if (code == 0) {
404 krb5_data_free(&value);
405 return 0;
406 }
407 return code;
408 }
409
410 code = hdb_remove_aliases(context, db, &key);
411 if (code) {
412 krb5_data_free(&key);
413 return code;
414 }
415 code = db->hdb__del(context, db, key);
416 krb5_data_free(&key);
417 return code;
418 }
419