b8e0f11b3a631be47beefa7b13317db133652d2a
[project/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / wifi_edit.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
8 m = SimpleForm("edit", translate("Edit Wireless Uplink Configuration"))
9 m.submit = translate("Save")
10 m.cancel = translate("Back to overview")
11 m.reset = false
12
13 function m.on_cancel()
14 http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
15 end
16
17 m.hidden = {
18 cfg = http.formvalue("cfg")
19 }
20
21 local s = uci:get_all("wireless", m.hidden.cfg)
22 if s ~= nil then
23 wssid = m:field(Value, "ssid", translate("SSID"))
24 wssid.datatype = "rangelength(1,32)"
25 wssid.default = s.ssid or ""
26
27 bssid = m:field(Value, "bssid", translate("BSSID"))
28 bssid.datatype = "macaddr"
29 bssid.default = s.bssid or ""
30
31 s.cipher = "auto"
32 if string.match(s.encryption, '\+') and not string.match(s.encryption, '^wep') then
33 s.pos = string.find(s.encryption, '\+')
34 s.cipher = string.sub(s.encryption, s.pos + 1)
35 s.encryption = string.sub(s.encryption, 0, s.pos - 1)
36 end
37
38 if s.encryption and s.encryption ~= "none" then
39 if string.match(s.encryption, '^wep') 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 = s.encryption
46
47 wkey = m:field(Value, "key", translate("Passphrase"))
48 wkey.datatype = "wepkey"
49 elseif string.match(s.encryption, '^psk') then
50 encr = m:field(ListValue, "encryption", translate("Encryption"))
51 encr:value("psk", "WPA PSK")
52 encr:value("psk-mixed", "WPA/WPA2 mixed")
53 encr:value("psk2", "WPA2 PSK")
54 encr.default = s.encryption
55
56 ciph = m:field(ListValue, "cipher", translate("Cipher"))
57 ciph:value("auto", translate("Automatic"))
58 ciph:value("ccmp", translate("Force CCMP (AES)"))
59 ciph:value("tkip", translate("Force TKIP"))
60 ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
61 ciph.default = s.cipher
62
63 wkey = m:field(Value, "key", translate("Passphrase"))
64 wkey.datatype = "wpakey"
65 elseif string.match(s.encryption, '^wpa') then
66 encr = m:field(ListValue, "encryption", translate("Encryption"))
67 encr:value("wpa", "WPA Enterprise")
68 encr:value("wpa-mixed", "WPA/WPA2 Enterprise mixed")
69 encr:value("wpa2", "WPA2 Enterprise")
70 encr.default = s.encryption
71
72 ciph = m:field(ListValue, "cipher", translate("Cipher"))
73 ciph:value("auto", translate("Automatic"))
74 ciph:value("ccmp", translate("Force CCMP (AES)"))
75 ciph:value("tkip", translate("Force TKIP"))
76 ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
77 ciph.default = s.cipher
78
79 eaptype = m:field(ListValue, "eap_type", translate("EAP-Method"))
80 eaptype:value("tls", "TLS")
81 eaptype:value("ttls", "TTLS")
82 eaptype:value("peap", "PEAP")
83 eaptype:value("fast", "FAST")
84 eaptype.default = s.eap_type or "peap"
85
86 authentication = m:field(ListValue, "auth", translate("Authentication"))
87 authentication:value("PAP")
88 authentication:value("CHAP")
89 authentication:value("MSCHAP")
90 authentication:value("MSCHAPV2")
91 authentication:value("EAP-GTC")
92 authentication:value("EAP-MD5")
93 authentication:value("EAP-MSCHAPV2")
94 authentication:value("EAP-TLS")
95 authentication.default = s.auth or "EAP-MSCHAPV2"
96
97 ident = m:field(Value, "identity", translate("Identity"))
98 ident.default = s.identity or ""
99
100 wkey = m:field(Value, "password", translate("Passphrase"))
101 wkey.datatype = "wpakey"
102
103 cacert = m:field(Value, "ca_cert", translate("Path to CA-Certificate"))
104 cacert.rmempty = true
105 cacert.default = s.ca_cert or ""
106
107 clientcert = m:field(Value, "client_cert", translate("Path to Client-Certificate"))
108 clientcert:depends("eap_type","tls")
109 clientcert.rmempty = true
110 clientcert.default = s.client_cert or ""
111
112 privkey = m:field(Value, "priv_key", translate("Path to Private Key"))
113 privkey:depends("eap_type","tls")
114 privkey.rmempty = true
115 privkey.default = s.priv_key or ""
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 privkeypwd.default = s.priv_key_pwd or ""
123 end
124 wkey.password = true
125 wkey.default = s.key or s.password
126 end
127 else
128 m.on_cancel()
129 end
130
131 function wssid.write(self, section, value)
132 uci:set("wireless", m.hidden.cfg, "ssid", wssid:formvalue(section))
133 uci:set("wireless", m.hidden.cfg, "bssid", bssid:formvalue(section))
134 if s.encryption and s.encryption ~= "none" then
135 if string.match(s.encryption, '^wep') then
136 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section))
137 uci:set("wireless", m.hidden.cfg, "key", wkey:formvalue(section) or "")
138 elseif string.match(s.encryption, '^psk') then
139 if ciph:formvalue(section) ~= "auto" then
140 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
141 else
142 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section))
143 end
144 uci:set("wireless", m.hidden.cfg, "key", wkey:formvalue(section) or "")
145 elseif string.match(s.encryption, '^wpa') then
146 if ciph:formvalue(section) ~= "auto" then
147 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
148 else
149 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section))
150 end
151 uci:set("wireless", m.hidden.cfg, "eap_type", eaptype:formvalue(section))
152 uci:set("wireless", m.hidden.cfg, "auth", authentication:formvalue(section))
153 uci:set("wireless", m.hidden.cfg, "identity", ident:formvalue(section) or "")
154 uci:set("wireless", m.hidden.cfg, "password", wkey:formvalue(section) or "")
155 uci:set("wireless", m.hidden.cfg, "ca_cert", cacert:formvalue(section) or "")
156 uci:set("wireless", m.hidden.cfg, "client_cert", clientcert:formvalue(section) or "")
157 uci:set("wireless", m.hidden.cfg, "priv_key", privkey:formvalue(section) or "")
158 uci:set("wireless", m.hidden.cfg, "priv_key_pwd", privkeypwd:formvalue(section) or "")
159 end
160 end
161 uci:save("wireless")
162 uci:commit("wireless")
163 m.on_cancel()
164 end
165
166 return m