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>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/rad_assert.h>
34 static rbtree_t *realms_byname = NULL;
37 typedef struct realm_regex_t {
39 struct realm_regex_t *next;
42 static realm_regex_t *realms_regex = NULL;
44 #endif /* HAVE_REGEX_H */
46 typedef struct realm_config_t {
52 bool wake_all_if_all_dead;
55 static realm_config_t *realm_config = NULL;
58 static rbtree_t *home_servers_byaddr = NULL;
59 static rbtree_t *home_servers_byname = NULL;
61 static int home_server_max_number = 0;
62 static rbtree_t *home_servers_bynumber = NULL;
65 static rbtree_t *home_pools_byname = NULL;
68 * Map the proxy server configuration parameters to variables.
70 static const CONF_PARSER proxy_config[] = {
71 { "retry_delay", PW_TYPE_INTEGER,
72 offsetof(realm_config_t, retry_delay),
73 NULL, STRINGIFY(RETRY_DELAY) },
75 { "retry_count", PW_TYPE_INTEGER,
76 offsetof(realm_config_t, retry_count),
77 NULL, STRINGIFY(RETRY_COUNT) },
79 { "default_fallback", PW_TYPE_BOOLEAN,
80 offsetof(realm_config_t, fallback),
83 { "dead_time", PW_TYPE_INTEGER,
84 offsetof(realm_config_t, dead_time),
85 NULL, STRINGIFY(DEAD_TIME) },
87 { "wake_all_if_all_dead", PW_TYPE_BOOLEAN,
88 offsetof(realm_config_t, wake_all_if_all_dead),
91 { NULL, -1, 0, NULL, NULL }
95 static int realm_name_cmp(void const *one, void const *two)
100 return strcasecmp(a->name, b->name);
105 static void home_server_free(void *data)
107 home_server_t *home = data;
111 tls_server_conf_free(home->tls);
119 static int home_server_name_cmp(void const *one, void const *two)
121 home_server_t const *a = one;
122 home_server_t const *b = two;
124 if (a->type < b->type) return -1;
125 if (a->type > b->type) return +1;
127 return strcasecmp(a->name, b->name);
130 static int home_server_addr_cmp(void const *one, void const *two)
133 home_server_t const *a = one;
134 home_server_t const *b = two;
136 if (a->server && !b->server) return -1;
137 if (!a->server && b->server) return +1;
138 if (a->server && b->server) {
139 rcode = a->type - b->type;
140 if (rcode != 0) return rcode;
141 return strcmp(a->server, b->server);
144 if (a->port < b->port) return -1;
145 if (a->port > b->port) return +1;
148 if (a->proto < b->proto) return -1;
149 if (a->proto > b->proto) return +1;
152 rcode = fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
153 if (rcode != 0) return rcode;
155 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
159 static int home_server_number_cmp(void const *one, void const *two)
161 home_server_t const *a = one;
162 home_server_t const *b = two;
164 return (a->number - b->number);
168 static int home_pool_name_cmp(void const *one, void const *two)
170 home_pool_t const *a = one;
171 home_pool_t const *b = two;
173 if (a->server_type < b->server_type) return -1;
174 if (a->server_type > b->server_type) return +1;
176 return strcasecmp(a->name, b->name);
180 static size_t xlat_cs(CONF_SECTION *cs, char const *fmt, char *out, size_t outlen)
183 char const *value = NULL;
188 if (strcmp(fmt, "instance") == 0) {
189 value = cf_section_name2(cs);
197 cp = cf_pair_find(cs, fmt);
198 if (!cp || !(value = cf_pair_value(cp))) {
204 strlcpy(out, value, outlen);
211 * Xlat for %{home_server:foo}
213 static ssize_t xlat_home_server(UNUSED void *instance, REQUEST *request,
214 char const *fmt, char *out, size_t outlen)
216 if (!fmt || !out || (outlen < 1)) return 0;
218 if (!request || !request->home_server) {
219 RWDEBUG("No home_server associated with this request");
225 return xlat_cs(request->home_server->cs, fmt, out, outlen);
230 * Xlat for %{home_server_pool:foo}
232 static ssize_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
233 char const *fmt, char *out, size_t outlen)
235 if (!fmt || !out || (outlen < 1)) return 0;
237 if (!request || !request->home_pool) {
238 RWDEBUG("No home_pool associated with this request");
244 return xlat_cs(request->home_pool->cs, fmt, out, outlen);
248 void realms_free(void)
252 rbtree_free(home_servers_bynumber);
253 home_servers_bynumber = NULL;
256 rbtree_free(home_servers_byname);
257 home_servers_byname = NULL;
259 rbtree_free(home_servers_byaddr);
260 home_servers_byaddr = NULL;
262 rbtree_free(home_pools_byname);
263 home_pools_byname = NULL;
266 rbtree_free(realms_byname);
267 realms_byname = NULL;
271 realm_regex_t *this, *next;
273 for (this = realms_regex; this != NULL; this = next) {
282 talloc_free(realm_config);
288 static CONF_PARSER limit_config[] = {
289 { "max_connections", PW_TYPE_INTEGER,
290 offsetof(home_server_t, limit.max_connections), NULL, "16" },
292 { "max_requests", PW_TYPE_INTEGER,
293 offsetof(home_server_t, limit.max_requests), NULL, "0" },
295 { "lifetime", PW_TYPE_INTEGER,
296 offsetof(home_server_t, limit.lifetime), NULL, "0" },
298 { "idle_timeout", PW_TYPE_INTEGER,
299 offsetof(home_server_t, limit.idle_timeout), NULL, "0" },
301 { NULL, -1, 0, NULL, NULL } /* end the list */
304 static struct in_addr hs_ip4addr;
305 static struct in6_addr hs_ip6addr;
306 static char *hs_srcipaddr = NULL;
307 static char *hs_type = NULL;
308 static char *hs_check = NULL;
309 static char *hs_virtual_server = NULL;
311 static char *hs_proto = NULL;
315 static CONF_PARSER home_server_coa[] = {
316 { "irt", PW_TYPE_INTEGER,
317 offsetof(home_server_t, coa_irt), 0, STRINGIFY(2) },
318 { "mrt", PW_TYPE_INTEGER,
319 offsetof(home_server_t, coa_mrt), 0, STRINGIFY(16) },
320 { "mrc", PW_TYPE_INTEGER,
321 offsetof(home_server_t, coa_mrc), 0, STRINGIFY(5) },
322 { "mrd", PW_TYPE_INTEGER,
323 offsetof(home_server_t, coa_mrd), 0, STRINGIFY(30) },
325 { NULL, -1, 0, NULL, NULL } /* end the list */
329 static CONF_PARSER home_server_config[] = {
330 { "ipaddr", PW_TYPE_IPADDR,
331 0, &hs_ip4addr, NULL },
332 { "ipv6addr", PW_TYPE_IPV6ADDR,
333 0, &hs_ip6addr, NULL },
334 { "virtual_server", PW_TYPE_STRING_PTR,
335 0, &hs_virtual_server, NULL },
337 { "port", PW_TYPE_INTEGER,
338 offsetof(home_server_t,port), NULL, "0" },
340 { "type", PW_TYPE_STRING_PTR,
344 { "proto", PW_TYPE_STRING_PTR,
345 0, &hs_proto, NULL },
348 { "secret", PW_TYPE_STRING_PTR | PW_TYPE_SECRET,
349 offsetof(home_server_t,secret), NULL, NULL},
351 { "src_ipaddr", PW_TYPE_STRING_PTR,
352 0, &hs_srcipaddr, NULL },
354 { "response_window", PW_TYPE_INTEGER,
355 offsetof(home_server_t,response_window), NULL, "30" },
356 { "max_outstanding", PW_TYPE_INTEGER,
357 offsetof(home_server_t,max_outstanding), NULL, "65536" },
359 { "zombie_period", PW_TYPE_INTEGER,
360 offsetof(home_server_t,zombie_period), NULL, "40" },
361 { "status_check", PW_TYPE_STRING_PTR,
362 0, &hs_check, "none" },
363 { "ping_check", PW_TYPE_STRING_PTR,
364 0, &hs_check, NULL },
366 { "ping_interval", PW_TYPE_INTEGER,
367 offsetof(home_server_t,ping_interval), NULL, "30" },
368 { "check_interval", PW_TYPE_INTEGER,
369 offsetof(home_server_t,ping_interval), NULL, "30" },
370 { "num_answers_to_alive", PW_TYPE_INTEGER,
371 offsetof(home_server_t,num_pings_to_alive), NULL, "3" },
372 { "revive_interval", PW_TYPE_INTEGER,
373 offsetof(home_server_t,revive_interval), NULL, "300" },
374 { "status_check_timeout", PW_TYPE_INTEGER,
375 offsetof(home_server_t,ping_timeout), NULL, "4" },
377 { "username", PW_TYPE_STRING_PTR,
378 offsetof(home_server_t,ping_user_name), NULL, NULL},
379 { "password", PW_TYPE_STRING_PTR,
380 offsetof(home_server_t,ping_user_password), NULL, NULL},
383 { "historic_average_window", PW_TYPE_INTEGER,
384 offsetof(home_server_t,ema.window), NULL, NULL },
388 { "coa", PW_TYPE_SUBSECTION, 0, NULL, (void const *) home_server_coa },
391 { "limit", PW_TYPE_SUBSECTION, 0, NULL, (void const *) limit_config },
393 { NULL, -1, 0, NULL, NULL } /* end the list */
397 static void null_free(UNUSED void *data)
401 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs)
409 hs_virtual_server = NULL;
411 name2 = cf_section_name2(cs);
414 "Home server section is missing a name.");
418 home = talloc_zero(rc, home_server_t);
422 home->state = HOME_STATE_UNKNOWN;
425 * Last packet sent / received are zero.
428 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
429 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
430 if (cf_section_parse(cs, home, home_server_config) < 0) {
435 * Figure out which one to use.
437 if (cf_pair_find(cs, "ipaddr")) {
438 home->ipaddr.af = AF_INET;
439 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
441 } else if (cf_pair_find(cs, "ipv6addr")) {
442 home->ipaddr.af = AF_INET6;
443 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
445 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
446 home->ipaddr.af = AF_UNSPEC;
447 home->server = cf_pair_value(cp);
450 "Invalid value for virtual_server");
454 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
457 "No such server %s", home->server);
462 * When CoA is used, the user has to specify the type
463 * of the home server, even when they point to
466 home->secret = strdup("");
471 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
474 TALLOC_FREE(hs_type);
483 if (!home->port || (home->port > 65535)) {
485 "No port, or invalid port defined for home server %s.",
492 "Fatal error! Home server %s is ourselves!",
498 * Use a reasonable default.
501 if (!hs_type) hs_type = talloc_strdup(cs, "auth+acct");
503 if (strcasecmp(hs_type, "auth") == 0) {
504 home->type = HOME_TYPE_AUTH;
506 } else if (strcasecmp(hs_type, "acct") == 0) {
507 home->type = HOME_TYPE_ACCT;
509 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
510 home->type = HOME_TYPE_AUTH;
514 } else if (strcasecmp(hs_type, "coa") == 0) {
515 home->type = HOME_TYPE_COA;
518 if (home->server != NULL) {
520 "Home servers of type \"coa\" cannot point to a virtual server");
527 "Invalid type \"%s\" for home server %s.",
531 if (hs_type) talloc_free(hs_type);
534 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
535 home->ping_check = HOME_PING_CHECK_NONE;
537 } else if (strcasecmp(hs_check, "status-server") == 0) {
538 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
540 } else if (strcasecmp(hs_check, "request") == 0) {
541 home->ping_check = HOME_PING_CHECK_REQUEST;
543 if (!home->ping_user_name ||
544 !*home->ping_user_name) {
545 cf_log_err_cs(cs, "You must supply a 'username' to enable status_check=request");
549 if ((home->type == HOME_TYPE_AUTH) &&
550 (!home->ping_user_password ||
551 !*home->ping_user_password)) {
552 cf_log_err_cs(cs, "You must supply a password to enable status_check=request");
558 "Invalid status_check \"%s\" for home server %s.",
564 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
565 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
566 if (!home->ping_user_name) {
567 cf_log_err_cs(cs, "You must supply a user name to enable status_check=request");
571 if ((home->type == HOME_TYPE_AUTH) &&
572 !home->ping_user_password) {
573 cf_log_err_cs(cs, "You must supply a password to enable status_check=request");
578 home->proto = IPPROTO_UDP;
581 if (strcmp(hs_proto, "udp") == 0) {
584 } else if (strcmp(hs_proto, "tcp") == 0) {
586 home->proto = IPPROTO_TCP;
588 if (home->ping_check != HOME_PING_CHECK_NONE) {
590 "Only 'status_check = none' is allowed for home servers with 'proto = tcp'");
596 "Unknown proto \"%s\".", hs_proto);
603 rbtree_finddata(home_servers_byaddr, home)) {
604 cf_log_err_cs(cs, "Duplicate home server");
609 * Check the TLS configuration.
611 tls = cf_section_sub_find(cs, "tls");
614 * If were doing RADSEC (tls+tcp) the secret should default
615 * to radsec, else a secret must be set.
619 if (tls && (home->proto == IPPROTO_TCP)) {
620 home->secret = "radsec";
624 cf_log_err_cs(cs, "No shared secret defined for home server %s", name2);
630 * If the home is a virtual server, don't look up source IP.
633 rad_assert(home->ipaddr.af != AF_UNSPEC);
636 * Otherwise look up the source IP using the same
637 * address family as the destination IP.
640 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
641 cf_log_err_cs(cs, "Failed parsing src_ipaddr");
647 * Source isn't specified: Source is
648 * the correct address family, but all zeros.
650 memset(&home->src_ipaddr, 0, sizeof(home->src_ipaddr));
651 home->src_ipaddr.af = home->ipaddr.af;
654 if (tls && (home->proto != IPPROTO_TCP)) {
655 cf_log_err_cs(cs, "TLS transport is not available for UDP sockets.");
662 cf_log_err_cs(cs, "TLS transport is not available in this executable.");
667 * Parse the SSL client configuration.
670 home->tls = tls_client_conf_parse(tls);
678 cf_log_err_cs(cs, "Virtual home_servers cannot have a \"tls\" subsection");
683 * Make sure that this is set.
685 if (home->src_ipaddr.af == AF_UNSPEC) {
686 home->src_ipaddr.af = home->ipaddr.af;
691 if (rbtree_finddata(home_servers_byname, home) != NULL) {
693 "Duplicate home server name %s.", name2);
698 (rbtree_finddata(home_servers_byaddr, home) != NULL)) {
700 "Duplicate home server IP %s.", name2);
704 if (!rbtree_insert(home_servers_byname, home)) {
706 "Internal error %d adding home server %s.",
712 !rbtree_insert(home_servers_byaddr, home)) {
713 rbtree_deletebydata(home_servers_byname, home);
715 "Internal error %d adding home server %s.",
721 home->number = home_server_max_number++;
722 if (!rbtree_insert(home_servers_bynumber, home)) {
723 rbtree_deletebydata(home_servers_byname, home);
724 if (home->ipaddr.af != AF_UNSPEC) {
725 rbtree_deletebydata(home_servers_byname, home);
728 "Internal error %d adding home server %s.",
734 if (home->max_outstanding < 8) home->max_outstanding = 8;
735 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
737 if (home->ping_interval < 6) home->ping_interval = 6;
738 if (home->ping_interval > 120) home->ping_interval = 120;
740 if (home->response_window < 1) home->response_window = 1;
741 if (home->response_window > 60) home->response_window = 60;
742 if (home->response_window > mainconfig.max_request_time) home->response_window = mainconfig.max_request_time;
744 if (home->zombie_period < 1) home->zombie_period = 1;
745 if (home->zombie_period > 120) home->zombie_period = 120;
747 if (home->zombie_period < home->response_window) {
748 home->zombie_period = home->response_window;
751 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
752 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
754 if (home->ping_timeout < 3) home->ping_timeout = 3;
755 if (home->ping_timeout > 10) home->ping_timeout = 10;
757 if (home->revive_interval < 60) home->revive_interval = 60;
758 if (home->revive_interval > 3600) home->revive_interval = 3600;
761 if (home->coa_irt < 1) home->coa_irt = 1;
762 if (home->coa_irt > 5) home->coa_irt = 5;
764 if (home->coa_mrc < 0) home->coa_mrc = 0;
765 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
767 if (home->coa_mrt < 0) home->coa_mrt = 0;
768 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
770 if (home->coa_mrd < 5) home->coa_mrd = 5;
771 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
774 if (home->limit.max_connections > 1024) home->limit.max_connections = 1024;
778 * UDP sockets can't be connection limited.
780 if (home->proto != IPPROTO_TCP) home->limit.max_connections = 0;
783 if ((home->limit.idle_timeout > 0) && (home->limit.idle_timeout < 5))
784 home->limit.idle_timeout = 5;
785 if ((home->limit.lifetime > 0) && (home->limit.lifetime < 5))
786 home->limit.lifetime = 5;
787 if ((home->limit.lifetime > 0) && (home->limit.idle_timeout > home->limit.lifetime))
788 home->limit.idle_timeout = 0;
790 tls = cf_item_parent(cf_sectiontoitem(cs));
791 if (strcmp(cf_section_name1(tls), "server") == 0) {
792 home->parent_server = cf_section_name2(tls);
796 home_server_t *home2 = talloc(rc, home_server_t);
798 memcpy(home2, home, sizeof(*home2));
800 home2->type = HOME_TYPE_ACCT;
802 home2->ping_user_password = NULL;
804 home2->parent_server = home->parent_server;
806 if (!rbtree_insert(home_servers_byname, home2)) {
808 "Internal error %d adding home server %s.",
815 !rbtree_insert(home_servers_byaddr, home2)) {
816 rbtree_deletebydata(home_servers_byname, home2);
818 "Internal error %d adding home server %s.",
825 home2->number = home_server_max_number++;
826 if (!rbtree_insert(home_servers_bynumber, home2)) {
827 rbtree_deletebydata(home_servers_byname, home2);
828 if (!home2->server) {
829 rbtree_deletebydata(home_servers_byname, home2);
832 "Internal error %d adding home server %s.",
841 * Mark it as already processed
843 cf_data_add(cs, "home_server", null_free, null_free);
849 static home_pool_t *server_pool_alloc(char const *name, home_pool_type_t type,
850 int server_type, int num_home_servers)
854 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
856 if (!pool) return NULL; /* just for pairanoia */
858 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
863 pool->server_type = server_type;
864 pool->num_home_servers = num_home_servers;
870 * Ensure any home_server clauses in a home_server_pool section reference
871 * defined home servers, which should already have been created, regardless
872 * of where they appear in the configuration.
874 static int pool_check_home_server(UNUSED realm_config_t *rc, CONF_PAIR *cp,
875 char const *name, int server_type,
876 home_server_t **phome)
878 home_server_t myhome, *home;
882 "No value given for home_server.");
887 myhome.type = server_type;
888 home = rbtree_finddata(home_servers_byname, &myhome);
894 cf_log_err_cp(cp, "Unknown home_server \"%s\".", name);
899 static int server_pool_add(realm_config_t *rc,
900 CONF_SECTION *cs, int server_type, int do_print)
903 home_pool_t *pool = NULL;
906 int num_home_servers;
909 name2 = cf_section_name1(cs);
910 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
911 (strcasecmp(name2, "home_server_pool") != 0))) {
913 "Section is not a home_server_pool.");
917 name2 = cf_section_name2(cs);
920 "Server pool section is missing a name.");
925 * Count the home servers and initalize them.
927 num_home_servers = 0;
928 for (cp = cf_pair_find(cs, "home_server");
930 cp = cf_pair_find_next(cs, cp, "home_server")) {
933 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
934 server_type, &home)) {
939 if (num_home_servers == 0) {
941 "No home servers defined in pool %s",
946 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
949 cf_log_err_cs(cs, "Failed allocating memory for pool");
956 * Fallback servers must be defined, and must be
959 cp = cf_pair_find(cs, "fallback");
962 if (server_type == HOME_TYPE_COA) {
963 cf_log_err_cs(cs, "Home server pools of type \"coa\" cannot have a fallback virtual server.");
968 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
969 server_type, &pool->fallback)) {
974 if (!pool->fallback->server) {
975 cf_log_err_cs(cs, "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
980 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
982 cp = cf_pair_find(cs, "type");
984 static FR_NAME_NUMBER pool_types[] = {
985 { "load-balance", HOME_POOL_LOAD_BALANCE },
987 { "fail-over", HOME_POOL_FAIL_OVER },
988 { "fail_over", HOME_POOL_FAIL_OVER },
990 { "round-robin", HOME_POOL_LOAD_BALANCE },
991 { "round_robin", HOME_POOL_LOAD_BALANCE },
993 { "client-balance", HOME_POOL_CLIENT_BALANCE },
994 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
995 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
999 value = cf_pair_value(cp);
1002 "No value given for type.");
1006 pool->type = fr_str2int(pool_types, value, 0);
1009 "Unknown type \"%s\".",
1014 if (do_print) cf_log_info(cs, "\ttype = %s", value);
1017 cp = cf_pair_find(cs, "virtual_server");
1019 pool->virtual_server = cf_pair_value(cp);
1020 if (!pool->virtual_server) {
1021 cf_log_err_cp(cp, "No value given for virtual_server");
1026 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
1029 if (!cf_section_sub_find_name2(rc->cs, "server",
1030 pool->virtual_server)) {
1031 cf_log_err_cp(cp, "No such server %s",
1032 pool->virtual_server);
1038 num_home_servers = 0;
1039 for (cp = cf_pair_find(cs, "home_server");
1041 cp = cf_pair_find_next(cs, cp, "home_server")) {
1042 home_server_t myhome;
1044 value = cf_pair_value(cp);
1046 memset(&myhome, 0, sizeof(myhome));
1047 myhome.name = value;
1048 myhome.type = server_type;
1050 home = rbtree_finddata(home_servers_byname, &myhome);
1052 DEBUG2("Internal sanity check failed");
1057 WDEBUG2("Duplicate home server %s in server pool %s", home->name, pool->name);
1061 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1062 pool->servers[num_home_servers++] = home;
1063 } /* loop over home_server's */
1065 if (pool->fallback && do_print) {
1066 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1069 if (!rbtree_insert(home_pools_byname, pool)) {
1070 rad_assert("Internal sanity check failed");
1074 if (do_print) cf_log_info(cs, " }");
1076 cf_data_add(cs, "home_server_pool", pool, free);
1078 rad_assert(pool->server_type != 0);
1083 if (do_print) cf_log_info(cs, " }");
1089 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1091 char const *name, char const *secret,
1092 home_pool_type_t ldflag, home_pool_t **pool_p,
1093 int type, char const *server)
1096 int i, insert_point, num_home_servers;
1097 home_server_t myhome, *home;
1098 home_pool_t mypool, *pool;
1099 CONF_SECTION *subcs;
1101 (void) rc; /* -Wunused */
1110 * LOCAL realms get sanity checked, and nothing else happens.
1112 if (strcmp(name, "LOCAL") == 0) {
1114 cf_log_err_cs(cs, "Realm \"%s\" cannot be both LOCAL and remote", name);
1121 return 0; /* Not proxying. Can't do non-LOCAL realms */
1124 mypool.name = realm;
1125 mypool.server_type = type;
1126 pool = rbtree_finddata(home_pools_byname, &mypool);
1128 if (pool->type != ldflag) {
1129 cf_log_err_cs(cs, "Inconsistent ldflag for server pool \"%s\"", name);
1133 if (pool->server_type != type) {
1134 cf_log_err_cs(cs, "Inconsistent home server type for server pool \"%s\"", name);
1141 home = rbtree_finddata(home_servers_byname, &myhome);
1143 if (secret && (strcmp(home->secret, secret) != 0)) {
1144 cf_log_err_cs(cs, "Inconsistent shared secret for home server \"%s\"", name);
1148 if (home->type != type) {
1149 cf_log_err_cs(cs, "Inconsistent type for home server \"%s\"", name);
1154 * See if the home server is already listed
1155 * in the pool. If so, do nothing else.
1157 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1158 if (pool->servers[i] == home) {
1165 * If we do have a pool, check that there is room to
1166 * insert the home server we've found, or the one that we
1169 * Note that we insert it into the LAST available
1170 * position, in order to maintain the same order as in
1171 * the configuration files.
1175 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1176 if (pool->servers[i]) break;
1178 if (!pool->servers[i]) {
1183 if (insert_point < 0) {
1184 cf_log_err_cs(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);
1190 * No home server, allocate one.
1196 home = talloc_zero(rc, home_server_t);
1198 home->hostname = name;
1200 home->secret = secret;
1202 home->proto = IPPROTO_UDP;
1204 p = strchr(name, ':');
1206 if (type == HOME_TYPE_AUTH) {
1207 home->port = PW_AUTH_UDP_PORT;
1209 home->port = PW_ACCT_UDP_PORT;
1215 } else if (p == name) {
1217 "Invalid hostname %s.",
1223 home->port = atoi(p + 1);
1224 if ((home->port == 0) || (home->port > 65535)) {
1232 q = rad_malloc((p - name) + 1);
1233 memcpy(q, name, (p - name));
1239 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1241 "Failed looking up hostname %s.",
1247 home->src_ipaddr.af = home->ipaddr.af;
1249 home->ipaddr.af = AF_UNSPEC;
1250 home->server = server;
1255 * Use the old-style configuration.
1257 home->max_outstanding = 65535*16;
1258 home->zombie_period = rc->retry_delay * rc->retry_count;
1259 if (home->zombie_period == 0) home->zombie_period =30;
1260 home->response_window = home->zombie_period - 1;
1262 home->ping_check = HOME_PING_CHECK_NONE;
1264 home->revive_interval = rc->dead_time;
1266 if (rbtree_finddata(home_servers_byaddr, home)) {
1267 cf_log_err_cs(cs, "Home server %s has the same IP address and/or port as another home server.", name);
1272 if (!rbtree_insert(home_servers_byname, home)) {
1273 cf_log_err_cs(cs, "Internal error %d adding home server %s.", __LINE__, name);
1278 if (!rbtree_insert(home_servers_byaddr, home)) {
1279 rbtree_deletebydata(home_servers_byname, home);
1280 cf_log_err_cs(cs, "Internal error %d adding home server %s.", __LINE__, name);
1286 home->number = home_server_max_number++;
1287 if (!rbtree_insert(home_servers_bynumber, home)) {
1288 rbtree_deletebydata(home_servers_byname, home);
1289 if (home->ipaddr.af != AF_UNSPEC) {
1290 rbtree_deletebydata(home_servers_byname, home);
1293 "Internal error %d adding home server %s.",
1302 * We now have a home server, see if we can insert it
1303 * into pre-existing pool.
1305 if (insert_point >= 0) {
1306 rad_assert(pool != NULL);
1307 pool->servers[insert_point] = home;
1311 rad_assert(pool == NULL);
1312 rad_assert(home != NULL);
1315 * Count the old-style realms of this name.
1317 num_home_servers = 0;
1318 for (subcs = cf_section_find_next(cs, NULL, "realm");
1320 subcs = cf_section_find_next(cs, subcs, "realm")) {
1321 char const *this = cf_section_name2(subcs);
1323 if (!this || (strcmp(this, realm) != 0)) continue;
1327 if (num_home_servers == 0) {
1328 cf_log_err_cs(cs, "Internal error counting pools for home server %s.", name);
1333 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1336 pool->servers[0] = home;
1338 if (!rbtree_insert(home_pools_byname, pool)) {
1339 rad_assert("Internal sanity check failed");
1349 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1352 char const *secret = NULL;
1353 home_pool_type_t ldflag;
1356 cp = cf_pair_find(cs, "ldflag");
1357 ldflag = HOME_POOL_FAIL_OVER;
1359 host = cf_pair_value(cp);
1361 cf_log_err_cp(cp, "No value specified for ldflag");
1365 if (strcasecmp(host, "fail_over") == 0) {
1366 cf_log_info(cs, "\tldflag = fail_over");
1368 } else if (strcasecmp(host, "round_robin") == 0) {
1369 ldflag = HOME_POOL_LOAD_BALANCE;
1370 cf_log_info(cs, "\tldflag = round_robin");
1373 cf_log_err_cs(cs, "Unknown value \"%s\" for ldflag", host);
1376 } /* else don't print it. */
1379 * Allow old-style if it doesn't exist, or if it exists and
1382 cp = cf_pair_find(cs, "authhost");
1384 host = cf_pair_value(cp);
1386 cf_log_err_cp(cp, "No value specified for authhost");
1390 if (strcmp(host, "LOCAL") != 0) {
1391 cp = cf_pair_find(cs, "secret");
1393 cf_log_err_cs(cs, "No shared secret supplied for realm: %s", r->name);
1397 secret = cf_pair_value(cp);
1399 cf_log_err_cp(cp, "No value specified for secret");
1404 cf_log_info(cs, "\tauthhost = %s", host);
1406 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1407 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1412 cp = cf_pair_find(cs, "accthost");
1414 host = cf_pair_value(cp);
1416 cf_log_err_cp(cp, "No value specified for accthost");
1421 * Don't look for a secret again if it was found
1424 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1425 cp = cf_pair_find(cs, "secret");
1427 cf_log_err_cs(cs, "No shared secret supplied for realm: %s", r->name);
1431 secret = cf_pair_value(cp);
1433 cf_log_err_cp(cp, "No value specified for secret");
1438 cf_log_info(cs, "\taccthost = %s", host);
1440 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1441 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1446 cp = cf_pair_find(cs, "virtual_server");
1448 host = cf_pair_value(cp);
1450 cf_log_err_cp(cp, "No value specified for virtual_server");
1454 cf_log_info(cs, "\tvirtual_server = %s", host);
1456 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1457 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1460 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1461 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1466 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1474 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1475 char const *name, home_pool_t **dest,
1476 int server_type, int do_print)
1478 home_pool_t mypool, *pool;
1481 mypool.server_type = server_type;
1483 pool = rbtree_finddata(home_pools_byname, &mypool);
1485 CONF_SECTION *pool_cs;
1487 pool_cs = cf_section_sub_find_name2(rc->cs,
1491 pool_cs = cf_section_sub_find_name2(rc->cs,
1496 cf_log_err_cs(cs, "Failed to find home_server_pool \"%s\"", name);
1500 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1504 pool = rbtree_finddata(home_pools_byname, &mypool);
1506 ERROR("Internal sanity check failed in add_pool_to_realm");
1511 if (pool->server_type != server_type) {
1512 cf_log_err_cs(cs, "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1523 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1529 home_pool_t *auth_pool, *acct_pool;
1530 char const *auth_pool_name, *acct_pool_name;
1532 char const *coa_pool_name;
1533 home_pool_t *coa_pool;
1537 name2 = cf_section_name1(cs);
1538 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1539 cf_log_err_cs(cs, "Section is not a realm.");
1543 name2 = cf_section_name2(cs);
1545 cf_log_err_cs(cs, "Realm section is missing the realm name.");
1550 auth_pool = acct_pool = NULL;
1551 auth_pool_name = acct_pool_name = NULL;
1554 coa_pool_name = NULL;
1558 * Prefer new configuration to old one.
1560 cp = cf_pair_find(cs, "pool");
1561 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1562 if (cp) auth_pool_name = cf_pair_value(cp);
1563 if (cp && auth_pool_name) {
1564 acct_pool_name = auth_pool_name;
1565 if (!add_pool_to_realm(rc, cs,
1566 auth_pool_name, &auth_pool,
1567 HOME_TYPE_AUTH, 1)) {
1570 if (!add_pool_to_realm(rc, cs,
1571 auth_pool_name, &acct_pool,
1572 HOME_TYPE_ACCT, 0)) {
1577 cp = cf_pair_find(cs, "auth_pool");
1578 if (cp) auth_pool_name = cf_pair_value(cp);
1579 if (cp && auth_pool_name) {
1581 cf_log_err_cs(cs, "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1584 if (!add_pool_to_realm(rc, cs,
1585 auth_pool_name, &auth_pool,
1586 HOME_TYPE_AUTH, 1)) {
1591 cp = cf_pair_find(cs, "acct_pool");
1592 if (cp) acct_pool_name = cf_pair_value(cp);
1593 if (cp && acct_pool_name) {
1594 int do_print = true;
1597 cf_log_err_cs(cs, "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1603 (strcmp(auth_pool_name, acct_pool_name) != 0))) {
1607 if (!add_pool_to_realm(rc, cs,
1608 acct_pool_name, &acct_pool,
1609 HOME_TYPE_ACCT, do_print)) {
1615 cp = cf_pair_find(cs, "coa_pool");
1616 if (cp) coa_pool_name = cf_pair_value(cp);
1617 if (cp && coa_pool_name) {
1618 int do_print = true;
1620 if (!add_pool_to_realm(rc, cs,
1621 coa_pool_name, &coa_pool,
1622 HOME_TYPE_COA, do_print)) {
1629 cf_log_info(cs, " realm %s {", name2);
1633 * The realm MAY already exist if it's an old-style realm.
1634 * In that case, merge the old-style realm with this one.
1636 r = realm_find2(name2);
1637 if (r && (strcmp(r->name, name2) == 0)) {
1638 if (cf_pair_find(cs, "auth_pool") ||
1639 cf_pair_find(cs, "acct_pool")) {
1640 cf_log_err_cs(cs, "Duplicate realm \"%s\"", name2);
1644 if (!old_realm_config(rc, cs, r)) {
1648 cf_log_info(cs, " } # realm %s", name2);
1654 if (name2[0] == '~') {
1659 * Include substring matches.
1661 rcode = regcomp(®, name2 + 1, REG_EXTENDED | REG_NOSUB | REG_ICASE);
1665 regerror(rcode, ®, buffer, sizeof(buffer));
1668 "Invalid regex \"%s\": %s",
1676 r = rad_malloc(sizeof(*r));
1677 memset(r, 0, sizeof(*r));
1682 r->auth_pool = auth_pool;
1683 r->acct_pool = acct_pool;
1685 r->coa_pool = coa_pool;
1688 if (auth_pool_name &&
1689 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1690 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1692 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1693 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1695 if (coa_pool_name) cf_log_info(cs, "\tcoa_pool = %s", coa_pool_name);
1700 cp = cf_pair_find(cs, "nostrip");
1701 if (cp && (cf_pair_value(cp) == NULL)) {
1703 cf_log_info(cs, "\tnostrip");
1707 * We're a new-style realm. Complain if we see the old
1710 if (r->auth_pool || r->acct_pool) {
1711 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1712 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1713 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1714 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1715 WDEBUG2("Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1720 * The realm MAY be an old-style realm, as there
1721 * was no auth_pool or acct_pool. Double-check
1722 * it, just to be safe.
1724 } else if (!old_realm_config(rc, cs, r)) {
1730 * It's a regex. Add it to a separate list.
1732 if (name2[0] == '~') {
1733 realm_regex_t *rr, **last;
1735 rr = rad_malloc(sizeof(*rr));
1737 last = &realms_regex;
1738 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1746 cf_log_info(cs, " }");
1751 if (!rbtree_insert(realms_byname, r)) {
1752 rad_assert("Internal sanity check failed");
1756 cf_log_info(cs, " }");
1761 cf_log_info(cs, " } # realm %s", name2);
1767 static const FR_NAME_NUMBER home_server_types[] = {
1768 { "auth", HOME_TYPE_AUTH },
1769 { "auth+acct", HOME_TYPE_AUTH },
1770 { "acct", HOME_TYPE_ACCT },
1771 { "coa", HOME_TYPE_COA },
1775 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1778 char const *name, *type;
1780 CONF_SECTION *server_cs;
1782 cp = cf_pair_find(cs, "home_server");
1784 cf_log_err_cs(cs, "Pool does not contain a \"home_server\" entry");
1785 return HOME_TYPE_INVALID;
1788 name = cf_pair_value(cp);
1790 cf_log_err_cp(cp, "home_server entry does not reference a home server");
1791 return HOME_TYPE_INVALID;
1794 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1796 cf_log_err_cp(cp, "home_server \"%s\" does not exist", name);
1797 return HOME_TYPE_INVALID;
1800 cp = cf_pair_find(server_cs, "type");
1802 cf_log_err_cs(server_cs, "home_server %s does not contain a \"type\" entry", name);
1803 return HOME_TYPE_INVALID;
1806 type = cf_pair_value(cp);
1808 cf_log_err_cs(server_cs, "home_server %s contains an empty \"type\" entry", name);
1809 return HOME_TYPE_INVALID;
1812 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1813 if (home == HOME_TYPE_INVALID) {
1814 cf_log_err_cs(server_cs, "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1815 return HOME_TYPE_INVALID;
1818 return home; /* 'cause we miss it so much */
1822 int realms_init(CONF_SECTION *config)
1826 CONF_SECTION *server_cs;
1828 realm_config_t *rc, *old_rc;
1830 if (realms_byname) return 1;
1832 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1833 if (!realms_byname) {
1839 home_servers_byaddr = rbtree_create(home_server_addr_cmp, home_server_free, 0);
1840 if (!home_servers_byaddr) {
1845 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1846 if (!home_servers_byname) {
1852 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1853 if (!home_servers_bynumber) {
1859 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1860 if (!home_pools_byname) {
1866 rc = talloc_zero(NULL, realm_config_t);
1870 cs = cf_subsection_find_next(config, NULL, "proxy");
1872 if (cf_section_parse(cs, rc, proxy_config) < 0) {
1873 ERROR("Failed parsing proxy section");
1877 * Must be called after realms_free as home_servers
1878 * parented by rc are in trees freed by realms_free()
1884 rc->dead_time = DEAD_TIME;
1885 rc->retry_count = RETRY_COUNT;
1886 rc->retry_delay = RETRY_DELAY;
1888 rc->wake_all_if_all_dead= 0;
1891 for (cs = cf_subsection_find_next(config, NULL, "home_server");
1893 cs = cf_subsection_find_next(config, cs, "home_server")) {
1894 if (!home_server_add(rc, cs)) goto error;
1898 * Loop over virtual servers to find homes which are
1901 for (server_cs = cf_subsection_find_next(config, NULL, "server");
1903 server_cs = cf_subsection_find_next(config, server_cs, "server")) {
1904 for (cs = cf_subsection_find_next(server_cs, NULL, "home_server");
1906 cs = cf_subsection_find_next(server_cs, cs, "home_server")) {
1907 if (!home_server_add(rc, cs)) goto error;
1912 for (cs = cf_subsection_find_next(config, NULL, "realm");
1914 cs = cf_subsection_find_next(config, cs, "realm")) {
1915 if (!realm_add(rc, cs)) goto error;
1920 * CoA pools aren't necessarily tied to realms.
1922 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1924 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1928 * Pool was already loaded.
1930 if (cf_data_find(cs, "home_server_pool")) continue;
1932 type = pool_peek_type(config, cs);
1933 if (type == HOME_TYPE_INVALID) goto error;
1934 if (!server_pool_add(rc, cs, type, true)) goto error;
1940 xlat_register("home_server", xlat_home_server, NULL, NULL);
1941 xlat_register("home_server_pool", xlat_server_pool, NULL, NULL);
1945 * Swap pointers atomically.
1947 old_rc = realm_config;
1949 talloc_free(old_rc);
1955 * Find a realm where "name" might be the regex.
1957 REALM *realm_find2(char const *name)
1962 if (!name) name = "NULL";
1964 myrealm.name = name;
1965 realm = rbtree_finddata(realms_byname, &myrealm);
1966 if (realm) return realm;
1970 realm_regex_t *this;
1972 for (this = realms_regex; this != NULL; this = this->next) {
1973 if (strcmp(this->realm->name, name) == 0) {
1981 * Couldn't find a realm. Look for DEFAULT.
1983 myrealm.name = "DEFAULT";
1984 return rbtree_finddata(realms_byname, &myrealm);
1989 * Find a realm in the REALM list.
1991 REALM *realm_find(char const *name)
1996 if (!name) name = "NULL";
1998 myrealm.name = name;
1999 realm = rbtree_finddata(realms_byname, &myrealm);
2000 if (realm) return realm;
2004 realm_regex_t *this;
2006 for (this = realms_regex; this != NULL; this = this->next) {
2011 * Include substring matches.
2013 if (regcomp(®, this->realm->name + 1, REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
2017 compare = regexec(®, name, 0, NULL, 0);
2020 if (compare == 0) return this->realm;
2026 * Couldn't find a realm. Look for DEFAULT.
2028 myrealm.name = "DEFAULT";
2029 return rbtree_finddata(realms_byname, &myrealm);
2036 * Allocate the proxy list if it doesn't already exist, and copy request
2037 * VPs into it. Setup src/dst IP addresses based on home server, and
2038 * calculate and add the message-authenticator.
2040 * This is a distinct function from home_server_ldb, as not all home_server_t
2041 * lookups result in the *CURRENT* request being proxied,
2042 * as in rlm_replicate, and this may trigger asserts elsewhere in the
2045 void home_server_update_request(home_server_t *home, REQUEST *request)
2049 * Allocate the proxy packet, only if it wasn't
2050 * already allocated by a module. This check is
2051 * mainly to support the proxying of EAP-TTLS and
2052 * EAP-PEAP tunneled requests.
2054 * In those cases, the EAP module creates a
2055 * "fake" request, and recursively passes it
2056 * through the authentication stage of the
2057 * server. The module then checks if the request
2058 * was supposed to be proxied, and if so, creates
2059 * a proxy packet from the TUNNELED request, and
2060 * not from the EAP request outside of the
2063 * The proxy then works like normal, except that
2064 * the response packet is "eaten" by the EAP
2065 * module, and encapsulated into an EAP packet.
2067 if (!request->proxy) {
2068 request->proxy = rad_alloc(request, true);
2069 if (!request->proxy) {
2076 * Copy the request, then look up name
2077 * and plain-text password in the copy.
2079 * Note that the User-Name attribute is
2080 * the *original* as sent over by the
2081 * client. The Stripped-User-Name
2082 * attribute is the one hacked through
2085 request->proxy->vps = paircopy(request->proxy,
2086 request->packet->vps);
2090 * Update the various fields as appropriate.
2092 request->proxy->src_ipaddr = home->src_ipaddr;
2093 request->proxy->src_port = 0;
2094 request->proxy->dst_ipaddr = home->ipaddr;
2095 request->proxy->dst_port = home->port;
2096 request->home_server = home;
2099 * Access-Requests have a Message-Authenticator added,
2100 * unless one already exists.
2102 if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2103 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY)) {
2104 pairmake(request->proxy, &request->proxy->vps,
2105 "Message-Authenticator", "0x00",
2110 home_server_t *home_server_ldb(char const *realmname,
2111 home_pool_t *pool, REQUEST *request)
2115 home_server_t *found = NULL;
2116 home_server_t *zombie = NULL;
2120 * Determine how to pick choose the home server.
2122 switch (pool->type) {
2126 * For load-balancing by client IP address, we
2127 * pick a home server by hashing the client IP.
2129 * This isn't as even a load distribution as
2130 * tracking the State attribute, but it's better
2133 case HOME_POOL_CLIENT_BALANCE:
2134 switch (request->packet->src_ipaddr.af) {
2136 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2137 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2140 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2141 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2147 start = hash % pool->num_home_servers;
2150 case HOME_POOL_CLIENT_PORT_BALANCE:
2151 switch (request->packet->src_ipaddr.af) {
2153 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2154 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2157 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2158 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2164 fr_hash_update(&request->packet->src_port,
2165 sizeof(request->packet->src_port), hash);
2166 start = hash % pool->num_home_servers;
2169 case HOME_POOL_KEYED_BALANCE:
2170 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY, 0, TAG_ANY)) != NULL) {
2171 hash = fr_hash(vp->vp_strvalue, vp->length);
2172 start = hash % pool->num_home_servers;
2177 case HOME_POOL_LOAD_BALANCE:
2178 case HOME_POOL_FAIL_OVER:
2182 default: /* this shouldn't happen... */
2189 * Starting with the home server we chose, loop through
2190 * all home servers. If the current one is dead, skip
2191 * it. If it is too busy, skip it.
2193 * Otherwise, use it.
2195 for (count = 0; count < pool->num_home_servers; count++) {
2196 home_server_t *home = pool->servers[(start + count) % pool->num_home_servers];
2198 if (!home) continue;
2201 * Skip dead home servers.
2203 * Home servers that are unknown, alive, or zombie
2204 * are used for proxying.
2206 if (home->state == HOME_STATE_IS_DEAD) {
2211 * This home server is too busy. Choose another one.
2213 if (home->currently_outstanding >= home->max_outstanding) {
2219 * We read the packet from a detail file, AND it
2220 * came from this server. Don't re-proxy it
2223 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2224 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
2225 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2231 * Default virtual: ignore homes tied to a
2234 if (!request->server && home->parent_server) {
2239 * A virtual AND home is tied to virtual,
2240 * ignore ones which don't match.
2242 if (request->server && home->parent_server &&
2243 strcmp(request->server, home->parent_server) != 0) {
2248 * Allow request->server && !home->parent_server
2250 * i.e. virtuals can proxy to globally defined
2255 * It's zombie, so we remember the first zombie
2256 * we find, but we don't mark it as a "live"
2259 if (home->state == HOME_STATE_ZOMBIE) {
2260 if (!zombie) zombie = home;
2265 * We've found the first "live" one. Use that.
2267 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2273 * Otherwise we're doing some kind of load balancing.
2274 * If we haven't found one yet, pick this one.
2281 RDEBUG3("PROXY %s %d\t%s %d",
2282 found->name, found->currently_outstanding,
2283 home->name, home->currently_outstanding);
2286 * Prefer this server if it's less busy than the
2287 * one we had previously found.
2289 if (home->currently_outstanding < found->currently_outstanding) {
2290 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2291 home->name, found->name);
2297 * Ignore servers which are busier than the one
2300 if (home->currently_outstanding > found->currently_outstanding) {
2301 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2302 home->name, found->name);
2307 * From the list of servers which have the same
2308 * load, choose one at random.
2310 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2313 } /* loop over the home servers */
2316 * We have no live servers, BUT we have a zombie. Use
2317 * the zombie as a last resort.
2319 if (!found && zombie) {
2325 * There's a fallback if they're all dead.
2327 if (!found && pool->fallback) {
2328 found = pool->fallback;
2330 WDEBUG("Home server pool %s failing over to fallback %s",
2331 pool->name, found->server);
2332 if (pool->in_fallback) goto update_and_return;
2334 pool->in_fallback = true;
2337 * Run the trigger once an hour saying that
2340 if ((pool->time_all_dead + 3600) < request->timestamp) {
2341 pool->time_all_dead = request->timestamp;
2342 exec_trigger(request, pool->cs, "home_server_pool.fallback", false);
2348 if ((found != pool->fallback) && pool->in_fallback) {
2349 pool->in_fallback = false;
2350 exec_trigger(request, pool->cs, "home_server_pool.normal", false);
2357 * No live match found, and no fallback to the "DEFAULT"
2358 * realm. We fix this by blindly marking all servers as
2359 * "live". But only do it for ones that don't support
2360 * "pings", as they will be marked live when they
2361 * actually are live.
2363 if (!realm_config->fallback &&
2364 realm_config->wake_all_if_all_dead) {
2365 for (count = 0; count < pool->num_home_servers; count++) {
2366 home_server_t *home = pool->servers[count];
2368 if (!home) continue;
2370 if ((home->state == HOME_STATE_IS_DEAD) &&
2371 (home->ping_check == HOME_PING_CHECK_NONE)) {
2372 home->state = HOME_STATE_ALIVE;
2373 if (!found) found = home;
2377 if (found) goto update_and_return;
2381 * Still nothing. Look up the DEFAULT realm, but only
2382 * if we weren't looking up the NULL or DEFAULT realms.
2384 if (realm_config->fallback &&
2386 (strcmp(realmname, "NULL") != 0) &&
2387 (strcmp(realmname, "DEFAULT") != 0)) {
2388 REALM *rd = realm_find("DEFAULT");
2390 if (!rd) return NULL;
2393 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2394 pool = rd->auth_pool;
2396 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2397 pool = rd->acct_pool;
2399 if (!pool) return NULL;
2401 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2402 return home_server_ldb(rd->name, pool, request);
2406 * Still haven't found anything. Oh well.
2412 home_server_t *home_server_find(fr_ipaddr_t *ipaddr, int port, int proto)
2414 home_server_t myhome;
2416 memset(&myhome, 0, sizeof(myhome));
2417 myhome.ipaddr = *ipaddr;
2418 myhome.src_ipaddr.af = ipaddr->af;
2421 myhome.proto = proto;
2423 myhome.proto = IPPROTO_UDP;
2425 myhome.server = NULL; /* we're not called for internal proxying */
2427 return rbtree_finddata(home_servers_byaddr, &myhome);
2431 home_server_t *home_server_byname(char const *name, int type)
2433 home_server_t myhome;
2435 memset(&myhome, 0, sizeof(myhome));
2439 return rbtree_finddata(home_servers_byname, &myhome);
2444 home_server_t *home_server_bynumber(int number)
2446 home_server_t myhome;
2448 memset(&myhome, 0, sizeof(myhome));
2449 myhome.number = number;
2450 myhome.server = NULL; /* we're not called for internal proxying */
2452 return rbtree_finddata(home_servers_bynumber, &myhome);
2456 home_pool_t *home_pool_byname(char const *name, int type)
2460 memset(&mypool, 0, sizeof(mypool));
2462 mypool.server_type = type;
2463 return rbtree_finddata(home_pools_byname, &mypool);