Merge pull request #2329 from TDT-AG/pr/20181123-luci-theme-material
[oweals/luci.git] / applications / luci-app-openvpn / luasrc / model / cbi / openvpn-basic.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local fs = require("nixio.fs")
5
6 local basicParams = {
7         --
8         -- Widget, Name, Default(s), Description
9         --
10         { ListValue,
11                 "verb",
12                 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 },
13                 translate("Set output verbosity") },
14         { Value,
15                 "nice",
16                 0,
17                 translate("Change process priority") },
18         { Value,
19                 "port",
20                 1194,
21                 translate("TCP/UDP port # for both local and remote") },
22         { ListValue,
23                 "dev_type",
24                 { "tun", "tap" },
25                 translate("Type of used device") },
26         { Value,
27                 "ifconfig",
28                 "10.200.200.3 10.200.200.1",
29                 translate("Set tun/tap adapter parameters") },
30         { Value,
31                 "server",
32                 "10.200.200.0 255.255.255.0",
33                 translate("Configure server mode") },
34         { Value,
35                 "server_bridge",
36                 "192.168.1.1 255.255.255.0 192.168.1.128 192.168.1.254",
37                 translate("Configure server bridge") },
38         { Flag,
39                 "nobind",
40                 0,
41                 translate("Do not bind to local address and port") },
42         { Value,
43                 "keepalive",
44                 "10 60",
45                 translate("Helper directive to simplify the expression of --ping and --ping-restart in server mode configurations") },
46         { ListValue,
47                 "proto",
48                 { "udp", "tcp-client", "tcp-server" },
49                 translate("Use protocol") },
50         { Flag,
51                 "client",
52                 0,
53                 translate("Configure client mode") },
54         { Flag,
55                 "client_to_client",
56                 0,
57                 translate("Allow client-to-client traffic") },
58         { DynamicList,
59                 "remote",
60                 "vpnserver.example.org",
61                 translate("Remote host name or ip address") },
62         { FileUpload,
63                 "secret",
64                 "/etc/openvpn/secret.key",
65                 translate("Enable Static Key encryption mode (non-TLS)") },
66         { ListValue,
67                 "key_direction",
68                 { 0, 1 },
69                 translate("The key direction for 'tls-auth' and 'secret' options") },
70         { FileUpload,
71                 "pkcs12",
72                 "/etc/easy-rsa/keys/some-client.pk12",
73                 translate("PKCS#12 file containing keys") },
74         { FileUpload,
75                 "ca",
76                 "/etc/easy-rsa/keys/ca.crt",
77                 translate("Certificate authority") },
78         { FileUpload,
79                 "dh",
80                 "/etc/easy-rsa/keys/dh1024.pem",
81                 translate("Diffie Hellman parameters") },
82         { FileUpload,
83                 "cert",
84                 "/etc/easy-rsa/keys/some-client.crt",
85                 translate("Local certificate") },
86         { FileUpload,
87                 "key",
88                 "/etc/easy-rsa/keys/some-client.key",
89                 translate("Local private key") },
90 }
91
92
93 local m = Map("openvpn")
94 m.redirect = luci.dispatcher.build_url("admin", "services", "openvpn")
95 m.apply_on_parse = true
96
97 local p = m:section( SimpleSection )
98 p.template = "openvpn/pageswitch"
99 p.mode     = "basic"
100 p.instance = arg[1]
101
102
103 local s = m:section( NamedSection, arg[1], "openvpn" )
104
105 for _, option in ipairs(basicParams) do
106         local o = s:option(
107                 option[1], option[2],
108                 option[2], option[4]
109         )
110
111         o.optional = true
112
113         if option[1] == DummyValue then
114                 o.value = option[3]
115         elseif option[1] == FileUpload then
116
117                 function o.cfgvalue(self, section)
118                         local cfg_val = AbstractValue.cfgvalue(self, section)
119
120                         if cfg_val then
121                                 return cfg_val
122                         end
123                 end
124
125                 function o.formvalue(self, section)
126                         local sel_val = AbstractValue.formvalue(self, section)
127                         local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
128
129                         if sel_val and sel_val ~= "" then
130                                 return sel_val
131                         end
132
133                         if txt_val and txt_val ~= "" then
134                                 return txt_val
135                         end
136                 end
137
138                 function o.remove(self, section)
139                         local cfg_val = AbstractValue.cfgvalue(self, section)
140                         local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
141                         
142                         if cfg_val and fs.access(cfg_val) and txt_val == "" then
143                                 fs.unlink(cfg_val)
144                         end
145                         return AbstractValue.remove(self, section)
146                 end
147         elseif option[1] == Flag then
148                 o.default = nil
149         else
150                 if option[1] == DynamicList then
151                         function o.cfgvalue(...)
152                                 local val = AbstractValue.cfgvalue(...)
153                                 return ( val and type(val) ~= "table" ) and { val } or val
154                         end
155                 end
156
157                 if type(option[3]) == "table" then
158                         if o.optional then o:value("", "-- remove --") end
159                         for _, v in ipairs(option[3]) do
160                                 v = tostring(v)
161                                 o:value(v)
162                         end
163                         o.default = tostring(option[3][1])
164                 else
165                         o.default = tostring(option[3])
166                 end
167         end
168
169         for i=5,#option do
170                 if type(option[i]) == "table" then
171                         o:depends(option[i])
172                 end
173         end
174 end
175
176 return m