Updated debug message, in the hope that a little more text would make
[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 #ifndef NDEBUG
268         if (debug_flag > 0) {
269                 printf("  PEAP: Processing from tunneled session code %p %d\n",
270                        reply, reply->code);
271                 
272                 for (vp = reply->vps; vp != NULL; vp = vp->next) {
273                         putchar('\t');vp_print(stdout, vp);putchar('\n');
274                 }
275         }
276 #endif
277
278         switch (reply->code) {
279         case PW_AUTHENTICATION_ACK:
280                 DEBUG2("  PEAP: Tunneled authentication was successful.");
281                 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
282                 eappeap_success(handler, tls_session);
283                 rcode = RLM_MODULE_HANDLED;
284
285                 /*
286                  *      If we've been told to use the attributes from
287                  *      the reply, then do so.
288                  *
289                  *      WARNING: This may leak information about the
290                  *      tunneled user!
291                  */
292                 if (t->use_tunneled_reply) {
293                         /*
294                          *      Clean up the tunneled reply.
295                          */
296                         pairdelete(&reply->vps, PW_PROXY_STATE);
297                         pairdelete(&reply->vps, PW_EAP_MESSAGE);
298
299                         pairadd(&request->reply->vps, reply->vps);
300                         reply->vps = NULL;
301                 }
302                 break;
303
304         case PW_AUTHENTICATION_REJECT:
305                 DEBUG2("  PEAP: Tunneled authentication was rejected.");
306                 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
307                 eappeap_failure(handler, tls_session);
308                 rcode = RLM_MODULE_HANDLED;
309                 break;
310
311         case PW_ACCESS_CHALLENGE:
312                 DEBUG2("  PEAP: Got tunneled Access-Challenge");
313
314                 /*
315                  *      Keep the State attribute, if necessary.
316                  *
317                  *      Get rid of the old State, too.
318                  */
319                 pairfree(&t->state);
320                 pairmove2(&t->state, &(reply->vps), PW_STATE);
321
322                 /*
323                  *      PEAP takes only EAP-Message attributes inside
324                  *      of the tunnel.  Any Reply-Message in the
325                  *      Access-Challenge is ignored.
326                  */
327                 vp = NULL;
328                 pairmove2(&vp, &(reply->vps), PW_EAP_MESSAGE);
329
330                 /*
331                  *      Handle the ACK, by tunneling any necessary reply
332                  *      VP's back to the client.
333                  */
334                 if (vp) {
335                         vp2eap(tls_session, vp);
336                         pairfree(&vp);
337                 }
338
339                 rcode = RLM_MODULE_HANDLED;
340                 break;
341
342         default:
343                 DEBUG2("  PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
344                 rcode = RLM_MODULE_REJECT;
345                 break;
346         }
347
348         return rcode;
349 }
350
351
352 /*
353  *      Do post-proxy processing,
354  */
355 static int eappeap_postproxy(EAP_HANDLER *handler, void *data)
356 {
357         int rcode;
358         tls_session_t *tls_session = (tls_session_t *) data;
359         REQUEST *fake;
360
361         DEBUG2("  PEAP: Passing reply from proxy back into the tunnel.");
362
363         /*
364          *      If there was a fake request associated with the proxied
365          *      request, do more processing of it.
366          */
367         fake = (REQUEST *) request_data_get(handler->request,
368                                             handler->request->proxy,
369                                             REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
370         
371         /*
372          *      Do the callback, if it exists, and if it was a success.
373          */
374         if (fake && (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
375                 VALUE_PAIR *vp;
376                 REQUEST *request = handler->request;
377
378                 /*
379                  *      Terrible hacks.
380                  */
381                 rad_assert(fake->packet == NULL);
382                 fake->packet = request->proxy;
383                 request->proxy = NULL;
384
385                 rad_assert(fake->reply == NULL);
386                 fake->reply = request->proxy_reply;
387                 request->proxy_reply = NULL;
388
389                 /*
390                  *      Perform a post-auth stage, which will get the EAP
391                  *      handler, too...
392                  */
393                 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
394                 DEBUG2("  PEAP: Passing reply back for EAP-MS-CHAP-V2 %p %d",
395                        fake, fake->reply->code);
396                 rcode = module_post_proxy(fake);
397
398                 /*
399                  *      FIXME: If rcode returns fail, do something
400                  *      intelligent...
401                  */
402                 DEBUG2("  POST-PROXY %d", rcode);
403                 rcode = rad_postauth(fake);
404                 DEBUG2("  POST-AUTH %d", rcode);
405
406 #ifndef NDEBUG
407                 if (debug_flag > 0) {
408                         printf("  PEAP: Final reply from tunneled session code %d\n",
409                                fake->reply->code);
410                         
411                         for (vp = fake->reply->vps; vp != NULL; vp = vp->next) {
412                                 putchar('\t');vp_print(stdout, vp);putchar('\n');
413                         }
414                 }
415 #endif
416
417                 /*
418                  *      Terrible hacks.
419                  */
420                 request->proxy = fake->packet;
421                 fake->packet = NULL;
422                 request->proxy_reply = fake->reply;
423                 fake->reply = NULL;
424
425                 /*
426                  *      And we're done with this request.
427                  */
428
429                 switch (rcode) {
430                 case RLM_MODULE_FAIL:
431                         request_free(&fake);
432                         eaptls_fail(handler->eap_ds, 0);
433                         return 0;
434                         break;
435                         
436                 default:  /* Don't Do Anything */
437                         DEBUG2(" PEAP: Got reply %d",
438                                request->proxy_reply->code);
439                         break;
440                 }       
441         }
442         request_free(&fake);    /* robust if fake == NULL */
443
444         /*
445          *      If there was no EAP-Message in the reply packet, then
446          *      we know that we're supposed to re-run the "authenticate"
447          *      stage, in order to get the right kind of handling...
448          */
449
450         /*
451          *      Process the reply from the home server.
452          */
453
454         rcode = process_reply(handler, tls_session, handler->request,
455                               handler->request->proxy_reply);
456
457         /*
458          *      The proxy code uses the reply from the home server as
459          *      the basis for the reply to the NAS.  We don't want that,
460          *      so we toss it, after we've had our way with it.
461          */
462         pairfree(&handler->request->proxy_reply->vps);
463
464         switch (rcode) {
465         case RLM_MODULE_REJECT:
466                 DEBUG2("  PEAP: Reply was rejected");
467                 eaptls_fail(handler->eap_ds, 0);
468                 return 0;
469
470         case RLM_MODULE_HANDLED:
471                 DEBUG2("  PEAP: Reply was handled");
472                 eaptls_request(handler->eap_ds, tls_session);
473                 return 1;
474
475         case RLM_MODULE_OK:
476                 DEBUG2("  PEAP: Reply was OK");
477                 eaptls_success(handler->eap_ds, 0);
478                 eaptls_gen_mppe_keys(&handler->request->reply->vps,
479                                      tls_session->ssl,
480                                      "client EAP encryption");
481                 return 1;
482
483         default:
484                 DEBUG2("  PEAP: Reply was unknown.");
485                 break;
486         }
487
488         eaptls_fail(handler->eap_ds, 0);
489         return 0;
490 }
491
492 /*
493  *      Free a request.
494  */
495 static void my_request_free(void *data)
496 {
497         REQUEST *request = (REQUEST *)data;
498
499         request_free(&request);
500 }
501
502
503 /*
504  *      Process the pseudo-EAP contents of the tunneled data.
505  */
506 int eappeap_process(EAP_HANDLER *handler, tls_session_t *tls_session)
507 {
508         int err;
509         peap_tunnel_t *t = tls_session->opaque;
510         REQUEST *fake;
511         VALUE_PAIR *vp;
512         int rcode = RLM_MODULE_REJECT;
513         const uint8_t *data;
514         unsigned int data_len;
515         unsigned char buffer[1024];
516 #ifndef NDEBUG
517         int i;
518 #endif
519
520         REQUEST *request = handler->request;
521         EAP_DS *eap_ds = handler->eap_ds;
522
523         /*
524          *      Grab the dirty data, and copy it to our buffer.
525          *
526          *      I *really* don't like these 'record_t' things...
527          */
528         data_len = record_minus(&tls_session->dirty_in, buffer, sizeof(buffer));
529         data = buffer;
530
531         /*
532          *      Write the data from the dirty buffer (i.e. packet
533          *      data) into the buffer which we will give to SSL for
534          *      decoding.
535          *
536          *      Some of this code COULD technically go into the TLS
537          *      module, in eaptls_process(), where it returns EAPTLS_OK.
538          *
539          *      Similarly, the writing of data to the SSL context could
540          *      go there, too...
541          */
542         BIO_write(tls_session->into_ssl, buffer, data_len);
543         record_init(&tls_session->clean_out);
544
545         /*
546          *      Read (and decrypt) the tunneled data from the SSL session,
547          *      and put it into the decrypted data buffer.
548          */
549         err = SSL_read(tls_session->ssl, tls_session->clean_out.data,
550                        sizeof(tls_session->clean_out.data));
551         if (err < 0) {
552                 /*
553                  *      FIXME: Call SSL_get_error() to see what went
554                  *      wrong.
555                  */
556                 radlog(L_INFO, "rlm_eap_peap: SSL_read Error");
557                 return RLM_MODULE_REJECT;
558         }
559
560         /*
561          *      If there's no data, maybe this is an ACK to an
562          *      MS-CHAP2-Success.
563          */
564         if (err == 0) {
565                 /*
566                  *      FIXME: Call SSL_get_error() to see what went
567                  *      wrong.
568                  */
569                 radlog(L_INFO, "rlm_eap_peap: No data inside of the tunnel.");
570                 return RLM_MODULE_REJECT;
571         }
572
573         data_len = tls_session->clean_out.used = err;
574         data = tls_session->clean_out.data;
575
576 #ifndef NDEBUG
577         if (debug_flag > 2) {
578                 for (i = 0; i < data_len; i++) {
579                         if ((i & 0x0f) == 0) printf("  PEAP tunnel data in %04x: ", i);
580
581                         printf("%02x ", data[i]);
582
583                         if ((i & 0x0f) == 0x0f) printf("\n");
584                 }
585                 if ((data_len & 0x0f) != 0) printf("\n");
586         }
587 #endif
588
589         if (!eapmessage_verify(data, data_len)) {
590                 return RLM_MODULE_REJECT;
591         }
592
593         DEBUG2("  rlm_eap_peap: Tunneled data is valid.");
594
595         /*
596          *      If we authenticated the user, then it's OK.
597          */
598         if (t->status == PEAP_STATUS_SENT_TLV_SUCCESS) {
599                 if (eappeap_check_tlv(data)) {
600                         DEBUG2("  rlm_eap_peap: Success");
601                         return RLM_MODULE_OK;
602                 }
603
604                 return RLM_MODULE_REJECT;
605
606         } else if (t->status == PEAP_STATUS_SENT_TLV_FAILURE) {
607                 DEBUG2("  rlm_eap_peap:  Had sent TLV failure.  User was rejcted rejected earlier in this session.");
608                 return RLM_MODULE_REJECT;
609         }
610
611         fake = request_alloc_fake(request);
612
613         rad_assert(fake->packet->vps == NULL);
614
615         fake->packet->vps = eap2vp(eap_ds, data, data_len);
616         if (!fake->packet->vps) {
617                 DEBUG2("  rlm_eap_peap: Unable to convert tunneled EAP packet to internal server data structures");
618                 return PW_AUTHENTICATION_REJECT;
619         }
620
621 #ifndef NDEBUG
622         if (debug_flag > 0) {
623           printf("  PEAP: Got tunneled EAP-Message\n");
624
625           for (vp = fake->packet->vps; vp != NULL; vp = vp->next) {
626             putchar('\t');vp_print(stdout, vp);putchar('\n');
627           }
628         }
629 #endif
630
631         /*
632          *      Tell the request that it's a fake one.
633          */
634         vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
635         if (vp) {
636                 pairadd(&fake->packet->vps, vp);
637         }
638
639         /*
640          *      Update other items in the REQUEST data structure.
641          */
642         if (!t->username) {
643                 /*
644                  *      There's no User-Name in the tunneled session,
645                  *      so we add one here, by pulling it out of the
646                  *      EAP-Identity packet.
647                  */
648                 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
649                         t->username = pairmake("User-Name", "", T_OP_EQ);
650                         rad_assert(t->username != NULL);
651
652                         memcpy(t->username->strvalue, data + 1, data_len - 1);
653                         t->username->length = data_len - 1;
654                         t->username->strvalue[t->username->length] = 0;
655                         DEBUG2("  PEAP: Got tunneled identity of %s", t->username->strvalue);
656
657                         /*
658                          *      If there's a default EAP type,
659                          *      set it here.
660                          */
661                         if (t->default_eap_type != 0) {
662                                 DEBUG2("  PEAP: Setting default EAP type for tunneled EAP session.");
663                                 vp = pairmake("EAP-Type", "0", T_OP_EQ);
664                                 vp->lvalue = t->default_eap_type;
665                                 pairadd(&fake->config_items, vp);
666                         }
667                 }
668         } /* else there WAS a t->username */
669
670         if (t->username) {
671                 vp = paircopy(t->username);
672                 pairadd(&fake->packet->vps, vp);
673                 fake->username = pairfind(fake->packet->vps, PW_USER_NAME);
674                 DEBUG2("  PEAP: Setting User-Name to %s",
675                        fake->username->strvalue);
676         }
677
678         /*
679          *      Add the State attribute, too, if it exists.
680          */
681         if (t->state) {
682                 DEBUG2("  PEAP: Adding old state with %02x %02x",
683                        t->state->strvalue[0], t->state->strvalue[1]);
684                 vp = paircopy(t->state);
685                 if (vp) pairadd(&fake->packet->vps, vp);
686         }
687
688         /*
689          *      If this is set, we copy SOME of the request attributes
690          *      from outside of the tunnel to inside of the tunnel.
691          *
692          *      We copy ONLY those attributes which do NOT already
693          *      exist in the tunneled request.
694          *
695          *      This code is copied from ../rlm_eap_ttls/ttls.c
696          */
697         if (t->copy_request_to_tunnel) {
698                 VALUE_PAIR *copy;
699
700                 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
701                         /*
702                          *      The attribute is a server-side thingy,
703                          *      don't copy it.
704                          */
705                         if ((vp->attribute > 255) &&
706                             (((vp->attribute >> 16) & 0xffff) == 0)) {
707                                 continue;
708                         }
709
710                         /*
711                          *      The outside attribute is already in the
712                          *      tunnel, don't copy it.
713                          *
714                          *      This works for BOTH attributes which
715                          *      are originally in the tunneled request,
716                          *      AND attributes which are copied there
717                          *      from below.
718                          */
719                         if (pairfind(fake->packet->vps, vp->attribute)) {
720                                 continue;
721                         }
722
723                         /*
724                          *      Some attributes are handled specially.
725                          */
726                         switch (vp->attribute) {
727                                 /*
728                                  *      NEVER copy Message-Authenticator,
729                                  *      EAP-Message, or State.  They're
730                                  *      only for outside of the tunnel.
731                                  */
732                         case PW_USER_NAME:
733                         case PW_USER_PASSWORD:
734                         case PW_CHAP_PASSWORD:
735                         case PW_CHAP_CHALLENGE:
736                         case PW_PROXY_STATE:
737                         case PW_MESSAGE_AUTHENTICATOR:
738                         case PW_EAP_MESSAGE:
739                         case PW_STATE:
740                                 continue;
741                                 break;
742
743                                 /*
744                                  *      By default, copy it over.
745                                  */
746                         default:
747                                 break;
748                         }
749
750                         /*
751                          *      Don't copy from the head, we've already
752                          *      checked it.
753                          */
754                         copy = paircopy2(vp, vp->attribute);
755                         pairadd(&fake->packet->vps, copy);
756                 }
757         }
758
759 #ifndef NDEBUG
760         if (debug_flag > 0) {
761                 printf("  PEAP: Sending tunneled request\n");
762
763                 for (vp = fake->packet->vps; vp != NULL; vp = vp->next) {
764                         putchar('\t');vp_print(stdout, vp);putchar('\n');
765                 }
766         }
767 #endif
768
769         /*
770          *      Call authentication recursively, which will
771          *      do PAP, CHAP, MS-CHAP, etc.
772          */
773         rad_authenticate(fake);
774
775         /*
776          *      Note that we don't do *anything* with the reply
777          *      attributes.
778          */
779 #ifndef NDEBUG
780         if (debug_flag > 0) {
781                 printf("  PEAP: Got tunneled reply RADIUS code %d\n",
782                  fake->reply->code);
783
784                 for (vp = fake->reply->vps; vp != NULL; vp = vp->next) {
785                         putchar('\t');vp_print(stdout, vp);putchar('\n');
786                 }
787         }
788 #endif
789
790         /*
791          *      Decide what to do with the reply.
792          */
793         switch (fake->reply->code) {
794         case 0:                 /* No reply code, must be proxied... */
795                 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM);
796
797                 if (vp) {
798                         eap_tunnel_data_t *tunnel;
799
800                         /*
801                          *      The tunneled request was NOT handled,
802                          *      it has to be proxied.  This means that
803                          *      the "authenticate" stage was never
804                          *      performed.
805                          *
806                          *      If we are told to NOT proxy the
807                          *      tunneled request as EAP, then this
808                          *      means that we've got to decode it,
809                          *      which means that we MUST run the
810                          *      "authenticate" portion by hand, here.
811                          *
812                          *      Once the tunneled EAP session is ALMOST
813                          *      done, THEN we proxy it...
814                          */
815                         if (!t->proxy_tunneled_request_as_eap) {
816                                 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
817
818                                 /*
819                                  *      Hmm... should we check for
820                                  *      Auth-Type & EAP-Message here?
821                                  */
822
823
824                                 /*
825                                  *      Run the EAP authentication.
826                                  */
827                                 DEBUG2("  PEAP: Calling authenticate in order to initiate tunneled EAP session.");
828                                 rcode = module_authenticate(PW_AUTHTYPE_EAP, fake);
829                                 if (rcode == RLM_MODULE_OK) {
830                                         /*
831                                          *      Authentication succeeded! Rah!
832                                          */
833                                         fake->reply->code = PW_AUTHENTICATION_ACK;
834                                         goto do_process;
835                                 }
836
837                                 if (rcode != RLM_MODULE_HANDLED) {
838                                         DEBUG2("  PEAP: Can't handle the return code %d", rcode);
839                                         rcode = RLM_MODULE_REJECT;
840                                         goto done;
841                                 }
842
843                                 /*
844                                  *      The module decided it wasn't
845                                  *      done.  Handle it like normal.
846                                  */
847                                 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
848                                         DEBUG2("    PEAP: Cancelling proxy to realm %s until the tunneled EAP session has been established", vp->strvalue);
849                                         goto do_process;
850                                 }
851
852                                 /*
853                                  *      The module has decoded the
854                                  *      EAP-Message into another set
855                                  *      of attributes.
856                                  */
857                                 pairdelete(&fake->packet->vps,
858                                            PW_EAP_MESSAGE);
859                         }
860
861                         DEBUG2("  PEAP: Tunneled authentication will be proxied to %s", vp->strvalue);
862
863                         /*
864                          *      Tell the original request that it's going
865                          *      to be proxied.
866                          */
867                         pairmove2(&(request->config_items),
868                                   &(fake->config_items),
869                                   PW_PROXY_TO_REALM);
870
871                         /*
872                          *      Seed the proxy packet with the
873                          *      tunneled request.
874                          */
875                         rad_assert(request->proxy == NULL);
876                         request->proxy = fake->packet;
877                         fake->packet = NULL;
878                         rad_free(&fake->reply);
879                         fake->reply = NULL;
880
881                         /*
882                          *      Set up the callbacks for the tunnel
883                          */
884                         tunnel = rad_malloc(sizeof(*tunnel));
885                         memset(tunnel, 0, sizeof(*tunnel));
886
887                         tunnel->tls_session = tls_session;
888                         tunnel->callback = eappeap_postproxy;
889
890                         /*
891                          *      Associate the callback with the request.
892                          */
893                         rcode = request_data_add(request,
894                                                  request->proxy,
895                                                  REQUEST_DATA_EAP_TUNNEL_CALLBACK,
896                                                  tunnel, free);
897                         rad_assert(rcode == 0);
898
899                         /*
900                          *      We're not proxying it as EAP, so we've got
901                          *      to do the callback later.
902                          */
903                         if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
904                                 DEBUG2("  PEAP: Remembering to do EAP-MS-CHAP-V2 post-proxy.");
905
906                                 /*
907                                  *      rlm_eap.c has taken care of associating
908                                  *      the handler with the fake request.
909                                  *
910                                  *      So we associate the fake request with
911                                  *      this request.
912                                  */
913                                 rcode = request_data_add(request,
914                                                          request->proxy,
915                                                          REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
916                                                          fake, my_request_free);
917                                 rad_assert(rcode == 0);
918                                 
919                                 /*
920                                  *      Do NOT free the fake request!
921                                  */
922                                 return RLM_MODULE_UPDATED;
923                         }
924
925                         /*
926                          *      Didn't authenticate the packet, but
927                          *      we're proxying it.
928                          */
929                         rcode = RLM_MODULE_UPDATED;
930
931                 } else {
932                         DEBUG2("  PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
933                         rcode = RLM_MODULE_REJECT;
934                 }
935                 break;
936
937         default:
938         do_process:
939                 rcode = process_reply(handler, tls_session, request,
940                                       fake->reply);
941                 break;
942         }
943
944  done:
945         request_free(&fake);
946
947         return rcode;
948 }