| 1 |
/* |
| 2 |
* Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Portions Copyright (c) 2009 Apple Inc. All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions |
| 10 |
* are met: |
| 11 |
* |
| 12 |
* 1. Redistributions of source code must retain the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer. |
| 14 |
* |
| 15 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 16 |
* notice, this list of conditions and the following disclaimer in the |
| 17 |
* documentation and/or other materials provided with the distribution. |
| 18 |
* |
| 19 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 20 |
* may be used to endorse or promote products derived from this software |
| 21 |
* without specific prior written permission. |
| 22 |
* |
| 23 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 24 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 27 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 29 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 32 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 |
* SUCH DAMAGE. |
| 34 |
*/ |
| 35 |
|
| 36 |
#define KRB5_KDB_DISALLOW_POSTDATED 0x00000001 |
| 37 |
#define KRB5_KDB_DISALLOW_FORWARDABLE 0x00000002 |
| 38 |
#define KRB5_KDB_DISALLOW_TGT_BASED 0x00000004 |
| 39 |
#define KRB5_KDB_DISALLOW_RENEWABLE 0x00000008 |
| 40 |
#define KRB5_KDB_DISALLOW_PROXIABLE 0x00000010 |
| 41 |
#define KRB5_KDB_DISALLOW_DUP_SKEY 0x00000020 |
| 42 |
#define KRB5_KDB_DISALLOW_ALL_TIX 0x00000040 |
| 43 |
#define KRB5_KDB_REQUIRES_PRE_AUTH 0x00000080 |
| 44 |
#define KRB5_KDB_REQUIRES_HW_AUTH 0x00000100 |
| 45 |
#define KRB5_KDB_REQUIRES_PWCHANGE 0x00000200 |
| 46 |
#define KRB5_KDB_DISALLOW_SVR 0x00001000 |
| 47 |
#define KRB5_KDB_PWCHANGE_SERVICE 0x00002000 |
| 48 |
#define KRB5_KDB_SUPPORT_DESMD5 0x00004000 |
| 49 |
#define KRB5_KDB_NEW_PRINC 0x00008000 |
| 50 |
|
| 51 |
/* |
| 52 |
|
| 53 |
key: krb5_unparse_name + NUL |
| 54 |
|
| 55 |
16: baselength |
| 56 |
32: attributes |
| 57 |
32: max time |
| 58 |
32: max renewable time |
| 59 |
32: client expire |
| 60 |
32: passwd expire |
| 61 |
32: last successful passwd |
| 62 |
32: last failed attempt |
| 63 |
32: num of failed attempts |
| 64 |
16: num tl data |
| 65 |
16: num data data |
| 66 |
16: principal length |
| 67 |
length: principal |
| 68 |
for num tl data times |
| 69 |
16: tl data type |
| 70 |
16: tl data length |
| 71 |
length: length |
| 72 |
for num key data times |
| 73 |
16: version (num keyblocks) |
| 74 |
16: kvno |
| 75 |
for version times: |
| 76 |
16: type |
| 77 |
16: length |
| 78 |
length: keydata |
| 79 |
|
| 80 |
|
| 81 |
key_data_contents[0] |
| 82 |
|
| 83 |
int16: length |
| 84 |
read-of-data: key-encrypted, key-usage 0, master-key |
| 85 |
|
| 86 |
salt: |
| 87 |
version2 = salt in key_data->key_data_contents[1] |
| 88 |
else default salt. |
| 89 |
|
| 90 |
*/ |
| 91 |
|
| 92 |
#include "hdb_locl.h" |
| 93 |
|
| 94 |
typedef struct MITDB { |
| 95 |
HDB db; /* Generic */ |
| 96 |
int do_sync; /* MITDB-specific */ |
| 97 |
} MITDB; |
| 98 |
|
| 99 |
static void |
| 100 |
attr_to_flags(unsigned attr, HDBFlags *flags) |
| 101 |
{ |
| 102 |
flags->postdate = !(attr & KRB5_KDB_DISALLOW_POSTDATED); |
| 103 |
flags->forwardable = !(attr & KRB5_KDB_DISALLOW_FORWARDABLE); |
| 104 |
flags->initial = !!(attr & KRB5_KDB_DISALLOW_TGT_BASED); |
| 105 |
flags->renewable = !(attr & KRB5_KDB_DISALLOW_RENEWABLE); |
| 106 |
flags->proxiable = !(attr & KRB5_KDB_DISALLOW_PROXIABLE); |
| 107 |
/* DUP_SKEY */ |
| 108 |
flags->invalid = !!(attr & KRB5_KDB_DISALLOW_ALL_TIX); |
| 109 |
flags->require_preauth = !!(attr & KRB5_KDB_REQUIRES_PRE_AUTH); |
| 110 |
flags->require_hwauth = !!(attr & KRB5_KDB_REQUIRES_HW_AUTH); |
| 111 |
flags->server = !(attr & KRB5_KDB_DISALLOW_SVR); |
| 112 |
flags->change_pw = !!(attr & KRB5_KDB_PWCHANGE_SERVICE); |
| 113 |
flags->client = 1; /* XXX */ |
| 114 |
} |
| 115 |
|
| 116 |
#define KDB_V1_BASE_LENGTH 38 |
| 117 |
|
| 118 |
#define CHECK(x) do { if ((x)) goto out; } while(0) |
| 119 |
|
| 120 |
#ifdef HAVE_DB1 |
| 121 |
static krb5_error_code |
| 122 |
mdb_principal2key(krb5_context context, |
| 123 |
krb5_const_principal principal, |
| 124 |
krb5_data *key) |
| 125 |
{ |
| 126 |
krb5_error_code ret; |
| 127 |
char *str; |
| 128 |
|
| 129 |
ret = krb5_unparse_name(context, principal, &str); |
| 130 |
if (ret) |
| 131 |
return ret; |
| 132 |
key->data = str; |
| 133 |
key->length = strlen(str) + 1; |
| 134 |
return 0; |
| 135 |
} |
| 136 |
#endif /* HAVE_DB1 */ |
| 137 |
|
| 138 |
#define KRB5_KDB_SALTTYPE_NORMAL 0 |
| 139 |
#define KRB5_KDB_SALTTYPE_V4 1 |
| 140 |
#define KRB5_KDB_SALTTYPE_NOREALM 2 |
| 141 |
#define KRB5_KDB_SALTTYPE_ONLYREALM 3 |
| 142 |
#define KRB5_KDB_SALTTYPE_SPECIAL 4 |
| 143 |
#define KRB5_KDB_SALTTYPE_AFS3 5 |
| 144 |
#define KRB5_KDB_SALTTYPE_CERTHASH 6 |
| 145 |
|
| 146 |
static krb5_error_code |
| 147 |
fix_salt(krb5_context context, hdb_entry *ent, Key *k) |
| 148 |
{ |
| 149 |
krb5_error_code ret; |
| 150 |
Salt *salt = k->salt; |
| 151 |
/* fix salt type */ |
| 152 |
switch((int)salt->type) { |
| 153 |
case KRB5_KDB_SALTTYPE_NORMAL: |
| 154 |
salt->type = KRB5_PADATA_PW_SALT; |
| 155 |
break; |
| 156 |
case KRB5_KDB_SALTTYPE_V4: |
| 157 |
krb5_data_free(&salt->salt); |
| 158 |
salt->type = KRB5_PADATA_PW_SALT; |
| 159 |
break; |
| 160 |
case KRB5_KDB_SALTTYPE_NOREALM: |
| 161 |
{ |
| 162 |
size_t len; |
| 163 |
size_t i; |
| 164 |
char *p; |
| 165 |
|
| 166 |
len = 0; |
| 167 |
for (i = 0; i < ent->principal->name.name_string.len; ++i) |
| 168 |
len += strlen(ent->principal->name.name_string.val[i]); |
| 169 |
ret = krb5_data_alloc (&salt->salt, len); |
| 170 |
if (ret) |
| 171 |
return ret; |
| 172 |
p = salt->salt.data; |
| 173 |
for (i = 0; i < ent->principal->name.name_string.len; ++i) { |
| 174 |
memcpy (p, |
| 175 |
ent->principal->name.name_string.val[i], |
| 176 |
strlen(ent->principal->name.name_string.val[i])); |
| 177 |
p += strlen(ent->principal->name.name_string.val[i]); |
| 178 |
} |
| 179 |
|
| 180 |
salt->type = KRB5_PADATA_PW_SALT; |
| 181 |
break; |
| 182 |
} |
| 183 |
case KRB5_KDB_SALTTYPE_ONLYREALM: |
| 184 |
krb5_data_free(&salt->salt); |
| 185 |
ret = krb5_data_copy(&salt->salt, |
| 186 |
ent->principal->realm, |
| 187 |
strlen(ent->principal->realm)); |
| 188 |
if(ret) |
| 189 |
return ret; |
| 190 |
salt->type = KRB5_PADATA_PW_SALT; |
| 191 |
break; |
| 192 |
case KRB5_KDB_SALTTYPE_SPECIAL: |
| 193 |
salt->type = KRB5_PADATA_PW_SALT; |
| 194 |
break; |
| 195 |
case KRB5_KDB_SALTTYPE_AFS3: |
| 196 |
krb5_data_free(&salt->salt); |
| 197 |
ret = krb5_data_copy(&salt->salt, |
| 198 |
ent->principal->realm, |
| 199 |
strlen(ent->principal->realm)); |
| 200 |
if(ret) |
| 201 |
return ret; |
| 202 |
salt->type = KRB5_PADATA_AFS3_SALT; |
| 203 |
break; |
| 204 |
case KRB5_KDB_SALTTYPE_CERTHASH: |
| 205 |
krb5_data_free(&salt->salt); |
| 206 |
free(k->salt); |
| 207 |
k->salt = NULL; |
| 208 |
break; |
| 209 |
default: |
| 210 |
abort(); |
| 211 |
} |
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
/** |
| 217 |
* This function takes a key from a krb5_storage from an MIT KDB encoded |
| 218 |
* entry and places it in the given Key object. |
| 219 |
* |
| 220 |
* @param context Context |
| 221 |
* @param entry HDB entry |
| 222 |
* @param sp krb5_storage with current offset set to the beginning of a |
| 223 |
* key |
| 224 |
* @param version See comments in caller body for the backstory on this |
| 225 |
* @param k Key * to load the key into |
| 226 |
*/ |
| 227 |
static krb5_error_code |
| 228 |
mdb_keyvalue2key(krb5_context context, hdb_entry *entry, krb5_storage *sp, uint16_t version, Key *k) |
| 229 |
{ |
| 230 |
size_t i; |
| 231 |
uint16_t u16, type; |
| 232 |
krb5_error_code ret; |
| 233 |
|
| 234 |
k->mkvno = malloc(sizeof(*k->mkvno)); |
| 235 |
if (k->mkvno == NULL) { |
| 236 |
ret = ENOMEM; |
| 237 |
goto out; |
| 238 |
} |
| 239 |
*k->mkvno = 1; |
| 240 |
|
| 241 |
for (i = 0; i < version; i++) { |
| 242 |
CHECK(ret = krb5_ret_uint16(sp, &type)); |
| 243 |
CHECK(ret = krb5_ret_uint16(sp, &u16)); |
| 244 |
if (i == 0) { |
| 245 |
/* This "version" means we have a key */ |
| 246 |
k->key.keytype = type; |
| 247 |
/* |
| 248 |
* MIT stores keys encrypted keys as {16-bit length |
| 249 |
* of plaintext key, {encrypted key}}. The reason |
| 250 |
* for this is that the Kerberos cryptosystem is not |
| 251 |
* length-preserving. Heimdal's approach is to |
| 252 |
* truncate the plaintext to the expected length of |
| 253 |
* the key given its enctype, so we ignore this |
| 254 |
* 16-bit length-of-plaintext-key field. |
| 255 |
*/ |
| 256 |
if (u16 > 2) { |
| 257 |
krb5_storage_seek(sp, 2, SEEK_CUR); /* skip real length */ |
| 258 |
k->key.keyvalue.length = u16 - 2; /* adjust cipher len */ |
| 259 |
k->key.keyvalue.data = malloc(k->key.keyvalue.length); |
| 260 |
krb5_storage_read(sp, k->key.keyvalue.data, |
| 261 |
k->key.keyvalue.length); |
| 262 |
} else { |
| 263 |
/* We'll ignore this key; see our caller */ |
| 264 |
k->key.keyvalue.length = 0; |
| 265 |
k->key.keyvalue.data = NULL; |
| 266 |
krb5_storage_seek(sp, u16, SEEK_CUR); /* skip real length */ |
| 267 |
} |
| 268 |
} else if (i == 1) { |
| 269 |
/* This "version" means we have a salt */ |
| 270 |
k->salt = calloc(1, sizeof(*k->salt)); |
| 271 |
if (k->salt == NULL) { |
| 272 |
ret = ENOMEM; |
| 273 |
goto out; |
| 274 |
} |
| 275 |
k->salt->type = type; |
| 276 |
if (u16 != 0) { |
| 277 |
k->salt->salt.data = malloc(u16); |
| 278 |
if (k->salt->salt.data == NULL) { |
| 279 |
ret = ENOMEM; |
| 280 |
goto out; |
| 281 |
} |
| 282 |
k->salt->salt.length = u16; |
| 283 |
krb5_storage_read(sp, k->salt->salt.data, k->salt->salt.length); |
| 284 |
} |
| 285 |
fix_salt(context, entry, k); |
| 286 |
} else { |
| 287 |
/* |
| 288 |
* Whatever this "version" might be, we skip it |
| 289 |
* |
| 290 |
* XXX A krb5.conf parameter requesting that we log |
| 291 |
* about strangeness like this, or return an error |
| 292 |
* from here, might be nice. |
| 293 |
*/ |
| 294 |
krb5_storage_seek(sp, u16, SEEK_CUR); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
return 0; |
| 299 |
|
| 300 |
out: |
| 301 |
free_Key(k); |
| 302 |
return ret; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
static krb5_error_code |
| 307 |
add_1des_dup(krb5_context context, Keys *keys, Key *key, krb5_keytype keytype) |
| 308 |
{ |
| 309 |
key->key.keytype = keytype; |
| 310 |
return add_Keys(keys, key); |
| 311 |
} |
| 312 |
|
| 313 |
/* |
| 314 |
* This monstrosity is here so we can avoid having to do enctype |
| 315 |
* similarity checking in the KDC. This helper function dups 1DES keys |
| 316 |
* in a keyset for all the similar 1DES enctypes for which keys are |
| 317 |
* missing. And, of course, we do this only if there's any 1DES keys in |
| 318 |
* the keyset to begin with. |
| 319 |
*/ |
| 320 |
static krb5_error_code |
| 321 |
dup_similar_keys_in_keyset(krb5_context context, Keys *keys) |
| 322 |
{ |
| 323 |
krb5_error_code ret; |
| 324 |
size_t i, k; |
| 325 |
Key key; |
| 326 |
int keyset_has_1des_crc = 0; |
| 327 |
int keyset_has_1des_md4 = 0; |
| 328 |
int keyset_has_1des_md5 = 0; |
| 329 |
|
| 330 |
memset(&key, 0, sizeof (key)); |
| 331 |
k = keys->len; |
| 332 |
for (i = 0; i < keys->len; i++) { |
| 333 |
if (keys->val[i].key.keytype == ETYPE_DES_CBC_CRC) { |
| 334 |
keyset_has_1des_crc = 1; |
| 335 |
if (k == keys->len) |
| 336 |
k = i; |
| 337 |
} else if (keys->val[i].key.keytype == ETYPE_DES_CBC_MD4) { |
| 338 |
keyset_has_1des_crc = 1; |
| 339 |
if (k == keys->len) |
| 340 |
k = i; |
| 341 |
} else if (keys->val[i].key.keytype == ETYPE_DES_CBC_MD5) { |
| 342 |
keyset_has_1des_crc = 1; |
| 343 |
if (k == keys->len) |
| 344 |
k = i; |
| 345 |
} |
| 346 |
} |
| 347 |
if (k == keys->len) |
| 348 |
return 0; |
| 349 |
|
| 350 |
ret = copy_Key(&keys->val[k], &key); |
| 351 |
if (ret) |
| 352 |
return ret; |
| 353 |
if (!keyset_has_1des_crc) { |
| 354 |
ret = add_1des_dup(context, keys, &key, ETYPE_DES_CBC_CRC); |
| 355 |
if (ret) |
| 356 |
goto out; |
| 357 |
} |
| 358 |
if (!keyset_has_1des_md4) { |
| 359 |
ret = add_1des_dup(context, keys, &key, ETYPE_DES_CBC_MD4); |
| 360 |
if (ret) |
| 361 |
goto out; |
| 362 |
} |
| 363 |
if (!keyset_has_1des_md5) { |
| 364 |
ret = add_1des_dup(context, keys, &key, ETYPE_DES_CBC_MD5); |
| 365 |
if (ret) |
| 366 |
goto out; |
| 367 |
} |
| 368 |
|
| 369 |
out: |
| 370 |
free_Key(&key); |
| 371 |
return ret; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
static krb5_error_code |
| 376 |
dup_similar_keys(krb5_context context, hdb_entry *entry) |
| 377 |
{ |
| 378 |
krb5_error_code ret; |
| 379 |
HDB_Ext_KeySet *hist_keys; |
| 380 |
HDB_extension *extp; |
| 381 |
size_t i; |
| 382 |
|
| 383 |
ret = dup_similar_keys_in_keyset(context, &entry->keys); |
| 384 |
if (ret) |
| 385 |
return ret; |
| 386 |
extp = hdb_find_extension(entry, choice_HDB_extension_data_hist_keys); |
| 387 |
if (extp == NULL) |
| 388 |
return 0; |
| 389 |
|
| 390 |
hist_keys = &extp->data.u.hist_keys; |
| 391 |
for (i = 0; i < hist_keys->len; i++) { |
| 392 |
ret = dup_similar_keys_in_keyset(context, &hist_keys->val[i].keys); |
| 393 |
if (ret) |
| 394 |
return ret; |
| 395 |
} |
| 396 |
return 0; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
/** |
| 401 |
* This function parses an MIT krb5 encoded KDB entry and fills in the |
| 402 |
* given HDB entry with it. |
| 403 |
* |
| 404 |
* @param context krb5_context |
| 405 |
* @param data Encoded MIT KDB entry |
| 406 |
* @param target_kvno Desired kvno, or 0 for the entry's current kvno |
| 407 |
* @param entry Desired kvno, or 0 for the entry's current kvno |
| 408 |
*/ |
| 409 |
krb5_error_code |
| 410 |
_hdb_mdb_value2entry(krb5_context context, krb5_data *data, |
| 411 |
krb5_kvno target_kvno, hdb_entry *entry) |
| 412 |
{ |
| 413 |
krb5_error_code ret; |
| 414 |
krb5_storage *sp; |
| 415 |
Key k; |
| 416 |
krb5_kvno key_kvno; |
| 417 |
uint32_t u32; |
| 418 |
uint16_t u16, num_keys, num_tl; |
| 419 |
ssize_t sz; |
| 420 |
size_t i; |
| 421 |
char *p; |
| 422 |
|
| 423 |
memset(&k, 0, sizeof (k)); |
| 424 |
memset(entry, 0, sizeof(*entry)); |
| 425 |
|
| 426 |
sp = krb5_storage_from_data(data); |
| 427 |
if (sp == NULL) { |
| 428 |
krb5_set_error_message(context, ENOMEM, "out of memory"); |
| 429 |
return ENOMEM; |
| 430 |
} |
| 431 |
|
| 432 |
krb5_storage_set_byteorder(sp, KRB5_STORAGE_BYTEORDER_LE); |
| 433 |
|
| 434 |
/* |
| 435 |
* 16: baselength |
| 436 |
* |
| 437 |
* The story here is that these 16 bits have to be a constant: |
| 438 |
* KDB_V1_BASE_LENGTH. Once upon a time a different value here |
| 439 |
* would have been used to indicate the presence of "extra data" |
| 440 |
* between the "base" contents and the {principal name, TL data, |
| 441 |
* keys} that follow it. Nothing supports such "extra data" |
| 442 |
* nowadays, so neither do we here. |
| 443 |
* |
| 444 |
* XXX But... surely we ought to log about this extra data, or skip |
| 445 |
* it, or something, in case anyone has MIT KDBs with ancient |
| 446 |
* entries in them... Logging would allow the admin to know which |
| 447 |
* entries to dump with MIT krb5's kdb5_util. But logging would be |
| 448 |
* noisy. For now we do nothing. |
| 449 |
*/ |
| 450 |
CHECK(ret = krb5_ret_uint16(sp, &u16)); |
| 451 |
if (u16 != KDB_V1_BASE_LENGTH) { ret = EINVAL; goto out; } |
| 452 |
/* 32: attributes */ |
| 453 |
CHECK(ret = krb5_ret_uint32(sp, &u32)); |
| 454 |
attr_to_flags(u32, &entry->flags); |
| 455 |
|
| 456 |
/* 32: max time */ |
| 457 |
CHECK(ret = krb5_ret_uint32(sp, &u32)); |
| 458 |
if (u32) { |
| 459 |
entry->max_life = malloc(sizeof(*entry->max_life)); |
| 460 |
*entry->max_life = u32; |
| 461 |
} |
| 462 |
/* 32: max renewable time */ |
| 463 |
CHECK(ret = krb5_ret_uint32(sp, &u32)); |
| 464 |
if (u32) { |
| 465 |
entry->max_renew = malloc(sizeof(*entry->max_renew)); |
| 466 |
*entry->max_renew = u32; |
| 467 |
} |
| 468 |
/* 32: client expire */ |
| 469 |
CHECK(ret = krb5_ret_uint32(sp, &u32)); |
| 470 |
if (u32) { |
| 471 |
entry->valid_end = malloc(sizeof(*entry->valid_end)); |
| 472 |
*entry->valid_end = u32; |
| 473 |
} |
| 474 |
/* 32: passwd expire */ |
| 475 |
CHECK(ret = krb5_ret_uint32(sp, &u32)); |
| 476 |
if (u32) { |
| 477 |
entry->pw_end = malloc(sizeof(*entry->pw_end)); |
| 478 |
*entry->pw_end = u32; |
| 479 |
} |
| 480 |
/* 32: last successful passwd */ |
| 481 |
CHECK(ret = krb5_ret_uint32(sp, &u32)); |
| 482 |
/* 32: last failed attempt */ |
| 483 |
CHECK(ret = krb5_ret_uint32(sp, &u32)); |
| 484 |
/* 32: num of failed attempts */ |
| 485 |
CHECK(ret = krb5_ret_uint32(sp, &u32)); |
| 486 |
/* 16: num tl data */ |
| 487 |
CHECK(ret = krb5_ret_uint16(sp, &u16)); |
| 488 |
num_tl = u16; |
| 489 |
/* 16: num key data */ |
| 490 |
CHECK(ret = krb5_ret_uint16(sp, &u16)); |
| 491 |
num_keys = u16; |
| 492 |
/* 16: principal length */ |
| 493 |
CHECK(ret = krb5_ret_uint16(sp, &u16)); |
| 494 |
/* length: principal */ |
| 495 |
{ |
| 496 |
/* |
| 497 |
* Note that the principal name includes the NUL in the entry, |
| 498 |
* but we don't want to take chances, so we add an extra NUL. |
| 499 |
*/ |
| 500 |
p = malloc(u16 + 1); |
| 501 |
if (p == NULL) { |
| 502 |
ret = ENOMEM; |
| 503 |
goto out; |
| 504 |
} |
| 505 |
sz = krb5_storage_read(sp, p, u16); |
| 506 |
if (sz != u16) { |
| 507 |
ret = EINVAL; /* XXX */ |
| 508 |
goto out; |
| 509 |
} |
| 510 |
p[u16] = '\0'; |
| 511 |
CHECK(ret = krb5_parse_name(context, p, &entry->principal)); |
| 512 |
free(p); |
| 513 |
} |
| 514 |
/* for num tl data times |
| 515 |
16: tl data type |
| 516 |
16: tl data length |
| 517 |
length: length */ |
| 518 |
#define mit_KRB5_TL_LAST_PWD_CHANGE 1 |
| 519 |
#define mit_KRB5_TL_MOD_PRINC 2 |
| 520 |
for (i = 0; i < num_tl; i++) { |
| 521 |
int tl_type; |
| 522 |
krb5_principal modby; |
| 523 |
/* 16: TL data type */ |
| 524 |
CHECK(ret = krb5_ret_uint16(sp, &u16)); |
| 525 |
tl_type = u16; |
| 526 |
/* 16: TL data length */ |
| 527 |
CHECK(ret = krb5_ret_uint16(sp, &u16)); |
| 528 |
/* |
| 529 |
* For rollback to MIT purposes we really must understand some |
| 530 |
* TL data! |
| 531 |
* |
| 532 |
* XXX Move all this to separate functions, one per-TL type. |
| 533 |
*/ |
| 534 |
switch (tl_type) { |
| 535 |
case mit_KRB5_TL_LAST_PWD_CHANGE: |
| 536 |
CHECK(ret = krb5_ret_uint32(sp, &u32)); |
| 537 |
CHECK(ret = hdb_entry_set_pw_change_time(context, entry, u32)); |
| 538 |
break; |
| 539 |
case mit_KRB5_TL_MOD_PRINC: |
| 540 |
if (u16 < 5) { |
| 541 |
ret = EINVAL; /* XXX */ |
| 542 |
goto out; |
| 543 |
} |
| 544 |
CHECK(ret = krb5_ret_uint32(sp, &u32)); /* mod time */ |
| 545 |
p = malloc(u16 - 4 + 1); |
| 546 |
if (!p) { |
| 547 |
ret = ENOMEM; |
| 548 |
goto out; |
| 549 |
} |
| 550 |
p[u16 - 4] = '\0'; |
| 551 |
sz = krb5_storage_read(sp, p, u16 - 4); |
| 552 |
if (sz != u16 - 4) { |
| 553 |
ret = EINVAL; /* XXX */ |
| 554 |
goto out; |
| 555 |
} |
| 556 |
CHECK(ret = krb5_parse_name(context, p, &modby)); |
| 557 |
ret = hdb_set_last_modified_by(context, entry, modby, u32); |
| 558 |
krb5_free_principal(context, modby); |
| 559 |
free(p); |
| 560 |
break; |
| 561 |
default: |
| 562 |
krb5_storage_seek(sp, u16, SEEK_CUR); |
| 563 |
break; |
| 564 |
} |
| 565 |
} |
| 566 |
/* |
| 567 |
* for num key data times |
| 568 |
* 16: "version" |
| 569 |
* 16: kvno |
| 570 |
* for version times: |
| 571 |
* 16: type |
| 572 |
* 16: length |
| 573 |
* length: keydata |
| 574 |
* |
| 575 |
* "version" here is really 1 or 2, the first meaning there's only |
| 576 |
* keys for this kvno, the second meaning there's keys and salt[s?]. |
| 577 |
* That's right... hold that gag reflex, you can do it. |
| 578 |
*/ |
| 579 |
for (i = 0; i < num_keys; i++) { |
| 580 |
uint16_t version; |
| 581 |
|
| 582 |
CHECK(ret = krb5_ret_uint16(sp, &u16)); |
| 583 |
version = u16; |
| 584 |
CHECK(ret = krb5_ret_uint16(sp, &u16)); |
| 585 |
key_kvno = u16; |
| 586 |
|
| 587 |
ret = mdb_keyvalue2key(context, entry, sp, version, &k); |
| 588 |
if (ret) |
| 589 |
goto out; |
| 590 |
if (k.key.keytype == 0 || k.key.keyvalue.length == 0) { |
| 591 |
/* |
| 592 |
* Older MIT KDBs may have enctype 0 / length 0 keys. We |
| 593 |
* ignore these. |
| 594 |
*/ |
| 595 |
free_Key(&k); |
| 596 |
continue; |
| 597 |
} |
| 598 |
|
| 599 |
if ((target_kvno == 0 && entry->kvno < key_kvno) || |
| 600 |
(target_kvno == key_kvno && entry->kvno != target_kvno)) { |
| 601 |
/* |
| 602 |
* MIT's KDB doesn't keep track of kvno. The highest kvno |
| 603 |
* is the current kvno, and we just found a new highest |
| 604 |
* kvno or the desired kvno. |
| 605 |
* |
| 606 |
* Note that there's no guarantee of any key ordering, but |
| 607 |
* generally MIT KDB entries have keys in strictly |
| 608 |
* descending kvno order. |
| 609 |
* |
| 610 |
* XXX We do assume that keys are clustered by kvno. If |
| 611 |
* not, then bad. It might be possible to construct |
| 612 |
* non-clustered keys via the kadm5 API. It wouldn't be |
| 613 |
* hard to cope with this, since if it happens the worst |
| 614 |
* that will happen is that some of the current keys can be |
| 615 |
* found in the history extension, and we could just pull |
| 616 |
* them back out in that case. |
| 617 |
*/ |
| 618 |
ret = hdb_add_current_keys_to_history(context, entry); |
| 619 |
if (ret) |
| 620 |
goto out; |
| 621 |
free_Keys(&entry->keys); |
| 622 |
ret = add_Keys(&entry->keys, &k); |
| 623 |
free_Key(&k); |
| 624 |
if (ret) |
| 625 |
goto out; |
| 626 |
entry->kvno = key_kvno; |
| 627 |
continue; |
| 628 |
} |
| 629 |
|
| 630 |
if (entry->kvno == key_kvno) { |
| 631 |
/* |
| 632 |
* Note that if key_kvno == 0 and target_kvno == 0 then we |
| 633 |
* end up adding those keys here. Yeah, kvno 0 is very |
| 634 |
* special for us, but just in case, we keep such keys. |
| 635 |
*/ |
| 636 |
ret = add_Keys(&entry->keys, &k); |
| 637 |
free_Key(&k); |
| 638 |
if (ret) |
| 639 |
goto out; |
| 640 |
entry->kvno = key_kvno; |
| 641 |
} else { |
| 642 |
ret = hdb_add_history_key(context, entry, key_kvno, &k); |
| 643 |
if (ret) |
| 644 |
goto out; |
| 645 |
free_Key(&k); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
if (target_kvno != 0 && entry->kvno != target_kvno) { |
| 650 |
ret = HDB_ERR_KVNO_NOT_FOUND; |
| 651 |
goto out; |
| 652 |
} |
| 653 |
|
| 654 |
krb5_storage_free(sp); |
| 655 |
|
| 656 |
return dup_similar_keys(context, entry); |
| 657 |
|
| 658 |
out: |
| 659 |
krb5_storage_free(sp); |
| 660 |
|
| 661 |
if (ret == HEIM_ERR_EOF) |
| 662 |
/* Better error code than "end of file" */ |
| 663 |
ret = HEIM_ERR_BAD_HDBENT_ENCODING; |
| 664 |
free_hdb_entry(entry); |
| 665 |
free_Key(&k); |
| 666 |
return ret; |
| 667 |
} |
| 668 |
|
| 669 |
#if 0 |
| 670 |
static krb5_error_code |
| 671 |
mdb_entry2value(krb5_context context, hdb_entry *entry, krb5_data *data) |
| 672 |
{ |
| 673 |
return EINVAL; |
| 674 |
} |
| 675 |
#endif |
| 676 |
|
| 677 |
#if HAVE_DB1 |
| 678 |
|
| 679 |
#if defined(HAVE_DB_185_H) |
| 680 |
#include <db_185.h> |
| 681 |
#elif defined(HAVE_DB_H) |
| 682 |
#include <db.h> |
| 683 |
#endif |
| 684 |
|
| 685 |
|
| 686 |
static krb5_error_code |
| 687 |
mdb_close(krb5_context context, HDB *db) |
| 688 |
{ |
| 689 |
DB *d = (DB*)db->hdb_db; |
| 690 |
(*d->close)(d); |
| 691 |
return 0; |
| 692 |
} |
| 693 |
|
| 694 |
static krb5_error_code |
| 695 |
mdb_destroy(krb5_context context, HDB *db) |
| 696 |
{ |
| 697 |
krb5_error_code ret; |
| 698 |
|
| 699 |
ret = hdb_clear_master_key (context, db); |
| 700 |
free(db->hdb_name); |
| 701 |
free(db); |
| 702 |
return ret; |
| 703 |
} |
| 704 |
|
| 705 |
static krb5_error_code |
| 706 |
mdb_set_sync(krb5_context context, HDB *db, int on) |
| 707 |
{ |
| 708 |
MITDB *mdb = (MITDB *)db; |
| 709 |
DB *d = (DB*)db->hdb_db; |
| 710 |
|
| 711 |
mdb->do_sync = on; |
| 712 |
if (on) |
| 713 |
return fsync((*d->fd)(d)); |
| 714 |
return 0; |
| 715 |
} |
| 716 |
|
| 717 |
static krb5_error_code |
| 718 |
mdb_lock(krb5_context context, HDB *db, int operation) |
| 719 |
{ |
| 720 |
DB *d = (DB*)db->hdb_db; |
| 721 |
int fd = (*d->fd)(d); |
| 722 |
krb5_error_code ret; |
| 723 |
|
| 724 |
if (db->lock_count > 1) { |
| 725 |
db->lock_count++; |
| 726 |
if (db->lock_type == HDB_WLOCK || db->lock_count == operation) |
| 727 |
return 0; |
| 728 |
} |
| 729 |
|
| 730 |
if(fd < 0) { |
| 731 |
krb5_set_error_message(context, HDB_ERR_CANT_LOCK_DB, |
| 732 |
"Can't lock database: %s", db->hdb_name); |
| 733 |
return HDB_ERR_CANT_LOCK_DB; |
| 734 |
} |
| 735 |
ret = hdb_lock(fd, operation); |
| 736 |
if (ret) |
| 737 |
return ret; |
| 738 |
db->lock_count++; |
| 739 |
return 0; |
| 740 |
} |
| 741 |
|
| 742 |
static krb5_error_code |
| 743 |
mdb_unlock(krb5_context context, HDB *db) |
| 744 |
{ |
| 745 |
DB *d = (DB*)db->hdb_db; |
| 746 |
int fd = (*d->fd)(d); |
| 747 |
|
| 748 |
if (db->lock_count > 1) { |
| 749 |
db->lock_count--; |
| 750 |
return 0; |
| 751 |
} |
| 752 |
heim_assert(db->lock_count == 1, "HDB lock/unlock sequence does not match"); |
| 753 |
db->lock_count--; |
| 754 |
|
| 755 |
if(fd < 0) { |
| 756 |
krb5_set_error_message(context, HDB_ERR_CANT_LOCK_DB, |
| 757 |
"Can't unlock database: %s", db->hdb_name); |
| 758 |
return HDB_ERR_CANT_LOCK_DB; |
| 759 |
} |
| 760 |
return hdb_unlock(fd); |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
static krb5_error_code |
| 765 |
mdb_seq(krb5_context context, HDB *db, |
| 766 |
unsigned flags, hdb_entry_ex *entry, int flag) |
| 767 |
{ |
| 768 |
DB *d = (DB*)db->hdb_db; |
| 769 |
DBT key, value; |
| 770 |
krb5_data key_data, data; |
| 771 |
int code; |
| 772 |
|
| 773 |
code = db->hdb_lock(context, db, HDB_RLOCK); |
| 774 |
if(code == -1) { |
| 775 |
krb5_set_error_message(context, HDB_ERR_DB_INUSE, "Database %s in use", db->hdb_name); |
| 776 |
return HDB_ERR_DB_INUSE; |
| 777 |
} |
| 778 |
code = (*d->seq)(d, &key, &value, flag); |
| 779 |
db->hdb_unlock(context, db); /* XXX check value */ |
| 780 |
if(code == -1) { |
| 781 |
code = errno; |
| 782 |
krb5_set_error_message(context, code, "Database %s seq error: %s", |
| 783 |
db->hdb_name, strerror(code)); |
| 784 |
return code; |
| 785 |
} |
| 786 |
if(code == 1) { |
| 787 |
krb5_clear_error_message(context); |
| 788 |
return HDB_ERR_NOENTRY; |
| 789 |
} |
| 790 |
|
| 791 |
key_data.data = key.data; |
| 792 |
key_data.length = key.size; |
| 793 |
data.data = value.data; |
| 794 |
data.length = value.size; |
| 795 |
memset(entry, 0, sizeof(*entry)); |
| 796 |
|
| 797 |
if (_hdb_mdb_value2entry(context, &data, 0, &entry->entry)) |
| 798 |
return mdb_seq(context, db, flags, entry, R_NEXT); |
| 799 |
|
| 800 |
if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) { |
| 801 |
code = hdb_unseal_keys (context, db, &entry->entry); |
| 802 |
if (code) |
| 803 |
hdb_free_entry (context, entry); |
| 804 |
} |
| 805 |
|
| 806 |
return code; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
static krb5_error_code |
| 811 |
mdb_firstkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry) |
| 812 |
{ |
| 813 |
return mdb_seq(context, db, flags, entry, R_FIRST); |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
static krb5_error_code |
| 818 |
mdb_nextkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry) |
| 819 |
{ |
| 820 |
return mdb_seq(context, db, flags, entry, R_NEXT); |
| 821 |
} |
| 822 |
|
| 823 |
static krb5_error_code |
| 824 |
mdb_rename(krb5_context context, HDB *db, const char *new_name) |
| 825 |
{ |
| 826 |
int ret; |
| 827 |
char *old = NULL; |
| 828 |
char *new = NULL; |
| 829 |
|
| 830 |
if (asprintf(&old, "%s.db", db->hdb_name) < 0) |
| 831 |
goto out; |
| 832 |
if (asprintf(&new, "%s.db", new_name) < 0) |
| 833 |
goto out; |
| 834 |
ret = rename(old, new); |
| 835 |
if(ret) |
| 836 |
goto out; |
| 837 |
|
| 838 |
free(db->hdb_name); |
| 839 |
db->hdb_name = strdup(new_name); |
| 840 |
errno = 0; |
| 841 |
|
| 842 |
out: |
| 843 |
free(old); |
| 844 |
free(new); |
| 845 |
return errno; |
| 846 |
} |
| 847 |
|
| 848 |
static krb5_error_code |
| 849 |
mdb__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply) |
| 850 |
{ |
| 851 |
DB *d = (DB*)db->hdb_db; |
| 852 |
DBT k, v; |
| 853 |
int code; |
| 854 |
|
| 855 |
k.data = key.data; |
| 856 |
k.size = key.length; |
| 857 |
code = db->hdb_lock(context, db, HDB_RLOCK); |
| 858 |
if(code) |
| 859 |
return code; |
| 860 |
code = (*d->get)(d, &k, &v, 0); |
| 861 |
db->hdb_unlock(context, db); |
| 862 |
if(code < 0) { |
| 863 |
code = errno; |
| 864 |
krb5_set_error_message(context, code, "Database %s get error: %s", |
| 865 |
db->hdb_name, strerror(code)); |
| 866 |
return code; |
| 867 |
} |
| 868 |
if(code == 1) { |
| 869 |
krb5_clear_error_message(context); |
| 870 |
return HDB_ERR_NOENTRY; |
| 871 |
} |
| 872 |
|
| 873 |
krb5_data_copy(reply, v.data, v.size); |
| 874 |
return 0; |
| 875 |
} |
| 876 |
|
| 877 |
static krb5_error_code |
| 878 |
mdb__put(krb5_context context, HDB *db, int replace, |
| 879 |
krb5_data key, krb5_data value) |
| 880 |
{ |
| 881 |
MITDB *mdb = (MITDB *)db; |
| 882 |
DB *d = (DB*)db->hdb_db; |
| 883 |
DBT k, v; |
| 884 |
int code; |
| 885 |
|
| 886 |
k.data = key.data; |
| 887 |
k.size = key.length; |
| 888 |
v.data = value.data; |
| 889 |
v.size = value.length; |
| 890 |
code = db->hdb_lock(context, db, HDB_WLOCK); |
| 891 |
if(code) |
| 892 |
return code; |
| 893 |
code = (*d->put)(d, &k, &v, replace ? 0 : R_NOOVERWRITE); |
| 894 |
if (code == 0) { |
| 895 |
code = mdb_set_sync(context, db, mdb->do_sync); |
| 896 |
db->hdb_unlock(context, db); |
| 897 |
return code; |
| 898 |
} |
| 899 |
db->hdb_unlock(context, db); |
| 900 |
if(code < 0) { |
| 901 |
code = errno; |
| 902 |
krb5_set_error_message(context, code, "Database %s put error: %s", |
| 903 |
db->hdb_name, strerror(code)); |
| 904 |
return code; |
| 905 |
} |
| 906 |
krb5_clear_error_message(context); |
| 907 |
return HDB_ERR_EXISTS; |
| 908 |
} |
| 909 |
|
| 910 |
static krb5_error_code |
| 911 |
mdb__del(krb5_context context, HDB *db, krb5_data key) |
| 912 |
{ |
| 913 |
MITDB *mdb = (MITDB *)db; |
| 914 |
DB *d = (DB*)db->hdb_db; |
| 915 |
DBT k; |
| 916 |
krb5_error_code code; |
| 917 |
k.data = key.data; |
| 918 |
k.size = key.length; |
| 919 |
code = db->hdb_lock(context, db, HDB_WLOCK); |
| 920 |
if(code) |
| 921 |
return code; |
| 922 |
code = (*d->del)(d, &k, 0); |
| 923 |
if (code == 0) { |
| 924 |
code = mdb_set_sync(context, db, mdb->do_sync); |
| 925 |
db->hdb_unlock(context, db); |
| 926 |
return code; |
| 927 |
} |
| 928 |
db->hdb_unlock(context, db); |
| 929 |
if(code == 1) { |
| 930 |
code = errno; |
| 931 |
krb5_set_error_message(context, code, "Database %s put error: %s", |
| 932 |
db->hdb_name, strerror(code)); |
| 933 |
return code; |
| 934 |
} |
| 935 |
if(code < 0) |
| 936 |
return errno; |
| 937 |
return 0; |
| 938 |
} |
| 939 |
|
| 940 |
static krb5_error_code |
| 941 |
mdb_fetch_kvno(krb5_context context, HDB *db, krb5_const_principal principal, |
| 942 |
unsigned flags, krb5_kvno kvno, hdb_entry_ex *entry) |
| 943 |
{ |
| 944 |
krb5_data key, value; |
| 945 |
krb5_error_code ret; |
| 946 |
|
| 947 |
ret = mdb_principal2key(context, principal, &key); |
| 948 |
if (ret) |
| 949 |
return ret; |
| 950 |
ret = db->hdb__get(context, db, key, &value); |
| 951 |
krb5_data_free(&key); |
| 952 |
if(ret) |
| 953 |
return ret; |
| 954 |
ret = _hdb_mdb_value2entry(context, &value, kvno, &entry->entry); |
| 955 |
krb5_data_free(&value); |
| 956 |
if (ret) |
| 957 |
return ret; |
| 958 |
|
| 959 |
if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) { |
| 960 |
ret = hdb_unseal_keys (context, db, &entry->entry); |
| 961 |
if (ret) { |
| 962 |
hdb_free_entry(context, entry); |
| 963 |
return ret; |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
return 0; |
| 968 |
} |
| 969 |
|
| 970 |
static krb5_error_code |
| 971 |
mdb_store(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry) |
| 972 |
{ |
| 973 |
krb5_error_code ret; |
| 974 |
krb5_storage *sp = NULL; |
| 975 |
krb5_storage *spent = NULL; |
| 976 |
krb5_data line = { 0, 0 }; |
| 977 |
krb5_data kdb_ent = { 0, 0 }; |
| 978 |
krb5_data key = { 0, 0 }; |
| 979 |
krb5_data value = { 0, 0 }; |
| 980 |
ssize_t sz; |
| 981 |
|
| 982 |
if ((flags & HDB_F_PRECHECK) && (flags & HDB_F_REPLACE)) |
| 983 |
return 0; |
| 984 |
|
| 985 |
if ((flags & HDB_F_PRECHECK)) { |
| 986 |
ret = mdb_principal2key(context, entry->entry.principal, &key); |
| 987 |
if (ret) return ret; |
| 988 |
ret = db->hdb__get(context, db, key, &value); |
| 989 |
krb5_data_free(&key); |
| 990 |
if (ret == 0) |
| 991 |
krb5_data_free(&value); |
| 992 |
if (ret == HDB_ERR_NOENTRY) |
| 993 |
return 0; |
| 994 |
return ret ? ret : HDB_ERR_EXISTS; |
| 995 |
} |
| 996 |
|
| 997 |
sp = krb5_storage_emem(); |
| 998 |
if (!sp) return ENOMEM; |
| 999 |
ret = _hdb_set_master_key_usage(context, db, 0); /* MIT KDB uses KU 0 */ |
| 1000 |
ret = hdb_seal_keys(context, db, &entry->entry); |
| 1001 |
if (ret) return ret; |
| 1002 |
ret = entry2mit_string_int(context, sp, &entry->entry); |
| 1003 |
if (ret) goto out; |
| 1004 |
sz = krb5_storage_write(sp, "\n", 2); /* NUL-terminate */ |
| 1005 |
ret = ENOMEM; |
| 1006 |
if (sz == -1) goto out; |
| 1007 |
ret = krb5_storage_to_data(sp, &line); |
| 1008 |
if (ret) goto out; |
| 1009 |
|
| 1010 |
ret = ENOMEM; |
| 1011 |
spent = krb5_storage_emem(); |
| 1012 |
if (!spent) goto out; |
| 1013 |
ret = _hdb_mit_dump2mitdb_entry(context, line.data, spent); |
| 1014 |
if (ret) goto out; |
| 1015 |
ret = krb5_storage_to_data(spent, &kdb_ent); |
| 1016 |
if (ret) goto out; |
| 1017 |
ret = mdb_principal2key(context, entry->entry.principal, &key); |
| 1018 |
if (ret) goto out; |
| 1019 |
ret = mdb__put(context, db, 1, key, kdb_ent); |
| 1020 |
|
| 1021 |
out: |
| 1022 |
if (sp) |
| 1023 |
krb5_storage_free(sp); |
| 1024 |
if (spent) |
| 1025 |
krb5_storage_free(spent); |
| 1026 |
krb5_data_free(&line); |
| 1027 |
krb5_data_free(&kdb_ent); |
| 1028 |
krb5_data_free(&key); |
| 1029 |
|
| 1030 |
return ret; |
| 1031 |
} |
| 1032 |
|
| 1033 |
static krb5_error_code |
| 1034 |
mdb_remove(krb5_context context, HDB *db, |
| 1035 |
unsigned flags, krb5_const_principal principal) |
| 1036 |
{ |
| 1037 |
krb5_error_code code; |
| 1038 |
krb5_data key; |
| 1039 |
krb5_data value = { 0, 0 }; |
| 1040 |
|
| 1041 |
code = mdb_principal2key(context, principal, &key); |
| 1042 |
if (code) |
| 1043 |
return code; |
| 1044 |
if ((flags & HDB_F_PRECHECK)) { |
| 1045 |
code = db->hdb__get(context, db, key, &value); |
| 1046 |
krb5_data_free(&key); |
| 1047 |
if (code == 0) { |
| 1048 |
krb5_data_free(&value); |
| 1049 |
return 0; |
| 1050 |
} |
| 1051 |
return code; |
| 1052 |
} |
| 1053 |
|
| 1054 |
code = db->hdb__del(context, db, key); |
| 1055 |
krb5_data_free(&key); |
| 1056 |
return code; |
| 1057 |
} |
| 1058 |
|
| 1059 |
static krb5_error_code |
| 1060 |
mdb_open(krb5_context context, HDB *db, int flags, mode_t mode) |
| 1061 |
{ |
| 1062 |
char *fn; |
| 1063 |
char *actual_fn; |
| 1064 |
krb5_error_code ret; |
| 1065 |
struct stat st; |
| 1066 |
|
| 1067 |
if (asprintf(&fn, "%s.db", db->hdb_name) < 0) { |
| 1068 |
krb5_set_error_message(context, ENOMEM, "malloc: out of memory"); |
| 1069 |
return ENOMEM; |
| 1070 |
} |
| 1071 |
|
| 1072 |
if (stat(fn, &st) == 0) |
| 1073 |
actual_fn = fn; |
| 1074 |
else |
| 1075 |
actual_fn = db->hdb_name; |
| 1076 |
db->hdb_db = dbopen(actual_fn, flags, mode, DB_BTREE, NULL); |
| 1077 |
if (db->hdb_db == NULL) { |
| 1078 |
switch (errno) { |
| 1079 |
#ifdef EFTYPE |
| 1080 |
case EFTYPE: |
| 1081 |
#endif |
| 1082 |
case EINVAL: |
| 1083 |
db->hdb_db = dbopen(actual_fn, flags, mode, DB_HASH, NULL); |
| 1084 |
} |
| 1085 |
} |
| 1086 |
free(fn); |
| 1087 |
|
| 1088 |
if (db->hdb_db == NULL) { |
| 1089 |
ret = errno; |
| 1090 |
krb5_set_error_message(context, ret, "dbopen (%s): %s", |
| 1091 |
db->hdb_name, strerror(ret)); |
| 1092 |
return ret; |
| 1093 |
} |
| 1094 |
#if 0 |
| 1095 |
/* |
| 1096 |
* Don't do this -- MIT won't be able to handle the |
| 1097 |
* HDB_DB_FORMAT_ENTRY key. |
| 1098 |
*/ |
| 1099 |
if ((flags & O_ACCMODE) != O_RDONLY) |
| 1100 |
ret = hdb_init_db(context, db); |
| 1101 |
#endif |
| 1102 |
ret = hdb_check_db_format(context, db); |
| 1103 |
if (ret == HDB_ERR_NOENTRY) { |
| 1104 |
krb5_clear_error_message(context); |
| 1105 |
return 0; |
| 1106 |
} |
| 1107 |
if (ret) { |
| 1108 |
mdb_close(context, db); |
| 1109 |
krb5_set_error_message(context, ret, "hdb_open: failed %s database %s", |
| 1110 |
(flags & O_ACCMODE) == O_RDONLY ? |
| 1111 |
"checking format of" : "initialize", |
| 1112 |
db->hdb_name); |
| 1113 |
} |
| 1114 |
return ret; |
| 1115 |
} |
| 1116 |
|
| 1117 |
krb5_error_code |
| 1118 |
hdb_mitdb_create(krb5_context context, HDB **db, |
| 1119 |
const char *filename) |
| 1120 |
{ |
| 1121 |
MITDB **mdb = (MITDB **)db; |
| 1122 |
*mdb = calloc(1, sizeof(**mdb)); |
| 1123 |
if (*mdb == NULL) { |
| 1124 |
krb5_set_error_message(context, ENOMEM, "malloc: out of memory"); |
| 1125 |
return ENOMEM; |
| 1126 |
} |
| 1127 |
|
| 1128 |
(*db)->hdb_db = NULL; |
| 1129 |
(*db)->hdb_name = strdup(filename); |
| 1130 |
if ((*db)->hdb_name == NULL) { |
| 1131 |
free(*db); |
| 1132 |
*db = NULL; |
| 1133 |
krb5_set_error_message(context, ENOMEM, "malloc: out of memory"); |
| 1134 |
return ENOMEM; |
| 1135 |
} |
| 1136 |
(*mdb)->do_sync = 1; |
| 1137 |
(*db)->hdb_master_key_set = 0; |
| 1138 |
(*db)->hdb_openp = 0; |
| 1139 |
(*db)->hdb_capability_flags = 0; |
| 1140 |
(*db)->hdb_open = mdb_open; |
| 1141 |
(*db)->hdb_close = mdb_close; |
| 1142 |
(*db)->hdb_fetch_kvno = mdb_fetch_kvno; |
| 1143 |
(*db)->hdb_store = mdb_store; |
| 1144 |
(*db)->hdb_remove = mdb_remove; |
| 1145 |
(*db)->hdb_firstkey = mdb_firstkey; |
| 1146 |
(*db)->hdb_nextkey= mdb_nextkey; |
| 1147 |
(*db)->hdb_lock = mdb_lock; |
| 1148 |
(*db)->hdb_unlock = mdb_unlock; |
| 1149 |
(*db)->hdb_rename = mdb_rename; |
| 1150 |
(*db)->hdb__get = mdb__get; |
| 1151 |
(*db)->hdb__put = mdb__put; |
| 1152 |
(*db)->hdb__del = mdb__del; |
| 1153 |
(*db)->hdb_destroy = mdb_destroy; |
| 1154 |
(*db)->hdb_set_sync = mdb_set_sync; |
| 1155 |
return 0; |
| 1156 |
} |
| 1157 |
|
| 1158 |
#endif /* HAVE_DB1 */ |
| 1159 |
|
| 1160 |
/* |
| 1161 |
can have any number of princ stanzas. |
| 1162 |
format is as follows (only \n indicates newlines) |
| 1163 |
princ\t%d\t (%d is KRB5_KDB_V1_BASE_LENGTH, always 38) |
| 1164 |
%d\t (strlen of principal e.g. shadow/[email protected]) |
| 1165 |
%d\t (number of tl_data) |
| 1166 |
%d\t (number of key data, e.g. how many keys for this user) |
| 1167 |
%d\t (extra data length) |
| 1168 |
%s\t (principal name) |
| 1169 |
%d\t (attributes) |
| 1170 |
%d\t (max lifetime, seconds) |
| 1171 |
%d\t (max renewable life, seconds) |
| 1172 |
%d\t (expiration, seconds since epoch or 2145830400 for never) |
| 1173 |
%d\t (password expiration, seconds, 0 for never) |
| 1174 |
%d\t (last successful auth, seconds since epoch) |
| 1175 |
%d\t (last failed auth, per above) |
| 1176 |
%d\t (failed auth count) |
| 1177 |
foreach tl_data 0 to number of tl_data - 1 as above |
| 1178 |
%d\t%d\t (data type, data length) |
| 1179 |
foreach tl_data 0 to length-1 |
| 1180 |
%02x (tl data contents[element n]) |
| 1181 |
except if tl_data length is 0 |
| 1182 |
%d (always -1) |
| 1183 |
\t |
| 1184 |
foreach key 0 to number of keys - 1 as above |
| 1185 |
%d\t%d\t (key data version, kvno) |
| 1186 |
foreach version 0 to key data version - 1 (a key or a salt) |
| 1187 |
%d\t%d\t(data type for this key, data length for this key) |
| 1188 |
foreach key data length 0 to length-1 |
| 1189 |
%02x (key data contents[element n]) |
| 1190 |
except if key_data length is 0 |
| 1191 |
%d (always -1) |
| 1192 |
\t |
| 1193 |
foreach extra data length 0 to length - 1 |
| 1194 |
%02x (extra data part) |
| 1195 |
unless no extra data |
| 1196 |
%d (always -1) |
| 1197 |
;\n |
| 1198 |
|
| 1199 |
*/ |
| 1200 |
|
| 1201 |
#if 0 |
| 1202 |
/* Why ever did we loop? */ |
| 1203 |
static char * |
| 1204 |
nexttoken(char **p) |
| 1205 |
{ |
| 1206 |
char *q; |
| 1207 |
do { |
| 1208 |
q = strsep(p, " \t"); |
| 1209 |
} while(q && *q == '\0'); |
| 1210 |
return q; |
| 1211 |
} |
| 1212 |
#endif |
| 1213 |
|
| 1214 |
static char * |
| 1215 |
nexttoken(char **p, size_t len, const char *what) |
| 1216 |
{ |
| 1217 |
char *q; |
| 1218 |
|
| 1219 |
if (*p == NULL) |
| 1220 |
return NULL; |
| 1221 |
|
| 1222 |
q = *p; |
| 1223 |
*p += len; |
| 1224 |
/* Must be followed by a delimiter (right?) */ |
| 1225 |
if (strsep(p, " \t") != q + len) { |
| 1226 |
warnx("No tokens left in dump entry while looking for %s", what); |
| 1227 |
return NULL; |
| 1228 |
} |
| 1229 |
if (*q == '\0') |
| 1230 |
warnx("Empty last token in dump entry while looking for %s", what); |
| 1231 |
return q; |
| 1232 |
} |
| 1233 |
|
| 1234 |
static size_t |
| 1235 |
getdata(char **p, unsigned char *buf, size_t len, const char *what) |
| 1236 |
{ |
| 1237 |
size_t i; |
| 1238 |
int v; |
| 1239 |
char *q = nexttoken(p, 0, what); |
| 1240 |
if (q == NULL) { |
| 1241 |
warnx("Failed to find hex-encoded binary data (%s) in dump", what); |
| 1242 |
return 0; |
| 1243 |
} |
| 1244 |
i = 0; |
| 1245 |
while (*q && i < len) { |
| 1246 |
if (sscanf(q, "%02x", &v) != 1) |
| 1247 |
break; |
| 1248 |
buf[i++] = v; |
| 1249 |
q += 2; |
| 1250 |
} |
| 1251 |
return i; |
| 1252 |
} |
| 1253 |
|
| 1254 |
static int |
| 1255 |
getint(char **p, const char *what) |
| 1256 |
{ |
| 1257 |
int val; |
| 1258 |
char *q = nexttoken(p, 0, what); |
| 1259 |
if (!q) { |
| 1260 |
warnx("Failed to find a signed integer (%s) in dump", what); |
| 1261 |
return -1; |
| 1262 |
} |
| 1263 |
if (sscanf(q, "%d", &val) != 1) |
| 1264 |
return -1; |
| 1265 |
return val; |
| 1266 |
} |
| 1267 |
|
| 1268 |
static unsigned int |
| 1269 |
getuint(char **p, const char *what) |
| 1270 |
{ |
| 1271 |
int val; |
| 1272 |
char *q = nexttoken(p, 0, what); |
| 1273 |
if (!q) { |
| 1274 |
warnx("Failed to find an unsigned integer (%s) in dump", what); |
| 1275 |
return 0; |
| 1276 |
} |
| 1277 |
if (sscanf(q, "%u", &val) != 1) |
| 1278 |
return 0; |
| 1279 |
return val; |
| 1280 |
} |
| 1281 |
|
| 1282 |
#define KRB5_KDB_SALTTYPE_NORMAL 0 |
| 1283 |
#define KRB5_KDB_SALTTYPE_V4 1 |
| 1284 |
#define KRB5_KDB_SALTTYPE_NOREALM 2 |
| 1285 |
#define KRB5_KDB_SALTTYPE_ONLYREALM 3 |
| 1286 |
#define KRB5_KDB_SALTTYPE_SPECIAL 4 |
| 1287 |
#define KRB5_KDB_SALTTYPE_AFS3 5 |
| 1288 |
|
| 1289 |
#define CHECK_UINT(num) \ |
| 1290 |
if ((num) < 0 || (num) > INT_MAX) return EINVAL |
| 1291 |
#define CHECK_UINT16(num) \ |
| 1292 |
if ((num) < 0 || (num) > 1<<15) return EINVAL |
| 1293 |
#define CHECK_NUM(num, maxv) \ |
| 1294 |
if ((num) > (maxv)) return EINVAL |
| 1295 |
|
| 1296 |
/* |
| 1297 |
* This utility function converts an MIT dump entry to an MIT on-disk |
| 1298 |
* encoded entry, which can then be decoded with _hdb_mdb_value2entry(). |
| 1299 |
* This allows us to have a single decoding function (_hdb_mdb_value2entry), |
| 1300 |
* which makes the code cleaner (less code duplication), if a bit less |
| 1301 |
* efficient. It also will allow us to have a function to dump an HDB |
| 1302 |
* entry in MIT format so we can dump HDB into MIT format for rollback |
| 1303 |
* purposes. And that will allow us to write to MIT KDBs, again |
| 1304 |
* somewhat inefficiently, also for migration/rollback purposes. |
| 1305 |
*/ |
| 1306 |
int |
| 1307 |
_hdb_mit_dump2mitdb_entry(krb5_context context, char *line, krb5_storage *sp) |
| 1308 |
{ |
| 1309 |
krb5_error_code ret = EINVAL; |
| 1310 |
char *p = line, *q; |
| 1311 |
char *princ; |
| 1312 |
ssize_t sz; |
| 1313 |
size_t i; |
| 1314 |
size_t princ_len; |
| 1315 |
unsigned int num_tl_data; |
| 1316 |
size_t num_key_data; |
| 1317 |
unsigned int attributes; |
| 1318 |
int tmp; |
| 1319 |
|
| 1320 |
krb5_storage_set_byteorder(sp, KRB5_STORAGE_BYTEORDER_LE); |
| 1321 |
|
| 1322 |
q = nexttoken(&p, 0, "record type (princ or policy)"); |
| 1323 |
if (strcmp(q, "kdb5_util") == 0 || strcmp(q, "policy") == 0 || |
| 1324 |
strcmp(q, "princ") != 0) { |
| 1325 |
warnx("Supposed MIT dump entry does not start with 'kdb5_util', " |
| 1326 |
"'policy', nor 'princ'"); |
| 1327 |
return -1; |
| 1328 |
} |
| 1329 |
if (getint(&p, "constant '38'") != 38) { |
| 1330 |
warnx("Dump entry does not start with '38<TAB>'"); |
| 1331 |
return EINVAL; |
| 1332 |
} |
| 1333 |
#define KDB_V1_BASE_LENGTH 38 |
| 1334 |
ret = krb5_store_int16(sp, KDB_V1_BASE_LENGTH); |
| 1335 |
if (ret) return ret; |
| 1336 |
|
| 1337 |
princ_len = getuint(&p, "principal name length"); |
| 1338 |
if (princ_len > (1<<15) - 1) { |
| 1339 |
warnx("Principal name in dump entry too long (%llu)", |
| 1340 |
(unsigned long long)princ_len); |
| 1341 |
return EINVAL; |
| 1342 |
} |
| 1343 |
num_tl_data = getuint(&p, "number of TL data"); |
| 1344 |
num_key_data = getuint(&p, "number of key data"); |
| 1345 |
getint(&p, "5th field, length of 'extra data'"); |
| 1346 |
princ = nexttoken(&p, (int)princ_len, "principal name"); |
| 1347 |
if (princ == NULL) { |
| 1348 |
warnx("Failed to read principal name (expected length %llu)", |
| 1349 |
(unsigned long long)princ_len); |
| 1350 |
return -1; |
| 1351 |
} |
| 1352 |
|
| 1353 |
attributes = getuint(&p, "attributes"); |
| 1354 |
ret = krb5_store_uint32(sp, attributes); |
| 1355 |
if (ret) return ret; |
| 1356 |
|
| 1357 |
tmp = getint(&p, "max life"); |
| 1358 |
CHECK_UINT(tmp); |
| 1359 |
ret = krb5_store_uint32(sp, tmp); |
| 1360 |
if (ret) return ret; |
| 1361 |
|
| 1362 |
tmp = getint(&p, "max renewable life"); |
| 1363 |
CHECK_UINT(tmp); |
| 1364 |
ret = krb5_store_uint32(sp, tmp); |
| 1365 |
if (ret) return ret; |
| 1366 |
|
| 1367 |
tmp = getint(&p, "expiration"); |
| 1368 |
CHECK_UINT(tmp); |
| 1369 |
ret = krb5_store_uint32(sp, tmp); |
| 1370 |
if (ret) return ret; |
| 1371 |
|
| 1372 |
tmp = getint(&p, "pw expiration"); |
| 1373 |
CHECK_UINT(tmp); |
| 1374 |
ret = krb5_store_uint32(sp, tmp); |
| 1375 |
if (ret) return ret; |
| 1376 |
|
| 1377 |
tmp = getint(&p, "last auth"); |
| 1378 |
CHECK_UINT(tmp); |
| 1379 |
ret = krb5_store_uint32(sp, tmp); |
| 1380 |
if (ret) return ret; |
| 1381 |
|
| 1382 |
tmp = getint(&p, "last failed auth"); |
| 1383 |
CHECK_UINT(tmp); |
| 1384 |
ret = krb5_store_uint32(sp, tmp); |
| 1385 |
if (ret) return ret; |
| 1386 |
|
| 1387 |
tmp = getint(&p,"fail auth count"); |
| 1388 |
CHECK_UINT(tmp); |
| 1389 |
ret = krb5_store_uint32(sp, tmp); |
| 1390 |
if (ret) return ret; |
| 1391 |
|
| 1392 |
/* add TL data count */ |
| 1393 |
CHECK_NUM(num_tl_data, 1023); |
| 1394 |
ret = krb5_store_uint16(sp, num_tl_data); |
| 1395 |
if (ret) return ret; |
| 1396 |
|
| 1397 |
/* add key count */ |
| 1398 |
CHECK_NUM(num_key_data, 1023); |
| 1399 |
ret = krb5_store_uint16(sp, num_key_data); |
| 1400 |
if (ret) return ret; |
| 1401 |
|
| 1402 |
/* add principal unparsed name length and unparsed name */ |
| 1403 |
princ_len = strlen(princ); |
| 1404 |
princ_len++; /* must count and write the NUL in the on-disk encoding */ |
| 1405 |
ret = krb5_store_uint16(sp, princ_len); |
| 1406 |
if (ret) return ret; |
| 1407 |
sz = krb5_storage_write(sp, princ, princ_len); |
| 1408 |
if (sz == -1) return ENOMEM; |
| 1409 |
|
| 1410 |
/* scan and write TL data */ |
| 1411 |
for (i = 0; i < num_tl_data; i++) { |
| 1412 |
char *reading_what; |
| 1413 |
int tl_type, tl_length; |
| 1414 |
unsigned char *buf; |
| 1415 |
|
| 1416 |
tl_type = getint(&p, "TL data type"); |
| 1417 |
tl_length = getint(&p, "data length"); |
| 1418 |
|
| 1419 |
if (asprintf(&reading_what, "TL data type %d (length %d)", |
| 1420 |
tl_type, tl_length) < 0) |
| 1421 |
return ENOMEM; |
| 1422 |
|
| 1423 |
/* |
| 1424 |
* XXX Leaking reading_what, but only on ENOMEM cases anyways, |
| 1425 |
* so we don't care. |
| 1426 |
*/ |
| 1427 |
CHECK_UINT16(tl_type); |
| 1428 |
ret = krb5_store_uint16(sp, tl_type); |
| 1429 |
if (ret) return ret; |
| 1430 |
CHECK_UINT16(tl_length); |
| 1431 |
ret = krb5_store_uint16(sp, tl_length); |
| 1432 |
if (ret) return ret; |
| 1433 |
|
| 1434 |
if (tl_length) { |
| 1435 |
buf = malloc(tl_length); |
| 1436 |
if (!buf) return ENOMEM; |
| 1437 |
if (getdata(&p, buf, tl_length, reading_what) != tl_length) |
| 1438 |
return EINVAL; |
| 1439 |
sz = krb5_storage_write(sp, buf, tl_length); |
| 1440 |
free(buf); |
| 1441 |
if (sz == -1) return ENOMEM; |
| 1442 |
} else { |
| 1443 |
if (strcmp(nexttoken(&p, 0, "'-1' field"), "-1") != 0) return EINVAL; |
| 1444 |
} |
| 1445 |
free(reading_what); |
| 1446 |
} |
| 1447 |
|
| 1448 |
for (i = 0; i < num_key_data; i++) { |
| 1449 |
unsigned char *buf; |
| 1450 |
int key_versions; |
| 1451 |
int kvno; |
| 1452 |
int keytype; |
| 1453 |
int keylen; |
| 1454 |
size_t k; |
| 1455 |
|
| 1456 |
key_versions = getint(&p, "key data 'version'"); |
| 1457 |
CHECK_UINT16(key_versions); |
| 1458 |
ret = krb5_store_int16(sp, key_versions); |
| 1459 |
if (ret) return ret; |
| 1460 |
|
| 1461 |
kvno = getint(&p, "kvno"); |
| 1462 |
CHECK_UINT16(kvno); |
| 1463 |
ret = krb5_store_int16(sp, kvno); |
| 1464 |
if (ret) return ret; |
| 1465 |
|
| 1466 |
for (k = 0; k < key_versions; k++) { |
| 1467 |
keytype = getint(&p, "enctype"); |
| 1468 |
CHECK_UINT16(keytype); |
| 1469 |
ret = krb5_store_int16(sp, keytype); |
| 1470 |
if (ret) return ret; |
| 1471 |
|
| 1472 |
keylen = getint(&p, "encrypted key length"); |
| 1473 |
CHECK_UINT16(keylen); |
| 1474 |
ret = krb5_store_int16(sp, keylen); |
| 1475 |
if (ret) return ret; |
| 1476 |
|
| 1477 |
if (keylen) { |
| 1478 |
buf = malloc(keylen); |
| 1479 |
if (!buf) return ENOMEM; |
| 1480 |
if (getdata(&p, buf, keylen, "key (or salt) data") != keylen) |
| 1481 |
return EINVAL; |
| 1482 |
sz = krb5_storage_write(sp, buf, keylen); |
| 1483 |
free(buf); |
| 1484 |
if (sz == -1) return ENOMEM; |
| 1485 |
} else { |
| 1486 |
if (strcmp(nexttoken(&p, 0, |
| 1487 |
"'-1' zero-length key/salt field"), |
| 1488 |
"-1") != 0) { |
| 1489 |
warnx("Expected '-1' field because key/salt length is 0"); |
| 1490 |
return -1; |
| 1491 |
} |
| 1492 |
} |
| 1493 |
} |
| 1494 |
} |
| 1495 |
/* |
| 1496 |
* The rest is "extra data", but there's never any and we wouldn't |
| 1497 |
* know what to do with it. |
| 1498 |
*/ |
| 1499 |
/* nexttoken(&p, 0, "extra data"); */ |
| 1500 |
return 0; |
| 1501 |
} |
| 1502 |
|