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