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