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->server && !b->server) return -1;
106 if (!a->server && b->server) return +1;
107 if (a->server && b->server) {
108 int rcode = a->type - b->type;
109 if (rcode != 0) return rcode;
110 return strcmp(a->server, b->server);
113 if (a->port < b->port) return -1;
114 if (a->port > b->port) return +1;
116 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
120 static int home_pool_name_cmp(const void *one, const void *two)
122 const home_pool_t *a = one;
123 const home_pool_t *b = two;
125 if (a->server_type < b->server_type) return -1;
126 if (a->server_type > b->server_type) return +1;
128 return strcasecmp(a->name, b->name);
133 * Xlat for %{home_server:foo}
135 static size_t xlat_home_server(UNUSED void *instance, REQUEST *request,
136 char *fmt, char *out, size_t outlen,
137 UNUSED RADIUS_ESCAPE_STRING func)
139 const char *value = NULL;
142 if (!fmt || !out || (outlen < 1)) return 0;
144 if (!request || !request->home_server) {
149 cp = cf_pair_find(request->home_server->cs, fmt);
150 if (!cp || !(value = cf_pair_value(cp))) {
155 strlcpy(out, value, outlen);
162 * Xlat for %{home_server_pool:foo}
164 static size_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
165 char *fmt, char *out, size_t outlen,
166 UNUSED RADIUS_ESCAPE_STRING func)
168 const char *value = NULL;
171 if (!fmt || !out || (outlen < 1)) return 0;
173 if (!request || !request->home_pool) {
178 cp = cf_pair_find(request->home_pool->cs, fmt);
179 if (!cp || !(value = cf_pair_value(cp))) {
184 strlcpy(out, value, outlen);
190 void realms_free(void)
192 rbtree_free(home_servers_byname);
193 home_servers_byname = NULL;
195 rbtree_free(home_servers_byaddr);
196 home_servers_byaddr = NULL;
198 rbtree_free(home_pools_byname);
199 home_pools_byname = NULL;
201 rbtree_free(realms_byname);
202 realms_byname = NULL;
208 static struct in_addr hs_ip4addr;
209 static struct in6_addr hs_ip6addr;
210 static char *hs_type = NULL;
211 static char *hs_check = NULL;
212 static char *hs_virtual_server = NULL;
214 static CONF_PARSER home_server_config[] = {
215 { "ipaddr", PW_TYPE_IPADDR,
216 0, &hs_ip4addr, NULL },
217 { "ipv6addr", PW_TYPE_IPV6ADDR,
218 0, &hs_ip6addr, NULL },
219 { "virtual_server", PW_TYPE_STRING_PTR,
220 0, &hs_virtual_server, NULL },
222 { "port", PW_TYPE_INTEGER,
223 offsetof(home_server,port), NULL, "0" },
225 { "type", PW_TYPE_STRING_PTR,
228 { "secret", PW_TYPE_STRING_PTR,
229 offsetof(home_server,secret), NULL, NULL},
231 { "response_window", PW_TYPE_INTEGER,
232 offsetof(home_server,response_window), NULL, "30" },
233 { "max_outstanding", PW_TYPE_INTEGER,
234 offsetof(home_server,max_outstanding), NULL, "65536" },
236 { "zombie_period", PW_TYPE_INTEGER,
237 offsetof(home_server,zombie_period), NULL, "40" },
238 { "status_check", PW_TYPE_STRING_PTR,
239 0, &hs_check, "none" },
240 { "ping_check", PW_TYPE_STRING_PTR,
241 0, &hs_check, "none" },
243 { "ping_interval", PW_TYPE_INTEGER,
244 offsetof(home_server,ping_interval), NULL, "30" },
245 { "check_interval", PW_TYPE_INTEGER,
246 offsetof(home_server,ping_interval), NULL, "30" },
247 { "num_answers_to_alive", PW_TYPE_INTEGER,
248 offsetof(home_server,num_pings_to_alive), NULL, "3" },
249 { "num_pings_to_alive", PW_TYPE_INTEGER,
250 offsetof(home_server,num_pings_to_alive), NULL, "3" },
251 { "revive_interval", PW_TYPE_INTEGER,
252 offsetof(home_server,revive_interval), NULL, "300" },
253 { "status_check_timeout", PW_TYPE_INTEGER,
254 offsetof(home_server,ping_timeout), NULL, "4" },
256 { "username", PW_TYPE_STRING_PTR,
257 offsetof(home_server,ping_user_name), NULL, NULL},
258 { "password", PW_TYPE_STRING_PTR,
259 offsetof(home_server,ping_user_password), NULL, NULL},
261 { NULL, -1, 0, NULL, NULL } /* end the list */
266 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs, int type)
273 free(hs_virtual_server); /* used only for printing during parsing */
274 hs_virtual_server = NULL;
276 name2 = cf_section_name1(cs);
277 if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
278 cf_log_err(cf_sectiontoitem(cs),
279 "Section is not a home_server.");
283 name2 = cf_section_name2(cs);
285 cf_log_err(cf_sectiontoitem(cs),
286 "Home server section is missing a name.");
290 home = rad_malloc(sizeof(*home));
291 memset(home, 0, sizeof(*home));
296 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
297 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
298 cf_section_parse(cs, home, home_server_config);
301 * Figure out which one to use.
303 if (cf_pair_find(cs, "ipaddr")) {
304 home->ipaddr.af = AF_INET;
305 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
307 } else if (cf_pair_find(cs, "ipv6addr")) {
308 home->ipaddr.af = AF_INET6;
309 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
311 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
312 home->ipaddr.af = AF_UNSPEC;
313 home->server = cf_pair_value(cp);
315 cf_log_err(cf_sectiontoitem(cs),
316 "Invalid value for virtual_server");
320 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
322 cf_log_err(cf_sectiontoitem(cs),
323 "No such server %s", home->server);
329 home->secret = strdup("");
333 cf_log_err(cf_sectiontoitem(cs),
334 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
345 if (!home->port || (home->port > 65535)) {
346 cf_log_err(cf_sectiontoitem(cs),
347 "No port, or invalid port defined for home server %s.",
353 cf_log_err(cf_sectiontoitem(cs),
354 "Fatal error! Home server %s is ourselves!",
360 cf_log_err(cf_sectiontoitem(cs),
361 "No shared secret defined for home server %s.",
368 * Use a reasonable default.
371 if (!hs_type) hs_type = strdup("auth+acct");
373 if (strcasecmp(hs_type, "auth") == 0) {
374 home->type = HOME_TYPE_AUTH;
375 if (type != home->type) {
376 cf_log_err(cf_sectiontoitem(cs),
377 "Server pool of \"acct\" servers cannot include home server %s of type \"auth\"",
383 } else if (strcasecmp(hs_type, "acct") == 0) {
384 home->type = HOME_TYPE_ACCT;
385 if (type != home->type) {
386 cf_log_err(cf_sectiontoitem(cs),
387 "Server pool of \"auth\" servers cannot include home server %s of type \"acct\"",
393 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
394 home->type = HOME_TYPE_AUTH;
398 cf_log_err(cf_sectiontoitem(cs),
399 "Invalid type \"%s\" for home server %s.",
411 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
412 home->ping_check = HOME_PING_CHECK_NONE;
414 } else if (strcasecmp(hs_check, "status-server") == 0) {
415 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
417 } else if (strcasecmp(hs_check, "request") == 0) {
418 home->ping_check = HOME_PING_CHECK_REQUEST;
421 cf_log_err(cf_sectiontoitem(cs),
422 "Invalid ping_check \"%s\" for home server %s.",
432 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
433 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
434 if (!home->ping_user_name) {
435 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable ping checks");
440 if ((home->type == HOME_TYPE_AUTH) &&
441 !home->ping_user_password) {
442 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable ping checks");
448 if (rbtree_finddata(home_servers_byaddr, home)) {
449 DEBUG2("Ignoring duplicate home server %s.", name2);
453 if (!rbtree_insert(home_servers_byname, home)) {
454 cf_log_err(cf_sectiontoitem(cs),
455 "Internal error adding home server %s.",
461 if (!rbtree_insert(home_servers_byaddr, home)) {
462 rbtree_deletebydata(home_servers_byname, home);
463 cf_log_err(cf_sectiontoitem(cs),
464 "Internal error adding home server %s.",
470 if (home->response_window < 5) home->response_window = 5;
471 if (home->response_window > 60) home->response_window = 60;
473 if (home->max_outstanding < 8) home->max_outstanding = 8;
474 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
476 if (home->ping_interval < 6) home->ping_interval = 6;
477 if (home->ping_interval > 120) home->ping_interval = 120;
479 if (home->zombie_period < 20) home->zombie_period = 20;
480 if (home->zombie_period > 120) home->zombie_period = 120;
482 if (home->zombie_period < home->response_window) {
483 home->zombie_period = home->response_window;
486 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
487 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
489 if (home->ping_timeout < 3) home->ping_timeout = 3;
490 if (home->ping_timeout > 10) home->ping_timeout = 10;
492 if (home->revive_interval < 60) home->revive_interval = 60;
493 if (home->revive_interval > 3600) home->revive_interval = 3600;
496 home_server *home2 = rad_malloc(sizeof(*home2));
498 memcpy(home2, home, sizeof(*home2));
500 home2->type = HOME_TYPE_ACCT;
502 home2->ping_user_password = NULL;
505 if (!rbtree_insert(home_servers_byname, home2)) {
506 cf_log_err(cf_sectiontoitem(cs),
507 "Internal error adding home server %s.",
513 if (!rbtree_insert(home_servers_byaddr, home2)) {
514 rbtree_deletebydata(home_servers_byname, home2);
515 cf_log_err(cf_sectiontoitem(cs),
516 "Internal error adding home server %s.",
527 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
528 int server_type, int num_home_servers)
532 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
534 if (!pool) return NULL; /* just for pairanoia */
536 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
541 pool->server_type = server_type;
542 pool->num_home_servers = num_home_servers;
547 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
548 const char *name, int server_type,
551 home_server myhome, *home;
552 CONF_SECTION *server_cs;
555 cf_log_err(cf_pairtoitem(cp),
556 "No value given for home_server.");
561 myhome.type = server_type;
562 home = rbtree_finddata(home_servers_byname, &myhome);
568 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
570 cf_log_err(cf_pairtoitem(cp),
571 "Unknown home_server \"%s\".", name);
575 if (!home_server_add(rc, server_cs, server_type)) {
579 home = rbtree_finddata(home_servers_byname, &myhome);
581 radlog(L_ERR, "Internal sanity check failed %d",
591 static int server_pool_add(realm_config_t *rc,
592 CONF_SECTION *cs, int server_type, int do_print)
595 home_pool_t *pool = NULL;
598 int num_home_servers;
601 name2 = cf_section_name1(cs);
602 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
603 (strcasecmp(name2, "home_server_pool") != 0))) {
604 cf_log_err(cf_sectiontoitem(cs),
605 "Section is not a home_server_pool.");
609 name2 = cf_section_name2(cs);
611 cf_log_err(cf_sectiontoitem(cs),
612 "Server pool section is missing a name.");
617 * Count the home servers and initalize them.
619 num_home_servers = 0;
620 for (cp = cf_pair_find(cs, "home_server");
622 cp = cf_pair_find_next(cs, cp, "home_server")) {
625 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
626 server_type, &home)) {
632 if (num_home_servers == 0) {
633 cf_log_err(cf_sectiontoitem(cs),
634 "No home servers defined in pool %s",
639 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
644 cp = cf_pair_find(cs, "fallback");
646 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
647 server_type, &pool->fallback)) {
653 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
655 cp = cf_pair_find(cs, "type");
657 static FR_NAME_NUMBER pool_types[] = {
658 { "load-balance", HOME_POOL_LOAD_BALANCE },
659 { "fail-over", HOME_POOL_FAIL_OVER },
660 { "round_robin", HOME_POOL_LOAD_BALANCE },
661 { "fail_over", HOME_POOL_FAIL_OVER },
662 { "client-balance", HOME_POOL_CLIENT_BALANCE },
663 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
664 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
668 value = cf_pair_value(cp);
670 cf_log_err(cf_pairtoitem(cp),
671 "No value given for type.");
675 pool->type = fr_str2int(pool_types, value, 0);
677 cf_log_err(cf_pairtoitem(cp),
678 "Unknown type \"%s\".",
683 if (do_print) cf_log_info(cs, "\ttype = %s", value);
686 cp = cf_pair_find(cs, "virtual_server");
688 pool->virtual_server = cf_pair_value(cp);
689 if (do_print && pool->virtual_server) {
690 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
693 if (!cf_section_sub_find_name2(rc->cs, "server",
694 pool->virtual_server)) {
695 cf_log_err(cf_pairtoitem(cp), "No such server %s",
696 pool->virtual_server);
702 num_home_servers = 0;
703 for (cp = cf_pair_find(cs, "home_server");
705 cp = cf_pair_find_next(cs, cp, "home_server")) {
708 value = cf_pair_value(cp);
710 memset(&myhome, 0, sizeof(&myhome));
712 myhome.type = server_type;
714 home = rbtree_finddata(home_servers_byname, &myhome);
716 DEBUG2("Internal sanity check failed");
721 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
725 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
726 pool->servers[num_home_servers++] = home;
727 } /* loop over home_server's */
729 if (pool->fallback && do_print) {
730 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
733 if (!rbtree_insert(home_pools_byname, pool)) {
734 rad_assert("Internal sanity check failed");
738 if (do_print) cf_log_info(cs, " }");
740 rad_assert(pool->server_type != 0);
745 if (do_print) cf_log_info(cs, " }");
751 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
753 const char *name, const char *secret,
754 home_pool_type_t ldflag, home_pool_t **pool_p,
755 int type, const char *server)
757 int i, insert_point, num_home_servers;
758 home_server myhome, *home;
759 home_pool_t mypool, *pool;
763 * LOCAL realms get sanity checked, and nothing else happens.
765 if (strcmp(name, "LOCAL") == 0) {
767 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
774 mypool.server_type = type;
775 pool = rbtree_finddata(home_pools_byname, &mypool);
777 if (pool->type != ldflag) {
778 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
782 if (pool->server_type != type) {
783 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
790 home = rbtree_finddata(home_servers_byname, &myhome);
792 if (strcmp(home->secret, secret) != 0) {
793 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
797 if (home->type != type) {
798 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
803 * See if the home server is already listed
804 * in the pool. If so, do nothing else.
806 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
807 if (pool->servers[i] == home) {
814 * If we do have a pool, check that there is room to
815 * insert the home server we've found, or the one that we
818 * Note that we insert it into the LAST available
819 * position, in order to maintain the same order as in
820 * the configuration files.
824 for (i = pool->num_home_servers - 1; i >= 0; i--) {
825 if (pool->servers[i]) break;
827 if (!pool->servers[i]) {
832 if (insert_point < 0) {
833 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);
839 * No home server, allocate one.
845 home = rad_malloc(sizeof(*home));
846 memset(home, 0, sizeof(*home));
849 home->hostname = name;
851 home->secret = secret;
854 p = strchr(name, ':');
856 if (type == HOME_TYPE_AUTH) {
857 home->port = PW_AUTH_UDP_PORT;
859 home->port = PW_ACCT_UDP_PORT;
865 } else if (p == name) {
866 cf_log_err(cf_sectiontoitem(cs),
867 "Invalid hostname %s.",
873 home->port = atoi(p + 1);
874 if ((home->port == 0) || (home->port > 65535)) {
875 cf_log_err(cf_sectiontoitem(cs),
882 q = rad_malloc((p - name) + 1);
883 memcpy(q, name, (p - name));
889 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
890 cf_log_err(cf_sectiontoitem(cs),
891 "Failed looking up hostname %s.",
898 home->ipaddr.af = AF_UNSPEC;
899 home->server = server;
904 * Use the old-style configuration.
906 home->max_outstanding = 65535*16;
907 home->zombie_period = rc->retry_delay * rc->retry_count;
908 if (home->zombie_period == 0) home->zombie_period =30;
909 home->response_window = home->zombie_period - 1;
911 home->ping_check = HOME_PING_CHECK_NONE;
913 home->revive_interval = rc->dead_time;
915 if (rbtree_finddata(home_servers_byaddr, home)) {
916 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
921 if (!rbtree_insert(home_servers_byname, home)) {
922 cf_log_err(cf_sectiontoitem(cs), "Internal error adding home server %s.", name);
927 if (!rbtree_insert(home_servers_byaddr, home)) {
928 rbtree_deletebydata(home_servers_byname, home);
929 cf_log_err(cf_sectiontoitem(cs), "Internal error adding home server %s.", name);
936 * We now have a home server, see if we can insert it
937 * into pre-existing pool.
939 if (insert_point >= 0) {
940 rad_assert(pool != NULL);
941 pool->servers[insert_point] = home;
945 rad_assert(pool == NULL);
946 rad_assert(home != NULL);
949 * Count the old-style realms of this name.
951 num_home_servers = 0;
952 for (subcs = cf_section_find_next(cs, NULL, "realm");
954 subcs = cf_section_find_next(cs, subcs, "realm")) {
955 const char *this = cf_section_name2(subcs);
957 if (!this || (strcmp(this, realm) != 0)) continue;
961 if (num_home_servers == 0) {
962 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
967 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
970 pool->servers[0] = home;
972 if (!rbtree_insert(home_pools_byname, pool)) {
973 rad_assert("Internal sanity check failed");
982 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
985 const char *secret = NULL;
986 home_pool_type_t ldflag;
989 cp = cf_pair_find(cs, "ldflag");
990 ldflag = HOME_POOL_FAIL_OVER;
992 host = cf_pair_value(cp);
994 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
998 if (strcasecmp(host, "fail_over") == 0) {
999 cf_log_info(cs, "\tldflag = fail_over");
1001 } else if (strcasecmp(host, "round_robin") == 0) {
1002 ldflag = HOME_POOL_LOAD_BALANCE;
1003 cf_log_info(cs, "\tldflag = round_robin");
1006 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
1009 } /* else don't print it. */
1012 * Allow old-style if it doesn't exist, or if it exists and
1015 cp = cf_pair_find(cs, "authhost");
1017 host = cf_pair_value(cp);
1019 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
1023 if (strcmp(host, "LOCAL") != 0) {
1024 cp = cf_pair_find(cs, "secret");
1026 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1030 secret = cf_pair_value(cp);
1032 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1037 cf_log_info(cs, "\tauthhost = %s", host);
1039 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1040 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1045 cp = cf_pair_find(cs, "accthost");
1047 host = cf_pair_value(cp);
1049 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1054 * Don't look for a secret again if it was found
1057 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1058 cp = cf_pair_find(cs, "secret");
1060 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1064 secret = cf_pair_value(cp);
1066 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1071 cf_log_info(cs, "\taccthost = %s", host);
1073 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1074 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1079 cp = cf_pair_find(cs, "virtual_server");
1081 host = cf_pair_value(cp);
1083 cf_log_err(cf_pairtoitem(cp), "No value specified for virtual_server");
1087 cf_log_info(cs, "\tvirtual_server = %s", host);
1089 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1090 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1093 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1094 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1099 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1106 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1107 const char *name, home_pool_t **dest,
1108 int server_type, int do_print)
1110 home_pool_t mypool, *pool;
1113 mypool.server_type = server_type;
1115 pool = rbtree_finddata(home_pools_byname, &mypool);
1117 CONF_SECTION *pool_cs;
1119 pool_cs = cf_section_sub_find_name2(rc->cs,
1123 pool_cs = cf_section_sub_find_name2(rc->cs,
1128 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1132 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1136 pool = rbtree_finddata(home_pools_byname, &mypool);
1138 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1143 if (pool->server_type != server_type) {
1144 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1153 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1158 home_pool_t *auth_pool, *acct_pool;
1159 const char *auth_pool_name, *acct_pool_name;
1161 name2 = cf_section_name1(cs);
1162 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1163 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1167 name2 = cf_section_name2(cs);
1169 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1173 auth_pool = acct_pool = NULL;
1174 auth_pool_name = acct_pool_name = NULL;
1177 * Prefer new configuration to old one.
1179 cp = cf_pair_find(cs, "pool");
1180 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1181 if (cp) auth_pool_name = cf_pair_value(cp);
1182 if (cp && auth_pool_name) {
1183 acct_pool_name = auth_pool_name;
1184 if (!add_pool_to_realm(rc, cs,
1185 auth_pool_name, &auth_pool,
1186 HOME_TYPE_AUTH, 1)) {
1189 if (!add_pool_to_realm(rc, cs,
1190 auth_pool_name, &acct_pool,
1191 HOME_TYPE_ACCT, 0)) {
1196 cp = cf_pair_find(cs, "auth_pool");
1197 if (cp) auth_pool_name = cf_pair_value(cp);
1198 if (cp && auth_pool_name) {
1200 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1203 if (!add_pool_to_realm(rc, cs,
1204 auth_pool_name, &auth_pool,
1205 HOME_TYPE_AUTH, 1)) {
1210 cp = cf_pair_find(cs, "acct_pool");
1211 if (cp) acct_pool_name = cf_pair_value(cp);
1212 if (cp && acct_pool_name) {
1213 int do_print = TRUE;
1216 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1221 (strcmp(auth_pool_name, acct_pool_name) != 0)) {
1225 if (!add_pool_to_realm(rc, cs,
1226 acct_pool_name, &acct_pool,
1227 HOME_TYPE_ACCT, do_print)) {
1232 cf_log_info(cs, " realm %s {", name2);
1235 * The realm MAY already exist if it's an old-style realm.
1236 * In that case, merge the old-style realm with this one.
1238 r = realm_find(name2);
1240 if (cf_pair_find(cs, "auth_pool") ||
1241 cf_pair_find(cs, "acct_pool")) {
1242 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1246 if (!old_realm_config(rc, cs, r)) {
1250 cf_log_info(cs, " } # realm %s", name2);
1254 r = rad_malloc(sizeof(*r));
1255 memset(r, 0, sizeof(*r));
1258 r->auth_pool = auth_pool;
1259 r->acct_pool = acct_pool;
1262 if (auth_pool_name &&
1263 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1264 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1266 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1267 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1270 cp = cf_pair_find(cs, "nostrip");
1271 if (cp && (cf_pair_value(cp) == NULL)) {
1273 cf_log_info(cs, "\tnostrip");
1277 * We're a new-style realm. Complain if we see the old
1280 if (r->auth_pool || r->acct_pool) {
1281 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1282 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1283 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1284 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1285 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1290 * The realm MAY be an old-style realm, as there
1291 * was no auth_pool or acct_pool. Double-check
1292 * it, just to be safe.
1294 } else if (!old_realm_config(rc, cs, r)) {
1298 if (!rbtree_insert(realms_byname, r)) {
1299 rad_assert("Internal sanity check failed");
1303 cf_log_info(cs, " }");
1308 cf_log_info(cs, " } # realm %s", name2);
1314 int realms_init(CONF_SECTION *config)
1317 realm_config_t *rc, *old_rc;
1319 if (realms_byname) return 1;
1321 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1322 if (!realms_byname) {
1327 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
1328 if (!home_servers_byaddr) {
1333 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1334 if (!home_servers_byname) {
1339 home_pools_byname = rbtree_create(home_pool_name_cmp, free, 0);
1340 if (!home_pools_byname) {
1345 rc = rad_malloc(sizeof(*rc));
1346 memset(rc, 0, sizeof(*rc));
1349 cs = cf_subsection_find_next(config, NULL, "proxy");
1351 cf_section_parse(cs, rc, proxy_config);
1353 rc->dead_time = DEAD_TIME;
1354 rc->retry_count = RETRY_COUNT;
1355 rc->retry_delay = RETRY_DELAY;
1357 rc->wake_all_if_all_dead= 0;
1360 for (cs = cf_subsection_find_next(config, NULL, "realm");
1362 cs = cf_subsection_find_next(config, cs, "realm")) {
1363 if (!realm_add(rc, cs)) {
1370 xlat_register("home_server", xlat_home_server, NULL);
1371 xlat_register("home_server_pool", xlat_server_pool, NULL);
1374 * Swap pointers atomically.
1376 old_rc = realm_config;
1385 * Find a realm in the REALM list.
1387 REALM *realm_find(const char *name)
1392 if (!name) name = "NULL";
1394 myrealm.name = name;
1395 realm = rbtree_finddata(realms_byname, &myrealm);
1396 if (realm) return realm;
1399 * Couldn't find a realm. Look for DEFAULT.
1401 myrealm.name = "DEFAULT";
1402 return rbtree_finddata(realms_byname, &myrealm);
1406 home_server *home_server_ldb(const char *realmname,
1407 home_pool_t *pool, REQUEST *request)
1411 home_server *found = NULL;
1417 * Determine how to pick choose the home server.
1419 switch (pool->type) {
1423 * For load-balancing by client IP address, we
1424 * pick a home server by hashing the client IP.
1426 * This isn't as even a load distribution as
1427 * tracking the State attribute, but it's better
1430 case HOME_POOL_CLIENT_BALANCE:
1431 switch (request->packet->src_ipaddr.af) {
1433 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1434 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1437 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1438 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1444 start = hash % pool->num_home_servers;
1447 case HOME_POOL_CLIENT_PORT_BALANCE:
1448 switch (request->packet->src_ipaddr.af) {
1450 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1451 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1454 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1455 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1461 fr_hash_update(&request->packet->src_port,
1462 sizeof(request->packet->src_port), hash);
1463 start = hash % pool->num_home_servers;
1466 case HOME_POOL_KEYED_BALANCE:
1467 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY)) != NULL) {
1468 hash = fr_hash(vp->vp_strvalue, vp->length);
1469 start = hash % pool->num_home_servers;
1474 case HOME_POOL_LOAD_BALANCE:
1475 found = pool->servers[0];
1483 * Starting with the home server we chose, loop through
1484 * all home servers. If the current one is dead, skip
1485 * it. If it is too busy, skip it.
1487 * Otherwise, use it.
1489 for (count = 0; count < pool->num_home_servers; count++) {
1490 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
1492 if (home->state == HOME_STATE_IS_DEAD) {
1497 * This home server is too busy. Choose another one.
1499 if (home->currently_outstanding >= home->max_outstanding) {
1503 if (pool->type != HOME_POOL_LOAD_BALANCE) {
1507 DEBUG3("PROXY %s %d\t%s %d",
1508 found->name, found->currently_outstanding,
1509 home->name, home->currently_outstanding);
1512 * Prefer this server if it's less busy than the
1513 * one we previously found.
1515 if (home->currently_outstanding < found->currently_outstanding) {
1516 DEBUG3("Choosing %s: It's less busy than %s",
1517 home->name, found->name);
1523 * Ignore servers which are busier than the one
1526 if (home->currently_outstanding > found->currently_outstanding) {
1527 DEBUG3("Skipping %s: It's busier than %s",
1528 home->name, found->name);
1532 if (home->total_requests_sent < found->total_requests_sent) {
1533 DEBUG3("Choosing %s: It's been less busy than %s",
1534 home->name, found->name);
1539 if (home->total_requests_sent > found->total_requests_sent) {
1540 DEBUG3("Skipping %s: It's been busier than %s",
1541 home->name, found->name);
1546 * From the list of servers which have the same
1547 * load, choose one at random.
1549 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
1553 } /* loop over the home servers */
1555 if (found) return found;
1558 * There's a fallback if they're all dead.
1560 if (pool->fallback &&
1561 (pool->fallback->state == HOME_STATE_ALIVE)) {
1562 return pool->fallback;
1566 * No live match found, and no fallback to the "DEFAULT"
1567 * realm. We fix this by blindly marking all servers as
1568 * "live". But only do it for ones that don't support
1569 * "pings", as they will be marked live when they
1570 * actually are live.
1572 if (!realm_config->fallback &&
1573 realm_config->wake_all_if_all_dead) {
1574 home_server *lb = NULL;
1576 for (count = 0; count < pool->num_home_servers; count++) {
1577 home_server *home = pool->servers[count];
1579 if ((home->state == HOME_STATE_IS_DEAD) &&
1580 (home->ping_check == HOME_PING_CHECK_NONE)) {
1581 home->state = HOME_STATE_ALIVE;
1590 * Still nothing. Look up the DEFAULT realm, but only
1591 * if we weren't looking up the NULL or DEFAULT realms.
1593 if (realm_config->fallback &&
1595 (strcmp(realmname, "NULL") != 0) &&
1596 (strcmp(realmname, "DEFAULT") != 0)) {
1597 REALM *rd = realm_find("DEFAULT");
1599 if (!rd) return NULL;
1602 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1603 pool = rd->auth_pool;
1605 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1606 pool = rd->acct_pool;
1608 if (!pool) return NULL;
1610 DEBUG2(" Realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
1611 return home_server_ldb(rd->name, pool, request);
1615 * Still haven't found anything. Oh well.
1621 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port)
1625 memset(&myhome, 0, sizeof(myhome));
1626 myhome.ipaddr = *ipaddr;
1628 myhome.server = NULL; /* we're not called for internal proxying */
1630 return rbtree_finddata(home_servers_byaddr, &myhome);