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(void *ptr) {
35 struct principal_container *pc = ptr;
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 ENCTYPE_ARCFOUR_HMAC,
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 DEBUG(1,("kinit for %s failed (%s)\n",
214 cli_credentials_get_principal(credentials, mem_ctx),
215 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
217 talloc_free(mem_ctx);
223 static int free_keytab(void *ptr) {
224 struct keytab_container *ktc = ptr;
225 krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
230 int smb_krb5_open_keytab(TALLOC_CTX *mem_ctx,
231 struct smb_krb5_context *smb_krb5_context,
232 const char *keytab_name, struct keytab_container **ktc)
236 ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
238 DEBUG(1,("failed to open krb5 keytab: %s\n",
239 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
244 *ktc = talloc(mem_ctx, struct keytab_container);
249 (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
250 (*ktc)->keytab = keytab;
251 talloc_set_destructor(*ktc, free_keytab);
256 struct enctypes_container {
257 struct smb_krb5_context *smb_krb5_context;
258 krb5_enctype *enctypes;
261 static int free_enctypes(void *ptr) {
262 struct enctypes_container *etc = ptr;
263 free_kerberos_etypes(etc->smb_krb5_context->krb5_context, etc->enctypes);
267 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
268 const char *princ_string,
269 krb5_principal princ,
270 krb5_principal salt_princ,
272 const char *password_s,
273 struct smb_krb5_context *smb_krb5_context,
278 krb5_enctype *enctypes;
279 char *enctype_string;
280 struct enctypes_container *etc;
282 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
287 etc = talloc(mem_ctx, struct enctypes_container);
289 talloc_free(mem_ctx);
292 ret = get_kerberos_allowed_etypes(smb_krb5_context->krb5_context,
295 DEBUG(1,("keytab_add_keys: getting encrption types failed (%s)\n",
296 error_message(ret)));
297 talloc_free(mem_ctx);
301 etc->smb_krb5_context = talloc_reference(etc, smb_krb5_context);
302 etc->enctypes = enctypes;
304 talloc_set_destructor(etc, free_enctypes);
306 password.data = discard_const_p(char *, password_s);
307 password.length = strlen(password_s);
309 for (i=0; enctypes[i]; i++) {
310 krb5_keytab_entry entry;
311 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context,
312 salt_princ, &password, &entry.keyblock, enctypes[i]);
314 talloc_free(mem_ctx);
318 entry.principal = princ;
320 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
321 enctype_string = NULL;
322 krb5_enctype_to_string(smb_krb5_context->krb5_context, enctypes[i], &enctype_string);
324 DEBUG(1, ("Failed to add %s entry for %s(kvno %d) to keytab: %s\n",
328 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
330 talloc_free(mem_ctx);
331 free(enctype_string);
332 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
336 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
339 free(enctype_string);
341 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
343 talloc_free(mem_ctx);
347 static int create_keytab(TALLOC_CTX *parent_ctx,
348 struct cli_credentials *machine_account,
349 struct smb_krb5_context *smb_krb5_context,
354 const char *password_s;
355 char *enctype_string;
356 const char *old_secret;
358 krb5_principal salt_princ;
359 krb5_principal princ;
360 const char *princ_string;
362 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
367 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
368 /* Get the principal we will store the new keytab entries under */
369 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
371 DEBUG(1,("create_keytab: makeing krb5 principal failed (%s)\n",
372 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
374 talloc_free(mem_ctx);
378 /* The salt used to generate these entries may be different however, fetch that */
379 ret = salt_principal_from_credentials(mem_ctx, machine_account,
383 DEBUG(1,("create_keytab: makeing salt principal failed (%s)\n",
384 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
386 talloc_free(mem_ctx);
390 /* Finally, do the dance to get the password to put in the entry */
391 password_s = cli_credentials_get_password(machine_account);
393 /* If we don't have the plaintext password, try for
394 * the MD4 password hash */
396 krb5_keytab_entry entry;
397 const struct samr_Password *mach_pwd;
398 mach_pwd = cli_credentials_get_nt_hash(machine_account, mem_ctx);
400 DEBUG(1, ("create_keytab: Domain trust informaton for account %s not available\n",
401 cli_credentials_get_principal(machine_account, mem_ctx)));
402 talloc_free(mem_ctx);
405 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
406 ENCTYPE_ARCFOUR_HMAC,
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 krb5_enctype_to_string(smb_krb5_context->krb5_context, ENCTYPE_ARCFOUR_HMAC, &enctype_string);
431 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
432 cli_credentials_get_principal(machine_account, mem_ctx),
433 cli_credentials_get_kvno(machine_account),
435 free(enctype_string);
437 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
439 /* Can't go any further, we only have this one key */
440 talloc_free(mem_ctx);
444 kvno = cli_credentials_get_kvno(machine_account);
445 /* good, we actually have the real plaintext */
446 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
447 kvno, password_s, smb_krb5_context, keytab);
449 talloc_free(mem_ctx);
453 if (!add_old || kvno == 0) {
454 talloc_free(mem_ctx);
458 old_secret = cli_credentials_get_old_password(machine_account);
460 talloc_free(mem_ctx);
464 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
465 kvno - 1, old_secret, smb_krb5_context, keytab);
467 talloc_free(mem_ctx);
471 talloc_free(mem_ctx);
477 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
479 * These entries are now stale, we only keep the current, and previous entries around.
481 * Inspired by the code in Samba3 for 'use kerberos keytab'.
485 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
486 struct cli_credentials *machine_account,
487 struct smb_krb5_context *smb_krb5_context,
488 krb5_keytab keytab, BOOL *found_previous)
490 krb5_error_code ret, ret2;
491 krb5_kt_cursor cursor;
492 krb5_principal princ;
494 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
495 const char *princ_string;
500 *found_previous = False;
501 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
503 /* Get the principal we will store the new keytab entries under */
504 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
506 DEBUG(1,("update_keytab: makeing krb5 principal failed (%s)\n",
507 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
509 talloc_free(mem_ctx);
513 kvno = cli_credentials_get_kvno(machine_account);
515 /* for each entry in the keytab */
516 ret = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
520 case HEIM_ERR_OPNOTSUPP:
523 /* no point enumerating if there isn't anything here */
524 talloc_free(mem_ctx);
527 DEBUG(1,("failed to open keytab for read of old entries: %s\n",
528 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
530 talloc_free(mem_ctx);
535 krb5_keytab_entry entry;
536 ret = krb5_kt_next_entry(smb_krb5_context->krb5_context, keytab, &entry, &cursor);
540 /* if it matches our principal */
541 if (!krb5_kt_compare(smb_krb5_context->krb5_context, &entry, princ, 0, 0)) {
542 /* Free the entry, it wasn't the one we were looking for anyway */
543 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
547 /* delete it, if it is not kvno -1 */
548 if (entry.vno != (kvno - 1 )) {
549 /* Release the enumeration. We are going to
550 * have to start this from the top again,
551 * because deletes during enumeration may not
552 * always be consistant.
554 * Also, the enumeration locks a FILE: keytab
557 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
559 ret = krb5_kt_remove_entry(smb_krb5_context->krb5_context, keytab, &entry);
560 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
562 /* Deleted: Restart from the top */
563 ret2 = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
565 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
566 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
567 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
570 talloc_free(mem_ctx);
579 *found_previous = True;
582 /* Free the entry, we don't need it any more */
583 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
587 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
597 DEBUG(1,("failed in deleting old entries for principal: %s: %s\n",
599 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
602 talloc_free(mem_ctx);
606 int smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
607 struct cli_credentials *machine_account,
608 struct smb_krb5_context *smb_krb5_context,
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,
630 keytab_container->keytab,
631 found_previous ? False : True);
632 talloc_free(mem_ctx);
636 int smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
637 struct cli_credentials *machine_account,
638 struct smb_krb5_context *smb_krb5_context,
639 struct keytab_container **keytab_container)
642 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
643 const char *rand_string;
644 const char *keytab_name;
649 *keytab_container = talloc(mem_ctx, struct keytab_container);
651 rand_string = generate_random_str(mem_ctx, 16);
653 talloc_free(mem_ctx);
657 keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s",
660 talloc_free(mem_ctx);
664 ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, keytab_container);
669 ret = smb_krb5_update_keytab(mem_ctx, machine_account, smb_krb5_context, *keytab_container);
671 talloc_steal(parent_ctx, *keytab_container);
673 *keytab_container = NULL;
675 talloc_free(mem_ctx);