Merge pull request #1735 from sumpfralle/olsr-jsoninfo-parser-handle-empty-result
[oweals/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / wifi_edit.lua
1 -- Copyright 2017-2019 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 util    = require("luci.util")
8 local scripts = util.split(util.trim(util.exec("ls /etc/travelmate/*.login 2>/dev/null")), "\n", nil, true) or {}
9
10 m = SimpleForm("edit", translate("Edit Wireless Uplink Configuration"))
11 m.submit = translate("Save")
12 m.cancel = translate("Back to overview")
13 m.reset = false
14
15 function m.on_cancel()
16         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
17 end
18
19 m.hidden = {
20         cfg = http.formvalue("cfg")
21 }
22
23 local s = uci:get_all("wireless", m.hidden.cfg)
24
25 if s ~= nil then
26         wssid = m:field(Value, "ssid", translate("SSID"))
27         wssid.datatype = "rangelength(1,32)"
28         wssid.default = s.ssid or ""
29
30         bssid = m:field(Value, "bssid", translate("BSSID"))
31         bssid.datatype = "macaddr"
32         bssid.default = s.bssid or ""
33
34         s.cipher = "auto"
35         if string.match(s.encryption, '%+') and not string.match(s.encryption, '^wep') then
36                 s.pos = string.find(s.encryption, '%+')
37                 s.cipher = string.sub(s.encryption, s.pos + 1)
38                 s.encryption = string.sub(s.encryption, 0, s.pos - 1)
39         end
40
41         if s.encryption and s.encryption ~= "none" then
42                 if string.match(s.encryption, '^wep') then
43                         encr = m:field(ListValue, "encryption", translate("Encryption"))
44                         encr:value("wep", "WEP")
45                         encr:value("wep+open", "WEP Open System")
46                         encr:value("wep+mixed", "WEP mixed")
47                         encr:value("wep+shared", "WEP Shared Key")
48                         encr.default = s.encryption
49
50                         wkey = m:field(Value, "key", translate("Passphrase"))
51                         wkey.datatype = "wepkey"
52                 elseif string.match(s.encryption, '^psk') then
53                         encr = m:field(ListValue, "encryption", translate("Encryption"))
54                         encr:value("psk", "WPA PSK")
55                         encr:value("psk-mixed", "WPA/WPA2 mixed")
56                         encr:value("psk2", "WPA2 PSK")
57                         encr.default = s.encryption
58
59                         ciph = m:field(ListValue, "cipher", translate("Cipher"))
60                         ciph:value("auto", translate("Automatic"))
61                         ciph:value("ccmp", translate("Force CCMP (AES)"))
62                         ciph:value("tkip", translate("Force TKIP"))
63                         ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
64                         ciph.default = s.cipher
65
66                         wkey = m:field(Value, "key", translate("Passphrase"))
67                         wkey.datatype = "wpakey"
68                 elseif string.match(s.encryption, '^wpa') 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 = s.encryption
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 = s.cipher
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 = s.eap_type or "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:value("auth=PAP")
99                         authentication:value("auth=MSCHAPV2")
100                         authentication.default = s.auth or "EAP-MSCHAPV2"
101
102                         ident = m:field(Value, "identity", translate("Identity"))
103                         ident.default = s.identity or ""
104
105                         wkey = m:field(Value, "password", translate("Passphrase"))
106                         wkey.datatype = "wpakey"
107
108                         cacert = m:field(Value, "ca_cert", translate("Path to CA-Certificate"))
109                         cacert.rmempty = true
110                         cacert.default = s.ca_cert or ""
111                         
112                         clientcert = m:field(Value, "client_cert", translate("Path to Client-Certificate"))
113                         clientcert:depends("eap_type","tls")
114                         clientcert.rmempty = true
115                         clientcert.default = s.client_cert or ""
116
117                         privkey = m:field(Value, "priv_key", translate("Path to Private Key"))
118                         privkey:depends("eap_type","tls")
119                         privkey.rmempty = true
120                         privkey.default = s.priv_key or ""
121
122                         privkeypwd = m:field(Value, "priv_key_pwd", translate("Password of Private Key"))
123                         privkeypwd:depends("eap_type","tls")
124                         privkeypwd.datatype = "wpakey"
125                         privkeypwd.password = true
126                         privkeypwd.rmempty = true
127                         privkeypwd.default = s.priv_key_pwd or ""
128                 end
129                 wkey.password = true
130                 wkey.default = s.key or s.password
131         end
132
133         local login_section = (s.ssid or "") .. (s.bssid or "")
134         login_section = login_section:gsub("[^%w_]", "_")
135         local cmd = uci:get("travelmate", login_section, "command")
136         cmd_list = m:field(ListValue, "cmdlist", translate("Auto Login Script"),
137                 translate("External script reference which will be called for automated captive portal logins."))
138         cmd_list:value("none")
139         for _, z in ipairs(scripts) do
140                 cmd_list:value(z)
141         end
142         cmd_list.default = cmd or "none"
143 else
144         m.on_cancel()
145 end
146
147 function wssid.write(self, section, value)
148         uci:set("wireless", m.hidden.cfg, "ssid", wssid:formvalue(section))
149         uci:set("wireless", m.hidden.cfg, "bssid", bssid:formvalue(section))
150         if s.encryption and s.encryption ~= "none" then
151                 if string.match(s.encryption, '^wep') then
152                         uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section))
153                         uci:set("wireless", m.hidden.cfg, "key", wkey:formvalue(section) or "")
154                 elseif string.match(s.encryption, '^psk') then
155                         if ciph:formvalue(section) ~= "auto" then
156                                 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
157                         else
158                                 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section))
159                         end
160                         uci:set("wireless", m.hidden.cfg, "key", wkey:formvalue(section) or "")
161                 elseif string.match(s.encryption, '^wpa') then
162                         if ciph:formvalue(section) ~= "auto" then
163                                 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
164                         else
165                                 uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section))
166                         end
167                         uci:set("wireless", m.hidden.cfg, "eap_type", eaptype:formvalue(section))
168                         uci:set("wireless", m.hidden.cfg, "auth", authentication:formvalue(section))
169                         uci:set("wireless", m.hidden.cfg, "identity", ident:formvalue(section) or "")
170                         uci:set("wireless", m.hidden.cfg, "password", wkey:formvalue(section) or "")
171                         uci:set("wireless", m.hidden.cfg, "ca_cert", cacert:formvalue(section) or "")
172                         uci:set("wireless", m.hidden.cfg, "client_cert", clientcert:formvalue(section) or "")
173                         uci:set("wireless", m.hidden.cfg, "priv_key", privkey:formvalue(section) or "")
174                         uci:set("wireless", m.hidden.cfg, "priv_key_pwd", privkeypwd:formvalue(section) or "")
175                 end
176         end
177         local login_section = (wssid:formvalue(section) or "") .. (bssid:formvalue(section) or "")
178         login_section = login_section:gsub("[^%w_]", "_")
179         if not uci:get("travelmate", login_section) and cmd_list:formvalue(section) ~= "none" then
180                 uci:set("travelmate", login_section, "login")
181         end
182         if uci:get("travelmate", login_section) then
183                 uci:set("travelmate", login_section, "command", cmd_list:formvalue(section))
184                 uci:save("travelmate")
185                 uci:commit("travelmate")
186         end
187         uci:save("wireless")
188         uci:commit("wireless")
189         luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>&1")
190         m.on_cancel()
191 end
192
193 return m