modules/admin-full: rewrite wifi join wizzard
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / wifi_add.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2009 Jo-Philipp Wich <xm@subsignal.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 local nw   = require "luci.model.network"
16 local fw   = require "luci.model.firewall"
17 local wl   = require "luci.model.wireless"
18 local uci  = require "luci.model.uci".cursor()
19 local http = require "luci.http"
20
21 local iw = luci.sys.wifi.getiwinfo(http.formvalue("device"))
22
23 m = SimpleForm("network", translate("Join Network: Settings"))
24 m.cancel = translate("Back to scan results")
25 m.reset = false
26
27 function m.on_cancel()
28         local dev = http.formvalue("device")
29         http.redirect(luci.dispatcher.build_url(
30                 dev and "admin/network/wireless_join?device=" .. dev
31                         or "admin/network/wireless"
32         ))
33 end
34
35 nw.init(uci)
36 fw.init(uci)
37 wl.init(uci)
38
39 m.hidden = {
40         device      = http.formvalue("device"),
41         join        = http.formvalue("join"),
42         channel     = http.formvalue("channel"),
43         mode        = http.formvalue("mode"),
44         bssid       = http.formvalue("bssid"),
45         wep         = http.formvalue("wep"),
46         wpa_suites      = http.formvalue("wpa_suites"),
47         wpa_version = http.formvalue("wpa_version")
48 }
49
50 if iw and iw.mbssid_support then
51         replace = m:field(Flag, "replace", translate("Replace wireless configuration"),
52                 translate("An additional network will be created if you leave this unchecked."))
53 else
54         replace = m:field(DummyValue, "replace", translate("Replace wireless configuration"))
55         replace.default = translate("The hardware is not multi-SSID capable and existing " ..
56                 "configuration will be replaced if you proceed.")
57
58         function replace.formvalue() return "1" end
59 end
60
61 if http.formvalue("wep") == "1" then
62         key = m:field(Value, "key", translate("WEP passphrase"),
63                 translate("Specify the secret encryption key here."))
64
65         key.password = true
66
67 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and m.hidden.wpa_suites == "PSK" then
68         key = m:field(Value, "key", translate("WPA passphrase"),
69                 translate("Specify the secret encryption key here."))
70
71         key.password = true
72         --m.hidden.wpa_suite = (tonumber(http.formvalue("wpa_version")) or 0) >= 2 and "psk2" or "psk"
73 end
74
75 attachnet = m:field(Flag, "_attach", translate("Attach to existing network"),
76         translate("If the interface is attached to an existing network it will be <em>bridged</em> " ..
77                 "to the existing interfaces and is covered by the firewall zone of the choosen network. " ..
78                 "Uncheck this option to define a new standalone network."
79         ))
80
81 attachnet.rmempty = false
82 attachnet.default = http.formvalue("cbi.submit") and nil or "1"
83
84 function attachnet.formvalue(self, section)
85         if not http.formvalue("cbi.submit") then
86                 return m.hidden.mode == "Ad-Hoc" and "0" or "1"
87         else
88                 return Value.formvalue(self, section) and "1" or "0"
89         end
90 end
91
92 attachnet.cfgvalue = attachnet.formvalue
93
94 newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
95         translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
96                 "<code>0-9</code> and <code>_</code>"
97         ))
98
99 newnet:depends("_attach", "")
100 newnet.default = m.hidden.mode == "Ad-Hoc" and "mesh"
101
102 addnet = m:field(Value, "_netname_attach",
103         translate("Network to attach interface to"))
104
105 addnet.template = "cbi/network_netlist"
106 addnet.widget = "radio"
107 addnet.default = "wan"
108 addnet.nocreate = true
109 addnet:depends("_attach", "1")
110
111 fwzone = m:field(Value, "_fwzone",
112         translate("Create / Assign firewall-zone"),
113         translate("Choose the firewall zone you want to assign to this interface. Select <em>unspecified</em> to remove the interface from the associated zone or fill out the <em>create</em> field to define a new zone and attach the interface to it."))
114
115 fwzone.template = "cbi/firewall_zonelist"
116 fwzone:depends("_attach", "")
117 fwzone.default = m.hidden.mode == "Ad-Hoc" and "mesh"
118
119 function attachnet.parse(self, section)
120         Flag.parse(self, section)
121
122         if http.formvalue("cbi.submit") then
123                 local net, zone
124                 local value = self:formvalue(section)
125
126                 if value == "1" then
127                         net = nw:get_network(addnet:formvalue(section))
128                         if net then
129                                 net:type("bridge")
130                         end
131                 else
132                         local zval = fwzone:formvalue(section)
133
134                         net = nw:add_network(newnet:formvalue(section), { proto = "dhcp" })
135                         zone = fw:get_zone(zval)
136
137                         if not zone and zval == '-' then
138                                 zval = m:formvalue(fwzone:cbid(section) .. ".newzone")
139                                 if zval and #zval > 0 then
140                                         zone = fw:add_zone(zval)
141                                 end
142                         end
143                 end
144
145                 if not net then
146                         self.error = { [section] = "missing" }
147                 else
148                         local wdev = wl:get_device(m.hidden.device)
149                         wdev:disabled(false)
150                         wdev:channel(m.hidden.channel)
151
152                         if replace:formvalue(section) then
153                                 local n
154                                 for _, n in ipairs(wdev:get_networks()) do
155                                         wl:del_network(n:name())
156                                 end
157                         end
158
159                         local wconf = {
160                                 device  = m.hidden.device,
161                                 ssid    = m.hidden.join,
162                                 mode    = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta"),
163                                 network = net:name()
164                         }
165
166                         if m.hidden.wep == "1" then
167                                 wconf.encryption = "wep"
168                                 wconf.key        = key and key:formvalue(section) or ""
169                         elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
170                                 wconf.encryption = (tonumber(m.hidden.wpa_version) or 0) >= 2 and "psk2" or "psk"
171                                 wconf.key        = key and key:formvalue(section) or ""
172                         else
173                                 wconf.encryption = "none"
174                         end
175
176                         if wconf.mode == "adhoc" then
177                                 wconf.bssid = m.hidden.bssid
178                         end
179
180                         local wnet = wl:add_network(wconf)
181
182                         if wnet then
183                                 if zone then
184                                         fw:del_network(net:name())
185                                         zone:add_network(net:name())
186                                 end
187
188                                 uci:save("wireless")
189                                 uci:save("network")
190                                 uci:save("firewall")
191
192                                 uci:commit("wireless")
193                                 uci:commit("network")
194                                 uci:commit("firewall")
195
196                                 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless",
197                                         wdev:name(), wnet:name()))
198                         end
199                 end
200         end
201 end
202
203 attachnet.remove = attachnet.write
204
205 function fwzone.cfgvalue(self, section)
206         self.iface = section
207         local z = fw:get_zone_by_network(section)
208         return z and z:name()
209 end
210
211 return m