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, unpack =
16 tonumber, ipairs, pairs, pcall, type, next, setmetatable, require, select, unpack
22 return os.execute(...) / 256
27 -- containing the whole environment is returned otherwise this function returns
28 -- the corresponding string value for the given name or nil if no such variable
32 function hostname(newname)
33 if type(newname) == "string" and #newname > 0 then
34 fs.writefile( "/proc/sys/kernel/hostname", newname )
37 return nixio.uname().nodename
41 function httpget(url, stream, target)
43 local source = stream and io.popen or luci.util.exec
44 return source("wget -qO- %s" % luci.util.shellquote(url))
46 return os.execute("wget -qO %s %s" %
47 {luci.util.shellquote(target), luci.util.shellquote(url)})
52 return os.execute("reboot >/dev/null 2>&1")
56 return luci.util.exec("logread")
60 return luci.util.exec("dmesg")
63 function uniqueid(bytes)
64 local rand = fs.readfile("/dev/urandom", bytes)
65 return rand and nixio.bin.hexlify(rand)
69 return nixio.sysinfo().uptime
75 local function _nethints(what, callback)
76 local _, k, e, mac, ip, name, duid, iaid
77 local cur = uci.cursor()
82 local function _add(i, ...)
83 local k = select(i, ...)
85 if not hosts[k] then hosts[k] = { } end
86 hosts[k][1] = select(1, ...) or hosts[k][1]
87 hosts[k][2] = select(2, ...) or hosts[k][2]
88 hosts[k][3] = select(3, ...) or hosts[k][3]
89 hosts[k][4] = select(4, ...) or hosts[k][4]
93 luci.ip.neighbors(nil, function(neigh)
94 if neigh.mac and neigh.family == 4 then
95 _add(what, neigh.mac:string(), neigh.dest:string(), nil, nil)
96 elseif neigh.mac and neigh.family == 6 then
97 _add(what, neigh.mac:string(), nil, neigh.dest:string(), nil)
101 if fs.access("/etc/ethers") then
102 for e in io.lines("/etc/ethers") do
103 mac, name = e:match("^([a-fA-F0-9:-]+)%s+(%S+)")
104 mac = luci.ip.checkmac(mac)
106 if luci.ip.checkip4(name) then
107 _add(what, mac, name, nil, nil)
109 _add(what, mac, nil, nil, name)
115 cur:foreach("dhcp", "dnsmasq",
117 if s.leasefile and fs.access(s.leasefile) then
118 for e in io.lines(s.leasefile) do
119 mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
120 mac = luci.ip.checkmac(mac)
122 _add(what, mac, ip, nil, name ~= "*" and name)
129 cur:foreach("dhcp", "odhcpd",
131 if type(s.leasefile) == "string" and fs.access(s.leasefile) then
132 for e in io.lines(s.leasefile) do
133 duid, iaid, name, _, ip = e:match("^# %S+ (%S+) (%S+) (%S+) (-?%d+) %S+ %S+ ([0-9a-f:.]+)/[0-9]+")
134 mac = net.duid_to_mac(duid)
136 if ip and iaid == "ipv4" then
137 _add(what, mac, ip, nil, name ~= "*" and name)
139 _add(what, mac, nil, ip, name ~= "*" and name)
147 cur:foreach("dhcp", "host",
149 for mac in luci.util.imatch(s.mac) do
150 mac = luci.ip.checkmac(mac)
152 _add(what, mac, s.ip, nil, s.name)
157 for _, e in ipairs(nixio.getifaddrs()) do
158 if e.name ~= "lo" then
159 ifn[e.name] = ifn[e.name] or { }
160 if e.family == "packet" and e.addr and #e.addr == 17 then
161 ifn[e.name][1] = e.addr:upper()
162 elseif e.family == "inet" then
163 ifn[e.name][2] = e.addr
164 elseif e.family == "inet6" then
165 ifn[e.name][3] = e.addr
170 for _, e in pairs(ifn) do
171 if e[what] and (e[2] or e[3]) then
172 _add(what, e[1], e[2], e[3], e[4])
176 for _, e in pairs(hosts) do
177 lookup[#lookup+1] = (what > 1) and e[what] or (e[2] or e[3])
181 lookup = luci.util.ubus("network.rrdns", "lookup", {
188 for _, e in luci.util.kspairs(hosts) do
189 callback(e[1], e[2], e[3], lookup[e[2]] or lookup[e[3]] or e[4])
193 -- Each entry contains the values in the following order:
195 function net.mac_hints(callback)
197 _nethints(1, function(mac, v4, v6, name)
199 if name and name ~= mac then
200 callback(mac, name or v4)
205 _nethints(1, function(mac, v4, v6, name)
207 if name and name ~= mac then
208 rv[#rv+1] = { mac, name or v4 }
215 -- Each entry contains the values in the following order:
217 function net.ipv4_hints(callback)
219 _nethints(2, function(mac, v4, v6, name)
221 if name and name ~= v4 then
227 _nethints(2, function(mac, v4, v6, name)
229 if name and name ~= v4 then
230 rv[#rv+1] = { v4, name }
237 -- Each entry contains the values in the following order:
239 function net.ipv6_hints(callback)
241 _nethints(3, function(mac, v4, v6, name)
243 if name and name ~= v6 then
249 _nethints(3, function(mac, v4, v6, name)
251 if name and name ~= v6 then
252 rv[#rv+1] = { v6, name }
259 function net.host_hints(callback)
261 _nethints(1, function(mac, v4, v6, name)
262 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
263 callback(mac, v4, v6, name)
268 _nethints(1, function(mac, v4, v6, name)
269 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
271 if v4 then e.ipv4 = v4 end
272 if v6 then e.ipv6 = v6 end
273 if name then e.name = name end
281 function net.conntrack(callback)
282 local ok, nfct = pcall(io.lines, "/proc/net/nf_conntrack")
283 if not ok or not nfct then
287 local line, connt = nil, (not callback) and { }
289 local fam, l3, l4, timeout, tuples =
290 line:match("^(ipv[46]) +(%d+) +%S+ +(%d+) +(%d+) +(.+)$")
292 if fam and l3 and l4 and timeout and not tuples:match("^TIME_WAIT ") then
293 l4 = nixio.getprotobynumber(l4)
299 layer4 = l4 and l4.name or "unknown",
300 timeout = tonumber(timeout, 10)
304 for key, val in tuples:gmatch("(%w+)=(%S+)") do
305 if key == "bytes" or key == "packets" then
306 entry[key] = entry[key] + tonumber(val, 10)
307 elseif key == "src" or key == "dst" then
308 if entry[key] == nil then
309 entry[key] = luci.ip.new(val):string()
311 elseif key == "sport" or key == "dport" then
312 if entry[key] == nil then
323 connt[#connt+1] = entry
328 return callback and true or connt
331 function net.devices()
334 for k, v in ipairs(nixio.getifaddrs()) do
335 if v.name and not seen[v.name] then
337 devs[#devs+1] = v.name
343 function net.duid_to_mac(duid)
344 local b1, b2, b3, b4, b5, b6
346 if type(duid) == "string" then
347 -- DUID-LLT / Ethernet
349 b1, b2, b3, b4, b5, b6 = duid:match("^00010001(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)%x%x%x%x%x%x%x%x$")
351 -- DUID-LL / Ethernet
352 elseif #duid == 20 then
353 b1, b2, b3, b4, b5, b6 = duid:match("^00030001(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$")
355 -- DUID-LL / Ethernet (Without Header)
356 elseif #duid == 12 then
357 b1, b2, b3, b4, b5, b6 = duid:match("^(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$")
361 return b1 and luci.ip.checkmac(table.concat({ b1, b2, b3, b4, b5, b6 }, ":"))
366 function process.info(key)
367 local s = {uid = nixio.getuid(), gid = nixio.getgid()}
368 return not key and s or s[key]
371 function process.list()
374 local ps = luci.util.execi("/bin/busybox top -bn1")
381 local pid, ppid, user, stat, vsz, mem, cpu, cmd = line:match(
382 "^ *(%d+) +(%d+) +(%S.-%S) +([RSDZTW][<NW ][<N ]) +(%d+) +(%d+%%) +(%d+%%) +(.+)"
385 local idx = tonumber(pid)
386 if idx and not cmd:match("top %-bn1") then
403 function process.setgroup(gid)
404 return nixio.setgid(gid)
407 function process.setuser(uid)
408 return nixio.setuid(uid)
411 process.signal = nixio.kill
413 local function xclose(fd)
414 if fd and fd:fileno() > 2 then
419 function process.exec(command, stdout, stderr, nowait)
420 local out_r, out_w, err_r, err_w
421 if stdout then out_r, out_w = nixio.pipe() end
422 if stderr then err_r, err_w = nixio.pipe() end
424 local pid = nixio.fork()
428 local null = nixio.open("/dev/null", "w+")
430 nixio.dup(out_w or null, nixio.stdout)
431 nixio.dup(err_w or null, nixio.stderr)
432 nixio.dup(null, nixio.stdin)
440 nixio.exec(unpack(command))
444 local _, pfds, rv = nil, {}, { code = -1, pid = pid }
452 cb = type(stdout) == "function" and stdout,
454 events = nixio.poll_flags("in", "err", "hup")
461 cb = type(stderr) == "function" and stderr,
463 events = nixio.poll_flags("in", "err", "hup")
468 local nfds, err = nixio.poll(pfds, -1)
469 if not nfds and err ~= nixio.const.EINTR then
474 for i = #pfds, 1, -1 do
476 if rfd.revents > 0 then
477 local chunk, err = rfd.fd:read(4096)
478 if chunk and #chunk > 0 then
482 rfd.buf = rfd.buf or {}
483 rfd.buf[#rfd.buf + 1] = chunk
486 table.remove(pfds, i)
488 rv[rfd.name] = table.concat(rfd.buf, "")
497 _, _, rv.code = nixio.waitpid(pid)
506 -- { "uid", "gid", "name", "passwd", "dir", "shell", "gecos" }
507 user.getuser = nixio.getpw
509 function user.getpasswd(username)
510 local pwe = nixio.getsp and nixio.getsp(username) or nixio.getpw(username)
511 local pwh = pwe and (pwe.pwdp or pwe.passwd)
512 if not pwh or #pwh < 1 or pwh == "!" or pwh == "x" then
519 function user.checkpasswd(username, pass)
520 local pwh, pwe = user.getpasswd(username)
522 return (pwh == nil or nixio.crypt(pass, pwh) == pwh)
527 function user.setpasswd(username, password)
528 return os.execute("(echo %s; sleep 1; echo %s) | passwd %s >/dev/null 2>&1" %{
529 luci.util.shellquote(password),
530 luci.util.shellquote(password),
531 luci.util.shellquote(username)
538 function wifi.getiwinfo(ifname)
539 local ntm = require "luci.model.network"
543 local wnet = ntm:get_wifinet(ifname)
544 if wnet and wnet.iwinfo then
548 local wdev = ntm:get_wifidev(ifname)
549 if wdev and wdev.iwinfo then
553 return { ifname = ifname }
558 init.dir = "/etc/init.d/"
560 function init.names()
562 for name in fs.glob(init.dir.."*") do
563 names[#names+1] = fs.basename(name)
568 function init.index(name)
569 if fs.access(init.dir..name) then
570 return call("env -i sh -c 'source %s%s enabled; exit ${START:-255}' >/dev/null"
575 local function init_action(action, name)
576 if fs.access(init.dir..name) then
577 return call("env -i %s%s %s >/dev/null" %{ init.dir, name, action })
581 function init.enabled(name)
582 return (init_action("enabled", name) == 0)
585 function init.enable(name)
586 return (init_action("enable", name) == 0)
589 function init.disable(name)
590 return (init_action("disable", name) == 0)
593 function init.start(name)
594 return (init_action("start", name) == 0)
597 function init.stop(name)
598 return (init_action("stop", name) == 0)
601 function init.restart(name)
602 return (init_action("restart", name) == 0)
605 function init.reload(name)
606 return (init_action("reload", name) == 0)