r8302: import mini HEIMDAL into the tree
[metze/samba/wip.git] / source / heimdal / kdc / 524.c
1 /*
2  * Copyright (c) 1997-2005 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 "kdc_locl.h"
35
36 RCSID("$Id: 524.c,v 1.34 2005/06/30 01:47:35 lha Exp $");
37
38 #include <krb5-v4compat.h>
39
40 /*
41  * fetch the server from `t', returning the name in malloced memory in
42  * `spn' and the entry itself in `server'
43  */
44
45 static krb5_error_code
46 fetch_server (krb5_context context, 
47               krb5_kdc_configuration *config,
48               const Ticket *t,
49               char **spn,
50               hdb_entry **server,
51               const char *from)
52 {
53     krb5_error_code ret;
54     krb5_principal sprinc;
55
56     ret = _krb5_principalname2krb5_principal(&sprinc, t->sname, t->realm);
57     if (ret) {
58         kdc_log(context, config, 0, "_krb5_principalname2krb5_principal: %s",
59                 krb5_get_err_text(context, ret));
60         return ret;
61     }
62     ret = krb5_unparse_name(context, sprinc, spn);
63     if (ret) {
64         krb5_free_principal(context, sprinc);
65         kdc_log(context, config, 0, "krb5_unparse_name: %s",
66                 krb5_get_err_text(context, ret));
67         return ret;
68     }
69     ret = _kdc_db_fetch(context, config, sprinc, HDB_ENT_TYPE_SERVER, server);
70     krb5_free_principal(context, sprinc);
71     if (ret) {
72         kdc_log(context, config, 0,
73         "Request to convert ticket from %s for unknown principal %s: %s",
74                 from, *spn, krb5_get_err_text(context, ret));
75         if (ret == HDB_ERR_NOENTRY)
76             ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
77         return ret;
78     }
79     return 0;
80 }
81
82 static krb5_error_code
83 log_524 (krb5_context context, 
84          krb5_kdc_configuration *config,
85          const EncTicketPart *et,
86          const char *from,
87          const char *spn)
88 {
89     krb5_principal client;
90     char *cpn;
91     krb5_error_code ret;
92
93     ret = _krb5_principalname2krb5_principal(&client, et->cname, et->crealm);
94     if (ret) {
95         kdc_log(context, config, 0, "_krb5_principalname2krb5_principal: %s",
96                 krb5_get_err_text (context, ret));
97         return ret;
98     }
99     ret = krb5_unparse_name(context, client, &cpn);
100     if (ret) {
101         krb5_free_principal(context, client);
102         kdc_log(context, config, 0, "krb5_unparse_name: %s",
103                 krb5_get_err_text (context, ret));
104         return ret;
105     }
106     kdc_log(context, config, 1, "524-REQ %s from %s for %s", cpn, from, spn);
107     free(cpn);
108     krb5_free_principal(context, client);
109     return 0;
110 }
111
112 static krb5_error_code
113 verify_flags (krb5_context context, 
114               krb5_kdc_configuration *config,
115               const EncTicketPart *et,
116               const char *spn)
117 {
118     if(et->endtime < kdc_time){
119         kdc_log(context, config, 0, "Ticket expired (%s)", spn);
120         return KRB5KRB_AP_ERR_TKT_EXPIRED;
121     }
122     if(et->flags.invalid){
123         kdc_log(context, config, 0, "Ticket not valid (%s)", spn);
124         return KRB5KRB_AP_ERR_TKT_NYV;
125     }
126     return 0;
127 }
128
129 /*
130  * set the `et->caddr' to the most appropriate address to use, where
131  * `addr' is the address the request was received from.
132  */
133
134 static krb5_error_code
135 set_address (krb5_context context, 
136              krb5_kdc_configuration *config,
137              EncTicketPart *et,
138              struct sockaddr *addr,
139              const char *from)
140 {
141     krb5_error_code ret;
142     krb5_address *v4_addr;
143
144     v4_addr = malloc (sizeof(*v4_addr));
145     if (v4_addr == NULL)
146         return ENOMEM;
147
148     ret = krb5_sockaddr2address(context, addr, v4_addr);
149     if(ret) {
150         free (v4_addr);
151         kdc_log(context, config, 0, "Failed to convert address (%s)", from);
152         return ret;
153     }
154             
155     if (et->caddr && !krb5_address_search (context, v4_addr, et->caddr)) {
156         kdc_log(context, config, 0, "Incorrect network address (%s)", from);
157         krb5_free_address(context, v4_addr);
158         free (v4_addr);
159         return KRB5KRB_AP_ERR_BADADDR;
160     }
161     if(v4_addr->addr_type == KRB5_ADDRESS_INET) {
162         /* we need to collapse the addresses in the ticket to a
163            single address; best guess is to use the address the
164            connection came from */
165         
166         if (et->caddr != NULL) {
167             free_HostAddresses(et->caddr);
168         } else {
169             et->caddr = malloc (sizeof (*et->caddr));
170             if (et->caddr == NULL) {
171                 krb5_free_address(context, v4_addr);
172                 free(v4_addr);
173                 return ENOMEM;
174             }
175         }
176         et->caddr->val = v4_addr;
177         et->caddr->len = 1;
178     } else {
179         krb5_free_address(context, v4_addr);
180         free(v4_addr);
181     }
182     return 0;
183 }
184
185
186 static krb5_error_code
187 encrypt_v4_ticket(krb5_context context, 
188                   krb5_kdc_configuration *config,
189                   void *buf, 
190                   size_t len, 
191                   krb5_keyblock *skey, 
192                   EncryptedData *reply)
193 {
194     krb5_crypto crypto;
195     krb5_error_code ret;
196     ret = krb5_crypto_init(context, skey, ETYPE_DES_PCBC_NONE, &crypto);
197     if (ret) {
198         free(buf);
199         kdc_log(context, config, 0, "krb5_crypto_init failed: %s",
200                 krb5_get_err_text(context, ret));
201         return ret;
202     }
203
204     ret = krb5_encrypt_EncryptedData(context, 
205                                      crypto,
206                                      KRB5_KU_TICKET,
207                                      buf,
208                                      len,
209                                      0,
210                                      reply);
211     krb5_crypto_destroy(context, crypto);
212     if(ret) {
213         kdc_log(context, config, 0, "Failed to encrypt data: %s",
214                 krb5_get_err_text(context, ret));
215         return ret;
216     }
217     return 0;
218 }
219
220 static krb5_error_code
221 encode_524_response(krb5_context context, 
222                     krb5_kdc_configuration *config,
223                     const char *spn, const EncTicketPart et,
224                     const Ticket *t, hdb_entry *server, 
225                     EncryptedData *ticket, int *kvno)
226 {
227     krb5_error_code ret;
228     int use_2b;
229     size_t len;
230
231     use_2b = krb5_config_get_bool(context, NULL, "kdc", "use_2b", spn, NULL);
232     if(use_2b) {
233         ASN1_MALLOC_ENCODE(EncryptedData, 
234                            ticket->cipher.data, ticket->cipher.length, 
235                            &t->enc_part, &len, ret);
236         
237         if (ret) {
238             kdc_log(context, config, 0, 
239                     "Failed to encode v4 (2b) ticket (%s)", spn);
240             return ret;
241         }
242         
243         ticket->etype = 0;
244         ticket->kvno = NULL;
245         *kvno = 213; /* 2b's use this magic kvno */
246     } else {
247         unsigned char buf[MAX_KTXT_LEN + 4 * 4];
248         Key *skey;
249         
250         if (!config->enable_v4_cross_realm && strcmp (et.crealm, t->realm) != 0) {
251             kdc_log(context, config, 0, "524 cross-realm %s -> %s disabled", et.crealm,
252                     t->realm);
253             return KRB5KDC_ERR_POLICY;
254         }
255
256         ret = _kdc_encode_v4_ticket(context, config, 
257                                     buf + sizeof(buf) - 1, sizeof(buf),
258                                     &et, &t->sname, &len);
259         if(ret){
260             kdc_log(context, config, 0,
261                     "Failed to encode v4 ticket (%s)", spn);
262             return ret;
263         }
264         ret = _kdc_get_des_key(context, server, TRUE, FALSE, &skey);
265         if(ret){
266             kdc_log(context, config, 0,
267                     "no suitable DES key for server (%s)", spn);
268             return ret;
269         }
270         ret = encrypt_v4_ticket(context, config, buf + sizeof(buf) - len, len, 
271                                 &skey->key, ticket);
272         if(ret){
273             kdc_log(context, config, 0,
274                     "Failed to encrypt v4 ticket (%s)", spn);
275             return ret;
276         }
277         *kvno = server->kvno;
278     }
279
280     return 0;
281 }
282
283 /*
284  * process a 5->4 request, based on `t', and received `from, addr',
285  * returning the reply in `reply'
286  */
287
288 krb5_error_code
289 _kdc_do_524(krb5_context context, 
290             krb5_kdc_configuration *config,
291             const Ticket *t, krb5_data *reply,
292             const char *from, struct sockaddr *addr)
293 {
294     krb5_error_code ret = 0;
295     krb5_crypto crypto;
296     hdb_entry *server = NULL;
297     Key *skey;
298     krb5_data et_data;
299     EncTicketPart et;
300     EncryptedData ticket;
301     krb5_storage *sp;
302     char *spn = NULL;
303     unsigned char buf[MAX_KTXT_LEN + 4 * 4];
304     size_t len;
305     int kvno = 0;
306     
307     if(!config->enable_524) {
308         ret = KRB5KDC_ERR_POLICY;
309         kdc_log(context, config, 0,
310                 "Rejected ticket conversion request from %s", from);
311         goto out;
312     }
313
314     ret = fetch_server (context, config, t, &spn, &server, from);
315     if (ret) {
316         goto out;
317     }
318
319     ret = hdb_enctype2key(context, server, t->enc_part.etype, &skey);
320     if(ret){
321         kdc_log(context, config, 0,
322                 "No suitable key found for server (%s) from %s", spn, from);
323         goto out;
324     }
325     ret = krb5_crypto_init(context, &skey->key, 0, &crypto);
326     if (ret) {
327         kdc_log(context, config, 0, "krb5_crypto_init failed: %s",
328                 krb5_get_err_text(context, ret));
329         goto out;
330     }
331     ret = krb5_decrypt_EncryptedData (context,
332                                       crypto,
333                                       KRB5_KU_TICKET,
334                                       &t->enc_part,
335                                       &et_data);
336     krb5_crypto_destroy(context, crypto);
337     if(ret){
338         kdc_log(context, config, 0,
339                 "Failed to decrypt ticket from %s for %s", from, spn);
340         goto out;
341     }
342     ret = krb5_decode_EncTicketPart(context, et_data.data, et_data.length, 
343                                     &et, &len);
344     krb5_data_free(&et_data);
345     if(ret){
346         kdc_log(context, config, 0,
347                 "Failed to decode ticket from %s for %s", from, spn);
348         goto out;
349     }
350
351     ret = log_524 (context, config, &et, from, spn);
352     if (ret) {
353         free_EncTicketPart(&et);
354         goto out;
355     }
356
357     ret = verify_flags (context, config, &et, spn);
358     if (ret) {
359         free_EncTicketPart(&et);
360         goto out;
361     }
362
363     ret = set_address (context, config, &et, addr, from);
364     if (ret) {
365         free_EncTicketPart(&et);
366         goto out;
367     }
368
369     ret = encode_524_response(context, config, spn, et, t,
370                               server, &ticket, &kvno);
371     free_EncTicketPart(&et);
372
373  out:
374     /* make reply */
375     memset(buf, 0, sizeof(buf));
376     sp = krb5_storage_from_mem(buf, sizeof(buf));
377     krb5_store_int32(sp, ret);
378     if(ret == 0){
379         krb5_store_int32(sp, kvno);
380         krb5_store_data(sp, ticket.cipher);
381         /* Aargh! This is coded as a KTEXT_ST. */
382         krb5_storage_seek(sp, MAX_KTXT_LEN - ticket.cipher.length, SEEK_CUR);
383         krb5_store_int32(sp, 0); /* mbz */
384         free_EncryptedData(&ticket);
385     }
386     ret = krb5_storage_to_data(sp, reply);
387     reply->length = krb5_storage_seek(sp, 0, SEEK_CUR);
388     krb5_storage_free(sp);
389     
390     if(spn)
391         free(spn);
392     if(server)
393         _kdc_free_ent (context, server);
394     return ret;
395 }