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