@@ -39,25 +39,41 @@ license = "GNU General Public License v3.0 -- See https://www.gnu.org/licenses"
39
39
categories = {" default" , " safe" }
40
40
41
41
42
- getDefaultConfPath = function (pathSeparator )
43
- local confPath = nil
44
- local scriptPath = debug.getinfo (1 ," S" ).source :sub (2 )
45
- local scriptPathSplit = {}
46
- for i in scriptPath :gmatch (" [^" .. pathSeparator .. " ]+" ) do
47
- table.insert (scriptPathSplit , i )
48
- end
49
- table.remove (scriptPathSplit )
50
- if # scriptPathSplit == 0 then
51
- table.insert (scriptPathSplit , " ." )
42
+ exists = function (path )
43
+ local ok , err , code = os.rename (path , path )
44
+ if not ok then
45
+ if code == 13 then
46
+ -- Permission denied, but it exists
47
+ return true
48
+ else
49
+ return false
50
+ end
51
+ else
52
+ return true
52
53
end
53
- for _ , v in pairs (scriptPathSplit ) do
54
- if confPath == nil then
55
- confPath = v
54
+ end
55
+
56
+ getDefaultConfPath = function (pathSeparator )
57
+ if pathSeparator == " /" then
58
+ if exists (" /usr/share/nmap" ) then
59
+ return " /usr/share/nmap/scripts/nlist.conf"
60
+ elseif exists (" /usr/local/share/nmap" ) then
61
+ return " /usr/local/share/nmap/scripts/nlist.conf"
62
+ else
63
+ stdnse .debug (1 , " Scripts directory location unknown" )
64
+ end
65
+ elseif pathSeparator == " \\ " then
66
+ if exists (" C:\\ Program Files (x86)\\ Nmap" ) then
67
+ return " C:\\ Program Files (x86)\\ Nmap\\ scripts\\ nlist.conf"
68
+ elseif exists (" C:\\ Program Files\\ Nmap" ) then
69
+ return " C:\\ Program Files\\ Nmap\\ scripts\\ nlist.conf"
56
70
else
57
- confPath = confPath .. pathSeparator .. v
71
+ stdnse . debug ( 1 , " Scripts directory location unknown " )
58
72
end
73
+ else
74
+ stdnse .debug (1 , " Unknown OS" )
59
75
end
60
- return confPath .. pathSeparator .. " nlist.conf "
76
+ return nil
61
77
end
62
78
63
79
getHomeConfPath = function (pathSeparator )
@@ -72,7 +88,7 @@ getHomeConfPath = function(pathSeparator)
72
88
end
73
89
end
74
90
75
- parseConfFile = function (confFile )
91
+ parseConfFile = function (confFile , confPath )
76
92
if not confFile then
77
93
stdnse .debug (1 , " Could not open config file: '%s'" , confPath )
78
94
return false , string.format (" Config file '%s' could not be opened" , confPath )
@@ -118,10 +134,13 @@ action = function(host, port)
118
134
if not confFile then
119
135
stdnse .debug (1 , " No '.nlist' file found in home directory" )
120
136
confPath = getDefaultConfPath (pathSeparator )
137
+ if not confPath then
138
+ return " Could not open config file"
139
+ end
121
140
confFile = io.open (confPath , " r" )
122
141
end
123
142
stdnse .debug (1 , " Using config file: '%s'" , confPath )
124
- local status , config = parseConfFile (confFile )
143
+ local status , config = parseConfFile (confFile , confPath )
125
144
if not status then
126
145
return config
127
146
end
@@ -153,6 +172,9 @@ action = function(host, port)
153
172
if config [" use_default_rules" ] then
154
173
stdnse .debug (1 , " Loading default rules" , confPath )
155
174
local defConfPath = getDefaultConfPath (pathSeparator )
175
+ if not defConfPath then
176
+ return " Could not open default config file"
177
+ end
156
178
local confFile = io.open (defConfPath , " r" )
157
179
local status , conf = parseConfFile (confFile )
158
180
defConfig = conf
0 commit comments