append tunneled reply, rather than moving it
[freeradius.git] / src / modules / rlm_eap / types / rlm_eap_peap / rlm_eap_peap.c
1 /*
2  * rlm_eap_peap.c  contains the interfaces that are called from eap
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2003 Alan DeKok <aland@freeradius.org>
21  * Copyright 2006 The FreeRADIUS server project
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/autoconf.h>
28 #include "eap_peap.h"
29
30 typedef struct rlm_eap_peap_t {
31         /*
32          *      Default tunneled EAP type
33          */
34         char    *default_eap_type_name;
35         int     default_eap_type;
36
37         /*
38          *      Use the reply attributes from the tunneled session in
39          *      the non-tunneled reply to the client.
40          */
41         int     use_tunneled_reply;
42
43         /*
44          *      Use SOME of the request attributes from outside of the
45          *      tunneled session in the tunneled request
46          */
47         int     copy_request_to_tunnel;
48
49         /*
50          *      Proxy tunneled session as EAP, or as de-capsulated
51          *      protocol.
52          */
53         int     proxy_tunneled_request_as_eap;
54
55         /*
56          *      Virtual server for inner tunnel session.
57          */
58         char    *virtual_server;
59 } rlm_eap_peap_t;
60
61
62 static CONF_PARSER module_config[] = {
63         { "default_eap_type", PW_TYPE_STRING_PTR,
64           offsetof(rlm_eap_peap_t, default_eap_type_name), NULL, "mschapv2" },
65
66         { "copy_request_to_tunnel", PW_TYPE_BOOLEAN,
67           offsetof(rlm_eap_peap_t, copy_request_to_tunnel), NULL, "no" },
68
69         { "use_tunneled_reply", PW_TYPE_BOOLEAN,
70           offsetof(rlm_eap_peap_t, use_tunneled_reply), NULL, "no" },
71
72         { "proxy_tunneled_request_as_eap", PW_TYPE_BOOLEAN,
73           offsetof(rlm_eap_peap_t, proxy_tunneled_request_as_eap), NULL, "yes" },
74
75         { "virtual_server", PW_TYPE_STRING_PTR,
76           offsetof(rlm_eap_peap_t, virtual_server), NULL, NULL },
77
78         { NULL, -1, 0, NULL, NULL }           /* end the list */
79 };
80
81 /*
82  *      Detach the module.
83  */
84 static int eappeap_detach(void *arg)
85 {
86         rlm_eap_peap_t *inst = (rlm_eap_peap_t *) arg;
87
88
89         free(inst);
90
91         return 0;
92 }
93
94 /*
95  *      Attach the module.
96  */
97 static int eappeap_attach(CONF_SECTION *cs, void **instance)
98 {
99         rlm_eap_peap_t *inst;
100
101         inst = malloc(sizeof(*inst));
102         if (!inst) {
103                 radlog(L_ERR, "rlm_eap_peap: out of memory");
104                 return -1;
105         }
106         memset(inst, 0, sizeof(*inst));
107
108         /*
109          *      Parse the configuration attributes.
110          */
111         if (cf_section_parse(cs, inst, module_config) < 0) {
112                 eappeap_detach(inst);
113                 return -1;
114         }
115
116         /*
117          *      Convert the name to an integer, to make it easier to
118          *      handle.
119          */
120         inst->default_eap_type = eaptype_name2type(inst->default_eap_type_name);
121         if (inst->default_eap_type < 0) {
122                 radlog(L_ERR, "rlm_eap_peap: Unknown EAP type %s",
123                        inst->default_eap_type_name);
124                 eappeap_detach(inst);
125                 return -1;
126         }
127
128         *instance = inst;
129
130         return 0;
131 }
132
133 /*
134  *      Free the PEAP per-session data
135  */
136 static void peap_free(void *p)
137 {
138         peap_tunnel_t *t = (peap_tunnel_t *) p;
139
140         if (!t) return;
141
142         pairfree(&t->username);
143         pairfree(&t->state);
144         pairfree(&t->accept_vps);
145
146         free(t);
147 }
148
149
150 /*
151  *      Free the PEAP per-session data
152  */
153 static peap_tunnel_t *peap_alloc(rlm_eap_peap_t *inst)
154 {
155         peap_tunnel_t *t;
156
157         t = rad_malloc(sizeof(*t));
158         memset(t, 0, sizeof(*t));
159
160         t->default_eap_type = inst->default_eap_type;
161         t->copy_request_to_tunnel = inst->copy_request_to_tunnel;
162         t->use_tunneled_reply = inst->use_tunneled_reply;
163         t->proxy_tunneled_request_as_eap = inst->proxy_tunneled_request_as_eap;
164         t->virtual_server = inst->virtual_server;
165         t->session_resumption_state = PEAP_RESUMPTION_MAYBE;
166
167         return t;
168 }
169
170 /*
171  *      Do authentication, by letting EAP-TLS do most of the work.
172  */
173 static int eappeap_authenticate(void *arg, EAP_HANDLER *handler)
174 {
175         int rcode;
176         eaptls_status_t status;
177         rlm_eap_peap_t *inst = (rlm_eap_peap_t *) arg;
178         tls_session_t *tls_session = (tls_session_t *) handler->opaque;
179         peap_tunnel_t *peap = tls_session->opaque;
180         REQUEST *request = handler->request;
181
182         /*
183          *      Session resumption requires the storage of data, so
184          *      allocate it if it doesn't already exist.
185          */
186         if (!tls_session->opaque) {
187                 peap = tls_session->opaque = peap_alloc(inst);
188                 tls_session->free_opaque = peap_free;
189         }
190
191         status = eaptls_process(handler);
192         RDEBUG2("eaptls_process returned %d\n", status);
193         switch (status) {
194                 /*
195                  *      EAP-TLS handshake was successful, tell the
196                  *      client to keep talking.
197                  *
198                  *      If this was EAP-TLS, we would just return
199                  *      an EAP-TLS-Success packet here.
200                  */
201         case EAPTLS_SUCCESS:
202                 if (SSL_session_reused(tls_session->ssl)) {
203                         uint8_t tlv_packet[11];
204                         
205                         RDEBUG2("Skipping Phase2 because of session resumption.");
206                         peap->session_resumption_state = PEAP_RESUMPTION_YES;
207                         
208                         tlv_packet[0] = PW_EAP_REQUEST;
209                         tlv_packet[1] = handler->eap_ds->response->id +1;
210                         tlv_packet[2] = 0;
211                         tlv_packet[3] = 11;     /* length of this packet */
212                         tlv_packet[4] = PW_EAP_TLV;
213                         tlv_packet[5] = 0x80;
214                         tlv_packet[6] = EAP_TLV_ACK_RESULT;
215                         tlv_packet[7] = 0;
216                         tlv_packet[8] = 2;      /* length of the data portion */
217                         tlv_packet[9] = 0;
218                         tlv_packet[10] = EAP_TLV_SUCCESS;
219                         
220                         peap->status = PEAP_STATUS_SENT_TLV_SUCCESS;
221
222                         (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
223                         tls_handshake_send(tls_session);
224                         (tls_session->record_init)(&tls_session->clean_in);
225
226                 } else {
227                         eap_packet_t eap_packet;
228
229                         eap_packet.code = PW_EAP_REQUEST;
230                         eap_packet.id = handler->eap_ds->response->id + 1;
231                         eap_packet.length[0] = 0;
232                         eap_packet.length[1] = EAP_HEADER_LEN + 1;
233                         eap_packet.data[0] = PW_EAP_IDENTITY;
234
235                         (tls_session->record_plus)(&tls_session->clean_in,
236                                                   &eap_packet, sizeof(eap_packet));
237
238                         tls_handshake_send(tls_session);
239                         (tls_session->record_init)(&tls_session->clean_in);
240                 }
241
242                 eaptls_request(handler->eap_ds, tls_session);
243                 RDEBUG2("EAPTLS_SUCCESS");
244                 return 1;
245
246                 /*
247                  *      The TLS code is still working on the TLS
248                  *      exchange, and it's a valid TLS request.
249                  *      do nothing.
250                  */
251         case EAPTLS_HANDLED:
252           /*
253            *    FIXME: If the SSL session is established, grab the state
254            *    and EAP id from the inner tunnel, and update it with
255            *    the expected EAP id!
256            */
257                 RDEBUG2("EAPTLS_HANDLED");
258                 return 1;
259
260                 /*
261                  *      Handshake is done, proceed with decoding tunneled
262                  *      data.
263                  */
264         case EAPTLS_OK:
265                 RDEBUG2("EAPTLS_OK");
266                 break;
267
268                 /*
269                  *      Anything else: fail.
270                  */
271         default:
272                 RDEBUG2("EAPTLS_OTHERS");
273                 return 0;
274         }
275
276         /*
277          *      Session is established, proceed with decoding
278          *      tunneled data.
279          */
280         RDEBUG2("Session established.  Decoding tunneled attributes.");
281
282         /*
283          *      We may need PEAP data associated with the session, so
284          *      allocate it here, if it wasn't already alloacted.
285          */
286         if (!tls_session->opaque) {
287                 tls_session->opaque = peap_alloc(inst);
288                 tls_session->free_opaque = peap_free;
289         }
290
291         /*
292          *      Process the PEAP portion of the request.
293          */
294         rcode = eappeap_process(handler, tls_session);
295         switch (rcode) {
296         case RLM_MODULE_REJECT:
297                 eaptls_fail(handler, 0);
298                 return 0;
299
300         case RLM_MODULE_HANDLED:
301                 eaptls_request(handler->eap_ds, tls_session);
302                 return 1;
303
304         case RLM_MODULE_OK:
305                 /*
306                  *      Move the saved VP's from the Access-Accept to
307                  *      our Access-Accept.
308                  */
309                 peap = tls_session->opaque;
310                 if (peap->accept_vps) {
311                         RDEBUG2("Using saved attributes from the original Access-Accept");
312                         pairadd(&handler->request->reply->vps, &peap->accept_vps);
313                         pairfree(&peap->accept_vps);
314                 }
315
316                 /*
317                  *      Success: Automatically return MPPE keys.
318                  */
319                 return eaptls_success(handler, 0);
320
321                 /*
322                  *      No response packet, MUST be proxying it.
323                  *      The main EAP module will take care of discovering
324                  *      that the request now has a "proxy" packet, and
325                  *      will proxy it, rather than returning an EAP packet.
326                  */
327         case RLM_MODULE_UPDATED:
328                 rad_assert(handler->request->proxy != NULL);
329                 return 1;
330                 break;
331
332         default:
333                 break;
334         }
335
336         eaptls_fail(handler, 0);
337         return 0;
338 }
339
340
341 /*
342  *      The module name should be the only globally exported symbol.
343  *      That is, everything else should be 'static'.
344  */
345 EAP_TYPE rlm_eap_peap = {
346         "eap_peap",
347         eappeap_attach,                 /* attach */
348         /*
349          *      Note! There is NO eappeap_initate() function, as the
350          *      main EAP module takes care of calling
351          *      eaptls_initiate().
352          *
353          *      This is because PEAP is a protocol on top of TLS, so
354          *      before we need to do PEAP, we've got to initiate a TLS
355          *      session.
356          */
357         NULL,                           /* Start the initial request */
358         NULL,                           /* authorization */
359         eappeap_authenticate,           /* authentication */
360         eappeap_detach                  /* detach */
361 };