From: Alan T. DeKok Date: Sun, 21 Aug 2022 13:45:27 +0000 (-0400) Subject: allow setting of options via the command line for migration X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=60aea62e37217a399070c45185587afae8708b4b;p=thirdparty%2Ffreeradius-server.git allow setting of options via the command line for migration We need to have the variables set before parsing the configuration files, so their configuration can't go into the files. --- diff --git a/src/bin/radiusd.c b/src/bin/radiusd.c index c2dd55cb402..aefac34013b 100644 --- a/src/bin/radiusd.c +++ b/src/bin/radiusd.c @@ -354,7 +354,7 @@ int main(int argc, char *argv[]) } /* Process the options. */ - while ((c = getopt(argc, argv, "Cd:D:e:fhi:l:Mmn:p:PrstTvxX")) != -1) switch (c) { + while ((c = getopt(argc, argv, "Cd:D:e:fhi:l:Mmn:p:PrsS:tTvxX")) != -1) switch (c) { case 'C': check_config = true; config->spawn_workers = false; @@ -425,6 +425,14 @@ int main(int argc, char *argv[]) config->daemonize = false; break; + case 'S': /* Migration support */ + if (main_config_parse_option(optarg) < 0) { + fprintf(stderr, "%s: Unknown configuration option '%s'\n", + config->name, optarg); + EXIT_WITH_FAILURE; + } + break; + case 't': /* no child threads */ config->spawn_workers = false; break; diff --git a/src/bin/unit_test_module.c b/src/bin/unit_test_module.c index 6d1dddcd2d5..a9d8c59e5b6 100644 --- a/src/bin/unit_test_module.c +++ b/src/bin/unit_test_module.c @@ -636,7 +636,7 @@ int main(int argc, char *argv[]) default_log.print_level = true; /* Process the options. */ - while ((c = getopt(argc, argv, "c:d:D:f:hi:mMn:o:O:p:r:xXz")) != -1) { + while ((c = getopt(argc, argv, "c:d:D:f:hi:mMn:o:O:p:r:S:xXz")) != -1) { switch (c) { case 'c': count = atoi(optarg); @@ -691,6 +691,14 @@ int main(int argc, char *argv[]) receipt_file = optarg; break; + case 'S': /* Migration support */ + if (main_config_parse_option(optarg) < 0) { + fprintf(stderr, "%s: Unknown configuration option '%s'\n", + config->name, optarg); + fr_exit_now(EXIT_FAILURE); + } + break; + case 'X': fr_debug_lvl += 2; default_log.print_level = true; diff --git a/src/lib/server/main_config.c b/src/lib/server/main_config.c index 0dfba227bb3..c48a2c7f016 100644 --- a/src/lib/server/main_config.c +++ b/src/lib/server/main_config.c @@ -174,6 +174,9 @@ static const CONF_PARSER thread_config[] = { CONF_PARSER_TERMINATOR }; +/* + * Migration configuration. + */ static const CONF_PARSER migrate_config[] = { { FR_CONF_OFFSET("unflatten_after_decode", FR_TYPE_BOOL | FR_TYPE_HIDDEN, main_config_t, unflatten_after_decode) }, { FR_CONF_OFFSET("unflatten_before_encode", FR_TYPE_BOOL | FR_TYPE_HIDDEN, main_config_t, unflatten_before_encode) }, @@ -1420,3 +1423,43 @@ void main_config_hup(main_config_t *config) INFO("HUP - NYI in version 4"); /* Not yet implemented in v4 */ } + +static fr_table_num_ordered_t config_arg_table[] = { + { L("parse_new_conditions"), offsetof(main_config_t, parse_new_conditions) }, + { L("use_new_conditions"), offsetof(main_config_t, use_new_conditions) }, + { L("tmpl_tokenize_all_nested"), offsetof(main_config_t, tmpl_tokenize_all_nested) }, +}; +static size_t config_arg_table_len = NUM_ELEMENTS(config_arg_table); + +/* + * Migration function that allows for command-line over-ride of + * data structures which need to be initialized before the + * configuration files are loaded. + * + * This should really only be temporary, until we get rid of flat vs nested. + */ +int main_config_parse_option(char const *value) +{ + fr_value_box_t box; + size_t offset; + char const *p; + + p = strchr(value, '='); + if (!p) return -1; + + offset = fr_table_value_by_substr(config_arg_table, value, p - value, 0); + if (!offset) return -1; + + p += 1; + + fr_value_box_init(&box, FR_TYPE_BOOL, NULL, false); + if (fr_value_box_from_str(NULL, &box, FR_TYPE_BOOL, NULL, + p, strlen(p), NULL, false) < 0) { + fr_perror("Invalid boolean \"%s\"", p); + fr_exit(1); + } + + *(bool *) (((uintptr_t) main_config) + offset) = box.vb_bool; + + return 0; +} diff --git a/src/lib/server/main_config.h b/src/lib/server/main_config.h index 8a8af7cd379..28cbf0179f5 100644 --- a/src/lib/server/main_config.h +++ b/src/lib/server/main_config.h @@ -165,6 +165,8 @@ void main_config_name_set_default(main_config_t *config, char const *name, boo void main_config_raddb_dir_set(main_config_t *config, char const *path); void main_config_dict_dir_set(main_config_t *config, char const *path); +int main_config_parse_option(char const *value); /* flat / nested migration */ + void main_config_exclusive_proc_done(main_config_t const *config); int main_config_exclusive_proc_child(main_config_t const *config); int main_config_exclusive_proc(main_config_t *config);