]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #1409 in SNORT/snort3 from reload_fname to master
authorTom Peters (thopeter) <thopeter@cisco.com>
Tue, 6 Nov 2018 21:59:50 +0000 (16:59 -0500)
committerTom Peters (thopeter) <thopeter@cisco.com>
Tue, 6 Nov 2018 21:59:50 +0000 (16:59 -0500)
Squashed commit of the following:

commit a105a63d69690ef36649c21b977a1dab62f14774
Author: mdagon <mdagon@cisco.com>
Date:   Wed Aug 29 18:07:04 2018 -0400

    parser: bad filename during reload is not a fatal error

src/main/shell.cc
src/main/shell.h
src/main/snort.cc
src/parser/parser.cc
src/parser/parser.h

index 90176b50de3916d143803097be0da62d4adbbafb..206e5a484934add57ab5a101ee35971a7f099289 100644 (file)
@@ -72,13 +72,19 @@ static int get_line_number(lua_State* L)
 
 #endif
 
-static void load_config(lua_State* L, const char* file, const char* tweaks)
+static bool load_config(lua_State* L, const char* file, const char* tweaks, bool is_fatal)
 {
     Lua::ManageStack ms(L);
-
     if ( luaL_loadfile(L, file) )
-        FatalError("can't load %s: %s\n", file, lua_tostring(L, -1));
-
+    {
+        if (is_fatal)
+            FatalError("can't load %s: %s\n", file, lua_tostring(L, -1));
+        else
+        {
+            ParseError("can't load %s: %s\n", file, lua_tostring(L, -1));
+            return false;
+        }
+    }
     if ( tweaks and *tweaks )
     {
         lua_pushstring(L, tweaks);
@@ -87,6 +93,8 @@ static void load_config(lua_State* L, const char* file, const char* tweaks)
 
     if ( lua_pcall(L, 0, 0, 0) )
         FatalError("can't init %s: %s\n", file, lua_tostring(L, -1));
+
+    return true;
 }
 
 static void load_overrides(lua_State* L, string& s)
@@ -122,16 +130,19 @@ static void run_config(lua_State* L, const char* t)
     }
 }
 
-static void config_lua(
-    lua_State* L, const char* file, string& s, const char* tweaks)
+static bool config_lua(
+    lua_State* L, const char* file, string& s, const char* tweaks, bool is_fatal)
 {
     if ( file && *file )
-        load_config(L, file, tweaks);
+        if (!load_config(L, file, tweaks, is_fatal))
+            return false;
 
     if ( !s.empty() )
         load_overrides(L, s);
 
     run_config(L, "_G");
+
+    return true;
 }
 
 //-------------------------------------------------------------------------
@@ -189,7 +200,7 @@ void Shell::set_overrides(Shell* sh)
     overrides += sh->overrides;
 }
 
-void Shell::configure(SnortConfig* sc)
+bool Shell::configure(SnortConfig* sc, bool is_fatal)
 {
     assert(file.size());
     ModuleManager::set_config(sc);
@@ -207,13 +218,15 @@ void Shell::configure(SnortConfig* sc)
     }
 
     const char* base_name = push_relative_path(file.c_str());
-    config_lua(lua, base_name, overrides, sc->tweaks.c_str());
+    if(! config_lua(lua, base_name, overrides, sc->tweaks.c_str(), is_fatal))
+        return false;
 
     set_default_policy(sc);
     ModuleManager::set_config(nullptr);
     loaded = true;
 
     pop_relative_path();
+    return true;
 }
 
 void Shell::install(const char* name, const luaL_Reg* reg)
index a8eb5d968e5df8b23c2dcc9bcee3f6278a8c9ad1..f64f18cdc2e7384b8d49d2cc1146ccf7a41be399 100644 (file)
@@ -41,7 +41,7 @@ public:
     void set_overrides(const char*);
     void set_overrides(Shell*);
 
-    void configure(snort::SnortConfig*);
+    bool configure(snort::SnortConfig*, bool is_fatal = true);
     void install(const char*, const struct luaL_Reg*);
     void execute(const char*, std::string&);
 
index e3c201ec9b094437c891cc82c861a9a49054e2e5..7f643aebcdde3441ca9a05f21d3a065b2a4600a5 100644 (file)
@@ -291,7 +291,8 @@ void Snort::init(int argc, char** argv)
     if ( !Piglet::piglet_mode() )
 #endif
     if ( !SnortConfig::get_conf()->output.empty() )
