From d8cca8b5c51f2915c3dcf3d8af59e53a032a1a56 Mon Sep 17 00:00:00 2001 From: Seamus Tuohy Date: Tue, 29 Oct 2013 09:21:07 -0400 Subject: [PATCH 1/2] removed commotion helpers function for replacement with new helpers module --- luasrc/commotion_helpers.lua | 201 ----------------------------------- 1 file changed, 201 deletions(-) delete mode 100644 luasrc/commotion_helpers.lua diff --git a/luasrc/commotion_helpers.lua b/luasrc/commotion_helpers.lua deleted file mode 100644 index 533a037..0000000 --- a/luasrc/commotion_helpers.lua +++ /dev/null @@ -1,201 +0,0 @@ -function DIE(str) - luci.http.status(500, "Internal Server Error") - luci.http.write(str) - luci.http.close() -end - ---- Redirects a page to https if the path is within the "node" path. --- @param node node path to check. format as such -> "/NODE/" --- @param env A table containing the REQUEST_URI and the SERVER_NAME. Can take full luci.http.getenv() --- @return True if page is to be redirected. False if path does not include node or if https is already on. -function check_https(node, env) - if string.match(env.REQUEST_URI, node) then - if env.HTTPS ~= "on" then - luci.http.redirect("https://"..env.SERVER_NAME..env.REQUEST_URI) - return true - end - return false - end - return false -end - ---- Gives the md5 and sha1 fingerprints of uhttpd server. ---- Call as such: md5, sha1 = ssl_cert_fingerprints() If you only give one var name it will only give you md5. Call like: _, sha1 = to throw away md5 --- @return md5 and sha1 hash of uhttpd. -function ssl_cert_fingerprints() - --get cert file from /etc/config/uhttpd - local uci = luci.model.uci.cursor() - local cert = uci:get('uhttpd','main','cert') - --get md5 and sha1 hash's of cert file - local md5 = luci.sys.exec("md5sum "..cert) - local sha1 = luci.sys.exec("sha1sum "..cert) - -- remove the filename and extra spaces then uppercase the cert string - sha1 = string.upper(sha1:match("(%w*)%s*"..cert)) - md5 = string.upper(md5:match("(%w*)%s*"..cert)) - --add colons between pairs of two chars - sha1 = sha1:gsub("(%w%w)", "%1:") - md5 = md5:gsub("(%w%w)", "%1:") - --remove the final colon - sha1 = sha1:sub(1, -2) - md5 = md5:sub(1,-2) - return md5, sha1 -end - -function uci_encode(str) - if (str) then - str = string.gsub (str, "([^%w])", function(c) return '_' .. tostring(string.byte(c)) end) - end - return str -end - -function html_encode(str) - return string.gsub(str,"[<>&\n\r\"]",function(c) return html_replacements[c] or c end) -end - -function url_encode(str) - return string.gsub(str,"[<>%s]",function(c) return url_replacements[c] or c end) -end - -function printf(tmpl,t) - return (tmpl:gsub('($%b{})', function(w) return t[w:sub(3, -2)] or w end)) -end - -function log(msg) - if (type(msg) == "table") then - for key, val in pairs(msg) do - log('{') - log(key) - log(':') - log(val) - log('}') - end - else - luci.sys.exec("logger -t luci \"" .. tostring(msg) .. '"') - end -end - -function is_ip4addr(str) - local i,j, _1, _2, _3, _4 = string.find(str, '^(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)$') - if (i and - (tonumber(_1) >= 0 and tonumber(_1) <= 255) and - (tonumber(_2) >= 0 and tonumber(_2) <= 255) and - (tonumber(_3) >= 0 and tonumber(_3) <= 255) and - (tonumber(_4) >= 0 and tonumber(_4) <= 255)) then - return true - end - return false -end - -function is_ip4addr_cidr(str) - local i,j, _1, _2 = string.find(str, '^(.+)/(%d+)$') - if i and is_ip4addr(_1) and tonumber(_2) >= 0 and tonumber(_2) <= 32 then - return true - end - return false -end - -function is_ssid(str) - -- SSID can have almost anything in it - if #tostring(str) < 32 then - return tostring(str):match("[%w%p]+[%s]*[%w%p]*]*") - else - return nil - end -end - -function is_mode(str) - -- Modes are simple, but also match the "-" in Ad-Hoc - return tostring(str):match("[%w%-]*") -end - -function is_chan(str) - -- Channels are plain digits - return tonumber(string.match(str, "[%d]+")) -end - -function is_bitRate(br) - -- Bitrate can start with a space and we want to display Mb/s - return br:match("[%s]?[%d%.]*[%s][%/%a]+") -end - -function is_email(email) - return tostring(email):match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?") -end - -function is_hostname(str) ---alphanumeric and hyphen Less than 63 chars ---cannot start or end with a hyphen - if #tostring(str) < 63 then - return tostring(str):match("^%w[%w%-]*%w$") - else - return nil - end -end - -function is_fqdn(str) --- alphanumeric and hyphen less than 255 chars --- each label must be less than 63 chars - if #tostring(str) < 255 then - -- Should check that each label is < 63 chars -- - return tostring(str):match("^[%w%.%-]+$") - else - return nil - end -end - -function is_macaddr(str) - local i,j, _1, _2, _3, _4, _5, _6 = string.find(str, '^(%x%x):(%x%x):(%x%x):(%x%x):(%x%x):(%x%x)$') - if i then return true end - return false -end - -function is_uint(str) - return str:find("^%d+$") -end - -function is_hex(str) - return str:find("^%x+$") -end - -function is_port(str) - return is_uint(str) and tonumber(str) >= 0 and tonumber(str) <= 65535 -end - -function table.contains(table, element) - for _, value in pairs(table) do - if value == element then - return true - end - end - return false -end - -function list_ifaces() - local uci = luci.model.uci.cursor() - local r = {zone_to_iface = {}, iface_to_zone = {}} - uci:foreach("network", "interface", - function(zone) - if zone['.name'] == 'loopback' then return end - local iface = luci.sys.exec("ubus call network.interface." .. zone['.name'] .. " status |grep '\"device\"' | cut -d '\"' -f 4"):gsub("%s$","") - r.zone_to_iface[zone['.name']]=iface - r.iface_to_zone[iface]=zone['.name'] - end - ) - return r -end - -html_replacements = { - ["<"] = "<", - [">"] = ">", - ["&"] = "&", - ["\n"] = " ", - ["\r"] = " ", - ["\""] = """ - } - -url_replacements = { - ["<"] = "%3C", - [">"] = "%3E", - [" "] = "%20", - ['"'] = "%22" - } From 6da37110571e679aec8e98dbfea7240cd81789b1 Mon Sep 17 00:00:00 2001 From: Seamus Tuohy Date: Wed, 30 Oct 2013 09:52:49 -0400 Subject: [PATCH 2/2] pointed old log references to log function in new modularized commotion helpers package --- luasrc/controller/commotion/meshprofile.lua | 37 +++++++++++-------- .../controller/commotion/serval_keyring.lua | 30 ++++++++------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/luasrc/controller/commotion/meshprofile.lua b/luasrc/controller/commotion/meshprofile.lua index 6a95113..1e14a97 100644 --- a/luasrc/controller/commotion/meshprofile.lua +++ b/luasrc/controller/commotion/meshprofile.lua @@ -15,7 +15,6 @@ module("luci.controller.commotion.meshprofile", package.seeall) require "luci.model.uci" require "luci.fs" require "luci.sys" -require "commotion_helpers" local profileDir = "/etc/commotion/profiles.d/" @@ -30,7 +29,8 @@ function index() end function main(ERR) - log("main started") + local debug = require "luci.commotion.debugger" + debug.log("main started") if not ERR then ERR = nil end @@ -42,7 +42,7 @@ function main(ERR) function(s) if s['.name'] and s.profile then table.insert(available, {s['.name'], s.profile}) - --log(s['.name'] .. " uses " .. s.profile) + --debug.log(s['.name'] .. " uses " .. s.profile) end end) local profiles = {} @@ -57,13 +57,14 @@ function main(ERR) end function ifprocess() - log("Processing profile application...") + local debug = require "luci.commotion.debugger" + debug.log("Processing profile application...") local error = nil local values = luci.http.formvalue() local tif = values["interfaces"] local p = values["profiles"] local uci = luci.model.uci.cursor() - log("Applying " .. p .. " to " .. tif) + debug.log("Applying " .. p .. " to " .. tif) old_prof = uci:get('network', tif, "profile") local wireless = false uci:foreach('wireless','wifi-iface', @@ -98,10 +99,11 @@ function finish() end function checkFile(file) + local debug = require "luci.commotion.debugger" --[=[ Checks the uploaded profile to ensure the required settings are there. --]=] - log("file check started") + debug.log("file check started") local error = nil if luci.fs.isfile(file) then --required fields for a commotion profile @@ -132,7 +134,7 @@ function checkFile(file) end if not contained then table.insert(missing, x) - log("Field "..x.." is missing.") + debug.log("Field "..x.." is missing.") end end end @@ -143,11 +145,11 @@ function checkFile(file) --remove file because it is BAD removed = luci.sys.call('rm ' .. file) else - log("Profile seems to be correctly formatted.") + debug.log("Profile seems to be correctly formatted.") end else error = "There does not seem to be a file here..." - log("File is missing") + debug.log("File is missing") end if error then return error @@ -168,7 +170,8 @@ end function up() --[=[calls the file uploader and checks if the file is a correct config. --]=] - log("up started") + local debug = require "luci.commotion.debugger" + debug.log("up started") local error = nil setFileHandler("/etc/commotion/profiles.d/", "config") local values = luci.http.formvalue() @@ -196,7 +199,7 @@ end function download(filename) --TODO remove the luci.http.status calls and replace them with calls to main(error) with the appropriate text to inform the user of why they cannot download it. - log("download started") + debug.log("download started") -- no file name provided if not filename then luci.http.status(403, "Forbidden") @@ -247,6 +250,7 @@ function setFileHandler(location, input_name, file_name) file_name: (string, optional) The optional name you would like the file to be saved as. If left blank the file keeps its uploaded name. --]=] + local debug = require "luci.commotion.debugger" local sys = require "luci.sys" local fs = require "luci.fs" local configLoc = location @@ -257,14 +261,14 @@ function setFileHandler(location, input_name, file_name) complete = nil if meta and meta.name == input_name then if file_name ~= nil then - log("starting download") + debug.log("starting download") fp = io.open(configLoc .. file_name, "w") else - log("starting download") + debug.log("starting download") fp = io.open(configLoc .. meta.file, "w") end else - log("file not of specified input type (input name variable)") + debug.log("file not of specified input type (input name variable)") end end if chunk then @@ -272,7 +276,7 @@ function setFileHandler(location, input_name, file_name) end if eof then fp:close() - log("file downloaded") + debug.log("file downloaded") end end) end @@ -280,6 +284,7 @@ end function flush_wireless_profile(old_profile, new_profile, interface) --TODO need a userspace warning that channel settings will not take effect and need to be done in the settings page. + local debug = require "luci.commotion.debugger" local uci = luci.model.uci.cursor() local found = nil local old_dev = nil @@ -298,7 +303,7 @@ function flush_wireless_profile(old_profile, new_profile, interface) error = luci.i18n.translate("You have multiple wireless interfaces on a single network interface. This is not allowed.") end end) - --log(tostring(conflict).." is the conflict level") + --debug.log(tostring(conflict).." is the conflict level") if error ~= nil then return error else diff --git a/luasrc/controller/commotion/serval_keyring.lua b/luasrc/controller/commotion/serval_keyring.lua index b901636..cefa8ae 100644 --- a/luasrc/controller/commotion/serval_keyring.lua +++ b/luasrc/controller/commotion/serval_keyring.lua @@ -13,8 +13,6 @@ http://www.apache.org/licenses/LICENSE-2.0 module("luci.controller.commotion.serval_keyring", package.seeall) -require "commotion_helpers" - local key_file = "/etc/commotion/keys.d/mdp/" function index() @@ -36,7 +34,8 @@ function main(Err) end function new_keyring() - log("Creating New Keyring...") + local debug = require "luci.commotion.debugger" + debug.log("Creating New Keyring...") local values = luci.http.formvalue() local new = values["new_keyring"] local rm = luci.sys.call("rm "..key_file.."serval.keyring") @@ -49,7 +48,7 @@ function new_keyring() local AND = " && " --Run the actual serval command to create a new keyring & key local new_key = luci.sys.call(s_start..AND..s_stop) - --log(luci.sys.exec(s_list_key)) + --debug.log(luci.sys.exec(s_list_key)) --If no errors occured in sys calls if rm ~= 1 and new_key ~= 1 then finish() @@ -70,19 +69,20 @@ end ---calls the file uploader and checks if the file is a correct config. function up() - log("uploader started") + local debug = require "luci.commotion.debugger" + debug.log("uploader started") local error = nil setFileHandler("/tmp/", "upload", "serval.keyring") - --log(luci.sys.exec("md5sum /tmp/serval.keyring")) + --debug.log(luci.sys.exec("md5sum /tmp/serval.keyring")) local values = luci.http.formvalue() local ul = values["upload"] if ul ~= '' and ul ~= nil then - log("checking file") + debug.log("checking file") error = checkFile("/tmp/serval.keyring") end --remove file if errors, copy it to correct directory and finish if a keyring if error ~= nil then - log("error found") + debug.log("error found") local rm = luci.sys.call("rm /tmp/serval.keyring") main(error) else @@ -108,12 +108,13 @@ function down() end function download(filename) + local debug = require "luci.commotion.debugger" --TODO remove the luci.http.status calls and replace them with calls to main(error) with the appropriate text to inform the user of why they cannot download it. - log("download started") + debug.log("download started") local f = io.open(filename) -- file does not exist if not f then - log("File Does Not Exist") + debug.log("File Does Not Exist") luci.http.status(403, "Access denied") return end @@ -130,6 +131,7 @@ end --@param input_name: (string) The name specified by the input html field. --@param file_name (string, optional) The optional name you would like the file to be saved as. If left blank the file keeps its uploaded name. function setFileHandler(location, input_name, file_name) + local debug = require "luci.commotion.debugger" local sys = require "luci.sys" local fs = require "luci.fs" local configLoc = location @@ -140,14 +142,14 @@ function setFileHandler(location, input_name, file_name) complete = nil if meta and meta.name == input_name then if file_name ~= nil then - log("starting download") + debug.log("starting download") fp = io.open(configLoc .. file_name, "w") else - log("starting download") + debug.log("starting download") fp = io.open(configLoc .. meta.file, "w") end else - log("file not of specified input type (input name variable)") + debug.log("file not of specified input type (input name variable)") end end if chunk then @@ -155,7 +157,7 @@ function setFileHandler(location, input_name, file_name) end if eof then fp:close() - log("file downloaded") + debug.log("file downloaded") end end) end