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