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 local function _nethints(what, callback)
121 local _, k, e, mac, ip, name
122 local cur = uci.cursor()
127 local function _add(i, ...)
128 local k = select(i, ...)
130 if not hosts[k] then hosts[k] = { } end
131 hosts[k][1] = select(1, ...) or hosts[k][1]
132 hosts[k][2] = select(2, ...) or hosts[k][2]
133 hosts[k][3] = select(3, ...) or hosts[k][3]
134 hosts[k][4] = select(4, ...) or hosts[k][4]
138 luci.ip.neighbors(nil, function(neigh)
139 if neigh.mac and neigh.family == 4 then
140 _add(what, neigh.mac:upper(), neigh.dest:string(), nil, nil)
141 elseif neigh.mac and neigh.family == 6 then
142 _add(what, neigh.mac:upper(), nil, neigh.dest:string(), nil)
146 if fs.access("/etc/ethers") then
147 for e in io.lines("/etc/ethers") do
148 mac, ip = e:match("^([a-f0-9]%S+) (%S+)")
150 _add(what, mac:upper(), ip, nil, nil)
155 cur:foreach("dhcp", "dnsmasq",
157 if s.leasefile and fs.access(s.leasefile) then
158 for e in io.lines(s.leasefile) do
159 mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
161 _add(what, mac:upper(), ip, nil, name ~= "*" and name)
168 cur:foreach("dhcp", "host",
170 for mac in luci.util.imatch(s.mac) do
171 _add(what, mac:upper(), s.ip, nil, s.name)
175 for _, e in ipairs(nixio.getifaddrs()) do
176 if e.name ~= "lo" then
177 ifn[e.name] = ifn[e.name] or { }
178 if e.family == "packet" and e.addr and #e.addr == 17 then
179 ifn[e.name][1] = e.addr:upper()
180 elseif e.family == "inet" then
181 ifn[e.name][2] = e.addr
182 elseif e.family == "inet6" then
183 ifn[e.name][3] = e.addr
188 for _, e in pairs(ifn) do
189 if e[what] and (e[2] or e[3]) then
190 _add(what, e[1], e[2], e[3], e[4])
194 for _, e in pairs(hosts) do
195 lookup[#lookup+1] = (what > 1) and e[what] or (e[2] or e[3])
199 lookup = luci.util.ubus("network.rrdns", "lookup", {
206 for _, e in luci.util.kspairs(hosts) do
207 callback(e[1], e[2], e[3], lookup[e[2]] or lookup[e[3]] or e[4])
211 -- Each entry contains the values in the following order:
213 function net.mac_hints(callback)
215 _nethints(1, function(mac, v4, v6, name)
217 if name and name ~= mac then
218 callback(mac, name or v4)
223 _nethints(1, function(mac, v4, v6, name)
225 if name and name ~= mac then
226 rv[#rv+1] = { mac, name or v4 }
233 -- Each entry contains the values in the following order:
235 function net.ipv4_hints(callback)
237 _nethints(2, function(mac, v4, v6, name)
239 if name and name ~= v4 then
245 _nethints(2, function(mac, v4, v6, name)
247 if name and name ~= v4 then
248 rv[#rv+1] = { v4, name }
255 -- Each entry contains the values in the following order:
257 function net.ipv6_hints(callback)
259 _nethints(3, function(mac, v4, v6, name)
261 if name and name ~= v6 then
267 _nethints(3, function(mac, v4, v6, name)
269 if name and name ~= v6 then
270 rv[#rv+1] = { v6, name }
277 function net.host_hints(callback)
279 _nethints(1, function(mac, v4, v6, name)
280 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
281 callback(mac, v4, v6, name)
286 _nethints(1, function(mac, v4, v6, name)
287 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
289 if v4 then e.ipv4 = v4 end
290 if v6 then e.ipv6 = v6 end
291 if name then e.name = name end
299 function net.conntrack(callback)
300 local ok, nfct = pcall(io.lines, "/proc/net/nf_conntrack")
301 if not ok or not nfct then
305 local line, connt = nil, (not callback) and { }
307 local fam, l3, l4, timeout, tuples =
308 line:match("^(ipv[46]) +(%d+) +%S+ +(%d+) +(%d+) +(.+)$")
310 if fam and l3 and l4 and timeout and not tuples:match("^TIME_WAIT ") then
311 l4 = nixio.getprotobynumber(l4)
317 layer4 = l4 and l4.name or "unknown",
318 timeout = tonumber(timeout, 10)
322 for key, val in tuples:gmatch("(%w+)=(%S+)") do
323 if key == "bytes" or key == "packets" then
324 entry[key] = entry[key] + tonumber(val, 10)
325 elseif key == "src" or key == "dst" then
326 if entry[key] == nil then
327 entry[key] = luci.ip.new(val):string()
329 elseif key == "sport" or key == "dport" then
330 if entry[key] == nil then
341 connt[#connt+1] = entry
346 return callback and true or connt
349 function net.devices()
351 for k, v in ipairs(nixio.getifaddrs()) do
352 if v.family == "packet" then
353 devs[#devs+1] = v.name
362 function process.info(key)
363 local s = {uid = nixio.getuid(), gid = nixio.getgid()}
364 return not key and s or s[key]
367 function process.list()
370 local ps = luci.util.execi("/bin/busybox top -bn1")
377 local pid, ppid, user, stat, vsz, mem, cpu, cmd = line:match(
378 "^ *(%d+) +(%d+) +(%S.-%S) +([RSDZTW][W ][<N ]) +(%d+) +(%d+%%) +(%d+%%) +(.+)"
381 local idx = tonumber(pid)
399 function process.setgroup(gid)
400 return nixio.setgid(gid)
403 function process.setuser(uid)
404 return nixio.setuid(uid)
407 process.signal = nixio.kill
412 -- { "uid", "gid", "name", "passwd", "dir", "shell", "gecos" }
413 user.getuser = nixio.getpw
415 function user.getpasswd(username)
416 local pwe = nixio.getsp and nixio.getsp(username) or nixio.getpw(username)
417 local pwh = pwe and (pwe.pwdp or pwe.passwd)
418 if not pwh or #pwh < 1 or pwh == "!" or pwh == "x" then
425 function user.checkpasswd(username, pass)
426 local pwh, pwe = user.getpasswd(username)
428 return (pwh == nil or nixio.crypt(pass, pwh) == pwh)
433 function user.setpasswd(username, password)
435 password = password:gsub("'", [['"'"']])
439 username = username:gsub("'", [['"'"']])
443 "(echo '" .. password .. "'; sleep 1; echo '" .. password .. "') | " ..
444 "passwd '" .. username .. "' >/dev/null 2>&1"
451 function wifi.getiwinfo(ifname)
452 local stat, iwinfo = pcall(require, "iwinfo")
455 local d, n = ifname:match("^(%w+)%.network(%d+)")
456 local wstate = luci.util.ubus("network.wireless", "status") or { }
459 n = n and tonumber(n) or 1
461 if type(wstate[d]) == "table" and
462 type(wstate[d].interfaces) == "table" and
463 type(wstate[d].interfaces[n]) == "table" and
464 type(wstate[d].interfaces[n].ifname) == "string"
466 ifname = wstate[d].interfaces[n].ifname
471 local t = stat and iwinfo.type(ifname)
472 local x = t and iwinfo[t] or { }
473 return setmetatable({}, {
474 __index = function(t, k)
475 if k == "ifname" then
487 init.dir = "/etc/init.d/"
489 function init.names()
491 for name in fs.glob(init.dir.."*") do
492 names[#names+1] = fs.basename(name)
497 function init.index(name)
498 if fs.access(init.dir..name) then
499 return call("env -i sh -c 'source %s%s enabled; exit ${START:-255}' >/dev/null"
504 local function init_action(action, name)
505 if fs.access(init.dir..name) then
506 return call("env -i %s%s %s >/dev/null" %{ init.dir, name, action })
510 function init.enabled(name)
511 return (init_action("enabled", name) == 0)
514 function init.enable(name)
515 return (init_action("enable", name) == 1)
518 function init.disable(name)
519 return (init_action("disable", name) == 0)
522 function init.start(name)
523 return (init_action("start", name) == 0)
526 function init.stop(name)
527 return (init_action("stop", name) == 0)