s3-waf: Get winbindd to build again with current master
[Samba/gebeck_regimport.git] / source3 / libsmb / clikrb5.c
blob04c9bbf829cd1f73cdc2965dabd72dec16df3324
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 "authdata.h"
27 #ifdef HAVE_KRB5
29 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
30 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
31 #define GSSAPI_CHECKSUM_SIZE (12+GSSAPI_BNDLENGTH)
33 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY)
34 static krb5_error_code ads_krb5_get_fwd_ticket( krb5_context context,
35 krb5_auth_context *auth_context,
36 krb5_creds *credsp,
37 krb5_ccache ccache,
38 krb5_data *authenticator);
39 #endif
41 /**************************************************************
42 Wrappers around kerberos string functions that convert from
43 utf8 -> unix charset and vica versa.
44 **************************************************************/
46 /**************************************************************
47 krb5_parse_name that takes a UNIX charset.
48 **************************************************************/
50 krb5_error_code smb_krb5_parse_name(krb5_context context,
51 const char *name, /* in unix charset */
52 krb5_principal *principal)
54 krb5_error_code ret;
55 char *utf8_name;
56 size_t converted_size;
58 if (!push_utf8_talloc(talloc_tos(), &utf8_name, name, &converted_size)) {
59 return ENOMEM;
62 ret = krb5_parse_name(context, utf8_name, principal);
63 TALLOC_FREE(utf8_name);
64 return ret;
67 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
68 /**************************************************************
69 krb5_parse_name_norealm that takes a UNIX charset.
70 **************************************************************/
72 static krb5_error_code smb_krb5_parse_name_norealm_conv(krb5_context context,
73 const char *name, /* in unix charset */
74 krb5_principal *principal)
76 krb5_error_code ret;
77 char *utf8_name;
78 size_t converted_size;
80 *principal = NULL;
81 if (!push_utf8_talloc(talloc_tos(), &utf8_name, name, &converted_size)) {
82 return ENOMEM;
85 ret = krb5_parse_name_norealm(context, utf8_name, principal);
86 TALLOC_FREE(utf8_name);
87 return ret;
89 #endif
91 /**************************************************************
92 krb5_parse_name that returns a UNIX charset name. Must
93 be freed with talloc_free() call.
94 **************************************************************/
96 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
97 krb5_context context,
98 krb5_const_principal principal,
99 char **unix_name)
101 krb5_error_code ret;
102 char *utf8_name;
103 size_t converted_size;
105 *unix_name = NULL;
106 ret = krb5_unparse_name(context, principal, &utf8_name);
107 if (ret) {
108 return ret;
111 if (!pull_utf8_talloc(mem_ctx, unix_name, utf8_name, &converted_size)) {
112 krb5_free_unparsed_name(context, utf8_name);
113 return ENOMEM;
115 krb5_free_unparsed_name(context, utf8_name);
116 return 0;
119 #ifndef HAVE_KRB5_SET_REAL_TIME
121 * This function is not in the Heimdal mainline.
123 krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
125 krb5_error_code ret;
126 int32_t sec, usec;
128 ret = krb5_us_timeofday(context, &sec, &usec);
129 if (ret)
130 return ret;
132 context->kdc_sec_offset = seconds - sec;
133 context->kdc_usec_offset = microseconds - usec;
135 return 0;
137 #endif
139 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
141 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
143 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
144 * to krb5_set_default_tgs_ktypes. See
145 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
147 * If the MIT libraries are not exporting internal symbols, we will end up in
148 * this branch, which is correct. Otherwise we will continue to use the
149 * internal symbol
151 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
153 return krb5_set_default_tgs_enctypes(ctx, enc);
156 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
158 /* Heimdal */
159 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
161 return krb5_set_default_in_tkt_etypes(ctx, enc);
164 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
166 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
168 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
169 /* HEIMDAL */
170 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
172 memset(pkaddr, '\0', sizeof(krb5_address));
173 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
174 if (paddr->ss_family == AF_INET6) {
175 pkaddr->addr_type = KRB5_ADDRESS_INET6;
176 pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
177 pkaddr->address.data = (char *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
178 return true;
180 #endif
181 if (paddr->ss_family == AF_INET) {
182 pkaddr->addr_type = KRB5_ADDRESS_INET;
183 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
184 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
185 return true;
187 return false;
189 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
190 /* MIT */
191 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
193 memset(pkaddr, '\0', sizeof(krb5_address));
194 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
195 if (paddr->ss_family == AF_INET6) {
196 pkaddr->addrtype = ADDRTYPE_INET6;
197 pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
198 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
199 return true;
201 #endif
202 if (paddr->ss_family == AF_INET) {
203 pkaddr->addrtype = ADDRTYPE_INET;
204 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
205 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
206 return true;
208 return false;
210 #else
211 #error UNKNOWN_ADDRTYPE
212 #endif
214 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
215 static int create_kerberos_key_from_string_direct(krb5_context context,
216 krb5_principal host_princ,
217 krb5_data *password,
218 krb5_keyblock *key,
219 krb5_enctype enctype)
221 int ret = 0;
222 krb5_data salt;
223 krb5_encrypt_block eblock;
225 ret = krb5_principal2salt(context, host_princ, &salt);
226 if (ret) {
227 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
228 return ret;
230 krb5_use_enctype(context, &eblock, enctype);
231 ret = krb5_string_to_key(context, &eblock, key, password, &salt);
232 SAFE_FREE(salt.data);
234 return ret;
236 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
237 static int create_kerberos_key_from_string_direct(krb5_context context,
238 krb5_principal host_princ,
239 krb5_data *password,
240 krb5_keyblock *key,
241 krb5_enctype enctype)
243 int ret;
244 krb5_salt salt;
246 ret = krb5_get_pw_salt(context, host_princ, &salt);
247 if (ret) {
248 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
249 return ret;
252 ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, salt, key);
253 krb5_free_salt(context, salt);
255 return ret;
257 #else
258 #error UNKNOWN_CREATE_KEY_FUNCTIONS
259 #endif
261 int create_kerberos_key_from_string(krb5_context context,
262 krb5_principal host_princ,
263 krb5_data *password,
264 krb5_keyblock *key,
265 krb5_enctype enctype,
266 bool no_salt)
268 krb5_principal salt_princ = NULL;
269 int ret;
271 * Check if we've determined that the KDC is salting keys for this
272 * principal/enctype in a non-obvious way. If it is, try to match
273 * its behavior.
275 if (no_salt) {
276 KRB5_KEY_DATA(key) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
277 if (!KRB5_KEY_DATA(key)) {
278 return ENOMEM;
280 memcpy(KRB5_KEY_DATA(key), password->data, password->length);
281 KRB5_KEY_LENGTH(key) = password->length;
282 KRB5_KEY_TYPE(key) = enctype;
283 return 0;
285 salt_princ = kerberos_fetch_salt_princ_for_host_princ(context, host_princ, enctype);
286 ret = create_kerberos_key_from_string_direct(context, salt_princ ? salt_princ : host_princ, password, key, enctype);
287 if (salt_princ) {
288 krb5_free_principal(context, salt_princ);
290 return ret;
293 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
294 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
295 krb5_enctype **enctypes)
297 return krb5_get_permitted_enctypes(context, enctypes);
299 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
300 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
301 krb5_enctype **enctypes)
303 return krb5_get_default_in_tkt_etypes(context, enctypes);
305 #else
306 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
307 #endif
309 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
310 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
311 krb5_auth_context auth_context,
312 krb5_keyblock *keyblock)
314 return krb5_auth_con_setkey(context, auth_context, keyblock);
316 #endif
318 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
319 DATA_BLOB *edata,
320 DATA_BLOB *edata_out)
322 DATA_BLOB edata_contents;
323 ASN1_DATA *data;
324 int edata_type;
326 if (!edata->length) {
327 return False;
330 data = asn1_init(mem_ctx);
331 if (data == NULL) {
332 return false;
335 asn1_load(data, *edata);
336 asn1_start_tag(data, ASN1_SEQUENCE(0));
337 asn1_start_tag(data, ASN1_CONTEXT(1));
338 asn1_read_Integer(data, &edata_type);
340 if (edata_type != KRB5_PADATA_PW_SALT) {
341 DEBUG(0,("edata is not of required type %d but of type %d\n",
342 KRB5_PADATA_PW_SALT, edata_type));
343 asn1_free(data);
344 return False;
347 asn1_start_tag(data, ASN1_CONTEXT(2));
348 asn1_read_OctetString(data, talloc_autofree_context(), &edata_contents);
349 asn1_end_tag(data);
350 asn1_end_tag(data);
351 asn1_end_tag(data);
352 asn1_free(data);
354 *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length);
356 data_blob_free(&edata_contents);
358 return True;
362 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
364 DATA_BLOB pac_contents;
365 ASN1_DATA *data;
366 int data_type;
368 if (!auth_data->length) {
369 return False;
372 data = asn1_init(mem_ctx);
373 if (data == NULL) {
374 return false;
377 asn1_load(data, *auth_data);
378 asn1_start_tag(data, ASN1_SEQUENCE(0));
379 asn1_start_tag(data, ASN1_SEQUENCE(0));
380 asn1_start_tag(data, ASN1_CONTEXT(0));
381 asn1_read_Integer(data, &data_type);
383 if (data_type != KRB5_AUTHDATA_WIN2K_PAC ) {
384 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type));
385 asn1_free(data);
386 return False;
389 asn1_end_tag(data);
390 asn1_start_tag(data, ASN1_CONTEXT(1));
391 asn1_read_OctetString(data, talloc_autofree_context(), &pac_contents);
392 asn1_end_tag(data);
393 asn1_end_tag(data);
394 asn1_end_tag(data);
395 asn1_free(data);
397 *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length);
399 data_blob_free(&pac_contents);
401 return True;
404 bool get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, krb5_ticket *tkt)
406 DATA_BLOB auth_data_wrapped;
407 bool got_auth_data_pac = False;
408 int i;
410 #if defined(HAVE_KRB5_TKT_ENC_PART2)
411 if (tkt->enc_part2 && tkt->enc_part2->authorization_data &&
412 tkt->enc_part2->authorization_data[0] &&
413 tkt->enc_part2->authorization_data[0]->length)
415 for (i = 0; tkt->enc_part2->authorization_data[i] != NULL; i++) {
417 if (tkt->enc_part2->authorization_data[i]->ad_type !=
418 KRB5_AUTHDATA_IF_RELEVANT) {
419 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
420 tkt->enc_part2->authorization_data[i]->ad_type));
421 continue;
424 auth_data_wrapped = data_blob(tkt->enc_part2->authorization_data[i]->contents,
425 tkt->enc_part2->authorization_data[i]->length);
427 /* check if it is a PAC */
428 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
429 data_blob_free(&auth_data_wrapped);
431 if (got_auth_data_pac) {
432 return true;
436 return got_auth_data_pac;
439 #else
440 if (tkt->ticket.authorization_data &&
441 tkt->ticket.authorization_data->len)
443 for (i = 0; i < tkt->ticket.authorization_data->len; i++) {
445 if (tkt->ticket.authorization_data->val[i].ad_type !=
446 KRB5_AUTHDATA_IF_RELEVANT) {
447 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
448 tkt->ticket.authorization_data->val[i].ad_type));
449 continue;
452 auth_data_wrapped = data_blob(tkt->ticket.authorization_data->val[i].ad_data.data,
453 tkt->ticket.authorization_data->val[i].ad_data.length);
455 /* check if it is a PAC */
456 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
457 data_blob_free(&auth_data_wrapped);
459 if (got_auth_data_pac) {
460 return true;
464 return got_auth_data_pac;
466 #endif
467 return False;
470 krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
472 #if defined(HAVE_KRB5_TKT_ENC_PART2)
473 return tkt->enc_part2->client;
474 #else
475 return tkt->client;
476 #endif
479 #if !defined(HAVE_KRB5_LOCATE_KDC)
481 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
482 * to a public interface for this functionality, so we have to be able to live
483 * without it if the MIT libraries are hiding their internal symbols.
486 #if defined(KRB5_KRBHST_INIT)
487 /* Heimdal */
488 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, struct sockaddr **addr_pp, int *naddrs, int get_masters)
490 krb5_krbhst_handle hnd;
491 krb5_krbhst_info *hinfo;
492 krb5_error_code rc;
493 int num_kdcs, i;
494 struct sockaddr *sa;
495 struct addrinfo *ai;
497 *addr_pp = NULL;
498 *naddrs = 0;
500 rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
501 if (rc) {
502 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
503 return rc;
506 for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
509 krb5_krbhst_reset(ctx, hnd);
511 if (!num_kdcs) {
512 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
513 krb5_krbhst_free(ctx, hnd);
514 return -1;
517 sa = SMB_MALLOC_ARRAY( struct sockaddr, num_kdcs );
518 if (!sa) {
519 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
520 krb5_krbhst_free(ctx, hnd);
521 naddrs = 0;
522 return -1;
525 memset(sa, '\0', sizeof(struct sockaddr) * num_kdcs );
527 for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
529 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
530 rc = krb5_krbhst_get_addrinfo(ctx, hinfo, &ai);
531 if (rc) {
532 DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc)));
533 continue;
535 #endif
536 if (hinfo->ai && hinfo->ai->ai_family == AF_INET)
537 memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
540 krb5_krbhst_free(ctx, hnd);
542 *naddrs = num_kdcs;
543 *addr_pp = sa;
544 return 0;
547 #else /* ! defined(KRB5_KRBHST_INIT) */
549 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
550 struct sockaddr **addr_pp, int *naddrs, int get_masters)
552 DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
553 return KRB5_KDC_UNREACH;
556 #endif /* KRB5_KRBHST_INIT */
558 #else /* ! HAVE_KRB5_LOCATE_KDC */
560 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
561 struct sockaddr **addr_pp, int *naddrs, int get_masters)
563 return krb5_locate_kdc(ctx, realm, addr_pp, naddrs, get_masters);
566 #endif /* HAVE_KRB5_LOCATE_KDC */
568 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
569 void krb5_free_unparsed_name(krb5_context context, char *val)
571 SAFE_FREE(val);
573 #endif
575 void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
577 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
578 if (pdata->data) {
579 krb5_free_data_contents(context, pdata);
581 #else
582 SAFE_FREE(pdata->data);
583 #endif
586 void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype)
588 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
589 KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype;
590 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
591 KRB5_KEY_TYPE((&pcreds->session)) = enctype;
592 #else
593 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
594 #endif
597 bool kerberos_compatible_enctypes(krb5_context context,
598 krb5_enctype enctype1,
599 krb5_enctype enctype2)
601 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
602 krb5_boolean similar = 0;
604 krb5_c_enctype_compare(context, enctype1, enctype2, &similar);
605 return similar ? True : False;
606 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
607 return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False;
608 #endif
611 static bool ads_cleanup_expired_creds(krb5_context context,
612 krb5_ccache ccache,
613 krb5_creds *credsp)
615 krb5_error_code retval;
616 const char *cc_type = krb5_cc_get_type(context, ccache);
618 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
619 cc_type, krb5_cc_get_name(context, ccache),
620 http_timestring(talloc_tos(), credsp->times.endtime)));
622 /* we will probably need new tickets if the current ones
623 will expire within 10 seconds.
625 if (credsp->times.endtime >= (time(NULL) + 10))
626 return False;
628 /* heimdal won't remove creds from a file ccache, and
629 perhaps we shouldn't anyway, since internally we
630 use memory ccaches, and a FILE one probably means that
631 we're using creds obtained outside of our exectuable
633 if (strequal(cc_type, "FILE")) {
634 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
635 return False;
638 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
639 if (retval) {
640 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
641 error_message(retval)));
642 /* If we have an error in this, we want to display it,
643 but continue as though we deleted it */
645 return True;
649 we can't use krb5_mk_req because w2k wants the service to be in a particular format
651 static krb5_error_code ads_krb5_mk_req(krb5_context context,
652 krb5_auth_context *auth_context,
653 const krb5_flags ap_req_options,
654 const char *principal,
655 krb5_ccache ccache,
656 krb5_data *outbuf,
657 time_t *expire_time,
658 const char *impersonate_princ_s)
660 krb5_error_code retval;
661 krb5_principal server;
662 krb5_principal impersonate_princ = NULL;
663 krb5_creds * credsp;
664 krb5_creds creds;
665 krb5_data in_data;
666 bool creds_ready = False;
667 int i = 0, maxtries = 3;
669 ZERO_STRUCT(in_data);
671 retval = smb_krb5_parse_name(context, principal, &server);
672 if (retval) {
673 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
674 return retval;
677 if (impersonate_princ_s) {
678 retval = smb_krb5_parse_name(context, impersonate_princ_s,
679 &impersonate_princ);
680 if (retval) {
681 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s));
682 goto cleanup_princ;
686 /* obtain ticket & session key */
687 ZERO_STRUCT(creds);
688 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
689 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
690 error_message(retval)));
691 goto cleanup_princ;
694 if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
695 /* This can commonly fail on smbd startup with no ticket in the cache.
696 * Report at higher level than 1. */
697 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
698 error_message(retval)));
699 goto cleanup_creds;
702 while (!creds_ready && (i < maxtries)) {
704 if ((retval = smb_krb5_get_credentials(context, ccache,
705 creds.client,
706 creds.server,
707 impersonate_princ,
708 &credsp))) {
709 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
710 principal, error_message(retval)));
711 goto cleanup_creds;
714 /* cope with ticket being in the future due to clock skew */
715 if ((unsigned)credsp->times.starttime > time(NULL)) {
716 time_t t = time(NULL);
717 int time_offset =(int)((unsigned)credsp->times.starttime-t);
718 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
719 krb5_set_real_time(context, t + time_offset + 1, 0);
722 if (!ads_cleanup_expired_creds(context, ccache, credsp)) {
723 creds_ready = True;
726 i++;
729 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
730 principal, krb5_cc_get_type(context, ccache), krb5_cc_get_name(context, ccache),
731 http_timestring(talloc_tos(), (unsigned)credsp->times.endtime),
732 (unsigned)credsp->times.endtime));
734 if (expire_time) {
735 *expire_time = (time_t)credsp->times.endtime;
738 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY)
739 if( credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE ) {
740 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
741 as part of the kerberos exchange. */
743 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
745 if( *auth_context == NULL ) {
746 /* Allocate if it has not yet been allocated. */
747 retval = krb5_auth_con_init( context, auth_context );
748 if (retval) {
749 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_init failed (%s)\n",
750 error_message(retval)));
751 goto cleanup_creds;
755 retval = krb5_auth_con_setuseruserkey( context, *auth_context, &credsp->keyblock );
756 if (retval) {
757 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_setuseruserkey failed (%s)\n",
758 error_message(retval)));
759 goto cleanup_creds;
762 /* Must use a subkey for forwarded tickets. */
763 retval = krb5_auth_con_setflags( context, *auth_context, KRB5_AUTH_CONTEXT_USE_SUBKEY);
764 if (retval) {
765 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_setflags failed (%s)\n",
766 error_message(retval)));
767 goto cleanup_creds;
770 retval = ads_krb5_get_fwd_ticket( context,
771 auth_context,
772 credsp,
773 ccache,
774 &in_data );
775 if (retval) {
776 DEBUG( 3, ("ads_krb5_get_fwd_ticket failed (%s)\n",
777 error_message( retval ) ) );
780 * This is not fatal. Delete the *auth_context and continue
781 * with krb5_mk_req_extended to get a non-forwardable ticket.
784 if (in_data.data) {
785 free( in_data.data );
786 in_data.data = NULL;
787 in_data.length = 0;
789 krb5_auth_con_free(context, *auth_context);
790 *auth_context = NULL;
793 #endif
795 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
796 &in_data, credsp, outbuf);
797 if (retval) {
798 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
799 error_message(retval)));
802 if (in_data.data) {
803 free( in_data.data );
804 in_data.length = 0;
807 krb5_free_creds(context, credsp);
809 cleanup_creds:
810 krb5_free_cred_contents(context, &creds);
812 cleanup_princ:
813 krb5_free_principal(context, server);
814 if (impersonate_princ) {
815 krb5_free_principal(context, impersonate_princ);
818 return retval;
822 get a kerberos5 ticket for the given service
824 int cli_krb5_get_ticket(const char *principal, time_t time_offset,
825 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
826 uint32 extra_ap_opts, const char *ccname,
827 time_t *tgs_expire,
828 const char *impersonate_princ_s)
831 krb5_error_code retval;
832 krb5_data packet;
833 krb5_context context = NULL;
834 krb5_ccache ccdef = NULL;
835 krb5_auth_context auth_context = NULL;
836 krb5_enctype enc_types[] = {
837 #ifdef ENCTYPE_ARCFOUR_HMAC
838 ENCTYPE_ARCFOUR_HMAC,
839 #endif
840 ENCTYPE_DES_CBC_MD5,
841 ENCTYPE_DES_CBC_CRC,
842 ENCTYPE_NULL};
844 initialize_krb5_error_table();
845 retval = krb5_init_context(&context);
846 if (retval) {
847 DEBUG(1,("cli_krb5_get_ticket: krb5_init_context failed (%s)\n",
848 error_message(retval)));
849 goto failed;
852 if (time_offset != 0) {
853 krb5_set_real_time(context, time(NULL) + time_offset, 0);
856 if ((retval = krb5_cc_resolve(context, ccname ?
857 ccname : krb5_cc_default_name(context), &ccdef))) {
858 DEBUG(1,("cli_krb5_get_ticket: krb5_cc_default failed (%s)\n",
859 error_message(retval)));
860 goto failed;
863 if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
864 DEBUG(1,("cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s)\n",
865 error_message(retval)));
866 goto failed;
869 if ((retval = ads_krb5_mk_req(context,
870 &auth_context,
871 AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
872 principal,
873 ccdef, &packet,
874 tgs_expire,
875 impersonate_princ_s))) {
876 goto failed;
879 get_krb5_smb_session_key(context, auth_context, session_key_krb5, False);
881 *ticket = data_blob(packet.data, packet.length);
883 kerberos_free_data_contents(context, &packet);
885 failed:
887 if ( context ) {
888 if (ccdef)
889 krb5_cc_close(context, ccdef);
890 if (auth_context)
891 krb5_auth_con_free(context, auth_context);
892 krb5_free_context(context);
895 return retval;
898 bool get_krb5_smb_session_key(krb5_context context, krb5_auth_context auth_context, DATA_BLOB *session_key, bool remote)
900 krb5_keyblock *skey = NULL;
901 krb5_error_code err = 0;
902 bool ret = false;
904 if (remote) {
905 err = krb5_auth_con_getremotesubkey(context, auth_context, &skey);
906 } else {
907 err = krb5_auth_con_getlocalsubkey(context, auth_context, &skey);
910 if (err || skey == NULL) {
911 DEBUG(10, ("KRB5 error getting session key %d\n", err));
912 goto done;
915 DEBUG(10, ("Got KRB5 session key of length %d\n", (int)KRB5_KEY_LENGTH(skey)));
916 *session_key = data_blob(KRB5_KEY_DATA(skey), KRB5_KEY_LENGTH(skey));
917 dump_data_pw("KRB5 Session Key:\n", session_key->data, session_key->length);
919 ret = true;
921 done:
922 if (skey) {
923 krb5_free_keyblock(context, skey);
926 return ret;
930 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
931 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i );
933 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
935 static krb5_data kdata;
937 kdata.data = (char *)krb5_principal_get_comp_string(context, principal, i);
938 kdata.length = strlen((const char *)kdata.data);
939 return &kdata;
941 #endif
943 krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
945 /* Try krb5_free_keytab_entry_contents first, since
946 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
947 * krb5_kt_free_entry but only has a prototype for the first, while the
948 * second is considered private.
950 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
951 return krb5_free_keytab_entry_contents(context, kt_entry);
952 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
953 return krb5_kt_free_entry(context, kt_entry);
954 #else
955 #error UNKNOWN_KT_FREE_FUNCTION
956 #endif
959 void smb_krb5_checksum_from_pac_sig(krb5_checksum *cksum,
960 struct PAC_SIGNATURE_DATA *sig)
962 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
963 cksum->cksumtype = (krb5_cksumtype)sig->type;
964 cksum->checksum.length = sig->signature.length;
965 cksum->checksum.data = sig->signature.data;
966 #else
967 cksum->checksum_type = (krb5_cksumtype)sig->type;
968 cksum->length = sig->signature.length;
969 cksum->contents = sig->signature.data;
970 #endif
973 krb5_error_code smb_krb5_verify_checksum(krb5_context context,
974 const krb5_keyblock *keyblock,
975 krb5_keyusage usage,
976 krb5_checksum *cksum,
977 uint8 *data,
978 size_t length)
980 krb5_error_code ret;
982 /* verify the checksum */
984 /* welcome to the wonderful world of samba's kerberos abstraction layer:
986 * function heimdal 0.6.1rc3 heimdal 0.7 MIT krb 1.4.2
987 * -----------------------------------------------------------------------------
988 * krb5_c_verify_checksum - works works
989 * krb5_verify_checksum works (6 args) works (6 args) broken (7 args)
992 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
994 krb5_boolean checksum_valid = False;
995 krb5_data input;
997 input.data = (char *)data;
998 input.length = length;
1000 ret = krb5_c_verify_checksum(context,
1001 keyblock,
1002 usage,
1003 &input,
1004 cksum,
1005 &checksum_valid);
1006 if (ret) {
1007 DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n",
1008 error_message(ret)));
1009 return ret;
1012 if (!checksum_valid)
1013 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1016 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
1018 /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
1019 * without enctype and it ignores any key_usage types - Guenther */
1023 krb5_crypto crypto;
1024 ret = krb5_crypto_init(context,
1025 keyblock,
1027 &crypto);
1028 if (ret) {
1029 DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n",
1030 error_message(ret)));
1031 return ret;
1034 ret = krb5_verify_checksum(context,
1035 crypto,
1036 usage,
1037 data,
1038 length,
1039 cksum);
1041 krb5_crypto_destroy(context, crypto);
1044 #else
1045 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
1046 #endif
1048 return ret;
1051 time_t get_authtime_from_tkt(krb5_ticket *tkt)
1053 #if defined(HAVE_KRB5_TKT_ENC_PART2)
1054 return tkt->enc_part2->times.authtime;
1055 #else
1056 return tkt->ticket.authtime;
1057 #endif
1060 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1061 static int get_kvno_from_ap_req(krb5_ap_req *ap_req)
1063 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
1064 if (ap_req->ticket->enc_part.kvno)
1065 return ap_req->ticket->enc_part.kvno;
1066 #else /* Heimdal */
1067 if (ap_req->ticket.enc_part.kvno)
1068 return *ap_req->ticket.enc_part.kvno;
1069 #endif
1070 return 0;
1073 static krb5_enctype get_enctype_from_ap_req(krb5_ap_req *ap_req)
1075 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
1076 return ap_req->ticket.enc_part.etype;
1077 #else /* MIT */
1078 return ap_req->ticket->enc_part.enctype;
1079 #endif
1081 #endif /* HAVE_KRB5_DECODE_AP_REQ */
1083 static krb5_error_code
1084 get_key_from_keytab(krb5_context context,
1085 krb5_const_principal server,
1086 krb5_enctype enctype,
1087 krb5_kvno kvno,
1088 krb5_keyblock **out_key)
1090 krb5_keytab_entry entry;
1091 krb5_error_code ret;
1092 krb5_keytab keytab;
1093 char *name = NULL;
1094 krb5_keyblock *keyp;
1096 /* We have to open a new keytab handle here, as MIT does
1097 an implicit open/getnext/close on krb5_kt_get_entry. We
1098 may be in the middle of a keytab enumeration when this is
1099 called. JRA. */
1101 ret = smb_krb5_open_keytab(context, NULL, False, &keytab);
1102 if (ret) {
1103 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
1104 return ret;
1107 if ( DEBUGLEVEL >= 10 ) {
1108 if (smb_krb5_unparse_name(talloc_tos(), context, server, &name) == 0) {
1109 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
1110 kvno, enctype, name));
1111 TALLOC_FREE(name);
1115 ret = krb5_kt_get_entry(context,
1116 keytab,
1117 server,
1118 kvno,
1119 enctype,
1120 &entry);
1122 if (ret) {
1123 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret)));
1124 goto out;
1127 keyp = KRB5_KT_KEY(&entry);
1129 ret = krb5_copy_keyblock(context, keyp, out_key);
1130 if (ret) {
1131 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret)));
1132 goto out;
1135 smb_krb5_kt_free_entry(context, &entry);
1137 out:
1138 krb5_kt_close(context, keytab);
1139 return ret;
1142 /* Prototypes */
1144 krb5_error_code smb_krb5_get_keyinfo_from_ap_req(krb5_context context,
1145 const krb5_data *inbuf,
1146 krb5_kvno *kvno,
1147 krb5_enctype *enctype)
1149 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1151 krb5_error_code ret;
1152 krb5_ap_req ap_req;
1154 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
1155 if (ret)
1156 return ret;
1158 *kvno = get_kvno_from_ap_req(&ap_req);
1159 *enctype = get_enctype_from_ap_req(&ap_req);
1161 free_AP_REQ(&ap_req);
1162 return 0;
1164 #endif
1166 /* Possibly not an appropriate error code. */
1167 return KRB5KDC_ERR_BADOPTION;
1170 krb5_error_code krb5_rd_req_return_keyblock_from_keytab(krb5_context context,
1171 krb5_auth_context *auth_context,
1172 const krb5_data *inbuf,
1173 krb5_const_principal server,
1174 krb5_keytab keytab,
1175 krb5_flags *ap_req_options,
1176 krb5_ticket **ticket,
1177 krb5_keyblock **keyblock)
1179 krb5_error_code ret;
1180 krb5_kvno kvno;
1181 krb5_enctype enctype;
1182 krb5_keyblock *local_keyblock;
1184 ret = krb5_rd_req(context,
1185 auth_context,
1186 inbuf,
1187 server,
1188 keytab,
1189 ap_req_options,
1190 ticket);
1191 if (ret) {
1192 return ret;
1195 #ifdef KRB5_TICKET_HAS_KEYINFO
1196 enctype = (*ticket)->enc_part.enctype;
1197 kvno = (*ticket)->enc_part.kvno;
1198 #else
1199 ret = smb_krb5_get_keyinfo_from_ap_req(context, inbuf, &kvno, &enctype);
1200 if (ret) {
1201 return ret;
1203 #endif
1205 ret = get_key_from_keytab(context,
1206 server,
1207 enctype,
1208 kvno,
1209 &local_keyblock);
1210 if (ret) {
1211 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1212 goto out;
1215 out:
1216 if (ret && local_keyblock != NULL) {
1217 krb5_free_keyblock(context, local_keyblock);
1218 } else {
1219 *keyblock = local_keyblock;
1222 return ret;
1225 krb5_error_code smb_krb5_parse_name_norealm(krb5_context context,
1226 const char *name,
1227 krb5_principal *principal)
1229 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1230 return smb_krb5_parse_name_norealm_conv(context, name, principal);
1231 #endif
1233 /* we are cheating here because parse_name will in fact set the realm.
1234 * We don't care as the only caller of smb_krb5_parse_name_norealm
1235 * ignores the realm anyway when calling
1236 * smb_krb5_principal_compare_any_realm later - Guenther */
1238 return smb_krb5_parse_name(context, name, principal);
1241 bool smb_krb5_principal_compare_any_realm(krb5_context context,
1242 krb5_const_principal princ1,
1243 krb5_const_principal princ2)
1245 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1247 return krb5_principal_compare_any_realm(context, princ1, princ2);
1249 /* krb5_princ_size is a macro in MIT */
1250 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1252 int i, len1, len2;
1253 const krb5_data *p1, *p2;
1255 len1 = krb5_princ_size(context, princ1);
1256 len2 = krb5_princ_size(context, princ2);
1258 if (len1 != len2)
1259 return False;
1261 for (i = 0; i < len1; i++) {
1263 p1 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ1), i);
1264 p2 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ2), i);
1266 if (p1->length != p2->length || memcmp(p1->data, p2->data, p1->length))
1267 return False;
1270 return True;
1271 #else
1272 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1273 #endif
1276 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string, /* FILE:/tmp/krb5cc_0 */
1277 const char *client_string, /* gd@BER.SUSE.DE */
1278 const char *service_string, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1279 time_t *expire_time)
1281 krb5_error_code ret;
1282 krb5_context context = NULL;
1283 krb5_ccache ccache = NULL;
1284 krb5_principal client = NULL;
1285 krb5_creds creds, creds_in, *creds_out = NULL;
1287 ZERO_STRUCT(creds);
1288 ZERO_STRUCT(creds_in);
1290 initialize_krb5_error_table();
1291 ret = krb5_init_context(&context);
1292 if (ret) {
1293 goto done;
1296 if (!ccache_string) {
1297 ccache_string = krb5_cc_default_name(context);
1300 if (!ccache_string) {
1301 ret = EINVAL;
1302 goto done;
1305 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
1307 /* FIXME: we should not fall back to defaults */
1308 ret = krb5_cc_resolve(context, CONST_DISCARD(char *, ccache_string), &ccache);
1309 if (ret) {
1310 goto done;
1313 if (client_string) {
1314 ret = smb_krb5_parse_name(context, client_string, &client);
1315 if (ret) {
1316 goto done;
1318 } else {
1319 ret = krb5_cc_get_principal(context, ccache, &client);
1320 if (ret) {
1321 goto done;
1325 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1327 ret = krb5_get_renewed_creds(context, &creds, client, ccache, CONST_DISCARD(char *, service_string));
1328 if (ret) {
1329 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1330 goto done;
1333 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1335 krb5_kdc_flags flags;
1336 krb5_realm *client_realm = NULL;
1338 ret = krb5_copy_principal(context, client, &creds_in.client);
1339 if (ret) {
1340 goto done;
1343 if (service_string) {
1344 ret = smb_krb5_parse_name(context, service_string, &creds_in.server);
1345 if (ret) {
1346 goto done;
1348 } else {
1349 /* build tgt service by default */
1350 client_realm = krb5_princ_realm(context, creds_in.client);
1351 if (!client_realm) {
1352 ret = ENOMEM;
1353 goto done;
1355 ret = krb5_make_principal(context, &creds_in.server, *client_realm, KRB5_TGS_NAME, *client_realm, NULL);
1356 if (ret) {
1357 goto done;
1361 flags.i = 0;
1362 flags.b.renewable = flags.b.renew = True;
1364 ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &creds_in, &creds_out);
1365 if (ret) {
1366 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1367 goto done;
1370 creds = *creds_out;
1372 #else
1373 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1374 #endif
1376 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1377 ret = krb5_cc_initialize(context, ccache, client);
1378 if (ret) {
1379 goto done;
1382 ret = krb5_cc_store_cred(context, ccache, &creds);
1384 if (expire_time) {
1385 *expire_time = (time_t) creds.times.endtime;
1388 done:
1389 krb5_free_cred_contents(context, &creds_in);
1391 if (creds_out) {
1392 krb5_free_creds(context, creds_out);
1393 } else {
1394 krb5_free_cred_contents(context, &creds);
1397 if (client) {
1398 krb5_free_principal(context, client);
1400 if (ccache) {
1401 krb5_cc_close(context, ccache);
1403 if (context) {
1404 krb5_free_context(context);
1407 return ret;
1410 krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr)
1412 krb5_error_code ret = 0;
1413 if (addr == NULL) {
1414 return ret;
1416 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1417 krb5_free_addresses(context, addr->addrs);
1418 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1419 ret = krb5_free_addresses(context, addr->addrs);
1420 SAFE_FREE(addr->addrs);
1421 #endif
1422 SAFE_FREE(addr);
1423 addr = NULL;
1424 return ret;
1427 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr)
1429 krb5_error_code ret = 0;
1430 nstring buf;
1431 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1432 krb5_address **addrs = NULL;
1433 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1434 krb5_addresses *addrs = NULL;
1435 #endif
1437 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
1438 if (*kerb_addr == NULL) {
1439 return ENOMEM;
1442 put_name(buf, global_myname(), ' ', 0x20);
1444 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1446 int num_addr = 2;
1448 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
1449 if (addrs == NULL) {
1450 SAFE_FREE(*kerb_addr);
1451 return ENOMEM;
1454 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
1456 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1457 if (addrs[0] == NULL) {
1458 SAFE_FREE(addrs);
1459 SAFE_FREE(*kerb_addr);
1460 return ENOMEM;
1463 addrs[0]->magic = KV5M_ADDRESS;
1464 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
1465 addrs[0]->length = MAX_NETBIOSNAME_LEN;
1466 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
1467 if (addrs[0]->contents == NULL) {
1468 SAFE_FREE(addrs[0]);
1469 SAFE_FREE(addrs);
1470 SAFE_FREE(*kerb_addr);
1471 return ENOMEM;
1474 memcpy(addrs[0]->contents, buf, addrs[0]->length);
1476 addrs[1] = NULL;
1478 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1480 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
1481 if (addrs == NULL) {
1482 SAFE_FREE(*kerb_addr);
1483 return ENOMEM;
1486 memset(addrs, 0, sizeof(krb5_addresses));
1488 addrs->len = 1;
1489 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1490 if (addrs->val == NULL) {
1491 SAFE_FREE(addrs);
1492 SAFE_FREE(kerb_addr);
1493 return ENOMEM;
1496 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
1497 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
1498 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
1499 if (addrs->val[0].address.data == NULL) {
1500 SAFE_FREE(addrs->val);
1501 SAFE_FREE(addrs);
1502 SAFE_FREE(*kerb_addr);
1503 return ENOMEM;
1506 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
1508 #else
1509 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1510 #endif
1511 (*kerb_addr)->addrs = addrs;
1513 return ret;
1516 void smb_krb5_free_error(krb5_context context, krb5_error *krberror)
1518 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1519 krb5_free_error_contents(context, krberror);
1520 #else /* MIT */
1521 krb5_free_error(context, krberror);
1522 #endif
1525 krb5_error_code handle_krberror_packet(krb5_context context,
1526 krb5_data *packet)
1528 krb5_error_code ret;
1529 bool got_error_code = False;
1531 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1533 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1535 krb5_error krberror;
1537 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1538 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1539 error_message(ret)));
1540 return ret;
1543 if (krberror.e_data == NULL || krberror.e_data->data == NULL) {
1544 ret = (krb5_error_code) krberror.error_code;
1545 got_error_code = True;
1548 smb_krb5_free_error(context, &krberror);
1550 #else /* MIT */
1552 krb5_error *krberror;
1554 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1555 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1556 error_message(ret)));
1557 return ret;
1560 if (krberror->e_data.data == NULL) {
1561 #if defined(ERROR_TABLE_BASE_krb5)
1562 ret = ERROR_TABLE_BASE_krb5 + (krb5_error_code) krberror->error;
1563 #else
1564 ret = (krb5_error_code)krberror->error;
1565 #endif
1566 got_error_code = True;
1568 smb_krb5_free_error(context, krberror);
1570 #endif
1571 if (got_error_code) {
1572 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1573 error_message(ret), ret));
1575 return ret;
1578 krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context,
1579 krb5_get_init_creds_opt **opt)
1581 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1582 /* Heimdal or modern MIT version */
1583 return krb5_get_init_creds_opt_alloc(context, opt);
1584 #else
1585 /* Historical MIT version */
1586 krb5_get_init_creds_opt *my_opt;
1588 *opt = NULL;
1590 if ((my_opt = SMB_MALLOC_P(krb5_get_init_creds_opt)) == NULL) {
1591 return ENOMEM;
1594 krb5_get_init_creds_opt_init(my_opt);
1596 *opt = my_opt;
1597 return 0;
1598 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1601 void smb_krb5_get_init_creds_opt_free(krb5_context context,
1602 krb5_get_init_creds_opt *opt)
1604 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1606 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1607 /* Modern MIT or Heimdal version */
1608 krb5_get_init_creds_opt_free(context, opt);
1609 #else
1610 /* Heimdal version */
1611 krb5_get_init_creds_opt_free(opt);
1612 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1614 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1615 /* Historical MIT version */
1616 SAFE_FREE(opt);
1617 opt = NULL;
1618 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1621 krb5_enctype smb_get_enctype_from_kt_entry(krb5_keytab_entry *kt_entry)
1623 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
1627 /* caller needs to free etype_s */
1628 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
1629 krb5_enctype enctype,
1630 char **etype_s)
1632 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1633 return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
1634 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1635 char buf[256];
1636 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
1637 if (ret) {
1638 return ret;
1640 *etype_s = SMB_STRDUP(buf);
1641 if (!*etype_s) {
1642 return ENOMEM;
1644 return ret;
1645 #else
1646 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1647 #endif
1650 krb5_error_code smb_krb5_mk_error(krb5_context context,
1651 krb5_error_code error_code,
1652 const krb5_principal server,
1653 krb5_data *reply)
1655 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1657 * The MIT interface is *terrible*.
1658 * We have to construct this ourselves...
1660 krb5_error e;
1662 memset(&e, 0, sizeof(e));
1663 krb5_us_timeofday(context, &e.stime, &e.susec);
1664 e.server = server;
1665 #if defined(krb5_err_base)
1666 e.error = error_code - krb5_err_base;
1667 #elif defined(ERROR_TABLE_BASE_krb5)
1668 e.error = error_code - ERROR_TABLE_BASE_krb5;
1669 #else
1670 e.error = error_code; /* Almost certainly wrong, but what can we do... ? */
1671 #endif
1673 return krb5_mk_error(context, &e, reply);
1674 #else /* Heimdal. */
1675 return krb5_mk_error(context,
1676 error_code,
1677 NULL,
1678 NULL, /* e_data */
1679 NULL,
1680 server,
1681 NULL,
1682 NULL,
1683 reply);
1684 #endif
1687 /**********************************************************************
1688 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1689 * allows to process non-default keytab names.
1690 * @param context krb5_context
1691 * @param keytab_name_req string
1692 * @param write_access bool if writable keytab is required
1693 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1694 * @return krb5_error_code
1695 **********************************************************************/
1697 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1698 #ifndef MAX_KEYTAB_NAME_LEN
1699 #define MAX_KEYTAB_NAME_LEN 1100
1700 #endif
1702 krb5_error_code smb_krb5_open_keytab(krb5_context context,
1703 const char *keytab_name_req,
1704 bool write_access,
1705 krb5_keytab *keytab)
1707 krb5_error_code ret = 0;
1708 TALLOC_CTX *mem_ctx;
1709 char keytab_string[MAX_KEYTAB_NAME_LEN];
1710 char *kt_str = NULL;
1711 bool found_valid_name = False;
1712 const char *pragma = "FILE";
1713 const char *tmp = NULL;
1715 if (!write_access && !keytab_name_req) {
1716 /* caller just wants to read the default keytab readonly, so be it */
1717 return krb5_kt_default(context, keytab);
1720 mem_ctx = talloc_init("smb_krb5_open_keytab");
1721 if (!mem_ctx) {
1722 return ENOMEM;
1725 #ifdef HAVE_WRFILE_KEYTAB
1726 if (write_access) {
1727 pragma = "WRFILE";
1729 #endif
1731 if (keytab_name_req) {
1733 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1734 ret = KRB5_CONFIG_NOTENUFSPACE;
1735 goto out;
1738 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
1739 (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
1740 tmp = keytab_name_req;
1741 goto resolve;
1744 if (keytab_name_req[0] != '/') {
1745 ret = KRB5_KT_BADNAME;
1746 goto out;
1749 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1750 if (!tmp) {
1751 ret = ENOMEM;
1752 goto out;
1755 goto resolve;
1758 /* we need to handle more complex keytab_strings, like:
1759 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1761 ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1762 if (ret) {
1763 goto out;
1766 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1768 tmp = talloc_strdup(mem_ctx, keytab_string);
1769 if (!tmp) {
1770 ret = ENOMEM;
1771 goto out;
1774 if (strncmp(tmp, "ANY:", 4) == 0) {
1775 tmp += 4;
1778 memset(&keytab_string, '\0', sizeof(keytab_string));
1780 while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1781 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1782 found_valid_name = True;
1783 tmp = kt_str;
1784 tmp += 7;
1787 if (strncmp(kt_str, "FILE:", 5) == 0) {
1788 found_valid_name = True;
1789 tmp = kt_str;
1790 tmp += 5;
1793 if (tmp[0] == '/') {
1794 /* Treat as a FILE: keytab definition. */
1795 found_valid_name = true;
1798 if (found_valid_name) {
1799 if (tmp[0] != '/') {
1800 ret = KRB5_KT_BADNAME;
1801 goto out;
1804 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1805 if (!tmp) {
1806 ret = ENOMEM;
1807 goto out;
1809 break;
1813 if (!found_valid_name) {
1814 ret = KRB5_KT_UNKNOWN_TYPE;
1815 goto out;
1818 resolve:
1819 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1820 ret = krb5_kt_resolve(context, tmp, keytab);
1822 out:
1823 TALLOC_FREE(mem_ctx);
1824 return ret;
1827 krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
1828 krb5_context context,
1829 krb5_keytab keytab,
1830 const char **keytab_name)
1832 char keytab_string[MAX_KEYTAB_NAME_LEN];
1833 krb5_error_code ret = 0;
1835 ret = krb5_kt_get_name(context, keytab,
1836 keytab_string, MAX_KEYTAB_NAME_LEN - 2);
1837 if (ret) {
1838 return ret;
1841 *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1842 if (!*keytab_name) {
1843 return ENOMEM;
1846 return ret;
1849 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY)
1850 /**************************************************************
1851 Routine: ads_krb5_get_fwd_ticket
1852 Description:
1853 When a service ticket is flagged as trusted
1854 for delegation we should provide a forwardable
1855 ticket so that the remote host can act on our
1856 behalf. This is done by taking the 2nd forwardable
1857 TGT and storing it in the GSS-API authenticator
1858 "checksum". This routine will populate
1859 the krb5_data authenticator with this TGT.
1860 Parameters:
1861 krb5_context context: The kerberos context for this authentication.
1862 krb5_auth_context: The authentication context.
1863 krb5_creds *credsp: The ticket credentials (AS-REP).
1864 krb5_ccache ccache: The credentials cache.
1865 krb5_data &authenticator: The checksum field that will store the TGT, and
1866 authenticator.data must be freed by the caller.
1868 Returns:
1869 krb5_error_code: 0 if no errors, otherwise set.
1870 **************************************************************/
1872 static krb5_error_code ads_krb5_get_fwd_ticket( krb5_context context,
1873 krb5_auth_context *auth_context,
1874 krb5_creds *credsp,
1875 krb5_ccache ccache,
1876 krb5_data *authenticator)
1878 krb5_data fwdData;
1879 krb5_error_code retval = 0;
1880 char *pChksum = NULL;
1881 char *p = NULL;
1883 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
1884 but still has the symbol */
1885 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
1886 krb5_error_code krb5_auth_con_set_req_cksumtype(
1887 krb5_context context,
1888 krb5_auth_context auth_context,
1889 krb5_cksumtype cksumtype);
1890 #endif
1892 ZERO_STRUCT(fwdData);
1893 ZERO_STRUCTP(authenticator);
1895 retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */
1896 *auth_context, /* Authentication context [in] */
1897 CONST_DISCARD(char *, KRB5_TGS_NAME), /* Ticket service name ("krbtgt") [in] */
1898 credsp->client, /* Client principal for the tgt [in] */
1899 credsp->server, /* Server principal for the tgt [in] */
1900 ccache, /* Credential cache to use for storage [in] */
1901 1, /* Turn on for "Forwardable ticket" [in] */
1902 &fwdData ); /* Resulting response [out] */
1905 if (retval) {
1906 DEBUG(1,("ads_krb5_get_fwd_ticket: krb5_fwd_tgt_creds failed (%s)\n",
1907 error_message(retval)));
1908 goto out;
1911 if ((unsigned int)GSSAPI_CHECKSUM_SIZE + (unsigned int)fwdData.length <
1912 (unsigned int)GSSAPI_CHECKSUM_SIZE) {
1913 retval = EINVAL;
1914 goto out;
1917 /* We're going to allocate a gssChecksum structure with a little
1918 extra data the length of the kerberos credentials length
1919 (APPLICATION 22) so that we can pack it on the end of the structure.
1922 pChksum = (char *)SMB_MALLOC(GSSAPI_CHECKSUM_SIZE + fwdData.length );
1923 if (!pChksum) {
1924 retval = ENOMEM;
1925 goto out;
1928 p = pChksum;
1930 SIVAL(p, 0, GSSAPI_BNDLENGTH);
1931 p += 4;
1933 /* Zero out the bindings fields */
1934 memset(p, '\0', GSSAPI_BNDLENGTH );
1935 p += GSSAPI_BNDLENGTH;
1937 SIVAL(p, 0, GSS_C_DELEG_FLAG );
1938 p += 4;
1939 SSVAL(p, 0, 1 );
1940 p += 2;
1941 SSVAL(p, 0, fwdData.length );
1942 p += 2;
1944 /* Migrate the kerberos KRB_CRED data to the checksum delegation */
1945 memcpy(p, fwdData.data, fwdData.length );
1946 p += fwdData.length;
1948 /* We need to do this in order to allow our GSS-API */
1949 retval = krb5_auth_con_set_req_cksumtype( context, *auth_context, GSSAPI_CHECKSUM );
1950 if (retval) {
1951 goto out;
1954 /* We now have a service ticket, now turn it into an AP-REQ. */
1955 authenticator->length = fwdData.length + GSSAPI_CHECKSUM_SIZE;
1957 /* Caller should call free() when they're done with this. */
1958 authenticator->data = (char *)pChksum;
1960 out:
1962 /* Remove that input data, we never needed it anyway. */
1963 if (fwdData.length > 0) {
1964 krb5_free_data_contents( context, &fwdData );
1967 return retval;
1969 #endif
1971 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1972 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1973 defined(HAVE_KRB5_GET_CREDS)
1974 static krb5_error_code smb_krb5_get_credentials_for_user_opt(krb5_context context,
1975 krb5_ccache ccache,
1976 krb5_principal me,
1977 krb5_principal server,
1978 krb5_principal impersonate_princ,
1979 krb5_creds **out_creds)
1981 krb5_error_code ret;
1982 krb5_get_creds_opt opt;
1984 ret = krb5_get_creds_opt_alloc(context, &opt);
1985 if (ret) {
1986 goto done;
1988 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
1990 if (impersonate_princ) {
1991 ret = krb5_get_creds_opt_set_impersonate(context, opt,
1992 impersonate_princ);
1993 if (ret) {
1994 goto done;
1998 ret = krb5_get_creds(context, opt, ccache, server, out_creds);
1999 if (ret) {
2000 goto done;
2003 done:
2004 if (opt) {
2005 krb5_get_creds_opt_free(context, opt);
2007 return ret;
2009 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
2011 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
2012 static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context,
2013 krb5_ccache ccache,
2014 krb5_principal me,
2015 krb5_principal server,
2016 krb5_principal impersonate_princ,
2017 krb5_creds **out_creds)
2019 krb5_error_code ret;
2020 krb5_creds in_creds;
2022 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
2023 krb5_error_code KRB5_CALLCONV
2024 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
2025 krb5_ccache ccache, krb5_creds *in_creds,
2026 krb5_data *subject_cert,
2027 krb5_creds **out_creds);
2028 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
2030 ZERO_STRUCT(in_creds);
2032 if (impersonate_princ) {
2034 in_creds.server = me;
2035 in_creds.client = impersonate_princ;
2037 ret = krb5_get_credentials_for_user(context,
2038 0, /* krb5_flags options */
2039 ccache,
2040 &in_creds,
2041 NULL, /* krb5_data *subject_cert */
2042 out_creds);
2043 } else {
2044 in_creds.client = me;
2045 in_creds.server = server;
2047 ret = krb5_get_credentials(context, 0, ccache,
2048 &in_creds, out_creds);
2051 return ret;
2053 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
2056 * smb_krb5_get_credentials
2058 * @brief Get krb5 credentials for a server
2060 * @param[in] context An initialized krb5_context
2061 * @param[in] ccache An initialized krb5_ccache
2062 * @param[in] me The krb5_principal of the caller
2063 * @param[in] server The krb5_principal of the requested service
2064 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
2065 * @param[out] out_creds The returned krb5_creds structure
2066 * @return krb5_error_code
2069 krb5_error_code smb_krb5_get_credentials(krb5_context context,
2070 krb5_ccache ccache,
2071 krb5_principal me,
2072 krb5_principal server,
2073 krb5_principal impersonate_princ,
2074 krb5_creds **out_creds)
2076 krb5_error_code ret;
2077 krb5_creds *creds = NULL;
2079 *out_creds = NULL;
2081 if (impersonate_princ) {
2082 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
2083 ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds);
2084 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
2085 ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds);
2086 #else
2087 ret = ENOTSUP;
2088 #endif
2089 } else {
2090 krb5_creds in_creds;
2092 ZERO_STRUCT(in_creds);
2094 in_creds.client = me;
2095 in_creds.server = server;
2097 ret = krb5_get_credentials(context, 0, ccache,
2098 &in_creds, &creds);
2100 if (ret) {
2101 goto done;
2104 ret = krb5_cc_store_cred(context, ccache, creds);
2105 if (ret) {
2106 goto done;
2109 if (out_creds) {
2110 *out_creds = creds;
2113 done:
2114 if (creds && ret) {
2115 krb5_free_creds(context, creds);
2118 return ret;
2122 * smb_krb5_get_creds
2124 * @brief Get krb5 credentials for a server
2126 * @param[in] server_s The string name of the service
2127 * @param[in] time_offset The offset to the KDCs time in seconds (optional)
2128 * @param[in] cc The krb5 credential cache string name (optional)
2129 * @param[in] impersonate_princ_s The string principal name to impersonate (optional)
2130 * @param[out] creds_p The returned krb5_creds structure
2131 * @return krb5_error_code
2134 krb5_error_code smb_krb5_get_creds(const char *server_s,
2135 time_t time_offset,
2136 const char *cc,
2137 const char *impersonate_princ_s,
2138 krb5_creds **creds_p)
2140 krb5_error_code ret;
2141 krb5_context context = NULL;
2142 krb5_principal me = NULL;
2143 krb5_principal server = NULL;
2144 krb5_principal impersonate_princ = NULL;
2145 krb5_creds *creds = NULL;
2146 krb5_ccache ccache = NULL;
2148 *creds_p = NULL;
2150 initialize_krb5_error_table();
2151 ret = krb5_init_context(&context);
2152 if (ret) {
2153 goto done;
2156 if (time_offset != 0) {
2157 krb5_set_real_time(context, time(NULL) + time_offset, 0);
2160 ret = krb5_cc_resolve(context, cc ? cc :
2161 krb5_cc_default_name(context), &ccache);
2162 if (ret) {
2163 goto done;
2166 ret = krb5_cc_get_principal(context, ccache, &me);
2167 if (ret) {
2168 goto done;
2171 ret = smb_krb5_parse_name(context, server_s, &server);
2172 if (ret) {
2173 goto done;
2176 if (impersonate_princ_s) {
2177 ret = smb_krb5_parse_name(context, impersonate_princ_s,
2178 &impersonate_princ);
2179 if (ret) {
2180 goto done;
2184 ret = smb_krb5_get_credentials(context, ccache,
2185 me, server, impersonate_princ,
2186 &creds);
2187 if (ret) {
2188 goto done;
2191 ret = krb5_cc_store_cred(context, ccache, creds);
2192 if (ret) {
2193 goto done;
2196 if (creds_p) {
2197 *creds_p = creds;
2200 DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n",
2201 server_s));
2203 if (impersonate_princ_s) {
2204 char *client = NULL;
2206 ret = smb_krb5_unparse_name(talloc_tos(), context, creds->client, &client);
2207 if (ret) {
2208 goto done;
2210 DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n",
2211 client));
2212 TALLOC_FREE(client);
2215 done:
2216 if (!context) {
2217 return ret;
2220 if (creds && ret) {
2221 krb5_free_creds(context, creds);
2223 if (server) {
2224 krb5_free_principal(context, server);
2226 if (me) {
2227 krb5_free_principal(context, me);
2229 if (impersonate_princ) {
2230 krb5_free_principal(context, impersonate_princ);
2232 if (ccache) {
2233 krb5_cc_close(context, ccache);
2235 krb5_free_context(context);
2237 return ret;
2241 * smb_krb5_principal_get_realm
2243 * @brief Get realm of a principal
2245 * @param[in] context The krb5_context
2246 * @param[in] principal The principal
2247 * @return pointer to the realm
2251 char *smb_krb5_principal_get_realm(krb5_context context,
2252 krb5_principal principal)
2254 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2255 return krb5_principal_get_realm(context, principal);
2256 #elif defined(krb5_princ_realm) /* MIT */
2257 krb5_data *realm;
2258 realm = krb5_princ_realm(context, principal);
2259 return (char *)realm->data;
2260 #else
2261 return NULL;
2262 #endif
2265 #else /* HAVE_KRB5 */
2266 /* this saves a few linking headaches */
2267 int cli_krb5_get_ticket(const char *principal, time_t time_offset,
2268 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
2269 const char *ccname, time_t *tgs_expire,
2270 const char *impersonate_princ_s)
2272 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2273 return 1;
2276 #endif