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