1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 13:11:28 +00:00

Add a new cnse.fetchscript, which first checks for an absolute path,

then looks in the scripts subdirectory, then in the current directory.
cnse.fetchfile_absolute now checks for an absolute path, then calls
nmap_fetchfile if that fails (and no longer looks in scripts/). Use
cnse.fetchscript when accessing files that should be in the scripts
subdirectory.
This commit is contained in:
david
2011-03-26 06:48:29 +00:00
parent c8f79b8afe
commit a9a84873f9
4 changed files with 43 additions and 14 deletions

View File

@@ -50,19 +50,35 @@ static bool filename_is_absolute(const char *file) {
return false;
}
static int nse_fetchfile(char *path, size_t path_len, const char *file) {
int type = nmap_fetchfile(path, path_len, file);
/* This is a modification of nmap_fetchfile specialized to look for files
* in the scripts subdirectory. If the path is absolute, it is always tried
* verbatim. Otherwise, the file is looked for under scripts/, and then finally
* in the current directory.
*/
static int nse_fetchscript(char *path, size_t path_len, const char *file) {
std::string scripts_path = std::string(SCRIPT_ENGINE_LUA_DIR) + std::string(file);
int type;
// lets look in <nmap>/scripts too
if(type == 0) {
std::string alt_path = std::string(SCRIPT_ENGINE_LUA_DIR) + std::string(file);
type = nmap_fetchfile(path, path_len, alt_path.c_str());
if (filename_is_absolute(file)) {
if (o.debugging > 1)
log_write(LOG_STDOUT, "%s: Trying absolute path %s\n", SCRIPT_ENGINE, file);
Strncpy(path, file, path_len);
return nmap_fileexistsandisreadable(file);
}
// lets look in <path>/scripts
type = nmap_fetchfile(path, path_len, scripts_path.c_str());
if (type == 0) {
// current directory
Strncpy(path, file, path_len);
return nmap_fileexistsandisreadable(file);
}
return type;
}
/* This is a modification of nse_fetchfile that first looks for an
/* This is a modification of nmap_fetchfile that first looks for an
* absolute file name.
*/
static int nse_fetchfile_absolute(char *path, size_t path_len, const char *file) {
@@ -73,13 +89,13 @@ static int nse_fetchfile_absolute(char *path, size_t path_len, const char *file)
return nmap_fileexistsandisreadable(file);
}
return nse_fetchfile(path, path_len, file);
return nmap_fetchfile(path, path_len, file);
}
int fetchfile_absolute (lua_State *L)
static int nse_fetch (lua_State *L, int (*fetch)(char *, size_t, const char *))
{
char path[MAXPATHLEN];
switch (nse_fetchfile_absolute(path, sizeof(path), luaL_checkstring(L, 1)))
switch (fetch(path, sizeof(path), luaL_checkstring(L, 1)))
{
case 0: // no such path
lua_pushnil(L);
@@ -94,11 +110,21 @@ int fetchfile_absolute (lua_State *L)
lua_pushstring(L, path);
break;
default:
return luaL_error(L, "nse_fetchfile_absolute returned bad code");
return luaL_error(L, "nse_fetch returned bad code");
}
return 2;
}
int fetchscript (lua_State *L)
{
return nse_fetch(L, nse_fetchscript);
}
int fetchfile_absolute (lua_State *L)
{
return nse_fetch(L, nse_fetchfile_absolute);
}
/* LuaFileSystem directory iterator port.
*

View File

@@ -1,6 +1,8 @@
#ifndef NSE_FS
#define NSE_FS
int fetchscript (lua_State *L);
int fetchfile_absolute (lua_State *L);
int nse_readdir (lua_State *L);

View File

@@ -257,6 +257,7 @@ static void open_cnse (lua_State *L)
{
static const luaL_Reg nse[] = {
{"fetchfile_absolute", fetchfile_absolute},
{"fetchscript", fetchscript},
{"dir", nse_readdir},
{"nsock_loop", nsock_loop},
{"key_was_pressed", key_was_pressed},

View File

@@ -540,7 +540,7 @@ local function get_chosen_scripts (rules)
for globalized_rule, rule_table in pairs(entry_rules) do
if setfenv(rule_table.compiled_rule, env)() then -- run the compiled rule
used_rules[rule_table.original_rule] = true;
local t, path = cnse.fetchfile_absolute(filename);
local t, path = cnse.fetchscript(filename);
if t == "file" then
if not files_loaded[path] then
script = Script.new(path);
@@ -568,9 +568,9 @@ local function get_chosen_scripts (rules)
-- Now load any scripts listed by name rather than by category.
for rule, loaded in pairs(used_rules) do
if not loaded then -- attempt to load the file/directory
local t, path = cnse.fetchfile_absolute(rule);
local t, path = cnse.fetchscript(rule);
if t == nil then -- perhaps omitted the extension?
t, path = cnse.fetchfile_absolute(rule..".nse");
t, path = cnse.fetchscript(rule..".nse");
end
if t == nil then
error("'"..rule.."' did not match a category, filename, or directory");