2 * This program is is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2 if the
4 * License as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * @brief Implements the EAP framework.
21 * @copyright 2000-2003,2006 The FreeRADIUS server project
22 * @copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
23 * @copyright 2003 Alan DeKok <aland@freeradius.org>
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/modules.h>
33 static const CONF_PARSER module_config[] = {
34 { "default_eap_type", PW_TYPE_STRING_PTR,
35 offsetof(rlm_eap_t, default_method_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_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"},
45 { NULL, -1, 0, NULL, NULL } /* end the list */
49 * delete all the allocated space by eap module
51 static int eap_detach(void *instance)
55 inst = (rlm_eap_t *)instance;
58 pthread_mutex_destroy(&(inst->session_mutex));
59 if (inst->handler_tree) pthread_mutex_destroy(&(inst->handler_mutex));
62 rbtree_free(inst->session_tree);
63 if (inst->handler_tree) rbtree_free(inst->handler_tree);
64 inst->session_tree = NULL;
72 * Compare two handlers.
74 static int eap_handler_cmp(const void *a, const void *b)
77 const eap_handler_t *one = a;
78 const eap_handler_t *two = b;
80 if (one->eap_id < two->eap_id) return -1;
81 if (one->eap_id > two->eap_id) return +1;
83 rcode = memcmp(one->state, two->state, sizeof(one->state));
84 if (rcode != 0) return rcode;
87 * As of 2.1.8, we don't key off of source IP. This
88 * a NAS to send packets load-balanced (or fail-over)
89 * across multiple intermediate proxies, and still have
92 if (fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr) != 0) {
93 DEBUGW("EAP packets are arriving from two different upstream "
94 "servers. Has there been a proxy fail-over?");
102 * Compare two handler pointers
104 static int eap_handler_ptr_cmp(const void *a, const void *b)
106 if (a < b) return -1;
107 if (a > b) return +1;
113 * read the config section and load all the eap authentication types present.
115 static int eap_instantiate(CONF_SECTION *cs, void **instance)
123 *instance = inst = talloc_zero(cs, rlm_eap_t);
124 if (!inst) return -1;
126 if (cf_section_parse(cs, inst, module_config) < 0) {
132 * Create our own random pool.
134 for (i = 0; i < 256; i++) {
135 inst->rand_pool.randrsl[i] = fr_rand();
137 fr_randinit(&inst->rand_pool, 1);
138 inst->rand_pool.randcnt = 0;
140 inst->xlat_name = cf_section_name2(cs);
141 if (!inst->xlat_name) inst->xlat_name = "EAP";
143 /* Load all the configured EAP-Types */
145 for(scs = cf_subsection_find_next(cs, NULL, NULL);
147 scs = cf_subsection_find_next(cs, scs, NULL)) {
151 name = cf_section_name1(scs);
154 if (!strcmp(name, TLS_CONFIG_SECTION)) continue;
156 method = eap_name2type(name);
157 if (method == PW_EAP_INVALID) {
158 radlog(L_ERR, "rlm_eap: Unknown EAP method %s",
165 if ((method < PW_EAP_MD5) && (method > PW_EAP_MAX_TYPES)) {
166 radlog(L_ERR, "rlm_eap: EAP method %s outside of "
167 "valid range", name);
174 #ifndef HAVE_OPENSSL_SSL_H
176 * This allows the default configuration to be
177 * shipped with EAP-TLS, etc. enabled. If the
178 * system doesn't have OpenSSL, they will be
181 * If the system does have OpenSSL, then this
182 * code will not be used. The administrator will
183 * then have to delete the tls,
184 * etc. configurations from eap.conf in order to
185 * have EAP without the TLS types.
187 if ((method == PW_EAP_TLS) ||
188 (method == PW_EAP_TTLS) ||
189 (method == PW_EAP_PEAP)) {
190 DEBUG2("Ignoring EAP method %s because we do not have "
191 "OpenSSL support", name);
199 if (eap_module_load(&inst->methods[method], method, scs) < 0) {
200 talloc_steal(inst, inst->methods[method]);
206 talloc_steal(inst, inst->methods[method]);
207 num_methods++; /* successfully loaded one more methods */
210 if (num_methods == 0) {
211 radlog(L_ERR, "rlm_eap: No EAP method configured, module "
212 "cannot do anything.");
220 * Ensure that the default EAP type is loaded.
222 method = eap_name2type(inst->default_method_name);
223 if (method == PW_EAP_INVALID) {
224 radlog(L_ERR, "rlm_eap: Unknown default EAP method %s",
225 inst->default_method_name);
230 if (inst->methods[method] == NULL) {
231 radlog(L_ERR, "rlm_eap: No such sub-type for default EAP "
232 "method %s", inst->default_method_name);
237 inst->default_method = method; /* save the numerical method */
240 * List of sessions are set to NULL by the memset
245 * Lookup sessions in the tree. We don't free them in
246 * the tree, as that's taken care of elsewhere...
248 inst->session_tree = rbtree_create(eap_handler_cmp, NULL, 0);
249 if (!inst->session_tree) {
250 radlog(L_ERR, "rlm_eap: Cannot initialize tree");
256 inst->handler_tree = rbtree_create(eap_handler_ptr_cmp, NULL, 0);
257 if (!inst->handler_tree) {
258 radlog(L_ERR, "rlm_eap: Cannot initialize tree");
263 #ifdef HAVE_PTHREAD_H
264 if (pthread_mutex_init(&(inst->handler_mutex), NULL) < 0) {
265 radlog(L_ERR, "rlm_eap: Failed initializing mutex: %s", strerror(errno));
272 #ifdef HAVE_PTHREAD_H
273 if (pthread_mutex_init(&(inst->session_mutex), NULL) < 0) {
274 radlog(L_ERR, "rlm_eap: Failed initializing mutex: %s", strerror(errno));
285 * For backwards compatibility.
287 static rlm_rcode_t eap_authenticate(void *instance, REQUEST *request)
290 eap_handler_t *handler;
291 eap_packet_raw_t *eap_packet;
295 inst = (rlm_eap_t *) instance;
297 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
298 RDEBUGE("You set 'Auth-Type = EAP' for a request that does "
299 "not contain an EAP-Message attribute!");
300 return RLM_MODULE_INVALID;
304 * Get the eap packet to start with
306 eap_packet = eap_vp2packet(request->packet->vps);
307 if (eap_packet == NULL) {
308 radlog_request(L_ERR, 0, request, "Malformed EAP Message");
309 return RLM_MODULE_FAIL;
313 * Create the eap handler. The eap_packet will end up being
314 * "swallowed" into the handler, so we can't access it after
317 handler = eap_handler(inst, &eap_packet, request);
318 if (handler == NULL) {
319 RDEBUG2("Failed in handler");
320 return RLM_MODULE_INVALID;
324 * Select the appropriate method or default to the
327 status = eap_method_select(inst, handler);
332 if (status == EAP_INVALID) {
334 eap_handler_free(inst, handler);
335 RDEBUG2("Failed in EAP select");
336 return RLM_MODULE_INVALID;
341 * If we're doing horrible tunneling work, remember it.
343 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
344 RDEBUG2(" Not-EAP proxy set. Not composing EAP");
346 * Add the handle to the proxied list, so that we
347 * can retrieve it in the post-proxy stage, and
350 handler->inst_holder = inst;
351 status = request_data_add(request,
352 inst, REQUEST_DATA_eap_handler_t,
353 handler, (void *) eap_opaque_free);
354 rad_assert(status == 0);
356 return RLM_MODULE_HANDLED;
362 * Maybe the request was marked to be proxied. If so,
365 if (request->proxy != NULL) {
366 VALUE_PAIR *vp = NULL;
368 rad_assert(request->proxy_reply == NULL);
371 * Add the handle to the proxied list, so that we
372 * can retrieve it in the post-proxy stage, and
375 handler->inst_holder = inst;
376 status = request_data_add(request,
377 inst, REQUEST_DATA_eap_handler_t,
379 (void *) eap_opaque_free);
380 rad_assert(status == 0);
383 * Some simple sanity checks. These should really
384 * be handled by the radius library...
386 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
388 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
390 vp = pairmake("Message-Authenticator",
392 rad_assert(vp != NULL);
393 pairadd(&(request->proxy->vps), vp);
398 * Delete the "proxied to" attribute, as it's
399 * set to 127.0.0.1 for tunneled requests, and
400 * we don't want to tell the world that...
402 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS, TAG_ANY);
404 RDEBUG2(" Tunneled session will be proxied. Not doing EAP.");
405 return RLM_MODULE_HANDLED;
410 * We are done, wrap the EAP-request in RADIUS to send
411 * with all other required radius attributes
413 rcode = eap_compose(handler);
416 * Add to the list only if it is EAP-Request, OR if
417 * it's LEAP, and a response.
419 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
420 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) ||
423 * LEAP is a little different. At Stage 4,
424 * it sends an EAP-Success message, but we still
425 * need to keep the State attribute & session
426 * data structure around for the AP Challenge.
428 * At stage 6, LEAP sends an EAP-Response, which
429 * isn't put into the list.
431 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
432 (handler->eap_ds->response->type.num == PW_EAP_LEAP) &&
433 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
434 (handler->eap_ds->request->type.num == 0))) {
437 * Return FAIL if we can't remember the handler.
438 * This is actually disallowed by the
439 * specification, as unexpected FAILs could have
440 * been forged. However, we want to signal to
441 * everyone else involved that we are
442 * intentionally failing the session, as opposed
443 * to accidentally failing it.
445 if (!eaplist_add(inst, handler)) {
446 RDEBUG("Failed adding handler to the list");
448 eap_handler_free(inst, handler);
449 return RLM_MODULE_FAIL;
453 RDEBUG2("Freeing handler");
454 /* handler is not required any more, free it now */
455 eap_handler_free(inst, handler);
459 * If it's an Access-Accept, RFC 2869, Section 2.3.1
460 * says that we MUST include a User-Name attribute in the
463 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
468 * Doesn't exist, add it in.
470 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
472 vp = pairmake("User-Name", "",
474 strlcpy(vp->vp_strvalue, request->username->vp_strvalue,
475 sizeof(vp->vp_strvalue));
476 vp->length = request->username->length;
477 rad_assert(vp != NULL);
478 pairadd(&(request->reply->vps), vp);
482 * Cisco AP1230 has a bug and needs a zero
483 * terminated string in Access-Accept.
485 if ((inst->cisco_accounting_username_bug) &&
486 (vp->length < (int) sizeof(vp->vp_strvalue))) {
487 vp->vp_strvalue[vp->length] = '\0';
496 * EAP authorization DEPENDS on other rlm authorizations,
497 * to check for user existance & get their configured values.
498 * It Handles EAP-START Messages, User-Name initilization.
500 static rlm_rcode_t eap_authorize(void *instance, REQUEST *request)
506 inst = (rlm_eap_t *)instance;
510 * We don't do authorization again, once we've seen the
511 * proxy reply (or the proxied packet)
513 if (request->proxy != NULL)
514 return RLM_MODULE_NOOP;
518 * For EAP_START, send Access-Challenge with EAP Identity
519 * request. even when we have to proxy this request
521 * RFC 2869, Section 2.3.1 notes that the "domain" of the
522 * user, (i.e. where to proxy him) comes from the EAP-Identity,
523 * so we CANNOT proxy the user, until we know his identity.
525 * We therefore send an EAP Identity request.
527 status = eap_start(inst, request);
530 return RLM_MODULE_NOOP;
532 return RLM_MODULE_FAIL;
534 return RLM_MODULE_HANDLED;
542 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
543 * it MUST copy the identity into the User-Name attribute.
545 * But we don't worry about that too much. We depend on
546 * each EAP sub-module to look for handler->request->username,
547 * and to get excited if it doesn't appear.
549 vp = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
550 if ((!vp) || (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
551 vp = pairmake("Auth-Type", inst->xlat_name, T_OP_EQ);
553 RDEBUG2("Failed to create Auth-Type %s: %s\n",
554 inst->xlat_name, fr_strerror());
555 return RLM_MODULE_FAIL;
557 pairadd(&request->config_items, vp);
559 RDEBUG2W("Auth-Type already set. Not setting to EAP");
562 if (status == EAP_OK) return RLM_MODULE_OK;
564 return RLM_MODULE_UPDATED;
570 * If we're proxying EAP, then there may be magic we need
573 static rlm_rcode_t eap_post_proxy(void *inst, REQUEST *request)
578 eap_handler_t *handler;
581 * Just in case the admin lists EAP in post-proxy-type Fail.
583 if (!request->proxy_reply) return RLM_MODULE_NOOP;
586 * If there was a handler associated with this request,
587 * then it's a tunneled request which was proxied...
589 handler = request_data_get(request, inst, REQUEST_DATA_eap_handler_t);
590 if (handler != NULL) {
592 eap_tunnel_data_t *data;
595 * Grab the tunnel callbacks from the request.
597 data = (eap_tunnel_data_t *) request_data_get(request,
599 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
601 radlog_request(L_ERR, 0, request, "Failed to retrieve callback for tunneled session!");
602 eap_handler_free(inst, handler);
603 return RLM_MODULE_FAIL;
609 RDEBUG2("Doing post-proxy callback");
610 rcode = data->callback(handler, data->tls_session);
613 RDEBUG2("Failed in post-proxy callback");
615 eap_handler_free(inst, handler);
616 return RLM_MODULE_REJECT;
620 * We are done, wrap the EAP-request in RADIUS to send
621 * with all other required radius attributes
623 eap_compose(handler);
626 * Add to the list only if it is EAP-Request, OR if
627 * it's LEAP, and a response.
629 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
630 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) {
631 if (!eaplist_add(inst, handler)) {
633 eap_handler_free(inst, handler);
634 return RLM_MODULE_FAIL;
637 } else { /* couldn't have been LEAP, there's no tunnel */
638 RDEBUG2("Freeing handler");
639 /* handler is not required any more, free it now */
640 eap_handler_free(inst, handler);
644 * If it's an Access-Accept, RFC 2869, Section 2.3.1
645 * says that we MUST include a User-Name attribute in the
648 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
651 * Doesn't exist, add it in.
653 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
655 vp = pairmake("User-Name", request->username->vp_strvalue,
657 rad_assert(vp != NULL);
658 pairadd(&(request->reply->vps), vp);
662 return RLM_MODULE_OK;
664 RDEBUG2("No pre-existing handler found");
668 * There may be more than one Cisco-AVPair.
669 * Ensure we find the one with the LEAP attribute.
671 vp = request->proxy_reply->vps;
674 * Hmm... there's got to be a better way to
675 * discover codes for vendor attributes.
677 * This is vendor Cisco (9), Cisco-AVPair
680 vp = pairfind(vp, 1, 9, TAG_ANY);
682 return RLM_MODULE_NOOP;
686 * If it's "leap:session-key", then stop.
688 * The format is VERY specific!
690 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
695 * Not this AV-pair. Go to the next one.
701 * The format is very specific.
703 if (vp->length != 17 + 34) {
704 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
705 vp->length, 17 + 34);
706 return RLM_MODULE_NOOP;
710 * Decrypt the session key, using the proxy data.
712 i = 34; /* starts off with 34 octets */
713 len = rad_tunnel_pwdecode(vp->vp_octets + 17, &i,
714 request->home_server->secret,
715 request->proxy->vector);
718 * FIXME: Assert that i == 16.
722 * Encrypt the session key again, using the request data.
724 rad_tunnel_pwencode(vp->vp_strvalue + 17, &len,
725 request->client->secret,
726 request->packet->vector);
728 return RLM_MODULE_UPDATED;
732 static rlm_rcode_t eap_post_auth(void *instance, REQUEST *request)
734 rlm_eap_t *inst = instance;
736 eap_handler_t *handler;
737 eap_packet_raw_t *eap_packet;
740 * Only build a failure message if something previously rejected the request
742 vp = pairfind(request->config_items, PW_POSTAUTHTYPE, 0, TAG_ANY);
744 if (!vp || (vp->vp_integer != PW_POSTAUTHTYPE_REJECT)) return RLM_MODULE_NOOP;
746 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
747 RDEBUG2("Request didn't contain an EAP-Message, not inserting EAP-Failure");
748 return RLM_MODULE_NOOP;
751 if (pairfind(request->reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
752 RDEBUG2("Reply already contained an EAP-Message, not inserting EAP-Failure");
753 return RLM_MODULE_NOOP;
756 eap_packet = eap_vp2packet(request->packet->vps);
757 if (eap_packet == NULL) {
758 radlog_request(L_ERR, 0, request, "Malformed EAP Message");
759 return RLM_MODULE_FAIL;
762 handler = eap_handler(inst, &eap_packet, request);
763 if (handler == NULL) {
764 RDEBUG2("Failed to get handler, probably already removed, not inserting EAP-Failure");
765 return RLM_MODULE_NOOP;
768 RDEBUG2("Request was previously rejected, inserting EAP-Failure");
770 eap_handler_free(inst, handler);
773 * Make sure there's a message authenticator attribute in the response
774 * RADIUS protocol code will calculate the correct value later...
776 vp = pairfind(request->reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
778 vp = pairmake("Message-Authenticator",
780 rad_assert(vp != NULL);
781 pairadd(&(request->reply->vps), vp);
784 return RLM_MODULE_UPDATED;
788 * The module name should be the only globally exported symbol.
789 * That is, everything else should be 'static'.
794 RLM_TYPE_CHECK_CONFIG_SAFE, /* type */
795 eap_instantiate, /* instantiation */
796 eap_detach, /* detach */
798 eap_authenticate, /* authentication */
799 eap_authorize, /* authorization */
800 NULL, /* preaccounting */
801 NULL, /* accounting */
802 NULL, /* checksimul */
803 NULL, /* pre-proxy */
805 eap_post_proxy, /* post-proxy */
809 eap_post_auth /* post-auth */