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 "system/time.h"
27 #include "system/network.h"
28 #include "auth/kerberos/kerberos.h"
29 #include "auth/auth.h"
31 struct principal_container {
32 struct smb_krb5_context *smb_krb5_context;
33 krb5_principal principal;
36 static int free_principal(void *ptr) {
37 struct principal_container *pc = ptr;
38 /* current heimdal - 0.6.3, which we need anyway, fixes segfaults here */
39 krb5_free_principal(pc->smb_krb5_context->krb5_context, pc->principal);
44 krb5_error_code salt_principal_from_credentials(TALLOC_CTX *parent_ctx,
45 struct cli_credentials *machine_account,
46 struct smb_krb5_context *smb_krb5_context,
47 krb5_principal *salt_princ)
50 char *machine_username;
53 const char *salt_principal;
54 struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
59 salt_principal = cli_credentials_get_salt_principal(machine_account);
61 ret = krb5_parse_name(smb_krb5_context->krb5_context, salt_principal, salt_princ);
63 machine_username = talloc_strdup(mem_ctx, cli_credentials_get_username(machine_account));
65 if (!machine_username) {
70 if (machine_username[strlen(machine_username)-1] == '$') {
71 machine_username[strlen(machine_username)-1] = '\0';
73 lower_realm = strlower_talloc(mem_ctx, cli_credentials_get_realm(machine_account));
79 salt_body = talloc_asprintf(mem_ctx, "%s.%s", machine_username,
86 ret = krb5_make_principal(smb_krb5_context->krb5_context, salt_princ,
87 cli_credentials_get_realm(machine_account),
88 "host", salt_body, NULL);
92 /* This song-and-dance effectivly puts the principal
93 * into talloc, so we can't loose it. */
94 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
95 mem_ctx->principal = *salt_princ;
96 talloc_set_destructor(mem_ctx, free_principal);
101 /* Obtain the principal set on this context. Requires a
102 * smb_krb5_context because we are doing krb5 principal parsing with
103 * the library routines. The returned princ is placed in the talloc
104 * system by means of a destructor (do *not* free). */
106 krb5_error_code principal_from_credentials(TALLOC_CTX *parent_ctx,
107 struct cli_credentials *credentials,
108 struct smb_krb5_context *smb_krb5_context,
109 krb5_principal *princ)
112 const char *princ_string;
113 struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
118 princ_string = cli_credentials_get_principal(credentials, mem_ctx);
120 /* A NULL here has meaning, as the gssapi server case will
121 * then use the principal from the client */
123 talloc_free(mem_ctx);
128 ret = krb5_parse_name(smb_krb5_context->krb5_context,
129 princ_string, princ);
132 /* This song-and-dance effectivly puts the principal
133 * into talloc, so we can't loose it. */
134 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
135 mem_ctx->principal = *princ;
136 talloc_set_destructor(mem_ctx, free_principal);
142 * Return a freshly allocated ccache (destroyed by destructor on child
143 * of parent_ctx), for a given set of client credentials
146 krb5_error_code kinit_to_ccache(TALLOC_CTX *parent_ctx,
147 struct cli_credentials *credentials,
148 struct smb_krb5_context *smb_krb5_context,
152 const char *password;
154 krb5_principal princ;
156 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
162 ret = principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &princ);
164 talloc_free(mem_ctx);
168 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 ENCTYPE_ARCFOUR_HMAC,
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 /* cope with ticket being in the future due to clock skew */
199 if ((unsigned)kdc_time > time(NULL)) {
200 time_t t = time(NULL);
201 int time_offset =(unsigned)kdc_time-t;
202 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
203 krb5_set_real_time(smb_krb5_context->krb5_context, t + time_offset + 1, 0);
206 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
207 DEBUG(1,("kinit for %s failed (%s)\n",
208 cli_credentials_get_principal(credentials, mem_ctx),
209 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
211 talloc_free(mem_ctx);
215 DEBUG(1,("kinit for %s failed (%s)\n",
216 cli_credentials_get_principal(credentials, mem_ctx),
217 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
219 talloc_free(mem_ctx);
225 static int free_keytab(void *ptr) {
226 struct keytab_container *ktc = ptr;
227 krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
232 int smb_krb5_open_keytab(TALLOC_CTX *mem_ctx,
233 struct smb_krb5_context *smb_krb5_context,
234 const char *keytab_name, struct keytab_container **ktc)
238 ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
240 DEBUG(1,("failed to open krb5 keytab: %s\n",
241 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
246 *ktc = talloc(mem_ctx, struct keytab_container);
251 (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
252 (*ktc)->keytab = keytab;
253 talloc_set_destructor(*ktc, free_keytab);
258 struct enctypes_container {
259 struct smb_krb5_context *smb_krb5_context;
260 krb5_enctype *enctypes;
263 static int free_enctypes(void *ptr) {
264 struct enctypes_container *etc = ptr;
265 free_kerberos_etypes(etc->smb_krb5_context->krb5_context, etc->enctypes);
269 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
270 const char *princ_string,
271 krb5_principal princ,
272 krb5_principal salt_princ,
274 const char *password_s,
275 struct smb_krb5_context *smb_krb5_context,
280 krb5_enctype *enctypes;
281 char *enctype_string;
282 struct enctypes_container *etc;
284 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
289 etc = talloc(mem_ctx, struct enctypes_container);
291 talloc_free(mem_ctx);
294 ret = get_kerberos_allowed_etypes(smb_krb5_context->krb5_context,
297 DEBUG(1,("keytab_add_keys: getting encrption types failed (%s)\n",
298 error_message(ret)));
299 talloc_free(mem_ctx);
303 etc->smb_krb5_context = talloc_reference(etc, smb_krb5_context);
304 etc->enctypes = enctypes;
306 talloc_set_destructor(etc, free_enctypes);
308 password.data = discard_const_p(char *, password_s);
309 password.length = strlen(password_s);
311 for (i=0; enctypes[i]; i++) {
312 krb5_keytab_entry entry;
313 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context,
314 salt_princ, &password, &entry.keyblock, enctypes[i]);
316 talloc_free(mem_ctx);
320 entry.principal = princ;
322 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
323 enctype_string = NULL;
324 krb5_enctype_to_string(smb_krb5_context->krb5_context, enctypes[i], &enctype_string);
326 DEBUG(1, ("Failed to add %s entry for %s(kvno %d) to keytab: %s\n",
330 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
332 talloc_free(mem_ctx);
333 free(enctype_string);
334 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
338 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
341 free(enctype_string);
343 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
345 talloc_free(mem_ctx);
349 static int create_keytab(TALLOC_CTX *parent_ctx,
350 struct cli_credentials *machine_account,
351 struct smb_krb5_context *smb_krb5_context,
356 const char *password_s;
357 char *enctype_string;
358 const char *old_secret;
360 krb5_principal salt_princ;
361 krb5_principal princ;
362 const char *princ_string;
364 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
369 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
370 /* Get the principal we will store the new keytab entries under */
371 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
373 DEBUG(1,("create_keytab: makeing krb5 principal failed (%s)\n",
374 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
376 talloc_free(mem_ctx);
380 /* The salt used to generate these entries may be different however, fetch that */
381 ret = salt_principal_from_credentials(mem_ctx, machine_account,
385 DEBUG(1,("create_keytab: makeing salt principal failed (%s)\n",
386 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
388 talloc_free(mem_ctx);
392 /* Finally, do the dance to get the password to put in the entry */
393 password_s = cli_credentials_get_password(machine_account);
395 /* If we don't have the plaintext password, try for
396 * the MD4 password hash */
398 krb5_keytab_entry entry;
399 const struct samr_Password *mach_pwd;
400 mach_pwd = cli_credentials_get_nt_hash(machine_account, mem_ctx);
402 talloc_free(mem_ctx);
403 DEBUG(1, ("create_keytab: Domain trust informaton for account %s not available\n",
404 cli_credentials_get_principal(machine_account, mem_ctx)));
407 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
408 ENCTYPE_ARCFOUR_HMAC,
409 mach_pwd->hash, sizeof(mach_pwd->hash),
412 DEBUG(1, ("create_keytab: krb5_keyblock_init failed: %s\n",
413 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
418 entry.principal = princ;
419 entry.vno = cli_credentials_get_kvno(machine_account);
420 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
422 DEBUG(1, ("Failed to add ARCFOUR_HMAC (only) entry for %s to keytab: %s",
423 cli_credentials_get_principal(machine_account, mem_ctx),
424 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
426 talloc_free(mem_ctx);
427 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
431 krb5_enctype_to_string(smb_krb5_context->krb5_context, ENCTYPE_ARCFOUR_HMAC, &enctype_string);
432 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
433 cli_credentials_get_principal(machine_account, mem_ctx),
434 cli_credentials_get_kvno(machine_account),
436 free(enctype_string);
438 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
440 /* Can't go any further, we only have this one key */
441 talloc_free(mem_ctx);
445 kvno = cli_credentials_get_kvno(machine_account);
446 /* good, we actually have the real plaintext */
447 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
448 kvno, password_s, smb_krb5_context, keytab);
450 talloc_free(mem_ctx);
454 if (!add_old || kvno == 0) {
455 talloc_free(mem_ctx);
459 old_secret = cli_credentials_get_old_password(machine_account);
461 talloc_free(mem_ctx);
465 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
466 kvno - 1, old_secret, smb_krb5_context, keytab);
468 talloc_free(mem_ctx);
472 talloc_free(mem_ctx);
478 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
480 * These entries are now stale, we only keep the current, and previous entries around.
482 * Inspired by the code in Samba3 for 'use kerberos keytab'.
486 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
487 struct cli_credentials *machine_account,
488 struct smb_krb5_context *smb_krb5_context,
489 krb5_keytab keytab, BOOL *found_previous)
491 krb5_error_code ret, ret2;
492 krb5_kt_cursor cursor;
493 krb5_principal princ;
495 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
496 const char *princ_string;
501 *found_previous = False;
502 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
504 /* Get the principal we will store the new keytab entries under */
505 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
507 DEBUG(1,("update_keytab: makeing krb5 principal failed (%s)\n",
508 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
510 talloc_free(mem_ctx);
514 kvno = cli_credentials_get_kvno(machine_account);
516 /* for each entry in the keytab */
517 ret = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
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);