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