2 * rlm_eap.c contains handles that are called from modules.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000-2003,2006 The FreeRADIUS server project
21 * Copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
22 * Copyright 2003 Alan DeKok <aland@freeradius.org>
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/modules.h>
33 static const CONF_PARSER module_config[] = {
34 { "default_eap_type", PW_TYPE_STRING_PTR,
35 offsetof(rlm_eap_t, default_eap_type_name), NULL, "md5" },
36 { "timer_expire", PW_TYPE_INTEGER,
37 offsetof(rlm_eap_t, timer_limit), NULL, "60"},
38 { "ignore_unknown_eap_types", PW_TYPE_BOOLEAN,
39 offsetof(rlm_eap_t, ignore_unknown_eap_types), NULL, "no" },
40 { "cisco_accounting_username_bug", PW_TYPE_BOOLEAN,
41 offsetof(rlm_eap_t, cisco_accounting_username_bug), NULL, "no" },
42 { "max_sessions", PW_TYPE_INTEGER,
43 offsetof(rlm_eap_t, max_sessions), NULL, "2048"},
45 { NULL, -1, 0, NULL, NULL } /* end the list */
49 * delete all the allocated space by eap module
51 static int eap_detach(void *instance)
56 inst = (rlm_eap_t *)instance;
58 rbtree_free(inst->session_tree);
59 if (inst->handler_tree) rbtree_free(inst->handler_tree);
60 inst->session_tree = NULL;
63 for (i = 0; i < PW_EAP_MAX_TYPES; i++) {
64 if (inst->types[i]) eaptype_free(inst->types[i]);
65 inst->types[i] = NULL;
68 pthread_mutex_destroy(&(inst->session_mutex));
69 if (fr_debug_flag) pthread_mutex_destroy(&(inst->handler_mutex));
78 * Compare two handlers.
80 static int eap_handler_cmp(const void *a, const void *b)
83 const EAP_HANDLER *one = a;
84 const EAP_HANDLER *two = b;
86 if (one->eap_id < two->eap_id) return -1;
87 if (one->eap_id > two->eap_id) return +1;
89 rcode = memcmp(one->state, two->state, sizeof(one->state));
90 if (rcode != 0) return rcode;
93 * As of 2.1.8, we don't key off of source IP. This
94 * a NAS to send packets load-balanced (or fail-over)
95 * across multiple intermediate proxies, and still have
98 if (fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr) != 0) {
99 DEBUG("WARNING: EAP packets are arriving from two different upstream servers. Has there been a proxy fail-over?");
107 * Compare two handler pointers
109 static int eap_handler_ptr_cmp(const void *a, const void *b)
111 return (((uint8_t *) a) - ((uint8_t *) b));
116 * read the config section and load all the eap authentication types present.
118 static int eap_instantiate(CONF_SECTION *cs, void **instance)
125 inst = (rlm_eap_t *) malloc(sizeof(*inst));
129 memset(inst, 0, sizeof(*inst));
130 if (cf_section_parse(cs, inst, module_config) < 0) {
136 * Create our own random pool.
138 for (i = 0; i < 256; i++) {
139 inst->rand_pool.randrsl[i] = fr_rand();
141 fr_randinit(&inst->rand_pool, 1);
142 inst->rand_pool.randcnt = 0;
144 inst->xlat_name = cf_section_name2(cs);
145 if (!inst->xlat_name) inst->xlat_name = "EAP";
147 /* Load all the configured EAP-Types */
149 for(scs=cf_subsection_find_next(cs, NULL, NULL);
151 scs=cf_subsection_find_next(cs, scs, NULL)) {
153 const char *auth_type;
155 auth_type = cf_section_name1(scs);
157 if (!auth_type) continue;
159 eap_type = eaptype_name2type(auth_type);
161 radlog(L_ERR, "rlm_eap: Unknown EAP type %s",
167 #ifndef HAVE_OPENSSL_SSL_H
169 * This allows the default configuration to be
170 * shipped with EAP-TLS, etc. enabled. If the
171 * system doesn't have OpenSSL, they will be
174 * If the system does have OpenSSL, then this
175 * code will not be used. The administrator will
176 * then have to delete the tls,
177 * etc. configurations from eap.conf in order to
178 * have EAP without the TLS types.
180 if ((eap_type == PW_EAP_TLS) ||
181 (eap_type == PW_EAP_TTLS) ||
182 (eap_type == PW_EAP_PEAP)) {
183 DEBUG2("Ignoring EAP-Type/%s because we do not have OpenSSL support.", auth_type);
189 * If we're asked to load TTLS or PEAP, ensure
190 * that we've first loaded TLS.
192 if (((eap_type == PW_EAP_TTLS) ||
193 (eap_type == PW_EAP_PEAP)) &&
194 (inst->types[PW_EAP_TLS] == NULL)) {
195 radlog(L_ERR, "rlm_eap: Unable to load EAP-Type/%s, as EAP-Type/TLS is required first.",
203 if (eaptype_load(&inst->types[eap_type], eap_type, scs) < 0) {
208 num_types++; /* successfully loaded one more types */
211 if (num_types == 0) {
212 radlog(L_ERR|L_CONS, "rlm_eap: No EAP type configured, module cannot do anything.");
218 * Ensure that the default EAP type is loaded.
220 eap_type = eaptype_name2type(inst->default_eap_type_name);
222 radlog(L_ERR|L_CONS, "rlm_eap: Unknown default EAP type %s",
223 inst->default_eap_type_name);
228 if (inst->types[eap_type] == NULL) {
229 radlog(L_ERR|L_CONS, "rlm_eap: No such sub-type for default EAP type %s",
230 inst->default_eap_type_name);
234 inst->default_eap_type = eap_type; /* save the numerical type */
237 * List of sessions are set to NULL by the memset
242 * Lookup sessions in the tree. We don't free them in
243 * the tree, as that's taken care of elsewhere...
245 inst->session_tree = rbtree_create(eap_handler_cmp, NULL, 0);
246 if (!inst->session_tree) {
247 radlog(L_ERR|L_CONS, "rlm_eap: Cannot initialize tree");
253 inst->handler_tree = rbtree_create(eap_handler_ptr_cmp, NULL, 0);
254 if (!inst->handler_tree) {
255 radlog(L_ERR|L_CONS, "rlm_eap: Cannot initialize tree");
260 if (pthread_mutex_init(&(inst->handler_mutex), NULL) < 0) {
261 radlog(L_ERR|L_CONS, "rlm_eap: Failed initializing mutex: %s", strerror(errno));
267 if (pthread_mutex_init(&(inst->session_mutex), NULL) < 0) {
268 radlog(L_ERR|L_CONS, "rlm_eap: Failed initializing mutex: %s", strerror(errno));
279 * For backwards compatibility.
281 static int eap_authenticate(void *instance, REQUEST *request)
284 EAP_HANDLER *handler;
285 eap_packet_t *eap_packet;
288 inst = (rlm_eap_t *) instance;
290 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0)) {
291 RDEBUG("ERROR: You set 'Auth-Type = EAP' for a request that does not contain an EAP-Message attribute!");
292 return RLM_MODULE_INVALID;
296 * Get the eap packet to start with
298 eap_packet = eap_vp2packet(request->packet->vps);
299 if (eap_packet == NULL) {
300 radlog_request(L_ERR, 0, request, "Malformed EAP Message");
301 return RLM_MODULE_FAIL;
305 * Create the eap handler. The eap_packet will end up being
306 * "swallowed" into the handler, so we can't access it after
309 handler = eap_handler(inst, &eap_packet, request);
310 if (handler == NULL) {
311 RDEBUG2("Failed in handler");
312 return RLM_MODULE_INVALID;
316 * Select the appropriate eap_type or default to the
319 rcode = eaptype_select(inst, handler);
324 if (rcode == EAP_INVALID) {
326 eap_handler_free(inst, handler);
327 RDEBUG2("Failed in EAP select");
328 return RLM_MODULE_INVALID;
333 * If we're doing horrible tunneling work, remember it.
335 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
336 RDEBUG2(" Not-EAP proxy set. Not composing EAP");
338 * Add the handle to the proxied list, so that we
339 * can retrieve it in the post-proxy stage, and
342 rcode = request_data_add(request,
343 inst, REQUEST_DATA_EAP_HANDLER,
345 (void *) eap_handler_free);
346 rad_assert(rcode == 0);
348 return RLM_MODULE_HANDLED;
354 * Maybe the request was marked to be proxied. If so,
357 if (request->proxy != NULL) {
358 VALUE_PAIR *vp = NULL;
360 rad_assert(request->proxy_reply == NULL);
363 * Add the handle to the proxied list, so that we
364 * can retrieve it in the post-proxy stage, and
367 rcode = request_data_add(request,
368 inst, REQUEST_DATA_EAP_HANDLER,
370 (void *) eap_handler_free);
371 rad_assert(rcode == 0);
374 * Some simple sanity checks. These should really
375 * be handled by the radius library...
377 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE, 0);
379 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0);
381 vp = pairmake("Message-Authenticator",
383 rad_assert(vp != NULL);
384 pairadd(&(request->proxy->vps), vp);
389 * Delete the "proxied to" attribute, as it's
390 * set to 127.0.0.1 for tunneled requests, and
391 * we don't want to tell the world that...
393 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS);
395 RDEBUG2(" Tunneled session will be proxied. Not doing EAP.");
396 return RLM_MODULE_HANDLED;
401 * We are done, wrap the EAP-request in RADIUS to send
402 * with all other required radius attributes
404 rcode = eap_compose(handler);
407 * Add to the list only if it is EAP-Request, OR if
408 * it's LEAP, and a response.
410 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
411 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) ||
414 * LEAP is a little different. At Stage 4,
415 * it sends an EAP-Success message, but we still
416 * need to keep the State attribute & session
417 * data structure around for the AP Challenge.
419 * At stage 6, LEAP sends an EAP-Response, which
420 * isn't put into the list.
422 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
423 (handler->eap_ds->response->type.type == PW_EAP_LEAP) &&
424 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
425 (handler->eap_ds->request->type.type == 0))) {
428 * Return FAIL if we can't remember the handler.
429 * This is actually disallowed by the
430 * specification, as unexpected FAILs could have
431 * been forged. However, we want to signal to
432 * everyone else involved that we are
433 * intentionally failing the session, as opposed
434 * to accidentally failing it.
436 if (!eaplist_add(inst, handler)) {
438 eap_handler_free(inst, handler);
439 return RLM_MODULE_FAIL;
443 RDEBUG2("Freeing handler");
444 /* handler is not required any more, free it now */
445 eap_handler_free(inst, handler);
449 * If it's an Access-Accept, RFC 2869, Section 2.3.1
450 * says that we MUST include a User-Name attribute in the
453 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
458 * Doesn't exist, add it in.
460 vp = pairfind(request->reply->vps, PW_USER_NAME, 0);
462 vp = pairmake("User-Name", "",
464 strlcpy(vp->vp_strvalue, request->username->vp_strvalue,
465 sizeof(vp->vp_strvalue));
466 vp->length = request->username->length;
467 rad_assert(vp != NULL);
468 pairadd(&(request->reply->vps), vp);
472 * Cisco AP1230 has a bug and needs a zero
473 * terminated string in Access-Accept.
475 if ((inst->cisco_accounting_username_bug) &&
476 (vp->length < (int) sizeof(vp->vp_strvalue))) {
477 vp->vp_strvalue[vp->length] = '\0';
486 * EAP authorization DEPENDS on other rlm authorizations,
487 * to check for user existance & get their configured values.
488 * It Handles EAP-START Messages, User-Name initilization.
490 static int eap_authorize(void *instance, REQUEST *request)
496 inst = (rlm_eap_t *)instance;
500 * We don't do authorization again, once we've seen the
501 * proxy reply (or the proxied packet)
503 if (request->proxy != NULL)
504 return RLM_MODULE_NOOP;
508 * For EAP_START, send Access-Challenge with EAP Identity
509 * request. even when we have to proxy this request
511 * RFC 2869, Section 2.3.1 notes that the "domain" of the
512 * user, (i.e. where to proxy him) comes from the EAP-Identity,
513 * so we CANNOT proxy the user, until we know his identity.
515 * We therefore send an EAP Identity request.
517 status = eap_start(inst, request);
520 return RLM_MODULE_NOOP;
522 return RLM_MODULE_FAIL;
524 return RLM_MODULE_HANDLED;
532 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
533 * it MUST copy the identity into the User-Name attribute.
535 * But we don't worry about that too much. We depend on
536 * each EAP sub-module to look for handler->request->username,
537 * and to get excited if it doesn't appear.
540 vp = pairfind(request->config_items, PW_AUTH_TYPE, 0);
542 (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
543 vp = pairmake("Auth-Type", inst->xlat_name, T_OP_EQ);
545 RDEBUG2("Failed to create Auth-Type %s: %s\n",
546 inst->xlat_name, fr_strerror());
547 return RLM_MODULE_FAIL;
549 pairadd(&request->config_items, vp);
551 RDEBUG2("WARNING: Auth-Type already set. Not setting to EAP");
554 if (status == EAP_OK) return RLM_MODULE_OK;
556 return RLM_MODULE_UPDATED;
562 * If we're proxying EAP, then there may be magic we need
565 static int eap_post_proxy(void *inst, REQUEST *request)
570 EAP_HANDLER *handler;
573 * Just in case the admin lists EAP in post-proxy-type Fail.
575 if (!request->proxy_reply) return RLM_MODULE_NOOP;
578 * If there was a handler associated with this request,
579 * then it's a tunneled request which was proxied...
581 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
582 if (handler != NULL) {
584 eap_tunnel_data_t *data;
587 * Grab the tunnel callbacks from the request.
589 data = (eap_tunnel_data_t *) request_data_get(request,
591 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
593 radlog_request(L_ERR, 0, request, "Failed to retrieve callback for tunneled session!");
594 eap_handler_free(inst, handler);
595 return RLM_MODULE_FAIL;
601 RDEBUG2("Doing post-proxy callback");
602 rcode = data->callback(handler, data->tls_session);
605 RDEBUG2("Failed in post-proxy callback");
607 eap_handler_free(inst, handler);
608 return RLM_MODULE_REJECT;
612 * We are done, wrap the EAP-request in RADIUS to send
613 * with all other required radius attributes
615 eap_compose(handler);
618 * Add to the list only if it is EAP-Request, OR if
619 * it's LEAP, and a response.
621 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
622 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
623 if (!eaplist_add(inst, handler)) {
625 eap_handler_free(inst, handler);
626 return RLM_MODULE_FAIL;
629 } else { /* couldn't have been LEAP, there's no tunnel */
630 RDEBUG2("Freeing handler");
631 /* handler is not required any more, free it now */
632 eap_handler_free(inst, handler);
636 * If it's an Access-Accept, RFC 2869, Section 2.3.1
637 * says that we MUST include a User-Name attribute in the
640 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
643 * Doesn't exist, add it in.
645 vp = pairfind(request->reply->vps, PW_USER_NAME, 0);
647 vp = pairmake("User-Name", request->username->vp_strvalue,
649 rad_assert(vp != NULL);
650 pairadd(&(request->reply->vps), vp);
654 return RLM_MODULE_OK;
656 RDEBUG2("No pre-existing handler found");
660 * There may be more than one Cisco-AVPair.
661 * Ensure we find the one with the LEAP attribute.
663 vp = request->proxy_reply->vps;
666 * Hmm... there's got to be a better way to
667 * discover codes for vendor attributes.
669 * This is vendor Cisco (9), Cisco-AVPair
672 vp = pairfind(vp, 1, 9);
674 return RLM_MODULE_NOOP;
678 * If it's "leap:session-key", then stop.
680 * The format is VERY specific!
682 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
687 * Not this AV-pair. Go to the next one.
693 * The format is very specific.
695 if (vp->length != 17 + 34) {
696 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
697 vp->length, 17 + 34);
698 return RLM_MODULE_NOOP;
702 * Decrypt the session key, using the proxy data.
704 i = 34; /* starts off with 34 octets */
705 len = rad_tunnel_pwdecode(vp->vp_octets + 17, &i,
706 request->home_server->secret,
707 request->proxy->vector);
710 * FIXME: Assert that i == 16.
714 * Encrypt the session key again, using the request data.
716 rad_tunnel_pwencode(vp->vp_strvalue + 17, &len,
717 request->client->secret,
718 request->packet->vector);
720 return RLM_MODULE_UPDATED;
725 * The module name should be the only globally exported symbol.
726 * That is, everything else should be 'static'.
731 RLM_TYPE_CHECK_CONFIG_SAFE, /* type */
732 eap_instantiate, /* instantiation */
733 eap_detach, /* detach */
735 eap_authenticate, /* authentication */
736 eap_authorize, /* authorization */
737 NULL, /* preaccounting */
738 NULL, /* accounting */
739 NULL, /* checksimul */
740 NULL, /* pre-proxy */
742 eap_post_proxy, /* post-proxy */