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, name = e:match("^([a-fA-F0-9:]+)%s+(%S+)")
151 if luci.ip.IPv4(name) then
152 _add(what, mac:upper(), name, nil, nil)
154 _add(what, mac:upper(), nil, nil, name)
160 cur:foreach("dhcp", "dnsmasq",
162 if s.leasefile and fs.access(s.leasefile) then
163 for e in io.lines(s.leasefile) do
164 mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
166 _add(what, mac:upper(), ip, nil, name ~= "*" and name)
173 cur:foreach("dhcp", "host",
175 for mac in luci.util.imatch(s.mac) do
176 _add(what, mac:upper(), s.ip, nil, s.name)
180 for _, e in ipairs(nixio.getifaddrs()) do
181 if e.name ~= "lo" then
182 ifn[e.name] = ifn[e.name] or { }
183 if e.family == "packet" and e.addr and #e.addr == 17 then
184 ifn[e.name][1] = e.addr:upper()
185 elseif e.family == "inet" then
186 ifn[e.name][2] = e.addr
187 elseif e.family == "inet6" then
188 ifn[e.name][3] = e.addr
193 for _, e in pairs(ifn) do
194 if e[what] and (e[2] or e[3]) then
195 _add(what, e[1], e[2], e[3], e[4])
199 for _, e in pairs(hosts) do
200 lookup[#lookup+1] = (what > 1) and e[what] or (e[2] or e[3])
204 lookup = luci.util.ubus("network.rrdns", "lookup", {
211 for _, e in luci.util.kspairs(hosts) do
212 callback(e[1], e[2], e[3], lookup[e[2]] or lookup[e[3]] or e[4])
216 -- Each entry contains the values in the following order:
218 function net.mac_hints(callback)
220 _nethints(1, function(mac, v4, v6, name)
222 if name and name ~= mac then
223 callback(mac, name or v4)
228 _nethints(1, function(mac, v4, v6, name)
230 if name and name ~= mac then
231 rv[#rv+1] = { mac, name or v4 }
238 -- Each entry contains the values in the following order:
240 function net.ipv4_hints(callback)
242 _nethints(2, function(mac, v4, v6, name)
244 if name and name ~= v4 then
250 _nethints(2, function(mac, v4, v6, name)
252 if name and name ~= v4 then
253 rv[#rv+1] = { v4, name }
260 -- Each entry contains the values in the following order:
262 function net.ipv6_hints(callback)
264 _nethints(3, function(mac, v4, v6, name)
266 if name and name ~= v6 then
272 _nethints(3, function(mac, v4, v6, name)
274 if name and name ~= v6 then
275 rv[#rv+1] = { v6, name }
282 function net.host_hints(callback)
284 _nethints(1, function(mac, v4, v6, name)
285 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
286 callback(mac, v4, v6, name)
291 _nethints(1, function(mac, v4, v6, name)
292 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
294 if v4 then e.ipv4 = v4 end
295 if v6 then e.ipv6 = v6 end
296 if name then e.name = name end
304 function net.conntrack(callback)
305 local ok, nfct = pcall(io.lines, "/proc/net/nf_conntrack")
306 if not ok or not nfct then
310 local line, connt = nil, (not callback) and { }
312 local fam, l3, l4, timeout, tuples =
313 line:match("^(ipv[46]) +(%d+) +%S+ +(%d+) +(%d+) +(.+)$")
315 if fam and l3 and l4 and timeout and not tuples:match("^TIME_WAIT ") then
316 l4 = nixio.getprotobynumber(l4)
322 layer4 = l4 and l4.name or "unknown",
323 timeout = tonumber(timeout, 10)
327 for key, val in tuples:gmatch("(%w+)=(%S+)") do
328 if key == "bytes" or key == "packets" then
329 entry[key] = entry[key] + tonumber(val, 10)
330 elseif key == "src" or key == "dst" then
331 if entry[key] == nil then
332 entry[key] = luci.ip.new(val):string()
334 elseif key == "sport" or key == "dport" then
335 if entry[key] == nil then
346 connt[#connt+1] = entry
351 return callback and true or connt
354 function net.devices()
357 for k, v in ipairs(nixio.getifaddrs()) do
358 if v.name and not seen[v.name] then
360 devs[#devs+1] = v.name
369 function process.info(key)
370 local s = {uid = nixio.getuid(), gid = nixio.getgid()}
371 return not key and s or s[key]
374 function process.list()
377 local ps = luci.util.execi("/bin/busybox top -bn1")
384 local pid, ppid, user, stat, vsz, mem, cpu, cmd = line:match(
385 "^ *(%d+) +(%d+) +(%S.-%S) +([RSDZTW][W ][<N ]) +(%d+) +(%d+%%) +(%d+%%) +(.+)"
388 local idx = tonumber(pid)
406 function process.setgroup(gid)
407 return nixio.setgid(gid)
410 function process.setuser(uid)
411 return nixio.setuid(uid)
414 process.signal = nixio.kill
419 -- { "uid", "gid", "name", "passwd", "dir", "shell", "gecos" }
420 user.getuser = nixio.getpw
422 function user.getpasswd(username)
423 local pwe = nixio.getsp and nixio.getsp(username) or nixio.getpw(username)
424 local pwh = pwe and (pwe.pwdp or pwe.passwd)
425 if not pwh or #pwh < 1 or pwh == "!" or pwh == "x" then
432 function user.checkpasswd(username, pass)
433 local pwh, pwe = user.getpasswd(username)
435 return (pwh == nil or nixio.crypt(pass, pwh) == pwh)
440 function user.setpasswd(username, password)
442 password = password:gsub("'", [['"'"']])
446 username = username:gsub("'", [['"'"']])
450 "(echo '" .. password .. "'; sleep 1; echo '" .. password .. "') | " ..
451 "passwd '" .. username .. "' >/dev/null 2>&1"
458 function wifi.getiwinfo(ifname)
461 local wnet = ntm:get_wifinet(ifname)
462 if wnet and wnet.iwinfo then
466 local wdev = ntm:get_wifidev(ifname)
467 if wdev and wdev.iwinfo then
471 return { ifname = ifname }
476 init.dir = "/etc/init.d/"
478 function init.names()
480 for name in fs.glob(init.dir.."*") do
481 names[#names+1] = fs.basename(name)
486 function init.index(name)
487 if fs.access(init.dir..name) then
488 return call("env -i sh -c 'source %s%s enabled; exit ${START:-255}' >/dev/null"
493 local function init_action(action, name)
494 if fs.access(init.dir..name) then
495 return call("env -i %s%s %s >/dev/null" %{ init.dir, name, action })
499 function init.enabled(name)
500 return (init_action("enabled", name) == 0)
503 function init.enable(name)
504 return (init_action("enable", name) == 1)
507 function init.disable(name)
508 return (init_action("disable", name) == 0)
511 function init.start(name)
512 return (init_action("start", name) == 0)
515 function init.stop(name)
516 return (init_action("stop", name) == 0)