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