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)
409 free(hs_virtual_server); /* used only for printing during parsing */
410 hs_virtual_server = NULL;
412 name2 = cf_section_name2(cs);
414 cf_log_err(cf_sectiontoitem(cs),
415 "Home server section is missing a name.");
419 home = rad_malloc(sizeof(*home));
420 memset(home, 0, sizeof(*home));
426 * For zombie period calculations. We want to count
427 * zombies from the time when the server starts, instead
430 home->last_packet = time(NULL);
433 * Authentication servers have a default "no_response_fail = 0".
434 * Accounting servers have a default "no_response_fail = 1".
436 * This is because authentication packets are retried, so
437 * they can fail over to another home server. Accounting
438 * packets are not retried, so they cannot fail over, and
439 * instead should be rejected immediately.
441 home->no_response_fail = 2;
443 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
444 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
445 if (cf_section_parse(cs, home, home_server_config) < 0) {
450 * Figure out which one to use.
452 if (cf_pair_find(cs, "ipaddr")) {
453 home->ipaddr.af = AF_INET;
454 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
456 } else if (cf_pair_find(cs, "ipv6addr")) {
457 home->ipaddr.af = AF_INET6;
458 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
460 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
461 home->ipaddr.af = AF_UNSPEC;
462 home->server = cf_pair_value(cp);
464 cf_log_err(cf_sectiontoitem(cs),
465 "Invalid value for virtual_server");
469 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
471 cf_log_err(cf_sectiontoitem(cs),
472 "No such server %s", home->server);
477 * When CoA is used, the user has to specify the type
478 * of the home server, even when they point to
481 home->secret = strdup("");
485 cf_log_err(cf_sectiontoitem(cs),
486 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
503 if (!home->port || (home->port > 65535)) {
504 cf_log_err(cf_sectiontoitem(cs),
505 "No port, or invalid port defined for home server %s.",
511 cf_log_err(cf_sectiontoitem(cs),
512 "Fatal error! Home server %s is ourselves!",
518 cf_log_err(cf_sectiontoitem(cs),
519 "No shared secret defined for home server %s.",
525 * Use a reasonable default.
528 if (!hs_type) hs_type = strdup("auth+acct");
530 if (strcasecmp(hs_type, "auth") == 0) {
531 home->type = HOME_TYPE_AUTH;
532 if (home->no_response_fail == 2) home->no_response_fail = 0;
534 } else if (strcasecmp(hs_type, "acct") == 0) {
535 home->type = HOME_TYPE_ACCT;
536 if (home->no_response_fail == 2) home->no_response_fail = 1;
538 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
539 home->type = HOME_TYPE_AUTH;
543 } else if (strcasecmp(hs_type, "coa") == 0) {
544 home->type = HOME_TYPE_COA;
547 if (home->server != NULL) {
548 cf_log_err(cf_sectiontoitem(cs),
549 "Home servers of type \"coa\" cannot point to a virtual server");
555 cf_log_err(cf_sectiontoitem(cs),
556 "Invalid type \"%s\" for home server %s.",
563 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
564 home->ping_check = HOME_PING_CHECK_NONE;
566 } else if (strcasecmp(hs_check, "status-server") == 0) {
567 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
569 } else if (strcasecmp(hs_check, "request") == 0) {
570 home->ping_check = HOME_PING_CHECK_REQUEST;
573 cf_log_err(cf_sectiontoitem(cs),
574 "Invalid ping_check \"%s\" for home server %s.",
581 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
582 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
583 if (!home->ping_user_name) {
584 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable ping checks");
588 if ((home->type == HOME_TYPE_AUTH) &&
589 !home->ping_user_password) {
590 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable ping checks");
595 home->proto = IPPROTO_UDP;
598 if (strcmp(hs_proto, "udp") == 0) {
602 } else if (strcmp(hs_proto, "tcp") == 0) {
605 home->proto = IPPROTO_TCP;
608 cf_log_err(cf_sectiontoitem(cs),
609 "Unknown proto \"%s\".", hs_proto);
616 rbtree_finddata(home_servers_byaddr, home)) {
617 cf_log_err(cf_sectiontoitem(cs), "Duplicate home server");
622 * If the home is a virtual server, don't look up source IP.
625 rad_assert(home->ipaddr.af != AF_UNSPEC);
628 * Otherwise look up the source IP using the same
629 * address family as the destination IP.
632 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
633 cf_log_err(cf_sectiontoitem(cs), "Failed parsing src_ipaddr");
639 * Source isn't specified: Source is
640 * the correct address family, but all zeros.
642 home->src_ipaddr.af = home->ipaddr.af;
649 if (!rbtree_insert(home_servers_byname, home)) {
650 cf_log_err(cf_sectiontoitem(cs),
651 "Internal error %d adding home server %s.",
657 !rbtree_insert(home_servers_byaddr, home)) {
658 rbtree_deletebydata(home_servers_byname, home);
659 cf_log_err(cf_sectiontoitem(cs),
660 "Internal error %d adding home server %s.",
666 home->number = home_server_max_number++;
667 if (!rbtree_insert(home_servers_bynumber, home)) {
668 rbtree_deletebydata(home_servers_byname, home);
669 if (home->ipaddr.af != AF_UNSPEC) {
670 rbtree_deletebydata(home_servers_byname, home);
672 cf_log_err(cf_sectiontoitem(cs),
673 "Internal error %d adding home server %s.",
679 if (home->max_outstanding < 8) home->max_outstanding = 8;
680 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
682 if (home->ping_interval < 6) home->ping_interval = 6;
683 if (home->ping_interval > 120) home->ping_interval = 120;
685 if (home->response_window < 1) home->response_window = 1;
686 if (home->response_window > 60) home->response_window = 60;
688 if (home->zombie_period < 1) home->zombie_period = 1;
689 if (home->zombie_period > 120) home->zombie_period = 120;
691 if (home->zombie_period < home->response_window) {
692 home->zombie_period = home->response_window;
695 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
696 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
698 if (home->ping_timeout < 3) home->ping_timeout = 3;
699 if (home->ping_timeout > 10) home->ping_timeout = 10;
701 if (home->revive_interval < 60) home->revive_interval = 60;
702 if (home->revive_interval > 3600) home->revive_interval = 3600;
705 if (home->coa_irt < 1) home->coa_irt = 1;
706 if (home->coa_irt > 5) home->coa_irt = 5;
708 if (home->coa_mrc < 0) home->coa_mrc = 0;
709 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
711 if (home->coa_mrt < 0) home->coa_mrt = 0;
712 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
714 if (home->coa_mrd < 5) home->coa_mrd = 5;
715 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
718 if (home->max_connections > 1024) home->max_connections = 1024;
722 * UDP sockets can't be connection limited.
724 if (home->proto != IPPROTO_TCP) home->max_connections = 0;
727 if ((home->idle_timeout > 0) && (home->idle_timeout < 5))
728 home->idle_timeout = 5;
729 if ((home->lifetime > 0) && (home->lifetime < 5))
731 if ((home->lifetime > 0) && (home->idle_timeout > home->lifetime))
732 home->idle_timeout = 0;
735 home_server *home2 = rad_malloc(sizeof(*home2));
737 memcpy(home2, home, sizeof(*home2));
739 home2->type = HOME_TYPE_ACCT;
741 home2->ping_user_password = NULL;
744 if (home->no_response_fail == 2) home->no_response_fail = 0;
745 if (home2->no_response_fail == 2) home2->no_response_fail = 1;
747 if (!rbtree_insert(home_servers_byname, home2)) {
748 cf_log_err(cf_sectiontoitem(cs),
749 "Internal error %d adding home server %s.",
756 !rbtree_insert(home_servers_byaddr, home2)) {
757 rbtree_deletebydata(home_servers_byname, home2);
758 cf_log_err(cf_sectiontoitem(cs),
759 "Internal error %d adding home server %s.",
766 home2->number = home_server_max_number++;
767 if (!rbtree_insert(home_servers_bynumber, home2)) {
768 rbtree_deletebydata(home_servers_byname, home2);
769 if (!home2->server) {
770 rbtree_deletebydata(home_servers_byname, home2);
772 cf_log_err(cf_sectiontoitem(cs),
773 "Internal error %d adding home server %s.",
782 * Mark it as already processed
784 cf_data_add(cs, "home_server", null_free, null_free);
790 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
791 int server_type, int num_home_servers)
795 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
797 if (!pool) return NULL; /* just for pairanoia */
799 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
804 pool->server_type = server_type;
805 pool->num_home_servers = num_home_servers;
810 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
811 const char *name, int server_type,
814 home_server myhome, *home;
815 CONF_SECTION *server_cs;
818 cf_log_err(cf_pairtoitem(cp),
819 "No value given for home_server.");
824 myhome.type = server_type;
825 home = rbtree_finddata(home_servers_byname, &myhome);
831 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
833 cf_log_err(cf_pairtoitem(cp),
834 "Unknown home_server \"%s\".", name);
838 home = rbtree_finddata(home_servers_byname, &myhome);
840 cf_log_err(cf_pairtoitem(cp),
841 "Internal error %d adding home server \"%s\".",
851 static int server_pool_add(realm_config_t *rc,
852 CONF_SECTION *cs, int server_type, int do_print)
855 home_pool_t *pool = NULL;
858 int num_home_servers;
861 name2 = cf_section_name1(cs);
862 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
863 (strcasecmp(name2, "home_server_pool") != 0))) {
864 cf_log_err(cf_sectiontoitem(cs),
865 "Section is not a home_server_pool.");
869 name2 = cf_section_name2(cs);
871 cf_log_err(cf_sectiontoitem(cs),
872 "Server pool section is missing a name.");
877 * Count the home servers and initalize them.
879 num_home_servers = 0;
880 for (cp = cf_pair_find(cs, "home_server");
882 cp = cf_pair_find_next(cs, cp, "home_server")) {
885 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
886 server_type, &home)) {
891 if (num_home_servers == 0) {
892 cf_log_err(cf_sectiontoitem(cs),
893 "No home servers defined in pool %s",
898 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
904 * Fallback servers must be defined, and must be
907 cp = cf_pair_find(cs, "fallback");
910 if (server_type == HOME_TYPE_COA) {
911 cf_log_err(cf_sectiontoitem(cs), "Home server pools of type \"coa\" cannot have a fallback virtual server.");
916 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
917 server_type, &pool->fallback)) {
922 if (!pool->fallback->server) {
923 cf_log_err(cf_sectiontoitem(cs), "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
928 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
930 cp = cf_pair_find(cs, "type");
932 static FR_NAME_NUMBER pool_types[] = {
933 { "load-balance", HOME_POOL_LOAD_BALANCE },
934 { "fail-over", HOME_POOL_FAIL_OVER },
935 { "round_robin", HOME_POOL_LOAD_BALANCE },
936 { "fail_over", HOME_POOL_FAIL_OVER },
937 { "client-balance", HOME_POOL_CLIENT_BALANCE },
938 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
939 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
943 value = cf_pair_value(cp);
945 cf_log_err(cf_pairtoitem(cp),
946 "No value given for type.");
950 pool->type = fr_str2int(pool_types, value, 0);
952 cf_log_err(cf_pairtoitem(cp),
953 "Unknown type \"%s\".",
958 if (do_print) cf_log_info(cs, "\ttype = %s", value);
961 cp = cf_pair_find(cs, "virtual_server");
963 pool->virtual_server = cf_pair_value(cp);
964 if (!pool->virtual_server) {
965 cf_log_err(cf_pairtoitem(cp), "No value given for virtual_server");
970 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
973 if (!cf_section_sub_find_name2(rc->cs, "server",
974 pool->virtual_server)) {
975 cf_log_err(cf_pairtoitem(cp), "No such server %s",
976 pool->virtual_server);
982 num_home_servers = 0;
983 for (cp = cf_pair_find(cs, "home_server");
985 cp = cf_pair_find_next(cs, cp, "home_server")) {
988 value = cf_pair_value(cp);
990 memset(&myhome, 0, sizeof(&myhome));
992 myhome.type = server_type;
994 home = rbtree_finddata(home_servers_byname, &myhome);
996 DEBUG2("Internal sanity check failed");
1001 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
1005 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1006 pool->servers[num_home_servers++] = home;
1007 } /* loop over home_server's */
1009 if (pool->fallback && do_print) {
1010 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1013 if (!rbtree_insert(home_pools_byname, pool)) {
1014 rad_assert("Internal sanity check failed");
1018 if (do_print) cf_log_info(cs, " }");
1020 cf_data_add(cs, "home_server_pool", pool, free);
1022 rad_assert(pool->server_type != 0);
1027 if (do_print) cf_log_info(cs, " }");
1033 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1035 const char *name, const char *secret,
1036 home_pool_type_t ldflag, home_pool_t **pool_p,
1037 int type, const char *server)
1040 int i, insert_point, num_home_servers;
1041 home_server myhome, *home;
1042 home_pool_t mypool, *pool;
1043 CONF_SECTION *subcs;
1045 rc = rc; /* -Wunused */
1054 * LOCAL realms get sanity checked, and nothing else happens.
1056 if (strcmp(name, "LOCAL") == 0) {
1058 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
1065 return 0; /* Not proxying. Can't do non-LOCAL realms */
1068 mypool.name = realm;
1069 mypool.server_type = type;
1070 pool = rbtree_finddata(home_pools_byname, &mypool);
1072 if (pool->type != ldflag) {
1073 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
1077 if (pool->server_type != type) {
1078 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
1085 home = rbtree_finddata(home_servers_byname, &myhome);
1087 if (secret && (strcmp(home->secret, secret) != 0)) {
1088 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
1092 if (home->type != type) {
1093 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
1098 * See if the home server is already listed
1099 * in the pool. If so, do nothing else.
1101 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1102 if (pool->servers[i] == home) {
1109 * If we do have a pool, check that there is room to
1110 * insert the home server we've found, or the one that we
1113 * Note that we insert it into the LAST available
1114 * position, in order to maintain the same order as in
1115 * the configuration files.
1119 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1120 if (pool->servers[i]) break;
1122 if (!pool->servers[i]) {
1127 if (insert_point < 0) {
1128 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);
1134 * No home server, allocate one.
1140 home = rad_malloc(sizeof(*home));
1141 memset(home, 0, sizeof(*home));
1144 home->hostname = name;
1146 home->secret = secret;
1148 home->proto = IPPROTO_UDP;
1150 p = strchr(name, ':');
1152 if (type == HOME_TYPE_AUTH) {
1153 home->port = PW_AUTH_UDP_PORT;
1155 home->port = PW_ACCT_UDP_PORT;
1161 } else if (p == name) {
1162 cf_log_err(cf_sectiontoitem(cs),
1163 "Invalid hostname %s.",
1169 home->port = atoi(p + 1);
1170 if ((home->port == 0) || (home->port > 65535)) {
1171 cf_log_err(cf_sectiontoitem(cs),
1178 q = rad_malloc((p - name) + 1);
1179 memcpy(q, name, (p - name));
1185 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1186 cf_log_err(cf_sectiontoitem(cs),
1187 "Failed looking up hostname %s.",
1193 home->src_ipaddr.af = home->ipaddr.af;
1195 home->ipaddr.af = AF_UNSPEC;
1196 home->server = server;
1201 * Use the old-style configuration.
1203 home->max_outstanding = 65535*16;
1204 home->zombie_period = rc->retry_delay * rc->retry_count;
1205 if (home->zombie_period == 0) home->zombie_period =30;
1206 home->response_window = home->zombie_period - 1;
1208 home->ping_check = HOME_PING_CHECK_NONE;
1210 home->revive_interval = rc->dead_time;
1212 if (rbtree_finddata(home_servers_byaddr, home)) {
1213 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
1218 if (!rbtree_insert(home_servers_byname, home)) {
1219 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1224 if (!rbtree_insert(home_servers_byaddr, home)) {
1225 rbtree_deletebydata(home_servers_byname, home);
1226 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1232 home->number = home_server_max_number++;
1233 if (!rbtree_insert(home_servers_bynumber, home)) {
1234 rbtree_deletebydata(home_servers_byname, home);
1235 if (home->ipaddr.af != AF_UNSPEC) {
1236 rbtree_deletebydata(home_servers_byname, home);
1238 cf_log_err(cf_sectiontoitem(cs),
1239 "Internal error %d adding home server %s.",
1248 * We now have a home server, see if we can insert it
1249 * into pre-existing pool.
1251 if (insert_point >= 0) {
1252 rad_assert(pool != NULL);
1253 pool->servers[insert_point] = home;
1257 rad_assert(pool == NULL);
1258 rad_assert(home != NULL);
1261 * Count the old-style realms of this name.
1263 num_home_servers = 0;
1264 for (subcs = cf_section_find_next(cs, NULL, "realm");
1266 subcs = cf_section_find_next(cs, subcs, "realm")) {
1267 const char *this = cf_section_name2(subcs);
1269 if (!this || (strcmp(this, realm) != 0)) continue;
1273 if (num_home_servers == 0) {
1274 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
1279 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1282 pool->servers[0] = home;
1284 if (!rbtree_insert(home_pools_byname, pool)) {
1285 rad_assert("Internal sanity check failed");
1295 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1298 const char *secret = NULL;
1299 home_pool_type_t ldflag;
1302 cp = cf_pair_find(cs, "ldflag");
1303 ldflag = HOME_POOL_FAIL_OVER;
1305 host = cf_pair_value(cp);
1307 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
1311 if (strcasecmp(host, "fail_over") == 0) {
1312 cf_log_info(cs, "\tldflag = fail_over");
1314 } else if (strcasecmp(host, "round_robin") == 0) {
1315 ldflag = HOME_POOL_LOAD_BALANCE;
1316 cf_log_info(cs, "\tldflag = round_robin");
1319 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
1322 } /* else don't print it. */
1325 * Allow old-style if it doesn't exist, or if it exists and
1328 cp = cf_pair_find(cs, "authhost");
1330 host = cf_pair_value(cp);
1332 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
1336 if (strcmp(host, "LOCAL") != 0) {
1337 cp = cf_pair_find(cs, "secret");
1339 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1343 secret = cf_pair_value(cp);
1345 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1350 cf_log_info(cs, "\tauthhost = %s", host);
1352 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1353 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1358 cp = cf_pair_find(cs, "accthost");
1360 host = cf_pair_value(cp);
1362 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1367 * Don't look for a secret again if it was found
1370 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1371 cp = cf_pair_find(cs, "secret");
1373 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1377 secret = cf_pair_value(cp);
1379 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1384 cf_log_info(cs, "\taccthost = %s", host);
1386 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1387 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1392 cp = cf_pair_find(cs, "virtual_server");
1394 host = cf_pair_value(cp);
1396 cf_log_err(cf_pairtoitem(cp), "No value specified for virtual_server");
1400 cf_log_info(cs, "\tvirtual_server = %s", host);
1402 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1403 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1406 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1407 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1412 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1420 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1421 const char *name, home_pool_t **dest,
1422 int server_type, int do_print)
1424 home_pool_t mypool, *pool;
1427 mypool.server_type = server_type;
1429 pool = rbtree_finddata(home_pools_byname, &mypool);
1431 CONF_SECTION *pool_cs;
1433 pool_cs = cf_section_sub_find_name2(rc->cs,
1437 pool_cs = cf_section_sub_find_name2(rc->cs,
1442 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1446 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1450 pool = rbtree_finddata(home_pools_byname, &mypool);
1452 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1457 if (pool->server_type != server_type) {
1458 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1469 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1475 home_pool_t *auth_pool, *acct_pool;
1476 const char *auth_pool_name, *acct_pool_name;
1479 name2 = cf_section_name1(cs);
1480 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1481 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1485 name2 = cf_section_name2(cs);
1487 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1492 auth_pool = acct_pool = NULL;
1493 auth_pool_name = acct_pool_name = NULL;
1496 * Prefer new configuration to old one.
1498 cp = cf_pair_find(cs, "pool");
1499 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1500 if (cp) auth_pool_name = cf_pair_value(cp);
1501 if (cp && auth_pool_name) {
1502 acct_pool_name = auth_pool_name;
1503 if (!add_pool_to_realm(rc, cs,
1504 auth_pool_name, &auth_pool,
1505 HOME_TYPE_AUTH, 1)) {
1508 if (!add_pool_to_realm(rc, cs,
1509 auth_pool_name, &acct_pool,
1510 HOME_TYPE_ACCT, 0)) {
1515 cp = cf_pair_find(cs, "auth_pool");
1516 if (cp) auth_pool_name = cf_pair_value(cp);
1517 if (cp && auth_pool_name) {
1519 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1522 if (!add_pool_to_realm(rc, cs,
1523 auth_pool_name, &auth_pool,
1524 HOME_TYPE_AUTH, 1)) {
1529 cp = cf_pair_find(cs, "acct_pool");
1530 if (cp) acct_pool_name = cf_pair_value(cp);
1531 if (cp && acct_pool_name) {
1532 int do_print = TRUE;
1535 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1540 (strcmp(auth_pool_name, acct_pool_name) != 0)) {
1544 if (!add_pool_to_realm(rc, cs,
1545 acct_pool_name, &acct_pool,
1546 HOME_TYPE_ACCT, do_print)) {
1552 cf_log_info(cs, " realm %s {", name2);
1556 * The realm MAY already exist if it's an old-style realm.
1557 * In that case, merge the old-style realm with this one.
1559 r = realm_find2(name2);
1560 if (r && (strcmp(r->name, name2) == 0)) {
1561 if (cf_pair_find(cs, "auth_pool") ||
1562 cf_pair_find(cs, "acct_pool")) {
1563 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1567 if (!old_realm_config(rc, cs, r)) {
1571 cf_log_info(cs, " } # realm %s", name2);
1577 if (name2[0] == '~') {
1582 * Include substring matches.
1584 rcode = regcomp(®, name2 + 1,
1585 REG_EXTENDED | REG_NOSUB | REG_ICASE);
1589 regerror(rcode, ®, buffer, sizeof(buffer));
1591 cf_log_err(cf_sectiontoitem(cs),
1592 "Invalid regex \"%s\": %s",
1600 r = rad_malloc(sizeof(*r));
1601 memset(r, 0, sizeof(*r));
1606 r->auth_pool = auth_pool;
1607 r->acct_pool = acct_pool;
1609 if (auth_pool_name &&
1610 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1611 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1613 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1614 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1618 cp = cf_pair_find(cs, "nostrip");
1619 if (cp && (cf_pair_value(cp) == NULL)) {
1621 cf_log_info(cs, "\tnostrip");
1625 * We're a new-style realm. Complain if we see the old
1628 if (r->auth_pool || r->acct_pool) {
1629 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1630 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1631 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1632 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1633 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1638 * The realm MAY be an old-style realm, as there
1639 * was no auth_pool or acct_pool. Double-check
1640 * it, just to be safe.
1642 } else if (!old_realm_config(rc, cs, r)) {
1648 * It's a regex. Add it to a separate list.
1650 if (name2[0] == '~') {
1651 realm_regex_t *rr, **last;
1653 rr = rad_malloc(sizeof(*rr));
1655 last = &realms_regex;
1656 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1664 cf_log_info(cs, " }");
1669 if (!rbtree_insert(realms_byname, r)) {
1670 rad_assert("Internal sanity check failed");
1674 cf_log_info(cs, " }");
1679 cf_log_info(cs, " } # realm %s", name2);
1685 static const FR_NAME_NUMBER home_server_types[] = {
1686 { "auth", HOME_TYPE_AUTH },
1687 { "auth+acct", HOME_TYPE_AUTH },
1688 { "acct", HOME_TYPE_ACCT },
1689 { "coa", HOME_TYPE_COA },
1693 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1696 const char *name, *type;
1698 CONF_SECTION *server_cs;
1700 cp = cf_pair_find(cs, "home_server");
1702 cf_log_err(cf_sectiontoitem(cs), "Pool does not contain a \"home_server\" entry");
1703 return HOME_TYPE_INVALID;
1706 name = cf_pair_value(cp);
1708 cf_log_err(cf_pairtoitem(cp), "home_server entry does not reference a home server");
1709 return HOME_TYPE_INVALID;
1712 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1714 cf_log_err(cf_pairtoitem(cp), "home_server \"%s\" does not exist", name);
1715 return HOME_TYPE_INVALID;
1718 cp = cf_pair_find(server_cs, "type");
1720 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s does not contain a \"type\" entry", name);
1721 return HOME_TYPE_INVALID;
1724 type = cf_pair_value(cp);
1726 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an empty \"type\" entry", name);
1727 return HOME_TYPE_INVALID;
1730 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1731 if (home == HOME_TYPE_INVALID) {
1732 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1733 return HOME_TYPE_INVALID;
1736 return home; /* 'cause we miss it so much */
1740 int realms_init(CONF_SECTION *config)
1743 realm_config_t *rc, *old_rc;
1745 if (realms_byname) return 1;
1747 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1748 if (!realms_byname) {
1754 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
1755 if (!home_servers_byaddr) {
1760 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1761 if (!home_servers_byname) {
1767 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1768 if (!home_servers_bynumber) {
1774 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1775 if (!home_pools_byname) {
1781 rc = rad_malloc(sizeof(*rc));
1782 memset(rc, 0, sizeof(*rc));
1786 cs = cf_subsection_find_next(config, NULL, "proxy");
1788 cf_section_parse(cs, rc, proxy_config);
1790 rc->dead_time = DEAD_TIME;
1791 rc->retry_count = RETRY_COUNT;
1792 rc->retry_delay = RETRY_DELAY;
1794 rc->wake_all_if_all_dead= 0;
1798 for (cs = cf_subsection_find_next(config, NULL, "home_server");
1800 cs = cf_subsection_find_next(config, cs, "home_server")) {
1801 if (!home_server_add(rc, cs)) {
1808 for (cs = cf_subsection_find_next(config, NULL, "realm");
1810 cs = cf_subsection_find_next(config, cs, "realm")) {
1811 if (!realm_add(rc, cs)) {
1820 * CoA pools aren't tied to realms.
1822 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1824 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1828 * Pool was already loaded.
1830 if (cf_data_find(cs, "home_server_pool")) continue;
1832 type = pool_peek_type(config, cs);
1833 if (type == HOME_TYPE_INVALID) return 0;
1835 if (!server_pool_add(rc, cs, type, TRUE)) {
1843 xlat_register("home_server", xlat_home_server, NULL);
1844 xlat_register("home_server_pool", xlat_server_pool, NULL);
1848 * Swap pointers atomically.
1850 old_rc = realm_config;
1858 * Find a realm where "name" might be the regex.
1860 REALM *realm_find2(const char *name)
1865 if (!name) name = "NULL";
1867 myrealm.name = name;
1868 realm = rbtree_finddata(realms_byname, &myrealm);
1869 if (realm) return realm;
1873 realm_regex_t *this;
1875 for (this = realms_regex; this != NULL; this = this->next) {
1876 if (strcmp(this->realm->name, name) == 0) {
1884 * Couldn't find a realm. Look for DEFAULT.
1886 myrealm.name = "DEFAULT";
1887 return rbtree_finddata(realms_byname, &myrealm);
1892 * Find a realm in the REALM list.
1894 REALM *realm_find(const char *name)
1899 if (!name) name = "NULL";
1901 myrealm.name = name;
1902 realm = rbtree_finddata(realms_byname, &myrealm);
1903 if (realm) return realm;
1907 realm_regex_t *this;
1909 for (this = realms_regex; this != NULL; this = this->next) {
1914 * Include substring matches.
1916 if (regcomp(®, this->realm->name + 1,
1917 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
1921 compare = regexec(®, name, 0, NULL, 0);
1924 if (compare == 0) return this->realm;
1930 * Couldn't find a realm. Look for DEFAULT.
1932 myrealm.name = "DEFAULT";
1933 return rbtree_finddata(realms_byname, &myrealm);
1938 home_server *home_server_ldb(const char *realmname,
1939 home_pool_t *pool, REQUEST *request)
1943 home_server *found = NULL;
1944 home_server *zombie = NULL;
1948 * Determine how to pick choose the home server.
1950 switch (pool->type) {
1954 * For load-balancing by client IP address, we
1955 * pick a home server by hashing the client IP.
1957 * This isn't as even a load distribution as
1958 * tracking the State attribute, but it's better
1961 case HOME_POOL_CLIENT_BALANCE:
1962 switch (request->packet->src_ipaddr.af) {
1964 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1965 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1968 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1969 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1975 start = hash % pool->num_home_servers;
1978 case HOME_POOL_CLIENT_PORT_BALANCE:
1979 switch (request->packet->src_ipaddr.af) {
1981 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1982 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1985 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1986 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1992 fr_hash_update(&request->packet->src_port,
1993 sizeof(request->packet->src_port), hash);
1994 start = hash % pool->num_home_servers;
1997 case HOME_POOL_KEYED_BALANCE:
1998 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY, 0)) != NULL) {
1999 hash = fr_hash(vp->vp_strvalue, vp->length);
2000 start = hash % pool->num_home_servers;
2005 case HOME_POOL_LOAD_BALANCE:
2006 case HOME_POOL_FAIL_OVER:
2010 default: /* this shouldn't happen... */
2017 * Starting with the home server we chose, loop through
2018 * all home servers. If the current one is dead, skip
2019 * it. If it is too busy, skip it.
2021 * Otherwise, use it.
2023 for (count = 0; count < pool->num_home_servers; count++) {
2024 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
2026 if (!home) continue;
2029 * Skip dead home servers.
2031 if (home->state == HOME_STATE_IS_DEAD) {
2036 * This home server is too busy. Choose another one.
2038 if (home->currently_outstanding >= home->max_outstanding) {
2044 * We read the packet from a detail file, AND it
2045 * came from this server. Don't re-proxy it
2048 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2049 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
2050 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2056 * It's zombie, so we remember the first zombie
2057 * we find, but we don't mark it as a "live"
2060 if (home->state == HOME_STATE_ZOMBIE) {
2061 if (!zombie) zombie = home;
2066 * We've found the first "live" one. Use that.
2068 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2074 * Otherwise we're doing some kind of load balancing.
2075 * If we haven't found one yet, pick this one.
2082 RDEBUG3("PROXY %s %d\t%s %d",
2083 found->name, found->currently_outstanding,
2084 home->name, home->currently_outstanding);
2087 * Prefer this server if it's less busy than the
2088 * one we had previously found.
2090 if (home->currently_outstanding < found->currently_outstanding) {
2091 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2092 home->name, found->name);
2098 * Ignore servers which are busier than the one
2101 if (home->currently_outstanding > found->currently_outstanding) {
2102 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2103 home->name, found->name);
2108 * From the list of servers which have the same
2109 * load, choose one at random.
2111 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2114 } /* loop over the home servers */
2117 * We have no live servers, BUT we have a zombie. Use
2118 * the zombie as a last resort.
2120 if (!found && zombie) {
2126 * There's a fallback if they're all dead.
2128 if (!found && pool->fallback) {
2129 found = pool->fallback;
2135 * Allocate the proxy packet, only if it wasn't
2136 * already allocated by a module. This check is
2137 * mainly to support the proxying of EAP-TTLS and
2138 * EAP-PEAP tunneled requests.
2140 * In those cases, the EAP module creates a
2141 * "fake" request, and recursively passes it
2142 * through the authentication stage of the
2143 * server. The module then checks if the request
2144 * was supposed to be proxied, and if so, creates
2145 * a proxy packet from the TUNNELED request, and
2146 * not from the EAP request outside of the
2149 * The proxy then works like normal, except that
2150 * the response packet is "eaten" by the EAP
2151 * module, and encapsulated into an EAP packet.
2153 if (!request->proxy) {
2154 if ((request->proxy = rad_alloc(TRUE)) == NULL) {
2155 radlog(L_ERR|L_CONS, "no memory");
2160 * Copy the request, then look up name
2161 * and plain-text password in the copy.
2163 * Note that the User-Name attribute is
2164 * the *original* as sent over by the
2165 * client. The Stripped-User-Name
2166 * attribute is the one hacked through
2169 request->proxy->vps = paircopy(request->packet->vps);
2173 * Update the various fields as appropriate.
2175 request->proxy->src_ipaddr = found->src_ipaddr;
2176 request->proxy->src_port = 0;
2177 request->proxy->dst_ipaddr = found->ipaddr;
2178 request->proxy->dst_port = found->port;
2179 request->home_server = found;
2182 * We're supposed to add a Message-Authenticator
2183 * if it doesn't exist, and it doesn't exist.
2185 if (found->message_authenticator &&
2186 (request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2187 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0)) {
2188 radius_pairmake(request, &request->proxy->vps,
2189 "Message-Authenticator", "0x00",
2197 * No live match found, and no fallback to the "DEFAULT"
2198 * realm. We fix this by blindly marking all servers as
2199 * "live". But only do it for ones that don't support
2200 * "pings", as they will be marked live when they
2201 * actually are live.
2203 if (!realm_config->fallback &&
2204 realm_config->wake_all_if_all_dead) {
2205 for (count = 0; count < pool->num_home_servers; count++) {
2206 home_server *home = pool->servers[count];
2208 if (!home) continue;
2210 if ((home->state == HOME_STATE_IS_DEAD) &&
2211 (home->ping_check == HOME_PING_CHECK_NONE)) {
2212 home->state = HOME_STATE_ALIVE;
2213 if (!found) found = home;
2217 if (found) goto update_and_return;
2221 * Still nothing. Look up the DEFAULT realm, but only
2222 * if we weren't looking up the NULL or DEFAULT realms.
2224 if (realm_config->fallback &&
2226 (strcmp(realmname, "NULL") != 0) &&
2227 (strcmp(realmname, "DEFAULT") != 0)) {
2228 REALM *rd = realm_find("DEFAULT");
2230 if (!rd) return NULL;
2233 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2234 pool = rd->auth_pool;
2236 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2237 pool = rd->acct_pool;
2239 if (!pool) return NULL;
2241 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2242 return home_server_ldb(rd->name, pool, request);
2246 * Still haven't found anything. Oh well.
2252 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port, int proto)
2256 memset(&myhome, 0, sizeof(myhome));
2257 myhome.ipaddr = *ipaddr;
2260 myhome.proto = proto;
2262 myhome.proto = IPPROTO_UDP;
2264 myhome.server = NULL; /* we're not called for internal proxying */
2266 return rbtree_finddata(home_servers_byaddr, &myhome);
2270 home_server *home_server_byname(const char *name, int type)
2274 memset(&myhome, 0, sizeof(myhome));
2278 return rbtree_finddata(home_servers_byname, &myhome);
2283 home_server *home_server_bynumber(int number)
2287 memset(&myhome, 0, sizeof(myhome));
2288 myhome.number = number;
2289 myhome.server = NULL; /* we're not called for internal proxying */
2291 return rbtree_finddata(home_servers_bynumber, &myhome);
2295 home_pool_t *home_pool_byname(const char *name, int type)
2299 memset(&mypool, 0, sizeof(mypool));
2301 mypool.server_type = type;
2302 return rbtree_finddata(home_pools_byname, &mypool);