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" },
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));
56 rbtree_free(inst->session_tree);
57 inst->session_tree = NULL;
65 * Compare two handlers.
67 static int eap_handler_cmp(void const *a, void const *b)
70 eap_handler_t const *one = a;
71 eap_handler_t const *two = b;
73 if (one->eap_id < two->eap_id) return -1;
74 if (one->eap_id > two->eap_id) return +1;
76 rcode = memcmp(one->state, two->state, sizeof(one->state));
77 if (rcode != 0) return rcode;
80 * As of 2.1.8, we don't key off of source IP. This
81 * a NAS to send packets load-balanced (or fail-over)
82 * across multiple intermediate proxies, and still have
85 if (fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr) != 0) {
86 WARN("EAP packets are arriving from two different upstream "
87 "servers. Has there been a proxy fail-over?");
95 * read the config section and load all the eap authentication types present.
97 static int mod_instantiate(CONF_SECTION *cs, void *instance)
103 rlm_eap_t *inst = instance;
106 * Create our own random pool.
108 for (i = 0; i < 256; i++) {
109 inst->rand_pool.randrsl[i] = fr_rand();
111 fr_randinit(&inst->rand_pool, 1);
112 inst->rand_pool.randcnt = 0;
114 inst->xlat_name = cf_section_name2(cs);
115 if (!inst->xlat_name) inst->xlat_name = "EAP";
117 /* Load all the configured EAP-Types */
119 for(scs = cf_subsection_find_next(cs, NULL, NULL);
121 scs = cf_subsection_find_next(cs, scs, NULL)) {
125 name = cf_section_name1(scs);
128 if (!strcmp(name, TLS_CONFIG_SECTION)) continue;
130 method = eap_name2type(name);
131 if (method == PW_EAP_INVALID) {
132 cf_log_err_cs(cs, "No dictionary definition for EAP method %s", name);
136 if ((method < PW_EAP_MD5) || (method >= PW_EAP_MAX_TYPES)) {
137 cf_log_err_cs(cs, "Invalid EAP method %s (unsupported)", name);
141 #if !defined(HAVE_OPENSSL_SSL_H) || !defined(HAVE_LIBSSL)
143 * This allows the default configuration to be
144 * shipped with EAP-TLS, etc. enabled. If the
145 * system doesn't have OpenSSL, they will be
148 * If the system does have OpenSSL, then this
149 * code will not be used. The administrator will
150 * then have to delete the tls,
151 * etc. configurations from eap.conf in order to
152 * have EAP without the TLS types.
154 if ((method == PW_EAP_TLS) ||
155 (method == PW_EAP_TTLS) ||
156 (method == PW_EAP_PEAP)) {
157 DEBUG2("rlm_eap (%s): Ignoring EAP method %s because we do not have OpenSSL support",
158 inst->xlat_name, name);
166 ret = eap_module_instantiate(inst, &inst->methods[method], method, scs);
168 (void) talloc_get_type_abort(inst->methods[method], eap_module_t);
171 (void) talloc_steal(inst, inst->methods[method]);
175 (void) talloc_steal(inst, inst->methods[method]);
176 num_methods++; /* successfully loaded one more methods */
179 if (num_methods == 0) {
180 cf_log_err_cs(cs, "No EAP method configured, module cannot do anything");
185 * Ensure that the default EAP type is loaded.
187 method = eap_name2type(inst->default_method_name);
188 if (method == PW_EAP_INVALID) {
189 cf_log_err_cs(cs, "No dictionary definition for default EAP method '%s'",
190 inst->default_method_name);
194 if (!inst->methods[method]) {
195 cf_log_err_cs(cs, "No such sub-type for default EAP method %s",
196 inst->default_method_name);
199 inst->default_method = method; /* save the numerical method */
202 * List of sessions are set to NULL by the memset
207 * Lookup sessions in the tree. We don't free them in
208 * the tree, as that's taken care of elsewhere...
210 inst->session_tree = rbtree_create(NULL, eap_handler_cmp, NULL, 0);
211 if (!inst->session_tree) {
212 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
215 fr_link_talloc_ctx_free(inst, inst->session_tree);
217 #ifdef HAVE_PTHREAD_H
218 if (pthread_mutex_init(&(inst->session_mutex), NULL) < 0) {
219 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, fr_syserror(errno));
229 * For backwards compatibility.
231 static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
234 eap_handler_t *handler;
235 eap_packet_raw_t *eap_packet;
239 inst = (rlm_eap_t *) instance;
241 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
242 REDEBUG("You set 'Auth-Type = EAP' for a request that does "
243 "not contain an EAP-Message attribute!");
244 return RLM_MODULE_INVALID;
248 * Get the eap packet to start with
250 eap_packet = eap_vp2packet(request, request->packet->vps);
252 RERROR("Malformed EAP Message: %s", fr_strerror());
253 return RLM_MODULE_FAIL;
257 * Create the eap handler. The eap_packet will end up being
258 * "swallowed" into the handler, so we can't access it after
261 handler = eap_handler(inst, &eap_packet, request);
263 RDEBUG2("Failed in handler");
264 return RLM_MODULE_INVALID;
268 * Select the appropriate method or default to the
271 status = eap_method_select(inst, handler);
276 if (status == EAP_INVALID) {
278 talloc_free(handler);
279 RDEBUG2("Failed in EAP select");
280 return RLM_MODULE_INVALID;
285 * If we're doing horrible tunneling work, remember it.
287 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
288 RDEBUG2("No EAP proxy set. Not composing EAP");
290 * Add the handle to the proxied list, so that we
291 * can retrieve it in the post-proxy stage, and
294 handler->inst_holder = inst;
295 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
297 rad_assert(status == 0);
298 return RLM_MODULE_HANDLED;
304 * Maybe the request was marked to be proxied. If so,
307 if (request->proxy != NULL) {
308 VALUE_PAIR *vp = NULL;
310 rad_assert(!request->proxy_reply);
313 * Add the handle to the proxied list, so that we
314 * can retrieve it in the post-proxy stage, and
317 handler->inst_holder = inst;
319 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
321 rad_assert(status == 0);
324 * Some simple sanity checks. These should really
325 * be handled by the radius library...
327 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
329 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
331 pairmake(request->proxy,
332 &request->proxy->vps,
333 "Message-Authenticator",
339 * Delete the "proxied to" attribute, as it's
340 * set to 127.0.0.1 for tunneled requests, and
341 * we don't want to tell the world that...
343 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS, TAG_ANY);
345 RDEBUG2("Tunneled session will be proxied. Not doing EAP");
346 return RLM_MODULE_HANDLED;
351 * We are done, wrap the EAP-request in RADIUS to send
352 * with all other required radius attributes
354 rcode = eap_compose(handler);
357 * Add to the list only if it is EAP-Request, OR if
358 * it's LEAP, and a response.
360 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
361 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) ||
364 * LEAP is a little different. At Stage 4,
365 * it sends an EAP-Success message, but we still
366 * need to keep the State attribute & session
367 * data structure around for the AP Challenge.
369 * At stage 6, LEAP sends an EAP-Response, which
370 * isn't put into the list.
372 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
373 (handler->eap_ds->response->type.num == PW_EAP_LEAP) &&
374 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
375 (handler->eap_ds->request->type.num == 0))) {
378 * Return FAIL if we can't remember the handler.
379 * This is actually disallowed by the
380 * specification, as unexpected FAILs could have
381 * been forged. However, we want to signal to
382 * everyone else involved that we are
383 * intentionally failing the session, as opposed
384 * to accidentally failing it.
386 if (!eaplist_add(inst, handler)) {
387 RDEBUG("Failed adding handler to the list");
389 talloc_free(handler);
390 return RLM_MODULE_FAIL;
394 RDEBUG2("Freeing handler");
395 /* handler is not required any more, free it now */
396 talloc_free(handler);
400 * If it's an Access-Accept, RFC 2869, Section 2.3.1
401 * says that we MUST include a User-Name attribute in the
404 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
409 * Doesn't exist, add it in.
411 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
413 vp = paircopyvp(request->reply, request->username);
414 pairadd(&request->reply->vps, vp);
418 * Cisco AP1230 has a bug and needs a zero
419 * terminated string in Access-Accept.
421 if (inst->mod_accounting_username_bug) {
422 char const *old = vp->vp_strvalue;
423 char *new = talloc_zero_array(vp, char, vp->vp_length + 1);
425 memcpy(new, old, vp->vp_length);
426 vp->vp_strvalue = new;
437 * EAP authorization DEPENDS on other rlm authorizations,
438 * to check for user existence & get their configured values.
439 * It Handles EAP-START Messages, User-Name initilization.
441 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
447 inst = (rlm_eap_t *)instance;
451 * We don't do authorization again, once we've seen the
452 * proxy reply (or the proxied packet)
454 if (request->proxy != NULL)
455 return RLM_MODULE_NOOP;
459 * For EAP_START, send Access-Challenge with EAP Identity
460 * request. even when we have to proxy this request
462 * RFC 2869, Section 2.3.1 notes that the "domain" of the
463 * user, (i.e. where to proxy him) comes from the EAP-Identity,
464 * so we CANNOT proxy the user, until we know his identity.
466 * We therefore send an EAP Identity request.
468 status = eap_start(inst, request);
471 return RLM_MODULE_NOOP;
473 return RLM_MODULE_FAIL;
475 return RLM_MODULE_HANDLED;
483 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
484 * it MUST copy the identity into the User-Name attribute.
486 * But we don't worry about that too much. We depend on
487 * each EAP sub-module to look for handler->request->username,
488 * and to get excited if it doesn't appear.
490 vp = pairfind(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
491 if ((!vp) || (vp->vp_integer != PW_AUTH_TYPE_REJECT)) {
492 vp = pairmake_config("Auth-Type", inst->xlat_name, T_OP_EQ);
494 RDEBUG2("Failed to create Auth-Type %s: %s\n",
495 inst->xlat_name, fr_strerror());
496 return RLM_MODULE_FAIL;
499 RWDEBUG2("Auth-Type already set. Not setting to EAP");
502 if (status == EAP_OK) return RLM_MODULE_OK;
504 return RLM_MODULE_UPDATED;
510 * If we're proxying EAP, then there may be magic we need
513 static rlm_rcode_t CC_HINT(nonnull) mod_post_proxy(void *inst, REQUEST *request)
519 eap_handler_t *handler;
523 * If there was a handler associated with this request,
524 * then it's a tunneled request which was proxied...
526 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
527 if (handler != NULL) {
529 eap_tunnel_data_t *data;
532 * Grab the tunnel callbacks from the request.
534 data = (eap_tunnel_data_t *) request_data_get(request,
536 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
538 RERROR("Failed to retrieve callback for tunneled session!");
539 talloc_free(handler);
540 return RLM_MODULE_FAIL;
546 RDEBUG2("Doing post-proxy callback");
547 rcode = data->callback(handler, data->tls_session);
550 RDEBUG2("Failed in post-proxy callback");
552 talloc_free(handler);
553 return RLM_MODULE_REJECT;
557 * We are done, wrap the EAP-request in RADIUS to send
558 * with all other required radius attributes
560 eap_compose(handler);
563 * Add to the list only if it is EAP-Request, OR if
564 * it's LEAP, and a response.
566 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
567 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) {
568 if (!eaplist_add(inst, handler)) {
570 talloc_free(handler);
571 return RLM_MODULE_FAIL;
574 } else { /* couldn't have been LEAP, there's no tunnel */
575 RDEBUG2("Freeing handler");
576 /* handler is not required any more, free it now */
577 talloc_free(handler);
581 * If it's an Access-Accept, RFC 2869, Section 2.3.1
582 * says that we MUST include a User-Name attribute in the
585 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
588 * Doesn't exist, add it in.
590 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
592 pairmake_reply("User-Name",
593 request->username->vp_strvalue,
598 return RLM_MODULE_OK;
600 RDEBUG2("No pre-existing handler found");
606 if (!request->proxy_reply) return RLM_MODULE_NOOP;
609 * There may be more than one Cisco-AVPair.
610 * Ensure we find the one with the LEAP attribute.
612 fr_cursor_init(&cursor, &request->proxy_reply->vps);
615 * Hmm... there's got to be a better way to
616 * discover codes for vendor attributes.
618 * This is vendor Cisco (9), Cisco-AVPair
621 vp = fr_cursor_next_by_num(&cursor, 1, 9, TAG_ANY);
623 return RLM_MODULE_NOOP;
627 * If it's "leap:session-key", then stop.
629 * The format is VERY specific!
631 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
637 * The format is very specific.
639 if (vp->vp_length != (17 + 34)) {
640 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %zu: Expected %d",
641 vp->vp_length, 17 + 34);
642 return RLM_MODULE_NOOP;
646 * Decrypt the session key, using the proxy data.
648 * Note that the session key is *binary*, and therefore
649 * may contain embedded zeros. So we have to use memdup.
650 * However, Cisco-AVPair is a "string", so the rest of the
651 * code assumes that it's terminated by a trailing '\0'.
653 * So... be sure to (a) use memdup, and (b) include the last
657 p = talloc_memdup(vp, vp->vp_strvalue, vp->vp_length + 1);
658 talloc_set_type(p, uint8_t);
659 len = rad_tunnel_pwdecode((uint8_t *)p + 17, &i, request->home_server->secret, request->proxy->vector);
662 * FIXME: Assert that i == 16.
666 * Encrypt the session key again, using the request data.
668 rad_tunnel_pwencode(p + 17, &len,
669 request->client->secret,
670 request->packet->vector);
673 return RLM_MODULE_UPDATED;
677 static rlm_rcode_t CC_HINT(nonnull) mod_post_auth(void *instance, REQUEST *request)
679 rlm_eap_t *inst = instance;
681 eap_handler_t *handler;
682 eap_packet_raw_t *eap_packet;
685 * Only build a failure message if something previously rejected the request
687 vp = pairfind(request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
689 if (!vp || (vp->vp_integer != PW_POST_AUTH_TYPE_REJECT)) return RLM_MODULE_NOOP;
691 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
692 RDEBUG2("Request didn't contain an EAP-Message, not inserting EAP-Failure");
693 return RLM_MODULE_NOOP;
696 if (pairfind(request->reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
697 RDEBUG2("Reply already contained an EAP-Message, not inserting EAP-Failure");
698 return RLM_MODULE_NOOP;
701 eap_packet = eap_vp2packet(request, request->packet->vps);
703 RERROR("Malformed EAP Message: %s", fr_strerror());
704 return RLM_MODULE_FAIL;
707 handler = eap_handler(inst, &eap_packet, request);
709 RDEBUG2("Failed to get handler, probably already removed, not inserting EAP-Failure");
710 return RLM_MODULE_NOOP;
713 RDEBUG2("Request was previously rejected, inserting EAP-Failure");
715 talloc_free(handler);
718 * Make sure there's a message authenticator attribute in the response
719 * RADIUS protocol code will calculate the correct value later...
721 vp = pairfind(request->reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
723 pairmake_reply("Message-Authenticator", "0x00", T_OP_EQ);
726 return RLM_MODULE_UPDATED;
730 * The module name should be the only globally exported symbol.
731 * That is, everything else should be 'static'.
733 extern module_t rlm_eap;
735 .magic = RLM_MODULE_INIT,
737 .inst_size = sizeof(rlm_eap_t),
738 .config = module_config,
739 .instantiate = mod_instantiate,
740 .detach = mod_detach,
742 [MOD_AUTHENTICATE] = mod_authenticate,
743 [MOD_AUTHORIZE] = mod_authorize,
745 [MOD_POST_PROXY] = mod_post_proxy,
747 [MOD_POST_AUTH] = mod_post_auth