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 static int realm_name_cmp(const void *one, const void *two)
47 return strcasecmp(a->name, b->name);
51 static int home_server_name_cmp(const void *one, const void *two)
53 const home_server *a = one;
54 const home_server *b = two;
56 if (a->type < b->type) return -1;
57 if (a->type > b->type) return +1;
59 return strcasecmp(a->name, b->name);
62 static int home_server_addr_cmp(const void *one, const void *two)
64 const home_server *a = one;
65 const home_server *b = two;
67 if (a->port < b->port) return -1;
68 if (a->port > b->port) return +1;
70 return lrad_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
74 static int home_pool_name_cmp(const void *one, const void *two)
76 const home_pool_t *a = one;
77 const home_pool_t *b = two;
79 if (a->server_type < b->server_type) return -1;
80 if (a->server_type > b->server_type) return +1;
82 return strcasecmp(a->name, b->name);
86 void realms_free(void)
88 rbtree_free(home_servers_byname);
89 home_servers_byname = NULL;
91 rbtree_free(home_servers_byaddr);
92 home_servers_byaddr = NULL;
94 rbtree_free(home_pools_byname);
95 home_pools_byname = NULL;
97 rbtree_free(realms_byname);
102 int realms_init(CONF_SECTION *config)
106 if (realms_byname) return 1;
108 realms_byname = rbtree_create(realm_name_cmp, free, 0);
109 if (!realms_byname) {
114 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
115 if (!home_servers_byaddr) {
120 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
121 if (!home_servers_byname) {
126 home_pools_byname = rbtree_create(home_pool_name_cmp, free, 0);
127 if (!home_pools_byname) {
132 for (cs = cf_subsection_find_next(config, NULL, "realm");
134 cs = cf_subsection_find_next(config, cs, "realm")) {
135 if (!realm_add(cs)) {
144 static struct in_addr hs_ip4addr;
145 static struct in6_addr hs_ip6addr;
146 static char *hs_type = NULL;
147 static char *hs_check = NULL;
149 static CONF_PARSER home_server_config[] = {
150 { "ipaddr", PW_TYPE_IPADDR,
151 0, &hs_ip4addr, NULL },
152 { "ipv6addr", PW_TYPE_IPV6ADDR,
153 0, &hs_ip6addr, NULL },
155 { "port", PW_TYPE_INTEGER,
156 offsetof(home_server,port), NULL, "0" },
158 { "type", PW_TYPE_STRING_PTR,
161 { "secret", PW_TYPE_STRING_PTR,
162 offsetof(home_server,secret), NULL, NULL},
164 { "response_window", PW_TYPE_INTEGER,
165 offsetof(home_server,response_window), NULL, "30" },
166 { "max_outstanding", PW_TYPE_INTEGER,
167 offsetof(home_server,max_outstanding), NULL, "65536" },
169 { "zombie_period", PW_TYPE_INTEGER,
170 offsetof(home_server,zombie_period), NULL, "40" },
171 { "status_check", PW_TYPE_STRING_PTR,
172 0, &hs_check, "none" },
173 { "ping_check", PW_TYPE_STRING_PTR,
174 0, &hs_check, "none" },
176 { "ping_interval", PW_TYPE_INTEGER,
177 offsetof(home_server,ping_interval), NULL, "30" },
178 { "check_interval", PW_TYPE_INTEGER,
179 offsetof(home_server,ping_interval), NULL, "30" },
180 { "num_answers_to_alive", PW_TYPE_INTEGER,
181 offsetof(home_server,num_pings_to_alive), NULL, "3" },
182 { "num_pings_to_alive", PW_TYPE_INTEGER,
183 offsetof(home_server,num_pings_to_alive), NULL, "3" },
184 { "revive_interval", PW_TYPE_INTEGER,
185 offsetof(home_server,revive_interval), NULL, "300" },
186 { "status_check_timeout", PW_TYPE_INTEGER,
187 offsetof(home_server,ping_timeout), NULL, "4" },
189 { "username", PW_TYPE_STRING_PTR,
190 offsetof(home_server,ping_user_name), NULL, NULL},
191 { "password", PW_TYPE_STRING_PTR,
192 offsetof(home_server,ping_user_password), NULL, NULL},
194 { NULL, -1, 0, NULL, NULL } /* end the list */
199 static int home_server_add(CONF_SECTION *cs, int type)
205 name2 = cf_section_name1(cs);
206 if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
207 cf_log_err(cf_sectiontoitem(cs),
208 "Section is not a home_server.");
212 name2 = cf_section_name2(cs);
214 cf_log_err(cf_sectiontoitem(cs),
215 "Home server section is missing a name.");
219 home = rad_malloc(sizeof(*home));
220 memset(home, 0, sizeof(*home));
224 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
225 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
226 cf_section_parse(cs, home, home_server_config);
228 if ((htonl(hs_ip4addr.s_addr) == INADDR_NONE) &&
229 IN6_IS_ADDR_UNSPECIFIED(&hs_ip6addr)) {
230 cf_log_err(cf_sectiontoitem(cs),
231 "No IPv4 or IPv6 address defined for home server %s.",
242 * Figure out which one to use.
244 * FIXME: Set hostname to something?
246 if (htonl(hs_ip4addr.s_addr) != INADDR_NONE) {
247 home->ipaddr.af = AF_INET;
248 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
250 } else if (!IN6_IS_ADDR_UNSPECIFIED(&hs_ip6addr)) {
251 home->ipaddr.af = AF_INET6;
252 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
255 cf_log_err(cf_sectiontoitem(cs),
256 "Internal sanity check failed for home server %s.",
266 if (!home->port || (home->port > 65535)) {
267 cf_log_err(cf_sectiontoitem(cs),
268 "No port, or invalid port defined for home server %s.",
279 cf_log_err(cf_sectiontoitem(cs),
280 "Fatal error! Home server %s is ourselves!",
291 cf_log_err(cf_sectiontoitem(cs),
292 "Fatal error! Home server %s has no type defined!",
302 if (strcasecmp(hs_type, "auth") == 0) {
303 home->type = HOME_TYPE_AUTH;
304 if (type != home->type) {
305 cf_log_err(cf_sectiontoitem(cs),
306 "Server pool of \"acct\" servers cannot include home server %s of type \"auth\"",
312 } else if (strcasecmp(hs_type, "acct") == 0) {
313 home->type = HOME_TYPE_ACCT;
314 if (type != home->type) {
315 cf_log_err(cf_sectiontoitem(cs),
316 "Server pool of \"auth\" servers cannot include home server %s of type \"acct\"",
322 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
323 home->type = HOME_TYPE_AUTH;
327 cf_log_err(cf_sectiontoitem(cs),
328 "Invalid type \"%s\" for home server %s.",
341 cf_log_err(cf_sectiontoitem(cs),
342 "No shared secret defined for home server %s.",
348 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
349 home->ping_check = HOME_PING_CHECK_NONE;
351 } else if (strcasecmp(hs_check, "status-server") == 0) {
352 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
354 } else if (strcasecmp(hs_check, "request") == 0) {
355 home->ping_check = HOME_PING_CHECK_REQUEST;
358 cf_log_err(cf_sectiontoitem(cs),
359 "Invalid ping_check \"%s\" for home server %s.",
369 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
370 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
371 if (!home->ping_user_name) {
372 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable ping checks");
377 if ((home->type == HOME_TYPE_AUTH) &&
378 !home->ping_user_password) {
379 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable ping checks");
385 if (rbtree_finddata(home_servers_byaddr, home)) {
386 DEBUG2("Ignoring duplicate home server %s.", name2);
390 if (!rbtree_insert(home_servers_byname, home)) {
391 cf_log_err(cf_sectiontoitem(cs),
392 "Internal error adding home server %s.",
398 if (!rbtree_insert(home_servers_byaddr, home)) {
399 rbtree_deletebydata(home_servers_byname, home);
400 cf_log_err(cf_sectiontoitem(cs),
401 "Internal error adding home server %s.",
407 if (home->response_window < 5) home->response_window = 5;
408 if (home->response_window > 60) home->response_window = 60;
410 if (home->max_outstanding < 8) home->max_outstanding = 8;
411 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
413 if (home->ping_interval < 6) home->ping_interval = 6;
414 if (home->ping_interval > 120) home->ping_interval = 120;
416 if (home->zombie_period < 20) home->zombie_period = 20;
417 if (home->zombie_period > 120) home->zombie_period = 120;
419 if (home->zombie_period < home->response_window) {
420 home->zombie_period = home->response_window;
423 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
424 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
426 if (home->ping_timeout < 3) home->ping_timeout = 3;
427 if (home->ping_timeout > 10) home->ping_timeout = 10;
429 if (home->revive_interval < 60) home->revive_interval = 60;
430 if (home->revive_interval > 3600) home->revive_interval = 3600;
433 home_server *home2 = rad_malloc(sizeof(*home2));
435 memcpy(home2, home, sizeof(*home2));
437 home2->type = HOME_TYPE_ACCT;
439 home2->ping_user_password = NULL;
441 if (!rbtree_insert(home_servers_byname, home2)) {
442 cf_log_err(cf_sectiontoitem(cs),
443 "Internal error adding home server %s.",
449 if (!rbtree_insert(home_servers_byaddr, home2)) {
450 rbtree_deletebydata(home_servers_byname, home2);
451 cf_log_err(cf_sectiontoitem(cs),
452 "Internal error adding home server %s.",
463 static int server_pool_add(CONF_SECTION *cs, int server_type, int do_print)
466 home_pool_t *pool = NULL;
469 int num_home_servers;
471 name2 = cf_section_name1(cs);
472 if (!name2 || (strcasecmp(name2, "server_pool") != 0)) {
473 cf_log_err(cf_sectiontoitem(cs),
474 "Section is not a server_pool.");
478 name2 = cf_section_name2(cs);
480 cf_log_err(cf_sectiontoitem(cs),
481 "Server pool section is missing a name.");
486 * Count the home servers and initalize them.
488 num_home_servers = 0;
489 for (cp = cf_pair_find(cs, "home_server");
491 cp = cf_pair_find_next(cs, cp, "home_server")) {
492 home_server myhome, *home;
493 CONF_SECTION *server_cs;
497 value = cf_pair_value(cp);
499 cf_log_err(cf_pairtoitem(cp),
500 "No value given for home_server.");
505 myhome.type = server_type;
506 home = rbtree_finddata(home_servers_byname, &myhome);
509 server_cs = cf_section_sub_find_name2(mainconfig.config,
513 cf_log_err(cf_pairtoitem(cp),
514 "Unknown home_server \"%s\".",
519 if (!home_server_add(server_cs, server_type)) {
523 home = rbtree_finddata(home_servers_byname, &myhome);
525 radlog(L_ERR, "Internal sanity check failed %d",
531 if (num_home_servers == 0) {
532 cf_log_err(cf_sectiontoitem(cs),
533 "No home servers defined in pool %s",
538 pool = rad_malloc(sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
539 memset(pool, 0, sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
541 pool->type = HOME_POOL_FAIL_OVER;
543 pool->server_type = server_type;
545 if (do_print) DEBUG2(" server_pool %s {", name2);
547 cp = cf_pair_find(cs, "type");
549 static LRAD_NAME_NUMBER pool_types[] = {
550 { "load-balance", HOME_POOL_LOAD_BALANCE },
551 { "fail-over", HOME_POOL_FAIL_OVER },
552 { "round_robin", HOME_POOL_LOAD_BALANCE },
553 { "fail_over", HOME_POOL_FAIL_OVER },
554 { "client-balance", HOME_POOL_CLIENT_BALANCE },
555 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
556 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
560 value = cf_pair_value(cp);
562 cf_log_err(cf_pairtoitem(cp),
563 "No value given for type.");
567 pool->type = lrad_str2int(pool_types, value, 0);
569 cf_log_err(cf_pairtoitem(cp),
570 "Unknown type \"%s\".",
575 if (do_print) DEBUG2("\ttype = %s", value);
578 for (cp = cf_pair_find(cs, "home_server");
580 cp = cf_pair_find_next(cs, cp, "home_server")) {
581 home_server myhome, *home;
583 value = cf_pair_value(cp);
585 cf_log_err(cf_pairtoitem(cp),
586 "No value given for home_server.");
591 myhome.type = server_type;
593 home = rbtree_finddata(home_servers_byname, &myhome);
595 DEBUG2("Internal sanity check failed");
600 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
604 if (do_print) DEBUG2("\thome_server = %s", home->name);
605 pool->servers[pool->num_home_servers] = home;
606 pool->num_home_servers++;
607 } /* loop over home_server's */
609 if (!rbtree_insert(home_pools_byname, pool)) {
610 rad_assert("Internal sanity check failed");
614 if (do_print) DEBUG2(" }");
616 rad_assert(pool->server_type != 0);
621 if (do_print) DEBUG2(" }");
627 static int old_server_add(CONF_SECTION *cs, const char *realm,
628 const char *name, const char *secret,
629 home_pool_type_t ldflag, home_pool_t **pool_p,
632 int i, insert_point, num_home_servers;
633 home_server myhome, *home;
634 home_pool_t mypool, *pool;
638 * LOCAL realms get sanity checked, and nothing else happens.
640 if (strcmp(name, "LOCAL") == 0) {
642 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
649 mypool.server_type = type;
650 pool = rbtree_finddata(home_pools_byname, &mypool);
652 if (pool->type != ldflag) {
653 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
657 if (pool->server_type != type) {
658 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
665 home = rbtree_finddata(home_servers_byname, &myhome);
667 if (strcmp(home->secret, secret) != 0) {
668 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
672 if (home->type != type) {
673 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
678 * See if the home server is already listed
679 * in the pool. If so, do nothing else.
681 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
682 if (pool->servers[i] == home) {
689 * If we do have a pool, check that there is room to
690 * insert the home server we've found, or the one that we
693 * Note that we insert it into the LAST available
694 * position, in order to maintain the same order as in
695 * the configuration files.
699 for (i = pool->num_home_servers - 1; i >= 0; i--) {
700 if (pool->servers[i]) break;
702 if (!pool->servers[i]) {
707 if (insert_point < 0) {
708 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);
714 * No home server, allocate one.
720 home = rad_malloc(sizeof(*home));
721 memset(home, 0, sizeof(*home));
724 home->hostname = name;
726 home->secret = secret;
728 p = strchr(name, ':');
730 if (type == HOME_TYPE_AUTH) {
731 home->port = PW_AUTH_UDP_PORT;
733 home->port = PW_ACCT_UDP_PORT;
739 } else if (p == name) {
740 cf_log_err(cf_sectiontoitem(cs),
741 "Invalid hostname %s.",
747 home->port = atoi(p + 1);
748 if ((home->port == 0) || (home->port > 65535)) {
749 cf_log_err(cf_sectiontoitem(cs),
756 q = rad_malloc((p - name) + 1);
757 memcpy(q, name, (p - name));
762 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
763 cf_log_err(cf_sectiontoitem(cs),
764 "Failed looking up hostname %s.",
773 * Use the old-style configuration.
775 home->max_outstanding = 65535*16;
776 home->zombie_period = mainconfig.proxy_retry_delay * mainconfig.proxy_retry_count;
777 if (home->zombie_period == 0) home->zombie_period =30;
778 home->response_window = home->zombie_period - 1;
780 home->ping_check = HOME_PING_CHECK_NONE;
782 home->revive_interval = mainconfig.proxy_dead_time;
784 if (rbtree_finddata(home_servers_byaddr, home)) {
785 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
790 if (!rbtree_insert(home_servers_byname, home)) {
791 cf_log_err(cf_sectiontoitem(cs), "Internal error adding home server %s.", name);
796 if (!rbtree_insert(home_servers_byaddr, home)) {
797 rbtree_deletebydata(home_servers_byname, home);
798 cf_log_err(cf_sectiontoitem(cs), "Internal error adding home server %s.", name);
805 * We now have a home server, see if we can insert it
806 * into pre-existing pool.
808 if (insert_point >= 0) {
809 rad_assert(pool != NULL);
810 pool->servers[insert_point] = home;
814 rad_assert(pool == NULL);
815 rad_assert(home != NULL);
818 * Count the old-style realms of this name.
820 num_home_servers = 0;
821 for (subcs = cf_section_find_next(cs, NULL, "realm");
823 subcs = cf_section_find_next(cs, subcs, "realm")) {
824 const char *this = cf_section_name2(subcs);
826 if (!this || (strcmp(this, realm) != 0)) continue;
830 if (num_home_servers == 0) {
831 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
836 pool = rad_malloc(sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
837 memset(pool, 0, sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
841 pool->server_type = type;
842 pool->num_home_servers = num_home_servers;
843 pool->servers[0] = home;
845 if (!rbtree_insert(home_pools_byname, pool)) {
846 rad_assert("Internal sanity check failed");
855 static int old_realm_config(CONF_SECTION *cs, REALM *r)
858 const char *secret = NULL;
859 home_pool_type_t ldflag;
862 cp = cf_pair_find(cs, "ldflag");
863 ldflag = HOME_POOL_FAIL_OVER;
865 host = cf_pair_value(cp);
867 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
871 if (strcasecmp(host, "fail_over") == 0) {
872 DEBUG2("\tldflag = fail_over");
874 } else if (strcasecmp(host, "round_robin") == 0) {
875 ldflag = HOME_POOL_LOAD_BALANCE;
876 DEBUG2("\tldflag = round_robin");
879 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
882 } /* else don't print it. */
885 * Allow old-style if it doesn't exist, or if it exists and
888 cp = cf_pair_find(cs, "authhost");
890 host = cf_pair_value(cp);
892 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
896 if (strcmp(host, "LOCAL") != 0) {
897 cp = cf_pair_find(cs, "secret");
899 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
903 secret = cf_pair_value(cp);
905 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
910 DEBUG2("\tauthhost = %s", host);
912 if (!old_server_add(cs, r->name, host, secret, ldflag,
913 &r->auth_pool, HOME_TYPE_AUTH)) {
918 cp = cf_pair_find(cs, "accthost");
920 host = cf_pair_value(cp);
922 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
927 * Don't look for a secret again if it was found
930 if ((strcmp(host, "LOCAL") != 0) && !secret) {
931 cp = cf_pair_find(cs, "secret");
933 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
937 secret = cf_pair_value(cp);
939 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
944 DEBUG2("\taccthost = %s", host);
946 if (!old_server_add(cs, r->name, host, secret, ldflag,
947 &r->acct_pool, HOME_TYPE_ACCT)) {
952 if (secret) DEBUG2("\tsecret = %s", secret);
959 static int add_pool_to_realm(CONF_SECTION *cs,
960 const char *name, home_pool_t **dest,
961 int server_type, int do_print)
963 home_pool_t mypool, *pool;
966 mypool.server_type = server_type;
968 pool = rbtree_finddata(home_pools_byname, &mypool);
970 CONF_SECTION *pool_cs;
972 pool_cs = cf_section_sub_find_name2(mainconfig.config,
976 cf_log_err(cf_sectiontoitem(cs), "Failed to find server_pool \"%s\"", name);
980 if (!server_pool_add(pool_cs, server_type, do_print)) {
984 pool = rbtree_finddata(home_pools_byname, &mypool);
986 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
991 if (pool->server_type != server_type) {
992 cf_log_err(cf_sectiontoitem(cs), "Incompatible server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1001 int realm_add(CONF_SECTION *cs)
1006 home_pool_t *auth_pool, *acct_pool;
1007 const char *auth_pool_name, *acct_pool_name;
1009 name2 = cf_section_name1(cs);
1010 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1011 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1015 name2 = cf_section_name2(cs);
1017 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1021 auth_pool = acct_pool = NULL;
1022 auth_pool_name = acct_pool_name = NULL;
1025 * Prefer new configuration to old one.
1027 cp = cf_pair_find(cs, "pool");
1028 if (cp) auth_pool_name = cf_pair_value(cp);
1029 if (cp && auth_pool_name) {
1030 acct_pool_name = auth_pool_name;
1031 if (!add_pool_to_realm(cs,
1032 auth_pool_name, &auth_pool,
1033 HOME_TYPE_AUTH, 1)) {
1036 if (!add_pool_to_realm(cs,
1037 auth_pool_name, &acct_pool,
1038 HOME_TYPE_ACCT, 0)) {
1043 cp = cf_pair_find(cs, "auth_pool");
1044 if (cp) auth_pool_name = cf_pair_value(cp);
1045 if (cp && auth_pool_name) {
1047 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1050 if (!add_pool_to_realm(cs,
1051 auth_pool_name, &auth_pool,
1052 HOME_TYPE_AUTH, 1)) {
1057 cp = cf_pair_find(cs, "acct_pool");
1058 if (cp) acct_pool_name = cf_pair_value(cp);
1059 if (cp && acct_pool_name) {
1060 int do_print = TRUE;
1063 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1068 (strcmp(auth_pool_name, acct_pool_name) != 0)) {
1072 if (!add_pool_to_realm(cs,
1073 acct_pool_name, &acct_pool,
1074 HOME_TYPE_ACCT, do_print)) {
1079 DEBUG2(" realm %s {", name2);
1082 * The realm MAY already exist if it's an old-style realm.
1083 * In that case, merge the old-style realm with this one.
1085 r = realm_find(name2);
1087 if (cf_pair_find(cs, "auth_pool") ||
1088 cf_pair_find(cs, "acct_pool")) {
1089 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1093 if (!old_realm_config(cs, r)) {
1097 DEBUG2(" } # realm %s", name2);
1101 r = rad_malloc(sizeof(*r));
1102 memset(r, 0, sizeof(*r));
1105 r->auth_pool = auth_pool;
1106 r->acct_pool = acct_pool;
1109 if (auth_pool_name &&
1110 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1111 DEBUG2("\tpool = %s", auth_pool_name);
1113 if (auth_pool_name) DEBUG2("\tauth_pool = %s", auth_pool_name);
1114 if (acct_pool_name) DEBUG2("\tacct_pool = %s", acct_pool_name);
1117 cp = cf_pair_find(cs, "nostrip");
1118 if (cp && (cf_pair_value(cp) == NULL)) {
1120 DEBUG2("\tnostrip");
1124 * We're a new-style realm. Complain if we see the old
1127 if (r->auth_pool || r->acct_pool) {
1128 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1129 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1130 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1131 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1132 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1137 * The realm MAY be an old-style realm, as there
1138 * was no auth_pool or acct_pool. Double-check
1139 * it, just to be safe.
1141 } else if (!old_realm_config(cs, r)) {
1145 if (!rbtree_insert(realms_byname, r)) {
1146 rad_assert("Internal sanity check failed");
1155 DEBUG2(" } # realm %s", name2);
1162 * Find a realm in the REALM list.
1164 REALM *realm_find(const char *name)
1168 if (!name) name = "NULL";
1170 myrealm.name = name;
1171 return rbtree_finddata(realms_byname, &myrealm);
1175 home_server *home_server_ldb(const char *realmname,
1176 home_pool_t *pool, REQUEST *request)
1180 home_server *found = NULL;
1186 * Determine how to pick choose the home server.
1188 switch (pool->type) {
1192 * For load-balancing by client IP address, we
1193 * pick a home server by hashing the client IP.
1195 * This isn't as even a load distribution as
1196 * tracking the State attribute, but it's better
1199 case HOME_POOL_CLIENT_BALANCE:
1200 switch (request->packet->src_ipaddr.af) {
1202 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1203 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1206 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1207 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1213 start = hash % pool->num_home_servers;
1216 case HOME_POOL_CLIENT_PORT_BALANCE:
1217 switch (request->packet->src_ipaddr.af) {
1219 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1220 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1223 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1224 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1230 lrad_hash_update(&request->packet->src_port,
1231 sizeof(request->packet->src_port), hash);
1232 start = hash % pool->num_home_servers;
1235 case HOME_POOL_KEYED_BALANCE:
1236 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY)) != NULL) {
1237 hash = lrad_hash(vp->vp_strvalue, vp->length);
1238 start = hash % pool->num_home_servers;
1243 case HOME_POOL_LOAD_BALANCE:
1244 found = pool->servers[0];
1252 * Starting with the home server we chose, loop through
1253 * all home servers. If the current one is dead, skip
1254 * it. If it is too busy, skip it.
1256 * Otherwise, use it.
1258 for (count = 0; count < pool->num_home_servers; count++) {
1259 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
1261 if (home->state == HOME_STATE_IS_DEAD) {
1266 * This home server is too busy. Choose another one.
1268 if (home->currently_outstanding >= home->max_outstanding) {
1272 if (pool->type != HOME_POOL_LOAD_BALANCE) {
1276 DEBUG3("PROXY %s %d\t%s %d",
1277 found->name, found->currently_outstanding,
1278 home->name, home->currently_outstanding);
1281 * Prefer this server if it's less busy than the
1282 * one we previously found.
1284 if (home->currently_outstanding < found->currently_outstanding) {
1285 DEBUG3("Choosing %s: It's less busy than %s",
1286 home->name, found->name);
1292 * Ignore servers which are busier than the one
1295 if (home->currently_outstanding > found->currently_outstanding) {
1296 DEBUG3("Skipping %s: It's busier than %s",
1297 home->name, found->name);
1301 if (home->total_requests_sent < found->total_requests_sent) {
1302 DEBUG3("Choosing %s: It's been less busy than %s",
1303 home->name, found->name);
1308 if (home->total_requests_sent > found->total_requests_sent) {
1309 DEBUG3("Skipping %s: It's been busier than %s",
1310 home->name, found->name);
1315 * From the list of servers which have the same
1316 * load, choose one at random.
1318 if (((count + 1) * (lrad_rand() & 0xffff)) < (uint32_t) 0x10000) {
1322 } /* loop over the home servers */
1324 if (found) return found;
1327 * No live match found, and no fallback to the "DEFAULT"
1328 * realm. We fix this by blindly marking all servers as
1329 * "live". But only do it for ones that don't support
1330 * "pings", as they will be marked live when they
1331 * actually are live.
1333 if (!mainconfig.proxy_fallback &&
1334 mainconfig.wake_all_if_all_dead) {
1335 home_server *lb = NULL;
1337 for (count = 0; count < pool->num_home_servers; count++) {
1338 home_server *home = pool->servers[count];
1340 if ((home->state == HOME_STATE_IS_DEAD) &&
1341 (home->ping_check == HOME_PING_CHECK_NONE)) {
1342 home->state = HOME_STATE_ALIVE;
1351 * Still nothing. Look up the DEFAULT realm, but only
1352 * if we weren't looking up the NULL or DEFAULT realms.
1354 if (mainconfig.proxy_fallback &&
1356 (strcmp(realmname, "NULL") != 0) &&
1357 (strcmp(realmname, "DEFAULT") != 0)) {
1358 REALM *rd = realm_find("DEFAULT");
1360 if (!rd) return NULL;
1363 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1364 pool = rd->auth_pool;
1366 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1367 pool = rd->acct_pool;
1369 if (!pool) return NULL;
1371 DEBUG2(" Realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
1372 return home_server_ldb(rd->name, pool, request);
1376 * Still haven't found anything. Oh well.
1382 home_server *home_server_find(lrad_ipaddr_t *ipaddr, int port)
1386 myhome.ipaddr = *ipaddr;
1389 return rbtree_finddata(home_servers_byaddr, &myhome);