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 2002/09/04 16:26:04 joda Exp $");
38 static krb5_error_code
39 add_addrs(krb5_context context,
49 for (a = ai; a != NULL; a = a->ai_next)
54 tmp = realloc(addr->val, addr->len * sizeof(*addr->val));
56 krb5_set_error_string(context, "malloc: out of memory");
61 for (j = i; j < addr->len; ++j) {
62 addr->val[i].addr_type = 0;
63 krb5_data_zero(&addr->val[i].address);
65 for (a = ai; a != NULL; a = a->ai_next) {
66 ret = krb5_sockaddr2address (context, a->ai_addr, &addr->val[i]);
69 else if (ret == KRB5_PROG_ATYPE_NOSUPP)
70 krb5_clear_error_string (context);
77 krb5_free_addresses (context, addr);
82 * Forward credentials for `client' to host `hostname`,
83 * making them forwardable if `forwardable', and returning the
84 * blob of data to sent in `out_data'.
85 * If hostname == NULL, pick it from `server'
89 krb5_fwd_tgt_creds (krb5_context context,
90 krb5_auth_context auth_context,
92 krb5_principal client,
93 krb5_principal server,
101 krb5_const_realm client_realm;
103 flags |= KDC_OPT_FORWARDED;
106 flags |= KDC_OPT_FORWARDABLE;
108 if (hostname == NULL &&
109 krb5_principal_get_type(context, server) == KRB5_NT_SRV_HST) {
110 const char *inst = krb5_principal_get_comp_string(context, server, 0);
111 const char *host = krb5_principal_get_comp_string(context, server, 1);
114 strcmp(inst, "host") == 0 &&
116 krb5_principal_get_comp_string(context, server, 2) == NULL)
120 client_realm = krb5_principal_get_realm(context, client);
122 memset (&creds, 0, sizeof(creds));
123 creds.client = client;
125 ret = krb5_build_principal(context,
127 strlen(client_realm),
135 ret = krb5_get_forwarded_creds (context,
150 krb5_get_forwarded_creds (krb5_context context,
151 krb5_auth_context auth_context,
154 const char *hostname,
155 krb5_creds *in_creds,
159 krb5_creds *out_creds;
160 krb5_addresses addrs;
162 KrbCredInfo *krb_cred_info;
163 EncKrbCredPart enc_krb_cred_part;
168 krb5_kdc_flags kdc_flags;
177 ret = getaddrinfo (hostname, NULL, NULL, &ai);
180 krb5_set_error_string(context, "resolving %s: %s",
181 hostname, gai_strerror(ret));
182 return krb5_eai_to_heim_errno(ret, save_errno);
185 ret = add_addrs (context, &addrs, ai);
192 ret = krb5_get_kdc_cred (context,
199 krb5_free_addresses (context, &addrs);
204 memset (&cred, 0, sizeof(cred));
206 cred.msg_type = krb_cred;
207 ALLOC_SEQ(&cred.tickets, 1);
208 if (cred.tickets.val == NULL) {
210 krb5_set_error_string(context, "malloc: out of memory");
213 ret = decode_Ticket(out_creds->ticket.data,
214 out_creds->ticket.length,
215 cred.tickets.val, &len);
219 memset (&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part));
220 ALLOC_SEQ(&enc_krb_cred_part.ticket_info, 1);
221 if (enc_krb_cred_part.ticket_info.val == NULL) {
223 krb5_set_error_string(context, "malloc: out of memory");
227 krb5_us_timeofday (context, &sec, &usec);
229 ALLOC(enc_krb_cred_part.timestamp, 1);
230 if (enc_krb_cred_part.timestamp == NULL) {
232 krb5_set_error_string(context, "malloc: out of memory");
235 *enc_krb_cred_part.timestamp = sec;
236 ALLOC(enc_krb_cred_part.usec, 1);
237 if (enc_krb_cred_part.usec == NULL) {
239 krb5_set_error_string(context, "malloc: out of memory");
242 *enc_krb_cred_part.usec = usec;
244 if (auth_context->local_address && auth_context->local_port) {
246 const krb5_realm *realm;
248 realm = krb5_princ_realm(context, out_creds->server);
249 krb5_appdefault_boolean(context, NULL, *realm, "no-addresses", FALSE,
252 ret = krb5_make_addrport (context,
253 &enc_krb_cred_part.s_address,
254 auth_context->local_address,
255 auth_context->local_port);
261 if (auth_context->remote_address) {
262 if (auth_context->remote_port) {
264 const krb5_realm *realm;
266 realm = krb5_princ_realm(context, out_creds->server);
267 krb5_appdefault_boolean(context, NULL, *realm, "no-addresses",
270 ret = krb5_make_addrport (context,
271 &enc_krb_cred_part.r_address,
272 auth_context->remote_address,
273 auth_context->remote_port);
278 ALLOC(enc_krb_cred_part.r_address, 1);
279 if (enc_krb_cred_part.r_address == NULL) {
281 krb5_set_error_string(context, "malloc: out of memory");
285 ret = krb5_copy_address (context, auth_context->remote_address,
286 enc_krb_cred_part.r_address);
292 /* fill ticket_info.val[0] */
294 enc_krb_cred_part.ticket_info.len = 1;
296 krb_cred_info = enc_krb_cred_part.ticket_info.val;
298 copy_EncryptionKey (&out_creds->session, &krb_cred_info->key);
299 ALLOC(krb_cred_info->prealm, 1);
300 copy_Realm (&out_creds->client->realm, krb_cred_info->prealm);
301 ALLOC(krb_cred_info->pname, 1);
302 copy_PrincipalName(&out_creds->client->name, krb_cred_info->pname);
303 ALLOC(krb_cred_info->flags, 1);
304 *krb_cred_info->flags = out_creds->flags.b;
305 ALLOC(krb_cred_info->authtime, 1);
306 *krb_cred_info->authtime = out_creds->times.authtime;
307 ALLOC(krb_cred_info->starttime, 1);
308 *krb_cred_info->starttime = out_creds->times.starttime;
309 ALLOC(krb_cred_info->endtime, 1);
310 *krb_cred_info->endtime = out_creds->times.endtime;
311 ALLOC(krb_cred_info->renew_till, 1);
312 *krb_cred_info->renew_till = out_creds->times.renew_till;
313 ALLOC(krb_cred_info->srealm, 1);
314 copy_Realm (&out_creds->server->realm, krb_cred_info->srealm);
315 ALLOC(krb_cred_info->sname, 1);
316 copy_PrincipalName (&out_creds->server->name, krb_cred_info->sname);
317 ALLOC(krb_cred_info->caddr, 1);
318 copy_HostAddresses (&out_creds->addresses, krb_cred_info->caddr);
320 krb5_free_creds (context, out_creds);
322 /* encode EncKrbCredPart */
324 ASN1_MALLOC_ENCODE(EncKrbCredPart, buf, buf_size,
325 &enc_krb_cred_part, &len, ret);
326 free_EncKrbCredPart (&enc_krb_cred_part);
328 free_KRB_CRED(&cred);
332 krb5_abortx(context, "internal error in ASN.1 encoder");
334 if (auth_context->local_subkey)
335 key = auth_context->local_subkey;
336 else if (auth_context->remote_subkey)
337 key = auth_context->remote_subkey;
339 key = auth_context->keyblock;
341 ret = krb5_crypto_init(context, key, 0, &crypto);
344 free_KRB_CRED(&cred);
347 ret = krb5_encrypt_EncryptedData (context,
355 krb5_crypto_destroy(context, crypto);
357 free_KRB_CRED(&cred);
361 ASN1_MALLOC_ENCODE(KRB_CRED, buf, buf_size, &cred, &len, ret);
362 free_KRB_CRED (&cred);
366 krb5_abortx(context, "internal error in ASN.1 encoder");
367 out_data->length = len;
368 out_data->data = buf;
371 free_EncKrbCredPart(&enc_krb_cred_part);
373 free_KRB_CRED(&cred);
375 krb5_free_creds (context, out_creds);