Signed / unsigned fixes and function prototypes
[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(request, 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(request, 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(handler->request, 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(REQUEST *request, 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(request, 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(request, 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 const char *peap_state(peap_tunnel_t *t)
595 {
596         switch (t->status) {
597                 case PEAP_STATUS_TUNNEL_ESTABLISHED:
598                         return "TUNNEL ESTABLISHED";
599                 case PEAP_STATUS_INNER_IDENTITY_REQ_SENT:
600                         return "WAITING FOR INNER IDENTITY";
601                 case PEAP_STATUS_SENT_TLV_SUCCESS:
602                         return "send tlv success";
603                 case PEAP_STATUS_SENT_TLV_FAILURE:
604                         return "send tlv failure";
605                 case PEAP_STATUS_PHASE2_INIT:
606                         return "phase2_init";
607                 case PEAP_STATUS_PHASE2:
608                         return "phase2";
609                 default:
610                         break;
611         }
612         return "?";
613 }
614
615 static void print_tunneled_data(const uint8_t *data, size_t data_len)
616 {
617         size_t i;
618
619         if ((debug_flag > 2) && fr_log_fp) {
620                 for (i = 0; i < data_len; i++) {
621                   if ((i & 0x0f) == 0) fprintf(fr_log_fp, "  PEAP tunnel data in %02x: ", (int) i);
622                         
623                         fprintf(fr_log_fp, "%02x ", data[i]);
624                         
625                         if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
626                 }
627                 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
628         }
629 }
630
631
632 /*
633  *      Process the pseudo-EAP contents of the tunneled data.
634  */
635 int eappeap_process(EAP_HANDLER *handler, tls_session_t *tls_session)
636 {
637         peap_tunnel_t *t = tls_session->opaque;
638         REQUEST *fake;
639         VALUE_PAIR *vp;
640         int rcode = RLM_MODULE_REJECT;
641         const uint8_t   *data;
642         unsigned int data_len;
643
644         REQUEST *request = handler->request;
645         EAP_DS *eap_ds = handler->eap_ds;
646
647         /*
648          *      Just look at the buffer directly, without doing
649          *      record_minus.  This lets us avoid another data copy.
650          */
651         data_len = tls_session->clean_out.used;
652         tls_session->clean_out.used = 0;
653         data = tls_session->clean_out.data;
654
655         RDEBUG2("Peap state %s", peap_state(t));
656
657         if ((t->status != PEAP_STATUS_TUNNEL_ESTABLISHED) &&
658             !eapmessage_verify(request, data, data_len)) {
659                 RDEBUG2("FAILED processing PEAP: Tunneled data is invalid.");
660                 if (debug_flag > 2) print_tunneled_data(data, data_len);
661                 return RLM_MODULE_REJECT;
662         }
663
664         switch (t->status) {
665         case PEAP_STATUS_TUNNEL_ESTABLISHED:
666                 /* FIXME: should be no data in the buffer here, check & assert? */
667                 
668                 if (SSL_session_reused(tls_session->ssl)) {
669                         RDEBUG2("Skipping Phase2 because of session resumption");
670                         t->session_resumption_state = PEAP_RESUMPTION_YES;
671                         
672                         /* we're good, send success TLV */
673                         t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
674                         eappeap_success(handler, tls_session);
675                         
676                 } else {
677                         /* send an identity request */
678                         t->session_resumption_state = PEAP_RESUMPTION_NO;
679                         t->status = PEAP_STATUS_INNER_IDENTITY_REQ_SENT;
680                         eappeap_identity(handler, tls_session);
681                 }
682                 return RLM_MODULE_HANDLED;
683
684         case PEAP_STATUS_INNER_IDENTITY_REQ_SENT:
685                 /* we're expecting an identity response */
686                 if (data[0] != PW_EAP_IDENTITY) {
687                         RDEBUG("Expected EAP-Identity, got something else.");
688                         return RLM_MODULE_REJECT;
689                 }
690
691                 if (data_len >= sizeof(t->username->vp_strvalue)) {
692                         RDEBUG("EAP-Identity is too long");
693                         return RLM_MODULE_REJECT;
694                 }
695                 
696                 /*
697                  *      Save it for later.
698                  */
699                 t->username = pairmake("User-Name", "", T_OP_EQ);
700                 rad_assert(t->username != NULL);
701                 
702                 memcpy(t->username->vp_strvalue, data + 1, data_len - 1);
703                 t->username->length = data_len - 1;
704                 t->username->vp_strvalue[t->username->length] = 0;
705                 RDEBUG("Got inner identity '%s'", t->username->vp_strvalue);
706                 
707                 t->status = PEAP_STATUS_PHASE2_INIT;
708                 break;
709
710         /*
711          *      If we authenticated the user, then it's OK.
712          */
713         case PEAP_STATUS_SENT_TLV_SUCCESS:
714                 if (eappeap_check_tlv(request, data)) {
715                         RDEBUG2("Success");
716                         return RLM_MODULE_OK;
717                 }
718
719                 /*
720                  *      Otherwise, the client rejected the session
721                  *      resumption.  If the session is being re-used,
722                  *      we need to do a full authentication.
723                  *
724                  *      We do this by sending an EAP-Identity request
725                  *      inside of the PEAP tunnel.
726                  */
727                 if (t->session_resumption_state == PEAP_RESUMPTION_YES) {
728                         RDEBUG2("Client rejected session resumption.  Re-starting full authentication");
729                         
730                         /*
731                          *      Mark session resumption status.
732                          */
733                         t->status = PEAP_STATUS_INNER_IDENTITY_REQ_SENT;
734                         t->session_resumption_state = PEAP_RESUMPTION_NO;
735                         
736                         eappeap_identity(handler, tls_session);
737                         return RLM_MODULE_HANDLED;
738                 }
739
740                 RDEBUG2("We sent a success, but received something weird in return.");
741                 return RLM_MODULE_REJECT;
742
743         /*
744          *      Damned if I know why the clients continue sending EAP
745          *      packets after we told them to f*ck off.
746          */
747         case PEAP_STATUS_SENT_TLV_FAILURE:
748                 RDEBUG(" The users session was previously rejected: returning reject (again.)");
749                 RDEBUG(" *** This means you need to read the PREVIOUS messages in the debug output");
750                 RDEBUG(" *** to find out the reason why the user was rejected.");
751                 RDEBUG(" *** Look for \"reject\" or \"fail\".  Those earlier messages will tell you.");
752                 RDEBUG(" *** what went wrong, and how to fix the problem.");
753                 return RLM_MODULE_REJECT;
754
755                 case PEAP_STATUS_PHASE2_INIT:
756                         RDEBUG("In state machine in phase2 init?");
757
758                 case PEAP_STATUS_PHASE2:
759                         break;
760
761                 default:
762                         RDEBUG2("Unhandled state in peap");
763                         return RLM_MODULE_REJECT;
764         }
765
766         fake = request_alloc_fake(request);
767
768         rad_assert(fake->packet->vps == NULL);
769
770         switch (t->status) {
771                 /*
772                  *      If we're in PHASE2_INIT, the phase2 method hasn't been
773                  *      sent an Identity packet yet; do so from the stored
774                  *      username and this will kick off the phase2 eap method
775                  */
776                 
777         case PEAP_STATUS_PHASE2_INIT: {
778                 int len = t->username->length + EAP_HEADER_LEN + 1;
779
780                 t->status = PEAP_STATUS_PHASE2;
781
782                 vp = paircreate(PW_EAP_MESSAGE, 0, PW_TYPE_OCTETS);
783
784                 vp->vp_octets[0] = PW_EAP_RESPONSE;
785                 vp->vp_octets[1] = eap_ds->response->id;
786                 vp->vp_octets[2] = (len >> 8) & 0xff;
787                 vp->vp_octets[3] = len & 0xff;
788                 vp->vp_octets[4] = PW_EAP_IDENTITY;
789
790                 memcpy(vp->vp_octets + EAP_HEADER_LEN + 1, t->username->vp_strvalue, t->username->length);
791                 vp->length = len;
792
793                 pairadd(&fake->packet->vps, vp);
794
795                 if (t->default_eap_type != 0) {
796                         RDEBUG2("Setting default EAP type for tunneled EAP session.");
797                         vp = pairmake("EAP-Type", "0", T_OP_EQ);
798                         vp->vp_integer = t->default_eap_type;
799                         pairadd(&fake->config_items, vp);
800                 }
801                 break; }
802
803         case PEAP_STATUS_PHASE2:
804                 fake->packet->vps = eap2vp(request, eap_ds, data, data_len);
805                 if (!fake->packet->vps) {
806                         request_free(&fake);
807                         RDEBUG2("Unable to convert tunneled EAP packet to internal server data structures");
808                         return PW_AUTHENTICATION_REJECT;
809                 }
810                 break;
811
812         default:
813                 RDEBUG("Invalid state change in PEAP.");
814                 return PW_AUTHENTICATION_REJECT;
815         }
816
817         if ((debug_flag > 0) && fr_log_fp) {
818                 RDEBUG("Got tunneled request");
819                 
820                 debug_pair_list(fake->packet->vps);
821
822                 fprintf(fr_log_fp, "server %s {\n",
823                         (fake->server == NULL) ? "" : fake->server);
824         }
825
826         /*
827          *      Tell the request that it's a fake one.
828          */
829         vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
830         if (vp) {
831                 pairadd(&fake->packet->vps, vp);
832         }
833
834         /*
835          *      Update other items in the REQUEST data structure.
836          */
837         if (!t->username) {
838                 /*
839                  *      There's no User-Name in the tunneled session,
840                  *      so we add one here, by pulling it out of the
841                  *      EAP-Identity packet.
842                  */
843                 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
844                         t->username = pairmake("User-Name", "", T_OP_EQ);
845                         rad_assert(t->username != NULL);
846
847                         memcpy(t->username->vp_strvalue, data + 1, data_len - 1);
848                         t->username->length = data_len - 1;
849                         t->username->vp_strvalue[t->username->length] = 0;
850                         DEBUG2("  PEAP: Got tunneled identity of %s", t->username->vp_strvalue);
851
852                         /*
853                          *      If there's a default EAP type,
854                          *      set it here.
855                          */
856                         if (t->default_eap_type != 0) {
857                                 DEBUG2("  PEAP: Setting default EAP type for tunneled EAP session.");
858                                 vp = pairmake("EAP-Type", "0", T_OP_EQ);
859                                 vp->vp_integer = t->default_eap_type;
860                                 pairadd(&fake->config_items, vp);
861                         }
862                 }
863         } /* else there WAS a t->username */
864
865         if (t->username) {
866                 vp = paircopy(t->username);
867                 pairadd(&fake->packet->vps, vp);
868                 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0);
869                 DEBUG2("  PEAP: Setting User-Name to %s",
870                        fake->username->vp_strvalue);
871         }
872
873         /*
874          *      Add the State attribute, too, if it exists.
875          */
876         if (t->state) {
877                 vp = paircopy(t->state);
878                 if (vp) pairadd(&fake->packet->vps, vp);
879         }
880
881         /*
882          *      If this is set, we copy SOME of the request attributes
883          *      from outside of the tunnel to inside of the tunnel.
884          *
885          *      We copy ONLY those attributes which do NOT already
886          *      exist in the tunneled request.
887          *
888          *      This code is copied from ../rlm_eap_ttls/ttls.c
889          */
890         if (t->copy_request_to_tunnel) {
891                 VALUE_PAIR *copy;
892
893                 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
894                         /*
895                          *      The attribute is a server-side thingy,
896                          *      don't copy it.
897                          */
898                         if ((vp->attribute > 255) &&
899                             (vp->vendor == 0)) {
900                                 continue;
901                         }
902
903                         /*
904                          *      The outside attribute is already in the
905                          *      tunnel, don't copy it.
906                          *
907                          *      This works for BOTH attributes which
908                          *      are originally in the tunneled request,
909                          *      AND attributes which are copied there
910                          *      from below.
911                          */
912                         if (pairfind(fake->packet->vps, vp->attribute, vp->vendor)) {
913                                 continue;
914                         }
915
916                         /*
917                          *      Some attributes are handled specially.
918                          */
919                         switch (vp->attribute) {
920                                 /*
921                                  *      NEVER copy Message-Authenticator,
922                                  *      EAP-Message, or State.  They're
923                                  *      only for outside of the tunnel.
924                                  */
925                         case PW_USER_NAME:
926                         case PW_USER_PASSWORD:
927                         case PW_CHAP_PASSWORD:
928                         case PW_CHAP_CHALLENGE:
929                         case PW_PROXY_STATE:
930                         case PW_MESSAGE_AUTHENTICATOR:
931                         case PW_EAP_MESSAGE:
932                         case PW_STATE:
933                                 continue;
934                                 break;
935
936                                 /*
937                                  *      By default, copy it over.
938                                  */
939                         default:
940                                 break;
941                         }
942
943                         /*
944                          *      Don't copy from the head, we've already
945                          *      checked it.
946                          */
947                         copy = paircopy2(vp, vp->attribute, vp->vendor);
948                         pairadd(&fake->packet->vps, copy);
949                 }
950         }
951
952         if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0)) != NULL) {
953                 fake->server = vp->vp_strvalue;
954
955         } else if (t->virtual_server) {
956                 fake->server = t->virtual_server;
957
958         } /* else fake->server == request->server */
959
960         if ((debug_flag > 0) && fr_log_fp) {
961                 fprintf(fr_log_fp, "Sending tunneled request\n");
962
963                 debug_pair_list(fake->packet->vps);
964
965                 fprintf(fr_log_fp, "server %s {\n",
966                         (fake->server == NULL) ? "" : fake->server);
967         }
968
969         /*
970          *      Call authentication recursively, which will
971          *      do PAP, CHAP, MS-CHAP, etc.
972          */
973         rad_authenticate(fake);
974
975         /*
976          *      Note that we don't do *anything* with the reply
977          *      attributes.
978          */
979         if ((debug_flag > 0) && fr_log_fp) {
980                 fprintf(fr_log_fp, "} # server %s\n",
981                         (fake->server == NULL) ? "" : fake->server);
982
983                 RDEBUG("Got tunneled reply code %d", fake->reply->code);
984                 
985                 debug_pair_list(fake->reply->vps);
986         }
987
988         /*
989          *      Decide what to do with the reply.
990          */
991         switch (fake->reply->code) {
992         case 0:                 /* No reply code, must be proxied... */
993 #ifdef WITH_PROXY
994                 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM, 0);
995
996                 if (vp) {
997                         eap_tunnel_data_t *tunnel;
998
999                         /*
1000                          *      The tunneled request was NOT handled,
1001                          *      it has to be proxied.  This means that
1002                          *      the "authenticate" stage was never
1003                          *      performed.
1004                          *
1005                          *      If we are told to NOT proxy the
1006                          *      tunneled request as EAP, then this
1007                          *      means that we've got to decode it,
1008                          *      which means that we MUST run the
1009                          *      "authenticate" portion by hand, here.
1010                          *
1011                          *      Once the tunneled EAP session is ALMOST
1012                          *      done, THEN we proxy it...
1013                          */
1014                         if (!t->proxy_tunneled_request_as_eap) {
1015                                 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
1016
1017                                 /*
1018                                  *      Hmm... should we check for
1019                                  *      Auth-Type & EAP-Message here?
1020                                  */
1021
1022
1023                                 /*
1024                                  *      Run the EAP authentication.
1025                                  */
1026                                 DEBUG2("  PEAP: Calling authenticate in order to initiate tunneled EAP session.");
1027                                 rcode = module_authenticate(PW_AUTHTYPE_EAP, fake);
1028                                 if (rcode == RLM_MODULE_OK) {
1029                                         /*
1030                                          *      Authentication succeeded! Rah!
1031                                          */
1032                                         fake->reply->code = PW_AUTHENTICATION_ACK;
1033                                         goto do_process;
1034                                 }
1035
1036                                 if (rcode != RLM_MODULE_HANDLED) {
1037                                         DEBUG2("  PEAP: Can't handle the return code %d", rcode);
1038                                         rcode = RLM_MODULE_REJECT;
1039                                         goto done;
1040                                 }
1041
1042                                 /*
1043                                  *      The module decided it wasn't
1044                                  *      done.  Handle it like normal.
1045                                  */
1046                                 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
1047                                         DEBUG2("    PEAP: Cancelling proxy to realm %s until the tunneled EAP session has been established", vp->vp_strvalue);
1048                                         goto do_process;
1049                                 }
1050
1051                                 /*
1052                                  *      The module has decoded the
1053                                  *      EAP-Message into another set
1054                                  *      of attributes.
1055                                  */
1056                                 pairdelete(&fake->packet->vps,
1057                                            PW_EAP_MESSAGE, 0);
1058                         }
1059
1060                         DEBUG2("  PEAP: Tunneled authentication will be proxied to %s", vp->vp_strvalue);
1061
1062                         /*
1063                          *      Tell the original request that it's going
1064                          *      to be proxied.
1065                          */
1066                         pairmove2(&(request->config_items),
1067                                   &(fake->config_items),
1068                                   PW_PROXY_TO_REALM, 0);
1069
1070                         /*
1071                          *      Seed the proxy packet with the
1072                          *      tunneled request.
1073                          */
1074                         rad_assert(request->proxy == NULL);
1075                         request->proxy = fake->packet;
1076                         memset(&request->proxy->src_ipaddr, 0,
1077                                sizeof(request->proxy->src_ipaddr));
1078                         memset(&request->proxy->src_ipaddr, 0,
1079                                sizeof(request->proxy->src_ipaddr));
1080                         request->proxy->src_port = 0;
1081                         request->proxy->dst_port = 0;
1082                         fake->packet = NULL;
1083                         rad_free(&fake->reply);
1084                         fake->reply = NULL;
1085
1086                         /*
1087                          *      Set up the callbacks for the tunnel
1088                          */
1089                         tunnel = rad_malloc(sizeof(*tunnel));
1090                         memset(tunnel, 0, sizeof(*tunnel));
1091
1092                         tunnel->tls_session = tls_session;
1093                         tunnel->callback = eappeap_postproxy;
1094
1095                         /*
1096                          *      Associate the callback with the request.
1097                          */
1098                         rcode = request_data_add(request,
1099                                                  request->proxy,
1100                                                  REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1101                                                  tunnel, free);
1102                         rad_assert(rcode == 0);
1103
1104                         /*
1105                          *      We're not proxying it as EAP, so we've got
1106                          *      to do the callback later.
1107                          */
1108                         if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
1109                                 DEBUG2("  PEAP: Remembering to do EAP-MS-CHAP-V2 post-proxy.");
1110
1111                                 /*
1112                                  *      rlm_eap.c has taken care of associating
1113                                  *      the handler with the fake request.
1114                                  *
1115                                  *      So we associate the fake request with
1116                                  *      this request.
1117                                  */
1118                                 rcode = request_data_add(request,
1119                                                          request->proxy,
1120                                                          REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1121                                                          fake, my_request_free);
1122                                 rad_assert(rcode == 0);
1123
1124                                 /*
1125                                  *      Do NOT free the fake request!
1126                                  */
1127                                 return RLM_MODULE_UPDATED;
1128                         }
1129
1130                         /*
1131                          *      Didn't authenticate the packet, but
1132                          *      we're proxying it.
1133                          */
1134                         rcode = RLM_MODULE_UPDATED;
1135
1136                 } else
1137 #endif  /* WITH_PROXY */
1138                   {
1139                         DEBUG2("  PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
1140                         rcode = RLM_MODULE_REJECT;
1141                   }
1142                 break;
1143
1144         default:
1145         do_process:
1146                 rcode = process_reply(handler, tls_session, request,
1147                                       fake->reply);
1148                 break;
1149         }
1150
1151  done:
1152         request_free(&fake);
1153
1154         return rcode;
1155 }