2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
40 * Take the `body' and encode it into `padata' using the credentials
44 static krb5_error_code
45 make_pa_tgs_req(krb5_context context
,
57 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, body
, &len
, ret
);
61 krb5_abortx(context
, "internal error in ASN.1 encoder");
65 ret
= _krb5_mk_req_internal(context
, &ac
, 0, &in_data
, creds
,
66 &padata
->padata_value
,
67 KRB5_KU_TGS_REQ_AUTH_CKSUM
,
68 KRB5_KU_TGS_REQ_AUTH
);
73 padata
->padata_type
= KRB5_PADATA_TGS_REQ
;
78 * Set the `enc-authorization-data' in `req_body' based on `authdata'
81 static krb5_error_code
82 set_auth_data (krb5_context context
,
83 KDC_REQ_BODY
*req_body
,
84 krb5_authdata
*authdata
,
85 krb5_keyblock
*subkey
)
93 ASN1_MALLOC_ENCODE(AuthorizationData
, buf
, buf_size
, authdata
,
98 krb5_abortx(context
, "internal error in ASN.1 encoder");
100 ALLOC(req_body
->enc_authorization_data
, 1);
101 if (req_body
->enc_authorization_data
== NULL
) {
103 krb5_set_error_message(context
, ENOMEM
,
104 N_("malloc: out of memory", ""));
107 ret
= krb5_crypto_init(context
, subkey
, 0, &crypto
);
110 free (req_body
->enc_authorization_data
);
111 req_body
->enc_authorization_data
= NULL
;
114 krb5_encrypt_EncryptedData(context
,
116 KRB5_KU_TGS_REQ_AUTH_DAT_SUBKEY
,
120 req_body
->enc_authorization_data
);
122 krb5_crypto_destroy(context
, crypto
);
124 req_body
->enc_authorization_data
= NULL
;
130 * Create a tgs-req in `t' with `addresses', `flags', `second_ticket'
131 * (if not-NULL), `in_creds', `krbtgt', and returning the generated
132 * subkey in `subkey'.
135 static krb5_error_code
136 init_tgs_req (krb5_context context
,
138 krb5_addresses
*addresses
,
139 krb5_kdc_flags flags
,
140 Ticket
*second_ticket
,
141 krb5_creds
*in_creds
,
144 const METHOD_DATA
*padata
,
145 krb5_keyblock
**subkey
,
148 krb5_auth_context ac
= NULL
;
149 krb5_error_code ret
= 0;
151 memset(t
, 0, sizeof(*t
));
153 t
->msg_type
= krb_tgs_req
;
154 if (in_creds
->session
.keytype
) {
155 ALLOC_SEQ(&t
->req_body
.etype
, 1);
156 if(t
->req_body
.etype
.val
== NULL
) {
158 krb5_set_error_message(context
, ret
,
159 N_("malloc: out of memory", ""));
162 t
->req_body
.etype
.val
[0] = in_creds
->session
.keytype
;
164 ret
= krb5_init_etype(context
,
165 &t
->req_body
.etype
.len
,
166 &t
->req_body
.etype
.val
,
171 t
->req_body
.addresses
= addresses
;
172 t
->req_body
.kdc_options
= flags
.b
;
173 ret
= copy_Realm(&in_creds
->server
->realm
, &t
->req_body
.realm
);
176 ALLOC(t
->req_body
.sname
, 1);
177 if (t
->req_body
.sname
== NULL
) {
179 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
183 /* some versions of some code might require that the client be
184 present in TGS-REQs, but this is clearly against the spec */
186 ret
= copy_PrincipalName(&in_creds
->server
->name
, t
->req_body
.sname
);
190 /* req_body.till should be NULL if there is no endtime specified,
191 but old MIT code (like DCE secd) doesn't like that */
192 ALLOC(t
->req_body
.till
, 1);
193 if(t
->req_body
.till
== NULL
){
195 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
198 *t
->req_body
.till
= in_creds
->times
.endtime
;
200 t
->req_body
.nonce
= nonce
;
202 ALLOC(t
->req_body
.additional_tickets
, 1);
203 if (t
->req_body
.additional_tickets
== NULL
) {
205 krb5_set_error_message(context
, ret
,
206 N_("malloc: out of memory", ""));
209 ALLOC_SEQ(t
->req_body
.additional_tickets
, 1);
210 if (t
->req_body
.additional_tickets
->val
== NULL
) {
212 krb5_set_error_message(context
, ret
,
213 N_("malloc: out of memory", ""));
216 ret
= copy_Ticket(second_ticket
, t
->req_body
.additional_tickets
->val
);
221 if (t
->padata
== NULL
) {
223 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
226 ALLOC_SEQ(t
->padata
, 1 + padata
->len
);
227 if (t
->padata
->val
== NULL
) {
229 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
234 for (i
= 0; i
< padata
->len
; i
++) {
235 ret
= copy_PA_DATA(&padata
->val
[i
], &t
->padata
->val
[i
+ 1]);
237 krb5_set_error_message(context
, ret
,
238 N_("malloc: out of memory", ""));
244 ret
= krb5_auth_con_init(context
, &ac
);
248 ret
= krb5_auth_con_generatelocalsubkey(context
, ac
, &krbtgt
->session
);
252 ret
= set_auth_data (context
, &t
->req_body
, &in_creds
->authdata
,
257 ret
= make_pa_tgs_req(context
,
265 ret
= krb5_auth_con_getlocalsubkey(context
, ac
, subkey
);
271 krb5_auth_con_free(context
, ac
);
273 t
->req_body
.addresses
= NULL
;
280 _krb5_get_krbtgt(krb5_context context
,
288 memset(&tmp_cred
, 0, sizeof(tmp_cred
));
290 ret
= krb5_cc_get_principal(context
, id
, &tmp_cred
.client
);
294 ret
= krb5_make_principal(context
,
301 krb5_free_principal(context
, tmp_cred
.client
);
304 ret
= krb5_get_credentials(context
,
309 krb5_free_principal(context
, tmp_cred
.client
);
310 krb5_free_principal(context
, tmp_cred
.server
);
316 /* DCE compatible decrypt proc */
317 static krb5_error_code KRB5_CALLCONV
318 decrypt_tkt_with_subkey (krb5_context context
,
320 krb5_key_usage usage
,
321 krb5_const_pointer subkey
,
322 krb5_kdc_rep
*dec_rep
)
332 * start out with trying with subkey if we have one
335 ret
= krb5_crypto_init(context
, subkey
, 0, &crypto
);
338 ret
= krb5_decrypt_EncryptedData (context
,
340 KRB5_KU_TGS_REP_ENC_PART_SUB_KEY
,
341 &dec_rep
->kdc_rep
.enc_part
,
343 krb5_crypto_destroy(context
, crypto
);
345 if (subkey
== NULL
|| ret
) {
346 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
349 ret
= krb5_decrypt_EncryptedData (context
,
351 KRB5_KU_TGS_REP_ENC_PART_SESSION
,
352 &dec_rep
->kdc_rep
.enc_part
,
354 krb5_crypto_destroy(context
, crypto
);
359 ret
= decode_EncASRepPart(data
.data
,
364 ret
= decode_EncTGSRepPart(data
.data
,
369 krb5_set_error_message(context
, ret
,
370 N_("Failed to decode encpart in ticket", ""));
371 krb5_data_free (&data
);
375 static krb5_error_code
376 get_cred_kdc(krb5_context context
,
378 krb5_kdc_flags flags
,
379 krb5_addresses
*addresses
,
380 krb5_creds
*in_creds
,
382 krb5_principal impersonate_principal
,
383 Ticket
*second_ticket
,
384 krb5_creds
*out_creds
)
393 krb5_keyblock
*subkey
= NULL
;
395 Ticket second_ticket_data
;
398 krb5_data_zero(&resp
);
399 krb5_data_zero(&enc
);
403 krb5_generate_random_block(&nonce
, sizeof(nonce
));
406 if(flags
.b
.enc_tkt_in_skey
&& second_ticket
== NULL
){
407 ret
= decode_Ticket(in_creds
->second_ticket
.data
,
408 in_creds
->second_ticket
.length
,
409 &second_ticket_data
, &len
);
412 second_ticket
= &second_ticket_data
;
416 if (impersonate_principal
) {
423 self
.name
= impersonate_principal
->name
;
424 self
.realm
= impersonate_principal
->realm
;
425 self
.auth
= estrdup("Kerberos");
427 ret
= _krb5_s4u2self_to_checksumdata(context
, &self
, &data
);
433 ret
= krb5_crypto_init(context
, &krbtgt
->session
, 0, &crypto
);
436 krb5_data_free(&data
);
440 ret
= krb5_create_checksum(context
,
447 krb5_crypto_destroy(context
, crypto
);
448 krb5_data_free(&data
);
454 ASN1_MALLOC_ENCODE(PA_S4U2Self
, buf
, len
, &self
, &size
, ret
);
456 free_Checksum(&self
.cksum
);
460 krb5_abortx(context
, "internal asn1 error");
462 ret
= krb5_padata_add(context
, &padata
, KRB5_PADATA_FOR_USER
, buf
, len
);
467 ret
= init_tgs_req (context
,
481 ASN1_MALLOC_ENCODE(TGS_REQ
, enc
.data
, enc
.length
, &req
, &len
, ret
);
484 if(enc
.length
!= len
)
485 krb5_abortx(context
, "internal error in ASN.1 encoder");
487 /* don't free addresses */
488 req
.req_body
.addresses
= NULL
;
495 krb5_sendto_ctx stctx
;
496 ret
= krb5_sendto_ctx_alloc(context
, &stctx
);
499 krb5_sendto_ctx_set_func(stctx
, _krb5_kdc_retry
, NULL
);
501 ret
= krb5_sendto_context (context
, stctx
, &enc
,
502 krbtgt
->server
->name
.name_string
.val
[1],
504 krb5_sendto_ctx_free(context
, stctx
);
509 memset(&rep
, 0, sizeof(rep
));
510 if(decode_TGS_REP(resp
.data
, resp
.length
, &rep
.kdc_rep
, &len
) == 0) {
513 ret
= krb5_copy_principal(context
,
518 ret
= krb5_copy_principal(context
,
523 /* this should go someplace else */
524 out_creds
->times
.endtime
= in_creds
->times
.endtime
;
526 /* XXX should do better testing */
527 if (flags
.b
.constrained_delegation
|| impersonate_principal
)
528 eflags
|= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH
;
530 ret
= _krb5_extract_ticket(context
,
539 decrypt_tkt_with_subkey
,
542 krb5_free_kdc_rep(context
, &rep
);
543 } else if(krb5_rd_error(context
, &resp
, &error
) == 0) {
544 ret
= krb5_error_from_rd_error(context
, &error
, in_creds
);
545 krb5_free_error_contents(context
, &error
);
546 } else if(resp
.length
> 0 && ((char*)resp
.data
)[0] == 4) {
547 ret
= KRB5KRB_AP_ERR_V4_REPLY
;
548 krb5_clear_error_message(context
);
550 ret
= KRB5KRB_AP_ERR_MSG_TYPE
;
551 krb5_clear_error_message(context
);
555 if (second_ticket
== &second_ticket_data
)
556 free_Ticket(&second_ticket_data
);
557 free_METHOD_DATA(&padata
);
558 krb5_data_free(&resp
);
559 krb5_data_free(&enc
);
561 krb5_free_keyblock(context
, subkey
);
567 * same as above, just get local addresses first if the krbtgt have
568 * them and the realm is not addressless
571 static krb5_error_code
572 get_cred_kdc_address(krb5_context context
,
574 krb5_kdc_flags flags
,
575 krb5_addresses
*addrs
,
576 krb5_creds
*in_creds
,
578 krb5_principal impersonate_principal
,
579 Ticket
*second_ticket
,
580 krb5_creds
*out_creds
)
583 krb5_addresses addresses
= { 0, NULL
};
586 * Inherit the address-ness of the krbtgt if the address is not
590 if (addrs
== NULL
&& krbtgt
->addresses
.len
!= 0) {
593 krb5_appdefault_boolean(context
, NULL
, krbtgt
->server
->realm
,
594 "no-addresses", FALSE
, &noaddr
);
597 krb5_get_all_client_addrs(context
, &addresses
);
598 /* XXX this sucks. */
600 if(addresses
.len
== 0)
604 ret
= get_cred_kdc(context
, id
, flags
, addrs
, in_creds
,
605 krbtgt
, impersonate_principal
,
606 second_ticket
, out_creds
);
607 krb5_free_addresses(context
, &addresses
);
611 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
612 krb5_get_kdc_cred(krb5_context context
,
614 krb5_kdc_flags flags
,
615 krb5_addresses
*addresses
,
616 Ticket
*second_ticket
,
617 krb5_creds
*in_creds
,
618 krb5_creds
**out_creds
624 *out_creds
= calloc(1, sizeof(**out_creds
));
625 if(*out_creds
== NULL
) {
626 krb5_set_error_message(context
, ENOMEM
,
627 N_("malloc: out of memory", ""));
630 ret
= _krb5_get_krbtgt (context
,
632 in_creds
->server
->realm
,
639 ret
= get_cred_kdc(context
, id
, flags
, addresses
,
640 in_creds
, krbtgt
, NULL
, NULL
, *out_creds
);
641 krb5_free_creds (context
, krbtgt
);
650 not_found(krb5_context context
, krb5_const_principal p
, krb5_error_code code
)
655 ret
= krb5_unparse_name(context
, p
, &str
);
657 krb5_clear_error_message(context
);
660 krb5_set_error_message(context
, code
,
661 N_("Matching credential (%s) not found", ""), str
);
666 static krb5_error_code
667 find_cred(krb5_context context
,
669 krb5_principal server
,
671 krb5_creds
*out_creds
)
676 krb5_cc_clear_mcred(&mcreds
);
677 mcreds
.server
= server
;
678 ret
= krb5_cc_retrieve_cred(context
, id
, KRB5_TC_DONT_MATCH_REALM
,
682 while(tgts
&& *tgts
){
683 if(krb5_compare_creds(context
, KRB5_TC_DONT_MATCH_REALM
,
685 ret
= krb5_copy_creds_contents(context
, *tgts
, out_creds
);
690 return not_found(context
, server
, KRB5_CC_NOTFOUND
);
693 static krb5_error_code
694 add_cred(krb5_context context
, krb5_creds
const *tkt
, krb5_creds
***tgts
)
698 krb5_creds
**tmp
= *tgts
;
700 for(i
= 0; tmp
&& tmp
[i
]; i
++); /* XXX */
701 tmp
= realloc(tmp
, (i
+2)*sizeof(*tmp
));
703 krb5_set_error_message(context
, ENOMEM
,
704 N_("malloc: out of memory", ""));
708 ret
= krb5_copy_creds(context
, tkt
, &tmp
[i
]);
715 creds = cc_get_cred(server)
716 if(creds) return creds
717 tgt = cc_get_cred(krbtgt/server_realm@any_realm)
719 return get_cred_tgt(server, tgt)
720 if(client_realm == server_realm)
722 tgt = get_cred(krbtgt/server_realm@client_realm)
723 while(tgt_inst != server_realm)
724 tgt = get_cred(krbtgt/server_realm@tgt_inst)
725 return get_cred_tgt(server, tgt)
728 static krb5_error_code
729 get_cred_kdc_capath(krb5_context context
,
730 krb5_kdc_flags flags
,
732 krb5_creds
*in_creds
,
733 krb5_principal impersonate_principal
,
734 Ticket
*second_ticket
,
735 krb5_creds
**out_creds
,
736 krb5_creds
***ret_tgts
)
739 krb5_creds
*tgt
, tmp_creds
;
740 krb5_const_realm client_realm
, server_realm
, try_realm
;
741 int ok_as_delegate
= 1;
745 client_realm
= krb5_principal_get_realm(context
, in_creds
->client
);
746 server_realm
= krb5_principal_get_realm(context
, in_creds
->server
);
747 memset(&tmp_creds
, 0, sizeof(tmp_creds
));
748 ret
= krb5_copy_principal(context
, in_creds
->client
, &tmp_creds
.client
);
752 try_realm
= krb5_config_get_string(context
, NULL
, "capaths",
753 client_realm
, server_realm
, NULL
);
754 if (try_realm
== NULL
)
755 try_realm
= client_realm
;
757 ret
= krb5_make_principal(context
,
764 krb5_free_principal(context
, tmp_creds
.client
);
770 ret
= find_cred(context
, ccache
, tmp_creds
.server
,
773 if (try_realm
!= client_realm
)
774 ok_as_delegate
= tgts
.flags
.b
.ok_as_delegate
;
776 *out_creds
= calloc(1, sizeof(**out_creds
));
777 if(*out_creds
== NULL
) {
779 krb5_set_error_message(context
, ret
,
780 N_("malloc: out of memory", ""));
782 ret
= get_cred_kdc_address(context
, ccache
, flags
, NULL
,
784 impersonate_principal
,
790 } else if (ok_as_delegate
== 0)
791 (*out_creds
)->flags
.b
.ok_as_delegate
= 0;
793 krb5_free_cred_contents(context
, &tgts
);
794 krb5_free_principal(context
, tmp_creds
.server
);
795 krb5_free_principal(context
, tmp_creds
.client
);
799 if(krb5_realm_compare(context
, in_creds
->client
, in_creds
->server
))
800 return not_found(context
, in_creds
->server
, KRB5_CC_NOTFOUND
);
802 /* XXX this can loop forever */
804 heim_general_string tgt_inst
;
806 ret
= get_cred_kdc_capath(context
, flags
, ccache
, &tmp_creds
,
807 NULL
, NULL
, &tgt
, ret_tgts
);
809 krb5_free_principal(context
, tmp_creds
.server
);
810 krb5_free_principal(context
, tmp_creds
.client
);
814 * if either of the chain or the ok_as_delegate was stripped
815 * by the kdc, make sure we strip it too.
817 if (ok_as_delegate
== 0 || tgt
->flags
.b
.ok_as_delegate
== 0) {
819 tgt
->flags
.b
.ok_as_delegate
= 0;
822 ret
= add_cred(context
, tgt
, ret_tgts
);
824 krb5_free_principal(context
, tmp_creds
.server
);
825 krb5_free_principal(context
, tmp_creds
.client
);
828 tgt_inst
= tgt
->server
->name
.name_string
.val
[1];
829 if(strcmp(tgt_inst
, server_realm
) == 0)
831 krb5_free_principal(context
, tmp_creds
.server
);
832 ret
= krb5_make_principal(context
, &tmp_creds
.server
,
833 tgt_inst
, KRB5_TGS_NAME
, server_realm
, NULL
);
835 krb5_free_principal(context
, tmp_creds
.server
);
836 krb5_free_principal(context
, tmp_creds
.client
);
839 ret
= krb5_free_creds(context
, tgt
);
841 krb5_free_principal(context
, tmp_creds
.server
);
842 krb5_free_principal(context
, tmp_creds
.client
);
847 krb5_free_principal(context
, tmp_creds
.server
);
848 krb5_free_principal(context
, tmp_creds
.client
);
849 *out_creds
= calloc(1, sizeof(**out_creds
));
850 if(*out_creds
== NULL
) {
852 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
854 ret
= get_cred_kdc_address (context
, ccache
, flags
, NULL
,
855 in_creds
, tgt
, impersonate_principal
,
856 second_ticket
, *out_creds
);
862 krb5_free_creds(context
, tgt
);
866 static krb5_error_code
867 get_cred_kdc_referral(krb5_context context
,
868 krb5_kdc_flags flags
,
870 krb5_creds
*in_creds
,
871 krb5_principal impersonate_principal
,
872 Ticket
*second_ticket
,
873 krb5_creds
**out_creds
,
874 krb5_creds
***ret_tgts
)
876 krb5_const_realm client_realm
;
878 krb5_creds tgt
, referral
, ticket
;
880 int ok_as_delegate
= 1;
882 if (in_creds
->server
->name
.name_string
.len
< 2 && !flags
.b
.canonicalize
) {
883 krb5_set_error_message(context
, KRB5KDC_ERR_PATH_NOT_ACCEPTED
,
884 N_("Name too short to do referals, skipping", ""));
885 return KRB5KDC_ERR_PATH_NOT_ACCEPTED
;
888 memset(&tgt
, 0, sizeof(tgt
));
889 memset(&ticket
, 0, sizeof(ticket
));
891 flags
.b
.canonicalize
= 1;
895 client_realm
= krb5_principal_get_realm(context
, in_creds
->client
);
897 /* find tgt for the clients base realm */
899 krb5_principal tgtname
;
901 ret
= krb5_make_principal(context
, &tgtname
,
909 ret
= find_cred(context
, ccache
, tgtname
, *ret_tgts
, &tgt
);
910 krb5_free_principal(context
, tgtname
);
915 referral
= *in_creds
;
916 ret
= krb5_copy_principal(context
, in_creds
->server
, &referral
.server
);
918 krb5_free_cred_contents(context
, &tgt
);
921 ret
= krb5_principal_set_realm(context
, referral
.server
, client_realm
);
923 krb5_free_cred_contents(context
, &tgt
);
924 krb5_free_principal(context
, referral
.server
);
928 while (loop
++ < 17) {
929 krb5_creds
**tickets
;
931 char *referral_realm
;
933 /* Use cache if we are not doing impersonation or contrainte deleg */
934 if (impersonate_principal
== NULL
|| flags
.b
.constrained_delegation
) {
935 krb5_cc_clear_mcred(&mcreds
);
936 mcreds
.server
= referral
.server
;
937 ret
= krb5_cc_retrieve_cred(context
, ccache
, 0, &mcreds
, &ticket
);
942 ret
= get_cred_kdc_address(context
, ccache
, flags
, NULL
,
943 &referral
, &tgt
, impersonate_principal
,
944 second_ticket
, &ticket
);
949 /* Did we get the right ticket ? */
950 if (krb5_principal_compare_any_realm(context
,
955 if (!krb5_principal_is_krbtgt(context
, ticket
.server
)) {
956 krb5_set_error_message(context
, KRB5KRB_AP_ERR_NOT_US
,
957 N_("Got back an non krbtgt "
958 "ticket referrals", ""));
959 ret
= KRB5KRB_AP_ERR_NOT_US
;
963 referral_realm
= ticket
.server
->name
.name_string
.val
[1];
965 /* check that there are no referrals loops */
968 krb5_cc_clear_mcred(&mcreds
);
969 mcreds
.server
= ticket
.server
;
971 while(tickets
&& *tickets
){
972 if(krb5_compare_creds(context
,
973 KRB5_TC_DONT_MATCH_REALM
,
977 krb5_set_error_message(context
, KRB5_GET_IN_TKT_LOOP
,
978 N_("Referral from %s "
979 "loops back to realm %s", ""),
982 ret
= KRB5_GET_IN_TKT_LOOP
;
989 * if either of the chain or the ok_as_delegate was stripped
990 * by the kdc, make sure we strip it too.
993 if (ok_as_delegate
== 0 || ticket
.flags
.b
.ok_as_delegate
== 0) {
995 ticket
.flags
.b
.ok_as_delegate
= 0;
998 ret
= add_cred(context
, &ticket
, ret_tgts
);
1002 /* try realm in the referral */
1003 ret
= krb5_principal_set_realm(context
,
1006 krb5_free_cred_contents(context
, &tgt
);
1008 memset(&ticket
, 0, sizeof(ticket
));
1013 ret
= krb5_copy_creds(context
, &ticket
, out_creds
);
1016 krb5_free_principal(context
, referral
.server
);
1017 krb5_free_cred_contents(context
, &tgt
);
1018 krb5_free_cred_contents(context
, &ticket
);
1024 * Glue function between referrals version and old client chasing
1029 _krb5_get_cred_kdc_any(krb5_context context
,
1030 krb5_kdc_flags flags
,
1032 krb5_creds
*in_creds
,
1033 krb5_principal impersonate_principal
,
1034 Ticket
*second_ticket
,
1035 krb5_creds
**out_creds
,
1036 krb5_creds
***ret_tgts
)
1038 krb5_error_code ret
;
1041 ret
= krb5_cc_get_kdc_offset(context
, ccache
, &offset
);
1043 context
->kdc_sec_offset
= offset
;
1044 context
->kdc_usec_offset
= 0;
1047 ret
= get_cred_kdc_referral(context
,
1051 impersonate_principal
,
1055 if (ret
== 0 || flags
.b
.canonicalize
)
1057 return get_cred_kdc_capath(context
,
1061 impersonate_principal
,
1068 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1069 krb5_get_credentials_with_flags(krb5_context context
,
1071 krb5_kdc_flags flags
,
1073 krb5_creds
*in_creds
,
1074 krb5_creds
**out_creds
)
1076 krb5_error_code ret
;
1078 krb5_creds
*res_creds
;
1081 if (in_creds
->session
.keytype
) {
1082 ret
= krb5_enctype_valid(context
, in_creds
->session
.keytype
);
1088 res_creds
= calloc(1, sizeof(*res_creds
));
1089 if (res_creds
== NULL
) {
1090 krb5_set_error_message(context
, ENOMEM
,
1091 N_("malloc: out of memory", ""));
1095 if (in_creds
->session
.keytype
)
1096 options
|= KRB5_TC_MATCH_KEYTYPE
;
1099 * If we got a credential, check if credential is expired before
1102 ret
= krb5_cc_retrieve_cred(context
,
1104 in_creds
->session
.keytype
?
1105 KRB5_TC_MATCH_KEYTYPE
: 0,
1106 in_creds
, res_creds
);
1108 * If we got a credential, check if credential is expired before
1109 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1112 krb5_timestamp timeret
;
1114 /* If expired ok, don't bother checking */
1115 if(options
& KRB5_GC_EXPIRED_OK
) {
1116 *out_creds
= res_creds
;
1120 krb5_timeofday(context
, &timeret
);
1121 if(res_creds
->times
.endtime
> timeret
) {
1122 *out_creds
= res_creds
;
1125 if(options
& KRB5_GC_CACHED
)
1126 krb5_cc_remove_cred(context
, ccache
, 0, res_creds
);
1128 } else if(ret
!= KRB5_CC_END
) {
1133 if(options
& KRB5_GC_CACHED
)
1134 return not_found(context
, in_creds
->server
, KRB5_CC_NOTFOUND
);
1136 if(options
& KRB5_GC_USER_USER
)
1137 flags
.b
.enc_tkt_in_skey
= 1;
1138 if (flags
.b
.enc_tkt_in_skey
)
1139 options
|= KRB5_GC_NO_STORE
;
1142 ret
= _krb5_get_cred_kdc_any(context
, flags
, ccache
,
1143 in_creds
, NULL
, NULL
, out_creds
, &tgts
);
1144 for(i
= 0; tgts
&& tgts
[i
]; i
++) {
1145 krb5_cc_store_cred(context
, ccache
, tgts
[i
]);
1146 krb5_free_creds(context
, tgts
[i
]);
1149 if(ret
== 0 && (options
& KRB5_GC_NO_STORE
) == 0)
1150 krb5_cc_store_cred(context
, ccache
, *out_creds
);
1154 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1155 krb5_get_credentials(krb5_context context
,
1158 krb5_creds
*in_creds
,
1159 krb5_creds
**out_creds
)
1161 krb5_kdc_flags flags
;
1163 return krb5_get_credentials_with_flags(context
, options
, flags
,
1164 ccache
, in_creds
, out_creds
);
1167 struct krb5_get_creds_opt_data
{
1168 krb5_principal self
;
1170 krb5_enctype enctype
;
1175 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1176 krb5_get_creds_opt_alloc(krb5_context context
, krb5_get_creds_opt
*opt
)
1178 *opt
= calloc(1, sizeof(**opt
));
1180 krb5_set_error_message(context
, ENOMEM
,
1181 N_("malloc: out of memory", ""));
1187 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1188 krb5_get_creds_opt_free(krb5_context context
, krb5_get_creds_opt opt
)
1191 krb5_free_principal(context
, opt
->self
);
1193 free_Ticket(opt
->ticket
);
1196 memset(opt
, 0, sizeof(*opt
));
1200 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1201 krb5_get_creds_opt_set_options(krb5_context context
,
1202 krb5_get_creds_opt opt
,
1205 opt
->options
= options
;
1208 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1209 krb5_get_creds_opt_add_options(krb5_context context
,
1210 krb5_get_creds_opt opt
,
1213 opt
->options
|= options
;
1216 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1217 krb5_get_creds_opt_set_enctype(krb5_context context
,
1218 krb5_get_creds_opt opt
,
1219 krb5_enctype enctype
)
1221 opt
->enctype
= enctype
;
1224 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1225 krb5_get_creds_opt_set_impersonate(krb5_context context
,
1226 krb5_get_creds_opt opt
,
1227 krb5_const_principal self
)
1230 krb5_free_principal(context
, opt
->self
);
1231 return krb5_copy_principal(context
, self
, &opt
->self
);
1234 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1235 krb5_get_creds_opt_set_ticket(krb5_context context
,
1236 krb5_get_creds_opt opt
,
1237 const Ticket
*ticket
)
1240 free_Ticket(opt
->ticket
);
1245 krb5_error_code ret
;
1247 opt
->ticket
= malloc(sizeof(*ticket
));
1248 if (opt
->ticket
== NULL
) {
1249 krb5_set_error_message(context
, ENOMEM
,
1250 N_("malloc: out of memory", ""));
1253 ret
= copy_Ticket(ticket
, opt
->ticket
);
1257 krb5_set_error_message(context
, ret
,
1258 N_("malloc: out of memory", ""));
1267 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1268 krb5_get_creds(krb5_context context
,
1269 krb5_get_creds_opt opt
,
1271 krb5_const_principal inprinc
,
1272 krb5_creds
**out_creds
)
1274 krb5_kdc_flags flags
;
1276 krb5_creds in_creds
;
1277 krb5_error_code ret
;
1279 krb5_creds
*res_creds
;
1282 if (opt
&& opt
->enctype
) {
1283 ret
= krb5_enctype_valid(context
, opt
->enctype
);
1288 memset(&in_creds
, 0, sizeof(in_creds
));
1289 in_creds
.server
= rk_UNCONST(inprinc
);
1291 ret
= krb5_cc_get_principal(context
, ccache
, &in_creds
.client
);
1296 options
= opt
->options
;
1302 res_creds
= calloc(1, sizeof(*res_creds
));
1303 if (res_creds
== NULL
) {
1304 krb5_free_principal(context
, in_creds
.client
);
1305 krb5_set_error_message(context
, ENOMEM
,
1306 N_("malloc: out of memory", ""));
1310 if (opt
&& opt
->enctype
) {
1311 in_creds
.session
.keytype
= opt
->enctype
;
1312 options
|= KRB5_TC_MATCH_KEYTYPE
;
1316 * If we got a credential, check if credential is expired before
1319 ret
= krb5_cc_retrieve_cred(context
,
1321 options
& KRB5_TC_MATCH_KEYTYPE
,
1322 &in_creds
, res_creds
);
1324 * If we got a credential, check if credential is expired before
1325 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1328 krb5_timestamp timeret
;
1330 /* If expired ok, don't bother checking */
1331 if(options
& KRB5_GC_EXPIRED_OK
) {
1332 *out_creds
= res_creds
;
1333 krb5_free_principal(context
, in_creds
.client
);
1337 krb5_timeofday(context
, &timeret
);
1338 if(res_creds
->times
.endtime
> timeret
) {
1339 *out_creds
= res_creds
;
1340 krb5_free_principal(context
, in_creds
.client
);
1343 if(options
& KRB5_GC_CACHED
)
1344 krb5_cc_remove_cred(context
, ccache
, 0, res_creds
);
1346 } else if(ret
!= KRB5_CC_END
) {
1348 krb5_free_principal(context
, in_creds
.client
);
1352 if(options
& KRB5_GC_CACHED
) {
1353 krb5_free_principal(context
, in_creds
.client
);
1354 ret
= not_found(context
, in_creds
.server
, KRB5_CC_NOTFOUND
);
1357 if(options
& KRB5_GC_USER_USER
) {
1358 flags
.b
.enc_tkt_in_skey
= 1;
1359 options
|= KRB5_GC_NO_STORE
;
1361 if (options
& KRB5_GC_FORWARDABLE
)
1362 flags
.b
.forwardable
= 1;
1363 if (options
& KRB5_GC_NO_TRANSIT_CHECK
)
1364 flags
.b
.disable_transited_check
= 1;
1365 if (options
& KRB5_GC_CONSTRAINED_DELEGATION
) {
1366 flags
.b
.request_anonymous
= 1; /* XXX ARGH confusion */
1367 flags
.b
.constrained_delegation
= 1;
1369 if (options
& KRB5_GC_CANONICALIZE
)
1370 flags
.b
.canonicalize
= 1;
1373 ret
= _krb5_get_cred_kdc_any(context
, flags
, ccache
,
1374 &in_creds
, opt
->self
, opt
->ticket
,
1376 krb5_free_principal(context
, in_creds
.client
);
1377 for(i
= 0; tgts
&& tgts
[i
]; i
++) {
1378 krb5_cc_store_cred(context
, ccache
, tgts
[i
]);
1379 krb5_free_creds(context
, tgts
[i
]);
1382 if(ret
== 0 && (options
& KRB5_GC_NO_STORE
) == 0)
1383 krb5_cc_store_cred(context
, ccache
, *out_creds
);
1386 _krb5_debug(context
, 5, "krb5_get_creds: ret = %d", ret
);
1395 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1396 krb5_get_renewed_creds(krb5_context context
,
1398 krb5_const_principal client
,
1400 const char *in_tkt_service
)
1402 krb5_error_code ret
;
1403 krb5_kdc_flags flags
;
1404 krb5_creds in
, *template, *out
= NULL
;
1406 memset(&in
, 0, sizeof(in
));
1407 memset(creds
, 0, sizeof(*creds
));
1409 ret
= krb5_copy_principal(context
, client
, &in
.client
);
1413 if (in_tkt_service
) {
1414 ret
= krb5_parse_name(context
, in_tkt_service
, &in
.server
);
1416 krb5_free_principal(context
, in
.client
);
1420 const char *realm
= krb5_principal_get_realm(context
, client
);
1422 ret
= krb5_make_principal(context
, &in
.server
, realm
, KRB5_TGS_NAME
,
1425 krb5_free_principal(context
, in
.client
);
1431 flags
.b
.renewable
= flags
.b
.renew
= 1;
1434 * Get template from old credential cache for the same entry, if
1435 * this failes, no worries.
1437 ret
= krb5_get_credentials(context
, KRB5_GC_CACHED
, ccache
, &in
, &template);
1439 flags
.b
.forwardable
= template->flags
.b
.forwardable
;
1440 flags
.b
.proxiable
= template->flags
.b
.proxiable
;
1441 krb5_free_creds (context
, template);
1444 ret
= krb5_get_kdc_cred(context
, ccache
, flags
, NULL
, NULL
, &in
, &out
);
1445 krb5_free_principal(context
, in
.client
);
1446 krb5_free_principal(context
, in
.server
);
1450 ret
= krb5_copy_creds_contents(context
, out
, creds
);
1451 krb5_free_creds(context
, out
);