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 luci.ip.neighbors(nil, function(neigh)
172 if neigh.mac and neigh.family == 4 then
173 _add(what, neigh.mac:upper(), neigh.dest:string(), nil, nil)
174 elseif neigh.mac and neigh.family == 6 then
175 _add(what, neigh.mac:upper(), nil, neigh.dest:string(), nil)
179 if fs.access("/etc/ethers") then
180 for e in io.lines("/etc/ethers") do
181 mac, ip = e:match("^([a-f0-9]%S+) (%S+)")
183 _add(what, mac:upper(), ip, nil, nil)
188 cur:foreach("dhcp", "dnsmasq",
190 if s.leasefile and fs.access(s.leasefile) then
191 for e in io.lines(s.leasefile) do
192 mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
194 _add(what, mac:upper(), ip, nil, name ~= "*" and name)
201 cur:foreach("dhcp", "host",
203 for mac in luci.util.imatch(s.mac) do
204 _add(what, mac:upper(), s.ip, nil, s.name)
208 for _, e in ipairs(nixio.getifaddrs()) do
209 if e.name ~= "lo" then
210 ifn[e.name] = ifn[e.name] or { }
211 if e.family == "packet" and e.addr and #e.addr == 17 then
212 ifn[e.name][1] = e.addr:upper()
213 elseif e.family == "inet" then
214 ifn[e.name][2] = e.addr
215 elseif e.family == "inet6" then
216 ifn[e.name][3] = e.addr
221 for _, e in pairs(ifn) do
222 if e[what] and (e[2] or e[3]) then
223 _add(what, e[1], e[2], e[3], e[4])
227 for _, e in luci.util.kspairs(hosts) do
228 callback(e[1], e[2], e[3], e[4])
232 -- Each entry contains the values in the following order:
234 function net.mac_hints(callback)
236 _nethints(1, function(mac, v4, v6, name)
237 name = name or nixio.getnameinfo(v4 or v6, nil, 100) or v4
238 if name and name ~= mac then
239 callback(mac, name or nixio.getnameinfo(v4 or v6, nil, 100) or v4)
244 _nethints(1, function(mac, v4, v6, name)
245 name = name or nixio.getnameinfo(v4 or v6, nil, 100) or v4
246 if name and name ~= mac then
247 rv[#rv+1] = { mac, name or nixio.getnameinfo(v4 or v6, nil, 100) or v4 }
254 -- Each entry contains the values in the following order:
256 function net.ipv4_hints(callback)
258 _nethints(2, function(mac, v4, v6, name)
259 name = name or nixio.getnameinfo(v4, nil, 100) or mac
260 if name and name ~= v4 then
266 _nethints(2, function(mac, v4, v6, name)
267 name = name or nixio.getnameinfo(v4, nil, 100) or mac
268 if name and name ~= v4 then
269 rv[#rv+1] = { v4, name }
276 -- Each entry contains the values in the following order:
278 function net.ipv6_hints(callback)
280 _nethints(3, function(mac, v4, v6, name)
281 name = name or nixio.getnameinfo(v6, nil, 100) or mac
282 if name and name ~= v6 then
288 _nethints(3, function(mac, v4, v6, name)
289 name = name or nixio.getnameinfo(v6, nil, 100) or mac
290 if name and name ~= v6 then
291 rv[#rv+1] = { v6, name }
298 function net.host_hints(callback)
300 _nethints(1, function(mac, v4, v6, name)
301 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
302 callback(mac, v4, v6, name)
307 _nethints(1, function(mac, v4, v6, name)
308 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
310 if v4 then e.ipv4 = v4 end
311 if v6 then e.ipv6 = v6 end
312 if name then e.name = name end
320 function net.conntrack(callback)
321 local ok, nfct = pcall(io.lines, "/proc/net/nf_conntrack")
322 if not ok or not nfct then
326 local line, connt = nil, (not callback) and { }
328 local fam, l3, l4, timeout, tuples =
329 line:match("^(ipv[46]) +(%d+) +%S+ +(%d+) +(%d+) +(.+)$")
331 if fam and l3 and l4 and timeout and not tuples:match("^TIME_WAIT ") then
332 l4 = nixio.getprotobynumber(l4)
338 layer4 = l4 and l4.name or "unknown",
339 timeout = tonumber(timeout, 10)
343 for key, val in tuples:gmatch("(%w+)=(%S+)") do
344 if key == "bytes" or key == "packets" then
345 entry[key] = entry[key] + tonumber(val, 10)
346 elseif key == "src" or key == "dst" then
347 if entry[key] == nil then
348 entry[key] = luci.ip.new(val):string()
350 elseif key == "sport" or key == "dport" then
351 if entry[key] == nil then
362 connt[#connt+1] = entry
367 return callback and true or connt
370 function net.devices()
372 for k, v in ipairs(nixio.getifaddrs()) do
373 if v.family == "packet" then
374 devs[#devs+1] = v.name
381 function net.deviceinfo()
383 for k, v in ipairs(nixio.getifaddrs()) do
384 if v.family == "packet" then
409 -- The following fields are defined for route entry tables:
410 -- { "dest", "gateway", "metric", "refcount", "usecount", "irtt",
411 -- "flags", "device" }
412 function net.routes(callback)
415 for line in io.lines("/proc/net/route") do
417 local dev, dst_ip, gateway, flags, refcnt, usecnt, metric,
418 dst_mask, mtu, win, irtt = line:match(
419 "([^%s]+)\t([A-F0-9]+)\t([A-F0-9]+)\t([A-F0-9]+)\t" ..
420 "(%d+)\t(%d+)\t(%d+)\t([A-F0-9]+)\t(%d+)\t(%d+)\t(%d+)"
424 gateway = luci.ip.Hex( gateway, 32, luci.ip.FAMILY_INET4 )
425 dst_mask = luci.ip.Hex( dst_mask, 32, luci.ip.FAMILY_INET4 )
426 dst_ip = luci.ip.Hex(
427 dst_ip, dst_mask:prefix(dst_mask), luci.ip.FAMILY_INET4
433 metric = tonumber(metric),
434 refcount = tonumber(refcnt),
435 usecount = tonumber(usecnt),
437 window = tonumber(window),
438 irtt = tonumber(irtt),
439 flags = tonumber(flags, 16),
446 routes[#routes+1] = rt
454 -- The following fields are defined for route entry tables:
455 -- { "source", "dest", "nexthop", "metric", "refcount", "usecount",
456 -- "flags", "device" }
457 function net.routes6(callback)
458 if fs.access("/proc/net/ipv6_route", "r") then
461 for line in io.lines("/proc/net/ipv6_route") do
463 local dst_ip, dst_prefix, src_ip, src_prefix, nexthop,
464 metric, refcnt, usecnt, flags, dev = line:match(
465 "([a-f0-9]+) ([a-f0-9]+) " ..
466 "([a-f0-9]+) ([a-f0-9]+) " ..
467 "([a-f0-9]+) ([a-f0-9]+) " ..
468 "([a-f0-9]+) ([a-f0-9]+) " ..
469 "([a-f0-9]+) +([^%s]+)"
472 if dst_ip and dst_prefix and
473 src_ip and src_prefix and
474 nexthop and metric and
475 refcnt and usecnt and
478 src_ip = luci.ip.Hex(
479 src_ip, tonumber(src_prefix, 16), luci.ip.FAMILY_INET6, false
482 dst_ip = luci.ip.Hex(
483 dst_ip, tonumber(dst_prefix, 16), luci.ip.FAMILY_INET6, false
486 nexthop = luci.ip.Hex( nexthop, 128, luci.ip.FAMILY_INET6, false )
492 metric = tonumber(metric, 16),
493 refcount = tonumber(refcnt, 16),
494 usecount = tonumber(usecnt, 16),
495 flags = tonumber(flags, 16),
498 -- lua number is too small for storing the metric
499 -- add a metric_raw field with the original content
506 routes[#routes+1] = rt
515 function net.pingtest(host)
516 return os.execute("ping -c1 '"..host:gsub("'", '').."' >/dev/null 2>&1")
522 function process.info(key)
523 local s = {uid = nixio.getuid(), gid = nixio.getgid()}
524 return not key and s or s[key]
527 function process.list()
530 local ps = luci.util.execi("/bin/busybox top -bn1")
537 local pid, ppid, user, stat, vsz, mem, cpu, cmd = line:match(
538 "^ *(%d+) +(%d+) +(%S.-%S) +([RSDZTW][W ][<N ]) +(%d+) +(%d+%%) +(%d+%%) +(.+)"
541 local idx = tonumber(pid)
559 function process.setgroup(gid)
560 return nixio.setgid(gid)
563 function process.setuser(uid)
564 return nixio.setuid(uid)
567 process.signal = nixio.kill
572 -- { "uid", "gid", "name", "passwd", "dir", "shell", "gecos" }
573 user.getuser = nixio.getpw
575 function user.getpasswd(username)
576 local pwe = nixio.getsp and nixio.getsp(username) or nixio.getpw(username)
577 local pwh = pwe and (pwe.pwdp or pwe.passwd)
578 if not pwh or #pwh < 1 or pwh == "!" or pwh == "x" then
585 function user.checkpasswd(username, pass)
586 local pwh, pwe = user.getpasswd(username)
588 return (pwh == nil or nixio.crypt(pass, pwh) == pwh)
593 function user.setpasswd(username, password)
595 password = password:gsub("'", [['"'"']])
599 username = username:gsub("'", [['"'"']])
603 "(echo '" .. password .. "'; sleep 1; echo '" .. password .. "') | " ..
604 "passwd '" .. username .. "' >/dev/null 2>&1"
611 function wifi.getiwinfo(ifname)
612 local stat, iwinfo = pcall(require, "iwinfo")
615 local d, n = ifname:match("^(%w+)%.network(%d+)")
616 local wstate = luci.util.ubus("network.wireless", "status") or { }
619 n = n and tonumber(n) or 1
621 if type(wstate[d]) == "table" and
622 type(wstate[d].interfaces) == "table" and
623 type(wstate[d].interfaces[n]) == "table" and
624 type(wstate[d].interfaces[n].ifname) == "string"
626 ifname = wstate[d].interfaces[n].ifname
631 local t = stat and iwinfo.type(ifname)
632 local x = t and iwinfo[t] or { }
633 return setmetatable({}, {
634 __index = function(t, k)
635 if k == "ifname" then
647 init.dir = "/etc/init.d/"
649 function init.names()
651 for name in fs.glob(init.dir.."*") do
652 names[#names+1] = fs.basename(name)
657 function init.index(name)
658 if fs.access(init.dir..name) then
659 return call("env -i sh -c 'source %s%s enabled; exit ${START:-255}' >/dev/null"
664 local function init_action(action, name)
665 if fs.access(init.dir..name) then
666 return call("env -i %s%s %s >/dev/null" %{ init.dir, name, action })
670 function init.enabled(name)
671 return (init_action("enabled", name) == 0)
674 function init.enable(name)
675 return (init_action("enable", name) == 1)
678 function init.disable(name)
679 return (init_action("disable", name) == 0)
682 function init.start(name)
683 return (init_action("start", name) == 0)
686 function init.stop(name)
687 return (init_action("stop", name) == 0)