2 * realms.c Realm handling code
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2007 The FreeRADIUS server project
21 * Copyright 2007 Alan DeKok <aland@deployingradius.com>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/rad_assert.h>
39 * For POSIX Regular expressions.
40 * (0) Means no extended regular expressions.
41 * REG_EXTENDED means use extended regular expressions.
44 #define REG_EXTENDED (0)
56 static rbtree_t *realms_byname = NULL;
59 typedef struct realm_regex_t {
61 struct realm_regex_t *next;
64 static realm_regex_t *realms_regex = NULL;
66 #endif /* HAVE_REGEX_H */
68 typedef struct realm_config_t {
74 int wake_all_if_all_dead;
77 static realm_config_t *realm_config = NULL;
80 static rbtree_t *home_servers_byaddr = NULL;
81 static rbtree_t *home_servers_byname = NULL;
83 static int home_server_max_number = 0;
84 static rbtree_t *home_servers_bynumber = NULL;
87 static rbtree_t *home_pools_byname = NULL;
90 * Map the proxy server configuration parameters to variables.
92 static const CONF_PARSER proxy_config[] = {
93 { "retry_delay", PW_TYPE_INTEGER,
94 offsetof(realm_config_t, retry_delay),
95 NULL, Stringify(RETRY_DELAY) },
97 { "retry_count", PW_TYPE_INTEGER,
98 offsetof(realm_config_t, retry_count),
99 NULL, Stringify(RETRY_COUNT) },
101 { "default_fallback", PW_TYPE_BOOLEAN,
102 offsetof(realm_config_t, fallback),
105 { "dead_time", PW_TYPE_INTEGER,
106 offsetof(realm_config_t, dead_time),
107 NULL, Stringify(DEAD_TIME) },
109 { "wake_all_if_all_dead", PW_TYPE_BOOLEAN,
110 offsetof(realm_config_t, wake_all_if_all_dead),
113 { NULL, -1, 0, NULL, NULL }
117 static int realm_name_cmp(const void *one, const void *two)
119 const REALM *a = one;
120 const REALM *b = two;
122 return strcasecmp(a->name, b->name);
127 static int home_server_name_cmp(const void *one, const void *two)
129 const home_server *a = one;
130 const home_server *b = two;
132 if (a->type < b->type) return -1;
133 if (a->type > b->type) return +1;
135 return strcasecmp(a->name, b->name);
138 static int home_server_addr_cmp(const void *one, const void *two)
140 const home_server *a = one;
141 const home_server *b = two;
143 if (a->server && !b->server) return -1;
144 if (!a->server && b->server) return +1;
146 if (a->server && b->server) {
147 int rcode = a->type - b->type;
148 if (rcode != 0) return rcode;
149 return strcmp(a->server, b->server);
153 if (a->proto < b->proto) return -1;
154 if (a->proto > b->proto) return +1;
157 if (a->port < b->port) return -1;
158 if (a->port > b->port) return +1;
160 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
164 static int home_server_number_cmp(const void *one, const void *two)
166 const home_server *a = one;
167 const home_server *b = two;
169 return (a->number - b->number);
173 static int home_pool_name_cmp(const void *one, const void *two)
175 const home_pool_t *a = one;
176 const home_pool_t *b = two;
178 if (a->server_type < b->server_type) return -1;
179 if (a->server_type > b->server_type) return +1;
181 return strcasecmp(a->name, b->name);
185 static size_t xlat_cs(CONF_SECTION *cs, char *fmt, char *out, size_t outlen)
188 const char *value = NULL;
193 if (strcmp(fmt, "instance") == 0) {
194 value = cf_section_name2(cs);
202 cp = cf_pair_find(cs, fmt);
203 if (!cp || !(value = cf_pair_value(cp))) {
209 strlcpy(out, value, outlen);
216 * Xlat for %{home_server:foo}
218 static size_t xlat_home_server(UNUSED void *instance, REQUEST *request,
219 char *fmt, char *out, size_t outlen,
220 UNUSED RADIUS_ESCAPE_STRING func)
222 if (!fmt || !out || (outlen < 1)) return 0;
224 if (!request || !request->home_server) {
229 return xlat_cs(request->home_server->cs, fmt, out, outlen);
234 * Xlat for %{home_server_pool:foo}
236 static size_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
237 char *fmt, char *out, size_t outlen,
238 UNUSED RADIUS_ESCAPE_STRING func)
240 if (!fmt || !out || (outlen < 1)) return 0;
242 if (!request || !request->home_pool) {
247 return xlat_cs(request->home_pool->cs, fmt, out, outlen);
251 void realms_free(void)
255 rbtree_free(home_servers_bynumber);
256 home_servers_bynumber = NULL;
259 rbtree_free(home_servers_byname);
260 home_servers_byname = NULL;
262 rbtree_free(home_servers_byaddr);
263 home_servers_byaddr = NULL;
265 rbtree_free(home_pools_byname);
266 home_pools_byname = NULL;
269 rbtree_free(realms_byname);
270 realms_byname = NULL;
274 realm_regex_t *this, *next;
276 for (this = realms_regex; this != NULL; this = next) {
291 static CONF_PARSER limit_config[] = {
292 { "max_connections", PW_TYPE_INTEGER,
293 offsetof(home_server, max_connections), NULL, "16" },
295 { "max_requests", PW_TYPE_INTEGER,
296 offsetof(home_server,max_requests), NULL, "0" },
298 { "lifetime", PW_TYPE_INTEGER,
299 offsetof(home_server,lifetime), NULL, "0" },
301 { "idle_timeout", PW_TYPE_INTEGER,
302 offsetof(home_server,idle_timeout), NULL, "0" },
304 { NULL, -1, 0, NULL, NULL } /* end the list */
307 static struct in_addr hs_ip4addr;
308 static struct in6_addr hs_ip6addr;
309 static char *hs_srcipaddr = NULL;
310 static char *hs_type = NULL;
311 static char *hs_check = NULL;
312 static char *hs_virtual_server = NULL;
314 static char *hs_proto = NULL;
317 static CONF_PARSER home_server_config[] = {
318 { "ipaddr", PW_TYPE_IPADDR,
319 0, &hs_ip4addr, NULL },
320 { "ipv6addr", PW_TYPE_IPV6ADDR,
321 0, &hs_ip6addr, NULL },
322 { "virtual_server", PW_TYPE_STRING_PTR,
323 0, &hs_virtual_server, NULL },
325 { "port", PW_TYPE_INTEGER,
326 offsetof(home_server,port), NULL, "0" },
328 { "type", PW_TYPE_STRING_PTR,
332 { "proto", PW_TYPE_STRING_PTR,
333 0, &hs_proto, NULL },
336 { "secret", PW_TYPE_STRING_PTR,
337 offsetof(home_server,secret), NULL, NULL},
339 { "src_ipaddr", PW_TYPE_STRING_PTR,
340 0, &hs_srcipaddr, NULL },
342 { "response_window", PW_TYPE_INTEGER,
343 offsetof(home_server,response_window), NULL, "30" },
344 { "no_response_fail", PW_TYPE_BOOLEAN,
345 offsetof(home_server,no_response_fail), NULL, NULL },
346 { "max_outstanding", PW_TYPE_INTEGER,
347 offsetof(home_server,max_outstanding), NULL, "65536" },
348 { "require_message_authenticator", PW_TYPE_BOOLEAN,
349 offsetof(home_server, message_authenticator), 0, NULL },
351 { "zombie_period", PW_TYPE_INTEGER,
352 offsetof(home_server,zombie_period), NULL, "40" },
353 { "status_check", PW_TYPE_STRING_PTR,
354 0, &hs_check, "none" },
355 { "ping_check", PW_TYPE_STRING_PTR,
356 0, &hs_check, NULL },
358 { "ping_interval", PW_TYPE_INTEGER,
359 offsetof(home_server,ping_interval), NULL, "30" },
360 { "check_interval", PW_TYPE_INTEGER,
361 offsetof(home_server,ping_interval), NULL, "30" },
362 { "num_answers_to_alive", PW_TYPE_INTEGER,
363 offsetof(home_server,num_pings_to_alive), NULL, "3" },
364 { "num_pings_to_alive", PW_TYPE_INTEGER,
365 offsetof(home_server,num_pings_to_alive), NULL, "3" },
366 { "revive_interval", PW_TYPE_INTEGER,
367 offsetof(home_server,revive_interval), NULL, "300" },
368 { "status_check_timeout", PW_TYPE_INTEGER,
369 offsetof(home_server,ping_timeout), NULL, "4" },
371 { "username", PW_TYPE_STRING_PTR,
372 offsetof(home_server,ping_user_name), NULL, NULL},
373 { "password", PW_TYPE_STRING_PTR,
374 offsetof(home_server,ping_user_password), NULL, NULL},
377 { "historic_average_window", PW_TYPE_INTEGER,
378 offsetof(home_server,ema.window), NULL, NULL },
382 { "irt", PW_TYPE_INTEGER,
383 offsetof(home_server, coa_irt), 0, Stringify(2) },
384 { "mrt", PW_TYPE_INTEGER,
385 offsetof(home_server, coa_mrt), 0, Stringify(16) },
386 { "mrc", PW_TYPE_INTEGER,
387 offsetof(home_server, coa_mrc), 0, Stringify(5) },
388 { "mrd", PW_TYPE_INTEGER,
389 offsetof(home_server, coa_mrd), 0, Stringify(30) },
392 { "limit", PW_TYPE_SUBSECTION, 0, NULL, (const void *) limit_config },
394 { NULL, -1, 0, NULL, NULL } /* end the list */
398 static void null_free(UNUSED void *data)
402 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs, int pool_type)
409 free(hs_virtual_server); /* used only for printing during parsing */
410 hs_virtual_server = NULL;
412 name2 = cf_section_name1(cs);
413 if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
414 cf_log_err(cf_sectiontoitem(cs),
415 "Section is not a home_server.");
419 name2 = cf_section_name2(cs);
421 cf_log_err(cf_sectiontoitem(cs),
422 "Home server section is missing a name.");
426 home = rad_malloc(sizeof(*home));
427 memset(home, 0, sizeof(*home));
433 * For zombie period calculations. We want to count
434 * zombies from the time when the server starts, instead
437 home->last_packet = time(NULL);
440 * Authentication servers have a default "no_response_fail = 0".
441 * Accounting servers have a default "no_response_fail = 1".
443 * This is because authentication packets are retried, so
444 * they can fail over to another home server. Accounting
445 * packets are not retried, so they cannot fail over, and
446 * instead should be rejected immediately.
448 home->no_response_fail = 2;
450 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
451 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
452 if (cf_section_parse(cs, home, home_server_config) < 0) {
457 * Figure out which one to use.
459 if (cf_pair_find(cs, "ipaddr")) {
460 home->ipaddr.af = AF_INET;
461 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
463 } else if (cf_pair_find(cs, "ipv6addr")) {
464 home->ipaddr.af = AF_INET6;
465 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
467 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
468 home->ipaddr.af = AF_UNSPEC;
469 home->server = cf_pair_value(cp);
471 cf_log_err(cf_sectiontoitem(cs),
472 "Invalid value for virtual_server");
476 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
478 cf_log_err(cf_sectiontoitem(cs),
479 "No such server %s", home->server);
484 * When CoA is used, the user has to specify the type
485 * of the home server, even when they point to
488 home->secret = strdup("");
492 cf_log_err(cf_sectiontoitem(cs),
493 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
510 if (!home->port || (home->port > 65535)) {
511 cf_log_err(cf_sectiontoitem(cs),
512 "No port, or invalid port defined for home server %s.",
518 cf_log_err(cf_sectiontoitem(cs),
519 "Fatal error! Home server %s is ourselves!",
525 cf_log_err(cf_sectiontoitem(cs),
526 "No shared secret defined for home server %s.",
532 * Use a reasonable default.
535 if (!hs_type) hs_type = strdup("auth+acct");
537 if (strcasecmp(hs_type, "auth") == 0) {
538 home->type = HOME_TYPE_AUTH;
539 if (home->no_response_fail == 2) home->no_response_fail = 0;
540 if (pool_type != home->type) {
542 cf_log_err(cf_sectiontoitem(cs),
543 "Home server %s of unexpected type \"%s\"",
548 } else if (strcasecmp(hs_type, "acct") == 0) {
549 home->type = HOME_TYPE_ACCT;
550 if (home->no_response_fail == 2) home->no_response_fail = 1;
551 if (pool_type != home->type) goto mismatch;
553 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
554 home->type = HOME_TYPE_AUTH;
558 } else if (strcasecmp(hs_type, "coa") == 0) {
559 home->type = HOME_TYPE_COA;
562 if (pool_type != home->type) goto mismatch;
564 if (home->server != NULL) {
565 cf_log_err(cf_sectiontoitem(cs),
566 "Home servers of type \"coa\" cannot point to a virtual server");
572 cf_log_err(cf_sectiontoitem(cs),
573 "Invalid type \"%s\" for home server %s.",
580 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
581 home->ping_check = HOME_PING_CHECK_NONE;
583 } else if (strcasecmp(hs_check, "status-server") == 0) {
584 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
586 } else if (strcasecmp(hs_check, "request") == 0) {
587 home->ping_check = HOME_PING_CHECK_REQUEST;
590 cf_log_err(cf_sectiontoitem(cs),
591 "Invalid ping_check \"%s\" for home server %s.",
598 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
599 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
600 if (!home->ping_user_name) {
601 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable ping checks");
605 if ((home->type == HOME_TYPE_AUTH) &&
606 !home->ping_user_password) {
607 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable ping checks");
612 home->proto = IPPROTO_UDP;
615 if (strcmp(hs_proto, "udp") == 0) {
619 } else if (strcmp(hs_proto, "tcp") == 0) {
622 home->proto = IPPROTO_TCP;
625 cf_log_err(cf_sectiontoitem(cs),
626 "Unknown proto \"%s\".", hs_proto);
633 rbtree_finddata(home_servers_byaddr, home)) {
634 cf_log_err(cf_sectiontoitem(cs), "Duplicate home server");
639 * If the home is a virtual server, don't look up source IP.
642 rad_assert(home->ipaddr.af != AF_UNSPEC);
645 * Otherwise look up the source IP using the same
646 * address family as the destination IP.
649 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
650 cf_log_err(cf_sectiontoitem(cs), "Failed parsing src_ipaddr");
656 * Source isn't specified: Source is
657 * the correct address family, but all zeros.
659 home->src_ipaddr.af = home->ipaddr.af;
666 if (!rbtree_insert(home_servers_byname, home)) {
667 cf_log_err(cf_sectiontoitem(cs),
668 "Internal error %d adding home server %s.",
674 !rbtree_insert(home_servers_byaddr, home)) {
675 rbtree_deletebydata(home_servers_byname, home);
676 cf_log_err(cf_sectiontoitem(cs),
677 "Internal error %d adding home server %s.",
683 home->number = home_server_max_number++;
684 if (!rbtree_insert(home_servers_bynumber, home)) {
685 rbtree_deletebydata(home_servers_byname, home);
686 if (home->ipaddr.af != AF_UNSPEC) {
687 rbtree_deletebydata(home_servers_byname, home);
689 cf_log_err(cf_sectiontoitem(cs),
690 "Internal error %d adding home server %s.",
696 if (home->max_outstanding < 8) home->max_outstanding = 8;
697 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
699 if (home->ping_interval < 6) home->ping_interval = 6;
700 if (home->ping_interval > 120) home->ping_interval = 120;
702 if (home->response_window < 1) home->response_window = 1;
703 if (home->response_window > 60) home->response_window = 60;
705 if (home->zombie_period < 1) home->zombie_period = 1;
706 if (home->zombie_period > 120) home->zombie_period = 120;
708 if (home->zombie_period < home->response_window) {
709 home->zombie_period = home->response_window;
712 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
713 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
715 if (home->ping_timeout < 3) home->ping_timeout = 3;
716 if (home->ping_timeout > 10) home->ping_timeout = 10;
718 if (home->revive_interval < 60) home->revive_interval = 60;
719 if (home->revive_interval > 3600) home->revive_interval = 3600;
722 if (home->coa_irt < 1) home->coa_irt = 1;
723 if (home->coa_irt > 5) home->coa_irt = 5;
725 if (home->coa_mrc < 0) home->coa_mrc = 0;
726 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
728 if (home->coa_mrt < 0) home->coa_mrt = 0;
729 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
731 if (home->coa_mrd < 5) home->coa_mrd = 5;
732 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
735 if (home->max_connections > 1024) home->max_connections = 1024;
739 * UDP sockets can't be connection limited.
741 if (home->proto != IPPROTO_TCP) home->max_connections = 0;
744 if ((home->idle_timeout > 0) && (home->idle_timeout < 5))
745 home->idle_timeout = 5;
746 if ((home->lifetime > 0) && (home->lifetime < 5))
748 if ((home->lifetime > 0) && (home->idle_timeout > home->lifetime))
749 home->idle_timeout = 0;
752 home_server *home2 = rad_malloc(sizeof(*home2));
754 memcpy(home2, home, sizeof(*home2));
756 home2->type = HOME_TYPE_ACCT;
758 home2->ping_user_password = NULL;
761 if (home->no_response_fail == 2) home->no_response_fail = 0;
762 if (home2->no_response_fail == 2) home2->no_response_fail = 1;
764 if (!rbtree_insert(home_servers_byname, home2)) {
765 cf_log_err(cf_sectiontoitem(cs),
766 "Internal error %d adding home server %s.",
773 !rbtree_insert(home_servers_byaddr, home2)) {
774 rbtree_deletebydata(home_servers_byname, home2);
775 cf_log_err(cf_sectiontoitem(cs),
776 "Internal error %d adding home server %s.",
783 home2->number = home_server_max_number++;
784 if (!rbtree_insert(home_servers_bynumber, home2)) {
785 rbtree_deletebydata(home_servers_byname, home2);
786 if (!home2->server) {
787 rbtree_deletebydata(home_servers_byname, home2);
789 cf_log_err(cf_sectiontoitem(cs),
790 "Internal error %d adding home server %s.",
799 * Mark it as already processed
801 cf_data_add(cs, "home_server", null_free, null_free);
807 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
808 int server_type, int num_home_servers)
812 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
814 if (!pool) return NULL; /* just for pairanoia */
816 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
821 pool->server_type = server_type;
822 pool->num_home_servers = num_home_servers;
827 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
828 const char *name, int server_type,
831 home_server myhome, *home;
832 CONF_SECTION *server_cs;
835 cf_log_err(cf_pairtoitem(cp),
836 "No value given for home_server.");
841 myhome.type = server_type;
842 home = rbtree_finddata(home_servers_byname, &myhome);
848 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
850 cf_log_err(cf_pairtoitem(cp),
851 "Unknown home_server \"%s\".", name);
855 if (!home_server_add(rc, server_cs, server_type)) {
859 home = rbtree_finddata(home_servers_byname, &myhome);
861 cf_log_err(cf_pairtoitem(cp),
862 "Internal error %d adding home server \"%s\".",
872 static int server_pool_add(realm_config_t *rc,
873 CONF_SECTION *cs, int server_type, int do_print)
876 home_pool_t *pool = NULL;
879 int num_home_servers;
882 name2 = cf_section_name1(cs);
883 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
884 (strcasecmp(name2, "home_server_pool") != 0))) {
885 cf_log_err(cf_sectiontoitem(cs),
886 "Section is not a home_server_pool.");
890 name2 = cf_section_name2(cs);
892 cf_log_err(cf_sectiontoitem(cs),
893 "Server pool section is missing a name.");
898 * Count the home servers and initalize them.
900 num_home_servers = 0;
901 for (cp = cf_pair_find(cs, "home_server");
903 cp = cf_pair_find_next(cs, cp, "home_server")) {
906 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
907 server_type, &home)) {
912 if (num_home_servers == 0) {
913 cf_log_err(cf_sectiontoitem(cs),
914 "No home servers defined in pool %s",
919 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
925 * Fallback servers must be defined, and must be
928 cp = cf_pair_find(cs, "fallback");
931 if (server_type == HOME_TYPE_COA) {
932 cf_log_err(cf_sectiontoitem(cs), "Home server pools of type \"coa\" cannot have a fallback virtual server.");
937 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
938 server_type, &pool->fallback)) {
943 if (!pool->fallback->server) {
944 cf_log_err(cf_sectiontoitem(cs), "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
949 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
951 cp = cf_pair_find(cs, "type");
953 static FR_NAME_NUMBER pool_types[] = {
954 { "load-balance", HOME_POOL_LOAD_BALANCE },
955 { "fail-over", HOME_POOL_FAIL_OVER },
956 { "round_robin", HOME_POOL_LOAD_BALANCE },
957 { "fail_over", HOME_POOL_FAIL_OVER },
958 { "client-balance", HOME_POOL_CLIENT_BALANCE },
959 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
960 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
964 value = cf_pair_value(cp);
966 cf_log_err(cf_pairtoitem(cp),
967 "No value given for type.");
971 pool->type = fr_str2int(pool_types, value, 0);
973 cf_log_err(cf_pairtoitem(cp),
974 "Unknown type \"%s\".",
979 if (do_print) cf_log_info(cs, "\ttype = %s", value);
982 cp = cf_pair_find(cs, "virtual_server");
984 pool->virtual_server = cf_pair_value(cp);
985 if (!pool->virtual_server) {
986 cf_log_err(cf_pairtoitem(cp), "No value given for virtual_server");
991 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
994 if (!cf_section_sub_find_name2(rc->cs, "server",
995 pool->virtual_server)) {
996 cf_log_err(cf_pairtoitem(cp), "No such server %s",
997 pool->virtual_server);
1003 num_home_servers = 0;
1004 for (cp = cf_pair_find(cs, "home_server");
1006 cp = cf_pair_find_next(cs, cp, "home_server")) {
1009 value = cf_pair_value(cp);
1011 memset(&myhome, 0, sizeof(&myhome));
1012 myhome.name = value;
1013 myhome.type = server_type;
1015 home = rbtree_finddata(home_servers_byname, &myhome);
1017 DEBUG2("Internal sanity check failed");
1022 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
1026 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1027 pool->servers[num_home_servers++] = home;
1028 } /* loop over home_server's */
1030 if (pool->fallback && do_print) {
1031 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1034 if (!rbtree_insert(home_pools_byname, pool)) {
1035 rad_assert("Internal sanity check failed");
1039 if (do_print) cf_log_info(cs, " }");
1041 cf_data_add(cs, "home_server_pool", pool, free);
1043 rad_assert(pool->server_type != 0);
1048 if (do_print) cf_log_info(cs, " }");
1054 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1056 const char *name, const char *secret,
1057 home_pool_type_t ldflag, home_pool_t **pool_p,
1058 int type, const char *server)
1061 int i, insert_point, num_home_servers;
1062 home_server myhome, *home;
1063 home_pool_t mypool, *pool;
1064 CONF_SECTION *subcs;
1066 rc = rc; /* -Wunused */
1075 * LOCAL realms get sanity checked, and nothing else happens.
1077 if (strcmp(name, "LOCAL") == 0) {
1079 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
1086 return 0; /* Not proxying. Can't do non-LOCAL realms */
1089 mypool.name = realm;
1090 mypool.server_type = type;
1091 pool = rbtree_finddata(home_pools_byname, &mypool);
1093 if (pool->type != ldflag) {
1094 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
1098 if (pool->server_type != type) {
1099 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
1106 home = rbtree_finddata(home_servers_byname, &myhome);
1108 if (secret && (strcmp(home->secret, secret) != 0)) {
1109 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
1113 if (home->type != type) {
1114 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
1119 * See if the home server is already listed
1120 * in the pool. If so, do nothing else.
1122 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1123 if (pool->servers[i] == home) {
1130 * If we do have a pool, check that there is room to
1131 * insert the home server we've found, or the one that we
1134 * Note that we insert it into the LAST available
1135 * position, in order to maintain the same order as in
1136 * the configuration files.
1140 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1141 if (pool->servers[i]) break;
1143 if (!pool->servers[i]) {
1148 if (insert_point < 0) {
1149 cf_log_err(cf_sectiontoitem(cs), "No room in pool to add home server \"%s\". Please update the realm configuration to use the new-style home servers and server pools.", name);
1155 * No home server, allocate one.
1161 home = rad_malloc(sizeof(*home));
1162 memset(home, 0, sizeof(*home));
1165 home->hostname = name;
1167 home->secret = secret;
1169 home->proto = IPPROTO_UDP;
1171 p = strchr(name, ':');
1173 if (type == HOME_TYPE_AUTH) {
1174 home->port = PW_AUTH_UDP_PORT;
1176 home->port = PW_ACCT_UDP_PORT;
1182 } else if (p == name) {
1183 cf_log_err(cf_sectiontoitem(cs),
1184 "Invalid hostname %s.",
1190 home->port = atoi(p + 1);
1191 if ((home->port == 0) || (home->port > 65535)) {
1192 cf_log_err(cf_sectiontoitem(cs),
1199 q = rad_malloc((p - name) + 1);
1200 memcpy(q, name, (p - name));
1206 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1207 cf_log_err(cf_sectiontoitem(cs),
1208 "Failed looking up hostname %s.",
1214 home->src_ipaddr.af = home->ipaddr.af;
1216 home->ipaddr.af = AF_UNSPEC;
1217 home->server = server;
1222 * Use the old-style configuration.
1224 home->max_outstanding = 65535*16;
1225 home->zombie_period = rc->retry_delay * rc->retry_count;
1226 if (home->zombie_period == 0) home->zombie_period =30;
1227 home->response_window = home->zombie_period - 1;
1229 home->ping_check = HOME_PING_CHECK_NONE;
1231 home->revive_interval = rc->dead_time;
1233 if (rbtree_finddata(home_servers_byaddr, home)) {
1234 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
1239 if (!rbtree_insert(home_servers_byname, home)) {
1240 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1245 if (!rbtree_insert(home_servers_byaddr, home)) {
1246 rbtree_deletebydata(home_servers_byname, home);
1247 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1253 home->number = home_server_max_number++;
1254 if (!rbtree_insert(home_servers_bynumber, home)) {
1255 rbtree_deletebydata(home_servers_byname, home);
1256 if (home->ipaddr.af != AF_UNSPEC) {
1257 rbtree_deletebydata(home_servers_byname, home);
1259 cf_log_err(cf_sectiontoitem(cs),
1260 "Internal error %d adding home server %s.",
1269 * We now have a home server, see if we can insert it
1270 * into pre-existing pool.
1272 if (insert_point >= 0) {
1273 rad_assert(pool != NULL);
1274 pool->servers[insert_point] = home;
1278 rad_assert(pool == NULL);
1279 rad_assert(home != NULL);
1282 * Count the old-style realms of this name.
1284 num_home_servers = 0;
1285 for (subcs = cf_section_find_next(cs, NULL, "realm");
1287 subcs = cf_section_find_next(cs, subcs, "realm")) {
1288 const char *this = cf_section_name2(subcs);
1290 if (!this || (strcmp(this, realm) != 0)) continue;
1294 if (num_home_servers == 0) {
1295 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
1300 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1303 pool->servers[0] = home;
1305 if (!rbtree_insert(home_pools_byname, pool)) {
1306 rad_assert("Internal sanity check failed");
1316 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1319 const char *secret = NULL;
1320 home_pool_type_t ldflag;
1323 cp = cf_pair_find(cs, "ldflag");
1324 ldflag = HOME_POOL_FAIL_OVER;
1326 host = cf_pair_value(cp);
1328 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
1332 if (strcasecmp(host, "fail_over") == 0) {
1333 cf_log_info(cs, "\tldflag = fail_over");
1335 } else if (strcasecmp(host, "round_robin") == 0) {
1336 ldflag = HOME_POOL_LOAD_BALANCE;
1337 cf_log_info(cs, "\tldflag = round_robin");
1340 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
1343 } /* else don't print it. */
1346 * Allow old-style if it doesn't exist, or if it exists and
1349 cp = cf_pair_find(cs, "authhost");
1351 host = cf_pair_value(cp);
1353 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
1357 if (strcmp(host, "LOCAL") != 0) {
1358 cp = cf_pair_find(cs, "secret");
1360 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1364 secret = cf_pair_value(cp);
1366 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1371 cf_log_info(cs, "\tauthhost = %s", host);
1373 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1374 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1379 cp = cf_pair_find(cs, "accthost");
1381 host = cf_pair_value(cp);
1383 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1388 * Don't look for a secret again if it was found
1391 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1392 cp = cf_pair_find(cs, "secret");
1394 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1398 secret = cf_pair_value(cp);
1400 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1405 cf_log_info(cs, "\taccthost = %s", host);
1407 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1408 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1413 cp = cf_pair_find(cs, "virtual_server");
1415 host = cf_pair_value(cp);
1417 cf_log_err(cf_pairtoitem(cp), "No value specified for virtual_server");
1421 cf_log_info(cs, "\tvirtual_server = %s", host);
1423 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1424 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1427 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1428 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1433 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1441 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1442 const char *name, home_pool_t **dest,
1443 int server_type, int do_print)
1445 home_pool_t mypool, *pool;
1448 mypool.server_type = server_type;
1450 pool = rbtree_finddata(home_pools_byname, &mypool);
1452 CONF_SECTION *pool_cs;
1454 pool_cs = cf_section_sub_find_name2(rc->cs,
1458 pool_cs = cf_section_sub_find_name2(rc->cs,
1463 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1467 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1471 pool = rbtree_finddata(home_pools_byname, &mypool);
1473 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1478 if (pool->server_type != server_type) {
1479 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1490 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1496 home_pool_t *auth_pool, *acct_pool;
1497 const char *auth_pool_name, *acct_pool_name;
1500 name2 = cf_section_name1(cs);
1501 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1502 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1506 name2 = cf_section_name2(cs);
1508 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1513 auth_pool = acct_pool = NULL;
1514 auth_pool_name = acct_pool_name = NULL;
1517 * Prefer new configuration to old one.
1519 cp = cf_pair_find(cs, "pool");
1520 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1521 if (cp) auth_pool_name = cf_pair_value(cp);
1522 if (cp && auth_pool_name) {
1523 acct_pool_name = auth_pool_name;
1524 if (!add_pool_to_realm(rc, cs,
1525 auth_pool_name, &auth_pool,
1526 HOME_TYPE_AUTH, 1)) {
1529 if (!add_pool_to_realm(rc, cs,
1530 auth_pool_name, &acct_pool,
1531 HOME_TYPE_ACCT, 0)) {
1536 cp = cf_pair_find(cs, "auth_pool");
1537 if (cp) auth_pool_name = cf_pair_value(cp);
1538 if (cp && auth_pool_name) {
1540 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1543 if (!add_pool_to_realm(rc, cs,
1544 auth_pool_name, &auth_pool,
1545 HOME_TYPE_AUTH, 1)) {
1550 cp = cf_pair_find(cs, "acct_pool");
1551 if (cp) acct_pool_name = cf_pair_value(cp);
1552 if (cp && acct_pool_name) {
1553 int do_print = TRUE;
1556 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1561 (strcmp(auth_pool_name, acct_pool_name) != 0)) {
1565 if (!add_pool_to_realm(rc, cs,
1566 acct_pool_name, &acct_pool,
1567 HOME_TYPE_ACCT, do_print)) {
1573 cf_log_info(cs, " realm %s {", name2);
1577 * The realm MAY already exist if it's an old-style realm.
1578 * In that case, merge the old-style realm with this one.
1580 r = realm_find2(name2);
1581 if (r && (strcmp(r->name, name2) == 0)) {
1582 if (cf_pair_find(cs, "auth_pool") ||
1583 cf_pair_find(cs, "acct_pool")) {
1584 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1588 if (!old_realm_config(rc, cs, r)) {
1592 cf_log_info(cs, " } # realm %s", name2);
1598 if (name2[0] == '~') {
1603 * Include substring matches.
1605 rcode = regcomp(®, name2 + 1,
1606 REG_EXTENDED | REG_NOSUB | REG_ICASE);
1610 regerror(rcode, ®, buffer, sizeof(buffer));
1612 cf_log_err(cf_sectiontoitem(cs),
1613 "Invalid regex \"%s\": %s",
1621 r = rad_malloc(sizeof(*r));
1622 memset(r, 0, sizeof(*r));
1627 r->auth_pool = auth_pool;
1628 r->acct_pool = acct_pool;
1630 if (auth_pool_name &&
1631 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1632 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1634 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1635 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1639 cp = cf_pair_find(cs, "nostrip");
1640 if (cp && (cf_pair_value(cp) == NULL)) {
1642 cf_log_info(cs, "\tnostrip");
1646 * We're a new-style realm. Complain if we see the old
1649 if (r->auth_pool || r->acct_pool) {
1650 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1651 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1652 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1653 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1654 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1659 * The realm MAY be an old-style realm, as there
1660 * was no auth_pool or acct_pool. Double-check
1661 * it, just to be safe.
1663 } else if (!old_realm_config(rc, cs, r)) {
1669 * It's a regex. Add it to a separate list.
1671 if (name2[0] == '~') {
1672 realm_regex_t *rr, **last;
1674 rr = rad_malloc(sizeof(*rr));
1676 last = &realms_regex;
1677 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1685 cf_log_info(cs, " }");
1690 if (!rbtree_insert(realms_byname, r)) {
1691 rad_assert("Internal sanity check failed");
1695 cf_log_info(cs, " }");
1700 cf_log_info(cs, " } # realm %s", name2);
1706 static const FR_NAME_NUMBER home_server_types[] = {
1707 { "auth", HOME_TYPE_AUTH },
1708 { "auth+acct", HOME_TYPE_AUTH },
1709 { "acct", HOME_TYPE_ACCT },
1710 { "coa", HOME_TYPE_COA },
1714 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1717 const char *name, *type;
1719 CONF_SECTION *server_cs;
1721 cp = cf_pair_find(cs, "home_server");
1723 cf_log_err(cf_sectiontoitem(cs), "Pool does not contain a \"home_server\" entry");
1724 return HOME_TYPE_INVALID;
1727 name = cf_pair_value(cp);
1729 cf_log_err(cf_pairtoitem(cp), "home_server entry does not reference a home server");
1730 return HOME_TYPE_INVALID;
1733 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1735 cf_log_err(cf_pairtoitem(cp), "home_server \"%s\" does not exist", name);
1736 return HOME_TYPE_INVALID;
1739 cp = cf_pair_find(server_cs, "type");
1741 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s does not contain a \"type\" entry", name);
1742 return HOME_TYPE_INVALID;
1745 type = cf_pair_value(cp);
1747 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an empty \"type\" entry", name);
1748 return HOME_TYPE_INVALID;
1751 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1752 if (home == HOME_TYPE_INVALID) {
1753 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1754 return HOME_TYPE_INVALID;
1757 return home; /* 'cause we miss it so much */
1761 int realms_init(CONF_SECTION *config)
1764 realm_config_t *rc, *old_rc;
1766 if (realms_byname) return 1;
1768 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1769 if (!realms_byname) {
1775 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
1776 if (!home_servers_byaddr) {
1781 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1782 if (!home_servers_byname) {
1788 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1789 if (!home_servers_bynumber) {
1795 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1796 if (!home_pools_byname) {
1802 rc = rad_malloc(sizeof(*rc));
1803 memset(rc, 0, sizeof(*rc));
1807 cs = cf_subsection_find_next(config, NULL, "proxy");
1809 cf_section_parse(cs, rc, proxy_config);
1811 rc->dead_time = DEAD_TIME;
1812 rc->retry_count = RETRY_COUNT;
1813 rc->retry_delay = RETRY_DELAY;
1815 rc->wake_all_if_all_dead= 0;
1819 for (cs = cf_subsection_find_next(config, NULL, "realm");
1821 cs = cf_subsection_find_next(config, cs, "realm")) {
1822 if (!realm_add(rc, cs)) {
1831 * CoA pools aren't tied to realms.
1833 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1835 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1839 * Pool was already loaded.
1841 if (cf_data_find(cs, "home_server_pool")) continue;
1843 type = pool_peek_type(config, cs);
1844 if (type == HOME_TYPE_INVALID) return 0;
1846 if (!server_pool_add(rc, cs, type, TRUE)) {
1852 * CoA home servers aren't tied to realms.
1854 for (cs = cf_subsection_find_next(config, NULL, "home_server");
1856 cs = cf_subsection_find_next(config, cs, "home_server")) {
1858 * Server was already loaded.
1860 if (cf_data_find(cs, "home_server")) continue;
1862 if (!home_server_add(rc, cs, HOME_TYPE_COA)) {
1870 xlat_register("home_server", xlat_home_server, NULL);
1871 xlat_register("home_server_pool", xlat_server_pool, NULL);
1875 * Swap pointers atomically.
1877 old_rc = realm_config;
1885 * Find a realm where "name" might be the regex.
1887 REALM *realm_find2(const char *name)
1892 if (!name) name = "NULL";
1894 myrealm.name = name;
1895 realm = rbtree_finddata(realms_byname, &myrealm);
1896 if (realm) return realm;
1900 realm_regex_t *this;
1902 for (this = realms_regex; this != NULL; this = this->next) {
1903 if (strcmp(this->realm->name, name) == 0) {
1911 * Couldn't find a realm. Look for DEFAULT.
1913 myrealm.name = "DEFAULT";
1914 return rbtree_finddata(realms_byname, &myrealm);
1919 * Find a realm in the REALM list.
1921 REALM *realm_find(const char *name)
1926 if (!name) name = "NULL";
1928 myrealm.name = name;
1929 realm = rbtree_finddata(realms_byname, &myrealm);
1930 if (realm) return realm;
1934 realm_regex_t *this;
1936 for (this = realms_regex; this != NULL; this = this->next) {
1941 * Include substring matches.
1943 if (regcomp(®, this->realm->name + 1,
1944 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
1948 compare = regexec(®, name, 0, NULL, 0);
1951 if (compare == 0) return this->realm;
1957 * Couldn't find a realm. Look for DEFAULT.
1959 myrealm.name = "DEFAULT";
1960 return rbtree_finddata(realms_byname, &myrealm);
1965 home_server *home_server_ldb(const char *realmname,
1966 home_pool_t *pool, REQUEST *request)
1970 home_server *found = NULL;
1971 home_server *zombie = NULL;
1975 * Determine how to pick choose the home server.
1977 switch (pool->type) {
1981 * For load-balancing by client IP address, we
1982 * pick a home server by hashing the client IP.
1984 * This isn't as even a load distribution as
1985 * tracking the State attribute, but it's better
1988 case HOME_POOL_CLIENT_BALANCE:
1989 switch (request->packet->src_ipaddr.af) {
1991 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1992 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1995 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1996 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2002 start = hash % pool->num_home_servers;
2005 case HOME_POOL_CLIENT_PORT_BALANCE:
2006 switch (request->packet->src_ipaddr.af) {
2008 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2009 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2012 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2013 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2019 fr_hash_update(&request->packet->src_port,
2020 sizeof(request->packet->src_port), hash);
2021 start = hash % pool->num_home_servers;
2024 case HOME_POOL_KEYED_BALANCE:
2025 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY, 0)) != NULL) {
2026 hash = fr_hash(vp->vp_strvalue, vp->length);
2027 start = hash % pool->num_home_servers;
2032 case HOME_POOL_LOAD_BALANCE:
2033 case HOME_POOL_FAIL_OVER:
2037 default: /* this shouldn't happen... */
2044 * Starting with the home server we chose, loop through
2045 * all home servers. If the current one is dead, skip
2046 * it. If it is too busy, skip it.
2048 * Otherwise, use it.
2050 for (count = 0; count < pool->num_home_servers; count++) {
2051 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
2053 if (!home) continue;
2056 * Skip dead home servers.
2058 if (home->state == HOME_STATE_IS_DEAD) {
2063 * This home server is too busy. Choose another one.
2065 if (home->currently_outstanding >= home->max_outstanding) {
2071 * We read the packet from a detail file, AND it
2072 * came from this server. Don't re-proxy it
2075 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2076 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
2077 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2083 * It's zombie, so we remember the first zombie
2084 * we find, but we don't mark it as a "live"
2087 if (home->state == HOME_STATE_ZOMBIE) {
2088 if (!zombie) zombie = home;
2093 * We've found the first "live" one. Use that.
2095 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2101 * Otherwise we're doing some kind of load balancing.
2102 * If we haven't found one yet, pick this one.
2109 RDEBUG3("PROXY %s %d\t%s %d",
2110 found->name, found->currently_outstanding,
2111 home->name, home->currently_outstanding);
2114 * Prefer this server if it's less busy than the
2115 * one we had previously found.
2117 if (home->currently_outstanding < found->currently_outstanding) {
2118 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2119 home->name, found->name);
2125 * Ignore servers which are busier than the one
2128 if (home->currently_outstanding > found->currently_outstanding) {
2129 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2130 home->name, found->name);
2135 * From the list of servers which have the same
2136 * load, choose one at random.
2138 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2141 } /* loop over the home servers */
2144 * We have no live servers, BUT we have a zombie. Use
2145 * the zombie as a last resort.
2147 if (!found && zombie) {
2153 * There's a fallback if they're all dead.
2155 if (!found && pool->fallback) {
2156 found = pool->fallback;
2162 * Allocate the proxy packet, only if it wasn't
2163 * already allocated by a module. This check is
2164 * mainly to support the proxying of EAP-TTLS and
2165 * EAP-PEAP tunneled requests.
2167 * In those cases, the EAP module creates a
2168 * "fake" request, and recursively passes it
2169 * through the authentication stage of the
2170 * server. The module then checks if the request
2171 * was supposed to be proxied, and if so, creates
2172 * a proxy packet from the TUNNELED request, and
2173 * not from the EAP request outside of the
2176 * The proxy then works like normal, except that
2177 * the response packet is "eaten" by the EAP
2178 * module, and encapsulated into an EAP packet.
2180 if (!request->proxy) {
2181 if ((request->proxy = rad_alloc(TRUE)) == NULL) {
2182 radlog(L_ERR|L_CONS, "no memory");
2187 * Copy the request, then look up name
2188 * and plain-text password in the copy.
2190 * Note that the User-Name attribute is
2191 * the *original* as sent over by the
2192 * client. The Stripped-User-Name
2193 * attribute is the one hacked through
2196 request->proxy->vps = paircopy(request->packet->vps);
2200 * Update the various fields as appropriate.
2202 request->proxy->src_ipaddr = found->src_ipaddr;
2203 request->proxy->src_port = 0;
2204 request->proxy->dst_ipaddr = found->ipaddr;
2205 request->proxy->dst_port = found->port;
2206 request->home_server = found;
2209 * We're supposed to add a Message-Authenticator
2210 * if it doesn't exist, and it doesn't exist.
2212 if (found->message_authenticator &&
2213 (request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2214 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0)) {
2215 radius_pairmake(request, &request->proxy->vps,
2216 "Message-Authenticator", "0x00",
2224 * No live match found, and no fallback to the "DEFAULT"
2225 * realm. We fix this by blindly marking all servers as
2226 * "live". But only do it for ones that don't support
2227 * "pings", as they will be marked live when they
2228 * actually are live.
2230 if (!realm_config->fallback &&
2231 realm_config->wake_all_if_all_dead) {
2232 for (count = 0; count < pool->num_home_servers; count++) {
2233 home_server *home = pool->servers[count];
2235 if (!home) continue;
2237 if ((home->state == HOME_STATE_IS_DEAD) &&
2238 (home->ping_check == HOME_PING_CHECK_NONE)) {
2239 home->state = HOME_STATE_ALIVE;
2240 if (!found) found = home;
2244 if (found) goto update_and_return;
2248 * Still nothing. Look up the DEFAULT realm, but only
2249 * if we weren't looking up the NULL or DEFAULT realms.
2251 if (realm_config->fallback &&
2253 (strcmp(realmname, "NULL") != 0) &&
2254 (strcmp(realmname, "DEFAULT") != 0)) {
2255 REALM *rd = realm_find("DEFAULT");
2257 if (!rd) return NULL;
2260 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2261 pool = rd->auth_pool;
2263 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2264 pool = rd->acct_pool;
2266 if (!pool) return NULL;
2268 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2269 return home_server_ldb(rd->name, pool, request);
2273 * Still haven't found anything. Oh well.
2279 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port, int proto)
2283 memset(&myhome, 0, sizeof(myhome));
2284 myhome.ipaddr = *ipaddr;
2287 myhome.proto = proto;
2289 myhome.proto = IPPROTO_UDP;
2291 myhome.server = NULL; /* we're not called for internal proxying */
2293 return rbtree_finddata(home_servers_byaddr, &myhome);
2297 home_server *home_server_byname(const char *name, int type)
2301 memset(&myhome, 0, sizeof(myhome));
2305 return rbtree_finddata(home_servers_byname, &myhome);
2310 home_server *home_server_bynumber(int number)
2314 memset(&myhome, 0, sizeof(myhome));
2315 myhome.number = number;
2316 myhome.server = NULL; /* we're not called for internal proxying */
2318 return rbtree_finddata(home_servers_bynumber, &myhome);
2322 home_pool_t *home_pool_byname(const char *name, int type)
2326 memset(&mypool, 0, sizeof(mypool));
2328 mypool.server_type = type;
2329 return rbtree_finddata(home_pools_byname, &mypool);