aredn/files/www/cgi-bin/supporttool

219 lines
6.3 KiB
Lua
Executable File

#!/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 <http://www.gnu.org/licenses/>.
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.utils")
require("uci")
require("iwinfo")
local wifiif = uci.cursor():get("network", "wifi", "device")
local phy = iwinfo.nl80211.phyname(wifiif)
local mfg = capture("/usr/local/bin/get_hardware_mfg"):chomp()
local files = {
"/etc/config/",
"/etc/config.mesh/",
"/etc/local/",
"/etc/mesh-release",
"/etc/os-release",
"/tmp/etc/",
"/var/run/hosts_olsr",
"/var/run/hosts_olsr.stable",
"/var/run/services_olsr",
"/tmp/rssi.dat",
"/tmp/rssi.log",
"/tmp/rssi_ath10k.log",
"/tmp/station_monitor.log",
"/tmp/olsrd.log",
"/tmp/manager.log",
"/tmp/manager.log.0",
"/tmp/dnsmasq.d/",
"/tmp/AutoDistReset.log",
"/tmp/lqm.info",
"/tmp/wireless_monitor.info",
"/tmp/wireless_monitor.log",
"/tmp/sysinfo/board_name",
"/tmp/sysinfo/boardid",
"/tmp/sysinfo/hardware_mfg",
"/tmp/sysinfo/hardware_type",
"/tmp/sysinfo/model",
"/sys/kernel/debug/ieee80211/phy0/ath9k/ack_to",
"/sys/kernel/debug/ieee80211/phy1/ath9k/ack_to",
"/etc/board.json"
}
local sensitive = {
"/etc/config/vtun",
"/etc/config.mesh/vtun",
"/etc/config/network",
"/etc/config.mesh/wireguard"
}
local cmds = {
"cat /proc/cpuinfo",
"cat /proc/meminfo",
"df -k",
"dmesg",
"ifconfig",
"ethtool eth0",
"ethtool eth1",
"ip link",
"ip addr",
"ip neigh",
"ip route list",
"ip route list table 29",
"ip route list table 30",
"ip route list table 31",
"ip route list table main",
"ip route list table default",
"ip rule list",
"netstat -aln",
"iwinfo",
"iwinfo " .. wifiif .. " assoclist",
phy and "iw phy " .. phy .. " info" or "",
"iw dev " .. wifiif .. " info",
"iw dev " .. wifiif .. " scan",
"iw dev " .. wifiif .. " station dump",
"nft list ruleset",
"md5sum /www/cgi-bin/*",
"echo /all | nc 127.0.0.1 2006",
"opkg list-installed",
"ps -w",
"/usr/local/bin/get_hardwaretype",
"/usr/local/bin/get_boardid",
"/usr/local/bin/get_model",
"/usr/local/bin/get_hardware_mfg",
"logread",
}
local cmds_ubnt = {
"cat /dev/mtd0|grep 'U-Boot'|head -n1"
}
-- need space for this
local vfs = nixio.fs.statvfs("/tmp")
local fspace = vfs.bfree * vfs.bsize / 1024
if fspace < 2048 then
os.exit(1)
end
remove_all("/tmp/sd")
for _, path in ipairs(files)
do
if nixio.fs.stat(path) then
local m = path:match("^/(.*/).*/$")
if m then
os.execute("mkdir -p /tmp/sd/" .. m);
os.execute("cp -r -p " .. path .. " /tmp/sd/" .. m)
else
m = path:match("^/(.*/).*")
os.execute("mkdir -p /tmp/sd/" .. m);
os.execute("cp -r -p " .. path .. " /tmp/sd/" .. m)
end
end
end
-- hide sensitive information
for _, path in ipairs(sensitive)
do
local file = "/tmp/sd" .. path
if nixio.fs.stat(file) then
local lines = read_all(file)
local nlines = {}
for line in lines:gmatch("([^\n]*\n?)")
do
line = line:gsub("option passwd.+", "option passwd '***HIDDEN***'\n")
line = line:gsub("option public_key.+", "option public_key '***HIDDEN***'\n")
line = line:gsub("option private_key.+", "option private_key '***HIDDEN***'\n")
line = line:gsub("option key.+", "option key '***HIDDEN***'\n")
nlines[#nlines + 1] = line
end
write_all(file, table.concat(nlines, ""))
end
end
-- remove passwords from config file
os.execute("sed -i -e 's/ key.*$/ key ******/' /tmp/sd/etc/config/wireless")
os.execute("sed -i -e 's/_key =.*$/_key =/' /tmp/sd/etc/config.mesh/_setup")
local f = io.open("/tmp/sd/data.txt", "w")
if f then
if mfg == "Ubiquiti" then
for _, cmd in ipairs(cmds_ubnt)
do
local p = io.popen(cmd)
if p then
f:write("\n========== " .. cmd .. " (UBNT only) ==========\n")
f:write(p:read("*a"))
p:close()
end
end
end
for _, cmd in ipairs(cmds)
do
local p = io.popen(cmd)
if p then
f:write("\n========== " .. cmd .. " ==========\n")
f:write(p:read("*a"))
p:close()
end
end
f:close()
end
os.execute("tar -zcf /tmp/supportdata.tar.gz -C /tmp/sd ./")
-- cleaup the temp files
remove_all("/tmp/sd")
local nodename = capture("uname -n"):chomp()
local tstamp = capture("date +%Y%m%d%H%M"):chomp()
if os.getenv("GATEWAY_INTERFACE") ~= "" then
local fn = "supportdata-" .. nodename .. "-" .. tstamp .. ".tar.gz"
print("Content-type: application/x-gzip\r")
print("Content-Disposition: attachment; filename=" .. fn .. "\r")
print("\r")
io.write(read_all("/tmp/supportdata.tar.gz"))
else
local fn = "/tmp/supportdata-" .. nodename .. "-" .. tstamp .. ".tar.gz"
nixio.fs.remove(fn)
nixio.fs.rename("/tmp/supportdata.tar.gz", fn)
print("File created: " .. fn)
print("Please copy this file and remove from the node")
print("to free up resources.")
end