check a pointer before dereferencing it; not sure why userdata == NULL though
[Samba.git] / source / libsmb / clikrb5.c
blobbef6998a493ebdcd35427eb2cf75697bb6282177
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 krb5_free_ktypes(context, enctypes);
135 return;
136 #else
137 SAFE_FREE(enctypes);
138 return;
139 #endif
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);
149 #endif
151 void get_auth_data_from_tkt(DATA_BLOB *auth_data, krb5_ticket *tkt)
153 #if defined(HAVE_KRB5_TKT_ENC_PART2)
154 if (tkt->enc_part2)
155 *auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
156 tkt->enc_part2->authorization_data[0]->length);
157 #else
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);
161 #endif
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;
168 #else
169 return tkt->client;
170 #endif
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;
178 krb5_error_code rc;
179 int num_kdcs, i;
180 struct sockaddr *sa;
182 *addr_pp = NULL;
183 *naddrs = 0;
185 rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
186 if (rc) {
187 DEBUG(0, ("krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
188 return rc;
191 for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
194 krb5_krbhst_reset(ctx, hnd);
196 if (!num_kdcs) {
197 DEBUG(0, ("krb5_locate_kdc: zero kdcs found !\n"));
198 krb5_krbhst_free(ctx, hnd);
199 return -1;
202 sa = malloc( sizeof(struct sockaddr) * num_kdcs );
203 if (!sa) {
204 DEBUG(0, ("krb5_locate_kdc: malloc failed\n"));
205 krb5_krbhst_free(ctx, hnd);
206 naddrs = 0;
207 return -1;
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);
219 *naddrs = num_kdcs;
220 *addr_pp = sa;
221 return 0;
223 #endif
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,
232 krb5_ccache ccache,
233 krb5_data *outbuf)
235 krb5_error_code retval;
236 krb5_principal server;
237 krb5_creds * credsp;
238 krb5_creds creds;
239 krb5_data in_data;
241 retval = krb5_parse_name(context, principal, &server);
242 if (retval) {
243 DEBUG(1,("Failed to parse principal %s\n", principal));
244 return retval;
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)));
252 goto cleanup_princ;
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)));
258 goto cleanup_creds;
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)));
265 goto cleanup_creds;
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);
276 in_data.length = 0;
277 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
278 &in_data, credsp, outbuf);
279 if (retval) {
280 DEBUG(1,("krb5_mk_req_extended failed (%s)\n",
281 error_message(retval)));
284 krb5_free_creds(context, credsp);
286 cleanup_creds:
287 krb5_free_cred_contents(context, &creds);
289 cleanup_princ:
290 krb5_free_principal(context, server);
292 return retval;
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;
301 krb5_data packet;
302 krb5_ccache ccdef;
303 krb5_context context;
304 krb5_auth_context auth_context = NULL;
305 DATA_BLOB ret;
306 krb5_enctype enc_types[] = {
307 #ifdef ENCTYPE_ARCFOUR_HMAC
308 ENCTYPE_ARCFOUR_HMAC,
309 #endif
310 ENCTYPE_DES_CBC_MD5,
311 ENCTYPE_DES_CBC_CRC,
312 ENCTYPE_NULL};
314 retval = krb5_init_context(&context);
315 if (retval) {
316 DEBUG(1,("krb5_init_context failed (%s)\n",
317 error_message(retval)));
318 goto failed;
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)));
328 goto failed;
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)));
334 goto failed;
337 if ((retval = krb5_mk_req2(context,
338 &auth_context,
340 principal,
341 ccdef, &packet))) {
342 goto failed;
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);
349 return ret;
351 failed:
352 if ( 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);
365 #endif