1 -- Copyright 2017-2019 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
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"}
13 m = SimpleForm("add", translate("Add Wireless Uplink Configuration"))
14 m.submit = translate("Save")
15 m.cancel = translate("Back to overview")
18 function m.on_cancel()
19 http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
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
32 if m.hidden.wpa_version == 4 then
33 if string.find(m.hidden.description, "OWE") then
34 m.hidden.wpa_version = 5
38 if m.hidden.ssid == "" then
39 wssid = m:field(Value, "ssid", translate("SSID (hidden)"))
41 wssid = m:field(Value, "ssid", translate("SSID"))
43 wssid.datatype = "rangelength(1,32)"
44 wssid.default = m.hidden.ssid or ""
46 nobssid = m:field(Flag, "no_bssid", translate("Ignore BSSID"))
47 if m.hidden.ssid == "" then
48 nobssid.default = nobssid.disabled
50 nobssid.default = nobssid.enabled
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 ""
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"
67 wkey = m:field(Value, "key", translate("WEP-Passphrase"))
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"
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)"))
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"
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"
105 ident = m:field(Value, "identity", translate("Identity"))
107 wkey = m:field(Value, "password", translate("Password"))
109 wkey.datatype = "wpakey"
111 cacert = m:field(Value, "ca_cert", translate("Path to CA-Certificate"))
112 cacert.rmempty = true
114 clientcert = m:field(Value, "client_cert", translate("Path to Client-Certificate"))
115 clientcert:depends("eap_type","tls")
116 clientcert.rmempty = true
118 privkey = m:field(Value, "priv_key", translate("Path to Private Key"))
119 privkey:depends("eap_type","tls")
120 privkey.rmempty = true
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
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"
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"
147 wkey = m:field(Value, "key", translate("WPA-Passphrase"))
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")
158 local login_section = (m.hidden.device or "") .. "_" .. (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
168 cmd_args:depends("cmdlist", z)
170 cmd_list:value("none")
171 cmd_list.default = cmd or "none"
172 cmd_args.default = cmd_args_default
174 function wssid.write(self, section, value)
175 login_section = (m.hidden.device or "") .. "_" .. (wssid:formvalue(section) or "") .. "_" .. (bssid:formvalue(section) or "")
176 login_section = login_section:gsub("[^%w_]", "_")
177 newsection = uci:section("wireless", "wifi-iface", login_section, {
180 device = m.hidden.device,
181 ssid = wssid:formvalue(section),
182 bssid = bssid:formvalue(section),
187 if string.find(encr:formvalue(section), '^wep') then
188 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
189 uci:set("wireless", newsection, "key", wkey:formvalue(section) or "")
190 elseif string.find(encr:formvalue(section), '^wpa') then
191 uci:set("wireless", newsection, "eap_type", eaptype:formvalue(section))
192 uci:set("wireless", newsection, "auth", authentication:formvalue(section))
193 uci:set("wireless", newsection, "identity", ident:formvalue(section) or "")
194 uci:set("wireless", newsection, "password", wkey:formvalue(section) or "")
195 uci:set("wireless", newsection, "ca_cert", cacert:formvalue(section) or "")
196 uci:set("wireless", newsection, "client_cert", clientcert:formvalue(section) or "")
197 uci:set("wireless", newsection, "priv_key", privkey:formvalue(section) or "")
198 uci:set("wireless", newsection, "priv_key_pwd", privkeypwd:formvalue(section) or "")
199 elseif encr:formvalue(section) ~= "owe" then
200 uci:set("wireless", newsection, "key", wkey:formvalue(section) or "")
202 if ciph and ciph:formvalue(section) and ciph:formvalue(section) ~= "auto" then
203 uci:set("wireless", newsection, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
205 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
208 uci:set("wireless", newsection, "encryption", "none")
211 if not uci:get("travelmate", login_section) and cmd_list:formvalue(section) ~= "none" then
212 uci:set("travelmate", login_section, "login")
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")
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"))