xref: /freebsd/crypto/krb5/src/tests/hist.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/hist.c - Perform unusual operations on history keys */
3 /*
4  * Copyright (C) 2012 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * This program is invoked from t_pwhist.py to simulate some conditions
35  * normally only seen in databases created before krb5 1.3.  With the "make"
36  * argument, the history key is rolled over to a kvno containing two keys
37  * (since krb5 1.3 we ordinarily ensure that there's only one).  With the
38  * "swap" argument, the two history keys are swapped in order; we use this
39  * operation to simulate the case where krb5 1.7 or earlier chose something
40  * other than the first history key to create password history entries.
41  */
42 
43 #include <k5-int.h>
44 #include <kadm5/admin.h>
45 
46 static void
check(krb5_error_code ret)47 check(krb5_error_code ret)
48 {
49     if (ret) {
50 	fprintf(stderr, "Unexpected failure, aborting\n");
51 	abort();
52     }
53 }
54 
55 int
main(int argc,char ** argv)56 main(int argc, char **argv)
57 {
58     krb5_context ctx;
59     krb5_db_entry *ent;
60     krb5_principal hprinc;
61     kadm5_principal_ent_rec kent;
62     krb5_key_salt_tuple ks[2];
63     krb5_key_data kd;
64     kadm5_config_params params = { 0 };
65     void *handle;
66     char *realm;
67     long mask = KADM5_PRINCIPAL | KADM5_MAX_LIFE | KADM5_ATTRIBUTES;
68 
69     check(kadm5_init_krb5_context(&ctx));
70     check(krb5_parse_name(ctx, "kadmin/history", &hprinc));
71     check(krb5_get_default_realm(ctx, &realm));
72     params.mask |= KADM5_CONFIG_REALM;
73     params.realm = realm;
74     check(kadm5_init(ctx, "user", "", "", &params, KADM5_STRUCT_VERSION,
75                      KADM5_API_VERSION_4, NULL, &handle));
76     if (strcmp(argv[1], "make") == 0) {
77         memset(&kent, 0, sizeof(kent));
78         kent.principal = hprinc;
79         kent.max_life = KRB5_KDB_DISALLOW_ALL_TIX;
80         kent.attributes = 0;
81 	ks[0].ks_enctype = ENCTYPE_AES256_CTS_HMAC_SHA1_96;
82 	ks[0].ks_salttype = KRB5_KDB_SALTTYPE_NORMAL;
83 	ks[1].ks_enctype = ENCTYPE_AES128_CTS_HMAC_SHA1_96;
84 	ks[1].ks_salttype = KRB5_KDB_SALTTYPE_NORMAL;
85         check(kadm5_create_principal_3(handle, &kent, mask, 2, ks, NULL));
86     } else if (strcmp(argv[1], "swap") == 0) {
87         check(krb5_db_get_principal(ctx, hprinc, 0, &ent));
88 	kd = ent->key_data[0];
89 	ent->key_data[0] = ent->key_data[1];
90 	ent->key_data[1] = kd;
91         check(krb5_db_put_principal(ctx, ent));
92         krb5_db_free_principal(ctx, ent);
93     }
94     krb5_free_default_realm(ctx, realm);
95     kadm5_destroy(handle);
96     krb5_free_principal(ctx, hprinc);
97     krb5_free_context(ctx);
98     return 0;
99 }
100