31869478fad7476c9e09b683ba92c9b27e1be274
[project/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / wifi_add.lua
1 -- Copyright 2017 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
9 m = SimpleForm("add", translate("Add Wireless Uplink Configuration"))
10 m.submit = translate("Save")
11 m.cancel = translate("Back to overview")
12 m.reset = false
13
14 function m.on_cancel()
15         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
16 end
17
18 m.hidden = {
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")
25 }
26
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 ""
31 else
32         wssid = m:field(Value, "ssid", translate("SSID (hidden)"))
33 end
34
35 bssid = m:field(Value, "bssid", translate("BSSID"))
36 bssid.datatype = "macaddr"
37 bssid.default = m.hidden.bssid or ""
38
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"
46
47         wkey = m:field(Value, "key", translate("WEP-Passphrase"))
48         wkey.password = true
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")
56                 encr.default = "psk2"
57
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)"))
63                 ciph.default = "auto"
64
65                 wkey = m:field(Value, "key", translate("WPA-Passphrase"))
66                 wkey.password = true
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")
73                 encr.default = "wpa2"
74
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)"))
80                 ciph.default = "auto"
81
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"
88
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"
99
100                 ident = m:field(Value, "identity", translate("Identity"))
101
102                 wkey = m:field(Value, "password", translate("Password"))
103                 wkey.password = true
104                 wkey.datatype = "wpakey"
105
106                 cacert = m:field(Value, "ca_cert", translate("Path to CA-Certificate"))
107                 cacert.rmempty = true
108
109                 clientcert = m:field(Value, "client_cert", translate("Path to Client-Certificate"))
110                 clientcert:depends("eap_type","tls")
111                 clientcert.rmempty = true
112
113                 privkey = m:field(Value, "priv_key", translate("Path to Private Key"))
114                 privkey:depends("eap_type","tls")
115                 privkey.rmempty = true
116
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
122         end
123 end
124
125 function wssid.write(self, section, value)
126         newsection = uci:section("wireless", "wifi-iface", nil, {
127                 mode     = "sta",
128                 network  = trmiface,
129                 device   = m.hidden.device,
130                 ssid     = wssid:formvalue(section),
131                 bssid    = bssid:formvalue(section),
132                 disabled = "1"
133         })
134         
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))
142                         else
143                                 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
144                         end
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))
149                         else
150                                 uci:set("wireless", newsection, "encryption", encr:formvalue(section))
151                         end
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 "")
160                 end
161         else
162                 uci:set("wireless", newsection, "encryption", "none")
163         end
164         uci:save("wireless")
165         uci:commit("wireless")
166         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
167 end
168
169 return m