diff --git a/nse_fs.cc b/nse_fs.cc index 38b882b0e..9552deb12 100644 --- a/nse_fs.cc +++ b/nse_fs.cc @@ -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 /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 /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. * diff --git a/nse_fs.h b/nse_fs.h index 2be2486e7..c9ae45dbe 100644 --- a/nse_fs.h +++ b/nse_fs.h @@ -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); diff --git a/nse_main.cc b/nse_main.cc index c24ed91e7..101c0ecdb 100644 --- a/nse_main.cc +++ b/nse_main.cc @@ -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}, diff --git a/nse_main.lua b/nse_main.lua index db52a0832..cc04f59cc 100644 --- a/nse_main.lua +++ b/nse_main.lua @@ -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");