2 * realms.c Realm handling code
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2007 The FreeRADIUS server project
21 * Copyright 2007 Alan DeKok <aland@deployingradius.com>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/rad_assert.h>
39 * For POSIX Regular expressions.
40 * (0) Means no extended regular expressions.
41 * REG_EXTENDED means use extended regular expressions.
44 #define REG_EXTENDED (0)
56 static rbtree_t *realms_byname = NULL;
59 typedef struct realm_regex_t {
61 struct realm_regex_t *next;
64 static realm_regex_t *realms_regex = NULL;
66 #endif /* HAVE_REGEX_H */
68 typedef struct realm_config_t {
74 int wake_all_if_all_dead;
77 static realm_config_t *realm_config = NULL;
80 static rbtree_t *home_servers_byaddr = NULL;
81 static rbtree_t *home_servers_byname = NULL;
83 static int home_server_max_number = 0;
84 static rbtree_t *home_servers_bynumber = NULL;
87 static rbtree_t *home_pools_byname = NULL;
90 * Map the proxy server configuration parameters to variables.
92 static const CONF_PARSER proxy_config[] = {
93 { "retry_delay", PW_TYPE_INTEGER,
94 offsetof(realm_config_t, retry_delay),
95 NULL, Stringify(RETRY_DELAY) },
97 { "retry_count", PW_TYPE_INTEGER,
98 offsetof(realm_config_t, retry_count),
99 NULL, Stringify(RETRY_COUNT) },
101 { "default_fallback", PW_TYPE_BOOLEAN,
102 offsetof(realm_config_t, fallback),
105 { "dead_time", PW_TYPE_INTEGER,
106 offsetof(realm_config_t, dead_time),
107 NULL, Stringify(DEAD_TIME) },
109 { "wake_all_if_all_dead", PW_TYPE_BOOLEAN,
110 offsetof(realm_config_t, wake_all_if_all_dead),
113 { NULL, -1, 0, NULL, NULL }
117 static int realm_name_cmp(const void *one, const void *two)
119 const REALM *a = one;
120 const REALM *b = two;
122 return strcasecmp(a->name, b->name);
127 static int home_server_name_cmp(const void *one, const void *two)
129 const home_server *a = one;
130 const home_server *b = two;
132 if (a->type < b->type) return -1;
133 if (a->type > b->type) return +1;
135 return strcasecmp(a->name, b->name);
138 static int home_server_addr_cmp(const void *one, const void *two)
140 const home_server *a = one;
141 const home_server *b = two;
143 if (a->server && !b->server) return -1;
144 if (!a->server && b->server) return +1;
145 if (a->server && b->server) {
146 int rcode = a->type - b->type;
147 if (rcode != 0) return rcode;
148 return strcmp(a->server, b->server);
151 if (a->port < b->port) return -1;
152 if (a->port > b->port) return +1;
154 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
158 static int home_server_number_cmp(const void *one, const void *two)
160 const home_server *a = one;
161 const home_server *b = two;
163 return (a->number - b->number);
167 static int home_pool_name_cmp(const void *one, const void *two)
169 const home_pool_t *a = one;
170 const home_pool_t *b = two;
172 if (a->server_type < b->server_type) return -1;
173 if (a->server_type > b->server_type) return +1;
175 return strcasecmp(a->name, b->name);
180 * Xlat for %{home_server:foo}
182 static size_t xlat_home_server(UNUSED void *instance, REQUEST *request,
183 char *fmt, char *out, size_t outlen,
184 UNUSED RADIUS_ESCAPE_STRING func)
186 const char *value = NULL;
189 if (!fmt || !out || (outlen < 1)) return 0;
191 if (!request || !request->home_server) {
196 cp = cf_pair_find(request->home_server->cs, fmt);
197 if (!cp || !(value = cf_pair_value(cp))) {
202 strlcpy(out, value, outlen);
209 * Xlat for %{home_server_pool:foo}
211 static size_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
212 char *fmt, char *out, size_t outlen,
213 UNUSED RADIUS_ESCAPE_STRING func)
215 const char *value = NULL;
218 if (!fmt || !out || (outlen < 1)) return 0;
220 if (!request || !request->home_pool) {
225 cp = cf_pair_find(request->home_pool->cs, fmt);
226 if (!cp || !(value = cf_pair_value(cp))) {
231 strlcpy(out, value, outlen);
237 void realms_free(void)
241 rbtree_free(home_servers_bynumber);
242 home_servers_bynumber = NULL;
245 rbtree_free(home_servers_byname);
246 home_servers_byname = NULL;
248 rbtree_free(home_servers_byaddr);
249 home_servers_byaddr = NULL;
251 rbtree_free(home_pools_byname);
252 home_pools_byname = NULL;
255 rbtree_free(realms_byname);
256 realms_byname = NULL;
260 realm_regex_t *this, *next;
262 for (this = realms_regex; this != NULL; this = next) {
277 static struct in_addr hs_ip4addr;
278 static struct in6_addr hs_ip6addr;
279 static char *hs_srcipaddr = NULL;
280 static char *hs_type = NULL;
281 static char *hs_check = NULL;
282 static char *hs_virtual_server = NULL;
284 static CONF_PARSER home_server_config[] = {
285 { "ipaddr", PW_TYPE_IPADDR,
286 0, &hs_ip4addr, NULL },
287 { "ipv6addr", PW_TYPE_IPV6ADDR,
288 0, &hs_ip6addr, NULL },
289 { "virtual_server", PW_TYPE_STRING_PTR,
290 0, &hs_virtual_server, NULL },
292 { "port", PW_TYPE_INTEGER,
293 offsetof(home_server,port), NULL, "0" },
295 { "type", PW_TYPE_STRING_PTR,
298 { "secret", PW_TYPE_STRING_PTR,
299 offsetof(home_server,secret), NULL, NULL},
301 { "src_ipaddr", PW_TYPE_STRING_PTR,
302 0, &hs_srcipaddr, NULL },
304 { "response_window", PW_TYPE_INTEGER,
305 offsetof(home_server,response_window), NULL, "30" },
306 { "no_response_fail", PW_TYPE_BOOLEAN,
307 offsetof(home_server,no_response_fail), NULL, NULL },
308 { "max_outstanding", PW_TYPE_INTEGER,
309 offsetof(home_server,max_outstanding), NULL, "65536" },
310 { "require_message_authenticator", PW_TYPE_BOOLEAN,
311 offsetof(home_server, message_authenticator), 0, NULL },
313 { "zombie_period", PW_TYPE_INTEGER,
314 offsetof(home_server,zombie_period), NULL, "40" },
315 { "status_check", PW_TYPE_STRING_PTR,
316 0, &hs_check, "none" },
317 { "ping_check", PW_TYPE_STRING_PTR,
318 0, &hs_check, NULL },
320 { "ping_interval", PW_TYPE_INTEGER,
321 offsetof(home_server,ping_interval), NULL, "30" },
322 { "check_interval", PW_TYPE_INTEGER,
323 offsetof(home_server,ping_interval), NULL, "30" },
324 { "num_answers_to_alive", PW_TYPE_INTEGER,
325 offsetof(home_server,num_pings_to_alive), NULL, "3" },
326 { "num_pings_to_alive", PW_TYPE_INTEGER,
327 offsetof(home_server,num_pings_to_alive), NULL, "3" },
328 { "revive_interval", PW_TYPE_INTEGER,
329 offsetof(home_server,revive_interval), NULL, "300" },
330 { "status_check_timeout", PW_TYPE_INTEGER,
331 offsetof(home_server,ping_timeout), NULL, "4" },
333 { "username", PW_TYPE_STRING_PTR,
334 offsetof(home_server,ping_user_name), NULL, NULL},
335 { "password", PW_TYPE_STRING_PTR,
336 offsetof(home_server,ping_user_password), NULL, NULL},
339 { "historic_average_window", PW_TYPE_INTEGER,
340 offsetof(home_server,ema.window), NULL, NULL },
344 { "irt", PW_TYPE_INTEGER,
345 offsetof(home_server, coa_irt), 0, Stringify(2) },
346 { "mrt", PW_TYPE_INTEGER,
347 offsetof(home_server, coa_mrt), 0, Stringify(16) },
348 { "mrc", PW_TYPE_INTEGER,
349 offsetof(home_server, coa_mrc), 0, Stringify(5) },
350 { "mrd", PW_TYPE_INTEGER,
351 offsetof(home_server, coa_mrd), 0, Stringify(30) },
354 { NULL, -1, 0, NULL, NULL } /* end the list */
358 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs, int pool_type)
365 free(hs_virtual_server); /* used only for printing during parsing */
366 hs_virtual_server = NULL;
368 name2 = cf_section_name1(cs);
369 if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
370 cf_log_err(cf_sectiontoitem(cs),
371 "Section is not a home_server.");
375 name2 = cf_section_name2(cs);
377 cf_log_err(cf_sectiontoitem(cs),
378 "Home server section is missing a name.");
382 home = rad_malloc(sizeof(*home));
383 memset(home, 0, sizeof(*home));
388 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
389 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
390 if (cf_section_parse(cs, home, home_server_config) < 0) {
396 * Figure out which one to use.
398 if (cf_pair_find(cs, "ipaddr")) {
399 home->ipaddr.af = AF_INET;
400 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
402 } else if (cf_pair_find(cs, "ipv6addr")) {
403 home->ipaddr.af = AF_INET6;
404 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
406 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
407 home->ipaddr.af = AF_UNSPEC;
408 home->server = cf_pair_value(cp);
410 cf_log_err(cf_sectiontoitem(cs),
411 "Invalid value for virtual_server");
415 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
417 cf_log_err(cf_sectiontoitem(cs),
418 "No such server %s", home->server);
423 * When CoA is used, the user has to specify the type
424 * of the home server, even when they point to
427 home->secret = strdup("");
431 cf_log_err(cf_sectiontoitem(cs),
432 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
445 if (!home->port || (home->port > 65535)) {
446 cf_log_err(cf_sectiontoitem(cs),
447 "No port, or invalid port defined for home server %s.",
453 cf_log_err(cf_sectiontoitem(cs),
454 "Fatal error! Home server %s is ourselves!",
460 cf_log_err(cf_sectiontoitem(cs),
461 "No shared secret defined for home server %s.",
467 * Use a reasonable default.
470 if (!hs_type) hs_type = strdup("auth+acct");
472 if (strcasecmp(hs_type, "auth") == 0) {
473 home->type = HOME_TYPE_AUTH;
474 if (pool_type != home->type) {
476 cf_log_err(cf_sectiontoitem(cs),
477 "Server pool cannot include home server %s of type \"%s\"",
482 } else if (strcasecmp(hs_type, "acct") == 0) {
483 home->type = HOME_TYPE_ACCT;
484 if (pool_type != home->type) goto mismatch;
486 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
487 home->type = HOME_TYPE_AUTH;
491 } else if (strcasecmp(hs_type, "coa") == 0) {
492 home->type = HOME_TYPE_COA;
495 if (pool_type != home->type) goto mismatch;
497 if (home->server != NULL) {
498 cf_log_err(cf_sectiontoitem(cs),
499 "Home servers of type \"coa\" cannot point to a virtual server");
505 cf_log_err(cf_sectiontoitem(cs),
506 "Invalid type \"%s\" for home server %s.",
513 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
514 home->ping_check = HOME_PING_CHECK_NONE;
516 } else if (strcasecmp(hs_check, "status-server") == 0) {
517 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
519 } else if (strcasecmp(hs_check, "request") == 0) {
520 home->ping_check = HOME_PING_CHECK_REQUEST;
523 cf_log_err(cf_sectiontoitem(cs),
524 "Invalid ping_check \"%s\" for home server %s.",
531 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
532 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
533 if (!home->ping_user_name) {
534 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable ping checks");
538 if ((home->type == HOME_TYPE_AUTH) &&
539 !home->ping_user_password) {
540 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable ping checks");
545 if ((home->ipaddr.af != AF_UNSPEC) && /* could be virtual server */
546 rbtree_finddata(home_servers_byaddr, home)) {
547 cf_log_err(cf_sectiontoitem(cs), "Duplicate home server");
552 * Look up the name using the *same* address family as
553 * for the home server.
555 if (hs_srcipaddr && (home->ipaddr.af != AF_UNSPEC)) {
556 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
557 cf_log_err(cf_sectiontoitem(cs), "Failed parsing src_ipaddr");
564 if (!rbtree_insert(home_servers_byname, home)) {
565 cf_log_err(cf_sectiontoitem(cs),
566 "Internal error %d adding home server %s.",
571 if ((home->ipaddr.af != AF_UNSPEC) && /* could be virtual server */
572 !rbtree_insert(home_servers_byaddr, home)) {
573 rbtree_deletebydata(home_servers_byname, home);
574 cf_log_err(cf_sectiontoitem(cs),
575 "Internal error %d adding home server %s.",
581 home->number = home_server_max_number++;
582 if (!rbtree_insert(home_servers_bynumber, home)) {
583 rbtree_deletebydata(home_servers_byname, home);
584 if (home->ipaddr.af != AF_UNSPEC) {
585 rbtree_deletebydata(home_servers_byname, home);
587 cf_log_err(cf_sectiontoitem(cs),
588 "Internal error %d adding home server %s.",
594 if (home->max_outstanding < 8) home->max_outstanding = 8;
595 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
597 if (home->ping_interval < 6) home->ping_interval = 6;
598 if (home->ping_interval > 120) home->ping_interval = 120;
600 if (home->response_window < 1) home->response_window = 1;
601 if (home->response_window > 60) home->response_window = 60;
603 if (home->zombie_period < 1) home->zombie_period = 1;
604 if (home->zombie_period > 120) home->zombie_period = 120;
606 if (home->zombie_period < home->response_window) {
607 home->zombie_period = home->response_window;
610 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
611 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
613 if (home->ping_timeout < 3) home->ping_timeout = 3;
614 if (home->ping_timeout > 10) home->ping_timeout = 10;
616 if (home->revive_interval < 60) home->revive_interval = 60;
617 if (home->revive_interval > 3600) home->revive_interval = 3600;
620 if (home->coa_irt < 1) home->coa_irt = 1;
621 if (home->coa_irt > 5) home->coa_irt = 5;
623 if (home->coa_mrc < 0) home->coa_mrc = 0;
624 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
626 if (home->coa_mrt < 0) home->coa_mrt = 0;
627 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
629 if (home->coa_mrd < 5) home->coa_mrd = 5;
630 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
634 home_server *home2 = rad_malloc(sizeof(*home2));
636 memcpy(home2, home, sizeof(*home2));
638 home2->type = HOME_TYPE_ACCT;
640 home2->ping_user_password = NULL;
643 if (!rbtree_insert(home_servers_byname, home2)) {
644 cf_log_err(cf_sectiontoitem(cs),
645 "Internal error %d adding home server %s.",
651 if ((home->ipaddr.af != AF_UNSPEC) &&
652 !rbtree_insert(home_servers_byaddr, home2)) {
653 rbtree_deletebydata(home_servers_byname, home2);
654 cf_log_err(cf_sectiontoitem(cs),
655 "Internal error %d adding home server %s.",
662 home2->number = home_server_max_number++;
663 if (!rbtree_insert(home_servers_bynumber, home2)) {
664 rbtree_deletebydata(home_servers_byname, home2);
665 if (home2->ipaddr.af != AF_UNSPEC) {
666 rbtree_deletebydata(home_servers_byname, home2);
668 cf_log_err(cf_sectiontoitem(cs),
669 "Internal error %d adding home server %s.",
681 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
682 int server_type, int num_home_servers)
686 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
688 if (!pool) return NULL; /* just for pairanoia */
690 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
695 pool->server_type = server_type;
696 pool->num_home_servers = num_home_servers;
701 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
702 const char *name, int server_type,
705 home_server myhome, *home;
706 CONF_SECTION *server_cs;
709 cf_log_err(cf_pairtoitem(cp),
710 "No value given for home_server.");
715 myhome.type = server_type;
716 home = rbtree_finddata(home_servers_byname, &myhome);
722 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
724 cf_log_err(cf_pairtoitem(cp),
725 "Unknown home_server \"%s\".", name);
729 if (!home_server_add(rc, server_cs, server_type)) {
733 home = rbtree_finddata(home_servers_byname, &myhome);
743 static int server_pool_add(realm_config_t *rc,
744 CONF_SECTION *cs, int server_type, int do_print)
747 home_pool_t *pool = NULL;
750 int num_home_servers;
753 name2 = cf_section_name1(cs);
754 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
755 (strcasecmp(name2, "home_server_pool") != 0))) {
756 cf_log_err(cf_sectiontoitem(cs),
757 "Section is not a home_server_pool.");
761 name2 = cf_section_name2(cs);
763 cf_log_err(cf_sectiontoitem(cs),
764 "Server pool section is missing a name.");
769 * Count the home servers and initalize them.
771 num_home_servers = 0;
772 for (cp = cf_pair_find(cs, "home_server");
774 cp = cf_pair_find_next(cs, cp, "home_server")) {
777 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
778 server_type, &home)) {
784 if (num_home_servers == 0) {
785 cf_log_err(cf_sectiontoitem(cs),
786 "No home servers defined in pool %s",
791 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
797 * Fallback servers must be defined, and must be
800 cp = cf_pair_find(cs, "fallback");
803 if (server_type == HOME_TYPE_COA) {
804 cf_log_err(cf_sectiontoitem(cs), "Home server pools of type \"coa\" cannot have a fallback virtual server.");
809 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
810 server_type, &pool->fallback)) {
815 if (!pool->fallback->server) {
816 cf_log_err(cf_sectiontoitem(cs), "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
821 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
823 cp = cf_pair_find(cs, "type");
825 static FR_NAME_NUMBER pool_types[] = {
826 { "load-balance", HOME_POOL_LOAD_BALANCE },
827 { "fail-over", HOME_POOL_FAIL_OVER },
828 { "round_robin", HOME_POOL_LOAD_BALANCE },
829 { "fail_over", HOME_POOL_FAIL_OVER },
830 { "client-balance", HOME_POOL_CLIENT_BALANCE },
831 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
832 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
836 value = cf_pair_value(cp);
838 cf_log_err(cf_pairtoitem(cp),
839 "No value given for type.");
843 pool->type = fr_str2int(pool_types, value, 0);
845 cf_log_err(cf_pairtoitem(cp),
846 "Unknown type \"%s\".",
851 if (do_print) cf_log_info(cs, "\ttype = %s", value);
854 cp = cf_pair_find(cs, "virtual_server");
856 pool->virtual_server = cf_pair_value(cp);
857 if (!pool->virtual_server) {
858 cf_log_err(cf_pairtoitem(cp), "No value given for virtual_server");
863 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
866 if (!cf_section_sub_find_name2(rc->cs, "server",
867 pool->virtual_server)) {
868 cf_log_err(cf_pairtoitem(cp), "No such server %s",
869 pool->virtual_server);
875 num_home_servers = 0;
876 for (cp = cf_pair_find(cs, "home_server");
878 cp = cf_pair_find_next(cs, cp, "home_server")) {
881 value = cf_pair_value(cp);
883 memset(&myhome, 0, sizeof(&myhome));
885 myhome.type = server_type;
887 home = rbtree_finddata(home_servers_byname, &myhome);
889 DEBUG2("Internal sanity check failed");
894 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
898 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
899 pool->servers[num_home_servers++] = home;
900 } /* loop over home_server's */
902 if (pool->fallback && do_print) {
903 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
906 if (!rbtree_insert(home_pools_byname, pool)) {
907 rad_assert("Internal sanity check failed");
911 if (do_print) cf_log_info(cs, " }");
913 cf_data_add(cs, "home_server_pool", pool, free);
915 rad_assert(pool->server_type != 0);
920 if (do_print) cf_log_info(cs, " }");
926 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
928 const char *name, const char *secret,
929 home_pool_type_t ldflag, home_pool_t **pool_p,
930 int type, const char *server)
933 int i, insert_point, num_home_servers;
934 home_server myhome, *home;
935 home_pool_t mypool, *pool;
938 rc = rc; /* -Wunused */
947 * LOCAL realms get sanity checked, and nothing else happens.
949 if (strcmp(name, "LOCAL") == 0) {
951 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
958 return 0; /* Not proxying. Can't do non-LOCAL realms */
962 mypool.server_type = type;
963 pool = rbtree_finddata(home_pools_byname, &mypool);
965 if (pool->type != ldflag) {
966 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
970 if (pool->server_type != type) {
971 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
978 home = rbtree_finddata(home_servers_byname, &myhome);
980 if (secret && (strcmp(home->secret, secret) != 0)) {
981 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
985 if (home->type != type) {
986 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
991 * See if the home server is already listed
992 * in the pool. If so, do nothing else.
994 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
995 if (pool->servers[i] == home) {
1002 * If we do have a pool, check that there is room to
1003 * insert the home server we've found, or the one that we
1006 * Note that we insert it into the LAST available
1007 * position, in order to maintain the same order as in
1008 * the configuration files.
1012 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1013 if (pool->servers[i]) break;
1015 if (!pool->servers[i]) {
1020 if (insert_point < 0) {
1021 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);
1027 * No home server, allocate one.
1033 home = rad_malloc(sizeof(*home));
1034 memset(home, 0, sizeof(*home));
1037 home->hostname = name;
1039 home->secret = secret;
1042 p = strchr(name, ':');
1044 if (type == HOME_TYPE_AUTH) {
1045 home->port = PW_AUTH_UDP_PORT;
1047 home->port = PW_ACCT_UDP_PORT;
1053 } else if (p == name) {
1054 cf_log_err(cf_sectiontoitem(cs),
1055 "Invalid hostname %s.",
1061 home->port = atoi(p + 1);
1062 if ((home->port == 0) || (home->port > 65535)) {
1063 cf_log_err(cf_sectiontoitem(cs),
1070 q = rad_malloc((p - name) + 1);
1071 memcpy(q, name, (p - name));
1077 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1078 cf_log_err(cf_sectiontoitem(cs),
1079 "Failed looking up hostname %s.",
1086 home->ipaddr.af = AF_UNSPEC;
1087 home->server = server;
1092 * Use the old-style configuration.
1094 home->max_outstanding = 65535*16;
1095 home->zombie_period = rc->retry_delay * rc->retry_count;
1096 if (home->zombie_period == 0) home->zombie_period =30;
1097 home->response_window = home->zombie_period - 1;
1099 home->ping_check = HOME_PING_CHECK_NONE;
1101 home->revive_interval = rc->dead_time;
1103 if (rbtree_finddata(home_servers_byaddr, home)) {
1104 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
1109 if (!rbtree_insert(home_servers_byname, home)) {
1110 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1115 if (!rbtree_insert(home_servers_byaddr, home)) {
1116 rbtree_deletebydata(home_servers_byname, home);
1117 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1123 home->number = home_server_max_number++;
1124 if (!rbtree_insert(home_servers_bynumber, home)) {
1125 rbtree_deletebydata(home_servers_byname, home);
1126 if (home->ipaddr.af != AF_UNSPEC) {
1127 rbtree_deletebydata(home_servers_byname, home);
1129 cf_log_err(cf_sectiontoitem(cs),
1130 "Internal error %d adding home server %s.",
1139 * We now have a home server, see if we can insert it
1140 * into pre-existing pool.
1142 if (insert_point >= 0) {
1143 rad_assert(pool != NULL);
1144 pool->servers[insert_point] = home;
1148 rad_assert(pool == NULL);
1149 rad_assert(home != NULL);
1152 * Count the old-style realms of this name.
1154 num_home_servers = 0;
1155 for (subcs = cf_section_find_next(cs, NULL, "realm");
1157 subcs = cf_section_find_next(cs, subcs, "realm")) {
1158 const char *this = cf_section_name2(subcs);
1160 if (!this || (strcmp(this, realm) != 0)) continue;
1164 if (num_home_servers == 0) {
1165 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
1170 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1173 pool->servers[0] = home;
1175 if (!rbtree_insert(home_pools_byname, pool)) {
1176 rad_assert("Internal sanity check failed");
1186 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1189 const char *secret = NULL;
1190 home_pool_type_t ldflag;
1193 cp = cf_pair_find(cs, "ldflag");
1194 ldflag = HOME_POOL_FAIL_OVER;
1196 host = cf_pair_value(cp);
1198 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
1202 if (strcasecmp(host, "fail_over") == 0) {
1203 cf_log_info(cs, "\tldflag = fail_over");
1205 } else if (strcasecmp(host, "round_robin") == 0) {
1206 ldflag = HOME_POOL_LOAD_BALANCE;
1207 cf_log_info(cs, "\tldflag = round_robin");
1210 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
1213 } /* else don't print it. */
1216 * Allow old-style if it doesn't exist, or if it exists and
1219 cp = cf_pair_find(cs, "authhost");
1221 host = cf_pair_value(cp);
1223 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
1227 if (strcmp(host, "LOCAL") != 0) {
1228 cp = cf_pair_find(cs, "secret");
1230 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1234 secret = cf_pair_value(cp);
1236 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1241 cf_log_info(cs, "\tauthhost = %s", host);
1243 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1244 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1249 cp = cf_pair_find(cs, "accthost");
1251 host = cf_pair_value(cp);
1253 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1258 * Don't look for a secret again if it was found
1261 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1262 cp = cf_pair_find(cs, "secret");
1264 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1268 secret = cf_pair_value(cp);
1270 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1275 cf_log_info(cs, "\taccthost = %s", host);
1277 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1278 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1283 cp = cf_pair_find(cs, "virtual_server");
1285 host = cf_pair_value(cp);
1287 cf_log_err(cf_pairtoitem(cp), "No value specified for virtual_server");
1291 cf_log_info(cs, "\tvirtual_server = %s", host);
1293 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1294 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1297 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1298 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1303 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1311 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1312 const char *name, home_pool_t **dest,
1313 int server_type, int do_print)
1315 home_pool_t mypool, *pool;
1318 mypool.server_type = server_type;
1320 pool = rbtree_finddata(home_pools_byname, &mypool);
1322 CONF_SECTION *pool_cs;
1324 pool_cs = cf_section_sub_find_name2(rc->cs,
1328 pool_cs = cf_section_sub_find_name2(rc->cs,
1333 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1337 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1341 pool = rbtree_finddata(home_pools_byname, &mypool);
1343 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1348 if (pool->server_type != server_type) {
1349 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1360 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1366 home_pool_t *auth_pool, *acct_pool;
1367 const char *auth_pool_name, *acct_pool_name;
1370 name2 = cf_section_name1(cs);
1371 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1372 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1376 name2 = cf_section_name2(cs);
1378 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1383 auth_pool = acct_pool = NULL;
1384 auth_pool_name = acct_pool_name = NULL;
1387 * Prefer new configuration to old one.
1389 cp = cf_pair_find(cs, "pool");
1390 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1391 if (cp) auth_pool_name = cf_pair_value(cp);
1392 if (cp && auth_pool_name) {
1393 acct_pool_name = auth_pool_name;
1394 if (!add_pool_to_realm(rc, cs,
1395 auth_pool_name, &auth_pool,
1396 HOME_TYPE_AUTH, 1)) {
1399 if (!add_pool_to_realm(rc, cs,
1400 auth_pool_name, &acct_pool,
1401 HOME_TYPE_ACCT, 0)) {
1406 cp = cf_pair_find(cs, "auth_pool");
1407 if (cp) auth_pool_name = cf_pair_value(cp);
1408 if (cp && auth_pool_name) {
1410 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1413 if (!add_pool_to_realm(rc, cs,
1414 auth_pool_name, &auth_pool,
1415 HOME_TYPE_AUTH, 1)) {
1420 cp = cf_pair_find(cs, "acct_pool");
1421 if (cp) acct_pool_name = cf_pair_value(cp);
1422 if (cp && acct_pool_name) {
1423 int do_print = TRUE;
1426 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1431 (strcmp(auth_pool_name, acct_pool_name) != 0)) {
1435 if (!add_pool_to_realm(rc, cs,
1436 acct_pool_name, &acct_pool,
1437 HOME_TYPE_ACCT, do_print)) {
1443 cf_log_info(cs, " realm %s {", name2);
1447 * The realm MAY already exist if it's an old-style realm.
1448 * In that case, merge the old-style realm with this one.
1450 r = realm_find2(name2);
1451 if (r && (strcmp(r->name, name2) == 0)) {
1452 if (cf_pair_find(cs, "auth_pool") ||
1453 cf_pair_find(cs, "acct_pool")) {
1454 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1458 if (!old_realm_config(rc, cs, r)) {
1462 cf_log_info(cs, " } # realm %s", name2);
1468 if (name2[0] == '~') {
1472 * Include substring matches.
1474 if (regcomp(®, name2 + 1,
1475 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
1476 cf_log_err(cf_sectiontoitem(cs),
1477 "Invalid regex in realm \"%s\"", name2);
1484 r = rad_malloc(sizeof(*r));
1485 memset(r, 0, sizeof(*r));
1490 r->auth_pool = auth_pool;
1491 r->acct_pool = acct_pool;
1493 if (auth_pool_name &&
1494 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1495 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1497 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1498 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1502 cp = cf_pair_find(cs, "nostrip");
1503 if (cp && (cf_pair_value(cp) == NULL)) {
1505 cf_log_info(cs, "\tnostrip");
1509 * We're a new-style realm. Complain if we see the old
1512 if (r->auth_pool || r->acct_pool) {
1513 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1514 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1515 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1516 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1517 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1522 * The realm MAY be an old-style realm, as there
1523 * was no auth_pool or acct_pool. Double-check
1524 * it, just to be safe.
1526 } else if (!old_realm_config(rc, cs, r)) {
1532 * It's a regex. Add it to a separate list.
1534 if (name2[0] == '~') {
1535 realm_regex_t *rr, **last;
1537 rr = rad_malloc(sizeof(*rr));
1539 last = &realms_regex;
1540 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1548 cf_log_info(cs, " }");
1553 if (!rbtree_insert(realms_byname, r)) {
1554 rad_assert("Internal sanity check failed");
1558 cf_log_info(cs, " }");
1563 cf_log_info(cs, " } # realm %s", name2);
1569 static const FR_NAME_NUMBER home_server_types[] = {
1570 { "auth", HOME_TYPE_AUTH },
1571 { "auth+acct", HOME_TYPE_AUTH },
1572 { "acct", HOME_TYPE_ACCT },
1573 { "coa", HOME_TYPE_COA },
1577 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1580 const char *name, *type;
1582 CONF_SECTION *server_cs;
1584 cp = cf_pair_find(cs, "home_server");
1586 cf_log_err(cf_sectiontoitem(cs), "Pool does not contain a \"home_server\" entry");
1587 return HOME_TYPE_INVALID;
1590 name = cf_pair_value(cp);
1592 cf_log_err(cf_pairtoitem(cp), "home_server entry does not reference a home server");
1593 return HOME_TYPE_INVALID;
1596 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1598 cf_log_err(cf_pairtoitem(cp), "home_server \"%s\" does not exist", name);
1599 return HOME_TYPE_INVALID;
1602 cp = cf_pair_find(server_cs, "type");
1604 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s does not contain a \"type\" entry", name);
1605 return HOME_TYPE_INVALID;
1608 type = cf_pair_value(cp);
1610 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an empty \"type\" entry", name);
1611 return HOME_TYPE_INVALID;
1614 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1615 if (home == HOME_TYPE_INVALID) {
1616 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1617 return HOME_TYPE_INVALID;
1620 return home; /* 'cause we miss it so much */
1624 int realms_init(CONF_SECTION *config)
1627 realm_config_t *rc, *old_rc;
1629 if (realms_byname) return 1;
1631 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1632 if (!realms_byname) {
1638 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
1639 if (!home_servers_byaddr) {
1644 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1645 if (!home_servers_byname) {
1651 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1652 if (!home_servers_bynumber) {
1658 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1659 if (!home_pools_byname) {
1665 rc = rad_malloc(sizeof(*rc));
1666 memset(rc, 0, sizeof(*rc));
1670 cs = cf_subsection_find_next(config, NULL, "proxy");
1672 cf_section_parse(cs, rc, proxy_config);
1674 rc->dead_time = DEAD_TIME;
1675 rc->retry_count = RETRY_COUNT;
1676 rc->retry_delay = RETRY_DELAY;
1678 rc->wake_all_if_all_dead= 0;
1682 for (cs = cf_subsection_find_next(config, NULL, "realm");
1684 cs = cf_subsection_find_next(config, cs, "realm")) {
1685 if (!realm_add(rc, cs)) {
1694 * CoA pools aren't tied to realms.
1696 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1698 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1702 * Pool was already loaded.
1704 if (cf_data_find(cs, "home_server_pool")) continue;
1706 type = pool_peek_type(config, cs);
1707 if (type == HOME_TYPE_INVALID) return 0;
1709 if (!server_pool_add(rc, cs, type, TRUE)) {
1717 xlat_register("home_server", xlat_home_server, NULL);
1718 xlat_register("home_server_pool", xlat_server_pool, NULL);
1722 * Swap pointers atomically.
1724 old_rc = realm_config;
1732 * Find a realm where "name" might be the regex.
1734 REALM *realm_find2(const char *name)
1739 if (!name) name = "NULL";
1741 myrealm.name = name;
1742 realm = rbtree_finddata(realms_byname, &myrealm);
1743 if (realm) return realm;
1747 realm_regex_t *this;
1749 for (this = realms_regex; this != NULL; this = this->next) {
1750 if (strcmp(this->realm->name, name) == 0) {
1758 * Couldn't find a realm. Look for DEFAULT.
1760 myrealm.name = "DEFAULT";
1761 return rbtree_finddata(realms_byname, &myrealm);
1766 * Find a realm in the REALM list.
1768 REALM *realm_find(const char *name)
1773 if (!name) name = "NULL";
1775 myrealm.name = name;
1776 realm = rbtree_finddata(realms_byname, &myrealm);
1777 if (realm) return realm;
1781 realm_regex_t *this;
1783 for (this = realms_regex; this != NULL; this = this->next) {
1788 * Include substring matches.
1790 if (regcomp(®, this->realm->name + 1,
1791 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
1795 compare = regexec(®, name, 0, NULL, 0);
1798 if (compare == 0) return this->realm;
1804 * Couldn't find a realm. Look for DEFAULT.
1806 myrealm.name = "DEFAULT";
1807 return rbtree_finddata(realms_byname, &myrealm);
1812 home_server *home_server_ldb(const char *realmname,
1813 home_pool_t *pool, REQUEST *request)
1817 home_server *found = NULL;
1818 home_server *zombie = NULL;
1822 * Determine how to pick choose the home server.
1824 switch (pool->type) {
1828 * For load-balancing by client IP address, we
1829 * pick a home server by hashing the client IP.
1831 * This isn't as even a load distribution as
1832 * tracking the State attribute, but it's better
1835 case HOME_POOL_CLIENT_BALANCE:
1836 switch (request->packet->src_ipaddr.af) {
1838 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1839 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1842 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1843 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1849 start = hash % pool->num_home_servers;
1852 case HOME_POOL_CLIENT_PORT_BALANCE:
1853 switch (request->packet->src_ipaddr.af) {
1855 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1856 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1859 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1860 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1866 fr_hash_update(&request->packet->src_port,
1867 sizeof(request->packet->src_port), hash);
1868 start = hash % pool->num_home_servers;
1871 case HOME_POOL_KEYED_BALANCE:
1872 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY)) != NULL) {
1873 hash = fr_hash(vp->vp_strvalue, vp->length);
1874 start = hash % pool->num_home_servers;
1879 case HOME_POOL_LOAD_BALANCE:
1880 case HOME_POOL_FAIL_OVER:
1884 default: /* this shouldn't happen... */
1891 * Starting with the home server we chose, loop through
1892 * all home servers. If the current one is dead, skip
1893 * it. If it is too busy, skip it.
1895 * Otherwise, use it.
1897 for (count = 0; count < pool->num_home_servers; count++) {
1898 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
1900 if (!home) continue;
1903 * Skip dead home servers.
1905 if (home->state == HOME_STATE_IS_DEAD) {
1910 * This home server is too busy. Choose another one.
1912 if (home->currently_outstanding >= home->max_outstanding) {
1918 * We read the packet from a detail file, AND it
1919 * came from this server. Don't re-proxy it
1922 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
1923 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
1924 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
1930 * It's zombie, so we remember the first zombie
1931 * we find, but we don't mark it as a "live"
1934 if (home->state == HOME_STATE_ZOMBIE) {
1935 if (!zombie) zombie = home;
1940 * We've found the first "live" one. Use that.
1942 if (pool->type == HOME_POOL_FAIL_OVER) {
1948 * Otherwise we're doing some kind of load balancing.
1949 * If we haven't found one yet, pick this one.
1956 RDEBUG3("PROXY %s %d\t%s %d",
1957 found->name, found->currently_outstanding,
1958 home->name, home->currently_outstanding);
1961 * Prefer this server if it's less busy than the
1962 * one we had previously found.
1964 if (home->currently_outstanding < found->currently_outstanding) {
1965 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
1966 home->name, found->name);
1972 * Ignore servers which are busier than the one
1975 if (home->currently_outstanding > found->currently_outstanding) {
1976 RDEBUG3("PROXY Skipping %s: It's busier than %s",
1977 home->name, found->name);
1982 * From the list of servers which have the same
1983 * load, choose one at random.
1985 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
1988 } /* loop over the home servers */
1991 * We have no live servers, BUT we have a zombie. Use
1992 * the zombie as a last resort.
1994 if (!found && zombie) {
2000 * There's a fallback if they're all dead.
2002 if (!found && pool->fallback) {
2003 found = pool->fallback;
2009 * Allocate the proxy packet, only if it wasn't
2010 * already allocated by a module. This check is
2011 * mainly to support the proxying of EAP-TTLS and
2012 * EAP-PEAP tunneled requests.
2014 * In those cases, the EAP module creates a
2015 * "fake" request, and recursively passes it
2016 * through the authentication stage of the
2017 * server. The module then checks if the request
2018 * was supposed to be proxied, and if so, creates
2019 * a proxy packet from the TUNNELED request, and
2020 * not from the EAP request outside of the
2023 * The proxy then works like normal, except that
2024 * the response packet is "eaten" by the EAP
2025 * module, and encapsulated into an EAP packet.
2027 if (!request->proxy) {
2028 if ((request->proxy = rad_alloc(TRUE)) == NULL) {
2029 radlog(L_ERR|L_CONS, "no memory");
2034 * Copy the request, then look up name
2035 * and plain-text password in the copy.
2037 * Note that the User-Name attribute is
2038 * the *original* as sent over by the
2039 * client. The Stripped-User-Name
2040 * attribute is the one hacked through
2043 request->proxy->vps = paircopy(request->packet->vps);
2046 * Set the source IP address for proxying.
2048 request->proxy->src_ipaddr = found->src_ipaddr;
2052 * Update the various fields as appropriate.
2054 request->proxy->dst_ipaddr = found->ipaddr;
2055 request->proxy->dst_port = found->port;
2056 request->home_server = found;
2059 * We're supposed to add a Message-Authenticator
2060 * if it doesn't exist, and it doesn't exist.
2062 if (found->message_authenticator &&
2063 (request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2064 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR)) {
2065 radius_pairmake(request, &request->proxy->vps,
2066 "Message-Authenticator", "0x00",
2074 * No live match found, and no fallback to the "DEFAULT"
2075 * realm. We fix this by blindly marking all servers as
2076 * "live". But only do it for ones that don't support
2077 * "pings", as they will be marked live when they
2078 * actually are live.
2080 if (!realm_config->fallback &&
2081 realm_config->wake_all_if_all_dead) {
2082 for (count = 0; count < pool->num_home_servers; count++) {
2083 home_server *home = pool->servers[count];
2085 if (!home) continue;
2087 if ((home->state == HOME_STATE_IS_DEAD) &&
2088 (home->ping_check == HOME_PING_CHECK_NONE)) {
2089 home->state = HOME_STATE_ALIVE;
2090 if (!found) found = home;
2094 if (found) goto update_and_return;
2098 * Still nothing. Look up the DEFAULT realm, but only
2099 * if we weren't looking up the NULL or DEFAULT realms.
2101 if (realm_config->fallback &&
2103 (strcmp(realmname, "NULL") != 0) &&
2104 (strcmp(realmname, "DEFAULT") != 0)) {
2105 REALM *rd = realm_find("DEFAULT");
2107 if (!rd) return NULL;
2110 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2111 pool = rd->auth_pool;
2113 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2114 pool = rd->acct_pool;
2116 if (!pool) return NULL;
2118 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2119 return home_server_ldb(rd->name, pool, request);
2123 * Still haven't found anything. Oh well.
2129 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port)
2133 memset(&myhome, 0, sizeof(myhome));
2134 myhome.ipaddr = *ipaddr;
2136 myhome.server = NULL; /* we're not called for internal proxying */
2138 return rbtree_finddata(home_servers_byaddr, &myhome);
2142 home_server *home_server_byname(const char *name)
2146 memset(&myhome, 0, sizeof(myhome));
2149 return rbtree_finddata(home_servers_byname, &myhome);
2154 home_server *home_server_bynumber(int number)
2158 memset(&myhome, 0, sizeof(myhome));
2159 myhome.number = number;
2160 myhome.server = NULL; /* we're not called for internal proxying */
2162 return rbtree_finddata(home_servers_bynumber, &myhome);
2166 home_pool_t *home_pool_byname(const char *name, int type)
2170 memset(&mypool, 0, sizeof(mypool));
2172 mypool.server_type = type;
2173 return rbtree_finddata(home_pools_byname, &mypool);
2179 static int home_server_create_callback(void *ctx, void *data)
2181 rad_listen_t *head = ctx;
2182 home_server *home = data;
2186 * If there WAS a src address defined, ensure that a
2187 * proxy listener has been defined.
2189 if (home->src_ipaddr.af != AF_UNSPEC) {
2190 this = proxy_new_listener(&home->src_ipaddr, TRUE);
2193 * Failed to create it: Die
2195 if (!this) return 1;
2197 this->next = head->next;
2205 * Taking a void* here solves some header issues.
2207 int home_server_create_listeners(void *ctx)
2209 rad_listen_t *head = ctx;
2211 if (!home_servers_byaddr) return 0;
2213 rad_assert(head != NULL);
2216 * Add the listeners to the TAIL of the list.
2218 while (head->next) head = head->next;
2220 if (rbtree_walk(home_servers_byaddr, InOrder,
2221 home_server_create_callback, head) != 0) {