default_keys.c revision 256281
1193323Sed/*
2193323Sed * Copyright (c) 2003 Kungliga Tekniska H��gskolan
3193323Sed * (Royal Institute of Technology, Stockholm, Sweden).
4193323Sed * All rights reserved.
5193323Sed *
6193323Sed * Redistribution and use in source and binary forms, with or without
7193323Sed * modification, are permitted provided that the following conditions
8193323Sed * are met:
9193323Sed *
10193323Sed * 1. Redistributions of source code must retain the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer.
12193323Sed *
13193323Sed * 2. Redistributions in binary form must reproduce the above copyright
14193323Sed *    notice, this list of conditions and the following disclaimer in the
15193323Sed *    documentation and/or other materials provided with the distribution.
16193323Sed *
17206274Srdivacky * 3. Neither the name of the Institute nor the names of its contributors
18193323Sed *    may be used to endorse or promote products derived from this software
19193323Sed *    without specific prior written permission.
20193323Sed *
21193323Sed * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22206274Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24198090Srdivacky * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31193323Sed * SUCH DAMAGE.
32193323Sed */
33193323Sed
34193323Sed#include "kadm5_locl.h"
35193323Sed#include <err.h>
36193323Sed
37193323SedRCSID("$Id$");
38193323Sed
39193323Sedstatic void
40198090Srdivackyprint_keys(krb5_context context, Key *keys, size_t nkeys)
41193323Sed{
42193323Sed    krb5_error_code ret;
43193323Sed    char *str;
44193323Sed    int i;
45193323Sed
46193323Sed    printf("keys:\n");
47193323Sed
48193323Sed    for (i = 0; i < nkeys; i++) {
49193323Sed
50193323Sed	ret = krb5_enctype_to_string(context, keys[i].key.keytype, &str);
51193323Sed	if (ret)
52193323Sed	    krb5_err(context, ret, 1, "krb5_enctype_to_string: %d\n",
53193323Sed		     (int)keys[i].key.keytype);
54193323Sed
55193323Sed	printf("\tenctype %s", str);
56193323Sed	free(str);
57193323Sed
58193323Sed	if (keys[i].salt) {
59193323Sed	    printf(" salt: ");
60193323Sed
61193323Sed	    switch (keys[i].salt->type) {
62193323Sed	    case KRB5_PW_SALT:
63193323Sed		printf("pw-salt:");
64193323Sed		break;
65193323Sed	    case KRB5_AFS3_SALT:
66193323Sed		printf("afs3-salt:");
67193323Sed		break;
68193323Sed	    default:
69193323Sed		printf("unknown salt: %d", keys[i].salt->type);
70193323Sed		break;
71193323Sed	    }
72193323Sed	    if (keys[i].salt->salt.length)
73193323Sed		printf("%.*s", (int)keys[i].salt->salt.length,
74193323Sed		       (char *)keys[i].salt->salt.data);
75193323Sed	}
76193323Sed	printf("\n");
77193323Sed    }
78193323Sed    printf("end keys:\n");
79193323Sed}
80193323Sed
81193323Sedstatic void
82193323Sedparse_file(krb5_context context, krb5_principal principal, int no_salt)
83193323Sed{
84193323Sed    krb5_error_code ret;
85193323Sed    size_t nkeys;
86193323Sed    Key *keys;
87193323Sed
88193323Sed    ret = hdb_generate_key_set(context, principal, &keys, &nkeys, no_salt);
89193323Sed    if (ret)
90193323Sed	krb5_err(context, 1, ret, "hdb_generate_key_set");
91193323Sed
92193323Sed    print_keys(context, keys, nkeys);
93193323Sed
94193323Sed    hdb_free_keys(context, nkeys, keys);
95193323Sed}
96193323Sed
97193323Sedint
98193323Sedmain(int argc, char **argv)
99193323Sed{
100193323Sed    krb5_error_code ret;
101193323Sed    krb5_context context;
102193323Sed    krb5_principal principal;
103193323Sed
104193323Sed    ret = krb5_init_context(&context);
105193323Sed    if (ret)
106193323Sed	errx(1, "krb5_init_context");
107193323Sed
108193323Sed    ret = krb5_parse_name(context, "lha@SU.SE", &principal);
109193323Sed    if (ret)
110193323Sed	krb5_err(context, ret, 1, "krb5_parse_name");
111193323Sed
112193323Sed    parse_file(context, principal, 0);
113193323Sed    parse_file(context, principal, 1);
114193323Sed
115193323Sed    krb5_free_principal(context, principal);
116193323Sed
117193323Sed    krb5_free_context(context);
118193323Sed
119193323Sed    return 0;
120193323Sed}
121193323Sed