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>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/rad_assert.h>
35 static rbtree_t *realms_byname = NULL;
37 static rbtree_t *home_servers_byaddr = NULL;
38 static rbtree_t *home_servers_byname = NULL;
40 static rbtree_t *home_pools_byname = NULL;
42 typedef struct realm_config_t {
48 int wake_all_if_all_dead;
51 static realm_config_t *realm_config = NULL;
54 * Map the proxy server configuration parameters to variables.
56 static const CONF_PARSER proxy_config[] = {
57 { "retry_delay", PW_TYPE_INTEGER,
58 offsetof(realm_config_t, retry_delay),
59 NULL, Stringify(RETRY_DELAY) },
61 { "retry_count", PW_TYPE_INTEGER,
62 offsetof(realm_config_t, retry_count),
63 NULL, Stringify(RETRY_COUNT) },
65 { "default_fallback", PW_TYPE_BOOLEAN,
66 offsetof(realm_config_t, fallback),
69 { "dead_time", PW_TYPE_INTEGER,
70 offsetof(realm_config_t, dead_time),
71 NULL, Stringify(DEAD_TIME) },
73 { "wake_all_if_all_dead", PW_TYPE_BOOLEAN,
74 offsetof(realm_config_t, wake_all_if_all_dead),
77 { NULL, -1, 0, NULL, NULL }
80 static int realm_name_cmp(const void *one, const void *two)
85 return strcasecmp(a->name, b->name);
89 static int home_server_name_cmp(const void *one, const void *two)
91 const home_server *a = one;
92 const home_server *b = two;
94 if (a->type < b->type) return -1;
95 if (a->type > b->type) return +1;
97 return strcasecmp(a->name, b->name);
100 static int home_server_addr_cmp(const void *one, const void *two)
102 const home_server *a = one;
103 const home_server *b = two;
105 if (a->port < b->port) return -1;
106 if (a->port > b->port) return +1;
108 return lrad_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
112 static int home_pool_name_cmp(const void *one, const void *two)
114 const home_pool_t *a = one;
115 const home_pool_t *b = two;
117 if (a->server_type < b->server_type) return -1;
118 if (a->server_type > b->server_type) return +1;
120 return strcasecmp(a->name, b->name);
125 * Xlat for %{home_server:foo}
127 static int xlat_home_server(UNUSED void *instance, REQUEST *request,
128 char *fmt, char *out, size_t outlen,
129 UNUSED RADIUS_ESCAPE_STRING func)
131 const char *value = NULL;
134 if (!fmt || !out || (outlen < 1)) return 0;
136 if (!request || !request->home_server) {
141 cp = cf_pair_find(request->home_server->cs, fmt);
142 if (!cp || !(value = cf_pair_value(cp))) {
147 strlcpy(out, value, outlen);
154 * Xlat for %{home_server_pool:foo}
156 static int xlat_server_pool(UNUSED void *instance, REQUEST *request,
157 char *fmt, char *out, size_t outlen,
158 UNUSED RADIUS_ESCAPE_STRING func)
160 const char *value = NULL;
163 if (!fmt || !out || (outlen < 1)) return 0;
165 if (!request || !request->home_pool) {
170 cp = cf_pair_find(request->home_pool->cs, fmt);
171 if (!cp || !(value = cf_pair_value(cp))) {
176 strlcpy(out, value, outlen);
182 void realms_free(void)
184 rbtree_free(home_servers_byname);
185 home_servers_byname = NULL;
187 rbtree_free(home_servers_byaddr);
188 home_servers_byaddr = NULL;
190 rbtree_free(home_pools_byname);
191 home_pools_byname = NULL;
193 rbtree_free(realms_byname);
194 realms_byname = NULL;
198 static struct in_addr hs_ip4addr;
199 static struct in6_addr hs_ip6addr;
200 static char *hs_type = NULL;
201 static char *hs_check = NULL;
203 static CONF_PARSER home_server_config[] = {
204 { "ipaddr", PW_TYPE_IPADDR,
205 0, &hs_ip4addr, NULL },
206 { "ipv6addr", PW_TYPE_IPV6ADDR,
207 0, &hs_ip6addr, NULL },
209 { "port", PW_TYPE_INTEGER,
210 offsetof(home_server,port), NULL, "0" },
212 { "type", PW_TYPE_STRING_PTR,
215 { "secret", PW_TYPE_STRING_PTR,
216 offsetof(home_server,secret), NULL, NULL},
218 { "response_window", PW_TYPE_INTEGER,
219 offsetof(home_server,response_window), NULL, "30" },
220 { "max_outstanding", PW_TYPE_INTEGER,
221 offsetof(home_server,max_outstanding), NULL, "65536" },
223 { "zombie_period", PW_TYPE_INTEGER,
224 offsetof(home_server,zombie_period), NULL, "40" },
225 { "status_check", PW_TYPE_STRING_PTR,
226 0, &hs_check, "none" },
227 { "ping_check", PW_TYPE_STRING_PTR,
228 0, &hs_check, "none" },
230 { "ping_interval", PW_TYPE_INTEGER,
231 offsetof(home_server,ping_interval), NULL, "30" },
232 { "check_interval", PW_TYPE_INTEGER,
233 offsetof(home_server,ping_interval), NULL, "30" },
234 { "num_answers_to_alive", PW_TYPE_INTEGER,
235 offsetof(home_server,num_pings_to_alive), NULL, "3" },
236 { "num_pings_to_alive", PW_TYPE_INTEGER,
237 offsetof(home_server,num_pings_to_alive), NULL, "3" },
238 { "revive_interval", PW_TYPE_INTEGER,
239 offsetof(home_server,revive_interval), NULL, "300" },
240 { "status_check_timeout", PW_TYPE_INTEGER,
241 offsetof(home_server,ping_timeout), NULL, "4" },
243 { "username", PW_TYPE_STRING_PTR,
244 offsetof(home_server,ping_user_name), NULL, NULL},
245 { "password", PW_TYPE_STRING_PTR,
246 offsetof(home_server,ping_user_password), NULL, NULL},
248 { NULL, -1, 0, NULL, NULL } /* end the list */
253 static int home_server_add(CONF_SECTION *cs, int type)
259 name2 = cf_section_name1(cs);
260 if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
261 cf_log_err(cf_sectiontoitem(cs),
262 "Section is not a home_server.");
266 name2 = cf_section_name2(cs);
268 cf_log_err(cf_sectiontoitem(cs),
269 "Home server section is missing a name.");
273 home = rad_malloc(sizeof(*home));
274 memset(home, 0, sizeof(*home));
279 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
280 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
281 cf_section_parse(cs, home, home_server_config);
283 if (!cf_pair_find(cs, "ipaddr") &&
284 !cf_pair_find(cs, "ipv6addr")) {
285 cf_log_err(cf_sectiontoitem(cs),
286 "No IPv4 or IPv6 address defined for home server %s.",
297 * Figure out which one to use.
299 if (cf_pair_find(cs, "ipaddr")) {
300 home->ipaddr.af = AF_INET;
301 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
303 } else if (cf_pair_find(cs, "ipv6addr")) {
304 home->ipaddr.af = AF_INET6;
305 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
308 cf_log_err(cf_sectiontoitem(cs),
309 "Internal sanity check failed for home server %s.",
319 if (!home->port || (home->port > 65535)) {
320 cf_log_err(cf_sectiontoitem(cs),
321 "No port, or invalid port defined for home server %s.",
332 cf_log_err(cf_sectiontoitem(cs),
333 "Fatal error! Home server %s is ourselves!",
344 cf_log_err(cf_sectiontoitem(cs),
345 "Fatal error! Home server %s has no type defined!",
355 if (strcasecmp(hs_type, "auth") == 0) {
356 home->type = HOME_TYPE_AUTH;
357 if (type != home->type) {
358 cf_log_err(cf_sectiontoitem(cs),
359 "Server pool of \"acct\" servers cannot include home server %s of type \"auth\"",
365 } else if (strcasecmp(hs_type, "acct") == 0) {
366 home->type = HOME_TYPE_ACCT;
367 if (type != home->type) {
368 cf_log_err(cf_sectiontoitem(cs),
369 "Server pool of \"auth\" servers cannot include home server %s of type \"acct\"",
375 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
376 home->type = HOME_TYPE_AUTH;
380 cf_log_err(cf_sectiontoitem(cs),
381 "Invalid type \"%s\" for home server %s.",
394 cf_log_err(cf_sectiontoitem(cs),
395 "No shared secret defined for home server %s.",
401 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
402 home->ping_check = HOME_PING_CHECK_NONE;
404 } else if (strcasecmp(hs_check, "status-server") == 0) {
405 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
407 } else if (strcasecmp(hs_check, "request") == 0) {
408 home->ping_check = HOME_PING_CHECK_REQUEST;
411 cf_log_err(cf_sectiontoitem(cs),
412 "Invalid ping_check \"%s\" for home server %s.",
422 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
423 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
424 if (!home->ping_user_name) {
425 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable ping checks");
430 if ((home->type == HOME_TYPE_AUTH) &&
431 !home->ping_user_password) {
432 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable ping checks");
438 if (rbtree_finddata(home_servers_byaddr, home)) {
439 DEBUG2("Ignoring duplicate home server %s.", name2);
443 if (!rbtree_insert(home_servers_byname, home)) {
444 cf_log_err(cf_sectiontoitem(cs),
445 "Internal error adding home server %s.",
451 if (!rbtree_insert(home_servers_byaddr, home)) {
452 rbtree_deletebydata(home_servers_byname, home);
453 cf_log_err(cf_sectiontoitem(cs),
454 "Internal error adding home server %s.",
460 if (home->response_window < 5) home->response_window = 5;
461 if (home->response_window > 60) home->response_window = 60;
463 if (home->max_outstanding < 8) home->max_outstanding = 8;
464 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
466 if (home->ping_interval < 6) home->ping_interval = 6;
467 if (home->ping_interval > 120) home->ping_interval = 120;
469 if (home->zombie_period < 20) home->zombie_period = 20;
470 if (home->zombie_period > 120) home->zombie_period = 120;
472 if (home->zombie_period < home->response_window) {
473 home->zombie_period = home->response_window;
476 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
477 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
479 if (home->ping_timeout < 3) home->ping_timeout = 3;
480 if (home->ping_timeout > 10) home->ping_timeout = 10;
482 if (home->revive_interval < 60) home->revive_interval = 60;
483 if (home->revive_interval > 3600) home->revive_interval = 3600;
486 home_server *home2 = rad_malloc(sizeof(*home2));
488 memcpy(home2, home, sizeof(*home2));
490 home2->type = HOME_TYPE_ACCT;
492 home2->ping_user_password = NULL;
495 if (!rbtree_insert(home_servers_byname, home2)) {
496 cf_log_err(cf_sectiontoitem(cs),
497 "Internal error adding home server %s.",
503 if (!rbtree_insert(home_servers_byaddr, home2)) {
504 rbtree_deletebydata(home_servers_byname, home2);
505 cf_log_err(cf_sectiontoitem(cs),
506 "Internal error adding home server %s.",
517 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
518 int server_type, int num_home_servers)
522 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
524 if (!pool) return NULL; /* just for pairanoia */
526 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
531 pool->server_type = server_type;
532 pool->num_home_servers = num_home_servers;
538 static int server_pool_add(realm_config_t *rc,
539 CONF_SECTION *cs, int server_type, int do_print)
542 home_pool_t *pool = NULL;
545 int num_home_servers;
547 name2 = cf_section_name1(cs);
548 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
549 (strcasecmp(name2, "home_server_pool") != 0))) {
550 cf_log_err(cf_sectiontoitem(cs),
551 "Section is not a home_server_pool.");
555 name2 = cf_section_name2(cs);
557 cf_log_err(cf_sectiontoitem(cs),
558 "Server pool section is missing a name.");
563 * Count the home servers and initalize them.
565 num_home_servers = 0;
566 for (cp = cf_pair_find(cs, "home_server");
568 cp = cf_pair_find_next(cs, cp, "home_server")) {
569 home_server myhome, *home;
570 CONF_SECTION *server_cs;
574 value = cf_pair_value(cp);
576 cf_log_err(cf_pairtoitem(cp),
577 "No value given for home_server.");
582 myhome.type = server_type;
583 home = rbtree_finddata(home_servers_byname, &myhome);
586 server_cs = cf_section_sub_find_name2(rc->cs,
590 cf_log_err(cf_pairtoitem(cp),
591 "Unknown home_server \"%s\".",
596 if (!home_server_add(server_cs, server_type)) {
600 home = rbtree_finddata(home_servers_byname, &myhome);
602 radlog(L_ERR, "Internal sanity check failed %d",
608 if (num_home_servers == 0) {
609 cf_log_err(cf_sectiontoitem(cs),
610 "No home servers defined in pool %s",
615 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
619 if (do_print) DEBUG2(" home_server_pool %s {", name2);
621 cp = cf_pair_find(cs, "type");
623 static LRAD_NAME_NUMBER pool_types[] = {
624 { "load-balance", HOME_POOL_LOAD_BALANCE },
625 { "fail-over", HOME_POOL_FAIL_OVER },
626 { "round_robin", HOME_POOL_LOAD_BALANCE },
627 { "fail_over", HOME_POOL_FAIL_OVER },
628 { "client-balance", HOME_POOL_CLIENT_BALANCE },
629 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
630 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
634 value = cf_pair_value(cp);
636 cf_log_err(cf_pairtoitem(cp),
637 "No value given for type.");
641 pool->type = lrad_str2int(pool_types, value, 0);
643 cf_log_err(cf_pairtoitem(cp),
644 "Unknown type \"%s\".",
649 if (do_print) DEBUG2("\ttype = %s", value);
652 num_home_servers = 0;
653 for (cp = cf_pair_find(cs, "home_server");
655 cp = cf_pair_find_next(cs, cp, "home_server")) {
656 home_server myhome, *home;
658 value = cf_pair_value(cp);
660 cf_log_err(cf_pairtoitem(cp),
661 "No value given for home_server.");
666 myhome.type = server_type;
668 home = rbtree_finddata(home_servers_byname, &myhome);
670 DEBUG2("Internal sanity check failed");
675 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
679 if (do_print) DEBUG2("\thome_server = %s", home->name);
680 pool->servers[num_home_servers++] = home;
681 } /* loop over home_server's */
683 if (!rbtree_insert(home_pools_byname, pool)) {
684 rad_assert("Internal sanity check failed");
688 if (do_print) DEBUG2(" }");
690 rad_assert(pool->server_type != 0);
695 if (do_print) DEBUG2(" }");
701 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
703 const char *name, const char *secret,
704 home_pool_type_t ldflag, home_pool_t **pool_p,
707 int i, insert_point, num_home_servers;
708 home_server myhome, *home;
709 home_pool_t mypool, *pool;
713 * LOCAL realms get sanity checked, and nothing else happens.
715 if (strcmp(name, "LOCAL") == 0) {
717 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
724 mypool.server_type = type;
725 pool = rbtree_finddata(home_pools_byname, &mypool);
727 if (pool->type != ldflag) {
728 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
732 if (pool->server_type != type) {
733 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
740 home = rbtree_finddata(home_servers_byname, &myhome);
742 if (strcmp(home->secret, secret) != 0) {
743 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
747 if (home->type != type) {
748 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
753 * See if the home server is already listed
754 * in the pool. If so, do nothing else.
756 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
757 if (pool->servers[i] == home) {
764 * If we do have a pool, check that there is room to
765 * insert the home server we've found, or the one that we
768 * Note that we insert it into the LAST available
769 * position, in order to maintain the same order as in
770 * the configuration files.
774 for (i = pool->num_home_servers - 1; i >= 0; i--) {
775 if (pool->servers[i]) break;
777 if (!pool->servers[i]) {
782 if (insert_point < 0) {
783 cf_log_err(cf_sectiontoitem(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);
789 * No home server, allocate one.
795 home = rad_malloc(sizeof(*home));
796 memset(home, 0, sizeof(*home));
799 home->hostname = name;
801 home->secret = secret;
804 p = strchr(name, ':');
806 if (type == HOME_TYPE_AUTH) {
807 home->port = PW_AUTH_UDP_PORT;
809 home->port = PW_ACCT_UDP_PORT;
815 } else if (p == name) {
816 cf_log_err(cf_sectiontoitem(cs),
817 "Invalid hostname %s.",
823 home->port = atoi(p + 1);
824 if ((home->port == 0) || (home->port > 65535)) {
825 cf_log_err(cf_sectiontoitem(cs),
832 q = rad_malloc((p - name) + 1);
833 memcpy(q, name, (p - name));
838 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
839 cf_log_err(cf_sectiontoitem(cs),
840 "Failed looking up hostname %s.",
849 * Use the old-style configuration.
851 home->max_outstanding = 65535*16;
852 home->zombie_period = rc->retry_delay * rc->retry_count;
853 if (home->zombie_period == 0) home->zombie_period =30;
854 home->response_window = home->zombie_period - 1;
856 home->ping_check = HOME_PING_CHECK_NONE;
858 home->revive_interval = rc->dead_time;
860 if (rbtree_finddata(home_servers_byaddr, home)) {
861 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
866 if (!rbtree_insert(home_servers_byname, home)) {
867 cf_log_err(cf_sectiontoitem(cs), "Internal error adding home server %s.", name);
872 if (!rbtree_insert(home_servers_byaddr, home)) {
873 rbtree_deletebydata(home_servers_byname, home);
874 cf_log_err(cf_sectiontoitem(cs), "Internal error adding home server %s.", name);
881 * We now have a home server, see if we can insert it
882 * into pre-existing pool.
884 if (insert_point >= 0) {
885 rad_assert(pool != NULL);
886 pool->servers[insert_point] = home;
890 rad_assert(pool == NULL);
891 rad_assert(home != NULL);
894 * Count the old-style realms of this name.
896 num_home_servers = 0;
897 for (subcs = cf_section_find_next(cs, NULL, "realm");
899 subcs = cf_section_find_next(cs, subcs, "realm")) {
900 const char *this = cf_section_name2(subcs);
902 if (!this || (strcmp(this, realm) != 0)) continue;
906 if (num_home_servers == 0) {
907 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
912 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
915 pool->servers[0] = home;
917 if (!rbtree_insert(home_pools_byname, pool)) {
918 rad_assert("Internal sanity check failed");
927 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
930 const char *secret = NULL;
931 home_pool_type_t ldflag;
934 cp = cf_pair_find(cs, "ldflag");
935 ldflag = HOME_POOL_FAIL_OVER;
937 host = cf_pair_value(cp);
939 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
943 if (strcasecmp(host, "fail_over") == 0) {
944 DEBUG2("\tldflag = fail_over");
946 } else if (strcasecmp(host, "round_robin") == 0) {
947 ldflag = HOME_POOL_LOAD_BALANCE;
948 DEBUG2("\tldflag = round_robin");
951 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
954 } /* else don't print it. */
957 * Allow old-style if it doesn't exist, or if it exists and
960 cp = cf_pair_find(cs, "authhost");
962 host = cf_pair_value(cp);
964 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
968 if (strcmp(host, "LOCAL") != 0) {
969 cp = cf_pair_find(cs, "secret");
971 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
975 secret = cf_pair_value(cp);
977 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
982 DEBUG2("\tauthhost = %s", host);
984 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
985 &r->auth_pool, HOME_TYPE_AUTH)) {
990 cp = cf_pair_find(cs, "accthost");
992 host = cf_pair_value(cp);
994 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
999 * Don't look for a secret again if it was found
1002 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1003 cp = cf_pair_find(cs, "secret");
1005 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1009 secret = cf_pair_value(cp);
1011 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1016 DEBUG2("\taccthost = %s", host);
1018 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1019 &r->acct_pool, HOME_TYPE_ACCT)) {
1024 if (secret) DEBUG2("\tsecret = %s", secret);
1031 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1032 const char *name, home_pool_t **dest,
1033 int server_type, int do_print)
1035 home_pool_t mypool, *pool;
1038 mypool.server_type = server_type;
1040 pool = rbtree_finddata(home_pools_byname, &mypool);
1042 CONF_SECTION *pool_cs;
1044 pool_cs = cf_section_sub_find_name2(rc->cs,
1048 pool_cs = cf_section_sub_find_name2(rc->cs,
1053 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1057 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1061 pool = rbtree_finddata(home_pools_byname, &mypool);
1063 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1068 if (pool->server_type != server_type) {
1069 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1078 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1083 home_pool_t *auth_pool, *acct_pool;
1084 const char *auth_pool_name, *acct_pool_name;
1086 name2 = cf_section_name1(cs);
1087 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1088 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1092 name2 = cf_section_name2(cs);
1094 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1098 auth_pool = acct_pool = NULL;
1099 auth_pool_name = acct_pool_name = NULL;
1102 * Prefer new configuration to old one.
1104 cp = cf_pair_find(cs, "pool");
1105 if (cp) auth_pool_name = cf_pair_value(cp);
1106 if (cp && auth_pool_name) {
1107 acct_pool_name = auth_pool_name;
1108 if (!add_pool_to_realm(rc, cs,
1109 auth_pool_name, &auth_pool,
1110 HOME_TYPE_AUTH, 1)) {
1113 if (!add_pool_to_realm(rc, cs,
1114 auth_pool_name, &acct_pool,
1115 HOME_TYPE_ACCT, 0)) {
1120 cp = cf_pair_find(cs, "auth_pool");
1121 if (cp) auth_pool_name = cf_pair_value(cp);
1122 if (cp && auth_pool_name) {
1124 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1127 if (!add_pool_to_realm(rc, cs,
1128 auth_pool_name, &auth_pool,
1129 HOME_TYPE_AUTH, 1)) {
1134 cp = cf_pair_find(cs, "acct_pool");
1135 if (cp) acct_pool_name = cf_pair_value(cp);
1136 if (cp && acct_pool_name) {
1137 int do_print = TRUE;
1140 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1145 (strcmp(auth_pool_name, acct_pool_name) != 0)) {
1149 if (!add_pool_to_realm(rc, cs,
1150 acct_pool_name, &acct_pool,
1151 HOME_TYPE_ACCT, do_print)) {
1156 DEBUG2(" realm %s {", name2);
1159 * The realm MAY already exist if it's an old-style realm.
1160 * In that case, merge the old-style realm with this one.
1162 r = realm_find(name2);
1164 if (cf_pair_find(cs, "auth_pool") ||
1165 cf_pair_find(cs, "acct_pool")) {
1166 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1170 if (!old_realm_config(rc, cs, r)) {
1174 DEBUG2(" } # realm %s", name2);
1178 r = rad_malloc(sizeof(*r));
1179 memset(r, 0, sizeof(*r));
1182 r->auth_pool = auth_pool;
1183 r->acct_pool = acct_pool;
1186 if (auth_pool_name &&
1187 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1188 DEBUG2("\tpool = %s", auth_pool_name);
1190 if (auth_pool_name) DEBUG2("\tauth_pool = %s", auth_pool_name);
1191 if (acct_pool_name) DEBUG2("\tacct_pool = %s", acct_pool_name);
1194 cp = cf_pair_find(cs, "nostrip");
1195 if (cp && (cf_pair_value(cp) == NULL)) {
1197 DEBUG2("\tnostrip");
1201 * We're a new-style realm. Complain if we see the old
1204 if (r->auth_pool || r->acct_pool) {
1205 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1206 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1207 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1208 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1209 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1214 * The realm MAY be an old-style realm, as there
1215 * was no auth_pool or acct_pool. Double-check
1216 * it, just to be safe.
1218 } else if (!old_realm_config(rc, cs, r)) {
1222 if (!rbtree_insert(realms_byname, r)) {
1223 rad_assert("Internal sanity check failed");
1232 DEBUG2(" } # realm %s", name2);
1238 int realms_init(CONF_SECTION *config)
1241 realm_config_t *rc, *old_rc;
1243 if (realms_byname) return 1;
1245 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1246 if (!realms_byname) {
1251 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
1252 if (!home_servers_byaddr) {
1257 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1258 if (!home_servers_byname) {
1263 home_pools_byname = rbtree_create(home_pool_name_cmp, free, 0);
1264 if (!home_pools_byname) {
1269 cs = cf_subsection_find_next(config, NULL, "proxy");
1271 radlog(L_ERR, "No proxy section defined");
1276 rc = rad_malloc(sizeof(*rc));
1277 memset(rc, 0, sizeof(*rc));
1280 cf_section_parse(cs, rc, proxy_config);
1282 for (cs = cf_subsection_find_next(config, NULL, "realm");
1284 cs = cf_subsection_find_next(config, cs, "realm")) {
1285 if (!realm_add(rc, cs)) {
1291 xlat_register("home_server", xlat_home_server, NULL);
1292 xlat_register("home_server_pool", xlat_server_pool, NULL);
1295 * Swap pointers atomically.
1297 old_rc = realm_config;
1306 * Find a realm in the REALM list.
1308 REALM *realm_find(const char *name)
1312 if (!name) name = "NULL";
1314 myrealm.name = name;
1315 return rbtree_finddata(realms_byname, &myrealm);
1319 home_server *home_server_ldb(const char *realmname,
1320 home_pool_t *pool, REQUEST *request)
1324 home_server *found = NULL;
1330 * Determine how to pick choose the home server.
1332 switch (pool->type) {
1336 * For load-balancing by client IP address, we
1337 * pick a home server by hashing the client IP.
1339 * This isn't as even a load distribution as
1340 * tracking the State attribute, but it's better
1343 case HOME_POOL_CLIENT_BALANCE:
1344 switch (request->packet->src_ipaddr.af) {
1346 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1347 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1350 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1351 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1357 start = hash % pool->num_home_servers;
1360 case HOME_POOL_CLIENT_PORT_BALANCE:
1361 switch (request->packet->src_ipaddr.af) {
1363 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1364 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1367 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1368 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1374 lrad_hash_update(&request->packet->src_port,
1375 sizeof(request->packet->src_port), hash);
1376 start = hash % pool->num_home_servers;
1379 case HOME_POOL_KEYED_BALANCE:
1380 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY)) != NULL) {
1381 hash = lrad_hash(vp->vp_strvalue, vp->length);
1382 start = hash % pool->num_home_servers;
1387 case HOME_POOL_LOAD_BALANCE:
1388 found = pool->servers[0];
1396 * Starting with the home server we chose, loop through
1397 * all home servers. If the current one is dead, skip
1398 * it. If it is too busy, skip it.
1400 * Otherwise, use it.
1402 for (count = 0; count < pool->num_home_servers; count++) {
1403 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
1405 if (home->state == HOME_STATE_IS_DEAD) {
1410 * This home server is too busy. Choose another one.
1412 if (home->currently_outstanding >= home->max_outstanding) {
1416 if (pool->type != HOME_POOL_LOAD_BALANCE) {
1420 DEBUG3("PROXY %s %d\t%s %d",
1421 found->name, found->currently_outstanding,
1422 home->name, home->currently_outstanding);
1425 * Prefer this server if it's less busy than the
1426 * one we previously found.
1428 if (home->currently_outstanding < found->currently_outstanding) {
1429 DEBUG3("Choosing %s: It's less busy than %s",
1430 home->name, found->name);
1436 * Ignore servers which are busier than the one
1439 if (home->currently_outstanding > found->currently_outstanding) {
1440 DEBUG3("Skipping %s: It's busier than %s",
1441 home->name, found->name);
1445 if (home->total_requests_sent < found->total_requests_sent) {
1446 DEBUG3("Choosing %s: It's been less busy than %s",
1447 home->name, found->name);
1452 if (home->total_requests_sent > found->total_requests_sent) {
1453 DEBUG3("Skipping %s: It's been busier than %s",
1454 home->name, found->name);
1459 * From the list of servers which have the same
1460 * load, choose one at random.
1462 if (((count + 1) * (lrad_rand() & 0xffff)) < (uint32_t) 0x10000) {
1466 } /* loop over the home servers */
1468 if (found) return found;
1471 * No live match found, and no fallback to the "DEFAULT"
1472 * realm. We fix this by blindly marking all servers as
1473 * "live". But only do it for ones that don't support
1474 * "pings", as they will be marked live when they
1475 * actually are live.
1477 if (!realm_config->fallback &&
1478 realm_config->wake_all_if_all_dead) {
1479 home_server *lb = NULL;
1481 for (count = 0; count < pool->num_home_servers; count++) {
1482 home_server *home = pool->servers[count];
1484 if ((home->state == HOME_STATE_IS_DEAD) &&
1485 (home->ping_check == HOME_PING_CHECK_NONE)) {
1486 home->state = HOME_STATE_ALIVE;
1495 * Still nothing. Look up the DEFAULT realm, but only
1496 * if we weren't looking up the NULL or DEFAULT realms.
1498 if (realm_config->fallback &&
1500 (strcmp(realmname, "NULL") != 0) &&
1501 (strcmp(realmname, "DEFAULT") != 0)) {
1502 REALM *rd = realm_find("DEFAULT");
1504 if (!rd) return NULL;
1507 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1508 pool = rd->auth_pool;
1510 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1511 pool = rd->acct_pool;
1513 if (!pool) return NULL;
1515 DEBUG2(" Realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
1516 return home_server_ldb(rd->name, pool, request);
1520 * Still haven't found anything. Oh well.
1526 home_server *home_server_find(lrad_ipaddr_t *ipaddr, int port)
1530 myhome.ipaddr = *ipaddr;
1533 return rbtree_finddata(home_servers_byaddr, &myhome);