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);
186 * Xlat for %{home_server:foo}
188 static size_t xlat_home_server(UNUSED void *instance, REQUEST *request,
189 char *fmt, char *out, size_t outlen,
190 UNUSED RADIUS_ESCAPE_STRING func)
192 const char *value = NULL;
195 if (!fmt || !out || (outlen < 1)) return 0;
197 if (!request || !request->home_server) {
202 cp = cf_pair_find(request->home_server->cs, fmt);
203 if (!cp || !(value = cf_pair_value(cp))) {
208 strlcpy(out, value, outlen);
215 * Xlat for %{home_server_pool:foo}
217 static size_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
218 char *fmt, char *out, size_t outlen,
219 UNUSED RADIUS_ESCAPE_STRING func)
221 const char *value = NULL;
224 if (!fmt || !out || (outlen < 1)) return 0;
226 if (!request || !request->home_pool) {
231 cp = cf_pair_find(request->home_pool->cs, fmt);
232 if (!cp || !(value = cf_pair_value(cp))) {
237 strlcpy(out, value, outlen);
243 void realms_free(void)
247 rbtree_free(home_servers_bynumber);
248 home_servers_bynumber = NULL;
251 rbtree_free(home_servers_byname);
252 home_servers_byname = NULL;
254 rbtree_free(home_servers_byaddr);
255 home_servers_byaddr = NULL;
257 rbtree_free(home_pools_byname);
258 home_pools_byname = NULL;
261 rbtree_free(realms_byname);
262 realms_byname = NULL;
266 realm_regex_t *this, *next;
268 for (this = realms_regex; this != NULL; this = next) {
283 static CONF_PARSER limit_config[] = {
284 { "max_connections", PW_TYPE_INTEGER,
285 offsetof(home_server, max_connections), NULL, "16" },
287 { "max_requests", PW_TYPE_INTEGER,
288 offsetof(home_server,max_requests), NULL, "0" },
290 { "lifetime", PW_TYPE_INTEGER,
291 offsetof(home_server,lifetime), NULL, "0" },
293 { "idle_timeout", PW_TYPE_INTEGER,
294 offsetof(home_server,idle_timeout), NULL, "0" },
296 { NULL, -1, 0, NULL, NULL } /* end the list */
299 static struct in_addr hs_ip4addr;
300 static struct in6_addr hs_ip6addr;
301 static char *hs_srcipaddr = NULL;
302 static char *hs_type = NULL;
303 static char *hs_check = NULL;
304 static char *hs_virtual_server = NULL;
306 static char *hs_proto = NULL;
309 static CONF_PARSER home_server_config[] = {
310 { "ipaddr", PW_TYPE_IPADDR,
311 0, &hs_ip4addr, NULL },
312 { "ipv6addr", PW_TYPE_IPV6ADDR,
313 0, &hs_ip6addr, NULL },
314 { "virtual_server", PW_TYPE_STRING_PTR,
315 0, &hs_virtual_server, NULL },
317 { "port", PW_TYPE_INTEGER,
318 offsetof(home_server,port), NULL, "0" },
320 { "type", PW_TYPE_STRING_PTR,
324 { "proto", PW_TYPE_STRING_PTR,
325 0, &hs_proto, NULL },
328 { "secret", PW_TYPE_STRING_PTR,
329 offsetof(home_server,secret), NULL, NULL},
331 { "src_ipaddr", PW_TYPE_STRING_PTR,
332 0, &hs_srcipaddr, NULL },
334 { "response_window", PW_TYPE_INTEGER,
335 offsetof(home_server,response_window), NULL, "30" },
336 { "no_response_fail", PW_TYPE_BOOLEAN,
337 offsetof(home_server,no_response_fail), NULL, NULL },
338 { "max_outstanding", PW_TYPE_INTEGER,
339 offsetof(home_server,max_outstanding), NULL, "65536" },
340 { "require_message_authenticator", PW_TYPE_BOOLEAN,
341 offsetof(home_server, message_authenticator), 0, NULL },
343 { "zombie_period", PW_TYPE_INTEGER,
344 offsetof(home_server,zombie_period), NULL, "40" },
345 { "status_check", PW_TYPE_STRING_PTR,
346 0, &hs_check, "none" },
347 { "ping_check", PW_TYPE_STRING_PTR,
348 0, &hs_check, NULL },
350 { "ping_interval", PW_TYPE_INTEGER,
351 offsetof(home_server,ping_interval), NULL, "30" },
352 { "check_interval", PW_TYPE_INTEGER,
353 offsetof(home_server,ping_interval), NULL, "30" },
354 { "num_answers_to_alive", PW_TYPE_INTEGER,
355 offsetof(home_server,num_pings_to_alive), NULL, "3" },
356 { "num_pings_to_alive", PW_TYPE_INTEGER,
357 offsetof(home_server,num_pings_to_alive), NULL, "3" },
358 { "revive_interval", PW_TYPE_INTEGER,
359 offsetof(home_server,revive_interval), NULL, "300" },
360 { "status_check_timeout", PW_TYPE_INTEGER,
361 offsetof(home_server,ping_timeout), NULL, "4" },
363 { "username", PW_TYPE_STRING_PTR,
364 offsetof(home_server,ping_user_name), NULL, NULL},
365 { "password", PW_TYPE_STRING_PTR,
366 offsetof(home_server,ping_user_password), NULL, NULL},
369 { "historic_average_window", PW_TYPE_INTEGER,
370 offsetof(home_server,ema.window), NULL, NULL },
374 { "irt", PW_TYPE_INTEGER,
375 offsetof(home_server, coa_irt), 0, Stringify(2) },
376 { "mrt", PW_TYPE_INTEGER,
377 offsetof(home_server, coa_mrt), 0, Stringify(16) },
378 { "mrc", PW_TYPE_INTEGER,
379 offsetof(home_server, coa_mrc), 0, Stringify(5) },
380 { "mrd", PW_TYPE_INTEGER,
381 offsetof(home_server, coa_mrd), 0, Stringify(30) },
384 { "limit", PW_TYPE_SUBSECTION, 0, NULL, (const void *) limit_config },
386 { NULL, -1, 0, NULL, NULL } /* end the list */
390 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs, int pool_type)
397 free(hs_virtual_server); /* used only for printing during parsing */
398 hs_virtual_server = NULL;
400 name2 = cf_section_name1(cs);
401 if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
402 cf_log_err(cf_sectiontoitem(cs),
403 "Section is not a home_server.");
407 name2 = cf_section_name2(cs);
409 cf_log_err(cf_sectiontoitem(cs),
410 "Home server section is missing a name.");
414 home = rad_malloc(sizeof(*home));
415 memset(home, 0, sizeof(*home));
421 * Authentication servers have a default "no_response_fail = 0".
422 * Accounting servers have a default "no_response_fail = 1".
424 * This is because authentication packets are retried, so
425 * they can fail over to another home server. Accounting
426 * packets are not retried, so they cannot fail over, and
427 * instead should be rejected immediately.
429 home->no_response_fail = 2;
431 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
432 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
433 if (cf_section_parse(cs, home, home_server_config) < 0) {
438 * Figure out which one to use.
440 if (cf_pair_find(cs, "ipaddr")) {
441 home->ipaddr.af = AF_INET;
442 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
444 } else if (cf_pair_find(cs, "ipv6addr")) {
445 home->ipaddr.af = AF_INET6;
446 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
448 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
449 home->ipaddr.af = AF_UNSPEC;
450 home->server = cf_pair_value(cp);
452 cf_log_err(cf_sectiontoitem(cs),
453 "Invalid value for virtual_server");
457 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
459 cf_log_err(cf_sectiontoitem(cs),
460 "No such server %s", home->server);
465 * When CoA is used, the user has to specify the type
466 * of the home server, even when they point to
469 home->secret = strdup("");
473 cf_log_err(cf_sectiontoitem(cs),
474 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
491 if (!home->port || (home->port > 65535)) {
492 cf_log_err(cf_sectiontoitem(cs),
493 "No port, or invalid port defined for home server %s.",
499 cf_log_err(cf_sectiontoitem(cs),
500 "Fatal error! Home server %s is ourselves!",
506 cf_log_err(cf_sectiontoitem(cs),
507 "No shared secret defined for home server %s.",
513 * Use a reasonable default.
516 if (!hs_type) hs_type = strdup("auth+acct");
518 if (strcasecmp(hs_type, "auth") == 0) {
519 home->type = HOME_TYPE_AUTH;
520 if (home->no_response_fail == 2) home->no_response_fail = 0;
521 if (pool_type != home->type) {
523 cf_log_err(cf_sectiontoitem(cs),
524 "Server pool cannot include home server %s of type \"%s\"",
529 } else if (strcasecmp(hs_type, "acct") == 0) {
530 home->type = HOME_TYPE_ACCT;
531 if (home->no_response_fail == 2) home->no_response_fail = 1;
532 if (pool_type != home->type) goto mismatch;
534 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
535 home->type = HOME_TYPE_AUTH;
539 } else if (strcasecmp(hs_type, "coa") == 0) {
540 home->type = HOME_TYPE_COA;
543 if (pool_type != home->type) goto mismatch;
545 if (home->server != NULL) {
546 cf_log_err(cf_sectiontoitem(cs),
547 "Home servers of type \"coa\" cannot point to a virtual server");
553 cf_log_err(cf_sectiontoitem(cs),
554 "Invalid type \"%s\" for home server %s.",
561 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
562 home->ping_check = HOME_PING_CHECK_NONE;
564 } else if (strcasecmp(hs_check, "status-server") == 0) {
565 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
567 } else if (strcasecmp(hs_check, "request") == 0) {
568 home->ping_check = HOME_PING_CHECK_REQUEST;
571 cf_log_err(cf_sectiontoitem(cs),
572 "Invalid ping_check \"%s\" for home server %s.",
579 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
580 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
581 if (!home->ping_user_name) {
582 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable ping checks");
586 if ((home->type == HOME_TYPE_AUTH) &&
587 !home->ping_user_password) {
588 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable ping checks");
593 home->proto = IPPROTO_UDP;
596 if (strcmp(hs_proto, "udp") == 0) {
600 } else if (strcmp(hs_proto, "tcp") == 0) {
603 home->proto = IPPROTO_TCP;
606 cf_log_err(cf_sectiontoitem(cs),
607 "Unknown proto \"%s\".", hs_proto);
614 rbtree_finddata(home_servers_byaddr, home)) {
615 cf_log_err(cf_sectiontoitem(cs), "Duplicate home server");
620 * If the home is a virtual server, don't look up source IP.
623 rad_assert(home->ipaddr.af != AF_UNSPEC);
626 * Otherwise look up the source IP using the same
627 * address family as the destination IP.
630 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
631 cf_log_err(cf_sectiontoitem(cs), "Failed parsing src_ipaddr");
637 * Source isn't specified: Source is
638 * the correct address family, but all zeros.
640 home->src_ipaddr.af = home->ipaddr.af;
647 if (!rbtree_insert(home_servers_byname, home)) {
648 cf_log_err(cf_sectiontoitem(cs),
649 "Internal error %d adding home server %s.",
655 !rbtree_insert(home_servers_byaddr, home)) {
656 rbtree_deletebydata(home_servers_byname, home);
657 cf_log_err(cf_sectiontoitem(cs),
658 "Internal error %d adding home server %s.",
664 home->number = home_server_max_number++;
665 if (!rbtree_insert(home_servers_bynumber, home)) {
666 rbtree_deletebydata(home_servers_byname, home);
667 if (home->ipaddr.af != AF_UNSPEC) {
668 rbtree_deletebydata(home_servers_byname, home);
670 cf_log_err(cf_sectiontoitem(cs),
671 "Internal error %d adding home server %s.",
677 if (home->max_outstanding < 8) home->max_outstanding = 8;
678 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
680 if (home->ping_interval < 6) home->ping_interval = 6;
681 if (home->ping_interval > 120) home->ping_interval = 120;
683 if (home->response_window < 1) home->response_window = 1;
684 if (home->response_window > 60) home->response_window = 60;
686 if (home->zombie_period < 1) home->zombie_period = 1;
687 if (home->zombie_period > 120) home->zombie_period = 120;
689 if (home->zombie_period < home->response_window) {
690 home->zombie_period = home->response_window;
693 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
694 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
696 if (home->ping_timeout < 3) home->ping_timeout = 3;
697 if (home->ping_timeout > 10) home->ping_timeout = 10;
699 if (home->revive_interval < 60) home->revive_interval = 60;
700 if (home->revive_interval > 3600) home->revive_interval = 3600;
703 if (home->coa_irt < 1) home->coa_irt = 1;
704 if (home->coa_irt > 5) home->coa_irt = 5;
706 if (home->coa_mrc < 0) home->coa_mrc = 0;
707 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
709 if (home->coa_mrt < 0) home->coa_mrt = 0;
710 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
712 if (home->coa_mrd < 5) home->coa_mrd = 5;
713 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
716 if (home->max_connections > 1024) home->max_connections = 1024;
720 * UDP sockets can't be connection limited.
722 if (home->proto != IPPROTO_TCP) home->max_connections = 0;
725 if ((home->idle_timeout > 0) && (home->idle_timeout < 5))
726 home->idle_timeout = 5;
727 if ((home->lifetime > 0) && (home->lifetime < 5))
729 if ((home->lifetime > 0) && (home->idle_timeout > home->lifetime))
730 home->idle_timeout = 0;
733 home_server *home2 = rad_malloc(sizeof(*home2));
735 memcpy(home2, home, sizeof(*home2));
737 home2->type = HOME_TYPE_ACCT;
739 home2->ping_user_password = NULL;
742 if (home->no_response_fail == 2) home->no_response_fail = 0;
743 if (home2->no_response_fail == 2) home2->no_response_fail = 1;
745 if (!rbtree_insert(home_servers_byname, home2)) {
746 cf_log_err(cf_sectiontoitem(cs),
747 "Internal error %d adding home server %s.",
754 !rbtree_insert(home_servers_byaddr, home2)) {
755 rbtree_deletebydata(home_servers_byname, home2);
756 cf_log_err(cf_sectiontoitem(cs),
757 "Internal error %d adding home server %s.",
764 home2->number = home_server_max_number++;
765 if (!rbtree_insert(home_servers_bynumber, home2)) {
766 rbtree_deletebydata(home_servers_byname, home2);
767 if (!home2->server) {
768 rbtree_deletebydata(home_servers_byname, home2);
770 cf_log_err(cf_sectiontoitem(cs),
771 "Internal error %d adding home server %s.",
783 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
784 int server_type, int num_home_servers)
788 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
790 if (!pool) return NULL; /* just for pairanoia */
792 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
797 pool->server_type = server_type;
798 pool->num_home_servers = num_home_servers;
803 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
804 const char *name, int server_type,
807 home_server myhome, *home;
808 CONF_SECTION *server_cs;
811 cf_log_err(cf_pairtoitem(cp),
812 "No value given for home_server.");
817 myhome.type = server_type;
818 home = rbtree_finddata(home_servers_byname, &myhome);
824 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
826 cf_log_err(cf_pairtoitem(cp),
827 "Unknown home_server \"%s\".", name);
831 if (!home_server_add(rc, server_cs, server_type)) {
835 home = rbtree_finddata(home_servers_byname, &myhome);
837 cf_log_err(cf_pairtoitem(cp),
838 "Internal error %d adding home server \"%s\".",
848 static int server_pool_add(realm_config_t *rc,
849 CONF_SECTION *cs, int server_type, int do_print)
852 home_pool_t *pool = NULL;
855 int num_home_servers;
858 name2 = cf_section_name1(cs);
859 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
860 (strcasecmp(name2, "home_server_pool") != 0))) {
861 cf_log_err(cf_sectiontoitem(cs),
862 "Section is not a home_server_pool.");
866 name2 = cf_section_name2(cs);
868 cf_log_err(cf_sectiontoitem(cs),
869 "Server pool section is missing a name.");
874 * Count the home servers and initalize them.
876 num_home_servers = 0;
877 for (cp = cf_pair_find(cs, "home_server");
879 cp = cf_pair_find_next(cs, cp, "home_server")) {
882 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
883 server_type, &home)) {
888 if (num_home_servers == 0) {
889 cf_log_err(cf_sectiontoitem(cs),
890 "No home servers defined in pool %s",
895 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
901 * Fallback servers must be defined, and must be
904 cp = cf_pair_find(cs, "fallback");
907 if (server_type == HOME_TYPE_COA) {
908 cf_log_err(cf_sectiontoitem(cs), "Home server pools of type \"coa\" cannot have a fallback virtual server.");
913 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
914 server_type, &pool->fallback)) {
919 if (!pool->fallback->server) {
920 cf_log_err(cf_sectiontoitem(cs), "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
925 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
927 cp = cf_pair_find(cs, "type");
929 static FR_NAME_NUMBER pool_types[] = {
930 { "load-balance", HOME_POOL_LOAD_BALANCE },
931 { "fail-over", HOME_POOL_FAIL_OVER },
932 { "round_robin", HOME_POOL_LOAD_BALANCE },
933 { "fail_over", HOME_POOL_FAIL_OVER },
934 { "client-balance", HOME_POOL_CLIENT_BALANCE },
935 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
936 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
940 value = cf_pair_value(cp);
942 cf_log_err(cf_pairtoitem(cp),
943 "No value given for type.");
947 pool->type = fr_str2int(pool_types, value, 0);
949 cf_log_err(cf_pairtoitem(cp),
950 "Unknown type \"%s\".",
955 if (do_print) cf_log_info(cs, "\ttype = %s", value);
958 cp = cf_pair_find(cs, "virtual_server");
960 pool->virtual_server = cf_pair_value(cp);
961 if (!pool->virtual_server) {
962 cf_log_err(cf_pairtoitem(cp), "No value given for virtual_server");
967 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
970 if (!cf_section_sub_find_name2(rc->cs, "server",
971 pool->virtual_server)) {
972 cf_log_err(cf_pairtoitem(cp), "No such server %s",
973 pool->virtual_server);
979 num_home_servers = 0;
980 for (cp = cf_pair_find(cs, "home_server");
982 cp = cf_pair_find_next(cs, cp, "home_server")) {
985 value = cf_pair_value(cp);
987 memset(&myhome, 0, sizeof(&myhome));
989 myhome.type = server_type;
991 home = rbtree_finddata(home_servers_byname, &myhome);
993 DEBUG2("Internal sanity check failed");
998 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
1002 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1003 pool->servers[num_home_servers++] = home;
1004 } /* loop over home_server's */
1006 if (pool->fallback && do_print) {
1007 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1010 if (!rbtree_insert(home_pools_byname, pool)) {
1011 rad_assert("Internal sanity check failed");
1015 if (do_print) cf_log_info(cs, " }");
1017 cf_data_add(cs, "home_server_pool", pool, free);
1019 rad_assert(pool->server_type != 0);
1024 if (do_print) cf_log_info(cs, " }");
1030 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1032 const char *name, const char *secret,
1033 home_pool_type_t ldflag, home_pool_t **pool_p,
1034 int type, const char *server)
1037 int i, insert_point, num_home_servers;
1038 home_server myhome, *home;
1039 home_pool_t mypool, *pool;
1040 CONF_SECTION *subcs;
1042 rc = rc; /* -Wunused */
1051 * LOCAL realms get sanity checked, and nothing else happens.
1053 if (strcmp(name, "LOCAL") == 0) {
1055 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
1062 return 0; /* Not proxying. Can't do non-LOCAL realms */
1065 mypool.name = realm;
1066 mypool.server_type = type;
1067 pool = rbtree_finddata(home_pools_byname, &mypool);
1069 if (pool->type != ldflag) {
1070 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
1074 if (pool->server_type != type) {
1075 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
1082 home = rbtree_finddata(home_servers_byname, &myhome);
1084 if (secret && (strcmp(home->secret, secret) != 0)) {
1085 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
1089 if (home->type != type) {
1090 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
1095 * See if the home server is already listed
1096 * in the pool. If so, do nothing else.
1098 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1099 if (pool->servers[i] == home) {
1106 * If we do have a pool, check that there is room to
1107 * insert the home server we've found, or the one that we
1110 * Note that we insert it into the LAST available
1111 * position, in order to maintain the same order as in
1112 * the configuration files.
1116 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1117 if (pool->servers[i]) break;
1119 if (!pool->servers[i]) {
1124 if (insert_point < 0) {
1125 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);
1131 * No home server, allocate one.
1137 home = rad_malloc(sizeof(*home));
1138 memset(home, 0, sizeof(*home));
1141 home->hostname = name;
1143 home->secret = secret;
1145 home->proto = IPPROTO_UDP;
1147 p = strchr(name, ':');
1149 if (type == HOME_TYPE_AUTH) {
1150 home->port = PW_AUTH_UDP_PORT;
1152 home->port = PW_ACCT_UDP_PORT;
1158 } else if (p == name) {
1159 cf_log_err(cf_sectiontoitem(cs),
1160 "Invalid hostname %s.",
1166 home->port = atoi(p + 1);
1167 if ((home->port == 0) || (home->port > 65535)) {
1168 cf_log_err(cf_sectiontoitem(cs),
1175 q = rad_malloc((p - name) + 1);
1176 memcpy(q, name, (p - name));
1182 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1183 cf_log_err(cf_sectiontoitem(cs),
1184 "Failed looking up hostname %s.",
1190 home->src_ipaddr.af = home->ipaddr.af;
1192 home->ipaddr.af = AF_UNSPEC;
1193 home->server = server;
1198 * Use the old-style configuration.
1200 home->max_outstanding = 65535*16;
1201 home->zombie_period = rc->retry_delay * rc->retry_count;
1202 if (home->zombie_period == 0) home->zombie_period =30;
1203 home->response_window = home->zombie_period - 1;
1205 home->ping_check = HOME_PING_CHECK_NONE;
1207 home->revive_interval = rc->dead_time;
1209 if (rbtree_finddata(home_servers_byaddr, home)) {
1210 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
1215 if (!rbtree_insert(home_servers_byname, home)) {
1216 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1221 if (!rbtree_insert(home_servers_byaddr, home)) {
1222 rbtree_deletebydata(home_servers_byname, home);
1223 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1229 home->number = home_server_max_number++;
1230 if (!rbtree_insert(home_servers_bynumber, home)) {
1231 rbtree_deletebydata(home_servers_byname, home);
1232 if (home->ipaddr.af != AF_UNSPEC) {
1233 rbtree_deletebydata(home_servers_byname, home);
1235 cf_log_err(cf_sectiontoitem(cs),
1236 "Internal error %d adding home server %s.",
1245 * We now have a home server, see if we can insert it
1246 * into pre-existing pool.
1248 if (insert_point >= 0) {
1249 rad_assert(pool != NULL);
1250 pool->servers[insert_point] = home;
1254 rad_assert(pool == NULL);
1255 rad_assert(home != NULL);
1258 * Count the old-style realms of this name.
1260 num_home_servers = 0;
1261 for (subcs = cf_section_find_next(cs, NULL, "realm");
1263 subcs = cf_section_find_next(cs, subcs, "realm")) {
1264 const char *this = cf_section_name2(subcs);
1266 if (!this || (strcmp(this, realm) != 0)) continue;
1270 if (num_home_servers == 0) {
1271 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
1276 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1279 pool->servers[0] = home;
1281 if (!rbtree_insert(home_pools_byname, pool)) {
1282 rad_assert("Internal sanity check failed");
1292 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1295 const char *secret = NULL;
1296 home_pool_type_t ldflag;
1299 cp = cf_pair_find(cs, "ldflag");
1300 ldflag = HOME_POOL_FAIL_OVER;
1302 host = cf_pair_value(cp);
1304 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
1308 if (strcasecmp(host, "fail_over") == 0) {
1309 cf_log_info(cs, "\tldflag = fail_over");
1311 } else if (strcasecmp(host, "round_robin") == 0) {
1312 ldflag = HOME_POOL_LOAD_BALANCE;
1313 cf_log_info(cs, "\tldflag = round_robin");
1316 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
1319 } /* else don't print it. */
1322 * Allow old-style if it doesn't exist, or if it exists and
1325 cp = cf_pair_find(cs, "authhost");
1327 host = cf_pair_value(cp);
1329 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
1333 if (strcmp(host, "LOCAL") != 0) {
1334 cp = cf_pair_find(cs, "secret");
1336 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1340 secret = cf_pair_value(cp);
1342 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1347 cf_log_info(cs, "\tauthhost = %s", host);
1349 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1350 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1355 cp = cf_pair_find(cs, "accthost");
1357 host = cf_pair_value(cp);
1359 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1364 * Don't look for a secret again if it was found
1367 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1368 cp = cf_pair_find(cs, "secret");
1370 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1374 secret = cf_pair_value(cp);
1376 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1381 cf_log_info(cs, "\taccthost = %s", host);
1383 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1384 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1389 cp = cf_pair_find(cs, "virtual_server");
1391 host = cf_pair_value(cp);
1393 cf_log_err(cf_pairtoitem(cp), "No value specified for virtual_server");
1397 cf_log_info(cs, "\tvirtual_server = %s", host);
1399 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1400 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1403 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1404 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1409 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1417 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1418 const char *name, home_pool_t **dest,
1419 int server_type, int do_print)
1421 home_pool_t mypool, *pool;
1424 mypool.server_type = server_type;
1426 pool = rbtree_finddata(home_pools_byname, &mypool);
1428 CONF_SECTION *pool_cs;
1430 pool_cs = cf_section_sub_find_name2(rc->cs,
1434 pool_cs = cf_section_sub_find_name2(rc->cs,
1439 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1443 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1447 pool = rbtree_finddata(home_pools_byname, &mypool);
1449 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1454 if (pool->server_type != server_type) {
1455 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1466 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1472 home_pool_t *auth_pool, *acct_pool;
1473 const char *auth_pool_name, *acct_pool_name;
1476 name2 = cf_section_name1(cs);
1477 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1478 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1482 name2 = cf_section_name2(cs);
1484 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1489 auth_pool = acct_pool = NULL;
1490 auth_pool_name = acct_pool_name = NULL;
1493 * Prefer new configuration to old one.
1495 cp = cf_pair_find(cs, "pool");
1496 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1497 if (cp) auth_pool_name = cf_pair_value(cp);
1498 if (cp && auth_pool_name) {
1499 acct_pool_name = auth_pool_name;
1500 if (!add_pool_to_realm(rc, cs,
1501 auth_pool_name, &auth_pool,
1502 HOME_TYPE_AUTH, 1)) {
1505 if (!add_pool_to_realm(rc, cs,
1506 auth_pool_name, &acct_pool,
1507 HOME_TYPE_ACCT, 0)) {
1512 cp = cf_pair_find(cs, "auth_pool");
1513 if (cp) auth_pool_name = cf_pair_value(cp);
1514 if (cp && auth_pool_name) {
1516 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1519 if (!add_pool_to_realm(rc, cs,
1520 auth_pool_name, &auth_pool,
1521 HOME_TYPE_AUTH, 1)) {
1526 cp = cf_pair_find(cs, "acct_pool");
1527 if (cp) acct_pool_name = cf_pair_value(cp);
1528 if (cp && acct_pool_name) {
1529 int do_print = TRUE;
1532 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1537 (strcmp(auth_pool_name, acct_pool_name) != 0)) {
1541 if (!add_pool_to_realm(rc, cs,
1542 acct_pool_name, &acct_pool,
1543 HOME_TYPE_ACCT, do_print)) {
1549 cf_log_info(cs, " realm %s {", name2);
1553 * The realm MAY already exist if it's an old-style realm.
1554 * In that case, merge the old-style realm with this one.
1556 r = realm_find2(name2);
1557 if (r && (strcmp(r->name, name2) == 0)) {
1558 if (cf_pair_find(cs, "auth_pool") ||
1559 cf_pair_find(cs, "acct_pool")) {
1560 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1564 if (!old_realm_config(rc, cs, r)) {
1568 cf_log_info(cs, " } # realm %s", name2);
1574 if (name2[0] == '~') {
1578 * Include substring matches.
1580 if (regcomp(®, name2 + 1,
1581 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
1582 cf_log_err(cf_sectiontoitem(cs),
1583 "Invalid regex in realm \"%s\"", name2);
1590 r = rad_malloc(sizeof(*r));
1591 memset(r, 0, sizeof(*r));
1596 r->auth_pool = auth_pool;
1597 r->acct_pool = acct_pool;
1599 if (auth_pool_name &&
1600 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1601 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1603 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1604 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1608 cp = cf_pair_find(cs, "nostrip");
1609 if (cp && (cf_pair_value(cp) == NULL)) {
1611 cf_log_info(cs, "\tnostrip");
1615 * We're a new-style realm. Complain if we see the old
1618 if (r->auth_pool || r->acct_pool) {
1619 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1620 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1621 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1622 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1623 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1628 * The realm MAY be an old-style realm, as there
1629 * was no auth_pool or acct_pool. Double-check
1630 * it, just to be safe.
1632 } else if (!old_realm_config(rc, cs, r)) {
1638 * It's a regex. Add it to a separate list.
1640 if (name2[0] == '~') {
1641 realm_regex_t *rr, **last;
1643 rr = rad_malloc(sizeof(*rr));
1645 last = &realms_regex;
1646 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1654 cf_log_info(cs, " }");
1659 if (!rbtree_insert(realms_byname, r)) {
1660 rad_assert("Internal sanity check failed");
1664 cf_log_info(cs, " }");
1669 cf_log_info(cs, " } # realm %s", name2);
1675 static const FR_NAME_NUMBER home_server_types[] = {
1676 { "auth", HOME_TYPE_AUTH },
1677 { "auth+acct", HOME_TYPE_AUTH },
1678 { "acct", HOME_TYPE_ACCT },
1679 { "coa", HOME_TYPE_COA },
1683 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1686 const char *name, *type;
1688 CONF_SECTION *server_cs;
1690 cp = cf_pair_find(cs, "home_server");
1692 cf_log_err(cf_sectiontoitem(cs), "Pool does not contain a \"home_server\" entry");
1693 return HOME_TYPE_INVALID;
1696 name = cf_pair_value(cp);
1698 cf_log_err(cf_pairtoitem(cp), "home_server entry does not reference a home server");
1699 return HOME_TYPE_INVALID;
1702 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1704 cf_log_err(cf_pairtoitem(cp), "home_server \"%s\" does not exist", name);
1705 return HOME_TYPE_INVALID;
1708 cp = cf_pair_find(server_cs, "type");
1710 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s does not contain a \"type\" entry", name);
1711 return HOME_TYPE_INVALID;
1714 type = cf_pair_value(cp);
1716 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an empty \"type\" entry", name);
1717 return HOME_TYPE_INVALID;
1720 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1721 if (home == HOME_TYPE_INVALID) {
1722 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1723 return HOME_TYPE_INVALID;
1726 return home; /* 'cause we miss it so much */
1730 int realms_init(CONF_SECTION *config)
1733 realm_config_t *rc, *old_rc;
1735 if (realms_byname) return 1;
1737 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1738 if (!realms_byname) {
1744 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
1745 if (!home_servers_byaddr) {
1750 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1751 if (!home_servers_byname) {
1757 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1758 if (!home_servers_bynumber) {
1764 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1765 if (!home_pools_byname) {
1771 rc = rad_malloc(sizeof(*rc));
1772 memset(rc, 0, sizeof(*rc));
1776 cs = cf_subsection_find_next(config, NULL, "proxy");
1778 cf_section_parse(cs, rc, proxy_config);
1780 rc->dead_time = DEAD_TIME;
1781 rc->retry_count = RETRY_COUNT;
1782 rc->retry_delay = RETRY_DELAY;
1784 rc->wake_all_if_all_dead= 0;
1788 for (cs = cf_subsection_find_next(config, NULL, "realm");
1790 cs = cf_subsection_find_next(config, cs, "realm")) {
1791 if (!realm_add(rc, cs)) {
1800 * CoA pools aren't tied to realms.
1802 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1804 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1808 * Pool was already loaded.
1810 if (cf_data_find(cs, "home_server_pool")) continue;
1812 type = pool_peek_type(config, cs);
1813 if (type == HOME_TYPE_INVALID) return 0;
1815 if (!server_pool_add(rc, cs, type, TRUE)) {
1823 xlat_register("home_server", xlat_home_server, NULL);
1824 xlat_register("home_server_pool", xlat_server_pool, NULL);
1828 * Swap pointers atomically.
1830 old_rc = realm_config;
1838 * Find a realm where "name" might be the regex.
1840 REALM *realm_find2(const char *name)
1845 if (!name) name = "NULL";
1847 myrealm.name = name;
1848 realm = rbtree_finddata(realms_byname, &myrealm);
1849 if (realm) return realm;
1853 realm_regex_t *this;
1855 for (this = realms_regex; this != NULL; this = this->next) {
1856 if (strcmp(this->realm->name, name) == 0) {
1864 * Couldn't find a realm. Look for DEFAULT.
1866 myrealm.name = "DEFAULT";
1867 return rbtree_finddata(realms_byname, &myrealm);
1872 * Find a realm in the REALM list.
1874 REALM *realm_find(const char *name)
1879 if (!name) name = "NULL";
1881 myrealm.name = name;
1882 realm = rbtree_finddata(realms_byname, &myrealm);
1883 if (realm) return realm;
1887 realm_regex_t *this;
1889 for (this = realms_regex; this != NULL; this = this->next) {
1894 * Include substring matches.
1896 if (regcomp(®, this->realm->name + 1,
1897 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
1901 compare = regexec(®, name, 0, NULL, 0);
1904 if (compare == 0) return this->realm;
1910 * Couldn't find a realm. Look for DEFAULT.
1912 myrealm.name = "DEFAULT";
1913 return rbtree_finddata(realms_byname, &myrealm);
1916 fr_realm_status_t realm_status(const char *name, int flag)
1923 if (!name) return FR_REALM_UNKNOWN;
1925 myrealm.name = name;
1926 realm = rbtree_finddata(realms_byname, &myrealm);
1927 if (!realm) return FR_REALM_UNKNOWN;
1930 pool = realm->auth_pool;
1932 pool = realm->acct_pool;
1935 if (!pool) return FR_REALM_ALIVE; /* local realms are always alive */
1938 * FIXME: We should really have WITH_PROXY around some of
1941 for (i = 0; i < pool->num_home_servers; i++) {
1942 home_server *home = pool->servers[i];
1944 if (home->state != HOME_STATE_IS_DEAD) return FR_REALM_ALIVE;
1948 * There's a LOCAL fallback server. It will do SOMETHING
1949 * with the request. So the realm is alive.
1951 if (pool->fallback) return FR_REALM_ALIVE;
1953 return FR_REALM_DEAD;
1958 home_server *home_server_ldb(const char *realmname,
1959 home_pool_t *pool, REQUEST *request)
1963 home_server *found = NULL;
1964 home_server *zombie = NULL;
1968 * Determine how to pick choose the home server.
1970 switch (pool->type) {
1974 * For load-balancing by client IP address, we
1975 * pick a home server by hashing the client IP.
1977 * This isn't as even a load distribution as
1978 * tracking the State attribute, but it's better
1981 case HOME_POOL_CLIENT_BALANCE:
1982 switch (request->packet->src_ipaddr.af) {
1984 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1985 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1988 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1989 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1995 start = hash % pool->num_home_servers;
1998 case HOME_POOL_CLIENT_PORT_BALANCE:
1999 switch (request->packet->src_ipaddr.af) {
2001 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2002 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2005 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2006 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2012 fr_hash_update(&request->packet->src_port,
2013 sizeof(request->packet->src_port), hash);
2014 start = hash % pool->num_home_servers;
2017 case HOME_POOL_KEYED_BALANCE:
2018 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY)) != NULL) {
2019 hash = fr_hash(vp->vp_strvalue, vp->length);
2020 start = hash % pool->num_home_servers;
2025 case HOME_POOL_LOAD_BALANCE:
2026 case HOME_POOL_FAIL_OVER:
2030 default: /* this shouldn't happen... */
2037 * Starting with the home server we chose, loop through
2038 * all home servers. If the current one is dead, skip
2039 * it. If it is too busy, skip it.
2041 * Otherwise, use it.
2043 for (count = 0; count < pool->num_home_servers; count++) {
2044 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
2046 if (!home) continue;
2049 * Skip dead home servers.
2051 if (home->state == HOME_STATE_IS_DEAD) {
2056 * This home server is too busy. Choose another one.
2058 if (home->currently_outstanding >= home->max_outstanding) {
2064 * We read the packet from a detail file, AND it
2065 * came from this server. Don't re-proxy it
2068 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2069 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
2070 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2076 * It's zombie, so we remember the first zombie
2077 * we find, but we don't mark it as a "live"
2080 if (home->state == HOME_STATE_ZOMBIE) {
2081 if (!zombie) zombie = home;
2086 * We've found the first "live" one. Use that.
2088 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2094 * Otherwise we're doing some kind of load balancing.
2095 * If we haven't found one yet, pick this one.
2102 RDEBUG3("PROXY %s %d\t%s %d",
2103 found->name, found->currently_outstanding,
2104 home->name, home->currently_outstanding);
2107 * Prefer this server if it's less busy than the
2108 * one we had previously found.
2110 if (home->currently_outstanding < found->currently_outstanding) {
2111 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2112 home->name, found->name);
2118 * Ignore servers which are busier than the one
2121 if (home->currently_outstanding > found->currently_outstanding) {
2122 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2123 home->name, found->name);
2128 * From the list of servers which have the same
2129 * load, choose one at random.
2131 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2134 } /* loop over the home servers */
2137 * We have no live servers, BUT we have a zombie. Use
2138 * the zombie as a last resort.
2140 if (!found && zombie) {
2146 * There's a fallback if they're all dead.
2148 if (!found && pool->fallback) {
2149 found = pool->fallback;
2155 * Allocate the proxy packet, only if it wasn't
2156 * already allocated by a module. This check is
2157 * mainly to support the proxying of EAP-TTLS and
2158 * EAP-PEAP tunneled requests.
2160 * In those cases, the EAP module creates a
2161 * "fake" request, and recursively passes it
2162 * through the authentication stage of the
2163 * server. The module then checks if the request
2164 * was supposed to be proxied, and if so, creates
2165 * a proxy packet from the TUNNELED request, and
2166 * not from the EAP request outside of the
2169 * The proxy then works like normal, except that
2170 * the response packet is "eaten" by the EAP
2171 * module, and encapsulated into an EAP packet.
2173 if (!request->proxy) {
2174 if ((request->proxy = rad_alloc(TRUE)) == NULL) {
2175 radlog(L_ERR|L_CONS, "no memory");
2180 * Copy the request, then look up name
2181 * and plain-text password in the copy.
2183 * Note that the User-Name attribute is
2184 * the *original* as sent over by the
2185 * client. The Stripped-User-Name
2186 * attribute is the one hacked through
2189 request->proxy->vps = paircopy(request->packet->vps);
2192 * Set the source IP address for proxying.
2194 request->proxy->src_ipaddr = found->src_ipaddr;
2198 * Update the various fields as appropriate.
2200 request->proxy->src_ipaddr = found->src_ipaddr;
2201 request->proxy->src_port = 0;
2202 request->proxy->dst_ipaddr = found->ipaddr;
2203 request->proxy->dst_port = found->port;
2204 request->home_server = found;
2207 * We're supposed to add a Message-Authenticator
2208 * if it doesn't exist, and it doesn't exist.
2210 if (found->message_authenticator &&
2211 (request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2212 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR)) {
2213 radius_pairmake(request, &request->proxy->vps,
2214 "Message-Authenticator", "0x00",
2222 * No live match found, and no fallback to the "DEFAULT"
2223 * realm. We fix this by blindly marking all servers as
2224 * "live". But only do it for ones that don't support
2225 * "pings", as they will be marked live when they
2226 * actually are live.
2228 if (!realm_config->fallback &&
2229 realm_config->wake_all_if_all_dead) {
2230 for (count = 0; count < pool->num_home_servers; count++) {
2231 home_server *home = pool->servers[count];
2233 if (!home) continue;
2235 if ((home->state == HOME_STATE_IS_DEAD) &&
2236 (home->ping_check == HOME_PING_CHECK_NONE)) {
2237 home->state = HOME_STATE_ALIVE;
2238 if (!found) found = home;
2242 if (found) goto update_and_return;
2246 * Still nothing. Look up the DEFAULT realm, but only
2247 * if we weren't looking up the NULL or DEFAULT realms.
2249 if (realm_config->fallback &&
2251 (strcmp(realmname, "NULL") != 0) &&
2252 (strcmp(realmname, "DEFAULT") != 0)) {
2253 REALM *rd = realm_find("DEFAULT");
2255 if (!rd) return NULL;
2258 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2259 pool = rd->auth_pool;
2261 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2262 pool = rd->acct_pool;
2264 if (!pool) return NULL;
2266 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2267 return home_server_ldb(rd->name, pool, request);
2271 * Still haven't found anything. Oh well.
2277 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port, int proto)
2281 memset(&myhome, 0, sizeof(myhome));
2282 myhome.ipaddr = *ipaddr;
2285 myhome.proto = proto;
2287 myhome.proto = IPPROTO_UDP;
2289 myhome.server = NULL; /* we're not called for internal proxying */
2291 return rbtree_finddata(home_servers_byaddr, &myhome);
2295 home_server *home_server_byname(const char *name, int type)
2299 memset(&myhome, 0, sizeof(myhome));
2303 return rbtree_finddata(home_servers_byname, &myhome);
2308 home_server *home_server_bynumber(int number)
2312 memset(&myhome, 0, sizeof(myhome));
2313 myhome.number = number;
2314 myhome.server = NULL; /* we're not called for internal proxying */
2316 return rbtree_finddata(home_servers_bynumber, &myhome);
2320 home_pool_t *home_pool_byname(const char *name, int type)
2324 memset(&mypool, 0, sizeof(mypool));
2326 mypool.server_type = type;
2327 return rbtree_finddata(home_pools_byname, &mypool);