modules/admin-full: disable hosts page if dnsmasq is not installed
[project/luci.git] / modules / admin-full / luasrc / controller / admin / network.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 module("luci.controller.admin.network", package.seeall)
16
17 function index()
18 require("luci.i18n")
19 local uci = require("luci.model.uci").cursor()
20 local i18n = luci.i18n.translate
21 local has_wifi = nixio.fs.stat("/etc/config/wireless")
22 local has_switch = false
23
24 uci:foreach("network", "switch",
25 function(s)
26 has_switch = true
27 return false
28 end
29 )
30
31 local page
32
33 page = node("admin", "network")
34 page.target = alias("admin", "network", "network")
35 page.title = i18n("Network")
36 page.order = 50
37 page.index = true
38
39 if has_switch then
40 page = node("admin", "network", "vlan")
41 page.target = cbi("admin_network/vlan")
42 page.title = i18n("Switch")
43 page.order = 20
44 end
45
46 if has_wifi and has_wifi.size > 0 then
47 page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("Wifi"), 15)
48 page.leaf = true
49 page.subindex = true
50
51 page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
52 page.leaf = true
53
54 page = entry({"admin", "network", "wireless_add"}, call("wifi_add"), nil, 16)
55 page.leaf = true
56
57 page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
58 page.leaf = true
59
60 page = entry({"admin", "network", "wireless_status"}, call("wifi_status"), nil, 16)
61 page.leaf = true
62 end
63
64 page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
65 page.leaf = true
66 page.subindex = true
67
68 page = entry({"admin", "network", "iface_add"}, cbi("admin_network/iface_add"), nil)
69 page.leaf = true
70
71 page = entry({"admin", "network", "iface_delete"}, call("iface_delete"), nil)
72 page.leaf = true
73
74 page = entry({"admin", "network", "iface_status"}, call("iface_status"), nil)
75 page.leaf = true
76
77 page = entry({"admin", "network", "iface_reconnect"}, call("iface_reconnect"), nil)
78 page.leaf = true
79
80 page = entry({"admin", "network", "iface_shutdown"}, call("iface_shutdown"), nil)
81 page.leaf = true
82
83 uci:foreach("network", "interface",
84 function (section)
85 local ifc = section[".name"]
86 if ifc ~= "loopback" then
87 entry({"admin", "network", "network", ifc},
88 true,
89 ifc:upper())
90 end
91 end
92 )
93
94 if nixio.fs.access("/etc/config/dhcp") then
95 page = node("admin", "network", "dhcpleases")
96 page.target = cbi("admin_network/dhcpleases")
97 page.title = i18n("DHCP Leases")
98 page.order = 30
99
100 page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
101 page.leaf = true
102
103 page = node("admin", "network", "hosts")
104 page.target = cbi("admin_network/hosts")
105 page.title = i18n("Hostnames")
106 page.order = 40
107 end
108
109 page = node("admin", "network", "routes")
110 page.target = cbi("admin_network/routes")
111 page.title = i18n("Static Routes")
112 page.order = 50
113
114 end
115
116 function wifi_join()
117 local function param(x)
118 return luci.http.formvalue(x)
119 end
120
121 local function ptable(x)
122 x = param(x)
123 return x and (type(x) ~= "table" and { x } or x) or {}
124 end
125
126 local dev = param("device")
127 local ssid = param("join")
128
129 if dev and ssid then
130 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
131
132 if cancel then
133 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
134 else
135 local cbi = require "luci.cbi"
136 local tpl = require "luci.template"
137 local map = luci.cbi.load("admin_network/wifi_add")[1]
138
139 if map:parse() ~= cbi.FORM_DONE then
140 tpl.render("header")
141 map:render()
142 tpl.render("footer")
143 end
144 end
145 else
146 luci.template.render("admin_network/wifi_join")
147 end
148 end
149
150 function wifi_add()
151 local dev = luci.http.formvalue("device")
152 local ntm = require "luci.model.network".init()
153
154 dev = dev and ntm:get_wifidev(dev)
155
156 if dev then
157 local net = dev:add_wifinet({
158 mode = "ap",
159 ssid = "OpenWrt",
160 encryption = "none"
161 })
162
163 ntm:save("wireless")
164 luci.http.redirect(net:adminlink())
165 end
166 end
167
168 function wifi_delete(network)
169 local ntm = require "luci.model.network".init()
170
171 ntm:del_wifinet(network)
172 ntm:save("wireless")
173
174 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
175 end
176
177 function iface_status()
178 local path = luci.dispatcher.context.requestpath
179 local x = luci.model.uci.cursor_state()
180 local rv = { }
181
182 local iface
183 for iface in path[#path]:gmatch("[%w%.%-]+") do
184 local dev
185 if x:get("network", iface, "type") == "bridge" then
186 dev = "br-" .. iface
187 else
188 dev = x:get("network", iface, "device") or ""
189 end
190
191 if #dev == 0 or dev:match("^%d") or dev:match("%W") then
192 dev = x:get("network", iface, "ifname") or ""
193 dev = dev:match("%S+")
194 end
195
196 local info
197 local data = { id = iface }
198 for _, info in ipairs(nixio.getifaddrs()) do
199 local name = info.name:match("[^:]+")
200 if name == dev then
201 if info.family == "packet" then
202 data.flags = info.flags
203 data.stats = info.data
204 data.macaddr = info.addr
205 data.ifname = name
206 elseif info.family == "inet" then
207 data.ipaddrs = data.ipaddrs or { }
208 data.ipaddrs[#data.ipaddrs+1] = {
209 addr = info.addr,
210 broadaddr = info.broadaddr,
211 dstaddr = info.dstaddr,
212 netmask = info.netmask,
213 prefix = info.prefix
214 }
215 elseif info.family == "inet6" then
216 data.ip6addrs = data.ip6addrs or { }
217 data.ip6addrs[#data.ip6addrs+1] = {
218 addr = info.addr,
219 netmask = info.netmask,
220 prefix = info.prefix
221 }
222 end
223 end
224 end
225
226 if next(data) then
227 rv[#rv+1] = data
228 end
229 end
230
231 if #rv > 0 then
232 luci.http.prepare_content("application/json")
233 luci.http.write_json(rv)
234 return
235 end
236
237 luci.http.status(404, "No such device")
238 end
239
240 function iface_reconnect()
241 local path = luci.dispatcher.context.requestpath
242 local iface = path[#path]
243 local netmd = require "luci.model.network".init()
244
245 local net = netmd:get_network(iface)
246 if net then
247 local ifn
248 for _, ifn in ipairs(net:get_interfaces()) do
249 local wnet = ifn:get_wifinet()
250 if wnet then
251 local wdev = wnet:get_device()
252 if wdev then
253 luci.sys.call(
254 "env -i /sbin/wifi up %q >/dev/null 2>/dev/null"
255 % wdev:name()
256 )
257
258 luci.http.status(200, "Reconnected")
259 return
260 end
261 end
262 end
263
264 luci.sys.call("env -i /sbin/ifup %q >/dev/null 2>/dev/null" % iface)
265 luci.http.status(200, "Reconnected")
266 return
267 end
268
269 luci.http.status(404, "No such interface")
270 end
271
272 function iface_shutdown()
273 local path = luci.dispatcher.context.requestpath
274 local iface = path[#path]
275 local netmd = require "luci.model.network".init()
276
277 local net = netmd:get_network(iface)
278 if net then
279 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
280 luci.http.status(200, "Shutdown")
281 return
282 end
283
284 luci.http.status(404, "No such interface")
285 end
286
287 function iface_delete()
288 local path = luci.dispatcher.context.requestpath
289 local iface = path[#path]
290 local netmd = require "luci.model.network".init()
291
292 local net = netmd:del_network(iface)
293 if net then
294 luci.sys.call("env -i /sbin/ifdown %q >/dev/null 2>/dev/null" % iface)
295 luci.http.redirect(luci.dispatcher.build_url("admin/network/network"))
296 netmd:commit("network")
297 netmd:commit("wireless")
298 return
299 end
300
301 luci.http.status(404, "No such interface")
302 end
303
304 function wifi_status()
305 local path = luci.dispatcher.context.requestpath
306 local arp = luci.sys.net.arptable()
307 local rv = { }
308
309 local dev
310 for dev in path[#path]:gmatch("[%w%.%-]+") do
311 local j = { id = dev }
312 local iw = luci.sys.wifi.getiwinfo(dev)
313 if iw then
314 local f
315 for _, f in ipairs({
316 "channel", "frequency", "txpower", "bitrate", "signal", "noise",
317 "quality", "quality_max", "mode", "ssid", "bssid", "country",
318 "encryption", "ifname", "assoclist"
319 }) do
320 j[f] = iw[f]
321 end
322 end
323 rv[#rv+1] = j
324 end
325
326 if #rv > 0 then
327 luci.http.prepare_content("application/json")
328 luci.http.write_json(rv)
329 return
330 end
331
332 luci.http.status(404, "No such device")
333 end
334
335 function lease_status()
336 local rv = { }
337 local leasefile = "/var/dhcp.leases"
338
339 local uci = require "luci.model.uci".cursor()
340 local nfs = require "nixio.fs"
341
342 uci:foreach("dhcp", "dnsmasq",
343 function(s)
344 if s.leasefile and nfs.access(s.leasefile) then
345 leasefile = s.leasefile
346 return false
347 end
348 end)
349
350 local fd = io.open(leasefile, "r")
351 if fd then
352 while true do
353 local ln = fd:read("*l")
354 if not ln then
355 break
356 else
357 local ts, mac, ip, name = ln:match("^(%d+) (%S+) (%S+) (%S+)")
358 if ts and mac and ip and name then
359 rv[#rv+1] = {
360 expires = os.difftime(tonumber(ts) or 0, os.time()),
361 macaddr = mac,
362 ipaddr = ip,
363 hostname = (name ~= "*") and name
364 }
365 end
366 end
367 end
368 fd:close()
369 end
370
371 luci.http.prepare_content("application/json")
372 luci.http.write_json(rv)
373 end