r16100: Patch from Michael Wood <mwood@icts.uct.ac.za>: s/then/than/ for correct...
[kai/samba.git] / source / heimdal / lib / krb5 / get_for_creds.c
1 /*
2  * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
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. 
16  *
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. 
20  *
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. 
32  */
33
34 #include <krb5_locl.h>
35
36 RCSID("$Id: get_for_creds.c,v 1.49 2006/04/10 09:28:15 lha Exp $");
37
38 static krb5_error_code
39 add_addrs(krb5_context context,
40           krb5_addresses *addr,
41           struct addrinfo *ai)
42 {
43     krb5_error_code ret;
44     unsigned n, i;
45     void *tmp;
46     struct addrinfo *a;
47
48     n = 0;
49     for (a = ai; a != NULL; a = a->ai_next)
50         ++n;
51
52     tmp = realloc(addr->val, (addr->len + n) * sizeof(*addr->val));
53     if (tmp == NULL && (addr->len + n) != 0) {
54         krb5_set_error_string(context, "malloc: out of memory");
55         ret = ENOMEM;
56         goto fail;
57     }
58     addr->val = tmp;
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);
62     }
63     i = addr->len;
64     for (a = ai; a != NULL; a = a->ai_next) {
65         krb5_address ad;
66
67         ret = krb5_sockaddr2address (context, a->ai_addr, &ad);
68         if (ret == 0) {
69             if (krb5_address_search(context, &ad, addr))
70                 krb5_free_address(context, &ad);
71             else
72                 addr->val[i++] = ad;
73         }
74         else if (ret == KRB5_PROG_ATYPE_NOSUPP)
75             krb5_clear_error_string (context);
76         else
77             goto fail;
78         addr->len = i;
79     }
80     return 0;
81 fail:
82     krb5_free_addresses (context, addr);
83     return ret;
84 }
85
86 /*
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'
91  */
92
93 krb5_error_code KRB5_LIB_FUNCTION
94 krb5_fwd_tgt_creds (krb5_context        context,
95                     krb5_auth_context   auth_context,
96                     const char          *hostname,
97                     krb5_principal      client,
98                     krb5_principal      server,
99                     krb5_ccache         ccache,
100                     int                 forwardable,
101                     krb5_data           *out_data)
102 {
103     krb5_flags flags = 0;
104     krb5_creds creds;
105     krb5_error_code ret;
106     krb5_const_realm client_realm;
107
108     flags |= KDC_OPT_FORWARDED;
109
110     if (forwardable)
111         flags |= KDC_OPT_FORWARDABLE;
112
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);
117
118         if (inst != NULL &&
119             strcmp(inst, "host") == 0 &&
120             host != NULL && 
121             krb5_principal_get_comp_string(context, server, 2) == NULL)
122             hostname = host;
123     }
124
125     client_realm = krb5_principal_get_realm(context, client);
126     
127     memset (&creds, 0, sizeof(creds));
128     creds.client = client;
129
130     ret = krb5_build_principal(context,
131                                &creds.server,
132                                strlen(client_realm),
133                                client_realm,
134                                KRB5_TGS_NAME,
135                                client_realm,
136                                NULL);
137     if (ret)
138         return ret;
139
140     ret = krb5_get_forwarded_creds (context,
141                                     auth_context,
142                                     ccache,
143                                     flags,
144                                     hostname,
145                                     &creds,
146                                     out_data);
147     return ret;
148 }
149
150 /*
151  *
152  */
153
154 krb5_error_code KRB5_LIB_FUNCTION
155 krb5_get_forwarded_creds (krb5_context      context,
156                           krb5_auth_context auth_context,
157                           krb5_ccache       ccache,
158                           krb5_flags        flags,
159                           const char        *hostname,
160                           krb5_creds        *in_creds,
161                           krb5_data         *out_data)
162 {
163     krb5_error_code ret;
164     krb5_creds *out_creds;
165     krb5_addresses *paddrs = NULL;
166     krb5_addresses addrs;
167     KRB_CRED cred;
168     KrbCredInfo *krb_cred_info;
169     EncKrbCredPart enc_krb_cred_part;
170     size_t len;
171     unsigned char *buf;
172     size_t buf_size;
173     krb5_kdc_flags kdc_flags;
174     krb5_crypto crypto;
175     int save_errno;
176     krb5_creds *ticket;
177     char *realm;
178     krb5_boolean noaddr_ever;
179
180     addrs.len = 0;
181     addrs.val = NULL;
182
183     realm = in_creds->client->realm;
184
185     krb5_appdefault_boolean(context, NULL, realm, "no-addresses-ever", 
186                             TRUE, &noaddr_ever);
187     if (!noaddr_ever) {
188             struct addrinfo *ai;
189             paddrs = &addrs;
190             
191             /*
192              * If tickets are address-less, forward address-less tickets.
193              */
194             
195             ret = _krb5_get_krbtgt (context,
196                                     ccache,
197                                     realm,
198                                     &ticket);
199             if(ret == 0) {
200                     if (ticket->addresses.len == 0)
201                             paddrs = NULL;
202                     krb5_free_creds (context, ticket);
203             }
204             
205             if (paddrs != NULL) {
206                     
207                     ret = getaddrinfo (hostname, NULL, NULL, &ai);
208                     if (ret) {
209                             save_errno = errno;
210                             krb5_set_error_string(context, "resolving %s: %s",
211                                                   hostname, gai_strerror(ret));
212                             return krb5_eai_to_heim_errno(ret, save_errno);
213                     }
214                     
215                     ret = add_addrs (context, &addrs, ai);
216                     freeaddrinfo (ai);
217                     if (ret)
218                             return ret;
219             }
220     }
221
222     kdc_flags.b = int2KDCOptions(flags);
223
224     ret = krb5_get_kdc_cred (context,
225                              ccache,
226                              kdc_flags,
227                              paddrs,
228                              NULL,
229                              in_creds,
230                              &out_creds);
231     krb5_free_addresses (context, &addrs);
232     if (ret) {
233         return ret;
234     }
235
236     memset (&cred, 0, sizeof(cred));
237     cred.pvno = 5;
238     cred.msg_type = krb_cred;
239     ALLOC_SEQ(&cred.tickets, 1);
240     if (cred.tickets.val == NULL) {
241         ret = ENOMEM;
242         krb5_set_error_string(context, "malloc: out of memory");
243         goto out2;
244     }
245     ret = decode_Ticket(out_creds->ticket.data,
246                         out_creds->ticket.length,
247                         cred.tickets.val, &len);
248     if (ret)
249         goto out3;
250
251     memset (&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part));
252     ALLOC_SEQ(&enc_krb_cred_part.ticket_info, 1);
253     if (enc_krb_cred_part.ticket_info.val == NULL) {
254         ret = ENOMEM;
255         krb5_set_error_string(context, "malloc: out of memory");
256         goto out4;
257     }
258     
259     if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) {
260         krb5_timestamp sec;
261         int32_t usec;
262         
263         krb5_us_timeofday (context, &sec, &usec);
264         
265         ALLOC(enc_krb_cred_part.timestamp, 1);
266         if (enc_krb_cred_part.timestamp == NULL) {
267             ret = ENOMEM;
268             krb5_set_error_string(context, "malloc: out of memory");
269             goto out4;
270         }
271         *enc_krb_cred_part.timestamp = sec;
272         ALLOC(enc_krb_cred_part.usec, 1);
273         if (enc_krb_cred_part.usec == NULL) {
274             ret = ENOMEM;
275             krb5_set_error_string(context, "malloc: out of memory");
276             goto out4;
277         }
278         *enc_krb_cred_part.usec      = usec;
279     } else {
280         enc_krb_cred_part.timestamp = NULL;
281         enc_krb_cred_part.usec = NULL;
282     }
283
284     if (auth_context->local_address && auth_context->local_port && paddrs) {
285
286         ret = krb5_make_addrport (context,
287                                   &enc_krb_cred_part.s_address,
288                                   auth_context->local_address,
289                                   auth_context->local_port);
290         if (ret)
291             goto out4;
292     }
293
294     if (auth_context->remote_address) {
295         if (auth_context->remote_port) {
296             krb5_boolean noaddr;
297             krb5_const_realm srealm;
298
299             srealm = krb5_principal_get_realm(context, out_creds->server);
300             /* Is this correct, and should we use the paddrs == NULL
301                trick here as well? Having an address-less ticket may
302                indicate that we don't know our own global address, but
303                it does not necessary mean that we don't know the
304                server's. */
305             krb5_appdefault_boolean(context, NULL, srealm, "no-addresses",
306                                     FALSE, &noaddr);
307             if (!noaddr) {
308                 ret = krb5_make_addrport (context,
309                                           &enc_krb_cred_part.r_address,
310                                           auth_context->remote_address,
311                                           auth_context->remote_port);
312                 if (ret)
313                     goto out4;
314             }
315         } else {
316             ALLOC(enc_krb_cred_part.r_address, 1);
317             if (enc_krb_cred_part.r_address == NULL) {
318                 ret = ENOMEM;
319                 krb5_set_error_string(context, "malloc: out of memory");
320                 goto out4;
321             }
322
323             ret = krb5_copy_address (context, auth_context->remote_address,
324                                      enc_krb_cred_part.r_address);
325             if (ret)
326                 goto out4;
327         }
328     }
329
330     /* fill ticket_info.val[0] */
331
332     enc_krb_cred_part.ticket_info.len = 1;
333
334     krb_cred_info = enc_krb_cred_part.ticket_info.val;
335
336     copy_EncryptionKey (&out_creds->session, &krb_cred_info->key);
337     ALLOC(krb_cred_info->prealm, 1);
338     copy_Realm (&out_creds->client->realm, krb_cred_info->prealm);
339     ALLOC(krb_cred_info->pname, 1);
340     copy_PrincipalName(&out_creds->client->name, krb_cred_info->pname);
341     ALLOC(krb_cred_info->flags, 1);
342     *krb_cred_info->flags          = out_creds->flags.b;
343     ALLOC(krb_cred_info->authtime, 1);
344     *krb_cred_info->authtime       = out_creds->times.authtime;
345     ALLOC(krb_cred_info->starttime, 1);
346     *krb_cred_info->starttime      = out_creds->times.starttime;
347     ALLOC(krb_cred_info->endtime, 1);
348     *krb_cred_info->endtime        = out_creds->times.endtime;
349     ALLOC(krb_cred_info->renew_till, 1);
350     *krb_cred_info->renew_till = out_creds->times.renew_till;
351     ALLOC(krb_cred_info->srealm, 1);
352     copy_Realm (&out_creds->server->realm, krb_cred_info->srealm);
353     ALLOC(krb_cred_info->sname, 1);
354     copy_PrincipalName (&out_creds->server->name, krb_cred_info->sname);
355     ALLOC(krb_cred_info->caddr, 1);
356     copy_HostAddresses (&out_creds->addresses, krb_cred_info->caddr);
357
358     krb5_free_creds (context, out_creds);
359
360     /* encode EncKrbCredPart */
361
362     ASN1_MALLOC_ENCODE(EncKrbCredPart, buf, buf_size, 
363                        &enc_krb_cred_part, &len, ret);
364     free_EncKrbCredPart (&enc_krb_cred_part);
365     if (ret) {
366         free_KRB_CRED(&cred);
367         return ret;
368     }
369     if(buf_size != len)
370         krb5_abortx(context, "internal error in ASN.1 encoder");
371
372     if (auth_context->flags & KRB5_AUTH_CONTEXT_CLEAR_FORWARDED_CRED) {
373         cred.enc_part.etype = ENCTYPE_NULL;
374         cred.enc_part.kvno = NULL;
375         cred.enc_part.cipher.data = buf;
376         cred.enc_part.cipher.length = buf_size;
377     } else {
378         /* 
379          * Here older versions than 0.7.2 of Heimdal used the local or
380          * remote subkey. That is wrong, the session key should be
381          * used. Heimdal 0.7.2 and newer have code to try both in the
382          * receiving end.
383          */
384         
385         ret = krb5_crypto_init(context, auth_context->keyblock, 0, &crypto);
386         if (ret) {
387             free(buf);
388             free_KRB_CRED(&cred);
389             return ret;
390         }
391         ret = krb5_encrypt_EncryptedData (context,
392                                           crypto,
393                                           KRB5_KU_KRB_CRED,
394                                           buf,
395                                           len,
396                                           0,
397                                           &cred.enc_part);
398         free(buf);
399         krb5_crypto_destroy(context, crypto);
400         if (ret) {
401             free_KRB_CRED(&cred);
402             return ret;
403         }
404     }
405
406     ASN1_MALLOC_ENCODE(KRB_CRED, buf, buf_size, &cred, &len, ret);
407     free_KRB_CRED (&cred);
408     if (ret)
409         return ret;
410     if(buf_size != len)
411         krb5_abortx(context, "internal error in ASN.1 encoder");
412     out_data->length = len;
413     out_data->data   = buf;
414     return 0;
415  out4:
416     free_EncKrbCredPart(&enc_krb_cred_part);
417  out3:
418     free_KRB_CRED(&cred);
419  out2:
420     krb5_free_creds (context, out_creds);
421     return ret;
422 }