-        EventManager::instantiate(SnortConfig::get_conf()->output.c_str(), SnortConfig::get_conf());
+        EventManager::instantiate(SnortConfig::get_conf()->output.c_str(),
+            SnortConfig::get_conf());
 
     if (SnortConfig::alert_before_pass())
     {
@@ -332,8 +333,10 @@ void Snort::init(int argc, char** argv)
     Trough::setup();
 
     // FIXIT-L refactor stuff done here and in snort_config.cc::VerifyReload()
-    if ( SnortConfig::get_conf()->bpf_filter.empty() && !SnortConfig::get_conf()->bpf_file.empty() )
-        SnortConfig::get_conf()->bpf_filter = read_infile("bpf_file", SnortConfig::get_conf()->bpf_file.c_str());
+    if ( SnortConfig::get_conf()->bpf_filter.empty() &&
+        !SnortConfig::get_conf()->bpf_file.empty() )
+        SnortConfig::get_conf()->bpf_filter = read_infile("bpf_file",
+            SnortConfig::get_conf()->bpf_file.c_str());
 
     if ( !SnortConfig::get_conf()->bpf_filter.empty() )
         LogMessage("Snort BPF option: %s\n", SnortConfig::get_conf()->bpf_filter.c_str());
@@ -558,10 +561,10 @@ SnortConfig* Snort::get_reload_config(const char* fname)
     trim_heap();
 
     parser_init();
-    SnortConfig* sc = ParseSnortConf(snort_cmd_line_conf, fname);
+    SnortConfig* sc = ParseSnortConf(snort_cmd_line_conf, fname, false);
     sc->merge(snort_cmd_line_conf);
 
-    if ( ModuleManager::get_errors() || !sc->verify() )
+    if ( get_parse_errors() || ModuleManager::get_errors() || !sc->verify() )
     {
         parser_term(sc);
         delete sc;
index 8f235962d0ca3b7849a4cf86c92afd67da3c5cd3..a11b8d5321fe6b9615503edc3dc8f62458acceb4 100644 (file)
@@ -249,10 +249,9 @@ static void DefineIfaceVar(SnortConfig* sc, char* iname, const uint8_t* network,
 // Find all up interfaces and define iface_ADDRESS vars for them
 static void DefineAllIfaceVars(SnortConfig* sc)
 {
-
     // FIXIT-L don't come back here on reload unless we are going to find
     // new ifaces.  Cache retrieved devs so if user is running with dropped
-    // privs and does a reload, we can use previous values 
+    // privs and does a reload, we can use previous values
     static int num_vars = 0;
 
     // should be more than enough to cover the number of interfaces on a machine
@@ -348,16 +347,17 @@ static void printRuleListOrder(RuleListNode* node)
     LogMessage("%s\n", buf);
 }
 
-static void parse_file(SnortConfig* sc, Shell* sh)
+static bool parse_file(SnortConfig* sc, Shell* sh, bool is_fatal)
 {
     const char* fname = sh->get_file();
 
     if ( !fname || !*fname )
-        return;
+        return false;
 
     push_parse_location(fname, 0);
-    sh->configure(sc);
+    bool success = sh->configure(sc, is_fatal);
     pop_parse_location();
+    return success;
 }
 
 //-------------------------------------------------------------------------
@@ -382,7 +382,7 @@ void parser_term(SnortConfig* sc)
     sc->free_rule_state_list();
 }
 
-SnortConfig* ParseSnortConf(const SnortConfig* boot_conf, const char* fname)
+SnortConfig* ParseSnortConf(const SnortConfig* boot_conf, const char* fname, bool is_fatal)
 {
     SnortConfig* sc = new SnortConfig(SnortConfig::get_conf()->proto_ref);
 
@@ -440,8 +440,11 @@ SnortConfig* ParseSnortConf(const SnortConfig* boot_conf, const char* fname)
             break;
 
         set_policies(sc, sh);
-        parse_file(sc, sh);
+
+        if (!parse_file(sc, sh, is_fatal))
+            return sc;
     }
+
     set_default_policy(sc);
     return sc;
 }
@@ -737,7 +740,7 @@ RuleTreeNode* deleteRtnFromOtn(OptTreeNode* otn, PolicyId policyId, SnortConfig*
 
         if ( remove && rtn )
         {
-            RuleTreeNodeKey key{ rtn, policyId };
+            RuleTreeNodeKey key { rtn, policyId };
             if ( sc && sc->rtn_hash_table )
                 xhash_remove(sc->rtn_hash_table, &key);
         }
@@ -753,7 +756,7 @@ RuleTreeNode* deleteRtnFromOtn(OptTreeNode* otn, SnortConfig* sc)
     return deleteRtnFromOtn(otn, get_ips_policy()->policy_id, sc);
 }
 
-static uint32_t rtn_hash_func(HashFnc*, const unsigned char *k, int)
+static uint32_t rtn_hash_func(HashFnc*, const unsigned chark, int)
 {
     uint32_t a,b,c;
     const RuleTreeNodeKey* rtnk = (const RuleTreeNodeKey*)k;
@@ -774,7 +777,7 @@ static uint32_t rtn_hash_func(HashFnc*, const unsigned char *k, int)
     return c;
 }
 
-static int rtn_compare_func(const void *k1, const void *k2, size_t)
+static int rtn_compare_func(const void* k1, const void* k2, size_t)
 {
     const RuleTreeNodeKey* rtnk1 = (const RuleTreeNodeKey*)k1;
     const RuleTreeNodeKey* rtnk2 = (const RuleTreeNodeKey*)k2;
@@ -787,7 +790,7 @@ static int rtn_compare_func(const void *k1, const void *k2, size_t)
 
     if (same_headers(rtnk1->rtn, rtnk2->rtn))
         return 0;
-    
+
     return 1;
 }
 
@@ -854,7 +857,7 @@ int addRtnToOtn(SnortConfig* sc, OptTreeNode* otn, RuleTreeNode* rtn, PolicyId p
     return 0; //success
 }
 
-int addRtnToOtn(SnortConfig*sc, OptTreeNode* otn, RuleTreeNode* rtn)
+int addRtnToOtn(SnortConfig* sc, OptTreeNode* otn, RuleTreeNode* rtn)
 {
     return addRtnToOtn(sc, otn, rtn, get_ips_policy()->policy_id);
 }
index a6411b57317738aee1be362dbebb95a26b425b88..bbb2dba42e438e73d309a0e4633f72dbe08d7ffa 100644 (file)
@@ -41,7 +41,8 @@ void push_parse_location(const char* name, unsigned line = 1);
 void pop_parse_location();
 void inc_parse_position();
 
-snort::SnortConfig* ParseSnortConf(const snort::SnortConfig*, const char* fname = nullptr);
+snort::SnortConfig* ParseSnortConf(const snort::SnortConfig*, const char* fname = nullptr,
+    bool is_fatal = true);
 void ParseRules(snort::SnortConfig*);
 
 void OrderRuleLists(snort::SnortConfig*, const char*);