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>
39 * Take the `body' and encode it into `padata' using the credentials
43 static krb5_error_code
44 make_pa_tgs_req(krb5_context context
,
56 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, body
, &len
, ret
);
60 krb5_abortx(context
, "internal error in ASN.1 encoder");
64 ret
= _krb5_mk_req_internal(context
, &ac
, 0, &in_data
, creds
,
65 &padata
->padata_value
,
66 KRB5_KU_TGS_REQ_AUTH_CKSUM
,
67 KRB5_KU_TGS_REQ_AUTH
);
72 padata
->padata_type
= KRB5_PADATA_TGS_REQ
;
77 * Set the `enc-authorization-data' in `req_body' based on `authdata'
80 static krb5_error_code
81 set_auth_data (krb5_context context
,
82 KDC_REQ_BODY
*req_body
,
83 krb5_authdata
*authdata
,
92 ASN1_MALLOC_ENCODE(AuthorizationData
, buf
, buf_size
, authdata
,
97 krb5_abortx(context
, "internal error in ASN.1 encoder");
99 ALLOC(req_body
->enc_authorization_data
, 1);
100 if (req_body
->enc_authorization_data
== NULL
) {
102 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
105 ret
= krb5_crypto_init(context
, key
, 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
,
115 /* KRB5_KU_TGS_REQ_AUTH_DAT_SESSION? */
119 req_body
->enc_authorization_data
);
121 krb5_crypto_destroy(context
, crypto
);
123 req_body
->enc_authorization_data
= NULL
;
129 * Create a tgs-req in `t' with `addresses', `flags', `second_ticket'
130 * (if not-NULL), `in_creds', `krbtgt', and returning the generated
131 * subkey in `subkey'.
134 static krb5_error_code
135 init_tgs_req (krb5_context context
,
137 krb5_addresses
*addresses
,
138 krb5_kdc_flags flags
,
139 Ticket
*second_ticket
,
140 krb5_creds
*in_creds
,
143 const METHOD_DATA
*padata
,
144 krb5_keyblock
**subkey
,
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
, "malloc: out of memory");
159 t
->req_body
.etype
.val
[0] = in_creds
->session
.keytype
;
161 ret
= krb5_init_etype(context
,
162 &t
->req_body
.etype
.len
,
163 &t
->req_body
.etype
.val
,
168 t
->req_body
.addresses
= addresses
;
169 t
->req_body
.kdc_options
= flags
.b
;
170 ret
= copy_Realm(&in_creds
->server
->realm
, &t
->req_body
.realm
);
173 ALLOC(t
->req_body
.sname
, 1);
174 if (t
->req_body
.sname
== NULL
) {
176 krb5_set_error_message(context
, ret
, "malloc: out of memory");
180 /* some versions of some code might require that the client be
181 present in TGS-REQs, but this is clearly against the spec */
183 ret
= copy_PrincipalName(&in_creds
->server
->name
, t
->req_body
.sname
);
187 /* req_body.till should be NULL if there is no endtime specified,
188 but old MIT code (like DCE secd) doesn't like that */
189 ALLOC(t
->req_body
.till
, 1);
190 if(t
->req_body
.till
== NULL
){
192 krb5_set_error_message(context
, ret
, "malloc: out of memory");
195 *t
->req_body
.till
= in_creds
->times
.endtime
;
197 t
->req_body
.nonce
= nonce
;
199 ALLOC(t
->req_body
.additional_tickets
, 1);
200 if (t
->req_body
.additional_tickets
== NULL
) {
202 krb5_set_error_message(context
, ret
, "malloc: out of memory");
205 ALLOC_SEQ(t
->req_body
.additional_tickets
, 1);
206 if (t
->req_body
.additional_tickets
->val
== NULL
) {
208 krb5_set_error_message(context
, ret
, "malloc: out of memory");
211 ret
= copy_Ticket(second_ticket
, t
->req_body
.additional_tickets
->val
);
216 if (t
->padata
== NULL
) {
218 krb5_set_error_message(context
, ret
, "malloc: out of memory");
221 ALLOC_SEQ(t
->padata
, 1 + padata
->len
);
222 if (t
->padata
->val
== NULL
) {
224 krb5_set_error_message(context
, ret
, "malloc: out of memory");
229 for (i
= 0; i
< padata
->len
; i
++) {
230 ret
= copy_PA_DATA(&padata
->val
[i
], &t
->padata
->val
[i
+ 1]);
232 krb5_set_error_message(context
, ret
, "malloc: out of memory");
239 krb5_auth_context ac
;
240 krb5_keyblock
*key
= NULL
;
242 ret
= krb5_auth_con_init(context
, &ac
);
246 if (krb5_config_get_bool_default(context
, NULL
, FALSE
,
248 krbtgt
->server
->realm
,
249 "tgs_require_subkey",
252 ret
= krb5_generate_subkey (context
, &krbtgt
->session
, &key
);
254 krb5_auth_con_free (context
, ac
);
258 ret
= krb5_auth_con_setlocalsubkey(context
, ac
, key
);
261 krb5_free_keyblock (context
, key
);
262 krb5_auth_con_free (context
, ac
);
267 ret
= set_auth_data (context
, &t
->req_body
, &in_creds
->authdata
,
268 key
? key
: &krbtgt
->session
);
271 krb5_free_keyblock (context
, key
);
272 krb5_auth_con_free (context
, ac
);
276 ret
= make_pa_tgs_req(context
,
283 krb5_free_keyblock (context
, key
);
284 krb5_auth_con_free(context
, ac
);
289 krb5_auth_con_free(context
, ac
);
293 t
->req_body
.addresses
= NULL
;
300 _krb5_get_krbtgt(krb5_context context
,
308 memset(&tmp_cred
, 0, sizeof(tmp_cred
));
310 ret
= krb5_cc_get_principal(context
, id
, &tmp_cred
.client
);
314 ret
= krb5_make_principal(context
,
321 krb5_free_principal(context
, tmp_cred
.client
);
324 ret
= krb5_get_credentials(context
,
329 krb5_free_principal(context
, tmp_cred
.client
);
330 krb5_free_principal(context
, tmp_cred
.server
);
336 /* DCE compatible decrypt proc */
337 static krb5_error_code
338 decrypt_tkt_with_subkey (krb5_context context
,
340 krb5_key_usage usage
,
341 krb5_const_pointer subkey
,
342 krb5_kdc_rep
*dec_rep
)
349 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
352 ret
= krb5_decrypt_EncryptedData (context
,
355 &dec_rep
->kdc_rep
.enc_part
,
357 krb5_crypto_destroy(context
, crypto
);
359 /* DCE compat -- try to decrypt with subkey */
360 ret
= krb5_crypto_init(context
, subkey
, 0, &crypto
);
363 ret
= krb5_decrypt_EncryptedData (context
,
365 KRB5_KU_TGS_REP_ENC_PART_SUB_KEY
,
366 &dec_rep
->kdc_rep
.enc_part
,
368 krb5_crypto_destroy(context
, crypto
);
373 ret
= krb5_decode_EncASRepPart(context
,
379 ret
= krb5_decode_EncTGSRepPart(context
,
384 krb5_data_free (&data
);
388 static krb5_error_code
389 get_cred_kdc(krb5_context context
,
391 krb5_kdc_flags flags
,
392 krb5_addresses
*addresses
,
393 krb5_creds
*in_creds
,
395 krb5_principal impersonate_principal
,
396 Ticket
*second_ticket
,
397 krb5_creds
*out_creds
)
406 krb5_keyblock
*subkey
= NULL
;
408 Ticket second_ticket_data
;
411 krb5_data_zero(&resp
);
412 krb5_data_zero(&enc
);
416 krb5_generate_random_block(&nonce
, sizeof(nonce
));
419 if(flags
.b
.enc_tkt_in_skey
&& second_ticket
== NULL
){
420 ret
= decode_Ticket(in_creds
->second_ticket
.data
,
421 in_creds
->second_ticket
.length
,
422 &second_ticket_data
, &len
);
425 second_ticket
= &second_ticket_data
;
429 if (impersonate_principal
) {
436 self
.name
= impersonate_principal
->name
;
437 self
.realm
= impersonate_principal
->realm
;
438 self
.auth
= estrdup("Kerberos");
440 ret
= _krb5_s4u2self_to_checksumdata(context
, &self
, &data
);
446 ret
= krb5_crypto_init(context
, &krbtgt
->session
, 0, &crypto
);
449 krb5_data_free(&data
);
453 ret
= krb5_create_checksum(context
,
460 krb5_crypto_destroy(context
, crypto
);
461 krb5_data_free(&data
);
467 ASN1_MALLOC_ENCODE(PA_S4U2Self
, buf
, len
, &self
, &size
, ret
);
469 free_Checksum(&self
.cksum
);
473 krb5_abortx(context
, "internal asn1 error");
475 ret
= krb5_padata_add(context
, &padata
, KRB5_PADATA_S4U2SELF
, buf
, len
);
480 ret
= init_tgs_req (context
,
494 ASN1_MALLOC_ENCODE(TGS_REQ
, enc
.data
, enc
.length
, &req
, &len
, ret
);
497 if(enc
.length
!= len
)
498 krb5_abortx(context
, "internal error in ASN.1 encoder");
500 /* don't free addresses */
501 req
.req_body
.addresses
= NULL
;
508 krb5_sendto_ctx stctx
;
509 ret
= krb5_sendto_ctx_alloc(context
, &stctx
);
512 krb5_sendto_ctx_set_func(stctx
, _krb5_kdc_retry
, NULL
);
514 ret
= krb5_sendto_context (context
, stctx
, &enc
,
515 krbtgt
->server
->name
.name_string
.val
[1],
517 krb5_sendto_ctx_free(context
, stctx
);
522 memset(&rep
, 0, sizeof(rep
));
523 if(decode_TGS_REP(resp
.data
, resp
.length
, &rep
.kdc_rep
, &len
) == 0) {
526 ret
= krb5_copy_principal(context
,
531 ret
= krb5_copy_principal(context
,
536 /* this should go someplace else */
537 out_creds
->times
.endtime
= in_creds
->times
.endtime
;
539 /* XXX should do better testing */
540 if (flags
.b
.constrained_delegation
|| impersonate_principal
)
541 eflags
|= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH
;
543 ret
= _krb5_extract_ticket(context
,
548 KRB5_KU_TGS_REP_ENC_PART_SESSION
,
552 decrypt_tkt_with_subkey
,
555 krb5_free_kdc_rep(context
, &rep
);
556 } else if(krb5_rd_error(context
, &resp
, &error
) == 0) {
557 ret
= krb5_error_from_rd_error(context
, &error
, in_creds
);
558 krb5_free_error_contents(context
, &error
);
559 } else if(resp
.data
&& ((char*)resp
.data
)[0] == 4) {
560 ret
= KRB5KRB_AP_ERR_V4_REPLY
;
561 krb5_clear_error_string(context
);
563 ret
= KRB5KRB_AP_ERR_MSG_TYPE
;
564 krb5_clear_error_string(context
);
568 if (second_ticket
== &second_ticket_data
)
569 free_Ticket(&second_ticket_data
);
570 free_METHOD_DATA(&padata
);
571 krb5_data_free(&resp
);
572 krb5_data_free(&enc
);
574 krb5_free_keyblock_contents(context
, subkey
);
582 * same as above, just get local addresses first if the krbtgt have
583 * them and the realm is not addressless
586 static krb5_error_code
587 get_cred_kdc_address(krb5_context context
,
589 krb5_kdc_flags flags
,
590 krb5_addresses
*addrs
,
591 krb5_creds
*in_creds
,
593 krb5_principal impersonate_principal
,
594 Ticket
*second_ticket
,
595 krb5_creds
*out_creds
)
598 krb5_addresses addresses
= { 0, NULL
};
601 * Inherit the address-ness of the krbtgt if the address is not
605 if (addrs
== NULL
&& krbtgt
->addresses
.len
!= 0) {
608 krb5_appdefault_boolean(context
, NULL
, krbtgt
->server
->realm
,
609 "no-addresses", FALSE
, &noaddr
);
612 krb5_get_all_client_addrs(context
, &addresses
);
613 /* XXX this sucks. */
615 if(addresses
.len
== 0)
619 ret
= get_cred_kdc(context
, id
, flags
, addrs
, in_creds
,
620 krbtgt
, impersonate_principal
,
621 second_ticket
, out_creds
);
622 krb5_free_addresses(context
, &addresses
);
626 krb5_error_code KRB5_LIB_FUNCTION
627 krb5_get_kdc_cred(krb5_context context
,
629 krb5_kdc_flags flags
,
630 krb5_addresses
*addresses
,
631 Ticket
*second_ticket
,
632 krb5_creds
*in_creds
,
633 krb5_creds
**out_creds
639 *out_creds
= calloc(1, sizeof(**out_creds
));
640 if(*out_creds
== NULL
) {
641 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
644 ret
= _krb5_get_krbtgt (context
,
646 in_creds
->server
->realm
,
652 ret
= get_cred_kdc(context
, id
, flags
, addresses
,
653 in_creds
, krbtgt
, NULL
, NULL
, *out_creds
);
654 krb5_free_creds (context
, krbtgt
);
661 not_found(krb5_context context
, krb5_const_principal p
, krb5_error_code code
)
666 ret
= krb5_unparse_name(context
, p
, &str
);
668 krb5_clear_error_string(context
);
671 krb5_set_error_message(context
, code
, "Matching credential (%s) not found", str
);
676 static krb5_error_code
677 find_cred(krb5_context context
,
679 krb5_principal server
,
681 krb5_creds
*out_creds
)
686 krb5_cc_clear_mcred(&mcreds
);
687 mcreds
.server
= server
;
688 ret
= krb5_cc_retrieve_cred(context
, id
, KRB5_TC_DONT_MATCH_REALM
,
692 while(tgts
&& *tgts
){
693 if(krb5_compare_creds(context
, KRB5_TC_DONT_MATCH_REALM
,
695 ret
= krb5_copy_creds_contents(context
, *tgts
, out_creds
);
700 return not_found(context
, server
, KRB5_CC_NOTFOUND
);
703 static krb5_error_code
704 add_cred(krb5_context context
, krb5_creds
const *tkt
, krb5_creds
***tgts
)
708 krb5_creds
**tmp
= *tgts
;
710 for(i
= 0; tmp
&& tmp
[i
]; i
++); /* XXX */
711 tmp
= realloc(tmp
, (i
+2)*sizeof(*tmp
));
713 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
717 ret
= krb5_copy_creds(context
, tkt
, &tmp
[i
]);
724 creds = cc_get_cred(server)
725 if(creds) return creds
726 tgt = cc_get_cred(krbtgt/server_realm@any_realm)
728 return get_cred_tgt(server, tgt)
729 if(client_realm == server_realm)
731 tgt = get_cred(krbtgt/server_realm@client_realm)
732 while(tgt_inst != server_realm)
733 tgt = get_cred(krbtgt/server_realm@tgt_inst)
734 return get_cred_tgt(server, tgt)
737 static krb5_error_code
738 get_cred_kdc_capath(krb5_context context
,
739 krb5_kdc_flags flags
,
741 krb5_creds
*in_creds
,
742 krb5_principal impersonate_principal
,
743 Ticket
*second_ticket
,
744 krb5_creds
**out_creds
,
745 krb5_creds
***ret_tgts
)
748 krb5_creds
*tgt
, tmp_creds
;
749 krb5_const_realm client_realm
, server_realm
, try_realm
;
753 client_realm
= krb5_principal_get_realm(context
, in_creds
->client
);
754 server_realm
= krb5_principal_get_realm(context
, in_creds
->server
);
755 memset(&tmp_creds
, 0, sizeof(tmp_creds
));
756 ret
= krb5_copy_principal(context
, in_creds
->client
, &tmp_creds
.client
);
760 try_realm
= krb5_config_get_string(context
, NULL
, "capaths",
761 client_realm
, server_realm
, NULL
);
762 if (try_realm
== NULL
)
763 try_realm
= client_realm
;
765 ret
= krb5_make_principal(context
,
772 krb5_free_principal(context
, tmp_creds
.client
);
778 ret
= find_cred(context
, ccache
, tmp_creds
.server
,
781 *out_creds
= calloc(1, sizeof(**out_creds
));
782 if(*out_creds
== NULL
) {
784 krb5_set_error_message(context
, ret
, "malloc: out of memory");
786 ret
= get_cred_kdc_address(context
, ccache
, flags
, NULL
,
788 impersonate_principal
,
796 krb5_free_cred_contents(context
, &tgts
);
797 krb5_free_principal(context
, tmp_creds
.server
);
798 krb5_free_principal(context
, tmp_creds
.client
);
802 if(krb5_realm_compare(context
, in_creds
->client
, in_creds
->server
))
803 return not_found(context
, in_creds
->server
, KRB5_CC_NOTFOUND
);
805 /* XXX this can loop forever */
807 heim_general_string tgt_inst
;
809 ret
= get_cred_kdc_capath(context
, flags
, ccache
, &tmp_creds
,
810 NULL
, NULL
, &tgt
, ret_tgts
);
812 krb5_free_principal(context
, tmp_creds
.server
);
813 krb5_free_principal(context
, tmp_creds
.client
);
816 ret
= add_cred(context
, tgt
, ret_tgts
);
818 krb5_free_principal(context
, tmp_creds
.server
);
819 krb5_free_principal(context
, tmp_creds
.client
);
822 tgt_inst
= tgt
->server
->name
.name_string
.val
[1];
823 if(strcmp(tgt_inst
, server_realm
) == 0)
825 krb5_free_principal(context
, tmp_creds
.server
);
826 ret
= krb5_make_principal(context
, &tmp_creds
.server
,
827 tgt_inst
, KRB5_TGS_NAME
, server_realm
, NULL
);
829 krb5_free_principal(context
, tmp_creds
.server
);
830 krb5_free_principal(context
, tmp_creds
.client
);
833 ret
= krb5_free_creds(context
, tgt
);
835 krb5_free_principal(context
, tmp_creds
.server
);
836 krb5_free_principal(context
, tmp_creds
.client
);
841 krb5_free_principal(context
, tmp_creds
.server
);
842 krb5_free_principal(context
, tmp_creds
.client
);
843 *out_creds
= calloc(1, sizeof(**out_creds
));
844 if(*out_creds
== NULL
) {
846 krb5_set_error_message(context
, ret
, "malloc: out of memory");
848 ret
= get_cred_kdc_address (context
, ccache
, flags
, NULL
,
849 in_creds
, tgt
, impersonate_principal
,
850 second_ticket
, *out_creds
);
856 krb5_free_creds(context
, tgt
);
860 static krb5_error_code
861 get_cred_kdc_referral(krb5_context context
,
862 krb5_kdc_flags flags
,
864 krb5_creds
*in_creds
,
865 krb5_principal impersonate_principal
,
866 Ticket
*second_ticket
,
867 krb5_creds
**out_creds
,
868 krb5_creds
***ret_tgts
)
870 krb5_const_realm client_realm
;
872 krb5_creds tgt
, referral
, ticket
;
875 memset(&tgt
, 0, sizeof(tgt
));
876 memset(&ticket
, 0, sizeof(ticket
));
878 flags
.b
.canonicalize
= 1;
882 client_realm
= krb5_principal_get_realm(context
, in_creds
->client
);
884 /* find tgt for the clients base realm */
886 krb5_principal tgtname
;
888 ret
= krb5_make_principal(context
, &tgtname
,
896 ret
= find_cred(context
, ccache
, tgtname
, *ret_tgts
, &tgt
);
897 krb5_free_principal(context
, tgtname
);
902 referral
= *in_creds
;
903 ret
= krb5_copy_principal(context
, in_creds
->server
, &referral
.server
);
905 krb5_free_cred_contents(context
, &tgt
);
908 ret
= krb5_principal_set_realm(context
, referral
.server
, client_realm
);
910 krb5_free_cred_contents(context
, &tgt
);
911 krb5_free_principal(context
, referral
.server
);
915 while (loop
++ < 17) {
916 krb5_creds
**tickets
;
918 char *referral_realm
;
920 /* Use cache if we are not doing impersonation or contrainte deleg */
921 if (impersonate_principal
== NULL
|| flags
.b
.constrained_delegation
) {
922 krb5_cc_clear_mcred(&mcreds
);
923 mcreds
.server
= referral
.server
;
924 ret
= krb5_cc_retrieve_cred(context
, ccache
, 0, &mcreds
, &ticket
);
929 ret
= get_cred_kdc_address (context
, ccache
, flags
, NULL
,
930 &referral
, &tgt
, impersonate_principal
,
931 second_ticket
, &ticket
);
936 /* Did we get the right ticket ? */
937 if (krb5_principal_compare_any_realm(context
,
942 if (ticket
.server
->name
.name_string
.len
!= 2 &&
943 strcmp(ticket
.server
->name
.name_string
.val
[0], KRB5_TGS_NAME
) != 0)
945 krb5_set_error_message(context
, KRB5KRB_AP_ERR_NOT_US
,
946 "Got back an non krbtgt ticket referrals");
947 krb5_free_cred_contents(context
, &ticket
);
948 return KRB5KRB_AP_ERR_NOT_US
;
951 referral_realm
= ticket
.server
->name
.name_string
.val
[1];
953 /* check that there are no referrals loops */
956 krb5_cc_clear_mcred(&mcreds
);
957 mcreds
.server
= ticket
.server
;
959 while(tickets
&& *tickets
){
960 if(krb5_compare_creds(context
,
961 KRB5_TC_DONT_MATCH_REALM
,
965 krb5_set_error_message(context
, KRB5_GET_IN_TKT_LOOP
,
966 "Referral from %s loops back to realm %s",
969 krb5_free_cred_contents(context
, &ticket
);
970 return KRB5_GET_IN_TKT_LOOP
;
975 ret
= add_cred(context
, &ticket
, ret_tgts
);
977 krb5_free_cred_contents(context
, &ticket
);
981 /* try realm in the referral */
982 ret
= krb5_principal_set_realm(context
,
985 krb5_free_cred_contents(context
, &tgt
);
987 memset(&ticket
, 0, sizeof(ticket
));
992 ret
= krb5_copy_creds(context
, &ticket
, out_creds
);
995 krb5_free_principal(context
, referral
.server
);
996 krb5_free_cred_contents(context
, &tgt
);
1002 * Glue function between referrals version and old client chasing
1006 static krb5_error_code
1007 get_cred_kdc_any(krb5_context context
,
1008 krb5_kdc_flags flags
,
1010 krb5_creds
*in_creds
,
1011 krb5_principal impersonate_principal
,
1012 Ticket
*second_ticket
,
1013 krb5_creds
**out_creds
,
1014 krb5_creds
***ret_tgts
)
1016 krb5_error_code ret
;
1018 ret
= get_cred_kdc_referral(context
,
1022 impersonate_principal
,
1026 if (ret
== 0 || flags
.b
.canonicalize
)
1028 return get_cred_kdc_capath(context
,
1032 impersonate_principal
,
1039 krb5_error_code KRB5_LIB_FUNCTION
1040 krb5_get_cred_from_kdc_opt(krb5_context context
,
1042 krb5_creds
*in_creds
,
1043 krb5_creds
**out_creds
,
1044 krb5_creds
***ret_tgts
,
1049 return get_cred_kdc_any(context
, f
, ccache
,
1050 in_creds
, NULL
, NULL
,
1051 out_creds
, ret_tgts
);
1054 krb5_error_code KRB5_LIB_FUNCTION
1055 krb5_get_cred_from_kdc(krb5_context context
,
1057 krb5_creds
*in_creds
,
1058 krb5_creds
**out_creds
,
1059 krb5_creds
***ret_tgts
)
1061 return krb5_get_cred_from_kdc_opt(context
, ccache
,
1062 in_creds
, out_creds
, ret_tgts
, 0);
1066 krb5_error_code KRB5_LIB_FUNCTION
1067 krb5_get_credentials_with_flags(krb5_context context
,
1069 krb5_kdc_flags flags
,
1071 krb5_creds
*in_creds
,
1072 krb5_creds
**out_creds
)
1074 krb5_error_code ret
;
1076 krb5_creds
*res_creds
;
1080 res_creds
= calloc(1, sizeof(*res_creds
));
1081 if (res_creds
== NULL
) {
1082 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
1086 if (in_creds
->session
.keytype
)
1087 options
|= KRB5_TC_MATCH_KEYTYPE
;
1090 * If we got a credential, check if credential is expired before
1093 ret
= krb5_cc_retrieve_cred(context
,
1095 in_creds
->session
.keytype
?
1096 KRB5_TC_MATCH_KEYTYPE
: 0,
1097 in_creds
, res_creds
);
1099 * If we got a credential, check if credential is expired before
1100 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1103 krb5_timestamp timeret
;
1105 /* If expired ok, don't bother checking */
1106 if(options
& KRB5_GC_EXPIRED_OK
) {
1107 *out_creds
= res_creds
;
1111 krb5_timeofday(context
, &timeret
);
1112 if(res_creds
->times
.endtime
> timeret
) {
1113 *out_creds
= res_creds
;
1116 if(options
& KRB5_GC_CACHED
)
1117 krb5_cc_remove_cred(context
, ccache
, 0, res_creds
);
1119 } else if(ret
!= KRB5_CC_END
) {
1124 if(options
& KRB5_GC_CACHED
)
1125 return not_found(context
, in_creds
->server
, KRB5_CC_NOTFOUND
);
1127 if(options
& KRB5_GC_USER_USER
)
1128 flags
.b
.enc_tkt_in_skey
= 1;
1129 if (flags
.b
.enc_tkt_in_skey
)
1130 options
|= KRB5_GC_NO_STORE
;
1133 ret
= get_cred_kdc_any(context
, flags
, ccache
,
1134 in_creds
, NULL
, NULL
, out_creds
, &tgts
);
1135 for(i
= 0; tgts
&& tgts
[i
]; i
++) {
1136 krb5_cc_store_cred(context
, ccache
, tgts
[i
]);
1137 krb5_free_creds(context
, tgts
[i
]);
1140 if(ret
== 0 && (options
& KRB5_GC_NO_STORE
) == 0)
1141 krb5_cc_store_cred(context
, ccache
, *out_creds
);
1145 krb5_error_code KRB5_LIB_FUNCTION
1146 krb5_get_credentials(krb5_context context
,
1149 krb5_creds
*in_creds
,
1150 krb5_creds
**out_creds
)
1152 krb5_kdc_flags flags
;
1154 return krb5_get_credentials_with_flags(context
, options
, flags
,
1155 ccache
, in_creds
, out_creds
);
1158 struct krb5_get_creds_opt_data
{
1159 krb5_principal self
;
1161 krb5_enctype enctype
;
1166 krb5_error_code KRB5_LIB_FUNCTION
1167 krb5_get_creds_opt_alloc(krb5_context context
, krb5_get_creds_opt
*opt
)
1169 *opt
= calloc(1, sizeof(**opt
));
1171 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
1177 void KRB5_LIB_FUNCTION
1178 krb5_get_creds_opt_free(krb5_context context
, krb5_get_creds_opt opt
)
1181 krb5_free_principal(context
, opt
->self
);
1182 memset(opt
, 0, sizeof(*opt
));
1186 void KRB5_LIB_FUNCTION
1187 krb5_get_creds_opt_set_options(krb5_context context
,
1188 krb5_get_creds_opt opt
,
1191 opt
->options
= options
;
1194 void KRB5_LIB_FUNCTION
1195 krb5_get_creds_opt_add_options(krb5_context context
,
1196 krb5_get_creds_opt opt
,
1199 opt
->options
|= options
;
1202 void KRB5_LIB_FUNCTION
1203 krb5_get_creds_opt_set_enctype(krb5_context context
,
1204 krb5_get_creds_opt opt
,
1205 krb5_enctype enctype
)
1207 opt
->enctype
= enctype
;
1210 krb5_error_code KRB5_LIB_FUNCTION
1211 krb5_get_creds_opt_set_impersonate(krb5_context context
,
1212 krb5_get_creds_opt opt
,
1213 krb5_const_principal self
)
1216 krb5_free_principal(context
, opt
->self
);
1217 return krb5_copy_principal(context
, self
, &opt
->self
);
1220 krb5_error_code KRB5_LIB_FUNCTION
1221 krb5_get_creds_opt_set_ticket(krb5_context context
,
1222 krb5_get_creds_opt opt
,
1223 const Ticket
*ticket
)
1226 free_Ticket(opt
->ticket
);
1231 krb5_error_code ret
;
1233 opt
->ticket
= malloc(sizeof(*ticket
));
1234 if (opt
->ticket
== NULL
) {
1235 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
1238 ret
= copy_Ticket(ticket
, opt
->ticket
);
1242 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1251 krb5_error_code KRB5_LIB_FUNCTION
1252 krb5_get_creds(krb5_context context
,
1253 krb5_get_creds_opt opt
,
1255 krb5_const_principal inprinc
,
1256 krb5_creds
**out_creds
)
1258 krb5_kdc_flags flags
;
1260 krb5_creds in_creds
;
1261 krb5_error_code ret
;
1263 krb5_creds
*res_creds
;
1266 memset(&in_creds
, 0, sizeof(in_creds
));
1267 in_creds
.server
= rk_UNCONST(inprinc
);
1269 ret
= krb5_cc_get_principal(context
, ccache
, &in_creds
.client
);
1273 options
= opt
->options
;
1277 res_creds
= calloc(1, sizeof(*res_creds
));
1278 if (res_creds
== NULL
) {
1279 krb5_free_principal(context
, in_creds
.client
);
1280 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
1285 in_creds
.session
.keytype
= opt
->enctype
;
1286 options
|= KRB5_TC_MATCH_KEYTYPE
;
1290 * If we got a credential, check if credential is expired before
1293 ret
= krb5_cc_retrieve_cred(context
,
1295 opt
->enctype
? KRB5_TC_MATCH_KEYTYPE
: 0,
1296 &in_creds
, res_creds
);
1298 * If we got a credential, check if credential is expired before
1299 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1302 krb5_timestamp timeret
;
1304 /* If expired ok, don't bother checking */
1305 if(options
& KRB5_GC_EXPIRED_OK
) {
1306 *out_creds
= res_creds
;
1307 krb5_free_principal(context
, in_creds
.client
);
1311 krb5_timeofday(context
, &timeret
);
1312 if(res_creds
->times
.endtime
> timeret
) {
1313 *out_creds
= res_creds
;
1314 krb5_free_principal(context
, in_creds
.client
);
1317 if(options
& KRB5_GC_CACHED
)
1318 krb5_cc_remove_cred(context
, ccache
, 0, res_creds
);
1320 } else if(ret
!= KRB5_CC_END
) {
1322 krb5_free_principal(context
, in_creds
.client
);
1326 if(options
& KRB5_GC_CACHED
) {
1327 krb5_free_principal(context
, in_creds
.client
);
1328 return not_found(context
, in_creds
.server
, KRB5_CC_NOTFOUND
);
1330 if(options
& KRB5_GC_USER_USER
) {
1331 flags
.b
.enc_tkt_in_skey
= 1;
1332 options
|= KRB5_GC_NO_STORE
;
1334 if (options
& KRB5_GC_FORWARDABLE
)
1335 flags
.b
.forwardable
= 1;
1336 if (options
& KRB5_GC_NO_TRANSIT_CHECK
)
1337 flags
.b
.disable_transited_check
= 1;
1338 if (options
& KRB5_GC_CONSTRAINED_DELEGATION
) {
1339 flags
.b
.request_anonymous
= 1; /* XXX ARGH confusion */
1340 flags
.b
.constrained_delegation
= 1;
1342 if (options
& KRB5_GC_CANONICALIZE
)
1343 flags
.b
.canonicalize
= 1;
1346 ret
= get_cred_kdc_any(context
, flags
, ccache
,
1347 &in_creds
, opt
->self
, opt
->ticket
,
1349 krb5_free_principal(context
, in_creds
.client
);
1350 for(i
= 0; tgts
&& tgts
[i
]; i
++) {
1351 krb5_cc_store_cred(context
, ccache
, tgts
[i
]);
1352 krb5_free_creds(context
, tgts
[i
]);
1355 if(ret
== 0 && (options
& KRB5_GC_NO_STORE
) == 0)
1356 krb5_cc_store_cred(context
, ccache
, *out_creds
);
1364 krb5_error_code KRB5_LIB_FUNCTION
1365 krb5_get_renewed_creds(krb5_context context
,
1367 krb5_const_principal client
,
1369 const char *in_tkt_service
)
1371 krb5_error_code ret
;
1372 krb5_kdc_flags flags
;
1373 krb5_creds in
, *template, *out
= NULL
;
1375 memset(&in
, 0, sizeof(in
));
1376 memset(creds
, 0, sizeof(*creds
));
1378 ret
= krb5_copy_principal(context
, client
, &in
.client
);
1382 if (in_tkt_service
) {
1383 ret
= krb5_parse_name(context
, in_tkt_service
, &in
.server
);
1385 krb5_free_principal(context
, in
.client
);
1389 const char *realm
= krb5_principal_get_realm(context
, client
);
1391 ret
= krb5_make_principal(context
, &in
.server
, realm
, KRB5_TGS_NAME
,
1394 krb5_free_principal(context
, in
.client
);
1400 flags
.b
.renewable
= flags
.b
.renew
= 1;
1403 * Get template from old credential cache for the same entry, if
1404 * this failes, no worries.
1406 ret
= krb5_get_credentials(context
, KRB5_GC_CACHED
, ccache
, &in
, &template);
1408 flags
.b
.forwardable
= template->flags
.b
.forwardable
;
1409 flags
.b
.proxiable
= template->flags
.b
.proxiable
;
1410 krb5_free_creds (context
, template);
1413 ret
= krb5_get_kdc_cred(context
, ccache
, flags
, NULL
, NULL
, &in
, &out
);
1414 krb5_free_principal(context
, in
.client
);
1415 krb5_free_principal(context
, in
.server
);
1419 ret
= krb5_copy_creds_contents(context
, out
, creds
);
1420 krb5_free_creds(context
, out
);