Merge pull request #2307 from dibdot/ovpn
[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, "verb", { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }, translate("Set output verbosity") },
11         { Value, "nice",0, translate("Change process priority") },
12         { Value,"port",1194, translate("TCP/UDP port # for both local and remote") },
13         { ListValue,"dev_type",{ "tun", "tap" }, translate("Type of used device") },
14
15         { Value,"ifconfig","10.200.200.3 10.200.200.1", translate("Set tun/tap adapter parameters") },
16         { Value,"server","10.200.200.0 255.255.255.0", translate("Configure server mode") },
17         { Value,"server_bridge","192.168.1.1 255.255.255.0 192.168.1.128 192.168.1.254", translate("Configure server bridge") },
18         { Flag,"nobind",0, translate("Do not bind to local address and port") },
19
20         { Value,"keepalive","10 60", translate("Helper directive to simplify the expression of --ping and --ping-restart in server mode configurations") },
21
22         { ListValue,"proto",{ "udp", "tcp-client", "tcp-server" }, translate("Use protocol") },
23
24         { Flag,"client",0, translate("Configure client mode") },
25         { Flag,"client_to_client",0, translate("Allow client-to-client traffic") },
26         { DynamicList,"remote","vpnserver.example.org", translate("Remote host name or ip address") },
27
28         { FileUpload,"secret","/etc/openvpn/secret.key", translate("Enable Static Key encryption mode (non-TLS)") },
29         { ListValue,"key_direction", { 0, 1 }, translate("The key direction for 'tls-auth' and 'secret' options") },
30         { FileUpload,"pkcs12","/etc/easy-rsa/keys/some-client.pk12", translate("PKCS#12 file containing keys") },
31         { FileUpload,"ca","/etc/easy-rsa/keys/ca.crt", translate("Certificate authority") },
32         { FileUpload,"dh","/etc/easy-rsa/keys/dh1024.pem", translate("Diffie Hellman parameters") },
33         { FileUpload,"cert","/etc/easy-rsa/keys/some-client.crt", translate("Local certificate") },
34         { FileUpload,"key","/etc/easy-rsa/keys/some-client.key", translate("Local private key") },
35         { Value,"config","/etc/openvpn/ovpn-file.ovpn", translate("Local OVPN configuration file") },
36 }
37
38
39 local m = Map("openvpn")
40 local p = m:section( SimpleSection )
41
42 m.apply_on_parse = true
43
44
45 p.template = "openvpn/pageswitch"
46 p.mode     = "basic"
47 p.instance = arg[1]
48
49
50 local s = m:section( NamedSection, arg[1], "openvpn" )
51
52 for _, option in ipairs(basicParams) do
53         local o = s:option(
54                 option[1], option[2],
55                 option[2], option[4]
56         )
57         
58         o.optional = true
59
60         if option[1] == DummyValue then
61                 o.value = option[3]
62         elseif option[1] == FileUpload then
63
64                 function o.cfgvalue(self, section)
65                         local cfg_val = AbstractValue.cfgvalue(self, section)
66
67                         if cfg_val then
68                                 return cfg_val
69                         end
70                 end
71
72                 function o.formvalue(self, section)
73                         local sel_val = AbstractValue.formvalue(self, section)
74                         local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
75
76                         if sel_val and sel_val ~= "" then
77                                 return sel_val
78                         end
79
80                         if txt_val and txt_val ~= "" then
81                                 return txt_val
82                         end
83                 end
84
85                 function o.remove(self, section)
86                         local cfg_val = AbstractValue.cfgvalue(self, section)
87                         local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
88                         
89                         if cfg_val and fs.access(cfg_val) and txt_val == "" then
90                                 fs.unlink(cfg_val)
91                         end
92                         return AbstractValue.remove(self, section)
93                 end
94         else
95                 if option[1] == DynamicList then
96                         function o.cfgvalue(...)
97                                 local val = AbstractValue.cfgvalue(...)
98                                 return ( val and type(val) ~= "table" ) and { val } or val
99                         end
100                 end
101
102                 if type(option[3]) == "table" then
103                         if o.optional then o:value("", "-- remove --") end
104                         for _, v in ipairs(option[3]) do
105                                 v = tostring(v)
106                                 o:value(v)
107                         end
108                         o.default = tostring(option[3][1])
109                 else
110                         o.default = tostring(option[3])
111                 end
112         end
113
114         for i=5,#option do
115                 if type(option[i]) == "table" then
116                         o:depends(option[i])
117                 end
118         end
119 end
120
121 return m