summaryrefslogtreecommitdiffstats
path: root/lbu-model.lua
blob: f38d34d0ce5b7b02b74e0898a76e963169c6b690 (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
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
210
211
212
213
214
215
216
217
218
219
220
module (..., package.seeall)

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

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 err = {}
	local ret = ""
	local f = io.popen("/sbin/lbu commit " .. flag .. " 2>&1", "r")
--	local ret = f:read("*a")
	for line in f:lines() do
		ret = ret .. line .. "\n"
		--Look for error messages in output
		local searchrow, search = string.match(line, "^(lbu.*(%-%a).*)")
		if (search) then err[search] = searchrow end
	end
	f:close()
	return ret, err
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

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

function getstatus ()
	local path = configfile
	local status = {}
	local statustxt = nil
	if (#list() == 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, errors = getconfig()
	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, errors
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)
	--See to that only allowed flags are passed to the process
	flag = string.match(flag or "", "%-%a") or ""
	return getLbuCommit("-v " .. flag)
end
function getsimulate(self, flag)
	--See to that only allowed flags are passed to the process
	flag = string.match(flag or "", "%-%a") or ""
	return getLbuCommit("-n " .. flag)
end

function getconfig ()
	local path = configfile
	local config = {}
	local errors = {}
	local lbumedias = {}
	table.insert(lbumedias, {name="floppy", value="floppy"})
	table.insert(lbumedias, {name="usb", value="usb"})
	if (fs.is_file(path)) then
		config = getopts.getoptsfromfile(path) or {}
	end
	config["lbu_included"]= getincexl("include") or {}
	config["lbu_excluded"]= getincexl("exclude") or {}
	config["LBU_MEDIA_LIST"]= lbumedias or {}
	config["DEFAULT_CIPHER_LIST"]= getciphers() or {}
	-- Next section is to set/show default values when a option is not configured in the configfile
	config["LBU_MEDIA"] = config["LBU_MEDIA"] or ""
	config["DEFAULT_CIPHER"] = config["DEFAULT_CIPHER"] or ""
	-- Next section is to print errormessages when configs are wrong
	if (config["LBU_MEDIA"] == "") or (config["LBU_MEDIA"] == nil) then
		errors["LBU_MEDIA"] = "'Media' needs to be configured!"
	end
	if (config["PASSWORD"] == nil) and (config["ENCRYPTION"] ~= nil) then
		errors["PASSWORD"] = "Encryption without password is not allowed!<BR> Deactivate 'Password protection' or configure a password!"
	end
	for k,v in pairs(errors) do
		errors["last"] = v
	end

	return config, errors
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)
	local configfilecontent = nil
	local path = configfile
	local cmdoutput = nil
	if not (fs.is_file(path)) then
		fs.write_file(path,"")
	end
	local deactivate = ""
	if not (value) or (value == "") then
		deactivate = "#"	
	end
	-- Hardcode some parameters (e.g for some checkboxes)
	if (variable == "ENCRYPTION") then value = "$DEFAULT_CIPHER" end
	configfilecontent = fs.read_file_as_array(path) or {}
	-- Search if the variable is defined in the file
	for k,v in pairs(configfilecontent) do
		if (string.match(v,"^%s*%#*%s*" .. variable)) then
			cmdoutput = format.search_replace(configfilecontent,"^%s*%#*%s*" .. variable .. ".*$", deactivate .. variable .. "=" .. value)
			break
		end
	end
	--If variable is not changed (didn't exist) then create a new row with this variable
	if not (cmdoutput) then
		cmdoutput = configfilecontent
		table.insert(cmdoutput,deactivate .. variable .. "=" .. (value or ""))
	end
	-- Write changes to file
	fs.write_file(path,table.concat(cmdoutput,"\n"))
	return cmdoutput
end
function get_filedetails()
	local filedetails = {}
	local path = configfile
	filedetails.details = {path=path, size="0",mtime=""}
	filedetails.content = ""
	if (fs.is_file(path)) then
		filedetails.details = fs.stat(path)
		filedetails.content = fs.read_file(path)
	end
	return filedetails
end
function update_filecontent (self, modifications)
	local path = configfile
	local file_result,err = fs.write_file(path, format.dostounix(modifications))
	return file_result, err
end