diff options
author | Sarah Day <sarahday@mit.edu> | 2016-01-21 11:16:21 -0500 |
---|---|---|
committer | Greg Hudson <ghudson@mit.edu> | 2016-02-03 11:03:19 -0500 |
commit | 44ad57d8d38efc944f64536354435f5b721c0ee0 (patch) | |
tree | aa8fbc247f0052a14219fcf02f93c64582283e00 /src/lib/kdb | |
parent | 83ed75feba32e46f736fcce0d96a0445f29b96c2 (diff) | |
download | krb5-44ad57d8d38efc944f64536354435f5b721c0ee0.zip krb5-44ad57d8d38efc944f64536354435f5b721c0ee0.tar.gz krb5-44ad57d8d38efc944f64536354435f5b721c0ee0.tar.bz2 |
Sort principal key_data retrieved from KDB module
Some krb5 code expects principal keys to be sorted by kvno in
descending order. To make sure this is followed in all KDB
implementations, sort the keys in libkdb5 after principals are
retrieved.
Diffstat (limited to 'src/lib/kdb')
-rw-r--r-- | src/lib/kdb/kdb5.c | 61 | ||||
-rw-r--r-- | src/lib/kdb/libkdb5.exports | 1 |
2 files changed, 59 insertions, 3 deletions
diff --git a/src/lib/kdb/kdb5.c b/src/lib/kdb/kdb5.c index 50daf74..deafed1 100644 --- a/src/lib/kdb/kdb5.c +++ b/src/lib/kdb/kdb5.c @@ -1,6 +1,7 @@ /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* - * Copyright 2006, 2009, 2010 by the Massachusetts Institute of Technology. + * Copyright 2006, 2009, 2010, 2016 by the Massachusetts Institute of + * Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -752,7 +753,15 @@ krb5_db_get_principal(krb5_context kcontext, krb5_const_principal search_for, return status; if (v->get_principal == NULL) return KRB5_PLUGIN_OP_NOTSUPP; - return v->get_principal(kcontext, search_for, flags, entry); + status = v->get_principal(kcontext, search_for, flags, entry); + if (status) + return status; + + /* Sort the keys in the db entry as some parts of krb5 expect it to be. */ + if ((*entry)->key_data != NULL) + krb5_dbe_sort_key_data((*entry)->key_data, (*entry)->n_key_data); + + return 0; } void @@ -942,6 +951,26 @@ krb5_db_delete_principal(krb5_context kcontext, krb5_principal search_for) return status; } +/* + * Use a proxy function for iterate so that we can sort the keys before sending + * them to the callback. + */ +struct callback_proxy_args { + int (*func)(krb5_pointer, krb5_db_entry *); + krb5_pointer func_arg; +}; + +static int +sort_entry_callback_proxy(krb5_pointer func_arg, krb5_db_entry *entry) +{ + struct callback_proxy_args *args = (struct callback_proxy_args *)func_arg; + + /* Sort the keys in the db entry as some parts of krb5 expect it to be. */ + if (entry && entry->key_data) + krb5_dbe_sort_key_data(entry->key_data, entry->n_key_data); + return args->func(args->func_arg, entry); +} + krb5_error_code krb5_db_iterate(krb5_context kcontext, char *match_entry, int (*func)(krb5_pointer, krb5_db_entry *), @@ -949,13 +978,20 @@ krb5_db_iterate(krb5_context kcontext, char *match_entry, { krb5_error_code status = 0; kdb_vftabl *v; + struct callback_proxy_args proxy_args; status = get_vftabl(kcontext, &v); if (status) return status; if (v->iterate == NULL) return KRB5_PLUGIN_OP_NOTSUPP; - return v->iterate(kcontext, match_entry, func, func_arg, iterflags); + + /* Use the proxy function to sort key data before passing entries to + * callback. */ + proxy_args.func = func; + proxy_args.func_arg = func_arg; + return v->iterate(kcontext, match_entry, sort_entry_callback_proxy, + &proxy_args, iterflags); } /* Return a read only pointer alias to mkey list. Do not free this! */ @@ -2564,3 +2600,22 @@ krb5_db_check_allowed_to_delegate(krb5_context kcontext, return KRB5_PLUGIN_OP_NOTSUPP; return v->check_allowed_to_delegate(kcontext, client, server, proxy); } + +void +krb5_dbe_sort_key_data(krb5_key_data *key_data, size_t key_data_length) +{ + size_t i, j; + krb5_key_data tmp; + + /* Use insertion sort as a stable sort. */ + for (i = 1; i < key_data_length; i++) { + j = i; + while (j > 0 && + key_data[j - 1].key_data_kvno < key_data[j].key_data_kvno) { + tmp = key_data[j]; + key_data[j] = key_data[j - 1]; + key_data[j - 1] = tmp; + j--; + } + } +} diff --git a/src/lib/kdb/libkdb5.exports b/src/lib/kdb/libkdb5.exports index cb4c3df..68ac537 100644 --- a/src/lib/kdb/libkdb5.exports +++ b/src/lib/kdb/libkdb5.exports @@ -99,3 +99,4 @@ ulog_get_sno_status ulog_replay ulog_set_last xdr_kdb_incr_update_t +krb5_dbe_sort_key_data |