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(fake, 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 * Supplicant ACKs our failure.
877 case PEAP_STATUS_SENT_TLV_FAILURE:
879 RIDEBUG("The users session was previously rejected: returning reject (again.)");
880 RIDEBUG("This means you need to read the PREVIOUS messages in the debug output");
881 RIDEBUG("to find out the reason why the user was rejected");
882 RIDEBUG("Look for \"reject\" or \"fail\". Those earlier messages will tell you");
883 RIDEBUG("what went wrong, and how to fix the problem");
886 return RLM_MODULE_REJECT;
888 case PEAP_STATUS_PHASE2_INIT:
889 RDEBUG("In state machine in phase2 init?");
891 case PEAP_STATUS_PHASE2:
895 REDEBUG("Unhandled state in peap");
896 return RLM_MODULE_REJECT;
899 fake = request_alloc_fake(request);
901 rad_assert(!fake->packet->vps);
905 * If we're in PHASE2_INIT, the phase2 method hasn't been
906 * sent an Identity packet yet; do so from the stored
907 * username and this will kick off the phase2 eap method
910 case PEAP_STATUS_PHASE2_INIT: {
911 size_t len = t->username->vp_length + EAP_HEADER_LEN + 1;
914 t->status = PEAP_STATUS_PHASE2;
916 vp = fr_pair_afrom_num(fake->packet, PW_EAP_MESSAGE, 0);
918 vp->vp_octets = q = talloc_array(vp, uint8_t, vp->vp_length);
920 q[0] = PW_EAP_RESPONSE;
921 q[1] = eap_ds->response->id;
922 q[2] = (len >> 8) & 0xff;
924 q[4] = PW_EAP_IDENTITY;
926 memcpy(q + EAP_HEADER_LEN + 1,
927 t->username->vp_strvalue, t->username->vp_length);
929 fr_pair_add(&fake->packet->vps, vp);
931 if (t->default_method != 0) {
932 RDEBUG2("Setting default EAP type for tunneled EAP session");
933 vp = fr_pair_make(fake, &fake->config, "EAP-Type", "0", T_OP_EQ);
934 vp->vp_integer = t->default_method;
938 case PEAP_STATUS_PHASE2:
939 fake->packet->vps = eap2vp(request, fake->packet,
940 eap_ds, data, data_len);
941 if (!fake->packet->vps) {
943 RDEBUG2("Unable to convert tunneled EAP packet to internal server data structures");
944 return RLM_MODULE_REJECT;
949 REDEBUG("Invalid state change in PEAP");
950 return RLM_MODULE_REJECT;
953 RDEBUG2("Got tunneled request");
954 rdebug_pair_list(L_DBG_LVL_2, request, fake->packet->vps, NULL);
957 * Update other items in the REQUEST data structure.
961 * There's no User-Name in the tunneled session,
962 * so we add one here, by pulling it out of the
963 * EAP-Identity packet.
965 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
966 t->username = fr_pair_make(t, NULL, "User-Name", NULL, T_OP_EQ);
967 rad_assert(t->username != NULL);
969 fr_pair_value_bstrncpy(t->username, data + 1, data_len - 1);
971 RDEBUG2("Got tunneled identity of %s", t->username->vp_strvalue);
974 * If there's a default EAP type,
977 if (t->default_method != 0) {
978 RDEBUG2("Setting default EAP type for tunneled EAP session");
979 vp = fr_pair_make(fake, &fake->config, "EAP-Type", "0", T_OP_EQ);
980 vp->vp_integer = t->default_method;
983 } /* else there WAS a t->username */
985 setup_fake_request(request, fake, t);
987 if ((vp = fr_pair_find_by_num(request->config, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
988 fake->server = vp->vp_strvalue;
990 } else if (t->virtual_server) {
991 fake->server = t->virtual_server;
993 } /* else fake->server == request->server */
996 RDEBUG2("Sending tunneled request to %s", fake->server);
998 RDEBUG2("Sending tunnelled request");
1000 rdebug_pair_list(L_DBG_LVL_2, request, fake->packet->vps, NULL);
1003 * Call authentication recursively, which will
1004 * do PAP, CHAP, MS-CHAP, etc.
1006 rad_virtual_server(fake);
1009 * Note that we don't do *anything* with the reply
1012 RDEBUG2("Got tunneled reply code %d", fake->reply->code);
1013 rdebug_pair_list(L_DBG_LVL_2, request, fake->reply->vps, NULL);
1016 * Decide what to do with the reply.
1018 switch (fake->reply->code) {
1019 case 0: /* No reply code, must be proxied... */
1021 vp = fr_pair_find_by_num(fake->config, PW_PROXY_TO_REALM, 0, TAG_ANY);
1024 eap_tunnel_data_t *tunnel;
1027 * The tunneled request was NOT handled,
1028 * it has to be proxied. This means that
1029 * the "authenticate" stage was never
1032 * If we are told to NOT proxy the
1033 * tunneled request as EAP, then this
1034 * means that we've got to decode it,
1035 * which means that we MUST run the
1036 * "authenticate" portion by hand, here.
1038 * Once the tunneled EAP session is ALMOST
1039 * done, THEN we proxy it...
1041 if (!t->proxy_tunneled_request_as_eap) {
1042 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
1045 * Hmm... should we check for
1046 * Auth-Type & EAP-Message here?
1051 * Run the EAP authentication.
1053 RDEBUG2("Calling authenticate in order to initiate tunneled EAP session");
1054 rcode = process_authenticate(PW_AUTH_TYPE_EAP, fake);
1055 if (rcode == RLM_MODULE_OK) {
1057 * Authentication succeeded! Rah!
1059 fake->reply->code = PW_CODE_ACCESS_ACCEPT;
1063 if (rcode != RLM_MODULE_HANDLED) {
1064 RDEBUG("Can't handle the return code %d", rcode);
1065 rcode = RLM_MODULE_REJECT;
1070 * The module decided it wasn't
1071 * done. Handle it like normal.
1073 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
1074 RDEBUG2("Cancelling proxy to realm %s until the tunneled EAP session "
1075 "has been established", vp->vp_strvalue);
1080 * The module has decoded the
1081 * EAP-Message into another set
1084 fr_pair_delete_by_num(&fake->packet->vps,
1085 PW_EAP_MESSAGE, 0, TAG_ANY);
1088 RDEBUG2("Tunnelled authentication will be proxied to %s", vp->vp_strvalue);
1091 * Tell the original request that it's going
1094 fr_pair_list_mcopy_by_num(request, &request->config,
1096 PW_PROXY_TO_REALM, 0, TAG_ANY);
1099 * Seed the proxy packet with the
1102 rad_assert(!request->proxy);
1103 request->proxy = talloc_steal(request, fake->packet);
1104 memset(&request->proxy->src_ipaddr, 0,
1105 sizeof(request->proxy->src_ipaddr));
1106 memset(&request->proxy->dst_ipaddr, 0,
1107 sizeof(request->proxy->dst_ipaddr));
1108 request->proxy->src_port = 0;
1109 request->proxy->dst_port = 0;
1110 fake->packet = NULL;
1111 rad_free(&fake->reply);
1115 * Set up the callbacks for the tunnel
1117 tunnel = talloc_zero(request, eap_tunnel_data_t);
1118 tunnel->tls_session = tls_session;
1119 tunnel->callback = eappeap_postproxy;
1122 * Associate the callback with the request.
1124 rcode = request_data_add(request,
1126 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1128 rad_assert(rcode == 0);
1131 * We're not proxying it as EAP, so we've got
1132 * to do the callback later.
1134 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
1135 RDEBUG2("Remembering to do EAP-MS-CHAP-V2 post-proxy");
1138 * rlm_eap.c has taken care of associating
1139 * the handler with the fake request.
1141 * So we associate the fake request with
1144 rcode = request_data_add(request, request->proxy,
1145 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1147 rad_assert(rcode == 0);
1150 * Do NOT free the fake request!
1152 return RLM_MODULE_UPDATED;
1156 * Didn't authenticate the packet, but
1157 * we're proxying it.
1159 rcode = RLM_MODULE_UPDATED;
1162 #endif /* WITH_PROXY */
1164 REDEBUG("Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
1165 rcode = RLM_MODULE_REJECT;
1173 rcode = process_reply(handler, tls_session, request,
1186 static int CC_HINT(nonnull) setup_fake_request(REQUEST *request, REQUEST *fake, peap_tunnel_t *t) {
1191 * Tell the request that it's a fake one.
1193 fr_pair_make(fake->packet, &fake->packet->vps, "Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
1196 vp = fr_pair_list_copy(fake->packet, t->username);
1197 fr_pair_add(&fake->packet->vps, vp);
1198 fake->username = vp;
1199 RDEBUG2("Setting User-Name to %s", fake->username->vp_strvalue);
1201 RDEBUG2("No tunnel username (SSL resumption?)");
1206 * Add the State attribute, too, if it exists.
1209 vp = fr_pair_list_copy(fake->packet, t->state);
1210 if (vp) fr_pair_add(&fake->packet->vps, vp);
1214 * If this is set, we copy SOME of the request attributes
1215 * from outside of the tunnel to inside of the tunnel.
1217 * We copy ONLY those attributes which do NOT already
1218 * exist in the tunneled request.
1220 * This code is copied from ../rlm_eap_ttls/ttls.c
1222 if (t->copy_request_to_tunnel) {
1226 for (vp = fr_cursor_init(&cursor, &request->packet->vps);
1228 vp = fr_cursor_next(&cursor)) {
1230 * The attribute is a server-side thingy,
1233 if ((vp->da->attr > 255) && (((vp->da->attr >> 16) & 0xffff) == 0)) {
1238 * The outside attribute is already in the
1239 * tunnel, don't copy it.
1241 * This works for BOTH attributes which
1242 * are originally in the tunneled request,
1243 * AND attributes which are copied there
1246 if (fr_pair_find_by_da(fake->packet->vps, vp->da, TAG_ANY)) continue;
1249 * Some attributes are handled specially.
1251 switch (vp->da->attr) {
1253 * NEVER copy Message-Authenticator,
1254 * EAP-Message, or State. They're
1255 * only for outside of the tunnel.
1258 case PW_USER_PASSWORD:
1259 case PW_CHAP_PASSWORD:
1260 case PW_CHAP_CHALLENGE:
1261 case PW_PROXY_STATE:
1262 case PW_MESSAGE_AUTHENTICATOR:
1263 case PW_EAP_MESSAGE:
1268 * By default, copy it over.
1275 * Don't copy from the head, we've already
1278 copy = fr_pair_list_copy_by_num(fake->packet, vp, vp->da->attr, vp->da->vendor, TAG_ANY);
1279 fr_pair_add(&fake->packet->vps, copy);