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 { "cisco_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" },
39 CONF_PARSER_TERMINATOR
43 * delete all the allocated space by eap module
45 static int mod_detach(void *instance)
49 inst = (rlm_eap_t *)instance;
52 pthread_mutex_destroy(&(inst->session_mutex));
55 rbtree_free(inst->session_tree);
56 inst->session_tree = NULL;
64 * Compare two handlers.
66 static int eap_handler_cmp(void const *a, void const *b)
69 eap_handler_t const *one = a;
70 eap_handler_t const *two = b;
72 if (one->eap_id < two->eap_id) return -1;
73 if (one->eap_id > two->eap_id) return +1;
75 rcode = memcmp(one->state, two->state, sizeof(one->state));
76 if (rcode != 0) return rcode;
79 * As of 2.1.8, we don't key off of source IP. This
80 * a NAS to send packets load-balanced (or fail-over)
81 * across multiple intermediate proxies, and still have
84 if (fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr) != 0) {
85 WARN("EAP packets are arriving from two different upstream "
86 "servers. Has there been a proxy fail-over?");
94 * read the config section and load all the eap authentication types present.
96 static int mod_instantiate(CONF_SECTION *cs, void *instance)
102 rlm_eap_t *inst = instance;
105 * Create our own random pool.
107 for (i = 0; i < 256; i++) {
108 inst->rand_pool.randrsl[i] = fr_rand();
110 fr_randinit(&inst->rand_pool, 1);
111 inst->rand_pool.randcnt = 0;
113 inst->xlat_name = cf_section_name2(cs);
114 if (!inst->xlat_name) inst->xlat_name = "EAP";
116 /* Load all the configured EAP-Types */
118 for(scs = cf_subsection_find_next(cs, NULL, NULL);
120 scs = cf_subsection_find_next(cs, scs, NULL)) {
123 name = cf_section_name1(scs);
126 if (!strcmp(name, TLS_CONFIG_SECTION)) continue;
129 * Easier sometimes than commenting out blocks,
130 * or deleting blocks.
132 if (!strcmp(name, "disable")) continue;
134 method = eap_name2type(name);
135 if (method == PW_EAP_INVALID) {
136 cf_log_err_cs(cs, "No dictionary definition for EAP method %s", name);
140 if ((method < PW_EAP_MD5) || (method >= PW_EAP_MAX_TYPES)) {
141 cf_log_err_cs(cs, "Invalid EAP method %s (unsupported)", name);
145 #if !defined(HAVE_OPENSSL_SSL_H) || !defined(HAVE_LIBSSL)
147 * This allows the default configuration to be
148 * shipped with EAP-TLS, etc. enabled. If the
149 * system doesn't have OpenSSL, they will be
152 * If the system does have OpenSSL, then this
153 * code will not be used. The administrator will
154 * then have to delete the tls,
155 * etc. configurations from eap.conf in order to
156 * have EAP without the TLS types.
163 WARN("rlm_eap (%s): Ignoring EAP method %s because we don't have OpenSSL support",
164 inst->xlat_name, name);
175 ret = eap_module_instantiate(inst, &inst->methods[method], method, scs);
177 (void) talloc_get_type_abort(inst->methods[method], eap_module_t);
180 (void) talloc_steal(inst, inst->methods[method]);
184 (void) talloc_steal(inst, inst->methods[method]);
185 num_methods++; /* successfully loaded one more methods */
188 if (num_methods == 0) {
189 cf_log_err_cs(cs, "No EAP method configured, module cannot do anything");
194 * Ensure that the default EAP type is loaded.
196 method = eap_name2type(inst->default_method_name);
197 if (method == PW_EAP_INVALID) {
198 cf_log_err_cs(cs, "No dictionary definition for default EAP method '%s'",
199 inst->default_method_name);
203 if (!inst->methods[method]) {
204 cf_log_err_cs(cs, "No such sub-type for default EAP method %s",
205 inst->default_method_name);
208 inst->default_method = method; /* save the numerical method */
211 * List of sessions are set to NULL by the memset
216 * Lookup sessions in the tree. We don't free them in
217 * the tree, as that's taken care of elsewhere...
219 inst->session_tree = rbtree_create(NULL, eap_handler_cmp, NULL, 0);
220 if (!inst->session_tree) {
221 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
224 fr_link_talloc_ctx_free(inst, inst->session_tree);
226 #ifdef HAVE_PTHREAD_H
227 if (pthread_mutex_init(&(inst->session_mutex), NULL) < 0) {
228 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, fr_syserror(errno));
238 * For backwards compatibility.
240 static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
243 eap_handler_t *handler;
244 eap_packet_raw_t *eap_packet;
248 inst = (rlm_eap_t *) instance;
250 if (!fr_pair_find_by_num(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
251 REDEBUG("You set 'Auth-Type = EAP' for a request that does "
252 "not contain an EAP-Message attribute!");
253 return RLM_MODULE_INVALID;
257 * Get the eap packet to start with
259 eap_packet = eap_vp2packet(request, request->packet->vps);
261 RERROR("Malformed EAP Message: %s", fr_strerror());
262 return RLM_MODULE_FAIL;
266 * Create the eap handler. The eap_packet will end up being
267 * "swallowed" into the handler, so we can't access it after
270 handler = eap_handler(inst, &eap_packet, request);
272 RDEBUG2("Failed in handler");
273 return RLM_MODULE_INVALID;
277 * Select the appropriate method or default to the
280 status = eap_method_select(inst, handler);
285 if (status == EAP_INVALID) {
287 talloc_free(handler);
288 RDEBUG2("Failed in EAP select");
289 return RLM_MODULE_INVALID;
294 * If we're doing horrible tunneling work, remember it.
296 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
297 RDEBUG2("No EAP proxy set. Not composing EAP");
299 * Add the handle to the proxied list, so that we
300 * can retrieve it in the post-proxy stage, and
303 handler->inst_holder = inst;
304 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
306 rad_assert(status == 0);
307 return RLM_MODULE_HANDLED;
313 * Maybe the request was marked to be proxied. If so,
316 if (request->proxy != NULL) {
317 VALUE_PAIR *vp = NULL;
319 rad_assert(!request->proxy_reply);
322 * Add the handle to the proxied list, so that we
323 * can retrieve it in the post-proxy stage, and
326 handler->inst_holder = inst;
328 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
330 rad_assert(status == 0);
333 * Some simple sanity checks. These should really
334 * be handled by the radius library...
336 vp = fr_pair_find_by_num(request->proxy->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
338 vp = fr_pair_find_by_num(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
340 fr_pair_make(request->proxy,
341 &request->proxy->vps,
342 "Message-Authenticator",
348 * Delete the "proxied to" attribute, as it's
349 * set to 127.0.0.1 for tunneled requests, and
350 * we don't want to tell the world that...
352 fr_pair_delete_by_num(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS, TAG_ANY);
354 RDEBUG2("Tunneled session will be proxied. Not doing EAP");
355 return RLM_MODULE_HANDLED;
360 * We are done, wrap the EAP-request in RADIUS to send
361 * with all other required radius attributes
363 rcode = eap_compose(handler);
366 * Add to the list only if it is EAP-Request, OR if
367 * it's LEAP, and a response.
369 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
370 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) ||
373 * LEAP is a little different. At Stage 4,
374 * it sends an EAP-Success message, but we still
375 * need to keep the State attribute & session
376 * data structure around for the AP Challenge.
378 * At stage 6, LEAP sends an EAP-Response, which
379 * isn't put into the list.
381 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
382 (handler->eap_ds->response->type.num == PW_EAP_LEAP) &&
383 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
384 (handler->eap_ds->request->type.num == 0))) {
387 * Return FAIL if we can't remember the handler.
388 * This is actually disallowed by the
389 * specification, as unexpected FAILs could have
390 * been forged. However, we want to signal to
391 * everyone else involved that we are
392 * intentionally failing the session, as opposed
393 * to accidentally failing it.
395 if (!eaplist_add(inst, handler)) {
396 RDEBUG("Failed adding handler to the list");
398 talloc_free(handler);
399 return RLM_MODULE_FAIL;
403 RDEBUG2("Freeing handler");
404 /* handler is not required any more, free it now */
405 talloc_free(handler);
409 * If it's an Access-Accept, RFC 2869, Section 2.3.1
410 * says that we MUST include a User-Name attribute in the
413 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
418 * Doesn't exist, add it in.
420 vp = fr_pair_find_by_num(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
422 vp = fr_pair_copy(request->reply, request->username);
423 fr_pair_add(&request->reply->vps, vp);
427 * Cisco AP1230 has a bug and needs a zero
428 * terminated string in Access-Accept.
430 if (inst->mod_accounting_username_bug) {
431 char const *old = vp->vp_strvalue;
432 char *new = talloc_zero_array(vp, char, vp->vp_length + 1);
434 memcpy(new, old, vp->vp_length);
435 vp->vp_strvalue = new;
446 * EAP authorization DEPENDS on other rlm authorizations,
447 * to check for user existence & get their configured values.
448 * It Handles EAP-START Messages, User-Name initilization.
450 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
456 inst = (rlm_eap_t *)instance;
460 * We don't do authorization again, once we've seen the
461 * proxy reply (or the proxied packet)
463 if (request->proxy != NULL)
464 return RLM_MODULE_NOOP;
468 * For EAP_START, send Access-Challenge with EAP Identity
469 * request. even when we have to proxy this request
471 * RFC 2869, Section 2.3.1 notes that the "domain" of the
472 * user, (i.e. where to proxy him) comes from the EAP-Identity,
473 * so we CANNOT proxy the user, until we know his identity.
475 * We therefore send an EAP Identity request.
477 status = eap_start(inst, request);
480 return RLM_MODULE_NOOP;
482 return RLM_MODULE_FAIL;
484 return RLM_MODULE_HANDLED;
492 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
493 * it MUST copy the identity into the User-Name attribute.
495 * But we don't worry about that too much. We depend on
496 * each EAP sub-module to look for handler->request->username,
497 * and to get excited if it doesn't appear.
499 vp = fr_pair_find_by_num(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
500 if ((!vp) || (vp->vp_integer != PW_AUTH_TYPE_REJECT)) {
501 vp = pair_make_config("Auth-Type", inst->xlat_name, T_OP_EQ);
503 RDEBUG2("Failed to create Auth-Type %s: %s\n",
504 inst->xlat_name, fr_strerror());
505 return RLM_MODULE_FAIL;
508 RWDEBUG2("Auth-Type already set. Not setting to EAP");
511 if (status == EAP_OK) return RLM_MODULE_OK;
513 return RLM_MODULE_UPDATED;
519 * If we're proxying EAP, then there may be magic we need
522 static rlm_rcode_t CC_HINT(nonnull) mod_post_proxy(void *inst, REQUEST *request)
529 eap_handler_t *handler;
533 * If there was a handler associated with this request,
534 * then it's a tunneled request which was proxied...
536 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
537 if (handler != NULL) {
539 eap_tunnel_data_t *data;
542 * Grab the tunnel callbacks from the request.
544 data = (eap_tunnel_data_t *) request_data_get(request,
546 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
548 RERROR("Failed to retrieve callback for tunneled session!");
549 talloc_free(handler);
550 return RLM_MODULE_FAIL;
556 RDEBUG2("Doing post-proxy callback");
557 rcode = data->callback(handler, data->tls_session);
560 RDEBUG2("Failed in post-proxy callback");
562 talloc_free(handler);
563 return RLM_MODULE_REJECT;
567 * We are done, wrap the EAP-request in RADIUS to send
568 * with all other required radius attributes
570 eap_compose(handler);
573 * Add to the list only if it is EAP-Request, OR if
574 * it's LEAP, and a response.
576 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
577 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) {
578 if (!eaplist_add(inst, handler)) {
580 talloc_free(handler);
581 return RLM_MODULE_FAIL;
584 } else { /* couldn't have been LEAP, there's no tunnel */
585 RDEBUG2("Freeing handler");
586 /* handler is not required any more, free it now */
587 talloc_free(handler);
591 * If it's an Access-Accept, RFC 2869, Section 2.3.1
592 * says that we MUST include a User-Name attribute in the
595 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
598 * Doesn't exist, add it in.
600 vp = fr_pair_find_by_num(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
602 pair_make_reply("User-Name",
603 request->username->vp_strvalue,
608 return RLM_MODULE_OK;
610 RDEBUG2("No pre-existing handler found");
616 if (!request->proxy_reply) return RLM_MODULE_NOOP;
619 * There may be more than one Cisco-AVPair.
620 * Ensure we find the one with the LEAP attribute.
622 fr_cursor_init(&cursor, &request->proxy_reply->vps);
625 * Hmm... there's got to be a better way to
626 * discover codes for vendor attributes.
628 * This is vendor Cisco (9), Cisco-AVPair
631 vp = fr_cursor_next_by_num(&cursor, 1, 9, TAG_ANY);
633 return RLM_MODULE_NOOP;
637 * If it's "leap:session-key", then stop.
639 * The format is VERY specific!
641 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
647 * The format is very specific.
649 if (vp->vp_length != (17 + 34)) {
650 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %zu: Expected %d",
651 vp->vp_length, 17 + 34);
652 return RLM_MODULE_NOOP;
656 * Decrypt the session key, using the proxy data.
658 * Note that the session key is *binary*, and therefore
659 * may contain embedded zeros. So we have to use memdup.
660 * However, Cisco-AVPair is a "string", so the rest of the
661 * code assumes that it's terminated by a trailing '\0'.
663 * So... be sure to (a) use memdup, and (b) include the last
667 p = talloc_memdup(vp, vp->vp_strvalue, vp->vp_length + 1);
668 talloc_set_type(p, uint8_t);
669 ret = rad_tunnel_pwdecode((uint8_t *)p + 17, &i, request->home_server->secret, request->proxy->vector);
671 REDEBUG("Decoding leap:session-key failed");
673 return RLM_MODULE_FAIL;
678 REDEBUG("Decoded key length is incorrect, must be 16 bytes");
680 return RLM_MODULE_FAIL;
684 * Encrypt the session key again, using the request data.
686 ret = rad_tunnel_pwencode(p + 17, &len, request->client->secret, request->packet->vector);
688 REDEBUG("Decoding leap:session-key failed");
690 return RLM_MODULE_FAIL;
693 fr_pair_value_strsteal(vp, p);
695 return RLM_MODULE_UPDATED;
699 static rlm_rcode_t CC_HINT(nonnull) mod_post_auth(void *instance, REQUEST *request)
701 rlm_eap_t *inst = instance;
703 eap_handler_t *handler;
704 eap_packet_raw_t *eap_packet;
707 * Only build a failure message if something previously rejected the request
709 vp = fr_pair_find_by_num(request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
711 if (!vp || (vp->vp_integer != PW_POST_AUTH_TYPE_REJECT)) return RLM_MODULE_NOOP;
713 if (!fr_pair_find_by_num(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
714 RDEBUG3("Request didn't contain an EAP-Message, not inserting EAP-Failure");
715 return RLM_MODULE_NOOP;
718 if (fr_pair_find_by_num(request->reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
719 RDEBUG3("Reply already contained an EAP-Message, not inserting EAP-Failure");
720 return RLM_MODULE_NOOP;
723 eap_packet = eap_vp2packet(request, request->packet->vps);
725 RERROR("Malformed EAP Message: %s", fr_strerror());
726 return RLM_MODULE_FAIL;
729 handler = eap_handler(inst, &eap_packet, request);
731 RDEBUG2("Failed to get handler, probably already removed, not inserting EAP-Failure");
732 return RLM_MODULE_NOOP;
735 RDEBUG2("Request was previously rejected, inserting EAP-Failure");
737 talloc_free(handler);
740 * Make sure there's a message authenticator attribute in the response
741 * RADIUS protocol code will calculate the correct value later...
743 vp = fr_pair_find_by_num(request->reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
745 pair_make_reply("Message-Authenticator", "0x00", T_OP_EQ);
748 return RLM_MODULE_UPDATED;
752 * The module name should be the only globally exported symbol.
753 * That is, everything else should be 'static'.
755 extern module_t rlm_eap;
757 .magic = RLM_MODULE_INIT,
759 .inst_size = sizeof(rlm_eap_t),
760 .config = module_config,
761 .instantiate = mod_instantiate,
762 .detach = mod_detach,
764 [MOD_AUTHENTICATE] = mod_authenticate,
765 [MOD_AUTHORIZE] = mod_authorize,
767 [MOD_POST_PROXY] = mod_post_proxy,
769 [MOD_POST_AUTH] = mod_post_auth