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" },
43 { NULL, -1, 0, NULL, NULL } /* end the list */
47 * delete all the allocated space by eap module
49 static int eap_detach(void *instance)
54 inst = (rlm_eap_t *)instance;
56 rbtree_free(inst->session_tree);
57 inst->session_tree = NULL;
60 for (i = 0; i < PW_EAP_MAX_TYPES; i++) {
61 if (inst->types[i]) eaptype_free(inst->types[i]);
62 inst->types[i] = NULL;
65 pthread_mutex_destroy(&(inst->session_mutex));
74 * Compare two handlers.
76 static int eap_handler_cmp(const void *a, const void *b)
79 const EAP_HANDLER *one = a;
80 const EAP_HANDLER *two = b;
83 if (one->eap_id < two->eap_id) return -1;
84 if (one->eap_id > two->eap_id) return +1;
86 rcode = lrad_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr);
87 if (rcode != 0) return rcode;
89 return memcmp(one->state, two->state, sizeof(one->state));
94 * read the config section and load all the eap authentication types present.
96 static int eap_instantiate(CONF_SECTION *cs, void **instance)
103 inst = (rlm_eap_t *) malloc(sizeof(*inst));
107 memset(inst, 0, sizeof(*inst));
108 if (cf_section_parse(cs, inst, module_config) < 0) {
113 /* Load all the configured EAP-Types */
115 for(scs=cf_subsection_find_next(cs, NULL, NULL);
117 scs=cf_subsection_find_next(cs, scs, NULL)) {
119 const char *auth_type;
121 auth_type = cf_section_name1(scs);
123 if (!auth_type) continue;
125 eap_type = eaptype_name2type(auth_type);
127 radlog(L_ERR|L_CONS, "rlm_eap: Unknown EAP type %s",
135 * This allows the default configuration to be
136 * shipped with EAP-TLS, etc. enabled. If the
137 * system doesn't have OpenSSL, they will be
140 * If the system does have OpenSSL, then this
141 * code will not be used. The administrator will
142 * then have to delete the tls,
143 * etc. configurations from eap.conf in order to
144 * have EAP without the TLS types.
146 if ((eap_type == PW_EAP_TLS) ||
147 (eap_type == PW_EAP_TTLS) ||
148 (eap_type == PW_EAP_PEAP)) {
149 DEBUG2("rlm_eap: Ignoring EAP-Type/%s because we do not have OpenSSL support.", auth_type);
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->packet->dst_port == 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;
476 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
477 * it MUST copy the identity into the User-Name attribute.
479 * But we don't worry about that too much. We depend on
480 * each EAP sub-module to look for handler->request->username,
481 * and to get excited if it doesn't appear.
484 vp = pairfind(request->config_items, PW_AUTH_TYPE);
486 (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
487 vp = pairmake("Auth-Type", "EAP", T_OP_EQ);
489 return RLM_MODULE_FAIL;
491 pairadd(&request->config_items, vp);
494 if (status == EAP_OK) return RLM_MODULE_OK;
496 return RLM_MODULE_UPDATED;
500 * If we're proxying EAP, then there may be magic we need
503 static int eap_post_proxy(void *inst, REQUEST *request)
507 EAP_HANDLER *handler;
510 * If there was a handler associated with this request,
511 * then it's a tunneled request which was proxied...
513 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
514 if (handler != NULL) {
516 eap_tunnel_data_t *data;
519 * Grab the tunnel callbacks from the request.
521 data = (eap_tunnel_data_t *) request_data_get(request,
523 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
525 radlog(L_ERR, "rlm_eap: Failed to retrieve callback for tunneled session!");
526 eap_handler_free(handler);
527 return RLM_MODULE_FAIL;
533 rcode = data->callback(handler, data->tls_session);
537 eap_handler_free(handler);
538 return RLM_MODULE_REJECT;
542 * We are done, wrap the EAP-request in RADIUS to send
543 * with all other required radius attributes
545 rcode = eap_compose(handler);
548 * Add to the list only if it is EAP-Request, OR if
549 * it's LEAP, and a response.
551 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
552 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
553 eaplist_add(inst, handler);
555 } else { /* couldn't have been LEAP, there's no tunnel */
556 DEBUG2(" rlm_eap: Freeing handler");
557 /* handler is not required any more, free it now */
558 eap_handler_free(handler);
562 * If it's an Access-Accept, RFC 2869, Section 2.3.1
563 * says that we MUST include a User-Name attribute in the
566 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
569 * Doesn't exist, add it in.
571 vp = pairfind(request->reply->vps, PW_USER_NAME);
573 vp = pairmake("User-Name", request->username->vp_strvalue,
575 rad_assert(vp != NULL);
576 pairadd(&(request->reply->vps), vp);
580 return RLM_MODULE_OK;
585 * There may be more than one Cisco-AVPair.
586 * Ensure we find the one with the LEAP attribute.
588 vp = request->proxy_reply->vps;
591 * Hmm... there's got to be a better way to
592 * discover codes for vendor attributes.
594 * This is vendor Cisco (9), Cisco-AVPair
597 vp = pairfind(vp, (9 << 16) | 1);
599 return RLM_MODULE_NOOP;
603 * If it's "leap:session-key", then stop.
605 * The format is VERY specific!
607 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
612 * Not this AV-pair. Go to the next one.
618 * The format is very specific.
620 if (vp->length != 17 + 34) {
621 DEBUG2(" rlm_eap: Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
622 vp->length, 17 + 34);
623 return RLM_MODULE_NOOP;
627 * Decrypt the session key, using the proxy data.
629 i = 34; /* starts off with 34 octets */
630 len = rad_tunnel_pwdecode(vp->vp_strvalue + 17, &i,
631 request->home_server->secret,
632 request->proxy->vector);
635 * FIXME: Assert that i == 16.
639 * Encrypt the session key again, using the request data.
641 rad_tunnel_pwencode(vp->vp_strvalue + 17, &len,
642 request->client->secret,
643 request->packet->vector);
645 return RLM_MODULE_UPDATED;
650 * The module name should be the only globally exported symbol.
651 * That is, everything else should be 'static'.
656 RLM_TYPE_THREAD_SAFE, /* type */
657 eap_instantiate, /* instantiation */
658 eap_detach, /* detach */
660 eap_authenticate, /* authentication */
661 eap_authorize, /* authorization */
662 NULL, /* preaccounting */
663 NULL, /* accounting */
664 NULL, /* checksimul */
665 NULL, /* pre-proxy */
666 eap_post_proxy, /* post-proxy */