1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
module(..., package.seeall)
-- Load libraries
require("modelfunctions")
require("validator")
require("fs")
require("posix")
require("getopts")
-- Set variables
local configfile = "/etc/ssh/sshd_config"
local processname = "sshd"
local packagename = "openssh-server"
local header = "SSH"
local path="PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin "
local default = {
Port = 22,
ListenAddress = "0.0.0.0",
-- PermitRootLogin = true,
PasswordAuthentication = true,
UseDNS = true,
}
-- ################################################################################
-- LOCAL FUNCTIONS
local function parseconfigfile(file)
file = file or ""
local retval = {}
for line in string.gmatch(file, "([^\n]+)\n?") do
line = string.gsub(line, "#.*$", "")
if line and line ~= "" then
table.insert(retval, {})
for word in string.gmatch(line, "%S+") do
table.insert(retval[#retval], word)
end
end
end
return retval
end
-- return "Yes" or "No" on true/false or value as string
local function config_value(value)
if type(value) == "boolean" then
if value then
return "Yes"
else
return "No"
end
end
return tostring(value)
end
local function validateconfig(config)
if config.ListenAddress and not validator.is_ipv4(config.ListenAddress) then
return false, { ['ListenAddress'] = "You entered invalid IP", }
end
if config.Port and not validator.is_port(config.Port) then
return false, { ['Port'] = "You entered invalid Port", }
end
return true
end
-- ################################################################################
-- PUBLIC FUNCTIONS
-- valid keywords and default config
function read_config()
local conf = {}
local f = io.open(configfile, "r")
local line, key, _, k, v
if not f then
return nil
end
-- clone default conf
for k, v in pairs(default) do
conf[k] = v
end
for line in f:lines() do
line = string.gsub(line, "#.*", "")
for key, _ in pairs(default) do
local k,v = string.match(line, "^("..key..")%s+(.*)")
if k then
conf[k] = v
end
end
end
f:close()
return conf
end
function startstop_service(action)require("getopts")
return modelfunctions.startstop_service(processname, action)
end
function getstatus()
return modelfunctions.getstatus(processname, packagename, header .. " status")
end
function getconfigfile()
return modelfunctions.getfiledetails(configfile)
end
function setconfigfile(filedetails)
filedetails.value.filename.value = configfile
return modelfunctions.setfiledetails(filedetails)
end
function write_config(config)
local k, v, lines, i,j
local errtxt = {}
local conf = {}
local validated, errtxt = validateconfig(config)
if not validated then
return false, errtxt
end
-- filter out unsupported keys
for k,v in pairs(default) do
if (config[k] == nil) or (config[k] == "") then
conf[k] = "no"
else
conf[k] = config[k]
end
end
lines = fs.read_file_as_array(configfile) or {}
for i, j in ipairs(lines) do
for k, v in pairs(conf) do
if string.match(j, "^#?"..k.."%s+") then
lines[i] = k .. " " .. config_value(v)
conf[k] = nil
end
end
end
-- append config opts to end
for k,v in pairs(conf) do
table.insert(lines, k .. " " .. config_value(v))
end
-- write file
posix.mkdir(posix.dirname(configfile))
local f = io.open(configfile, "w")
for _,i in ipairs(lines) do
f:write(i.."\n")
end
f:close()
return true
end
function list_conn_peers()
local output = {}
local netstat = {}
local ps = {}
local who = {}
config = read_config()
local f = io.popen( path .. 'netstat -lna | grep ' .. tostring(config.Port) .. ' | grep "ESTABLISHED"' )
for line in f:lines() do
local peer = string.match(line, "^%S*%s*%S*%s*%S*%s*%S*%s*(%S*)")
peer = string.match(peer, "^.*:(%S-):%d*$")
if (peer) then
if not (netstat[peer]) then netstat[peer] = {cnt=0} end
netstat[peer]['cnt'] = (tonumber(netstat[peer]['cnt']) + 1)
end
end
f:close()
local f = io.popen( path .. 'ps | grep "sshd:" | grep -v "grep"' )
for line in f:lines() do
table.insert(ps, string.match(line,".-%@(%S*)$"))
end
f:close()
for peer,v in pairs(netstat) do
if not (netstat[peer]['tty']) then netstat[peer]['tty'] = {} end
local f = io.popen( path .. 'who | grep "' .. tostring(peer) .. '" | egrep "' .. table.concat(ps, "|") .. '"' )
for line in f:lines() do
local user,tty,idle,time = string.match(line, "^(%S*)%s*(%S*)%s*(%S*)%s*(%S*%s*%S*%s*%S*)")
table.insert(netstat[peer]['tty'], {
user=user,
tty=tty,
idle=idle,
time=time,
})
end
f:close()
end
for k,v in pairs(netstat) do
table.insert(output, v)
output[#output]['host'] = k
end
return output
end
|