summaryrefslogtreecommitdiffstats
path: root/lbu-model.lua
blob: b0f5cf7f4e0a22b2963ec2329b96b9b086224593 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
module (..., package.seeall)

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

local configfile = "/etc/lbu/lbu.conf"

-- ################################################################################
-- LOCAL FUNCTIONS

local function get_version ()
	local f,error = io.popen("/sbin/lbu 2>&1")
	local programversion = f:read("*l")
	f:close()
	return programversion
end

local function getLbuStatus()
	local ret = {}
	local f = io.popen("/sbin/lbu status -v 2>&1", "r")
	if not (f) then return ret end
	for line in f:lines() do
		if (string.match(line, "^Include files")) then break end
		if (string.match(line, "^Exclude files")) then break end
		local status, name = string.match(line, "^(%S+)%s+(.+)$")
		if (status) and (name) then
			ret[string.gsub('/' .. name, "/+", "/")] = status
		end
	end
	f:close()
	return ret
end

local function getLbuCommit(flag)
	local f = io.popen("/sbin/lbu commit " .. flag .. " 2>&1", "r")
	local ret = f:read("*a")
	f:close()
	return ret
end

local function getciphers()
	local opensslciphers = {}
	local watchdog = nil
	local f = io.popen("/usr/bin/openssl -v 2>&1", "r")
	if not (f) then return ciphers end
	for line in f:lines() do
		if (watchdog) then
			for cipher in string.gmatch(line, "(%S+)") do
				table.insert(opensslciphers,tostring(cipher))
			end
		end
		if (string.match(line, "^Cipher commands")) then watchdog="yes" end
	end
	f:close()
	return opensslciphers
end

local function getincexl(state)
	local incexl = {}
	if (string.lower(state) == "include") or (string.lower(state) == "exclude") then
		local f = io.popen("/sbin/lbu " .. string.lower(state) .. " -l 2>&1", "r")
		if not (f) then return incexl end
		for line in f:lines() do
			table.insert(incexl,tostring(line))
		end
		f:close()
	end
	return incexl
end

local function checkexistens(file,variable)
	local filecontent = fs.read_file_as_array(file)
	for k,v in ipairs(filecontent) do
		return v
	end
	return nil
end

-- ################################################################################
-- PUBLIC FUNCTIONS

function getstatus ()
	local path = configfile
	local status = {}
	local statustxt = nil
	local lbustatus = list()
	if (#lbustatus == 0) then
		statustxt = "OK! (There is no uncommited files)"
	else
		statustxt = "WARNING! (Until you commit, you will lose your changes at next reboot/shutdown!)"
	end
	local config = getopts.getoptsfromfile(path)
	status["LBU_MEDIA"] = config["LBU_MEDIA"]
	status["ENCRYPTION"] = config["ENCRYPTION"]
	status["DEFAULT_CIPHER"] = config["DEFAULT_CIPHER"]
	status["version"] = get_version()
	status["status"] = statustxt
	return status
end

function list(self)
	local ret = {}
	local lbuStatus = getLbuStatus()
	for k,v in pairs(lbuStatus) do
		ret[#ret + 1] = { name=k, status=v }
	end
	table.sort(ret, function(a,b) return (a.name < b.name) end)
	return ret
end

function getcommit(self, flag)
	if flag ~= "-d" then flag = "" end
	return getLbuCommit("-v " .. flag)
end
function getsimulate(self, flag)
	if flag ~= "-d" then flag = "" end
	return getLbuCommit("-n " .. flag)
end

function getconfig ()
	local path = configfile
	local config = {}
	local lbumedias = {}
	table.insert(lbumedias, {name="No default", value=""})
	table.insert(lbumedias, {name="floppy", value="floppy"})
	table.insert(lbumedias, {name="usb", value="usb"})
	config = getopts.getoptsfromfile(path)
	config["lbu_included"]= getincexl("include")
	config["lbu_excluded"]= getincexl("exclude")
	config["LBU_MEDIA_LIST"]= lbumedias
	config["DEFAULT_CIPHER_LIST"]= getciphers()
--	config["debug"] = getciphers()
	return config
end

function lbuincexcl(self,state,value,addremove)
	local incexl = nil
	if (string.lower(addremove or "") == "add") then
		addremove = ""
	elseif (string.lower(addremove or "") == "remove") then
		addremove = "-r"
	else
		return "Function setincexl() - Invalid option! Use add|remove when calling this function!"
	end
	if (string.lower(state) == "include") or (string.lower(state) == "exclude") then
		local f = io.popen("/sbin/lbu " .. string.lower(state) .. " " .. addremove .. " -v " .. value .." 2>&1", "r")
		if not (f) then return incexl end
		incexl = f:read("*a")
		f:close()
	else
		return "Function setincexl() - Invalid command! Use include|exclude when calling this function!"
	end
	return incexl
end

function editconfig (self,variable,value,state)
	local configfilecontent = nil
	local path = configfile
	local cmdoutput = {}
	if (state == "change_value" ) then
		configfilecontent = fs.read_file_as_array(configfile)
		if not (value) or (value == "") then
			cmdoutput = format.search_replace(configfilecontent,"^%s*%#*%s*" .. variable, "#" .. variable)
		else
			cmdoutput = format.search_replace(configfilecontent,"^%s*%#*%s*" .. variable .. ".*$", variable .. "=" .. value)
		end
		fs.write_file(configfile,table.concat(cmdoutput,"\n"))
	elseif (state == "change_state" ) then
		configfilecontent = fs.read_file_as_array(configfile)
		if not (value) or (value == "") then
			cmdoutput = format.search_replace(configfilecontent,"^%s*%#*%s*" .. variable, "#" .. variable)
		else
			cmdoutput = format.search_replace(configfilecontent,"^%s*%#*%s*" .. variable, variable)
		end
		fs.write_file(configfile,table.concat(cmdoutput,"\n"))
	else
		return "Function ediconfig() - Wrong usage of this function! usage editconfig(variable,value,state)\nvariable=Name of the variable\nvalue=The new value (when adding)\nstate=change_value|change_state depending on if you want to add some value or remove some variable."
	end
	return cmdoutput
end