#!/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® 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")
require("aredn.html")
require("uci")
require("aredn.info")
require("aredn.olsr")
local html = aredn.html
local cursor = uci.cursor()
local config = aredn.info.get_nvram("config")
local node = aredn.info.get_nvram("node")
if not node or node == "" then
node = "NOCALL"
end
local tactical = aredn.info.get_nvram("tactical")
if not tactical then
tactical = ""
end
-- post_data
local parms = {}
if os.getenv("REQUEST_METHOD") == "POST" then
require('luci.http')
local request = luci.http.Request(nixio.getenv(),
function()
local v = io.read(1024)
if not v then
io.close()
end
return v
end
)
parms = request:formvalue()
end
local function validate_service_name(name)
if not name or name == "" or name:match("[:-\"|<>]") then
return false
else
return true
end
end
local function validate_service_protocol(proto)
if not proto or proto == "" or proto:match("[:-\"|<>]") or not proto:match("^%w+") then
return false
else
return true
end
end
local function validate_service_suffix(suffix)
if not suffix or suffix:match("[:-\"|<>]") or not suffix:match("^[%w/?&._=#-]*$") then
return false
else
return true
end
end
local mac_pattern = "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x"
local mac_wildcard = "*:*:*:*:*:*"
-- accepts nonempty strings
local function validate_nonempty_matcher(matcher)
return matcher ~= ""
end
-- accepts MAC addresses with optional bytewise wildcard substitution
local function validate_mac_matcher(matcher)
return matcher:gsub("%x%x", "*") == mac_wildcard
end
-- accepts sequences of one or more hexadecimal digit pairs separated by colons
local function validate_hexbytes_matcher(matcher)
return matcher:gsub("[XY]", "Z"):gsub("%x%x:", "X"):gsub("%x%x", "Y"):match("^X*Y$")
end
-- accepts host names with optional trailing wildcard
local function validate_host_matcher(matcher)
return matcher:match("^%w[%w.-]*%*?$")
end
-- accepts Net-ASCII (RFC 854) with escapes for supported control characters
local function validate_net_ascii(matcher)
local no_escapes = matcher:gsub("\\[\\benrt]", "")
return not no_escapes:match("[^ -Z^-~%[%]]")
end
local FORCE_SUPPORTED_HINT = "Send option even when not requested by client"
local FORCE_UNSUPPORTED_HINT = "Only options with an explicit tag can be sent always."
local dhcp_tag_conds = {
{ key = "vendorclass",
name = "Vendor Class",
validator = validate_nonempty_matcher,
hint = "all or part of the class value sent by the client",
jsPlaceholder = "subpart of class string",
},
{ key = "userclass",
name = "User Class",
validator = validate_nonempty_matcher,
hint = "all or part of the class value sent by the client",
jsPlaceholder = "subpart of class string",
},
{ key = "mac",
name = "MAC Address",
validator = validate_mac_matcher,
hint = "six bytes, each either two hexadecimal digits or a single asterisk, separated by colons",
jsPattern = [[^(?:(?:[0-9a-fA-F]{2}|\\*):){5}(?:[0-9a-fA-F]{2}|\\*)$]],
jsPlaceholder = "*:*:*:*:*:*",
},
{ key = "circuitid",
name = "Agent Circuit ID",
validator = validate_hexbytes_matcher,
hint = "one or more bytes, each two hexadecimal digits, separated by colons",
jsPattern = [[^(?:(?:[0-9a-fA-F]{2}|\\*):)*(?:[0-9a-fA-F]{2}|\\*)$]],
jsPlaceholder = "12:3a:bc",
},
{ key = "remoteid",
name = "Agent Remote ID",
validator = validate_hexbytes_matcher,
hint = "one or more bytes, each two hexadecimal digits, separated by colons",
jsPattern = [[^(?:(?:[0-9a-fA-F]{2}|\\*):)*(?:[0-9a-fA-F]{2}|\\*)$]],
jsPlaceholder = "12:3a:bc",
},
{ key = "subscriberid",
name = "Subscriber-ID",
validator = validate_net_ascii,
hint = [[ASCII printing characters and \\ escapes; use \\\\ \\b \\e \\n \\r or \\t for \\, BS, ESC, LF, CR, or HT, respectively]],
jsPattern = [[^(?:[ -\\[\\]-~]|\\\\[\\\\benrt])+$]],
jsPlaceholder = "ASCII string\\\\r\\\\n& escapes",
},
}
local dhcp_tag_validators = {}
for _, cond in ipairs(dhcp_tag_conds)
do
dhcp_tag_validators[cond.key] = {
validator = cond.validator,
hint = cond.hint,
jsPattern = cond.jsPattern,
jsPlaceholder = cond.jsPlaceholder}
end
local function html_safe(s)
return s:gsub("&", "&"):gsub(">", ">"):gsub("<", "<")
end
local serv_err = {}
local function serverr(msg)
serv_err[#serv_err + 1] = msg:gsub(">", ">"):gsub("<", "<")
end
local port_err = {}
local function porterr(msg)
port_err[#port_err + 1] = msg
end
local dmz_err = {}
local function dmzerr(msg)
dmz_err[#dmz_err + 1] = msg
end
local dhcp_err = {}
local function dhcperr(msg)
dhcp_err[#dhcp_err + 1] = msg
end
local dhcptag_err = {}
local function dhcptagerr(msg)
dhcptag_err[#dhcptag_err + 1] = msg
end
local dhcpopt_err = {}
local function dhcpopterr(msg)
dhcpopt_err[#dhcpopt_err + 1] = msg
end
local alias_err = {}
local function aliaserr(msg)
alias_err[#alias_err + 1] = msg
end
local errors = {}
local function err(msg)
errors[#errors + 1] = msg
end
local hidden = {}
local function hide(m)
hidden[#hidden + 1] = m
end
local hosts = {}
local addrs = {}
local macs = {}
if config == "" or nixio.fs.stat("/tmp/reboot-required") then
http_header()
html.header(node .. " setup", true)
html.print("
")
html.alert_banner()
html.navbar_admin("ports")
html.print(" ")
if config == "" then
html.print("This page is not available until the configuration has been set.")
else
html.print("The specified configuration is invalid, try flushing your browser cache or reboot the mesh node.")
end
html.print(" |
")
html.print("
")
html.footer();
html.print("