From: Bhagya Tholpady (bbantwal) Date: Tue, 20 Oct 2020 23:51:03 +0000 (+0000) Subject: Merge pull request #2534 in SNORT/snort3 from ~SELYSENK/snort3:wizard_dump_config... X-Git-Tag: 3.0.3-3~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=38ec7816046685302801870ae915721b943bf589;p=thirdparty%2Fsnort3.git Merge pull request #2534 in SNORT/snort3 from ~SELYSENK/snort3:wizard_dump_config to master Squashed commit of the following: commit c9a30bcd84350ec29b7e05a10dadf0740605a25d Author: Serhii Lysenko Date: Thu Oct 8 16:35:43 2020 +0300 dump_config: don't print names for list elements --- diff --git a/src/dump_config/json_config_output.cc b/src/dump_config/json_config_output.cc index 73d34d354..9e52c9cb1 100644 --- a/src/dump_config/json_config_output.cc +++ b/src/dump_config/json_config_output.cc @@ -27,7 +27,7 @@ using namespace snort; -static void dump_value(JsonStream& json, const BaseConfigNode* node) +static void dump_value(JsonStream& json, const char* node_name, const BaseConfigNode* node) { const Value* value = node->get_value(); if ( !value ) @@ -37,11 +37,11 @@ static void dump_value(JsonStream& json, const BaseConfigNode* node) { case Parameter::PT_BOOL: case Parameter::PT_IMPLIED: - value->get_bool() ? json.put_true(node->get_name().c_str()) : - json.put_false(node->get_name().c_str()); + value->get_bool() ? json.put_true(node_name) : + json.put_false(node_name); break; case Parameter::PT_INT: - json.put(node->get_name().c_str(), value->get_long()); + json.put(node_name, value->get_long()); break; case Parameter::PT_REAL: { @@ -51,35 +51,40 @@ static void dump_value(JsonStream& json, const BaseConfigNode* node) if ( pos != std::string::npos ) precision = value_str.size() - pos - 1; - json.put(node->get_name().c_str(), value->get_real(), precision); + json.put(node_name, value->get_real(), precision); break; } default: - json.put(node->get_name().c_str(), value->get_origin_string()); + json.put(node_name, value->get_origin_string()); break; } } -static void dump_modules(JsonStream& json, const BaseConfigNode* node) +static void dump_tree(JsonStream& json, const BaseConfigNode* node, bool list_node = false) { - Parameter::Type type = node->get_type(); - if ( type == Parameter::PT_LIST ) - json.open_array(node->get_name().c_str()); - else if ( type == Parameter::PT_TABLE ) - { - std::string name = node->get_name(); - name.empty() ? json.open() : json.open(name.c_str()); - } - else - dump_value(json, node); + Parameter::Type node_type = node->get_type(); + const std::string node_name = node->get_name(); + const char* node_name_cstr = nullptr; - for ( const auto n : node->get_children() ) - dump_modules(json, n); + if ( !list_node ) + node_name_cstr = node_name.c_str(); - if ( type == Parameter::PT_LIST ) - json.close_array(); - else if ( type == Parameter::PT_TABLE ) + if ( node_type == Parameter::PT_TABLE ) + { + json.open(node_name_cstr); + for ( const auto n : node->get_children() ) + dump_tree(json, n); json.close(); + } + else if ( node_type == Parameter::PT_LIST ) + { + json.open_array(node_name_cstr); + for ( const auto n : node->get_children() ) + dump_tree(json, n, true); + json.close_array(); + } + else + dump_value(json, node_name_cstr, node); } JsonAllConfigOutput::JsonAllConfigOutput() : @@ -96,7 +101,7 @@ void JsonAllConfigOutput::dump(const ConfigData& config_data) json.open("config"); for ( const auto config_tree: config_data.config_trees ) - dump_modules(json, config_tree); + dump_tree(json, config_tree); json.close(); json.close(); @@ -107,7 +112,7 @@ void JsonTopConfigOutput::dump(const ConfigData& config_data) json.open(); for ( const auto config_tree: config_data.config_trees ) - dump_modules(json, config_tree); + dump_tree(json, config_tree); json.close(); } diff --git a/src/dump_config/text_config_output.cc b/src/dump_config/text_config_output.cc index f8607c5f5..75407ac88 100644 --- a/src/dump_config/text_config_output.cc +++ b/src/dump_config/text_config_output.cc @@ -26,7 +26,7 @@ using namespace snort; -void TextConfigOutput::dump_value(const BaseConfigNode* node, const std::string& config_name) +static void dump_value(const BaseConfigNode* node, const std::string& config_name) { const Value* value = node->get_value(); if ( !value ) @@ -56,32 +56,29 @@ void TextConfigOutput::dump_value(const BaseConfigNode* node, const std::string& } } -void TextConfigOutput::dump_modules(const BaseConfigNode* parent, const std::string& config_name) +static void dump_tree(const BaseConfigNode* node, const std::string& config_name) { - static char buf[16]; - int list_index = 0; - for ( const auto node : parent->get_children() ) - { - std::string full_config_name(config_name); - std::string node_name = node->get_name(); + Parameter::Type node_type = node->get_type(); - if ( !node_name.empty() ) - { - full_config_name += "."; - full_config_name += node_name; - } - else + if ( node_type == Parameter::PT_TABLE ) + { + for ( const auto child : node->get_children() ) + dump_tree(child, config_name + "." + child->get_name()); + } + else if ( node_type == Parameter::PT_LIST ) + { + char suffix[16]; + int list_index = 0; + for ( const auto child : node->get_children() ) { - sprintf(buf, "%i", list_index++); - full_config_name += "["; - full_config_name += buf; - full_config_name += "]"; + snprintf(suffix, 16, "[%i]", list_index); + const std::string full_config_name = config_name + suffix; + dump_tree(child, full_config_name); + list_index++; } - - dump_modules(node, full_config_name); } - - dump_value(parent, config_name); + else + dump_value(node, config_name); } void TextConfigOutput::dump(const ConfigData& config_data) @@ -91,6 +88,6 @@ void TextConfigOutput::dump(const ConfigData& config_data) std::cout << output << std::endl; for ( const auto config_tree: config_data.config_trees ) - dump_modules(config_tree, config_tree->get_name()); + dump_tree(config_tree, config_tree->get_name()); } diff --git a/src/dump_config/text_config_output.h b/src/dump_config/text_config_output.h index 92affbbf3..da36efea5 100644 --- a/src/dump_config/text_config_output.h +++ b/src/dump_config/text_config_output.h @@ -33,9 +33,6 @@ public: private: void dump(const ConfigData&) override; - - void dump_modules(const BaseConfigNode* parent, const std::string& config_name); - void dump_value(const BaseConfigNode* parent, const std::string& config_name); }; #endif // TEXT_CONFIG_OUTPUT_H diff --git a/src/main/shell.cc b/src/main/shell.cc index 4fdc8b60c..c924536ff 100644 --- a/src/main/shell.cc +++ b/src/main/shell.cc @@ -244,14 +244,10 @@ void Shell::set_config_value(const std::string& fqn, const snort::Value& value) if ( !s_config_output || !s_current_node ) return; - // lua interpreter does not call open_table for simple list items like (string) - // we have to add tree node for this item + // don't give names to list elements if ( s_current_node->get_type() == Parameter::PT_LIST ) { - add_config_child_node("", Parameter::PT_TABLE); - s_current_node->add_child_node(new ValueConfigNode(s_current_node, value)); - s_current_node = s_current_node->get_parent_node(); - + s_current_node->add_child_node(new ValueConfigNode(s_current_node, value, "")); return; }