Add roken/rename.c to fix non-standard rename()
[heimdal.git] / lib / krb5 / get_cred.c
blob8f9d46219023b500607782cd93f8ba3b39b0cd75
1 /*
2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "krb5_locl.h"
37 #include <assert.h>
40 * Take the `body' and encode it into `padata' using the credentials
41 * in `creds'.
44 static krb5_error_code
45 make_pa_tgs_req(krb5_context context,
46 krb5_auth_context ac,
47 KDC_REQ_BODY *body,
48 PA_DATA *padata,
49 krb5_creds *creds)
51 u_char *buf;
52 size_t buf_size;
53 size_t len;
54 krb5_data in_data;
55 krb5_error_code ret;
57 ASN1_MALLOC_ENCODE(KDC_REQ_BODY, buf, buf_size, body, &len, ret);
58 if (ret)
59 goto out;
60 if(buf_size != len)
61 krb5_abortx(context, "internal error in ASN.1 encoder");
63 in_data.length = len;
64 in_data.data = buf;
65 ret = _krb5_mk_req_internal(context, &ac, 0, &in_data, creds,
66 &padata->padata_value,
67 KRB5_KU_TGS_REQ_AUTH_CKSUM,
68 KRB5_KU_TGS_REQ_AUTH);
69 out:
70 free (buf);
71 if(ret)
72 return ret;
73 padata->padata_type = KRB5_PADATA_TGS_REQ;
74 return 0;
78 * Set the `enc-authorization-data' in `req_body' based on `authdata'
81 static krb5_error_code
82 set_auth_data (krb5_context context,
83 KDC_REQ_BODY *req_body,
84 krb5_authdata *authdata,
85 krb5_keyblock *subkey)
87 if(authdata->len) {
88 size_t len, buf_size;
89 unsigned char *buf;
90 krb5_crypto crypto;
91 krb5_error_code ret;
93 ASN1_MALLOC_ENCODE(AuthorizationData, buf, buf_size, authdata,
94 &len, ret);
95 if (ret)
96 return ret;
97 if (buf_size != len)
98 krb5_abortx(context, "internal error in ASN.1 encoder");
100 ALLOC(req_body->enc_authorization_data, 1);
101 if (req_body->enc_authorization_data == NULL) {
102 free (buf);
103 krb5_set_error_message(context, ENOMEM,
104 N_("malloc: out of memory", ""));
105 return ENOMEM;
107 ret = krb5_crypto_init(context, subkey, 0, &crypto);
108 if (ret) {
109 free (buf);
110 free (req_body->enc_authorization_data);
111 req_body->enc_authorization_data = NULL;
112 return ret;
114 krb5_encrypt_EncryptedData(context,
115 crypto,
116 KRB5_KU_TGS_REQ_AUTH_DAT_SUBKEY,
117 buf,
118 len,
120 req_body->enc_authorization_data);
121 free (buf);
122 krb5_crypto_destroy(context, crypto);
123 } else {
124 req_body->enc_authorization_data = NULL;
126 return 0;
130 * Create a tgs-req in `t' with `addresses', `flags', `second_ticket'
131 * (if not-NULL), `in_creds', `krbtgt', and returning the generated
132 * subkey in `subkey'.
135 static krb5_error_code
136 init_tgs_req (krb5_context context,
137 krb5_ccache ccache,
138 krb5_addresses *addresses,
139 krb5_kdc_flags flags,
140 Ticket *second_ticket,
141 krb5_creds *in_creds,
142 krb5_creds *krbtgt,
143 unsigned nonce,
144 const METHOD_DATA *padata,
145 krb5_keyblock **subkey,
146 TGS_REQ *t)
148 krb5_auth_context ac = NULL;
149 krb5_error_code ret = 0;
151 memset(t, 0, sizeof(*t));
152 t->pvno = 5;
153 t->msg_type = krb_tgs_req;
154 if (in_creds->session.keytype) {
155 ALLOC_SEQ(&t->req_body.etype, 1);
156 if(t->req_body.etype.val == NULL) {
157 ret = ENOMEM;
158 krb5_set_error_message(context, ret,
159 N_("malloc: out of memory", ""));
160 goto fail;
162 t->req_body.etype.val[0] = in_creds->session.keytype;
163 } else {
164 ret = krb5_init_etype(context,
165 &t->req_body.etype.len,
166 &t->req_body.etype.val,
167 NULL);
169 if (ret)
170 goto fail;
171 t->req_body.addresses = addresses;
172 t->req_body.kdc_options = flags.b;
173 ret = copy_Realm(&in_creds->server->realm, &t->req_body.realm);
174 if (ret)
175 goto fail;
176 ALLOC(t->req_body.sname, 1);
177 if (t->req_body.sname == NULL) {
178 ret = ENOMEM;
179 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
180 goto fail;
183 /* some versions of some code might require that the client be
184 present in TGS-REQs, but this is clearly against the spec */
186 ret = copy_PrincipalName(&in_creds->server->name, t->req_body.sname);
187 if (ret)
188 goto fail;
190 /* req_body.till should be NULL if there is no endtime specified,
191 but old MIT code (like DCE secd) doesn't like that */
192 ALLOC(t->req_body.till, 1);
193 if(t->req_body.till == NULL){
194 ret = ENOMEM;
195 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
196 goto fail;
198 *t->req_body.till = in_creds->times.endtime;
200 t->req_body.nonce = nonce;
201 if(second_ticket){
202 ALLOC(t->req_body.additional_tickets, 1);
203 if (t->req_body.additional_tickets == NULL) {
204 ret = ENOMEM;
205 krb5_set_error_message(context, ret,
206 N_("malloc: out of memory", ""));
207 goto fail;
209 ALLOC_SEQ(t->req_body.additional_tickets, 1);
210 if (t->req_body.additional_tickets->val == NULL) {
211 ret = ENOMEM;
212 krb5_set_error_message(context, ret,
213 N_("malloc: out of memory", ""));
214 goto fail;
216 ret = copy_Ticket(second_ticket, t->req_body.additional_tickets->val);
217 if (ret)
218 goto fail;
220 ALLOC(t->padata, 1);
221 if (t->padata == NULL) {
222 ret = ENOMEM;
223 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
224 goto fail;
226 ALLOC_SEQ(t->padata, 1 + padata->len);
227 if (t->padata->val == NULL) {
228 ret = ENOMEM;
229 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
230 goto fail;
233 int i;
234 for (i = 0; i < padata->len; i++) {
235 ret = copy_PA_DATA(&padata->val[i], &t->padata->val[i + 1]);
236 if (ret) {
237 krb5_set_error_message(context, ret,
238 N_("malloc: out of memory", ""));
239 goto fail;
244 ret = krb5_auth_con_init(context, &ac);
245 if(ret)
246 goto fail;
248 ret = krb5_auth_con_generatelocalsubkey(context, ac, &krbtgt->session);
249 if (ret)
250 goto fail;
252 ret = set_auth_data (context, &t->req_body, &in_creds->authdata,
253 ac->local_subkey);
254 if (ret)
255 goto fail;
257 ret = make_pa_tgs_req(context,
259 &t->req_body,
260 &t->padata->val[0],
261 krbtgt);
262 if(ret)
263 goto fail;
265 ret = krb5_auth_con_getlocalsubkey(context, ac, subkey);
266 if (ret)
267 goto fail;
269 fail:
270 if (ac)
271 krb5_auth_con_free(context, ac);
272 if (ret) {
273 t->req_body.addresses = NULL;
274 free_TGS_REQ (t);
276 return ret;
279 krb5_error_code
280 _krb5_get_krbtgt(krb5_context context,
281 krb5_ccache id,
282 krb5_realm realm,
283 krb5_creds **cred)
285 krb5_error_code ret;
286 krb5_creds tmp_cred;
288 memset(&tmp_cred, 0, sizeof(tmp_cred));
290 ret = krb5_cc_get_principal(context, id, &tmp_cred.client);
291 if (ret)
292 return ret;
294 ret = krb5_make_principal(context,
295 &tmp_cred.server,
296 realm,
297 KRB5_TGS_NAME,
298 realm,
299 NULL);
300 if(ret) {
301 krb5_free_principal(context, tmp_cred.client);
302 return ret;
304 ret = krb5_get_credentials(context,
305 KRB5_GC_CACHED,
307 &tmp_cred,
308 cred);
309 krb5_free_principal(context, tmp_cred.client);
310 krb5_free_principal(context, tmp_cred.server);
311 if(ret)
312 return ret;
313 return 0;
316 /* DCE compatible decrypt proc */
317 static krb5_error_code KRB5_CALLCONV
318 decrypt_tkt_with_subkey (krb5_context context,
319 krb5_keyblock *key,
320 krb5_key_usage usage,
321 krb5_const_pointer subkey,
322 krb5_kdc_rep *dec_rep)
324 krb5_error_code ret;
325 krb5_data data;
326 size_t size;
327 krb5_crypto crypto;
329 assert(usage == 0);
332 * start out with trying with subkey if we have one
334 if (subkey) {
335 ret = krb5_crypto_init(context, subkey, 0, &crypto);
336 if (ret)
337 return ret;
338 ret = krb5_decrypt_EncryptedData (context,
339 crypto,
340 KRB5_KU_TGS_REP_ENC_PART_SUB_KEY,
341 &dec_rep->kdc_rep.enc_part,
342 &data);
343 krb5_crypto_destroy(context, crypto);
345 if (subkey == NULL || ret) {
346 ret = krb5_crypto_init(context, key, 0, &crypto);
347 if (ret)
348 return ret;
349 ret = krb5_decrypt_EncryptedData (context,
350 crypto,
351 KRB5_KU_TGS_REP_ENC_PART_SESSION,
352 &dec_rep->kdc_rep.enc_part,
353 &data);
354 krb5_crypto_destroy(context, crypto);
356 if (ret)
357 return ret;
359 ret = decode_EncASRepPart(data.data,
360 data.length,
361 &dec_rep->enc_part,
362 &size);
363 if (ret)
364 ret = decode_EncTGSRepPart(data.data,
365 data.length,
366 &dec_rep->enc_part,
367 &size);
368 if (ret)
369 krb5_set_error_message(context, ret,
370 N_("Failed to decode encpart in ticket", ""));
371 krb5_data_free (&data);
372 return ret;
375 static krb5_error_code
376 get_cred_kdc(krb5_context context,
377 krb5_ccache id,
378 krb5_kdc_flags flags,
379 krb5_addresses *addresses,
380 krb5_creds *in_creds,
381 krb5_creds *krbtgt,
382 krb5_principal impersonate_principal,
383 Ticket *second_ticket,
384 krb5_creds *out_creds)
386 TGS_REQ req;
387 krb5_data enc;
388 krb5_data resp;
389 krb5_kdc_rep rep;
390 KRB_ERROR error;
391 krb5_error_code ret;
392 unsigned nonce;
393 krb5_keyblock *subkey = NULL;
394 size_t len;
395 Ticket second_ticket_data;
396 METHOD_DATA padata;
398 krb5_data_zero(&resp);
399 krb5_data_zero(&enc);
400 padata.val = NULL;
401 padata.len = 0;
403 krb5_generate_random_block(&nonce, sizeof(nonce));
404 nonce &= 0xffffffff;
406 if(flags.b.enc_tkt_in_skey && second_ticket == NULL){
407 ret = decode_Ticket(in_creds->second_ticket.data,
408 in_creds->second_ticket.length,
409 &second_ticket_data, &len);
410 if(ret)
411 return ret;
412 second_ticket = &second_ticket_data;
416 if (impersonate_principal) {
417 krb5_crypto crypto;
418 PA_S4U2Self self;
419 krb5_data data;
420 void *buf;
421 size_t size;
423 self.name = impersonate_principal->name;
424 self.realm = impersonate_principal->realm;
425 self.auth = estrdup("Kerberos");
427 ret = _krb5_s4u2self_to_checksumdata(context, &self, &data);
428 if (ret) {
429 free(self.auth);
430 goto out;
433 ret = krb5_crypto_init(context, &krbtgt->session, 0, &crypto);
434 if (ret) {
435 free(self.auth);
436 krb5_data_free(&data);
437 goto out;
440 ret = krb5_create_checksum(context,
441 crypto,
442 KRB5_KU_OTHER_CKSUM,
444 data.data,
445 data.length,
446 &self.cksum);
447 krb5_crypto_destroy(context, crypto);
448 krb5_data_free(&data);
449 if (ret) {
450 free(self.auth);
451 goto out;
454 ASN1_MALLOC_ENCODE(PA_S4U2Self, buf, len, &self, &size, ret);
455 free(self.auth);
456 free_Checksum(&self.cksum);
457 if (ret)
458 goto out;
459 if (len != size)
460 krb5_abortx(context, "internal asn1 error");
462 ret = krb5_padata_add(context, &padata, KRB5_PADATA_FOR_USER, buf, len);
463 if (ret)
464 goto out;
467 ret = init_tgs_req (context,
469 addresses,
470 flags,
471 second_ticket,
472 in_creds,
473 krbtgt,
474 nonce,
475 &padata,
476 &subkey,
477 &req);
478 if (ret)
479 goto out;
481 ASN1_MALLOC_ENCODE(TGS_REQ, enc.data, enc.length, &req, &len, ret);
482 if (ret)
483 goto out;
484 if(enc.length != len)
485 krb5_abortx(context, "internal error in ASN.1 encoder");
487 /* don't free addresses */
488 req.req_body.addresses = NULL;
489 free_TGS_REQ(&req);
492 * Send and receive
495 krb5_sendto_ctx stctx;
496 ret = krb5_sendto_ctx_alloc(context, &stctx);
497 if (ret)
498 return ret;
499 krb5_sendto_ctx_set_func(stctx, _krb5_kdc_retry, NULL);
501 ret = krb5_sendto_context (context, stctx, &enc,
502 krbtgt->server->name.name_string.val[1],
503 &resp);
504 krb5_sendto_ctx_free(context, stctx);
506 if(ret)
507 goto out;
509 memset(&rep, 0, sizeof(rep));
510 if(decode_TGS_REP(resp.data, resp.length, &rep.kdc_rep, &len) == 0) {
511 unsigned eflags = 0;
513 ret = krb5_copy_principal(context,
514 in_creds->client,
515 &out_creds->client);
516 if(ret)
517 goto out2;
518 ret = krb5_copy_principal(context,
519 in_creds->server,
520 &out_creds->server);
521 if(ret)
522 goto out2;
523 /* this should go someplace else */
524 out_creds->times.endtime = in_creds->times.endtime;
526 /* XXX should do better testing */
527 if (flags.b.constrained_delegation || impersonate_principal)
528 eflags |= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH;
530 ret = _krb5_extract_ticket(context,
531 &rep,
532 out_creds,
533 &krbtgt->session,
534 NULL,
536 &krbtgt->addresses,
537 nonce,
538 eflags,
539 decrypt_tkt_with_subkey,
540 subkey);
541 out2:
542 krb5_free_kdc_rep(context, &rep);
543 } else if(krb5_rd_error(context, &resp, &error) == 0) {
544 ret = krb5_error_from_rd_error(context, &error, in_creds);
545 krb5_free_error_contents(context, &error);
546 } else if(resp.length > 0 && ((char*)resp.data)[0] == 4) {
547 ret = KRB5KRB_AP_ERR_V4_REPLY;
548 krb5_clear_error_message(context);
549 } else {
550 ret = KRB5KRB_AP_ERR_MSG_TYPE;
551 krb5_clear_error_message(context);
554 out:
555 if (second_ticket == &second_ticket_data)
556 free_Ticket(&second_ticket_data);
557 free_METHOD_DATA(&padata);
558 krb5_data_free(&resp);
559 krb5_data_free(&enc);
560 if(subkey)
561 krb5_free_keyblock(context, subkey);
562 return ret;
567 * same as above, just get local addresses first if the krbtgt have
568 * them and the realm is not addressless
571 static krb5_error_code
572 get_cred_kdc_address(krb5_context context,
573 krb5_ccache id,
574 krb5_kdc_flags flags,
575 krb5_addresses *addrs,
576 krb5_creds *in_creds,
577 krb5_creds *krbtgt,
578 krb5_principal impersonate_principal,
579 Ticket *second_ticket,
580 krb5_creds *out_creds)
582 krb5_error_code ret;
583 krb5_addresses addresses = { 0, NULL };
586 * Inherit the address-ness of the krbtgt if the address is not
587 * specified.
590 if (addrs == NULL && krbtgt->addresses.len != 0) {
591 krb5_boolean noaddr;
593 krb5_appdefault_boolean(context, NULL, krbtgt->server->realm,
594 "no-addresses", FALSE, &noaddr);
596 if (!noaddr) {
597 krb5_get_all_client_addrs(context, &addresses);
598 /* XXX this sucks. */
599 addrs = &addresses;
600 if(addresses.len == 0)
601 addrs = NULL;
604 ret = get_cred_kdc(context, id, flags, addrs, in_creds,
605 krbtgt, impersonate_principal,
606 second_ticket, out_creds);
607 krb5_free_addresses(context, &addresses);
608 return ret;
611 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
612 krb5_get_kdc_cred(krb5_context context,
613 krb5_ccache id,
614 krb5_kdc_flags flags,
615 krb5_addresses *addresses,
616 Ticket *second_ticket,
617 krb5_creds *in_creds,
618 krb5_creds **out_creds
621 krb5_error_code ret;
622 krb5_creds *krbtgt;
624 *out_creds = calloc(1, sizeof(**out_creds));
625 if(*out_creds == NULL) {
626 krb5_set_error_message(context, ENOMEM,
627 N_("malloc: out of memory", ""));
628 return ENOMEM;
630 ret = _krb5_get_krbtgt (context,
632 in_creds->server->realm,
633 &krbtgt);
634 if(ret) {
635 free(*out_creds);
636 *out_creds = NULL;
637 return ret;
639 ret = get_cred_kdc(context, id, flags, addresses,
640 in_creds, krbtgt, NULL, NULL, *out_creds);
641 krb5_free_creds (context, krbtgt);
642 if(ret) {
643 free(*out_creds);
644 *out_creds = NULL;
646 return ret;
649 static int
650 not_found(krb5_context context, krb5_const_principal p, krb5_error_code code)
652 krb5_error_code ret;
653 char *str;
655 ret = krb5_unparse_name(context, p, &str);
656 if(ret) {
657 krb5_clear_error_message(context);
658 return code;
660 krb5_set_error_message(context, code,
661 N_("Matching credential (%s) not found", ""), str);
662 free(str);
663 return code;
666 static krb5_error_code
667 find_cred(krb5_context context,
668 krb5_ccache id,
669 krb5_principal server,
670 krb5_creds **tgts,
671 krb5_creds *out_creds)
673 krb5_error_code ret;
674 krb5_creds mcreds;
676 krb5_cc_clear_mcred(&mcreds);
677 mcreds.server = server;
678 ret = krb5_cc_retrieve_cred(context, id, KRB5_TC_DONT_MATCH_REALM,
679 &mcreds, out_creds);
680 if(ret == 0)
681 return 0;
682 while(tgts && *tgts){
683 if(krb5_compare_creds(context, KRB5_TC_DONT_MATCH_REALM,
684 &mcreds, *tgts)){
685 ret = krb5_copy_creds_contents(context, *tgts, out_creds);
686 return ret;
688 tgts++;
690 return not_found(context, server, KRB5_CC_NOTFOUND);
693 static krb5_error_code
694 add_cred(krb5_context context, krb5_creds const *tkt, krb5_creds ***tgts)
696 int i;
697 krb5_error_code ret;
698 krb5_creds **tmp = *tgts;
700 for(i = 0; tmp && tmp[i]; i++); /* XXX */
701 tmp = realloc(tmp, (i+2)*sizeof(*tmp));
702 if(tmp == NULL) {
703 krb5_set_error_message(context, ENOMEM,
704 N_("malloc: out of memory", ""));
705 return ENOMEM;
707 *tgts = tmp;
708 ret = krb5_copy_creds(context, tkt, &tmp[i]);
709 tmp[i+1] = NULL;
710 return ret;
714 get_cred(server)
715 creds = cc_get_cred(server)
716 if(creds) return creds
717 tgt = cc_get_cred(krbtgt/server_realm@any_realm)
718 if(tgt)
719 return get_cred_tgt(server, tgt)
720 if(client_realm == server_realm)
721 return NULL
722 tgt = get_cred(krbtgt/server_realm@client_realm)
723 while(tgt_inst != server_realm)
724 tgt = get_cred(krbtgt/server_realm@tgt_inst)
725 return get_cred_tgt(server, tgt)
728 static krb5_error_code
729 get_cred_kdc_capath(krb5_context context,
730 krb5_kdc_flags flags,
731 krb5_ccache ccache,
732 krb5_creds *in_creds,
733 krb5_principal impersonate_principal,
734 Ticket *second_ticket,
735 krb5_creds **out_creds,
736 krb5_creds ***ret_tgts)
738 krb5_error_code ret;
739 krb5_creds *tgt, tmp_creds;
740 krb5_const_realm client_realm, server_realm, try_realm;
741 int ok_as_delegate = 1;
743 *out_creds = NULL;
745 client_realm = krb5_principal_get_realm(context, in_creds->client);
746 server_realm = krb5_principal_get_realm(context, in_creds->server);
747 memset(&tmp_creds, 0, sizeof(tmp_creds));
748 ret = krb5_copy_principal(context, in_creds->client, &tmp_creds.client);
749 if(ret)
750 return ret;
752 try_realm = krb5_config_get_string(context, NULL, "capaths",
753 client_realm, server_realm, NULL);
754 if (try_realm == NULL)
755 try_realm = client_realm;
757 ret = krb5_make_principal(context,
758 &tmp_creds.server,
759 try_realm,
760 KRB5_TGS_NAME,
761 server_realm,
762 NULL);
763 if(ret){
764 krb5_free_principal(context, tmp_creds.client);
765 return ret;
768 krb5_creds tgts;
770 ret = find_cred(context, ccache, tmp_creds.server,
771 *ret_tgts, &tgts);
772 if(ret == 0){
773 if (try_realm != client_realm)
774 ok_as_delegate = tgts.flags.b.ok_as_delegate;
776 *out_creds = calloc(1, sizeof(**out_creds));
777 if(*out_creds == NULL) {
778 ret = ENOMEM;
779 krb5_set_error_message(context, ret,
780 N_("malloc: out of memory", ""));
781 } else {
782 ret = get_cred_kdc_address(context, ccache, flags, NULL,
783 in_creds, &tgts,
784 impersonate_principal,
785 second_ticket,
786 *out_creds);
787 if (ret) {
788 free (*out_creds);
789 *out_creds = NULL;
790 } else if (ok_as_delegate == 0)
791 (*out_creds)->flags.b.ok_as_delegate = 0;
793 krb5_free_cred_contents(context, &tgts);
794 krb5_free_principal(context, tmp_creds.server);
795 krb5_free_principal(context, tmp_creds.client);
796 return ret;
799 if(krb5_realm_compare(context, in_creds->client, in_creds->server))
800 return not_found(context, in_creds->server, KRB5_CC_NOTFOUND);
802 /* XXX this can loop forever */
803 while(1){
804 heim_general_string tgt_inst;
806 ret = get_cred_kdc_capath(context, flags, ccache, &tmp_creds,
807 NULL, NULL, &tgt, ret_tgts);
808 if(ret) {
809 krb5_free_principal(context, tmp_creds.server);
810 krb5_free_principal(context, tmp_creds.client);
811 return ret;
814 * if either of the chain or the ok_as_delegate was stripped
815 * by the kdc, make sure we strip it too.
817 if (ok_as_delegate == 0 || tgt->flags.b.ok_as_delegate == 0) {
818 ok_as_delegate = 0;
819 tgt->flags.b.ok_as_delegate = 0;
822 ret = add_cred(context, tgt, ret_tgts);
823 if(ret) {
824 krb5_free_principal(context, tmp_creds.server);
825 krb5_free_principal(context, tmp_creds.client);
826 return ret;
828 tgt_inst = tgt->server->name.name_string.val[1];
829 if(strcmp(tgt_inst, server_realm) == 0)
830 break;
831 krb5_free_principal(context, tmp_creds.server);
832 ret = krb5_make_principal(context, &tmp_creds.server,
833 tgt_inst, KRB5_TGS_NAME, server_realm, NULL);
834 if(ret) {
835 krb5_free_principal(context, tmp_creds.server);
836 krb5_free_principal(context, tmp_creds.client);
837 return ret;
839 ret = krb5_free_creds(context, tgt);
840 if(ret) {
841 krb5_free_principal(context, tmp_creds.server);
842 krb5_free_principal(context, tmp_creds.client);
843 return ret;
847 krb5_free_principal(context, tmp_creds.server);
848 krb5_free_principal(context, tmp_creds.client);
849 *out_creds = calloc(1, sizeof(**out_creds));
850 if(*out_creds == NULL) {
851 ret = ENOMEM;
852 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
853 } else {
854 ret = get_cred_kdc_address (context, ccache, flags, NULL,
855 in_creds, tgt, impersonate_principal,
856 second_ticket, *out_creds);
857 if (ret) {
858 free (*out_creds);
859 *out_creds = NULL;
862 krb5_free_creds(context, tgt);
863 return ret;
866 static krb5_error_code
867 get_cred_kdc_referral(krb5_context context,
868 krb5_kdc_flags flags,
869 krb5_ccache ccache,
870 krb5_creds *in_creds,
871 krb5_principal impersonate_principal,
872 Ticket *second_ticket,
873 krb5_creds **out_creds,
874 krb5_creds ***ret_tgts)
876 krb5_const_realm client_realm;
877 krb5_error_code ret;
878 krb5_creds tgt, referral, ticket;
879 int loop = 0;
880 int ok_as_delegate = 1;
882 if (in_creds->server->name.name_string.len < 2 && !flags.b.canonicalize) {
883 krb5_set_error_message(context, KRB5KDC_ERR_PATH_NOT_ACCEPTED,
884 N_("Name too short to do referals, skipping", ""));
885 return KRB5KDC_ERR_PATH_NOT_ACCEPTED;
888 memset(&tgt, 0, sizeof(tgt));
889 memset(&ticket, 0, sizeof(ticket));
891 flags.b.canonicalize = 1;
893 *out_creds = NULL;
895 client_realm = krb5_principal_get_realm(context, in_creds->client);
897 /* find tgt for the clients base realm */
899 krb5_principal tgtname;
901 ret = krb5_make_principal(context, &tgtname,
902 client_realm,
903 KRB5_TGS_NAME,
904 client_realm,
905 NULL);
906 if(ret)
907 return ret;
909 ret = find_cred(context, ccache, tgtname, *ret_tgts, &tgt);
910 krb5_free_principal(context, tgtname);
911 if (ret)
912 return ret;
915 referral = *in_creds;
916 ret = krb5_copy_principal(context, in_creds->server, &referral.server);
917 if (ret) {
918 krb5_free_cred_contents(context, &tgt);
919 return ret;
921 ret = krb5_principal_set_realm(context, referral.server, client_realm);
922 if (ret) {
923 krb5_free_cred_contents(context, &tgt);
924 krb5_free_principal(context, referral.server);
925 return ret;
928 while (loop++ < 17) {
929 krb5_creds **tickets;
930 krb5_creds mcreds;
931 char *referral_realm;
933 /* Use cache if we are not doing impersonation or contrainte deleg */
934 if (impersonate_principal == NULL || flags.b.constrained_delegation) {
935 krb5_cc_clear_mcred(&mcreds);
936 mcreds.server = referral.server;
937 ret = krb5_cc_retrieve_cred(context, ccache, 0, &mcreds, &ticket);
938 } else
939 ret = EINVAL;
941 if (ret) {
942 ret = get_cred_kdc_address(context, ccache, flags, NULL,
943 &referral, &tgt, impersonate_principal,
944 second_ticket, &ticket);
945 if (ret)
946 goto out;
949 /* Did we get the right ticket ? */
950 if (krb5_principal_compare_any_realm(context,
951 referral.server,
952 ticket.server))
953 break;
955 if (!krb5_principal_is_krbtgt(context, ticket.server)) {
956 krb5_set_error_message(context, KRB5KRB_AP_ERR_NOT_US,
957 N_("Got back an non krbtgt "
958 "ticket referrals", ""));
959 ret = KRB5KRB_AP_ERR_NOT_US;
960 goto out;
963 referral_realm = ticket.server->name.name_string.val[1];
965 /* check that there are no referrals loops */
966 tickets = *ret_tgts;
968 krb5_cc_clear_mcred(&mcreds);
969 mcreds.server = ticket.server;
971 while(tickets && *tickets){
972 if(krb5_compare_creds(context,
973 KRB5_TC_DONT_MATCH_REALM,
974 &mcreds,
975 *tickets))
977 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
978 N_("Referral from %s "
979 "loops back to realm %s", ""),
980 tgt.server->realm,
981 referral_realm);
982 ret = KRB5_GET_IN_TKT_LOOP;
983 goto out;
985 tickets++;
989 * if either of the chain or the ok_as_delegate was stripped
990 * by the kdc, make sure we strip it too.
993 if (ok_as_delegate == 0 || ticket.flags.b.ok_as_delegate == 0) {
994 ok_as_delegate = 0;
995 ticket.flags.b.ok_as_delegate = 0;
998 ret = add_cred(context, &ticket, ret_tgts);
999 if (ret)
1000 goto out;
1002 /* try realm in the referral */
1003 ret = krb5_principal_set_realm(context,
1004 referral.server,
1005 referral_realm);
1006 krb5_free_cred_contents(context, &tgt);
1007 tgt = ticket;
1008 memset(&ticket, 0, sizeof(ticket));
1009 if (ret)
1010 goto out;
1013 ret = krb5_copy_creds(context, &ticket, out_creds);
1015 out:
1016 krb5_free_principal(context, referral.server);
1017 krb5_free_cred_contents(context, &tgt);
1018 krb5_free_cred_contents(context, &ticket);
1019 return ret;
1024 * Glue function between referrals version and old client chasing
1025 * codebase.
1028 krb5_error_code
1029 _krb5_get_cred_kdc_any(krb5_context context,
1030 krb5_kdc_flags flags,
1031 krb5_ccache ccache,
1032 krb5_creds *in_creds,
1033 krb5_principal impersonate_principal,
1034 Ticket *second_ticket,
1035 krb5_creds **out_creds,
1036 krb5_creds ***ret_tgts)
1038 krb5_error_code ret;
1039 krb5_deltat offset;
1041 ret = krb5_cc_get_kdc_offset(context, ccache, &offset);
1042 if (ret) {
1043 context->kdc_sec_offset = offset;
1044 context->kdc_usec_offset = 0;
1047 ret = get_cred_kdc_referral(context,
1048 flags,
1049 ccache,
1050 in_creds,
1051 impersonate_principal,
1052 second_ticket,
1053 out_creds,
1054 ret_tgts);
1055 if (ret == 0 || flags.b.canonicalize)
1056 return ret;
1057 return get_cred_kdc_capath(context,
1058 flags,
1059 ccache,
1060 in_creds,
1061 impersonate_principal,
1062 second_ticket,
1063 out_creds,
1064 ret_tgts);
1068 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1069 krb5_get_credentials_with_flags(krb5_context context,
1070 krb5_flags options,
1071 krb5_kdc_flags flags,
1072 krb5_ccache ccache,
1073 krb5_creds *in_creds,
1074 krb5_creds **out_creds)
1076 krb5_error_code ret;
1077 krb5_creds **tgts;
1078 krb5_creds *res_creds;
1079 int i;
1081 if (in_creds->session.keytype) {
1082 ret = krb5_enctype_valid(context, in_creds->session.keytype);
1083 if (ret)
1084 return ret;
1087 *out_creds = NULL;
1088 res_creds = calloc(1, sizeof(*res_creds));
1089 if (res_creds == NULL) {
1090 krb5_set_error_message(context, ENOMEM,
1091 N_("malloc: out of memory", ""));
1092 return ENOMEM;
1095 if (in_creds->session.keytype)
1096 options |= KRB5_TC_MATCH_KEYTYPE;
1099 * If we got a credential, check if credential is expired before
1100 * returning it.
1102 ret = krb5_cc_retrieve_cred(context,
1103 ccache,
1104 in_creds->session.keytype ?
1105 KRB5_TC_MATCH_KEYTYPE : 0,
1106 in_creds, res_creds);
1108 * If we got a credential, check if credential is expired before
1109 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1111 if (ret == 0) {
1112 krb5_timestamp timeret;
1114 /* If expired ok, don't bother checking */
1115 if(options & KRB5_GC_EXPIRED_OK) {
1116 *out_creds = res_creds;
1117 return 0;
1120 krb5_timeofday(context, &timeret);
1121 if(res_creds->times.endtime > timeret) {
1122 *out_creds = res_creds;
1123 return 0;
1125 if(options & KRB5_GC_CACHED)
1126 krb5_cc_remove_cred(context, ccache, 0, res_creds);
1128 } else if(ret != KRB5_CC_END) {
1129 free(res_creds);
1130 return ret;
1132 free(res_creds);
1133 if(options & KRB5_GC_CACHED)
1134 return not_found(context, in_creds->server, KRB5_CC_NOTFOUND);
1136 if(options & KRB5_GC_USER_USER)
1137 flags.b.enc_tkt_in_skey = 1;
1138 if (flags.b.enc_tkt_in_skey)
1139 options |= KRB5_GC_NO_STORE;
1141 tgts = NULL;
1142 ret = _krb5_get_cred_kdc_any(context, flags, ccache,
1143 in_creds, NULL, NULL, out_creds, &tgts);
1144 for(i = 0; tgts && tgts[i]; i++) {
1145 krb5_cc_store_cred(context, ccache, tgts[i]);
1146 krb5_free_creds(context, tgts[i]);
1148 free(tgts);
1149 if(ret == 0 && (options & KRB5_GC_NO_STORE) == 0)
1150 krb5_cc_store_cred(context, ccache, *out_creds);
1151 return ret;
1154 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1155 krb5_get_credentials(krb5_context context,
1156 krb5_flags options,
1157 krb5_ccache ccache,
1158 krb5_creds *in_creds,
1159 krb5_creds **out_creds)
1161 krb5_kdc_flags flags;
1162 flags.i = 0;
1163 return krb5_get_credentials_with_flags(context, options, flags,
1164 ccache, in_creds, out_creds);
1167 struct krb5_get_creds_opt_data {
1168 krb5_principal self;
1169 krb5_flags options;
1170 krb5_enctype enctype;
1171 Ticket *ticket;
1175 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1176 krb5_get_creds_opt_alloc(krb5_context context, krb5_get_creds_opt *opt)
1178 *opt = calloc(1, sizeof(**opt));
1179 if (*opt == NULL) {
1180 krb5_set_error_message(context, ENOMEM,
1181 N_("malloc: out of memory", ""));
1182 return ENOMEM;
1184 return 0;
1187 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1188 krb5_get_creds_opt_free(krb5_context context, krb5_get_creds_opt opt)
1190 if (opt->self)
1191 krb5_free_principal(context, opt->self);
1192 if (opt->ticket) {
1193 free_Ticket(opt->ticket);
1194 free(opt->ticket);
1196 memset(opt, 0, sizeof(*opt));
1197 free(opt);
1200 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1201 krb5_get_creds_opt_set_options(krb5_context context,
1202 krb5_get_creds_opt opt,
1203 krb5_flags options)
1205 opt->options = options;
1208 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1209 krb5_get_creds_opt_add_options(krb5_context context,
1210 krb5_get_creds_opt opt,
1211 krb5_flags options)
1213 opt->options |= options;
1216 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1217 krb5_get_creds_opt_set_enctype(krb5_context context,
1218 krb5_get_creds_opt opt,
1219 krb5_enctype enctype)
1221 opt->enctype = enctype;
1224 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1225 krb5_get_creds_opt_set_impersonate(krb5_context context,
1226 krb5_get_creds_opt opt,
1227 krb5_const_principal self)
1229 if (opt->self)
1230 krb5_free_principal(context, opt->self);
1231 return krb5_copy_principal(context, self, &opt->self);
1234 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1235 krb5_get_creds_opt_set_ticket(krb5_context context,
1236 krb5_get_creds_opt opt,
1237 const Ticket *ticket)
1239 if (opt->ticket) {
1240 free_Ticket(opt->ticket);
1241 free(opt->ticket);
1242 opt->ticket = NULL;
1244 if (ticket) {
1245 krb5_error_code ret;
1247 opt->ticket = malloc(sizeof(*ticket));
1248 if (opt->ticket == NULL) {
1249 krb5_set_error_message(context, ENOMEM,
1250 N_("malloc: out of memory", ""));
1251 return ENOMEM;
1253 ret = copy_Ticket(ticket, opt->ticket);
1254 if (ret) {
1255 free(opt->ticket);
1256 opt->ticket = NULL;
1257 krb5_set_error_message(context, ret,
1258 N_("malloc: out of memory", ""));
1259 return ret;
1262 return 0;
1267 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1268 krb5_get_creds(krb5_context context,
1269 krb5_get_creds_opt opt,
1270 krb5_ccache ccache,
1271 krb5_const_principal inprinc,
1272 krb5_creds **out_creds)
1274 krb5_kdc_flags flags;
1275 krb5_flags options;
1276 krb5_creds in_creds;
1277 krb5_error_code ret;
1278 krb5_creds **tgts;
1279 krb5_creds *res_creds;
1280 int i;
1282 if (opt && opt->enctype) {
1283 ret = krb5_enctype_valid(context, opt->enctype);
1284 if (ret)
1285 return ret;
1288 memset(&in_creds, 0, sizeof(in_creds));
1289 in_creds.server = rk_UNCONST(inprinc);
1291 ret = krb5_cc_get_principal(context, ccache, &in_creds.client);
1292 if (ret)
1293 return ret;
1295 if (opt)
1296 options = opt->options;
1297 else
1298 options = 0;
1299 flags.i = 0;
1301 *out_creds = NULL;
1302 res_creds = calloc(1, sizeof(*res_creds));
1303 if (res_creds == NULL) {
1304 krb5_free_principal(context, in_creds.client);
1305 krb5_set_error_message(context, ENOMEM,
1306 N_("malloc: out of memory", ""));
1307 return ENOMEM;
1310 if (opt && opt->enctype) {
1311 in_creds.session.keytype = opt->enctype;
1312 options |= KRB5_TC_MATCH_KEYTYPE;
1316 * If we got a credential, check if credential is expired before
1317 * returning it.
1319 ret = krb5_cc_retrieve_cred(context,
1320 ccache,
1321 options & KRB5_TC_MATCH_KEYTYPE,
1322 &in_creds, res_creds);
1324 * If we got a credential, check if credential is expired before
1325 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1327 if (ret == 0) {
1328 krb5_timestamp timeret;
1330 /* If expired ok, don't bother checking */
1331 if(options & KRB5_GC_EXPIRED_OK) {
1332 *out_creds = res_creds;
1333 krb5_free_principal(context, in_creds.client);
1334 goto out;
1337 krb5_timeofday(context, &timeret);
1338 if(res_creds->times.endtime > timeret) {
1339 *out_creds = res_creds;
1340 krb5_free_principal(context, in_creds.client);
1341 goto out;
1343 if(options & KRB5_GC_CACHED)
1344 krb5_cc_remove_cred(context, ccache, 0, res_creds);
1346 } else if(ret != KRB5_CC_END) {
1347 free(res_creds);
1348 krb5_free_principal(context, in_creds.client);
1349 goto out;
1351 free(res_creds);
1352 if(options & KRB5_GC_CACHED) {
1353 krb5_free_principal(context, in_creds.client);
1354 ret = not_found(context, in_creds.server, KRB5_CC_NOTFOUND);
1355 goto out;
1357 if(options & KRB5_GC_USER_USER) {
1358 flags.b.enc_tkt_in_skey = 1;
1359 options |= KRB5_GC_NO_STORE;
1361 if (options & KRB5_GC_FORWARDABLE)
1362 flags.b.forwardable = 1;
1363 if (options & KRB5_GC_NO_TRANSIT_CHECK)
1364 flags.b.disable_transited_check = 1;
1365 if (options & KRB5_GC_CONSTRAINED_DELEGATION) {
1366 flags.b.request_anonymous = 1; /* XXX ARGH confusion */
1367 flags.b.constrained_delegation = 1;
1369 if (options & KRB5_GC_CANONICALIZE)
1370 flags.b.canonicalize = 1;
1372 tgts = NULL;
1373 ret = _krb5_get_cred_kdc_any(context, flags, ccache,
1374 &in_creds, opt->self, opt->ticket,
1375 out_creds, &tgts);
1376 krb5_free_principal(context, in_creds.client);
1377 for(i = 0; tgts && tgts[i]; i++) {
1378 krb5_cc_store_cred(context, ccache, tgts[i]);
1379 krb5_free_creds(context, tgts[i]);
1381 free(tgts);
1382 if(ret == 0 && (options & KRB5_GC_NO_STORE) == 0)
1383 krb5_cc_store_cred(context, ccache, *out_creds);
1385 out:
1386 _krb5_debug(context, 5, "krb5_get_creds: ret = %d", ret);
1388 return ret;
1395 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1396 krb5_get_renewed_creds(krb5_context context,
1397 krb5_creds *creds,
1398 krb5_const_principal client,
1399 krb5_ccache ccache,
1400 const char *in_tkt_service)
1402 krb5_error_code ret;
1403 krb5_kdc_flags flags;
1404 krb5_creds in, *template, *out = NULL;
1406 memset(&in, 0, sizeof(in));
1407 memset(creds, 0, sizeof(*creds));
1409 ret = krb5_copy_principal(context, client, &in.client);
1410 if (ret)
1411 return ret;
1413 if (in_tkt_service) {
1414 ret = krb5_parse_name(context, in_tkt_service, &in.server);
1415 if (ret) {
1416 krb5_free_principal(context, in.client);
1417 return ret;
1419 } else {
1420 const char *realm = krb5_principal_get_realm(context, client);
1422 ret = krb5_make_principal(context, &in.server, realm, KRB5_TGS_NAME,
1423 realm, NULL);
1424 if (ret) {
1425 krb5_free_principal(context, in.client);
1426 return ret;
1430 flags.i = 0;
1431 flags.b.renewable = flags.b.renew = 1;
1434 * Get template from old credential cache for the same entry, if
1435 * this failes, no worries.
1437 ret = krb5_get_credentials(context, KRB5_GC_CACHED, ccache, &in, &template);
1438 if (ret == 0) {
1439 flags.b.forwardable = template->flags.b.forwardable;
1440 flags.b.proxiable = template->flags.b.proxiable;
1441 krb5_free_creds (context, template);
1444 ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &in, &out);
1445 krb5_free_principal(context, in.client);
1446 krb5_free_principal(context, in.server);
1447 if (ret)
1448 return ret;
1450 ret = krb5_copy_creds_contents(context, out, creds);
1451 krb5_free_creds(context, out);
1453 return ret;