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"
30 struct principal_container
{
31 struct smb_krb5_context
*smb_krb5_context
;
32 krb5_principal principal
;
35 static krb5_error_code
free_principal(struct principal_container
*pc
)
37 /* current heimdal - 0.6.3, which we need anyway, fixes segfaults here */
38 krb5_free_principal(pc
->smb_krb5_context
->krb5_context
, pc
->principal
);
44 static krb5_error_code
parse_principal(TALLOC_CTX
*parent_ctx
,
45 const char *princ_string
,
46 struct smb_krb5_context
*smb_krb5_context
,
47 krb5_principal
*princ
,
48 const char **error_string
)
51 struct principal_container
*mem_ctx
;
52 if (princ_string
== NULL
) {
57 ret
= krb5_parse_name(smb_krb5_context
->krb5_context
,
61 (*error_string
) = smb_get_krb5_error_message(smb_krb5_context
->krb5_context
, ret
, parent_ctx
);
65 mem_ctx
= talloc(parent_ctx
, struct principal_container
);
67 (*error_string
) = error_message(ENOMEM
);
71 /* This song-and-dance effectivly puts the principal
72 * into talloc, so we can't loose it. */
73 mem_ctx
->smb_krb5_context
= talloc_reference(mem_ctx
, smb_krb5_context
);
74 mem_ctx
->principal
= *princ
;
75 talloc_set_destructor(mem_ctx
, free_principal
);
79 static krb5_error_code
salt_principal_from_credentials(TALLOC_CTX
*parent_ctx
,
80 struct cli_credentials
*machine_account
,
81 struct smb_krb5_context
*smb_krb5_context
,
82 krb5_principal
*salt_princ
)
85 char *machine_username
;
88 const char *salt_principal
;
89 const char *error_string
;
90 struct principal_container
*mem_ctx
= talloc(parent_ctx
, struct principal_container
);
95 salt_principal
= cli_credentials_get_salt_principal(machine_account
);
97 ret
= parse_principal(parent_ctx
, salt_principal
, smb_krb5_context
, salt_princ
, &error_string
);
99 machine_username
= talloc_strdup(mem_ctx
, cli_credentials_get_username(machine_account
));
101 if (!machine_username
) {
102 talloc_free(mem_ctx
);
106 if (machine_username
[strlen(machine_username
)-1] == '$') {
107 machine_username
[strlen(machine_username
)-1] = '\0';
109 lower_realm
= strlower_talloc(mem_ctx
, cli_credentials_get_realm(machine_account
));
111 talloc_free(mem_ctx
);
115 salt_body
= talloc_asprintf(mem_ctx
, "%s.%s", machine_username
,
118 talloc_free(mem_ctx
);
122 ret
= krb5_make_principal(smb_krb5_context
->krb5_context
, salt_princ
,
123 cli_credentials_get_realm(machine_account
),
124 "host", salt_body
, NULL
);
126 /* This song-and-dance effectivly puts the principal
127 * into talloc, so we can't loose it. */
128 mem_ctx
->smb_krb5_context
= talloc_reference(mem_ctx
, smb_krb5_context
);
129 mem_ctx
->principal
= *salt_princ
;
130 talloc_set_destructor(mem_ctx
, free_principal
);
137 /* Obtain the principal set on this context. Requires a
138 * smb_krb5_context because we are doing krb5 principal parsing with
139 * the library routines. The returned princ is placed in the talloc
140 * system by means of a destructor (do *not* free). */
142 krb5_error_code
principal_from_credentials(TALLOC_CTX
*parent_ctx
,
143 struct cli_credentials
*credentials
,
144 struct smb_krb5_context
*smb_krb5_context
,
145 krb5_principal
*princ
,
146 const char **error_string
)
149 const char *princ_string
;
150 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
152 (*error_string
) = error_message(ENOMEM
);
155 princ_string
= cli_credentials_get_principal(credentials
, mem_ctx
);
157 (*error_string
) = error_message(ENOMEM
);
161 ret
= parse_principal(parent_ctx
, princ_string
,
162 smb_krb5_context
, princ
, error_string
);
163 talloc_free(mem_ctx
);
167 /* Obtain the principal set on this context. Requires a
168 * smb_krb5_context because we are doing krb5 principal parsing with
169 * the library routines. The returned princ is placed in the talloc
170 * system by means of a destructor (do *not* free). */
172 krb5_error_code
impersonate_principal_from_credentials(TALLOC_CTX
*parent_ctx
,
173 struct cli_credentials
*credentials
,
174 struct smb_krb5_context
*smb_krb5_context
,
175 krb5_principal
*princ
,
176 const char **error_string
)
178 return parse_principal(parent_ctx
, cli_credentials_get_impersonate_principal(credentials
),
179 smb_krb5_context
, princ
, error_string
);
183 * Return a freshly allocated ccache (destroyed by destructor on child
184 * of parent_ctx), for a given set of client credentials
187 krb5_error_code
kinit_to_ccache(TALLOC_CTX
*parent_ctx
,
188 struct cli_credentials
*credentials
,
189 struct smb_krb5_context
*smb_krb5_context
,
191 const char **error_string
)
194 const char *password
, *target_service
;
196 krb5_principal princ
;
197 krb5_principal impersonate_principal
;
199 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
202 (*error_string
) = strerror(ENOMEM
);
206 ret
= principal_from_credentials(mem_ctx
, credentials
, smb_krb5_context
, &princ
, error_string
);
208 talloc_free(mem_ctx
);
212 ret
= impersonate_principal_from_credentials(mem_ctx
, credentials
, smb_krb5_context
, &impersonate_principal
, error_string
);
214 talloc_free(mem_ctx
);
218 target_service
= cli_credentials_get_target_service(credentials
);
220 password
= cli_credentials_get_password(credentials
);
225 ret
= kerberos_kinit_password_cc(smb_krb5_context
->krb5_context
, ccache
,
227 impersonate_principal
, target_service
,
229 } else if (impersonate_principal
) {
230 (*error_string
) = "INTERNAL error: Cannot impersonate principal with just a keyblock. A password must be specified in the credentials";
233 /* No password available, try to use a keyblock instead */
235 krb5_keyblock keyblock
;
236 const struct samr_Password
*mach_pwd
;
237 mach_pwd
= cli_credentials_get_nt_hash(credentials
, mem_ctx
);
239 talloc_free(mem_ctx
);
240 (*error_string
) = "kinit_to_ccache: No password available for kinit\n";
243 ret
= krb5_keyblock_init(smb_krb5_context
->krb5_context
,
244 ENCTYPE_ARCFOUR_HMAC
,
245 mach_pwd
->hash
, sizeof(mach_pwd
->hash
),
249 ret
= kerberos_kinit_keyblock_cc(smb_krb5_context
->krb5_context
, ccache
,
253 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &keyblock
);
257 if (ret
== KRB5KRB_AP_ERR_SKEW
|| ret
== KRB5_KDCREP_SKEW
) {
258 /* Perhaps we have been given an invalid skew, so try again without it */
259 time_t t
= time(NULL
);
260 krb5_set_real_time(smb_krb5_context
->krb5_context
, t
, 0);
262 /* not a skew problem */
267 if (ret
== KRB5KRB_AP_ERR_SKEW
|| ret
== KRB5_KDCREP_SKEW
) {
268 (*error_string
) = talloc_asprintf(credentials
, "kinit for %s failed (%s)\n",
269 cli_credentials_get_principal(credentials
, mem_ctx
),
270 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
272 talloc_free(mem_ctx
);
276 /* cope with ticket being in the future due to clock skew */
277 if ((unsigned)kdc_time
> time(NULL
)) {
278 time_t t
= time(NULL
);
279 int time_offset
=(unsigned)kdc_time
-t
;
280 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
281 krb5_set_real_time(smb_krb5_context
->krb5_context
, t
+ time_offset
+ 1, 0);
284 if (ret
== KRB5KDC_ERR_PREAUTH_FAILED
&& cli_credentials_wrong_password(credentials
)) {
285 ret
= kinit_to_ccache(parent_ctx
,
288 ccache
, error_string
);
291 (*error_string
) = talloc_asprintf(credentials
, "kinit for %s failed (%s)\n",
292 cli_credentials_get_principal(credentials
, mem_ctx
),
293 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
295 talloc_free(mem_ctx
);
298 talloc_free(mem_ctx
);
302 static krb5_error_code
free_keytab(struct keytab_container
*ktc
)
304 return krb5_kt_close(ktc
->smb_krb5_context
->krb5_context
, ktc
->keytab
);
307 krb5_error_code
smb_krb5_open_keytab(TALLOC_CTX
*mem_ctx
,
308 struct smb_krb5_context
*smb_krb5_context
,
309 const char *keytab_name
, struct keytab_container
**ktc
)
313 ret
= krb5_kt_resolve(smb_krb5_context
->krb5_context
, keytab_name
, &keytab
);
315 DEBUG(1,("failed to open krb5 keytab: %s\n",
316 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
321 *ktc
= talloc(mem_ctx
, struct keytab_container
);
326 (*ktc
)->smb_krb5_context
= talloc_reference(*ktc
, smb_krb5_context
);
327 (*ktc
)->keytab
= keytab
;
328 talloc_set_destructor(*ktc
, free_keytab
);
333 static krb5_error_code
keytab_add_keys(TALLOC_CTX
*parent_ctx
,
334 const char *princ_string
,
335 krb5_principal princ
,
336 krb5_principal salt_princ
,
338 const char *password_s
,
339 struct smb_krb5_context
*smb_krb5_context
,
340 const char **enctype_strings
,
346 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
351 password
.data
= discard_const_p(char *, password_s
);
352 password
.length
= strlen(password_s
);
354 for (i
=0; enctype_strings
[i
]; i
++) {
355 krb5_keytab_entry entry
;
356 krb5_enctype enctype
;
357 ret
= krb5_string_to_enctype(smb_krb5_context
->krb5_context
, enctype_strings
[i
], &enctype
);
359 DEBUG(1, ("Failed to interpret %s as a krb5 encryption type: %s\n",
361 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
363 talloc_free(mem_ctx
);
366 ret
= create_kerberos_key_from_string(smb_krb5_context
->krb5_context
,
367 salt_princ
, &password
, &entry
.keyblock
, enctype
);
369 talloc_free(mem_ctx
);
373 entry
.principal
= princ
;
375 ret
= krb5_kt_add_entry(smb_krb5_context
->krb5_context
, keytab
, &entry
);
377 DEBUG(1, ("Failed to add %s entry for %s(kvno %d) to keytab: %s\n",
381 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
383 talloc_free(mem_ctx
);
384 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &entry
.keyblock
);
388 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n",
390 enctype_strings
[i
]));
392 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &entry
.keyblock
);
394 talloc_free(mem_ctx
);
398 static krb5_error_code
create_keytab(TALLOC_CTX
*parent_ctx
,
399 struct cli_credentials
*machine_account
,
400 struct smb_krb5_context
*smb_krb5_context
,
401 const char **enctype_strings
,
406 const char *password_s
;
407 const char *old_secret
;
409 krb5_principal salt_princ
;
410 krb5_principal princ
;
411 const char *princ_string
;
412 const char *error_string
;
414 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
419 princ_string
= cli_credentials_get_principal(machine_account
, mem_ctx
);
420 /* Get the principal we will store the new keytab entries under */
421 ret
= principal_from_credentials(mem_ctx
, machine_account
, smb_krb5_context
, &princ
, &error_string
);
423 DEBUG(1,("create_keytab: makeing krb5 principal failed (%s)\n", error_string
));
424 talloc_free(mem_ctx
);
428 /* The salt used to generate these entries may be different however, fetch that */
429 ret
= salt_principal_from_credentials(mem_ctx
, machine_account
,
433 DEBUG(1,("create_keytab: makeing salt principal failed (%s)\n",
434 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
436 talloc_free(mem_ctx
);
440 /* Finally, do the dance to get the password to put in the entry */
441 password_s
= cli_credentials_get_password(machine_account
);
443 krb5_keytab_entry entry
;
444 const struct samr_Password
*mach_pwd
;
446 if (!str_list_check(enctype_strings
, "arcfour-hmac-md5")) {
447 DEBUG(1, ("Asked to create keytab, but with only an NT hash supplied, "
448 "but not listing arcfour-hmac-md5 as an enc type to include in the keytab!\n"));
449 talloc_free(mem_ctx
);
453 /* If we don't have the plaintext password, try for
454 * the MD4 password hash */
455 mach_pwd
= cli_credentials_get_nt_hash(machine_account
, mem_ctx
);
457 /* OK, nothing to do here */
458 talloc_free(mem_ctx
);
461 ret
= krb5_keyblock_init(smb_krb5_context
->krb5_context
,
462 ETYPE_ARCFOUR_HMAC_MD5
,
463 mach_pwd
->hash
, sizeof(mach_pwd
->hash
),
466 DEBUG(1, ("create_keytab: krb5_keyblock_init failed: %s\n",
467 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
469 talloc_free(mem_ctx
);
473 entry
.principal
= princ
;
474 entry
.vno
= cli_credentials_get_kvno(machine_account
);
475 ret
= krb5_kt_add_entry(smb_krb5_context
->krb5_context
, keytab
, &entry
);
477 DEBUG(1, ("Failed to add ARCFOUR_HMAC (only) entry for %s to keytab: %s",
478 cli_credentials_get_principal(machine_account
, mem_ctx
),
479 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
481 talloc_free(mem_ctx
);
482 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &entry
.keyblock
);
486 DEBUG(5, ("Added %s(kvno %d) to keytab (arcfour-hmac-md5)\n",
487 cli_credentials_get_principal(machine_account
, mem_ctx
),
488 cli_credentials_get_kvno(machine_account
)));
490 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &entry
.keyblock
);
492 /* Can't go any further, we only have this one key */
493 talloc_free(mem_ctx
);
497 kvno
= cli_credentials_get_kvno(machine_account
);
498 /* good, we actually have the real plaintext */
499 ret
= keytab_add_keys(mem_ctx
, princ_string
, princ
, salt_princ
,
500 kvno
, password_s
, smb_krb5_context
,
501 enctype_strings
, keytab
);
503 talloc_free(mem_ctx
);
507 if (!add_old
|| kvno
== 0) {
508 talloc_free(mem_ctx
);
512 old_secret
= cli_credentials_get_old_password(machine_account
);
514 talloc_free(mem_ctx
);
518 ret
= keytab_add_keys(mem_ctx
, princ_string
, princ
, salt_princ
,
519 kvno
- 1, old_secret
, smb_krb5_context
,
520 enctype_strings
, keytab
);
522 talloc_free(mem_ctx
);
526 talloc_free(mem_ctx
);
532 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
534 * These entries are now stale, we only keep the current, and previous entries around.
536 * Inspired by the code in Samba3 for 'use kerberos keytab'.
540 static krb5_error_code
remove_old_entries(TALLOC_CTX
*parent_ctx
,
541 struct cli_credentials
*machine_account
,
542 struct smb_krb5_context
*smb_krb5_context
,
543 krb5_keytab keytab
, bool *found_previous
)
545 krb5_error_code ret
, ret2
;
546 krb5_kt_cursor cursor
;
547 krb5_principal princ
;
549 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
550 const char *princ_string
;
551 const char *error_string
;
557 *found_previous
= false;
558 princ_string
= cli_credentials_get_principal(machine_account
, mem_ctx
);
560 /* Get the principal we will store the new keytab entries under */
561 ret
= principal_from_credentials(mem_ctx
, machine_account
, smb_krb5_context
, &princ
, &error_string
);
563 DEBUG(1,("update_keytab: makeing krb5 principal failed (%s)\n", error_string
));
564 talloc_free(mem_ctx
);
568 kvno
= cli_credentials_get_kvno(machine_account
);
570 /* for each entry in the keytab */
571 ret
= krb5_kt_start_seq_get(smb_krb5_context
->krb5_context
, keytab
, &cursor
);
575 case HEIM_ERR_OPNOTSUPP
:
578 /* no point enumerating if there isn't anything here */
579 talloc_free(mem_ctx
);
582 DEBUG(1,("failed to open keytab for read of old entries: %s\n",
583 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
585 talloc_free(mem_ctx
);
590 krb5_keytab_entry entry
;
591 ret
= krb5_kt_next_entry(smb_krb5_context
->krb5_context
, keytab
, &entry
, &cursor
);
595 /* if it matches our principal */
596 if (!krb5_kt_compare(smb_krb5_context
->krb5_context
, &entry
, princ
, 0, 0)) {
597 /* Free the entry, it wasn't the one we were looking for anyway */
598 krb5_kt_free_entry(smb_krb5_context
->krb5_context
, &entry
);
602 /* delete it, if it is not kvno -1 */
603 if (entry
.vno
!= (kvno
- 1 )) {
604 /* Release the enumeration. We are going to
605 * have to start this from the top again,
606 * because deletes during enumeration may not
607 * always be consistant.
609 * Also, the enumeration locks a FILE: keytab
612 krb5_kt_end_seq_get(smb_krb5_context
->krb5_context
, keytab
, &cursor
);
614 ret
= krb5_kt_remove_entry(smb_krb5_context
->krb5_context
, keytab
, &entry
);
615 krb5_kt_free_entry(smb_krb5_context
->krb5_context
, &entry
);
617 /* Deleted: Restart from the top */
618 ret2
= krb5_kt_start_seq_get(smb_krb5_context
->krb5_context
, keytab
, &cursor
);
620 krb5_kt_free_entry(smb_krb5_context
->krb5_context
, &entry
);
621 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
622 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
625 talloc_free(mem_ctx
);
634 *found_previous
= true;
637 /* Free the entry, we don't need it any more */
638 krb5_kt_free_entry(smb_krb5_context
->krb5_context
, &entry
);
642 krb5_kt_end_seq_get(smb_krb5_context
->krb5_context
, keytab
, &cursor
);
652 DEBUG(1,("failed in deleting old entries for principal: %s: %s\n",
654 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
657 talloc_free(mem_ctx
);
661 krb5_error_code
smb_krb5_update_keytab(TALLOC_CTX
*parent_ctx
,
662 struct cli_credentials
*machine_account
,
663 struct smb_krb5_context
*smb_krb5_context
,
664 const char **enctype_strings
,
665 struct keytab_container
*keytab_container
)
669 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
674 ret
= remove_old_entries(mem_ctx
, machine_account
,
675 smb_krb5_context
, keytab_container
->keytab
, &found_previous
);
677 talloc_free(mem_ctx
);
681 /* Create a new keytab. If during the cleanout we found
682 * entires for kvno -1, then don't try and duplicate them.
683 * Otherwise, add kvno, and kvno -1 */
685 ret
= create_keytab(mem_ctx
, machine_account
, smb_krb5_context
,
687 keytab_container
->keytab
,
688 found_previous
? false : true);
689 talloc_free(mem_ctx
);
693 krb5_error_code
smb_krb5_create_memory_keytab(TALLOC_CTX
*parent_ctx
,
694 struct cli_credentials
*machine_account
,
695 struct smb_krb5_context
*smb_krb5_context
,
696 const char **enctype_strings
,
697 struct keytab_container
**keytab_container
)
700 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
701 const char *rand_string
;
702 const char *keytab_name
;
707 *keytab_container
= talloc(mem_ctx
, struct keytab_container
);
709 rand_string
= generate_random_str(mem_ctx
, 16);
711 talloc_free(mem_ctx
);
715 keytab_name
= talloc_asprintf(mem_ctx
, "MEMORY:%s",
718 talloc_free(mem_ctx
);
722 ret
= smb_krb5_open_keytab(mem_ctx
, smb_krb5_context
, keytab_name
, keytab_container
);
727 ret
= smb_krb5_update_keytab(mem_ctx
, machine_account
, smb_krb5_context
, enctype_strings
, *keytab_container
);
729 talloc_steal(parent_ctx
, *keytab_container
);
731 *keytab_container
= NULL
;
733 talloc_free(mem_ctx
);