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", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_eap_t, default_method_name), "md5" },
34 { "timer_expire", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_eap_t, timer_limit), "60" },
35 { "ignore_unknown_eap_types", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_eap_t, ignore_unknown_types), "no" },
36 { "mod_accounting_username_bug", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_eap_t, mod_accounting_username_bug), "no" },
37 { "max_sessions", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_eap_t, max_sessions), "2048" },
39 { NULL, -1, 0, NULL, NULL } /* end the list */
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));
53 if (inst->handler_tree) pthread_mutex_destroy(&(inst->handler_mutex));
56 rbtree_free(inst->session_tree);
57 if (inst->handler_tree) {
58 rbtree_free(inst->handler_tree);
60 * Must be NULL else when nodes are freed they try to
61 * delete themselves from the tree.
63 inst->handler_tree = NULL;
65 inst->session_tree = NULL;
73 * Compare two handlers.
75 static int eap_handler_cmp(void const *a, void const *b)
78 eap_handler_t const *one = a;
79 eap_handler_t const *two = b;
81 if (one->eap_id < two->eap_id) return -1;
82 if (one->eap_id > two->eap_id) return +1;
84 rcode = memcmp(one->state, two->state, sizeof(one->state));
85 if (rcode != 0) return rcode;
88 * As of 2.1.8, we don't key off of source IP. This
89 * a NAS to send packets load-balanced (or fail-over)
90 * across multiple intermediate proxies, and still have
93 if (fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr) != 0) {
94 WARN("EAP packets are arriving from two different upstream "
95 "servers. Has there been a proxy fail-over?");
103 * Compare two handler pointers
105 static int eap_handler_ptr_cmp(void const *a, void const *b)
107 if (a < b) return -1;
108 if (a > b) return +1;
114 * read the config section and load all the eap authentication types present.
116 static int mod_instantiate(CONF_SECTION *cs, void *instance)
122 rlm_eap_t *inst = instance;
125 * Create our own random pool.
127 for (i = 0; i < 256; i++) {
128 inst->rand_pool.randrsl[i] = fr_rand();
130 fr_randinit(&inst->rand_pool, 1);
131 inst->rand_pool.randcnt = 0;
133 inst->xlat_name = cf_section_name2(cs);
134 if (!inst->xlat_name) inst->xlat_name = "EAP";
136 /* Load all the configured EAP-Types */
138 for(scs = cf_subsection_find_next(cs, NULL, NULL);
140 scs = cf_subsection_find_next(cs, scs, NULL)) {
144 name = cf_section_name1(scs);
147 if (!strcmp(name, TLS_CONFIG_SECTION)) continue;
149 method = eap_name2type(name);
150 if (method == PW_EAP_INVALID) {
151 cf_log_err_cs(cs, "No dictionary definition for EAP method %s", name);
155 if ((method < PW_EAP_MD5) || (method >= PW_EAP_MAX_TYPES)) {
156 cf_log_err_cs(cs, "Invalid EAP method %s (unsupported)", name);
160 #if !defined(HAVE_OPENSSL_SSL_H) || !defined(HAVE_LIBSSL)
162 * This allows the default configuration to be
163 * shipped with EAP-TLS, etc. enabled. If the
164 * system doesn't have OpenSSL, they will be
167 * If the system does have OpenSSL, then this
168 * code will not be used. The administrator will
169 * then have to delete the tls,
170 * etc. configurations from eap.conf in order to
171 * have EAP without the TLS types.
173 if ((method == PW_EAP_TLS) ||
174 (method == PW_EAP_TTLS) ||
175 (method == PW_EAP_PEAP)) {
176 DEBUG2("rlm_eap (%s): Ignoring EAP method %s because we do not have OpenSSL support",
177 inst->xlat_name, name);
185 ret = eap_module_load(inst, &inst->methods[method], method, scs);
187 (void) talloc_get_type_abort(inst->methods[method], eap_module_t);
190 (void) talloc_steal(inst, inst->methods[method]);
194 (void) talloc_steal(inst, inst->methods[method]);
195 num_methods++; /* successfully loaded one more methods */
198 if (num_methods == 0) {
199 cf_log_err_cs(cs, "No EAP method configured, module cannot do anything");
204 * Ensure that the default EAP type is loaded.
206 method = eap_name2type(inst->default_method_name);
207 if (method == PW_EAP_INVALID) {
208 cf_log_err_cs(cs, "No dictionary definition for default EAP method '%s'",
209 inst->default_method_name);
213 if (!inst->methods[method]) {
214 cf_log_err_cs(cs, "No such sub-type for default EAP method %s",
215 inst->default_method_name);
218 inst->default_method = method; /* save the numerical method */
221 * List of sessions are set to NULL by the memset
226 * Lookup sessions in the tree. We don't free them in
227 * the tree, as that's taken care of elsewhere...
229 inst->session_tree = rbtree_create(inst, eap_handler_cmp, NULL, 0);
230 if (!inst->session_tree) {
231 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
236 inst->handler_tree = rbtree_create(inst, eap_handler_ptr_cmp, NULL, 0);
237 if (!inst->handler_tree) {
238 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
242 #ifdef HAVE_PTHREAD_H
243 if (pthread_mutex_init(&(inst->handler_mutex), NULL) < 0) {
244 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, fr_syserror(errno));
250 #ifdef HAVE_PTHREAD_H
251 if (pthread_mutex_init(&(inst->session_mutex), NULL) < 0) {
252 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, fr_syserror(errno));
262 * For backwards compatibility.
264 static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
267 eap_handler_t *handler;
268 eap_packet_raw_t *eap_packet;
272 inst = (rlm_eap_t *) instance;
274 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
275 REDEBUG("You set 'Auth-Type = EAP' for a request that does "
276 "not contain an EAP-Message attribute!");
277 return RLM_MODULE_INVALID;
281 * Get the eap packet to start with
283 eap_packet = eap_vp2packet(request, request->packet->vps);
285 RERROR("Malformed EAP Message");
286 return RLM_MODULE_FAIL;
290 * Create the eap handler. The eap_packet will end up being
291 * "swallowed" into the handler, so we can't access it after
294 handler = eap_handler(inst, &eap_packet, request);
296 RDEBUG2("Failed in handler");
297 return RLM_MODULE_INVALID;
301 * Select the appropriate method or default to the
304 status = eap_method_select(inst, handler);
309 if (status == EAP_INVALID) {
311 talloc_free(handler);
312 RDEBUG2("Failed in EAP select");
313 return RLM_MODULE_INVALID;
318 * If we're doing horrible tunneling work, remember it.
320 if ((request->log.lvl & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
321 RDEBUG2("No EAP proxy set. Not composing EAP");
323 * Add the handle to the proxied list, so that we
324 * can retrieve it in the post-proxy stage, and
327 handler->inst_holder = inst;
328 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
330 rad_assert(status == 0);
331 return RLM_MODULE_HANDLED;
337 * Maybe the request was marked to be proxied. If so,
340 if (request->proxy != NULL) {
341 VALUE_PAIR *vp = NULL;
343 rad_assert(!request->proxy_reply);
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;
352 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
354 rad_assert(status == 0);
357 * Some simple sanity checks. These should really
358 * be handled by the radius library...
360 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
362 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
364 pairmake(request->proxy,
365 &request->proxy->vps,
366 "Message-Authenticator",
372 * Delete the "proxied to" attribute, as it's
373 * set to 127.0.0.1 for tunneled requests, and
374 * we don't want to tell the world that...
376 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS, TAG_ANY);
378 RDEBUG2("Tunneled session will be proxied. Not doing EAP");
379 return RLM_MODULE_HANDLED;
384 * We are done, wrap the EAP-request in RADIUS to send
385 * with all other required radius attributes
387 rcode = eap_compose(handler);
390 * Add to the list only if it is EAP-Request, OR if
391 * it's LEAP, and a response.
393 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
394 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) ||
397 * LEAP is a little different. At Stage 4,
398 * it sends an EAP-Success message, but we still
399 * need to keep the State attribute & session
400 * data structure around for the AP Challenge.
402 * At stage 6, LEAP sends an EAP-Response, which
403 * isn't put into the list.
405 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
406 (handler->eap_ds->response->type.num == PW_EAP_LEAP) &&
407 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
408 (handler->eap_ds->request->type.num == 0))) {
411 * Return FAIL if we can't remember the handler.
412 * This is actually disallowed by the
413 * specification, as unexpected FAILs could have
414 * been forged. However, we want to signal to
415 * everyone else involved that we are
416 * intentionally failing the session, as opposed
417 * to accidentally failing it.
419 if (!eaplist_add(inst, handler)) {
420 RDEBUG("Failed adding handler to the list");
422 talloc_free(handler);
423 return RLM_MODULE_FAIL;
427 RDEBUG2("Freeing handler");
428 /* handler is not required any more, free it now */
429 talloc_free(handler);
433 * If it's an Access-Accept, RFC 2869, Section 2.3.1
434 * says that we MUST include a User-Name attribute in the
437 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
442 * Doesn't exist, add it in.
444 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
446 vp = paircopyvp(request->reply, request->username);
447 pairadd(&request->reply->vps, vp);
451 * Cisco AP1230 has a bug and needs a zero
452 * terminated string in Access-Accept.
454 if (inst->mod_accounting_username_bug) {
455 char const *old = vp->vp_strvalue;
456 char *new = talloc_zero_array(vp, char, vp->length + 1);
458 memcpy(new, old, vp->length);
459 vp->vp_strvalue = new;
470 * EAP authorization DEPENDS on other rlm authorizations,
471 * to check for user existence & get their configured values.
472 * It Handles EAP-START Messages, User-Name initilization.
474 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
480 inst = (rlm_eap_t *)instance;
484 * We don't do authorization again, once we've seen the
485 * proxy reply (or the proxied packet)
487 if (request->proxy != NULL)
488 return RLM_MODULE_NOOP;
492 * For EAP_START, send Access-Challenge with EAP Identity
493 * request. even when we have to proxy this request
495 * RFC 2869, Section 2.3.1 notes that the "domain" of the
496 * user, (i.e. where to proxy him) comes from the EAP-Identity,
497 * so we CANNOT proxy the user, until we know his identity.
499 * We therefore send an EAP Identity request.
501 status = eap_start(inst, request);
504 return RLM_MODULE_NOOP;
506 return RLM_MODULE_FAIL;
508 return RLM_MODULE_HANDLED;
516 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
517 * it MUST copy the identity into the User-Name attribute.
519 * But we don't worry about that too much. We depend on
520 * each EAP sub-module to look for handler->request->username,
521 * and to get excited if it doesn't appear.
523 vp = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
524 if ((!vp) || (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
525 vp = pairmake_config("Auth-Type", inst->xlat_name, T_OP_EQ);
527 RDEBUG2("Failed to create Auth-Type %s: %s\n",
528 inst->xlat_name, fr_strerror());
529 return RLM_MODULE_FAIL;
532 RWDEBUG2("Auth-Type already set. Not setting to EAP");
535 if (status == EAP_OK) return RLM_MODULE_OK;
537 return RLM_MODULE_UPDATED;
543 * If we're proxying EAP, then there may be magic we need
546 static rlm_rcode_t CC_HINT(nonnull) mod_post_proxy(void *inst, REQUEST *request)
552 eap_handler_t *handler;
556 * If there was a handler associated with this request,
557 * then it's a tunneled request which was proxied...
559 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
560 if (handler != NULL) {
562 eap_tunnel_data_t *data;
565 * Grab the tunnel callbacks from the request.
567 data = (eap_tunnel_data_t *) request_data_get(request,
569 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
571 RERROR("Failed to retrieve callback for tunneled session!");
572 talloc_free(handler);
573 return RLM_MODULE_FAIL;
579 RDEBUG2("Doing post-proxy callback");
580 rcode = data->callback(handler, data->tls_session);
583 RDEBUG2("Failed in post-proxy callback");
585 talloc_free(handler);
586 return RLM_MODULE_REJECT;
590 * We are done, wrap the EAP-request in RADIUS to send
591 * with all other required radius attributes
593 eap_compose(handler);
596 * Add to the list only if it is EAP-Request, OR if
597 * it's LEAP, and a response.
599 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
600 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) {
601 if (!eaplist_add(inst, handler)) {
603 talloc_free(handler);
604 return RLM_MODULE_FAIL;
607 } else { /* couldn't have been LEAP, there's no tunnel */
608 RDEBUG2("Freeing handler");
609 /* handler is not required any more, free it now */
610 talloc_free(handler);
614 * If it's an Access-Accept, RFC 2869, Section 2.3.1
615 * says that we MUST include a User-Name attribute in the
618 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
621 * Doesn't exist, add it in.
623 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
625 pairmake_reply("User-Name",
626 request->username->vp_strvalue,
631 return RLM_MODULE_OK;
633 RDEBUG2("No pre-existing handler found");
639 if (!request->proxy_reply) return RLM_MODULE_NOOP;
642 * There may be more than one Cisco-AVPair.
643 * Ensure we find the one with the LEAP attribute.
645 fr_cursor_init(&cursor, &request->proxy_reply->vps);
648 * Hmm... there's got to be a better way to
649 * discover codes for vendor attributes.
651 * This is vendor Cisco (9), Cisco-AVPair
654 vp = fr_cursor_next_by_num(&cursor, 1, 9, TAG_ANY);
656 return RLM_MODULE_NOOP;
660 * If it's "leap:session-key", then stop.
662 * The format is VERY specific!
664 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
670 * The format is very specific.
672 if (vp->length != (17 + 34)) {
673 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %zu: Expected %d",
674 vp->length, 17 + 34);
675 return RLM_MODULE_NOOP;
679 * Decrypt the session key, using the proxy data.
681 * Note that the session key is *binary*, and therefore
682 * may contain embedded zeros. So we have to use memdup.
685 p = talloc_memdup(vp, vp->vp_octets, vp->length);
686 talloc_set_type(p, uint8_t);
687 len = rad_tunnel_pwdecode((uint8_t *)p + 17, &i, request->home_server->secret, request->proxy->vector);
690 * FIXME: Assert that i == 16.
694 * Encrypt the session key again, using the request data.
696 rad_tunnel_pwencode(p + 17, &len,
697 request->client->secret,
698 request->packet->vector);
701 return RLM_MODULE_UPDATED;
705 static rlm_rcode_t CC_HINT(nonnull) mod_post_auth(void *instance, REQUEST *request)
707 rlm_eap_t *inst = instance;
709 eap_handler_t *handler;
710 eap_packet_raw_t *eap_packet;
713 * Only build a failure message if something previously rejected the request
715 vp = pairfind(request->config_items, PW_POSTAUTHTYPE, 0, TAG_ANY);
717 if (!vp || (vp->vp_integer != PW_POSTAUTHTYPE_REJECT)) return RLM_MODULE_NOOP;
719 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
720 RDEBUG2("Request didn't contain an EAP-Message, not inserting EAP-Failure");
721 return RLM_MODULE_NOOP;
724 if (pairfind(request->reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
725 RDEBUG2("Reply already contained an EAP-Message, not inserting EAP-Failure");
726 return RLM_MODULE_NOOP;
729 eap_packet = eap_vp2packet(request, request->packet->vps);
731 RERROR("Malformed EAP Message");
732 return RLM_MODULE_FAIL;
735 handler = eap_handler(inst, &eap_packet, request);
737 RDEBUG2("Failed to get handler, probably already removed, not inserting EAP-Failure");
738 return RLM_MODULE_NOOP;
741 RDEBUG2("Request was previously rejected, inserting EAP-Failure");
743 talloc_free(handler);
746 * Make sure there's a message authenticator attribute in the response
747 * RADIUS protocol code will calculate the correct value later...
749 vp = pairfind(request->reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
751 pairmake_reply("Message-Authenticator", "0x00", T_OP_EQ);
754 return RLM_MODULE_UPDATED;
758 * The module name should be the only globally exported symbol.
759 * That is, everything else should be 'static'.
767 mod_instantiate, /* instantiation */
768 mod_detach, /* detach */
770 mod_authenticate, /* authentication */
771 mod_authorize, /* authorization */
772 NULL, /* preaccounting */
773 NULL, /* accounting */
774 NULL, /* checksimul */
775 NULL, /* pre-proxy */
777 mod_post_proxy, /* post-proxy */
781 mod_post_auth /* post-auth */