import HEAD into svn+ssh://svn.samba.org/home/svn/samba/trunk
[metze/old/v3-2-winbind-ndr.git] / source / libsmb / 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
24 #ifdef HAVE_KRB5
25
26 #ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE
27 #define KRB5_KEY_TYPE(k)        ((k)->keytype)
28 #define KRB5_KEY_LENGTH(k)      ((k)->keyvalue.length)
29 #define KRB5_KEY_DATA(k)        ((k)->keyvalue.data)
30 #else
31 #define KRB5_KEY_TYPE(k)        ((k)->enctype)
32 #define KRB5_KEY_LENGTH(k)      ((k)->length)
33 #define KRB5_KEY_DATA(k)        ((k)->contents)
34 #endif /* HAVE_KRB5_KEYBLOCK_KEYVALUE */
35
36 #ifndef HAVE_KRB5_SET_REAL_TIME
37 /*
38  * This function is not in the Heimdal mainline.
39  */
40  krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
41 {
42         krb5_error_code ret;
43         int32_t sec, usec;
44
45         ret = krb5_us_timeofday(context, &sec, &usec);
46         if (ret)
47                 return ret;
48
49         context->kdc_sec_offset = seconds - sec;
50         context->kdc_usec_offset = microseconds - usec;
51
52         return 0;
53 }
54 #endif
55
56 #if defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES) && !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
57  krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
58 {
59         return krb5_set_default_in_tkt_etypes(ctx, enc);
60 }
61 #endif
62
63 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
64 /* HEIMDAL */
65  void setup_kaddr( krb5_address *pkaddr, struct sockaddr *paddr)
66 {
67         pkaddr->addr_type = KRB5_ADDRESS_INET;
68         pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
69         pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
70 }
71 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
72 /* MIT */
73  void setup_kaddr( krb5_address *pkaddr, struct sockaddr *paddr)
74 {
75         pkaddr->addrtype = ADDRTYPE_INET;
76         pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
77         pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
78 }
79 #else
80  __ERROR__XX__UNKNOWN_ADDRTYPE
81 #endif
82
83 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY)
84  int create_kerberos_key_from_string(krb5_context context,
85                                         krb5_principal host_princ,
86                                         krb5_data *password,
87                                         krb5_keyblock *key,
88                                         krb5_enctype enctype)
89 {
90         int ret;
91         krb5_data salt;
92         krb5_encrypt_block eblock;
93
94         ret = krb5_principal2salt(context, host_princ, &salt);
95         if (ret) {
96                 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
97                 return ret;
98         }
99         krb5_use_enctype(context, &eblock, enctype);
100         ret = krb5_string_to_key(context, &eblock, key, password, &salt);
101         SAFE_FREE(salt.data);
102         return ret;
103 }
104 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
105  int create_kerberos_key_from_string(krb5_context context,
106                                         krb5_principal host_princ,
107                                         krb5_data *password,
108                                         krb5_keyblock *key,
109                                         krb5_enctype enctype)
110 {
111         int ret;
112         krb5_salt salt;
113
114         ret = krb5_get_pw_salt(context, host_princ, &salt);
115         if (ret) {
116                 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
117                 return ret;
118         }
119         return krb5_string_to_key_salt(context, enctype, password->data,
120                 salt, key);
121 }
122 #else
123  __ERROR_XX_UNKNOWN_CREATE_KEY_FUNCTIONS
124 #endif
125
126 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
127 krb5_error_code get_kerberos_allowed_etypes(krb5_context context, 
128                                             krb5_enctype **enctypes)
129 {
130         return krb5_get_permitted_enctypes(context, enctypes);
131 }
132 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
133 krb5_error_code get_kerberos_allowed_etypes(krb5_context context, 
134                                             krb5_enctype **enctypes)
135 {
136         return krb5_get_default_in_tkt_etypes(context, enctypes);
137 }
138 #else
139 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
140 #endif
141
142  void free_kerberos_etypes(krb5_context context, 
143                            krb5_enctype *enctypes)
144 {
145 #if defined(HAVE_KRB5_FREE_KTYPES)
146         krb5_free_ktypes(context, enctypes);
147         return;
148 #else
149         SAFE_FREE(enctypes);
150         return;
151 #endif
152 }
153
154 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
155  krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
156                                         krb5_auth_context auth_context,
157                                         krb5_keyblock *keyblock)
158 {
159         return krb5_auth_con_setkey(context, auth_context, keyblock);
160 }
161 #endif
162
163  void get_auth_data_from_tkt(DATA_BLOB *auth_data, krb5_ticket *tkt)
164 {
165 #if defined(HAVE_KRB5_TKT_ENC_PART2)
166         if (tkt->enc_part2)
167                 *auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
168                         tkt->enc_part2->authorization_data[0]->length);
169 #else
170         if (tkt->ticket.authorization_data && tkt->ticket.authorization_data->len)
171                 *auth_data = data_blob(tkt->ticket.authorization_data->val->ad_data.data,
172                         tkt->ticket.authorization_data->val->ad_data.length);
173 #endif
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
194         *addr_pp = NULL;
195         *naddrs = 0;
196
197         rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
198         if (rc) {
199                 DEBUG(0, ("krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
200                 return rc;
201         }
202
203         for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
204                 ;
205
206         krb5_krbhst_reset(ctx, hnd);
207
208         if (!num_kdcs) {
209                 DEBUG(0, ("krb5_locate_kdc: zero kdcs found !\n"));
210                 krb5_krbhst_free(ctx, hnd);
211                 return -1;
212         }
213
214         sa = malloc( sizeof(struct sockaddr) * num_kdcs );
215         if (!sa) {
216                 DEBUG(0, ("krb5_locate_kdc: malloc failed\n"));
217                 krb5_krbhst_free(ctx, hnd);
218                 naddrs = 0;
219                 return -1;
220         }
221
222         memset(*addr_pp, '\0', sizeof(struct sockaddr) * num_kdcs );
223
224         for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
225                 if (hinfo->ai->ai_family == AF_INET)
226                         memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
227         }
228
229         krb5_krbhst_free(ctx, hnd);
230
231         *naddrs = num_kdcs;
232         *addr_pp = sa;
233         return 0;
234 }
235 #endif
236
237 /*
238   we can't use krb5_mk_req because w2k wants the service to be in a particular format
239 */
240 static krb5_error_code ads_krb5_mk_req(krb5_context context, 
241                                        krb5_auth_context *auth_context, 
242                                        const krb5_flags ap_req_options,
243                                        const char *principal,
244                                        krb5_ccache ccache, 
245                                        krb5_data *outbuf)
246 {
247         krb5_error_code           retval;
248         krb5_principal    server;
249         krb5_creds              * credsp;
250         krb5_creds                creds;
251         krb5_data in_data;
252         
253         retval = krb5_parse_name(context, principal, &server);
254         if (retval) {
255                 DEBUG(1,("Failed to parse principal %s\n", principal));
256                 return retval;
257         }
258         
259         /* obtain ticket & session key */
260         ZERO_STRUCT(creds);
261         if ((retval = krb5_copy_principal(context, server, &creds.server))) {
262                 DEBUG(1,("krb5_copy_principal failed (%s)\n", 
263                          error_message(retval)));
264                 goto cleanup_princ;
265         }
266         
267         if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
268                 DEBUG(1,("krb5_cc_get_principal failed (%s)\n", 
269                          error_message(retval)));
270                 goto cleanup_creds;
271         }
272
273         if ((retval = krb5_get_credentials(context, 0,
274                                            ccache, &creds, &credsp))) {
275                 DEBUG(1,("krb5_get_credentials failed for %s (%s)\n", 
276                          principal, error_message(retval)));
277                 goto cleanup_creds;
278         }
279
280         /* cope with the ticket being in the future due to clock skew */
281         if ((unsigned)credsp->times.starttime > time(NULL)) {
282                 time_t t = time(NULL);
283                 int time_offset = (unsigned)credsp->times.starttime - t;
284                 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
285                 krb5_set_real_time(context, t + time_offset + 1, 0);
286         }
287
288         in_data.length = 0;
289         retval = krb5_mk_req_extended(context, auth_context, ap_req_options, 
290                                       &in_data, credsp, outbuf);
291         if (retval) {
292                 DEBUG(1,("krb5_mk_req_extended failed (%s)\n", 
293                          error_message(retval)));
294         }
295         
296         krb5_free_creds(context, credsp);
297
298 cleanup_creds:
299         krb5_free_cred_contents(context, &creds);
300
301 cleanup_princ:
302         krb5_free_principal(context, server);
303
304         return retval;
305 }
306
307 /*
308   get a kerberos5 ticket for the given service 
309 */
310 int cli_krb5_get_ticket(const char *principal, time_t time_offset, 
311                         DATA_BLOB *ticket, DATA_BLOB *session_key_krb5)
312 {
313         krb5_error_code retval;
314         krb5_data packet;
315         krb5_ccache ccdef;
316         krb5_context context;
317         krb5_auth_context auth_context = NULL;
318         krb5_enctype enc_types[] = {
319 #ifdef ENCTYPE_ARCFOUR_HMAC
320                 ENCTYPE_ARCFOUR_HMAC,
321 #endif 
322                 ENCTYPE_DES_CBC_MD5, 
323                 ENCTYPE_DES_CBC_CRC, 
324                 ENCTYPE_NULL};
325         
326         retval = krb5_init_context(&context);
327         if (retval) {
328                 DEBUG(1,("krb5_init_context failed (%s)\n", 
329                          error_message(retval)));
330                 goto failed;
331         }
332
333         if (time_offset != 0) {
334                 krb5_set_real_time(context, time(NULL) + time_offset, 0);
335         }
336
337         if ((retval = krb5_cc_default(context, &ccdef))) {
338                 DEBUG(1,("krb5_cc_default failed (%s)\n",
339                          error_message(retval)));
340                 goto failed;
341         }
342
343         if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
344                 DEBUG(1,("krb5_set_default_tgs_ktypes failed (%s)\n",
345                          error_message(retval)));
346                 goto failed;
347         }
348
349         if ((retval = ads_krb5_mk_req(context, 
350                                         &auth_context, 
351                                         AP_OPTS_USE_SUBKEY, 
352                                         principal,
353                                         ccdef, &packet))) {
354                 goto failed;
355         }
356
357         get_krb5_smb_session_key(context, auth_context, session_key_krb5, False);
358
359         *ticket = data_blob(packet.data, packet.length);
360
361 /* Hmm, heimdal dooesn't have this - what's the correct call? */
362 #ifdef HAVE_KRB5_FREE_DATA_CONTENTS
363         krb5_free_data_contents(context, &packet); 
364 #endif
365
366 failed:
367         if ( context )
368                 krb5_free_context(context);
369                 
370         return retval;
371 }
372
373  BOOL get_krb5_smb_session_key(krb5_context context, krb5_auth_context auth_context, DATA_BLOB *session_key, BOOL remote)
374  {
375         krb5_keyblock *skey;
376         krb5_error_code err;
377         BOOL ret = False;
378
379         memset(session_key, 0, 16);
380
381         if (remote)
382                 err = krb5_auth_con_getremotesubkey(context, auth_context, &skey);
383         else
384                 err = krb5_auth_con_getlocalsubkey(context, auth_context, &skey);
385         if (err == 0 && skey != NULL) {
386                 DEBUG(10, ("Got KRB5 session key of length %d\n",  KRB5_KEY_LENGTH(skey)));
387                 *session_key = data_blob(KRB5_KEY_DATA(skey), KRB5_KEY_LENGTH(skey));
388                 dump_data_pw("KRB5 Session Key:\n", session_key->data, session_key->length);
389
390                 ret = True;
391
392                 krb5_free_keyblock(context, skey);
393         } else {
394                 DEBUG(10, ("KRB5 error getting session key %d\n", err));
395         }
396
397         return ret;
398  }
399
400
401 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
402  const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
403 {
404         static krb5_data kdata;
405
406         kdata.data = krb5_principal_get_comp_string(context, principal, i);
407         kdata.length = strlen(kdata.data);
408         return &kdata;
409 }
410 #endif
411
412 #else /* HAVE_KRB5 */
413  /* this saves a few linking headaches */
414 int cli_krb5_get_ticket(const char *principal, time_t time_offset, 
415                         DATA_BLOB *ticket, DATA_BLOB *session_key_krb5) 
416 {
417          DEBUG(0,("NO KERBEROS SUPPORT\n"));
418          return 1;
419 }
420
421 #endif