Forward specifcied RADIUS attributes between AAA backend and client
[strongswan.git] / src / libcharon / plugins / eap_radius / eap_radius.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "eap_radius.h"
17 #include "eap_radius_forward.h"
18
19 #include "radius_message.h"
20 #include "radius_client.h"
21
22 #include <daemon.h>
23
24 #define TUNNEL_TYPE_ESP 9
25
26 typedef struct private_eap_radius_t private_eap_radius_t;
27
28 /**
29 * Private data of an eap_radius_t object.
30 */
31 struct private_eap_radius_t {
32
33 /**
34 * Public authenticator_t interface.
35 */
36 eap_radius_t public;
37
38 /**
39 * ID of the server
40 */
41 identification_t *server;
42
43 /**
44 * ID of the peer
45 */
46 identification_t *peer;
47
48 /**
49 * EAP method type we are proxying
50 */
51 eap_type_t type;
52
53 /**
54 * EAP vendor, if any
55 */
56 u_int32_t vendor;
57
58 /**
59 * EAP message identifier
60 */
61 u_int8_t identifier;
62
63 /**
64 * RADIUS client instance
65 */
66 radius_client_t *client;
67
68 /**
69 * TRUE to use EAP-Start, FALSE to send EAP-Identity Response directly
70 */
71 bool eap_start;
72
73 /**
74 * Prefix to prepend to EAP identity
75 */
76 char *id_prefix;
77
78 /**
79 * Handle the Class attribute as group membership information?
80 */
81 bool class_group;
82
83 /**
84 * Handle the Filter-Id attribute as IPsec CHILD_SA name?
85 */
86 bool filter_id;
87 };
88
89 /**
90 * Add EAP-Identity to RADIUS message
91 */
92 static void add_eap_identity(private_eap_radius_t *this,
93 radius_message_t *request)
94 {
95 struct {
96 /** EAP code (REQUEST/RESPONSE) */
97 u_int8_t code;
98 /** unique message identifier */
99 u_int8_t identifier;
100 /** length of whole message */
101 u_int16_t length;
102 /** EAP type */
103 u_int8_t type;
104 /** identity data */
105 u_int8_t data[];
106 } __attribute__((__packed__)) *hdr;
107 chunk_t id, prefix;
108 size_t len;
109
110 id = this->peer->get_encoding(this->peer);
111 prefix = chunk_create(this->id_prefix, strlen(this->id_prefix));
112 len = sizeof(*hdr) + prefix.len + id.len;
113
114 hdr = alloca(len);
115 hdr->code = EAP_RESPONSE;
116 hdr->identifier = this->identifier;
117 hdr->length = htons(len);
118 hdr->type = EAP_IDENTITY;
119 memcpy(hdr->data, prefix.ptr, prefix.len);
120 memcpy(hdr->data + prefix.len, id.ptr, id.len);
121
122 request->add(request, RAT_EAP_MESSAGE, chunk_create((u_char*)hdr, len));
123 }
124
125 /**
126 * Copy EAP-Message attribute from RADIUS message to an new EAP payload
127 */
128 static bool radius2ike(private_eap_radius_t *this,
129 radius_message_t *msg, eap_payload_t **out)
130 {
131 enumerator_t *enumerator;
132 eap_payload_t *payload;
133 chunk_t data, message = chunk_empty;
134 int type;
135
136 enumerator = msg->create_enumerator(msg);
137 while (enumerator->enumerate(enumerator, &type, &data))
138 {
139 if (type == RAT_EAP_MESSAGE && data.len)
140 {
141 message = chunk_cat("mc", message, data);
142 }
143 }
144 enumerator->destroy(enumerator);
145 if (message.len)
146 {
147 *out = payload = eap_payload_create_data(message);
148
149 /* apply EAP method selected by RADIUS server */
150 this->type = payload->get_type(payload, &this->vendor);
151
152 DBG3(DBG_IKE, "%N payload %B", eap_type_names, this->type, &message);
153 free(message.ptr);
154 return TRUE;
155 }
156 return FALSE;
157 }
158
159 METHOD(eap_method_t, initiate, status_t,
160 private_eap_radius_t *this, eap_payload_t **out)
161 {
162 radius_message_t *request, *response;
163 status_t status = FAILED;
164 chunk_t username;
165
166 request = radius_message_create(RMC_ACCESS_REQUEST);
167 username = chunk_create(this->id_prefix, strlen(this->id_prefix));
168 username = chunk_cata("cc", username, this->peer->get_encoding(this->peer));
169 request->add(request, RAT_USER_NAME, username);
170
171 if (this->eap_start)
172 {
173 request->add(request, RAT_EAP_MESSAGE, chunk_empty);
174 }
175 else
176 {
177 add_eap_identity(this, request);
178 }
179 eap_radius_forward_from_ike(request);
180
181 response = this->client->request(this->client, request);
182 if (response)
183 {
184 eap_radius_forward_to_ike(response);
185 if (radius2ike(this, response, out))
186 {
187 status = NEED_MORE;
188 }
189 response->destroy(response);
190 }
191 request->destroy(request);
192 return status;
193 }
194
195 /**
196 * Handle the Class attribute as group membership information
197 */
198 static void process_class(private_eap_radius_t *this, radius_message_t *msg)
199 {
200 enumerator_t *enumerator;
201 chunk_t data;
202 int type;
203
204 enumerator = msg->create_enumerator(msg);
205 while (enumerator->enumerate(enumerator, &type, &data))
206 {
207 if (type == RAT_CLASS)
208 {
209 identification_t *id;
210 ike_sa_t *ike_sa;
211 auth_cfg_t *auth;
212
213 if (data.len >= 44)
214 { /* quirk: ignore long class attributes, these are used for
215 * other purposes by some RADIUS servers (such as NPS). */
216 continue;
217 }
218
219 ike_sa = charon->bus->get_sa(charon->bus);
220 if (ike_sa)
221 {
222 auth = ike_sa->get_auth_cfg(ike_sa, FALSE);
223 id = identification_create_from_data(data);
224 DBG1(DBG_CFG, "received group membership '%Y' from RADIUS", id);
225 auth->add(auth, AUTH_RULE_GROUP, id);
226 }
227 }
228 }
229 enumerator->destroy(enumerator);
230 }
231
232 /**
233 * Handle the Filter-Id attribute as IPsec CHILD_SA name
234 */
235 static void process_filter_id(private_eap_radius_t *this, radius_message_t *msg)
236 {
237 enumerator_t *enumerator;
238 int type;
239 u_int8_t tunnel_tag;
240 u_int32_t tunnel_type;
241 chunk_t filter_id = chunk_empty, data;
242 bool is_esp_tunnel = FALSE;
243
244 enumerator = msg->create_enumerator(msg);
245 while (enumerator->enumerate(enumerator, &type, &data))
246 {
247 switch (type)
248 {
249 case RAT_TUNNEL_TYPE:
250 if (data.len != 4)
251 {
252 continue;
253 }
254 tunnel_tag = *data.ptr;
255 *data.ptr = 0x00;
256 tunnel_type = untoh32(data.ptr);
257 DBG1(DBG_IKE, "received RADIUS attribute Tunnel-Type: "
258 "tag = %u, value = %u", tunnel_tag, tunnel_type);
259 is_esp_tunnel = (tunnel_type == TUNNEL_TYPE_ESP);
260 break;
261 case RAT_FILTER_ID:
262 filter_id = data;
263 DBG1(DBG_IKE, "received RADIUS attribute Filter-Id: "
264 "'%.*s'", filter_id.len, filter_id.ptr);
265 break;
266 default:
267 break;
268 }
269 }
270 enumerator->destroy(enumerator);
271
272 if (is_esp_tunnel && filter_id.len)
273 {
274 identification_t *id;
275 ike_sa_t *ike_sa;
276 auth_cfg_t *auth;
277
278 ike_sa = charon->bus->get_sa(charon->bus);
279 if (ike_sa)
280 {
281 auth = ike_sa->get_auth_cfg(ike_sa, FALSE);
282 id = identification_create_from_data(filter_id);
283 auth->add(auth, AUTH_RULE_GROUP, id);
284 }
285 }
286 }
287
288 /**
289 * Handle Session-Timeout attribte
290 */
291 static void process_timeout(private_eap_radius_t *this, radius_message_t *msg)
292 {
293 enumerator_t *enumerator;
294 ike_sa_t *ike_sa;
295 chunk_t data;
296 int type;
297
298 enumerator = msg->create_enumerator(msg);
299 while (enumerator->enumerate(enumerator, &type, &data))
300 {
301 if (type == RAT_SESSION_TIMEOUT && data.len == 4)
302 {
303 ike_sa = charon->bus->get_sa(charon->bus);
304 if (ike_sa)
305 {
306 ike_sa->set_auth_lifetime(ike_sa, untoh32(data.ptr));
307 }
308 }
309 }
310 enumerator->destroy(enumerator);
311 }
312
313 METHOD(eap_method_t, process, status_t,
314 private_eap_radius_t *this, eap_payload_t *in, eap_payload_t **out)
315 {
316 radius_message_t *request, *response;
317 status_t status = FAILED;
318 chunk_t data;
319
320 request = radius_message_create(RMC_ACCESS_REQUEST);
321 request->add(request, RAT_USER_NAME, this->peer->get_encoding(this->peer));
322 data = in->get_data(in);
323 DBG3(DBG_IKE, "%N payload %B", eap_type_names, this->type, &data);
324
325 /* fragment data suitable for RADIUS (not more than 253 bytes) */
326 while (data.len > 253)
327 {
328 request->add(request, RAT_EAP_MESSAGE, chunk_create(data.ptr, 253));
329 data = chunk_skip(data, 253);
330 }
331 request->add(request, RAT_EAP_MESSAGE, data);
332
333 eap_radius_forward_from_ike(request);
334 response = this->client->request(this->client, request);
335 if (response)
336 {
337 eap_radius_forward_to_ike(response);
338 switch (response->get_code(response))
339 {
340 case RMC_ACCESS_CHALLENGE:
341 if (radius2ike(this, response, out))
342 {
343 status = NEED_MORE;
344 break;
345 }
346 status = FAILED;
347 break;
348 case RMC_ACCESS_ACCEPT:
349 if (this->class_group)
350 {
351 process_class(this, response);
352 }
353 if (this->filter_id)
354 {
355 process_filter_id(this, response);
356 }
357 process_timeout(this, response);
358 DBG1(DBG_IKE, "RADIUS authentication of '%Y' successful",
359 this->peer);
360 status = SUCCESS;
361 break;
362 case RMC_ACCESS_REJECT:
363 default:
364 DBG1(DBG_IKE, "RADIUS authentication of '%Y' failed", this->peer);
365 status = FAILED;
366 break;
367 }
368 response->destroy(response);
369 }
370 request->destroy(request);
371 return status;
372 }
373
374 METHOD(eap_method_t, get_type, eap_type_t,
375 private_eap_radius_t *this, u_int32_t *vendor)
376 {
377 *vendor = this->vendor;
378 return this->type;
379 }
380
381 METHOD(eap_method_t, get_msk, status_t,
382 private_eap_radius_t *this, chunk_t *out)
383 {
384 chunk_t msk;
385
386 msk = this->client->get_msk(this->client);
387 if (msk.len)
388 {
389 *out = msk;
390 return SUCCESS;
391 }
392 return FAILED;
393 }
394
395 METHOD(eap_method_t, get_identifier, u_int8_t,
396 private_eap_radius_t *this)
397 {
398 return this->identifier;
399 }
400
401 METHOD(eap_method_t, set_identifier, void,
402 private_eap_radius_t *this, u_int8_t identifier)
403 {
404 this->identifier = identifier;
405 }
406
407 METHOD(eap_method_t, is_mutual, bool,
408 private_eap_radius_t *this)
409 {
410 switch (this->type)
411 {
412 case EAP_AKA:
413 case EAP_SIM:
414 return TRUE;
415 default:
416 return FALSE;
417 }
418 }
419
420 METHOD(eap_method_t, destroy, void,
421 private_eap_radius_t *this)
422 {
423 this->peer->destroy(this->peer);
424 this->server->destroy(this->server);
425 this->client->destroy(this->client);
426 free(this);
427 }
428
429 /**
430 * Generic constructor
431 */
432 eap_radius_t *eap_radius_create(identification_t *server, identification_t *peer)
433 {
434 private_eap_radius_t *this;
435
436 INIT(this,
437 .public = {
438 .eap_method = {
439 .initiate = _initiate,
440 .process = _process,
441 .get_type = _get_type,
442 .is_mutual = _is_mutual,
443 .get_msk = _get_msk,
444 .get_identifier = _get_identifier,
445 .set_identifier = _set_identifier,
446 .destroy = _destroy,
447 },
448 },
449 /* initially EAP_RADIUS, but is set to the method selected by RADIUS */
450 .type = EAP_RADIUS,
451 .eap_start = lib->settings->get_bool(lib->settings,
452 "charon.plugins.eap-radius.eap_start", FALSE),
453 .id_prefix = lib->settings->get_str(lib->settings,
454 "charon.plugins.eap-radius.id_prefix", ""),
455 .class_group = lib->settings->get_bool(lib->settings,
456 "charon.plugins.eap-radius.class_group", FALSE),
457 .filter_id = lib->settings->get_bool(lib->settings,
458 "charon.plugins.eap-radius.filter_id", FALSE),
459
460 );
461 this->client = radius_client_create();
462 if (!this->client)
463 {
464 free(this);
465 return NULL;
466 }
467 this->peer = peer->clone(peer);
468 this->server = server->clone(server);
469 return &this->public;
470 }
471