From: Arran Cudbard-Bell Date: Mon, 22 Dec 2014 22:52:12 +0000 (-0500) Subject: Split out add and alloc functions for home servers X-Git-Tag: release_3_0_7~408 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6c9562394600bbdf9f473f4d8fcbd3df55eb2c92;p=thirdparty%2Ffreeradius-server.git Split out add and alloc functions for home servers --- diff --git a/src/include/realms.h b/src/include/realms.h index 0f234324f23..4e8b263fcb3 100644 --- a/src/include/realms.h +++ b/src/include/realms.h @@ -54,6 +54,7 @@ typedef struct fr_socket_limit_t { typedef struct home_server { char const *name; + bool dual; //!< One of a pair of homeservers on consecutive ports. char const *server; //!< For internal proxying char const *parent_server; @@ -185,7 +186,8 @@ int realm_realm_add( REALM *r, CONF_SECTION *cs); void home_server_update_request(home_server_t *home, REQUEST *request); home_server_t *home_server_ldb(char const *realmname, home_pool_t *pool, REQUEST *request); home_server_t *home_server_find(fr_ipaddr_t *ipaddr, uint16_t port, int proto); -bool home_server_afrom_cs(realm_config_t *rc, CONF_SECTION *cs); + +home_server_t *home_server_afrom_cs(realm_config_t *rc, CONF_SECTION *cs); #ifdef WITH_COA home_server_t *home_server_byname(char const *name, int type); diff --git a/src/main/realms.c b/src/main/realms.c index 7e450c85b91..a6d7e7eded6 100644 --- a/src/main/realms.c +++ b/src/main/realms.c @@ -482,16 +482,61 @@ static bool home_server_insert(home_server_t *home, CONF_SECTION *cs) return true; } -/** Add a new home server defined by a CONF_SECTION +/** Add an already allocate home_server_t to the various trees + * + * @param rc Realm config to add home server to. + * @param home server to add. + * @return true on success, else false on error. + */ +static bool home_server_add(realm_config_t *rc, home_server_t *home) +{ + if (rbtree_finddata(home_servers_byname, home) != NULL) { + cf_log_err_cs(home->cs, "Duplicate home server name %s", home->name); + return false; + } + + if (!home->server && (rbtree_finddata(home_servers_byaddr, home) != NULL)) { + cf_log_err_cs(home->cs, "Duplicate home server IP %s", home->name); + return false; + } + + if (!home_server_insert(home, home->cs)) return false; + + if (home->dual) { + home_server_t *home2 = talloc(rc, home_server_t); + + memcpy(home2, home, sizeof(*home2)); + + home2->type = HOME_TYPE_ACCT; + home2->dual = true; + home2->port++; + home2->ping_user_password = NULL; + home2->cs = home->cs; + home2->parent_server = home->parent_server; + + if (!home_server_insert(home2, home->cs)) { + talloc_free(home2); + return false; + } + } + + /* + * Mark it as already processed + */ + cf_data_add(home->cs, "home_server", (void *)null_free, null_free); + + return true; +} + +/** Alloc a new home server defined by a CONF_SECTION * * @param rc Realm config to add home server to. * @param cs Configuration section containing home server parameters. - * @return true of home server was added, false on error. + * @return a new home_server_t alloced in the context of the realm_config, or NULL on error. */ -bool home_server_afrom_cs(realm_config_t *rc, CONF_SECTION *cs) +home_server_t *home_server_afrom_cs(realm_config_t *rc, CONF_SECTION *cs) { char const *name2; - bool dual = false; home_server_t *home; CONF_SECTION *tls; @@ -579,7 +624,7 @@ skip_port: switch (type) { case HOME_TYPE_AUTH_ACCT: - dual = true; + home->dual = true; home->type = HOME_TYPE_AUTH; break; @@ -746,41 +791,7 @@ skip_port: realm_home_server_sanitize(home, cs); - if (rbtree_finddata(home_servers_byname, home) != NULL) { - cf_log_err_cs(cs, "Duplicate home server name %s.", name2); - goto error; - } - - if (!home->server && (rbtree_finddata(home_servers_byaddr, home) != NULL)) { - cf_log_err_cs(cs, "Duplicate home server IP %s.", name2); - goto error; - } - - if (!home_server_insert(home, cs)) goto error; - - if (dual) { - home_server_t *home2 = talloc(rc, home_server_t); - - memcpy(home2, home, sizeof(*home2)); - - home2->type = HOME_TYPE_ACCT; - home2->port++; - home2->ping_user_password = NULL; - home2->cs = cs; - home2->parent_server = home->parent_server; - - if (!home_server_insert(home2, cs)) { - talloc_free(home2); - goto error; - } - } - - /* - * Mark it as already processed - */ - cf_data_add(cs, "home_server", (void *)null_free, null_free); - - return true; + return home; } static home_pool_t *server_pool_alloc(char const *name, home_pool_type_t type, @@ -1203,7 +1214,7 @@ static int old_server_add(realm_config_t *rc, CONF_SECTION *cs, cf_log_err_cs(cs, "Inconsistent ldflag for server pool \"%s\"", name); return 0; } - + /* * GCC throws signed comparison warning here without the cast * very strange... @@ -1958,7 +1969,11 @@ int realms_init(CONF_SECTION *config) for (cs = cf_subsection_find_next(config, NULL, "home_server"); cs != NULL; cs = cf_subsection_find_next(config, cs, "home_server")) { - if (!home_server_afrom_cs(rc, cs)) goto error; + home_server_t *home; + + home = home_server_afrom_cs(rc, cs); + if (!home) goto error; + if (!home_server_add(rc, home)) goto error; } /* @@ -1971,7 +1986,11 @@ int realms_init(CONF_SECTION *config) for (cs = cf_subsection_find_next(server_cs, NULL, "home_server"); cs != NULL; cs = cf_subsection_find_next(server_cs, cs, "home_server")) { - if (!home_server_afrom_cs(rc, cs)) goto error; + home_server_t *home; + + home = home_server_afrom_cs(rc, cs); + if (!home) goto error; + if (!home_server_add(rc, home)) goto error; } } #endif