export more
[heimdal.git] / lib / krb5 / get_cred.c
blob2936d837e8e69ea8e1a19f787de80f753d3f50ec
1 /*
2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include <krb5_locl.h>
35 #include <assert.h>
38 * Take the `body' and encode it into `padata' using the credentials
39 * in `creds'.
42 static krb5_error_code
43 make_pa_tgs_req(krb5_context context,
44 krb5_auth_context ac,
45 KDC_REQ_BODY *body,
46 PA_DATA *padata,
47 krb5_creds *creds)
49 u_char *buf;
50 size_t buf_size;
51 size_t len;
52 krb5_data in_data;
53 krb5_error_code ret;
55 ASN1_MALLOC_ENCODE(KDC_REQ_BODY, buf, buf_size, body, &len, ret);
56 if (ret)
57 goto out;
58 if(buf_size != len)
59 krb5_abortx(context, "internal error in ASN.1 encoder");
61 in_data.length = len;
62 in_data.data = buf;
63 ret = _krb5_mk_req_internal(context, &ac, 0, &in_data, creds,
64 &padata->padata_value,
65 KRB5_KU_TGS_REQ_AUTH_CKSUM,
66 KRB5_KU_TGS_REQ_AUTH);
67 out:
68 free (buf);
69 if(ret)
70 return ret;
71 padata->padata_type = KRB5_PADATA_TGS_REQ;
72 return 0;
76 * Set the `enc-authorization-data' in `req_body' based on `authdata'
79 static krb5_error_code
80 set_auth_data (krb5_context context,
81 KDC_REQ_BODY *req_body,
82 krb5_authdata *authdata,
83 krb5_keyblock *subkey)
85 if(authdata->len) {
86 size_t len, buf_size;
87 unsigned char *buf;
88 krb5_crypto crypto;
89 krb5_error_code ret;
91 ASN1_MALLOC_ENCODE(AuthorizationData, buf, buf_size, authdata,
92 &len, ret);
93 if (ret)
94 return ret;
95 if (buf_size != len)
96 krb5_abortx(context, "internal error in ASN.1 encoder");
98 ALLOC(req_body->enc_authorization_data, 1);
99 if (req_body->enc_authorization_data == NULL) {
100 free (buf);
101 krb5_set_error_message(context, ENOMEM,
102 N_("malloc: out of memory", ""));
103 return ENOMEM;
105 ret = krb5_crypto_init(context, subkey, 0, &crypto);
106 if (ret) {
107 free (buf);
108 free (req_body->enc_authorization_data);
109 req_body->enc_authorization_data = NULL;
110 return ret;
112 krb5_encrypt_EncryptedData(context,
113 crypto,
114 KRB5_KU_TGS_REQ_AUTH_DAT_SUBKEY,
115 buf,
116 len,
118 req_body->enc_authorization_data);
119 free (buf);
120 krb5_crypto_destroy(context, crypto);
121 } else {
122 req_body->enc_authorization_data = NULL;
124 return 0;
128 * Create a tgs-req in `t' with `addresses', `flags', `second_ticket'
129 * (if not-NULL), `in_creds', `krbtgt', and returning the generated
130 * subkey in `subkey'.
133 static krb5_error_code
134 init_tgs_req (krb5_context context,
135 krb5_ccache ccache,
136 krb5_addresses *addresses,
137 krb5_kdc_flags flags,
138 Ticket *second_ticket,
139 krb5_creds *in_creds,
140 krb5_creds *krbtgt,
141 unsigned nonce,
142 const METHOD_DATA *padata,
143 krb5_keyblock **subkey,
144 TGS_REQ *t)
146 krb5_auth_context ac = NULL;
147 krb5_error_code ret = 0;
149 memset(t, 0, sizeof(*t));
150 t->pvno = 5;
151 t->msg_type = krb_tgs_req;
152 if (in_creds->session.keytype) {
153 ALLOC_SEQ(&t->req_body.etype, 1);
154 if(t->req_body.etype.val == NULL) {
155 ret = ENOMEM;
156 krb5_set_error_message(context, ret,
157 N_("malloc: out of memory", ""));
158 goto fail;
160 t->req_body.etype.val[0] = in_creds->session.keytype;
161 } else {
162 ret = krb5_init_etype(context,
163 &t->req_body.etype.len,
164 &t->req_body.etype.val,
165 NULL);
167 if (ret)
168 goto fail;
169 t->req_body.addresses = addresses;
170 t->req_body.kdc_options = flags.b;
171 ret = copy_Realm(&in_creds->server->realm, &t->req_body.realm);
172 if (ret)
173 goto fail;
174 ALLOC(t->req_body.sname, 1);
175 if (t->req_body.sname == NULL) {
176 ret = ENOMEM;
177 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
178 goto fail;
181 /* some versions of some code might require that the client be
182 present in TGS-REQs, but this is clearly against the spec */
184 ret = copy_PrincipalName(&in_creds->server->name, t->req_body.sname);
185 if (ret)
186 goto fail;
188 /* req_body.till should be NULL if there is no endtime specified,
189 but old MIT code (like DCE secd) doesn't like that */
190 ALLOC(t->req_body.till, 1);
191 if(t->req_body.till == NULL){
192 ret = ENOMEM;
193 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
194 goto fail;
196 *t->req_body.till = in_creds->times.endtime;
198 t->req_body.nonce = nonce;
199 if(second_ticket){
200 ALLOC(t->req_body.additional_tickets, 1);
201 if (t->req_body.additional_tickets == NULL) {
202 ret = ENOMEM;
203 krb5_set_error_message(context, ret,
204 N_("malloc: out of memory", ""));
205 goto fail;
207 ALLOC_SEQ(t->req_body.additional_tickets, 1);
208 if (t->req_body.additional_tickets->val == NULL) {
209 ret = ENOMEM;
210 krb5_set_error_message(context, ret,
211 N_("malloc: out of memory", ""));
212 goto fail;
214 ret = copy_Ticket(second_ticket, t->req_body.additional_tickets->val);
215 if (ret)
216 goto fail;
218 ALLOC(t->padata, 1);
219 if (t->padata == NULL) {
220 ret = ENOMEM;
221 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
222 goto fail;
224 ALLOC_SEQ(t->padata, 1 + padata->len);
225 if (t->padata->val == NULL) {
226 ret = ENOMEM;
227 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
228 goto fail;
231 int i;
232 for (i = 0; i < padata->len; i++) {
233 ret = copy_PA_DATA(&padata->val[i], &t->padata->val[i + 1]);
234 if (ret) {
235 krb5_set_error_message(context, ret,
236 N_("malloc: out of memory", ""));
237 goto fail;
242 ret = krb5_auth_con_init(context, &ac);
243 if(ret)
244 goto fail;
246 ret = krb5_auth_con_generatelocalsubkey(context, ac, &krbtgt->session);
247 if (ret)
248 goto fail;
250 ret = set_auth_data (context, &t->req_body, &in_creds->authdata,
251 ac->local_subkey);
252 if (ret)
253 goto fail;
255 ret = make_pa_tgs_req(context,
257 &t->req_body,
258 &t->padata->val[0],
259 krbtgt);
260 if(ret)
261 goto fail;
263 ret = krb5_auth_con_getlocalsubkey(context, ac, subkey);
264 if (ret)
265 goto fail;
267 fail:
268 if (ac)
269 krb5_auth_con_free(context, ac);
270 if (ret) {
271 t->req_body.addresses = NULL;
272 free_TGS_REQ (t);
274 return ret;
277 krb5_error_code
278 _krb5_get_krbtgt(krb5_context context,
279 krb5_ccache id,
280 krb5_realm realm,
281 krb5_creds **cred)
283 krb5_error_code ret;
284 krb5_creds tmp_cred;
286 memset(&tmp_cred, 0, sizeof(tmp_cred));
288 ret = krb5_cc_get_principal(context, id, &tmp_cred.client);
289 if (ret)
290 return ret;
292 ret = krb5_make_principal(context,
293 &tmp_cred.server,
294 realm,
295 KRB5_TGS_NAME,
296 realm,
297 NULL);
298 if(ret) {
299 krb5_free_principal(context, tmp_cred.client);
300 return ret;
302 ret = krb5_get_credentials(context,
303 KRB5_GC_CACHED,
305 &tmp_cred,
306 cred);
307 krb5_free_principal(context, tmp_cred.client);
308 krb5_free_principal(context, tmp_cred.server);
309 if(ret)
310 return ret;
311 return 0;
314 /* DCE compatible decrypt proc */
315 static krb5_error_code
316 decrypt_tkt_with_subkey (krb5_context context,
317 krb5_keyblock *key,
318 krb5_key_usage usage,
319 krb5_const_pointer subkey,
320 krb5_kdc_rep *dec_rep)
322 krb5_error_code ret;
323 krb5_data data;
324 size_t size;
325 krb5_crypto crypto;
327 assert(usage == 0);
330 * start out with trying with subkey if we have one
332 if (subkey) {
333 ret = krb5_crypto_init(context, subkey, 0, &crypto);
334 if (ret)
335 return ret;
336 ret = krb5_decrypt_EncryptedData (context,
337 crypto,
338 KRB5_KU_TGS_REP_ENC_PART_SUB_KEY,
339 &dec_rep->kdc_rep.enc_part,
340 &data);
341 krb5_crypto_destroy(context, crypto);
343 if (subkey == NULL || ret) {
344 ret = krb5_crypto_init(context, key, 0, &crypto);
345 if (ret)
346 return ret;
347 ret = krb5_decrypt_EncryptedData (context,
348 crypto,
349 KRB5_KU_TGS_REP_ENC_PART_SESSION,
350 &dec_rep->kdc_rep.enc_part,
351 &data);
352 krb5_crypto_destroy(context, crypto);
354 if (ret)
355 return ret;
357 ret = decode_EncASRepPart(data.data,
358 data.length,
359 &dec_rep->enc_part,
360 &size);
361 if (ret)
362 ret = decode_EncTGSRepPart(data.data,
363 data.length,
364 &dec_rep->enc_part,
365 &size);
366 if (ret)
367 krb5_set_error_message(context, ret,
368 N_("Failed to decode encpart in ticket", ""));
369 krb5_data_free (&data);
370 return ret;
373 static krb5_error_code
374 get_cred_kdc(krb5_context context,
375 krb5_ccache id,
376 krb5_kdc_flags flags,
377 krb5_addresses *addresses,
378 krb5_creds *in_creds,
379 krb5_creds *krbtgt,
380 krb5_principal impersonate_principal,
381 Ticket *second_ticket,
382 krb5_creds *out_creds)
384 TGS_REQ req;
385 krb5_data enc;
386 krb5_data resp;
387 krb5_kdc_rep rep;
388 KRB_ERROR error;
389 krb5_error_code ret;
390 unsigned nonce;
391 krb5_keyblock *subkey = NULL;
392 size_t len;
393 Ticket second_ticket_data;
394 METHOD_DATA padata;
396 krb5_data_zero(&resp);
397 krb5_data_zero(&enc);
398 padata.val = NULL;
399 padata.len = 0;
401 krb5_generate_random_block(&nonce, sizeof(nonce));
402 nonce &= 0xffffffff;
404 if(flags.b.enc_tkt_in_skey && second_ticket == NULL){
405 ret = decode_Ticket(in_creds->second_ticket.data,
406 in_creds->second_ticket.length,
407 &second_ticket_data, &len);
408 if(ret)
409 return ret;
410 second_ticket = &second_ticket_data;
414 if (impersonate_principal) {
415 krb5_crypto crypto;
416 PA_S4U2Self self;
417 krb5_data data;
418 void *buf;
419 size_t size;
421 self.name = impersonate_principal->name;
422 self.realm = impersonate_principal->realm;
423 self.auth = estrdup("Kerberos");
425 ret = _krb5_s4u2self_to_checksumdata(context, &self, &data);
426 if (ret) {
427 free(self.auth);
428 goto out;
431 ret = krb5_crypto_init(context, &krbtgt->session, 0, &crypto);
432 if (ret) {
433 free(self.auth);
434 krb5_data_free(&data);
435 goto out;
438 ret = krb5_create_checksum(context,
439 crypto,
440 KRB5_KU_OTHER_CKSUM,
442 data.data,
443 data.length,
444 &self.cksum);
445 krb5_crypto_destroy(context, crypto);
446 krb5_data_free(&data);
447 if (ret) {
448 free(self.auth);
449 goto out;
452 ASN1_MALLOC_ENCODE(PA_S4U2Self, buf, len, &self, &size, ret);
453 free(self.auth);
454 free_Checksum(&self.cksum);
455 if (ret)
456 goto out;
457 if (len != size)
458 krb5_abortx(context, "internal asn1 error");
460 ret = krb5_padata_add(context, &padata, KRB5_PADATA_FOR_USER, buf, len);
461 if (ret)
462 goto out;
465 ret = init_tgs_req (context,
467 addresses,
468 flags,
469 second_ticket,
470 in_creds,
471 krbtgt,
472 nonce,
473 &padata,
474 &subkey,
475 &req);
476 if (ret)
477 goto out;
479 ASN1_MALLOC_ENCODE(TGS_REQ, enc.data, enc.length, &req, &len, ret);
480 if (ret)
481 goto out;
482 if(enc.length != len)
483 krb5_abortx(context, "internal error in ASN.1 encoder");
485 /* don't free addresses */
486 req.req_body.addresses = NULL;
487 free_TGS_REQ(&req);
490 * Send and receive
493 krb5_sendto_ctx stctx;
494 ret = krb5_sendto_ctx_alloc(context, &stctx);
495 if (ret)
496 return ret;
497 krb5_sendto_ctx_set_func(stctx, _krb5_kdc_retry, NULL);
499 ret = krb5_sendto_context (context, stctx, &enc,
500 krbtgt->server->name.name_string.val[1],
501 &resp);
502 krb5_sendto_ctx_free(context, stctx);
504 if(ret)
505 goto out;
507 memset(&rep, 0, sizeof(rep));
508 if(decode_TGS_REP(resp.data, resp.length, &rep.kdc_rep, &len) == 0) {
509 unsigned eflags = 0;
511 ret = krb5_copy_principal(context,
512 in_creds->client,
513 &out_creds->client);
514 if(ret)
515 goto out2;
516 ret = krb5_copy_principal(context,
517 in_creds->server,
518 &out_creds->server);
519 if(ret)
520 goto out2;
521 /* this should go someplace else */
522 out_creds->times.endtime = in_creds->times.endtime;
524 /* XXX should do better testing */
525 if (flags.b.constrained_delegation || impersonate_principal)
526 eflags |= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH;
528 ret = _krb5_extract_ticket(context,
529 &rep,
530 out_creds,
531 &krbtgt->session,
532 NULL,
534 &krbtgt->addresses,
535 nonce,
536 eflags,
537 decrypt_tkt_with_subkey,
538 subkey);
539 out2:
540 krb5_free_kdc_rep(context, &rep);
541 } else if(krb5_rd_error(context, &resp, &error) == 0) {
542 ret = krb5_error_from_rd_error(context, &error, in_creds);
543 krb5_free_error_contents(context, &error);
544 } else if(resp.length > 0 && ((char*)resp.data)[0] == 4) {
545 ret = KRB5KRB_AP_ERR_V4_REPLY;
546 krb5_clear_error_message(context);
547 } else {
548 ret = KRB5KRB_AP_ERR_MSG_TYPE;
549 krb5_clear_error_message(context);
552 out:
553 if (second_ticket == &second_ticket_data)
554 free_Ticket(&second_ticket_data);
555 free_METHOD_DATA(&padata);
556 krb5_data_free(&resp);
557 krb5_data_free(&enc);
558 if(subkey)
559 krb5_free_keyblock(context, subkey);
560 return ret;
565 * same as above, just get local addresses first if the krbtgt have
566 * them and the realm is not addressless
569 static krb5_error_code
570 get_cred_kdc_address(krb5_context context,
571 krb5_ccache id,
572 krb5_kdc_flags flags,
573 krb5_addresses *addrs,
574 krb5_creds *in_creds,
575 krb5_creds *krbtgt,
576 krb5_principal impersonate_principal,
577 Ticket *second_ticket,
578 krb5_creds *out_creds)
580 krb5_error_code ret;
581 krb5_addresses addresses = { 0, NULL };
584 * Inherit the address-ness of the krbtgt if the address is not
585 * specified.
588 if (addrs == NULL && krbtgt->addresses.len != 0) {
589 krb5_boolean noaddr;
591 krb5_appdefault_boolean(context, NULL, krbtgt->server->realm,
592 "no-addresses", FALSE, &noaddr);
594 if (!noaddr) {
595 krb5_get_all_client_addrs(context, &addresses);
596 /* XXX this sucks. */
597 addrs = &addresses;
598 if(addresses.len == 0)
599 addrs = NULL;
602 ret = get_cred_kdc(context, id, flags, addrs, in_creds,
603 krbtgt, impersonate_principal,
604 second_ticket, out_creds);
605 krb5_free_addresses(context, &addresses);
606 return ret;
609 krb5_error_code KRB5_LIB_FUNCTION
610 krb5_get_kdc_cred(krb5_context context,
611 krb5_ccache id,
612 krb5_kdc_flags flags,
613 krb5_addresses *addresses,
614 Ticket *second_ticket,
615 krb5_creds *in_creds,
616 krb5_creds **out_creds
619 krb5_error_code ret;
620 krb5_creds *krbtgt;
622 *out_creds = calloc(1, sizeof(**out_creds));
623 if(*out_creds == NULL) {
624 krb5_set_error_message(context, ENOMEM,
625 N_("malloc: out of memory", ""));
626 return ENOMEM;
628 ret = _krb5_get_krbtgt (context,
630 in_creds->server->realm,
631 &krbtgt);
632 if(ret) {
633 free(*out_creds);
634 *out_creds = NULL;
635 return ret;
637 ret = get_cred_kdc(context, id, flags, addresses,
638 in_creds, krbtgt, NULL, NULL, *out_creds);
639 krb5_free_creds (context, krbtgt);
640 if(ret) {
641 free(*out_creds);
642 *out_creds = NULL;
644 return ret;
647 static int
648 not_found(krb5_context context, krb5_const_principal p, krb5_error_code code)
650 krb5_error_code ret;
651 char *str;
653 ret = krb5_unparse_name(context, p, &str);
654 if(ret) {
655 krb5_clear_error_message(context);
656 return code;
658 krb5_set_error_message(context, code,
659 N_("Matching credential (%s) not found", ""), str);
660 free(str);
661 return code;
664 static krb5_error_code
665 find_cred(krb5_context context,
666 krb5_ccache id,
667 krb5_principal server,
668 krb5_creds **tgts,
669 krb5_creds *out_creds)
671 krb5_error_code ret;
672 krb5_creds mcreds;
674 krb5_cc_clear_mcred(&mcreds);
675 mcreds.server = server;
676 ret = krb5_cc_retrieve_cred(context, id, KRB5_TC_DONT_MATCH_REALM,
677 &mcreds, out_creds);
678 if(ret == 0)
679 return 0;
680 while(tgts && *tgts){
681 if(krb5_compare_creds(context, KRB5_TC_DONT_MATCH_REALM,
682 &mcreds, *tgts)){
683 ret = krb5_copy_creds_contents(context, *tgts, out_creds);
684 return ret;
686 tgts++;
688 return not_found(context, server, KRB5_CC_NOTFOUND);
691 static krb5_error_code
692 add_cred(krb5_context context, krb5_creds const *tkt, krb5_creds ***tgts)
694 int i;
695 krb5_error_code ret;
696 krb5_creds **tmp = *tgts;
698 for(i = 0; tmp && tmp[i]; i++); /* XXX */
699 tmp = realloc(tmp, (i+2)*sizeof(*tmp));
700 if(tmp == NULL) {
701 krb5_set_error_message(context, ENOMEM,
702 N_("malloc: out of memory", ""));
703 return ENOMEM;
705 *tgts = tmp;
706 ret = krb5_copy_creds(context, tkt, &tmp[i]);
707 tmp[i+1] = NULL;
708 return ret;
712 get_cred(server)
713 creds = cc_get_cred(server)
714 if(creds) return creds
715 tgt = cc_get_cred(krbtgt/server_realm@any_realm)
716 if(tgt)
717 return get_cred_tgt(server, tgt)
718 if(client_realm == server_realm)
719 return NULL
720 tgt = get_cred(krbtgt/server_realm@client_realm)
721 while(tgt_inst != server_realm)
722 tgt = get_cred(krbtgt/server_realm@tgt_inst)
723 return get_cred_tgt(server, tgt)
726 static krb5_error_code
727 get_cred_kdc_capath(krb5_context context,
728 krb5_kdc_flags flags,
729 krb5_ccache ccache,
730 krb5_creds *in_creds,
731 krb5_principal impersonate_principal,
732 Ticket *second_ticket,
733 krb5_creds **out_creds,
734 krb5_creds ***ret_tgts)
736 krb5_error_code ret;
737 krb5_creds *tgt, tmp_creds;
738 krb5_const_realm client_realm, server_realm, try_realm;
739 int ok_as_delegate = 1;
741 *out_creds = NULL;
743 client_realm = krb5_principal_get_realm(context, in_creds->client);
744 server_realm = krb5_principal_get_realm(context, in_creds->server);
745 memset(&tmp_creds, 0, sizeof(tmp_creds));
746 ret = krb5_copy_principal(context, in_creds->client, &tmp_creds.client);
747 if(ret)
748 return ret;
750 try_realm = krb5_config_get_string(context, NULL, "capaths",
751 client_realm, server_realm, NULL);
752 if (try_realm == NULL)
753 try_realm = client_realm;
755 ret = krb5_make_principal(context,
756 &tmp_creds.server,
757 try_realm,
758 KRB5_TGS_NAME,
759 server_realm,
760 NULL);
761 if(ret){
762 krb5_free_principal(context, tmp_creds.client);
763 return ret;
766 krb5_creds tgts;
768 ret = find_cred(context, ccache, tmp_creds.server,
769 *ret_tgts, &tgts);
770 if(ret == 0){
771 if (try_realm != client_realm)
772 ok_as_delegate = tgts.flags.b.ok_as_delegate;
774 *out_creds = calloc(1, sizeof(**out_creds));
775 if(*out_creds == NULL) {
776 ret = ENOMEM;
777 krb5_set_error_message(context, ret,
778 N_("malloc: out of memory", ""));
779 } else {
780 ret = get_cred_kdc_address(context, ccache, flags, NULL,
781 in_creds, &tgts,
782 impersonate_principal,
783 second_ticket,
784 *out_creds);
785 if (ret) {
786 free (*out_creds);
787 *out_creds = NULL;
788 } else if (ok_as_delegate == 0)
789 (*out_creds)->flags.b.ok_as_delegate = 0;
791 krb5_free_cred_contents(context, &tgts);
792 krb5_free_principal(context, tmp_creds.server);
793 krb5_free_principal(context, tmp_creds.client);
794 return ret;
797 if(krb5_realm_compare(context, in_creds->client, in_creds->server))
798 return not_found(context, in_creds->server, KRB5_CC_NOTFOUND);
800 /* XXX this can loop forever */
801 while(1){
802 heim_general_string tgt_inst;
804 ret = get_cred_kdc_capath(context, flags, ccache, &tmp_creds,
805 NULL, NULL, &tgt, ret_tgts);
806 if(ret) {
807 krb5_free_principal(context, tmp_creds.server);
808 krb5_free_principal(context, tmp_creds.client);
809 return ret;
812 * if either of the chain or the ok_as_delegate was stripped
813 * by the kdc, make sure we strip it too.
815 if (ok_as_delegate == 0 || tgt->flags.b.ok_as_delegate == 0) {
816 ok_as_delegate = 0;
817 tgt->flags.b.ok_as_delegate = 0;
820 ret = add_cred(context, tgt, ret_tgts);
821 if(ret) {
822 krb5_free_principal(context, tmp_creds.server);
823 krb5_free_principal(context, tmp_creds.client);
824 return ret;
826 tgt_inst = tgt->server->name.name_string.val[1];
827 if(strcmp(tgt_inst, server_realm) == 0)
828 break;
829 krb5_free_principal(context, tmp_creds.server);
830 ret = krb5_make_principal(context, &tmp_creds.server,
831 tgt_inst, KRB5_TGS_NAME, server_realm, NULL);
832 if(ret) {
833 krb5_free_principal(context, tmp_creds.server);
834 krb5_free_principal(context, tmp_creds.client);
835 return ret;
837 ret = krb5_free_creds(context, tgt);
838 if(ret) {
839 krb5_free_principal(context, tmp_creds.server);
840 krb5_free_principal(context, tmp_creds.client);
841 return ret;
845 krb5_free_principal(context, tmp_creds.server);
846 krb5_free_principal(context, tmp_creds.client);
847 *out_creds = calloc(1, sizeof(**out_creds));
848 if(*out_creds == NULL) {
849 ret = ENOMEM;
850 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
851 } else {
852 ret = get_cred_kdc_address (context, ccache, flags, NULL,
853 in_creds, tgt, impersonate_principal,
854 second_ticket, *out_creds);
855 if (ret) {
856 free (*out_creds);
857 *out_creds = NULL;
860 krb5_free_creds(context, tgt);
861 return ret;
864 static krb5_error_code
865 get_cred_kdc_referral(krb5_context context,
866 krb5_kdc_flags flags,
867 krb5_ccache ccache,
868 krb5_creds *in_creds,
869 krb5_principal impersonate_principal,
870 Ticket *second_ticket,
871 krb5_creds **out_creds,
872 krb5_creds ***ret_tgts)
874 krb5_const_realm client_realm;
875 krb5_error_code ret;
876 krb5_creds tgt, referral, ticket;
877 int loop = 0;
878 int ok_as_delegate = 1;
880 if (in_creds->server->name.name_string.len < 2 && !flags.b.canonicalize) {
881 krb5_set_error_message(context, KRB5KDC_ERR_PATH_NOT_ACCEPTED,
882 N_("Name too short to do referals, skipping", ""));
883 return KRB5KDC_ERR_PATH_NOT_ACCEPTED;
886 memset(&tgt, 0, sizeof(tgt));
887 memset(&ticket, 0, sizeof(ticket));
889 flags.b.canonicalize = 1;
891 *out_creds = NULL;
893 client_realm = krb5_principal_get_realm(context, in_creds->client);
895 /* find tgt for the clients base realm */
897 krb5_principal tgtname;
899 ret = krb5_make_principal(context, &tgtname,
900 client_realm,
901 KRB5_TGS_NAME,
902 client_realm,
903 NULL);
904 if(ret)
905 return ret;
907 ret = find_cred(context, ccache, tgtname, *ret_tgts, &tgt);
908 krb5_free_principal(context, tgtname);
909 if (ret)
910 return ret;
913 referral = *in_creds;
914 ret = krb5_copy_principal(context, in_creds->server, &referral.server);
915 if (ret) {
916 krb5_free_cred_contents(context, &tgt);
917 return ret;
919 ret = krb5_principal_set_realm(context, referral.server, client_realm);
920 if (ret) {
921 krb5_free_cred_contents(context, &tgt);
922 krb5_free_principal(context, referral.server);
923 return ret;
926 while (loop++ < 17) {
927 krb5_creds **tickets;
928 krb5_creds mcreds;
929 char *referral_realm;
931 /* Use cache if we are not doing impersonation or contrainte deleg */
932 if (impersonate_principal == NULL || flags.b.constrained_delegation) {
933 krb5_cc_clear_mcred(&mcreds);
934 mcreds.server = referral.server;
935 ret = krb5_cc_retrieve_cred(context, ccache, 0, &mcreds, &ticket);
936 } else
937 ret = EINVAL;
939 if (ret) {
940 ret = get_cred_kdc_address (context, ccache, flags, NULL,
941 &referral, &tgt, impersonate_principal,
942 second_ticket, &ticket);
943 if (ret)
944 goto out;
947 /* Did we get the right ticket ? */
948 if (krb5_principal_compare_any_realm(context,
949 referral.server,
950 ticket.server))
951 break;
953 if (!krb5_principal_is_krbtgt(context, ticket.server)) {
954 krb5_set_error_message(context, KRB5KRB_AP_ERR_NOT_US,
955 N_("Got back an non krbtgt "
956 "ticket referrals", ""));
957 krb5_free_cred_contents(context, &ticket);
958 return KRB5KRB_AP_ERR_NOT_US;
961 referral_realm = ticket.server->name.name_string.val[1];
963 /* check that there are no referrals loops */
964 tickets = *ret_tgts;
966 krb5_cc_clear_mcred(&mcreds);
967 mcreds.server = ticket.server;
969 while(tickets && *tickets){
970 if(krb5_compare_creds(context,
971 KRB5_TC_DONT_MATCH_REALM,
972 &mcreds,
973 *tickets))
975 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
976 N_("Referral from %s "
977 "loops back to realm %s", ""),
978 tgt.server->realm,
979 referral_realm);
980 krb5_free_cred_contents(context, &ticket);
981 return KRB5_GET_IN_TKT_LOOP;
983 tickets++;
987 * if either of the chain or the ok_as_delegate was stripped
988 * by the kdc, make sure we strip it too.
991 if (ok_as_delegate == 0 || ticket.flags.b.ok_as_delegate == 0) {
992 ok_as_delegate = 0;
993 ticket.flags.b.ok_as_delegate = 0;
996 ret = add_cred(context, &ticket, ret_tgts);
997 if (ret) {
998 krb5_free_cred_contents(context, &ticket);
999 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 return ret;
1023 * Glue function between referrals version and old client chasing
1024 * codebase.
1027 krb5_error_code
1028 _krb5_get_cred_kdc_any(krb5_context context,
1029 krb5_kdc_flags flags,
1030 krb5_ccache ccache,
1031 krb5_creds *in_creds,
1032 krb5_principal impersonate_principal,
1033 Ticket *second_ticket,
1034 krb5_creds **out_creds,
1035 krb5_creds ***ret_tgts)
1037 krb5_error_code ret;
1039 ret = get_cred_kdc_referral(context,
1040 flags,
1041 ccache,
1042 in_creds,
1043 impersonate_principal,
1044 second_ticket,
1045 out_creds,
1046 ret_tgts);
1047 if (ret == 0 || flags.b.canonicalize)
1048 return ret;
1049 return get_cred_kdc_capath(context,
1050 flags,
1051 ccache,
1052 in_creds,
1053 impersonate_principal,
1054 second_ticket,
1055 out_creds,
1056 ret_tgts);
1060 krb5_error_code KRB5_LIB_FUNCTION
1061 krb5_get_credentials_with_flags(krb5_context context,
1062 krb5_flags options,
1063 krb5_kdc_flags flags,
1064 krb5_ccache ccache,
1065 krb5_creds *in_creds,
1066 krb5_creds **out_creds)
1068 krb5_error_code ret;
1069 krb5_creds **tgts;
1070 krb5_creds *res_creds;
1071 int i;
1073 if (in_creds->session.keytype) {
1074 ret = krb5_enctype_valid(context, in_creds->session.keytype);
1075 if (ret)
1076 return ret;
1079 *out_creds = NULL;
1080 res_creds = calloc(1, sizeof(*res_creds));
1081 if (res_creds == NULL) {
1082 krb5_set_error_message(context, ENOMEM,
1083 N_("malloc: out of memory", ""));
1084 return ENOMEM;
1087 if (in_creds->session.keytype)
1088 options |= KRB5_TC_MATCH_KEYTYPE;
1091 * If we got a credential, check if credential is expired before
1092 * returning it.
1094 ret = krb5_cc_retrieve_cred(context,
1095 ccache,
1096 in_creds->session.keytype ?
1097 KRB5_TC_MATCH_KEYTYPE : 0,
1098 in_creds, res_creds);
1100 * If we got a credential, check if credential is expired before
1101 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1103 if (ret == 0) {
1104 krb5_timestamp timeret;
1106 /* If expired ok, don't bother checking */
1107 if(options & KRB5_GC_EXPIRED_OK) {
1108 *out_creds = res_creds;
1109 return 0;
1112 krb5_timeofday(context, &timeret);
1113 if(res_creds->times.endtime > timeret) {
1114 *out_creds = res_creds;
1115 return 0;
1117 if(options & KRB5_GC_CACHED)
1118 krb5_cc_remove_cred(context, ccache, 0, res_creds);
1120 } else if(ret != KRB5_CC_END) {
1121 free(res_creds);
1122 return ret;
1124 free(res_creds);
1125 if(options & KRB5_GC_CACHED)
1126 return not_found(context, in_creds->server, KRB5_CC_NOTFOUND);
1128 if(options & KRB5_GC_USER_USER)
1129 flags.b.enc_tkt_in_skey = 1;
1130 if (flags.b.enc_tkt_in_skey)
1131 options |= KRB5_GC_NO_STORE;
1133 tgts = NULL;
1134 ret = _krb5_get_cred_kdc_any(context, flags, ccache,
1135 in_creds, NULL, NULL, out_creds, &tgts);
1136 for(i = 0; tgts && tgts[i]; i++) {
1137 krb5_cc_store_cred(context, ccache, tgts[i]);
1138 krb5_free_creds(context, tgts[i]);
1140 free(tgts);
1141 if(ret == 0 && (options & KRB5_GC_NO_STORE) == 0)
1142 krb5_cc_store_cred(context, ccache, *out_creds);
1143 return ret;
1146 krb5_error_code KRB5_LIB_FUNCTION
1147 krb5_get_credentials(krb5_context context,
1148 krb5_flags options,
1149 krb5_ccache ccache,
1150 krb5_creds *in_creds,
1151 krb5_creds **out_creds)
1153 krb5_kdc_flags flags;
1154 flags.i = 0;
1155 return krb5_get_credentials_with_flags(context, options, flags,
1156 ccache, in_creds, out_creds);
1159 struct krb5_get_creds_opt_data {
1160 krb5_principal self;
1161 krb5_flags options;
1162 krb5_enctype enctype;
1163 Ticket *ticket;
1167 krb5_error_code KRB5_LIB_FUNCTION
1168 krb5_get_creds_opt_alloc(krb5_context context, krb5_get_creds_opt *opt)
1170 *opt = calloc(1, sizeof(**opt));
1171 if (*opt == NULL) {
1172 krb5_set_error_message(context, ENOMEM,
1173 N_("malloc: out of memory", ""));
1174 return ENOMEM;
1176 return 0;
1179 void KRB5_LIB_FUNCTION
1180 krb5_get_creds_opt_free(krb5_context context, krb5_get_creds_opt opt)
1182 if (opt->self)
1183 krb5_free_principal(context, opt->self);
1184 if (opt->ticket) {
1185 free_Ticket(opt->ticket);
1186 free(opt->ticket);
1188 memset(opt, 0, sizeof(*opt));
1189 free(opt);
1192 void KRB5_LIB_FUNCTION
1193 krb5_get_creds_opt_set_options(krb5_context context,
1194 krb5_get_creds_opt opt,
1195 krb5_flags options)
1197 opt->options = options;
1200 void KRB5_LIB_FUNCTION
1201 krb5_get_creds_opt_add_options(krb5_context context,
1202 krb5_get_creds_opt opt,
1203 krb5_flags options)
1205 opt->options |= options;
1208 void KRB5_LIB_FUNCTION
1209 krb5_get_creds_opt_set_enctype(krb5_context context,
1210 krb5_get_creds_opt opt,
1211 krb5_enctype enctype)
1213 opt->enctype = enctype;
1216 krb5_error_code KRB5_LIB_FUNCTION
1217 krb5_get_creds_opt_set_impersonate(krb5_context context,
1218 krb5_get_creds_opt opt,
1219 krb5_const_principal self)
1221 if (opt->self)
1222 krb5_free_principal(context, opt->self);
1223 return krb5_copy_principal(context, self, &opt->self);
1226 krb5_error_code KRB5_LIB_FUNCTION
1227 krb5_get_creds_opt_set_ticket(krb5_context context,
1228 krb5_get_creds_opt opt,
1229 const Ticket *ticket)
1231 if (opt->ticket) {
1232 free_Ticket(opt->ticket);
1233 free(opt->ticket);
1234 opt->ticket = NULL;
1236 if (ticket) {
1237 krb5_error_code ret;
1239 opt->ticket = malloc(sizeof(*ticket));
1240 if (opt->ticket == NULL) {
1241 krb5_set_error_message(context, ENOMEM,
1242 N_("malloc: out of memory", ""));
1243 return ENOMEM;
1245 ret = copy_Ticket(ticket, opt->ticket);
1246 if (ret) {
1247 free(opt->ticket);
1248 opt->ticket = NULL;
1249 krb5_set_error_message(context, ret,
1250 N_("malloc: out of memory", ""));
1251 return ret;
1254 return 0;
1259 krb5_error_code KRB5_LIB_FUNCTION
1260 krb5_get_creds(krb5_context context,
1261 krb5_get_creds_opt opt,
1262 krb5_ccache ccache,
1263 krb5_const_principal inprinc,
1264 krb5_creds **out_creds)
1266 krb5_kdc_flags flags;
1267 krb5_flags options;
1268 krb5_creds in_creds;
1269 krb5_error_code ret;
1270 krb5_creds **tgts;
1271 krb5_creds *res_creds;
1272 int i;
1274 if (opt && opt->enctype) {
1275 ret = krb5_enctype_valid(context, opt->enctype);
1276 if (ret)
1277 return ret;
1280 memset(&in_creds, 0, sizeof(in_creds));
1281 in_creds.server = rk_UNCONST(inprinc);
1283 ret = krb5_cc_get_principal(context, ccache, &in_creds.client);
1284 if (ret)
1285 return ret;
1287 if (opt)
1288 options = opt->options;
1289 else
1290 options = 0;
1291 flags.i = 0;
1293 *out_creds = NULL;
1294 res_creds = calloc(1, sizeof(*res_creds));
1295 if (res_creds == NULL) {
1296 krb5_free_principal(context, in_creds.client);
1297 krb5_set_error_message(context, ENOMEM,
1298 N_("malloc: out of memory", ""));
1299 return ENOMEM;
1302 if (opt && opt->enctype) {
1303 in_creds.session.keytype = opt->enctype;
1304 options |= KRB5_TC_MATCH_KEYTYPE;
1308 * If we got a credential, check if credential is expired before
1309 * returning it.
1311 ret = krb5_cc_retrieve_cred(context,
1312 ccache,
1313 options & KRB5_TC_MATCH_KEYTYPE,
1314 &in_creds, res_creds);
1316 * If we got a credential, check if credential is expired before
1317 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
1319 if (ret == 0) {
1320 krb5_timestamp timeret;
1322 /* If expired ok, don't bother checking */
1323 if(options & KRB5_GC_EXPIRED_OK) {
1324 *out_creds = res_creds;
1325 krb5_free_principal(context, in_creds.client);
1326 return 0;
1329 krb5_timeofday(context, &timeret);
1330 if(res_creds->times.endtime > timeret) {
1331 *out_creds = res_creds;
1332 krb5_free_principal(context, in_creds.client);
1333 return 0;
1335 if(options & KRB5_GC_CACHED)
1336 krb5_cc_remove_cred(context, ccache, 0, res_creds);
1338 } else if(ret != KRB5_CC_END) {
1339 free(res_creds);
1340 krb5_free_principal(context, in_creds.client);
1341 return ret;
1343 free(res_creds);
1344 if(options & KRB5_GC_CACHED) {
1345 krb5_free_principal(context, in_creds.client);
1346 return not_found(context, in_creds.server, KRB5_CC_NOTFOUND);
1348 if(options & KRB5_GC_USER_USER) {
1349 flags.b.enc_tkt_in_skey = 1;
1350 options |= KRB5_GC_NO_STORE;
1352 if (options & KRB5_GC_FORWARDABLE)
1353 flags.b.forwardable = 1;
1354 if (options & KRB5_GC_NO_TRANSIT_CHECK)
1355 flags.b.disable_transited_check = 1;
1356 if (options & KRB5_GC_CONSTRAINED_DELEGATION) {
1357 flags.b.request_anonymous = 1; /* XXX ARGH confusion */
1358 flags.b.constrained_delegation = 1;
1360 if (options & KRB5_GC_CANONICALIZE)
1361 flags.b.canonicalize = 1;
1363 tgts = NULL;
1364 ret = _krb5_get_cred_kdc_any(context, flags, ccache,
1365 &in_creds, opt->self, opt->ticket,
1366 out_creds, &tgts);
1367 krb5_free_principal(context, in_creds.client);
1368 for(i = 0; tgts && tgts[i]; i++) {
1369 krb5_cc_store_cred(context, ccache, tgts[i]);
1370 krb5_free_creds(context, tgts[i]);
1372 free(tgts);
1373 if(ret == 0 && (options & KRB5_GC_NO_STORE) == 0)
1374 krb5_cc_store_cred(context, ccache, *out_creds);
1375 return ret;
1382 krb5_error_code KRB5_LIB_FUNCTION
1383 krb5_get_renewed_creds(krb5_context context,
1384 krb5_creds *creds,
1385 krb5_const_principal client,
1386 krb5_ccache ccache,
1387 const char *in_tkt_service)
1389 krb5_error_code ret;
1390 krb5_kdc_flags flags;
1391 krb5_creds in, *template, *out = NULL;
1393 memset(&in, 0, sizeof(in));
1394 memset(creds, 0, sizeof(*creds));
1396 ret = krb5_copy_principal(context, client, &in.client);
1397 if (ret)
1398 return ret;
1400 if (in_tkt_service) {
1401 ret = krb5_parse_name(context, in_tkt_service, &in.server);
1402 if (ret) {
1403 krb5_free_principal(context, in.client);
1404 return ret;
1406 } else {
1407 const char *realm = krb5_principal_get_realm(context, client);
1409 ret = krb5_make_principal(context, &in.server, realm, KRB5_TGS_NAME,
1410 realm, NULL);
1411 if (ret) {
1412 krb5_free_principal(context, in.client);
1413 return ret;
1417 flags.i = 0;
1418 flags.b.renewable = flags.b.renew = 1;
1421 * Get template from old credential cache for the same entry, if
1422 * this failes, no worries.
1424 ret = krb5_get_credentials(context, KRB5_GC_CACHED, ccache, &in, &template);
1425 if (ret == 0) {
1426 flags.b.forwardable = template->flags.b.forwardable;
1427 flags.b.proxiable = template->flags.b.proxiable;
1428 krb5_free_creds (context, template);
1431 ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &in, &out);
1432 krb5_free_principal(context, in.client);
1433 krb5_free_principal(context, in.server);
1434 if (ret)
1435 return ret;
1437 ret = krb5_copy_creds_contents(context, out, creds);
1438 krb5_free_creds(context, out);
1440 return ret;