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/auth.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 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);
169 ret = kerberos_kinit_password_cc(smb_krb5_context->krb5_context, ccache,
171 password, NULL, &kdc_time);
173 /* No password available, try to use a keyblock instead */
175 krb5_keyblock keyblock;
176 const struct samr_Password *mach_pwd;
177 mach_pwd = cli_credentials_get_nt_hash(credentials, mem_ctx);
179 talloc_free(mem_ctx);
180 DEBUG(1, ("kinit_to_ccache: No password available for kinit\n"));
183 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
184 ETYPE_ARCFOUR_HMAC_MD5,
185 mach_pwd->hash, sizeof(mach_pwd->hash),
189 ret = kerberos_kinit_keyblock_cc(smb_krb5_context->krb5_context, ccache,
191 &keyblock, NULL, &kdc_time);
192 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &keyblock);
196 /* cope with ticket being in the future due to clock skew */
197 if ((unsigned)kdc_time > time(NULL)) {
198 time_t t = time(NULL);
199 int time_offset =(unsigned)kdc_time-t;
200 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
201 krb5_set_real_time(smb_krb5_context->krb5_context, t + time_offset + 1, 0);
204 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
205 DEBUG(1,("kinit for %s failed (%s)\n",
206 cli_credentials_get_principal(credentials, mem_ctx),
207 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
209 talloc_free(mem_ctx);
213 if (ret == KRB5KDC_ERR_PREAUTH_FAILED && cli_credentials_wrong_password(credentials)) {
214 ret = kinit_to_ccache(parent_ctx,
220 DEBUG(1,("kinit for %s failed (%s)\n",
221 cli_credentials_get_principal(credentials, mem_ctx),
222 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
224 talloc_free(mem_ctx);
230 static int free_keytab(struct keytab_container *ktc)
232 krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
237 int smb_krb5_open_keytab(TALLOC_CTX *mem_ctx,
238 struct smb_krb5_context *smb_krb5_context,
239 const char *keytab_name, struct keytab_container **ktc)
243 ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
245 DEBUG(1,("failed to open krb5 keytab: %s\n",
246 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
251 *ktc = talloc(mem_ctx, struct keytab_container);
256 (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
257 (*ktc)->keytab = keytab;
258 talloc_set_destructor(*ktc, free_keytab);
263 struct enctypes_container {
264 struct smb_krb5_context *smb_krb5_context;
265 krb5_enctype *enctypes;
268 static int free_enctypes(struct enctypes_container *etc)
270 free_kerberos_etypes(etc->smb_krb5_context->krb5_context, etc->enctypes);
274 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
275 const char *princ_string,
276 krb5_principal princ,
277 krb5_principal salt_princ,
279 const char *password_s,
280 struct smb_krb5_context *smb_krb5_context,
285 krb5_enctype *enctypes;
286 char *enctype_string;
287 struct enctypes_container *etc;
289 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
294 etc = talloc(mem_ctx, struct enctypes_container);
296 talloc_free(mem_ctx);
299 ret = get_kerberos_allowed_etypes(smb_krb5_context->krb5_context,
302 DEBUG(1,("keytab_add_keys: getting encrption types failed (%s)\n",
303 error_message(ret)));
304 talloc_free(mem_ctx);
308 etc->smb_krb5_context = talloc_reference(etc, smb_krb5_context);
309 etc->enctypes = enctypes;
311 talloc_set_destructor(etc, free_enctypes);
313 password.data = discard_const_p(char *, password_s);
314 password.length = strlen(password_s);
316 for (i=0; enctypes[i]; i++) {
317 krb5_keytab_entry entry;
318 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context,
319 salt_princ, &password, &entry.keyblock, enctypes[i]);
321 talloc_free(mem_ctx);
325 entry.principal = princ;
327 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
328 enctype_string = NULL;
329 krb5_enctype_to_string(smb_krb5_context->krb5_context, enctypes[i], &enctype_string);
331 DEBUG(1, ("Failed to add %s entry for %s(kvno %d) to keytab: %s\n",
335 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
337 talloc_free(mem_ctx);
338 free(enctype_string);
339 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
343 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
346 free(enctype_string);
348 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
350 talloc_free(mem_ctx);
354 static int create_keytab(TALLOC_CTX *parent_ctx,
355 struct cli_credentials *machine_account,
356 struct smb_krb5_context *smb_krb5_context,
361 const char *password_s;
362 char *enctype_string;
363 const char *old_secret;
365 krb5_principal salt_princ;
366 krb5_principal princ;
367 const char *princ_string;
369 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
374 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
375 /* Get the principal we will store the new keytab entries under */
376 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
378 DEBUG(1,("create_keytab: makeing krb5 principal failed (%s)\n",
379 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
381 talloc_free(mem_ctx);
385 /* The salt used to generate these entries may be different however, fetch that */
386 ret = salt_principal_from_credentials(mem_ctx, machine_account,
390 DEBUG(1,("create_keytab: makeing salt principal failed (%s)\n",
391 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
393 talloc_free(mem_ctx);
397 /* Finally, do the dance to get the password to put in the entry */
398 password_s = cli_credentials_get_password(machine_account);
400 /* If we don't have the plaintext password, try for
401 * the MD4 password hash */
403 krb5_keytab_entry entry;
404 const struct samr_Password *mach_pwd;
405 mach_pwd = cli_credentials_get_nt_hash(machine_account, mem_ctx);
407 DEBUG(1, ("create_keytab: Domain trust informaton for account %s not available\n",
408 cli_credentials_get_principal(machine_account, mem_ctx)));
409 talloc_free(mem_ctx);
412 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
413 ETYPE_ARCFOUR_HMAC_MD5,
414 mach_pwd->hash, sizeof(mach_pwd->hash),
417 DEBUG(1, ("create_keytab: krb5_keyblock_init failed: %s\n",
418 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
420 talloc_free(mem_ctx);
424 entry.principal = princ;
425 entry.vno = cli_credentials_get_kvno(machine_account);
426 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
428 DEBUG(1, ("Failed to add ARCFOUR_HMAC (only) entry for %s to keytab: %s",
429 cli_credentials_get_principal(machine_account, mem_ctx),
430 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
432 talloc_free(mem_ctx);
433 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
437 krb5_enctype_to_string(smb_krb5_context->krb5_context,
438 ETYPE_ARCFOUR_HMAC_MD5,
440 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
441 cli_credentials_get_principal(machine_account, mem_ctx),
442 cli_credentials_get_kvno(machine_account),
444 free(enctype_string);
446 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
448 /* Can't go any further, we only have this one key */
449 talloc_free(mem_ctx);
453 kvno = cli_credentials_get_kvno(machine_account);
454 /* good, we actually have the real plaintext */
455 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
456 kvno, password_s, smb_krb5_context, keytab);
458 talloc_free(mem_ctx);
462 if (!add_old || kvno == 0) {
463 talloc_free(mem_ctx);
467 old_secret = cli_credentials_get_old_password(machine_account);
469 talloc_free(mem_ctx);
473 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
474 kvno - 1, old_secret, smb_krb5_context, keytab);
476 talloc_free(mem_ctx);
480 talloc_free(mem_ctx);
486 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
488 * These entries are now stale, we only keep the current, and previous entries around.
490 * Inspired by the code in Samba3 for 'use kerberos keytab'.
494 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
495 struct cli_credentials *machine_account,
496 struct smb_krb5_context *smb_krb5_context,
497 krb5_keytab keytab, BOOL *found_previous)
499 krb5_error_code ret, ret2;
500 krb5_kt_cursor cursor;
501 krb5_principal princ;
503 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
504 const char *princ_string;
509 *found_previous = False;
510 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
512 /* Get the principal we will store the new keytab entries under */
513 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
515 DEBUG(1,("update_keytab: makeing krb5 principal failed (%s)\n",
516 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
518 talloc_free(mem_ctx);
522 kvno = cli_credentials_get_kvno(machine_account);
524 /* for each entry in the keytab */
525 ret = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
529 case HEIM_ERR_OPNOTSUPP:
532 /* no point enumerating if there isn't anything here */
533 talloc_free(mem_ctx);
536 DEBUG(1,("failed to open keytab for read of old entries: %s\n",
537 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
539 talloc_free(mem_ctx);
544 krb5_keytab_entry entry;
545 ret = krb5_kt_next_entry(smb_krb5_context->krb5_context, keytab, &entry, &cursor);
549 /* if it matches our principal */
550 if (!krb5_kt_compare(smb_krb5_context->krb5_context, &entry, princ, 0, 0)) {
551 /* Free the entry, it wasn't the one we were looking for anyway */
552 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
556 /* delete it, if it is not kvno -1 */
557 if (entry.vno != (kvno - 1 )) {
558 /* Release the enumeration. We are going to
559 * have to start this from the top again,
560 * because deletes during enumeration may not
561 * always be consistant.
563 * Also, the enumeration locks a FILE: keytab
566 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
568 ret = krb5_kt_remove_entry(smb_krb5_context->krb5_context, keytab, &entry);
569 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
571 /* Deleted: Restart from the top */
572 ret2 = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
574 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
575 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
576 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
579 talloc_free(mem_ctx);
588 *found_previous = True;
591 /* Free the entry, we don't need it any more */
592 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
596 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
606 DEBUG(1,("failed in deleting old entries for principal: %s: %s\n",
608 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
611 talloc_free(mem_ctx);
615 int smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
616 struct cli_credentials *machine_account,
617 struct smb_krb5_context *smb_krb5_context,
618 struct keytab_container *keytab_container)
622 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
627 ret = remove_old_entries(mem_ctx, machine_account,
628 smb_krb5_context, keytab_container->keytab, &found_previous);
630 talloc_free(mem_ctx);
634 /* Create a new keytab. If during the cleanout we found
635 * entires for kvno -1, then don't try and duplicate them.
636 * Otherwise, add kvno, and kvno -1 */
638 ret = create_keytab(mem_ctx, machine_account, smb_krb5_context,
639 keytab_container->keytab,
640 found_previous ? False : True);
641 talloc_free(mem_ctx);
645 int smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
646 struct cli_credentials *machine_account,
647 struct smb_krb5_context *smb_krb5_context,
648 struct keytab_container **keytab_container)
651 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
652 const char *rand_string;
653 const char *keytab_name;
658 *keytab_container = talloc(mem_ctx, struct keytab_container);
660 rand_string = generate_random_str(mem_ctx, 16);
662 talloc_free(mem_ctx);
666 keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s",
669 talloc_free(mem_ctx);
673 ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, keytab_container);
678 ret = smb_krb5_update_keytab(mem_ctx, machine_account, smb_krb5_context, *keytab_container);
680 talloc_steal(parent_ctx, *keytab_container);
682 *keytab_container = NULL;
684 talloc_free(mem_ctx);