luci-mod-network: interfaces.js: issue iface restart via file/exec rpc call
[project/luci.git] / modules / luci-mod-network / luasrc / controller / admin / network.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2011-2018 Jo-Philipp Wich <jo@mein.io>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.controller.admin.network", package.seeall)
6
7 function index()
8 local uci = require("luci.model.uci").cursor()
9 local page
10
11 -- if page.inreq then
12 local has_switch = false
13
14 uci:foreach("network", "switch",
15 function(s)
16 has_switch = true
17 return false
18 end)
19
20 if has_switch then
21 entry({"admin", "network", "switch"}, view("network/switch"), _("Switch"), 20)
22 end
23
24
25 local has_wifi = false
26
27 uci:foreach("wireless", "wifi-device",
28 function(s)
29 has_wifi = true
30 return false
31 end)
32
33 if has_wifi then
34 page = entry({"admin", "network", "wireless"}, view("network/wireless"), _('Wireless'), 15)
35 page.leaf = true
36 end
37
38
39 page = entry({"admin", "network", "iface_down"}, post("iface_down"), nil)
40 page.leaf = true
41
42 page = entry({"admin", "network", "network"}, view("network/interfaces"), _("Interfaces"), 10)
43 page.leaf = true
44 page.subindex = true
45
46
47 if nixio.fs.access("/etc/config/dhcp") then
48 page = node("admin", "network", "dhcp")
49 page.target = view("network/dhcp")
50 page.title = _("DHCP and DNS")
51 page.order = 30
52
53 page = node("admin", "network", "hosts")
54 page.target = view("network/hosts")
55 page.title = _("Hostnames")
56 page.order = 40
57 end
58
59 page = node("admin", "network", "routes")
60 page.target = view("network/routes")
61 page.title = _("Static Routes")
62 page.order = 50
63
64 page = node("admin", "network", "diagnostics")
65 page.target = template("admin_network/diagnostics")
66 page.title = _("Diagnostics")
67 page.order = 60
68
69 page = entry({"admin", "network", "diag_ping"}, post("diag_ping"), nil)
70 page.leaf = true
71
72 page = entry({"admin", "network", "diag_nslookup"}, post("diag_nslookup"), nil)
73 page.leaf = true
74
75 page = entry({"admin", "network", "diag_traceroute"}, post("diag_traceroute"), nil)
76 page.leaf = true
77
78 page = entry({"admin", "network", "diag_ping6"}, post("diag_ping6"), nil)
79 page.leaf = true
80
81 page = entry({"admin", "network", "diag_traceroute6"}, post("diag_traceroute6"), nil)
82 page.leaf = true
83 -- end
84 end
85
86 local function addr2dev(addr, src)
87 local ip = require "luci.ip"
88 local route = ip.route(addr, src)
89 if not src and route and route.src then
90 route = ip.route(addr, route.src:string())
91 end
92 return route and route.dev
93 end
94
95 function iface_down(iface, force)
96 local netmd = require "luci.model.network".init()
97 local peer = luci.http.getenv("REMOTE_ADDR")
98 local serv = luci.http.getenv("SERVER_ADDR")
99
100 if force ~= "force" and serv and peer then
101 local dev = addr2dev(peer, serv)
102 if dev then
103 local nets = netmd:get_networks()
104 local outnet = nil
105 local _, net, ai
106
107 for _, net in ipairs(nets) do
108 if net:contains_interface(dev) then
109 outnet = net
110 break
111 end
112 end
113
114 if outnet:name() == iface then
115 luci.http.status(409, "Is inbound interface")
116 return
117 end
118
119 local peeraddr = outnet:get("peeraddr")
120 for _, ai in ipairs(peeraddr and nixio.getaddrinfo(peeraddr) or {}) do
121 local peerdev = addr2dev(ai.address)
122 for _, net in ipairs(peerdev and nets or {}) do
123 if net:contains_interface(peerdev) and net:name() == iface then
124 luci.http.status(409, "Is inbound interface")
125 return
126 end
127 end
128 end
129 end
130 end
131
132 if netmd:get_network(iface) then
133 luci.sys.call("env -i /sbin/ifdown %s >/dev/null 2>/dev/null"
134 % luci.util.shellquote(iface))
135 luci.http.status(200, "Shut down")
136 return
137 end
138
139 luci.http.status(404, "No such interface")
140 end
141
142 function diag_command(cmd, addr)
143 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
144 luci.http.prepare_content("text/plain")
145
146 local util = io.popen(cmd % luci.util.shellquote(addr))
147 if util then
148 while true do
149 local ln = util:read("*l")
150 if not ln then break end
151 luci.http.write(ln)
152 luci.http.write("\n")
153 end
154
155 util:close()
156 end
157
158 return
159 end
160
161 luci.http.status(500, "Bad address")
162 end
163
164 function diag_ping(addr)
165 diag_command("ping -c 5 -W 1 %s 2>&1", addr)
166 end
167
168 function diag_traceroute(addr)
169 diag_command("traceroute -q 1 -w 1 -n %s 2>&1", addr)
170 end
171
172 function diag_nslookup(addr)
173 diag_command("nslookup %s 2>&1", addr)
174 end
175
176 function diag_ping6(addr)
177 diag_command("ping6 -c 5 %s 2>&1", addr)
178 end
179
180 function diag_traceroute6(addr)
181 diag_command("traceroute6 -q 1 -w 2 -n %s 2>&1", addr)
182 end