
[Code reviewed by Love Hörnquist Åstrand <lha@kth.se>] Added heim_db_*() entry points for dealing with databases, and make krb5_aname_to_localname() use it. The following enhancements to libheimbase are included: - Add heim_data_t and heim_string_t "reference" variants to avoid memory copies of potentially large data/strings. See heim_data_ref_create() and heim_string_ref_create(). - Added enhancements to heim_array_t to allow their use for queues and stacks, and to improve performance. See heim_array_insert_value(). - Added XPath-like accessors for heim_object_t. See heim_path_get(), heim_path_copy(), heim_path_create(), and heim_path_delete(). These are used extensively in the DB framework's generic composition of ACID support and in the test_base program - Made libheimbase more consistent with Core Foundation naming conventions. See heim_{dict, array}_{get, copy}_value() and heim_path_{get, copy}(). - Added functionality to and fixed bugs in base/json.c: - heim_serialize(); - depth limit for JSON parsing (for DoS protection); - pretty-printing; - JSON compliance (see below); - flag options for parsing and serializing; these are needed because of impedance mismatches between heim_object_t and JSON (e.g., heim_dict_t allows non-string keys, but JSON does not; heimbase supports binary data, while JSON does not). - Added heim_error_enomem(). - Enhanced the test_base program to test new functionality and to use heim_path*() to better test JSON encoding. This includes some fuzz testing of JSON parsing, and running the test under valgrind. - Started to add doxygen documentation for libheimbase (but doc build for libheimbase is still incomplete). Note that there's still some incomplete JSON support: - JSON string quoting is not fully implemented; - libheimbase lacks support for real numbers, while JSON has it -- otherwise libheimbase is a superset of JSON, specifically in that any heim_object_t can be a key for an associative array. The following DB backends are supported natively: - "sorted-text", a binary search of sorted (in C locale), flat text files; - "json", a backend that stores DB contents serialized as JSON (this is intended for configuration-like contents). The DB framework supports: - multiple key/value tables per-DB - ACID transactions The DB framework also natively implements ACID transactions for any DB backends that a) do not provide transactions natively, b) do provide lock/unlock/sync methods (even on Windows). This includes autocommit of DB updates outside transactions. Future DB enhancements may include: - add backends for various DB types (BDB, CDB, MDB, ...); - make libhdb use heim_db_t; - add a command-line tool for interfacing to databases via libheimbase (e.g., to get/set/delete values, create/copy/ backup DBs, inspect history, check integrity); - framework-level transaction logging (with redo and undo logging), for generic incremental replication; - framework-level DB integrity checking. We could store a MAC of the XOR of a hash function applied to {key, value} for every entry in the DB, then use this to check DB integrity incrementally during incremental replication, as well as for the whole DB.
440 lines
11 KiB
C
440 lines
11 KiB
C
/*
|
|
* Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
|
|
* (Royal Institute of Technology, Stockholm, Sweden).
|
|
* All rights reserved.
|
|
*
|
|
* Portions Copyright (c) 2009 Apple Inc. All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
*
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*/
|
|
|
|
#include "kdc_locl.h"
|
|
|
|
struct perf {
|
|
unsigned long as_req;
|
|
unsigned long tgs_req;
|
|
struct timeval start;
|
|
struct timeval stop;
|
|
struct perf *next;
|
|
} *ptop;
|
|
|
|
#ifdef SUPPORT_DETACH
|
|
int detach_from_console = -1;
|
|
#define DETACH_IS_DEFAULT FALSE
|
|
#endif
|
|
|
|
static krb5_kdc_configuration *kdc_config;
|
|
static krb5_context kdc_context;
|
|
|
|
static struct sockaddr_storage sa;
|
|
static const char *astr = "0.0.0.0";
|
|
|
|
static void eval_object(heim_object_t);
|
|
|
|
|
|
/*
|
|
*
|
|
*/
|
|
|
|
static krb5_error_code
|
|
send_to_kdc(krb5_context c, void *ptr, krb5_krbhst_info *hi, time_t timeout,
|
|
const krb5_data *in, krb5_data *out)
|
|
{
|
|
krb5_error_code ret;
|
|
|
|
krb5_kdc_update_time(NULL);
|
|
|
|
ret = krb5_kdc_process_request(kdc_context, kdc_config,
|
|
in->data, in->length,
|
|
out, NULL, astr,
|
|
(struct sockaddr *)&sa, 0);
|
|
if (ret)
|
|
krb5_err(c, 1, ret, "krb5_kdc_process_request");
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
perf_start(struct perf *perf)
|
|
{
|
|
memset(perf, 0, sizeof(*perf));
|
|
|
|
gettimeofday(&perf->start, NULL);
|
|
perf->next = ptop;
|
|
ptop = perf;
|
|
}
|
|
|
|
static void
|
|
perf_stop(struct perf *perf)
|
|
{
|
|
gettimeofday(&perf->stop, NULL);
|
|
ptop = perf->next;
|
|
|
|
if (ptop) {
|
|
ptop->as_req += perf->as_req;
|
|
ptop->tgs_req += perf->tgs_req;
|
|
}
|
|
|
|
timevalsub(&perf->stop, &perf->start);
|
|
printf("time: %lu.%06lu\n",
|
|
(unsigned long)perf->stop.tv_sec,
|
|
(unsigned long)perf->stop.tv_usec);
|
|
|
|
#define USEC_PER_SEC 1000000
|
|
|
|
if (perf->as_req) {
|
|
double as_ps = 0.0;
|
|
as_ps = (perf->as_req * USEC_PER_SEC) / (double)((perf->stop.tv_sec * USEC_PER_SEC) + perf->stop.tv_usec);
|
|
printf("as-req/s %.2lf (total %lu requests)\n", as_ps, perf->as_req);
|
|
}
|
|
|
|
if (perf->tgs_req) {
|
|
double tgs_ps = 0.0;
|
|
tgs_ps = (perf->tgs_req * USEC_PER_SEC) / (double)((perf->stop.tv_sec * USEC_PER_SEC) + perf->stop.tv_usec);
|
|
printf("tgs-req/s %.2lf (total %lu requests)\n", tgs_ps, perf->tgs_req);
|
|
}
|
|
}
|
|
|
|
/*
|
|
*
|
|
*/
|
|
|
|
static void
|
|
eval_repeat(heim_dict_t o)
|
|
{
|
|
heim_object_t or = heim_dict_get_value(o, HSTR("value"));
|
|
heim_number_t n = heim_dict_get_value(o, HSTR("num"));
|
|
int i, num;
|
|
struct perf perf;
|
|
|
|
perf_start(&perf);
|
|
|
|
heim_assert(or != NULL, "value missing");
|
|
heim_assert(n != NULL, "num missing");
|
|
|
|
num = heim_number_get_int(n);
|
|
heim_assert(num >= 0, "num >= 0");
|
|
|
|
for (i = 0; i < num; i++)
|
|
eval_object(or);
|
|
|
|
perf_stop(&perf);
|
|
}
|
|
|
|
/*
|
|
*
|
|
*/
|
|
|
|
static void
|
|
eval_kinit(heim_dict_t o)
|
|
{
|
|
heim_string_t user, password, keytab, fast_armor_cc, pk_user_id, ccache;
|
|
krb5_get_init_creds_opt *opt;
|
|
krb5_init_creds_context ctx;
|
|
krb5_principal client;
|
|
krb5_keytab kt = NULL;
|
|
krb5_ccache fast_cc = NULL;
|
|
krb5_error_code ret;
|
|
|
|
if (ptop)
|
|
ptop->as_req++;
|
|
|
|
user = heim_dict_get_value(o, HSTR("client"));
|
|
if (user == NULL)
|
|
krb5_errx(kdc_context, 1, "no client");
|
|
|
|
password = heim_dict_get_value(o, HSTR("password"));
|
|
keytab = heim_dict_get_value(o, HSTR("keytab"));
|
|
pk_user_id = heim_dict_get_value(o, HSTR("pkinit-user-cert-id"));
|
|
if (password == NULL && keytab == NULL && pk_user_id == NULL)
|
|
krb5_errx(kdc_context, 1, "password, keytab, nor PKINIT user cert ID");
|
|
|
|
ccache = heim_dict_get_value(o, HSTR("ccache"));
|
|
|
|
ret = krb5_parse_name(kdc_context, heim_string_get_utf8(user), &client);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_unparse_name");
|
|
|
|
/* PKINIT parts */
|
|
ret = krb5_get_init_creds_opt_alloc (kdc_context, &opt);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_get_init_creds_opt_alloc");
|
|
|
|
if (pk_user_id) {
|
|
heim_bool_t rsaobj = heim_dict_get_value(o, HSTR("pkinit-use-rsa"));
|
|
int use_rsa = rsaobj ? heim_bool_val(rsaobj) : 0;
|
|
|
|
ret = krb5_get_init_creds_opt_set_pkinit(kdc_context, opt,
|
|
client,
|
|
heim_string_get_utf8(pk_user_id),
|
|
NULL, NULL, NULL,
|
|
use_rsa ? 2 : 0,
|
|
NULL, NULL, NULL);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_get_init_creds_opt_set_pkinit");
|
|
}
|
|
|
|
ret = krb5_init_creds_init(kdc_context, client, NULL, NULL, 0, opt, &ctx);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_init_creds_init");
|
|
|
|
fast_armor_cc = heim_dict_get_value(o, HSTR("fast-armor-cc"));
|
|
if (fast_armor_cc) {
|
|
|
|
ret = krb5_cc_resolve(kdc_context, heim_string_get_utf8(fast_armor_cc), &fast_cc);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
|
|
|
|
ret = krb5_init_creds_set_fast_ccache(kdc_context, ctx, fast_cc);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_fast_ccache");
|
|
}
|
|
|
|
if (password) {
|
|
ret = krb5_init_creds_set_password(kdc_context, ctx,
|
|
heim_string_get_utf8(password));
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_password");
|
|
}
|
|
if (keytab) {
|
|
ret = krb5_kt_resolve(kdc_context, heim_string_get_utf8(keytab), &kt);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_kt_resolve");
|
|
|
|
ret = krb5_init_creds_set_keytab(kdc_context, ctx, kt);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_init_creds_set_keytab");
|
|
}
|
|
|
|
ret = krb5_init_creds_get(kdc_context, ctx);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_init_creds_get");
|
|
|
|
if (ccache) {
|
|
const char *name = heim_string_get_utf8(ccache);
|
|
krb5_creds cred;
|
|
krb5_ccache cc;
|
|
|
|
ret = krb5_init_creds_get_creds(kdc_context, ctx, &cred);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_init_creds_get_creds");
|
|
|
|
ret = krb5_cc_resolve(kdc_context, name, &cc);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
|
|
|
|
krb5_init_creds_store(kdc_context, ctx, cc);
|
|
|
|
ret = krb5_cc_close(kdc_context, cc);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_cc_close");
|
|
|
|
krb5_free_cred_contents(kdc_context, &cred);
|
|
}
|
|
|
|
krb5_init_creds_free(kdc_context, ctx);
|
|
|
|
if (kt)
|
|
krb5_kt_close(kdc_context, kt);
|
|
if (fast_cc)
|
|
krb5_cc_close(kdc_context, fast_cc);
|
|
}
|
|
|
|
/*
|
|
*
|
|
*/
|
|
|
|
static void
|
|
eval_kgetcred(heim_dict_t o)
|
|
{
|
|
heim_string_t server, ccache;
|
|
krb5_get_creds_opt opt;
|
|
heim_bool_t nostore;
|
|
krb5_error_code ret;
|
|
krb5_ccache cc = NULL;
|
|
krb5_principal s;
|
|
krb5_creds *out = NULL;
|
|
|
|
if (ptop)
|
|
ptop->tgs_req++;
|
|
|
|
server = heim_dict_get_value(o, HSTR("server"));
|
|
if (server == NULL)
|
|
krb5_errx(kdc_context, 1, "no server");
|
|
|
|
ccache = heim_dict_get_value(o, HSTR("ccache"));
|
|
if (ccache == NULL)
|
|
krb5_errx(kdc_context, 1, "no ccache");
|
|
|
|
nostore = heim_dict_get_value(o, HSTR("nostore"));
|
|
if (nostore == NULL)
|
|
nostore = heim_bool_create(1);
|
|
|
|
ret = krb5_cc_resolve(kdc_context, heim_string_get_utf8(ccache), &cc);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
|
|
|
|
ret = krb5_parse_name(kdc_context, heim_string_get_utf8(server), &s);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_parse_name");
|
|
|
|
ret = krb5_get_creds_opt_alloc(kdc_context, &opt);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_get_creds_opt_alloc");
|
|
|
|
if (heim_bool_val(nostore))
|
|
krb5_get_creds_opt_add_options(kdc_context, opt, KRB5_GC_NO_STORE);
|
|
|
|
ret = krb5_get_creds(kdc_context, opt, cc, s, &out);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_get_creds");
|
|
|
|
krb5_free_creds(kdc_context, out);
|
|
krb5_free_principal(kdc_context, s);
|
|
krb5_get_creds_opt_free(kdc_context, opt);
|
|
krb5_cc_close(kdc_context, cc);
|
|
}
|
|
|
|
|
|
/*
|
|
*
|
|
*/
|
|
|
|
static void
|
|
eval_kdestroy(heim_dict_t o)
|
|
{
|
|
heim_string_t ccache = heim_dict_get_value(o, HSTR("ccache"));;
|
|
krb5_error_code ret;
|
|
const char *name;
|
|
krb5_ccache cc;
|
|
|
|
heim_assert(ccache != NULL, "ccache_missing");
|
|
|
|
name = heim_string_get_utf8(ccache);
|
|
|
|
ret = krb5_cc_resolve(kdc_context, name, &cc);
|
|
if (ret)
|
|
krb5_err(kdc_context, 1, ret, "krb5_cc_resolve");
|
|
|
|
krb5_cc_destroy(kdc_context, cc);
|
|
}
|
|
|
|
|
|
/*
|
|
*
|
|
*/
|
|
|
|
static void
|
|
eval_array_element(heim_object_t o, void *ptr)
|
|
{
|
|
eval_object(o);
|
|
}
|
|
|
|
static void
|
|
eval_object(heim_object_t o)
|
|
{
|
|
heim_tid_t t = heim_get_tid(o);
|
|
|
|
if (t == heim_array_get_type_id()) {
|
|
heim_array_iterate_f(o, NULL, eval_array_element);
|
|
} else if (t == heim_dict_get_type_id()) {
|
|
const char *op = heim_dict_get_value(o, HSTR("op"));
|
|
|
|
heim_assert(op != NULL, "op missing");
|
|
|
|
if (strcmp(op, "repeat") == 0) {
|
|
eval_repeat(o);
|
|
} else if (strcmp(op, "kinit") == 0) {
|
|
eval_kinit(o);
|
|
} else if (strcmp(op, "kgetcred") == 0) {
|
|
eval_kgetcred(o);
|
|
} else if (strcmp(op, "kdestroy") == 0) {
|
|
eval_kdestroy(o);
|
|
} else {
|
|
errx(1, "unsupported ops %s", op);
|
|
}
|
|
|
|
} else
|
|
errx(1, "unsupported");
|
|
}
|
|
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
krb5_error_code ret;
|
|
int optidx = 0;
|
|
|
|
setprogname(argv[0]);
|
|
|
|
ret = krb5_init_context(&kdc_context);
|
|
if (ret == KRB5_CONFIG_BADFORMAT)
|
|
errx (1, "krb5_init_context failed to parse configuration file");
|
|
else if (ret)
|
|
errx (1, "krb5_init_context failed: %d", ret);
|
|
|
|
ret = krb5_kt_register(kdc_context, &hdb_kt_ops);
|
|
if (ret)
|
|
errx (1, "krb5_kt_register(HDB) failed: %d", ret);
|
|
|
|
kdc_config = configure(kdc_context, argc, argv, &optidx);
|
|
|
|
argc -= optidx;
|
|
argv += optidx;
|
|
|
|
if (argc == 0)
|
|
errx(1, "missing operations");
|
|
|
|
krb5_set_send_to_kdc_func(kdc_context, send_to_kdc, NULL);
|
|
|
|
{
|
|
void *buf;
|
|
size_t size;
|
|
heim_object_t o;
|
|
|
|
if (rk_undumpdata(argv[0], &buf, &size))
|
|
errx(1, "undumpdata: %s", argv[0]);
|
|
|
|
o = heim_json_create_with_bytes(buf, size, 10, 0, NULL);
|
|
free(buf);
|
|
if (o == NULL)
|
|
errx(1, "heim_json");
|
|
|
|
/*
|
|
* do the work here
|
|
*/
|
|
|
|
eval_object(o);
|
|
|
|
heim_release(o);
|
|
}
|
|
|
|
krb5_free_context(kdc_context);
|
|
return 0;
|
|
}
|