2 * eap.c rfc2284 & rfc2869 implementation
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>
28 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
29 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30 * | Code | Identifier | Length |
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * EAP Request and Response Packet Format
37 * --- ------- --- -------- ------ ------
39 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Code | Identifier | Length |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | Type | Type-Data ...
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
47 * EAP Success and Failure Packet Format
48 * --- ------- --- ------- ------ ------
50 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * | Code | Identifier | Length |
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 #include <freeradius-devel/ident.h>
62 static const char *eap_codes[] = {
63 "", /* 0 is invalid */
71 * Load all the required eap authentication types.
72 * Get all the supported EAP-types from config file.
74 int eaptype_load(EAP_TYPES **type, int eap_type, CONF_SECTION *cs)
78 const char *eaptype_name;
82 eaptype_name = eaptype_type2name(eap_type, namebuf, sizeof(namebuf));
83 snprintf(buffer, sizeof(buffer), "rlm_eap_%s", eaptype_name);
85 /* Link the loaded EAP-Type */
86 handle = lt_dlopenext(buffer);
88 radlog(L_ERR, "rlm_eap: Failed to link EAP-Type/%s: %s",
89 eaptype_name, lt_dlerror());
93 /* Make room for the EAP-Type */
94 node = (EAP_TYPES *)malloc(sizeof(EAP_TYPES));
96 radlog(L_ERR, "rlm_eap: out of memory");
99 memset(node, 0, sizeof(*node));
101 /* fill in the structure */
102 node->handle = handle;
106 * In general, this is a terrible idea. It works here
107 * solely because the eap_type2name function returns a
108 * 'static const char *' pointer sometimes, and we can
109 * ONLY link to module which are named in that static
112 node->typename = eaptype_name;
113 node->type_data = NULL;
115 node->type = (EAP_TYPE *)lt_dlsym(node->handle, buffer);
117 radlog(L_ERR, "rlm_eap: Failed linking to %s structure in %s: %s",
118 buffer, eaptype_name, lt_dlerror());
119 lt_dlclose(node->handle); /* ignore any errors */
123 cf_log_module(cs, "Linked to sub-module %s", buffer);
125 cf_log_module(cs, "Instantiating eap-%s", eaptype_name);
127 if ((node->type->attach) &&
128 ((node->type->attach)(node->cs, &(node->type_data)) < 0)) {
130 radlog(L_ERR, "rlm_eap: Failed to initialize type %s",
132 lt_dlclose(node->handle);
142 * Call the appropriate handle with the right eap_type.
144 static int eaptype_call(EAP_TYPES *atype, EAP_HANDLER *handler)
148 DEBUG2(" rlm_eap: processing type %s", atype->typename);
150 rad_assert(atype != NULL);
152 switch (handler->stage) {
154 if (!atype->type->initiate(atype->type_data, handler))
160 * The called function updates the EAP reply packet.
162 if (!atype->type->authorize ||
163 !atype->type->authorize(atype->type_data, handler))
169 * The called function updates the EAP reply packet.
171 if (!atype->type->authenticate ||
172 !atype->type->authenticate(atype->type_data, handler))
177 /* Should never enter here */
178 radlog(L_DBG, "rlm_eap: Invalid operation on eap_type");
187 * Based on TYPE, call the appropriate EAP-type handler
188 * Default to the configured EAP-Type
189 * for all Unsupported EAP-Types
191 int eaptype_select(rlm_eap_t *inst, EAP_HANDLER *handler)
194 unsigned int default_eap_type = inst->default_eap_type;
198 const char *eaptype_name;
200 eaptype = &handler->eap_ds->response->type;
203 * Don't trust anyone.
205 if ((eaptype->type == 0) ||
206 (eaptype->type > PW_EAP_MAX_TYPES)) {
207 DEBUG2(" rlm_eap: Asked to select bad type");
212 * Multiple levels of nesting are invalid.
214 if (handler->request->parent && handler->request->parent->parent) {
215 DEBUG2(" rlm_eap: Multiple levels of TLS nesting is invalid.");
220 * Figure out what to do.
222 switch(eaptype->type) {
223 case PW_EAP_IDENTITY:
224 DEBUG2(" rlm_eap: EAP Identity");
227 * Allow per-user configuration of EAP types.
229 vp = pairfind(handler->request->config_items,
231 if (vp) default_eap_type = vp->vp_integer;
237 if ((default_eap_type < PW_EAP_MD5) ||
238 (default_eap_type > PW_EAP_MAX_TYPES) ||
239 (inst->types[default_eap_type] == NULL)) {
240 DEBUG2(" rlm_eap: No such EAP type %s",
241 eaptype_type2name(default_eap_type,
242 namebuf, sizeof(namebuf)));
246 handler->stage = INITIATE;
247 handler->eap_type = default_eap_type;
250 * Wild & crazy stuff! For TTLS & PEAP, we
251 * initiate a TLS session, and then pass that
252 * session data to TTLS or PEAP for the
253 * authenticate stage.
255 * Handler->eap_type holds the TRUE type.
257 if ((default_eap_type == PW_EAP_TTLS) ||
258 (default_eap_type == PW_EAP_PEAP)) {
259 default_eap_type = PW_EAP_TLS;
262 if ((default_eap_type == PW_EAP_TNC) &&
263 !handler->request->parent) {
264 DEBUG2(" rlm_eap: ERROR: EAP-TNC must be run inside of a TLS method.");
268 if (eaptype_call(inst->types[default_eap_type],
270 DEBUG2(" rlm_eap: Default EAP type %s failed in initiate",
271 eaptype_type2name(default_eap_type,
272 namebuf, sizeof(namebuf)));
279 * The NAK data is the preferred EAP type(s) of
282 * RFC 3748 says to list one or more proposed
283 * alternative types, one per octet, or to use
284 * 0 for no alternative.
286 DEBUG2(" rlm_eap: EAP NAK");
289 * Delete old data, if necessary.
291 if (handler->opaque && handler->free_opaque) {
292 handler->free_opaque(handler->opaque);
293 handler->free_opaque = NULL;
294 handler->opaque = NULL;
297 if (eaptype->data == NULL) {
298 DEBUG2(" rlm_eap: Empty NAK packet, cannot decide what EAP type the client wants.");
303 * Pick one type out of the one they asked for,
304 * as they may have asked for many.
306 default_eap_type = 0;
307 vp = pairfind(handler->request->config_items,
309 for (i = 0; i < eaptype->length; i++) {
311 * It is invalid to request identity,
312 * notification & nak in nak.
314 * Type 0 is valid, and means there are no
317 if (eaptype->data[i] < PW_EAP_MD5) {
318 DEBUG2(" rlm_eap: NAK asked for bad type %d",
323 if ((eaptype->data[i] > PW_EAP_MAX_TYPES) ||
324 !inst->types[eaptype->data[i]]) {
325 DEBUG2(" rlm_eap: NAK asked for unsupported type %d",
330 eaptype_name = eaptype_type2name(eaptype->data[i],
335 * Prevent a firestorm if the client is confused.
337 if (handler->eap_type == eaptype->data[i]) {
338 DEBUG2(" rlm_eap: ERROR! Our request for %s was NAK'd with a request for %s. Skipping the requested type.",
339 eaptype_name, eaptype_name);
344 * Enforce per-user configuration of EAP
347 if (vp && (vp->vp_integer != eaptype->data[i])) {
349 DEBUG2(" rlm_eap: Client wants %s, while we require %s. Skipping the requested type.",
351 eaptype_type2name(vp->vp_integer,
357 default_eap_type = eaptype->data[i];
362 * We probably want to return 'fail' here...
364 if (!default_eap_type) {
365 DEBUG2(" rlm_eap: No common EAP types found.");
368 eaptype_name = eaptype_type2name(default_eap_type,
369 namebuf, sizeof(namebuf));
370 DEBUG2(" rlm_eap: EAP-NAK asked for EAP-Type/%s",
377 * Key off of the configured sub-modules.
380 eaptype_name = eaptype_type2name(eaptype->type,
383 DEBUG2(" rlm_eap: EAP/%s", eaptype_name);
386 * We haven't configured it, it doesn't exit.
388 if (!inst->types[eaptype->type]) {
389 DEBUG2(" rlm_eap: EAP type %d is unsupported",
394 rad_assert(handler->stage == AUTHENTICATE);
395 handler->eap_type = eaptype->type;
396 if (eaptype_call(inst->types[eaptype->type],
398 DEBUG2(" rlm_eap: Handler failed in EAP/%s",
410 * compose EAP reply packet in EAP-Message attr of RADIUS. If
411 * EAP exceeds 253, frame it in multiple EAP-Message attrs.
413 * Set the RADIUS reply codes based on EAP request codes. Append
414 * any additonal VPs to RADIUS reply
416 int eap_compose(EAP_HANDLER *handler)
419 eap_packet_t *eap_packet;
420 REQUEST *request = handler->request;
421 EAP_DS *eap_ds = handler->eap_ds;
422 EAP_PACKET *reply = eap_ds->request;
426 * The Id for the EAP packet to the NAS wasn't set.
429 * LEAP requires the Id to be incremented on EAP-Success
430 * in Stage 4, so that we can carry on the conversation
431 * where the client asks us to authenticate ourselves
434 if (!eap_ds->set_request_id) {
436 * Id serves to suppport request/response
437 * retransmission in the EAP layer and as such
438 * must be different for 'adjacent' packets
439 * except in case of success/failure-replies.
441 * RFC2716 (EAP-TLS) requires this to be
442 * incremented, RFC2284 only makes the above-
443 * mentioned restriction.
445 reply->id = handler->eap_ds->response->id;
447 switch (reply->code) {
449 * The Id is a simple "ack" for success
452 * RFC 3748 section 4.2 says
454 * ... The Identifier field MUST match
455 * the Identifier field of the Response
456 * packet that it is sent in response
464 * We've sent a response to their
465 * request, the Id is incremented.
471 DEBUG2(" rlm_eap: Underlying EAP-Type set EAP ID to %d",
476 * For Request & Response packets, set the EAP sub-type,
477 * if the EAP sub-module didn't already set it.
479 * This allows the TLS module to be "morphic", and means
480 * that the TTLS and PEAP modules can call it to do most
481 * of their dirty work.
483 if (((eap_ds->request->code == PW_EAP_REQUEST) ||
484 (eap_ds->request->code == PW_EAP_RESPONSE)) &&
485 (eap_ds->request->type.type == 0)) {
486 rad_assert(handler->eap_type >= PW_EAP_MD5);
487 rad_assert(handler->eap_type <= PW_EAP_MAX_TYPES);
489 eap_ds->request->type.type = handler->eap_type;
493 * FIXME: We malloc memory for the eap packet, and then
494 * immediately copy that data into VALUE_PAIRs. This
495 * could be done more efficiently...
497 if (eap_wireformat(reply) == EAP_INVALID) {
498 return RLM_MODULE_INVALID;
500 eap_packet = (eap_packet_t *)reply->packet;
502 vp = eap_packet2vp(eap_packet);
503 if (!vp) return RLM_MODULE_INVALID;
504 pairadd(&(request->reply->vps), vp);
507 * EAP-Message is always associated with
508 * Message-Authenticator but not vice-versa.
510 * Don't add a Message-Authenticator if it's already
513 vp = pairfind(request->reply->vps, PW_MESSAGE_AUTHENTICATOR);
515 vp = paircreate(PW_MESSAGE_AUTHENTICATOR, PW_TYPE_OCTETS);
516 memset(vp->vp_octets, 0, AUTH_VECTOR_LEN);
517 vp->length = AUTH_VECTOR_LEN;
518 pairadd(&(request->reply->vps), vp);
521 /* Set request reply code, but only if it's not already set. */
522 rcode = RLM_MODULE_OK;
523 if (!request->reply->code) switch(reply->code) {
524 case PW_EAP_RESPONSE:
525 request->reply->code = PW_AUTHENTICATION_ACK;
526 rcode = RLM_MODULE_HANDLED; /* leap weirdness */
529 request->reply->code = PW_AUTHENTICATION_ACK;
530 rcode = RLM_MODULE_OK;
533 request->reply->code = PW_AUTHENTICATION_REJECT;
534 rcode = RLM_MODULE_REJECT;
537 request->reply->code = PW_ACCESS_CHALLENGE;
538 rcode = RLM_MODULE_HANDLED;
542 * When we're pulling MS-CHAPv2 out of EAP-MS-CHAPv2,
543 * we do so WITHOUT setting a reply code, as the
544 * request is being proxied.
546 if (request->options & RAD_REQUEST_OPTION_PROXY_EAP) {
547 return RLM_MODULE_HANDLED;
550 /* Should never enter here */
551 radlog(L_ERR, "rlm_eap: reply code %d is unknown, Rejecting the request.", reply->code);
552 request->reply->code = PW_AUTHENTICATION_REJECT;
553 reply->code = PW_EAP_FAILURE;
554 rcode = RLM_MODULE_REJECT;
562 * Radius criteria, EAP-Message is invalid without Message-Authenticator
563 * For EAP_START, send Access-Challenge with EAP Identity request.
565 int eap_start(rlm_eap_t *inst, REQUEST *request)
567 VALUE_PAIR *vp, *proxy;
570 eap_msg = pairfind(request->packet->vps, PW_EAP_MESSAGE);
571 if (eap_msg == NULL) {
572 DEBUG2(" rlm_eap: No EAP-Message, not doing EAP");
577 * Look for EAP-Type = None (FreeRADIUS specific attribute)
578 * this allows you to NOT do EAP for some users.
580 vp = pairfind(request->packet->vps, PW_EAP_TYPE);
581 if (vp && vp->vp_integer == 0) {
582 DEBUG2(" rlm_eap: Found EAP-Message, but EAP-Type = None, so we're not doing EAP.");
587 * http://www.freeradius.org/rfc/rfc2869.html#EAP-Message
589 * Checks for Message-Authenticator are handled by rad_recv().
593 * Check for a Proxy-To-Realm. Don't get excited over LOCAL
596 proxy = pairfind(request->config_items, PW_PROXY_TO_REALM);
601 * If it's a LOCAL realm, then we're not proxying
604 realm = realm_find(proxy->vp_strvalue);
605 if (realm && (realm->auth_pool == NULL)) {
611 * Check the length before de-referencing the contents.
613 * Lengths of zero are required by the RFC for EAP-Start,
614 * but we've never seen them in practice.
616 * Lengths of two are what we see in practice as
619 if ((eap_msg->length == 0) || (eap_msg->length == 2)) {
624 * It's a valid EAP-Start, but the request
625 * was marked as being proxied. So we don't
626 * do EAP, as the home server will do it.
630 DEBUG2(" rlm_eap: Request is supposed to be proxied to Realm %s. Not doing EAP.", proxy->vp_strvalue);
634 DEBUG2(" rlm_eap: Got EAP_START message");
635 if ((eap_ds = eap_ds_alloc()) == NULL) {
636 DEBUG2(" rlm_eap: EAP Start failed in allocation");
641 * It's an EAP-Start packet. Tell them to stop wasting
642 * our time, and give us an EAP-Identity packet.
644 * Hmm... we should probably check the contents of the
645 * EAP-Start packet for something...
647 eap_ds->request->code = PW_EAP_REQUEST;
648 eap_ds->request->type.type = PW_EAP_IDENTITY;
651 * We don't have a handler, but eap_compose needs one,
652 * (for various reasons), so we fake it out here.
654 memset(&handler, 0, sizeof(handler));
655 handler.request = request;
656 handler.eap_ds = eap_ds;
658 eap_compose(&handler);
660 eap_ds_free(&eap_ds);
662 } /* end of handling EAP-Start */
665 * The EAP packet header is 4 bytes, plus one byte of
666 * EAP sub-type. Short packets are discarded, unless
669 if (eap_msg->length < (EAP_HEADER_LEN + 1)) {
670 if (proxy) goto do_proxy;
672 DEBUG2(" rlm_eap: Ignoring EAP-Message which is too short to be meaningful.");
677 * Create an EAP-Type containing the EAP-type
680 vp = paircreate(PW_EAP_TYPE, PW_TYPE_INTEGER);
682 vp->vp_integer = eap_msg->vp_octets[4];
683 pairadd(&(request->packet->vps), vp);
687 * If the request was marked to be proxied, do it now.
688 * This is done after checking for a valid length
689 * (which may not be good), and after adding the EAP-Type
690 * attribute. This lets other modules selectively cancel
691 * proxying based on EAP-Type.
693 if (proxy) goto do_proxy;
696 * From now on, we're supposed to be handling the
697 * EAP packet. We better understand it...
701 * We're allowed only a few codes. Request, Response,
702 * Success, or Failure.
704 if ((eap_msg->vp_octets[0] == 0) ||
705 (eap_msg->vp_octets[0] > PW_EAP_MAX_CODES)) {
706 DEBUG2(" rlm_eap: Unknown EAP packet");
708 DEBUG2(" rlm_eap: EAP packet type %s id %d length %d",
709 eap_codes[eap_msg->vp_octets[0]],
710 eap_msg->vp_octets[1],
715 * We handle request and responses. The only other defined
716 * codes are success and fail. The client SHOULD NOT be
717 * sending success/fail packets to us, as it doesn't make
720 if ((eap_msg->vp_octets[0] != PW_EAP_REQUEST) &&
721 (eap_msg->vp_octets[0] != PW_EAP_RESPONSE)) {
722 DEBUG2(" rlm_eap: Ignoring EAP packet which we don't know how to handle.");
727 * We've been told to ignore unknown EAP types, AND it's
728 * an unknown type. Return "NOOP", which will cause the
729 * eap_authorize() to return NOOP.
731 * EAP-Identity, Notification, and NAK are all handled
732 * internally, so they never have handlers.
734 if ((eap_msg->vp_octets[4] >= PW_EAP_MD5) &&
735 inst->ignore_unknown_eap_types &&
736 ((eap_msg->vp_octets[4] == 0) ||
737 (eap_msg->vp_octets[4] > PW_EAP_MAX_TYPES) ||
738 (inst->types[eap_msg->vp_octets[4]] == NULL))) {
739 DEBUG2(" rlm_eap: Ignoring Unknown EAP type");
744 * They're NAKing the EAP type we wanted to use, and
745 * asking for one which we don't support.
747 * NAK is code + id + length1 + length + NAK
748 * + requested EAP type(s).
750 * We know at this point that we can't handle the
751 * request. We could either return an EAP-Fail here, but
752 * it's not too critical.
754 * By returning "noop", we can ensure that authorize()
755 * returns NOOP, and another module may choose to proxy
758 if ((eap_msg->vp_octets[4] == PW_EAP_NAK) &&
759 (eap_msg->length >= (EAP_HEADER_LEN + 2)) &&
760 inst->ignore_unknown_eap_types &&
761 ((eap_msg->vp_octets[5] == 0) ||
762 (eap_msg->vp_octets[5] > PW_EAP_MAX_TYPES) ||
763 (inst->types[eap_msg->vp_octets[5]] == NULL))) {
764 DEBUG2(" rlm_eap: Ignoring NAK with request for unknown EAP type");
768 if ((eap_msg->vp_octets[4] == PW_EAP_TTLS) ||
769 (eap_msg->vp_octets[4] == PW_EAP_PEAP)) {
770 DEBUG2(" rlm_eap: Continuing tunnel setup.");
775 * Later EAP messages are longer than the 'start'
776 * message, so if everything is OK, this function returns
777 * 'no start found', so that the rest of the EAP code can
778 * use the State attribute to match this EAP-Message to
779 * an ongoing conversation.
781 DEBUG2(" rlm_eap: No EAP Start, assuming it's an on-going EAP conversation");
787 * compose EAP FAILURE packet in EAP-Message
789 void eap_fail(EAP_HANDLER *handler)
792 * Delete any previous replies.
794 pairdelete(&handler->request->reply->vps, PW_EAP_MESSAGE);
795 pairdelete(&handler->request->reply->vps, PW_STATE);
797 eap_packet_free(&handler->eap_ds->request);
798 handler->eap_ds->request = eap_packet_alloc();
800 handler->eap_ds->request->code = PW_EAP_FAILURE;
801 eap_compose(handler);
805 * compose EAP SUCCESS packet in EAP-Message
807 void eap_success(EAP_HANDLER *handler)
809 handler->eap_ds->request->code = PW_EAP_SUCCESS;
810 eap_compose(handler);
814 * Basic EAP packet verfications & validations
816 static int eap_validation(eap_packet_t *eap_packet)
820 memcpy(&len, eap_packet->length, sizeof(uint16_t));
824 * High level EAP packet checks
826 if ((len <= EAP_HEADER_LEN) ||
827 ((eap_packet->code != PW_EAP_RESPONSE) &&
828 (eap_packet->code != PW_EAP_REQUEST)) ||
829 (eap_packet->data[0] <= 0) ||
830 (eap_packet->data[0] > PW_EAP_MAX_TYPES)) {
832 radlog(L_AUTH, "rlm_eap: Incorrect EAP Message, "
833 "Ignoring the packet");
837 /* we don't expect notification, but we send it */
838 if (eap_packet->data[0] == PW_EAP_NOTIFICATION) {
839 radlog(L_AUTH, "rlm_eap: Got NOTIFICATION, "
840 "Ignoring the packet");
849 * Get the user Identity only from EAP-Identity packets
851 static char *eap_identity(eap_packet_t *eap_packet)
857 if ((eap_packet == NULL) ||
858 (eap_packet->code != PW_EAP_RESPONSE) ||
859 (eap_packet->data[0] != PW_EAP_IDENTITY)) {
863 memcpy(&len, eap_packet->length, sizeof(uint16_t));
866 if ((len <= 5) || (eap_packet->data[1] == 0x00)) {
867 radlog(L_ERR, "rlm_eap: UserIdentity Unknown ");
872 identity = (char *)malloc(size + 1);
873 if (identity == NULL) {
874 radlog(L_ERR, "rlm_eap: out of memory");
877 memcpy(identity, &eap_packet->data[1], size);
878 identity[size] = '\0';
885 * Create our Request-Response data structure with the eap packet
887 static EAP_DS *eap_buildds(eap_packet_t **eap_packet_p)
889 EAP_DS *eap_ds = NULL;
890 eap_packet_t *eap_packet = *eap_packet_p;
894 if ((eap_ds = eap_ds_alloc()) == NULL) {
898 eap_ds->response->packet = (unsigned char *)eap_packet;
899 eap_ds->response->code = eap_packet->code;
900 eap_ds->response->id = eap_packet->id;
901 eap_ds->response->type.type = eap_packet->data[0];
903 memcpy(&len, eap_packet->length, sizeof(uint16_t));
905 eap_ds->response->length = len;
908 * We've eaten the eap packet into the eap_ds.
910 *eap_packet_p = NULL;
913 * First 5 bytes in eap, are code + id + length(2) + type.
915 * The rest is type-specific data. We skip type while
916 * getting typedata from data.
918 typelen = len - 5/*code + id + length + type */;
921 * Since the packet contains the complete
922 * eap_packet, typedata will be a ptr in packet
925 eap_ds->response->type.data = eap_ds->response->packet + 5/*code+id+length+type*/;
926 eap_ds->response->type.length = typelen;
928 eap_ds->response->type.length = 0;
929 eap_ds->response->type.data = NULL;
937 * If identity response then create a fresh handler & fill the identity
938 * else handler MUST be in our list, get that.
939 * This handler creation cannot fail
941 * username contains REQUEST->username which might have been stripped.
942 * identity contains the one sent in EAP-Identity response
944 EAP_HANDLER *eap_handler(rlm_eap_t *inst, eap_packet_t **eap_packet_p,
947 EAP_HANDLER *handler = NULL;
948 eap_packet_t *eap_packet = *eap_packet_p;
952 * Ensure it's a valid EAP-Request, or EAP-Response.
954 if (eap_validation(eap_packet) == EAP_INVALID) {
956 *eap_packet_p = NULL;
961 * EAP_HANDLER MUST be found in the list if it is not
962 * EAP-Identity response
964 if (eap_packet->data[0] != PW_EAP_IDENTITY) {
965 handler = eaplist_find(inst, request, eap_packet);
966 if (handler == NULL) {
967 /* Either send EAP_Identity or EAP-Fail */
968 radlog(L_ERR, "rlm_eap: Either EAP-request timed out OR"
969 " EAP-response to an unknown EAP-request");
971 *eap_packet_p = NULL;
976 * Even more paranoia. Without this, some weird
977 * clients could do crazy things.
979 * It's ok to send EAP sub-type NAK in response
980 * to a request for a particular type, but it's NOT
981 * OK to blindly return data for another type.
983 if ((eap_packet->data[0] != PW_EAP_NAK) &&
984 (eap_packet->data[0] != handler->eap_type)) {
985 radlog(L_ERR, "rlm_eap: Response appears to match, but EAP type is wrong.");
987 *eap_packet_p = NULL;
991 vp = pairfind(request->packet->vps, PW_USER_NAME);
994 * NAS did not set the User-Name
995 * attribute, so we set it here and
996 * prepend it to the beginning of the
997 * request vps so that autz's work
1000 DEBUG2("rlm_eap: Broken NAS did not set User-Name, setting from EAP Identity");
1001 vp = pairmake("User-Name", handler->identity, T_OP_EQ);
1003 radlog(L_ERR, "rlm_eap: out of memory");
1004 free(*eap_packet_p);
1005 *eap_packet_p = NULL;
1008 vp->next = request->packet->vps;
1009 request->packet->vps = vp;
1013 * A little more paranoia. If the NAS
1014 * *did* set the User-Name, and it doesn't
1015 * match the identity, (i.e. If they
1016 * change their User-Name part way through
1017 * the EAP transaction), then reject the
1018 * request as the NAS is doing something
1021 if (strncmp(handler->identity, vp->vp_strvalue,
1022 MAX_STRING_LEN) != 0) {
1023 radlog(L_ERR, "rlm_eap: Identity does not match User-Name. Authentication failed.");
1024 free(*eap_packet_p);
1025 *eap_packet_p = NULL;
1029 } else { /* packet was EAP identity */
1030 handler = eap_handler_alloc();
1031 if (handler == NULL) {
1032 radlog(L_ERR, "rlm_eap: out of memory");
1033 free(*eap_packet_p);
1034 *eap_packet_p = NULL;
1039 * All fields in the handler are set to zero.
1041 handler->identity = eap_identity(eap_packet);
1042 if (handler->identity == NULL) {
1043 radlog(L_ERR, "rlm_eap: Identity Unknown, authentication failed");
1044 free(*eap_packet_p);
1045 *eap_packet_p = NULL;
1046 eap_handler_free(handler);
1050 vp = pairfind(request->packet->vps, PW_USER_NAME);
1053 * NAS did not set the User-Name
1054 * attribute, so we set it here and
1055 * prepend it to the beginning of the
1056 * request vps so that autz's work
1059 DEBUG2("rlm_eap: WARNING NAS did not set User-Name. Setting it locally from EAP Identity");
1060 vp = pairmake("User-Name", handler->identity, T_OP_EQ);
1062 radlog(L_ERR, "rlm_eap: out of memory");
1063 free(*eap_packet_p);
1064 *eap_packet_p = NULL;
1065 eap_handler_free(handler);
1068 vp->next = request->packet->vps;
1069 request->packet->vps = vp;
1072 * Paranoia. If the NAS *did* set the
1073 * User-Name, and it doesn't match the
1074 * identity, the NAS is doing something
1075 * funny, so reject the request.
1077 if (strncmp(handler->identity, vp->vp_strvalue,
1078 MAX_STRING_LEN) != 0) {
1079 radlog(L_ERR, "rlm_eap: Identity does not match User-Name, setting from EAP Identity.");
1080 free(*eap_packet_p);
1081 *eap_packet_p = NULL;
1082 eap_handler_free(handler);
1088 handler->eap_ds = eap_buildds(eap_packet_p);
1089 if (handler->eap_ds == NULL) {
1090 free(*eap_packet_p);
1091 *eap_packet_p = NULL;
1092 eap_handler_free(handler);
1096 handler->timestamp = request->timestamp;
1097 handler->request = request; /* LEAP needs this */