build: Assert that auth_domain, auth_builtin, auth_sam, auth_winbind are builtin
[Samba.git] / lib / krb5_wrap / keytab_util.c
blobf9a8679290cf221d6ebde2a1a651df7ad73fb460
1 /*
2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * Copyright (c) 2011 Andrew Bartlett
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "includes.h"
37 #include "krb5_samba.h"
39 static krb5_boolean
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)
45 return false;
46 return true;
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)
54 krb5_error_code ret;
55 krb5_keytab_entry dummy;
57 char *name_str;
58 char *etype_str;
59 ret = krb5_unparse_name (context, entry.principal, &name_str);
60 if(ret) {
61 krb5_set_error_message(context, ret, "krb5_unparse_name");
62 name_str = NULL; /* XXX */
63 return ret;
65 ret = smb_krb5_enctype_to_string(context,
66 KRB5_KEY_TYPE(KRB5_KT_KEY(&entry)),
67 &etype_str);
68 if(ret) {
69 krb5_set_error_message(context, ret, "krb5_enctype_to_string");
70 etype_str = NULL; /* XXX */
71 return ret;
73 ret = krb5_kt_get_entry(context, dst_keytab,
74 entry.principal,
75 entry.vno,
76 KRB5_KEY_TYPE(KRB5_KT_KEY(&entry)),
77 &dummy);
78 if(ret == 0) {
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);
89 free(name_str);
90 free(etype_str);
91 return ret;
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);
96 free(name_str);
97 free(etype_str);
98 return ret;
100 ret = krb5_kt_add_entry (context, dst_keytab, &entry);
101 krb5_kt_free_entry (context, &entry);
102 if (ret) {
103 krb5_set_error_message (context, ret, "adding %s/%s/%u",
104 name_str, etype_str, entry.vno);
105 free(name_str);
106 free(etype_str);
107 return ret;
109 free(name_str);
110 free(etype_str);
111 return ret;
114 krb5_error_code kt_copy(krb5_context context, const char *from, const char *to)
116 krb5_error_code ret;
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);
122 if (ret) {
123 krb5_set_error_message (context, ret, "resolving src keytab `%s'", from);
124 return ret;
127 ret = krb5_kt_resolve (context, to, &dst_keytab);
128 if (ret) {
129 krb5_kt_close (context, src_keytab);
130 krb5_set_error_message (context, ret, "resolving dst keytab `%s'", to);
131 return ret;
134 ret = krb5_kt_start_seq_get (context, src_keytab, &cursor);
135 if (ret) {
136 krb5_set_error_message (context, ret, "krb5_kt_start_seq_get %s", from);
137 goto out;
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);
143 if (ret) {
144 break;
147 krb5_kt_end_seq_get (context, src_keytab, &cursor);
149 out:
150 krb5_kt_close (context, src_keytab);
151 krb5_kt_close (context, dst_keytab);
152 if (ret == KRB5_KT_END) {
153 return 0;
154 } else if (ret == 0) {
155 return EINVAL;
157 return ret;
160 krb5_error_code kt_copy_one_principal(krb5_context context,
161 const char *from,
162 const char *to,
163 const char *principal,
164 krb5_kvno kvno,
165 const krb5_enctype *enctypes)
167 krb5_error_code ret;
168 krb5_keytab src_keytab, dst_keytab;
169 krb5_keytab_entry entry;
170 krb5_principal princ;
171 int i;
172 bool found_one = false;
174 ret = krb5_parse_name (context, principal, &princ);
175 if(ret) {
176 krb5_set_error_message(context, ret, "krb5_unparse_name");
177 return ret;
180 ret = krb5_kt_resolve (context, from, &src_keytab);
181 if (ret) {
182 krb5_set_error_message(context, ret, "resolving src keytab `%s'", from);
183 return ret;
186 ret = krb5_kt_resolve (context, to, &dst_keytab);
187 if (ret) {
188 krb5_kt_close (context, src_keytab);
189 krb5_set_error_message(context, ret, "resolving dst keytab `%s'", to);
190 return ret;
193 for (i=0; enctypes[i]; i++) {
194 ret = krb5_kt_get_entry(context, src_keytab,
195 princ,
196 kvno,
197 enctypes[i],
198 &entry);
199 if (ret == KRB5_KT_NOTFOUND) {
200 continue;
201 } else if (ret) {
202 break;
204 found_one = true;
205 ret = copy_one_entry(context, src_keytab, dst_keytab, entry);
206 if (ret) {
207 break;
210 if (ret == KRB5_KT_NOTFOUND) {
211 if (!found_one) {
212 char *princ_string;
213 int ret2 = krb5_unparse_name (context, princ, &princ_string);
214 if (ret2) {
215 krb5_set_error_message(context, ret,
216 "failed to fetch principal %s",
217 princ_string);
219 } else {
220 /* Not finding an enc type is not an error,
221 * as long as we copied one for the principal */
222 ret = 0;
226 krb5_kt_close (context, src_keytab);
227 krb5_kt_close (context, dst_keytab);
228 return ret;
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,
235 krb5_kvno kvno,
236 krb5_enctype enctype)
238 if (principal) {
239 if (!krb5_principal_compare(context,
240 entry->principal, principal)) {
241 return false;
244 if (kvno) {
245 if (entry->vno != kvno) {
246 return false;
249 if (enctype) {
250 if (KRB5_KEY_TYPE(KRB5_KT_KEY(entry)) != enctype) {
251 return false;
255 return true;
257 #endif