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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "system/kerberos.h"
26 #include "auth/kerberos/kerberos.h"
27 #include "auth/credentials/credentials.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);
242 static int free_keytab(struct keytab_container *ktc)
244 krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
249 int smb_krb5_open_keytab(TALLOC_CTX *mem_ctx,
250 struct smb_krb5_context *smb_krb5_context,
251 const char *keytab_name, struct keytab_container **ktc)
255 ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
257 DEBUG(1,("failed to open krb5 keytab: %s\n",
258 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
263 *ktc = talloc(mem_ctx, struct keytab_container);
268 (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
269 (*ktc)->keytab = keytab;
270 talloc_set_destructor(*ktc, free_keytab);
275 struct enctypes_container {
276 struct smb_krb5_context *smb_krb5_context;
277 krb5_enctype *enctypes;
280 static int free_enctypes(struct enctypes_container *etc)
282 free_kerberos_etypes(etc->smb_krb5_context->krb5_context, etc->enctypes);
286 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
287 const char *princ_string,
288 krb5_principal princ,
289 krb5_principal salt_princ,
291 const char *password_s,
292 struct smb_krb5_context *smb_krb5_context,
297 krb5_enctype *enctypes;
298 char *enctype_string;
299 struct enctypes_container *etc;
301 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
306 etc = talloc(mem_ctx, struct enctypes_container);
308 talloc_free(mem_ctx);
311 ret = get_kerberos_allowed_etypes(smb_krb5_context->krb5_context,
314 DEBUG(1,("keytab_add_keys: getting encrption types failed (%s)\n",
315 error_message(ret)));
316 talloc_free(mem_ctx);
320 etc->smb_krb5_context = talloc_reference(etc, smb_krb5_context);
321 etc->enctypes = enctypes;
323 talloc_set_destructor(etc, free_enctypes);
325 password.data = discard_const_p(char *, password_s);
326 password.length = strlen(password_s);
328 for (i=0; enctypes[i]; i++) {
329 krb5_keytab_entry entry;
330 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context,
331 salt_princ, &password, &entry.keyblock, enctypes[i]);
333 talloc_free(mem_ctx);
337 entry.principal = princ;
339 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
340 enctype_string = NULL;
341 krb5_enctype_to_string(smb_krb5_context->krb5_context, enctypes[i], &enctype_string);
343 DEBUG(1, ("Failed to add %s entry for %s(kvno %d) to keytab: %s\n",
347 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
349 talloc_free(mem_ctx);
350 free(enctype_string);
351 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
355 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
358 free(enctype_string);
360 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
362 talloc_free(mem_ctx);
366 static int create_keytab(TALLOC_CTX *parent_ctx,
367 struct cli_credentials *machine_account,
368 struct smb_krb5_context *smb_krb5_context,
373 const char *password_s;
374 char *enctype_string;
375 const char *old_secret;
377 krb5_principal salt_princ;
378 krb5_principal princ;
379 const char *princ_string;
381 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
386 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
387 /* Get the principal we will store the new keytab entries under */
388 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
390 DEBUG(1,("create_keytab: makeing krb5 principal failed (%s)\n",
391 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
393 talloc_free(mem_ctx);
397 /* The salt used to generate these entries may be different however, fetch that */
398 ret = salt_principal_from_credentials(mem_ctx, machine_account,
402 DEBUG(1,("create_keytab: makeing salt principal failed (%s)\n",
403 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
405 talloc_free(mem_ctx);
409 /* Finally, do the dance to get the password to put in the entry */
410 password_s = cli_credentials_get_password(machine_account);
412 /* If we don't have the plaintext password, try for
413 * the MD4 password hash */
415 krb5_keytab_entry entry;
416 const struct samr_Password *mach_pwd;
417 mach_pwd = cli_credentials_get_nt_hash(machine_account, mem_ctx);
419 DEBUG(1, ("create_keytab: Domain trust informaton for account %s not available\n",
420 cli_credentials_get_principal(machine_account, mem_ctx)));
421 talloc_free(mem_ctx);
424 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
425 ETYPE_ARCFOUR_HMAC_MD5,
426 mach_pwd->hash, sizeof(mach_pwd->hash),
429 DEBUG(1, ("create_keytab: krb5_keyblock_init failed: %s\n",
430 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
432 talloc_free(mem_ctx);
436 entry.principal = princ;
437 entry.vno = cli_credentials_get_kvno(machine_account);
438 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
440 DEBUG(1, ("Failed to add ARCFOUR_HMAC (only) entry for %s to keytab: %s",
441 cli_credentials_get_principal(machine_account, mem_ctx),
442 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
444 talloc_free(mem_ctx);
445 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
449 krb5_enctype_to_string(smb_krb5_context->krb5_context,
450 ETYPE_ARCFOUR_HMAC_MD5,
452 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
453 cli_credentials_get_principal(machine_account, mem_ctx),
454 cli_credentials_get_kvno(machine_account),
456 free(enctype_string);
458 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
460 /* Can't go any further, we only have this one key */
461 talloc_free(mem_ctx);
465 kvno = cli_credentials_get_kvno(machine_account);
466 /* good, we actually have the real plaintext */
467 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
468 kvno, password_s, smb_krb5_context, keytab);
470 talloc_free(mem_ctx);
474 if (!add_old || kvno == 0) {
475 talloc_free(mem_ctx);
479 old_secret = cli_credentials_get_old_password(machine_account);
481 talloc_free(mem_ctx);
485 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
486 kvno - 1, old_secret, smb_krb5_context, keytab);
488 talloc_free(mem_ctx);
492 talloc_free(mem_ctx);
498 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
500 * These entries are now stale, we only keep the current, and previous entries around.
502 * Inspired by the code in Samba3 for 'use kerberos keytab'.
506 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
507 struct cli_credentials *machine_account,
508 struct smb_krb5_context *smb_krb5_context,
509 krb5_keytab keytab, BOOL *found_previous)
511 krb5_error_code ret, ret2;
512 krb5_kt_cursor cursor;
513 krb5_principal princ;
515 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
516 const char *princ_string;
521 *found_previous = False;
522 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
524 /* Get the principal we will store the new keytab entries under */
525 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
527 DEBUG(1,("update_keytab: makeing krb5 principal failed (%s)\n",
528 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
530 talloc_free(mem_ctx);
534 kvno = cli_credentials_get_kvno(machine_account);
536 /* for each entry in the keytab */
537 ret = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
541 case HEIM_ERR_OPNOTSUPP:
544 /* no point enumerating if there isn't anything here */
545 talloc_free(mem_ctx);
548 DEBUG(1,("failed to open keytab for read of old entries: %s\n",
549 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
551 talloc_free(mem_ctx);
556 krb5_keytab_entry entry;
557 ret = krb5_kt_next_entry(smb_krb5_context->krb5_context, keytab, &entry, &cursor);
561 /* if it matches our principal */
562 if (!krb5_kt_compare(smb_krb5_context->krb5_context, &entry, princ, 0, 0)) {
563 /* Free the entry, it wasn't the one we were looking for anyway */
564 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
568 /* delete it, if it is not kvno -1 */
569 if (entry.vno != (kvno - 1 )) {
570 /* Release the enumeration. We are going to
571 * have to start this from the top again,
572 * because deletes during enumeration may not
573 * always be consistant.
575 * Also, the enumeration locks a FILE: keytab
578 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
580 ret = krb5_kt_remove_entry(smb_krb5_context->krb5_context, keytab, &entry);
581 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
583 /* Deleted: Restart from the top */
584 ret2 = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
586 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
587 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
588 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
591 talloc_free(mem_ctx);
600 *found_previous = True;
603 /* Free the entry, we don't need it any more */
604 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
608 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
618 DEBUG(1,("failed in deleting old entries for principal: %s: %s\n",
620 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
623 talloc_free(mem_ctx);
627 int smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
628 struct cli_credentials *machine_account,
629 struct smb_krb5_context *smb_krb5_context,
630 struct keytab_container *keytab_container)
634 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
639 ret = remove_old_entries(mem_ctx, machine_account,
640 smb_krb5_context, keytab_container->keytab, &found_previous);
642 talloc_free(mem_ctx);
646 /* Create a new keytab. If during the cleanout we found
647 * entires for kvno -1, then don't try and duplicate them.
648 * Otherwise, add kvno, and kvno -1 */
650 ret = create_keytab(mem_ctx, machine_account, smb_krb5_context,
651 keytab_container->keytab,
652 found_previous ? False : True);
653 talloc_free(mem_ctx);
657 int smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
658 struct cli_credentials *machine_account,
659 struct smb_krb5_context *smb_krb5_context,
660 struct keytab_container **keytab_container)
663 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
664 const char *rand_string;
665 const char *keytab_name;
670 *keytab_container = talloc(mem_ctx, struct keytab_container);
672 rand_string = generate_random_str(mem_ctx, 16);
674 talloc_free(mem_ctx);
678 keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s",
681 talloc_free(mem_ctx);
685 ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, keytab_container);
690 ret = smb_krb5_update_keytab(mem_ctx, machine_account, smb_krb5_context, *keytab_container);
692 talloc_steal(parent_ctx, *keytab_container);
694 *keytab_container = NULL;
696 talloc_free(mem_ctx);