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;
82 if (one->eap_id < two->eap_id) return -1;
83 if (one->eap_id > two->eap_id) return +1;
85 rcode = fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr);
86 if (rcode != 0) return rcode;
88 return memcmp(one->state, two->state, sizeof(one->state));
93 * read the config section and load all the eap authentication types present.
95 static int eap_instantiate(CONF_SECTION *cs, void **instance)
102 inst = (rlm_eap_t *) malloc(sizeof(*inst));
106 memset(inst, 0, sizeof(*inst));
107 if (cf_section_parse(cs, inst, module_config) < 0) {
113 * Create our own random pool.
115 for (i = 0; i < 256; i++) {
116 inst->rand_pool.randrsl[i] = fr_rand();
118 fr_randinit(&inst->rand_pool, 1);
120 /* Load all the configured EAP-Types */
122 for(scs=cf_subsection_find_next(cs, NULL, NULL);
124 scs=cf_subsection_find_next(cs, scs, NULL)) {
126 const char *auth_type;
128 auth_type = cf_section_name1(scs);
130 if (!auth_type) continue;
132 eap_type = eaptype_name2type(auth_type);
134 radlog(L_ERR|L_CONS, "rlm_eap: Unknown EAP type %s",
140 #ifndef HAVE_OPENSSL_SSL_H
142 * This allows the default configuration to be
143 * shipped with EAP-TLS, etc. enabled. If the
144 * system doesn't have OpenSSL, they will be
147 * If the system does have OpenSSL, then this
148 * code will not be used. The administrator will
149 * then have to delete the tls,
150 * etc. configurations from eap.conf in order to
151 * have EAP without the TLS types.
153 if ((eap_type == PW_EAP_TLS) ||
154 (eap_type == PW_EAP_TTLS) ||
155 (eap_type == PW_EAP_PEAP)) {
156 DEBUG2("rlm_eap: Ignoring EAP-Type/%s because we do not have OpenSSL support.", auth_type);
162 * If we're asked to load TTLS or PEAP, ensure
163 * that we've first loaded TLS.
165 if (((eap_type == PW_EAP_TTLS) ||
166 (eap_type == PW_EAP_PEAP)) &&
167 (inst->types[PW_EAP_TLS] == NULL)) {
168 radlog(L_ERR, "rlm_eap: Unable to load EAP-Type/%s, as EAP-Type/TLS is required first.",
176 if (eaptype_load(&inst->types[eap_type], eap_type, scs) < 0) {
181 num_types++; /* successfully loaded one more types */
184 if (num_types == 0) {
185 radlog(L_ERR|L_CONS, "rlm_eap: No EAP type configured, module cannot do anything.");
191 * Ensure that the default EAP type is loaded.
193 eap_type = eaptype_name2type(inst->default_eap_type_name);
195 radlog(L_ERR|L_CONS, "rlm_eap: Unknown default EAP type %s",
196 inst->default_eap_type_name);
201 if (inst->types[eap_type] == NULL) {
202 radlog(L_ERR|L_CONS, "rlm_eap: No such sub-type for default EAP type %s",
203 inst->default_eap_type_name);
207 inst->default_eap_type = eap_type; /* save the numerical type */
210 * List of sessions are set to NULL by the memset
215 * Lookup sessions in the tree. We don't free them in
216 * the tree, as that's taken care of elsewhere...
218 inst->session_tree = rbtree_create(eap_handler_cmp, NULL, 0);
219 if (!inst->session_tree) {
220 radlog(L_ERR|L_CONS, "rlm_eap: Cannot initialize tree");
225 pthread_mutex_init(&(inst->session_mutex), NULL);
233 * For backwards compatibility.
235 static int eap_authenticate(void *instance, REQUEST *request)
238 EAP_HANDLER *handler;
239 eap_packet_t *eap_packet;
242 inst = (rlm_eap_t *) instance;
245 * Get the eap packet to start with
247 eap_packet = eap_vp2packet(request->packet->vps);
248 if (eap_packet == NULL) {
249 radlog(L_ERR, "rlm_eap: Malformed EAP Message");
250 return RLM_MODULE_FAIL;
254 * Create the eap handler. The eap_packet will end up being
255 * "swallowed" into the handler, so we can't access it after
258 handler = eap_handler(inst, &eap_packet, request);
259 if (handler == NULL) {
260 DEBUG2(" rlm_eap: Failed in handler");
261 return RLM_MODULE_INVALID;
265 * Select the appropriate eap_type or default to the
268 rcode = eaptype_select(inst, handler);
273 if (rcode == EAP_INVALID) {
275 eap_handler_free(handler);
276 DEBUG2(" rlm_eap: Failed in EAP select");
277 return RLM_MODULE_INVALID;
281 * If we're doing horrible tunneling work, remember it.
283 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
284 DEBUG2(" Not-EAP proxy set. Not composing EAP");
286 * Add the handle to the proxied list, so that we
287 * can retrieve it in the post-proxy stage, and
290 rcode = request_data_add(request,
291 inst, REQUEST_DATA_EAP_HANDLER,
293 (void *) eap_handler_free);
294 rad_assert(rcode == 0);
296 return RLM_MODULE_HANDLED;
301 * Maybe the request was marked to be proxied. If so,
304 if (request->proxy != NULL) {
305 VALUE_PAIR *vp = NULL;
307 rad_assert(request->proxy_reply == NULL);
310 * Add the handle to the proxied list, so that we
311 * can retrieve it in the post-proxy stage, and
314 rcode = request_data_add(request,
315 inst, REQUEST_DATA_EAP_HANDLER,
317 (void *) eap_handler_free);
318 rad_assert(rcode == 0);
321 * Some simple sanity checks. These should really
322 * be handled by the radius library...
324 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE);
326 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR);
328 vp = pairmake("Message-Authenticator",
330 rad_assert(vp != NULL);
331 pairadd(&(request->proxy->vps), vp);
336 * Delete the "proxied to" attribute, as it's
337 * set to 127.0.0.1 for tunneled requests, and
338 * we don't want to tell the world that...
340 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO);
342 DEBUG2(" Tunneled session will be proxied. Not doing EAP.");
343 return RLM_MODULE_HANDLED;
347 * We are done, wrap the EAP-request in RADIUS to send
348 * with all other required radius attributes
350 rcode = eap_compose(handler);
353 * Add to the list only if it is EAP-Request, OR if
354 * it's LEAP, and a response.
356 if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
357 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) ||
360 * LEAP is a little different. At Stage 4,
361 * it sends an EAP-Success message, but we still
362 * need to keep the State attribute & session
363 * data structure around for the AP Challenge.
365 * At stage 6, LEAP sends an EAP-Response, which
366 * isn't put into the list.
368 ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
369 (handler->eap_ds->response->type.type == PW_EAP_LEAP) &&
370 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
371 (handler->eap_ds->request->type.type == 0))) {
373 if (!eaplist_add(inst, handler)) {
375 eap_handler_free(handler);
376 return RLM_MODULE_FAIL;
380 DEBUG2(" rlm_eap: Freeing handler");
381 /* handler is not required any more, free it now */
382 eap_handler_free(handler);
386 * If it's an Access-Accept, RFC 2869, Section 2.3.1
387 * says that we MUST include a User-Name attribute in the
390 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
395 * Doesn't exist, add it in.
397 vp = pairfind(request->reply->vps, PW_USER_NAME);
399 vp = pairmake("User-Name", request->username->vp_strvalue,
401 rad_assert(vp != NULL);
402 pairadd(&(request->reply->vps), vp);
406 * Cisco AP1230 has a bug and needs a zero
407 * terminated string in Access-Accept.
409 if ((inst->cisco_accounting_username_bug) &&
410 (vp->length < (int) sizeof(vp->vp_strvalue))) {
411 vp->vp_strvalue[vp->length] = '\0';
420 * EAP authorization DEPENDS on other rlm authorizations,
421 * to check for user existance & get their configured values.
422 * It Handles EAP-START Messages, User-Name initilization.
424 static int eap_authorize(void *instance, REQUEST *request)
430 inst = (rlm_eap_t *)instance;
433 * We don't do authorization again, once we've seen the
434 * proxy reply (or the proxied packet)
436 if (request->proxy != NULL)
437 return RLM_MODULE_NOOP;
440 * For EAP_START, send Access-Challenge with EAP Identity
441 * request. even when we have to proxy this request
443 * RFC 2869, Section 2.3.1 notes that the "domain" of the
444 * user, (i.e. where to proxy him) comes from the EAP-Identity,
445 * so we CANNOT proxy the user, until we know his identity.
447 * We therefore send an EAP Identity request.
449 status = eap_start(inst, request);
452 return RLM_MODULE_NOOP;
454 return RLM_MODULE_FAIL;
456 return RLM_MODULE_HANDLED;
464 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
465 * it MUST copy the identity into the User-Name attribute.
467 * But we don't worry about that too much. We depend on
468 * each EAP sub-module to look for handler->request->username,
469 * and to get excited if it doesn't appear.
472 vp = pairfind(request->config_items, PW_AUTH_TYPE);
474 (vp->vp_integer != PW_AUTHTYPE_REJECT)) {
475 vp = pairmake("Auth-Type", "EAP", T_OP_EQ);
477 return RLM_MODULE_FAIL;
479 pairadd(&request->config_items, vp);
482 if (status == EAP_OK) return RLM_MODULE_OK;
484 return RLM_MODULE_UPDATED;
488 * If we're proxying EAP, then there may be magic we need
491 static int eap_post_proxy(void *inst, REQUEST *request)
496 EAP_HANDLER *handler;
499 * If there was a handler associated with this request,
500 * then it's a tunneled request which was proxied...
502 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
503 if (handler != NULL) {
505 eap_tunnel_data_t *data;
508 * Grab the tunnel callbacks from the request.
510 data = (eap_tunnel_data_t *) request_data_get(request,
512 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
514 radlog(L_ERR, "rlm_eap: Failed to retrieve callback for tunneled session!");
515 eap_handler_free(handler);
516 return RLM_MODULE_FAIL;
522 rcode = data->callback(handler, data->tls_session);
526 eap_handler_free(handler);
527 return RLM_MODULE_REJECT;
531 * We are done, wrap the EAP-request in RADIUS to send
532 * with all other required radius attributes
534 rcode = eap_compose(handler);
537 * Add to the list only if it is EAP-Request, OR if
538 * it's LEAP, and a response.
540 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
541 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
542 eaplist_add(inst, handler);
544 } else { /* couldn't have been LEAP, there's no tunnel */
545 DEBUG2(" rlm_eap: Freeing handler");
546 /* handler is not required any more, free it now */
547 eap_handler_free(handler);
551 * If it's an Access-Accept, RFC 2869, Section 2.3.1
552 * says that we MUST include a User-Name attribute in the
555 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
558 * Doesn't exist, add it in.
560 vp = pairfind(request->reply->vps, PW_USER_NAME);
562 vp = pairmake("User-Name", request->username->vp_strvalue,
564 rad_assert(vp != NULL);
565 pairadd(&(request->reply->vps), vp);
569 return RLM_MODULE_OK;
574 * There may be more than one Cisco-AVPair.
575 * Ensure we find the one with the LEAP attribute.
577 vp = request->proxy_reply->vps;
580 * Hmm... there's got to be a better way to
581 * discover codes for vendor attributes.
583 * This is vendor Cisco (9), Cisco-AVPair
586 vp = pairfind(vp, (9 << 16) | 1);
588 return RLM_MODULE_NOOP;
592 * If it's "leap:session-key", then stop.
594 * The format is VERY specific!
596 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
601 * Not this AV-pair. Go to the next one.
607 * The format is very specific.
609 if (vp->length != 17 + 34) {
610 DEBUG2(" rlm_eap: Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
611 vp->length, 17 + 34);
612 return RLM_MODULE_NOOP;
616 * Decrypt the session key, using the proxy data.
618 i = 34; /* starts off with 34 octets */
619 len = rad_tunnel_pwdecode(vp->vp_octets + 17, &i,
620 request->home_server->secret,
621 request->proxy->vector);
624 * FIXME: Assert that i == 16.
628 * Encrypt the session key again, using the request data.
630 rad_tunnel_pwencode(vp->vp_strvalue + 17, &len,
631 request->client->secret,
632 request->packet->vector);
634 return RLM_MODULE_UPDATED;
639 * The module name should be the only globally exported symbol.
640 * That is, everything else should be 'static'.
645 RLM_TYPE_CHECK_CONFIG_SAFE, /* type */
646 eap_instantiate, /* instantiation */
647 eap_detach, /* detach */
649 eap_authenticate, /* authentication */
650 eap_authorize, /* authorization */
651 NULL, /* preaccounting */
652 NULL, /* accounting */
653 NULL, /* checksimul */
654 NULL, /* pre-proxy */
655 eap_post_proxy, /* post-proxy */