r19416: Fix some c++ warnings.
[ira/wip.git] / source3 / 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    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7    Copyright (C) Guenther Deschner 2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #define KRB5_PRIVATE    1       /* this file uses PRIVATE interfaces! */
25 #define KRB5_DEPRECATED 1       /* this file uses DEPRECATED interfaces! */
26
27 #include "includes.h"
28
29 #ifdef HAVE_KRB5
30
31 #ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE
32 #define KRB5_KEY_TYPE(k)        ((k)->keytype)
33 #define KRB5_KEY_LENGTH(k)      ((k)->keyvalue.length)
34 #define KRB5_KEY_DATA(k)        ((k)->keyvalue.data)
35 #else
36 #define KRB5_KEY_TYPE(k)        ((k)->enctype)
37 #define KRB5_KEY_LENGTH(k)      ((k)->length)
38 #define KRB5_KEY_DATA(k)        ((k)->contents)
39 #endif /* HAVE_KRB5_KEYBLOCK_KEYVALUE */
40
41 /**************************************************************
42  Wrappers around kerberos string functions that convert from
43  utf8 -> unix charset and vica versa.
44 **************************************************************/
45
46 /**************************************************************
47  krb5_parse_name that takes a UNIX charset.
48 **************************************************************/
49
50  krb5_error_code smb_krb5_parse_name(krb5_context context,
51                                 const char *name, /* in unix charset */
52                                 krb5_principal *principal)
53 {
54         krb5_error_code ret;
55         char *utf8_name;
56
57         if (push_utf8_allocate(&utf8_name, name) == (size_t)-1) {
58                 return ENOMEM;
59         }
60
61         ret = krb5_parse_name(context, utf8_name, principal);
62         SAFE_FREE(utf8_name);
63         return ret;
64 }
65
66 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
67 /**************************************************************
68  krb5_parse_name_norealm that takes a UNIX charset.
69 **************************************************************/
70
71 static krb5_error_code smb_krb5_parse_name_norealm_conv(krb5_context context,
72                                 const char *name, /* in unix charset */
73                                 krb5_principal *principal)
74 {
75         krb5_error_code ret;
76         char *utf8_name;
77
78         if (push_utf8_allocate(&utf8_name, name) == (size_t)-1) {
79                 return ENOMEM;
80         }
81
82         ret = krb5_parse_name_norealm(context, utf8_name, principal);
83         SAFE_FREE(utf8_name);
84         return ret;
85 }
86 #endif
87
88 /**************************************************************
89  krb5_parse_name that returns a UNIX charset name. Must
90  be freed with normal free() call.
91 **************************************************************/
92
93  krb5_error_code smb_krb5_unparse_name(krb5_context context,
94                                         krb5_const_principal principal,
95                                         char **unix_name)
96 {
97         krb5_error_code ret;
98         char *utf8_name;
99
100         ret = krb5_unparse_name(context, principal, &utf8_name);
101         if (ret) {
102                 return ret;
103         }
104
105         if (pull_utf8_allocate(unix_name, utf8_name)==-1) {
106                 krb5_free_unparsed_name(context, utf8_name);
107                 return ENOMEM;
108         }
109         krb5_free_unparsed_name(context, utf8_name);
110         return 0;
111 }
112
113 #ifndef HAVE_KRB5_SET_REAL_TIME
114 /*
115  * This function is not in the Heimdal mainline.
116  */
117  krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
118 {
119         krb5_error_code ret;
120         int32_t sec, usec;
121
122         ret = krb5_us_timeofday(context, &sec, &usec);
123         if (ret)
124                 return ret;
125
126         context->kdc_sec_offset = seconds - sec;
127         context->kdc_usec_offset = microseconds - usec;
128
129         return 0;
130 }
131 #endif
132
133 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
134
135 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
136
137 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
138  * to krb5_set_default_tgs_ktypes. See
139  *         http://lists.samba.org/archive/samba-technical/2006-July/048271.html
140  *
141  * If the MIT libraries are not exporting internal symbols, we will end up in
142  * this branch, which is correct. Otherwise we will continue to use the
143  * internal symbol
144  */
145  krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
146 {
147     return krb5_set_default_tgs_enctypes(ctx, enc);
148 }
149
150 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
151
152 /* Heimdal */
153  krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
154 {
155         return krb5_set_default_in_tkt_etypes(ctx, enc);
156 }
157
158 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
159
160 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
161
162 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
163 /* HEIMDAL */
164  void setup_kaddr( krb5_address *pkaddr, struct sockaddr *paddr)
165 {
166         pkaddr->addr_type = KRB5_ADDRESS_INET;
167         pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
168         pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
169 }
170 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
171 /* MIT */
172  void setup_kaddr( krb5_address *pkaddr, struct sockaddr *paddr)
173 {
174         pkaddr->addrtype = ADDRTYPE_INET;
175         pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
176         pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
177 }
178 #else
179 #error UNKNOWN_ADDRTYPE
180 #endif
181
182 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
183  int create_kerberos_key_from_string_direct(krb5_context context,
184                                         krb5_principal host_princ,
185                                         krb5_data *password,
186                                         krb5_keyblock *key,
187                                         krb5_enctype enctype)
188 {
189         int ret;
190         krb5_data salt;
191         krb5_encrypt_block eblock;
192
193         ret = krb5_principal2salt(context, host_princ, &salt);
194         if (ret) {
195                 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
196                 return ret;
197         }
198         krb5_use_enctype(context, &eblock, enctype);
199         ret = krb5_string_to_key(context, &eblock, key, password, &salt);
200         SAFE_FREE(salt.data);
201         return ret;
202 }
203 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
204  int create_kerberos_key_from_string_direct(krb5_context context,
205                                         krb5_principal host_princ,
206                                         krb5_data *password,
207                                         krb5_keyblock *key,
208                                         krb5_enctype enctype)
209 {
210         int ret;
211         krb5_salt salt;
212
213         ret = krb5_get_pw_salt(context, host_princ, &salt);
214         if (ret) {
215                 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
216                 return ret;
217         }
218         
219         ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, salt, key);
220         krb5_free_salt(context, salt);
221         return ret;
222 }
223 #else
224 #error UNKNOWN_CREATE_KEY_FUNCTIONS
225 #endif
226
227  int create_kerberos_key_from_string(krb5_context context,
228                                         krb5_principal host_princ,
229                                         krb5_data *password,
230                                         krb5_keyblock *key,
231                                         krb5_enctype enctype)
232 {
233         krb5_principal salt_princ = NULL;
234         int ret;
235         /*
236          * Check if we've determined that the KDC is salting keys for this
237          * principal/enctype in a non-obvious way.  If it is, try to match
238          * its behavior.
239          */
240         salt_princ = kerberos_fetch_salt_princ_for_host_princ(context, host_princ, enctype);
241         ret = create_kerberos_key_from_string_direct(context, salt_princ ? salt_princ : host_princ, password, key, enctype);
242         if (salt_princ) {
243                 krb5_free_principal(context, salt_princ);
244         }
245         return ret;
246 }
247
248 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
249  krb5_error_code get_kerberos_allowed_etypes(krb5_context context, 
250                                             krb5_enctype **enctypes)
251 {
252         return krb5_get_permitted_enctypes(context, enctypes);
253 }
254 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
255  krb5_error_code get_kerberos_allowed_etypes(krb5_context context, 
256                                             krb5_enctype **enctypes)
257 {
258         return krb5_get_default_in_tkt_etypes(context, enctypes);
259 }
260 #else
261 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
262 #endif
263
264 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
265  krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
266                                         krb5_auth_context auth_context,
267                                         krb5_keyblock *keyblock)
268 {
269         return krb5_auth_con_setkey(context, auth_context, keyblock);
270 }
271 #endif
272
273 BOOL unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
274 {
275         DATA_BLOB pac_contents;
276         ASN1_DATA data;
277         int data_type;
278
279         if (!auth_data->length) {
280                 return False;
281         }
282
283         asn1_load(&data, *auth_data);
284         asn1_start_tag(&data, ASN1_SEQUENCE(0));
285         asn1_start_tag(&data, ASN1_SEQUENCE(0));
286         asn1_start_tag(&data, ASN1_CONTEXT(0));
287         asn1_read_Integer(&data, &data_type);
288         
289         if (data_type != KRB5_AUTHDATA_WIN2K_PAC ) {
290                 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type));
291                 asn1_free(&data);
292                 return False;
293         }
294         
295         asn1_end_tag(&data);
296         asn1_start_tag(&data, ASN1_CONTEXT(1));
297         asn1_read_OctetString(&data, &pac_contents);
298         asn1_end_tag(&data);
299         asn1_end_tag(&data);
300         asn1_end_tag(&data);
301         asn1_free(&data);
302
303         *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length);
304
305         data_blob_free(&pac_contents);
306
307         return True;
308 }
309
310  BOOL get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, krb5_ticket *tkt)
311 {
312         DATA_BLOB auth_data_wrapped;
313         BOOL got_auth_data_pac = False;
314         int i;
315         
316 #if defined(HAVE_KRB5_TKT_ENC_PART2)
317         if (tkt->enc_part2 && tkt->enc_part2->authorization_data && 
318             tkt->enc_part2->authorization_data[0] && 
319             tkt->enc_part2->authorization_data[0]->length)
320         {
321                 for (i = 0; tkt->enc_part2->authorization_data[i] != NULL; i++) {
322                 
323                         if (tkt->enc_part2->authorization_data[i]->ad_type != 
324                             KRB5_AUTHDATA_IF_RELEVANT) {
325                                 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n", 
326                                         tkt->enc_part2->authorization_data[i]->ad_type));
327                                 continue;
328                         }
329
330                         auth_data_wrapped = data_blob(tkt->enc_part2->authorization_data[i]->contents,
331                                                       tkt->enc_part2->authorization_data[i]->length);
332
333                         /* check if it is a PAC */
334                         got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
335                         data_blob_free(&auth_data_wrapped);
336                         
337                         if (!got_auth_data_pac) {
338                                 continue;
339                         }
340                 }
341
342                 return got_auth_data_pac;
343         }
344                 
345 #else
346         if (tkt->ticket.authorization_data && 
347             tkt->ticket.authorization_data->len)
348         {
349                 for (i = 0; i < tkt->ticket.authorization_data->len; i++) {
350                         
351                         if (tkt->ticket.authorization_data->val[i].ad_type != 
352                             KRB5_AUTHDATA_IF_RELEVANT) {
353                                 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n", 
354                                         tkt->ticket.authorization_data->val[i].ad_type));
355                                 continue;
356                         }
357
358                         auth_data_wrapped = data_blob(tkt->ticket.authorization_data->val[i].ad_data.data,
359                                                       tkt->ticket.authorization_data->val[i].ad_data.length);
360
361                         /* check if it is a PAC */
362                         got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
363                         data_blob_free(&auth_data_wrapped);
364                         
365                         if (!got_auth_data_pac) {
366                                 continue;
367                         }
368                 }
369
370                 return got_auth_data_pac;
371         }
372 #endif
373         return False;
374 }
375
376  krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
377 {
378 #if defined(HAVE_KRB5_TKT_ENC_PART2)
379         return tkt->enc_part2->client;
380 #else
381         return tkt->client;
382 #endif
383 }
384
385 #if !defined(HAVE_KRB5_LOCATE_KDC)
386
387 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
388  * to a public interface for this functionality, so we have to be able to live
389  * without it if the MIT libraries are hiding their internal symbols.
390  */
391
392 #if defined(KRB5_KRBHST_INIT)
393 /* Heimdal */
394  krb5_error_code krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, struct sockaddr **addr_pp, int *naddrs, int get_masters)
395 {
396         krb5_krbhst_handle hnd;
397         krb5_krbhst_info *hinfo;
398         krb5_error_code rc;
399         int num_kdcs, i;
400         struct sockaddr *sa;
401         struct addrinfo *ai;
402
403         *addr_pp = NULL;
404         *naddrs = 0;
405
406         rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
407         if (rc) {
408                 DEBUG(0, ("krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
409                 return rc;
410         }
411
412         for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
413                 ;
414
415         krb5_krbhst_reset(ctx, hnd);
416
417         if (!num_kdcs) {
418                 DEBUG(0, ("krb5_locate_kdc: zero kdcs found !\n"));
419                 krb5_krbhst_free(ctx, hnd);
420                 return -1;
421         }
422
423         sa = SMB_MALLOC_ARRAY( struct sockaddr, num_kdcs );
424         if (!sa) {
425                 DEBUG(0, ("krb5_locate_kdc: malloc failed\n"));
426                 krb5_krbhst_free(ctx, hnd);
427                 naddrs = 0;
428                 return -1;
429         }
430
431         memset(sa, '\0', sizeof(struct sockaddr) * num_kdcs );
432
433         for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
434
435 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
436                 rc = krb5_krbhst_get_addrinfo(ctx, hinfo, &ai);
437                 if (rc) {
438                         DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc)));
439                         continue;
440                 }
441 #endif
442                 if (hinfo->ai && hinfo->ai->ai_family == AF_INET) 
443                         memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
444         }
445
446         krb5_krbhst_free(ctx, hnd);
447
448         *naddrs = num_kdcs;
449         *addr_pp = sa;
450         return 0;
451 }
452
453 #else /* ! defined(KRB5_KRBHST_INIT) */
454
455  krb5_error_code krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
456                 struct sockaddr **addr_pp, int *naddrs, int get_masters)
457 {
458         DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
459         return KRB5_KDC_UNREACH;
460 }
461
462 #endif /* KRB5_KRBHST_INIT */
463
464 #endif /* HAVE_KRB5_LOCATE_KDC */
465
466 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
467  void krb5_free_unparsed_name(krb5_context context, char *val)
468 {
469         SAFE_FREE(val);
470 }
471 #endif
472
473  void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
474 {
475 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
476         if (pdata->data) {
477                 krb5_free_data_contents(context, pdata);
478         }
479 #else
480         SAFE_FREE(pdata->data);
481 #endif
482 }
483
484  void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype)
485 {
486 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
487         KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype;
488 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
489         KRB5_KEY_TYPE((&pcreds->session)) = enctype;
490 #else
491 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
492 #endif
493 }
494
495  BOOL kerberos_compatible_enctypes(krb5_context context,
496                                   krb5_enctype enctype1,
497                                   krb5_enctype enctype2)
498 {
499 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
500         krb5_boolean similar = 0;
501
502         krb5_c_enctype_compare(context, enctype1, enctype2, &similar);
503         return similar ? True : False;
504 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
505         return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False;
506 #endif
507 }
508
509 static BOOL ads_cleanup_expired_creds(krb5_context context, 
510                                       krb5_ccache  ccache,
511                                       krb5_creds  *credsp)
512 {
513         krb5_error_code retval;
514         const char *cc_type = krb5_cc_get_type(context, ccache);
515
516         DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
517                   cc_type, krb5_cc_get_name(context, ccache),
518                   http_timestring(credsp->times.endtime)));
519
520         /* we will probably need new tickets if the current ones
521            will expire within 10 seconds.
522         */
523         if (credsp->times.endtime >= (time(NULL) + 10))
524                 return False;
525
526         /* heimdal won't remove creds from a file ccache, and 
527            perhaps we shouldn't anyway, since internally we 
528            use memory ccaches, and a FILE one probably means that
529            we're using creds obtained outside of our exectuable
530         */
531         if (strequal(cc_type, "FILE")) {
532                 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
533                 return False;
534         }
535
536         retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
537         if (retval) {
538                 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
539                           error_message(retval)));
540                 /* If we have an error in this, we want to display it,
541                    but continue as though we deleted it */
542         }
543         return True;
544 }
545
546 /*
547   we can't use krb5_mk_req because w2k wants the service to be in a particular format
548 */
549 static krb5_error_code ads_krb5_mk_req(krb5_context context, 
550                                        krb5_auth_context *auth_context, 
551                                        const krb5_flags ap_req_options,
552                                        const char *principal,
553                                        krb5_ccache ccache, 
554                                        krb5_data *outbuf)
555 {
556         krb5_error_code           retval;
557         krb5_principal    server;
558         krb5_creds              * credsp;
559         krb5_creds                creds;
560         krb5_data in_data;
561         BOOL creds_ready = False;
562         int i = 0, maxtries = 3;
563         
564         retval = smb_krb5_parse_name(context, principal, &server);
565         if (retval) {
566                 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
567                 return retval;
568         }
569         
570         /* obtain ticket & session key */
571         ZERO_STRUCT(creds);
572         if ((retval = krb5_copy_principal(context, server, &creds.server))) {
573                 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n", 
574                          error_message(retval)));
575                 goto cleanup_princ;
576         }
577         
578         if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
579                 /* This can commonly fail on smbd startup with no ticket in the cache.
580                  * Report at higher level than 1. */
581                 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n", 
582                          error_message(retval)));
583                 goto cleanup_creds;
584         }
585
586         while (!creds_ready && (i < maxtries)) {
587                 if ((retval = krb5_get_credentials(context, 0, ccache, 
588                                                    &creds, &credsp))) {
589                         DEBUG(1,("ads_krb5_mk_req: krb5_get_credentials failed for %s (%s)\n",
590                                  principal, error_message(retval)));
591                         goto cleanup_creds;
592                 }
593
594                 /* cope with ticket being in the future due to clock skew */
595                 if ((unsigned)credsp->times.starttime > time(NULL)) {
596                         time_t t = time(NULL);
597                         int time_offset =(int)((unsigned)credsp->times.starttime-t);
598                         DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
599                         krb5_set_real_time(context, t + time_offset + 1, 0);
600                 }
601
602                 if (!ads_cleanup_expired_creds(context, ccache, credsp))
603                         creds_ready = True;
604
605                 i++;
606         }
607
608         DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
609                   principal, krb5_cc_get_type(context, ccache), krb5_cc_get_name(context, ccache),
610                   http_timestring((unsigned)credsp->times.endtime), 
611                   (unsigned)credsp->times.endtime));
612
613         in_data.length = 0;
614         retval = krb5_mk_req_extended(context, auth_context, ap_req_options, 
615                                       &in_data, credsp, outbuf);
616         if (retval) {
617                 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n", 
618                          error_message(retval)));
619         }
620         
621         krb5_free_creds(context, credsp);
622
623 cleanup_creds:
624         krb5_free_cred_contents(context, &creds);
625
626 cleanup_princ:
627         krb5_free_principal(context, server);
628
629         return retval;
630 }
631
632 /*
633   get a kerberos5 ticket for the given service 
634 */
635 int cli_krb5_get_ticket(const char *principal, time_t time_offset, 
636                         DATA_BLOB *ticket, DATA_BLOB *session_key_krb5, 
637                         uint32 extra_ap_opts, const char *ccname)
638 {
639         krb5_error_code retval;
640         krb5_data packet;
641         krb5_context context = NULL;
642         krb5_ccache ccdef = NULL;
643         krb5_auth_context auth_context = NULL;
644         krb5_enctype enc_types[] = {
645 #ifdef ENCTYPE_ARCFOUR_HMAC
646                 ENCTYPE_ARCFOUR_HMAC,
647 #endif 
648                 ENCTYPE_DES_CBC_MD5, 
649                 ENCTYPE_DES_CBC_CRC, 
650                 ENCTYPE_NULL};
651
652         initialize_krb5_error_table();
653         retval = krb5_init_context(&context);
654         if (retval) {
655                 DEBUG(1,("cli_krb5_get_ticket: krb5_init_context failed (%s)\n", 
656                          error_message(retval)));
657                 goto failed;
658         }
659
660         if (time_offset != 0) {
661                 krb5_set_real_time(context, time(NULL) + time_offset, 0);
662         }
663
664         if ((retval = krb5_cc_resolve(context, ccname ?
665                         ccname : krb5_cc_default_name(context), &ccdef))) {
666                 DEBUG(1,("cli_krb5_get_ticket: krb5_cc_default failed (%s)\n",
667                          error_message(retval)));
668                 goto failed;
669         }
670
671         if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
672                 DEBUG(1,("cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s)\n",
673                          error_message(retval)));
674                 goto failed;
675         }
676
677         if ((retval = ads_krb5_mk_req(context, 
678                                         &auth_context, 
679                                         AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
680                                         principal,
681                                         ccdef, &packet))) {
682                 goto failed;
683         }
684
685         get_krb5_smb_session_key(context, auth_context, session_key_krb5, False);
686
687         *ticket = data_blob(packet.data, packet.length);
688
689         kerberos_free_data_contents(context, &packet); 
690
691 failed:
692
693         if ( context ) {
694                 if (ccdef)
695                         krb5_cc_close(context, ccdef);
696                 if (auth_context)
697                         krb5_auth_con_free(context, auth_context);
698                 krb5_free_context(context);
699         }
700                 
701         return retval;
702 }
703
704  BOOL get_krb5_smb_session_key(krb5_context context, krb5_auth_context auth_context, DATA_BLOB *session_key, BOOL remote)
705  {
706         krb5_keyblock *skey;
707         krb5_error_code err;
708         BOOL ret = False;
709
710         if (remote)
711                 err = krb5_auth_con_getremotesubkey(context, auth_context, &skey);
712         else
713                 err = krb5_auth_con_getlocalsubkey(context, auth_context, &skey);
714         if (err == 0 && skey != NULL) {
715                 DEBUG(10, ("Got KRB5 session key of length %d\n",  (int)KRB5_KEY_LENGTH(skey)));
716                 *session_key = data_blob(KRB5_KEY_DATA(skey), KRB5_KEY_LENGTH(skey));
717                 dump_data_pw("KRB5 Session Key:\n", session_key->data, session_key->length);
718
719                 ret = True;
720
721                 krb5_free_keyblock(context, skey);
722         } else {
723                 DEBUG(10, ("KRB5 error getting session key %d\n", err));
724         }
725
726         return ret;
727  }
728
729
730 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
731  const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i );
732
733  const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
734 {
735         static krb5_data kdata;
736
737         kdata.data = (char *)krb5_principal_get_comp_string(context, principal, i);
738         kdata.length = strlen((const char *)kdata.data);
739         return &kdata;
740 }
741 #endif
742
743  krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
744 {
745 #if defined(HAVE_KRB5_KT_FREE_ENTRY)
746         return krb5_kt_free_entry(context, kt_entry);
747 #elif defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
748         return krb5_free_keytab_entry_contents(context, kt_entry);
749 #else
750 #error UNKNOWN_KT_FREE_FUNCTION
751 #endif
752 }
753
754  void smb_krb5_checksum_from_pac_sig(krb5_checksum *cksum, 
755                                     PAC_SIGNATURE_DATA *sig)
756 {
757 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
758         cksum->cksumtype        = (krb5_cksumtype)sig->type;
759         cksum->checksum.length  = sig->signature.buf_len;
760         cksum->checksum.data    = sig->signature.buffer;
761 #else
762         cksum->checksum_type    = (krb5_cksumtype)sig->type;
763         cksum->length           = sig->signature.buf_len;
764         cksum->contents         = sig->signature.buffer;
765 #endif
766 }
767
768  krb5_error_code smb_krb5_verify_checksum(krb5_context context,
769                                          krb5_keyblock *keyblock,
770                                          krb5_keyusage usage,
771                                          krb5_checksum *cksum,
772                                          uint8 *data,
773                                          size_t length)
774 {
775         krb5_error_code ret;
776
777         /* verify the checksum */
778
779         /* welcome to the wonderful world of samba's kerberos abstraction layer:
780          * 
781          * function                     heimdal 0.6.1rc3        heimdal 0.7     MIT krb 1.4.2
782          * -----------------------------------------------------------------------------
783          * krb5_c_verify_checksum       -                       works           works
784          * krb5_verify_checksum         works (6 args)          works (6 args)  broken (7 args) 
785          */
786
787 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
788         {
789                 krb5_boolean checksum_valid = False;
790                 krb5_data input;
791
792                 input.data = (char *)data;
793                 input.length = length;
794
795                 ret = krb5_c_verify_checksum(context, 
796                                              keyblock, 
797                                              usage,
798                                              &input, 
799                                              cksum,
800                                              &checksum_valid);
801                 if (ret) {
802                         DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n", 
803                                 error_message(ret)));
804                         return ret;
805                 }
806
807                 if (!checksum_valid)
808                         ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
809         }
810
811 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
812
813         /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
814          * without enctype and it ignores any key_usage types - Guenther */
815
816         {
817
818                 krb5_crypto crypto;
819                 ret = krb5_crypto_init(context,
820                                        keyblock,
821                                        0,
822                                        &crypto);
823                 if (ret) {
824                         DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n", 
825                                 error_message(ret)));
826                         return ret;
827                 }
828
829                 ret = krb5_verify_checksum(context,
830                                            crypto,
831                                            usage,
832                                            data,
833                                            length,
834                                            cksum);
835
836                 krb5_crypto_destroy(context, crypto);
837         }
838
839 #else
840 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
841 #endif
842
843         return ret;
844 }
845
846  time_t get_authtime_from_tkt(krb5_ticket *tkt)
847 {
848 #if defined(HAVE_KRB5_TKT_ENC_PART2)
849         return tkt->enc_part2->times.authtime;
850 #else
851         return tkt->ticket.authtime;
852 #endif
853 }
854
855 static int get_kvno_from_ap_req(krb5_ap_req *ap_req)
856 {
857 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
858         if (ap_req->ticket->enc_part.kvno)
859                 return ap_req->ticket->enc_part.kvno;
860 #else /* Heimdal */
861         if (ap_req->ticket.enc_part.kvno) 
862                 return *ap_req->ticket.enc_part.kvno;
863 #endif
864         return 0;
865 }
866
867 static krb5_enctype get_enctype_from_ap_req(krb5_ap_req *ap_req)
868 {
869 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
870         return ap_req->ticket.enc_part.etype;
871 #else /* MIT */
872         return ap_req->ticket->enc_part.enctype;
873 #endif
874 }
875
876 static krb5_error_code
877 get_key_from_keytab(krb5_context context,
878                     krb5_const_principal server,
879                     krb5_enctype enctype,
880                     krb5_kvno kvno,
881                     krb5_keyblock **out_key)
882 {
883         krb5_keytab_entry entry;
884         krb5_error_code ret;
885         krb5_keytab keytab;
886         char *name = NULL;
887
888         /* We have to open a new keytab handle here, as MIT does
889            an implicit open/getnext/close on krb5_kt_get_entry. We
890            may be in the middle of a keytab enumeration when this is
891            called. JRA. */
892
893         ret = krb5_kt_default(context, &keytab);
894         if (ret) {
895                 DEBUG(0,("get_key_from_keytab: failed to open keytab: %s\n", error_message(ret)));
896                 return ret;
897         }
898
899         if ( DEBUGLEVEL >= 10 ) {
900                 if (smb_krb5_unparse_name(context, server, &name) == 0) {
901                         DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n", 
902                                 kvno, enctype, name));
903                         SAFE_FREE(name);
904                 }
905         }
906
907         ret = krb5_kt_get_entry(context,
908                                 keytab,
909                                 server,
910                                 kvno,
911                                 enctype,
912                                 &entry);
913
914         if (ret) {
915                 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret)));
916                 goto out;
917         }
918
919 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK /* Heimdal */
920         ret = krb5_copy_keyblock(context, &entry.keyblock, out_key);
921 #elif defined(HAVE_KRB5_KEYTAB_ENTRY_KEY) /* MIT */
922         ret = krb5_copy_keyblock(context, &entry.key, out_key);
923 #else
924 #error UNKNOWN_KRB5_KEYTAB_ENTRY_FORMAT
925 #endif
926
927         if (ret) {
928                 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret)));
929                 goto out;
930         }
931                 
932         smb_krb5_kt_free_entry(context, &entry);
933         
934 out:    
935         krb5_kt_close(context, keytab);
936         return ret;
937 }
938
939 /* Prototypes */
940
941  krb5_error_code smb_krb5_get_keyinfo_from_ap_req(krb5_context context, 
942                                                  const krb5_data *inbuf, 
943                                                  krb5_kvno *kvno, 
944                                                  krb5_enctype *enctype)
945 {
946 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
947         {
948                 krb5_error_code ret;
949                 krb5_ap_req ap_req;
950                 
951                 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
952                 if (ret)
953                         return ret;
954
955                 *kvno = get_kvno_from_ap_req(&ap_req);
956                 *enctype = get_enctype_from_ap_req(&ap_req);
957
958                 free_AP_REQ(&ap_req);
959                 return 0;
960         }
961 #endif
962
963         /* Possibly not an appropriate error code. */
964         return KRB5KDC_ERR_BADOPTION;
965 }
966
967  krb5_error_code krb5_rd_req_return_keyblock_from_keytab(krb5_context context,
968                                                         krb5_auth_context *auth_context,
969                                                         const krb5_data *inbuf,
970                                                         krb5_const_principal server,
971                                                         krb5_keytab keytab,
972                                                         krb5_flags *ap_req_options,
973                                                         krb5_ticket **ticket, 
974                                                         krb5_keyblock **keyblock)
975 {
976         krb5_error_code ret;
977         krb5_kvno kvno;
978         krb5_enctype enctype;
979         krb5_keyblock *local_keyblock;
980
981         ret = krb5_rd_req(context, 
982                           auth_context, 
983                           inbuf, 
984                           server, 
985                           keytab, 
986                           ap_req_options, 
987                           ticket);
988         if (ret) {
989                 return ret;
990         }
991         
992 #ifdef KRB5_TICKET_HAS_KEYINFO
993         enctype = (*ticket)->enc_part.enctype;
994         kvno = (*ticket)->enc_part.kvno;
995 #else
996         ret = smb_krb5_get_keyinfo_from_ap_req(context, inbuf, &kvno, &enctype);
997         if (ret) {
998                 return ret;
999         }
1000 #endif
1001
1002         ret = get_key_from_keytab(context, 
1003                                   server,
1004                                   enctype,
1005                                   kvno,
1006                                   &local_keyblock);
1007         if (ret) {
1008                 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1009                 goto out;
1010         }
1011
1012 out:
1013         if (ret && local_keyblock != NULL) {
1014                 krb5_free_keyblock(context, local_keyblock);
1015         } else {
1016                 *keyblock = local_keyblock;
1017         }
1018
1019         return ret;
1020 }
1021
1022  krb5_error_code smb_krb5_parse_name_norealm(krb5_context context, 
1023                                             const char *name, 
1024                                             krb5_principal *principal)
1025 {
1026 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1027         return smb_krb5_parse_name_norealm_conv(context, name, principal);
1028 #endif
1029
1030         /* we are cheating here because parse_name will in fact set the realm.
1031          * We don't care as the only caller of smb_krb5_parse_name_norealm
1032          * ignores the realm anyway when calling
1033          * smb_krb5_principal_compare_any_realm later - Guenther */
1034
1035         return smb_krb5_parse_name(context, name, principal);
1036 }
1037
1038  BOOL smb_krb5_principal_compare_any_realm(krb5_context context, 
1039                                           krb5_const_principal princ1, 
1040                                           krb5_const_principal princ2)
1041 {
1042 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1043
1044         return krb5_principal_compare_any_realm(context, princ1, princ2);
1045
1046 /* krb5_princ_size is a macro in MIT */
1047 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1048
1049         int i, len1, len2;
1050         const krb5_data *p1, *p2;
1051
1052         len1 = krb5_princ_size(context, princ1);
1053         len2 = krb5_princ_size(context, princ2);
1054
1055         if (len1 != len2)
1056                 return False;
1057
1058         for (i = 0; i < len1; i++) {
1059
1060                 p1 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ1), i);
1061                 p2 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ2), i);
1062
1063                 if (p1->length != p2->length || memcmp(p1->data, p2->data, p1->length))
1064                         return False;
1065         }
1066
1067         return True;
1068 #else
1069 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1070 #endif
1071 }
1072
1073  krb5_error_code smb_krb5_renew_ticket(const char *ccache_string,       /* FILE:/tmp/krb5cc_0 */
1074                                        const char *client_string,       /* gd@BER.SUSE.DE */
1075                                        const char *service_string,      /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1076                                        time_t *new_start_time)
1077 {
1078         krb5_error_code ret;
1079         krb5_context context = NULL;
1080         krb5_ccache ccache = NULL;
1081         krb5_principal client = NULL;
1082
1083         initialize_krb5_error_table();
1084         ret = krb5_init_context(&context);
1085         if (ret) {
1086                 goto done;
1087         }
1088
1089         if (!ccache_string) {
1090                 ccache_string = krb5_cc_default_name(context);
1091         }
1092
1093         DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
1094
1095         /* FIXME: we should not fall back to defaults */
1096         ret = krb5_cc_resolve(context, CONST_DISCARD(char *, ccache_string), &ccache);
1097         if (ret) {
1098                 goto done;
1099         }
1100
1101 #ifdef HAVE_KRB5_GET_RENEWED_CREDS      /* MIT */
1102         {
1103                 krb5_creds creds;
1104         
1105                 if (client_string) {
1106                         ret = smb_krb5_parse_name(context, client_string, &client);
1107                         if (ret) {
1108                                 goto done;
1109                         }
1110                 } else {
1111                         ret = krb5_cc_get_principal(context, ccache, &client);
1112                         if (ret) {
1113                                 goto done;
1114                         }
1115                 }
1116         
1117                 ret = krb5_get_renewed_creds(context, &creds, client, ccache, CONST_DISCARD(char *, service_string));
1118                 if (ret) {
1119                         DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1120                         goto done;
1121                 }
1122
1123                 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1124                 ret = krb5_cc_initialize(context, ccache, client);
1125                 if (ret) {
1126                         goto done;
1127                 }
1128         
1129                 ret = krb5_cc_store_cred(context, ccache, &creds);
1130
1131                 if (new_start_time) {
1132                         *new_start_time = (time_t) creds.times.renew_till;
1133                 }
1134
1135                 krb5_free_cred_contents(context, &creds);
1136         }
1137 #elif defined(HAVE_KRB5_GET_KDC_CRED)   /* Heimdal */
1138         {
1139                 krb5_kdc_flags flags;
1140                 krb5_creds creds_in;
1141                 krb5_realm *client_realm;
1142                 krb5_creds *creds;
1143
1144                 memset(&creds_in, 0, sizeof(creds_in));
1145
1146                 if (client_string) {
1147                         ret = smb_krb5_parse_name(context, client_string, &creds_in.client);
1148                         if (ret) {
1149                                 goto done;
1150                         }
1151                 } else {
1152                         ret = krb5_cc_get_principal(context, ccache, &creds_in.client);
1153                         if (ret) {
1154                                 goto done;
1155                         }
1156                 }
1157
1158                 if (service_string) {
1159                         ret = smb_krb5_parse_name(context, service_string, &creds_in.server);
1160                         if (ret) { 
1161                                 goto done;
1162                         }
1163                 } else {
1164                         /* build tgt service by default */
1165                         client_realm = krb5_princ_realm(context, client);
1166                         ret = krb5_make_principal(context, &creds_in.server, *client_realm, KRB5_TGS_NAME, *client_realm, NULL);
1167                         if (ret) {
1168                                 goto done;
1169                         }
1170                 }
1171
1172                 flags.i = 0;
1173                 flags.b.renewable = flags.b.renew = True;
1174
1175                 ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &creds_in, &creds);
1176                 if (ret) {
1177                         DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1178                         goto done;
1179                 }
1180                 
1181                 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1182                 ret = krb5_cc_initialize(context, ccache, creds_in.client);
1183                 if (ret) {
1184                         goto done;
1185                 }
1186         
1187                 ret = krb5_cc_store_cred(context, ccache, creds);
1188
1189                 if (new_start_time) {
1190                         *new_start_time = (time_t) creds->times.renew_till;
1191                 }
1192                                                 
1193                 krb5_free_cred_contents(context, &creds_in);
1194                 krb5_free_creds(context, creds);
1195         }
1196 #else
1197 #error No suitable krb5 ticket renew function available
1198 #endif
1199
1200
1201 done:
1202         if (client) {
1203                 krb5_free_principal(context, client);
1204         }
1205         if (context) {
1206                 krb5_free_context(context);
1207         }
1208         if (ccache) {
1209                 krb5_cc_close(context, ccache);
1210         }
1211
1212         return ret;
1213     
1214 }
1215
1216  krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr)
1217 {
1218         krb5_error_code ret = 0;
1219         if (addr == NULL) {
1220                 return ret;
1221         }
1222 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1223         krb5_free_addresses(context, addr->addrs);
1224 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1225         ret = krb5_free_addresses(context, addr->addrs);
1226         SAFE_FREE(addr->addrs);
1227 #endif
1228         SAFE_FREE(addr);
1229         addr = NULL;
1230         return ret;
1231 }
1232
1233  krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr)
1234 {
1235         krb5_error_code ret = 0;
1236         nstring buf;
1237 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1238         krb5_address **addrs = NULL;
1239 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1240         krb5_addresses *addrs = NULL;
1241 #endif
1242
1243         *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
1244         if (*kerb_addr == NULL) {
1245                 return ENOMEM;
1246         }
1247
1248         put_name(buf, global_myname(), ' ', 0x20);
1249
1250 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1251         {
1252                 int num_addr = 2;
1253
1254                 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
1255                 if (addrs == NULL) {
1256                         SAFE_FREE(kerb_addr);
1257                         return ENOMEM;
1258                 }
1259
1260                 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
1261
1262                 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1263                 if (addrs[0] == NULL) {
1264                         SAFE_FREE(addrs);
1265                         SAFE_FREE(kerb_addr);
1266                         return ENOMEM;
1267                 }
1268
1269                 addrs[0]->magic = KV5M_ADDRESS;
1270                 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
1271                 addrs[0]->length = MAX_NETBIOSNAME_LEN;
1272                 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
1273                 if (addrs[0]->contents == NULL) {
1274                         SAFE_FREE(addrs[0]);
1275                         SAFE_FREE(addrs);
1276                         SAFE_FREE(kerb_addr);
1277                         return ENOMEM;
1278                 }
1279
1280                 memcpy(addrs[0]->contents, buf, addrs[0]->length);
1281
1282                 addrs[1] = NULL;
1283         }
1284 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1285         {
1286                 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
1287                 if (addrs == NULL) {
1288                         SAFE_FREE(kerb_addr);
1289                         return ENOMEM;
1290                 }
1291
1292                 memset(addrs, 0, sizeof(krb5_addresses));
1293
1294                 addrs->len = 1;
1295                 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1296                 if (addrs->val == NULL) {
1297                         SAFE_FREE(addrs);
1298                         SAFE_FREE(kerb_addr);
1299                         return ENOMEM;
1300                 }
1301
1302                 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
1303                 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
1304                 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
1305                 if (addrs->val[0].address.data == NULL) {
1306                         SAFE_FREE(addrs->val);
1307                         SAFE_FREE(addrs);
1308                         SAFE_FREE(kerb_addr);
1309                         return ENOMEM;
1310                 }
1311
1312                 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
1313         }
1314 #else
1315 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1316 #endif
1317         (*kerb_addr)->addrs = addrs;
1318
1319         return ret;
1320 }
1321
1322  void smb_krb5_free_error(krb5_context context, krb5_error *krberror)
1323 {
1324 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1325         krb5_free_error_contents(context, krberror);
1326 #else /* MIT */
1327         krb5_free_error(context, krberror);
1328 #endif
1329 }
1330
1331  krb5_error_code handle_krberror_packet(krb5_context context,
1332                                         krb5_data *packet)
1333 {
1334         krb5_error_code ret;
1335         BOOL got_error_code = False;
1336
1337         DEBUG(10,("handle_krberror_packet: got error packet\n"));
1338         
1339 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1340         {
1341                 krb5_error krberror;
1342
1343                 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1344                         DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n", 
1345                                 error_message(ret)));
1346                         return ret;
1347                 }
1348
1349                 if (krberror.e_data == NULL || krberror.e_data->data == NULL) {
1350                         ret = (krb5_error_code) krberror.error_code;
1351                         got_error_code = True;
1352                 }
1353
1354                 smb_krb5_free_error(context, &krberror);
1355         }
1356 #else /* MIT */
1357         {
1358                 krb5_error *krberror;
1359
1360                 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1361                         DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n", 
1362                                 error_message(ret)));
1363                         return ret;
1364                 }
1365
1366                 if (krberror->e_data.data == NULL) {
1367                         ret = ERROR_TABLE_BASE_krb5 + (krb5_error_code) krberror->error;
1368                         got_error_code = True;
1369                 }
1370                 smb_krb5_free_error(context, krberror);
1371         }
1372 #endif
1373         if (got_error_code) {
1374                 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n", 
1375                         error_message(ret), ret));
1376         }
1377         return ret;
1378 }
1379
1380 #else /* HAVE_KRB5 */
1381  /* this saves a few linking headaches */
1382  int cli_krb5_get_ticket(const char *principal, time_t time_offset, 
1383                         DATA_BLOB *ticket, DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
1384                         const char *ccname) 
1385 {
1386          DEBUG(0,("NO KERBEROS SUPPORT\n"));
1387          return 1;
1388 }
1389
1390 #endif