2 * Copyright (c) 1997 - 2004 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("$Id: get_for_creds.c,v 1.47 2006/02/03 11:37:29 lha Exp $");
38 static krb5_error_code
39 add_addrs(krb5_context context,
49 for (a = ai; a != NULL; a = a->ai_next)
52 tmp = realloc(addr->val, (addr->len + n) * sizeof(*addr->val));
54 krb5_set_error_string(context, "malloc: out of memory");
59 for (i = addr->len; i < (addr->len + n); ++i) {
60 addr->val[i].addr_type = 0;
61 krb5_data_zero(&addr->val[i].address);
64 for (a = ai; a != NULL; a = a->ai_next) {
67 ret = krb5_sockaddr2address (context, a->ai_addr, &ad);
69 if (krb5_address_search(context, &ad, addr))
70 krb5_free_address(context, &ad);
74 else if (ret == KRB5_PROG_ATYPE_NOSUPP)
75 krb5_clear_error_string (context);
82 krb5_free_addresses (context, addr);
87 * Forward credentials for `client' to host `hostname`,
88 * making them forwardable if `forwardable', and returning the
89 * blob of data to sent in `out_data'.
90 * If hostname == NULL, pick it from `server'
93 krb5_error_code KRB5_LIB_FUNCTION
94 krb5_fwd_tgt_creds (krb5_context context,
95 krb5_auth_context auth_context,
97 krb5_principal client,
98 krb5_principal server,
103 krb5_flags flags = 0;
106 krb5_const_realm client_realm;
108 flags |= KDC_OPT_FORWARDED;
111 flags |= KDC_OPT_FORWARDABLE;
113 if (hostname == NULL &&
114 krb5_principal_get_type(context, server) == KRB5_NT_SRV_HST) {
115 const char *inst = krb5_principal_get_comp_string(context, server, 0);
116 const char *host = krb5_principal_get_comp_string(context, server, 1);
119 strcmp(inst, "host") == 0 &&
121 krb5_principal_get_comp_string(context, server, 2) == NULL)
125 client_realm = krb5_principal_get_realm(context, client);
127 memset (&creds, 0, sizeof(creds));
128 creds.client = client;
130 ret = krb5_build_principal(context,
132 strlen(client_realm),
140 ret = krb5_get_forwarded_creds (context,
154 krb5_error_code KRB5_LIB_FUNCTION
155 krb5_get_forwarded_creds (krb5_context context,
156 krb5_auth_context auth_context,
159 const char *hostname,
160 krb5_creds *in_creds,
164 krb5_creds *out_creds;
165 krb5_addresses *paddrs = NULL;
166 krb5_addresses addrs;
168 KrbCredInfo *krb_cred_info;
169 EncKrbCredPart enc_krb_cred_part;
173 krb5_kdc_flags kdc_flags;
178 krb5_boolean noaddr_ever;
183 if (in_creds->client && in_creds->client->realm)
184 realm = in_creds->client->realm;
186 realm = in_creds->server->realm;
188 krb5_appdefault_boolean(context, NULL, realm, "no-addresses-ever",
195 * If tickets are address-less, forward address-less tickets.
198 ret = _krb5_get_krbtgt (context,
203 if (ticket->addresses.len == 0)
205 krb5_free_creds (context, ticket);
208 if (paddrs != NULL) {
210 ret = getaddrinfo (hostname, NULL, NULL, &ai);
213 krb5_set_error_string(context, "resolving %s: %s",
214 hostname, gai_strerror(ret));
215 return krb5_eai_to_heim_errno(ret, save_errno);
218 ret = add_addrs (context, &addrs, ai);
225 kdc_flags.b = int2KDCOptions(flags);
227 ret = krb5_get_kdc_cred (context,
234 krb5_free_addresses (context, &addrs);
239 memset (&cred, 0, sizeof(cred));
241 cred.msg_type = krb_cred;
242 ALLOC_SEQ(&cred.tickets, 1);
243 if (cred.tickets.val == NULL) {
245 krb5_set_error_string(context, "malloc: out of memory");
248 ret = decode_Ticket(out_creds->ticket.data,
249 out_creds->ticket.length,
250 cred.tickets.val, &len);
254 memset (&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part));
255 ALLOC_SEQ(&enc_krb_cred_part.ticket_info, 1);
256 if (enc_krb_cred_part.ticket_info.val == NULL) {
258 krb5_set_error_string(context, "malloc: out of memory");
262 if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) {
266 krb5_us_timeofday (context, &sec, &usec);
268 ALLOC(enc_krb_cred_part.timestamp, 1);
269 if (enc_krb_cred_part.timestamp == NULL) {
271 krb5_set_error_string(context, "malloc: out of memory");
274 *enc_krb_cred_part.timestamp = sec;
275 ALLOC(enc_krb_cred_part.usec, 1);
276 if (enc_krb_cred_part.usec == NULL) {
278 krb5_set_error_string(context, "malloc: out of memory");
281 *enc_krb_cred_part.usec = usec;
283 enc_krb_cred_part.timestamp = NULL;
284 enc_krb_cred_part.usec = NULL;
287 if (auth_context->local_address && auth_context->local_port && paddrs) {
289 ret = krb5_make_addrport (context,
290 &enc_krb_cred_part.s_address,
291 auth_context->local_address,
292 auth_context->local_port);
297 if (auth_context->remote_address) {
298 if (auth_context->remote_port) {
300 krb5_const_realm srealm;
302 srealm = krb5_principal_get_realm(context, out_creds->server);
303 /* Is this correct, and should we use the paddrs == NULL
304 trick here as well? Having an address-less ticket may
305 indicate that we don't know our own global address, but
306 it does not necessary mean that we don't know the
308 krb5_appdefault_boolean(context, NULL, srealm, "no-addresses",
311 ret = krb5_make_addrport (context,
312 &enc_krb_cred_part.r_address,
313 auth_context->remote_address,
314 auth_context->remote_port);
319 ALLOC(enc_krb_cred_part.r_address, 1);
320 if (enc_krb_cred_part.r_address == NULL) {
322 krb5_set_error_string(context, "malloc: out of memory");
326 ret = krb5_copy_address (context, auth_context->remote_address,
327 enc_krb_cred_part.r_address);
333 /* fill ticket_info.val[0] */
335 enc_krb_cred_part.ticket_info.len = 1;
337 krb_cred_info = enc_krb_cred_part.ticket_info.val;
339 copy_EncryptionKey (&out_creds->session, &krb_cred_info->key);
340 ALLOC(krb_cred_info->prealm, 1);
341 copy_Realm (&out_creds->client->realm, krb_cred_info->prealm);
342 ALLOC(krb_cred_info->pname, 1);
343 copy_PrincipalName(&out_creds->client->name, krb_cred_info->pname);
344 ALLOC(krb_cred_info->flags, 1);
345 *krb_cred_info->flags = out_creds->flags.b;
346 ALLOC(krb_cred_info->authtime, 1);
347 *krb_cred_info->authtime = out_creds->times.authtime;
348 ALLOC(krb_cred_info->starttime, 1);
349 *krb_cred_info->starttime = out_creds->times.starttime;
350 ALLOC(krb_cred_info->endtime, 1);
351 *krb_cred_info->endtime = out_creds->times.endtime;
352 ALLOC(krb_cred_info->renew_till, 1);
353 *krb_cred_info->renew_till = out_creds->times.renew_till;
354 ALLOC(krb_cred_info->srealm, 1);
355 copy_Realm (&out_creds->server->realm, krb_cred_info->srealm);
356 ALLOC(krb_cred_info->sname, 1);
357 copy_PrincipalName (&out_creds->server->name, krb_cred_info->sname);
358 ALLOC(krb_cred_info->caddr, 1);
359 copy_HostAddresses (&out_creds->addresses, krb_cred_info->caddr);
361 krb5_free_creds (context, out_creds);
363 /* encode EncKrbCredPart */
365 ASN1_MALLOC_ENCODE(EncKrbCredPart, buf, buf_size,
366 &enc_krb_cred_part, &len, ret);
367 free_EncKrbCredPart (&enc_krb_cred_part);
369 free_KRB_CRED(&cred);
373 krb5_abortx(context, "internal error in ASN.1 encoder");
375 if (auth_context->flags & KRB5_AUTH_CONTEXT_CLEAR_FORWARDED_CRED) {
376 cred.enc_part.etype = ENCTYPE_NULL;
377 cred.enc_part.kvno = NULL;
378 cred.enc_part.cipher.data = buf;
379 cred.enc_part.cipher.length = buf_size;
382 * Here older versions then 0.7.2 of Heimdal used the local or
383 * remote subkey. That is wrong, the session key should be
384 * used. Heimdal 0.7.2 and newer have code to try both in the
388 ret = krb5_crypto_init(context, auth_context->keyblock, 0, &crypto);
391 free_KRB_CRED(&cred);
394 ret = krb5_encrypt_EncryptedData (context,
402 krb5_crypto_destroy(context, crypto);
404 free_KRB_CRED(&cred);
409 ASN1_MALLOC_ENCODE(KRB_CRED, buf, buf_size, &cred, &len, ret);
410 free_KRB_CRED (&cred);
414 krb5_abortx(context, "internal error in ASN.1 encoder");
415 out_data->length = len;
416 out_data->data = buf;
419 free_EncKrbCredPart(&enc_krb_cred_part);
421 free_KRB_CRED(&cred);
423 krb5_free_creds (context, out_creds);