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"
39 static krb5_error_code
40 get_cred_kdc_capath(krb5_context
, krb5_kdc_flags
,
41 krb5_ccache
, krb5_creds
*, krb5_principal
,
42 Ticket
*, krb5_creds
**, krb5_creds
***);
45 * Take the `body' and encode it into `padata' using the credentials
49 static krb5_error_code
50 make_pa_tgs_req(krb5_context context
,
62 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, body
, &len
, ret
);
66 krb5_abortx(context
, "internal error in ASN.1 encoder");
70 ret
= _krb5_mk_req_internal(context
, &ac
, 0, &in_data
, creds
,
71 &padata
->padata_value
,
72 KRB5_KU_TGS_REQ_AUTH_CKSUM
,
73 KRB5_KU_TGS_REQ_AUTH
);
78 padata
->padata_type
= KRB5_PADATA_TGS_REQ
;
83 * Set the `enc-authorization-data' in `req_body' based on `authdata'
86 static krb5_error_code
87 set_auth_data (krb5_context context
,
88 KDC_REQ_BODY
*req_body
,
89 krb5_authdata
*authdata
,
90 krb5_keyblock
*subkey
)
93 size_t len
= 0, buf_size
;
98 ASN1_MALLOC_ENCODE(AuthorizationData
, buf
, buf_size
, authdata
,
103 krb5_abortx(context
, "internal error in ASN.1 encoder");
105 ALLOC(req_body
->enc_authorization_data
, 1);
106 if (req_body
->enc_authorization_data
== NULL
) {
108 krb5_set_error_message(context
, ENOMEM
,
109 N_("malloc: out of memory", ""));
112 ret
= krb5_crypto_init(context
, subkey
, 0, &crypto
);
115 free (req_body
->enc_authorization_data
);
116 req_body
->enc_authorization_data
= NULL
;
119 krb5_encrypt_EncryptedData(context
,
121 KRB5_KU_TGS_REQ_AUTH_DAT_SUBKEY
,
125 req_body
->enc_authorization_data
);
127 krb5_crypto_destroy(context
, crypto
);
129 req_body
->enc_authorization_data
= NULL
;
135 * Create a tgs-req in `t' with `addresses', `flags', `second_ticket'
136 * (if not-NULL), `in_creds', `krbtgt', and returning the generated
137 * subkey in `subkey'.
140 static krb5_error_code
141 init_tgs_req (krb5_context context
,
143 krb5_addresses
*addresses
,
144 krb5_kdc_flags flags
,
145 Ticket
*second_ticket
,
146 krb5_creds
*in_creds
,
149 const METHOD_DATA
*padata
,
150 krb5_keyblock
**subkey
,
153 krb5_auth_context ac
= NULL
;
154 krb5_error_code ret
= 0;
156 memset(t
, 0, sizeof(*t
));
158 t
->msg_type
= krb_tgs_req
;
159 if (in_creds
->session
.keytype
) {
160 ALLOC_SEQ(&t
->req_body
.etype
, 1);
161 if(t
->req_body
.etype
.val
== NULL
) {
163 krb5_set_error_message(context
, ret
,
164 N_("malloc: out of memory", ""));
167 t
->req_body
.etype
.val
[0] = in_creds
->session
.keytype
;
169 ret
= _krb5_init_etype(context
,
170 KRB5_PDU_TGS_REQUEST
,
171 &t
->req_body
.etype
.len
,
172 &t
->req_body
.etype
.val
,
177 t
->req_body
.addresses
= addresses
;
178 t
->req_body
.kdc_options
= flags
.b
;
179 ret
= copy_Realm(&in_creds
->server
->realm
, &t
->req_body
.realm
);
182 ALLOC(t
->req_body
.sname
, 1);
183 if (t
->req_body
.sname
== NULL
) {
185 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
189 /* some versions of some code might require that the client be
190 present in TGS-REQs, but this is clearly against the spec */
192 ret
= copy_PrincipalName(&in_creds
->server
->name
, t
->req_body
.sname
);
196 /* req_body.till should be NULL if there is no endtime specified,
197 but old MIT code (like DCE secd) doesn't like that */
198 ALLOC(t
->req_body
.till
, 1);
199 if(t
->req_body
.till
== NULL
){
201 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
204 *t
->req_body
.till
= in_creds
->times
.endtime
;
206 t
->req_body
.nonce
= nonce
;
208 ALLOC(t
->req_body
.additional_tickets
, 1);
209 if (t
->req_body
.additional_tickets
== NULL
) {
211 krb5_set_error_message(context
, ret
,
212 N_("malloc: out of memory", ""));
215 ALLOC_SEQ(t
->req_body
.additional_tickets
, 1);
216 if (t
->req_body
.additional_tickets
->val
== NULL
) {
218 krb5_set_error_message(context
, ret
,
219 N_("malloc: out of memory", ""));
222 ret
= copy_Ticket(second_ticket
, t
->req_body
.additional_tickets
->val
);
227 if (t
->padata
== NULL
) {
229 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
232 ALLOC_SEQ(t
->padata
, 1 + padata
->len
);
233 if (t
->padata
->val
== NULL
) {
235 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
240 for (i
= 0; i
< padata
->len
; i
++) {
241 ret
= copy_PA_DATA(&padata
->val
[i
], &t
->padata
->val
[i
+ 1]);
243 krb5_set_error_message(context
, ret
,
244 N_("malloc: out of memory", ""));
250 ret
= krb5_auth_con_init(context
, &ac
);
254 ret
= krb5_auth_con_generatelocalsubkey(context
, ac
, &krbtgt
->session
);
258 ret
= set_auth_data (context
, &t
->req_body
, &in_creds
->authdata
,
263 ret
= make_pa_tgs_req(context
,
271 ret
= krb5_auth_con_getlocalsubkey(context
, ac
, subkey
);
277 krb5_auth_con_free(context
, ac
);
279 t
->req_body
.addresses
= NULL
;
286 _krb5_get_krbtgt(krb5_context context
,
294 memset(&tmp_cred
, 0, sizeof(tmp_cred
));
296 ret
= krb5_cc_get_principal(context
, id
, &tmp_cred
.client
);
300 ret
= krb5_make_principal(context
,
307 krb5_free_principal(context
, tmp_cred
.client
);
310 ret
= krb5_get_credentials(context
,
315 krb5_free_principal(context
, tmp_cred
.client
);
316 krb5_free_principal(context
, tmp_cred
.server
);
322 /* DCE compatible decrypt proc */
323 static krb5_error_code KRB5_CALLCONV
324 decrypt_tkt_with_subkey (krb5_context context
,
326 krb5_key_usage usage
,
327 krb5_const_pointer skey
,
328 krb5_kdc_rep
*dec_rep
)
330 const krb5_keyblock
*subkey
= skey
;
331 krb5_error_code ret
= 0;
338 krb5_data_zero(&data
);
341 * start out with trying with subkey if we have one
344 ret
= krb5_crypto_init(context
, subkey
, 0, &crypto
);
347 ret
= krb5_decrypt_EncryptedData (context
,
349 KRB5_KU_TGS_REP_ENC_PART_SUB_KEY
,
350 &dec_rep
->kdc_rep
.enc_part
,
353 * If the is Windows 2000 DC, we need to retry with key usage
354 * 8 when doing ARCFOUR.
356 if (ret
&& subkey
->keytype
== ETYPE_ARCFOUR_HMAC_MD5
) {
357 ret
= krb5_decrypt_EncryptedData(context
,
360 &dec_rep
->kdc_rep
.enc_part
,
363 krb5_crypto_destroy(context
, crypto
);
365 if (subkey
== NULL
|| ret
) {
366 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
369 ret
= krb5_decrypt_EncryptedData (context
,
371 KRB5_KU_TGS_REP_ENC_PART_SESSION
,
372 &dec_rep
->kdc_rep
.enc_part
,
374 krb5_crypto_destroy(context
, crypto
);
379 ret
= decode_EncASRepPart(data
.data
,
384 ret
= decode_EncTGSRepPart(data
.data
,
389 krb5_set_error_message(context
, ret
,
390 N_("Failed to decode encpart in ticket", ""));
391 krb5_data_free (&data
);
395 static krb5_error_code
396 get_cred_kdc(krb5_context context
,
398 krb5_kdc_flags flags
,
399 krb5_addresses
*addresses
,
400 krb5_creds
*in_creds
,
402 krb5_principal impersonate_principal
,
403 Ticket
*second_ticket
,
404 krb5_creds
*out_creds
)
413 krb5_keyblock
*subkey
= NULL
;
415 Ticket second_ticket_data
;
418 krb5_data_zero(&resp
);
419 krb5_data_zero(&enc
);
423 krb5_generate_random_block(&nonce
, sizeof(nonce
));
426 if(flags
.b
.enc_tkt_in_skey
&& second_ticket
== NULL
){
427 ret
= decode_Ticket(in_creds
->second_ticket
.data
,
428 in_creds
->second_ticket
.length
,
429 &second_ticket_data
, &len
);
432 second_ticket
= &second_ticket_data
;
436 if (impersonate_principal
) {
443 self
.name
= impersonate_principal
->name
;
444 self
.realm
= impersonate_principal
->realm
;
445 self
.auth
= estrdup("Kerberos");
447 ret
= _krb5_s4u2self_to_checksumdata(context
, &self
, &data
);
453 ret
= krb5_crypto_init(context
, &krbtgt
->session
, 0, &crypto
);
456 krb5_data_free(&data
);
460 ret
= krb5_create_checksum(context
,
467 krb5_crypto_destroy(context
, crypto
);
468 krb5_data_free(&data
);
474 ASN1_MALLOC_ENCODE(PA_S4U2Self
, buf
, len
, &self
, &size
, ret
);
476 free_Checksum(&self
.cksum
);
480 krb5_abortx(context
, "internal asn1 error");
482 ret
= krb5_padata_add(context
, &padata
, KRB5_PADATA_FOR_USER
, buf
, len
);
487 ret
= init_tgs_req (context
,
501 ASN1_MALLOC_ENCODE(TGS_REQ
, enc
.data
, enc
.length
, &req
, &len
, ret
);
504 if(enc
.length
!= len
)
505 krb5_abortx(context
, "internal error in ASN.1 encoder");
507 /* don't free addresses */
508 req
.req_body
.addresses
= NULL
;
515 krb5_sendto_ctx stctx
;
516 ret
= krb5_sendto_ctx_alloc(context
, &stctx
);
519 krb5_sendto_ctx_set_func(stctx
, _krb5_kdc_retry
, NULL
);
521 ret
= krb5_sendto_context (context
, stctx
, &enc
,
522 krbtgt
->server
->name
.name_string
.val
[1],
524 krb5_sendto_ctx_free(context
, stctx
);
529 memset(&rep
, 0, sizeof(rep
));
530 if(decode_TGS_REP(resp
.data
, resp
.length
, &rep
.kdc_rep
, &len
) == 0) {
533 ret
= krb5_copy_principal(context
,
538 ret
= krb5_copy_principal(context
,
543 /* this should go someplace else */
544 out_creds
->times
.endtime
= in_creds
->times
.endtime
;
546 /* XXX should do better testing */
547 if (flags
.b
.constrained_delegation
|| impersonate_principal
)
548 eflags
|= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH
;
550 ret
= _krb5_extract_ticket(context
,
559 decrypt_tkt_with_subkey
,
562 krb5_free_kdc_rep(context
, &rep
);
563 } else if(krb5_rd_error(context
, &resp
, &error
) == 0) {
564 ret
= krb5_error_from_rd_error(context
, &error
, in_creds
);
565 krb5_free_error_contents(context
, &error
);
566 } else if(resp
.length
> 0 && ((char*)resp
.data
)[0] == 4) {
567 ret
= KRB5KRB_AP_ERR_V4_REPLY
;
568 krb5_clear_error_message(context
);
570 ret
= KRB5KRB_AP_ERR_MSG_TYPE
;
571 krb5_clear_error_message(context
);
575 if (second_ticket
== &second_ticket_data
)
576 free_Ticket(&second_ticket_data
);
577 free_METHOD_DATA(&padata
);
578 krb5_data_free(&resp
);
579 krb5_data_free(&enc
);
581 krb5_free_keyblock(context
, subkey
);
587 * same as above, just get local addresses first if the krbtgt have
588 * them and the realm is not addressless
591 static krb5_error_code
592 get_cred_kdc_address(krb5_context context
,
594 krb5_kdc_flags flags
,
595 krb5_addresses
*addrs
,
596 krb5_creds
*in_creds
,
598 krb5_principal impersonate_principal
,
599 Ticket
*second_ticket
,
600 krb5_creds
*out_creds
)
603 krb5_addresses addresses
= { 0, NULL
};
606 * Inherit the address-ness of the krbtgt if the address is not
610 if (addrs
== NULL
&& krbtgt
->addresses
.len
!= 0) {
613 krb5_appdefault_boolean(context
, NULL
, krbtgt
->server
->realm
,
614 "no-addresses", FALSE
, &noaddr
);
617 krb5_get_all_client_addrs(context
, &addresses
);
618 /* XXX this sucks. */
620 if(addresses
.len
== 0)
624 ret
= get_cred_kdc(context
, id
, flags
, addrs
, in_creds
,
625 krbtgt
, impersonate_principal
,
626 second_ticket
, out_creds
);
627 krb5_free_addresses(context
, &addresses
);
631 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
632 krb5_get_kdc_cred(krb5_context context
,
634 krb5_kdc_flags flags
,
635 krb5_addresses
*addresses
,
636 Ticket
*second_ticket
,
637 krb5_creds
*in_creds
,
638 krb5_creds
**out_creds
644 *out_creds
= calloc(1, sizeof(**out_creds
));
645 if(*out_creds
== NULL
) {
646 krb5_set_error_message(context
, ENOMEM
,
647 N_("malloc: out of memory", ""));
650 ret
= _krb5_get_krbtgt (context
,
652 in_creds
->server
->realm
,
659 ret
= get_cred_kdc(context
, id
, flags
, addresses
,
660 in_creds
, krbtgt
, NULL
, NULL
, *out_creds
);
661 krb5_free_creds (context
, krbtgt
);
670 not_found(krb5_context context
, krb5_const_principal p
, krb5_error_code code
)
675 ret
= krb5_unparse_name(context
, p
, &str
);
677 krb5_clear_error_message(context
);
680 krb5_set_error_message(context
, code
,
681 N_("Matching credential (%s) not found", ""), str
);
686 static krb5_error_code
687 find_cred(krb5_context context
,
689 krb5_principal server
,
691 krb5_creds
*out_creds
)
696 krb5_cc_clear_mcred(&mcreds
);
697 mcreds
.server
= server
;
698 ret
= krb5_cc_retrieve_cred(context
, id
, KRB5_TC_DONT_MATCH_REALM
,
702 while(tgts
&& *tgts
){
703 if(krb5_compare_creds(context
, KRB5_TC_DONT_MATCH_REALM
,
705 ret
= krb5_copy_creds_contents(context
, *tgts
, out_creds
);
710 return not_found(context
, server
, KRB5_CC_NOTFOUND
);
713 static krb5_error_code
714 add_cred(krb5_context context
, krb5_creds
const *tkt
, krb5_creds
***tgts
)
718 krb5_creds
**tmp
= *tgts
;
720 for(i
= 0; tmp
&& tmp
[i
]; i
++); /* XXX */
721 tmp
= realloc(tmp
, (i
+2)*sizeof(*tmp
));
723 krb5_set_error_message(context
, ENOMEM
,
724 N_("malloc: out of memory", ""));
728 ret
= krb5_copy_creds(context
, tkt
, &tmp
[i
]);
733 static krb5_error_code
734 get_cred_kdc_capath_worker(krb5_context context
,
735 krb5_kdc_flags flags
,
737 krb5_creds
*in_creds
,
738 krb5_const_realm try_realm
,
739 krb5_principal impersonate_principal
,
740 Ticket
*second_ticket
,
741 krb5_creds
**out_creds
,
742 krb5_creds
***ret_tgts
)
745 krb5_creds
*tgt
, tmp_creds
;
746 krb5_const_realm client_realm
, server_realm
;
747 int ok_as_delegate
= 1;
751 client_realm
= krb5_principal_get_realm(context
, in_creds
->client
);
752 server_realm
= krb5_principal_get_realm(context
, in_creds
->server
);
753 memset(&tmp_creds
, 0, sizeof(tmp_creds
));
754 ret
= krb5_copy_principal(context
, in_creds
->client
, &tmp_creds
.client
);
758 ret
= krb5_make_principal(context
,
765 krb5_free_principal(context
, tmp_creds
.client
);
771 ret
= find_cred(context
, ccache
, tmp_creds
.server
,
774 /* only allow implicit ok_as_delegate if the realm is the clients realm */
775 if (strcmp(try_realm
, client_realm
) != 0 || strcmp(try_realm
, server_realm
) != 0)
776 ok_as_delegate
= tgts
.flags
.b
.ok_as_delegate
;
778 *out_creds
= calloc(1, sizeof(**out_creds
));
779 if(*out_creds
== NULL
) {
781 krb5_set_error_message(context
, ret
,
782 N_("malloc: out of memory", ""));
784 ret
= get_cred_kdc_address(context
, ccache
, flags
, NULL
,
786 impersonate_principal
,
792 } else if (ok_as_delegate
== 0)
793 (*out_creds
)->flags
.b
.ok_as_delegate
= 0;
795 krb5_free_cred_contents(context
, &tgts
);
796 krb5_free_principal(context
, tmp_creds
.server
);
797 krb5_free_principal(context
, tmp_creds
.client
);
801 if(krb5_realm_compare(context
, in_creds
->client
, in_creds
->server
))
802 return not_found(context
, in_creds
->server
, KRB5_CC_NOTFOUND
);
804 /* XXX this can loop forever */
806 heim_general_string tgt_inst
;
808 ret
= get_cred_kdc_capath(context
, flags
, ccache
, &tmp_creds
,
809 NULL
, NULL
, &tgt
, ret_tgts
);
811 krb5_free_principal(context
, tmp_creds
.server
);
812 krb5_free_principal(context
, tmp_creds
.client
);
816 * if either of the chain or the ok_as_delegate was stripped
817 * by the kdc, make sure we strip it too.
819 if (ok_as_delegate
== 0 || tgt
->flags
.b
.ok_as_delegate
== 0) {
821 tgt
->flags
.b
.ok_as_delegate
= 0;
824 ret
= add_cred(context
, tgt
, ret_tgts
);
826 krb5_free_principal(context
, tmp_creds
.server
);
827 krb5_free_principal(context
, tmp_creds
.client
);
830 tgt_inst
= tgt
->server
->name
.name_string
.val
[1];
831 if(strcmp(tgt_inst
, server_realm
) == 0)
833 krb5_free_principal(context
, tmp_creds
.server
);
834 ret
= krb5_make_principal(context
, &tmp_creds
.server
,
835 tgt_inst
, KRB5_TGS_NAME
, server_realm
, NULL
);
837 krb5_free_principal(context
, tmp_creds
.server
);
838 krb5_free_principal(context
, tmp_creds
.client
);
841 ret
= krb5_free_creds(context
, tgt
);
843 krb5_free_principal(context
, tmp_creds
.server
);
844 krb5_free_principal(context
, tmp_creds
.client
);
849 krb5_free_principal(context
, tmp_creds
.server
);
850 krb5_free_principal(context
, tmp_creds
.client
);
851 *out_creds
= calloc(1, sizeof(**out_creds
));
852 if(*out_creds
== NULL
) {
854 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
856 ret
= get_cred_kdc_address (context
, ccache
, flags
, NULL
,
857 in_creds
, tgt
, impersonate_principal
,
858 second_ticket
, *out_creds
);
864 krb5_free_creds(context
, tgt
);
870 creds = cc_get_cred(server)
871 if(creds) return creds
872 tgt = cc_get_cred(krbtgt/server_realm@any_realm)
874 return get_cred_tgt(server, tgt)
875 if(client_realm == server_realm)
877 tgt = get_cred(krbtgt/server_realm@client_realm)
878 while(tgt_inst != server_realm)
879 tgt = get_cred(krbtgt/server_realm@tgt_inst)
880 return get_cred_tgt(server, tgt)
883 static krb5_error_code
884 get_cred_kdc_capath(krb5_context context
,
885 krb5_kdc_flags flags
,
887 krb5_creds
*in_creds
,
888 krb5_principal impersonate_principal
,
889 Ticket
*second_ticket
,
890 krb5_creds
**out_creds
,
891 krb5_creds
***ret_tgts
)
894 krb5_const_realm client_realm
, server_realm
, try_realm
;
896 client_realm
= krb5_principal_get_realm(context
, in_creds
->client
);
897 server_realm
= krb5_principal_get_realm(context
, in_creds
->server
);
899 try_realm
= client_realm
;
900 ret
= get_cred_kdc_capath_worker(context
, flags
, ccache
, in_creds
, try_realm
,
901 impersonate_principal
, second_ticket
, out_creds
,
904 if (ret
== KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
) {
905 try_realm
= krb5_config_get_string(context
, NULL
, "capaths",
906 client_realm
, server_realm
, NULL
);
908 if (try_realm
!= NULL
&& strcmp(try_realm
, client_realm
)) {
909 ret
= get_cred_kdc_capath_worker(context
, flags
, ccache
, in_creds
,
910 try_realm
, impersonate_principal
,
911 second_ticket
, out_creds
, ret_tgts
);
918 static krb5_error_code
919 get_cred_kdc_referral(krb5_context context
,
920 krb5_kdc_flags flags
,
922 krb5_creds
*in_creds
,
923 krb5_principal impersonate_principal
,
924 Ticket
*second_ticket
,
925 krb5_creds
**out_creds
,
926 krb5_creds
***ret_tgts
)
928 krb5_const_realm client_realm
;
930 krb5_creds tgt
, referral
, ticket
;
932 int ok_as_delegate
= 1;
934 if (in_creds
->server
->name
.name_string
.len
< 2 && !flags
.b
.canonicalize
) {
935 krb5_set_error_message(context
, KRB5KDC_ERR_PATH_NOT_ACCEPTED
,
936 N_("Name too short to do referals, skipping", ""));
937 return KRB5KDC_ERR_PATH_NOT_ACCEPTED
;
940 memset(&tgt
, 0, sizeof(tgt
));
941 memset(&ticket
, 0, sizeof(ticket
));
943 flags
.b
.canonicalize
= 1;
947 client_realm
= krb5_principal_get_realm(context
, in_creds
->client
);
949 /* find tgt for the clients base realm */
951 krb5_principal tgtname
;
953 ret
= krb5_make_principal(context
, &tgtname
,
961 ret
= find_cred(context
, ccache
, tgtname
, *ret_tgts
, &tgt
);
962 krb5_free_principal(context
, tgtname
);
967 referral
= *in_creds
;
968 ret
= krb5_copy_principal(context
, in_creds
->server
, &referral
.server
);
970 krb5_free_cred_contents(context
, &tgt
);
973 ret
= krb5_principal_set_realm(context
, referral
.server
, client_realm
);
975 krb5_free_cred_contents(context
, &tgt
);
976 krb5_free_principal(context
, referral
.server
);
980 while (loop
++ < 17) {
981 krb5_creds
**tickets
;
983 char *referral_realm
;
985 /* Use cache if we are not doing impersonation or contrainte deleg */
986 if (impersonate_principal
== NULL
|| flags
.b
.constrained_delegation
) {
987 krb5_cc_clear_mcred(&mcreds
);
988 mcreds
.server
= referral
.server
;
989 ret
= krb5_cc_retrieve_cred(context
, ccache
, 0, &mcreds
, &ticket
);
994 ret
= get_cred_kdc_address(context
, ccache
, flags
, NULL
,
995 &referral
, &tgt
, impersonate_principal
,
996 second_ticket
, &ticket
);
1001 /* Did we get the right ticket ? */
1002 if (krb5_principal_compare_any_realm(context
,
1007 if (!krb5_principal_is_krbtgt(context
, ticket
.server
)) {
1008 krb5_set_error_message(context
, KRB5KRB_AP_ERR_NOT_US
,
1009 N_("Got back an non krbtgt "
1010 "ticket referrals", ""));
1011 ret
= KRB5KRB_AP_ERR_NOT_US
;
1015 referral_realm
= ticket
.server
->name
.name_string
.val
[1];
1017 /* check that there are no referrals loops */
1018 tickets
= *ret_tgts
;
1020 krb5_cc_clear_mcred(&mcreds
);
1021 mcreds
.server
= ticket
.server
;
1023 while(tickets
&& *tickets
){
1024 if(krb5_compare_creds(context
,
1025 KRB5_TC_DONT_MATCH_REALM
,
1029 krb5_set_error_message(context
, KRB5_GET_IN_TKT_LOOP
,
1030 N_("Referral from %s "
1031 "loops back to realm %s", ""),
1034 ret
= KRB5_GET_IN_TKT_LOOP
;
1041 * if either of the chain or the ok_as_delegate was stripped
1042 * by the kdc, make sure we strip it too.
1045 if (ok_as_delegate
== 0 || ticket
.flags
.b
.ok_as_delegate
== 0) {
1047 ticket
.flags
.b
.ok_as_delegate
= 0;
1050 ret
= add_cred(context
, &ticket
, ret_tgts
);
1054 /* try realm in the referral */
1055 ret
= krb5_principal_set_realm(context
,
1058 krb5_free_cred_contents(context
, &tgt
);
1060 memset(&ticket
, 0, sizeof(ticket
));
1065 ret
= krb5_copy_creds(context
, &ticket
, out_creds
);
1068 krb5_free_principal(context
, referral
.server
);
1069 krb5_free_cred_contents(context
, &tgt
);
1070 krb5_free_cred_contents(context
, &ticket
);
1076 * Glue function between referrals version and old client chasing
1081 _krb5_get_cred_kdc_any(krb5_context context
,
1082 krb5_kdc_flags flags
,
1084 krb5_creds
*in_creds
,
1085 krb5_principal impersonate_principal
,
1086 Ticket
*second_ticket
,
1087 krb5_creds
**out_creds
,
1088 krb5_creds
***ret_tgts
)
1090 krb5_error_code ret
;
1093 ret
= krb5_cc_get_kdc_offset(context
, ccache
, &offset
);
1095 context
->kdc_sec_offset
= offset
;
1096 context
->kdc_usec_offset
= 0;
1099 ret
= get_cred_kdc_referral(context
,
1103 impersonate_principal
,
1107 if (ret
== 0 || flags
.b
.canonicalize
)
1109 return get_cred_kdc_capath(context
,
1113 impersonate_principal
,
1120 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1121 krb5_get_credentials_with_flags(krb5_context context
,
1123 krb5_kdc_flags flags
,
1125 krb5_creds
*in_creds
,
1126 krb5_creds
**out_creds
)
1128 krb5_error_code ret
;
1130 krb5_creds
*res_creds
;
1133 if (in_creds
->session
.keytype
) {
1134 ret
= krb5_enctype_valid(context
, in_creds
->session
.keytype
);
1140 res_creds
= calloc(1, sizeof(*res_creds
));
1141 if (res_creds
== NULL
) {
1142 krb5_set_error_message(context
, ENOMEM
,
1143 N_("malloc: out of memory", ""));
1147 if (in_creds
->session
.keytype
)
1148 options
|= KRB5_TC_MATCH_KEYTYPE
;
1151 * If we got a credential, check if credential is expired before
1154 ret
= krb5_cc_retrieve_cred(context
,
1156 in_creds
->session
.keytype
?
1157 KRB5_TC_MATCH_KEYTYPE
: 0,
1158 in_creds
, res_creds
);
1160 * If we got a credential, check if credential is expired before
1161 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1164 krb5_timestamp timeret
;
1166 /* If expired ok, don't bother checking */
1167 if(options
& KRB5_GC_EXPIRED_OK
) {
1168 *out_creds
= res_creds
;
1172 krb5_timeofday(context
, &timeret
);
1173 if(res_creds
->times
.endtime
> timeret
) {
1174 *out_creds
= res_creds
;
1177 if(options
& KRB5_GC_CACHED
)
1178 krb5_cc_remove_cred(context
, ccache
, 0, res_creds
);
1180 } else if(ret
!= KRB5_CC_END
) {
1185 if(options
& KRB5_GC_CACHED
)
1186 return not_found(context
, in_creds
->server
, KRB5_CC_NOTFOUND
);
1188 if(options
& KRB5_GC_USER_USER
)
1189 flags
.b
.enc_tkt_in_skey
= 1;
1190 if (flags
.b
.enc_tkt_in_skey
)
1191 options
|= KRB5_GC_NO_STORE
;
1194 ret
= _krb5_get_cred_kdc_any(context
, flags
, ccache
,
1195 in_creds
, NULL
, NULL
, out_creds
, &tgts
);
1196 for(i
= 0; tgts
&& tgts
[i
]; i
++) {
1197 krb5_cc_store_cred(context
, ccache
, tgts
[i
]);
1198 krb5_free_creds(context
, tgts
[i
]);
1201 if(ret
== 0 && (options
& KRB5_GC_NO_STORE
) == 0)
1202 krb5_cc_store_cred(context
, ccache
, *out_creds
);
1206 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1207 krb5_get_credentials(krb5_context context
,
1210 krb5_creds
*in_creds
,
1211 krb5_creds
**out_creds
)
1213 krb5_kdc_flags flags
;
1215 return krb5_get_credentials_with_flags(context
, options
, flags
,
1216 ccache
, in_creds
, out_creds
);
1219 struct krb5_get_creds_opt_data
{
1220 krb5_principal self
;
1222 krb5_enctype enctype
;
1227 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1228 krb5_get_creds_opt_alloc(krb5_context context
, krb5_get_creds_opt
*opt
)
1230 *opt
= calloc(1, sizeof(**opt
));
1232 krb5_set_error_message(context
, ENOMEM
,
1233 N_("malloc: out of memory", ""));
1239 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1240 krb5_get_creds_opt_free(krb5_context context
, krb5_get_creds_opt opt
)
1243 krb5_free_principal(context
, opt
->self
);
1245 free_Ticket(opt
->ticket
);
1248 memset(opt
, 0, sizeof(*opt
));
1252 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1253 krb5_get_creds_opt_set_options(krb5_context context
,
1254 krb5_get_creds_opt opt
,
1257 opt
->options
= options
;
1260 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1261 krb5_get_creds_opt_add_options(krb5_context context
,
1262 krb5_get_creds_opt opt
,
1265 opt
->options
|= options
;
1268 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
1269 krb5_get_creds_opt_set_enctype(krb5_context context
,
1270 krb5_get_creds_opt opt
,
1271 krb5_enctype enctype
)
1273 opt
->enctype
= enctype
;
1276 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1277 krb5_get_creds_opt_set_impersonate(krb5_context context
,
1278 krb5_get_creds_opt opt
,
1279 krb5_const_principal self
)
1282 krb5_free_principal(context
, opt
->self
);
1283 return krb5_copy_principal(context
, self
, &opt
->self
);
1286 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1287 krb5_get_creds_opt_set_ticket(krb5_context context
,
1288 krb5_get_creds_opt opt
,
1289 const Ticket
*ticket
)
1292 free_Ticket(opt
->ticket
);
1297 krb5_error_code ret
;
1299 opt
->ticket
= malloc(sizeof(*ticket
));
1300 if (opt
->ticket
== NULL
) {
1301 krb5_set_error_message(context
, ENOMEM
,
1302 N_("malloc: out of memory", ""));
1305 ret
= copy_Ticket(ticket
, opt
->ticket
);
1309 krb5_set_error_message(context
, ret
,
1310 N_("malloc: out of memory", ""));
1319 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1320 krb5_get_creds(krb5_context context
,
1321 krb5_get_creds_opt opt
,
1323 krb5_const_principal inprinc
,
1324 krb5_creds
**out_creds
)
1326 krb5_kdc_flags flags
;
1328 krb5_creds in_creds
;
1329 krb5_error_code ret
;
1331 krb5_creds
*res_creds
;
1334 if (opt
&& opt
->enctype
) {
1335 ret
= krb5_enctype_valid(context
, opt
->enctype
);
1340 memset(&in_creds
, 0, sizeof(in_creds
));
1341 in_creds
.server
= rk_UNCONST(inprinc
);
1343 ret
= krb5_cc_get_principal(context
, ccache
, &in_creds
.client
);
1348 options
= opt
->options
;
1354 res_creds
= calloc(1, sizeof(*res_creds
));
1355 if (res_creds
== NULL
) {
1356 krb5_free_principal(context
, in_creds
.client
);
1357 krb5_set_error_message(context
, ENOMEM
,
1358 N_("malloc: out of memory", ""));
1362 if (opt
&& opt
->enctype
) {
1363 in_creds
.session
.keytype
= opt
->enctype
;
1364 options
|= KRB5_TC_MATCH_KEYTYPE
;
1368 * If we got a credential, check if credential is expired before
1371 ret
= krb5_cc_retrieve_cred(context
,
1373 options
& KRB5_TC_MATCH_KEYTYPE
,
1374 &in_creds
, res_creds
);
1376 * If we got a credential, check if credential is expired before
1377 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1380 krb5_timestamp timeret
;
1382 /* If expired ok, don't bother checking */
1383 if(options
& KRB5_GC_EXPIRED_OK
) {
1384 *out_creds
= res_creds
;
1385 krb5_free_principal(context
, in_creds
.client
);
1389 krb5_timeofday(context
, &timeret
);
1390 if(res_creds
->times
.endtime
> timeret
) {
1391 *out_creds
= res_creds
;
1392 krb5_free_principal(context
, in_creds
.client
);
1395 if(options
& KRB5_GC_CACHED
)
1396 krb5_cc_remove_cred(context
, ccache
, 0, res_creds
);
1398 } else if(ret
!= KRB5_CC_END
) {
1400 krb5_free_principal(context
, in_creds
.client
);
1404 if(options
& KRB5_GC_CACHED
) {
1405 krb5_free_principal(context
, in_creds
.client
);
1406 ret
= not_found(context
, in_creds
.server
, KRB5_CC_NOTFOUND
);
1409 if(options
& KRB5_GC_USER_USER
) {
1410 flags
.b
.enc_tkt_in_skey
= 1;
1411 options
|= KRB5_GC_NO_STORE
;
1413 if (options
& KRB5_GC_FORWARDABLE
)
1414 flags
.b
.forwardable
= 1;
1415 if (options
& KRB5_GC_NO_TRANSIT_CHECK
)
1416 flags
.b
.disable_transited_check
= 1;
1417 if (options
& KRB5_GC_CONSTRAINED_DELEGATION
) {
1418 flags
.b
.request_anonymous
= 1; /* XXX ARGH confusion */
1419 flags
.b
.constrained_delegation
= 1;
1421 if (options
& KRB5_GC_CANONICALIZE
)
1422 flags
.b
.canonicalize
= 1;
1425 ret
= _krb5_get_cred_kdc_any(context
, flags
, ccache
,
1426 &in_creds
, opt
->self
, opt
->ticket
,
1428 krb5_free_principal(context
, in_creds
.client
);
1429 for(i
= 0; tgts
&& tgts
[i
]; i
++) {
1430 krb5_cc_store_cred(context
, ccache
, tgts
[i
]);
1431 krb5_free_creds(context
, tgts
[i
]);
1434 if(ret
== 0 && (options
& KRB5_GC_NO_STORE
) == 0)
1435 krb5_cc_store_cred(context
, ccache
, *out_creds
);
1438 _krb5_debug(context
, 5, "krb5_get_creds: ret = %d", ret
);
1447 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1448 krb5_get_renewed_creds(krb5_context context
,
1450 krb5_const_principal client
,
1452 const char *in_tkt_service
)
1454 krb5_error_code ret
;
1455 krb5_kdc_flags flags
;
1456 krb5_creds in
, *template, *out
= NULL
;
1458 memset(&in
, 0, sizeof(in
));
1459 memset(creds
, 0, sizeof(*creds
));
1461 ret
= krb5_copy_principal(context
, client
, &in
.client
);
1465 if (in_tkt_service
) {
1466 ret
= krb5_parse_name(context
, in_tkt_service
, &in
.server
);
1468 krb5_free_principal(context
, in
.client
);
1472 const char *realm
= krb5_principal_get_realm(context
, client
);
1474 ret
= krb5_make_principal(context
, &in
.server
, realm
, KRB5_TGS_NAME
,
1477 krb5_free_principal(context
, in
.client
);
1483 flags
.b
.renewable
= flags
.b
.renew
= 1;
1486 * Get template from old credential cache for the same entry, if
1487 * this failes, no worries.
1489 ret
= krb5_get_credentials(context
, KRB5_GC_CACHED
, ccache
, &in
, &template);
1491 flags
.b
.forwardable
= template->flags
.b
.forwardable
;
1492 flags
.b
.proxiable
= template->flags
.b
.proxiable
;
1493 krb5_free_creds (context
, template);
1496 ret
= krb5_get_kdc_cred(context
, ccache
, flags
, NULL
, NULL
, &in
, &out
);
1497 krb5_free_principal(context
, in
.client
);
1498 krb5_free_principal(context
, in
.server
);
1502 ret
= krb5_copy_creds_contents(context
, out
, creds
);
1503 krb5_free_creds(context
, out
);