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;
112 static int home_server_name_cmp(void const *one, void const *two)
114 home_server_t const *a = one;
115 home_server_t const *b = two;
117 if (a->type < b->type) return -1;
118 if (a->type > b->type) return +1;
120 return strcasecmp(a->name, b->name);
123 static int home_server_addr_cmp(void const *one, void const *two)
126 home_server_t const *a = one;
127 home_server_t const *b = two;
129 if (a->server && !b->server) return -1;
130 if (!a->server && b->server) return +1;
131 if (a->server && b->server) {
132 rcode = a->type - b->type;
133 if (rcode != 0) return rcode;
134 return strcmp(a->server, b->server);
137 if (a->port < b->port) return -1;
138 if (a->port > b->port) return +1;
141 if (a->proto < b->proto) return -1;
142 if (a->proto > b->proto) return +1;
145 rcode = fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
146 if (rcode != 0) return rcode;
148 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
152 static int home_server_number_cmp(void const *one, void const *two)
154 home_server_t const *a = one;
155 home_server_t const *b = two;
157 return (a->number - b->number);
161 static int home_pool_name_cmp(void const *one, void const *two)
163 home_pool_t const *a = one;
164 home_pool_t const *b = two;
166 if (a->server_type < b->server_type) return -1;
167 if (a->server_type > b->server_type) return +1;
169 return strcasecmp(a->name, b->name);
173 static size_t xlat_cs(CONF_SECTION *cs, char const *fmt, char *out, size_t outlen)
176 char const *value = NULL;
181 if (strcmp(fmt, "instance") == 0) {
182 value = cf_section_name2(cs);
190 cp = cf_pair_find(cs, fmt);
191 if (!cp || !(value = cf_pair_value(cp))) {
197 strlcpy(out, value, outlen);
204 * Xlat for %{home_server:foo}
206 static ssize_t xlat_home_server(UNUSED void *instance, REQUEST *request,
207 char const *fmt, char *out, size_t outlen)
209 if (!fmt || !out || (outlen < 1)) return 0;
211 if (!request || !request->home_server) {
212 RWDEBUG("No home_server associated with this request");
218 return xlat_cs(request->home_server->cs, fmt, out, outlen);
223 * Xlat for %{home_server_pool:foo}
225 static ssize_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
226 char const *fmt, char *out, size_t outlen)
228 if (!fmt || !out || (outlen < 1)) return 0;
230 if (!request || !request->home_pool) {
231 RWDEBUG("No home_pool associated with this request");
237 return xlat_cs(request->home_pool->cs, fmt, out, outlen);
241 void realms_free(void)
245 rbtree_free(home_servers_bynumber);
246 home_servers_bynumber = NULL;
249 rbtree_free(home_servers_byname);
250 home_servers_byname = NULL;
252 rbtree_free(home_servers_byaddr);
253 home_servers_byaddr = NULL;
255 rbtree_free(home_pools_byname);
256 home_pools_byname = NULL;
259 rbtree_free(realms_byname);
260 realms_byname = NULL;
264 realm_regex_t *this, *next;
266 for (this = realms_regex; this != NULL; this = next) {
275 talloc_free(realm_config);
281 static CONF_PARSER limit_config[] = {
282 { "max_connections", PW_TYPE_INTEGER,
283 offsetof(home_server_t, limit.max_connections), NULL, "16" },
285 { "max_requests", PW_TYPE_INTEGER,
286 offsetof(home_server_t, limit.max_requests), NULL, "0" },
288 { "lifetime", PW_TYPE_INTEGER,
289 offsetof(home_server_t, limit.lifetime), NULL, "0" },
291 { "idle_timeout", PW_TYPE_INTEGER,
292 offsetof(home_server_t, limit.idle_timeout), NULL, "0" },
294 { NULL, -1, 0, NULL, NULL } /* end the list */
297 static struct in_addr hs_ip4addr;
298 static struct in6_addr hs_ip6addr;
299 static char *hs_srcipaddr = NULL;
300 static char *hs_type = NULL;
301 static char *hs_check = NULL;
302 static char *hs_virtual_server = NULL;
304 static char *hs_proto = NULL;
308 static CONF_PARSER home_server_coa[] = {
309 { "irt", PW_TYPE_INTEGER,
310 offsetof(home_server_t, coa_irt), 0, STRINGIFY(2) },
311 { "mrt", PW_TYPE_INTEGER,
312 offsetof(home_server_t, coa_mrt), 0, STRINGIFY(16) },
313 { "mrc", PW_TYPE_INTEGER,
314 offsetof(home_server_t, coa_mrc), 0, STRINGIFY(5) },
315 { "mrd", PW_TYPE_INTEGER,
316 offsetof(home_server_t, coa_mrd), 0, STRINGIFY(30) },
318 { NULL, -1, 0, NULL, NULL } /* end the list */
322 static CONF_PARSER home_server_config[] = {
323 { "ipaddr", PW_TYPE_IPADDR,
324 0, &hs_ip4addr, NULL },
325 { "ipv6addr", PW_TYPE_IPV6ADDR,
326 0, &hs_ip6addr, NULL },
327 { "virtual_server", PW_TYPE_STRING_PTR,
328 0, &hs_virtual_server, NULL },
330 { "port", PW_TYPE_INTEGER,
331 offsetof(home_server_t,port), NULL, "0" },
333 { "type", PW_TYPE_STRING_PTR,
337 { "proto", PW_TYPE_STRING_PTR,
338 0, &hs_proto, NULL },
341 { "secret", PW_TYPE_STRING_PTR,
342 offsetof(home_server_t,secret), NULL, NULL},
344 { "src_ipaddr", PW_TYPE_STRING_PTR,
345 0, &hs_srcipaddr, NULL },
347 { "response_window", PW_TYPE_INTEGER,
348 offsetof(home_server_t,response_window), NULL, "30" },
349 { "max_outstanding", PW_TYPE_INTEGER,
350 offsetof(home_server_t,max_outstanding), NULL, "65536" },
352 { "zombie_period", PW_TYPE_INTEGER,
353 offsetof(home_server_t,zombie_period), NULL, "40" },
354 { "status_check", PW_TYPE_STRING_PTR,
355 0, &hs_check, "none" },
356 { "ping_check", PW_TYPE_STRING_PTR,
357 0, &hs_check, NULL },
359 { "ping_interval", PW_TYPE_INTEGER,
360 offsetof(home_server_t,ping_interval), NULL, "30" },
361 { "check_interval", PW_TYPE_INTEGER,
362 offsetof(home_server_t,ping_interval), NULL, "30" },
363 { "num_answers_to_alive", PW_TYPE_INTEGER,
364 offsetof(home_server_t,num_pings_to_alive), NULL, "3" },
365 { "revive_interval", PW_TYPE_INTEGER,
366 offsetof(home_server_t,revive_interval), NULL, "300" },
367 { "status_check_timeout", PW_TYPE_INTEGER,
368 offsetof(home_server_t,ping_timeout), NULL, "4" },
370 { "username", PW_TYPE_STRING_PTR,
371 offsetof(home_server_t,ping_user_name), NULL, NULL},
372 { "password", PW_TYPE_STRING_PTR,
373 offsetof(home_server_t,ping_user_password), NULL, NULL},
376 { "historic_average_window", PW_TYPE_INTEGER,
377 offsetof(home_server_t,ema.window), NULL, NULL },
381 { "coa", PW_TYPE_SUBSECTION, 0, NULL, (void const *) home_server_coa },
384 { "limit", PW_TYPE_SUBSECTION, 0, NULL, (void const *) limit_config },
386 { NULL, -1, 0, NULL, NULL } /* end the list */
390 static void null_free(UNUSED void *data)
394 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs)
402 hs_virtual_server = NULL;
404 name2 = cf_section_name2(cs);
407 "Home server section is missing a name.");
411 home = talloc_zero(rc, home_server_t);
415 home->state = HOME_STATE_UNKNOWN;
418 * Last packet sent / received are zero.
421 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
422 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
423 if (cf_section_parse(cs, home, home_server_config) < 0) {
428 * Figure out which one to use.
430 if (cf_pair_find(cs, "ipaddr")) {
431 home->ipaddr.af = AF_INET;
432 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
434 } else if (cf_pair_find(cs, "ipv6addr")) {
435 home->ipaddr.af = AF_INET6;
436 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
438 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
439 home->ipaddr.af = AF_UNSPEC;
440 home->server = cf_pair_value(cp);
443 "Invalid value for virtual_server");
447 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
450 "No such server %s", home->server);
455 * When CoA is used, the user has to specify the type
456 * of the home server, even when they point to
459 home->secret = strdup("");
464 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
467 TALLOC_FREE(hs_type);
476 if (!home->port || (home->port > 65535)) {
478 "No port, or invalid port defined for home server %s.",
485 "Fatal error! Home server %s is ourselves!",
491 * Use a reasonable default.
494 if (!hs_type) hs_type = talloc_strdup(cs, "auth+acct");
496 if (strcasecmp(hs_type, "auth") == 0) {
497 home->type = HOME_TYPE_AUTH;
499 } else if (strcasecmp(hs_type, "acct") == 0) {
500 home->type = HOME_TYPE_ACCT;
502 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
503 home->type = HOME_TYPE_AUTH;
507 } else if (strcasecmp(hs_type, "coa") == 0) {
508 home->type = HOME_TYPE_COA;
511 if (home->server != NULL) {
513 "Home servers of type \"coa\" cannot point to a virtual server");
520 "Invalid type \"%s\" for home server %s.",
524 if (hs_type) talloc_free(hs_type);
527 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
528 home->ping_check = HOME_PING_CHECK_NONE;
530 } else if (strcasecmp(hs_check, "status-server") == 0) {
531 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
533 } else if (strcasecmp(hs_check, "request") == 0) {
534 home->ping_check = HOME_PING_CHECK_REQUEST;
536 if (!home->ping_user_name ||
537 !*home->ping_user_name) {
538 cf_log_err_cs(cs, "You must supply a 'username' to enable status_check=request");
542 if ((home->type == HOME_TYPE_AUTH) &&
543 (!home->ping_user_password ||
544 !*home->ping_user_password)) {
545 cf_log_err_cs(cs, "You must supply a password to enable status_check=request");
551 "Invalid status_check \"%s\" for home server %s.",
557 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
558 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
559 if (!home->ping_user_name) {
560 cf_log_err_cs(cs, "You must supply a user name to enable status_check=request");
564 if ((home->type == HOME_TYPE_AUTH) &&
565 !home->ping_user_password) {
566 cf_log_err_cs(cs, "You must supply a password to enable status_check=request");
571 home->proto = IPPROTO_UDP;
574 if (strcmp(hs_proto, "udp") == 0) {
577 } else if (strcmp(hs_proto, "tcp") == 0) {
579 home->proto = IPPROTO_TCP;
581 if (home->ping_check != HOME_PING_CHECK_NONE) {
583 "Only 'status_check = none' is allowed for home servers with 'proto = tcp'");
589 "Unknown proto \"%s\".", hs_proto);
596 rbtree_finddata(home_servers_byaddr, home)) {
597 cf_log_err_cs(cs, "Duplicate home server");
602 * Check the TLS configuration.
604 tls = cf_section_sub_find(cs, "tls");
607 * If were doing RADSEC (tls+tcp) the secret should default
608 * to radsec, else a secret must be set.
611 if (!tls || (home->proto != IPPROTO_TCP)) {
612 cf_log_err_cs(cs, "No shared secret defined for home server %s", name2);
616 home->secret = "radsec";
620 * If the home is a virtual server, don't look up source IP.
623 rad_assert(home->ipaddr.af != AF_UNSPEC);
626 * Otherwise look up the source IP using the same
627 * address family as the destination IP.
630 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
631 cf_log_err_cs(cs, "Failed parsing src_ipaddr");
637 * Source isn't specified: Source is
638 * the correct address family, but all zeros.
640 memset(&home->src_ipaddr, 0, sizeof(home->src_ipaddr));
641 home->src_ipaddr.af = home->ipaddr.af;
644 if (tls && (home->proto != IPPROTO_TCP)) {
645 cf_log_err_cs(cs, "TLS transport is not available for UDP sockets.");
652 cf_log_err_cs(cs, "TLS transport is not available in this executable.");
657 * Parse the SSL client configuration.
660 home->tls = tls_client_conf_parse(tls);
668 cf_log_err_cs(cs, "Virtual home_servers cannot have a \"tls\" subsection");
673 * Make sure that this is set.
675 if (home->src_ipaddr.af == AF_UNSPEC) {
676 home->src_ipaddr.af = home->ipaddr.af;
681 if (rbtree_finddata(home_servers_byname, home) != NULL) {
683 "Duplicate home server name %s.", name2);
688 (rbtree_finddata(home_servers_byaddr, home) != NULL)) {
690 "Duplicate home server IP %s.", name2);
694 if (!rbtree_insert(home_servers_byname, home)) {
696 "Internal error %d adding home server %s.",
702 !rbtree_insert(home_servers_byaddr, home)) {
703 rbtree_deletebydata(home_servers_byname, home);
705 "Internal error %d adding home server %s.",
711 home->number = home_server_max_number++;
712 if (!rbtree_insert(home_servers_bynumber, home)) {
713 rbtree_deletebydata(home_servers_byname, home);
714 if (home->ipaddr.af != AF_UNSPEC) {
715 rbtree_deletebydata(home_servers_byname, home);
718 "Internal error %d adding home server %s.",
724 if (home->max_outstanding < 8) home->max_outstanding = 8;
725 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
727 if (home->ping_interval < 6) home->ping_interval = 6;
728 if (home->ping_interval > 120) home->ping_interval = 120;
730 if (home->response_window < 1) home->response_window = 1;
731 if (home->response_window > 60) home->response_window = 60;
732 if (home->response_window > mainconfig.max_request_time) home->response_window = mainconfig.max_request_time;
734 if (home->zombie_period < 1) home->zombie_period = 1;
735 if (home->zombie_period > 120) home->zombie_period = 120;
737 if (home->zombie_period < home->response_window) {
738 home->zombie_period = home->response_window;
741 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
742 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
744 if (home->ping_timeout < 3) home->ping_timeout = 3;
745 if (home->ping_timeout > 10) home->ping_timeout = 10;
747 if (home->revive_interval < 60) home->revive_interval = 60;
748 if (home->revive_interval > 3600) home->revive_interval = 3600;
751 if (home->coa_irt < 1) home->coa_irt = 1;
752 if (home->coa_irt > 5) home->coa_irt = 5;
754 if (home->coa_mrc < 0) home->coa_mrc = 0;
755 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
757 if (home->coa_mrt < 0) home->coa_mrt = 0;
758 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
760 if (home->coa_mrd < 5) home->coa_mrd = 5;
761 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
764 if (home->limit.max_connections > 1024) home->limit.max_connections = 1024;
768 * UDP sockets can't be connection limited.
770 if (home->proto != IPPROTO_TCP) home->limit.max_connections = 0;
773 if ((home->limit.idle_timeout > 0) && (home->limit.idle_timeout < 5))
774 home->limit.idle_timeout = 5;
775 if ((home->limit.lifetime > 0) && (home->limit.lifetime < 5))
776 home->limit.lifetime = 5;
777 if ((home->limit.lifetime > 0) && (home->limit.idle_timeout > home->limit.lifetime))
778 home->limit.idle_timeout = 0;
780 tls = cf_item_parent(cf_sectiontoitem(cs));
781 if (strcmp(cf_section_name1(tls), "server") == 0) {
782 home->parent_server = cf_section_name2(tls);
786 home_server_t *home2 = talloc(rc, home_server_t);
788 memcpy(home2, home, sizeof(*home2));
790 home2->type = HOME_TYPE_ACCT;
792 home2->ping_user_password = NULL;
794 home2->parent_server = home->parent_server;
796 if (!rbtree_insert(home_servers_byname, home2)) {
798 "Internal error %d adding home server %s.",
805 !rbtree_insert(home_servers_byaddr, home2)) {
806 rbtree_deletebydata(home_servers_byname, home2);
808 "Internal error %d adding home server %s.",
815 home2->number = home_server_max_number++;
816 if (!rbtree_insert(home_servers_bynumber, home2)) {
817 rbtree_deletebydata(home_servers_byname, home2);
818 if (!home2->server) {
819 rbtree_deletebydata(home_servers_byname, home2);
822 "Internal error %d adding home server %s.",
831 * Mark it as already processed
833 cf_data_add(cs, "home_server", null_free, null_free);
839 static home_pool_t *server_pool_alloc(char const *name, home_pool_type_t type,
840 int server_type, int num_home_servers)
844 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
846 if (!pool) return NULL; /* just for pairanoia */
848 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
853 pool->server_type = server_type;
854 pool->num_home_servers = num_home_servers;
860 * Ensure any home_server clauses in a home_server_pool section reference
861 * defined home servers, which should already have been created, regardless
862 * of where they appear in the configuration.
864 static int pool_check_home_server(UNUSED realm_config_t *rc, CONF_PAIR *cp,
865 char const *name, int server_type,
866 home_server_t **phome)
868 home_server_t myhome, *home;
872 "No value given for home_server.");
877 myhome.type = server_type;
878 home = rbtree_finddata(home_servers_byname, &myhome);
884 cf_log_err_cp(cp, "Unknown home_server \"%s\".", name);
889 static int server_pool_add(realm_config_t *rc,
890 CONF_SECTION *cs, int server_type, int do_print)
893 home_pool_t *pool = NULL;
896 int num_home_servers;
899 name2 = cf_section_name1(cs);
900 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
901 (strcasecmp(name2, "home_server_pool") != 0))) {
903 "Section is not a home_server_pool.");
907 name2 = cf_section_name2(cs);
910 "Server pool section is missing a name.");
915 * Count the home servers and initalize them.
917 num_home_servers = 0;
918 for (cp = cf_pair_find(cs, "home_server");
920 cp = cf_pair_find_next(cs, cp, "home_server")) {
923 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
924 server_type, &home)) {
929 if (num_home_servers == 0) {
931 "No home servers defined in pool %s",
936 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
939 cf_log_err_cs(cs, "Failed allocating memory for pool");
946 * Fallback servers must be defined, and must be
949 cp = cf_pair_find(cs, "fallback");
952 if (server_type == HOME_TYPE_COA) {
953 cf_log_err_cs(cs, "Home server pools of type \"coa\" cannot have a fallback virtual server.");
958 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
959 server_type, &pool->fallback)) {
964 if (!pool->fallback->server) {
965 cf_log_err_cs(cs, "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
970 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
972 cp = cf_pair_find(cs, "type");
974 static FR_NAME_NUMBER pool_types[] = {
975 { "load-balance", HOME_POOL_LOAD_BALANCE },
977 { "fail-over", HOME_POOL_FAIL_OVER },
978 { "fail_over", HOME_POOL_FAIL_OVER },
980 { "round-robin", HOME_POOL_LOAD_BALANCE },
981 { "round_robin", HOME_POOL_LOAD_BALANCE },
983 { "client-balance", HOME_POOL_CLIENT_BALANCE },
984 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
985 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
989 value = cf_pair_value(cp);
992 "No value given for type.");
996 pool->type = fr_str2int(pool_types, value, 0);
999 "Unknown type \"%s\".",
1004 if (do_print) cf_log_info(cs, "\ttype = %s", value);
1007 cp = cf_pair_find(cs, "virtual_server");
1009 pool->virtual_server = cf_pair_value(cp);
1010 if (!pool->virtual_server) {
1011 cf_log_err_cp(cp, "No value given for virtual_server");
1016 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
1019 if (!cf_section_sub_find_name2(rc->cs, "server",
1020 pool->virtual_server)) {
1021 cf_log_err_cp(cp, "No such server %s",
1022 pool->virtual_server);
1028 num_home_servers = 0;
1029 for (cp = cf_pair_find(cs, "home_server");
1031 cp = cf_pair_find_next(cs, cp, "home_server")) {
1032 home_server_t myhome;
1034 value = cf_pair_value(cp);
1036 memset(&myhome, 0, sizeof(myhome));
1037 myhome.name = value;
1038 myhome.type = server_type;
1040 home = rbtree_finddata(home_servers_byname, &myhome);
1042 DEBUG2("Internal sanity check failed");
1047 WDEBUG2("Duplicate home server %s in server pool %s", home->name, pool->name);
1051 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1052 pool->servers[num_home_servers++] = home;
1053 } /* loop over home_server's */
1055 if (pool->fallback && do_print) {
1056 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1059 if (!rbtree_insert(home_pools_byname, pool)) {
1060 rad_assert("Internal sanity check failed");
1064 if (do_print) cf_log_info(cs, " }");
1066 cf_data_add(cs, "home_server_pool", pool, free);
1068 rad_assert(pool->server_type != 0);
1073 if (do_print) cf_log_info(cs, " }");
1079 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1081 char const *name, char const *secret,
1082 home_pool_type_t ldflag, home_pool_t **pool_p,
1083 int type, char const *server)
1086 int i, insert_point, num_home_servers;
1087 home_server_t myhome, *home;
1088 home_pool_t mypool, *pool;
1089 CONF_SECTION *subcs;
1091 (void) rc; /* -Wunused */
1100 * LOCAL realms get sanity checked, and nothing else happens.
1102 if (strcmp(name, "LOCAL") == 0) {
1104 cf_log_err_cs(cs, "Realm \"%s\" cannot be both LOCAL and remote", name);
1111 return 0; /* Not proxying. Can't do non-LOCAL realms */
1114 mypool.name = realm;
1115 mypool.server_type = type;
1116 pool = rbtree_finddata(home_pools_byname, &mypool);
1118 if (pool->type != ldflag) {
1119 cf_log_err_cs(cs, "Inconsistent ldflag for server pool \"%s\"", name);
1123 if (pool->server_type != type) {
1124 cf_log_err_cs(cs, "Inconsistent home server type for server pool \"%s\"", name);
1131 home = rbtree_finddata(home_servers_byname, &myhome);
1133 if (secret && (strcmp(home->secret, secret) != 0)) {
1134 cf_log_err_cs(cs, "Inconsistent shared secret for home server \"%s\"", name);
1138 if (home->type != type) {
1139 cf_log_err_cs(cs, "Inconsistent type for home server \"%s\"", name);
1144 * See if the home server is already listed
1145 * in the pool. If so, do nothing else.
1147 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1148 if (pool->servers[i] == home) {
1155 * If we do have a pool, check that there is room to
1156 * insert the home server we've found, or the one that we
1159 * Note that we insert it into the LAST available
1160 * position, in order to maintain the same order as in
1161 * the configuration files.
1165 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1166 if (pool->servers[i]) break;
1168 if (!pool->servers[i]) {
1173 if (insert_point < 0) {
1174 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);
1180 * No home server, allocate one.
1186 home = talloc_zero(rc, home_server_t);
1188 home->hostname = name;
1190 home->secret = secret;
1192 home->proto = IPPROTO_UDP;
1194 p = strchr(name, ':');
1196 if (type == HOME_TYPE_AUTH) {
1197 home->port = PW_AUTH_UDP_PORT;
1199 home->port = PW_ACCT_UDP_PORT;
1205 } else if (p == name) {
1207 "Invalid hostname %s.",
1213 home->port = atoi(p + 1);
1214 if ((home->port == 0) || (home->port > 65535)) {
1222 q = rad_malloc((p - name) + 1);
1223 memcpy(q, name, (p - name));
1229 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1231 "Failed looking up hostname %s.",
1237 home->src_ipaddr.af = home->ipaddr.af;
1239 home->ipaddr.af = AF_UNSPEC;
1240 home->server = server;
1245 * Use the old-style configuration.
1247 home->max_outstanding = 65535*16;
1248 home->zombie_period = rc->retry_delay * rc->retry_count;
1249 if (home->zombie_period == 0) home->zombie_period =30;
1250 home->response_window = home->zombie_period - 1;
1252 home->ping_check = HOME_PING_CHECK_NONE;
1254 home->revive_interval = rc->dead_time;
1256 if (rbtree_finddata(home_servers_byaddr, home)) {
1257 cf_log_err_cs(cs, "Home server %s has the same IP address and/or port as another home server.", name);
1262 if (!rbtree_insert(home_servers_byname, home)) {
1263 cf_log_err_cs(cs, "Internal error %d adding home server %s.", __LINE__, name);
1268 if (!rbtree_insert(home_servers_byaddr, home)) {
1269 rbtree_deletebydata(home_servers_byname, home);
1270 cf_log_err_cs(cs, "Internal error %d adding home server %s.", __LINE__, name);
1276 home->number = home_server_max_number++;
1277 if (!rbtree_insert(home_servers_bynumber, home)) {
1278 rbtree_deletebydata(home_servers_byname, home);
1279 if (home->ipaddr.af != AF_UNSPEC) {
1280 rbtree_deletebydata(home_servers_byname, home);
1283 "Internal error %d adding home server %s.",
1292 * We now have a home server, see if we can insert it
1293 * into pre-existing pool.
1295 if (insert_point >= 0) {
1296 rad_assert(pool != NULL);
1297 pool->servers[insert_point] = home;
1301 rad_assert(pool == NULL);
1302 rad_assert(home != NULL);
1305 * Count the old-style realms of this name.
1307 num_home_servers = 0;
1308 for (subcs = cf_section_find_next(cs, NULL, "realm");
1310 subcs = cf_section_find_next(cs, subcs, "realm")) {
1311 char const *this = cf_section_name2(subcs);
1313 if (!this || (strcmp(this, realm) != 0)) continue;
1317 if (num_home_servers == 0) {
1318 cf_log_err_cs(cs, "Internal error counting pools for home server %s.", name);
1323 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1326 pool->servers[0] = home;
1328 if (!rbtree_insert(home_pools_byname, pool)) {
1329 rad_assert("Internal sanity check failed");
1339 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1342 char const *secret = NULL;
1343 home_pool_type_t ldflag;
1346 cp = cf_pair_find(cs, "ldflag");
1347 ldflag = HOME_POOL_FAIL_OVER;
1349 host = cf_pair_value(cp);
1351 cf_log_err_cp(cp, "No value specified for ldflag");
1355 if (strcasecmp(host, "fail_over") == 0) {
1356 cf_log_info(cs, "\tldflag = fail_over");
1358 } else if (strcasecmp(host, "round_robin") == 0) {
1359 ldflag = HOME_POOL_LOAD_BALANCE;
1360 cf_log_info(cs, "\tldflag = round_robin");
1363 cf_log_err_cs(cs, "Unknown value \"%s\" for ldflag", host);
1366 } /* else don't print it. */
1369 * Allow old-style if it doesn't exist, or if it exists and
1372 cp = cf_pair_find(cs, "authhost");
1374 host = cf_pair_value(cp);
1376 cf_log_err_cp(cp, "No value specified for authhost");
1380 if (strcmp(host, "LOCAL") != 0) {
1381 cp = cf_pair_find(cs, "secret");
1383 cf_log_err_cs(cs, "No shared secret supplied for realm: %s", r->name);
1387 secret = cf_pair_value(cp);
1389 cf_log_err_cp(cp, "No value specified for secret");
1394 cf_log_info(cs, "\tauthhost = %s", host);
1396 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1397 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1402 cp = cf_pair_find(cs, "accthost");
1404 host = cf_pair_value(cp);
1406 cf_log_err_cp(cp, "No value specified for accthost");
1411 * Don't look for a secret again if it was found
1414 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1415 cp = cf_pair_find(cs, "secret");
1417 cf_log_err_cs(cs, "No shared secret supplied for realm: %s", r->name);
1421 secret = cf_pair_value(cp);
1423 cf_log_err_cp(cp, "No value specified for secret");
1428 cf_log_info(cs, "\taccthost = %s", host);
1430 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1431 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1436 cp = cf_pair_find(cs, "virtual_server");
1438 host = cf_pair_value(cp);
1440 cf_log_err_cp(cp, "No value specified for virtual_server");
1444 cf_log_info(cs, "\tvirtual_server = %s", host);
1446 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1447 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1450 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1451 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1456 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1464 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1465 char const *name, home_pool_t **dest,
1466 int server_type, int do_print)
1468 home_pool_t mypool, *pool;
1471 mypool.server_type = server_type;
1473 pool = rbtree_finddata(home_pools_byname, &mypool);
1475 CONF_SECTION *pool_cs;
1477 pool_cs = cf_section_sub_find_name2(rc->cs,
1481 pool_cs = cf_section_sub_find_name2(rc->cs,
1486 cf_log_err_cs(cs, "Failed to find home_server_pool \"%s\"", name);
1490 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1494 pool = rbtree_finddata(home_pools_byname, &mypool);
1496 ERROR("Internal sanity check failed in add_pool_to_realm");
1501 if (pool->server_type != server_type) {
1502 cf_log_err_cs(cs, "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1513 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1519 home_pool_t *auth_pool, *acct_pool;
1520 char const *auth_pool_name, *acct_pool_name;
1522 char const *coa_pool_name;
1523 home_pool_t *coa_pool;
1527 name2 = cf_section_name1(cs);
1528 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1529 cf_log_err_cs(cs, "Section is not a realm.");
1533 name2 = cf_section_name2(cs);
1535 cf_log_err_cs(cs, "Realm section is missing the realm name.");
1540 auth_pool = acct_pool = NULL;
1541 auth_pool_name = acct_pool_name = NULL;
1544 coa_pool_name = NULL;
1548 * Prefer new configuration to old one.
1550 cp = cf_pair_find(cs, "pool");
1551 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1552 if (cp) auth_pool_name = cf_pair_value(cp);
1553 if (cp && auth_pool_name) {
1554 acct_pool_name = auth_pool_name;
1555 if (!add_pool_to_realm(rc, cs,
1556 auth_pool_name, &auth_pool,
1557 HOME_TYPE_AUTH, 1)) {
1560 if (!add_pool_to_realm(rc, cs,
1561 auth_pool_name, &acct_pool,
1562 HOME_TYPE_ACCT, 0)) {
1567 cp = cf_pair_find(cs, "auth_pool");
1568 if (cp) auth_pool_name = cf_pair_value(cp);
1569 if (cp && auth_pool_name) {
1571 cf_log_err_cs(cs, "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1574 if (!add_pool_to_realm(rc, cs,
1575 auth_pool_name, &auth_pool,
1576 HOME_TYPE_AUTH, 1)) {
1581 cp = cf_pair_find(cs, "acct_pool");
1582 if (cp) acct_pool_name = cf_pair_value(cp);
1583 if (cp && acct_pool_name) {
1584 int do_print = true;
1587 cf_log_err_cs(cs, "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1593 (strcmp(auth_pool_name, acct_pool_name) != 0))) {
1597 if (!add_pool_to_realm(rc, cs,
1598 acct_pool_name, &acct_pool,
1599 HOME_TYPE_ACCT, do_print)) {
1605 cp = cf_pair_find(cs, "coa_pool");
1606 if (cp) coa_pool_name = cf_pair_value(cp);
1607 if (cp && coa_pool_name) {
1608 int do_print = true;
1610 if (!add_pool_to_realm(rc, cs,
1611 coa_pool_name, &coa_pool,
1612 HOME_TYPE_COA, do_print)) {
1619 cf_log_info(cs, " realm %s {", name2);
1623 * The realm MAY already exist if it's an old-style realm.
1624 * In that case, merge the old-style realm with this one.
1626 r = realm_find2(name2);
1627 if (r && (strcmp(r->name, name2) == 0)) {
1628 if (cf_pair_find(cs, "auth_pool") ||
1629 cf_pair_find(cs, "acct_pool")) {
1630 cf_log_err_cs(cs, "Duplicate realm \"%s\"", name2);
1634 if (!old_realm_config(rc, cs, r)) {
1638 cf_log_info(cs, " } # realm %s", name2);
1644 if (name2[0] == '~') {
1649 * Include substring matches.
1651 rcode = regcomp(®, name2 + 1, REG_EXTENDED | REG_NOSUB | REG_ICASE);
1655 regerror(rcode, ®, buffer, sizeof(buffer));
1658 "Invalid regex \"%s\": %s",
1666 r = rad_malloc(sizeof(*r));
1667 memset(r, 0, sizeof(*r));
1672 r->auth_pool = auth_pool;
1673 r->acct_pool = acct_pool;
1675 r->coa_pool = coa_pool;
1678 if (auth_pool_name &&
1679 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1680 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1682 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1683 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1685 if (coa_pool_name) cf_log_info(cs, "\tcoa_pool = %s", coa_pool_name);
1690 cp = cf_pair_find(cs, "nostrip");
1691 if (cp && (cf_pair_value(cp) == NULL)) {
1693 cf_log_info(cs, "\tnostrip");
1697 * We're a new-style realm. Complain if we see the old
1700 if (r->auth_pool || r->acct_pool) {
1701 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1702 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1703 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1704 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1705 WDEBUG2("Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1710 * The realm MAY be an old-style realm, as there
1711 * was no auth_pool or acct_pool. Double-check
1712 * it, just to be safe.
1714 } else if (!old_realm_config(rc, cs, r)) {
1720 * It's a regex. Add it to a separate list.
1722 if (name2[0] == '~') {
1723 realm_regex_t *rr, **last;
1725 rr = rad_malloc(sizeof(*rr));
1727 last = &realms_regex;
1728 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1736 cf_log_info(cs, " }");
1741 if (!rbtree_insert(realms_byname, r)) {
1742 rad_assert("Internal sanity check failed");
1746 cf_log_info(cs, " }");
1751 cf_log_info(cs, " } # realm %s", name2);
1757 static const FR_NAME_NUMBER home_server_types[] = {
1758 { "auth", HOME_TYPE_AUTH },
1759 { "auth+acct", HOME_TYPE_AUTH },
1760 { "acct", HOME_TYPE_ACCT },
1761 { "coa", HOME_TYPE_COA },
1765 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1768 char const *name, *type;
1770 CONF_SECTION *server_cs;
1772 cp = cf_pair_find(cs, "home_server");
1774 cf_log_err_cs(cs, "Pool does not contain a \"home_server\" entry");
1775 return HOME_TYPE_INVALID;
1778 name = cf_pair_value(cp);
1780 cf_log_err_cp(cp, "home_server entry does not reference a home server");
1781 return HOME_TYPE_INVALID;
1784 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1786 cf_log_err_cp(cp, "home_server \"%s\" does not exist", name);
1787 return HOME_TYPE_INVALID;
1790 cp = cf_pair_find(server_cs, "type");
1792 cf_log_err_cs(server_cs, "home_server %s does not contain a \"type\" entry", name);
1793 return HOME_TYPE_INVALID;
1796 type = cf_pair_value(cp);
1798 cf_log_err_cs(server_cs, "home_server %s contains an empty \"type\" entry", name);
1799 return HOME_TYPE_INVALID;
1802 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1803 if (home == HOME_TYPE_INVALID) {
1804 cf_log_err_cs(server_cs, "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1805 return HOME_TYPE_INVALID;
1808 return home; /* 'cause we miss it so much */
1812 int realms_init(CONF_SECTION *config)
1816 CONF_SECTION *server_cs;
1818 realm_config_t *rc, *old_rc;
1820 if (realms_byname) return 1;
1822 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1823 if (!realms_byname) {
1829 home_servers_byaddr = rbtree_create(home_server_addr_cmp, home_server_free, 0);
1830 if (!home_servers_byaddr) {
1835 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1836 if (!home_servers_byname) {
1842 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1843 if (!home_servers_bynumber) {
1849 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1850 if (!home_pools_byname) {
1856 rc = talloc_zero(NULL, realm_config_t);
1860 cs = cf_subsection_find_next(config, NULL, "proxy");
1862 if (cf_section_parse(cs, rc, proxy_config) < 0) {
1863 ERROR("Failed parsing proxy section");
1867 * Must be called after realms_free as home_servers
1868 * parented by rc are in trees freed by realms_free()
1874 rc->dead_time = DEAD_TIME;
1875 rc->retry_count = RETRY_COUNT;
1876 rc->retry_delay = RETRY_DELAY;
1878 rc->wake_all_if_all_dead= 0;
1881 for (cs = cf_subsection_find_next(config, NULL, "home_server");
1883 cs = cf_subsection_find_next(config, cs, "home_server")) {
1884 if (!home_server_add(rc, cs)) goto error;
1888 * Loop over virtual servers to find homes which are
1891 for (server_cs = cf_subsection_find_next(config, NULL, "server");
1893 server_cs = cf_subsection_find_next(config, server_cs, "server")) {
1894 for (cs = cf_subsection_find_next(server_cs, NULL, "home_server");
1896 cs = cf_subsection_find_next(server_cs, cs, "home_server")) {
1897 if (!home_server_add(rc, cs)) goto error;
1902 for (cs = cf_subsection_find_next(config, NULL, "realm");
1904 cs = cf_subsection_find_next(config, cs, "realm")) {
1905 if (!realm_add(rc, cs)) goto error;
1910 * CoA pools aren't necessarily tied to realms.
1912 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1914 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1918 * Pool was already loaded.
1920 if (cf_data_find(cs, "home_server_pool")) continue;
1922 type = pool_peek_type(config, cs);
1923 if (type == HOME_TYPE_INVALID) goto error;
1924 if (!server_pool_add(rc, cs, type, true)) goto error;
1930 xlat_register("home_server", xlat_home_server, NULL, NULL);
1931 xlat_register("home_server_pool", xlat_server_pool, NULL, NULL);
1935 * Swap pointers atomically.
1937 old_rc = realm_config;
1939 talloc_free(old_rc);
1945 * Find a realm where "name" might be the regex.
1947 REALM *realm_find2(char const *name)
1952 if (!name) name = "NULL";
1954 myrealm.name = name;
1955 realm = rbtree_finddata(realms_byname, &myrealm);
1956 if (realm) return realm;
1960 realm_regex_t *this;
1962 for (this = realms_regex; this != NULL; this = this->next) {
1963 if (strcmp(this->realm->name, name) == 0) {
1971 * Couldn't find a realm. Look for DEFAULT.
1973 myrealm.name = "DEFAULT";
1974 return rbtree_finddata(realms_byname, &myrealm);
1979 * Find a realm in the REALM list.
1981 REALM *realm_find(char const *name)
1986 if (!name) name = "NULL";
1988 myrealm.name = name;
1989 realm = rbtree_finddata(realms_byname, &myrealm);
1990 if (realm) return realm;
1994 realm_regex_t *this;
1996 for (this = realms_regex; this != NULL; this = this->next) {
2001 * Include substring matches.
2003 if (regcomp(®, this->realm->name + 1, REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
2007 compare = regexec(®, name, 0, NULL, 0);
2010 if (compare == 0) return this->realm;
2016 * Couldn't find a realm. Look for DEFAULT.
2018 myrealm.name = "DEFAULT";
2019 return rbtree_finddata(realms_byname, &myrealm);
2026 * Allocate the proxy list if it doesn't already exist, and copy request
2027 * VPs into it. Setup src/dst IP addresses based on home server, and
2028 * calculate and add the message-authenticator.
2030 * This is a distinct function from home_server_ldb, as not all home_server_t
2031 * lookups result in the *CURRENT* request being proxied,
2032 * as in rlm_replicate, and this may trigger asserts elsewhere in the
2035 void home_server_update_request(home_server_t *home, REQUEST *request)
2039 * Allocate the proxy packet, only if it wasn't
2040 * already allocated by a module. This check is
2041 * mainly to support the proxying of EAP-TTLS and
2042 * EAP-PEAP tunneled requests.
2044 * In those cases, the EAP module creates a
2045 * "fake" request, and recursively passes it
2046 * through the authentication stage of the
2047 * server. The module then checks if the request
2048 * was supposed to be proxied, and if so, creates
2049 * a proxy packet from the TUNNELED request, and
2050 * not from the EAP request outside of the
2053 * The proxy then works like normal, except that
2054 * the response packet is "eaten" by the EAP
2055 * module, and encapsulated into an EAP packet.
2057 if (!request->proxy) {
2058 request->proxy = rad_alloc(request, true);
2059 if (!request->proxy) {
2066 * Copy the request, then look up name
2067 * and plain-text password in the copy.
2069 * Note that the User-Name attribute is
2070 * the *original* as sent over by the
2071 * client. The Stripped-User-Name
2072 * attribute is the one hacked through
2075 request->proxy->vps = paircopy(request->proxy,
2076 request->packet->vps);
2080 * Update the various fields as appropriate.
2082 request->proxy->src_ipaddr = home->src_ipaddr;
2083 request->proxy->src_port = 0;
2084 request->proxy->dst_ipaddr = home->ipaddr;
2085 request->proxy->dst_port = home->port;
2086 request->home_server = home;
2089 * Access-Requests have a Message-Authenticator added,
2090 * unless one already exists.
2092 if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2093 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY)) {
2094 pairmake(request->proxy, &request->proxy->vps,
2095 "Message-Authenticator", "0x00",
2100 home_server_t *home_server_ldb(char const *realmname,
2101 home_pool_t *pool, REQUEST *request)
2105 home_server_t *found = NULL;
2106 home_server_t *zombie = NULL;
2110 * Determine how to pick choose the home server.
2112 switch (pool->type) {
2116 * For load-balancing by client IP address, we
2117 * pick a home server by hashing the client IP.
2119 * This isn't as even a load distribution as
2120 * tracking the State attribute, but it's better
2123 case HOME_POOL_CLIENT_BALANCE:
2124 switch (request->packet->src_ipaddr.af) {
2126 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2127 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2130 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2131 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2137 start = hash % pool->num_home_servers;
2140 case HOME_POOL_CLIENT_PORT_BALANCE:
2141 switch (request->packet->src_ipaddr.af) {
2143 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2144 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2147 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2148 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2154 fr_hash_update(&request->packet->src_port,
2155 sizeof(request->packet->src_port), hash);
2156 start = hash % pool->num_home_servers;
2159 case HOME_POOL_KEYED_BALANCE:
2160 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY, 0, TAG_ANY)) != NULL) {
2161 hash = fr_hash(vp->vp_strvalue, vp->length);
2162 start = hash % pool->num_home_servers;
2167 case HOME_POOL_LOAD_BALANCE:
2168 case HOME_POOL_FAIL_OVER:
2172 default: /* this shouldn't happen... */
2179 * Starting with the home server we chose, loop through
2180 * all home servers. If the current one is dead, skip
2181 * it. If it is too busy, skip it.
2183 * Otherwise, use it.
2185 for (count = 0; count < pool->num_home_servers; count++) {
2186 home_server_t *home = pool->servers[(start + count) % pool->num_home_servers];
2188 if (!home) continue;
2191 * Skip dead home servers.
2193 * Home servers that are unknown, alive, or zombie
2194 * are used for proxying.
2196 if (home->state == HOME_STATE_IS_DEAD) {
2201 * This home server is too busy. Choose another one.
2203 if (home->currently_outstanding >= home->max_outstanding) {
2209 * We read the packet from a detail file, AND it
2210 * came from this server. Don't re-proxy it
2213 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2214 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
2215 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2221 * Default virtual: ignore homes tied to a
2224 if (!request->server && home->parent_server) {
2229 * A virtual AND home is tied to virtual,
2230 * ignore ones which don't match.
2232 if (request->server && home->parent_server &&
2233 strcmp(request->server, home->parent_server) != 0) {
2238 * Allow request->server && !home->parent_server
2240 * i.e. virtuals can proxy to globally defined
2245 * It's zombie, so we remember the first zombie
2246 * we find, but we don't mark it as a "live"
2249 if (home->state == HOME_STATE_ZOMBIE) {
2250 if (!zombie) zombie = home;
2255 * We've found the first "live" one. Use that.
2257 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2263 * Otherwise we're doing some kind of load balancing.
2264 * If we haven't found one yet, pick this one.
2271 RDEBUG3("PROXY %s %d\t%s %d",
2272 found->name, found->currently_outstanding,
2273 home->name, home->currently_outstanding);
2276 * Prefer this server if it's less busy than the
2277 * one we had previously found.
2279 if (home->currently_outstanding < found->currently_outstanding) {
2280 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2281 home->name, found->name);
2287 * Ignore servers which are busier than the one
2290 if (home->currently_outstanding > found->currently_outstanding) {
2291 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2292 home->name, found->name);
2297 * From the list of servers which have the same
2298 * load, choose one at random.
2300 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2303 } /* loop over the home servers */
2306 * We have no live servers, BUT we have a zombie. Use
2307 * the zombie as a last resort.
2309 if (!found && zombie) {
2315 * There's a fallback if they're all dead.
2317 if (!found && pool->fallback) {
2318 found = pool->fallback;
2320 WDEBUG("Home server pool %s failing over to fallback %s",
2321 pool->name, found->server);
2322 if (pool->in_fallback) goto update_and_return;
2324 pool->in_fallback = true;
2327 * Run the trigger once an hour saying that
2330 if ((pool->time_all_dead + 3600) < request->timestamp) {
2331 pool->time_all_dead = request->timestamp;
2332 exec_trigger(request, pool->cs, "home_server_pool.fallback", false);
2338 if ((found != pool->fallback) && pool->in_fallback) {
2339 pool->in_fallback = false;
2340 exec_trigger(request, pool->cs, "home_server_pool.normal", false);
2347 * No live match found, and no fallback to the "DEFAULT"
2348 * realm. We fix this by blindly marking all servers as
2349 * "live". But only do it for ones that don't support
2350 * "pings", as they will be marked live when they
2351 * actually are live.
2353 if (!realm_config->fallback &&
2354 realm_config->wake_all_if_all_dead) {
2355 for (count = 0; count < pool->num_home_servers; count++) {
2356 home_server_t *home = pool->servers[count];
2358 if (!home) continue;
2360 if ((home->state == HOME_STATE_IS_DEAD) &&
2361 (home->ping_check == HOME_PING_CHECK_NONE)) {
2362 home->state = HOME_STATE_ALIVE;
2363 if (!found) found = home;
2367 if (found) goto update_and_return;
2371 * Still nothing. Look up the DEFAULT realm, but only
2372 * if we weren't looking up the NULL or DEFAULT realms.
2374 if (realm_config->fallback &&
2376 (strcmp(realmname, "NULL") != 0) &&
2377 (strcmp(realmname, "DEFAULT") != 0)) {
2378 REALM *rd = realm_find("DEFAULT");
2380 if (!rd) return NULL;
2383 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2384 pool = rd->auth_pool;
2386 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2387 pool = rd->acct_pool;
2389 if (!pool) return NULL;
2391 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2392 return home_server_ldb(rd->name, pool, request);
2396 * Still haven't found anything. Oh well.
2402 home_server_t *home_server_find(fr_ipaddr_t *ipaddr, int port, int proto)
2404 home_server_t myhome;
2406 memset(&myhome, 0, sizeof(myhome));
2407 myhome.ipaddr = *ipaddr;
2408 myhome.src_ipaddr.af = ipaddr->af;
2411 myhome.proto = proto;
2413 myhome.proto = IPPROTO_UDP;
2415 myhome.server = NULL; /* we're not called for internal proxying */
2417 return rbtree_finddata(home_servers_byaddr, &myhome);
2421 home_server_t *home_server_byname(char const *name, int type)
2423 home_server_t myhome;
2425 memset(&myhome, 0, sizeof(myhome));
2429 return rbtree_finddata(home_servers_byname, &myhome);
2434 home_server_t *home_server_bynumber(int number)
2436 home_server_t myhome;
2438 memset(&myhome, 0, sizeof(myhome));
2439 myhome.number = number;
2440 myhome.server = NULL; /* we're not called for internal proxying */
2442 return rbtree_finddata(home_servers_bynumber, &myhome);
2446 home_pool_t *home_pool_byname(char const *name, int type)
2450 memset(&mypool, 0, sizeof(mypool));
2452 mypool.server_type = type;
2453 return rbtree_finddata(home_pools_byname, &mypool);