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>
39 * For POSIX Regular expressions.
40 * (0) Means no extended regular expressions.
41 * REG_EXTENDED means use extended regular expressions.
44 #define REG_EXTENDED (0)
56 static rbtree_t *realms_byname = NULL;
59 typedef struct realm_regex_t {
61 struct realm_regex_t *next;
64 static realm_regex_t *realms_regex = NULL;
66 #endif /* HAVE_REGEX_H */
68 typedef struct realm_config_t {
74 int wake_all_if_all_dead;
77 static realm_config_t *realm_config = NULL;
80 static rbtree_t *home_servers_byaddr = NULL;
81 static rbtree_t *home_servers_byname = NULL;
83 static int home_server_max_number = 0;
84 static rbtree_t *home_servers_bynumber = NULL;
87 static rbtree_t *home_pools_byname = NULL;
90 * Map the proxy server configuration parameters to variables.
92 static const CONF_PARSER proxy_config[] = {
93 { "retry_delay", PW_TYPE_INTEGER,
94 offsetof(realm_config_t, retry_delay),
95 NULL, Stringify(RETRY_DELAY) },
97 { "retry_count", PW_TYPE_INTEGER,
98 offsetof(realm_config_t, retry_count),
99 NULL, Stringify(RETRY_COUNT) },
101 { "default_fallback", PW_TYPE_BOOLEAN,
102 offsetof(realm_config_t, fallback),
105 { "dead_time", PW_TYPE_INTEGER,
106 offsetof(realm_config_t, dead_time),
107 NULL, Stringify(DEAD_TIME) },
109 { "wake_all_if_all_dead", PW_TYPE_BOOLEAN,
110 offsetof(realm_config_t, wake_all_if_all_dead),
113 { NULL, -1, 0, NULL, NULL }
117 static int realm_name_cmp(const void *one, const void *two)
119 const REALM *a = one;
120 const REALM *b = two;
122 return strcasecmp(a->name, b->name);
127 static int home_server_name_cmp(const void *one, const void *two)
129 const home_server *a = one;
130 const home_server *b = two;
132 if (a->type < b->type) return -1;
133 if (a->type > b->type) return +1;
135 return strcasecmp(a->name, b->name);
138 static int home_server_addr_cmp(const void *one, const void *two)
140 const home_server *a = one;
141 const home_server *b = two;
143 if (a->server && !b->server) return -1;
144 if (!a->server && b->server) return +1;
145 if (a->server && b->server) {
146 int rcode = a->type - b->type;
147 if (rcode != 0) return rcode;
148 return strcmp(a->server, b->server);
151 if (a->port < b->port) return -1;
152 if (a->port > b->port) return +1;
154 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
158 static int home_server_number_cmp(const void *one, const void *two)
160 const home_server *a = one;
161 const home_server *b = two;
163 return (a->number - b->number);
167 static int home_pool_name_cmp(const void *one, const void *two)
169 const home_pool_t *a = one;
170 const home_pool_t *b = two;
172 if (a->server_type < b->server_type) return -1;
173 if (a->server_type > b->server_type) return +1;
175 return strcasecmp(a->name, b->name);
180 * Xlat for %{home_server:foo}
182 static size_t xlat_home_server(UNUSED void *instance, REQUEST *request,
183 char *fmt, char *out, size_t outlen,
184 UNUSED RADIUS_ESCAPE_STRING func)
186 const char *value = NULL;
189 if (!fmt || !out || (outlen < 1)) return 0;
191 if (!request || !request->home_server) {
196 cp = cf_pair_find(request->home_server->cs, fmt);
197 if (!cp || !(value = cf_pair_value(cp))) {
202 strlcpy(out, value, outlen);
209 * Xlat for %{home_server_pool:foo}
211 static size_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
212 char *fmt, char *out, size_t outlen,
213 UNUSED RADIUS_ESCAPE_STRING func)
215 const char *value = NULL;
218 if (!fmt || !out || (outlen < 1)) return 0;
220 if (!request || !request->home_pool) {
225 cp = cf_pair_find(request->home_pool->cs, fmt);
226 if (!cp || !(value = cf_pair_value(cp))) {
231 strlcpy(out, value, outlen);
237 void realms_free(void)
241 rbtree_free(home_servers_bynumber);
242 home_servers_bynumber = NULL;
245 rbtree_free(home_servers_byname);
246 home_servers_byname = NULL;
248 rbtree_free(home_servers_byaddr);
249 home_servers_byaddr = NULL;
251 rbtree_free(home_pools_byname);
252 home_pools_byname = NULL;
255 rbtree_free(realms_byname);
256 realms_byname = NULL;
260 realm_regex_t *this, *next;
262 for (this = realms_regex; this != NULL; this = next) {
275 static struct in_addr hs_ip4addr;
276 static struct in6_addr hs_ip6addr;
277 static char *hs_type = NULL;
278 static char *hs_check = NULL;
279 static char *hs_virtual_server = NULL;
281 static CONF_PARSER home_server_config[] = {
282 { "ipaddr", PW_TYPE_IPADDR,
283 0, &hs_ip4addr, NULL },
284 { "ipv6addr", PW_TYPE_IPV6ADDR,
285 0, &hs_ip6addr, NULL },
286 { "virtual_server", PW_TYPE_STRING_PTR,
287 0, &hs_virtual_server, NULL },
289 { "port", PW_TYPE_INTEGER,
290 offsetof(home_server,port), NULL, "0" },
292 { "type", PW_TYPE_STRING_PTR,
295 { "secret", PW_TYPE_STRING_PTR,
296 offsetof(home_server,secret), NULL, NULL},
298 { "response_window", PW_TYPE_INTEGER,
299 offsetof(home_server,response_window), NULL, "30" },
300 { "max_outstanding", PW_TYPE_INTEGER,
301 offsetof(home_server,max_outstanding), NULL, "65536" },
303 { "zombie_period", PW_TYPE_INTEGER,
304 offsetof(home_server,zombie_period), NULL, "40" },
305 { "status_check", PW_TYPE_STRING_PTR,
306 0, &hs_check, "none" },
307 { "ping_check", PW_TYPE_STRING_PTR,
308 0, &hs_check, NULL },
310 { "ping_interval", PW_TYPE_INTEGER,
311 offsetof(home_server,ping_interval), NULL, "30" },
312 { "check_interval", PW_TYPE_INTEGER,
313 offsetof(home_server,ping_interval), NULL, "30" },
314 { "num_answers_to_alive", PW_TYPE_INTEGER,
315 offsetof(home_server,num_pings_to_alive), NULL, "3" },
316 { "num_pings_to_alive", PW_TYPE_INTEGER,
317 offsetof(home_server,num_pings_to_alive), NULL, "3" },
318 { "revive_interval", PW_TYPE_INTEGER,
319 offsetof(home_server,revive_interval), NULL, "300" },
320 { "status_check_timeout", PW_TYPE_INTEGER,
321 offsetof(home_server,ping_timeout), NULL, "4" },
323 { "username", PW_TYPE_STRING_PTR,
324 offsetof(home_server,ping_user_name), NULL, NULL},
325 { "password", PW_TYPE_STRING_PTR,
326 offsetof(home_server,ping_user_password), NULL, NULL},
329 { "historic_average_window", PW_TYPE_INTEGER,
330 offsetof(home_server,ema.window), NULL, NULL },
334 { "irt", PW_TYPE_INTEGER,
335 offsetof(home_server, coa_irt), 0, Stringify(2) },
336 { "mrt", PW_TYPE_INTEGER,
337 offsetof(home_server, coa_mrt), 0, Stringify(16) },
338 { "mrc", PW_TYPE_INTEGER,
339 offsetof(home_server, coa_mrc), 0, Stringify(5) },
340 { "mrd", PW_TYPE_INTEGER,
341 offsetof(home_server, coa_mrd), 0, Stringify(30) },
344 { NULL, -1, 0, NULL, NULL } /* end the list */
348 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs, int pool_type)
355 free(hs_virtual_server); /* used only for printing during parsing */
356 hs_virtual_server = NULL;
358 name2 = cf_section_name1(cs);
359 if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
360 cf_log_err(cf_sectiontoitem(cs),
361 "Section is not a home_server.");
365 name2 = cf_section_name2(cs);
367 cf_log_err(cf_sectiontoitem(cs),
368 "Home server section is missing a name.");
372 home = rad_malloc(sizeof(*home));
373 memset(home, 0, sizeof(*home));
378 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
379 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
380 if (cf_section_parse(cs, home, home_server_config) < 0) {
386 * Figure out which one to use.
388 if (cf_pair_find(cs, "ipaddr")) {
389 home->ipaddr.af = AF_INET;
390 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
392 } else if (cf_pair_find(cs, "ipv6addr")) {
393 home->ipaddr.af = AF_INET6;
394 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
396 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
397 home->ipaddr.af = AF_UNSPEC;
398 home->server = cf_pair_value(cp);
400 cf_log_err(cf_sectiontoitem(cs),
401 "Invalid value for virtual_server");
405 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
407 cf_log_err(cf_sectiontoitem(cs),
408 "No such server %s", home->server);
413 * When CoA is used, the user has to specify the type
414 * of the home server, even when they point to
417 home->secret = strdup("");
421 cf_log_err(cf_sectiontoitem(cs),
422 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
433 if (!home->port || (home->port > 65535)) {
434 cf_log_err(cf_sectiontoitem(cs),
435 "No port, or invalid port defined for home server %s.",
441 cf_log_err(cf_sectiontoitem(cs),
442 "Fatal error! Home server %s is ourselves!",
448 cf_log_err(cf_sectiontoitem(cs),
449 "No shared secret defined for home server %s.",
455 * Use a reasonable default.
458 if (!hs_type) hs_type = strdup("auth+acct");
460 if (strcasecmp(hs_type, "auth") == 0) {
461 home->type = HOME_TYPE_AUTH;
462 if (pool_type != home->type) {
464 cf_log_err(cf_sectiontoitem(cs),
465 "Server pool cannot include home server %s of type \"%s\"",
470 } else if (strcasecmp(hs_type, "acct") == 0) {
471 home->type = HOME_TYPE_ACCT;
472 if (pool_type != home->type) goto mismatch;
474 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
475 home->type = HOME_TYPE_AUTH;
479 } else if (strcasecmp(hs_type, "coa") == 0) {
480 home->type = HOME_TYPE_COA;
483 if (pool_type != home->type) goto mismatch;
485 if (home->server != NULL) {
486 cf_log_err(cf_sectiontoitem(cs),
487 "Home servers of type \"coa\" cannot point to a virtual server");
493 cf_log_err(cf_sectiontoitem(cs),
494 "Invalid type \"%s\" for home server %s.",
501 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
502 home->ping_check = HOME_PING_CHECK_NONE;
504 } else if (strcasecmp(hs_check, "status-server") == 0) {
505 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
507 } else if (strcasecmp(hs_check, "request") == 0) {
508 home->ping_check = HOME_PING_CHECK_REQUEST;
511 cf_log_err(cf_sectiontoitem(cs),
512 "Invalid ping_check \"%s\" for home server %s.",
519 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
520 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
521 if (!home->ping_user_name) {
522 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable ping checks");
526 if ((home->type == HOME_TYPE_AUTH) &&
527 !home->ping_user_password) {
528 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable ping checks");
533 if ((home->ipaddr.af != AF_UNSPEC) && /* could be virtual server */
534 rbtree_finddata(home_servers_byaddr, home)) {
535 cf_log_err(cf_sectiontoitem(cs), "Duplicate home server");
539 if (!rbtree_insert(home_servers_byname, home)) {
540 cf_log_err(cf_sectiontoitem(cs),
541 "Internal error %d adding home server %s.",
546 if ((home->ipaddr.af != AF_UNSPEC) && /* could be virtual server */
547 !rbtree_insert(home_servers_byaddr, home)) {
548 rbtree_deletebydata(home_servers_byname, home);
549 cf_log_err(cf_sectiontoitem(cs),
550 "Internal error %d adding home server %s.",
556 home->number = home_server_max_number++;
557 if (!rbtree_insert(home_servers_bynumber, home)) {
558 rbtree_deletebydata(home_servers_byname, home);
559 if (home->ipaddr.af != AF_UNSPEC) {
560 rbtree_deletebydata(home_servers_byname, home);
562 cf_log_err(cf_sectiontoitem(cs),
563 "Internal error %d adding home server %s.",
569 if (home->response_window < 5) home->response_window = 5;
570 if (home->response_window > 60) home->response_window = 60;
572 if (home->max_outstanding < 8) home->max_outstanding = 8;
573 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
575 if (home->ping_interval < 6) home->ping_interval = 6;
576 if (home->ping_interval > 120) home->ping_interval = 120;
578 if (home->zombie_period < 20) home->zombie_period = 20;
579 if (home->zombie_period > 120) home->zombie_period = 120;
581 if (home->zombie_period < home->response_window) {
582 home->zombie_period = home->response_window;
585 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
586 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
588 if (home->ping_timeout < 3) home->ping_timeout = 3;
589 if (home->ping_timeout > 10) home->ping_timeout = 10;
591 if (home->revive_interval < 60) home->revive_interval = 60;
592 if (home->revive_interval > 3600) home->revive_interval = 3600;
595 if (home->coa_irt < 1) home->coa_irt = 1;
596 if (home->coa_irt > 5) home->coa_irt = 5;
598 if (home->coa_mrc < 0) home->coa_mrc = 0;
599 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
601 if (home->coa_mrt < 0) home->coa_mrt = 0;
602 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
604 if (home->coa_mrd < 5) home->coa_mrd = 5;
605 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
609 home_server *home2 = rad_malloc(sizeof(*home2));
611 memcpy(home2, home, sizeof(*home2));
613 home2->type = HOME_TYPE_ACCT;
615 home2->ping_user_password = NULL;
618 if (!rbtree_insert(home_servers_byname, home2)) {
619 cf_log_err(cf_sectiontoitem(cs),
620 "Internal error %d adding home server %s.",
626 if ((home->ipaddr.af != AF_UNSPEC) &&
627 !rbtree_insert(home_servers_byaddr, home2)) {
628 rbtree_deletebydata(home_servers_byname, home2);
629 cf_log_err(cf_sectiontoitem(cs),
630 "Internal error %d adding home server %s.",
637 home2->number = home_server_max_number++;
638 if (!rbtree_insert(home_servers_bynumber, home2)) {
639 rbtree_deletebydata(home_servers_byname, home2);
640 if (home2->ipaddr.af != AF_UNSPEC) {
641 rbtree_deletebydata(home_servers_byname, home2);
643 cf_log_err(cf_sectiontoitem(cs),
644 "Internal error %d adding home server %s.",
656 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
657 int server_type, int num_home_servers)
661 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
663 if (!pool) return NULL; /* just for pairanoia */
665 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
670 pool->server_type = server_type;
671 pool->num_home_servers = num_home_servers;
676 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
677 const char *name, int server_type,
680 home_server myhome, *home;
681 CONF_SECTION *server_cs;
684 cf_log_err(cf_pairtoitem(cp),
685 "No value given for home_server.");
690 myhome.type = server_type;
691 home = rbtree_finddata(home_servers_byname, &myhome);
697 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
699 cf_log_err(cf_pairtoitem(cp),
700 "Unknown home_server \"%s\".", name);
704 if (!home_server_add(rc, server_cs, server_type)) {
708 home = rbtree_finddata(home_servers_byname, &myhome);
718 static int server_pool_add(realm_config_t *rc,
719 CONF_SECTION *cs, int server_type, int do_print)
722 home_pool_t *pool = NULL;
725 int num_home_servers;
728 name2 = cf_section_name1(cs);
729 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
730 (strcasecmp(name2, "home_server_pool") != 0))) {
731 cf_log_err(cf_sectiontoitem(cs),
732 "Section is not a home_server_pool.");
736 name2 = cf_section_name2(cs);
738 cf_log_err(cf_sectiontoitem(cs),
739 "Server pool section is missing a name.");
744 * Count the home servers and initalize them.
746 num_home_servers = 0;
747 for (cp = cf_pair_find(cs, "home_server");
749 cp = cf_pair_find_next(cs, cp, "home_server")) {
752 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
753 server_type, &home)) {
759 if (num_home_servers == 0) {
760 cf_log_err(cf_sectiontoitem(cs),
761 "No home servers defined in pool %s",
766 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
772 * Fallback servers must be defined, and must be
775 cp = cf_pair_find(cs, "fallback");
778 if (server_type == HOME_TYPE_COA) {
779 cf_log_err(cf_sectiontoitem(cs), "Home server pools of type \"coa\" cannot have a fallback virtual server.");
784 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
785 server_type, &pool->fallback)) {
790 if (!pool->fallback->server) {
791 cf_log_err(cf_sectiontoitem(cs), "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
796 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
798 cp = cf_pair_find(cs, "type");
800 static FR_NAME_NUMBER pool_types[] = {
801 { "load-balance", HOME_POOL_LOAD_BALANCE },
802 { "fail-over", HOME_POOL_FAIL_OVER },
803 { "round_robin", HOME_POOL_LOAD_BALANCE },
804 { "fail_over", HOME_POOL_FAIL_OVER },
805 { "client-balance", HOME_POOL_CLIENT_BALANCE },
806 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
807 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
811 value = cf_pair_value(cp);
813 cf_log_err(cf_pairtoitem(cp),
814 "No value given for type.");
818 pool->type = fr_str2int(pool_types, value, 0);
820 cf_log_err(cf_pairtoitem(cp),
821 "Unknown type \"%s\".",
826 if (do_print) cf_log_info(cs, "\ttype = %s", value);
829 cp = cf_pair_find(cs, "virtual_server");
831 pool->virtual_server = cf_pair_value(cp);
832 if (!pool->virtual_server) {
833 cf_log_err(cf_pairtoitem(cp), "No value given for virtual_server");
838 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
841 if (!cf_section_sub_find_name2(rc->cs, "server",
842 pool->virtual_server)) {
843 cf_log_err(cf_pairtoitem(cp), "No such server %s",
844 pool->virtual_server);
850 num_home_servers = 0;
851 for (cp = cf_pair_find(cs, "home_server");
853 cp = cf_pair_find_next(cs, cp, "home_server")) {
856 value = cf_pair_value(cp);
858 memset(&myhome, 0, sizeof(&myhome));
860 myhome.type = server_type;
862 home = rbtree_finddata(home_servers_byname, &myhome);
864 DEBUG2("Internal sanity check failed");
869 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
873 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
874 pool->servers[num_home_servers++] = home;
875 } /* loop over home_server's */
877 if (pool->fallback && do_print) {
878 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
881 if (!rbtree_insert(home_pools_byname, pool)) {
882 rad_assert("Internal sanity check failed");
886 if (do_print) cf_log_info(cs, " }");
888 cf_data_add(cs, "home_server_pool", pool, NULL);
890 rad_assert(pool->server_type != 0);
895 if (do_print) cf_log_info(cs, " }");
901 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
903 const char *name, const char *secret,
904 home_pool_type_t ldflag, home_pool_t **pool_p,
905 int type, const char *server)
908 int i, insert_point, num_home_servers;
909 home_server myhome, *home;
910 home_pool_t mypool, *pool;
913 rc = rc; /* -Wunused */
922 * LOCAL realms get sanity checked, and nothing else happens.
924 if (strcmp(name, "LOCAL") == 0) {
926 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
933 return 0; /* Not proxying. Can't do non-LOCAL realms */
937 mypool.server_type = type;
938 pool = rbtree_finddata(home_pools_byname, &mypool);
940 if (pool->type != ldflag) {
941 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
945 if (pool->server_type != type) {
946 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
953 home = rbtree_finddata(home_servers_byname, &myhome);
955 if (secret && (strcmp(home->secret, secret) != 0)) {
956 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
960 if (home->type != type) {
961 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
966 * See if the home server is already listed
967 * in the pool. If so, do nothing else.
969 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
970 if (pool->servers[i] == home) {
977 * If we do have a pool, check that there is room to
978 * insert the home server we've found, or the one that we
981 * Note that we insert it into the LAST available
982 * position, in order to maintain the same order as in
983 * the configuration files.
987 for (i = pool->num_home_servers - 1; i >= 0; i--) {
988 if (pool->servers[i]) break;
990 if (!pool->servers[i]) {
995 if (insert_point < 0) {
996 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);
1002 * No home server, allocate one.
1008 home = rad_malloc(sizeof(*home));
1009 memset(home, 0, sizeof(*home));
1012 home->hostname = name;
1014 home->secret = secret;
1017 p = strchr(name, ':');
1019 if (type == HOME_TYPE_AUTH) {
1020 home->port = PW_AUTH_UDP_PORT;
1022 home->port = PW_ACCT_UDP_PORT;
1028 } else if (p == name) {
1029 cf_log_err(cf_sectiontoitem(cs),
1030 "Invalid hostname %s.",
1036 home->port = atoi(p + 1);
1037 if ((home->port == 0) || (home->port > 65535)) {
1038 cf_log_err(cf_sectiontoitem(cs),
1045 q = rad_malloc((p - name) + 1);
1046 memcpy(q, name, (p - name));
1052 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1053 cf_log_err(cf_sectiontoitem(cs),
1054 "Failed looking up hostname %s.",
1061 home->ipaddr.af = AF_UNSPEC;
1062 home->server = server;
1067 * Use the old-style configuration.
1069 home->max_outstanding = 65535*16;
1070 home->zombie_period = rc->retry_delay * rc->retry_count;
1071 if (home->zombie_period == 0) home->zombie_period =30;
1072 home->response_window = home->zombie_period - 1;
1074 home->ping_check = HOME_PING_CHECK_NONE;
1076 home->revive_interval = rc->dead_time;
1078 if (rbtree_finddata(home_servers_byaddr, home)) {
1079 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
1084 if (!rbtree_insert(home_servers_byname, home)) {
1085 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1090 if (!rbtree_insert(home_servers_byaddr, home)) {
1091 rbtree_deletebydata(home_servers_byname, home);
1092 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1098 home->number = home_server_max_number++;
1099 if (!rbtree_insert(home_servers_bynumber, home)) {
1100 rbtree_deletebydata(home_servers_byname, home);
1101 if (home->ipaddr.af != AF_UNSPEC) {
1102 rbtree_deletebydata(home_servers_byname, home);
1104 cf_log_err(cf_sectiontoitem(cs),
1105 "Internal error %d adding home server %s.",
1114 * We now have a home server, see if we can insert it
1115 * into pre-existing pool.
1117 if (insert_point >= 0) {
1118 rad_assert(pool != NULL);
1119 pool->servers[insert_point] = home;
1123 rad_assert(pool == NULL);
1124 rad_assert(home != NULL);
1127 * Count the old-style realms of this name.
1129 num_home_servers = 0;
1130 for (subcs = cf_section_find_next(cs, NULL, "realm");
1132 subcs = cf_section_find_next(cs, subcs, "realm")) {
1133 const char *this = cf_section_name2(subcs);
1135 if (!this || (strcmp(this, realm) != 0)) continue;
1139 if (num_home_servers == 0) {
1140 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
1145 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1148 pool->servers[0] = home;
1150 if (!rbtree_insert(home_pools_byname, pool)) {
1151 rad_assert("Internal sanity check failed");
1161 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1164 const char *secret = NULL;
1165 home_pool_type_t ldflag;
1168 cp = cf_pair_find(cs, "ldflag");
1169 ldflag = HOME_POOL_FAIL_OVER;
1171 host = cf_pair_value(cp);
1173 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
1177 if (strcasecmp(host, "fail_over") == 0) {
1178 cf_log_info(cs, "\tldflag = fail_over");
1180 } else if (strcasecmp(host, "round_robin") == 0) {
1181 ldflag = HOME_POOL_LOAD_BALANCE;
1182 cf_log_info(cs, "\tldflag = round_robin");
1185 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
1188 } /* else don't print it. */
1191 * Allow old-style if it doesn't exist, or if it exists and
1194 cp = cf_pair_find(cs, "authhost");
1196 host = cf_pair_value(cp);
1198 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
1202 if (strcmp(host, "LOCAL") != 0) {
1203 cp = cf_pair_find(cs, "secret");
1205 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1209 secret = cf_pair_value(cp);
1211 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1216 cf_log_info(cs, "\tauthhost = %s", host);
1218 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1219 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1224 cp = cf_pair_find(cs, "accthost");
1226 host = cf_pair_value(cp);
1228 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1233 * Don't look for a secret again if it was found
1236 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1237 cp = cf_pair_find(cs, "secret");
1239 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1243 secret = cf_pair_value(cp);
1245 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1250 cf_log_info(cs, "\taccthost = %s", host);
1252 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1253 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1258 cp = cf_pair_find(cs, "virtual_server");
1260 host = cf_pair_value(cp);
1262 cf_log_err(cf_pairtoitem(cp), "No value specified for virtual_server");
1266 cf_log_info(cs, "\tvirtual_server = %s", host);
1268 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1269 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1272 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1273 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1278 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1286 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1287 const char *name, home_pool_t **dest,
1288 int server_type, int do_print)
1290 home_pool_t mypool, *pool;
1293 mypool.server_type = server_type;
1295 pool = rbtree_finddata(home_pools_byname, &mypool);
1297 CONF_SECTION *pool_cs;
1299 pool_cs = cf_section_sub_find_name2(rc->cs,
1303 pool_cs = cf_section_sub_find_name2(rc->cs,
1308 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1312 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1316 pool = rbtree_finddata(home_pools_byname, &mypool);
1318 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1323 if (pool->server_type != server_type) {
1324 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1335 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1341 home_pool_t *auth_pool, *acct_pool;
1342 const char *auth_pool_name, *acct_pool_name;
1345 name2 = cf_section_name1(cs);
1346 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1347 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1351 name2 = cf_section_name2(cs);
1353 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1358 auth_pool = acct_pool = NULL;
1359 auth_pool_name = acct_pool_name = NULL;
1362 * Prefer new configuration to old one.
1364 cp = cf_pair_find(cs, "pool");
1365 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1366 if (cp) auth_pool_name = cf_pair_value(cp);
1367 if (cp && auth_pool_name) {
1368 acct_pool_name = auth_pool_name;
1369 if (!add_pool_to_realm(rc, cs,
1370 auth_pool_name, &auth_pool,
1371 HOME_TYPE_AUTH, 1)) {
1374 if (!add_pool_to_realm(rc, cs,
1375 auth_pool_name, &acct_pool,
1376 HOME_TYPE_ACCT, 0)) {
1381 cp = cf_pair_find(cs, "auth_pool");
1382 if (cp) auth_pool_name = cf_pair_value(cp);
1383 if (cp && auth_pool_name) {
1385 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1388 if (!add_pool_to_realm(rc, cs,
1389 auth_pool_name, &auth_pool,
1390 HOME_TYPE_AUTH, 1)) {
1395 cp = cf_pair_find(cs, "acct_pool");
1396 if (cp) acct_pool_name = cf_pair_value(cp);
1397 if (cp && acct_pool_name) {
1398 int do_print = TRUE;
1401 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1406 (strcmp(auth_pool_name, acct_pool_name) != 0)) {
1410 if (!add_pool_to_realm(rc, cs,
1411 acct_pool_name, &acct_pool,
1412 HOME_TYPE_ACCT, do_print)) {
1418 cf_log_info(cs, " realm %s {", name2);
1422 * The realm MAY already exist if it's an old-style realm.
1423 * In that case, merge the old-style realm with this one.
1425 r = realm_find2(name2);
1426 if (r && (strcmp(r->name, name2) == 0)) {
1427 if (cf_pair_find(cs, "auth_pool") ||
1428 cf_pair_find(cs, "acct_pool")) {
1429 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1433 if (!old_realm_config(rc, cs, r)) {
1437 cf_log_info(cs, " } # realm %s", name2);
1443 if (name2[0] == '~') {
1447 * Include substring matches.
1449 if (regcomp(®, name2 + 1,
1450 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
1451 cf_log_err(cf_sectiontoitem(cs),
1452 "Invalid regex in realm \"%s\"", name2);
1459 r = rad_malloc(sizeof(*r));
1460 memset(r, 0, sizeof(*r));
1465 r->auth_pool = auth_pool;
1466 r->acct_pool = acct_pool;
1468 if (auth_pool_name &&
1469 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1470 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1472 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1473 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1477 cp = cf_pair_find(cs, "nostrip");
1478 if (cp && (cf_pair_value(cp) == NULL)) {
1480 cf_log_info(cs, "\tnostrip");
1484 * We're a new-style realm. Complain if we see the old
1487 if (r->auth_pool || r->acct_pool) {
1488 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1489 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1490 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1491 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1492 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1497 * The realm MAY be an old-style realm, as there
1498 * was no auth_pool or acct_pool. Double-check
1499 * it, just to be safe.
1501 } else if (!old_realm_config(rc, cs, r)) {
1507 * It's a regex. Add it to a separate list.
1509 if (name2[0] == '~') {
1510 realm_regex_t *rr, **last;
1512 rr = rad_malloc(sizeof(*rr));
1514 last = &realms_regex;
1515 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1523 cf_log_info(cs, " }");
1528 if (!rbtree_insert(realms_byname, r)) {
1529 rad_assert("Internal sanity check failed");
1533 cf_log_info(cs, " }");
1538 cf_log_info(cs, " } # realm %s", name2);
1544 static int pool_peek_type(CONF_SECTION *cs)
1550 cp = cf_pair_find(cs, "home_server");
1552 cf_log_err(cf_sectiontoitem(cs), "Pool does not contain a \"home_server\" entry");
1553 return HOME_TYPE_INVALID;
1556 name = cf_pair_value(cp);
1558 cf_log_err(cf_pairtoitem(cp), "home_server entry does not reference a home server");
1559 return HOME_TYPE_INVALID;
1562 home = home_server_byname(name);
1564 cf_log_err(cf_pairtoitem(cp), "home_server \"%s\" does not exist", name);
1565 return HOME_TYPE_INVALID;
1572 int realms_init(CONF_SECTION *config)
1575 realm_config_t *rc, *old_rc;
1577 if (realms_byname) return 1;
1579 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1580 if (!realms_byname) {
1586 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
1587 if (!home_servers_byaddr) {
1592 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1593 if (!home_servers_byname) {
1599 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1600 if (!home_servers_bynumber) {
1606 home_pools_byname = rbtree_create(home_pool_name_cmp, free, 0);
1607 if (!home_pools_byname) {
1613 rc = rad_malloc(sizeof(*rc));
1614 memset(rc, 0, sizeof(*rc));
1618 cs = cf_subsection_find_next(config, NULL, "proxy");
1620 cf_section_parse(cs, rc, proxy_config);
1622 rc->dead_time = DEAD_TIME;
1623 rc->retry_count = RETRY_COUNT;
1624 rc->retry_delay = RETRY_DELAY;
1626 rc->wake_all_if_all_dead= 0;
1630 for (cs = cf_subsection_find_next(config, NULL, "realm");
1632 cs = cf_subsection_find_next(config, cs, "realm")) {
1633 if (!realm_add(rc, cs)) {
1642 * CoA pools aren't tied to realms.
1644 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1646 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1649 if (cf_data_find(cs, "home_server_pool")) continue;
1651 type = pool_peek_type(cs);
1652 if (type == HOME_TYPE_INVALID) return 0;
1654 if (!server_pool_add(rc, cs, type, TRUE)) {
1662 xlat_register("home_server", xlat_home_server, NULL);
1663 xlat_register("home_server_pool", xlat_server_pool, NULL);
1667 * Swap pointers atomically.
1669 old_rc = realm_config;
1677 * Find a realm where "name" might be the regex.
1679 REALM *realm_find2(const char *name)
1684 if (!name) name = "NULL";
1686 myrealm.name = name;
1687 realm = rbtree_finddata(realms_byname, &myrealm);
1688 if (realm) return realm;
1692 realm_regex_t *this;
1694 for (this = realms_regex; this != NULL; this = this->next) {
1695 if (strcmp(this->realm->name, name) == 0) {
1703 * Couldn't find a realm. Look for DEFAULT.
1705 myrealm.name = "DEFAULT";
1706 return rbtree_finddata(realms_byname, &myrealm);
1711 * Find a realm in the REALM list.
1713 REALM *realm_find(const char *name)
1718 if (!name) name = "NULL";
1720 myrealm.name = name;
1721 realm = rbtree_finddata(realms_byname, &myrealm);
1722 if (realm) return realm;
1726 realm_regex_t *this;
1728 for (this = realms_regex; this != NULL; this = this->next) {
1733 * Include substring matches.
1735 if (regcomp(®, this->realm->name + 1,
1736 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
1740 compare = regexec(®, name, 0, NULL, 0);
1743 if (compare == 0) return this->realm;
1749 * Couldn't find a realm. Look for DEFAULT.
1751 myrealm.name = "DEFAULT";
1752 return rbtree_finddata(realms_byname, &myrealm);
1757 home_server *home_server_ldb(const char *realmname,
1758 home_pool_t *pool, REQUEST *request)
1762 home_server *found = NULL;
1768 * Determine how to pick choose the home server.
1770 switch (pool->type) {
1774 * For load-balancing by client IP address, we
1775 * pick a home server by hashing the client IP.
1777 * This isn't as even a load distribution as
1778 * tracking the State attribute, but it's better
1781 case HOME_POOL_CLIENT_BALANCE:
1782 switch (request->packet->src_ipaddr.af) {
1784 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1785 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1788 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1789 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1795 start = hash % pool->num_home_servers;
1798 case HOME_POOL_CLIENT_PORT_BALANCE:
1799 switch (request->packet->src_ipaddr.af) {
1801 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1802 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1805 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1806 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1812 fr_hash_update(&request->packet->src_port,
1813 sizeof(request->packet->src_port), hash);
1814 start = hash % pool->num_home_servers;
1817 case HOME_POOL_KEYED_BALANCE:
1818 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY)) != NULL) {
1819 hash = fr_hash(vp->vp_strvalue, vp->length);
1820 start = hash % pool->num_home_servers;
1825 case HOME_POOL_LOAD_BALANCE:
1826 found = pool->servers[0];
1834 * Starting with the home server we chose, loop through
1835 * all home servers. If the current one is dead, skip
1836 * it. If it is too busy, skip it.
1838 * Otherwise, use it.
1840 for (count = 0; count < pool->num_home_servers; count++) {
1841 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
1843 if (home->state == HOME_STATE_IS_DEAD) {
1848 * This home server is too busy. Choose another one.
1850 if (home->currently_outstanding >= home->max_outstanding) {
1856 * We read the packet from a detail file, AND it
1857 * came from this server. Don't re-proxy it
1860 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
1861 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
1862 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
1867 if (pool->type != HOME_POOL_LOAD_BALANCE) {
1876 RDEBUG3("PROXY %s %d\t%s %d",
1877 found->name, found->currently_outstanding,
1878 home->name, home->currently_outstanding);
1881 * Prefer this server if it's less busy than the
1882 * one we previously found.
1884 if (home->currently_outstanding < found->currently_outstanding) {
1885 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
1886 home->name, found->name);
1892 * Ignore servers which are busier than the one
1895 if (home->currently_outstanding > found->currently_outstanding) {
1896 RDEBUG3("PROXY Skipping %s: It's busier than %s",
1897 home->name, found->name);
1902 * From the list of servers which have the same
1903 * load, choose one at random.
1905 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
1909 } /* loop over the home servers */
1911 if (found) return found;
1914 * There's a fallback if they're all dead.
1916 if (pool->fallback) {
1917 return pool->fallback;
1921 * No live match found, and no fallback to the "DEFAULT"
1922 * realm. We fix this by blindly marking all servers as
1923 * "live". But only do it for ones that don't support
1924 * "pings", as they will be marked live when they
1925 * actually are live.
1927 if (!realm_config->fallback &&
1928 realm_config->wake_all_if_all_dead) {
1929 home_server *lb = NULL;
1931 for (count = 0; count < pool->num_home_servers; count++) {
1932 home_server *home = pool->servers[count];
1934 if ((home->state == HOME_STATE_IS_DEAD) &&
1935 (home->ping_check == HOME_PING_CHECK_NONE)) {
1936 home->state = HOME_STATE_ALIVE;
1945 * Still nothing. Look up the DEFAULT realm, but only
1946 * if we weren't looking up the NULL or DEFAULT realms.
1948 if (realm_config->fallback &&
1950 (strcmp(realmname, "NULL") != 0) &&
1951 (strcmp(realmname, "DEFAULT") != 0)) {
1952 REALM *rd = realm_find("DEFAULT");
1954 if (!rd) return NULL;
1957 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1958 pool = rd->auth_pool;
1960 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1961 pool = rd->acct_pool;
1963 if (!pool) return NULL;
1965 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
1966 return home_server_ldb(rd->name, pool, request);
1970 * Still haven't found anything. Oh well.
1976 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port)
1980 memset(&myhome, 0, sizeof(myhome));
1981 myhome.ipaddr = *ipaddr;
1983 myhome.server = NULL; /* we're not called for internal proxying */
1985 return rbtree_finddata(home_servers_byaddr, &myhome);
1989 home_server *home_server_byname(const char *name)
1993 memset(&myhome, 0, sizeof(myhome));
1996 return rbtree_finddata(home_servers_byname, &myhome);
2001 home_server *home_server_bynumber(int number)
2005 memset(&myhome, 0, sizeof(myhome));
2006 myhome.number = number;
2007 myhome.server = NULL; /* we're not called for internal proxying */
2009 return rbtree_finddata(home_servers_bynumber, &myhome);
2013 home_pool_t *home_pool_byname(const char *name, int type)
2017 memset(&mypool, 0, sizeof(mypool));
2019 mypool.server_type = type;
2020 return rbtree_finddata(home_pools_byname, &mypool);