7ad8dd7b6c5074ce25d29680fdb87daaecfdb2cf
[samba.git] / source4 / 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__XX__UNKNOWN_ADDRTYPE
75 #endif
76
77 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY)
78  int create_kerberos_key_from_string(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(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_XX_UNKNOWN_CREATE_KEY_FUNCTIONS
118 #endif
119
120 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
121  krb5_error_code get_kerberos_allowed_etypes(krb5_context context, 
122                                             krb5_enctype **enctypes)
123 {
124         return krb5_get_permitted_enctypes(context, enctypes);
125 }
126 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
127  krb5_error_code get_kerberos_allowed_etypes(krb5_context context, 
128                                             krb5_enctype **enctypes)
129 {
130         return krb5_get_default_in_tkt_etypes(context, enctypes);
131 }
132 #else
133 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
134 #endif
135
136  void free_kerberos_etypes(krb5_context context, 
137                            krb5_enctype *enctypes)
138 {
139 #if defined(HAVE_KRB5_FREE_KTYPES)
140         krb5_free_ktypes(context, enctypes);
141         return;
142 #else
143         SAFE_FREE(enctypes);
144         return;
145 #endif
146 }
147
148 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
149  krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
150                                         krb5_auth_context auth_context,
151                                         krb5_keyblock *keyblock)
152 {
153         return krb5_auth_con_setkey(context, auth_context, keyblock);
154 }
155 #endif
156
157  DATA_BLOB get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, 
158                             krb5_ticket *tkt)
159 {
160         DATA_BLOB auth_data = data_blob(NULL, 0); 
161 #if defined(HAVE_KRB5_TKT_ENC_PART2)
162         if (tkt && tkt->enc_part2 
163             && tkt->enc_part2->authorization_data
164             && tkt->enc_part2->authorization_data[0]
165             && tkt->enc_part2->authorization_data[0]->length)
166                 auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
167                         tkt->enc_part2->authorization_data[0]->length);
168 #else
169         if (tkt && tkt->ticket.authorization_data && tkt->ticket.authorization_data->len)
170                 auth_data = data_blob(tkt->ticket.authorization_data->val->ad_data.data,
171                         tkt->ticket.authorization_data->val->ad_data.length);
172 #endif
173         return auth_data;
174 }
175
176  krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
177 {
178 #if defined(HAVE_KRB5_TKT_ENC_PART2)
179         return tkt->enc_part2->client;
180 #else
181         return tkt->client;
182 #endif
183 }
184
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)
187 {
188         krb5_krbhst_handle hnd;
189         krb5_krbhst_info *hinfo;
190         krb5_error_code rc;
191         int num_kdcs, i;
192         struct sockaddr *sa;
193         struct addrinfo **ai;
194
195         *addr_pp = NULL;
196         *naddrs = 0;
197
198         rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
199         if (rc) {
200                 DEBUG(0, ("krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
201                 return rc;
202         }
203
204         for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
205                 ;
206
207         krb5_krbhst_reset(ctx, hnd);
208
209         if (!num_kdcs) {
210                 DEBUG(0, ("krb5_locate_kdc: zero kdcs found !\n"));
211                 krb5_krbhst_free(ctx, hnd);
212                 return -1;
213         }
214
215         sa = malloc_array_p(struct sockaddr, num_kdcs);
216         if (!sa) {
217                 DEBUG(0, ("krb5_locate_kdc: malloc failed\n"));
218                 krb5_krbhst_free(ctx, hnd);
219                 naddrs = 0;
220                 return -1;
221         }
222
223         *addr_pp = malloc_array_p(struct sockaddr, num_kdcs);
224         memset(*addr_pp, '\0', sizeof(struct sockaddr) * num_kdcs );
225
226         for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
227
228 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
229                 rc = krb5_krbhst_get_addrinfo(ctx, hinfo, ai);
230                 if (rc) {
231                         DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc)));
232                         return rc;
233                 }
234 #endif
235                 if (hinfo->ai && hinfo->ai->ai_family == AF_INET) 
236                         memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
237         }
238
239         krb5_krbhst_free(ctx, hnd);
240
241         *naddrs = num_kdcs;
242         *addr_pp = sa;
243         return 0;
244 }
245 #endif
246
247 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
248  void krb5_free_unparsed_name(krb5_context context, char *val)
249 {
250         SAFE_FREE(val);
251 }
252 #endif
253
254 static BOOL ads_cleanup_expired_creds(krb5_context context, 
255                                       krb5_ccache  ccache,
256                                       krb5_creds  *credsp)
257 {
258         krb5_error_code retval;
259         TALLOC_CTX *mem_ctx = talloc_init("ticket expied time");
260         if (!mem_ctx) {
261                 return False;
262         }
263
264         DEBUG(3, ("Ticket in ccache[%s] expiration %s\n",
265                   krb5_cc_default_name(context),
266                   http_timestring(mem_ctx, credsp->times.endtime)));
267
268         talloc_destroy(mem_ctx);
269
270         /* we will probably need new tickets if the current ones
271            will expire within 10 seconds.
272         */
273         if (credsp->times.endtime >= (time(NULL) + 10))
274                 return False;
275
276         /* heimdal won't remove creds from a file ccache, and 
277            perhaps we shouldn't anyway, since internally we 
278            use memory ccaches, and a FILE one probably means that
279            we're using creds obtained outside of our exectuable
280         */
281         if (StrCaseCmp(krb5_cc_get_type(context, ccache), "FILE") == 0) {
282                 DEBUG(5, ("We do not remove creds from a FILE ccache\n"));
283                 return False;
284         }
285         
286         retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
287         if (retval) {
288                 DEBUG(1, ("krb5_cc_remove_cred failed, err %s\n",
289                           error_message(retval)));
290                 /* If we have an error in this, we want to display it,
291                    but continue as though we deleted it */
292         }
293         return True;
294 }
295
296 /*
297   we can't use krb5_mk_req because w2k wants the service to be in a particular format
298 */
299  krb5_error_code ads_krb5_mk_req(krb5_context context, 
300                                 krb5_auth_context *auth_context, 
301                                 const krb5_flags ap_req_options,
302                                 const char *principal,
303                                 krb5_ccache ccache, 
304                                 krb5_data *outbuf)
305 {
306         krb5_error_code           retval;
307         krb5_principal    server;
308         krb5_creds              * credsp;
309         krb5_creds                creds;
310         krb5_data in_data;
311         BOOL creds_ready = False;
312         
313         TALLOC_CTX *mem_ctx = NULL;
314
315         retval = krb5_parse_name(context, principal, &server);
316         if (retval) {
317                 DEBUG(1,("Failed to parse principal %s\n", principal));
318                 return retval;
319         }
320         
321         /* obtain ticket & session key */
322         ZERO_STRUCT(creds);
323         if ((retval = krb5_copy_principal(context, server, &creds.server))) {
324                 DEBUG(1,("krb5_copy_principal failed (%s)\n", 
325                          error_message(retval)));
326                 goto cleanup_princ;
327         }
328         
329         if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
330                 DEBUG(10,("krb5_cc_get_principal failed (%s)\n", 
331                          error_message(retval)));
332                 goto cleanup_creds;
333         }
334
335         while(!creds_ready) {
336                 if ((retval = krb5_get_credentials(context, 0, ccache, 
337                                                    &creds, &credsp))) {
338                         DEBUG(1,("krb5_get_credentials failed for %s (%s)\n",
339                                  principal, error_message(retval)));
340                         goto cleanup_creds;
341                 }
342
343                 /* cope with ticket being in the future due to clock skew */
344                 if ((unsigned)credsp->times.starttime > time(NULL)) {
345                         time_t t = time(NULL);
346                         int time_offset =(unsigned)credsp->times.starttime-t;
347                         DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
348                         krb5_set_real_time(context, t + time_offset + 1, 0);
349                 }
350
351                 if (!ads_cleanup_expired_creds(context, ccache, credsp))
352                         creds_ready = True;
353         }
354
355         mem_ctx = talloc_init("ticket expied time");
356         if (!mem_ctx) {
357                 retval = ENOMEM;
358                 goto cleanup_creds;
359         }
360         DEBUG(10,("Ticket (%s) in ccache (%s) is valid until: (%s - %d)\n",
361                   principal, krb5_cc_default_name(context),
362                   http_timestring(mem_ctx, (unsigned)credsp->times.endtime), 
363                   (unsigned)credsp->times.endtime));
364         
365         in_data.length = 0;
366         retval = krb5_mk_req_extended(context, auth_context, ap_req_options, 
367                                       &in_data, credsp, outbuf);
368         if (retval) {
369                 DEBUG(1,("krb5_mk_req_extended failed (%s)\n", 
370                          error_message(retval)));
371         }
372         
373         krb5_free_creds(context, credsp);
374
375 cleanup_creds:
376         krb5_free_cred_contents(context, &creds);
377
378 cleanup_princ:
379         krb5_free_principal(context, server);
380
381         if (mem_ctx) {
382                 talloc_destroy(mem_ctx);
383         }
384         return retval;
385 }
386
387
388 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
389  const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
390 {
391         static krb5_data kdata;
392
393         kdata.data = discard_const(krb5_principal_get_comp_string(context, principal, i));
394         kdata.length = strlen(kdata.data);
395         return &kdata;
396 }
397 #endif
398
399  krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
400 {
401 #if defined(HAVE_KRB5_KT_FREE_ENTRY)
402         return krb5_kt_free_entry(context, kt_entry);
403 #elif defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
404         return krb5_free_keytab_entry_contents(context, kt_entry);
405 #else
406 #error UNKNOWN_KT_FREE_FUNCTION
407 #endif
408 }
409
410 #endif