2 * peap.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
25 USES_APPLE_DEPRECATED_API /* OpenSSL API has been deprecated by Apple */
29 static int setup_fake_request(REQUEST *request, REQUEST *fake, peap_tunnel_t *t);
32 * Send protected EAP-Failure
34 * Result-TLV = Failure
36 static int eappeap_failure(eap_handler_t *handler, tls_session_t *tls_session)
38 uint8_t tlv_packet[11];
39 REQUEST *request = handler->request;
43 tlv_packet[0] = PW_EAP_REQUEST;
44 tlv_packet[1] = handler->eap_ds->response->id +1;
46 tlv_packet[3] = 11; /* length of this packet */
47 tlv_packet[4] = PW_EAP_TLV;
49 tlv_packet[6] = EAP_TLV_ACK_RESULT;
51 tlv_packet[8] = 2; /* length of the data portion */
53 tlv_packet[10] = EAP_TLV_FAILURE;
55 (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
58 * FIXME: Check the return code.
60 tls_handshake_send(request, tls_session);
67 * Send protected EAP-Success
69 * Result-TLV = Success
71 static int eappeap_success(eap_handler_t *handler, tls_session_t *tls_session)
73 uint8_t tlv_packet[11];
74 REQUEST *request = handler->request;
78 tlv_packet[0] = PW_EAP_REQUEST;
79 tlv_packet[1] = handler->eap_ds->response->id +1;
81 tlv_packet[3] = 11; /* length of this packet */
82 tlv_packet[4] = PW_EAP_TLV;
83 tlv_packet[5] = 0x80; /* mandatory AVP */
84 tlv_packet[6] = EAP_TLV_ACK_RESULT;
86 tlv_packet[8] = 2; /* length of the data portion */
88 tlv_packet[10] = EAP_TLV_SUCCESS;
90 (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
93 * FIXME: Check the return code.
95 tls_handshake_send(request, tls_session);
101 static int eappeap_identity(eap_handler_t *handler, tls_session_t *tls_session)
103 eap_packet_raw_t eap_packet;
105 eap_packet.code = PW_EAP_REQUEST;
106 eap_packet.id = handler->eap_ds->response->id + 1;
107 eap_packet.length[0] = 0;
108 eap_packet.length[1] = EAP_HEADER_LEN + 1;
109 eap_packet.data[0] = PW_EAP_IDENTITY;
111 (tls_session->record_plus)(&tls_session->clean_in,
112 &eap_packet, sizeof(eap_packet));
114 tls_handshake_send(handler->request, tls_session);
115 (tls_session->record_init)(&tls_session->clean_in);
121 * Send an MS SoH request
123 static int eappeap_soh(eap_handler_t *handler, tls_session_t *tls_session)
125 uint8_t tlv_packet[20];
127 tlv_packet[0] = 254; /* extended type */
130 tlv_packet[2] = 0x01; /* ms vendor */
131 tlv_packet[3] = 0x37;
133 tlv_packet[4] = 0; /* ms soh eap */
136 tlv_packet[7] = 0x21;
138 tlv_packet[8] = 0; /* vendor-spec tlv */
142 tlv_packet[11] = 8; /* payload len */
144 tlv_packet[12] = 0; /* ms vendor */
146 tlv_packet[14] = 0x01;
147 tlv_packet[15] = 0x37;
154 (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 20);
155 tls_handshake_send(handler->request, tls_session);
159 static void eapsoh_verify(REQUEST *request, RADIUS_PACKET *packet,
160 uint8_t const *data, unsigned int data_len) {
163 uint8_t eap_method_base;
168 vp = fr_pair_make(packet, &packet->vps, "SoH-Supported", "no", T_OP_EQ);
169 if (data && data[0] == PW_EAP_NAK) {
170 RDEBUG("SoH - client NAKed");
174 if (!data || data_len < 8) {
175 RDEBUG("SoH - eap payload too short");
179 eap_method_base = *data++;
180 if (eap_method_base != 254) {
181 RDEBUG("SoH - response is not extended EAP: %i", eap_method_base);
185 eap_vendor = soh_pull_be_24(data); data += 3;
186 if (eap_vendor != 0x137) {
187 RDEBUG("SoH - extended eap vendor %08x is not Microsoft", eap_vendor);
191 eap_method = soh_pull_be_32(data); data += 4;
192 if (eap_method != 0x21) {
193 RDEBUG("SoH - response eap type %08x is not EAP-SoH", eap_method);
198 rv = soh_verify(request, data, data_len - 8);
200 RDEBUG("SoH - error decoding payload: %s", fr_strerror());
207 * Verify the tunneled EAP message.
209 static int eapmessage_verify(REQUEST *request,
210 uint8_t const *data, unsigned int data_len)
212 eap_packet_raw_t const *eap_packet = (eap_packet_raw_t const *) data;
213 eap_type_t eap_method;
216 * No data, OR only 1 byte of EAP type.
218 if (!data || (data_len == 0) ||
219 ((data_len <= 1) && (data[0] != PW_EAP_IDENTITY))) {
224 switch (eap_method) {
225 case PW_EAP_IDENTITY:
227 RDEBUG2("Identity - ");
230 RDEBUG2("Identity - %*s",
231 data_len - 1, data + 1);
235 * If the first byte of the packet is
236 * EAP-Response, and the EAP data is a TLV,
237 * then it looks OK...
239 case PW_EAP_RESPONSE:
240 if (eap_packet->data[0] == PW_EAP_TLV) {
241 RDEBUG2("Received EAP-TLV response");
244 RDEBUG2("Got something weird");
249 * We normally do Microsoft MS-CHAPv2 (26), versus
250 * Cisco MS-CHAPv2 (29).
252 case PW_EAP_MSCHAPV2:
254 RDEBUG2("EAP method %s (%d)", eap_type2name(eap_method),
263 * Convert a pseudo-EAP packet to a list of VALUE_PAIR's.
265 static VALUE_PAIR *eap2vp(UNUSED REQUEST *request, RADIUS_PACKET *packet,
267 uint8_t const *data, size_t data_len)
271 VALUE_PAIR *vp = NULL, *head = NULL;
274 if (data_len > 65535) return NULL; /* paranoia */
276 vp = fr_pair_afrom_num(packet, PW_EAP_MESSAGE, 0);
282 if (total > 249) total = 249;
285 * Hand-build an EAP packet from the crap in PEAP version 0.
287 vp->vp_length = EAP_HEADER_LEN + total;
288 vp->vp_octets = p = talloc_array(vp, uint8_t, vp->vp_length);
290 p[0] = PW_EAP_RESPONSE;
291 p[1] = eap_ds->response->id;
292 p[2] = (data_len + EAP_HEADER_LEN) >> 8;
293 p[3] = (data_len + EAP_HEADER_LEN) & 0xff;
295 memcpy(p + EAP_HEADER_LEN, data, total);
297 fr_cursor_init(&cursor, &head);
298 fr_cursor_insert(&cursor, vp);
299 while (total < data_len) {
300 vp = fr_pair_afrom_num(packet, PW_EAP_MESSAGE, 0);
302 fr_pair_list_free(&head);
306 fr_pair_value_memcpy(vp, data + total, (data_len - total));
308 total += vp->vp_length;
310 fr_cursor_insert(&cursor, vp);
318 * Convert a list of VALUE_PAIR's to an EAP packet, through the
319 * simple expedient of dumping the EAP message
321 static int vp2eap(REQUEST *request, tls_session_t *tls_session, VALUE_PAIR *vp)
323 rad_assert(vp != NULL);
328 * Skip the id, code, and length. Just write the EAP
329 * type & data to the client.
332 if ((rad_debug_lvl > 2) && fr_log_fp) {
333 size_t i, total, start = EAP_HEADER_LEN;
336 for (this = fr_cursor_init(&cursor, &vp); this; this = fr_cursor_next(&cursor)) {
337 for (i = start; i < vp->vp_length; i++) {
338 if ((total & 0x0f) == 0) {
339 fprintf(fr_log_fp, " PEAP tunnel data out %04x: ", (int) total);
341 fprintf(fr_log_fp, "%02x ", vp->vp_octets[i]);
343 if ((total & 0x0f) == 0x0f) {
344 fprintf(fr_log_fp, "\n");
353 if ((total & 0x0f) != 0) {
354 fprintf(fr_log_fp, "\n");
360 * Send the EAP data in the first attribute, WITHOUT the
363 (tls_session->record_plus)(&tls_session->clean_in, vp->vp_octets + EAP_HEADER_LEN, vp->vp_length - EAP_HEADER_LEN);
366 * Send the rest of the EAP data, but skipping the first VP.
368 fr_cursor_init(&cursor, &vp);
369 for (this = fr_cursor_next(&cursor);
371 this = fr_cursor_next(&cursor)) {
372 (tls_session->record_plus)(&tls_session->clean_in, this->vp_octets, this->vp_length);
375 tls_handshake_send(request, tls_session);
382 * See if there's a TLV in the response.
384 static int eappeap_check_tlv(REQUEST *request, uint8_t const *data,
387 eap_packet_raw_t const *eap_packet = (eap_packet_raw_t const *) data;
389 if (data_len < 11) return 0;
392 * Look for success or failure.
394 if ((eap_packet->code == PW_EAP_RESPONSE) &&
395 (eap_packet->data[0] == PW_EAP_TLV)) {
396 if (data[10] == EAP_TLV_SUCCESS) {
400 if (data[10] == EAP_TLV_FAILURE) {
401 RDEBUG2("Client rejected our response. The password is probably incorrect");
406 RDEBUG("Unknown TLV %02x", data[10]);
413 * Use a reply packet to determine what to do.
415 static rlm_rcode_t CC_HINT(nonnull) process_reply(eap_handler_t *handler, tls_session_t *tls_session,
416 REQUEST *request, RADIUS_PACKET *reply)
418 rlm_rcode_t rcode = RLM_MODULE_REJECT;
420 peap_tunnel_t *t = tls_session->opaque;
422 if ((rad_debug_lvl > 0) && fr_log_fp) {
423 RDEBUG("Got tunneled reply RADIUS code %d", reply->code);
424 rdebug_pair_list(L_DBG_LVL_1, request, reply->vps, NULL);
427 switch (reply->code) {
428 case PW_CODE_ACCESS_ACCEPT:
429 RDEBUG2("Tunneled authentication was successful");
430 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
431 eappeap_success(handler, tls_session);
432 rcode = RLM_MODULE_HANDLED;
435 * If we've been told to use the attributes from
436 * the reply, then do so.
438 * WARNING: This may leak information about the
441 if (t->use_tunneled_reply) {
442 RDEBUG2("Saving tunneled attributes for later");
445 * Clean up the tunneled reply.
447 fr_pair_delete_by_num(&reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
448 fr_pair_delete_by_num(&reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
449 fr_pair_delete_by_num(&reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
452 * Delete MPPE keys & encryption policy. We don't
455 fr_pair_delete_by_num(&reply->vps, 7, VENDORPEC_MICROSOFT, TAG_ANY);
456 fr_pair_delete_by_num(&reply->vps, 8, VENDORPEC_MICROSOFT, TAG_ANY);
457 fr_pair_delete_by_num(&reply->vps, 16, VENDORPEC_MICROSOFT, TAG_ANY);
458 fr_pair_delete_by_num(&reply->vps, 17, VENDORPEC_MICROSOFT, TAG_ANY);
460 fr_pair_list_free(&t->accept_vps); /* for proxying MS-CHAP2 */
461 fr_pair_list_mcopy_by_num(t, &t->accept_vps, &reply->vps, 0, 0, TAG_ANY);
462 rad_assert(!reply->vps);
466 case PW_CODE_ACCESS_REJECT:
467 RDEBUG2("Tunneled authentication was rejected");
468 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
469 eappeap_failure(handler, tls_session);
470 rcode = RLM_MODULE_HANDLED;
473 case PW_CODE_ACCESS_CHALLENGE:
474 RDEBUG2("Got tunneled Access-Challenge");
477 * Keep the State attribute, if necessary.
479 * Get rid of the old State, too.
481 fr_pair_list_free(&t->state);
482 fr_pair_list_mcopy_by_num(t, &t->state, &reply->vps, PW_STATE, 0, TAG_ANY);
485 * PEAP takes only EAP-Message attributes inside
486 * of the tunnel. Any Reply-Message in the
487 * Access-Challenge is ignored.
490 fr_pair_list_mcopy_by_num(t, &vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
493 * Handle EAP-MSCHAP-V2, where Access-Accept's
494 * from the home server may contain MS-CHAP2-Success,
495 * which the module turns into challenges, so that
496 * the client may respond to the challenge with
499 if (t->home_access_accept && t->use_tunneled_reply) {
500 RDEBUG2("Saving tunneled attributes for later");
503 * Clean up the tunneled reply.
505 fr_pair_delete_by_num(&reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
506 fr_pair_delete_by_num(&reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
508 rad_assert(!t->accept_vps);
509 fr_pair_list_mcopy_by_num(t, &t->accept_vps, &reply->vps, 0, 0, TAG_ANY);
510 rad_assert(!reply->vps);
514 * Handle the ACK, by tunneling any necessary reply
515 * VP's back to the client.
518 vp2eap(request, tls_session, vp);
519 fr_pair_list_free(&vp);
522 rcode = RLM_MODULE_HANDLED;
526 RDEBUG2("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
527 rcode = RLM_MODULE_REJECT;
536 * Do post-proxy processing,
538 static int CC_HINT(nonnull) eappeap_postproxy(eap_handler_t *handler, void *data)
541 tls_session_t *tls_session = (tls_session_t *) data;
542 REQUEST *fake, *request = handler->request;
544 RDEBUG2("Passing reply from proxy back into the tunnel");
547 * If there was a fake request associated with the proxied
548 * request, do more processing of it.
550 fake = (REQUEST *) request_data_get(handler->request,
551 handler->request->proxy,
552 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
555 * Do the callback, if it exists, and if it was a success.
557 if (fake && (handler->request->proxy_reply->code == PW_CODE_ACCESS_ACCEPT)) {
558 peap_tunnel_t *t = tls_session->opaque;
560 t->home_access_accept = true;
565 rad_assert(!fake->packet);
566 fake->packet = talloc_steal(fake, request->proxy);
567 fake->packet->src_ipaddr = request->packet->src_ipaddr;
568 request->proxy = NULL;
570 rad_assert(!fake->reply);
571 fake->reply = talloc_steal(fake, request->proxy_reply);
572 request->proxy_reply = NULL;
574 if ((rad_debug_lvl > 0) && fr_log_fp) {
575 fprintf(fr_log_fp, "server %s {\n", fake->server);
579 * Perform a post-auth stage, which will get the EAP
582 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
583 RDEBUG2("Passing reply back for EAP-MS-CHAP-V2");
584 process_post_proxy(0, fake);
587 * FIXME: If rcode returns fail, do something
590 rcode = rad_postauth(fake);
592 if ((rad_debug_lvl > 0) && fr_log_fp) {
593 fprintf(fr_log_fp, "} # server %s\n", fake->server);
595 RDEBUG("Final reply from tunneled session code %d", fake->reply->code);
596 rdebug_pair_list(L_DBG_LVL_1, request, fake->reply->vps, NULL);
602 request->proxy = talloc_steal(request, fake->packet);
604 request->proxy_reply = talloc_steal(request, fake->reply);
608 * And we're done with this request.
612 case RLM_MODULE_FAIL:
614 eaptls_fail(handler, 0);
617 default: /* Don't Do Anything */
618 RDEBUG2("Got reply %d", request->proxy_reply->code);
622 talloc_free(fake); /* robust if !fake */
625 * If there was no EAP-Message in the reply packet, then
626 * we know that we're supposed to re-run the "authenticate"
627 * stage, in order to get the right kind of handling...
631 * Process the reply from the home server.
634 rcode = process_reply(handler, tls_session, handler->request,
635 handler->request->proxy_reply);
638 * The proxy code uses the reply from the home server as
639 * the basis for the reply to the NAS. We don't want that,
640 * so we toss it, after we've had our way with it.
642 fr_pair_list_free(&handler->request->proxy_reply->vps);
645 case RLM_MODULE_REJECT:
646 RDEBUG2("Reply was rejected");
647 eaptls_fail(handler, 0);
650 case RLM_MODULE_HANDLED:
651 RDEBUG2("Reply was handled");
652 eaptls_request(handler->eap_ds, tls_session);
653 request->proxy_reply->code = PW_CODE_ACCESS_CHALLENGE;
657 RDEBUG2("Reply was OK");
660 * Success: Automatically return MPPE keys.
662 return eaptls_success(handler, 0);
665 RDEBUG2("Reply was unknown");
669 eaptls_fail(handler, 0);
675 static char const *peap_state(peap_tunnel_t *t)
678 case PEAP_STATUS_TUNNEL_ESTABLISHED:
679 return "TUNNEL ESTABLISHED";
681 case PEAP_STATUS_WAIT_FOR_SOH_RESPONSE:
682 return "WAITING FOR SOH RESPONSE";
684 case PEAP_STATUS_INNER_IDENTITY_REQ_SENT:
685 return "WAITING FOR INNER IDENTITY";
687 case PEAP_STATUS_SENT_TLV_SUCCESS:
688 return "send tlv success";
690 case PEAP_STATUS_SENT_TLV_FAILURE:
691 return "send tlv failure";
693 case PEAP_STATUS_PHASE2_INIT:
694 return "phase2_init";
696 case PEAP_STATUS_PHASE2:
705 static void print_tunneled_data(uint8_t const *data, size_t data_len)
709 if ((rad_debug_lvl > 2) && fr_log_fp) {
710 for (i = 0; i < data_len; i++) {
711 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " PEAP tunnel data in %02x: ", (int) i);
713 fprintf(fr_log_fp, "%02x ", data[i]);
715 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
717 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
723 * Process the pseudo-EAP contents of the tunneled data.
725 rlm_rcode_t eappeap_process(eap_handler_t *handler, tls_session_t *tls_session)
727 peap_tunnel_t *t = tls_session->opaque;
730 rlm_rcode_t rcode = RLM_MODULE_REJECT;
732 unsigned int data_len;
734 REQUEST *request = handler->request;
735 EAP_DS *eap_ds = handler->eap_ds;
738 * Just look at the buffer directly, without doing
739 * record_minus. This lets us avoid another data copy.
741 data_len = tls_session->clean_out.used;
742 tls_session->clean_out.used = 0;
743 data = tls_session->clean_out.data;
745 RDEBUG2("PEAP state %s", peap_state(t));
747 if ((t->status != PEAP_STATUS_TUNNEL_ESTABLISHED) &&
748 !eapmessage_verify(request, data, data_len)) {
749 REDEBUG("Tunneled data is invalid");
750 if (rad_debug_lvl > 2) print_tunneled_data(data, data_len);
751 return RLM_MODULE_REJECT;
755 case PEAP_STATUS_TUNNEL_ESTABLISHED:
756 /* FIXME: should be no data in the buffer here, check & assert? */
758 if (SSL_session_reused(tls_session->ssl)) {
759 RDEBUG2("Skipping Phase2 because of session resumption");
760 t->session_resumption_state = PEAP_RESUMPTION_YES;
762 t->status = PEAP_STATUS_WAIT_FOR_SOH_RESPONSE;
763 RDEBUG2("Requesting SoH from client");
764 eappeap_soh(handler, tls_session);
765 return RLM_MODULE_HANDLED;
767 /* we're good, send success TLV */
768 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
769 eappeap_success(handler, tls_session);
772 /* send an identity request */
773 t->session_resumption_state = PEAP_RESUMPTION_NO;
774 t->status = PEAP_STATUS_INNER_IDENTITY_REQ_SENT;
775 eappeap_identity(handler, tls_session);
777 return RLM_MODULE_HANDLED;
779 case PEAP_STATUS_INNER_IDENTITY_REQ_SENT:
780 /* we're expecting an identity response */
781 if (data[0] != PW_EAP_IDENTITY) {
782 REDEBUG("Expected EAP-Identity, got something else");
783 return RLM_MODULE_REJECT;
789 t->username = fr_pair_make(t, NULL, "User-Name", NULL, T_OP_EQ);
790 rad_assert(t->username != NULL);
792 fr_pair_value_bstrncpy(t->username, data + 1, data_len - 1);
794 RDEBUG("Got inner identity '%s'", t->username->vp_strvalue);
796 t->status = PEAP_STATUS_WAIT_FOR_SOH_RESPONSE;
797 RDEBUG2("Requesting SoH from client");
798 eappeap_soh(handler, tls_session);
799 return RLM_MODULE_HANDLED;
801 t->status = PEAP_STATUS_PHASE2_INIT;
804 case PEAP_STATUS_WAIT_FOR_SOH_RESPONSE:
805 fake = request_alloc_fake(request);
806 rad_assert(!fake->packet->vps);
807 eapsoh_verify(request, fake->packet, data, data_len);
808 setup_fake_request(request, fake, t);
810 if (t->soh_virtual_server) {
811 fake->server = t->soh_virtual_server;
813 RDEBUG("Sending SoH request to server %s", fake->server ? fake->server : "NULL");
814 rad_virtual_server(fake);
816 if (fake->reply->code != PW_CODE_ACCESS_ACCEPT) {
817 RDEBUG2("SoH was rejected");
819 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
820 eappeap_failure(handler, tls_session);
821 return RLM_MODULE_HANDLED;
824 /* save the SoH VPs */
825 rad_assert(!t->soh_reply_vps);
826 fr_pair_list_mcopy_by_num(t, &t->soh_reply_vps, &fake->reply->vps, 0, 0, TAG_ANY);
827 rad_assert(!fake->reply->vps);
830 if (t->session_resumption_state == PEAP_RESUMPTION_YES) {
831 /* we're good, send success TLV */
832 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
833 eappeap_success(handler, tls_session);
834 return RLM_MODULE_HANDLED;
837 t->status = PEAP_STATUS_PHASE2_INIT;
842 * If we authenticated the user, then it's OK.
844 case PEAP_STATUS_SENT_TLV_SUCCESS:
845 if (eappeap_check_tlv(request, data, data_len)) {
847 return RLM_MODULE_OK;
851 * Otherwise, the client rejected the session
852 * resumption. If the session is being re-used,
853 * we need to do a full authentication.
855 * We do this by sending an EAP-Identity request
856 * inside of the PEAP tunnel.
858 if (t->session_resumption_state == PEAP_RESUMPTION_YES) {
859 RDEBUG2("Client rejected session resumption. Re-starting full authentication");
862 * Mark session resumption status.
864 t->status = PEAP_STATUS_INNER_IDENTITY_REQ_SENT;
865 t->session_resumption_state = PEAP_RESUMPTION_NO;
867 eappeap_identity(handler, tls_session);
868 return RLM_MODULE_HANDLED;
871 REDEBUG("We sent a success, but the client did not agree");
872 return RLM_MODULE_REJECT;
875 * Damned if I know why the clients continue sending EAP
876 * packets after we told them to f*ck off.
878 case PEAP_STATUS_SENT_TLV_FAILURE:
880 RINFO("The users session was previously rejected: returning reject (again.)");
881 RINFO("This means you need to read the PREVIOUS messages in the debug output");
882 RINFO("to find out the reason why the user was rejected");
883 RINFO("Look for \"reject\" or \"fail\". Those earlier messages will tell you");
884 RINFO("what went wrong, and how to fix the problem");
887 return RLM_MODULE_REJECT;
889 case PEAP_STATUS_PHASE2_INIT:
890 RDEBUG("In state machine in phase2 init?");
892 case PEAP_STATUS_PHASE2:
896 REDEBUG("Unhandled state in peap");
897 return RLM_MODULE_REJECT;
900 fake = request_alloc_fake(request);
902 rad_assert(!fake->packet->vps);
906 * If we're in PHASE2_INIT, the phase2 method hasn't been
907 * sent an Identity packet yet; do so from the stored
908 * username and this will kick off the phase2 eap method
911 case PEAP_STATUS_PHASE2_INIT: {
912 size_t len = t->username->vp_length + EAP_HEADER_LEN + 1;
915 t->status = PEAP_STATUS_PHASE2;
917 vp = fr_pair_afrom_num(fake->packet, PW_EAP_MESSAGE, 0);
919 vp->vp_octets = q = talloc_array(vp, uint8_t, vp->vp_length);
921 q[0] = PW_EAP_RESPONSE;
922 q[1] = eap_ds->response->id;
923 q[2] = (len >> 8) & 0xff;
925 q[4] = PW_EAP_IDENTITY;
927 memcpy(q + EAP_HEADER_LEN + 1,
928 t->username->vp_strvalue, t->username->vp_length);
930 fr_pair_add(&fake->packet->vps, vp);
932 if (t->default_method != 0) {
933 RDEBUG2("Setting default EAP type for tunneled EAP session");
934 vp = fr_pair_make(fake, &fake->config, "EAP-Type", "0", T_OP_EQ);
935 vp->vp_integer = t->default_method;
939 case PEAP_STATUS_PHASE2:
940 fake->packet->vps = eap2vp(request, fake->packet,
941 eap_ds, data, data_len);
942 if (!fake->packet->vps) {
944 RDEBUG2("Unable to convert tunneled EAP packet to internal server data structures");
945 return RLM_MODULE_REJECT;
950 REDEBUG("Invalid state change in PEAP");
951 return RLM_MODULE_REJECT;
954 RDEBUG2("Got tunneled request");
955 rdebug_pair_list(L_DBG_LVL_2, request, fake->packet->vps, NULL);
958 * Update other items in the REQUEST data structure.
962 * There's no User-Name in the tunneled session,
963 * so we add one here, by pulling it out of the
964 * EAP-Identity packet.
966 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
967 t->username = fr_pair_make(t, NULL, "User-Name", NULL, T_OP_EQ);
968 rad_assert(t->username != NULL);
970 fr_pair_value_bstrncpy(t->username, data + 1, data_len - 1);
972 RDEBUG2("Got tunneled identity of %s", t->username->vp_strvalue);
975 * If there's a default EAP type,
978 if (t->default_method != 0) {
979 RDEBUG2("Setting default EAP type for tunneled EAP session");
980 vp = fr_pair_make(fake, &fake->config, "EAP-Type", "0", T_OP_EQ);
981 vp->vp_integer = t->default_method;
984 } /* else there WAS a t->username */
986 setup_fake_request(request, fake, t);
988 if ((vp = fr_pair_find_by_num(request->config, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
989 fake->server = vp->vp_strvalue;
991 } else if (t->virtual_server) {
992 fake->server = t->virtual_server;
994 } /* else fake->server == request->server */
997 RDEBUG2("Sending tunneled request to %s", fake->server);
999 RDEBUG2("Sending tunnelled request");
1001 rdebug_pair_list(L_DBG_LVL_2, request, fake->packet->vps, NULL);
1004 * Call authentication recursively, which will
1005 * do PAP, CHAP, MS-CHAP, etc.
1007 rad_virtual_server(fake);
1010 * Note that we don't do *anything* with the reply
1013 RDEBUG2("Got tunneled reply code %d", fake->reply->code);
1014 rdebug_pair_list(L_DBG_LVL_2, request, fake->reply->vps, NULL);
1017 * Decide what to do with the reply.
1019 switch (fake->reply->code) {
1020 case 0: /* No reply code, must be proxied... */
1022 vp = fr_pair_find_by_num(fake->config, PW_PROXY_TO_REALM, 0, TAG_ANY);
1025 eap_tunnel_data_t *tunnel;
1028 * The tunneled request was NOT handled,
1029 * it has to be proxied. This means that
1030 * the "authenticate" stage was never
1033 * If we are told to NOT proxy the
1034 * tunneled request as EAP, then this
1035 * means that we've got to decode it,
1036 * which means that we MUST run the
1037 * "authenticate" portion by hand, here.
1039 * Once the tunneled EAP session is ALMOST
1040 * done, THEN we proxy it...
1042 if (!t->proxy_tunneled_request_as_eap) {
1043 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
1046 * Hmm... should we check for
1047 * Auth-Type & EAP-Message here?
1052 * Run the EAP authentication.
1054 RDEBUG2("Calling authenticate in order to initiate tunneled EAP session");
1055 rcode = process_authenticate(PW_AUTH_TYPE_EAP, fake);
1056 if (rcode == RLM_MODULE_OK) {
1058 * Authentication succeeded! Rah!
1060 fake->reply->code = PW_CODE_ACCESS_ACCEPT;
1064 if (rcode != RLM_MODULE_HANDLED) {
1065 RDEBUG("Can't handle the return code %d", rcode);
1066 rcode = RLM_MODULE_REJECT;
1071 * The module decided it wasn't
1072 * done. Handle it like normal.
1074 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
1075 RDEBUG2("Cancelling proxy to realm %s until the tunneled EAP session "
1076 "has been established", vp->vp_strvalue);
1081 * The module has decoded the
1082 * EAP-Message into another set
1085 fr_pair_delete_by_num(&fake->packet->vps,
1086 PW_EAP_MESSAGE, 0, TAG_ANY);
1089 RDEBUG2("Tunnelled authentication will be proxied to %s", vp->vp_strvalue);
1092 * Tell the original request that it's going
1095 fr_pair_list_mcopy_by_num(request, &request->config,
1097 PW_PROXY_TO_REALM, 0, TAG_ANY);
1100 * Seed the proxy packet with the
1103 rad_assert(!request->proxy);
1104 request->proxy = talloc_steal(request, fake->packet);
1105 memset(&request->proxy->src_ipaddr, 0,
1106 sizeof(request->proxy->src_ipaddr));
1107 memset(&request->proxy->dst_ipaddr, 0,
1108 sizeof(request->proxy->dst_ipaddr));
1109 request->proxy->src_port = 0;
1110 request->proxy->dst_port = 0;
1111 fake->packet = NULL;
1112 rad_free(&fake->reply);
1116 * Set up the callbacks for the tunnel
1118 tunnel = talloc_zero(request, eap_tunnel_data_t);
1119 tunnel->tls_session = tls_session;
1120 tunnel->callback = eappeap_postproxy;
1123 * Associate the callback with the request.
1125 rcode = request_data_add(request,
1127 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1129 rad_assert(rcode == 0);
1132 * We're not proxying it as EAP, so we've got
1133 * to do the callback later.
1135 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
1136 RDEBUG2("Remembering to do EAP-MS-CHAP-V2 post-proxy");
1139 * rlm_eap.c has taken care of associating
1140 * the handler with the fake request.
1142 * So we associate the fake request with
1145 rcode = request_data_add(request, request->proxy,
1146 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1148 rad_assert(rcode == 0);
1151 * Do NOT free the fake request!
1153 return RLM_MODULE_UPDATED;
1157 * Didn't authenticate the packet, but
1158 * we're proxying it.
1160 rcode = RLM_MODULE_UPDATED;
1163 #endif /* WITH_PROXY */
1165 REDEBUG("Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
1166 rcode = RLM_MODULE_REJECT;
1174 rcode = process_reply(handler, tls_session, request,
1187 static int CC_HINT(nonnull) setup_fake_request(REQUEST *request, REQUEST *fake, peap_tunnel_t *t) {
1192 * Tell the request that it's a fake one.
1194 fr_pair_make(fake->packet, &fake->packet->vps, "Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
1197 vp = fr_pair_list_copy(fake->packet, t->username);
1198 fr_pair_add(&fake->packet->vps, vp);
1199 fake->username = vp;
1200 RDEBUG2("Setting User-Name to %s", fake->username->vp_strvalue);
1202 RDEBUG2("No tunnel username (SSL resumption?)");
1207 * Add the State attribute, too, if it exists.
1210 vp = fr_pair_list_copy(fake->packet, t->state);
1211 if (vp) fr_pair_add(&fake->packet->vps, vp);
1215 * If this is set, we copy SOME of the request attributes
1216 * from outside of the tunnel to inside of the tunnel.
1218 * We copy ONLY those attributes which do NOT already
1219 * exist in the tunneled request.
1221 * This code is copied from ../rlm_eap_ttls/ttls.c
1223 if (t->copy_request_to_tunnel) {
1227 for (vp = fr_cursor_init(&cursor, &request->packet->vps);
1229 vp = fr_cursor_next(&cursor)) {
1231 * The attribute is a server-side thingy,
1234 if ((vp->da->attr > 255) && (((vp->da->attr >> 16) & 0xffff) == 0)) {
1239 * The outside attribute is already in the
1240 * tunnel, don't copy it.
1242 * This works for BOTH attributes which
1243 * are originally in the tunneled request,
1244 * AND attributes which are copied there
1247 if (fr_pair_find_by_da(fake->packet->vps, vp->da, TAG_ANY)) continue;
1250 * Some attributes are handled specially.
1252 switch (vp->da->attr) {
1254 * NEVER copy Message-Authenticator,
1255 * EAP-Message, or State. They're
1256 * only for outside of the tunnel.
1259 case PW_USER_PASSWORD:
1260 case PW_CHAP_PASSWORD:
1261 case PW_CHAP_CHALLENGE:
1262 case PW_PROXY_STATE:
1263 case PW_MESSAGE_AUTHENTICATOR:
1264 case PW_EAP_MESSAGE:
1269 * By default, copy it over.
1276 * Don't copy from the head, we've already
1279 copy = fr_pair_list_copy_by_num(fake->packet, vp, vp->da->attr, vp->da->vendor, TAG_ANY);
1280 fr_pair_add(&fake->packet->vps, copy);