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;
136 tls_server_conf_free(home->tls);
143 static int home_server_name_cmp(const void *one, const void *two)
145 const home_server *a = one;
146 const home_server *b = two;
148 if (a->type < b->type) return -1;
149 if (a->type > b->type) return +1;
151 return strcasecmp(a->name, b->name);
154 static int home_server_addr_cmp(const void *one, const void *two)
157 const home_server *a = one;
158 const home_server *b = two;
160 if (a->server && !b->server) return -1;
161 if (!a->server && b->server) return +1;
162 if (a->server && b->server) {
163 rcode = a->type - b->type;
164 if (rcode != 0) return rcode;
165 return strcmp(a->server, b->server);
168 if (a->port < b->port) return -1;
169 if (a->port > b->port) return +1;
172 if (a->proto < b->proto) return -1;
173 if (a->proto > b->proto) return +1;
176 rcode = fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
177 if (rcode != 0) return rcode;
179 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
183 static int home_server_number_cmp(const void *one, const void *two)
185 const home_server *a = one;
186 const home_server *b = two;
188 return (a->number - b->number);
192 static int home_pool_name_cmp(const void *one, const void *two)
194 const home_pool_t *a = one;
195 const home_pool_t *b = two;
197 if (a->server_type < b->server_type) return -1;
198 if (a->server_type > b->server_type) return +1;
200 return strcasecmp(a->name, b->name);
204 static size_t xlat_cs(CONF_SECTION *cs, char *fmt, char *out, size_t outlen)
207 const char *value = NULL;
212 if (strcmp(fmt, "instance") == 0) {
213 value = cf_section_name2(cs);
221 cp = cf_pair_find(cs, fmt);
222 if (!cp || !(value = cf_pair_value(cp))) {
228 strlcpy(out, value, outlen);
235 * Xlat for %{home_server:foo}
237 static size_t xlat_home_server(UNUSED void *instance, REQUEST *request,
238 char *fmt, char *out, size_t outlen,
239 UNUSED RADIUS_ESCAPE_STRING func)
241 if (!fmt || !out || (outlen < 1)) return 0;
243 if (!request || !request->home_server) {
248 return xlat_cs(request->home_server->cs, fmt, out, outlen);
253 * Xlat for %{home_server_pool:foo}
255 static size_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
256 char *fmt, char *out, size_t outlen,
257 UNUSED RADIUS_ESCAPE_STRING func)
259 if (!fmt || !out || (outlen < 1)) return 0;
261 if (!request || !request->home_pool) {
266 return xlat_cs(request->home_pool->cs, fmt, out, outlen);
270 void realms_free(void)
274 rbtree_free(home_servers_bynumber);
275 home_servers_bynumber = NULL;
278 rbtree_free(home_servers_byname);
279 home_servers_byname = NULL;
281 rbtree_free(home_servers_byaddr);
282 home_servers_byaddr = NULL;
284 rbtree_free(home_pools_byname);
285 home_pools_byname = NULL;
288 rbtree_free(realms_byname);
289 realms_byname = NULL;
293 realm_regex_t *this, *next;
295 for (this = realms_regex; this != NULL; this = next) {
310 static CONF_PARSER limit_config[] = {
311 { "max_connections", PW_TYPE_INTEGER,
312 offsetof(home_server, max_connections), NULL, "16" },
314 { "max_requests", PW_TYPE_INTEGER,
315 offsetof(home_server,max_requests), NULL, "0" },
317 { "lifetime", PW_TYPE_INTEGER,
318 offsetof(home_server,lifetime), NULL, "0" },
320 { "idle_timeout", PW_TYPE_INTEGER,
321 offsetof(home_server,idle_timeout), NULL, "0" },
323 { NULL, -1, 0, NULL, NULL } /* end the list */
326 static struct in_addr hs_ip4addr;
327 static struct in6_addr hs_ip6addr;
328 static char *hs_srcipaddr = NULL;
329 static char *hs_type = NULL;
330 static char *hs_check = NULL;
331 static char *hs_virtual_server = NULL;
333 static char *hs_proto = NULL;
337 static CONF_PARSER home_server_coa[] = {
338 { "irt", PW_TYPE_INTEGER,
339 offsetof(home_server, coa_irt), 0, Stringify(2) },
340 { "mrt", PW_TYPE_INTEGER,
341 offsetof(home_server, coa_mrt), 0, Stringify(16) },
342 { "mrc", PW_TYPE_INTEGER,
343 offsetof(home_server, coa_mrc), 0, Stringify(5) },
344 { "mrd", PW_TYPE_INTEGER,
345 offsetof(home_server, coa_mrd), 0, Stringify(30) },
347 { NULL, -1, 0, NULL, NULL } /* end the list */
351 static CONF_PARSER home_server_config[] = {
352 { "ipaddr", PW_TYPE_IPADDR,
353 0, &hs_ip4addr, NULL },
354 { "ipv6addr", PW_TYPE_IPV6ADDR,
355 0, &hs_ip6addr, NULL },
356 { "virtual_server", PW_TYPE_STRING_PTR,
357 0, &hs_virtual_server, NULL },
359 { "port", PW_TYPE_INTEGER,
360 offsetof(home_server,port), NULL, "0" },
362 { "type", PW_TYPE_STRING_PTR,
366 { "proto", PW_TYPE_STRING_PTR,
367 0, &hs_proto, NULL },
370 { "secret", PW_TYPE_STRING_PTR,
371 offsetof(home_server,secret), NULL, NULL},
373 { "src_ipaddr", PW_TYPE_STRING_PTR,
374 0, &hs_srcipaddr, NULL },
376 { "response_window", PW_TYPE_INTEGER,
377 offsetof(home_server,response_window), NULL, "30" },
378 { "no_response_fail", PW_TYPE_BOOLEAN,
379 offsetof(home_server,no_response_fail), NULL, NULL },
380 { "max_outstanding", PW_TYPE_INTEGER,
381 offsetof(home_server,max_outstanding), NULL, "65536" },
382 { "require_message_authenticator", PW_TYPE_BOOLEAN,
383 offsetof(home_server, message_authenticator), 0, NULL },
385 { "zombie_period", PW_TYPE_INTEGER,
386 offsetof(home_server,zombie_period), NULL, "40" },
387 { "status_check", PW_TYPE_STRING_PTR,
388 0, &hs_check, "none" },
389 { "ping_check", PW_TYPE_STRING_PTR,
390 0, &hs_check, NULL },
392 { "ping_interval", PW_TYPE_INTEGER,
393 offsetof(home_server,ping_interval), NULL, "30" },
394 { "check_interval", PW_TYPE_INTEGER,
395 offsetof(home_server,ping_interval), NULL, "30" },
396 { "num_answers_to_alive", PW_TYPE_INTEGER,
397 offsetof(home_server,num_pings_to_alive), NULL, "3" },
398 { "num_pings_to_alive", PW_TYPE_INTEGER,
399 offsetof(home_server,num_pings_to_alive), NULL, "3" },
400 { "revive_interval", PW_TYPE_INTEGER,
401 offsetof(home_server,revive_interval), NULL, "300" },
402 { "status_check_timeout", PW_TYPE_INTEGER,
403 offsetof(home_server,ping_timeout), NULL, "4" },
405 { "username", PW_TYPE_STRING_PTR,
406 offsetof(home_server,ping_user_name), NULL, NULL},
407 { "password", PW_TYPE_STRING_PTR,
408 offsetof(home_server,ping_user_password), NULL, NULL},
411 { "historic_average_window", PW_TYPE_INTEGER,
412 offsetof(home_server,ema.window), NULL, NULL },
416 { "coa", PW_TYPE_SUBSECTION, 0, NULL, (const void *) home_server_coa },
419 { "limit", PW_TYPE_SUBSECTION, 0, NULL, (const void *) limit_config },
421 { NULL, -1, 0, NULL, NULL } /* end the list */
425 static void null_free(UNUSED void *data)
429 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs)
437 free(hs_virtual_server); /* used only for printing during parsing */
438 hs_virtual_server = NULL;
440 name2 = cf_section_name2(cs);
442 cf_log_err(cf_sectiontoitem(cs),
443 "Home server section is missing a name.");
447 home = rad_malloc(sizeof(*home));
448 memset(home, 0, sizeof(*home));
454 * For zombie period calculations. We want to count
455 * zombies from the time when the server starts, instead
458 home->last_packet = time(NULL);
461 * Authentication servers have a default "no_response_fail = 0".
462 * Accounting servers have a default "no_response_fail = 1".
464 * This is because authentication packets are retried, so
465 * they can fail over to another home server. Accounting
466 * packets are not retried, so they cannot fail over, and
467 * instead should be rejected immediately.
469 home->no_response_fail = 2;
471 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
472 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
473 if (cf_section_parse(cs, home, home_server_config) < 0) {
478 * Figure out which one to use.
480 if (cf_pair_find(cs, "ipaddr")) {
481 home->ipaddr.af = AF_INET;
482 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
484 } else if (cf_pair_find(cs, "ipv6addr")) {
485 home->ipaddr.af = AF_INET6;
486 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
488 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
489 home->ipaddr.af = AF_UNSPEC;
490 home->server = cf_pair_value(cp);
492 cf_log_err(cf_sectiontoitem(cs),
493 "Invalid value for virtual_server");
497 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
499 cf_log_err(cf_sectiontoitem(cs),
500 "No such server %s", home->server);
505 * When CoA is used, the user has to specify the type
506 * of the home server, even when they point to
509 home->secret = strdup("");
513 cf_log_err(cf_sectiontoitem(cs),
514 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
531 if (!home->port || (home->port > 65535)) {
532 cf_log_err(cf_sectiontoitem(cs),
533 "No port, or invalid port defined for home server %s.",
539 cf_log_err(cf_sectiontoitem(cs),
540 "Fatal error! Home server %s is ourselves!",
546 cf_log_err(cf_sectiontoitem(cs),
547 "No shared secret defined for home server %s.",
553 * Use a reasonable default.
556 if (!hs_type) hs_type = strdup("auth+acct");
558 if (strcasecmp(hs_type, "auth") == 0) {
559 home->type = HOME_TYPE_AUTH;
560 if (home->no_response_fail == 2) home->no_response_fail = 0;
562 } else if (strcasecmp(hs_type, "acct") == 0) {
563 home->type = HOME_TYPE_ACCT;
564 if (home->no_response_fail == 2) home->no_response_fail = 1;
566 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
567 home->type = HOME_TYPE_AUTH;
571 } else if (strcasecmp(hs_type, "coa") == 0) {
572 home->type = HOME_TYPE_COA;
575 if (home->server != NULL) {
576 cf_log_err(cf_sectiontoitem(cs),
577 "Home servers of type \"coa\" cannot point to a virtual server");
583 cf_log_err(cf_sectiontoitem(cs),
584 "Invalid type \"%s\" for home server %s.",
591 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
592 home->ping_check = HOME_PING_CHECK_NONE;
594 } else if (strcasecmp(hs_check, "status-server") == 0) {
595 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
597 } else if (strcasecmp(hs_check, "request") == 0) {
598 home->ping_check = HOME_PING_CHECK_REQUEST;
600 if (!home->ping_user_name ||
601 !*home->ping_user_name) {
602 cf_log_err(cf_sectiontoitem(cs), "You must supply a 'username' to enable status_check=request");
606 if ((home->type == HOME_TYPE_AUTH) &&
607 (!home->ping_user_password ||
608 !*home->ping_user_password)) {
609 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable status_check=request");
614 cf_log_err(cf_sectiontoitem(cs),
615 "Invalid status__check \"%s\" for home server %s.",
622 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
623 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
624 if (!home->ping_user_name) {
625 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable status_check=request");
629 if ((home->type == HOME_TYPE_AUTH) &&
630 !home->ping_user_password) {
631 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable status_check=request");
636 home->proto = IPPROTO_UDP;
639 if (strcmp(hs_proto, "udp") == 0) {
643 } else if (strcmp(hs_proto, "tcp") == 0) {
646 home->proto = IPPROTO_TCP;
648 if (home->ping_check != HOME_PING_CHECK_NONE) {
649 cf_log_err(cf_sectiontoitem(cs),
650 "Only 'status_check = none' is allowed for home servers with 'proto = tcp'");
655 cf_log_err(cf_sectiontoitem(cs),
656 "Unknown proto \"%s\".", hs_proto);
663 rbtree_finddata(home_servers_byaddr, home)) {
664 cf_log_err(cf_sectiontoitem(cs), "Duplicate home server");
669 * Check the TLS configuration.
671 tls = cf_section_sub_find(cs, "tls");
674 * If the home is a virtual server, don't look up source IP.
677 rad_assert(home->ipaddr.af != AF_UNSPEC);
680 * Otherwise look up the source IP using the same
681 * address family as the destination IP.
684 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
685 cf_log_err(cf_sectiontoitem(cs), "Failed parsing src_ipaddr");
691 * Source isn't specified: Source is
692 * the correct address family, but all zeros.
694 memset(&home->src_ipaddr, 0, sizeof(home->src_ipaddr));
695 home->src_ipaddr.af = home->ipaddr.af;
698 if (tls && (home->proto != IPPROTO_TCP)) {
699 cf_log_err(cf_sectiontoitem(cs), "TLS transport is not available for UDP sockets.");
706 cf_log_err(cf_sectiontoitem(cs), "TLS transport is not available in this executable.");
711 * Parse the SSL client configuration.
714 home->tls = tls_client_conf_parse(tls);
722 cf_log_err(cf_sectiontoitem(cs), "Virtual home_servers cannot have a \"tls\" subsection");
727 * Make sure that this is set.
729 if (home->src_ipaddr.af == AF_UNSPEC) {
730 home->src_ipaddr.af = home->ipaddr.af;
736 if (rbtree_finddata(home_servers_byname, home) != NULL) {
737 cf_log_err(cf_sectiontoitem(cs),
738 "Duplicate home server name %s.", name2);
743 (rbtree_finddata(home_servers_byaddr, home) != NULL)) {
744 cf_log_err(cf_sectiontoitem(cs),
745 "Duplicate home server IP %s.", name2);
749 if (!rbtree_insert(home_servers_byname, home)) {
750 cf_log_err(cf_sectiontoitem(cs),
751 "Internal error %d adding home server %s.",
757 !rbtree_insert(home_servers_byaddr, home)) {
758 rbtree_deletebydata(home_servers_byname, home);
759 cf_log_err(cf_sectiontoitem(cs),
760 "Internal error %d adding home server %s.",
766 home->number = home_server_max_number++;
767 if (!rbtree_insert(home_servers_bynumber, home)) {
768 rbtree_deletebydata(home_servers_byname, home);
769 if (home->ipaddr.af != AF_UNSPEC) {
770 rbtree_deletebydata(home_servers_byname, home);
772 cf_log_err(cf_sectiontoitem(cs),
773 "Internal error %d adding home server %s.",
779 if (home->max_outstanding < 8) home->max_outstanding = 8;
780 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
782 if (home->ping_interval < 6) home->ping_interval = 6;
783 if (home->ping_interval > 120) home->ping_interval = 120;
785 if (home->response_window < 1) home->response_window = 1;
786 if (home->response_window > 60) home->response_window = 60;
787 if (home->response_window > mainconfig.max_request_time) home->response_window = mainconfig.max_request_time;
789 if (home->zombie_period < 1) home->zombie_period = 1;
790 if (home->zombie_period > 120) home->zombie_period = 120;
792 if (home->zombie_period < home->response_window) {
793 home->zombie_period = home->response_window;
796 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
797 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
799 if (home->ping_timeout < 3) home->ping_timeout = 3;
800 if (home->ping_timeout > 10) home->ping_timeout = 10;
802 if (home->revive_interval < 60) home->revive_interval = 60;
803 if (home->revive_interval > 3600) home->revive_interval = 3600;
806 if (home->coa_irt < 1) home->coa_irt = 1;
807 if (home->coa_irt > 5) home->coa_irt = 5;
809 if (home->coa_mrc < 0) home->coa_mrc = 0;
810 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
812 if (home->coa_mrt < 0) home->coa_mrt = 0;
813 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
815 if (home->coa_mrd < 5) home->coa_mrd = 5;
816 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
819 if (home->max_connections > 1024) home->max_connections = 1024;
823 * UDP sockets can't be connection limited.
825 if (home->proto != IPPROTO_TCP) home->max_connections = 0;
828 if ((home->idle_timeout > 0) && (home->idle_timeout < 5))
829 home->idle_timeout = 5;
830 if ((home->lifetime > 0) && (home->lifetime < 5))
832 if ((home->lifetime > 0) && (home->idle_timeout > home->lifetime))
833 home->idle_timeout = 0;
835 tls = cf_item_parent(cf_sectiontoitem(cs));
836 if (strcmp(cf_section_name1(tls), "server") == 0) {
837 home->parent_server = cf_section_name2(tls);
841 home_server *home2 = rad_malloc(sizeof(*home2));
843 memcpy(home2, home, sizeof(*home2));
845 home2->type = HOME_TYPE_ACCT;
847 home2->ping_user_password = NULL;
849 home2->parent_server = home->parent_server;
851 if (home->no_response_fail == 2) home->no_response_fail = 0;
852 if (home2->no_response_fail == 2) home2->no_response_fail = 1;
854 if (!rbtree_insert(home_servers_byname, home2)) {
855 cf_log_err(cf_sectiontoitem(cs),
856 "Internal error %d adding home server %s.",
863 !rbtree_insert(home_servers_byaddr, home2)) {
864 rbtree_deletebydata(home_servers_byname, home2);
865 cf_log_err(cf_sectiontoitem(cs),
866 "Internal error %d adding home server %s.",
873 home2->number = home_server_max_number++;
874 if (!rbtree_insert(home_servers_bynumber, home2)) {
875 rbtree_deletebydata(home_servers_byname, home2);
876 if (!home2->server) {
877 rbtree_deletebydata(home_servers_byname, home2);
879 cf_log_err(cf_sectiontoitem(cs),
880 "Internal error %d adding home server %s.",
889 * Mark it as already processed
891 cf_data_add(cs, "home_server", null_free, null_free);
897 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
898 int server_type, int num_home_servers)
902 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
904 if (!pool) return NULL; /* just for pairanoia */
906 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
911 pool->server_type = server_type;
912 pool->num_home_servers = num_home_servers;
917 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
918 const char *name, int server_type,
921 home_server myhome, *home;
922 CONF_SECTION *server_cs;
925 cf_log_err(cf_pairtoitem(cp),
926 "No value given for home_server.");
931 myhome.type = server_type;
932 home = rbtree_finddata(home_servers_byname, &myhome);
938 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
940 cf_log_err(cf_pairtoitem(cp),
941 "Unknown home_server \"%s\".", name);
945 home = rbtree_finddata(home_servers_byname, &myhome);
947 cf_log_err(cf_pairtoitem(cp),
948 "Internal error %d adding home server \"%s\".",
958 static int server_pool_add(realm_config_t *rc,
959 CONF_SECTION *cs, int server_type, int do_print)
962 home_pool_t *pool = NULL;
965 int num_home_servers;
968 name2 = cf_section_name1(cs);
969 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
970 (strcasecmp(name2, "home_server_pool") != 0))) {
971 cf_log_err(cf_sectiontoitem(cs),
972 "Section is not a home_server_pool.");
976 name2 = cf_section_name2(cs);
978 cf_log_err(cf_sectiontoitem(cs),
979 "Server pool section is missing a name.");
984 * Count the home servers and initalize them.
986 num_home_servers = 0;
987 for (cp = cf_pair_find(cs, "home_server");
989 cp = cf_pair_find_next(cs, cp, "home_server")) {
992 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
993 server_type, &home)) {
998 if (num_home_servers == 0) {
999 cf_log_err(cf_sectiontoitem(cs),
1000 "No home servers defined in pool %s",
1005 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
1011 * Fallback servers must be defined, and must be
1014 cp = cf_pair_find(cs, "fallback");
1017 if (server_type == HOME_TYPE_COA) {
1018 cf_log_err(cf_sectiontoitem(cs), "Home server pools of type \"coa\" cannot have a fallback virtual server.");
1023 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
1024 server_type, &pool->fallback)) {
1029 if (!pool->fallback->server) {
1030 cf_log_err(cf_sectiontoitem(cs), "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
1035 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
1037 cp = cf_pair_find(cs, "type");
1039 static FR_NAME_NUMBER pool_types[] = {
1040 { "load-balance", HOME_POOL_LOAD_BALANCE },
1041 { "fail-over", HOME_POOL_FAIL_OVER },
1042 { "round_robin", HOME_POOL_LOAD_BALANCE },
1043 { "fail_over", HOME_POOL_FAIL_OVER },
1044 { "client-balance", HOME_POOL_CLIENT_BALANCE },
1045 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
1046 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
1050 value = cf_pair_value(cp);
1052 cf_log_err(cf_pairtoitem(cp),
1053 "No value given for type.");
1057 pool->type = fr_str2int(pool_types, value, 0);
1059 cf_log_err(cf_pairtoitem(cp),
1060 "Unknown type \"%s\".",
1065 if (do_print) cf_log_info(cs, "\ttype = %s", value);
1068 cp = cf_pair_find(cs, "virtual_server");
1070 pool->virtual_server = cf_pair_value(cp);
1071 if (!pool->virtual_server) {
1072 cf_log_err(cf_pairtoitem(cp), "No value given for virtual_server");
1077 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
1080 if (!cf_section_sub_find_name2(rc->cs, "server",
1081 pool->virtual_server)) {
1082 cf_log_err(cf_pairtoitem(cp), "No such server %s",
1083 pool->virtual_server);
1089 num_home_servers = 0;
1090 for (cp = cf_pair_find(cs, "home_server");
1092 cp = cf_pair_find_next(cs, cp, "home_server")) {
1095 value = cf_pair_value(cp);
1097 memset(&myhome, 0, sizeof(myhome));
1098 myhome.name = value;
1099 myhome.type = server_type;
1101 home = rbtree_finddata(home_servers_byname, &myhome);
1103 DEBUG2("Internal sanity check failed");
1108 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
1112 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1113 pool->servers[num_home_servers++] = home;
1114 } /* loop over home_server's */
1116 if (pool->fallback && do_print) {
1117 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1120 if (!rbtree_insert(home_pools_byname, pool)) {
1121 rad_assert("Internal sanity check failed");
1125 if (do_print) cf_log_info(cs, " }");
1127 cf_data_add(cs, "home_server_pool", pool, free);
1129 rad_assert(pool->server_type != 0);
1134 if (do_print) cf_log_info(cs, " }");
1140 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1142 const char *name, const char *secret,
1143 home_pool_type_t ldflag, home_pool_t **pool_p,
1144 int type, const char *server)
1147 int i, insert_point, num_home_servers;
1148 home_server myhome, *home;
1149 home_pool_t mypool, *pool;
1150 CONF_SECTION *subcs;
1152 rc = rc; /* -Wunused */
1161 * LOCAL realms get sanity checked, and nothing else happens.
1163 if (strcmp(name, "LOCAL") == 0) {
1165 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
1172 return 0; /* Not proxying. Can't do non-LOCAL realms */
1175 mypool.name = realm;
1176 mypool.server_type = type;
1177 pool = rbtree_finddata(home_pools_byname, &mypool);
1179 if (pool->type != ldflag) {
1180 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
1184 if (pool->server_type != type) {
1185 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
1192 home = rbtree_finddata(home_servers_byname, &myhome);
1194 if (secret && (strcmp(home->secret, secret) != 0)) {
1195 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
1199 if (home->type != type) {
1200 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
1205 * See if the home server is already listed
1206 * in the pool. If so, do nothing else.
1208 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1209 if (pool->servers[i] == home) {
1216 * If we do have a pool, check that there is room to
1217 * insert the home server we've found, or the one that we
1220 * Note that we insert it into the LAST available
1221 * position, in order to maintain the same order as in
1222 * the configuration files.
1226 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1227 if (pool->servers[i]) break;
1229 if (!pool->servers[i]) {
1234 if (insert_point < 0) {
1235 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);
1241 * No home server, allocate one.
1247 home = rad_malloc(sizeof(*home));
1248 memset(home, 0, sizeof(*home));
1251 home->hostname = name;
1253 home->secret = secret;
1255 home->proto = IPPROTO_UDP;
1257 p = strchr(name, ':');
1259 if (type == HOME_TYPE_AUTH) {
1260 home->port = PW_AUTH_UDP_PORT;
1262 home->port = PW_ACCT_UDP_PORT;
1268 } else if (p == name) {
1269 cf_log_err(cf_sectiontoitem(cs),
1270 "Invalid hostname %s.",
1276 home->port = atoi(p + 1);
1277 if ((home->port == 0) || (home->port > 65535)) {
1278 cf_log_err(cf_sectiontoitem(cs),
1285 q = rad_malloc((p - name) + 1);
1286 memcpy(q, name, (p - name));
1292 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1293 cf_log_err(cf_sectiontoitem(cs),
1294 "Failed looking up hostname %s.",
1300 home->src_ipaddr.af = home->ipaddr.af;
1302 home->ipaddr.af = AF_UNSPEC;
1303 home->server = server;
1308 * Use the old-style configuration.
1310 home->max_outstanding = 65535*16;
1311 home->zombie_period = rc->retry_delay * rc->retry_count;
1312 if (home->zombie_period == 0) home->zombie_period =30;
1313 home->response_window = home->zombie_period - 1;
1315 home->ping_check = HOME_PING_CHECK_NONE;
1317 home->revive_interval = rc->dead_time;
1319 if (rbtree_finddata(home_servers_byaddr, home)) {
1320 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
1325 if (!rbtree_insert(home_servers_byname, home)) {
1326 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1331 if (!rbtree_insert(home_servers_byaddr, home)) {
1332 rbtree_deletebydata(home_servers_byname, home);
1333 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1339 home->number = home_server_max_number++;
1340 if (!rbtree_insert(home_servers_bynumber, home)) {
1341 rbtree_deletebydata(home_servers_byname, home);
1342 if (home->ipaddr.af != AF_UNSPEC) {
1343 rbtree_deletebydata(home_servers_byname, home);
1345 cf_log_err(cf_sectiontoitem(cs),
1346 "Internal error %d adding home server %s.",
1355 * We now have a home server, see if we can insert it
1356 * into pre-existing pool.
1358 if (insert_point >= 0) {
1359 rad_assert(pool != NULL);
1360 pool->servers[insert_point] = home;
1364 rad_assert(pool == NULL);
1365 rad_assert(home != NULL);
1368 * Count the old-style realms of this name.
1370 num_home_servers = 0;
1371 for (subcs = cf_section_find_next(cs, NULL, "realm");
1373 subcs = cf_section_find_next(cs, subcs, "realm")) {
1374 const char *this = cf_section_name2(subcs);
1376 if (!this || (strcmp(this, realm) != 0)) continue;
1380 if (num_home_servers == 0) {
1381 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
1386 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1389 pool->servers[0] = home;
1391 if (!rbtree_insert(home_pools_byname, pool)) {
1392 rad_assert("Internal sanity check failed");
1402 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1405 const char *secret = NULL;
1406 home_pool_type_t ldflag;
1409 cp = cf_pair_find(cs, "ldflag");
1410 ldflag = HOME_POOL_FAIL_OVER;
1412 host = cf_pair_value(cp);
1414 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
1418 if (strcasecmp(host, "fail_over") == 0) {
1419 cf_log_info(cs, "\tldflag = fail_over");
1421 } else if (strcasecmp(host, "round_robin") == 0) {
1422 ldflag = HOME_POOL_LOAD_BALANCE;
1423 cf_log_info(cs, "\tldflag = round_robin");
1426 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
1429 } /* else don't print it. */
1432 * Allow old-style if it doesn't exist, or if it exists and
1435 cp = cf_pair_find(cs, "authhost");
1437 host = cf_pair_value(cp);
1439 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
1443 if (strcmp(host, "LOCAL") != 0) {
1444 cp = cf_pair_find(cs, "secret");
1446 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1450 secret = cf_pair_value(cp);
1452 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1457 cf_log_info(cs, "\tauthhost = %s", host);
1459 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1460 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1465 cp = cf_pair_find(cs, "accthost");
1467 host = cf_pair_value(cp);
1469 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1474 * Don't look for a secret again if it was found
1477 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1478 cp = cf_pair_find(cs, "secret");
1480 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1484 secret = cf_pair_value(cp);
1486 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1491 cf_log_info(cs, "\taccthost = %s", host);
1493 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1494 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1499 cp = cf_pair_find(cs, "virtual_server");
1501 host = cf_pair_value(cp);
1503 cf_log_err(cf_pairtoitem(cp), "No value specified for virtual_server");
1507 cf_log_info(cs, "\tvirtual_server = %s", host);
1509 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1510 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1513 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1514 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1519 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1527 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1528 const char *name, home_pool_t **dest,
1529 int server_type, int do_print)
1531 home_pool_t mypool, *pool;
1534 mypool.server_type = server_type;
1536 pool = rbtree_finddata(home_pools_byname, &mypool);
1538 CONF_SECTION *pool_cs;
1540 pool_cs = cf_section_sub_find_name2(rc->cs,
1544 pool_cs = cf_section_sub_find_name2(rc->cs,
1549 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1553 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1557 pool = rbtree_finddata(home_pools_byname, &mypool);
1559 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1564 if (pool->server_type != server_type) {
1565 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1576 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1582 home_pool_t *auth_pool, *acct_pool;
1583 const char *auth_pool_name, *acct_pool_name;
1586 name2 = cf_section_name1(cs);
1587 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1588 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1592 name2 = cf_section_name2(cs);
1594 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1599 auth_pool = acct_pool = NULL;
1600 auth_pool_name = acct_pool_name = NULL;
1603 * Prefer new configuration to old one.
1605 cp = cf_pair_find(cs, "pool");
1606 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1607 if (cp) auth_pool_name = cf_pair_value(cp);
1608 if (cp && auth_pool_name) {
1609 acct_pool_name = auth_pool_name;
1610 if (!add_pool_to_realm(rc, cs,
1611 auth_pool_name, &auth_pool,
1612 HOME_TYPE_AUTH, 1)) {
1615 if (!add_pool_to_realm(rc, cs,
1616 auth_pool_name, &acct_pool,
1617 HOME_TYPE_ACCT, 0)) {
1622 cp = cf_pair_find(cs, "auth_pool");
1623 if (cp) auth_pool_name = cf_pair_value(cp);
1624 if (cp && auth_pool_name) {
1626 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1629 if (!add_pool_to_realm(rc, cs,
1630 auth_pool_name, &auth_pool,
1631 HOME_TYPE_AUTH, 1)) {
1636 cp = cf_pair_find(cs, "acct_pool");
1637 if (cp) acct_pool_name = cf_pair_value(cp);
1638 if (cp && acct_pool_name) {
1639 int do_print = TRUE;
1642 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1648 (strcmp(auth_pool_name, acct_pool_name) != 0))) {
1652 if (!add_pool_to_realm(rc, cs,
1653 acct_pool_name, &acct_pool,
1654 HOME_TYPE_ACCT, do_print)) {
1660 cf_log_info(cs, " realm %s {", name2);
1664 * The realm MAY already exist if it's an old-style realm.
1665 * In that case, merge the old-style realm with this one.
1667 r = realm_find2(name2);
1668 if (r && (strcmp(r->name, name2) == 0)) {
1669 if (cf_pair_find(cs, "auth_pool") ||
1670 cf_pair_find(cs, "acct_pool")) {
1671 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1675 if (!old_realm_config(rc, cs, r)) {
1679 cf_log_info(cs, " } # realm %s", name2);
1685 if (name2[0] == '~') {
1690 * Include substring matches.
1692 rcode = regcomp(®, name2 + 1,
1693 REG_EXTENDED | REG_NOSUB | REG_ICASE);
1697 regerror(rcode, ®, buffer, sizeof(buffer));
1699 cf_log_err(cf_sectiontoitem(cs),
1700 "Invalid regex \"%s\": %s",
1708 r = rad_malloc(sizeof(*r));
1709 memset(r, 0, sizeof(*r));
1714 r->auth_pool = auth_pool;
1715 r->acct_pool = acct_pool;
1717 if (auth_pool_name &&
1718 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1719 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1721 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1722 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1726 cp = cf_pair_find(cs, "nostrip");
1727 if (cp && (cf_pair_value(cp) == NULL)) {
1729 cf_log_info(cs, "\tnostrip");
1733 * We're a new-style realm. Complain if we see the old
1736 if (r->auth_pool || r->acct_pool) {
1737 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1738 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1739 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1740 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1741 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1746 * The realm MAY be an old-style realm, as there
1747 * was no auth_pool or acct_pool. Double-check
1748 * it, just to be safe.
1750 } else if (!old_realm_config(rc, cs, r)) {
1756 * It's a regex. Add it to a separate list.
1758 if (name2[0] == '~') {
1759 realm_regex_t *rr, **last;
1761 rr = rad_malloc(sizeof(*rr));
1763 last = &realms_regex;
1764 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1772 cf_log_info(cs, " }");
1777 if (!rbtree_insert(realms_byname, r)) {
1778 rad_assert("Internal sanity check failed");
1782 cf_log_info(cs, " }");
1787 cf_log_info(cs, " } # realm %s", name2);
1793 static const FR_NAME_NUMBER home_server_types[] = {
1794 { "auth", HOME_TYPE_AUTH },
1795 { "auth+acct", HOME_TYPE_AUTH },
1796 { "acct", HOME_TYPE_ACCT },
1797 { "coa", HOME_TYPE_COA },
1801 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1804 const char *name, *type;
1806 CONF_SECTION *server_cs;
1808 cp = cf_pair_find(cs, "home_server");
1810 cf_log_err(cf_sectiontoitem(cs), "Pool does not contain a \"home_server\" entry");
1811 return HOME_TYPE_INVALID;
1814 name = cf_pair_value(cp);
1816 cf_log_err(cf_pairtoitem(cp), "home_server entry does not reference a home server");
1817 return HOME_TYPE_INVALID;
1820 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1822 cf_log_err(cf_pairtoitem(cp), "home_server \"%s\" does not exist", name);
1823 return HOME_TYPE_INVALID;
1826 cp = cf_pair_find(server_cs, "type");
1828 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s does not contain a \"type\" entry", name);
1829 return HOME_TYPE_INVALID;
1832 type = cf_pair_value(cp);
1834 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an empty \"type\" entry", name);
1835 return HOME_TYPE_INVALID;
1838 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1839 if (home == HOME_TYPE_INVALID) {
1840 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1841 return HOME_TYPE_INVALID;
1844 return home; /* 'cause we miss it so much */
1848 int realms_init(CONF_SECTION *config)
1852 CONF_SECTION *server_cs;
1854 realm_config_t *rc, *old_rc;
1856 if (realms_byname) return 1;
1858 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1859 if (!realms_byname) {
1865 home_servers_byaddr = rbtree_create(home_server_addr_cmp, home_server_free, 0);
1866 if (!home_servers_byaddr) {
1871 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1872 if (!home_servers_byname) {
1878 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1879 if (!home_servers_bynumber) {
1885 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1886 if (!home_pools_byname) {
1892 rc = rad_malloc(sizeof(*rc));
1893 memset(rc, 0, sizeof(*rc));
1897 cs = cf_subsection_find_next(config, NULL, "proxy");
1899 cf_section_parse(cs, rc, proxy_config);
1901 rc->dead_time = DEAD_TIME;
1902 rc->retry_count = RETRY_COUNT;
1903 rc->retry_delay = RETRY_DELAY;
1905 rc->wake_all_if_all_dead= 0;
1908 for (cs = cf_subsection_find_next(config, NULL, "home_server");
1910 cs = cf_subsection_find_next(config, cs, "home_server")) {
1911 if (!home_server_add(rc, cs)) {
1919 * Loop over virtual servers to find homes which are
1922 for (server_cs = cf_subsection_find_next(config, NULL, "server");
1924 server_cs = cf_subsection_find_next(config, server_cs, "server")) {
1925 for (cs = cf_subsection_find_next(server_cs, NULL, "home_server");
1927 cs = cf_subsection_find_next(server_cs, cs, "home_server")) {
1928 if (!home_server_add(rc, cs)) {
1937 for (cs = cf_subsection_find_next(config, NULL, "realm");
1939 cs = cf_subsection_find_next(config, cs, "realm")) {
1940 if (!realm_add(rc, cs)) {
1949 * CoA pools aren't tied to realms.
1951 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1953 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1957 * Pool was already loaded.
1959 if (cf_data_find(cs, "home_server_pool")) continue;
1961 type = pool_peek_type(config, cs);
1962 if (type == HOME_TYPE_INVALID) {
1968 if (!server_pool_add(rc, cs, type, TRUE)) {
1978 xlat_register("home_server", xlat_home_server, NULL);
1979 xlat_register("home_server_pool", xlat_server_pool, NULL);
1983 * Swap pointers atomically.
1985 old_rc = realm_config;
1993 * Find a realm where "name" might be the regex.
1995 REALM *realm_find2(const char *name)
2000 if (!name) name = "NULL";
2002 myrealm.name = name;
2003 realm = rbtree_finddata(realms_byname, &myrealm);
2004 if (realm) return realm;
2008 realm_regex_t *this;
2010 for (this = realms_regex; this != NULL; this = this->next) {
2011 if (strcmp(this->realm->name, name) == 0) {
2019 * Couldn't find a realm. Look for DEFAULT.
2021 myrealm.name = "DEFAULT";
2022 return rbtree_finddata(realms_byname, &myrealm);
2027 * Find a realm in the REALM list.
2029 REALM *realm_find(const char *name)
2034 if (!name) name = "NULL";
2036 myrealm.name = name;
2037 realm = rbtree_finddata(realms_byname, &myrealm);
2038 if (realm) return realm;
2042 realm_regex_t *this;
2044 for (this = realms_regex; this != NULL; this = this->next) {
2049 * Include substring matches.
2051 if (regcomp(®, this->realm->name + 1,
2052 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
2056 compare = regexec(®, name, 0, NULL, 0);
2059 if (compare == 0) return this->realm;
2065 * Couldn't find a realm. Look for DEFAULT.
2067 myrealm.name = "DEFAULT";
2068 return rbtree_finddata(realms_byname, &myrealm);
2073 home_server *home_server_ldb(const char *realmname,
2074 home_pool_t *pool, REQUEST *request)
2078 home_server *found = NULL;
2079 home_server *zombie = NULL;
2083 * Determine how to pick choose the home server.
2085 switch (pool->type) {
2089 * For load-balancing by client IP address, we
2090 * pick a home server by hashing the client IP.
2092 * This isn't as even a load distribution as
2093 * tracking the State attribute, but it's better
2096 case HOME_POOL_CLIENT_BALANCE:
2097 switch (request->packet->src_ipaddr.af) {
2099 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2100 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2103 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2104 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2110 start = hash % pool->num_home_servers;
2113 case HOME_POOL_CLIENT_PORT_BALANCE:
2114 switch (request->packet->src_ipaddr.af) {
2116 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2117 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2120 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2121 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2127 fr_hash_update(&request->packet->src_port,
2128 sizeof(request->packet->src_port), hash);
2129 start = hash % pool->num_home_servers;
2132 case HOME_POOL_KEYED_BALANCE:
2133 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY, 0)) != NULL) {
2134 hash = fr_hash(vp->vp_strvalue, vp->length);
2135 start = hash % pool->num_home_servers;
2140 case HOME_POOL_LOAD_BALANCE:
2141 case HOME_POOL_FAIL_OVER:
2145 default: /* this shouldn't happen... */
2152 * Starting with the home server we chose, loop through
2153 * all home servers. If the current one is dead, skip
2154 * it. If it is too busy, skip it.
2156 * Otherwise, use it.
2158 for (count = 0; count < pool->num_home_servers; count++) {
2159 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
2161 if (!home) continue;
2164 * Skip dead home servers.
2166 if (home->state == HOME_STATE_IS_DEAD) {
2171 * This home server is too busy. Choose another one.
2173 if (home->currently_outstanding >= home->max_outstanding) {
2179 * We read the packet from a detail file, AND it
2180 * came from this server. Don't re-proxy it
2183 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2184 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
2185 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2191 * Default virtual: ignore homes tied to a
2194 if (!request->server && home->parent_server) {
2199 * A virtual AND home is tied to virtual,
2200 * ignore ones which don't match.
2202 if (request->server && home->parent_server &&
2203 strcmp(request->server, home->parent_server) != 0) {
2208 * Allow request->server && !home->parent_server
2210 * i.e. virtuals can proxy to globally defined
2215 * It's zombie, so we remember the first zombie
2216 * we find, but we don't mark it as a "live"
2219 if (home->state == HOME_STATE_ZOMBIE) {
2220 if (!zombie) zombie = home;
2225 * We've found the first "live" one. Use that.
2227 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2233 * Otherwise we're doing some kind of load balancing.
2234 * If we haven't found one yet, pick this one.
2241 RDEBUG3("PROXY %s %d\t%s %d",
2242 found->name, found->currently_outstanding,
2243 home->name, home->currently_outstanding);
2246 * Prefer this server if it's less busy than the
2247 * one we had previously found.
2249 if (home->currently_outstanding < found->currently_outstanding) {
2250 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2251 home->name, found->name);
2257 * Ignore servers which are busier than the one
2260 if (home->currently_outstanding > found->currently_outstanding) {
2261 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2262 home->name, found->name);
2267 * From the list of servers which have the same
2268 * load, choose one at random.
2270 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2273 } /* loop over the home servers */
2276 * We have no live servers, BUT we have a zombie. Use
2277 * the zombie as a last resort.
2279 if (!found && zombie) {
2285 * There's a fallback if they're all dead.
2287 if (!found && pool->fallback) {
2288 found = pool->fallback;
2290 DEBUG("WARNING: Home server pool %s failing over to fallback %s",
2291 pool->name, found->server);
2292 if (pool->in_fallback) goto update_and_return;
2294 pool->in_fallback = TRUE;
2297 * Run the trigger once an hour saying that
2300 if ((pool->time_all_dead + 3600) < request->timestamp) {
2301 pool->time_all_dead = request->timestamp;
2302 exec_trigger(request, pool->cs, "home_server_pool.fallback");
2308 if ((found != pool->fallback) && pool->in_fallback) {
2309 pool->in_fallback = FALSE;
2310 exec_trigger(request, pool->cs, "home_server_pool.normal");
2314 * Allocate the proxy packet, only if it wasn't
2315 * already allocated by a module. This check is
2316 * mainly to support the proxying of EAP-TTLS and
2317 * EAP-PEAP tunneled requests.
2319 * In those cases, the EAP module creates a
2320 * "fake" request, and recursively passes it
2321 * through the authentication stage of the
2322 * server. The module then checks if the request
2323 * was supposed to be proxied, and if so, creates
2324 * a proxy packet from the TUNNELED request, and
2325 * not from the EAP request outside of the
2328 * The proxy then works like normal, except that
2329 * the response packet is "eaten" by the EAP
2330 * module, and encapsulated into an EAP packet.
2332 if (!request->proxy) {
2333 if ((request->proxy = rad_alloc(TRUE)) == NULL) {
2334 radlog(L_ERR|L_CONS, "no memory");
2339 * Copy the request, then look up name
2340 * and plain-text password in the copy.
2342 * Note that the User-Name attribute is
2343 * the *original* as sent over by the
2344 * client. The Stripped-User-Name
2345 * attribute is the one hacked through
2348 request->proxy->vps = paircopy(request->packet->vps);
2352 * Update the various fields as appropriate.
2354 request->proxy->src_ipaddr = found->src_ipaddr;
2355 request->proxy->src_port = 0;
2356 request->proxy->dst_ipaddr = found->ipaddr;
2357 request->proxy->dst_port = found->port;
2358 request->home_server = found;
2361 * We're supposed to add a Message-Authenticator
2362 * if it doesn't exist, and it doesn't exist.
2364 if (found->message_authenticator &&
2365 (request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2366 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0)) {
2367 radius_pairmake(request, &request->proxy->vps,
2368 "Message-Authenticator", "0x00",
2376 * No live match found, and no fallback to the "DEFAULT"
2377 * realm. We fix this by blindly marking all servers as
2378 * "live". But only do it for ones that don't support
2379 * "pings", as they will be marked live when they
2380 * actually are live.
2382 if (!realm_config->fallback &&
2383 realm_config->wake_all_if_all_dead) {
2384 for (count = 0; count < pool->num_home_servers; count++) {
2385 home_server *home = pool->servers[count];
2387 if (!home) continue;
2389 if ((home->state == HOME_STATE_IS_DEAD) &&
2390 (home->ping_check == HOME_PING_CHECK_NONE)) {
2391 home->state = HOME_STATE_ALIVE;
2392 if (!found) found = home;
2396 if (found) goto update_and_return;
2400 * Still nothing. Look up the DEFAULT realm, but only
2401 * if we weren't looking up the NULL or DEFAULT realms.
2403 if (realm_config->fallback &&
2405 (strcmp(realmname, "NULL") != 0) &&
2406 (strcmp(realmname, "DEFAULT") != 0)) {
2407 REALM *rd = realm_find("DEFAULT");
2409 if (!rd) return NULL;
2412 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2413 pool = rd->auth_pool;
2415 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2416 pool = rd->acct_pool;
2418 if (!pool) return NULL;
2420 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2421 return home_server_ldb(rd->name, pool, request);
2425 * Still haven't found anything. Oh well.
2431 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port, int proto)
2435 memset(&myhome, 0, sizeof(myhome));
2436 myhome.ipaddr = *ipaddr;
2437 myhome.src_ipaddr.af = ipaddr->af;
2440 myhome.proto = proto;
2442 myhome.proto = IPPROTO_UDP;
2444 myhome.server = NULL; /* we're not called for internal proxying */
2446 return rbtree_finddata(home_servers_byaddr, &myhome);
2450 home_server *home_server_byname(const char *name, int type)
2454 memset(&myhome, 0, sizeof(myhome));
2458 return rbtree_finddata(home_servers_byname, &myhome);
2463 home_server *home_server_bynumber(int number)
2467 memset(&myhome, 0, sizeof(myhome));
2468 myhome.number = number;
2469 myhome.server = NULL; /* we're not called for internal proxying */
2471 return rbtree_finddata(home_servers_bynumber, &myhome);
2475 home_pool_t *home_pool_byname(const char *name, int type)
2479 memset(&mypool, 0, sizeof(mypool));
2481 mypool.server_type = type;
2482 return rbtree_finddata(home_pools_byname, &mypool);