Add "proxy_tunneled_request_as_eap" configuration entry, which tells
[freeradius.git] / src / modules / rlm_eap / types / rlm_eap_peap / peap.c
1 /*
2  * peap.c  contains the interfaces that are called from eap
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *   Copyright 2003 Alan DeKok <aland@freeradius.org>
21  */
22 #include "eap_tls.h"
23 #include "eap_peap.h"
24
25 /*
26  *      Send protected EAP-Failure
27  *
28  *       Result-TLV = Failure
29  */
30 static int eappeap_failure(EAP_HANDLER *handler, tls_session_t *tls_session)
31 {
32         uint8_t tlv_packet[11];
33
34         DEBUG2("  rlm_eap_peap: FAILURE");
35
36         tlv_packet[0] = PW_EAP_REQUEST;
37         tlv_packet[1] = handler->eap_ds->response->id +1;
38         tlv_packet[2] = 0;
39         tlv_packet[3] = 11;     /* length of this packet */
40         tlv_packet[4] = PW_EAP_TLV;
41         tlv_packet[5] = 0x80;
42         tlv_packet[6] = EAP_TLV_ACK_RESULT;
43         tlv_packet[7] = 0;
44         tlv_packet[8] = 2;      /* length of the data portion */
45         tlv_packet[9] = 0;
46         tlv_packet[10] = EAP_TLV_FAILURE;
47
48         record_plus(&tls_session->clean_in, tlv_packet, 11);
49
50         /*
51          *      FIXME: Check the return code.
52          */
53         tls_handshake_send(tls_session);
54         record_init(&tls_session->clean_in);
55
56         return 1;
57 }
58
59
60 /*
61  *      Send protected EAP-Success
62  *
63  *       Result-TLV = Success
64  */
65 static int eappeap_success(EAP_HANDLER *handler, tls_session_t *tls_session)
66 {
67         uint8_t tlv_packet[11];
68
69         DEBUG2("  rlm_eap_peap: SUCCESS");
70
71         tlv_packet[0] = PW_EAP_REQUEST;
72         tlv_packet[1] = handler->eap_ds->response->id +1;
73         tlv_packet[2] = 0;
74         tlv_packet[3] = 11;     /* length of this packet */
75         tlv_packet[4] = PW_EAP_TLV;
76         tlv_packet[5] = 0x80;   /* mandatory AVP */
77         tlv_packet[6] = EAP_TLV_ACK_RESULT;
78         tlv_packet[7] = 0;
79         tlv_packet[8] = 2;      /* length of the data portion */
80         tlv_packet[9] = 0;
81         tlv_packet[10] = EAP_TLV_SUCCESS;
82
83         record_plus(&tls_session->clean_in, tlv_packet, 11);
84
85         /*
86          *      FIXME: Check the return code.
87          */
88         tls_handshake_send(tls_session);
89         record_init(&tls_session->clean_in);
90
91         return 1;
92 }
93
94
95 /*
96  *      Verify the tunneled EAP message.
97  */
98 static int eapmessage_verify(const uint8_t *data, unsigned int data_len)
99 {
100         const eap_packet_t *eap_packet = (const eap_packet_t *) data;
101         uint8_t eap_type;
102         char identity[256];
103
104         if (!data || (data_len <= 1)) {
105                 return 0;
106         }
107
108         eap_type = *data;
109         switch (eap_type) {
110         case PW_EAP_IDENTITY:
111                 memcpy(identity, data + 1, data_len - 1);
112                 identity[data_len - 1] = '\0';
113                 DEBUG2("  rlm_eap_peap: Identity - %s", identity);
114                 return 1;
115                 break;
116                 
117                 /*
118                  *      If the first byte of the packet is
119                  *      EAP-Response, and the EAP data is a TLV,
120                  *      then it looks OK...
121                  */
122         case PW_EAP_RESPONSE:
123                 if (eap_packet->data[0] == PW_EAP_TLV) {
124                         DEBUG2("  rlm_eap_peap: Received EAP-TLV response.");
125                         return 1;
126                 }
127                 DEBUG2("  rlm_eap_peap: Got something weird.");
128                 break;
129
130
131                 /*
132                  *      We normally do Microsoft MS-CHAPv2 (26), versus
133                  *      Cisco MS-CHAPv2 (29).
134                  */
135         case PW_EAP_MSCHAPV2:
136         default:
137                 DEBUG2("  rlm_eap_peap: EAP type %s",
138                        eaptype_type2name(eap_type,
139                                          identity, sizeof(identity)));
140                 return 1;
141                 break;
142         }
143
144         return 0;
145 }
146
147 /*
148  *      Convert a pseudo-EAP packet to a list of VALUE_PAIR's.
149  */
150 static VALUE_PAIR *eap2vp(EAP_DS *eap_ds,
151                           const uint8_t *data, unsigned int data_len)
152 {
153         VALUE_PAIR *vp = NULL;
154
155         /*
156          *      Sanity check this...
157          */
158         if (data_len + EAP_HEADER_LEN > MAX_STRING_LEN) {
159                 radlog(L_ERR, "rlm_eap_peap: EAP Response packet is too large.  Code must be fixed to handle this.");
160                 return NULL;
161         }
162
163         vp = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
164         if (!vp) {
165                 DEBUG2("  rlm_eap_peap: Failure in creating VP");
166                 return NULL;
167         }
168
169         /*
170          *      Hand-build an EAP packet from the crap in PEAP version 0.
171          */
172         vp->strvalue[0] = PW_EAP_RESPONSE;
173         vp->strvalue[1] = eap_ds->response->id;
174         vp->strvalue[2] = 0;
175         vp->strvalue[3] = EAP_HEADER_LEN + data_len;
176
177         memcpy(vp->strvalue + EAP_HEADER_LEN, data, data_len);
178         vp->length = EAP_HEADER_LEN + data_len;
179
180         return vp;
181 }
182
183
184 /*
185  *      Convert a list of VALUE_PAIR's to an EAP packet, through the
186  *      simple expedient of dumping the EAP message
187  */
188 static int vp2eap(tls_session_t *tls_session, VALUE_PAIR *vp)
189 {
190         if (vp->next != NULL) {
191                 radlog(L_ERR, "rlm_eap_peap: EAP Request packet is too large.  Code must be fixed to handle this.");
192                 return 0;
193         }
194
195         /*
196          *      Skip the id, code, and length.  Just write the EAP
197          *      type & data to the client.
198          */
199 #ifndef NDEBUG
200         if (debug_flag > 2) {
201                 int i;
202                 int total = vp->length - 4;
203                 
204                 if (debug_flag > 0) for (i = 0; i < total; i++) {
205                         if ((i & 0x0f) == 0) printf("  PEAP tunnel data out %04x: ", i);
206                         
207                         printf("%02x ", vp->strvalue[i + 4]);
208                         
209                         if ((i & 0x0f) == 0x0f) printf("\n");
210                 }
211                 if ((total & 0x0f) != 0) printf("\n");
212         }
213 #endif
214
215         /*
216          *      Send the EAP data, WITHOUT the header.
217          */
218 #if 1
219         record_plus(&tls_session->clean_in, vp->strvalue + EAP_HEADER_LEN,
220                 vp->length - EAP_HEADER_LEN);
221 #else
222         record_plus(&tls_session->clean_in, vp->strvalue, vp->length);
223 #endif
224         tls_handshake_send(tls_session);
225         record_init(&tls_session->clean_in);
226
227         return 1;
228 }
229
230
231 /*
232  *      See if there's a TLV in the response.
233  */
234 static int eappeap_check_tlv(const uint8_t *data)
235 {
236         const eap_packet_t *eap_packet = (const eap_packet_t *) data;
237
238         /*
239          *      Look for success or failure.
240          */
241         if ((eap_packet->code == PW_EAP_RESPONSE) &&
242             (eap_packet->data[0] == PW_EAP_TLV)) {
243                 if (data[10] == EAP_TLV_SUCCESS) {
244                         return 1;
245                 }
246
247                 if (data[10] == EAP_TLV_FAILURE) {
248                         DEBUG2("  rlm_eap_peap: Client rejected our response.  The password is probably incorrect.");
249                         return 0;
250                 }
251         }
252
253         return 0;
254 }
255
256
257 /*
258  *      Use a reply packet to determine what to do.
259  */
260 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
261                          REQUEST *request, RADIUS_PACKET *reply)
262 {
263         int rcode = RLM_MODULE_REJECT;
264         VALUE_PAIR *vp;
265         peap_tunnel_t *t = tls_session->opaque;
266
267         switch (reply->code) {
268         case PW_AUTHENTICATION_ACK:
269                 DEBUG2("  PEAP: Tunneled authentication was successful.");
270                 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
271                 eappeap_success(handler, tls_session);
272                 rcode = RLM_MODULE_HANDLED;
273                 
274                 /*
275                  *      If we've been told to use the attributes from
276                  *      the reply, then do so.
277                  *
278                  *      WARNING: This may leak information about the
279                  *      tunneled user!
280                  */
281                 if (t->use_tunneled_reply) {
282                         pairadd(&request->reply->vps, reply->vps);
283                         reply->vps = NULL;
284                 }
285                 break;
286
287         case PW_AUTHENTICATION_REJECT:
288                 DEBUG2("  PEAP: Tunneled authentication was rejected.");
289                 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
290                 eappeap_failure(handler, tls_session);
291                 rcode = RLM_MODULE_HANDLED;
292                 break;
293
294         case PW_ACCESS_CHALLENGE:
295                 DEBUG2("  PEAP: Got tunneled Access-Challenge");
296
297                 /*
298                  *      Keep the State attribute, if necessary.
299                  *
300                  *      Get rid of the old State, too.
301                  */
302                 pairfree(&t->state);
303                 pairmove2(&t->state, &(reply->vps), PW_STATE);
304
305                 /*
306                  *      PEAP takes only EAP-Message attributes inside
307                  *      of the tunnel.  Any Reply-Message in the
308                  *      Access-Challenge is ignored.
309                  */
310                 vp = NULL;
311                 pairmove2(&vp, &(reply->vps), PW_EAP_MESSAGE);
312
313                 /*
314                  *      Handle the ACK, by tunneling any necessary reply
315                  *      VP's back to the client.
316                  */
317                 if (vp) {
318                         vp2eap(tls_session, vp);
319                         pairfree(&vp);
320                 }
321
322                 rcode = RLM_MODULE_HANDLED;
323                 break;
324
325         default:
326                 DEBUG2("  PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
327                 rcode = RLM_MODULE_REJECT;
328                 break;
329         }
330
331         return rcode;
332 }
333
334
335 /*
336  *      Do post-proxy processing,
337  */
338 static int eappeap_postproxy(EAP_HANDLER *handler, void *data)
339 {
340         int rcode;
341         tls_session_t *tls_session = (tls_session_t *) data;
342
343         DEBUG2("  PEAP: Passing reply from proxy back into the tunnel.");
344
345         /*
346          *      If there was no EAP-Message in the reply packet, then
347          *      we know that we're supposed to re-run the "authenticate"
348          *      stage, in order to get the right kind of handling...
349          */
350
351         /*
352          *      Process the reply from the home server.
353          */
354         rcode = process_reply(handler, tls_session, handler->request,
355                               handler->request->proxy_reply);
356
357         /*
358          *      The proxy code uses the reply from the home server as
359          *      the basis for the reply to the NAS.  We don't want that,
360          *      so we toss it, after we've had our way with it.
361          */
362         pairfree(&handler->request->proxy_reply->vps);
363
364         switch (rcode) {
365         case RLM_MODULE_REJECT:
366                 DEBUG2("  PEAP: Reply was rejected");
367                 eaptls_fail(handler->eap_ds, 0);
368                 return 0;
369           
370         case RLM_MODULE_HANDLED:
371                 DEBUG2("  PEAP: Reply was handled");
372                 eaptls_request(handler->eap_ds, tls_session);
373                 return 1;
374
375         case RLM_MODULE_OK:
376                 DEBUG2("  PEAP: Reply was OK");
377                 eaptls_success(handler->eap_ds, 0);
378                 eaptls_gen_mppe_keys(&handler->request->reply->vps, 
379                                      tls_session->ssl,
380                                      "client EAP encryption");
381                 return 1;
382
383         default:
384                 DEBUG2("  PEAP: Reply was unknown.");
385                 break;
386         }
387
388         eaptls_fail(handler->eap_ds, 0);
389         return 0;
390 }
391
392
393 /*
394  *      Process the pseudo-EAP contents of the tunneled data.
395  */
396 int eappeap_process(EAP_HANDLER *handler, tls_session_t *tls_session)
397 {
398         int err;
399         peap_tunnel_t *t = tls_session->opaque;
400         REQUEST *fake;
401         VALUE_PAIR *vp;
402         int rcode = RLM_MODULE_REJECT;
403         const uint8_t *data;
404         unsigned int data_len;
405         unsigned char buffer[1024];
406 #ifndef NDEBUG
407         int i;
408 #endif
409
410         REQUEST *request = handler->request;
411         EAP_DS *eap_ds = handler->eap_ds;
412
413         /*
414          *      Grab the dirty data, and copy it to our buffer.
415          *
416          *      I *really* don't like these 'record_t' things...
417          */
418         data_len = record_minus(&tls_session->dirty_in, buffer, sizeof(buffer));
419         data = buffer;
420
421         /*
422          *      Write the data from the dirty buffer (i.e. packet
423          *      data) into the buffer which we will give to SSL for
424          *      decoding.
425          *
426          *      Some of this code COULD technically go into the TLS
427          *      module, in eaptls_process(), where it returns EAPTLS_OK.
428          *
429          *      Similarly, the writing of data to the SSL context could
430          *      go there, too...
431          */
432         BIO_write(tls_session->into_ssl, buffer, data_len);
433         record_init(&tls_session->clean_out);
434
435         /*
436          *      Read (and decrypt) the tunneled data from the SSL session,
437          *      and put it into the decrypted data buffer.
438          */
439         err = SSL_read(tls_session->ssl, tls_session->clean_out.data,
440                        sizeof(tls_session->clean_out.data));
441         if (err < 0) {
442                 /*
443                  *      FIXME: Call SSL_get_error() to see what went
444                  *      wrong.
445                  */
446                 radlog(L_INFO, "rlm_eap_peap: SSL_read Error");
447                 return RLM_MODULE_REJECT;
448         }
449
450         /*
451          *      If there's no data, maybe this is an ACK to an
452          *      MS-CHAP2-Success.
453          */     
454         if (err == 0) {
455                 /*
456                  *      FIXME: Call SSL_get_error() to see what went
457                  *      wrong.
458                  */
459                 radlog(L_INFO, "rlm_eap_peap: No data inside of the tunnel.");
460                 return RLM_MODULE_REJECT;
461         }
462  
463         data_len = tls_session->clean_out.used = err;
464         data = tls_session->clean_out.data;
465
466 #ifndef NDEBUG
467         if (debug_flag > 2) {
468                 for (i = 0; i < data_len; i++) {
469                         if ((i & 0x0f) == 0) printf("  PEAP tunnel data in %04x: ", i);
470                         
471                         printf("%02x ", data[i]);
472                         
473                         if ((i & 0x0f) == 0x0f) printf("\n");
474                 }
475                 if ((data_len & 0x0f) != 0) printf("\n");
476         }
477 #endif
478
479         if (!eapmessage_verify(data, data_len)) {
480                 return RLM_MODULE_REJECT;
481         }
482
483         DEBUG2("  rlm_eap_peap: Tunneled data is valid.");
484
485         /*
486          *      If we authenticated the user, then it's OK.
487          */
488         if (t->status == PEAP_STATUS_SENT_TLV_SUCCESS) {
489                 if (eappeap_check_tlv(data)) {
490                         DEBUG2("  rlm_eap_peap: Success");
491                         return RLM_MODULE_OK;
492                 }
493
494                 return RLM_MODULE_REJECT;
495
496         } else if (t->status == PEAP_STATUS_SENT_TLV_FAILURE) {
497                 DEBUG2("  rlm_eap_peap:  Had sent TLV failure, rejecting.");
498                 return RLM_MODULE_REJECT;
499         }
500
501         fake = request_alloc_fake(request);
502
503         rad_assert(fake->packet->vps == NULL);
504
505         fake->packet->vps = eap2vp(eap_ds, data, data_len);
506         if (!fake->packet->vps) {
507                 DEBUG2("  rlm_eap_peap: Unable to convert tunneled EAP packet to internal server data structures");
508                 return PW_AUTHENTICATION_REJECT;
509         }
510
511 #ifndef NDEBUG
512         if (debug_flag > 0) {
513           printf("  PEAP: Got tunneled EAP-Message\n");
514
515           for (vp = fake->packet->vps; vp != NULL; vp = vp->next) {
516             putchar('\t');vp_print(stdout, vp);putchar('\n');
517           }
518         }
519 #endif
520
521         /*
522          *      Tell the request that it's a fake one.
523          */
524         vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
525         if (vp) {
526                 pairadd(&fake->packet->vps, vp);
527         }
528
529         /*
530          *      Update other items in the REQUEST data structure.
531          */
532         if (!t->username) {
533                 /*
534                  *      There's no User-Name in the tunneled session,
535                  *      so we add one here, by pulling it out of the
536                  *      EAP-Identity packet.
537                  */
538                 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
539                         t->username = pairmake("User-Name", "", T_OP_EQ);
540                         rad_assert(t->username != NULL);
541                                 
542                         memcpy(t->username->strvalue, data+1, data_len - 1);
543                         t->username->length = data_len -1;
544                         t->username->strvalue[t->username->length] = 0;
545                         DEBUG2("  PEAP: Got tunneled identity of %s", t->username->strvalue);
546
547                         /*
548                          *      If there's a default EAP type,
549                          *      set it here.
550                          */
551                         if (t->default_eap_type != 0) {
552                           DEBUG2("  PEAP: Setting default EAP type for tunneled EAP session.");
553                           vp = pairmake("EAP-Type", "0", T_OP_EQ);
554                           vp->lvalue = t->default_eap_type;
555                           pairadd(&fake->config_items, vp);
556                         }
557                 }
558         } /* else there WAS a t->username */
559
560         if (t->username) {
561                 vp = paircopy(t->username);
562                 pairadd(&fake->packet->vps, vp);
563                 fake->username = pairfind(fake->packet->vps, PW_USER_NAME);
564         }
565
566         /*
567          *      Add the State attribute, too, if it exists.
568          */
569         if (t->state) {
570                 DEBUG2("  PEAP: Adding old state with %02x %02x",
571                        t->state->strvalue[0], t->state->strvalue[1]);
572                 vp = paircopy(t->state);
573                 if (vp) pairadd(&fake->packet->vps, vp);
574         }
575
576         /*
577          *      If this is set, we copy SOME of the request attributes
578          *      from outside of the tunnel to inside of the tunnel.
579          *
580          *      We copy ONLY those attributes which do NOT already
581          *      exist in the tunneled request.
582          *
583          *      This code is copied from ../rlm_eap_ttls/ttls.c
584          */
585         if (t->copy_request_to_tunnel) {
586                 VALUE_PAIR *copy;
587
588                 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
589                         /*
590                          *      The attribute is a server-side thingy,
591                          *      don't copy it.
592                          */
593                         if ((vp->attribute > 255) &&
594                             (((vp->attribute >> 16) & 0xffff) == 0)) {
595                                 continue;
596                         }
597
598                         /*
599                          *      The outside attribute is already in the
600                          *      tunnel, don't copy it.
601                          *
602                          *      This works for BOTH attributes which
603                          *      are originally in the tunneled request,
604                          *      AND attributes which are copied there
605                          *      from below.
606                          */
607                         if (pairfind(fake->packet->vps, vp->attribute)) {
608                                 continue;
609                         }
610
611                         /*
612                          *      Some attributes are handled specially.
613                          */
614                         switch (vp->attribute) {
615                                 /*
616                                  *      NEVER copy Message-Authenticator,
617                                  *      EAP-Message, or State.  They're
618                                  *      only for outside of the tunnel.
619                                  */
620                         case PW_USER_NAME:
621                         case PW_USER_PASSWORD:
622                         case PW_CHAP_PASSWORD:
623                         case PW_CHAP_CHALLENGE:
624                         case PW_PROXY_STATE:
625                         case PW_MESSAGE_AUTHENTICATOR:
626                         case PW_EAP_MESSAGE:
627                         case PW_STATE:
628                                 continue;
629                                 break;
630
631                                 /*
632                                  *      By default, copy it over.
633                                  */
634                         default:
635                                 break;
636                         }
637
638                         /*
639                          *      Don't copy from the head, we've already
640                          *      checked it.
641                          */
642                         copy = paircopy2(vp, vp->attribute);
643                         pairadd(&fake->packet->vps, copy);
644                 }
645         }
646
647 #ifndef NDEBUG
648         if (debug_flag > 0) {
649                 printf("  PEAP: Sending tunneled request\n");
650                 
651                 for (vp = fake->packet->vps; vp != NULL; vp = vp->next) {
652                         putchar('\t');vp_print(stdout, vp);putchar('\n');
653                 }
654         }
655 #endif
656
657         /*
658          *      Call authentication recursively, which will
659          *      do PAP, CHAP, MS-CHAP, etc.
660          */
661         rad_authenticate(fake);
662
663         /*
664          *      Note that we don't do *anything* with the reply
665          *      attributes.
666          */
667 #ifndef NDEBUG
668         if (debug_flag > 0) {
669                 printf("  PEAP: Got tunneled reply RADIUS code %d\n",
670                  fake->reply->code);
671                 
672                 for (vp = fake->reply->vps; vp != NULL; vp = vp->next) {
673                         putchar('\t');vp_print(stdout, vp);putchar('\n');
674                 }
675         }
676 #endif
677
678         /*
679          *      Decide what to do with the reply.
680          */
681         switch (fake->reply->code) {
682         case 0:                 /* No reply code, must be proxied... */
683                 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM);
684
685                 if (vp) {
686                         eap_tunnel_data_t *tunnel;
687
688
689                         /*
690                          *      The tunneled request was NOT handled,
691                          *      it has to be proxied.  This means that
692                          *      the "authenticate" stage was never
693                          *      performed.
694                          *
695                          *      If we are told to NOT proxy the
696                          *      tunneled request as EAP, then this
697                          *      means that we've got to decode it,
698                          *      which means that we MUST run the
699                          *      "authenticate" portion by hand, here.
700                          *
701                          *      Once the tunneled EAP session is ALMOST
702                          *      done, THEN we proxy it...
703                          */
704                         if (!t->proxy_tunneled_request_as_eap) {
705                                 int rcode;
706                                 
707                                 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
708                                 
709                                 /*
710                                  *      Hmm... should we check for
711                                  *      Auth-Type & EAP-Message here?
712                                  */
713
714                                 
715                                 /*
716                                  *      Run the EAP authentication.
717                                  */
718                                 DEBUG2("  PEAP: Calling authenticate in order to initiate tunneled EAP session.");
719                                 rcode = module_authenticate(PW_AUTHTYPE_EAP, fake);
720                                 if (rcode != RLM_MODULE_HANDLED) {
721                                         DEBUG2("  PEAP: Can't handle the return code %d", rcode);
722                                         rcode = RLM_MODULE_REJECT;
723                                         goto done;
724                                 }
725                                 
726                                 /*
727                                  *      The module decided it wasn't
728                                  *      done.  Handle it like normal.
729                                  */
730                                 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
731                                         DEBUG2("    PEAP: Cancelling proxy to realm %s until the tunneled EAP session has been established", vp->strvalue);
732                                         goto do_process;
733                                 }
734
735                                 /*
736                                  *      The module has decoded the
737                                  *      EAP-Message into another set
738                                  *      of attributes.
739                                  */
740                                 pairdelete(&fake->packet->vps,
741                                            PW_EAP_MESSAGE);
742                         }
743
744                         DEBUG2("  PEAP: Tunneled authentication will be proxied to %s", vp->strvalue);
745
746                         /*
747                          *      Tell the original request that it's going
748                          *      to be proxied.
749                          */
750                         pairmove2(&(request->config_items),
751                                   &(fake->config_items),
752                                   PW_PROXY_TO_REALM);
753
754                         /*
755                          *      Seed the proxy packet with the
756                          *      tunneled request.
757                          */
758                         rad_assert(request->proxy == NULL);
759                         request->proxy = fake->packet;
760                         fake->packet = NULL;
761
762                         /*
763                          *      Set up the callbacks for the tunnel
764                          */
765                         tunnel = rad_malloc(sizeof(*tunnel));
766                         memset(tunnel, 0, sizeof(*tunnel));
767
768                         tunnel->tls_session = tls_session;
769                         tunnel->callback = eappeap_postproxy;
770
771                         /*
772                          *      Associate the callback with the request.
773                          */
774                         rcode = request_data_add(request, 
775                                                  request->proxy,
776                                                  REQUEST_DATA_EAP_TUNNEL_CALLBACK,
777                                                  tunnel, free);
778                         rad_assert(rcode == 0);
779                         
780                         /*
781                          *      Didn't authenticate the packet, but
782                          *      we're proxying it.
783                          */
784                         rcode = RLM_MODULE_UPDATED;
785
786                 } else {
787                         DEBUG2("  PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
788                         rcode = RLM_MODULE_REJECT;
789                 }
790                 break;
791
792         default:
793         do_process:
794                 rcode = process_reply(handler, tls_session, request,
795                                       fake->reply);
796                 break;
797         }
798         
799  done:
800         request_free(&fake);
801         
802         return rcode;
803 }