f940081072ae3cc183ccb585ff09cb43c8ceed7b
[kai/samba.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-2007
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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #define KRB5_PRIVATE    1       /* this file uses PRIVATE interfaces! */
24 #define KRB5_DEPRECATED 1       /* this file uses DEPRECATED interfaces! */
25
26 #include "includes.h"
27
28 #ifdef HAVE_KRB5
29
30 #define GSSAPI_CHECKSUM      0x8003             /* Checksum type value for Kerberos */
31 #define GSSAPI_BNDLENGTH     16                 /* Bind Length (rfc-1964 pg.3) */
32 #define GSSAPI_CHECKSUM_SIZE (12+GSSAPI_BNDLENGTH)
33
34 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY)
35 static krb5_error_code ads_krb5_get_fwd_ticket( krb5_context context,
36                                          krb5_auth_context *auth_context,
37                                          krb5_creds *credsp,
38                                          krb5_ccache ccache,
39                                          krb5_data *authenticator);
40 #endif
41
42 /**************************************************************
43  Wrappers around kerberos string functions that convert from
44  utf8 -> unix charset and vica versa.
45 **************************************************************/
46
47 /**************************************************************
48  krb5_parse_name that takes a UNIX charset.
49 **************************************************************/
50
51  krb5_error_code smb_krb5_parse_name(krb5_context context,
52                                 const char *name, /* in unix charset */
53                                 krb5_principal *principal)
54 {
55         krb5_error_code ret;
56         char *utf8_name;
57         size_t converted_size;
58
59         if (!push_utf8_allocate(&utf8_name, name, &converted_size)) {
60                 return ENOMEM;
61         }
62
63         ret = krb5_parse_name(context, utf8_name, principal);
64         SAFE_FREE(utf8_name);
65         return ret;
66 }
67
68 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
69 /**************************************************************
70  krb5_parse_name_norealm that takes a UNIX charset.
71 **************************************************************/
72
73 static krb5_error_code smb_krb5_parse_name_norealm_conv(krb5_context context,
74                                 const char *name, /* in unix charset */
75                                 krb5_principal *principal)
76 {
77         krb5_error_code ret;
78         char *utf8_name;
79         size_t converted_size;
80
81         *principal = NULL;
82         if (!push_utf8_allocate(&utf8_name, name, &converted_size)) {
83                 return ENOMEM;
84         }
85
86         ret = krb5_parse_name_norealm(context, utf8_name, principal);
87         SAFE_FREE(utf8_name);
88         return ret;
89 }
90 #endif
91
92 /**************************************************************
93  krb5_parse_name that returns a UNIX charset name. Must
94  be freed with normal free() call.
95 **************************************************************/
96
97  krb5_error_code smb_krb5_unparse_name(krb5_context context,
98                                         krb5_const_principal principal,
99                                         char **unix_name)
100 {
101         krb5_error_code ret;
102         char *utf8_name;
103         size_t converted_size;
104
105         *unix_name = NULL;
106         ret = krb5_unparse_name(context, principal, &utf8_name);
107         if (ret) {
108                 return ret;
109         }
110
111         if (!pull_utf8_allocate(unix_name, utf8_name, &converted_size)) {
112                 krb5_free_unparsed_name(context, utf8_name);
113                 return ENOMEM;
114         }
115         krb5_free_unparsed_name(context, utf8_name);
116         return 0;
117 }
118
119 #ifndef HAVE_KRB5_SET_REAL_TIME
120 /*
121  * This function is not in the Heimdal mainline.
122  */
123  krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
124 {
125         krb5_error_code ret;
126         int32_t sec, usec;
127
128         ret = krb5_us_timeofday(context, &sec, &usec);
129         if (ret)
130                 return ret;
131
132         context->kdc_sec_offset = seconds - sec;
133         context->kdc_usec_offset = microseconds - usec;
134
135         return 0;
136 }
137 #endif
138
139 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
140
141 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
142
143 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
144  * to krb5_set_default_tgs_ktypes. See
145  *         http://lists.samba.org/archive/samba-technical/2006-July/048271.html
146  *
147  * If the MIT libraries are not exporting internal symbols, we will end up in
148  * this branch, which is correct. Otherwise we will continue to use the
149  * internal symbol
150  */
151  krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
152 {
153     return krb5_set_default_tgs_enctypes(ctx, enc);
154 }
155
156 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
157
158 /* Heimdal */
159  krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
160 {
161         return krb5_set_default_in_tkt_etypes(ctx, enc);
162 }
163
164 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
165
166 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
167
168 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
169 /* HEIMDAL */
170  bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
171 {
172         memset(pkaddr, '\0', sizeof(krb5_address));
173 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
174         if (paddr->ss_family == AF_INET6) {
175                 pkaddr->addr_type = KRB5_ADDRESS_INET6;
176                 pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
177                 pkaddr->address.data = (char *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
178                 return true;
179         }
180 #endif
181         if (paddr->ss_family == AF_INET) {
182                 pkaddr->addr_type = KRB5_ADDRESS_INET;
183                 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
184                 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
185                 return true;
186         }
187         return false;
188 }
189 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
190 /* MIT */
191  bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
192 {
193         memset(pkaddr, '\0', sizeof(krb5_address));
194 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
195         if (paddr->ss_family == AF_INET6) {
196                 pkaddr->addrtype = ADDRTYPE_INET6;
197                 pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
198                 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
199                 return true;
200         }
201 #endif
202         if (paddr->ss_family == AF_INET) {
203                 pkaddr->addrtype = ADDRTYPE_INET;
204                 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
205                 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
206                 return true;
207         }
208         return false;
209 }
210 #else
211 #error UNKNOWN_ADDRTYPE
212 #endif
213
214 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
215 static int create_kerberos_key_from_string_direct(krb5_context context,
216                                                   krb5_principal host_princ,
217                                                   krb5_data *password,
218                                                   krb5_keyblock *key,
219                                                   krb5_enctype enctype)
220 {
221         int ret = 0;
222         krb5_data salt;
223         krb5_encrypt_block eblock;
224
225         ret = krb5_principal2salt(context, host_princ, &salt);
226         if (ret) {
227                 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
228                 return ret;
229         }
230         krb5_use_enctype(context, &eblock, enctype);
231         ret = krb5_string_to_key(context, &eblock, key, password, &salt);
232         SAFE_FREE(salt.data);
233
234         return ret;
235 }
236 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
237 static int create_kerberos_key_from_string_direct(krb5_context context,
238                                                   krb5_principal host_princ,
239                                                   krb5_data *password,
240                                                   krb5_keyblock *key,
241                                                   krb5_enctype enctype)
242 {
243         int ret;
244         krb5_salt salt;
245
246         ret = krb5_get_pw_salt(context, host_princ, &salt);
247         if (ret) {
248                 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
249                 return ret;
250         }
251
252         ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, salt, key);
253         krb5_free_salt(context, salt);
254
255         return ret;
256 }
257 #else
258 #error UNKNOWN_CREATE_KEY_FUNCTIONS
259 #endif
260
261  int create_kerberos_key_from_string(krb5_context context,
262                                         krb5_principal host_princ,
263                                         krb5_data *password,
264                                         krb5_keyblock *key,
265                                         krb5_enctype enctype,
266                                         bool no_salt)
267 {
268         krb5_principal salt_princ = NULL;
269         int ret;
270         /*
271          * Check if we've determined that the KDC is salting keys for this
272          * principal/enctype in a non-obvious way.  If it is, try to match
273          * its behavior.
274          */
275         if (no_salt) {
276                 KRB5_KEY_DATA(key) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
277                 if (!KRB5_KEY_DATA(key)) {
278                         return ENOMEM;
279                 }
280                 memcpy(KRB5_KEY_DATA(key), password->data, password->length);
281                 KRB5_KEY_LENGTH(key) = password->length;
282                 KRB5_KEY_TYPE(key) = enctype;
283                 return 0;
284         }
285         salt_princ = kerberos_fetch_salt_princ_for_host_princ(context, host_princ, enctype);
286         ret = create_kerberos_key_from_string_direct(context, salt_princ ? salt_princ : host_princ, password, key, enctype);
287         if (salt_princ) {
288                 krb5_free_principal(context, salt_princ);
289         }
290         return ret;
291 }
292
293 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
294  krb5_error_code get_kerberos_allowed_etypes(krb5_context context, 
295                                             krb5_enctype **enctypes)
296 {
297         return krb5_get_permitted_enctypes(context, enctypes);
298 }
299 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
300  krb5_error_code get_kerberos_allowed_etypes(krb5_context context, 
301                                             krb5_enctype **enctypes)
302 {
303         return krb5_get_default_in_tkt_etypes(context, enctypes);
304 }
305 #else
306 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
307 #endif
308
309 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
310  krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
311                                         krb5_auth_context auth_context,
312                                         krb5_keyblock *keyblock)
313 {
314         return krb5_auth_con_setkey(context, auth_context, keyblock);
315 }
316 #endif
317
318 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx, 
319                            DATA_BLOB *edata, 
320                            DATA_BLOB *edata_out)
321 {
322         DATA_BLOB edata_contents;
323         ASN1_DATA data;
324         int edata_type;
325
326         if (!edata->length) {
327                 return False;
328         }
329
330         asn1_load(&data, *edata);
331         asn1_start_tag(&data, ASN1_SEQUENCE(0));
332         asn1_start_tag(&data, ASN1_CONTEXT(1));
333         asn1_read_Integer(&data, &edata_type);
334
335         if (edata_type != KRB5_PADATA_PW_SALT) {
336                 DEBUG(0,("edata is not of required type %d but of type %d\n", 
337                         KRB5_PADATA_PW_SALT, edata_type));
338                 asn1_free(&data);
339                 return False;
340         }
341         
342         asn1_start_tag(&data, ASN1_CONTEXT(2));
343         asn1_read_OctetString(&data, &edata_contents);
344         asn1_end_tag(&data);
345         asn1_end_tag(&data);
346         asn1_end_tag(&data);
347         asn1_free(&data);
348
349         *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length);
350
351         data_blob_free(&edata_contents);
352
353         return True;
354 }
355
356
357 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
358 {
359         DATA_BLOB pac_contents;
360         ASN1_DATA data;
361         int data_type;
362
363         if (!auth_data->length) {
364                 return False;
365         }
366
367         asn1_load(&data, *auth_data);
368         asn1_start_tag(&data, ASN1_SEQUENCE(0));
369         asn1_start_tag(&data, ASN1_SEQUENCE(0));
370         asn1_start_tag(&data, ASN1_CONTEXT(0));
371         asn1_read_Integer(&data, &data_type);
372         
373         if (data_type != KRB5_AUTHDATA_WIN2K_PAC ) {
374                 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type));
375                 asn1_free(&data);
376                 return False;
377         }
378         
379         asn1_end_tag(&data);
380         asn1_start_tag(&data, ASN1_CONTEXT(1));
381         asn1_read_OctetString(&data, &pac_contents);
382         asn1_end_tag(&data);
383         asn1_end_tag(&data);
384         asn1_end_tag(&data);
385         asn1_free(&data);
386
387         *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length);
388
389         data_blob_free(&pac_contents);
390
391         return True;
392 }
393
394  bool get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, krb5_ticket *tkt)
395 {
396         DATA_BLOB auth_data_wrapped;
397         bool got_auth_data_pac = False;
398         int i;
399         
400 #if defined(HAVE_KRB5_TKT_ENC_PART2)
401         if (tkt->enc_part2 && tkt->enc_part2->authorization_data && 
402             tkt->enc_part2->authorization_data[0] && 
403             tkt->enc_part2->authorization_data[0]->length)
404         {
405                 for (i = 0; tkt->enc_part2->authorization_data[i] != NULL; i++) {
406                 
407                         if (tkt->enc_part2->authorization_data[i]->ad_type != 
408                             KRB5_AUTHDATA_IF_RELEVANT) {
409                                 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n", 
410                                         tkt->enc_part2->authorization_data[i]->ad_type));
411                                 continue;
412                         }
413
414                         auth_data_wrapped = data_blob(tkt->enc_part2->authorization_data[i]->contents,
415                                                       tkt->enc_part2->authorization_data[i]->length);
416
417                         /* check if it is a PAC */
418                         got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
419                         data_blob_free(&auth_data_wrapped);
420
421                         if (got_auth_data_pac) {
422                                 return true;
423                         }
424                 }
425
426                 return got_auth_data_pac;
427         }
428                 
429 #else
430         if (tkt->ticket.authorization_data && 
431             tkt->ticket.authorization_data->len)
432         {
433                 for (i = 0; i < tkt->ticket.authorization_data->len; i++) {
434                         
435                         if (tkt->ticket.authorization_data->val[i].ad_type != 
436                             KRB5_AUTHDATA_IF_RELEVANT) {
437                                 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n", 
438                                         tkt->ticket.authorization_data->val[i].ad_type));
439                                 continue;
440                         }
441
442                         auth_data_wrapped = data_blob(tkt->ticket.authorization_data->val[i].ad_data.data,
443                                                       tkt->ticket.authorization_data->val[i].ad_data.length);
444
445                         /* check if it is a PAC */
446                         got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
447                         data_blob_free(&auth_data_wrapped);
448
449                         if (got_auth_data_pac) {
450                                 return true;
451                         }
452                 }
453
454                 return got_auth_data_pac;
455         }
456 #endif
457         return False;
458 }
459
460  krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
461 {
462 #if defined(HAVE_KRB5_TKT_ENC_PART2)
463         return tkt->enc_part2->client;
464 #else
465         return tkt->client;
466 #endif
467 }
468
469 #if !defined(HAVE_KRB5_LOCATE_KDC)
470
471 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
472  * to a public interface for this functionality, so we have to be able to live
473  * without it if the MIT libraries are hiding their internal symbols.
474  */
475
476 #if defined(KRB5_KRBHST_INIT)
477 /* Heimdal */
478  krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, struct sockaddr **addr_pp, int *naddrs, int get_masters)
479 {
480         krb5_krbhst_handle hnd;
481         krb5_krbhst_info *hinfo;
482         krb5_error_code rc;
483         int num_kdcs, i;
484         struct sockaddr *sa;
485         struct addrinfo *ai;
486
487         *addr_pp = NULL;
488         *naddrs = 0;
489
490         rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
491         if (rc) {
492                 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
493                 return rc;
494         }
495
496         for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
497                 ;
498
499         krb5_krbhst_reset(ctx, hnd);
500
501         if (!num_kdcs) {
502                 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
503                 krb5_krbhst_free(ctx, hnd);
504                 return -1;
505         }
506
507         sa = SMB_MALLOC_ARRAY( struct sockaddr, num_kdcs );
508         if (!sa) {
509                 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
510                 krb5_krbhst_free(ctx, hnd);
511                 naddrs = 0;
512                 return -1;
513         }
514
515         memset(sa, '\0', sizeof(struct sockaddr) * num_kdcs );
516
517         for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
518
519 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
520                 rc = krb5_krbhst_get_addrinfo(ctx, hinfo, &ai);
521                 if (rc) {
522                         DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc)));
523                         continue;
524                 }
525 #endif
526                 if (hinfo->ai && hinfo->ai->ai_family == AF_INET) 
527                         memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
528         }
529
530         krb5_krbhst_free(ctx, hnd);
531
532         *naddrs = num_kdcs;
533         *addr_pp = sa;
534         return 0;
535 }
536
537 #else /* ! defined(KRB5_KRBHST_INIT) */
538
539  krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
540                 struct sockaddr **addr_pp, int *naddrs, int get_masters)
541 {
542         DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
543         return KRB5_KDC_UNREACH;
544 }
545
546 #endif /* KRB5_KRBHST_INIT */
547
548 #else /* ! HAVE_KRB5_LOCATE_KDC */
549
550  krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
551                 struct sockaddr **addr_pp, int *naddrs, int get_masters)
552 {
553         return krb5_locate_kdc(ctx, realm, addr_pp, naddrs, get_masters);
554 }
555
556 #endif /* HAVE_KRB5_LOCATE_KDC */
557
558 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
559  void krb5_free_unparsed_name(krb5_context context, char *val)
560 {
561         SAFE_FREE(val);
562 }
563 #endif
564
565  void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
566 {
567 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
568         if (pdata->data) {
569                 krb5_free_data_contents(context, pdata);
570         }
571 #else
572         SAFE_FREE(pdata->data);
573 #endif
574 }
575
576  void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype)
577 {
578 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
579         KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype;
580 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
581         KRB5_KEY_TYPE((&pcreds->session)) = enctype;
582 #else
583 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
584 #endif
585 }
586
587  bool kerberos_compatible_enctypes(krb5_context context,
588                                   krb5_enctype enctype1,
589                                   krb5_enctype enctype2)
590 {
591 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
592         krb5_boolean similar = 0;
593
594         krb5_c_enctype_compare(context, enctype1, enctype2, &similar);
595         return similar ? True : False;
596 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
597         return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False;
598 #endif
599 }
600
601 static bool ads_cleanup_expired_creds(krb5_context context, 
602                                       krb5_ccache  ccache,
603                                       krb5_creds  *credsp)
604 {
605         krb5_error_code retval;
606         const char *cc_type = krb5_cc_get_type(context, ccache);
607
608         DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
609                   cc_type, krb5_cc_get_name(context, ccache),
610                   http_timestring(credsp->times.endtime)));
611
612         /* we will probably need new tickets if the current ones
613            will expire within 10 seconds.
614         */
615         if (credsp->times.endtime >= (time(NULL) + 10))
616                 return False;
617
618         /* heimdal won't remove creds from a file ccache, and 
619            perhaps we shouldn't anyway, since internally we 
620            use memory ccaches, and a FILE one probably means that
621            we're using creds obtained outside of our exectuable
622         */
623         if (strequal(cc_type, "FILE")) {
624                 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
625                 return False;
626         }
627
628         retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
629         if (retval) {
630                 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
631                           error_message(retval)));
632                 /* If we have an error in this, we want to display it,
633                    but continue as though we deleted it */
634         }
635         return True;
636 }
637
638 /*
639   we can't use krb5_mk_req because w2k wants the service to be in a particular format
640 */
641 static krb5_error_code ads_krb5_mk_req(krb5_context context, 
642                                        krb5_auth_context *auth_context, 
643                                        const krb5_flags ap_req_options,
644                                        const char *principal,
645                                        krb5_ccache ccache, 
646                                        krb5_data *outbuf, 
647                                        time_t *expire_time)
648 {
649         krb5_error_code           retval;
650         krb5_principal    server;
651         krb5_creds              * credsp;
652         krb5_creds                creds;
653         krb5_data in_data;
654         bool creds_ready = False;
655         int i = 0, maxtries = 3;
656         
657         ZERO_STRUCT(in_data);
658
659         retval = smb_krb5_parse_name(context, principal, &server);
660         if (retval) {
661                 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
662                 return retval;
663         }
664         
665         /* obtain ticket & session key */
666         ZERO_STRUCT(creds);
667         if ((retval = krb5_copy_principal(context, server, &creds.server))) {
668                 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n", 
669                          error_message(retval)));
670                 goto cleanup_princ;
671         }
672         
673         if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
674                 /* This can commonly fail on smbd startup with no ticket in the cache.
675                  * Report at higher level than 1. */
676                 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n", 
677                          error_message(retval)));
678                 goto cleanup_creds;
679         }
680
681         while (!creds_ready && (i < maxtries)) {
682
683                 if ((retval = krb5_get_credentials(context, 0, ccache, 
684                                                    &creds, &credsp))) {
685                         DEBUG(1,("ads_krb5_mk_req: krb5_get_credentials failed for %s (%s)\n",
686                                  principal, error_message(retval)));
687                         goto cleanup_creds;
688                 }
689
690                 /* cope with ticket being in the future due to clock skew */
691                 if ((unsigned)credsp->times.starttime > time(NULL)) {
692                         time_t t = time(NULL);
693                         int time_offset =(int)((unsigned)credsp->times.starttime-t);
694                         DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
695                         krb5_set_real_time(context, t + time_offset + 1, 0);
696                 }
697
698                 if (!ads_cleanup_expired_creds(context, ccache, credsp)) {
699                         creds_ready = True;
700                 }
701
702                 i++;
703         }
704
705         DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
706                   principal, krb5_cc_get_type(context, ccache), krb5_cc_get_name(context, ccache),
707                   http_timestring((unsigned)credsp->times.endtime), 
708                   (unsigned)credsp->times.endtime));
709
710         if (expire_time) {
711                 *expire_time = (time_t)credsp->times.endtime;
712         }
713
714 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY)
715         if( credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE ) {
716                 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
717                  as part of the kerberos exchange. */
718
719                 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n")  );
720
721                 if( *auth_context == NULL ) {
722                         /* Allocate if it has not yet been allocated. */
723                         retval = krb5_auth_con_init( context, auth_context );
724                         if (retval) {
725                                 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_init failed (%s)\n",
726                                         error_message(retval)));
727                                 goto cleanup_creds;
728                         }
729                 }
730
731                 retval = krb5_auth_con_setuseruserkey( context, *auth_context, &credsp->keyblock );
732                 if (retval) {
733                         DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_setuseruserkey failed (%s)\n",
734                                 error_message(retval)));
735                         goto cleanup_creds;
736                 }
737
738                 /* Must use a subkey for forwarded tickets. */
739                 retval = krb5_auth_con_setflags( context, *auth_context, KRB5_AUTH_CONTEXT_USE_SUBKEY);
740                 if (retval) {
741                         DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_setflags failed (%s)\n",
742                                 error_message(retval)));
743                         goto cleanup_creds;
744                 }
745
746                 retval = ads_krb5_get_fwd_ticket( context,
747                                                 auth_context,
748                                                 credsp,
749                                                 ccache,
750                                                 &in_data );
751                 if (retval) {
752                         DEBUG( 1, ("ads_krb5_get_fwd_ticket failed (%s)\n",
753                                    error_message( retval ) ) );
754                         goto cleanup_creds;
755                 }
756         }
757 #endif
758
759         retval = krb5_mk_req_extended(context, auth_context, ap_req_options, 
760                                       &in_data, credsp, outbuf);
761         if (retval) {
762                 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n", 
763                          error_message(retval)));
764         }
765
766         if (in_data.data) {
767                 free( in_data.data );
768                 in_data.length = 0;
769         }
770
771         krb5_free_creds(context, credsp);
772
773 cleanup_creds:
774         krb5_free_cred_contents(context, &creds);
775
776 cleanup_princ:
777         krb5_free_principal(context, server);
778
779         return retval;
780 }
781
782 /*
783   get a kerberos5 ticket for the given service 
784 */
785 int cli_krb5_get_ticket(const char *principal, time_t time_offset, 
786                         DATA_BLOB *ticket, DATA_BLOB *session_key_krb5, 
787                         uint32 extra_ap_opts, const char *ccname, 
788                         time_t *tgs_expire)
789
790 {
791         krb5_error_code retval;
792         krb5_data packet;
793         krb5_context context = NULL;
794         krb5_ccache ccdef = NULL;
795         krb5_auth_context auth_context = NULL;
796         krb5_enctype enc_types[] = {
797 #ifdef ENCTYPE_ARCFOUR_HMAC
798                 ENCTYPE_ARCFOUR_HMAC,
799 #endif 
800                 ENCTYPE_DES_CBC_MD5, 
801                 ENCTYPE_DES_CBC_CRC, 
802                 ENCTYPE_NULL};
803
804         initialize_krb5_error_table();
805         retval = krb5_init_context(&context);
806         if (retval) {
807                 DEBUG(1,("cli_krb5_get_ticket: krb5_init_context failed (%s)\n", 
808                          error_message(retval)));
809                 goto failed;
810         }
811
812         if (time_offset != 0) {
813                 krb5_set_real_time(context, time(NULL) + time_offset, 0);
814         }
815
816         if ((retval = krb5_cc_resolve(context, ccname ?
817                         ccname : krb5_cc_default_name(context), &ccdef))) {
818                 DEBUG(1,("cli_krb5_get_ticket: krb5_cc_default failed (%s)\n",
819                          error_message(retval)));
820                 goto failed;
821         }
822
823         if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
824                 DEBUG(1,("cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s)\n",
825                          error_message(retval)));
826                 goto failed;
827         }
828
829         if ((retval = ads_krb5_mk_req(context, 
830                                         &auth_context, 
831                                         AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
832                                         principal,
833                                         ccdef, &packet,
834                                         tgs_expire))) {
835                 goto failed;
836         }
837
838         get_krb5_smb_session_key(context, auth_context, session_key_krb5, False);
839
840         *ticket = data_blob(packet.data, packet.length);
841
842         kerberos_free_data_contents(context, &packet); 
843
844 failed:
845
846         if ( context ) {
847                 if (ccdef)
848                         krb5_cc_close(context, ccdef);
849                 if (auth_context)
850                         krb5_auth_con_free(context, auth_context);
851                 krb5_free_context(context);
852         }
853                 
854         return retval;
855 }
856
857  bool get_krb5_smb_session_key(krb5_context context, krb5_auth_context auth_context, DATA_BLOB *session_key, bool remote)
858  {
859         krb5_keyblock *skey;
860         krb5_error_code err;
861         bool ret = False;
862
863         if (remote)
864                 err = krb5_auth_con_getremotesubkey(context, auth_context, &skey);
865         else
866                 err = krb5_auth_con_getlocalsubkey(context, auth_context, &skey);
867         if (err == 0 && skey != NULL) {
868                 DEBUG(10, ("Got KRB5 session key of length %d\n",  (int)KRB5_KEY_LENGTH(skey)));
869                 *session_key = data_blob(KRB5_KEY_DATA(skey), KRB5_KEY_LENGTH(skey));
870                 dump_data_pw("KRB5 Session Key:\n", session_key->data, session_key->length);
871
872                 ret = True;
873
874                 krb5_free_keyblock(context, skey);
875         } else {
876                 DEBUG(10, ("KRB5 error getting session key %d\n", err));
877         }
878
879         return ret;
880  }
881
882
883 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
884  const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i );
885
886  const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
887 {
888         static krb5_data kdata;
889
890         kdata.data = (char *)krb5_principal_get_comp_string(context, principal, i);
891         kdata.length = strlen((const char *)kdata.data);
892         return &kdata;
893 }
894 #endif
895
896  krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
897 {
898 #if defined(HAVE_KRB5_KT_FREE_ENTRY)
899         return krb5_kt_free_entry(context, kt_entry);
900 #elif defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
901         return krb5_free_keytab_entry_contents(context, kt_entry);
902 #else
903 #error UNKNOWN_KT_FREE_FUNCTION
904 #endif
905 }
906
907  void smb_krb5_checksum_from_pac_sig(krb5_checksum *cksum,
908                                      struct PAC_SIGNATURE_DATA *sig)
909 {
910 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
911         cksum->cksumtype        = (krb5_cksumtype)sig->type;
912         cksum->checksum.length  = sig->signature.length;
913         cksum->checksum.data    = sig->signature.data;
914 #else
915         cksum->checksum_type    = (krb5_cksumtype)sig->type;
916         cksum->length           = sig->signature.length;
917         cksum->contents         = sig->signature.data;
918 #endif
919 }
920
921  krb5_error_code smb_krb5_verify_checksum(krb5_context context,
922                                           const krb5_keyblock *keyblock,
923                                          krb5_keyusage usage,
924                                          krb5_checksum *cksum,
925                                          uint8 *data,
926                                          size_t length)
927 {
928         krb5_error_code ret;
929
930         /* verify the checksum */
931
932         /* welcome to the wonderful world of samba's kerberos abstraction layer:
933          * 
934          * function                     heimdal 0.6.1rc3        heimdal 0.7     MIT krb 1.4.2
935          * -----------------------------------------------------------------------------
936          * krb5_c_verify_checksum       -                       works           works
937          * krb5_verify_checksum         works (6 args)          works (6 args)  broken (7 args) 
938          */
939
940 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
941         {
942                 krb5_boolean checksum_valid = False;
943                 krb5_data input;
944
945                 input.data = (char *)data;
946                 input.length = length;
947
948                 ret = krb5_c_verify_checksum(context, 
949                                              keyblock, 
950                                              usage,
951                                              &input, 
952                                              cksum,
953                                              &checksum_valid);
954                 if (ret) {
955                         DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n", 
956                                 error_message(ret)));
957                         return ret;
958                 }
959
960                 if (!checksum_valid)
961                         ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
962         }
963
964 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
965
966         /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
967          * without enctype and it ignores any key_usage types - Guenther */
968
969         {
970
971                 krb5_crypto crypto;
972                 ret = krb5_crypto_init(context,
973                                        keyblock,
974                                        0,
975                                        &crypto);
976                 if (ret) {
977                         DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n", 
978                                 error_message(ret)));
979                         return ret;
980                 }
981
982                 ret = krb5_verify_checksum(context,
983                                            crypto,
984                                            usage,
985                                            data,
986                                            length,
987                                            cksum);
988
989                 krb5_crypto_destroy(context, crypto);
990         }
991
992 #else
993 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
994 #endif
995
996         return ret;
997 }
998
999  time_t get_authtime_from_tkt(krb5_ticket *tkt)
1000 {
1001 #if defined(HAVE_KRB5_TKT_ENC_PART2)
1002         return tkt->enc_part2->times.authtime;
1003 #else
1004         return tkt->ticket.authtime;
1005 #endif
1006 }
1007
1008 #ifdef HAVE_KRB5_DECODE_AP_REQ  /* Heimdal */
1009 static int get_kvno_from_ap_req(krb5_ap_req *ap_req)
1010 {
1011 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
1012         if (ap_req->ticket->enc_part.kvno)
1013                 return ap_req->ticket->enc_part.kvno;
1014 #else /* Heimdal */
1015         if (ap_req->ticket.enc_part.kvno) 
1016                 return *ap_req->ticket.enc_part.kvno;
1017 #endif
1018         return 0;
1019 }
1020
1021 static krb5_enctype get_enctype_from_ap_req(krb5_ap_req *ap_req)
1022 {
1023 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
1024         return ap_req->ticket.enc_part.etype;
1025 #else /* MIT */
1026         return ap_req->ticket->enc_part.enctype;
1027 #endif
1028 }
1029 #endif  /* HAVE_KRB5_DECODE_AP_REQ */
1030
1031 static krb5_error_code
1032 get_key_from_keytab(krb5_context context,
1033                     krb5_const_principal server,
1034                     krb5_enctype enctype,
1035                     krb5_kvno kvno,
1036                     krb5_keyblock **out_key)
1037 {
1038         krb5_keytab_entry entry;
1039         krb5_error_code ret;
1040         krb5_keytab keytab;
1041         char *name = NULL;
1042         krb5_keyblock *keyp;
1043
1044         /* We have to open a new keytab handle here, as MIT does
1045            an implicit open/getnext/close on krb5_kt_get_entry. We
1046            may be in the middle of a keytab enumeration when this is
1047            called. JRA. */
1048
1049         ret = smb_krb5_open_keytab(context, NULL, False, &keytab);
1050         if (ret) {
1051                 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
1052                 return ret;
1053         }
1054
1055         if ( DEBUGLEVEL >= 10 ) {
1056                 if (smb_krb5_unparse_name(context, server, &name) == 0) {
1057                         DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n", 
1058                                 kvno, enctype, name));
1059                         SAFE_FREE(name);
1060                 }
1061         }
1062
1063         ret = krb5_kt_get_entry(context,
1064                                 keytab,
1065                                 server,
1066                                 kvno,
1067                                 enctype,
1068                                 &entry);
1069
1070         if (ret) {
1071                 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret)));
1072                 goto out;
1073         }
1074
1075         keyp = KRB5_KT_KEY(&entry);
1076
1077         ret = krb5_copy_keyblock(context, keyp, out_key);
1078         if (ret) {
1079                 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret)));
1080                 goto out;
1081         }
1082                 
1083         smb_krb5_kt_free_entry(context, &entry);
1084         
1085 out:    
1086         krb5_kt_close(context, keytab);
1087         return ret;
1088 }
1089
1090 /* Prototypes */
1091
1092  krb5_error_code smb_krb5_get_keyinfo_from_ap_req(krb5_context context, 
1093                                                  const krb5_data *inbuf, 
1094                                                  krb5_kvno *kvno, 
1095                                                  krb5_enctype *enctype)
1096 {
1097 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1098         {
1099                 krb5_error_code ret;
1100                 krb5_ap_req ap_req;
1101                 
1102                 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
1103                 if (ret)
1104                         return ret;
1105
1106                 *kvno = get_kvno_from_ap_req(&ap_req);
1107                 *enctype = get_enctype_from_ap_req(&ap_req);
1108
1109                 free_AP_REQ(&ap_req);
1110                 return 0;
1111         }
1112 #endif
1113
1114         /* Possibly not an appropriate error code. */
1115         return KRB5KDC_ERR_BADOPTION;
1116 }
1117
1118  krb5_error_code krb5_rd_req_return_keyblock_from_keytab(krb5_context context,
1119                                                         krb5_auth_context *auth_context,
1120                                                         const krb5_data *inbuf,
1121                                                         krb5_const_principal server,
1122                                                         krb5_keytab keytab,
1123                                                         krb5_flags *ap_req_options,
1124                                                         krb5_ticket **ticket, 
1125                                                         krb5_keyblock **keyblock)
1126 {
1127         krb5_error_code ret;
1128         krb5_kvno kvno;
1129         krb5_enctype enctype;
1130         krb5_keyblock *local_keyblock;
1131
1132         ret = krb5_rd_req(context, 
1133                           auth_context, 
1134                           inbuf, 
1135                           server, 
1136                           keytab, 
1137                           ap_req_options, 
1138                           ticket);
1139         if (ret) {
1140                 return ret;
1141         }
1142         
1143 #ifdef KRB5_TICKET_HAS_KEYINFO
1144         enctype = (*ticket)->enc_part.enctype;
1145         kvno = (*ticket)->enc_part.kvno;
1146 #else
1147         ret = smb_krb5_get_keyinfo_from_ap_req(context, inbuf, &kvno, &enctype);
1148         if (ret) {
1149                 return ret;
1150         }
1151 #endif
1152
1153         ret = get_key_from_keytab(context, 
1154                                   server,
1155                                   enctype,
1156                                   kvno,
1157                                   &local_keyblock);
1158         if (ret) {
1159                 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1160                 goto out;
1161         }
1162
1163 out:
1164         if (ret && local_keyblock != NULL) {
1165                 krb5_free_keyblock(context, local_keyblock);
1166         } else {
1167                 *keyblock = local_keyblock;
1168         }
1169
1170         return ret;
1171 }
1172
1173  krb5_error_code smb_krb5_parse_name_norealm(krb5_context context, 
1174                                             const char *name, 
1175                                             krb5_principal *principal)
1176 {
1177 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1178         return smb_krb5_parse_name_norealm_conv(context, name, principal);
1179 #endif
1180
1181         /* we are cheating here because parse_name will in fact set the realm.
1182          * We don't care as the only caller of smb_krb5_parse_name_norealm
1183          * ignores the realm anyway when calling
1184          * smb_krb5_principal_compare_any_realm later - Guenther */
1185
1186         return smb_krb5_parse_name(context, name, principal);
1187 }
1188
1189  bool smb_krb5_principal_compare_any_realm(krb5_context context, 
1190                                           krb5_const_principal princ1, 
1191                                           krb5_const_principal princ2)
1192 {
1193 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1194
1195         return krb5_principal_compare_any_realm(context, princ1, princ2);
1196
1197 /* krb5_princ_size is a macro in MIT */
1198 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1199
1200         int i, len1, len2;
1201         const krb5_data *p1, *p2;
1202
1203         len1 = krb5_princ_size(context, princ1);
1204         len2 = krb5_princ_size(context, princ2);
1205
1206         if (len1 != len2)
1207                 return False;
1208
1209         for (i = 0; i < len1; i++) {
1210
1211                 p1 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ1), i);
1212                 p2 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ2), i);
1213
1214                 if (p1->length != p2->length || memcmp(p1->data, p2->data, p1->length))
1215                         return False;
1216         }
1217
1218         return True;
1219 #else
1220 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1221 #endif
1222 }
1223
1224  krb5_error_code smb_krb5_renew_ticket(const char *ccache_string,       /* FILE:/tmp/krb5cc_0 */
1225                                        const char *client_string,       /* gd@BER.SUSE.DE */
1226                                        const char *service_string,      /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1227                                        time_t *expire_time)
1228 {
1229         krb5_error_code ret;
1230         krb5_context context = NULL;
1231         krb5_ccache ccache = NULL;
1232         krb5_principal client = NULL;
1233         krb5_creds creds, creds_in, *creds_out = NULL;
1234
1235         ZERO_STRUCT(creds);
1236         ZERO_STRUCT(creds_in);
1237
1238         initialize_krb5_error_table();
1239         ret = krb5_init_context(&context);
1240         if (ret) {
1241                 goto done;
1242         }
1243
1244         if (!ccache_string) {
1245                 ccache_string = krb5_cc_default_name(context);
1246         }
1247
1248         if (!ccache_string) {
1249                 ret = EINVAL;
1250                 goto done;
1251         }
1252
1253         DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
1254
1255         /* FIXME: we should not fall back to defaults */
1256         ret = krb5_cc_resolve(context, CONST_DISCARD(char *, ccache_string), &ccache);
1257         if (ret) {
1258                 goto done;
1259         }
1260
1261         if (client_string) {
1262                 ret = smb_krb5_parse_name(context, client_string, &client);
1263                 if (ret) {
1264                         goto done;
1265                 }
1266         } else {
1267                 ret = krb5_cc_get_principal(context, ccache, &client);
1268                 if (ret) {
1269                         goto done;
1270                 }
1271         }
1272
1273 #ifdef HAVE_KRB5_GET_RENEWED_CREDS      /* MIT */
1274         {
1275                 ret = krb5_get_renewed_creds(context, &creds, client, ccache, CONST_DISCARD(char *, service_string));
1276                 if (ret) {
1277                         DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1278                         goto done;
1279                 }
1280         }
1281 #elif defined(HAVE_KRB5_GET_KDC_CRED)   /* Heimdal */
1282         {
1283                 krb5_kdc_flags flags;
1284                 krb5_realm *client_realm = NULL;
1285
1286                 ret = krb5_copy_principal(context, client, &creds_in.client);
1287                 if (ret) {
1288                         goto done;
1289                 }
1290
1291                 if (service_string) {
1292                         ret = smb_krb5_parse_name(context, service_string, &creds_in.server);
1293                         if (ret) { 
1294                                 goto done;
1295                         }
1296                 } else {
1297                         /* build tgt service by default */
1298                         client_realm = krb5_princ_realm(context, creds_in.client);
1299                         if (!client_realm) {
1300                                 ret = ENOMEM;
1301                                 goto done;
1302                         }
1303                         ret = krb5_make_principal(context, &creds_in.server, *client_realm, KRB5_TGS_NAME, *client_realm, NULL);
1304                         if (ret) {
1305                                 goto done;
1306                         }
1307                 }
1308
1309                 flags.i = 0;
1310                 flags.b.renewable = flags.b.renew = True;
1311
1312                 ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &creds_in, &creds_out);
1313                 if (ret) {
1314                         DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1315                         goto done;
1316                 }
1317
1318                 creds = *creds_out;
1319         }
1320 #else
1321 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1322 #endif
1323
1324         /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1325         ret = krb5_cc_initialize(context, ccache, client);
1326         if (ret) {
1327                 goto done;
1328         }
1329         
1330         ret = krb5_cc_store_cred(context, ccache, &creds);
1331
1332         if (expire_time) {
1333                 *expire_time = (time_t) creds.times.endtime;
1334         }
1335
1336 done:
1337         krb5_free_cred_contents(context, &creds_in);
1338
1339         if (creds_out) {
1340                 krb5_free_creds(context, creds_out);
1341         } else {
1342                 krb5_free_cred_contents(context, &creds);
1343         }
1344
1345         if (client) {
1346                 krb5_free_principal(context, client);
1347         }
1348         if (ccache) {
1349                 krb5_cc_close(context, ccache);
1350         }
1351         if (context) {
1352                 krb5_free_context(context);
1353         }
1354
1355         return ret;
1356 }
1357
1358  krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr)
1359 {
1360         krb5_error_code ret = 0;
1361         if (addr == NULL) {
1362                 return ret;
1363         }
1364 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1365         krb5_free_addresses(context, addr->addrs);
1366 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1367         ret = krb5_free_addresses(context, addr->addrs);
1368         SAFE_FREE(addr->addrs);
1369 #endif
1370         SAFE_FREE(addr);
1371         addr = NULL;
1372         return ret;
1373 }
1374
1375  krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr)
1376 {
1377         krb5_error_code ret = 0;
1378         nstring buf;
1379 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1380         krb5_address **addrs = NULL;
1381 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1382         krb5_addresses *addrs = NULL;
1383 #endif
1384
1385         *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
1386         if (*kerb_addr == NULL) {
1387                 return ENOMEM;
1388         }
1389
1390         put_name(buf, global_myname(), ' ', 0x20);
1391
1392 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1393         {
1394                 int num_addr = 2;
1395
1396                 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
1397                 if (addrs == NULL) {
1398                         SAFE_FREE(kerb_addr);
1399                         return ENOMEM;
1400                 }
1401
1402                 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
1403
1404                 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1405                 if (addrs[0] == NULL) {
1406                         SAFE_FREE(addrs);
1407                         SAFE_FREE(kerb_addr);
1408                         return ENOMEM;
1409                 }
1410
1411                 addrs[0]->magic = KV5M_ADDRESS;
1412                 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
1413                 addrs[0]->length = MAX_NETBIOSNAME_LEN;
1414                 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
1415                 if (addrs[0]->contents == NULL) {
1416                         SAFE_FREE(addrs[0]);
1417                         SAFE_FREE(addrs);
1418                         SAFE_FREE(kerb_addr);
1419                         return ENOMEM;
1420                 }
1421
1422                 memcpy(addrs[0]->contents, buf, addrs[0]->length);
1423
1424                 addrs[1] = NULL;
1425         }
1426 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1427         {
1428                 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
1429                 if (addrs == NULL) {
1430                         SAFE_FREE(kerb_addr);
1431                         return ENOMEM;
1432                 }
1433
1434                 memset(addrs, 0, sizeof(krb5_addresses));
1435
1436                 addrs->len = 1;
1437                 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1438                 if (addrs->val == NULL) {
1439                         SAFE_FREE(addrs);
1440                         SAFE_FREE(kerb_addr);
1441                         return ENOMEM;
1442                 }
1443
1444                 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
1445                 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
1446                 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
1447                 if (addrs->val[0].address.data == NULL) {
1448                         SAFE_FREE(addrs->val);
1449                         SAFE_FREE(addrs);
1450                         SAFE_FREE(kerb_addr);
1451                         return ENOMEM;
1452                 }
1453
1454                 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
1455         }
1456 #else
1457 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1458 #endif
1459         (*kerb_addr)->addrs = addrs;
1460
1461         return ret;
1462 }
1463
1464  void smb_krb5_free_error(krb5_context context, krb5_error *krberror)
1465 {
1466 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1467         krb5_free_error_contents(context, krberror);
1468 #else /* MIT */
1469         krb5_free_error(context, krberror);
1470 #endif
1471 }
1472
1473  krb5_error_code handle_krberror_packet(krb5_context context,
1474                                         krb5_data *packet)
1475 {
1476         krb5_error_code ret;
1477         bool got_error_code = False;
1478
1479         DEBUG(10,("handle_krberror_packet: got error packet\n"));
1480         
1481 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1482         {
1483                 krb5_error krberror;
1484
1485                 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1486                         DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n", 
1487                                 error_message(ret)));
1488                         return ret;
1489                 }
1490
1491                 if (krberror.e_data == NULL || krberror.e_data->data == NULL) {
1492                         ret = (krb5_error_code) krberror.error_code;
1493                         got_error_code = True;
1494                 }
1495
1496                 smb_krb5_free_error(context, &krberror);
1497         }
1498 #else /* MIT */
1499         {
1500                 krb5_error *krberror;
1501
1502                 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1503                         DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n", 
1504                                 error_message(ret)));
1505                         return ret;
1506                 }
1507
1508                 if (krberror->e_data.data == NULL) {
1509                         ret = ERROR_TABLE_BASE_krb5 + (krb5_error_code) krberror->error;
1510                         got_error_code = True;
1511                 }
1512                 smb_krb5_free_error(context, krberror);
1513         }
1514 #endif
1515         if (got_error_code) {
1516                 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n", 
1517                         error_message(ret), ret));
1518         }
1519         return ret;
1520 }
1521
1522  krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context,
1523                                             krb5_get_init_creds_opt **opt)
1524 {
1525 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1526         /* Heimdal or modern MIT version */
1527         return krb5_get_init_creds_opt_alloc(context, opt);
1528 #else
1529         /* Historical MIT version */
1530         krb5_get_init_creds_opt *my_opt;
1531
1532         *opt = NULL;
1533
1534         if ((my_opt = SMB_MALLOC_P(krb5_get_init_creds_opt)) == NULL) {
1535                 return ENOMEM;
1536         }
1537
1538         krb5_get_init_creds_opt_init(my_opt);
1539
1540         *opt =  my_opt;
1541         return 0;
1542 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC  */
1543 }
1544
1545  void smb_krb5_get_init_creds_opt_free(krb5_context context,
1546                                 krb5_get_init_creds_opt *opt)
1547 {
1548 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1549
1550 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1551         /* Modern MIT or Heimdal version */
1552         krb5_get_init_creds_opt_free(context, opt);
1553 #else
1554         /* Heimdal version */
1555         krb5_get_init_creds_opt_free(opt);
1556 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1557
1558 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1559         /* Historical MIT version */
1560         SAFE_FREE(opt);
1561         opt = NULL;
1562 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1563 }
1564
1565  krb5_enctype smb_get_enctype_from_kt_entry(krb5_keytab_entry *kt_entry)
1566 {
1567         return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
1568 }
1569
1570
1571 /* caller needs to free etype_s */
1572  krb5_error_code smb_krb5_enctype_to_string(krb5_context context, 
1573                                             krb5_enctype enctype, 
1574                                             char **etype_s)
1575 {
1576 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1577         return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
1578 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1579         char buf[256];
1580         krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
1581         if (ret) {
1582                 return ret;
1583         }
1584         *etype_s = SMB_STRDUP(buf);
1585         if (!*etype_s) {
1586                 return ENOMEM;
1587         }
1588         return ret;
1589 #else
1590 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1591 #endif
1592 }
1593
1594  krb5_error_code smb_krb5_mk_error(krb5_context context,
1595                                 krb5_error_code error_code,
1596                                 const krb5_principal server,
1597                                 krb5_data *reply)
1598 {
1599 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1600         /*
1601          * The MIT interface is *terrible*.
1602          * We have to construct this ourselves...
1603          */
1604         krb5_error e;
1605
1606         memset(&e, 0, sizeof(e));
1607         krb5_us_timeofday(context, &e.stime, &e.susec);
1608         e.server = server;
1609 #if defined(krb5_err_base)
1610         e.error = error_code - krb5_err_base;
1611 #elif defined(ERROR_TABLE_BASE_krb5)
1612         e.error = error_code - ERROR_TABLE_BASE_krb5;
1613 #else
1614         e.error = error_code; /* Almost certainly wrong, but what can we do... ? */
1615 #endif
1616
1617         return krb5_mk_error(context, &e, reply);
1618 #else /* Heimdal. */
1619         return krb5_mk_error(context,
1620                                 error_code,
1621                                 NULL,
1622                                 NULL, /* e_data */
1623                                 NULL,
1624                                 server,
1625                                 NULL,
1626                                 NULL,
1627                                 reply);
1628 #endif
1629 }
1630
1631 /**********************************************************************
1632  * Open a krb5 keytab with flags, handles readonly or readwrite access and
1633  * allows to process non-default keytab names.
1634  * @param context krb5_context 
1635  * @param keytab_name_req string
1636  * @param write_access bool if writable keytab is required
1637  * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1638  * @return krb5_error_code
1639 **********************************************************************/
1640
1641 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1642 #ifndef MAX_KEYTAB_NAME_LEN
1643 #define MAX_KEYTAB_NAME_LEN 1100
1644 #endif
1645
1646  krb5_error_code smb_krb5_open_keytab(krb5_context context,
1647                                       const char *keytab_name_req,
1648                                       bool write_access,
1649                                       krb5_keytab *keytab)
1650 {
1651         krb5_error_code ret = 0;
1652         TALLOC_CTX *mem_ctx;
1653         char keytab_string[MAX_KEYTAB_NAME_LEN];
1654         char *kt_str = NULL;
1655         bool found_valid_name = False;
1656         const char *pragma = "FILE";
1657         const char *tmp = NULL;
1658
1659         if (!write_access && !keytab_name_req) {
1660                 /* caller just wants to read the default keytab readonly, so be it */
1661                 return krb5_kt_default(context, keytab);
1662         }
1663
1664         mem_ctx = talloc_init("smb_krb5_open_keytab");
1665         if (!mem_ctx) {
1666                 return ENOMEM;
1667         }
1668
1669 #ifdef HAVE_WRFILE_KEYTAB 
1670         if (write_access) {
1671                 pragma = "WRFILE";
1672         }
1673 #endif
1674
1675         if (keytab_name_req) {
1676
1677                 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1678                         ret = KRB5_CONFIG_NOTENUFSPACE;
1679                         goto out;
1680                 }
1681
1682                 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) || 
1683                     (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
1684                         tmp = keytab_name_req;
1685                         goto resolve;
1686                 }
1687
1688                 if (keytab_name_req[0] != '/') {
1689                         ret = KRB5_KT_BADNAME;
1690                         goto out;
1691                 }
1692
1693                 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1694                 if (!tmp) {
1695                         ret = ENOMEM;
1696                         goto out;
1697                 }
1698
1699                 goto resolve;
1700         }
1701
1702         /* we need to handle more complex keytab_strings, like:
1703          * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1704
1705         ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1706         if (ret) {
1707                 goto out;
1708         }
1709
1710         DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1711
1712         tmp = talloc_strdup(mem_ctx, keytab_string);
1713         if (!tmp) {
1714                 ret = ENOMEM;
1715                 goto out;
1716         }
1717
1718         if (strncmp(tmp, "ANY:", 4) == 0) {
1719                 tmp += 4;
1720         }
1721
1722         memset(&keytab_string, '\0', sizeof(keytab_string));
1723
1724         while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1725                 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1726                         found_valid_name = True;
1727                         tmp = kt_str;
1728                         tmp += 7;
1729                 }
1730
1731                 if (strncmp(kt_str, "FILE:", 5) == 0) {
1732                         found_valid_name = True;
1733                         tmp = kt_str;
1734                         tmp += 5;
1735                 }
1736
1737                 if (found_valid_name) {
1738                         if (tmp[0] != '/') {
1739                                 ret = KRB5_KT_BADNAME;
1740                                 goto out;
1741                         }
1742
1743                         tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1744                         if (!tmp) {
1745                                 ret = ENOMEM;
1746                                 goto out;
1747                         }
1748                         break;
1749                 }
1750         }
1751
1752         if (!found_valid_name) {
1753                 ret = KRB5_KT_UNKNOWN_TYPE;
1754                 goto out;
1755         }
1756
1757  resolve:
1758         DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1759         ret = krb5_kt_resolve(context, tmp, keytab);
1760
1761  out:
1762         TALLOC_FREE(mem_ctx);
1763         return ret;
1764 }
1765
1766 krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
1767                                      krb5_context context,
1768                                      krb5_keytab keytab,
1769                                      const char **keytab_name)
1770 {
1771         char keytab_string[MAX_KEYTAB_NAME_LEN];
1772         krb5_error_code ret = 0;
1773
1774         ret = krb5_kt_get_name(context, keytab,
1775                                keytab_string, MAX_KEYTAB_NAME_LEN - 2);
1776         if (ret) {
1777                 return ret;
1778         }
1779
1780         *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1781         if (!*keytab_name) {
1782                 return ENOMEM;
1783         }
1784
1785         return ret;
1786 }
1787
1788 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY)
1789 /**************************************************************
1790 Routine: ads_krb5_get_fwd_ticket
1791  Description:
1792     When a service ticket is flagged as trusted
1793     for delegation we should provide a forwardable
1794     ticket so that the remote host can act on our
1795     behalf.  This is done by taking the 2nd forwardable
1796     TGT and storing it in the GSS-API authenticator
1797     "checksum".  This routine will populate
1798     the krb5_data authenticator with this TGT.
1799  Parameters:
1800     krb5_context context: The kerberos context for this authentication.
1801     krb5_auth_context:    The authentication context.
1802     krb5_creds *credsp:   The ticket credentials (AS-REP).
1803     krb5_ccache ccache:   The credentials cache.
1804     krb5_data &authenticator: The checksum field that will store the TGT, and
1805      authenticator.data must be freed by the caller.
1806
1807  Returns:
1808     krb5_error_code: 0 if no errors, otherwise set.
1809 **************************************************************/
1810
1811 static krb5_error_code ads_krb5_get_fwd_ticket( krb5_context context,
1812                                          krb5_auth_context *auth_context,
1813                                          krb5_creds *credsp,
1814                                          krb5_ccache ccache,
1815                                          krb5_data *authenticator)
1816 {
1817         krb5_data fwdData;
1818         krb5_error_code retval = 0;
1819         char *pChksum = NULL;
1820         char *p = NULL;
1821
1822         ZERO_STRUCT(fwdData);
1823         ZERO_STRUCTP(authenticator);
1824
1825         retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */
1826                                 *auth_context,  /* Authentication context [in] */
1827                                 CONST_DISCARD(char *, KRB5_TGS_NAME),  /* Ticket service name ("krbtgt") [in] */
1828                                 credsp->client, /* Client principal for the tgt [in] */
1829                                 credsp->server, /* Server principal for the tgt [in] */
1830                                 ccache,         /* Credential cache to use for storage [in] */
1831                                 1,              /* Turn on for "Forwardable ticket" [in] */
1832                                 &fwdData );     /* Resulting response [out] */
1833
1834
1835         if (retval) {
1836                 DEBUG(1,("ads_krb5_get_fwd_ticket: krb5_fwd_tgt_creds failed (%s)\n", 
1837                         error_message(retval)));
1838                 goto out;
1839         }
1840
1841         if ((unsigned int)GSSAPI_CHECKSUM_SIZE + (unsigned int)fwdData.length <
1842                 (unsigned int)GSSAPI_CHECKSUM_SIZE) {
1843                 retval = EINVAL;
1844                 goto out;
1845         }
1846
1847         /* We're going to allocate a gssChecksum structure with a little
1848            extra data the length of the kerberos credentials length
1849            (APPLICATION 22) so that we can pack it on the end of the structure.
1850         */
1851
1852         pChksum = (char *)SMB_MALLOC(GSSAPI_CHECKSUM_SIZE + fwdData.length );
1853         if (!pChksum) {
1854                 retval = ENOMEM;
1855                 goto out;
1856         }
1857
1858         p = pChksum;
1859
1860         SIVAL(p, 0, GSSAPI_BNDLENGTH);
1861         p += 4;
1862
1863         /* Zero out the bindings fields */
1864         memset(p, '\0', GSSAPI_BNDLENGTH );
1865         p += GSSAPI_BNDLENGTH;
1866
1867         SIVAL(p, 0, GSS_C_DELEG_FLAG );
1868         p += 4;
1869         SSVAL(p, 0, 1 );
1870         p += 2;
1871         SSVAL(p, 0, fwdData.length );
1872         p += 2;
1873
1874         /* Migrate the kerberos KRB_CRED data to the checksum delegation */
1875         memcpy(p, fwdData.data, fwdData.length );
1876         p += fwdData.length;
1877
1878         /* We need to do this in order to allow our GSS-API  */
1879         retval = krb5_auth_con_set_req_cksumtype( context, *auth_context, GSSAPI_CHECKSUM );
1880         if (retval) {
1881                 goto out;
1882         }
1883
1884         /* We now have a service ticket, now turn it into an AP-REQ. */
1885         authenticator->length = fwdData.length + GSSAPI_CHECKSUM_SIZE;
1886
1887         /* Caller should call free() when they're done with this. */
1888         authenticator->data = (char *)pChksum;
1889
1890   out:
1891
1892         /* Remove that input data, we never needed it anyway. */
1893         if (fwdData.length > 0) {
1894                 krb5_free_data_contents( context, &fwdData );
1895         }
1896
1897         return retval;
1898 }
1899 #endif
1900
1901 #else /* HAVE_KRB5 */
1902  /* this saves a few linking headaches */
1903  int cli_krb5_get_ticket(const char *principal, time_t time_offset, 
1904                         DATA_BLOB *ticket, DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
1905                         const char *ccname, time_t *tgs_expire) 
1906 {
1907          DEBUG(0,("NO KERBEROS SUPPORT\n"));
1908          return 1;
1909 }
1910
1911 #endif