Merge pull request #1718 from dibdot/travelmate
[project/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / wifi_edit.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
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:value("auth=PAP")
96                         authentication:value("auth=MSCHAPV2")
97                         authentication.default = s.auth or "EAP-MSCHAPV2"
98
99                         ident = m:field(Value, "identity", translate("Identity"))
100                         ident.default = s.identity or ""
101
102                         wkey = m:field(Value, "password", translate("Passphrase"))
103                         wkey.datatype = "wpakey"
104
105                         cacert = m:field(Value, "ca_cert", translate("Path to CA-Certificate"))
106                         cacert.rmempty = true
107                         cacert.default = s.ca_cert or ""
108                         
109                         clientcert = m:field(Value, "client_cert", translate("Path to Client-Certificate"))
110                         clientcert:depends("eap_type","tls")
111                         clientcert.rmempty = true
112                         clientcert.default = s.client_cert or ""
113
114                         privkey = m:field(Value, "priv_key", translate("Path to Private Key"))
115                         privkey:depends("eap_type","tls")
116                         privkey.rmempty = true
117                         privkey.default = s.priv_key or ""
118
119                         privkeypwd = m:field(Value, "priv_key_pwd", translate("Password of Private Key"))
120                         privkeypwd:depends("eap_type","tls")
121                         privkeypwd.datatype = "wpakey"
122                         privkeypwd.password = true
123                         privkeypwd.rmempty = true
124                         privkeypwd.default = s.priv_key_pwd or ""
125                 end
126                 wkey.password = true
127                 wkey.default = s.key or s.password
128         end
129 else
130         m.on_cancel()
131 end
132
133 function wssid.write(self, section, value)
134         uci:set("wireless", m.hidden.cfg, "ssid", wssid:formvalue(section))
135         uci:set("wireless", m.hidden.cfg, "bssid", bssid:formvalue(section))
136         if s.encryption and s.encryption ~= "none" then
137                 if string.match(s.encryption, '^wep') then
138                         uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section))
139                         uci:set("wireless", m.hidden.cfg, "key", wkey:formvalue(section) or "")
140                 elseif string.match(s.encryption, '^psk') then
141                         if ciph:formvalue(section) ~= "auto" then
142                                 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
143                         else
144                                 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section))
145                         end
146                         uci:set("wireless", m.hidden.cfg, "key", wkey:formvalue(section) or "")
147                 elseif string.match(s.encryption, '^wpa') then
148                         if ciph:formvalue(section) ~= "auto" then
149                                 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
150                         else
151                                 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section))
152                         end
153                         uci:set("wireless", m.hidden.cfg, "eap_type", eaptype:formvalue(section))
154                         uci:set("wireless", m.hidden.cfg, "auth", authentication:formvalue(section))
155                         uci:set("wireless", m.hidden.cfg, "identity", ident:formvalue(section) or "")
156                         uci:set("wireless", m.hidden.cfg, "password", wkey:formvalue(section) or "")
157                         uci:set("wireless", m.hidden.cfg, "ca_cert", cacert:formvalue(section) or "")
158                         uci:set("wireless", m.hidden.cfg, "client_cert", clientcert:formvalue(section) or "")
159                         uci:set("wireless", m.hidden.cfg, "priv_key", privkey:formvalue(section) or "")
160                         uci:set("wireless", m.hidden.cfg, "priv_key_pwd", privkeypwd:formvalue(section) or "")
161                 end
162         end
163         uci:save("wireless")
164         uci:commit("wireless")
165         luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>&1")
166         m.on_cancel()
167 end
168
169 return m