s3:torture/test_smb2: check directory listing during reauth
[Samba.git] / source3 / libsmb / clikrb5.c
blob304972fce40766ef32384d95954de90267c56015
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-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/>.
23 #include "includes.h"
24 #include "smb_krb5.h"
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
31 #endif
33 #ifndef KRB5_AUTHDATA_IF_RELEVANT
34 #define KRB5_AUTHDATA_IF_RELEVANT 1
35 #endif
37 #ifdef HAVE_KRB5
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(
48 krb5_context context,
49 krb5_auth_context auth_context,
50 krb5_cksumtype cksumtype);
51 #endif
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
63 * internal symbol
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)
72 /* Heimdal */
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)
83 /* HEIMDAL */
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);
92 return true;
94 #endif
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);
99 return true;
101 return false;
103 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
104 /* MIT */
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);
113 return true;
115 #endif
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);
120 return true;
122 return false;
124 #else
125 #error UNKNOWN_ADDRTYPE
126 #endif
128 int create_kerberos_key_from_string(krb5_context context,
129 krb5_principal host_princ,
130 krb5_data *password,
131 krb5_keyblock *key,
132 krb5_enctype enctype,
133 bool no_salt)
135 krb5_principal salt_princ = NULL;
136 int ret;
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
140 * its behavior.
142 if (no_salt) {
143 KRB5_KEY_DATA(key) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
144 if (!KRB5_KEY_DATA(key)) {
145 return ENOMEM;
147 memcpy(KRB5_KEY_DATA(key), password->data, password->length);
148 KRB5_KEY_LENGTH(key) = password->length;
149 KRB5_KEY_TYPE(key) = enctype;
150 return 0;
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);
154 if (salt_princ) {
155 krb5_free_principal(context, salt_princ);
157 return ret;
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);
172 #else
173 return krb5_get_default_in_tkt_etypes(context, enctypes);
174 #endif
176 #else
177 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
178 #endif
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);
187 #endif
189 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
190 DATA_BLOB *edata,
191 DATA_BLOB *edata_out)
193 DATA_BLOB edata_contents;
194 ASN1_DATA *data;
195 int edata_type;
197 if (!edata->length) {
198 return False;
201 data = asn1_init(mem_ctx);
202 if (data == NULL) {
203 return false;
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));
214 asn1_free(data);
215 return False;
218 asn1_start_tag(data, ASN1_CONTEXT(2));
219 asn1_read_OctetString(data, talloc_tos(), &edata_contents);
220 asn1_end_tag(data);
221 asn1_end_tag(data);
222 asn1_end_tag(data);
223 asn1_free(data);
225 *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length);
227 data_blob_free(&edata_contents);
229 return True;
233 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
235 DATA_BLOB pac_contents;
236 ASN1_DATA *data;
237 int data_type;
239 if (!auth_data->length) {
240 return False;
243 data = asn1_init(mem_ctx);
244 if (data == NULL) {
245 return false;
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));
256 asn1_free(data);
257 return False;
260 asn1_end_tag(data);
261 asn1_start_tag(data, ASN1_CONTEXT(1));
262 asn1_read_OctetString(data, talloc_tos(), &pac_contents);
263 asn1_end_tag(data);
264 asn1_end_tag(data);
265 asn1_end_tag(data);
266 asn1_free(data);
268 *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length);
270 data_blob_free(&pac_contents);
272 return True;
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;
279 int i;
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));
292 continue;
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) {
303 return true;
307 return got_auth_data_pac;
310 #else
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));
320 continue;
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) {
331 return true;
335 return got_auth_data_pac;
337 #endif
338 return False;
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;
345 #else
346 return tkt->client;
347 #endif
350 static bool ads_cleanup_expired_creds(krb5_context context,
351 krb5_ccache ccache,
352 krb5_creds *credsp)
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))
365 return False;
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));
374 return False;
377 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
378 if (retval) {
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 */
384 return True;
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 );
395 if (retval) {
396 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
397 error_message(retval)));
398 return retval;
401 /* Ensure this is an addressless ticket. */
402 retval = krb5_auth_con_setaddrs(context, *auth_context, NULL, NULL);
403 if (retval) {
404 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
405 error_message(retval)));
408 return 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] */
413 uint32_t gss_flags)
415 unsigned int orig_length = in_data->length;
416 unsigned int base_cksum_size = GSSAPI_CHECKSUM_SIZE;
417 char *gss_cksum = NULL;
419 if (orig_length) {
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) {
426 return EINVAL;
429 gss_cksum = (char *)SMB_MALLOC(base_cksum_size + orig_length);
430 if (gss_cksum == NULL) {
431 return ENOMEM;
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
442 * SMB servers.
444 * See bug #7883
446 memset(&gss_cksum[4], 0x00, GSSAPI_BNDLENGTH);
448 SIVAL(gss_cksum, 20, gss_flags);
450 if (orig_length) {
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);
455 free(in_data->data);
456 in_data->data = NULL;
457 in_data->length = 0;
459 in_data->data = gss_cksum;
460 in_data->length = base_cksum_size + orig_length;
461 return 0;
463 #endif
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,
472 krb5_ccache ccache,
473 krb5_data *outbuf,
474 time_t *expire_time,
475 const char *impersonate_princ_s)
477 krb5_error_code retval;
478 krb5_principal server;
479 krb5_principal impersonate_princ = NULL;
480 krb5_creds * credsp;
481 krb5_creds creds;
482 krb5_data in_data;
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);
489 if (retval) {
490 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
491 return retval;
494 if (impersonate_princ_s) {
495 retval = smb_krb5_parse_name(context, impersonate_princ_s,
496 &impersonate_princ);
497 if (retval) {
498 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s));
499 goto cleanup_princ;
503 /* obtain ticket & session key */
504 ZERO_STRUCT(creds);
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)));
508 goto cleanup_princ;
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)));
516 goto cleanup_creds;
519 while (!creds_ready && (i < maxtries)) {
521 if ((retval = smb_krb5_get_credentials(context, ccache,
522 creds.client,
523 creds.server,
524 impersonate_princ,
525 &credsp))) {
526 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
527 principal, error_message(retval)));
528 goto cleanup_creds;
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)) {
540 creds_ready = True;
543 i++;
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));
551 if (expire_time) {
552 *expire_time = (time_t)credsp->times.endtime;
555 /* Allocate the auth_context. */
556 retval = setup_auth_context(context, auth_context);
557 if (retval) {
558 DEBUG(1,("setup_auth_context failed (%s)\n",
559 error_message(retval)));
560 goto cleanup_creds;
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,
574 *auth_context,
575 &credsp->keyblock );
576 if (retval) {
577 DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
578 error_message(retval)));
579 goto cleanup_creds;
582 /* Must use a subkey for forwarded tickets. */
583 retval = krb5_auth_con_setflags(context,
584 *auth_context,
585 KRB5_AUTH_CONTEXT_USE_SUBKEY);
586 if (retval) {
587 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
588 error_message(retval)));
589 goto cleanup_creds;
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] */
601 if (retval) {
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.
610 if (in_data.data) {
611 free( in_data.data );
612 in_data.data = NULL;
613 in_data.length = 0;
615 krb5_auth_con_free(context, *auth_context);
616 *auth_context = NULL;
617 retval = setup_auth_context(context, auth_context);
618 if (retval) {
619 DEBUG(1,("setup_auth_context failed (%s)\n",
620 error_message(retval)));
621 goto cleanup_creds;
623 } else {
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);
631 if (retval) {
632 goto cleanup_data;
635 /* We always want GSS-checksum types. */
636 retval = krb5_auth_con_set_req_cksumtype(context, *auth_context, GSSAPI_CHECKSUM );
637 if (retval) {
638 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
639 error_message(retval)));
640 goto cleanup_data;
643 #endif
645 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
646 &in_data, credsp, outbuf);
647 if (retval) {
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)
653 cleanup_data:
654 #endif
656 if (in_data.data) {
657 free( in_data.data );
658 in_data.length = 0;
661 krb5_free_creds(context, credsp);
663 cleanup_creds:
664 krb5_free_cred_contents(context, &creds);
666 cleanup_princ:
667 krb5_free_principal(context, server);
668 if (impersonate_princ) {
669 krb5_free_principal(context, impersonate_princ);
672 return retval;
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,
682 time_t *tgs_expire,
683 const char *impersonate_princ_s)
686 krb5_error_code retval;
687 krb5_data packet;
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,
693 ENCTYPE_DES_CBC_MD5,
694 ENCTYPE_DES_CBC_CRC,
695 ENCTYPE_NULL};
697 initialize_krb5_error_table();
698 retval = krb5_init_context(&context);
699 if (retval) {
700 DEBUG(1, ("krb5_init_context failed (%s)\n",
701 error_message(retval)));
702 goto failed;
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)));
713 goto failed;
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)));
719 goto failed;
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);
726 if (retval) {
727 goto failed;
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);
737 failed:
739 if (context) {
740 if (ccdef)
741 krb5_cc_close(context, ccdef);
742 if (auth_context)
743 krb5_auth_con_free(context, auth_context);
744 krb5_free_context(context);
747 return retval;
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;
757 bool ret = false;
759 if (remote) {
760 err = krb5_auth_con_getremotesubkey(context,
761 auth_context, &skey);
762 } else {
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));
769 goto done;
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,
776 KRB5_KEY_DATA(skey),
777 KRB5_KEY_LENGTH(skey));
778 dump_data_pw("KRB5 Session Key:\n",
779 session_key->data,
780 session_key->length);
782 ret = true;
784 done:
785 if (skey) {
786 krb5_free_keyblock(context, skey);
789 return ret;
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);
802 return &kdata;
804 #endif
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;
810 #else
811 return tkt->ticket.authtime;
812 #endif
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;
821 #else /* Heimdal */
822 if (ap_req->ticket.enc_part.kvno)
823 return *ap_req->ticket.enc_part.kvno;
824 #endif
825 return 0;
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;
832 #else /* MIT */
833 return ap_req->ticket->enc_part.enctype;
834 #endif
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,
842 krb5_kvno kvno,
843 krb5_keyblock **out_key)
845 krb5_keytab_entry entry;
846 krb5_error_code ret;
847 krb5_keytab keytab;
848 char *name = NULL;
849 krb5_keyblock *keyp;
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
854 called. JRA. */
856 ret = smb_krb5_open_keytab(context, NULL, False, &keytab);
857 if (ret) {
858 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
859 return 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));
866 TALLOC_FREE(name);
870 ret = krb5_kt_get_entry(context,
871 keytab,
872 server,
873 kvno,
874 enctype,
875 &entry);
877 if (ret) {
878 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret)));
879 goto out;
882 keyp = KRB5_KT_KEY(&entry);
884 ret = krb5_copy_keyblock(context, keyp, out_key);
885 if (ret) {
886 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret)));
887 goto out;
890 smb_krb5_kt_free_entry(context, &entry);
892 out:
893 krb5_kt_close(context, keytab);
894 return ret;
897 /* Prototypes */
899 krb5_error_code smb_krb5_get_keyinfo_from_ap_req(krb5_context context,
900 const krb5_data *inbuf,
901 krb5_kvno *kvno,
902 krb5_enctype *enctype)
904 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
906 krb5_error_code ret;
907 krb5_ap_req ap_req;
909 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
910 if (ret)
911 return ret;
913 *kvno = get_kvno_from_ap_req(&ap_req);
914 *enctype = get_enctype_from_ap_req(&ap_req);
916 free_AP_REQ(&ap_req);
917 return 0;
919 #endif
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,
929 krb5_keytab keytab,
930 krb5_flags *ap_req_options,
931 krb5_ticket **ticket,
932 krb5_keyblock **keyblock)
934 krb5_error_code ret;
935 krb5_kvno kvno;
936 krb5_enctype enctype;
937 krb5_keyblock *local_keyblock;
939 ret = krb5_rd_req(context,
940 auth_context,
941 inbuf,
942 server,
943 keytab,
944 ap_req_options,
945 ticket);
946 if (ret) {
947 return ret;
950 #ifdef KRB5_TICKET_HAS_KEYINFO
951 enctype = (*ticket)->enc_part.enctype;
952 kvno = (*ticket)->enc_part.kvno;
953 #else
954 ret = smb_krb5_get_keyinfo_from_ap_req(context, inbuf, &kvno, &enctype);
955 if (ret) {
956 return ret;
958 #endif
960 ret = get_key_from_keytab(context,
961 server,
962 enctype,
963 kvno,
964 &local_keyblock);
965 if (ret) {
966 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
967 goto out;
970 out:
971 if (ret && local_keyblock != NULL) {
972 krb5_free_keyblock(context, local_keyblock);
973 } else {
974 *keyblock = local_keyblock;
977 return ret;
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 */
983 time_t *expire_time)
985 krb5_error_code ret;
986 krb5_context context = NULL;
987 krb5_ccache ccache = NULL;
988 krb5_principal client = NULL;
989 krb5_creds creds, creds_in, *creds_out = NULL;
991 ZERO_STRUCT(creds);
992 ZERO_STRUCT(creds_in);
994 initialize_krb5_error_table();
995 ret = krb5_init_context(&context);
996 if (ret) {
997 goto done;
1000 if (!ccache_string) {
1001 ccache_string = krb5_cc_default_name(context);
1004 if (!ccache_string) {
1005 ret = EINVAL;
1006 goto done;
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);
1013 if (ret) {
1014 goto done;
1017 if (client_string) {
1018 ret = smb_krb5_parse_name(context, client_string, &client);
1019 if (ret) {
1020 goto done;
1022 } else {
1023 ret = krb5_cc_get_principal(context, ccache, &client);
1024 if (ret) {
1025 goto done;
1029 ret = krb5_get_renewed_creds(context, &creds, client, ccache, discard_const_p(char, service_string));
1030 if (ret) {
1031 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1032 goto done;
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);
1037 if (ret) {
1038 goto done;
1041 ret = krb5_cc_store_cred(context, ccache, &creds);
1043 if (expire_time) {
1044 *expire_time = (time_t) creds.times.endtime;
1047 done:
1048 krb5_free_cred_contents(context, &creds_in);
1050 if (creds_out) {
1051 krb5_free_creds(context, creds_out);
1052 } else {
1053 krb5_free_cred_contents(context, &creds);
1056 if (client) {
1057 krb5_free_principal(context, client);
1059 if (ccache) {
1060 krb5_cc_close(context, ccache);
1062 if (context) {
1063 krb5_free_context(context);
1066 return ret;
1069 krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr)
1071 krb5_error_code ret = 0;
1072 if (addr == NULL) {
1073 return ret;
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);
1080 #endif
1081 SAFE_FREE(addr);
1082 addr = NULL;
1083 return ret;
1086 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr)
1088 krb5_error_code ret = 0;
1089 nstring buf;
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;
1094 #endif
1096 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
1097 if (*kerb_addr == NULL) {
1098 return ENOMEM;
1101 put_name(buf, lp_netbios_name(), ' ', 0x20);
1103 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1105 int num_addr = 2;
1107 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
1108 if (addrs == NULL) {
1109 SAFE_FREE(*kerb_addr);
1110 return ENOMEM;
1113 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
1115 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1116 if (addrs[0] == NULL) {
1117 SAFE_FREE(addrs);
1118 SAFE_FREE(*kerb_addr);
1119 return ENOMEM;
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]);
1128 SAFE_FREE(addrs);
1129 SAFE_FREE(*kerb_addr);
1130 return ENOMEM;
1133 memcpy(addrs[0]->contents, buf, addrs[0]->length);
1135 addrs[1] = NULL;
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);
1142 return ENOMEM;
1145 memset(addrs, 0, sizeof(krb5_addresses));
1147 addrs->len = 1;
1148 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1149 if (addrs->val == NULL) {
1150 SAFE_FREE(addrs);
1151 SAFE_FREE(kerb_addr);
1152 return ENOMEM;
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);
1160 SAFE_FREE(addrs);
1161 SAFE_FREE(*kerb_addr);
1162 return ENOMEM;
1165 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
1167 #else
1168 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1169 #endif
1170 (*kerb_addr)->addrs = addrs;
1172 return ret;
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);
1179 #else /* MIT */
1180 krb5_free_error(context, krberror);
1181 #endif
1184 krb5_error_code handle_krberror_packet(krb5_context context,
1185 krb5_data *packet)
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)));
1199 return 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);
1209 #else /* MIT */
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)));
1216 return 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;
1222 #else
1223 ret = (krb5_error_code)krberror->error;
1224 #endif
1225 got_error_code = True;
1227 smb_krb5_free_error(context, krberror);
1229 #endif
1230 if (got_error_code) {
1231 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1232 error_message(ret), ret));
1234 return 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,
1260 char **etype_s)
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)
1265 char buf[256];
1266 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
1267 if (ret) {
1268 return ret;
1270 *etype_s = SMB_STRDUP(buf);
1271 if (!*etype_s) {
1272 return ENOMEM;
1274 return ret;
1275 #else
1276 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1277 #endif
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
1293 #endif
1295 krb5_error_code smb_krb5_open_keytab(krb5_context context,
1296 const char *keytab_name_req,
1297 bool write_access,
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");
1314 if (!mem_ctx) {
1315 return ENOMEM;
1318 #ifdef HAVE_WRFILE_KEYTAB
1319 if (write_access) {
1320 pragma = "WRFILE";
1322 #endif
1324 if (keytab_name_req) {
1326 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1327 ret = KRB5_CONFIG_NOTENUFSPACE;
1328 goto out;
1331 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
1332 (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
1333 tmp = keytab_name_req;
1334 goto resolve;
1337 if (keytab_name_req[0] != '/') {
1338 ret = KRB5_KT_BADNAME;
1339 goto out;
1342 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1343 if (!tmp) {
1344 ret = ENOMEM;
1345 goto out;
1348 goto resolve;
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);
1355 if (ret) {
1356 goto out;
1359 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1361 tmp = talloc_strdup(mem_ctx, keytab_string);
1362 if (!tmp) {
1363 ret = ENOMEM;
1364 goto out;
1367 if (strncmp(tmp, "ANY:", 4) == 0) {
1368 tmp += 4;
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;
1376 tmp = kt_str;
1377 tmp += 7;
1380 if (strncmp(kt_str, "FILE:", 5) == 0) {
1381 found_valid_name = True;
1382 tmp = kt_str;
1383 tmp += 5;
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;
1394 goto out;
1397 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1398 if (!tmp) {
1399 ret = ENOMEM;
1400 goto out;
1402 break;
1406 if (!found_valid_name) {
1407 ret = KRB5_KT_UNKNOWN_TYPE;
1408 goto out;
1411 resolve:
1412 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1413 ret = krb5_kt_resolve(context, tmp, keytab);
1415 out:
1416 TALLOC_FREE(mem_ctx);
1417 return ret;
1420 krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
1421 krb5_context context,
1422 krb5_keytab keytab,
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);
1430 if (ret) {
1431 return ret;
1434 *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1435 if (!*keytab_name) {
1436 return ENOMEM;
1439 return ret;
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,
1446 krb5_ccache ccache,
1447 krb5_principal me,
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);
1456 if (ret) {
1457 goto done;
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,
1463 impersonate_princ);
1464 if (ret) {
1465 goto done;
1469 ret = krb5_get_creds(context, opt, ccache, server, out_creds);
1470 if (ret) {
1471 goto done;
1474 done:
1475 if (opt) {
1476 krb5_get_creds_opt_free(context, opt);
1478 return ret;
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,
1484 krb5_ccache ccache,
1485 krb5_principal me,
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 */
1510 ccache,
1511 &in_creds,
1512 NULL, /* krb5_data *subject_cert */
1513 out_creds);
1514 } else {
1515 in_creds.client = me;
1516 in_creds.server = server;
1518 ret = krb5_get_credentials(context, 0, ccache,
1519 &in_creds, out_creds);
1522 return ret;
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,
1541 krb5_ccache ccache,
1542 krb5_principal me,
1543 krb5_principal server,
1544 krb5_principal impersonate_princ,
1545 krb5_creds **out_creds)
1547 krb5_error_code ret;
1548 krb5_creds *creds = NULL;
1550 *out_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);
1557 #else
1558 ret = ENOTSUP;
1559 #endif
1560 } else {
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,
1569 &in_creds, &creds);
1571 if (ret) {
1572 goto done;
1575 if (out_creds) {
1576 *out_creds = creds;
1579 done:
1580 if (creds && ret) {
1581 krb5_free_creds(context, creds);
1584 return ret;
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,
1601 time_t time_offset,
1602 const char *cc,
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;
1614 *creds_p = NULL;
1616 initialize_krb5_error_table();
1617 ret = krb5_init_context(&context);
1618 if (ret) {
1619 goto done;
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);
1628 if (ret) {
1629 goto done;
1632 ret = krb5_cc_get_principal(context, ccache, &me);
1633 if (ret) {
1634 goto done;
1637 ret = smb_krb5_parse_name(context, server_s, &server);
1638 if (ret) {
1639 goto done;
1642 if (impersonate_princ_s) {
1643 ret = smb_krb5_parse_name(context, impersonate_princ_s,
1644 &impersonate_princ);
1645 if (ret) {
1646 goto done;
1650 ret = smb_krb5_get_credentials(context, ccache,
1651 me, server, impersonate_princ,
1652 &creds);
1653 if (ret) {
1654 goto done;
1657 ret = krb5_cc_store_cred(context, ccache, creds);
1658 if (ret) {
1659 goto done;
1662 if (creds_p) {
1663 *creds_p = creds;
1666 DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n",
1667 server_s));
1669 if (impersonate_princ_s) {
1670 char *client = NULL;
1672 ret = smb_krb5_unparse_name(talloc_tos(), context, creds->client, &client);
1673 if (ret) {
1674 goto done;
1676 DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n",
1677 client));
1678 TALLOC_FREE(client);
1681 done:
1682 if (!context) {
1683 return ret;
1686 if (creds && ret) {
1687 krb5_free_creds(context, creds);
1689 if (server) {
1690 krb5_free_principal(context, server);
1692 if (me) {
1693 krb5_free_principal(context, me);
1695 if (impersonate_princ) {
1696 krb5_free_principal(context, impersonate_princ);
1698 if (ccache) {
1699 krb5_cc_close(context, ccache);
1701 krb5_free_context(context);
1703 return ret;
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 */
1723 krb5_data *realm;
1724 realm = krb5_princ_realm(context, principal);
1725 return discard_const_p(char, realm->data);
1726 #else
1727 return NULL;
1728 #endif
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"));
1741 return 1;
1744 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
1746 DEBUG(0,("NO KERBEROS SUPPORT\n"));
1747 return false;
1750 #endif /* HAVE_KRB5 */