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"
10 local ntm = require "luci.model.network"
13 luci.util = require "luci.util"
14 luci.ip = require "luci.ip"
16 local tonumber, ipairs, pairs, pcall, type, next, setmetatable, require, select, unpack =
17 tonumber, ipairs, pairs, pcall, type, next, setmetatable, require, select, unpack
23 return os.execute(...) / 256
28 -- containing the whole environment is returned otherwise this function returns
29 -- the corresponding string value for the given name or nil if no such variable
33 function hostname(newname)
34 if type(newname) == "string" and #newname > 0 then
35 fs.writefile( "/proc/sys/kernel/hostname", newname )
38 return nixio.uname().nodename
42 function httpget(url, stream, target)
44 local source = stream and io.popen or luci.util.exec
45 return source("wget -qO- %s" % luci.util.shellquote(url))
47 return os.execute("wget -qO %s %s" %
48 {luci.util.shellquote(target), luci.util.shellquote(url)})
53 return os.execute("reboot >/dev/null 2>&1")
57 return luci.util.exec("logread")
61 return luci.util.exec("dmesg")
64 function uniqueid(bytes)
65 local rand = fs.readfile("/dev/urandom", bytes)
66 return rand and nixio.bin.hexlify(rand)
70 return nixio.sysinfo().uptime
76 local function _nethints(what, callback)
77 local _, k, e, mac, ip, name, duid, iaid
78 local cur = uci.cursor()
83 local function _add(i, ...)
84 local k = select(i, ...)
86 if not hosts[k] then hosts[k] = { } end
87 hosts[k][1] = select(1, ...) or hosts[k][1]
88 hosts[k][2] = select(2, ...) or hosts[k][2]
89 hosts[k][3] = select(3, ...) or hosts[k][3]
90 hosts[k][4] = select(4, ...) or hosts[k][4]
94 luci.ip.neighbors(nil, function(neigh)
95 if neigh.mac and neigh.family == 4 then
96 _add(what, neigh.mac:string(), neigh.dest:string(), nil, nil)
97 elseif neigh.mac and neigh.family == 6 then
98 _add(what, neigh.mac:string(), nil, neigh.dest:string(), nil)
102 if fs.access("/etc/ethers") then
103 for e in io.lines("/etc/ethers") do
104 mac, name = e:match("^([a-fA-F0-9:-]+)%s+(%S+)")
105 mac = luci.ip.checkmac(mac)
107 if luci.ip.checkip4(name) then
108 _add(what, mac, name, nil, nil)
110 _add(what, mac, nil, nil, name)
116 cur:foreach("dhcp", "dnsmasq",
118 if s.leasefile and fs.access(s.leasefile) then
119 for e in io.lines(s.leasefile) do
120 mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
121 mac = luci.ip.checkmac(mac)
123 _add(what, mac, ip, nil, name ~= "*" and name)
130 cur:foreach("dhcp", "odhcpd",
132 if type(s.leasefile) == "string" and fs.access(s.leasefile) then
133 for e in io.lines(s.leasefile) do
134 duid, iaid, name, _, ip = e:match("^# %S+ (%S+) (%S+) (%S+) (-?%d+) %S+ %S+ ([0-9a-f:.]+)/[0-9]+")
135 mac = net.duid_to_mac(duid)
137 if ip and iaid == "ipv4" then
138 _add(what, mac, ip, nil, name ~= "*" and name)
140 _add(what, mac, nil, ip, name ~= "*" and name)
148 cur:foreach("dhcp", "host",
150 for mac in luci.util.imatch(s.mac) do
151 mac = luci.ip.checkmac(mac)
153 _add(what, mac, s.ip, nil, s.name)
158 for _, e in ipairs(nixio.getifaddrs()) do
159 if e.name ~= "lo" then
160 ifn[e.name] = ifn[e.name] or { }
161 if e.family == "packet" and e.addr and #e.addr == 17 then
162 ifn[e.name][1] = e.addr:upper()
163 elseif e.family == "inet" then
164 ifn[e.name][2] = e.addr
165 elseif e.family == "inet6" then
166 ifn[e.name][3] = e.addr
171 for _, e in pairs(ifn) do
172 if e[what] and (e[2] or e[3]) then
173 _add(what, e[1], e[2], e[3], e[4])
177 for _, e in pairs(hosts) do
178 lookup[#lookup+1] = (what > 1) and e[what] or (e[2] or e[3])
182 lookup = luci.util.ubus("network.rrdns", "lookup", {
189 for _, e in luci.util.kspairs(hosts) do
190 callback(e[1], e[2], e[3], lookup[e[2]] or lookup[e[3]] or e[4])
194 -- Each entry contains the values in the following order:
196 function net.mac_hints(callback)
198 _nethints(1, function(mac, v4, v6, name)
200 if name and name ~= mac then
201 callback(mac, name or v4)
206 _nethints(1, function(mac, v4, v6, name)
208 if name and name ~= mac then
209 rv[#rv+1] = { mac, name or v4 }
216 -- Each entry contains the values in the following order:
218 function net.ipv4_hints(callback)
220 _nethints(2, function(mac, v4, v6, name)
222 if name and name ~= v4 then
228 _nethints(2, function(mac, v4, v6, name)
230 if name and name ~= v4 then
231 rv[#rv+1] = { v4, name }
238 -- Each entry contains the values in the following order:
240 function net.ipv6_hints(callback)
242 _nethints(3, function(mac, v4, v6, name)
244 if name and name ~= v6 then
250 _nethints(3, function(mac, v4, v6, name)
252 if name and name ~= v6 then
253 rv[#rv+1] = { v6, name }
260 function net.host_hints(callback)
262 _nethints(1, function(mac, v4, v6, name)
263 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
264 callback(mac, v4, v6, name)
269 _nethints(1, function(mac, v4, v6, name)
270 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
272 if v4 then e.ipv4 = v4 end
273 if v6 then e.ipv6 = v6 end
274 if name then e.name = name end
282 function net.conntrack(callback)
283 local ok, nfct = pcall(io.lines, "/proc/net/nf_conntrack")
284 if not ok or not nfct then
288 local line, connt = nil, (not callback) and { }
290 local fam, l3, l4, timeout, tuples =
291 line:match("^(ipv[46]) +(%d+) +%S+ +(%d+) +(%d+) +(.+)$")
293 if fam and l3 and l4 and timeout and not tuples:match("^TIME_WAIT ") then
294 l4 = nixio.getprotobynumber(l4)
300 layer4 = l4 and l4.name or "unknown",
301 timeout = tonumber(timeout, 10)
305 for key, val in tuples:gmatch("(%w+)=(%S+)") do
306 if key == "bytes" or key == "packets" then
307 entry[key] = entry[key] + tonumber(val, 10)
308 elseif key == "src" or key == "dst" then
309 if entry[key] == nil then
310 entry[key] = luci.ip.new(val):string()
312 elseif key == "sport" or key == "dport" then
313 if entry[key] == nil then
324 connt[#connt+1] = entry
329 return callback and true or connt
332 function net.devices()
335 for k, v in ipairs(nixio.getifaddrs()) do
336 if v.name and not seen[v.name] then
338 devs[#devs+1] = v.name
344 function net.duid_to_mac(duid)
345 local b1, b2, b3, b4, b5, b6
347 if type(duid) == "string" then
348 -- DUID-LLT / Ethernet
350 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$")
352 -- DUID-LL / Ethernet
353 elseif #duid == 20 then
354 b1, b2, b3, b4, b5, b6 = duid:match("^00030001(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$")
356 -- DUID-LL / Ethernet (Without Header)
357 elseif #duid == 12 then
358 b1, b2, b3, b4, b5, b6 = duid:match("^(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$")
362 return b1 and luci.ip.checkmac(table.concat({ b1, b2, b3, b4, b5, b6 }, ":"))
367 function process.info(key)
368 local s = {uid = nixio.getuid(), gid = nixio.getgid()}
369 return not key and s or s[key]
372 function process.list()
375 local ps = luci.util.execi("/bin/busybox top -bn1")
382 local pid, ppid, user, stat, vsz, mem, cpu, cmd = line:match(
383 "^ *(%d+) +(%d+) +(%S.-%S) +([RSDZTW][<NW ][<N ]) +(%d+) +(%d+%%) +(%d+%%) +(.+)"
386 local idx = tonumber(pid)
387 if idx and not cmd:match("top %-bn1") then
404 function process.setgroup(gid)
405 return nixio.setgid(gid)
408 function process.setuser(uid)
409 return nixio.setuid(uid)
412 process.signal = nixio.kill
414 local function xclose(fd)
415 if fd and fd:fileno() > 2 then
420 function process.exec(command, stdout, stderr, nowait)
421 local out_r, out_w, err_r, err_w
422 if stdout then out_r, out_w = nixio.pipe() end
423 if stderr then err_r, err_w = nixio.pipe() end
425 local pid = nixio.fork()
429 local null = nixio.open("/dev/null", "w+")
431 nixio.dup(out_w or null, nixio.stdout)
432 nixio.dup(err_w or null, nixio.stderr)
433 nixio.dup(null, nixio.stdin)
441 nixio.exec(unpack(command))
445 local _, pfds, rv = nil, {}, { code = -1, pid = pid }
453 cb = type(stdout) == "function" and stdout,
455 events = nixio.poll_flags("in", "err", "hup")
462 cb = type(stderr) == "function" and stderr,
464 events = nixio.poll_flags("in", "err", "hup")
469 local nfds, err = nixio.poll(pfds, -1)
470 if not nfds and err ~= nixio.const.EINTR then
475 for i = #pfds, 1, -1 do
477 if rfd.revents > 0 then
478 local chunk, err = rfd.fd:read(4096)
479 if chunk and #chunk > 0 then
483 rfd.buf = rfd.buf or {}
484 rfd.buf[#rfd.buf + 1] = chunk
487 table.remove(pfds, i)
489 rv[rfd.name] = table.concat(rfd.buf, "")
498 _, _, rv.code = nixio.waitpid(pid)
507 -- { "uid", "gid", "name", "passwd", "dir", "shell", "gecos" }
508 user.getuser = nixio.getpw
510 function user.getpasswd(username)
511 local pwe = nixio.getsp and nixio.getsp(username) or nixio.getpw(username)
512 local pwh = pwe and (pwe.pwdp or pwe.passwd)
513 if not pwh or #pwh < 1 or pwh == "!" or pwh == "x" then
520 function user.checkpasswd(username, pass)
521 local pwh, pwe = user.getpasswd(username)
523 return (pwh == nil or nixio.crypt(pass, pwh) == pwh)
528 function user.setpasswd(username, password)
529 return os.execute("(echo %s; sleep 1; echo %s) | passwd %s >/dev/null 2>&1" %{
530 luci.util.shellquote(password),
531 luci.util.shellquote(password),
532 luci.util.shellquote(username)
539 function wifi.getiwinfo(ifname)
542 local wnet = ntm:get_wifinet(ifname)
543 if wnet and wnet.iwinfo then
547 local wdev = ntm:get_wifidev(ifname)
548 if wdev and wdev.iwinfo then
552 return { ifname = ifname }
557 init.dir = "/etc/init.d/"
559 function init.names()
561 for name in fs.glob(init.dir.."*") do
562 names[#names+1] = fs.basename(name)
567 function init.index(name)
568 if fs.access(init.dir..name) then
569 return call("env -i sh -c 'source %s%s enabled; exit ${START:-255}' >/dev/null"
574 local function init_action(action, name)
575 if fs.access(init.dir..name) then
576 return call("env -i %s%s %s >/dev/null" %{ init.dir, name, action })
580 function init.enabled(name)
581 return (init_action("enabled", name) == 0)
584 function init.enable(name)
585 return (init_action("enable", name) == 0)
588 function init.disable(name)
589 return (init_action("disable", name) == 0)
592 function init.start(name)
593 return (init_action("start", name) == 0)
596 function init.stop(name)
597 return (init_action("stop", name) == 0)
600 function init.restart(name)
601 return (init_action("restart", name) == 0)
604 function init.reload(name)
605 return (init_action("reload", name) == 0)