| 1 |
/* |
| 2 |
* Copyright (c) 1997 - 2006 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 <krb5_locl.h> |
| 35 |
#include "kadm5_locl.h" |
| 36 |
#include <assert.h> |
| 37 |
|
| 38 |
RCSID("$Id$"); |
| 39 |
|
| 40 |
static kadm5_ret_t |
| 41 |
add_tl_data(kadm5_principal_ent_t ent, int16_t type, |
| 42 |
const void *data, size_t size) |
| 43 |
{ |
| 44 |
krb5_tl_data *tl; |
| 45 |
|
| 46 |
tl = calloc(1, sizeof(*tl)); |
| 47 |
if (tl == NULL) |
| 48 |
return _kadm5_error_code(ENOMEM); |
| 49 |
|
| 50 |
tl->tl_data_type = type; |
| 51 |
tl->tl_data_length = size; |
| 52 |
tl->tl_data_contents = malloc(size); |
| 53 |
if (tl->tl_data_contents == NULL && size != 0) { |
| 54 |
free(tl); |
| 55 |
return _kadm5_error_code(ENOMEM); |
| 56 |
} |
| 57 |
memcpy(tl->tl_data_contents, data, size); |
| 58 |
|
| 59 |
tl->tl_data_next = ent->tl_data; |
| 60 |
ent->tl_data = tl; |
| 61 |
ent->n_tl_data++; |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
static |
| 67 |
krb5_error_code |
| 68 |
copy_keyset_to_kadm5(kadm5_server_context *context, krb5_kvno kvno, |
| 69 |
size_t n_keys, Key *keys, krb5_salt *salt, |
| 70 |
kadm5_principal_ent_t out) |
| 71 |
{ |
| 72 |
size_t i; |
| 73 |
Key *key; |
| 74 |
krb5_key_data *kd; |
| 75 |
krb5_data *sp; |
| 76 |
krb5_error_code ret = 0; |
| 77 |
|
| 78 |
for (i = 0; i < n_keys; i++) { |
| 79 |
key = &keys[i]; |
| 80 |
kd = &out->key_data[out->n_key_data]; |
| 81 |
kd->key_data_ver = 2; |
| 82 |
kd->key_data_kvno = kvno; |
| 83 |
kd->key_data_type[0] = key->key.keytype; |
| 84 |
if(key->salt) |
| 85 |
kd->key_data_type[1] = key->salt->type; |
| 86 |
else |
| 87 |
kd->key_data_type[1] = KRB5_PADATA_PW_SALT; |
| 88 |
/* setup key */ |
| 89 |
kd->key_data_length[0] = key->key.keyvalue.length; |
| 90 |
kd->key_data_contents[0] = malloc(kd->key_data_length[0]); |
| 91 |
if(kd->key_data_contents[0] == NULL && kd->key_data_length[0] != 0){ |
| 92 |
ret = ENOMEM; |
| 93 |
break; |
| 94 |
} |
| 95 |
memcpy(kd->key_data_contents[0], key->key.keyvalue.data, |
| 96 |
kd->key_data_length[0]); |
| 97 |
/* setup salt */ |
| 98 |
if(key->salt) |
| 99 |
sp = &key->salt->salt; |
| 100 |
else |
| 101 |
sp = &salt->saltvalue; |
| 102 |
kd->key_data_length[1] = sp->length; |
| 103 |
kd->key_data_contents[1] = malloc(kd->key_data_length[1]); |
| 104 |
if(kd->key_data_length[1] != 0 |
| 105 |
&& kd->key_data_contents[1] == NULL) { |
| 106 |
memset(kd->key_data_contents[0], 0, kd->key_data_length[0]); |
| 107 |
ret = ENOMEM; |
| 108 |
break; |
| 109 |
} |
| 110 |
memcpy(kd->key_data_contents[1], sp->data, kd->key_data_length[1]); |
| 111 |
out->n_key_data++; |
| 112 |
} |
| 113 |
|
| 114 |
return ret; |
| 115 |
} |
| 116 |
|
| 117 |
kadm5_ret_t |
| 118 |
kadm5_s_get_principal(void *server_handle, |
| 119 |
krb5_principal princ, |
| 120 |
kadm5_principal_ent_t out, |
| 121 |
uint32_t mask) |
| 122 |
{ |
| 123 |
kadm5_server_context *context = server_handle; |
| 124 |
kadm5_ret_t ret; |
| 125 |
hdb_entry_ex ent; |
| 126 |
|
| 127 |
memset(&ent, 0, sizeof(ent)); |
| 128 |
memset(out, 0, sizeof(*out)); |
| 129 |
|
| 130 |
if (!context->keep_open) { |
| 131 |
ret = context->db->hdb_open(context->context, context->db, O_RDONLY, 0); |
| 132 |
if (ret) |
| 133 |
return ret; |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
* We may want to attempt to recover the log on read operations, but we |
| 138 |
* because the HDB/log lock order is reversed on slaves, in order to avoid |
| 139 |
* lock contention from kadm5srv apps we need to make sure that the the HDB |
| 140 |
* open for read-write is optimistic and attempts only a non-blocking lock, |
| 141 |
* and if it doesn't get it then it should fallback to read-only. But we |
| 142 |
* don't have that option in the hdb_open() interface at this time. |
| 143 |
* |
| 144 |
* For now we won't attempt to recover the log. |
| 145 |
*/ |
| 146 |
|
| 147 |
ret = context->db->hdb_fetch_kvno(context->context, context->db, princ, |
| 148 |
HDB_F_DECRYPT|HDB_F_ALL_KVNOS| |
| 149 |
HDB_F_GET_ANY|HDB_F_ADMIN_DATA, 0, &ent); |
| 150 |
|
| 151 |
if (!context->keep_open) |
| 152 |
context->db->hdb_close(context->context, context->db); |
| 153 |
if(ret) |
| 154 |
return _kadm5_error_code(ret); |
| 155 |
|
| 156 |
if(mask & KADM5_PRINCIPAL) |
| 157 |
ret = krb5_copy_principal(context->context, ent.entry.principal, |
| 158 |
&out->principal); |
| 159 |
if(ret) |
| 160 |
goto out; |
| 161 |
if(mask & KADM5_PRINC_EXPIRE_TIME && ent.entry.valid_end) |
| 162 |
out->princ_expire_time = *ent.entry.valid_end; |
| 163 |
if(mask & KADM5_PW_EXPIRATION && ent.entry.pw_end) |
| 164 |
out->pw_expiration = *ent.entry.pw_end; |
| 165 |
if(mask & KADM5_LAST_PWD_CHANGE) |
| 166 |
hdb_entry_get_pw_change_time(&ent.entry, &out->last_pwd_change); |
| 167 |
if(mask & KADM5_ATTRIBUTES){ |
| 168 |
out->attributes |= ent.entry.flags.postdate ? 0 : KRB5_KDB_DISALLOW_POSTDATED; |
| 169 |
out->attributes |= ent.entry.flags.forwardable ? 0 : KRB5_KDB_DISALLOW_FORWARDABLE; |
| 170 |
out->attributes |= ent.entry.flags.initial ? KRB5_KDB_DISALLOW_TGT_BASED : 0; |
| 171 |
out->attributes |= ent.entry.flags.renewable ? 0 : KRB5_KDB_DISALLOW_RENEWABLE; |
| 172 |
out->attributes |= ent.entry.flags.proxiable ? 0 : KRB5_KDB_DISALLOW_PROXIABLE; |
| 173 |
out->attributes |= ent.entry.flags.invalid ? KRB5_KDB_DISALLOW_ALL_TIX : 0; |
| 174 |
out->attributes |= ent.entry.flags.require_preauth ? KRB5_KDB_REQUIRES_PRE_AUTH : 0; |
| 175 |
out->attributes |= ent.entry.flags.require_pwchange ? KRB5_KDB_REQUIRES_PWCHANGE : 0; |
| 176 |
out->attributes |= ent.entry.flags.server ? 0 : KRB5_KDB_DISALLOW_SVR; |
| 177 |
out->attributes |= ent.entry.flags.change_pw ? KRB5_KDB_PWCHANGE_SERVICE : 0; |
| 178 |
out->attributes |= ent.entry.flags.ok_as_delegate ? KRB5_KDB_OK_AS_DELEGATE : 0; |
| 179 |
out->attributes |= ent.entry.flags.trusted_for_delegation ? KRB5_KDB_TRUSTED_FOR_DELEGATION : 0; |
| 180 |
out->attributes |= ent.entry.flags.allow_kerberos4 ? KRB5_KDB_ALLOW_KERBEROS4 : 0; |
| 181 |
out->attributes |= ent.entry.flags.allow_digest ? KRB5_KDB_ALLOW_DIGEST : 0; |
| 182 |
} |
| 183 |
if(mask & KADM5_MAX_LIFE) { |
| 184 |
if(ent.entry.max_life) |
| 185 |
out->max_life = *ent.entry.max_life; |
| 186 |
else |
| 187 |
out->max_life = INT_MAX; |
| 188 |
} |
| 189 |
if(mask & KADM5_MOD_TIME) { |
| 190 |
if(ent.entry.modified_by) |
| 191 |
out->mod_date = ent.entry.modified_by->time; |
| 192 |
else |
| 193 |
out->mod_date = ent.entry.created_by.time; |
| 194 |
} |
| 195 |
if(mask & KADM5_MOD_NAME) { |
| 196 |
if(ent.entry.modified_by) { |
| 197 |
if (ent.entry.modified_by->principal != NULL) |
| 198 |
ret = krb5_copy_principal(context->context, |
| 199 |
ent.entry.modified_by->principal, |
| 200 |
&out->mod_name); |
| 201 |
} else if(ent.entry.created_by.principal != NULL) |
| 202 |
ret = krb5_copy_principal(context->context, |
| 203 |
ent.entry.created_by.principal, |
| 204 |
&out->mod_name); |
| 205 |
else |
| 206 |
out->mod_name = NULL; |
| 207 |
} |
| 208 |
if(ret) |
| 209 |
goto out; |
| 210 |
|
| 211 |
if(mask & KADM5_KVNO) |
| 212 |
out->kvno = ent.entry.kvno; |
| 213 |
if(mask & KADM5_MKVNO) { |
| 214 |
size_t n; |
| 215 |
out->mkvno = 0; /* XXX */ |
| 216 |
for(n = 0; n < ent.entry.keys.len; n++) |
| 217 |
if(ent.entry.keys.val[n].mkvno) { |
| 218 |
out->mkvno = *ent.entry.keys.val[n].mkvno; /* XXX this isn't right */ |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
#if 0 /* XXX implement */ |
| 223 |
if(mask & KADM5_AUX_ATTRIBUTES) |
| 224 |
; |
| 225 |
if(mask & KADM5_LAST_SUCCESS) |
| 226 |
; |
| 227 |
if(mask & KADM5_LAST_FAILED) |
| 228 |
; |
| 229 |
if(mask & KADM5_FAIL_AUTH_COUNT) |
| 230 |
; |
| 231 |
#endif |
| 232 |
if(mask & KADM5_POLICY) { |
| 233 |
HDB_extension *ext; |
| 234 |
|
| 235 |
ext = hdb_find_extension(&ent.entry, choice_HDB_extension_data_policy); |
| 236 |
if (ext == NULL) { |
| 237 |
out->policy = strdup("default"); |
| 238 |
/* It's OK if we retun NULL instead of "default" */ |
| 239 |
} else { |
| 240 |
out->policy = strdup(ext->data.u.policy); |
| 241 |
if (out->policy == NULL) { |
| 242 |
ret = ENOMEM; |
| 243 |
goto out; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
if(mask & KADM5_MAX_RLIFE) { |
| 248 |
if(ent.entry.max_renew) |
| 249 |
out->max_renewable_life = *ent.entry.max_renew; |
| 250 |
else |
| 251 |
out->max_renewable_life = INT_MAX; |
| 252 |
} |
| 253 |
if(mask & KADM5_KEY_DATA){ |
| 254 |
size_t i; |
| 255 |
size_t n_keys = ent.entry.keys.len; |
| 256 |
krb5_salt salt; |
| 257 |
HDB_extension *ext; |
| 258 |
HDB_Ext_KeySet *hist_keys = NULL; |
| 259 |
|
| 260 |
/* Don't return stale keys to kadm5 clients */ |
| 261 |
ret = hdb_prune_keys(context->context, &ent.entry); |
| 262 |
if (ret) |
| 263 |
goto out; |
| 264 |
ext = hdb_find_extension(&ent.entry, choice_HDB_extension_data_hist_keys); |
| 265 |
if (ext != NULL) |
| 266 |
hist_keys = &ext->data.u.hist_keys; |
| 267 |
|
| 268 |
krb5_get_pw_salt(context->context, ent.entry.principal, &salt); |
| 269 |
for (i = 0; hist_keys != NULL && i < hist_keys->len; i++) |
| 270 |
n_keys += hist_keys->val[i].keys.len; |
| 271 |
out->key_data = malloc(n_keys * sizeof(*out->key_data)); |
| 272 |
if (out->key_data == NULL && n_keys != 0) { |
| 273 |
ret = ENOMEM; |
| 274 |
goto out; |
| 275 |
} |
| 276 |
out->n_key_data = 0; |
| 277 |
ret = copy_keyset_to_kadm5(context, ent.entry.kvno, ent.entry.keys.len, |
| 278 |
ent.entry.keys.val, &salt, out); |
| 279 |
if (ret) |
| 280 |
goto out; |
| 281 |
for (i = 0; hist_keys != NULL && i < hist_keys->len; i++) { |
| 282 |
ret = copy_keyset_to_kadm5(context, hist_keys->val[i].kvno, |
| 283 |
hist_keys->val[i].keys.len, |
| 284 |
hist_keys->val[i].keys.val, |
| 285 |
&salt, out); |
| 286 |
if (ret) |
| 287 |
goto out; |
| 288 |
} |
| 289 |
krb5_free_salt(context->context, salt); |
| 290 |
assert( out->n_key_data == n_keys ); |
| 291 |
} |
| 292 |
if (ret) |
| 293 |
goto out; |
| 294 |
if(mask & KADM5_TL_DATA) { |
| 295 |
time_t last_pw_expire; |
| 296 |
const HDB_Ext_PKINIT_acl *acl; |
| 297 |
const HDB_Ext_Aliases *aliases; |
| 298 |
|
| 299 |
ret = hdb_entry_get_pw_change_time(&ent.entry, &last_pw_expire); |
| 300 |
if (ret == 0 && last_pw_expire) { |
| 301 |
unsigned char buf[4]; |
| 302 |
_krb5_put_int(buf, last_pw_expire, sizeof(buf)); |
| 303 |
ret = add_tl_data(out, KRB5_TL_LAST_PWD_CHANGE, buf, sizeof(buf)); |
| 304 |
} |
| 305 |
if (ret) |
| 306 |
goto out; |
| 307 |
/* |
| 308 |
* If the client was allowed to get key data, let it have the |
| 309 |
* password too. |
| 310 |
*/ |
| 311 |
if (mask & KADM5_KEY_DATA) { |
| 312 |
heim_utf8_string pw; |
| 313 |
|
| 314 |
ret = hdb_entry_get_password(context->context, |
| 315 |
context->db, &ent.entry, &pw); |
| 316 |
if (ret == 0) { |
| 317 |
(void) add_tl_data(out, KRB5_TL_PASSWORD, pw, strlen(pw) + 1); |
| 318 |
free(pw); |
| 319 |
} |
| 320 |
krb5_clear_error_message(context->context); |
| 321 |
} |
| 322 |
|
| 323 |
ret = hdb_entry_get_pkinit_acl(&ent.entry, &acl); |
| 324 |
if (ret == 0 && acl) { |
| 325 |
krb5_data buf; |
| 326 |
size_t len; |
| 327 |
|
| 328 |
ASN1_MALLOC_ENCODE(HDB_Ext_PKINIT_acl, buf.data, buf.length, |
| 329 |
acl, &len, ret); |
| 330 |
if (ret) |
| 331 |
goto out; |
| 332 |
if (len != buf.length) |
| 333 |
krb5_abortx(context->context, |
| 334 |
"internal ASN.1 encoder error"); |
| 335 |
ret = add_tl_data(out, KRB5_TL_PKINIT_ACL, buf.data, buf.length); |
| 336 |
free(buf.data); |
| 337 |
if (ret) |
| 338 |
goto out; |
| 339 |
} |
| 340 |
if (ret) |
| 341 |
goto out; |
| 342 |
|
| 343 |
ret = hdb_entry_get_aliases(&ent.entry, &aliases); |
| 344 |
if (ret == 0 && aliases) { |
| 345 |
krb5_data buf; |
| 346 |
size_t len; |
| 347 |
|
| 348 |
ASN1_MALLOC_ENCODE(HDB_Ext_Aliases, buf.data, buf.length, |
| 349 |
aliases, &len, ret); |
| 350 |
if (ret) |
| 351 |
goto out; |
| 352 |
if (len != buf.length) |
| 353 |
krb5_abortx(context->context, |
| 354 |
"internal ASN.1 encoder error"); |
| 355 |
ret = add_tl_data(out, KRB5_TL_ALIASES, buf.data, buf.length); |
| 356 |
free(buf.data); |
| 357 |
if (ret) |
| 358 |
goto out; |
| 359 |
} |
| 360 |
if (ret) |
| 361 |
goto out; |
| 362 |
} |
| 363 |
|
| 364 |
out: |
| 365 |
if (ret) |
| 366 |
kadm5_free_principal_ent(context, out); |
| 367 |
hdb_free_entry(context->context, &ent); |
| 368 |
|
| 369 |
return _kadm5_error_code(ret); |
| 370 |
} |