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