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 = fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr);
88 if (rcode != 0) return rcode;
90 return memcmp(one->state, two->state, sizeof(one->state));
95 * read the config section and load all the eap authentication types present.
97 static int eap_instantiate(CONF_SECTION *cs, void **instance)
104 inst = (rlm_eap_t *) malloc(sizeof(*inst));
108 memset(inst, 0, sizeof(*inst));
109 if (cf_section_parse(cs, inst, module_config) < 0) {
115 * Create our own random pool.
117 for (i = 0; i < 256; i++) {
118 inst->rand_pool.randrsl[i] = fr_rand();
120 fr_randinit(&inst->rand_pool, 1);
121 inst->rand_pool.randcnt = 0;
123 inst->xlat_name = cf_section_name2(cs);
124 if (!inst->xlat_name) inst->xlat_name = "EAP";
126 /* Load all the configured EAP-Types */
128 for(scs=cf_subsection_find_next(cs, NULL, NULL);
130 scs=cf_subsection_find_next(cs, scs, NULL)) {
132 const char *auth_type;
134 auth_type = cf_section_name1(scs);
136 if (!auth_type) continue;
138 eap_type = eaptype_name2type(auth_type);
140 radlog(L_ERR|L_CONS, "rlm_eap: Unknown EAP type %s",
146 #ifndef HAVE_OPENSSL_SSL_H
148 * This allows the default configuration to be
149 * shipped with EAP-TLS, etc. enabled. If the
150 * system doesn't have OpenSSL, they will be
153 * If the system does have OpenSSL, then this
154 * code will not be used. The administrator will
155 * then have to delete the tls,
156 * etc. configurations from eap.conf in order to
157 * have EAP without the TLS types.
159 if ((eap_type == PW_EAP_TLS) ||
160 (eap_type == PW_EAP_TTLS) ||
161 (eap_type == PW_EAP_PEAP)) {
162 DEBUG2("rlm_eap: Ignoring EAP-Type/%s because we do not have OpenSSL support.", auth_type);
168 * If we're asked to load TTLS or PEAP, ensure
169 * that we've first loaded TLS.
171 if (((eap_type == PW_EAP_TTLS) ||
172 (eap_type == PW_EAP_PEAP)) &&
173 (inst->types[PW_EAP_TLS] == NULL)) {
174 radlog(L_ERR, "rlm_eap: Unable to load EAP-Type/%s, as EAP-Type/TLS is required first.",
182 if (eaptype_load(&inst->types[eap_type], eap_type, scs) < 0) {
187 num_types++; /* successfully loaded one more types */
190 if (num_types == 0) {
191 radlog(L_ERR|L_CONS, "rlm_eap: No EAP type configured, module cannot do anything.");
197 * Ensure that the default EAP type is loaded.
199 eap_type = eaptype_name2type(inst->default_eap_type_name);
201 radlog(L_ERR|L_CONS, "rlm_eap: Unknown default EAP type %s",
202 inst->default_eap_type_name);
207 if (inst->types[eap_type] == NULL) {
208 radlog(L_ERR|L_CONS, "rlm_eap: No such sub-type for default EAP type %s",
209 inst->default_eap_type_name);
213 inst->default_eap_type = eap_type; /* save the numerical type */
216 * List of sessions are set to NULL by the memset
221 * Lookup sessions in the tree. We don't free them in
222 * the tree, as that's taken care of elsewhere...
224 inst->session_tree = rbtree_create(eap_handler_cmp, NULL, 0);
225 if (!inst->session_tree) {
226 radlog(L_ERR|L_CONS, "rlm_eap: Cannot initialize tree");
231 pthread_mutex_init(&(inst->session_mutex), NULL);
239 * For backwards compatibility.
241 static int eap_authenticate(void *instance, REQUEST *request)
244 EAP_HANDLER *handler;
245 eap_packet_t *eap_packet;
248 inst = (rlm_eap_t *) instance;
251 * Get the eap packet to start with
253 eap_packet = eap_vp2packet(request->packet->vps);
254 if (eap_packet == NULL) {
255 radlog(L_ERR, "rlm_eap: Malformed EAP Message");
256 return RLM_MODULE_FAIL;
260 * Create the eap handler. The eap_packet will end up being
261 * "swallowed" into the handler, so we can't access it after
264 handler = eap_handler(inst, &eap_packet, request);
265 if (handler == NULL) {
266 DEBUG2(" rlm_eap: Failed in handler");
267 return RLM_MODULE_INVALID;
271 * Select the appropriate eap_type or default to the
274 rcode = eaptype_select(inst, handler);
279 if (rcode == EAP_INVALID) {
281 eap_handler_free(handler);
282 DEBUG2(" rlm_eap: Failed in EAP select");
283 return RLM_MODULE_INVALID;
287 * If we're doing horrible tunneling work, remember it.
289 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
290 DEBUG2(" Not-EAP proxy set. Not composing EAP");
292 * Add the handle to the proxied list, so that we
293 * can retrieve it in the post-proxy stage, and
296 rcode = request_data_add(request,
297 inst, REQUEST_DATA_EAP_HANDLER,
299 (void *) eap_handler_free);
300 rad_assert(rcode == 0);
302 return RLM_MODULE_HANDLED;
307 * Maybe the request was marked to be proxied. If so,
310 if (request->proxy != NULL) {
311 VALUE_PAIR *vp = NULL;
313 rad_assert(request->proxy_reply == NULL);
316 * Add the handle to the proxied list, so that we
317 * can retrieve it in the post-proxy stage, and
320 rcode = request_data_add(request,
321 inst, REQUEST_DATA_EAP_HANDLER,
323 (void *) eap_handler_free);
324 rad_assert(rcode == 0);
327 * Some simple sanity checks. These should really
328 * be handled by the radius library...
330 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE);
332 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR);
334 vp = pairmake("Message-Authenticator",
336 rad_assert(vp != NULL);
337 pairadd(&(request->proxy->vps), vp);
342 * Delete the "proxied to" attribute, as it's
343 * set to 127.0.0.1 for tunneled requests, and
344 * we don't want to tell the world that...
346 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO);
348 DEBUG2(" Tunneled session will be proxied. Not doing EAP.");
349 return RLM_MODULE_HANDLED;
353 * We are done, wrap the EAP-request in RADIUS to send
354 * with all other required radius attributes
356 rcode = eap_compose(handler);
359 * Add to the list only if it is EAP-Request, OR if
360 * it's LEAP, and a response.
362 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
363 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) ||
366 * LEAP is a little different. At Stage 4,
367 * it sends an EAP-Success message, but we still
368 * need to keep the State attribute & session
369 * data structure around for the AP Challenge.
371 * At stage 6, LEAP sends an EAP-Response, which
372 * isn't put into the list.
374 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
375 (handler->eap_ds->response->type.type == PW_EAP_LEAP) &&
376 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
377 (handler->eap_ds->request->type.type == 0))) {
379 if (!eaplist_add(inst, handler)) {
381 eap_handler_free(handler);
382 return RLM_MODULE_FAIL;
386 DEBUG2(" rlm_eap: Freeing handler");
387 /* handler is not required any more, free it now */
388 eap_handler_free(handler);
392 * If it's an Access-Accept, RFC 2869, Section 2.3.1
393 * says that we MUST include a User-Name attribute in the
396 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
401 * Doesn't exist, add it in.
403 vp = pairfind(request->reply->vps, PW_USER_NAME);
405 vp = pairmake("User-Name", request->username->vp_strvalue,
407 rad_assert(vp != NULL);
408 pairadd(&(request->reply->vps), vp);
412 * Cisco AP1230 has a bug and needs a zero
413 * terminated string in Access-Accept.
415 if ((inst->cisco_accounting_username_bug) &&
416 (vp->length < (int) sizeof(vp->vp_strvalue))) {
417 vp->vp_strvalue[vp->length] = '\0';
426 * EAP authorization DEPENDS on other rlm authorizations,
427 * to check for user existance & get their configured values.
428 * It Handles EAP-START Messages, User-Name initilization.
430 static int eap_authorize(void *instance, REQUEST *request)
436 inst = (rlm_eap_t *)instance;
439 * We don't do authorization again, once we've seen the
440 * proxy reply (or the proxied packet)
442 if (request->proxy != NULL)
443 return RLM_MODULE_NOOP;
446 * For EAP_START, send Access-Challenge with EAP Identity
447 * request. even when we have to proxy this request
449 * RFC 2869, Section 2.3.1 notes that the "domain" of the
450 * user, (i.e. where to proxy him) comes from the EAP-Identity,
451 * so we CANNOT proxy the user, until we know his identity.
453 * We therefore send an EAP Identity request.
455 status = eap_start(inst, request);
458 return RLM_MODULE_NOOP;
460 return RLM_MODULE_FAIL;
462 return RLM_MODULE_HANDLED;
470 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
471 * it MUST copy the identity into the User-Name attribute.
473 * But we don't worry about that too much. We depend on
474 * each EAP sub-module to look for handler->request->username,
475 * and to get excited if it doesn't appear.
478 vp = pairfind(request->config_items, PW_AUTH_TYPE);
480 (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
481 vp = pairmake("Auth-Type", inst->xlat_name, T_OP_EQ);
483 return RLM_MODULE_FAIL;
485 pairadd(&request->config_items, vp);
488 if (status == EAP_OK) return RLM_MODULE_OK;
490 return RLM_MODULE_UPDATED;
494 * If we're proxying EAP, then there may be magic we need
497 static int eap_post_proxy(void *inst, REQUEST *request)
502 EAP_HANDLER *handler;
505 * If there was a handler associated with this request,
506 * then it's a tunneled request which was proxied...
508 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
509 if (handler != NULL) {
511 eap_tunnel_data_t *data;
514 * Grab the tunnel callbacks from the request.
516 data = (eap_tunnel_data_t *) request_data_get(request,
518 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
520 radlog(L_ERR, "rlm_eap: Failed to retrieve callback for tunneled session!");
521 eap_handler_free(handler);
522 return RLM_MODULE_FAIL;
528 DEBUG2(" rlm_eap: Doing post-proxy callback");
529 rcode = data->callback(handler, data->tls_session);
532 DEBUG2(" rlm_eap: Failed in post-proxy callback");
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 if (!eaplist_add(inst, handler)) {
552 eap_handler_free(handler);
553 return RLM_MODULE_FAIL;
556 } else { /* couldn't have been LEAP, there's no tunnel */
557 DEBUG2(" rlm_eap: Freeing handler");
558 /* handler is not required any more, free it now */
559 eap_handler_free(handler);
563 * If it's an Access-Accept, RFC 2869, Section 2.3.1
564 * says that we MUST include a User-Name attribute in the
567 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
570 * Doesn't exist, add it in.
572 vp = pairfind(request->reply->vps, PW_USER_NAME);
574 vp = pairmake("User-Name", request->username->vp_strvalue,
576 rad_assert(vp != NULL);
577 pairadd(&(request->reply->vps), vp);
581 return RLM_MODULE_OK;
583 DEBUG2(" rlm_eap: No pre-existing handler found");
588 * There may be more than one Cisco-AVPair.
589 * Ensure we find the one with the LEAP attribute.
591 vp = request->proxy_reply->vps;
594 * Hmm... there's got to be a better way to
595 * discover codes for vendor attributes.
597 * This is vendor Cisco (9), Cisco-AVPair
600 vp = pairfind(vp, (9 << 16) | 1);
602 return RLM_MODULE_NOOP;
606 * If it's "leap:session-key", then stop.
608 * The format is VERY specific!
610 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
615 * Not this AV-pair. Go to the next one.
621 * The format is very specific.
623 if (vp->length != 17 + 34) {
624 DEBUG2(" rlm_eap: Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
625 vp->length, 17 + 34);
626 return RLM_MODULE_NOOP;
630 * Decrypt the session key, using the proxy data.
632 i = 34; /* starts off with 34 octets */
633 len = rad_tunnel_pwdecode(vp->vp_octets + 17, &i,
634 request->home_server->secret,
635 request->proxy->vector);
638 * FIXME: Assert that i == 16.
642 * Encrypt the session key again, using the request data.
644 rad_tunnel_pwencode(vp->vp_strvalue + 17, &len,
645 request->client->secret,
646 request->packet->vector);
648 return RLM_MODULE_UPDATED;
653 * The module name should be the only globally exported symbol.
654 * That is, everything else should be 'static'.
659 RLM_TYPE_CHECK_CONFIG_SAFE, /* type */
660 eap_instantiate, /* instantiation */
661 eap_detach, /* detach */
663 eap_authenticate, /* authentication */
664 eap_authorize, /* authorization */
665 NULL, /* preaccounting */
666 NULL, /* accounting */
667 NULL, /* checksimul */
668 NULL, /* pre-proxy */
669 eap_post_proxy, /* post-proxy */