22cdcb4d6ed65c66f5d549e4253dfa9b14afea9c
[project/luci.git] / applications / luci-openvpn / luasrc / model / cbi / openvpn.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 require("luci.fs")
16 require("luci.ip")
17 require("luci.sys")
18 require("luci.model.uci")
19
20
21 local uci = luci.model.uci.cursor()
22
23 local m = Map("openvpn")
24 local s = m:section( TypedSection, "openvpn" )
25 s.template = "cbi/tblsection"
26 s.template_addremove = "openvpn/cbi-select-input-add"
27 s.addremove = true
28 s.add_select_options = { }
29 s.extedit = luci.dispatcher.build_url(
30         "admin", "services", "openvpn", "basic", "%s"
31 )
32
33 uci:load("openvpn_recipes")
34 uci:foreach( "openvpn_recipes", "openvpn_recipe",
35         function(section)
36                 s.add_select_options[section['.name']] =
37                         section['_description'] or section['.name']
38         end
39 )
40
41 function s.parse(self, section)
42         local recipe = luci.http.formvalue(
43                 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
44                 self.sectiontype .. ".select"
45         )
46
47         if recipe and not s.add_select_options[recipe] then
48                 self.invalid_cts = true
49         else
50                 TypedSection.parse( self, section )
51         end
52 end
53
54 function s.create(self, name)
55         local recipe = luci.http.formvalue(
56                 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
57                 self.sectiontype .. ".select"
58         )
59
60         uci:section(
61                 "openvpn", "openvpn", name,
62                 uci:get_all( "openvpn_recipes", recipe )
63         )
64
65         uci:delete("openvpn", name, "_role")
66         uci:delete("openvpn", name, "_description")
67         uci:save("openvpn")
68
69         luci.http.redirect( self.extedit:format(name) )
70 end
71
72
73 s:option( Flag, "enable" )
74
75 local active = s:option( DummyValue, "_active" )
76 function active.cfgvalue(self, section)
77         if luci.fs.isfile("/var/run/openvpn_%s.pid" % section) then
78                 local pid = io.lines("/var/run/openvpn_%s.pid" % section)()
79                 if pid and #pid > 0 and tonumber(pid) ~= nil then
80                         return (luci.sys.process.signal(pid, 0)) and "yes (" .. pid .. ")" or "no"
81                 end
82         end
83         return "no"
84 end
85
86 local port = s:option( DummyValue, "port" )
87 function port.cfgvalue(self, section)
88         local val = AbstractValue.cfgvalue(self, section)
89         return val or "1194"
90 end
91
92 local proto = s:option( DummyValue, "proto" )
93 function proto.cfgvalue(self, section)
94         local val = AbstractValue.cfgvalue(self, section)
95         return val or "udp"
96 end
97
98
99 return m