hdb: Move virtual principals into HDB layer
This is a large commit that adds several features: - Revamps and moves virtual host-based service principal functionality from kdc/ to lib/hdb/ so that it may be automatically visible to lib/kadm5/, as well as kadmin(1)/kadmind(8) and ktutil(1). The changes are backwards-incompatible. - Completes support for documenting a service principal's supported enctypes in its HDB entry independently of its long-term keys. This will reduce HDB bloat by not requiring that service principals have more long-term keys than they need just to document the service's supported enctypes. - Adds support for storing krb5.conf content in principals' HDB entries. This may eventually be used for causing Heimdal KDC services to reconfigure primary/secondary roles automatically by discovering the configured primary in an HDB entry for the realm. For now this will be used to help reduce the amount of configuration needed by clients of an upcoming HTTP binding of the kadmin service.
This commit is contained in:
355
kadmin/mod.c
355
kadmin/mod.c
@@ -34,7 +34,7 @@
|
||||
#include "kadmin_locl.h"
|
||||
#include "kadmin-commands.h"
|
||||
|
||||
static void
|
||||
void
|
||||
add_tl(kadm5_principal_ent_rec *princ, int type, krb5_data *data)
|
||||
{
|
||||
krb5_tl_data *tl, **ptl;
|
||||
@@ -185,6 +185,43 @@ add_pkinit_acl(krb5_context contextp, kadm5_principal_ent_rec *princ,
|
||||
add_tl(princ, KRB5_TL_EXTENSION, &buf);
|
||||
}
|
||||
|
||||
static krb5_error_code
|
||||
add_etypes(krb5_context contextp,
|
||||
kadm5_principal_ent_rec *princ,
|
||||
struct getarg_strings *strings)
|
||||
{
|
||||
krb5_error_code ret = 0;
|
||||
HDB_EncTypeList etypes;
|
||||
krb5_data buf;
|
||||
size_t i, size;
|
||||
|
||||
etypes.len = strings->num_strings;
|
||||
if ((etypes.val = calloc(strings->num_strings,
|
||||
sizeof(etypes.val[0]))) == NULL)
|
||||
krb5_err(contextp, 1, ret, "Out of memory");
|
||||
|
||||
for (i = 0; i < strings->num_strings; i++) {
|
||||
krb5_enctype etype;
|
||||
|
||||
ret = krb5_string_to_enctype(contextp, strings->strings[i], &etype);
|
||||
if (ret) {
|
||||
krb5_warn(contextp, ret, "Could not parse enctype %s",
|
||||
strings->strings[i]);
|
||||
return ret;
|
||||
}
|
||||
etypes.val[i] = etype;
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
ASN1_MALLOC_ENCODE(HDB_EncTypeList, buf.data, buf.length,
|
||||
&etypes, &size, ret);
|
||||
}
|
||||
if (ret || buf.length != size)
|
||||
abort();
|
||||
add_tl(princ, KRB5_TL_ETYPES, &buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
add_kvno_diff(krb5_context contextp, kadm5_principal_ent_rec *princ,
|
||||
int is_svc_diff, krb5_kvno kvno_diff)
|
||||
@@ -216,6 +253,35 @@ add_kvno_diff(krb5_context contextp, kadm5_principal_ent_rec *princ,
|
||||
add_tl(princ, KRB5_TL_EXTENSION, &buf);
|
||||
}
|
||||
|
||||
static void
|
||||
add_krb5_config(kadm5_principal_ent_rec *princ, const char *fname)
|
||||
{
|
||||
HDB_extension ext;
|
||||
krb5_data buf;
|
||||
size_t size;
|
||||
int ret;
|
||||
|
||||
memset(&ext, 0, sizeof(ext));
|
||||
ext.mandatory = FALSE;
|
||||
ext.data.element = choice_HDB_extension_data_krb5_config;
|
||||
|
||||
if ((ret = rk_undumpdata(fname,
|
||||
&ext.data.u.krb5_config.data,
|
||||
&ext.data.u.krb5_config.length))) {
|
||||
krb5_warn(context, ret, "Could not read %s", fname);
|
||||
return;
|
||||
}
|
||||
|
||||
ASN1_MALLOC_ENCODE(HDB_extension, buf.data, buf.length,
|
||||
&ext, &size, ret);
|
||||
free_HDB_extension(&ext);
|
||||
if (ret)
|
||||
abort();
|
||||
if (buf.length != size)
|
||||
abort();
|
||||
add_tl(princ, KRB5_TL_EXTENSION, &buf);
|
||||
}
|
||||
|
||||
static int
|
||||
do_mod_entry(krb5_principal principal, void *data)
|
||||
{
|
||||
@@ -240,9 +306,11 @@ do_mod_entry(krb5_principal principal, void *data)
|
||||
e->attributes_string ||
|
||||
e->policy_string ||
|
||||
e->kvno_integer != -1 ||
|
||||
e->service_enctypes_strings.num_strings ||
|
||||
e->constrained_delegation_strings.num_strings ||
|
||||
e->alias_strings.num_strings ||
|
||||
e->pkinit_acl_strings.num_strings ||
|
||||
e->krb5_config_file_string ||
|
||||
e->hist_kvno_diff_clnt_integer != -1 ||
|
||||
e->hist_kvno_diff_svc_integer != -1) {
|
||||
ret = set_entry(context, &princ, &mask,
|
||||
@@ -269,6 +337,10 @@ do_mod_entry(krb5_principal principal, void *data)
|
||||
add_pkinit_acl(context, &princ, &e->pkinit_acl_strings);
|
||||
mask |= KADM5_TL_DATA;
|
||||
}
|
||||
if (e->service_enctypes_strings.num_strings) {
|
||||
ret = add_etypes(context, &princ, &e->service_enctypes_strings);
|
||||
mask |= KADM5_TL_DATA;
|
||||
}
|
||||
if (e->hist_kvno_diff_clnt_integer != -1) {
|
||||
add_kvno_diff(context, &princ, 0, e->hist_kvno_diff_clnt_integer);
|
||||
mask |= KADM5_TL_DATA;
|
||||
@@ -277,6 +349,10 @@ do_mod_entry(krb5_principal principal, void *data)
|
||||
add_kvno_diff(context, &princ, 1, e->hist_kvno_diff_svc_integer);
|
||||
mask |= KADM5_TL_DATA;
|
||||
}
|
||||
if (e->krb5_config_file_string) {
|
||||
add_krb5_config(&princ, e->krb5_config_file_string);
|
||||
mask |= KADM5_TL_DATA;
|
||||
}
|
||||
} else
|
||||
ret = edit_entry(&princ, &mask, NULL, 0);
|
||||
if(ret == 0) {
|
||||
@@ -303,3 +379,280 @@ mod_entry(struct modify_options *opt, int argc, char **argv)
|
||||
return ret != 0;
|
||||
}
|
||||
|
||||
static int
|
||||
do_mod_ns_entry(krb5_principal principal, void *data)
|
||||
{
|
||||
krb5_error_code ret;
|
||||
kadm5_principal_ent_rec princ;
|
||||
int mask = 0;
|
||||
struct modify_namespace_options *e = data;
|
||||
|
||||
memset (&princ, 0, sizeof(princ));
|
||||
ret = kadm5_get_principal(kadm_handle, principal, &princ,
|
||||
KADM5_PRINCIPAL | KADM5_ATTRIBUTES |
|
||||
KADM5_MAX_LIFE | KADM5_MAX_RLIFE |
|
||||
KADM5_PRINC_EXPIRE_TIME |
|
||||
KADM5_PW_EXPIRATION);
|
||||
if(ret)
|
||||
return ret;
|
||||
|
||||
if(e->max_ticket_life_string ||
|
||||
e->max_renewable_life_string ||
|
||||
e->attributes_string ||
|
||||
e->enctypes_strings.num_strings ||
|
||||
e->krb5_config_file_string) {
|
||||
ret = set_entry(context, &princ, &mask, e->max_ticket_life_string,
|
||||
e->max_renewable_life_string, NULL, NULL,
|
||||
e->attributes_string, NULL);
|
||||
if (e->enctypes_strings.num_strings) {
|
||||
ret = add_etypes(context, &princ, &e->enctypes_strings);
|
||||
mask |= KADM5_TL_DATA;
|
||||
}
|
||||
if (e->krb5_config_file_string) {
|
||||
add_krb5_config(&princ, e->krb5_config_file_string);
|
||||
mask |= KADM5_TL_DATA;
|
||||
}
|
||||
} else
|
||||
ret = edit_entry(&princ, &mask, NULL, 0);
|
||||
if(ret == 0) {
|
||||
ret = kadm5_modify_principal(kadm_handle, &princ, mask);
|
||||
if(ret)
|
||||
krb5_warn(context, ret, "kadm5_modify_principal");
|
||||
}
|
||||
|
||||
kadm5_free_principal_ent(kadm_handle, &princ);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
modify_namespace(struct modify_namespace_options *opt, int argc, char **argv)
|
||||
{
|
||||
krb5_error_code ret = 0;
|
||||
int i;
|
||||
|
||||
for(i = 0; i < argc; i++) {
|
||||
ret = foreach_principal(argv[i], do_mod_ns_entry, "mod_ns", opt);
|
||||
if (ret)
|
||||
break;
|
||||
}
|
||||
return ret != 0;
|
||||
}
|
||||
|
||||
#if 0
|
||||
struct modify_namespace_key_rotation_options {
|
||||
int force_flag;
|
||||
int keep_base_key_flag;
|
||||
char* revoke_old_string;
|
||||
char* new_key_rotation_epoch_string;
|
||||
char* new_key_rotation_period_string;
|
||||
};
|
||||
#endif
|
||||
|
||||
static int
|
||||
princ2kstuple(kadm5_principal_ent_rec *princ,
|
||||
unsigned int kvno,
|
||||
krb5_key_salt_tuple **kstuple,
|
||||
size_t *nkstuple)
|
||||
{
|
||||
krb5_error_code ret = 0;
|
||||
HDB_EncTypeList etypes;
|
||||
krb5_tl_data *tl;
|
||||
size_t i;
|
||||
|
||||
*kstuple = 0;
|
||||
*nkstuple = 0;
|
||||
etypes.len = 0;
|
||||
etypes.val = 0;
|
||||
for (tl = princ->tl_data; tl; tl = tl->tl_data_next) {
|
||||
if (tl->tl_data_type != KRB5_TL_ETYPES || tl->tl_data_length < 0)
|
||||
continue;
|
||||
ret = decode_HDB_EncTypeList(tl->tl_data_contents, tl->tl_data_length,
|
||||
&etypes, NULL);
|
||||
if (ret)
|
||||
break;
|
||||
*nkstuple = etypes.len;
|
||||
*kstuple = ecalloc(etypes.len, sizeof(kstuple[0][0]));
|
||||
for (i = 0; i < etypes.len; i++) {
|
||||
(*kstuple)[i].ks_enctype = etypes.val[i];
|
||||
(*kstuple)[i].ks_salttype = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (princ->n_key_data > 0) {
|
||||
*kstuple = ecalloc(1, sizeof(kstuple[0][0]));
|
||||
*nkstuple = 1;
|
||||
for (i = 0; i < princ->n_key_data; i++) {
|
||||
if (princ->key_data->key_data_kvno == kvno) {
|
||||
(*kstuple)[0].ks_enctype = princ->key_data->key_data_type[0];
|
||||
(*kstuple)[0].ks_salttype = princ->key_data->key_data_type[1];
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
krb5_warnx(context, "Could not determine what enctypes to generate "
|
||||
"keys for; recreate namespace?");
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
static int
|
||||
randkey_kr(kadm5_principal_ent_rec *princ,
|
||||
unsigned int old_kvno,
|
||||
unsigned int kvno)
|
||||
{
|
||||
krb5_key_salt_tuple *kstuple = 0;
|
||||
krb5_error_code ret = 0;
|
||||
size_t nkstuple = 0;
|
||||
|
||||
/*
|
||||
* We might be using kadm5clnt, so we'll use kadm5_randkey_principal_3(),
|
||||
* which will generate new keys on the server side. This allows a race,
|
||||
* but it will be detected by the key rotation update checks in lib/kadm5
|
||||
* and lib/hdb.
|
||||
*/
|
||||
ret = princ2kstuple(princ, old_kvno, &kstuple, &nkstuple);
|
||||
if (ret == 0)
|
||||
ret = kadm5_randkey_principal_3(kadm_handle, princ->principal, 1,
|
||||
nkstuple, kstuple, NULL, NULL);
|
||||
free(kstuple);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
do_mod_ns_kr(krb5_principal principal, void *data)
|
||||
{
|
||||
krb5_error_code ret;
|
||||
kadm5_principal_ent_rec princ;
|
||||
struct modify_namespace_key_rotation_options *e = data;
|
||||
HDB_Ext_KeyRotation existing;
|
||||
HDB_Ext_KeyRotation new_kr;
|
||||
HDB_extension ext;
|
||||
KeyRotation new_krs[3];
|
||||
krb5_tl_data *tl;
|
||||
krb5_data d;
|
||||
time_t now = time(NULL);
|
||||
size_t size;
|
||||
int freeit = 0;
|
||||
|
||||
d.data = 0;
|
||||
d.length = 0;
|
||||
new_kr.len = 0;
|
||||
new_kr.val = new_krs;
|
||||
ext.mandatory = 0;
|
||||
ext.data.element = choice_HDB_extension_data_key_rotation;
|
||||
ext.data.u.key_rotation.len = 0;
|
||||
ext.data.u.key_rotation.val = 0;
|
||||
existing.len = 0;
|
||||
existing.val = 0;
|
||||
memset(&new_krs, 0, sizeof(new_krs));
|
||||
memset(&princ, 0, sizeof(princ));
|
||||
|
||||
if (e->force_flag || e->revoke_old_string) {
|
||||
krb5_warnx(context, "--force and --revoke-old not implemented yet");
|
||||
return ENOTSUP;
|
||||
}
|
||||
|
||||
ret = kadm5_get_principal(kadm_handle, principal, &princ,
|
||||
KADM5_PRINCIPAL | KADM5_KVNO |
|
||||
KADM5_KEY_DATA | KADM5_TL_DATA);
|
||||
if (ret == 0) {
|
||||
freeit = 1;
|
||||
for (tl = princ.tl_data; tl; tl = tl->tl_data_next) {
|
||||
if (tl->tl_data_type != KRB5_TL_KRB5_CONFIG)
|
||||
continue;
|
||||
ret = decode_HDB_Ext_KeyRotation(tl->tl_data_contents,
|
||||
tl->tl_data_length, &existing, NULL);
|
||||
if (ret) {
|
||||
krb5_warn(context, ret, "unable to decode existing key "
|
||||
"rotation schedule");
|
||||
kadm5_free_principal_ent(kadm_handle, &princ);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
if (!existing.len) {
|
||||
krb5_warnx(context, "no key rotation schedule; "
|
||||
"re-create namespace?");
|
||||
kadm5_free_principal_ent(kadm_handle, &princ);
|
||||
return EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
krb5_warn(context, ret, "No such namespace");
|
||||
kadm5_free_principal_ent(kadm_handle, &princ);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (existing.len > 1)
|
||||
new_kr.val[1] = existing.val[0];
|
||||
if (existing.len > 2)
|
||||
new_kr.val[2] = existing.val[1];
|
||||
new_kr.val[0].flags = existing.val[0].flags;
|
||||
new_kr.val[0].base_kvno = princ.kvno + 2; /* XXX Compute better */
|
||||
new_kr.val[0].base_key_kvno = existing.val[0].base_key_kvno + 1;
|
||||
if (e->new_key_rotation_epoch_string) {
|
||||
if ((ret = str2time_t(e->new_key_rotation_epoch_string,
|
||||
&new_kr.val[0].epoch)))
|
||||
krb5_warn(context, ret, "Invalid epoch specification: %s",
|
||||
e->new_key_rotation_epoch_string);
|
||||
} else {
|
||||
new_kr.val[0].epoch = existing.val[0].epoch +
|
||||
existing.val[0].period * (princ.kvno - new_kr.val[0].base_kvno);
|
||||
}
|
||||
if (ret == 0 && e->new_key_rotation_period_string) {
|
||||
time_t t;
|
||||
|
||||
if ((ret = str2time_t(e->new_key_rotation_period_string, &t)))
|
||||
krb5_warn(context, ret, "Invalid period specification: %s",
|
||||
e->new_key_rotation_period_string);
|
||||
else
|
||||
new_kr.val[0].period = t;
|
||||
} else {
|
||||
new_kr.val[0].period = existing.val[0].period +
|
||||
existing.val[0].period * (princ.kvno - new_kr.val[0].base_kvno);
|
||||
}
|
||||
if (new_kr.val[0].epoch < now) {
|
||||
krb5_warnx(context, "New epoch cannot be in the past");
|
||||
ret = EINVAL;
|
||||
}
|
||||
if (new_kr.val[0].epoch < 30) {
|
||||
krb5_warnx(context, "New period cannot be less than 30s");
|
||||
ret = EINVAL;
|
||||
}
|
||||
if (ret == 0)
|
||||
ret = randkey_kr(&princ, princ.kvno, new_kr.val[0].base_key_kvno);
|
||||
ext.data.u.key_rotation = new_kr;
|
||||
if (ret == 0)
|
||||
ASN1_MALLOC_ENCODE(HDB_extension, d.data, d.length,
|
||||
&ext, &size, ret);
|
||||
if (ret == 0)
|
||||
add_tl(&princ, KRB5_TL_EXTENSION, &d);
|
||||
if (ret == 0) {
|
||||
ret = kadm5_modify_principal(kadm_handle, &princ,
|
||||
KADM5_PRINCIPAL | KADM5_TL_DATA);
|
||||
if (ret)
|
||||
krb5_warn(context, ret, "Could not update namespace");
|
||||
}
|
||||
|
||||
krb5_data_free(&d);
|
||||
free_HDB_Ext_KeyRotation(&existing);
|
||||
if (freeit)
|
||||
kadm5_free_principal_ent(kadm_handle, &princ);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
modify_ns_kr(struct modify_namespace_key_rotation_options *opt,
|
||||
int argc,
|
||||
char **argv)
|
||||
{
|
||||
krb5_error_code ret = 0;
|
||||
int i;
|
||||
|
||||
for(i = 0; i < argc; i++) {
|
||||
ret = foreach_principal(argv[i], do_mod_ns_kr, "mod_ns", opt);
|
||||
if (ret)
|
||||
break;
|
||||
}
|
||||
return ret != 0;
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user