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