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 typedef struct realm_config_t {
48 int wake_all_if_all_dead;
51 static realm_config_t *realm_config = NULL;
54 * Map the proxy server configuration parameters to variables.
56 static const CONF_PARSER proxy_config[] = {
57 { "retry_delay", PW_TYPE_INTEGER,
58 offsetof(realm_config_t, retry_delay),
59 NULL, Stringify(RETRY_DELAY) },
61 { "retry_count", PW_TYPE_INTEGER,
62 offsetof(realm_config_t, retry_count),
63 NULL, Stringify(RETRY_COUNT) },
65 { "default_fallback", PW_TYPE_BOOLEAN,
66 offsetof(realm_config_t, fallback),
69 { "dead_time", PW_TYPE_INTEGER,
70 offsetof(realm_config_t, dead_time),
71 NULL, Stringify(DEAD_TIME) },
73 { "wake_all_if_all_dead", PW_TYPE_BOOLEAN,
74 offsetof(realm_config_t, wake_all_if_all_dead),
77 { NULL, -1, 0, NULL, NULL }
80 static int realm_name_cmp(const void *one, const void *two)
85 return strcasecmp(a->name, b->name);
89 static int home_server_name_cmp(const void *one, const void *two)
91 const home_server *a = one;
92 const home_server *b = two;
94 if (a->type < b->type) return -1;
95 if (a->type > b->type) return +1;
97 return strcasecmp(a->name, b->name);
100 static int home_server_addr_cmp(const void *one, const void *two)
102 const home_server *a = one;
103 const home_server *b = two;
105 if (a->port < b->port) return -1;
106 if (a->port > b->port) return +1;
108 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
112 static int home_pool_name_cmp(const void *one, const void *two)
114 const home_pool_t *a = one;
115 const home_pool_t *b = two;
117 if (a->server_type < b->server_type) return -1;
118 if (a->server_type > b->server_type) return +1;
120 return strcasecmp(a->name, b->name);
125 * Xlat for %{home_server:foo}
127 static size_t xlat_home_server(UNUSED void *instance, REQUEST *request,
128 char *fmt, char *out, size_t outlen,
129 UNUSED RADIUS_ESCAPE_STRING func)
131 const char *value = NULL;
134 if (!fmt || !out || (outlen < 1)) return 0;
136 if (!request || !request->home_server) {
141 cp = cf_pair_find(request->home_server->cs, fmt);
142 if (!cp || !(value = cf_pair_value(cp))) {
147 strlcpy(out, value, outlen);
154 * Xlat for %{home_server_pool:foo}
156 static size_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
157 char *fmt, char *out, size_t outlen,
158 UNUSED RADIUS_ESCAPE_STRING func)
160 const char *value = NULL;
163 if (!fmt || !out || (outlen < 1)) return 0;
165 if (!request || !request->home_pool) {
170 cp = cf_pair_find(request->home_pool->cs, fmt);
171 if (!cp || !(value = cf_pair_value(cp))) {
176 strlcpy(out, value, outlen);
182 void realms_free(void)
184 rbtree_free(home_servers_byname);
185 home_servers_byname = NULL;
187 rbtree_free(home_servers_byaddr);
188 home_servers_byaddr = NULL;
190 rbtree_free(home_pools_byname);
191 home_pools_byname = NULL;
193 rbtree_free(realms_byname);
194 realms_byname = NULL;
198 static struct in_addr hs_ip4addr;
199 static struct in6_addr hs_ip6addr;
200 static char *hs_type = NULL;
201 static char *hs_check = NULL;
203 static CONF_PARSER home_server_config[] = {
204 { "ipaddr", PW_TYPE_IPADDR,
205 0, &hs_ip4addr, NULL },
206 { "ipv6addr", PW_TYPE_IPV6ADDR,
207 0, &hs_ip6addr, NULL },
209 { "port", PW_TYPE_INTEGER,
210 offsetof(home_server,port), NULL, "0" },
212 { "type", PW_TYPE_STRING_PTR,
215 { "secret", PW_TYPE_STRING_PTR,
216 offsetof(home_server,secret), NULL, NULL},
218 { "response_window", PW_TYPE_INTEGER,
219 offsetof(home_server,response_window), NULL, "30" },
220 { "max_outstanding", PW_TYPE_INTEGER,
221 offsetof(home_server,max_outstanding), NULL, "65536" },
223 { "zombie_period", PW_TYPE_INTEGER,
224 offsetof(home_server,zombie_period), NULL, "40" },
225 { "status_check", PW_TYPE_STRING_PTR,
226 0, &hs_check, "none" },
227 { "ping_check", PW_TYPE_STRING_PTR,
228 0, &hs_check, "none" },
230 { "ping_interval", PW_TYPE_INTEGER,
231 offsetof(home_server,ping_interval), NULL, "30" },
232 { "check_interval", PW_TYPE_INTEGER,
233 offsetof(home_server,ping_interval), NULL, "30" },
234 { "num_answers_to_alive", PW_TYPE_INTEGER,
235 offsetof(home_server,num_pings_to_alive), NULL, "3" },
236 { "num_pings_to_alive", PW_TYPE_INTEGER,
237 offsetof(home_server,num_pings_to_alive), NULL, "3" },
238 { "revive_interval", PW_TYPE_INTEGER,
239 offsetof(home_server,revive_interval), NULL, "300" },
240 { "status_check_timeout", PW_TYPE_INTEGER,
241 offsetof(home_server,ping_timeout), NULL, "4" },
243 { "username", PW_TYPE_STRING_PTR,
244 offsetof(home_server,ping_user_name), NULL, NULL},
245 { "password", PW_TYPE_STRING_PTR,
246 offsetof(home_server,ping_user_password), NULL, NULL},
248 { NULL, -1, 0, NULL, NULL } /* end the list */
253 static int home_server_add(CONF_SECTION *cs, int type)
259 name2 = cf_section_name1(cs);
260 if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
261 cf_log_err(cf_sectiontoitem(cs),
262 "Section is not a home_server.");
266 name2 = cf_section_name2(cs);
268 cf_log_err(cf_sectiontoitem(cs),
269 "Home server section is missing a name.");
273 home = rad_malloc(sizeof(*home));
274 memset(home, 0, sizeof(*home));
279 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
280 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
281 cf_section_parse(cs, home, home_server_config);
283 if (!cf_pair_find(cs, "ipaddr") &&
284 !cf_pair_find(cs, "ipv6addr")) {
285 cf_log_err(cf_sectiontoitem(cs),
286 "No IPv4 or IPv6 address defined for home server %s.",
297 * Figure out which one to use.
299 if (cf_pair_find(cs, "ipaddr")) {
300 home->ipaddr.af = AF_INET;
301 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
303 } else if (cf_pair_find(cs, "ipv6addr")) {
304 home->ipaddr.af = AF_INET6;
305 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
308 cf_log_err(cf_sectiontoitem(cs),
309 "Internal sanity check failed for home server %s.",
319 if (!home->port || (home->port > 65535)) {
320 cf_log_err(cf_sectiontoitem(cs),
321 "No port, or invalid port defined for home server %s.",
332 cf_log_err(cf_sectiontoitem(cs),
333 "Fatal error! Home server %s is ourselves!",
344 * Use a reasonable default.
346 if (!hs_type) hs_type = strdup("auth+acct");
348 if (strcasecmp(hs_type, "auth") == 0) {
349 home->type = HOME_TYPE_AUTH;
350 if (type != home->type) {
351 cf_log_err(cf_sectiontoitem(cs),
352 "Server pool of \"acct\" servers cannot include home server %s of type \"auth\"",
358 } else if (strcasecmp(hs_type, "acct") == 0) {
359 home->type = HOME_TYPE_ACCT;
360 if (type != home->type) {
361 cf_log_err(cf_sectiontoitem(cs),
362 "Server pool of \"auth\" servers cannot include home server %s of type \"acct\"",
368 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
369 home->type = HOME_TYPE_AUTH;
373 cf_log_err(cf_sectiontoitem(cs),
374 "Invalid type \"%s\" for home server %s.",
387 cf_log_err(cf_sectiontoitem(cs),
388 "No shared secret defined for home server %s.",
394 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
395 home->ping_check = HOME_PING_CHECK_NONE;
397 } else if (strcasecmp(hs_check, "status-server") == 0) {
398 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
400 } else if (strcasecmp(hs_check, "request") == 0) {
401 home->ping_check = HOME_PING_CHECK_REQUEST;
404 cf_log_err(cf_sectiontoitem(cs),
405 "Invalid ping_check \"%s\" for home server %s.",
415 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
416 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
417 if (!home->ping_user_name) {
418 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable ping checks");
423 if ((home->type == HOME_TYPE_AUTH) &&
424 !home->ping_user_password) {
425 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable ping checks");
431 if (rbtree_finddata(home_servers_byaddr, home)) {
432 DEBUG2("Ignoring duplicate home server %s.", name2);
436 if (!rbtree_insert(home_servers_byname, home)) {
437 cf_log_err(cf_sectiontoitem(cs),
438 "Internal error adding home server %s.",
444 if (!rbtree_insert(home_servers_byaddr, home)) {
445 rbtree_deletebydata(home_servers_byname, home);
446 cf_log_err(cf_sectiontoitem(cs),
447 "Internal error adding home server %s.",
453 if (home->response_window < 5) home->response_window = 5;
454 if (home->response_window > 60) home->response_window = 60;
456 if (home->max_outstanding < 8) home->max_outstanding = 8;
457 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
459 if (home->ping_interval < 6) home->ping_interval = 6;
460 if (home->ping_interval > 120) home->ping_interval = 120;
462 if (home->zombie_period < 20) home->zombie_period = 20;
463 if (home->zombie_period > 120) home->zombie_period = 120;
465 if (home->zombie_period < home->response_window) {
466 home->zombie_period = home->response_window;
469 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
470 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
472 if (home->ping_timeout < 3) home->ping_timeout = 3;
473 if (home->ping_timeout > 10) home->ping_timeout = 10;
475 if (home->revive_interval < 60) home->revive_interval = 60;
476 if (home->revive_interval > 3600) home->revive_interval = 3600;
479 home_server *home2 = rad_malloc(sizeof(*home2));
481 memcpy(home2, home, sizeof(*home2));
483 home2->type = HOME_TYPE_ACCT;
485 home2->ping_user_password = NULL;
488 if (!rbtree_insert(home_servers_byname, home2)) {
489 cf_log_err(cf_sectiontoitem(cs),
490 "Internal error adding home server %s.",
496 if (!rbtree_insert(home_servers_byaddr, home2)) {
497 rbtree_deletebydata(home_servers_byname, home2);
498 cf_log_err(cf_sectiontoitem(cs),
499 "Internal error adding home server %s.",
510 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
511 int server_type, int num_home_servers)
515 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
517 if (!pool) return NULL; /* just for pairanoia */
519 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
524 pool->server_type = server_type;
525 pool->num_home_servers = num_home_servers;
531 static int server_pool_add(realm_config_t *rc,
532 CONF_SECTION *cs, int server_type, int do_print)
535 home_pool_t *pool = NULL;
538 int num_home_servers;
540 name2 = cf_section_name1(cs);
541 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
542 (strcasecmp(name2, "home_server_pool") != 0))) {
543 cf_log_err(cf_sectiontoitem(cs),
544 "Section is not a home_server_pool.");
548 name2 = cf_section_name2(cs);
550 cf_log_err(cf_sectiontoitem(cs),
551 "Server pool section is missing a name.");
556 * Count the home servers and initalize them.
558 num_home_servers = 0;
559 for (cp = cf_pair_find(cs, "home_server");
561 cp = cf_pair_find_next(cs, cp, "home_server")) {
562 home_server myhome, *home;
563 CONF_SECTION *server_cs;
567 value = cf_pair_value(cp);
569 cf_log_err(cf_pairtoitem(cp),
570 "No value given for home_server.");
575 myhome.type = server_type;
576 home = rbtree_finddata(home_servers_byname, &myhome);
579 server_cs = cf_section_sub_find_name2(rc->cs,
583 cf_log_err(cf_pairtoitem(cp),
584 "Unknown home_server \"%s\".",
589 if (!home_server_add(server_cs, server_type)) {
593 home = rbtree_finddata(home_servers_byname, &myhome);
595 radlog(L_ERR, "Internal sanity check failed %d",
601 if (num_home_servers == 0) {
602 cf_log_err(cf_sectiontoitem(cs),
603 "No home servers defined in pool %s",
608 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
612 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
614 cp = cf_pair_find(cs, "type");
616 static FR_NAME_NUMBER pool_types[] = {
617 { "load-balance", HOME_POOL_LOAD_BALANCE },
618 { "fail-over", HOME_POOL_FAIL_OVER },
619 { "round_robin", HOME_POOL_LOAD_BALANCE },
620 { "fail_over", HOME_POOL_FAIL_OVER },
621 { "client-balance", HOME_POOL_CLIENT_BALANCE },
622 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
623 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
627 value = cf_pair_value(cp);
629 cf_log_err(cf_pairtoitem(cp),
630 "No value given for type.");
634 pool->type = fr_str2int(pool_types, value, 0);
636 cf_log_err(cf_pairtoitem(cp),
637 "Unknown type \"%s\".",
642 if (do_print) cf_log_info(cs, "\ttype = %s", value);
645 cp = cf_pair_find(cs, "virtual_server");
647 pool->virtual_server = cf_pair_value(cp);
648 if (do_print && pool->virtual_server) {
649 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
652 if (!cf_section_sub_find_name2(rc->cs, "server",
653 pool->virtual_server)) {
654 cf_log_err(cf_pairtoitem(cp), "No such server %s",
655 pool->virtual_server);
661 num_home_servers = 0;
662 for (cp = cf_pair_find(cs, "home_server");
664 cp = cf_pair_find_next(cs, cp, "home_server")) {
665 home_server myhome, *home;
667 value = cf_pair_value(cp);
669 cf_log_err(cf_pairtoitem(cp),
670 "No value given for home_server.");
675 myhome.type = server_type;
677 home = rbtree_finddata(home_servers_byname, &myhome);
679 DEBUG2("Internal sanity check failed");
684 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
688 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
689 pool->servers[num_home_servers++] = home;
690 } /* loop over home_server's */
692 if (!rbtree_insert(home_pools_byname, pool)) {
693 rad_assert("Internal sanity check failed");
697 if (do_print) cf_log_info(cs, " }");
699 rad_assert(pool->server_type != 0);
704 if (do_print) cf_log_info(cs, " }");
710 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
712 const char *name, const char *secret,
713 home_pool_type_t ldflag, home_pool_t **pool_p,
716 int i, insert_point, num_home_servers;
717 home_server myhome, *home;
718 home_pool_t mypool, *pool;
722 * LOCAL realms get sanity checked, and nothing else happens.
724 if (strcmp(name, "LOCAL") == 0) {
726 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
733 mypool.server_type = type;
734 pool = rbtree_finddata(home_pools_byname, &mypool);
736 if (pool->type != ldflag) {
737 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
741 if (pool->server_type != type) {
742 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
749 home = rbtree_finddata(home_servers_byname, &myhome);
751 if (strcmp(home->secret, secret) != 0) {
752 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
756 if (home->type != type) {
757 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
762 * See if the home server is already listed
763 * in the pool. If so, do nothing else.
765 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
766 if (pool->servers[i] == home) {
773 * If we do have a pool, check that there is room to
774 * insert the home server we've found, or the one that we
777 * Note that we insert it into the LAST available
778 * position, in order to maintain the same order as in
779 * the configuration files.
783 for (i = pool->num_home_servers - 1; i >= 0; i--) {
784 if (pool->servers[i]) break;
786 if (!pool->servers[i]) {
791 if (insert_point < 0) {
792 cf_log_err(cf_sectiontoitem(cs), "No room in pool to add home server \"%s\". Please update the realm configuration to use the new-style home servers and server pools.", name);
798 * No home server, allocate one.
804 home = rad_malloc(sizeof(*home));
805 memset(home, 0, sizeof(*home));
808 home->hostname = name;
810 home->secret = secret;
813 p = strchr(name, ':');
815 if (type == HOME_TYPE_AUTH) {
816 home->port = PW_AUTH_UDP_PORT;
818 home->port = PW_ACCT_UDP_PORT;
824 } else if (p == name) {
825 cf_log_err(cf_sectiontoitem(cs),
826 "Invalid hostname %s.",
832 home->port = atoi(p + 1);
833 if ((home->port == 0) || (home->port > 65535)) {
834 cf_log_err(cf_sectiontoitem(cs),
841 q = rad_malloc((p - name) + 1);
842 memcpy(q, name, (p - name));
847 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
848 cf_log_err(cf_sectiontoitem(cs),
849 "Failed looking up hostname %s.",
858 * Use the old-style configuration.
860 home->max_outstanding = 65535*16;
861 home->zombie_period = rc->retry_delay * rc->retry_count;
862 if (home->zombie_period == 0) home->zombie_period =30;
863 home->response_window = home->zombie_period - 1;
865 home->ping_check = HOME_PING_CHECK_NONE;
867 home->revive_interval = rc->dead_time;
869 if (rbtree_finddata(home_servers_byaddr, home)) {
870 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
875 if (!rbtree_insert(home_servers_byname, home)) {
876 cf_log_err(cf_sectiontoitem(cs), "Internal error adding home server %s.", name);
881 if (!rbtree_insert(home_servers_byaddr, home)) {
882 rbtree_deletebydata(home_servers_byname, home);
883 cf_log_err(cf_sectiontoitem(cs), "Internal error adding home server %s.", name);
890 * We now have a home server, see if we can insert it
891 * into pre-existing pool.
893 if (insert_point >= 0) {
894 rad_assert(pool != NULL);
895 pool->servers[insert_point] = home;
899 rad_assert(pool == NULL);
900 rad_assert(home != NULL);
903 * Count the old-style realms of this name.
905 num_home_servers = 0;
906 for (subcs = cf_section_find_next(cs, NULL, "realm");
908 subcs = cf_section_find_next(cs, subcs, "realm")) {
909 const char *this = cf_section_name2(subcs);
911 if (!this || (strcmp(this, realm) != 0)) continue;
915 if (num_home_servers == 0) {
916 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
921 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
924 pool->servers[0] = home;
926 if (!rbtree_insert(home_pools_byname, pool)) {
927 rad_assert("Internal sanity check failed");
936 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
939 const char *secret = NULL;
940 home_pool_type_t ldflag;
943 cp = cf_pair_find(cs, "ldflag");
944 ldflag = HOME_POOL_FAIL_OVER;
946 host = cf_pair_value(cp);
948 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
952 if (strcasecmp(host, "fail_over") == 0) {
953 cf_log_info(cs, "\tldflag = fail_over");
955 } else if (strcasecmp(host, "round_robin") == 0) {
956 ldflag = HOME_POOL_LOAD_BALANCE;
957 cf_log_info(cs, "\tldflag = round_robin");
960 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
963 } /* else don't print it. */
966 * Allow old-style if it doesn't exist, or if it exists and
969 cp = cf_pair_find(cs, "authhost");
971 host = cf_pair_value(cp);
973 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
977 if (strcmp(host, "LOCAL") != 0) {
978 cp = cf_pair_find(cs, "secret");
980 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
984 secret = cf_pair_value(cp);
986 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
991 cf_log_info(cs, "\tauthhost = %s", host);
993 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
994 &r->auth_pool, HOME_TYPE_AUTH)) {
999 cp = cf_pair_find(cs, "accthost");
1001 host = cf_pair_value(cp);
1003 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1008 * Don't look for a secret again if it was found
1011 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1012 cp = cf_pair_find(cs, "secret");
1014 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1018 secret = cf_pair_value(cp);
1020 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1025 cf_log_info(cs, "\taccthost = %s", host);
1027 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1028 &r->acct_pool, HOME_TYPE_ACCT)) {
1033 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1040 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1041 const char *name, home_pool_t **dest,
1042 int server_type, int do_print)
1044 home_pool_t mypool, *pool;
1047 mypool.server_type = server_type;
1049 pool = rbtree_finddata(home_pools_byname, &mypool);
1051 CONF_SECTION *pool_cs;
1053 pool_cs = cf_section_sub_find_name2(rc->cs,
1057 pool_cs = cf_section_sub_find_name2(rc->cs,
1062 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1066 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1070 pool = rbtree_finddata(home_pools_byname, &mypool);
1072 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1077 if (pool->server_type != server_type) {
1078 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1087 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1092 home_pool_t *auth_pool, *acct_pool;
1093 const char *auth_pool_name, *acct_pool_name;
1095 name2 = cf_section_name1(cs);
1096 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1097 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1101 name2 = cf_section_name2(cs);
1103 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1107 auth_pool = acct_pool = NULL;
1108 auth_pool_name = acct_pool_name = NULL;
1111 * Prefer new configuration to old one.
1113 cp = cf_pair_find(cs, "pool");
1114 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1115 if (cp) auth_pool_name = cf_pair_value(cp);
1116 if (cp && auth_pool_name) {
1117 acct_pool_name = auth_pool_name;
1118 if (!add_pool_to_realm(rc, cs,
1119 auth_pool_name, &auth_pool,
1120 HOME_TYPE_AUTH, 1)) {
1123 if (!add_pool_to_realm(rc, cs,
1124 auth_pool_name, &acct_pool,
1125 HOME_TYPE_ACCT, 0)) {
1130 cp = cf_pair_find(cs, "auth_pool");
1131 if (cp) auth_pool_name = cf_pair_value(cp);
1132 if (cp && auth_pool_name) {
1134 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1137 if (!add_pool_to_realm(rc, cs,
1138 auth_pool_name, &auth_pool,
1139 HOME_TYPE_AUTH, 1)) {
1144 cp = cf_pair_find(cs, "acct_pool");
1145 if (cp) acct_pool_name = cf_pair_value(cp);
1146 if (cp && acct_pool_name) {
1147 int do_print = TRUE;
1150 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1155 (strcmp(auth_pool_name, acct_pool_name) != 0)) {
1159 if (!add_pool_to_realm(rc, cs,
1160 acct_pool_name, &acct_pool,
1161 HOME_TYPE_ACCT, do_print)) {
1166 cf_log_info(cs, " realm %s {", name2);
1169 * The realm MAY already exist if it's an old-style realm.
1170 * In that case, merge the old-style realm with this one.
1172 r = realm_find(name2);
1174 if (cf_pair_find(cs, "auth_pool") ||
1175 cf_pair_find(cs, "acct_pool")) {
1176 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1180 if (!old_realm_config(rc, cs, r)) {
1184 cf_log_info(cs, " } # realm %s", name2);
1188 r = rad_malloc(sizeof(*r));
1189 memset(r, 0, sizeof(*r));
1192 r->auth_pool = auth_pool;
1193 r->acct_pool = acct_pool;
1196 if (auth_pool_name &&
1197 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1198 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1200 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1201 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1204 cp = cf_pair_find(cs, "nostrip");
1205 if (cp && (cf_pair_value(cp) == NULL)) {
1207 cf_log_info(cs, "\tnostrip");
1211 * We're a new-style realm. Complain if we see the old
1214 if (r->auth_pool || r->acct_pool) {
1215 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1216 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1217 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1218 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1219 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1224 * The realm MAY be an old-style realm, as there
1225 * was no auth_pool or acct_pool. Double-check
1226 * it, just to be safe.
1228 } else if (!old_realm_config(rc, cs, r)) {
1232 if (!rbtree_insert(realms_byname, r)) {
1233 rad_assert("Internal sanity check failed");
1237 cf_log_info(cs, " }");
1242 cf_log_info(cs, " } # realm %s", name2);
1248 int realms_init(CONF_SECTION *config)
1251 realm_config_t *rc, *old_rc;
1253 if (realms_byname) return 1;
1255 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1256 if (!realms_byname) {
1261 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
1262 if (!home_servers_byaddr) {
1267 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1268 if (!home_servers_byname) {
1273 home_pools_byname = rbtree_create(home_pool_name_cmp, free, 0);
1274 if (!home_pools_byname) {
1279 rc = rad_malloc(sizeof(*rc));
1280 memset(rc, 0, sizeof(*rc));
1283 cs = cf_subsection_find_next(config, NULL, "proxy");
1285 cf_section_parse(cs, rc, proxy_config);
1287 rc->dead_time = DEAD_TIME;
1288 rc->retry_count = RETRY_COUNT;
1289 rc->retry_delay = RETRY_DELAY;
1291 rc->wake_all_if_all_dead= 0;
1294 for (cs = cf_subsection_find_next(config, NULL, "realm");
1296 cs = cf_subsection_find_next(config, cs, "realm")) {
1297 if (!realm_add(rc, cs)) {
1304 xlat_register("home_server", xlat_home_server, NULL);
1305 xlat_register("home_server_pool", xlat_server_pool, NULL);
1308 * Swap pointers atomically.
1310 old_rc = realm_config;
1319 * Find a realm in the REALM list.
1321 REALM *realm_find(const char *name)
1325 if (!name) name = "NULL";
1327 myrealm.name = name;
1328 return rbtree_finddata(realms_byname, &myrealm);
1332 home_server *home_server_ldb(const char *realmname,
1333 home_pool_t *pool, REQUEST *request)
1337 home_server *found = NULL;
1343 * Determine how to pick choose the home server.
1345 switch (pool->type) {
1349 * For load-balancing by client IP address, we
1350 * pick a home server by hashing the client IP.
1352 * This isn't as even a load distribution as
1353 * tracking the State attribute, but it's better
1356 case HOME_POOL_CLIENT_BALANCE:
1357 switch (request->packet->src_ipaddr.af) {
1359 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1360 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1363 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1364 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1370 start = hash % pool->num_home_servers;
1373 case HOME_POOL_CLIENT_PORT_BALANCE:
1374 switch (request->packet->src_ipaddr.af) {
1376 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1377 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1380 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1381 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1387 fr_hash_update(&request->packet->src_port,
1388 sizeof(request->packet->src_port), hash);
1389 start = hash % pool->num_home_servers;
1392 case HOME_POOL_KEYED_BALANCE:
1393 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY)) != NULL) {
1394 hash = fr_hash(vp->vp_strvalue, vp->length);
1395 start = hash % pool->num_home_servers;
1400 case HOME_POOL_LOAD_BALANCE:
1401 found = pool->servers[0];
1409 * Starting with the home server we chose, loop through
1410 * all home servers. If the current one is dead, skip
1411 * it. If it is too busy, skip it.
1413 * Otherwise, use it.
1415 for (count = 0; count < pool->num_home_servers; count++) {
1416 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
1418 if (home->state == HOME_STATE_IS_DEAD) {
1423 * This home server is too busy. Choose another one.
1425 if (home->currently_outstanding >= home->max_outstanding) {
1429 if (pool->type != HOME_POOL_LOAD_BALANCE) {
1433 DEBUG3("PROXY %s %d\t%s %d",
1434 found->name, found->currently_outstanding,
1435 home->name, home->currently_outstanding);
1438 * Prefer this server if it's less busy than the
1439 * one we previously found.
1441 if (home->currently_outstanding < found->currently_outstanding) {
1442 DEBUG3("Choosing %s: It's less busy than %s",
1443 home->name, found->name);
1449 * Ignore servers which are busier than the one
1452 if (home->currently_outstanding > found->currently_outstanding) {
1453 DEBUG3("Skipping %s: It's busier than %s",
1454 home->name, found->name);
1458 if (home->total_requests_sent < found->total_requests_sent) {
1459 DEBUG3("Choosing %s: It's been less busy than %s",
1460 home->name, found->name);
1465 if (home->total_requests_sent > found->total_requests_sent) {
1466 DEBUG3("Skipping %s: It's been busier than %s",
1467 home->name, found->name);
1472 * From the list of servers which have the same
1473 * load, choose one at random.
1475 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
1479 } /* loop over the home servers */
1481 if (found) return found;
1484 * No live match found, and no fallback to the "DEFAULT"
1485 * realm. We fix this by blindly marking all servers as
1486 * "live". But only do it for ones that don't support
1487 * "pings", as they will be marked live when they
1488 * actually are live.
1490 if (!realm_config->fallback &&
1491 realm_config->wake_all_if_all_dead) {
1492 home_server *lb = NULL;
1494 for (count = 0; count < pool->num_home_servers; count++) {
1495 home_server *home = pool->servers[count];
1497 if ((home->state == HOME_STATE_IS_DEAD) &&
1498 (home->ping_check == HOME_PING_CHECK_NONE)) {
1499 home->state = HOME_STATE_ALIVE;
1508 * Still nothing. Look up the DEFAULT realm, but only
1509 * if we weren't looking up the NULL or DEFAULT realms.
1511 if (realm_config->fallback &&
1513 (strcmp(realmname, "NULL") != 0) &&
1514 (strcmp(realmname, "DEFAULT") != 0)) {
1515 REALM *rd = realm_find("DEFAULT");
1517 if (!rd) return NULL;
1520 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1521 pool = rd->auth_pool;
1523 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1524 pool = rd->acct_pool;
1526 if (!pool) return NULL;
1528 DEBUG2(" Realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
1529 return home_server_ldb(rd->name, pool, request);
1533 * Still haven't found anything. Oh well.
1539 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port)
1543 myhome.ipaddr = *ipaddr;
1546 return rbtree_finddata(home_servers_byaddr, &myhome);