]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #2439 in SNORT/snort3 from ~OSERHIIE/snort3:trace_all_modules_opti...
authorBhagya Tholpady (bbantwal) <bbantwal@cisco.com>
Tue, 8 Sep 2020 16:20:30 +0000 (16:20 +0000)
committerBhagya Tholpady (bbantwal) <bbantwal@cisco.com>
Tue, 8 Sep 2020 16:20:30 +0000 (16:20 +0000)
Squashed commit of the following:

commit 63012926daf4107ca5ae00376d9cdef0fdd39212
Author: Oleksandr Serhiienko <oserhiie@cisco.com>
Date:   Wed Aug 26 12:27:05 2020 +0300

    trace: add support for modules.all option

src/trace/trace_module.cc
src/trace/trace_module.h
src/trace/trace_parser.cc
src/trace/trace_parser.h
src/trace/trace_swap.cc

index b15db5cb50698cb6a049fc4a8d56584c1e8327cc..0dd800626c19b4087bc5e9956bc307b6172bc089 100644 (file)
 
 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;
-}
-
index f9d88afa8b1fe174fe9b94f9ff0135ddc1b3b488..9255e3dd828d546eccbbd8051b7444d8f3621864 100644 (file)
@@ -47,7 +47,6 @@ public:
     { return GLOBAL; }
 
 private:
-    std::string find_module(const char* config_name) const;
     void generate_params();
 
 private:
index 8d7593f3a826667c2068e430874f6a1af6bf81bb..9da7477e958ac590bc69a35ccb3db0e3ccaa11a4 100644 (file)
@@ -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;
index f96a5c1d5807b401e6061ddcce5247114cd98335..89da5fc1d151641c29d801ea14f62b58e467688f 100644 (file)
@@ -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<std::string, std::map<std::string, bool>> s_configured_trace_options;
 };
 
index 2acfc0aa9b6484e382ee19dfa27e2dfb87fc0267..ba212020463f5e73cdda2d357c2be96d90b25afb 100644 (file)
@@ -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);