diff --git a/CHANGELOG b/CHANGELOG
index 6a86965ec..b6618ddaf 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,9 @@
# Nmap Changelog ($Id$); -*-text-*-
+o [NSE] Fixed a bug which caused some NSE scripts to fail, due to the
+ absence of the NSE SCRIPT_NAME environment variable when the scripts
+ are loaded. Michael Pattrick reported the problem. [Djalal]
+
o Fixed some inconsistencies in nmap-os-db reported by Xavier Sudre
from netVigilance.
diff --git a/docs/scripting.xml b/docs/scripting.xml
index a39f2de77..96471a10b 100644
--- a/docs/scripting.xml
+++ b/docs/scripting.xml
@@ -1252,6 +1252,8 @@ action refer to .
"prerule", "hostrule",
"portrule" or
"postrule".
+ This variable is only available after the evaluation of
+ the rule functions.
diff --git a/nse_main.lua b/nse_main.lua
index 170f0f860..6772f22ce 100644
--- a/nse_main.lua
+++ b/nse_main.lua
@@ -303,6 +303,7 @@ do
local script_type = assert(NSE_SCRIPT_RULES[rule]);
if not self[rule] then return nil end -- No rule for this script?
local file_closure = self.file_closure;
+ -- Rebuild the environment for the running thread.
local env = {
SCRIPT_PATH = self.filename,
SCRIPT_NAME = self.short_basename,
@@ -366,10 +367,15 @@ do
"Warning: Loading '%s' -- the recommended file extension is '.nse'.",
filename);
end
+ local basename = match(filename, "[/\\]([^/\\]-)$") or filename;
+ local short_basename = match(filename, "[/\\]([^/\\]-)%.nse$") or
+ match(filename, "[/\\]([^/\\]-)%.[^.]*$") or
+ filename;
local file_closure = assert(loadfile(filename));
-- Give the closure its own environment, with global access
local env = {
- filename = filename,
+ SCRIPT_PATH = filename,
+ SCRIPT_NAME = short_basename,
dependencies = {},
};
setmetatable(env, {__index = _G});
@@ -412,11 +418,9 @@ do
-- Return the script
local script = {
filename = filename,
- basename = match(filename, "[/\\]([^/\\]-)$") or filename,
- short_basename = match(filename, "[/\\]([^/\\]-)%.nse$") or
- match(filename, "[/\\]([^/\\]-)%.[^.]*$") or
- filename,
- id = match(filename, "^.-[/\\]([^\\/]-)%.nse$") or filename,
+ basename = basename,
+ short_basename = short_basename,
+ id = match(filename, "^.-[/\\]([^\\/]-)%.nse$") or short_basename,
file_closure = file_closure,
prerule = prerule,
hostrule = hostrule,
diff --git a/scripts/ssh-hostkey.nse b/scripts/ssh-hostkey.nse
index ba859fe69..187f9af5c 100644
--- a/scripts/ssh-hostkey.nse
+++ b/scripts/ssh-hostkey.nse
@@ -51,7 +51,7 @@ else
portrule = function() return false end
action = function() end
stdnse.print_debug( 3, "Skipping %s script because OpenSSL is missing.",
- filename)
+ SCRIPT_NAME)
return;
end