2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * Copyright (c) 2011 Andrew Bartlett
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "krb5_samba.h"
40 compare_keyblock(const krb5_keyblock
*a
, const krb5_keyblock
*b
)
42 if (KRB5_KEY_TYPE(a
) != KRB5_KEY_TYPE(b
) ||
43 KRB5_KEY_LENGTH(a
) != KRB5_KEY_LENGTH(b
) ||
44 memcmp(KRB5_KEY_DATA(a
), KRB5_KEY_DATA(b
), KRB5_KEY_LENGTH(a
)) != 0)
49 static krb5_error_code
copy_one_entry(krb5_context context
,
50 krb5_keytab src_keytab
,
51 krb5_keytab dst_keytab
,
52 krb5_keytab_entry entry
)
55 krb5_keytab_entry dummy
;
59 ret
= krb5_unparse_name (context
, entry
.principal
, &name_str
);
61 krb5_set_error_message(context
, ret
, "krb5_unparse_name");
62 name_str
= NULL
; /* XXX */
65 ret
= smb_krb5_enctype_to_string(context
,
66 KRB5_KEY_TYPE(KRB5_KT_KEY(&entry
)),
69 krb5_set_error_message(context
, ret
, "krb5_enctype_to_string");
70 etype_str
= NULL
; /* XXX */
73 ret
= krb5_kt_get_entry(context
, dst_keytab
,
76 KRB5_KEY_TYPE(KRB5_KT_KEY(&entry
)),
79 /* this entry is already in the new keytab, so no need to
80 copy it; if the keyblocks are not the same, something
81 is weird, so complain about that */
82 if (!compare_keyblock(KRB5_KT_KEY(&entry
), KRB5_KT_KEY(&dummy
))) {
83 DEBUG(2, ("copy_one_entry: entry with different keyvalue "
84 "already exists for %s, keytype %s, kvno %d",
85 name_str
, etype_str
, entry
.vno
));
87 krb5_kt_free_entry(context
, &dummy
);
88 krb5_kt_free_entry (context
, &entry
);
92 } else if(ret
!= KRB5_KT_NOTFOUND
) {
93 krb5_set_error_message (context
, ret
, "fetching %s/%s/%u",
94 name_str
, etype_str
, entry
.vno
);
95 krb5_kt_free_entry (context
, &entry
);
100 ret
= krb5_kt_add_entry (context
, dst_keytab
, &entry
);
101 krb5_kt_free_entry (context
, &entry
);
103 krb5_set_error_message (context
, ret
, "adding %s/%s/%u",
104 name_str
, etype_str
, entry
.vno
);
114 krb5_error_code
kt_copy(krb5_context context
, const char *from
, const char *to
)
117 krb5_keytab src_keytab
, dst_keytab
;
118 krb5_kt_cursor cursor
;
119 krb5_keytab_entry entry
;
121 ret
= krb5_kt_resolve (context
, from
, &src_keytab
);
123 krb5_set_error_message (context
, ret
, "resolving src keytab `%s'", from
);
127 ret
= krb5_kt_resolve (context
, to
, &dst_keytab
);
129 krb5_kt_close (context
, src_keytab
);
130 krb5_set_error_message (context
, ret
, "resolving dst keytab `%s'", to
);
134 ret
= krb5_kt_start_seq_get (context
, src_keytab
, &cursor
);
136 krb5_set_error_message (context
, ret
, "krb5_kt_start_seq_get %s", from
);
140 while((ret
= krb5_kt_next_entry(context
, src_keytab
,
141 &entry
, &cursor
)) == 0) {
142 ret
= copy_one_entry(context
, src_keytab
, dst_keytab
, entry
);
147 krb5_kt_end_seq_get (context
, src_keytab
, &cursor
);
150 krb5_kt_close (context
, src_keytab
);
151 krb5_kt_close (context
, dst_keytab
);
152 if (ret
== KRB5_KT_END
) {
154 } else if (ret
== 0) {
160 krb5_error_code
kt_copy_one_principal(krb5_context context
,
163 const char *principal
,
165 krb5_enctype
*enctypes
)
168 krb5_keytab src_keytab
, dst_keytab
;
169 krb5_keytab_entry entry
;
170 krb5_principal princ
;
172 bool found_one
= false;
174 ret
= krb5_parse_name (context
, principal
, &princ
);
176 krb5_set_error_message(context
, ret
, "krb5_unparse_name");
180 ret
= krb5_kt_resolve (context
, from
, &src_keytab
);
182 krb5_set_error_message(context
, ret
, "resolving src keytab `%s'", from
);
186 ret
= krb5_kt_resolve (context
, to
, &dst_keytab
);
188 krb5_kt_close (context
, src_keytab
);
189 krb5_set_error_message(context
, ret
, "resolving dst keytab `%s'", to
);
193 for (i
=0; enctypes
[i
]; i
++) {
194 ret
= krb5_kt_get_entry(context
, src_keytab
,
199 if (ret
== KRB5_KT_NOTFOUND
) {
205 ret
= copy_one_entry(context
, src_keytab
, dst_keytab
, entry
);
210 if (ret
== KRB5_KT_NOTFOUND
) {
213 int ret2
= krb5_unparse_name (context
, princ
, &princ_string
);
215 krb5_set_error_message(context
, ret
,
216 "failed to fetch principal %s",
220 /* Not finding an enc type is not an error,
221 * as long as we copied one for the principal */
226 krb5_kt_close (context
, src_keytab
);
227 krb5_kt_close (context
, dst_keytab
);
231 #if !defined(HAVE_KRB5_KT_COMPARE)
232 krb5_boolean
smb_krb5_kt_compare(krb5_context context
,
233 krb5_keytab_entry
*entry
,
234 krb5_const_principal principal
,
236 krb5_enctype enctype
)
239 if (!krb5_principal_compare(context
,
240 entry
->principal
, principal
)) {
245 if (entry
->vno
!= kvno
) {
250 if (KRB5_KEY_TYPE(KRB5_KT_KEY(entry
)) != enctype
) {