3 local json = require "luci.jsonc"
4 local fs = require "nixio.fs"
6 local function readfile(path)
7 local s = fs.readfile(path)
8 return s and (s:gsub("^%s+", ""):gsub("%s+$", ""))
13 args = { name = "name" },
15 local sys = require "luci.sys"
16 local _, name, scripts = nil, nil, {}
17 for _, name in ipairs(args.name and { args.name } or sys.init.names()) do
18 local index = sys.init.index(name)
20 scripts[name] = { index = index, enabled = sys.init.enabled(name) }
22 return { error = "No such init script" }
30 args = { name = "name", action = "action" },
32 local sys = require "luci.sys"
33 if type(sys.init[args.action]) ~= "function" then
34 return { error = "Invalid action" }
36 return { result = sys.init[args.action](args.name) }
42 return { result = os.time() }
47 args = { localtime = 0 },
49 local sys = require "luci.sys"
50 local date = os.date("*t", args.localtime)
52 sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d' >/dev/null" %{ date.year, date.month, date.day, date.hour, date.min, date.sec })
53 sys.call("/etc/init.d/sysfixtime restart >/dev/null")
55 return { result = args.localtime }
61 local util = require "luci.util"
62 local zones = require "luci.sys.zoneinfo"
64 local tz = readfile("/etc/TZ")
65 local res = util.ubus("uci", "get", {
67 section = "@system[0]",
73 for _, zone in ipairs(zones.TZ) do
76 active = (res and res.value == zone[1]) and true or nil
85 local iter = fs.dir("/sys/class/leds")
93 result[led] = { triggers = {} }
95 s = readfile("/sys/class/leds/"..led.."/trigger")
96 for s in (s or ""):gmatch("%S+") do
97 m = s:match("^%[(.+)%]$")
98 result[led].triggers[#result[led].triggers+1] = m or s
99 result[led].active_trigger = m or result[led].active_trigger
102 s = readfile("/sys/class/leds/"..led.."/brightness")
104 result[led].brightness = tonumber(s)
107 s = readfile("/sys/class/leds/"..led.."/max_brightness")
109 result[led].max_brightness = tonumber(s)
120 local fs = require "nixio.fs"
121 local iter = fs.glob("/sys/bus/usb/devices/[0-9]*/manufacturer")
129 local id = p:match("/([^/]+)/manufacturer$")
131 result.devices[#result.devices+1] = {
133 vid = readfile("/sys/bus/usb/devices/"..id.."/idVendor"),
134 pid = readfile("/sys/bus/usb/devices/"..id.."/idProduct"),
135 vendor = readfile("/sys/bus/usb/devices/"..id.."/manufacturer"),
136 product = readfile("/sys/bus/usb/devices/"..id.."/product"),
137 speed = tonumber((readfile("/sys/bus/usb/devices/"..id.."/product")))
142 iter = fs.glob("/sys/bus/usb/devices/*/*-port[0-9]*")
149 local port = p:match("([^/]+)$")
150 local link = fs.readlink(p.."/device")
152 result.ports[#result.ports+1] = {
154 device = link and fs.basename(link)
163 getConntrackHelpers = {
165 local ok, fd = pcall(io.open, "/usr/share/fw3/helpers.conf", "r")
168 if not (ok and fd) then
169 ok, fd = pcall(io.open, "/usr/share/firewall4/helpers", "r")
176 local line = fd:read("*l")
181 if line:match("^%s*config%s") then
187 local opt, val = line:match("^%s*option%s+(%S+)%s+(%S.*)$")
189 opt = opt:gsub("^'(.+)'$", "%1"):gsub('^"(.+)"$', "%1")
190 val = val:gsub("^'(.+)'$", "%1"):gsub('^"(.+)"$', "%1")
203 return { result = rv }
209 local fs = require "nixio.fs"
213 rv.firewall = fs.access("/sbin/fw3")
214 rv.firewall4 = fs.access("/sbin/fw4")
215 rv.opkg = fs.access("/bin/opkg")
216 rv.offloading = fs.access("/sys/module/xt_FLOWOFFLOAD/refcnt") or fs.access("/sys/module/nft_flow_offload/refcnt")
217 rv.br2684ctl = fs.access("/usr/sbin/br2684ctl")
218 rv.swconfig = fs.access("/sbin/swconfig")
219 rv.odhcpd = fs.access("/usr/sbin/odhcpd")
220 rv.zram = fs.access("/sys/class/zram-control")
221 rv.sysntpd = fs.readlink("/usr/sbin/ntpd") and true
222 rv.ipv6 = fs.access("/proc/net/ipv6_route")
223 rv.dropbear = fs.access("/usr/sbin/dropbear")
224 rv.cabundle = fs.access("/etc/ssl/certs/ca-certificates.crt")
225 rv.relayd = fs.access("/usr/sbin/relayd")
226 rv.dsl = fs.access("/sbin/dsl_cpe_control") or fs.access("/sbin/vdsl_cpe_control")
228 local wifi_features = { "eap", "11n", "11ac", "11r", "acs", "sae", "owe", "suiteb192", "wep", "wps" }
230 if fs.access("/usr/sbin/hostapd") then
231 rv.hostapd = { cli = fs.access("/usr/sbin/hostapd_cli") }
234 for _, feature in ipairs(wifi_features) do
235 rv.hostapd[feature] =
236 (os.execute(string.format("/usr/sbin/hostapd -v%s >/dev/null 2>/dev/null", feature)) == 0)
240 if fs.access("/usr/sbin/wpa_supplicant") then
241 rv.wpasupplicant = { cli = fs.access("/usr/sbin/wpa_cli") }
244 for _, feature in ipairs(wifi_features) do
245 rv.wpasupplicant[feature] =
246 (os.execute(string.format("/usr/sbin/wpa_supplicant -v%s >/dev/null 2>/dev/null", feature)) == 0)
250 ok, fd = pcall(io.popen, "dnsmasq --version 2>/dev/null")
255 local line = fd:read("*l")
260 local opts = line:match("^Compile time options: (.+)$")
263 for opt in opts:gmatch("%S+") do
264 local no = opt:match("^no%-(%S+)$")
265 rv.dnsmasq[string.lower(no or opt)] = not no
274 ok, fd = pcall(io.popen, "ipset --help 2>/dev/null")
281 local line = fd:read("*l")
284 elseif line:match("^Supported set types:") then
287 local set, ver = line:match("^%s+(%S+)%s+(%d+)")
288 if set and not rv.ipset[set] then
289 rv.ipset[set] = tonumber(ver)
301 getSwconfigFeatures = {
302 args = { switch = "switch0" },
303 call = function(args)
304 local util = require "luci.util"
306 -- Parse some common switch properties from swconfig help output.
307 local swc, err = io.popen("swconfig dev %s help 2>/dev/null" % util.shellquote(args.switch))
309 local is_port_attr = false
310 local is_vlan_attr = false
314 local line = swc:read("*l")
315 if not line then break end
317 if line:match("^%s+%-%-vlan") then
320 elseif line:match("^%s+%-%-port") then
324 elseif line:match("cpu @") then
325 rv.switch_title = line:match("^switch%d: %w+%((.-)%)")
326 rv.num_vlans = tonumber(line:match("vlans: (%d+)")) or 16
329 elseif line:match(": pvid") or line:match(": tag") or line:match(": vid") then
330 if is_vlan_attr then rv.vid_option = line:match(": (%w+)") end
332 elseif line:match(": enable_vlan4k") then
333 rv.vlan4k_option = "enable_vlan4k"
335 elseif line:match(": enable_vlan") then
336 rv.vlan_option = "enable_vlan"
338 elseif line:match(": enable_learning") then
339 rv.learning_option = "enable_learning"
341 elseif line:match(": enable_mirror_rx") then
342 rv.mirror_option = "enable_mirror_rx"
344 elseif line:match(": max_length") then
345 rv.jumbo_option = "max_length"
352 return { error = "No such switch" }
357 return { error = err }
362 getSwconfigPortState = {
363 args = { switch = "switch0" },
364 call = function(args)
365 local util = require "luci.util"
367 local swc, err = io.popen("swconfig dev %s show 2>/dev/null" % util.shellquote(args.switch))
372 local line = swc:read("*l")
373 if not line or (line:match("^VLAN %d+:") and #ports > 0) then
377 local pnum = line:match("^Port (%d+):$")
380 port = tonumber(pnum),
389 ports[#ports+1] = port
395 if line:match("full[%- ]duplex") then
399 m = line:match(" speed:(%d+)")
401 port.speed = tonumber(m)
404 m = line:match("(%d+) Mbps")
405 if m and port.speed == 0 then
406 port.speed = tonumber(m)
409 m = line:match("link: (%d+)")
410 if m and port.speed == 0 then
411 port.speed = tonumber(m)
414 if line:match("link: ?up") or line:match("status: ?up") then
418 if line:match("auto%-negotiate") or line:match("link:.-auto") then
422 if line:match("link:.-rxflow") then
426 if line:match("link:.-txflow") then
434 if not next(ports) then
435 return { error = "No such switch" }
438 return { result = ports }
440 return { error = err }
446 args = { username = "root", password = "password" },
447 call = function(args)
448 local util = require "luci.util"
450 result = (os.execute("(echo %s; sleep 1; echo %s) | /bin/busybox passwd %s >/dev/null 2>&1" %{
451 luci.util.shellquote(args.password),
452 luci.util.shellquote(args.password),
453 luci.util.shellquote(args.username)
461 local fs = require "nixio.fs"
463 local block = io.popen("/sbin/block info", "r")
468 local ln = block:read("*l")
473 local dev = ln:match("^/dev/(.-):")
475 local s = tonumber((fs.readfile("/sys/class/block/" .. dev .."/size")))
477 dev = "/dev/" .. dev,
482 for key, val in ln:gmatch([[(%w+)="(.-)"]]) do
494 return { error = "Unable to execute block utility" }
501 return { result = (os.execute("/sbin/block detect > /etc/config/fstab") == 0) }
507 local fs = require "nixio.fs"
509 local fd, err = io.open("/proc/mounts", "r")
514 local ln = fd:read("*l")
519 local device, mount, fstype, options, freq, pass = ln:match("^(%S*) (%S*) (%S*) (%S*) (%d+) (%d+)$")
520 if device and mount then
521 device = device:gsub("\\(%d+)", function(n) return string.char(tonumber(n, 8)) end)
522 mount = mount:gsub("\\(%d+)", function(n) return string.char(tonumber(n, 8)) end)
524 local stat = fs.statvfs(mount)
525 if stat and stat.blocks > 0 then
529 size = stat.bsize * stat.blocks,
530 avail = stat.bsize * stat.bavail,
531 free = stat.bsize * stat.bfree
539 return { result = rv }
541 return { error = err }
547 args = { mode = "interface", device = "eth0" },
548 call = function(args)
549 local util = require "luci.util"
552 if args.mode == "interface" then
553 flags = "-i %s" % util.shellquote(args.device)
554 elseif args.mode == "wireless" then
555 flags = "-r %s" % util.shellquote(args.device)
556 elseif args.mode == "conntrack" then
558 elseif args.mode == "load" then
561 return { error = "Invalid mode" }
564 local fd, err = io.popen("luci-bwc %s" % flags, "r")
566 local parse = json.new()
572 local ln = fd:read("*l")
577 done, err = parse:parse((ln:gsub("%d+", "%1.0")))
580 err = "Unexpected JSON data"
590 done, err = parse:parse("]")
593 return { error = err }
595 return { error = "Incomplete JSON data" }
597 return { result = parse:get() }
600 return { error = err }
607 local sys = require "luci.sys"
608 return { result = sys.net.conntrack() }
614 local sys = require "luci.sys"
616 for _, v in pairs(sys.process.list()) do
619 return { result = res }
624 local function parseInput()
625 local parse = json.new()
629 local chunk = io.read(4096)
632 elseif not done and not err then
633 done, err = parse:parse(chunk)
638 print(json.stringify({ error = err or "Incomplete input" }))
645 local function validateArgs(func, uargs)
646 local method = methods[func]
648 print(json.stringify({ error = "Method not found" }))
652 if type(uargs) ~= "table" then
653 print(json.stringify({ error = "Invalid arguments" }))
657 uargs.ubus_rpc_session = nil
660 local margs = method.args or {}
661 for k, v in pairs(uargs) do
662 if margs[k] == nil or
663 (v ~= nil and type(v) ~= type(margs[k]))
665 print(json.stringify({ error = "Invalid arguments" }))
673 if arg[1] == "list" then
674 local _, method, rv = nil, nil, {}
675 for _, method in pairs(methods) do rv[_] = method.args or {} end
676 print((json.stringify(rv):gsub(":%[%]", ":{}")))
677 elseif arg[1] == "call" then
678 local args = parseInput()
679 local method = validateArgs(arg[2], args)
680 local result, code = method.call(args)
681 print((json.stringify(result):gsub("^%[%]$", "{}")))