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 {
53 bool wake_all_if_all_dead;
56 static realm_config_t *realm_config = NULL;
57 static bool realms_initialized = false;
60 static rbtree_t *home_servers_byaddr = NULL;
61 static rbtree_t *home_servers_byname = NULL;
63 static int home_server_max_number = 0;
64 static rbtree_t *home_servers_bynumber = NULL;
67 static rbtree_t *home_pools_byname = NULL;
70 * Map the proxy server configuration parameters to variables.
72 static const CONF_PARSER proxy_config[] = {
73 { "retry_delay", FR_CONF_OFFSET(PW_TYPE_INTEGER, realm_config_t, retry_delay), STRINGIFY(RETRY_DELAY) },
75 { "retry_count", FR_CONF_OFFSET(PW_TYPE_INTEGER, realm_config_t, retry_count), STRINGIFY(RETRY_COUNT) },
77 { "default_fallback", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, realm_config_t, fallback), "no" },
79 { "dynamic", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, realm_config_t, dynamic), NULL },
81 { "dead_time", FR_CONF_OFFSET(PW_TYPE_INTEGER, realm_config_t, dead_time), STRINGIFY(DEAD_TIME) },
83 { "wake_all_if_all_dead", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, realm_config_t, wake_all_if_all_dead), "no" },
85 { NULL, -1, 0, NULL, NULL }
89 static int realm_name_cmp(void const *one, void const *two)
94 return strcasecmp(a->name, b->name);
99 static void home_server_free(void *data)
101 home_server_t *home = data;
106 static int home_server_name_cmp(void const *one, void const *two)
108 home_server_t const *a = one;
109 home_server_t const *b = two;
111 if (a->type < b->type) return -1;
112 if (a->type > b->type) return +1;
114 return strcasecmp(a->name, b->name);
117 static int home_server_addr_cmp(void const *one, void const *two)
120 home_server_t const *a = one;
121 home_server_t const *b = two;
123 if (a->server && !b->server) return -1;
124 if (!a->server && b->server) return +1;
125 if (a->server && b->server) {
126 rcode = a->type - b->type;
127 if (rcode != 0) return rcode;
128 return strcmp(a->server, b->server);
131 if (a->port < b->port) return -1;
132 if (a->port > b->port) return +1;
135 if (a->proto < b->proto) return -1;
136 if (a->proto > b->proto) return +1;
139 rcode = fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
140 if (rcode != 0) return rcode;
142 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
146 static int home_server_number_cmp(void const *one, void const *two)
148 home_server_t const *a = one;
149 home_server_t const *b = two;
151 return (a->number - b->number);
155 static int home_pool_name_cmp(void const *one, void const *two)
157 home_pool_t const *a = one;
158 home_pool_t const *b = two;
160 if (a->server_type < b->server_type) return -1;
161 if (a->server_type > b->server_type) return +1;
163 return strcasecmp(a->name, b->name);
167 static size_t CC_HINT(nonnull) xlat_cs(CONF_SECTION *cs, char const *fmt, char *out, size_t outlen)
169 char const *value = NULL;
174 if (strcmp(fmt, "instance") == 0) {
175 value = cf_section_name2(cs);
183 cp = cf_pair_find(cs, fmt);
184 if (!cp || !(value = cf_pair_value(cp))) {
190 strlcpy(out, value, outlen);
197 * Xlat for %{home_server:foo}
199 static ssize_t CC_HINT(nonnull) xlat_home_server(UNUSED void *instance, REQUEST *request,
200 char const *fmt, char *out, size_t outlen)
202 if (!request->home_server) {
203 RWDEBUG("No home_server associated with this request");
209 return xlat_cs(request->home_server->cs, fmt, out, outlen);
214 * Xlat for %{home_server_pool:foo}
216 static ssize_t CC_HINT(nonnull) xlat_server_pool(UNUSED void *instance, REQUEST *request,
217 char const *fmt, char *out, size_t outlen)
219 if (!request->home_pool) {
220 RWDEBUG("No home_pool associated with this request");
226 return xlat_cs(request->home_pool->cs, fmt, out, outlen);
230 void realms_free(void)
234 rbtree_free(home_servers_bynumber);
235 home_servers_bynumber = NULL;
238 rbtree_free(home_servers_byname);
239 home_servers_byname = NULL;
241 rbtree_free(home_servers_byaddr);
242 home_servers_byaddr = NULL;
244 rbtree_free(home_pools_byname);
245 home_pools_byname = NULL;
248 rbtree_free(realms_byname);
249 realms_byname = NULL;
253 realm_regex_t *this, *next;
255 for (this = realms_regex; this != NULL; this = next) {
264 talloc_free(realm_config);
270 static CONF_PARSER limit_config[] = {
271 { "max_connections", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, limit.max_connections), "16" },
272 { "max_requests", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, limit.max_requests), "0" },
273 { "lifetime", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, limit.lifetime), "0" },
274 { "idle_timeout", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, limit.idle_timeout), "0" },
276 { NULL, -1, 0, NULL, NULL } /* end the list */
279 static fr_ipaddr_t hs_ipaddr;
280 static char const *hs_srcipaddr = NULL;
281 static char const *hs_type = NULL;
282 static char const *hs_check = NULL;
283 static char const *hs_virtual_server = NULL;
285 static char const *hs_proto = NULL;
289 static CONF_PARSER home_server_coa[] = {
290 { "irt", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, coa_irt), STRINGIFY(2) },
291 { "mrt", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, coa_mrt), STRINGIFY(16) },
292 { "mrc", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, coa_mrc), STRINGIFY(5) },
293 { "mrd", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, coa_mrd), STRINGIFY(30) },
295 { NULL, -1, 0, NULL, NULL } /* end the list */
299 static CONF_PARSER home_server_config[] = {
300 { "ipaddr", FR_CONF_POINTER(PW_TYPE_IP_ADDR, &hs_ipaddr), NULL },
301 { "ipv4addr", FR_CONF_POINTER(PW_TYPE_IPV4_ADDR, &hs_ipaddr), NULL },
302 { "ipv6addr", FR_CONF_POINTER(PW_TYPE_IPV6_ADDR, &hs_ipaddr), NULL },
303 { "virtual_server", FR_CONF_POINTER(PW_TYPE_STRING, &hs_virtual_server), NULL },
305 { "port", FR_CONF_OFFSET(PW_TYPE_SHORT, home_server_t, port), "0" },
307 { "type", FR_CONF_POINTER(PW_TYPE_STRING, &hs_type), NULL },
310 { "proto", FR_CONF_POINTER(PW_TYPE_STRING, &hs_proto), NULL },
313 { "secret", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_SECRET, home_server_t, secret), NULL },
315 { "src_ipaddr", FR_CONF_POINTER(PW_TYPE_STRING, &hs_srcipaddr), NULL },
317 { "response_window", FR_CONF_OFFSET(PW_TYPE_TIMEVAL, home_server_t, response_window), "30" },
318 { "response_timeouts", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, max_response_timeouts), "1" },
319 { "max_outstanding", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, max_outstanding), "65536" },
321 { "zombie_period", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, zombie_period), "40" },
322 { "status_check", FR_CONF_POINTER(PW_TYPE_STRING, &hs_check), "none" },
323 { "ping_check", FR_CONF_POINTER(PW_TYPE_STRING, &hs_check), NULL },
325 { "ping_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, ping_interval), "30" },
326 { "check_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, ping_interval), "30" },
328 { "check_timeout", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, ping_timeout), "4" },
329 { "status_check_timeout", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, ping_timeout), NULL },
331 { "num_answers_to_alive", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, num_pings_to_alive), "3" },
332 { "revive_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, revive_interval), "300" },
334 { "username", FR_CONF_OFFSET(PW_TYPE_STRING, home_server_t, ping_user_name), NULL },
335 { "password", FR_CONF_OFFSET(PW_TYPE_STRING, home_server_t, ping_user_password), NULL },
338 { "historic_average_window", FR_CONF_OFFSET(PW_TYPE_INTEGER, home_server_t, ema.window), NULL },
342 { "coa", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) home_server_coa },
345 { "limit", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) limit_config },
347 { NULL, -1, 0, NULL, NULL } /* end the list */
351 static void null_free(UNUSED void *data)
356 int realms_home_server_add(home_server_t *home, CONF_SECTION *cs, int dual)
358 CONF_SECTION *parent = NULL;
359 const char * name2 = home->name;
362 * Make sure that this is set.
364 if (home->src_ipaddr.af == AF_UNSPEC) {
365 home->src_ipaddr.af = home->ipaddr.af;
370 if (rbtree_finddata(home_servers_byname, home) != NULL) {
372 "Duplicate home server name %s.", name2);
377 (rbtree_finddata(home_servers_byaddr, home) != NULL)) {
379 "Duplicate home server IP %s.", name2);
383 if (!rbtree_insert(home_servers_byname, home)) {
385 "Internal error %d adding home server %s.",
391 !rbtree_insert(home_servers_byaddr, home)) {
392 rbtree_deletebydata(home_servers_byname, home);
394 "Internal error %d adding home server %s.",
400 home->number = home_server_max_number++;
401 if (!rbtree_insert(home_servers_bynumber, home)) {
402 rbtree_deletebydata(home_servers_byname, home);
403 if (home->ipaddr.af != AF_UNSPEC) {
404 rbtree_deletebydata(home_servers_byname, home);
407 "Internal error %d adding home server %s.",
413 FR_INTEGER_BOUND_CHECK("max_outstanding", home->max_outstanding, >=, 8);
414 FR_INTEGER_BOUND_CHECK("max_outstanding", home->max_outstanding, <=, 65536*16);
416 FR_INTEGER_BOUND_CHECK("ping_interval", home->ping_interval, >=, 6);
417 FR_INTEGER_BOUND_CHECK("ping_interval", home->ping_interval, <=, 120);
419 FR_TIMEVAL_BOUND_CHECK("response_window", &home->response_window, >=, 0, 1000);
420 FR_TIMEVAL_BOUND_CHECK("response_window", &home->response_window, <=, 60, 0);
421 FR_TIMEVAL_BOUND_CHECK("response_window", &home->response_window, <=,
422 main_config.max_request_time, 0);
425 * Track the minimum response window, so that we can
426 * correctly set the timers in process.c
428 if (timercmp(&main_config.init_delay, &home->response_window, >)) {
429 main_config.init_delay = home->response_window;
432 FR_INTEGER_BOUND_CHECK("zombie_period", home->zombie_period, >=, 1);
433 FR_INTEGER_BOUND_CHECK("zombie_period", home->zombie_period, <=, 120);
434 FR_INTEGER_BOUND_CHECK("zombie_period", home->zombie_period, >=, (uint32_t) home->response_window.tv_sec);
436 FR_INTEGER_BOUND_CHECK("num_pings_to_alive", home->num_pings_to_alive, >=, 3);
437 FR_INTEGER_BOUND_CHECK("num_pings_to_alive", home->num_pings_to_alive, <=, 10);
439 FR_INTEGER_BOUND_CHECK("ping_timeout", home->ping_timeout, >=, 3);
440 FR_INTEGER_BOUND_CHECK("ping_timeout", home->ping_timeout, <=, 10);
442 FR_INTEGER_BOUND_CHECK("revive_interval", home->revive_interval, >=, 60);
443 FR_INTEGER_BOUND_CHECK("revive_interval", home->revive_interval, <=, 3600);
446 FR_INTEGER_BOUND_CHECK("coa_irt", home->coa_irt, >=, 1);
447 FR_INTEGER_BOUND_CHECK("coa_irt", home->coa_irt, <=, 5);
449 FR_INTEGER_BOUND_CHECK("coa_mrc", home->coa_mrc, <=, 20);
451 FR_INTEGER_BOUND_CHECK("coa_mrt", home->coa_mrt, <=, 30);
453 FR_INTEGER_BOUND_CHECK("coa_mrd", home->coa_mrd, >=, 5);
454 FR_INTEGER_BOUND_CHECK("coa_mrd", home->coa_mrd, <=, 60);
457 FR_INTEGER_BOUND_CHECK("max_connections", home->limit.max_connections, <=, 1024);
461 * UDP sockets can't be connection limited.
463 if (home->proto != IPPROTO_TCP) home->limit.max_connections = 0;
466 if ((home->limit.idle_timeout > 0) && (home->limit.idle_timeout < 5))
467 home->limit.idle_timeout = 5;
468 if ((home->limit.lifetime > 0) && (home->limit.lifetime < 5))
469 home->limit.lifetime = 5;
470 if ((home->limit.lifetime > 0) && (home->limit.idle_timeout > home->limit.lifetime))
471 home->limit.idle_timeout = 0;
473 parent = cf_item_parent(cf_sectiontoitem(cs));
474 if (strcmp(cf_section_name1(parent), "server") == 0) {
475 home->parent_server = cf_section_name2(parent);
479 home_server_t *home2 = talloc(home, home_server_t);
481 memcpy(home2, home, sizeof(*home2));
483 home2->type = HOME_TYPE_ACCT;
485 home2->ping_user_password = NULL;
487 home2->parent_server = home->parent_server;
489 if (!rbtree_insert(home_servers_byname, home2)) {
491 "Internal error %d adding home server %s.",
498 !rbtree_insert(home_servers_byaddr, home2)) {
499 rbtree_deletebydata(home_servers_byname, home2);
501 "Internal error %d adding home server %s.",
508 home2->number = home_server_max_number++;
509 if (!rbtree_insert(home_servers_bynumber, home2)) {
510 rbtree_deletebydata(home_servers_byname, home2);
511 if (!home2->server) {
512 rbtree_deletebydata(home_servers_byname, home2);
515 "Internal error %d adding home server %s.",
529 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs)
537 hs_virtual_server = NULL;
539 name2 = cf_section_name2(cs);
541 cf_log_err_cs(cs, "Home server section is missing a name");
545 home = talloc_zero(rc, home_server_t);
549 home->state = HOME_STATE_UNKNOWN;
552 * Last packet sent / received are zero.
555 memset(&hs_ipaddr, 0, sizeof(hs_ipaddr));
556 if (cf_section_parse(cs, home, home_server_config) < 0) {
561 * Figure out which one to use.
563 if (cf_pair_find(cs, "ipaddr") || cf_pair_find(cs, "ipv4addr") || cf_pair_find(cs, "ipv6addr")) {
564 if (is_wildcard(&hs_ipaddr)) {
565 cf_log_err_cs(cs, "Wildcard '*' addresses are not permitted for home servers");
568 home->ipaddr = hs_ipaddr;
569 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
570 home->ipaddr.af = AF_UNSPEC;
571 home->server = cf_pair_value(cp);
574 "Invalid value for virtual_server");
578 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
581 "No such server %s", home->server);
586 * When CoA is used, the user has to specify the type
587 * of the home server, even when they point to
590 home->secret = strdup("");
594 cf_log_err_cs(cs, "No ipaddr, ipv4addr, ipv6addr, or virtual_server defined for home server \"%s\"", name2);
605 if (home->port == 0) {
606 cf_log_err_cs(cs, "No port, or invalid port defined for home server %s", name2);
611 * Use a reasonable default.
614 if (!hs_type || (strcasecmp(hs_type, "auth+acct") == 0)) {
615 home->type = HOME_TYPE_AUTH;
617 } else if (strcasecmp(hs_type, "auth") == 0) {
618 home->type = HOME_TYPE_AUTH;
620 } else if (strcasecmp(hs_type, "acct") == 0) {
621 home->type = HOME_TYPE_ACCT;
623 } else if (strcasecmp(hs_type, "coa") == 0) {
624 home->type = HOME_TYPE_COA;
627 if (home->server != NULL) {
628 cf_log_err_cs(cs, "Home servers of type \"coa\" cannot point to a virtual server");
634 cf_log_err_cs(cs, "Invalid type \"%s\" for home server %s.", hs_type, name2);
639 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
640 home->ping_check = HOME_PING_CHECK_NONE;
642 } else if (strcasecmp(hs_check, "status-server") == 0) {
643 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
645 } else if (strcasecmp(hs_check, "request") == 0) {
646 home->ping_check = HOME_PING_CHECK_REQUEST;
648 if (!home->ping_user_name ||
649 !*home->ping_user_name) {
650 cf_log_err_cs(cs, "You must supply a 'username' to enable status_check=request");
654 if ((home->type == HOME_TYPE_AUTH) &&
655 (!home->ping_user_password ||
656 !*home->ping_user_password)) {
657 cf_log_err_cs(cs, "You must supply a password to enable status_check=request");
663 "Invalid status_check \"%s\" for home server %s.",
669 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
670 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
671 if (!home->ping_user_name) {
672 cf_log_err_cs(cs, "You must supply a user name to enable status_check=request");
676 if ((home->type == HOME_TYPE_AUTH) &&
677 !home->ping_user_password) {
678 cf_log_err_cs(cs, "You must supply a password to enable status_check=request");
683 home->proto = IPPROTO_UDP;
686 if (strcmp(hs_proto, "udp") == 0) {
689 } else if (strcmp(hs_proto, "tcp") == 0) {
691 home->proto = IPPROTO_TCP;
693 if (home->ping_check != HOME_PING_CHECK_NONE) {
695 "Only 'status_check = none' is allowed for home servers with 'proto = tcp'");
701 "Unknown proto \"%s\".", hs_proto);
708 rbtree_finddata(home_servers_byaddr, home)) {
709 cf_log_err_cs(cs, "Duplicate home server");
714 * Check the TLS configuration.
716 tls = cf_section_sub_find(cs, "tls");
719 * If were doing RADSEC (tls+tcp) the secret should default
720 * to radsec, else a secret must be set.
724 if (tls && (home->proto == IPPROTO_TCP)) {
725 home->secret = "radsec";
729 cf_log_err_cs(cs, "No shared secret defined for home server %s", name2);
735 * If the home is a virtual server, don't look up source IP.
738 rad_assert(home->ipaddr.af != AF_UNSPEC);
741 * Otherwise look up the source IP using the same
742 * address family as the destination IP.
745 if (ip_hton(&home->src_ipaddr, home->ipaddr.af, hs_srcipaddr, false) < 0) {
746 cf_log_err_cs(cs, "Failed parsing src_ipaddr");
752 * Source isn't specified: Source is
753 * the correct address family, but all zeros.
755 memset(&home->src_ipaddr, 0, sizeof(home->src_ipaddr));
756 home->src_ipaddr.af = home->ipaddr.af;
759 if (tls && (home->proto != IPPROTO_TCP)) {
760 cf_log_err_cs(cs, "TLS transport is not available for UDP sockets");
767 cf_log_err_cs(cs, "TLS transport is not available in this executable");
772 * Parse the SSL client configuration.
775 home->tls = tls_client_conf_parse(tls);
783 cf_log_err_cs(cs, "Virtual home_servers cannot have a \"tls\" subsection");
789 return realm_home_server_add(home, cs, dual);
793 int realm_home_server_add(home_server_t *home, CONF_SECTION *cs, int dual)
795 const char *name2 = home->name;
796 CONF_SECTION *parent = NULL;
799 * The structs aren't mutex protected. Refuse to destroy
802 if (realms_initialized && !realm_config->dynamic) {
803 DEBUG("Must set \"dynamic = true\" in proxy.conf");
808 * Make sure that this is set.
810 if (home->src_ipaddr.af == AF_UNSPEC) {
811 home->src_ipaddr.af = home->ipaddr.af;
814 if (rbtree_finddata(home_servers_byname, home) != NULL) {
816 "Duplicate home server name %s.", name2);
821 (rbtree_finddata(home_servers_byaddr, home) != NULL)) {
823 "Duplicate home server IP %s.", name2);
827 if (!rbtree_insert(home_servers_byname, home)) {
829 "Internal error %d adding home server %s.",
835 !rbtree_insert(home_servers_byaddr, home)) {
836 rbtree_deletebydata(home_servers_byname, home);
838 "Internal error %d adding home server %s.",
844 home->number = home_server_max_number++;
845 if (!rbtree_insert(home_servers_bynumber, home)) {
846 rbtree_deletebydata(home_servers_byname, home);
847 if (home->ipaddr.af != AF_UNSPEC) {
848 rbtree_deletebydata(home_servers_byname, home);
851 "Internal error %d adding home server %s.",
857 FR_INTEGER_BOUND_CHECK("max_outstanding", home->max_outstanding, >=, 8);
858 FR_INTEGER_BOUND_CHECK("max_outstanding", home->max_outstanding, <=, 65536*16);
860 FR_INTEGER_BOUND_CHECK("ping_interval", home->ping_interval, >=, 6);
861 FR_INTEGER_BOUND_CHECK("ping_interval", home->ping_interval, <=, 120);
863 FR_TIMEVAL_BOUND_CHECK("response_window", &home->response_window, >=, 0, 1000);
864 FR_TIMEVAL_BOUND_CHECK("response_window", &home->response_window, <=, 60, 0);
865 FR_TIMEVAL_BOUND_CHECK("response_window", &home->response_window, <=,
866 main_config.max_request_time, 0);
868 FR_INTEGER_BOUND_CHECK("response_timeouts", home->max_response_timeouts, >=, 1);
869 FR_INTEGER_BOUND_CHECK("response_timeouts", home->max_response_timeouts, <=, 1000);
872 * Track the minimum response window, so that we can
873 * correctly set the timers in process.c
875 if (timercmp(&main_config.init_delay, &home->response_window, >)) {
876 main_config.init_delay = home->response_window;
879 FR_INTEGER_BOUND_CHECK("zombie_period", home->zombie_period, >=, 1);
880 FR_INTEGER_BOUND_CHECK("zombie_period", home->zombie_period, <=, 120);
881 FR_INTEGER_BOUND_CHECK("zombie_period", home->zombie_period, >=, (uint32_t) home->response_window.tv_sec);
883 FR_INTEGER_BOUND_CHECK("num_pings_to_alive", home->num_pings_to_alive, >=, 3);
884 FR_INTEGER_BOUND_CHECK("num_pings_to_alive", home->num_pings_to_alive, <=, 10);
886 FR_INTEGER_BOUND_CHECK("check_timeout", home->ping_timeout, >=, 1);
887 FR_INTEGER_BOUND_CHECK("check_timeout", home->ping_timeout, <=, 10);
889 FR_INTEGER_BOUND_CHECK("revive_interval", home->revive_interval, >=, 60);
890 FR_INTEGER_BOUND_CHECK("revive_interval", home->revive_interval, <=, 3600);
893 FR_INTEGER_BOUND_CHECK("coa_irt", home->coa_irt, >=, 1);
894 FR_INTEGER_BOUND_CHECK("coa_irt", home->coa_irt, <=, 5);
896 FR_INTEGER_BOUND_CHECK("coa_mrc", home->coa_mrc, <=, 20);
898 FR_INTEGER_BOUND_CHECK("coa_mrt", home->coa_mrt, <=, 30);
900 FR_INTEGER_BOUND_CHECK("coa_mrd", home->coa_mrd, >=, 5);
901 FR_INTEGER_BOUND_CHECK("coa_mrd", home->coa_mrd, <=, 60);
904 FR_INTEGER_BOUND_CHECK("max_connections", home->limit.max_connections, <=, 1024);
908 * UDP sockets can't be connection limited.
910 if (home->proto != IPPROTO_TCP) home->limit.max_connections = 0;
913 if ((home->limit.idle_timeout > 0) && (home->limit.idle_timeout < 5))
914 home->limit.idle_timeout = 5;
915 if ((home->limit.lifetime > 0) && (home->limit.lifetime < 5))
916 home->limit.lifetime = 5;
917 if ((home->limit.lifetime > 0) && (home->limit.idle_timeout > home->limit.lifetime))
918 home->limit.idle_timeout = 0;
920 parent = cf_item_parent(cf_sectiontoitem(cs));
921 if (strcmp(cf_section_name1(parent), "server") == 0) {
922 home->parent_server = cf_section_name2(parent);
926 home_server_t *home2 = talloc(home, home_server_t);
928 memcpy(home2, home, sizeof(*home2));
930 home2->type = HOME_TYPE_ACCT;
932 home2->ping_user_password = NULL;
934 home2->parent_server = home->parent_server;
936 if (!rbtree_insert(home_servers_byname, home2)) {
938 "Internal error %d adding home server %s.",
945 !rbtree_insert(home_servers_byaddr, home2)) {
946 rbtree_deletebydata(home_servers_byname, home2);
948 "Internal error %d adding home server %s.",
956 home2->number = home_server_max_number++;
957 if (!rbtree_insert(home_servers_bynumber, home2)) {
958 rbtree_deletebydata(home_servers_byname, home2);
959 if (!home2->server) {
960 rbtree_deletebydata(home_servers_byname, home2);
963 "Internal error %d adding home server %s.",
972 * Mark it as already processed
974 cf_data_add(cs, "home_server", null_free, null_free);
980 static home_pool_t *server_pool_alloc(char const *name, home_pool_type_t type,
981 int server_type, int num_home_servers)
985 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
987 if (!pool) return NULL; /* just for pairanoia */
989 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
994 pool->server_type = server_type;
995 pool->num_home_servers = num_home_servers;
1001 * Ensure any home_server clauses in a home_server_pool section reference
1002 * defined home servers, which should already have been created, regardless
1003 * of where they appear in the configuration.
1005 static int pool_check_home_server(UNUSED realm_config_t *rc, CONF_PAIR *cp,
1006 char const *name, int server_type,
1007 home_server_t **phome)
1009 home_server_t myhome, *home;
1013 "No value given for home_server");
1018 myhome.type = server_type;
1019 home = rbtree_finddata(home_servers_byname, &myhome);
1025 cf_log_err_cp(cp, "Unknown home_server \"%s\".", name);
1030 int realm_pool_add(home_pool_t *pool, UNUSED CONF_SECTION *cs)
1033 * The structs aren't mutex protected. Refuse to destroy
1036 if (realms_initialized && !realm_config->dynamic) {
1037 DEBUG("Must set \"dynamic = true\" in proxy.conf");
1041 if (!rbtree_insert(home_pools_byname, pool)) {
1042 rad_assert("Internal sanity check failed" == NULL);
1049 static int server_pool_add(realm_config_t *rc,
1050 CONF_SECTION *cs, int server_type, int do_print)
1053 home_pool_t *pool = NULL;
1056 int num_home_servers;
1057 home_server_t *home;
1059 name2 = cf_section_name1(cs);
1060 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
1061 (strcasecmp(name2, "home_server_pool") != 0))) {
1063 "Section is not a home_server_pool");
1067 name2 = cf_section_name2(cs);
1070 "Server pool section is missing a name");
1075 * Count the home servers and initalize them.
1077 num_home_servers = 0;
1078 for (cp = cf_pair_find(cs, "home_server");
1080 cp = cf_pair_find_next(cs, cp, "home_server")) {
1083 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
1084 server_type, &home)) {
1085 DEBUG("SHIT %d", __LINE__);
1090 if (num_home_servers == 0) {
1092 "No home servers defined in pool %s",
1097 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
1100 cf_log_err_cs(cs, "Failed allocating memory for pool");
1107 * Fallback servers must be defined, and must be
1110 cp = cf_pair_find(cs, "fallback");
1113 if (server_type == HOME_TYPE_COA) {
1114 cf_log_err_cs(cs, "Home server pools of type \"coa\" cannot have a fallback virtual server");
1119 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
1120 server_type, &pool->fallback)) {
1125 if (!pool->fallback->server) {
1126 cf_log_err_cs(cs, "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
1131 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
1133 cp = cf_pair_find(cs, "type");
1135 static FR_NAME_NUMBER pool_types[] = {
1136 { "load-balance", HOME_POOL_LOAD_BALANCE },
1138 { "fail-over", HOME_POOL_FAIL_OVER },
1139 { "fail_over", HOME_POOL_FAIL_OVER },
1141 { "round-robin", HOME_POOL_LOAD_BALANCE },
1142 { "round_robin", HOME_POOL_LOAD_BALANCE },
1144 { "client-balance", HOME_POOL_CLIENT_BALANCE },
1145 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
1146 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
1150 value = cf_pair_value(cp);
1153 "No value given for type");
1157 pool->type = fr_str2int(pool_types, value, 0);
1160 "Unknown type \"%s\".",
1165 if (do_print) cf_log_info(cs, "\ttype = %s", value);
1168 cp = cf_pair_find(cs, "virtual_server");
1170 pool->virtual_server = cf_pair_value(cp);
1171 if (!pool->virtual_server) {
1172 cf_log_err_cp(cp, "No value given for virtual_server");
1177 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
1180 if (!cf_section_sub_find_name2(rc->cs, "server",
1181 pool->virtual_server)) {
1182 cf_log_err_cp(cp, "No such server %s",
1183 pool->virtual_server);
1189 num_home_servers = 0;
1190 for (cp = cf_pair_find(cs, "home_server");
1192 cp = cf_pair_find_next(cs, cp, "home_server")) {
1193 home_server_t myhome;
1195 value = cf_pair_value(cp);
1197 memset(&myhome, 0, sizeof(myhome));
1198 myhome.name = value;
1199 myhome.type = server_type;
1201 home = rbtree_finddata(home_servers_byname, &myhome);
1203 DEBUG2("Internal sanity check failed");
1208 WARN("Duplicate home server %s in server pool %s", home->name, pool->name);
1212 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1213 pool->servers[num_home_servers++] = home;
1214 } /* loop over home_server's */
1216 if (pool->fallback && do_print) {
1217 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1220 if (!realm_pool_add(pool, cs)) goto error;
1222 if (do_print) cf_log_info(cs, " }");
1224 cf_data_add(cs, "home_server_pool", pool, free);
1226 rad_assert(pool->server_type != 0);
1231 if (do_print) cf_log_info(cs, " }");
1237 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1239 char const *name, char const *secret,
1240 home_pool_type_t ldflag, home_pool_t **pool_p,
1241 int type, char const *server)
1244 int i, insert_point, num_home_servers;
1245 home_server_t myhome, *home;
1246 home_pool_t mypool, *pool;
1247 CONF_SECTION *subcs;
1249 (void) rc; /* -Wunused */
1258 * LOCAL realms get sanity checked, and nothing else happens.
1260 if (strcmp(name, "LOCAL") == 0) {
1262 cf_log_err_cs(cs, "Realm \"%s\" cannot be both LOCAL and remote", name);
1269 return 0; /* Not proxying. Can't do non-LOCAL realms */
1272 mypool.name = realm;
1273 mypool.server_type = type;
1274 pool = rbtree_finddata(home_pools_byname, &mypool);
1276 if (pool->type != ldflag) {
1277 cf_log_err_cs(cs, "Inconsistent ldflag for server pool \"%s\"", name);
1281 if (pool->server_type != type) {
1282 cf_log_err_cs(cs, "Inconsistent home server type for server pool \"%s\"", name);
1289 home = rbtree_finddata(home_servers_byname, &myhome);
1291 if (secret && (strcmp(home->secret, secret) != 0)) {
1292 cf_log_err_cs(cs, "Inconsistent shared secret for home server \"%s\"", name);
1296 if (home->type != type) {
1297 cf_log_err_cs(cs, "Inconsistent type for home server \"%s\"", name);
1302 * See if the home server is already listed
1303 * in the pool. If so, do nothing else.
1305 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1306 if (pool->servers[i] == home) {
1313 * If we do have a pool, check that there is room to
1314 * insert the home server we've found, or the one that we
1317 * Note that we insert it into the LAST available
1318 * position, in order to maintain the same order as in
1319 * the configuration files.
1323 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1324 if (pool->servers[i]) break;
1326 if (!pool->servers[i]) {
1331 if (insert_point < 0) {
1332 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);
1338 * No home server, allocate one.
1344 home = talloc_zero(rc, home_server_t);
1346 home->hostname = name;
1348 home->secret = secret;
1350 home->proto = IPPROTO_UDP;
1352 p = strchr(name, ':');
1354 if (type == HOME_TYPE_AUTH) {
1355 home->port = PW_AUTH_UDP_PORT;
1357 home->port = PW_ACCT_UDP_PORT;
1363 } else if (p == name) {
1364 cf_log_err_cs(cs, "Invalid hostname %s", name);
1368 unsigned long port = strtoul(p + 1, NULL, 0);
1369 if ((port == 0) || (port > 65535)) {
1370 cf_log_err_cs(cs, "Invalid port %s", p + 1);
1375 home->port = (uint16_t)port;
1376 q = talloc_array(home, char, (p - name) + 1);
1377 memcpy(q, name, (p - name));
1383 if (ip_hton(&home->ipaddr, AF_UNSPEC, p, false) < 0) {
1385 "Failed looking up hostname %s.",
1391 home->src_ipaddr.af = home->ipaddr.af;
1393 home->ipaddr.af = AF_UNSPEC;
1394 home->server = server;
1399 * Use the old-style configuration.
1401 home->max_outstanding = 65535*16;
1402 home->zombie_period = rc->retry_delay * rc->retry_count;
1403 if (home->zombie_period < 2) home->zombie_period = 30;
1404 home->response_window.tv_sec = home->zombie_period - 1;
1405 home->response_window.tv_usec = 0;
1407 home->ping_check = HOME_PING_CHECK_NONE;
1409 home->revive_interval = rc->dead_time;
1411 if (rbtree_finddata(home_servers_byaddr, home)) {
1412 cf_log_err_cs(cs, "Home server %s has the same IP address and/or port as another home server.", name);
1417 if (!rbtree_insert(home_servers_byname, home)) {
1418 cf_log_err_cs(cs, "Internal error %d adding home server %s.", __LINE__, name);
1423 if (!rbtree_insert(home_servers_byaddr, home)) {
1424 rbtree_deletebydata(home_servers_byname, home);
1425 cf_log_err_cs(cs, "Internal error %d adding home server %s.", __LINE__, name);
1431 home->number = home_server_max_number++;
1432 if (!rbtree_insert(home_servers_bynumber, home)) {
1433 rbtree_deletebydata(home_servers_byname, home);
1434 if (home->ipaddr.af != AF_UNSPEC) {
1435 rbtree_deletebydata(home_servers_byname, home);
1438 "Internal error %d adding home server %s.",
1447 * We now have a home server, see if we can insert it
1448 * into pre-existing pool.
1450 if (insert_point >= 0) {
1451 rad_assert(pool != NULL);
1452 pool->servers[insert_point] = home;
1456 rad_assert(pool == NULL);
1457 rad_assert(home != NULL);
1460 * Count the old-style realms of this name.
1462 num_home_servers = 0;
1463 for (subcs = cf_section_find_next(cs, NULL, "realm");
1465 subcs = cf_section_find_next(cs, subcs, "realm")) {
1466 char const *this = cf_section_name2(subcs);
1468 if (!this || (strcmp(this, realm) != 0)) continue;
1472 if (num_home_servers == 0) {
1473 cf_log_err_cs(cs, "Internal error counting pools for home server %s.", name);
1478 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1480 cf_log_err_cs(cs, "Out of memory");
1486 pool->servers[0] = home;
1488 if (!rbtree_insert(home_pools_byname, pool)) {
1489 rad_assert("Internal sanity check failed" == NULL);
1499 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1502 char const *secret = NULL;
1503 home_pool_type_t ldflag;
1506 cp = cf_pair_find(cs, "ldflag");
1507 ldflag = HOME_POOL_FAIL_OVER;
1509 host = cf_pair_value(cp);
1511 cf_log_err_cp(cp, "No value specified for ldflag");
1515 if (strcasecmp(host, "fail_over") == 0) {
1516 cf_log_info(cs, "\tldflag = fail_over");
1518 } else if (strcasecmp(host, "round_robin") == 0) {
1519 ldflag = HOME_POOL_LOAD_BALANCE;
1520 cf_log_info(cs, "\tldflag = round_robin");
1523 cf_log_err_cs(cs, "Unknown value \"%s\" for ldflag", host);
1526 } /* else don't print it. */
1529 * Allow old-style if it doesn't exist, or if it exists and
1532 cp = cf_pair_find(cs, "authhost");
1534 host = cf_pair_value(cp);
1536 cf_log_err_cp(cp, "No value specified for authhost");
1540 if (strcmp(host, "LOCAL") != 0) {
1541 cp = cf_pair_find(cs, "secret");
1543 cf_log_err_cs(cs, "No shared secret supplied for realm: %s", r->name);
1547 secret = cf_pair_value(cp);
1549 cf_log_err_cp(cp, "No value specified for secret");
1554 cf_log_info(cs, "\tauthhost = %s", host);
1556 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1557 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1562 cp = cf_pair_find(cs, "accthost");
1564 host = cf_pair_value(cp);
1566 cf_log_err_cp(cp, "No value specified for accthost");
1571 * Don't look for a secret again if it was found
1574 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1575 cp = cf_pair_find(cs, "secret");
1577 cf_log_err_cs(cs, "No shared secret supplied for realm: %s", r->name);
1581 secret = cf_pair_value(cp);
1583 cf_log_err_cp(cp, "No value specified for secret");
1588 cf_log_info(cs, "\taccthost = %s", host);
1590 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1591 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1596 cp = cf_pair_find(cs, "virtual_server");
1598 host = cf_pair_value(cp);
1600 cf_log_err_cp(cp, "No value specified for virtual_server");
1604 cf_log_info(cs, "\tvirtual_server = %s", host);
1606 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1607 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1610 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1611 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1616 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1624 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1625 char const *name, home_pool_t **dest,
1626 int server_type, int do_print)
1628 home_pool_t mypool, *pool;
1631 mypool.server_type = server_type;
1633 pool = rbtree_finddata(home_pools_byname, &mypool);
1635 CONF_SECTION *pool_cs;
1637 pool_cs = cf_section_sub_find_name2(rc->cs,
1641 pool_cs = cf_section_sub_find_name2(rc->cs,
1646 cf_log_err_cs(cs, "Failed to find home_server_pool \"%s\"", name);
1650 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1654 pool = rbtree_finddata(home_pools_byname, &mypool);
1656 ERROR("Internal sanity check failed in add_pool_to_realm");
1661 if (pool->server_type != server_type) {
1662 cf_log_err_cs(cs, "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1673 int realms_realm_add (REALM *r, CONF_SECTION *cs)
1677 * It's a regex. Add it to a separate list.
1679 if (r->name[0] == '~') {
1680 realm_regex_t *rr, **last;
1682 rr = rad_malloc(sizeof(*rr));
1684 last = &realms_regex;
1685 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1696 if (!rbtree_insert(realms_byname, r)) {
1697 rad_assert("Internal sanity check failed");
1705 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1711 home_pool_t *auth_pool, *acct_pool;
1712 char const *auth_pool_name, *acct_pool_name;
1714 char const *coa_pool_name;
1715 home_pool_t *coa_pool;
1719 name2 = cf_section_name1(cs);
1720 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1721 cf_log_err_cs(cs, "Section is not a realm");
1725 name2 = cf_section_name2(cs);
1727 cf_log_err_cs(cs, "Realm section is missing the realm name");
1732 auth_pool = acct_pool = NULL;
1733 auth_pool_name = acct_pool_name = NULL;
1736 coa_pool_name = NULL;
1740 * Prefer new configuration to old one.
1742 cp = cf_pair_find(cs, "pool");
1743 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1744 if (cp) auth_pool_name = cf_pair_value(cp);
1745 if (cp && auth_pool_name) {
1746 acct_pool_name = auth_pool_name;
1747 if (!add_pool_to_realm(rc, cs,
1748 auth_pool_name, &auth_pool,
1749 HOME_TYPE_AUTH, 1)) {
1752 if (!add_pool_to_realm(rc, cs,
1753 auth_pool_name, &acct_pool,
1754 HOME_TYPE_ACCT, 0)) {
1759 cp = cf_pair_find(cs, "auth_pool");
1760 if (cp) auth_pool_name = cf_pair_value(cp);
1761 if (cp && auth_pool_name) {
1763 cf_log_err_cs(cs, "Cannot use \"pool\" and \"auth_pool\" at the same time");
1766 if (!add_pool_to_realm(rc, cs,
1767 auth_pool_name, &auth_pool,
1768 HOME_TYPE_AUTH, 1)) {
1773 cp = cf_pair_find(cs, "acct_pool");
1774 if (cp) acct_pool_name = cf_pair_value(cp);
1775 if (cp && acct_pool_name) {
1776 bool do_print = true;
1779 cf_log_err_cs(cs, "Cannot use \"pool\" and \"acct_pool\" at the same time");
1785 (strcmp(auth_pool_name, acct_pool_name) != 0))) {
1789 if (!add_pool_to_realm(rc, cs,
1790 acct_pool_name, &acct_pool,
1791 HOME_TYPE_ACCT, do_print)) {
1797 cp = cf_pair_find(cs, "coa_pool");
1798 if (cp) coa_pool_name = cf_pair_value(cp);
1799 if (cp && coa_pool_name) {
1800 bool do_print = true;
1802 if (!add_pool_to_realm(rc, cs,
1803 coa_pool_name, &coa_pool,
1804 HOME_TYPE_COA, do_print)) {
1811 cf_log_info(cs, " realm %s {", name2);
1815 * The realm MAY already exist if it's an old-style realm.
1816 * In that case, merge the old-style realm with this one.
1818 r = realm_find2(name2);
1819 if (r && (strcmp(r->name, name2) == 0)) {
1820 if (cf_pair_find(cs, "auth_pool") ||
1821 cf_pair_find(cs, "acct_pool")) {
1822 cf_log_err_cs(cs, "Duplicate realm \"%s\"", name2);
1826 if (!old_realm_config(rc, cs, r)) {
1830 cf_log_info(cs, " } # realm %s", name2);
1835 r = rad_malloc(sizeof(*r));
1836 memset(r, 0, sizeof(*r));
1841 r->auth_pool = auth_pool;
1842 r->acct_pool = acct_pool;
1844 r->coa_pool = coa_pool;
1847 if (auth_pool_name &&
1848 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1849 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1851 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1852 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1854 if (coa_pool_name) cf_log_info(cs, "\tcoa_pool = %s", coa_pool_name);
1859 cp = cf_pair_find(cs, "nostrip");
1860 if (cp && (cf_pair_value(cp) == NULL)) {
1862 cf_log_info(cs, "\tnostrip");
1866 * We're a new-style realm. Complain if we see the old
1869 if (r->auth_pool || r->acct_pool) {
1870 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1871 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1872 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1873 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1874 WARN("Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1879 * The realm MAY be an old-style realm, as there
1880 * was no auth_pool or acct_pool. Double-check
1881 * it, just to be safe.
1883 } else if (!old_realm_config(rc, cs, r)) {
1887 if (!realm_realm_add(r, cs)) {
1891 cf_log_info(cs, " }");
1896 cf_log_info(cs, " } # realm %s", name2);
1902 int realm_realm_add(REALM *r, CONF_SECTION *cs)
1905 * The structs aren't mutex protected. Refuse to destroy
1908 if (realms_initialized && !realm_config->dynamic) {
1909 DEBUG("Must set \"dynamic = true\" in proxy.conf");
1915 * It's a regex. Sanity check it, and add it to a
1918 if (r->name[0] == '~') {
1920 realm_regex_t *rr, **last;
1924 * Include substring matches.
1926 rcode = regcomp(®, r->name + 1, REG_EXTENDED | REG_NOSUB | REG_ICASE);
1930 regerror(rcode, ®, buffer, sizeof(buffer));
1933 "Invalid regex \"%s\": %s",
1934 r->name + 1, buffer);
1939 rr = rad_malloc(sizeof(*rr));
1941 last = &realms_regex;
1942 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1952 if (!rbtree_insert(realms_byname, r)) {
1953 rad_assert("Internal sanity check failed" == NULL);
1961 static const FR_NAME_NUMBER home_server_types[] = {
1962 { "auth", HOME_TYPE_AUTH },
1963 { "auth+acct", HOME_TYPE_AUTH },
1964 { "acct", HOME_TYPE_ACCT },
1965 { "coa", HOME_TYPE_COA },
1969 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1972 char const *name, *type;
1974 CONF_SECTION *server_cs;
1976 cp = cf_pair_find(cs, "home_server");
1978 cf_log_err_cs(cs, "Pool does not contain a \"home_server\" entry");
1979 return HOME_TYPE_INVALID;
1982 name = cf_pair_value(cp);
1984 cf_log_err_cp(cp, "home_server entry does not reference a home server");
1985 return HOME_TYPE_INVALID;
1988 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1990 cf_log_err_cp(cp, "home_server \"%s\" does not exist", name);
1991 return HOME_TYPE_INVALID;
1994 cp = cf_pair_find(server_cs, "type");
1996 cf_log_err_cs(server_cs, "home_server %s does not contain a \"type\" entry", name);
1997 return HOME_TYPE_INVALID;
2000 type = cf_pair_value(cp);
2002 cf_log_err_cs(server_cs, "home_server %s contains an empty \"type\" entry", name);
2003 return HOME_TYPE_INVALID;
2006 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
2007 if (home == HOME_TYPE_INVALID) {
2008 cf_log_err_cs(server_cs, "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
2009 return HOME_TYPE_INVALID;
2012 return home; /* 'cause we miss it so much */
2016 int realms_init(CONF_SECTION *config)
2021 CONF_SECTION *server_cs;
2025 if (realms_initialized) return 1;
2027 rc = talloc_zero(NULL, realm_config_t);
2031 cs = cf_subsection_find_next(config, NULL, "proxy");
2033 if (cf_section_parse(cs, rc, proxy_config) < 0) {
2034 ERROR("Failed parsing proxy section");
2038 rc->dead_time = DEAD_TIME;
2039 rc->retry_count = RETRY_COUNT;
2040 rc->retry_delay = RETRY_DELAY;
2041 rc->fallback = false;
2042 rc->dynamic = false;
2043 rc->wake_all_if_all_dead= 0;
2047 flags = RBTREE_FLAG_LOCK;
2050 home_servers_byaddr = rbtree_create(NULL, home_server_addr_cmp, home_server_free, flags);
2051 if (!home_servers_byaddr) goto error;
2053 home_servers_byname = rbtree_create(NULL, home_server_name_cmp, NULL, flags);
2054 if (!home_servers_byname) goto error;
2057 home_servers_bynumber = rbtree_create(NULL, home_server_number_cmp, NULL, flags);
2058 if (!home_servers_bynumber) goto error;
2061 home_pools_byname = rbtree_create(NULL, home_pool_name_cmp, NULL, flags);
2062 if (!home_pools_byname) goto error;
2064 for (cs = cf_subsection_find_next(config, NULL, "home_server");
2066 cs = cf_subsection_find_next(config, cs, "home_server")) {
2067 if (!home_server_add(rc, cs)) goto error;
2071 * Loop over virtual servers to find home servers which
2072 * are defined in them.
2074 for (server_cs = cf_subsection_find_next(config, NULL, "server");
2076 server_cs = cf_subsection_find_next(config, server_cs, "server")) {
2077 for (cs = cf_subsection_find_next(server_cs, NULL, "home_server");
2079 cs = cf_subsection_find_next(server_cs, cs, "home_server")) {
2080 if (!home_server_add(rc, cs)) goto error;
2086 * Now create the realms, which point to the home servers
2087 * and home server pools.
2089 realms_byname = rbtree_create(NULL, realm_name_cmp, NULL, flags);
2090 if (!realms_byname) goto error;
2092 for (cs = cf_subsection_find_next(config, NULL, "realm");
2094 cs = cf_subsection_find_next(config, cs, "realm")) {
2095 if (!realm_add(rc, cs)) {
2099 * Must be called after realms_free as home_servers
2100 * parented by rc are in trees freed by realms_free()
2109 * CoA pools aren't necessarily tied to realms.
2111 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
2113 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
2117 * Pool was already loaded.
2119 if (cf_data_find(cs, "home_server_pool")) continue;
2121 type = pool_peek_type(config, cs);
2122 if (type == HOME_TYPE_INVALID) goto error;
2123 if (!server_pool_add(rc, cs, type, true)) goto error;
2128 xlat_register("home_server", xlat_home_server, NULL, NULL);
2129 xlat_register("home_server_pool", xlat_server_pool, NULL, NULL);
2133 realms_initialized = true;
2138 * Find a realm where "name" might be the regex.
2140 REALM *realm_find2(char const *name)
2145 if (!name) name = "NULL";
2147 myrealm.name = name;
2148 realm = rbtree_finddata(realms_byname, &myrealm);
2149 if (realm) return realm;
2153 realm_regex_t *this;
2155 for (this = realms_regex; this != NULL; this = this->next) {
2156 if (strcmp(this->realm->name, name) == 0) {
2164 * Couldn't find a realm. Look for DEFAULT.
2166 myrealm.name = "DEFAULT";
2167 return rbtree_finddata(realms_byname, &myrealm);
2172 * Find a realm in the REALM list.
2174 REALM *realm_find(char const *name)
2179 if (!name) name = "NULL";
2181 myrealm.name = name;
2182 realm = rbtree_finddata(realms_byname, &myrealm);
2183 if (realm) return realm;
2187 realm_regex_t *this;
2189 for (this = realms_regex; this != NULL; this = this->next) {
2194 * Include substring matches.
2196 if (regcomp(®, this->realm->name + 1, REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
2200 compare = regexec(®, name, 0, NULL, 0);
2203 if (compare == 0) return this->realm;
2209 * Couldn't find a realm. Look for DEFAULT.
2211 myrealm.name = "DEFAULT";
2212 return rbtree_finddata(realms_byname, &myrealm);
2219 * Allocate the proxy list if it doesn't already exist, and copy request
2220 * VPs into it. Setup src/dst IP addresses based on home server, and
2221 * calculate and add the message-authenticator.
2223 * This is a distinct function from home_server_ldb, as not all home_server_t
2224 * lookups result in the *CURRENT* request being proxied,
2225 * as in rlm_replicate, and this may trigger asserts elsewhere in the
2228 void home_server_update_request(home_server_t *home, REQUEST *request)
2232 * Allocate the proxy packet, only if it wasn't
2233 * already allocated by a module. This check is
2234 * mainly to support the proxying of EAP-TTLS and
2235 * EAP-PEAP tunneled requests.
2237 * In those cases, the EAP module creates a
2238 * "fake" request, and recursively passes it
2239 * through the authentication stage of the
2240 * server. The module then checks if the request
2241 * was supposed to be proxied, and if so, creates
2242 * a proxy packet from the TUNNELED request, and
2243 * not from the EAP request outside of the
2246 * The proxy then works like normal, except that
2247 * the response packet is "eaten" by the EAP
2248 * module, and encapsulated into an EAP packet.
2250 if (!request->proxy) {
2251 request->proxy = rad_alloc(request, true);
2252 if (!request->proxy) {
2259 * Copy the request, then look up name
2260 * and plain-text password in the copy.
2262 * Note that the User-Name attribute is
2263 * the *original* as sent over by the
2264 * client. The Stripped-User-Name
2265 * attribute is the one hacked through
2268 request->proxy->vps = paircopy(request->proxy,
2269 request->packet->vps);
2273 * Update the various fields as appropriate.
2275 request->proxy->src_ipaddr = home->src_ipaddr;
2276 request->proxy->src_port = 0;
2277 request->proxy->dst_ipaddr = home->ipaddr;
2278 request->proxy->dst_port = home->port;
2280 request->proxy->proto = home->proto;
2282 request->home_server = home;
2285 * Access-Requests have a Message-Authenticator added,
2286 * unless one already exists.
2288 if ((request->packet->code == PW_CODE_ACCESS_REQUEST) &&
2289 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY)) {
2290 pairmake(request->proxy, &request->proxy->vps,
2291 "Message-Authenticator", "0x00",
2296 home_server_t *home_server_ldb(char const *realmname,
2297 home_pool_t *pool, REQUEST *request)
2301 home_server_t *found = NULL;
2302 home_server_t *zombie = NULL;
2306 * Determine how to pick choose the home server.
2308 switch (pool->type) {
2312 * For load-balancing by client IP address, we
2313 * pick a home server by hashing the client IP.
2315 * This isn't as even a load distribution as
2316 * tracking the State attribute, but it's better
2319 case HOME_POOL_CLIENT_BALANCE:
2320 switch (request->packet->src_ipaddr.af) {
2322 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2323 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2326 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2327 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2333 start = hash % pool->num_home_servers;
2336 case HOME_POOL_CLIENT_PORT_BALANCE:
2337 switch (request->packet->src_ipaddr.af) {
2339 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2340 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2343 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2344 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2350 fr_hash_update(&request->packet->src_port,
2351 sizeof(request->packet->src_port), hash);
2352 start = hash % pool->num_home_servers;
2355 case HOME_POOL_KEYED_BALANCE:
2356 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY, 0, TAG_ANY)) != NULL) {
2357 hash = fr_hash(vp->vp_strvalue, vp->length);
2358 start = hash % pool->num_home_servers;
2363 case HOME_POOL_LOAD_BALANCE:
2364 case HOME_POOL_FAIL_OVER:
2368 default: /* this shouldn't happen... */
2375 * Starting with the home server we chose, loop through
2376 * all home servers. If the current one is dead, skip
2377 * it. If it is too busy, skip it.
2379 * Otherwise, use it.
2381 for (count = 0; count < pool->num_home_servers; count++) {
2382 home_server_t *home = pool->servers[(start + count) % pool->num_home_servers];
2384 if (!home) continue;
2387 * Skip dead home servers.
2389 * Home servers that are unknown, alive, or zombie
2390 * are used for proxying.
2392 if (home->state == HOME_STATE_IS_DEAD) {
2397 * This home server is too busy. Choose another one.
2399 if (home->currently_outstanding >= home->max_outstanding) {
2405 * We read the packet from a detail file, AND it
2406 * came from this server. Don't re-proxy it
2409 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2410 (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) &&
2411 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2417 * Default virtual: ignore homes tied to a
2420 if (!request->server && home->parent_server) {
2425 * A virtual AND home is tied to virtual,
2426 * ignore ones which don't match.
2428 if (request->server && home->parent_server &&
2429 strcmp(request->server, home->parent_server) != 0) {
2434 * Allow request->server && !home->parent_server
2436 * i.e. virtuals can proxy to globally defined
2441 * It's zombie, so we remember the first zombie
2442 * we find, but we don't mark it as a "live"
2445 if (home->state == HOME_STATE_ZOMBIE) {
2446 if (!zombie) zombie = home;
2451 * We've found the first "live" one. Use that.
2453 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2459 * Otherwise we're doing some kind of load balancing.
2460 * If we haven't found one yet, pick this one.
2467 RDEBUG3("PROXY %s %d\t%s %d",
2468 found->name, found->currently_outstanding,
2469 home->name, home->currently_outstanding);
2472 * Prefer this server if it's less busy than the
2473 * one we had previously found.
2475 if (home->currently_outstanding < found->currently_outstanding) {
2476 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2477 home->name, found->name);
2483 * Ignore servers which are busier than the one
2486 if (home->currently_outstanding > found->currently_outstanding) {
2487 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2488 home->name, found->name);
2493 * From the list of servers which have the same
2494 * load, choose one at random.
2496 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2499 } /* loop over the home servers */
2502 * We have no live servers, BUT we have a zombie. Use
2503 * the zombie as a last resort.
2505 if (!found && zombie) {
2511 * There's a fallback if they're all dead.
2513 if (!found && pool->fallback) {
2514 found = pool->fallback;
2516 WARN("Home server pool %s failing over to fallback %s",
2517 pool->name, found->server);
2518 if (pool->in_fallback) goto update_and_return;
2520 pool->in_fallback = true;
2523 * Run the trigger once an hour saying that
2526 if ((pool->time_all_dead + 3600) < request->timestamp) {
2527 pool->time_all_dead = request->timestamp;
2528 exec_trigger(request, pool->cs, "home_server_pool.fallback", false);
2534 if ((found != pool->fallback) && pool->in_fallback) {
2535 pool->in_fallback = false;
2536 exec_trigger(request, pool->cs, "home_server_pool.normal", false);
2543 * No live match found, and no fallback to the "DEFAULT"
2544 * realm. We fix this by blindly marking all servers as
2545 * "live". But only do it for ones that don't support
2546 * "pings", as they will be marked live when they
2547 * actually are live.
2549 if (!realm_config->fallback &&
2550 realm_config->wake_all_if_all_dead) {
2551 for (count = 0; count < pool->num_home_servers; count++) {
2552 home_server_t *home = pool->servers[count];
2554 if (!home) continue;
2556 if ((home->state == HOME_STATE_IS_DEAD) &&
2557 (home->ping_check == HOME_PING_CHECK_NONE)) {
2558 home->state = HOME_STATE_ALIVE;
2559 home->response_timeouts = 0;
2560 if (!found) found = home;
2564 if (found) goto update_and_return;
2568 * Still nothing. Look up the DEFAULT realm, but only
2569 * if we weren't looking up the NULL or DEFAULT realms.
2571 if (realm_config->fallback &&
2573 (strcmp(realmname, "NULL") != 0) &&
2574 (strcmp(realmname, "DEFAULT") != 0)) {
2575 REALM *rd = realm_find("DEFAULT");
2577 if (!rd) return NULL;
2580 if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
2581 pool = rd->auth_pool;
2583 } else if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
2584 pool = rd->acct_pool;
2586 if (!pool) return NULL;
2588 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2589 return home_server_ldb(rd->name, pool, request);
2593 * Still haven't found anything. Oh well.
2599 home_server_t *home_server_find(fr_ipaddr_t *ipaddr, uint16_t port, int proto)
2601 home_server_t myhome;
2603 memset(&myhome, 0, sizeof(myhome));
2604 myhome.ipaddr = *ipaddr;
2605 myhome.src_ipaddr.af = ipaddr->af;
2608 myhome.proto = proto;
2610 myhome.proto = IPPROTO_UDP;
2612 myhome.server = NULL; /* we're not called for internal proxying */
2614 return rbtree_finddata(home_servers_byaddr, &myhome);
2618 home_server_t *home_server_byname(char const *name, int type)
2620 home_server_t myhome;
2622 memset(&myhome, 0, sizeof(myhome));
2626 return rbtree_finddata(home_servers_byname, &myhome);
2631 home_server_t *home_server_bynumber(int number)
2633 home_server_t myhome;
2635 memset(&myhome, 0, sizeof(myhome));
2636 myhome.number = number;
2637 myhome.server = NULL; /* we're not called for internal proxying */
2639 return rbtree_finddata(home_servers_bynumber, &myhome);
2643 home_pool_t *home_pool_byname(char const *name, int type)
2647 memset(&mypool, 0, sizeof(mypool));
2649 mypool.server_type = type;
2650 return rbtree_finddata(home_pools_byname, &mypool);