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 2000,2006 The FreeRADIUS server project
21 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
22 * Copyright 2000 Alan DeKok <aland@ox.org>
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/rad_assert.h>
36 static rbtree_t *realms_byname = NULL;
38 static rbtree_t *home_servers_byaddr = NULL;
39 static rbtree_t *home_servers_byname = NULL;
41 static rbtree_t *home_pools_byname = NULL;
43 static int realm_name_cmp(const void *one, const void *two)
48 return strcasecmp(a->name, b->name);
52 static int home_server_name_cmp(const void *one, const void *two)
54 const home_server *a = one;
55 const home_server *b = two;
57 return strcasecmp(a->name, b->name);
60 static int home_server_addr_cmp(const void *one, const void *two)
62 const home_server *a = one;
63 const home_server *b = two;
65 if (a->ipaddr.af < b->ipaddr.af) return -1;
66 if (a->ipaddr.af > b->ipaddr.af) return +1;
68 if (a->port < b->port) return -1;
69 if (a->port > b->port) return +1;
71 switch (a->ipaddr.af) {
73 return memcmp(&a->ipaddr.ipaddr.ip4addr,
74 &b->ipaddr.ipaddr.ip4addr,
75 sizeof(a->ipaddr.ipaddr.ip4addr));
78 return memcmp(&a->ipaddr.ipaddr.ip6addr,
79 &b->ipaddr.ipaddr.ip6addr,
80 sizeof(a->ipaddr.ipaddr.ip6addr));
90 static int home_pool_name_cmp(const void *one, const void *two)
92 const home_pool_t *a = one;
93 const home_pool_t *b = two;
95 return strcasecmp(a->name, b->name);
99 void realms_free(void)
101 rbtree_free(home_servers_byname);
102 home_servers_byname = NULL;
104 rbtree_free(home_servers_byaddr);
105 home_servers_byaddr = NULL;
107 rbtree_free(home_pools_byname);
108 home_pools_byname = NULL;
110 rbtree_free(realms_byname);
111 realms_byname = NULL;
115 int realms_init(const char *filename)
119 if (realms_byname) return 1;
121 realms_byname = rbtree_create(realm_name_cmp, free, 0);
122 if (!realms_byname) {
127 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
128 if (!home_servers_byaddr) {
133 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
134 if (!home_servers_byname) {
139 home_pools_byname = rbtree_create(home_pool_name_cmp, free, 0);
140 if (!home_pools_byname) {
145 for (cs = cf_subsection_find_next(mainconfig.config, NULL, "realm");
147 cs = cf_subsection_find_next(mainconfig.config, cs, "realm")) {
148 if (!realm_add(filename, cs)) {
157 static struct in_addr hs_ip4addr;
158 static struct in6_addr hs_ip6addr;
159 static char *hs_type = NULL;
160 static char *hs_check = NULL;
162 static CONF_PARSER home_server_config[] = {
163 { "ipaddr", PW_TYPE_IPADDR,
164 0, &hs_ip4addr, NULL },
165 { "ipv6addr", PW_TYPE_IPV6ADDR,
166 0, &hs_ip6addr, NULL },
168 { "hostname", PW_TYPE_STRING_PTR,
169 offsetof(home_server,hostname), NULL, NULL},
170 { "port", PW_TYPE_INTEGER,
171 offsetof(home_server,port), NULL, "0" },
173 { "type", PW_TYPE_STRING_PTR,
176 { "secret", PW_TYPE_STRING_PTR,
177 offsetof(home_server,secret), NULL, NULL},
179 { "response_window", PW_TYPE_INTEGER,
180 offsetof(home_server,response_window), NULL, "30" },
181 { "max_outstanding", PW_TYPE_INTEGER,
182 offsetof(home_server,max_outstanding), NULL, "65536" },
184 { "zombie_period", PW_TYPE_INTEGER,
185 offsetof(home_server,zombie_period), NULL, "40" },
186 { "ping_check", PW_TYPE_STRING_PTR,
187 0, &hs_check, "none" },
189 { "ping_interval", PW_TYPE_INTEGER,
190 offsetof(home_server,ping_interval), NULL, "30" },
191 { "num_pings_to_alive", PW_TYPE_INTEGER,
192 offsetof(home_server,num_pings_to_alive), NULL, "3" },
193 { "revive_interval", PW_TYPE_INTEGER,
194 offsetof(home_server,revive_interval), NULL, "300" },
196 { "username", PW_TYPE_STRING_PTR,
197 offsetof(home_server,ping_user_name), NULL, NULL},
198 { "password", PW_TYPE_STRING_PTR,
199 offsetof(home_server,ping_user_password), NULL, NULL},
201 { NULL, -1, 0, NULL, NULL } /* end the list */
206 static int home_server_add(const char *filename, CONF_SECTION *cs)
211 name2 = cf_section_name1(cs);
212 if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
213 radlog(L_ERR, "%s[%d]: Section is not a home_server.",
214 filename, cf_section_lineno(cs));
218 name2 = cf_section_name2(cs);
220 radlog(L_ERR, "%s[%d]: Home server section is missing a name.",
221 filename, cf_section_lineno(cs));
225 home = rad_malloc(sizeof(*home));
226 memset(home, 0, sizeof(*home));
230 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
231 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
232 cf_section_parse(cs, home, home_server_config);
234 if (!home->hostname && (htonl(hs_ip4addr.s_addr) == INADDR_NONE) &&
235 IN6_IS_ADDR_UNSPECIFIED(&hs_ip6addr)) {
236 radlog(L_ERR, "%s[%d]: No hostname, IPv4 address, or IPv6 address defined for home server %s.",
237 filename, cf_section_lineno(cs), name2);
247 * FIXME: Parse home->hostname!
249 * Right now, only ipaddr && ip6addr are used.
250 * The old-style parsing still allows hostnames.
252 if (htonl(hs_ip4addr.s_addr) != INADDR_NONE) {
253 home->ipaddr.af = AF_INET;
254 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
256 } else if (!IN6_IS_ADDR_UNSPECIFIED(&hs_ip6addr)) {
257 home->ipaddr.af = AF_INET6;
258 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
261 radlog(L_ERR, "%s[%d]: FIXME: parse hostname for home server %s.",
262 filename, cf_section_lineno(cs), name2);
271 if (!home->port || (home->port > 65535)) {
272 radlog(L_ERR, "%s[%d]: No port, or invalid port defined for home server %s.",
273 filename, cf_section_lineno(cs), name2);
283 radlog(L_ERR, "%s[%d]: Fatal error! Home server %s is ourselves!",
284 filename, cf_section_lineno(cs), name2);
293 if (strcasecmp(hs_type, "auth") == 0) {
294 home->type = HOME_TYPE_AUTH;
296 } else if (strcasecmp(hs_type, "acct") == 0) {
297 home->type = HOME_TYPE_ACCT;
300 radlog(L_ERR, "%s[%d]: Invalid type \"%s\" for home server %s.",
301 filename, cf_section_lineno(cs), hs_type, name2);
313 radlog(L_ERR, "%s[%d]: No shared secret defined for home server %s.",
314 filename, cf_section_lineno(cs), name2);
319 if (strcasecmp(hs_check, "none") == 0) {
320 home->ping_check = HOME_PING_CHECK_NONE;
322 } else if (strcasecmp(hs_check, "status-server") == 0) {
323 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
325 } else if (strcasecmp(hs_check, "request") == 0) {
326 home->ping_check = HOME_PING_CHECK_REQUEST;
329 radlog(L_ERR, "%s[%d]: Invalid ping_check \"%s\" for home server %s.",
330 filename, cf_section_lineno(cs), hs_check, name2);
339 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
340 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
341 if (!home->ping_user_name) {
342 radlog(L_INFO, "%s[%d]: You must supply a user name to enable ping checks",
343 filename, cf_section_lineno(cs));
348 if ((home->type == HOME_TYPE_AUTH) &&
349 !home->ping_user_password) {
350 radlog(L_INFO, "%s[%d]: You must supply a password to enable ping checks",
351 filename, cf_section_lineno(cs));
357 if (rbtree_finddata(home_servers_byaddr, home)) {
358 radlog(L_INFO, "%s[%d]: Ignoring duplicate home server %s.",
359 filename, cf_section_lineno(cs), name2);
363 if (!rbtree_insert(home_servers_byname, home)) {
364 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
365 filename, cf_section_lineno(cs), name2);
370 if (!rbtree_insert(home_servers_byaddr, home)) {
371 rbtree_deletebydata(home_servers_byname, home);
372 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
373 filename, cf_section_lineno(cs), name2);
378 if (home->response_window < 5) home->response_window = 5;
379 if (home->response_window > 60) home->response_window = 60;
381 if (home->max_outstanding < 8) home->max_outstanding = 8;
382 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
384 if (home->ping_interval < 6) home->ping_interval = 6;
385 if (home->ping_interval > 120) home->ping_interval = 120;
387 if (home->zombie_period < 20) home->zombie_period = 20;
388 if (home->zombie_period > 120) home->zombie_period = 120;
390 if (home->zombie_period < home->response_window) {
391 home->zombie_period = home->response_window;
394 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
395 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
397 if (home->revive_interval < 60) home->revive_interval = 60;
398 if (home->revive_interval > 3600) home->revive_interval = 3600;
404 static int server_pool_add(const char *filename, CONF_SECTION *cs)
410 int num_home_servers;
412 name2 = cf_section_name1(cs);
413 if (!name2 || (strcasecmp(name2, "server_pool") != 0)) {
414 radlog(L_ERR, "%s[%d]: Section is not a server_pool.",
415 filename, cf_section_lineno(cs));
419 name2 = cf_section_name2(cs);
421 radlog(L_ERR, "%s[%d]: Server pool section is missing a name.",
422 filename, cf_section_lineno(cs));
426 num_home_servers = 0;
427 for (cp = cf_pair_find(cs, "home_server");
429 cp = cf_pair_find_next(cs, cp, "home_server")) {
433 if (num_home_servers == 0) {
434 radlog(L_ERR, "%s[%d]: No home servers defined in pool %s",
435 filename, cf_section_lineno(cs), name2);
439 pool = rad_malloc(sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
440 memset(pool, 0, sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
442 pool->type = HOME_POOL_FAIL_OVER;
445 cp = cf_pair_find(cs, "type");
447 static LRAD_NAME_NUMBER pool_types[] = {
448 { "load-balance", HOME_POOL_LOAD_BALANCE },
449 { "fail-over", HOME_POOL_FAIL_OVER },
450 { "round_robin", HOME_POOL_LOAD_BALANCE },
451 { "fail_over", HOME_POOL_FAIL_OVER },
452 { "client-balance", HOME_POOL_CLIENT_BALANCE },
456 value = cf_pair_value(cp);
458 radlog(L_ERR, "%s[%d]: No value given for type.",
459 filename, cf_pair_lineno(cp));
464 pool->type = lrad_str2int(pool_types, value, 0);
466 radlog(L_ERR, "%s[%d]: Unknown type \"%s\".",
467 filename, cf_pair_lineno(cp), value);
472 DEBUG2(" server_pool %s: type = %s", name2, value);
475 for (cp = cf_pair_find(cs, "home_server");
477 cp = cf_pair_find_next(cs, cp, "home_server")) {
478 home_server myhome, *home;
480 value = cf_pair_value(cp);
482 radlog(L_ERR, "%s[%d]: No value given for home_server.",
483 filename, cf_pair_lineno(cp));
490 home = rbtree_finddata(home_servers_byname, &myhome);
492 CONF_SECTION *server_cs;
494 server_cs = cf_section_sub_find_name2(NULL,
498 radlog(L_ERR, "%s[%d]: Unknown home_server \"%s\".",
499 filename, cf_pair_lineno(cp), value);
504 if (!home_server_add(filename, server_cs)) {
509 home = rbtree_finddata(home_servers_byname, &myhome);
511 rad_assert("Internal sanity check failed");
516 if (!pool->server_type) {
517 rad_assert(home->type != 0);
518 pool->server_type = home->type;
520 } else if (pool->server_type != home->type) {
521 radlog(L_ERR, "%s[%d]: Home server \"%s\" is not of the same type as previous servers in server pool %s",
522 filename, cf_pair_lineno(cp), value, pool->name);
528 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
532 DEBUG2(" server_pool %s: home_server = %s", name2, home->name);
533 pool->servers[pool->num_home_servers] = home;
534 pool->num_home_servers++;
536 } /* loop over home_server's */
538 if (!rbtree_insert(home_pools_byname, pool)) {
539 rad_assert("Internal sanity check failed");
543 rad_assert(pool->server_type != 0);
549 static int old_server_add(const char *filename, int lineno,
550 const char *name, const char *secret,
551 home_pool_type_t ldflag, home_pool_t **pool_p,
554 int i, insert_point, num_home_servers;
555 home_server myhome, *home;
556 home_pool_t mypool, *pool;
560 * LOCAL realms get sanity checked, and nothing else happens.
562 if (strcmp(name, "LOCAL") == 0) {
564 radlog(L_ERR, "%s[%d]: Realm \"%s\" cannot be both LOCAL and remote", filename, lineno, name);
571 pool = rbtree_finddata(home_pools_byname, &mypool);
573 if (pool->type != ldflag) {
574 radlog(L_ERR, "%s[%d]: Inconsistent ldflag for server pool \"%s\"", filename, lineno, name);
578 if (pool->server_type != type) {
579 radlog(L_ERR, "%s[%d]: Inconsistent home server type for server pool \"%s\"", filename, lineno, name);
585 home = rbtree_finddata(home_servers_byname, &myhome);
587 if (strcmp(home->secret, secret) != 0) {
588 radlog(L_ERR, "%s[%d]: Inconsistent shared secret for home server \"%s\"", filename, lineno, name);
592 if (home->type != type) {
593 radlog(L_ERR, "%s[%d]: Inconsistent type for home server \"%s\"", filename, lineno, name);
598 * See if the home server is already listed
599 * in the pool. If so, do nothing else.
601 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
602 if (pool->servers[i] == home) {
609 * If we do have a pool, check that there is room to
610 * insert the home server we've found, or the one that we
613 * Note that we insert it into the LAST available
614 * position, in order to maintain the same order as in
615 * the configuration files.
619 for (i = pool->num_home_servers - 1; i >= 0; i--) {
620 if (pool->servers[i]) break;
622 if (!pool->servers[i]) {
627 if (insert_point < 0) {
628 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);
634 * No home server, allocate one.
640 home = rad_malloc(sizeof(*home));
641 memset(home, 0, sizeof(*home));
644 home->hostname = name;
646 home->secret = secret;
648 p = strchr(name, ':');
650 if (type == HOME_TYPE_AUTH) {
651 home->port = PW_AUTH_UDP_PORT;
653 home->port = PW_ACCT_UDP_PORT;
659 } else if (p == name) {
660 radlog(L_ERR, "%s[%d]: Invalid hostname %s.",
661 filename, lineno, name);
666 home->port = atoi(p + 1);
667 if ((home->port == 0) || (home->port > 65535)) {
668 radlog(L_ERR, "%s[%d]: Invalid port %s.",
669 filename, lineno, p + 1);
674 q = rad_malloc((p - name) + 1);
675 memcpy(q, name, (p - name));
680 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
681 radlog(L_ERR, "%s[%d]: Failed looking up hostname %s.",
682 filename, lineno, p);
690 * Use the old-style configuration.
692 home->max_outstanding = 65535*16;
693 home->zombie_period = mainconfig.proxy_retry_delay * mainconfig.proxy_retry_count;
694 if (home->zombie_period == 0) home->zombie_period =30;
695 home->response_window = home->zombie_period - 1;
697 home->ping_check = HOME_PING_CHECK_NONE;
699 home->revive_interval = mainconfig.proxy_dead_time;
701 if (rbtree_finddata(home_servers_byaddr, home)) {
702 radlog(L_ERR, "%s[%d]: Home server %s has the same IP address as another home server.",
703 filename, lineno, name);
708 if (!rbtree_insert(home_servers_byname, home)) {
709 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
710 filename, lineno, name);
715 if (!rbtree_insert(home_servers_byaddr, home)) {
716 rbtree_deletebydata(home_servers_byname, home);
717 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
718 filename, lineno, name);
725 * We now have a home server, see if we can insert it
726 * into pre-existing pool.
728 if (insert_point >= 0) {
729 rad_assert(pool != NULL);
730 pool->servers[insert_point] = home;
734 rad_assert(pool == NULL);
735 rad_assert(home != NULL);
738 * Count the old-style realms of this name.
740 num_home_servers = 0;
741 for (cs = cf_section_sub_find_name2(mainconfig.config, "realm", name);
743 cs = cf_section_sub_find_name2(cs, "realm", name)) {
748 pool = rad_malloc(sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
749 memset(pool, 0, sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
753 pool->server_type = type;
754 pool->num_home_servers = num_home_servers;
755 pool->servers[0] = home;
757 if (!rbtree_insert(home_pools_byname, pool)) {
758 rad_assert("Internal sanity check failed");
767 static int old_realm_config(const char *filename, CONF_SECTION *cs, REALM *r)
771 home_pool_type_t ldflag;
773 secret = cf_section_value_find(cs, "secret");
775 host = cf_section_value_find(cs, "ldflag");
777 (strcasecmp(host, "fail_over") == 0)) {
778 ldflag = HOME_POOL_FAIL_OVER;
779 DEBUG2(" realm %s: ldflag = fail_over", r->name);
781 } else if (strcasecmp(host, "round_robin") == 0) {
782 ldflag = HOME_POOL_LOAD_BALANCE;
783 DEBUG2(" realm %s: ldflag = round_robin", r->name);
786 radlog(L_ERR, "%s[%d]: Unknown value \"%s\" for ldflag",
787 filename, cf_section_lineno(cs), host);
792 * Allow old-style if it doesn't exist, or if it exists and
795 if (((host = cf_section_value_find(cs, "authhost")) != NULL) &&
796 (strcmp(host, "LOCAL") != 0)) {
798 radlog(L_ERR, "%s[%d]: No shared secret supplied for realm: %s",
799 filename, cf_section_lineno(cs), r->name);
803 DEBUG2(" realm %s: authhost = %s", r->name, host);
805 if (!old_server_add(filename, cf_section_lineno(cs),
806 host, secret, ldflag,
807 &r->auth_pool, HOME_TYPE_AUTH)) {
812 if (((host = cf_section_value_find(cs, "accthost")) != NULL) &&
813 (strcmp(host, "LOCAL") != 0)) {
815 radlog(L_ERR, "%s[%d]: No shared secret supplied for realm: %s",
816 filename, cf_section_lineno(cs), r->name);
820 DEBUG2(" realm %s: accthost = %s", r->name, host);
822 if (!old_server_add(filename, cf_section_lineno(cs),
823 host, secret, ldflag,
824 &r->auth_pool, HOME_TYPE_ACCT)) {
829 if (secret) DEBUG2(" realm %s: secret = %s", r->name, secret);
836 static int add_pool_to_realm(const char *filename, int lineno,
837 const char *name, home_pool_t **dest,
840 home_pool_t mypool, *pool;
843 pool = rbtree_finddata(home_pools_byname, &mypool);
845 CONF_SECTION *pool_cs;
847 pool_cs = cf_section_sub_find_name2(NULL, "server_pool",
850 radlog(L_ERR, "%s[%d]: Failed to find server_pool \"%s\"",
851 filename, lineno, name);
855 if (!server_pool_add(filename, pool_cs)) {
859 pool = rbtree_finddata(home_pools_byname, &mypool);
861 rad_assert("Internal sanity check failed");
866 if (pool->server_type != server_type) {
867 radlog(L_ERR, "%s[%d]: Incompatible server_pool \"%s\" (mixed auth_pool / acct_pool)",
868 filename, lineno, name);
877 int realm_add(const char *filename, CONF_SECTION *cs)
884 name2 = cf_section_name1(cs);
885 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
886 radlog(L_ERR, "%s[%d]: Section is not a realm.",
887 filename, cf_section_lineno(cs));
891 name2 = cf_section_name2(cs);
893 radlog(L_ERR, "%s[%d]: Realm section is missing the realm name.",
894 filename, cf_section_lineno(cs));
899 * The realm MAY already exist if it's an old-style realm.
900 * In that case, merge the old-style realm with this one.
902 r = realm_find(name2);
904 if (cf_pair_find(cs, "auth_pool") ||
905 cf_pair_find(cs, "acct_pool")) {
906 radlog(L_ERR, "%s[%d]: Duplicate realm \"%s\"",
907 filename, cf_section_lineno(cs), name2);
911 if (!old_realm_config(filename, cs, r)) {
918 r = rad_malloc(sizeof(*r));
919 memset(r, 0, sizeof(*r));
924 * Prefer new configuration to old one.
926 cp = cf_pair_find(cs, "auth_pool");
927 if (cp) pool = cf_pair_value(cp);
929 if (!add_pool_to_realm(filename, cf_pair_lineno(cp),
930 pool, &r->auth_pool, HOME_TYPE_AUTH)) {
934 DEBUG2(" realm %s: auth_pool = %s", name2, pool);
937 cp = cf_pair_find(cs, "acct_pool");
938 if (cp) pool = cf_pair_value(cp);
940 if (!add_pool_to_realm(filename, cf_pair_lineno(cp),
941 pool, &r->acct_pool, HOME_TYPE_ACCT)) {
945 DEBUG2(" realm %s: acct_pool = %s", name2, pool);
950 if ((cf_section_value_find(cs, "nostrip")) != NULL) {
952 DEBUG2(" realm %s: nostrip", name2);
956 * We're a new-style realm. Complain if we see the old
959 if (r->auth_pool || r->acct_pool) {
960 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
961 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
962 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
963 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
964 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
969 * The realm MAY be an old-style realm, as there
970 * was no auth_pool or acct_pool. Double-check
971 * it, just to be safe.
973 } else if (!old_realm_config(filename, cs, r)) {
978 if (!rbtree_insert(realms_byname, r)) {
979 rad_assert("Internal sanity check failed");
989 * Find a realm in the REALM list.
991 REALM *realm_find(const char *name)
995 if (!name) name = "NULL";
998 return rbtree_finddata(realms_byname, &myrealm);
1002 home_server *home_server_ldb(const char *realmname,
1003 home_pool_t *pool, REQUEST *request)
1011 * Determine how to pick choose the home server.
1013 switch (pool->type) {
1017 * Load balancing. Pick one at random.
1019 case HOME_POOL_LOAD_BALANCE:
1021 start = hash % pool->num_home_servers;
1025 * For load-balancing by client IP address, we
1026 * pick a home server by hashing the client IP.
1028 * This isn't as even a load distribution as
1029 * tracking the State attribute, but it's better
1032 case HOME_POOL_CLIENT_BALANCE:
1033 switch (request->packet->src_ipaddr.af) {
1035 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1036 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1039 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1040 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1046 start = hash % pool->num_home_servers;
1055 * Starting with the home server we chose, loop through
1056 * all home servers. If the current one is dead, skip
1057 * it. If it is too busy, skip it.
1059 * Otherwise, use it.
1061 for (count = 0; count < pool->num_home_servers; count++) {
1062 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
1064 if (home->state == HOME_STATE_IS_DEAD) {
1069 * This home server is too busy. Choose another one.
1071 if (home->currently_outstanding >= home->max_outstanding) {
1076 } /* loop over the home servers */
1079 * No live match found, and no fallback to the "DEFAULT"
1080 * realm. We fix this by blindly marking all servers as
1081 * "live". But only do it for ones that don't support
1082 * "pings", as they will be marked live when they
1083 * actually are live.
1085 if (!mainconfig.proxy_fallback &&
1086 mainconfig.wake_all_if_all_dead) {
1087 home_server *lb = NULL;
1089 for (count = 0; count < pool->num_home_servers; count++) {
1090 home_server *home = pool->servers[count];
1092 if ((home->state == HOME_STATE_IS_DEAD) &&
1093 (home->ping_check == HOME_PING_CHECK_NONE)) {
1094 home->state = HOME_STATE_ALIVE;
1103 * Still nothing. Look up the DEFAULT realm, but only
1104 * if we weren't looking up the NULL or DEFAULT realms.
1106 if (mainconfig.proxy_fallback &&
1108 (strcmp(realmname, "NULL") != 0) &&
1109 (strcmp(realmname, "DEFAULT") != 0)) {
1110 REALM *rd = realm_find("DEFAULT");
1112 if (!rd) return NULL;
1115 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1116 pool = rd->auth_pool;
1118 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1119 pool = rd->acct_pool;
1121 if (!pool) return NULL;
1123 DEBUG2(" Realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
1124 return home_server_ldb(rd->name, pool, request);
1128 * Still haven't found anything. Oh well.
1134 home_server *home_server_find(lrad_ipaddr_t *ipaddr, int port)
1138 myhome.ipaddr = *ipaddr;
1141 return rbtree_finddata(home_servers_byaddr, &myhome);