Merge pull request #2424 from LuKePicci/luci-proto-ppp_pppoe_host-uniq
[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         { ListValue,
43                 "comp_lzo",
44                 {"yes","no","adaptive"},
45                 translate("Use fast LZO compression") },
46         { Value,
47                 "keepalive",
48                 "10 60",
49                 translate("Helper directive to simplify the expression of --ping and --ping-restart in server mode configurations") },
50         { ListValue,
51                 "proto",
52                 { "udp", "tcp-client", "tcp-server" },
53                 translate("Use protocol") },
54         { Flag,
55                 "client",
56                 0,
57                 translate("Configure client mode") },
58         { Flag,
59                 "client_to_client",
60                 0,
61                 translate("Allow client-to-client traffic") },
62         { DynamicList,
63                 "remote",
64                 "vpnserver.example.org",
65                 translate("Remote host name or ip address") },
66         { FileUpload,
67                 "secret",
68                 "/etc/openvpn/secret.key",
69                 translate("Enable Static Key encryption mode (non-TLS)") },
70         { ListValue,
71                 "key_direction",
72                 { 0, 1 },
73                 translate("The key direction for 'tls-auth' and 'secret' options") },
74         { FileUpload,
75                 "pkcs12",
76                 "/etc/easy-rsa/keys/some-client.pk12",
77                 translate("PKCS#12 file containing keys") },
78         { FileUpload,
79                 "ca",
80                 "/etc/easy-rsa/keys/ca.crt",
81                 translate("Certificate authority") },
82         { FileUpload,
83                 "dh",
84                 "/etc/easy-rsa/keys/dh1024.pem",
85                 translate("Diffie Hellman parameters") },
86         { FileUpload,
87                 "cert",
88                 "/etc/easy-rsa/keys/some-client.crt",
89                 translate("Local certificate") },
90         { FileUpload,
91                 "key",
92                 "/etc/easy-rsa/keys/some-client.key",
93                 translate("Local private key") },
94 }
95
96
97 local m = Map("openvpn")
98 m.redirect = luci.dispatcher.build_url("admin", "services", "openvpn")
99 m.apply_on_parse = true
100
101 local p = m:section( SimpleSection )
102 p.template = "openvpn/pageswitch"
103 p.mode     = "basic"
104 p.instance = arg[1]
105
106
107 local s = m:section( NamedSection, arg[1], "openvpn" )
108
109 for _, option in ipairs(basicParams) do
110         local o = s:option(
111                 option[1], option[2],
112                 option[2], option[4]
113         )
114
115         o.optional = true
116
117         if option[1] == DummyValue then
118                 o.value = option[3]
119         elseif option[1] == FileUpload then
120
121                 function o.cfgvalue(self, section)
122                         local cfg_val = AbstractValue.cfgvalue(self, section)
123
124                         if cfg_val then
125                                 return cfg_val
126                         end
127                 end
128
129                 function o.formvalue(self, section)
130                         local sel_val = AbstractValue.formvalue(self, section)
131                         local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
132
133                         if sel_val and sel_val ~= "" then
134                                 return sel_val
135                         end
136
137                         if txt_val and txt_val ~= "" then
138                                 return txt_val
139                         end
140                 end
141
142                 function o.remove(self, section)
143                         local cfg_val = AbstractValue.cfgvalue(self, section)
144                         local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
145                         
146                         if cfg_val and fs.access(cfg_val) and txt_val == "" then
147                                 fs.unlink(cfg_val)
148                         end
149                         return AbstractValue.remove(self, section)
150                 end
151         elseif option[1] == Flag then
152                 o.default = nil
153         else
154                 if option[1] == DynamicList then
155                         function o.cfgvalue(...)
156                                 local val = AbstractValue.cfgvalue(...)
157                                 return ( val and type(val) ~= "table" ) and { val } or val
158                         end
159                 end
160
161                 if type(option[3]) == "table" then
162                         if o.optional then o:value("", "-- remove --") end
163                         for _, v in ipairs(option[3]) do
164                                 v = tostring(v)
165                                 o:value(v)
166                         end
167                         o.default = tostring(option[3][1])
168                 else
169                         o.default = tostring(option[3])
170                 end
171         end
172
173         for i=5,#option do
174                 if type(option[i]) == "table" then
175                         o:depends(option[i])
176                 end
177         end
178 end
179
180 return m