Translated using Weblate (Japanese)
[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         if string.match(s.encryption, '%+') and not string.match(s.encryption, '^wep') then
35                 s.pos = string.find(s.encryption, '%+')
36                 s.cipher = string.sub(s.encryption, s.pos + 1)
37                 s.encryption = string.sub(s.encryption, 0, s.pos - 1)
38         end
39
40         if s.encryption and s.encryption ~= "none" then
41                 if string.match(s.encryption, '^wep') then
42                         encr = m:field(ListValue, "encryption", translate("Encryption"))
43                         encr:value("wep", "WEP")
44                         encr:value("wep+open", "WEP Open System")
45                         encr:value("wep+mixed", "WEP mixed")
46                         encr:value("wep+shared", "WEP Shared Key")
47                         encr.default = s.encryption
48
49                         wkey = m:field(Value, "key", translate("Passphrase"))
50                         wkey.datatype = "wepkey"
51                         wkey.password = true
52                         wkey.default = s.key
53                 elseif string.match(s.encryption, '^wpa') then
54                         encr = m:field(ListValue, "encryption", translate("Encryption"))
55                         encr:value("wpa", "WPA Enterprise")
56                         encr:value("wpa-mixed", "WPA/WPA2 Enterprise mixed")
57                         encr:value("wpa2", "WPA2 Enterprise")
58                         encr.default = s.encryption
59
60                         ciph = m:field(ListValue, "cipher", translate("Cipher"))
61                         ciph:value("auto", translate("Automatic"))
62                         ciph:value("ccmp", translate("Force CCMP (AES)"))
63                         ciph:value("tkip", translate("Force TKIP"))
64                         ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
65                         ciph.default = s.cipher
66
67                         eaptype = m:field(ListValue, "eap_type", translate("EAP-Method"))
68                         eaptype:value("tls", "TLS")
69                         eaptype:value("ttls", "TTLS")
70                         eaptype:value("peap", "PEAP")
71                         eaptype:value("fast", "FAST")
72                         eaptype.default = s.eap_type or "peap"
73
74                         authentication = m:field(ListValue, "auth", translate("Authentication"))
75                         authentication:value("PAP")
76                         authentication:value("CHAP")
77                         authentication:value("MSCHAP")
78                         authentication:value("MSCHAPV2")
79                         authentication:value("EAP-GTC")
80                         authentication:value("EAP-MD5")
81                         authentication:value("EAP-MSCHAPV2")
82                         authentication:value("EAP-TLS")
83                         authentication:value("auth=PAP")
84                         authentication:value("auth=MSCHAPV2")
85                         authentication.default = s.auth or "EAP-MSCHAPV2"
86
87                         ident = m:field(Value, "identity", translate("Identity"))
88                         ident.default = s.identity or ""
89
90                         wkey = m:field(Value, "password", translate("Passphrase"))
91                         wkey.datatype = "wpakey"
92                         wkey.password = true
93                         wkey.default = s.password
94
95                         cacert = m:field(Value, "ca_cert", translate("Path to CA-Certificate"))
96                         cacert.rmempty = true
97                         cacert.default = s.ca_cert or ""
98                         
99                         clientcert = m:field(Value, "client_cert", translate("Path to Client-Certificate"))
100                         clientcert:depends("eap_type","tls")
101                         clientcert.rmempty = true
102                         clientcert.default = s.client_cert or ""
103
104                         privkey = m:field(Value, "priv_key", translate("Path to Private Key"))
105                         privkey:depends("eap_type","tls")
106                         privkey.rmempty = true
107                         privkey.default = s.priv_key or ""
108
109                         privkeypwd = m:field(Value, "priv_key_pwd", translate("Password of Private Key"))
110                         privkeypwd:depends("eap_type","tls")
111                         privkeypwd.datatype = "wpakey"
112                         privkeypwd.password = true
113                         privkeypwd.rmempty = true
114                         privkeypwd.default = s.priv_key_pwd or ""
115                 else
116                         encr = m:field(ListValue, "encryption", translate("Encryption"))
117                         encr:value("psk", "WPA-PSK")
118                         encr:value("psk2", "WPA2-PSK")
119                         encr:value("psk-mixed", "WPA/WPA2 mixed")
120                         encr:value("sae", "WPA3-SAE")
121                         encr:value("owe", "OWE (open network)")
122                         encr:value("sae-mixed", "WPA2/WPA3 mixed")
123                         encr.default = s.encryption
124
125                         ciph = m:field(ListValue, "cipher", translate("Cipher"))
126                         ciph:value("auto", translate("Automatic"))
127                         ciph:value("ccmp", translate("Force CCMP (AES)"))
128                         ciph:value("tkip", translate("Force TKIP"))
129                         ciph:value("tkip+ccmp", translate("Force TKIP and CCMP (AES)"))
130                         ciph:depends("encryption", "psk")
131                         ciph:depends("encryption", "psk2")
132                         ciph:depends("encryption", "psk-mixed")
133                         ciph.default = s.cipher or "auto"
134
135                         wkey = m:field(Value, "key", translate("Passphrase"))
136                         wkey.datatype = "wpakey"
137                         wkey.password = true
138                         wkey:depends("encryption", "psk")
139                         wkey:depends("encryption", "psk2")
140                         wkey:depends("encryption", "psk-mixed")
141                         wkey:depends("encryption", "sae")
142                         wkey:depends("encryption", "sae-mixed")
143                         wkey.default = s.key
144                 end
145         end
146 else
147         m.on_cancel()
148 end
149
150 local login_section = (s.device or "") .. "_" .. (s.ssid or "") .. "_" .. (s.bssid or "")
151 login_section = login_section:gsub("[^%w_]", "_")
152 local cmd = uci:get("travelmate", login_section, "command")
153 local cmd_args_default = uci:get("travelmate", login_section, "command_args")
154 cmd_list = m:field(ListValue, "cmdlist", translate("Auto Login Script"),
155         translate("External script reference which will be called for automated captive portal logins."))
156 cmd_args = m:field(Value, "cmdargs", translate("Optional Arguments"),
157         translate("Space separated list of additional arguments passed to the Auto Login Script, i.e. username and password"))
158 for _, z in ipairs(scripts) do
159         cmd_list:value(z)
160         cmd_args:depends("cmdlist", z)
161 end
162 cmd_list:value("none")
163 cmd_list.default = cmd or "none"
164 cmd_args.default = cmd_args_default
165
166 function wssid.write(self, section, value)
167         uci:set("wireless", m.hidden.cfg, "ssid", wssid:formvalue(section))
168         uci:set("wireless", m.hidden.cfg, "bssid", bssid:formvalue(section))
169         if encr then
170                 if string.find(encr:formvalue(section), '^wep') then
171                         uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section))
172                         uci:set("wireless", m.hidden.cfg, "key", wkey:formvalue(section) or "")
173                 elseif string.find(encr:formvalue(section), '^wpa') then
174                         uci:set("wireless", m.hidden.cfg, "eap_type", eaptype:formvalue(section))
175                         uci:set("wireless", m.hidden.cfg, "auth", authentication:formvalue(section))
176                         uci:set("wireless", m.hidden.cfg, "identity", ident:formvalue(section) or "")
177                         uci:set("wireless", m.hidden.cfg, "password", wkey:formvalue(section) or "")
178                         uci:set("wireless", m.hidden.cfg, "ca_cert", cacert:formvalue(section) or "")
179                         uci:set("wireless", m.hidden.cfg, "client_cert", clientcert:formvalue(section) or "")
180                         uci:set("wireless", m.hidden.cfg, "priv_key", privkey:formvalue(section) or "")
181                         uci:set("wireless", m.hidden.cfg, "priv_key_pwd", privkeypwd:formvalue(section) or "")
182                 elseif encr:formvalue(section) ~= "owe" then
183                         uci:set("wireless", m.hidden.cfg, "key", wkey:formvalue(section) or "")
184                 end
185                 if ciph and ciph:formvalue(section) and ciph:formvalue(section) ~= "auto" then
186                         uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section) .. "+" .. ciph:formvalue(section))
187                 else
188                         uci:set("wireless", m.hidden.cfg, "encryption", encr:formvalue(section))
189                 end
190         end
191
192         if not uci:get("travelmate", login_section) and cmd_list:formvalue(section) ~= "none" then
193                 uci:set("travelmate", login_section, "login")
194         end
195         if uci:get("travelmate", login_section) then
196                 uci:set("travelmate", login_section, "command", cmd_list:formvalue(section))
197                 uci:set("travelmate", login_section, "command_args", cmd_args:formvalue(section))
198                 uci:save("travelmate")
199                 uci:commit("travelmate")
200         end
201         uci:save("wireless")
202         uci:commit("wireless")
203         luci.sys.call("env -i /bin/ubus call network reload >/dev/null 2>&1")
204         m.on_cancel()
205 end
206
207 return m