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] = '\0';
473 new[vp->length + 1] = '\0';
474 vp->vp_strvalue = new;
485 * EAP authorization DEPENDS on other rlm authorizations,
486 * to check for user existence & get their configured values.
487 * It Handles EAP-START Messages, User-Name initilization.
489 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
495 inst = (rlm_eap_t *)instance;
499 * We don't do authorization again, once we've seen the
500 * proxy reply (or the proxied packet)
502 if (request->proxy != NULL)
503 return RLM_MODULE_NOOP;
507 * For EAP_START, send Access-Challenge with EAP Identity
508 * request. even when we have to proxy this request
510 * RFC 2869, Section 2.3.1 notes that the "domain" of the
511 * user, (i.e. where to proxy him) comes from the EAP-Identity,
512 * so we CANNOT proxy the user, until we know his identity.
514 * We therefore send an EAP Identity request.
516 status = eap_start(inst, request);
519 return RLM_MODULE_NOOP;
521 return RLM_MODULE_FAIL;
523 return RLM_MODULE_HANDLED;
531 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
532 * it MUST copy the identity into the User-Name attribute.
534 * But we don't worry about that too much. We depend on
535 * each EAP sub-module to look for handler->request->username,
536 * and to get excited if it doesn't appear.
538 vp = fr_pair_find_by_num(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
539 if ((!vp) || (vp->vp_integer != PW_AUTH_TYPE_REJECT)) {
540 vp = pair_make_config("Auth-Type", inst->xlat_name, T_OP_EQ);
542 RDEBUG2("Failed to create Auth-Type %s: %s\n",
543 inst->xlat_name, fr_strerror());
544 return RLM_MODULE_FAIL;
547 RWDEBUG2("Auth-Type already set. Not setting to EAP");
550 if (status == EAP_OK) return RLM_MODULE_OK;
552 return RLM_MODULE_UPDATED;
558 * If we're proxying EAP, then there may be magic we need
561 static rlm_rcode_t CC_HINT(nonnull) mod_post_proxy(void *inst, REQUEST *request)
568 eap_handler_t *handler;
572 * If there was a handler associated with this request,
573 * then it's a tunneled request which was proxied...
575 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
576 if (handler != NULL) {
578 eap_tunnel_data_t *data;
581 * Grab the tunnel callbacks from the request.
583 data = (eap_tunnel_data_t *) request_data_get(request,
585 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
587 RERROR("Failed to retrieve callback for tunneled session!");
588 talloc_free(handler);
589 return RLM_MODULE_FAIL;
595 RDEBUG2("Doing post-proxy callback");
596 rcode = data->callback(handler, data->tls_session);
599 RDEBUG2("Failed in post-proxy callback");
601 talloc_free(handler);
602 return RLM_MODULE_REJECT;
606 * We are done, wrap the EAP-request in RADIUS to send
607 * with all other required radius attributes
609 eap_compose(handler);
612 * Add to the list only if it is EAP-Request, OR if
613 * it's LEAP, and a response.
615 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
616 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) {
617 if (!eaplist_add(inst, handler)) {
619 talloc_free(handler);
620 return RLM_MODULE_FAIL;
623 } else { /* couldn't have been LEAP, there's no tunnel */
624 RDEBUG2("Freeing handler");
625 /* handler is not required any more, free it now */
626 talloc_free(handler);
630 * If it's an Access-Accept, RFC 2869, Section 2.3.1
631 * says that we MUST include a User-Name attribute in the
634 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
637 * Doesn't exist, add it in.
639 vp = fr_pair_find_by_num(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
641 pair_make_reply("User-Name",
642 request->username->vp_strvalue,
647 return RLM_MODULE_OK;
649 RDEBUG2("No pre-existing handler found");
655 if (!request->proxy_reply) return RLM_MODULE_NOOP;
658 * There may be more than one Cisco-AVPair.
659 * Ensure we find the one with the LEAP attribute.
661 fr_cursor_init(&cursor, &request->proxy_reply->vps);
664 * Hmm... there's got to be a better way to
665 * discover codes for vendor attributes.
667 * This is vendor Cisco (9), Cisco-AVPair
670 vp = fr_cursor_next_by_num(&cursor, 1, 9, TAG_ANY);
672 return RLM_MODULE_NOOP;
676 * If it's "leap:session-key", then stop.
678 * The format is VERY specific!
680 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
686 * The format is very specific.
688 if (vp->vp_length != (17 + 34)) {
689 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %zu: Expected %d",
690 vp->vp_length, 17 + 34);
691 return RLM_MODULE_NOOP;
695 * Decrypt the session key, using the proxy data.
697 * Note that the session key is *binary*, and therefore
698 * may contain embedded zeros. So we have to use memdup.
699 * However, Cisco-AVPair is a "string", so the rest of the
700 * code assumes that it's terminated by a trailing '\0'.
702 * So... be sure to (a) use memdup, and (b) include the last
706 p = talloc_memdup(vp, vp->vp_strvalue, vp->vp_length + 1);
707 talloc_set_type(p, uint8_t);
708 ret = rad_tunnel_pwdecode((uint8_t *)p + 17, &i, request->home_server->secret, request->proxy->vector);
710 REDEBUG("Decoding leap:session-key failed");
712 return RLM_MODULE_FAIL;
717 REDEBUG("Decoded key length is incorrect, must be 16 bytes");
719 return RLM_MODULE_FAIL;
723 * Encrypt the session key again, using the request data.
725 ret = rad_tunnel_pwencode(p + 17, &len, request->client->secret, request->packet->vector);
727 REDEBUG("Decoding leap:session-key failed");
729 return RLM_MODULE_FAIL;
732 fr_pair_value_strsteal(vp, p);
734 return RLM_MODULE_UPDATED;
738 static rlm_rcode_t CC_HINT(nonnull) mod_post_auth(void *instance, REQUEST *request)
740 rlm_eap_t *inst = instance;
742 eap_handler_t *handler;
743 eap_packet_raw_t *eap_packet;
746 * Only build a failure message if something previously rejected the request
748 vp = fr_pair_find_by_num(request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
750 if (!vp || (vp->vp_integer != PW_POST_AUTH_TYPE_REJECT)) return RLM_MODULE_NOOP;
752 if (!fr_pair_find_by_num(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
753 RDEBUG3("Request didn't contain an EAP-Message, not inserting EAP-Failure");
754 return RLM_MODULE_NOOP;
757 if (fr_pair_find_by_num(request->reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
758 RDEBUG3("Reply already contained an EAP-Message, not inserting EAP-Failure");
759 return RLM_MODULE_NOOP;
762 eap_packet = eap_vp2packet(request, request->packet->vps);
764 RERROR("Malformed EAP Message: %s", fr_strerror());
765 return RLM_MODULE_FAIL;
768 handler = eap_handler(inst, &eap_packet, request);
770 RDEBUG2("Failed to get handler, probably already removed, not inserting EAP-Failure");
771 return RLM_MODULE_NOOP;
774 RDEBUG2("Request was previously rejected, inserting EAP-Failure");
776 talloc_free(handler);
779 * Make sure there's a message authenticator attribute in the response
780 * RADIUS protocol code will calculate the correct value later...
782 vp = fr_pair_find_by_num(request->reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
784 pair_make_reply("Message-Authenticator", "0x00", T_OP_EQ);
787 return RLM_MODULE_UPDATED;
791 * The module name should be the only globally exported symbol.
792 * That is, everything else should be 'static'.
794 extern module_t rlm_eap;
796 .magic = RLM_MODULE_INIT,
798 .inst_size = sizeof(rlm_eap_t),
799 .config = module_config,
800 .instantiate = mod_instantiate,
801 .detach = mod_detach,
803 [MOD_AUTHENTICATE] = mod_authenticate,
804 [MOD_AUTHORIZE] = mod_authorize,
806 [MOD_POST_PROXY] = mod_post_proxy,
808 [MOD_POST_AUTH] = mod_post_auth