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 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/autoconf.h>
27 #include <freeradius-devel/modules.h>
29 static const char rcsid[] = "$Id$";
31 static const CONF_PARSER module_config[] = {
32 { "default_eap_type", PW_TYPE_STRING_PTR,
33 offsetof(rlm_eap_t, default_eap_type_name), NULL, "md5" },
34 { "timer_expire", PW_TYPE_INTEGER,
35 offsetof(rlm_eap_t, timer_limit), NULL, "60"},
36 { "ignore_unknown_eap_types", PW_TYPE_BOOLEAN,
37 offsetof(rlm_eap_t, ignore_unknown_eap_types), NULL, "no" },
38 { "cisco_accounting_username_bug", PW_TYPE_BOOLEAN,
39 offsetof(rlm_eap_t, cisco_accounting_username_bug), NULL, "no" },
41 { NULL, -1, 0, NULL, NULL } /* end the list */
45 * delete all the allocated space by eap module
47 static int eap_detach(void *instance)
52 inst = (rlm_eap_t *)instance;
54 rbtree_free(inst->session_tree);
55 inst->session_tree = NULL;
58 for (i = 0; i < PW_EAP_MAX_TYPES; i++) {
59 if (inst->types[i]) eaptype_free(inst->types[i]);
60 inst->types[i] = NULL;
63 pthread_mutex_destroy(&(inst->session_mutex));
65 if (inst->default_eap_type_name) free(inst->default_eap_type_name);
73 * Compare two handlers.
75 static int eap_handler_cmp(const void *a, const void *b)
78 const EAP_HANDLER *one = a;
79 const EAP_HANDLER *two = b;
82 if (one->src_ipaddr.af < two->src_ipaddr.af) return -1;
83 if (one->src_ipaddr.af > two->src_ipaddr.af) return +1;
85 if (one->eap_id < two->eap_id) return -1;
86 if (one->eap_id > two->eap_id) return +1;
88 switch (one->src_ipaddr.af) {
90 rcode = memcmp(&one->src_ipaddr.ipaddr.ip4addr,
91 &two->src_ipaddr.ipaddr.ip4addr,
92 sizeof(one->src_ipaddr.ipaddr.ip4addr));
95 rcode = memcmp(&one->src_ipaddr.ipaddr.ip6addr,
96 &two->src_ipaddr.ipaddr.ip6addr,
97 sizeof(one->src_ipaddr.ipaddr.ip6addr));
100 return -1; /* FIXME: die! */
104 * We could optimize this away, but the compiler should
105 * do that work for us, and this coding style helps us
106 * remember what to do if we add more checks later.
108 if (rcode != 0) return rcode;
110 return memcmp(one->state, two->state, sizeof(one->state));
115 * read the config section and load all the eap authentication types present.
117 static int eap_instantiate(CONF_SECTION *cs, void **instance)
124 inst = (rlm_eap_t *) malloc(sizeof(*inst));
128 memset(inst, 0, sizeof(*inst));
129 if (cf_section_parse(cs, inst, module_config) < 0) {
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 auth_type = cf_section_name1(scs);
144 if (!auth_type) continue;
146 eap_type = eaptype_name2type(auth_type);
148 radlog(L_ERR|L_CONS, "rlm_eap: Unknown EAP type %s",
155 * If we're asked to load TTLS or PEAP, ensure
156 * that we've first loaded TLS.
158 if (((eap_type == PW_EAP_TTLS) ||
159 (eap_type == PW_EAP_PEAP)) &&
160 (inst->types[PW_EAP_TLS] == NULL)) {
161 radlog(L_ERR, "rlm_eap: Unable to load EAP-Type/%s, as EAP-Type/TLS is required first.",
169 if (eaptype_load(&inst->types[eap_type], eap_type, scs) < 0) {
174 num_types++; /* successfully loaded one more types */
177 if (num_types == 0) {
178 radlog(L_ERR|L_CONS, "rlm_eap: No EAP type configured, module cannot do anything.");
184 * Ensure that the default EAP type is loaded.
186 eap_type = eaptype_name2type(inst->default_eap_type_name);
188 radlog(L_ERR|L_CONS, "rlm_eap: Unknown default EAP type %s",
189 inst->default_eap_type_name);
194 if (inst->types[eap_type] == NULL) {
195 radlog(L_ERR|L_CONS, "rlm_eap: No such sub-type for default EAP type %s",
196 inst->default_eap_type_name);
200 inst->default_eap_type = eap_type; /* save the numerical type */
203 * List of sessions are set to NULL by the memset
207 /* Generate a state key, specific to eap */
211 * Lookup sessions in the tree. We don't free them in
212 * the tree, as that's taken care of elsewhere...
214 inst->session_tree = rbtree_create(eap_handler_cmp, NULL, 0);
215 if (!inst->session_tree) {
216 radlog(L_ERR|L_CONS, "rlm_eap: Cannot initialize tree");
221 pthread_mutex_init(&(inst->session_mutex), NULL);
229 * For backwards compatibility.
231 static int eap_authenticate(void *instance, REQUEST *request)
234 EAP_HANDLER *handler;
235 eap_packet_t *eap_packet;
238 inst = (rlm_eap_t *) instance;
241 * Get the eap packet to start with
243 eap_packet = eap_attribute(request->packet->vps);
244 if (eap_packet == NULL) {
245 radlog(L_ERR, "rlm_eap: Malformed EAP Message");
246 return RLM_MODULE_FAIL;
250 * Create the eap handler. The eap_packet will end up being
251 * "swallowed" into the handler, so we can't access it after
254 handler = eap_handler(inst, &eap_packet, request);
255 if (handler == NULL) {
256 DEBUG2(" rlm_eap: Failed in handler");
257 return RLM_MODULE_INVALID;
261 * If it's a recursive request, then disallow
262 * TLS, TTLS, and PEAP, inside of the TLS tunnel.
264 if ((request->options & RAD_REQUEST_OPTION_FAKE_REQUEST) != 0) {
265 switch(handler->eap_ds->response->type.type) {
269 DEBUG2(" rlm_eap: Unable to tunnel TLS inside of TLS");
271 eap_handler_free(handler);
272 return RLM_MODULE_INVALID;
275 default: /* It may be OK, allow it to proceed */
282 * Select the appropriate eap_type or default to the
285 rcode = eaptype_select(inst, handler);
290 if (rcode == EAP_INVALID) {
292 eap_handler_free(handler);
293 DEBUG2(" rlm_eap: Failed in EAP select");
294 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 DEBUG2(" 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,
309 handler, eap_handler_free);
310 rad_assert(rcode == 0);
312 return RLM_MODULE_HANDLED;
317 * Maybe the request was marked to be proxied. If so,
320 if (request->proxy != NULL) {
321 VALUE_PAIR *vp = NULL;
323 rad_assert(request->proxy_reply == NULL);
326 * Add the handle to the proxied list, so that we
327 * can retrieve it in the post-proxy stage, and
330 rcode = request_data_add(request,
331 inst, REQUEST_DATA_EAP_HANDLER,
332 handler, eap_handler_free);
333 rad_assert(rcode == 0);
336 * Some simple sanity checks. These should really
337 * be handled by the radius library...
339 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE);
341 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR);
343 vp = pairmake("Message-Authenticator",
345 rad_assert(vp != NULL);
346 pairadd(&(request->proxy->vps), vp);
351 * Delete the "proxied to" attribute, as it's
352 * set to 127.0.0.1 for tunneled requests, and
353 * we don't want to tell the world that...
355 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO);
357 DEBUG2(" Tunneled session will be proxied. Not doing EAP.");
358 return RLM_MODULE_HANDLED;
362 * We are done, wrap the EAP-request in RADIUS to send
363 * with all other required radius attributes
365 rcode = eap_compose(handler);
368 * Add to the list only if it is EAP-Request, OR if
369 * it's LEAP, and a response.
371 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
372 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
373 eaplist_add(inst, handler);
376 * LEAP is a little different. At Stage 4,
377 * it sends an EAP-Success message, but we still
378 * need to keep the State attribute & session
379 * data structure around for the AP Challenge.
381 * At stage 6, LEAP sends an EAP-Response, which
382 * isn't put into the list.
384 } else if ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
385 (handler->eap_ds->response->type.type == PW_EAP_LEAP) &&
386 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
387 (handler->eap_ds->request->type.type == 0)) {
389 eaplist_add(inst, handler);
392 DEBUG2(" rlm_eap: Freeing handler");
393 /* handler is not required any more, free it now */
394 eap_handler_free(handler);
398 * If it's an Access-Accept, RFC 2869, Section 2.3.1
399 * says that we MUST include a User-Name attribute in the
402 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
407 * Doesn't exist, add it in.
409 vp = pairfind(request->reply->vps, PW_USER_NAME);
411 vp = pairmake("User-Name", request->username->vp_strvalue,
413 rad_assert(vp != NULL);
414 pairadd(&(request->reply->vps), vp);
418 * Cisco AP1230 has a bug and needs a zero
419 * terminated string in Access-Accept.
421 if ((inst->cisco_accounting_username_bug) &&
422 (vp->length < (int) sizeof(vp->vp_strvalue))) {
423 vp->vp_strvalue[vp->length] = '\0';
432 * EAP authorization DEPENDS on other rlm authorizations,
433 * to check for user existance & get their configured values.
434 * It Handles EAP-START Messages, User-Name initilization.
436 static int eap_authorize(void *instance, REQUEST *request)
442 inst = (rlm_eap_t *)instance;
445 * We don't do authorization again, once we've seen the
446 * proxy reply (or the proxied packet)
448 if (request->proxy != NULL)
449 return RLM_MODULE_NOOP;
452 * For EAP_START, send Access-Challenge with EAP Identity
453 * request. even when we have to proxy this request
455 * RFC 2869, Section 2.3.1 notes that the "domain" of the
456 * user, (i.e. where to proxy him) comes from the EAP-Identity,
457 * so we CANNOT proxy the user, until we know his identity.
459 * We therefore send an EAP Identity request.
461 status = eap_start(inst, request);
464 return RLM_MODULE_NOOP;
466 return RLM_MODULE_FAIL;
468 return RLM_MODULE_HANDLED;
475 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
476 * it MUST copy the identity into the User-Name attribute.
478 * But we don't worry about that too much. We depend on
479 * each EAP sub-module to look for handler->request->username,
480 * and to get excited if it doesn't appear.
483 vp = pairfind(request->config_items, PW_AUTH_TYPE);
485 (vp->lvalue != PW_AUTHTYPE_REJECT)) {
486 vp = pairmake("Auth-Type", "EAP", T_OP_EQ);
488 return RLM_MODULE_FAIL;
490 pairadd(&request->config_items, vp);
493 return RLM_MODULE_UPDATED;
497 * If we're proxying EAP, then there may be magic we need
500 static int eap_post_proxy(void *inst, REQUEST *request)
504 EAP_HANDLER *handler;
507 * If there was a handler associated with this request,
508 * then it's a tunneled request which was proxied...
510 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
511 if (handler != NULL) {
513 eap_tunnel_data_t *data;
516 * Grab the tunnel callbacks from the request.
518 data = (eap_tunnel_data_t *) request_data_get(request,
520 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
522 radlog(L_ERR, "rlm_eap: Failed to retrieve callback for tunneled session!");
523 eap_handler_free(handler);
524 return RLM_MODULE_FAIL;
530 rcode = data->callback(handler, data->tls_session);
534 eap_handler_free(handler);
535 return RLM_MODULE_REJECT;
539 * We are done, wrap the EAP-request in RADIUS to send
540 * with all other required radius attributes
542 rcode = eap_compose(handler);
545 * Add to the list only if it is EAP-Request, OR if
546 * it's LEAP, and a response.
548 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
549 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
550 eaplist_add(inst, handler);
552 } else { /* couldn't have been LEAP, there's no tunnel */
553 DEBUG2(" rlm_eap: Freeing handler");
554 /* handler is not required any more, free it now */
555 eap_handler_free(handler);
559 * If it's an Access-Accept, RFC 2869, Section 2.3.1
560 * says that we MUST include a User-Name attribute in the
563 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
566 * Doesn't exist, add it in.
568 vp = pairfind(request->reply->vps, PW_USER_NAME);
570 vp = pairmake("User-Name", request->username->vp_strvalue,
572 rad_assert(vp != NULL);
573 pairadd(&(request->reply->vps), vp);
577 return RLM_MODULE_OK;
582 * There may be more than one Cisco-AVPair.
583 * Ensure we find the one with the LEAP attribute.
585 vp = request->proxy_reply->vps;
588 * Hmm... there's got to be a better way to
589 * discover codes for vendor attributes.
591 * This is vendor Cisco (9), Cisco-AVPair
594 vp = pairfind(vp, (9 << 16) | 1);
596 return RLM_MODULE_NOOP;
600 * If it's "leap:session-key", then stop.
602 * The format is VERY specific!
604 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
609 * Not this AV-pair. Go to the next one.
615 * The format is very specific.
617 if (vp->length != 17 + 34) {
618 DEBUG2(" rlm_eap: Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
619 vp->length, 17 + 34);
620 return RLM_MODULE_NOOP;
624 * Decrypt the session key, using the proxy data.
626 i = 34; /* starts off with 34 octets */
627 len = rad_tunnel_pwdecode(vp->vp_strvalue + 17, &i,
628 request->proxysecret,
629 request->proxy->vector);
632 * FIXME: Assert that i == 16.
636 * Encrypt the session key again, using the request data.
638 rad_tunnel_pwencode(vp->vp_strvalue + 17, &len,
640 request->packet->vector);
642 return RLM_MODULE_UPDATED;
647 * The module name should be the only globally exported symbol.
648 * That is, everything else should be 'static'.
653 RLM_TYPE_THREAD_SAFE, /* type */
654 eap_instantiate, /* instantiation */
655 eap_detach, /* detach */
657 eap_authenticate, /* authentication */
658 eap_authorize, /* authorization */
659 NULL, /* preaccounting */
660 NULL, /* accounting */
661 NULL, /* checksimul */
662 NULL, /* pre-proxy */
663 eap_post_proxy, /* post-proxy */