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>
35 static const CONF_PARSER module_config[] = {
36 { "default_eap_type", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_eap_t, default_method_name), "md5" },
37 { "timer_expire", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_eap_t, timer_limit), "60" },
38 { "ignore_unknown_eap_types", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_eap_t, ignore_unknown_types), "no" },
39 { "cisco_accounting_username_bug", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_eap_t, mod_accounting_username_bug), "no" },
40 { "max_sessions", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_eap_t, max_sessions), "2048" },
41 CONF_PARSER_TERMINATOR
45 * delete all the allocated space by eap module
47 static int mod_detach(void *instance)
51 inst = (rlm_eap_t *)instance;
54 pthread_mutex_destroy(&(inst->session_mutex));
57 rbtree_free(inst->session_tree);
58 inst->session_tree = NULL;
66 * Compare two handlers.
68 static int eap_handler_cmp(void const *a, void const *b)
71 eap_handler_t const *one = a;
72 eap_handler_t const *two = b;
74 if (one->eap_id < two->eap_id) return -1;
75 if (one->eap_id > two->eap_id) return +1;
77 rcode = memcmp(one->state, two->state, sizeof(one->state));
78 if (rcode != 0) return rcode;
81 * As of 2.1.8, we don't key off of source IP. This
82 * a NAS to send packets load-balanced (or fail-over)
83 * across multiple intermediate proxies, and still have
86 if (fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr) != 0) {
87 WARN("EAP packets are arriving from two different upstream "
88 "servers. Has there been a proxy fail-over?");
96 * read the config section and load all the eap authentication types present.
98 static int mod_instantiate(CONF_SECTION *cs, void *instance)
104 rlm_eap_t *inst = instance;
107 * Create our own random pool.
109 for (i = 0; i < 256; i++) {
110 inst->rand_pool.randrsl[i] = fr_rand();
112 fr_randinit(&inst->rand_pool, 1);
113 inst->rand_pool.randcnt = 0;
115 inst->xlat_name = cf_section_name2(cs);
116 if (!inst->xlat_name) inst->xlat_name = "EAP";
118 if (!dict_valbyname(PW_AUTH_TYPE, 0, inst->xlat_name)) {
119 cf_log_err_cs(cs, "Failed to find 'Auth-Type %s' section. Cannot authenticate users.",
124 /* Load all the configured EAP-Types */
126 for(scs = cf_subsection_find_next(cs, NULL, NULL);
128 scs = cf_subsection_find_next(cs, scs, NULL)) {
131 name = cf_section_name1(scs);
134 if (!strcmp(name, TLS_CONFIG_SECTION)) continue;
137 * Easier sometimes than commenting out blocks,
138 * or deleting blocks.
140 if (!strcmp(name, "disable")) continue;
142 method = eap_name2type(name);
143 if (method == PW_EAP_INVALID) {
144 cf_log_err_cs(cs, "No dictionary definition for EAP method %s", name);
148 if ((method < PW_EAP_MD5) || (method >= PW_EAP_MAX_TYPES)) {
149 cf_log_err_cs(cs, "Invalid EAP method %s (unsupported)", name);
153 #if !defined(HAVE_OPENSSL_SSL_H) || !defined(HAVE_LIBSSL)
155 * This allows the default configuration to be
156 * shipped with EAP-TLS, etc. enabled. If the
157 * system doesn't have OpenSSL, they will be
160 * If the system does have OpenSSL, then this
161 * code will not be used. The administrator will
162 * then have to delete the tls,
163 * etc. configurations from eap.conf in order to
164 * have EAP without the TLS types.
171 WARN("rlm_eap (%s): Ignoring EAP method %s because we don't have OpenSSL support",
172 inst->xlat_name, name);
183 ret = eap_module_instantiate(inst, &inst->methods[method], method, scs);
185 (void) talloc_get_type_abort(inst->methods[method], eap_module_t);
188 (void) talloc_steal(inst, inst->methods[method]);
192 (void) talloc_steal(inst, inst->methods[method]);
193 num_methods++; /* successfully loaded one more methods */
196 if (num_methods == 0) {
197 cf_log_err_cs(cs, "No EAP method configured, module cannot do anything");
202 * Ensure that the default EAP type is loaded.
204 method = eap_name2type(inst->default_method_name);
205 if (method == PW_EAP_INVALID) {
206 cf_log_err_cs(cs, "No dictionary definition for default EAP method '%s'",
207 inst->default_method_name);
211 if (!inst->methods[method]) {
212 cf_log_err_cs(cs, "No such sub-type for default EAP method %s",
213 inst->default_method_name);
216 inst->default_method = method; /* save the numerical method */
219 * List of sessions are set to NULL by the memset
224 * Lookup sessions in the tree. We don't free them in
225 * the tree, as that's taken care of elsewhere...
227 inst->session_tree = rbtree_create(NULL, eap_handler_cmp, NULL, 0);
228 if (!inst->session_tree) {
229 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
232 fr_link_talloc_ctx_free(inst, inst->session_tree);
234 #ifdef HAVE_PTHREAD_H
235 if (pthread_mutex_init(&(inst->session_mutex), NULL) < 0) {
236 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, fr_syserror(errno));
246 * For backwards compatibility.
248 static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
251 eap_handler_t *handler;
252 eap_packet_raw_t *eap_packet;
256 inst = (rlm_eap_t *) instance;
258 if (!fr_pair_find_by_num(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
259 REDEBUG("You set 'Auth-Type = %s' for a request that does "
260 "not contain an EAP-Message attribute!", inst->xlat_name);
261 return RLM_MODULE_INVALID;
265 * Get the eap packet to start with
267 eap_packet = eap_vp2packet(request, request->packet->vps);
269 RERROR("Malformed EAP Message: %s", fr_strerror());
270 return RLM_MODULE_FAIL;
274 * Create the eap handler. The eap_packet will end up being
275 * "swallowed" into the handler, so we can't access it after
278 handler = eap_handler(inst, &eap_packet, request);
280 RDEBUG2("Failed in handler");
281 return RLM_MODULE_INVALID;
285 * Select the appropriate method or default to the
288 status = eap_method_select(inst, handler);
293 if (status == EAP_INVALID) {
295 talloc_free(handler);
296 RDEBUG2("Failed in EAP select");
297 return RLM_MODULE_INVALID;
302 * If we're doing horrible tunneling work, remember it.
304 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
305 RDEBUG2("No EAP proxy set. Not composing EAP");
307 * Add the handle to the proxied list, so that we
308 * can retrieve it in the post-proxy stage, and
311 handler->inst_holder = inst;
312 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
314 rad_assert(status == 0);
315 return RLM_MODULE_HANDLED;
321 * Maybe the request was marked to be proxied. If so,
324 if (request->proxy != NULL) {
325 VALUE_PAIR *vp = NULL;
327 rad_assert(!request->proxy_reply);
330 * Add the handle to the proxied list, so that we
331 * can retrieve it in the post-proxy stage, and
334 handler->inst_holder = inst;
336 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
338 rad_assert(status == 0);
341 * Some simple sanity checks. These should really
342 * be handled by the radius library...
344 vp = fr_pair_find_by_num(request->proxy->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
346 vp = fr_pair_find_by_num(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
348 fr_pair_make(request->proxy,
349 &request->proxy->vps,
350 "Message-Authenticator",
356 * Delete the "proxied to" attribute, as it's
357 * set to 127.0.0.1 for tunneled requests, and
358 * we don't want to tell the world that...
360 fr_pair_delete_by_num(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS, TAG_ANY);
362 RWDEBUG2("Tunneled session will be proxied. Not doing EAP");
363 return RLM_MODULE_HANDLED;
368 * We are done, wrap the EAP-request in RADIUS to send
369 * with all other required radius attributes
371 rcode = eap_compose(handler);
374 * Add to the list only if it is EAP-Request, OR if
375 * it's LEAP, and a response.
377 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
378 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) ||
381 * LEAP is a little different. At Stage 4,
382 * it sends an EAP-Success message, but we still
383 * need to keep the State attribute & session
384 * data structure around for the AP Challenge.
386 * At stage 6, LEAP sends an EAP-Response, which
387 * isn't put into the list.
389 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
390 (handler->eap_ds->response->type.num == PW_EAP_LEAP) &&
391 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
392 (handler->eap_ds->request->type.num == 0))) {
395 * Return FAIL if we can't remember the handler.
396 * This is actually disallowed by the
397 * specification, as unexpected FAILs could have
398 * been forged. However, we want to signal to
399 * everyone else involved that we are
400 * intentionally failing the session, as opposed
401 * to accidentally failing it.
403 if (!eaplist_add(inst, handler)) {
404 RDEBUG("Failed adding handler to the list");
406 talloc_free(handler);
407 return RLM_MODULE_FAIL;
412 * Enable the cached entry on success.
414 if (handler->eap_ds->request->code == PW_EAP_SUCCESS) {
417 vp = fr_pair_find_by_num(request->state, PW_TLS_CACHE_FILENAME, 0, TAG_ANY);
418 if (vp) (void) chmod(vp->vp_strvalue, S_IRUSR | S_IWUSR);
422 * Disable the cached entry on failure.
424 if (handler->eap_ds->request->code == PW_EAP_FAILURE) {
427 vp = fr_pair_find_by_num(request->state, PW_TLS_CACHE_FILENAME, 0, TAG_ANY);
428 if (vp) (void) unlink(vp->vp_strvalue);
431 RDEBUG2("Freeing handler");
432 /* handler is not required any more, free it now */
433 talloc_free(handler);
437 * If it's an Access-Accept, RFC 2869, Section 2.3.1
438 * says that we MUST include a User-Name attribute in the
441 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
446 * Doesn't exist, add it in.
448 vp = fr_pair_find_by_num(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
450 vp = fr_pair_copy(request->reply, request->username);
451 fr_pair_add(&request->reply->vps, vp);
455 * Cisco AP1230 has a bug and needs a zero
456 * terminated string in Access-Accept. This
457 * means it requires 2 trailing zeros. One to
458 * send in the RADIUS packet, and the other to
459 * convince the rest of the server that
460 * vp->vp_strvalue is still a NUL-terminated C
463 if (inst->mod_accounting_username_bug) {
464 char const *old = vp->vp_strvalue;
467 vp->vp_length++; /* account for an additional zero */
469 new = talloc_array(vp, char, vp->vp_length + 1);
471 memcpy(new, old, vp->vp_length);
472 new[vp->length + 1] = '\0';
473 vp->vp_strvalue = new;
483 * EAP authorization DEPENDS on other rlm authorizations,
484 * to check for user existence & get their configured values.
485 * It Handles EAP-START Messages, User-Name initilization.
487 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
493 inst = (rlm_eap_t *)instance;
497 * We don't do authorization again, once we've seen the
498 * proxy reply (or the proxied packet)
500 if (request->proxy != NULL)
501 return RLM_MODULE_NOOP;
505 * For EAP_START, send Access-Challenge with EAP Identity
506 * request. even when we have to proxy this request
508 * RFC 2869, Section 2.3.1 notes that the "domain" of the
509 * user, (i.e. where to proxy him) comes from the EAP-Identity,
510 * so we CANNOT proxy the user, until we know his identity.
512 * We therefore send an EAP Identity request.
514 status = eap_start(inst, request);
517 return RLM_MODULE_NOOP;
519 return RLM_MODULE_FAIL;
521 return RLM_MODULE_HANDLED;
529 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
530 * it MUST copy the identity into the User-Name attribute.
532 * But we don't worry about that too much. We depend on
533 * each EAP sub-module to look for handler->request->username,
534 * and to get excited if it doesn't appear.
536 vp = fr_pair_find_by_num(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
537 if ((!vp) || (vp->vp_integer != PW_AUTH_TYPE_REJECT)) {
538 vp = pair_make_config("Auth-Type", inst->xlat_name, T_OP_EQ);
540 RDEBUG2("Failed to create Auth-Type %s: %s\n",
541 inst->xlat_name, fr_strerror());
542 return RLM_MODULE_FAIL;
545 RWDEBUG2("Auth-Type already set. Not setting to EAP");
548 if (status == EAP_OK) return RLM_MODULE_OK;
550 return RLM_MODULE_UPDATED;
556 * If we're proxying EAP, then there may be magic we need
559 static rlm_rcode_t CC_HINT(nonnull) mod_post_proxy(void *inst, REQUEST *request)
566 eap_handler_t *handler;
570 * If there was a handler associated with this request,
571 * then it's a tunneled request which was proxied...
573 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
574 if (handler != NULL) {
576 eap_tunnel_data_t *data;
579 * Grab the tunnel callbacks from the request.
581 data = (eap_tunnel_data_t *) request_data_get(request,
583 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
585 RERROR("Failed to retrieve callback for tunneled session!");
586 talloc_free(handler);
587 return RLM_MODULE_FAIL;
593 RDEBUG2("Doing post-proxy callback");
594 rcode = data->callback(handler, data->tls_session);
597 RDEBUG2("Failed in post-proxy callback");
599 talloc_free(handler);
600 return RLM_MODULE_REJECT;
604 * We are done, wrap the EAP-request in RADIUS to send
605 * with all other required radius attributes
607 eap_compose(handler);
610 * Add to the list only if it is EAP-Request, OR if
611 * it's LEAP, and a response.
613 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
614 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) {
615 if (!eaplist_add(inst, handler)) {
617 talloc_free(handler);
618 return RLM_MODULE_FAIL;
621 } else { /* couldn't have been LEAP, there's no tunnel */
622 RDEBUG2("Freeing handler");
623 /* handler is not required any more, free it now */
624 talloc_free(handler);
628 * If it's an Access-Accept, RFC 2869, Section 2.3.1
629 * says that we MUST include a User-Name attribute in the
632 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
635 * Doesn't exist, add it in.
637 vp = fr_pair_find_by_num(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
639 pair_make_reply("User-Name",
640 request->username->vp_strvalue,
645 return RLM_MODULE_OK;
647 RDEBUG2("No pre-existing handler found");
653 if (!request->proxy_reply) return RLM_MODULE_NOOP;
656 * There may be more than one Cisco-AVPair.
657 * Ensure we find the one with the LEAP attribute.
659 fr_cursor_init(&cursor, &request->proxy_reply->vps);
662 * Hmm... there's got to be a better way to
663 * discover codes for vendor attributes.
665 * This is vendor Cisco (9), Cisco-AVPair
668 vp = fr_cursor_next_by_num(&cursor, 1, 9, TAG_ANY);
670 return RLM_MODULE_NOOP;
674 * If it's "leap:session-key", then stop.
676 * The format is VERY specific!
678 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
684 * The format is very specific.
686 if (vp->vp_length != (17 + 34)) {
687 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %zu: Expected %d",
688 vp->vp_length, 17 + 34);
689 return RLM_MODULE_NOOP;
693 * Decrypt the session key, using the proxy data.
695 * Note that the session key is *binary*, and therefore
696 * may contain embedded zeros. So we have to use memdup.
697 * However, Cisco-AVPair is a "string", so the rest of the
698 * code assumes that it's terminated by a trailing '\0'.
700 * So... be sure to (a) use memdup, and (b) include the last
704 p = talloc_memdup(vp, vp->vp_strvalue, vp->vp_length + 1);
705 talloc_set_type(p, uint8_t);
706 ret = rad_tunnel_pwdecode((uint8_t *)p + 17, &i, request->home_server->secret, request->proxy->vector);
708 REDEBUG("Decoding leap:session-key failed");
710 return RLM_MODULE_FAIL;
715 REDEBUG("Decoded key length is incorrect, must be 16 bytes");
717 return RLM_MODULE_FAIL;
721 * Encrypt the session key again, using the request data.
723 ret = rad_tunnel_pwencode(p + 17, &len, request->client->secret, request->packet->vector);
725 REDEBUG("Decoding leap:session-key failed");
727 return RLM_MODULE_FAIL;
730 fr_pair_value_strsteal(vp, p);
732 return RLM_MODULE_UPDATED;
736 static rlm_rcode_t CC_HINT(nonnull) mod_post_auth(void *instance, REQUEST *request)
738 rlm_eap_t *inst = instance;
740 eap_handler_t *handler;
741 eap_packet_raw_t *eap_packet;
744 * Only build a failure message if something previously rejected the request
746 vp = fr_pair_find_by_num(request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
748 if (!vp || (vp->vp_integer != PW_POST_AUTH_TYPE_REJECT)) return RLM_MODULE_NOOP;
750 if (!fr_pair_find_by_num(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
751 RDEBUG3("Request didn't contain an EAP-Message, not inserting EAP-Failure");
752 return RLM_MODULE_NOOP;
755 if (fr_pair_find_by_num(request->reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
756 RDEBUG3("Reply already contained an EAP-Message, not inserting EAP-Failure");
757 return RLM_MODULE_NOOP;
760 eap_packet = eap_vp2packet(request, request->packet->vps);
762 RERROR("Malformed EAP Message: %s", fr_strerror());
763 return RLM_MODULE_FAIL;
766 handler = eap_handler(inst, &eap_packet, request);
768 RDEBUG2("Failed to get handler, probably already removed, not inserting EAP-Failure");
769 return RLM_MODULE_NOOP;
772 RDEBUG2("Request was previously rejected, inserting EAP-Failure");
774 talloc_free(handler);
777 * Make sure there's a message authenticator attribute in the response
778 * RADIUS protocol code will calculate the correct value later...
780 vp = fr_pair_find_by_num(request->reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
782 pair_make_reply("Message-Authenticator", "0x00", T_OP_EQ);
785 return RLM_MODULE_UPDATED;
789 * The module name should be the only globally exported symbol.
790 * That is, everything else should be 'static'.
792 extern module_t rlm_eap;
794 .magic = RLM_MODULE_INIT,
796 .inst_size = sizeof(rlm_eap_t),
797 .config = module_config,
798 .instantiate = mod_instantiate,
799 .detach = mod_detach,
801 [MOD_AUTHENTICATE] = mod_authenticate,
802 [MOD_AUTHORIZE] = mod_authorize,
804 [MOD_POST_PROXY] = mod_post_proxy,
806 [MOD_POST_AUTH] = mod_post_auth