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 =
17 tonumber, ipairs, pairs, pcall, type, next, setmetatable, require, select
23 return os.execute(...) / 256
30 local k = {"fs", "blocks", "used", "available", "percent", "mountpoint"}
31 local ps = luci.util.execi("df")
43 for value in line:gmatch("[^%s]+") do
50 -- this is a rather ugly workaround to cope with wrapped lines in
53 -- /dev/scsi/host0/bus0/target0/lun0/part3
54 -- 114382024 93566472 15005244 86% /mnt/usb
60 for value in line:gmatch("[^%s]+") do
66 table.insert(data, row)
73 -- containing the whole environment is returned otherwise this function returns
74 -- the corresponding string value for the given name or nil if no such variable
78 function hostname(newname)
79 if type(newname) == "string" and #newname > 0 then
80 fs.writefile( "/proc/sys/kernel/hostname", newname )
83 return nixio.uname().nodename
87 function httpget(url, stream, target)
89 local source = stream and io.popen or luci.util.exec
90 return source("wget -qO- %s" % luci.util.shellquote(url))
92 return os.execute("wget -qO %s %s" %
93 {luci.util.shellquote(target), luci.util.shellquote(url)})
98 return os.execute("reboot >/dev/null 2>&1")
102 return luci.util.exec("logread")
106 return luci.util.exec("dmesg")
109 function uniqueid(bytes)
110 local rand = fs.readfile("/dev/urandom", bytes)
111 return rand and nixio.bin.hexlify(rand)
115 return nixio.sysinfo().uptime
121 local function _nethints(what, callback)
122 local _, k, e, mac, ip, name
123 local cur = uci.cursor()
128 local function _add(i, ...)
129 local k = select(i, ...)
131 if not hosts[k] then hosts[k] = { } end
132 hosts[k][1] = select(1, ...) or hosts[k][1]
133 hosts[k][2] = select(2, ...) or hosts[k][2]
134 hosts[k][3] = select(3, ...) or hosts[k][3]
135 hosts[k][4] = select(4, ...) or hosts[k][4]
139 luci.ip.neighbors(nil, function(neigh)
140 if neigh.mac and neigh.family == 4 then
141 _add(what, neigh.mac:string(), neigh.dest:string(), nil, nil)
142 elseif neigh.mac and neigh.family == 6 then
143 _add(what, neigh.mac:string(), nil, neigh.dest:string(), nil)
147 if fs.access("/etc/ethers") then
148 for e in io.lines("/etc/ethers") do
149 mac, name = e:match("^([a-fA-F0-9:-]+)%s+(%S+)")
150 mac = luci.ip.checkmac(mac)
152 if luci.ip.checkip4(name) then
153 _add(what, mac, name, nil, nil)
155 _add(what, mac, nil, nil, name)
161 cur:foreach("dhcp", "dnsmasq",
163 if s.leasefile and fs.access(s.leasefile) then
164 for e in io.lines(s.leasefile) do
165 mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
166 mac = luci.ip.checkmac(mac)
168 _add(what, mac, ip, nil, name ~= "*" and name)
175 cur:foreach("dhcp", "host",
177 for mac in luci.util.imatch(s.mac) do
178 mac = luci.ip.checkmac(mac)
180 _add(what, mac, s.ip, nil, s.name)
185 for _, e in ipairs(nixio.getifaddrs()) do
186 if e.name ~= "lo" then
187 ifn[e.name] = ifn[e.name] or { }
188 if e.family == "packet" and e.addr and #e.addr == 17 then
189 ifn[e.name][1] = e.addr:upper()
190 elseif e.family == "inet" then
191 ifn[e.name][2] = e.addr
192 elseif e.family == "inet6" then
193 ifn[e.name][3] = e.addr
198 for _, e in pairs(ifn) do
199 if e[what] and (e[2] or e[3]) then
200 _add(what, e[1], e[2], e[3], e[4])
204 for _, e in pairs(hosts) do
205 lookup[#lookup+1] = (what > 1) and e[what] or (e[2] or e[3])
209 lookup = luci.util.ubus("network.rrdns", "lookup", {
216 for _, e in luci.util.kspairs(hosts) do
217 callback(e[1], e[2], e[3], lookup[e[2]] or lookup[e[3]] or e[4])
221 -- Each entry contains the values in the following order:
223 function net.mac_hints(callback)
225 _nethints(1, function(mac, v4, v6, name)
227 if name and name ~= mac then
228 callback(mac, name or v4)
233 _nethints(1, function(mac, v4, v6, name)
235 if name and name ~= mac then
236 rv[#rv+1] = { mac, name or v4 }
243 -- Each entry contains the values in the following order:
245 function net.ipv4_hints(callback)
247 _nethints(2, function(mac, v4, v6, name)
249 if name and name ~= v4 then
255 _nethints(2, function(mac, v4, v6, name)
257 if name and name ~= v4 then
258 rv[#rv+1] = { v4, name }
265 -- Each entry contains the values in the following order:
267 function net.ipv6_hints(callback)
269 _nethints(3, function(mac, v4, v6, name)
271 if name and name ~= v6 then
277 _nethints(3, function(mac, v4, v6, name)
279 if name and name ~= v6 then
280 rv[#rv+1] = { v6, name }
287 function net.host_hints(callback)
289 _nethints(1, function(mac, v4, v6, name)
290 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
291 callback(mac, v4, v6, name)
296 _nethints(1, function(mac, v4, v6, name)
297 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
299 if v4 then e.ipv4 = v4 end
300 if v6 then e.ipv6 = v6 end
301 if name then e.name = name end
309 function net.conntrack(callback)
310 local ok, nfct = pcall(io.lines, "/proc/net/nf_conntrack")
311 if not ok or not nfct then
315 local line, connt = nil, (not callback) and { }
317 local fam, l3, l4, timeout, tuples =
318 line:match("^(ipv[46]) +(%d+) +%S+ +(%d+) +(%d+) +(.+)$")
320 if fam and l3 and l4 and timeout and not tuples:match("^TIME_WAIT ") then
321 l4 = nixio.getprotobynumber(l4)
327 layer4 = l4 and l4.name or "unknown",
328 timeout = tonumber(timeout, 10)
332 for key, val in tuples:gmatch("(%w+)=(%S+)") do
333 if key == "bytes" or key == "packets" then
334 entry[key] = entry[key] + tonumber(val, 10)
335 elseif key == "src" or key == "dst" then
336 if entry[key] == nil then
337 entry[key] = luci.ip.new(val):string()
339 elseif key == "sport" or key == "dport" then
340 if entry[key] == nil then
351 connt[#connt+1] = entry
356 return callback and true or connt
359 function net.devices()
362 for k, v in ipairs(nixio.getifaddrs()) do
363 if v.name and not seen[v.name] then
365 devs[#devs+1] = v.name
374 function process.info(key)
375 local s = {uid = nixio.getuid(), gid = nixio.getgid()}
376 return not key and s or s[key]
379 function process.list()
382 local ps = luci.util.execi("/bin/busybox top -bn1")
389 local pid, ppid, user, stat, vsz, mem, cpu, cmd = line:match(
390 "^ *(%d+) +(%d+) +(%S.-%S) +([RSDZTW][W ][<N ]) +(%d+) +(%d+%%) +(%d+%%) +(.+)"
393 local idx = tonumber(pid)
411 function process.setgroup(gid)
412 return nixio.setgid(gid)
415 function process.setuser(uid)
416 return nixio.setuid(uid)
419 process.signal = nixio.kill
424 -- { "uid", "gid", "name", "passwd", "dir", "shell", "gecos" }
425 user.getuser = nixio.getpw
427 function user.getpasswd(username)
428 local pwe = nixio.getsp and nixio.getsp(username) or nixio.getpw(username)
429 local pwh = pwe and (pwe.pwdp or pwe.passwd)
430 if not pwh or #pwh < 1 or pwh == "!" or pwh == "x" then
437 function user.checkpasswd(username, pass)
438 local pwh, pwe = user.getpasswd(username)
440 return (pwh == nil or nixio.crypt(pass, pwh) == pwh)
445 function user.setpasswd(username, password)
446 return os.execute("(echo %s; sleep 1; echo %s) | passwd %s >/dev/null 2>&1" %{
447 luci.util.shellquote(password),
448 luci.util.shellquote(password),
449 luci.util.shellquote(username)
456 function wifi.getiwinfo(ifname)
459 local wnet = ntm:get_wifinet(ifname)
460 if wnet and wnet.iwinfo then
464 local wdev = ntm:get_wifidev(ifname)
465 if wdev and wdev.iwinfo then
469 return { ifname = ifname }
474 init.dir = "/etc/init.d/"
476 function init.names()
478 for name in fs.glob(init.dir.."*") do
479 names[#names+1] = fs.basename(name)
484 function init.index(name)
485 if fs.access(init.dir..name) then
486 return call("env -i sh -c 'source %s%s enabled; exit ${START:-255}' >/dev/null"
491 local function init_action(action, name)
492 if fs.access(init.dir..name) then
493 return call("env -i %s%s %s >/dev/null" %{ init.dir, name, action })
497 function init.enabled(name)
498 return (init_action("enabled", name) == 0)
501 function init.enable(name)
502 return (init_action("enable", name) == 1)
505 function init.disable(name)
506 return (init_action("disable", name) == 0)
509 function init.start(name)
510 return (init_action("start", name) == 0)
513 function init.stop(name)
514 return (init_action("stop", name) == 0)