From: Alan T. DeKok Date: Sat, 18 Apr 2020 23:56:50 +0000 (-0400) Subject: added provisions for dynamic home servers X-Git-Tag: release_3_0_22~615 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=97f3a072a6aaa19dab51a01afde9cf9cd50f6dda;p=thirdparty%2Ffreeradius-server.git added provisions for dynamic home servers and glued them into radmin. Untested as of yet. --- diff --git a/src/include/realms.h b/src/include/realms.h index 6dae8b4f85a..cbbb9da4a92 100644 --- a/src/include/realms.h +++ b/src/include/realms.h @@ -206,6 +206,9 @@ home_server_t *home_server_bynumber(int number); #endif home_pool_t *home_pool_byname(char const *name, int type); +int home_server_afrom_file(char const *filename); +int home_server_delete(char const *name, char const *type); + #ifdef __cplusplus } #endif diff --git a/src/main/command.c b/src/main/command.c index 844898eed10..b38cc0d1952 100644 --- a/src/main/command.c +++ b/src/main/command.c @@ -2597,6 +2597,36 @@ static int command_del_client(rad_listen_t *listener, int argc, char *argv[]) } +static int command_del_home_server(rad_listen_t *listener, int argc, char *argv[]) +{ + if (argc < 2) { + cprintf_error(listener, " and are required\n"); + return 0; + } + + if (home_server_delete(argv[0], argv[1]) < 0) { + cprintf_error(listener, "Failed deleted home_server %s - %s", fr_strerror()); + return 0; + } + + return CMD_OK; +} + +static int command_add_home_server_file(rad_listen_t *listener, int argc, char *argv[]) +{ + if (argc < 1) { + cprintf_error(listener, " is required\n"); + return 0; + } + + if (home_server_afrom_file(argv[0]) < 0) { + cprintf_error(listener, "Unable to add home server - %s\n", fr_strerror()); + return 0; + } + + return CMD_OK; +} + static fr_command_table_t command_table_del_client[] = { { "ipaddr", FR_WRITE, "del client ipaddr [udp|tcp] [listen ] - Delete a dynamically created client", @@ -2606,11 +2636,23 @@ static fr_command_table_t command_table_del_client[] = { }; +static fr_command_table_t command_table_del_home_server[] = { + { "dynamic", FR_WRITE, + "del home_server dynamic [auth|acct|coa] - Delete a dynamically created home_server", + command_del_client, NULL }, + + { NULL, 0, NULL, NULL, NULL } +}; + static fr_command_table_t command_table_del[] = { { "client", FR_WRITE, "del client - Delete client configuration commands", NULL, command_table_del_client }, + { "home_server", FR_WRITE, + "del home_server - Delete home_server configuration commands", + NULL, command_table_del_client }, + { NULL, 0, NULL, NULL, NULL } }; @@ -2624,11 +2666,24 @@ static fr_command_table_t command_table_add_client[] = { }; +static fr_command_table_t command_table_add_home_server[] = { + { "file", FR_WRITE, + "add home_server file - Add new home serverdefinition from ", + command_add_home_server_file, NULL }, + + { NULL, 0, NULL, NULL, NULL } +}; + + static fr_command_table_t command_table_add[] = { { "client", FR_WRITE, "add client - Add client configuration commands", NULL, command_table_add_client }, + { "home_server", FR_WRITE, + "add home_server - Add home server configuration commands", + NULL, command_table_add_home_server }, + { NULL, 0, NULL, NULL, NULL } }; #endif diff --git a/src/main/realms.c b/src/main/realms.c index eb425981163..1bd916c05bc 100644 --- a/src/main/realms.c +++ b/src/main/realms.c @@ -2805,4 +2805,102 @@ home_pool_t *home_pool_byname(char const *name, int type) return rbtree_finddata(home_pools_byname, &mypool); } + +int home_server_afrom_file(char const *filename) +{ + CONF_SECTION *cs, *subcs; + char const *p; + + if (!realm_config->dynamic) { + fr_strerror_printf("Must set 'dynamic' in proxy.conf for dynamic home servers to work"); + return -1; + } + + cs = cf_section_alloc(NULL, "home", filename); + if (!cs) { + fr_strerror_printf("Failed allocating memory"); + return -1; + } + + if (cf_file_read(cs, filename) < 0) { + fr_strerror_printf("Failed reading file %s", filename); + talloc_free(cs); + return -1; + } + + p = strrchr(filename, '/'); + if (p) { + p++; + } else { + p = filename; + } + + subcs = cf_section_sub_find_name2(cs, "home", p); + if (!subcs) { + fr_strerror_printf("No 'home_server %p' definition in the file."); + talloc_free(cs); + return -1; + } + + home = home_server_afrom_cs(realm_config, realm_config, cs); + if (!home) { + fr_strerror_printf("Failed parsing configuration to a home_server structure"); + talloc_free(cs); + return -1; + } + + home->dynamic = true; + + if (home->server || home->dual) { + fr_strerror_printf("Dynamic home_server '%s' cannot have 'server' or 'auth+acct'", p); + talloc_free(cs); + talloc_free(home); + return -1; + } + + if (!realm_home_server_add(home)) { + fr_strerror_printf("Failed adding home_server to the internal data structures"); + talloc_free(cs); + talloc_free(home); + return -1; + } + + return 0; +} + +int home_server_delete(char const *name, char const *type_name) +{ + home_server_t *home; + int type; + + if (!realm_config->dynamic) { + fr_strerror_printf("Must set 'dynamic' in proxy.conf for dynamic home servers to work"); + return -1; + } + + type = fr_str2int(home_server_types, home->type_str, HOME_TYPE_INVALID); + if (type == HOME_TYPE_INVALID) { + fr_strerror_printf("Unknown home_server type '%s'", type_name); + return -1; + } + + home = home_server_byname(name, type); + if (!home) { + fr_strerror_printf("Failed to find home_server %s", name); + return -1; + } + + if (!home->dynamic) { + fr_strerror_printf("Cannot delete static home_server %s", name); + return -1; + } + + (void) rbtree_deletebydata(home_servers_byname, home); + (void) rbtree_deletebydata(home_servers_byaddr, home); + + /* + * Leak home, and home->cs. Oh well. + */ + return 0; +} #endif