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"
30 #include "auth/kerberos/kerberos_util.h"
32 struct principal_container
{
33 struct smb_krb5_context
*smb_krb5_context
;
34 krb5_principal principal
;
35 const char *string_form
; /* Optional */
38 static krb5_error_code
free_principal(struct principal_container
*pc
)
40 /* current heimdal - 0.6.3, which we need anyway, fixes segfaults here */
41 krb5_free_principal(pc
->smb_krb5_context
->krb5_context
, pc
->principal
);
47 static krb5_error_code
parse_principal(TALLOC_CTX
*parent_ctx
,
48 const char *princ_string
,
49 struct smb_krb5_context
*smb_krb5_context
,
50 krb5_principal
*princ
,
51 const char **error_string
)
54 struct principal_container
*mem_ctx
;
55 if (princ_string
== NULL
) {
60 ret
= krb5_parse_name(smb_krb5_context
->krb5_context
,
64 (*error_string
) = smb_get_krb5_error_message(
65 smb_krb5_context
->krb5_context
,
70 mem_ctx
= talloc(parent_ctx
, struct principal_container
);
72 (*error_string
) = error_message(ENOMEM
);
76 /* This song-and-dance effectivly puts the principal
77 * into talloc, so we can't loose it. */
78 mem_ctx
->smb_krb5_context
= talloc_reference(mem_ctx
,
80 mem_ctx
->principal
= *princ
;
81 talloc_set_destructor(mem_ctx
, free_principal
);
85 /* Obtain the principal set on this context. Requires a
86 * smb_krb5_context because we are doing krb5 principal parsing with
87 * the library routines. The returned princ is placed in the talloc
88 * system by means of a destructor (do *not* free). */
90 krb5_error_code
principal_from_credentials(TALLOC_CTX
*parent_ctx
,
91 struct cli_credentials
*credentials
,
92 struct smb_krb5_context
*smb_krb5_context
,
93 krb5_principal
*princ
,
94 enum credentials_obtained
*obtained
,
95 const char **error_string
)
98 const char *princ_string
;
99 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
100 *obtained
= CRED_UNINITIALISED
;
103 (*error_string
) = error_message(ENOMEM
);
106 princ_string
= cli_credentials_get_principal_and_obtained(credentials
,
114 ret
= parse_principal(parent_ctx
, princ_string
,
115 smb_krb5_context
, princ
, error_string
);
116 talloc_free(mem_ctx
);
120 /* Obtain the principal set on this context. Requires a
121 * smb_krb5_context because we are doing krb5 principal parsing with
122 * the library routines. The returned princ is placed in the talloc
123 * system by means of a destructor (do *not* free). */
125 static krb5_error_code
impersonate_principal_from_credentials(
126 TALLOC_CTX
*parent_ctx
,
127 struct cli_credentials
*credentials
,
128 struct smb_krb5_context
*smb_krb5_context
,
129 krb5_principal
*princ
,
130 const char **error_string
)
132 return parse_principal(parent_ctx
,
133 cli_credentials_get_impersonate_principal(credentials
),
134 smb_krb5_context
, princ
, error_string
);
137 krb5_error_code
smb_krb5_create_principals_array(TALLOC_CTX
*mem_ctx
,
138 krb5_context context
,
139 const char *account_name
,
143 uint32_t *pnum_principals
,
144 krb5_principal
**pprincipals
,
145 const char **error_string
)
147 krb5_error_code code
;
149 uint32_t num_principals
= 0;
150 krb5_principal
*principals
;
153 tmp_ctx
= talloc_new(mem_ctx
);
154 if (tmp_ctx
== NULL
) {
155 *error_string
= "Cannot allocate tmp_ctx";
160 *error_string
= "Cannot create principal without a realm";
165 if (account_name
== NULL
&& (num_spns
== 0 || spns
== NULL
)) {
166 *error_string
= "Cannot create principal without an account or SPN";
171 if (account_name
!= NULL
&& account_name
[0] != '\0') {
174 num_principals
+= num_spns
;
176 principals
= talloc_zero_array(tmp_ctx
,
179 if (principals
== NULL
) {
180 *error_string
= "Cannot allocate principals";
185 for (i
= 0; i
< num_spns
; i
++) {
186 code
= krb5_parse_name(context
, spns
[i
], &(principals
[i
]));
188 *error_string
= smb_get_krb5_error_message(context
,
195 if (account_name
!= NULL
&& account_name
[0] != '\0') {
196 code
= smb_krb5_make_principal(context
,
202 *error_string
= smb_get_krb5_error_message(context
,
209 if (pnum_principals
!= NULL
) {
210 *pnum_principals
= num_principals
;
212 if (pprincipals
!= NULL
) {
213 *pprincipals
= talloc_steal(mem_ctx
, principals
);
219 talloc_free(tmp_ctx
);
224 * Return a freshly allocated ccache (destroyed by destructor on child
225 * of parent_ctx), for a given set of client credentials
228 krb5_error_code
kinit_to_ccache(TALLOC_CTX
*parent_ctx
,
229 struct cli_credentials
*credentials
,
230 struct smb_krb5_context
*smb_krb5_context
,
231 struct tevent_context
*event_ctx
,
233 enum credentials_obtained
*obtained
,
234 const char **error_string
)
237 const char *password
;
238 const char *self_service
;
239 const char *target_service
;
241 krb5_principal princ
;
242 krb5_principal impersonate_principal
;
244 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
245 krb5_get_init_creds_opt
*krb_options
;
248 (*error_string
) = strerror(ENOMEM
);
252 ret
= principal_from_credentials(mem_ctx
, credentials
, smb_krb5_context
, &princ
, obtained
, error_string
);
254 talloc_free(mem_ctx
);
259 (*error_string
) = talloc_asprintf(credentials
, "principal, username or realm was not specified in the credentials");
260 talloc_free(mem_ctx
);
261 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
264 ret
= impersonate_principal_from_credentials(mem_ctx
, credentials
, smb_krb5_context
, &impersonate_principal
, error_string
);
266 talloc_free(mem_ctx
);
270 self_service
= cli_credentials_get_self_service(credentials
);
271 target_service
= cli_credentials_get_target_service(credentials
);
273 password
= cli_credentials_get_password(credentials
);
275 /* setup the krb5 options we want */
276 if ((ret
= krb5_get_init_creds_opt_alloc(smb_krb5_context
->krb5_context
, &krb_options
))) {
277 (*error_string
) = talloc_asprintf(credentials
, "krb5_get_init_creds_opt_alloc failed (%s)\n",
278 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
280 talloc_free(mem_ctx
);
284 #ifdef SAMBA4_USES_HEIMDAL /* Disable for now MIT reads defaults when needed */
285 /* get the defaults */
286 krb5_get_init_creds_opt_set_default_flags(smb_krb5_context
->krb5_context
, NULL
, NULL
, krb_options
);
288 /* set if we want a forwardable ticket */
289 switch (cli_credentials_get_krb_forwardable(credentials
)) {
290 case CRED_AUTO_KRB_FORWARDABLE
:
292 case CRED_NO_KRB_FORWARDABLE
:
293 krb5_get_init_creds_opt_set_forwardable(krb_options
, FALSE
);
295 case CRED_FORCE_KRB_FORWARDABLE
:
296 krb5_get_init_creds_opt_set_forwardable(krb_options
, TRUE
);
300 #ifdef SAMBA4_USES_HEIMDAL /* FIXME: MIT does not have this yet */
302 * In order to work against windows KDCs even if we use
303 * the netbios domain name as realm, we need to add the following
305 * KRB5_INIT_CREDS_NO_C_CANON_CHECK;
306 * KRB5_INIT_CREDS_NO_C_NO_EKU_CHECK;
308 * On MIT: Set pkinit_eku_checking to none
310 krb5_get_init_creds_opt_set_win2k(smb_krb5_context
->krb5_context
,
313 krb5_get_init_creds_opt_set_canonicalize(krb_options
, true);
318 #ifdef SAMBA4_USES_HEIMDAL
319 struct tevent_context
*previous_ev
;
320 /* Do this every time, in case we have weird recursive issues here */
321 ret
= smb_krb5_context_set_event_ctx(smb_krb5_context
, event_ctx
, &previous_ev
);
323 talloc_free(mem_ctx
);
328 if (impersonate_principal
) {
329 #ifdef SAMBA4_USES_HEIMDAL
330 ret
= kerberos_kinit_s4u2_cc(
331 smb_krb5_context
->krb5_context
,
332 ccache
, princ
, password
,
333 impersonate_principal
,
334 self_service
, target_service
,
335 krb_options
, NULL
, &kdc_time
);
337 talloc_free(mem_ctx
);
338 (*error_string
) = "INTERNAL error: s4u2 ops "
339 "are not supported with MIT build yet";
343 ret
= kerberos_kinit_password_cc(
344 smb_krb5_context
->krb5_context
,
345 ccache
, princ
, password
,
347 krb_options
, NULL
, &kdc_time
);
349 } else if (impersonate_principal
) {
350 talloc_free(mem_ctx
);
351 (*error_string
) = "INTERNAL error: Cannot impersonate principal with just a keyblock. A password must be specified in the credentials";
354 /* No password available, try to use a keyblock instead */
356 krb5_keyblock keyblock
;
357 const struct samr_Password
*mach_pwd
;
358 mach_pwd
= cli_credentials_get_nt_hash(credentials
, mem_ctx
);
360 talloc_free(mem_ctx
);
361 (*error_string
) = "kinit_to_ccache: No password available for kinit\n";
362 krb5_get_init_creds_opt_free(smb_krb5_context
->krb5_context
, krb_options
);
363 #ifdef SAMBA4_USES_HEIMDAL
364 smb_krb5_context_remove_event_ctx(smb_krb5_context
, previous_ev
, event_ctx
);
368 ret
= smb_krb5_keyblock_init_contents(smb_krb5_context
->krb5_context
,
369 ENCTYPE_ARCFOUR_HMAC
,
370 mach_pwd
->hash
, sizeof(mach_pwd
->hash
),
374 ret
= kerberos_kinit_keyblock_cc(smb_krb5_context
->krb5_context
, ccache
,
376 target_service
, krb_options
,
378 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &keyblock
);
382 #ifdef SAMBA4_USES_HEIMDAL
383 smb_krb5_context_remove_event_ctx(smb_krb5_context
, previous_ev
, event_ctx
);
386 if (ret
== KRB5KRB_AP_ERR_SKEW
|| ret
== KRB5_KDCREP_SKEW
) {
387 /* Perhaps we have been given an invalid skew, so try again without it */
388 time_t t
= time(NULL
);
389 krb5_set_real_time(smb_krb5_context
->krb5_context
, t
, 0);
391 /* not a skew problem */
396 krb5_get_init_creds_opt_free(smb_krb5_context
->krb5_context
, krb_options
);
398 if (ret
== KRB5KRB_AP_ERR_SKEW
|| ret
== KRB5_KDCREP_SKEW
) {
399 (*error_string
) = talloc_asprintf(credentials
, "kinit for %s failed (%s)\n",
400 cli_credentials_get_principal(credentials
, mem_ctx
),
401 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
403 talloc_free(mem_ctx
);
407 /* cope with ticket being in the future due to clock skew */
408 if ((unsigned)kdc_time
> time(NULL
)) {
409 time_t t
= time(NULL
);
410 int time_offset
=(unsigned)kdc_time
-t
;
411 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
412 krb5_set_real_time(smb_krb5_context
->krb5_context
, t
+ time_offset
+ 1, 0);
415 if (ret
== KRB5KDC_ERR_PREAUTH_FAILED
&& cli_credentials_wrong_password(credentials
)) {
416 ret
= kinit_to_ccache(parent_ctx
,
425 (*error_string
) = talloc_asprintf(credentials
, "kinit for %s failed (%s)\n",
426 cli_credentials_get_principal(credentials
, mem_ctx
),
427 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
429 talloc_free(mem_ctx
);
433 DEBUG(10,("kinit for %s succeeded\n",
434 cli_credentials_get_principal(credentials
, mem_ctx
)));
437 talloc_free(mem_ctx
);
441 static krb5_error_code
free_keytab_container(struct keytab_container
*ktc
)
443 return krb5_kt_close(ktc
->smb_krb5_context
->krb5_context
, ktc
->keytab
);
446 krb5_error_code
smb_krb5_get_keytab_container(TALLOC_CTX
*mem_ctx
,
447 struct smb_krb5_context
*smb_krb5_context
,
448 krb5_keytab opt_keytab
,
449 const char *keytab_name
,
450 struct keytab_container
**ktc
)
458 ret
= krb5_kt_resolve(smb_krb5_context
->krb5_context
,
459 keytab_name
, &keytab
);
461 DEBUG(1,("failed to open krb5 keytab: %s\n",
462 smb_get_krb5_error_message(
463 smb_krb5_context
->krb5_context
,
469 *ktc
= talloc(mem_ctx
, struct keytab_container
);
474 (*ktc
)->smb_krb5_context
= talloc_reference(*ktc
, smb_krb5_context
);
475 (*ktc
)->keytab
= keytab
;
476 (*ktc
)->password_based
= false;
477 talloc_set_destructor(*ktc
, free_keytab_container
);
483 * Walk the keytab, looking for entries of this principal name,
484 * with KVNO other than current kvno -1.
486 * These entries are now stale,
487 * we only keep the current and previous entries around.
489 * Inspired by the code in Samba3 for 'use kerberos keytab'.
491 krb5_error_code
smb_krb5_remove_obsolete_keytab_entries(TALLOC_CTX
*mem_ctx
,
492 krb5_context context
,
494 uint32_t num_principals
,
495 krb5_principal
*principals
,
497 bool *found_previous
,
498 const char **error_string
)
501 krb5_error_code code
;
502 krb5_kt_cursor cursor
;
504 tmp_ctx
= talloc_new(mem_ctx
);
505 if (tmp_ctx
== NULL
) {
506 *error_string
= "Cannot allocate tmp_ctx";
510 *found_previous
= true;
512 code
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
516 #ifdef HEIM_ERR_OPNOTSUPP
517 case HEIM_ERR_OPNOTSUPP
:
521 /* no point enumerating if there isn't anything here */
525 *error_string
= talloc_asprintf(mem_ctx
,
526 "failed to open keytab for read of old entries: %s\n",
527 smb_get_krb5_error_message(context
, code
, mem_ctx
));
532 krb5_kvno old_kvno
= kvno
- 1;
533 krb5_keytab_entry entry
;
534 bool matched
= false;
537 code
= krb5_kt_next_entry(context
, keytab
, &entry
, &cursor
);
542 for (i
= 0; i
< num_principals
; i
++) {
545 ok
= smb_krb5_kt_compare(context
,
558 * Free the entry, it wasn't the one we were looking
561 krb5_kt_free_entry(context
, &entry
);
562 /* Make sure we do not double free */
568 * Delete it, if it is not kvno - 1.
570 * Some keytab files store the kvno only in 8bits. Limit the
571 * compare to 8bits, so that we don't miss old keys and delete
574 if ((entry
.vno
& 0xff) != (old_kvno
& 0xff)) {
577 /* Release the enumeration. We are going to
578 * have to start this from the top again,
579 * because deletes during enumeration may not
580 * always be consistent.
582 * Also, the enumeration locks a FILE: keytab
584 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
586 code
= krb5_kt_remove_entry(context
, keytab
, &entry
);
587 krb5_kt_free_entry(context
, &entry
);
589 /* Make sure we do not double free */
592 /* Deleted: Restart from the top */
593 rc
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
595 krb5_kt_free_entry(context
, &entry
);
597 /* Make sure we do not double free */
600 DEBUG(1, ("failed to restart enumeration of keytab: %s\n",
601 smb_get_krb5_error_message(context
,
605 talloc_free(tmp_ctx
);
614 *found_previous
= true;
617 /* Free the entry, we don't need it any more */
618 krb5_kt_free_entry(context
, &entry
);
619 /* Make sure we do not double free */
623 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
633 *error_string
= talloc_asprintf(mem_ctx
,
634 "failed in deleting old entries for principal: %s\n",
635 smb_get_krb5_error_message(context
,
642 talloc_free(tmp_ctx
);