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 inst->session_tree = NULL;
62 for (i = 0; i < PW_EAP_MAX_TYPES; i++) {
63 if (inst->types[i]) eaptype_free(inst->types[i]);
64 inst->types[i] = NULL;
67 pthread_mutex_destroy(&(inst->session_mutex));
76 * Compare two handlers.
78 static int eap_handler_cmp(const void *a, const void *b)
81 const EAP_HANDLER *one = a;
82 const EAP_HANDLER *two = b;
84 if (one->eap_id < two->eap_id) return -1;
85 if (one->eap_id > two->eap_id) return +1;
87 rcode = memcmp(one->state, two->state, sizeof(one->state));
88 if (rcode != 0) return rcode;
91 * As of 2.1.8, we don't key off of source IP. This
92 * a NAS to send packets load-balanced (or fail-over)
93 * across multiple intermediate proxies, and still have
96 if (fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr) != 0) {
97 DEBUG("WARNING: EAP packets are arriving from two different upstream servers. Has there been a proxy fail-over?");
105 * read the config section and load all the eap authentication types present.
107 static int eap_instantiate(CONF_SECTION *cs, void **instance)
114 inst = (rlm_eap_t *) malloc(sizeof(*inst));
118 memset(inst, 0, sizeof(*inst));
119 if (cf_section_parse(cs, inst, module_config) < 0) {
125 * Create our own random pool.
127 for (i = 0; i < 256; i++) {
128 inst->rand_pool.randrsl[i] = fr_rand();
130 fr_randinit(&inst->rand_pool, 1);
131 inst->rand_pool.randcnt = 0;
133 inst->xlat_name = cf_section_name2(cs);
134 if (!inst->xlat_name) inst->xlat_name = "EAP";
136 /* Load all the configured EAP-Types */
138 for(scs=cf_subsection_find_next(cs, NULL, NULL);
140 scs=cf_subsection_find_next(cs, scs, NULL)) {
142 const char *auth_type;
144 auth_type = cf_section_name1(scs);
146 if (!auth_type) continue;
148 eap_type = eaptype_name2type(auth_type);
150 radlog(L_ERR, "rlm_eap: Unknown EAP type %s",
156 #ifndef HAVE_OPENSSL_SSL_H
158 * This allows the default configuration to be
159 * shipped with EAP-TLS, etc. enabled. If the
160 * system doesn't have OpenSSL, they will be
163 * If the system does have OpenSSL, then this
164 * code will not be used. The administrator will
165 * then have to delete the tls,
166 * etc. configurations from eap.conf in order to
167 * have EAP without the TLS types.
169 if ((eap_type == PW_EAP_TLS) ||
170 (eap_type == PW_EAP_TTLS) ||
171 (eap_type == PW_EAP_PEAP)) {
172 DEBUG2("Ignoring EAP-Type/%s because we do not have OpenSSL support.", auth_type);
178 * If we're asked to load TTLS or PEAP, ensure
179 * that we've first loaded TLS.
181 if (((eap_type == PW_EAP_TTLS) ||
182 (eap_type == PW_EAP_PEAP)) &&
183 (inst->types[PW_EAP_TLS] == NULL)) {
184 radlog(L_ERR, "rlm_eap: Unable to load EAP-Type/%s, as EAP-Type/TLS is required first.",
192 if (eaptype_load(&inst->types[eap_type], eap_type, scs) < 0) {
197 num_types++; /* successfully loaded one more types */
200 if (num_types == 0) {
201 radlog(L_ERR|L_CONS, "rlm_eap: No EAP type configured, module cannot do anything.");
207 * Ensure that the default EAP type is loaded.
209 eap_type = eaptype_name2type(inst->default_eap_type_name);
211 radlog(L_ERR|L_CONS, "rlm_eap: Unknown default EAP type %s",
212 inst->default_eap_type_name);
217 if (inst->types[eap_type] == NULL) {
218 radlog(L_ERR|L_CONS, "rlm_eap: No such sub-type for default EAP type %s",
219 inst->default_eap_type_name);
223 inst->default_eap_type = eap_type; /* save the numerical type */
226 * List of sessions are set to NULL by the memset
231 * Lookup sessions in the tree. We don't free them in
232 * the tree, as that's taken care of elsewhere...
234 inst->session_tree = rbtree_create(eap_handler_cmp, NULL, 0);
235 if (!inst->session_tree) {
236 radlog(L_ERR|L_CONS, "rlm_eap: Cannot initialize tree");
241 pthread_mutex_init(&(inst->session_mutex), NULL);
249 * For backwards compatibility.
251 static int eap_authenticate(void *instance, REQUEST *request)
254 EAP_HANDLER *handler;
255 eap_packet_t *eap_packet;
258 inst = (rlm_eap_t *) instance;
260 if (!pairfind(request->packet->vps, PW_EAP_MESSAGE, 0)) {
261 RDEBUG("ERROR: You set 'Auth-Type = EAP' for a request that does not contain an EAP-Message attribute!");
262 return RLM_MODULE_INVALID;
266 * Get the eap packet to start with
268 eap_packet = eap_vp2packet(request->packet->vps);
269 if (eap_packet == NULL) {
270 radlog_request(L_ERR, 0, request, "Malformed EAP Message");
271 return RLM_MODULE_FAIL;
275 * Create the eap handler. The eap_packet will end up being
276 * "swallowed" into the handler, so we can't access it after
279 handler = eap_handler(inst, &eap_packet, request);
280 if (handler == NULL) {
281 RDEBUG2("Failed in handler");
282 return RLM_MODULE_INVALID;
286 * Select the appropriate eap_type or default to the
289 rcode = eaptype_select(inst, handler);
294 if (rcode == EAP_INVALID) {
296 eap_handler_free(handler);
297 RDEBUG2("Failed in EAP select");
298 return RLM_MODULE_INVALID;
303 * If we're doing horrible tunneling work, remember it.
305 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
306 RDEBUG2(" Not-EAP proxy set. Not composing EAP");
308 * Add the handle to the proxied list, so that we
309 * can retrieve it in the post-proxy stage, and
312 rcode = request_data_add(request,
313 inst, REQUEST_DATA_EAP_HANDLER,
315 (void *) eap_handler_free);
316 rad_assert(rcode == 0);
318 return RLM_MODULE_HANDLED;
324 * Maybe the request was marked to be proxied. If so,
327 if (request->proxy != NULL) {
328 VALUE_PAIR *vp = NULL;
330 rad_assert(request->proxy_reply == NULL);
333 * Add the handle to the proxied list, so that we
334 * can retrieve it in the post-proxy stage, and
337 rcode = request_data_add(request,
338 inst, REQUEST_DATA_EAP_HANDLER,
340 (void *) eap_handler_free);
341 rad_assert(rcode == 0);
344 * Some simple sanity checks. These should really
345 * be handled by the radius library...
347 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE, 0);
349 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0);
351 vp = pairmake("Message-Authenticator",
353 rad_assert(vp != NULL);
354 pairadd(&(request->proxy->vps), vp);
359 * Delete the "proxied to" attribute, as it's
360 * set to 127.0.0.1 for tunneled requests, and
361 * we don't want to tell the world that...
363 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS);
365 RDEBUG2(" Tunneled session will be proxied. Not doing EAP.");
366 return RLM_MODULE_HANDLED;
371 * We are done, wrap the EAP-request in RADIUS to send
372 * with all other required radius attributes
374 rcode = eap_compose(handler);
377 * Add to the list only if it is EAP-Request, OR if
378 * it's LEAP, and a response.
380 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
381 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) ||
384 * LEAP is a little different. At Stage 4,
385 * it sends an EAP-Success message, but we still
386 * need to keep the State attribute & session
387 * data structure around for the AP Challenge.
389 * At stage 6, LEAP sends an EAP-Response, which
390 * isn't put into the list.
392 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
393 (handler->eap_ds->response->type.type == PW_EAP_LEAP) &&
394 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
395 (handler->eap_ds->request->type.type == 0))) {
398 * Return FAIL if we can't remember the handler.
399 * This is actually disallowed by the
400 * specification, as unexpected FAILs could have
401 * been forged. However, we want to signal to
402 * everyone else involved that we are
403 * intentionally failing the session, as opposed
404 * to accidentally failing it.
406 if (!eaplist_add(inst, handler)) {
408 eap_handler_free(handler);
409 return RLM_MODULE_FAIL;
413 RDEBUG2("Freeing handler");
414 /* handler is not required any more, free it now */
415 eap_handler_free(handler);
419 * If it's an Access-Accept, RFC 2869, Section 2.3.1
420 * says that we MUST include a User-Name attribute in the
423 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
428 * Doesn't exist, add it in.
430 vp = pairfind(request->reply->vps, PW_USER_NAME, 0);
432 vp = pairmake("User-Name", "",
434 strlcpy(vp->vp_strvalue, request->username->vp_strvalue,
435 sizeof(vp->vp_strvalue));
436 vp->length = request->username->length;
437 rad_assert(vp != NULL);
438 pairadd(&(request->reply->vps), vp);
442 * Cisco AP1230 has a bug and needs a zero
443 * terminated string in Access-Accept.
445 if ((inst->cisco_accounting_username_bug) &&
446 (vp->length < (int) sizeof(vp->vp_strvalue))) {
447 vp->vp_strvalue[vp->length] = '\0';
456 * EAP authorization DEPENDS on other rlm authorizations,
457 * to check for user existance & get their configured values.
458 * It Handles EAP-START Messages, User-Name initilization.
460 static int eap_authorize(void *instance, REQUEST *request)
466 inst = (rlm_eap_t *)instance;
470 * We don't do authorization again, once we've seen the
471 * proxy reply (or the proxied packet)
473 if (request->proxy != NULL)
474 return RLM_MODULE_NOOP;
478 * For EAP_START, send Access-Challenge with EAP Identity
479 * request. even when we have to proxy this request
481 * RFC 2869, Section 2.3.1 notes that the "domain" of the
482 * user, (i.e. where to proxy him) comes from the EAP-Identity,
483 * so we CANNOT proxy the user, until we know his identity.
485 * We therefore send an EAP Identity request.
487 status = eap_start(inst, request);
490 return RLM_MODULE_NOOP;
492 return RLM_MODULE_FAIL;
494 return RLM_MODULE_HANDLED;
502 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
503 * it MUST copy the identity into the User-Name attribute.
505 * But we don't worry about that too much. We depend on
506 * each EAP sub-module to look for handler->request->username,
507 * and to get excited if it doesn't appear.
510 vp = pairfind(request->config_items, PW_AUTH_TYPE, 0);
512 (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
513 vp = pairmake("Auth-Type", inst->xlat_name, T_OP_EQ);
515 RDEBUG2("Failed to create Auth-Type %s: %s\n",
516 inst->xlat_name, fr_strerror());
517 return RLM_MODULE_FAIL;
519 pairadd(&request->config_items, vp);
521 RDEBUG2("WARNING: Auth-Type already set. Not setting to EAP");
524 if (status == EAP_OK) return RLM_MODULE_OK;
526 return RLM_MODULE_UPDATED;
532 * If we're proxying EAP, then there may be magic we need
535 static int eap_post_proxy(void *inst, REQUEST *request)
540 EAP_HANDLER *handler;
543 * Just in case the admin lists EAP in post-proxy-type Fail.
545 if (!request->proxy_reply) return RLM_MODULE_NOOP;
548 * If there was a handler associated with this request,
549 * then it's a tunneled request which was proxied...
551 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
552 if (handler != NULL) {
554 eap_tunnel_data_t *data;
557 * Grab the tunnel callbacks from the request.
559 data = (eap_tunnel_data_t *) request_data_get(request,
561 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
563 radlog_request(L_ERR, 0, request, "Failed to retrieve callback for tunneled session!");
564 eap_handler_free(handler);
565 return RLM_MODULE_FAIL;
571 RDEBUG2("Doing post-proxy callback");
572 rcode = data->callback(handler, data->tls_session);
575 RDEBUG2("Failed in post-proxy callback");
577 eap_handler_free(handler);
578 return RLM_MODULE_REJECT;
582 * We are done, wrap the EAP-request in RADIUS to send
583 * with all other required radius attributes
585 eap_compose(handler);
588 * Add to the list only if it is EAP-Request, OR if
589 * it's LEAP, and a response.
591 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
592 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
593 if (!eaplist_add(inst, handler)) {
595 eap_handler_free(handler);
596 return RLM_MODULE_FAIL;
599 } else { /* couldn't have been LEAP, there's no tunnel */
600 RDEBUG2("Freeing handler");
601 /* handler is not required any more, free it now */
602 eap_handler_free(handler);
606 * If it's an Access-Accept, RFC 2869, Section 2.3.1
607 * says that we MUST include a User-Name attribute in the
610 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
613 * Doesn't exist, add it in.
615 vp = pairfind(request->reply->vps, PW_USER_NAME, 0);
617 vp = pairmake("User-Name", request->username->vp_strvalue,
619 rad_assert(vp != NULL);
620 pairadd(&(request->reply->vps), vp);
624 return RLM_MODULE_OK;
626 RDEBUG2("No pre-existing handler found");
630 * There may be more than one Cisco-AVPair.
631 * Ensure we find the one with the LEAP attribute.
633 vp = request->proxy_reply->vps;
636 * Hmm... there's got to be a better way to
637 * discover codes for vendor attributes.
639 * This is vendor Cisco (9), Cisco-AVPair
642 vp = pairfind(vp, 1, 9);
644 return RLM_MODULE_NOOP;
648 * If it's "leap:session-key", then stop.
650 * The format is VERY specific!
652 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
657 * Not this AV-pair. Go to the next one.
663 * The format is very specific.
665 if (vp->length != 17 + 34) {
666 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
667 vp->length, 17 + 34);
668 return RLM_MODULE_NOOP;
672 * Decrypt the session key, using the proxy data.
674 i = 34; /* starts off with 34 octets */
675 len = rad_tunnel_pwdecode(vp->vp_octets + 17, &i,
676 request->home_server->secret,
677 request->proxy->vector);
680 * FIXME: Assert that i == 16.
684 * Encrypt the session key again, using the request data.
686 rad_tunnel_pwencode(vp->vp_strvalue + 17, &len,
687 request->client->secret,
688 request->packet->vector);
690 return RLM_MODULE_UPDATED;
695 * The module name should be the only globally exported symbol.
696 * That is, everything else should be 'static'.
701 RLM_TYPE_CHECK_CONFIG_SAFE, /* type */
702 eap_instantiate, /* instantiation */
703 eap_detach, /* detach */
705 eap_authenticate, /* authentication */
706 eap_authorize, /* authorization */
707 NULL, /* preaccounting */
708 NULL, /* accounting */
709 NULL, /* checksimul */
710 NULL, /* pre-proxy */
712 eap_post_proxy, /* post-proxy */