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.
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.
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
20 * @brief Integration with external trust router code
22 * @copyright 2014 Network RADIUS SARL
24 #include <trust_router/tid.h>
25 #include <freeradius-devel/radiusd.h>
26 #include <freeradius-devel/rad_assert.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/realms.h>
30 #ifdef HAVE_TRUST_ROUTER_TR_DH_H
31 #include "trustrouter.h"
33 #include <trust_router/tr_dh.h>
34 static TIDC_INSTANCE *global_tidc = NULL;
47 if (global_tidc) return true;
49 global_tidc = tidc_create();
51 DEBUG2( "tr_init: Error creating global TIDC instance.\n");
55 if (!tidc_set_dh(global_tidc, tr_create_dh_params(NULL, 0))) {
56 DEBUG2( "tr_init: Error creating client DH params.\n");
63 static fr_tls_server_conf_t *construct_tls(TIDC_INSTANCE *inst,
67 fr_tls_server_conf_t *tls;
68 unsigned char *key_buf = NULL;
73 tls = talloc_zero( hs, fr_tls_server_conf_t);
74 if (!tls) return NULL;
76 aaa_server_dh = tid_srvr_get_dh(server);
77 keylen = tr_compute_dh_key(&key_buf, aaa_server_dh->pub_key,
84 hexbuf = talloc_size(tls, keylen*2 + 1);
85 if (!hexbuf) goto error;
87 tr_bin_to_hex(key_buf, keylen, hexbuf, 2*keylen + 1);
89 tls->psk_password = hexbuf;
90 tls->psk_identity = talloc_strdup(tls, tid_srvr_get_key_name(server)->buf);
92 tls->cipher_list = talloc_strdup(tls, "PSK");
93 tls->fragment_size = 4200;
94 tls->ctx = tls_init_ctx(tls, 1);
95 if (!tls->ctx) goto error;
97 memset(key_buf, 0, keylen);
103 memset(key_buf, 0, keylen);
106 if (hexbuf) memset(hexbuf, 0, keylen*2);
108 if (tls) talloc_free(tls);
112 static char *build_pool_name(TALLOC_CTX *ctx, TID_RESP *resp)
114 size_t index, sa_len, sl;
115 TID_SRVR_BLK *server;
116 char *pool_name = NULL;
118 const struct sockaddr *sa;
119 pool_name = talloc_strdup(ctx, "hp-");
121 tid_resp_servers_foreach(resp, server, index) {
122 tid_srvr_get_address(server, &sa, &sa_len);
123 if (0 != getnameinfo(sa, sa_len,
124 addr_buf, sizeof(addr_buf)-1,
125 NULL, 0, NI_NUMERICHOST)) {
126 DEBUG2("getnameinfo failed");
130 sl = strlen(addr_buf);
131 rad_assert(sl+2 <= sizeof(addr_buf));
134 addr_buf[sl+1] = '\0';
136 pool_name = talloc_strdup_append(pool_name, addr_buf);
142 static home_server_t *srvr_blk_to_home_server(TALLOC_CTX *ctx,
145 char const *realm_name)
147 home_server_t *hs = NULL;
148 const struct sockaddr *sa = NULL;
150 fr_ipaddr_t home_server_ip;
154 rad_assert(blk != NULL);
155 tid_srvr_get_address(blk, &sa, &sa_len);
156 switch (sa->sa_family) {
159 const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
160 home_server_ip.af = AF_INET;
161 home_server_ip.scope = 0;
162 home_server_ip.ipaddr.ip4addr = sin->sin_addr;
163 port = ntohs(sin->sin_port);
168 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *) sa;
169 home_server_ip.af = AF_INET6;
170 home_server_ip.scope = sin6->sin6_scope_id;
171 home_server_ip.ipaddr.ip6addr = sin6->sin6_addr;
176 DEBUG2("Unknown address family in tid srvr block");
180 if (0 != getnameinfo(sa, sa_len,
185 DEBUG2("getnameinfo failed");
189 hs = talloc_zero(ctx, home_server_t);
190 if (!hs) return NULL;
193 * All dynamic home servers are for authentication.
195 hs->type = HOME_TYPE_AUTH;
196 hs->ipaddr = home_server_ip;
197 hs->src_ipaddr.af = home_server_ip.af;
198 hs->log_name = talloc_asprintf(hs, "%s-for-%s", nametemp, realm_name);
199 hs->name = talloc_strdup(hs, nametemp);
201 hs->proto = IPPROTO_TCP;
202 hs->secret = talloc_strdup(hs, "radsec");
203 hs->response_window.tv_sec = 30;
204 hs->last_packet_recv = time(NULL);
206 hs->tls = construct_tls(inst, hs, blk);
207 if (!hs->tls) goto error;
209 realm_home_server_sanitize(hs, NULL);
217 static home_pool_t *servers_to_pool(TALLOC_CTX *ctx,
220 const char *realm_name)
222 home_pool_t *pool = NULL;
223 size_t num_servers = 0, index;
224 TID_SRVR_BLK *server = NULL;
226 num_servers = tid_resp_get_num_servers(resp);
228 pool = talloc_zero_size(ctx, sizeof(*pool) + num_servers *sizeof(home_server_t *));
229 if (!pool) goto error;
231 pool->type = HOME_POOL_CLIENT_PORT_BALANCE;
232 pool->server_type = HOME_TYPE_AUTH;
234 pool->name = build_pool_name(pool, resp);
235 if (!pool->name) goto error;
237 pool->num_home_servers = num_servers;
239 tid_resp_servers_foreach(resp, server, index) {
242 hs = srvr_blk_to_home_server(pool, inst, server, realm_name);
244 pool->servers[index] = hs;
250 if (pool) talloc_free(pool);
255 static void tr_response_func( TIDC_INSTANCE *inst,
256 UNUSED TID_REQ *req, TID_RESP *resp,
259 struct resp_opaque *opaque = (struct resp_opaque *) cookie;
260 REALM *nr = opaque->orig_realm;
262 if (tid_resp_get_result(resp) != TID_SUCCESS) {
265 opaque->result = tid_resp_get_result(resp);
266 memset(opaque->err_msg, 0, sizeof(opaque->err_msg));
268 if (tid_resp_get_err_msg(resp)) {
269 TR_NAME *err_msg = tid_resp_get_err_msg(resp);
270 err_msg_len = err_msg->len+1;
271 if (err_msg_len > sizeof(opaque->err_msg))
272 err_msg_len = sizeof(opaque->err_msg);
273 strlcpy(opaque->err_msg, err_msg->buf, err_msg_len);
279 nr = talloc_zero(NULL, REALM);
281 nr->name = talloc_move(nr, &opaque->fr_realm_name);
282 nr->auth_pool = servers_to_pool(nr, inst, resp, opaque->fr_realm_name);
283 if (!realm_realm_add(nr, NULL)) goto error;
286 home_pool_t *old_pool = nr->auth_pool;
287 home_pool_t *new_pool;
289 new_pool = servers_to_pool(nr, inst, resp, opaque->fr_realm_name);
291 ERROR("Unable to recreate pool for %s", opaque->fr_realm_name);
294 nr->auth_pool = new_pool;
297 * Mark the old pool as "to be freed"
299 realm_pool_free(old_pool);
302 opaque->output_realm = nr;
306 if (nr && !opaque->orig_realm) {
313 static bool update_required(REALM const *r)
315 const home_pool_t *pool;
317 const home_server_t *server;
318 time_t now = time(NULL);
321 * No pool. Not our realm.
323 if (!r->auth_pool) return false;
327 for (i = 0; i < pool->num_home_servers; i++) {
328 server = pool->servers[i];
331 * The realm was loaded from the configuration
334 if (server->cs) return false;
337 * These values don't make sense.
339 if ((server->last_packet_recv > (now + 5)) ||
340 (server->last_failed_open > (now + 5))) {
345 * This server has received a packet in the last
346 * 5 minutes. It doesn't need an update.
348 if ((now - server->last_packet_recv) < 300) {
353 * If we've opened in the last 10 minutes, then
354 * open rather than update.
356 if ((now - server->last_failed_open) > 600) {
366 REALM *tr_query_realm(char const *realm,
367 char const *community,
369 char const *trustrouter,
375 struct resp_opaque cookie;
377 if (!realm) return NULL;
379 /* clear the cookie structure */
380 memset (&cookie, 0, sizeof(cookie));
382 cookie.fr_realm_name = talloc_asprintf(NULL,
386 cookie.orig_realm = cookie.output_realm = realm_find(cookie.fr_realm_name);
388 if (cookie.orig_realm && !update_required(cookie.orig_realm)) {
389 talloc_free(cookie.fr_realm_name);
390 return cookie.orig_realm;
393 /* Set-up TID connection */
394 DEBUG2("Opening TIDC connection to %s:%u", trustrouter, port);
396 conn = tidc_open_connection(global_tidc, (char *)trustrouter, port, &gssctx);
399 DEBUG2("Error in tidc_open_connection.\n");
403 /* Send a TID request */
404 rcode = tidc_send_request(global_tidc, conn, gssctx, (char *)rprealm,
405 (char *) realm, (char *)community,
406 &tr_response_func, &cookie);
409 DEBUG2("Error in tidc_send_request, rc = %d.\n", rcode);
414 if (cookie.fr_realm_name)
415 talloc_free(cookie.fr_realm_name);
417 return cookie.output_realm;
419 #endif /* HAVE_TRUST_ROUTER_TR_DH_H */