2 * rlm_eap_ttls.c contains the interfaces that are called from eap
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 2003 Alan DeKok <aland@freeradius.org>
21 * Copyright 2006 The FreeRADIUS server project
24 #include <freeradius-devel/ident.h>
28 #include "eap_chbind.h"
32 * 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
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * |V M r r r r r r| AVP Length |
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * Verify that the diameter packet is valid.
47 static int diameter_verify(REQUEST *request,
48 const uint8_t *data, unsigned int data_len)
53 unsigned int remaining = data_len;
55 while (remaining > 0) {
58 if (remaining < hdr_len) {
59 RDEBUG2(" Diameter attribute is too small (%u) to contain a Diameter header", remaining);
63 memcpy(&attr, data, sizeof(attr));
65 memcpy(&length, data + 4, sizeof(length));
66 length = ntohl(length);
68 if ((data[4] & 0x80) != 0) {
70 RDEBUG2(" Diameter attribute is too small to contain a Diameter header with Vendor-Id");
78 * Get the length. If it's too big, die.
83 * Too short or too long is bad.
85 if (length <= (hdr_len - 4)) {
86 RDEBUG2("Tunneled attribute %u is too short (%u < %u) to contain anything useful.", attr, length, hdr_len);
90 if (length > remaining) {
91 RDEBUG2("Tunneled attribute %u is longer than room remaining in the packet (%u > %u).", attr, length, remaining);
96 * Check for broken implementations, which don't
97 * pad the AVP to a 4-octet boundary.
99 if (remaining == length) break;
102 * The length does NOT include the padding, so
103 * we've got to account for it here by rounding up
104 * to the nearest 4-byte boundary.
110 * If the rest of the diameter packet is larger than
111 * this attribute, continue.
113 * Otherwise, if the attribute over-flows the end
114 * of the packet, die.
116 if (remaining < length) {
117 RDEBUG2E("Diameter attribute overflows packet!");
122 * remaining > length, continue.
129 * We got this far. It looks OK.
136 * Convert diameter attributes to our VALUE_PAIR's
138 static VALUE_PAIR *diameter2vp(REQUEST *request, SSL *ssl,
139 const uint8_t *data, size_t data_len)
146 size_t data_left = data_len;
147 VALUE_PAIR *first = NULL;
148 VALUE_PAIR **last = &first;
151 while (data_left > 0) {
152 rad_assert(data_left <= data_len);
153 memcpy(&attr, data, sizeof(attr));
158 memcpy(&length, data, sizeof(length));
160 length = ntohl(length);
163 * A "vendor" flag, with a vendor ID of zero,
164 * is equivalent to no vendor. This is stupid.
167 if ((length & (1 << 31)) != 0) {
168 memcpy(&vendor, data, sizeof(vendor));
169 vendor = ntohl(vendor);
171 data += 4; /* skip the vendor field, it's zero */
172 offset += 4; /* offset to value field */
174 if (attr > 65535) goto next_attr;
175 if (vendor > FR_MAX_VENDOR) goto next_attr;
179 * FIXME: Handle the M bit. For now, we assume that
180 * some other module takes care of any attribute
181 * with the M bit set.
187 length &= 0x00ffffff;
190 * Get the size of the value portion of the
193 size = length - offset;
196 * Vendor attributes can be larger than 255.
197 * Normal attributes cannot be.
199 if ((attr > 255) && (vendor == 0)) {
200 RDEBUG2W("Skipping Diameter attribute %u",
206 * EAP-Message AVPs can be larger than 253 octets.
208 if ((size > 253) && !((vendor == 0) && (attr == PW_EAP_MESSAGE))) {
209 RDEBUG2W("diameter2vp skipping long attribute %u", attr);
214 * RADIUS VSAs are handled as Diameter attributes
215 * with Vendor-Id == 0, and the VSA data packed
216 * into the "String" field as per normal.
218 * EXCEPT for the MS-CHAP attributes.
220 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
224 buffer[0] = PW_VENDOR_SPECIFIC;
225 buffer[1] = size + 2;
226 memcpy(buffer + 2, data, size);
229 decoded = rad_attr2vp(NULL, NULL, NULL,
230 buffer, size + 2, &vp);
232 RDEBUG2E("diameter2vp failed decoding attr: %s",
237 if ((size_t) decoded != size + 2) {
238 RDEBUG2E("diameter2vp failed to entirely decode VSA");
247 } while (vp != NULL);
253 * Create it. If this fails, it's because we're OOM.
256 vp = paircreate(attr, vendor);
258 RDEBUG2("Failure in creating VP");
262 if (vendor == VENDORPEC_UKERNA) {
263 RDEBUG("Received UKERNA attr %d!", attr);
267 * If it's a type from our dictionary, then
268 * we need to put the data in a relevant place.
270 switch (vp->da->type) {
271 case PW_TYPE_INTEGER:
273 if (size != vp->length) {
277 * Bad format. Create a "raw"
281 if (vp) pairfree(&vp);
282 da = dict_attrunknown(attr, vendor, TRUE);
283 if (!da) return NULL;
284 vp = pairalloc(NULL, da);
285 if (size >= 253) size = 253;
287 memcpy(vp->vp_octets, data, vp->length);
290 memcpy(&vp->vp_integer, data, vp->length);
293 * Stored in host byte order: change it.
295 vp->vp_integer = ntohl(vp->vp_integer);
298 case PW_TYPE_INTEGER64:
299 if (size != vp->length) goto raw;
300 memcpy(&vp->vp_integer64, data, vp->length);
303 * Stored in host byte order: change it.
305 vp->vp_integer64 = ntohll(vp->vp_integer64);
309 if (size != vp->length) {
310 RDEBUG2("Invalid length attribute %d",
316 memcpy(&vp->vp_ipaddr, data, vp->length);
319 * Stored in network byte order: don't change it.
324 if (size != vp->length) goto raw;
325 vp->vp_integer = data[0];
329 if (size != vp->length) goto raw;
330 vp->vp_integer = (data[0] * 256) + data[1];
334 if (size != vp->length) goto raw;
335 memcpy(&vp->vp_signed, data, vp->length);
336 vp->vp_signed = ntohl(vp->vp_signed);
339 case PW_TYPE_IPV6ADDR:
340 if (size != vp->length) goto raw;
341 memcpy(&vp->vp_ipv6addr, data, vp->length);
344 case PW_TYPE_IPV6PREFIX:
345 if (size != vp->length) goto raw;
346 memcpy(&vp->vp_ipv6prefix, data, vp->length);
350 * String, octet, etc. Copy the data from the
351 * value field over verbatim.
354 if (attr == PW_EAP_MESSAGE) {
355 const uint8_t *eap_message = data;
358 * vp exists the first time around.
362 if (vp->length > 253) vp->length = 253;
363 memcpy(vp->vp_octets, eap_message,
367 eap_message += vp->length;
372 if (size == 0) break;
374 vp = paircreate(attr, vendor);
376 RDEBUG2("Failure in creating VP");
383 } /* else it's another kind of attribute */
387 if (size >= 253) size = 253;
389 memcpy(vp->vp_strvalue, data, vp->length);
394 * User-Password is NUL padded to a multiple
395 * of 16 bytes. Let's chop it to something
398 * NOTE: This means that the User-Password
399 * attribute CANNOT EVER have embedded zeros in it!
401 if ((vp->da->vendor == 0) && (vp->da->attr == PW_USER_PASSWORD)) {
403 * If the password is exactly 16 octets,
404 * it won't be zero-terminated.
406 vp->vp_strvalue[vp->length] = '\0';
407 vp->length = strlen(vp->vp_strvalue);
411 * Ensure that the client is using the
412 * correct challenge. This weirdness is
413 * to protect against against replay
414 * attacks, where anyone observing the
415 * CHAP exchange could pose as that user,
416 * by simply choosing to use the same
419 * By using a challenge based on
420 * information from the current session,
421 * we can guarantee that the client is
422 * not *choosing* a challenge.
424 * We're a little forgiving in that we
425 * have loose checks on the length, and
426 * we do NOT check the Id (first octet of
427 * the response to the challenge)
429 * But if the client gets the challenge correct,
430 * we're not too worried about the Id.
432 if (((vp->da->vendor == 0) && (vp->da->attr == PW_CHAP_CHALLENGE)) ||
433 ((vp->da->vendor == VENDORPEC_MICROSOFT) && (vp->da->attr == PW_MSCHAP_CHALLENGE))) {
434 uint8_t challenge[16];
436 if ((vp->length < 8) ||
438 RDEBUG("Tunneled challenge has invalid length");
444 eapttls_gen_challenge(ssl, challenge,
447 if (memcmp(challenge, vp->vp_octets,
449 RDEBUG("Tunneled challenge is incorrect");
464 * Catch non-aligned attributes.
466 if (data_left == length) break;
469 * The length does NOT include the padding, so
470 * we've got to account for it here by rounding up
471 * to the nearest 4-byte boundary.
476 rad_assert(data_left >= length);
478 data += length - offset; /* already updated */
482 * We got this far. It looks OK.
488 * Convert VALUE_PAIR's to diameter attributes, and write them
491 * The ONLY VALUE_PAIR's which may be passed to this function
492 * are ones which can go inside of a RADIUS (i.e. diameter)
493 * packet. So no server-configuration attributes, or the like.
495 static int vp2diameter(REQUEST *request, tls_session_t *tls_session, VALUE_PAIR *first)
498 * RADIUS packets are no more than 4k in size, so if
499 * we've got more than 4k of data to write, it's very
502 uint8_t buffer[4096];
514 for (vp = first; vp != NULL; vp = vp->next) {
516 * Too much data: die.
518 if ((total + vp->length + 12) >= sizeof(buffer)) {
519 RDEBUG2("output buffer is full!");
524 * Hmm... we don't group multiple EAP-Messages
525 * together. Maybe we should...
529 * Length is no more than 253, due to RADIUS
533 vendor = vp->da->vendor;
535 attr = vp->da->attr & 0xffff;
542 * Hmm... set the M bit for all attributes?
548 memcpy(p, &attr, sizeof(attr));
552 length += 8; /* includes 8 bytes of attr & length */
555 length += 4; /* include 4 bytes of vendor */
557 length = ntohl(length);
558 memcpy(p, &length, sizeof(length));
562 vendor = ntohl(vendor);
563 memcpy(p, &vendor, sizeof(vendor));
567 length = ntohl(length);
568 memcpy(p, &length, sizeof(length));
573 switch (vp->da->type) {
574 case PW_TYPE_INTEGER:
576 attr = htonl(vp->vp_integer); /* stored in host order */
577 memcpy(p, &attr, sizeof(attr));
581 case PW_TYPE_INTEGER64:
582 attr64 = htonll(vp->vp_integer64); /* stored in host order */
583 memcpy(p, &attr64, sizeof(attr64));
588 memcpy(p, &vp->vp_ipaddr, 4); /* network order */
595 memcpy(p, vp->vp_strvalue, vp->length);
601 * Skip to the end of the data.
607 * Align the data to a multiple of 4 bytes.
609 if ((total & 0x03) != 0) {
612 length = 4 - (total & 0x03);
613 for (i = 0; i < length; i++) {
619 } /* loop over the VP's to write. */
622 * Write the data in the buffer to the SSL session.
628 if ((debug_flag > 2) && fr_log_fp) {
629 for (i = 0; i < total; i++) {
630 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " TTLS tunnel data out %04x: ", (int) i);
632 fprintf(fr_log_fp, "%02x ", buffer[i]);
634 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
636 if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
640 (tls_session->record_plus)(&tls_session->clean_in, buffer, total);
643 * FIXME: Check the return code.
645 tls_handshake_send(request, tls_session);
655 * Use a reply packet to determine what to do.
657 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
658 REQUEST *request, RADIUS_PACKET *reply)
660 int rcode = RLM_MODULE_REJECT;
662 ttls_tunnel_t *t = tls_session->opaque;
664 handler = handler; /* -Wunused */
665 rad_assert(request != NULL);
666 rad_assert(handler->request == request);
669 * If the response packet was Access-Accept, then
670 * we're OK. If not, die horribly.
672 * FIXME: Take MS-CHAP2-Success attribute, and
673 * tunnel it back to the client, to authenticate
674 * ourselves to the client.
676 * FIXME: If we have an Access-Challenge, then
677 * the Reply-Message is tunneled back to the client.
679 * FIXME: If we have an EAP-Message, then that message
680 * must be tunneled back to the client.
682 * FIXME: If we have an Access-Challenge with a State
683 * attribute, then do we tunnel that to the client, or
684 * keep track of it ourselves?
686 * FIXME: EAP-Messages can only start with 'identity',
687 * NOT 'eap start', so we should check for that....
689 switch (reply->code) {
690 case PW_AUTHENTICATION_ACK:
691 RDEBUG("Got tunneled Access-Accept");
693 rcode = RLM_MODULE_OK;
696 * MS-CHAP2-Success means that we do NOT return
697 * an Access-Accept, but instead tunnel that
698 * attribute to the client, and keep going with
699 * the TTLS session. Once the client accepts
700 * our identity, it will respond with an empty
701 * packet, and we will send EAP-Success.
704 pairmove2(&vp, &reply->vps, PW_MSCHAP2_SUCCESS, VENDORPEC_MICROSOFT, TAG_ANY);
706 RDEBUG("Got MS-CHAP2-Success, tunneling it to the client in a challenge.");
707 rcode = RLM_MODULE_HANDLED;
708 t->authenticated = TRUE;
711 * Delete MPPE keys & encryption policy. We don't
714 pairdelete(&reply->vps, 7, VENDORPEC_MICROSOFT, TAG_ANY);
715 pairdelete(&reply->vps, 8, VENDORPEC_MICROSOFT, TAG_ANY);
716 pairdelete(&reply->vps, 16, VENDORPEC_MICROSOFT, TAG_ANY);
717 pairdelete(&reply->vps, 17, VENDORPEC_MICROSOFT, TAG_ANY);
720 * Use the tunneled reply, but not now.
722 if (t->use_tunneled_reply) {
723 t->accept_vps = reply->vps;
727 } else { /* no MS-CHAP2-Success */
729 * Can only have EAP-Message if there's
730 * no MS-CHAP2-Success. (FIXME: EAP-MSCHAP?)
732 * We also do NOT tunnel the EAP-Success
733 * attribute back to the client, as the client
734 * can figure it out, from the non-tunneled
735 * EAP-Success packet.
737 pairmove2(&vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
742 * Handle the ACK, by tunneling any necessary reply
743 * VP's back to the client.
746 vp2diameter(request, tls_session, vp);
751 * If we've been told to use the attributes from
752 * the reply, then do so.
754 * WARNING: This may leak information about the
757 if (t->use_tunneled_reply) {
758 pairdelete(&reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
759 pairadd(&request->reply->vps, reply->vps);
765 case PW_AUTHENTICATION_REJECT:
766 RDEBUG("Got tunneled Access-Reject");
767 rcode = RLM_MODULE_REJECT;
771 * Handle Access-Challenge, but only if we
772 * send tunneled reply data. This is because
773 * an Access-Challenge means that we MUST tunnel
774 * a Reply-Message to the client.
776 case PW_ACCESS_CHALLENGE:
777 RDEBUG("Got tunneled Access-Challenge");
780 * Keep the State attribute, if necessary.
782 * Get rid of the old State, too.
785 pairmove2(&t->state, &reply->vps, PW_STATE, 0, TAG_ANY);
788 * We should really be a bit smarter about this,
789 * and move over only those attributes which
790 * are relevant to the authentication request,
791 * but that's a lot more work, and this "dumb"
792 * method works in 99.9% of the situations.
795 pairmove2(&vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
798 * There MUST be a Reply-Message in the challenge,
799 * which we tunnel back to the client.
801 * If there isn't one in the reply VP's, then
802 * we MUST create one, with an empty string as
805 pairmove2(&vp, &reply->vps, PW_REPLY_MESSAGE, 0, TAG_ANY);
808 * Handle the ACK, by tunneling any necessary reply
809 * VP's back to the client.
812 vp2diameter(request, tls_session, vp);
815 rcode = RLM_MODULE_HANDLED;
819 RDEBUG("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
820 rcode = RLM_MODULE_INVALID;
830 * Do post-proxy processing,
832 static int eapttls_postproxy(EAP_HANDLER *handler, void *data)
835 tls_session_t *tls_session = (tls_session_t *) data;
836 REQUEST *fake, *request = handler->request;
838 rad_assert(request != NULL);
839 RDEBUG("Passing reply from proxy back into the tunnel.");
842 * If there was a fake request associated with the proxied
843 * request, do more processing of it.
845 fake = (REQUEST *) request_data_get(handler->request,
846 handler->request->proxy,
847 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
850 * Do the callback, if it exists, and if it was a success.
853 handler->request->proxy_reply &&
854 (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
858 rad_assert(fake->packet == NULL);
859 fake->packet = request->proxy;
860 fake->packet->src_ipaddr = request->packet->src_ipaddr;
861 request->proxy = NULL;
863 rad_assert(fake->reply == NULL);
864 fake->reply = request->proxy_reply;
865 request->proxy_reply = NULL;
867 if ((debug_flag > 0) && fr_log_fp) {
868 fprintf(fr_log_fp, "server %s {\n",
869 (fake->server == NULL) ? "" : fake->server);
873 * Perform a post-auth stage for the tunneled
876 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
877 rcode = rad_postauth(fake);
878 RDEBUG2("post-auth returns %d", rcode);
880 if ((debug_flag > 0) && fr_log_fp) {
881 fprintf(fr_log_fp, "} # server %s\n",
882 (fake->server == NULL) ? "" : fake->server);
884 RDEBUG("Final reply from tunneled session code %d",
886 debug_pair_list(fake->reply->vps);
892 request->proxy = fake->packet;
894 request->proxy_reply = fake->reply;
898 * And we're done with this request.
902 case RLM_MODULE_FAIL:
904 eaptls_fail(handler, 0);
908 default: /* Don't Do Anything */
909 RDEBUG2("Got reply %d",
910 request->proxy_reply->code);
914 request_free(&fake); /* robust if fake == NULL */
917 * Process the reply from the home server.
919 rcode = process_reply(handler, tls_session, handler->request,
920 handler->request->proxy_reply);
923 * The proxy code uses the reply from the home server as
924 * the basis for the reply to the NAS. We don't want that,
925 * so we toss it, after we've had our way with it.
927 pairfree(&handler->request->proxy_reply->vps);
930 case RLM_MODULE_REJECT:
931 RDEBUG("Reply was rejected");
934 case RLM_MODULE_HANDLED:
935 RDEBUG("Reply was handled");
936 eaptls_request(handler->eap_ds, tls_session);
940 RDEBUG("Reply was OK");
943 * Success: Automatically return MPPE keys.
945 return eaptls_success(handler, 0);
948 RDEBUG("Reply was unknown.");
952 eaptls_fail(handler, 0);
960 static void my_request_free(void *data)
962 REQUEST *request = (REQUEST *)data;
964 request_free(&request);
966 #endif /* WITH_PROXY */
969 * Process the "diameter" contents of the tunneled data.
971 int eapttls_process(EAP_HANDLER *handler, tls_session_t *tls_session)
973 int rcode = PW_AUTHENTICATION_REJECT;
979 REQUEST *request = handler->request;
980 eap_chbind_packet_t *chbind_packet;
983 rad_assert(request != NULL);
986 * Just look at the buffer directly, without doing
989 data_len = tls_session->clean_out.used;
990 tls_session->clean_out.used = 0;
991 data = tls_session->clean_out.data;
993 t = (ttls_tunnel_t *) tls_session->opaque;
996 * If there's no data, maybe this is an ACK to an
1000 if (t->authenticated) {
1001 RDEBUG("Got ACK, and the user was already authenticated.");
1002 return PW_AUTHENTICATION_ACK;
1003 } /* else no session, no data, die. */
1006 * FIXME: Call SSL_get_error() to see what went
1009 RDEBUG2("SSL_read Error");
1010 return PW_AUTHENTICATION_REJECT;
1014 if ((debug_flag > 2) && fr_log_fp) {
1017 for (i = 0; i < data_len; i++) {
1018 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " TTLS tunnel data in %04x: ", (int) i);
1020 fprintf(fr_log_fp, "%02x ", data[i]);
1022 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1024 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
1028 if (!diameter_verify(request, data, data_len)) {
1029 return PW_AUTHENTICATION_REJECT;
1033 * Allocate a fake REQUEST structe.
1035 fake = request_alloc_fake(request);
1037 rad_assert(fake->packet->vps == NULL);
1040 * Add the tunneled attributes to the fake request.
1042 fake->packet->vps = diameter2vp(request, tls_session->ssl, data, data_len);
1043 if (!fake->packet->vps) {
1044 request_free(&fake);
1045 return PW_AUTHENTICATION_REJECT;
1049 * Tell the request that it's a fake one.
1051 vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
1053 pairadd(&fake->packet->vps, vp);
1056 if ((debug_flag > 0) && fr_log_fp) {
1057 RDEBUG("Got tunneled request");
1059 debug_pair_list(fake->packet->vps);
1063 * Update other items in the REQUEST data structure.
1065 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1066 fake->password = pairfind(fake->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1069 * No User-Name, try to create one from stored data.
1071 if (!fake->username) {
1073 * No User-Name in the stored data, look for
1074 * an EAP-Identity, and pull it out of there.
1077 vp = pairfind(fake->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1079 (vp->length >= EAP_HEADER_LEN + 2) &&
1080 (vp->vp_strvalue[0] == PW_EAP_RESPONSE) &&
1081 (vp->vp_strvalue[EAP_HEADER_LEN] == PW_EAP_IDENTITY) &&
1082 (vp->vp_strvalue[EAP_HEADER_LEN + 1] != 0)) {
1084 * Create & remember a User-Name
1086 t->username = pairmake("User-Name", "", T_OP_EQ);
1087 rad_assert(t->username != NULL);
1089 memcpy(t->username->vp_strvalue, vp->vp_strvalue + 5,
1091 t->username->length = vp->length - 5;
1092 t->username->vp_strvalue[t->username->length] = 0;
1094 RDEBUG("Got tunneled identity of %s",
1095 t->username->vp_strvalue);
1098 * If there's a default EAP type,
1101 if (t->default_eap_type != 0) {
1102 RDEBUG("Setting default EAP type for tunneled EAP session.");
1103 vp = paircreate(PW_EAP_TYPE, 0);
1104 rad_assert(vp != NULL);
1105 vp->vp_integer = t->default_eap_type;
1106 pairadd(&fake->config_items, vp);
1111 * Don't reject the request outright,
1112 * as it's permitted to do EAP without
1115 RDEBUG2W("No EAP-Identity found to start EAP conversation.");
1117 } /* else there WAS a t->username */
1120 vp = paircopy(t->username);
1121 pairadd(&fake->packet->vps, vp);
1122 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1124 } /* else the request ALREADY had a User-Name */
1127 * Add the State attribute, too, if it exists.
1130 vp = paircopy(t->state);
1131 if (vp) pairadd(&fake->packet->vps, vp);
1135 * If this is set, we copy SOME of the request attributes
1136 * from outside of the tunnel to inside of the tunnel.
1138 * We copy ONLY those attributes which do NOT already
1139 * exist in the tunneled request.
1141 if (t->copy_request_to_tunnel) {
1144 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
1146 * The attribute is a server-side thingy,
1149 if ((vp->da->attr > 255) &&
1150 (vp->da->vendor == 0)) {
1155 * The outside attribute is already in the
1156 * tunnel, don't copy it.
1158 * This works for BOTH attributes which
1159 * are originally in the tunneled request,
1160 * AND attributes which are copied there
1163 if (pairfind(fake->packet->vps, vp->da->attr, vp->da->vendor, TAG_ANY)) {
1168 * Some attributes are handled specially.
1170 switch (vp->da->attr) {
1172 * NEVER copy Message-Authenticator,
1173 * EAP-Message, or State. They're
1174 * only for outside of the tunnel.
1177 case PW_USER_PASSWORD:
1178 case PW_CHAP_PASSWORD:
1179 case PW_CHAP_CHALLENGE:
1180 case PW_PROXY_STATE:
1181 case PW_MESSAGE_AUTHENTICATOR:
1182 case PW_EAP_MESSAGE:
1188 * By default, copy it over.
1195 * Don't copy from the head, we've already
1198 copy = paircopy2(vp, vp->da->attr, vp->da->vendor, TAG_ANY);
1199 pairadd(&fake->packet->vps, copy);
1203 if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
1204 fake->server = vp->vp_strvalue;
1206 } else if (t->virtual_server) {
1207 fake->server = t->virtual_server;
1209 } /* else fake->server == request->server */
1212 if ((debug_flag > 0) && fr_log_fp) {
1213 RDEBUG("Sending tunneled request");
1215 debug_pair_list(fake->packet->vps);
1217 fprintf(fr_log_fp, "server %s {\n",
1218 (fake->server == NULL) ? "" : fake->server);
1222 * Process channel binding here.
1224 chbind_len = eap_chbind_vp2packet(fake->packet->vps, &chbind_packet);
1225 if (chbind_len > 0) {
1226 CHBIND_REQ *req = chbind_allocate();
1227 RDEBUG("received chbind request");
1228 req->chbind_req_pkt = (uint8_t *)chbind_packet;
1229 req->chbind_req_len = chbind_len;
1230 if (fake->username) {
1231 req->username = fake->username->vp_octets;
1232 req->username_len = fake->username->length;
1234 req->username = NULL;
1235 req->username_len = 0;
1237 chbind_process(request, req);
1239 /* free the chbind packet; we're done with it */
1240 free(chbind_packet);
1242 /* encapsulate response here */
1243 if (req->chbind_resp_len > 0) {
1244 RDEBUG("sending chbind response");
1245 pairadd(&fake->reply->vps,
1246 eap_chbind_packet2vp((eap_chbind_packet_t *)req->chbind_resp,
1247 req->chbind_resp_len));
1249 RDEBUG("no chbind response");
1252 /* clean up chbind req */
1257 * Call authentication recursively, which will
1258 * do PAP, CHAP, MS-CHAP, etc.
1260 rad_virtual_server(fake);
1263 * Note that we don't do *anything* with the reply
1266 if ((debug_flag > 0) && fr_log_fp) {
1267 fprintf(fr_log_fp, "} # server %s\n",
1268 (fake->server == NULL) ? "" : fake->server);
1270 RDEBUG("Got tunneled reply code %d", fake->reply->code);
1272 debug_pair_list(fake->reply->vps);
1276 * Decide what to do with the reply.
1278 switch (fake->reply->code) {
1279 case 0: /* No reply code, must be proxied... */
1281 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY);
1283 eap_tunnel_data_t *tunnel;
1284 RDEBUG("Tunneled authentication will be proxied to %s", vp->vp_strvalue);
1287 * Tell the original request that it's going
1290 pairmove2(&(request->config_items),
1291 &(fake->config_items),
1292 PW_PROXY_TO_REALM, 0, TAG_ANY);
1295 * Seed the proxy packet with the
1298 rad_assert(request->proxy == NULL);
1299 request->proxy = fake->packet;
1300 memset(&request->proxy->src_ipaddr, 0,
1301 sizeof(request->proxy->src_ipaddr));
1302 memset(&request->proxy->src_ipaddr, 0,
1303 sizeof(request->proxy->src_ipaddr));
1304 request->proxy->src_port = 0;
1305 request->proxy->dst_port = 0;
1306 fake->packet = NULL;
1307 rad_free(&fake->reply);
1311 * Set up the callbacks for the tunnel
1313 tunnel = rad_malloc(sizeof(*tunnel));
1314 memset(tunnel, 0, sizeof(*tunnel));
1316 tunnel->tls_session = tls_session;
1317 tunnel->callback = eapttls_postproxy;
1320 * Associate the callback with the request.
1322 rcode = request_data_add(request,
1324 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1326 rad_assert(rcode == 0);
1329 * rlm_eap.c has taken care of associating
1330 * the handler with the fake request.
1332 * So we associate the fake request with
1335 rcode = request_data_add(request,
1337 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1338 fake, my_request_free);
1339 rad_assert(rcode == 0);
1343 * Didn't authenticate the packet, but
1344 * we're proxying it.
1346 rcode = PW_STATUS_CLIENT;
1349 #endif /* WITH_PROXY */
1351 RDEBUG("No tunneled reply was found for request %d , and the request was not proxied: rejecting the user.",
1353 rcode = PW_AUTHENTICATION_REJECT;
1359 * Returns RLM_MODULE_FOO, and we want to return
1362 rcode = process_reply(handler, tls_session, request,
1365 case RLM_MODULE_REJECT:
1366 rcode = PW_AUTHENTICATION_REJECT;
1369 case RLM_MODULE_HANDLED:
1370 rcode = PW_ACCESS_CHALLENGE;
1374 rcode = PW_AUTHENTICATION_ACK;
1378 rcode = PW_AUTHENTICATION_REJECT;
1384 request_free(&fake);