2022-03-13 21:19:04 -06:00
#!/usr/bin/lua
--[[
2024-05-29 01:45:25 -06:00
Part of AREDN® -- Used for creating Amateur Radio Emergency Data Networks
2022-03-13 21:19:04 -06:00
Copyright (C) 2021 Tim Wilkinson
Original Perl Copyright (C) 2015 Conrad Lara
See Contributors file for additional contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional Terms:
2024-05-29 01:45:25 -06:00
Additional use restrictions exist on the AREDN® trademark and logo.
2022-03-13 21:19:04 -06:00
See AREDNLicense.txt for more info.
2024-05-29 01:45:25 -06:00
Attributions to the AREDN® Project must be retained in the source code.
2022-03-13 21:19:04 -06:00
If importing this code into a new or existing project attribution
2024-05-29 01:45:25 -06:00
to the AREDN® project must be added to the source code.
2022-03-13 21:19:04 -06:00
You must not misrepresent the origin of the material contained within.
Modified versions must be modified to attribute to the original source
and be marked in reasonable ways as differentiate it from the original
version
--]]
require("nixio")
require("aredn.hardware")
require("aredn.http")
require("aredn.utils")
2024-04-01 23:15:45 -06:00
require("aredn.html")
2022-03-13 21:19:04 -06:00
require("uci")
2024-04-01 23:15:45 -06:00
require("aredn.info")
2022-03-13 21:19:04 -06:00
require("ubus")
2023-01-25 11:41:21 -07:00
require("luci.jsonc")
2022-03-13 21:19:04 -06:00
local html = aredn.html
local cursor = uci.cursor()
local conn = ubus.connect()
-- handle firmware updates
local fw_install = false
local fw_output = {}
2023-01-25 11:41:21 -07:00
local fw_versions = {}
local fw_names = {}
2022-03-13 21:19:04 -06:00
local fw_version = ""
function fwout(msg)
fw_output[#fw_output + 1] = msg
end
function get_default_gw()
-- a node with a wired default gw will route via this
local p = io.popen("ip route list table 254")
if p then
for line in p:lines()
do
local gw = line:match("^default%svia%s([%d%.]+)")
if gw then
p:close()
return gw
end
end
p:close()
end
-- table 31 is populated by OLSR
p = io.popen("ip route list table 31")
if p then
for line in p:lines()
do
local gw = line:match("^default%svia%s([%d%.]+)")
if gw then
p:close()
return gw
end
end
p:close()
end
return "none"
end
function word_wrap(len, lines)
local output = ""
for _, str in ipairs(lines)
do
while #str > len
do
local str1 = str:sub(1, len)
local str2 = str:sub(len + 1)
local m, x = str1:match("^(.*)%s(%S+)$")
if m then
output = output .. m .. "\n"
str = x .. str2
else
output = output .. str1 .. "\n"
str = str2
end
end
output = output .. str .. "\n"
end
return output:sub(1, #output - 1)
end
2024-04-07 14:04:00 -06:00
function print_firmware_notice(reboot_when, href_addr, text_addr)
html.print([[
<center><h2>Firmware will be written in the background.</h2>
<h3>If your computer is connected to the LAN of this node you may need to acquire<br>
a new IP address and reset any name service caches you may be using.</h3>
<h3>The node will reboot ]] .. reboot_when .. [[.<br>
When the node has finished booting you should ensure your computer has<br>
received a new IP address and reconnect with<br>
<a href='http://]] .. href_addr .. [[:8080/'>http://]] .. text_addr .. [[:8080/</a><br>
This page will automatically reload once the upgrade has completed</h3>
<br><h3><label for='cdprogress'>Writing firmware: </label><progress id='cdprogress' max='300'/></h3>
<h1>Time Remaining: <span id='countdown'></h1>
</center></body></html>
]])
end
2022-03-13 21:19:04 -06:00
-- read_postdata
local parms = {}
local firmfile = ""
if os.getenv("REQUEST_METHOD") == "POST" then
2023-12-17 17:20:41 -07:00
require('luci.http')
2022-12-22 13:22:49 -07:00
local request = luci.http.Request(nixio.getenv(),
2022-03-13 21:19:04 -06:00
function()
local v = io.read(1024)
if not v then
io.close()
end
return v
end
)
-- only allow file uploading without active tunnels
2022-12-22 13:22:49 -07:00
local fp
request:setfilehandler(
function(meta, chunk, eof)
if not fp then
if meta and meta.file then
firmfile = meta.file
2024-08-05 18:11:53 -06:00
if firmfile:match("sysupgrade%.bin$") or firmfile:match("sysupgrade%-v7%.bin$") or firmfile:match("ext4%-combined%.img%.gz$") then
2022-12-22 13:22:49 -07:00
-- Uploading a system upgrade - clear out memory early
2023-01-29 20:21:03 -07:00
os.execute("/usr/local/bin/upgrade_prepare.sh stop > /dev/null 2>&1")
2022-03-13 21:19:04 -06:00
end
2022-12-22 13:22:49 -07:00
end
2024-08-15 21:28:45 -06:00
nixio.fs.mkdir("/tmp/web")
2022-12-22 13:22:49 -07:00
nixio.fs.mkdir("/tmp/web/upload")
fp = io.open("/tmp/web/upload/file", "w")
2022-03-13 21:19:04 -06:00
end
2022-12-22 13:22:49 -07:00
if chunk then
fp:write(chunk)
end
if eof then
fp:close()
end
end
)
2022-03-13 21:19:04 -06:00
parms = request:formvalue()
end
if parms.button_reboot then
2024-03-25 20:52:27 -06:00
html.reboot()
2022-03-13 21:19:04 -06:00
end
local node = aredn.info.get_nvram("node")
local tmpdir = "/tmp/web/admin"
nixio.fs.mkdir("/tmp/web")
nixio.fs.mkdir("/tmp/web/admin")
2023-12-12 21:01:39 -07:00
-- set the curl command options
local curl = "curl -A 'node: " .. node .. "' "
2022-03-13 21:19:04 -06:00
local serverpaths = {}
2024-08-15 21:28:45 -06:00
local aredn_firmware = cursor:get("aredn", "@downloads[0]", "firmware_aredn")
if aredn_firmware then
serverpaths[#serverpaths + 1] = aredn_firmware
else
local uciserverpath = cursor:get("aredn", "@downloads[0]", "firmwarepath")
if not uciserverpath then
uciserverpath = ""
end
serverpaths[#serverpaths + 1] = uciserverpath:match("^(.*)/firmware") or uciserverpath
2022-03-13 21:19:04 -06:00
end
local hardwaretype = aredn.hardware.get_type()
local targettype = conn:call("system", "board", {}).release.target
2023-04-28 12:01:58 -06:00
local local_firmware = "local_firmware.bin"
local firmware_type = "bin"
2024-08-05 18:11:53 -06:00
local firmware_subtype = ""
2023-04-28 12:01:58 -06:00
if targettype:match("x86") then
local_firmware = "local_firmware.img.gz"
firmware_type = "gz"
end
2022-03-13 21:19:04 -06:00
-- handle TPLink and Mikrotik exception conditions
local mfg = aredn.hardware.get_manufacturer()
local mfgprefix = ""
if mfg:match("[Uu]biquiti") then
mfgprefix = "ubnt"
elseif mfg:match("[Mm]ikro[Tt]ik") then
mfgprefix = "mikrotik"
elseif mfg:match("[Tt][Pp]-[Ll]ink") then
mfgprefix = "cpe"
end
2024-08-05 18:11:53 -06:00
-- Handle Mikrotik v7 bootloader
if mfgprefix == "mikrotik" then
local f = io.open("/sys/firmware/mikrotik/soft_config/bios_version")
if f then
local booter_version = f:read("*a")
f:close();
if booter_version:match("^7%.") then
firmware_subtype = "v7"
end
end
end
2022-03-13 21:19:04 -06:00
-- refresh fw
if parms.button_refresh_fw then
nixio.fs.remove("/tmp/web/firmware.list")
2024-08-15 21:28:45 -06:00
if get_default_gw() ~= "none" or serverpaths[1]:match("%.local%.mesh") then
fwout("Downloading firmware list from " .. serverpaths[1] .. "...")
2023-01-25 11:41:21 -07:00
local config_versions
local config_serverpath
2022-03-13 21:19:04 -06:00
for _, serverpath in ipairs(serverpaths)
do
2024-08-15 21:28:45 -06:00
config_serverpath = serverpath .. "/afs/www/"
2023-12-12 21:01:39 -07:00
for line in io.popen(curl .. " -o - " .. config_serverpath .. "config.js 2> /dev/null"):lines()
2023-01-25 11:41:21 -07:00
do
local v = line:match("versions: {(.+)}")
if v then
config_versions = v
break
end
end
if config_versions then
2022-03-13 21:19:04 -06:00
break
end
end
2023-01-25 11:41:21 -07:00
if not config_versions then
2023-12-19 13:06:45 -07:00
fwout("Failed to load firmware versions")
2022-03-13 21:19:04 -06:00
else
2023-01-25 11:41:21 -07:00
local firmware_versions = {}
for k, v in config_versions:gmatch("'([^']+)': '([^']+)'")
do
2023-01-27 21:08:22 -07:00
firmware_versions[k] = v
2023-01-25 11:41:21 -07:00
end
local board_type = aredn.hardware.get_board_type():gsub(",", "_")
2024-01-03 23:32:41 -07:00
local firmware_list = {}
for attempt = 1, 3
2023-01-25 11:41:21 -07:00
do
2024-01-03 23:32:41 -07:00
for ver, data in pairs(firmware_versions)
do
local raw = io.popen(curl .. " -o - " .. config_serverpath .. data .. "/overview.json 2> /dev/null")
local info = luci.jsonc.parse(raw:read("*a") or "")
raw:close()
if info then
firmware_versions[ver] = nil
for _, profile in ipairs(info.profiles)
do
2024-02-05 12:51:43 -07:00
if profile.id == board_type or ((board_type == "qemu" or board_type == "vmware") and profile.id == "generic" and profile.target == "x86/64") then
2024-01-03 23:32:41 -07:00
firmware_list[ver] = {
overview = config_serverpath .. data .. "/" .. profile.target .. "/" .. profile.id .. ".json",
target = info.image_url:gsub("{target}", profile.target)
}
break
end
2023-01-25 11:41:21 -07:00
end
end
end
end
local f = io.open("/tmp/web/firmware.list", "w")
if f then
2024-01-03 23:32:41 -07:00
f:write(luci.jsonc.stringify(firmware_list, true))
2023-01-25 11:41:21 -07:00
f:close()
end
2024-01-03 23:32:41 -07:00
local err = false
for _, _ in pairs(firmware_versions)
do
err = true
end
2023-12-19 13:06:45 -07:00
if err then
fwout("Failed to load all firmware versions. List is incomplete")
else
fwout("Done")
end
2022-03-13 21:19:04 -06:00
end
2013-11-14 23:11:16 -07:00
else
2022-03-13 21:19:04 -06:00
fwout("Error: no route to Host")
end
end
-- generate data structures
-- and set fw_version
2023-01-25 11:41:21 -07:00
--firmware_list_gen()
if nixio.fs.stat("/etc/mesh-release") then
for line in io.lines("/etc/mesh-release")
do
fw_version = line:chomp()
break
end
end
local f = io.open("/tmp/web/firmware.list")
if f then
2023-01-28 20:58:46 -07:00
fw_versions = luci.jsonc.parse(f:read("*a") or "")
2023-01-25 11:41:21 -07:00
f:close()
if fw_versions then
for v, d in pairs(fw_versions)
do
fw_names[#fw_names + 1] = v
end
-- Sort in version number order (newest at the top) but with nightlies at the bottom
table.sort(fw_names, function (a, b)
if a:match("%-") then
return false
elseif b:match("%-") then
return true
end
local ai = a:gmatch("(%d+)")
local bi = b:gmatch("(%d+)")
while true
do
local va = tonumber(ai() or nil)
local vb = tonumber(bi() or nil)
if not va then
return false
elseif not vb then
return true
elseif va < vb then
return false
elseif va > vb then
return true
end
end
return false
end)
end
end
2022-03-13 21:19:04 -06:00
2022-05-08 10:13:11 -06:00
-- sideload fw
2023-04-28 12:01:58 -06:00
if parms.button_apply_fw and nixio.fs.stat("/tmp/web/" .. local_firmware) then
2022-05-08 10:13:11 -06:00
nixio.fs.mkdir("/tmp/web/upload")
2023-04-28 12:01:58 -06:00
os.execute("mv -f /tmp/web/" .. local_firmware .. " /tmp/web/upload/file")
if firmware_type == "gz" then
firmfile = "arednmesh-sideload-ext4-combined.img.gz"
else
firmfile = "arednmesh-sideload-sysupgrade.bin"
end
2022-05-08 10:13:11 -06:00
parms.button_ul_fw = "Upload"
2023-01-29 20:21:03 -07:00
os.execute("/usr/local/bin/upgrade_prepare.sh stop > /dev/null 2>&1")
2022-05-08 10:13:11 -06:00
end
2022-03-13 21:19:04 -06:00
-- upload fw
if parms.button_ul_fw and nixio.fs.stat("/tmp/web/upload/file") then
os.execute("mv -f /tmp/web/upload/file " .. tmpdir .. "/firmware")
2024-08-05 18:11:53 -06:00
if firmware_type == "bin" and (firmfile:match("sysupgrade%.bin$") or firmfile:match("sysupgrade%-v7%.bin$")) then -- full firmware
2022-03-13 21:19:04 -06:00
fw_install = true
-- drop the page cache to take pressure off tmps when checking the firmware
write_all("/proc/sys/vm/drop_caches", "3")
-- check firmware header
2023-01-05 17:37:39 -07:00
local fcheck = capture("/usr/local/bin/firmwarecheck.sh " .. tmpdir .. "/firmware")
if fcheck ~= "" then
2022-03-13 21:19:04 -06:00
fwout("Firmware CANNOT be updated")
2023-01-05 17:37:39 -07:00
fwout("firmware file is not valid: " .. fcheck)
2022-03-13 21:19:04 -06:00
fw_install = false
nixio.fs.remove(tmpdir .. "/firmware")
if os.execute("/usr/local/bin/uploadctlservices restore > /dev/null 2>&1") ~= 0 then
fwout("Failed to restart all services, please reboot this node.")
end
end
2023-04-28 12:01:58 -06:00
elseif firmware_type == "gz" and firmfile:match("ext4%-combined%.img%.gz$") then -- full x86 firmware
fw_install = true
2013-11-14 23:11:16 -07:00
else
2022-03-13 21:19:04 -06:00
fwout("Firmware CANNOT be updated")
fwout("the uploaded file is not recognized")
nixio.fs.remove(tmpdir .. "/firmware")
2023-01-29 20:21:03 -07:00
os.execute("/usr/local/bin/upgrade_prepare.sh restore > /dev/null 2>&1")
2022-03-13 21:19:04 -06:00
if os.execute("/usr/local/bin/uploadctlservices restore > /dev/null 2>&1") ~= 0 then
fwout("Failed to restart all services, please reboot this node.")
end
end
end
-- download fw
if parms.button_dl_fw and parms.dl_fw ~= "default" then
2024-08-15 21:28:45 -06:00
if get_default_gw() ~= "none" or serverpaths[1]:match("%.local%.mesh") then
2022-03-13 21:19:04 -06:00
nixio.fs.remove(tmpdir .. "/firmware")
os.execute("/usr/local/bin/uploadctlservices update > /dev/null 2>&1")
2022-03-16 20:45:26 -06:00
2023-01-25 11:41:21 -07:00
fw_install = false
local err
2022-03-13 21:19:04 -06:00
2023-12-12 21:01:39 -07:00
local f = io.popen(curl .. " -o - " .. fw_versions[parms.dl_fw].overview .. " 2> /dev/null")
2023-01-28 20:58:46 -07:00
local fwinfo = luci.jsonc.parse(f:read("*a") or "")
2023-01-25 11:41:21 -07:00
f:close()
if fwinfo then
local fwimage
for _, image in ipairs(fwinfo.images)
do
2024-08-05 18:11:53 -06:00
if (firmware_type == "bin" and firmware_subtype == "" and image.type == "sysupgrade") or (firmware_type == "bin" and firmware_subtype == "" and image.type == "nand-sysupgrade") or (firmware_type == "bin" and firmware_subtype == "v7" and image.type == "sysupgrade-v7") or (firmware_type == "gz" and image.type == "combined") then
2023-01-25 11:41:21 -07:00
fwimage = {
url = fw_versions[parms.dl_fw].target .. "/" .. image.name,
sha = image.sha256
}
break
2022-03-13 21:19:04 -06:00
end
end
2023-01-25 11:41:21 -07:00
if fwimage then
2023-12-12 21:01:39 -07:00
-- We shutdown dnsmasq to save memory, so we resolve the URL's IP address before doing that if we can
local url = fwimage.url
local host = url:match("^https?://([^/:]+)")
local headers = ""
if host then
local ip = iplookup(host)
if ip then
url = url:gsub(host:gsub("%-","%%-"):gsub("%.","%%."), ip)
headers = "-H 'Host: " .. host .. "' "
os.execute("/usr/local/bin/upgrade_prepare.sh stop > /dev/null 2>&1")
end
end
if os.execute(curl .. "-o " .. tmpdir .. "/firmware " .. headers .. url .. " > /dev/null 2>>" .. tmpdir .. "/curl.err") ~= 0 then
err = "Download failed!\n" .. read_all(tmpdir .. "/curl.err")
2023-01-25 11:41:21 -07:00
else
local sha = capture("sha256sum " .. tmpdir .. "/firmware"):match("^(%S+)")
if sha ~= fwimage.sha then
err = "firmware file checksum failed"
else
fw_install = true
end
2022-03-13 21:19:04 -06:00
end
2023-01-25 11:41:21 -07:00
else
2023-04-28 12:01:58 -06:00
err = "upgrade is not available"
2022-03-13 21:19:04 -06:00
end
else
2023-01-25 11:41:21 -07:00
err = "the downloaded file cannot be found"
end
2023-12-12 21:01:39 -07:00
nixio.fs.remove(tmpdir .. "/curl.err")
2023-01-25 11:41:21 -07:00
if err then
2022-03-13 21:19:04 -06:00
fwout("Firmware CANNOT be updated")
2023-01-25 11:41:21 -07:00
fwout(err)
2023-01-29 20:21:03 -07:00
os.execute("/usr/local/bin/upgrade_prepare.sh restore > /dev/null 2>&1")
2022-03-13 21:19:04 -06:00
if os.execute("/usr/local/bin/uploadctlservices restore > /dev/null 2>&1") ~= 0 then
fwout("Failed to restart all services, please reboot this node.")
end
2023-01-25 11:41:21 -07:00
nixio.fs.remove(tmpdir .. "/firmware")
2022-03-13 21:19:04 -06:00
end
2013-11-14 23:11:16 -07:00
else
2022-03-13 21:19:04 -06:00
fwout("Error: no route to Host")
2023-12-12 21:01:39 -07:00
nixio.fs.remove(tmpdir .. "/curl.err")
2022-03-13 21:19:04 -06:00
end
end
-- install fw
if fw_install and nixio.fs.stat(tmpdir .. "/firmware") then
2022-03-16 20:45:26 -06:00
http_header()
html.print("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">")
html.print("<html>")
html.print("<head>")
html.print("<title>FIRMWARE UPDATE IN PROGRESS</title>")
html.print("<meta http-equiv='expires' content='0'>")
html.print("<meta http-equiv='cache-control' content='no-cache'>")
html.print("<meta http-equiv='pragma' content='no-cache'>")
html.print("<meta name='robots' content='noindex'>")
if not nixio.fs.readlink("/tmp/web/style.css") then
if not nixio.fs.stat("/tmp/web") then
nixio.fs.mkdir("/tmp/web")
end
nixio.fs.symlink("/www/aredn.css", "/tmp/web/style.css")
end
2024-04-04 13:10:58 -06:00
-- Show a different reload address if we're running on the ramdisk
local displayaddress = "192.168.1.1"
local waitaddress = displayaddress
if parms.checkbox_keep_settings then
for line in io.lines("/proc/mounts")
do
if line:match("overlay") or line:match("ext4") then
displayaddress = node .. ".local.mesh"
waitaddress = nil
break
end
end
end
2022-03-16 20:45:26 -06:00
html.print("<style>")
html.print(read_all("/tmp/web/style.css"))
html.print("</style>")
2024-04-04 13:10:58 -06:00
html.wait_for_reboot(120, 300, waitaddress)
2022-03-13 21:19:04 -06:00
html.print("</head>")
html.print("<body><center>")
html.print("<h2>The firmware is being updated.</h2>")
html.print("<h1>DO NOT REMOVE POWER UNTIL UPDATE IS FINISHED</h1>")
html.print("</center><br>")
2022-03-16 20:45:26 -06:00
local upgradecmd = nil
2022-03-13 21:19:04 -06:00
if parms.checkbox_keep_settings then
local fin = io.open("/etc/arednsysupgrade.conf", "r")
if fin then
local fout = io.open("/tmp/sysupgradefilelist", "w")
if fout then
for line in fin:lines()
do
if not line:match("^#") and nixio.fs.stat(line) then
fout:write(line .. "\n")
end
end
fout:close()
fin:close()
aredn.info.set_nvram("nodeupgraded", "1")
if os.execute("tar -czf /tmp/arednsysupgradebackup.tgz -T /tmp/sysupgradefilelist > /dev/null 2>&1") ~= 0 then
html.print([[
<center><h2>ERROR: Could not backup filesystem.</h2>
<h3>An error occured trying to backup the file system. Node will now reboot.
</center>
]])
html.footer()
html.print("</body></html>")
http_footer()
aredn.info.set_nvram("nodeupgraded", "0")
2022-12-22 13:22:49 -07:00
os.execute("reboot >/dev/null 2>&1")
2022-03-13 21:19:04 -06:00
else
2024-04-07 14:04:00 -06:00
print_firmware_notice("twice while the configuration is applied", displayaddress, displayaddress)
2022-03-13 21:19:04 -06:00
http_footer()
nixio.fs.remove("/tmp/sysupgradefilelist")
2022-12-22 13:22:49 -07:00
upgradecmd = "/usr/local/bin/aredn_sysupgrade -f /tmp/arednsysupgradebackup.tgz -q " .. tmpdir .. "/firmware 2>&1 &"
2022-03-13 21:19:04 -06:00
end
else
fin:close()
2023-06-29 00:47:25 -06:00
html.footer()
html.print("</body></html>")
http_footer()
2022-03-13 21:19:04 -06:00
end
2022-03-16 20:45:26 -06:00
else
html.print([[
<center><h2>ERROR: Failed to create backup.</h2>
<h3>An error occured trying to backup the file system. Node will now reboot.
</center>
]])
html.footer()
html.print("</body></html>")
http_footer()
2022-12-22 13:22:49 -07:00
os.execute("reboot >/dev/null 2>&1")
2022-03-13 21:19:04 -06:00
end
2013-11-14 23:11:16 -07:00
else
2024-04-07 14:04:00 -06:00
print_firmware_notice("after the firmware has been written to flash memory", "localnode.local.mesh", "192.168.1.1")
2022-03-13 21:19:04 -06:00
http_footer()
2022-12-22 13:22:49 -07:00
upgradecmd = "/usr/local/bin/aredn_sysupgrade -q -n " .. tmpdir .. "/firmware 2>&1 &"
2022-03-16 20:45:26 -06:00
end
if upgradecmd then
os.execute(upgradecmd)
2022-03-13 21:19:04 -06:00
end
os.exit()
end
-- handle package actions
local pkg_output = {}
function pkgout(msg)
pkg_output[#pkg_output + 1] = msg
end
local permpkg = {}
if nixio.fs.stat("/etc/permpkg") then
for line in io.lines("/etc/permpkg")
do
if not line:match("^#") then
permpkg[line] = true
end
end
end
2022-03-17 13:28:20 -06:00
local meshpkgs = capture("grep -q \".local.mesh\" /etc/opkg/distfeeds.conf"):chomp()
2022-07-26 12:40:23 -06:00
function update_advertised_services()
local script = "/etc/cron.hourly/check-services"
local stat = nixio.fs.stat(script)
if stat.type == "reg" and nixio.bit.band(tonumber(stat.modedec, 8), tonumber(111, 8)) ~= 0 then
os.execute("(cd /tmp;" .. script .. " 2>&1 | logger -p daemon.debug -t " .. script .. ")&")
end
end
2023-09-15 22:16:27 -06:00
local package_store = "/etc/package_store"
local package_catalog = package_store .. "/catalog.json"
function record_package(op, ipkg, file)
nixio.fs.mkdir(package_store)
local catalog = luci.jsonc.parse(read_all(package_catalog) or '{"installed":{}}')
if op == "upload" then
local package = ipkg:match("^([^_]+)")
if package then
os.execute("/bin/cp -f " .. file .. " " .. package_store .. "/" .. package .. ".ipk")
catalog.installed[package] = "local"
end
elseif op == "download" then
local result = capture("opkg status " .. ipkg .. " 2>&1")
local package = result:match("Package: (%S+)")
if package then
catalog.installed[package] = "global"
end
elseif op == "remove" then
nixio.fs.remove(package_store .. "/" .. ipkg .. ".ipk")
catalog.installed[ipkg] = nil
end
2023-09-16 19:09:18 -06:00
os.remove(package_catalog)
for _,_ in pairs(catalog.installed)
do
write_all(package_catalog, luci.jsonc.stringify(catalog))
break
end
2023-09-15 22:16:27 -06:00
end
2022-03-13 21:19:04 -06:00
-- upload package
if parms.button_ul_pkg and nixio.fs.stat("/tmp/web/upload/file") then
os.execute("mv -f /tmp/web/upload/file /tmp/web/upload/newpkg.ipk")
os.execute("/usr/local/bin/uploadctlservices opkginstall > /dev/null 2>&1")
2022-03-17 13:28:20 -06:00
local result = capture("opkg -force-overwrite install /tmp/web/upload/newpkg.ipk 2>&1")
2022-12-27 09:46:47 -07:00
if result:match("satisfy_dependencies_for:") or result:match("cannot find dependency") then
2022-03-17 13:28:20 -06:00
-- dependency failure - silently update dependencies and try again
os.execute("opkg update > /dev/null 2>&1")
os.execute("opkg list | grep -v '^ ' | cut -f1,3 -d' ' | gzip -c > /etc/opkg.list.gz")
result = capture("opkg -force-overwrite install /tmp/web/upload/newpkg.ipk 2>&1")
end
pkgout(result)
2023-09-15 22:16:27 -06:00
record_package("upload", firmfile, "/tmp/web/upload/newpkg.ipk")
2022-03-13 21:19:04 -06:00
os.execute("rm -rf /tmp/opkg-*")
nixio.fs.remove("/tmp/web/upload/newpkg.ipk")
if os.execute("/usr/local/bin/uploadctlservices restore > /dev/null 2>&1") ~= 0 then
pkgout("Failed to restart all services, please reboot this node.")
2022-07-26 12:40:23 -06:00
else
update_advertised_services()
2022-03-13 21:19:04 -06:00
end
end
-- download package
if parms.button_dl_pkg and parms.dl_pkg ~= "default" then
if get_default_gw() ~= "none" or meshpkgs ~= "" then
os.execute("/usr/local/bin/uploadctlservices opkginstall > /dev/null 2>&1")
2022-03-17 19:36:08 -06:00
local result = capture("opkg -force-overwrite install " .. parms.dl_pkg .. " 2>&1")
2022-12-27 09:46:47 -07:00
if result:match("satisfy_dependencies_for:") or result:match("cannot find dependency") then
2022-03-17 19:36:08 -06:00
-- dependency failure - silently update dependencies and try again
os.execute("opkg update > /dev/null 2>&1")
os.execute("opkg list | grep -v '^ ' | cut -f1,3 -d' ' | gzip -c > /etc/opkg.list.gz")
result = capture("opkg -force-overwrite install " .. parms.dl_pkg .. " 2>&1")
end
pkgout(result)
2023-09-15 22:16:27 -06:00
record_package("download", parms.dl_pkg)
2022-03-13 21:19:04 -06:00
if os.execute("/usr/local/bin/uploadctlservices restore > /dev/null 2>&1") ~= 0 then
pkgout("Failed to restart all services, please reboot this node.")
2022-07-26 12:40:23 -06:00
else
update_advertised_services()
2022-03-13 21:19:04 -06:00
end
2013-11-14 23:11:16 -07:00
else
2022-03-13 21:19:04 -06:00
pkgout("Error: no route to Host")
end
end
-- refresh package list
if parms.button_refresh_pkg then
if get_default_gw() ~= "none" or meshpkgs ~= "" then
pkgout(capture("opkg update 2>&1"))
os.execute("opkg list | grep -v '^ ' | cut -f1,3 -d' ' | gzip -c > /etc/opkg.list.gz")
2013-11-14 23:11:16 -07:00
else
2022-03-13 21:19:04 -06:00
pkgout("Error: no route to Host")
end
end
-- remove package
if parms.button_rm_pkg and parms.rm_pkg ~= "default" and not permpkg[parms.rm_pkg] then
local output = capture("opkg remove " .. parms.rm_pkg .. " 2>&1")
pkgout(output)
if not output:match("No packages removed") then
2022-04-28 08:05:29 -06:00
pkgout("Package removed successfully")
2022-07-26 12:40:23 -06:00
update_advertised_services()
2023-09-15 22:16:27 -06:00
record_package("remove", parms.rm_pkg)
2022-03-13 21:19:04 -06:00
end
end
2022-08-29 15:49:45 -06:00
-- generate support data file
if parms.button_support_data then
os.execute("/www/cgi-bin/supporttool")
end
2022-03-13 21:19:04 -06:00
-- generate data structures
local pkgs = {}
local pkgver = {}
local f = io.popen("opkg list_installed | cut -f1,3 -d' '")
if f then
for line in f:lines()
do
local pkg, ver = line:match("(.+)%s(.+)")
if ver then
pkgs[#pkgs + 1] = pkg
pkgver[pkg] = ver
end
end
f:close()
end
local dl_pkgs = {}
local dlpkgver = {}
if nixio.fs.stat("/etc/opkg.list.gz") then
local f = io.popen("zcat /etc/opkg.list.gz")
if f then
for line in f:lines()
do
local pkg, ver = line:match("(.+)%s(.+)")
if ver and not (pkgver[pkg] and pkgver[pkg] == ver) then
dl_pkgs[#dl_pkgs + 1] = pkg
dlpkgver[pkg] = ver
end
end
f:close()
end
end
-- handle ssh key actions
local key_output = {}
function keyout(msg)
key_output[#key_output + 1] = msg
end
local keyfile = "/etc/dropbear/authorized_keys"
-- upload key
if parms.button_ul_key and nixio.fs.stat("/tmp/web/upload/file") then
local count = 0
if nixio.fs.stat(keyfile) then
for _ in io.lines(keyfile)
do
count = count + 1
end
end
os.execute("grep ^ssh- /tmp/web/upload/file >> " .. keyfile)
local count = 0
for _ in io.lines(keyfile)
do
count = count - 1
end
if count == 0 then
keyout("Error: file does not appear to be an ssh key file")
keyout("Authorized keys not changed.")
2013-11-14 23:11:16 -07:00
else
2022-03-13 21:19:04 -06:00
keyout("Key installed.")
end
nixio.fs.remove("/tmp/web/upload/file")
if os.execute("/usr/local/bin/uploadctlservices restore > /dev/null 2>&1") ~= 0 then
keyout("Failed to restart all services, please reboot this node.")
end
end
-- remove key
if parms.button_rm_key and parms.rm_key ~= "default" and nixio.fs.stat(keyfile) then
local count = 0
for _ in io.lines(keyfile)
do
count = count + 1
end
os.execute("grep -v '" .. parms.rm_key .. "' " .. keyfile .. " > " .. tmpdir .. "/keys")
os.execute("mv -f " .. tmpdir .. "/keys " .. keyfile)
for _ in io.lines(keyfile)
do
count = count - 1
end
if count == 0 then
keyout("Error: authorized keys were not changed.")
else
keyout("Key " .. parms.rm_key .. " removed.")
end
end
-- generate data structures
local keys = {}
local f = io.open(tmpdir .. "/newkeys", "w")
if f then
if nixio.fs.stat(keyfile) then
for line in io.lines(keyfile)
do
local type, key, who, extra = line:match("(%S+)%s+(%S+)%s+(%S+)(.*)")
if extra == "" and who:match(".@.") and type:match("^ssh-") then
keys[#keys + 1] = who
f:write(type .. " " .. key .. " " .. who .. "\n")
end
end
end
f:close()
end
-- sanitize the key file
if nixio.fs.stat(keyfile) and os.execute("diff " .. keyfile .. " " .. tmpdir .. "/newkeys >/dev/null 2>&1") ~= 0 then
os.execute("mv -f " .. tmpdir .. "/newkeys " .. keyfile)
keyout("Info: key file sanitized.")
end
remove_all("/tmp/web/upload")
remove_all(tmpdir)
-- generate the page
2023-06-29 00:47:25 -06:00
http_header(true)
2023-01-25 11:41:21 -07:00
html.header(node .. " administration", true)
html.print("<body><center>")
2022-03-13 21:19:04 -06:00
html.alert_banner()
2023-04-25 21:07:19 -06:00
html.print("<form method=post action=admin enctype='multipart/form-data'>")
2022-03-13 21:19:04 -06:00
-- nav
2023-04-25 21:07:19 -06:00
html.navbar_admin("admin")
html.print("<table width=850>")
2022-03-13 21:19:04 -06:00
html.print("<tr><td align=center><a href='/help.html#admin' target='_blank'>Help</a> ")
html.print("<input type=submit name=button_reboot value=Reboot style='font-weight:bold' title='Immediately reboot this node'>")
html.print("</td></tr>")
html.print("<tr><td align=center>")
2022-07-19 16:47:12 -06:00
html.print("<table cellspacing=10 width=100%>")
2022-03-13 21:19:04 -06:00
-- firmware
html.print("<tr><td align=center>")
2022-07-19 16:47:12 -06:00
html.print("<table cellspacing=10 width=100%>")
html.print("<tr><th colspan=3 style=background-color:lightseagreen>Firmware Update</th></tr>")
2022-03-13 21:19:04 -06:00
if #fw_output > 0 then
html.print("<tr><td colspan=3 align=center><table><tr><td><b>")
html.print("<pre>" .. word_wrap(80, fw_output) .. "</pre>")
html.print("</b></td></tr></table></td></tr>")
end
2024-08-05 18:11:53 -06:00
html.print("<tr><td align=center colspan=3><b>Current Version:</b> " .. fw_version .. " <b>Hardware Type:</b> (" .. targettype .. (firmware_subtype ~= "" and "-" .. firmware_subtype or "") .. ") " .. mfgprefix .. " (" .. hardwaretype .. ")</td></tr>")
2022-07-19 16:47:12 -06:00
html.print("<tr><td align=center colspan=3>Keep Existing Configuration Settings <input type=checkbox name=checkbox_keep_settings checked title='keep existing configuration settings'></td></tr>")
2023-03-10 16:39:23 -07:00
if nixio.fs.stat("/tmp/force-upgrade-this-is-dangerous") then
html.print("<tr><td align=center colspan=3><span style=background-color:red;font-size:140%;> ALL FIRMWARE UPGRADE COMPATIBILITY CHECKS HAVE BEEN DISABLED </span></td></tr>")
end
2022-03-13 21:19:04 -06:00
html.print("<tr>")
html.print("<td>Upload Firmware</td>")
2023-04-28 12:01:58 -06:00
html.print("<td><input style='width:100%' type=file name=firmfile title='choose the firmware file to install from your hard drive' accept='." .. firmware_type .. "'></td>")
2023-05-03 18:22:30 -06:00
html.print("<td align=center><input type=submit name=button_ul_fw value=Upload title='install the firmware' style='width:90px' onclick='this.value=\"Uploading...\"'></td>")
2022-03-13 21:19:04 -06:00
html.print("</tr>")
html.print("<tr>")
html.print("<td>Download Firmware</td>")
html.print("<td><select name=dl_fw style='font-family:monospace'>")
html.print("<option value=default selected>- Select Firmware -</option>")
2023-01-25 11:41:21 -07:00
for _, fwi in ipairs(fw_names)
2022-03-13 21:19:04 -06:00
do
2023-01-25 11:41:21 -07:00
html.print("<option value=" .. fwi .. ">" .. fwi .. (fwi:match("%-") and " (nightly)" or "") .. "</option>")
2022-03-13 21:19:04 -06:00
end
html.print("</select>")
html.print("<input type=submit name=button_refresh_fw value=Refresh title='download the list of available firmware versions'>")
2023-05-03 18:22:30 -06:00
html.print("<td align=center><input type=submit name=button_dl_fw value=Download title='install the firmware' style='width:90px' onclick='this.value=\"Downloading...\"'></td>")
2022-03-13 21:19:04 -06:00
html.print("</tr>")
2022-07-19 16:47:12 -06:00
2022-05-08 10:13:11 -06:00
html.print("<tr>")
html.print("<td>Load Local Firmware</td>")
2022-07-19 16:47:12 -06:00
html.print("<td style='font-family:monospace'>")
2023-04-28 12:01:58 -06:00
html.print("<input type=submit name=button_apply_fw value='Apply Local Firmware' title='apply firmware previously uploaded to this node and named as shown' " .. (nixio.fs.stat("/tmp/web/" .. local_firmware) and "" or "disabled") .. ">")
html.print(" /tmp/web/" .. local_firmware .. "</td>")
2022-05-08 10:13:11 -06:00
html.print("</tr>")
2022-03-13 21:19:04 -06:00
html.print("</table></td></tr>")
2022-07-19 16:47:12 -06:00
html.print("<tr><td colspan=3></td></tr>")
2022-03-13 21:19:04 -06:00
-- packages
html.print("<tr><td align=center>")
2022-07-19 16:47:12 -06:00
html.print("<table cellspacing=10 width=100%>")
html.print("<tr><th colspan=3 style=background-color:lightseagreen>Package Management</th></tr>")
2022-03-13 21:19:04 -06:00
2022-10-14 14:32:22 -06:00
-- low memory warning
if isLowMemNode() then
html.print("<tr><td align=center colspan=3><span style=background-color:cyan;font-size:140%;> Recommend not to install extra packages due to low memory on this node </span></td></tr>")
end
2022-03-13 21:19:04 -06:00
if #pkg_output > 0 then
-- opkg can produce duplicate first lines, remove them here
while pkg_output[2] and pkg_output[1] == pkg_output[2]
do
pkg_output:remove(1)
end
html.print("<tr><td colspan=3 align=center><table><tr><td><b><pre>")
html.print(word_wrap(80, pkg_output))
html.print("</pre></b></td></tr></table></td></tr>")
end
html.print("<tr>")
html.print("<td>Upload Package</td>")
2022-07-20 17:15:13 -06:00
html.print("<td><input style='width:100%' type=file name=ul_pkg title='choose the .ipk file to install from your hard drive'> </td>")
2022-03-17 19:57:48 -06:00
html.print("<td align=center><input type=submit name=button_ul_pkg value=Upload title='install the package'>")
2022-03-13 21:19:04 -06:00
html.print("</td>")
html.print("</tr>")
html.print("<tr>")
html.print("<td>Download Package</td>")
html.print("<td><select name=dl_pkg style='font-family:monospace'>")
html.print("<option value=default selected>- Select Package -</option>")
for _, dlp in ipairs(dl_pkgs)
do
html.print("<option value=" .. dlp .. ">" .. dlp .. " " .. dlpkgver[dlp] .. "</option>")
end
html.print("</select>")
html.print("<input type=submit name=button_refresh_pkg value=Refresh title='download the list of available packages (warning: this takes a lot of space)'>")
html.print("<td align=center><input type=submit name=button_dl_pkg value=Download title='install the package'></td>")
html.print("</tr>")
html.print("<tr>")
html.print("<td>Remove Package</td>")
html.print("<td><select name=rm_pkg style='font-family:monospace'>")
html.print("<option value=default selected>- Select Package -</option>")
for _, pkg in ipairs(pkgs)
do
html.print("<option value=" .. pkg .. " " .. (permpkg[pkg] and "disabled" or "") .. ">" .. pkg .. " " .. pkgver[pkg] .. "</option>")
end
html.print("</select></td>")
html.print("<td align=center><input type=submit name=button_rm_pkg value=Remove title='remove the selected package'></td>")
html.print("</tr>")
html.print("</table></td></tr>")
2022-07-19 16:47:12 -06:00
html.print("<tr><td colspan=3></td></tr>")
2022-03-13 21:19:04 -06:00
-- ssh keys
html.print("<tr><td align=center>")
2022-07-19 16:47:12 -06:00
html.print("<table cellspacing=10 width=100%>")
html.print("<tr><th colspan=3 style=background-color:lightseagreen>Authorized SSH Keys</th></tr>")
2022-03-13 21:19:04 -06:00
if #key_output > 0 then
html.print("<tr><td colspan=3 align=center><table><tr><td><b><pre>")
html.print(word_wrap(80, key_output))
html.print("</pre></b></td></tr></table></td></tr>")
end
html.print("<tr>")
html.print("<td>Upload Key</td>")
2022-07-20 17:15:13 -06:00
html.print("<td><input style='width:100%' type=file name=sshkey title='choose the id_rsa.pub file to install from your hard drive'></td>")
2022-03-13 21:19:04 -06:00
html.print("<td align=center><input type=submit name=button_ul_key value=Upload title='install the key'")
html.print("></td>")
html.print("</tr>")
html.print("<tr>")
html.print("<td>Remove Key</td>")
html.print("<td><select name=rm_key style='font-family:monospace'>")
html.print("<option value=default selected>- Select Key -</option>")
for _, key in ipairs(keys)
do
html.print("<option value=" .. key .. ">" .. key .. "</option>")
end
html.print("</select>")
html.print("<td align=center><input type=submit name=button_rm_key value=Remove title='remove the selected key'></td>")
html.print("</tr>")
html.print("</table></td></tr>")
2022-07-19 16:47:12 -06:00
html.print("<tr><td colspan=3></td></tr>")
2022-03-13 21:19:04 -06:00
2022-07-19 16:47:12 -06:00
html.print("<tr><th colspan=3 style=background-color:lightseagreen>Support Data</th></tr>")
2022-08-29 15:49:45 -06:00
html.print("<tr><td colspan=3 align=center><input type=submit name=button_support_data value='Download Support Data' title='Download a Support Data file'></td></tr>")
2022-03-13 21:19:04 -06:00
2022-07-19 16:47:12 -06:00
html.print("<tr><td colspan=3></td></tr>")
2022-03-13 21:19:04 -06:00
html.print("</table>")
html.print("</td></tr>")
html.print("</table>")
html.print("</form>")
html.print("</center>")
html.footer()
html.print("</body>")
html.print("</html>")
http_footer()