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