modules/admin-full: support deleting wireless networks and fix wireless join
[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 module("luci.controller.admin.network", package.seeall)
15
16 function index()
17         require("luci.i18n")
18         local uci = require("luci.model.uci").cursor()
19         local i18n = luci.i18n.translate
20
21         local page  = node("admin", "network")
22         page.target = alias("admin", "network", "network")
23         page.title  = i18n("network")
24         page.order  = 50
25         page.index  = true
26
27         local page  = node("admin", "network", "vlan")
28         page.target = cbi("admin_network/vlan")
29         page.title  = i18n("a_n_switch")
30         page.order  = 20
31
32         local page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("wifi"), 15)
33         page.i18n   = "wifi"
34         page.leaf = true
35         page.subindex = true
36
37         uci:foreach("wireless", "wifi-device",
38                 function (section)
39                         local ifc = section[".name"]
40                                 entry({"admin", "network", "wireless", ifc},
41                                  true,
42                                  ifc:upper()).i18n = "wifi"
43                 end
44         )
45
46         local page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
47         page.i18n = "wifi"
48         page.leaf = true
49
50         local page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
51         page.i18n = "wifi"
52         page.leaf = true
53
54         local page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("interfaces", "Schnittstellen"), 10)
55         page.leaf   = true
56         page.subindex = true
57
58         local page = entry({"admin", "network", "add"}, cbi("admin_network/iface_add"), nil)
59         page.leaf = true
60
61         uci:foreach("network", "interface",
62                 function (section)
63                         local ifc = section[".name"]
64                         if ifc ~= "loopback" then
65                                 entry({"admin", "network", "network", ifc},
66                                  true,
67                                  ifc:upper())
68                         end
69                 end
70         )
71
72         local page  = node("admin", "network", "dhcp")
73         page.target = cbi("admin_network/dhcp")
74         page.title  = "DHCP"
75         page.order  = 30
76         page.subindex = true
77
78         entry(
79          {"admin", "network", "dhcp", "leases"},
80          cbi("admin_network/dhcpleases"),
81          i18n("dhcp_leases")
82         )
83
84         local page  = node("admin", "network", "hosts")
85         page.target = cbi("admin_network/hosts")
86         page.title  = i18n("hostnames", "Hostnames")
87         page.order  = 40
88
89         local page  = node("admin", "network", "routes")
90         page.target = cbi("admin_network/routes")
91         page.title  = i18n("a_n_routes_static")
92         page.order  = 50
93
94 end
95
96 function wifi_join()
97         local function param(x)
98                 return luci.http.formvalue(x)
99         end
100
101         local function ptable(x)
102                 x = param(x)
103                 return x and (type(x) ~= "table" and { x } or x) or {}
104         end
105
106         local dev  = param("device")
107         local ssid = param("join")
108
109         if dev and ssid then
110                 local wep     = (tonumber(param("wep")) == 1)
111                 local wpa     = tonumber(param("wpa_version")) or 0
112                 local channel = tonumber(param("channel"))
113                 local mode    = param("mode")
114                 local bssid   = param("bssid")
115
116                 local confirm = (param("confirm") == "1")
117                 local cancel  = param("cancel") and true or false
118
119                 if confirm and not cancel then
120                         local fixed_bssid = (param("fixed_bssid") == "1")
121                         local replace_net = (param("replace_net") == "1")
122                         local autoconnect = (param("autoconnect") == "1")
123                         local attach_intf = param("attach_intf")
124
125                         local uci = require "luci.model.uci".cursor()
126
127                         if replace_net then
128                                 uci:delete_all("wireless", "wifi-iface")
129                         end
130
131                         local wificonf = {
132                                 device  = dev,
133                                 mode    = (mode == "Ad-Hoc" and "adhoc" or "sta"),
134                                 ssid    = ssid
135                         }
136
137                         if attach_intf and uci:get("network", attach_intf) == "interface" then
138                                 -- target network already has a interface, make it a bridge
139                                 uci:set("network", attach_intf, "type", "bridge")
140                                 uci:save("network")
141                                 uci:commit("network")
142
143                                 wificonf.network = attach_intf
144
145                                 if autoconnect then
146                                         require "luci.sys".call("/sbin/ifup " .. attach_intf)
147                                 end
148                         end
149
150                         if fixed_bssid then
151                                 wificonf.bssid = bssid
152                         end
153
154                         if wep then
155                                 wificonf.encryption = "wep"
156                                 wificonf.key = param("key")
157                         elseif wpa > 0 then
158                                 wificonf.encryption = param("wpa_suite")
159                                 wificonf.key = param("key")
160                         end
161
162                         local s = uci:section("wireless", "wifi-iface", nil, wificonf)
163                         uci:delete("wireless", dev, "disabled")
164                         uci:set("wireless", dev, "channel", channel)
165
166                         uci:save("wireless")
167                         uci:commit("wireless")
168
169                         if autoconnect then
170                                 require "luci.sys".call("/sbin/wifi")
171                         end
172
173                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
174                 elseif cancel then
175                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
176                 else
177                         luci.template.render("admin_network/wifi_join_settings")
178                 end
179         else
180                 luci.template.render("admin_network/wifi_join")
181         end
182 end
183
184 function wifi_delete(network)
185         local uci = require "luci.model.uci".cursor()
186         local wlm = require "luci.model.wireless"
187
188         wlm.init(uci)
189         wlm:del_network(network)
190
191         uci:save("wireless")
192         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
193 end