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->port < b->port) return -1;
66 if (a->port > b->port) return +1;
68 return lrad_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
72 static int home_pool_name_cmp(const void *one, const void *two)
74 const home_pool_t *a = one;
75 const home_pool_t *b = two;
77 return strcasecmp(a->name, b->name);
81 void realms_free(void)
83 rbtree_free(home_servers_byname);
84 home_servers_byname = NULL;
86 rbtree_free(home_servers_byaddr);
87 home_servers_byaddr = NULL;
89 rbtree_free(home_pools_byname);
90 home_pools_byname = NULL;
92 rbtree_free(realms_byname);
97 int realms_init(const char *filename)
101 if (realms_byname) return 1;
103 realms_byname = rbtree_create(realm_name_cmp, free, 0);
104 if (!realms_byname) {
109 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
110 if (!home_servers_byaddr) {
115 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
116 if (!home_servers_byname) {
121 home_pools_byname = rbtree_create(home_pool_name_cmp, free, 0);
122 if (!home_pools_byname) {
127 for (cs = cf_subsection_find_next(mainconfig.config, NULL, "realm");
129 cs = cf_subsection_find_next(mainconfig.config, cs, "realm")) {
130 if (!realm_add(filename, cs)) {
139 static struct in_addr hs_ip4addr;
140 static struct in6_addr hs_ip6addr;
141 static char *hs_type = NULL;
142 static char *hs_check = NULL;
144 static CONF_PARSER home_server_config[] = {
145 { "ipaddr", PW_TYPE_IPADDR,
146 0, &hs_ip4addr, NULL },
147 { "ipv6addr", PW_TYPE_IPV6ADDR,
148 0, &hs_ip6addr, NULL },
150 { "hostname", PW_TYPE_STRING_PTR,
151 offsetof(home_server,hostname), NULL, NULL},
152 { "port", PW_TYPE_INTEGER,
153 offsetof(home_server,port), NULL, "0" },
155 { "type", PW_TYPE_STRING_PTR,
158 { "secret", PW_TYPE_STRING_PTR,
159 offsetof(home_server,secret), NULL, NULL},
161 { "response_window", PW_TYPE_INTEGER,
162 offsetof(home_server,response_window), NULL, "30" },
163 { "max_outstanding", PW_TYPE_INTEGER,
164 offsetof(home_server,max_outstanding), NULL, "65536" },
166 { "zombie_period", PW_TYPE_INTEGER,
167 offsetof(home_server,zombie_period), NULL, "40" },
168 { "status_check", PW_TYPE_STRING_PTR,
169 0, &hs_check, "none" },
170 { "ping_check", PW_TYPE_STRING_PTR,
171 0, &hs_check, "none" },
173 { "ping_interval", PW_TYPE_INTEGER,
174 offsetof(home_server,ping_interval), NULL, "30" },
175 { "check_interval", PW_TYPE_INTEGER,
176 offsetof(home_server,ping_interval), NULL, "30" },
177 { "num_answers_to_alive", PW_TYPE_INTEGER,
178 offsetof(home_server,num_pings_to_alive), NULL, "3" },
179 { "num_pings_to_alive", PW_TYPE_INTEGER,
180 offsetof(home_server,num_pings_to_alive), NULL, "3" },
181 { "revive_interval", PW_TYPE_INTEGER,
182 offsetof(home_server,revive_interval), NULL, "300" },
183 { "status_check_timeout", PW_TYPE_INTEGER,
184 offsetof(home_server,ping_timeout), NULL, "4" },
186 { "username", PW_TYPE_STRING_PTR,
187 offsetof(home_server,ping_user_name), NULL, NULL},
188 { "password", PW_TYPE_STRING_PTR,
189 offsetof(home_server,ping_user_password), NULL, NULL},
191 { NULL, -1, 0, NULL, NULL } /* end the list */
196 static int home_server_add(const char *filename, CONF_SECTION *cs)
201 name2 = cf_section_name1(cs);
202 if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
203 radlog(L_ERR, "%s[%d]: Section is not a home_server.",
204 filename, cf_section_lineno(cs));
208 name2 = cf_section_name2(cs);
210 radlog(L_ERR, "%s[%d]: Home server section is missing a name.",
211 filename, cf_section_lineno(cs));
215 home = rad_malloc(sizeof(*home));
216 memset(home, 0, sizeof(*home));
220 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
221 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
222 cf_section_parse(cs, home, home_server_config);
224 if (!home->hostname && (htonl(hs_ip4addr.s_addr) == INADDR_NONE) &&
225 IN6_IS_ADDR_UNSPECIFIED(&hs_ip6addr)) {
226 radlog(L_ERR, "%s[%d]: No hostname, IPv4 address, or IPv6 address defined for home server %s.",
227 filename, cf_section_lineno(cs), name2);
237 * FIXME: Parse home->hostname!
239 * Right now, only ipaddr && ip6addr are used.
240 * The old-style parsing still allows hostnames.
242 if (htonl(hs_ip4addr.s_addr) != INADDR_NONE) {
243 home->ipaddr.af = AF_INET;
244 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
246 } else if (!IN6_IS_ADDR_UNSPECIFIED(&hs_ip6addr)) {
247 home->ipaddr.af = AF_INET6;
248 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
251 radlog(L_ERR, "%s[%d]: FIXME: parse hostname for home server %s.",
252 filename, cf_section_lineno(cs), name2);
261 if (!home->port || (home->port > 65535)) {
262 radlog(L_ERR, "%s[%d]: No port, or invalid port defined for home server %s.",
263 filename, cf_section_lineno(cs), name2);
273 radlog(L_ERR, "%s[%d]: Fatal error! Home server %s is ourselves!",
274 filename, cf_section_lineno(cs), name2);
283 if (strcasecmp(hs_type, "auth") == 0) {
284 home->type = HOME_TYPE_AUTH;
286 } else if (strcasecmp(hs_type, "acct") == 0) {
287 home->type = HOME_TYPE_ACCT;
290 radlog(L_ERR, "%s[%d]: Invalid type \"%s\" for home server %s.",
291 filename, cf_section_lineno(cs), hs_type, name2);
303 radlog(L_ERR, "%s[%d]: No shared secret defined for home server %s.",
304 filename, cf_section_lineno(cs), name2);
309 if (strcasecmp(hs_check, "none") == 0) {
310 home->ping_check = HOME_PING_CHECK_NONE;
312 } else if (strcasecmp(hs_check, "status-server") == 0) {
313 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
315 } else if (strcasecmp(hs_check, "request") == 0) {
316 home->ping_check = HOME_PING_CHECK_REQUEST;
319 radlog(L_ERR, "%s[%d]: Invalid ping_check \"%s\" for home server %s.",
320 filename, cf_section_lineno(cs), hs_check, name2);
329 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
330 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
331 if (!home->ping_user_name) {
332 radlog(L_INFO, "%s[%d]: You must supply a user name to enable ping checks",
333 filename, cf_section_lineno(cs));
338 if ((home->type == HOME_TYPE_AUTH) &&
339 !home->ping_user_password) {
340 radlog(L_INFO, "%s[%d]: You must supply a password to enable ping checks",
341 filename, cf_section_lineno(cs));
347 if (rbtree_finddata(home_servers_byaddr, home)) {
348 radlog(L_INFO, "%s[%d]: Ignoring duplicate home server %s.",
349 filename, cf_section_lineno(cs), name2);
353 if (!rbtree_insert(home_servers_byname, home)) {
354 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
355 filename, cf_section_lineno(cs), name2);
360 if (!rbtree_insert(home_servers_byaddr, home)) {
361 rbtree_deletebydata(home_servers_byname, home);
362 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
363 filename, cf_section_lineno(cs), name2);
368 if (home->response_window < 5) home->response_window = 5;
369 if (home->response_window > 60) home->response_window = 60;
371 if (home->max_outstanding < 8) home->max_outstanding = 8;
372 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
374 if (home->ping_interval < 6) home->ping_interval = 6;
375 if (home->ping_interval > 120) home->ping_interval = 120;
377 if (home->zombie_period < 20) home->zombie_period = 20;
378 if (home->zombie_period > 120) home->zombie_period = 120;
380 if (home->zombie_period < home->response_window) {
381 home->zombie_period = home->response_window;
384 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
385 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
387 if (home->ping_timeout < 3) home->ping_timeout = 3;
388 if (home->ping_timeout > 10) home->ping_timeout = 10;
390 if (home->revive_interval < 60) home->revive_interval = 60;
391 if (home->revive_interval > 3600) home->revive_interval = 3600;
397 static int server_pool_add(const char *filename, CONF_SECTION *cs)
400 home_pool_t *pool = NULL;
403 int num_home_servers;
405 name2 = cf_section_name1(cs);
406 if (!name2 || (strcasecmp(name2, "server_pool") != 0)) {
407 radlog(L_ERR, "%s[%d]: Section is not a server_pool.",
408 filename, cf_section_lineno(cs));
412 name2 = cf_section_name2(cs);
414 radlog(L_ERR, "%s[%d]: Server pool section is missing a name.",
415 filename, cf_section_lineno(cs));
419 DEBUG2(" server_pool %s {", name2);
421 num_home_servers = 0;
422 for (cp = cf_pair_find(cs, "home_server");
424 cp = cf_pair_find_next(cs, cp, "home_server")) {
428 if (num_home_servers == 0) {
429 radlog(L_ERR, "%s[%d]: No home servers defined in pool %s",
430 filename, cf_section_lineno(cs), name2);
434 pool = rad_malloc(sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
435 memset(pool, 0, sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
437 pool->type = HOME_POOL_FAIL_OVER;
440 cp = cf_pair_find(cs, "type");
442 static LRAD_NAME_NUMBER pool_types[] = {
443 { "load-balance", HOME_POOL_LOAD_BALANCE },
444 { "fail-over", HOME_POOL_FAIL_OVER },
445 { "round_robin", HOME_POOL_LOAD_BALANCE },
446 { "fail_over", HOME_POOL_FAIL_OVER },
447 { "client-balance", HOME_POOL_CLIENT_BALANCE },
451 value = cf_pair_value(cp);
453 radlog(L_ERR, "%s[%d]: No value given for type.",
454 filename, cf_pair_lineno(cp));
458 pool->type = lrad_str2int(pool_types, value, 0);
460 radlog(L_ERR, "%s[%d]: Unknown type \"%s\".",
461 filename, cf_pair_lineno(cp), value);
465 DEBUG2("\ttype = %s", name2, value);
468 for (cp = cf_pair_find(cs, "home_server");
470 cp = cf_pair_find_next(cs, cp, "home_server")) {
471 home_server myhome, *home;
473 value = cf_pair_value(cp);
475 radlog(L_ERR, "%s[%d]: No value given for home_server.",
476 filename, cf_pair_lineno(cp));
482 home = rbtree_finddata(home_servers_byname, &myhome);
484 CONF_SECTION *server_cs;
486 server_cs = cf_section_sub_find_name2(NULL,
490 radlog(L_ERR, "%s[%d]: Unknown home_server \"%s\".",
491 filename, cf_pair_lineno(cp), value);
495 DEBUG2(" server_pool %s: Adding home_server %s",
498 if (!home_server_add(filename, server_cs)) {
502 home = rbtree_finddata(home_servers_byname, &myhome);
504 rad_assert("Internal sanity check failed");
509 if (!pool->server_type) {
510 rad_assert(home->type != 0);
511 pool->server_type = home->type;
513 } else if (pool->server_type != home->type) {
514 radlog(L_ERR, "%s[%d]: Home server \"%s\" is not of the same type as previous servers in server pool %s",
515 filename, cf_pair_lineno(cp), value, pool->name);
520 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
524 DEBUG2("\thome_server = %s", name2, home->name);
525 pool->servers[pool->num_home_servers] = home;
526 pool->num_home_servers++;
528 } /* loop over home_server's */
530 if (!rbtree_insert(home_pools_byname, pool)) {
531 rad_assert("Internal sanity check failed");
535 DEBUG2(" } # server_pool %s", name2);
537 rad_assert(pool->server_type != 0);
542 DEBUG2(" } # server_pool %s", name2);
548 static int old_server_add(const char *filename, int lineno,
549 const char *name, const char *secret,
550 home_pool_type_t ldflag, home_pool_t **pool_p,
553 int i, insert_point, num_home_servers;
554 home_server myhome, *home;
555 home_pool_t mypool, *pool;
559 * LOCAL realms get sanity checked, and nothing else happens.
561 if (strcmp(name, "LOCAL") == 0) {
563 radlog(L_ERR, "%s[%d]: Realm \"%s\" cannot be both LOCAL and remote", filename, lineno, name);
570 pool = rbtree_finddata(home_pools_byname, &mypool);
572 if (pool->type != ldflag) {
573 radlog(L_ERR, "%s[%d]: Inconsistent ldflag for server pool \"%s\"", filename, lineno, name);
577 if (pool->server_type != type) {
578 radlog(L_ERR, "%s[%d]: Inconsistent home server type for server pool \"%s\"", filename, lineno, name);
584 home = rbtree_finddata(home_servers_byname, &myhome);
586 if (strcmp(home->secret, secret) != 0) {
587 radlog(L_ERR, "%s[%d]: Inconsistent shared secret for home server \"%s\"", filename, lineno, name);
591 if (home->type != type) {
592 radlog(L_ERR, "%s[%d]: Inconsistent type for home server \"%s\"", filename, lineno, name);
597 * See if the home server is already listed
598 * in the pool. If so, do nothing else.
600 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
601 if (pool->servers[i] == home) {
608 * If we do have a pool, check that there is room to
609 * insert the home server we've found, or the one that we
612 * Note that we insert it into the LAST available
613 * position, in order to maintain the same order as in
614 * the configuration files.
618 for (i = pool->num_home_servers - 1; i >= 0; i--) {
619 if (pool->servers[i]) break;
621 if (!pool->servers[i]) {
626 if (insert_point < 0) {
627 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);
633 * No home server, allocate one.
639 home = rad_malloc(sizeof(*home));
640 memset(home, 0, sizeof(*home));
643 home->hostname = name;
645 home->secret = secret;
647 p = strchr(name, ':');
649 if (type == HOME_TYPE_AUTH) {
650 home->port = PW_AUTH_UDP_PORT;
652 home->port = PW_ACCT_UDP_PORT;
658 } else if (p == name) {
659 radlog(L_ERR, "%s[%d]: Invalid hostname %s.",
660 filename, lineno, name);
665 home->port = atoi(p + 1);
666 if ((home->port == 0) || (home->port > 65535)) {
667 radlog(L_ERR, "%s[%d]: Invalid port %s.",
668 filename, lineno, p + 1);
673 q = rad_malloc((p - name) + 1);
674 memcpy(q, name, (p - name));
679 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
680 radlog(L_ERR, "%s[%d]: Failed looking up hostname %s.",
681 filename, lineno, p);
689 * Use the old-style configuration.
691 home->max_outstanding = 65535*16;
692 home->zombie_period = mainconfig.proxy_retry_delay * mainconfig.proxy_retry_count;
693 if (home->zombie_period == 0) home->zombie_period =30;
694 home->response_window = home->zombie_period - 1;
696 home->ping_check = HOME_PING_CHECK_NONE;
698 home->revive_interval = mainconfig.proxy_dead_time;
700 if (rbtree_finddata(home_servers_byaddr, home)) {
701 radlog(L_ERR, "%s[%d]: Home server %s has the same IP address as another home server.",
702 filename, lineno, name);
707 if (!rbtree_insert(home_servers_byname, home)) {
708 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
709 filename, lineno, name);
714 if (!rbtree_insert(home_servers_byaddr, home)) {
715 rbtree_deletebydata(home_servers_byname, home);
716 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
717 filename, lineno, name);
724 * We now have a home server, see if we can insert it
725 * into pre-existing pool.
727 if (insert_point >= 0) {
728 rad_assert(pool != NULL);
729 pool->servers[insert_point] = home;
733 rad_assert(pool == NULL);
734 rad_assert(home != NULL);
737 * Count the old-style realms of this name.
739 num_home_servers = 0;
740 for (cs = cf_section_sub_find_name2(mainconfig.config, "realm", name);
742 cs = cf_section_sub_find_name2(cs, "realm", name)) {
747 pool = rad_malloc(sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
748 memset(pool, 0, sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
752 pool->server_type = type;
753 pool->num_home_servers = num_home_servers;
754 pool->servers[0] = home;
756 if (!rbtree_insert(home_pools_byname, pool)) {
757 rad_assert("Internal sanity check failed");
766 static int old_realm_config(const char *filename, CONF_SECTION *cs, REALM *r)
770 home_pool_type_t ldflag;
772 secret = cf_section_value_find(cs, "secret");
774 host = cf_section_value_find(cs, "ldflag");
776 (strcasecmp(host, "fail_over") == 0)) {
777 ldflag = HOME_POOL_FAIL_OVER;
778 DEBUG2("\tldflag = fail_over", r->name);
780 } else if (strcasecmp(host, "round_robin") == 0) {
781 ldflag = HOME_POOL_LOAD_BALANCE;
782 DEBUG2("\tldflag = round_robin", r->name);
785 radlog(L_ERR, "%s[%d]: Unknown value \"%s\" for ldflag",
786 filename, cf_section_lineno(cs), host);
791 * Allow old-style if it doesn't exist, or if it exists and
794 if (((host = cf_section_value_find(cs, "authhost")) != NULL) &&
795 (strcmp(host, "LOCAL") != 0)) {
797 radlog(L_ERR, "%s[%d]: No shared secret supplied for realm: %s",
798 filename, cf_section_lineno(cs), r->name);
802 DEBUG2("\tauthhost = %s", r->name, host);
804 if (!old_server_add(filename, cf_section_lineno(cs),
805 host, secret, ldflag,
806 &r->auth_pool, HOME_TYPE_AUTH)) {
811 if (((host = cf_section_value_find(cs, "accthost")) != NULL) &&
812 (strcmp(host, "LOCAL") != 0)) {
814 radlog(L_ERR, "%s[%d]: No shared secret supplied for realm: %s",
815 filename, cf_section_lineno(cs), r->name);
819 DEBUG2("\taccthost = %s", r->name, host);
821 if (!old_server_add(filename, cf_section_lineno(cs),
822 host, secret, ldflag,
823 &r->auth_pool, HOME_TYPE_ACCT)) {
828 if (secret) DEBUG2("\tsecret = %s", r->name, secret);
835 static int add_pool_to_realm(const char *filename, int lineno,
836 const char *name, home_pool_t **dest,
839 home_pool_t mypool, *pool;
842 pool = rbtree_finddata(home_pools_byname, &mypool);
844 CONF_SECTION *pool_cs;
846 pool_cs = cf_section_sub_find_name2(NULL, "server_pool",
849 radlog(L_ERR, "%s[%d]: Failed to find server_pool \"%s\"",
850 filename, lineno, name);
854 if (!server_pool_add(filename, pool_cs)) {
858 pool = rbtree_finddata(home_pools_byname, &mypool);
860 rad_assert("Internal sanity check failed");
865 if (pool->server_type != server_type) {
866 radlog(L_ERR, "%s[%d]: Incompatible server_pool \"%s\" (mixed auth_pool / acct_pool)",
867 filename, lineno, name);
876 int realm_add(const char *filename, CONF_SECTION *cs)
883 name2 = cf_section_name1(cs);
884 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
885 radlog(L_ERR, "%s[%d]: Section is not a realm.",
886 filename, cf_section_lineno(cs));
890 name2 = cf_section_name2(cs);
892 radlog(L_ERR, "%s[%d]: Realm section is missing the realm name.",
893 filename, cf_section_lineno(cs));
897 DEBUG2(" realm %s {", name2);
900 * The realm MAY already exist if it's an old-style realm.
901 * In that case, merge the old-style realm with this one.
903 r = realm_find(name2);
905 if (cf_pair_find(cs, "auth_pool") ||
906 cf_pair_find(cs, "acct_pool")) {
907 radlog(L_ERR, "%s[%d]: Duplicate realm \"%s\"",
908 filename, cf_section_lineno(cs), name2);
912 if (!old_realm_config(filename, cs, r)) {
916 DEBUG2(" } # realm %s", name2);
920 r = rad_malloc(sizeof(*r));
921 memset(r, 0, sizeof(*r));
926 * Prefer new configuration to old one.
928 cp = cf_pair_find(cs, "auth_pool");
929 if (cp) pool = cf_pair_value(cp);
931 DEBUG2(" realm %s: Adding server_pool %s",
933 if (!add_pool_to_realm(filename, cf_pair_lineno(cp),
934 pool, &r->auth_pool, HOME_TYPE_AUTH)) {
937 DEBUG2("\tauth_pool = %s", name2, pool);
940 cp = cf_pair_find(cs, "acct_pool");
941 if (cp) pool = cf_pair_value(cp);
943 DEBUG2(" realm %s: Adding server_pool %s",
945 if (!add_pool_to_realm(filename, cf_pair_lineno(cp),
946 pool, &r->acct_pool, HOME_TYPE_ACCT)) {
949 DEBUG2("\tacct_pool = %s", name2, pool);
954 if ((cf_section_value_find(cs, "nostrip")) != NULL) {
956 DEBUG2("\tnostrip", name2);
960 * We're a new-style realm. Complain if we see the old
963 if (r->auth_pool || r->acct_pool) {
964 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
965 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
966 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
967 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
968 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
973 * The realm MAY be an old-style realm, as there
974 * was no auth_pool or acct_pool. Double-check
975 * it, just to be safe.
977 } else if (!old_realm_config(filename, cs, r)) {
981 if (!rbtree_insert(realms_byname, r)) {
982 rad_assert("Internal sanity check failed");
986 DEBUG2(" } # realm %s", name2);
991 DEBUG2(" } # realm %s", name2);
998 * Find a realm in the REALM list.
1000 REALM *realm_find(const char *name)
1004 if (!name) name = "NULL";
1006 myrealm.name = name;
1007 return rbtree_finddata(realms_byname, &myrealm);
1011 home_server *home_server_ldb(const char *realmname,
1012 home_pool_t *pool, REQUEST *request)
1020 * Determine how to pick choose the home server.
1022 switch (pool->type) {
1026 * Load balancing. Pick one at random.
1028 case HOME_POOL_LOAD_BALANCE:
1030 start = hash % pool->num_home_servers;
1034 * For load-balancing by client IP address, we
1035 * pick a home server by hashing the client IP.
1037 * This isn't as even a load distribution as
1038 * tracking the State attribute, but it's better
1041 case HOME_POOL_CLIENT_BALANCE:
1042 switch (request->packet->src_ipaddr.af) {
1044 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1045 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1048 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1049 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1055 start = hash % pool->num_home_servers;
1064 * Starting with the home server we chose, loop through
1065 * all home servers. If the current one is dead, skip
1066 * it. If it is too busy, skip it.
1068 * Otherwise, use it.
1070 for (count = 0; count < pool->num_home_servers; count++) {
1071 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
1073 if (home->state == HOME_STATE_IS_DEAD) {
1078 * This home server is too busy. Choose another one.
1080 if (home->currently_outstanding >= home->max_outstanding) {
1085 } /* loop over the home servers */
1088 * No live match found, and no fallback to the "DEFAULT"
1089 * realm. We fix this by blindly marking all servers as
1090 * "live". But only do it for ones that don't support
1091 * "pings", as they will be marked live when they
1092 * actually are live.
1094 if (!mainconfig.proxy_fallback &&
1095 mainconfig.wake_all_if_all_dead) {
1096 home_server *lb = NULL;
1098 for (count = 0; count < pool->num_home_servers; count++) {
1099 home_server *home = pool->servers[count];
1101 if ((home->state == HOME_STATE_IS_DEAD) &&
1102 (home->ping_check == HOME_PING_CHECK_NONE)) {
1103 home->state = HOME_STATE_ALIVE;
1112 * Still nothing. Look up the DEFAULT realm, but only
1113 * if we weren't looking up the NULL or DEFAULT realms.
1115 if (mainconfig.proxy_fallback &&
1117 (strcmp(realmname, "NULL") != 0) &&
1118 (strcmp(realmname, "DEFAULT") != 0)) {
1119 REALM *rd = realm_find("DEFAULT");
1121 if (!rd) return NULL;
1124 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1125 pool = rd->auth_pool;
1127 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1128 pool = rd->acct_pool;
1130 if (!pool) return NULL;
1132 DEBUG2(" Realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
1133 return home_server_ldb(rd->name, pool, request);
1137 * Still haven't found anything. Oh well.
1143 home_server *home_server_find(lrad_ipaddr_t *ipaddr, int port)
1147 myhome.ipaddr = *ipaddr;
1150 return rbtree_finddata(home_servers_byaddr, &myhome);