a0b843c324f5c8ab1c8d3dfc2aa340f1c0a6ca4f
[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         rad_assert(vp != NULL);
244
245         /*
246          *      Skip the id, code, and length.  Just write the EAP
247          *      type & data to the client.
248          */
249 #ifndef NDEBUG
250         if ((debug_flag > 2) && fr_log_fp) {
251                 size_t i, total;
252                 VALUE_PAIR *this;
253
254                 total = 0;
255
256                 for (this = vp; this != NULL; this = this->next) {
257                         int start = 0;
258
259                         if (this == vp) start = EAP_HEADER_LEN;
260                         
261                         for (i = start; i < vp->length; i++) {
262                           if ((total & 0x0f) == 0) fprintf(fr_log_fp, "  PEAP tunnel data out %04x: ", (int) total);
263
264                                 fprintf(fr_log_fp, "%02x ", vp->vp_octets[i]);
265                                 
266                                 if ((total & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
267                                 total++;
268                         }
269                 }
270                 if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
271         }
272 #endif
273
274         /*
275          *      Send the EAP data, WITHOUT the header.
276          */
277         (tls_session->record_plus)(&tls_session->clean_in,
278                                    vp->vp_octets + EAP_HEADER_LEN,
279                                    vp->length - EAP_HEADER_LEN);
280         
281         /*
282          *      Send the rest of the EAP data.
283          */
284         for (vp = vp->next; vp != NULL; vp = vp->next) {
285                 (tls_session->record_plus)(&tls_session->clean_in,
286                                            vp->vp_octets, vp->length);
287         }
288
289         tls_handshake_send(request, tls_session);
290
291         return 1;
292 }
293
294
295 /*
296  *      See if there's a TLV in the response.
297  */
298 static int eappeap_check_tlv(REQUEST *request, const uint8_t *data)
299 {
300         const eap_packet_t *eap_packet = (const eap_packet_t *) data;
301
302         /*
303          *      Look for success or failure.
304          */
305         if ((eap_packet->code == PW_EAP_RESPONSE) &&
306             (eap_packet->data[0] == PW_EAP_TLV)) {
307                 if (data[10] == EAP_TLV_SUCCESS) {
308                         return 1;
309                 }
310
311                 if (data[10] == EAP_TLV_FAILURE) {
312                         RDEBUG2("Client rejected our response.  The password is probably incorrect.");
313                         return 0;
314                 }
315         }
316
317         return 0;
318 }
319
320
321 /*
322  *      Use a reply packet to determine what to do.
323  */
324 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
325                          REQUEST *request, RADIUS_PACKET *reply)
326 {
327         int rcode = RLM_MODULE_REJECT;
328         VALUE_PAIR *vp;
329         peap_tunnel_t *t = tls_session->opaque;
330
331         if ((debug_flag > 0) && fr_log_fp) {
332                 RDEBUG("Got tunneled reply RADIUS code %d",
333                        reply->code);
334
335                 debug_pair_list(reply->vps);
336         }
337
338         switch (reply->code) {
339         case PW_AUTHENTICATION_ACK:
340                 RDEBUG2("Tunneled authentication was successful.");
341                 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
342                 eappeap_success(handler, tls_session);
343                 rcode = RLM_MODULE_HANDLED;
344
345                 /*
346                  *      If we've been told to use the attributes from
347                  *      the reply, then do so.
348                  *
349                  *      WARNING: This may leak information about the
350                  *      tunneled user!
351                  */
352                 if (t->use_tunneled_reply) {
353                         RDEBUG2("Saving tunneled attributes for later");
354
355                         /*
356                          *      Clean up the tunneled reply.
357                          */
358                         pairdelete(&reply->vps, PW_PROXY_STATE, 0);
359                         pairdelete(&reply->vps, PW_EAP_MESSAGE, 0);
360                         pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR, 0);
361
362                         /*
363                          *      Delete MPPE keys & encryption policy.  We don't
364                          *      want these here.
365                          */
366                         pairdelete(&reply->vps, 7, VENDORPEC_MICROSOFT);
367                         pairdelete(&reply->vps, 8, VENDORPEC_MICROSOFT);
368                         pairdelete(&reply->vps, 16, VENDORPEC_MICROSOFT);
369                         pairdelete(&reply->vps, 17, VENDORPEC_MICROSOFT);
370
371                         t->accept_vps = reply->vps;
372                         reply->vps = NULL;
373                 }
374                 break;
375
376         case PW_AUTHENTICATION_REJECT:
377                 RDEBUG2("Tunneled authentication was rejected.");
378                 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
379                 eappeap_failure(handler, tls_session);
380                 rcode = RLM_MODULE_HANDLED;
381                 break;
382
383         case PW_ACCESS_CHALLENGE:
384                 RDEBUG2("Got tunneled Access-Challenge");
385
386                 /*
387                  *      Keep the State attribute, if necessary.
388                  *
389                  *      Get rid of the old State, too.
390                  */
391                 pairfree(&t->state);
392                 pairmove2(&t->state, &(reply->vps), PW_STATE, 0);
393
394                 /*
395                  *      PEAP takes only EAP-Message attributes inside
396                  *      of the tunnel.  Any Reply-Message in the
397                  *      Access-Challenge is ignored.
398                  */
399                 vp = NULL;
400                 pairmove2(&vp, &(reply->vps), PW_EAP_MESSAGE, 0);
401
402                 /*
403                  *      Handle EAP-MSCHAP-V2, where Access-Accept's
404                  *      from the home server may contain MS-CHAP-Success,
405                  *      which the module turns into challenges, so that
406                  *      the client may respond to the challenge with
407                  *      an "ack" packet.
408                  */
409                 if (t->home_access_accept && t->use_tunneled_reply) {
410                         RDEBUG2("Saving tunneled attributes for later");
411
412                         /*
413                          *      Clean up the tunneled reply.
414                          */
415                         pairdelete(&reply->vps, PW_PROXY_STATE, 0);
416                         pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR, 0);
417
418                         t->accept_vps = reply->vps;
419                         reply->vps = NULL;
420                 }
421
422                 /*
423                  *      Handle the ACK, by tunneling any necessary reply
424                  *      VP's back to the client.
425                  */
426                 if (vp) {
427                         vp2eap(request, tls_session, vp);
428                         pairfree(&vp);
429                 }
430
431                 rcode = RLM_MODULE_HANDLED;
432                 break;
433
434         default:
435                 RDEBUG2("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
436                 rcode = RLM_MODULE_REJECT;
437                 break;
438         }
439
440         return rcode;
441 }
442
443 #ifdef WITH_PROXY
444 /*
445  *      Do post-proxy processing,
446  */
447 static int eappeap_postproxy(EAP_HANDLER *handler, void *data)
448 {
449         int rcode;
450         tls_session_t *tls_session = (tls_session_t *) data;
451         REQUEST *fake, *request = handler->request;
452
453         rad_assert(request != NULL);
454         RDEBUG2("Passing reply from proxy back into the tunnel.");
455
456         /*
457          *      If there was a fake request associated with the proxied
458          *      request, do more processing of it.
459          */
460         fake = (REQUEST *) request_data_get(handler->request,
461                                             handler->request->proxy,
462                                             REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
463
464         /*
465          *      Do the callback, if it exists, and if it was a success.
466          */
467         if (fake && (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
468                 peap_tunnel_t *t = tls_session->opaque;
469
470                 t->home_access_accept = TRUE;
471
472                 /*
473                  *      Terrible hacks.
474                  */
475                 rad_assert(fake->packet == NULL);
476                 fake->packet = request->proxy;
477                 fake->packet->src_ipaddr = request->packet->src_ipaddr;
478                 request->proxy = NULL;
479
480                 rad_assert(fake->reply == NULL);
481                 fake->reply = request->proxy_reply;
482                 request->proxy_reply = NULL;
483
484                 if ((debug_flag > 0) && fr_log_fp) {
485                         fprintf(fr_log_fp, "server %s {\n", fake->server);
486                 }
487
488                 /*
489                  *      Perform a post-auth stage, which will get the EAP
490                  *      handler, too...
491                  */
492                 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
493                 RDEBUG2("Passing reply back for EAP-MS-CHAP-V2");
494                 module_post_proxy(0, fake);
495
496                 /*
497                  *      FIXME: If rcode returns fail, do something
498                  *      intelligent...
499                  */
500                 rcode = rad_postauth(fake);
501
502                 if ((debug_flag > 0) && fr_log_fp) {
503                         fprintf(fr_log_fp, "} # server %s\n", fake->server);
504                         
505                         RDEBUG("Final reply from tunneled session code %d",
506                                fake->reply->code);
507                 
508                         debug_pair_list(fake->reply->vps);
509                 }
510
511                 /*
512                  *      Terrible hacks.
513                  */
514                 request->proxy = fake->packet;
515                 fake->packet = NULL;
516                 request->proxy_reply = fake->reply;
517                 fake->reply = NULL;
518
519                 /*
520                  *      And we're done with this request.
521                  */
522
523                 switch (rcode) {
524                 case RLM_MODULE_FAIL:
525                         request_free(&fake);
526                         eaptls_fail(handler, 0);
527                         return 0;
528                         break;
529
530                 default:  /* Don't Do Anything */
531                         RDEBUG2("Got reply %d", request->proxy_reply->code);
532                         break;
533                 }
534         }
535         request_free(&fake);    /* robust if fake == NULL */
536
537         /*
538          *      If there was no EAP-Message in the reply packet, then
539          *      we know that we're supposed to re-run the "authenticate"
540          *      stage, in order to get the right kind of handling...
541          */
542
543         /*
544          *      Process the reply from the home server.
545          */
546
547         rcode = process_reply(handler, tls_session, handler->request,
548                               handler->request->proxy_reply);
549
550         /*
551          *      The proxy code uses the reply from the home server as
552          *      the basis for the reply to the NAS.  We don't want that,
553          *      so we toss it, after we've had our way with it.
554          */
555         pairfree(&handler->request->proxy_reply->vps);
556
557         switch (rcode) {
558         case RLM_MODULE_REJECT:
559                 RDEBUG2("Reply was rejected");
560                 eaptls_fail(handler, 0);
561                 return 0;
562
563         case RLM_MODULE_HANDLED:
564                 RDEBUG2("Reply was handled");
565                 eaptls_request(handler->eap_ds, tls_session);
566                 return 1;
567
568         case RLM_MODULE_OK:
569                 RDEBUG2("Reply was OK");
570
571                 /*
572                  *      Success: Automatically return MPPE keys.
573                  */
574                 return eaptls_success(handler, 0);
575
576         default:
577                 RDEBUG2("Reply was unknown.");
578                 break;
579         }
580
581         eaptls_fail(handler, 0);
582         return 0;
583 }
584
585 /*
586  *      Free a request.
587  */
588 static void my_request_free(void *data)
589 {
590         REQUEST *request = (REQUEST *)data;
591
592         request_free(&request);
593 }
594 #endif
595
596
597 static const char *peap_state(peap_tunnel_t *t)
598 {
599         switch (t->status) {
600                 case PEAP_STATUS_TUNNEL_ESTABLISHED:
601                         return "TUNNEL ESTABLISHED";
602                 case PEAP_STATUS_INNER_IDENTITY_REQ_SENT:
603                         return "WAITING FOR INNER IDENTITY";
604                 case PEAP_STATUS_SENT_TLV_SUCCESS:
605                         return "send tlv success";
606                 case PEAP_STATUS_SENT_TLV_FAILURE:
607                         return "send tlv failure";
608                 case PEAP_STATUS_PHASE2_INIT:
609                         return "phase2_init";
610                 case PEAP_STATUS_PHASE2:
611                         return "phase2";
612                 default:
613                         break;
614         }
615         return "?";
616 }
617
618 static void print_tunneled_data(const uint8_t *data, size_t data_len)
619 {
620         size_t i;
621
622         if ((debug_flag > 2) && fr_log_fp) {
623                 for (i = 0; i < data_len; i++) {
624                   if ((i & 0x0f) == 0) fprintf(fr_log_fp, "  PEAP tunnel data in %02x: ", (int) i);
625                         
626                         fprintf(fr_log_fp, "%02x ", data[i]);
627                         
628                         if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
629                 }
630                 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
631         }
632 }
633
634
635 /*
636  *      Process the pseudo-EAP contents of the tunneled data.
637  */
638 int eappeap_process(EAP_HANDLER *handler, tls_session_t *tls_session)
639 {
640         peap_tunnel_t *t = tls_session->opaque;
641         REQUEST *fake;
642         VALUE_PAIR *vp;
643         int rcode = RLM_MODULE_REJECT;
644         const uint8_t   *data;
645         unsigned int data_len;
646
647         REQUEST *request = handler->request;
648         EAP_DS *eap_ds = handler->eap_ds;
649
650         rad_assert(request != NULL);
651
652         /*
653          *      Just look at the buffer directly, without doing
654          *      record_minus.  This lets us avoid another data copy.
655          */
656         data_len = tls_session->clean_out.used;
657         tls_session->clean_out.used = 0;
658         data = tls_session->clean_out.data;
659
660         RDEBUG2("Peap state %s", peap_state(t));
661
662         if ((t->status != PEAP_STATUS_TUNNEL_ESTABLISHED) &&
663             !eapmessage_verify(request, data, data_len)) {
664                 RDEBUG2("FAILED processing PEAP: Tunneled data is invalid.");
665                 if (debug_flag > 2) print_tunneled_data(data, data_len);
666                 return RLM_MODULE_REJECT;
667         }
668
669         switch (t->status) {
670         case PEAP_STATUS_TUNNEL_ESTABLISHED:
671                 /* FIXME: should be no data in the buffer here, check & assert? */
672                 
673                 if (SSL_session_reused(tls_session->ssl)) {
674                         RDEBUG2("Skipping Phase2 because of session resumption");
675                         t->session_resumption_state = PEAP_RESUMPTION_YES;
676                         
677                         /* we're good, send success TLV */
678                         t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
679                         eappeap_success(handler, tls_session);
680                         
681                 } else {
682                         /* send an identity request */
683                         t->session_resumption_state = PEAP_RESUMPTION_NO;
684                         t->status = PEAP_STATUS_INNER_IDENTITY_REQ_SENT;
685                         eappeap_identity(handler, tls_session);
686                 }
687                 return RLM_MODULE_HANDLED;
688
689         case PEAP_STATUS_INNER_IDENTITY_REQ_SENT:
690                 /* we're expecting an identity response */
691                 if (data[0] != PW_EAP_IDENTITY) {
692                         RDEBUG("Expected EAP-Identity, got something else.");
693                         return RLM_MODULE_REJECT;
694                 }
695
696                 if (data_len >= sizeof(t->username->vp_strvalue)) {
697                         RDEBUG("EAP-Identity is too long");
698                         return RLM_MODULE_REJECT;
699                 }
700                 
701                 /*
702                  *      Save it for later.
703                  */
704                 t->username = pairmake("User-Name", "", T_OP_EQ);
705                 rad_assert(t->username != NULL);
706                 
707                 memcpy(t->username->vp_strvalue, data + 1, data_len - 1);
708                 t->username->length = data_len - 1;
709                 t->username->vp_strvalue[t->username->length] = 0;
710                 RDEBUG("Got inner identity '%s'", t->username->vp_strvalue);
711                 
712                 t->status = PEAP_STATUS_PHASE2_INIT;
713                 break;
714
715         /*
716          *      If we authenticated the user, then it's OK.
717          */
718         case PEAP_STATUS_SENT_TLV_SUCCESS:
719                 if (eappeap_check_tlv(request, data)) {
720                         RDEBUG2("Success");
721                         return RLM_MODULE_OK;
722                 }
723
724                 /*
725                  *      Otherwise, the client rejected the session
726                  *      resumption.  If the session is being re-used,
727                  *      we need to do a full authentication.
728                  *
729                  *      We do this by sending an EAP-Identity request
730                  *      inside of the PEAP tunnel.
731                  */
732                 if (t->session_resumption_state == PEAP_RESUMPTION_YES) {
733                         RDEBUG2("Client rejected session resumption.  Re-starting full authentication");
734                         
735                         /*
736                          *      Mark session resumption status.
737                          */
738                         t->status = PEAP_STATUS_INNER_IDENTITY_REQ_SENT;
739                         t->session_resumption_state = PEAP_RESUMPTION_NO;
740                         
741                         eappeap_identity(handler, tls_session);
742                         return RLM_MODULE_HANDLED;
743                 }
744
745                 RDEBUG2("We sent a success, but received something weird in return.");
746                 return RLM_MODULE_REJECT;
747
748         /*
749          *      Damned if I know why the clients continue sending EAP
750          *      packets after we told them to f*ck off.
751          */
752         case PEAP_STATUS_SENT_TLV_FAILURE:
753                 RDEBUG(" The users session was previously rejected: returning reject (again.)");
754                 RDEBUG(" *** This means you need to read the PREVIOUS messages in the debug output");
755                 RDEBUG(" *** to find out the reason why the user was rejected.");
756                 RDEBUG(" *** Look for \"reject\" or \"fail\".  Those earlier messages will tell you.");
757                 RDEBUG(" *** what went wrong, and how to fix the problem.");
758                 return RLM_MODULE_REJECT;
759
760                 case PEAP_STATUS_PHASE2_INIT:
761                         RDEBUG("In state machine in phase2 init?");
762
763                 case PEAP_STATUS_PHASE2:
764                         break;
765
766                 default:
767                         RDEBUG2("Unhandled state in peap");
768                         return RLM_MODULE_REJECT;
769         }
770
771         fake = request_alloc_fake(request);
772
773         rad_assert(fake->packet->vps == NULL);
774
775         switch (t->status) {
776                 /*
777                  *      If we're in PHASE2_INIT, the phase2 method hasn't been
778                  *      sent an Identity packet yet; do so from the stored
779                  *      username and this will kick off the phase2 eap method
780                  */
781                 
782         case PEAP_STATUS_PHASE2_INIT: {
783                 int len = t->username->length + EAP_HEADER_LEN + 1;
784
785                 t->status = PEAP_STATUS_PHASE2;
786
787                 vp = paircreate(PW_EAP_MESSAGE, 0, PW_TYPE_OCTETS);
788
789                 vp->vp_octets[0] = PW_EAP_RESPONSE;
790                 vp->vp_octets[1] = eap_ds->response->id;
791                 vp->vp_octets[2] = (len >> 8) & 0xff;
792                 vp->vp_octets[3] = len & 0xff;
793                 vp->vp_octets[4] = PW_EAP_IDENTITY;
794
795                 memcpy(vp->vp_octets + EAP_HEADER_LEN + 1, t->username->vp_strvalue, t->username->length);
796                 vp->length = len;
797
798                 pairadd(&fake->packet->vps, vp);
799
800                 if (t->default_eap_type != 0) {
801                         RDEBUG2("Setting default EAP type for tunneled EAP session.");
802                         vp = pairmake("EAP-Type", "0", T_OP_EQ);
803                         vp->vp_integer = t->default_eap_type;
804                         pairadd(&fake->config_items, vp);
805                 }
806                 break; }
807
808         case PEAP_STATUS_PHASE2:
809                 fake->packet->vps = eap2vp(request, eap_ds, data, data_len);
810                 if (!fake->packet->vps) {
811                         request_free(&fake);
812                         RDEBUG2("Unable to convert tunneled EAP packet to internal server data structures");
813                         return PW_AUTHENTICATION_REJECT;
814                 }
815                 break;
816
817         default:
818                 RDEBUG("Invalid state change in PEAP.");
819                 return PW_AUTHENTICATION_REJECT;
820         }
821
822         if ((debug_flag > 0) && fr_log_fp) {
823                 RDEBUG("Got tunneled request");
824                 
825                 debug_pair_list(fake->packet->vps);
826
827                 fprintf(fr_log_fp, "server %s {\n",
828                         (fake->server == NULL) ? "" : fake->server);
829         }
830
831         /*
832          *      Tell the request that it's a fake one.
833          */
834         vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
835         if (vp) {
836                 pairadd(&fake->packet->vps, vp);
837         }
838
839         /*
840          *      Update other items in the REQUEST data structure.
841          */
842         if (!t->username) {
843                 /*
844                  *      There's no User-Name in the tunneled session,
845                  *      so we add one here, by pulling it out of the
846                  *      EAP-Identity packet.
847                  */
848                 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
849                         t->username = pairmake("User-Name", "", T_OP_EQ);
850                         rad_assert(t->username != NULL);
851
852                         memcpy(t->username->vp_strvalue, data + 1, data_len - 1);
853                         t->username->length = data_len - 1;
854                         t->username->vp_strvalue[t->username->length] = 0;
855                         DEBUG2("  PEAP: Got tunneled identity of %s", t->username->vp_strvalue);
856
857                         /*
858                          *      If there's a default EAP type,
859                          *      set it here.
860                          */
861                         if (t->default_eap_type != 0) {
862                                 DEBUG2("  PEAP: Setting default EAP type for tunneled EAP session.");
863                                 vp = pairmake("EAP-Type", "0", T_OP_EQ);
864                                 vp->vp_integer = t->default_eap_type;
865                                 pairadd(&fake->config_items, vp);
866                         }
867                 }
868         } /* else there WAS a t->username */
869
870         if (t->username) {
871                 vp = paircopy(t->username);
872                 pairadd(&fake->packet->vps, vp);
873                 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0);
874                 DEBUG2("  PEAP: Setting User-Name to %s",
875                        fake->username->vp_strvalue);
876         }
877
878         /*
879          *      Add the State attribute, too, if it exists.
880          */
881         if (t->state) {
882                 vp = paircopy(t->state);
883                 if (vp) pairadd(&fake->packet->vps, vp);
884         }
885
886         /*
887          *      If this is set, we copy SOME of the request attributes
888          *      from outside of the tunnel to inside of the tunnel.
889          *
890          *      We copy ONLY those attributes which do NOT already
891          *      exist in the tunneled request.
892          *
893          *      This code is copied from ../rlm_eap_ttls/ttls.c
894          */
895         if (t->copy_request_to_tunnel) {
896                 VALUE_PAIR *copy;
897
898                 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
899                         /*
900                          *      The attribute is a server-side thingy,
901                          *      don't copy it.
902                          */
903                         if ((vp->attribute > 255) &&
904                             (vp->vendor == 0)) {
905                                 continue;
906                         }
907
908                         /*
909                          *      The outside attribute is already in the
910                          *      tunnel, don't copy it.
911                          *
912                          *      This works for BOTH attributes which
913                          *      are originally in the tunneled request,
914                          *      AND attributes which are copied there
915                          *      from below.
916                          */
917                         if (pairfind(fake->packet->vps, vp->attribute, vp->vendor)) {
918                                 continue;
919                         }
920
921                         /*
922                          *      Some attributes are handled specially.
923                          */
924                         switch (vp->attribute) {
925                                 /*
926                                  *      NEVER copy Message-Authenticator,
927                                  *      EAP-Message, or State.  They're
928                                  *      only for outside of the tunnel.
929                                  */
930                         case PW_USER_NAME:
931                         case PW_USER_PASSWORD:
932                         case PW_CHAP_PASSWORD:
933                         case PW_CHAP_CHALLENGE:
934                         case PW_PROXY_STATE:
935                         case PW_MESSAGE_AUTHENTICATOR:
936                         case PW_EAP_MESSAGE:
937                         case PW_STATE:
938                                 continue;
939                                 break;
940
941                                 /*
942                                  *      By default, copy it over.
943                                  */
944                         default:
945                                 break;
946                         }
947
948                         /*
949                          *      Don't copy from the head, we've already
950                          *      checked it.
951                          */
952                         copy = paircopy2(vp, vp->attribute, vp->vendor);
953                         pairadd(&fake->packet->vps, copy);
954                 }
955         }
956
957         if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0)) != NULL) {
958                 fake->server = vp->vp_strvalue;
959
960         } else if (t->virtual_server) {
961                 fake->server = t->virtual_server;
962
963         } /* else fake->server == request->server */
964
965         if ((debug_flag > 0) && fr_log_fp) {
966                 fprintf(fr_log_fp, "Sending tunneled request\n");
967
968                 debug_pair_list(fake->packet->vps);
969
970                 fprintf(fr_log_fp, "server %s {\n",
971                         (fake->server == NULL) ? "" : fake->server);
972         }
973
974         /*
975          *      Call authentication recursively, which will
976          *      do PAP, CHAP, MS-CHAP, etc.
977          */
978         rad_authenticate(fake);
979
980         /*
981          *      Note that we don't do *anything* with the reply
982          *      attributes.
983          */
984         if ((debug_flag > 0) && fr_log_fp) {
985                 fprintf(fr_log_fp, "} # server %s\n",
986                         (fake->server == NULL) ? "" : fake->server);
987
988                 RDEBUG("Got tunneled reply code %d", fake->reply->code);
989                 
990                 debug_pair_list(fake->reply->vps);
991         }
992
993         /*
994          *      Decide what to do with the reply.
995          */
996         switch (fake->reply->code) {
997         case 0:                 /* No reply code, must be proxied... */
998 #ifdef WITH_PROXY
999                 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM, 0);
1000
1001                 if (vp) {
1002                         eap_tunnel_data_t *tunnel;
1003
1004                         /*
1005                          *      The tunneled request was NOT handled,
1006                          *      it has to be proxied.  This means that
1007                          *      the "authenticate" stage was never
1008                          *      performed.
1009                          *
1010                          *      If we are told to NOT proxy the
1011                          *      tunneled request as EAP, then this
1012                          *      means that we've got to decode it,
1013                          *      which means that we MUST run the
1014                          *      "authenticate" portion by hand, here.
1015                          *
1016                          *      Once the tunneled EAP session is ALMOST
1017                          *      done, THEN we proxy it...
1018                          */
1019                         if (!t->proxy_tunneled_request_as_eap) {
1020                                 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
1021
1022                                 /*
1023                                  *      Hmm... should we check for
1024                                  *      Auth-Type & EAP-Message here?
1025                                  */
1026
1027
1028                                 /*
1029                                  *      Run the EAP authentication.
1030                                  */
1031                                 DEBUG2("  PEAP: Calling authenticate in order to initiate tunneled EAP session.");
1032                                 rcode = module_authenticate(PW_AUTHTYPE_EAP, fake);
1033                                 if (rcode == RLM_MODULE_OK) {
1034                                         /*
1035                                          *      Authentication succeeded! Rah!
1036                                          */
1037                                         fake->reply->code = PW_AUTHENTICATION_ACK;
1038                                         goto do_process;
1039                                 }
1040
1041                                 if (rcode != RLM_MODULE_HANDLED) {
1042                                         DEBUG2("  PEAP: Can't handle the return code %d", rcode);
1043                                         rcode = RLM_MODULE_REJECT;
1044                                         goto done;
1045                                 }
1046
1047                                 /*
1048                                  *      The module decided it wasn't
1049                                  *      done.  Handle it like normal.
1050                                  */
1051                                 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
1052                                         DEBUG2("    PEAP: Cancelling proxy to realm %s until the tunneled EAP session has been established", vp->vp_strvalue);
1053                                         goto do_process;
1054                                 }
1055
1056                                 /*
1057                                  *      The module has decoded the
1058                                  *      EAP-Message into another set
1059                                  *      of attributes.
1060                                  */
1061                                 pairdelete(&fake->packet->vps,
1062                                            PW_EAP_MESSAGE, 0);
1063                         }
1064
1065                         DEBUG2("  PEAP: Tunneled authentication will be proxied to %s", vp->vp_strvalue);
1066
1067                         /*
1068                          *      Tell the original request that it's going
1069                          *      to be proxied.
1070                          */
1071                         pairmove2(&(request->config_items),
1072                                   &(fake->config_items),
1073                                   PW_PROXY_TO_REALM, 0);
1074
1075                         /*
1076                          *      Seed the proxy packet with the
1077                          *      tunneled request.
1078                          */
1079                         rad_assert(request->proxy == NULL);
1080                         request->proxy = fake->packet;
1081                         memset(&request->proxy->src_ipaddr, 0,
1082                                sizeof(request->proxy->src_ipaddr));
1083                         memset(&request->proxy->src_ipaddr, 0,
1084                                sizeof(request->proxy->src_ipaddr));
1085                         request->proxy->src_port = 0;
1086                         request->proxy->dst_port = 0;
1087                         fake->packet = NULL;
1088                         rad_free(&fake->reply);
1089                         fake->reply = NULL;
1090
1091                         /*
1092                          *      Set up the callbacks for the tunnel
1093                          */
1094                         tunnel = rad_malloc(sizeof(*tunnel));
1095                         memset(tunnel, 0, sizeof(*tunnel));
1096
1097                         tunnel->tls_session = tls_session;
1098                         tunnel->callback = eappeap_postproxy;
1099
1100                         /*
1101                          *      Associate the callback with the request.
1102                          */
1103                         rcode = request_data_add(request,
1104                                                  request->proxy,
1105                                                  REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1106                                                  tunnel, free);
1107                         rad_assert(rcode == 0);
1108
1109                         /*
1110                          *      We're not proxying it as EAP, so we've got
1111                          *      to do the callback later.
1112                          */
1113                         if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
1114                                 DEBUG2("  PEAP: Remembering to do EAP-MS-CHAP-V2 post-proxy.");
1115
1116                                 /*
1117                                  *      rlm_eap.c has taken care of associating
1118                                  *      the handler with the fake request.
1119                                  *
1120                                  *      So we associate the fake request with
1121                                  *      this request.
1122                                  */
1123                                 rcode = request_data_add(request,
1124                                                          request->proxy,
1125                                                          REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1126                                                          fake, my_request_free);
1127                                 rad_assert(rcode == 0);
1128
1129                                 /*
1130                                  *      Do NOT free the fake request!
1131                                  */
1132                                 return RLM_MODULE_UPDATED;
1133                         }
1134
1135                         /*
1136                          *      Didn't authenticate the packet, but
1137                          *      we're proxying it.
1138                          */
1139                         rcode = RLM_MODULE_UPDATED;
1140
1141                 } else
1142 #endif  /* WITH_PROXY */
1143                   {
1144                         DEBUG2("  PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
1145                         rcode = RLM_MODULE_REJECT;
1146                   }
1147                 break;
1148
1149         default:
1150         do_process:
1151                 rcode = process_reply(handler, tls_session, request,
1152                                       fake->reply);
1153                 break;
1154         }
1155
1156  done:
1157         request_free(&fake);
1158
1159         return rcode;
1160 }