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;
687 if (home->response_window > mainconfig.max_request_time) home->response_window = mainconfig.max_request_time;
689 if (home->zombie_period < 1) home->zombie_period = 1;
690 if (home->zombie_period > 120) home->zombie_period = 120;
692 if (home->zombie_period < home->response_window) {
693 home->zombie_period = home->response_window;
696 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
697 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
699 if (home->ping_timeout < 3) home->ping_timeout = 3;
700 if (home->ping_timeout > 10) home->ping_timeout = 10;
702 if (home->revive_interval < 60) home->revive_interval = 60;
703 if (home->revive_interval > 3600) home->revive_interval = 3600;
706 if (home->coa_irt < 1) home->coa_irt = 1;
707 if (home->coa_irt > 5) home->coa_irt = 5;
709 if (home->coa_mrc < 0) home->coa_mrc = 0;
710 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
712 if (home->coa_mrt < 0) home->coa_mrt = 0;
713 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
715 if (home->coa_mrd < 5) home->coa_mrd = 5;
716 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
719 if (home->max_connections > 1024) home->max_connections = 1024;
723 * UDP sockets can't be connection limited.
725 if (home->proto != IPPROTO_TCP) home->max_connections = 0;
728 if ((home->idle_timeout > 0) && (home->idle_timeout < 5))
729 home->idle_timeout = 5;
730 if ((home->lifetime > 0) && (home->lifetime < 5))
732 if ((home->lifetime > 0) && (home->idle_timeout > home->lifetime))
733 home->idle_timeout = 0;
736 home_server *home2 = rad_malloc(sizeof(*home2));
738 memcpy(home2, home, sizeof(*home2));
740 home2->type = HOME_TYPE_ACCT;
742 home2->ping_user_password = NULL;
745 if (home->no_response_fail == 2) home->no_response_fail = 0;
746 if (home2->no_response_fail == 2) home2->no_response_fail = 1;
748 if (!rbtree_insert(home_servers_byname, home2)) {
749 cf_log_err(cf_sectiontoitem(cs),
750 "Internal error %d adding home server %s.",
757 !rbtree_insert(home_servers_byaddr, home2)) {
758 rbtree_deletebydata(home_servers_byname, home2);
759 cf_log_err(cf_sectiontoitem(cs),
760 "Internal error %d adding home server %s.",
767 home2->number = home_server_max_number++;
768 if (!rbtree_insert(home_servers_bynumber, home2)) {
769 rbtree_deletebydata(home_servers_byname, home2);
770 if (!home2->server) {
771 rbtree_deletebydata(home_servers_byname, home2);
773 cf_log_err(cf_sectiontoitem(cs),
774 "Internal error %d adding home server %s.",
783 * Mark it as already processed
785 cf_data_add(cs, "home_server", null_free, null_free);
791 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
792 int server_type, int num_home_servers)
796 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
798 if (!pool) return NULL; /* just for pairanoia */
800 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
805 pool->server_type = server_type;
806 pool->num_home_servers = num_home_servers;
811 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
812 const char *name, int server_type,
815 home_server myhome, *home;
816 CONF_SECTION *server_cs;
819 cf_log_err(cf_pairtoitem(cp),
820 "No value given for home_server.");
825 myhome.type = server_type;
826 home = rbtree_finddata(home_servers_byname, &myhome);
832 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
834 cf_log_err(cf_pairtoitem(cp),
835 "Unknown home_server \"%s\".", name);
839 home = rbtree_finddata(home_servers_byname, &myhome);
841 cf_log_err(cf_pairtoitem(cp),
842 "Internal error %d adding home server \"%s\".",
852 static int server_pool_add(realm_config_t *rc,
853 CONF_SECTION *cs, int server_type, int do_print)
856 home_pool_t *pool = NULL;
859 int num_home_servers;
862 name2 = cf_section_name1(cs);
863 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
864 (strcasecmp(name2, "home_server_pool") != 0))) {
865 cf_log_err(cf_sectiontoitem(cs),
866 "Section is not a home_server_pool.");
870 name2 = cf_section_name2(cs);
872 cf_log_err(cf_sectiontoitem(cs),
873 "Server pool section is missing a name.");
878 * Count the home servers and initalize them.
880 num_home_servers = 0;
881 for (cp = cf_pair_find(cs, "home_server");
883 cp = cf_pair_find_next(cs, cp, "home_server")) {
886 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
887 server_type, &home)) {
892 if (num_home_servers == 0) {
893 cf_log_err(cf_sectiontoitem(cs),
894 "No home servers defined in pool %s",
899 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
905 * Fallback servers must be defined, and must be
908 cp = cf_pair_find(cs, "fallback");
911 if (server_type == HOME_TYPE_COA) {
912 cf_log_err(cf_sectiontoitem(cs), "Home server pools of type \"coa\" cannot have a fallback virtual server.");
917 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
918 server_type, &pool->fallback)) {
923 if (!pool->fallback->server) {
924 cf_log_err(cf_sectiontoitem(cs), "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
929 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
931 cp = cf_pair_find(cs, "type");
933 static FR_NAME_NUMBER pool_types[] = {
934 { "load-balance", HOME_POOL_LOAD_BALANCE },
935 { "fail-over", HOME_POOL_FAIL_OVER },
936 { "round_robin", HOME_POOL_LOAD_BALANCE },
937 { "fail_over", HOME_POOL_FAIL_OVER },
938 { "client-balance", HOME_POOL_CLIENT_BALANCE },
939 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
940 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
944 value = cf_pair_value(cp);
946 cf_log_err(cf_pairtoitem(cp),
947 "No value given for type.");
951 pool->type = fr_str2int(pool_types, value, 0);
953 cf_log_err(cf_pairtoitem(cp),
954 "Unknown type \"%s\".",
959 if (do_print) cf_log_info(cs, "\ttype = %s", value);
962 cp = cf_pair_find(cs, "virtual_server");
964 pool->virtual_server = cf_pair_value(cp);
965 if (!pool->virtual_server) {
966 cf_log_err(cf_pairtoitem(cp), "No value given for virtual_server");
971 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
974 if (!cf_section_sub_find_name2(rc->cs, "server",
975 pool->virtual_server)) {
976 cf_log_err(cf_pairtoitem(cp), "No such server %s",
977 pool->virtual_server);
983 num_home_servers = 0;
984 for (cp = cf_pair_find(cs, "home_server");
986 cp = cf_pair_find_next(cs, cp, "home_server")) {
989 value = cf_pair_value(cp);
991 memset(&myhome, 0, sizeof(&myhome));
993 myhome.type = server_type;
995 home = rbtree_finddata(home_servers_byname, &myhome);
997 DEBUG2("Internal sanity check failed");
1002 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
1006 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1007 pool->servers[num_home_servers++] = home;
1008 } /* loop over home_server's */
1010 if (pool->fallback && do_print) {
1011 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1014 if (!rbtree_insert(home_pools_byname, pool)) {
1015 rad_assert("Internal sanity check failed");
1019 if (do_print) cf_log_info(cs, " }");
1021 cf_data_add(cs, "home_server_pool", pool, free);
1023 rad_assert(pool->server_type != 0);
1028 if (do_print) cf_log_info(cs, " }");
1034 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1036 const char *name, const char *secret,
1037 home_pool_type_t ldflag, home_pool_t **pool_p,
1038 int type, const char *server)
1041 int i, insert_point, num_home_servers;
1042 home_server myhome, *home;
1043 home_pool_t mypool, *pool;
1044 CONF_SECTION *subcs;
1046 rc = rc; /* -Wunused */
1055 * LOCAL realms get sanity checked, and nothing else happens.
1057 if (strcmp(name, "LOCAL") == 0) {
1059 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
1066 return 0; /* Not proxying. Can't do non-LOCAL realms */
1069 mypool.name = realm;
1070 mypool.server_type = type;
1071 pool = rbtree_finddata(home_pools_byname, &mypool);
1073 if (pool->type != ldflag) {
1074 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
1078 if (pool->server_type != type) {
1079 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
1086 home = rbtree_finddata(home_servers_byname, &myhome);
1088 if (secret && (strcmp(home->secret, secret) != 0)) {
1089 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
1093 if (home->type != type) {
1094 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
1099 * See if the home server is already listed
1100 * in the pool. If so, do nothing else.
1102 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1103 if (pool->servers[i] == home) {
1110 * If we do have a pool, check that there is room to
1111 * insert the home server we've found, or the one that we
1114 * Note that we insert it into the LAST available
1115 * position, in order to maintain the same order as in
1116 * the configuration files.
1120 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1121 if (pool->servers[i]) break;
1123 if (!pool->servers[i]) {
1128 if (insert_point < 0) {
1129 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);
1135 * No home server, allocate one.
1141 home = rad_malloc(sizeof(*home));
1142 memset(home, 0, sizeof(*home));
1145 home->hostname = name;
1147 home->secret = secret;
1149 home->proto = IPPROTO_UDP;
1151 p = strchr(name, ':');
1153 if (type == HOME_TYPE_AUTH) {
1154 home->port = PW_AUTH_UDP_PORT;
1156 home->port = PW_ACCT_UDP_PORT;
1162 } else if (p == name) {
1163 cf_log_err(cf_sectiontoitem(cs),
1164 "Invalid hostname %s.",
1170 home->port = atoi(p + 1);
1171 if ((home->port == 0) || (home->port > 65535)) {
1172 cf_log_err(cf_sectiontoitem(cs),
1179 q = rad_malloc((p - name) + 1);
1180 memcpy(q, name, (p - name));
1186 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1187 cf_log_err(cf_sectiontoitem(cs),
1188 "Failed looking up hostname %s.",
1194 home->src_ipaddr.af = home->ipaddr.af;
1196 home->ipaddr.af = AF_UNSPEC;
1197 home->server = server;
1202 * Use the old-style configuration.
1204 home->max_outstanding = 65535*16;
1205 home->zombie_period = rc->retry_delay * rc->retry_count;
1206 if (home->zombie_period == 0) home->zombie_period =30;
1207 home->response_window = home->zombie_period - 1;
1209 home->ping_check = HOME_PING_CHECK_NONE;
1211 home->revive_interval = rc->dead_time;
1213 if (rbtree_finddata(home_servers_byaddr, home)) {
1214 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
1219 if (!rbtree_insert(home_servers_byname, home)) {
1220 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1225 if (!rbtree_insert(home_servers_byaddr, home)) {
1226 rbtree_deletebydata(home_servers_byname, home);
1227 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1233 home->number = home_server_max_number++;
1234 if (!rbtree_insert(home_servers_bynumber, home)) {
1235 rbtree_deletebydata(home_servers_byname, home);
1236 if (home->ipaddr.af != AF_UNSPEC) {
1237 rbtree_deletebydata(home_servers_byname, home);
1239 cf_log_err(cf_sectiontoitem(cs),
1240 "Internal error %d adding home server %s.",
1249 * We now have a home server, see if we can insert it
1250 * into pre-existing pool.
1252 if (insert_point >= 0) {
1253 rad_assert(pool != NULL);
1254 pool->servers[insert_point] = home;
1258 rad_assert(pool == NULL);
1259 rad_assert(home != NULL);
1262 * Count the old-style realms of this name.
1264 num_home_servers = 0;
1265 for (subcs = cf_section_find_next(cs, NULL, "realm");
1267 subcs = cf_section_find_next(cs, subcs, "realm")) {
1268 const char *this = cf_section_name2(subcs);
1270 if (!this || (strcmp(this, realm) != 0)) continue;
1274 if (num_home_servers == 0) {
1275 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
1280 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1283 pool->servers[0] = home;
1285 if (!rbtree_insert(home_pools_byname, pool)) {
1286 rad_assert("Internal sanity check failed");
1296 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1299 const char *secret = NULL;
1300 home_pool_type_t ldflag;
1303 cp = cf_pair_find(cs, "ldflag");
1304 ldflag = HOME_POOL_FAIL_OVER;
1306 host = cf_pair_value(cp);
1308 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
1312 if (strcasecmp(host, "fail_over") == 0) {
1313 cf_log_info(cs, "\tldflag = fail_over");
1315 } else if (strcasecmp(host, "round_robin") == 0) {
1316 ldflag = HOME_POOL_LOAD_BALANCE;
1317 cf_log_info(cs, "\tldflag = round_robin");
1320 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
1323 } /* else don't print it. */
1326 * Allow old-style if it doesn't exist, or if it exists and
1329 cp = cf_pair_find(cs, "authhost");
1331 host = cf_pair_value(cp);
1333 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
1337 if (strcmp(host, "LOCAL") != 0) {
1338 cp = cf_pair_find(cs, "secret");
1340 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1344 secret = cf_pair_value(cp);
1346 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1351 cf_log_info(cs, "\tauthhost = %s", host);
1353 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1354 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1359 cp = cf_pair_find(cs, "accthost");
1361 host = cf_pair_value(cp);
1363 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1368 * Don't look for a secret again if it was found
1371 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1372 cp = cf_pair_find(cs, "secret");
1374 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1378 secret = cf_pair_value(cp);
1380 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1385 cf_log_info(cs, "\taccthost = %s", host);
1387 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1388 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1393 cp = cf_pair_find(cs, "virtual_server");
1395 host = cf_pair_value(cp);
1397 cf_log_err(cf_pairtoitem(cp), "No value specified for virtual_server");
1401 cf_log_info(cs, "\tvirtual_server = %s", host);
1403 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1404 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1407 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1408 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1413 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1421 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1422 const char *name, home_pool_t **dest,
1423 int server_type, int do_print)
1425 home_pool_t mypool, *pool;
1428 mypool.server_type = server_type;
1430 pool = rbtree_finddata(home_pools_byname, &mypool);
1432 CONF_SECTION *pool_cs;
1434 pool_cs = cf_section_sub_find_name2(rc->cs,
1438 pool_cs = cf_section_sub_find_name2(rc->cs,
1443 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1447 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1451 pool = rbtree_finddata(home_pools_byname, &mypool);
1453 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1458 if (pool->server_type != server_type) {
1459 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1470 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1476 home_pool_t *auth_pool, *acct_pool;
1477 const char *auth_pool_name, *acct_pool_name;
1480 name2 = cf_section_name1(cs);
1481 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1482 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1486 name2 = cf_section_name2(cs);
1488 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1493 auth_pool = acct_pool = NULL;
1494 auth_pool_name = acct_pool_name = NULL;
1497 * Prefer new configuration to old one.
1499 cp = cf_pair_find(cs, "pool");
1500 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1501 if (cp) auth_pool_name = cf_pair_value(cp);
1502 if (cp && auth_pool_name) {
1503 acct_pool_name = auth_pool_name;
1504 if (!add_pool_to_realm(rc, cs,
1505 auth_pool_name, &auth_pool,
1506 HOME_TYPE_AUTH, 1)) {
1509 if (!add_pool_to_realm(rc, cs,
1510 auth_pool_name, &acct_pool,
1511 HOME_TYPE_ACCT, 0)) {
1516 cp = cf_pair_find(cs, "auth_pool");
1517 if (cp) auth_pool_name = cf_pair_value(cp);
1518 if (cp && auth_pool_name) {
1520 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1523 if (!add_pool_to_realm(rc, cs,
1524 auth_pool_name, &auth_pool,
1525 HOME_TYPE_AUTH, 1)) {
1530 cp = cf_pair_find(cs, "acct_pool");
1531 if (cp) acct_pool_name = cf_pair_value(cp);
1532 if (cp && acct_pool_name) {
1533 int do_print = TRUE;
1536 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1541 (strcmp(auth_pool_name, acct_pool_name) != 0)) {
1545 if (!add_pool_to_realm(rc, cs,
1546 acct_pool_name, &acct_pool,
1547 HOME_TYPE_ACCT, do_print)) {
1553 cf_log_info(cs, " realm %s {", name2);
1557 * The realm MAY already exist if it's an old-style realm.
1558 * In that case, merge the old-style realm with this one.
1560 r = realm_find2(name2);
1561 if (r && (strcmp(r->name, name2) == 0)) {
1562 if (cf_pair_find(cs, "auth_pool") ||
1563 cf_pair_find(cs, "acct_pool")) {
1564 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1568 if (!old_realm_config(rc, cs, r)) {
1572 cf_log_info(cs, " } # realm %s", name2);
1578 if (name2[0] == '~') {
1583 * Include substring matches.
1585 rcode = regcomp(®, name2 + 1,
1586 REG_EXTENDED | REG_NOSUB | REG_ICASE);
1590 regerror(rcode, ®, buffer, sizeof(buffer));
1592 cf_log_err(cf_sectiontoitem(cs),
1593 "Invalid regex \"%s\": %s",
1601 r = rad_malloc(sizeof(*r));
1602 memset(r, 0, sizeof(*r));
1607 r->auth_pool = auth_pool;
1608 r->acct_pool = acct_pool;
1610 if (auth_pool_name &&
1611 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1612 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1614 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1615 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1619 cp = cf_pair_find(cs, "nostrip");
1620 if (cp && (cf_pair_value(cp) == NULL)) {
1622 cf_log_info(cs, "\tnostrip");
1626 * We're a new-style realm. Complain if we see the old
1629 if (r->auth_pool || r->acct_pool) {
1630 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1631 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1632 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1633 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1634 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1639 * The realm MAY be an old-style realm, as there
1640 * was no auth_pool or acct_pool. Double-check
1641 * it, just to be safe.
1643 } else if (!old_realm_config(rc, cs, r)) {
1649 * It's a regex. Add it to a separate list.
1651 if (name2[0] == '~') {
1652 realm_regex_t *rr, **last;
1654 rr = rad_malloc(sizeof(*rr));
1656 last = &realms_regex;
1657 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1665 cf_log_info(cs, " }");
1670 if (!rbtree_insert(realms_byname, r)) {
1671 rad_assert("Internal sanity check failed");
1675 cf_log_info(cs, " }");
1680 cf_log_info(cs, " } # realm %s", name2);
1686 static const FR_NAME_NUMBER home_server_types[] = {
1687 { "auth", HOME_TYPE_AUTH },
1688 { "auth+acct", HOME_TYPE_AUTH },
1689 { "acct", HOME_TYPE_ACCT },
1690 { "coa", HOME_TYPE_COA },
1694 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1697 const char *name, *type;
1699 CONF_SECTION *server_cs;
1701 cp = cf_pair_find(cs, "home_server");
1703 cf_log_err(cf_sectiontoitem(cs), "Pool does not contain a \"home_server\" entry");
1704 return HOME_TYPE_INVALID;
1707 name = cf_pair_value(cp);
1709 cf_log_err(cf_pairtoitem(cp), "home_server entry does not reference a home server");
1710 return HOME_TYPE_INVALID;
1713 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1715 cf_log_err(cf_pairtoitem(cp), "home_server \"%s\" does not exist", name);
1716 return HOME_TYPE_INVALID;
1719 cp = cf_pair_find(server_cs, "type");
1721 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s does not contain a \"type\" entry", name);
1722 return HOME_TYPE_INVALID;
1725 type = cf_pair_value(cp);
1727 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an empty \"type\" entry", name);
1728 return HOME_TYPE_INVALID;
1731 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1732 if (home == HOME_TYPE_INVALID) {
1733 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1734 return HOME_TYPE_INVALID;
1737 return home; /* 'cause we miss it so much */
1741 int realms_init(CONF_SECTION *config)
1744 realm_config_t *rc, *old_rc;
1746 if (realms_byname) return 1;
1748 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1749 if (!realms_byname) {
1755 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
1756 if (!home_servers_byaddr) {
1761 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1762 if (!home_servers_byname) {
1768 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1769 if (!home_servers_bynumber) {
1775 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1776 if (!home_pools_byname) {
1782 rc = rad_malloc(sizeof(*rc));
1783 memset(rc, 0, sizeof(*rc));
1787 cs = cf_subsection_find_next(config, NULL, "proxy");
1789 cf_section_parse(cs, rc, proxy_config);
1791 rc->dead_time = DEAD_TIME;
1792 rc->retry_count = RETRY_COUNT;
1793 rc->retry_delay = RETRY_DELAY;
1795 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)) {
1809 for (cs = cf_subsection_find_next(config, NULL, "realm");
1811 cs = cf_subsection_find_next(config, cs, "realm")) {
1812 if (!realm_add(rc, cs)) {
1821 * CoA pools aren't tied to realms.
1823 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1825 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1829 * Pool was already loaded.
1831 if (cf_data_find(cs, "home_server_pool")) continue;
1833 type = pool_peek_type(config, cs);
1834 if (type == HOME_TYPE_INVALID) return 0;
1836 if (!server_pool_add(rc, cs, type, TRUE)) {
1844 xlat_register("home_server", xlat_home_server, NULL);
1845 xlat_register("home_server_pool", xlat_server_pool, NULL);
1849 * Swap pointers atomically.
1851 old_rc = realm_config;
1859 * Find a realm where "name" might be the regex.
1861 REALM *realm_find2(const char *name)
1866 if (!name) name = "NULL";
1868 myrealm.name = name;
1869 realm = rbtree_finddata(realms_byname, &myrealm);
1870 if (realm) return realm;
1874 realm_regex_t *this;
1876 for (this = realms_regex; this != NULL; this = this->next) {
1877 if (strcmp(this->realm->name, name) == 0) {
1885 * Couldn't find a realm. Look for DEFAULT.
1887 myrealm.name = "DEFAULT";
1888 return rbtree_finddata(realms_byname, &myrealm);
1893 * Find a realm in the REALM list.
1895 REALM *realm_find(const char *name)
1900 if (!name) name = "NULL";
1902 myrealm.name = name;
1903 realm = rbtree_finddata(realms_byname, &myrealm);
1904 if (realm) return realm;
1908 realm_regex_t *this;
1910 for (this = realms_regex; this != NULL; this = this->next) {
1915 * Include substring matches.
1917 if (regcomp(®, this->realm->name + 1,
1918 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
1922 compare = regexec(®, name, 0, NULL, 0);
1925 if (compare == 0) return this->realm;
1931 * Couldn't find a realm. Look for DEFAULT.
1933 myrealm.name = "DEFAULT";
1934 return rbtree_finddata(realms_byname, &myrealm);
1939 home_server *home_server_ldb(const char *realmname,
1940 home_pool_t *pool, REQUEST *request)
1944 home_server *found = NULL;
1945 home_server *zombie = NULL;
1949 * Determine how to pick choose the home server.
1951 switch (pool->type) {
1955 * For load-balancing by client IP address, we
1956 * pick a home server by hashing the client IP.
1958 * This isn't as even a load distribution as
1959 * tracking the State attribute, but it's better
1962 case HOME_POOL_CLIENT_BALANCE:
1963 switch (request->packet->src_ipaddr.af) {
1965 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1966 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1969 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1970 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1976 start = hash % pool->num_home_servers;
1979 case HOME_POOL_CLIENT_PORT_BALANCE:
1980 switch (request->packet->src_ipaddr.af) {
1982 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1983 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1986 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1987 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1993 fr_hash_update(&request->packet->src_port,
1994 sizeof(request->packet->src_port), hash);
1995 start = hash % pool->num_home_servers;
1998 case HOME_POOL_KEYED_BALANCE:
1999 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY, 0)) != NULL) {
2000 hash = fr_hash(vp->vp_strvalue, vp->length);
2001 start = hash % pool->num_home_servers;
2006 case HOME_POOL_LOAD_BALANCE:
2007 case HOME_POOL_FAIL_OVER:
2011 default: /* this shouldn't happen... */
2018 * Starting with the home server we chose, loop through
2019 * all home servers. If the current one is dead, skip
2020 * it. If it is too busy, skip it.
2022 * Otherwise, use it.
2024 for (count = 0; count < pool->num_home_servers; count++) {
2025 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
2027 if (!home) continue;
2030 * Skip dead home servers.
2032 if (home->state == HOME_STATE_IS_DEAD) {
2037 * This home server is too busy. Choose another one.
2039 if (home->currently_outstanding >= home->max_outstanding) {
2045 * We read the packet from a detail file, AND it
2046 * came from this server. Don't re-proxy it
2049 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2050 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
2051 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2057 * It's zombie, so we remember the first zombie
2058 * we find, but we don't mark it as a "live"
2061 if (home->state == HOME_STATE_ZOMBIE) {
2062 if (!zombie) zombie = home;
2067 * We've found the first "live" one. Use that.
2069 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2075 * Otherwise we're doing some kind of load balancing.
2076 * If we haven't found one yet, pick this one.
2083 RDEBUG3("PROXY %s %d\t%s %d",
2084 found->name, found->currently_outstanding,
2085 home->name, home->currently_outstanding);
2088 * Prefer this server if it's less busy than the
2089 * one we had previously found.
2091 if (home->currently_outstanding < found->currently_outstanding) {
2092 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2093 home->name, found->name);
2099 * Ignore servers which are busier than the one
2102 if (home->currently_outstanding > found->currently_outstanding) {
2103 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2104 home->name, found->name);
2109 * From the list of servers which have the same
2110 * load, choose one at random.
2112 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2115 } /* loop over the home servers */
2118 * We have no live servers, BUT we have a zombie. Use
2119 * the zombie as a last resort.
2121 if (!found && zombie) {
2127 * There's a fallback if they're all dead.
2129 if (!found && pool->fallback) {
2130 found = pool->fallback;
2136 * Allocate the proxy packet, only if it wasn't
2137 * already allocated by a module. This check is
2138 * mainly to support the proxying of EAP-TTLS and
2139 * EAP-PEAP tunneled requests.
2141 * In those cases, the EAP module creates a
2142 * "fake" request, and recursively passes it
2143 * through the authentication stage of the
2144 * server. The module then checks if the request
2145 * was supposed to be proxied, and if so, creates
2146 * a proxy packet from the TUNNELED request, and
2147 * not from the EAP request outside of the
2150 * The proxy then works like normal, except that
2151 * the response packet is "eaten" by the EAP
2152 * module, and encapsulated into an EAP packet.
2154 if (!request->proxy) {
2155 if ((request->proxy = rad_alloc(TRUE)) == NULL) {
2156 radlog(L_ERR|L_CONS, "no memory");
2161 * Copy the request, then look up name
2162 * and plain-text password in the copy.
2164 * Note that the User-Name attribute is
2165 * the *original* as sent over by the
2166 * client. The Stripped-User-Name
2167 * attribute is the one hacked through
2170 request->proxy->vps = paircopy(request->packet->vps);
2174 * Update the various fields as appropriate.
2176 request->proxy->src_ipaddr = found->src_ipaddr;
2177 request->proxy->src_port = 0;
2178 request->proxy->dst_ipaddr = found->ipaddr;
2179 request->proxy->dst_port = found->port;
2180 request->home_server = found;
2183 * We're supposed to add a Message-Authenticator
2184 * if it doesn't exist, and it doesn't exist.
2186 if (found->message_authenticator &&
2187 (request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2188 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0)) {
2189 radius_pairmake(request, &request->proxy->vps,
2190 "Message-Authenticator", "0x00",
2198 * No live match found, and no fallback to the "DEFAULT"
2199 * realm. We fix this by blindly marking all servers as
2200 * "live". But only do it for ones that don't support
2201 * "pings", as they will be marked live when they
2202 * actually are live.
2204 if (!realm_config->fallback &&
2205 realm_config->wake_all_if_all_dead) {
2206 for (count = 0; count < pool->num_home_servers; count++) {
2207 home_server *home = pool->servers[count];
2209 if (!home) continue;
2211 if ((home->state == HOME_STATE_IS_DEAD) &&
2212 (home->ping_check == HOME_PING_CHECK_NONE)) {
2213 home->state = HOME_STATE_ALIVE;
2214 if (!found) found = home;
2218 if (found) goto update_and_return;
2222 * Still nothing. Look up the DEFAULT realm, but only
2223 * if we weren't looking up the NULL or DEFAULT realms.
2225 if (realm_config->fallback &&
2227 (strcmp(realmname, "NULL") != 0) &&
2228 (strcmp(realmname, "DEFAULT") != 0)) {
2229 REALM *rd = realm_find("DEFAULT");
2231 if (!rd) return NULL;
2234 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2235 pool = rd->auth_pool;
2237 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2238 pool = rd->acct_pool;
2240 if (!pool) return NULL;
2242 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2243 return home_server_ldb(rd->name, pool, request);
2247 * Still haven't found anything. Oh well.
2253 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port, int proto)
2257 memset(&myhome, 0, sizeof(myhome));
2258 myhome.ipaddr = *ipaddr;
2261 myhome.proto = proto;
2263 myhome.proto = IPPROTO_UDP;
2265 myhome.server = NULL; /* we're not called for internal proxying */
2267 return rbtree_finddata(home_servers_byaddr, &myhome);
2271 home_server *home_server_byname(const char *name, int type)
2275 memset(&myhome, 0, sizeof(myhome));
2279 return rbtree_finddata(home_servers_byname, &myhome);
2284 home_server *home_server_bynumber(int number)
2288 memset(&myhome, 0, sizeof(myhome));
2289 myhome.number = number;
2290 myhome.server = NULL; /* we're not called for internal proxying */
2292 return rbtree_finddata(home_servers_bynumber, &myhome);
2296 home_pool_t *home_pool_byname(const char *name, int type)
2300 memset(&mypool, 0, sizeof(mypool));
2302 mypool.server_type = type;
2303 return rbtree_finddata(home_pools_byname, &mypool);