2 Unix SMB/CIFS implementation.
3 simple kerberos5 routines for active directory
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Luke Howard 2002
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 #ifndef HAVE_KRB5_SET_REAL_TIME
28 * This function is not in the Heimdal mainline.
30 krb5_error_code
krb5_set_real_time(krb5_context context
, int32_t seconds
, int32_t microseconds
)
35 ret
= krb5_us_timeofday(context
, &sec
, &usec
);
39 context
->kdc_sec_offset
= seconds
- sec
;
40 context
->kdc_usec_offset
= microseconds
- usec
;
46 #if defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES) && !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
47 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
49 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
53 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
55 void setup_kaddr( krb5_address
*pkaddr
, struct sockaddr
*paddr
)
57 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
58 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
59 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
61 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
63 void setup_kaddr( krb5_address
*pkaddr
, struct sockaddr
*paddr
)
65 pkaddr
->addrtype
= ADDRTYPE_INET
;
66 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
67 pkaddr
->contents
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
70 __ERROR__XX__UNKNOWN_ADDRTYPE
73 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY)
74 int create_kerberos_key_from_string(krb5_context context
,
75 krb5_principal host_princ
,
82 krb5_encrypt_block eblock
;
84 ret
= krb5_principal2salt(context
, host_princ
, &salt
);
86 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret
)));
89 krb5_use_enctype(context
, &eblock
, enctype
);
90 return krb5_string_to_key(context
, &eblock
, key
, password
, &salt
);
92 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
93 int create_kerberos_key_from_string(krb5_context context
,
94 krb5_principal host_princ
,
102 ret
= krb5_get_pw_salt(context
, host_princ
, &salt
);
104 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret
)));
107 return krb5_string_to_key_salt(context
, enctype
, password
->data
,
111 __ERROR_XX_UNKNOWN_CREATE_KEY_FUNCTIONS
114 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
115 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
116 krb5_enctype
**enctypes
)
118 return krb5_get_permitted_enctypes(context
, enctypes
);
120 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
121 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
122 krb5_enctype
**enctypes
)
124 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
127 __ERROR_XX_UNKNOWN_GET_ENCTYPES_FUNCTIONS
130 void free_kerberos_etypes(krb5_context context
,
131 krb5_enctype
*enctypes
)
133 #if defined(HAVE_KRB5_FREE_KTYPES)
134 krb5_free_ktypes(context
, enctypes
);
142 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
143 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
144 krb5_auth_context auth_context
,
145 krb5_keyblock
*keyblock
)
147 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
151 void get_auth_data_from_tkt(DATA_BLOB
*auth_data
, krb5_ticket
*tkt
)
153 #if defined(HAVE_KRB5_TKT_ENC_PART2)
155 *auth_data
= data_blob(tkt
->enc_part2
->authorization_data
[0]->contents
,
156 tkt
->enc_part2
->authorization_data
[0]->length
);
158 if (tkt
->ticket
.authorization_data
&& tkt
->ticket
.authorization_data
->len
)
159 *auth_data
= data_blob(tkt
->ticket
.authorization_data
->val
->ad_data
.data
,
160 tkt
->ticket
.authorization_data
->val
->ad_data
.length
);
164 krb5_const_principal
get_principal_from_tkt(krb5_ticket
*tkt
)
166 #if defined(HAVE_KRB5_TKT_ENC_PART2)
167 return tkt
->enc_part2
->client
;
173 #if !defined(HAVE_KRB5_LOCATE_KDC)
174 krb5_error_code
krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
, struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
176 krb5_krbhst_handle hnd
;
177 krb5_krbhst_info
*hinfo
;
185 rc
= krb5_krbhst_init(ctx
, realm
->data
, KRB5_KRBHST_KDC
, &hnd
);
187 DEBUG(0, ("krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc
)));
191 for ( num_kdcs
= 0; (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); num_kdcs
++)
194 krb5_krbhst_reset(ctx
, hnd
);
197 DEBUG(0, ("krb5_locate_kdc: zero kdcs found !\n"));
198 krb5_krbhst_free(ctx
, hnd
);
202 sa
= malloc( sizeof(struct sockaddr
) * num_kdcs
);
204 DEBUG(0, ("krb5_locate_kdc: malloc failed\n"));
205 krb5_krbhst_free(ctx
, hnd
);
210 memset(*addr_pp
, '\0', sizeof(struct sockaddr
) * num_kdcs
);
212 for (i
= 0; i
< num_kdcs
&& (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); i
++) {
213 if (hinfo
->ai
->ai_family
== AF_INET
)
214 memcpy(&sa
[i
], hinfo
->ai
->ai_addr
, sizeof(struct sockaddr
));
217 krb5_krbhst_free(ctx
, hnd
);
226 we can't use krb5_mk_req because w2k wants the service to be in a particular format
228 static krb5_error_code
krb5_mk_req2(krb5_context context
,
229 krb5_auth_context
*auth_context
,
230 const krb5_flags ap_req_options
,
231 const char *principal
,
235 krb5_error_code retval
;
236 krb5_principal server
;
241 retval
= krb5_parse_name(context
, principal
, &server
);
243 DEBUG(1,("Failed to parse principal %s\n", principal
));
247 /* obtain ticket & session key */
248 memset((char *)&creds
, 0, sizeof(creds
));
249 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
250 DEBUG(1,("krb5_copy_principal failed (%s)\n",
251 error_message(retval
)));
255 if ((retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
))) {
256 DEBUG(1,("krb5_cc_get_principal failed (%s)\n",
257 error_message(retval
)));
261 if ((retval
= krb5_get_credentials(context
, 0,
262 ccache
, &creds
, &credsp
))) {
263 DEBUG(1,("krb5_get_credentials failed for %s (%s)\n",
264 principal
, error_message(retval
)));
268 /* cope with the ticket being in the future due to clock skew */
269 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
270 time_t t
= time(NULL
);
271 int time_offset
= (unsigned)credsp
->times
.starttime
- t
;
272 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
273 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
277 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
278 &in_data
, credsp
, outbuf
);
280 DEBUG(1,("krb5_mk_req_extended failed (%s)\n",
281 error_message(retval
)));
284 krb5_free_creds(context
, credsp
);
287 krb5_free_cred_contents(context
, &creds
);
290 krb5_free_principal(context
, server
);
296 get a kerberos5 ticket for the given service
298 DATA_BLOB
krb5_get_ticket(const char *principal
, time_t time_offset
)
300 krb5_error_code retval
;
303 krb5_context context
;
304 krb5_auth_context auth_context
= NULL
;
306 krb5_enctype enc_types
[] = {
307 #ifdef ENCTYPE_ARCFOUR_HMAC
308 ENCTYPE_ARCFOUR_HMAC
,
314 retval
= krb5_init_context(&context
);
316 DEBUG(1,("krb5_init_context failed (%s)\n",
317 error_message(retval
)));
321 if (time_offset
!= 0) {
322 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
325 if ((retval
= krb5_cc_default(context
, &ccdef
))) {
326 DEBUG(1,("krb5_cc_default failed (%s)\n",
327 error_message(retval
)));
331 if ((retval
= krb5_set_default_tgs_ktypes(context
, enc_types
))) {
332 DEBUG(1,("krb5_set_default_tgs_ktypes failed (%s)\n",
333 error_message(retval
)));
337 if ((retval
= krb5_mk_req2(context
,
345 ret
= data_blob(packet
.data
, packet
.length
);
346 /* Hmm, heimdal dooesn't have this - what's the correct call? */
347 /* krb5_free_data_contents(context, &packet); */
348 krb5_free_context(context
);
353 krb5_free_context(context
);
355 return data_blob(NULL
, 0);
358 #else /* HAVE_KRB5 */
359 /* this saves a few linking headaches */
360 DATA_BLOB
krb5_get_ticket(const char *principal
, time_t time_offset
)
362 DEBUG(0,("NO KERBEROS SUPPORT\n"));
363 return data_blob(NULL
, 0);