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", PW_TYPE_STRING_PTR,
34 offsetof(rlm_eap_t, default_method_name), NULL, "md5" },
35 { "timer_expire", PW_TYPE_INTEGER,
36 offsetof(rlm_eap_t, timer_limit), NULL, "60"},
37 { "ignore_unknown_eap_types", PW_TYPE_BOOLEAN,
38 offsetof(rlm_eap_t, ignore_unknown_types), NULL, "no" },
39 { "mod_accounting_username_bug", PW_TYPE_BOOLEAN,
40 offsetof(rlm_eap_t, mod_accounting_username_bug), NULL, "no" },
41 { "max_sessions", PW_TYPE_INTEGER,
42 offsetof(rlm_eap_t, max_sessions), NULL, "2048"},
44 { NULL, -1, 0, NULL, NULL } /* end the list */
48 * delete all the allocated space by eap module
50 static int mod_detach(void *instance)
54 inst = (rlm_eap_t *)instance;
57 pthread_mutex_destroy(&(inst->session_mutex));
58 if (inst->handler_tree) pthread_mutex_destroy(&(inst->handler_mutex));
61 rbtree_free(inst->session_tree);
62 if (inst->handler_tree) rbtree_free(inst->handler_tree);
63 inst->session_tree = NULL;
71 * Compare two handlers.
73 static int eap_handler_cmp(void const *a, void const *b)
76 eap_handler_t const *one = a;
77 eap_handler_t const *two = b;
79 if (one->eap_id < two->eap_id) return -1;
80 if (one->eap_id > two->eap_id) return +1;
82 rcode = memcmp(one->state, two->state, sizeof(one->state));
83 if (rcode != 0) return rcode;
86 * As of 2.1.8, we don't key off of source IP. This
87 * a NAS to send packets load-balanced (or fail-over)
88 * across multiple intermediate proxies, and still have
91 if (fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr) != 0) {
92 WDEBUG("EAP packets are arriving from two different upstream "
93 "servers. Has there been a proxy fail-over?");
101 * Compare two handler pointers
103 static int eap_handler_ptr_cmp(void const *a, void const *b)
105 if (a < b) return -1;
106 if (a > b) return +1;
112 * read the config section and load all the eap authentication types present.
114 static int mod_instantiate(CONF_SECTION *cs, void *instance)
120 rlm_eap_t *inst = instance;
123 * Create our own random pool.
125 for (i = 0; i < 256; i++) {
126 inst->rand_pool.randrsl[i] = fr_rand();
128 fr_randinit(&inst->rand_pool, 1);
129 inst->rand_pool.randcnt = 0;
131 inst->xlat_name = cf_section_name2(cs);
132 if (!inst->xlat_name) inst->xlat_name = "EAP";
134 /* Load all the configured EAP-Types */
136 for(scs = cf_subsection_find_next(cs, NULL, NULL);
138 scs = cf_subsection_find_next(cs, scs, NULL)) {
142 name = cf_section_name1(scs);
145 if (!strcmp(name, TLS_CONFIG_SECTION)) continue;
147 method = eap_name2type(name);
148 if (method == PW_EAP_INVALID) {
149 cf_log_err_cs(cs, "Unknown EAP method %s", name);
153 if ((method < PW_EAP_MD5) || (method >= PW_EAP_MAX_TYPES)) {
154 cf_log_err_cs(cs, "Invalid EAP method %s (unsupported)", name);
158 #if !defined(HAVE_OPENSSL_SSL_H) || !defined(HAVE_LIBSSL)
160 * This allows the default configuration to be
161 * shipped with EAP-TLS, etc. enabled. If the
162 * system doesn't have OpenSSL, they will be
165 * If the system does have OpenSSL, then this
166 * code will not be used. The administrator will
167 * then have to delete the tls,
168 * etc. configurations from eap.conf in order to
169 * have EAP without the TLS types.
171 if ((method == PW_EAP_TLS) ||
172 (method == PW_EAP_TTLS) ||
173 (method == PW_EAP_PEAP)) {
174 DEBUG2("rlm_eap (%s): Ignoring EAP method %s because we do not have OpenSSL support",
175 inst->xlat_name, name);
183 ret = eap_module_load(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, "Unknown 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(eap_handler_cmp, NULL, 0);
228 if (!inst->session_tree) {
229 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
234 inst->handler_tree = rbtree_create(eap_handler_ptr_cmp, NULL, 0);
235 if (!inst->handler_tree) {
236 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
240 #ifdef HAVE_PTHREAD_H
241 if (pthread_mutex_init(&(inst->handler_mutex), NULL) < 0) {
242 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, strerror(errno));
248 #ifdef HAVE_PTHREAD_H
249 if (pthread_mutex_init(&(inst->session_mutex), NULL) < 0) {
250 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, strerror(errno));
260 * For backwards compatibility.
262 static rlm_rcode_t mod_authenticate(void *instance, REQUEST *request)
265 eap_handler_t *handler;
266 eap_packet_raw_t *eap_packet;
270 inst = (rlm_eap_t *) instance;
272 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
273 REDEBUG("You set 'Auth-Type = EAP' for a request that does "
274 "not contain an EAP-Message attribute!");
275 return RLM_MODULE_INVALID;
279 * Get the eap packet to start with
281 eap_packet = eap_vp2packet(request, request->packet->vps);
283 RERROR("Malformed EAP Message");
284 return RLM_MODULE_FAIL;
288 * Create the eap handler. The eap_packet will end up being
289 * "swallowed" into the handler, so we can't access it after
292 handler = eap_handler(inst, &eap_packet, request);
294 RDEBUG2("Failed in handler");
295 return RLM_MODULE_INVALID;
299 * Select the appropriate method or default to the
302 status = eap_method_select(inst, handler);
307 if (status == EAP_INVALID) {
309 talloc_free(handler);
310 RDEBUG2("Failed in EAP select");
311 return RLM_MODULE_INVALID;
316 * If we're doing horrible tunneling work, remember it.
318 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
319 RDEBUG2(" Not-EAP proxy set. Not composing EAP");
321 * Add the handle to the proxied list, so that we
322 * can retrieve it in the post-proxy stage, and
325 handler->inst_holder = inst;
326 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
328 rad_assert(status == 0);
329 return RLM_MODULE_HANDLED;
335 * Maybe the request was marked to be proxied. If so,
338 if (request->proxy != NULL) {
339 VALUE_PAIR *vp = NULL;
341 rad_assert(!request->proxy_reply);
344 * Add the handle to the proxied list, so that we
345 * can retrieve it in the post-proxy stage, and
348 handler->inst_holder = inst;
350 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
352 rad_assert(status == 0);
355 * Some simple sanity checks. These should really
356 * be handled by the radius library...
358 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
360 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
362 pairmake(request->proxy,
363 &request->proxy->vps,
364 "Message-Authenticator",
370 * Delete the "proxied to" attribute, as it's
371 * set to 127.0.0.1 for tunneled requests, and
372 * we don't want to tell the world that...
374 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS, TAG_ANY);
376 RDEBUG2(" Tunneled session will be proxied. Not doing EAP.");
377 return RLM_MODULE_HANDLED;
382 * We are done, wrap the EAP-request in RADIUS to send
383 * with all other required radius attributes
385 rcode = eap_compose(handler);
388 * Add to the list only if it is EAP-Request, OR if
389 * it's LEAP, and a response.
391 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
392 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) ||
395 * LEAP is a little different. At Stage 4,
396 * it sends an EAP-Success message, but we still
397 * need to keep the State attribute & session
398 * data structure around for the AP Challenge.
400 * At stage 6, LEAP sends an EAP-Response, which
401 * isn't put into the list.
403 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
404 (handler->eap_ds->response->type.num == PW_EAP_LEAP) &&
405 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
406 (handler->eap_ds->request->type.num == 0))) {
409 * Return FAIL if we can't remember the handler.
410 * This is actually disallowed by the
411 * specification, as unexpected FAILs could have
412 * been forged. However, we want to signal to
413 * everyone else involved that we are
414 * intentionally failing the session, as opposed
415 * to accidentally failing it.
417 if (!eaplist_add(inst, handler)) {
418 RDEBUG("Failed adding handler to the list");
420 talloc_free(handler);
421 return RLM_MODULE_FAIL;
425 RDEBUG2("Freeing handler");
426 /* handler is not required any more, free it now */
427 talloc_free(handler);
431 * If it's an Access-Accept, RFC 2869, Section 2.3.1
432 * says that we MUST include a User-Name attribute in the
435 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
440 * Doesn't exist, add it in.
442 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
444 vp = paircopyvp(request->reply, request->username);
445 pairadd(&request->reply->vps, vp);
449 * Cisco AP1230 has a bug and needs a zero
450 * terminated string in Access-Accept.
452 if (inst->mod_accounting_username_bug) {
453 char const *old = vp->vp_strvalue;
454 char *new = talloc_zero_array(vp, char, vp->length + 1);
456 memcpy(new, old, vp->length);
457 vp->vp_strvalue = new;
468 * EAP authorization DEPENDS on other rlm authorizations,
469 * to check for user existence & get their configured values.
470 * It Handles EAP-START Messages, User-Name initilization.
472 static rlm_rcode_t mod_authorize(void *instance, REQUEST *request)
478 inst = (rlm_eap_t *)instance;
482 * We don't do authorization again, once we've seen the
483 * proxy reply (or the proxied packet)
485 if (request->proxy != NULL)
486 return RLM_MODULE_NOOP;
490 * For EAP_START, send Access-Challenge with EAP Identity
491 * request. even when we have to proxy this request
493 * RFC 2869, Section 2.3.1 notes that the "domain" of the
494 * user, (i.e. where to proxy him) comes from the EAP-Identity,
495 * so we CANNOT proxy the user, until we know his identity.
497 * We therefore send an EAP Identity request.
499 status = eap_start(inst, request);
502 return RLM_MODULE_NOOP;
504 return RLM_MODULE_FAIL;
506 return RLM_MODULE_HANDLED;
514 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
515 * it MUST copy the identity into the User-Name attribute.
517 * But we don't worry about that too much. We depend on
518 * each EAP sub-module to look for handler->request->username,
519 * and to get excited if it doesn't appear.
521 vp = pairfind(request->config_items, PW_AUTH_TYPE, 0, TAG_ANY);
522 if ((!vp) || (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
523 vp = pairmake_config("Auth-Type", inst->xlat_name, T_OP_EQ);
525 RDEBUG2("Failed to create Auth-Type %s: %s\n",
526 inst->xlat_name, fr_strerror());
527 return RLM_MODULE_FAIL;
530 RWDEBUG2("Auth-Type already set. Not setting to EAP");
533 if (status == EAP_OK) return RLM_MODULE_OK;
535 return RLM_MODULE_UPDATED;
541 * If we're proxying EAP, then there may be magic we need
544 static rlm_rcode_t mod_post_proxy(void *inst, REQUEST *request)
550 eap_handler_t *handler;
554 * Just in case the admin lists EAP in post-proxy-type Fail.
556 if (!request->proxy_reply) return RLM_MODULE_NOOP;
559 * If there was a handler associated with this request,
560 * then it's a tunneled request which was proxied...
562 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
563 if (handler != NULL) {
565 eap_tunnel_data_t *data;
568 * Grab the tunnel callbacks from the request.
570 data = (eap_tunnel_data_t *) request_data_get(request,
572 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
574 RERROR("Failed to retrieve callback for tunneled session!");
575 talloc_free(handler);
576 return RLM_MODULE_FAIL;
582 RDEBUG2("Doing post-proxy callback");
583 rcode = data->callback(handler, data->tls_session);
586 RDEBUG2("Failed in post-proxy callback");
588 talloc_free(handler);
589 return RLM_MODULE_REJECT;
593 * We are done, wrap the EAP-request in RADIUS to send
594 * with all other required radius attributes
596 eap_compose(handler);
599 * Add to the list only if it is EAP-Request, OR if
600 * it's LEAP, and a response.
602 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
603 (handler->eap_ds->request->type.num >= PW_EAP_MD5)) {
604 if (!eaplist_add(inst, handler)) {
606 talloc_free(handler);
607 return RLM_MODULE_FAIL;
610 } else { /* couldn't have been LEAP, there's no tunnel */
611 RDEBUG2("Freeing handler");
612 /* handler is not required any more, free it now */
613 talloc_free(handler);
617 * If it's an Access-Accept, RFC 2869, Section 2.3.1
618 * says that we MUST include a User-Name attribute in the
621 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
624 * Doesn't exist, add it in.
626 vp = pairfind(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
628 pairmake_reply("User-Name",
629 request->username->vp_strvalue,
634 return RLM_MODULE_OK;
636 RDEBUG2("No pre-existing handler found");
640 * There may be more than one Cisco-AVPair.
641 * Ensure we find the one with the LEAP attribute.
643 paircursor(&cursor, &request->proxy_reply->vps);
646 * Hmm... there's got to be a better way to
647 * discover codes for vendor attributes.
649 * This is vendor Cisco (9), Cisco-AVPair
652 vp = pairfindnext(&cursor, 1, 9, TAG_ANY);
654 return RLM_MODULE_NOOP;
658 * If it's "leap:session-key", then stop.
660 * The format is VERY specific!
662 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
668 * The format is very specific.
670 if (vp->length != 17 + 34) {
671 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
672 vp->length, 17 + 34);
673 return RLM_MODULE_NOOP;
677 * Decrypt the session key, using the proxy data.
679 i = 34; /* starts off with 34 octets */
680 p = talloc_strdup(vp, vp->vp_strvalue);
681 len = rad_tunnel_pwdecode((uint8_t *)p + 17, &i,
682 request->home_server->secret,
683 request->proxy->vector);
686 * FIXME: Assert that i == 16.
690 * Encrypt the session key again, using the request data.
692 rad_tunnel_pwencode(p + 17, &len,
693 request->client->secret,
694 request->packet->vector);
695 // talloc_free(vp->vp_strvalue);
699 return RLM_MODULE_UPDATED;
703 static rlm_rcode_t mod_post_auth(void *instance, REQUEST *request)
705 rlm_eap_t *inst = instance;
707 eap_handler_t *handler;
708 eap_packet_raw_t *eap_packet;
711 * Only build a failure message if something previously rejected the request
713 vp = pairfind(request->config_items, PW_POSTAUTHTYPE, 0, TAG_ANY);
715 if (!vp || (vp->vp_integer != PW_POSTAUTHTYPE_REJECT)) return RLM_MODULE_NOOP;
717 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
718 RDEBUG2("Request didn't contain an EAP-Message, not inserting EAP-Failure");
719 return RLM_MODULE_NOOP;
722 if (pairfind(request->reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
723 RDEBUG2("Reply already contained an EAP-Message, not inserting EAP-Failure");
724 return RLM_MODULE_NOOP;
727 eap_packet = eap_vp2packet(request, request->packet->vps);
729 RERROR("Malformed EAP Message");
730 return RLM_MODULE_FAIL;
733 handler = eap_handler(inst, &eap_packet, request);
735 RDEBUG2("Failed to get handler, probably already removed, not inserting EAP-Failure");
736 return RLM_MODULE_NOOP;
739 RDEBUG2("Request was previously rejected, inserting EAP-Failure");
741 talloc_free(handler);
744 * Make sure there's a message authenticator attribute in the response
745 * RADIUS protocol code will calculate the correct value later...
747 vp = pairfind(request->reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
749 pairmake_reply("Message-Authenticator", "0x00", T_OP_EQ);
752 return RLM_MODULE_UPDATED;
756 * The module name should be the only globally exported symbol.
757 * That is, everything else should be 'static'.
762 RLM_TYPE_CHECK_CONFIG_SAFE, /* type */
765 mod_instantiate, /* instantiation */
766 mod_detach, /* detach */
768 mod_authenticate, /* authentication */
769 mod_authorize, /* authorization */
770 NULL, /* preaccounting */
771 NULL, /* accounting */
772 NULL, /* checksimul */
773 NULL, /* pre-proxy */
775 mod_post_proxy, /* post-proxy */
779 mod_post_auth /* post-auth */