get_for_creds.c revision 90926
1130803Smarcel/*
2130803Smarcel * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan
3130803Smarcel * (Royal Institute of Technology, Stockholm, Sweden).
4130803Smarcel * All rights reserved.
5130803Smarcel *
6130803Smarcel * Redistribution and use in source and binary forms, with or without
7130803Smarcel * modification, are permitted provided that the following conditions
8130803Smarcel * are met:
9130803Smarcel *
10130803Smarcel * 1. Redistributions of source code must retain the above copyright
11130803Smarcel *    notice, this list of conditions and the following disclaimer.
12130803Smarcel *
13130803Smarcel * 2. Redistributions in binary form must reproduce the above copyright
14130803Smarcel *    notice, this list of conditions and the following disclaimer in the
15130803Smarcel *    documentation and/or other materials provided with the distribution.
16130803Smarcel *
17130803Smarcel * 3. Neither the name of the Institute nor the names of its contributors
18130803Smarcel *    may be used to endorse or promote products derived from this software
19130803Smarcel *    without specific prior written permission.
20130803Smarcel *
21130803Smarcel * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22130803Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23130803Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24130803Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25130803Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26130803Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27130803Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28130803Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29130803Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30130803Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31130803Smarcel * SUCH DAMAGE.
32130803Smarcel */
33130803Smarcel
34130803Smarcel#include <krb5_locl.h>
35130803Smarcel
36130803SmarcelRCSID("$Id: get_for_creds.c,v 1.31 2001/07/19 17:33:22 assar Exp $");
37130803Smarcel
38130803Smarcelstatic krb5_error_code
39130803Smarceladd_addrs(krb5_context context,
40130803Smarcel	  krb5_addresses *addr,
41130803Smarcel	  struct addrinfo *ai)
42130803Smarcel{
43130803Smarcel    krb5_error_code ret;
44130803Smarcel    unsigned n, i, j;
45130803Smarcel    void *tmp;
46130803Smarcel    struct addrinfo *a;
47130803Smarcel
48130803Smarcel    n = 0;
49130803Smarcel    for (a = ai; a != NULL; a = a->ai_next)
50130803Smarcel	++n;
51130803Smarcel
52130803Smarcel    i = addr->len;
53130803Smarcel    addr->len += n;
54130803Smarcel    tmp = realloc(addr->val, addr->len * sizeof(*addr->val));
55130803Smarcel    if (tmp == NULL) {
56130803Smarcel	krb5_set_error_string(context, "malloc: out of memory");
57130803Smarcel	ret = ENOMEM;
58130803Smarcel	goto fail;
59130803Smarcel    }
60130803Smarcel    addr->val = tmp;
61130803Smarcel    for (j = i; j < addr->len; ++j) {
62130803Smarcel	addr->val[i].addr_type = 0;
63130803Smarcel	krb5_data_zero(&addr->val[i].address);
64130803Smarcel    }
65130803Smarcel    for (a = ai; a != NULL; a = a->ai_next) {
66130803Smarcel	ret = krb5_sockaddr2address (context, a->ai_addr, &addr->val[i]);
67130803Smarcel	if (ret == 0)
68130803Smarcel	    ++i;
69130803Smarcel	else if (ret == KRB5_PROG_ATYPE_NOSUPP)
70130803Smarcel	    krb5_clear_error_string (context);
71130803Smarcel	else
72130803Smarcel	    goto fail;
73130803Smarcel    }
74130803Smarcel    addr->len = i;
75130803Smarcel    return 0;
76130803Smarcelfail:
77130803Smarcel    krb5_free_addresses (context, addr);
78130803Smarcel    return ret;
79130803Smarcel}
80130803Smarcel
81130803Smarcel/*
82130803Smarcel * Forward credentials for `client' to host `hostname`,
83130803Smarcel * making them forwardable if `forwardable', and returning the
84130803Smarcel * blob of data to sent in `out_data'.
85130803Smarcel * If hostname == NULL, pick it from `server'
86130803Smarcel */
87130803Smarcel
88130803Smarcelkrb5_error_code
89130803Smarcelkrb5_fwd_tgt_creds (krb5_context	context,
90130803Smarcel		    krb5_auth_context	auth_context,
91130803Smarcel		    const char		*hostname,
92130803Smarcel		    krb5_principal	client,
93130803Smarcel		    krb5_principal	server,
94		    krb5_ccache		ccache,
95		    int			forwardable,
96		    krb5_data		*out_data)
97{
98    krb5_flags flags = 0;
99    krb5_creds creds;
100    krb5_error_code ret;
101    krb5_const_realm client_realm;
102
103    flags |= KDC_OPT_FORWARDED;
104
105    if (forwardable)
106	flags |= KDC_OPT_FORWARDABLE;
107
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);
112
113	if (inst != NULL &&
114	    strcmp(inst, "host") == 0 &&
115	    host != NULL &&
116	    krb5_principal_get_comp_string(context, server, 2) == NULL)
117	    hostname = host;
118    }
119
120    client_realm = krb5_principal_get_realm(context, client);
121
122    memset (&creds, 0, sizeof(creds));
123    creds.client = client;
124
125    ret = krb5_build_principal(context,
126			       &creds.server,
127			       strlen(client_realm),
128			       client_realm,
129			       KRB5_TGS_NAME,
130			       client_realm,
131			       NULL);
132    if (ret)
133	return ret;
134
135    ret = krb5_get_forwarded_creds (context,
136				    auth_context,
137				    ccache,
138				    flags,
139				    hostname,
140				    &creds,
141				    out_data);
142    return ret;
143}
144
145/*
146 *
147 */
148
149krb5_error_code
150krb5_get_forwarded_creds (krb5_context	    context,
151			  krb5_auth_context auth_context,
152			  krb5_ccache       ccache,
153			  krb5_flags        flags,
154			  const char        *hostname,
155			  krb5_creds        *in_creds,
156			  krb5_data         *out_data)
157{
158    krb5_error_code ret;
159    krb5_creds *out_creds;
160    krb5_addresses addrs;
161    KRB_CRED cred;
162    KrbCredInfo *krb_cred_info;
163    EncKrbCredPart enc_krb_cred_part;
164    size_t len;
165    u_char buf[1024];
166    int32_t sec, usec;
167    krb5_kdc_flags kdc_flags;
168    krb5_crypto crypto;
169    struct addrinfo *ai;
170    int save_errno;
171
172    addrs.len = 0;
173    addrs.val = NULL;
174
175    ret = getaddrinfo (hostname, NULL, NULL, &ai);
176    if (ret) {
177	save_errno = errno;
178	krb5_set_error_string(context, "resolving %s: %s",
179			      hostname, gai_strerror(ret));
180	return krb5_eai_to_heim_errno(ret, save_errno);
181    }
182
183    ret = add_addrs (context, &addrs, ai);
184    freeaddrinfo (ai);
185    if (ret)
186	return ret;
187
188    kdc_flags.i = flags;
189
190    ret = krb5_get_kdc_cred (context,
191			     ccache,
192			     kdc_flags,
193			     &addrs,
194			     NULL,
195			     in_creds,
196			     &out_creds);
197    krb5_free_addresses (context, &addrs);
198    if (ret) {
199	return ret;
200    }
201
202    memset (&cred, 0, sizeof(cred));
203    cred.pvno = 5;
204    cred.msg_type = krb_cred;
205    ALLOC_SEQ(&cred.tickets, 1);
206    if (cred.tickets.val == NULL) {
207	ret = ENOMEM;
208	krb5_set_error_string(context, "malloc: out of memory");
209	goto out2;
210    }
211    ret = decode_Ticket(out_creds->ticket.data,
212			out_creds->ticket.length,
213			cred.tickets.val, &len);
214    if (ret)
215	goto out3;
216
217    memset (&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part));
218    ALLOC_SEQ(&enc_krb_cred_part.ticket_info, 1);
219    if (enc_krb_cred_part.ticket_info.val == NULL) {
220	ret = ENOMEM;
221	krb5_set_error_string(context, "malloc: out of memory");
222	goto out4;
223    }
224
225    krb5_us_timeofday (context, &sec, &usec);
226
227    ALLOC(enc_krb_cred_part.timestamp, 1);
228    if (enc_krb_cred_part.timestamp == NULL) {
229	ret = ENOMEM;
230	krb5_set_error_string(context, "malloc: out of memory");
231	goto out4;
232    }
233    *enc_krb_cred_part.timestamp = sec;
234    ALLOC(enc_krb_cred_part.usec, 1);
235    if (enc_krb_cred_part.usec == NULL) {
236 	ret = ENOMEM;
237	krb5_set_error_string(context, "malloc: out of memory");
238	goto out4;
239    }
240    *enc_krb_cred_part.usec      = usec;
241
242    if (auth_context->local_address && auth_context->local_port) {
243	krb5_boolean noaddr;
244	const krb5_realm *realm;
245
246	realm = krb5_princ_realm(context, out_creds->server);
247	krb5_appdefault_boolean(context, NULL, *realm, "no-addresses", FALSE,
248				&noaddr);
249	if (!noaddr) {
250	    ret = krb5_make_addrport (context,
251				      &enc_krb_cred_part.s_address,
252				      auth_context->local_address,
253				      auth_context->local_port);
254	    if (ret)
255		goto out4;
256	}
257    }
258
259    if (auth_context->remote_address) {
260	ALLOC(enc_krb_cred_part.r_address, 1);
261	if (enc_krb_cred_part.r_address == NULL) {
262	    ret = ENOMEM;
263	krb5_set_error_string(context, "malloc: out of memory");
264	    goto out4;
265	}
266
267	ret = krb5_copy_address (context, auth_context->remote_address,
268				 enc_krb_cred_part.r_address);
269	if (ret)
270	    goto out4;
271    }
272
273    /* fill ticket_info.val[0] */
274
275    enc_krb_cred_part.ticket_info.len = 1;
276
277    krb_cred_info = enc_krb_cred_part.ticket_info.val;
278
279    copy_EncryptionKey (&out_creds->session, &krb_cred_info->key);
280    ALLOC(krb_cred_info->prealm, 1);
281    copy_Realm (&out_creds->client->realm, krb_cred_info->prealm);
282    ALLOC(krb_cred_info->pname, 1);
283    copy_PrincipalName(&out_creds->client->name, krb_cred_info->pname);
284    ALLOC(krb_cred_info->flags, 1);
285    *krb_cred_info->flags          = out_creds->flags.b;
286    ALLOC(krb_cred_info->authtime, 1);
287    *krb_cred_info->authtime       = out_creds->times.authtime;
288    ALLOC(krb_cred_info->starttime, 1);
289    *krb_cred_info->starttime      = out_creds->times.starttime;
290    ALLOC(krb_cred_info->endtime, 1);
291    *krb_cred_info->endtime        = out_creds->times.endtime;
292    ALLOC(krb_cred_info->renew_till, 1);
293    *krb_cred_info->renew_till = out_creds->times.renew_till;
294    ALLOC(krb_cred_info->srealm, 1);
295    copy_Realm (&out_creds->server->realm, krb_cred_info->srealm);
296    ALLOC(krb_cred_info->sname, 1);
297    copy_PrincipalName (&out_creds->server->name, krb_cred_info->sname);
298    ALLOC(krb_cred_info->caddr, 1);
299    copy_HostAddresses (&out_creds->addresses, krb_cred_info->caddr);
300
301    krb5_free_creds (context, out_creds);
302
303    /* encode EncKrbCredPart */
304
305    ret = krb5_encode_EncKrbCredPart (context,
306				      buf + sizeof(buf) - 1, sizeof(buf),
307				      &enc_krb_cred_part, &len);
308    free_EncKrbCredPart (&enc_krb_cred_part);
309    if (ret) {
310	free_KRB_CRED(&cred);
311	return ret;
312    }
313
314    ret = krb5_crypto_init(context, auth_context->local_subkey, 0, &crypto);
315    if (ret) {
316	free_KRB_CRED(&cred);
317	return ret;
318    }
319    ret = krb5_encrypt_EncryptedData (context,
320				      crypto,
321				      KRB5_KU_KRB_CRED,
322				      buf + sizeof(buf) - len,
323				      len,
324				      0,
325				      &cred.enc_part);
326    krb5_crypto_destroy(context, crypto);
327    if (ret) {
328	free_KRB_CRED(&cred);
329	return ret;
330    }
331
332    ret = encode_KRB_CRED (buf + sizeof(buf) - 1, sizeof(buf),
333			   &cred, &len);
334    free_KRB_CRED (&cred);
335    if (ret)
336	return ret;
337    out_data->length = len;
338    out_data->data   = malloc(len);
339    if (out_data->data == NULL) {
340	krb5_set_error_string(context, "malloc: out of memory");
341	return ENOMEM;
342    }
343    memcpy (out_data->data, buf + sizeof(buf) - len, len);
344    return 0;
345out4:
346    free_EncKrbCredPart(&enc_krb_cred_part);
347out3:
348    free_KRB_CRED(&cred);
349out2:
350    krb5_free_creds (context, out_creds);
351    return ret;
352}
353