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 return strcasecmp(a->name, b->name);
59 static int home_server_addr_cmp(const void *one, const void *two)
61 const home_server *a = one;
62 const home_server *b = two;
64 if (a->port < b->port) return -1;
65 if (a->port > b->port) return +1;
67 return lrad_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
71 static int home_pool_name_cmp(const void *one, const void *two)
73 const home_pool_t *a = one;
74 const home_pool_t *b = two;
76 return strcasecmp(a->name, b->name);
80 void realms_free(void)
82 rbtree_free(home_servers_byname);
83 home_servers_byname = NULL;
85 rbtree_free(home_servers_byaddr);
86 home_servers_byaddr = NULL;
88 rbtree_free(home_pools_byname);
89 home_pools_byname = NULL;
91 rbtree_free(realms_byname);
96 int realms_init(const char *filename)
100 if (realms_byname) return 1;
102 realms_byname = rbtree_create(realm_name_cmp, free, 0);
103 if (!realms_byname) {
108 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
109 if (!home_servers_byaddr) {
114 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
115 if (!home_servers_byname) {
120 home_pools_byname = rbtree_create(home_pool_name_cmp, free, 0);
121 if (!home_pools_byname) {
126 for (cs = cf_subsection_find_next(mainconfig.config, NULL, "realm");
128 cs = cf_subsection_find_next(mainconfig.config, cs, "realm")) {
129 if (!realm_add(filename, cs)) {
138 static struct in_addr hs_ip4addr;
139 static struct in6_addr hs_ip6addr;
140 static char *hs_type = NULL;
141 static char *hs_check = NULL;
143 static CONF_PARSER home_server_config[] = {
144 { "ipaddr", PW_TYPE_IPADDR,
145 0, &hs_ip4addr, NULL },
146 { "ipv6addr", PW_TYPE_IPV6ADDR,
147 0, &hs_ip6addr, NULL },
149 { "hostname", PW_TYPE_STRING_PTR,
150 offsetof(home_server,hostname), NULL, NULL},
151 { "port", PW_TYPE_INTEGER,
152 offsetof(home_server,port), NULL, "0" },
154 { "type", PW_TYPE_STRING_PTR,
157 { "secret", PW_TYPE_STRING_PTR,
158 offsetof(home_server,secret), NULL, NULL},
160 { "response_window", PW_TYPE_INTEGER,
161 offsetof(home_server,response_window), NULL, "30" },
162 { "max_outstanding", PW_TYPE_INTEGER,
163 offsetof(home_server,max_outstanding), NULL, "65536" },
165 { "zombie_period", PW_TYPE_INTEGER,
166 offsetof(home_server,zombie_period), NULL, "40" },
167 { "status_check", PW_TYPE_STRING_PTR,
168 0, &hs_check, "none" },
169 { "ping_check", PW_TYPE_STRING_PTR,
170 0, &hs_check, "none" },
172 { "ping_interval", PW_TYPE_INTEGER,
173 offsetof(home_server,ping_interval), NULL, "30" },
174 { "check_interval", PW_TYPE_INTEGER,
175 offsetof(home_server,ping_interval), NULL, "30" },
176 { "num_answers_to_alive", PW_TYPE_INTEGER,
177 offsetof(home_server,num_pings_to_alive), NULL, "3" },
178 { "num_pings_to_alive", PW_TYPE_INTEGER,
179 offsetof(home_server,num_pings_to_alive), NULL, "3" },
180 { "revive_interval", PW_TYPE_INTEGER,
181 offsetof(home_server,revive_interval), NULL, "300" },
182 { "status_check_timeout", PW_TYPE_INTEGER,
183 offsetof(home_server,ping_timeout), NULL, "4" },
185 { "username", PW_TYPE_STRING_PTR,
186 offsetof(home_server,ping_user_name), NULL, NULL},
187 { "password", PW_TYPE_STRING_PTR,
188 offsetof(home_server,ping_user_password), NULL, NULL},
190 { NULL, -1, 0, NULL, NULL } /* end the list */
195 static int home_server_add(const char *filename, CONF_SECTION *cs)
200 name2 = cf_section_name1(cs);
201 if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
202 radlog(L_ERR, "%s[%d]: Section is not a home_server.",
203 filename, cf_section_lineno(cs));
207 name2 = cf_section_name2(cs);
209 radlog(L_ERR, "%s[%d]: Home server section is missing a name.",
210 filename, cf_section_lineno(cs));
214 home = rad_malloc(sizeof(*home));
215 memset(home, 0, sizeof(*home));
219 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
220 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
221 cf_section_parse(cs, home, home_server_config);
223 if (!home->hostname && (htonl(hs_ip4addr.s_addr) == INADDR_NONE) &&
224 IN6_IS_ADDR_UNSPECIFIED(&hs_ip6addr)) {
225 radlog(L_ERR, "%s[%d]: No hostname, IPv4 address, or IPv6 address defined for home server %s.",
226 filename, cf_section_lineno(cs), name2);
236 * FIXME: Parse home->hostname!
238 * Right now, only ipaddr && ip6addr are used.
239 * The old-style parsing still allows hostnames.
241 if (htonl(hs_ip4addr.s_addr) != INADDR_NONE) {
242 home->ipaddr.af = AF_INET;
243 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
245 } else if (!IN6_IS_ADDR_UNSPECIFIED(&hs_ip6addr)) {
246 home->ipaddr.af = AF_INET6;
247 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
250 radlog(L_ERR, "%s[%d]: FIXME: parse hostname for home server %s.",
251 filename, cf_section_lineno(cs), name2);
260 if (!home->port || (home->port > 65535)) {
261 radlog(L_ERR, "%s[%d]: No port, or invalid port defined for home server %s.",
262 filename, cf_section_lineno(cs), name2);
272 radlog(L_ERR, "%s[%d]: Fatal error! Home server %s is ourselves!",
273 filename, cf_section_lineno(cs), name2);
282 if (strcasecmp(hs_type, "auth") == 0) {
283 home->type = HOME_TYPE_AUTH;
285 } else if (strcasecmp(hs_type, "acct") == 0) {
286 home->type = HOME_TYPE_ACCT;
289 radlog(L_ERR, "%s[%d]: Invalid type \"%s\" for home server %s.",
290 filename, cf_section_lineno(cs), hs_type, name2);
302 radlog(L_ERR, "%s[%d]: No shared secret defined for home server %s.",
303 filename, cf_section_lineno(cs), name2);
308 if (strcasecmp(hs_check, "none") == 0) {
309 home->ping_check = HOME_PING_CHECK_NONE;
311 } else if (strcasecmp(hs_check, "status-server") == 0) {
312 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
314 } else if (strcasecmp(hs_check, "request") == 0) {
315 home->ping_check = HOME_PING_CHECK_REQUEST;
318 radlog(L_ERR, "%s[%d]: Invalid ping_check \"%s\" for home server %s.",
319 filename, cf_section_lineno(cs), hs_check, name2);
328 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
329 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
330 if (!home->ping_user_name) {
331 radlog(L_INFO, "%s[%d]: You must supply a user name to enable ping checks",
332 filename, cf_section_lineno(cs));
337 if ((home->type == HOME_TYPE_AUTH) &&
338 !home->ping_user_password) {
339 radlog(L_INFO, "%s[%d]: You must supply a password to enable ping checks",
340 filename, cf_section_lineno(cs));
346 if (rbtree_finddata(home_servers_byaddr, home)) {
347 radlog(L_INFO, "%s[%d]: Ignoring duplicate home server %s.",
348 filename, cf_section_lineno(cs), name2);
352 if (!rbtree_insert(home_servers_byname, home)) {
353 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
354 filename, cf_section_lineno(cs), name2);
359 if (!rbtree_insert(home_servers_byaddr, home)) {
360 rbtree_deletebydata(home_servers_byname, home);
361 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
362 filename, cf_section_lineno(cs), name2);
367 if (home->response_window < 5) home->response_window = 5;
368 if (home->response_window > 60) home->response_window = 60;
370 if (home->max_outstanding < 8) home->max_outstanding = 8;
371 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
373 if (home->ping_interval < 6) home->ping_interval = 6;
374 if (home->ping_interval > 120) home->ping_interval = 120;
376 if (home->zombie_period < 20) home->zombie_period = 20;
377 if (home->zombie_period > 120) home->zombie_period = 120;
379 if (home->zombie_period < home->response_window) {
380 home->zombie_period = home->response_window;
383 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
384 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
386 if (home->ping_timeout < 3) home->ping_timeout = 3;
387 if (home->ping_timeout > 10) home->ping_timeout = 10;
389 if (home->revive_interval < 60) home->revive_interval = 60;
390 if (home->revive_interval > 3600) home->revive_interval = 3600;
396 static int server_pool_add(const char *filename, CONF_SECTION *cs)
399 home_pool_t *pool = NULL;
402 int num_home_servers;
404 name2 = cf_section_name1(cs);
405 if (!name2 || (strcasecmp(name2, "server_pool") != 0)) {
406 radlog(L_ERR, "%s[%d]: Section is not a server_pool.",
407 filename, cf_section_lineno(cs));
411 name2 = cf_section_name2(cs);
413 radlog(L_ERR, "%s[%d]: Server pool section is missing a name.",
414 filename, cf_section_lineno(cs));
419 * Count the home servers and initalize them.
421 num_home_servers = 0;
422 for (cp = cf_pair_find(cs, "home_server");
424 cp = cf_pair_find_next(cs, cp, "home_server")) {
425 home_server myhome, *home;
426 CONF_SECTION *server_cs;
430 value = cf_pair_value(cp);
432 radlog(L_ERR, "%s[%d]: No value given for home_server.",
433 filename, cf_pair_lineno(cp));
438 home = rbtree_finddata(home_servers_byname, &myhome);
441 server_cs = cf_section_sub_find_name2(NULL,
445 radlog(L_ERR, "%s[%d]: Unknown home_server \"%s\".",
446 filename, cf_pair_lineno(cp), value);
450 if (!home_server_add(filename, server_cs)) {
454 home = rbtree_finddata(home_servers_byname, &myhome);
456 rad_assert("Internal sanity check failed");
461 if (num_home_servers == 0) {
462 radlog(L_ERR, "%s[%d]: No home servers defined in pool %s",
463 filename, cf_section_lineno(cs), name2);
467 pool = rad_malloc(sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
468 memset(pool, 0, sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
470 pool->type = HOME_POOL_FAIL_OVER;
473 DEBUG2(" server_pool %s {", name2);
475 cp = cf_pair_find(cs, "type");
477 static LRAD_NAME_NUMBER pool_types[] = {
478 { "load-balance", HOME_POOL_LOAD_BALANCE },
479 { "fail-over", HOME_POOL_FAIL_OVER },
480 { "round_robin", HOME_POOL_LOAD_BALANCE },
481 { "fail_over", HOME_POOL_FAIL_OVER },
482 { "client-balance", HOME_POOL_CLIENT_BALANCE },
483 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
487 value = cf_pair_value(cp);
489 radlog(L_ERR, "%s[%d]: No value given for type.",
490 filename, cf_pair_lineno(cp));
494 pool->type = lrad_str2int(pool_types, value, 0);
496 radlog(L_ERR, "%s[%d]: Unknown type \"%s\".",
497 filename, cf_pair_lineno(cp), value);
501 DEBUG2("\ttype = %s", name2, value);
504 for (cp = cf_pair_find(cs, "home_server");
506 cp = cf_pair_find_next(cs, cp, "home_server")) {
507 home_server myhome, *home;
509 value = cf_pair_value(cp);
511 radlog(L_ERR, "%s[%d]: No value given for home_server.",
512 filename, cf_pair_lineno(cp));
518 home = rbtree_finddata(home_servers_byname, &myhome);
520 DEBUG2("Internal sanity check failed");
524 if (!pool->server_type) {
525 rad_assert(home->type != 0);
526 pool->server_type = home->type;
528 } else if (pool->server_type != home->type) {
529 radlog(L_ERR, "%s[%d]: Home server \"%s\" is not of the same type as previous servers in server pool %s",
530 filename, cf_pair_lineno(cp), value, pool->name);
535 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
539 DEBUG2("\thome_server = %s", home->name);
540 pool->servers[pool->num_home_servers] = home;
541 pool->num_home_servers++;
542 } /* loop over home_server's */
544 if (!rbtree_insert(home_pools_byname, pool)) {
545 rad_assert("Internal sanity check failed");
551 rad_assert(pool->server_type != 0);
562 static int old_server_add(const char *filename, int lineno, const char *realm,
563 const char *name, const char *secret,
564 home_pool_type_t ldflag, home_pool_t **pool_p,
567 int i, insert_point, num_home_servers;
568 home_server myhome, *home;
569 home_pool_t mypool, *pool;
573 * LOCAL realms get sanity checked, and nothing else happens.
575 if (strcmp(name, "LOCAL") == 0) {
577 radlog(L_ERR, "%s[%d]: Realm \"%s\" cannot be both LOCAL and remote", filename, lineno, name);
584 pool = rbtree_finddata(home_pools_byname, &mypool);
586 if (pool->type != ldflag) {
587 radlog(L_ERR, "%s[%d]: Inconsistent ldflag for server pool \"%s\"", filename, lineno, name);
591 if (pool->server_type != type) {
592 radlog(L_ERR, "%s[%d]: Inconsistent home server type for server pool \"%s\"", filename, lineno, name);
598 home = rbtree_finddata(home_servers_byname, &myhome);
600 if (strcmp(home->secret, secret) != 0) {
601 radlog(L_ERR, "%s[%d]: Inconsistent shared secret for home server \"%s\"", filename, lineno, name);
605 if (home->type != type) {
606 radlog(L_ERR, "%s[%d]: Inconsistent type for home server \"%s\"", filename, lineno, name);
611 * See if the home server is already listed
612 * in the pool. If so, do nothing else.
614 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
615 if (pool->servers[i] == home) {
622 * If we do have a pool, check that there is room to
623 * insert the home server we've found, or the one that we
626 * Note that we insert it into the LAST available
627 * position, in order to maintain the same order as in
628 * the configuration files.
632 for (i = pool->num_home_servers - 1; i >= 0; i--) {
633 if (pool->servers[i]) break;
635 if (!pool->servers[i]) {
640 if (insert_point < 0) {
641 radlog(L_ERR, "%s[%d]: No room in pool to add home server \"%s\". Please update the realm configuration to use the new-style home servers and server pools.", filename, lineno, name);
647 * No home server, allocate one.
653 home = rad_malloc(sizeof(*home));
654 memset(home, 0, sizeof(*home));
657 home->hostname = name;
659 home->secret = secret;
661 p = strchr(name, ':');
663 if (type == HOME_TYPE_AUTH) {
664 home->port = PW_AUTH_UDP_PORT;
666 home->port = PW_ACCT_UDP_PORT;
672 } else if (p == name) {
673 radlog(L_ERR, "%s[%d]: Invalid hostname %s.",
674 filename, lineno, name);
679 home->port = atoi(p + 1);
680 if ((home->port == 0) || (home->port > 65535)) {
681 radlog(L_ERR, "%s[%d]: Invalid port %s.",
682 filename, lineno, p + 1);
687 q = rad_malloc((p - name) + 1);
688 memcpy(q, name, (p - name));
693 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
694 radlog(L_ERR, "%s[%d]: Failed looking up hostname %s.",
695 filename, lineno, p);
703 * Use the old-style configuration.
705 home->max_outstanding = 65535*16;
706 home->zombie_period = mainconfig.proxy_retry_delay * mainconfig.proxy_retry_count;
707 if (home->zombie_period == 0) home->zombie_period =30;
708 home->response_window = home->zombie_period - 1;
710 home->ping_check = HOME_PING_CHECK_NONE;
712 home->revive_interval = mainconfig.proxy_dead_time;
714 if (rbtree_finddata(home_servers_byaddr, home)) {
715 radlog(L_ERR, "%s[%d]: Home server %s has the same IP address as another home server.",
716 filename, lineno, name);
721 if (!rbtree_insert(home_servers_byname, home)) {
722 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
723 filename, lineno, name);
728 if (!rbtree_insert(home_servers_byaddr, home)) {
729 rbtree_deletebydata(home_servers_byname, home);
730 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
731 filename, lineno, name);
738 * We now have a home server, see if we can insert it
739 * into pre-existing pool.
741 if (insert_point >= 0) {
742 rad_assert(pool != NULL);
743 pool->servers[insert_point] = home;
747 rad_assert(pool == NULL);
748 rad_assert(home != NULL);
751 * Count the old-style realms of this name.
753 num_home_servers = 0;
754 for (cs = cf_section_sub_find_name2(mainconfig.config, "realm", realm);
756 cs = cf_section_sub_find_name2(cs, "realm", realm)) {
760 pool = rad_malloc(sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
761 memset(pool, 0, sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
765 pool->server_type = type;
766 pool->num_home_servers = num_home_servers;
767 pool->servers[0] = home;
769 if (!rbtree_insert(home_pools_byname, pool)) {
770 rad_assert("Internal sanity check failed");
779 static int old_realm_config(const char *filename, CONF_SECTION *cs, REALM *r)
783 home_pool_type_t ldflag;
785 secret = cf_section_value_find(cs, "secret");
787 host = cf_section_value_find(cs, "ldflag");
789 (strcasecmp(host, "fail_over") == 0)) {
790 ldflag = HOME_POOL_FAIL_OVER;
791 DEBUG2("\tldflag = fail_over");
793 } else if (strcasecmp(host, "round_robin") == 0) {
794 ldflag = HOME_POOL_LOAD_BALANCE;
795 DEBUG2("\tldflag = round_robin");
798 radlog(L_ERR, "%s[%d]: Unknown value \"%s\" for ldflag",
799 filename, cf_section_lineno(cs), host);
804 * Allow old-style if it doesn't exist, or if it exists and
807 if (((host = cf_section_value_find(cs, "authhost")) != NULL) &&
808 (strcmp(host, "LOCAL") != 0)) {
810 radlog(L_ERR, "%s[%d]: No shared secret supplied for realm: %s",
811 filename, cf_section_lineno(cs), r->name);
815 DEBUG2("\tauthhost = %s", host);
817 if (!old_server_add(filename, cf_section_lineno(cs),
818 r->name, host, secret, ldflag,
819 &r->auth_pool, HOME_TYPE_AUTH)) {
824 if (((host = cf_section_value_find(cs, "accthost")) != NULL) &&
825 (strcmp(host, "LOCAL") != 0)) {
827 radlog(L_ERR, "%s[%d]: No shared secret supplied for realm: %s",
828 filename, cf_section_lineno(cs), r->name);
832 DEBUG2("\taccthost = %s", host);
834 if (!old_server_add(filename, cf_section_lineno(cs),
835 r->name, host, secret, ldflag,
836 &r->acct_pool, HOME_TYPE_ACCT)) {
841 if (secret) DEBUG2("\tsecret = %s", secret);
848 static int add_pool_to_realm(const char *filename, int lineno,
849 const char *name, home_pool_t **dest,
852 home_pool_t mypool, *pool;
855 pool = rbtree_finddata(home_pools_byname, &mypool);
857 CONF_SECTION *pool_cs;
859 pool_cs = cf_section_sub_find_name2(NULL, "server_pool",
862 radlog(L_ERR, "%s[%d]: Failed to find server_pool \"%s\"",
863 filename, lineno, name);
867 if (!server_pool_add(filename, pool_cs)) {
871 pool = rbtree_finddata(home_pools_byname, &mypool);
873 rad_assert("Internal sanity check failed");
878 if (pool->server_type != server_type) {
879 radlog(L_ERR, "%s[%d]: Incompatible server_pool \"%s\" (mixed auth_pool / acct_pool)",
880 filename, lineno, name);
889 int realm_add(const char *filename, CONF_SECTION *cs)
894 home_pool_t *auth_pool, *acct_pool;
895 const char *auth_pool_name, *acct_pool_name;
897 name2 = cf_section_name1(cs);
898 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
899 radlog(L_ERR, "%s[%d]: Section is not a realm.",
900 filename, cf_section_lineno(cs));
904 name2 = cf_section_name2(cs);
906 radlog(L_ERR, "%s[%d]: Realm section is missing the realm name.",
907 filename, cf_section_lineno(cs));
911 auth_pool = acct_pool = NULL;
912 auth_pool_name = acct_pool_name = NULL;
915 * Prefer new configuration to old one.
917 cp = cf_pair_find(cs, "auth_pool");
918 if (cp) auth_pool_name = cf_pair_value(cp);
919 if (cp && auth_pool_name) {
920 if (!add_pool_to_realm(filename, cf_pair_lineno(cp),
921 auth_pool_name, &auth_pool,
927 cp = cf_pair_find(cs, "acct_pool");
928 if (cp) acct_pool_name = cf_pair_value(cp);
929 if (cp && acct_pool_name) {
930 if (!add_pool_to_realm(filename, cf_pair_lineno(cp),
931 acct_pool_name, &acct_pool,
937 DEBUG2(" realm %s {", name2);
940 * The realm MAY already exist if it's an old-style realm.
941 * In that case, merge the old-style realm with this one.
943 r = realm_find(name2);
945 if (cf_pair_find(cs, "auth_pool") ||
946 cf_pair_find(cs, "acct_pool")) {
947 radlog(L_ERR, "%s[%d]: Duplicate realm \"%s\"",
948 filename, cf_section_lineno(cs), name2);
952 if (!old_realm_config(filename, cs, r)) {
956 DEBUG2(" } # realm %s", name2);
960 r = rad_malloc(sizeof(*r));
961 memset(r, 0, sizeof(*r));
964 r->auth_pool = auth_pool;
965 r->acct_pool = acct_pool;
968 if (auth_pool_name) DEBUG2("\tauth_pool = %s", auth_pool_name);
969 if (acct_pool_name) DEBUG2("\tacct_pool = %s", acct_pool_name);
971 if ((cf_section_value_find(cs, "nostrip")) != NULL) {
973 DEBUG2("\tnostrip", name2);
977 * We're a new-style realm. Complain if we see the old
980 if (r->auth_pool || r->acct_pool) {
981 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
982 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
983 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
984 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
985 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
990 * The realm MAY be an old-style realm, as there
991 * was no auth_pool or acct_pool. Double-check
992 * it, just to be safe.
994 } else if (!old_realm_config(filename, cs, r)) {
998 if (!rbtree_insert(realms_byname, r)) {
999 rad_assert("Internal sanity check failed");
1008 DEBUG2(" } # realm %s", name2);
1015 * Find a realm in the REALM list.
1017 REALM *realm_find(const char *name)
1021 if (!name) name = "NULL";
1023 myrealm.name = name;
1024 return rbtree_finddata(realms_byname, &myrealm);
1028 home_server *home_server_ldb(const char *realmname,
1029 home_pool_t *pool, REQUEST *request)
1033 home_server *found = NULL;
1038 * Determine how to pick choose the home server.
1040 switch (pool->type) {
1044 * For load-balancing by client IP address, we
1045 * pick a home server by hashing the client IP.
1047 * This isn't as even a load distribution as
1048 * tracking the State attribute, but it's better
1051 case HOME_POOL_CLIENT_BALANCE:
1052 switch (request->packet->src_ipaddr.af) {
1054 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1055 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1058 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1059 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1065 start = hash % pool->num_home_servers;
1068 case HOME_POOL_CLIENT_PORT_BALANCE:
1069 switch (request->packet->src_ipaddr.af) {
1071 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1072 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1075 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1076 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1082 lrad_hash_update(&request->packet->src_port,
1083 sizeof(request->packet->src_port), hash);
1084 start = hash % pool->num_home_servers;
1087 case HOME_POOL_LOAD_BALANCE:
1088 found = pool->servers[0];
1096 * Starting with the home server we chose, loop through
1097 * all home servers. If the current one is dead, skip
1098 * it. If it is too busy, skip it.
1100 * Otherwise, use it.
1102 for (count = 0; count < pool->num_home_servers; count++) {
1103 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
1105 if (home->state == HOME_STATE_IS_DEAD) {
1110 * This home server is too busy. Choose another one.
1112 if (home->currently_outstanding >= home->max_outstanding) {
1116 if (pool->type != HOME_POOL_LOAD_BALANCE) {
1120 DEBUG3("PROXY %s %d\t%s %d",
1121 found->name, found->currently_outstanding,
1122 home->name, home->currently_outstanding);
1125 * Prefer this server if it's less busy than the
1126 * one we previously found.
1128 if (home->currently_outstanding < found->currently_outstanding) {
1129 DEBUG3("Choosing %s: It's less busy than %s",
1130 home->name, found->name);
1136 * Ignore servers which are busier than the one
1139 if (home->currently_outstanding > found->currently_outstanding) {
1140 DEBUG3("Skipping %s: It's busier than %s",
1141 home->name, found->name);
1146 * From the list of servers which have the same
1147 * load, choose one at random.
1149 if (((count + 1) * (lrad_rand() & 0xffff)) < (uint32_t) 0x10000) {
1153 } /* loop over the home servers */
1155 if (found) return found;
1158 * No live match found, and no fallback to the "DEFAULT"
1159 * realm. We fix this by blindly marking all servers as
1160 * "live". But only do it for ones that don't support
1161 * "pings", as they will be marked live when they
1162 * actually are live.
1164 if (!mainconfig.proxy_fallback &&
1165 mainconfig.wake_all_if_all_dead) {
1166 home_server *lb = NULL;
1168 for (count = 0; count < pool->num_home_servers; count++) {
1169 home_server *home = pool->servers[count];
1171 if ((home->state == HOME_STATE_IS_DEAD) &&
1172 (home->ping_check == HOME_PING_CHECK_NONE)) {
1173 home->state = HOME_STATE_ALIVE;
1182 * Still nothing. Look up the DEFAULT realm, but only
1183 * if we weren't looking up the NULL or DEFAULT realms.
1185 if (mainconfig.proxy_fallback &&
1187 (strcmp(realmname, "NULL") != 0) &&
1188 (strcmp(realmname, "DEFAULT") != 0)) {
1189 REALM *rd = realm_find("DEFAULT");
1191 if (!rd) return NULL;
1194 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1195 pool = rd->auth_pool;
1197 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1198 pool = rd->acct_pool;
1200 if (!pool) return NULL;
1202 DEBUG2(" Realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
1203 return home_server_ldb(rd->name, pool, request);
1207 * Still haven't found anything. Oh well.
1213 home_server *home_server_find(lrad_ipaddr_t *ipaddr, int port)
1217 myhome.ipaddr = *ipaddr;
1220 return rbtree_finddata(home_servers_byaddr, &myhome);