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>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
32 static const CONF_PARSER module_config[] = {
33 { "default_eap_type", PW_TYPE_STRING_PTR,
34 offsetof(rlm_eap_t, default_method_name), NULL, "md5" },
35 { "timer_expire", PW_TYPE_INTEGER,
36 offsetof(rlm_eap_t, timer_limit), NULL, "60"},
37 { "ignore_unknown_eap_types", PW_TYPE_BOOLEAN,
38 offsetof(rlm_eap_t, ignore_unknown_types), NULL, "no" },
39 { "mod_accounting_username_bug", PW_TYPE_BOOLEAN,
40 offsetof(rlm_eap_t, mod_accounting_username_bug), NULL, "no" },
41 { "max_sessions", PW_TYPE_INTEGER,
42 offsetof(rlm_eap_t, max_sessions), NULL, "2048"},
44 { NULL, -1, 0, NULL, NULL } /* end the list */
48 * delete all the allocated space by eap module
50 static int mod_detach(void *instance)
54 inst = (rlm_eap_t *)instance;
57 pthread_mutex_destroy(&(inst->session_mutex));
58 if (inst->handler_tree) pthread_mutex_destroy(&(inst->handler_mutex));
61 rbtree_free(inst->session_tree);
62 if (inst->handler_tree) {
63 rbtree_free(inst->handler_tree);
65 * Must be NULL else when nodes are freed they try to
66 * delete themselves from the tree.
68 inst->handler_tree = NULL;
70 inst->session_tree = NULL;
78 * Compare two handlers.
80 static int eap_handler_cmp(void const *a, void const *b)
83 eap_handler_t const *one = a;
84 eap_handler_t const *two = b;
86 if (one->eap_id < two->eap_id) return -1;
87 if (one->eap_id > two->eap_id) return +1;
89 rcode = memcmp(one->state, two->state, sizeof(one->state));
90 if (rcode != 0) return rcode;
93 * As of 2.1.8, we don't key off of source IP. This
94 * a NAS to send packets load-balanced (or fail-over)
95 * across multiple intermediate proxies, and still have
98 if (fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr) != 0) {
99 WARN("EAP packets are arriving from two different upstream "
100 "servers. Has there been a proxy fail-over?");
108 * Compare two handler pointers
110 static int eap_handler_ptr_cmp(void const *a, void const *b)
112 if (a < b) return -1;
113 if (a > b) return +1;
119 * read the config section and load all the eap authentication types present.
121 static int mod_instantiate(CONF_SECTION *cs, void *instance)
127 rlm_eap_t *inst = instance;
130 * Create our own random pool.
132 for (i = 0; i < 256; i++) {
133 inst->rand_pool.randrsl[i] = fr_rand();
135 fr_randinit(&inst->rand_pool, 1);
136 inst->rand_pool.randcnt = 0;
138 inst->xlat_name = cf_section_name2(cs);
139 if (!inst->xlat_name) inst->xlat_name = "EAP";
141 /* Load all the configured EAP-Types */
143 for(scs = cf_subsection_find_next(cs, NULL, NULL);
145 scs = cf_subsection_find_next(cs, scs, NULL)) {
149 name = cf_section_name1(scs);
152 if (!strcmp(name, TLS_CONFIG_SECTION)) continue;
154 method = eap_name2type(name);
155 if (method == PW_EAP_INVALID) {
156 cf_log_err_cs(cs, "No dictionary definition for EAP method %s", name);
160 if ((method < PW_EAP_MD5) || (method >= PW_EAP_MAX_TYPES)) {
161 cf_log_err_cs(cs, "Invalid EAP method %s (unsupported)", name);
165 #if !defined(HAVE_OPENSSL_SSL_H) || !defined(HAVE_LIBSSL)
167 * This allows the default configuration to be
168 * shipped with EAP-TLS, etc. enabled. If the
169 * system doesn't have OpenSSL, they will be
172 * If the system does have OpenSSL, then this
173 * code will not be used. The administrator will
174 * then have to delete the tls,
175 * etc. configurations from eap.conf in order to
176 * have EAP without the TLS types.
178 if ((method == PW_EAP_TLS) ||
179 (method == PW_EAP_TTLS) ||
180 (method == PW_EAP_PEAP)) {
181 DEBUG2("rlm_eap (%s): Ignoring EAP method %s because we do not have OpenSSL support",
182 inst->xlat_name, name);
190 ret = eap_module_load(inst, &inst->methods[method], method, scs);
192 (void) talloc_get_type_abort(inst->methods[method], eap_module_t);
195 (void) talloc_steal(inst, inst->methods[method]);
199 (void) talloc_steal(inst, inst->methods[method]);
200 num_methods++; /* successfully loaded one more methods */
203 if (num_methods == 0) {
204 cf_log_err_cs(cs, "No EAP method configured, module cannot do anything");
209 * Ensure that the default EAP type is loaded.
211 method = eap_name2type(inst->default_method_name);
212 if (method == PW_EAP_INVALID) {
213 cf_log_err_cs(cs, "No dictionary definition for default EAP method '%s'",
214 inst->default_method_name);
218 if (!inst->methods[method]) {
219 cf_log_err_cs(cs, "No such sub-type for default EAP method %s",
220 inst->default_method_name);
223 inst->default_method = method; /* save the numerical method */
226 * List of sessions are set to NULL by the memset
231 * Lookup sessions in the tree. We don't free them in
232 * the tree, as that's taken care of elsewhere...
234 inst->session_tree = rbtree_create(eap_handler_cmp, NULL, 0);
235 if (!inst->session_tree) {
236 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
241 inst->handler_tree = rbtree_create(eap_handler_ptr_cmp, NULL, 0);
242 if (!inst->handler_tree) {
243 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
247 #ifdef HAVE_PTHREAD_H
248 if (pthread_mutex_init(&(inst->handler_mutex), NULL) < 0) {
249 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, fr_syserror(errno));
255 #ifdef HAVE_PTHREAD_H
256 if (pthread_mutex_init(&(inst->session_mutex), NULL) < 0) {
257 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, fr_syserror(errno));
267 * For backwards compatibility.
269 static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
272 eap_handler_t *handler;
273 eap_packet_raw_t *eap_packet;
277 inst = (rlm_eap_t *) instance;
279 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
280 REDEBUG("You set 'Auth-Type = EAP' for a request that does "
281 "not contain an EAP-Message attribute!");
282 return RLM_MODULE_INVALID;
286 * Get the eap packet to start with
288 eap_packet = eap_vp2packet(request, request->packet->vps);
290 RERROR("Malformed EAP Message");
291 return RLM_MODULE_FAIL;
295 * Create the eap handler. The eap_packet will end up being
296 * "swallowed" into the handler, so we can't access it after
299 handler = eap_handler(inst, &eap_packet, request);
301 RDEBUG2("Failed in handler");
302 return RLM_MODULE_INVALID;
306 * Select the appropriate method or default to the
309 status = eap_method_select(inst, handler);
314 if (status == EAP_INVALID) {
316 talloc_free(handler);
317 RDEBUG2("Failed in EAP select");
318 return RLM_MODULE_INVALID;
323 * If we're doing horrible tunneling work, remember it.
325 if ((request->log.lvl & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
326 RDEBUG2("No EAP proxy set. Not composing EAP");
328 * Add the handle to the proxied list, so that we
329 * can retrieve it in the post-proxy stage, and
332 handler->inst_holder = inst;
333 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
335 rad_assert(status == 0);
336 return RLM_MODULE_HANDLED;
342 * Maybe the request was marked to be proxied. If so,
345 if (request->proxy != NULL) {
346 VALUE_PAIR *vp = NULL;
348 rad_assert(!request->proxy_reply);
351 * Add the handle to the proxied list, so that we
352 * can retrieve it in the post-proxy stage, and
355 handler->inst_holder = inst;
357 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
359 rad_assert(status == 0);
362 * Some simple sanity checks. These should really
363 * be handled by the radius library...
365 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
367 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
369 pairmake(request->proxy,
370 &request->proxy->vps,
371 "Message-Authenticator",
377 * Delete the "proxied to" attribute, as it's
378 * set to 127.0.0.1 for tunneled requests, and
379 * we don't want to tell the world that...
381 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS, TAG_ANY);
383 RDEBUG2("Tunneled session will be proxied. Not doing EAP");
384 return RLM_MODULE_HANDLED;
389 * We are done, wrap the EAP-request in RADIUS to send
390 * with all other required radius attributes
392 rcode = eap_compose(handler);
395 * Add to the list only if it is EAP-Request, OR if
396 * it's LEAP, and a response.
398 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
399 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) ||
402 * LEAP is a little different. At Stage 4,
403 * it sends an EAP-Success message, but we still
404 * need to keep the State attribute & session
405 * data structure around for the AP Challenge.
407 * At stage 6, LEAP sends an EAP-Response, which
408 * isn't put into the list.
410 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
411 (handler->eap_ds->response->type.num == PW_EAP_LEAP) &&
412 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
413 (handler->eap_ds->request->type.num == 0))) {
416 * Return FAIL if we can't remember the handler.
417 * This is actually disallowed by the
418 * specification, as unexpected FAILs could have
419 * been forged. However, we want to signal to
420 * everyone else involved that we are
421 * intentionally failing the session, as opposed
422 * to accidentally failing it.
424 if (!eaplist_add(inst, handler)) {
425 RDEBUG("Failed adding handler to the list");
427 talloc_free(handler);
428 return RLM_MODULE_FAIL;
432 RDEBUG2("Freeing handler");
433 /* handler is not required any more, free it now */
434 talloc_free(handler);
438 * If it's an Access-Accept, RFC 2869, Section 2.3.1
439 * says that we MUST include a User-Name attribute in the
442 if ((request->reply->code == PW_CODE_AUTHENTICATION_ACK) &&
447 * Doesn't exist, add it in.
449 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
451 vp = paircopyvp(request->reply, request->username);
452 pairadd(&request->reply->vps, vp);
456 * Cisco AP1230 has a bug and needs a zero
457 * terminated string in Access-Accept.
459 if (inst->mod_accounting_username_bug) {
460 char const *old = vp->vp_strvalue;
461 char *new = talloc_zero_array(vp, char, vp->length + 1);
463 memcpy(new, old, vp->length);
464 vp->vp_strvalue = new;
475 * EAP authorization DEPENDS on other rlm authorizations,
476 * to check for user existence & get their configured values.
477 * It Handles EAP-START Messages, User-Name initilization.
479 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
485 inst = (rlm_eap_t *)instance;
489 * We don't do authorization again, once we've seen the
490 * proxy reply (or the proxied packet)
492 if (request->proxy != NULL)
493 return RLM_MODULE_NOOP;
497 * For EAP_START, send Access-Challenge with EAP Identity
498 * request. even when we have to proxy this request
500 * RFC 2869, Section 2.3.1 notes that the "domain" of the
501 * user, (i.e. where to proxy him) comes from the EAP-Identity,
502 * so we CANNOT proxy the user, until we know his identity.
504 * We therefore send an EAP Identity request.
506 status = eap_start(inst, request);
509 return RLM_MODULE_NOOP;
511 return RLM_MODULE_FAIL;
513 return RLM_MODULE_HANDLED;
521 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
522 * it MUST copy the identity into the User-Name attribute.
524 * But we don't worry about that too much. We depend on
525 * each EAP sub-module to look for handler->request->username,
526 * and to get excited if it doesn't appear.
528 vp = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
529 if ((!vp) || (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
530 vp = pairmake_config("Auth-Type", inst->xlat_name, T_OP_EQ);
532 RDEBUG2("Failed to create Auth-Type %s: %s\n",
533 inst->xlat_name, fr_strerror());
534 return RLM_MODULE_FAIL;
537 RWDEBUG2("Auth-Type already set. Not setting to EAP");
540 if (status == EAP_OK) return RLM_MODULE_OK;
542 return RLM_MODULE_UPDATED;
548 * If we're proxying EAP, then there may be magic we need
551 static rlm_rcode_t CC_HINT(nonnull) mod_post_proxy(void *inst, REQUEST *request)
557 eap_handler_t *handler;
561 * Just in case the admin lists EAP in post-proxy-type Fail.
563 if (!request->proxy_reply) return RLM_MODULE_NOOP;
566 * If there was a handler associated with this request,
567 * then it's a tunneled request which was proxied...
569 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
570 if (handler != NULL) {
572 eap_tunnel_data_t *data;
575 * Grab the tunnel callbacks from the request.
577 data = (eap_tunnel_data_t *) request_data_get(request,
579 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
581 RERROR("Failed to retrieve callback for tunneled session!");
582 talloc_free(handler);
583 return RLM_MODULE_FAIL;
589 RDEBUG2("Doing post-proxy callback");
590 rcode = data->callback(handler, data->tls_session);
593 RDEBUG2("Failed in post-proxy callback");
595 talloc_free(handler);
596 return RLM_MODULE_REJECT;
600 * We are done, wrap the EAP-request in RADIUS to send
601 * with all other required radius attributes
603 eap_compose(handler);
606 * Add to the list only if it is EAP-Request, OR if
607 * it's LEAP, and a response.
609 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
610 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) {
611 if (!eaplist_add(inst, handler)) {
613 talloc_free(handler);
614 return RLM_MODULE_FAIL;
617 } else { /* couldn't have been LEAP, there's no tunnel */
618 RDEBUG2("Freeing handler");
619 /* handler is not required any more, free it now */
620 talloc_free(handler);
624 * If it's an Access-Accept, RFC 2869, Section 2.3.1
625 * says that we MUST include a User-Name attribute in the
628 if ((request->reply->code == PW_CODE_AUTHENTICATION_ACK) &&
631 * Doesn't exist, add it in.
633 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
635 pairmake_reply("User-Name",
636 request->username->vp_strvalue,
641 return RLM_MODULE_OK;
643 RDEBUG2("No pre-existing handler found");
649 if (!request->proxy_reply) return RLM_MODULE_NOOP;
652 * There may be more than one Cisco-AVPair.
653 * Ensure we find the one with the LEAP attribute.
655 fr_cursor_init(&cursor, &request->proxy_reply->vps);
658 * Hmm... there's got to be a better way to
659 * discover codes for vendor attributes.
661 * This is vendor Cisco (9), Cisco-AVPair
664 vp = fr_cursor_next_by_num(&cursor, 1, 9, TAG_ANY);
666 return RLM_MODULE_NOOP;
670 * If it's "leap:session-key", then stop.
672 * The format is VERY specific!
674 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
680 * The format is very specific.
682 if (vp->length != (17 + 34)) {
683 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %zu: Expected %d",
684 vp->length, 17 + 34);
685 return RLM_MODULE_NOOP;
689 * Decrypt the session key, using the proxy data.
691 * Note that the session key is *binary*, and therefore
692 * may contain embedded zeros. So we have to use memdup.
695 p = talloc_memdup(vp, vp->vp_octets, vp->length);
696 talloc_set_type(p, uint8_t);
697 len = rad_tunnel_pwdecode((uint8_t *)p + 17, &i, request->home_server->secret, request->proxy->vector);
700 * FIXME: Assert that i == 16.
704 * Encrypt the session key again, using the request data.
706 rad_tunnel_pwencode(p + 17, &len,
707 request->client->secret,
708 request->packet->vector);
711 return RLM_MODULE_UPDATED;
715 static rlm_rcode_t CC_HINT(nonnull) mod_post_auth(void *instance, REQUEST *request)
717 rlm_eap_t *inst = instance;
719 eap_handler_t *handler;
720 eap_packet_raw_t *eap_packet;
723 * Only build a failure message if something previously rejected the request
725 vp = pairfind(request->config_items, PW_POSTAUTHTYPE, 0, TAG_ANY);
727 if (!vp || (vp->vp_integer != PW_POSTAUTHTYPE_REJECT)) return RLM_MODULE_NOOP;
729 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
730 RDEBUG2("Request didn't contain an EAP-Message, not inserting EAP-Failure");
731 return RLM_MODULE_NOOP;
734 if (pairfind(request->reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
735 RDEBUG2("Reply already contained an EAP-Message, not inserting EAP-Failure");
736 return RLM_MODULE_NOOP;
739 eap_packet = eap_vp2packet(request, request->packet->vps);
741 RERROR("Malformed EAP Message");
742 return RLM_MODULE_FAIL;
745 handler = eap_handler(inst, &eap_packet, request);
747 RDEBUG2("Failed to get handler, probably already removed, not inserting EAP-Failure");
748 return RLM_MODULE_NOOP;
751 RDEBUG2("Request was previously rejected, inserting EAP-Failure");
753 talloc_free(handler);
756 * Make sure there's a message authenticator attribute in the response
757 * RADIUS protocol code will calculate the correct value later...
759 vp = pairfind(request->reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
761 pairmake_reply("Message-Authenticator", "0x00", T_OP_EQ);
764 return RLM_MODULE_UPDATED;
768 * The module name should be the only globally exported symbol.
769 * That is, everything else should be 'static'.
777 mod_instantiate, /* instantiation */
778 mod_detach, /* detach */
780 mod_authenticate, /* authentication */
781 mod_authorize, /* authorization */
782 NULL, /* preaccounting */
783 NULL, /* accounting */
784 NULL, /* checksimul */
785 NULL, /* pre-proxy */
787 mod_post_proxy, /* post-proxy */
791 mod_post_auth /* post-auth */