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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "system/kerberos.h"
25 #include "auth/kerberos/kerberos.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/credentials/credentials_proto.h"
28 #include "auth/credentials/credentials_krb5.h"
29 #include "auth/kerberos/kerberos_credentials.h"
31 struct principal_container
{
32 struct smb_krb5_context
*smb_krb5_context
;
33 krb5_principal principal
;
36 static krb5_error_code
free_principal(struct principal_container
*pc
)
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
);
45 static krb5_error_code
parse_principal(TALLOC_CTX
*parent_ctx
,
46 const char *princ_string
,
47 struct smb_krb5_context
*smb_krb5_context
,
48 krb5_principal
*princ
,
49 const char **error_string
)
52 struct principal_container
*mem_ctx
;
53 if (princ_string
== NULL
) {
58 ret
= krb5_parse_name(smb_krb5_context
->krb5_context
,
62 (*error_string
) = smb_get_krb5_error_message(smb_krb5_context
->krb5_context
, ret
, parent_ctx
);
66 mem_ctx
= talloc(parent_ctx
, struct principal_container
);
68 (*error_string
) = error_message(ENOMEM
);
72 /* This song-and-dance effectivly puts the principal
73 * into talloc, so we can't loose it. */
74 mem_ctx
->smb_krb5_context
= talloc_reference(mem_ctx
, smb_krb5_context
);
75 mem_ctx
->principal
= *princ
;
76 talloc_set_destructor(mem_ctx
, free_principal
);
80 static krb5_error_code
salt_principal_from_credentials(TALLOC_CTX
*parent_ctx
,
81 struct cli_credentials
*machine_account
,
82 struct smb_krb5_context
*smb_krb5_context
,
83 krb5_principal
*salt_princ
)
86 char *machine_username
;
89 const char *salt_principal
;
90 const char *error_string
;
91 struct principal_container
*mem_ctx
= talloc(parent_ctx
, struct principal_container
);
96 salt_principal
= cli_credentials_get_salt_principal(machine_account
);
98 ret
= parse_principal(parent_ctx
, salt_principal
, smb_krb5_context
, salt_princ
, &error_string
);
100 machine_username
= talloc_strdup(mem_ctx
, cli_credentials_get_username(machine_account
));
102 if (!machine_username
) {
103 talloc_free(mem_ctx
);
107 if (machine_username
[strlen(machine_username
)-1] == '$') {
108 machine_username
[strlen(machine_username
)-1] = '\0';
110 lower_realm
= strlower_talloc(mem_ctx
, cli_credentials_get_realm(machine_account
));
112 talloc_free(mem_ctx
);
116 salt_body
= talloc_asprintf(mem_ctx
, "%s.%s", machine_username
,
119 talloc_free(mem_ctx
);
123 ret
= krb5_make_principal(smb_krb5_context
->krb5_context
, salt_princ
,
124 cli_credentials_get_realm(machine_account
),
125 "host", salt_body
, NULL
);
127 /* This song-and-dance effectivly puts the principal
128 * into talloc, so we can't loose it. */
129 mem_ctx
->smb_krb5_context
= talloc_reference(mem_ctx
, smb_krb5_context
);
130 mem_ctx
->principal
= *salt_princ
;
131 talloc_set_destructor(mem_ctx
, free_principal
);
138 /* Obtain the principal set on this context. Requires a
139 * smb_krb5_context because we are doing krb5 principal parsing with
140 * the library routines. The returned princ is placed in the talloc
141 * system by means of a destructor (do *not* free). */
143 krb5_error_code
principal_from_credentials(TALLOC_CTX
*parent_ctx
,
144 struct cli_credentials
*credentials
,
145 struct smb_krb5_context
*smb_krb5_context
,
146 krb5_principal
*princ
,
147 enum credentials_obtained
*obtained
,
148 const char **error_string
)
151 const char *princ_string
;
152 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
154 (*error_string
) = error_message(ENOMEM
);
157 princ_string
= cli_credentials_get_principal_and_obtained(credentials
, mem_ctx
, obtained
);
159 (*error_string
) = error_message(ENOMEM
);
163 ret
= parse_principal(parent_ctx
, princ_string
,
164 smb_krb5_context
, princ
, error_string
);
165 talloc_free(mem_ctx
);
169 /* Obtain the principal set on this context. Requires a
170 * smb_krb5_context because we are doing krb5 principal parsing with
171 * the library routines. The returned princ is placed in the talloc
172 * system by means of a destructor (do *not* free). */
174 krb5_error_code
impersonate_principal_from_credentials(TALLOC_CTX
*parent_ctx
,
175 struct cli_credentials
*credentials
,
176 struct smb_krb5_context
*smb_krb5_context
,
177 krb5_principal
*princ
,
178 const char **error_string
)
180 return parse_principal(parent_ctx
, cli_credentials_get_impersonate_principal(credentials
),
181 smb_krb5_context
, princ
, error_string
);
185 * Return a freshly allocated ccache (destroyed by destructor on child
186 * of parent_ctx), for a given set of client credentials
189 krb5_error_code
kinit_to_ccache(TALLOC_CTX
*parent_ctx
,
190 struct cli_credentials
*credentials
,
191 struct smb_krb5_context
*smb_krb5_context
,
193 enum credentials_obtained
*obtained
,
194 const char **error_string
)
197 const char *password
, *target_service
;
199 krb5_principal princ
;
200 krb5_principal impersonate_principal
;
202 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
205 (*error_string
) = strerror(ENOMEM
);
209 ret
= principal_from_credentials(mem_ctx
, credentials
, smb_krb5_context
, &princ
, obtained
, error_string
);
211 talloc_free(mem_ctx
);
215 ret
= impersonate_principal_from_credentials(mem_ctx
, credentials
, smb_krb5_context
, &impersonate_principal
, error_string
);
217 talloc_free(mem_ctx
);
221 target_service
= cli_credentials_get_target_service(credentials
);
223 password
= cli_credentials_get_password(credentials
);
228 ret
= kerberos_kinit_password_cc(smb_krb5_context
->krb5_context
, ccache
,
230 impersonate_principal
, target_service
,
232 } else if (impersonate_principal
) {
233 (*error_string
) = "INTERNAL error: Cannot impersonate principal with just a keyblock. A password must be specified in the credentials";
236 /* No password available, try to use a keyblock instead */
238 krb5_keyblock keyblock
;
239 const struct samr_Password
*mach_pwd
;
240 mach_pwd
= cli_credentials_get_nt_hash(credentials
, mem_ctx
);
242 talloc_free(mem_ctx
);
243 (*error_string
) = "kinit_to_ccache: No password available for kinit\n";
246 ret
= krb5_keyblock_init(smb_krb5_context
->krb5_context
,
247 ENCTYPE_ARCFOUR_HMAC
,
248 mach_pwd
->hash
, sizeof(mach_pwd
->hash
),
252 ret
= kerberos_kinit_keyblock_cc(smb_krb5_context
->krb5_context
, ccache
,
256 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &keyblock
);
260 if (ret
== KRB5KRB_AP_ERR_SKEW
|| ret
== KRB5_KDCREP_SKEW
) {
261 /* Perhaps we have been given an invalid skew, so try again without it */
262 time_t t
= time(NULL
);
263 krb5_set_real_time(smb_krb5_context
->krb5_context
, t
, 0);
265 /* not a skew problem */
270 if (ret
== KRB5KRB_AP_ERR_SKEW
|| ret
== KRB5_KDCREP_SKEW
) {
271 (*error_string
) = talloc_asprintf(credentials
, "kinit for %s failed (%s)\n",
272 cli_credentials_get_principal(credentials
, mem_ctx
),
273 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
275 talloc_free(mem_ctx
);
279 /* cope with ticket being in the future due to clock skew */
280 if ((unsigned)kdc_time
> time(NULL
)) {
281 time_t t
= time(NULL
);
282 int time_offset
=(unsigned)kdc_time
-t
;
283 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
284 krb5_set_real_time(smb_krb5_context
->krb5_context
, t
+ time_offset
+ 1, 0);
287 if (ret
== KRB5KDC_ERR_PREAUTH_FAILED
&& cli_credentials_wrong_password(credentials
)) {
288 ret
= kinit_to_ccache(parent_ctx
,
295 (*error_string
) = talloc_asprintf(credentials
, "kinit for %s failed (%s)\n",
296 cli_credentials_get_principal(credentials
, mem_ctx
),
297 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
299 talloc_free(mem_ctx
);
302 talloc_free(mem_ctx
);
306 static krb5_error_code
free_keytab(struct keytab_container
*ktc
)
308 return krb5_kt_close(ktc
->smb_krb5_context
->krb5_context
, ktc
->keytab
);
311 krb5_error_code
smb_krb5_open_keytab(TALLOC_CTX
*mem_ctx
,
312 struct smb_krb5_context
*smb_krb5_context
,
313 const char *keytab_name
, struct keytab_container
**ktc
)
317 ret
= krb5_kt_resolve(smb_krb5_context
->krb5_context
, keytab_name
, &keytab
);
319 DEBUG(1,("failed to open krb5 keytab: %s\n",
320 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
325 *ktc
= talloc(mem_ctx
, struct keytab_container
);
330 (*ktc
)->smb_krb5_context
= talloc_reference(*ktc
, smb_krb5_context
);
331 (*ktc
)->keytab
= keytab
;
332 talloc_set_destructor(*ktc
, free_keytab
);
337 static krb5_error_code
keytab_add_keys(TALLOC_CTX
*parent_ctx
,
338 const char *princ_string
,
339 krb5_principal princ
,
340 krb5_principal salt_princ
,
342 const char *password_s
,
343 struct smb_krb5_context
*smb_krb5_context
,
344 const char **enctype_strings
,
350 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
355 password
.data
= discard_const_p(char *, password_s
);
356 password
.length
= strlen(password_s
);
358 for (i
=0; enctype_strings
[i
]; i
++) {
359 krb5_keytab_entry entry
;
360 krb5_enctype enctype
;
361 ret
= krb5_string_to_enctype(smb_krb5_context
->krb5_context
, enctype_strings
[i
], &enctype
);
363 DEBUG(1, ("Failed to interpret %s as a krb5 encryption type: %s\n",
365 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
367 talloc_free(mem_ctx
);
370 ret
= create_kerberos_key_from_string(smb_krb5_context
->krb5_context
,
371 salt_princ
, &password
, &entry
.keyblock
, enctype
);
373 talloc_free(mem_ctx
);
377 entry
.principal
= princ
;
379 ret
= krb5_kt_add_entry(smb_krb5_context
->krb5_context
, keytab
, &entry
);
381 DEBUG(1, ("Failed to add %s entry for %s(kvno %d) to keytab: %s\n",
385 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
387 talloc_free(mem_ctx
);
388 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &entry
.keyblock
);
392 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
394 enctype_strings
[i
]));
396 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &entry
.keyblock
);
398 talloc_free(mem_ctx
);
402 static krb5_error_code
create_keytab(TALLOC_CTX
*parent_ctx
,
403 struct cli_credentials
*machine_account
,
404 struct smb_krb5_context
*smb_krb5_context
,
405 const char **enctype_strings
,
410 const char *password_s
;
411 const char *old_secret
;
413 krb5_principal salt_princ
;
414 krb5_principal princ
;
415 const char *princ_string
;
416 const char *error_string
;
417 enum credentials_obtained obtained
;
419 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
424 princ_string
= cli_credentials_get_principal(machine_account
, mem_ctx
);
425 /* Get the principal we will store the new keytab entries under */
426 ret
= principal_from_credentials(mem_ctx
, machine_account
, smb_krb5_context
, &princ
, &obtained
, &error_string
);
428 DEBUG(1,("create_keytab: makeing krb5 principal failed (%s)\n", error_string
));
429 talloc_free(mem_ctx
);
433 /* The salt used to generate these entries may be different however, fetch that */
434 ret
= salt_principal_from_credentials(mem_ctx
, machine_account
,
438 DEBUG(1,("create_keytab: makeing salt principal failed (%s)\n",
439 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
441 talloc_free(mem_ctx
);
445 /* Finally, do the dance to get the password to put in the entry */
446 password_s
= cli_credentials_get_password(machine_account
);
448 krb5_keytab_entry entry
;
449 const struct samr_Password
*mach_pwd
;
451 if (!str_list_check(enctype_strings
, "arcfour-hmac-md5")) {
452 DEBUG(1, ("Asked to create keytab, but with only an NT hash supplied, "
453 "but not listing arcfour-hmac-md5 as an enc type to include in the keytab!\n"));
454 talloc_free(mem_ctx
);
458 /* If we don't have the plaintext password, try for
459 * the MD4 password hash */
460 mach_pwd
= cli_credentials_get_nt_hash(machine_account
, mem_ctx
);
462 /* OK, nothing to do here */
463 talloc_free(mem_ctx
);
466 ret
= krb5_keyblock_init(smb_krb5_context
->krb5_context
,
467 ETYPE_ARCFOUR_HMAC_MD5
,
468 mach_pwd
->hash
, sizeof(mach_pwd
->hash
),
471 DEBUG(1, ("create_keytab: krb5_keyblock_init failed: %s\n",
472 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
474 talloc_free(mem_ctx
);
478 entry
.principal
= princ
;
479 entry
.vno
= cli_credentials_get_kvno(machine_account
);
480 ret
= krb5_kt_add_entry(smb_krb5_context
->krb5_context
, keytab
, &entry
);
482 DEBUG(1, ("Failed to add ARCFOUR_HMAC (only) entry for %s to keytab: %s",
483 cli_credentials_get_principal(machine_account
, mem_ctx
),
484 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
486 talloc_free(mem_ctx
);
487 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &entry
.keyblock
);
491 DEBUG(5, ("Added %s(kvno %d) to keytab (arcfour-hmac-md5)\n",
492 cli_credentials_get_principal(machine_account
, mem_ctx
),
493 cli_credentials_get_kvno(machine_account
)));
495 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &entry
.keyblock
);
497 /* Can't go any further, we only have this one key */
498 talloc_free(mem_ctx
);
502 kvno
= cli_credentials_get_kvno(machine_account
);
503 /* good, we actually have the real plaintext */
504 ret
= keytab_add_keys(mem_ctx
, princ_string
, princ
, salt_princ
,
505 kvno
, password_s
, smb_krb5_context
,
506 enctype_strings
, keytab
);
508 talloc_free(mem_ctx
);
512 if (!add_old
|| kvno
== 0) {
513 talloc_free(mem_ctx
);
517 old_secret
= cli_credentials_get_old_password(machine_account
);
519 talloc_free(mem_ctx
);
523 ret
= keytab_add_keys(mem_ctx
, princ_string
, princ
, salt_princ
,
524 kvno
- 1, old_secret
, smb_krb5_context
,
525 enctype_strings
, keytab
);
527 talloc_free(mem_ctx
);
531 talloc_free(mem_ctx
);
537 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
539 * These entries are now stale, we only keep the current, and previous entries around.
541 * Inspired by the code in Samba3 for 'use kerberos keytab'.
545 static krb5_error_code
remove_old_entries(TALLOC_CTX
*parent_ctx
,
546 struct cli_credentials
*machine_account
,
547 struct smb_krb5_context
*smb_krb5_context
,
548 krb5_keytab keytab
, bool *found_previous
)
550 krb5_error_code ret
, ret2
;
551 krb5_kt_cursor cursor
;
552 krb5_principal princ
;
554 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
555 const char *princ_string
;
556 const char *error_string
;
557 enum credentials_obtained obtained
;
563 *found_previous
= false;
564 princ_string
= cli_credentials_get_principal(machine_account
, mem_ctx
);
566 /* Get the principal we will store the new keytab entries under */
567 ret
= principal_from_credentials(mem_ctx
, machine_account
, smb_krb5_context
, &princ
, &obtained
, &error_string
);
569 DEBUG(1,("update_keytab: makeing krb5 principal failed (%s)\n", error_string
));
570 talloc_free(mem_ctx
);
574 kvno
= cli_credentials_get_kvno(machine_account
);
576 /* for each entry in the keytab */
577 ret
= krb5_kt_start_seq_get(smb_krb5_context
->krb5_context
, keytab
, &cursor
);
581 case HEIM_ERR_OPNOTSUPP
:
584 /* no point enumerating if there isn't anything here */
585 talloc_free(mem_ctx
);
588 DEBUG(1,("failed to open keytab for read of old entries: %s\n",
589 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
591 talloc_free(mem_ctx
);
596 krb5_keytab_entry entry
;
597 ret
= krb5_kt_next_entry(smb_krb5_context
->krb5_context
, keytab
, &entry
, &cursor
);
601 /* if it matches our principal */
602 if (!krb5_kt_compare(smb_krb5_context
->krb5_context
, &entry
, princ
, 0, 0)) {
603 /* Free the entry, it wasn't the one we were looking for anyway */
604 krb5_kt_free_entry(smb_krb5_context
->krb5_context
, &entry
);
608 /* delete it, if it is not kvno -1 */
609 if (entry
.vno
!= (kvno
- 1 )) {
610 /* Release the enumeration. We are going to
611 * have to start this from the top again,
612 * because deletes during enumeration may not
613 * always be consistant.
615 * Also, the enumeration locks a FILE: keytab
618 krb5_kt_end_seq_get(smb_krb5_context
->krb5_context
, keytab
, &cursor
);
620 ret
= krb5_kt_remove_entry(smb_krb5_context
->krb5_context
, keytab
, &entry
);
621 krb5_kt_free_entry(smb_krb5_context
->krb5_context
, &entry
);
623 /* Deleted: Restart from the top */
624 ret2
= krb5_kt_start_seq_get(smb_krb5_context
->krb5_context
, keytab
, &cursor
);
626 krb5_kt_free_entry(smb_krb5_context
->krb5_context
, &entry
);
627 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
628 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
631 talloc_free(mem_ctx
);
640 *found_previous
= true;
643 /* Free the entry, we don't need it any more */
644 krb5_kt_free_entry(smb_krb5_context
->krb5_context
, &entry
);
648 krb5_kt_end_seq_get(smb_krb5_context
->krb5_context
, keytab
, &cursor
);
658 DEBUG(1,("failed in deleting old entries for principal: %s: %s\n",
660 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
663 talloc_free(mem_ctx
);
667 krb5_error_code
smb_krb5_update_keytab(TALLOC_CTX
*parent_ctx
,
668 struct cli_credentials
*machine_account
,
669 struct smb_krb5_context
*smb_krb5_context
,
670 const char **enctype_strings
,
671 struct keytab_container
*keytab_container
)
675 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
680 ret
= remove_old_entries(mem_ctx
, machine_account
,
681 smb_krb5_context
, keytab_container
->keytab
, &found_previous
);
683 talloc_free(mem_ctx
);
687 /* Create a new keytab. If during the cleanout we found
688 * entires for kvno -1, then don't try and duplicate them.
689 * Otherwise, add kvno, and kvno -1 */
691 ret
= create_keytab(mem_ctx
, machine_account
, smb_krb5_context
,
693 keytab_container
->keytab
,
694 found_previous
? false : true);
695 talloc_free(mem_ctx
);
699 krb5_error_code
smb_krb5_create_memory_keytab(TALLOC_CTX
*parent_ctx
,
700 struct cli_credentials
*machine_account
,
701 struct smb_krb5_context
*smb_krb5_context
,
702 const char **enctype_strings
,
703 struct keytab_container
**keytab_container
)
706 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
707 const char *rand_string
;
708 const char *keytab_name
;
713 *keytab_container
= talloc(mem_ctx
, struct keytab_container
);
715 rand_string
= generate_random_str(mem_ctx
, 16);
717 talloc_free(mem_ctx
);
721 keytab_name
= talloc_asprintf(mem_ctx
, "MEMORY:%s",
724 talloc_free(mem_ctx
);
728 ret
= smb_krb5_open_keytab(mem_ctx
, smb_krb5_context
, keytab_name
, keytab_container
);
733 ret
= smb_krb5_update_keytab(mem_ctx
, machine_account
, smb_krb5_context
, enctype_strings
, *keytab_container
);
735 talloc_steal(parent_ctx
, *keytab_container
);
737 *keytab_container
= NULL
;
739 talloc_free(mem_ctx
);