eap.h support for tunneled callbacks
[freeradius.git] / src / modules / rlm_eap / rlm_eap.c
1 /*
2  * rlm_eap.c  contains handles that are called from modules.
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 2000-2003  The FreeRADIUS server project
21  * Copyright 2001  hereUare Communications, Inc. <raghud@hereuare.com>
22  * Copyright 2003  Alan DeKok <aland@freeradius.org>
23  */
24
25 #include "autoconf.h"
26 #include "rlm_eap.h"
27 #include "modules.h"
28
29 static const char rcsid[] = "$Id$";
30
31 static const CONF_PARSER module_config[] = {
32         { "default_eap_type", PW_TYPE_STRING_PTR,
33           offsetof(rlm_eap_t, default_eap_type_name), NULL, "md5" },
34         { "timer_expire", PW_TYPE_INTEGER,
35           offsetof(rlm_eap_t, timer_limit), NULL, "60"},
36         { "ignore_unknown_eap_types", PW_TYPE_BOOLEAN,
37           offsetof(rlm_eap_t, ignore_unknown_eap_types), NULL, "no" },
38         
39         { NULL, -1, 0, NULL, NULL }           /* end the list */
40 };
41
42 static int eap_init(void)
43 {
44         return 0;
45 }
46
47
48 /*
49  * delete all the allocated space by eap module
50  */
51 static int eap_detach(void *instance)
52 {
53         rlm_eap_t *inst;
54         int i;
55
56         inst = (rlm_eap_t *)instance;
57
58         eaplist_free(inst);
59
60         for (i = 0; i < PW_EAP_MAX_TYPES; i++) {
61                 if (inst->types[i]) eaptype_free(inst->types[i]);
62                 inst->types[i] = NULL;
63         }
64
65 #ifdef HAVE_PTHREAD_H
66         pthread_mutex_destroy(&(inst->session_mutex));
67         pthread_mutex_destroy(&(inst->module_mutex));
68 #endif
69
70         if (inst->default_eap_type_name) free(inst->default_eap_type_name);
71         free(inst);
72
73         return 0;
74 }
75
76
77 /*
78  * read the config section and load all the eap authentication types present.
79  */
80 static int eap_instantiate(CONF_SECTION *cs, void **instance)
81 {
82         int             eap_type;
83         int             num_types;
84         CONF_SECTION    *scs;
85         rlm_eap_t       *inst;
86         
87         inst = (rlm_eap_t *) malloc(sizeof(*inst));
88         if (!inst) {
89                 return -1;
90         }
91         memset(inst, 0, sizeof(*inst));
92         if (cf_section_parse(cs, inst, module_config) < 0) {
93                 eap_detach(inst);
94                 return -1;
95         }
96
97         /* Load all the configured EAP-Types */
98         num_types = 0;
99         for(scs=cf_subsection_find_next(cs, NULL, NULL);
100                 scs != NULL;
101                 scs=cf_subsection_find_next(cs, scs, NULL)) {
102
103                 char    *auth_type;
104
105                 auth_type = cf_section_name1(scs);
106
107                 if (!auth_type)  continue;
108
109                 eap_type = eaptype_name2type(auth_type);
110                 if (eap_type < 0) {
111                         radlog(L_ERR|L_CONS, "rlm_eap: Unknown EAP type %s",
112                                auth_type);
113                         eap_detach(inst);
114                         return -1;
115                 }
116
117                 /*
118                  *      If we're asked to load TTLS or PEAP, ensure
119                  *      that we've first loaded TLS.
120                  */
121                 if (((eap_type == PW_EAP_TTLS) ||
122                      (eap_type == PW_EAP_PEAP)) &&
123                     (inst->types[PW_EAP_TLS] == NULL)) {
124                         radlog(L_ERR, "rlm_eap: Unable to load EAP-Type/%s, as EAP-Type/TLS is required first.",
125                                auth_type);
126                         return -1;
127                 }
128
129                 /*
130                  *      Load the type.
131                  */
132                 if (eaptype_load(&inst->types[eap_type], eap_type, scs) < 0) {
133                         eap_detach(inst);
134                         return -1;
135                 }
136
137                 num_types++;    /* successfully loaded one more types */
138         }
139
140         if (num_types == 0) {
141                 radlog(L_ERR|L_CONS, "rlm_eap: No EAP type configured, module cannot do anything.");
142                 eap_detach(inst);
143                 return -1;
144         }
145
146         /*
147          *      Ensure that the default EAP type is loaded.
148          */
149         eap_type = eaptype_name2type(inst->default_eap_type_name);
150         if (eap_type < 0) {
151                 radlog(L_ERR|L_CONS, "rlm_eap: Unknown default EAP type %s",
152                        inst->default_eap_type_name);
153                 eap_detach(inst);
154                 return -1;
155         }
156
157         if (inst->types[eap_type] == NULL) {
158                 radlog(L_ERR|L_CONS, "rlm_eap: No such sub-type for default EAP type %s",
159                        inst->default_eap_type_name);
160                 eap_detach(inst);
161                 return -1;
162         }
163         inst->default_eap_type = eap_type; /* save the numerical type */
164
165         /*
166          *      List of sessions are set to NULL by the memset
167          *      of 'inst', above.
168          */
169
170         /* Generate a state key, specific to eap */
171         generate_key();
172
173 #ifdef HAVE_PTHREAD_H
174         pthread_mutex_init(&(inst->session_mutex), NULL);
175         pthread_mutex_init(&(inst->module_mutex), NULL);
176 #endif
177         
178         *instance = inst;
179         return 0;
180 }
181
182 /*
183  *      Dumb wrapper.
184  *      FIXME: this should be done more intelligently...
185  */
186 static void my_handler_free(void *data)
187 {
188   EAP_HANDLER *handler = (EAP_HANDLER *) data;
189   eap_handler_free(&handler);
190 }
191
192 /*
193  *      For backwards compatibility.
194  */
195 static int eap_authenticate(void *instance, REQUEST *request)
196 {
197         rlm_eap_t       *inst;
198         EAP_HANDLER     *handler;
199         eap_packet_t    *eap_packet;
200         int             rcode;
201 #ifdef HAVE_PTHREAD_H
202         int             locked = FALSE;
203 #endif
204
205         inst = (rlm_eap_t *) instance;
206
207         /*
208          *      Get the eap packet  to start with
209          */
210         eap_packet = eap_attribute(request->packet->vps);
211         if (eap_packet == NULL) {
212                 radlog(L_ERR, "rlm_eap: Malformed EAP Message");
213                 return RLM_MODULE_FAIL;
214         }
215
216         /*
217          *      Create the eap handler.  The eap_packet will end up being
218          *      "swallowed" into the handler, so we can't access it after
219          *      this call.
220          */
221         handler = eap_handler(inst, &eap_packet, request);
222         if (handler == NULL) {
223                 DEBUG2("  rlm_eap: Failed in handler");
224                 return RLM_MODULE_INVALID;
225         }
226
227         /*
228          *      If it's a recursive request, then disallow
229          *      TLS, TTLS, and PEAP, inside of the TLS tunnel.
230          */
231         if ((request->options & RAD_REQUEST_OPTION_FAKE_REQUEST) != 0) {
232                 switch(handler->eap_ds->response->type.type) {
233                 case PW_EAP_TLS:
234                 case PW_EAP_TTLS:
235                 case PW_EAP_PEAP:
236                         DEBUG2(" rlm_eap: Unable to tunnel TLS inside of TLS");
237                         eap_fail(handler);
238                         eap_handler_free(&handler);
239                         return RLM_MODULE_INVALID;
240                         break;
241
242                 default:        /* It may be OK, allow it to proceed */
243                         break;
244
245                 }
246         }
247
248 #ifdef HAVE_PTHREAD_H
249         else {                  /* it's a normal request from a NAS */
250                 /*
251                  *      The OpenSSL code isn't strictly thread-safe,
252                  *      as we've got to provide callback functions.
253                  *
254                  *      Rather than doing that, we just ensure that the
255                  *      sub-modules are locked via a mutex.
256                  *
257                  *      Don't lock it if we're calling ourselves recursively,
258                  *      we've already got the lock.
259                  */
260                 pthread_mutex_lock(&(inst->module_mutex));
261                 locked = TRUE;  /* for recursive calls to the module */
262         }
263 #endif
264
265         /*
266          *      Select the appropriate eap_type or default to the
267          *      configured one
268          */
269         rcode = eaptype_select(inst, handler);
270
271 #ifdef HAVE_PTHREAD_H
272         if (locked) pthread_mutex_unlock(&(inst->module_mutex));
273 #endif
274
275         /*
276          *      If it failed, die.
277          */
278         if (rcode == EAP_INVALID) {
279                 eap_fail(handler);
280                 eap_handler_free(&handler);
281                 DEBUG2("  rlm_eap: Failed in EAP select");
282                 return RLM_MODULE_INVALID;
283         }
284
285         /*
286          *      Maybe the request was marked to be proxied.  If so,
287          *      proxy it.
288          */
289         if (request->proxy != NULL) {
290                 VALUE_PAIR *vp;
291
292                 rad_assert(request->proxy_reply == NULL);
293
294                 /*
295                  *      Add the handle to the proxied list, so that we
296                  *      can retrieve it in the post-proxy stage, and
297                  *      send a response.
298                  */
299                 rcode = request_data_add(request,
300                                          instance, REQUEST_DATA_EAP_HANDLER,
301                                          handler, my_handler_free);
302                 rad_assert(rcode == 0);
303
304                 /*
305                  *      Some simple sanity checks.  These should really
306                  *      be handled by the radius library...
307                  */
308                 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE);
309                 if (vp) {
310                         vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR);
311                         if (!vp) {
312                                 vp = pairmake("Message-Authenticator", "", T_OP_EQ);
313                                 rad_assert(vp != NULL);
314                                 pairadd(&(request->proxy->vps), vp);
315                         }
316                 }
317
318                 return RLM_MODULE_HANDLED;
319         }
320
321
322         /*
323          *      We are done, wrap the EAP-request in RADIUS to send
324          *      with all other required radius attributes
325          */
326         rcode = eap_compose(handler);
327
328         /*
329          *      Add to the list only if it is EAP-Request, OR if
330          *      it's LEAP, and a response.
331          */
332         if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
333             (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
334                 eaplist_add(inst, handler);
335
336                 /*
337                  *      LEAP is a little different.  At Stage 4,
338                  *      it sends an EAP-Success message, but we still
339                  *      need to keep the State attribute & session
340                  *      data structure around for the AP Challenge.
341                  *
342                  *      At stage 6, LEAP sends an EAP-Response, which
343                  *      isn't put into the list.
344                  */
345         } else if ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
346                    (handler->eap_ds->response->type.type == PW_EAP_LEAP) &&
347                    (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
348                    (handler->eap_ds->request->type.type == 0)) {
349
350                 eaplist_add(inst, handler);
351
352         } else {
353                 DEBUG2("  rlm_eap: Freeing handler");
354                 /* handler is not required any more, free it now */
355                 eap_handler_free(&handler);
356         }
357
358         /*
359          *      If it's an Access-Accept, RFC 2869, Section 2.3.1
360          *      says that we MUST include a User-Name attribute in the
361          *      Access-Accept.
362          */
363         if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
364             request->username) {
365                 VALUE_PAIR *vp;
366
367                 /*
368                  *      Doesn't exist, add it in.
369                  */
370                 vp = pairfind(request->reply->vps, PW_USER_NAME);
371                 if (!vp) {
372                         vp = pairmake("User-Name", request->username->strvalue,
373                                       T_OP_EQ);
374                         rad_assert(vp != NULL);
375                         pairadd(&(request->reply->vps), vp);
376                 }
377         }
378
379         return rcode;
380 }
381
382 /*
383  * EAP authorization DEPENDS on other rlm authorizations,
384  * to check for user existance & get their configured values.
385  * It Handles EAP-START Messages, User-Name initilization.
386  */
387 static int eap_authorize(void *instance, REQUEST *request)
388 {
389         rlm_eap_t       *inst;
390         int             status;
391         VALUE_PAIR      *vp;
392         
393         inst = (rlm_eap_t *)instance;
394
395         /*
396          *      We don't do authorization again, once we've seen the
397          *      proxy reply (or the proxied packet)
398          */
399         if (request->proxy != NULL)
400                 return RLM_MODULE_NOOP;
401
402         /*
403          *      For EAP_START, send Access-Challenge with EAP Identity
404          *      request.  even when we have to proxy this request
405          *
406          *      RFC 2869, Section 2.3.1 notes that the "domain" of the
407          *      user, (i.e. where to proxy him) comes from the EAP-Identity,
408          *      so we CANNOT proxy the user, until we know his identity.
409          *
410          *      We therefore send an EAP Identity request.
411          */
412         status = eap_start(inst, request);
413         switch(status) {
414         case EAP_NOOP:
415                 return RLM_MODULE_NOOP;
416         case EAP_FAIL:
417                 return RLM_MODULE_FAIL;
418         case EAP_FOUND:
419                 return RLM_MODULE_HANDLED;
420         case EAP_NOTFOUND:
421         default:
422                 break;
423         }
424         
425         /*
426          *      RFC 2869, Section 2.3.1.  If a NAS sends an EAP-Identity,
427          *      it MUST copy the identity into the User-Name attribute.
428          *
429          *      But we don't worry about that too much.  We depend on
430          *      each EAP sub-module to look for handler->request->username,
431          *      and to get excited if it doesn't appear.
432          */
433
434         vp = pairfind(request->config_items, PW_AUTH_TYPE);
435         if ((!vp) ||
436             (vp->lvalue != PW_AUTHTYPE_REJECT)) {
437                 vp = pairmake("Auth-Type", "EAP", T_OP_EQ);
438                 if (!vp) {
439                         return RLM_MODULE_FAIL;
440                 }
441                 pairadd(&request->config_items, vp);
442         }
443
444         return RLM_MODULE_UPDATED;
445 }
446
447 /*
448  *      If we're proxying EAP, then there may be magic we need
449  *      to do.
450  */
451 static int eap_post_proxy(void *inst, REQUEST *request)
452 {
453         int             i, len;
454         VALUE_PAIR      *vp;
455         EAP_HANDLER     *handler;
456
457         /*
458          *      If there was a handler associated with this request,
459          *      then it's a tunneled request which was proxied...
460          */
461         handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
462         if (handler != NULL) {
463                 int             rcode;
464                 eap_tunnel_data_t *data;
465
466                 /*
467                  *      Grab the tunnel callbacks from the request.
468                  */
469                 data = (eap_tunnel_data_t *) request_data_get(request,
470                                                               request->proxy,
471                                                               REQUEST_DATA_EAP_TUNNEL_CALLBACK);
472                 if (!data) {
473                         radlog(L_ERR, "rlm_eap: Failed to retrieve callback for tunneled session!");
474                         eap_handler_free(&handler);
475                         return RLM_MODULE_FAIL;
476                 }
477
478                 /*
479                  *      Do the callback...
480                  */
481                 rcode = data->callback(handler, data->tls_session);
482                 free(data);
483                 if (rcode == 0) {
484                         eap_handler_free(&handler);
485                         return RLM_MODULE_REJECT;
486                 }
487                 
488                 /*
489                  *      We are done, wrap the EAP-request in RADIUS to send
490                  *      with all other required radius attributes
491                  */
492                 rcode = eap_compose(handler);
493                 
494                 /*
495                  *      Add to the list only if it is EAP-Request, OR if
496                  *      it's LEAP, and a response.
497                  */
498                 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
499                     (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
500                         eaplist_add(inst, handler);
501
502                 } else {        /* couldn't have been LEAP, there's no tunnel */
503                         DEBUG2("  rlm_eap: Freeing handler");
504                         /* handler is not required any more, free it now */
505                         eap_handler_free(&handler);
506                 }
507                 
508                 /*
509                  *      If it's an Access-Accept, RFC 2869, Section 2.3.1
510                  *      says that we MUST include a User-Name attribute in the
511                  *      Access-Accept.
512                  */
513                 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
514                     request->username) {
515                         /*
516                          *      Doesn't exist, add it in.
517                          */
518                         vp = pairfind(request->reply->vps, PW_USER_NAME);
519                         if (!vp) {
520                                 vp = pairmake("User-Name", request->username->strvalue,
521                                               T_OP_EQ);
522                                 rad_assert(vp != NULL);
523                                 pairadd(&(request->reply->vps), vp);
524                         }
525                 }
526                 
527                 return RLM_MODULE_OK;
528         }
529
530
531         /*
532          *      There may be more than one Cisco-AVPair.
533          *      Ensure we find the one with the LEAP attribute.
534          */
535         vp = request->proxy_reply->vps;
536         for (;;) {
537                 /*
538                  *      Hmm... there's got to be a better way to
539                  *      discover codes for vendor attributes.
540                  *
541                  *      This is vendor Cisco (9), Cisco-AVPair
542                  *      attribute (1)
543                  */
544                 vp = pairfind(vp, (9 << 16)  | 1);
545                 if (!vp) {
546                         return RLM_MODULE_NOOP;
547                 }
548                 
549                 /*
550                  *      If it's "leap:session-key", then stop.
551                  *
552                  *      The format is VERY specific!
553                  */
554                 if (strncasecmp(vp->strvalue, "leap:session-key=", 17) == 0) {
555                         break;
556                 }
557         }
558
559         /*
560          *      The format is very specific.
561          */
562         if (vp->length != 17 + 34) {
563                 DEBUG2("  rlm_eap: Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
564                        vp->length, 17 + 34);
565                 return RLM_MODULE_NOOP;
566         }
567
568         /*
569          *      Decrypt the session key, using the proxy data.
570          */
571         i = 34;                 /* starts off with 34 octets */
572         len = rad_tunnel_pwdecode(vp->strvalue + 17, &i,
573                                   request->proxysecret,
574                                   request->proxy->vector);
575
576         /*
577          *      FIXME: Assert that i == 16.
578          */
579
580         /*
581          *      Encrypt the session key again, using the request data.
582          */
583         rad_tunnel_pwencode(vp->strvalue + 17, &len,
584                             request->secret,
585                             request->packet->vector);
586
587         return RLM_MODULE_UPDATED;
588 }
589
590
591 /*
592  *      The module name should be the only globally exported symbol.
593  *      That is, everything else should be 'static'.
594  */
595 module_t rlm_eap = {
596         "eap",
597         RLM_TYPE_THREAD_SAFE,           /* type */
598         eap_init,                       /* initialization */
599         eap_instantiate,                /* instantiation */
600         {
601                 eap_authenticate,       /* authentication */
602                 eap_authorize,          /* authorization */
603                 NULL,                   /* preaccounting */
604                 NULL,                   /* accounting */
605                 NULL,                   /* checksimul */
606                 NULL,                   /* pre-proxy */
607                 eap_post_proxy,         /* post-proxy */
608                 NULL                    /* post-auth */
609         },
610         eap_detach,                     /* detach */
611         NULL,                           /* destroy */
612 };