Fix more entries with back slashes that need to use literal formatting
[Samba/bjacke.git] / source3 / libsmb / clikrb5.c
blobc289740ab28536f204125a4af2a9024af40b980d
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
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.
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.
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/>.
23 #define KRB5_PRIVATE 1 /* this file uses PRIVATE interfaces! */
24 #define KRB5_DEPRECATED 1 /* this file uses DEPRECATED interfaces! */
26 #include "includes.h"
28 #ifdef HAVE_KRB5
30 #ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE /* Heimdal */
31 #define KRB5_KEY_TYPE(k) ((k)->keytype)
32 #define KRB5_KEY_LENGTH(k) ((k)->keyvalue.length)
33 #define KRB5_KEY_DATA(k) ((k)->keyvalue.data)
34 #else /* MIT */
35 #define KRB5_KEY_TYPE(k) ((k)->enctype)
36 #define KRB5_KEY_LENGTH(k) ((k)->length)
37 #define KRB5_KEY_DATA(k) ((k)->contents)
38 #endif /* HAVE_KRB5_KEYBLOCK_KEYVALUE */
40 /**************************************************************
41 Wrappers around kerberos string functions that convert from
42 utf8 -> unix charset and vica versa.
43 **************************************************************/
45 /**************************************************************
46 krb5_parse_name that takes a UNIX charset.
47 **************************************************************/
49 krb5_error_code smb_krb5_parse_name(krb5_context context,
50 const char *name, /* in unix charset */
51 krb5_principal *principal)
53 krb5_error_code ret;
54 char *utf8_name;
56 if (push_utf8_allocate(&utf8_name, name) == (size_t)-1) {
57 return ENOMEM;
60 ret = krb5_parse_name(context, utf8_name, principal);
61 SAFE_FREE(utf8_name);
62 return ret;
65 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
66 /**************************************************************
67 krb5_parse_name_norealm that takes a UNIX charset.
68 **************************************************************/
70 static krb5_error_code smb_krb5_parse_name_norealm_conv(krb5_context context,
71 const char *name, /* in unix charset */
72 krb5_principal *principal)
74 krb5_error_code ret;
75 char *utf8_name;
77 *principal = NULL;
78 if (push_utf8_allocate(&utf8_name, name) == (size_t)-1) {
79 return ENOMEM;
82 ret = krb5_parse_name_norealm(context, utf8_name, principal);
83 SAFE_FREE(utf8_name);
84 return ret;
86 #endif
88 /**************************************************************
89 krb5_parse_name that returns a UNIX charset name. Must
90 be freed with normal free() call.
91 **************************************************************/
93 krb5_error_code smb_krb5_unparse_name(krb5_context context,
94 krb5_const_principal principal,
95 char **unix_name)
97 krb5_error_code ret;
98 char *utf8_name;
100 *unix_name = NULL;
101 ret = krb5_unparse_name(context, principal, &utf8_name);
102 if (ret) {
103 return ret;
106 if (pull_utf8_allocate(unix_name, utf8_name)==-1) {
107 krb5_free_unparsed_name(context, utf8_name);
108 return ENOMEM;
110 krb5_free_unparsed_name(context, utf8_name);
111 return 0;
114 #ifndef HAVE_KRB5_SET_REAL_TIME
116 * This function is not in the Heimdal mainline.
118 krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
120 krb5_error_code ret;
121 int32_t sec, usec;
123 ret = krb5_us_timeofday(context, &sec, &usec);
124 if (ret)
125 return ret;
127 context->kdc_sec_offset = seconds - sec;
128 context->kdc_usec_offset = microseconds - usec;
130 return 0;
132 #endif
134 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
136 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
138 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
139 * to krb5_set_default_tgs_ktypes. See
140 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
142 * If the MIT libraries are not exporting internal symbols, we will end up in
143 * this branch, which is correct. Otherwise we will continue to use the
144 * internal symbol
146 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
148 return krb5_set_default_tgs_enctypes(ctx, enc);
151 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
153 /* Heimdal */
154 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
156 return krb5_set_default_in_tkt_etypes(ctx, enc);
159 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
161 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
163 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
164 /* HEIMDAL */
165 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
167 memset(pkaddr, '\0', sizeof(krb5_address));
168 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
169 if (paddr->ss_family == AF_INET6) {
170 pkaddr->addr_type = KRB5_ADDRESS_INET6;
171 pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
172 pkaddr->address.data = (char *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
173 return true;
175 #endif
176 if (paddr->ss_family == AF_INET) {
177 pkaddr->addr_type = KRB5_ADDRESS_INET;
178 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
179 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
180 return true;
182 return false;
184 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
185 /* MIT */
186 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
188 memset(pkaddr, '\0', sizeof(krb5_address));
189 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
190 if (paddr->ss_family == AF_INET6) {
191 pkaddr->addrtype = ADDRTYPE_INET6;
192 pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
193 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
194 return true;
196 #endif
197 if (paddr->ss_family == AF_INET) {
198 pkaddr->addrtype = ADDRTYPE_INET;
199 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
200 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
201 return true;
203 return false;
205 #else
206 #error UNKNOWN_ADDRTYPE
207 #endif
209 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
210 int create_kerberos_key_from_string_direct(krb5_context context,
211 krb5_principal host_princ,
212 krb5_data *password,
213 krb5_keyblock *key,
214 krb5_enctype enctype)
216 int ret;
217 krb5_data salt;
218 krb5_encrypt_block eblock;
220 ret = krb5_principal2salt(context, host_princ, &salt);
221 if (ret) {
222 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
223 return ret;
225 krb5_use_enctype(context, &eblock, enctype);
226 ret = krb5_string_to_key(context, &eblock, key, password, &salt);
227 SAFE_FREE(salt.data);
228 return ret;
230 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
231 int create_kerberos_key_from_string_direct(krb5_context context,
232 krb5_principal host_princ,
233 krb5_data *password,
234 krb5_keyblock *key,
235 krb5_enctype enctype)
237 int ret;
238 krb5_salt salt;
240 ret = krb5_get_pw_salt(context, host_princ, &salt);
241 if (ret) {
242 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
243 return ret;
246 ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, salt, key);
247 krb5_free_salt(context, salt);
248 return ret;
250 #else
251 #error UNKNOWN_CREATE_KEY_FUNCTIONS
252 #endif
254 int create_kerberos_key_from_string(krb5_context context,
255 krb5_principal host_princ,
256 krb5_data *password,
257 krb5_keyblock *key,
258 krb5_enctype enctype)
260 krb5_principal salt_princ = NULL;
261 int ret;
263 * Check if we've determined that the KDC is salting keys for this
264 * principal/enctype in a non-obvious way. If it is, try to match
265 * its behavior.
267 salt_princ = kerberos_fetch_salt_princ_for_host_princ(context, host_princ, enctype);
268 ret = create_kerberos_key_from_string_direct(context, salt_princ ? salt_princ : host_princ, password, key, enctype);
269 if (salt_princ) {
270 krb5_free_principal(context, salt_princ);
272 return ret;
275 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
276 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
277 krb5_enctype **enctypes)
279 return krb5_get_permitted_enctypes(context, enctypes);
281 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
282 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
283 krb5_enctype **enctypes)
285 return krb5_get_default_in_tkt_etypes(context, enctypes);
287 #else
288 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
289 #endif
291 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
292 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
293 krb5_auth_context auth_context,
294 krb5_keyblock *keyblock)
296 return krb5_auth_con_setkey(context, auth_context, keyblock);
298 #endif
300 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
301 DATA_BLOB *edata,
302 DATA_BLOB *edata_out)
304 DATA_BLOB edata_contents;
305 ASN1_DATA data;
306 int edata_type;
308 if (!edata->length) {
309 return False;
312 asn1_load(&data, *edata);
313 asn1_start_tag(&data, ASN1_SEQUENCE(0));
314 asn1_start_tag(&data, ASN1_CONTEXT(1));
315 asn1_read_Integer(&data, &edata_type);
317 if (edata_type != KRB5_PADATA_PW_SALT) {
318 DEBUG(0,("edata is not of required type %d but of type %d\n",
319 KRB5_PADATA_PW_SALT, edata_type));
320 asn1_free(&data);
321 return False;
324 asn1_start_tag(&data, ASN1_CONTEXT(2));
325 asn1_read_OctetString(&data, &edata_contents);
326 asn1_end_tag(&data);
327 asn1_end_tag(&data);
328 asn1_end_tag(&data);
329 asn1_free(&data);
331 *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length);
333 data_blob_free(&edata_contents);
335 return True;
339 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
341 DATA_BLOB pac_contents;
342 ASN1_DATA data;
343 int data_type;
345 if (!auth_data->length) {
346 return False;
349 asn1_load(&data, *auth_data);
350 asn1_start_tag(&data, ASN1_SEQUENCE(0));
351 asn1_start_tag(&data, ASN1_SEQUENCE(0));
352 asn1_start_tag(&data, ASN1_CONTEXT(0));
353 asn1_read_Integer(&data, &data_type);
355 if (data_type != KRB5_AUTHDATA_WIN2K_PAC ) {
356 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type));
357 asn1_free(&data);
358 return False;
361 asn1_end_tag(&data);
362 asn1_start_tag(&data, ASN1_CONTEXT(1));
363 asn1_read_OctetString(&data, &pac_contents);
364 asn1_end_tag(&data);
365 asn1_end_tag(&data);
366 asn1_end_tag(&data);
367 asn1_free(&data);
369 *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length);
371 data_blob_free(&pac_contents);
373 return True;
376 bool get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, krb5_ticket *tkt)
378 DATA_BLOB auth_data_wrapped;
379 bool got_auth_data_pac = False;
380 int i;
382 #if defined(HAVE_KRB5_TKT_ENC_PART2)
383 if (tkt->enc_part2 && tkt->enc_part2->authorization_data &&
384 tkt->enc_part2->authorization_data[0] &&
385 tkt->enc_part2->authorization_data[0]->length)
387 for (i = 0; tkt->enc_part2->authorization_data[i] != NULL; i++) {
389 if (tkt->enc_part2->authorization_data[i]->ad_type !=
390 KRB5_AUTHDATA_IF_RELEVANT) {
391 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
392 tkt->enc_part2->authorization_data[i]->ad_type));
393 continue;
396 auth_data_wrapped = data_blob(tkt->enc_part2->authorization_data[i]->contents,
397 tkt->enc_part2->authorization_data[i]->length);
399 /* check if it is a PAC */
400 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
401 data_blob_free(&auth_data_wrapped);
403 if (got_auth_data_pac) {
404 return true;
408 return got_auth_data_pac;
411 #else
412 if (tkt->ticket.authorization_data &&
413 tkt->ticket.authorization_data->len)
415 for (i = 0; i < tkt->ticket.authorization_data->len; i++) {
417 if (tkt->ticket.authorization_data->val[i].ad_type !=
418 KRB5_AUTHDATA_IF_RELEVANT) {
419 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
420 tkt->ticket.authorization_data->val[i].ad_type));
421 continue;
424 auth_data_wrapped = data_blob(tkt->ticket.authorization_data->val[i].ad_data.data,
425 tkt->ticket.authorization_data->val[i].ad_data.length);
427 /* check if it is a PAC */
428 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
429 data_blob_free(&auth_data_wrapped);
431 if (got_auth_data_pac) {
432 return true;
436 return got_auth_data_pac;
438 #endif
439 return False;
442 krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
444 #if defined(HAVE_KRB5_TKT_ENC_PART2)
445 return tkt->enc_part2->client;
446 #else
447 return tkt->client;
448 #endif
451 #if !defined(HAVE_KRB5_LOCATE_KDC)
453 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
454 * to a public interface for this functionality, so we have to be able to live
455 * without it if the MIT libraries are hiding their internal symbols.
458 #if defined(KRB5_KRBHST_INIT)
459 /* Heimdal */
460 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, struct sockaddr **addr_pp, int *naddrs, int get_masters)
462 krb5_krbhst_handle hnd;
463 krb5_krbhst_info *hinfo;
464 krb5_error_code rc;
465 int num_kdcs, i;
466 struct sockaddr *sa;
467 struct addrinfo *ai;
469 *addr_pp = NULL;
470 *naddrs = 0;
472 rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
473 if (rc) {
474 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
475 return rc;
478 for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
481 krb5_krbhst_reset(ctx, hnd);
483 if (!num_kdcs) {
484 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
485 krb5_krbhst_free(ctx, hnd);
486 return -1;
489 sa = SMB_MALLOC_ARRAY( struct sockaddr, num_kdcs );
490 if (!sa) {
491 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
492 krb5_krbhst_free(ctx, hnd);
493 naddrs = 0;
494 return -1;
497 memset(sa, '\0', sizeof(struct sockaddr) * num_kdcs );
499 for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
501 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
502 rc = krb5_krbhst_get_addrinfo(ctx, hinfo, &ai);
503 if (rc) {
504 DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc)));
505 continue;
507 #endif
508 if (hinfo->ai && hinfo->ai->ai_family == AF_INET)
509 memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
512 krb5_krbhst_free(ctx, hnd);
514 *naddrs = num_kdcs;
515 *addr_pp = sa;
516 return 0;
519 #else /* ! defined(KRB5_KRBHST_INIT) */
521 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
522 struct sockaddr **addr_pp, int *naddrs, int get_masters)
524 DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
525 return KRB5_KDC_UNREACH;
528 #endif /* KRB5_KRBHST_INIT */
530 #else /* ! HAVE_KRB5_LOCATE_KDC */
532 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
533 struct sockaddr **addr_pp, int *naddrs, int get_masters)
535 return krb5_locate_kdc(ctx, realm, addr_pp, naddrs, get_masters);
538 #endif /* HAVE_KRB5_LOCATE_KDC */
540 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
541 void krb5_free_unparsed_name(krb5_context context, char *val)
543 SAFE_FREE(val);
545 #endif
547 void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
549 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
550 if (pdata->data) {
551 krb5_free_data_contents(context, pdata);
553 #else
554 SAFE_FREE(pdata->data);
555 #endif
558 void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype)
560 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
561 KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype;
562 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
563 KRB5_KEY_TYPE((&pcreds->session)) = enctype;
564 #else
565 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
566 #endif
569 bool kerberos_compatible_enctypes(krb5_context context,
570 krb5_enctype enctype1,
571 krb5_enctype enctype2)
573 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
574 krb5_boolean similar = 0;
576 krb5_c_enctype_compare(context, enctype1, enctype2, &similar);
577 return similar ? True : False;
578 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
579 return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False;
580 #endif
583 static bool ads_cleanup_expired_creds(krb5_context context,
584 krb5_ccache ccache,
585 krb5_creds *credsp)
587 krb5_error_code retval;
588 const char *cc_type = krb5_cc_get_type(context, ccache);
590 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
591 cc_type, krb5_cc_get_name(context, ccache),
592 http_timestring(credsp->times.endtime)));
594 /* we will probably need new tickets if the current ones
595 will expire within 10 seconds.
597 if (credsp->times.endtime >= (time(NULL) + 10))
598 return False;
600 /* heimdal won't remove creds from a file ccache, and
601 perhaps we shouldn't anyway, since internally we
602 use memory ccaches, and a FILE one probably means that
603 we're using creds obtained outside of our exectuable
605 if (strequal(cc_type, "FILE")) {
606 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
607 return False;
610 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
611 if (retval) {
612 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
613 error_message(retval)));
614 /* If we have an error in this, we want to display it,
615 but continue as though we deleted it */
617 return True;
621 we can't use krb5_mk_req because w2k wants the service to be in a particular format
623 static krb5_error_code ads_krb5_mk_req(krb5_context context,
624 krb5_auth_context *auth_context,
625 const krb5_flags ap_req_options,
626 const char *principal,
627 krb5_ccache ccache,
628 krb5_data *outbuf,
629 time_t *expire_time)
631 krb5_error_code retval;
632 krb5_principal server;
633 krb5_creds * credsp;
634 krb5_creds creds;
635 krb5_data in_data;
636 bool creds_ready = False;
637 int i = 0, maxtries = 3;
639 retval = smb_krb5_parse_name(context, principal, &server);
640 if (retval) {
641 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
642 return retval;
645 /* obtain ticket & session key */
646 ZERO_STRUCT(creds);
647 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
648 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
649 error_message(retval)));
650 goto cleanup_princ;
653 if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
654 /* This can commonly fail on smbd startup with no ticket in the cache.
655 * Report at higher level than 1. */
656 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
657 error_message(retval)));
658 goto cleanup_creds;
661 while (!creds_ready && (i < maxtries)) {
663 if ((retval = krb5_get_credentials(context, 0, ccache,
664 &creds, &credsp))) {
665 DEBUG(1,("ads_krb5_mk_req: krb5_get_credentials failed for %s (%s)\n",
666 principal, error_message(retval)));
667 goto cleanup_creds;
670 /* cope with ticket being in the future due to clock skew */
671 if ((unsigned)credsp->times.starttime > time(NULL)) {
672 time_t t = time(NULL);
673 int time_offset =(int)((unsigned)credsp->times.starttime-t);
674 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
675 krb5_set_real_time(context, t + time_offset + 1, 0);
678 if (!ads_cleanup_expired_creds(context, ccache, credsp)) {
679 creds_ready = True;
682 i++;
685 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
686 principal, krb5_cc_get_type(context, ccache), krb5_cc_get_name(context, ccache),
687 http_timestring((unsigned)credsp->times.endtime),
688 (unsigned)credsp->times.endtime));
690 if (expire_time) {
691 *expire_time = (time_t)credsp->times.endtime;
694 in_data.length = 0;
695 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
696 &in_data, credsp, outbuf);
697 if (retval) {
698 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
699 error_message(retval)));
702 krb5_free_creds(context, credsp);
704 cleanup_creds:
705 krb5_free_cred_contents(context, &creds);
707 cleanup_princ:
708 krb5_free_principal(context, server);
710 return retval;
714 get a kerberos5 ticket for the given service
716 int cli_krb5_get_ticket(const char *principal, time_t time_offset,
717 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
718 uint32 extra_ap_opts, const char *ccname,
719 time_t *tgs_expire)
722 krb5_error_code retval;
723 krb5_data packet;
724 krb5_context context = NULL;
725 krb5_ccache ccdef = NULL;
726 krb5_auth_context auth_context = NULL;
727 krb5_enctype enc_types[] = {
728 #ifdef ENCTYPE_ARCFOUR_HMAC
729 ENCTYPE_ARCFOUR_HMAC,
730 #endif
731 ENCTYPE_DES_CBC_MD5,
732 ENCTYPE_DES_CBC_CRC,
733 ENCTYPE_NULL};
735 initialize_krb5_error_table();
736 retval = krb5_init_context(&context);
737 if (retval) {
738 DEBUG(1,("cli_krb5_get_ticket: krb5_init_context failed (%s)\n",
739 error_message(retval)));
740 goto failed;
743 if (time_offset != 0) {
744 krb5_set_real_time(context, time(NULL) + time_offset, 0);
747 if ((retval = krb5_cc_resolve(context, ccname ?
748 ccname : krb5_cc_default_name(context), &ccdef))) {
749 DEBUG(1,("cli_krb5_get_ticket: krb5_cc_default failed (%s)\n",
750 error_message(retval)));
751 goto failed;
754 if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
755 DEBUG(1,("cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s)\n",
756 error_message(retval)));
757 goto failed;
760 if ((retval = ads_krb5_mk_req(context,
761 &auth_context,
762 AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
763 principal,
764 ccdef, &packet,
765 tgs_expire))) {
766 goto failed;
769 get_krb5_smb_session_key(context, auth_context, session_key_krb5, False);
771 *ticket = data_blob(packet.data, packet.length);
773 kerberos_free_data_contents(context, &packet);
775 failed:
777 if ( context ) {
778 if (ccdef)
779 krb5_cc_close(context, ccdef);
780 if (auth_context)
781 krb5_auth_con_free(context, auth_context);
782 krb5_free_context(context);
785 return retval;
788 bool get_krb5_smb_session_key(krb5_context context, krb5_auth_context auth_context, DATA_BLOB *session_key, bool remote)
790 krb5_keyblock *skey;
791 krb5_error_code err;
792 bool ret = False;
794 if (remote)
795 err = krb5_auth_con_getremotesubkey(context, auth_context, &skey);
796 else
797 err = krb5_auth_con_getlocalsubkey(context, auth_context, &skey);
798 if (err == 0 && skey != NULL) {
799 DEBUG(10, ("Got KRB5 session key of length %d\n", (int)KRB5_KEY_LENGTH(skey)));
800 *session_key = data_blob(KRB5_KEY_DATA(skey), KRB5_KEY_LENGTH(skey));
801 dump_data_pw("KRB5 Session Key:\n", session_key->data, session_key->length);
803 ret = True;
805 krb5_free_keyblock(context, skey);
806 } else {
807 DEBUG(10, ("KRB5 error getting session key %d\n", err));
810 return ret;
814 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
815 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i );
817 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
819 static krb5_data kdata;
821 kdata.data = (char *)krb5_principal_get_comp_string(context, principal, i);
822 kdata.length = strlen((const char *)kdata.data);
823 return &kdata;
825 #endif
827 krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
829 #if defined(HAVE_KRB5_KT_FREE_ENTRY)
830 return krb5_kt_free_entry(context, kt_entry);
831 #elif defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
832 return krb5_free_keytab_entry_contents(context, kt_entry);
833 #else
834 #error UNKNOWN_KT_FREE_FUNCTION
835 #endif
838 void smb_krb5_checksum_from_pac_sig(krb5_checksum *cksum,
839 struct PAC_SIGNATURE_DATA *sig)
841 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
842 cksum->cksumtype = (krb5_cksumtype)sig->type;
843 cksum->checksum.length = sig->signature.length;
844 cksum->checksum.data = sig->signature.data;
845 #else
846 cksum->checksum_type = (krb5_cksumtype)sig->type;
847 cksum->length = sig->signature.length;
848 cksum->contents = sig->signature.data;
849 #endif
852 krb5_error_code smb_krb5_verify_checksum(krb5_context context,
853 const krb5_keyblock *keyblock,
854 krb5_keyusage usage,
855 krb5_checksum *cksum,
856 uint8 *data,
857 size_t length)
859 krb5_error_code ret;
861 /* verify the checksum */
863 /* welcome to the wonderful world of samba's kerberos abstraction layer:
865 * function heimdal 0.6.1rc3 heimdal 0.7 MIT krb 1.4.2
866 * -----------------------------------------------------------------------------
867 * krb5_c_verify_checksum - works works
868 * krb5_verify_checksum works (6 args) works (6 args) broken (7 args)
871 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
873 krb5_boolean checksum_valid = False;
874 krb5_data input;
876 input.data = (char *)data;
877 input.length = length;
879 ret = krb5_c_verify_checksum(context,
880 keyblock,
881 usage,
882 &input,
883 cksum,
884 &checksum_valid);
885 if (ret) {
886 DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n",
887 error_message(ret)));
888 return ret;
891 if (!checksum_valid)
892 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
895 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
897 /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
898 * without enctype and it ignores any key_usage types - Guenther */
902 krb5_crypto crypto;
903 ret = krb5_crypto_init(context,
904 keyblock,
906 &crypto);
907 if (ret) {
908 DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n",
909 error_message(ret)));
910 return ret;
913 ret = krb5_verify_checksum(context,
914 crypto,
915 usage,
916 data,
917 length,
918 cksum);
920 krb5_crypto_destroy(context, crypto);
923 #else
924 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
925 #endif
927 return ret;
930 time_t get_authtime_from_tkt(krb5_ticket *tkt)
932 #if defined(HAVE_KRB5_TKT_ENC_PART2)
933 return tkt->enc_part2->times.authtime;
934 #else
935 return tkt->ticket.authtime;
936 #endif
939 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
940 static int get_kvno_from_ap_req(krb5_ap_req *ap_req)
942 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
943 if (ap_req->ticket->enc_part.kvno)
944 return ap_req->ticket->enc_part.kvno;
945 #else /* Heimdal */
946 if (ap_req->ticket.enc_part.kvno)
947 return *ap_req->ticket.enc_part.kvno;
948 #endif
949 return 0;
952 static krb5_enctype get_enctype_from_ap_req(krb5_ap_req *ap_req)
954 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
955 return ap_req->ticket.enc_part.etype;
956 #else /* MIT */
957 return ap_req->ticket->enc_part.enctype;
958 #endif
960 #endif /* HAVE_KRB5_DECODE_AP_REQ */
962 static krb5_error_code
963 get_key_from_keytab(krb5_context context,
964 krb5_const_principal server,
965 krb5_enctype enctype,
966 krb5_kvno kvno,
967 krb5_keyblock **out_key)
969 krb5_keytab_entry entry;
970 krb5_error_code ret;
971 krb5_keytab keytab;
972 char *name = NULL;
974 /* We have to open a new keytab handle here, as MIT does
975 an implicit open/getnext/close on krb5_kt_get_entry. We
976 may be in the middle of a keytab enumeration when this is
977 called. JRA. */
979 ret = smb_krb5_open_keytab(context, NULL, False, &keytab);
980 if (ret) {
981 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
982 return ret;
985 if ( DEBUGLEVEL >= 10 ) {
986 if (smb_krb5_unparse_name(context, server, &name) == 0) {
987 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
988 kvno, enctype, name));
989 SAFE_FREE(name);
993 ret = krb5_kt_get_entry(context,
994 keytab,
995 server,
996 kvno,
997 enctype,
998 &entry);
1000 if (ret) {
1001 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret)));
1002 goto out;
1005 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK /* Heimdal */
1006 ret = krb5_copy_keyblock(context, &entry.keyblock, out_key);
1007 #elif defined(HAVE_KRB5_KEYTAB_ENTRY_KEY) /* MIT */
1008 ret = krb5_copy_keyblock(context, &entry.key, out_key);
1009 #else
1010 #error UNKNOWN_KRB5_KEYTAB_ENTRY_FORMAT
1011 #endif
1013 if (ret) {
1014 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret)));
1015 goto out;
1018 smb_krb5_kt_free_entry(context, &entry);
1020 out:
1021 krb5_kt_close(context, keytab);
1022 return ret;
1025 /* Prototypes */
1027 krb5_error_code smb_krb5_get_keyinfo_from_ap_req(krb5_context context,
1028 const krb5_data *inbuf,
1029 krb5_kvno *kvno,
1030 krb5_enctype *enctype)
1032 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1034 krb5_error_code ret;
1035 krb5_ap_req ap_req;
1037 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
1038 if (ret)
1039 return ret;
1041 *kvno = get_kvno_from_ap_req(&ap_req);
1042 *enctype = get_enctype_from_ap_req(&ap_req);
1044 free_AP_REQ(&ap_req);
1045 return 0;
1047 #endif
1049 /* Possibly not an appropriate error code. */
1050 return KRB5KDC_ERR_BADOPTION;
1053 krb5_error_code krb5_rd_req_return_keyblock_from_keytab(krb5_context context,
1054 krb5_auth_context *auth_context,
1055 const krb5_data *inbuf,
1056 krb5_const_principal server,
1057 krb5_keytab keytab,
1058 krb5_flags *ap_req_options,
1059 krb5_ticket **ticket,
1060 krb5_keyblock **keyblock)
1062 krb5_error_code ret;
1063 krb5_kvno kvno;
1064 krb5_enctype enctype;
1065 krb5_keyblock *local_keyblock;
1067 ret = krb5_rd_req(context,
1068 auth_context,
1069 inbuf,
1070 server,
1071 keytab,
1072 ap_req_options,
1073 ticket);
1074 if (ret) {
1075 return ret;
1078 #ifdef KRB5_TICKET_HAS_KEYINFO
1079 enctype = (*ticket)->enc_part.enctype;
1080 kvno = (*ticket)->enc_part.kvno;
1081 #else
1082 ret = smb_krb5_get_keyinfo_from_ap_req(context, inbuf, &kvno, &enctype);
1083 if (ret) {
1084 return ret;
1086 #endif
1088 ret = get_key_from_keytab(context,
1089 server,
1090 enctype,
1091 kvno,
1092 &local_keyblock);
1093 if (ret) {
1094 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1095 goto out;
1098 out:
1099 if (ret && local_keyblock != NULL) {
1100 krb5_free_keyblock(context, local_keyblock);
1101 } else {
1102 *keyblock = local_keyblock;
1105 return ret;
1108 krb5_error_code smb_krb5_parse_name_norealm(krb5_context context,
1109 const char *name,
1110 krb5_principal *principal)
1112 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1113 return smb_krb5_parse_name_norealm_conv(context, name, principal);
1114 #endif
1116 /* we are cheating here because parse_name will in fact set the realm.
1117 * We don't care as the only caller of smb_krb5_parse_name_norealm
1118 * ignores the realm anyway when calling
1119 * smb_krb5_principal_compare_any_realm later - Guenther */
1121 return smb_krb5_parse_name(context, name, principal);
1124 bool smb_krb5_principal_compare_any_realm(krb5_context context,
1125 krb5_const_principal princ1,
1126 krb5_const_principal princ2)
1128 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1130 return krb5_principal_compare_any_realm(context, princ1, princ2);
1132 /* krb5_princ_size is a macro in MIT */
1133 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1135 int i, len1, len2;
1136 const krb5_data *p1, *p2;
1138 len1 = krb5_princ_size(context, princ1);
1139 len2 = krb5_princ_size(context, princ2);
1141 if (len1 != len2)
1142 return False;
1144 for (i = 0; i < len1; i++) {
1146 p1 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ1), i);
1147 p2 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ2), i);
1149 if (p1->length != p2->length || memcmp(p1->data, p2->data, p1->length))
1150 return False;
1153 return True;
1154 #else
1155 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1156 #endif
1159 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string, /* FILE:/tmp/krb5cc_0 */
1160 const char *client_string, /* gd@BER.SUSE.DE */
1161 const char *service_string, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1162 time_t *expire_time)
1164 krb5_error_code ret;
1165 krb5_context context = NULL;
1166 krb5_ccache ccache = NULL;
1167 krb5_principal client = NULL;
1168 krb5_creds creds, creds_in, *creds_out = NULL;
1170 ZERO_STRUCT(creds);
1171 ZERO_STRUCT(creds_in);
1173 initialize_krb5_error_table();
1174 ret = krb5_init_context(&context);
1175 if (ret) {
1176 goto done;
1179 if (!ccache_string) {
1180 ccache_string = krb5_cc_default_name(context);
1183 if (!ccache_string) {
1184 ret = EINVAL;
1185 goto done;
1188 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
1190 /* FIXME: we should not fall back to defaults */
1191 ret = krb5_cc_resolve(context, CONST_DISCARD(char *, ccache_string), &ccache);
1192 if (ret) {
1193 goto done;
1196 if (client_string) {
1197 ret = smb_krb5_parse_name(context, client_string, &client);
1198 if (ret) {
1199 goto done;
1201 } else {
1202 ret = krb5_cc_get_principal(context, ccache, &client);
1203 if (ret) {
1204 goto done;
1208 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1210 ret = krb5_get_renewed_creds(context, &creds, client, ccache, CONST_DISCARD(char *, service_string));
1211 if (ret) {
1212 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1213 goto done;
1216 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1218 krb5_kdc_flags flags;
1219 krb5_realm *client_realm = NULL;
1221 ret = krb5_copy_principal(context, client, &creds_in.client);
1222 if (ret) {
1223 goto done;
1226 if (service_string) {
1227 ret = smb_krb5_parse_name(context, service_string, &creds_in.server);
1228 if (ret) {
1229 goto done;
1231 } else {
1232 /* build tgt service by default */
1233 client_realm = krb5_princ_realm(context, creds_in.client);
1234 if (!client_realm) {
1235 ret = ENOMEM;
1236 goto done;
1238 ret = krb5_make_principal(context, &creds_in.server, *client_realm, KRB5_TGS_NAME, *client_realm, NULL);
1239 if (ret) {
1240 goto done;
1244 flags.i = 0;
1245 flags.b.renewable = flags.b.renew = True;
1247 ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &creds_in, &creds_out);
1248 if (ret) {
1249 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1250 goto done;
1253 creds = *creds_out;
1255 #else
1256 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1257 #endif
1259 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1260 ret = krb5_cc_initialize(context, ccache, client);
1261 if (ret) {
1262 goto done;
1265 ret = krb5_cc_store_cred(context, ccache, &creds);
1267 if (expire_time) {
1268 *expire_time = (time_t) creds.times.endtime;
1271 done:
1272 krb5_free_cred_contents(context, &creds_in);
1274 if (creds_out) {
1275 krb5_free_creds(context, creds_out);
1276 } else {
1277 krb5_free_cred_contents(context, &creds);
1280 if (client) {
1281 krb5_free_principal(context, client);
1283 if (ccache) {
1284 krb5_cc_close(context, ccache);
1286 if (context) {
1287 krb5_free_context(context);
1290 return ret;
1293 krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr)
1295 krb5_error_code ret = 0;
1296 if (addr == NULL) {
1297 return ret;
1299 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1300 krb5_free_addresses(context, addr->addrs);
1301 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1302 ret = krb5_free_addresses(context, addr->addrs);
1303 SAFE_FREE(addr->addrs);
1304 #endif
1305 SAFE_FREE(addr);
1306 addr = NULL;
1307 return ret;
1310 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr)
1312 krb5_error_code ret = 0;
1313 nstring buf;
1314 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1315 krb5_address **addrs = NULL;
1316 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1317 krb5_addresses *addrs = NULL;
1318 #endif
1320 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
1321 if (*kerb_addr == NULL) {
1322 return ENOMEM;
1325 put_name(buf, global_myname(), ' ', 0x20);
1327 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1329 int num_addr = 2;
1331 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
1332 if (addrs == NULL) {
1333 SAFE_FREE(kerb_addr);
1334 return ENOMEM;
1337 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
1339 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1340 if (addrs[0] == NULL) {
1341 SAFE_FREE(addrs);
1342 SAFE_FREE(kerb_addr);
1343 return ENOMEM;
1346 addrs[0]->magic = KV5M_ADDRESS;
1347 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
1348 addrs[0]->length = MAX_NETBIOSNAME_LEN;
1349 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
1350 if (addrs[0]->contents == NULL) {
1351 SAFE_FREE(addrs[0]);
1352 SAFE_FREE(addrs);
1353 SAFE_FREE(kerb_addr);
1354 return ENOMEM;
1357 memcpy(addrs[0]->contents, buf, addrs[0]->length);
1359 addrs[1] = NULL;
1361 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1363 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
1364 if (addrs == NULL) {
1365 SAFE_FREE(kerb_addr);
1366 return ENOMEM;
1369 memset(addrs, 0, sizeof(krb5_addresses));
1371 addrs->len = 1;
1372 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1373 if (addrs->val == NULL) {
1374 SAFE_FREE(addrs);
1375 SAFE_FREE(kerb_addr);
1376 return ENOMEM;
1379 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
1380 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
1381 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
1382 if (addrs->val[0].address.data == NULL) {
1383 SAFE_FREE(addrs->val);
1384 SAFE_FREE(addrs);
1385 SAFE_FREE(kerb_addr);
1386 return ENOMEM;
1389 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
1391 #else
1392 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1393 #endif
1394 (*kerb_addr)->addrs = addrs;
1396 return ret;
1399 void smb_krb5_free_error(krb5_context context, krb5_error *krberror)
1401 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1402 krb5_free_error_contents(context, krberror);
1403 #else /* MIT */
1404 krb5_free_error(context, krberror);
1405 #endif
1408 krb5_error_code handle_krberror_packet(krb5_context context,
1409 krb5_data *packet)
1411 krb5_error_code ret;
1412 bool got_error_code = False;
1414 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1416 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1418 krb5_error krberror;
1420 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1421 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1422 error_message(ret)));
1423 return ret;
1426 if (krberror.e_data == NULL || krberror.e_data->data == NULL) {
1427 ret = (krb5_error_code) krberror.error_code;
1428 got_error_code = True;
1431 smb_krb5_free_error(context, &krberror);
1433 #else /* MIT */
1435 krb5_error *krberror;
1437 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1438 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1439 error_message(ret)));
1440 return ret;
1443 if (krberror->e_data.data == NULL) {
1444 ret = ERROR_TABLE_BASE_krb5 + (krb5_error_code) krberror->error;
1445 got_error_code = True;
1447 smb_krb5_free_error(context, krberror);
1449 #endif
1450 if (got_error_code) {
1451 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1452 error_message(ret), ret));
1454 return ret;
1457 krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context,
1458 krb5_get_init_creds_opt **opt)
1460 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1461 /* Heimdal or modern MIT version */
1462 return krb5_get_init_creds_opt_alloc(context, opt);
1463 #else
1464 /* Historical MIT version */
1465 krb5_get_init_creds_opt *my_opt;
1467 *opt = NULL;
1469 if ((my_opt = SMB_MALLOC_P(krb5_get_init_creds_opt)) == NULL) {
1470 return ENOMEM;
1473 krb5_get_init_creds_opt_init(my_opt);
1475 *opt = my_opt;
1476 return 0;
1477 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1480 void smb_krb5_get_init_creds_opt_free(krb5_context context,
1481 krb5_get_init_creds_opt *opt)
1483 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1485 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1486 /* Modern MIT or Heimdal version */
1487 krb5_get_init_creds_opt_free(context, opt);
1488 #else
1489 /* Heimdal version */
1490 krb5_get_init_creds_opt_free(opt);
1491 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1493 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1494 /* Historical MIT version */
1495 SAFE_FREE(opt);
1496 opt = NULL;
1497 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1500 krb5_enctype smb_get_enctype_from_kt_entry(const krb5_keytab_entry *kt_entry)
1502 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEY /* MIT */
1503 return kt_entry->key.enctype;
1504 #elif defined(HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK) /* Heimdal */
1505 return kt_entry->keyblock.keytype;
1506 #else
1507 #error UNKNOWN_KRB5_KEYTAB_ENTRY_KEYBLOCK_FORMAT
1508 #endif
1512 /* caller needs to free etype_s */
1513 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
1514 krb5_enctype enctype,
1515 char **etype_s)
1517 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1518 return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
1519 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1520 char buf[256];
1521 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
1522 if (ret) {
1523 return ret;
1525 *etype_s = SMB_STRDUP(buf);
1526 if (!*etype_s) {
1527 return ENOMEM;
1529 return ret;
1530 #else
1531 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1532 #endif
1535 krb5_error_code smb_krb5_mk_error(krb5_context context,
1536 krb5_error_code error_code,
1537 const krb5_principal server,
1538 krb5_data *reply)
1540 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1542 * The MIT interface is *terrible*.
1543 * We have to construct this ourselves...
1545 krb5_error e;
1547 memset(&e, 0, sizeof(e));
1548 krb5_us_timeofday(context, &e.stime, &e.susec);
1549 e.server = server;
1550 #if defined(krb5_err_base)
1551 e.error = error_code - krb5_err_base;
1552 #elif defined(ERROR_TABLE_BASE_krb5)
1553 e.error = error_code - ERROR_TABLE_BASE_krb5;
1554 #else
1555 e.error = error_code; /* Almost certainly wrong, but what can we do... ? */
1556 #endif
1558 return krb5_mk_error(context, &e, reply);
1559 #else /* Heimdal. */
1560 return krb5_mk_error(context,
1561 error_code,
1562 NULL,
1563 NULL, /* e_data */
1564 NULL,
1565 server,
1566 NULL,
1567 NULL,
1568 reply);
1569 #endif
1572 /**********************************************************************
1573 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1574 * allows to process non-default keytab names.
1575 * @param context krb5_context
1576 * @param keytab_name_req string
1577 * @param write_access bool if writable keytab is required
1578 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1579 * @return krb5_error_code
1580 **********************************************************************/
1582 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1583 #ifndef MAX_KEYTAB_NAME_LEN
1584 #define MAX_KEYTAB_NAME_LEN 1100
1585 #endif
1587 krb5_error_code smb_krb5_open_keytab(krb5_context context,
1588 const char *keytab_name_req,
1589 bool write_access,
1590 krb5_keytab *keytab)
1592 krb5_error_code ret = 0;
1593 TALLOC_CTX *mem_ctx;
1594 char keytab_string[MAX_KEYTAB_NAME_LEN];
1595 char *kt_str = NULL;
1596 bool found_valid_name = False;
1597 const char *pragma = "FILE";
1598 const char *tmp = NULL;
1600 if (!write_access && !keytab_name_req) {
1601 /* caller just wants to read the default keytab readonly, so be it */
1602 return krb5_kt_default(context, keytab);
1605 mem_ctx = talloc_init("smb_krb5_open_keytab");
1606 if (!mem_ctx) {
1607 return ENOMEM;
1610 #ifdef HAVE_WRFILE_KEYTAB
1611 if (write_access) {
1612 pragma = "WRFILE";
1614 #endif
1616 if (keytab_name_req) {
1618 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1619 ret = KRB5_CONFIG_NOTENUFSPACE;
1620 goto out;
1623 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
1624 (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
1625 tmp = keytab_name_req;
1626 goto resolve;
1629 if (keytab_name_req[0] != '/') {
1630 ret = KRB5_KT_BADNAME;
1631 goto out;
1634 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1635 if (!tmp) {
1636 ret = ENOMEM;
1637 goto out;
1640 goto resolve;
1643 /* we need to handle more complex keytab_strings, like:
1644 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1646 ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1647 if (ret) {
1648 goto out;
1651 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1653 tmp = talloc_strdup(mem_ctx, keytab_string);
1654 if (!tmp) {
1655 ret = ENOMEM;
1656 goto out;
1659 if (strncmp(tmp, "ANY:", 4) == 0) {
1660 tmp += 4;
1663 memset(&keytab_string, '\0', sizeof(keytab_string));
1665 while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1666 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1667 found_valid_name = True;
1668 tmp = kt_str;
1669 tmp += 7;
1672 if (strncmp(kt_str, "FILE:", 5) == 0) {
1673 found_valid_name = True;
1674 tmp = kt_str;
1675 tmp += 5;
1678 if (found_valid_name) {
1679 if (tmp[0] != '/') {
1680 ret = KRB5_KT_BADNAME;
1681 goto out;
1684 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1685 if (!tmp) {
1686 ret = ENOMEM;
1687 goto out;
1689 break;
1693 if (!found_valid_name) {
1694 ret = KRB5_KT_UNKNOWN_TYPE;
1695 goto out;
1698 resolve:
1699 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1700 ret = krb5_kt_resolve(context, tmp, keytab);
1702 out:
1703 TALLOC_FREE(mem_ctx);
1704 return ret;
1707 #else /* HAVE_KRB5 */
1708 /* this saves a few linking headaches */
1709 int cli_krb5_get_ticket(const char *principal, time_t time_offset,
1710 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
1711 const char *ccname, time_t *tgs_expire)
1713 DEBUG(0,("NO KERBEROS SUPPORT\n"));
1714 return 1;
1717 #endif