1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
/*
* mem.c Memory allocation, deallocation stuff.
*
* Version: $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2000,2001,2006 The FreeRADIUS server project
* Copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
*/
RCSID("$Id$")
#include <stdio.h>
#include "rlm_eap.h"
#ifdef WITH_TLS
#include <freeradius-devel/tls.h>
#endif
#ifdef HAVE_PTHREAD_H
#define PTHREAD_MUTEX_LOCK pthread_mutex_lock
#define PTHREAD_MUTEX_UNLOCK pthread_mutex_unlock
#else
#define PTHREAD_MUTEX_LOCK(_x)
#define PTHREAD_MUTEX_UNLOCK(_x)
#endif
static eap_handler_t *eaplist_delete(rlm_eap_t *inst, REQUEST *request,
eap_handler_t *handler, char const *msg, bool delete);
/*
* Allocate a new eap_packet_t
*/
EAP_DS *eap_ds_alloc(eap_handler_t *handler)
{
EAP_DS *eap_ds;
eap_ds = talloc_zero(handler, EAP_DS);
eap_ds->response = talloc_zero(eap_ds, eap_packet_t);
if (!eap_ds->response) {
eap_ds_free(&eap_ds);
return NULL;
}
eap_ds->request = talloc_zero(eap_ds, eap_packet_t);
if (!eap_ds->response) {
eap_ds_free(&eap_ds);
return NULL;
}
return eap_ds;
}
void eap_ds_free(EAP_DS **eap_ds_p)
{
EAP_DS *eap_ds;
if (!eap_ds_p) return;
eap_ds = *eap_ds_p;
if (!eap_ds) return;
if (eap_ds->response) talloc_free(eap_ds->response);
if (eap_ds->request) talloc_free(eap_ds->request);
talloc_free(eap_ds);
*eap_ds_p = NULL;
}
static int _eap_handler_free(eap_handler_t *handler)
{
if (handler->prev_eapds) eap_ds_free(&(handler->prev_eapds));
if (handler->eap_ds) eap_ds_free(&(handler->eap_ds));
if ((handler->opaque) && (handler->free_opaque)) {
handler->free_opaque(handler->opaque);
handler->opaque = NULL;
}
handler->opaque = NULL;
handler->free_opaque = NULL;
/*
* Give helpful debug messages if:
*
* we're debugging TLS sessions, which don't finish,
* and which aren't deleted early due to a likely RADIUS
* retransmit which nukes our ID, and therefore our stare.
*/
if (fr_debug_lvl && handler->tls && !handler->finished &&
(time(NULL) > (handler->timestamp + 3))) {
WARN("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
WARN("!! EAP session with state 0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x did not finish! !!",
handler->state[0], handler->state[1],
handler->state[2], handler->state[3],
handler->state[4], handler->state[5],
handler->state[6], handler->state[7],
handler->state[8], handler->state[9],
handler->state[10], handler->state[11],
handler->state[12], handler->state[13],
handler->state[14], handler->state[15]);
WARN("!! Please read http://wiki.freeradius.org/guide/Certificate_Compatibility !!");
WARN("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
return 0;
}
/*
* Allocate a new eap_handler_t
*/
eap_handler_t *eap_handler_alloc(rlm_eap_t *inst, REQUEST *request)
{
eap_handler_t *handler, *old;
char buffer[256];
handler = talloc_zero(NULL, eap_handler_t);
if (!handler) {
ERROR("Failed allocating handler");
return NULL;
}
handler->inst_holder = inst;
/* Doesn't need to be inside the critical region */
talloc_set_destructor(handler, _eap_handler_free);
if (!inst->dedup_tree) return handler;
if (radius_xlat(buffer, sizeof(buffer), request, inst->dedup_key, NULL, NULL) < 0) return handler;
handler->dedup = talloc_strdup(handler, buffer);
/*
* Delete any old handler
*/
PTHREAD_MUTEX_LOCK(&(inst->session_mutex));
old = rbtree_finddata(inst->dedup_tree, handler);
if (old) (void) eaplist_delete(inst, request, old, "Cancelling", true);
PTHREAD_MUTEX_UNLOCK(&(inst->session_mutex));
return handler;
}
void eaplist_free(rlm_eap_t *inst)
{
eap_handler_t *node, *next;
for (node = inst->session_head; node != NULL; node = next) {
next = node->next;
talloc_free(node);
}
inst->session_head = inst->session_tail = NULL;
}
/*
* Return a 32-bit random number.
*/
static uint32_t eap_rand(fr_randctx *ctx)
{
uint32_t num;
num = ctx->randrsl[ctx->randcnt++];
if (ctx->randcnt >= 256) {
ctx->randcnt = 0;
fr_isaac(ctx);
}
return num;
}
static eap_handler_t *eaplist_delete(rlm_eap_t *inst, REQUEST *request,
eap_handler_t *handler, char const *msg, bool delete)
{
rbnode_t *node;
if (delete && inst->dedup_tree) (void) rbtree_deletebydata(inst->dedup_tree, handler);
node = rbtree_find(inst->session_tree, handler);
if (!node) return NULL;
handler = rbtree_node2data(inst->session_tree, node);
RDEBUG("%s EAP session with state "
"0x%02x%02x%02x%02x%02x%02x%02x%02x", msg,
handler->state[0], handler->state[1],
handler->state[2], handler->state[3],
handler->state[4], handler->state[5],
handler->state[6], handler->state[7]);
/*
* Delete old handler from the tree.
*/
rbtree_delete(inst->session_tree, node);
/*
* And unsplice it from the linked list.
*/
if (handler->prev) {
handler->prev->next = handler->next;
} else {
inst->session_head = handler->next;
}
if (handler->next) {
handler->next->prev = handler->prev;
} else {
inst->session_tail = handler->prev;
}
handler->prev = handler->next = NULL;
if (!delete) return handler;
#ifdef WITH_TLS
/*
* Remove expired TLS sessions.
*/
switch (handler->type) {
case PW_EAP_TLS:
case PW_EAP_TTLS:
case PW_EAP_PEAP:
case PW_EAP_FAST:
tls_fail(handler->opaque); /* MUST be a tls_session! */
break;
default:
break;
}
#endif
talloc_free(handler);
return NULL;
}
static void eaplist_expire(rlm_eap_t *inst, REQUEST *request, time_t timestamp)
{
int i;
eap_handler_t *handler;
/*
* Check the first few handlers in the list, and delete
* them if they're too old. We don't need to check them
* all, as incoming requests will quickly cause older
* handlers to be deleted.
*
*/
for (i = 0; i < 3; i++) {
handler = inst->session_head;
if (!handler) break;
/*
* Expire entries from the start of the list.
* They should be the oldest ones.
*/
if ((timestamp - handler->timestamp) > (int)inst->timer_limit) {
(void) eaplist_delete(inst, request, handler, "Expiring", true);
} else {
break;
}
}
}
/*
* Add a handler to the set of active sessions.
*
* Since we're adding it to the list, we guess that this means
* the packet needs a State attribute. So add one.
*/
int eaplist_add(rlm_eap_t *inst, eap_handler_t *handler)
{
int status = 0;
VALUE_PAIR *state;
REQUEST *request = handler->request;
/*
* Generate State, since we've been asked to add it to
* the list.
*/
state = pair_make_reply("State", NULL, T_OP_EQ);
if (!state) return 0;
/*
* The time at which this request was made was the time
* at which it was received by the RADIUS server.
*/
handler->timestamp = request->timestamp;
handler->status = 1;
handler->src_ipaddr = request->packet->src_ipaddr;
handler->eap_id = handler->eap_ds->request->id;
/*
* Playing with a data structure shared among threads
* means that we need a lock, to avoid conflict.
*/
PTHREAD_MUTEX_LOCK(&(inst->session_mutex));
/*
* If we have a DoS attack, discard new sessions.
*/
if (rbtree_num_elements(inst->session_tree) >= inst->max_sessions) {
status = -1;
eaplist_expire(inst, request, handler->timestamp);
goto done;
}
/*
* Create a unique content for the State variable.
* It will be modified slightly per round trip, but less so
* than in 1.x.
*/
if (handler->trips == 0) {
int i;
for (i = 0; i < 4; i++) {
uint32_t lvalue;
lvalue = eap_rand(&inst->rand_pool);
memcpy(handler->state + i * 4, &lvalue,
sizeof(lvalue));
}
}
/*
* Add some more data to distinguish the sessions.
*/
handler->state[4] = handler->trips ^ handler->state[0];
handler->state[5] = handler->eap_id ^ handler->state[1];
handler->state[6] = handler->type ^ handler->state[2];
handler->state[8] = handler->state[2] ^ (((uint32_t) HEXIFY(RADIUSD_VERSION)) & 0xff);
handler->state[10] = handler->state[2] ^ ((((uint32_t) HEXIFY(RADIUSD_VERSION)) >> 8) & 0xff);
handler->state[12] = handler->state[2] ^ ((((uint32_t) HEXIFY(RADIUSD_VERSION)) >> 16) & 0xff);
fr_pair_value_memcpy(state, handler->state, sizeof(handler->state));
/*
* Big-time failure.
*/
status = rbtree_insert(inst->session_tree, handler);
if (status) {
eap_handler_t *prev;
prev = inst->session_tail;
if (prev) {
prev->next = handler;
handler->prev = prev;
handler->next = NULL;
inst->session_tail = handler;
} else {
inst->session_head = inst->session_tail = handler;
handler->next = handler->prev = NULL;
}
}
/*
* Now that we've finished mucking with the list,
* unlock it.
*/
done:
/*
* We don't need this any more.
*/
if (status > 0) handler->request = NULL;
PTHREAD_MUTEX_UNLOCK(&(inst->session_mutex));
if (status <= 0) {
fr_pair_delete_by_num(&request->reply->vps, PW_STATE, 0, TAG_ANY);
if (status < 0) {
static time_t last_logged = 0;
if (last_logged < handler->timestamp) {
last_logged = handler->timestamp;
ERROR("rlm_eap (%s): Too many open sessions. Try increasing \"max_sessions\" "
"in the EAP module configuration", inst->xlat_name);
}
} else {
ERROR("rlm_eap (%s): Failed to store handler", inst->xlat_name);
}
return 0;
}
RDEBUG("EAP session adding &reply:State = 0x%02x%02x%02x%02x%02x%02x%02x%02x",
state->vp_octets[0], state->vp_octets[1], state->vp_octets[2], state->vp_octets[3],
state->vp_octets[4], state->vp_octets[5], state->vp_octets[6], state->vp_octets[7]);
return 1;
}
/*
* Find a a previous EAP-Request sent by us, which matches
* the current EAP-Response.
*
* Then, release the handle from the list, and return it to
* the caller.
*
* Also since we fill the eap_ds with the present EAP-Response we
* got to free the prev_eapds & move the eap_ds to prev_eapds
*/
eap_handler_t *eaplist_find(rlm_eap_t *inst, REQUEST *request,
eap_packet_raw_t *eap_packet)
{
VALUE_PAIR *state;
eap_handler_t *handler, myHandler;
/*
* We key the sessions off of the 'state' attribute, so it
* must exist.
*/
state = fr_pair_find_by_num(request->packet->vps, PW_STATE, 0, TAG_ANY);
if (!state) {
REDEBUG("EAP requires the State attribute to work, but no State exists in the Access-Request packet.");
REDEBUG("The RADIUS client is broken. No amount of changing FreeRADIUS will fix the RADIUS client.");
return NULL;
}
if (state->vp_length != EAP_STATE_LEN) {
REDEBUG("The RADIUS client has mangled the State attribute, OR you are forcing EAP in the wrong situation");
return NULL;
}
myHandler.src_ipaddr = request->packet->src_ipaddr;
myHandler.eap_id = eap_packet->id;
memcpy(myHandler.state, state->vp_strvalue, sizeof(myHandler.state));
/*
* Playing with a data structure shared among threads
* means that we need a lock, to avoid conflict.
*/
PTHREAD_MUTEX_LOCK(&(inst->session_mutex));
eaplist_expire(inst, request, request->timestamp);
handler = eaplist_delete(inst, request, &myHandler, "Removing", false);
PTHREAD_MUTEX_UNLOCK(&(inst->session_mutex));
/*
* Might not have been there.
*/
if (!handler) {
RERROR("rlm_eap (%s): No EAP session matching state "
"0x%02x%02x%02x%02x%02x%02x%02x%02x",
inst->xlat_name,
state->vp_octets[0], state->vp_octets[1],
state->vp_octets[2], state->vp_octets[3],
state->vp_octets[4], state->vp_octets[5],
state->vp_octets[6], state->vp_octets[7]);
return NULL;
}
if (handler->trips >= 50) {
RERROR("rlm_eap (%s): Aborting! More than 50 roundtrips "
"made in session with state "
"0x%02x%02x%02x%02x%02x%02x%02x%02x",
inst->xlat_name,
state->vp_octets[0], state->vp_octets[1],
state->vp_octets[2], state->vp_octets[3],
state->vp_octets[4], state->vp_octets[5],
state->vp_octets[6], state->vp_octets[7]);
talloc_free(handler);
return NULL;
}
handler->trips++;
RDEBUG("Previous EAP request found for state "
"0x%02x%02x%02x%02x%02x%02x%02x%02x, released from the list",
state->vp_octets[0], state->vp_octets[1],
state->vp_octets[2], state->vp_octets[3],
state->vp_octets[4], state->vp_octets[5],
state->vp_octets[6], state->vp_octets[7]);
/*
* Remember what the previous request was.
*/
eap_ds_free(&(handler->prev_eapds));
handler->prev_eapds = handler->eap_ds;
handler->eap_ds = NULL;
return handler;
}
|