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 struct enctypes_container {
230 struct smb_krb5_context *smb_krb5_context;
231 krb5_enctype *enctypes;
234 static int free_enctypes(void *ptr) {
235 struct enctypes_container *etc = ptr;
236 free_kerberos_etypes(etc->smb_krb5_context->krb5_context, etc->enctypes);
240 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
241 const char *princ_string,
242 krb5_principal princ,
243 krb5_principal salt_princ,
245 const char *password_s,
246 struct smb_krb5_context *smb_krb5_context,
251 krb5_enctype *enctypes;
252 char *enctype_string;
253 struct enctypes_container *etc;
255 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
260 etc = talloc(mem_ctx, struct enctypes_container);
262 talloc_free(mem_ctx);
265 ret = get_kerberos_allowed_etypes(smb_krb5_context->krb5_context,
268 DEBUG(1,("keytab_add_keys: getting encrption types failed (%s)\n",
269 error_message(ret)));
270 talloc_free(mem_ctx);
274 etc->smb_krb5_context = talloc_reference(etc, smb_krb5_context);
275 etc->enctypes = enctypes;
277 talloc_set_destructor(etc, free_enctypes);
279 password.data = discard_const_p(char *, password_s);
280 password.length = strlen(password_s);
282 for (i=0; enctypes[i]; i++) {
283 krb5_keytab_entry entry;
284 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context,
285 salt_princ, &password, &entry.keyblock, enctypes[i]);
287 talloc_free(mem_ctx);
291 entry.principal = princ;
293 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
294 enctype_string = NULL;
295 krb5_enctype_to_string(smb_krb5_context->krb5_context, enctypes[i], &enctype_string);
297 DEBUG(1, ("Failed to add %s entry for %s(kvno %d) to keytab: %s\n",
301 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
303 talloc_free(mem_ctx);
304 free(enctype_string);
305 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
309 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
312 free(enctype_string);
314 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
316 talloc_free(mem_ctx);
320 static int create_keytab(TALLOC_CTX *parent_ctx,
321 struct cli_credentials *machine_account,
322 struct smb_krb5_context *smb_krb5_context,
327 const char *password_s;
328 char *enctype_string;
329 const char *old_secret;
331 krb5_principal salt_princ;
332 krb5_principal princ;
333 const char *princ_string;
335 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
340 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
341 /* Get the principal we will store the new keytab entries under */
342 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
344 DEBUG(1,("create_keytab: makeing krb5 principal failed (%s)\n",
345 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
347 talloc_free(mem_ctx);
351 /* The salt used to generate these entries may be different however, fetch that */
352 ret = salt_principal_from_credentials(mem_ctx, machine_account,
356 DEBUG(1,("create_keytab: makeing salt principal failed (%s)\n",
357 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
359 talloc_free(mem_ctx);
363 /* Finally, do the dance to get the password to put in the entry */
364 password_s = cli_credentials_get_password(machine_account);
366 /* If we don't have the plaintext password, try for
367 * the MD4 password hash */
369 krb5_keytab_entry entry;
370 const struct samr_Password *mach_pwd;
371 mach_pwd = cli_credentials_get_nt_hash(machine_account, mem_ctx);
373 talloc_free(mem_ctx);
374 DEBUG(1, ("create_keytab: Domain trust informaton for account %s not available\n",
375 cli_credentials_get_principal(machine_account, mem_ctx)));
378 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
379 ENCTYPE_ARCFOUR_HMAC,
380 mach_pwd->hash, sizeof(mach_pwd->hash),
383 DEBUG(1, ("create_keytab: krb5_keyblock_init failed: %s\n",
384 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
389 entry.principal = princ;
390 entry.vno = cli_credentials_get_kvno(machine_account);
391 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
393 DEBUG(1, ("Failed to add ARCFOUR_HMAC (only) entry for %s to keytab: %s",
394 cli_credentials_get_principal(machine_account, mem_ctx),
395 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
397 talloc_free(mem_ctx);
398 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
402 krb5_enctype_to_string(smb_krb5_context->krb5_context, ENCTYPE_ARCFOUR_HMAC, &enctype_string);
403 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
404 cli_credentials_get_principal(machine_account, mem_ctx),
405 cli_credentials_get_kvno(machine_account),
407 free(enctype_string);
409 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
411 /* Can't go any further, we only have this one key */
412 talloc_free(mem_ctx);
416 kvno = cli_credentials_get_kvno(machine_account);
417 /* good, we actually have the real plaintext */
418 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
419 kvno, password_s, smb_krb5_context, keytab);
421 talloc_free(mem_ctx);
425 if (!add_old || kvno == 0) {
426 talloc_free(mem_ctx);
430 old_secret = cli_credentials_get_old_password(machine_account);
432 talloc_free(mem_ctx);
436 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
437 kvno - 1, old_secret, smb_krb5_context, keytab);
439 talloc_free(mem_ctx);
443 talloc_free(mem_ctx);
449 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
451 * These entries are now stale, we only keep the current, and previous entries around.
453 * Inspired by the code in Samba3 for 'use kerberos keytab'.
457 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
458 struct cli_credentials *machine_account,
459 struct smb_krb5_context *smb_krb5_context,
460 krb5_keytab keytab, BOOL *found_previous)
462 krb5_error_code ret, ret2;
463 krb5_kt_cursor cursor;
464 krb5_principal princ;
466 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
467 const char *princ_string;
472 *found_previous = False;
473 princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
475 /* Get the principal we will store the new keytab entries under */
476 ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
478 DEBUG(1,("update_keytab: makeing krb5 principal failed (%s)\n",
479 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
481 talloc_free(mem_ctx);
485 kvno = cli_credentials_get_kvno(machine_account);
487 /* for each entry in the keytab */
488 ret = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
494 /* no point enumerating if there isn't anything here */
495 talloc_free(mem_ctx);
498 DEBUG(1,("failed to open keytab for read of old entries: %s\n",
499 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
501 talloc_free(mem_ctx);
506 krb5_keytab_entry entry;
507 ret = krb5_kt_next_entry(smb_krb5_context->krb5_context, keytab, &entry, &cursor);
511 /* if it matches our principal */
512 if (!krb5_kt_compare(smb_krb5_context->krb5_context, &entry, princ, 0, 0)) {
513 /* Free the entry, it wasn't the one we were looking for anyway */
514 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
518 /* delete it, if it is not kvno -1 */
519 if (entry.vno != (kvno - 1 )) {
520 /* Release the enumeration. We are going to
521 * have to start this from the top again,
522 * because deletes during enumeration may not
523 * always be consistant.
525 * Also, the enumeration locks the keytab
528 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
530 ret = krb5_kt_remove_entry(smb_krb5_context->krb5_context, keytab, &entry);
531 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
533 /* Deleted: Restart from the top */
534 ret2 = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
536 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
537 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
538 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
541 talloc_free(mem_ctx);
550 *found_previous = True;
553 /* Free the entry, we don't need it any more */
554 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
558 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
568 DEBUG(1,("failed in deleting old entries for principal: %s: %s\n",
570 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
573 talloc_free(mem_ctx);
577 int update_keytab(TALLOC_CTX *parent_ctx,
578 struct cli_credentials *machine_account,
579 struct smb_krb5_context *smb_krb5_context,
580 struct keytab_container *keytab_container)
584 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
589 ret = remove_old_entries(mem_ctx, machine_account,
590 smb_krb5_context, keytab_container->keytab, &found_previous);
592 talloc_free(mem_ctx);
596 /* Create a new keytab. If during the cleanout we found
597 * entires for kvno -1, then don't try and duplicate them.
598 * Otherwise, add kvno, and kvno -1 */
600 ret = create_keytab(mem_ctx, machine_account, smb_krb5_context,
601 keytab_container->keytab,
602 found_previous ? False : True);
603 talloc_free(mem_ctx);
607 int create_memory_keytab(TALLOC_CTX *parent_ctx,
608 struct cli_credentials *machine_account,
609 struct smb_krb5_context *smb_krb5_context,
610 struct keytab_container **keytab_container)
613 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
614 const char *keytab_name = "MEMORY:";
620 *keytab_container = talloc(mem_ctx, struct keytab_container);
622 /* Find the keytab */
623 ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
625 DEBUG(1,("failed to resolve keytab: %s: %s\n",
627 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
629 talloc_free(mem_ctx);
633 (*keytab_container)->smb_krb5_context = talloc_reference(*keytab_container, smb_krb5_context);
634 (*keytab_container)->keytab = keytab;
636 talloc_set_destructor(*keytab_container, free_keytab);
638 ret = update_keytab(mem_ctx, machine_account, smb_krb5_context, *keytab_container);
640 talloc_steal(parent_ctx, *keytab_container);
642 *keytab_container = NULL;
644 talloc_free(mem_ctx);