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 if (!dict_valbyname(PW_AUTH_TYPE, 0, inst->xlat_name)) {
117 cf_log_err_cs(cs, "Failed to find 'Auth-Type %s' section. Cannot authenticate users.",
122 /* Load all the configured EAP-Types */
124 for(scs = cf_subsection_find_next(cs, NULL, NULL);
126 scs = cf_subsection_find_next(cs, scs, NULL)) {
129 name = cf_section_name1(scs);
132 if (!strcmp(name, TLS_CONFIG_SECTION)) continue;
135 * Easier sometimes than commenting out blocks,
136 * or deleting blocks.
138 if (!strcmp(name, "disable")) continue;
140 method = eap_name2type(name);
141 if (method == PW_EAP_INVALID) {
142 cf_log_err_cs(cs, "No dictionary definition for EAP method %s", name);
146 if ((method < PW_EAP_MD5) || (method >= PW_EAP_MAX_TYPES)) {
147 cf_log_err_cs(cs, "Invalid EAP method %s (unsupported)", name);
151 #if !defined(HAVE_OPENSSL_SSL_H) || !defined(HAVE_LIBSSL)
153 * This allows the default configuration to be
154 * shipped with EAP-TLS, etc. enabled. If the
155 * system doesn't have OpenSSL, they will be
158 * If the system does have OpenSSL, then this
159 * code will not be used. The administrator will
160 * then have to delete the tls,
161 * etc. configurations from eap.conf in order to
162 * have EAP without the TLS types.
169 WARN("rlm_eap (%s): Ignoring EAP method %s because we don't have OpenSSL support",
170 inst->xlat_name, name);
181 ret = eap_module_instantiate(inst, &inst->methods[method], method, scs);
183 (void) talloc_get_type_abort(inst->methods[method], eap_module_t);
186 (void) talloc_steal(inst, inst->methods[method]);
190 (void) talloc_steal(inst, inst->methods[method]);
191 num_methods++; /* successfully loaded one more methods */
194 if (num_methods == 0) {
195 cf_log_err_cs(cs, "No EAP method configured, module cannot do anything");
200 * Ensure that the default EAP type is loaded.
202 method = eap_name2type(inst->default_method_name);
203 if (method == PW_EAP_INVALID) {
204 cf_log_err_cs(cs, "No dictionary definition for default EAP method '%s'",
205 inst->default_method_name);
209 if (!inst->methods[method]) {
210 cf_log_err_cs(cs, "No such sub-type for default EAP method %s",
211 inst->default_method_name);
214 inst->default_method = method; /* save the numerical method */
217 * List of sessions are set to NULL by the memset
222 * Lookup sessions in the tree. We don't free them in
223 * the tree, as that's taken care of elsewhere...
225 inst->session_tree = rbtree_create(NULL, eap_handler_cmp, NULL, 0);
226 if (!inst->session_tree) {
227 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
230 fr_link_talloc_ctx_free(inst, inst->session_tree);
232 #ifdef HAVE_PTHREAD_H
233 if (pthread_mutex_init(&(inst->session_mutex), NULL) < 0) {
234 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, fr_syserror(errno));
244 * For backwards compatibility.
246 static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
249 eap_handler_t *handler;
250 eap_packet_raw_t *eap_packet;
254 inst = (rlm_eap_t *) instance;
256 if (!fr_pair_find_by_num(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
257 REDEBUG("You set 'Auth-Type = %s' for a request that does "
258 "not contain an EAP-Message attribute!", inst->xlat_name);
259 return RLM_MODULE_INVALID;
263 * Get the eap packet to start with
265 eap_packet = eap_vp2packet(request, request->packet->vps);
267 RERROR("Malformed EAP Message: %s", fr_strerror());
268 return RLM_MODULE_FAIL;
272 * Create the eap handler. The eap_packet will end up being
273 * "swallowed" into the handler, so we can't access it after
276 handler = eap_handler(inst, &eap_packet, request);
278 RDEBUG2("Failed in handler");
279 return RLM_MODULE_INVALID;
283 * Select the appropriate method or default to the
286 status = eap_method_select(inst, handler);
291 if (status == EAP_INVALID) {
293 talloc_free(handler);
294 RDEBUG2("Failed in EAP select");
295 return RLM_MODULE_INVALID;
300 * If we're doing horrible tunneling work, remember it.
302 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
303 RDEBUG2("No EAP proxy set. Not composing EAP");
305 * Add the handle to the proxied list, so that we
306 * can retrieve it in the post-proxy stage, and
309 handler->inst_holder = inst;
310 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
312 rad_assert(status == 0);
313 return RLM_MODULE_HANDLED;
319 * Maybe the request was marked to be proxied. If so,
322 if (request->proxy != NULL) {
323 VALUE_PAIR *vp = NULL;
325 rad_assert(!request->proxy_reply);
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;
334 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
336 rad_assert(status == 0);
339 * Some simple sanity checks. These should really
340 * be handled by the radius library...
342 vp = fr_pair_find_by_num(request->proxy->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
344 vp = fr_pair_find_by_num(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
346 fr_pair_make(request->proxy,
347 &request->proxy->vps,
348 "Message-Authenticator",
354 * Delete the "proxied to" attribute, as it's
355 * set to 127.0.0.1 for tunneled requests, and
356 * we don't want to tell the world that...
358 fr_pair_delete_by_num(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS, TAG_ANY);
360 RDEBUG2("Tunneled session will be proxied. Not doing EAP");
361 return RLM_MODULE_HANDLED;
366 * We are done, wrap the EAP-request in RADIUS to send
367 * with all other required radius attributes
369 rcode = eap_compose(handler);
372 * Add to the list only if it is EAP-Request, OR if
373 * it's LEAP, and a response.
375 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
376 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) ||
379 * LEAP is a little different. At Stage 4,
380 * it sends an EAP-Success message, but we still
381 * need to keep the State attribute & session
382 * data structure around for the AP Challenge.
384 * At stage 6, LEAP sends an EAP-Response, which
385 * isn't put into the list.
387 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
388 (handler->eap_ds->response->type.num == PW_EAP_LEAP) &&
389 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
390 (handler->eap_ds->request->type.num == 0))) {
393 * Return FAIL if we can't remember the handler.
394 * This is actually disallowed by the
395 * specification, as unexpected FAILs could have
396 * been forged. However, we want to signal to
397 * everyone else involved that we are
398 * intentionally failing the session, as opposed
399 * to accidentally failing it.
401 if (!eaplist_add(inst, handler)) {
402 RDEBUG("Failed adding handler to the list");
404 talloc_free(handler);
405 return RLM_MODULE_FAIL;
409 RDEBUG2("Freeing handler");
410 /* handler is not required any more, free it now */
411 talloc_free(handler);
415 * If it's an Access-Accept, RFC 2869, Section 2.3.1
416 * says that we MUST include a User-Name attribute in the
419 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
424 * Doesn't exist, add it in.
426 vp = fr_pair_find_by_num(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
428 vp = fr_pair_copy(request->reply, request->username);
429 fr_pair_add(&request->reply->vps, vp);
433 * Cisco AP1230 has a bug and needs a zero
434 * terminated string in Access-Accept.
436 if (inst->mod_accounting_username_bug) {
437 char const *old = vp->vp_strvalue;
438 char *new = talloc_zero_array(vp, char, vp->vp_length + 1);
440 memcpy(new, old, vp->vp_length);
441 vp->vp_strvalue = new;
452 * EAP authorization DEPENDS on other rlm authorizations,
453 * to check for user existence & get their configured values.
454 * It Handles EAP-START Messages, User-Name initilization.
456 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
462 inst = (rlm_eap_t *)instance;
466 * We don't do authorization again, once we've seen the
467 * proxy reply (or the proxied packet)
469 if (request->proxy != NULL)
470 return RLM_MODULE_NOOP;
474 * For EAP_START, send Access-Challenge with EAP Identity
475 * request. even when we have to proxy this request
477 * RFC 2869, Section 2.3.1 notes that the "domain" of the
478 * user, (i.e. where to proxy him) comes from the EAP-Identity,
479 * so we CANNOT proxy the user, until we know his identity.
481 * We therefore send an EAP Identity request.
483 status = eap_start(inst, request);
486 return RLM_MODULE_NOOP;
488 return RLM_MODULE_FAIL;
490 return RLM_MODULE_HANDLED;
498 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
499 * it MUST copy the identity into the User-Name attribute.
501 * But we don't worry about that too much. We depend on
502 * each EAP sub-module to look for handler->request->username,
503 * and to get excited if it doesn't appear.
505 vp = fr_pair_find_by_num(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
506 if ((!vp) || (vp->vp_integer != PW_AUTH_TYPE_REJECT)) {
507 vp = pair_make_config("Auth-Type", inst->xlat_name, T_OP_EQ);
509 RDEBUG2("Failed to create Auth-Type %s: %s\n",
510 inst->xlat_name, fr_strerror());
511 return RLM_MODULE_FAIL;
514 RWDEBUG2("Auth-Type already set. Not setting to EAP");
517 if (status == EAP_OK) return RLM_MODULE_OK;
519 return RLM_MODULE_UPDATED;
525 * If we're proxying EAP, then there may be magic we need
528 static rlm_rcode_t CC_HINT(nonnull) mod_post_proxy(void *inst, REQUEST *request)
535 eap_handler_t *handler;
539 * If there was a handler associated with this request,
540 * then it's a tunneled request which was proxied...
542 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
543 if (handler != NULL) {
545 eap_tunnel_data_t *data;
548 * Grab the tunnel callbacks from the request.
550 data = (eap_tunnel_data_t *) request_data_get(request,
552 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
554 RERROR("Failed to retrieve callback for tunneled session!");
555 talloc_free(handler);
556 return RLM_MODULE_FAIL;
562 RDEBUG2("Doing post-proxy callback");
563 rcode = data->callback(handler, data->tls_session);
566 RDEBUG2("Failed in post-proxy callback");
568 talloc_free(handler);
569 return RLM_MODULE_REJECT;
573 * We are done, wrap the EAP-request in RADIUS to send
574 * with all other required radius attributes
576 eap_compose(handler);
579 * Add to the list only if it is EAP-Request, OR if
580 * it's LEAP, and a response.
582 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
583 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) {
584 if (!eaplist_add(inst, handler)) {
586 talloc_free(handler);
587 return RLM_MODULE_FAIL;
590 } else { /* couldn't have been LEAP, there's no tunnel */
591 RDEBUG2("Freeing handler");
592 /* handler is not required any more, free it now */
593 talloc_free(handler);
597 * If it's an Access-Accept, RFC 2869, Section 2.3.1
598 * says that we MUST include a User-Name attribute in the
601 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
604 * Doesn't exist, add it in.
606 vp = fr_pair_find_by_num(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
608 pair_make_reply("User-Name",
609 request->username->vp_strvalue,
614 return RLM_MODULE_OK;
616 RDEBUG2("No pre-existing handler found");
622 if (!request->proxy_reply) return RLM_MODULE_NOOP;
625 * There may be more than one Cisco-AVPair.
626 * Ensure we find the one with the LEAP attribute.
628 fr_cursor_init(&cursor, &request->proxy_reply->vps);
631 * Hmm... there's got to be a better way to
632 * discover codes for vendor attributes.
634 * This is vendor Cisco (9), Cisco-AVPair
637 vp = fr_cursor_next_by_num(&cursor, 1, 9, TAG_ANY);
639 return RLM_MODULE_NOOP;
643 * If it's "leap:session-key", then stop.
645 * The format is VERY specific!
647 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
653 * The format is very specific.
655 if (vp->vp_length != (17 + 34)) {
656 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %zu: Expected %d",
657 vp->vp_length, 17 + 34);
658 return RLM_MODULE_NOOP;
662 * Decrypt the session key, using the proxy data.
664 * Note that the session key is *binary*, and therefore
665 * may contain embedded zeros. So we have to use memdup.
666 * However, Cisco-AVPair is a "string", so the rest of the
667 * code assumes that it's terminated by a trailing '\0'.
669 * So... be sure to (a) use memdup, and (b) include the last
673 p = talloc_memdup(vp, vp->vp_strvalue, vp->vp_length + 1);
674 talloc_set_type(p, uint8_t);
675 ret = rad_tunnel_pwdecode((uint8_t *)p + 17, &i, request->home_server->secret, request->proxy->vector);
677 REDEBUG("Decoding leap:session-key failed");
679 return RLM_MODULE_FAIL;
684 REDEBUG("Decoded key length is incorrect, must be 16 bytes");
686 return RLM_MODULE_FAIL;
690 * Encrypt the session key again, using the request data.
692 ret = rad_tunnel_pwencode(p + 17, &len, request->client->secret, request->packet->vector);
694 REDEBUG("Decoding leap:session-key failed");
696 return RLM_MODULE_FAIL;
699 fr_pair_value_strsteal(vp, p);
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 = fr_pair_find_by_num(request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
717 if (!vp || (vp->vp_integer != PW_POST_AUTH_TYPE_REJECT)) return RLM_MODULE_NOOP;
719 if (!fr_pair_find_by_num(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
720 RDEBUG3("Request didn't contain an EAP-Message, not inserting EAP-Failure");
721 return RLM_MODULE_NOOP;
724 if (fr_pair_find_by_num(request->reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
725 RDEBUG3("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: %s", fr_strerror());
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 = fr_pair_find_by_num(request->reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
751 pair_make_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'.
761 extern module_t rlm_eap;
763 .magic = RLM_MODULE_INIT,
765 .inst_size = sizeof(rlm_eap_t),
766 .config = module_config,
767 .instantiate = mod_instantiate,
768 .detach = mod_detach,
770 [MOD_AUTHENTICATE] = mod_authenticate,
771 [MOD_AUTHORIZE] = mod_authorize,
773 [MOD_POST_PROXY] = mod_post_proxy,
775 [MOD_POST_AUTH] = mod_post_auth