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 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 then use the principal from the client */
120 talloc_free(mem_ctx);
125 ret = krb5_parse_name(smb_krb5_context->krb5_context,
126 princ_string, princ);
129 /* This song-and-dance effectivly puts the principal
130 * into talloc, so we can't loose it. */
131 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
132 mem_ctx->principal = *princ;
133 talloc_set_destructor(mem_ctx, free_principal);
139 * Return a freshly allocated ccache (destroyed by destructor on child
140 * of parent_ctx), for a given set of client credentials
143 krb5_error_code kinit_to_ccache(TALLOC_CTX *parent_ctx,
144 struct cli_credentials *credentials,
145 struct smb_krb5_context *smb_krb5_context,
149 const char *password;
151 krb5_principal princ;
153 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
159 ret = principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &princ);
161 talloc_free(mem_ctx);
165 password = cli_credentials_get_password(credentials);
168 ret = kerberos_kinit_password_cc(smb_krb5_context->krb5_context, ccache,
170 password, NULL, &kdc_time);
172 /* No password available, try to use a keyblock instead */
174 krb5_keyblock keyblock;
175 const struct samr_Password *mach_pwd;
176 mach_pwd = cli_credentials_get_nt_hash(credentials, mem_ctx);
178 talloc_free(mem_ctx);
179 DEBUG(1, ("kinit_to_ccache: No password available for kinit\n"));
182 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
183 ENCTYPE_ARCFOUR_HMAC,
184 mach_pwd->hash, sizeof(mach_pwd->hash),
188 ret = kerberos_kinit_keyblock_cc(smb_krb5_context->krb5_context, ccache,
190 &keyblock, NULL, &kdc_time);
191 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &keyblock);
195 /* cope with ticket being in the future due to clock skew */
196 if ((unsigned)kdc_time > time(NULL)) {
197 time_t t = time(NULL);
198 int time_offset =(unsigned)kdc_time-t;
199 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
200 krb5_set_real_time(smb_krb5_context->krb5_context, t + time_offset + 1, 0);
203 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
204 DEBUG(1,("kinit for %s failed (%s)\n",
205 cli_credentials_get_principal(credentials, mem_ctx),
206 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
208 talloc_free(mem_ctx);
212 DEBUG(1,("kinit for %s failed (%s)\n",
213 cli_credentials_get_principal(credentials, mem_ctx),
214 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
216 talloc_free(mem_ctx);
222 static int free_keytab(void *ptr) {
223 struct keytab_container *ktc = ptr;
224 krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
229 int smb_krb5_open_keytab(TALLOC_CTX *mem_ctx,
230 struct smb_krb5_context *smb_krb5_context,
231 const char *keytab_name, struct keytab_container **ktc)
235 ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
237 DEBUG(1,("failed to open krb5 keytab: %s\n",
238 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
243 *ktc = talloc(mem_ctx, struct keytab_container);
248 (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
249 (*ktc)->keytab = keytab;
250 talloc_set_destructor(*ktc, free_keytab);
255 struct enctypes_container {
256 struct smb_krb5_context *smb_krb5_context;
257 krb5_enctype *enctypes;
260 static int free_enctypes(void *ptr) {
261 struct enctypes_container *etc = ptr;
262 free_kerberos_etypes(etc->smb_krb5_context->krb5_context, etc->enctypes);
266 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
267 const char *princ_string,
268 krb5_principal princ,
269 krb5_principal salt_princ,
271 const char *password_s,
272 struct smb_krb5_context *smb_krb5_context,
277 krb5_enctype *enctypes;
278 char *enctype_string;
279 struct enctypes_container *etc;
281 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
286 etc = talloc(mem_ctx, struct enctypes_container);
288 talloc_free(mem_ctx);
291 ret = get_kerberos_allowed_etypes(smb_krb5_context->krb5_context,
294 DEBUG(1,("keytab_add_keys: getting encrption types failed (%s)\n",
295 error_message(ret)));
296 talloc_free(mem_ctx);
300 etc->smb_krb5_context = talloc_reference(etc, smb_krb5_context);
301 etc->enctypes = enctypes;
303 talloc_set_destructor(etc, free_enctypes);
305 password.data = discard_const_p(char *, password_s);
306 password.length = strlen(password_s);
308 for (i=0; enctypes[i]; i++) {
309 krb5_keytab_entry entry;
310 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context,
311 salt_princ, &password, &entry.keyblock, enctypes[i]);
313 talloc_free(mem_ctx);
317 entry.principal = princ;
319 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
320 enctype_string = NULL;
321 krb5_enctype_to_string(smb_krb5_context->krb5_context, enctypes[i], &enctype_string);
323 DEBUG(1, ("Failed to add %s entry for %s(kvno %d) to keytab: %s\n",
327 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
329 talloc_free(mem_ctx);
330 free(enctype_string);
331 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
335 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
338 free(enctype_string);
340 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
342 talloc_free(mem_ctx);
346 static int create_keytab(TALLOC_CTX *parent_ctx,
347 struct cli_credentials *machine_account,
348 struct smb_krb5_context *smb_krb5_context,
353 const char *password_s;
354 char *enctype_string;
355 const char *old_secret;
357 krb5_principal salt_princ;
358 krb5_principal princ;
359 const char *princ_string;
361 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
366 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
367 /* Get the principal we will store the new keytab entries under */
368 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
370 DEBUG(1,("create_keytab: makeing krb5 principal failed (%s)\n",
371 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
373 talloc_free(mem_ctx);
377 /* The salt used to generate these entries may be different however, fetch that */
378 ret = salt_principal_from_credentials(mem_ctx, machine_account,
382 DEBUG(1,("create_keytab: makeing salt principal failed (%s)\n",
383 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
385 talloc_free(mem_ctx);
389 /* Finally, do the dance to get the password to put in the entry */
390 password_s = cli_credentials_get_password(machine_account);
392 /* If we don't have the plaintext password, try for
393 * the MD4 password hash */
395 krb5_keytab_entry entry;
396 const struct samr_Password *mach_pwd;
397 mach_pwd = cli_credentials_get_nt_hash(machine_account, mem_ctx);
399 talloc_free(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)));
404 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
405 ENCTYPE_ARCFOUR_HMAC,
406 mach_pwd->hash, sizeof(mach_pwd->hash),
409 DEBUG(1, ("create_keytab: krb5_keyblock_init failed: %s\n",
410 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
415 entry.principal = princ;
416 entry.vno = cli_credentials_get_kvno(machine_account);
417 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
419 DEBUG(1, ("Failed to add ARCFOUR_HMAC (only) entry for %s to keytab: %s",
420 cli_credentials_get_principal(machine_account, mem_ctx),
421 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
423 talloc_free(mem_ctx);
424 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
428 krb5_enctype_to_string(smb_krb5_context->krb5_context, ENCTYPE_ARCFOUR_HMAC, &enctype_string);
429 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
430 cli_credentials_get_principal(machine_account, mem_ctx),
431 cli_credentials_get_kvno(machine_account),
433 free(enctype_string);
435 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
437 /* Can't go any further, we only have this one key */
438 talloc_free(mem_ctx);
442 kvno = cli_credentials_get_kvno(machine_account);
443 /* good, we actually have the real plaintext */
444 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
445 kvno, password_s, smb_krb5_context, 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, keytab);
465 talloc_free(mem_ctx);
469 talloc_free(mem_ctx);
475 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
477 * These entries are now stale, we only keep the current, and previous entries around.
479 * Inspired by the code in Samba3 for 'use kerberos keytab'.
483 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
484 struct cli_credentials *machine_account,
485 struct smb_krb5_context *smb_krb5_context,
486 krb5_keytab keytab, BOOL *found_previous)
488 krb5_error_code ret, ret2;
489 krb5_kt_cursor cursor;
490 krb5_principal princ;
492 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
493 const char *princ_string;
498 *found_previous = False;
499 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
501 /* Get the principal we will store the new keytab entries under */
502 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
504 DEBUG(1,("update_keytab: makeing krb5 principal failed (%s)\n",
505 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
507 talloc_free(mem_ctx);
511 kvno = cli_credentials_get_kvno(machine_account);
513 /* for each entry in the keytab */
514 ret = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
520 /* no point enumerating if there isn't anything here */
521 talloc_free(mem_ctx);
524 DEBUG(1,("failed to open keytab for read of old entries: %s\n",
525 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
527 talloc_free(mem_ctx);
532 krb5_keytab_entry entry;
533 ret = krb5_kt_next_entry(smb_krb5_context->krb5_context, keytab, &entry, &cursor);
537 /* if it matches our principal */
538 if (!krb5_kt_compare(smb_krb5_context->krb5_context, &entry, princ, 0, 0)) {
539 /* Free the entry, it wasn't the one we were looking for anyway */
540 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
544 /* delete it, if it is not kvno -1 */
545 if (entry.vno != (kvno - 1 )) {
546 /* Release the enumeration. We are going to
547 * have to start this from the top again,
548 * because deletes during enumeration may not
549 * always be consistant.
551 * Also, the enumeration locks the keytab
554 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
556 ret = krb5_kt_remove_entry(smb_krb5_context->krb5_context, keytab, &entry);
557 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
559 /* Deleted: Restart from the top */
560 ret2 = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
562 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
563 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
564 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
567 talloc_free(mem_ctx);
576 *found_previous = True;
579 /* Free the entry, we don't need it any more */
580 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
584 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
594 DEBUG(1,("failed in deleting old entries for principal: %s: %s\n",
596 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
599 talloc_free(mem_ctx);
603 int smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
604 struct cli_credentials *machine_account,
605 struct smb_krb5_context *smb_krb5_context,
606 struct keytab_container *keytab_container)
610 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
615 ret = remove_old_entries(mem_ctx, machine_account,
616 smb_krb5_context, keytab_container->keytab, &found_previous);
618 talloc_free(mem_ctx);
622 /* Create a new keytab. If during the cleanout we found
623 * entires for kvno -1, then don't try and duplicate them.
624 * Otherwise, add kvno, and kvno -1 */
626 ret = create_keytab(mem_ctx, machine_account, smb_krb5_context,
627 keytab_container->keytab,
628 found_previous ? False : True);
629 talloc_free(mem_ctx);
633 int smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
634 struct cli_credentials *machine_account,
635 struct smb_krb5_context *smb_krb5_context,
636 struct keytab_container **keytab_container)
639 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
640 const char *rand_string;
641 const char *keytab_name;
646 *keytab_container = talloc(mem_ctx, struct keytab_container);
648 rand_string = generate_random_str(mem_ctx, 16);
650 talloc_free(mem_ctx);
654 keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s",
657 talloc_free(mem_ctx);
661 ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, keytab_container);
666 ret = smb_krb5_update_keytab(mem_ctx, machine_account, smb_krb5_context, *keytab_container);
668 talloc_steal(parent_ctx, *keytab_container);
670 *keytab_container = NULL;
672 talloc_free(mem_ctx);