2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <krb5_locl.h>
38 * Take the `body' and encode it into `padata' using the credentials
42 static krb5_error_code
43 make_pa_tgs_req(krb5_context context
,
55 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, body
, &len
, ret
);
59 krb5_abortx(context
, "internal error in ASN.1 encoder");
63 ret
= _krb5_mk_req_internal(context
, &ac
, 0, &in_data
, creds
,
64 &padata
->padata_value
,
65 KRB5_KU_TGS_REQ_AUTH_CKSUM
,
66 KRB5_KU_TGS_REQ_AUTH
);
71 padata
->padata_type
= KRB5_PADATA_TGS_REQ
;
76 * Set the `enc-authorization-data' in `req_body' based on `authdata'
79 static krb5_error_code
80 set_auth_data (krb5_context context
,
81 KDC_REQ_BODY
*req_body
,
82 krb5_authdata
*authdata
,
83 krb5_keyblock
*subkey
)
91 ASN1_MALLOC_ENCODE(AuthorizationData
, buf
, buf_size
, authdata
,
96 krb5_abortx(context
, "internal error in ASN.1 encoder");
98 ALLOC(req_body
->enc_authorization_data
, 1);
99 if (req_body
->enc_authorization_data
== NULL
) {
101 krb5_set_error_message(context
, ENOMEM
,
102 N_("malloc: out of memory", ""));
105 ret
= krb5_crypto_init(context
, subkey
, 0, &crypto
);
108 free (req_body
->enc_authorization_data
);
109 req_body
->enc_authorization_data
= NULL
;
112 krb5_encrypt_EncryptedData(context
,
114 KRB5_KU_TGS_REQ_AUTH_DAT_SUBKEY
,
118 req_body
->enc_authorization_data
);
120 krb5_crypto_destroy(context
, crypto
);
122 req_body
->enc_authorization_data
= NULL
;
128 * Create a tgs-req in `t' with `addresses', `flags', `second_ticket'
129 * (if not-NULL), `in_creds', `krbtgt', and returning the generated
130 * subkey in `subkey'.
133 static krb5_error_code
134 init_tgs_req (krb5_context context
,
136 krb5_addresses
*addresses
,
137 krb5_kdc_flags flags
,
138 Ticket
*second_ticket
,
139 krb5_creds
*in_creds
,
142 const METHOD_DATA
*padata
,
143 krb5_keyblock
**subkey
,
146 krb5_auth_context ac
= NULL
;
147 krb5_error_code ret
= 0;
149 memset(t
, 0, sizeof(*t
));
151 t
->msg_type
= krb_tgs_req
;
152 if (in_creds
->session
.keytype
) {
153 ALLOC_SEQ(&t
->req_body
.etype
, 1);
154 if(t
->req_body
.etype
.val
== NULL
) {
156 krb5_set_error_message(context
, ret
,
157 N_("malloc: out of memory", ""));
160 t
->req_body
.etype
.val
[0] = in_creds
->session
.keytype
;
162 ret
= krb5_init_etype(context
,
163 &t
->req_body
.etype
.len
,
164 &t
->req_body
.etype
.val
,
169 t
->req_body
.addresses
= addresses
;
170 t
->req_body
.kdc_options
= flags
.b
;
171 ret
= copy_Realm(&in_creds
->server
->realm
, &t
->req_body
.realm
);
174 ALLOC(t
->req_body
.sname
, 1);
175 if (t
->req_body
.sname
== NULL
) {
177 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
181 /* some versions of some code might require that the client be
182 present in TGS-REQs, but this is clearly against the spec */
184 ret
= copy_PrincipalName(&in_creds
->server
->name
, t
->req_body
.sname
);
188 /* req_body.till should be NULL if there is no endtime specified,
189 but old MIT code (like DCE secd) doesn't like that */
190 ALLOC(t
->req_body
.till
, 1);
191 if(t
->req_body
.till
== NULL
){
193 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
196 *t
->req_body
.till
= in_creds
->times
.endtime
;
198 t
->req_body
.nonce
= nonce
;
200 ALLOC(t
->req_body
.additional_tickets
, 1);
201 if (t
->req_body
.additional_tickets
== NULL
) {
203 krb5_set_error_message(context
, ret
,
204 N_("malloc: out of memory", ""));
207 ALLOC_SEQ(t
->req_body
.additional_tickets
, 1);
208 if (t
->req_body
.additional_tickets
->val
== NULL
) {
210 krb5_set_error_message(context
, ret
,
211 N_("malloc: out of memory", ""));
214 ret
= copy_Ticket(second_ticket
, t
->req_body
.additional_tickets
->val
);
219 if (t
->padata
== NULL
) {
221 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
224 ALLOC_SEQ(t
->padata
, 1 + padata
->len
);
225 if (t
->padata
->val
== NULL
) {
227 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
232 for (i
= 0; i
< padata
->len
; i
++) {
233 ret
= copy_PA_DATA(&padata
->val
[i
], &t
->padata
->val
[i
+ 1]);
235 krb5_set_error_message(context
, ret
,
236 N_("malloc: out of memory", ""));
242 ret
= krb5_auth_con_init(context
, &ac
);
246 ret
= krb5_auth_con_generatelocalsubkey(context
, ac
, &krbtgt
->session
);
250 ret
= set_auth_data (context
, &t
->req_body
, &in_creds
->authdata
,
255 ret
= make_pa_tgs_req(context
,
263 ret
= krb5_auth_con_getlocalsubkey(context
, ac
, subkey
);
269 krb5_auth_con_free(context
, ac
);
271 t
->req_body
.addresses
= NULL
;
278 _krb5_get_krbtgt(krb5_context context
,
286 memset(&tmp_cred
, 0, sizeof(tmp_cred
));
288 ret
= krb5_cc_get_principal(context
, id
, &tmp_cred
.client
);
292 ret
= krb5_make_principal(context
,
299 krb5_free_principal(context
, tmp_cred
.client
);
302 ret
= krb5_get_credentials(context
,
307 krb5_free_principal(context
, tmp_cred
.client
);
308 krb5_free_principal(context
, tmp_cred
.server
);
314 /* DCE compatible decrypt proc */
315 static krb5_error_code
316 decrypt_tkt_with_subkey (krb5_context context
,
318 krb5_key_usage usage
,
319 krb5_const_pointer subkey
,
320 krb5_kdc_rep
*dec_rep
)
330 * start out with trying with subkey if we have one
333 ret
= krb5_crypto_init(context
, subkey
, 0, &crypto
);
336 ret
= krb5_decrypt_EncryptedData (context
,
338 KRB5_KU_TGS_REP_ENC_PART_SUB_KEY
,
339 &dec_rep
->kdc_rep
.enc_part
,
341 krb5_crypto_destroy(context
, crypto
);
343 if (subkey
== NULL
|| ret
) {
344 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
347 ret
= krb5_decrypt_EncryptedData (context
,
349 KRB5_KU_TGS_REP_ENC_PART_SESSION
,
350 &dec_rep
->kdc_rep
.enc_part
,
352 krb5_crypto_destroy(context
, crypto
);
357 ret
= decode_EncASRepPart(data
.data
,
362 ret
= decode_EncTGSRepPart(data
.data
,
367 krb5_set_error_message(context
, ret
,
368 N_("Failed to decode encpart in ticket", ""));
369 krb5_data_free (&data
);
373 static krb5_error_code
374 get_cred_kdc(krb5_context context
,
376 krb5_kdc_flags flags
,
377 krb5_addresses
*addresses
,
378 krb5_creds
*in_creds
,
380 krb5_principal impersonate_principal
,
381 Ticket
*second_ticket
,
382 krb5_creds
*out_creds
)
391 krb5_keyblock
*subkey
= NULL
;
393 Ticket second_ticket_data
;
396 krb5_data_zero(&resp
);
397 krb5_data_zero(&enc
);
401 krb5_generate_random_block(&nonce
, sizeof(nonce
));
404 if(flags
.b
.enc_tkt_in_skey
&& second_ticket
== NULL
){
405 ret
= decode_Ticket(in_creds
->second_ticket
.data
,
406 in_creds
->second_ticket
.length
,
407 &second_ticket_data
, &len
);
410 second_ticket
= &second_ticket_data
;
414 if (impersonate_principal
) {
421 self
.name
= impersonate_principal
->name
;
422 self
.realm
= impersonate_principal
->realm
;
423 self
.auth
= estrdup("Kerberos");
425 ret
= _krb5_s4u2self_to_checksumdata(context
, &self
, &data
);
431 ret
= krb5_crypto_init(context
, &krbtgt
->session
, 0, &crypto
);
434 krb5_data_free(&data
);
438 ret
= krb5_create_checksum(context
,
445 krb5_crypto_destroy(context
, crypto
);
446 krb5_data_free(&data
);
452 ASN1_MALLOC_ENCODE(PA_S4U2Self
, buf
, len
, &self
, &size
, ret
);
454 free_Checksum(&self
.cksum
);
458 krb5_abortx(context
, "internal asn1 error");
460 ret
= krb5_padata_add(context
, &padata
, KRB5_PADATA_FOR_USER
, buf
, len
);
465 ret
= init_tgs_req (context
,
479 ASN1_MALLOC_ENCODE(TGS_REQ
, enc
.data
, enc
.length
, &req
, &len
, ret
);
482 if(enc
.length
!= len
)
483 krb5_abortx(context
, "internal error in ASN.1 encoder");
485 /* don't free addresses */
486 req
.req_body
.addresses
= NULL
;
493 krb5_sendto_ctx stctx
;
494 ret
= krb5_sendto_ctx_alloc(context
, &stctx
);
497 krb5_sendto_ctx_set_func(stctx
, _krb5_kdc_retry
, NULL
);
499 ret
= krb5_sendto_context (context
, stctx
, &enc
,
500 krbtgt
->server
->name
.name_string
.val
[1],
502 krb5_sendto_ctx_free(context
, stctx
);
507 memset(&rep
, 0, sizeof(rep
));
508 if(decode_TGS_REP(resp
.data
, resp
.length
, &rep
.kdc_rep
, &len
) == 0) {
511 ret
= krb5_copy_principal(context
,
516 ret
= krb5_copy_principal(context
,
521 /* this should go someplace else */
522 out_creds
->times
.endtime
= in_creds
->times
.endtime
;
524 /* XXX should do better testing */
525 if (flags
.b
.constrained_delegation
|| impersonate_principal
)
526 eflags
|= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH
;
528 ret
= _krb5_extract_ticket(context
,
537 decrypt_tkt_with_subkey
,
540 krb5_free_kdc_rep(context
, &rep
);
541 } else if(krb5_rd_error(context
, &resp
, &error
) == 0) {
542 ret
= krb5_error_from_rd_error(context
, &error
, in_creds
);
543 krb5_free_error_contents(context
, &error
);
544 } else if(resp
.length
> 0 && ((char*)resp
.data
)[0] == 4) {
545 ret
= KRB5KRB_AP_ERR_V4_REPLY
;
546 krb5_clear_error_message(context
);
548 ret
= KRB5KRB_AP_ERR_MSG_TYPE
;
549 krb5_clear_error_message(context
);
553 if (second_ticket
== &second_ticket_data
)
554 free_Ticket(&second_ticket_data
);
555 free_METHOD_DATA(&padata
);
556 krb5_data_free(&resp
);
557 krb5_data_free(&enc
);
559 krb5_free_keyblock(context
, subkey
);
565 * same as above, just get local addresses first if the krbtgt have
566 * them and the realm is not addressless
569 static krb5_error_code
570 get_cred_kdc_address(krb5_context context
,
572 krb5_kdc_flags flags
,
573 krb5_addresses
*addrs
,
574 krb5_creds
*in_creds
,
576 krb5_principal impersonate_principal
,
577 Ticket
*second_ticket
,
578 krb5_creds
*out_creds
)
581 krb5_addresses addresses
= { 0, NULL
};
584 * Inherit the address-ness of the krbtgt if the address is not
588 if (addrs
== NULL
&& krbtgt
->addresses
.len
!= 0) {
591 krb5_appdefault_boolean(context
, NULL
, krbtgt
->server
->realm
,
592 "no-addresses", FALSE
, &noaddr
);
595 krb5_get_all_client_addrs(context
, &addresses
);
596 /* XXX this sucks. */
598 if(addresses
.len
== 0)
602 ret
= get_cred_kdc(context
, id
, flags
, addrs
, in_creds
,
603 krbtgt
, impersonate_principal
,
604 second_ticket
, out_creds
);
605 krb5_free_addresses(context
, &addresses
);
609 krb5_error_code KRB5_LIB_FUNCTION
610 krb5_get_kdc_cred(krb5_context context
,
612 krb5_kdc_flags flags
,
613 krb5_addresses
*addresses
,
614 Ticket
*second_ticket
,
615 krb5_creds
*in_creds
,
616 krb5_creds
**out_creds
622 *out_creds
= calloc(1, sizeof(**out_creds
));
623 if(*out_creds
== NULL
) {
624 krb5_set_error_message(context
, ENOMEM
,
625 N_("malloc: out of memory", ""));
628 ret
= _krb5_get_krbtgt (context
,
630 in_creds
->server
->realm
,
637 ret
= get_cred_kdc(context
, id
, flags
, addresses
,
638 in_creds
, krbtgt
, NULL
, NULL
, *out_creds
);
639 krb5_free_creds (context
, krbtgt
);
648 not_found(krb5_context context
, krb5_const_principal p
, krb5_error_code code
)
653 ret
= krb5_unparse_name(context
, p
, &str
);
655 krb5_clear_error_message(context
);
658 krb5_set_error_message(context
, code
,
659 N_("Matching credential (%s) not found", ""), str
);
664 static krb5_error_code
665 find_cred(krb5_context context
,
667 krb5_principal server
,
669 krb5_creds
*out_creds
)
674 krb5_cc_clear_mcred(&mcreds
);
675 mcreds
.server
= server
;
676 ret
= krb5_cc_retrieve_cred(context
, id
, KRB5_TC_DONT_MATCH_REALM
,
680 while(tgts
&& *tgts
){
681 if(krb5_compare_creds(context
, KRB5_TC_DONT_MATCH_REALM
,
683 ret
= krb5_copy_creds_contents(context
, *tgts
, out_creds
);
688 return not_found(context
, server
, KRB5_CC_NOTFOUND
);
691 static krb5_error_code
692 add_cred(krb5_context context
, krb5_creds
const *tkt
, krb5_creds
***tgts
)
696 krb5_creds
**tmp
= *tgts
;
698 for(i
= 0; tmp
&& tmp
[i
]; i
++); /* XXX */
699 tmp
= realloc(tmp
, (i
+2)*sizeof(*tmp
));
701 krb5_set_error_message(context
, ENOMEM
,
702 N_("malloc: out of memory", ""));
706 ret
= krb5_copy_creds(context
, tkt
, &tmp
[i
]);
713 creds = cc_get_cred(server)
714 if(creds) return creds
715 tgt = cc_get_cred(krbtgt/server_realm@any_realm)
717 return get_cred_tgt(server, tgt)
718 if(client_realm == server_realm)
720 tgt = get_cred(krbtgt/server_realm@client_realm)
721 while(tgt_inst != server_realm)
722 tgt = get_cred(krbtgt/server_realm@tgt_inst)
723 return get_cred_tgt(server, tgt)
726 static krb5_error_code
727 get_cred_kdc_capath(krb5_context context
,
728 krb5_kdc_flags flags
,
730 krb5_creds
*in_creds
,
731 krb5_principal impersonate_principal
,
732 Ticket
*second_ticket
,
733 krb5_creds
**out_creds
,
734 krb5_creds
***ret_tgts
)
737 krb5_creds
*tgt
, tmp_creds
;
738 krb5_const_realm client_realm
, server_realm
, try_realm
;
739 int ok_as_delegate
= 1;
743 client_realm
= krb5_principal_get_realm(context
, in_creds
->client
);
744 server_realm
= krb5_principal_get_realm(context
, in_creds
->server
);
745 memset(&tmp_creds
, 0, sizeof(tmp_creds
));
746 ret
= krb5_copy_principal(context
, in_creds
->client
, &tmp_creds
.client
);
750 try_realm
= krb5_config_get_string(context
, NULL
, "capaths",
751 client_realm
, server_realm
, NULL
);
752 if (try_realm
== NULL
)
753 try_realm
= client_realm
;
755 ret
= krb5_make_principal(context
,
762 krb5_free_principal(context
, tmp_creds
.client
);
768 ret
= find_cred(context
, ccache
, tmp_creds
.server
,
771 if (try_realm
!= client_realm
)
772 ok_as_delegate
= tgts
.flags
.b
.ok_as_delegate
;
774 *out_creds
= calloc(1, sizeof(**out_creds
));
775 if(*out_creds
== NULL
) {
777 krb5_set_error_message(context
, ret
,
778 N_("malloc: out of memory", ""));
780 ret
= get_cred_kdc_address(context
, ccache
, flags
, NULL
,
782 impersonate_principal
,
788 } else if (ok_as_delegate
== 0)
789 (*out_creds
)->flags
.b
.ok_as_delegate
= 0;
791 krb5_free_cred_contents(context
, &tgts
);
792 krb5_free_principal(context
, tmp_creds
.server
);
793 krb5_free_principal(context
, tmp_creds
.client
);
797 if(krb5_realm_compare(context
, in_creds
->client
, in_creds
->server
))
798 return not_found(context
, in_creds
->server
, KRB5_CC_NOTFOUND
);
800 /* XXX this can loop forever */
802 heim_general_string tgt_inst
;
804 ret
= get_cred_kdc_capath(context
, flags
, ccache
, &tmp_creds
,
805 NULL
, NULL
, &tgt
, ret_tgts
);
807 krb5_free_principal(context
, tmp_creds
.server
);
808 krb5_free_principal(context
, tmp_creds
.client
);
812 * if either of the chain or the ok_as_delegate was stripped
813 * by the kdc, make sure we strip it too.
815 if (ok_as_delegate
== 0 || tgt
->flags
.b
.ok_as_delegate
== 0) {
817 tgt
->flags
.b
.ok_as_delegate
= 0;
820 ret
= add_cred(context
, tgt
, ret_tgts
);
822 krb5_free_principal(context
, tmp_creds
.server
);
823 krb5_free_principal(context
, tmp_creds
.client
);
826 tgt_inst
= tgt
->server
->name
.name_string
.val
[1];
827 if(strcmp(tgt_inst
, server_realm
) == 0)
829 krb5_free_principal(context
, tmp_creds
.server
);
830 ret
= krb5_make_principal(context
, &tmp_creds
.server
,
831 tgt_inst
, KRB5_TGS_NAME
, server_realm
, NULL
);
833 krb5_free_principal(context
, tmp_creds
.server
);
834 krb5_free_principal(context
, tmp_creds
.client
);
837 ret
= krb5_free_creds(context
, tgt
);
839 krb5_free_principal(context
, tmp_creds
.server
);
840 krb5_free_principal(context
, tmp_creds
.client
);
845 krb5_free_principal(context
, tmp_creds
.server
);
846 krb5_free_principal(context
, tmp_creds
.client
);
847 *out_creds
= calloc(1, sizeof(**out_creds
));
848 if(*out_creds
== NULL
) {
850 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
852 ret
= get_cred_kdc_address (context
, ccache
, flags
, NULL
,
853 in_creds
, tgt
, impersonate_principal
,
854 second_ticket
, *out_creds
);
860 krb5_free_creds(context
, tgt
);
864 static krb5_error_code
865 get_cred_kdc_referral(krb5_context context
,
866 krb5_kdc_flags flags
,
868 krb5_creds
*in_creds
,
869 krb5_principal impersonate_principal
,
870 Ticket
*second_ticket
,
871 krb5_creds
**out_creds
,
872 krb5_creds
***ret_tgts
)
874 krb5_const_realm client_realm
;
876 krb5_creds tgt
, referral
, ticket
;
878 int ok_as_delegate
= 1;
880 if (in_creds
->server
->name
.name_string
.len
< 2 && !flags
.b
.canonicalize
) {
881 krb5_set_error_message(context
, KRB5KDC_ERR_PATH_NOT_ACCEPTED
,
882 N_("Name too short to do referals, skipping", ""));
883 return KRB5KDC_ERR_PATH_NOT_ACCEPTED
;
886 memset(&tgt
, 0, sizeof(tgt
));
887 memset(&ticket
, 0, sizeof(ticket
));
889 flags
.b
.canonicalize
= 1;
893 client_realm
= krb5_principal_get_realm(context
, in_creds
->client
);
895 /* find tgt for the clients base realm */
897 krb5_principal tgtname
;
899 ret
= krb5_make_principal(context
, &tgtname
,
907 ret
= find_cred(context
, ccache
, tgtname
, *ret_tgts
, &tgt
);
908 krb5_free_principal(context
, tgtname
);
913 referral
= *in_creds
;
914 ret
= krb5_copy_principal(context
, in_creds
->server
, &referral
.server
);
916 krb5_free_cred_contents(context
, &tgt
);
919 ret
= krb5_principal_set_realm(context
, referral
.server
, client_realm
);
921 krb5_free_cred_contents(context
, &tgt
);
922 krb5_free_principal(context
, referral
.server
);
926 while (loop
++ < 17) {
927 krb5_creds
**tickets
;
929 char *referral_realm
;
931 /* Use cache if we are not doing impersonation or contrainte deleg */
932 if (impersonate_principal
== NULL
|| flags
.b
.constrained_delegation
) {
933 krb5_cc_clear_mcred(&mcreds
);
934 mcreds
.server
= referral
.server
;
935 ret
= krb5_cc_retrieve_cred(context
, ccache
, 0, &mcreds
, &ticket
);
940 ret
= get_cred_kdc_address (context
, ccache
, flags
, NULL
,
941 &referral
, &tgt
, impersonate_principal
,
942 second_ticket
, &ticket
);
947 /* Did we get the right ticket ? */
948 if (krb5_principal_compare_any_realm(context
,
953 if (!krb5_principal_is_krbtgt(context
, ticket
.server
)) {
954 krb5_set_error_message(context
, KRB5KRB_AP_ERR_NOT_US
,
955 N_("Got back an non krbtgt "
956 "ticket referrals", ""));
957 krb5_free_cred_contents(context
, &ticket
);
958 return KRB5KRB_AP_ERR_NOT_US
;
961 referral_realm
= ticket
.server
->name
.name_string
.val
[1];
963 /* check that there are no referrals loops */
966 krb5_cc_clear_mcred(&mcreds
);
967 mcreds
.server
= ticket
.server
;
969 while(tickets
&& *tickets
){
970 if(krb5_compare_creds(context
,
971 KRB5_TC_DONT_MATCH_REALM
,
975 krb5_set_error_message(context
, KRB5_GET_IN_TKT_LOOP
,
976 N_("Referral from %s "
977 "loops back to realm %s", ""),
980 krb5_free_cred_contents(context
, &ticket
);
981 return KRB5_GET_IN_TKT_LOOP
;
987 * if either of the chain or the ok_as_delegate was stripped
988 * by the kdc, make sure we strip it too.
991 if (ok_as_delegate
== 0 || ticket
.flags
.b
.ok_as_delegate
== 0) {
993 ticket
.flags
.b
.ok_as_delegate
= 0;
996 ret
= add_cred(context
, &ticket
, ret_tgts
);
998 krb5_free_cred_contents(context
, &ticket
);
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
);
1023 * Glue function between referrals version and old client chasing
1028 _krb5_get_cred_kdc_any(krb5_context context
,
1029 krb5_kdc_flags flags
,
1031 krb5_creds
*in_creds
,
1032 krb5_principal impersonate_principal
,
1033 Ticket
*second_ticket
,
1034 krb5_creds
**out_creds
,
1035 krb5_creds
***ret_tgts
)
1037 krb5_error_code ret
;
1039 ret
= get_cred_kdc_referral(context
,
1043 impersonate_principal
,
1047 if (ret
== 0 || flags
.b
.canonicalize
)
1049 return get_cred_kdc_capath(context
,
1053 impersonate_principal
,
1060 krb5_error_code KRB5_LIB_FUNCTION
1061 krb5_get_credentials_with_flags(krb5_context context
,
1063 krb5_kdc_flags flags
,
1065 krb5_creds
*in_creds
,
1066 krb5_creds
**out_creds
)
1068 krb5_error_code ret
;
1070 krb5_creds
*res_creds
;
1073 if (in_creds
->session
.keytype
) {
1074 ret
= krb5_enctype_valid(context
, in_creds
->session
.keytype
);
1080 res_creds
= calloc(1, sizeof(*res_creds
));
1081 if (res_creds
== NULL
) {
1082 krb5_set_error_message(context
, ENOMEM
,
1083 N_("malloc: out of memory", ""));
1087 if (in_creds
->session
.keytype
)
1088 options
|= KRB5_TC_MATCH_KEYTYPE
;
1091 * If we got a credential, check if credential is expired before
1094 ret
= krb5_cc_retrieve_cred(context
,
1096 in_creds
->session
.keytype
?
1097 KRB5_TC_MATCH_KEYTYPE
: 0,
1098 in_creds
, res_creds
);
1100 * If we got a credential, check if credential is expired before
1101 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1104 krb5_timestamp timeret
;
1106 /* If expired ok, don't bother checking */
1107 if(options
& KRB5_GC_EXPIRED_OK
) {
1108 *out_creds
= res_creds
;
1112 krb5_timeofday(context
, &timeret
);
1113 if(res_creds
->times
.endtime
> timeret
) {
1114 *out_creds
= res_creds
;
1117 if(options
& KRB5_GC_CACHED
)
1118 krb5_cc_remove_cred(context
, ccache
, 0, res_creds
);
1120 } else if(ret
!= KRB5_CC_END
) {
1125 if(options
& KRB5_GC_CACHED
)
1126 return not_found(context
, in_creds
->server
, KRB5_CC_NOTFOUND
);
1128 if(options
& KRB5_GC_USER_USER
)
1129 flags
.b
.enc_tkt_in_skey
= 1;
1130 if (flags
.b
.enc_tkt_in_skey
)
1131 options
|= KRB5_GC_NO_STORE
;
1134 ret
= _krb5_get_cred_kdc_any(context
, flags
, ccache
,
1135 in_creds
, NULL
, NULL
, out_creds
, &tgts
);
1136 for(i
= 0; tgts
&& tgts
[i
]; i
++) {
1137 krb5_cc_store_cred(context
, ccache
, tgts
[i
]);
1138 krb5_free_creds(context
, tgts
[i
]);
1141 if(ret
== 0 && (options
& KRB5_GC_NO_STORE
) == 0)
1142 krb5_cc_store_cred(context
, ccache
, *out_creds
);
1146 krb5_error_code KRB5_LIB_FUNCTION
1147 krb5_get_credentials(krb5_context context
,
1150 krb5_creds
*in_creds
,
1151 krb5_creds
**out_creds
)
1153 krb5_kdc_flags flags
;
1155 return krb5_get_credentials_with_flags(context
, options
, flags
,
1156 ccache
, in_creds
, out_creds
);
1159 struct krb5_get_creds_opt_data
{
1160 krb5_principal self
;
1162 krb5_enctype enctype
;
1167 krb5_error_code KRB5_LIB_FUNCTION
1168 krb5_get_creds_opt_alloc(krb5_context context
, krb5_get_creds_opt
*opt
)
1170 *opt
= calloc(1, sizeof(**opt
));
1172 krb5_set_error_message(context
, ENOMEM
,
1173 N_("malloc: out of memory", ""));
1179 void KRB5_LIB_FUNCTION
1180 krb5_get_creds_opt_free(krb5_context context
, krb5_get_creds_opt opt
)
1183 krb5_free_principal(context
, opt
->self
);
1185 free_Ticket(opt
->ticket
);
1188 memset(opt
, 0, sizeof(*opt
));
1192 void KRB5_LIB_FUNCTION
1193 krb5_get_creds_opt_set_options(krb5_context context
,
1194 krb5_get_creds_opt opt
,
1197 opt
->options
= options
;
1200 void KRB5_LIB_FUNCTION
1201 krb5_get_creds_opt_add_options(krb5_context context
,
1202 krb5_get_creds_opt opt
,
1205 opt
->options
|= options
;
1208 void KRB5_LIB_FUNCTION
1209 krb5_get_creds_opt_set_enctype(krb5_context context
,
1210 krb5_get_creds_opt opt
,
1211 krb5_enctype enctype
)
1213 opt
->enctype
= enctype
;
1216 krb5_error_code KRB5_LIB_FUNCTION
1217 krb5_get_creds_opt_set_impersonate(krb5_context context
,
1218 krb5_get_creds_opt opt
,
1219 krb5_const_principal self
)
1222 krb5_free_principal(context
, opt
->self
);
1223 return krb5_copy_principal(context
, self
, &opt
->self
);
1226 krb5_error_code KRB5_LIB_FUNCTION
1227 krb5_get_creds_opt_set_ticket(krb5_context context
,
1228 krb5_get_creds_opt opt
,
1229 const Ticket
*ticket
)
1232 free_Ticket(opt
->ticket
);
1237 krb5_error_code ret
;
1239 opt
->ticket
= malloc(sizeof(*ticket
));
1240 if (opt
->ticket
== NULL
) {
1241 krb5_set_error_message(context
, ENOMEM
,
1242 N_("malloc: out of memory", ""));
1245 ret
= copy_Ticket(ticket
, opt
->ticket
);
1249 krb5_set_error_message(context
, ret
,
1250 N_("malloc: out of memory", ""));
1259 krb5_error_code KRB5_LIB_FUNCTION
1260 krb5_get_creds(krb5_context context
,
1261 krb5_get_creds_opt opt
,
1263 krb5_const_principal inprinc
,
1264 krb5_creds
**out_creds
)
1266 krb5_kdc_flags flags
;
1268 krb5_creds in_creds
;
1269 krb5_error_code ret
;
1271 krb5_creds
*res_creds
;
1274 if (opt
&& opt
->enctype
) {
1275 ret
= krb5_enctype_valid(context
, opt
->enctype
);
1280 memset(&in_creds
, 0, sizeof(in_creds
));
1281 in_creds
.server
= rk_UNCONST(inprinc
);
1283 ret
= krb5_cc_get_principal(context
, ccache
, &in_creds
.client
);
1288 options
= opt
->options
;
1294 res_creds
= calloc(1, sizeof(*res_creds
));
1295 if (res_creds
== NULL
) {
1296 krb5_free_principal(context
, in_creds
.client
);
1297 krb5_set_error_message(context
, ENOMEM
,
1298 N_("malloc: out of memory", ""));
1302 if (opt
&& opt
->enctype
) {
1303 in_creds
.session
.keytype
= opt
->enctype
;
1304 options
|= KRB5_TC_MATCH_KEYTYPE
;
1308 * If we got a credential, check if credential is expired before
1311 ret
= krb5_cc_retrieve_cred(context
,
1313 options
& KRB5_TC_MATCH_KEYTYPE
,
1314 &in_creds
, res_creds
);
1316 * If we got a credential, check if credential is expired before
1317 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1320 krb5_timestamp timeret
;
1322 /* If expired ok, don't bother checking */
1323 if(options
& KRB5_GC_EXPIRED_OK
) {
1324 *out_creds
= res_creds
;
1325 krb5_free_principal(context
, in_creds
.client
);
1329 krb5_timeofday(context
, &timeret
);
1330 if(res_creds
->times
.endtime
> timeret
) {
1331 *out_creds
= res_creds
;
1332 krb5_free_principal(context
, in_creds
.client
);
1335 if(options
& KRB5_GC_CACHED
)
1336 krb5_cc_remove_cred(context
, ccache
, 0, res_creds
);
1338 } else if(ret
!= KRB5_CC_END
) {
1340 krb5_free_principal(context
, in_creds
.client
);
1344 if(options
& KRB5_GC_CACHED
) {
1345 krb5_free_principal(context
, in_creds
.client
);
1346 return not_found(context
, in_creds
.server
, KRB5_CC_NOTFOUND
);
1348 if(options
& KRB5_GC_USER_USER
) {
1349 flags
.b
.enc_tkt_in_skey
= 1;
1350 options
|= KRB5_GC_NO_STORE
;
1352 if (options
& KRB5_GC_FORWARDABLE
)
1353 flags
.b
.forwardable
= 1;
1354 if (options
& KRB5_GC_NO_TRANSIT_CHECK
)
1355 flags
.b
.disable_transited_check
= 1;
1356 if (options
& KRB5_GC_CONSTRAINED_DELEGATION
) {
1357 flags
.b
.request_anonymous
= 1; /* XXX ARGH confusion */
1358 flags
.b
.constrained_delegation
= 1;
1360 if (options
& KRB5_GC_CANONICALIZE
)
1361 flags
.b
.canonicalize
= 1;
1364 ret
= _krb5_get_cred_kdc_any(context
, flags
, ccache
,
1365 &in_creds
, opt
->self
, opt
->ticket
,
1367 krb5_free_principal(context
, in_creds
.client
);
1368 for(i
= 0; tgts
&& tgts
[i
]; i
++) {
1369 krb5_cc_store_cred(context
, ccache
, tgts
[i
]);
1370 krb5_free_creds(context
, tgts
[i
]);
1373 if(ret
== 0 && (options
& KRB5_GC_NO_STORE
) == 0)
1374 krb5_cc_store_cred(context
, ccache
, *out_creds
);
1382 krb5_error_code KRB5_LIB_FUNCTION
1383 krb5_get_renewed_creds(krb5_context context
,
1385 krb5_const_principal client
,
1387 const char *in_tkt_service
)
1389 krb5_error_code ret
;
1390 krb5_kdc_flags flags
;
1391 krb5_creds in
, *template, *out
= NULL
;
1393 memset(&in
, 0, sizeof(in
));
1394 memset(creds
, 0, sizeof(*creds
));
1396 ret
= krb5_copy_principal(context
, client
, &in
.client
);
1400 if (in_tkt_service
) {
1401 ret
= krb5_parse_name(context
, in_tkt_service
, &in
.server
);
1403 krb5_free_principal(context
, in
.client
);
1407 const char *realm
= krb5_principal_get_realm(context
, client
);
1409 ret
= krb5_make_principal(context
, &in
.server
, realm
, KRB5_TGS_NAME
,
1412 krb5_free_principal(context
, in
.client
);
1418 flags
.b
.renewable
= flags
.b
.renew
= 1;
1421 * Get template from old credential cache for the same entry, if
1422 * this failes, no worries.
1424 ret
= krb5_get_credentials(context
, KRB5_GC_CACHED
, ccache
, &in
, &template);
1426 flags
.b
.forwardable
= template->flags
.b
.forwardable
;
1427 flags
.b
.proxiable
= template->flags
.b
.proxiable
;
1428 krb5_free_creds (context
, template);
1431 ret
= krb5_get_kdc_cred(context
, ccache
, flags
, NULL
, NULL
, &in
, &out
);
1432 krb5_free_principal(context
, in
.client
);
1433 krb5_free_principal(context
, in
.server
);
1437 ret
= krb5_copy_creds_contents(context
, out
, creds
);
1438 krb5_free_creds(context
, out
);