Sync with HEAD for verifying kerberos tickets.
[Samba/gebeck_regimport.git] / source / libsmb / clikrb5.c
blob96e737166ca347a5ee7c444b115db7ca17cb479a
1 /*
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.
22 #include "includes.h"
24 #ifdef HAVE_KRB5
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)
32 krb5_error_code ret;
33 int32_t sec, usec;
35 ret = krb5_us_timeofday(context, &sec, &usec);
36 if (ret)
37 return ret;
39 context->kdc_sec_offset = seconds - sec;
40 context->kdc_usec_offset = microseconds - usec;
42 return 0;
44 #endif
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);
51 #endif
53 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
54 /* HEIMDAL */
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)
62 /* MIT */
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);
69 #else
70 __ERROR__XX__UNKNOWN_ADDRTYPE
71 #endif
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,
76 krb5_data *password,
77 krb5_keyblock *key,
78 krb5_enctype enctype)
80 int ret;
81 krb5_data salt;
82 krb5_encrypt_block eblock;
84 ret = krb5_principal2salt(context, host_princ, &salt);
85 if (ret) {
86 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
87 return 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,
95 krb5_data *password,
96 krb5_keyblock *key,
97 krb5_enctype enctype)
99 int ret;
100 krb5_salt salt;
102 ret = krb5_get_pw_salt(context, host_princ, &salt);
103 if (ret) {
104 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
105 return ret;
107 return krb5_string_to_key_salt(context, enctype, password->data,
108 salt, key);
110 #else
111 __ERROR_XX_UNKNOWN_CREATE_KEY_FUNCTIONS
112 #endif
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);
126 #else
127 __ERROR_XX_UNKNOWN_GET_ENCTYPES_FUNCTIONS
128 #endif
130 void free_kerberos_etypes(krb5_context context,
131 krb5_enctype *enctypes)
133 #if defined(HAVE_KRB5_FREE_KTYPES)
134 return krb5_free_ktypes(context, enctypes);
135 #else
136 SAFE_FREE(enctypes);
137 return;
138 #endif
141 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
142 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
143 krb5_auth_context auth_context,
144 krb5_keyblock *keyblock)
146 return krb5_auth_con_setkey(context, auth_context, keyblock);
148 #endif
150 void get_auth_data_from_tkt(DATA_BLOB *auth_data, krb5_ticket *tkt)
152 #if defined(HAVE_KRB5_TKT_ENC_PART2)
153 if (tkt->enc_part2)
154 *auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
155 tkt->enc_part2->authorization_data[0]->length);
156 #else
157 if (tkt->ticket.authorization_data && tkt->ticket.authorization_data->len)
158 *auth_data = data_blob(tkt->ticket.authorization_data->val->ad_data.data,
159 tkt->ticket.authorization_data->val->ad_data.length);
160 #endif
163 krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
165 #if defined(HAVE_KRB5_TKT_ENC_PART2)
166 return tkt->enc_part2->client;
167 #else
168 return tkt->client;
169 #endif
172 #if !defined(HAVE_KRB5_LOCATE_KDC)
173 krb5_error_code krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, struct sockaddr **addr_pp, int *naddrs, int get_masters)
175 krb5_krbhst_handle hnd;
176 krb5_krbhst_info *hinfo;
177 krb5_error_code rc;
178 int num_kdcs, i;
179 struct sockaddr *sa;
181 *addr_pp = NULL;
182 *naddrs = 0;
184 rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
185 if (rc) {
186 DEBUG(0, ("krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
187 return rc;
190 for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
193 krb5_krbhst_reset(ctx, hnd);
195 if (!num_kdcs) {
196 DEBUG(0, ("krb5_locate_kdc: zero kdcs found !\n"));
197 krb5_krbhst_free(ctx, hnd);
198 return -1;
201 sa = malloc( sizeof(struct sockaddr) * num_kdcs );
202 if (!sa) {
203 DEBUG(0, ("krb5_locate_kdc: malloc failed\n"));
204 krb5_krbhst_free(ctx, hnd);
205 naddrs = 0;
206 return -1;
209 memset(*addr_pp, '\0', sizeof(struct sockaddr) * num_kdcs );
211 for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
212 if (hinfo->ai->ai_family == AF_INET)
213 memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
216 krb5_krbhst_free(ctx, hnd);
218 *naddrs = num_kdcs;
219 *addr_pp = sa;
220 return 0;
222 #endif
225 we can't use krb5_mk_req because w2k wants the service to be in a particular format
227 static krb5_error_code krb5_mk_req2(krb5_context context,
228 krb5_auth_context *auth_context,
229 const krb5_flags ap_req_options,
230 const char *principal,
231 krb5_ccache ccache,
232 krb5_data *outbuf)
234 krb5_error_code retval;
235 krb5_principal server;
236 krb5_creds * credsp;
237 krb5_creds creds;
238 krb5_data in_data;
240 retval = krb5_parse_name(context, principal, &server);
241 if (retval) {
242 DEBUG(1,("Failed to parse principal %s\n", principal));
243 return retval;
246 /* obtain ticket & session key */
247 memset((char *)&creds, 0, sizeof(creds));
248 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
249 DEBUG(1,("krb5_copy_principal failed (%s)\n",
250 error_message(retval)));
251 goto cleanup_princ;
254 if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
255 DEBUG(1,("krb5_cc_get_principal failed (%s)\n",
256 error_message(retval)));
257 goto cleanup_creds;
260 if ((retval = krb5_get_credentials(context, 0,
261 ccache, &creds, &credsp))) {
262 DEBUG(1,("krb5_get_credentials failed for %s (%s)\n",
263 principal, error_message(retval)));
264 goto cleanup_creds;
267 /* cope with the ticket being in the future due to clock skew */
268 if ((unsigned)credsp->times.starttime > time(NULL)) {
269 time_t t = time(NULL);
270 int time_offset = (unsigned)credsp->times.starttime - t;
271 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
272 krb5_set_real_time(context, t + time_offset + 1, 0);
275 in_data.length = 0;
276 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
277 &in_data, credsp, outbuf);
278 if (retval) {
279 DEBUG(1,("krb5_mk_req_extended failed (%s)\n",
280 error_message(retval)));
283 krb5_free_creds(context, credsp);
285 cleanup_creds:
286 krb5_free_cred_contents(context, &creds);
288 cleanup_princ:
289 krb5_free_principal(context, server);
291 return retval;
295 get a kerberos5 ticket for the given service
297 DATA_BLOB krb5_get_ticket(const char *principal, time_t time_offset)
299 krb5_error_code retval;
300 krb5_data packet;
301 krb5_ccache ccdef;
302 krb5_context context;
303 krb5_auth_context auth_context = NULL;
304 DATA_BLOB ret;
305 krb5_enctype enc_types[] = {
306 #ifdef ENCTYPE_ARCFOUR_HMAC
307 ENCTYPE_ARCFOUR_HMAC,
308 #endif
309 ENCTYPE_DES_CBC_MD5,
310 ENCTYPE_DES_CBC_CRC,
311 ENCTYPE_NULL};
313 retval = krb5_init_context(&context);
314 if (retval) {
315 DEBUG(1,("krb5_init_context failed (%s)\n",
316 error_message(retval)));
317 goto failed;
320 if (time_offset != 0) {
321 krb5_set_real_time(context, time(NULL) + time_offset, 0);
324 if ((retval = krb5_cc_default(context, &ccdef))) {
325 DEBUG(1,("krb5_cc_default failed (%s)\n",
326 error_message(retval)));
327 goto failed;
330 if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
331 DEBUG(1,("krb5_set_default_tgs_ktypes failed (%s)\n",
332 error_message(retval)));
333 goto failed;
336 if ((retval = krb5_mk_req2(context,
337 &auth_context,
339 principal,
340 ccdef, &packet))) {
341 goto failed;
344 ret = data_blob(packet.data, packet.length);
345 /* Hmm, heimdal dooesn't have this - what's the correct call? */
346 /* krb5_free_data_contents(context, &packet); */
347 krb5_free_context(context);
348 return ret;
350 failed:
351 if ( context )
352 krb5_free_context(context);
354 return data_blob(NULL, 0);
357 #else /* HAVE_KRB5 */
358 /* this saves a few linking headaches */
359 DATA_BLOB krb5_get_ticket(const char *principal, time_t time_offset)
361 DEBUG(0,("NO KERBEROS SUPPORT\n"));
362 return data_blob(NULL, 0);
364 #endif