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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
module (..., package.seeall)
-- Load libraries
require("fs")
require("format")
require("getopts")
require("daemoncontrol")
-- Set variables
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 availablemedias()
return {"floppy","usb"}
end
local function getLbuCommit(flag)
local err = {}
local ret = ""
local f = io.popen("/sbin/lbu commit " .. flag .. " 2>&1", "r")
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
local descrtxt
if (#list() == 0) then
statustxt = "There is no uncommited files"
else
statustxt = "WARNING!"
descrtxt = "Until you commit, you will lose your changes at next reboot/shutdown!"
end
local config = getconfig()
status["version"] = cfe({
name="status",
value=get_version(),
label="Program version",
})
status["status"] = cfe({
name="status",
value=statustxt,
descr=descrtxt,
label="Program status",
})
status["LBU_MEDIA"] = config["LBU_MEDIA"]
if (config["ENCRYPTION"]["checked"]) then
status["ENCRYPTION"] = config["ENCRYPTION"]
status["ENCRYPTION"]["descr"] = "Using cipher '".. config.DEFAULT_CIPHER.value .. "' for encryption"
end
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)
--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 configopts = {}
local errors = {}
if (fs.is_file(path)) then
configopts = getopts.getoptsfromfile(path, "") or {}
end
local lbumedias = availablemedias()
if not (configopts.LBU_MEDIA) then
local lbumessage = "No default"
table.insert(lbumedias, lbumessage)
config.LBU_MEDIA = lbumessage
else
config.LBU_MEDIA = configopts.LBU_MEDIA
end
config["debug"] = configopts
config["LBU_MEDIA"] = cfe({
name="LBU_MEDIA",
value=config["LBU_MEDIA"],
label="Default media for commit",
type="select",
option=lbumedias,
})
config["lbu_included"]= cfe({
name="lbu_included",
option=getincexl("include") or {},
label="Included item(s)",
type="select",
size=table.maxn(getincexl("include"))+1,
})
if (config["lbu_included"]["size"] == 1) then config["lbu_included"]["size"] = 2 end
config["lbu_excluded"]= cfe({
name="lbu_excluded",
option=getincexl("exclude") or {},
label="Excluded item(s)",
type="select",
size=table.maxn(getincexl("exclude"))+1,
})
if (config["lbu_excluded"]["size"] == 1) then config["lbu_excluded"]["size"] = 2 end
config["ENCRYPTION"] = cfe({
name="ENCRYPTION",
value="yes",
label="Password protected commits",
type="checkbox",
})
if (configopts["ENCRYPTION"]) and (#configopts["ENCRYPTION"] ~= 0) then
config["ENCRYPTION"]["checked"] = "yes"
end
config["DEFAULT_CIPHER"] = cfe({
name="DEFAULT_CIPHER",
value=configopts["DEFAULT_CIPHER"],
label="Cipher to use at encryption",
option=getciphers() or {},
type="select",
})
config["PASSWORD"] = cfe({
name="PASSWORD",
value=configopts["PASSWORD"],
label="Password when encrypting",
})
-- Next section is to print errormessages when configs are wrong
if (configopts["LBU_MEDIA"] == "") or (configopts["LBU_MEDIA"] == nil) then
config.LBU_MEDIA.errtxt = "'Media' needs to be configured!"
end
if (configopts["PASSWORD"] == nil) and (configopts["ENCRYPTION"] ~= nil) then
config.PASSWORD.errtxt = "Encryption without password is not allowed!<BR> Deactivate 'Password protection' or configure a password!"
end
for k,v in pairs(errors) do
config["errors"] = v
end
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 included|excluded 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
if (variable == "LBU_MEDIA") then
local errtxt
local lbumedias = availablemedias()
for k,v in pairs(lbumedias) do
errtxt = ""
if (value == v) then break end
errtxt = "Not a valid media!"
end
if (#errtxt > 0) then
return errtxt
end
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 value
end
function get_filedetails()
local path = configfile
local file = {}
local filedetails = {}
local config = {}
local filenameerrtxt
if (fs.is_file(path)) then
filedetails = fs.stat(path)
config = getconfig(path)
else
config = {}
config.filename = {}
config["filename"]["errtxt"]="Config file '".. path .. "' is missing!"
end
file["filename"] = cfe({
name="filename",
label="File name",
value=path,
errtxt=filenameerrtxt
})
file["filesize"] = cfe({
name="filesize",
label="File size",
value=filedetails.size or 0,
})
file["mtime"] = cfe({
name="mtime",
label="File date",
value=filedetails.mtime or "---",
})
file["filecontent"] = cfe({
type="longtext",
name="filecontent",
label="File content",
value=fs.read_file(path),
})
-- Sum all errors into one cfe
local sumerrors = ""
for k,v in pairs(config) do
if (config[k]) and (config[k]["errtxt"]) and (config[k]["errtxt"] ~= "") then
sumerrors = sumerrors .. config[k]["errtxt"] .. "\n"
end
end
if (sumerrors ~= "") then
file["sumerrors"] = cfe ({
name="sumerrors",
label = "Configuration errors",
errtxt = string.match(sumerrors, "(.-)\n$"),
})
end
return file
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
|