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>
35 #ifdef HAVE_PCREPOSIX_H
36 #include <pcreposix.h>
42 * For POSIX Regular expressions.
43 * (0) Means no extended regular expressions.
44 * REG_EXTENDED means use extended regular expressions.
47 #define REG_EXTENDED (0)
60 static rbtree_t *realms_byname = NULL;
63 typedef struct realm_regex_t {
65 struct realm_regex_t *next;
68 static realm_regex_t *realms_regex = NULL;
70 #endif /* HAVE_REGEX_H */
72 typedef struct realm_config_t {
78 int wake_all_if_all_dead;
81 static realm_config_t *realm_config = NULL;
84 static rbtree_t *home_servers_byaddr = NULL;
85 static rbtree_t *home_servers_byname = NULL;
87 static int home_server_max_number = 0;
88 static rbtree_t *home_servers_bynumber = NULL;
91 static rbtree_t *home_pools_byname = NULL;
94 * Map the proxy server configuration parameters to variables.
96 static const CONF_PARSER proxy_config[] = {
97 { "retry_delay", PW_TYPE_INTEGER,
98 offsetof(realm_config_t, retry_delay),
99 NULL, Stringify(RETRY_DELAY) },
101 { "retry_count", PW_TYPE_INTEGER,
102 offsetof(realm_config_t, retry_count),
103 NULL, Stringify(RETRY_COUNT) },
105 { "default_fallback", PW_TYPE_BOOLEAN,
106 offsetof(realm_config_t, fallback),
109 { "dead_time", PW_TYPE_INTEGER,
110 offsetof(realm_config_t, dead_time),
111 NULL, Stringify(DEAD_TIME) },
113 { "wake_all_if_all_dead", PW_TYPE_BOOLEAN,
114 offsetof(realm_config_t, wake_all_if_all_dead),
117 { NULL, -1, 0, NULL, NULL }
121 static int realm_name_cmp(const void *one, const void *two)
123 const REALM *a = one;
124 const REALM *b = two;
126 return strcasecmp(a->name, b->name);
131 static void home_server_free(void *data)
133 home_server *home = data;
138 static int home_server_name_cmp(const void *one, const void *two)
140 const home_server *a = one;
141 const home_server *b = two;
143 if (a->type < b->type) return -1;
144 if (a->type > b->type) return +1;
146 return strcasecmp(a->name, b->name);
149 static int home_server_addr_cmp(const void *one, const void *two)
152 const home_server *a = one;
153 const home_server *b = two;
155 if (a->server && !b->server) return -1;
156 if (!a->server && b->server) return +1;
157 if (a->server && b->server) {
158 rcode = a->type - b->type;
159 if (rcode != 0) return rcode;
160 return strcmp(a->server, b->server);
163 if (a->port < b->port) return -1;
164 if (a->port > b->port) return +1;
167 if (a->proto < b->proto) return -1;
168 if (a->proto > b->proto) return +1;
171 rcode = fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
172 if (rcode != 0) return rcode;
174 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
178 static int home_server_number_cmp(const void *one, const void *two)
180 const home_server *a = one;
181 const home_server *b = two;
183 return (a->number - b->number);
187 static int home_pool_name_cmp(const void *one, const void *two)
189 const home_pool_t *a = one;
190 const home_pool_t *b = two;
192 if (a->server_type < b->server_type) return -1;
193 if (a->server_type > b->server_type) return +1;
195 return strcasecmp(a->name, b->name);
199 static size_t xlat_cs(CONF_SECTION *cs, const char *fmt, char *out, size_t outlen)
202 const char *value = NULL;
207 if (strcmp(fmt, "instance") == 0) {
208 value = cf_section_name2(cs);
216 cp = cf_pair_find(cs, fmt);
217 if (!cp || !(value = cf_pair_value(cp))) {
223 strlcpy(out, value, outlen);
230 * Xlat for %{home_server:foo}
232 static size_t xlat_home_server(UNUSED void *instance, REQUEST *request,
233 const char *fmt, char *out, size_t outlen)
235 if (!fmt || !out || (outlen < 1)) return 0;
237 if (!request || !request->home_server) {
242 return xlat_cs(request->home_server->cs, fmt, out, outlen);
247 * Xlat for %{home_server_pool:foo}
249 static size_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
250 const char *fmt, char *out, size_t outlen)
252 if (!fmt || !out || (outlen < 1)) return 0;
254 if (!request || !request->home_pool) {
259 return xlat_cs(request->home_pool->cs, fmt, out, outlen);
263 void realms_free(void)
267 rbtree_free(home_servers_bynumber);
268 home_servers_bynumber = NULL;
271 rbtree_free(home_servers_byname);
272 home_servers_byname = NULL;
274 rbtree_free(home_servers_byaddr);
275 home_servers_byaddr = NULL;
277 rbtree_free(home_pools_byname);
278 home_pools_byname = NULL;
281 rbtree_free(realms_byname);
282 realms_byname = NULL;
286 realm_regex_t *this, *next;
288 for (this = realms_regex; this != NULL; this = next) {
303 static CONF_PARSER limit_config[] = {
304 { "max_connections", PW_TYPE_INTEGER,
305 offsetof(home_server, limit.max_connections), NULL, "16" },
307 { "max_requests", PW_TYPE_INTEGER,
308 offsetof(home_server, limit.max_requests), NULL, "0" },
310 { "lifetime", PW_TYPE_INTEGER,
311 offsetof(home_server, limit.lifetime), NULL, "0" },
313 { "idle_timeout", PW_TYPE_INTEGER,
314 offsetof(home_server, limit.idle_timeout), NULL, "0" },
316 { NULL, -1, 0, NULL, NULL } /* end the list */
319 static struct in_addr hs_ip4addr;
320 static struct in6_addr hs_ip6addr;
321 static char *hs_srcipaddr = NULL;
322 static char *hs_type = NULL;
323 static char *hs_check = NULL;
324 static char *hs_virtual_server = NULL;
326 static char *hs_proto = NULL;
330 static CONF_PARSER home_server_coa[] = {
331 { "irt", PW_TYPE_INTEGER,
332 offsetof(home_server, coa_irt), 0, Stringify(2) },
333 { "mrt", PW_TYPE_INTEGER,
334 offsetof(home_server, coa_mrt), 0, Stringify(16) },
335 { "mrc", PW_TYPE_INTEGER,
336 offsetof(home_server, coa_mrc), 0, Stringify(5) },
337 { "mrd", PW_TYPE_INTEGER,
338 offsetof(home_server, coa_mrd), 0, Stringify(30) },
340 { NULL, -1, 0, NULL, NULL } /* end the list */
344 static CONF_PARSER home_server_config[] = {
345 { "ipaddr", PW_TYPE_IPADDR,
346 0, &hs_ip4addr, NULL },
347 { "ipv6addr", PW_TYPE_IPV6ADDR,
348 0, &hs_ip6addr, NULL },
349 { "virtual_server", PW_TYPE_STRING_PTR,
350 0, &hs_virtual_server, NULL },
352 { "port", PW_TYPE_INTEGER,
353 offsetof(home_server,port), NULL, "0" },
355 { "type", PW_TYPE_STRING_PTR,
359 { "proto", PW_TYPE_STRING_PTR,
360 0, &hs_proto, NULL },
363 { "secret", PW_TYPE_STRING_PTR,
364 offsetof(home_server,secret), NULL, NULL},
366 { "src_ipaddr", PW_TYPE_STRING_PTR,
367 0, &hs_srcipaddr, NULL },
369 { "response_window", PW_TYPE_INTEGER,
370 offsetof(home_server,response_window), NULL, "30" },
371 { "max_outstanding", PW_TYPE_INTEGER,
372 offsetof(home_server,max_outstanding), NULL, "65536" },
374 { "zombie_period", PW_TYPE_INTEGER,
375 offsetof(home_server,zombie_period), NULL, "40" },
376 { "status_check", PW_TYPE_STRING_PTR,
377 0, &hs_check, "none" },
378 { "ping_check", PW_TYPE_STRING_PTR,
379 0, &hs_check, NULL },
381 { "ping_interval", PW_TYPE_INTEGER,
382 offsetof(home_server,ping_interval), NULL, "30" },
383 { "check_interval", PW_TYPE_INTEGER,
384 offsetof(home_server,ping_interval), NULL, "30" },
385 { "num_answers_to_alive", PW_TYPE_INTEGER,
386 offsetof(home_server,num_pings_to_alive), NULL, "3" },
387 { "revive_interval", PW_TYPE_INTEGER,
388 offsetof(home_server,revive_interval), NULL, "300" },
389 { "status_check_timeout", PW_TYPE_INTEGER,
390 offsetof(home_server,ping_timeout), NULL, "4" },
392 { "username", PW_TYPE_STRING_PTR,
393 offsetof(home_server,ping_user_name), NULL, NULL},
394 { "password", PW_TYPE_STRING_PTR,
395 offsetof(home_server,ping_user_password), NULL, NULL},
398 { "historic_average_window", PW_TYPE_INTEGER,
399 offsetof(home_server,ema.window), NULL, NULL },
403 { "coa", PW_TYPE_SUBSECTION, 0, NULL, (const void *) home_server_coa },
406 { "limit", PW_TYPE_SUBSECTION, 0, NULL, (const void *) limit_config },
408 { NULL, -1, 0, NULL, NULL } /* end the list */
412 static void null_free(UNUSED void *data)
416 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs)
424 hs_virtual_server = NULL;
426 name2 = cf_section_name2(cs);
428 cf_log_err(cf_sectiontoitem(cs),
429 "Home server section is missing a name.");
433 home = rad_malloc(sizeof(*home));
434 memset(home, 0, sizeof(*home));
438 home->state = HOME_STATE_UNKNOWN;
441 * Last packet sent / received are zero.
444 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
445 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
446 if (cf_section_parse(cs, home, home_server_config) < 0) {
451 * Figure out which one to use.
453 if (cf_pair_find(cs, "ipaddr")) {
454 home->ipaddr.af = AF_INET;
455 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
457 } else if (cf_pair_find(cs, "ipv6addr")) {
458 home->ipaddr.af = AF_INET6;
459 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
461 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
462 home->ipaddr.af = AF_UNSPEC;
463 home->server = cf_pair_value(cp);
465 cf_log_err(cf_sectiontoitem(cs),
466 "Invalid value for virtual_server");
470 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
472 cf_log_err(cf_sectiontoitem(cs),
473 "No such server %s", home->server);
478 * When CoA is used, the user has to specify the type
479 * of the home server, even when they point to
482 home->secret = strdup("");
486 cf_log_err(cf_sectiontoitem(cs),
487 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
490 talloc_free(hs_type);
500 if (!home->port || (home->port > 65535)) {
501 cf_log_err(cf_sectiontoitem(cs),
502 "No port, or invalid port defined for home server %s.",
508 cf_log_err(cf_sectiontoitem(cs),
509 "Fatal error! Home server %s is ourselves!",
515 cf_log_err(cf_sectiontoitem(cs),
516 "No shared secret defined for home server %s.",
522 * Use a reasonable default.
525 if (!hs_type) hs_type = talloc_strdup(cs, "auth+acct");
527 if (strcasecmp(hs_type, "auth") == 0) {
528 home->type = HOME_TYPE_AUTH;
530 } else if (strcasecmp(hs_type, "acct") == 0) {
531 home->type = HOME_TYPE_ACCT;
533 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
534 home->type = HOME_TYPE_AUTH;
538 } else if (strcasecmp(hs_type, "coa") == 0) {
539 home->type = HOME_TYPE_COA;
542 if (home->server != NULL) {
543 cf_log_err(cf_sectiontoitem(cs),
544 "Home servers of type \"coa\" cannot point to a virtual server");
550 cf_log_err(cf_sectiontoitem(cs),
551 "Invalid type \"%s\" for home server %s.",
555 if (hs_type) talloc_free(hs_type);
558 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
559 home->ping_check = HOME_PING_CHECK_NONE;
561 } else if (strcasecmp(hs_check, "status-server") == 0) {
562 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
564 } else if (strcasecmp(hs_check, "request") == 0) {
565 home->ping_check = HOME_PING_CHECK_REQUEST;
567 if (!home->ping_user_name ||
568 !*home->ping_user_name) {
569 cf_log_err(cf_sectiontoitem(cs), "You must supply a 'username' to enable status_check=request");
573 if ((home->type == HOME_TYPE_AUTH) &&
574 (!home->ping_user_password ||
575 !*home->ping_user_password)) {
576 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable status_check=request");
581 cf_log_err(cf_sectiontoitem(cs),
582 "Invalid status__check \"%s\" for home server %s.",
588 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
589 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
590 if (!home->ping_user_name) {
591 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable status_check=request");
595 if ((home->type == HOME_TYPE_AUTH) &&
596 !home->ping_user_password) {
597 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable status_check=request");
602 home->proto = IPPROTO_UDP;
605 if (strcmp(hs_proto, "udp") == 0) {
608 } else if (strcmp(hs_proto, "tcp") == 0) {
610 home->proto = IPPROTO_TCP;
612 if (home->ping_check != HOME_PING_CHECK_NONE) {
613 cf_log_err(cf_sectiontoitem(cs),
614 "Only 'status_check = none' is allowed for home servers with 'proto = tcp'");
619 cf_log_err(cf_sectiontoitem(cs),
620 "Unknown proto \"%s\".", hs_proto);
627 rbtree_finddata(home_servers_byaddr, home)) {
628 cf_log_err(cf_sectiontoitem(cs), "Duplicate home server");
633 * Check the TLS configuration.
635 tls = cf_section_sub_find(cs, "tls");
638 * If the home is a virtual server, don't look up source IP.
641 rad_assert(home->ipaddr.af != AF_UNSPEC);
644 * Otherwise look up the source IP using the same
645 * address family as the destination IP.
648 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
649 cf_log_err(cf_sectiontoitem(cs), "Failed parsing src_ipaddr");
655 * Source isn't specified: Source is
656 * the correct address family, but all zeros.
658 memset(&home->src_ipaddr, 0, sizeof(home->src_ipaddr));
659 home->src_ipaddr.af = home->ipaddr.af;
662 if (tls && (home->proto != IPPROTO_TCP)) {
663 cf_log_err(cf_sectiontoitem(cs), "TLS transport is not available for UDP sockets.");
670 cf_log_err(cf_sectiontoitem(cs), "TLS transport is not available in this executable.");
675 * Parse the SSL client configuration.
678 home->tls = tls_client_conf_parse(tls);
686 cf_log_err(cf_sectiontoitem(cs), "Virtual home_servers cannot have a \"tls\" subsection");
691 * Make sure that this is set.
693 if (home->src_ipaddr.af == AF_UNSPEC) {
694 home->src_ipaddr.af = home->ipaddr.af;
699 if (rbtree_finddata(home_servers_byname, home) != NULL) {
700 cf_log_err(cf_sectiontoitem(cs),
701 "Duplicate home server name %s.", name2);
706 (rbtree_finddata(home_servers_byaddr, home) != NULL)) {
707 cf_log_err(cf_sectiontoitem(cs),
708 "Duplicate home server IP %s.", name2);
712 if (!rbtree_insert(home_servers_byname, home)) {
713 cf_log_err(cf_sectiontoitem(cs),
714 "Internal error %d adding home server %s.",
720 !rbtree_insert(home_servers_byaddr, home)) {
721 rbtree_deletebydata(home_servers_byname, home);
722 cf_log_err(cf_sectiontoitem(cs),
723 "Internal error %d adding home server %s.",
729 home->number = home_server_max_number++;
730 if (!rbtree_insert(home_servers_bynumber, home)) {
731 rbtree_deletebydata(home_servers_byname, home);
732 if (home->ipaddr.af != AF_UNSPEC) {
733 rbtree_deletebydata(home_servers_byname, home);
735 cf_log_err(cf_sectiontoitem(cs),
736 "Internal error %d adding home server %s.",
742 if (home->max_outstanding < 8) home->max_outstanding = 8;
743 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
745 if (home->ping_interval < 6) home->ping_interval = 6;
746 if (home->ping_interval > 120) home->ping_interval = 120;
748 if (home->response_window < 1) home->response_window = 1;
749 if (home->response_window > 60) home->response_window = 60;
750 if (home->response_window > mainconfig.max_request_time) home->response_window = mainconfig.max_request_time;
752 if (home->zombie_period < 1) home->zombie_period = 1;
753 if (home->zombie_period > 120) home->zombie_period = 120;
755 if (home->zombie_period < home->response_window) {
756 home->zombie_period = home->response_window;
759 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
760 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
762 if (home->ping_timeout < 3) home->ping_timeout = 3;
763 if (home->ping_timeout > 10) home->ping_timeout = 10;
765 if (home->revive_interval < 60) home->revive_interval = 60;
766 if (home->revive_interval > 3600) home->revive_interval = 3600;
769 if (home->coa_irt < 1) home->coa_irt = 1;
770 if (home->coa_irt > 5) home->coa_irt = 5;
772 if (home->coa_mrc < 0) home->coa_mrc = 0;
773 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
775 if (home->coa_mrt < 0) home->coa_mrt = 0;
776 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
778 if (home->coa_mrd < 5) home->coa_mrd = 5;
779 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
782 if (home->limit.max_connections > 1024) home->limit.max_connections = 1024;
786 * UDP sockets can't be connection limited.
788 if (home->proto != IPPROTO_TCP) home->limit.max_connections = 0;
791 if ((home->limit.idle_timeout > 0) && (home->limit.idle_timeout < 5))
792 home->limit.idle_timeout = 5;
793 if ((home->limit.lifetime > 0) && (home->limit.lifetime < 5))
794 home->limit.lifetime = 5;
795 if ((home->limit.lifetime > 0) && (home->limit.idle_timeout > home->limit.lifetime))
796 home->limit.idle_timeout = 0;
798 tls = cf_item_parent(cf_sectiontoitem(cs));
799 if (strcmp(cf_section_name1(tls), "server") == 0) {
800 home->parent_server = cf_section_name2(tls);
804 home_server *home2 = rad_malloc(sizeof(*home2));
806 memcpy(home2, home, sizeof(*home2));
808 home2->type = HOME_TYPE_ACCT;
810 home2->ping_user_password = NULL;
812 home2->parent_server = home->parent_server;
814 if (!rbtree_insert(home_servers_byname, home2)) {
815 cf_log_err(cf_sectiontoitem(cs),
816 "Internal error %d adding home server %s.",
823 !rbtree_insert(home_servers_byaddr, home2)) {
824 rbtree_deletebydata(home_servers_byname, home2);
825 cf_log_err(cf_sectiontoitem(cs),
826 "Internal error %d adding home server %s.",
833 home2->number = home_server_max_number++;
834 if (!rbtree_insert(home_servers_bynumber, home2)) {
835 rbtree_deletebydata(home_servers_byname, home2);
836 if (!home2->server) {
837 rbtree_deletebydata(home_servers_byname, home2);
839 cf_log_err(cf_sectiontoitem(cs),
840 "Internal error %d adding home server %s.",
849 * Mark it as already processed
851 cf_data_add(cs, "home_server", null_free, null_free);
857 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
858 int server_type, int num_home_servers)
862 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
864 if (!pool) return NULL; /* just for pairanoia */
866 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
871 pool->server_type = server_type;
872 pool->num_home_servers = num_home_servers;
877 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
878 const char *name, int server_type,
881 home_server myhome, *home;
882 CONF_SECTION *server_cs;
885 cf_log_err(cf_pairtoitem(cp),
886 "No value given for home_server.");
891 myhome.type = server_type;
892 home = rbtree_finddata(home_servers_byname, &myhome);
898 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
900 cf_log_err(cf_pairtoitem(cp),
901 "Unknown home_server \"%s\".", name);
905 home = rbtree_finddata(home_servers_byname, &myhome);
907 cf_log_err(cf_pairtoitem(cp),
908 "Internal error %d adding home server \"%s\".",
918 static int server_pool_add(realm_config_t *rc,
919 CONF_SECTION *cs, int server_type, int do_print)
922 home_pool_t *pool = NULL;
925 int num_home_servers;
928 name2 = cf_section_name1(cs);
929 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
930 (strcasecmp(name2, "home_server_pool") != 0))) {
931 cf_log_err(cf_sectiontoitem(cs),
932 "Section is not a home_server_pool.");
936 name2 = cf_section_name2(cs);
938 cf_log_err(cf_sectiontoitem(cs),
939 "Server pool section is missing a name.");
944 * Count the home servers and initalize them.
946 num_home_servers = 0;
947 for (cp = cf_pair_find(cs, "home_server");
949 cp = cf_pair_find_next(cs, cp, "home_server")) {
952 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
953 server_type, &home)) {
958 if (num_home_servers == 0) {
959 cf_log_err(cf_sectiontoitem(cs),
960 "No home servers defined in pool %s",
965 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
971 * Fallback servers must be defined, and must be
974 cp = cf_pair_find(cs, "fallback");
977 if (server_type == HOME_TYPE_COA) {
978 cf_log_err(cf_sectiontoitem(cs), "Home server pools of type \"coa\" cannot have a fallback virtual server.");
983 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
984 server_type, &pool->fallback)) {
989 if (!pool->fallback->server) {
990 cf_log_err(cf_sectiontoitem(cs), "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
995 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
997 cp = cf_pair_find(cs, "type");
999 static FR_NAME_NUMBER pool_types[] = {
1000 { "load-balance", HOME_POOL_LOAD_BALANCE },
1001 { "fail-over", HOME_POOL_FAIL_OVER },
1002 { "round_robin", HOME_POOL_LOAD_BALANCE },
1003 { "fail_over", HOME_POOL_FAIL_OVER },
1004 { "client-balance", HOME_POOL_CLIENT_BALANCE },
1005 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
1006 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
1010 value = cf_pair_value(cp);
1012 cf_log_err(cf_pairtoitem(cp),
1013 "No value given for type.");
1017 pool->type = fr_str2int(pool_types, value, 0);
1019 cf_log_err(cf_pairtoitem(cp),
1020 "Unknown type \"%s\".",
1025 if (do_print) cf_log_info(cs, "\ttype = %s", value);
1028 cp = cf_pair_find(cs, "virtual_server");
1030 pool->virtual_server = cf_pair_value(cp);
1031 if (!pool->virtual_server) {
1032 cf_log_err(cf_pairtoitem(cp), "No value given for virtual_server");
1037 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
1040 if (!cf_section_sub_find_name2(rc->cs, "server",
1041 pool->virtual_server)) {
1042 cf_log_err(cf_pairtoitem(cp), "No such server %s",
1043 pool->virtual_server);
1049 num_home_servers = 0;
1050 for (cp = cf_pair_find(cs, "home_server");
1052 cp = cf_pair_find_next(cs, cp, "home_server")) {
1055 value = cf_pair_value(cp);
1057 memset(&myhome, 0, sizeof(myhome));
1058 myhome.name = value;
1059 myhome.type = server_type;
1061 home = rbtree_finddata(home_servers_byname, &myhome);
1063 DEBUG2("Internal sanity check failed");
1068 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
1072 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1073 pool->servers[num_home_servers++] = home;
1074 } /* loop over home_server's */
1076 if (pool->fallback && do_print) {
1077 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1080 if (!rbtree_insert(home_pools_byname, pool)) {
1081 rad_assert("Internal sanity check failed");
1085 if (do_print) cf_log_info(cs, " }");
1087 cf_data_add(cs, "home_server_pool", pool, free);
1089 rad_assert(pool->server_type != 0);
1094 if (do_print) cf_log_info(cs, " }");
1100 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1102 const char *name, const char *secret,
1103 home_pool_type_t ldflag, home_pool_t **pool_p,
1104 int type, const char *server)
1107 int i, insert_point, num_home_servers;
1108 home_server myhome, *home;
1109 home_pool_t mypool, *pool;
1110 CONF_SECTION *subcs;
1112 rc = rc; /* -Wunused */
1121 * LOCAL realms get sanity checked, and nothing else happens.
1123 if (strcmp(name, "LOCAL") == 0) {
1125 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
1132 return 0; /* Not proxying. Can't do non-LOCAL realms */
1135 mypool.name = realm;
1136 mypool.server_type = type;
1137 pool = rbtree_finddata(home_pools_byname, &mypool);
1139 if (pool->type != ldflag) {
1140 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
1144 if (pool->server_type != type) {
1145 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
1152 home = rbtree_finddata(home_servers_byname, &myhome);
1154 if (secret && (strcmp(home->secret, secret) != 0)) {
1155 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
1159 if (home->type != type) {
1160 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
1165 * See if the home server is already listed
1166 * in the pool. If so, do nothing else.
1168 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1169 if (pool->servers[i] == home) {
1176 * If we do have a pool, check that there is room to
1177 * insert the home server we've found, or the one that we
1180 * Note that we insert it into the LAST available
1181 * position, in order to maintain the same order as in
1182 * the configuration files.
1186 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1187 if (pool->servers[i]) break;
1189 if (!pool->servers[i]) {
1194 if (insert_point < 0) {
1195 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);
1201 * No home server, allocate one.
1207 home = rad_malloc(sizeof(*home));
1208 memset(home, 0, sizeof(*home));
1211 home->hostname = name;
1213 home->secret = secret;
1215 home->proto = IPPROTO_UDP;
1217 p = strchr(name, ':');
1219 if (type == HOME_TYPE_AUTH) {
1220 home->port = PW_AUTH_UDP_PORT;
1222 home->port = PW_ACCT_UDP_PORT;
1228 } else if (p == name) {
1229 cf_log_err(cf_sectiontoitem(cs),
1230 "Invalid hostname %s.",
1236 home->port = atoi(p + 1);
1237 if ((home->port == 0) || (home->port > 65535)) {
1238 cf_log_err(cf_sectiontoitem(cs),
1245 q = rad_malloc((p - name) + 1);
1246 memcpy(q, name, (p - name));
1252 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1253 cf_log_err(cf_sectiontoitem(cs),
1254 "Failed looking up hostname %s.",
1260 home->src_ipaddr.af = home->ipaddr.af;
1262 home->ipaddr.af = AF_UNSPEC;
1263 home->server = server;
1268 * Use the old-style configuration.
1270 home->max_outstanding = 65535*16;
1271 home->zombie_period = rc->retry_delay * rc->retry_count;
1272 if (home->zombie_period == 0) home->zombie_period =30;
1273 home->response_window = home->zombie_period - 1;
1275 home->ping_check = HOME_PING_CHECK_NONE;
1277 home->revive_interval = rc->dead_time;
1279 if (rbtree_finddata(home_servers_byaddr, home)) {
1280 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
1285 if (!rbtree_insert(home_servers_byname, home)) {
1286 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1291 if (!rbtree_insert(home_servers_byaddr, home)) {
1292 rbtree_deletebydata(home_servers_byname, home);
1293 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1299 home->number = home_server_max_number++;
1300 if (!rbtree_insert(home_servers_bynumber, home)) {
1301 rbtree_deletebydata(home_servers_byname, home);
1302 if (home->ipaddr.af != AF_UNSPEC) {
1303 rbtree_deletebydata(home_servers_byname, home);
1305 cf_log_err(cf_sectiontoitem(cs),
1306 "Internal error %d adding home server %s.",
1315 * We now have a home server, see if we can insert it
1316 * into pre-existing pool.
1318 if (insert_point >= 0) {
1319 rad_assert(pool != NULL);
1320 pool->servers[insert_point] = home;
1324 rad_assert(pool == NULL);
1325 rad_assert(home != NULL);
1328 * Count the old-style realms of this name.
1330 num_home_servers = 0;
1331 for (subcs = cf_section_find_next(cs, NULL, "realm");
1333 subcs = cf_section_find_next(cs, subcs, "realm")) {
1334 const char *this = cf_section_name2(subcs);
1336 if (!this || (strcmp(this, realm) != 0)) continue;
1340 if (num_home_servers == 0) {
1341 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
1346 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1349 pool->servers[0] = home;
1351 if (!rbtree_insert(home_pools_byname, pool)) {
1352 rad_assert("Internal sanity check failed");
1362 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1365 const char *secret = NULL;
1366 home_pool_type_t ldflag;
1369 cp = cf_pair_find(cs, "ldflag");
1370 ldflag = HOME_POOL_FAIL_OVER;
1372 host = cf_pair_value(cp);
1374 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
1378 if (strcasecmp(host, "fail_over") == 0) {
1379 cf_log_info(cs, "\tldflag = fail_over");
1381 } else if (strcasecmp(host, "round_robin") == 0) {
1382 ldflag = HOME_POOL_LOAD_BALANCE;
1383 cf_log_info(cs, "\tldflag = round_robin");
1386 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
1389 } /* else don't print it. */
1392 * Allow old-style if it doesn't exist, or if it exists and
1395 cp = cf_pair_find(cs, "authhost");
1397 host = cf_pair_value(cp);
1399 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
1403 if (strcmp(host, "LOCAL") != 0) {
1404 cp = cf_pair_find(cs, "secret");
1406 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1410 secret = cf_pair_value(cp);
1412 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1417 cf_log_info(cs, "\tauthhost = %s", host);
1419 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1420 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1425 cp = cf_pair_find(cs, "accthost");
1427 host = cf_pair_value(cp);
1429 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1434 * Don't look for a secret again if it was found
1437 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1438 cp = cf_pair_find(cs, "secret");
1440 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1444 secret = cf_pair_value(cp);
1446 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1451 cf_log_info(cs, "\taccthost = %s", host);
1453 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1454 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1459 cp = cf_pair_find(cs, "virtual_server");
1461 host = cf_pair_value(cp);
1463 cf_log_err(cf_pairtoitem(cp), "No value specified for virtual_server");
1467 cf_log_info(cs, "\tvirtual_server = %s", host);
1469 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1470 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1473 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1474 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1479 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1487 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1488 const char *name, home_pool_t **dest,
1489 int server_type, int do_print)
1491 home_pool_t mypool, *pool;
1494 mypool.server_type = server_type;
1496 pool = rbtree_finddata(home_pools_byname, &mypool);
1498 CONF_SECTION *pool_cs;
1500 pool_cs = cf_section_sub_find_name2(rc->cs,
1504 pool_cs = cf_section_sub_find_name2(rc->cs,
1509 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1513 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1517 pool = rbtree_finddata(home_pools_byname, &mypool);
1519 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1524 if (pool->server_type != server_type) {
1525 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1536 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1542 home_pool_t *auth_pool, *acct_pool;
1543 const char *auth_pool_name, *acct_pool_name;
1545 const char *coa_pool_name;
1546 home_pool_t *coa_pool;
1550 name2 = cf_section_name1(cs);
1551 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1552 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1556 name2 = cf_section_name2(cs);
1558 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1563 auth_pool = acct_pool = NULL;
1564 auth_pool_name = acct_pool_name = NULL;
1567 coa_pool_name = NULL;
1571 * Prefer new configuration to old one.
1573 cp = cf_pair_find(cs, "pool");
1574 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1575 if (cp) auth_pool_name = cf_pair_value(cp);
1576 if (cp && auth_pool_name) {
1577 acct_pool_name = auth_pool_name;
1578 if (!add_pool_to_realm(rc, cs,
1579 auth_pool_name, &auth_pool,
1580 HOME_TYPE_AUTH, 1)) {
1583 if (!add_pool_to_realm(rc, cs,
1584 auth_pool_name, &acct_pool,
1585 HOME_TYPE_ACCT, 0)) {
1590 cp = cf_pair_find(cs, "auth_pool");
1591 if (cp) auth_pool_name = cf_pair_value(cp);
1592 if (cp && auth_pool_name) {
1594 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1597 if (!add_pool_to_realm(rc, cs,
1598 auth_pool_name, &auth_pool,
1599 HOME_TYPE_AUTH, 1)) {
1604 cp = cf_pair_find(cs, "acct_pool");
1605 if (cp) acct_pool_name = cf_pair_value(cp);
1606 if (cp && acct_pool_name) {
1607 int do_print = TRUE;
1610 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1616 (strcmp(auth_pool_name, acct_pool_name) != 0))) {
1620 if (!add_pool_to_realm(rc, cs,
1621 acct_pool_name, &acct_pool,
1622 HOME_TYPE_ACCT, do_print)) {
1628 cp = cf_pair_find(cs, "coa_pool");
1629 if (cp) coa_pool_name = cf_pair_value(cp);
1630 if (cp && coa_pool_name) {
1631 int do_print = TRUE;
1633 if (!add_pool_to_realm(rc, cs,
1634 coa_pool_name, &coa_pool,
1635 HOME_TYPE_COA, do_print)) {
1642 cf_log_info(cs, " realm %s {", name2);
1646 * The realm MAY already exist if it's an old-style realm.
1647 * In that case, merge the old-style realm with this one.
1649 r = realm_find2(name2);
1650 if (r && (strcmp(r->name, name2) == 0)) {
1651 if (cf_pair_find(cs, "auth_pool") ||
1652 cf_pair_find(cs, "acct_pool")) {
1653 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1657 if (!old_realm_config(rc, cs, r)) {
1661 cf_log_info(cs, " } # realm %s", name2);
1667 if (name2[0] == '~') {
1672 * Include substring matches.
1674 rcode = regcomp(®, name2 + 1,
1675 REG_EXTENDED | REG_NOSUB | REG_ICASE);
1679 regerror(rcode, ®, buffer, sizeof(buffer));
1681 cf_log_err(cf_sectiontoitem(cs),
1682 "Invalid regex \"%s\": %s",
1690 r = rad_malloc(sizeof(*r));
1691 memset(r, 0, sizeof(*r));
1696 r->auth_pool = auth_pool;
1697 r->acct_pool = acct_pool;
1699 r->coa_pool = coa_pool;
1702 if (auth_pool_name &&
1703 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1704 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1706 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1707 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1709 if (coa_pool_name) cf_log_info(cs, "\tcoa_pool = %s", coa_pool_name);
1714 cp = cf_pair_find(cs, "nostrip");
1715 if (cp && (cf_pair_value(cp) == NULL)) {
1717 cf_log_info(cs, "\tnostrip");
1721 * We're a new-style realm. Complain if we see the old
1724 if (r->auth_pool || r->acct_pool) {
1725 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1726 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1727 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1728 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1729 DEBUG2W("Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1734 * The realm MAY be an old-style realm, as there
1735 * was no auth_pool or acct_pool. Double-check
1736 * it, just to be safe.
1738 } else if (!old_realm_config(rc, cs, r)) {
1744 * It's a regex. Add it to a separate list.
1746 if (name2[0] == '~') {
1747 realm_regex_t *rr, **last;
1749 rr = rad_malloc(sizeof(*rr));
1751 last = &realms_regex;
1752 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1760 cf_log_info(cs, " }");
1765 if (!rbtree_insert(realms_byname, r)) {
1766 rad_assert("Internal sanity check failed");
1770 cf_log_info(cs, " }");
1775 cf_log_info(cs, " } # realm %s", name2);
1781 static const FR_NAME_NUMBER home_server_types[] = {
1782 { "auth", HOME_TYPE_AUTH },
1783 { "auth+acct", HOME_TYPE_AUTH },
1784 { "acct", HOME_TYPE_ACCT },
1785 { "coa", HOME_TYPE_COA },
1789 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1792 const char *name, *type;
1794 CONF_SECTION *server_cs;
1796 cp = cf_pair_find(cs, "home_server");
1798 cf_log_err(cf_sectiontoitem(cs), "Pool does not contain a \"home_server\" entry");
1799 return HOME_TYPE_INVALID;
1802 name = cf_pair_value(cp);
1804 cf_log_err(cf_pairtoitem(cp), "home_server entry does not reference a home server");
1805 return HOME_TYPE_INVALID;
1808 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1810 cf_log_err(cf_pairtoitem(cp), "home_server \"%s\" does not exist", name);
1811 return HOME_TYPE_INVALID;
1814 cp = cf_pair_find(server_cs, "type");
1816 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s does not contain a \"type\" entry", name);
1817 return HOME_TYPE_INVALID;
1820 type = cf_pair_value(cp);
1822 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an empty \"type\" entry", name);
1823 return HOME_TYPE_INVALID;
1826 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1827 if (home == HOME_TYPE_INVALID) {
1828 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1829 return HOME_TYPE_INVALID;
1832 return home; /* 'cause we miss it so much */
1836 int realms_init(CONF_SECTION *config)
1840 CONF_SECTION *server_cs;
1842 realm_config_t *rc, *old_rc;
1844 if (realms_byname) return 1;
1846 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1847 if (!realms_byname) {
1853 home_servers_byaddr = rbtree_create(home_server_addr_cmp, home_server_free, 0);
1854 if (!home_servers_byaddr) {
1859 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1860 if (!home_servers_byname) {
1866 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1867 if (!home_servers_bynumber) {
1873 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1874 if (!home_pools_byname) {
1880 rc = rad_malloc(sizeof(*rc));
1881 memset(rc, 0, sizeof(*rc));
1885 cs = cf_subsection_find_next(config, NULL, "proxy");
1887 if (cf_section_parse(cs, rc, proxy_config) < 0) {
1888 radlog(L_ERR, "Failed parsing proxy section");
1895 rc->dead_time = DEAD_TIME;
1896 rc->retry_count = RETRY_COUNT;
1897 rc->retry_delay = RETRY_DELAY;
1899 rc->wake_all_if_all_dead= 0;
1902 for (cs = cf_subsection_find_next(config, NULL, "home_server");
1904 cs = cf_subsection_find_next(config, cs, "home_server")) {
1905 if (!home_server_add(rc, cs)) {
1913 * Loop over virtual servers to find homes which are
1916 for (server_cs = cf_subsection_find_next(config, NULL, "server");
1918 server_cs = cf_subsection_find_next(config, server_cs, "server")) {
1919 for (cs = cf_subsection_find_next(server_cs, NULL, "home_server");
1921 cs = cf_subsection_find_next(server_cs, cs, "home_server")) {
1922 if (!home_server_add(rc, cs)) {
1931 for (cs = cf_subsection_find_next(config, NULL, "realm");
1933 cs = cf_subsection_find_next(config, cs, "realm")) {
1934 if (!realm_add(rc, cs)) {
1943 * CoA pools aren't necessarily tied to realms.
1945 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1947 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1951 * Pool was already loaded.
1953 if (cf_data_find(cs, "home_server_pool")) continue;
1955 type = pool_peek_type(config, cs);
1956 if (type == HOME_TYPE_INVALID) {
1962 if (!server_pool_add(rc, cs, type, TRUE)) {
1972 xlat_register("home_server", xlat_home_server, NULL);
1973 xlat_register("home_server_pool", xlat_server_pool, NULL);
1977 * Swap pointers atomically.
1979 old_rc = realm_config;
1987 * Find a realm where "name" might be the regex.
1989 REALM *realm_find2(const char *name)
1994 if (!name) name = "NULL";
1996 myrealm.name = name;
1997 realm = rbtree_finddata(realms_byname, &myrealm);
1998 if (realm) return realm;
2002 realm_regex_t *this;
2004 for (this = realms_regex; this != NULL; this = this->next) {
2005 if (strcmp(this->realm->name, name) == 0) {
2013 * Couldn't find a realm. Look for DEFAULT.
2015 myrealm.name = "DEFAULT";
2016 return rbtree_finddata(realms_byname, &myrealm);
2021 * Find a realm in the REALM list.
2023 REALM *realm_find(const char *name)
2028 if (!name) name = "NULL";
2030 myrealm.name = name;
2031 realm = rbtree_finddata(realms_byname, &myrealm);
2032 if (realm) return realm;
2036 realm_regex_t *this;
2038 for (this = realms_regex; this != NULL; this = this->next) {
2043 * Include substring matches.
2045 if (regcomp(®, this->realm->name + 1,
2046 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
2050 compare = regexec(®, name, 0, NULL, 0);
2053 if (compare == 0) return this->realm;
2059 * Couldn't find a realm. Look for DEFAULT.
2061 myrealm.name = "DEFAULT";
2062 return rbtree_finddata(realms_byname, &myrealm);
2069 * Allocate the proxy list if it doesn't already exist, and copy request
2070 * VPs into it. Setup src/dst IP addresses based on home server, and
2071 * calculate and add the message-authenticator.
2073 * This is a distinct function from home_server_ldb, as not all home_server
2074 * lookups result in the *CURRENT* request being proxied,
2075 * as in rlm_replicate, and this may trigger asserts elsewhere in the
2078 void home_server_update_request(home_server *home, REQUEST *request)
2082 * Allocate the proxy packet, only if it wasn't
2083 * already allocated by a module. This check is
2084 * mainly to support the proxying of EAP-TTLS and
2085 * EAP-PEAP tunneled requests.
2087 * In those cases, the EAP module creates a
2088 * "fake" request, and recursively passes it
2089 * through the authentication stage of the
2090 * server. The module then checks if the request
2091 * was supposed to be proxied, and if so, creates
2092 * a proxy packet from the TUNNELED request, and
2093 * not from the EAP request outside of the
2096 * The proxy then works like normal, except that
2097 * the response packet is "eaten" by the EAP
2098 * module, and encapsulated into an EAP packet.
2100 if (!request->proxy) {
2101 request->proxy = rad_alloc(request, TRUE);
2102 if (!request->proxy) {
2103 radlog(L_ERR, "no memory");
2108 * Copy the request, then look up name
2109 * and plain-text password in the copy.
2111 * Note that the User-Name attribute is
2112 * the *original* as sent over by the
2113 * client. The Stripped-User-Name
2114 * attribute is the one hacked through
2117 request->proxy->vps = paircopy(request->packet->vps);
2121 * Update the various fields as appropriate.
2123 request->proxy->src_ipaddr = home->src_ipaddr;
2124 request->proxy->src_port = 0;
2125 request->proxy->dst_ipaddr = home->ipaddr;
2126 request->proxy->dst_port = home->port;
2127 request->home_server = home;
2130 * Access-Requests have a Message-Authenticator added,
2131 * unless one already exists.
2133 if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2134 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY)) {
2135 radius_pairmake(request, &request->proxy->vps,
2136 "Message-Authenticator", "0x00",
2141 home_server *home_server_ldb(const char *realmname,
2142 home_pool_t *pool, REQUEST *request)
2146 home_server *found = NULL;
2147 home_server *zombie = NULL;
2151 * Determine how to pick choose the home server.
2153 switch (pool->type) {
2157 * For load-balancing by client IP address, we
2158 * pick a home server by hashing the client IP.
2160 * This isn't as even a load distribution as
2161 * tracking the State attribute, but it's better
2164 case HOME_POOL_CLIENT_BALANCE:
2165 switch (request->packet->src_ipaddr.af) {
2167 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2168 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2171 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2172 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2178 start = hash % pool->num_home_servers;
2181 case HOME_POOL_CLIENT_PORT_BALANCE:
2182 switch (request->packet->src_ipaddr.af) {
2184 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2185 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2188 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2189 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2195 fr_hash_update(&request->packet->src_port,
2196 sizeof(request->packet->src_port), hash);
2197 start = hash % pool->num_home_servers;
2200 case HOME_POOL_KEYED_BALANCE:
2201 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY, 0, TAG_ANY)) != NULL) {
2202 hash = fr_hash(vp->vp_strvalue, vp->length);
2203 start = hash % pool->num_home_servers;
2208 case HOME_POOL_LOAD_BALANCE:
2209 case HOME_POOL_FAIL_OVER:
2213 default: /* this shouldn't happen... */
2220 * Starting with the home server we chose, loop through
2221 * all home servers. If the current one is dead, skip
2222 * it. If it is too busy, skip it.
2224 * Otherwise, use it.
2226 for (count = 0; count < pool->num_home_servers; count++) {
2227 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
2229 if (!home) continue;
2232 * Skip dead home servers.
2234 * Home servers that are unknown, alive, or zombie
2235 * are used for proxying.
2237 if (home->state == HOME_STATE_IS_DEAD) {
2242 * This home server is too busy. Choose another one.
2244 if (home->currently_outstanding >= home->max_outstanding) {
2250 * We read the packet from a detail file, AND it
2251 * came from this server. Don't re-proxy it
2254 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2255 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
2256 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2262 * Default virtual: ignore homes tied to a
2265 if (!request->server && home->parent_server) {
2270 * A virtual AND home is tied to virtual,
2271 * ignore ones which don't match.
2273 if (request->server && home->parent_server &&
2274 strcmp(request->server, home->parent_server) != 0) {
2279 * Allow request->server && !home->parent_server
2281 * i.e. virtuals can proxy to globally defined
2286 * It's zombie, so we remember the first zombie
2287 * we find, but we don't mark it as a "live"
2290 if (home->state == HOME_STATE_ZOMBIE) {
2291 if (!zombie) zombie = home;
2296 * We've found the first "live" one. Use that.
2298 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2304 * Otherwise we're doing some kind of load balancing.
2305 * If we haven't found one yet, pick this one.
2312 RDEBUG3("PROXY %s %d\t%s %d",
2313 found->name, found->currently_outstanding,
2314 home->name, home->currently_outstanding);
2317 * Prefer this server if it's less busy than the
2318 * one we had previously found.
2320 if (home->currently_outstanding < found->currently_outstanding) {
2321 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2322 home->name, found->name);
2328 * Ignore servers which are busier than the one
2331 if (home->currently_outstanding > found->currently_outstanding) {
2332 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2333 home->name, found->name);
2338 * From the list of servers which have the same
2339 * load, choose one at random.
2341 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2344 } /* loop over the home servers */
2347 * We have no live servers, BUT we have a zombie. Use
2348 * the zombie as a last resort.
2350 if (!found && zombie) {
2356 * There's a fallback if they're all dead.
2358 if (!found && pool->fallback) {
2359 found = pool->fallback;
2361 DEBUGW("Home server pool %s failing over to fallback %s",
2362 pool->name, found->server);
2363 if (pool->in_fallback) goto update_and_return;
2365 pool->in_fallback = TRUE;
2368 * Run the trigger once an hour saying that
2371 if ((pool->time_all_dead + 3600) < request->timestamp) {
2372 pool->time_all_dead = request->timestamp;
2373 exec_trigger(request, pool->cs, "home_server_pool.fallback", FALSE);
2379 if ((found != pool->fallback) && pool->in_fallback) {
2380 pool->in_fallback = FALSE;
2381 exec_trigger(request, pool->cs, "home_server_pool.normal", FALSE);
2388 * No live match found, and no fallback to the "DEFAULT"
2389 * realm. We fix this by blindly marking all servers as
2390 * "live". But only do it for ones that don't support
2391 * "pings", as they will be marked live when they
2392 * actually are live.
2394 if (!realm_config->fallback &&
2395 realm_config->wake_all_if_all_dead) {
2396 for (count = 0; count < pool->num_home_servers; count++) {
2397 home_server *home = pool->servers[count];
2399 if (!home) continue;
2401 if ((home->state == HOME_STATE_IS_DEAD) &&
2402 (home->ping_check == HOME_PING_CHECK_NONE)) {
2403 home->state = HOME_STATE_ALIVE;
2404 if (!found) found = home;
2408 if (found) goto update_and_return;
2412 * Still nothing. Look up the DEFAULT realm, but only
2413 * if we weren't looking up the NULL or DEFAULT realms.
2415 if (realm_config->fallback &&
2417 (strcmp(realmname, "NULL") != 0) &&
2418 (strcmp(realmname, "DEFAULT") != 0)) {
2419 REALM *rd = realm_find("DEFAULT");
2421 if (!rd) return NULL;
2424 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2425 pool = rd->auth_pool;
2427 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2428 pool = rd->acct_pool;
2430 if (!pool) return NULL;
2432 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2433 return home_server_ldb(rd->name, pool, request);
2437 * Still haven't found anything. Oh well.
2443 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port, int proto)
2447 memset(&myhome, 0, sizeof(myhome));
2448 myhome.ipaddr = *ipaddr;
2449 myhome.src_ipaddr.af = ipaddr->af;
2452 myhome.proto = proto;
2454 myhome.proto = IPPROTO_UDP;
2456 myhome.server = NULL; /* we're not called for internal proxying */
2458 return rbtree_finddata(home_servers_byaddr, &myhome);
2462 home_server *home_server_byname(const char *name, int type)
2466 memset(&myhome, 0, sizeof(myhome));
2470 return rbtree_finddata(home_servers_byname, &myhome);
2475 home_server *home_server_bynumber(int number)
2479 memset(&myhome, 0, sizeof(myhome));
2480 myhome.number = number;
2481 myhome.server = NULL; /* we're not called for internal proxying */
2483 return rbtree_finddata(home_servers_bynumber, &myhome);
2487 home_pool_t *home_pool_byname(const char *name, int type)
2491 memset(&mypool, 0, sizeof(mypool));
2493 mypool.server_type = type;
2494 return rbtree_finddata(home_pools_byname, &mypool);