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