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
27 #include "eap_chbind.h"
31 * 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
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * |V M r r r r r r| AVP Length |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * Verify that the diameter packet is valid.
46 static int diameter_verify(REQUEST *request, uint8_t const *data, unsigned int data_len)
51 unsigned int remaining = data_len;
53 while (remaining > 0) {
56 if (remaining < hdr_len) {
57 RDEBUG2("Diameter attribute is too small (%u) to contain a Diameter header", remaining);
61 memcpy(&attr, data, sizeof(attr));
63 memcpy(&length, data + 4, sizeof(length));
64 length = ntohl(length);
66 if ((data[4] & 0x80) != 0) {
68 RDEBUG2("Diameter attribute is too small to contain a Diameter header with Vendor-Id");
76 * Get the length. If it's too big, die.
81 * Too short or too long is bad.
83 if (length <= (hdr_len - 4)) {
84 RDEBUG2("Tunneled attribute %u is too short (%u < %u) to contain anything useful.", attr,
89 if (length > remaining) {
90 RDEBUG2("Tunneled attribute %u is longer than room remaining in the packet (%u > %u).", attr,
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 REDEBUG2("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, REQUEST *fake, SSL *ssl,
139 uint8_t const *data, size_t data_len)
146 size_t data_left = data_len;
148 VALUE_PAIR *first = NULL;
150 RADIUS_PACKET *packet = fake->packet; /* FIXME: api issues */
153 fr_cursor_init(&out, &first);
155 while (data_left > 0) {
156 rad_assert(data_left <= data_len);
157 memcpy(&attr, data, sizeof(attr));
162 memcpy(&length, data, sizeof(length));
164 length = ntohl(length);
167 * A "vendor" flag, with a vendor ID of zero,
168 * is equivalent to no vendor. This is stupid.
171 if ((length & (1 << 31)) != 0) {
172 memcpy(&vendor, data, sizeof(vendor));
173 vendor = ntohl(vendor);
175 data += 4; /* skip the vendor field, it's zero */
176 offset += 4; /* offset to value field */
178 if (attr > 65535) goto next_attr;
179 if (vendor > FR_MAX_VENDOR) goto next_attr;
183 * FIXME: Handle the M bit. For now, we assume that
184 * some other module takes care of any attribute
185 * with the M bit set.
191 length &= 0x00ffffff;
194 * Get the size of the value portion of the
197 size = length - offset;
200 * Vendor attributes can be larger than 255.
201 * Normal attributes cannot be.
203 if ((attr > 255) && (vendor == 0)) {
204 RWDEBUG2("Skipping Diameter attribute %u", attr);
209 * EAP-Message AVPs can be larger than 253 octets.
211 * For now, we rely on the main decoder in
212 * src/lib/radius to decode data into VPs. This
213 * means putting the data into a RADIUS attribute
214 * format. It also means that we can't handle
215 * "extended" attributes in the Diameter space. Oh well...
217 if ((size > 253) && !((vendor == 0) && (attr == PW_EAP_MESSAGE))) {
218 RWDEBUG2("diameter2vp skipping long attribute %u", attr);
223 * RADIUS VSAs are handled as Diameter attributes
224 * with Vendor-Id == 0, and the VSA data packed
225 * into the "String" field as per normal.
227 * EXCEPT for the MS-CHAP attributes.
229 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
233 buffer[0] = PW_VENDOR_SPECIFIC;
234 buffer[1] = size + 2;
235 memcpy(buffer + 2, data, size);
238 decoded = rad_attr2vp(packet, NULL, NULL, NULL,
239 buffer, size + 2, &vp);
241 REDEBUG2("diameter2vp failed decoding attr: %s",
246 if ((size_t) decoded != size + 2) {
247 REDEBUG2("diameter2vp failed to entirely decode VSA");
252 fr_cursor_insert(&out, vp);
258 * Create it. If this fails, it's because we're OOM.
261 vp = paircreate(packet, attr, vendor);
263 RDEBUG2("Failure in creating VP");
269 * If it's a type from our dictionary, then
270 * we need to put the data in a relevant place.
272 * @todo: Export the lib/radius.c decoder, and use it here!
274 switch (vp->da->type) {
275 case PW_TYPE_INTEGER:
277 if (size != vp->length) {
281 * Bad format. Create a "raw"
285 if (vp) pairfree(&vp);
286 da = dict_attrunknown(attr, vendor, true);
287 if (!da) return NULL;
288 vp = pairalloc(packet, da);
289 if (!vp) return NULL;
290 pairmemcpy(vp, data, size);
293 memcpy(&vp->vp_integer, data, vp->length);
296 * Stored in host byte order: change it.
298 vp->vp_integer = ntohl(vp->vp_integer);
301 case PW_TYPE_INTEGER64:
302 if (size != vp->length) goto raw;
303 memcpy(&vp->vp_integer64, data, vp->length);
306 * Stored in host byte order: change it.
308 vp->vp_integer64 = ntohll(vp->vp_integer64);
311 case PW_TYPE_IPV4_ADDR:
312 if (size != vp->length) {
313 RDEBUG2("Invalid length attribute %d",
319 memcpy(&vp->vp_ipaddr, data, vp->length);
322 * Stored in network byte order: don't change it.
327 if (size != vp->length) goto raw;
328 vp->vp_integer = data[0];
332 if (size != vp->length) goto raw;
333 vp->vp_integer = (data[0] * 256) + data[1];
337 if (size != vp->length) goto raw;
338 memcpy(&vp->vp_signed, data, vp->length);
339 vp->vp_signed = ntohl(vp->vp_signed);
342 case PW_TYPE_IPV6_ADDR:
343 if (size != vp->length) goto raw;
344 memcpy(&vp->vp_ipv6addr, data, vp->length);
347 case PW_TYPE_IPV6_PREFIX:
348 if (size != vp->length) goto raw;
349 memcpy(&vp->vp_ipv6prefix, data, vp->length);
353 * Ensure it's NUL terminated.
356 vp->vp_strvalue = p = talloc_array(vp, char, size + 1);
358 memcpy(p, data, size);
360 vp->length = strlen(p);
364 * Copy it over verbatim.
368 pairmemcpy(vp, data, size);
373 * Ensure that the client is using the
374 * correct challenge. This weirdness is
375 * to protect against against replay
376 * attacks, where anyone observing the
377 * CHAP exchange could pose as that user,
378 * by simply choosing to use the same
381 * By using a challenge based on
382 * information from the current session,
383 * we can guarantee that the client is
384 * not *choosing* a challenge.
386 * We're a little forgiving in that we
387 * have loose checks on the length, and
388 * we do NOT check the Id (first octet of
389 * the response to the challenge)
391 * But if the client gets the challenge correct,
392 * we're not too worried about the Id.
394 if (((vp->da->vendor == 0) && (vp->da->attr == PW_CHAP_CHALLENGE)) ||
395 ((vp->da->vendor == VENDORPEC_MICROSOFT) && (vp->da->attr == PW_MSCHAP_CHALLENGE))) {
396 uint8_t challenge[16];
398 if ((vp->length < 8) ||
400 RDEBUG("Tunneled challenge has invalid length");
406 eapttls_gen_challenge(ssl, challenge,
409 if (memcmp(challenge, vp->vp_octets,
411 RDEBUG("Tunneled challenge is incorrect");
421 fr_cursor_insert(&out, vp);
425 * Catch non-aligned attributes.
427 if (data_left == length) break;
430 * The length does NOT include the padding, so
431 * we've got to account for it here by rounding up
432 * to the nearest 4-byte boundary.
437 rad_assert(data_left >= length);
439 data += length - offset; /* already updated */
443 * We got this far. It looks OK.
449 * Convert VALUE_PAIR's to diameter attributes, and write them
452 * The ONLY VALUE_PAIR's which may be passed to this function
453 * are ones which can go inside of a RADIUS (i.e. diameter)
454 * packet. So no server-configuration attributes, or the like.
456 static int vp2diameter(REQUEST *request, tls_session_t *tls_session, VALUE_PAIR *first)
459 * RADIUS packets are no more than 4k in size, so if
460 * we've got more than 4k of data to write, it's very
463 uint8_t buffer[4096];
476 for (vp = fr_cursor_init(&cursor, &first); vp; vp = fr_cursor_next(&cursor)) {
478 * Too much data: die.
480 if ((total + vp->length + 12) >= sizeof(buffer)) {
481 RDEBUG2("output buffer is full!");
486 * Hmm... we don't group multiple EAP-Messages
487 * together. Maybe we should...
491 vendor = vp->da->vendor;
493 attr = vp->da->attr & 0xffff;
500 * Hmm... set the M bit for all attributes?
506 memcpy(p, &attr, sizeof(attr));
510 length += 8; /* includes 8 bytes of attr & length */
513 length += 4; /* include 4 bytes of vendor */
515 length = ntohl(length);
516 memcpy(p, &length, sizeof(length));
520 vendor = ntohl(vendor);
521 memcpy(p, &vendor, sizeof(vendor));
525 length = ntohl(length);
526 memcpy(p, &length, sizeof(length));
531 switch (vp->da->type) {
532 case PW_TYPE_INTEGER:
534 attr = htonl(vp->vp_integer); /* stored in host order */
535 memcpy(p, &attr, sizeof(attr));
539 case PW_TYPE_INTEGER64:
540 attr64 = htonll(vp->vp_integer64); /* stored in host order */
541 memcpy(p, &attr64, sizeof(attr64));
545 case PW_TYPE_IPV4_ADDR:
546 memcpy(p, &vp->vp_ipaddr, 4); /* network order */
553 memcpy(p, vp->vp_strvalue, vp->length);
559 * Skip to the end of the data.
565 * Align the data to a multiple of 4 bytes.
567 if ((total & 0x03) != 0) {
570 length = 4 - (total & 0x03);
571 for (i = 0; i < length; i++) {
577 } /* loop over the VP's to write. */
580 * Write the data in the buffer to the SSL session.
586 if ((debug_flag > 2) && fr_log_fp) {
587 for (i = 0; i < total; i++) {
588 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " TTLS tunnel data out %04x: ", (int) i);
590 fprintf(fr_log_fp, "%02x ", buffer[i]);
592 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
594 if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
598 (tls_session->record_plus)(&tls_session->clean_in, buffer, total);
601 * FIXME: Check the return code.
603 tls_handshake_send(request, tls_session);
613 * Use a reply packet to determine what to do.
615 static rlm_rcode_t CC_HINT(nonnull) process_reply(UNUSED eap_handler_t *handler, tls_session_t *tls_session,
616 REQUEST *request, RADIUS_PACKET *reply)
618 rlm_rcode_t rcode = RLM_MODULE_REJECT;
620 ttls_tunnel_t *t = tls_session->opaque;
622 rad_assert(handler->request == request);
625 * If the response packet was Access-Accept, then
626 * we're OK. If not, die horribly.
628 * FIXME: Take MS-CHAP2-Success attribute, and
629 * tunnel it back to the client, to authenticate
630 * ourselves to the client.
632 * FIXME: If we have an Access-Challenge, then
633 * the Reply-Message is tunneled back to the client.
635 * FIXME: If we have an EAP-Message, then that message
636 * must be tunneled back to the client.
638 * FIXME: If we have an Access-Challenge with a State
639 * attribute, then do we tunnel that to the client, or
640 * keep track of it ourselves?
642 * FIXME: EAP-Messages can only start with 'identity',
643 * NOT 'eap start', so we should check for that....
645 switch (reply->code) {
646 case PW_CODE_ACCESS_ACCEPT:
647 RDEBUG("Got tunneled Access-Accept");
649 rcode = RLM_MODULE_OK;
652 * MS-CHAP2-Success means that we do NOT return
653 * an Access-Accept, but instead tunnel that
654 * attribute to the client, and keep going with
655 * the TTLS session. Once the client accepts
656 * our identity, it will respond with an empty
657 * packet, and we will send EAP-Success.
660 pairfilter(tls_session, &vp, &reply->vps, PW_MSCHAP2_SUCCESS, VENDORPEC_MICROSOFT, TAG_ANY);
662 RDEBUG("Got MS-CHAP2-Success, tunneling it to the client in a challenge");
663 rcode = RLM_MODULE_HANDLED;
664 t->authenticated = true;
667 * Delete MPPE keys & encryption policy. We don't
670 pairdelete(&reply->vps, 7, VENDORPEC_MICROSOFT, TAG_ANY);
671 pairdelete(&reply->vps, 8, VENDORPEC_MICROSOFT, TAG_ANY);
672 pairdelete(&reply->vps, 16, VENDORPEC_MICROSOFT, TAG_ANY);
673 pairdelete(&reply->vps, 17, VENDORPEC_MICROSOFT, TAG_ANY);
676 * Use the tunneled reply, but not now.
678 if (t->use_tunneled_reply) {
679 rad_assert(!t->accept_vps);
680 pairfilter(t, &t->accept_vps, &reply->vps,
682 rad_assert(!reply->vps);
685 } else { /* no MS-CHAP2-Success */
687 * Can only have EAP-Message if there's
688 * no MS-CHAP2-Success. (FIXME: EAP-MSCHAP?)
690 * We also do NOT tunnel the EAP-Success
691 * attribute back to the client, as the client
692 * can figure it out, from the non-tunneled
693 * EAP-Success packet.
695 pairfilter(tls_session, &vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
699 /* move channel binding responses; we need to send them */
700 pairfilter(tls_session, &vp, &reply->vps, PW_UKERNA_CHBIND, VENDORPEC_UKERNA, TAG_ANY);
701 if (pairfind(vp, PW_UKERNA_CHBIND, VENDORPEC_UKERNA, TAG_ANY) != NULL) {
702 t->authenticated = true;
704 * Use the tunneled reply, but not now.
706 if (t->use_tunneled_reply) {
707 rad_assert(!t->accept_vps);
708 pairfilter(t, &t->accept_vps, &reply->vps,
710 rad_assert(!reply->vps);
712 rcode = RLM_MODULE_HANDLED;
716 * Handle the ACK, by tunneling any necessary reply
717 * VP's back to the client.
720 RDEBUG("sending tunneled reply attributes");
722 RDEBUG("end tunneled reply attributes");
723 vp2diameter(request, tls_session, vp);
728 * If we've been told to use the attributes from
729 * the reply, then do so.
731 * WARNING: This may leak information about the
734 if (t->use_tunneled_reply) {
735 pairdelete(&reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
736 pairfilter(request->reply, &request->reply->vps,
737 &reply->vps, 0, 0, TAG_ANY);
742 case PW_CODE_ACCESS_REJECT:
743 RDEBUG("Got tunneled Access-Reject");
744 rcode = RLM_MODULE_REJECT;
748 * Handle Access-Challenge, but only if we
749 * send tunneled reply data. This is because
750 * an Access-Challenge means that we MUST tunnel
751 * a Reply-Message to the client.
753 case PW_CODE_ACCESS_CHALLENGE:
754 RDEBUG("Got tunneled Access-Challenge");
757 * Keep the State attribute, if necessary.
759 * Get rid of the old State, too.
762 pairfilter(t, &t->state, &reply->vps, PW_STATE, 0, TAG_ANY);
765 * We should really be a bit smarter about this,
766 * and move over only those attributes which
767 * are relevant to the authentication request,
768 * but that's a lot more work, and this "dumb"
769 * method works in 99.9% of the situations.
772 pairfilter(t, &vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
775 * There MUST be a Reply-Message in the challenge,
776 * which we tunnel back to the client.
778 * If there isn't one in the reply VP's, then
779 * we MUST create one, with an empty string as
782 pairfilter(t, &vp, &reply->vps, PW_REPLY_MESSAGE, 0, TAG_ANY);
784 /* also move chbind messages, if any */
785 pairfilter(t, &vp, &reply->vps, PW_UKERNA_CHBIND, VENDORPEC_UKERNA,
789 * Handle the ACK, by tunneling any necessary reply
790 * VP's back to the client.
793 vp2diameter(request, tls_session, vp);
796 rcode = RLM_MODULE_HANDLED;
800 RDEBUG("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
801 rcode = RLM_MODULE_INVALID;
811 * Do post-proxy processing,
813 static int CC_HINT(nonnull) eapttls_postproxy(eap_handler_t *handler, void *data)
816 tls_session_t *tls_session = (tls_session_t *) data;
817 REQUEST *fake, *request = handler->request;
819 RDEBUG("Passing reply from proxy back into the tunnel");
822 * If there was a fake request associated with the proxied
823 * request, do more processing of it.
825 fake = (REQUEST *) request_data_get(handler->request,
826 handler->request->proxy,
827 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
830 * Do the callback, if it exists, and if it was a success.
832 if (fake && (handler->request->proxy_reply->code == PW_CODE_ACCESS_ACCEPT)) {
836 rad_assert(!fake->packet);
837 fake->packet = talloc_steal(fake, request->proxy);
838 fake->packet->src_ipaddr = request->packet->src_ipaddr;
839 request->proxy = NULL;
841 rad_assert(!fake->reply);
842 fake->reply = talloc_steal(fake, request->proxy_reply);
843 request->proxy_reply = NULL;
845 if ((debug_flag > 0) && fr_log_fp) {
846 fprintf(fr_log_fp, "server %s {\n",
847 (!fake->server) ? "" : fake->server);
851 * Perform a post-auth stage for the tunneled
854 fake->log.lvl &= ~RAD_REQUEST_OPTION_PROXY_EAP;
855 rcode = rad_postauth(fake);
856 RDEBUG2("post-auth returns %d", rcode);
858 if ((debug_flag > 0) && fr_log_fp) {
859 fprintf(fr_log_fp, "} # server %s\n",
860 (!fake->server) ? "" : fake->server);
862 RDEBUG("Final reply from tunneled session code %d",
864 debug_pair_list(fake->reply->vps);
870 request->proxy = talloc_steal(request, fake->packet);
872 request->proxy_reply = talloc_steal(request, fake->reply);
876 * And we're done with this request.
880 case RLM_MODULE_FAIL:
882 eaptls_fail(handler, 0);
885 default: /* Don't Do Anything */
886 RDEBUG2("Got reply %d",
887 request->proxy_reply->code);
891 talloc_free(fake); /* robust if !fake */
894 * Process the reply from the home server.
896 rcode = process_reply(handler, tls_session, handler->request, handler->request->proxy_reply);
899 * The proxy code uses the reply from the home server as
900 * the basis for the reply to the NAS. We don't want that,
901 * so we toss it, after we've had our way with it.
903 pairfree(&handler->request->proxy_reply->vps);
906 case RLM_MODULE_REJECT:
907 RDEBUG("Reply was rejected");
910 case RLM_MODULE_HANDLED:
911 RDEBUG("Reply was handled");
912 eaptls_request(handler->eap_ds, tls_session);
913 request->proxy_reply->code = PW_CODE_ACCESS_CHALLENGE;
917 RDEBUG("Reply was OK");
920 * Success: Automatically return MPPE keys.
922 return eaptls_success(handler, 0);
925 RDEBUG("Reply was unknown");
929 eaptls_fail(handler, 0);
933 #endif /* WITH_PROXY */
936 * Process the "diameter" contents of the tunneled data.
938 int eapttls_process(eap_handler_t *handler, tls_session_t *tls_session)
940 PW_CODE code = PW_CODE_ACCESS_REJECT;
947 REQUEST *request = handler->request;
948 chbind_packet_t *chbind;
951 * Just look at the buffer directly, without doing
954 data_len = tls_session->clean_out.used;
955 tls_session->clean_out.used = 0;
956 data = tls_session->clean_out.data;
958 t = (ttls_tunnel_t *) tls_session->opaque;
961 * If there's no data, maybe this is an ACK to an
965 if (t->authenticated) {
966 RDEBUG("Got ACK, and the user was already authenticated");
967 return PW_CODE_ACCESS_ACCEPT;
968 } /* else no session, no data, die. */
971 * FIXME: Call SSL_get_error() to see what went
974 RDEBUG2("SSL_read Error");
975 return PW_CODE_ACCESS_REJECT;
979 if ((debug_flag > 2) && fr_log_fp) {
982 for (i = 0; i < data_len; i++) {
983 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " TTLS tunnel data in %04x: ", (int) i);
985 fprintf(fr_log_fp, "%02x ", data[i]);
987 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
989 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
993 if (!diameter_verify(request, data, data_len)) {
994 return PW_CODE_ACCESS_REJECT;
998 * Allocate a fake REQUEST structe.
1000 fake = request_alloc_fake(request);
1002 rad_assert(!fake->packet->vps);
1005 * Add the tunneled attributes to the fake request.
1007 fake->packet->vps = diameter2vp(request, fake, tls_session->ssl, data, data_len);
1008 if (!fake->packet->vps) {
1010 return PW_CODE_ACCESS_REJECT;
1014 * Tell the request that it's a fake one.
1016 pairmake_packet("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
1018 if ((debug_flag > 0) && fr_log_fp) {
1019 RDEBUG("Got tunneled request");
1021 debug_pair_list(fake->packet->vps);
1025 * Update other items in the REQUEST data structure.
1027 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1028 fake->password = pairfind(fake->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1031 * No User-Name, try to create one from stored data.
1033 if (!fake->username) {
1035 * No User-Name in the stored data, look for
1036 * an EAP-Identity, and pull it out of there.
1039 vp = pairfind(fake->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1041 (vp->length >= EAP_HEADER_LEN + 2) &&
1042 (vp->vp_strvalue[0] == PW_EAP_RESPONSE) &&
1043 (vp->vp_strvalue[EAP_HEADER_LEN] == PW_EAP_IDENTITY) &&
1044 (vp->vp_strvalue[EAP_HEADER_LEN + 1] != 0)) {
1048 * Create & remember a User-Name
1050 t->username = pairmake(t, NULL, "User-Name", NULL, T_OP_EQ);
1051 rad_assert(t->username != NULL);
1052 t->username->length = vp->length - 5;
1054 t->username->vp_strvalue = p = talloc_array(t->username, char,
1055 t->username->length + 1);
1056 memcpy(p, vp->vp_octets + 5, t->username->length);
1057 p[t->username->length] = 0;
1059 RDEBUG("Got tunneled identity of %s",
1060 t->username->vp_strvalue);
1063 * If there's a default EAP type,
1066 if (t->default_method != 0) {
1067 RDEBUG("Setting default EAP type for tunneled EAP session");
1068 vp = paircreate(fake, PW_EAP_TYPE, 0);
1069 rad_assert(vp != NULL);
1070 vp->vp_integer = t->default_method;
1071 pairadd(&fake->config_items, vp);
1076 * Don't reject the request outright,
1077 * as it's permitted to do EAP without
1080 RWDEBUG2("No EAP-Identity found to start EAP conversation");
1082 } /* else there WAS a t->username */
1085 vp = paircopy(fake->packet, t->username);
1086 pairadd(&fake->packet->vps, vp);
1087 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1089 } /* else the request ALREADY had a User-Name */
1092 * Add the State attribute, too, if it exists.
1095 vp = paircopy(fake->packet, t->state);
1096 if (vp) pairadd(&fake->packet->vps, vp);
1100 * If this is set, we copy SOME of the request attributes
1101 * from outside of the tunnel to inside of the tunnel.
1103 * We copy ONLY those attributes which do NOT already
1104 * exist in the tunneled request.
1106 if (t->copy_request_to_tunnel) {
1110 for (vp = fr_cursor_init(&cursor, &request->packet->vps); vp; vp = fr_cursor_next(&cursor)) {
1112 * The attribute is a server-side thingy,
1115 if ((vp->da->attr > 255) &&
1116 (vp->da->vendor == 0)) {
1121 * The outside attribute is already in the
1122 * tunnel, don't copy it.
1124 * This works for BOTH attributes which
1125 * are originally in the tunneled request,
1126 * AND attributes which are copied there
1129 if (pairfind(fake->packet->vps, vp->da->attr, vp->da->vendor, TAG_ANY)) {
1134 * Some attributes are handled specially.
1136 switch (vp->da->attr) {
1138 * NEVER copy Message-Authenticator,
1139 * EAP-Message, or State. They're
1140 * only for outside of the tunnel.
1143 case PW_USER_PASSWORD:
1144 case PW_CHAP_PASSWORD:
1145 case PW_CHAP_CHALLENGE:
1146 case PW_PROXY_STATE:
1147 case PW_MESSAGE_AUTHENTICATOR:
1148 case PW_EAP_MESSAGE:
1154 * By default, copy it over.
1161 * Don't copy from the head, we've already
1164 copy = paircopy2(fake->packet, vp, vp->da->attr, vp->da->vendor, TAG_ANY);
1165 pairadd(&fake->packet->vps, copy);
1169 if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
1170 fake->server = vp->vp_strvalue;
1172 } else if (t->virtual_server) {
1173 fake->server = t->virtual_server;
1175 } /* else fake->server == request->server */
1178 if ((debug_flag > 0) && fr_log_fp) {
1179 RDEBUG("Sending tunneled request");
1183 * Process channel binding.
1185 chbind = eap_chbind_vp2packet(fake, fake->packet->vps);
1187 PW_CODE chbind_code;
1188 CHBIND_REQ *req = talloc_zero(fake, CHBIND_REQ);
1190 RDEBUG("received chbind request");
1191 req->request = chbind;
1192 if (fake->username) {
1193 req->username = fake->username;
1195 req->username = NULL;
1197 chbind_code = chbind_process(request, req);
1199 /* free the chbind packet; we're done with it */
1202 /* encapsulate response here */
1203 if (req->response) {
1204 RDEBUG("sending chbind response");
1205 pairadd(&fake->reply->vps,
1206 eap_chbind_packet2vp(fake, req->response));
1208 RDEBUG("no chbind response");
1211 /* clean up chbind req */
1214 if (chbind_code != PW_CODE_ACCESS_ACCEPT) {
1220 * Call authentication recursively, which will
1221 * do PAP, CHAP, MS-CHAP, etc.
1223 rad_virtual_server(fake);
1226 * Decide what to do with the reply.
1228 switch (fake->reply->code) {
1229 case 0: /* No reply code, must be proxied... */
1231 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY);
1233 eap_tunnel_data_t *tunnel;
1234 RDEBUG("Tunneled authentication will be proxied to %s", vp->vp_strvalue);
1237 * Tell the original request that it's going
1240 pairfilter(request, &request->config_items,
1241 &fake->config_items,
1242 PW_PROXY_TO_REALM, 0, TAG_ANY);
1245 * Seed the proxy packet with the
1248 rad_assert(!request->proxy);
1249 request->proxy = talloc_steal(request, fake->packet);
1250 memset(&request->proxy->src_ipaddr, 0,
1251 sizeof(request->proxy->src_ipaddr));
1252 memset(&request->proxy->src_ipaddr, 0,
1253 sizeof(request->proxy->src_ipaddr));
1254 request->proxy->src_port = 0;
1255 request->proxy->dst_port = 0;
1256 fake->packet = NULL;
1257 rad_free(&fake->reply);
1261 * Set up the callbacks for the tunnel
1263 tunnel = talloc_zero(request, eap_tunnel_data_t);
1264 tunnel->tls_session = tls_session;
1265 tunnel->callback = eapttls_postproxy;
1268 * Associate the callback with the request.
1270 code = request_data_add(request, request->proxy, REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1272 rad_assert(code == 0);
1275 * rlm_eap.c has taken care of associating
1276 * the handler with the fake request.
1278 * So we associate the fake request with
1281 code = request_data_add(request, request->proxy, REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1283 rad_assert(code == 0);
1287 * Didn't authenticate the packet, but
1288 * we're proxying it.
1290 code = PW_CODE_STATUS_CLIENT;
1293 #endif /* WITH_PROXY */
1295 RDEBUG("No tunneled reply was found for request %d , and the request was not proxied: rejecting the user.",
1297 code = PW_CODE_ACCESS_REJECT;
1303 * Returns RLM_MODULE_FOO, and we want to return PW_FOO
1305 rcode = process_reply(handler, tls_session, request, fake->reply);
1307 case RLM_MODULE_REJECT:
1308 code = PW_CODE_ACCESS_REJECT;
1311 case RLM_MODULE_HANDLED:
1312 code = PW_CODE_ACCESS_CHALLENGE;
1316 code = PW_CODE_ACCESS_ACCEPT;
1320 code = PW_CODE_ACCESS_REJECT;