summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2011-10-11 14:37:08 +0000
committerTed Trask <ttrask01@yahoo.com>2011-10-11 14:37:08 +0000
commit6b6de1fd8e91f2fe812db929fea4bef9d1224585 (patch)
tree5acabf997347d5954371a4dd840d82867423704f
parent933d3b99ff2dbc9da92f7e6327b9e4c58c33df79 (diff)
downloadacf-provisioning-6b6de1fd8e91f2fe812db929fea4bef9d1224585.tar.bz2
acf-provisioning-6b6de1fd8e91f2fe812db929fea4bef9d1224585.tar.xz
Implemented put_file for Polycom phones
-rwxr-xr-xcgi-bin/provisioning.cgi2
-rw-r--r--config/delete_device.lua2
-rw-r--r--config/process_put.lua61
-rw-r--r--provisioning-model.lua50
4 files changed, 102 insertions, 13 deletions
diff --git a/cgi-bin/provisioning.cgi b/cgi-bin/provisioning.cgi
index ee12e22..f0de432 100755
--- a/cgi-bin/provisioning.cgi
+++ b/cgi-bin/provisioning.cgi
@@ -124,7 +124,7 @@ elseif ( request_method == "PUT" ) then
local success = true
-- Protect against writing to arbitrary paths
- if string.match(path_info, "%.%.") then
+ if not mac or mac == "" or string.match(path_info, "%.%.") then
http_code(403)
log:close()
os.exit()
diff --git a/config/delete_device.lua b/config/delete_device.lua
index ad908ed..6e5a907 100644
--- a/config/delete_device.lua
+++ b/config/delete_device.lua
@@ -5,6 +5,8 @@ local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
APP.logevent("got to delete_device script")
+-- First, we delete the associated config files
+
-- We'll handle the deleting of the device by handling the resulting changing of the params
-- First, have to create a new set of params (with blank extensions)
local duplicatestructure
diff --git a/config/process_put.lua b/config/process_put.lua
new file mode 100644
index 0000000..0de6b9c
--- /dev/null
+++ b/config/process_put.lua
@@ -0,0 +1,61 @@
+-- This is the script run to process uploaded config files
+local functions, mac, data, device_id = ...
+
+APP.logevent("got to process_put script")
+
+-- Get the params
+local params = functions.get_device_params(device_id)
+
+function process_polycom()
+ local before, xml, after = string.match(data, "(.*<OVERRIDES )([^/]*)( />.*)")
+ if not xml then return end
+ local attrs = {}
+ for str in string.gmatch(xml, "%S+") do
+ local n,v = string.match(str, "([^=]+)=\"(.*)\"")
+ if not attrs[n] then
+ attrs[n] = v
+ end
+ end
+
+-- for n,v in pairs(attrs) do
+-- APP.logevent("name "..n.." val "..(v or ""))
+-- end
+
+ -- Read attributes from attrs and generate paramaters for ACF
+ local xmlout = {}
+ for n,v in pairs(attrs) do
+ -- search attribute name for reg_name like "reg.1."
+ -- and for rest like "fwdStatus"
+ local num, rest = string.match(n, "reg%.(%d+)%.([%.%a]*)$")
+
+ -- if rest is defined, then num is defined also
+ if rest == "fwd.busy.contact" then
+ params.value["reg"..num].value.forwardbusy.value = v
+ elseif rest == "fwd.noanswer.contact" then
+ params.value["reg"..num].value.forwardnoanswer.value = v
+ elseif rest == "fwdContact" then
+ params.value["reg"..num].value.forwardall.value = v
+ elseif rest == "fwd.busy.status" then
+ params.value["reg"..num].value.forwardbusyenable.value = (v == "1")
+ elseif rest == "fwd.noanswer.status" then
+ params.value["reg"..num].value.forwardnoanswerenable.value = (v == "1")
+ elseif rest == "fwdStatus" then
+ params.value["reg"..num].value.forwardallenable.value = (v == "1")
+ else
+ xmlout[#xmlout+1] = n.."=\""..v.."\""
+ end
+ end
+
+ data = before..table.concat(xmlout, " ")..after
+end
+
+-- Determine the template
+if string.match(params.value.device.label, "Polycom") then
+ process_polycom()
+end
+
+-- Update the params
+local params = functions.set_device_params(params)
+
+-- Return the updated data
+return data
diff --git a/provisioning-model.lua b/provisioning-model.lua
index 16bfd15..da39fb1 100644
--- a/provisioning-model.lua
+++ b/provisioning-model.lua
@@ -223,12 +223,8 @@ validateparam = function(p)
end
-- These are the functions that may be called from within loaded Lua code
-local functions = {
- getselectresponse=getselectresponse,
- runsqlcommand=runsqlcommand,
- get_device=get_device,
- get_device_params=get_device_params,
-}
+-- The actual functions are added at the end of the file, after they're declared
+local functions = {}
local validateparamcoded
validateparamcoded = function(p, top)
@@ -264,7 +260,7 @@ validateparamcoded = function(p, top)
end
local function callscript(script, ...)
- local result
+ local result, errtxt
local env = {}
setmetatable (env, {__index = _G})
-- loadfile loads into the global environment
@@ -273,14 +269,14 @@ local function callscript(script, ...)
local f = loadfile(script)
if f then
local res, err = pcall(function(...)
- result = f(functions, ...)
+ result, errtxt = f(functions, ...)
end, ...)
if not res and err then
assert(res, "Exception in "..script.." script\n"..err)
end
end
setfenv (0, _G)
- return result
+ return result, errtxt
end
local function validateluacode(code)
@@ -1915,11 +1911,32 @@ end
function put_file(mac, data)
local retval = cfe({ label="PUT Data" })
+
+ -- Error if there's no mac
+ if not mac or mac == "" then
+ retval.errtxt = "No MAC Address"
+ return retval
+ end
+
local res, err = pcall(function()
local connected = databaseconnect()
- retval.value, retval.errtxt = callscript(processputscriptfile, mac, data)
- -- If the script doesn't exist, allow the write
- retval.value = retval.value or data
+
+ -- Now, let's see if this device exists
+ local search = get_search_options()
+ search.value.id.value = "device.mac"
+ search.value.value.value = string.upper(mac)
+ search = search_device_values(search)
+ if search.errtxt then
+ retval.errtxt = search.errtxt
+ elseif #search.value.result.value == 0 then
+ retval.errtxt = "Device not found"
+ elseif #search.value.result.value > 1 then
+ retval.errtxt = "Multiple devices found"
+ else
+ retval.value, retval.errtxt = callscript(processputscriptfile, mac, data, search.value.result.value[1].device_id)
+ -- If the script doesn't exist, allow the write
+ retval.value = retval.value or data
+ end
if connected then databasedisconnect() end
end)
if not res and err then
@@ -1927,3 +1944,12 @@ function put_file(mac, data)
end
return retval
end
+
+-- The functions must be added after they're declared
+functions = {
+ getselectresponse=getselectresponse,
+ runsqlcommand=runsqlcommand,
+ get_device=get_device,
+ get_device_params=get_device_params,
+ set_device_params=set_device_params,
+}