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>
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,
47 const uint8_t *data, unsigned int data_len)
52 unsigned int remaining = data_len;
54 while (remaining > 0) {
57 if (remaining < hdr_len) {
58 RDEBUG2(" Diameter attribute is too small (%u) to contain a Diameter header", remaining);
62 memcpy(&attr, data, sizeof(attr));
64 memcpy(&length, data + 4, sizeof(length));
65 length = ntohl(length);
67 if ((data[4] & 0x80) != 0) {
69 RDEBUG2(" Diameter attribute is too small to contain a Diameter header with Vendor-Id");
77 * Get the length. If it's too big, die.
82 * Too short or too long is bad.
84 if (length <= (hdr_len - 4)) {
85 RDEBUG2("Tunneled attribute %u is too short (%u < %u) to contain anything useful.", attr, length, hdr_len);
89 if (length > remaining) {
90 RDEBUG2("Tunneled attribute %u is longer than room remaining in the packet (%u > %u).", attr, length, remaining);
95 * Check for broken implementations, which don't
96 * pad the AVP to a 4-octet boundary.
98 if (remaining == length) break;
101 * The length does NOT include the padding, so
102 * we've got to account for it here by rounding up
103 * to the nearest 4-byte boundary.
109 * If the rest of the diameter packet is larger than
110 * this attribute, continue.
112 * Otherwise, if the attribute over-flows the end
113 * of the packet, die.
115 if (remaining < length) {
116 RDEBUG2E("Diameter attribute overflows packet!");
121 * remaining > length, continue.
128 * We got this far. It looks OK.
135 * Convert diameter attributes to our VALUE_PAIR's
137 static VALUE_PAIR *diameter2vp(REQUEST *request, SSL *ssl,
138 const uint8_t *data, size_t data_len)
145 size_t data_left = data_len;
146 VALUE_PAIR *first = NULL;
147 VALUE_PAIR **last = &first;
150 while (data_left > 0) {
151 rad_assert(data_left <= data_len);
152 memcpy(&attr, data, sizeof(attr));
157 memcpy(&length, data, sizeof(length));
159 length = ntohl(length);
162 * A "vendor" flag, with a vendor ID of zero,
163 * is equivalent to no vendor. This is stupid.
166 if ((length & (1 << 31)) != 0) {
167 memcpy(&vendor, data, sizeof(vendor));
168 vendor = ntohl(vendor);
170 data += 4; /* skip the vendor field, it's zero */
171 offset += 4; /* offset to value field */
173 if (attr > 65535) goto next_attr;
174 if (vendor > FR_MAX_VENDOR) goto next_attr;
178 * FIXME: Handle the M bit. For now, we assume that
179 * some other module takes care of any attribute
180 * with the M bit set.
186 length &= 0x00ffffff;
189 * Get the size of the value portion of the
192 size = length - offset;
195 * Vendor attributes can be larger than 255.
196 * Normal attributes cannot be.
198 if ((attr > 255) && (vendor == 0)) {
199 RDEBUG2W("Skipping Diameter attribute %u",
205 * EAP-Message AVPs can be larger than 253 octets.
207 if ((size > 253) && !((vendor == 0) && (attr == PW_EAP_MESSAGE))) {
208 RDEBUG2W("diameter2vp skipping long attribute %u", attr);
213 * RADIUS VSAs are handled as Diameter attributes
214 * with Vendor-Id == 0, and the VSA data packed
215 * into the "String" field as per normal.
217 * EXCEPT for the MS-CHAP attributes.
219 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
223 buffer[0] = PW_VENDOR_SPECIFIC;
224 buffer[1] = size + 2;
225 memcpy(buffer + 2, data, size);
228 decoded = rad_attr2vp(NULL, NULL, NULL,
229 buffer, size + 2, &vp);
231 RDEBUG2E("diameter2vp failed decoding attr: %s",
236 if ((size_t) decoded != size + 2) {
237 RDEBUG2E("diameter2vp failed to entirely decode VSA");
246 } while (vp != NULL);
252 * Create it. If this fails, it's because we're OOM.
255 vp = paircreate(attr, vendor);
257 RDEBUG2("Failure in creating VP");
263 * If it's a type from our dictionary, then
264 * we need to put the data in a relevant place.
266 switch (vp->da->type) {
267 case PW_TYPE_INTEGER:
269 if (size != vp->length) {
273 * Bad format. Create a "raw"
277 if (vp) pairfree(&vp);
278 da = dict_attrunknown(attr, vendor, TRUE);
279 if (!da) return NULL;
280 vp = pairalloc(NULL, da);
281 if (size >= 253) size = 253;
283 memcpy(vp->vp_octets, data, vp->length);
286 memcpy(&vp->vp_integer, data, vp->length);
289 * Stored in host byte order: change it.
291 vp->vp_integer = ntohl(vp->vp_integer);
294 case PW_TYPE_INTEGER64:
295 if (size != vp->length) goto raw;
296 memcpy(&vp->vp_integer64, data, vp->length);
299 * Stored in host byte order: change it.
301 vp->vp_integer64 = ntohll(vp->vp_integer64);
305 if (size != vp->length) {
306 RDEBUG2("Invalid length attribute %d",
312 memcpy(&vp->vp_ipaddr, data, vp->length);
315 * Stored in network byte order: don't change it.
320 if (size != vp->length) goto raw;
321 vp->vp_integer = data[0];
325 if (size != vp->length) goto raw;
326 vp->vp_integer = (data[0] * 256) + data[1];
330 if (size != vp->length) goto raw;
331 memcpy(&vp->vp_signed, data, vp->length);
332 vp->vp_signed = ntohl(vp->vp_signed);
335 case PW_TYPE_IPV6ADDR:
336 if (size != vp->length) goto raw;
337 memcpy(&vp->vp_ipv6addr, data, vp->length);
340 case PW_TYPE_IPV6PREFIX:
341 if (size != vp->length) goto raw;
342 memcpy(&vp->vp_ipv6prefix, data, vp->length);
346 * String, octet, etc. Copy the data from the
347 * value field over verbatim.
350 if (attr == PW_EAP_MESSAGE) {
351 const uint8_t *eap_message = data;
354 * vp exists the first time around.
358 if (vp->length > 253) vp->length = 253;
359 memcpy(vp->vp_octets, eap_message,
363 eap_message += vp->length;
368 if (size == 0) break;
370 vp = paircreate(attr, vendor);
372 RDEBUG2("Failure in creating VP");
379 } /* else it's another kind of attribute */
383 if (size >= 253) size = 253;
385 memcpy(vp->vp_strvalue, data, vp->length);
390 * User-Password is NUL padded to a multiple
391 * of 16 bytes. Let's chop it to something
394 * NOTE: This means that the User-Password
395 * attribute CANNOT EVER have embedded zeros in it!
397 if ((vp->da->vendor == 0) && (vp->da->attr == PW_USER_PASSWORD)) {
399 * If the password is exactly 16 octets,
400 * it won't be zero-terminated.
402 vp->vp_strvalue[vp->length] = '\0';
403 vp->length = strlen(vp->vp_strvalue);
407 * Ensure that the client is using the
408 * correct challenge. This weirdness is
409 * to protect against against replay
410 * attacks, where anyone observing the
411 * CHAP exchange could pose as that user,
412 * by simply choosing to use the same
415 * By using a challenge based on
416 * information from the current session,
417 * we can guarantee that the client is
418 * not *choosing* a challenge.
420 * We're a little forgiving in that we
421 * have loose checks on the length, and
422 * we do NOT check the Id (first octet of
423 * the response to the challenge)
425 * But if the client gets the challenge correct,
426 * we're not too worried about the Id.
428 if (((vp->da->vendor == 0) && (vp->da->attr == PW_CHAP_CHALLENGE)) ||
429 ((vp->da->vendor == VENDORPEC_MICROSOFT) && (vp->da->attr == PW_MSCHAP_CHALLENGE))) {
430 uint8_t challenge[16];
432 if ((vp->length < 8) ||
434 RDEBUG("Tunneled challenge has invalid length");
440 eapttls_gen_challenge(ssl, challenge,
443 if (memcmp(challenge, vp->vp_octets,
445 RDEBUG("Tunneled challenge is incorrect");
460 * Catch non-aligned attributes.
462 if (data_left == length) break;
465 * The length does NOT include the padding, so
466 * we've got to account for it here by rounding up
467 * to the nearest 4-byte boundary.
472 rad_assert(data_left >= length);
474 data += length - offset; /* already updated */
478 * We got this far. It looks OK.
484 * Convert VALUE_PAIR's to diameter attributes, and write them
487 * The ONLY VALUE_PAIR's which may be passed to this function
488 * are ones which can go inside of a RADIUS (i.e. diameter)
489 * packet. So no server-configuration attributes, or the like.
491 static int vp2diameter(REQUEST *request, tls_session_t *tls_session, VALUE_PAIR *first)
494 * RADIUS packets are no more than 4k in size, so if
495 * we've got more than 4k of data to write, it's very
498 uint8_t buffer[4096];
510 for (vp = first; vp != NULL; vp = vp->next) {
512 * Too much data: die.
514 if ((total + vp->length + 12) >= sizeof(buffer)) {
515 RDEBUG2("output buffer is full!");
520 * Hmm... we don't group multiple EAP-Messages
521 * together. Maybe we should...
525 * Length is no more than 253, due to RADIUS
529 vendor = vp->da->vendor;
531 attr = vp->da->attr & 0xffff;
538 * Hmm... set the M bit for all attributes?
544 memcpy(p, &attr, sizeof(attr));
548 length += 8; /* includes 8 bytes of attr & length */
551 length += 4; /* include 4 bytes of vendor */
553 length = ntohl(length);
554 memcpy(p, &length, sizeof(length));
558 vendor = ntohl(vendor);
559 memcpy(p, &vendor, sizeof(vendor));
563 length = ntohl(length);
564 memcpy(p, &length, sizeof(length));
569 switch (vp->da->type) {
570 case PW_TYPE_INTEGER:
572 attr = htonl(vp->vp_integer); /* stored in host order */
573 memcpy(p, &attr, sizeof(attr));
577 case PW_TYPE_INTEGER64:
578 attr64 = htonll(vp->vp_integer64); /* stored in host order */
579 memcpy(p, &attr64, sizeof(attr64));
584 memcpy(p, &vp->vp_ipaddr, 4); /* network order */
591 memcpy(p, vp->vp_strvalue, vp->length);
597 * Skip to the end of the data.
603 * Align the data to a multiple of 4 bytes.
605 if ((total & 0x03) != 0) {
608 length = 4 - (total & 0x03);
609 for (i = 0; i < length; i++) {
615 } /* loop over the VP's to write. */
618 * Write the data in the buffer to the SSL session.
624 if ((debug_flag > 2) && fr_log_fp) {
625 for (i = 0; i < total; i++) {
626 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " TTLS tunnel data out %04x: ", (int) i);
628 fprintf(fr_log_fp, "%02x ", buffer[i]);
630 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
632 if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
636 (tls_session->record_plus)(&tls_session->clean_in, buffer, total);
639 * FIXME: Check the return code.
641 tls_handshake_send(request, tls_session);
651 * Use a reply packet to determine what to do.
653 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
654 REQUEST *request, RADIUS_PACKET *reply)
656 int rcode = RLM_MODULE_REJECT;
658 ttls_tunnel_t *t = tls_session->opaque;
660 handler = handler; /* -Wunused */
661 rad_assert(request != NULL);
662 rad_assert(handler->request == request);
665 * If the response packet was Access-Accept, then
666 * we're OK. If not, die horribly.
668 * FIXME: Take MS-CHAP2-Success attribute, and
669 * tunnel it back to the client, to authenticate
670 * ourselves to the client.
672 * FIXME: If we have an Access-Challenge, then
673 * the Reply-Message is tunneled back to the client.
675 * FIXME: If we have an EAP-Message, then that message
676 * must be tunneled back to the client.
678 * FIXME: If we have an Access-Challenge with a State
679 * attribute, then do we tunnel that to the client, or
680 * keep track of it ourselves?
682 * FIXME: EAP-Messages can only start with 'identity',
683 * NOT 'eap start', so we should check for that....
685 switch (reply->code) {
686 case PW_AUTHENTICATION_ACK:
687 RDEBUG("Got tunneled Access-Accept");
689 rcode = RLM_MODULE_OK;
692 * MS-CHAP2-Success means that we do NOT return
693 * an Access-Accept, but instead tunnel that
694 * attribute to the client, and keep going with
695 * the TTLS session. Once the client accepts
696 * our identity, it will respond with an empty
697 * packet, and we will send EAP-Success.
700 pairmove2(&vp, &reply->vps, PW_MSCHAP2_SUCCESS, VENDORPEC_MICROSOFT, TAG_ANY);
702 RDEBUG("Got MS-CHAP2-Success, tunneling it to the client in a challenge.");
703 rcode = RLM_MODULE_HANDLED;
704 t->authenticated = TRUE;
707 * Delete MPPE keys & encryption policy. We don't
710 pairdelete(&reply->vps, 7, VENDORPEC_MICROSOFT, TAG_ANY);
711 pairdelete(&reply->vps, 8, VENDORPEC_MICROSOFT, TAG_ANY);
712 pairdelete(&reply->vps, 16, VENDORPEC_MICROSOFT, TAG_ANY);
713 pairdelete(&reply->vps, 17, VENDORPEC_MICROSOFT, TAG_ANY);
716 * Use the tunneled reply, but not now.
718 if (t->use_tunneled_reply) {
719 t->accept_vps = reply->vps;
723 } else { /* no MS-CHAP2-Success */
725 * Can only have EAP-Message if there's
726 * no MS-CHAP2-Success. (FIXME: EAP-MSCHAP?)
728 * We also do NOT tunnel the EAP-Success
729 * attribute back to the client, as the client
730 * can figure it out, from the non-tunneled
731 * EAP-Success packet.
733 pairmove2(&vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
738 * Handle the ACK, by tunneling any necessary reply
739 * VP's back to the client.
742 vp2diameter(request, tls_session, vp);
747 * If we've been told to use the attributes from
748 * the reply, then do so.
750 * WARNING: This may leak information about the
753 if (t->use_tunneled_reply) {
754 pairdelete(&reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
755 pairadd(&request->reply->vps, reply->vps);
761 case PW_AUTHENTICATION_REJECT:
762 RDEBUG("Got tunneled Access-Reject");
763 rcode = RLM_MODULE_REJECT;
767 * Handle Access-Challenge, but only if we
768 * send tunneled reply data. This is because
769 * an Access-Challenge means that we MUST tunnel
770 * a Reply-Message to the client.
772 case PW_ACCESS_CHALLENGE:
773 RDEBUG("Got tunneled Access-Challenge");
776 * Keep the State attribute, if necessary.
778 * Get rid of the old State, too.
781 pairmove2(&t->state, &reply->vps, PW_STATE, 0, TAG_ANY);
784 * We should really be a bit smarter about this,
785 * and move over only those attributes which
786 * are relevant to the authentication request,
787 * but that's a lot more work, and this "dumb"
788 * method works in 99.9% of the situations.
791 pairmove2(&vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
794 * There MUST be a Reply-Message in the challenge,
795 * which we tunnel back to the client.
797 * If there isn't one in the reply VP's, then
798 * we MUST create one, with an empty string as
801 pairmove2(&vp, &reply->vps, PW_REPLY_MESSAGE, 0, TAG_ANY);
804 * Handle the ACK, by tunneling any necessary reply
805 * VP's back to the client.
808 vp2diameter(request, tls_session, vp);
811 rcode = RLM_MODULE_HANDLED;
815 RDEBUG("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
816 rcode = RLM_MODULE_INVALID;
826 * Do post-proxy processing,
828 static int eapttls_postproxy(EAP_HANDLER *handler, void *data)
831 tls_session_t *tls_session = (tls_session_t *) data;
832 REQUEST *fake, *request = handler->request;
834 rad_assert(request != NULL);
835 RDEBUG("Passing reply from proxy back into the tunnel.");
838 * If there was a fake request associated with the proxied
839 * request, do more processing of it.
841 fake = (REQUEST *) request_data_get(handler->request,
842 handler->request->proxy,
843 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
846 * Do the callback, if it exists, and if it was a success.
849 handler->request->proxy_reply &&
850 (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
854 rad_assert(fake->packet == NULL);
855 fake->packet = request->proxy;
856 fake->packet->src_ipaddr = request->packet->src_ipaddr;
857 request->proxy = NULL;
859 rad_assert(fake->reply == NULL);
860 fake->reply = request->proxy_reply;
861 request->proxy_reply = NULL;
863 if ((debug_flag > 0) && fr_log_fp) {
864 fprintf(fr_log_fp, "server %s {\n",
865 (fake->server == NULL) ? "" : fake->server);
869 * Perform a post-auth stage for the tunneled
872 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
873 rcode = rad_postauth(fake);
874 RDEBUG2("post-auth returns %d", rcode);
876 if ((debug_flag > 0) && fr_log_fp) {
877 fprintf(fr_log_fp, "} # server %s\n",
878 (fake->server == NULL) ? "" : fake->server);
880 RDEBUG("Final reply from tunneled session code %d",
882 debug_pair_list(fake->reply->vps);
888 request->proxy = fake->packet;
890 request->proxy_reply = fake->reply;
894 * And we're done with this request.
898 case RLM_MODULE_FAIL:
900 eaptls_fail(handler, 0);
904 default: /* Don't Do Anything */
905 RDEBUG2("Got reply %d",
906 request->proxy_reply->code);
910 request_free(&fake); /* robust if fake == NULL */
913 * Process the reply from the home server.
915 rcode = process_reply(handler, tls_session, handler->request,
916 handler->request->proxy_reply);
919 * The proxy code uses the reply from the home server as
920 * the basis for the reply to the NAS. We don't want that,
921 * so we toss it, after we've had our way with it.
923 pairfree(&handler->request->proxy_reply->vps);
926 case RLM_MODULE_REJECT:
927 RDEBUG("Reply was rejected");
930 case RLM_MODULE_HANDLED:
931 RDEBUG("Reply was handled");
932 eaptls_request(handler->eap_ds, tls_session);
936 RDEBUG("Reply was OK");
939 * Success: Automatically return MPPE keys.
941 return eaptls_success(handler, 0);
944 RDEBUG("Reply was unknown.");
948 eaptls_fail(handler, 0);
956 static void my_request_free(void *data)
958 REQUEST *request = (REQUEST *)data;
960 request_free(&request);
962 #endif /* WITH_PROXY */
965 * Process the "diameter" contents of the tunneled data.
967 int eapttls_process(EAP_HANDLER *handler, tls_session_t *tls_session)
969 int rcode = PW_AUTHENTICATION_REJECT;
975 REQUEST *request = handler->request;
976 eap_chbind_packet_t *chbind_packet;
979 rad_assert(request != NULL);
982 * Just look at the buffer directly, without doing
985 data_len = tls_session->clean_out.used;
986 tls_session->clean_out.used = 0;
987 data = tls_session->clean_out.data;
989 t = (ttls_tunnel_t *) tls_session->opaque;
992 * If there's no data, maybe this is an ACK to an
996 if (t->authenticated) {
997 RDEBUG("Got ACK, and the user was already authenticated.");
998 return PW_AUTHENTICATION_ACK;
999 } /* else no session, no data, die. */
1002 * FIXME: Call SSL_get_error() to see what went
1005 RDEBUG2("SSL_read Error");
1006 return PW_AUTHENTICATION_REJECT;
1010 if ((debug_flag > 2) && fr_log_fp) {
1013 for (i = 0; i < data_len; i++) {
1014 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " TTLS tunnel data in %04x: ", (int) i);
1016 fprintf(fr_log_fp, "%02x ", data[i]);
1018 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1020 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
1024 if (!diameter_verify(request, data, data_len)) {
1025 return PW_AUTHENTICATION_REJECT;
1029 * Allocate a fake REQUEST structe.
1031 fake = request_alloc_fake(request);
1033 rad_assert(fake->packet->vps == NULL);
1036 * Add the tunneled attributes to the fake request.
1038 fake->packet->vps = diameter2vp(request, tls_session->ssl, data, data_len);
1039 if (!fake->packet->vps) {
1040 request_free(&fake);
1041 return PW_AUTHENTICATION_REJECT;
1045 * Tell the request that it's a fake one.
1047 vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
1049 pairadd(&fake->packet->vps, vp);
1052 if ((debug_flag > 0) && fr_log_fp) {
1053 RDEBUG("Got tunneled request");
1055 debug_pair_list(fake->packet->vps);
1059 * Update other items in the REQUEST data structure.
1061 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1062 fake->password = pairfind(fake->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1065 * No User-Name, try to create one from stored data.
1067 if (!fake->username) {
1069 * No User-Name in the stored data, look for
1070 * an EAP-Identity, and pull it out of there.
1073 vp = pairfind(fake->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1075 (vp->length >= EAP_HEADER_LEN + 2) &&
1076 (vp->vp_strvalue[0] == PW_EAP_RESPONSE) &&
1077 (vp->vp_strvalue[EAP_HEADER_LEN] == PW_EAP_IDENTITY) &&
1078 (vp->vp_strvalue[EAP_HEADER_LEN + 1] != 0)) {
1080 * Create & remember a User-Name
1082 t->username = pairmake("User-Name", "", T_OP_EQ);
1083 rad_assert(t->username != NULL);
1085 memcpy(t->username->vp_strvalue, vp->vp_strvalue + 5,
1087 t->username->length = vp->length - 5;
1088 t->username->vp_strvalue[t->username->length] = 0;
1090 RDEBUG("Got tunneled identity of %s",
1091 t->username->vp_strvalue);
1094 * If there's a default EAP type,
1097 if (t->default_eap_type != 0) {
1098 RDEBUG("Setting default EAP type for tunneled EAP session.");
1099 vp = paircreate(PW_EAP_TYPE, 0);
1100 rad_assert(vp != NULL);
1101 vp->vp_integer = t->default_eap_type;
1102 pairadd(&fake->config_items, vp);
1107 * Don't reject the request outright,
1108 * as it's permitted to do EAP without
1111 RDEBUG2W("No EAP-Identity found to start EAP conversation.");
1113 } /* else there WAS a t->username */
1116 vp = paircopy(t->username);
1117 pairadd(&fake->packet->vps, vp);
1118 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1120 } /* else the request ALREADY had a User-Name */
1123 * Add the State attribute, too, if it exists.
1126 vp = paircopy(t->state);
1127 if (vp) pairadd(&fake->packet->vps, vp);
1131 * If this is set, we copy SOME of the request attributes
1132 * from outside of the tunnel to inside of the tunnel.
1134 * We copy ONLY those attributes which do NOT already
1135 * exist in the tunneled request.
1137 if (t->copy_request_to_tunnel) {
1140 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
1142 * The attribute is a server-side thingy,
1145 if ((vp->da->attr > 255) &&
1146 (vp->da->vendor == 0)) {
1151 * The outside attribute is already in the
1152 * tunnel, don't copy it.
1154 * This works for BOTH attributes which
1155 * are originally in the tunneled request,
1156 * AND attributes which are copied there
1159 if (pairfind(fake->packet->vps, vp->da->attr, vp->da->vendor, TAG_ANY)) {
1164 * Some attributes are handled specially.
1166 switch (vp->da->attr) {
1168 * NEVER copy Message-Authenticator,
1169 * EAP-Message, or State. They're
1170 * only for outside of the tunnel.
1173 case PW_USER_PASSWORD:
1174 case PW_CHAP_PASSWORD:
1175 case PW_CHAP_CHALLENGE:
1176 case PW_PROXY_STATE:
1177 case PW_MESSAGE_AUTHENTICATOR:
1178 case PW_EAP_MESSAGE:
1184 * By default, copy it over.
1191 * Don't copy from the head, we've already
1194 copy = paircopy2(vp, vp->da->attr, vp->da->vendor, TAG_ANY);
1195 pairadd(&fake->packet->vps, copy);
1199 if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
1200 fake->server = vp->vp_strvalue;
1202 } else if (t->virtual_server) {
1203 fake->server = t->virtual_server;
1205 } /* else fake->server == request->server */
1208 if ((debug_flag > 0) && fr_log_fp) {
1209 RDEBUG("Sending tunneled request");
1211 debug_pair_list(fake->packet->vps);
1213 fprintf(fr_log_fp, "server %s {\n",
1214 (fake->server == NULL) ? "" : fake->server);
1218 * Process channel binding here.
1220 chbind_len = eap_chbind_vp2packet(fake->packet->vps, &chbind_packet);
1221 if (chbind_len > 0) {
1222 /*CHBIND_REQ *req = chbind_allocate();
1223 req->chbind_req = chbind_packet;
1224 req->chbind_req_len = chbind_len;
1225 if (fake->username) {
1226 req->username = fake->username->vp_octets;
1227 req->username_len = fake->username->length;
1229 req->username = NULL;
1230 req->username_len = 0;
1232 chbind_process(request, req);
1235 /* free the chbind packet; we're done with it */
1236 free(chbind_packet);
1238 /* encapsulate response here */
1239 /*pairadd(replyvps, eap_chbind_packet2vp(req->chbind_resp,
1240 req->chbind_resp_len));
1243 /* clean up chbind req */
1244 /*chbind_free(req);*/
1248 * Call authentication recursively, which will
1249 * do PAP, CHAP, MS-CHAP, etc.
1251 rad_virtual_server(fake);
1254 * Note that we don't do *anything* with the reply
1257 if ((debug_flag > 0) && fr_log_fp) {
1258 fprintf(fr_log_fp, "} # server %s\n",
1259 (fake->server == NULL) ? "" : fake->server);
1261 RDEBUG("Got tunneled reply code %d", fake->reply->code);
1263 debug_pair_list(fake->reply->vps);
1267 * Decide what to do with the reply.
1269 switch (fake->reply->code) {
1270 case 0: /* No reply code, must be proxied... */
1272 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY);
1274 eap_tunnel_data_t *tunnel;
1275 RDEBUG("Tunneled authentication will be proxied to %s", vp->vp_strvalue);
1278 * Tell the original request that it's going
1281 pairmove2(&(request->config_items),
1282 &(fake->config_items),
1283 PW_PROXY_TO_REALM, 0, TAG_ANY);
1286 * Seed the proxy packet with the
1289 rad_assert(request->proxy == NULL);
1290 request->proxy = fake->packet;
1291 memset(&request->proxy->src_ipaddr, 0,
1292 sizeof(request->proxy->src_ipaddr));
1293 memset(&request->proxy->src_ipaddr, 0,
1294 sizeof(request->proxy->src_ipaddr));
1295 request->proxy->src_port = 0;
1296 request->proxy->dst_port = 0;
1297 fake->packet = NULL;
1298 rad_free(&fake->reply);
1302 * Set up the callbacks for the tunnel
1304 tunnel = rad_malloc(sizeof(*tunnel));
1305 memset(tunnel, 0, sizeof(*tunnel));
1307 tunnel->tls_session = tls_session;
1308 tunnel->callback = eapttls_postproxy;
1311 * Associate the callback with the request.
1313 rcode = request_data_add(request,
1315 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1317 rad_assert(rcode == 0);
1320 * rlm_eap.c has taken care of associating
1321 * the handler with the fake request.
1323 * So we associate the fake request with
1326 rcode = request_data_add(request,
1328 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1329 fake, my_request_free);
1330 rad_assert(rcode == 0);
1334 * Didn't authenticate the packet, but
1335 * we're proxying it.
1337 rcode = PW_STATUS_CLIENT;
1340 #endif /* WITH_PROXY */
1342 RDEBUG("No tunneled reply was found for request %d , and the request was not proxied: rejecting the user.",
1344 rcode = PW_AUTHENTICATION_REJECT;
1350 * Returns RLM_MODULE_FOO, and we want to return
1353 rcode = process_reply(handler, tls_session, request,
1356 case RLM_MODULE_REJECT:
1357 rcode = PW_AUTHENTICATION_REJECT;
1360 case RLM_MODULE_HANDLED:
1361 rcode = PW_ACCESS_CHALLENGE;
1365 rcode = PW_AUTHENTICATION_ACK;
1369 rcode = PW_AUTHENTICATION_REJECT;
1375 request_free(&fake);