Remove references to stdout, and use new debug_pair functions
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  *   Copyright 2003 Alan DeKok <aland@freeradius.org>
21  *   Copyright 2006 The FreeRADIUS server project
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include "eap_peap.h"
28
29 /*
30  *      Send protected EAP-Failure
31  *
32  *       Result-TLV = Failure
33  */
34 static int eappeap_failure(EAP_HANDLER *handler, tls_session_t *tls_session)
35 {
36         uint8_t tlv_packet[11];
37
38         DEBUG2("  rlm_eap_peap: FAILURE");
39
40         tlv_packet[0] = PW_EAP_REQUEST;
41         tlv_packet[1] = handler->eap_ds->response->id +1;
42         tlv_packet[2] = 0;
43         tlv_packet[3] = 11;     /* length of this packet */
44         tlv_packet[4] = PW_EAP_TLV;
45         tlv_packet[5] = 0x80;
46         tlv_packet[6] = EAP_TLV_ACK_RESULT;
47         tlv_packet[7] = 0;
48         tlv_packet[8] = 2;      /* length of the data portion */
49         tlv_packet[9] = 0;
50         tlv_packet[10] = EAP_TLV_FAILURE;
51
52         (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
53
54         /*
55          *      FIXME: Check the return code.
56          */
57         tls_handshake_send(tls_session);
58
59         return 1;
60 }
61
62
63 /*
64  *      Send protected EAP-Success
65  *
66  *       Result-TLV = Success
67  */
68 static int eappeap_success(EAP_HANDLER *handler, tls_session_t *tls_session)
69 {
70         uint8_t tlv_packet[11];
71
72         DEBUG2("  rlm_eap_peap: SUCCESS");
73
74         tlv_packet[0] = PW_EAP_REQUEST;
75         tlv_packet[1] = handler->eap_ds->response->id +1;
76         tlv_packet[2] = 0;
77         tlv_packet[3] = 11;     /* length of this packet */
78         tlv_packet[4] = PW_EAP_TLV;
79         tlv_packet[5] = 0x80;   /* mandatory AVP */
80         tlv_packet[6] = EAP_TLV_ACK_RESULT;
81         tlv_packet[7] = 0;
82         tlv_packet[8] = 2;      /* length of the data portion */
83         tlv_packet[9] = 0;
84         tlv_packet[10] = EAP_TLV_SUCCESS;
85
86         (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
87
88         /*
89          *      FIXME: Check the return code.
90          */
91         tls_handshake_send(tls_session);
92
93         return 1;
94 }
95
96
97 /*
98  *      Verify the tunneled EAP message.
99  */
100 static int eapmessage_verify(const uint8_t *data, unsigned int data_len)
101 {
102         const eap_packet_t *eap_packet = (const eap_packet_t *) data;
103         uint8_t eap_type;
104         char buffer[256];
105
106         if (!data || (data_len <= 1)) {
107                 return 0;
108         }
109
110         eap_type = *data;
111         switch (eap_type) {
112         case PW_EAP_IDENTITY:
113                 DEBUG2("  rlm_eap_peap: Identity - %*s",
114                        data_len - 1, data + 1);
115                 return 1;
116                 break;
117
118                 /*
119                  *      If the first byte of the packet is
120                  *      EAP-Response, and the EAP data is a TLV,
121                  *      then it looks OK...
122                  */
123         case PW_EAP_RESPONSE:
124                 if (eap_packet->data[0] == PW_EAP_TLV) {
125                         DEBUG2("  rlm_eap_peap: Received EAP-TLV response.");
126                         return 1;
127                 }
128                 DEBUG2("  rlm_eap_peap: Got something weird.");
129                 break;
130
131
132                 /*
133                  *      We normally do Microsoft MS-CHAPv2 (26), versus
134                  *      Cisco MS-CHAPv2 (29).
135                  */
136         case PW_EAP_MSCHAPV2:
137         default:
138                 DEBUG2("  rlm_eap_peap: EAP type %s",
139                        eaptype_type2name(eap_type,
140                                          buffer, sizeof(buffer)));
141                 return 1;
142                 break;
143         }
144
145         return 0;
146 }
147
148 /*
149  *      Convert a pseudo-EAP packet to a list of VALUE_PAIR's.
150  */
151 static VALUE_PAIR *eap2vp(EAP_DS *eap_ds,
152                           const uint8_t *data, size_t data_len)
153 {
154         size_t total;
155         VALUE_PAIR *vp = NULL, *head, **tail;
156
157         if (data_len > 65535) return NULL; /* paranoia */
158
159         vp = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
160         if (!vp) {
161                 DEBUG2("  rlm_eap_peap: Failure in creating VP");
162                 return NULL;
163         }
164
165         total = data_len;
166         if (total > 249) total = 249;
167
168         /*
169          *      Hand-build an EAP packet from the crap in PEAP version 0.
170          */
171         vp->vp_octets[0] = PW_EAP_RESPONSE;
172         vp->vp_octets[1] = eap_ds->response->id;
173         vp->vp_octets[2] = (data_len + EAP_HEADER_LEN) >> 8;
174         vp->vp_octets[3] = (data_len + EAP_HEADER_LEN) & 0xff;
175
176         memcpy(vp->vp_octets + EAP_HEADER_LEN, data, total);
177         vp->length = EAP_HEADER_LEN + total;
178
179         head = vp;
180         tail = &(vp->next);
181         while (total < data_len) {
182                 int vp_len;
183
184
185                 vp = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
186                 if (!vp) {
187                         DEBUG2("  rlm_eap_peap: Failure in creating VP");
188                         pairfree(&head);
189                         return NULL;
190                 }
191                 vp_len = (data_len - total);
192                 if (vp_len > 253) vp_len = 253;
193
194                 memcpy(vp->vp_octets, data + total, vp_len);
195                 vp->length = vp_len;
196                 
197                 total += vp_len;
198                 *tail = vp;
199                 tail = &(vp->next);
200         }
201
202         return head;
203 }
204
205
206 /*
207  *      Convert a list of VALUE_PAIR's to an EAP packet, through the
208  *      simple expedient of dumping the EAP message
209  */
210 static int vp2eap(tls_session_t *tls_session, VALUE_PAIR *vp)
211 {
212         /*
213          *      Skip the id, code, and length.  Just write the EAP
214          *      type & data to the client.
215          */
216 #ifndef NDEBUG
217         if ((debug_flag > 2) && fr_log_fp) {
218                 size_t i, total;
219                 VALUE_PAIR *this;
220
221                 total = 0;
222
223                 for (this = vp; this != NULL; this = this->next) {
224                         int start = 0;
225
226                         if (this == vp) start = EAP_HEADER_LEN;
227                         
228                         for (i = start; i < vp->length; i++) {
229                                 if ((total & 0x0f) == 0) fprintf(fr_log_fp, "  PEAP tunnel data out %04x: ", total);
230
231                                 fprintf(fr_log_fp, "%02x ", vp->vp_octets[i]);
232                                 
233                                 if ((total & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
234                                 total++;
235                         }
236                 }
237                 if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
238         }
239 #endif
240
241         /*
242          *      Send the EAP data, WITHOUT the header.
243          */
244         (tls_session->record_plus)(&tls_session->clean_in,
245                                    vp->vp_octets + EAP_HEADER_LEN,
246                                    vp->length - EAP_HEADER_LEN);
247         
248         /*
249          *      Send the rest of the EAP data.
250          */
251         for (vp = vp->next; vp != NULL; vp = vp->next) {
252                 (tls_session->record_plus)(&tls_session->clean_in,
253                                            vp->vp_octets, vp->length);
254         }
255
256         tls_handshake_send(tls_session);
257
258         return 1;
259 }
260
261
262 /*
263  *      See if there's a TLV in the response.
264  */
265 static int eappeap_check_tlv(const uint8_t *data)
266 {
267         const eap_packet_t *eap_packet = (const eap_packet_t *) data;
268
269         /*
270          *      Look for success or failure.
271          */
272         if ((eap_packet->code == PW_EAP_RESPONSE) &&
273             (eap_packet->data[0] == PW_EAP_TLV)) {
274                 if (data[10] == EAP_TLV_SUCCESS) {
275                         return 1;
276                 }
277
278                 if (data[10] == EAP_TLV_FAILURE) {
279                         DEBUG2("  rlm_eap_peap: Client rejected our response.  The password is probably incorrect.");
280                         return 0;
281                 }
282         }
283
284         return 0;
285 }
286
287
288 /*
289  *      Use a reply packet to determine what to do.
290  */
291 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
292                          UNUSED REQUEST *request, RADIUS_PACKET *reply)
293 {
294         int rcode = RLM_MODULE_REJECT;
295         VALUE_PAIR *vp;
296         peap_tunnel_t *t = tls_session->opaque;
297
298 #ifndef NDEBUG
299         if ((debug_flag > 0) && fr_log_fp) {
300                 fprintf(fr_log_fp, "  PEAP: Processing from tunneled session code %p %d\n",
301                        reply, reply->code);
302
303                 debug_pair_list(reply->vps);
304         }
305 #endif
306
307         switch (reply->code) {
308         case PW_AUTHENTICATION_ACK:
309                 DEBUG2("  PEAP: Tunneled authentication was successful.");
310                 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
311                 eappeap_success(handler, tls_session);
312                 rcode = RLM_MODULE_HANDLED;
313
314                 /*
315                  *      If we've been told to use the attributes from
316                  *      the reply, then do so.
317                  *
318                  *      WARNING: This may leak information about the
319                  *      tunneled user!
320                  */
321                 if (t->use_tunneled_reply) {
322                         DEBUG2("  Saving tunneled attributes for later");
323
324                         /*
325                          *      Clean up the tunneled reply.
326                          */
327                         pairdelete(&reply->vps, PW_PROXY_STATE);
328                         pairdelete(&reply->vps, PW_EAP_MESSAGE);
329                         pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR);
330
331                         t->accept_vps = reply->vps;
332                         reply->vps = NULL;
333                 }
334                 break;
335
336         case PW_AUTHENTICATION_REJECT:
337                 DEBUG2("  PEAP: Tunneled authentication was rejected.");
338                 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
339                 eappeap_failure(handler, tls_session);
340                 rcode = RLM_MODULE_HANDLED;
341                 break;
342
343         case PW_ACCESS_CHALLENGE:
344                 DEBUG2("  PEAP: Got tunneled Access-Challenge");
345
346                 /*
347                  *      Keep the State attribute, if necessary.
348                  *
349                  *      Get rid of the old State, too.
350                  */
351                 pairfree(&t->state);
352                 pairmove2(&t->state, &(reply->vps), PW_STATE);
353
354                 /*
355                  *      PEAP takes only EAP-Message attributes inside
356                  *      of the tunnel.  Any Reply-Message in the
357                  *      Access-Challenge is ignored.
358                  */
359                 vp = NULL;
360                 pairmove2(&vp, &(reply->vps), PW_EAP_MESSAGE);
361
362                 /*
363                  *      Handle EAP-MSCHAP-V2, where Access-Accept's
364                  *      from the home server may contain MS-CHAP-Success,
365                  *      which the module turns into challenges, so that
366                  *      the client may respond to the challenge with
367                  *      an "ack" packet.
368                  */
369                 if (t->home_access_accept && t->use_tunneled_reply) {
370                         DEBUG2("  Saving tunneled attributes for later");
371
372                         /*
373                          *      Clean up the tunneled reply.
374                          */
375                         pairdelete(&reply->vps, PW_PROXY_STATE);
376                         pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR);
377
378                         t->accept_vps = reply->vps;
379                         reply->vps = NULL;
380                 }
381
382                 /*
383                  *      Handle the ACK, by tunneling any necessary reply
384                  *      VP's back to the client.
385                  */
386                 if (vp) {
387                         vp2eap(tls_session, vp);
388                         pairfree(&vp);
389                 }
390
391                 rcode = RLM_MODULE_HANDLED;
392                 break;
393
394         default:
395                 DEBUG2("  PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
396                 rcode = RLM_MODULE_REJECT;
397                 break;
398         }
399
400         return rcode;
401 }
402
403
404 /*
405  *      Do post-proxy processing,
406  */
407 static int eappeap_postproxy(EAP_HANDLER *handler, void *data)
408 {
409         int rcode;
410         tls_session_t *tls_session = (tls_session_t *) data;
411         REQUEST *fake;
412
413         DEBUG2("  PEAP: Passing reply from proxy back into the tunnel.");
414
415         /*
416          *      If there was a fake request associated with the proxied
417          *      request, do more processing of it.
418          */
419         fake = (REQUEST *) request_data_get(handler->request,
420                                             handler->request->proxy,
421                                             REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
422
423         /*
424          *      Do the callback, if it exists, and if it was a success.
425          */
426         if (fake && (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
427                 REQUEST *request = handler->request;
428                 peap_tunnel_t *t = tls_session->opaque;
429
430                 t->home_access_accept = TRUE;
431
432                 /*
433                  *      Terrible hacks.
434                  */
435                 rad_assert(fake->packet == NULL);
436                 fake->packet = request->proxy;
437                 fake->packet->src_ipaddr = request->packet->src_ipaddr;
438                 request->proxy = NULL;
439
440                 rad_assert(fake->reply == NULL);
441                 fake->reply = request->proxy_reply;
442                 request->proxy_reply = NULL;
443
444                 /*
445                  *      Perform a post-auth stage, which will get the EAP
446                  *      handler, too...
447                  */
448                 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
449                 DEBUG2("  PEAP: Passing reply back for EAP-MS-CHAP-V2");
450                 rcode = module_post_proxy(0, fake);
451
452                 /*
453                  *      FIXME: If rcode returns fail, do something
454                  *      intelligent...
455                  */
456                 rcode = rad_postauth(fake);
457
458 #ifndef NDEBUG
459                 if ((debug_flag > 0) && fr_log_fp) {
460                         fprintf(fr_log_fp, "  PEAP: Final reply from tunneled session code %d\n",
461                                fake->reply->code);
462
463                         debug_pair_list(fake->reply->vps);
464                 }
465 #endif
466
467                 /*
468                  *      Terrible hacks.
469                  */
470                 request->proxy = fake->packet;
471                 fake->packet = NULL;
472                 request->proxy_reply = fake->reply;
473                 fake->reply = NULL;
474
475                 /*
476                  *      And we're done with this request.
477                  */
478
479                 switch (rcode) {
480                 case RLM_MODULE_FAIL:
481                         request_free(&fake);
482                         eaptls_fail(handler->eap_ds, 0);
483                         return 0;
484                         break;
485
486                 default:  /* Don't Do Anything */
487                         DEBUG2(" PEAP: Got reply %d",
488                                request->proxy_reply->code);
489                         break;
490                 }
491         }
492         request_free(&fake);    /* robust if fake == NULL */
493
494         /*
495          *      If there was no EAP-Message in the reply packet, then
496          *      we know that we're supposed to re-run the "authenticate"
497          *      stage, in order to get the right kind of handling...
498          */
499
500         /*
501          *      Process the reply from the home server.
502          */
503
504         rcode = process_reply(handler, tls_session, handler->request,
505                               handler->request->proxy_reply);
506
507         /*
508          *      The proxy code uses the reply from the home server as
509          *      the basis for the reply to the NAS.  We don't want that,
510          *      so we toss it, after we've had our way with it.
511          */
512         pairfree(&handler->request->proxy_reply->vps);
513
514         switch (rcode) {
515         case RLM_MODULE_REJECT:
516                 DEBUG2("  PEAP: Reply was rejected");
517                 eaptls_fail(handler->eap_ds, 0);
518                 return 0;
519
520         case RLM_MODULE_HANDLED:
521                 DEBUG2("  PEAP: Reply was handled");
522                 eaptls_request(handler->eap_ds, tls_session);
523                 return 1;
524
525         case RLM_MODULE_OK:
526                 DEBUG2("  PEAP: Reply was OK");
527                 eaptls_success(handler->eap_ds, 0);
528                 eaptls_gen_mppe_keys(&handler->request->reply->vps,
529                                      tls_session->ssl,
530                                      "client EAP encryption");
531                 return 1;
532
533         default:
534                 DEBUG2("  PEAP: Reply was unknown.");
535                 break;
536         }
537
538         eaptls_fail(handler->eap_ds, 0);
539         return 0;
540 }
541
542 /*
543  *      Free a request.
544  */
545 static void my_request_free(void *data)
546 {
547         REQUEST *request = (REQUEST *)data;
548
549         request_free(&request);
550 }
551
552
553 /*
554  *      Process the pseudo-EAP contents of the tunneled data.
555  */
556 int eappeap_process(EAP_HANDLER *handler, tls_session_t *tls_session)
557 {
558         int err;
559         peap_tunnel_t *t = tls_session->opaque;
560         REQUEST *fake;
561         VALUE_PAIR *vp;
562         int rcode = RLM_MODULE_REJECT;
563         const uint8_t   *data;
564         unsigned int data_len;
565 #ifndef NDEBUG
566         size_t i;
567 #endif
568
569         REQUEST *request = handler->request;
570         EAP_DS *eap_ds = handler->eap_ds;
571
572         /*
573          *      FIXME: if the SSL session says "want read", or
574          *      similar, leave the data in the clean_out buffer.  This
575          *      lets the application data be sent across multiple
576          *      fragments.
577          */
578         err = tls_handshake_recv(tls_session);
579         if (!err) {
580                 DEBUG2(" rlm_eap_peap: Failed in SSL");
581                 return RLM_MODULE_REJECT;
582         }
583
584         /*
585          *      Just look at the buffer directly, without doing
586          *      record_minus.  This lets us avoid another data copy.
587          */
588         data_len = tls_session->clean_out.used;
589         tls_session->clean_out.used = 0;
590         data = tls_session->clean_out.data;
591
592 #ifndef NDEBUG
593         if ((debug_flag > 2) && fr_log_fp) {
594                 for (i = 0; i < data_len; i++) {
595                         if ((i & 0x0f) == 0) fprintf(fr_log_fp, "  PEAP tunnel data in %04x: ", i);
596
597                         fprintf(fr_log_fp, "%02x ", data[i]);
598
599                         if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
600                 }
601                 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
602         }
603 #endif
604
605         if (!eapmessage_verify(data, data_len)) {
606                 DEBUG2("  rlm_eap_peap: Tunneled data is invalid.");
607                 return RLM_MODULE_REJECT;
608         }
609
610
611         /*
612          *      If we authenticated the user, then it's OK.
613          */
614         if (t->status == PEAP_STATUS_SENT_TLV_SUCCESS) {
615                 if (eappeap_check_tlv(data)) {
616                         DEBUG2("  rlm_eap_peap: Success");
617                         return RLM_MODULE_OK;
618                 }
619
620                 return RLM_MODULE_REJECT;
621
622         } else if (t->status == PEAP_STATUS_SENT_TLV_FAILURE) {
623                 DEBUG2("  rlm_eap_peap:  Had sent TLV failure.  User was rejected earlier in this session.");
624                 return RLM_MODULE_REJECT;
625         }
626
627         fake = request_alloc_fake(request);
628
629         rad_assert(fake->packet->vps == NULL);
630
631         fake->packet->vps = eap2vp(eap_ds, data, data_len);
632         if (!fake->packet->vps) {
633                 request_free(&fake);
634                 DEBUG2("  rlm_eap_peap: Unable to convert tunneled EAP packet to internal server data structures");
635                 return PW_AUTHENTICATION_REJECT;
636         }
637
638 #ifndef NDEBUG
639         if ((debug_flag > 0) && fr_log_fp) {
640                 fprintf(fr_log_fp, "  PEAP: Got tunneled EAP-Message\n");
641                 
642                 debug_pair_list(fake->packet->vps);
643         }
644 #endif
645
646         /*
647          *      Tell the request that it's a fake one.
648          */
649         vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
650         if (vp) {
651                 pairadd(&fake->packet->vps, vp);
652         }
653
654         /*
655          *      Update other items in the REQUEST data structure.
656          */
657         if (!t->username) {
658                 /*
659                  *      There's no User-Name in the tunneled session,
660                  *      so we add one here, by pulling it out of the
661                  *      EAP-Identity packet.
662                  */
663                 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
664                         t->username = pairmake("User-Name", "", T_OP_EQ);
665                         rad_assert(t->username != NULL);
666
667                         memcpy(t->username->vp_strvalue, data + 1, data_len - 1);
668                         t->username->length = data_len - 1;
669                         t->username->vp_strvalue[t->username->length] = 0;
670                         DEBUG2("  PEAP: Got tunneled identity of %s", t->username->vp_strvalue);
671
672                         /*
673                          *      If there's a default EAP type,
674                          *      set it here.
675                          */
676                         if (t->default_eap_type != 0) {
677                                 DEBUG2("  PEAP: Setting default EAP type for tunneled EAP session.");
678                                 vp = pairmake("EAP-Type", "0", T_OP_EQ);
679                                 vp->vp_integer = t->default_eap_type;
680                                 pairadd(&fake->config_items, vp);
681                         }
682                 }
683         } /* else there WAS a t->username */
684
685         if (t->username) {
686                 vp = paircopy(t->username);
687                 pairadd(&fake->packet->vps, vp);
688                 fake->username = pairfind(fake->packet->vps, PW_USER_NAME);
689                 DEBUG2("  PEAP: Setting User-Name to %s",
690                        fake->username->vp_strvalue);
691         }
692
693         /*
694          *      Add the State attribute, too, if it exists.
695          */
696         if (t->state) {
697                 vp = paircopy(t->state);
698                 if (vp) pairadd(&fake->packet->vps, vp);
699         }
700
701         /*
702          *      If this is set, we copy SOME of the request attributes
703          *      from outside of the tunnel to inside of the tunnel.
704          *
705          *      We copy ONLY those attributes which do NOT already
706          *      exist in the tunneled request.
707          *
708          *      This code is copied from ../rlm_eap_ttls/ttls.c
709          */
710         if (t->copy_request_to_tunnel) {
711                 VALUE_PAIR *copy;
712
713                 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
714                         /*
715                          *      The attribute is a server-side thingy,
716                          *      don't copy it.
717                          */
718                         if ((vp->attribute > 255) &&
719                             (((vp->attribute >> 16) & 0xffff) == 0)) {
720                                 continue;
721                         }
722
723                         /*
724                          *      The outside attribute is already in the
725                          *      tunnel, don't copy it.
726                          *
727                          *      This works for BOTH attributes which
728                          *      are originally in the tunneled request,
729                          *      AND attributes which are copied there
730                          *      from below.
731                          */
732                         if (pairfind(fake->packet->vps, vp->attribute)) {
733                                 continue;
734                         }
735
736                         /*
737                          *      Some attributes are handled specially.
738                          */
739                         switch (vp->attribute) {
740                                 /*
741                                  *      NEVER copy Message-Authenticator,
742                                  *      EAP-Message, or State.  They're
743                                  *      only for outside of the tunnel.
744                                  */
745                         case PW_USER_NAME:
746                         case PW_USER_PASSWORD:
747                         case PW_CHAP_PASSWORD:
748                         case PW_CHAP_CHALLENGE:
749                         case PW_PROXY_STATE:
750                         case PW_MESSAGE_AUTHENTICATOR:
751                         case PW_EAP_MESSAGE:
752                         case PW_STATE:
753                                 continue;
754                                 break;
755
756                                 /*
757                                  *      By default, copy it over.
758                                  */
759                         default:
760                                 break;
761                         }
762
763                         /*
764                          *      Don't copy from the head, we've already
765                          *      checked it.
766                          */
767                         copy = paircopy2(vp, vp->attribute);
768                         pairadd(&fake->packet->vps, copy);
769                 }
770         }
771
772         if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER)) != NULL) {
773                 fake->server = vp->vp_strvalue;
774
775         } else if (t->virtual_server) {
776                 fake->server = t->virtual_server;
777
778         } /* else fake->server == request->server */
779
780 #ifndef NDEBUG
781         if ((debug_flag > 0) && fr_log_fp) {
782                 fprintf(fr_log_fp, "  PEAP: Sending tunneled request\n");
783
784                 debug_pair_list(fake->packet->vps);
785
786                 fprintf(fr_log_fp, "server %s {\n", fake->server);
787         }
788 #endif
789
790         /*
791          *      Call authentication recursively, which will
792          *      do PAP, CHAP, MS-CHAP, etc.
793          */
794         rad_authenticate(fake);
795
796         /*
797          *      Note that we don't do *anything* with the reply
798          *      attributes.
799          */
800 #ifndef NDEBUG
801         if ((debug_flag > 0) && fr_log_fp) {
802                 fprintf(fr_log_fp, "} # server %s\n", fake->server);
803
804                 fprintf(fr_log_fp, "  PEAP: Got tunneled reply RADIUS code %d\n",
805                         fake->reply->code);
806                 
807                 debug_pair_list(fake->reply->vps);
808         }
809 #endif
810
811         /*
812          *      Decide what to do with the reply.
813          */
814         switch (fake->reply->code) {
815         case 0:                 /* No reply code, must be proxied... */
816                 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM);
817
818                 if (vp) {
819                         eap_tunnel_data_t *tunnel;
820
821                         /*
822                          *      The tunneled request was NOT handled,
823                          *      it has to be proxied.  This means that
824                          *      the "authenticate" stage was never
825                          *      performed.
826                          *
827                          *      If we are told to NOT proxy the
828                          *      tunneled request as EAP, then this
829                          *      means that we've got to decode it,
830                          *      which means that we MUST run the
831                          *      "authenticate" portion by hand, here.
832                          *
833                          *      Once the tunneled EAP session is ALMOST
834                          *      done, THEN we proxy it...
835                          */
836                         if (!t->proxy_tunneled_request_as_eap) {
837                                 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
838
839                                 /*
840                                  *      Hmm... should we check for
841                                  *      Auth-Type & EAP-Message here?
842                                  */
843
844
845                                 /*
846                                  *      Run the EAP authentication.
847                                  */
848                                 DEBUG2("  PEAP: Calling authenticate in order to initiate tunneled EAP session.");
849                                 rcode = module_authenticate(PW_AUTHTYPE_EAP, fake);
850                                 if (rcode == RLM_MODULE_OK) {
851                                         /*
852                                          *      Authentication succeeded! Rah!
853                                          */
854                                         fake->reply->code = PW_AUTHENTICATION_ACK;
855                                         goto do_process;
856                                 }
857
858                                 if (rcode != RLM_MODULE_HANDLED) {
859                                         DEBUG2("  PEAP: Can't handle the return code %d", rcode);
860                                         rcode = RLM_MODULE_REJECT;
861                                         goto done;
862                                 }
863
864                                 /*
865                                  *      The module decided it wasn't
866                                  *      done.  Handle it like normal.
867                                  */
868                                 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
869                                         DEBUG2("    PEAP: Cancelling proxy to realm %s until the tunneled EAP session has been established", vp->vp_strvalue);
870                                         goto do_process;
871                                 }
872
873                                 /*
874                                  *      The module has decoded the
875                                  *      EAP-Message into another set
876                                  *      of attributes.
877                                  */
878                                 pairdelete(&fake->packet->vps,
879                                            PW_EAP_MESSAGE);
880                         }
881
882                         DEBUG2("  PEAP: Tunneled authentication will be proxied to %s", vp->vp_strvalue);
883
884                         /*
885                          *      Tell the original request that it's going
886                          *      to be proxied.
887                          */
888                         pairmove2(&(request->config_items),
889                                   &(fake->config_items),
890                                   PW_PROXY_TO_REALM);
891
892                         /*
893                          *      Seed the proxy packet with the
894                          *      tunneled request.
895                          */
896                         rad_assert(request->proxy == NULL);
897                         request->proxy = fake->packet;
898                         fake->packet = NULL;
899                         rad_free(&fake->reply);
900                         fake->reply = NULL;
901
902                         /*
903                          *      Set up the callbacks for the tunnel
904                          */
905                         tunnel = rad_malloc(sizeof(*tunnel));
906                         memset(tunnel, 0, sizeof(*tunnel));
907
908                         tunnel->tls_session = tls_session;
909                         tunnel->callback = eappeap_postproxy;
910
911                         /*
912                          *      Associate the callback with the request.
913                          */
914                         rcode = request_data_add(request,
915                                                  request->proxy,
916                                                  REQUEST_DATA_EAP_TUNNEL_CALLBACK,
917                                                  tunnel, free);
918                         rad_assert(rcode == 0);
919
920                         /*
921                          *      We're not proxying it as EAP, so we've got
922                          *      to do the callback later.
923                          */
924                         if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
925                                 DEBUG2("  PEAP: Remembering to do EAP-MS-CHAP-V2 post-proxy.");
926
927                                 /*
928                                  *      rlm_eap.c has taken care of associating
929                                  *      the handler with the fake request.
930                                  *
931                                  *      So we associate the fake request with
932                                  *      this request.
933                                  */
934                                 rcode = request_data_add(request,
935                                                          request->proxy,
936                                                          REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
937                                                          fake, my_request_free);
938                                 rad_assert(rcode == 0);
939
940                                 /*
941                                  *      Do NOT free the fake request!
942                                  */
943                                 return RLM_MODULE_UPDATED;
944                         }
945
946                         /*
947                          *      Didn't authenticate the packet, but
948                          *      we're proxying it.
949                          */
950                         rcode = RLM_MODULE_UPDATED;
951
952                 } else {
953                         DEBUG2("  PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
954                         rcode = RLM_MODULE_REJECT;
955                 }
956                 break;
957
958         default:
959         do_process:
960                 rcode = process_reply(handler, tls_session, request,
961                                       fake->reply);
962                 break;
963         }
964
965  done:
966         request_free(&fake);
967
968         return rcode;
969 }