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()
126 local function _add(i, ...)
127 local k = select(i, ...)
129 if not hosts[k] then hosts[k] = { } end
130 hosts[k][1] = select(1, ...) or hosts[k][1]
131 hosts[k][2] = select(2, ...) or hosts[k][2]
132 hosts[k][3] = select(3, ...) or hosts[k][3]
133 hosts[k][4] = select(4, ...) or hosts[k][4]
137 luci.ip.neighbors(nil, function(neigh)
138 if neigh.mac and neigh.family == 4 then
139 _add(what, neigh.mac:upper(), neigh.dest:string(), nil, nil)
140 elseif neigh.mac and neigh.family == 6 then
141 _add(what, neigh.mac:upper(), nil, neigh.dest:string(), nil)
145 if fs.access("/etc/ethers") then
146 for e in io.lines("/etc/ethers") do
147 mac, ip = e:match("^([a-f0-9]%S+) (%S+)")
149 _add(what, mac:upper(), ip, nil, nil)
154 cur:foreach("dhcp", "dnsmasq",
156 if s.leasefile and fs.access(s.leasefile) then
157 for e in io.lines(s.leasefile) do
158 mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
160 _add(what, mac:upper(), ip, nil, name ~= "*" and name)
167 cur:foreach("dhcp", "host",
169 for mac in luci.util.imatch(s.mac) do
170 _add(what, mac:upper(), s.ip, nil, s.name)
174 for _, e in ipairs(nixio.getifaddrs()) do
175 if e.name ~= "lo" then
176 ifn[e.name] = ifn[e.name] or { }
177 if e.family == "packet" and e.addr and #e.addr == 17 then
178 ifn[e.name][1] = e.addr:upper()
179 elseif e.family == "inet" then
180 ifn[e.name][2] = e.addr
181 elseif e.family == "inet6" then
182 ifn[e.name][3] = e.addr
187 for _, e in pairs(ifn) do
188 if e[what] and (e[2] or e[3]) then
189 _add(what, e[1], e[2], e[3], e[4])
193 for _, e in luci.util.kspairs(hosts) do
194 callback(e[1], e[2], e[3], e[4])
198 -- Each entry contains the values in the following order:
200 function net.mac_hints(callback)
202 _nethints(1, function(mac, v4, v6, name)
203 name = name or nixio.getnameinfo(v4 or v6, nil, 100) or v4
204 if name and name ~= mac then
205 callback(mac, name or nixio.getnameinfo(v4 or v6, nil, 100) or v4)
210 _nethints(1, function(mac, v4, v6, name)
211 name = name or nixio.getnameinfo(v4 or v6, nil, 100) or v4
212 if name and name ~= mac then
213 rv[#rv+1] = { mac, name or nixio.getnameinfo(v4 or v6, nil, 100) or v4 }
220 -- Each entry contains the values in the following order:
222 function net.ipv4_hints(callback)
224 _nethints(2, function(mac, v4, v6, name)
225 name = name or nixio.getnameinfo(v4, nil, 100) or mac
226 if name and name ~= v4 then
232 _nethints(2, function(mac, v4, v6, name)
233 name = name or nixio.getnameinfo(v4, nil, 100) or mac
234 if name and name ~= v4 then
235 rv[#rv+1] = { v4, name }
242 -- Each entry contains the values in the following order:
244 function net.ipv6_hints(callback)
246 _nethints(3, function(mac, v4, v6, name)
247 name = name or nixio.getnameinfo(v6, nil, 100) or mac
248 if name and name ~= v6 then
254 _nethints(3, function(mac, v4, v6, name)
255 name = name or nixio.getnameinfo(v6, nil, 100) or mac
256 if name and name ~= v6 then
257 rv[#rv+1] = { v6, name }
264 function net.host_hints(callback)
266 _nethints(1, function(mac, v4, v6, name)
267 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
268 callback(mac, v4, v6, name)
273 _nethints(1, function(mac, v4, v6, name)
274 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
276 if v4 then e.ipv4 = v4 end
277 if v6 then e.ipv6 = v6 end
278 if name then e.name = name end
286 function net.conntrack(callback)
287 local ok, nfct = pcall(io.lines, "/proc/net/nf_conntrack")
288 if not ok or not nfct then
292 local line, connt = nil, (not callback) and { }
294 local fam, l3, l4, timeout, tuples =
295 line:match("^(ipv[46]) +(%d+) +%S+ +(%d+) +(%d+) +(.+)$")
297 if fam and l3 and l4 and timeout and not tuples:match("^TIME_WAIT ") then
298 l4 = nixio.getprotobynumber(l4)
304 layer4 = l4 and l4.name or "unknown",
305 timeout = tonumber(timeout, 10)
309 for key, val in tuples:gmatch("(%w+)=(%S+)") do
310 if key == "bytes" or key == "packets" then
311 entry[key] = entry[key] + tonumber(val, 10)
312 elseif key == "src" or key == "dst" then
313 if entry[key] == nil then
314 entry[key] = luci.ip.new(val):string()
316 elseif key == "sport" or key == "dport" then
317 if entry[key] == nil then
328 connt[#connt+1] = entry
333 return callback and true or connt
336 function net.devices()
338 for k, v in ipairs(nixio.getifaddrs()) do
339 if v.family == "packet" then
340 devs[#devs+1] = v.name
349 function process.info(key)
350 local s = {uid = nixio.getuid(), gid = nixio.getgid()}
351 return not key and s or s[key]
354 function process.list()
357 local ps = luci.util.execi("/bin/busybox top -bn1")
364 local pid, ppid, user, stat, vsz, mem, cpu, cmd = line:match(
365 "^ *(%d+) +(%d+) +(%S.-%S) +([RSDZTW][W ][<N ]) +(%d+) +(%d+%%) +(%d+%%) +(.+)"
368 local idx = tonumber(pid)
386 function process.setgroup(gid)
387 return nixio.setgid(gid)
390 function process.setuser(uid)
391 return nixio.setuid(uid)
394 process.signal = nixio.kill
399 -- { "uid", "gid", "name", "passwd", "dir", "shell", "gecos" }
400 user.getuser = nixio.getpw
402 function user.getpasswd(username)
403 local pwe = nixio.getsp and nixio.getsp(username) or nixio.getpw(username)
404 local pwh = pwe and (pwe.pwdp or pwe.passwd)
405 if not pwh or #pwh < 1 or pwh == "!" or pwh == "x" then
412 function user.checkpasswd(username, pass)
413 local pwh, pwe = user.getpasswd(username)
415 return (pwh == nil or nixio.crypt(pass, pwh) == pwh)
420 function user.setpasswd(username, password)
422 password = password:gsub("'", [['"'"']])
426 username = username:gsub("'", [['"'"']])
430 "(echo '" .. password .. "'; sleep 1; echo '" .. password .. "') | " ..
431 "passwd '" .. username .. "' >/dev/null 2>&1"
438 function wifi.getiwinfo(ifname)
439 local stat, iwinfo = pcall(require, "iwinfo")
442 local d, n = ifname:match("^(%w+)%.network(%d+)")
443 local wstate = luci.util.ubus("network.wireless", "status") or { }
446 n = n and tonumber(n) or 1
448 if type(wstate[d]) == "table" and
449 type(wstate[d].interfaces) == "table" and
450 type(wstate[d].interfaces[n]) == "table" and
451 type(wstate[d].interfaces[n].ifname) == "string"
453 ifname = wstate[d].interfaces[n].ifname
458 local t = stat and iwinfo.type(ifname)
459 local x = t and iwinfo[t] or { }
460 return setmetatable({}, {
461 __index = function(t, k)
462 if k == "ifname" then
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)