luci-app-openvpn: sync code style with openvpn-advanced
[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         { Value,
91                 "config",
92                 "/etc/openvpn/ovpn-file.ovpn",
93                 translate("Local OVPN configuration file") },
94 }
95
96
97 local m = Map("openvpn")
98 local p = m:section( SimpleSection )
99
100 m.apply_on_parse = true
101
102
103 p.template = "openvpn/pageswitch"
104 p.mode     = "basic"
105 p.instance = arg[1]
106
107
108 local s = m:section( NamedSection, arg[1], "openvpn" )
109
110 for _, option in ipairs(basicParams) do
111         local o = s:option(
112                 option[1], option[2],
113                 option[2], option[4]
114         )
115
116         o.optional = true
117
118         if option[1] == DummyValue then
119                 o.value = option[3]
120         elseif option[1] == FileUpload then
121
122                 function o.cfgvalue(self, section)
123                         local cfg_val = AbstractValue.cfgvalue(self, section)
124
125                         if cfg_val then
126                                 return cfg_val
127                         end
128                 end
129
130                 function o.formvalue(self, section)
131                         local sel_val = AbstractValue.formvalue(self, section)
132                         local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
133
134                         if sel_val and sel_val ~= "" then
135                                 return sel_val
136                         end
137
138                         if txt_val and txt_val ~= "" then
139                                 return txt_val
140                         end
141                 end
142
143                 function o.remove(self, section)
144                         local cfg_val = AbstractValue.cfgvalue(self, section)
145                         local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
146                         
147                         if cfg_val and fs.access(cfg_val) and txt_val == "" then
148                                 fs.unlink(cfg_val)
149                         end
150                         return AbstractValue.remove(self, section)
151                 end
152         else
153                 if option[1] == DynamicList then
154                         function o.cfgvalue(...)
155                                 local val = AbstractValue.cfgvalue(...)
156                                 return ( val and type(val) ~= "table" ) and { val } or val
157                         end
158                 end
159
160                 if type(option[3]) == "table" then
161                         if o.optional then o:value("", "-- remove --") end
162                         for _, v in ipairs(option[3]) do
163                                 v = tostring(v)
164                                 o:value(v)
165                         end
166                         o.default = tostring(option[3][1])
167                 else
168                         o.default = tostring(option[3])
169                 end
170         end
171
172         for i=5,#option do
173                 if type(option[i]) == "table" then
174                         o:depends(option[i])
175                 end
176         end
177 end
178
179 return m