From 5709d2bdac14d9d55d4a5892e90af9eb1c17cafa Mon Sep 17 00:00:00 2001 From: Seamus Tuohy Date: Mon, 19 Aug 2013 12:18:08 -0400 Subject: [PATCH 1/3] added serval keyring upload, download, and creation functionality --- .../controller/commotion/serval_keyring.lua | 154 ++++++++++++++++++ luasrc/view/commotion/serval_keyring.htm | 76 +++++++++ 2 files changed, 230 insertions(+) create mode 100644 luasrc/controller/commotion/serval_keyring.lua create mode 100644 luasrc/view/commotion/serval_keyring.htm diff --git a/luasrc/controller/commotion/serval_keyring.lua b/luasrc/controller/commotion/serval_keyring.lua new file mode 100644 index 0000000..81cb46a --- /dev/null +++ b/luasrc/controller/commotion/serval_keyring.lua @@ -0,0 +1,154 @@ +--[[ +LuCI - Lua Configuration Interface + +Copyright 2011 Josh King + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +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() + require("luci.i18n").loadc("commotion") + local i18n = luci.i18n.translate + + entry({"admin", "commotion", "serval_keyring_new"}, call("new_keyring")).leaf=true + entry({"admin", "commotion", "serval_keyring_down"}, call("down")).leaf=true + entry({"admin", "commotion", "serval_keyring_up"}, call("up")).leaf=true + entry({"admin", "commotion", "serval_keyring"}, call("main"), "Serval Keyring", 20).dependent=false +end + +function main(Err) + if not ERR then + ERR = nil + end + luci.http.prepare_content("text/html") + luci.template.render("commotion/serval_keyring", {Err = Err}) +end + +function new_keyring() + log("Creating New Keyring...") + local values = luci.http.formvalue() + local new = values["new_keyring"] + local rm = luci.sys.call("rm "..key_file.."serval.keyring") + local new_key = luci.sys.call("SERVALINSTANCE_PATH="..key_file.." servald start && SERVALINSTANCE_PATH="..key_file.." servald stop") + --If no errors occured in sys calls + if rm ~= 1 and new_key ~= 1 then + finish() + else + main("Serval process failed") + end +end + +function finish() + --TODO What kind of cleanup/setup do we need to do? + local olsrd = luci.sys.call("/etc/init.d/olsrd restart") + if olsrd == 0 then + main() + else + main("olsrd failed to restart") + end +end + +---calls the file uploader and checks if the file is a correct config. +function up() + log("up started") + local error = nil + setFileHandler("/tmp/", "upload", "serval.keyring") + local values = luci.http.formvalue() + log(values) + local ul = values["upload"] + if ul ~= '' and ul ~= nil then + 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") + log(error) + local rm = luci.sys.call("rm /tmp/serval.keyring") + main(error) + else + local rm = luci.sys.call("rm "..key_file.."serval.keyring") + local cp = luci.sys.call("cp /tmp/serval.keyring "..key_file..".") + finish() + end +end + +function checkFile(file) + local keyring = luci.sys.exec("SERVALINSTANCE_PATH="..key_file.." servald keyring list") + --log("<<<<>>>") + --log(keyring.." "..tostring(string.len(keyring))) + if string.match(keyring, "^%w*::%c$") == nil and string.len(keyring) ~= 67 then + return "The file supplied is not a proper keyring, or is password protected. Please upload another key." + end +end + + +function down() + local values = luci.http.formvalue() + download(key_file.."serval.keyring") + main() +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") + local f = io.open(filename) + -- file does not exist + if not f then + log("File Does Not Exist") + luci.http.status(403, "Access denied") + return + end + -- send it + luci.http.prepare_content("application/force-download") + luci.http.header("Content-Disposition", "attachment; filename=serval.keyring") + luci.ltn12.pump.all(luci.ltn12.source.file(f), luci.http.write) + io.close(f) +end + + +---Uploads a file to a specified location, and possible file name. +--@param location: (string) The full path to where the file should be saved. +--@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 sys = require "luci.sys" + local fs = require "luci.fs" + local configLoc = location + local fp + luci.http.setfilehandler( + function(meta, chunk, eof) + if not fp then + complete = nil + if meta and meta.name == input_name then + if file_name ~= nil then + log("starting download") + fp = io.open(configLoc .. file_name, "w") + else + log("starting download") + fp = io.open(configLoc .. meta.file, "w") + end + else + log("file not of specified input type (input name variable)") + end + if chunk then + fp:write(chunk) + end + if eof then + fp:close() + log("file downloaded") + end + end + end) +end diff --git a/luasrc/view/commotion/serval_keyring.htm b/luasrc/view/commotion/serval_keyring.htm new file mode 100644 index 0000000..7c1526d --- /dev/null +++ b/luasrc/view/commotion/serval_keyring.htm @@ -0,0 +1,76 @@ +<%+header%> + +<%- + uri = REQUEST_URI + uri_prefix, num_post = string.gsub(uri, "serval_keyring_.*", "serval_keyring") +-%> + +

<%:Serval Keyring%>

+ +

<%=Err%>

+ +

<%:Serval KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal Keyring%>

+ +

+ +

<%:Download a Serval Keyring%>

+
+
+
+
+ + +
+ <%:Download your current Serval keyring%> +
+
+
+
+ +
+ +

<%:Upload a Serval Keyring%>

+
+
+
+
+ + +
+ <%:Upload a serval key file file from your own computer.%> +
+
+
+
+ +
+ +

<%:Create a new Serval Keyring%>

+
+
+
+
+ + +
+ <%:Create a new keyring on this device, replacing the existing keyring.%> +
+
+
+
+ +
+ + + +<%+footer%> From 78a4e6083f1b17ad465ba057c4a6e181f3b34f8e Mon Sep 17 00:00:00 2001 From: Seamus Tuohy Date: Mon, 19 Aug 2013 17:01:21 -0400 Subject: [PATCH 2/3] added correct uploading and file checking to keyring uploader. --- .../controller/commotion/serval_keyring.lua | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/luasrc/controller/commotion/serval_keyring.lua b/luasrc/controller/commotion/serval_keyring.lua index 81cb46a..b901636 100644 --- a/luasrc/controller/commotion/serval_keyring.lua +++ b/luasrc/controller/commotion/serval_keyring.lua @@ -21,10 +21,10 @@ function index() require("luci.i18n").loadc("commotion") local i18n = luci.i18n.translate - entry({"admin", "commotion", "serval_keyring_new"}, call("new_keyring")).leaf=true - entry({"admin", "commotion", "serval_keyring_down"}, call("down")).leaf=true - entry({"admin", "commotion", "serval_keyring_up"}, call("up")).leaf=true - entry({"admin", "commotion", "serval_keyring"}, call("main"), "Serval Keyring", 20).dependent=false + entry({"admin", "commotion", "serval_keyring_new"}, call("new_keyring")) + entry({"admin", "commotion", "serval_keyring_down"}, call("down")) + entry({"admin", "commotion", "serval_keyring_up"}, call("up")) + entry({"admin", "commotion", "serval_keyring"}, call("main"), "Serval Keyring", 20) end function main(Err) @@ -40,7 +40,16 @@ function new_keyring() local values = luci.http.formvalue() local new = values["new_keyring"] local rm = luci.sys.call("rm "..key_file.."serval.keyring") - local new_key = luci.sys.call("SERVALINSTANCE_PATH="..key_file.." servald start && SERVALINSTANCE_PATH="..key_file.." servald stop") + --Define the various serval code to run + local s_path = "SERVALINSTANCE_PATH=" + local s_start = s_path..key_file.." servald start" + local s_stop = s_path..key_file.." servald stop" + --local s_add_key = s_path..key_file.." servald keyring add" + --local s_list_key = s_path..key_file.." servald keyring list" + 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)) --If no errors occured in sys calls if rm ~= 1 and new_key ~= 1 then finish() @@ -61,11 +70,11 @@ end ---calls the file uploader and checks if the file is a correct config. function up() - log("up started") + log("uploader started") local error = nil setFileHandler("/tmp/", "upload", "serval.keyring") + --log(luci.sys.exec("md5sum /tmp/serval.keyring")) local values = luci.http.formvalue() - log(values) local ul = values["upload"] if ul ~= '' and ul ~= nil then log("checking file") @@ -74,7 +83,6 @@ function up() --remove file if errors, copy it to correct directory and finish if a keyring if error ~= nil then log("error found") - log(error) local rm = luci.sys.call("rm /tmp/serval.keyring") main(error) else @@ -85,10 +93,9 @@ function up() end function checkFile(file) - local keyring = luci.sys.exec("SERVALINSTANCE_PATH="..key_file.." servald keyring list") - --log("<<<<>>>") - --log(keyring.." "..tostring(string.len(keyring))) - if string.match(keyring, "^%w*::%c$") == nil and string.len(keyring) ~= 67 then + local keyring = luci.sys.exec("SERVALINSTANCE_PATH=/tmp/ servald keyring list") + local key = string.match(keyring, "^(%w*):%w*:") + if key == nil or string.len(key) ~= 64 then return "The file supplied is not a proper keyring, or is password protected. Please upload another key." end end @@ -142,13 +149,13 @@ function setFileHandler(location, input_name, file_name) else log("file not of specified input type (input name variable)") end - if chunk then - fp:write(chunk) - end - if eof then - fp:close() - log("file downloaded") - end + end + if chunk then + fp:write(chunk) + end + if eof then + fp:close() + log("file downloaded") end end) end From 872a90b42be53b392e9cf77ac9877643a7ca1a6c Mon Sep 17 00:00:00 2001 From: Chris Ritzo Date: Thu, 22 Aug 2013 10:05:15 -0400 Subject: [PATCH 3/3] Reviewed and updated Serval Keyring labels and explanatory text. modified: luasrc/view/commotion/serval_keyring.htm --- luasrc/view/commotion/serval_keyring.htm | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/luasrc/view/commotion/serval_keyring.htm b/luasrc/view/commotion/serval_keyring.htm index 7c1526d..59e06e3 100644 --- a/luasrc/view/commotion/serval_keyring.htm +++ b/luasrc/view/commotion/serval_keyring.htm @@ -19,7 +19,7 @@

<%:Serval Keyring%>

<%=Err%>

-

<%:Serval KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal KeyringServal Keyring%>

+

<%:The Serval Keyring is used to sign all network traffic on a Commotion mesh network. Every node on your Commotion network should use the same Serval Keyring. On this page you can download the existing Serval keyring for this node, to back it up or to use when adding new nodes to your network. You can also generate a new Serval Keyring, or upload one from your computer.%>



@@ -28,7 +28,7 @@

<%:Download a Serval Keyring%>

- +
<%:Download your current Serval keyring%> @@ -44,10 +44,10 @@

<%:Upload a Serval Keyring%>

- +
- <%:Upload a serval key file file from your own computer.%> + <%:Upload a Serval key file file from your own computer.%>
@@ -60,7 +60,7 @@

<%:Create a new Serval Keyring%>

- +
<%:Create a new keyring on this device, replacing the existing keyring.%> @@ -68,7 +68,7 @@

<%:Create a new Serval Keyring%>

- +