2 * Copyright (c) 1997 - 2007 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>
36 __RCSID("$Heimdal: get_cred.c 21668 2007-07-22 11:28:05Z lha $"
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
,
58 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, body
, &len
, ret
);
62 krb5_abortx(context
, "internal error in ASN.1 encoder");
66 ret
= _krb5_mk_req_internal(context
, &ac
, 0, &in_data
, creds
,
67 &padata
->padata_value
,
68 KRB5_KU_TGS_REQ_AUTH_CKSUM
,
70 /* KRB5_KU_TGS_REQ_AUTH */);
75 padata
->padata_type
= KRB5_PADATA_TGS_REQ
;
80 * Set the `enc-authorization-data' in `req_body' based on `authdata'
83 static krb5_error_code
84 set_auth_data (krb5_context context
,
85 KDC_REQ_BODY
*req_body
,
86 krb5_authdata
*authdata
,
95 ASN1_MALLOC_ENCODE(AuthorizationData
, buf
, buf_size
, authdata
,
100 krb5_abortx(context
, "internal error in ASN.1 encoder");
102 ALLOC(req_body
->enc_authorization_data
, 1);
103 if (req_body
->enc_authorization_data
== NULL
) {
105 krb5_set_error_string(context
, "malloc: out of memory");
108 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
111 free (req_body
->enc_authorization_data
);
112 req_body
->enc_authorization_data
= NULL
;
115 krb5_encrypt_EncryptedData(context
,
117 KRB5_KU_TGS_REQ_AUTH_DAT_SUBKEY
,
118 /* KRB5_KU_TGS_REQ_AUTH_DAT_SESSION? */
122 req_body
->enc_authorization_data
);
124 krb5_crypto_destroy(context
, crypto
);
126 req_body
->enc_authorization_data
= NULL
;
132 * Create a tgs-req in `t' with `addresses', `flags', `second_ticket'
133 * (if not-NULL), `in_creds', `krbtgt', and returning the generated
134 * subkey in `subkey'.
137 static krb5_error_code
138 init_tgs_req (krb5_context context
,
140 krb5_addresses
*addresses
,
141 krb5_kdc_flags flags
,
142 Ticket
*second_ticket
,
143 krb5_creds
*in_creds
,
146 const METHOD_DATA
*padata
,
147 krb5_keyblock
**subkey
,
149 krb5_key_usage usage
)
151 krb5_error_code ret
= 0;
153 memset(t
, 0, sizeof(*t
));
155 t
->msg_type
= krb_tgs_req
;
156 if (in_creds
->session
.keytype
) {
157 ALLOC_SEQ(&t
->req_body
.etype
, 1);
158 if(t
->req_body
.etype
.val
== NULL
) {
160 krb5_set_error_string(context
, "malloc: out of memory");
163 t
->req_body
.etype
.val
[0] = in_creds
->session
.keytype
;
165 ret
= krb5_init_etype(context
,
166 &t
->req_body
.etype
.len
,
167 &t
->req_body
.etype
.val
,
172 t
->req_body
.addresses
= addresses
;
173 t
->req_body
.kdc_options
= flags
.b
;
174 ret
= copy_Realm(&in_creds
->server
->realm
, &t
->req_body
.realm
);
177 ALLOC(t
->req_body
.sname
, 1);
178 if (t
->req_body
.sname
== NULL
) {
180 krb5_set_error_string(context
, "malloc: out of memory");
184 /* some versions of some code might require that the client be
185 present in TGS-REQs, but this is clearly against the spec */
187 ret
= copy_PrincipalName(&in_creds
->server
->name
, t
->req_body
.sname
);
191 /* req_body.till should be NULL if there is no endtime specified,
192 but old MIT code (like DCE secd) doesn't like that */
193 ALLOC(t
->req_body
.till
, 1);
194 if(t
->req_body
.till
== NULL
){
196 krb5_set_error_string(context
, "malloc: out of memory");
199 *t
->req_body
.till
= in_creds
->times
.endtime
;
201 t
->req_body
.nonce
= nonce
;
203 ALLOC(t
->req_body
.additional_tickets
, 1);
204 if (t
->req_body
.additional_tickets
== NULL
) {
206 krb5_set_error_string(context
, "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_string(context
, "malloc: out of memory");
215 ret
= copy_Ticket(second_ticket
, t
->req_body
.additional_tickets
->val
);
220 if (t
->padata
== NULL
) {
222 krb5_set_error_string(context
, "malloc: out of memory");
225 ALLOC_SEQ(t
->padata
, 1 + padata
->len
);
226 if (t
->padata
->val
== NULL
) {
228 krb5_set_error_string(context
, "malloc: out of memory");
233 for (i
= 0; i
< padata
->len
; i
++) {
234 ret
= copy_PA_DATA(&padata
->val
[i
], &t
->padata
->val
[i
+ 1]);
236 krb5_set_error_string(context
, "malloc: out of memory");
243 krb5_auth_context ac
;
244 krb5_keyblock
*key
= NULL
;
246 ret
= krb5_auth_con_init(context
, &ac
);
250 if (krb5_config_get_bool_default(context
, NULL
, FALSE
,
252 krbtgt
->server
->realm
,
253 "tgs_require_subkey",
256 ret
= krb5_generate_subkey (context
, &krbtgt
->session
, &key
);
258 krb5_auth_con_free (context
, ac
);
262 ret
= krb5_auth_con_setlocalsubkey(context
, ac
, key
);
265 krb5_free_keyblock (context
, key
);
266 krb5_auth_con_free (context
, ac
);
271 ret
= set_auth_data (context
, &t
->req_body
, &in_creds
->authdata
,
272 key
? key
: &krbtgt
->session
);
275 krb5_free_keyblock (context
, key
);
276 krb5_auth_con_free (context
, ac
);
280 ret
= make_pa_tgs_req(context
,
288 krb5_free_keyblock (context
, key
);
289 krb5_auth_con_free(context
, ac
);
294 krb5_auth_con_free(context
, ac
);
298 t
->req_body
.addresses
= NULL
;
305 _krb5_get_krbtgt(krb5_context context
,
313 memset(&tmp_cred
, 0, sizeof(tmp_cred
));
315 ret
= krb5_cc_get_principal(context
, id
, &tmp_cred
.client
);
319 ret
= krb5_make_principal(context
,
326 krb5_free_principal(context
, tmp_cred
.client
);
329 ret
= krb5_get_credentials(context
,
334 krb5_free_principal(context
, tmp_cred
.client
);
335 krb5_free_principal(context
, tmp_cred
.server
);
341 /* DCE compatible decrypt proc */
342 static krb5_error_code
343 decrypt_tkt_with_subkey (krb5_context context
,
345 krb5_key_usage usage
,
346 krb5_const_pointer subkey
,
347 krb5_kdc_rep
*dec_rep
)
354 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
357 ret
= krb5_decrypt_EncryptedData (context
,
360 &dec_rep
->kdc_rep
.enc_part
,
362 krb5_crypto_destroy(context
, crypto
);
364 /* DCE compat -- try to decrypt with subkey */
365 ret
= krb5_crypto_init(context
, subkey
, 0, &crypto
);
368 ret
= krb5_decrypt_EncryptedData (context
,
370 KRB5_KU_TGS_REP_ENC_PART_SUB_KEY
,
371 &dec_rep
->kdc_rep
.enc_part
,
373 krb5_crypto_destroy(context
, crypto
);
378 ret
= krb5_decode_EncASRepPart(context
,
384 ret
= krb5_decode_EncTGSRepPart(context
,
389 krb5_data_free (&data
);
393 static krb5_error_code
394 get_cred_kdc_usage(krb5_context context
,
396 krb5_kdc_flags flags
,
397 krb5_addresses
*addresses
,
398 krb5_creds
*in_creds
,
400 krb5_principal impersonate_principal
,
401 Ticket
*second_ticket
,
402 krb5_creds
*out_creds
,
403 krb5_key_usage usage
)
412 krb5_keyblock
*subkey
= NULL
;
414 Ticket second_ticket_data
;
417 krb5_data_zero(&resp
);
418 krb5_data_zero(&enc
);
422 krb5_generate_random_block(&nonce
, sizeof(nonce
));
425 if(flags
.b
.enc_tkt_in_skey
&& second_ticket
== NULL
){
426 ret
= decode_Ticket(in_creds
->second_ticket
.data
,
427 in_creds
->second_ticket
.length
,
428 &second_ticket_data
, &len
);
431 second_ticket
= &second_ticket_data
;
435 if (impersonate_principal
) {
442 self
.name
= impersonate_principal
->name
;
443 self
.realm
= impersonate_principal
->realm
;
444 self
.auth
= estrdup("Kerberos");
446 ret
= _krb5_s4u2self_to_checksumdata(context
, &self
, &data
);
452 ret
= krb5_crypto_init(context
, &krbtgt
->session
, 0, &crypto
);
455 krb5_data_free(&data
);
459 ret
= krb5_create_checksum(context
,
466 krb5_crypto_destroy(context
, crypto
);
467 krb5_data_free(&data
);
473 ASN1_MALLOC_ENCODE(PA_S4U2Self
, buf
, len
, &self
, &size
, ret
);
475 free_Checksum(&self
.cksum
);
479 krb5_abortx(context
, "internal asn1 error");
481 ret
= krb5_padata_add(context
, &padata
, KRB5_PADATA_S4U2SELF
, buf
, len
);
486 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){
531 ret
= krb5_copy_principal(context
,
536 ret
= krb5_copy_principal(context
,
541 /* this should go someplace else */
542 out_creds
->times
.endtime
= in_creds
->times
.endtime
;
544 ret
= _krb5_extract_ticket(context
,
549 KRB5_KU_TGS_REP_ENC_PART_SESSION
,
552 EXTRACT_TICKET_ALLOW_CNAME_MISMATCH
|
553 EXTRACT_TICKET_ALLOW_SERVER_MISMATCH
,
554 decrypt_tkt_with_subkey
,
556 krb5_free_kdc_rep(context
, &rep
);
557 } else if(krb5_rd_error(context
, &resp
, &error
) == 0) {
558 ret
= krb5_error_from_rd_error(context
, &error
, in_creds
);
559 krb5_free_error_contents(context
, &error
);
560 } else if(resp
.data
&& ((char*)resp
.data
)[0] == 4) {
561 ret
= KRB5KRB_AP_ERR_V4_REPLY
;
562 krb5_clear_error_string(context
);
564 ret
= KRB5KRB_AP_ERR_MSG_TYPE
;
565 krb5_clear_error_string(context
);
569 if (second_ticket
== &second_ticket_data
)
570 free_Ticket(&second_ticket_data
);
571 free_METHOD_DATA(&padata
);
572 krb5_data_free(&resp
);
573 krb5_data_free(&enc
);
575 krb5_free_keyblock_contents(context
, subkey
);
582 static krb5_error_code
583 get_cred_kdc(krb5_context context
,
585 krb5_kdc_flags flags
,
586 krb5_addresses
*addresses
,
587 krb5_creds
*in_creds
,
589 krb5_principal impersonate_principal
,
590 Ticket
*second_ticket
,
591 krb5_creds
*out_creds
)
595 ret
= get_cred_kdc_usage(context
, id
, flags
, addresses
, in_creds
,
596 krbtgt
, impersonate_principal
, second_ticket
,
597 out_creds
, KRB5_KU_TGS_REQ_AUTH
);
598 if (ret
== KRB5KRB_AP_ERR_BAD_INTEGRITY
) {
599 krb5_clear_error_string (context
);
600 ret
= get_cred_kdc_usage(context
, id
, flags
, addresses
, in_creds
,
601 krbtgt
, impersonate_principal
, second_ticket
,
602 out_creds
, KRB5_KU_AP_REQ_AUTH
);
607 /* same as above, just get local addresses first */
609 static krb5_error_code
610 get_cred_kdc_la(krb5_context context
, krb5_ccache id
, krb5_kdc_flags flags
,
611 krb5_creds
*in_creds
, krb5_creds
*krbtgt
,
612 krb5_principal impersonate_principal
, Ticket
*second_ticket
,
613 krb5_creds
*out_creds
)
616 krb5_addresses addresses
, *addrs
= &addresses
;
618 krb5_get_all_client_addrs(context
, &addresses
);
619 /* XXX this sucks. */
620 if(addresses
.len
== 0)
622 ret
= get_cred_kdc(context
, id
, flags
, addrs
,
623 in_creds
, krbtgt
, impersonate_principal
, second_ticket
,
625 krb5_free_addresses(context
, &addresses
);
629 krb5_error_code KRB5_LIB_FUNCTION
630 krb5_get_kdc_cred(krb5_context context
,
632 krb5_kdc_flags flags
,
633 krb5_addresses
*addresses
,
634 Ticket
*second_ticket
,
635 krb5_creds
*in_creds
,
636 krb5_creds
**out_creds
642 *out_creds
= calloc(1, sizeof(**out_creds
));
643 if(*out_creds
== NULL
) {
644 krb5_set_error_string(context
, "malloc: out of memory");
647 ret
= _krb5_get_krbtgt (context
,
649 in_creds
->server
->realm
,
655 ret
= get_cred_kdc(context
, id
, flags
, addresses
,
656 in_creds
, krbtgt
, NULL
, NULL
, *out_creds
);
657 krb5_free_creds (context
, krbtgt
);
664 not_found(krb5_context context
, krb5_const_principal p
)
669 ret
= krb5_unparse_name(context
, p
, &str
);
671 krb5_clear_error_string(context
);
674 krb5_set_error_string(context
, "Matching credential (%s) not found", str
);
678 static krb5_error_code
679 find_cred(krb5_context context
,
681 krb5_principal server
,
683 krb5_creds
*out_creds
)
688 krb5_cc_clear_mcred(&mcreds
);
689 mcreds
.server
= server
;
690 ret
= krb5_cc_retrieve_cred(context
, id
, KRB5_TC_DONT_MATCH_REALM
,
694 while(tgts
&& *tgts
){
695 if(krb5_compare_creds(context
, KRB5_TC_DONT_MATCH_REALM
,
697 ret
= krb5_copy_creds_contents(context
, *tgts
, out_creds
);
702 not_found(context
, server
);
703 return KRB5_CC_NOTFOUND
;
706 static krb5_error_code
707 add_cred(krb5_context context
, krb5_creds
***tgts
, krb5_creds
*tkt
)
711 krb5_creds
**tmp
= *tgts
;
713 for(i
= 0; tmp
&& tmp
[i
]; i
++); /* XXX */
714 tmp
= realloc(tmp
, (i
+2)*sizeof(*tmp
));
716 krb5_set_error_string(context
, "malloc: out of memory");
720 ret
= krb5_copy_creds(context
, tkt
, &tmp
[i
]);
727 creds = cc_get_cred(server)
728 if(creds) return creds
729 tgt = cc_get_cred(krbtgt/server_realm@any_realm)
731 return get_cred_tgt(server, tgt)
732 if(client_realm == server_realm)
734 tgt = get_cred(krbtgt/server_realm@client_realm)
735 while(tgt_inst != server_realm)
736 tgt = get_cred(krbtgt/server_realm@tgt_inst)
737 return get_cred_tgt(server, tgt)
740 static krb5_error_code
741 get_cred_from_kdc_flags(krb5_context context
,
742 krb5_kdc_flags flags
,
744 krb5_creds
*in_creds
,
745 krb5_principal impersonate_principal
,
746 Ticket
*second_ticket
,
747 krb5_creds
**out_creds
,
748 krb5_creds
***ret_tgts
)
751 krb5_creds
*tgt
, tmp_creds
;
752 krb5_const_realm client_realm
, server_realm
, try_realm
;
756 client_realm
= krb5_principal_get_realm(context
, in_creds
->client
);
757 server_realm
= krb5_principal_get_realm(context
, in_creds
->server
);
758 memset(&tmp_creds
, 0, sizeof(tmp_creds
));
759 ret
= krb5_copy_principal(context
, in_creds
->client
, &tmp_creds
.client
);
763 try_realm
= krb5_config_get_string(context
, NULL
, "capaths",
764 client_realm
, server_realm
, NULL
);
767 /* XXX remove in future release */
768 if(try_realm
== NULL
)
769 try_realm
= krb5_config_get_string(context
, NULL
, "libdefaults",
770 "capath", server_realm
, NULL
);
773 if (try_realm
== NULL
)
774 try_realm
= client_realm
;
776 ret
= krb5_make_principal(context
,
783 krb5_free_principal(context
, tmp_creds
.client
);
788 /* XXX try krb5_cc_retrieve_cred first? */
789 ret
= find_cred(context
, ccache
, tmp_creds
.server
,
792 *out_creds
= calloc(1, sizeof(**out_creds
));
793 if(*out_creds
== NULL
) {
794 krb5_set_error_string(context
, "malloc: out of memory");
799 krb5_appdefault_boolean(context
, NULL
, tgts
.server
->realm
,
800 "no-addresses", FALSE
, &noaddr
);
803 ret
= get_cred_kdc(context
, ccache
, flags
, NULL
,
805 impersonate_principal
,
809 ret
= get_cred_kdc_la(context
, ccache
, flags
,
811 impersonate_principal
,
819 krb5_free_cred_contents(context
, &tgts
);
820 krb5_free_principal(context
, tmp_creds
.server
);
821 krb5_free_principal(context
, tmp_creds
.client
);
825 if(krb5_realm_compare(context
, in_creds
->client
, in_creds
->server
)) {
826 not_found(context
, in_creds
->server
);
827 return KRB5_CC_NOTFOUND
;
829 /* XXX this can loop forever */
831 heim_general_string tgt_inst
;
833 ret
= get_cred_from_kdc_flags(context
, flags
, ccache
, &tmp_creds
,
834 NULL
, NULL
, &tgt
, ret_tgts
);
836 krb5_free_principal(context
, tmp_creds
.server
);
837 krb5_free_principal(context
, tmp_creds
.client
);
840 ret
= add_cred(context
, ret_tgts
, tgt
);
842 krb5_free_principal(context
, tmp_creds
.server
);
843 krb5_free_principal(context
, tmp_creds
.client
);
846 tgt_inst
= tgt
->server
->name
.name_string
.val
[1];
847 if(strcmp(tgt_inst
, server_realm
) == 0)
849 krb5_free_principal(context
, tmp_creds
.server
);
850 ret
= krb5_make_principal(context
, &tmp_creds
.server
,
851 tgt_inst
, KRB5_TGS_NAME
, server_realm
, NULL
);
853 krb5_free_principal(context
, tmp_creds
.server
);
854 krb5_free_principal(context
, tmp_creds
.client
);
857 ret
= krb5_free_creds(context
, tgt
);
859 krb5_free_principal(context
, tmp_creds
.server
);
860 krb5_free_principal(context
, tmp_creds
.client
);
865 krb5_free_principal(context
, tmp_creds
.server
);
866 krb5_free_principal(context
, tmp_creds
.client
);
867 *out_creds
= calloc(1, sizeof(**out_creds
));
868 if(*out_creds
== NULL
) {
869 krb5_set_error_string(context
, "malloc: out of memory");
874 krb5_appdefault_boolean(context
, NULL
, tgt
->server
->realm
,
875 "no-addresses", KRB5_ADDRESSLESS_DEFAULT
,
878 ret
= get_cred_kdc (context
, ccache
, flags
, NULL
,
879 in_creds
, tgt
, NULL
, NULL
,
882 ret
= get_cred_kdc_la(context
, ccache
, flags
,
883 in_creds
, tgt
, NULL
, NULL
,
890 krb5_free_creds(context
, tgt
);
894 krb5_error_code KRB5_LIB_FUNCTION
895 krb5_get_cred_from_kdc_opt(krb5_context context
,
897 krb5_creds
*in_creds
,
898 krb5_creds
**out_creds
,
899 krb5_creds
***ret_tgts
,
904 return get_cred_from_kdc_flags(context
, f
, ccache
,
905 in_creds
, NULL
, NULL
,
906 out_creds
, ret_tgts
);
909 krb5_error_code KRB5_LIB_FUNCTION
910 krb5_get_cred_from_kdc(krb5_context context
,
912 krb5_creds
*in_creds
,
913 krb5_creds
**out_creds
,
914 krb5_creds
***ret_tgts
)
916 return krb5_get_cred_from_kdc_opt(context
, ccache
,
917 in_creds
, out_creds
, ret_tgts
, 0);
921 krb5_error_code KRB5_LIB_FUNCTION
922 krb5_get_credentials_with_flags(krb5_context context
,
924 krb5_kdc_flags flags
,
926 krb5_creds
*in_creds
,
927 krb5_creds
**out_creds
)
931 krb5_creds
*res_creds
;
935 res_creds
= calloc(1, sizeof(*res_creds
));
936 if (res_creds
== NULL
) {
937 krb5_set_error_string(context
, "malloc: out of memory");
941 if (in_creds
->session
.keytype
)
942 options
|= KRB5_TC_MATCH_KEYTYPE
;
945 * If we got a credential, check if credential is expired before
948 ret
= krb5_cc_retrieve_cred(context
,
950 in_creds
->session
.keytype
?
951 KRB5_TC_MATCH_KEYTYPE
: 0,
952 in_creds
, res_creds
);
954 * If we got a credential, check if credential is expired before
955 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
958 krb5_timestamp timeret
;
960 /* If expired ok, don't bother checking */
961 if(options
& KRB5_GC_EXPIRED_OK
) {
962 *out_creds
= res_creds
;
966 krb5_timeofday(context
, &timeret
);
967 if(res_creds
->times
.endtime
> timeret
) {
968 *out_creds
= res_creds
;
971 if(options
& KRB5_GC_CACHED
)
972 krb5_cc_remove_cred(context
, ccache
, 0, res_creds
);
974 } else if(ret
!= KRB5_CC_END
) {
979 if(options
& KRB5_GC_CACHED
) {
980 not_found(context
, in_creds
->server
);
981 return KRB5_CC_NOTFOUND
;
983 if(options
& KRB5_GC_USER_USER
)
984 flags
.b
.enc_tkt_in_skey
= 1;
985 if (flags
.b
.enc_tkt_in_skey
)
986 options
|= KRB5_GC_NO_STORE
;
989 ret
= get_cred_from_kdc_flags(context
, flags
, ccache
,
990 in_creds
, NULL
, NULL
, out_creds
, &tgts
);
991 for(i
= 0; tgts
&& tgts
[i
]; i
++) {
992 krb5_cc_store_cred(context
, ccache
, tgts
[i
]);
993 krb5_free_creds(context
, tgts
[i
]);
996 if(ret
== 0 && (options
& KRB5_GC_NO_STORE
) == 0)
997 krb5_cc_store_cred(context
, ccache
, *out_creds
);
1001 krb5_error_code KRB5_LIB_FUNCTION
1002 krb5_get_credentials(krb5_context context
,
1005 krb5_creds
*in_creds
,
1006 krb5_creds
**out_creds
)
1008 krb5_kdc_flags flags
;
1010 return krb5_get_credentials_with_flags(context
, options
, flags
,
1011 ccache
, in_creds
, out_creds
);
1014 struct krb5_get_creds_opt_data
{
1015 krb5_principal self
;
1017 krb5_enctype enctype
;
1022 krb5_error_code KRB5_LIB_FUNCTION
1023 krb5_get_creds_opt_alloc(krb5_context context
, krb5_get_creds_opt
*opt
)
1025 *opt
= calloc(1, sizeof(**opt
));
1027 krb5_set_error_string(context
, "malloc: out of memory");
1033 void KRB5_LIB_FUNCTION
1034 krb5_get_creds_opt_free(krb5_context context
, krb5_get_creds_opt opt
)
1037 krb5_free_principal(context
, opt
->self
);
1038 memset(opt
, 0, sizeof(*opt
));
1042 void KRB5_LIB_FUNCTION
1043 krb5_get_creds_opt_set_options(krb5_context context
,
1044 krb5_get_creds_opt opt
,
1047 opt
->options
= options
;
1050 void KRB5_LIB_FUNCTION
1051 krb5_get_creds_opt_add_options(krb5_context context
,
1052 krb5_get_creds_opt opt
,
1055 opt
->options
|= options
;
1058 void KRB5_LIB_FUNCTION
1059 krb5_get_creds_opt_set_enctype(krb5_context context
,
1060 krb5_get_creds_opt opt
,
1061 krb5_enctype enctype
)
1063 opt
->enctype
= enctype
;
1066 krb5_error_code KRB5_LIB_FUNCTION
1067 krb5_get_creds_opt_set_impersonate(krb5_context context
,
1068 krb5_get_creds_opt opt
,
1069 krb5_const_principal self
)
1072 krb5_free_principal(context
, opt
->self
);
1073 return krb5_copy_principal(context
, self
, &opt
->self
);
1076 krb5_error_code KRB5_LIB_FUNCTION
1077 krb5_get_creds_opt_set_ticket(krb5_context context
,
1078 krb5_get_creds_opt opt
,
1079 const Ticket
*ticket
)
1082 free_Ticket(opt
->ticket
);
1087 krb5_error_code ret
;
1089 opt
->ticket
= malloc(sizeof(*ticket
));
1090 if (opt
->ticket
== NULL
) {
1091 krb5_set_error_string(context
, "malloc: out of memory");
1094 ret
= copy_Ticket(ticket
, opt
->ticket
);
1098 krb5_set_error_string(context
, "malloc: out of memory");
1107 krb5_error_code KRB5_LIB_FUNCTION
1108 krb5_get_creds(krb5_context context
,
1109 krb5_get_creds_opt opt
,
1111 krb5_const_principal inprinc
,
1112 krb5_creds
**out_creds
)
1114 krb5_kdc_flags flags
;
1116 krb5_creds in_creds
;
1117 krb5_error_code ret
;
1119 krb5_creds
*res_creds
;
1122 memset(&in_creds
, 0, sizeof(in_creds
));
1123 in_creds
.server
= rk_UNCONST(inprinc
);
1125 ret
= krb5_cc_get_principal(context
, ccache
, &in_creds
.client
);
1129 options
= opt
->options
;
1133 res_creds
= calloc(1, sizeof(*res_creds
));
1134 if (res_creds
== NULL
) {
1135 krb5_free_principal(context
, in_creds
.client
);
1136 krb5_set_error_string(context
, "malloc: out of memory");
1141 in_creds
.session
.keytype
= opt
->enctype
;
1142 options
|= KRB5_TC_MATCH_KEYTYPE
;
1146 * If we got a credential, check if credential is expired before
1149 ret
= krb5_cc_retrieve_cred(context
,
1151 opt
->enctype
? KRB5_TC_MATCH_KEYTYPE
: 0,
1152 &in_creds
, res_creds
);
1154 * If we got a credential, check if credential is expired before
1155 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1158 krb5_timestamp timeret
;
1160 /* If expired ok, don't bother checking */
1161 if(options
& KRB5_GC_EXPIRED_OK
) {
1162 *out_creds
= res_creds
;
1163 krb5_free_principal(context
, in_creds
.client
);
1167 krb5_timeofday(context
, &timeret
);
1168 if(res_creds
->times
.endtime
> timeret
) {
1169 *out_creds
= res_creds
;
1170 krb5_free_principal(context
, in_creds
.client
);
1173 if(options
& KRB5_GC_CACHED
)
1174 krb5_cc_remove_cred(context
, ccache
, 0, res_creds
);
1176 } else if(ret
!= KRB5_CC_END
) {
1178 krb5_free_principal(context
, in_creds
.client
);
1182 if(options
& KRB5_GC_CACHED
) {
1183 not_found(context
, in_creds
.server
);
1184 krb5_free_principal(context
, in_creds
.client
);
1185 return KRB5_CC_NOTFOUND
;
1187 if(options
& KRB5_GC_USER_USER
) {
1188 flags
.b
.enc_tkt_in_skey
= 1;
1189 options
|= KRB5_GC_NO_STORE
;
1191 if (options
& KRB5_GC_FORWARDABLE
)
1192 flags
.b
.forwardable
= 1;
1193 if (options
& KRB5_GC_NO_TRANSIT_CHECK
)
1194 flags
.b
.disable_transited_check
= 1;
1195 if (options
& KRB5_GC_CONSTRAINED_DELEGATION
) {
1196 flags
.b
.request_anonymous
= 1; /* XXX ARGH confusion */
1197 flags
.b
.constrained_delegation
= 1;
1201 ret
= get_cred_from_kdc_flags(context
, flags
, ccache
,
1202 &in_creds
, opt
->self
, opt
->ticket
,
1204 krb5_free_principal(context
, in_creds
.client
);
1205 for(i
= 0; tgts
&& tgts
[i
]; i
++) {
1206 krb5_cc_store_cred(context
, ccache
, tgts
[i
]);
1207 krb5_free_creds(context
, tgts
[i
]);
1210 if(ret
== 0 && (options
& KRB5_GC_NO_STORE
) == 0)
1211 krb5_cc_store_cred(context
, ccache
, *out_creds
);
1219 krb5_error_code KRB5_LIB_FUNCTION
1220 krb5_get_renewed_creds(krb5_context context
,
1222 krb5_const_principal client
,
1224 const char *in_tkt_service
)
1226 krb5_error_code ret
;
1227 krb5_kdc_flags flags
;
1228 krb5_creds in
, *template, *out
= NULL
;
1230 memset(&in
, 0, sizeof(in
));
1231 memset(creds
, 0, sizeof(*creds
));
1233 ret
= krb5_copy_principal(context
, client
, &in
.client
);
1237 if (in_tkt_service
) {
1238 ret
= krb5_parse_name(context
, in_tkt_service
, &in
.server
);
1240 krb5_free_principal(context
, in
.client
);
1244 const char *realm
= krb5_principal_get_realm(context
, client
);
1246 ret
= krb5_make_principal(context
, &in
.server
, realm
, KRB5_TGS_NAME
,
1249 krb5_free_principal(context
, in
.client
);
1255 flags
.b
.renewable
= flags
.b
.renew
= 1;
1258 * Get template from old credential cache for the same entry, if
1259 * this failes, no worries.
1261 ret
= krb5_get_credentials(context
, KRB5_GC_CACHED
, ccache
, &in
, &template);
1263 flags
.b
.forwardable
= template->flags
.b
.forwardable
;
1264 flags
.b
.proxiable
= template->flags
.b
.proxiable
;
1265 krb5_free_creds (context
, template);
1268 ret
= krb5_get_kdc_cred(context
, ccache
, flags
, NULL
, NULL
, &in
, &out
);
1269 krb5_free_principal(context
, in
.client
);
1270 krb5_free_principal(context
, in
.server
);
1274 ret
= krb5_copy_creds_contents(context
, out
, creds
);
1275 krb5_free_creds(context
, out
);