No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / admin / copy.c
blob798f7aa9fa7097413ba8c4e7bdf342152609131e
1 /*
2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "ktutil_locl.h"
36 __RCSID("$Heimdal: copy.c 14260 2004-09-23 14:45:29Z joda $"
37 "$NetBSD$");
40 static krb5_boolean
41 compare_keyblock(const krb5_keyblock *a, const krb5_keyblock *b)
43 if(a->keytype != b->keytype ||
44 a->keyvalue.length != b->keyvalue.length ||
45 memcmp(a->keyvalue.data, b->keyvalue.data, a->keyvalue.length) != 0)
46 return FALSE;
47 return TRUE;
50 static int
51 kt_copy_int (const char *from, const char *to)
53 krb5_error_code ret;
54 krb5_keytab src_keytab, dst_keytab;
55 krb5_kt_cursor cursor;
56 krb5_keytab_entry entry, dummy;
58 ret = krb5_kt_resolve (context, from, &src_keytab);
59 if (ret) {
60 krb5_warn (context, ret, "resolving src keytab `%s'", from);
61 return 1;
64 ret = krb5_kt_resolve (context, to, &dst_keytab);
65 if (ret) {
66 krb5_kt_close (context, src_keytab);
67 krb5_warn (context, ret, "resolving dst keytab `%s'", to);
68 return 1;
71 ret = krb5_kt_start_seq_get (context, src_keytab, &cursor);
72 if (ret) {
73 krb5_warn (context, ret, "krb5_kt_start_seq_get %s", keytab_string);
74 goto out;
77 if (verbose_flag)
78 fprintf(stderr, "copying %s to %s\n", from, to);
80 while((ret = krb5_kt_next_entry(context, src_keytab,
81 &entry, &cursor)) == 0) {
82 char *name_str;
83 char *etype_str;
84 ret = krb5_unparse_name (context, entry.principal, &name_str);
85 if(ret) {
86 krb5_warn(context, ret, "krb5_unparse_name");
87 name_str = NULL; /* XXX */
89 ret = krb5_enctype_to_string(context, entry.keyblock.keytype, &etype_str);
90 if(ret) {
91 krb5_warn(context, ret, "krb5_enctype_to_string");
92 etype_str = NULL; /* XXX */
94 ret = krb5_kt_get_entry(context, dst_keytab,
95 entry.principal,
96 entry.vno,
97 entry.keyblock.keytype,
98 &dummy);
99 if(ret == 0) {
100 /* this entry is already in the new keytab, so no need to
101 copy it; if the keyblocks are not the same, something
102 is weird, so complain about that */
103 if(!compare_keyblock(&entry.keyblock, &dummy.keyblock)) {
104 krb5_warnx(context, "entry with different keyvalue "
105 "already exists for %s, keytype %s, kvno %d",
106 name_str, etype_str, entry.vno);
108 krb5_kt_free_entry(context, &dummy);
109 krb5_kt_free_entry (context, &entry);
110 free(name_str);
111 free(etype_str);
112 continue;
113 } else if(ret != KRB5_KT_NOTFOUND) {
114 krb5_warn (context, ret, "%s: fetching %s/%s/%u",
115 to, name_str, etype_str, entry.vno);
116 krb5_kt_free_entry (context, &entry);
117 free(name_str);
118 free(etype_str);
119 break;
121 if (verbose_flag)
122 fprintf (stderr, "copying %s, keytype %s, kvno %d\n", name_str,
123 etype_str, entry.vno);
124 ret = krb5_kt_add_entry (context, dst_keytab, &entry);
125 krb5_kt_free_entry (context, &entry);
126 if (ret) {
127 krb5_warn (context, ret, "%s: adding %s/%s/%u",
128 to, name_str, etype_str, entry.vno);
129 free(name_str);
130 free(etype_str);
131 break;
133 free(name_str);
134 free(etype_str);
136 krb5_kt_end_seq_get (context, src_keytab, &cursor);
138 out:
139 krb5_kt_close (context, src_keytab);
140 krb5_kt_close (context, dst_keytab);
141 return ret != 0;
145 kt_copy (void *opt, int argc, char **argv)
147 return kt_copy_int(argv[0], argv[1]);
151 srvconv(struct srvconvert_options *opt, int argc, char **argv)
153 char kt4[1024], kt5[1024];
155 snprintf(kt4, sizeof(kt4), "krb4:%s", opt->srvtab_string);
157 if(keytab_string != NULL)
158 return kt_copy_int(kt4, keytab_string);
160 krb5_kt_default_modify_name(context, kt5, sizeof(kt5));
161 return kt_copy_int(kt4, kt5);
165 srvcreate(struct srvcreate_options *opt, int argc, char **argv)
167 char kt4[1024], kt5[1024];
169 snprintf(kt4, sizeof(kt4), "krb4:%s", opt->srvtab_string);
171 if(keytab_string != NULL)
172 return kt_copy_int(keytab_string, kt4);
174 krb5_kt_default_name(context, kt5, sizeof(kt5));
175 return kt_copy_int(kt5, kt4);