luci-app-travelmate: bugfixes
[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 bssid = m:field(Value, "bssid", translate("BSSID"),
38         translatef("The BSSID information '%s' is optional and only required for hidden networks", m.hidden.bssid or ""))
39 bssid.datatype = "macaddr"
40 if m.hidden.ssid == "" then
41         bssid.default = m.hidden.bssid or ""
42 else
43         bssid.default = ""
44 end
45
46 if (tonumber(m.hidden.wep) or 0) == 1 then
47         encr = m:field(ListValue, "encryption", translate("Encryption"))
48         encr:value("wep", "WEP")
49         encr:value("wep+open", "WEP Open System")
50         encr:value("wep+mixed", "WEP mixed")
51         encr:value("wep+shared", "WEP Shared Key")
52         encr.default = "wep+open"
53
54         wkey = m:field(Value, "key", translate("WEP-Passphrase"))
55         wkey.password = true
56         wkey.datatype = "wepkey"
57 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
58         if m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2" then
59                 encr = m:field(ListValue, "encryption", translate("Encryption"))
60                 encr:value("psk", "WPA PSK")
61                 encr:value("psk-mixed", "WPA/WPA2 mixed")
62                 encr:value("psk2", "WPA2 PSK")
63                 encr.default = encr_psk[tonumber(m.hidden.wpa_version)] or "psk2"
64
65                 ciph = m:field(ListValue, "cipher", translate("Cipher"))
66                 ciph:value("auto", translate("Automatic"))
67                 ciph:value("ccmp", translate("Force CCMP (AES)"))
68                 ciph:value("tkip", translate("Force TKIP"))
69                 ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
70                 ciph.default = "auto"
71
72                 wkey = m:field(Value, "key", translate("WPA-Passphrase"))
73                 wkey.password = true
74                 wkey.datatype = "wpakey"
75         elseif m.hidden.wpa_suites == "802.1X" then
76                 encr = m:field(ListValue, "encryption", translate("Encryption"))
77                 encr:value("wpa", "WPA Enterprise")
78                 encr:value("wpa-mixed", "WPA/WPA2 Enterprise mixed")
79                 encr:value("wpa2", "WPA2 Enterprise")
80                 encr.default = encr_wpa[tonumber(m.hidden.wpa_version)] or "wpa2"
81
82                 ciph = m:field(ListValue, "cipher", translate("Cipher"))
83                 ciph:value("auto", translate("Automatic"))
84                 ciph:value("ccmp", translate("Force CCMP (AES)"))
85                 ciph:value("tkip", translate("Force TKIP"))
86                 ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
87                 ciph.default = "auto"
88
89                 eaptype = m:field(ListValue, "eap_type", translate("EAP-Method"))
90                 eaptype:value("tls", "TLS")
91                 eaptype:value("ttls", "TTLS")
92                 eaptype:value("peap", "PEAP")
93                 eaptype:value("fast", "FAST")
94                 eaptype.default = "peap"
95
96                 authentication = m:field(ListValue, "auth", translate("Authentication"))
97                 authentication:value("PAP")
98                 authentication:value("CHAP")
99                 authentication:value("MSCHAP")
100                 authentication:value("MSCHAPV2")
101                 authentication:value("EAP-GTC")
102                 authentication:value("EAP-MD5")
103                 authentication:value("EAP-MSCHAPV2")
104                 authentication:value("EAP-TLS")
105                 authentication:value("auth=PAP")
106                 authentication:value("auth=MSCHAPV2")
107                 authentication.default = "EAP-MSCHAPV2"
108
109                 ident = m:field(Value, "identity", translate("Identity"))
110
111                 wkey = m:field(Value, "password", translate("Password"))
112                 wkey.password = true
113                 wkey.datatype = "wpakey"
114
115                 cacert = m:field(Value, "ca_cert", translate("Path to CA-Certificate"))
116                 cacert.rmempty = true
117
118                 clientcert = m:field(Value, "client_cert", translate("Path to Client-Certificate"))
119                 clientcert:depends("eap_type","tls")
120                 clientcert.rmempty = true
121
122                 privkey = m:field(Value, "priv_key", translate("Path to Private Key"))
123                 privkey:depends("eap_type","tls")
124                 privkey.rmempty = true
125
126                 privkeypwd = m:field(Value, "priv_key_pwd", translate("Password of Private Key"))
127                 privkeypwd:depends("eap_type","tls")
128                 privkeypwd.datatype = "wpakey"
129                 privkeypwd.password = true
130                 privkeypwd.rmempty = true
131         end
132 end
133
134 function wssid.write(self, section, value)
135         newsection = uci:section("wireless", "wifi-iface", nil, {
136                 mode     = "sta",
137                 network  = trmiface,
138                 device   = m.hidden.device,
139                 ssid     = wssid:formvalue(section),
140                 bssid    = bssid:formvalue(section),
141                 disabled = "1"
142         })
143
144         if (tonumber(m.hidden.wep) or 0) == 1 then
145                 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
146                 uci:set("wireless", newsection, "key", wkey:formvalue(section) or "")
147         elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
148                 if m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2" then
149                         if ciph:formvalue(section) ~= "auto" then
150                                 uci:set("wireless", newsection, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
151                         else
152                                 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
153                         end
154                         uci:set("wireless", newsection, "key", wkey:formvalue(section) or "")
155                 elseif m.hidden.wpa_suites == "802.1X" then
156                         if ciph:formvalue(section) ~= "auto" then
157                                 uci:set("wireless", newsection, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
158                         else
159                                 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
160                         end
161                         uci:set("wireless", newsection, "eap_type", eaptype:formvalue(section))
162                         uci:set("wireless", newsection, "auth", authentication:formvalue(section))
163                         uci:set("wireless", newsection, "identity", ident:formvalue(section) or "")
164                         uci:set("wireless", newsection, "password", wkey:formvalue(section) or "")
165                         uci:set("wireless", newsection, "ca_cert", cacert:formvalue(section) or "")
166                         uci:set("wireless", newsection, "client_cert", clientcert:formvalue(section) or "")
167                         uci:set("wireless", newsection, "priv_key", privkey:formvalue(section) or "")
168                         uci:set("wireless", newsection, "priv_key_pwd", privkeypwd:formvalue(section) or "")
169                 end
170         else
171                 uci:set("wireless", newsection, "encryption", "none")
172         end
173         uci:save("wireless")
174         uci:commit("wireless")
175         luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>&1")
176         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
177 end
178
179 return m