2 Unix SMB/CIFS implementation.
4 Kerberos utility functions
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/kerberos.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/kerberos/kerberos.h"
28 #include "auth/kerberos/kerberos_util.h"
29 #include "auth/kerberos/kerberos_srv_keytab.h"
31 static void keytab_principals_free(krb5_context context,
32 uint32_t num_principals,
37 for (i = 0; i < num_principals; i++) {
38 krb5_free_principal(context, set[i]);
42 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
43 uint32_t num_principals,
44 krb5_principal *principals,
45 krb5_principal salt_princ,
47 const char *password_s,
49 krb5_enctype *enctypes,
51 const char **error_string)
58 password.data = discard_const_p(char, password_s);
59 password.length = strlen(password_s);
61 for (i = 0; enctypes[i]; i++) {
62 krb5_keytab_entry entry;
66 ret = smb_krb5_create_key_from_string(context,
73 *error_string = talloc_strdup(parent_ctx,
74 "Failed to create key from string");
80 for (p = 0; p < num_principals; p++) {
82 entry.principal = principals[p];
83 ret = krb5_kt_add_entry(context, keytab, &entry);
85 char *k5_error_string =
86 smb_get_krb5_error_message(context,
88 krb5_unparse_name(context,
89 principals[p], &unparsed);
90 *error_string = talloc_asprintf(parent_ctx,
91 "Failed to add enctype %d entry for "
92 "%s(kvno %d) to keytab: %s\n",
93 (int)enctypes[i], unparsed,
94 kvno, k5_error_string);
97 talloc_free(k5_error_string);
98 krb5_free_keyblock_contents(context,
103 DEBUG(5, ("Added key (kvno %d) to keytab (enctype %d)\n",
104 kvno, (int)enctypes[i]));
106 krb5_free_keyblock_contents(context, KRB5_KT_KEY(&entry));
111 static krb5_error_code create_keytab(TALLOC_CTX *parent_ctx,
112 const char *samAccountName,
114 const char *saltPrincipal,
116 const char *new_secret,
117 const char *old_secret,
118 uint32_t supp_enctypes,
119 uint32_t num_principals,
120 krb5_principal *principals,
121 krb5_context context,
124 const char **perror_string)
127 krb5_principal salt_princ = NULL;
128 krb5_enctype *enctypes;
130 const char *error_string = NULL;
133 /* There is no password here, so nothing to do */
137 mem_ctx = talloc_new(parent_ctx);
139 *perror_string = talloc_strdup(parent_ctx,
140 "unable to allocate tmp_ctx for create_keytab");
144 /* The salt used to generate these entries may be different however,
146 ret = krb5_parse_name(context, saltPrincipal, &salt_princ);
148 *perror_string = smb_get_krb5_error_message(context,
151 talloc_free(mem_ctx);
155 ret = ms_suptypes_to_ietf_enctypes(mem_ctx, supp_enctypes, &enctypes);
157 *perror_string = talloc_asprintf(parent_ctx,
158 "create_keytab: generating list of "
159 "encryption types failed (%s)\n",
160 smb_get_krb5_error_message(context,
165 ret = keytab_add_keys(mem_ctx,
168 salt_princ, kvno, new_secret,
169 context, enctypes, keytab, &error_string);
171 *perror_string = talloc_steal(parent_ctx, error_string);
175 if (old_secret && add_old && kvno != 0) {
176 ret = keytab_add_keys(mem_ctx,
179 salt_princ, kvno - 1, old_secret,
180 context, enctypes, keytab, &error_string);
182 *perror_string = talloc_steal(parent_ctx, error_string);
187 krb5_free_principal(context, salt_princ);
188 talloc_free(mem_ctx);
192 krb5_error_code smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
193 krb5_context context,
194 const char *keytab_name,
195 const char *samAccountName,
199 const char *saltPrincipal,
200 const char *new_secret,
201 const char *old_secret,
203 uint32_t supp_enctypes,
204 bool delete_all_kvno,
205 krb5_keytab *_keytab,
206 const char **perror_string)
210 bool found_previous = false;
212 krb5_principal *principals = NULL;
213 uint32_t num_principals = 0;
215 const char *error_string = NULL;
217 if (keytab_name == NULL) {
221 if (saltPrincipal == NULL) {
222 *perror_string = talloc_strdup(parent_ctx,
223 "No saltPrincipal provided");
227 ret = krb5_kt_resolve(context, keytab_name, &keytab);
229 *perror_string = smb_get_krb5_error_message(context,
234 DEBUG(5, ("Opened keytab %s\n", keytab_name));
236 tmp_ctx = talloc_new(parent_ctx);
238 *perror_string = talloc_strdup(parent_ctx,
239 "Failed to allocate memory context");
243 upper_realm = strupper_talloc(tmp_ctx, realm);
244 if (upper_realm == NULL) {
245 *perror_string = talloc_strdup(parent_ctx,
246 "Cannot allocate memory to upper case realm");
247 talloc_free(tmp_ctx);
251 ret = smb_krb5_create_principals_array(tmp_ctx,
261 *perror_string = talloc_asprintf(parent_ctx,
262 "Failed to load principals from ldb message: %s\n",
267 ret = smb_krb5_remove_obsolete_keytab_entries(tmp_ctx,
276 *perror_string = talloc_asprintf(parent_ctx,
277 "Failed to remove old principals from keytab: %s\n",
282 if (!delete_all_kvno) {
283 /* Create a new keytab. If during the cleanout we found
284 * entires for kvno -1, then don't try and duplicate them.
285 * Otherwise, add kvno, and kvno -1 */
287 ret = create_keytab(tmp_ctx,
288 samAccountName, upper_realm, saltPrincipal,
289 kvno, new_secret, old_secret,
294 found_previous ? false : true,
297 *perror_string = talloc_steal(parent_ctx, error_string);
301 if (ret == 0 && _keytab != NULL) {
302 /* caller wants the keytab handle back */
307 keytab_principals_free(context, num_principals, principals);
308 if (ret != 0 || _keytab == NULL) {
309 krb5_kt_close(context, keytab);
311 talloc_free(tmp_ctx);
315 krb5_error_code smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
316 krb5_context context,
317 const char *new_secret,
318 const char *samAccountName,
320 const char *salt_principal,
323 const char **keytab_name)
326 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
327 const char *rand_string;
328 const char *error_string = NULL;
333 rand_string = generate_random_str(mem_ctx, 16);
335 talloc_free(mem_ctx);
339 *keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s", rand_string);
340 if (*keytab_name == NULL) {
341 talloc_free(mem_ctx);
345 ret = smb_krb5_update_keytab(mem_ctx, context,
346 *keytab_name, samAccountName, realm,
347 NULL, 0, salt_principal, new_secret, NULL,
349 false, keytab, &error_string);
351 talloc_steal(parent_ctx, *keytab_name);
353 DEBUG(0, ("Failed to create in-memory keytab: %s\n",
357 talloc_free(mem_ctx);