1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local io = require "io"
5 local os = require "os"
6 local table = require "table"
7 local nixio = require "nixio"
8 local fs = require "nixio.fs"
9 local uci = require "luci.model.uci"
12 luci.util = require "luci.util"
13 luci.ip = require "luci.ip"
15 local tonumber, ipairs, pairs, pcall, type, next, setmetatable, require, select =
16 tonumber, ipairs, pairs, pcall, type, next, setmetatable, require, select
22 return os.execute(...) / 256
29 local k = {"fs", "blocks", "used", "available", "percent", "mountpoint"}
30 local ps = luci.util.execi("df")
42 for value in line:gmatch("[^%s]+") do
49 -- this is a rather ugly workaround to cope with wrapped lines in
52 -- /dev/scsi/host0/bus0/target0/lun0/part3
53 -- 114382024 93566472 15005244 86% /mnt/usb
59 for value in line:gmatch("[^%s]+") do
65 table.insert(data, row)
72 -- containing the whole environment is returned otherwise this function returns
73 -- the corresponding string value for the given name or nil if no such variable
77 function hostname(newname)
78 if type(newname) == "string" and #newname > 0 then
79 fs.writefile( "/proc/sys/kernel/hostname", newname )
82 return nixio.uname().nodename
86 function httpget(url, stream, target)
88 local source = stream and io.popen or luci.util.exec
89 return source("wget -qO- '"..url:gsub("'", "").."'")
91 return os.execute("wget -qO '%s' '%s'" %
92 {target:gsub("'", ""), url:gsub("'", "")})
97 return os.execute("reboot >/dev/null 2>&1")
101 return luci.util.exec("logread")
105 return luci.util.exec("dmesg")
108 function uniqueid(bytes)
109 local rand = fs.readfile("/dev/urandom", bytes)
110 return rand and nixio.bin.hexlify(rand)
114 return nixio.sysinfo().uptime
120 -- The following fields are defined for arp entry objects:
121 -- { "IP address", "HW address", "HW type", "Flags", "Mask", "Device" }
122 function net.arptable(callback)
123 local arp = (not callback) and {} or nil
125 if fs.access("/proc/net/arp") then
126 for e in io.lines("/proc/net/arp") do
128 for v in e:gmatch("%S+") do
134 ["IP address"] = r[1],
137 ["HW address"] = r[4],
154 local function _nethints(what, callback)
155 local _, k, e, mac, ip, name
156 local cur = uci.cursor()
160 local function _add(i, ...)
161 local k = select(i, ...)
163 if not hosts[k] then hosts[k] = { } end
164 hosts[k][1] = select(1, ...) or hosts[k][1]
165 hosts[k][2] = select(2, ...) or hosts[k][2]
166 hosts[k][3] = select(3, ...) or hosts[k][3]
167 hosts[k][4] = select(4, ...) or hosts[k][4]
171 if fs.access("/proc/net/arp") then
172 for e in io.lines("/proc/net/arp") do
173 ip, mac = e:match("^([%d%.]+)%s+%S+%s+%S+%s+([a-fA-F0-9:]+)%s+")
175 _add(what, mac:upper(), ip, nil, nil)
180 if fs.access("/etc/ethers") then
181 for e in io.lines("/etc/ethers") do
182 mac, ip = e:match("^([a-f0-9]%S+) (%S+)")
184 _add(what, mac:upper(), ip, nil, nil)
189 if fs.access("/var/dhcp.leases") then
190 for e in io.lines("/var/dhcp.leases") do
191 mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
193 _add(what, mac:upper(), ip, nil, name ~= "*" and name)
198 cur:foreach("dhcp", "host",
200 for mac in luci.util.imatch(s.mac) do
201 _add(what, mac:upper(), s.ip, nil, s.name)
205 for _, e in ipairs(nixio.getifaddrs()) do
206 if e.name ~= "lo" then
207 ifn[e.name] = ifn[e.name] or { }
208 if e.family == "packet" and e.addr and #e.addr == 17 then
209 ifn[e.name][1] = e.addr:upper()
210 elseif e.family == "inet" then
211 ifn[e.name][2] = e.addr
212 elseif e.family == "inet6" then
213 ifn[e.name][3] = e.addr
218 for _, e in pairs(ifn) do
219 if e[what] and (e[2] or e[3]) then
220 _add(what, e[1], e[2], e[3], e[4])
224 for _, e in luci.util.kspairs(hosts) do
225 callback(e[1], e[2], e[3], e[4])
229 -- Each entry contains the values in the following order:
231 function net.mac_hints(callback)
233 _nethints(1, function(mac, v4, v6, name)
234 name = name or nixio.getnameinfo(v4 or v6, nil, 100) or v4
235 if name and name ~= mac then
236 callback(mac, name or nixio.getnameinfo(v4 or v6, nil, 100) or v4)
241 _nethints(1, function(mac, v4, v6, name)
242 name = name or nixio.getnameinfo(v4 or v6, nil, 100) or v4
243 if name and name ~= mac then
244 rv[#rv+1] = { mac, name or nixio.getnameinfo(v4 or v6, nil, 100) or v4 }
251 -- Each entry contains the values in the following order:
253 function net.ipv4_hints(callback)
255 _nethints(2, function(mac, v4, v6, name)
256 name = name or nixio.getnameinfo(v4, nil, 100) or mac
257 if name and name ~= v4 then
263 _nethints(2, function(mac, v4, v6, name)
264 name = name or nixio.getnameinfo(v4, nil, 100) or mac
265 if name and name ~= v4 then
266 rv[#rv+1] = { v4, name }
273 -- Each entry contains the values in the following order:
275 function net.ipv6_hints(callback)
277 _nethints(3, function(mac, v4, v6, name)
278 name = name or nixio.getnameinfo(v6, nil, 100) or mac
279 if name and name ~= v6 then
285 _nethints(3, function(mac, v4, v6, name)
286 name = name or nixio.getnameinfo(v6, nil, 100) or mac
287 if name and name ~= v6 then
288 rv[#rv+1] = { v6, name }
295 function net.conntrack(callback)
296 local ok, nfct = pcall(io.lines, "/proc/net/nf_conntrack")
297 if not ok or not nfct then
301 local line, connt = nil, (not callback) and { }
303 local fam, l3, l4, timeout, state, tuples =
304 line:match("^(ipv[46]) +(%d+) +%S+ +(%d+) +(%d+) +([A-Z_]+) +(.+)$")
306 if fam and l3 and l4 and timeout and state and tuples and
309 l4 = nixio.getprotobynumber(l4)
315 layer4 = l4 and l4.name or "unknown",
316 timeout = tonumber(timeout, 10)
320 for key, val in tuples:gmatch("(%w+)=(%S+)") do
321 if key == "bytes" or key == "packets" then
322 entry[key] = entry[key] + tonumber(val, 10)
323 elseif key == "src" or key == "dst" or key == "sport" or key == "dport" then
324 if entry[key] == nil then
335 connt[#connt+1] = entry
340 return callback and true or connt
343 function net.devices()
345 for k, v in ipairs(nixio.getifaddrs()) do
346 if v.family == "packet" then
347 devs[#devs+1] = v.name
354 function net.deviceinfo()
356 for k, v in ipairs(nixio.getifaddrs()) do
357 if v.family == "packet" then
382 -- The following fields are defined for route entry tables:
383 -- { "dest", "gateway", "metric", "refcount", "usecount", "irtt",
384 -- "flags", "device" }
385 function net.routes(callback)
388 for line in io.lines("/proc/net/route") do
390 local dev, dst_ip, gateway, flags, refcnt, usecnt, metric,
391 dst_mask, mtu, win, irtt = line:match(
392 "([^%s]+)\t([A-F0-9]+)\t([A-F0-9]+)\t([A-F0-9]+)\t" ..
393 "(%d+)\t(%d+)\t(%d+)\t([A-F0-9]+)\t(%d+)\t(%d+)\t(%d+)"
397 gateway = luci.ip.Hex( gateway, 32, luci.ip.FAMILY_INET4 )
398 dst_mask = luci.ip.Hex( dst_mask, 32, luci.ip.FAMILY_INET4 )
399 dst_ip = luci.ip.Hex(
400 dst_ip, dst_mask:prefix(dst_mask), luci.ip.FAMILY_INET4
406 metric = tonumber(metric),
407 refcount = tonumber(refcnt),
408 usecount = tonumber(usecnt),
410 window = tonumber(window),
411 irtt = tonumber(irtt),
412 flags = tonumber(flags, 16),
419 routes[#routes+1] = rt
427 -- The following fields are defined for route entry tables:
428 -- { "source", "dest", "nexthop", "metric", "refcount", "usecount",
429 -- "flags", "device" }
430 function net.routes6(callback)
431 if fs.access("/proc/net/ipv6_route", "r") then
434 for line in io.lines("/proc/net/ipv6_route") do
436 local dst_ip, dst_prefix, src_ip, src_prefix, nexthop,
437 metric, refcnt, usecnt, flags, dev = line:match(
438 "([a-f0-9]+) ([a-f0-9]+) " ..
439 "([a-f0-9]+) ([a-f0-9]+) " ..
440 "([a-f0-9]+) ([a-f0-9]+) " ..
441 "([a-f0-9]+) ([a-f0-9]+) " ..
442 "([a-f0-9]+) +([^%s]+)"
445 if dst_ip and dst_prefix and
446 src_ip and src_prefix and
447 nexthop and metric and
448 refcnt and usecnt and
451 src_ip = luci.ip.Hex(
452 src_ip, tonumber(src_prefix, 16), luci.ip.FAMILY_INET6, false
455 dst_ip = luci.ip.Hex(
456 dst_ip, tonumber(dst_prefix, 16), luci.ip.FAMILY_INET6, false
459 nexthop = luci.ip.Hex( nexthop, 128, luci.ip.FAMILY_INET6, false )
465 metric = tonumber(metric, 16),
466 refcount = tonumber(refcnt, 16),
467 usecount = tonumber(usecnt, 16),
468 flags = tonumber(flags, 16),
471 -- lua number is too small for storing the metric
472 -- add a metric_raw field with the original content
479 routes[#routes+1] = rt
488 function net.pingtest(host)
489 return os.execute("ping -c1 '"..host:gsub("'", '').."' >/dev/null 2>&1")
495 function process.info(key)
496 local s = {uid = nixio.getuid(), gid = nixio.getgid()}
497 return not key and s or s[key]
500 function process.list()
503 local ps = luci.util.execi("/bin/busybox top -bn1")
510 local pid, ppid, user, stat, vsz, mem, cpu, cmd = line:match(
511 "^ *(%d+) +(%d+) +(%S.-%S) +([RSDZTW][W ][<N ]) +(%d+) +(%d+%%) +(%d+%%) +(.+)"
514 local idx = tonumber(pid)
532 function process.setgroup(gid)
533 return nixio.setgid(gid)
536 function process.setuser(uid)
537 return nixio.setuid(uid)
540 process.signal = nixio.kill
545 -- { "uid", "gid", "name", "passwd", "dir", "shell", "gecos" }
546 user.getuser = nixio.getpw
548 function user.getpasswd(username)
549 local pwe = nixio.getsp and nixio.getsp(username) or nixio.getpw(username)
550 local pwh = pwe and (pwe.pwdp or pwe.passwd)
551 if not pwh or #pwh < 1 or pwh == "!" or pwh == "x" then
558 function user.checkpasswd(username, pass)
559 local pwh, pwe = user.getpasswd(username)
561 return (pwh == nil or nixio.crypt(pass, pwh) == pwh)
566 function user.setpasswd(username, password)
568 password = password:gsub("'", [['"'"']])
572 username = username:gsub("'", [['"'"']])
576 "(echo '" .. password .. "'; sleep 1; echo '" .. password .. "') | " ..
577 "passwd '" .. username .. "' >/dev/null 2>&1"
584 function wifi.getiwinfo(ifname)
585 local stat, iwinfo = pcall(require, "iwinfo")
588 local d, n = ifname:match("^(%w+)%.network(%d+)")
589 local wstate = luci.util.ubus("network.wireless", "status") or { }
592 n = n and tonumber(n) or 1
594 if type(wstate[d]) == "table" and
595 type(wstate[d].interfaces) == "table" and
596 type(wstate[d].interfaces[n]) == "table" and
597 type(wstate[d].interfaces[n].ifname) == "string"
599 ifname = wstate[d].interfaces[n].ifname
604 local t = stat and iwinfo.type(ifname)
605 local x = t and iwinfo[t] or { }
606 return setmetatable({}, {
607 __index = function(t, k)
608 if k == "ifname" then
620 init.dir = "/etc/init.d/"
622 function init.names()
624 for name in fs.glob(init.dir.."*") do
625 names[#names+1] = fs.basename(name)
630 function init.index(name)
631 if fs.access(init.dir..name) then
632 return call("env -i sh -c 'source %s%s enabled; exit ${START:-255}' >/dev/null"
637 local function init_action(action, name)
638 if fs.access(init.dir..name) then
639 return call("env -i %s%s %s >/dev/null" %{ init.dir, name, action })
643 function init.enabled(name)
644 return (init_action("enabled", name) == 0)
647 function init.enable(name)
648 return (init_action("enable", name) == 1)
651 function init.disable(name)
652 return (init_action("disable", name) == 0)
655 function init.start(name)
656 return (init_action("start", name) == 0)
659 function init.stop(name)
660 return (init_action("stop", name) == 0)