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")
172 local line = fd:read("*l")
177 if line:match("^%s*config%s") then
183 local opt, val = line:match("^%s*option%s+(%S+)%s+(%S.*)$")
185 opt = opt:gsub("^'(.+)'$", "%1"):gsub('^"(.+)"$', "%1")
186 val = val:gsub("^'(.+)'$", "%1"):gsub('^"(.+)"$', "%1")
199 return { result = rv }
205 local fs = require "nixio.fs"
209 rv.firewall = fs.access("/sbin/fw3")
210 rv.opkg = fs.access("/bin/opkg")
211 rv.offloading = fs.access("/sys/module/xt_FLOWOFFLOAD/refcnt")
212 rv.br2684ctl = fs.access("/usr/sbin/br2684ctl")
213 rv.swconfig = fs.access("/sbin/swconfig")
214 rv.odhcpd = fs.access("/usr/sbin/odhcpd")
215 rv.zram = fs.access("/sys/class/zram-control")
216 rv.sysntpd = fs.readlink("/usr/sbin/ntpd") and true
217 rv.ipv6 = fs.access("/proc/net/ipv6_route")
218 rv.dropbear = fs.access("/usr/sbin/dropbear")
219 rv.cabundle = fs.access("/etc/ssl/certs/ca-certificates.crt")
220 rv.relayd = fs.access("/usr/sbin/relayd")
222 local wifi_features = { "eap", "11n", "11ac", "11r", "11w", "acs", "sae", "owe", "suiteb192" }
224 if fs.access("/usr/sbin/hostapd") then
225 rv.hostapd = { cli = fs.access("/usr/sbin/hostapd_cli") }
228 for _, feature in ipairs(wifi_features) do
229 rv.hostapd[feature] =
230 (os.execute(string.format("/usr/sbin/hostapd -v%s >/dev/null 2>/dev/null", feature)) == 0)
234 if fs.access("/usr/sbin/wpa_supplicant") then
235 rv.wpasupplicant = { cli = fs.access("/usr/sbin/wpa_cli") }
238 for _, feature in ipairs(wifi_features) do
239 rv.wpasupplicant[feature] =
240 (os.execute(string.format("/usr/sbin/wpa_supplicant -v%s >/dev/null 2>/dev/null", feature)) == 0)
244 ok, fd = pcall(io.popen, "dnsmasq --version 2>/dev/null")
249 local line = fd:read("*l")
254 local opts = line:match("^Compile time options: (.+)$")
257 for opt in opts:gmatch("%S+") do
258 local no = opt:match("^no%-(%S+)$")
259 rv.dnsmasq[string.lower(no or opt)] = not no
268 ok, fd = pcall(io.popen, "ipset --help 2>/dev/null")
275 local line = fd:read("*l")
278 elseif line:match("^Supported set types:") then
281 local set, ver = line:match("^%s+(%S+)%s+(%d+)")
282 if set and not rv.ipset[set] then
283 rv.ipset[set] = tonumber(ver)
295 getSwconfigFeatures = {
296 args = { switch = "switch0" },
297 call = function(args)
298 local util = require "luci.util"
300 -- Parse some common switch properties from swconfig help output.
301 local swc, err = io.popen("swconfig dev %s help 2>/dev/null" % util.shellquote(args.switch))
303 local is_port_attr = false
304 local is_vlan_attr = false
308 local line = swc:read("*l")
309 if not line then break end
311 if line:match("^%s+%-%-vlan") then
314 elseif line:match("^%s+%-%-port") then
318 elseif line:match("cpu @") then
319 rv.switch_title = line:match("^switch%d: %w+%((.-)%)")
320 rv.num_vlans = tonumber(line:match("vlans: (%d+)")) or 16
323 elseif line:match(": pvid") or line:match(": tag") or line:match(": vid") then
324 if is_vlan_attr then rv.vid_option = line:match(": (%w+)") end
326 elseif line:match(": enable_vlan4k") then
327 rv.vlan4k_option = "enable_vlan4k"
329 elseif line:match(": enable_vlan") then
330 rv.vlan_option = "enable_vlan"
332 elseif line:match(": enable_learning") then
333 rv.learning_option = "enable_learning"
335 elseif line:match(": enable_mirror_rx") then
336 rv.mirror_option = "enable_mirror_rx"
338 elseif line:match(": max_length") then
339 rv.jumbo_option = "max_length"
346 return { error = "No such switch" }
351 return { error = err }
356 getSwconfigPortState = {
357 args = { switch = "switch0" },
358 call = function(args)
359 local util = require "luci.util"
361 local swc, err = io.popen("swconfig dev %s show 2>/dev/null" % util.shellquote(args.switch))
366 local line = swc:read("*l")
367 if not line or (line:match("^VLAN %d+:") and #ports > 0) then
371 local pnum = line:match("^Port (%d+):")
374 port = tonumber(pnum),
383 ports[#ports+1] = port
389 if line:match("full[%- ]duplex") then
393 m = line:match(" speed:(%d+)")
395 port.speed = tonumber(m)
398 m = line:match("(%d+) Mbps")
399 if m and port.speed == 0 then
400 port.speed = tonumber(m)
403 m = line:match("link: (%d+)")
404 if m and port.speed == 0 then
405 port.speed = tonumber(m)
408 if line:match("link: ?up") or line:match("status: ?up") then
412 if line:match("auto%-negotiate") or line:match("link:.-auto") then
416 if line:match("link:.-rxflow") then
420 if line:match("link:.-txflow") then
428 if not next(ports) then
429 return { error = "No such switch" }
432 return { result = ports }
434 return { error = err }
440 args = { username = "root", password = "password" },
441 call = function(args)
442 local util = require "luci.util"
444 result = (os.execute("(echo %s; sleep 1; echo %s) | passwd %s >/dev/null 2>&1" %{
445 luci.util.shellquote(args.password),
446 luci.util.shellquote(args.password),
447 luci.util.shellquote(args.username)
455 local fs = require "nixio.fs"
457 local block = io.popen("/sbin/block info", "r")
462 local ln = block:read("*l")
467 local dev = ln:match("^/dev/(.-):")
469 local s = tonumber((fs.readfile("/sys/class/block/" .. dev .."/size")))
471 dev = "/dev/" .. dev,
476 for key, val in ln:gmatch([[(%w+)="(.-)"]]) do
488 return { error = "Unable to execute block utility" }
495 return { result = (os.execute("/sbin/block detect > /etc/config/fstab") == 0) }
501 local fs = require "nixio.fs"
503 local fd, err = io.open("/proc/mounts", "r")
508 local ln = fd:read("*l")
513 local device, mount, fstype, options, freq, pass = ln:match("^(%S*) (%S*) (%S*) (%S*) (%d+) (%d+)$")
514 if device and mount then
515 device = device:gsub("\\(%d+)", function(n) return string.char(tonumber(n, 8)) end)
516 mount = mount:gsub("\\(%d+)", function(n) return string.char(tonumber(n, 8)) end)
518 local stat = fs.statvfs(mount)
519 if stat and stat.blocks > 0 then
523 size = stat.bsize * stat.blocks,
524 avail = stat.bsize * stat.bavail,
525 free = stat.bsize * stat.bfree
533 return { result = rv }
535 return { error = err }
541 args = { mode = "interface", device = "eth0" },
542 call = function(args)
543 local util = require "luci.util"
546 if args.mode == "interface" then
547 flags = "-i %s" % util.shellquote(args.device)
548 elseif args.mode == "wireless" then
549 flags = "-r %s" % util.shellquote(args.device)
550 elseif args.mode == "conntrack" then
552 elseif args.mode == "load" then
555 return { error = "Invalid mode" }
558 local fd, err = io.popen("luci-bwc %s" % flags, "r")
560 local parse = json.new()
566 local ln = fd:read("*l")
571 done, err = parse:parse((ln:gsub("%d+", "%1.0")))
574 err = "Unexpected JSON data"
584 done, err = parse:parse("]")
587 return { error = err }
589 return { error = "Incomplete JSON data" }
591 return { result = parse:get() }
594 return { error = err }
601 local sys = require "luci.sys"
602 return { result = sys.net.conntrack() }
608 local sys = require "luci.sys"
610 for _, v in pairs(sys.process.list()) do
613 return { result = res }
618 local function parseInput()
619 local parse = json.new()
623 local chunk = io.read(4096)
626 elseif not done and not err then
627 done, err = parse:parse(chunk)
632 print(json.stringify({ error = err or "Incomplete input" }))
639 local function validateArgs(func, uargs)
640 local method = methods[func]
642 print(json.stringify({ error = "Method not found" }))
646 if type(uargs) ~= "table" then
647 print(json.stringify({ error = "Invalid arguments" }))
651 uargs.ubus_rpc_session = nil
654 local margs = method.args or {}
655 for k, v in pairs(uargs) do
656 if margs[k] == nil or
657 (v ~= nil and type(v) ~= type(margs[k]))
659 print(json.stringify({ error = "Invalid arguments" }))
667 if arg[1] == "list" then
668 local _, method, rv = nil, nil, {}
669 for _, method in pairs(methods) do rv[_] = method.args or {} end
670 print((json.stringify(rv):gsub(":%[%]", ":{}")))
671 elseif arg[1] == "call" then
672 local args = parseInput()
673 local method = validateArgs(arg[2], args)
674 local result, code = method.call(args)
675 print((json.stringify(result):gsub("^%[%]$", "{}")))