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 data_left = data_len;
55 while (data_left > 0) {
57 RDEBUG2(" Diameter attribute is too small to contain a Diameter header");
61 rad_assert(data_left <= data_len);
62 memcpy(&attr, data, sizeof(attr));
66 RDEBUG2(" Non-RADIUS attribute in tunneled authentication is not supported");
70 memcpy(&length, data , sizeof(length));
72 length = ntohl(length);
75 * A "vendor" flag, with a vendor ID of zero,
76 * is equivalent to no vendor. This is stupid.
79 if ((length & (1 << 31)) != 0) {
83 memcpy(&vendor, data, sizeof(vendor));
84 vendor = ntohl(vendor);
86 if (vendor > FR_MAX_VENDOR) {
87 RDEBUG2("Vendor codes larger than 2^24 are not supported");
91 da = dict_attrbyvalue(attr, vendor);
94 * SHOULD check ((length & (1 << 30)) != 0)
95 * for the mandatory bit.
98 RDEBUG2("Fatal! Vendor %u, Attribute %u was not found in our dictionary. ",
103 data += 4; /* skip the vendor field */
104 offset += 4; /* offset to value field */
108 * Ignore the M bit. We support all RADIUS attributes...
112 * Get the length. If it's too big, die.
114 length &= 0x00ffffff;
117 * Too short or too long is bad.
119 if (length < offset) {
120 RDEBUG2("Tunneled attribute %d is too short (%d)to contain anything useful.", attr, length);
125 * EAP Messages cane be longer than MAX_STRING_LEN.
126 * Other attributes cannot be.
128 if ((attr != PW_EAP_MESSAGE) &&
129 (length > (MAX_STRING_LEN + 8))) {
130 RDEBUG2("Tunneled attribute %d is too long (%d) to pack into a RADIUS attribute.", attr, length);
134 if (length > data_left) {
135 RDEBUG2("Tunneled attribute %d is longer than room left in the packet (%d > %d).", attr, length, data_left);
140 * Check for broken implementations, which don't
141 * pad the AVP to a 4-octet boundary.
143 if (data_left == length) break;
146 * The length does NOT include the padding, so
147 * we've got to account for it here by rounding up
148 * to the nearest 4-byte boundary.
154 * If the rest of the diameter packet is larger than
155 * this attribute, continue.
157 * Otherwise, if the attribute over-flows the end
158 * of the packet, die.
160 if (data_left < length) {
161 RDEBUG2("ERROR! Diameter attribute overflows packet!");
166 * Check again for equality, now that we're padded
167 * length to a multiple of 4 octets.
169 if (data_left == length) break;
172 * data_left > length, continue.
175 data += length - offset;
179 * We got this far. It looks OK.
186 * Convert diameter attributes to our VALUE_PAIR's
188 static VALUE_PAIR *diameter2vp(REQUEST *request, SSL *ssl,
189 const uint8_t *data, size_t data_len)
196 size_t data_left = data_len;
197 VALUE_PAIR *first = NULL;
198 VALUE_PAIR **last = &first;
201 while (data_left > 0) {
202 rad_assert(data_left <= data_len);
203 memcpy(&attr, data, sizeof(attr));
208 memcpy(&length, data, sizeof(length));
210 length = ntohl(length);
213 * A "vendor" flag, with a vendor ID of zero,
214 * is equivalent to no vendor. This is stupid.
217 if ((length & (1 << 31)) != 0) {
218 memcpy(&vendor, data, sizeof(vendor));
219 vendor = ntohl(vendor);
221 if (vendor > FR_MAX_VENDOR) {
222 RDEBUG2("Cannot handle vendor Id greater than 2^&24");
227 data += 4; /* skip the vendor field, it's zero */
228 offset += 4; /* offset to value field */
232 * Vendor attributes can be larger than 255.
233 * Normal attributes cannot be.
235 if ((attr > 255) && (vendor == 0)) {
236 RDEBUG2("Cannot handle Diameter attributes");
242 * FIXME: Handle the M bit. For now, we assume that
243 * some other module takes care of any attribute
244 * with the M bit set.
250 length &= 0x00ffffff;
253 * Get the size of the value portion of the
256 size = length - offset;
261 vp = paircreate(attr, vendor, PW_TYPE_OCTETS);
263 RDEBUG2("Failure in creating VP");
267 if (vendor == VENDORPEC_UKERNA) {
268 RDEBUG("Received UKERNA attr %d!", attr);
272 * If it's a type from our dictionary, then
273 * we need to put the data in a relevant place.
276 case PW_TYPE_INTEGER:
278 if (size != vp->length) {
279 RDEBUG2("Invalid length attribute %d",
285 memcpy(&vp->vp_integer, data, vp->length);
288 * Stored in host byte order: change it.
290 vp->vp_integer = ntohl(vp->vp_integer);
293 case PW_TYPE_INTEGER64:
294 if (size != vp->length) {
295 RDEBUG2("Invalid length attribute %d",
301 memcpy(&vp->vp_integer64, data, vp->length);
304 * Stored in host byte order: change it.
306 vp->vp_integer64 = ntohll(vp->vp_integer64);
310 if (size != vp->length) {
311 RDEBUG2("Invalid length attribute %d",
317 memcpy(&vp->vp_ipaddr, data, vp->length);
320 * Stored in network byte order: don't change it.
325 * String, octet, etc. Copy the data from the
326 * value field over verbatim.
328 * FIXME: Ipv6 attributes ?
332 if (attr == PW_EAP_MESSAGE) {
333 const uint8_t *eap_message = data;
336 * vp exists the first time around.
340 if (vp->length > 253) vp->length = 253;
341 memcpy(vp->vp_octets, eap_message,
345 eap_message += vp->length;
350 if (size == 0) break;
352 vp = paircreate(attr, vendor, PW_TYPE_OCTETS);
354 RDEBUG2("Failure in creating VP");
361 } /* else it's another kind of attribute */
366 memcpy(vp->vp_strvalue, data, vp->length);
371 * User-Password is NUL padded to a multiple
372 * of 16 bytes. Let's chop it to something
375 * NOTE: This means that the User-Password
376 * attribute CANNOT EVER have embedded zeros in it!
378 switch (vp->attribute) {
379 case PW_USER_PASSWORD:
381 * If the password is exactly 16 octets,
382 * it won't be zero-terminated.
384 vp->vp_strvalue[vp->length] = '\0';
385 vp->length = strlen(vp->vp_strvalue);
389 * Ensure that the client is using the
390 * correct challenge. This weirdness is
391 * to protect against against replay
392 * attacks, where anyone observing the
393 * CHAP exchange could pose as that user,
394 * by simply choosing to use the same
397 * By using a challenge based on
398 * information from the current session,
399 * we can guarantee that the client is
400 * not *choosing* a challenge.
402 * We're a little forgiving in that we
403 * have loose checks on the length, and
404 * we do NOT check the Id (first octet of
405 * the response to the challenge)
407 * But if the client gets the challenge correct,
408 * we're not too worried about the Id.
410 case PW_CHAP_CHALLENGE:
411 case PW_MSCHAP_CHALLENGE:
412 if ((vp->length < 8) ||
414 RDEBUG("Tunneled challenge has invalid length");
420 uint8_t challenge[16];
422 eapttls_gen_challenge(ssl, challenge,
425 if (memcmp(challenge, vp->vp_octets,
427 RDEBUG("Tunneled challenge is incorrect");
437 } /* switch over checking/re-writing of attributes. */
447 * Catch non-aligned attributes.
449 if (data_left == length) break;
452 * The length does NOT include the padding, so
453 * we've got to account for it here by rounding up
454 * to the nearest 4-byte boundary.
459 rad_assert(data_left >= length);
461 data += length - offset; /* already updated */
465 * We got this far. It looks OK.
471 * Convert VALUE_PAIR's to diameter attributes, and write them
474 * The ONLY VALUE_PAIR's which may be passed to this function
475 * are ones which can go inside of a RADIUS (i.e. diameter)
476 * packet. So no server-configuration attributes, or the like.
478 static int vp2diameter(REQUEST *request, tls_session_t *tls_session, VALUE_PAIR *first)
481 * RADIUS packets are no more than 4k in size, so if
482 * we've got more than 4k of data to write, it's very
485 uint8_t buffer[4096];
497 for (vp = first; vp != NULL; vp = vp->next) {
499 * Too much data: die.
501 if ((total + vp->length + 12) >= sizeof(buffer)) {
502 RDEBUG2("output buffer is full!");
507 * Hmm... we don't group multiple EAP-Messages
508 * together. Maybe we should...
512 * Length is no more than 253, due to RADIUS
518 attr = vp->attribute & 0xffff;
521 attr = vp->attribute;
525 * Hmm... set the M bit for all attributes?
531 memcpy(p, &attr, sizeof(attr));
535 length += 8; /* includes 8 bytes of attr & length */
538 length += 4; /* include 4 bytes of vendor */
540 length = ntohl(length);
541 memcpy(p, &length, sizeof(length));
545 vendor = ntohl(vendor);
546 memcpy(p, &vendor, sizeof(vendor));
550 length = ntohl(length);
551 memcpy(p, &length, sizeof(length));
557 case PW_TYPE_INTEGER:
559 attr = htonl(vp->vp_integer); /* stored in host order */
560 memcpy(p, &attr, sizeof(attr));
564 case PW_TYPE_INTEGER64:
565 attr64 = htonll(vp->vp_integer64); /* stored in host order */
566 memcpy(p, &attr64, sizeof(attr64));
571 memcpy(p, &vp->vp_ipaddr, 4); /* network order */
578 memcpy(p, vp->vp_strvalue, vp->length);
584 * Skip to the end of the data.
590 * Align the data to a multiple of 4 bytes.
592 if ((total & 0x03) != 0) {
595 length = 4 - (total & 0x03);
596 for (i = 0; i < length; i++) {
602 } /* loop over the VP's to write. */
605 * Write the data in the buffer to the SSL session.
611 if ((debug_flag > 2) && fr_log_fp) {
612 for (i = 0; i < total; i++) {
613 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " TTLS tunnel data out %04x: ", (int) i);
615 fprintf(fr_log_fp, "%02x ", buffer[i]);
617 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
619 if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
623 (tls_session->record_plus)(&tls_session->clean_in, buffer, total);
626 * FIXME: Check the return code.
628 tls_handshake_send(request, tls_session);
638 * Use a reply packet to determine what to do.
640 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
641 REQUEST *request, RADIUS_PACKET *reply)
643 int rcode = RLM_MODULE_REJECT;
645 ttls_tunnel_t *t = tls_session->opaque;
647 handler = handler; /* -Wunused */
648 rad_assert(request != NULL);
649 rad_assert(handler->request == request);
652 * If the response packet was Access-Accept, then
653 * we're OK. If not, die horribly.
655 * FIXME: Take MS-CHAP2-Success attribute, and
656 * tunnel it back to the client, to authenticate
657 * ourselves to the client.
659 * FIXME: If we have an Access-Challenge, then
660 * the Reply-Message is tunneled back to the client.
662 * FIXME: If we have an EAP-Message, then that message
663 * must be tunneled back to the client.
665 * FIXME: If we have an Access-Challenge with a State
666 * attribute, then do we tunnel that to the client, or
667 * keep track of it ourselves?
669 * FIXME: EAP-Messages can only start with 'identity',
670 * NOT 'eap start', so we should check for that....
672 switch (reply->code) {
673 case PW_AUTHENTICATION_ACK:
674 RDEBUG("Got tunneled Access-Accept");
676 rcode = RLM_MODULE_OK;
679 * MS-CHAP2-Success means that we do NOT return
680 * an Access-Accept, but instead tunnel that
681 * attribute to the client, and keep going with
682 * the TTLS session. Once the client accepts
683 * our identity, it will respond with an empty
684 * packet, and we will send EAP-Success.
687 pairmove2(&vp, &reply->vps, PW_MSCHAP2_SUCCESS, VENDORPEC_MICROSOFT);
689 RDEBUG("Got MS-CHAP2-Success, tunneling it to the client in a challenge.");
690 rcode = RLM_MODULE_HANDLED;
691 t->authenticated = TRUE;
694 * Delete MPPE keys & encryption policy. We don't
697 pairdelete(&reply->vps, 7, VENDORPEC_MICROSOFT);
698 pairdelete(&reply->vps, 8, VENDORPEC_MICROSOFT);
699 pairdelete(&reply->vps, 16, VENDORPEC_MICROSOFT);
700 pairdelete(&reply->vps, 17, VENDORPEC_MICROSOFT);
703 * Use the tunneled reply, but not now.
705 if (t->use_tunneled_reply) {
706 t->accept_vps = reply->vps;
710 } else { /* no MS-CHAP2-Success */
712 * Can only have EAP-Message if there's
713 * no MS-CHAP2-Success. (FIXME: EAP-MSCHAP?)
715 * We also do NOT tunnel the EAP-Success
716 * attribute back to the client, as the client
717 * can figure it out, from the non-tunneled
718 * EAP-Success packet.
720 pairmove2(&vp, &reply->vps, PW_EAP_MESSAGE, 0);
724 /* move channel binding responses; we need to send them */
725 pairmove2(&vp, &reply->vps, PW_UKERNA_CHBIND, VENDORPEC_UKERNA);
728 * Handle the ACK, by tunneling any necessary reply
729 * VP's back to the client.
732 RDEBUG("sending tunneled reply attributes");
734 RDEBUG("end tunneled reply attributes");
735 vp2diameter(request, tls_session, vp);
740 * If we've been told to use the attributes from
741 * the reply, then do so.
743 * WARNING: This may leak information about the
746 if (t->use_tunneled_reply) {
747 pairdelete(&reply->vps, PW_PROXY_STATE, 0);
748 pairadd(&request->reply->vps, reply->vps);
754 case PW_AUTHENTICATION_REJECT:
755 RDEBUG("Got tunneled Access-Reject");
756 rcode = RLM_MODULE_REJECT;
760 * Handle Access-Challenge, but only if we
761 * send tunneled reply data. This is because
762 * an Access-Challenge means that we MUST tunnel
763 * a Reply-Message to the client.
765 case PW_ACCESS_CHALLENGE:
766 RDEBUG("Got tunneled Access-Challenge");
769 * Keep the State attribute, if necessary.
771 * Get rid of the old State, too.
774 pairmove2(&t->state, &reply->vps, PW_STATE, 0);
777 * We should really be a bit smarter about this,
778 * and move over only those attributes which
779 * are relevant to the authentication request,
780 * but that's a lot more work, and this "dumb"
781 * method works in 99.9% of the situations.
784 pairmove2(&vp, &reply->vps, PW_EAP_MESSAGE, 0);
787 * There MUST be a Reply-Message in the challenge,
788 * which we tunnel back to the client.
790 * If there isn't one in the reply VP's, then
791 * we MUST create one, with an empty string as
794 pairmove2(&vp, &reply->vps, PW_REPLY_MESSAGE, 0);
796 /* also move chbind messages, if any */
797 pairmove2(&vp, &reply->vps, PW_UKERNA_CHBIND, VENDORPEC_UKERNA);
800 * Handle the ACK, by tunneling any necessary reply
801 * VP's back to the client.
804 vp2diameter(request, tls_session, vp);
807 rcode = RLM_MODULE_HANDLED;
811 RDEBUG("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
812 rcode = RLM_MODULE_INVALID;
822 * Do post-proxy processing,
824 static int eapttls_postproxy(EAP_HANDLER *handler, void *data)
827 tls_session_t *tls_session = (tls_session_t *) data;
828 REQUEST *fake, *request = handler->request;
830 rad_assert(request != NULL);
831 RDEBUG("Passing reply from proxy back into the tunnel.");
834 * If there was a fake request associated with the proxied
835 * request, do more processing of it.
837 fake = (REQUEST *) request_data_get(handler->request,
838 handler->request->proxy,
839 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
842 * Do the callback, if it exists, and if it was a success.
844 if (fake && (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
848 rad_assert(fake->packet == NULL);
849 fake->packet = request->proxy;
850 fake->packet->src_ipaddr = request->packet->src_ipaddr;
851 request->proxy = NULL;
853 rad_assert(fake->reply == NULL);
854 fake->reply = request->proxy_reply;
855 request->proxy_reply = NULL;
857 if ((debug_flag > 0) && fr_log_fp) {
858 fprintf(fr_log_fp, "server %s {\n",
859 (fake->server == NULL) ? "" : fake->server);
863 * Perform a post-auth stage for the tunneled
866 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
867 rcode = rad_postauth(fake);
868 RDEBUG2("post-auth returns %d", rcode);
870 if ((debug_flag > 0) && fr_log_fp) {
871 fprintf(fr_log_fp, "} # server %s\n",
872 (fake->server == NULL) ? "" : fake->server);
874 RDEBUG("Final reply from tunneled session code %d",
876 debug_pair_list(fake->reply->vps);
882 request->proxy = fake->packet;
884 request->proxy_reply = fake->reply;
888 * And we're done with this request.
892 case RLM_MODULE_FAIL:
894 eaptls_fail(handler, 0);
898 default: /* Don't Do Anything */
899 RDEBUG2("Got reply %d",
900 request->proxy_reply->code);
904 request_free(&fake); /* robust if fake == NULL */
907 * Process the reply from the home server.
909 rcode = process_reply(handler, tls_session, handler->request,
910 handler->request->proxy_reply);
913 * The proxy code uses the reply from the home server as
914 * the basis for the reply to the NAS. We don't want that,
915 * so we toss it, after we've had our way with it.
917 pairfree(&handler->request->proxy_reply->vps);
920 case RLM_MODULE_REJECT:
921 RDEBUG("Reply was rejected");
924 case RLM_MODULE_HANDLED:
925 RDEBUG("Reply was handled");
926 eaptls_request(handler->eap_ds, tls_session);
930 RDEBUG("Reply was OK");
933 * Success: Automatically return MPPE keys.
935 return eaptls_success(handler, 0);
938 RDEBUG("Reply was unknown.");
942 eaptls_fail(handler, 0);
950 static void my_request_free(void *data)
952 REQUEST *request = (REQUEST *)data;
954 request_free(&request);
956 #endif /* WITH_PROXY */
959 * Process the "diameter" contents of the tunneled data.
961 int eapttls_process(EAP_HANDLER *handler, tls_session_t *tls_session)
963 int rcode = PW_AUTHENTICATION_REJECT;
969 REQUEST *request = handler->request;
970 eap_chbind_packet_t *chbind_packet;
973 rad_assert(request != NULL);
976 * Just look at the buffer directly, without doing
979 data_len = tls_session->clean_out.used;
980 tls_session->clean_out.used = 0;
981 data = tls_session->clean_out.data;
983 t = (ttls_tunnel_t *) tls_session->opaque;
986 * If there's no data, maybe this is an ACK to an
990 if (t->authenticated) {
991 RDEBUG("Got ACK, and the user was already authenticated.");
992 return PW_AUTHENTICATION_ACK;
993 } /* else no session, no data, die. */
996 * FIXME: Call SSL_get_error() to see what went
999 RDEBUG2("SSL_read Error");
1000 return PW_AUTHENTICATION_REJECT;
1004 if ((debug_flag > 2) && fr_log_fp) {
1007 for (i = 0; i < data_len; i++) {
1008 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " TTLS tunnel data in %04x: ", (int) i);
1010 fprintf(fr_log_fp, "%02x ", data[i]);
1012 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1014 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
1018 if (!diameter_verify(request, data, data_len)) {
1019 return PW_AUTHENTICATION_REJECT;
1023 * Allocate a fake REQUEST structe.
1025 fake = request_alloc_fake(request);
1027 rad_assert(fake->packet->vps == NULL);
1030 * Add the tunneled attributes to the fake request.
1032 fake->packet->vps = diameter2vp(request, tls_session->ssl, data, data_len);
1033 if (!fake->packet->vps) {
1034 request_free(&fake);
1035 return PW_AUTHENTICATION_REJECT;
1039 * Tell the request that it's a fake one.
1041 vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
1043 pairadd(&fake->packet->vps, vp);
1046 if ((debug_flag > 0) && fr_log_fp) {
1047 RDEBUG("Got tunneled request");
1049 debug_pair_list(fake->packet->vps);
1053 * Update other items in the REQUEST data structure.
1055 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0);
1056 fake->password = pairfind(fake->packet->vps, PW_USER_PASSWORD, 0);
1059 * No User-Name, try to create one from stored data.
1061 if (!fake->username) {
1063 * No User-Name in the stored data, look for
1064 * an EAP-Identity, and pull it out of there.
1067 vp = pairfind(fake->packet->vps, PW_EAP_MESSAGE, 0);
1069 (vp->length >= EAP_HEADER_LEN + 2) &&
1070 (vp->vp_strvalue[0] == PW_EAP_RESPONSE) &&
1071 (vp->vp_strvalue[EAP_HEADER_LEN] == PW_EAP_IDENTITY) &&
1072 (vp->vp_strvalue[EAP_HEADER_LEN + 1] != 0)) {
1074 * Create & remember a User-Name
1076 t->username = pairmake("User-Name", "", T_OP_EQ);
1077 rad_assert(t->username != NULL);
1079 memcpy(t->username->vp_strvalue, vp->vp_strvalue + 5,
1081 t->username->length = vp->length - 5;
1082 t->username->vp_strvalue[t->username->length] = 0;
1084 RDEBUG("Got tunneled identity of %s",
1085 t->username->vp_strvalue);
1088 * If there's a default EAP type,
1091 if (t->default_eap_type != 0) {
1092 RDEBUG("Setting default EAP type for tunneled EAP session.");
1093 vp = paircreate(PW_EAP_TYPE, 0,
1095 rad_assert(vp != NULL);
1096 vp->vp_integer = t->default_eap_type;
1097 pairadd(&fake->config_items, vp);
1102 * Don't reject the request outright,
1103 * as it's permitted to do EAP without
1106 RDEBUG2("WARNING! No EAP-Identity found to start EAP conversation.");
1108 } /* else there WAS a t->username */
1111 vp = paircopy(t->username);
1112 pairadd(&fake->packet->vps, vp);
1113 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0);
1115 } /* else the request ALREADY had a User-Name */
1118 * Add the State attribute, too, if it exists.
1121 vp = paircopy(t->state);
1122 if (vp) pairadd(&fake->packet->vps, vp);
1126 * If this is set, we copy SOME of the request attributes
1127 * from outside of the tunnel to inside of the tunnel.
1129 * We copy ONLY those attributes which do NOT already
1130 * exist in the tunneled request.
1132 if (t->copy_request_to_tunnel) {
1135 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
1137 * The attribute is a server-side thingy,
1140 if ((vp->attribute > 255) &&
1141 (vp->vendor == 0)) {
1146 * The outside attribute is already in the
1147 * tunnel, don't copy it.
1149 * This works for BOTH attributes which
1150 * are originally in the tunneled request,
1151 * AND attributes which are copied there
1154 if (pairfind(fake->packet->vps, vp->attribute, vp->vendor)) {
1159 * Some attributes are handled specially.
1161 switch (vp->attribute) {
1163 * NEVER copy Message-Authenticator,
1164 * EAP-Message, or State. They're
1165 * only for outside of the tunnel.
1168 case PW_USER_PASSWORD:
1169 case PW_CHAP_PASSWORD:
1170 case PW_CHAP_CHALLENGE:
1171 case PW_PROXY_STATE:
1172 case PW_MESSAGE_AUTHENTICATOR:
1173 case PW_EAP_MESSAGE:
1179 * By default, copy it over.
1186 * Don't copy from the head, we've already
1189 copy = paircopy2(vp, vp->attribute, vp->vendor);
1190 pairadd(&fake->packet->vps, copy);
1194 if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0)) != NULL) {
1195 fake->server = vp->vp_strvalue;
1197 } else if (t->virtual_server) {
1198 fake->server = t->virtual_server;
1200 } /* else fake->server == request->server */
1203 if ((debug_flag > 0) && fr_log_fp) {
1204 RDEBUG("Sending tunneled request");
1206 debug_pair_list(fake->packet->vps);
1208 fprintf(fr_log_fp, "server %s {\n",
1209 (fake->server == NULL) ? "" : fake->server);
1213 * Process channel binding here.
1215 chbind_len = eap_chbind_vp2packet(fake->packet->vps, &chbind_packet);
1216 if (chbind_len > 0) {
1218 CHBIND_REQ *req = chbind_allocate();
1220 RDEBUG("received chbind request");
1221 req->chbind_req_pkt = (uint8_t *)chbind_packet;
1222 req->chbind_req_len = chbind_len;
1223 if (fake->username) {
1224 req->username = fake->username->vp_octets;
1225 req->username_len = fake->username->length;
1227 req->username = NULL;
1228 req->username_len = 0;
1230 chbind_rcode = chbind_process(request, req);
1232 /* free the chbind packet; we're done with it */
1233 free(chbind_packet);
1235 /* encapsulate response here */
1236 if (req->chbind_resp_len > 0) {
1237 RDEBUG("sending chbind response");
1238 pairadd(&fake->reply->vps,
1239 eap_chbind_packet2vp((eap_chbind_packet_t *)req->chbind_resp,
1240 req->chbind_resp_len));
1242 RDEBUG("no chbind response");
1245 /* clean up chbind req */
1248 if (chbind_rcode != PW_AUTHENTICATION_ACK)
1249 return chbind_rcode;
1253 * Call authentication recursively, which will
1254 * do PAP, CHAP, MS-CHAP, etc.
1256 rad_authenticate(fake);
1259 * Note that we don't do *anything* with the reply
1262 if ((debug_flag > 0) && fr_log_fp) {
1263 fprintf(fr_log_fp, "} # server %s\n",
1264 (fake->server == NULL) ? "" : fake->server);
1266 RDEBUG("Got tunneled reply code %d", fake->reply->code);
1268 debug_pair_list(fake->reply->vps);
1272 * Decide what to do with the reply.
1274 switch (fake->reply->code) {
1275 case 0: /* No reply code, must be proxied... */
1277 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM, 0);
1279 eap_tunnel_data_t *tunnel;
1280 RDEBUG("Tunneled authentication will be proxied to %s", vp->vp_strvalue);
1283 * Tell the original request that it's going
1286 pairmove2(&(request->config_items),
1287 &(fake->config_items),
1288 PW_PROXY_TO_REALM, 0);
1291 * Seed the proxy packet with the
1294 rad_assert(request->proxy == NULL);
1295 request->proxy = fake->packet;
1296 memset(&request->proxy->src_ipaddr, 0,
1297 sizeof(request->proxy->src_ipaddr));
1298 memset(&request->proxy->src_ipaddr, 0,
1299 sizeof(request->proxy->src_ipaddr));
1300 request->proxy->src_port = 0;
1301 request->proxy->dst_port = 0;
1302 fake->packet = NULL;
1303 rad_free(&fake->reply);
1307 * Set up the callbacks for the tunnel
1309 tunnel = rad_malloc(sizeof(*tunnel));
1310 memset(tunnel, 0, sizeof(*tunnel));
1312 tunnel->tls_session = tls_session;
1313 tunnel->callback = eapttls_postproxy;
1316 * Associate the callback with the request.
1318 rcode = request_data_add(request,
1320 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1322 rad_assert(rcode == 0);
1325 * rlm_eap.c has taken care of associating
1326 * the handler with the fake request.
1328 * So we associate the fake request with
1331 rcode = request_data_add(request,
1333 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1334 fake, my_request_free);
1335 rad_assert(rcode == 0);
1339 * Didn't authenticate the packet, but
1340 * we're proxying it.
1342 rcode = PW_STATUS_CLIENT;
1345 #endif /* WITH_PROXY */
1347 RDEBUG("No tunneled reply was found for request %d , and the request was not proxied: rejecting the user.",
1349 rcode = PW_AUTHENTICATION_REJECT;
1355 * Returns RLM_MODULE_FOO, and we want to return
1358 rcode = process_reply(handler, tls_session, request,
1361 case RLM_MODULE_REJECT:
1362 rcode = PW_AUTHENTICATION_REJECT;
1365 case RLM_MODULE_HANDLED:
1366 rcode = PW_ACCESS_CHALLENGE;
1370 rcode = PW_AUTHENTICATION_ACK;
1374 rcode = PW_AUTHENTICATION_REJECT;
1380 request_free(&fake);