luci-app-travelmate: bring back cbi element to wifi_add.lua
[project/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / wifi_add.lua
1 -- Copyright 2017-2018 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 local fs       = require("nixio.fs")
5 local uci      = require("luci.model.uci").cursor()
6 local http     = require("luci.http")
7 local trmiface = uci:get("travelmate", "global", "trm_iface") or "trm_wwan"
8 local encr_psk = {"psk", "psk2", "psk-mixed"}
9 local encr_wpa = {"wpa", "wpa2", "wpa-mixed"}
10
11 m = SimpleForm("add", translate("Add Wireless Uplink Configuration"))
12 m.submit = translate("Save")
13 m.cancel = translate("Back to overview")
14 m.reset = false
15
16 function m.on_cancel()
17         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
18 end
19
20 m.hidden = {
21         device      = http.formvalue("device"),
22         ssid        = http.formvalue("ssid"),
23         bssid       = http.formvalue("bssid"),
24         wep         = http.formvalue("wep"),
25         wpa_suites  = http.formvalue("wpa_suites"),
26         wpa_version = http.formvalue("wpa_version")
27 }
28
29 if m.hidden.ssid == "" then
30         wssid = m:field(Value, "ssid", translate("SSID (hidden)"))
31 else
32         wssid = m:field(Value, "ssid", translate("SSID"))
33 end
34 wssid.datatype = "rangelength(1,32)"
35 wssid.default = m.hidden.ssid or ""
36
37 nobssid = m:field(Flag, "no_bssid", translate("Ignore BSSID"))
38 if m.hidden.ssid == "" then
39         nobssid.default = nobssid.disabled
40 else
41         nobssid.default = nobssid.enabled
42 end
43
44 bssid = m:field(Value, "bssid", translate("BSSID"),
45         translatef("The BSSID information '%s' is optional and only required for hidden networks", m.hidden.bssid or ""))
46 bssid:depends("no_bssid", 0)
47 bssid.datatype = "macaddr"
48 bssid.default = m.hidden.bssid or ""
49
50 if (tonumber(m.hidden.wep) or 0) == 1 then
51         encr = m:field(ListValue, "encryption", translate("Encryption"))
52         encr:value("wep", "WEP")
53         encr:value("wep+open", "WEP Open System")
54         encr:value("wep+mixed", "WEP mixed")
55         encr:value("wep+shared", "WEP Shared Key")
56         encr.default = "wep+open"
57
58         wkey = m:field(Value, "key", translate("WEP-Passphrase"))
59         wkey.password = true
60         wkey.datatype = "wepkey"
61 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
62         if m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2" then
63                 encr = m:field(ListValue, "encryption", translate("Encryption"))
64                 encr:value("psk", "WPA PSK")
65                 encr:value("psk-mixed", "WPA/WPA2 mixed")
66                 encr:value("psk2", "WPA2 PSK")
67                 encr.default = encr_psk[tonumber(m.hidden.wpa_version)] or "psk2"
68
69                 ciph = m:field(ListValue, "cipher", translate("Cipher"))
70                 ciph:value("auto", translate("Automatic"))
71                 ciph:value("ccmp", translate("Force CCMP (AES)"))
72                 ciph:value("tkip", translate("Force TKIP"))
73                 ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
74                 ciph.default = "auto"
75
76                 wkey = m:field(Value, "key", translate("WPA-Passphrase"))
77                 wkey.password = true
78                 wkey.datatype = "wpakey"
79         elseif m.hidden.wpa_suites == "802.1X" then
80                 encr = m:field(ListValue, "encryption", translate("Encryption"))
81                 encr:value("wpa", "WPA Enterprise")
82                 encr:value("wpa-mixed", "WPA/WPA2 Enterprise mixed")
83                 encr:value("wpa2", "WPA2 Enterprise")
84                 encr.default = encr_wpa[tonumber(m.hidden.wpa_version)] or "wpa2"
85
86                 ciph = m:field(ListValue, "cipher", translate("Cipher"))
87                 ciph:value("auto", translate("Automatic"))
88                 ciph:value("ccmp", translate("Force CCMP (AES)"))
89                 ciph:value("tkip", translate("Force TKIP"))
90                 ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
91                 ciph.default = "auto"
92
93                 eaptype = m:field(ListValue, "eap_type", translate("EAP-Method"))
94                 eaptype:value("tls", "TLS")
95                 eaptype:value("ttls", "TTLS")
96                 eaptype:value("peap", "PEAP")
97                 eaptype:value("fast", "FAST")
98                 eaptype.default = "peap"
99
100                 authentication = m:field(ListValue, "auth", translate("Authentication"))
101                 authentication:value("PAP")
102                 authentication:value("CHAP")
103                 authentication:value("MSCHAP")
104                 authentication:value("MSCHAPV2")
105                 authentication:value("EAP-GTC")
106                 authentication:value("EAP-MD5")
107                 authentication:value("EAP-MSCHAPV2")
108                 authentication:value("EAP-TLS")
109                 authentication:value("auth=PAP")
110                 authentication:value("auth=MSCHAPV2")
111                 authentication.default = "EAP-MSCHAPV2"
112
113                 ident = m:field(Value, "identity", translate("Identity"))
114
115                 wkey = m:field(Value, "password", translate("Password"))
116                 wkey.password = true
117                 wkey.datatype = "wpakey"
118
119                 cacert = m:field(Value, "ca_cert", translate("Path to CA-Certificate"))
120                 cacert.rmempty = true
121
122                 clientcert = m:field(Value, "client_cert", translate("Path to Client-Certificate"))
123                 clientcert:depends("eap_type","tls")
124                 clientcert.rmempty = true
125
126                 privkey = m:field(Value, "priv_key", translate("Path to Private Key"))
127                 privkey:depends("eap_type","tls")
128                 privkey.rmempty = true
129
130                 privkeypwd = m:field(Value, "priv_key_pwd", translate("Password of Private Key"))
131                 privkeypwd:depends("eap_type","tls")
132                 privkeypwd.datatype = "wpakey"
133                 privkeypwd.password = true
134                 privkeypwd.rmempty = true
135         end
136 end
137
138 function wssid.write(self, section, value)
139         newsection = uci:section("wireless", "wifi-iface", nil, {
140                 mode     = "sta",
141                 network  = trmiface,
142                 device   = m.hidden.device,
143                 ssid     = wssid:formvalue(section),
144                 bssid    = bssid:formvalue(section),
145                 disabled = "1"
146         })
147
148         if (tonumber(m.hidden.wep) or 0) == 1 then
149                 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
150                 uci:set("wireless", newsection, "key", wkey:formvalue(section) or "")
151         elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
152                 if m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2" then
153                         if ciph:formvalue(section) ~= "auto" then
154                                 uci:set("wireless", newsection, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
155                         else
156                                 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
157                         end
158                         uci:set("wireless", newsection, "key", wkey:formvalue(section) or "")
159                 elseif m.hidden.wpa_suites == "802.1X" then
160                         if ciph:formvalue(section) ~= "auto" then
161                                 uci:set("wireless", newsection, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
162                         else
163                                 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
164                         end
165                         uci:set("wireless", newsection, "eap_type", eaptype:formvalue(section))
166                         uci:set("wireless", newsection, "auth", authentication:formvalue(section))
167                         uci:set("wireless", newsection, "identity", ident:formvalue(section) or "")
168                         uci:set("wireless", newsection, "password", wkey:formvalue(section) or "")
169                         uci:set("wireless", newsection, "ca_cert", cacert:formvalue(section) or "")
170                         uci:set("wireless", newsection, "client_cert", clientcert:formvalue(section) or "")
171                         uci:set("wireless", newsection, "priv_key", privkey:formvalue(section) or "")
172                         uci:set("wireless", newsection, "priv_key_pwd", privkeypwd:formvalue(section) or "")
173                 end
174         else
175                 uci:set("wireless", newsection, "encryption", "none")
176         end
177         uci:save("wireless")
178         uci:commit("wireless")
179         luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>&1")
180         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
181 end
182
183 return m