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 as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * @brief Implements the EAP framework.
22 * @copyright 2000-2003,2006 The FreeRADIUS server project
23 * @copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
24 * @copyright 2003 Alan DeKok <aland@freeradius.org>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/modules.h>
33 static const CONF_PARSER module_config[] = {
34 { "default_eap_type", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_eap_t, default_method_name), "md5" },
35 { "timer_expire", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_eap_t, timer_limit), "60" },
36 { "ignore_unknown_eap_types", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_eap_t, ignore_unknown_types), "no" },
37 { "mod_accounting_username_bug", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_eap_t, mod_accounting_username_bug), "no" },
38 { "max_sessions", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_eap_t, max_sessions), "2048" },
40 { NULL, -1, 0, NULL, NULL } /* end the list */
44 * delete all the allocated space by eap module
46 static int mod_detach(void *instance)
50 inst = (rlm_eap_t *)instance;
53 pthread_mutex_destroy(&(inst->session_mutex));
54 if (inst->handler_tree) pthread_mutex_destroy(&(inst->handler_mutex));
57 rbtree_free(inst->session_tree);
58 if (inst->handler_tree) {
59 rbtree_free(inst->handler_tree);
61 * Must be NULL else when nodes are freed they try to
62 * delete themselves from the tree.
64 inst->handler_tree = NULL;
66 inst->session_tree = NULL;
74 * Compare two handlers.
76 static int eap_handler_cmp(void const *a, void const *b)
79 eap_handler_t const *one = a;
80 eap_handler_t const *two = b;
82 if (one->eap_id < two->eap_id) return -1;
83 if (one->eap_id > two->eap_id) return +1;
85 rcode = memcmp(one->state, two->state, sizeof(one->state));
86 if (rcode != 0) return rcode;
89 * As of 2.1.8, we don't key off of source IP. This
90 * a NAS to send packets load-balanced (or fail-over)
91 * across multiple intermediate proxies, and still have
94 if (fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr) != 0) {
95 WARN("EAP packets are arriving from two different upstream "
96 "servers. Has there been a proxy fail-over?");
104 * Compare two handler pointers
106 static int eap_handler_ptr_cmp(void const *a, void const *b)
108 if (a < b) return -1;
109 if (a > b) return +1;
115 * read the config section and load all the eap authentication types present.
117 static int mod_instantiate(CONF_SECTION *cs, void *instance)
123 rlm_eap_t *inst = instance;
126 * Create our own random pool.
128 for (i = 0; i < 256; i++) {
129 inst->rand_pool.randrsl[i] = fr_rand();
131 fr_randinit(&inst->rand_pool, 1);
132 inst->rand_pool.randcnt = 0;
134 inst->xlat_name = cf_section_name2(cs);
135 if (!inst->xlat_name) inst->xlat_name = "EAP";
137 /* Load all the configured EAP-Types */
139 for(scs = cf_subsection_find_next(cs, NULL, NULL);
141 scs = cf_subsection_find_next(cs, scs, NULL)) {
145 name = cf_section_name1(scs);
148 if (!strcmp(name, TLS_CONFIG_SECTION)) continue;
150 method = eap_name2type(name);
151 if (method == PW_EAP_INVALID) {
152 cf_log_err_cs(cs, "No dictionary definition for EAP method %s", name);
156 if ((method < PW_EAP_MD5) || (method >= PW_EAP_MAX_TYPES)) {
157 cf_log_err_cs(cs, "Invalid EAP method %s (unsupported)", name);
161 #if !defined(HAVE_OPENSSL_SSL_H) || !defined(HAVE_LIBSSL)
163 * This allows the default configuration to be
164 * shipped with EAP-TLS, etc. enabled. If the
165 * system doesn't have OpenSSL, they will be
168 * If the system does have OpenSSL, then this
169 * code will not be used. The administrator will
170 * then have to delete the tls,
171 * etc. configurations from eap.conf in order to
172 * have EAP without the TLS types.
174 if ((method == PW_EAP_TLS) ||
175 (method == PW_EAP_TTLS) ||
176 (method == PW_EAP_PEAP)) {
177 DEBUG2("rlm_eap (%s): Ignoring EAP method %s because we do not have OpenSSL support",
178 inst->xlat_name, name);
186 ret = eap_module_load(inst, &inst->methods[method], method, scs);
188 (void) talloc_get_type_abort(inst->methods[method], eap_module_t);
191 (void) talloc_steal(inst, inst->methods[method]);
195 (void) talloc_steal(inst, inst->methods[method]);
196 num_methods++; /* successfully loaded one more methods */
199 if (num_methods == 0) {
200 cf_log_err_cs(cs, "No EAP method configured, module cannot do anything");
205 * Ensure that the default EAP type is loaded.
207 method = eap_name2type(inst->default_method_name);
208 if (method == PW_EAP_INVALID) {
209 cf_log_err_cs(cs, "No dictionary definition for default EAP method '%s'",
210 inst->default_method_name);
214 if (!inst->methods[method]) {
215 cf_log_err_cs(cs, "No such sub-type for default EAP method %s",
216 inst->default_method_name);
219 inst->default_method = method; /* save the numerical method */
222 * List of sessions are set to NULL by the memset
227 * Lookup sessions in the tree. We don't free them in
228 * the tree, as that's taken care of elsewhere...
230 inst->session_tree = rbtree_create(NULL, eap_handler_cmp, NULL, 0);
231 if (!inst->session_tree) {
232 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
235 fr_link_talloc_ctx_free(inst, inst->session_tree);
238 inst->handler_tree = rbtree_create(NULL, eap_handler_ptr_cmp, NULL, 0);
239 if (!inst->handler_tree) {
240 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
243 fr_link_talloc_ctx_free(inst, inst->handler_tree);
245 #ifdef HAVE_PTHREAD_H
246 if (pthread_mutex_init(&(inst->handler_mutex), NULL) < 0) {
247 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, fr_syserror(errno));
253 #ifdef HAVE_PTHREAD_H
254 if (pthread_mutex_init(&(inst->session_mutex), NULL) < 0) {
255 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, fr_syserror(errno));
265 * For backwards compatibility.
267 static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
270 eap_handler_t *handler;
271 eap_packet_raw_t *eap_packet;
275 inst = (rlm_eap_t *) instance;
277 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
278 REDEBUG("You set 'Auth-Type = EAP' for a request that does "
279 "not contain an EAP-Message attribute!");
280 return RLM_MODULE_INVALID;
284 * Get the eap packet to start with
286 eap_packet = eap_vp2packet(request, request->packet->vps);
288 RERROR("Malformed EAP Message: %s", fr_strerror());
289 return RLM_MODULE_FAIL;
293 * Create the eap handler. The eap_packet will end up being
294 * "swallowed" into the handler, so we can't access it after
297 handler = eap_handler(inst, &eap_packet, request);
299 RDEBUG2("Failed in handler");
300 return RLM_MODULE_INVALID;
304 * Select the appropriate method or default to the
307 status = eap_method_select(inst, handler);
312 if (status == EAP_INVALID) {
314 talloc_free(handler);
315 RDEBUG2("Failed in EAP select");
316 return RLM_MODULE_INVALID;
321 * If we're doing horrible tunneling work, remember it.
323 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
324 RDEBUG2("No EAP proxy set. Not composing EAP");
326 * Add the handle to the proxied list, so that we
327 * can retrieve it in the post-proxy stage, and
330 handler->inst_holder = inst;
331 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
333 rad_assert(status == 0);
334 return RLM_MODULE_HANDLED;
340 * Maybe the request was marked to be proxied. If so,
343 if (request->proxy != NULL) {
344 VALUE_PAIR *vp = NULL;
346 rad_assert(!request->proxy_reply);
349 * Add the handle to the proxied list, so that we
350 * can retrieve it in the post-proxy stage, and
353 handler->inst_holder = inst;
355 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
357 rad_assert(status == 0);
360 * Some simple sanity checks. These should really
361 * be handled by the radius library...
363 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
365 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
367 pairmake(request->proxy,
368 &request->proxy->vps,
369 "Message-Authenticator",
375 * Delete the "proxied to" attribute, as it's
376 * set to 127.0.0.1 for tunneled requests, and
377 * we don't want to tell the world that...
379 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS, TAG_ANY);
381 RDEBUG2("Tunneled session will be proxied. Not doing EAP");
382 return RLM_MODULE_HANDLED;
387 * We are done, wrap the EAP-request in RADIUS to send
388 * with all other required radius attributes
390 rcode = eap_compose(handler);
393 * Add to the list only if it is EAP-Request, OR if
394 * it's LEAP, and a response.
396 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
397 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) ||
400 * LEAP is a little different. At Stage 4,
401 * it sends an EAP-Success message, but we still
402 * need to keep the State attribute & session
403 * data structure around for the AP Challenge.
405 * At stage 6, LEAP sends an EAP-Response, which
406 * isn't put into the list.
408 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
409 (handler->eap_ds->response->type.num == PW_EAP_LEAP) &&
410 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
411 (handler->eap_ds->request->type.num == 0))) {
414 * Return FAIL if we can't remember the handler.
415 * This is actually disallowed by the
416 * specification, as unexpected FAILs could have
417 * been forged. However, we want to signal to
418 * everyone else involved that we are
419 * intentionally failing the session, as opposed
420 * to accidentally failing it.
422 if (!eaplist_add(inst, handler)) {
423 RDEBUG("Failed adding handler to the list");
425 talloc_free(handler);
426 return RLM_MODULE_FAIL;
430 RDEBUG2("Freeing handler");
431 /* handler is not required any more, free it now */
432 talloc_free(handler);
436 * If it's an Access-Accept, RFC 2869, Section 2.3.1
437 * says that we MUST include a User-Name attribute in the
440 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
445 * Doesn't exist, add it in.
447 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
449 vp = paircopyvp(request->reply, request->username);
450 pairadd(&request->reply->vps, vp);
454 * Cisco AP1230 has a bug and needs a zero
455 * terminated string in Access-Accept.
457 if (inst->mod_accounting_username_bug) {
458 char const *old = vp->vp_strvalue;
459 char *new = talloc_zero_array(vp, char, vp->vp_length + 1);
461 memcpy(new, old, vp->vp_length);
462 vp->vp_strvalue = new;
473 * EAP authorization DEPENDS on other rlm authorizations,
474 * to check for user existence & get their configured values.
475 * It Handles EAP-START Messages, User-Name initilization.
477 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
483 inst = (rlm_eap_t *)instance;
487 * We don't do authorization again, once we've seen the
488 * proxy reply (or the proxied packet)
490 if (request->proxy != NULL)
491 return RLM_MODULE_NOOP;
495 * For EAP_START, send Access-Challenge with EAP Identity
496 * request. even when we have to proxy this request
498 * RFC 2869, Section 2.3.1 notes that the "domain" of the
499 * user, (i.e. where to proxy him) comes from the EAP-Identity,
500 * so we CANNOT proxy the user, until we know his identity.
502 * We therefore send an EAP Identity request.
504 status = eap_start(inst, request);
507 return RLM_MODULE_NOOP;
509 return RLM_MODULE_FAIL;
511 return RLM_MODULE_HANDLED;
519 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
520 * it MUST copy the identity into the User-Name attribute.
522 * But we don't worry about that too much. We depend on
523 * each EAP sub-module to look for handler->request->username,
524 * and to get excited if it doesn't appear.
526 vp = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
527 if ((!vp) || (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
528 vp = pairmake_config("Auth-Type", inst->xlat_name, T_OP_EQ);
530 RDEBUG2("Failed to create Auth-Type %s: %s\n",
531 inst->xlat_name, fr_strerror());
532 return RLM_MODULE_FAIL;
535 RWDEBUG2("Auth-Type already set. Not setting to EAP");
538 if (status == EAP_OK) return RLM_MODULE_OK;
540 return RLM_MODULE_UPDATED;
546 * If we're proxying EAP, then there may be magic we need
549 static rlm_rcode_t CC_HINT(nonnull) mod_post_proxy(void *inst, REQUEST *request)
555 eap_handler_t *handler;
559 * If there was a handler associated with this request,
560 * then it's a tunneled request which was proxied...
562 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
563 if (handler != NULL) {
565 eap_tunnel_data_t *data;
568 * Grab the tunnel callbacks from the request.
570 data = (eap_tunnel_data_t *) request_data_get(request,
572 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
574 RERROR("Failed to retrieve callback for tunneled session!");
575 talloc_free(handler);
576 return RLM_MODULE_FAIL;
582 RDEBUG2("Doing post-proxy callback");
583 rcode = data->callback(handler, data->tls_session);
586 RDEBUG2("Failed in post-proxy callback");
588 talloc_free(handler);
589 return RLM_MODULE_REJECT;
593 * We are done, wrap the EAP-request in RADIUS to send
594 * with all other required radius attributes
596 eap_compose(handler);
599 * Add to the list only if it is EAP-Request, OR if
600 * it's LEAP, and a response.
602 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
603 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) {
604 if (!eaplist_add(inst, handler)) {
606 talloc_free(handler);
607 return RLM_MODULE_FAIL;
610 } else { /* couldn't have been LEAP, there's no tunnel */
611 RDEBUG2("Freeing handler");
612 /* handler is not required any more, free it now */
613 talloc_free(handler);
617 * If it's an Access-Accept, RFC 2869, Section 2.3.1
618 * says that we MUST include a User-Name attribute in the
621 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
624 * Doesn't exist, add it in.
626 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
628 pairmake_reply("User-Name",
629 request->username->vp_strvalue,
634 return RLM_MODULE_OK;
636 RDEBUG2("No pre-existing handler found");
642 if (!request->proxy_reply) return RLM_MODULE_NOOP;
645 * There may be more than one Cisco-AVPair.
646 * Ensure we find the one with the LEAP attribute.
648 fr_cursor_init(&cursor, &request->proxy_reply->vps);
651 * Hmm... there's got to be a better way to
652 * discover codes for vendor attributes.
654 * This is vendor Cisco (9), Cisco-AVPair
657 vp = fr_cursor_next_by_num(&cursor, 1, 9, TAG_ANY);
659 return RLM_MODULE_NOOP;
663 * If it's "leap:session-key", then stop.
665 * The format is VERY specific!
667 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
673 * The format is very specific.
675 if (vp->vp_length != (17 + 34)) {
676 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %zu: Expected %d",
677 vp->vp_length, 17 + 34);
678 return RLM_MODULE_NOOP;
682 * Decrypt the session key, using the proxy data.
684 * Note that the session key is *binary*, and therefore
685 * may contain embedded zeros. So we have to use memdup.
686 * However, Cisco-AVPair is a "string", so the rest of the
687 * code assumes that it's terminated by a trailing '\0'.
689 * So... be sure to (a) use memdup, and (b) include the last
693 p = talloc_memdup(vp, vp->vp_strvalue, vp->vp_length + 1);
694 talloc_set_type(p, uint8_t);
695 len = rad_tunnel_pwdecode((uint8_t *)p + 17, &i, request->home_server->secret, request->proxy->vector);
698 * FIXME: Assert that i == 16.
702 * Encrypt the session key again, using the request data.
704 rad_tunnel_pwencode(p + 17, &len,
705 request->client->secret,
706 request->packet->vector);
709 return RLM_MODULE_UPDATED;
713 static rlm_rcode_t CC_HINT(nonnull) mod_post_auth(void *instance, REQUEST *request)
715 rlm_eap_t *inst = instance;
717 eap_handler_t *handler;
718 eap_packet_raw_t *eap_packet;
721 * Only build a failure message if something previously rejected the request
723 vp = pairfind(request->config_items, PW_POSTAUTHTYPE, 0, TAG_ANY);
725 if (!vp || (vp->vp_integer != PW_POSTAUTHTYPE_REJECT)) return RLM_MODULE_NOOP;
727 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
728 RDEBUG2("Request didn't contain an EAP-Message, not inserting EAP-Failure");
729 return RLM_MODULE_NOOP;
732 if (pairfind(request->reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
733 RDEBUG2("Reply already contained an EAP-Message, not inserting EAP-Failure");
734 return RLM_MODULE_NOOP;
737 eap_packet = eap_vp2packet(request, request->packet->vps);
739 RERROR("Malformed EAP Message: %s", fr_strerror());
740 return RLM_MODULE_FAIL;
743 handler = eap_handler(inst, &eap_packet, request);
745 RDEBUG2("Failed to get handler, probably already removed, not inserting EAP-Failure");
746 return RLM_MODULE_NOOP;
749 RDEBUG2("Request was previously rejected, inserting EAP-Failure");
751 talloc_free(handler);
754 * Make sure there's a message authenticator attribute in the response
755 * RADIUS protocol code will calculate the correct value later...
757 vp = pairfind(request->reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
759 pairmake_reply("Message-Authenticator", "0x00", T_OP_EQ);
762 return RLM_MODULE_UPDATED;
766 * The module name should be the only globally exported symbol.
767 * That is, everything else should be 'static'.
769 extern module_t rlm_eap;
776 mod_instantiate, /* instantiation */
777 mod_detach, /* detach */
779 mod_authenticate, /* authentication */
780 mod_authorize, /* authorization */
781 NULL, /* preaccounting */
782 NULL, /* accounting */
783 NULL, /* checksimul */
784 NULL, /* pre-proxy */
786 mod_post_proxy, /* post-proxy */
790 mod_post_auth /* post-auth */