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 if ((node->type->attach) &&
124 ((node->type->attach)(node->cs, &(node->type_data)) < 0)) {
126 radlog(L_ERR, "rlm_eap: Failed to initialize type %s",
128 lt_dlclose(node->handle);
133 DEBUG("rlm_eap: Loaded and initialized type %s", eaptype_name);
139 * Call the appropriate handle with the right eap_type.
141 static int eaptype_call(EAP_TYPES *atype, EAP_HANDLER *handler)
145 DEBUG2(" rlm_eap: processing type %s", atype->typename);
147 rad_assert(atype != NULL);
149 switch (handler->stage) {
151 if (!atype->type->initiate(atype->type_data, handler))
157 * The called function updates the EAP reply packet.
159 if (!atype->type->authorize ||
160 !atype->type->authorize(atype->type_data, handler))
166 * The called function updates the EAP reply packet.
168 if (!atype->type->authenticate ||
169 !atype->type->authenticate(atype->type_data, handler))
174 /* Should never enter here */
175 radlog(L_DBG, "rlm_eap: Invalid operation on eap_type");
184 * Based on TYPE, call the appropriate EAP-type handler
185 * Default to the configured EAP-Type
186 * for all Unsupported EAP-Types
188 int eaptype_select(rlm_eap_t *inst, EAP_HANDLER *handler)
190 unsigned int default_eap_type = inst->default_eap_type;
194 const char *eaptype_name;
196 eaptype = &handler->eap_ds->response->type;
199 * Don't trust anyone.
201 if ((eaptype->type == 0) ||
202 (eaptype->type > PW_EAP_MAX_TYPES)) {
203 DEBUG2(" rlm_eap: Asked to select bad type");
208 * Figure out what to do.
210 switch(eaptype->type) {
211 case PW_EAP_IDENTITY:
212 DEBUG2(" rlm_eap: EAP Identity");
215 * Allow per-user configuration of EAP types.
217 vp = pairfind(handler->request->config_items,
219 if (vp) default_eap_type = vp->lvalue;
225 if ((default_eap_type < PW_EAP_MD5) ||
226 (default_eap_type > PW_EAP_MAX_TYPES) ||
227 (inst->types[default_eap_type] == NULL)) {
228 DEBUG2(" rlm_eap: No such EAP type %s",
229 eaptype_type2name(default_eap_type,
230 namebuf, sizeof(namebuf)));
234 handler->stage = INITIATE;
235 handler->eap_type = default_eap_type;
238 * Wild & crazy stuff! For TTLS & PEAP, we
239 * initiate a TLS session, and then pass that
240 * session data to TTLS or PEAP for the
241 * authenticate stage.
243 * Handler->eap_type holds the TRUE type.
245 if ((default_eap_type == PW_EAP_TTLS) ||
246 (default_eap_type == PW_EAP_PEAP)) {
247 default_eap_type = PW_EAP_TLS;
252 * We don't do TLS inside of TLS, as it's a bad
255 if (((handler->request->options & RAD_REQUEST_OPTION_FAKE_REQUEST) != 0) &&
256 (default_eap_type == PW_EAP_TLS)) {
257 DEBUG2(" rlm_eap: Unable to tunnel TLS inside of TLS");
261 if (eaptype_call(inst->types[default_eap_type],
263 DEBUG2(" rlm_eap: Default EAP type %s failed in initiate",
264 eaptype_type2name(default_eap_type,
265 namebuf, sizeof(namebuf)));
272 * The NAK data is the preferred EAP type(s) of
275 * RFC 3748 says to list one or more proposed
276 * alternative types, one per octet, or to use
277 * 0 for no alternative.
279 DEBUG2(" rlm_eap: EAP NAK");
282 * Delete old data, if necessary.
284 if (handler->opaque && handler->free_opaque) {
285 handler->free_opaque(handler->opaque);
286 handler->free_opaque = NULL;
287 handler->opaque = NULL;
291 * It is invalid to request identity,
292 * notification & nak in nak
294 if (eaptype->data == NULL) {
295 DEBUG2(" rlm_eap: Empty NAK packet, cannot decide what EAP type the client wants.");
300 * FIXME: Pick one type out of the one they asked
301 * for, as they may have asked for many.
303 if ((eaptype->data[0] < PW_EAP_MD5) ||
304 (eaptype->data[0] > PW_EAP_MAX_TYPES)) {
305 DEBUG2(" rlm_eap: NAK asked for bad type %d",
310 default_eap_type = eaptype->data[0];
311 eaptype_name = eaptype_type2name(default_eap_type,
312 namebuf, sizeof(namebuf));
313 DEBUG2(" rlm_eap: EAP-NAK asked for EAP-Type/%s",
317 * Prevent a firestorm if the client is confused.
319 if (handler->eap_type == default_eap_type) {
320 DEBUG2(" rlm_eap: ERROR! Our request for %s was NAK'd with a request for %s, what is the client thinking?",
321 eaptype_name, eaptype_name);
326 * Enforce per-user configuration of EAP types.
328 vp = pairfind(handler->request->config_items,
330 if (vp && (vp->lvalue != default_eap_type)) {
332 DEBUG2(" rlm_eap: Client wants %s, while we require %s, rejecting the user.",
334 eaptype_type2name(vp->lvalue,
343 * Key off of the configured sub-modules.
346 eaptype_name = eaptype_type2name(eaptype->type,
349 DEBUG2(" rlm_eap: EAP/%s", eaptype_name);
352 * We haven't configured it, it doesn't exit.
354 if (!inst->types[eaptype->type]) {
355 DEBUG2(" rlm_eap: EAP type %d is unsupported",
360 rad_assert(handler->stage == AUTHENTICATE);
361 handler->eap_type = eaptype->type;
362 if (eaptype_call(inst->types[eaptype->type],
364 DEBUG2(" rlm_eap: Handler failed in EAP/%s",
376 * EAP packet format to be sent over the wire
378 * i.e. code+id+length+data where data = null/type+typedata
381 static int eap_wireformat(EAP_PACKET *reply)
384 uint16_t total_length = 0;
386 if (reply == NULL) return EAP_INVALID;
388 total_length = EAP_HEADER_LEN;
389 if (reply->code < 3) {
390 total_length += 1/*EAPtype*/;
391 if (reply->type.data && reply->type.length > 0) {
392 total_length += reply->type.length;
396 reply->packet = (unsigned char *)malloc(total_length);
397 hdr = (eap_packet_t *)reply->packet;
399 radlog(L_ERR, "rlm_eap: out of memory");
403 hdr->code = (reply->code & 0xFF);
404 hdr->id = (reply->id & 0xFF);
405 total_length = htons(total_length);
406 memcpy(hdr->length, &total_length, sizeof(uint16_t));
409 * Request and Response packets are special.
411 if ((reply->code == PW_EAP_REQUEST) ||
412 (reply->code == PW_EAP_RESPONSE)) {
413 hdr->data[0] = (reply->type.type & 0xFF);
416 * Here since we cannot know the typedata format and length
418 * Type_data is expected to be wired by each EAP-Type
420 * Zero length/No typedata is supported as long as
423 if (reply->type.data && reply->type.length > 0) {
424 memcpy(&hdr->data[1], reply->type.data, reply->type.length);
425 free(reply->type.data);
426 reply->type.data = reply->packet + EAP_HEADER_LEN + 1/*EAPtype*/;
434 * compose EAP reply packet in EAP-Message attr of RADIUS. If
435 * EAP exceeds 253, frame it in multiple EAP-Message attrs.
437 * Set the RADIUS reply codes based on EAP request codes. Append
438 * any additonal VPs to RADIUS reply
440 int eap_compose(EAP_HANDLER *handler)
442 uint16_t eap_len, len;
445 eap_packet_t *eap_packet;
447 REQUEST *request = handler->request;
448 EAP_DS *eap_ds = handler->eap_ds;
449 EAP_PACKET *reply = eap_ds->request;
453 * The Id for the EAP packet to the NAS wasn't set.
456 * LEAP requires the Id to be incremented on EAP-Success
457 * in Stage 4, so that we can carry on the conversation
458 * where the client asks us to authenticate ourselves
461 if (!eap_ds->set_request_id) {
463 * Id serves to suppport request/response
464 * retransmission in the EAP layer and as such
465 * must be different for 'adjacent' packets
466 * except in case of success/failure-replies.
468 * RFC2716 (EAP-TLS) requires this to be
469 * incremented, RFC2284 only makes the above-
470 * mentioned restriction.
472 reply->id = handler->eap_ds->response->id;
474 switch (reply->code) {
476 * The Id is a simple "ack" for success
479 * RFC 3748 section 4.2 says
481 * ... The Identifier field MUST match
482 * the Identifier field of the Response
483 * packet that it is sent in response
491 * We've sent a response to their
492 * request, the Id is incremented.
498 DEBUG2(" rlm_eap: Underlying EAP-Type set EAP ID to %d",
503 * For Request & Response packets, set the EAP sub-type,
504 * if the EAP sub-module didn't already set it.
506 * This allows the TLS module to be "morphic", and means
507 * that the TTLS and PEAP modules can call it to do most
508 * of their dirty work.
510 if (((eap_ds->request->code == PW_EAP_REQUEST) ||
511 (eap_ds->request->code == PW_EAP_RESPONSE)) &&
512 (eap_ds->request->type.type == 0)) {
513 rad_assert(handler->eap_type >= PW_EAP_MD5);
514 rad_assert(handler->eap_type <= PW_EAP_MAX_TYPES);
516 eap_ds->request->type.type = handler->eap_type;
520 if (eap_wireformat(reply) == EAP_INVALID) {
521 return RLM_MODULE_INVALID;
523 eap_packet = (eap_packet_t *)reply->packet;
525 memcpy(&eap_len, &(eap_packet->length), sizeof(uint16_t));
526 len = eap_len = ntohs(eap_len);
527 ptr = (unsigned char *)eap_packet;
539 * create a value pair & append it to the request reply list
540 * This memory gets freed up when request is freed up
542 eap_msg = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
543 memcpy(eap_msg->vp_octets, ptr, len);
544 eap_msg->length = len;
545 pairadd(&(request->reply->vps), eap_msg);
551 * EAP-Message is always associated with
552 * Message-Authenticator but not vice-versa.
554 * Don't add a Message-Authenticator if it's already
557 vp = pairfind(request->reply->vps, PW_MESSAGE_AUTHENTICATOR);
559 vp = paircreate(PW_MESSAGE_AUTHENTICATOR, PW_TYPE_OCTETS);
560 memset(vp->vp_octets, 0, AUTH_VECTOR_LEN);
561 vp->length = AUTH_VECTOR_LEN;
562 pairadd(&(request->reply->vps), vp);
565 /* Set request reply code, but only if it's not already set. */
566 rcode = RLM_MODULE_OK;
567 if (!request->reply->code) switch(reply->code) {
568 case PW_EAP_RESPONSE:
569 request->reply->code = PW_AUTHENTICATION_ACK;
570 rcode = RLM_MODULE_HANDLED; /* leap weirdness */
573 request->reply->code = PW_AUTHENTICATION_ACK;
574 rcode = RLM_MODULE_OK;
577 request->reply->code = PW_AUTHENTICATION_REJECT;
578 rcode = RLM_MODULE_REJECT;
581 request->reply->code = PW_ACCESS_CHALLENGE;
582 rcode = RLM_MODULE_HANDLED;
586 * When we're pulling MS-CHAPv2 out of EAP-MS-CHAPv2,
587 * we do so WITHOUT setting a reply code, as the
588 * request is being proxied.
590 if (request->options & RAD_REQUEST_OPTION_PROXY_EAP) {
591 return RLM_MODULE_HANDLED;
594 /* Should never enter here */
595 radlog(L_ERR, "rlm_eap: reply code %d is unknown, Rejecting the request.", reply->code);
596 request->reply->code = PW_AUTHENTICATION_REJECT;
597 reply->code = PW_EAP_FAILURE;
598 rcode = RLM_MODULE_REJECT;
606 * Radius criteria, EAP-Message is invalid without Message-Authenticator
607 * For EAP_START, send Access-Challenge with EAP Identity request.
609 int eap_start(rlm_eap_t *inst, REQUEST *request)
611 VALUE_PAIR *vp, *proxy;
614 eap_msg = pairfind(request->packet->vps, PW_EAP_MESSAGE);
615 if (eap_msg == NULL) {
616 DEBUG2(" rlm_eap: No EAP-Message, not doing EAP");
621 * Look for EAP-Type = None (FreeRADIUS specific attribute)
622 * this allows you to NOT do EAP for some users.
624 vp = pairfind(request->packet->vps, PW_EAP_TYPE);
625 if (vp && vp->lvalue == 0) {
626 DEBUG2(" rlm_eap: Found EAP-Message, but EAP-Type = None, so we're not doing EAP.");
631 * http://www.freeradius.org/rfc/rfc2869.html#EAP-Message
633 * Checks for Message-Authenticator are handled by rad_recv().
637 * Check for a Proxy-To-Realm. Don't get excited over LOCAL
640 proxy = pairfind(request->config_items, PW_PROXY_TO_REALM);
645 * If it's a LOCAL realm, then we're not proxying
648 realm = realm_find(proxy->vp_strvalue, 0);
649 rad_assert(realm->ipaddr.af == AF_INET);
650 if (realm && (realm->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_NONE))) {
656 * Check the length before de-referencing the contents.
658 * Lengths of zero are required by the RFC for EAP-Start,
659 * but we've never seen them in practice.
661 * Lengths of two are what we see in practice as
664 if ((eap_msg->length == 0) || (eap_msg->length == 2)) {
669 * It's a valid EAP-Start, but the request
670 * was marked as being proxied. So we don't
671 * do EAP, as the home server will do it.
675 DEBUG2(" rlm_eap: Request is supposed to be proxied to Realm %s. Not doing EAP.", proxy->vp_strvalue);
679 DEBUG2(" rlm_eap: Got EAP_START message");
680 if ((eap_ds = eap_ds_alloc()) == NULL) {
681 DEBUG2(" rlm_eap: EAP Start failed in allocation");
686 * It's an EAP-Start packet. Tell them to stop wasting
687 * our time, and give us an EAP-Identity packet.
689 * Hmm... we should probably check the contents of the
690 * EAP-Start packet for something...
692 eap_ds->request->code = PW_EAP_REQUEST;
693 eap_ds->request->type.type = PW_EAP_IDENTITY;
696 * We don't have a handler, but eap_compose needs one,
697 * (for various reasons), so we fake it out here.
699 memset(&handler, 0, sizeof(handler));
700 handler.request = request;
701 handler.eap_ds = eap_ds;
703 eap_compose(&handler);
705 eap_ds_free(&eap_ds);
707 } /* end of handling EAP-Start */
710 * The EAP packet header is 4 bytes, plus one byte of
711 * EAP sub-type. Short packets are discarded, unless
714 if (eap_msg->length < (EAP_HEADER_LEN + 1)) {
715 if (proxy) goto do_proxy;
717 DEBUG2(" rlm_eap: Ignoring EAP-Message which is too short to be meaningful.");
722 * Create an EAP-Type containing the EAP-type
725 vp = paircreate(PW_EAP_TYPE, PW_TYPE_INTEGER);
727 vp->lvalue = eap_msg->vp_octets[4];
728 pairadd(&(request->packet->vps), vp);
732 * If the request was marked to be proxied, do it now.
733 * This is done after checking for a valid length
734 * (which may not be good), and after adding the EAP-Type
735 * attribute. This lets other modules selectively cancel
736 * proxying based on EAP-Type.
738 if (proxy) goto do_proxy;
741 * From now on, we're supposed to be handling the
742 * EAP packet. We better understand it...
746 * We're allowed only a few codes. Request, Response,
747 * Success, or Failure.
749 if ((eap_msg->vp_octets[0] == 0) ||
750 (eap_msg->vp_octets[0] > PW_EAP_MAX_CODES)) {
751 DEBUG2(" rlm_eap: Unknown EAP packet");
753 DEBUG2(" rlm_eap: EAP packet type %s id %d length %d",
754 eap_codes[eap_msg->vp_octets[0]],
755 eap_msg->vp_octets[1],
760 * We handle request and responses. The only other defined
761 * codes are success and fail. The client SHOULD NOT be
762 * sending success/fail packets to us, as it doesn't make
765 if ((eap_msg->vp_octets[0] != PW_EAP_REQUEST) &&
766 (eap_msg->vp_octets[0] != PW_EAP_RESPONSE)) {
767 DEBUG2(" rlm_eap: Ignoring EAP packet which we don't know how to handle.");
772 * We've been told to ignore unknown EAP types, AND it's
773 * an unknown type. Return "NOOP", which will cause the
774 * eap_authorize() to return NOOP.
776 * EAP-Identity, Notification, and NAK are all handled
777 * internally, so they never have handlers.
779 if ((eap_msg->vp_octets[4] >= PW_EAP_MD5) &&
780 inst->ignore_unknown_eap_types &&
781 ((eap_msg->vp_octets[4] == 0) ||
782 (eap_msg->vp_octets[4] > PW_EAP_MAX_TYPES) ||
783 (inst->types[eap_msg->vp_octets[4]] == NULL))) {
784 DEBUG2(" rlm_eap: Ignoring Unknown EAP type");
789 * They're NAKing the EAP type we wanted to use, and
790 * asking for one which we don't support.
792 * NAK is code + id + length1 + length + NAK
793 * + requested EAP type(s).
795 * We know at this point that we can't handle the
796 * request. We could either return an EAP-Fail here, but
797 * it's not too critical.
799 * By returning "noop", we can ensure that authorize()
800 * returns NOOP, and another module may choose to proxy
803 if ((eap_msg->vp_octets[4] == PW_EAP_NAK) &&
804 (eap_msg->length >= (EAP_HEADER_LEN + 2)) &&
805 inst->ignore_unknown_eap_types &&
806 ((eap_msg->vp_octets[5] == 0) ||
807 (eap_msg->vp_octets[5] > PW_EAP_MAX_TYPES) ||
808 (inst->types[eap_msg->vp_octets[5]] == NULL))) {
809 DEBUG2(" rlm_eap: Ignoring NAK with request for unknown EAP type");
814 * Later EAP messages are longer than the 'start'
815 * message, so if everything is OK, this function returns
816 * 'no start found', so that the rest of the EAP code can
817 * use the State attribute to match this EAP-Message to
818 * an ongoing conversation.
820 DEBUG2(" rlm_eap: No EAP Start, assuming it's an on-going EAP conversation");
826 * compose EAP FAILURE packet in EAP-Message
828 void eap_fail(EAP_HANDLER *handler)
830 handler->eap_ds->request->code = PW_EAP_FAILURE;
831 eap_compose(handler);
835 * compose EAP SUCCESS packet in EAP-Message
837 void eap_success(EAP_HANDLER *handler)
839 handler->eap_ds->request->code = PW_EAP_SUCCESS;
840 eap_compose(handler);
844 * Basic EAP packet verfications & validations
846 static int eap_validation(eap_packet_t *eap_packet)
850 memcpy(&len, eap_packet->length, sizeof(uint16_t));
854 * High level EAP packet checks
856 if ((len <= EAP_HEADER_LEN) ||
857 ((eap_packet->code != PW_EAP_RESPONSE) &&
858 (eap_packet->code != PW_EAP_REQUEST)) ||
859 (eap_packet->data[0] <= 0) ||
860 (eap_packet->data[0] > PW_EAP_MAX_TYPES)) {
862 radlog(L_AUTH, "rlm_eap: Incorrect EAP Message, "
863 "Ignoring the packet");
867 /* we don't expect notification, but we send it */
868 if (eap_packet->data[0] == PW_EAP_NOTIFICATION) {
869 radlog(L_AUTH, "rlm_eap: Got NOTIFICATION, "
870 "Ignoring the packet");
879 * Get the user Identity only from EAP-Identity packets
881 static char *eap_identity(eap_packet_t *eap_packet)
887 if ((eap_packet == NULL) ||
888 (eap_packet->code != PW_EAP_RESPONSE) ||
889 (eap_packet->data[0] != PW_EAP_IDENTITY)) {
893 memcpy(&len, eap_packet->length, sizeof(uint16_t));
896 if ((len <= 5) || (eap_packet->data[1] == 0x00)) {
897 radlog(L_ERR, "rlm_eap: UserIdentity Unknown ");
902 identity = (char *)malloc(size + 1);
903 if (identity == NULL) {
904 radlog(L_ERR, "rlm_eap: out of memory");
907 memcpy(identity, &eap_packet->data[1], size);
908 identity[size] = '\0';
915 * Create our Request-Response data structure with the eap packet
917 static EAP_DS *eap_buildds(eap_packet_t **eap_packet_p)
919 EAP_DS *eap_ds = NULL;
920 eap_packet_t *eap_packet = *eap_packet_p;
924 if ((eap_ds = eap_ds_alloc()) == NULL) {
928 eap_ds->response->packet = (unsigned char *)eap_packet;
929 eap_ds->response->code = eap_packet->code;
930 eap_ds->response->id = eap_packet->id;
931 eap_ds->response->type.type = eap_packet->data[0];
933 memcpy(&len, eap_packet->length, sizeof(uint16_t));
935 eap_ds->response->length = len;
938 * We've eaten the eap packet into the eap_ds.
940 *eap_packet_p = NULL;
943 * First 5 bytes in eap, are code + id + length(2) + type.
945 * The rest is type-specific data. We skip type while
946 * getting typedata from data.
948 typelen = len - 5/*code + id + length + type */;
951 * Since the packet contains the complete
952 * eap_packet, typedata will be a ptr in packet
955 eap_ds->response->type.data = eap_ds->response->packet + 5/*code+id+length+type*/;
956 eap_ds->response->type.length = typelen;
958 eap_ds->response->type.length = 0;
959 eap_ds->response->type.data = NULL;
967 * If identity response then create a fresh handler & fill the identity
968 * else handler MUST be in our list, get that.
969 * This handler creation cannot fail
971 * username contains REQUEST->username which might have been stripped.
972 * identity contains the one sent in EAP-Identity response
974 EAP_HANDLER *eap_handler(rlm_eap_t *inst, eap_packet_t **eap_packet_p,
977 EAP_HANDLER *handler = NULL;
978 eap_packet_t *eap_packet = *eap_packet_p;
982 * Ensure it's a valid EAP-Request, or EAP-Response.
984 if (eap_validation(eap_packet) == EAP_INVALID) {
986 *eap_packet_p = NULL;
991 * EAP_HANDLER MUST be found in the list if it is not
992 * EAP-Identity response
994 if (eap_packet->data[0] != PW_EAP_IDENTITY) {
995 handler = eaplist_find(inst, request, eap_packet);
996 if (handler == NULL) {
997 /* Either send EAP_Identity or EAP-Fail */
998 radlog(L_ERR, "rlm_eap: Either EAP-request timed out OR"
999 " EAP-response to an unknown EAP-request");
1000 free(*eap_packet_p);
1001 *eap_packet_p = NULL;
1006 * Even more paranoia. Without this, some weird
1007 * clients could do crazy things.
1009 * It's ok to send EAP sub-type NAK in response
1010 * to a request for a particular type, but it's NOT
1011 * OK to blindly return data for another type.
1013 if ((eap_packet->data[0] != PW_EAP_NAK) &&
1014 (eap_packet->data[0] != handler->eap_type)) {
1015 radlog(L_ERR, "rlm_eap: Response appears to match, but EAP type is wrong.");
1016 free(*eap_packet_p);
1017 *eap_packet_p = NULL;
1021 vp = pairfind(request->packet->vps, PW_USER_NAME);
1024 * NAS did not set the User-Name
1025 * attribute, so we set it here and
1026 * prepend it to the beginning of the
1027 * request vps so that autz's work
1030 radlog(L_INFO, "rlm_eap: Broken NAS did not set User-Name, setting from EAP Identity");
1031 vp = pairmake("User-Name", handler->identity, T_OP_EQ);
1033 radlog(L_ERR, "rlm_eap: out of memory");
1034 free(*eap_packet_p);
1035 *eap_packet_p = NULL;
1038 vp->next = request->packet->vps;
1039 request->packet->vps = vp;
1043 * A little more paranoia. If the NAS
1044 * *did* set the User-Name, and it doesn't
1045 * match the identity, (i.e. If they
1046 * change their User-Name part way through
1047 * the EAP transaction), then reject the
1048 * request as the NAS is doing something
1051 if (strncmp(handler->identity, vp->vp_strvalue,
1052 MAX_STRING_LEN) != 0) {
1053 radlog(L_ERR, "rlm_eap: Identity does not match User-Name. Authentication failed.");
1054 free(*eap_packet_p);
1055 *eap_packet_p = NULL;
1059 } else { /* packet was EAP identity */
1060 handler = eap_handler_alloc();
1061 if (handler == NULL) {
1062 radlog(L_ERR, "rlm_eap: out of memory");
1063 free(*eap_packet_p);
1064 *eap_packet_p = NULL;
1069 * All fields in the handler are set to zero.
1071 handler->identity = eap_identity(eap_packet);
1072 if (handler->identity == NULL) {
1073 radlog(L_ERR, "rlm_eap: Identity Unknown, authentication failed");
1074 free(*eap_packet_p);
1075 *eap_packet_p = NULL;
1076 eap_handler_free(handler);
1080 vp = pairfind(request->packet->vps, PW_USER_NAME);
1083 * NAS did not set the User-Name
1084 * attribute, so we set it here and
1085 * prepend it to the beginning of the
1086 * request vps so that autz's work
1089 radlog(L_INFO, "rlm_eap: WARNING NAS did not set User-Name. Setting it locally from EAP Identity");
1090 vp = pairmake("User-Name", handler->identity, T_OP_EQ);
1092 radlog(L_ERR, "rlm_eap: out of memory");
1093 free(*eap_packet_p);
1094 *eap_packet_p = NULL;
1097 vp->next = request->packet->vps;
1098 request->packet->vps = vp;
1101 * Paranoia. If the NAS *did* set the
1102 * User-Name, and it doesn't match the
1103 * identity, the NAS is doing something
1104 * funny, so reject the request.
1106 if (strncmp(handler->identity, vp->vp_strvalue,
1107 MAX_STRING_LEN) != 0) {
1108 radlog(L_ERR, "rlm_eap: Identity does not match User-Name, setting from EAP Identity.");
1109 free(*eap_packet_p);
1110 *eap_packet_p = NULL;
1111 eap_handler_free(handler);
1117 handler->eap_ds = eap_buildds(eap_packet_p);
1118 if (handler->eap_ds == NULL) {
1119 free(*eap_packet_p);
1120 *eap_packet_p = NULL;
1121 eap_handler_free(handler);
1125 handler->timestamp = request->timestamp;
1126 handler->request = request; /* LEAP needs this */