From: Bhagya Tholpady (bbantwal) Date: Tue, 8 Sep 2020 16:20:30 +0000 (+0000) Subject: Merge pull request #2439 in SNORT/snort3 from ~OSERHIIE/snort3:trace_all_modules_opti... X-Git-Tag: 3.0.2-6~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d0e755ed708f76ee772b518b5ea1c0e0bbacd3b1;p=thirdparty%2Fsnort3.git Merge pull request #2439 in SNORT/snort3 from ~OSERHIIE/snort3:trace_all_modules_option to master Squashed commit of the following: commit 63012926daf4107ca5ae00376d9cdef0fdd39212 Author: Oleksandr Serhiienko Date: Wed Aug 26 12:27:05 2020 +0300 trace: add support for modules.all option --- diff --git a/src/trace/trace_module.cc b/src/trace/trace_module.cc index b15db5cb5..0dd800626 100644 --- a/src/trace/trace_module.cc +++ b/src/trace/trace_module.cc @@ -36,6 +36,28 @@ using namespace snort; +// Helpers + +static std::string extract_module_option(const char* fqn) +{ + std::string option_name; + const std::string config_name(fqn); + const std::string pattern = "trace.modules."; + size_t start_pos = config_name.find(pattern); + if ( start_pos != std::string::npos ) + { + start_pos += pattern.size(); + size_t end_pos = config_name.find(".", start_pos); + size_t option_len = ( end_pos != std::string::npos ) ? end_pos - start_pos + : config_name.size() - start_pos; + + option_name = config_name.substr(start_pos, option_len); + } + return option_name; +} + +// Module stuff + #define trace_help "configure trace log messages" #define s_name "trace" @@ -87,6 +109,9 @@ void TraceModule::generate_params() std::sort(modules_params.begin(), modules_params.end(), [](const Parameter& l, const Parameter& r) { return (strcmp(l.name, r.name) < 0); }); + modules_params.emplace(modules_params.begin(), DEFAULT_TRACE_OPTION_NAME, Parameter::PT_INT, + "0:255", nullptr, "enable trace for all modules"); + modules_params.emplace_back(nullptr, Parameter::PT_MAX, nullptr, nullptr, nullptr); const static Parameter trace_constraints_params[] = @@ -171,8 +196,8 @@ bool TraceModule::set(const char* fqn, Value& v, SnortConfig*) } else if ( strstr(fqn, "trace.modules.") == fqn ) { - std::string module_name = find_module(fqn); - return trace_parser->set_traces(module_name, v); + std::string option_name = extract_module_option(fqn); + return trace_parser->set_traces(option_name, v); } else if ( strstr(fqn, "trace.constraints.") == fqn ) return trace_parser->set_constraints(v); @@ -221,19 +246,3 @@ bool TraceModule::end(const char* fqn, int, SnortConfig* sc) return true; } -std::string TraceModule::find_module(const char* fqn) const -{ - std::string module_name; - const std::string config_name(fqn); - const std::string pattern = "trace.modules."; - size_t start_pos = config_name.find(pattern); - if ( start_pos != std::string::npos ) - { - start_pos += pattern.size(); - size_t end_pos = config_name.find(".", start_pos); - if ( end_pos != std::string::npos ) - module_name = config_name.substr(start_pos, end_pos - start_pos); - } - return module_name; -} - diff --git a/src/trace/trace_module.h b/src/trace/trace_module.h index f9d88afa8..9255e3dd8 100644 --- a/src/trace/trace_module.h +++ b/src/trace/trace_module.h @@ -47,7 +47,6 @@ public: { return GLOBAL; } private: - std::string find_module(const char* config_name) const; void generate_params(); private: diff --git a/src/trace/trace_parser.cc b/src/trace/trace_parser.cc index 8d7593f3a..9da7477e9 100644 --- a/src/trace/trace_parser.cc +++ b/src/trace/trace_parser.cc @@ -44,26 +44,45 @@ TraceParser::TraceParser(TraceConfig* tc) reset_configured_trace_options(); } -bool TraceParser::set_traces(const std::string& module_name, const Value& val) +bool TraceParser::set_traces(const std::string& option_name, const Value& val) { - if ( !s_configured_trace_options.count(module_name) ) + if ( !s_configured_trace_options.count(option_name) + and option_name != DEFAULT_TRACE_OPTION_NAME ) return false; - if ( val.is(DEFAULT_TRACE_OPTION_NAME) ) + if ( option_name == DEFAULT_TRACE_OPTION_NAME ) { - const auto& trace_options = s_configured_trace_options[module_name]; + for ( const auto& trace_options : s_configured_trace_options ) + { + if ( trace_options.second.at(DEFAULT_TRACE_OPTION_NAME) ) + continue; + + for ( const auto& trace_option : trace_options.second ) + { + if ( !trace_option.second ) + trace_config->set_trace(trace_options.first, trace_option.first, + val.get_uint8()); + } + } + + return true; + } + else if ( val.is(DEFAULT_TRACE_OPTION_NAME) ) + { + auto& trace_options = s_configured_trace_options[option_name]; for ( const auto& trace_option : trace_options ) { if ( !trace_option.second ) - trace_config->set_trace(module_name, trace_option.first, val.get_uint8()); + trace_config->set_trace(option_name, trace_option.first, val.get_uint8()); } + trace_options[DEFAULT_TRACE_OPTION_NAME] = true; return true; } else { - bool res = trace_config->set_trace(module_name, val.get_name(), val.get_uint8()); - s_configured_trace_options[module_name][val.get_name()] = res; + bool res = trace_config->set_trace(option_name, val.get_name(), val.get_uint8()); + s_configured_trace_options[option_name][val.get_name()] = res; return res; } } @@ -142,9 +161,8 @@ void TraceParser::init_configured_trace_options() if ( trace_options ) { auto& module_trace_options = s_configured_trace_options[module->get_name()]; - if ( !trace_options->name ) - module_trace_options[DEFAULT_TRACE_OPTION_NAME] = false; + module_trace_options[DEFAULT_TRACE_OPTION_NAME] = false; while ( trace_options->name ) { module_trace_options[trace_options->name] = false; diff --git a/src/trace/trace_parser.h b/src/trace/trace_parser.h index f96a5c1d5..89da5fc1d 100644 --- a/src/trace/trace_parser.h +++ b/src/trace/trace_parser.h @@ -38,7 +38,7 @@ class TraceParser public: TraceParser(TraceConfig*); - bool set_traces(const std::string& module_name, const snort::Value& val); + bool set_traces(const std::string& option_name, const snort::Value& val); bool set_constraints(const snort::Value& val); void finalize_constraints(); @@ -56,6 +56,7 @@ private: private: TraceConfig* trace_config = nullptr; snort::PacketConstraints parsed_constraints; + static std::map> s_configured_trace_options; }; diff --git a/src/trace/trace_swap.cc b/src/trace/trace_swap.cc index 2acfc0aa9..ba2120204 100644 --- a/src/trace/trace_swap.cc +++ b/src/trace/trace_swap.cc @@ -215,46 +215,63 @@ static int set(lua_State* L) lua_pushnil(L); while ( lua_next(L, modules_tbl_idx) ) { - const char* module_name = luaL_checkstring(L, -2); - const Parameter* module_param = Parameter::find(modules_param, module_name); + const char* option_name = luaL_checkstring(L, -2); + const Parameter* option_param = Parameter::find(modules_param, option_name); - if ( !lua_istable(L, -1) or !module_param ) + // Trace table traversal + if ( lua_istable(L, -1) and option_param ) { - LogMessage("== invalid table is provided: %s.%s\n", root_element_key, - module_name); + int module_tbl_idx = lua_gettop(L); + lua_pushnil(L); + while ( lua_next(L, module_tbl_idx) ) + { + const char* val_name = luaL_checkstring(L, -2); + const Parameter* trace_param = Parameter::find( + (const Parameter*)option_param->range, val_name); - parse_err = true; - lua_pop(L, 1); - continue; - } + Value val(false); + val.set(trace_param); - // Trace table traversal - int module_tbl_idx = lua_gettop(L); - lua_pushnil(L); - while ( lua_next(L, module_tbl_idx) ) - { - const char* val_name = luaL_checkstring(L, -2); - const Parameter* trace_param = Parameter::find( - (const Parameter*)module_param->range, val_name); + if ( lua_isnumber(L, -1) ) + val.set((double)lua_tointeger(L, -1)); + else + val.set(luaL_checkstring(L, -1)); + + if ( !trace_param or !trace_param->validate(val) or + !trace_parser.set_traces(option_name, val) ) + { + LogMessage("== invalid trace value is provided: %s.%s.%s = %s\n", + root_element_key, option_name, val_name, + val.get_as_string().c_str()); - Value val(false); - val.set(trace_param); + parse_err = true; + } - if ( lua_isnumber(L, -1) ) - val.set((double)lua_tointeger(L, -1)); - else - val.set(luaL_checkstring(L, -1)); + lua_pop(L, 1); + } + } + // Enable all option + else if ( lua_isnumber(L, -1) and option_param ) + { + Value val((double)lua_tointeger(L, -1)); + val.set(option_param); - if ( !trace_param or !trace_param->validate(val) or - !trace_parser.set_traces(module_name, val) ) + if ( !option_param->validate(val) or + !trace_parser.set_traces(option_name, val) ) { - LogMessage("== invalid trace value is provided: %s.%s.%s = %s\n", - root_element_key, module_name, val_name, val.get_as_string().c_str()); + LogMessage("== invalid option value is provided: %s.%s = %s\n", + root_element_key, option_name, val.get_as_string().c_str()); parse_err = true; } + } + // Error + else + { + LogMessage("== invalid option is provided: %s.%s\n", root_element_key, + option_name); - lua_pop(L, 1); + parse_err = true; } lua_pop(L, 1);