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