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