r5037: got rid of all of the TALLOC_DEPRECATED stuff. My apologies for the
[jelmer/samba4-debian.git] / source / libcli / auth / clikrb5.c
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-2003
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23 #include "system/network.h"
24 #include "system/kerberos.h"
25 #include "libcli/auth/kerberos.h"
26 #include "system/time.h"
27
28 #ifdef HAVE_KRB5
29
30 #ifndef HAVE_KRB5_SET_REAL_TIME
31 /*
32  * This function is not in the Heimdal mainline.
33  */
34  krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
35 {
36         krb5_error_code ret;
37         int32_t sec, usec;
38
39         ret = krb5_us_timeofday(context, &sec, &usec);
40         if (ret)
41                 return ret;
42
43         context->kdc_sec_offset = seconds - sec;
44         context->kdc_usec_offset = microseconds - usec;
45
46         return 0;
47 }
48 #endif
49
50 #if defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES) && !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
51  krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
52 {
53         return krb5_set_default_in_tkt_etypes(ctx, enc);
54 }
55 #endif
56
57 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
58 /* HEIMDAL */
59  void setup_kaddr( krb5_address *pkaddr, struct sockaddr *paddr)
60 {
61         pkaddr->addr_type = KRB5_ADDRESS_INET;
62         pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
63         pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
64 }
65 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
66 /* MIT */
67  void setup_kaddr( krb5_address *pkaddr, struct sockaddr *paddr)
68 {
69         pkaddr->addrtype = ADDRTYPE_INET;
70         pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
71         pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
72 }
73 #else
74 #error UNKNOWN_ADDRTYPE
75 #endif
76
77 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
78  int create_kerberos_key_from_string_direct(krb5_context context,
79                                         krb5_principal host_princ,
80                                         krb5_data *password,
81                                         krb5_keyblock *key,
82                                         krb5_enctype enctype)
83 {
84         int ret;
85         krb5_data salt;
86         krb5_encrypt_block eblock;
87
88         ret = krb5_principal2salt(context, host_princ, &salt);
89         if (ret) {
90                 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
91                 return ret;
92         }
93         krb5_use_enctype(context, &eblock, enctype);
94         ret = krb5_string_to_key(context, &eblock, key, password, &salt);
95         SAFE_FREE(salt.data);
96         return ret;
97 }
98 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
99  int create_kerberos_key_from_string_direct(krb5_context context,
100                                         krb5_principal host_princ,
101                                         krb5_data *password,
102                                         krb5_keyblock *key,
103                                         krb5_enctype enctype)
104 {
105         int ret;
106         krb5_salt salt;
107
108         ret = krb5_get_pw_salt(context, host_princ, &salt);
109         if (ret) {
110                 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
111                 return ret;
112         }
113         return krb5_string_to_key_salt(context, enctype, password->data,
114                 salt, key);
115 }
116 #else
117 #error UNKNOWN_CREATE_KEY_FUNCTIONS
118 #endif
119
120  int create_kerberos_key_from_string(krb5_context context,
121                                         krb5_principal host_princ,
122                                         krb5_data *password,
123                                         krb5_keyblock *key,
124                                         krb5_enctype enctype)
125 {
126         krb5_principal salt_princ = NULL;
127         int ret;
128         /*
129          * Check if we've determined that the KDC is salting keys for this
130          * principal/enctype in a non-obvious way.  If it is, try to match
131          * its behavior.
132          */
133         salt_princ = kerberos_fetch_salt_princ_for_host_princ(context, host_princ, enctype);
134         ret = create_kerberos_key_from_string_direct(context, salt_princ ? salt_princ : host_princ, password, key, enctype);
135         if (salt_princ) {
136                 krb5_free_principal(context, salt_princ);
137         }
138         return ret;
139 }
140
141 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
142  krb5_error_code get_kerberos_allowed_etypes(krb5_context context, 
143                                             krb5_enctype **enctypes)
144 {
145         return krb5_get_permitted_enctypes(context, enctypes);
146 }
147 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
148  krb5_error_code get_kerberos_allowed_etypes(krb5_context context, 
149                                             krb5_enctype **enctypes)
150 {
151         return krb5_get_default_in_tkt_etypes(context, enctypes);
152 }
153 #else
154 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
155 #endif
156
157  void free_kerberos_etypes(krb5_context context, 
158                            krb5_enctype *enctypes)
159 {
160 #if defined(HAVE_KRB5_FREE_KTYPES)
161         krb5_free_ktypes(context, enctypes);
162         return;
163 #else
164         SAFE_FREE(enctypes);
165         return;
166 #endif
167 }
168
169 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
170  krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
171                                         krb5_auth_context auth_context,
172                                         krb5_keyblock *keyblock)
173 {
174         return krb5_auth_con_setkey(context, auth_context, keyblock);
175 }
176 #endif
177
178  DATA_BLOB get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, 
179                             krb5_ticket *tkt)
180 {
181         DATA_BLOB auth_data = data_blob(NULL, 0); 
182 #if defined(HAVE_KRB5_TKT_ENC_PART2)
183         if (tkt && tkt->enc_part2 
184             && tkt->enc_part2->authorization_data
185             && tkt->enc_part2->authorization_data[0]
186             && tkt->enc_part2->authorization_data[0]->length)
187                 auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
188                         tkt->enc_part2->authorization_data[0]->length);
189 #else
190         if (tkt && tkt->ticket.authorization_data && tkt->ticket.authorization_data->len)
191                 auth_data = data_blob(tkt->ticket.authorization_data->val->ad_data.data,
192                         tkt->ticket.authorization_data->val->ad_data.length);
193 #endif
194         return auth_data;
195 }
196
197  krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
198 {
199 #if defined(HAVE_KRB5_TKT_ENC_PART2)
200         return tkt->enc_part2->client;
201 #else
202         return tkt->client;
203 #endif
204 }
205
206 #if !defined(HAVE_KRB5_LOCATE_KDC)
207  krb5_error_code krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, struct sockaddr **addr_pp, int *naddrs, int get_masters)
208 {
209         krb5_krbhst_handle hnd;
210         krb5_krbhst_info *hinfo;
211         krb5_error_code rc;
212         int num_kdcs, i;
213         struct sockaddr *sa;
214         struct addrinfo *ai;
215
216         *addr_pp = NULL;
217         *naddrs = 0;
218
219         rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
220         if (rc) {
221                 DEBUG(0, ("krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
222                 return rc;
223         }
224
225         for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
226                 ;
227
228         krb5_krbhst_reset(ctx, hnd);
229
230         if (!num_kdcs) {
231                 DEBUG(0, ("krb5_locate_kdc: zero kdcs found !\n"));
232                 krb5_krbhst_free(ctx, hnd);
233                 return -1;
234         }
235
236         sa = malloc_array_p(struct sockaddr, num_kdcs);
237         if (!sa) {
238                 DEBUG(0, ("krb5_locate_kdc: malloc failed\n"));
239                 krb5_krbhst_free(ctx, hnd);
240                 naddrs = 0;
241                 return -1;
242         }
243
244         memset(sa, '\0', sizeof(struct sockaddr) * num_kdcs );
245
246         for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
247
248 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
249                 rc = krb5_krbhst_get_addrinfo(ctx, hinfo, &ai);
250                 if (rc) {
251                         DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc)));
252                         continue;
253                 }
254 #endif
255                 if (hinfo->ai && hinfo->ai->ai_family == AF_INET) 
256                         memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
257         }
258
259         krb5_krbhst_free(ctx, hnd);
260
261         *naddrs = num_kdcs;
262         *addr_pp = sa;
263         return 0;
264 }
265 #endif
266
267 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
268  void krb5_free_unparsed_name(krb5_context context, char *val)
269 {
270         SAFE_FREE(val);
271 }
272 #endif
273
274  void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
275 {
276 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
277         if (pdata->data) {
278                 krb5_free_data_contents(context, pdata);
279         }
280 #else
281         SAFE_FREE(pdata->data);
282 #endif
283 }
284
285  void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype)
286 {
287 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
288         KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype;
289 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
290         KRB5_KEY_TYPE((&pcreds->session)) = enctype;
291 #else
292 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
293 #endif
294 }
295
296  BOOL kerberos_compatible_enctypes(krb5_context context,
297                                   krb5_enctype enctype1,
298                                   krb5_enctype enctype2)
299 {
300 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
301         krb5_boolean similar = 0;
302
303         krb5_c_enctype_compare(context, enctype1, enctype2, &similar);
304         return similar ? True : False;
305 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
306         return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False;
307 #endif
308 }
309
310 static BOOL ads_cleanup_expired_creds(krb5_context context, 
311                                       krb5_ccache  ccache,
312                                       krb5_creds  *credsp)
313 {
314         krb5_error_code retval;
315         TALLOC_CTX *mem_ctx = talloc_init("ticket expied time");
316         if (!mem_ctx) {
317                 return False;
318         }
319
320         DEBUG(3, ("Ticket in ccache[%s] expiration %s\n",
321                   krb5_cc_default_name(context),
322                   http_timestring(mem_ctx, credsp->times.endtime)));
323
324         talloc_free(mem_ctx);
325
326         /* we will probably need new tickets if the current ones
327            will expire within 10 seconds.
328         */
329         if (credsp->times.endtime >= (time(NULL) + 10))
330                 return False;
331
332         /* heimdal won't remove creds from a file ccache, and 
333            perhaps we shouldn't anyway, since internally we 
334            use memory ccaches, and a FILE one probably means that
335            we're using creds obtained outside of our exectuable
336         */
337         if (StrCaseCmp(krb5_cc_get_type(context, ccache), "FILE") == 0) {
338                 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a FILE ccache\n"));
339                 return False;
340         }
341         
342         retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
343         if (retval) {
344                 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
345                           error_message(retval)));
346                 /* If we have an error in this, we want to display it,
347                    but continue as though we deleted it */
348         }
349         return True;
350 }
351
352 /*
353   we can't use krb5_mk_req because w2k wants the service to be in a particular format
354 */
355 krb5_error_code ads_krb5_mk_req(krb5_context context, 
356                                 krb5_auth_context *auth_context, 
357                                 const krb5_flags ap_req_options,
358                                 const char *principal,
359                                 krb5_ccache ccache, 
360                                 krb5_data *outbuf)
361 {
362         krb5_error_code           retval;
363         krb5_principal    server;
364         krb5_creds              * credsp;
365         krb5_creds                creds;
366         krb5_data in_data;
367         BOOL creds_ready = False;
368         
369         TALLOC_CTX *mem_ctx = NULL;
370
371         retval = krb5_parse_name(context, principal, &server);
372         if (retval) {
373                 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
374                 return retval;
375         }
376         
377         /* obtain ticket & session key */
378         ZERO_STRUCT(creds);
379         if ((retval = krb5_copy_principal(context, server, &creds.server))) {
380                 DEBUG(1,("krb5_copy_principal failed (%s)\n", 
381                          error_message(retval)));
382                 goto cleanup_princ;
383         }
384         
385         if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
386                 /* This can commonly fail on smbd startup with no ticket in the cache.
387                  * Report at higher level than 1. */
388                 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n", 
389                          error_message(retval)));
390                 goto cleanup_creds;
391         }
392
393         while(!creds_ready) {
394                 if ((retval = krb5_get_credentials(context, 0, ccache, 
395                                                    &creds, &credsp))) {
396                         DEBUG(1,("ads_krb5_mk_req: krb5_get_credentials failed for %s (%s)\n",
397                                  principal, error_message(retval)));
398                         goto cleanup_creds;
399                 }
400
401                 /* cope with ticket being in the future due to clock skew */
402                 if ((unsigned)credsp->times.starttime > time(NULL)) {
403                         time_t t = time(NULL);
404                         int time_offset =(unsigned)credsp->times.starttime-t;
405                         DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
406                         krb5_set_real_time(context, t + time_offset + 1, 0);
407                 }
408
409                 if (!ads_cleanup_expired_creds(context, ccache, credsp))
410                         creds_ready = True;
411         }
412
413         mem_ctx = talloc_init("ticket expied time");
414         if (!mem_ctx) {
415                 retval = ENOMEM;
416                 goto cleanup_creds;
417         }
418         DEBUG(10,("Ticket (%s) in ccache (%s) is valid until: (%s - %d)\n",
419                   principal, krb5_cc_default_name(context),
420                   http_timestring(mem_ctx, (unsigned)credsp->times.endtime), 
421                   (unsigned)credsp->times.endtime));
422         
423         in_data.length = 0;
424         retval = krb5_mk_req_extended(context, auth_context, ap_req_options, 
425                                       &in_data, credsp, outbuf);
426         if (retval) {
427                 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n", 
428                          error_message(retval)));
429         }
430         
431         krb5_free_creds(context, credsp);
432
433 cleanup_creds:
434         krb5_free_cred_contents(context, &creds);
435
436 cleanup_princ:
437         krb5_free_principal(context, server);
438
439         return retval;
440 }
441
442 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
443  const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
444 {
445         static krb5_data kdata;
446
447         kdata.data = discard_const(krb5_principal_get_comp_string(context, principal, i));
448         kdata.length = strlen(kdata.data);
449         return &kdata;
450 }
451 #endif
452
453  krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
454 {
455 #if defined(HAVE_KRB5_KT_FREE_ENTRY)
456         return krb5_kt_free_entry(context, kt_entry);
457 #elif defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
458         return krb5_free_keytab_entry_contents(context, kt_entry);
459 #else
460 #error UNKNOWN_KT_FREE_FUNCTION
461 #endif
462 }
463
464 #endif