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/kerberos/kerberos.h"
27 #include "auth/kerberos/kerberos_srv_keytab.h"
29 static void keytab_principals_free(krb5_context context, krb5_principal *set)
32 for (i = 0; set[i] != NULL; i++) {
33 krb5_free_principal(context, set[i]);
37 static krb5_error_code principals_from_list(TALLOC_CTX *parent_ctx,
38 const char *samAccountName,
40 const char **SPNs, int num_SPNs,
42 krb5_principal **principals_out,
43 const char **error_string)
49 krb5_principal *principals = NULL;
50 tmp_ctx = talloc_new(parent_ctx);
52 *error_string = "Cannot allocate tmp_ctx";
57 *error_string = "Cannot make principal without a realm";
62 upper_realm = strupper_talloc(tmp_ctx, realm);
64 *error_string = "Cannot allocate full upper case realm";
69 principals = talloc_zero_array(tmp_ctx, krb5_principal,
70 num_SPNs ? (num_SPNs + 2) : 2);
72 for (i = 0; num_SPNs && i < num_SPNs; i++) {
73 ret = krb5_parse_name(context, SPNs[i], &principals[i]);
76 *error_string = smb_get_krb5_error_message(context, ret,
83 ret = smb_krb5_make_principal(context, &principals[i],
84 upper_realm, samAccountName,
87 *error_string = smb_get_krb5_error_message(context, ret,
95 keytab_principals_free(context, principals);
97 *principals_out = talloc_steal(parent_ctx, principals);
103 static krb5_error_code salt_principal(TALLOC_CTX *parent_ctx,
104 const char *samAccountName,
106 const char *saltPrincipal,
107 krb5_context context,
108 krb5_principal *salt_princ,
109 const char **error_string)
113 char *machine_username;
121 ret = krb5_parse_name(context, saltPrincipal, salt_princ);
123 *error_string = smb_get_krb5_error_message(
124 context, ret, parent_ctx);
129 if (!samAccountName) {
130 (*error_string) = "Cannot determine salt principal, no "
131 "saltPrincipal or samAccountName specified";
136 *error_string = "Cannot make principal without a realm";
140 tmp_ctx = talloc_new(parent_ctx);
142 *error_string = "Cannot allocate tmp_ctx";
146 machine_username = talloc_strdup(tmp_ctx, samAccountName);
147 if (!machine_username) {
148 *error_string = "Cannot duplicate samAccountName";
149 talloc_free(tmp_ctx);
153 if (machine_username[strlen(machine_username)-1] == '$') {
154 machine_username[strlen(machine_username)-1] = '\0';
157 lower_realm = strlower_talloc(tmp_ctx, realm);
159 *error_string = "Cannot allocate to lower case realm";
160 talloc_free(tmp_ctx);
164 upper_realm = strupper_talloc(tmp_ctx, realm);
166 *error_string = "Cannot allocate to upper case realm";
167 talloc_free(tmp_ctx);
171 salt_body = talloc_asprintf(tmp_ctx, "%s.%s",
172 machine_username, lower_realm);
174 *error_string = "Cannot form salt principal body";
175 talloc_free(tmp_ctx);
179 ret = smb_krb5_make_principal(context, salt_princ, upper_realm,
180 "host", salt_body, NULL);
182 *error_string = smb_get_krb5_error_message(context,
186 talloc_free(tmp_ctx);
190 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
191 krb5_principal *principals,
192 krb5_principal salt_princ,
194 const char *password_s,
195 krb5_context context,
196 krb5_enctype *enctypes,
198 const char **error_string)
205 password.data = discard_const_p(char, password_s);
206 password.length = strlen(password_s);
208 for (i = 0; enctypes[i]; i++) {
209 krb5_keytab_entry entry;
213 ret = smb_krb5_create_key_from_string(context,
218 KRB5_KT_KEY(&entry));
225 for (p = 0; principals[p]; p++) {
227 entry.principal = principals[p];
228 ret = krb5_kt_add_entry(context, keytab, &entry);
230 char *k5_error_string =
231 smb_get_krb5_error_message(context,
233 krb5_unparse_name(context,
234 principals[p], &unparsed);
235 *error_string = talloc_asprintf(parent_ctx,
236 "Failed to add enctype %d entry for "
237 "%s(kvno %d) to keytab: %s\n",
238 (int)enctypes[i], unparsed,
239 kvno, k5_error_string);
242 talloc_free(k5_error_string);
243 krb5_free_keyblock_contents(context,
244 KRB5_KT_KEY(&entry));
248 DEBUG(5, ("Added key (kvno %d) to keytab (enctype %d)\n",
249 kvno, (int)enctypes[i]));
251 krb5_free_keyblock_contents(context, KRB5_KT_KEY(&entry));
256 static krb5_error_code create_keytab(TALLOC_CTX *parent_ctx,
257 const char *samAccountName,
259 const char *saltPrincipal,
261 const char *new_secret,
262 const char *old_secret,
263 uint32_t supp_enctypes,
264 krb5_principal *principals,
265 krb5_context context,
268 const char **error_string)
271 krb5_principal salt_princ = NULL;
272 krb5_enctype *enctypes;
276 /* There is no password here, so nothing to do */
280 mem_ctx = talloc_new(parent_ctx);
282 *error_string = talloc_strdup(parent_ctx,
283 "unable to allocate tmp_ctx for create_keytab");
287 /* The salt used to generate these entries may be different however,
289 ret = salt_principal(mem_ctx, samAccountName, realm, saltPrincipal,
290 context, &salt_princ, error_string);
292 talloc_free(mem_ctx);
296 ret = ms_suptypes_to_ietf_enctypes(mem_ctx, supp_enctypes, &enctypes);
298 *error_string = talloc_asprintf(parent_ctx,
299 "create_keytab: generating list of "
300 "encryption types failed (%s)\n",
301 smb_get_krb5_error_message(context,
306 ret = keytab_add_keys(mem_ctx, principals,
307 salt_princ, kvno, new_secret,
308 context, enctypes, keytab, error_string);
310 talloc_steal(parent_ctx, *error_string);
314 if (old_secret && add_old && kvno != 0) {
315 ret = keytab_add_keys(mem_ctx, principals,
316 salt_princ, kvno - 1, old_secret,
317 context, enctypes, keytab, error_string);
319 talloc_steal(parent_ctx, *error_string);
324 krb5_free_principal(context, salt_princ);
325 talloc_free(mem_ctx);
330 * Walk the keytab, looking for entries of this principal name,
331 * with KVNO other than current kvno -1.
333 * These entries are now stale,
334 * we only keep the current and previous entries around.
336 * Inspired by the code in Samba3 for 'use kerberos keytab'.
339 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
341 krb5_principal *principals,
342 bool delete_all_kvno,
343 krb5_context context,
345 bool *found_previous,
346 const char **error_string)
348 krb5_error_code ret, ret2;
349 krb5_kt_cursor cursor;
350 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
356 *found_previous = false;
358 /* for each entry in the keytab */
359 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
363 #ifdef HEIM_ERR_OPNOTSUPP
364 case HEIM_ERR_OPNOTSUPP:
368 /* no point enumerating if there isn't anything here */
369 talloc_free(mem_ctx);
372 *error_string = talloc_asprintf(parent_ctx,
373 "failed to open keytab for read of old entries: %s\n",
374 smb_get_krb5_error_message(context, ret, mem_ctx));
375 talloc_free(mem_ctx);
381 bool matched = false;
382 krb5_keytab_entry entry;
383 ret = krb5_kt_next_entry(context, keytab, &entry, &cursor);
387 for (i = 0; principals[i]; i++) {
388 /* if it matches our principal */
389 if (smb_krb5_kt_compare(context, &entry,
390 principals[i], 0, 0)) {
398 * it wasn't the one we were looking for anyway */
399 krb5_kt_free_entry(context, &entry);
403 /* delete it, if it is not kvno -1 */
404 if (entry.vno != (kvno - 1 )) {
405 /* Release the enumeration. We are going to
406 * have to start this from the top again,
407 * because deletes during enumeration may not
408 * always be consistent.
410 * Also, the enumeration locks a FILE: keytab
413 krb5_kt_end_seq_get(context, keytab, &cursor);
415 ret = krb5_kt_remove_entry(context, keytab, &entry);
416 krb5_kt_free_entry(context, &entry);
418 /* Deleted: Restart from the top */
419 ret2 = krb5_kt_start_seq_get(context, keytab, &cursor);
421 krb5_kt_free_entry(context, &entry);
422 DEBUG(1, ("failed to restart enumeration of keytab: %s\n",
423 smb_get_krb5_error_message(context,
426 talloc_free(mem_ctx);
435 *found_previous = true;
438 /* Free the entry, we don't need it any more */
439 krb5_kt_free_entry(context, &entry);
441 krb5_kt_end_seq_get(context, keytab, &cursor);
451 *error_string = talloc_asprintf(parent_ctx,
452 "failed in deleting old entries for principal: %s\n",
453 smb_get_krb5_error_message(context, ret, mem_ctx));
455 talloc_free(mem_ctx);
459 krb5_error_code smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
460 krb5_context context,
461 const char *keytab_name,
462 const char *samAccountName,
466 const char *saltPrincipal,
467 const char *new_secret,
468 const char *old_secret,
470 uint32_t supp_enctypes,
471 bool delete_all_kvno,
472 krb5_keytab *_keytab,
473 const char **error_string)
479 krb5_principal *principals = NULL;
481 if (keytab_name == NULL) {
485 ret = krb5_kt_resolve(context, keytab_name, &keytab);
487 *error_string = smb_get_krb5_error_message(context,
492 DEBUG(5, ("Opened keytab %s\n", keytab_name));
494 tmp_ctx = talloc_new(parent_ctx);
499 /* Get the principal we will store the new keytab entries under */
500 ret = principals_from_list(tmp_ctx,
501 samAccountName, realm, SPNs, num_SPNs,
502 context, &principals, error_string);
505 *error_string = talloc_asprintf(parent_ctx,
506 "Failed to load principals from ldb message: %s\n",
511 ret = remove_old_entries(tmp_ctx, kvno, principals, delete_all_kvno,
512 context, keytab, &found_previous, error_string);
514 *error_string = talloc_asprintf(parent_ctx,
515 "Failed to remove old principals from keytab: %s\n",
520 if (!delete_all_kvno) {
521 /* Create a new keytab. If during the cleanout we found
522 * entires for kvno -1, then don't try and duplicate them.
523 * Otherwise, add kvno, and kvno -1 */
525 ret = create_keytab(tmp_ctx,
526 samAccountName, realm, saltPrincipal,
527 kvno, new_secret, old_secret,
528 supp_enctypes, principals,
530 found_previous ? false : true,
533 talloc_steal(parent_ctx, *error_string);
537 if (ret == 0 && _keytab != NULL) {
538 /* caller wants the keytab handle back */
543 keytab_principals_free(context, principals);
544 if (ret != 0 || _keytab == NULL) {
545 krb5_kt_close(context, keytab);
547 talloc_free(tmp_ctx);
551 krb5_error_code smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
552 krb5_context context,
553 const char *new_secret,
554 const char *samAccountName,
558 const char **keytab_name)
561 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
562 const char *rand_string;
563 const char *error_string;
568 rand_string = generate_random_str(mem_ctx, 16);
570 talloc_free(mem_ctx);
574 *keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s", rand_string);
575 if (*keytab_name == NULL) {
576 talloc_free(mem_ctx);
581 ret = smb_krb5_update_keytab(mem_ctx, context,
582 *keytab_name, samAccountName, realm,
583 NULL, 0, NULL, new_secret, NULL,
585 false, keytab, &error_string);
587 talloc_steal(parent_ctx, *keytab_name);
589 DEBUG(0, ("Failed to create in-memory keytab: %s\n",
593 talloc_free(mem_ctx);