summaryrefslogtreecommitdiffstats
path: root/openntpd-model.lua
blob: 742c64abef1b8e7ae713b3704bd529b22949f2b4 (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
module (..., package.seeall)

require("format")
require("fs")

local ntpdconfig = "/etc/ntpd.conf"
local progname = "openntpd"

local function config_content( f )
	local config = {}
	config.name = f
	local conf_file = fs.read_file_as_array ( config.name )
	for i=1,table.maxn(conf_file) do
		local l = conf_file[i]
		-- Filter out commented lines
		if not string.find ( l, "^[;#].*" ) then
			local a,b,c = string.match ( l, "^%s*(%S+)%s*(%S*)%s*(%S*).*$" )
			if (a) then
				if not (config[string.lower(a)]) then
					config[string.lower(a)] = {}
				end
				if (string.lower(a) == "listen") then
					table.insert (config[string.lower(a)], {value=c} )
				else
					table.insert (config[string.lower(a)], {value=b} )
				end
			end
		end
	end
	return config
end

local function file_info ( path )
	require("posix")
	local filedetails = posix.stat(path)	
	filedetails["owner"]=rawget((posix.getpasswd(filedetails["uid"])),"name")
	filedetails["group"]=rawget((posix.getgroup(filedetails["gid"])),"name")
	filedetails["atimelong"]=os.date("%c", filedetails["atime"])
	filedetails["mtimelong"]=os.date("%c", filedetails["mtime"])
	filedetails["longname"]=path
	filedetails["name"]=basename(path)

	if ( filedetails["size"] > 1073741824 ) then
		filedetails["size"]=((filedetails["size"]/1073741824) - (filedetails["size"]/1073741824%0.1)) .. "G"
	elseif ( filedetails["size"] > 1048576 ) then
		filedetails["size"]=((filedetails["size"]/1048576) - (filedetails["size"]/1048576%0.1))  .. "M"
	elseif ( filedetails["size"] > 1024 ) then
		filedetails["size"]=((filedetails["size"]/1024) - (filedetails["size"]/1024%0.1)) .. "k"
	else
		filedetails["size"]=filedetails["size"]
	end
	return filedetails
end

local function get_version ()
	local f,error = io.popen("/sbin/apk_version -v -s openntpd")
	local programversion = f:read("*a")
	f:close()
	return programversion
end


-- ################################################################################
-- PUBLIC FUNCTIONS
--[[
function restart_service ()
	local f,err = io.popen("/etc/init.d/shorewall restart")
	local restart = f:read("*a")
	f:close()
	local status = get_status()
	status.restart = restart
	return status
end
--]]
--[[
function get_status ()
	local f,error = io.popen("/sbin/shorewall status")
	local fake = f:read("*l")
	local fake = f:read("*l")
	local programstatus = f:read("*l")
	local programstate = f:read("*l")
	f:close()
	local f,error = io.popen("/sbin/shorewall version")
	local programversion = f:read("*l")
	f:close()
	return {programversion=programversion,programstatus=programstatus,programstate=programstate}
end
--]]

function get ()
	local path = ntpdconfig
	config = config_content ( path )
	config["version"] = get_version()
	return config
end

function get_filecontent (self)
	local path = ntpdconfig
	file_content = nil
	if (fs.is_file(path)) then
		local filedetails = file_info(path)
		local file = io.open( path )
		local file_result = file:read("*a") or "unknown"
		file:close()
		file_content = {name=name, value=file_result, filedetails=filedetails}
	else
		file_content = {value="", errtxt="File is missing, but will be created when you save your new settings",filedetails={longname=path, size="0", mtimelong=""}}
	end 
	return file_content
end

function update_filecontent (self, modifications)
	local path = ntpdconfig
	local file_result,err = fs.write_file(path, format.dostounix(modifications))
	file_content = get_filecontent()
	return file_content
end