#!/usr/bin/lua
--[[
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
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 .
Additional Terms:
Additional use restrictions exist on the AREDN(TM) trademark and logo.
See AREDNLicense.txt for more info.
Attributions to the AREDN Project must be retained in the source code.
If importing this code into a new or existing project attribution
to the AREDN project must be added to the source code.
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")
aredn.html = require("aredn.html")
require("uci")
aredn.info = require("aredn.info")
require("ubus")
require("luci.sys")
local html = aredn.html
local cursor = uci.cursor()
local conn = ubus.connect()
-- handle firmware updates
local fw_install = false
local patch_install = false
local fw_output = {}
local fw_images = {}
local fw_md5 = {}
local fw_version = ""
function fwout(msg)
fw_output[#fw_output + 1] = msg
end
function 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
if nixio.fs.stat("/tmp/web/firmware.list") then
for line in io.lines("/tmp/web/firmware.list")
do
local md5, fw, tag = line:match("^(%S+) (%S+) (.*)")
if tag and tag ~= "none" and (tag == "all" or fw_version:match(tag)) then
fw_images[#fw_images + 1] = fw
fw_md5[fw] = md5
end
end
end
end
local tunnel_active = false
if nixio.fs.stat("/usr/sbin/vtund") and nixio.fs.stat("/etc/config/vtun") then
for line in io.lines("/etc/config/vtun")
do
if line:match("option enabled '1'") then
tunnel_active = true
break
end
end
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 reboot()
local node = aredn.info.get_nvram("node")
if node == "" then
node = "Node"
end
local lanip, _, lanmask = aredn.hardware.get_interface_ip4(aredn.hardware.get_iface_name("lan"))
local browser = os.getenv("REMOTE_ADDR")
local browser6 = browser:match("::ffff:([%d%.]+)")
if browser6 then
browser = browser6
end
local fromlan = false
local subnet_change = false
if lanip then
fromlan = validate_same_subnet(browser, lanip, lanmask)
if fromlan then
lanmask = ip_to_decimal(lanmask)
local cfgip = cursor:get("network", "lan", "ipaddr")
local cfgmask = ip_to_decimal(cursor:get("network", "lan", "netmask"))
if lanmask ~= cfgmask or nixio.bit.band(ip_to_decimal(lanip), lanmask) ~= nixio.bit.band(ip_to_decimal(cfgip), cfgmask) then
subnet_change = true
end
end
end
http_header()
if fromlan and subnet_change then
html.header(node .. " rebooting", true)
html.print("
")
html.print("
" .. node .. " is rebooting
")
html.print("
The LAN subnet has changed. You will need to acquire a new DHCP lease ")
html.print("and reset any name service caches you may be using.