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)
165 return { result = nixio.getifaddrs() }
171 local fp = io.open('/var/hosts/odhcpd')
174 for line in fp:lines() do
175 local dev, duid, name = string.match(line, '# (%S+)%s+(%S+)%s+%d+%s+(%S+)')
176 if dev and duid and name then
178 name = (name ~= "-") and name or nil,
189 getNetworkDevices = {
190 call = function(args)
191 local dir = fs.dir("/sys/class/net")
196 if not result[dev] then
197 result[dev] = { name = dev }
200 if fs.access("/sys/class/net/"..dev.."/master") then
201 local brname = fs.basename(fs.readlink("/sys/class/net/"..dev.."/master"))
202 if not result[brname] then
203 result[brname] = { name = brname }
206 if not result[brname].ports then
207 result[brname].ports = { }
210 result[brname].ports[#result[brname].ports+1] = dev
211 elseif fs.access("/sys/class/net/"..dev.."/bridge") then
212 if not result[dev].ports then
213 result[dev].ports = { }
216 result[dev].id = readfile("/sys/class/net/"..dev.."/bridge/bridge_id")
217 result[dev].stp = (readfile("/sys/class/net/"..dev.."/bridge/stp_state") ~= "0")
218 result[dev].bridge = true
221 local opr = readfile("/sys/class/net/"..dev.."/operstate")
223 result[dev].up = (opr == "up" or opr == "unknown")
224 result[dev].type = tonumber(readfile("/sys/class/net/"..dev.."/type"))
225 result[dev].name = dev
227 local mtu = tonumber(readfile("/sys/class/net/"..dev.."/mtu"))
228 if mtu and mtu > 0 then
229 result[dev].mtu = mtu
232 local qlen = tonumber(readfile("/sys/class/net/"..dev.."/tx_queue_len"))
233 if qlen and qlen > 0 then
234 result[dev].qlen = qlen
237 local master = fs.readlink("/sys/class/net/"..dev.."/master")
239 result[dev].master = fs.basename(master)
242 local mac = readfile("/sys/class/net/"..dev.."/address")
243 if mac and #mac == 17 then
244 result[dev].mac = mac
252 getConntrackHelpers = {
254 local ok, fd = pcall(io.open, "/usr/share/fw3/helpers.conf", "r")
261 local line = fd:read("*l")
266 if line:match("^%s*config%s") then
272 local opt, val = line:match("^%s*option%s+(%S+)%s+(%S.*)$")
274 opt = opt:gsub("^'(.+)'$", "%1"):gsub('^"(.+)"$', "%1")
275 val = val:gsub("^'(.+)'$", "%1"):gsub('^"(.+)"$', "%1")
288 return { result = rv }
294 local fs = require "nixio.fs"
298 rv.firewall = fs.access("/sbin/fw3")
299 rv.opkg = fs.access("/bin/opkg")
300 rv.offloading = fs.access("/sys/module/xt_FLOWOFFLOAD/refcnt")
301 rv.br2684ctl = fs.access("/usr/sbin/br2684ctl")
302 rv.swconfig = fs.access("/sbin/swconfig")
303 rv.odhcpd = fs.access("/usr/sbin/odhcpd")
304 rv.zram = fs.access("/sys/class/zram-control")
305 rv.sysntpd = fs.readlink("/usr/sbin/ntpd") and true
306 rv.ipv6 = fs.access("/proc/net/ipv6_route")
307 rv.dropbear = fs.access("/usr/sbin/dropbear")
309 local wifi_features = { "eap", "11n", "11ac", "11r", "11w", "acs", "sae", "owe", "suiteb192" }
311 if fs.access("/usr/sbin/hostapd") then
312 rv.hostapd = { cli = fs.access("/usr/sbin/hostapd_cli") }
315 for _, feature in ipairs(wifi_features) do
316 rv.hostapd[feature] =
317 (os.execute(string.format("/usr/sbin/hostapd -v%s >/dev/null 2>/dev/null", feature)) == 0)
321 if fs.access("/usr/sbin/wpa_supplicant") then
322 rv.wpasupplicant = { cli = fs.access("/usr/sbin/wpa_cli") }
325 for _, feature in ipairs(wifi_features) do
326 rv.wpasupplicant[feature] =
327 (os.execute(string.format("/usr/sbin/wpa_supplicant -v%s >/dev/null 2>/dev/null", feature)) == 0)
331 ok, fd = pcall(io.popen, "dnsmasq --version 2>/dev/null")
336 local line = fd:read("*l")
341 local opts = line:match("^Compile time options: (.+)$")
344 for opt in opts:gmatch("%S+") do
345 local no = opt:match("^no%-(%S+)$")
346 rv.dnsmasq[string.lower(no or opt)] = not no
355 ok, fd = pcall(io.popen, "ipset --help 2>/dev/null")
362 local line = fd:read("*l")
365 elseif line:match("^Supported set types:") then
368 local set, ver = line:match("^%s+(%S+)%s+(%d+)")
369 if set and not rv.ipset[set] then
370 rv.ipset[set] = tonumber(ver)
382 getSwconfigFeatures = {
383 args = { switch = "switch0" },
384 call = function(args)
385 local util = require "luci.util"
387 -- Parse some common switch properties from swconfig help output.
388 local swc, err = io.popen("swconfig dev %s help 2>/dev/null" % util.shellquote(args.switch))
390 local is_port_attr = false
391 local is_vlan_attr = false
395 local line = swc:read("*l")
396 if not line then break end
398 if line:match("^%s+%-%-vlan") then
401 elseif line:match("^%s+%-%-port") then
405 elseif line:match("cpu @") then
406 rv.switch_title = line:match("^switch%d: %w+%((.-)%)")
407 rv.num_vlans = tonumber(line:match("vlans: (%d+)")) or 16
410 elseif line:match(": pvid") or line:match(": tag") or line:match(": vid") then
411 if is_vlan_attr then rv.vid_option = line:match(": (%w+)") end
413 elseif line:match(": enable_vlan4k") then
414 rv.vlan4k_option = "enable_vlan4k"
416 elseif line:match(": enable_vlan") then
417 rv.vlan_option = "enable_vlan"
419 elseif line:match(": enable_learning") then
420 rv.learning_option = "enable_learning"
422 elseif line:match(": enable_mirror_rx") then
423 rv.mirror_option = "enable_mirror_rx"
425 elseif line:match(": max_length") then
426 rv.jumbo_option = "max_length"
433 return { error = "No such switch" }
438 return { error = err }
443 getSwconfigPortState = {
444 args = { switch = "switch0" },
445 call = function(args)
446 local util = require "luci.util"
448 local swc, err = io.popen("swconfig dev %s show 2>/dev/null" % util.shellquote(args.switch))
453 local line = swc:read("*l")
454 if not line then break end
456 local port, up = line:match("port:(%d+) link:(%w+)")
458 local speed = line:match(" speed:(%d+)")
459 local duplex = line:match(" (%w+)-duplex")
460 local txflow = line:match(" (txflow)")
461 local rxflow = line:match(" (rxflow)")
462 local auto = line:match(" (auto)")
465 port = tonumber(port) or 0,
466 speed = tonumber(speed) or 0,
468 duplex = (duplex == "full"),
469 rxflow = (not not rxflow),
470 txflow = (not not txflow),
471 auto = (not not auto)
478 if not next(ports) then
479 return { error = "No such switch" }
482 return { result = ports }
484 return { error = err }
490 args = { username = "root", password = "password" },
491 call = function(args)
492 local util = require "luci.util"
494 result = (os.execute("(echo %s; sleep 1; echo %s) | passwd %s >/dev/null 2>&1" %{
495 luci.util.shellquote(args.password),
496 luci.util.shellquote(args.password),
497 luci.util.shellquote(args.username)
505 local fs = require "nixio.fs"
507 local block = io.popen("/sbin/block info", "r")
512 local ln = block:read("*l")
517 local dev = ln:match("^/dev/(.-):")
519 local s = tonumber((fs.readfile("/sys/class/block/" .. dev .."/size")))
521 dev = "/dev/" .. dev,
526 for key, val in ln:gmatch([[(%w+)="(.-)"]]) do
538 return { error = "Unable to execute block utility" }
545 return { result = (os.execute("/sbin/block detect > /etc/config/fstab") == 0) }
551 local fs = require "nixio.fs"
553 local fd, err = io.open("/proc/mounts", "r")
558 local ln = fd:read("*l")
563 local device, mount, fstype, options, freq, pass = ln:match("^(%S*) (%S*) (%S*) (%S*) (%d+) (%d+)$")
564 if device and mount then
565 device = device:gsub("\\(%d+)", function(n) return string.char(tonumber(n, 8)) end)
566 mount = mount:gsub("\\(%d+)", function(n) return string.char(tonumber(n, 8)) end)
568 local stat = fs.statvfs(mount)
569 if stat and stat.blocks > 0 then
573 size = stat.bsize * stat.blocks,
574 avail = stat.bsize * stat.bavail,
575 free = stat.bsize * stat.bfree
583 return { result = rv }
585 return { error = err }
591 args = { path = "/mnt" },
592 call = function(args)
593 local util = require "luci.util"
594 return { result = (os.execute(string.format("/bin/umount %s", util.shellquote(args.path))) == 0) }
600 return { result = (os.execute("/sbin/reboot >/dev/null 2>&1") == 0) }
605 local function parseInput()
606 local parse = json.new()
610 local chunk = io.read(4096)
613 elseif not done and not err then
614 done, err = parse:parse(chunk)
619 print(json.stringify({ error = err or "Incomplete input" }))
626 local function validateArgs(func, uargs)
627 local method = methods[func]
629 print(json.stringify({ error = "Method not found" }))
633 if type(uargs) ~= "table" then
634 print(json.stringify({ error = "Invalid arguments" }))
638 uargs.ubus_rpc_session = nil
641 local margs = method.args or {}
642 for k, v in pairs(uargs) do
643 if margs[k] == nil or
644 (v ~= nil and type(v) ~= type(margs[k]))
646 print(json.stringify({ error = "Invalid arguments" }))
654 if arg[1] == "list" then
655 local _, method, rv = nil, nil, {}
656 for _, method in pairs(methods) do rv[_] = method.args or {} end
657 print((json.stringify(rv):gsub(":%[%]", ":{}")))
658 elseif arg[1] == "call" then
659 local args = parseInput()
660 local method = validateArgs(arg[2], args)
661 local result, code = method.call(args)
662 print((json.stringify(result):gsub("^%[%]$", "{}")))