/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/hist.c - Perform unusual operations on history keys */2/*3* Copyright (C) 2012 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132/*33* This program is invoked from t_policy.py to simulate some conditions34* normally only seen in older databases. It expects one argument, which can35* be:36*37* make: The kadmin/history entry is created with two keys. (Since krb5 1.338* we ordinarily ensure that there's only one.)39*40* swap: The kadmin/history entry previously created with "make" is modified41* to swap the order of its keys. We use this operation to simulate the case42* where krb5 1.7 or earlier chose something other than the first history key43* to create password history entries.44*45* des: The kadmin/history entry is modified to change its first key type to46* des-cbc-crc. The key length and contents are not changed. (DES support47* was removed in krb5 1.18.)48*/4950#include <k5-int.h>51#include <kadm5/admin.h>5253static void54check(krb5_error_code ret)55{56if (ret) {57fprintf(stderr, "Unexpected failure, aborting\n");58abort();59}60}6162int63main(int argc, char **argv)64{65krb5_context ctx;66krb5_db_entry *ent;67krb5_principal hprinc;68kadm5_principal_ent_rec kent;69krb5_key_salt_tuple ks[2];70krb5_key_data kd;71kadm5_config_params params = { 0 };72void *handle;73char *realm;74long mask = KADM5_PRINCIPAL | KADM5_MAX_LIFE | KADM5_ATTRIBUTES;7576check(kadm5_init_krb5_context(&ctx));77check(krb5_parse_name(ctx, "kadmin/history", &hprinc));78check(krb5_get_default_realm(ctx, &realm));79params.mask |= KADM5_CONFIG_REALM;80params.realm = realm;81check(kadm5_init(ctx, "user", "", "", ¶ms, KADM5_STRUCT_VERSION,82KADM5_API_VERSION_4, NULL, &handle));83if (strcmp(argv[1], "make") == 0) {84memset(&kent, 0, sizeof(kent));85kent.principal = hprinc;86kent.max_life = KRB5_KDB_DISALLOW_ALL_TIX;87kent.attributes = 0;88ks[0].ks_enctype = ENCTYPE_AES256_CTS_HMAC_SHA1_96;89ks[0].ks_salttype = KRB5_KDB_SALTTYPE_NORMAL;90ks[1].ks_enctype = ENCTYPE_AES128_CTS_HMAC_SHA1_96;91ks[1].ks_salttype = KRB5_KDB_SALTTYPE_NORMAL;92check(kadm5_create_principal_3(handle, &kent, mask, 2, ks, NULL));93} else if (strcmp(argv[1], "swap") == 0) {94check(krb5_db_get_principal(ctx, hprinc, 0, &ent));95kd = ent->key_data[0];96ent->key_data[0] = ent->key_data[1];97ent->key_data[1] = kd;98check(krb5_db_put_principal(ctx, ent));99krb5_db_free_principal(ctx, ent);100} else if (strcmp(argv[1], "des") == 0) {101check(krb5_db_get_principal(ctx, hprinc, 0, &ent));102assert(ent->n_key_data >= 1);103ent->key_data[0].key_data_type[0] = ENCTYPE_DES_CBC_CRC;104check(krb5_db_put_principal(ctx, ent));105krb5_db_free_principal(ctx, ent);106}107krb5_free_default_realm(ctx, realm);108kadm5_destroy(handle);109krb5_free_principal(ctx, hprinc);110krb5_free_context(ctx);111return 0;112}113114115