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-2009
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/>.
25 #include "../librpc/gen_ndr/krb5pac.h"
26 #include "../lib/util/asn1.h"
27 #include "libsmb/nmblib.h"
29 #ifndef KRB5_AUTHDATA_WIN2K_PAC
30 #define KRB5_AUTHDATA_WIN2K_PAC 128
33 #ifndef KRB5_AUTHDATA_IF_RELEVANT
34 #define KRB5_AUTHDATA_IF_RELEVANT 1
39 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
40 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
41 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
42 bind field, flags field. */
44 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
45 but still has the symbol */
46 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
47 krb5_error_code
krb5_auth_con_set_req_cksumtype(
49 krb5_auth_context auth_context
,
50 krb5_cksumtype cksumtype
);
53 #ifndef HAVE_KRB5_SET_REAL_TIME
55 * This function is not in the Heimdal mainline.
57 krb5_error_code
krb5_set_real_time(krb5_context context
, int32_t seconds
, int32_t microseconds
)
62 ret
= krb5_us_timeofday(context
, &sec
, &usec
);
66 context
->kdc_sec_offset
= seconds
- sec
;
67 context
->kdc_usec_offset
= microseconds
- usec
;
73 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
75 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
77 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
78 * to krb5_set_default_tgs_ktypes. See
79 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
81 * If the MIT libraries are not exporting internal symbols, we will end up in
82 * this branch, which is correct. Otherwise we will continue to use the
85 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
87 return krb5_set_default_tgs_enctypes(ctx
, enc
);
90 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
93 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
95 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
98 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
100 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
102 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
104 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
106 memset(pkaddr
, '\0', sizeof(krb5_address
));
107 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
108 if (paddr
->ss_family
== AF_INET6
) {
109 pkaddr
->addr_type
= KRB5_ADDRESS_INET6
;
110 pkaddr
->address
.length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
111 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
115 if (paddr
->ss_family
== AF_INET
) {
116 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
117 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
118 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
123 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
125 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
127 memset(pkaddr
, '\0', sizeof(krb5_address
));
128 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
129 if (paddr
->ss_family
== AF_INET6
) {
130 pkaddr
->addrtype
= ADDRTYPE_INET6
;
131 pkaddr
->length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
132 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
136 if (paddr
->ss_family
== AF_INET
) {
137 pkaddr
->addrtype
= ADDRTYPE_INET
;
138 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
139 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
145 #error UNKNOWN_ADDRTYPE
148 int create_kerberos_key_from_string(krb5_context context
,
149 krb5_principal host_princ
,
152 krb5_enctype enctype
,
155 krb5_principal salt_princ
= NULL
;
158 * Check if we've determined that the KDC is salting keys for this
159 * principal/enctype in a non-obvious way. If it is, try to match
163 KRB5_KEY_DATA(key
) = (KRB5_KEY_DATA_CAST
*)SMB_MALLOC(password
->length
);
164 if (!KRB5_KEY_DATA(key
)) {
167 memcpy(KRB5_KEY_DATA(key
), password
->data
, password
->length
);
168 KRB5_KEY_LENGTH(key
) = password
->length
;
169 KRB5_KEY_TYPE(key
) = enctype
;
172 salt_princ
= kerberos_fetch_salt_princ_for_host_princ(context
, host_princ
, enctype
);
173 ret
= create_kerberos_key_from_string_direct(context
, salt_princ
? salt_princ
: host_princ
, password
, key
, enctype
);
175 krb5_free_principal(context
, salt_princ
);
180 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
181 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
182 krb5_enctype
**enctypes
)
184 return krb5_get_permitted_enctypes(context
, enctypes
);
186 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
187 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
188 krb5_enctype
**enctypes
)
190 #ifdef HAVE_KRB5_PDU_NONE_DECL
191 return krb5_get_default_in_tkt_etypes(context
, KRB5_PDU_NONE
, enctypes
);
193 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
197 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
200 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
201 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
202 krb5_auth_context auth_context
,
203 krb5_keyblock
*keyblock
)
205 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
209 bool unwrap_edata_ntstatus(TALLOC_CTX
*mem_ctx
,
211 DATA_BLOB
*edata_out
)
213 DATA_BLOB edata_contents
;
217 if (!edata
->length
) {
221 data
= asn1_init(mem_ctx
);
226 asn1_load(data
, *edata
);
227 asn1_start_tag(data
, ASN1_SEQUENCE(0));
228 asn1_start_tag(data
, ASN1_CONTEXT(1));
229 asn1_read_Integer(data
, &edata_type
);
231 if (edata_type
!= KRB5_PADATA_PW_SALT
) {
232 DEBUG(0,("edata is not of required type %d but of type %d\n",
233 KRB5_PADATA_PW_SALT
, edata_type
));
238 asn1_start_tag(data
, ASN1_CONTEXT(2));
239 asn1_read_OctetString(data
, talloc_tos(), &edata_contents
);
245 *edata_out
= data_blob_talloc(mem_ctx
, edata_contents
.data
, edata_contents
.length
);
247 data_blob_free(&edata_contents
);
253 bool unwrap_pac(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, DATA_BLOB
*unwrapped_pac_data
)
255 DATA_BLOB pac_contents
;
259 if (!auth_data
->length
) {
263 data
= asn1_init(mem_ctx
);
268 asn1_load(data
, *auth_data
);
269 asn1_start_tag(data
, ASN1_SEQUENCE(0));
270 asn1_start_tag(data
, ASN1_SEQUENCE(0));
271 asn1_start_tag(data
, ASN1_CONTEXT(0));
272 asn1_read_Integer(data
, &data_type
);
274 if (data_type
!= KRB5_AUTHDATA_WIN2K_PAC
) {
275 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type
));
281 asn1_start_tag(data
, ASN1_CONTEXT(1));
282 asn1_read_OctetString(data
, talloc_tos(), &pac_contents
);
288 *unwrapped_pac_data
= data_blob_talloc(mem_ctx
, pac_contents
.data
, pac_contents
.length
);
290 data_blob_free(&pac_contents
);
295 bool get_auth_data_from_tkt(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, krb5_ticket
*tkt
)
297 DATA_BLOB auth_data_wrapped
;
298 bool got_auth_data_pac
= False
;
301 #if defined(HAVE_KRB5_TKT_ENC_PART2)
302 if (tkt
->enc_part2
&& tkt
->enc_part2
->authorization_data
&&
303 tkt
->enc_part2
->authorization_data
[0] &&
304 tkt
->enc_part2
->authorization_data
[0]->length
)
306 for (i
= 0; tkt
->enc_part2
->authorization_data
[i
] != NULL
; i
++) {
308 if (tkt
->enc_part2
->authorization_data
[i
]->ad_type
!=
309 KRB5_AUTHDATA_IF_RELEVANT
) {
310 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
311 tkt
->enc_part2
->authorization_data
[i
]->ad_type
));
315 auth_data_wrapped
= data_blob(tkt
->enc_part2
->authorization_data
[i
]->contents
,
316 tkt
->enc_part2
->authorization_data
[i
]->length
);
318 /* check if it is a PAC */
319 got_auth_data_pac
= unwrap_pac(mem_ctx
, &auth_data_wrapped
, auth_data
);
320 data_blob_free(&auth_data_wrapped
);
322 if (got_auth_data_pac
) {
327 return got_auth_data_pac
;
331 if (tkt
->ticket
.authorization_data
&&
332 tkt
->ticket
.authorization_data
->len
)
334 for (i
= 0; i
< tkt
->ticket
.authorization_data
->len
; i
++) {
336 if (tkt
->ticket
.authorization_data
->val
[i
].ad_type
!=
337 KRB5_AUTHDATA_IF_RELEVANT
) {
338 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
339 tkt
->ticket
.authorization_data
->val
[i
].ad_type
));
343 auth_data_wrapped
= data_blob(tkt
->ticket
.authorization_data
->val
[i
].ad_data
.data
,
344 tkt
->ticket
.authorization_data
->val
[i
].ad_data
.length
);
346 /* check if it is a PAC */
347 got_auth_data_pac
= unwrap_pac(mem_ctx
, &auth_data_wrapped
, auth_data
);
348 data_blob_free(&auth_data_wrapped
);
350 if (got_auth_data_pac
) {
355 return got_auth_data_pac
;
361 krb5_const_principal
get_principal_from_tkt(krb5_ticket
*tkt
)
363 #if defined(HAVE_KRB5_TKT_ENC_PART2)
364 return tkt
->enc_part2
->client
;
370 #if !defined(HAVE_KRB5_LOCATE_KDC)
372 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
373 * to a public interface for this functionality, so we have to be able to live
374 * without it if the MIT libraries are hiding their internal symbols.
377 #if defined(KRB5_KRBHST_INIT)
379 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
, struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
381 krb5_krbhst_handle hnd
;
382 krb5_krbhst_info
*hinfo
;
391 rc
= krb5_krbhst_init(ctx
, realm
->data
, KRB5_KRBHST_KDC
, &hnd
);
393 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc
)));
397 for ( num_kdcs
= 0; (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); num_kdcs
++)
400 krb5_krbhst_reset(ctx
, hnd
);
403 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
404 krb5_krbhst_free(ctx
, hnd
);
408 sa
= SMB_MALLOC_ARRAY( struct sockaddr
, num_kdcs
);
410 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
411 krb5_krbhst_free(ctx
, hnd
);
416 memset(sa
, '\0', sizeof(struct sockaddr
) * num_kdcs
);
418 for (i
= 0; i
< num_kdcs
&& (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); i
++) {
420 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
421 rc
= krb5_krbhst_get_addrinfo(ctx
, hinfo
, &ai
);
423 DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc
)));
427 if (hinfo
->ai
&& hinfo
->ai
->ai_family
== AF_INET
)
428 memcpy(&sa
[i
], hinfo
->ai
->ai_addr
, sizeof(struct sockaddr
));
431 krb5_krbhst_free(ctx
, hnd
);
438 #else /* ! defined(KRB5_KRBHST_INIT) */
440 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
,
441 struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
443 DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
444 return KRB5_KDC_UNREACH
;
447 #endif /* KRB5_KRBHST_INIT */
449 #else /* ! HAVE_KRB5_LOCATE_KDC */
451 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
,
452 struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
454 return krb5_locate_kdc(ctx
, realm
, addr_pp
, naddrs
, get_masters
);
457 #endif /* HAVE_KRB5_LOCATE_KDC */
459 void kerberos_set_creds_enctype(krb5_creds
*pcreds
, int enctype
)
461 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
462 KRB5_KEY_TYPE((&pcreds
->keyblock
)) = enctype
;
463 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
464 KRB5_KEY_TYPE((&pcreds
->session
)) = enctype
;
466 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
470 bool kerberos_compatible_enctypes(krb5_context context
,
471 krb5_enctype enctype1
,
472 krb5_enctype enctype2
)
474 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
475 krb5_boolean similar
= 0;
477 krb5_c_enctype_compare(context
, enctype1
, enctype2
, &similar
);
478 return similar
? True
: False
;
479 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
480 return krb5_enctypes_compatible_keys(context
, enctype1
, enctype2
) ? True
: False
;
484 static bool ads_cleanup_expired_creds(krb5_context context
,
488 krb5_error_code retval
;
489 const char *cc_type
= krb5_cc_get_type(context
, ccache
);
491 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
492 cc_type
, krb5_cc_get_name(context
, ccache
),
493 http_timestring(talloc_tos(), credsp
->times
.endtime
)));
495 /* we will probably need new tickets if the current ones
496 will expire within 10 seconds.
498 if (credsp
->times
.endtime
>= (time(NULL
) + 10))
501 /* heimdal won't remove creds from a file ccache, and
502 perhaps we shouldn't anyway, since internally we
503 use memory ccaches, and a FILE one probably means that
504 we're using creds obtained outside of our exectuable
506 if (strequal(cc_type
, "FILE")) {
507 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type
));
511 retval
= krb5_cc_remove_cred(context
, ccache
, 0, credsp
);
513 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
514 error_message(retval
)));
515 /* If we have an error in this, we want to display it,
516 but continue as though we deleted it */
521 /* Allocate and setup the auth context into the state we need. */
523 static krb5_error_code
setup_auth_context(krb5_context context
,
524 krb5_auth_context
*auth_context
)
526 krb5_error_code retval
;
528 retval
= krb5_auth_con_init(context
, auth_context
);
530 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
531 error_message(retval
)));
535 /* Ensure this is an addressless ticket. */
536 retval
= krb5_auth_con_setaddrs(context
, *auth_context
, NULL
, NULL
);
538 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
539 error_message(retval
)));
545 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
546 static krb5_error_code
create_gss_checksum(krb5_data
*in_data
, /* [inout] */
549 unsigned int orig_length
= in_data
->length
;
550 unsigned int base_cksum_size
= GSSAPI_CHECKSUM_SIZE
;
551 char *gss_cksum
= NULL
;
554 /* Extra length field for delgated ticket. */
555 base_cksum_size
+= 4;
558 if ((unsigned int)base_cksum_size
+ orig_length
<
559 (unsigned int)base_cksum_size
) {
563 gss_cksum
= (char *)SMB_MALLOC(base_cksum_size
+ orig_length
);
564 if (gss_cksum
== NULL
) {
568 memset(gss_cksum
, '\0', base_cksum_size
+ orig_length
);
569 SIVAL(gss_cksum
, 0, GSSAPI_BNDLENGTH
);
572 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
573 * This matches the behavior of heimdal and mit.
575 * And it is needed to work against some closed source
580 memset(&gss_cksum
[4], 0x00, GSSAPI_BNDLENGTH
);
582 SIVAL(gss_cksum
, 20, gss_flags
);
585 SSVAL(gss_cksum
, 24, 1); /* The Delegation Option identifier */
586 SSVAL(gss_cksum
, 26, orig_length
);
587 /* Copy the kerberos KRB_CRED data */
588 memcpy(gss_cksum
+ 28, in_data
->data
, orig_length
);
590 in_data
->data
= NULL
;
593 in_data
->data
= gss_cksum
;
594 in_data
->length
= base_cksum_size
+ orig_length
;
600 we can't use krb5_mk_req because w2k wants the service to be in a particular format
602 static krb5_error_code
ads_krb5_mk_req(krb5_context context
,
603 krb5_auth_context
*auth_context
,
604 const krb5_flags ap_req_options
,
605 const char *principal
,
609 const char *impersonate_princ_s
)
611 krb5_error_code retval
;
612 krb5_principal server
;
613 krb5_principal impersonate_princ
= NULL
;
617 bool creds_ready
= False
;
618 int i
= 0, maxtries
= 3;
620 ZERO_STRUCT(in_data
);
622 retval
= smb_krb5_parse_name(context
, principal
, &server
);
624 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal
));
628 if (impersonate_princ_s
) {
629 retval
= smb_krb5_parse_name(context
, impersonate_princ_s
,
632 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s
));
637 /* obtain ticket & session key */
639 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
640 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
641 error_message(retval
)));
645 if ((retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
))) {
646 /* This can commonly fail on smbd startup with no ticket in the cache.
647 * Report at higher level than 1. */
648 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
649 error_message(retval
)));
653 while (!creds_ready
&& (i
< maxtries
)) {
655 if ((retval
= smb_krb5_get_credentials(context
, ccache
,
660 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
661 principal
, error_message(retval
)));
665 /* cope with ticket being in the future due to clock skew */
666 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
667 time_t t
= time(NULL
);
668 int time_offset
=(int)((unsigned)credsp
->times
.starttime
-t
);
669 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
670 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
673 if (!ads_cleanup_expired_creds(context
, ccache
, credsp
)) {
680 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
681 principal
, krb5_cc_get_type(context
, ccache
), krb5_cc_get_name(context
, ccache
),
682 http_timestring(talloc_tos(), (unsigned)credsp
->times
.endtime
),
683 (unsigned)credsp
->times
.endtime
));
686 *expire_time
= (time_t)credsp
->times
.endtime
;
689 /* Allocate the auth_context. */
690 retval
= setup_auth_context(context
, auth_context
);
692 DEBUG(1,("setup_auth_context failed (%s)\n",
693 error_message(retval
)));
697 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
699 uint32_t gss_flags
= 0;
701 if( credsp
->ticket_flags
& TKT_FLG_OK_AS_DELEGATE
) {
702 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
703 as part of the kerberos exchange. */
705 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
707 retval
= krb5_auth_con_setuseruserkey(context
,
711 DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
712 error_message(retval
)));
716 /* Must use a subkey for forwarded tickets. */
717 retval
= krb5_auth_con_setflags(context
,
719 KRB5_AUTH_CONTEXT_USE_SUBKEY
);
721 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
722 error_message(retval
)));
726 retval
= krb5_fwd_tgt_creds(context
,/* Krb5 context [in] */
727 *auth_context
, /* Authentication context [in] */
728 discard_const_p(char, KRB5_TGS_NAME
), /* Ticket service name ("krbtgt") [in] */
729 credsp
->client
, /* Client principal for the tgt [in] */
730 credsp
->server
, /* Server principal for the tgt [in] */
731 ccache
, /* Credential cache to use for storage [in] */
732 1, /* Turn on for "Forwardable ticket" [in] */
733 &in_data
); /* Resulting response [out] */
736 DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n",
737 error_message( retval
) ) );
740 * This is not fatal. Delete the *auth_context and continue
741 * with krb5_mk_req_extended to get a non-forwardable ticket.
745 free( in_data
.data
);
749 krb5_auth_con_free(context
, *auth_context
);
750 *auth_context
= NULL
;
751 retval
= setup_auth_context(context
, auth_context
);
753 DEBUG(1,("setup_auth_context failed (%s)\n",
754 error_message(retval
)));
758 /* We got a delegated ticket. */
759 gss_flags
|= GSS_C_DELEG_FLAG
;
763 /* Frees and reallocates in_data into a GSS checksum blob. */
764 retval
= create_gss_checksum(&in_data
, gss_flags
);
769 /* We always want GSS-checksum types. */
770 retval
= krb5_auth_con_set_req_cksumtype(context
, *auth_context
, GSSAPI_CHECKSUM
);
772 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
773 error_message(retval
)));
779 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
780 &in_data
, credsp
, outbuf
);
782 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
783 error_message(retval
)));
786 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
791 free( in_data
.data
);
795 krb5_free_creds(context
, credsp
);
798 krb5_free_cred_contents(context
, &creds
);
801 krb5_free_principal(context
, server
);
802 if (impersonate_princ
) {
803 krb5_free_principal(context
, impersonate_princ
);
810 get a kerberos5 ticket for the given service
812 int cli_krb5_get_ticket(TALLOC_CTX
*mem_ctx
,
813 const char *principal
, time_t time_offset
,
814 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
815 uint32_t extra_ap_opts
, const char *ccname
,
817 const char *impersonate_princ_s
)
820 krb5_error_code retval
;
822 krb5_context context
= NULL
;
823 krb5_ccache ccdef
= NULL
;
824 krb5_auth_context auth_context
= NULL
;
825 krb5_enctype enc_types
[] = {
826 ENCTYPE_ARCFOUR_HMAC
,
831 initialize_krb5_error_table();
832 retval
= krb5_init_context(&context
);
834 DEBUG(1, ("krb5_init_context failed (%s)\n",
835 error_message(retval
)));
839 if (time_offset
!= 0) {
840 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
843 if ((retval
= krb5_cc_resolve(context
, ccname
?
844 ccname
: krb5_cc_default_name(context
), &ccdef
))) {
845 DEBUG(1, ("krb5_cc_default failed (%s)\n",
846 error_message(retval
)));
850 if ((retval
= krb5_set_default_tgs_ktypes(context
, enc_types
))) {
851 DEBUG(1, ("krb5_set_default_tgs_ktypes failed (%s)\n",
852 error_message(retval
)));
856 retval
= ads_krb5_mk_req(context
, &auth_context
,
857 AP_OPTS_USE_SUBKEY
| (krb5_flags
)extra_ap_opts
,
858 principal
, ccdef
, &packet
,
859 tgs_expire
, impersonate_princ_s
);
864 get_krb5_smb_session_key(mem_ctx
, context
, auth_context
,
865 session_key_krb5
, false);
867 *ticket
= data_blob_talloc(mem_ctx
, packet
.data
, packet
.length
);
869 kerberos_free_data_contents(context
, &packet
);
875 krb5_cc_close(context
, ccdef
);
877 krb5_auth_con_free(context
, auth_context
);
878 krb5_free_context(context
);
884 bool get_krb5_smb_session_key(TALLOC_CTX
*mem_ctx
,
885 krb5_context context
,
886 krb5_auth_context auth_context
,
887 DATA_BLOB
*session_key
, bool remote
)
889 krb5_keyblock
*skey
= NULL
;
890 krb5_error_code err
= 0;
894 err
= krb5_auth_con_getremotesubkey(context
,
895 auth_context
, &skey
);
897 err
= krb5_auth_con_getlocalsubkey(context
,
898 auth_context
, &skey
);
901 if (err
|| skey
== NULL
) {
902 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
906 DEBUG(10, ("Got KRB5 session key of length %d\n",
907 (int)KRB5_KEY_LENGTH(skey
)));
909 *session_key
= data_blob_talloc(mem_ctx
,
911 KRB5_KEY_LENGTH(skey
));
912 dump_data_pw("KRB5 Session Key:\n",
914 session_key
->length
);
920 krb5_free_keyblock(context
, skey
);
927 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
928 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
);
930 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
)
932 static krb5_data kdata
;
934 kdata
.data
= discard_const_p(char, krb5_principal_get_comp_string(context
, principal
, i
));
935 kdata
.length
= strlen((const char *)kdata
.data
);
940 time_t get_authtime_from_tkt(krb5_ticket
*tkt
)
942 #if defined(HAVE_KRB5_TKT_ENC_PART2)
943 return tkt
->enc_part2
->times
.authtime
;
945 return tkt
->ticket
.authtime
;
949 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
950 static int get_kvno_from_ap_req(krb5_ap_req
*ap_req
)
952 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
953 if (ap_req
->ticket
->enc_part
.kvno
)
954 return ap_req
->ticket
->enc_part
.kvno
;
956 if (ap_req
->ticket
.enc_part
.kvno
)
957 return *ap_req
->ticket
.enc_part
.kvno
;
962 static krb5_enctype
get_enctype_from_ap_req(krb5_ap_req
*ap_req
)
964 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
965 return ap_req
->ticket
.enc_part
.etype
;
967 return ap_req
->ticket
->enc_part
.enctype
;
970 #endif /* HAVE_KRB5_DECODE_AP_REQ */
972 static krb5_error_code
973 get_key_from_keytab(krb5_context context
,
974 krb5_const_principal server
,
975 krb5_enctype enctype
,
977 krb5_keyblock
**out_key
)
979 krb5_keytab_entry entry
;
985 /* We have to open a new keytab handle here, as MIT does
986 an implicit open/getnext/close on krb5_kt_get_entry. We
987 may be in the middle of a keytab enumeration when this is
990 ret
= smb_krb5_open_keytab(context
, NULL
, False
, &keytab
);
992 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret
)));
996 if ( DEBUGLEVEL
>= 10 ) {
997 if (smb_krb5_unparse_name(talloc_tos(), context
, server
, &name
) == 0) {
998 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
999 kvno
, enctype
, name
));
1004 ret
= krb5_kt_get_entry(context
,
1012 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret
)));
1016 keyp
= KRB5_KT_KEY(&entry
);
1018 ret
= krb5_copy_keyblock(context
, keyp
, out_key
);
1020 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret
)));
1024 smb_krb5_kt_free_entry(context
, &entry
);
1027 krb5_kt_close(context
, keytab
);
1033 krb5_error_code
smb_krb5_get_keyinfo_from_ap_req(krb5_context context
,
1034 const krb5_data
*inbuf
,
1036 krb5_enctype
*enctype
)
1038 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1040 krb5_error_code ret
;
1043 ret
= krb5_decode_ap_req(context
, inbuf
, &ap_req
);
1047 *kvno
= get_kvno_from_ap_req(&ap_req
);
1048 *enctype
= get_enctype_from_ap_req(&ap_req
);
1050 free_AP_REQ(&ap_req
);
1055 /* Possibly not an appropriate error code. */
1056 return KRB5KDC_ERR_BADOPTION
;
1059 krb5_error_code
krb5_rd_req_return_keyblock_from_keytab(krb5_context context
,
1060 krb5_auth_context
*auth_context
,
1061 const krb5_data
*inbuf
,
1062 krb5_const_principal server
,
1064 krb5_flags
*ap_req_options
,
1065 krb5_ticket
**ticket
,
1066 krb5_keyblock
**keyblock
)
1068 krb5_error_code ret
;
1070 krb5_enctype enctype
;
1071 krb5_keyblock
*local_keyblock
;
1073 ret
= krb5_rd_req(context
,
1084 #ifdef KRB5_TICKET_HAS_KEYINFO
1085 enctype
= (*ticket
)->enc_part
.enctype
;
1086 kvno
= (*ticket
)->enc_part
.kvno
;
1088 ret
= smb_krb5_get_keyinfo_from_ap_req(context
, inbuf
, &kvno
, &enctype
);
1094 ret
= get_key_from_keytab(context
,
1100 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1105 if (ret
&& local_keyblock
!= NULL
) {
1106 krb5_free_keyblock(context
, local_keyblock
);
1108 *keyblock
= local_keyblock
;
1114 krb5_error_code
smb_krb5_renew_ticket(const char *ccache_string
, /* FILE:/tmp/krb5cc_0 */
1115 const char *client_string
, /* gd@BER.SUSE.DE */
1116 const char *service_string
, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1117 time_t *expire_time
)
1119 krb5_error_code ret
;
1120 krb5_context context
= NULL
;
1121 krb5_ccache ccache
= NULL
;
1122 krb5_principal client
= NULL
;
1123 krb5_creds creds
, creds_in
, *creds_out
= NULL
;
1126 ZERO_STRUCT(creds_in
);
1128 initialize_krb5_error_table();
1129 ret
= krb5_init_context(&context
);
1134 if (!ccache_string
) {
1135 ccache_string
= krb5_cc_default_name(context
);
1138 if (!ccache_string
) {
1143 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string
));
1145 /* FIXME: we should not fall back to defaults */
1146 ret
= krb5_cc_resolve(context
, discard_const_p(char, ccache_string
), &ccache
);
1151 if (client_string
) {
1152 ret
= smb_krb5_parse_name(context
, client_string
, &client
);
1157 ret
= krb5_cc_get_principal(context
, ccache
, &client
);
1163 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1165 ret
= krb5_get_renewed_creds(context
, &creds
, client
, ccache
, discard_const_p(char, service_string
));
1167 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1171 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1173 krb5_kdc_flags flags
;
1174 krb5_realm
*client_realm
= NULL
;
1176 ret
= krb5_copy_principal(context
, client
, &creds_in
.client
);
1181 if (service_string
) {
1182 ret
= smb_krb5_parse_name(context
, service_string
, &creds_in
.server
);
1187 /* build tgt service by default */
1188 client_realm
= krb5_princ_realm(context
, creds_in
.client
);
1189 if (!client_realm
) {
1193 ret
= krb5_make_principal(context
, &creds_in
.server
, *client_realm
, KRB5_TGS_NAME
, *client_realm
, NULL
);
1200 flags
.b
.renewable
= flags
.b
.renew
= True
;
1202 ret
= krb5_get_kdc_cred(context
, ccache
, flags
, NULL
, NULL
, &creds_in
, &creds_out
);
1204 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1211 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1214 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1215 ret
= krb5_cc_initialize(context
, ccache
, client
);
1220 ret
= krb5_cc_store_cred(context
, ccache
, &creds
);
1223 *expire_time
= (time_t) creds
.times
.endtime
;
1227 krb5_free_cred_contents(context
, &creds_in
);
1230 krb5_free_creds(context
, creds_out
);
1232 krb5_free_cred_contents(context
, &creds
);
1236 krb5_free_principal(context
, client
);
1239 krb5_cc_close(context
, ccache
);
1242 krb5_free_context(context
);
1248 krb5_error_code
smb_krb5_free_addresses(krb5_context context
, smb_krb5_addresses
*addr
)
1250 krb5_error_code ret
= 0;
1254 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1255 krb5_free_addresses(context
, addr
->addrs
);
1256 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1257 ret
= krb5_free_addresses(context
, addr
->addrs
);
1258 SAFE_FREE(addr
->addrs
);
1265 krb5_error_code
smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses
**kerb_addr
)
1267 krb5_error_code ret
= 0;
1269 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1270 krb5_address
**addrs
= NULL
;
1271 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1272 krb5_addresses
*addrs
= NULL
;
1275 *kerb_addr
= (smb_krb5_addresses
*)SMB_MALLOC(sizeof(smb_krb5_addresses
));
1276 if (*kerb_addr
== NULL
) {
1280 put_name(buf
, lp_netbios_name(), ' ', 0x20);
1282 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1286 addrs
= (krb5_address
**)SMB_MALLOC(sizeof(krb5_address
*) * num_addr
);
1287 if (addrs
== NULL
) {
1288 SAFE_FREE(*kerb_addr
);
1292 memset(addrs
, 0, sizeof(krb5_address
*) * num_addr
);
1294 addrs
[0] = (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1295 if (addrs
[0] == NULL
) {
1297 SAFE_FREE(*kerb_addr
);
1301 addrs
[0]->magic
= KV5M_ADDRESS
;
1302 addrs
[0]->addrtype
= KRB5_ADDR_NETBIOS
;
1303 addrs
[0]->length
= MAX_NETBIOSNAME_LEN
;
1304 addrs
[0]->contents
= (unsigned char *)SMB_MALLOC(addrs
[0]->length
);
1305 if (addrs
[0]->contents
== NULL
) {
1306 SAFE_FREE(addrs
[0]);
1308 SAFE_FREE(*kerb_addr
);
1312 memcpy(addrs
[0]->contents
, buf
, addrs
[0]->length
);
1316 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1318 addrs
= (krb5_addresses
*)SMB_MALLOC(sizeof(krb5_addresses
));
1319 if (addrs
== NULL
) {
1320 SAFE_FREE(*kerb_addr
);
1324 memset(addrs
, 0, sizeof(krb5_addresses
));
1327 addrs
->val
= (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1328 if (addrs
->val
== NULL
) {
1330 SAFE_FREE(kerb_addr
);
1334 addrs
->val
[0].addr_type
= KRB5_ADDR_NETBIOS
;
1335 addrs
->val
[0].address
.length
= MAX_NETBIOSNAME_LEN
;
1336 addrs
->val
[0].address
.data
= (unsigned char *)SMB_MALLOC(addrs
->val
[0].address
.length
);
1337 if (addrs
->val
[0].address
.data
== NULL
) {
1338 SAFE_FREE(addrs
->val
);
1340 SAFE_FREE(*kerb_addr
);
1344 memcpy(addrs
->val
[0].address
.data
, buf
, addrs
->val
[0].address
.length
);
1347 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1349 (*kerb_addr
)->addrs
= addrs
;
1354 void smb_krb5_free_error(krb5_context context
, krb5_error
*krberror
)
1356 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1357 krb5_free_error_contents(context
, krberror
);
1359 krb5_free_error(context
, krberror
);
1363 krb5_error_code
handle_krberror_packet(krb5_context context
,
1366 krb5_error_code ret
;
1367 bool got_error_code
= False
;
1369 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1371 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1373 krb5_error krberror
;
1375 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1376 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1377 error_message(ret
)));
1381 if (krberror
.e_data
== NULL
|| krberror
.e_data
->data
== NULL
) {
1382 ret
= (krb5_error_code
) krberror
.error_code
;
1383 got_error_code
= True
;
1386 smb_krb5_free_error(context
, &krberror
);
1390 krb5_error
*krberror
;
1392 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1393 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1394 error_message(ret
)));
1398 if (krberror
->e_data
.data
== NULL
) {
1399 #if defined(ERROR_TABLE_BASE_krb5)
1400 ret
= ERROR_TABLE_BASE_krb5
+ (krb5_error_code
) krberror
->error
;
1402 ret
= (krb5_error_code
)krberror
->error
;
1404 got_error_code
= True
;
1406 smb_krb5_free_error(context
, krberror
);
1409 if (got_error_code
) {
1410 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1411 error_message(ret
), ret
));
1416 krb5_error_code
smb_krb5_get_init_creds_opt_alloc(krb5_context context
,
1417 krb5_get_init_creds_opt
**opt
)
1419 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1420 /* Heimdal or modern MIT version */
1421 return krb5_get_init_creds_opt_alloc(context
, opt
);
1423 /* Historical MIT version */
1424 krb5_get_init_creds_opt
*my_opt
;
1428 if ((my_opt
= SMB_MALLOC_P(krb5_get_init_creds_opt
)) == NULL
) {
1432 krb5_get_init_creds_opt_init(my_opt
);
1436 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1439 void smb_krb5_get_init_creds_opt_free(krb5_context context
,
1440 krb5_get_init_creds_opt
*opt
)
1442 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1444 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1445 /* Modern MIT or Heimdal version */
1446 krb5_get_init_creds_opt_free(context
, opt
);
1448 /* Heimdal version */
1449 krb5_get_init_creds_opt_free(opt
);
1450 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1452 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1453 /* Historical MIT version */
1456 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1459 krb5_enctype
smb_get_enctype_from_kt_entry(krb5_keytab_entry
*kt_entry
)
1461 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry
));
1465 /* caller needs to free etype_s */
1466 krb5_error_code
smb_krb5_enctype_to_string(krb5_context context
,
1467 krb5_enctype enctype
,
1470 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1471 return krb5_enctype_to_string(context
, enctype
, etype_s
); /* Heimdal */
1472 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1474 krb5_error_code ret
= krb5_enctype_to_string(enctype
, buf
, 256); /* MIT */
1478 *etype_s
= SMB_STRDUP(buf
);
1484 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1488 krb5_error_code
smb_krb5_mk_error(krb5_context context
,
1489 krb5_error_code error_code
,
1490 const krb5_principal server
,
1493 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1495 * The MIT interface is *terrible*.
1496 * We have to construct this ourselves...
1500 memset(&e
, 0, sizeof(e
));
1501 krb5_us_timeofday(context
, &e
.stime
, &e
.susec
);
1503 #if defined(krb5_err_base)
1504 e
.error
= error_code
- krb5_err_base
;
1505 #elif defined(ERROR_TABLE_BASE_krb5)
1506 e
.error
= error_code
- ERROR_TABLE_BASE_krb5
;
1508 e
.error
= error_code
; /* Almost certainly wrong, but what can we do... ? */
1511 return krb5_mk_error(context
, &e
, reply
);
1512 #else /* Heimdal. */
1513 return krb5_mk_error(context
,
1525 /**********************************************************************
1526 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1527 * allows to process non-default keytab names.
1528 * @param context krb5_context
1529 * @param keytab_name_req string
1530 * @param write_access bool if writable keytab is required
1531 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1532 * @return krb5_error_code
1533 **********************************************************************/
1535 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1536 #ifndef MAX_KEYTAB_NAME_LEN
1537 #define MAX_KEYTAB_NAME_LEN 1100
1540 krb5_error_code
smb_krb5_open_keytab(krb5_context context
,
1541 const char *keytab_name_req
,
1543 krb5_keytab
*keytab
)
1545 krb5_error_code ret
= 0;
1546 TALLOC_CTX
*mem_ctx
;
1547 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1548 char *kt_str
= NULL
;
1549 bool found_valid_name
= False
;
1550 const char *pragma
= "FILE";
1551 const char *tmp
= NULL
;
1553 if (!write_access
&& !keytab_name_req
) {
1554 /* caller just wants to read the default keytab readonly, so be it */
1555 return krb5_kt_default(context
, keytab
);
1558 mem_ctx
= talloc_init("smb_krb5_open_keytab");
1563 #ifdef HAVE_WRFILE_KEYTAB
1569 if (keytab_name_req
) {
1571 if (strlen(keytab_name_req
) > MAX_KEYTAB_NAME_LEN
) {
1572 ret
= KRB5_CONFIG_NOTENUFSPACE
;
1576 if ((strncmp(keytab_name_req
, "WRFILE:/", 8) == 0) ||
1577 (strncmp(keytab_name_req
, "FILE:/", 6) == 0)) {
1578 tmp
= keytab_name_req
;
1582 if (keytab_name_req
[0] != '/') {
1583 ret
= KRB5_KT_BADNAME
;
1587 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, keytab_name_req
);
1596 /* we need to handle more complex keytab_strings, like:
1597 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1599 ret
= krb5_kt_default_name(context
, &keytab_string
[0], MAX_KEYTAB_NAME_LEN
- 2);
1604 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string
));
1606 tmp
= talloc_strdup(mem_ctx
, keytab_string
);
1612 if (strncmp(tmp
, "ANY:", 4) == 0) {
1616 memset(&keytab_string
, '\0', sizeof(keytab_string
));
1618 while (next_token_talloc(mem_ctx
, &tmp
, &kt_str
, ",")) {
1619 if (strncmp(kt_str
, "WRFILE:", 7) == 0) {
1620 found_valid_name
= True
;
1625 if (strncmp(kt_str
, "FILE:", 5) == 0) {
1626 found_valid_name
= True
;
1631 if (tmp
[0] == '/') {
1632 /* Treat as a FILE: keytab definition. */
1633 found_valid_name
= true;
1636 if (found_valid_name
) {
1637 if (tmp
[0] != '/') {
1638 ret
= KRB5_KT_BADNAME
;
1642 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, tmp
);
1651 if (!found_valid_name
) {
1652 ret
= KRB5_KT_UNKNOWN_TYPE
;
1657 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp
));
1658 ret
= krb5_kt_resolve(context
, tmp
, keytab
);
1661 TALLOC_FREE(mem_ctx
);
1665 krb5_error_code
smb_krb5_keytab_name(TALLOC_CTX
*mem_ctx
,
1666 krb5_context context
,
1668 const char **keytab_name
)
1670 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1671 krb5_error_code ret
= 0;
1673 ret
= krb5_kt_get_name(context
, keytab
,
1674 keytab_string
, MAX_KEYTAB_NAME_LEN
- 2);
1679 *keytab_name
= talloc_strdup(mem_ctx
, keytab_string
);
1680 if (!*keytab_name
) {
1687 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1688 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1689 defined(HAVE_KRB5_GET_CREDS)
1690 static krb5_error_code
smb_krb5_get_credentials_for_user_opt(krb5_context context
,
1693 krb5_principal server
,
1694 krb5_principal impersonate_princ
,
1695 krb5_creds
**out_creds
)
1697 krb5_error_code ret
;
1698 krb5_get_creds_opt opt
;
1700 ret
= krb5_get_creds_opt_alloc(context
, &opt
);
1704 krb5_get_creds_opt_add_options(context
, opt
, KRB5_GC_FORWARDABLE
);
1706 if (impersonate_princ
) {
1707 ret
= krb5_get_creds_opt_set_impersonate(context
, opt
,
1714 ret
= krb5_get_creds(context
, opt
, ccache
, server
, out_creds
);
1721 krb5_get_creds_opt_free(context
, opt
);
1725 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
1727 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
1728 static krb5_error_code
smb_krb5_get_credentials_for_user(krb5_context context
,
1731 krb5_principal server
,
1732 krb5_principal impersonate_princ
,
1733 krb5_creds
**out_creds
)
1735 krb5_error_code ret
;
1736 krb5_creds in_creds
;
1738 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1739 krb5_error_code KRB5_CALLCONV
1740 krb5_get_credentials_for_user(krb5_context context
, krb5_flags options
,
1741 krb5_ccache ccache
, krb5_creds
*in_creds
,
1742 krb5_data
*subject_cert
,
1743 krb5_creds
**out_creds
);
1744 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1746 ZERO_STRUCT(in_creds
);
1748 if (impersonate_princ
) {
1750 in_creds
.server
= me
;
1751 in_creds
.client
= impersonate_princ
;
1753 ret
= krb5_get_credentials_for_user(context
,
1754 0, /* krb5_flags options */
1757 NULL
, /* krb5_data *subject_cert */
1760 in_creds
.client
= me
;
1761 in_creds
.server
= server
;
1763 ret
= krb5_get_credentials(context
, 0, ccache
,
1764 &in_creds
, out_creds
);
1769 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1772 * smb_krb5_get_credentials
1774 * @brief Get krb5 credentials for a server
1776 * @param[in] context An initialized krb5_context
1777 * @param[in] ccache An initialized krb5_ccache
1778 * @param[in] me The krb5_principal of the caller
1779 * @param[in] server The krb5_principal of the requested service
1780 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1781 * @param[out] out_creds The returned krb5_creds structure
1782 * @return krb5_error_code
1785 krb5_error_code
smb_krb5_get_credentials(krb5_context context
,
1788 krb5_principal server
,
1789 krb5_principal impersonate_princ
,
1790 krb5_creds
**out_creds
)
1792 krb5_error_code ret
;
1793 krb5_creds
*creds
= NULL
;
1797 if (impersonate_princ
) {
1798 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
1799 ret
= smb_krb5_get_credentials_for_user_opt(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
1800 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
1801 ret
= smb_krb5_get_credentials_for_user(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
1806 krb5_creds in_creds
;
1808 ZERO_STRUCT(in_creds
);
1810 in_creds
.client
= me
;
1811 in_creds
.server
= server
;
1813 ret
= krb5_get_credentials(context
, 0, ccache
,
1826 krb5_free_creds(context
, creds
);
1833 * smb_krb5_get_creds
1835 * @brief Get krb5 credentials for a server
1837 * @param[in] server_s The string name of the service
1838 * @param[in] time_offset The offset to the KDCs time in seconds (optional)
1839 * @param[in] cc The krb5 credential cache string name (optional)
1840 * @param[in] impersonate_princ_s The string principal name to impersonate (optional)
1841 * @param[out] creds_p The returned krb5_creds structure
1842 * @return krb5_error_code
1845 krb5_error_code
smb_krb5_get_creds(const char *server_s
,
1848 const char *impersonate_princ_s
,
1849 krb5_creds
**creds_p
)
1851 krb5_error_code ret
;
1852 krb5_context context
= NULL
;
1853 krb5_principal me
= NULL
;
1854 krb5_principal server
= NULL
;
1855 krb5_principal impersonate_princ
= NULL
;
1856 krb5_creds
*creds
= NULL
;
1857 krb5_ccache ccache
= NULL
;
1861 initialize_krb5_error_table();
1862 ret
= krb5_init_context(&context
);
1867 if (time_offset
!= 0) {
1868 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
1871 ret
= krb5_cc_resolve(context
, cc
? cc
:
1872 krb5_cc_default_name(context
), &ccache
);
1877 ret
= krb5_cc_get_principal(context
, ccache
, &me
);
1882 ret
= smb_krb5_parse_name(context
, server_s
, &server
);
1887 if (impersonate_princ_s
) {
1888 ret
= smb_krb5_parse_name(context
, impersonate_princ_s
,
1889 &impersonate_princ
);
1895 ret
= smb_krb5_get_credentials(context
, ccache
,
1896 me
, server
, impersonate_princ
,
1902 ret
= krb5_cc_store_cred(context
, ccache
, creds
);
1911 DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n",
1914 if (impersonate_princ_s
) {
1915 char *client
= NULL
;
1917 ret
= smb_krb5_unparse_name(talloc_tos(), context
, creds
->client
, &client
);
1921 DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n",
1923 TALLOC_FREE(client
);
1932 krb5_free_creds(context
, creds
);
1935 krb5_free_principal(context
, server
);
1938 krb5_free_principal(context
, me
);
1940 if (impersonate_princ
) {
1941 krb5_free_principal(context
, impersonate_princ
);
1944 krb5_cc_close(context
, ccache
);
1946 krb5_free_context(context
);
1952 * smb_krb5_principal_get_realm
1954 * @brief Get realm of a principal
1956 * @param[in] context The krb5_context
1957 * @param[in] principal The principal
1958 * @return pointer to the realm
1962 char *smb_krb5_principal_get_realm(krb5_context context
,
1963 krb5_principal principal
)
1965 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
1966 return discard_const_p(char, krb5_principal_get_realm(context
, principal
));
1967 #elif defined(krb5_princ_realm) /* MIT */
1969 realm
= krb5_princ_realm(context
, principal
);
1970 return discard_const_p(char, realm
->data
);
1976 #else /* HAVE_KRB5 */
1977 /* this saves a few linking headaches */
1978 int cli_krb5_get_ticket(TALLOC_CTX
*mem_ctx
,
1979 const char *principal
, time_t time_offset
,
1980 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
1981 uint32_t extra_ap_opts
,
1982 const char *ccname
, time_t *tgs_expire
,
1983 const char *impersonate_princ_s
)
1985 DEBUG(0,("NO KERBEROS SUPPORT\n"));
1989 bool unwrap_pac(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, DATA_BLOB
*unwrapped_pac_data
)
1991 DEBUG(0,("NO KERBEROS SUPPORT\n"));
1995 #endif /* HAVE_KRB5 */