xref: /freebsd/crypto/heimdal/admin/copy.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "ktutil_locl.h"
35 
36 RCSID("$Id: copy.c,v 1.9 2003/01/16 18:59:03 lha Exp $");
37 
38 
39 static krb5_boolean
40 compare_keyblock(const krb5_keyblock *a, const krb5_keyblock *b)
41 {
42     if(a->keytype != b->keytype ||
43        a->keyvalue.length != b->keyvalue.length ||
44        memcmp(a->keyvalue.data, b->keyvalue.data, a->keyvalue.length) != 0)
45 	return FALSE;
46     return TRUE;
47 }
48 
49 static int
50 kt_copy_int (const char *from, const char *to)
51 {
52     krb5_error_code ret;
53     krb5_keytab src_keytab, dst_keytab;
54     krb5_kt_cursor cursor;
55     krb5_keytab_entry entry, dummy;
56 
57     ret = krb5_kt_resolve (context, from, &src_keytab);
58     if (ret) {
59 	krb5_warn (context, ret, "resolving src keytab `%s'", from);
60 	return 1;
61     }
62 
63     ret = krb5_kt_resolve (context, to, &dst_keytab);
64     if (ret) {
65 	krb5_kt_close (context, src_keytab);
66 	krb5_warn (context, ret, "resolving dst keytab `%s'", to);
67 	return 1;
68     }
69 
70     ret = krb5_kt_start_seq_get (context, src_keytab, &cursor);
71     if (ret) {
72 	krb5_warn (context, ret, "krb5_kt_start_seq_get %s", keytab_string);
73 	goto out;
74     }
75 
76     if (verbose_flag)
77 	fprintf(stderr, "copying %s to %s\n", from, to);
78 
79     while((ret = krb5_kt_next_entry(context, src_keytab,
80 				    &entry, &cursor)) == 0) {
81 	char *name_str;
82 	char *etype_str;
83 	krb5_unparse_name (context, entry.principal, &name_str);
84 	krb5_enctype_to_string(context, entry.keyblock.keytype, &etype_str);
85 	ret = krb5_kt_get_entry(context, dst_keytab,
86 				entry.principal,
87 				entry.vno,
88 				entry.keyblock.keytype,
89 				&dummy);
90 	if(ret == 0) {
91 	    /* this entry is already in the new keytab, so no need to
92                copy it; if the keyblocks are not the same, something
93                is weird, so complain about that */
94 	    if(!compare_keyblock(&entry.keyblock, &dummy.keyblock)) {
95 		krb5_warnx(context, "entry with different keyvalue "
96 			   "already exists for %s, keytype %s, kvno %d",
97 			   name_str, etype_str, entry.vno);
98 	    }
99 	    krb5_kt_free_entry(context, &dummy);
100 	    krb5_kt_free_entry (context, &entry);
101 	    free(name_str);
102 	    free(etype_str);
103 	    continue;
104 	} else if(ret != KRB5_KT_NOTFOUND) {
105 	    krb5_warn(context, ret, "krb5_kt_get_entry(%s)", name_str);
106 	    krb5_kt_free_entry (context, &entry);
107 	    free(name_str);
108 	    free(etype_str);
109 	    break;
110 	}
111 	if (verbose_flag)
112 	    fprintf (stderr, "copying %s, keytype %s, kvno %d\n", name_str,
113 		     etype_str, entry.vno);
114 	ret = krb5_kt_add_entry (context, dst_keytab, &entry);
115 	krb5_kt_free_entry (context, &entry);
116 	if (ret) {
117 	    krb5_warn (context, ret, "krb5_kt_add_entry(%s)", name_str);
118 	    free(name_str);
119 	    free(etype_str);
120 	    break;
121 	}
122 	free(name_str);
123 	free(etype_str);
124     }
125     krb5_kt_end_seq_get (context, src_keytab, &cursor);
126 
127   out:
128     krb5_kt_close (context, src_keytab);
129     krb5_kt_close (context, dst_keytab);
130     return 0;
131 }
132 
133 int
134 kt_copy (int argc, char **argv)
135 {
136     int help_flag = 0;
137     int optind = 0;
138 
139     struct getargs args[] = {
140 	{ "help", 'h', arg_flag, NULL}
141     };
142 
143     int num_args = sizeof(args) / sizeof(args[0]);
144     int i = 0;
145 
146     args[i++].value = &help_flag;
147 
148     if(getarg(args, num_args, argc, argv, &optind)) {
149 	arg_printusage(args, num_args, "ktutil copy",
150 		       "keytab-src keytab-dest");
151 	return 1;
152     }
153     if (help_flag) {
154 	arg_printusage(args, num_args, "ktutil copy",
155 		       "keytab-src keytab-dest");
156 	return 1;
157     }
158 
159     argv += optind;
160     argc -= optind;
161 
162     if (argc != 2) {
163 	arg_printusage(args, num_args, "ktutil copy",
164 		       "keytab-src keytab-dest");
165 	return 1;
166     }
167 
168     return kt_copy_int(argv[0], argv[1]);
169 }
170 
171 #ifndef KEYFILE
172 #define KEYFILE SYSCONFDIR "/srvtab"
173 #endif
174 
175 /* copy to from v4 srvtab, just short for copy */
176 static int
177 conv(int srvconv, int argc, char **argv)
178 {
179     int help_flag = 0;
180     char *srvtab = KEYFILE;
181     int optind = 0;
182     char kt4[1024], kt5[1024];
183 
184     char *name;
185 
186     struct getargs args[] = {
187 	{ "srvtab", 's', arg_string, NULL},
188 	{ "help", 'h', arg_flag, NULL}
189     };
190 
191     int num_args = sizeof(args) / sizeof(args[0]);
192     int i = 0;
193 
194     args[i++].value = &srvtab;
195     args[i++].value = &help_flag;
196 
197     if(srvconv)
198 	name = "ktutil srvconvert";
199     else
200 	name = "ktutil srvcreate";
201 
202     if(getarg(args, num_args, argc, argv, &optind)){
203 	arg_printusage(args, num_args, name, "");
204 	return 1;
205     }
206     if(help_flag){
207 	arg_printusage(args, num_args, name, "");
208 	return 0;
209     }
210 
211     argc -= optind;
212     argv += optind;
213 
214     if (argc != 0) {
215 	arg_printusage(args, num_args, name, "");
216 	return 1;
217     }
218 
219     snprintf(kt4, sizeof(kt4), "krb4:%s", srvtab);
220 
221     if(srvconv) {
222 	if(keytab_string != NULL)
223 	    return kt_copy_int(kt4, keytab_string);
224 	else {
225 	    krb5_kt_default_modify_name(context, kt5, sizeof(kt5));
226 	    return kt_copy_int(kt4, kt5);
227 	}
228     } else {
229 	if(keytab_string != NULL)
230 	    return kt_copy_int(keytab_string, kt4);
231 
232 	krb5_kt_default_name(context, kt5, sizeof(kt5));
233 	return kt_copy_int(kt5, kt4);
234     }
235 }
236 
237 int
238 srvconv(int argc, char **argv)
239 {
240     return conv(1, argc, argv);
241 }
242 
243 int
244 srvcreate(int argc, char **argv)
245 {
246     return conv(0, argc, argv);
247 }
248