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- '"..url:gsub("'", "").."'")
92 return os.execute("wget -qO '%s' '%s'" %
93 {target:gsub("'", ""), url:gsub("'", "")})
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:upper(), neigh.dest:string(), nil, nil)
142 elseif neigh.mac and neigh.family == 6 then
143 _add(what, neigh.mac:upper(), nil, neigh.dest:string(), nil)
147 if fs.access("/etc/ethers") then
148 for e in io.lines("/etc/ethers") do
149 mac, ip = e:match("^([a-f0-9]%S+) (%S+)")
151 _add(what, mac:upper(), ip, nil, nil)
156 cur:foreach("dhcp", "dnsmasq",
158 if s.leasefile and fs.access(s.leasefile) then
159 for e in io.lines(s.leasefile) do
160 mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
162 _add(what, mac:upper(), ip, nil, name ~= "*" and name)
169 cur:foreach("dhcp", "host",
171 for mac in luci.util.imatch(s.mac) do
172 _add(what, mac:upper(), s.ip, nil, s.name)
176 for _, e in ipairs(nixio.getifaddrs()) do
177 if e.name ~= "lo" then
178 ifn[e.name] = ifn[e.name] or { }
179 if e.family == "packet" and e.addr and #e.addr == 17 then
180 ifn[e.name][1] = e.addr:upper()
181 elseif e.family == "inet" then
182 ifn[e.name][2] = e.addr
183 elseif e.family == "inet6" then
184 ifn[e.name][3] = e.addr
189 for _, e in pairs(ifn) do
190 if e[what] and (e[2] or e[3]) then
191 _add(what, e[1], e[2], e[3], e[4])
195 for _, e in pairs(hosts) do
196 lookup[#lookup+1] = (what > 1) and e[what] or (e[2] or e[3])
200 lookup = luci.util.ubus("network.rrdns", "lookup", {
207 for _, e in luci.util.kspairs(hosts) do
208 callback(e[1], e[2], e[3], lookup[e[2]] or lookup[e[3]] or e[4])
212 -- Each entry contains the values in the following order:
214 function net.mac_hints(callback)
216 _nethints(1, function(mac, v4, v6, name)
218 if name and name ~= mac then
219 callback(mac, name or v4)
224 _nethints(1, function(mac, v4, v6, name)
226 if name and name ~= mac then
227 rv[#rv+1] = { mac, name or v4 }
234 -- Each entry contains the values in the following order:
236 function net.ipv4_hints(callback)
238 _nethints(2, function(mac, v4, v6, name)
240 if name and name ~= v4 then
246 _nethints(2, function(mac, v4, v6, name)
248 if name and name ~= v4 then
249 rv[#rv+1] = { v4, name }
256 -- Each entry contains the values in the following order:
258 function net.ipv6_hints(callback)
260 _nethints(3, function(mac, v4, v6, name)
262 if name and name ~= v6 then
268 _nethints(3, function(mac, v4, v6, name)
270 if name and name ~= v6 then
271 rv[#rv+1] = { v6, name }
278 function net.host_hints(callback)
280 _nethints(1, function(mac, v4, v6, name)
281 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
282 callback(mac, v4, v6, name)
287 _nethints(1, function(mac, v4, v6, name)
288 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
290 if v4 then e.ipv4 = v4 end
291 if v6 then e.ipv6 = v6 end
292 if name then e.name = name end
300 function net.conntrack(callback)
301 local ok, nfct = pcall(io.lines, "/proc/net/nf_conntrack")
302 if not ok or not nfct then
306 local line, connt = nil, (not callback) and { }
308 local fam, l3, l4, timeout, tuples =
309 line:match("^(ipv[46]) +(%d+) +%S+ +(%d+) +(%d+) +(.+)$")
311 if fam and l3 and l4 and timeout and not tuples:match("^TIME_WAIT ") then
312 l4 = nixio.getprotobynumber(l4)
318 layer4 = l4 and l4.name or "unknown",
319 timeout = tonumber(timeout, 10)
323 for key, val in tuples:gmatch("(%w+)=(%S+)") do
324 if key == "bytes" or key == "packets" then
325 entry[key] = entry[key] + tonumber(val, 10)
326 elseif key == "src" or key == "dst" then
327 if entry[key] == nil then
328 entry[key] = luci.ip.new(val):string()
330 elseif key == "sport" or key == "dport" then
331 if entry[key] == nil then
342 connt[#connt+1] = entry
347 return callback and true or connt
350 function net.devices()
353 for k, v in ipairs(nixio.getifaddrs()) do
354 if v.name and not seen[v.name] then
356 devs[#devs+1] = v.name
365 function process.info(key)
366 local s = {uid = nixio.getuid(), gid = nixio.getgid()}
367 return not key and s or s[key]
370 function process.list()
373 local ps = luci.util.execi("/bin/busybox top -bn1")
380 local pid, ppid, user, stat, vsz, mem, cpu, cmd = line:match(
381 "^ *(%d+) +(%d+) +(%S.-%S) +([RSDZTW][W ][<N ]) +(%d+) +(%d+%%) +(%d+%%) +(.+)"
384 local idx = tonumber(pid)
402 function process.setgroup(gid)
403 return nixio.setgid(gid)
406 function process.setuser(uid)
407 return nixio.setuid(uid)
410 process.signal = nixio.kill
415 -- { "uid", "gid", "name", "passwd", "dir", "shell", "gecos" }
416 user.getuser = nixio.getpw
418 function user.getpasswd(username)
419 local pwe = nixio.getsp and nixio.getsp(username) or nixio.getpw(username)
420 local pwh = pwe and (pwe.pwdp or pwe.passwd)
421 if not pwh or #pwh < 1 or pwh == "!" or pwh == "x" then
428 function user.checkpasswd(username, pass)
429 local pwh, pwe = user.getpasswd(username)
431 return (pwh == nil or nixio.crypt(pass, pwh) == pwh)
436 function user.setpasswd(username, password)
438 password = password:gsub("'", [['"'"']])
442 username = username:gsub("'", [['"'"']])
446 "(echo '" .. password .. "'; sleep 1; echo '" .. password .. "') | " ..
447 "passwd '" .. username .. "' >/dev/null 2>&1"
454 function wifi.getiwinfo(ifname)
456 local wnet = ntm.wifinet(ifname)
457 return wnet.iwinfo or { ifname = ifname }
462 init.dir = "/etc/init.d/"
464 function init.names()
466 for name in fs.glob(init.dir.."*") do
467 names[#names+1] = fs.basename(name)
472 function init.index(name)
473 if fs.access(init.dir..name) then
474 return call("env -i sh -c 'source %s%s enabled; exit ${START:-255}' >/dev/null"
479 local function init_action(action, name)
480 if fs.access(init.dir..name) then
481 return call("env -i %s%s %s >/dev/null" %{ init.dir, name, action })
485 function init.enabled(name)
486 return (init_action("enabled", name) == 0)
489 function init.enable(name)
490 return (init_action("enable", name) == 1)
493 function init.disable(name)
494 return (init_action("disable", name) == 0)
497 function init.start(name)
498 return (init_action("start", name) == 0)
501 function init.stop(name)
502 return (init_action("stop", name) == 0)