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