summaryrefslogtreecommitdiffstats
path: root/openssh-model.lua
blob: 83fba22fd5fba3c8c1e454029a0784b0cfa5983d (plain)
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
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"
local header = "SSH"

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)
	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