#include "shell.h"
+#include <libgen.h>
+
#include <cassert>
#include <cstring>
#include <stdexcept>
#include "main/policy.h"
#include "main/snort_config.h"
#include "managers/module_manager.h"
+#include "parser/parser.h"
using namespace std;
lua_atpanic(lua, Shell::panic);
luaL_openlibs(lua);
+ char pwd[PATH_MAX];
+ parse_from = getcwd(pwd, sizeof(pwd));
+
if ( s )
- file = s;
+ set_file(s);
loaded = false;
}
void Shell::set_file(const char* s)
{
assert(file.empty());
- file = s;
+
+ if ( s && s[0] != '/' && parsing_follows_files )
+ {
+ file += parse_from;
+ file += '/';
+ }
+
+ file += s;
}
void Shell::set_overrides(const char* s)
set_network_policy(pt->second->network);
}
- config_lua(lua, file.c_str(), overrides);
+ const char* base_name = push_relative_path(file.c_str());
+ config_lua(lua, base_name, overrides);
+
set_default_policy(sc);
ModuleManager::set_config(nullptr);
loaded = true;
+
+ pop_relative_path();
}
void Shell::install(const char* name, const luaL_Reg* reg)
bool loaded;
lua_State* lua;
std::string file;
+ std::string parse_from;
std::string overrides;
};
{ "--pause", Parameter::PT_IMPLIED, nullptr, nullptr,
"wait for resume/quit command before processing packets/terminating", },
+ { "--parsing-follows-files", Parameter::PT_IMPLIED, nullptr, nullptr,
+ "parse relative paths from the perspective of the current configuration file" },
+
{ "--pcap-file", Parameter::PT_STRING, nullptr, nullptr,
"<file> file that contains a list of pcaps to read - read mode is implied" },
else if ( v.is("--pause") )
sc->run_flags |= RUN_FLAG__PAUSE;
+ else if ( v.is("--parsing-follows-files") )
+ parsing_follows_files = true;
+
else if ( v.is("--pcap-file") )
{
Trough::add_source(Trough::SOURCE_FILE_LIST, v.get_string());
#include "module_manager.h"
+#include <libgen.h>
#include <lua.hpp>
#include <cassert>
#include <iostream>
#include <mutex>
+#include <stack>
#include <string>
#include "framework/base_api.h"
return set_value(fqn, v);
}
+struct DirStackItem
+{
+ string previous_dir;
+ string base_name;
+};
+
+static std::stack<DirStackItem> dir_stack;
+
+SO_PUBLIC const char* push_relative_path(const char* file)
+{
+ if ( !parsing_follows_files )
+ return file;
+
+ dir_stack.push(DirStackItem());
+ DirStackItem& dsi = dir_stack.top();
+
+ char pwd[PATH_MAX];
+
+ if ( getcwd(pwd, sizeof(pwd)) == nullptr )
+ FatalError("Unable to determine process running directory\n");
+
+ dsi.previous_dir = pwd;
+
+ char* base_name_buf = snort_strdup(file);
+ dsi.base_name = basename(base_name_buf);
+ snort_free(base_name_buf);
+
+ char* dir_name_buf = snort_strdup(file);
+ char* dir_name = dirname(dir_name_buf);
+
+ if ( chdir(dir_name) != 0 )
+ FatalError("Unable to access %s\n", dir_name);
+
+ snort_free(dir_name_buf);
+
+ return dsi.base_name.c_str();
+}
+
+SO_PUBLIC void pop_relative_path()
+{
+ if ( !parsing_follows_files )
+ return;
+
+ assert( !dir_stack.empty() );
+
+ // We came from this directory, so it should still exist
+ const char* prev_dir = dir_stack.top().previous_dir.c_str();
+ if ( chdir(prev_dir) != 0 )
+ FatalError("Unable to access %s\n", prev_dir);
+
+ dir_stack.pop();
+}
+
static bool comp_mods(const ModHook* l, const ModHook* r)
{
const Module* lm = l->mod;
static std::set<uint32_t> gids;
};
+extern "C"
+{
+ // returns the correct path component to use for referencing the file
+ const char* push_relative_path(const char*);
+ void pop_relative_path();
+}
+
#endif
bool set_number(const char*, double);
bool set_string(const char*, const char*);
bool set_alias(const char*, const char*);
+const char* push_relative_path(const char*);
+void pop_relative_path();
]]
function include(file)
- dofile(file)
+ local base_name = ffi.C.push_relative_path(file)
+ dofile(ffi.string(base_name))
+ ffi.C.pop_relative_path()
end
function snort_traverse(tab, fqn)
#include "parse_stream.h"
#include "vars.h"
+bool parsing_follows_files = false;
+
static struct rule_index_map_t* ruleIndexMap = nullptr;
static std::string s_aux_rules;
PolicyId policyId;
};
+extern bool parsing_follows_files;
#endif