Index: nse_nmaplib.cc
===================================================================
--- nse_nmaplib.cc (revision 14054)
+++ nse_nmaplib.cc (working copy)
@@ -21,6 +21,9 @@
#include "nse_nmaplib.h"
#include "nse_nsock.h"
+/* This is used to index the registry in nse_main.lua. */
+#define NSE_SELECTED_BY_NAME "NSE_SELECTED_BY_NAME"
+
#define SCRIPT_ENGINE_PUSHSTRING_NOTNULL(c_str, str) if(c_str != NULL) {\
lua_pushstring(L, c_str); \
lua_setfield(L, -2, str); \
@@ -515,7 +518,21 @@
static int l_get_verbosity (lua_State *L)
{
- lua_pushnumber(L, o.verbose);
+ int verbosity;
+
+ verbosity = o.verbose;
+ /* Call the SELECTED_BY_NAME function in nse_main.lua. When a script is
+ selected by name, we lie to it and say the verbosity is one higher than it
+ really is. */
+ lua_getfield(L, LUA_REGISTRYINDEX, NSE_SELECTED_BY_NAME);
+ if (!lua_isnil(L, -1)) {
+ lua_call(L, 0, 1);
+ if (lua_toboolean(L, -1))
+ verbosity += 1;
+ }
+ lua_pop(L, 1);
+
+ lua_pushnumber(L, verbosity);
return 1;
}
Index: nselib/nmap.luadoc
===================================================================
--- nselib/nmap.luadoc (revision 14054)
+++ nselib/nmap.luadoc (working copy)
@@ -23,7 +23,10 @@
--- Returns the verbosity level as a non-negative integer.
--
--- The verbosity level can be set with the -v
option.
+-- The verbosity level can be set with the -v
option. When
+-- a script is given by name with the --script
option, as
+-- opposed to being selected by default or by category, its verbosity
+-- level is automatically increased by one.
-- @return The verbosity level.
-- @usage if nmap.verbosity() > 0 then ... end
function verbosity()
Index: nse_main.lua
===================================================================
--- nse_main.lua (revision 14054)
+++ nse_main.lua (working copy)
@@ -36,6 +36,7 @@
local BASE = "NSE_BASE";
local WAITING_TO_RUNNING = "NSE_WAITING_TO_RUNNING";
local DESTRUCTOR = "NSE_DESTRUCTOR";
+local SELECTED_BY_NAME = "NSE_SELECTED_BY_NAME";
local _R = debug.getregistry(); -- The registry
local _G = _G;
@@ -383,6 +384,11 @@
assert(type(category) == "string", "bad entry in script database");
r_categories[lower(category)] = true; -- Lowercase the entry
end
+
+ -- Was this entry selected by name with the --script option? We record
+ -- whether it was so that scripts so selected can get a verbosity boost.
+ -- See nmap.verbosity.
+ local selected_by_name = false;
-- A matching function for each script rule.
-- If the pattern directly matches a category (e.g. "all"), then
-- we return true. Otherwise we test if it is a filename or if
@@ -395,17 +401,21 @@
pattern = gsub(pattern, "[%^%$%(%)%%%.%[%]%+%-%?]", "%%%1"); -- esc magic
pattern = gsub(pattern, "%*", ".*"); -- change to Lua wildcard
pattern = "^"..pattern.."$"; -- anchor to beginning and end
- return not not find(escaped_basename, pattern);
+ local found = not not find(escaped_basename, pattern);
+ selected_by_name = selected_by_name or found;
+ return found;
end
local env = {m = m};
+ local script;
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);
if t == "file" then
if not files_loaded[path] then
- chosen_scripts[#chosen_scripts+1] = Script.new(path);
+ script = Script.new(path);
+ chosen_scripts[#chosen_scripts+1] = script;
files_loaded[path] = true;
-- do not break so other rules can be marked as used
end
@@ -415,6 +425,12 @@
end
end
end
+ if script then
+ script.selected_by_name = selected_by_name;
+ if script.selected_by_name then
+ print_debug(2, "Script %s was selected by name.", script.basename);
+ end
+ end
end
setfenv(db_closure, {Entry = entry});
@@ -430,7 +446,10 @@
if t == nil then
error("'"..rule.."' did not match a category, filename, or directory");
elseif t == "file" and not files_loaded[path] then
- chosen_scripts[#chosen_scripts+1] = Script.new(path);
+ local script = Script.new(path);
+ script.selected_by_name = true;
+ chosen_scripts[#chosen_scripts+1] = script;
+ print_debug(2, "Script %s was selected by name.", script.filename);
files_loaded[path] = true;
elseif t == "directory" then
for i, file in ipairs(cnse.dump_dir(path)) do
@@ -507,6 +526,9 @@
end
end
end
+ _R[SELECTED_BY_NAME] = function()
+ return current and current.selected_by_name;
+ end
-- Loop while any thread is running or waiting.
while next(running) or next(waiting) do