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(const char *filename)
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(mainconfig.config, NULL, "realm");
134 cs = cf_subsection_find_next(mainconfig.config, cs, "realm")) {
135 if (!realm_add(filename, 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 { "hostname", PW_TYPE_STRING_PTR,
156 offsetof(home_server,hostname), NULL, NULL},
157 { "port", PW_TYPE_INTEGER,
158 offsetof(home_server,port), NULL, "0" },
160 { "type", PW_TYPE_STRING_PTR,
163 { "secret", PW_TYPE_STRING_PTR,
164 offsetof(home_server,secret), NULL, NULL},
166 { "response_window", PW_TYPE_INTEGER,
167 offsetof(home_server,response_window), NULL, "30" },
168 { "max_outstanding", PW_TYPE_INTEGER,
169 offsetof(home_server,max_outstanding), NULL, "65536" },
171 { "zombie_period", PW_TYPE_INTEGER,
172 offsetof(home_server,zombie_period), NULL, "40" },
173 { "status_check", PW_TYPE_STRING_PTR,
174 0, &hs_check, "none" },
175 { "ping_check", PW_TYPE_STRING_PTR,
176 0, &hs_check, "none" },
178 { "ping_interval", PW_TYPE_INTEGER,
179 offsetof(home_server,ping_interval), NULL, "30" },
180 { "check_interval", PW_TYPE_INTEGER,
181 offsetof(home_server,ping_interval), NULL, "30" },
182 { "num_answers_to_alive", PW_TYPE_INTEGER,
183 offsetof(home_server,num_pings_to_alive), NULL, "3" },
184 { "num_pings_to_alive", PW_TYPE_INTEGER,
185 offsetof(home_server,num_pings_to_alive), NULL, "3" },
186 { "revive_interval", PW_TYPE_INTEGER,
187 offsetof(home_server,revive_interval), NULL, "300" },
188 { "status_check_timeout", PW_TYPE_INTEGER,
189 offsetof(home_server,ping_timeout), NULL, "4" },
191 { "username", PW_TYPE_STRING_PTR,
192 offsetof(home_server,ping_user_name), NULL, NULL},
193 { "password", PW_TYPE_STRING_PTR,
194 offsetof(home_server,ping_user_password), NULL, NULL},
196 { NULL, -1, 0, NULL, NULL } /* end the list */
201 static int home_server_add(const char *filename, CONF_SECTION *cs)
206 name2 = cf_section_name1(cs);
207 if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
208 radlog(L_ERR, "%s[%d]: Section is not a home_server.",
209 filename, cf_section_lineno(cs));
213 name2 = cf_section_name2(cs);
215 radlog(L_ERR, "%s[%d]: Home server section is missing a name.",
216 filename, cf_section_lineno(cs));
220 home = rad_malloc(sizeof(*home));
221 memset(home, 0, sizeof(*home));
225 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
226 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
227 cf_section_parse(cs, home, home_server_config);
229 if (!home->hostname && (htonl(hs_ip4addr.s_addr) == INADDR_NONE) &&
230 IN6_IS_ADDR_UNSPECIFIED(&hs_ip6addr)) {
231 radlog(L_ERR, "%s[%d]: No hostname, IPv4 address, or IPv6 address defined for home server %s.",
232 filename, cf_section_lineno(cs), name2);
242 * FIXME: Parse home->hostname!
244 * Right now, only ipaddr && ip6addr are used.
245 * The old-style parsing still allows hostnames.
247 if (htonl(hs_ip4addr.s_addr) != INADDR_NONE) {
248 home->ipaddr.af = AF_INET;
249 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
251 } else if (!IN6_IS_ADDR_UNSPECIFIED(&hs_ip6addr)) {
252 home->ipaddr.af = AF_INET6;
253 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
256 radlog(L_ERR, "%s[%d]: FIXME: parse hostname for home server %s.",
257 filename, cf_section_lineno(cs), name2);
266 if (!home->port || (home->port > 65535)) {
267 radlog(L_ERR, "%s[%d]: No port, or invalid port defined for home server %s.",
268 filename, cf_section_lineno(cs), name2);
278 radlog(L_ERR, "%s[%d]: Fatal error! Home server %s is ourselves!",
279 filename, cf_section_lineno(cs), name2);
288 if (strcasecmp(hs_type, "auth") == 0) {
289 home->type = HOME_TYPE_AUTH;
291 } else if (strcasecmp(hs_type, "acct") == 0) {
292 home->type = HOME_TYPE_ACCT;
295 radlog(L_ERR, "%s[%d]: Invalid type \"%s\" for home server %s.",
296 filename, cf_section_lineno(cs), hs_type, name2);
308 radlog(L_ERR, "%s[%d]: No shared secret defined for home server %s.",
309 filename, cf_section_lineno(cs), name2);
314 if (strcasecmp(hs_check, "none") == 0) {
315 home->ping_check = HOME_PING_CHECK_NONE;
317 } else if (strcasecmp(hs_check, "status-server") == 0) {
318 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
320 } else if (strcasecmp(hs_check, "request") == 0) {
321 home->ping_check = HOME_PING_CHECK_REQUEST;
324 radlog(L_ERR, "%s[%d]: Invalid ping_check \"%s\" for home server %s.",
325 filename, cf_section_lineno(cs), hs_check, name2);
334 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
335 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
336 if (!home->ping_user_name) {
337 radlog(L_INFO, "%s[%d]: You must supply a user name to enable ping checks",
338 filename, cf_section_lineno(cs));
343 if ((home->type == HOME_TYPE_AUTH) &&
344 !home->ping_user_password) {
345 radlog(L_INFO, "%s[%d]: You must supply a password to enable ping checks",
346 filename, cf_section_lineno(cs));
352 if (rbtree_finddata(home_servers_byaddr, home)) {
353 radlog(L_INFO, "%s[%d]: Ignoring duplicate home server %s.",
354 filename, cf_section_lineno(cs), name2);
358 if (!rbtree_insert(home_servers_byname, home)) {
359 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
360 filename, cf_section_lineno(cs), name2);
365 if (!rbtree_insert(home_servers_byaddr, home)) {
366 rbtree_deletebydata(home_servers_byname, home);
367 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
368 filename, cf_section_lineno(cs), name2);
373 if (home->response_window < 5) home->response_window = 5;
374 if (home->response_window > 60) home->response_window = 60;
376 if (home->max_outstanding < 8) home->max_outstanding = 8;
377 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
379 if (home->ping_interval < 6) home->ping_interval = 6;
380 if (home->ping_interval > 120) home->ping_interval = 120;
382 if (home->zombie_period < 20) home->zombie_period = 20;
383 if (home->zombie_period > 120) home->zombie_period = 120;
385 if (home->zombie_period < home->response_window) {
386 home->zombie_period = home->response_window;
389 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
390 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
392 if (home->ping_timeout < 3) home->ping_timeout = 3;
393 if (home->ping_timeout > 10) home->ping_timeout = 10;
395 if (home->revive_interval < 60) home->revive_interval = 60;
396 if (home->revive_interval > 3600) home->revive_interval = 3600;
402 static int server_pool_add(const char *filename, CONF_SECTION *cs, int type)
405 home_pool_t *pool = NULL;
408 int num_home_servers;
410 name2 = cf_section_name1(cs);
411 if (!name2 || (strcasecmp(name2, "server_pool") != 0)) {
412 radlog(L_ERR, "%s[%d]: Section is not a server_pool.",
413 filename, cf_section_lineno(cs));
417 name2 = cf_section_name2(cs);
419 radlog(L_ERR, "%s[%d]: Server pool section is missing a name.",
420 filename, cf_section_lineno(cs));
425 * Count the home servers and initalize them.
427 num_home_servers = 0;
428 for (cp = cf_pair_find(cs, "home_server");
430 cp = cf_pair_find_next(cs, cp, "home_server")) {
431 home_server myhome, *home;
432 CONF_SECTION *server_cs;
436 value = cf_pair_value(cp);
438 radlog(L_ERR, "%s[%d]: No value given for home_server.",
439 filename, cf_pair_lineno(cp));
445 home = rbtree_finddata(home_servers_byname, &myhome);
448 server_cs = cf_section_sub_find_name2(NULL,
452 radlog(L_ERR, "%s[%d]: Unknown home_server \"%s\".",
453 filename, cf_pair_lineno(cp), value);
457 if (!home_server_add(filename, server_cs)) {
461 home = rbtree_finddata(home_servers_byname, &myhome);
463 radlog(L_ERR, "Internal sanity check failed %d",
469 if (num_home_servers == 0) {
470 radlog(L_ERR, "%s[%d]: No home servers defined in pool %s",
471 filename, cf_section_lineno(cs), name2);
475 pool = rad_malloc(sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
476 memset(pool, 0, sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
478 pool->type = HOME_POOL_FAIL_OVER;
481 DEBUG2(" server_pool %s {", name2);
483 cp = cf_pair_find(cs, "type");
485 static LRAD_NAME_NUMBER pool_types[] = {
486 { "load-balance", HOME_POOL_LOAD_BALANCE },
487 { "fail-over", HOME_POOL_FAIL_OVER },
488 { "round_robin", HOME_POOL_LOAD_BALANCE },
489 { "fail_over", HOME_POOL_FAIL_OVER },
490 { "client-balance", HOME_POOL_CLIENT_BALANCE },
491 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
495 value = cf_pair_value(cp);
497 radlog(L_ERR, "%s[%d]: No value given for type.",
498 filename, cf_pair_lineno(cp));
502 pool->type = lrad_str2int(pool_types, value, 0);
504 radlog(L_ERR, "%s[%d]: Unknown type \"%s\".",
505 filename, cf_pair_lineno(cp), value);
509 DEBUG2("\ttype = %s", name2, value);
512 for (cp = cf_pair_find(cs, "home_server");
514 cp = cf_pair_find_next(cs, cp, "home_server")) {
515 home_server myhome, *home;
517 value = cf_pair_value(cp);
519 radlog(L_ERR, "%s[%d]: No value given for home_server.",
520 filename, cf_pair_lineno(cp));
527 home = rbtree_finddata(home_servers_byname, &myhome);
529 DEBUG2("Internal sanity check failed");
533 if (!pool->server_type) {
534 rad_assert(home->type != 0);
535 pool->server_type = home->type;
537 } else if (pool->server_type != home->type) {
538 radlog(L_ERR, "%s[%d]: Home server \"%s\" is not of the same type as previous servers in server pool %s",
539 filename, cf_pair_lineno(cp), value, pool->name);
544 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
548 DEBUG2("\thome_server = %s", home->name);
549 pool->servers[pool->num_home_servers] = home;
550 pool->num_home_servers++;
551 } /* loop over home_server's */
553 if (!rbtree_insert(home_pools_byname, pool)) {
554 rad_assert("Internal sanity check failed");
560 rad_assert(pool->server_type != 0);
571 static int old_server_add(const char *filename, int lineno, const char *realm,
572 const char *name, const char *secret,
573 home_pool_type_t ldflag, home_pool_t **pool_p,
576 int i, insert_point, num_home_servers;
577 home_server myhome, *home;
578 home_pool_t mypool, *pool;
582 * LOCAL realms get sanity checked, and nothing else happens.
584 if (strcmp(name, "LOCAL") == 0) {
586 radlog(L_ERR, "%s[%d]: Realm \"%s\" cannot be both LOCAL and remote", filename, lineno, name);
593 mypool.server_type = type;
594 pool = rbtree_finddata(home_pools_byname, &mypool);
596 if (pool->type != ldflag) {
597 radlog(L_ERR, "%s[%d]: Inconsistent ldflag for server pool \"%s\"", filename, lineno, name);
601 if (pool->server_type != type) {
602 radlog(L_ERR, "%s[%d]: Inconsistent home server type for server pool \"%s\"", filename, lineno, name);
609 home = rbtree_finddata(home_servers_byname, &myhome);
611 if (strcmp(home->secret, secret) != 0) {
612 radlog(L_ERR, "%s[%d]: Inconsistent shared secret for home server \"%s\"", filename, lineno, name);
616 if (home->type != type) {
617 radlog(L_ERR, "%s[%d]: Inconsistent type for home server \"%s\"", filename, lineno, name);
622 * See if the home server is already listed
623 * in the pool. If so, do nothing else.
625 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
626 if (pool->servers[i] == home) {
633 * If we do have a pool, check that there is room to
634 * insert the home server we've found, or the one that we
637 * Note that we insert it into the LAST available
638 * position, in order to maintain the same order as in
639 * the configuration files.
643 for (i = pool->num_home_servers - 1; i >= 0; i--) {
644 if (pool->servers[i]) break;
646 if (!pool->servers[i]) {
651 if (insert_point < 0) {
652 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);
658 * No home server, allocate one.
664 home = rad_malloc(sizeof(*home));
665 memset(home, 0, sizeof(*home));
668 home->hostname = name;
670 home->secret = secret;
672 p = strchr(name, ':');
674 if (type == HOME_TYPE_AUTH) {
675 home->port = PW_AUTH_UDP_PORT;
677 home->port = PW_ACCT_UDP_PORT;
683 } else if (p == name) {
684 radlog(L_ERR, "%s[%d]: Invalid hostname %s.",
685 filename, lineno, name);
690 home->port = atoi(p + 1);
691 if ((home->port == 0) || (home->port > 65535)) {
692 radlog(L_ERR, "%s[%d]: Invalid port %s.",
693 filename, lineno, p + 1);
698 q = rad_malloc((p - name) + 1);
699 memcpy(q, name, (p - name));
704 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
705 radlog(L_ERR, "%s[%d]: Failed looking up hostname %s.",
706 filename, lineno, p);
714 * Use the old-style configuration.
716 home->max_outstanding = 65535*16;
717 home->zombie_period = mainconfig.proxy_retry_delay * mainconfig.proxy_retry_count;
718 if (home->zombie_period == 0) home->zombie_period =30;
719 home->response_window = home->zombie_period - 1;
721 home->ping_check = HOME_PING_CHECK_NONE;
723 home->revive_interval = mainconfig.proxy_dead_time;
725 if (rbtree_finddata(home_servers_byaddr, home)) {
726 radlog(L_ERR, "%s[%d]: Home server %s has the same IP address as another home server.",
727 filename, lineno, name);
732 if (!rbtree_insert(home_servers_byname, home)) {
733 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
734 filename, lineno, name);
739 if (!rbtree_insert(home_servers_byaddr, home)) {
740 rbtree_deletebydata(home_servers_byname, home);
741 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
742 filename, lineno, name);
749 * We now have a home server, see if we can insert it
750 * into pre-existing pool.
752 if (insert_point >= 0) {
753 rad_assert(pool != NULL);
754 pool->servers[insert_point] = home;
758 rad_assert(pool == NULL);
759 rad_assert(home != NULL);
762 * Count the old-style realms of this name.
764 num_home_servers = 0;
765 for (cs = cf_section_sub_find_name2(mainconfig.config, "realm", realm);
767 cs = cf_section_sub_find_name2(cs, "realm", realm)) {
771 pool = rad_malloc(sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
772 memset(pool, 0, sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
776 pool->server_type = type;
777 pool->num_home_servers = num_home_servers;
778 pool->servers[0] = home;
780 if (!rbtree_insert(home_pools_byname, pool)) {
781 rad_assert("Internal sanity check failed");
790 static int old_realm_config(const char *filename, CONF_SECTION *cs, REALM *r)
794 home_pool_type_t ldflag;
796 secret = cf_section_value_find(cs, "secret");
798 host = cf_section_value_find(cs, "ldflag");
800 (strcasecmp(host, "fail_over") == 0)) {
801 ldflag = HOME_POOL_FAIL_OVER;
802 DEBUG2("\tldflag = fail_over");
804 } else if (strcasecmp(host, "round_robin") == 0) {
805 ldflag = HOME_POOL_LOAD_BALANCE;
806 DEBUG2("\tldflag = round_robin");
809 radlog(L_ERR, "%s[%d]: Unknown value \"%s\" for ldflag",
810 filename, cf_section_lineno(cs), host);
815 * Allow old-style if it doesn't exist, or if it exists and
818 if (((host = cf_section_value_find(cs, "authhost")) != NULL) &&
819 (strcmp(host, "LOCAL") != 0)) {
821 radlog(L_ERR, "%s[%d]: No shared secret supplied for realm: %s",
822 filename, cf_section_lineno(cs), r->name);
826 DEBUG2("\tauthhost = %s", host);
828 if (!old_server_add(filename, cf_section_lineno(cs),
829 r->name, host, secret, ldflag,
830 &r->auth_pool, HOME_TYPE_AUTH)) {
835 if (((host = cf_section_value_find(cs, "accthost")) != NULL) &&
836 (strcmp(host, "LOCAL") != 0)) {
838 radlog(L_ERR, "%s[%d]: No shared secret supplied for realm: %s",
839 filename, cf_section_lineno(cs), r->name);
843 DEBUG2("\taccthost = %s", host);
845 if (!old_server_add(filename, cf_section_lineno(cs),
846 r->name, host, secret, ldflag,
847 &r->acct_pool, HOME_TYPE_ACCT)) {
852 if (secret) DEBUG2("\tsecret = %s", secret);
859 static int add_pool_to_realm(const char *filename, int lineno,
860 const char *name, home_pool_t **dest,
863 home_pool_t mypool, *pool;
866 mypool.server_type = server_type;
867 pool = rbtree_finddata(home_pools_byname, &mypool);
869 CONF_SECTION *pool_cs;
871 pool_cs = cf_section_sub_find_name2(NULL, "server_pool",
874 radlog(L_ERR, "%s[%d]: Failed to find server_pool \"%s\"",
875 filename, lineno, name);
879 if (!server_pool_add(filename, pool_cs, server_type)) {
883 pool = rbtree_finddata(home_pools_byname, &mypool);
885 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
890 if (pool->server_type != server_type) {
891 radlog(L_ERR, "%s[%d]: Incompatible server_pool \"%s\" (mixed auth_pool / acct_pool)",
892 filename, lineno, name);
901 int realm_add(const char *filename, CONF_SECTION *cs)
906 home_pool_t *auth_pool, *acct_pool;
907 const char *auth_pool_name, *acct_pool_name;
909 name2 = cf_section_name1(cs);
910 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
911 radlog(L_ERR, "%s[%d]: Section is not a realm.",
912 filename, cf_section_lineno(cs));
916 name2 = cf_section_name2(cs);
918 radlog(L_ERR, "%s[%d]: Realm section is missing the realm name.",
919 filename, cf_section_lineno(cs));
923 auth_pool = acct_pool = NULL;
924 auth_pool_name = acct_pool_name = NULL;
927 * Prefer new configuration to old one.
929 cp = cf_pair_find(cs, "auth_pool");
930 if (cp) auth_pool_name = cf_pair_value(cp);
931 if (cp && auth_pool_name) {
932 if (!add_pool_to_realm(filename, cf_pair_lineno(cp),
933 auth_pool_name, &auth_pool,
939 cp = cf_pair_find(cs, "acct_pool");
940 if (cp) acct_pool_name = cf_pair_value(cp);
941 if (cp && acct_pool_name) {
942 if (!add_pool_to_realm(filename, cf_pair_lineno(cp),
943 acct_pool_name, &acct_pool,
949 DEBUG2(" realm %s {", name2);
952 * The realm MAY already exist if it's an old-style realm.
953 * In that case, merge the old-style realm with this one.
955 r = realm_find(name2);
957 if (cf_pair_find(cs, "auth_pool") ||
958 cf_pair_find(cs, "acct_pool")) {
959 radlog(L_ERR, "%s[%d]: Duplicate realm \"%s\"",
960 filename, cf_section_lineno(cs), name2);
964 if (!old_realm_config(filename, cs, r)) {
968 DEBUG2(" } # realm %s", name2);
972 r = rad_malloc(sizeof(*r));
973 memset(r, 0, sizeof(*r));
976 r->auth_pool = auth_pool;
977 r->acct_pool = acct_pool;
980 if (auth_pool_name) DEBUG2("\tauth_pool = %s", auth_pool_name);
981 if (acct_pool_name) DEBUG2("\tacct_pool = %s", acct_pool_name);
983 if ((cf_section_value_find(cs, "nostrip")) != NULL) {
985 DEBUG2("\tnostrip", name2);
989 * We're a new-style realm. Complain if we see the old
992 if (r->auth_pool || r->acct_pool) {
993 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
994 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
995 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
996 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
997 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1002 * The realm MAY be an old-style realm, as there
1003 * was no auth_pool or acct_pool. Double-check
1004 * it, just to be safe.
1006 } else if (!old_realm_config(filename, cs, r)) {
1010 if (!rbtree_insert(realms_byname, r)) {
1011 rad_assert("Internal sanity check failed");
1020 DEBUG2(" } # realm %s", name2);
1027 * Find a realm in the REALM list.
1029 REALM *realm_find(const char *name)
1033 if (!name) name = "NULL";
1035 myrealm.name = name;
1036 return rbtree_finddata(realms_byname, &myrealm);
1040 home_server *home_server_ldb(const char *realmname,
1041 home_pool_t *pool, REQUEST *request)
1045 home_server *found = NULL;
1050 * Determine how to pick choose the home server.
1052 switch (pool->type) {
1056 * For load-balancing by client IP address, we
1057 * pick a home server by hashing the client IP.
1059 * This isn't as even a load distribution as
1060 * tracking the State attribute, but it's better
1063 case HOME_POOL_CLIENT_BALANCE:
1064 switch (request->packet->src_ipaddr.af) {
1066 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1067 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1070 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1071 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1077 start = hash % pool->num_home_servers;
1080 case HOME_POOL_CLIENT_PORT_BALANCE:
1081 switch (request->packet->src_ipaddr.af) {
1083 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1084 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1087 hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1088 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1094 lrad_hash_update(&request->packet->src_port,
1095 sizeof(request->packet->src_port), hash);
1096 start = hash % pool->num_home_servers;
1099 case HOME_POOL_LOAD_BALANCE:
1100 found = pool->servers[0];
1108 * Starting with the home server we chose, loop through
1109 * all home servers. If the current one is dead, skip
1110 * it. If it is too busy, skip it.
1112 * Otherwise, use it.
1114 for (count = 0; count < pool->num_home_servers; count++) {
1115 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
1117 if (home->state == HOME_STATE_IS_DEAD) {
1122 * This home server is too busy. Choose another one.
1124 if (home->currently_outstanding >= home->max_outstanding) {
1128 if (pool->type != HOME_POOL_LOAD_BALANCE) {
1132 DEBUG3("PROXY %s %d\t%s %d",
1133 found->name, found->currently_outstanding,
1134 home->name, home->currently_outstanding);
1137 * Prefer this server if it's less busy than the
1138 * one we previously found.
1140 if (home->currently_outstanding < found->currently_outstanding) {
1141 DEBUG3("Choosing %s: It's less busy than %s",
1142 home->name, found->name);
1148 * Ignore servers which are busier than the one
1151 if (home->currently_outstanding > found->currently_outstanding) {
1152 DEBUG3("Skipping %s: It's busier than %s",
1153 home->name, found->name);
1158 * From the list of servers which have the same
1159 * load, choose one at random.
1161 if (((count + 1) * (lrad_rand() & 0xffff)) < (uint32_t) 0x10000) {
1165 } /* loop over the home servers */
1167 if (found) return found;
1170 * No live match found, and no fallback to the "DEFAULT"
1171 * realm. We fix this by blindly marking all servers as
1172 * "live". But only do it for ones that don't support
1173 * "pings", as they will be marked live when they
1174 * actually are live.
1176 if (!mainconfig.proxy_fallback &&
1177 mainconfig.wake_all_if_all_dead) {
1178 home_server *lb = NULL;
1180 for (count = 0; count < pool->num_home_servers; count++) {
1181 home_server *home = pool->servers[count];
1183 if ((home->state == HOME_STATE_IS_DEAD) &&
1184 (home->ping_check == HOME_PING_CHECK_NONE)) {
1185 home->state = HOME_STATE_ALIVE;
1194 * Still nothing. Look up the DEFAULT realm, but only
1195 * if we weren't looking up the NULL or DEFAULT realms.
1197 if (mainconfig.proxy_fallback &&
1199 (strcmp(realmname, "NULL") != 0) &&
1200 (strcmp(realmname, "DEFAULT") != 0)) {
1201 REALM *rd = realm_find("DEFAULT");
1203 if (!rd) return NULL;
1206 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1207 pool = rd->auth_pool;
1209 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1210 pool = rd->acct_pool;
1212 if (!pool) return NULL;
1214 DEBUG2(" Realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
1215 return home_server_ldb(rd->name, pool, request);
1219 * Still haven't found anything. Oh well.
1225 home_server *home_server_find(lrad_ipaddr_t *ipaddr, int port)
1229 myhome.ipaddr = *ipaddr;
1232 return rbtree_finddata(home_servers_byaddr, &myhome);