1*1c9681d1Schristos /*	$NetBSD: copy.c,v 1.2 2017/01/28 21:31:44 christos Exp $	*/
2f59d82ffSelric 
3f59d82ffSelric /*
4f59d82ffSelric  * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
5f59d82ffSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6f59d82ffSelric  * All rights reserved.
7f59d82ffSelric  *
8f59d82ffSelric  * Redistribution and use in source and binary forms, with or without
9f59d82ffSelric  * modification, are permitted provided that the following conditions
10f59d82ffSelric  * are met:
11f59d82ffSelric  *
12f59d82ffSelric  * 1. Redistributions of source code must retain the above copyright
13f59d82ffSelric  *    notice, this list of conditions and the following disclaimer.
14f59d82ffSelric  *
15f59d82ffSelric  * 2. Redistributions in binary form must reproduce the above copyright
16f59d82ffSelric  *    notice, this list of conditions and the following disclaimer in the
17f59d82ffSelric  *    documentation and/or other materials provided with the distribution.
18f59d82ffSelric  *
19f59d82ffSelric  * 3. Neither the name of the Institute nor the names of its contributors
20f59d82ffSelric  *    may be used to endorse or promote products derived from this software
21f59d82ffSelric  *    without specific prior written permission.
22f59d82ffSelric  *
23f59d82ffSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24f59d82ffSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f59d82ffSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f59d82ffSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27f59d82ffSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f59d82ffSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f59d82ffSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f59d82ffSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f59d82ffSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f59d82ffSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f59d82ffSelric  * SUCH DAMAGE.
34f59d82ffSelric  */
35f59d82ffSelric 
36f59d82ffSelric #include "ktutil_locl.h"
37f59d82ffSelric 
38*1c9681d1Schristos __RCSID("$NetBSD: copy.c,v 1.2 2017/01/28 21:31:44 christos Exp $");
39f59d82ffSelric 
40f59d82ffSelric 
41f59d82ffSelric static krb5_boolean
compare_keyblock(const krb5_keyblock * a,const krb5_keyblock * b)42f59d82ffSelric compare_keyblock(const krb5_keyblock *a, const krb5_keyblock *b)
43f59d82ffSelric {
44f59d82ffSelric     if(a->keytype != b->keytype ||
45f59d82ffSelric        a->keyvalue.length != b->keyvalue.length ||
46f59d82ffSelric        memcmp(a->keyvalue.data, b->keyvalue.data, a->keyvalue.length) != 0)
47f59d82ffSelric 	return FALSE;
48f59d82ffSelric     return TRUE;
49f59d82ffSelric }
50f59d82ffSelric 
51f59d82ffSelric int
kt_copy(void * opt,int argc,char ** argv)52f59d82ffSelric kt_copy (void *opt, int argc, char **argv)
53f59d82ffSelric {
54f59d82ffSelric     krb5_error_code ret;
55f59d82ffSelric     krb5_keytab src_keytab, dst_keytab;
56f59d82ffSelric     krb5_kt_cursor cursor;
57f59d82ffSelric     krb5_keytab_entry entry, dummy;
58f59d82ffSelric     const char *from = argv[0];
59f59d82ffSelric     const char *to = argv[1];
60f59d82ffSelric 
61f59d82ffSelric     ret = krb5_kt_resolve (context, from, &src_keytab);
62f59d82ffSelric     if (ret) {
63f59d82ffSelric 	krb5_warn (context, ret, "resolving src keytab `%s'", from);
64f59d82ffSelric 	return 1;
65f59d82ffSelric     }
66f59d82ffSelric 
67f59d82ffSelric     ret = krb5_kt_resolve (context, to, &dst_keytab);
68f59d82ffSelric     if (ret) {
69f59d82ffSelric 	krb5_kt_close (context, src_keytab);
70f59d82ffSelric 	krb5_warn (context, ret, "resolving dst keytab `%s'", to);
71f59d82ffSelric 	return 1;
72f59d82ffSelric     }
73f59d82ffSelric 
74f59d82ffSelric     ret = krb5_kt_start_seq_get (context, src_keytab, &cursor);
75f59d82ffSelric     if (ret) {
76f59d82ffSelric 	krb5_warn (context, ret, "krb5_kt_start_seq_get %s", keytab_string);
77f59d82ffSelric 	goto out;
78f59d82ffSelric     }
79f59d82ffSelric 
80f59d82ffSelric     if (verbose_flag)
81f59d82ffSelric 	fprintf(stderr, "copying %s to %s\n", from, to);
82f59d82ffSelric 
83f59d82ffSelric     while((ret = krb5_kt_next_entry(context, src_keytab,
84f59d82ffSelric 				    &entry, &cursor)) == 0) {
85f59d82ffSelric 	char *name_str;
86f59d82ffSelric 	char *etype_str;
87f59d82ffSelric 	ret = krb5_unparse_name (context, entry.principal, &name_str);
88f59d82ffSelric 	if(ret) {
89f59d82ffSelric 	    krb5_warn(context, ret, "krb5_unparse_name");
90f59d82ffSelric 	    name_str = NULL; /* XXX */
91f59d82ffSelric 	}
92f59d82ffSelric 	ret = krb5_enctype_to_string(context, entry.keyblock.keytype, &etype_str);
93f59d82ffSelric 	if(ret) {
94f59d82ffSelric 	    krb5_warn(context, ret, "krb5_enctype_to_string");
95f59d82ffSelric 	    etype_str = NULL; /* XXX */
96f59d82ffSelric 	}
97f59d82ffSelric 	ret = krb5_kt_get_entry(context, dst_keytab,
98f59d82ffSelric 				entry.principal,
99f59d82ffSelric 				entry.vno,
100f59d82ffSelric 				entry.keyblock.keytype,
101f59d82ffSelric 				&dummy);
102f59d82ffSelric 	if(ret == 0) {
103f59d82ffSelric 	    /* this entry is already in the new keytab, so no need to
104f59d82ffSelric                copy it; if the keyblocks are not the same, something
105f59d82ffSelric                is weird, so complain about that */
106f59d82ffSelric 	    if(!compare_keyblock(&entry.keyblock, &dummy.keyblock)) {
107f59d82ffSelric 		krb5_warnx(context, "entry with different keyvalue "
108f59d82ffSelric 			   "already exists for %s, keytype %s, kvno %d",
109f59d82ffSelric 			   name_str, etype_str, entry.vno);
110f59d82ffSelric 	    }
111f59d82ffSelric 	    krb5_kt_free_entry(context, &dummy);
112f59d82ffSelric 	    krb5_kt_free_entry (context, &entry);
113f59d82ffSelric 	    free(name_str);
114f59d82ffSelric 	    free(etype_str);
115f59d82ffSelric 	    continue;
116f59d82ffSelric 	} else if(ret != KRB5_KT_NOTFOUND) {
117f59d82ffSelric 	    krb5_warn (context, ret, "%s: fetching %s/%s/%u",
118f59d82ffSelric 		       to, name_str, etype_str, entry.vno);
119f59d82ffSelric 	    krb5_kt_free_entry (context, &entry);
120f59d82ffSelric 	    free(name_str);
121f59d82ffSelric 	    free(etype_str);
122f59d82ffSelric 	    break;
123f59d82ffSelric 	}
124f59d82ffSelric 	if (verbose_flag)
125f59d82ffSelric 	    fprintf (stderr, "copying %s, keytype %s, kvno %d\n", name_str,
126f59d82ffSelric 		     etype_str, entry.vno);
127f59d82ffSelric 	ret = krb5_kt_add_entry (context, dst_keytab, &entry);
128f59d82ffSelric 	krb5_kt_free_entry (context, &entry);
129f59d82ffSelric 	if (ret) {
130f59d82ffSelric 	    krb5_warn (context, ret, "%s: adding %s/%s/%u",
131f59d82ffSelric 		       to, name_str, etype_str, entry.vno);
132f59d82ffSelric 	    free(name_str);
133f59d82ffSelric 	    free(etype_str);
134f59d82ffSelric 	    break;
135f59d82ffSelric 	}
136f59d82ffSelric 	free(name_str);
137f59d82ffSelric 	free(etype_str);
138f59d82ffSelric     }
139f59d82ffSelric     krb5_kt_end_seq_get (context, src_keytab, &cursor);
140f59d82ffSelric 
141f59d82ffSelric   out:
142f59d82ffSelric     krb5_kt_close (context, src_keytab);
143f59d82ffSelric     krb5_kt_close (context, dst_keytab);
144f59d82ffSelric     return ret != 0;
145f59d82ffSelric }
146