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 *home = data;
112 static int home_server_name_cmp(void const *one, void const *two)
114 home_server const *a = one;
115 home_server 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 const *a = one;
127 home_server 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 const *a = one;
155 home_server 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, limit.max_connections), NULL, "16" },
285 { "max_requests", PW_TYPE_INTEGER,
286 offsetof(home_server, limit.max_requests), NULL, "0" },
288 { "lifetime", PW_TYPE_INTEGER,
289 offsetof(home_server, limit.lifetime), NULL, "0" },
291 { "idle_timeout", PW_TYPE_INTEGER,
292 offsetof(home_server, 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, coa_irt), 0, STRINGIFY(2) },
311 { "mrt", PW_TYPE_INTEGER,
312 offsetof(home_server, coa_mrt), 0, STRINGIFY(16) },
313 { "mrc", PW_TYPE_INTEGER,
314 offsetof(home_server, coa_mrc), 0, STRINGIFY(5) },
315 { "mrd", PW_TYPE_INTEGER,
316 offsetof(home_server, 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,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,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,response_window), NULL, "30" },
349 { "max_outstanding", PW_TYPE_INTEGER,
350 offsetof(home_server,max_outstanding), NULL, "65536" },
352 { "zombie_period", PW_TYPE_INTEGER,
353 offsetof(home_server,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,ping_interval), NULL, "30" },
361 { "check_interval", PW_TYPE_INTEGER,
362 offsetof(home_server,ping_interval), NULL, "30" },
363 { "num_answers_to_alive", PW_TYPE_INTEGER,
364 offsetof(home_server,num_pings_to_alive), NULL, "3" },
365 { "revive_interval", PW_TYPE_INTEGER,
366 offsetof(home_server,revive_interval), NULL, "300" },
367 { "status_check_timeout", PW_TYPE_INTEGER,
368 offsetof(home_server,ping_timeout), NULL, "4" },
370 { "username", PW_TYPE_STRING_PTR,
371 offsetof(home_server,ping_user_name), NULL, NULL},
372 { "password", PW_TYPE_STRING_PTR,
373 offsetof(home_server,ping_user_password), NULL, NULL},
376 { "historic_average_window", PW_TYPE_INTEGER,
377 offsetof(home_server,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)
395 int realms_home_server_add(home_server *home, CONF_SECTION *cs, int dual)
397 CONF_SECTION *parent = NULL;
398 const char * name2 = home->name;
401 * Make sure that this is set.
403 if (home->src_ipaddr.af == AF_UNSPEC) {
404 home->src_ipaddr.af = home->ipaddr.af;
409 if (rbtree_finddata(home_servers_byname, home) != NULL) {
411 "Duplicate home server name %s.", name2);
416 (rbtree_finddata(home_servers_byaddr, home) != NULL)) {
418 "Duplicate home server IP %s.", name2);
422 if (!rbtree_insert(home_servers_byname, home)) {
424 "Internal error %d adding home server %s.",
430 !rbtree_insert(home_servers_byaddr, home)) {
431 rbtree_deletebydata(home_servers_byname, home);
433 "Internal error %d adding home server %s.",
439 home->number = home_server_max_number++;
440 if (!rbtree_insert(home_servers_bynumber, home)) {
441 rbtree_deletebydata(home_servers_byname, home);
442 if (home->ipaddr.af != AF_UNSPEC) {
443 rbtree_deletebydata(home_servers_byname, home);
446 "Internal error %d adding home server %s.",
452 if (home->max_outstanding < 8) home->max_outstanding = 8;
453 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
455 if (home->ping_interval < 6) home->ping_interval = 6;
456 if (home->ping_interval > 120) home->ping_interval = 120;
458 if (home->response_window < 1) home->response_window = 1;
459 if (home->response_window > 60) home->response_window = 60;
460 if (home->response_window > mainconfig.max_request_time) home->response_window = mainconfig.max_request_time;
462 if (home->zombie_period < 1) home->zombie_period = 1;
463 if (home->zombie_period > 120) home->zombie_period = 120;
465 if (home->zombie_period < home->response_window) {
466 home->zombie_period = home->response_window;
469 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
470 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
472 if (home->ping_timeout < 3) home->ping_timeout = 3;
473 if (home->ping_timeout > 10) home->ping_timeout = 10;
475 if (home->revive_interval < 60) home->revive_interval = 60;
476 if (home->revive_interval > 3600) home->revive_interval = 3600;
479 if (home->coa_irt < 1) home->coa_irt = 1;
480 if (home->coa_irt > 5) home->coa_irt = 5;
482 if (home->coa_mrc < 0) home->coa_mrc = 0;
483 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
485 if (home->coa_mrt < 0) home->coa_mrt = 0;
486 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
488 if (home->coa_mrd < 5) home->coa_mrd = 5;
489 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
492 if (home->limit.max_connections > 1024) home->limit.max_connections = 1024;
496 * UDP sockets can't be connection limited.
498 if (home->proto != IPPROTO_TCP) home->limit.max_connections = 0;
501 if ((home->limit.idle_timeout > 0) && (home->limit.idle_timeout < 5))
502 home->limit.idle_timeout = 5;
503 if ((home->limit.lifetime > 0) && (home->limit.lifetime < 5))
504 home->limit.lifetime = 5;
505 if ((home->limit.lifetime > 0) && (home->limit.idle_timeout > home->limit.lifetime))
506 home->limit.idle_timeout = 0;
508 parent = cf_item_parent(cf_sectiontoitem(cs));
509 if (parent && strcmp(cf_section_name1(parent), "server") == 0) {
510 home->parent_server = cf_section_name2(parent);
514 home_server *home2 = rad_malloc(sizeof(*home2));
516 memcpy(home2, home, sizeof(*home2));
518 home2->type = HOME_TYPE_ACCT;
520 home2->ping_user_password = NULL;
522 home2->parent_server = home->parent_server;
524 if (!rbtree_insert(home_servers_byname, home2)) {
526 "Internal error %d adding home server %s.",
533 !rbtree_insert(home_servers_byaddr, home2)) {
534 rbtree_deletebydata(home_servers_byname, home2);
536 "Internal error %d adding home server %s.",
543 home2->number = home_server_max_number++;
544 if (!rbtree_insert(home_servers_bynumber, home2)) {
545 rbtree_deletebydata(home_servers_byname, home2);
546 if (!home2->server) {
547 rbtree_deletebydata(home_servers_byname, home2);
550 "Internal error %d adding home server %s.",
564 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs)
572 hs_virtual_server = NULL;
574 name2 = cf_section_name2(cs);
577 "Home server section is missing a name.");
581 home = talloc_zero(rc, home_server);
585 home->state = HOME_STATE_UNKNOWN;
588 * Last packet sent / received are zero.
591 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
592 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
593 if (cf_section_parse(cs, home, home_server_config) < 0) {
598 * Figure out which one to use.
600 if (cf_pair_find(cs, "ipaddr")) {
601 home->ipaddr.af = AF_INET;
602 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
604 } else if (cf_pair_find(cs, "ipv6addr")) {
605 home->ipaddr.af = AF_INET6;
606 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
608 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
609 home->ipaddr.af = AF_UNSPEC;
610 home->server = cf_pair_value(cp);
613 "Invalid value for virtual_server");
617 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
620 "No such server %s", home->server);
625 * When CoA is used, the user has to specify the type
626 * of the home server, even when they point to
629 home->secret = strdup("");
634 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
637 talloc_free(hs_type);
647 if (!home->port || (home->port > 65535)) {
649 "No port, or invalid port defined for home server %s.",
656 "Fatal error! Home server %s is ourselves!",
663 "No shared secret defined for home server %s.",
669 * Use a reasonable default.
672 if (!hs_type) hs_type = talloc_strdup(cs, "auth+acct");
674 if (strcasecmp(hs_type, "auth") == 0) {
675 home->type = HOME_TYPE_AUTH;
677 } else if (strcasecmp(hs_type, "acct") == 0) {
678 home->type = HOME_TYPE_ACCT;
680 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
681 home->type = HOME_TYPE_AUTH;
685 } else if (strcasecmp(hs_type, "coa") == 0) {
686 home->type = HOME_TYPE_COA;
689 if (home->server != NULL) {
691 "Home servers of type \"coa\" cannot point to a virtual server");
698 "Invalid type \"%s\" for home server %s.",
702 if (hs_type) talloc_free(hs_type);
705 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
706 home->ping_check = HOME_PING_CHECK_NONE;
708 } else if (strcasecmp(hs_check, "status-server") == 0) {
709 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
711 } else if (strcasecmp(hs_check, "request") == 0) {
712 home->ping_check = HOME_PING_CHECK_REQUEST;
714 if (!home->ping_user_name ||
715 !*home->ping_user_name) {
716 cf_log_err_cs(cs, "You must supply a 'username' to enable status_check=request");
720 if ((home->type == HOME_TYPE_AUTH) &&
721 (!home->ping_user_password ||
722 !*home->ping_user_password)) {
723 cf_log_err_cs(cs, "You must supply a password to enable status_check=request");
729 "Invalid status__check \"%s\" for home server %s.",
735 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
736 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
737 if (!home->ping_user_name) {
738 cf_log_err_cs(cs, "You must supply a user name to enable status_check=request");
742 if ((home->type == HOME_TYPE_AUTH) &&
743 !home->ping_user_password) {
744 cf_log_err_cs(cs, "You must supply a password to enable status_check=request");
749 home->proto = IPPROTO_UDP;
752 if (strcmp(hs_proto, "udp") == 0) {
755 } else if (strcmp(hs_proto, "tcp") == 0) {
757 home->proto = IPPROTO_TCP;
759 if (home->ping_check != HOME_PING_CHECK_NONE) {
761 "Only 'status_check = none' is allowed for home servers with 'proto = tcp'");
767 "Unknown proto \"%s\".", hs_proto);
774 rbtree_finddata(home_servers_byaddr, home)) {
775 cf_log_err_cs(cs, "Duplicate home server");
780 * Check the TLS configuration.
782 tls = cf_section_sub_find(cs, "tls");
785 * If the home is a virtual server, don't look up source IP.
788 rad_assert(home->ipaddr.af != AF_UNSPEC);
791 * Otherwise look up the source IP using the same
792 * address family as the destination IP.
795 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
796 cf_log_err_cs(cs, "Failed parsing src_ipaddr");
802 * Source isn't specified: Source is
803 * the correct address family, but all zeros.
805 memset(&home->src_ipaddr, 0, sizeof(home->src_ipaddr));
806 home->src_ipaddr.af = home->ipaddr.af;
809 if (tls && (home->proto != IPPROTO_TCP)) {
810 cf_log_err_cs(cs, "TLS transport is not available for UDP sockets.");
817 cf_log_err_cs(cs, "TLS transport is not available in this executable.");
822 * Parse the SSL client configuration.
825 home->tls = tls_client_conf_parse(tls);
833 cf_log_err_cs(cs, "Virtual home_servers cannot have a \"tls\" subsection");
837 if ( !realms_home_server_add( home, cs, dual))
841 * Mark it as already processed
843 cf_data_add(cs, "home_server", null_free, null_free);
849 static home_pool_t *server_pool_alloc(char const *name, home_pool_type_t type,
850 int server_type, int num_home_servers)
854 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
856 if (!pool) return NULL; /* just for pairanoia */
858 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
863 pool->server_type = server_type;
864 pool->num_home_servers = num_home_servers;
870 * Ensure any home_server clauses in a home_server_pool section reference
871 * defined home servers, which should already have been created, regardless
872 * of where they appear in the configuration.
874 static int pool_check_home_server(UNUSED realm_config_t *rc, CONF_PAIR *cp,
875 char const *name, int server_type,
878 home_server myhome, *home;
882 "No value given for home_server.");
887 myhome.type = server_type;
888 home = rbtree_finddata(home_servers_byname, &myhome);
894 cf_log_err_cp(cp, "Unknown home_server \"%s\".", name);
899 int realms_pool_add( home_pool_t *pool, UNUSED CONF_SECTION *cs)
901 if (!rbtree_insert(home_pools_byname, pool)) {
902 rad_assert("Internal sanity check failed");
909 static int server_pool_add(realm_config_t *rc,
910 CONF_SECTION *cs, int server_type, int do_print)
913 home_pool_t *pool = NULL;
916 int num_home_servers;
919 name2 = cf_section_name1(cs);
920 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
921 (strcasecmp(name2, "home_server_pool") != 0))) {
923 "Section is not a home_server_pool.");
927 name2 = cf_section_name2(cs);
930 "Server pool section is missing a name.");
935 * Count the home servers and initalize them.
937 num_home_servers = 0;
938 for (cp = cf_pair_find(cs, "home_server");
940 cp = cf_pair_find_next(cs, cp, "home_server")) {
943 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
944 server_type, &home)) {
949 if (num_home_servers == 0) {
951 "No home servers defined in pool %s",
956 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
959 cf_log_err_cs(cs, "Failed allocating memory for pool");
966 * Fallback servers must be defined, and must be
969 cp = cf_pair_find(cs, "fallback");
972 if (server_type == HOME_TYPE_COA) {
973 cf_log_err_cs(cs, "Home server pools of type \"coa\" cannot have a fallback virtual server.");
978 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
979 server_type, &pool->fallback)) {
984 if (!pool->fallback->server) {
985 cf_log_err_cs(cs, "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
990 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
992 cp = cf_pair_find(cs, "type");
994 static FR_NAME_NUMBER pool_types[] = {
995 { "load-balance", HOME_POOL_LOAD_BALANCE },
997 { "fail-over", HOME_POOL_FAIL_OVER },
998 { "fail_over", HOME_POOL_FAIL_OVER },
1000 { "round-robin", HOME_POOL_LOAD_BALANCE },
1001 { "round_robin", HOME_POOL_LOAD_BALANCE },
1003 { "client-balance", HOME_POOL_CLIENT_BALANCE },
1004 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
1005 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
1009 value = cf_pair_value(cp);
1012 "No value given for type.");
1016 pool->type = fr_str2int(pool_types, value, 0);
1019 "Unknown type \"%s\".",
1024 if (do_print) cf_log_info(cs, "\ttype = %s", value);
1027 cp = cf_pair_find(cs, "virtual_server");
1029 pool->virtual_server = cf_pair_value(cp);
1030 if (!pool->virtual_server) {
1031 cf_log_err_cp(cp, "No value given for virtual_server");
1036 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
1039 if (!cf_section_sub_find_name2(rc->cs, "server",
1040 pool->virtual_server)) {
1041 cf_log_err_cp(cp, "No such server %s",
1042 pool->virtual_server);
1048 num_home_servers = 0;
1049 for (cp = cf_pair_find(cs, "home_server");
1051 cp = cf_pair_find_next(cs, cp, "home_server")) {
1054 value = cf_pair_value(cp);
1056 memset(&myhome, 0, sizeof(myhome));
1057 myhome.name = value;
1058 myhome.type = server_type;
1060 home = rbtree_finddata(home_servers_byname, &myhome);
1062 DEBUG2("Internal sanity check failed");
1067 WDEBUG2("Duplicate home server %s in server pool %s", home->name, pool->name);
1071 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1072 pool->servers[num_home_servers++] = home;
1073 } /* loop over home_server's */
1075 if (pool->fallback && do_print) {
1076 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1079 if (! realms_pool_add(pool, cs)) {
1083 if (do_print) cf_log_info(cs, " }");
1085 cf_data_add(cs, "home_server_pool", pool, free);
1087 rad_assert(pool->server_type != 0);
1092 if (do_print) cf_log_info(cs, " }");
1098 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1100 char const *name, char const *secret,
1101 home_pool_type_t ldflag, home_pool_t **pool_p,
1102 int type, char const *server)
1105 int i, insert_point, num_home_servers;
1106 home_server myhome, *home;
1107 home_pool_t mypool, *pool;
1108 CONF_SECTION *subcs;
1110 (void) rc; /* -Wunused */
1119 * LOCAL realms get sanity checked, and nothing else happens.
1121 if (strcmp(name, "LOCAL") == 0) {
1123 cf_log_err_cs(cs, "Realm \"%s\" cannot be both LOCAL and remote", name);
1130 return 0; /* Not proxying. Can't do non-LOCAL realms */
1133 mypool.name = realm;
1134 mypool.server_type = type;
1135 pool = rbtree_finddata(home_pools_byname, &mypool);
1137 if (pool->type != ldflag) {
1138 cf_log_err_cs(cs, "Inconsistent ldflag for server pool \"%s\"", name);
1142 if (pool->server_type != type) {
1143 cf_log_err_cs(cs, "Inconsistent home server type for server pool \"%s\"", name);
1150 home = rbtree_finddata(home_servers_byname, &myhome);
1152 if (secret && (strcmp(home->secret, secret) != 0)) {
1153 cf_log_err_cs(cs, "Inconsistent shared secret for home server \"%s\"", name);
1157 if (home->type != type) {
1158 cf_log_err_cs(cs, "Inconsistent type for home server \"%s\"", name);
1163 * See if the home server is already listed
1164 * in the pool. If so, do nothing else.
1166 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1167 if (pool->servers[i] == home) {
1174 * If we do have a pool, check that there is room to
1175 * insert the home server we've found, or the one that we
1178 * Note that we insert it into the LAST available
1179 * position, in order to maintain the same order as in
1180 * the configuration files.
1184 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1185 if (pool->servers[i]) break;
1187 if (!pool->servers[i]) {
1192 if (insert_point < 0) {
1193 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);
1199 * No home server, allocate one.
1205 home = rad_malloc(sizeof(*home));
1206 memset(home, 0, sizeof(*home));
1209 home->hostname = name;
1211 home->secret = secret;
1213 home->proto = IPPROTO_UDP;
1215 p = strchr(name, ':');
1217 if (type == HOME_TYPE_AUTH) {
1218 home->port = PW_AUTH_UDP_PORT;
1220 home->port = PW_ACCT_UDP_PORT;
1226 } else if (p == name) {
1228 "Invalid hostname %s.",
1234 home->port = atoi(p + 1);
1235 if ((home->port == 0) || (home->port > 65535)) {
1243 q = rad_malloc((p - name) + 1);
1244 memcpy(q, name, (p - name));
1250 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1252 "Failed looking up hostname %s.",
1258 home->src_ipaddr.af = home->ipaddr.af;
1260 home->ipaddr.af = AF_UNSPEC;
1261 home->server = server;
1266 * Use the old-style configuration.
1268 home->max_outstanding = 65535*16;
1269 home->zombie_period = rc->retry_delay * rc->retry_count;
1270 if (home->zombie_period == 0) home->zombie_period =30;
1271 home->response_window = home->zombie_period - 1;
1273 home->ping_check = HOME_PING_CHECK_NONE;
1275 home->revive_interval = rc->dead_time;
1277 if (rbtree_finddata(home_servers_byaddr, home)) {
1278 cf_log_err_cs(cs, "Home server %s has the same IP address and/or port as another home server.", name);
1283 if (!rbtree_insert(home_servers_byname, home)) {
1284 cf_log_err_cs(cs, "Internal error %d adding home server %s.", __LINE__, name);
1289 if (!rbtree_insert(home_servers_byaddr, home)) {
1290 rbtree_deletebydata(home_servers_byname, home);
1291 cf_log_err_cs(cs, "Internal error %d adding home server %s.", __LINE__, name);
1297 home->number = home_server_max_number++;
1298 if (!rbtree_insert(home_servers_bynumber, home)) {
1299 rbtree_deletebydata(home_servers_byname, home);
1300 if (home->ipaddr.af != AF_UNSPEC) {
1301 rbtree_deletebydata(home_servers_byname, home);
1304 "Internal error %d adding home server %s.",
1313 * We now have a home server, see if we can insert it
1314 * into pre-existing pool.
1316 if (insert_point >= 0) {
1317 rad_assert(pool != NULL);
1318 pool->servers[insert_point] = home;
1322 rad_assert(pool == NULL);
1323 rad_assert(home != NULL);
1326 * Count the old-style realms of this name.
1328 num_home_servers = 0;
1329 for (subcs = cf_section_find_next(cs, NULL, "realm");
1331 subcs = cf_section_find_next(cs, subcs, "realm")) {
1332 char const *this = cf_section_name2(subcs);
1334 if (!this || (strcmp(this, realm) != 0)) continue;
1338 if (num_home_servers == 0) {
1339 cf_log_err_cs(cs, "Internal error counting pools for home server %s.", name);
1344 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1347 pool->servers[0] = home;
1349 if (!rbtree_insert(home_pools_byname, pool)) {
1350 rad_assert("Internal sanity check failed");
1360 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1363 char const *secret = NULL;
1364 home_pool_type_t ldflag;
1367 cp = cf_pair_find(cs, "ldflag");
1368 ldflag = HOME_POOL_FAIL_OVER;
1370 host = cf_pair_value(cp);
1372 cf_log_err_cp(cp, "No value specified for ldflag");
1376 if (strcasecmp(host, "fail_over") == 0) {
1377 cf_log_info(cs, "\tldflag = fail_over");
1379 } else if (strcasecmp(host, "round_robin") == 0) {
1380 ldflag = HOME_POOL_LOAD_BALANCE;
1381 cf_log_info(cs, "\tldflag = round_robin");
1384 cf_log_err_cs(cs, "Unknown value \"%s\" for ldflag", host);
1387 } /* else don't print it. */
1390 * Allow old-style if it doesn't exist, or if it exists and
1393 cp = cf_pair_find(cs, "authhost");
1395 host = cf_pair_value(cp);
1397 cf_log_err_cp(cp, "No value specified for authhost");
1401 if (strcmp(host, "LOCAL") != 0) {
1402 cp = cf_pair_find(cs, "secret");
1404 cf_log_err_cs(cs, "No shared secret supplied for realm: %s", r->name);
1408 secret = cf_pair_value(cp);
1410 cf_log_err_cp(cp, "No value specified for secret");
1415 cf_log_info(cs, "\tauthhost = %s", host);
1417 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1418 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1423 cp = cf_pair_find(cs, "accthost");
1425 host = cf_pair_value(cp);
1427 cf_log_err_cp(cp, "No value specified for accthost");
1432 * Don't look for a secret again if it was found
1435 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1436 cp = cf_pair_find(cs, "secret");
1438 cf_log_err_cs(cs, "No shared secret supplied for realm: %s", r->name);
1442 secret = cf_pair_value(cp);
1444 cf_log_err_cp(cp, "No value specified for secret");
1449 cf_log_info(cs, "\taccthost = %s", host);
1451 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1452 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1457 cp = cf_pair_find(cs, "virtual_server");
1459 host = cf_pair_value(cp);
1461 cf_log_err_cp(cp, "No value specified for virtual_server");
1465 cf_log_info(cs, "\tvirtual_server = %s", host);
1467 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1468 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1471 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1472 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1477 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1485 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1486 char const *name, home_pool_t **dest,
1487 int server_type, int do_print)
1489 home_pool_t mypool, *pool;
1492 mypool.server_type = server_type;
1494 pool = rbtree_finddata(home_pools_byname, &mypool);
1496 CONF_SECTION *pool_cs;
1498 pool_cs = cf_section_sub_find_name2(rc->cs,
1502 pool_cs = cf_section_sub_find_name2(rc->cs,
1507 cf_log_err_cs(cs, "Failed to find home_server_pool \"%s\"", name);
1511 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1515 pool = rbtree_finddata(home_pools_byname, &mypool);
1517 ERROR("Internal sanity check failed in add_pool_to_realm");
1522 if (pool->server_type != server_type) {
1523 cf_log_err_cs(cs, "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1534 int realms_realm_add (REALM *r, CONF_SECTION *cs)
1538 * It's a regex. Add it to a separate list.
1540 if (r->name[0] == '~') {
1541 realm_regex_t *rr, **last;
1543 rr = rad_malloc(sizeof(*rr));
1545 last = &realms_regex;
1546 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1557 if (!rbtree_insert(realms_byname, r)) {
1558 rad_assert("Internal sanity check failed");
1566 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1572 home_pool_t *auth_pool, *acct_pool;
1573 char const *auth_pool_name, *acct_pool_name;
1575 char const *coa_pool_name;
1576 home_pool_t *coa_pool;
1580 name2 = cf_section_name1(cs);
1581 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1582 cf_log_err_cs(cs, "Section is not a realm.");
1586 name2 = cf_section_name2(cs);
1588 cf_log_err_cs(cs, "Realm section is missing the realm name.");
1593 auth_pool = acct_pool = NULL;
1594 auth_pool_name = acct_pool_name = NULL;
1597 coa_pool_name = NULL;
1601 * Prefer new configuration to old one.
1603 cp = cf_pair_find(cs, "pool");
1604 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1605 if (cp) auth_pool_name = cf_pair_value(cp);
1606 if (cp && auth_pool_name) {
1607 acct_pool_name = auth_pool_name;
1608 if (!add_pool_to_realm(rc, cs,
1609 auth_pool_name, &auth_pool,
1610 HOME_TYPE_AUTH, 1)) {
1613 if (!add_pool_to_realm(rc, cs,
1614 auth_pool_name, &acct_pool,
1615 HOME_TYPE_ACCT, 0)) {
1620 cp = cf_pair_find(cs, "auth_pool");
1621 if (cp) auth_pool_name = cf_pair_value(cp);
1622 if (cp && auth_pool_name) {
1624 cf_log_err_cs(cs, "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1627 if (!add_pool_to_realm(rc, cs,
1628 auth_pool_name, &auth_pool,
1629 HOME_TYPE_AUTH, 1)) {
1634 cp = cf_pair_find(cs, "acct_pool");
1635 if (cp) acct_pool_name = cf_pair_value(cp);
1636 if (cp && acct_pool_name) {
1637 int do_print = true;
1640 cf_log_err_cs(cs, "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1646 (strcmp(auth_pool_name, acct_pool_name) != 0))) {
1650 if (!add_pool_to_realm(rc, cs,
1651 acct_pool_name, &acct_pool,
1652 HOME_TYPE_ACCT, do_print)) {
1658 cp = cf_pair_find(cs, "coa_pool");
1659 if (cp) coa_pool_name = cf_pair_value(cp);
1660 if (cp && coa_pool_name) {
1661 int do_print = true;
1663 if (!add_pool_to_realm(rc, cs,
1664 coa_pool_name, &coa_pool,
1665 HOME_TYPE_COA, do_print)) {
1672 cf_log_info(cs, " realm %s {", name2);
1676 * The realm MAY already exist if it's an old-style realm.
1677 * In that case, merge the old-style realm with this one.
1679 r = realm_find2(name2);
1680 if (r && (strcmp(r->name, name2) == 0)) {
1681 if (cf_pair_find(cs, "auth_pool") ||
1682 cf_pair_find(cs, "acct_pool")) {
1683 cf_log_err_cs(cs, "Duplicate realm \"%s\"", name2);
1687 if (!old_realm_config(rc, cs, r)) {
1691 cf_log_info(cs, " } # realm %s", name2);
1697 if (name2[0] == '~') {
1702 * Include substring matches.
1704 rcode = regcomp(®, name2 + 1, REG_EXTENDED | REG_NOSUB | REG_ICASE);
1708 regerror(rcode, ®, buffer, sizeof(buffer));
1711 "Invalid regex \"%s\": %s",
1719 r = rad_malloc(sizeof(*r));
1720 memset(r, 0, sizeof(*r));
1725 r->auth_pool = auth_pool;
1726 r->acct_pool = acct_pool;
1728 r->coa_pool = coa_pool;
1731 if (auth_pool_name &&
1732 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1733 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1735 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1736 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1738 if (coa_pool_name) cf_log_info(cs, "\tcoa_pool = %s", coa_pool_name);
1743 cp = cf_pair_find(cs, "nostrip");
1744 if (cp && (cf_pair_value(cp) == NULL)) {
1746 cf_log_info(cs, "\tnostrip");
1750 * We're a new-style realm. Complain if we see the old
1753 if (r->auth_pool || r->acct_pool) {
1754 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1755 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1756 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1757 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1758 WDEBUG2("Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1763 * The realm MAY be an old-style realm, as there
1764 * was no auth_pool or acct_pool. Double-check
1765 * it, just to be safe.
1767 } else if (!old_realm_config(rc, cs, r)) {
1771 if ( !realms_realm_add(r, cs))
1774 cf_log_info(cs, " }");
1778 cf_log_info(cs, " } # realm %s", name2);
1784 static const FR_NAME_NUMBER home_server_types[] = {
1785 { "auth", HOME_TYPE_AUTH },
1786 { "auth+acct", HOME_TYPE_AUTH },
1787 { "acct", HOME_TYPE_ACCT },
1788 { "coa", HOME_TYPE_COA },
1792 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1795 char const *name, *type;
1797 CONF_SECTION *server_cs;
1799 cp = cf_pair_find(cs, "home_server");
1801 cf_log_err_cs(cs, "Pool does not contain a \"home_server\" entry");
1802 return HOME_TYPE_INVALID;
1805 name = cf_pair_value(cp);
1807 cf_log_err_cp(cp, "home_server entry does not reference a home server");
1808 return HOME_TYPE_INVALID;
1811 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1813 cf_log_err_cp(cp, "home_server \"%s\" does not exist", name);
1814 return HOME_TYPE_INVALID;
1817 cp = cf_pair_find(server_cs, "type");
1819 cf_log_err_cs(server_cs, "home_server %s does not contain a \"type\" entry", name);
1820 return HOME_TYPE_INVALID;
1823 type = cf_pair_value(cp);
1825 cf_log_err_cs(server_cs, "home_server %s contains an empty \"type\" entry", name);
1826 return HOME_TYPE_INVALID;
1829 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1830 if (home == HOME_TYPE_INVALID) {
1831 cf_log_err_cs(server_cs, "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1832 return HOME_TYPE_INVALID;
1835 return home; /* 'cause we miss it so much */
1839 int realms_init(CONF_SECTION *config)
1843 CONF_SECTION *server_cs;
1845 realm_config_t *rc, *old_rc;
1847 if (realms_byname) return 1;
1849 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1850 if (!realms_byname) {
1856 home_servers_byaddr = rbtree_create(home_server_addr_cmp, home_server_free, 0);
1857 if (!home_servers_byaddr) {
1862 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1863 if (!home_servers_byname) {
1869 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1870 if (!home_servers_bynumber) {
1876 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1877 if (!home_pools_byname) {
1883 rc = talloc_zero(NULL, realm_config_t);
1887 cs = cf_subsection_find_next(config, NULL, "proxy");
1889 if (cf_section_parse(cs, rc, proxy_config) < 0) {
1890 ERROR("Failed parsing proxy section");
1897 rc->dead_time = DEAD_TIME;
1898 rc->retry_count = RETRY_COUNT;
1899 rc->retry_delay = RETRY_DELAY;
1901 rc->wake_all_if_all_dead= 0;
1904 for (cs = cf_subsection_find_next(config, NULL, "home_server");
1906 cs = cf_subsection_find_next(config, cs, "home_server")) {
1907 if (!home_server_add(rc, cs)) {
1915 * Loop over virtual servers to find homes which are
1918 for (server_cs = cf_subsection_find_next(config, NULL, "server");
1920 server_cs = cf_subsection_find_next(config, server_cs, "server")) {
1921 for (cs = cf_subsection_find_next(server_cs, NULL, "home_server");
1923 cs = cf_subsection_find_next(server_cs, cs, "home_server")) {
1924 if (!home_server_add(rc, cs)) {
1933 for (cs = cf_subsection_find_next(config, NULL, "realm");
1935 cs = cf_subsection_find_next(config, cs, "realm")) {
1936 if (!realm_add(rc, cs)) {
1945 * CoA pools aren't necessarily tied to realms.
1947 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1949 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1953 * Pool was already loaded.
1955 if (cf_data_find(cs, "home_server_pool")) continue;
1957 type = pool_peek_type(config, cs);
1958 if (type == HOME_TYPE_INVALID) {
1964 if (!server_pool_add(rc, cs, type, true)) {
1974 xlat_register("home_server", xlat_home_server, NULL, NULL);
1975 xlat_register("home_server_pool", xlat_server_pool, NULL, NULL);
1979 * Swap pointers atomically.
1981 old_rc = realm_config;
1989 * Find a realm where "name" might be the regex.
1991 REALM *realm_find2(char const *name)
1996 if (!name) name = "NULL";
1998 myrealm.name = name;
1999 realm = rbtree_finddata(realms_byname, &myrealm);
2000 if (realm) return realm;
2004 realm_regex_t *this;
2006 for (this = realms_regex; this != NULL; this = this->next) {
2007 if (strcmp(this->realm->name, name) == 0) {
2015 * Couldn't find a realm. Look for DEFAULT.
2017 myrealm.name = "DEFAULT";
2018 return rbtree_finddata(realms_byname, &myrealm);
2023 * Find a realm in the REALM list.
2025 REALM *realm_find(char const *name)
2030 if (!name) name = "NULL";
2032 myrealm.name = name;
2033 realm = rbtree_finddata(realms_byname, &myrealm);
2034 if (realm) return realm;
2038 realm_regex_t *this;
2040 for (this = realms_regex; this != NULL; this = this->next) {
2045 * Include substring matches.
2047 if (regcomp(®, this->realm->name + 1, REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
2051 compare = regexec(®, name, 0, NULL, 0);
2054 if (compare == 0) return this->realm;
2060 * Couldn't find a realm. Look for DEFAULT.
2062 myrealm.name = "DEFAULT";
2063 return rbtree_finddata(realms_byname, &myrealm);
2070 * Allocate the proxy list if it doesn't already exist, and copy request
2071 * VPs into it. Setup src/dst IP addresses based on home server, and
2072 * calculate and add the message-authenticator.
2074 * This is a distinct function from home_server_ldb, as not all home_server
2075 * lookups result in the *CURRENT* request being proxied,
2076 * as in rlm_replicate, and this may trigger asserts elsewhere in the
2079 void home_server_update_request(home_server *home, REQUEST *request)
2083 * Allocate the proxy packet, only if it wasn't
2084 * already allocated by a module. This check is
2085 * mainly to support the proxying of EAP-TTLS and
2086 * EAP-PEAP tunneled requests.
2088 * In those cases, the EAP module creates a
2089 * "fake" request, and recursively passes it
2090 * through the authentication stage of the
2091 * server. The module then checks if the request
2092 * was supposed to be proxied, and if so, creates
2093 * a proxy packet from the TUNNELED request, and
2094 * not from the EAP request outside of the
2097 * The proxy then works like normal, except that
2098 * the response packet is "eaten" by the EAP
2099 * module, and encapsulated into an EAP packet.
2101 if (!request->proxy) {
2102 request->proxy = rad_alloc(request, true);
2103 if (!request->proxy) {
2110 * Copy the request, then look up name
2111 * and plain-text password in the copy.
2113 * Note that the User-Name attribute is
2114 * the *original* as sent over by the
2115 * client. The Stripped-User-Name
2116 * attribute is the one hacked through
2119 request->proxy->vps = paircopy(request->proxy,
2120 request->packet->vps);
2124 * Update the various fields as appropriate.
2126 request->proxy->src_ipaddr = home->src_ipaddr;
2127 request->proxy->src_port = 0;
2128 request->proxy->dst_ipaddr = home->ipaddr;
2129 request->proxy->dst_port = home->port;
2130 request->home_server = home;
2133 * Access-Requests have a Message-Authenticator added,
2134 * unless one already exists.
2136 if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2137 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY)) {
2138 pairmake(request->proxy, &request->proxy->vps,
2139 "Message-Authenticator", "0x00",
2144 home_server *home_server_ldb(char const *realmname,
2145 home_pool_t *pool, REQUEST *request)
2149 home_server *found = NULL;
2150 home_server *zombie = NULL;
2154 * Determine how to pick choose the home server.
2156 switch (pool->type) {
2160 * For load-balancing by client IP address, we
2161 * pick a home server by hashing the client IP.
2163 * This isn't as even a load distribution as
2164 * tracking the State attribute, but it's better
2167 case HOME_POOL_CLIENT_BALANCE:
2168 switch (request->packet->src_ipaddr.af) {
2170 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2171 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2174 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2175 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2181 start = hash % pool->num_home_servers;
2184 case HOME_POOL_CLIENT_PORT_BALANCE:
2185 switch (request->packet->src_ipaddr.af) {
2187 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2188 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2191 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2192 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2198 fr_hash_update(&request->packet->src_port,
2199 sizeof(request->packet->src_port), hash);
2200 start = hash % pool->num_home_servers;
2203 case HOME_POOL_KEYED_BALANCE:
2204 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY, 0, TAG_ANY)) != NULL) {
2205 hash = fr_hash(vp->vp_strvalue, vp->length);
2206 start = hash % pool->num_home_servers;
2211 case HOME_POOL_LOAD_BALANCE:
2212 case HOME_POOL_FAIL_OVER:
2216 default: /* this shouldn't happen... */
2223 * Starting with the home server we chose, loop through
2224 * all home servers. If the current one is dead, skip
2225 * it. If it is too busy, skip it.
2227 * Otherwise, use it.
2229 for (count = 0; count < pool->num_home_servers; count++) {
2230 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
2232 if (!home) continue;
2235 * Skip dead home servers.
2237 * Home servers that are unknown, alive, or zombie
2238 * are used for proxying.
2240 if (home->state == HOME_STATE_IS_DEAD) {
2245 * This home server is too busy. Choose another one.
2247 if (home->currently_outstanding >= home->max_outstanding) {
2253 * We read the packet from a detail file, AND it
2254 * came from this server. Don't re-proxy it
2257 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2258 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
2259 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2265 * Default virtual: ignore homes tied to a
2268 if (!request->server && home->parent_server) {
2273 * A virtual AND home is tied to virtual,
2274 * ignore ones which don't match.
2276 if (request->server && home->parent_server &&
2277 strcmp(request->server, home->parent_server) != 0) {
2282 * Allow request->server && !home->parent_server
2284 * i.e. virtuals can proxy to globally defined
2289 * It's zombie, so we remember the first zombie
2290 * we find, but we don't mark it as a "live"
2293 if (home->state == HOME_STATE_ZOMBIE) {
2294 if (!zombie) zombie = home;
2299 * We've found the first "live" one. Use that.
2301 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2307 * Otherwise we're doing some kind of load balancing.
2308 * If we haven't found one yet, pick this one.
2315 RDEBUG3("PROXY %s %d\t%s %d",
2316 found->name, found->currently_outstanding,
2317 home->name, home->currently_outstanding);
2320 * Prefer this server if it's less busy than the
2321 * one we had previously found.
2323 if (home->currently_outstanding < found->currently_outstanding) {
2324 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2325 home->name, found->name);
2331 * Ignore servers which are busier than the one
2334 if (home->currently_outstanding > found->currently_outstanding) {
2335 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2336 home->name, found->name);
2341 * From the list of servers which have the same
2342 * load, choose one at random.
2344 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2347 } /* loop over the home servers */
2350 * We have no live servers, BUT we have a zombie. Use
2351 * the zombie as a last resort.
2353 if (!found && zombie) {
2359 * There's a fallback if they're all dead.
2361 if (!found && pool->fallback) {
2362 found = pool->fallback;
2364 WDEBUG("Home server pool %s failing over to fallback %s",
2365 pool->name, found->server);
2366 if (pool->in_fallback) goto update_and_return;
2368 pool->in_fallback = true;
2371 * Run the trigger once an hour saying that
2374 if ((pool->time_all_dead + 3600) < request->timestamp) {
2375 pool->time_all_dead = request->timestamp;
2376 exec_trigger(request, pool->cs, "home_server_pool.fallback", false);
2382 if ((found != pool->fallback) && pool->in_fallback) {
2383 pool->in_fallback = false;
2384 exec_trigger(request, pool->cs, "home_server_pool.normal", false);
2391 * No live match found, and no fallback to the "DEFAULT"
2392 * realm. We fix this by blindly marking all servers as
2393 * "live". But only do it for ones that don't support
2394 * "pings", as they will be marked live when they
2395 * actually are live.
2397 if (!realm_config->fallback &&
2398 realm_config->wake_all_if_all_dead) {
2399 for (count = 0; count < pool->num_home_servers; count++) {
2400 home_server *home = pool->servers[count];
2402 if (!home) continue;
2404 if ((home->state == HOME_STATE_IS_DEAD) &&
2405 (home->ping_check == HOME_PING_CHECK_NONE)) {
2406 home->state = HOME_STATE_ALIVE;
2407 if (!found) found = home;
2411 if (found) goto update_and_return;
2415 * Still nothing. Look up the DEFAULT realm, but only
2416 * if we weren't looking up the NULL or DEFAULT realms.
2418 if (realm_config->fallback &&
2420 (strcmp(realmname, "NULL") != 0) &&
2421 (strcmp(realmname, "DEFAULT") != 0)) {
2422 REALM *rd = realm_find("DEFAULT");
2424 if (!rd) return NULL;
2427 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2428 pool = rd->auth_pool;
2430 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2431 pool = rd->acct_pool;
2433 if (!pool) return NULL;
2435 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2436 return home_server_ldb(rd->name, pool, request);
2440 * Still haven't found anything. Oh well.
2446 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port, int proto)
2450 memset(&myhome, 0, sizeof(myhome));
2451 myhome.ipaddr = *ipaddr;
2452 myhome.src_ipaddr.af = ipaddr->af;
2455 myhome.proto = proto;
2457 myhome.proto = IPPROTO_UDP;
2459 myhome.server = NULL; /* we're not called for internal proxying */
2461 return rbtree_finddata(home_servers_byaddr, &myhome);
2465 home_server *home_server_byname(char const *name, int type)
2469 memset(&myhome, 0, sizeof(myhome));
2473 return rbtree_finddata(home_servers_byname, &myhome);
2478 home_server *home_server_bynumber(int number)
2482 memset(&myhome, 0, sizeof(myhome));
2483 myhome.number = number;
2484 myhome.server = NULL; /* we're not called for internal proxying */
2486 return rbtree_finddata(home_servers_bynumber, &myhome);
2490 home_pool_t *home_pool_byname(char const *name, int type)
2494 memset(&mypool, 0, sizeof(mypool));
2496 mypool.server_type = type;
2497 return rbtree_finddata(home_pools_byname, &mypool);