1 -- Copyright 2017 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 trmiface = uci.get("travelmate", "global", "trm_iface") or "trm_wwan"
9 m = SimpleForm("add", translate("Add Wireless Uplink Configuration"))
10 m.submit = translate("Save")
11 m.cancel = translate("Back to overview")
14 function m.on_cancel()
15 http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
19 device = http.formvalue("device"),
20 ssid = http.formvalue("ssid"),
21 bssid = http.formvalue("bssid"),
22 wep = http.formvalue("wep"),
23 wpa_suites = http.formvalue("wpa_suites"),
24 wpa_version = http.formvalue("wpa_version")
27 if m.hidden.ssid ~= "" then
28 wssid = m:field(Value, "ssid", translate("SSID"))
29 wssid.datatype = "rangelength(1,32)"
30 wssid.default = m.hidden.ssid or ""
32 wssid = m:field(Value, "ssid", translate("SSID (hidden)"))
35 bssid = m:field(Value, "bssid", translate("BSSID"))
36 bssid.datatype = "macaddr"
37 bssid.default = m.hidden.bssid or ""
39 if (tonumber(m.hidden.wep) or 0) == 1 then
40 encr = m:field(ListValue, "encryption", translate("Encryption"))
41 encr:value("wep", "WEP")
42 encr:value("wep+open", "WEP Open System")
43 encr:value("wep+mixed", "WEP mixed")
44 encr:value("wep+shared", "WEP Shared Key")
45 encr.default = "wep+open"
47 wkey = m:field(Value, "key", translate("WEP-Passphrase"))
49 wkey.datatype = "wepkey"
50 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
51 if m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2" then
52 encr = m:field(ListValue, "encryption", translate("Encryption"))
53 encr:value("psk", "WPA PSK")
54 encr:value("psk-mixed", "WPA/WPA2 mixed")
55 encr:value("psk2", "WPA2 PSK")
58 ciph = m:field(ListValue, "cipher", translate("Cipher"))
59 ciph:value("auto", translate("Automatic"))
60 ciph:value("ccmp", translate("Force CCMP (AES)"))
61 ciph:value("tkip", translate("Force TKIP"))
62 ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
65 wkey = m:field(Value, "key", translate("WPA-Passphrase"))
67 wkey.datatype = "wpakey"
68 elseif m.hidden.wpa_suites == "802.1X" then
69 encr = m:field(ListValue, "encryption", translate("Encryption"))
70 encr:value("wpa", "WPA Enterprise")
71 encr:value("wpa-mixed", "WPA/WPA2 Enterprise mixed")
72 encr:value("wpa2", "WPA2 Enterprise")
75 ciph = m:field(ListValue, "cipher", translate("Cipher"))
76 ciph:value("auto", translate("Automatic"))
77 ciph:value("ccmp", translate("Force CCMP (AES)"))
78 ciph:value("tkip", translate("Force TKIP"))
79 ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
82 eaptype = m:field(ListValue, "eap_type", translate("EAP-Method"))
83 eaptype:value("tls", "TLS")
84 eaptype:value("ttls", "TTLS")
85 eaptype:value("peap", "PEAP")
86 eaptype:value("fast", "FAST")
87 eaptype.default = "peap"
89 authentication = m:field(ListValue, "auth", translate("Authentication"))
90 authentication:value("PAP")
91 authentication:value("CHAP")
92 authentication:value("MSCHAP")
93 authentication:value("MSCHAPV2")
94 authentication:value("EAP-GTC")
95 authentication:value("EAP-MD5")
96 authentication:value("EAP-MSCHAPV2")
97 authentication:value("EAP-TLS")
98 authentication.default = "EAP-MSCHAPV2"
100 ident = m:field(Value, "identity", translate("Identity"))
102 wkey = m:field(Value, "password", translate("Password"))
104 wkey.datatype = "wpakey"
106 cacert = m:field(Value, "ca_cert", translate("Path to CA-Certificate"))
107 cacert.rmempty = true
109 clientcert = m:field(Value, "client_cert", translate("Path to Client-Certificate"))
110 clientcert:depends("eap_type","tls")
111 clientcert.rmempty = true
113 privkey = m:field(Value, "priv_key", translate("Path to Private Key"))
114 privkey:depends("eap_type","tls")
115 privkey.rmempty = true
117 privkeypwd = m:field(Value, "priv_key_pwd", translate("Password of Private Key"))
118 privkeypwd:depends("eap_type","tls")
119 privkeypwd.datatype = "wpakey"
120 privkeypwd.password = true
121 privkeypwd.rmempty = true
125 function wssid.write(self, section, value)
126 newsection = uci:section("wireless", "wifi-iface", nil, {
129 device = m.hidden.device,
130 ssid = wssid:formvalue(section),
131 bssid = bssid:formvalue(section),
135 if (tonumber(m.hidden.wep) or 0) == 1 then
136 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
137 uci:set("wireless", newsection, "key", wkey:formvalue(section) or "")
138 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
139 if m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2" then
140 if ciph:formvalue(section) ~= "auto" then
141 uci:set("wireless", newsection, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
143 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
145 uci:set("wireless", newsection, "key", wkey:formvalue(section) or "")
146 elseif m.hidden.wpa_suites == "802.1X" then
147 if ciph:formvalue(section) ~= "auto" then
148 uci:set("wireless", newsection, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
150 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
152 uci:set("wireless", newsection, "eap_type", eaptype:formvalue(section))
153 uci:set("wireless", newsection, "auth", authentication:formvalue(section))
154 uci:set("wireless", newsection, "identity", ident:formvalue(section) or "")
155 uci:set("wireless", newsection, "password", wkey:formvalue(section) or "")
156 uci:set("wireless", newsection, "ca_cert", cacert:formvalue(section) or "")
157 uci:set("wireless", newsection, "client_cert", clientcert:formvalue(section) or "")
158 uci:set("wireless", newsection, "priv_key", privkey:formvalue(section) or "")
159 uci:set("wireless", newsection, "priv_key_pwd", privkeypwd:formvalue(section) or "")
162 uci:set("wireless", newsection, "encryption", "none")
165 uci:commit("wireless")
166 http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))