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/autoconf.h>
30 #include <freeradius-devel/modules.h>
32 static const CONF_PARSER module_config[] = {
33 { "default_eap_type", PW_TYPE_STRING_PTR,
34 offsetof(rlm_eap_t, default_eap_type_name), NULL, "md5" },
35 { "timer_expire", PW_TYPE_INTEGER,
36 offsetof(rlm_eap_t, timer_limit), NULL, "60"},
37 { "ignore_unknown_eap_types", PW_TYPE_BOOLEAN,
38 offsetof(rlm_eap_t, ignore_unknown_eap_types), NULL, "no" },
39 { "cisco_accounting_username_bug", PW_TYPE_BOOLEAN,
40 offsetof(rlm_eap_t, cisco_accounting_username_bug), NULL, "no" },
42 { NULL, -1, 0, NULL, NULL } /* end the list */
46 * delete all the allocated space by eap module
48 static int eap_detach(void *instance)
53 inst = (rlm_eap_t *)instance;
55 rbtree_free(inst->session_tree);
56 inst->session_tree = NULL;
59 for (i = 0; i < PW_EAP_MAX_TYPES; i++) {
60 if (inst->types[i]) eaptype_free(inst->types[i]);
61 inst->types[i] = NULL;
64 pthread_mutex_destroy(&(inst->session_mutex));
66 if (inst->default_eap_type_name) free(inst->default_eap_type_name);
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->src_ipaddr.af < two->src_ipaddr.af) return -1;
84 if (one->src_ipaddr.af > two->src_ipaddr.af) return +1;
86 if (one->eap_id < two->eap_id) return -1;
87 if (one->eap_id > two->eap_id) return +1;
89 switch (one->src_ipaddr.af) {
91 rcode = memcmp(&one->src_ipaddr.ipaddr.ip4addr,
92 &two->src_ipaddr.ipaddr.ip4addr,
93 sizeof(one->src_ipaddr.ipaddr.ip4addr));
96 rcode = memcmp(&one->src_ipaddr.ipaddr.ip6addr,
97 &two->src_ipaddr.ipaddr.ip6addr,
98 sizeof(one->src_ipaddr.ipaddr.ip6addr));
101 return -1; /* FIXME: die! */
105 * We could optimize this away, but the compiler should
106 * do that work for us, and this coding style helps us
107 * remember what to do if we add more checks later.
109 if (rcode != 0) return rcode;
111 return memcmp(one->state, two->state, sizeof(one->state));
116 * read the config section and load all the eap authentication types present.
118 static int eap_instantiate(CONF_SECTION *cs, void **instance)
125 inst = (rlm_eap_t *) malloc(sizeof(*inst));
129 memset(inst, 0, sizeof(*inst));
130 if (cf_section_parse(cs, inst, module_config) < 0) {
135 /* Load all the configured EAP-Types */
137 for(scs=cf_subsection_find_next(cs, NULL, NULL);
139 scs=cf_subsection_find_next(cs, scs, NULL)) {
143 auth_type = cf_section_name1(scs);
145 if (!auth_type) continue;
147 eap_type = eaptype_name2type(auth_type);
149 radlog(L_ERR|L_CONS, "rlm_eap: Unknown EAP type %s",
156 * If we're asked to load TTLS or PEAP, ensure
157 * that we've first loaded TLS.
159 if (((eap_type == PW_EAP_TTLS) ||
160 (eap_type == PW_EAP_PEAP)) &&
161 (inst->types[PW_EAP_TLS] == NULL)) {
162 radlog(L_ERR, "rlm_eap: Unable to load EAP-Type/%s, as EAP-Type/TLS is required first.",
170 if (eaptype_load(&inst->types[eap_type], eap_type, scs) < 0) {
175 num_types++; /* successfully loaded one more types */
178 if (num_types == 0) {
179 radlog(L_ERR|L_CONS, "rlm_eap: No EAP type configured, module cannot do anything.");
185 * Ensure that the default EAP type is loaded.
187 eap_type = eaptype_name2type(inst->default_eap_type_name);
189 radlog(L_ERR|L_CONS, "rlm_eap: Unknown default EAP type %s",
190 inst->default_eap_type_name);
195 if (inst->types[eap_type] == NULL) {
196 radlog(L_ERR|L_CONS, "rlm_eap: No such sub-type for default EAP type %s",
197 inst->default_eap_type_name);
201 inst->default_eap_type = eap_type; /* save the numerical type */
204 * List of sessions are set to NULL by the memset
208 /* Generate a state key, specific to eap */
212 * Lookup sessions in the tree. We don't free them in
213 * the tree, as that's taken care of elsewhere...
215 inst->session_tree = rbtree_create(eap_handler_cmp, NULL, 0);
216 if (!inst->session_tree) {
217 radlog(L_ERR|L_CONS, "rlm_eap: Cannot initialize tree");
222 pthread_mutex_init(&(inst->session_mutex), NULL);
230 * For backwards compatibility.
232 static int eap_authenticate(void *instance, REQUEST *request)
235 EAP_HANDLER *handler;
236 eap_packet_t *eap_packet;
239 inst = (rlm_eap_t *) instance;
242 * Get the eap packet to start with
244 eap_packet = eap_attribute(request->packet->vps);
245 if (eap_packet == NULL) {
246 radlog(L_ERR, "rlm_eap: Malformed EAP Message");
247 return RLM_MODULE_FAIL;
251 * Create the eap handler. The eap_packet will end up being
252 * "swallowed" into the handler, so we can't access it after
255 handler = eap_handler(inst, &eap_packet, request);
256 if (handler == NULL) {
257 DEBUG2(" rlm_eap: Failed in handler");
258 return RLM_MODULE_INVALID;
262 * If it's a recursive request, then disallow
263 * TLS, TTLS, and PEAP, inside of the TLS tunnel.
265 if ((request->options & RAD_REQUEST_OPTION_FAKE_REQUEST) != 0) {
266 switch(handler->eap_ds->response->type.type) {
270 DEBUG2(" rlm_eap: Unable to tunnel TLS inside of TLS");
272 eap_handler_free(handler);
273 return RLM_MODULE_INVALID;
276 default: /* It may be OK, allow it to proceed */
283 * Select the appropriate eap_type or default to the
286 rcode = eaptype_select(inst, handler);
291 if (rcode == EAP_INVALID) {
293 eap_handler_free(handler);
294 DEBUG2(" rlm_eap: Failed in EAP select");
295 return RLM_MODULE_INVALID;
299 * If we're doing horrible tunneling work, remember it.
301 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
302 DEBUG2(" Not-EAP proxy set. Not composing EAP");
304 * Add the handle to the proxied list, so that we
305 * can retrieve it in the post-proxy stage, and
308 rcode = request_data_add(request,
309 inst, REQUEST_DATA_EAP_HANDLER,
310 handler, eap_handler_free);
311 rad_assert(rcode == 0);
313 return RLM_MODULE_HANDLED;
318 * Maybe the request was marked to be proxied. If so,
321 if (request->proxy != NULL) {
322 VALUE_PAIR *vp = NULL;
324 rad_assert(request->proxy_reply == NULL);
327 * Add the handle to the proxied list, so that we
328 * can retrieve it in the post-proxy stage, and
331 rcode = request_data_add(request,
332 inst, REQUEST_DATA_EAP_HANDLER,
333 handler, eap_handler_free);
334 rad_assert(rcode == 0);
337 * Some simple sanity checks. These should really
338 * be handled by the radius library...
340 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE);
342 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR);
344 vp = pairmake("Message-Authenticator",
346 rad_assert(vp != NULL);
347 pairadd(&(request->proxy->vps), vp);
352 * Delete the "proxied to" attribute, as it's
353 * set to 127.0.0.1 for tunneled requests, and
354 * we don't want to tell the world that...
356 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO);
358 DEBUG2(" Tunneled session will be proxied. Not doing EAP.");
359 return RLM_MODULE_HANDLED;
363 * We are done, wrap the EAP-request in RADIUS to send
364 * with all other required radius attributes
366 rcode = eap_compose(handler);
369 * Add to the list only if it is EAP-Request, OR if
370 * it's LEAP, and a response.
372 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
373 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
374 eaplist_add(inst, handler);
377 * LEAP is a little different. At Stage 4,
378 * it sends an EAP-Success message, but we still
379 * need to keep the State attribute & session
380 * data structure around for the AP Challenge.
382 * At stage 6, LEAP sends an EAP-Response, which
383 * isn't put into the list.
385 } else if ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
386 (handler->eap_ds->response->type.type == PW_EAP_LEAP) &&
387 (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
388 (handler->eap_ds->request->type.type == 0)) {
390 eaplist_add(inst, handler);
393 DEBUG2(" rlm_eap: Freeing handler");
394 /* handler is not required any more, free it now */
395 eap_handler_free(handler);
399 * If it's an Access-Accept, RFC 2869, Section 2.3.1
400 * says that we MUST include a User-Name attribute in the
403 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
408 * Doesn't exist, add it in.
410 vp = pairfind(request->reply->vps, PW_USER_NAME);
412 vp = pairmake("User-Name", request->username->vp_strvalue,
414 rad_assert(vp != NULL);
415 pairadd(&(request->reply->vps), vp);
419 * Cisco AP1230 has a bug and needs a zero
420 * terminated string in Access-Accept.
422 if ((inst->cisco_accounting_username_bug) &&
423 (vp->length < (int) sizeof(vp->vp_strvalue))) {
424 vp->vp_strvalue[vp->length] = '\0';
433 * EAP authorization DEPENDS on other rlm authorizations,
434 * to check for user existance & get their configured values.
435 * It Handles EAP-START Messages, User-Name initilization.
437 static int eap_authorize(void *instance, REQUEST *request)
443 inst = (rlm_eap_t *)instance;
446 * We don't do authorization again, once we've seen the
447 * proxy reply (or the proxied packet)
449 if (request->proxy != NULL)
450 return RLM_MODULE_NOOP;
453 * For EAP_START, send Access-Challenge with EAP Identity
454 * request. even when we have to proxy this request
456 * RFC 2869, Section 2.3.1 notes that the "domain" of the
457 * user, (i.e. where to proxy him) comes from the EAP-Identity,
458 * so we CANNOT proxy the user, until we know his identity.
460 * We therefore send an EAP Identity request.
462 status = eap_start(inst, request);
465 return RLM_MODULE_NOOP;
467 return RLM_MODULE_FAIL;
469 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->lvalue != PW_AUTHTYPE_REJECT)) {
487 vp = pairmake("Auth-Type", "EAP", T_OP_EQ);
489 return RLM_MODULE_FAIL;
491 pairadd(&request->config_items, vp);
494 return RLM_MODULE_UPDATED;
498 * If we're proxying EAP, then there may be magic we need
501 static int eap_post_proxy(void *inst, REQUEST *request)
505 EAP_HANDLER *handler;
508 * If there was a handler associated with this request,
509 * then it's a tunneled request which was proxied...
511 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
512 if (handler != NULL) {
514 eap_tunnel_data_t *data;
517 * Grab the tunnel callbacks from the request.
519 data = (eap_tunnel_data_t *) request_data_get(request,
521 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
523 radlog(L_ERR, "rlm_eap: Failed to retrieve callback for tunneled session!");
524 eap_handler_free(handler);
525 return RLM_MODULE_FAIL;
531 rcode = data->callback(handler, data->tls_session);
535 eap_handler_free(handler);
536 return RLM_MODULE_REJECT;
540 * We are done, wrap the EAP-request in RADIUS to send
541 * with all other required radius attributes
543 rcode = eap_compose(handler);
546 * Add to the list only if it is EAP-Request, OR if
547 * it's LEAP, and a response.
549 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
550 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
551 eaplist_add(inst, handler);
553 } else { /* couldn't have been LEAP, there's no tunnel */
554 DEBUG2(" rlm_eap: Freeing handler");
555 /* handler is not required any more, free it now */
556 eap_handler_free(handler);
560 * If it's an Access-Accept, RFC 2869, Section 2.3.1
561 * says that we MUST include a User-Name attribute in the
564 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
567 * Doesn't exist, add it in.
569 vp = pairfind(request->reply->vps, PW_USER_NAME);
571 vp = pairmake("User-Name", request->username->vp_strvalue,
573 rad_assert(vp != NULL);
574 pairadd(&(request->reply->vps), vp);
578 return RLM_MODULE_OK;
583 * There may be more than one Cisco-AVPair.
584 * Ensure we find the one with the LEAP attribute.
586 vp = request->proxy_reply->vps;
589 * Hmm... there's got to be a better way to
590 * discover codes for vendor attributes.
592 * This is vendor Cisco (9), Cisco-AVPair
595 vp = pairfind(vp, (9 << 16) | 1);
597 return RLM_MODULE_NOOP;
601 * If it's "leap:session-key", then stop.
603 * The format is VERY specific!
605 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
610 * Not this AV-pair. Go to the next one.
616 * The format is very specific.
618 if (vp->length != 17 + 34) {
619 DEBUG2(" rlm_eap: Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
620 vp->length, 17 + 34);
621 return RLM_MODULE_NOOP;
625 * Decrypt the session key, using the proxy data.
627 i = 34; /* starts off with 34 octets */
628 len = rad_tunnel_pwdecode(vp->vp_strvalue + 17, &i,
629 request->proxysecret,
630 request->proxy->vector);
633 * FIXME: Assert that i == 16.
637 * Encrypt the session key again, using the request data.
639 rad_tunnel_pwencode(vp->vp_strvalue + 17, &len,
641 request->packet->vector);
643 return RLM_MODULE_UPDATED;
648 * The module name should be the only globally exported symbol.
649 * That is, everything else should be 'static'.
654 RLM_TYPE_THREAD_SAFE, /* type */
655 eap_instantiate, /* instantiation */
656 eap_detach, /* detach */
658 eap_authenticate, /* authentication */
659 eap_authorize, /* authorization */
660 NULL, /* preaccounting */
661 NULL, /* accounting */
662 NULL, /* checksimul */
663 NULL, /* pre-proxy */
664 eap_post_proxy, /* post-proxy */