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