2 Unix SMB/CIFS implementation.
3 simple kerberos5 routines for active directory
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Luke Howard 2002-2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE
27 #define KRB5_KEY_TYPE(k) ((k)->keytype)
28 #define KRB5_KEY_LENGTH(k) ((k)->keyvalue.length)
29 #define KRB5_KEY_DATA(k) ((k)->keyvalue.data)
31 #define KRB5_KEY_TYPE(k) ((k)->enctype)
32 #define KRB5_KEY_LENGTH(k) ((k)->length)
33 #define KRB5_KEY_DATA(k) ((k)->contents)
34 #endif /* HAVE_KRB5_KEYBLOCK_KEYVALUE */
36 #ifndef HAVE_KRB5_SET_REAL_TIME
38 * This function is not in the Heimdal mainline.
40 krb5_error_code
krb5_set_real_time(krb5_context context
, int32_t seconds
, int32_t microseconds
)
45 ret
= krb5_us_timeofday(context
, &sec
, &usec
);
49 context
->kdc_sec_offset
= seconds
- sec
;
50 context
->kdc_usec_offset
= microseconds
- usec
;
56 #if defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES) && !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
57 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
59 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
63 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
65 void setup_kaddr( krb5_address
*pkaddr
, struct sockaddr
*paddr
)
67 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
68 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
69 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
71 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
73 void setup_kaddr( krb5_address
*pkaddr
, struct sockaddr
*paddr
)
75 pkaddr
->addrtype
= ADDRTYPE_INET
;
76 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
77 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
80 __ERROR__XX__UNKNOWN_ADDRTYPE
83 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY)
84 int create_kerberos_key_from_string(krb5_context context
,
85 krb5_principal host_princ
,
92 krb5_encrypt_block eblock
;
94 ret
= krb5_principal2salt(context
, host_princ
, &salt
);
96 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret
)));
99 krb5_use_enctype(context
, &eblock
, enctype
);
100 ret
= krb5_string_to_key(context
, &eblock
, key
, password
, &salt
);
101 SAFE_FREE(salt
.data
);
104 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
105 int create_kerberos_key_from_string(krb5_context context
,
106 krb5_principal host_princ
,
109 krb5_enctype enctype
)
114 ret
= krb5_get_pw_salt(context
, host_princ
, &salt
);
116 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret
)));
119 return krb5_string_to_key_salt(context
, enctype
, password
->data
,
123 __ERROR_XX_UNKNOWN_CREATE_KEY_FUNCTIONS
126 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
127 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
128 krb5_enctype
**enctypes
)
130 return krb5_get_permitted_enctypes(context
, enctypes
);
132 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
133 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
134 krb5_enctype
**enctypes
)
136 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
139 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
142 void free_kerberos_etypes(krb5_context context
,
143 krb5_enctype
*enctypes
)
145 #if defined(HAVE_KRB5_FREE_KTYPES)
146 krb5_free_ktypes(context
, enctypes
);
154 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
155 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
156 krb5_auth_context auth_context
,
157 krb5_keyblock
*keyblock
)
159 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
163 void get_auth_data_from_tkt(DATA_BLOB
*auth_data
, krb5_ticket
*tkt
)
165 #if defined(HAVE_KRB5_TKT_ENC_PART2)
167 *auth_data
= data_blob(tkt
->enc_part2
->authorization_data
[0]->contents
,
168 tkt
->enc_part2
->authorization_data
[0]->length
);
170 if (tkt
->ticket
.authorization_data
&& tkt
->ticket
.authorization_data
->len
)
171 *auth_data
= data_blob(tkt
->ticket
.authorization_data
->val
->ad_data
.data
,
172 tkt
->ticket
.authorization_data
->val
->ad_data
.length
);
176 krb5_const_principal
get_principal_from_tkt(krb5_ticket
*tkt
)
178 #if defined(HAVE_KRB5_TKT_ENC_PART2)
179 return tkt
->enc_part2
->client
;
185 #if !defined(HAVE_KRB5_LOCATE_KDC)
186 krb5_error_code
krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
, struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
188 krb5_krbhst_handle hnd
;
189 krb5_krbhst_info
*hinfo
;
197 rc
= krb5_krbhst_init(ctx
, realm
->data
, KRB5_KRBHST_KDC
, &hnd
);
199 DEBUG(0, ("krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc
)));
203 for ( num_kdcs
= 0; (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); num_kdcs
++)
206 krb5_krbhst_reset(ctx
, hnd
);
209 DEBUG(0, ("krb5_locate_kdc: zero kdcs found !\n"));
210 krb5_krbhst_free(ctx
, hnd
);
214 sa
= malloc( sizeof(struct sockaddr
) * num_kdcs
);
216 DEBUG(0, ("krb5_locate_kdc: malloc failed\n"));
217 krb5_krbhst_free(ctx
, hnd
);
222 memset(*addr_pp
, '\0', sizeof(struct sockaddr
) * num_kdcs
);
224 for (i
= 0; i
< num_kdcs
&& (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); i
++) {
225 if (hinfo
->ai
->ai_family
== AF_INET
)
226 memcpy(&sa
[i
], hinfo
->ai
->ai_addr
, sizeof(struct sockaddr
));
229 krb5_krbhst_free(ctx
, hnd
);
237 static BOOL
ads_cleanup_expired_creds(krb5_context context
,
241 krb5_error_code retval
;
243 DEBUG(3, ("Ticket in ccache[%s] expiration %s\n",
244 krb5_cc_default_name(context
),
245 http_timestring(credsp
->times
.endtime
)));
247 /* we will probably need new tickets if the current ones
248 will expire within 10 seconds.
250 if (credsp
->times
.endtime
>= (time(NULL
) + 10))
253 /* heimdal won't remove creds from a file ccache, and
254 perhaps we shouldn't anyway, since internally we
255 use memory ccaches, and a FILE one probably means that
256 we're using creds obtained outside of our exectuable
258 if (StrCaseCmp(krb5_cc_get_type(context
, ccache
), "FILE") == 0) {
259 DEBUG(5, ("We do not remove creds from a FILE ccache\n"));
263 retval
= krb5_cc_remove_cred(context
, ccache
, 0, credsp
);
265 DEBUG(1, ("krb5_cc_remove_cred failed, err %s\n",
266 error_message(retval
)));
267 /* If we have an error in this, we want to display it,
268 but continue as though we deleted it */
274 we can't use krb5_mk_req because w2k wants the service to be in a particular format
276 static krb5_error_code
ads_krb5_mk_req(krb5_context context
,
277 krb5_auth_context
*auth_context
,
278 const krb5_flags ap_req_options
,
279 const char *principal
,
283 krb5_error_code retval
;
284 krb5_principal server
;
288 BOOL creds_ready
= False
;
290 retval
= krb5_parse_name(context
, principal
, &server
);
292 DEBUG(1,("Failed to parse principal %s\n", principal
));
296 /* obtain ticket & session key */
298 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
299 DEBUG(1,("krb5_copy_principal failed (%s)\n",
300 error_message(retval
)));
304 if ((retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
))) {
305 DEBUG(1,("krb5_cc_get_principal failed (%s)\n",
306 error_message(retval
)));
310 while(!creds_ready
) {
311 if ((retval
= krb5_get_credentials(context
, 0, ccache
,
313 DEBUG(1,("krb5_get_credentials failed for %s (%s)\n",
314 principal
, error_message(retval
)));
318 /* cope with ticket being in the future due to clock skew */
319 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
320 time_t t
= time(NULL
);
321 int time_offset
=(unsigned)credsp
->times
.starttime
-t
;
322 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
323 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
326 if (!ads_cleanup_expired_creds(context
, ccache
, credsp
))
330 DEBUG(10,("Ticket (%s) in ccache (%s) is valid until: (%s - %d)\n",
331 principal
, krb5_cc_default_name(context
),
332 http_timestring((unsigned)credsp
->times
.endtime
),
333 (unsigned)credsp
->times
.endtime
));
336 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
337 &in_data
, credsp
, outbuf
);
339 DEBUG(1,("krb5_mk_req_extended failed (%s)\n",
340 error_message(retval
)));
343 krb5_free_creds(context
, credsp
);
346 krb5_free_cred_contents(context
, &creds
);
349 krb5_free_principal(context
, server
);
355 get a kerberos5 ticket for the given service
357 int cli_krb5_get_ticket(const char *principal
, time_t time_offset
,
358 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
)
360 krb5_error_code retval
;
362 krb5_context context
= NULL
;
363 krb5_ccache ccdef
= NULL
;
364 krb5_auth_context auth_context
= NULL
;
365 krb5_enctype enc_types
[] = {
366 #ifdef ENCTYPE_ARCFOUR_HMAC
367 ENCTYPE_ARCFOUR_HMAC
,
373 retval
= krb5_init_context(&context
);
375 DEBUG(1,("krb5_init_context failed (%s)\n",
376 error_message(retval
)));
380 if (time_offset
!= 0) {
381 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
384 if ((retval
= krb5_cc_default(context
, &ccdef
))) {
385 DEBUG(1,("krb5_cc_default failed (%s)\n",
386 error_message(retval
)));
390 if ((retval
= krb5_set_default_tgs_ktypes(context
, enc_types
))) {
391 DEBUG(1,("krb5_set_default_tgs_ktypes failed (%s)\n",
392 error_message(retval
)));
396 if ((retval
= ads_krb5_mk_req(context
,
404 get_krb5_smb_session_key(context
, auth_context
, session_key_krb5
, False
);
406 *ticket
= data_blob(packet
.data
, packet
.length
);
408 /* Hmm, heimdal dooesn't have this - what's the correct call? */
409 #ifdef HAVE_KRB5_FREE_DATA_CONTENTS
410 krb5_free_data_contents(context
, &packet
);
416 #if 0 /* JERRY -- disabled since it causes heimdal 0.6.1rc3 to die
419 krb5_cc_close(context
, ccdef
);
422 krb5_auth_con_free(context
, auth_context
);
423 krb5_free_context(context
);
429 BOOL
get_krb5_smb_session_key(krb5_context context
, krb5_auth_context auth_context
, DATA_BLOB
*session_key
, BOOL remote
)
435 memset(session_key
, 0, 16);
438 err
= krb5_auth_con_getremotesubkey(context
, auth_context
, &skey
);
440 err
= krb5_auth_con_getlocalsubkey(context
, auth_context
, &skey
);
441 if (err
== 0 && skey
!= NULL
) {
442 DEBUG(10, ("Got KRB5 session key of length %d\n", KRB5_KEY_LENGTH(skey
)));
443 *session_key
= data_blob(KRB5_KEY_DATA(skey
), KRB5_KEY_LENGTH(skey
));
444 dump_data_pw("KRB5 Session Key:\n", session_key
->data
, session_key
->length
);
448 krb5_free_keyblock(context
, skey
);
450 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
457 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
458 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
)
460 static krb5_data kdata
;
462 kdata
.data
= krb5_principal_get_comp_string(context
, principal
, i
);
463 kdata
.length
= strlen(kdata
.data
);
468 #else /* HAVE_KRB5 */
469 /* this saves a few linking headaches */
470 int cli_krb5_get_ticket(const char *principal
, time_t time_offset
,
471 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
)
473 DEBUG(0,("NO KERBEROS SUPPORT\n"));