luci-app-travelmate: sync with update 1.5.0
[oweals/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / wifi_add.lua
1 -- Copyright 2017-2019 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 util     = require("luci.util")
8 local scripts  = util.split(util.trim(util.exec("ls /etc/travelmate/*.login 2>/dev/null")), "\n", nil, true) or {}
9 local trmiface = uci:get("travelmate", "global", "trm_iface") or "trm_wwan"
10 local encr_psk = {"psk", "psk2", "psk-mixed", "sae", "owe", "sae-mixed"}
11 local encr_wpa = {"wpa", "wpa2", "wpa-mixed"}
12
13 m = SimpleForm("add", translate("Add Wireless Uplink Configuration"))
14 m.submit = translate("Save")
15 m.cancel = translate("Back to overview")
16 m.reset = false
17
18 function m.on_cancel()
19         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
20 end
21
22 m.hidden = {
23         device      = http.formvalue("device"),
24         ssid        = http.formvalue("ssid"),
25         bssid       = http.formvalue("bssid"),
26         description = http.formvalue("description"),
27         wep         = tonumber(http.formvalue("wep")) or 0,
28         wpa_suites  = http.formvalue("wpa_suites"),
29         wpa_version = tonumber(http.formvalue("wpa_version")) or 0
30 }
31
32 if m.hidden.wpa_version == 4 then
33         if string.find(m.hidden.description, "OWE") then
34                 m.hidden.wpa_version = 5
35         end
36 end
37
38 if m.hidden.ssid == "" then
39         wssid = m:field(Value, "ssid", translate("SSID (hidden)"))
40 else
41         wssid = m:field(Value, "ssid", translate("SSID"))
42 end
43 wssid.datatype = "rangelength(1,32)"
44 wssid.default = m.hidden.ssid or ""
45
46 nobssid = m:field(Flag, "no_bssid", translate("Ignore BSSID"))
47 if m.hidden.ssid == "" then
48         nobssid.default = nobssid.disabled
49 else
50         nobssid.default = nobssid.enabled
51 end
52
53 bssid = m:field(Value, "bssid", translate("BSSID"),
54         translatef("The BSSID information '%s' is optional and only required for hidden networks", m.hidden.bssid or ""))
55 bssid:depends("no_bssid", 0)
56 bssid.datatype = "macaddr"
57 bssid.default = m.hidden.bssid or ""
58
59 if m.hidden.wep == 1 then
60         encr = m:field(ListValue, "encryption", translate("Encryption"))
61         encr:value("wep", "WEP")
62         encr:value("wep+open", "WEP Open System")
63         encr:value("wep+mixed", "WEP mixed")
64         encr:value("wep+shared", "WEP Shared Key")
65         encr.default = "wep+open"
66
67         wkey = m:field(Value, "key", translate("WEP-Passphrase"))
68         wkey.password = true
69         wkey.datatype = "wepkey"
70 elseif m.hidden.wpa_version > 0 then
71         if m.hidden.wpa_suites == "802.1X" then
72                 encr = m:field(ListValue, "encryption", translate("Encryption"))
73                 encr:value("wpa", "WPA Enterprise")
74                 encr:value("wpa-mixed", "WPA/WPA2 Enterprise mixed")
75                 encr:value("wpa2", "WPA2 Enterprise")
76                 encr.default = encr_wpa[m.hidden.wpa_version] or "wpa2"
77
78                 ciph = m:field(ListValue, "cipher", translate("Cipher"))
79                 ciph:value("auto", translate("Automatic"))
80                 ciph:value("ccmp", translate("Force CCMP (AES)"))
81                 ciph:value("tkip", translate("Force TKIP"))
82                 ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
83                 ciph.default = "auto"
84
85                 eaptype = m:field(ListValue, "eap_type", translate("EAP-Method"))
86                 eaptype:value("tls", "TLS")
87                 eaptype:value("ttls", "TTLS")
88                 eaptype:value("peap", "PEAP")
89                 eaptype:value("fast", "FAST")
90                 eaptype.default = "peap"
91
92                 authentication = m:field(ListValue, "auth", translate("Authentication"))
93                 authentication:value("PAP")
94                 authentication:value("CHAP")
95                 authentication:value("MSCHAP")
96                 authentication:value("MSCHAPV2")
97                 authentication:value("EAP-GTC")
98                 authentication:value("EAP-MD5")
99                 authentication:value("EAP-MSCHAPV2")
100                 authentication:value("EAP-TLS")
101                 authentication:value("auth=PAP")
102                 authentication:value("auth=MSCHAPV2")
103                 authentication.default = "EAP-MSCHAPV2"
104
105                 ident = m:field(Value, "identity", translate("Identity"))
106
107                 wkey = m:field(Value, "password", translate("Password"))
108                 wkey.password = true
109                 wkey.datatype = "wpakey"
110
111                 cacert = m:field(Value, "ca_cert", translate("Path to CA-Certificate"))
112                 cacert.rmempty = true
113
114                 clientcert = m:field(Value, "client_cert", translate("Path to Client-Certificate"))
115                 clientcert:depends("eap_type","tls")
116                 clientcert.rmempty = true
117
118                 privkey = m:field(Value, "priv_key", translate("Path to Private Key"))
119                 privkey:depends("eap_type","tls")
120                 privkey.rmempty = true
121
122                 privkeypwd = m:field(Value, "priv_key_pwd", translate("Password of Private Key"))
123                 privkeypwd:depends("eap_type","tls")
124                 privkeypwd.datatype = "wpakey"
125                 privkeypwd.password = true
126                 privkeypwd.rmempty = true
127         else
128                 encr = m:field(ListValue, "encryption", translate("Encryption"))
129                 encr:value("psk", "WPA-PSK")
130                 encr:value("psk2", "WPA2-PSK")
131                 encr:value("psk-mixed", "WPA/WPA2 mixed")
132                 encr:value("sae", "WPA3-SAE")
133                 encr:value("owe", "OWE (open network)")
134                 encr:value("sae-mixed", "WPA2/WPA3 mixed")
135                 encr.default = encr_psk[m.hidden.wpa_version] or "psk2"
136
137                 ciph = m:field(ListValue, "cipher", translate("Cipher"))
138                 ciph:value("auto", translate("Automatic"))
139                 ciph:value("ccmp", translate("Force CCMP (AES)"))
140                 ciph:value("tkip", translate("Force TKIP"))
141                 ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
142                 ciph:depends("encryption", "psk")
143                 ciph:depends("encryption", "psk2")
144                 ciph:depends("encryption", "psk-mixed")
145                 ciph.default = "auto"
146
147                 wkey = m:field(Value, "key", translate("WPA-Passphrase"))
148                 wkey.password = true
149                 wkey.datatype = "wpakey"
150                 wkey:depends("encryption", "psk")
151                 wkey:depends("encryption", "psk2")
152                 wkey:depends("encryption", "psk-mixed")
153                 wkey:depends("encryption", "sae")
154                 wkey:depends("encryption", "sae-mixed")
155         end
156 end
157
158 local login_section = (m.hidden.ssid or "") .. (m.hidden.bssid or "")
159 login_section = login_section:gsub("[^%w_]", "_")
160 local cmd = uci:get("travelmate", login_section, "command")
161 local cmd_args_default = uci:get("travelmate", login_section, "command_args")
162 cmd_list = m:field(ListValue, "cmdlist", translate("Auto Login Script"),
163         translate("External script reference which will be called for automated captive portal logins."))
164 cmd_args = m:field(Value, "cmdargs", translate("Optional Arguments"),
165         translate("Space separated list of additional arguments passed to the Auto Login Script, i.e. username and password"))
166 for _, z in ipairs(scripts) do
167         cmd_list:value(z)
168         cmd_args:depends("cmdlist", z)
169 end
170 cmd_list:value("none")
171 cmd_list.default = cmd or "none"
172 cmd_args.default = cmd_args_default
173
174 function wssid.write(self, section, value)
175         newsection = uci:section("wireless", "wifi-iface", "trm_" .. login_section, {
176                 mode     = "sta",
177                 network  = trmiface,
178                 device   = m.hidden.device,
179                 ssid     = wssid:formvalue(section),
180                 bssid    = bssid:formvalue(section),
181                 disabled = "1"
182         })
183
184         if encr:formvalue(section) then
185                 if string.find(encr:formvalue(section), '^wep') then
186                         uci:set("wireless", newsection, "encryption", encr:formvalue(section))
187                         uci:set("wireless", newsection, "key", wkey:formvalue(section) or "")
188                 elseif string.find(encr:formvalue(section), '^wpa') then
189                         uci:set("wireless", newsection, "eap_type", eaptype:formvalue(section))
190                         uci:set("wireless", newsection, "auth", authentication:formvalue(section))
191                         uci:set("wireless", newsection, "identity", ident:formvalue(section) or "")
192                         uci:set("wireless", newsection, "password", wkey:formvalue(section) or "")
193                         uci:set("wireless", newsection, "ca_cert", cacert:formvalue(section) or "")
194                         uci:set("wireless", newsection, "client_cert", clientcert:formvalue(section) or "")
195                         uci:set("wireless", newsection, "priv_key", privkey:formvalue(section) or "")
196                         uci:set("wireless", newsection, "priv_key_pwd", privkeypwd:formvalue(section) or "")
197                 elseif encr:formvalue(section) ~= "owe" then
198                         uci:set("wireless", newsection, "key", wkey:formvalue(section) or "")
199                 end
200                 if ciph:formvalue(section) and ciph:formvalue(section) ~= "auto" then
201                         uci:set("wireless", newsection, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
202                 else
203                         uci:set("wireless", newsection, "encryption", encr:formvalue(section))
204                 end
205         else
206                 uci:set("wireless", newsection, "encryption", "none")
207         end
208
209         local login_section = (wssid:formvalue(section) or "") .. (bssid:formvalue(section) or "")
210         login_section = login_section:gsub("[^%w_]", "_")
211         if not uci:get("travelmate", login_section) and cmd_list:formvalue(section) ~= "none" then
212                 uci:set("travelmate", login_section, "login")
213         end
214         if uci:get("travelmate", login_section) then
215                 uci:set("travelmate", login_section, "command", cmd_list:formvalue(section))
216                 uci:set("travelmate", login_section, "command_args", cmd_args:formvalue(section))
217                 uci:save("travelmate")
218                 uci:commit("travelmate")
219         end
220         uci:save("wireless")
221         uci:commit("wireless")
222         luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>&1")
223         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
224 end
225
226 return m