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