2 * realms.c Realm handling code
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2007 The FreeRADIUS server project
21 * Copyright 2007 Alan DeKok <aland@deployingradius.com>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/rad_assert.h>
34 #ifdef HAVE_PCREPOSIX_H
35 #include <pcreposix.h>
41 * For POSIX Regular expressions.
42 * (0) Means no extended regular expressions.
43 * REG_EXTENDED means use extended regular expressions.
46 #define REG_EXTENDED (0)
59 static rbtree_t *realms_byname = NULL;
62 typedef struct realm_regex_t {
64 struct realm_regex_t *next;
67 static realm_regex_t *realms_regex = NULL;
69 #endif /* HAVE_REGEX_H */
71 typedef struct realm_config_t {
77 int wake_all_if_all_dead;
80 static realm_config_t *realm_config = NULL;
83 static rbtree_t *home_servers_byaddr = NULL;
84 static rbtree_t *home_servers_byname = NULL;
86 static int home_server_max_number = 0;
87 static rbtree_t *home_servers_bynumber = NULL;
90 static rbtree_t *home_pools_byname = NULL;
93 * Map the proxy server configuration parameters to variables.
95 static const CONF_PARSER proxy_config[] = {
96 { "retry_delay", PW_TYPE_INTEGER,
97 offsetof(realm_config_t, retry_delay),
98 NULL, Stringify(RETRY_DELAY) },
100 { "retry_count", PW_TYPE_INTEGER,
101 offsetof(realm_config_t, retry_count),
102 NULL, Stringify(RETRY_COUNT) },
104 { "default_fallback", PW_TYPE_BOOLEAN,
105 offsetof(realm_config_t, fallback),
108 { "dead_time", PW_TYPE_INTEGER,
109 offsetof(realm_config_t, dead_time),
110 NULL, Stringify(DEAD_TIME) },
112 { "wake_all_if_all_dead", PW_TYPE_BOOLEAN,
113 offsetof(realm_config_t, wake_all_if_all_dead),
116 { NULL, -1, 0, NULL, NULL }
120 static int realm_name_cmp(const void *one, const void *two)
122 const REALM *a = one;
123 const REALM *b = two;
125 return strcasecmp(a->name, b->name);
130 static void home_server_free(void *data)
132 home_server *home = data;
137 static int home_server_name_cmp(const void *one, const void *two)
139 const home_server *a = one;
140 const home_server *b = two;
142 if (a->type < b->type) return -1;
143 if (a->type > b->type) return +1;
145 return strcasecmp(a->name, b->name);
148 static int home_server_addr_cmp(const void *one, const void *two)
151 const home_server *a = one;
152 const home_server *b = two;
154 if (a->server && !b->server) return -1;
155 if (!a->server && b->server) return +1;
156 if (a->server && b->server) {
157 rcode = a->type - b->type;
158 if (rcode != 0) return rcode;
159 return strcmp(a->server, b->server);
162 if (a->port < b->port) return -1;
163 if (a->port > b->port) return +1;
166 if (a->proto < b->proto) return -1;
167 if (a->proto > b->proto) return +1;
170 rcode = fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
171 if (rcode != 0) return rcode;
173 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
177 static int home_server_number_cmp(const void *one, const void *two)
179 const home_server *a = one;
180 const home_server *b = two;
182 return (a->number - b->number);
186 static int home_pool_name_cmp(const void *one, const void *two)
188 const home_pool_t *a = one;
189 const home_pool_t *b = two;
191 if (a->server_type < b->server_type) return -1;
192 if (a->server_type > b->server_type) return +1;
194 return strcasecmp(a->name, b->name);
198 static size_t xlat_cs(CONF_SECTION *cs, const char *fmt, char *out, size_t outlen)
201 const char *value = NULL;
206 if (strcmp(fmt, "instance") == 0) {
207 value = cf_section_name2(cs);
215 cp = cf_pair_find(cs, fmt);
216 if (!cp || !(value = cf_pair_value(cp))) {
222 strlcpy(out, value, outlen);
229 * Xlat for %{home_server:foo}
231 static size_t xlat_home_server(UNUSED void *instance, REQUEST *request,
232 const char *fmt, char *out, size_t outlen)
234 if (!fmt || !out || (outlen < 1)) return 0;
236 if (!request || !request->home_server) {
241 return xlat_cs(request->home_server->cs, fmt, out, outlen);
246 * Xlat for %{home_server_pool:foo}
248 static size_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
249 const char *fmt, char *out, size_t outlen)
251 if (!fmt || !out || (outlen < 1)) return 0;
253 if (!request || !request->home_pool) {
258 return xlat_cs(request->home_pool->cs, fmt, out, outlen);
262 void realms_free(void)
266 rbtree_free(home_servers_bynumber);
267 home_servers_bynumber = NULL;
270 rbtree_free(home_servers_byname);
271 home_servers_byname = NULL;
273 rbtree_free(home_servers_byaddr);
274 home_servers_byaddr = NULL;
276 rbtree_free(home_pools_byname);
277 home_pools_byname = NULL;
280 rbtree_free(realms_byname);
281 realms_byname = NULL;
285 realm_regex_t *this, *next;
287 for (this = realms_regex; this != NULL; this = next) {
302 static CONF_PARSER limit_config[] = {
303 { "max_connections", PW_TYPE_INTEGER,
304 offsetof(home_server, limit.max_connections), NULL, "16" },
306 { "max_requests", PW_TYPE_INTEGER,
307 offsetof(home_server, limit.max_requests), NULL, "0" },
309 { "lifetime", PW_TYPE_INTEGER,
310 offsetof(home_server, limit.lifetime), NULL, "0" },
312 { "idle_timeout", PW_TYPE_INTEGER,
313 offsetof(home_server, limit.idle_timeout), NULL, "0" },
315 { NULL, -1, 0, NULL, NULL } /* end the list */
318 static struct in_addr hs_ip4addr;
319 static struct in6_addr hs_ip6addr;
320 static char *hs_srcipaddr = NULL;
321 static char *hs_type = NULL;
322 static char *hs_check = NULL;
323 static char *hs_virtual_server = NULL;
325 static char *hs_proto = NULL;
329 static CONF_PARSER home_server_coa[] = {
330 { "irt", PW_TYPE_INTEGER,
331 offsetof(home_server, coa_irt), 0, Stringify(2) },
332 { "mrt", PW_TYPE_INTEGER,
333 offsetof(home_server, coa_mrt), 0, Stringify(16) },
334 { "mrc", PW_TYPE_INTEGER,
335 offsetof(home_server, coa_mrc), 0, Stringify(5) },
336 { "mrd", PW_TYPE_INTEGER,
337 offsetof(home_server, coa_mrd), 0, Stringify(30) },
339 { NULL, -1, 0, NULL, NULL } /* end the list */
343 static CONF_PARSER home_server_config[] = {
344 { "ipaddr", PW_TYPE_IPADDR,
345 0, &hs_ip4addr, NULL },
346 { "ipv6addr", PW_TYPE_IPV6ADDR,
347 0, &hs_ip6addr, NULL },
348 { "virtual_server", PW_TYPE_STRING_PTR,
349 0, &hs_virtual_server, NULL },
351 { "port", PW_TYPE_INTEGER,
352 offsetof(home_server,port), NULL, "0" },
354 { "type", PW_TYPE_STRING_PTR,
358 { "proto", PW_TYPE_STRING_PTR,
359 0, &hs_proto, NULL },
362 { "secret", PW_TYPE_STRING_PTR,
363 offsetof(home_server,secret), NULL, NULL},
365 { "src_ipaddr", PW_TYPE_STRING_PTR,
366 0, &hs_srcipaddr, NULL },
368 { "response_window", PW_TYPE_INTEGER,
369 offsetof(home_server,response_window), NULL, "30" },
370 { "max_outstanding", PW_TYPE_INTEGER,
371 offsetof(home_server,max_outstanding), NULL, "65536" },
373 { "zombie_period", PW_TYPE_INTEGER,
374 offsetof(home_server,zombie_period), NULL, "40" },
375 { "status_check", PW_TYPE_STRING_PTR,
376 0, &hs_check, "none" },
377 { "ping_check", PW_TYPE_STRING_PTR,
378 0, &hs_check, NULL },
380 { "ping_interval", PW_TYPE_INTEGER,
381 offsetof(home_server,ping_interval), NULL, "30" },
382 { "check_interval", PW_TYPE_INTEGER,
383 offsetof(home_server,ping_interval), NULL, "30" },
384 { "num_answers_to_alive", PW_TYPE_INTEGER,
385 offsetof(home_server,num_pings_to_alive), NULL, "3" },
386 { "revive_interval", PW_TYPE_INTEGER,
387 offsetof(home_server,revive_interval), NULL, "300" },
388 { "status_check_timeout", PW_TYPE_INTEGER,
389 offsetof(home_server,ping_timeout), NULL, "4" },
391 { "username", PW_TYPE_STRING_PTR,
392 offsetof(home_server,ping_user_name), NULL, NULL},
393 { "password", PW_TYPE_STRING_PTR,
394 offsetof(home_server,ping_user_password), NULL, NULL},
397 { "historic_average_window", PW_TYPE_INTEGER,
398 offsetof(home_server,ema.window), NULL, NULL },
402 { "coa", PW_TYPE_SUBSECTION, 0, NULL, (const void *) home_server_coa },
405 { "limit", PW_TYPE_SUBSECTION, 0, NULL, (const void *) limit_config },
407 { NULL, -1, 0, NULL, NULL } /* end the list */
411 static void null_free(UNUSED void *data)
415 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs)
423 hs_virtual_server = NULL;
425 name2 = cf_section_name2(cs);
428 "Home server section is missing a name.");
432 home = rad_malloc(sizeof(*home));
433 memset(home, 0, sizeof(*home));
437 home->state = HOME_STATE_UNKNOWN;
440 * Last packet sent / received are zero.
443 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
444 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
445 if (cf_section_parse(cs, home, home_server_config) < 0) {
450 * Figure out which one to use.
452 if (cf_pair_find(cs, "ipaddr")) {
453 home->ipaddr.af = AF_INET;
454 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
456 } else if (cf_pair_find(cs, "ipv6addr")) {
457 home->ipaddr.af = AF_INET6;
458 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
460 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
461 home->ipaddr.af = AF_UNSPEC;
462 home->server = cf_pair_value(cp);
465 "Invalid value for virtual_server");
469 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
472 "No such server %s", home->server);
477 * When CoA is used, the user has to specify the type
478 * of the home server, even when they point to
481 home->secret = strdup("");
486 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
489 talloc_free(hs_type);
499 if (!home->port || (home->port > 65535)) {
501 "No port, or invalid port defined for home server %s.",
508 "Fatal error! Home server %s is ourselves!",
515 "No shared secret defined for home server %s.",
521 * Use a reasonable default.
524 if (!hs_type) hs_type = talloc_strdup(cs, "auth+acct");
526 if (strcasecmp(hs_type, "auth") == 0) {
527 home->type = HOME_TYPE_AUTH;
529 } else if (strcasecmp(hs_type, "acct") == 0) {
530 home->type = HOME_TYPE_ACCT;
532 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
533 home->type = HOME_TYPE_AUTH;
537 } else if (strcasecmp(hs_type, "coa") == 0) {
538 home->type = HOME_TYPE_COA;
541 if (home->server != NULL) {
543 "Home servers of type \"coa\" cannot point to a virtual server");
550 "Invalid type \"%s\" for home server %s.",
554 if (hs_type) talloc_free(hs_type);
557 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
558 home->ping_check = HOME_PING_CHECK_NONE;
560 } else if (strcasecmp(hs_check, "status-server") == 0) {
561 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
563 } else if (strcasecmp(hs_check, "request") == 0) {
564 home->ping_check = HOME_PING_CHECK_REQUEST;
566 if (!home->ping_user_name ||
567 !*home->ping_user_name) {
568 cf_log_err_cs(cs, "You must supply a 'username' to enable status_check=request");
572 if ((home->type == HOME_TYPE_AUTH) &&
573 (!home->ping_user_password ||
574 !*home->ping_user_password)) {
575 cf_log_err_cs(cs, "You must supply a password to enable status_check=request");
581 "Invalid status__check \"%s\" for home server %s.",
587 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
588 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
589 if (!home->ping_user_name) {
590 cf_log_err_cs(cs, "You must supply a user name to enable status_check=request");
594 if ((home->type == HOME_TYPE_AUTH) &&
595 !home->ping_user_password) {
596 cf_log_err_cs(cs, "You must supply a password to enable status_check=request");
601 home->proto = IPPROTO_UDP;
604 if (strcmp(hs_proto, "udp") == 0) {
607 } else if (strcmp(hs_proto, "tcp") == 0) {
609 home->proto = IPPROTO_TCP;
611 if (home->ping_check != HOME_PING_CHECK_NONE) {
613 "Only 'status_check = none' is allowed for home servers with 'proto = tcp'");
619 "Unknown proto \"%s\".", hs_proto);
626 rbtree_finddata(home_servers_byaddr, home)) {
627 cf_log_err_cs(cs, "Duplicate home server");
632 * Check the TLS configuration.
634 tls = cf_section_sub_find(cs, "tls");
637 * If the home is a virtual server, don't look up source IP.
640 rad_assert(home->ipaddr.af != AF_UNSPEC);
643 * Otherwise look up the source IP using the same
644 * address family as the destination IP.
647 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
648 cf_log_err_cs(cs, "Failed parsing src_ipaddr");
654 * Source isn't specified: Source is
655 * the correct address family, but all zeros.
657 memset(&home->src_ipaddr, 0, sizeof(home->src_ipaddr));
658 home->src_ipaddr.af = home->ipaddr.af;
661 if (tls && (home->proto != IPPROTO_TCP)) {
662 cf_log_err_cs(cs, "TLS transport is not available for UDP sockets.");
669 cf_log_err_cs(cs, "TLS transport is not available in this executable.");
674 * Parse the SSL client configuration.
677 home->tls = tls_client_conf_parse(tls);
685 cf_log_err_cs(cs, "Virtual home_servers cannot have a \"tls\" subsection");
690 * Make sure that this is set.
692 if (home->src_ipaddr.af == AF_UNSPEC) {
693 home->src_ipaddr.af = home->ipaddr.af;
698 if (rbtree_finddata(home_servers_byname, home) != NULL) {
700 "Duplicate home server name %s.", name2);
705 (rbtree_finddata(home_servers_byaddr, home) != NULL)) {
707 "Duplicate home server IP %s.", name2);
711 if (!rbtree_insert(home_servers_byname, home)) {
713 "Internal error %d adding home server %s.",
719 !rbtree_insert(home_servers_byaddr, home)) {
720 rbtree_deletebydata(home_servers_byname, home);
722 "Internal error %d adding home server %s.",
728 home->number = home_server_max_number++;
729 if (!rbtree_insert(home_servers_bynumber, home)) {
730 rbtree_deletebydata(home_servers_byname, home);
731 if (home->ipaddr.af != AF_UNSPEC) {
732 rbtree_deletebydata(home_servers_byname, home);
735 "Internal error %d adding home server %s.",
741 if (home->max_outstanding < 8) home->max_outstanding = 8;
742 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
744 if (home->ping_interval < 6) home->ping_interval = 6;
745 if (home->ping_interval > 120) home->ping_interval = 120;
747 if (home->response_window < 1) home->response_window = 1;
748 if (home->response_window > 60) home->response_window = 60;
749 if (home->response_window > mainconfig.max_request_time) home->response_window = mainconfig.max_request_time;
751 if (home->zombie_period < 1) home->zombie_period = 1;
752 if (home->zombie_period > 120) home->zombie_period = 120;
754 if (home->zombie_period < home->response_window) {
755 home->zombie_period = home->response_window;
758 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
759 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
761 if (home->ping_timeout < 3) home->ping_timeout = 3;
762 if (home->ping_timeout > 10) home->ping_timeout = 10;
764 if (home->revive_interval < 60) home->revive_interval = 60;
765 if (home->revive_interval > 3600) home->revive_interval = 3600;
768 if (home->coa_irt < 1) home->coa_irt = 1;
769 if (home->coa_irt > 5) home->coa_irt = 5;
771 if (home->coa_mrc < 0) home->coa_mrc = 0;
772 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
774 if (home->coa_mrt < 0) home->coa_mrt = 0;
775 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
777 if (home->coa_mrd < 5) home->coa_mrd = 5;
778 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
781 if (home->limit.max_connections > 1024) home->limit.max_connections = 1024;
785 * UDP sockets can't be connection limited.
787 if (home->proto != IPPROTO_TCP) home->limit.max_connections = 0;
790 if ((home->limit.idle_timeout > 0) && (home->limit.idle_timeout < 5))
791 home->limit.idle_timeout = 5;
792 if ((home->limit.lifetime > 0) && (home->limit.lifetime < 5))
793 home->limit.lifetime = 5;
794 if ((home->limit.lifetime > 0) && (home->limit.idle_timeout > home->limit.lifetime))
795 home->limit.idle_timeout = 0;
797 tls = cf_item_parent(cf_sectiontoitem(cs));
798 if (strcmp(cf_section_name1(tls), "server") == 0) {
799 home->parent_server = cf_section_name2(tls);
803 home_server *home2 = rad_malloc(sizeof(*home2));
805 memcpy(home2, home, sizeof(*home2));
807 home2->type = HOME_TYPE_ACCT;
809 home2->ping_user_password = NULL;
811 home2->parent_server = home->parent_server;
813 if (!rbtree_insert(home_servers_byname, home2)) {
815 "Internal error %d adding home server %s.",
822 !rbtree_insert(home_servers_byaddr, home2)) {
823 rbtree_deletebydata(home_servers_byname, home2);
825 "Internal error %d adding home server %s.",
832 home2->number = home_server_max_number++;
833 if (!rbtree_insert(home_servers_bynumber, home2)) {
834 rbtree_deletebydata(home_servers_byname, home2);
835 if (!home2->server) {
836 rbtree_deletebydata(home_servers_byname, home2);
839 "Internal error %d adding home server %s.",
848 * Mark it as already processed
850 cf_data_add(cs, "home_server", null_free, null_free);
856 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
857 int server_type, int num_home_servers)
861 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
863 if (!pool) return NULL; /* just for pairanoia */
865 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
870 pool->server_type = server_type;
871 pool->num_home_servers = num_home_servers;
876 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
877 const char *name, int server_type,
880 home_server myhome, *home;
881 CONF_SECTION *server_cs;
885 "No value given for home_server.");
890 myhome.type = server_type;
891 home = rbtree_finddata(home_servers_byname, &myhome);
897 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
900 "Unknown home_server \"%s\".", name);
904 home = rbtree_finddata(home_servers_byname, &myhome);
907 "Internal error %d adding home server \"%s\".",
917 static int server_pool_add(realm_config_t *rc,
918 CONF_SECTION *cs, int server_type, int do_print)
921 home_pool_t *pool = NULL;
924 int num_home_servers;
927 name2 = cf_section_name1(cs);
928 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
929 (strcasecmp(name2, "home_server_pool") != 0))) {
931 "Section is not a home_server_pool.");
935 name2 = cf_section_name2(cs);
938 "Server pool section is missing a name.");
943 * Count the home servers and initalize them.
945 num_home_servers = 0;
946 for (cp = cf_pair_find(cs, "home_server");
948 cp = cf_pair_find_next(cs, cp, "home_server")) {
951 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
952 server_type, &home)) {
957 if (num_home_servers == 0) {
959 "No home servers defined in pool %s",
964 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
970 * Fallback servers must be defined, and must be
973 cp = cf_pair_find(cs, "fallback");
976 if (server_type == HOME_TYPE_COA) {
977 cf_log_err_cs(cs, "Home server pools of type \"coa\" cannot have a fallback virtual server.");
982 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
983 server_type, &pool->fallback)) {
988 if (!pool->fallback->server) {
989 cf_log_err_cs(cs, "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
994 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
996 cp = cf_pair_find(cs, "type");
998 static FR_NAME_NUMBER pool_types[] = {
999 { "load-balance", HOME_POOL_LOAD_BALANCE },
1000 { "fail-over", HOME_POOL_FAIL_OVER },
1001 { "round_robin", HOME_POOL_LOAD_BALANCE },
1002 { "fail_over", HOME_POOL_FAIL_OVER },
1003 { "client-balance", HOME_POOL_CLIENT_BALANCE },
1004 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
1005 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
1009 value = cf_pair_value(cp);
1012 "No value given for type.");
1016 pool->type = fr_str2int(pool_types, value, 0);
1019 "Unknown type \"%s\".",
1024 if (do_print) cf_log_info(cs, "\ttype = %s", value);
1027 cp = cf_pair_find(cs, "virtual_server");
1029 pool->virtual_server = cf_pair_value(cp);
1030 if (!pool->virtual_server) {
1031 cf_log_err_cp(cp, "No value given for virtual_server");
1036 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
1039 if (!cf_section_sub_find_name2(rc->cs, "server",
1040 pool->virtual_server)) {
1041 cf_log_err_cp(cp, "No such server %s",
1042 pool->virtual_server);
1048 num_home_servers = 0;
1049 for (cp = cf_pair_find(cs, "home_server");
1051 cp = cf_pair_find_next(cs, cp, "home_server")) {
1054 value = cf_pair_value(cp);
1056 memset(&myhome, 0, sizeof(myhome));
1057 myhome.name = value;
1058 myhome.type = server_type;
1060 home = rbtree_finddata(home_servers_byname, &myhome);
1062 DEBUG2("Internal sanity check failed");
1067 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
1071 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1072 pool->servers[num_home_servers++] = home;
1073 } /* loop over home_server's */
1075 if (pool->fallback && do_print) {
1076 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1079 if (!rbtree_insert(home_pools_byname, pool)) {
1080 rad_assert("Internal sanity check failed");
1084 if (do_print) cf_log_info(cs, " }");
1086 cf_data_add(cs, "home_server_pool", pool, free);
1088 rad_assert(pool->server_type != 0);
1093 if (do_print) cf_log_info(cs, " }");
1099 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1101 const char *name, const char *secret,
1102 home_pool_type_t ldflag, home_pool_t **pool_p,
1103 int type, const char *server)
1106 int i, insert_point, num_home_servers;
1107 home_server myhome, *home;
1108 home_pool_t mypool, *pool;
1109 CONF_SECTION *subcs;
1111 (void) rc; /* -Wunused */
1120 * LOCAL realms get sanity checked, and nothing else happens.
1122 if (strcmp(name, "LOCAL") == 0) {
1124 cf_log_err_cs(cs, "Realm \"%s\" cannot be both LOCAL and remote", name);
1131 return 0; /* Not proxying. Can't do non-LOCAL realms */
1134 mypool.name = realm;
1135 mypool.server_type = type;
1136 pool = rbtree_finddata(home_pools_byname, &mypool);
1138 if (pool->type != ldflag) {
1139 cf_log_err_cs(cs, "Inconsistent ldflag for server pool \"%s\"", name);
1143 if (pool->server_type != type) {
1144 cf_log_err_cs(cs, "Inconsistent home server type for server pool \"%s\"", name);
1151 home = rbtree_finddata(home_servers_byname, &myhome);
1153 if (secret && (strcmp(home->secret, secret) != 0)) {
1154 cf_log_err_cs(cs, "Inconsistent shared secret for home server \"%s\"", name);
1158 if (home->type != type) {
1159 cf_log_err_cs(cs, "Inconsistent type for home server \"%s\"", name);
1164 * See if the home server is already listed
1165 * in the pool. If so, do nothing else.
1167 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1168 if (pool->servers[i] == home) {
1175 * If we do have a pool, check that there is room to
1176 * insert the home server we've found, or the one that we
1179 * Note that we insert it into the LAST available
1180 * position, in order to maintain the same order as in
1181 * the configuration files.
1185 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1186 if (pool->servers[i]) break;
1188 if (!pool->servers[i]) {
1193 if (insert_point < 0) {
1194 cf_log_err_cs(cs, "No room in pool to add home server \"%s\". Please update the realm configuration to use the new-style home servers and server pools.", name);
1200 * No home server, allocate one.
1206 home = rad_malloc(sizeof(*home));
1207 memset(home, 0, sizeof(*home));
1210 home->hostname = name;
1212 home->secret = secret;
1214 home->proto = IPPROTO_UDP;
1216 p = strchr(name, ':');
1218 if (type == HOME_TYPE_AUTH) {
1219 home->port = PW_AUTH_UDP_PORT;
1221 home->port = PW_ACCT_UDP_PORT;
1227 } else if (p == name) {
1229 "Invalid hostname %s.",
1235 home->port = atoi(p + 1);
1236 if ((home->port == 0) || (home->port > 65535)) {
1244 q = rad_malloc((p - name) + 1);
1245 memcpy(q, name, (p - name));
1251 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1253 "Failed looking up hostname %s.",
1259 home->src_ipaddr.af = home->ipaddr.af;
1261 home->ipaddr.af = AF_UNSPEC;
1262 home->server = server;
1267 * Use the old-style configuration.
1269 home->max_outstanding = 65535*16;
1270 home->zombie_period = rc->retry_delay * rc->retry_count;
1271 if (home->zombie_period == 0) home->zombie_period =30;
1272 home->response_window = home->zombie_period - 1;
1274 home->ping_check = HOME_PING_CHECK_NONE;
1276 home->revive_interval = rc->dead_time;
1278 if (rbtree_finddata(home_servers_byaddr, home)) {
1279 cf_log_err_cs(cs, "Home server %s has the same IP address and/or port as another home server.", name);
1284 if (!rbtree_insert(home_servers_byname, home)) {
1285 cf_log_err_cs(cs, "Internal error %d adding home server %s.", __LINE__, name);
1290 if (!rbtree_insert(home_servers_byaddr, home)) {
1291 rbtree_deletebydata(home_servers_byname, home);
1292 cf_log_err_cs(cs, "Internal error %d adding home server %s.", __LINE__, name);
1298 home->number = home_server_max_number++;
1299 if (!rbtree_insert(home_servers_bynumber, home)) {
1300 rbtree_deletebydata(home_servers_byname, home);
1301 if (home->ipaddr.af != AF_UNSPEC) {
1302 rbtree_deletebydata(home_servers_byname, home);
1305 "Internal error %d adding home server %s.",
1314 * We now have a home server, see if we can insert it
1315 * into pre-existing pool.
1317 if (insert_point >= 0) {
1318 rad_assert(pool != NULL);
1319 pool->servers[insert_point] = home;
1323 rad_assert(pool == NULL);
1324 rad_assert(home != NULL);
1327 * Count the old-style realms of this name.
1329 num_home_servers = 0;
1330 for (subcs = cf_section_find_next(cs, NULL, "realm");
1332 subcs = cf_section_find_next(cs, subcs, "realm")) {
1333 const char *this = cf_section_name2(subcs);
1335 if (!this || (strcmp(this, realm) != 0)) continue;
1339 if (num_home_servers == 0) {
1340 cf_log_err_cs(cs, "Internal error counting pools for home server %s.", name);
1345 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1348 pool->servers[0] = home;
1350 if (!rbtree_insert(home_pools_byname, pool)) {
1351 rad_assert("Internal sanity check failed");
1361 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1364 const char *secret = NULL;
1365 home_pool_type_t ldflag;
1368 cp = cf_pair_find(cs, "ldflag");
1369 ldflag = HOME_POOL_FAIL_OVER;
1371 host = cf_pair_value(cp);
1373 cf_log_err_cp(cp, "No value specified for ldflag");
1377 if (strcasecmp(host, "fail_over") == 0) {
1378 cf_log_info(cs, "\tldflag = fail_over");
1380 } else if (strcasecmp(host, "round_robin") == 0) {
1381 ldflag = HOME_POOL_LOAD_BALANCE;
1382 cf_log_info(cs, "\tldflag = round_robin");
1385 cf_log_err_cs(cs, "Unknown value \"%s\" for ldflag", host);
1388 } /* else don't print it. */
1391 * Allow old-style if it doesn't exist, or if it exists and
1394 cp = cf_pair_find(cs, "authhost");
1396 host = cf_pair_value(cp);
1398 cf_log_err_cp(cp, "No value specified for authhost");
1402 if (strcmp(host, "LOCAL") != 0) {
1403 cp = cf_pair_find(cs, "secret");
1405 cf_log_err_cs(cs, "No shared secret supplied for realm: %s", r->name);
1409 secret = cf_pair_value(cp);
1411 cf_log_err_cp(cp, "No value specified for secret");
1416 cf_log_info(cs, "\tauthhost = %s", host);
1418 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1419 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1424 cp = cf_pair_find(cs, "accthost");
1426 host = cf_pair_value(cp);
1428 cf_log_err_cp(cp, "No value specified for accthost");
1433 * Don't look for a secret again if it was found
1436 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1437 cp = cf_pair_find(cs, "secret");
1439 cf_log_err_cs(cs, "No shared secret supplied for realm: %s", r->name);
1443 secret = cf_pair_value(cp);
1445 cf_log_err_cp(cp, "No value specified for secret");
1450 cf_log_info(cs, "\taccthost = %s", host);
1452 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1453 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1458 cp = cf_pair_find(cs, "virtual_server");
1460 host = cf_pair_value(cp);
1462 cf_log_err_cp(cp, "No value specified for virtual_server");
1466 cf_log_info(cs, "\tvirtual_server = %s", host);
1468 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1469 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1472 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1473 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1478 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1486 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1487 const char *name, home_pool_t **dest,
1488 int server_type, int do_print)
1490 home_pool_t mypool, *pool;
1493 mypool.server_type = server_type;
1495 pool = rbtree_finddata(home_pools_byname, &mypool);
1497 CONF_SECTION *pool_cs;
1499 pool_cs = cf_section_sub_find_name2(rc->cs,
1503 pool_cs = cf_section_sub_find_name2(rc->cs,
1508 cf_log_err_cs(cs, "Failed to find home_server_pool \"%s\"", name);
1512 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1516 pool = rbtree_finddata(home_pools_byname, &mypool);
1518 DEBUGE("Internal sanity check failed in add_pool_to_realm");
1523 if (pool->server_type != server_type) {
1524 cf_log_err_cs(cs, "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1535 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1541 home_pool_t *auth_pool, *acct_pool;
1542 const char *auth_pool_name, *acct_pool_name;
1544 const char *coa_pool_name;
1545 home_pool_t *coa_pool;
1549 name2 = cf_section_name1(cs);
1550 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1551 cf_log_err_cs(cs, "Section is not a realm.");
1555 name2 = cf_section_name2(cs);
1557 cf_log_err_cs(cs, "Realm section is missing the realm name.");
1562 auth_pool = acct_pool = NULL;
1563 auth_pool_name = acct_pool_name = NULL;
1566 coa_pool_name = NULL;
1570 * Prefer new configuration to old one.
1572 cp = cf_pair_find(cs, "pool");
1573 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1574 if (cp) auth_pool_name = cf_pair_value(cp);
1575 if (cp && auth_pool_name) {
1576 acct_pool_name = auth_pool_name;
1577 if (!add_pool_to_realm(rc, cs,
1578 auth_pool_name, &auth_pool,
1579 HOME_TYPE_AUTH, 1)) {
1582 if (!add_pool_to_realm(rc, cs,
1583 auth_pool_name, &acct_pool,
1584 HOME_TYPE_ACCT, 0)) {
1589 cp = cf_pair_find(cs, "auth_pool");
1590 if (cp) auth_pool_name = cf_pair_value(cp);
1591 if (cp && auth_pool_name) {
1593 cf_log_err_cs(cs, "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1596 if (!add_pool_to_realm(rc, cs,
1597 auth_pool_name, &auth_pool,
1598 HOME_TYPE_AUTH, 1)) {
1603 cp = cf_pair_find(cs, "acct_pool");
1604 if (cp) acct_pool_name = cf_pair_value(cp);
1605 if (cp && acct_pool_name) {
1606 int do_print = TRUE;
1609 cf_log_err_cs(cs, "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1615 (strcmp(auth_pool_name, acct_pool_name) != 0))) {
1619 if (!add_pool_to_realm(rc, cs,
1620 acct_pool_name, &acct_pool,
1621 HOME_TYPE_ACCT, do_print)) {
1627 cp = cf_pair_find(cs, "coa_pool");
1628 if (cp) coa_pool_name = cf_pair_value(cp);
1629 if (cp && coa_pool_name) {
1630 int do_print = TRUE;
1632 if (!add_pool_to_realm(rc, cs,
1633 coa_pool_name, &coa_pool,
1634 HOME_TYPE_COA, do_print)) {
1641 cf_log_info(cs, " realm %s {", name2);
1645 * The realm MAY already exist if it's an old-style realm.
1646 * In that case, merge the old-style realm with this one.
1648 r = realm_find2(name2);
1649 if (r && (strcmp(r->name, name2) == 0)) {
1650 if (cf_pair_find(cs, "auth_pool") ||
1651 cf_pair_find(cs, "acct_pool")) {
1652 cf_log_err_cs(cs, "Duplicate realm \"%s\"", name2);
1656 if (!old_realm_config(rc, cs, r)) {
1660 cf_log_info(cs, " } # realm %s", name2);
1666 if (name2[0] == '~') {
1671 * Include substring matches.
1673 rcode = regcomp(®, name2 + 1,
1674 REG_EXTENDED | REG_NOSUB | REG_ICASE);
1678 regerror(rcode, ®, buffer, sizeof(buffer));
1681 "Invalid regex \"%s\": %s",
1689 r = rad_malloc(sizeof(*r));
1690 memset(r, 0, sizeof(*r));
1695 r->auth_pool = auth_pool;
1696 r->acct_pool = acct_pool;
1698 r->coa_pool = coa_pool;
1701 if (auth_pool_name &&
1702 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1703 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1705 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1706 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1708 if (coa_pool_name) cf_log_info(cs, "\tcoa_pool = %s", coa_pool_name);
1713 cp = cf_pair_find(cs, "nostrip");
1714 if (cp && (cf_pair_value(cp) == NULL)) {
1716 cf_log_info(cs, "\tnostrip");
1720 * We're a new-style realm. Complain if we see the old
1723 if (r->auth_pool || r->acct_pool) {
1724 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1725 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1726 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1727 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1728 DEBUG2W("Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1733 * The realm MAY be an old-style realm, as there
1734 * was no auth_pool or acct_pool. Double-check
1735 * it, just to be safe.
1737 } else if (!old_realm_config(rc, cs, r)) {
1743 * It's a regex. Add it to a separate list.
1745 if (name2[0] == '~') {
1746 realm_regex_t *rr, **last;
1748 rr = rad_malloc(sizeof(*rr));
1750 last = &realms_regex;
1751 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1759 cf_log_info(cs, " }");
1764 if (!rbtree_insert(realms_byname, r)) {
1765 rad_assert("Internal sanity check failed");
1769 cf_log_info(cs, " }");
1774 cf_log_info(cs, " } # realm %s", name2);
1780 static const FR_NAME_NUMBER home_server_types[] = {
1781 { "auth", HOME_TYPE_AUTH },
1782 { "auth+acct", HOME_TYPE_AUTH },
1783 { "acct", HOME_TYPE_ACCT },
1784 { "coa", HOME_TYPE_COA },
1788 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1791 const char *name, *type;
1793 CONF_SECTION *server_cs;
1795 cp = cf_pair_find(cs, "home_server");
1797 cf_log_err_cs(cs, "Pool does not contain a \"home_server\" entry");
1798 return HOME_TYPE_INVALID;
1801 name = cf_pair_value(cp);
1803 cf_log_err_cp(cp, "home_server entry does not reference a home server");
1804 return HOME_TYPE_INVALID;
1807 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1809 cf_log_err_cp(cp, "home_server \"%s\" does not exist", name);
1810 return HOME_TYPE_INVALID;
1813 cp = cf_pair_find(server_cs, "type");
1815 cf_log_err_cs(server_cs, "home_server %s does not contain a \"type\" entry", name);
1816 return HOME_TYPE_INVALID;
1819 type = cf_pair_value(cp);
1821 cf_log_err_cs(server_cs, "home_server %s contains an empty \"type\" entry", name);
1822 return HOME_TYPE_INVALID;
1825 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1826 if (home == HOME_TYPE_INVALID) {
1827 cf_log_err_cs(server_cs, "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1828 return HOME_TYPE_INVALID;
1831 return home; /* 'cause we miss it so much */
1835 int realms_init(CONF_SECTION *config)
1839 CONF_SECTION *server_cs;
1841 realm_config_t *rc, *old_rc;
1843 if (realms_byname) return 1;
1845 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1846 if (!realms_byname) {
1852 home_servers_byaddr = rbtree_create(home_server_addr_cmp, home_server_free, 0);
1853 if (!home_servers_byaddr) {
1858 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1859 if (!home_servers_byname) {
1865 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1866 if (!home_servers_bynumber) {
1872 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1873 if (!home_pools_byname) {
1879 rc = rad_malloc(sizeof(*rc));
1880 memset(rc, 0, sizeof(*rc));
1884 cs = cf_subsection_find_next(config, NULL, "proxy");
1886 if (cf_section_parse(cs, rc, proxy_config) < 0) {
1887 DEBUGE("Failed parsing proxy section");
1894 rc->dead_time = DEAD_TIME;
1895 rc->retry_count = RETRY_COUNT;
1896 rc->retry_delay = RETRY_DELAY;
1898 rc->wake_all_if_all_dead= 0;
1901 for (cs = cf_subsection_find_next(config, NULL, "home_server");
1903 cs = cf_subsection_find_next(config, cs, "home_server")) {
1904 if (!home_server_add(rc, cs)) {
1912 * Loop over virtual servers to find homes which are
1915 for (server_cs = cf_subsection_find_next(config, NULL, "server");
1917 server_cs = cf_subsection_find_next(config, server_cs, "server")) {
1918 for (cs = cf_subsection_find_next(server_cs, NULL, "home_server");
1920 cs = cf_subsection_find_next(server_cs, cs, "home_server")) {
1921 if (!home_server_add(rc, cs)) {
1930 for (cs = cf_subsection_find_next(config, NULL, "realm");
1932 cs = cf_subsection_find_next(config, cs, "realm")) {
1933 if (!realm_add(rc, cs)) {
1942 * CoA pools aren't necessarily tied to realms.
1944 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1946 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1950 * Pool was already loaded.
1952 if (cf_data_find(cs, "home_server_pool")) continue;
1954 type = pool_peek_type(config, cs);
1955 if (type == HOME_TYPE_INVALID) {
1961 if (!server_pool_add(rc, cs, type, TRUE)) {
1971 xlat_register("home_server", xlat_home_server, NULL, NULL);
1972 xlat_register("home_server_pool", xlat_server_pool, NULL, NULL);
1976 * Swap pointers atomically.
1978 old_rc = realm_config;
1986 * Find a realm where "name" might be the regex.
1988 REALM *realm_find2(const char *name)
1993 if (!name) name = "NULL";
1995 myrealm.name = name;
1996 realm = rbtree_finddata(realms_byname, &myrealm);
1997 if (realm) return realm;
2001 realm_regex_t *this;
2003 for (this = realms_regex; this != NULL; this = this->next) {
2004 if (strcmp(this->realm->name, name) == 0) {
2012 * Couldn't find a realm. Look for DEFAULT.
2014 myrealm.name = "DEFAULT";
2015 return rbtree_finddata(realms_byname, &myrealm);
2020 * Find a realm in the REALM list.
2022 REALM *realm_find(const char *name)
2027 if (!name) name = "NULL";
2029 myrealm.name = name;
2030 realm = rbtree_finddata(realms_byname, &myrealm);
2031 if (realm) return realm;
2035 realm_regex_t *this;
2037 for (this = realms_regex; this != NULL; this = this->next) {
2042 * Include substring matches.
2044 if (regcomp(®, this->realm->name + 1,
2045 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
2049 compare = regexec(®, name, 0, NULL, 0);
2052 if (compare == 0) return this->realm;
2058 * Couldn't find a realm. Look for DEFAULT.
2060 myrealm.name = "DEFAULT";
2061 return rbtree_finddata(realms_byname, &myrealm);
2068 * Allocate the proxy list if it doesn't already exist, and copy request
2069 * VPs into it. Setup src/dst IP addresses based on home server, and
2070 * calculate and add the message-authenticator.
2072 * This is a distinct function from home_server_ldb, as not all home_server
2073 * lookups result in the *CURRENT* request being proxied,
2074 * as in rlm_replicate, and this may trigger asserts elsewhere in the
2077 void home_server_update_request(home_server *home, REQUEST *request)
2081 * Allocate the proxy packet, only if it wasn't
2082 * already allocated by a module. This check is
2083 * mainly to support the proxying of EAP-TTLS and
2084 * EAP-PEAP tunneled requests.
2086 * In those cases, the EAP module creates a
2087 * "fake" request, and recursively passes it
2088 * through the authentication stage of the
2089 * server. The module then checks if the request
2090 * was supposed to be proxied, and if so, creates
2091 * a proxy packet from the TUNNELED request, and
2092 * not from the EAP request outside of the
2095 * The proxy then works like normal, except that
2096 * the response packet is "eaten" by the EAP
2097 * module, and encapsulated into an EAP packet.
2099 if (!request->proxy) {
2100 request->proxy = rad_alloc(request, TRUE);
2101 if (!request->proxy) {
2102 DEBUGE("no memory");
2107 * Copy the request, then look up name
2108 * and plain-text password in the copy.
2110 * Note that the User-Name attribute is
2111 * the *original* as sent over by the
2112 * client. The Stripped-User-Name
2113 * attribute is the one hacked through
2116 request->proxy->vps = paircopy(request->proxy,
2117 request->packet->vps);
2121 * Update the various fields as appropriate.
2123 request->proxy->src_ipaddr = home->src_ipaddr;
2124 request->proxy->src_port = 0;
2125 request->proxy->dst_ipaddr = home->ipaddr;
2126 request->proxy->dst_port = home->port;
2127 request->home_server = home;
2130 * Access-Requests have a Message-Authenticator added,
2131 * unless one already exists.
2133 if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2134 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY)) {
2135 pairmake(request->proxy, &request->proxy->vps,
2136 "Message-Authenticator", "0x00",
2141 home_server *home_server_ldb(const char *realmname,
2142 home_pool_t *pool, REQUEST *request)
2146 home_server *found = NULL;
2147 home_server *zombie = NULL;
2151 * Determine how to pick choose the home server.
2153 switch (pool->type) {
2157 * For load-balancing by client IP address, we
2158 * pick a home server by hashing the client IP.
2160 * This isn't as even a load distribution as
2161 * tracking the State attribute, but it's better
2164 case HOME_POOL_CLIENT_BALANCE:
2165 switch (request->packet->src_ipaddr.af) {
2167 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2168 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2171 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2172 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2178 start = hash % pool->num_home_servers;
2181 case HOME_POOL_CLIENT_PORT_BALANCE:
2182 switch (request->packet->src_ipaddr.af) {
2184 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2185 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2188 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2189 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2195 fr_hash_update(&request->packet->src_port,
2196 sizeof(request->packet->src_port), hash);
2197 start = hash % pool->num_home_servers;
2200 case HOME_POOL_KEYED_BALANCE:
2201 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY, 0, TAG_ANY)) != NULL) {
2202 hash = fr_hash(vp->vp_strvalue, vp->length);
2203 start = hash % pool->num_home_servers;
2208 case HOME_POOL_LOAD_BALANCE:
2209 case HOME_POOL_FAIL_OVER:
2213 default: /* this shouldn't happen... */
2220 * Starting with the home server we chose, loop through
2221 * all home servers. If the current one is dead, skip
2222 * it. If it is too busy, skip it.
2224 * Otherwise, use it.
2226 for (count = 0; count < pool->num_home_servers; count++) {
2227 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
2229 if (!home) continue;
2232 * Skip dead home servers.
2234 * Home servers that are unknown, alive, or zombie
2235 * are used for proxying.
2237 if (home->state == HOME_STATE_IS_DEAD) {
2242 * This home server is too busy. Choose another one.
2244 if (home->currently_outstanding >= home->max_outstanding) {
2250 * We read the packet from a detail file, AND it
2251 * came from this server. Don't re-proxy it
2254 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2255 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
2256 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2262 * Default virtual: ignore homes tied to a
2265 if (!request->server && home->parent_server) {
2270 * A virtual AND home is tied to virtual,
2271 * ignore ones which don't match.
2273 if (request->server && home->parent_server &&
2274 strcmp(request->server, home->parent_server) != 0) {
2279 * Allow request->server && !home->parent_server
2281 * i.e. virtuals can proxy to globally defined
2286 * It's zombie, so we remember the first zombie
2287 * we find, but we don't mark it as a "live"
2290 if (home->state == HOME_STATE_ZOMBIE) {
2291 if (!zombie) zombie = home;
2296 * We've found the first "live" one. Use that.
2298 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2304 * Otherwise we're doing some kind of load balancing.
2305 * If we haven't found one yet, pick this one.
2312 RDEBUG3("PROXY %s %d\t%s %d",
2313 found->name, found->currently_outstanding,
2314 home->name, home->currently_outstanding);
2317 * Prefer this server if it's less busy than the
2318 * one we had previously found.
2320 if (home->currently_outstanding < found->currently_outstanding) {
2321 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2322 home->name, found->name);
2328 * Ignore servers which are busier than the one
2331 if (home->currently_outstanding > found->currently_outstanding) {
2332 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2333 home->name, found->name);
2338 * From the list of servers which have the same
2339 * load, choose one at random.
2341 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2344 } /* loop over the home servers */
2347 * We have no live servers, BUT we have a zombie. Use
2348 * the zombie as a last resort.
2350 if (!found && zombie) {
2356 * There's a fallback if they're all dead.
2358 if (!found && pool->fallback) {
2359 found = pool->fallback;
2361 DEBUGW("Home server pool %s failing over to fallback %s",
2362 pool->name, found->server);
2363 if (pool->in_fallback) goto update_and_return;
2365 pool->in_fallback = TRUE;
2368 * Run the trigger once an hour saying that
2371 if ((pool->time_all_dead + 3600) < request->timestamp) {
2372 pool->time_all_dead = request->timestamp;
2373 exec_trigger(request, pool->cs, "home_server_pool.fallback", FALSE);
2379 if ((found != pool->fallback) && pool->in_fallback) {
2380 pool->in_fallback = FALSE;
2381 exec_trigger(request, pool->cs, "home_server_pool.normal", FALSE);
2388 * No live match found, and no fallback to the "DEFAULT"
2389 * realm. We fix this by blindly marking all servers as
2390 * "live". But only do it for ones that don't support
2391 * "pings", as they will be marked live when they
2392 * actually are live.
2394 if (!realm_config->fallback &&
2395 realm_config->wake_all_if_all_dead) {
2396 for (count = 0; count < pool->num_home_servers; count++) {
2397 home_server *home = pool->servers[count];
2399 if (!home) continue;
2401 if ((home->state == HOME_STATE_IS_DEAD) &&
2402 (home->ping_check == HOME_PING_CHECK_NONE)) {
2403 home->state = HOME_STATE_ALIVE;
2404 if (!found) found = home;
2408 if (found) goto update_and_return;
2412 * Still nothing. Look up the DEFAULT realm, but only
2413 * if we weren't looking up the NULL or DEFAULT realms.
2415 if (realm_config->fallback &&
2417 (strcmp(realmname, "NULL") != 0) &&
2418 (strcmp(realmname, "DEFAULT") != 0)) {
2419 REALM *rd = realm_find("DEFAULT");
2421 if (!rd) return NULL;
2424 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2425 pool = rd->auth_pool;
2427 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2428 pool = rd->acct_pool;
2430 if (!pool) return NULL;
2432 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2433 return home_server_ldb(rd->name, pool, request);
2437 * Still haven't found anything. Oh well.
2443 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port, int proto)
2447 memset(&myhome, 0, sizeof(myhome));
2448 myhome.ipaddr = *ipaddr;
2449 myhome.src_ipaddr.af = ipaddr->af;
2452 myhome.proto = proto;
2454 myhome.proto = IPPROTO_UDP;
2456 myhome.server = NULL; /* we're not called for internal proxying */
2458 return rbtree_finddata(home_servers_byaddr, &myhome);
2462 home_server *home_server_byname(const char *name, int type)
2466 memset(&myhome, 0, sizeof(myhome));
2470 return rbtree_finddata(home_servers_byname, &myhome);
2475 home_server *home_server_bynumber(int number)
2479 memset(&myhome, 0, sizeof(myhome));
2480 myhome.number = number;
2481 myhome.server = NULL; /* we're not called for internal proxying */
2483 return rbtree_finddata(home_servers_bynumber, &myhome);
2487 home_pool_t *home_pool_byname(const char *name, int type)
2491 memset(&mypool, 0, sizeof(mypool));
2493 mypool.server_type = type;
2494 return rbtree_finddata(home_pools_byname, &mypool);