track TLS cache filename
[freeradius.git] / src / modules / rlm_eap / rlm_eap.c
1 /*
2  *   This program is is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or (at
5  *   your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16
17 /**
18  * $Id$
19  * @file rlm_eap.c
20  * @brief Implements the EAP framework.
21  *
22  * @copyright 2000-2003,2006  The FreeRADIUS server project
23  * @copyright 2001  hereUare Communications, Inc. <raghud@hereuare.com>
24  * @copyright 2003  Alan DeKok <aland@freeradius.org>
25  */
26 RCSID("$Id$")
27
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/modules.h>
30
31 #include "rlm_eap.h"
32
33 #include <sys/stat.h>
34
35 static const CONF_PARSER module_config[] = {
36         { "default_eap_type", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_eap_t, default_method_name), "md5" },
37         { "timer_expire", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_eap_t, timer_limit), "60" },
38         { "ignore_unknown_eap_types", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_eap_t, ignore_unknown_types), "no" },
39         { "cisco_accounting_username_bug", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_eap_t, mod_accounting_username_bug), "no" },
40         { "max_sessions", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_eap_t, max_sessions), "2048" },
41         CONF_PARSER_TERMINATOR
42 };
43
44 /*
45  * delete all the allocated space by eap module
46  */
47 static int mod_detach(void *instance)
48 {
49         rlm_eap_t *inst;
50
51         inst = (rlm_eap_t *)instance;
52
53 #ifdef HAVE_PTHREAD_H
54         pthread_mutex_destroy(&(inst->session_mutex));
55 #endif
56
57         rbtree_free(inst->session_tree);
58         inst->session_tree = NULL;
59         eaplist_free(inst);
60
61         return 0;
62 }
63
64
65 /*
66  *      Compare two handlers.
67  */
68 static int eap_handler_cmp(void const *a, void const *b)
69 {
70         int rcode;
71         eap_handler_t const *one = a;
72         eap_handler_t const *two = b;
73
74         if (one->eap_id < two->eap_id) return -1;
75         if (one->eap_id > two->eap_id) return +1;
76
77         rcode = memcmp(one->state, two->state, sizeof(one->state));
78         if (rcode != 0) return rcode;
79
80         /*
81          *      As of 2.1.8, we don't key off of source IP.  This
82          *      a NAS to send packets load-balanced (or fail-over)
83          *      across multiple intermediate proxies, and still have
84          *      EAP work.
85          */
86         if (fr_ipaddr_cmp(&one->src_ipaddr, &two->src_ipaddr) != 0) {
87                 WARN("EAP packets are arriving from two different upstream "
88                        "servers.  Has there been a proxy fail-over?");
89         }
90
91         return 0;
92 }
93
94
95 /*
96  * read the config section and load all the eap authentication types present.
97  */
98 static int mod_instantiate(CONF_SECTION *cs, void *instance)
99 {
100         int             i, ret;
101         eap_type_t      method;
102         int             num_methods;
103         CONF_SECTION    *scs;
104         rlm_eap_t       *inst = instance;
105
106         /*
107          *      Create our own random pool.
108          */
109         for (i = 0; i < 256; i++) {
110                 inst->rand_pool.randrsl[i] = fr_rand();
111         }
112         fr_randinit(&inst->rand_pool, 1);
113         inst->rand_pool.randcnt = 0;
114
115         inst->xlat_name = cf_section_name2(cs);
116         if (!inst->xlat_name) inst->xlat_name = "EAP";
117
118         if (!dict_valbyname(PW_AUTH_TYPE, 0, inst->xlat_name)) {
119                 cf_log_err_cs(cs, "Failed to find 'Auth-Type %s' section.  Cannot authenticate users.",
120                               inst->xlat_name);
121                 return -1;
122         }
123
124         /* Load all the configured EAP-Types */
125         num_methods = 0;
126         for(scs = cf_subsection_find_next(cs, NULL, NULL);
127             scs != NULL;
128             scs = cf_subsection_find_next(cs, scs, NULL)) {
129                 char const *name;
130
131                 name = cf_section_name1(scs);
132                 if (!name)  continue;
133
134                 if (!strcmp(name, TLS_CONFIG_SECTION))  continue;
135
136                 /*
137                  *      Easier sometimes than commenting out blocks,
138                  *      or deleting blocks.
139                  */
140                 if (!strcmp(name, "disable")) continue;
141
142                 method = eap_name2type(name);
143                 if (method == PW_EAP_INVALID) {
144                         cf_log_err_cs(cs, "No dictionary definition for EAP method %s", name);
145                         return -1;
146                 }
147
148                 if ((method < PW_EAP_MD5) || (method >= PW_EAP_MAX_TYPES)) {
149                         cf_log_err_cs(cs, "Invalid EAP method %s (unsupported)", name);
150                         return -1;
151                 }
152
153 #if !defined(HAVE_OPENSSL_SSL_H) || !defined(HAVE_LIBSSL)
154                 /*
155                  *      This allows the default configuration to be
156                  *      shipped with EAP-TLS, etc. enabled.  If the
157                  *      system doesn't have OpenSSL, they will be
158                  *      ignored.
159                  *
160                  *      If the system does have OpenSSL, then this
161                  *      code will not be used.  The administrator will
162                  *      then have to delete the tls,
163                  *      etc. configurations from eap.conf in order to
164                  *      have EAP without the TLS types.
165                  */
166                 switch (method) {
167                 case PW_EAP_TLS:
168                 case PW_EAP_TTLS:
169                 case PW_EAP_PEAP:
170                 case PW_EAP_PWD:
171                         WARN("rlm_eap (%s): Ignoring EAP method %s because we don't have OpenSSL support",
172                              inst->xlat_name, name);
173                         continue;
174
175                 default:
176                         break;
177                 }
178 #endif
179
180                 /*
181                  *      Load the type.
182                  */
183                 ret = eap_module_instantiate(inst, &inst->methods[method], method, scs);
184
185                 (void) talloc_get_type_abort(inst->methods[method], eap_module_t);
186
187                 if (ret < 0) {
188                         (void) talloc_steal(inst, inst->methods[method]);
189                         return -1;
190                 }
191
192                 (void) talloc_steal(inst, inst->methods[method]);
193                 num_methods++;  /* successfully loaded one more methods */
194         }
195
196         if (num_methods == 0) {
197                 cf_log_err_cs(cs, "No EAP method configured, module cannot do anything");
198                 return -1;
199         }
200
201         /*
202          *      Ensure that the default EAP type is loaded.
203          */
204         method = eap_name2type(inst->default_method_name);
205         if (method == PW_EAP_INVALID) {
206                 cf_log_err_cs(cs, "No dictionary definition for default EAP method '%s'",
207                        inst->default_method_name);
208                 return -1;
209         }
210
211         if (!inst->methods[method]) {
212                 cf_log_err_cs(cs, "No such sub-type for default EAP method %s",
213                        inst->default_method_name);
214                 return -1;
215         }
216         inst->default_method = method; /* save the numerical method */
217
218         /*
219          *      List of sessions are set to NULL by the memset
220          *      of 'inst', above.
221          */
222
223         /*
224          *      Lookup sessions in the tree.  We don't free them in
225          *      the tree, as that's taken care of elsewhere...
226          */
227         inst->session_tree = rbtree_create(NULL, eap_handler_cmp, NULL, 0);
228         if (!inst->session_tree) {
229                 ERROR("rlm_eap (%s): Cannot initialize tree", inst->xlat_name);
230                 return -1;
231         }
232         fr_link_talloc_ctx_free(inst, inst->session_tree);
233
234 #ifdef HAVE_PTHREAD_H
235         if (pthread_mutex_init(&(inst->session_mutex), NULL) < 0) {
236                 ERROR("rlm_eap (%s): Failed initializing mutex: %s", inst->xlat_name, fr_syserror(errno));
237                 return -1;
238         }
239 #endif
240
241         return 0;
242 }
243
244
245 /*
246  *      For backwards compatibility.
247  */
248 static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
249 {
250         rlm_eap_t               *inst;
251         eap_handler_t           *handler;
252         eap_packet_raw_t        *eap_packet;
253         eap_rcode_t             status;
254         rlm_rcode_t             rcode;
255
256         inst = (rlm_eap_t *) instance;
257
258         if (!fr_pair_find_by_num(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
259                 REDEBUG("You set 'Auth-Type = %s' for a request that does "
260                         "not contain an EAP-Message attribute!", inst->xlat_name);
261                 return RLM_MODULE_INVALID;
262         }
263
264         /*
265          *      Get the eap packet  to start with
266          */
267         eap_packet = eap_vp2packet(request, request->packet->vps);
268         if (!eap_packet) {
269                 RERROR("Malformed EAP Message: %s", fr_strerror());
270                 return RLM_MODULE_FAIL;
271         }
272
273         /*
274          *      Create the eap handler.  The eap_packet will end up being
275          *      "swallowed" into the handler, so we can't access it after
276          *      this call.
277          */
278         handler = eap_handler(inst, &eap_packet, request);
279         if (!handler) {
280                 RDEBUG2("Failed in handler");
281                 return RLM_MODULE_INVALID;
282         }
283
284         /*
285          *      Select the appropriate method or default to the
286          *      configured one
287          */
288         status = eap_method_select(inst, handler);
289
290         /*
291          *      If it failed, die.
292          */
293         if (status == EAP_INVALID) {
294                 eap_fail(handler);
295                 talloc_free(handler);
296                 RDEBUG2("Failed in EAP select");
297                 return RLM_MODULE_INVALID;
298         }
299
300 #ifdef WITH_PROXY
301         /*
302          *      If we're doing horrible tunneling work, remember it.
303          */
304         if ((request->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
305                 RDEBUG2("No EAP proxy set.  Not composing EAP");
306                 /*
307                  *      Add the handle to the proxied list, so that we
308                  *      can retrieve it in the post-proxy stage, and
309                  *      send a response.
310                  */
311                 handler->inst_holder = inst;
312                 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
313
314                 rad_assert(status == 0);
315                 return RLM_MODULE_HANDLED;
316         }
317 #endif
318
319 #ifdef WITH_PROXY
320         /*
321          *      Maybe the request was marked to be proxied.  If so,
322          *      proxy it.
323          */
324         if (request->proxy != NULL) {
325                 VALUE_PAIR *vp = NULL;
326
327                 rad_assert(!request->proxy_reply);
328
329                 /*
330                  *      Add the handle to the proxied list, so that we
331                  *      can retrieve it in the post-proxy stage, and
332                  *      send a response.
333                  */
334                 handler->inst_holder = inst;
335
336                 status = request_data_add(request, inst, REQUEST_DATA_EAP_HANDLER, handler, true);
337
338                 rad_assert(status == 0);
339
340                 /*
341                  *      Some simple sanity checks.  These should really
342                  *      be handled by the radius library...
343                  */
344                 vp = fr_pair_find_by_num(request->proxy->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
345                 if (vp) {
346                         vp = fr_pair_find_by_num(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
347                         if (!vp) {
348                                 fr_pair_make(request->proxy,
349                                          &request->proxy->vps,
350                                          "Message-Authenticator",
351                                          NULL, T_OP_EQ);
352                         }
353                 }
354
355                 /*
356                  *      Delete the "proxied to" attribute, as it's
357                  *      set to 127.0.0.1 for tunneled requests, and
358                  *      we don't want to tell the world that...
359                  */
360                 fr_pair_delete_by_num(&request->proxy->vps, PW_FREERADIUS_PROXIED_TO, VENDORPEC_FREERADIUS, TAG_ANY);
361
362                 RWDEBUG2("Tunneled session will be proxied.  Not doing EAP");
363                 return RLM_MODULE_HANDLED;
364         }
365 #endif
366
367         /*
368          *      We are done, wrap the EAP-request in RADIUS to send
369          *      with all other required radius attributes
370          */
371         rcode = eap_compose(handler);
372
373         /*
374          *      Add to the list only if it is EAP-Request, OR if
375          *      it's LEAP, and a response.
376          */
377         if (((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
378             (handler->eap_ds->request->type.num >= PW_EAP_MD5)) ||
379
380                 /*
381                  *      LEAP is a little different.  At Stage 4,
382                  *      it sends an EAP-Success message, but we still
383                  *      need to keep the State attribute & session
384                  *      data structure around for the AP Challenge.
385                  *
386                  *      At stage 6, LEAP sends an EAP-Response, which
387                  *      isn't put into the list.
388                  */
389             ((handler->eap_ds->response->code == PW_EAP_RESPONSE) &&
390              (handler->eap_ds->response->type.num == PW_EAP_LEAP) &&
391              (handler->eap_ds->request->code == PW_EAP_SUCCESS) &&
392              (handler->eap_ds->request->type.num == 0))) {
393
394                 /*
395                  *      Return FAIL if we can't remember the handler.
396                  *      This is actually disallowed by the
397                  *      specification, as unexpected FAILs could have
398                  *      been forged.  However, we want to signal to
399                  *      everyone else involved that we are
400                  *      intentionally failing the session, as opposed
401                  *      to accidentally failing it.
402                  */
403                 if (!eaplist_add(inst, handler)) {
404                         RDEBUG("Failed adding handler to the list");
405                         eap_fail(handler);
406                         talloc_free(handler);
407                         return RLM_MODULE_FAIL;
408                 }
409
410         } else {
411                 RDEBUG2("Freeing handler");
412                 /* handler is not required any more, free it now */
413                 talloc_free(handler);
414         }
415
416         /*
417          *      Enable the cached entry on success.
418          */
419         if (handler->eap_ds->request->code == PW_EAP_SUCCESS) {
420                 VALUE_PAIR *vp;
421
422                 vp = fr_pair_find_by_num(request->state, PW_TLS_CACHE_FILENAME, 0, TAG_ANY);
423                 if (vp) (void) chmod(vp->vp_strvalue, S_IRUSR | S_IWUSR);
424         }
425
426         /*
427          *      Disable the cached entry on failure.
428          */
429         if (handler->eap_ds->request->code == PW_EAP_FAILURE) {
430                 VALUE_PAIR *vp;
431
432                 vp = fr_pair_find_by_num(request->state, PW_TLS_CACHE_FILENAME, 0, TAG_ANY);
433                 if (vp) (void) unlink(vp->vp_strvalue);
434         }
435
436         /*
437          *      If it's an Access-Accept, RFC 2869, Section 2.3.1
438          *      says that we MUST include a User-Name attribute in the
439          *      Access-Accept.
440          */
441         if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
442             request->username) {
443                 VALUE_PAIR *vp;
444
445                 /*
446                  *      Doesn't exist, add it in.
447                  */
448                 vp = fr_pair_find_by_num(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
449                 if (!vp) {
450                         vp = fr_pair_copy(request->reply, request->username);
451                         fr_pair_add(&request->reply->vps, vp);
452                 }
453
454                 /*
455                  *      Cisco AP1230 has a bug and needs a zero
456                  *      terminated string in Access-Accept.
457                  */
458                 if (inst->mod_accounting_username_bug) {
459                         char const *old = vp->vp_strvalue;
460                         char *new = talloc_zero_array(vp, char, vp->vp_length + 1);
461
462                         memcpy(new, old, vp->vp_length);
463                         vp->vp_strvalue = new;
464                         vp->vp_length++;
465
466                         rad_const_free(old);
467                 }
468         }
469
470         return rcode;
471 }
472
473 /*
474  * EAP authorization DEPENDS on other rlm authorizations,
475  * to check for user existence & get their configured values.
476  * It Handles EAP-START Messages, User-Name initilization.
477  */
478 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
479 {
480         rlm_eap_t       *inst;
481         int             status;
482         VALUE_PAIR      *vp;
483
484         inst = (rlm_eap_t *)instance;
485
486 #ifdef WITH_PROXY
487         /*
488          *      We don't do authorization again, once we've seen the
489          *      proxy reply (or the proxied packet)
490          */
491         if (request->proxy != NULL)
492                 return RLM_MODULE_NOOP;
493 #endif
494
495         /*
496          *      For EAP_START, send Access-Challenge with EAP Identity
497          *      request.  even when we have to proxy this request
498          *
499          *      RFC 2869, Section 2.3.1 notes that the "domain" of the
500          *      user, (i.e. where to proxy him) comes from the EAP-Identity,
501          *      so we CANNOT proxy the user, until we know his identity.
502          *
503          *      We therefore send an EAP Identity request.
504          */
505         status = eap_start(inst, request);
506         switch (status) {
507         case EAP_NOOP:
508                 return RLM_MODULE_NOOP;
509         case EAP_FAIL:
510                 return RLM_MODULE_FAIL;
511         case EAP_FOUND:
512                 return RLM_MODULE_HANDLED;
513         case EAP_OK:
514         case EAP_NOTFOUND:
515         default:
516                 break;
517         }
518
519         /*
520          *      RFC 2869, Section 2.3.1.  If a NAS sends an EAP-Identity,
521          *      it MUST copy the identity into the User-Name attribute.
522          *
523          *      But we don't worry about that too much.  We depend on
524          *      each EAP sub-module to look for handler->request->username,
525          *      and to get excited if it doesn't appear.
526          */
527         vp = fr_pair_find_by_num(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
528         if ((!vp) || (vp->vp_integer != PW_AUTH_TYPE_REJECT)) {
529                 vp = pair_make_config("Auth-Type", inst->xlat_name, T_OP_EQ);
530                 if (!vp) {
531                         RDEBUG2("Failed to create Auth-Type %s: %s\n",
532                                 inst->xlat_name, fr_strerror());
533                         return RLM_MODULE_FAIL;
534                 }
535         } else {
536                 RWDEBUG2("Auth-Type already set.  Not setting to EAP");
537         }
538
539         if (status == EAP_OK) return RLM_MODULE_OK;
540
541         return RLM_MODULE_UPDATED;
542 }
543
544
545 #ifdef WITH_PROXY
546 /*
547  *      If we're proxying EAP, then there may be magic we need
548  *      to do.
549  */
550 static rlm_rcode_t CC_HINT(nonnull) mod_post_proxy(void *inst, REQUEST *request)
551 {
552         size_t          i;
553         size_t          len;
554         ssize_t         ret;
555         char            *p;
556         VALUE_PAIR      *vp;
557         eap_handler_t   *handler;
558         vp_cursor_t     cursor;
559
560         /*
561          *      If there was a handler associated with this request,
562          *      then it's a tunneled request which was proxied...
563          */
564         handler = request_data_get(request, inst, REQUEST_DATA_EAP_HANDLER);
565         if (handler != NULL) {
566                 rlm_rcode_t rcode;
567                 eap_tunnel_data_t *data;
568
569                 /*
570                  *      Grab the tunnel callbacks from the request.
571                  */
572                 data = (eap_tunnel_data_t *) request_data_get(request,
573                                                               request->proxy,
574                                                               REQUEST_DATA_EAP_TUNNEL_CALLBACK);
575                 if (!data) {
576                         RERROR("Failed to retrieve callback for tunneled session!");
577                         talloc_free(handler);
578                         return RLM_MODULE_FAIL;
579                 }
580
581                 /*
582                  *      Do the callback...
583                  */
584                 RDEBUG2("Doing post-proxy callback");
585                 rcode = data->callback(handler, data->tls_session);
586                 talloc_free(data);
587                 if (rcode == 0) {
588                         RDEBUG2("Failed in post-proxy callback");
589                         eap_fail(handler);
590                         talloc_free(handler);
591                         return RLM_MODULE_REJECT;
592                 }
593
594                 /*
595                  *      We are done, wrap the EAP-request in RADIUS to send
596                  *      with all other required radius attributes
597                  */
598                 eap_compose(handler);
599
600                 /*
601                  *      Add to the list only if it is EAP-Request, OR if
602                  *      it's LEAP, and a response.
603                  */
604                 if ((handler->eap_ds->request->code == PW_EAP_REQUEST) &&
605                     (handler->eap_ds->request->type.num >= PW_EAP_MD5)) {
606                         if (!eaplist_add(inst, handler)) {
607                                 eap_fail(handler);
608                                 talloc_free(handler);
609                                 return RLM_MODULE_FAIL;
610                         }
611
612                 } else {        /* couldn't have been LEAP, there's no tunnel */
613                         RDEBUG2("Freeing handler");
614                         /* handler is not required any more, free it now */
615                         talloc_free(handler);
616                 }
617
618                 /*
619                  *      If it's an Access-Accept, RFC 2869, Section 2.3.1
620                  *      says that we MUST include a User-Name attribute in the
621                  *      Access-Accept.
622                  */
623                 if ((request->reply->code == PW_CODE_ACCESS_ACCEPT) &&
624                     request->username) {
625                         /*
626                          *      Doesn't exist, add it in.
627                          */
628                         vp = fr_pair_find_by_num(request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
629                         if (!vp) {
630                                 pair_make_reply("User-Name",
631                                                request->username->vp_strvalue,
632                                                T_OP_EQ);
633                         }
634                 }
635
636                 return RLM_MODULE_OK;
637         } else {
638                 RDEBUG2("No pre-existing handler found");
639         }
640
641         /*
642          *      This is allowed.
643          */
644         if (!request->proxy_reply) return RLM_MODULE_NOOP;
645
646         /*
647          *      There may be more than one Cisco-AVPair.
648          *      Ensure we find the one with the LEAP attribute.
649          */
650         fr_cursor_init(&cursor, &request->proxy_reply->vps);
651         for (;;) {
652                 /*
653                  *      Hmm... there's got to be a better way to
654                  *      discover codes for vendor attributes.
655                  *
656                  *      This is vendor Cisco (9), Cisco-AVPair
657                  *      attribute (1)
658                  */
659                 vp = fr_cursor_next_by_num(&cursor, 1, 9, TAG_ANY);
660                 if (!vp) {
661                         return RLM_MODULE_NOOP;
662                 }
663
664                 /*
665                  *      If it's "leap:session-key", then stop.
666                  *
667                  *      The format is VERY specific!
668                  */
669                 if (strncasecmp(vp->vp_strvalue, "leap:session-key=", 17) == 0) {
670                         break;
671                 }
672         }
673
674         /*
675          *      The format is very specific.
676          */
677         if (vp->vp_length != (17 + 34)) {
678                 RDEBUG2("Cisco-AVPair with leap:session-key has incorrect length %zu: Expected %d",
679                        vp->vp_length, 17 + 34);
680                 return RLM_MODULE_NOOP;
681         }
682
683         /*
684          *      Decrypt the session key, using the proxy data.
685          *
686          *      Note that the session key is *binary*, and therefore
687          *      may contain embedded zeros.  So we have to use memdup.
688          *      However, Cisco-AVPair is a "string", so the rest of the
689          *      code assumes that it's terminated by a trailing '\0'.
690          *
691          *      So... be sure to (a) use memdup, and (b) include the last
692          *      zero byte.
693          */
694         i = 34;
695         p = talloc_memdup(vp, vp->vp_strvalue, vp->vp_length + 1);
696         talloc_set_type(p, uint8_t);
697         ret = rad_tunnel_pwdecode((uint8_t *)p + 17, &i, request->home_server->secret, request->proxy->vector);
698         if (ret < 0) {
699                 REDEBUG("Decoding leap:session-key failed");
700                 talloc_free(p);
701                 return RLM_MODULE_FAIL;
702         }
703         len = i;
704
705         if (i != 16) {
706                 REDEBUG("Decoded key length is incorrect, must be 16 bytes");
707                 talloc_free(p);
708                 return RLM_MODULE_FAIL;
709         }
710
711         /*
712          *      Encrypt the session key again, using the request data.
713          */
714         ret = rad_tunnel_pwencode(p + 17, &len, request->client->secret, request->packet->vector);
715         if (ret < 0) {
716                 REDEBUG("Decoding leap:session-key failed");
717                 talloc_free(p);
718                 return RLM_MODULE_FAIL;
719         }
720
721         fr_pair_value_strsteal(vp, p);
722
723         return RLM_MODULE_UPDATED;
724 }
725 #endif
726
727 static rlm_rcode_t CC_HINT(nonnull) mod_post_auth(void *instance, REQUEST *request)
728 {
729         rlm_eap_t       *inst = instance;
730         VALUE_PAIR      *vp;
731         eap_handler_t   *handler;
732         eap_packet_raw_t        *eap_packet;
733
734         /*
735          * Only build a failure message if something previously rejected the request
736          */
737         vp = fr_pair_find_by_num(request->config, PW_POST_AUTH_TYPE, 0, TAG_ANY);
738
739         if (!vp || (vp->vp_integer != PW_POST_AUTH_TYPE_REJECT)) return RLM_MODULE_NOOP;
740
741         if (!fr_pair_find_by_num(request->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
742                 RDEBUG3("Request didn't contain an EAP-Message, not inserting EAP-Failure");
743                 return RLM_MODULE_NOOP;
744         }
745
746         if (fr_pair_find_by_num(request->reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY)) {
747                 RDEBUG3("Reply already contained an EAP-Message, not inserting EAP-Failure");
748                 return RLM_MODULE_NOOP;
749         }
750
751         eap_packet = eap_vp2packet(request, request->packet->vps);
752         if (!eap_packet) {
753                 RERROR("Malformed EAP Message: %s", fr_strerror());
754                 return RLM_MODULE_FAIL;
755         }
756
757         handler = eap_handler(inst, &eap_packet, request);
758         if (!handler) {
759                 RDEBUG2("Failed to get handler, probably already removed, not inserting EAP-Failure");
760                 return RLM_MODULE_NOOP;
761         }
762
763         RDEBUG2("Request was previously rejected, inserting EAP-Failure");
764         eap_fail(handler);
765         talloc_free(handler);
766
767         /*
768          * Make sure there's a message authenticator attribute in the response
769          * RADIUS protocol code will calculate the correct value later...
770          */
771         vp = fr_pair_find_by_num(request->reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
772         if (!vp) {
773                 pair_make_reply("Message-Authenticator", "0x00", T_OP_EQ);
774         }
775
776         return RLM_MODULE_UPDATED;
777 }
778
779 /*
780  *      The module name should be the only globally exported symbol.
781  *      That is, everything else should be 'static'.
782  */
783 extern module_t rlm_eap;
784 module_t rlm_eap = {
785         .magic          = RLM_MODULE_INIT,
786         .name           = "eap",
787         .inst_size      = sizeof(rlm_eap_t),
788         .config         = module_config,
789         .instantiate    = mod_instantiate,
790         .detach         = mod_detach,
791         .methods = {
792                 [MOD_AUTHENTICATE]      = mod_authenticate,
793                 [MOD_AUTHORIZE]         = mod_authorize,
794 #ifdef WITH_PROXY
795                 [MOD_POST_PROXY]        = mod_post_proxy,
796 #endif
797                 [MOD_POST_AUTH]         = mod_post_auth
798         },
799 };