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 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
55 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
57 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
58 * to krb5_set_default_tgs_ktypes. See
59 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
61 * If the MIT libraries are not exporting internal symbols, we will end up in
62 * this branch, which is correct. Otherwise we will continue to use the
65 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
67 return krb5_set_default_tgs_enctypes(ctx
, enc
);
70 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
73 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
75 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
78 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
80 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
82 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
84 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
86 memset(pkaddr
, '\0', sizeof(krb5_address
));
87 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
88 if (paddr
->ss_family
== AF_INET6
) {
89 pkaddr
->addr_type
= KRB5_ADDRESS_INET6
;
90 pkaddr
->address
.length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
91 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
95 if (paddr
->ss_family
== AF_INET
) {
96 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
97 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
98 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
103 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
105 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
107 memset(pkaddr
, '\0', sizeof(krb5_address
));
108 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
109 if (paddr
->ss_family
== AF_INET6
) {
110 pkaddr
->addrtype
= ADDRTYPE_INET6
;
111 pkaddr
->length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
112 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
116 if (paddr
->ss_family
== AF_INET
) {
117 pkaddr
->addrtype
= ADDRTYPE_INET
;
118 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
119 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
125 #error UNKNOWN_ADDRTYPE
128 int create_kerberos_key_from_string(krb5_context context
,
129 krb5_principal host_princ
,
132 krb5_enctype enctype
,
135 krb5_principal salt_princ
= NULL
;
138 * Check if we've determined that the KDC is salting keys for this
139 * principal/enctype in a non-obvious way. If it is, try to match
143 KRB5_KEY_DATA(key
) = (KRB5_KEY_DATA_CAST
*)SMB_MALLOC(password
->length
);
144 if (!KRB5_KEY_DATA(key
)) {
147 memcpy(KRB5_KEY_DATA(key
), password
->data
, password
->length
);
148 KRB5_KEY_LENGTH(key
) = password
->length
;
149 KRB5_KEY_TYPE(key
) = enctype
;
152 salt_princ
= kerberos_fetch_salt_princ_for_host_princ(context
, host_princ
, enctype
);
153 ret
= create_kerberos_key_from_string_direct(context
, salt_princ
? salt_princ
: host_princ
, password
, key
, enctype
);
155 krb5_free_principal(context
, salt_princ
);
160 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
161 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
162 krb5_enctype
**enctypes
)
164 return krb5_get_permitted_enctypes(context
, enctypes
);
166 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
167 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
168 krb5_enctype
**enctypes
)
170 #ifdef HAVE_KRB5_PDU_NONE_DECL
171 return krb5_get_default_in_tkt_etypes(context
, KRB5_PDU_NONE
, enctypes
);
173 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
177 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
180 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
181 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
182 krb5_auth_context auth_context
,
183 krb5_keyblock
*keyblock
)
185 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
189 bool unwrap_edata_ntstatus(TALLOC_CTX
*mem_ctx
,
191 DATA_BLOB
*edata_out
)
193 DATA_BLOB edata_contents
;
197 if (!edata
->length
) {
201 data
= asn1_init(mem_ctx
);
206 asn1_load(data
, *edata
);
207 asn1_start_tag(data
, ASN1_SEQUENCE(0));
208 asn1_start_tag(data
, ASN1_CONTEXT(1));
209 asn1_read_Integer(data
, &edata_type
);
211 if (edata_type
!= KRB5_PADATA_PW_SALT
) {
212 DEBUG(0,("edata is not of required type %d but of type %d\n",
213 KRB5_PADATA_PW_SALT
, edata_type
));
218 asn1_start_tag(data
, ASN1_CONTEXT(2));
219 asn1_read_OctetString(data
, talloc_tos(), &edata_contents
);
225 *edata_out
= data_blob_talloc(mem_ctx
, edata_contents
.data
, edata_contents
.length
);
227 data_blob_free(&edata_contents
);
233 bool unwrap_pac(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, DATA_BLOB
*unwrapped_pac_data
)
235 DATA_BLOB pac_contents
;
239 if (!auth_data
->length
) {
243 data
= asn1_init(mem_ctx
);
248 asn1_load(data
, *auth_data
);
249 asn1_start_tag(data
, ASN1_SEQUENCE(0));
250 asn1_start_tag(data
, ASN1_SEQUENCE(0));
251 asn1_start_tag(data
, ASN1_CONTEXT(0));
252 asn1_read_Integer(data
, &data_type
);
254 if (data_type
!= KRB5_AUTHDATA_WIN2K_PAC
) {
255 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type
));
261 asn1_start_tag(data
, ASN1_CONTEXT(1));
262 asn1_read_OctetString(data
, talloc_tos(), &pac_contents
);
268 *unwrapped_pac_data
= data_blob_talloc(mem_ctx
, pac_contents
.data
, pac_contents
.length
);
270 data_blob_free(&pac_contents
);
275 bool get_auth_data_from_tkt(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, krb5_ticket
*tkt
)
277 DATA_BLOB auth_data_wrapped
;
278 bool got_auth_data_pac
= False
;
281 #if defined(HAVE_KRB5_TKT_ENC_PART2)
282 if (tkt
->enc_part2
&& tkt
->enc_part2
->authorization_data
&&
283 tkt
->enc_part2
->authorization_data
[0] &&
284 tkt
->enc_part2
->authorization_data
[0]->length
)
286 for (i
= 0; tkt
->enc_part2
->authorization_data
[i
] != NULL
; i
++) {
288 if (tkt
->enc_part2
->authorization_data
[i
]->ad_type
!=
289 KRB5_AUTHDATA_IF_RELEVANT
) {
290 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
291 tkt
->enc_part2
->authorization_data
[i
]->ad_type
));
295 auth_data_wrapped
= data_blob(tkt
->enc_part2
->authorization_data
[i
]->contents
,
296 tkt
->enc_part2
->authorization_data
[i
]->length
);
298 /* check if it is a PAC */
299 got_auth_data_pac
= unwrap_pac(mem_ctx
, &auth_data_wrapped
, auth_data
);
300 data_blob_free(&auth_data_wrapped
);
302 if (got_auth_data_pac
) {
307 return got_auth_data_pac
;
311 if (tkt
->ticket
.authorization_data
&&
312 tkt
->ticket
.authorization_data
->len
)
314 for (i
= 0; i
< tkt
->ticket
.authorization_data
->len
; i
++) {
316 if (tkt
->ticket
.authorization_data
->val
[i
].ad_type
!=
317 KRB5_AUTHDATA_IF_RELEVANT
) {
318 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
319 tkt
->ticket
.authorization_data
->val
[i
].ad_type
));
323 auth_data_wrapped
= data_blob(tkt
->ticket
.authorization_data
->val
[i
].ad_data
.data
,
324 tkt
->ticket
.authorization_data
->val
[i
].ad_data
.length
);
326 /* check if it is a PAC */
327 got_auth_data_pac
= unwrap_pac(mem_ctx
, &auth_data_wrapped
, auth_data
);
328 data_blob_free(&auth_data_wrapped
);
330 if (got_auth_data_pac
) {
335 return got_auth_data_pac
;
341 krb5_const_principal
get_principal_from_tkt(krb5_ticket
*tkt
)
343 #if defined(HAVE_KRB5_TKT_ENC_PART2)
344 return tkt
->enc_part2
->client
;
350 static bool ads_cleanup_expired_creds(krb5_context context
,
354 krb5_error_code retval
;
355 const char *cc_type
= krb5_cc_get_type(context
, ccache
);
357 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
358 cc_type
, krb5_cc_get_name(context
, ccache
),
359 http_timestring(talloc_tos(), credsp
->times
.endtime
)));
361 /* we will probably need new tickets if the current ones
362 will expire within 10 seconds.
364 if (credsp
->times
.endtime
>= (time(NULL
) + 10))
367 /* heimdal won't remove creds from a file ccache, and
368 perhaps we shouldn't anyway, since internally we
369 use memory ccaches, and a FILE one probably means that
370 we're using creds obtained outside of our exectuable
372 if (strequal(cc_type
, "FILE")) {
373 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type
));
377 retval
= krb5_cc_remove_cred(context
, ccache
, 0, credsp
);
379 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
380 error_message(retval
)));
381 /* If we have an error in this, we want to display it,
382 but continue as though we deleted it */
387 /* Allocate and setup the auth context into the state we need. */
389 static krb5_error_code
setup_auth_context(krb5_context context
,
390 krb5_auth_context
*auth_context
)
392 krb5_error_code retval
;
394 retval
= krb5_auth_con_init(context
, auth_context
);
396 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
397 error_message(retval
)));
401 /* Ensure this is an addressless ticket. */
402 retval
= krb5_auth_con_setaddrs(context
, *auth_context
, NULL
, NULL
);
404 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
405 error_message(retval
)));
411 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
412 static krb5_error_code
create_gss_checksum(krb5_data
*in_data
, /* [inout] */
415 unsigned int orig_length
= in_data
->length
;
416 unsigned int base_cksum_size
= GSSAPI_CHECKSUM_SIZE
;
417 char *gss_cksum
= NULL
;
420 /* Extra length field for delgated ticket. */
421 base_cksum_size
+= 4;
424 if ((unsigned int)base_cksum_size
+ orig_length
<
425 (unsigned int)base_cksum_size
) {
429 gss_cksum
= (char *)SMB_MALLOC(base_cksum_size
+ orig_length
);
430 if (gss_cksum
== NULL
) {
434 memset(gss_cksum
, '\0', base_cksum_size
+ orig_length
);
435 SIVAL(gss_cksum
, 0, GSSAPI_BNDLENGTH
);
438 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
439 * This matches the behavior of heimdal and mit.
441 * And it is needed to work against some closed source
446 memset(&gss_cksum
[4], 0x00, GSSAPI_BNDLENGTH
);
448 SIVAL(gss_cksum
, 20, gss_flags
);
451 SSVAL(gss_cksum
, 24, 1); /* The Delegation Option identifier */
452 SSVAL(gss_cksum
, 26, orig_length
);
453 /* Copy the kerberos KRB_CRED data */
454 memcpy(gss_cksum
+ 28, in_data
->data
, orig_length
);
456 in_data
->data
= NULL
;
459 in_data
->data
= gss_cksum
;
460 in_data
->length
= base_cksum_size
+ orig_length
;
466 we can't use krb5_mk_req because w2k wants the service to be in a particular format
468 static krb5_error_code
ads_krb5_mk_req(krb5_context context
,
469 krb5_auth_context
*auth_context
,
470 const krb5_flags ap_req_options
,
471 const char *principal
,
475 const char *impersonate_princ_s
)
477 krb5_error_code retval
;
478 krb5_principal server
;
479 krb5_principal impersonate_princ
= NULL
;
483 bool creds_ready
= False
;
484 int i
= 0, maxtries
= 3;
486 ZERO_STRUCT(in_data
);
488 retval
= smb_krb5_parse_name(context
, principal
, &server
);
490 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal
));
494 if (impersonate_princ_s
) {
495 retval
= smb_krb5_parse_name(context
, impersonate_princ_s
,
498 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s
));
503 /* obtain ticket & session key */
505 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
506 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
507 error_message(retval
)));
511 if ((retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
))) {
512 /* This can commonly fail on smbd startup with no ticket in the cache.
513 * Report at higher level than 1. */
514 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
515 error_message(retval
)));
519 while (!creds_ready
&& (i
< maxtries
)) {
521 if ((retval
= smb_krb5_get_credentials(context
, ccache
,
526 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
527 principal
, error_message(retval
)));
531 /* cope with ticket being in the future due to clock skew */
532 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
533 time_t t
= time(NULL
);
534 int time_offset
=(int)((unsigned)credsp
->times
.starttime
-t
);
535 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
536 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
539 if (!ads_cleanup_expired_creds(context
, ccache
, credsp
)) {
546 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
547 principal
, krb5_cc_get_type(context
, ccache
), krb5_cc_get_name(context
, ccache
),
548 http_timestring(talloc_tos(), (unsigned)credsp
->times
.endtime
),
549 (unsigned)credsp
->times
.endtime
));
552 *expire_time
= (time_t)credsp
->times
.endtime
;
555 /* Allocate the auth_context. */
556 retval
= setup_auth_context(context
, auth_context
);
558 DEBUG(1,("setup_auth_context failed (%s)\n",
559 error_message(retval
)));
563 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
565 uint32_t gss_flags
= 0;
567 if( credsp
->ticket_flags
& TKT_FLG_OK_AS_DELEGATE
) {
568 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
569 as part of the kerberos exchange. */
571 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
573 retval
= krb5_auth_con_setuseruserkey(context
,
577 DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
578 error_message(retval
)));
582 /* Must use a subkey for forwarded tickets. */
583 retval
= krb5_auth_con_setflags(context
,
585 KRB5_AUTH_CONTEXT_USE_SUBKEY
);
587 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
588 error_message(retval
)));
592 retval
= krb5_fwd_tgt_creds(context
,/* Krb5 context [in] */
593 *auth_context
, /* Authentication context [in] */
594 discard_const_p(char, KRB5_TGS_NAME
), /* Ticket service name ("krbtgt") [in] */
595 credsp
->client
, /* Client principal for the tgt [in] */
596 credsp
->server
, /* Server principal for the tgt [in] */
597 ccache
, /* Credential cache to use for storage [in] */
598 1, /* Turn on for "Forwardable ticket" [in] */
599 &in_data
); /* Resulting response [out] */
602 DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n",
603 error_message( retval
) ) );
606 * This is not fatal. Delete the *auth_context and continue
607 * with krb5_mk_req_extended to get a non-forwardable ticket.
611 free( in_data
.data
);
615 krb5_auth_con_free(context
, *auth_context
);
616 *auth_context
= NULL
;
617 retval
= setup_auth_context(context
, auth_context
);
619 DEBUG(1,("setup_auth_context failed (%s)\n",
620 error_message(retval
)));
624 /* We got a delegated ticket. */
625 gss_flags
|= GSS_C_DELEG_FLAG
;
629 /* Frees and reallocates in_data into a GSS checksum blob. */
630 retval
= create_gss_checksum(&in_data
, gss_flags
);
635 /* We always want GSS-checksum types. */
636 retval
= krb5_auth_con_set_req_cksumtype(context
, *auth_context
, GSSAPI_CHECKSUM
);
638 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
639 error_message(retval
)));
645 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
646 &in_data
, credsp
, outbuf
);
648 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
649 error_message(retval
)));
652 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
657 free( in_data
.data
);
661 krb5_free_creds(context
, credsp
);
664 krb5_free_cred_contents(context
, &creds
);
667 krb5_free_principal(context
, server
);
668 if (impersonate_princ
) {
669 krb5_free_principal(context
, impersonate_princ
);
676 get a kerberos5 ticket for the given service
678 int cli_krb5_get_ticket(TALLOC_CTX
*mem_ctx
,
679 const char *principal
, time_t time_offset
,
680 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
681 uint32_t extra_ap_opts
, const char *ccname
,
683 const char *impersonate_princ_s
)
686 krb5_error_code retval
;
688 krb5_context context
= NULL
;
689 krb5_ccache ccdef
= NULL
;
690 krb5_auth_context auth_context
= NULL
;
691 krb5_enctype enc_types
[] = {
692 ENCTYPE_ARCFOUR_HMAC
,
697 initialize_krb5_error_table();
698 retval
= krb5_init_context(&context
);
700 DEBUG(1, ("krb5_init_context failed (%s)\n",
701 error_message(retval
)));
705 if (time_offset
!= 0) {
706 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
709 if ((retval
= krb5_cc_resolve(context
, ccname
?
710 ccname
: krb5_cc_default_name(context
), &ccdef
))) {
711 DEBUG(1, ("krb5_cc_default failed (%s)\n",
712 error_message(retval
)));
716 if ((retval
= krb5_set_default_tgs_ktypes(context
, enc_types
))) {
717 DEBUG(1, ("krb5_set_default_tgs_ktypes failed (%s)\n",
718 error_message(retval
)));
722 retval
= ads_krb5_mk_req(context
, &auth_context
,
723 AP_OPTS_USE_SUBKEY
| (krb5_flags
)extra_ap_opts
,
724 principal
, ccdef
, &packet
,
725 tgs_expire
, impersonate_princ_s
);
730 get_krb5_smb_session_key(mem_ctx
, context
, auth_context
,
731 session_key_krb5
, false);
733 *ticket
= data_blob_talloc(mem_ctx
, packet
.data
, packet
.length
);
735 kerberos_free_data_contents(context
, &packet
);
741 krb5_cc_close(context
, ccdef
);
743 krb5_auth_con_free(context
, auth_context
);
744 krb5_free_context(context
);
750 bool get_krb5_smb_session_key(TALLOC_CTX
*mem_ctx
,
751 krb5_context context
,
752 krb5_auth_context auth_context
,
753 DATA_BLOB
*session_key
, bool remote
)
755 krb5_keyblock
*skey
= NULL
;
756 krb5_error_code err
= 0;
760 err
= krb5_auth_con_getremotesubkey(context
,
761 auth_context
, &skey
);
763 err
= krb5_auth_con_getlocalsubkey(context
,
764 auth_context
, &skey
);
767 if (err
|| skey
== NULL
) {
768 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
772 DEBUG(10, ("Got KRB5 session key of length %d\n",
773 (int)KRB5_KEY_LENGTH(skey
)));
775 *session_key
= data_blob_talloc(mem_ctx
,
777 KRB5_KEY_LENGTH(skey
));
778 dump_data_pw("KRB5 Session Key:\n",
780 session_key
->length
);
786 krb5_free_keyblock(context
, skey
);
793 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
794 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
);
796 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
)
798 static krb5_data kdata
;
800 kdata
.data
= discard_const_p(char, krb5_principal_get_comp_string(context
, principal
, i
));
801 kdata
.length
= strlen((const char *)kdata
.data
);
806 time_t get_authtime_from_tkt(krb5_ticket
*tkt
)
808 #if defined(HAVE_KRB5_TKT_ENC_PART2)
809 return tkt
->enc_part2
->times
.authtime
;
811 return tkt
->ticket
.authtime
;
815 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
816 static int get_kvno_from_ap_req(krb5_ap_req
*ap_req
)
818 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
819 if (ap_req
->ticket
->enc_part
.kvno
)
820 return ap_req
->ticket
->enc_part
.kvno
;
822 if (ap_req
->ticket
.enc_part
.kvno
)
823 return *ap_req
->ticket
.enc_part
.kvno
;
828 static krb5_enctype
get_enctype_from_ap_req(krb5_ap_req
*ap_req
)
830 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
831 return ap_req
->ticket
.enc_part
.etype
;
833 return ap_req
->ticket
->enc_part
.enctype
;
836 #endif /* HAVE_KRB5_DECODE_AP_REQ */
838 static krb5_error_code
839 get_key_from_keytab(krb5_context context
,
840 krb5_const_principal server
,
841 krb5_enctype enctype
,
843 krb5_keyblock
**out_key
)
845 krb5_keytab_entry entry
;
851 /* We have to open a new keytab handle here, as MIT does
852 an implicit open/getnext/close on krb5_kt_get_entry. We
853 may be in the middle of a keytab enumeration when this is
856 ret
= smb_krb5_open_keytab(context
, NULL
, False
, &keytab
);
858 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret
)));
862 if ( DEBUGLEVEL
>= 10 ) {
863 if (smb_krb5_unparse_name(talloc_tos(), context
, server
, &name
) == 0) {
864 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
865 kvno
, enctype
, name
));
870 ret
= krb5_kt_get_entry(context
,
878 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret
)));
882 keyp
= KRB5_KT_KEY(&entry
);
884 ret
= krb5_copy_keyblock(context
, keyp
, out_key
);
886 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret
)));
890 smb_krb5_kt_free_entry(context
, &entry
);
893 krb5_kt_close(context
, keytab
);
899 krb5_error_code
smb_krb5_get_keyinfo_from_ap_req(krb5_context context
,
900 const krb5_data
*inbuf
,
902 krb5_enctype
*enctype
)
904 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
909 ret
= krb5_decode_ap_req(context
, inbuf
, &ap_req
);
913 *kvno
= get_kvno_from_ap_req(&ap_req
);
914 *enctype
= get_enctype_from_ap_req(&ap_req
);
916 free_AP_REQ(&ap_req
);
921 /* Possibly not an appropriate error code. */
922 return KRB5KDC_ERR_BADOPTION
;
925 krb5_error_code
krb5_rd_req_return_keyblock_from_keytab(krb5_context context
,
926 krb5_auth_context
*auth_context
,
927 const krb5_data
*inbuf
,
928 krb5_const_principal server
,
930 krb5_flags
*ap_req_options
,
931 krb5_ticket
**ticket
,
932 krb5_keyblock
**keyblock
)
936 krb5_enctype enctype
;
937 krb5_keyblock
*local_keyblock
;
939 ret
= krb5_rd_req(context
,
950 #ifdef KRB5_TICKET_HAS_KEYINFO
951 enctype
= (*ticket
)->enc_part
.enctype
;
952 kvno
= (*ticket
)->enc_part
.kvno
;
954 ret
= smb_krb5_get_keyinfo_from_ap_req(context
, inbuf
, &kvno
, &enctype
);
960 ret
= get_key_from_keytab(context
,
966 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
971 if (ret
&& local_keyblock
!= NULL
) {
972 krb5_free_keyblock(context
, local_keyblock
);
974 *keyblock
= local_keyblock
;
980 krb5_error_code
smb_krb5_renew_ticket(const char *ccache_string
, /* FILE:/tmp/krb5cc_0 */
981 const char *client_string
, /* gd@BER.SUSE.DE */
982 const char *service_string
, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
986 krb5_context context
= NULL
;
987 krb5_ccache ccache
= NULL
;
988 krb5_principal client
= NULL
;
989 krb5_creds creds
, creds_in
, *creds_out
= NULL
;
992 ZERO_STRUCT(creds_in
);
994 initialize_krb5_error_table();
995 ret
= krb5_init_context(&context
);
1000 if (!ccache_string
) {
1001 ccache_string
= krb5_cc_default_name(context
);
1004 if (!ccache_string
) {
1009 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string
));
1011 /* FIXME: we should not fall back to defaults */
1012 ret
= krb5_cc_resolve(context
, discard_const_p(char, ccache_string
), &ccache
);
1017 if (client_string
) {
1018 ret
= smb_krb5_parse_name(context
, client_string
, &client
);
1023 ret
= krb5_cc_get_principal(context
, ccache
, &client
);
1029 ret
= krb5_get_renewed_creds(context
, &creds
, client
, ccache
, discard_const_p(char, service_string
));
1031 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1035 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1036 ret
= krb5_cc_initialize(context
, ccache
, client
);
1041 ret
= krb5_cc_store_cred(context
, ccache
, &creds
);
1044 *expire_time
= (time_t) creds
.times
.endtime
;
1048 krb5_free_cred_contents(context
, &creds_in
);
1051 krb5_free_creds(context
, creds_out
);
1053 krb5_free_cred_contents(context
, &creds
);
1057 krb5_free_principal(context
, client
);
1060 krb5_cc_close(context
, ccache
);
1063 krb5_free_context(context
);
1069 krb5_error_code
smb_krb5_free_addresses(krb5_context context
, smb_krb5_addresses
*addr
)
1071 krb5_error_code ret
= 0;
1075 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1076 krb5_free_addresses(context
, addr
->addrs
);
1077 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1078 ret
= krb5_free_addresses(context
, addr
->addrs
);
1079 SAFE_FREE(addr
->addrs
);
1086 krb5_error_code
smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses
**kerb_addr
)
1088 krb5_error_code ret
= 0;
1090 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1091 krb5_address
**addrs
= NULL
;
1092 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1093 krb5_addresses
*addrs
= NULL
;
1096 *kerb_addr
= (smb_krb5_addresses
*)SMB_MALLOC(sizeof(smb_krb5_addresses
));
1097 if (*kerb_addr
== NULL
) {
1101 put_name(buf
, lp_netbios_name(), ' ', 0x20);
1103 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1107 addrs
= (krb5_address
**)SMB_MALLOC(sizeof(krb5_address
*) * num_addr
);
1108 if (addrs
== NULL
) {
1109 SAFE_FREE(*kerb_addr
);
1113 memset(addrs
, 0, sizeof(krb5_address
*) * num_addr
);
1115 addrs
[0] = (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1116 if (addrs
[0] == NULL
) {
1118 SAFE_FREE(*kerb_addr
);
1122 addrs
[0]->magic
= KV5M_ADDRESS
;
1123 addrs
[0]->addrtype
= KRB5_ADDR_NETBIOS
;
1124 addrs
[0]->length
= MAX_NETBIOSNAME_LEN
;
1125 addrs
[0]->contents
= (unsigned char *)SMB_MALLOC(addrs
[0]->length
);
1126 if (addrs
[0]->contents
== NULL
) {
1127 SAFE_FREE(addrs
[0]);
1129 SAFE_FREE(*kerb_addr
);
1133 memcpy(addrs
[0]->contents
, buf
, addrs
[0]->length
);
1137 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1139 addrs
= (krb5_addresses
*)SMB_MALLOC(sizeof(krb5_addresses
));
1140 if (addrs
== NULL
) {
1141 SAFE_FREE(*kerb_addr
);
1145 memset(addrs
, 0, sizeof(krb5_addresses
));
1148 addrs
->val
= (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1149 if (addrs
->val
== NULL
) {
1151 SAFE_FREE(kerb_addr
);
1155 addrs
->val
[0].addr_type
= KRB5_ADDR_NETBIOS
;
1156 addrs
->val
[0].address
.length
= MAX_NETBIOSNAME_LEN
;
1157 addrs
->val
[0].address
.data
= (unsigned char *)SMB_MALLOC(addrs
->val
[0].address
.length
);
1158 if (addrs
->val
[0].address
.data
== NULL
) {
1159 SAFE_FREE(addrs
->val
);
1161 SAFE_FREE(*kerb_addr
);
1165 memcpy(addrs
->val
[0].address
.data
, buf
, addrs
->val
[0].address
.length
);
1168 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1170 (*kerb_addr
)->addrs
= addrs
;
1175 void smb_krb5_free_error(krb5_context context
, krb5_error
*krberror
)
1177 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1178 krb5_free_error_contents(context
, krberror
);
1180 krb5_free_error(context
, krberror
);
1184 krb5_error_code
handle_krberror_packet(krb5_context context
,
1187 krb5_error_code ret
;
1188 bool got_error_code
= False
;
1190 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1192 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1194 krb5_error krberror
;
1196 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1197 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1198 error_message(ret
)));
1202 if (krberror
.e_data
== NULL
|| krberror
.e_data
->data
== NULL
) {
1203 ret
= (krb5_error_code
) krberror
.error_code
;
1204 got_error_code
= True
;
1207 smb_krb5_free_error(context
, &krberror
);
1211 krb5_error
*krberror
;
1213 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1214 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1215 error_message(ret
)));
1219 if (krberror
->e_data
.data
== NULL
) {
1220 #if defined(ERROR_TABLE_BASE_krb5)
1221 ret
= ERROR_TABLE_BASE_krb5
+ (krb5_error_code
) krberror
->error
;
1223 ret
= (krb5_error_code
)krberror
->error
;
1225 got_error_code
= True
;
1227 smb_krb5_free_error(context
, krberror
);
1230 if (got_error_code
) {
1231 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1232 error_message(ret
), ret
));
1237 krb5_error_code
smb_krb5_get_init_creds_opt_alloc(krb5_context context
,
1238 krb5_get_init_creds_opt
**opt
)
1240 /* Heimdal or modern MIT version */
1241 return krb5_get_init_creds_opt_alloc(context
, opt
);
1244 void smb_krb5_get_init_creds_opt_free(krb5_context context
,
1245 krb5_get_init_creds_opt
*opt
)
1247 /* Modern MIT or Heimdal version */
1248 krb5_get_init_creds_opt_free(context
, opt
);
1251 krb5_enctype
smb_get_enctype_from_kt_entry(krb5_keytab_entry
*kt_entry
)
1253 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry
));
1257 /* caller needs to free etype_s */
1258 krb5_error_code
smb_krb5_enctype_to_string(krb5_context context
,
1259 krb5_enctype enctype
,
1262 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1263 return krb5_enctype_to_string(context
, enctype
, etype_s
); /* Heimdal */
1264 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1266 krb5_error_code ret
= krb5_enctype_to_string(enctype
, buf
, 256); /* MIT */
1270 *etype_s
= SMB_STRDUP(buf
);
1276 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1280 /**********************************************************************
1281 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1282 * allows to process non-default keytab names.
1283 * @param context krb5_context
1284 * @param keytab_name_req string
1285 * @param write_access bool if writable keytab is required
1286 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1287 * @return krb5_error_code
1288 **********************************************************************/
1290 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1291 #ifndef MAX_KEYTAB_NAME_LEN
1292 #define MAX_KEYTAB_NAME_LEN 1100
1295 krb5_error_code
smb_krb5_open_keytab(krb5_context context
,
1296 const char *keytab_name_req
,
1298 krb5_keytab
*keytab
)
1300 krb5_error_code ret
= 0;
1301 TALLOC_CTX
*mem_ctx
;
1302 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1303 char *kt_str
= NULL
;
1304 bool found_valid_name
= False
;
1305 const char *pragma
= "FILE";
1306 const char *tmp
= NULL
;
1308 if (!write_access
&& !keytab_name_req
) {
1309 /* caller just wants to read the default keytab readonly, so be it */
1310 return krb5_kt_default(context
, keytab
);
1313 mem_ctx
= talloc_init("smb_krb5_open_keytab");
1318 #ifdef HAVE_WRFILE_KEYTAB
1324 if (keytab_name_req
) {
1326 if (strlen(keytab_name_req
) > MAX_KEYTAB_NAME_LEN
) {
1327 ret
= KRB5_CONFIG_NOTENUFSPACE
;
1331 if ((strncmp(keytab_name_req
, "WRFILE:/", 8) == 0) ||
1332 (strncmp(keytab_name_req
, "FILE:/", 6) == 0)) {
1333 tmp
= keytab_name_req
;
1337 if (keytab_name_req
[0] != '/') {
1338 ret
= KRB5_KT_BADNAME
;
1342 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, keytab_name_req
);
1351 /* we need to handle more complex keytab_strings, like:
1352 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1354 ret
= krb5_kt_default_name(context
, &keytab_string
[0], MAX_KEYTAB_NAME_LEN
- 2);
1359 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string
));
1361 tmp
= talloc_strdup(mem_ctx
, keytab_string
);
1367 if (strncmp(tmp
, "ANY:", 4) == 0) {
1371 memset(&keytab_string
, '\0', sizeof(keytab_string
));
1373 while (next_token_talloc(mem_ctx
, &tmp
, &kt_str
, ",")) {
1374 if (strncmp(kt_str
, "WRFILE:", 7) == 0) {
1375 found_valid_name
= True
;
1380 if (strncmp(kt_str
, "FILE:", 5) == 0) {
1381 found_valid_name
= True
;
1386 if (tmp
[0] == '/') {
1387 /* Treat as a FILE: keytab definition. */
1388 found_valid_name
= true;
1391 if (found_valid_name
) {
1392 if (tmp
[0] != '/') {
1393 ret
= KRB5_KT_BADNAME
;
1397 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, tmp
);
1406 if (!found_valid_name
) {
1407 ret
= KRB5_KT_UNKNOWN_TYPE
;
1412 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp
));
1413 ret
= krb5_kt_resolve(context
, tmp
, keytab
);
1416 TALLOC_FREE(mem_ctx
);
1420 krb5_error_code
smb_krb5_keytab_name(TALLOC_CTX
*mem_ctx
,
1421 krb5_context context
,
1423 const char **keytab_name
)
1425 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1426 krb5_error_code ret
= 0;
1428 ret
= krb5_kt_get_name(context
, keytab
,
1429 keytab_string
, MAX_KEYTAB_NAME_LEN
- 2);
1434 *keytab_name
= talloc_strdup(mem_ctx
, keytab_string
);
1435 if (!*keytab_name
) {
1442 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1443 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1444 defined(HAVE_KRB5_GET_CREDS)
1445 static krb5_error_code
smb_krb5_get_credentials_for_user_opt(krb5_context context
,
1448 krb5_principal server
,
1449 krb5_principal impersonate_princ
,
1450 krb5_creds
**out_creds
)
1452 krb5_error_code ret
;
1453 krb5_get_creds_opt opt
;
1455 ret
= krb5_get_creds_opt_alloc(context
, &opt
);
1459 krb5_get_creds_opt_add_options(context
, opt
, KRB5_GC_FORWARDABLE
);
1461 if (impersonate_princ
) {
1462 ret
= krb5_get_creds_opt_set_impersonate(context
, opt
,
1469 ret
= krb5_get_creds(context
, opt
, ccache
, server
, out_creds
);
1476 krb5_get_creds_opt_free(context
, opt
);
1480 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
1482 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
1483 static krb5_error_code
smb_krb5_get_credentials_for_user(krb5_context context
,
1486 krb5_principal server
,
1487 krb5_principal impersonate_princ
,
1488 krb5_creds
**out_creds
)
1490 krb5_error_code ret
;
1491 krb5_creds in_creds
;
1493 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1494 krb5_error_code KRB5_CALLCONV
1495 krb5_get_credentials_for_user(krb5_context context
, krb5_flags options
,
1496 krb5_ccache ccache
, krb5_creds
*in_creds
,
1497 krb5_data
*subject_cert
,
1498 krb5_creds
**out_creds
);
1499 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1501 ZERO_STRUCT(in_creds
);
1503 if (impersonate_princ
) {
1505 in_creds
.server
= me
;
1506 in_creds
.client
= impersonate_princ
;
1508 ret
= krb5_get_credentials_for_user(context
,
1509 0, /* krb5_flags options */
1512 NULL
, /* krb5_data *subject_cert */
1515 in_creds
.client
= me
;
1516 in_creds
.server
= server
;
1518 ret
= krb5_get_credentials(context
, 0, ccache
,
1519 &in_creds
, out_creds
);
1524 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1527 * smb_krb5_get_credentials
1529 * @brief Get krb5 credentials for a server
1531 * @param[in] context An initialized krb5_context
1532 * @param[in] ccache An initialized krb5_ccache
1533 * @param[in] me The krb5_principal of the caller
1534 * @param[in] server The krb5_principal of the requested service
1535 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1536 * @param[out] out_creds The returned krb5_creds structure
1537 * @return krb5_error_code
1540 krb5_error_code
smb_krb5_get_credentials(krb5_context context
,
1543 krb5_principal server
,
1544 krb5_principal impersonate_princ
,
1545 krb5_creds
**out_creds
)
1547 krb5_error_code ret
;
1548 krb5_creds
*creds
= NULL
;
1552 if (impersonate_princ
) {
1553 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
1554 ret
= smb_krb5_get_credentials_for_user_opt(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
1555 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
1556 ret
= smb_krb5_get_credentials_for_user(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
1561 krb5_creds in_creds
;
1563 ZERO_STRUCT(in_creds
);
1565 in_creds
.client
= me
;
1566 in_creds
.server
= server
;
1568 ret
= krb5_get_credentials(context
, 0, ccache
,
1581 krb5_free_creds(context
, creds
);
1588 * smb_krb5_get_creds
1590 * @brief Get krb5 credentials for a server
1592 * @param[in] server_s The string name of the service
1593 * @param[in] time_offset The offset to the KDCs time in seconds (optional)
1594 * @param[in] cc The krb5 credential cache string name (optional)
1595 * @param[in] impersonate_princ_s The string principal name to impersonate (optional)
1596 * @param[out] creds_p The returned krb5_creds structure
1597 * @return krb5_error_code
1600 krb5_error_code
smb_krb5_get_creds(const char *server_s
,
1603 const char *impersonate_princ_s
,
1604 krb5_creds
**creds_p
)
1606 krb5_error_code ret
;
1607 krb5_context context
= NULL
;
1608 krb5_principal me
= NULL
;
1609 krb5_principal server
= NULL
;
1610 krb5_principal impersonate_princ
= NULL
;
1611 krb5_creds
*creds
= NULL
;
1612 krb5_ccache ccache
= NULL
;
1616 initialize_krb5_error_table();
1617 ret
= krb5_init_context(&context
);
1622 if (time_offset
!= 0) {
1623 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
1626 ret
= krb5_cc_resolve(context
, cc
? cc
:
1627 krb5_cc_default_name(context
), &ccache
);
1632 ret
= krb5_cc_get_principal(context
, ccache
, &me
);
1637 ret
= smb_krb5_parse_name(context
, server_s
, &server
);
1642 if (impersonate_princ_s
) {
1643 ret
= smb_krb5_parse_name(context
, impersonate_princ_s
,
1644 &impersonate_princ
);
1650 ret
= smb_krb5_get_credentials(context
, ccache
,
1651 me
, server
, impersonate_princ
,
1657 ret
= krb5_cc_store_cred(context
, ccache
, creds
);
1666 DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n",
1669 if (impersonate_princ_s
) {
1670 char *client
= NULL
;
1672 ret
= smb_krb5_unparse_name(talloc_tos(), context
, creds
->client
, &client
);
1676 DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n",
1678 TALLOC_FREE(client
);
1687 krb5_free_creds(context
, creds
);
1690 krb5_free_principal(context
, server
);
1693 krb5_free_principal(context
, me
);
1695 if (impersonate_princ
) {
1696 krb5_free_principal(context
, impersonate_princ
);
1699 krb5_cc_close(context
, ccache
);
1701 krb5_free_context(context
);
1707 * smb_krb5_principal_get_realm
1709 * @brief Get realm of a principal
1711 * @param[in] context The krb5_context
1712 * @param[in] principal The principal
1713 * @return pointer to the realm
1717 char *smb_krb5_principal_get_realm(krb5_context context
,
1718 krb5_principal principal
)
1720 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
1721 return discard_const_p(char, krb5_principal_get_realm(context
, principal
));
1722 #elif defined(krb5_princ_realm) /* MIT */
1724 realm
= krb5_princ_realm(context
, principal
);
1725 return discard_const_p(char, realm
->data
);
1731 #else /* HAVE_KRB5 */
1732 /* this saves a few linking headaches */
1733 int cli_krb5_get_ticket(TALLOC_CTX
*mem_ctx
,
1734 const char *principal
, time_t time_offset
,
1735 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
1736 uint32_t extra_ap_opts
,
1737 const char *ccname
, time_t *tgs_expire
,
1738 const char *impersonate_princ_s
)
1740 DEBUG(0,("NO KERBEROS SUPPORT\n"));
1744 bool unwrap_pac(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, DATA_BLOB
*unwrapped_pac_data
)
1746 DEBUG(0,("NO KERBEROS SUPPORT\n"));
1750 #endif /* HAVE_KRB5 */