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;
261 * Get the eap packet to start with
263 eap_packet = eap_vp2packet(request->packet->vps);
264 if (eap_packet == NULL) {
265 radlog_request(L_ERR, 0, request, "Malformed EAP Message");
266 return RLM_MODULE_FAIL;
270 * Create the eap handler. The eap_packet will end up being
271 * "swallowed" into the handler, so we can't access it after
274 handler = eap_handler(inst, &eap_packet, request);
275 if (handler == NULL) {
276 RDEBUG2("Failed in handler");
277 return RLM_MODULE_INVALID;
281 * Select the appropriate eap_type or default to the
284 rcode = eaptype_select(inst, handler);
289 if (rcode == EAP_INVALID) {
291 eap_handler_free(handler);
292 RDEBUG2("Failed in EAP select");
293 return RLM_MODULE_INVALID;
298 * If we're doing horrible tunneling work, remember it.
300 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
301 RDEBUG2(" Not-EAP proxy set. Not composing EAP");
303 * Add the handle to the proxied list, so that we
304 * can retrieve it in the post-proxy stage, and
307 rcode = request_data_add(request,
308 inst, REQUEST_DATA_EAP_HANDLER,
310 (void *) eap_handler_free);
311 rad_assert(rcode == 0);
313 return RLM_MODULE_HANDLED;
319 * Maybe the request was marked to be proxied. If so,
322 if (request->proxy != NULL) {
323 VALUE_PAIR *vp = NULL;
325 rad_assert(request->proxy_reply == NULL);
328 * Add the handle to the proxied list, so that we
329 * can retrieve it in the post-proxy stage, and
332 rcode = request_data_add(request,
333 inst, REQUEST_DATA_EAP_HANDLER,
335 (void *) eap_handler_free);
336 rad_assert(rcode == 0);
339 * Some simple sanity checks. These should really
340 * be handled by the radius library...
342 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE, 0);
344 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0);
346 vp = pairmake("Message-Authenticator",
348 rad_assert(vp != NULL);
349 pairadd(&(request->proxy->vps), vp);
354 * Delete the "proxied to" attribute, as it's
355 * set to 127.0.0.1 for tunneled requests, and
356 * we don't want to tell the world that...
358 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS);
360 RDEBUG2(" Tunneled session will be proxied. Not doing EAP.");
361 return RLM_MODULE_HANDLED;
366 * We are done, wrap the EAP-request in RADIUS to send
367 * with all other required radius attributes
369 rcode = eap_compose(handler);
372 * Add to the list only if it is EAP-Request, OR if
373 * it's LEAP, and a response.
375 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
376 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) ||
379 * LEAP is a little different. At Stage 4,
380 * it sends an EAP-Success message, but we still
381 * need to keep the State attribute & session
382 * data structure around for the AP Challenge.
384 * At stage 6, LEAP sends an EAP-Response, which
385 * isn't put into the list.
387 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
388 (handler->eap_ds->response->type.type == PW_EAP_LEAP) &&
389 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
390 (handler->eap_ds->request->type.type == 0))) {
393 * Return FAIL if we can't remember the handler.
394 * This is actually disallowed by the
395 * specification, as unexpected FAILs could have
396 * been forged. However, we want to signal to
397 * everyone else involved that we are
398 * intentionally failing the session, as opposed
399 * to accidentally failing it.
401 if (!eaplist_add(inst, handler)) {
403 eap_handler_free(handler);
404 return RLM_MODULE_FAIL;
408 RDEBUG2("Freeing handler");
409 /* handler is not required any more, free it now */
410 eap_handler_free(handler);
414 * If it's an Access-Accept, RFC 2869, Section 2.3.1
415 * says that we MUST include a User-Name attribute in the
418 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
423 * Doesn't exist, add it in.
425 vp = pairfind(request->reply->vps, PW_USER_NAME, 0);
427 vp = pairmake("User-Name", "",
429 strlcpy(vp->vp_strvalue, request->username->vp_strvalue,
430 sizeof(vp->vp_strvalue));
431 vp->length = request->username->length;
432 rad_assert(vp != NULL);
433 pairadd(&(request->reply->vps), vp);
437 * Cisco AP1230 has a bug and needs a zero
438 * terminated string in Access-Accept.
440 if ((inst->cisco_accounting_username_bug) &&
441 (vp->length < (int) sizeof(vp->vp_strvalue))) {
442 vp->vp_strvalue[vp->length] = '\0';
451 * EAP authorization DEPENDS on other rlm authorizations,
452 * to check for user existance & get their configured values.
453 * It Handles EAP-START Messages, User-Name initilization.
455 static int eap_authorize(void *instance, REQUEST *request)
461 inst = (rlm_eap_t *)instance;
465 * We don't do authorization again, once we've seen the
466 * proxy reply (or the proxied packet)
468 if (request->proxy != NULL)
469 return RLM_MODULE_NOOP;
473 * For EAP_START, send Access-Challenge with EAP Identity
474 * request. even when we have to proxy this request
476 * RFC 2869, Section 2.3.1 notes that the "domain" of the
477 * user, (i.e. where to proxy him) comes from the EAP-Identity,
478 * so we CANNOT proxy the user, until we know his identity.
480 * We therefore send an EAP Identity request.
482 status = eap_start(inst, request);
485 return RLM_MODULE_NOOP;
487 return RLM_MODULE_FAIL;
489 return RLM_MODULE_HANDLED;
497 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
498 * it MUST copy the identity into the User-Name attribute.
500 * But we don't worry about that too much. We depend on
501 * each EAP sub-module to look for handler->request->username,
502 * and to get excited if it doesn't appear.
505 vp = pairfind(request->config_items, PW_AUTH_TYPE, 0);
507 (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
508 vp = pairmake("Auth-Type", inst->xlat_name, T_OP_EQ);
510 RDEBUG2("Failed to create Auth-Type %s: %s\n",
511 inst->xlat_name, fr_strerror());
512 return RLM_MODULE_FAIL;
514 pairadd(&request->config_items, vp);
517 if (status == EAP_OK) return RLM_MODULE_OK;
519 return RLM_MODULE_UPDATED;
525 * If we're proxying EAP, then there may be magic we need
528 static int eap_post_proxy(void *inst, REQUEST *request)
533 EAP_HANDLER *handler;
536 * Just in case the admin lists EAP in post-proxy-type Fail.
538 if (!request->proxy_reply) return RLM_MODULE_NOOP;
541 * If there was a handler associated with this request,
542 * then it's a tunneled request which was proxied...
544 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
545 if (handler != NULL) {
547 eap_tunnel_data_t *data;
550 * Grab the tunnel callbacks from the request.
552 data = (eap_tunnel_data_t *) request_data_get(request,
554 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
556 radlog_request(L_ERR, 0, request, "Failed to retrieve callback for tunneled session!");
557 eap_handler_free(handler);
558 return RLM_MODULE_FAIL;
564 RDEBUG2("Doing post-proxy callback");
565 rcode = data->callback(handler, data->tls_session);
568 RDEBUG2("Failed in post-proxy callback");
570 eap_handler_free(handler);
571 return RLM_MODULE_REJECT;
575 * We are done, wrap the EAP-request in RADIUS to send
576 * with all other required radius attributes
578 eap_compose(handler);
581 * Add to the list only if it is EAP-Request, OR if
582 * it's LEAP, and a response.
584 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
585 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
586 if (!eaplist_add(inst, handler)) {
588 eap_handler_free(handler);
589 return RLM_MODULE_FAIL;
592 } else { /* couldn't have been LEAP, there's no tunnel */
593 RDEBUG2("Freeing handler");
594 /* handler is not required any more, free it now */
595 eap_handler_free(handler);
599 * If it's an Access-Accept, RFC 2869, Section 2.3.1
600 * says that we MUST include a User-Name attribute in the
603 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
606 * Doesn't exist, add it in.
608 vp = pairfind(request->reply->vps, PW_USER_NAME, 0);
610 vp = pairmake("User-Name", request->username->vp_strvalue,
612 rad_assert(vp != NULL);
613 pairadd(&(request->reply->vps), vp);
617 return RLM_MODULE_OK;
619 RDEBUG2("No pre-existing handler found");
623 * There may be more than one Cisco-AVPair.
624 * Ensure we find the one with the LEAP attribute.
626 vp = request->proxy_reply->vps;
629 * Hmm... there's got to be a better way to
630 * discover codes for vendor attributes.
632 * This is vendor Cisco (9), Cisco-AVPair
635 vp = pairfind(vp, 1, 9);
637 return RLM_MODULE_NOOP;
641 * If it's "leap:session-key", then stop.
643 * The format is VERY specific!
645 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
650 * Not this AV-pair. Go to the next one.
656 * The format is very specific.
658 if (vp->length != 17 + 34) {
659 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
660 vp->length, 17 + 34);
661 return RLM_MODULE_NOOP;
665 * Decrypt the session key, using the proxy data.
667 i = 34; /* starts off with 34 octets */
668 len = rad_tunnel_pwdecode(vp->vp_octets + 17, &i,
669 request->home_server->secret,
670 request->proxy->vector);
673 * FIXME: Assert that i == 16.
677 * Encrypt the session key again, using the request data.
679 rad_tunnel_pwencode(vp->vp_strvalue + 17, &len,
680 request->client->secret,
681 request->packet->vector);
683 return RLM_MODULE_UPDATED;
688 * The module name should be the only globally exported symbol.
689 * That is, everything else should be 'static'.
694 RLM_TYPE_CHECK_CONFIG_SAFE, /* type */
695 eap_instantiate, /* instantiation */
696 eap_detach, /* detach */
698 eap_authenticate, /* authentication */
699 eap_authorize, /* authorization */
700 NULL, /* preaccounting */
701 NULL, /* accounting */
702 NULL, /* checksimul */
703 NULL, /* pre-proxy */
705 eap_post_proxy, /* post-proxy */