2 Unix SMB/CIFS implementation.
4 Kerberos utility functions for GENSEC
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/>.
24 #include "system/kerberos.h"
25 #include "auth/kerberos/kerberos.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/credentials/credentials_krb5.h"
29 struct principal_container {
30 struct smb_krb5_context *smb_krb5_context;
31 krb5_principal principal;
34 static int free_principal(struct principal_container *pc)
36 /* current heimdal - 0.6.3, which we need anyway, fixes segfaults here */
37 krb5_free_principal(pc->smb_krb5_context->krb5_context, pc->principal);
42 static krb5_error_code salt_principal_from_credentials(TALLOC_CTX *parent_ctx,
43 struct cli_credentials *machine_account,
44 struct smb_krb5_context *smb_krb5_context,
45 krb5_principal *salt_princ)
48 char *machine_username;
51 const char *salt_principal;
52 struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
57 salt_principal = cli_credentials_get_salt_principal(machine_account);
59 ret = krb5_parse_name(smb_krb5_context->krb5_context, salt_principal, salt_princ);
61 machine_username = talloc_strdup(mem_ctx, cli_credentials_get_username(machine_account));
63 if (!machine_username) {
68 if (machine_username[strlen(machine_username)-1] == '$') {
69 machine_username[strlen(machine_username)-1] = '\0';
71 lower_realm = strlower_talloc(mem_ctx, cli_credentials_get_realm(machine_account));
77 salt_body = talloc_asprintf(mem_ctx, "%s.%s", machine_username,
84 ret = krb5_make_principal(smb_krb5_context->krb5_context, salt_princ,
85 cli_credentials_get_realm(machine_account),
86 "host", salt_body, NULL);
90 /* This song-and-dance effectivly puts the principal
91 * into talloc, so we can't loose it. */
92 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
93 mem_ctx->principal = *salt_princ;
94 talloc_set_destructor(mem_ctx, free_principal);
99 /* Obtain the principal set on this context. Requires a
100 * smb_krb5_context because we are doing krb5 principal parsing with
101 * the library routines. The returned princ is placed in the talloc
102 * system by means of a destructor (do *not* free). */
104 krb5_error_code principal_from_credentials(TALLOC_CTX *parent_ctx,
105 struct cli_credentials *credentials,
106 struct smb_krb5_context *smb_krb5_context,
107 krb5_principal *princ)
110 const char *princ_string;
111 struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
116 princ_string = cli_credentials_get_principal(credentials, mem_ctx);
118 /* A NULL here has meaning, as the gssapi server case will
119 * then use the principal from the client */
121 talloc_free(mem_ctx);
126 ret = krb5_parse_name(smb_krb5_context->krb5_context,
127 princ_string, princ);
130 /* This song-and-dance effectivly puts the principal
131 * into talloc, so we can't loose it. */
132 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
133 mem_ctx->principal = *princ;
134 talloc_set_destructor(mem_ctx, free_principal);
140 * Return a freshly allocated ccache (destroyed by destructor on child
141 * of parent_ctx), for a given set of client credentials
144 krb5_error_code kinit_to_ccache(TALLOC_CTX *parent_ctx,
145 struct cli_credentials *credentials,
146 struct smb_krb5_context *smb_krb5_context,
150 const char *password;
152 krb5_principal princ;
154 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
160 ret = principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &princ);
162 talloc_free(mem_ctx);
166 password = cli_credentials_get_password(credentials);
171 ret = kerberos_kinit_password_cc(smb_krb5_context->krb5_context, ccache,
173 password, NULL, &kdc_time);
175 /* No password available, try to use a keyblock instead */
177 krb5_keyblock keyblock;
178 const struct samr_Password *mach_pwd;
179 mach_pwd = cli_credentials_get_nt_hash(credentials, mem_ctx);
181 talloc_free(mem_ctx);
182 DEBUG(1, ("kinit_to_ccache: No password available for kinit\n"));
185 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
186 ETYPE_ARCFOUR_HMAC_MD5,
187 mach_pwd->hash, sizeof(mach_pwd->hash),
191 ret = kerberos_kinit_keyblock_cc(smb_krb5_context->krb5_context, ccache,
193 &keyblock, NULL, &kdc_time);
194 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &keyblock);
198 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
199 /* Perhaps we have been given an invalid skew, so try again without it */
200 time_t t = time(NULL);
201 krb5_set_real_time(smb_krb5_context->krb5_context, t, 0);
203 /* not a skew problem */
208 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
209 DEBUG(1,("kinit for %s failed (%s)\n",
210 cli_credentials_get_principal(credentials, mem_ctx),
211 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
213 talloc_free(mem_ctx);
217 /* cope with ticket being in the future due to clock skew */
218 if ((unsigned)kdc_time > time(NULL)) {
219 time_t t = time(NULL);
220 int time_offset =(unsigned)kdc_time-t;
221 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
222 krb5_set_real_time(smb_krb5_context->krb5_context, t + time_offset + 1, 0);
225 if (ret == KRB5KDC_ERR_PREAUTH_FAILED && cli_credentials_wrong_password(credentials)) {
226 ret = kinit_to_ccache(parent_ctx,
232 DEBUG(1,("kinit for %s failed (%s)\n",
233 cli_credentials_get_principal(credentials, mem_ctx),
234 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
236 talloc_free(mem_ctx);
239 talloc_free(mem_ctx);
243 static int free_keytab(struct keytab_container *ktc)
245 krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
250 int smb_krb5_open_keytab(TALLOC_CTX *mem_ctx,
251 struct smb_krb5_context *smb_krb5_context,
252 const char *keytab_name, struct keytab_container **ktc)
256 ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
258 DEBUG(1,("failed to open krb5 keytab: %s\n",
259 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
264 *ktc = talloc(mem_ctx, struct keytab_container);
269 (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
270 (*ktc)->keytab = keytab;
271 talloc_set_destructor(*ktc, free_keytab);
276 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
277 const char *princ_string,
278 krb5_principal princ,
279 krb5_principal salt_princ,
281 const char *password_s,
282 struct smb_krb5_context *smb_krb5_context,
283 const char **enctype_strings,
289 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
294 password.data = discard_const_p(char *, password_s);
295 password.length = strlen(password_s);
297 for (i=0; enctype_strings[i]; i++) {
298 krb5_keytab_entry entry;
299 krb5_enctype enctype;
300 ret = krb5_string_to_enctype(smb_krb5_context->krb5_context, enctype_strings[i], &enctype);
302 DEBUG(1, ("Failed to interpret %s as a krb5 encryption type: %s\n",
304 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
306 talloc_free(mem_ctx);
309 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context,
310 salt_princ, &password, &entry.keyblock, enctype);
312 talloc_free(mem_ctx);
316 entry.principal = princ;
318 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
320 DEBUG(1, ("Failed to add %s entry for %s(kvno %d) to keytab: %s\n",
324 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
326 talloc_free(mem_ctx);
327 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
331 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
333 enctype_strings[i]));
335 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
337 talloc_free(mem_ctx);
341 static int create_keytab(TALLOC_CTX *parent_ctx,
342 struct cli_credentials *machine_account,
343 struct smb_krb5_context *smb_krb5_context,
344 const char **enctype_strings,
349 const char *password_s;
350 const char *old_secret;
352 krb5_principal salt_princ;
353 krb5_principal princ;
354 const char *princ_string;
356 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
361 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
362 /* Get the principal we will store the new keytab entries under */
363 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
365 DEBUG(1,("create_keytab: makeing krb5 principal failed (%s)\n",
366 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
368 talloc_free(mem_ctx);
372 /* The salt used to generate these entries may be different however, fetch that */
373 ret = salt_principal_from_credentials(mem_ctx, machine_account,
377 DEBUG(1,("create_keytab: makeing salt principal failed (%s)\n",
378 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
380 talloc_free(mem_ctx);
384 /* Finally, do the dance to get the password to put in the entry */
385 password_s = cli_credentials_get_password(machine_account);
387 krb5_keytab_entry entry;
388 const struct samr_Password *mach_pwd;
390 if (!str_list_check(enctype_strings, "arcfour-hmac-md5")) {
391 DEBUG(1, ("Asked to create keytab, but with only an NT hash supplied, "
392 "but not listing arcfour-hmac-md5 as an enc type to include in the keytab!\n"));
393 talloc_free(mem_ctx);
397 /* If we don't have the plaintext password, try for
398 * the MD4 password hash */
399 mach_pwd = cli_credentials_get_nt_hash(machine_account, mem_ctx);
401 /* OK, nothing to do here */
402 talloc_free(mem_ctx);
405 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
406 ETYPE_ARCFOUR_HMAC_MD5,
407 mach_pwd->hash, sizeof(mach_pwd->hash),
410 DEBUG(1, ("create_keytab: krb5_keyblock_init failed: %s\n",
411 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
413 talloc_free(mem_ctx);
417 entry.principal = princ;
418 entry.vno = cli_credentials_get_kvno(machine_account);
419 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
421 DEBUG(1, ("Failed to add ARCFOUR_HMAC (only) entry for %s to keytab: %s",
422 cli_credentials_get_principal(machine_account, mem_ctx),
423 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
425 talloc_free(mem_ctx);
426 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
430 DEBUG(5, ("Added %s(kvno %d) to keytab (arcfour-hmac-md5)\n",
431 cli_credentials_get_principal(machine_account, mem_ctx),
432 cli_credentials_get_kvno(machine_account)));
434 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
436 /* Can't go any further, we only have this one key */
437 talloc_free(mem_ctx);
441 kvno = cli_credentials_get_kvno(machine_account);
442 /* good, we actually have the real plaintext */
443 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
444 kvno, password_s, smb_krb5_context,
445 enctype_strings, keytab);
447 talloc_free(mem_ctx);
451 if (!add_old || kvno == 0) {
452 talloc_free(mem_ctx);
456 old_secret = cli_credentials_get_old_password(machine_account);
458 talloc_free(mem_ctx);
462 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
463 kvno - 1, old_secret, smb_krb5_context,
464 enctype_strings, keytab);
466 talloc_free(mem_ctx);
470 talloc_free(mem_ctx);
476 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
478 * These entries are now stale, we only keep the current, and previous entries around.
480 * Inspired by the code in Samba3 for 'use kerberos keytab'.
484 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
485 struct cli_credentials *machine_account,
486 struct smb_krb5_context *smb_krb5_context,
487 krb5_keytab keytab, BOOL *found_previous)
489 krb5_error_code ret, ret2;
490 krb5_kt_cursor cursor;
491 krb5_principal princ;
493 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
494 const char *princ_string;
499 *found_previous = False;
500 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
502 /* Get the principal we will store the new keytab entries under */
503 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
505 DEBUG(1,("update_keytab: makeing krb5 principal failed (%s)\n",
506 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
508 talloc_free(mem_ctx);
512 kvno = cli_credentials_get_kvno(machine_account);
514 /* for each entry in the keytab */
515 ret = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
519 case HEIM_ERR_OPNOTSUPP:
522 /* no point enumerating if there isn't anything here */
523 talloc_free(mem_ctx);
526 DEBUG(1,("failed to open keytab for read of old entries: %s\n",
527 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
529 talloc_free(mem_ctx);
534 krb5_keytab_entry entry;
535 ret = krb5_kt_next_entry(smb_krb5_context->krb5_context, keytab, &entry, &cursor);
539 /* if it matches our principal */
540 if (!krb5_kt_compare(smb_krb5_context->krb5_context, &entry, princ, 0, 0)) {
541 /* Free the entry, it wasn't the one we were looking for anyway */
542 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
546 /* delete it, if it is not kvno -1 */
547 if (entry.vno != (kvno - 1 )) {
548 /* Release the enumeration. We are going to
549 * have to start this from the top again,
550 * because deletes during enumeration may not
551 * always be consistant.
553 * Also, the enumeration locks a FILE: keytab
556 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
558 ret = krb5_kt_remove_entry(smb_krb5_context->krb5_context, keytab, &entry);
559 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
561 /* Deleted: Restart from the top */
562 ret2 = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
564 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
565 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
566 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
569 talloc_free(mem_ctx);
578 *found_previous = True;
581 /* Free the entry, we don't need it any more */
582 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
586 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
596 DEBUG(1,("failed in deleting old entries for principal: %s: %s\n",
598 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
601 talloc_free(mem_ctx);
605 int smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
606 struct cli_credentials *machine_account,
607 struct smb_krb5_context *smb_krb5_context,
608 const char **enctype_strings,
609 struct keytab_container *keytab_container)
613 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
618 ret = remove_old_entries(mem_ctx, machine_account,
619 smb_krb5_context, keytab_container->keytab, &found_previous);
621 talloc_free(mem_ctx);
625 /* Create a new keytab. If during the cleanout we found
626 * entires for kvno -1, then don't try and duplicate them.
627 * Otherwise, add kvno, and kvno -1 */
629 ret = create_keytab(mem_ctx, machine_account, smb_krb5_context,
631 keytab_container->keytab,
632 found_previous ? False : True);
633 talloc_free(mem_ctx);
637 _PUBLIC_ int smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
638 struct cli_credentials *machine_account,
639 struct smb_krb5_context *smb_krb5_context,
640 const char **enctype_strings,
641 struct keytab_container **keytab_container)
644 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
645 const char *rand_string;
646 const char *keytab_name;
651 *keytab_container = talloc(mem_ctx, struct keytab_container);
653 rand_string = generate_random_str(mem_ctx, 16);
655 talloc_free(mem_ctx);
659 keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s",
662 talloc_free(mem_ctx);
666 ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, keytab_container);
671 ret = smb_krb5_update_keytab(mem_ctx, machine_account, smb_krb5_context, enctype_strings, *keytab_container);
673 talloc_steal(parent_ctx, *keytab_container);
675 *keytab_container = NULL;
677 talloc_free(mem_ctx);