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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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>
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 */
44 static int eap_init(void)
51 * delete all the allocated space by eap module
53 static int eap_detach(void *instance)
58 inst = (rlm_eap_t *)instance;
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;
68 pthread_mutex_destroy(&(inst->session_mutex));
69 pthread_mutex_destroy(&(inst->module_mutex));
72 if (inst->default_eap_type_name) free(inst->default_eap_type_name);
80 * read the config section and load all the eap authentication types present.
82 static int eap_instantiate(CONF_SECTION *cs, void **instance)
89 inst = (rlm_eap_t *) malloc(sizeof(*inst));
93 memset(inst, 0, sizeof(*inst));
94 if (cf_section_parse(cs, inst, module_config) < 0) {
99 /* Load all the configured EAP-Types */
101 for(scs=cf_subsection_find_next(cs, NULL, NULL);
103 scs=cf_subsection_find_next(cs, scs, NULL)) {
107 auth_type = cf_section_name1(scs);
109 if (!auth_type) continue;
111 eap_type = eaptype_name2type(auth_type);
113 radlog(L_ERR|L_CONS, "rlm_eap: Unknown EAP type %s",
120 * If we're asked to load TTLS or PEAP, ensure
121 * that we've first loaded TLS.
123 if (((eap_type == PW_EAP_TTLS) ||
124 (eap_type == PW_EAP_PEAP)) &&
125 (inst->types[PW_EAP_TLS] == NULL)) {
126 radlog(L_ERR, "rlm_eap: Unable to load EAP-Type/%s, as EAP-Type/TLS is required first.",
134 if (eaptype_load(&inst->types[eap_type], eap_type, scs) < 0) {
139 num_types++; /* successfully loaded one more types */
142 if (num_types == 0) {
143 radlog(L_ERR|L_CONS, "rlm_eap: No EAP type configured, module cannot do anything.");
149 * Ensure that the default EAP type is loaded.
151 eap_type = eaptype_name2type(inst->default_eap_type_name);
153 radlog(L_ERR|L_CONS, "rlm_eap: Unknown default EAP type %s",
154 inst->default_eap_type_name);
159 if (inst->types[eap_type] == NULL) {
160 radlog(L_ERR|L_CONS, "rlm_eap: No such sub-type for default EAP type %s",
161 inst->default_eap_type_name);
165 inst->default_eap_type = eap_type; /* save the numerical type */
168 * List of sessions are set to NULL by the memset
172 /* Generate a state key, specific to eap */
175 #ifdef HAVE_PTHREAD_H
176 pthread_mutex_init(&(inst->session_mutex), NULL);
177 pthread_mutex_init(&(inst->module_mutex), NULL);
186 * FIXME: this should be done more intelligently...
188 static void my_handler_free(void *data)
190 EAP_HANDLER *handler = (EAP_HANDLER *) data;
191 eap_handler_free(&handler);
195 * For backwards compatibility.
197 static int eap_authenticate(void *instance, REQUEST *request)
200 EAP_HANDLER *handler;
201 eap_packet_t *eap_packet;
203 #ifdef HAVE_PTHREAD_H
207 inst = (rlm_eap_t *) instance;
210 * Get the eap packet to start with
212 eap_packet = eap_attribute(request->packet->vps);
213 if (eap_packet == NULL) {
214 radlog(L_ERR, "rlm_eap: Malformed EAP Message");
215 return RLM_MODULE_FAIL;
219 * Create the eap handler. The eap_packet will end up being
220 * "swallowed" into the handler, so we can't access it after
223 handler = eap_handler(inst, &eap_packet, request);
224 if (handler == NULL) {
225 DEBUG2(" rlm_eap: Failed in handler");
226 return RLM_MODULE_INVALID;
230 * If it's a recursive request, then disallow
231 * TLS, TTLS, and PEAP, inside of the TLS tunnel.
233 if ((request->options & RAD_REQUEST_OPTION_FAKE_REQUEST) != 0) {
234 switch(handler->eap_ds->response->type.type) {
238 DEBUG2(" rlm_eap: Unable to tunnel TLS inside of TLS");
240 eap_handler_free(&handler);
241 return RLM_MODULE_INVALID;
244 default: /* It may be OK, allow it to proceed */
250 #ifdef HAVE_PTHREAD_H
251 else { /* it's a normal request from a NAS */
253 * The OpenSSL code isn't strictly thread-safe,
254 * as we've got to provide callback functions.
256 * Rather than doing that, we just ensure that the
257 * sub-modules are locked via a mutex.
259 * Don't lock it if we're calling ourselves recursively,
260 * we've already got the lock.
262 pthread_mutex_lock(&(inst->module_mutex));
263 locked = TRUE; /* for recursive calls to the module */
268 * Select the appropriate eap_type or default to the
271 rcode = eaptype_select(inst, handler);
273 #ifdef HAVE_PTHREAD_H
274 if (locked) pthread_mutex_unlock(&(inst->module_mutex));
280 if (rcode == EAP_INVALID) {
282 eap_handler_free(&handler);
283 DEBUG2(" rlm_eap: Failed in EAP select");
284 return RLM_MODULE_INVALID;
288 * If we're doing horrible tunneling work, remember it.
290 if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
291 DEBUG2(" Not-EAP proxy set. Not composing EAP");
293 * Add the handle to the proxied list, so that we
294 * can retrieve it in the post-proxy stage, and
297 rcode = request_data_add(request,
298 inst, REQUEST_DATA_EAP_HANDLER,
299 handler, my_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,
322 handler, my_handler_free);
323 rad_assert(rcode == 0);
326 * Some simple sanity checks. These should really
327 * be handled by the radius library...
329 vp = pairfind(request->proxy->vps, PW_EAP_MESSAGE);
331 vp = pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR);
333 vp = pairmake("Message-Authenticator",
335 rad_assert(vp != NULL);
336 pairadd(&(request->proxy->vps), vp);
341 * Delete the "proxied to" attribute, as it's
342 * set to 127.0.0.1 for tunneled requests, and
343 * we don't want to tell the world that...
345 pairdelete(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO);
347 DEBUG2(" Tunneled session will be proxied. Not doing EAP.");
348 return RLM_MODULE_HANDLED;
352 * We are done, wrap the EAP-request in RADIUS to send
353 * with all other required radius attributes
355 rcode = eap_compose(handler);
358 * Add to the list only if it is EAP-Request, OR if
359 * it's LEAP, and a response.
361 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
362 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
363 eaplist_add(inst, handler);
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 } else if ((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 eaplist_add(inst, handler);
382 DEBUG2(" rlm_eap: Freeing handler");
383 /* handler is not required any more, free it now */
384 eap_handler_free(&handler);
388 * If it's an Access-Accept, RFC 2869, Section 2.3.1
389 * says that we MUST include a User-Name attribute in the
392 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
397 * Doesn't exist, add it in.
399 vp = pairfind(request->reply->vps, PW_USER_NAME);
401 vp = pairmake("User-Name", request->username->strvalue,
403 rad_assert(vp != NULL);
404 pairadd(&(request->reply->vps), vp);
408 * Cisco AP1230 has a bug and needs a zero
409 * terminated string in Access-Accept.
411 if ((inst->cisco_accounting_username_bug) &&
412 (vp->length < (int) sizeof(vp->strvalue))) {
413 vp->strvalue[vp->length] = '\0';
422 * EAP authorization DEPENDS on other rlm authorizations,
423 * to check for user existance & get their configured values.
424 * It Handles EAP-START Messages, User-Name initilization.
426 static int eap_authorize(void *instance, REQUEST *request)
432 inst = (rlm_eap_t *)instance;
435 * We don't do authorization again, once we've seen the
436 * proxy reply (or the proxied packet)
438 if (request->proxy != NULL)
439 return RLM_MODULE_NOOP;
442 * For EAP_START, send Access-Challenge with EAP Identity
443 * request. even when we have to proxy this request
445 * RFC 2869, Section 2.3.1 notes that the "domain" of the
446 * user, (i.e. where to proxy him) comes from the EAP-Identity,
447 * so we CANNOT proxy the user, until we know his identity.
449 * We therefore send an EAP Identity request.
451 status = eap_start(inst, request);
454 return RLM_MODULE_NOOP;
456 return RLM_MODULE_FAIL;
458 return RLM_MODULE_HANDLED;
465 * RFC 2869, Section 2.3.1. If a NAS sends an EAP-Identity,
466 * it MUST copy the identity into the User-Name attribute.
468 * But we don't worry about that too much. We depend on
469 * each EAP sub-module to look for handler->request->username,
470 * and to get excited if it doesn't appear.
473 vp = pairfind(request->config_items, PW_AUTH_TYPE);
475 (vp->lvalue != PW_AUTHTYPE_REJECT)) {
476 vp = pairmake("Auth-Type", "EAP", T_OP_EQ);
478 return RLM_MODULE_FAIL;
480 pairadd(&request->config_items, vp);
483 return RLM_MODULE_UPDATED;
487 * If we're proxying EAP, then there may be magic we need
490 static int eap_post_proxy(void *inst, REQUEST *request)
494 EAP_HANDLER *handler;
497 * If there was a handler associated with this request,
498 * then it's a tunneled request which was proxied...
500 handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
501 if (handler != NULL) {
503 eap_tunnel_data_t *data;
506 * Grab the tunnel callbacks from the request.
508 data = (eap_tunnel_data_t *) request_data_get(request,
510 REQUEST_DATA_EAP_TUNNEL_CALLBACK);
512 radlog(L_ERR, "rlm_eap: Failed to retrieve callback for tunneled session!");
513 eap_handler_free(&handler);
514 return RLM_MODULE_FAIL;
520 rcode = data->callback(handler, data->tls_session);
523 eap_handler_free(&handler);
524 return RLM_MODULE_REJECT;
528 * We are done, wrap the EAP-request in RADIUS to send
529 * with all other required radius attributes
531 rcode = eap_compose(handler);
534 * Add to the list only if it is EAP-Request, OR if
535 * it's LEAP, and a response.
537 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
538 (handler->eap_ds->request->type.type >= PW_EAP_MD5)) {
539 eaplist_add(inst, handler);
541 } else { /* couldn't have been LEAP, there's no tunnel */
542 DEBUG2(" rlm_eap: Freeing handler");
543 /* handler is not required any more, free it now */
544 eap_handler_free(&handler);
548 * If it's an Access-Accept, RFC 2869, Section 2.3.1
549 * says that we MUST include a User-Name attribute in the
552 if ((request->reply->code == PW_AUTHENTICATION_ACK) &&
555 * Doesn't exist, add it in.
557 vp = pairfind(request->reply->vps, PW_USER_NAME);
559 vp = pairmake("User-Name", request->username->strvalue,
561 rad_assert(vp != NULL);
562 pairadd(&(request->reply->vps), vp);
566 return RLM_MODULE_OK;
571 * There may be more than one Cisco-AVPair.
572 * Ensure we find the one with the LEAP attribute.
574 vp = request->proxy_reply->vps;
577 * Hmm... there's got to be a better way to
578 * discover codes for vendor attributes.
580 * This is vendor Cisco (9), Cisco-AVPair
583 vp = pairfind(vp, (9 << 16) | 1);
585 return RLM_MODULE_NOOP;
589 * If it's "leap:session-key", then stop.
591 * The format is VERY specific!
593 if (strncasecmp(vp->strvalue, "leap:session-key=", 17) == 0) {
598 * Not this AV-pair. Go to the next one.
604 * The format is very specific.
606 if (vp->length != 17 + 34) {
607 DEBUG2(" rlm_eap: Cisco-AVPair with leap:session-key has incorrect length %d: Expected %d",
608 vp->length, 17 + 34);
609 return RLM_MODULE_NOOP;
613 * Decrypt the session key, using the proxy data.
615 i = 34; /* starts off with 34 octets */
616 len = rad_tunnel_pwdecode(vp->strvalue + 17, &i,
617 request->proxysecret,
618 request->proxy->vector);
621 * FIXME: Assert that i == 16.
625 * Encrypt the session key again, using the request data.
627 rad_tunnel_pwencode(vp->strvalue + 17, &len,
629 request->packet->vector);
631 return RLM_MODULE_UPDATED;
636 * The module name should be the only globally exported symbol.
637 * That is, everything else should be 'static'.
641 RLM_TYPE_THREAD_SAFE, /* type */
642 eap_init, /* initialization */
643 eap_instantiate, /* instantiation */
645 eap_authenticate, /* authentication */
646 eap_authorize, /* authorization */
647 NULL, /* preaccounting */
648 NULL, /* accounting */
649 NULL, /* checksimul */
650 NULL, /* pre-proxy */
651 eap_post_proxy, /* post-proxy */
654 eap_detach, /* detach */