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