luci-app-openvpn: fix stray uci permission warning
[project/luci.git] / applications / luci-app-openvpn / luasrc / model / cbi / openvpn-file.lua
1 -- Licensed to the public under the Apache License 2.0.
2
3 local ip = require("luci.ip")
4 local fs = require("nixio.fs")
5 local util = require("luci.util")
6 local uci = require("luci.model.uci").cursor()
7 local cfg_file = uci:get("openvpn", arg[1], "config")
8 local auth_file = cfg_file:match("(.+)%..+").. ".auth"
9
10 local function makeForm(id, title, desc)
11 local t = Template("openvpn/pageswitch")
12 t.mode = "file"
13 t.instance = arg[1]
14
15 local f = SimpleForm(id, title, desc)
16 f:append(t)
17
18 return f
19 end
20
21 if not cfg_file or not fs.access(cfg_file) then
22 local f = makeForm("error", nil, translatef("The OVPN config file (%s) could not be found, please check your configuration.", cfg_file or "n/a"))
23 f:append(Template("openvpn/ovpn_css"))
24 f.reset = false
25 f.submit = false
26 return f
27 end
28
29 if fs.stat(cfg_file).size >= 102400 then
30 local f = makeForm("error", nil,
31 translatef("The size of the OVPN config file (%s) is too large for online editing in LuCI (≥ 100 KB). ", cfg_file)
32 .. translate("Please edit this file directly in a terminal session."))
33 f:append(Template("openvpn/ovpn_css"))
34 f.reset = false
35 f.submit = false
36 return f
37 end
38
39 f = makeForm("cfg", nil)
40 f:append(Template("openvpn/ovpn_css"))
41 f.submit = translate("Save")
42 f.reset = false
43
44 s = f:section(SimpleSection, nil, translatef("Section to modify the OVPN config file (%s)", cfg_file))
45 file = s:option(TextValue, "data1")
46 file.datatype = "string"
47 file.rows = 20
48
49 function file.cfgvalue()
50 return fs.readfile(cfg_file) or ""
51 end
52
53 function file.write(self, section, data1)
54 return fs.writefile(cfg_file, "\n" .. util.trim(data1:gsub("\r\n", "\n")) .. "\n")
55 end
56
57 function file.remove(self, section, value)
58 return fs.writefile(cfg_file, "")
59 end
60
61 function s.handle(self, state, data1)
62 return true
63 end
64
65 s = f:section(SimpleSection, nil, translatef("Section to add an optional 'auth-user-pass' file with your credentials (%s)", auth_file))
66 file = s:option(TextValue, "data2")
67 file.datatype = "string"
68 file.rows = 5
69
70 function file.cfgvalue()
71 return fs.readfile(auth_file) or ""
72 end
73
74 function file.write(self, section, data2)
75 return fs.writefile(auth_file, util.trim(data2:gsub("\r\n", "\n")) .. "\n")
76 end
77
78 function file.remove(self, section, value)
79 return fs.writefile(auth_file, "")
80 end
81
82 function s.handle(self, state, data2)
83 return true
84 end
85
86 return f