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