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