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 krb5_error_code
free_principal(struct principal_container
*pc
)
34 /* current heimdal - 0.6.3, which we need anyway, fixes segfaults here */
35 krb5_free_principal(pc
->smb_krb5_context
->krb5_context
, pc
->principal
);
41 krb5_error_code
parse_principal(TALLOC_CTX
*parent_ctx
,
42 const char *princ_string
,
43 struct smb_krb5_context
*smb_krb5_context
,
44 krb5_principal
*princ
,
45 const char **error_string
)
48 struct principal_container
*mem_ctx
;
49 if (princ_string
== NULL
) {
54 ret
= krb5_parse_name(smb_krb5_context
->krb5_context
,
58 (*error_string
) = smb_get_krb5_error_message(
59 smb_krb5_context
->krb5_context
,
64 mem_ctx
= talloc(parent_ctx
, struct principal_container
);
66 (*error_string
) = error_message(ENOMEM
);
70 /* This song-and-dance effectivly puts the principal
71 * into talloc, so we can't loose it. */
72 mem_ctx
->smb_krb5_context
= talloc_reference(mem_ctx
,
74 mem_ctx
->principal
= *princ
;
75 talloc_set_destructor(mem_ctx
, free_principal
);
79 /* Obtain the principal set on this context. Requires a
80 * smb_krb5_context because we are doing krb5 principal parsing with
81 * the library routines. The returned princ is placed in the talloc
82 * system by means of a destructor (do *not* free). */
84 krb5_error_code
principal_from_credentials(TALLOC_CTX
*parent_ctx
,
85 struct cli_credentials
*credentials
,
86 struct smb_krb5_context
*smb_krb5_context
,
87 krb5_principal
*princ
,
88 enum credentials_obtained
*obtained
,
89 const char **error_string
)
92 const char *princ_string
;
93 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
94 *obtained
= CRED_UNINITIALISED
;
97 (*error_string
) = error_message(ENOMEM
);
100 princ_string
= cli_credentials_get_principal_and_obtained(credentials
,
108 ret
= parse_principal(parent_ctx
, princ_string
,
109 smb_krb5_context
, princ
, error_string
);
110 talloc_free(mem_ctx
);
114 /* Obtain the principal set on this context. Requires a
115 * smb_krb5_context because we are doing krb5 principal parsing with
116 * the library routines. The returned princ is placed in the talloc
117 * system by means of a destructor (do *not* free). */
119 static krb5_error_code
impersonate_principal_from_credentials(
120 TALLOC_CTX
*parent_ctx
,
121 struct cli_credentials
*credentials
,
122 struct smb_krb5_context
*smb_krb5_context
,
123 krb5_principal
*princ
,
124 const char **error_string
)
126 return parse_principal(parent_ctx
,
127 cli_credentials_get_impersonate_principal(credentials
),
128 smb_krb5_context
, princ
, error_string
);
132 * Return a freshly allocated ccache (destroyed by destructor on child
133 * of parent_ctx), for a given set of client credentials
136 krb5_error_code
kinit_to_ccache(TALLOC_CTX
*parent_ctx
,
137 struct cli_credentials
*credentials
,
138 struct smb_krb5_context
*smb_krb5_context
,
139 struct tevent_context
*event_ctx
,
141 enum credentials_obtained
*obtained
,
142 const char **error_string
)
145 const char *password
;
146 const char *self_service
;
147 const char *target_service
;
149 krb5_principal princ
;
150 krb5_principal impersonate_principal
;
152 TALLOC_CTX
*mem_ctx
= talloc_new(parent_ctx
);
153 krb5_get_init_creds_opt
*krb_options
;
156 (*error_string
) = strerror(ENOMEM
);
160 ret
= principal_from_credentials(mem_ctx
, credentials
, smb_krb5_context
, &princ
, obtained
, error_string
);
162 talloc_free(mem_ctx
);
167 (*error_string
) = talloc_asprintf(credentials
, "principal, username or realm was not specified in the credentials");
168 talloc_free(mem_ctx
);
169 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
172 ret
= impersonate_principal_from_credentials(mem_ctx
, credentials
, smb_krb5_context
, &impersonate_principal
, error_string
);
174 talloc_free(mem_ctx
);
178 self_service
= cli_credentials_get_self_service(credentials
);
179 target_service
= cli_credentials_get_target_service(credentials
);
181 password
= cli_credentials_get_password(credentials
);
183 /* setup the krb5 options we want */
184 if ((ret
= krb5_get_init_creds_opt_alloc(smb_krb5_context
->krb5_context
, &krb_options
))) {
185 (*error_string
) = talloc_asprintf(credentials
, "krb5_get_init_creds_opt_alloc failed (%s)\n",
186 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
188 talloc_free(mem_ctx
);
192 /* get the defaults */
193 krb5_get_init_creds_opt_set_default_flags(smb_krb5_context
->krb5_context
, NULL
, NULL
, krb_options
);
195 /* set if we want a forwardable ticket */
196 switch (cli_credentials_get_krb_forwardable(credentials
)) {
197 case CRED_AUTO_KRB_FORWARDABLE
:
199 case CRED_NO_KRB_FORWARDABLE
:
200 krb5_get_init_creds_opt_set_forwardable(krb_options
, FALSE
);
202 case CRED_FORCE_KRB_FORWARDABLE
:
203 krb5_get_init_creds_opt_set_forwardable(krb_options
, TRUE
);
208 * In order to work against windows KDCs even if we use
209 * the netbios domain name as realm, we need to add the following
211 * KRB5_INIT_CREDS_NO_C_CANON_CHECK;
212 * KRB5_INIT_CREDS_NO_C_NO_EKU_CHECK;
214 krb5_get_init_creds_opt_set_win2k(smb_krb5_context
->krb5_context
,
219 struct tevent_context
*previous_ev
;
220 /* Do this every time, in case we have weird recursive issues here */
221 ret
= smb_krb5_context_set_event_ctx(smb_krb5_context
, event_ctx
, &previous_ev
);
223 talloc_free(mem_ctx
);
227 ret
= kerberos_kinit_password_cc(smb_krb5_context
->krb5_context
, ccache
,
229 impersonate_principal
,
234 } else if (impersonate_principal
) {
235 talloc_free(mem_ctx
);
236 (*error_string
) = "INTERNAL error: Cannot impersonate principal with just a keyblock. A password must be specified in the credentials";
239 /* No password available, try to use a keyblock instead */
241 krb5_keyblock keyblock
;
242 const struct samr_Password
*mach_pwd
;
243 mach_pwd
= cli_credentials_get_nt_hash(credentials
, mem_ctx
);
245 talloc_free(mem_ctx
);
246 (*error_string
) = "kinit_to_ccache: No password available for kinit\n";
247 krb5_get_init_creds_opt_free(smb_krb5_context
->krb5_context
, krb_options
);
248 smb_krb5_context_remove_event_ctx(smb_krb5_context
, previous_ev
, event_ctx
);
251 ret
= krb5_keyblock_init(smb_krb5_context
->krb5_context
,
252 ENCTYPE_ARCFOUR_HMAC
,
253 mach_pwd
->hash
, sizeof(mach_pwd
->hash
),
257 ret
= kerberos_kinit_keyblock_cc(smb_krb5_context
->krb5_context
, ccache
,
259 target_service
, krb_options
,
261 krb5_free_keyblock_contents(smb_krb5_context
->krb5_context
, &keyblock
);
265 smb_krb5_context_remove_event_ctx(smb_krb5_context
, previous_ev
, event_ctx
);
267 if (ret
== KRB5KRB_AP_ERR_SKEW
|| ret
== KRB5_KDCREP_SKEW
) {
268 /* Perhaps we have been given an invalid skew, so try again without it */
269 time_t t
= time(NULL
);
270 krb5_set_real_time(smb_krb5_context
->krb5_context
, t
, 0);
272 /* not a skew problem */
277 krb5_get_init_creds_opt_free(smb_krb5_context
->krb5_context
, krb_options
);
279 if (ret
== KRB5KRB_AP_ERR_SKEW
|| ret
== KRB5_KDCREP_SKEW
) {
280 (*error_string
) = talloc_asprintf(credentials
, "kinit for %s failed (%s)\n",
281 cli_credentials_get_principal(credentials
, mem_ctx
),
282 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
284 talloc_free(mem_ctx
);
288 /* cope with ticket being in the future due to clock skew */
289 if ((unsigned)kdc_time
> time(NULL
)) {
290 time_t t
= time(NULL
);
291 int time_offset
=(unsigned)kdc_time
-t
;
292 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
293 krb5_set_real_time(smb_krb5_context
->krb5_context
, t
+ time_offset
+ 1, 0);
296 if (ret
== KRB5KDC_ERR_PREAUTH_FAILED
&& cli_credentials_wrong_password(credentials
)) {
297 ret
= kinit_to_ccache(parent_ctx
,
306 (*error_string
) = talloc_asprintf(credentials
, "kinit for %s failed (%s)\n",
307 cli_credentials_get_principal(credentials
, mem_ctx
),
308 smb_get_krb5_error_message(smb_krb5_context
->krb5_context
,
310 talloc_free(mem_ctx
);
313 talloc_free(mem_ctx
);
317 static krb5_error_code
free_keytab_container(struct keytab_container
*ktc
)
319 return krb5_kt_close(ktc
->smb_krb5_context
->krb5_context
, ktc
->keytab
);
322 krb5_error_code
smb_krb5_get_keytab_container(TALLOC_CTX
*mem_ctx
,
323 struct smb_krb5_context
*smb_krb5_context
,
324 const char *keytab_name
,
325 struct keytab_container
**ktc
)
329 ret
= krb5_kt_resolve(smb_krb5_context
->krb5_context
,
330 keytab_name
, &keytab
);
332 DEBUG(1,("failed to open krb5 keytab: %s\n",
333 smb_get_krb5_error_message(
334 smb_krb5_context
->krb5_context
,
339 *ktc
= talloc(mem_ctx
, struct keytab_container
);
344 (*ktc
)->smb_krb5_context
= talloc_reference(*ktc
, smb_krb5_context
);
345 (*ktc
)->keytab
= keytab
;
346 talloc_set_destructor(*ktc
, free_keytab_container
);