2 * Copyright (c) 1997 - 2002 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.34.4.1 2004/01/09 00:51:55 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'
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,
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 addrs, *paddrs;
167 KrbCredInfo *krb_cred_info;
168 EncKrbCredPart enc_krb_cred_part;
172 krb5_kdc_flags kdc_flags;
180 if (in_creds->client && in_creds->client->realm)
181 realm = in_creds->client->realm;
183 realm = in_creds->server->realm;
190 * If tickets are address-less, forward address-less tickets.
193 ret = _krb5_get_krbtgt (context,
198 if (ticket->addresses.len == 0)
200 krb5_free_creds (context, ticket);
203 if (paddrs != NULL) {
205 ret = getaddrinfo (hostname, NULL, NULL, &ai);
208 krb5_set_error_string(context, "resolving %s: %s",
209 hostname, gai_strerror(ret));
210 return krb5_eai_to_heim_errno(ret, save_errno);
213 ret = add_addrs (context, &addrs, ai);
221 ret = krb5_get_kdc_cred (context,
228 krb5_free_addresses (context, &addrs);
233 memset (&cred, 0, sizeof(cred));
235 cred.msg_type = krb_cred;
236 ALLOC_SEQ(&cred.tickets, 1);
237 if (cred.tickets.val == NULL) {
239 krb5_set_error_string(context, "malloc: out of memory");
242 ret = decode_Ticket(out_creds->ticket.data,
243 out_creds->ticket.length,
244 cred.tickets.val, &len);
248 memset (&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part));
249 ALLOC_SEQ(&enc_krb_cred_part.ticket_info, 1);
250 if (enc_krb_cred_part.ticket_info.val == NULL) {
252 krb5_set_error_string(context, "malloc: out of memory");
256 if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) {
259 krb5_us_timeofday (context, &sec, &usec);
261 ALLOC(enc_krb_cred_part.timestamp, 1);
262 if (enc_krb_cred_part.timestamp == NULL) {
264 krb5_set_error_string(context, "malloc: out of memory");
267 *enc_krb_cred_part.timestamp = sec;
268 ALLOC(enc_krb_cred_part.usec, 1);
269 if (enc_krb_cred_part.usec == NULL) {
271 krb5_set_error_string(context, "malloc: out of memory");
274 *enc_krb_cred_part.usec = usec;
276 enc_krb_cred_part.timestamp = NULL;
277 enc_krb_cred_part.usec = NULL;
280 if (auth_context->local_address && auth_context->local_port) {
282 krb5_const_realm realm;
284 realm = krb5_principal_get_realm(context, out_creds->server);
285 krb5_appdefault_boolean(context, NULL, realm, "no-addresses", FALSE,
288 ret = krb5_make_addrport (context,
289 &enc_krb_cred_part.s_address,
290 auth_context->local_address,
291 auth_context->local_port);
297 if (auth_context->remote_address) {
298 if (auth_context->remote_port) {
300 krb5_const_realm realm;
302 realm = krb5_principal_get_realm(context, out_creds->server);
303 krb5_appdefault_boolean(context, NULL, realm, "no-addresses",
306 ret = krb5_make_addrport (context,
307 &enc_krb_cred_part.r_address,
308 auth_context->remote_address,
309 auth_context->remote_port);
314 ALLOC(enc_krb_cred_part.r_address, 1);
315 if (enc_krb_cred_part.r_address == NULL) {
317 krb5_set_error_string(context, "malloc: out of memory");
321 ret = krb5_copy_address (context, auth_context->remote_address,
322 enc_krb_cred_part.r_address);
328 /* fill ticket_info.val[0] */
330 enc_krb_cred_part.ticket_info.len = 1;
332 krb_cred_info = enc_krb_cred_part.ticket_info.val;
334 copy_EncryptionKey (&out_creds->session, &krb_cred_info->key);
335 ALLOC(krb_cred_info->prealm, 1);
336 copy_Realm (&out_creds->client->realm, krb_cred_info->prealm);
337 ALLOC(krb_cred_info->pname, 1);
338 copy_PrincipalName(&out_creds->client->name, krb_cred_info->pname);
339 ALLOC(krb_cred_info->flags, 1);
340 *krb_cred_info->flags = out_creds->flags.b;
341 ALLOC(krb_cred_info->authtime, 1);
342 *krb_cred_info->authtime = out_creds->times.authtime;
343 ALLOC(krb_cred_info->starttime, 1);
344 *krb_cred_info->starttime = out_creds->times.starttime;
345 ALLOC(krb_cred_info->endtime, 1);
346 *krb_cred_info->endtime = out_creds->times.endtime;
347 ALLOC(krb_cred_info->renew_till, 1);
348 *krb_cred_info->renew_till = out_creds->times.renew_till;
349 ALLOC(krb_cred_info->srealm, 1);
350 copy_Realm (&out_creds->server->realm, krb_cred_info->srealm);
351 ALLOC(krb_cred_info->sname, 1);
352 copy_PrincipalName (&out_creds->server->name, krb_cred_info->sname);
353 ALLOC(krb_cred_info->caddr, 1);
354 copy_HostAddresses (&out_creds->addresses, krb_cred_info->caddr);
356 krb5_free_creds (context, out_creds);
358 /* encode EncKrbCredPart */
360 ASN1_MALLOC_ENCODE(EncKrbCredPart, buf, buf_size,
361 &enc_krb_cred_part, &len, ret);
362 free_EncKrbCredPart (&enc_krb_cred_part);
364 free_KRB_CRED(&cred);
368 krb5_abortx(context, "internal error in ASN.1 encoder");
370 if (auth_context->local_subkey)
371 key = auth_context->local_subkey;
372 else if (auth_context->remote_subkey)
373 key = auth_context->remote_subkey;
375 key = auth_context->keyblock;
377 ret = krb5_crypto_init(context, key, 0, &crypto);
380 free_KRB_CRED(&cred);
383 ret = krb5_encrypt_EncryptedData (context,
391 krb5_crypto_destroy(context, crypto);
393 free_KRB_CRED(&cred);
397 ASN1_MALLOC_ENCODE(KRB_CRED, buf, buf_size, &cred, &len, ret);
398 free_KRB_CRED (&cred);
402 krb5_abortx(context, "internal error in ASN.1 encoder");
403 out_data->length = len;
404 out_data->data = buf;
407 free_EncKrbCredPart(&enc_krb_cred_part);
409 free_KRB_CRED(&cred);
411 krb5_free_creds (context, out_creds);