import from branch_1_1:
[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 } rlm_eap_peap_t;
55
56
57 static CONF_PARSER module_config[] = {
58         { "default_eap_type", PW_TYPE_STRING_PTR,
59           offsetof(rlm_eap_peap_t, default_eap_type_name), NULL, "mschapv2" },
60
61         { "copy_request_to_tunnel", PW_TYPE_BOOLEAN,
62           offsetof(rlm_eap_peap_t, copy_request_to_tunnel), NULL, "no" },
63
64         { "use_tunneled_reply", PW_TYPE_BOOLEAN,
65           offsetof(rlm_eap_peap_t, use_tunneled_reply), NULL, "no" },
66
67         { "proxy_tunneled_request_as_eap", PW_TYPE_BOOLEAN,
68           offsetof(rlm_eap_peap_t, proxy_tunneled_request_as_eap), NULL, "yes" },
69
70         { NULL, -1, 0, NULL, NULL }           /* end the list */
71 };
72
73 /*
74  *      Detach the module.
75  */
76 static int eappeap_detach(void *arg)
77 {
78         rlm_eap_peap_t *inst = (rlm_eap_peap_t *) arg;
79
80
81         free(inst);
82
83         return 0;
84 }
85
86 /*
87  *      Attach the module.
88  */
89 static int eappeap_attach(CONF_SECTION *cs, void **instance)
90 {
91         rlm_eap_peap_t *inst;
92
93         inst = malloc(sizeof(*inst));
94         if (!inst) {
95                 radlog(L_ERR, "rlm_eap_peap: out of memory");
96                 return -1;
97         }
98         memset(inst, 0, sizeof(*inst));
99
100         /*
101          *      Parse the configuration attributes.
102          */
103         if (cf_section_parse(cs, inst, module_config) < 0) {
104                 eappeap_detach(inst);
105                 return -1;
106         }
107
108         /*
109          *      Convert the name to an integer, to make it easier to
110          *      handle.
111          */
112         inst->default_eap_type = eaptype_name2type(inst->default_eap_type_name);
113         if (inst->default_eap_type < 0) {
114                 radlog(L_ERR, "rlm_eap_peap: Unknown EAP type %s",
115                        inst->default_eap_type_name);
116                 eappeap_detach(inst);
117                 return -1;
118         }
119
120         *instance = inst;
121
122         return 0;
123 }
124
125 /*
126  *      Free the PEAP per-session data
127  */
128 static void peap_free(void *p)
129 {
130         peap_tunnel_t *t = (peap_tunnel_t *) p;
131
132         if (!t) return;
133
134         pairfree(&t->username);
135         pairfree(&t->state);
136         pairfree(&t->accept_vps);
137
138         free(t);
139 }
140
141
142 /*
143  *      Free the PEAP per-session data
144  */
145 static peap_tunnel_t *peap_alloc(rlm_eap_peap_t *inst)
146 {
147         peap_tunnel_t *t;
148
149         t = rad_malloc(sizeof(*t));
150         memset(t, 0, sizeof(*t));
151
152         t->default_eap_type = inst->default_eap_type;
153         t->copy_request_to_tunnel = inst->copy_request_to_tunnel;
154         t->use_tunneled_reply = inst->use_tunneled_reply;
155         t->proxy_tunneled_request_as_eap = inst->proxy_tunneled_request_as_eap;
156
157         return t;
158 }
159
160 /*
161  *      Do authentication, by letting EAP-TLS do most of the work.
162  */
163 static int eappeap_authenticate(void *arg, EAP_HANDLER *handler)
164 {
165         int rcode;
166         eaptls_status_t status;
167         rlm_eap_peap_t *inst = (rlm_eap_peap_t *) arg;
168         tls_session_t *tls_session = (tls_session_t *) handler->opaque;
169
170         DEBUG2("  rlm_eap_peap: Authenticate");
171
172         status = eaptls_process(handler);
173         DEBUG2("  eaptls_process returned %d\n", status);
174         switch (status) {
175                 /*
176                  *      EAP-TLS handshake was successful, tell the
177                  *      client to keep talking.
178                  *
179                  *      If this was EAP-TLS, we would just return
180                  *      an EAP-TLS-Success packet here.
181                  */
182         case EAPTLS_SUCCESS:
183                 {
184                         eap_packet_t eap_packet;
185
186                         eap_packet.code = PW_EAP_REQUEST;
187                         eap_packet.id = handler->eap_ds->response->id + 1;
188                         eap_packet.length[0] = 0;
189                         eap_packet.length[1] = EAP_HEADER_LEN + 1;
190                         eap_packet.data[0] = PW_EAP_IDENTITY;
191
192                         (tls_session->record_plus)(&tls_session->clean_in,
193                                                   &eap_packet, sizeof(eap_packet));
194                         
195                         tls_handshake_send(tls_session);
196                         (tls_session->record_init)(&tls_session->clean_in);
197                 }
198                 eaptls_request(handler->eap_ds, tls_session);
199                 DEBUG2("  rlm_eap_peap: EAPTLS_SUCCESS");
200                 return 1;
201
202                 /*
203                  *      The TLS code is still working on the TLS
204                  *      exchange, and it's a valid TLS request.
205                  *      do nothing.
206                  */
207         case EAPTLS_HANDLED:
208                 DEBUG2("  rlm_eap_peap: EAPTLS_HANDLED");
209                 return 1;
210
211                 /*
212                  *      Handshake is done, proceed with decoding tunneled
213                  *      data.
214                  */
215         case EAPTLS_OK:
216                 DEBUG2("  rlm_eap_peap: EAPTLS_OK");
217                 break;
218
219                 /*
220                  *      Anything else: fail.
221                  */
222         default:
223                 DEBUG2("  rlm_eap_peap: EAPTLS_OTHERS");
224                 return 0;
225         }
226
227         /*
228          *      Session is established, proceed with decoding
229          *      tunneled data.
230          */
231         DEBUG2("  rlm_eap_peap: Session established.  Decoding tunneled attributes.");
232
233         /*
234          *      We may need PEAP data associated with the session, so
235          *      allocate it here, if it wasn't already alloacted.
236          */
237         if (!tls_session->opaque) {
238                 tls_session->opaque = peap_alloc(inst);
239                 tls_session->free_opaque = peap_free;
240         }
241
242         /*
243          *      Process the PEAP portion of the request.
244          */
245         rcode = eappeap_process(handler, tls_session);
246         switch (rcode) {
247         case RLM_MODULE_REJECT:
248                 eaptls_fail(handler->eap_ds, 0);
249                 return 0;
250
251         case RLM_MODULE_HANDLED:
252                 eaptls_request(handler->eap_ds, tls_session);
253                 return 1;
254
255         case RLM_MODULE_OK:
256                 eaptls_success(handler->eap_ds, 0);
257
258                 /*
259                  *      Move the saved VP's from the Access-Accept to
260                  *      our Access-Accept.
261                  */
262                 if (((peap_tunnel_t *) tls_session->opaque)->accept_vps) {
263                         DEBUG2("  Using saved attributes from the original Access-Accept");
264                 }
265                 pairadd(&handler->request->reply->vps,
266                         ((peap_tunnel_t *) tls_session->opaque)->accept_vps);
267                 ((peap_tunnel_t *) tls_session->opaque)->accept_vps = NULL;
268
269                 eaptls_gen_mppe_keys(&handler->request->reply->vps,
270                                      tls_session->ssl,
271                                      "client EAP encryption");
272
273                 return 1;
274
275                 /*
276                  *      No response packet, MUST be proxying it.
277                  *      The main EAP module will take care of discovering
278                  *      that the request now has a "proxy" packet, and
279                  *      will proxy it, rather than returning an EAP packet.
280                  */
281         case RLM_MODULE_UPDATED:
282                 rad_assert(handler->request->proxy != NULL);
283                 return 1;
284                 break;
285
286         default:
287                 break;
288         }
289
290         eaptls_fail(handler->eap_ds, 0);
291         return 0;
292 }
293
294
295 /*
296  *      The module name should be the only globally exported symbol.
297  *      That is, everything else should be 'static'.
298  */
299 EAP_TYPE rlm_eap_peap = {
300         "eap_peap",
301         eappeap_attach,                 /* attach */
302         /*
303          *      Note! There is NO eappeap_initate() function, as the
304          *      main EAP module takes care of calling
305          *      eaptls_initiate().
306          *
307          *      This is because PEAP is a protocol on top of TLS, so
308          *      before we need to do PEAP, we've got to initiate a TLS
309          *      session.
310          */
311         NULL,                           /* Start the initial request */
312         NULL,                           /* authorization */
313         eappeap_authenticate,           /* authentication */
314         eappeap_detach                  /* detach */
315 };