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 { "mod_accounting_username_bug", PW_TYPE_BOOLEAN,
41 offsetof(rlm_eap_t, mod_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 mod_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 mod_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) {
131 * Create our own random pool.
133 for (i = 0; i < 256; i++) {
134 inst->rand_pool.randrsl[i] = fr_rand();
136 fr_randinit(&inst->rand_pool, 1);
137 inst->rand_pool.randcnt = 0;
139 inst->xlat_name = cf_section_name2(cs);
140 if (!inst->xlat_name) inst->xlat_name = "EAP";
142 /* Load all the configured EAP-Types */
144 for(scs = cf_subsection_find_next(cs, NULL, NULL);
146 scs = cf_subsection_find_next(cs, scs, NULL)) {
150 name = cf_section_name1(scs);
153 if (!strcmp(name, TLS_CONFIG_SECTION)) continue;
155 method = eap_name2type(name);
156 if (method == PW_EAP_INVALID) {
157 cf_log_err_cs(cs, "Unknown EAP method %s", name);
161 if ((method < PW_EAP_MD5) || (method >= PW_EAP_MAX_TYPES)) {
162 cf_log_err_cs(cs, "Invalid EAP method %s (unsupported)", name);
166 #if !defined(HAVE_OPENSSL_SSL_H) || !defined(HAVE_LIBSSL)
168 * This allows the default configuration to be
169 * shipped with EAP-TLS, etc. enabled. If the
170 * system doesn't have OpenSSL, they will be
173 * If the system does have OpenSSL, then this
174 * code will not be used. The administrator will
175 * then have to delete the tls,
176 * etc. configurations from eap.conf in order to
177 * have EAP without the TLS types.
179 if ((method == PW_EAP_TLS) ||
180 (method == PW_EAP_TTLS) ||
181 (method == PW_EAP_PEAP)) {
182 DEBUG2("rlm_eap (%s): Ignoring EAP method %s because we do not have OpenSSL support",
183 inst->xlat_name, name);
191 ret = eap_module_load(inst, &inst->methods[method], method, scs);
193 (void) talloc_get_type_abort(inst->methods[method], eap_module_t);
196 talloc_steal(inst, inst->methods[method]);
200 talloc_steal(inst, inst->methods[method]);
201 num_methods++; /* successfully loaded one more methods */
204 if (num_methods == 0) {
205 cf_log_err_cs(cs, "No EAP method configured, module cannot do anything");
210 * Ensure that the default EAP type is loaded.
212 method = eap_name2type(inst->default_method_name);
213 if (method == PW_EAP_INVALID) {
214 cf_log_err_cs(cs, "Unknown default EAP method '%s'",
215 inst->default_method_name);
219 if (!inst->methods[method]) {
220 cf_log_err_cs(cs, "No such sub-type for default EAP method %s",
221 inst->default_method_name);
224 inst->default_method = method; /* save the numerical method */
227 * List of sessions are set to NULL by the memset
232 * Lookup sessions in the tree. We don't free them in
233 * the tree, as that's taken care of elsewhere...
235 inst->session_tree = rbtree_create(eap_handler_cmp, NULL, 0);
236 if (!inst->session_tree) {
237 radlog(L_ERR, "rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
242 inst->handler_tree = rbtree_create(eap_handler_ptr_cmp, NULL, 0);
243 if (!inst->handler_tree) {
244 radlog(L_ERR, "rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
248 #ifdef HAVE_PTHREAD_H
249 if (pthread_mutex_init(&(inst->handler_mutex), NULL) < 0) {
250 radlog(L_ERR, "rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, strerror(errno));
256 #ifdef HAVE_PTHREAD_H
257 if (pthread_mutex_init(&(inst->session_mutex), NULL) < 0) {
258 radlog(L_ERR, "rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, strerror(errno));
268 * For backwards compatibility.
270 static rlm_rcode_t mod_authenticate(void *instance, REQUEST *request)
273 eap_handler_t *handler;
274 eap_packet_raw_t *eap_packet;
278 inst = (rlm_eap_t *) instance;
280 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
281 RDEBUGE("You set 'Auth-Type = EAP' for a request that does "
282 "not contain an EAP-Message attribute!");
283 return RLM_MODULE_INVALID;
287 * Get the eap packet to start with
289 eap_packet = eap_vp2packet(request, request->packet->vps);
291 radlog_request(L_ERR, 0, request, "Malformed EAP Message");
292 return RLM_MODULE_FAIL;
296 * Create the eap handler. The eap_packet will end up being
297 * "swallowed" into the handler, so we can't access it after
300 handler = eap_handler(inst, &eap_packet, request);
302 RDEBUG2("Failed in handler");
303 return RLM_MODULE_INVALID;
307 * Select the appropriate method or default to the
310 status = eap_method_select(inst, handler);
315 if (status == EAP_INVALID) {
317 eap_handler_free(inst, handler);
318 RDEBUG2("Failed in EAP select");
319 return RLM_MODULE_INVALID;
324 * If we're doing horrible tunneling work, remember it.
326 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
327 RDEBUG2(" Not-EAP proxy set. Not composing EAP");
329 * Add the handle to the proxied list, so that we
330 * can retrieve it in the post-proxy stage, and
333 handler->inst_holder = inst;
334 status = request_data_add(request,
335 inst, REQUEST_DATA_eap_handler_t,
336 handler, (void *) eap_opaque_free);
337 rad_assert(status == 0);
338 return RLM_MODULE_HANDLED;
344 * Maybe the request was marked to be proxied. If so,
347 if (request->proxy != NULL) {
348 VALUE_PAIR *vp = NULL;
350 rad_assert(!request->proxy_reply);
353 * Add the handle to the proxied list, so that we
354 * can retrieve it in the post-proxy stage, and
357 handler->inst_holder = inst;
358 status = request_data_add(request,
359 inst, REQUEST_DATA_eap_handler_t,
361 (void *) eap_opaque_free);
362 rad_assert(status == 0);
365 * Some simple sanity checks. These should really
366 * be handled by the radius library...
368 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
370 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
372 pairmake(request->proxy,
373 &request->proxy->vps,
374 "Message-Authenticator",
380 * Delete the "proxied to" attribute, as it's
381 * set to 127.0.0.1 for tunneled requests, and
382 * we don't want to tell the world that...
384 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS, TAG_ANY);
386 RDEBUG2(" Tunneled session will be proxied. Not doing EAP.");
387 return RLM_MODULE_HANDLED;
392 * We are done, wrap the EAP-request in RADIUS to send
393 * with all other required radius attributes
395 rcode = eap_compose(handler);
398 * Add to the list only if it is EAP-Request, OR if
399 * it's LEAP, and a response.
401 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
402 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) ||
405 * LEAP is a little different. At Stage 4,
406 * it sends an EAP-Success message, but we still
407 * need to keep the State attribute & session
408 * data structure around for the AP Challenge.
410 * At stage 6, LEAP sends an EAP-Response, which
411 * isn't put into the list.
413 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
414 (handler->eap_ds->response->type.num == PW_EAP_LEAP) &&
415 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
416 (handler->eap_ds->request->type.num == 0))) {
419 * Return FAIL if we can't remember the handler.
420 * This is actually disallowed by the
421 * specification, as unexpected FAILs could have
422 * been forged. However, we want to signal to
423 * everyone else involved that we are
424 * intentionally failing the session, as opposed
425 * to accidentally failing it.
427 if (!eaplist_add(inst, handler)) {
428 RDEBUG("Failed adding handler to the list");
430 eap_handler_free(inst, handler);
431 return RLM_MODULE_FAIL;
435 RDEBUG2("Freeing handler");
436 /* handler is not required any more, free it now */
437 eap_handler_free(inst, handler);
441 * If it's an Access-Accept, RFC 2869, Section 2.3.1
442 * says that we MUST include a User-Name attribute in the
445 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
450 * Doesn't exist, add it in.
452 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
454 vp = pairmake_reply("User-Name", "",
456 strlcpy(vp->vp_strvalue, request->username->vp_strvalue,
457 sizeof(vp->vp_strvalue));
458 vp->length = request->username->length;
462 * Cisco AP1230 has a bug and needs a zero
463 * terminated string in Access-Accept.
465 if ((inst->mod_accounting_username_bug) &&
466 (vp->length < (int) sizeof(vp->vp_strvalue))) {
467 vp->vp_strvalue[vp->length] = '\0';
476 * EAP authorization DEPENDS on other rlm authorizations,
477 * to check for user existance & get their configured values.
478 * It Handles EAP-START Messages, User-Name initilization.
480 static rlm_rcode_t mod_authorize(void *instance, REQUEST *request)
486 inst = (rlm_eap_t *)instance;
490 * We don't do authorization again, once we've seen the
491 * proxy reply (or the proxied packet)
493 if (request->proxy != NULL)
494 return RLM_MODULE_NOOP;
498 * For EAP_START, send Access-Challenge with EAP Identity
499 * request. even when we have to proxy this request
501 * RFC 2869, Section 2.3.1 notes that the "domain" of the
502 * user, (i.e. where to proxy him) comes from the EAP-Identity,
503 * so we CANNOT proxy the user, until we know his identity.
505 * We therefore send an EAP Identity request.
507 status = eap_start(inst, request);
510 return RLM_MODULE_NOOP;
512 return RLM_MODULE_FAIL;
514 return RLM_MODULE_HANDLED;
522 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
523 * it MUST copy the identity into the User-Name attribute.
525 * But we don't worry about that too much. We depend on
526 * each EAP sub-module to look for handler->request->username,
527 * and to get excited if it doesn't appear.
529 vp = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
530 if ((!vp) || (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
531 vp = pairmake_config("Auth-Type", inst->xlat_name, T_OP_EQ);
533 RDEBUG2("Failed to create Auth-Type %s: %s\n",
534 inst->xlat_name, fr_strerror());
535 return RLM_MODULE_FAIL;
538 RDEBUG2W("Auth-Type already set. Not setting to EAP");
541 if (status == EAP_OK) return RLM_MODULE_OK;
543 return RLM_MODULE_UPDATED;
549 * If we're proxying EAP, then there may be magic we need
552 static rlm_rcode_t mod_post_proxy(void *inst, REQUEST *request)
557 eap_handler_t *handler;
560 * Just in case the admin lists EAP in post-proxy-type Fail.
562 if (!request->proxy_reply) return RLM_MODULE_NOOP;
565 * If there was a handler associated with this request,
566 * then it's a tunneled request which was proxied...
568 handler = request_data_get(request, inst, REQUEST_DATA_eap_handler_t);
569 if (handler != NULL) {
571 eap_tunnel_data_t *data;
574 * Grab the tunnel callbacks from the request.
576 data = (eap_tunnel_data_t *) request_data_get(request,
578 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
580 radlog_request(L_ERR, 0, request, "Failed to retrieve callback for tunneled session!");
581 eap_handler_free(inst, handler);
582 return RLM_MODULE_FAIL;
588 RDEBUG2("Doing post-proxy callback");
589 rcode = data->callback(handler, data->tls_session);
592 RDEBUG2("Failed in post-proxy callback");
594 eap_handler_free(inst, handler);
595 return RLM_MODULE_REJECT;
599 * We are done, wrap the EAP-request in RADIUS to send
600 * with all other required radius attributes
602 eap_compose(handler);
605 * Add to the list only if it is EAP-Request, OR if
606 * it's LEAP, and a response.
608 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
609 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) {
610 if (!eaplist_add(inst, handler)) {
612 eap_handler_free(inst, handler);
613 return RLM_MODULE_FAIL;
616 } else { /* couldn't have been LEAP, there's no tunnel */
617 RDEBUG2("Freeing handler");
618 /* handler is not required any more, free it now */
619 eap_handler_free(inst, handler);
623 * If it's an Access-Accept, RFC 2869, Section 2.3.1
624 * says that we MUST include a User-Name attribute in the
627 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
630 * Doesn't exist, add it in.
632 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
634 pairmake_reply("User-Name",
635 request->username->vp_strvalue,
640 return RLM_MODULE_OK;
642 RDEBUG2("No pre-existing handler found");
646 * There may be more than one Cisco-AVPair.
647 * Ensure we find the one with the LEAP attribute.
649 vp = request->proxy_reply->vps;
652 * Hmm... there's got to be a better way to
653 * discover codes for vendor attributes.
655 * This is vendor Cisco (9), Cisco-AVPair
658 vp = pairfind(vp, 1, 9, TAG_ANY);
660 return RLM_MODULE_NOOP;
664 * If it's "leap:session-key", then stop.
666 * The format is VERY specific!
668 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
673 * Not this AV-pair. Go to the next one.
679 * The format is very specific.
681 if (vp->length != 17 + 34) {
682 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
683 vp->length, 17 + 34);
684 return RLM_MODULE_NOOP;
688 * Decrypt the session key, using the proxy data.
690 i = 34; /* starts off with 34 octets */
691 len = rad_tunnel_pwdecode(vp->vp_octets + 17, &i,
692 request->home_server->secret,
693 request->proxy->vector);
696 * FIXME: Assert that i == 16.
700 * Encrypt the session key again, using the request data.
702 rad_tunnel_pwencode(vp->vp_strvalue + 17, &len,
703 request->client->secret,
704 request->packet->vector);
706 return RLM_MODULE_UPDATED;
710 static rlm_rcode_t mod_post_auth(void *instance, REQUEST *request)
712 rlm_eap_t *inst = instance;
714 eap_handler_t *handler;
715 eap_packet_raw_t *eap_packet;
718 * Only build a failure message if something previously rejected the request
720 vp = pairfind(request->config_items, PW_POSTAUTHTYPE, 0, TAG_ANY);
722 if (!vp || (vp->vp_integer != PW_POSTAUTHTYPE_REJECT)) return RLM_MODULE_NOOP;
724 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
725 RDEBUG2("Request didn't contain an EAP-Message, not inserting EAP-Failure");
726 return RLM_MODULE_NOOP;
729 if (pairfind(request->reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
730 RDEBUG2("Reply already contained an EAP-Message, not inserting EAP-Failure");
731 return RLM_MODULE_NOOP;
734 eap_packet = eap_vp2packet(request, request->packet->vps);
736 radlog_request(L_ERR, 0, request, "Malformed EAP Message");
737 return RLM_MODULE_FAIL;
740 handler = eap_handler(inst, &eap_packet, request);
742 RDEBUG2("Failed to get handler, probably already removed, not inserting EAP-Failure");
743 return RLM_MODULE_NOOP;
746 RDEBUG2("Request was previously rejected, inserting EAP-Failure");
748 eap_handler_free(inst, handler);
751 * Make sure there's a message authenticator attribute in the response
752 * RADIUS protocol code will calculate the correct value later...
754 vp = pairfind(request->reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
756 pairmake_reply("Message-Authenticator", "0x00", T_OP_EQ);
759 return RLM_MODULE_UPDATED;
763 * The module name should be the only globally exported symbol.
764 * That is, everything else should be 'static'.
769 RLM_TYPE_CHECK_CONFIG_SAFE, /* type */
770 mod_instantiate, /* instantiation */
771 mod_detach, /* detach */
773 mod_authenticate, /* authentication */
774 mod_authorize, /* authorization */
775 NULL, /* preaccounting */
776 NULL, /* accounting */
777 NULL, /* checksimul */
778 NULL, /* pre-proxy */
780 mod_post_proxy, /* post-proxy */
784 mod_post_auth /* post-auth */