Many more changes to get it to build.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2000-2003,2006  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 <freeradius-devel/ident.h>
26 RCSID("$Id$")
27
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/modules.h>
30
31 #include "rlm_eap.h"
32
33 static const CONF_PARSER module_config[] = {
34         { "default_eap_type", PW_TYPE_STRING_PTR,
35           offsetof(rlm_eap_t, default_eap_type_name), NULL, "md5" },
36         { "timer_expire", PW_TYPE_INTEGER,
37           offsetof(rlm_eap_t, timer_limit), NULL, "60"},
38         { "ignore_unknown_eap_types", PW_TYPE_BOOLEAN,
39           offsetof(rlm_eap_t, ignore_unknown_eap_types), NULL, "no" },
40         { "cisco_accounting_username_bug", PW_TYPE_BOOLEAN,
41           offsetof(rlm_eap_t, cisco_accounting_username_bug), NULL, "no" },
42         { "max_sessions", PW_TYPE_INTEGER,
43           offsetof(rlm_eap_t, max_sessions), NULL, "2048"},
44
45         { NULL, -1, 0, NULL, NULL }           /* end the list */
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         rbtree_free(inst->session_tree);
59         inst->session_tree = NULL;
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         pthread_mutex_destroy(&(inst->session_mutex));
68
69         free(inst);
70
71         return 0;
72 }
73
74
75 /*
76  *      Compare two handlers.
77  */
78 static int eap_handler_cmp(const void *a, const void *b)
79 {
80         int rcode;
81         const EAP_HANDLER *one = a;
82         const EAP_HANDLER *two = b;
83
84         if (one->eap_id < two->eap_id) return -1;
85         if (one->eap_id > two->eap_id) return +1;
86
87         rcode = memcmp(one->state, two->state, sizeof(one->state));
88         if (rcode != 0) return rcode;
89
90         /*
91          *      As of 2.1.8, we don't key off of source IP.  This
92          *      a NAS to send packets load-balanced (or fail-over)
93          *      across multiple intermediate proxies, and still have
94          *      EAP work.
95          */
96         if (fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr) != 0) {
97                 DEBUG("WARNING: EAP packets are arriving from two different upstream servers.  Has there been a proxy fail-over?");
98         }
99
100         return 0;
101 }
102
103
104 /*
105  * read the config section and load all the eap authentication types present.
106  */
107 static int eap_instantiate(CONF_SECTION *cs, void **instance)
108 {
109         int             i, eap_type;
110         int             num_types;
111         CONF_SECTION    *scs;
112         rlm_eap_t       *inst;
113
114         inst = (rlm_eap_t *) malloc(sizeof(*inst));
115         if (!inst) {
116                 return -1;
117         }
118         memset(inst, 0, sizeof(*inst));
119         if (cf_section_parse(cs, inst, module_config) < 0) {
120                 eap_detach(inst);
121                 return -1;
122         }
123
124         /*
125          *      Create our own random pool.
126          */
127         for (i = 0; i < 256; i++) {
128                 inst->rand_pool.randrsl[i] = fr_rand();
129         }
130         fr_randinit(&inst->rand_pool, 1);
131         inst->rand_pool.randcnt = 0;
132
133         inst->xlat_name = cf_section_name2(cs);
134         if (!inst->xlat_name) inst->xlat_name = "EAP";
135
136         /* Load all the configured EAP-Types */
137         num_types = 0;
138         for(scs=cf_subsection_find_next(cs, NULL, NULL);
139                 scs != NULL;
140                 scs=cf_subsection_find_next(cs, scs, NULL)) {
141
142                 const char      *auth_type;
143
144                 auth_type = cf_section_name1(scs);
145
146                 if (!auth_type)  continue;
147
148                 eap_type = eaptype_name2type(auth_type);
149                 if (eap_type < 0) {
150                         radlog(L_ERR, "rlm_eap: Unknown EAP type %s",
151                                auth_type);
152                         eap_detach(inst);
153                         return -1;
154                 }
155
156 #ifndef HAVE_OPENSSL_SSL_H
157                 /*
158                  *      This allows the default configuration to be
159                  *      shipped with EAP-TLS, etc. enabled.  If the
160                  *      system doesn't have OpenSSL, they will be
161                  *      ignored.
162                  *
163                  *      If the system does have OpenSSL, then this
164                  *      code will not be used.  The administrator will
165                  *      then have to delete the tls,
166                  *      etc. configurations from eap.conf in order to
167                  *      have EAP without the TLS types.
168                  */
169                 if ((eap_type == PW_EAP_TLS) ||
170                     (eap_type == PW_EAP_TTLS) ||
171                     (eap_type == PW_EAP_PEAP)) {
172                         DEBUG2("Ignoring EAP-Type/%s because we do not have OpenSSL support.", auth_type);
173                         continue;
174                 }
175 #endif
176
177                 /*
178                  *      If we're asked to load TTLS or PEAP, ensure
179                  *      that we've first loaded TLS.
180                  */
181                 if (((eap_type == PW_EAP_TTLS) ||
182                      (eap_type == PW_EAP_PEAP)) &&
183                     (inst->types[PW_EAP_TLS] == NULL)) {
184                         radlog(L_ERR, "rlm_eap: Unable to load EAP-Type/%s, as EAP-Type/TLS is required first.",
185                                auth_type);
186                         return -1;
187                 }
188
189                 /*
190                  *      Load the type.
191                  */
192                 if (eaptype_load(&inst->types[eap_type], eap_type, scs) < 0) {
193                         eap_detach(inst);
194                         return -1;
195                 }
196
197                 num_types++;    /* successfully loaded one more types */
198         }
199
200         if (num_types == 0) {
201                 radlog(L_ERR|L_CONS, "rlm_eap: No EAP type configured, module cannot do anything.");
202                 eap_detach(inst);
203                 return -1;
204         }
205
206         /*
207          *      Ensure that the default EAP type is loaded.
208          */
209         eap_type = eaptype_name2type(inst->default_eap_type_name);
210         if (eap_type < 0) {
211                 radlog(L_ERR|L_CONS, "rlm_eap: Unknown default EAP type %s",
212                        inst->default_eap_type_name);
213                 eap_detach(inst);
214                 return -1;
215         }
216
217         if (inst->types[eap_type] == NULL) {
218                 radlog(L_ERR|L_CONS, "rlm_eap: No such sub-type for default EAP type %s",
219                        inst->default_eap_type_name);
220                 eap_detach(inst);
221                 return -1;
222         }
223         inst->default_eap_type = eap_type; /* save the numerical type */
224
225         /*
226          *      List of sessions are set to NULL by the memset
227          *      of 'inst', above.
228          */
229
230         /*
231          *      Lookup sessions in the tree.  We don't free them in
232          *      the tree, as that's taken care of elsewhere...
233          */
234         inst->session_tree = rbtree_create(eap_handler_cmp, NULL, 0);
235         if (!inst->session_tree) {
236                 radlog(L_ERR|L_CONS, "rlm_eap: Cannot initialize tree");
237                 eap_detach(inst);
238                 return -1;
239         }
240
241         pthread_mutex_init(&(inst->session_mutex), NULL);
242
243         *instance = inst;
244         return 0;
245 }
246
247
248 /*
249  *      For backwards compatibility.
250  */
251 static int eap_authenticate(void *instance, REQUEST *request)
252 {
253         rlm_eap_t       *inst;
254         EAP_HANDLER     *handler;
255         eap_packet_t    *eap_packet;
256         int             rcode;
257
258         inst = (rlm_eap_t *) instance;
259
260         /*
261          *      Get the eap packet  to start with
262          */
263         eap_packet = eap_vp2packet(request->packet->vps);
264         if (eap_packet == NULL) {
265                 radlog_request(L_ERR, 0, request, "Malformed EAP Message");
266                 return RLM_MODULE_FAIL;
267         }
268
269         /*
270          *      Create the eap handler.  The eap_packet will end up being
271          *      "swallowed" into the handler, so we can't access it after
272          *      this call.
273          */
274         handler = eap_handler(inst, &eap_packet, request);
275         if (handler == NULL) {
276                 RDEBUG2("Failed in handler");
277                 return RLM_MODULE_INVALID;
278         }
279
280         /*
281          *      Select the appropriate eap_type or default to the
282          *      configured one
283          */
284         rcode = eaptype_select(inst, handler);
285
286         /*
287          *      If it failed, die.
288          */
289         if (rcode == EAP_INVALID) {
290                 eap_fail(handler);
291                 eap_handler_free(handler);
292                 RDEBUG2("Failed in EAP select");
293                 return RLM_MODULE_INVALID;
294         }
295
296 #ifdef WITH_PROXY
297         /*
298          *      If we're doing horrible tunneling work, remember it.
299          */
300         if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
301                 RDEBUG2("  Not-EAP proxy set.  Not composing EAP");
302                 /*
303                  *      Add the handle to the proxied list, so that we
304                  *      can retrieve it in the post-proxy stage, and
305                  *      send a response.
306                  */
307                 rcode = request_data_add(request,
308                                          inst, REQUEST_DATA_EAP_HANDLER,
309                                          handler,
310                                          (void *) eap_handler_free);
311                 rad_assert(rcode == 0);
312
313                 return RLM_MODULE_HANDLED;
314         }
315 #endif
316
317 #ifdef WITH_PROXY
318         /*
319          *      Maybe the request was marked to be proxied.  If so,
320          *      proxy it.
321          */
322         if (request->proxy != NULL) {
323                 VALUE_PAIR *vp = NULL;
324
325                 rad_assert(request->proxy_reply == NULL);
326
327                 /*
328                  *      Add the handle to the proxied list, so that we
329                  *      can retrieve it in the post-proxy stage, and
330                  *      send a response.
331                  */
332                 rcode = request_data_add(request,
333                                          inst, REQUEST_DATA_EAP_HANDLER,
334                                          handler,
335                                          (void *) eap_handler_free);
336                 rad_assert(rcode == 0);
337
338                 /*
339                  *      Some simple sanity checks.  These should really
340                  *      be handled by the radius library...
341                  */
342                 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE, 0);
343                 if (vp) {
344                         vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0);
345                         if (!vp) {
346                                 vp = pairmake("Message-Authenticator",
347                                               "0x00", T_OP_EQ);
348                                 rad_assert(vp != NULL);
349                                 pairadd(&(request->proxy->vps), vp);
350                         }
351                 }
352
353                 /*
354                  *      Delete the "proxied to" attribute, as it's
355                  *      set to 127.0.0.1 for tunneled requests, and
356                  *      we don't want to tell the world that...
357                  */
358                 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS);
359
360                 RDEBUG2("  Tunneled session will be proxied.  Not doing EAP.");
361                 return RLM_MODULE_HANDLED;
362         }
363 #endif
364
365         /*
366          *      We are done, wrap the EAP-request in RADIUS to send
367          *      with all other required radius attributes
368          */
369         rcode = eap_compose(handler);
370
371         /*
372          *      Add to the list only if it is EAP-Request, OR if
373          *      it's LEAP, and a response.
374          */
375         if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
376             (handler->eap_ds->request->type.type >= PW_EAP_MD5)) ||
377
378                 /*
379                  *      LEAP is a little different.  At Stage 4,
380                  *      it sends an EAP-Success message, but we still
381                  *      need to keep the State attribute & session
382                  *      data structure around for the AP Challenge.
383                  *
384                  *      At stage 6, LEAP sends an EAP-Response, which
385                  *      isn't put into the list.
386                  */
387             ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
388              (handler->eap_ds->response->type.type == PW_EAP_LEAP) &&
389              (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
390              (handler->eap_ds->request->type.type == 0))) {
391
392                 /*
393                  *      Return FAIL if we can't remember the handler.
394                  *      This is actually disallowed by the
395                  *      specification, as unexpected FAILs could have
396                  *      been forged.  However, we want to signal to
397                  *      everyone else involved that we are
398                  *      intentionally failing the session, as opposed
399                  *      to accidentally failing it.
400                  */
401                 if (!eaplist_add(inst, handler)) {
402                         eap_fail(handler);
403                         eap_handler_free(handler);
404                         return RLM_MODULE_FAIL;
405                 }
406
407         } else {
408                 RDEBUG2("Freeing handler");
409                 /* handler is not required any more, free it now */
410                 eap_handler_free(handler);
411         }
412
413         /*
414          *      If it's an Access-Accept, RFC 2869, Section 2.3.1
415          *      says that we MUST include a User-Name attribute in the
416          *      Access-Accept.
417          */
418         if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
419             request->username) {
420                 VALUE_PAIR *vp;
421
422                 /*
423                  *      Doesn't exist, add it in.
424                  */
425                 vp = pairfind(request->reply->vps, PW_USER_NAME, 0);
426                 if (!vp) {
427                         vp = pairmake("User-Name", "",
428                                       T_OP_EQ);
429                         strlcpy(vp->vp_strvalue, request->username->vp_strvalue,
430                                 sizeof(vp->vp_strvalue));
431                         vp->length = request->username->length;
432                         rad_assert(vp != NULL);
433                         pairadd(&(request->reply->vps), vp);
434                 }
435
436                 /*
437                  *      Cisco AP1230 has a bug and needs a zero
438                  *      terminated string in Access-Accept.
439                  */
440                 if ((inst->cisco_accounting_username_bug) &&
441                     (vp->length < (int) sizeof(vp->vp_strvalue))) {
442                         vp->vp_strvalue[vp->length] = '\0';
443                         vp->length++;
444                 }
445         }
446
447         return rcode;
448 }
449
450 /*
451  * EAP authorization DEPENDS on other rlm authorizations,
452  * to check for user existance & get their configured values.
453  * It Handles EAP-START Messages, User-Name initilization.
454  */
455 static int eap_authorize(void *instance, REQUEST *request)
456 {
457         rlm_eap_t       *inst;
458         int             status;
459         VALUE_PAIR      *vp;
460
461         inst = (rlm_eap_t *)instance;
462
463 #ifdef WITH_PROXY
464         /*
465          *      We don't do authorization again, once we've seen the
466          *      proxy reply (or the proxied packet)
467          */
468         if (request->proxy != NULL)
469                 return RLM_MODULE_NOOP;
470 #endif
471
472         /*
473          *      For EAP_START, send Access-Challenge with EAP Identity
474          *      request.  even when we have to proxy this request
475          *
476          *      RFC 2869, Section 2.3.1 notes that the "domain" of the
477          *      user, (i.e. where to proxy him) comes from the EAP-Identity,
478          *      so we CANNOT proxy the user, until we know his identity.
479          *
480          *      We therefore send an EAP Identity request.
481          */
482         status = eap_start(inst, request);
483         switch(status) {
484         case EAP_NOOP:
485                 return RLM_MODULE_NOOP;
486         case EAP_FAIL:
487                 return RLM_MODULE_FAIL;
488         case EAP_FOUND:
489                 return RLM_MODULE_HANDLED;
490         case EAP_OK:
491         case EAP_NOTFOUND:
492         default:
493                 break;
494         }
495
496         /*
497          *      RFC 2869, Section 2.3.1.  If a NAS sends an EAP-Identity,
498          *      it MUST copy the identity into the User-Name attribute.
499          *
500          *      But we don't worry about that too much.  We depend on
501          *      each EAP sub-module to look for handler->request->username,
502          *      and to get excited if it doesn't appear.
503          */
504
505         vp = pairfind(request->config_items, PW_AUTH_TYPE, 0);
506         if ((!vp) ||
507             (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
508                 vp = pairmake("Auth-Type", inst->xlat_name, T_OP_EQ);
509                 if (!vp) {
510                         RDEBUG2("Failed to create Auth-Type %s: %s\n",
511                                 inst->xlat_name, fr_strerror());
512                         return RLM_MODULE_FAIL;
513                 }
514                 pairadd(&request->config_items, vp);
515         }
516
517         if (status == EAP_OK) return RLM_MODULE_OK;
518
519         return RLM_MODULE_UPDATED;
520 }
521
522
523 #ifdef WITH_PROXY
524 /*
525  *      If we're proxying EAP, then there may be magic we need
526  *      to do.
527  */
528 static int eap_post_proxy(void *inst, REQUEST *request)
529 {
530         size_t          i;
531         size_t          len;
532         VALUE_PAIR      *vp;
533         EAP_HANDLER     *handler;
534
535         /*
536          *      Just in case the admin lists EAP in post-proxy-type Fail.
537          */
538         if (!request->proxy_reply) return RLM_MODULE_NOOP;
539
540         /*
541          *      If there was a handler associated with this request,
542          *      then it's a tunneled request which was proxied...
543          */
544         handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
545         if (handler != NULL) {
546                 int             rcode;
547                 eap_tunnel_data_t *data;
548
549                 /*
550                  *      Grab the tunnel callbacks from the request.
551                  */
552                 data = (eap_tunnel_data_t *) request_data_get(request,
553                                                               request->proxy,
554                                                               REQUEST_DATA_EAP_TUNNEL_CALLBACK);
555                 if (!data) {
556                         radlog_request(L_ERR, 0, request, "Failed to retrieve callback for tunneled session!");
557                         eap_handler_free(handler);
558                         return RLM_MODULE_FAIL;
559                 }
560
561                 /*
562                  *      Do the callback...
563                  */
564                 RDEBUG2("Doing post-proxy callback");
565                 rcode = data->callback(handler, data->tls_session);
566                 free(data);
567                 if (rcode == 0) {
568                         RDEBUG2("Failed in post-proxy callback");
569                         eap_fail(handler);
570                         eap_handler_free(handler);
571                         return RLM_MODULE_REJECT;
572                 }
573
574                 /*
575                  *      We are done, wrap the EAP-request in RADIUS to send
576                  *      with all other required radius attributes
577                  */
578                 eap_compose(handler);
579
580                 /*
581                  *      Add to the list only if it is EAP-Request, OR if
582                  *      it's LEAP, and a response.
583                  */
584                 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
585                     (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
586                         if (!eaplist_add(inst, handler)) {
587                                 eap_fail(handler);
588                                 eap_handler_free(handler);
589                                 return RLM_MODULE_FAIL;
590                         }
591                         
592                 } else {        /* couldn't have been LEAP, there's no tunnel */
593                         RDEBUG2("Freeing handler");
594                         /* handler is not required any more, free it now */
595                         eap_handler_free(handler);
596                 }
597
598                 /*
599                  *      If it's an Access-Accept, RFC 2869, Section 2.3.1
600                  *      says that we MUST include a User-Name attribute in the
601                  *      Access-Accept.
602                  */
603                 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
604                     request->username) {
605                         /*
606                          *      Doesn't exist, add it in.
607                          */
608                         vp = pairfind(request->reply->vps, PW_USER_NAME, 0);
609                         if (!vp) {
610                                 vp = pairmake("User-Name", request->username->vp_strvalue,
611                                               T_OP_EQ);
612                                 rad_assert(vp != NULL);
613                                 pairadd(&(request->reply->vps), vp);
614                         }
615                 }
616
617                 return RLM_MODULE_OK;
618         } else {
619                 RDEBUG2("No pre-existing handler found");
620         }
621
622         /*
623          *      There may be more than one Cisco-AVPair.
624          *      Ensure we find the one with the LEAP attribute.
625          */
626         vp = request->proxy_reply->vps;
627         for (;;) {
628                 /*
629                  *      Hmm... there's got to be a better way to
630                  *      discover codes for vendor attributes.
631                  *
632                  *      This is vendor Cisco (9), Cisco-AVPair
633                  *      attribute (1)
634                  */
635                 vp = pairfind(vp, 1, 9);
636                 if (!vp) {
637                         return RLM_MODULE_NOOP;
638                 }
639
640                 /*
641                  *      If it's "leap:session-key", then stop.
642                  *
643                  *      The format is VERY specific!
644                  */
645                 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
646                         break;
647                 }
648
649                 /*
650                  *      Not this AV-pair.  Go to the next one.
651                  */
652                 vp = vp->next;
653         }
654
655         /*
656          *      The format is very specific.
657          */
658         if (vp->length != 17 + 34) {
659                 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
660                        vp->length, 17 + 34);
661                 return RLM_MODULE_NOOP;
662         }
663
664         /*
665          *      Decrypt the session key, using the proxy data.
666          */
667         i = 34;                 /* starts off with 34 octets */
668         len = rad_tunnel_pwdecode(vp->vp_octets + 17, &i,
669                                   request->home_server->secret,
670                                   request->proxy->vector);
671
672         /*
673          *      FIXME: Assert that i == 16.
674          */
675
676         /*
677          *      Encrypt the session key again, using the request data.
678          */
679         rad_tunnel_pwencode(vp->vp_strvalue + 17, &len,
680                             request->client->secret,
681                             request->packet->vector);
682
683         return RLM_MODULE_UPDATED;
684 }
685 #endif
686
687 /*
688  *      The module name should be the only globally exported symbol.
689  *      That is, everything else should be 'static'.
690  */
691 module_t rlm_eap = {
692         RLM_MODULE_INIT,
693         "eap",
694         RLM_TYPE_CHECK_CONFIG_SAFE,     /* type */
695         eap_instantiate,                /* instantiation */
696         eap_detach,                     /* detach */
697         {
698                 eap_authenticate,       /* authentication */
699                 eap_authorize,          /* authorization */
700                 NULL,                   /* preaccounting */
701                 NULL,                   /* accounting */
702                 NULL,                   /* checksimul */
703                 NULL,                   /* pre-proxy */
704 #ifdef WITH_PROXY
705                 eap_post_proxy,         /* post-proxy */
706 #else
707                 NULL,
708 #endif
709                 NULL                    /* post-auth */
710         },
711 };