treewide: filter shell arguments through shellquote() where applicable
[project/luci.git] / applications / luci-app-openvpn / luasrc / model / cbi / openvpn.lua
index 2f865e0..ccad531 100644 (file)
@@ -1,16 +1,5 @@
---[[
-LuCI - Lua Configuration Interface
-
-Copyright 2008 Steven Barth <steven@midlink.org>
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-$Id$
-]]--
+-- Copyright 2008 Steven Barth <steven@midlink.org>
+-- Licensed to the public under the Apache License 2.0.
 
 local fs  = require "nixio.fs"
 local sys = require "luci.sys"
 
 local fs  = require "nixio.fs"
 local sys = require "luci.sys"
@@ -36,6 +25,15 @@ uci:foreach( "openvpn_recipes", "openvpn_recipe",
        end
 )
 
        end
 )
 
+function s.getPID(section) -- Universal function which returns valid pid # or nil
+       local pid = sys.exec("%s | grep -w [o]penvpn(%s)" % { psstring, section })
+       if pid and #pid > 0 then
+               return tonumber(pid:match("^%d+"))
+       else
+               return nil
+       end
+end
+
 function s.parse(self, section)
        local recipe = luci.http.formvalue(
                luci.cbi.CREATE_PREFIX .. self.config .. "." ..
 function s.parse(self, section)
        local recipe = luci.http.formvalue(
                luci.cbi.CREATE_PREFIX .. self.config .. "." ..
@@ -79,8 +77,8 @@ s:option( Flag, "enabled", translate("Enabled") )
 
 local active = s:option( DummyValue, "_active", translate("Started") )
 function active.cfgvalue(self, section)
 
 local active = s:option( DummyValue, "_active", translate("Started") )
 function active.cfgvalue(self, section)
-       local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
-       if pid and #pid > 0 and tonumber(pid) ~= nil then
+       local pid = s.getPID(section)
+       if pid ~= nil then
                return (sys.process.signal(pid, 0))
                        and translatef("yes (%i)", pid)
                        or  translate("no")
                return (sys.process.signal(pid, 0))
                        and translatef("yes (%i)", pid)
                        or  translate("no")
@@ -94,8 +92,8 @@ updown.redirect = luci.dispatcher.build_url(
        "admin", "services", "openvpn"
 )
 function updown.cbid(self, section)
        "admin", "services", "openvpn"
 )
 function updown.cbid(self, section)
-       local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
-       self._state = pid and #pid > 0 and sys.process.signal(pid, 0)
+       local pid = s.getPID(section)
+       self._state = pid ~= nil and sys.process.signal(pid, 0)
        self.option = self._state and "stop" or "start"
        return AbstractValue.cbid(self, section)
 end
        self.option = self._state and "stop" or "start"
        return AbstractValue.cbid(self, section)
 end
@@ -105,8 +103,10 @@ function updown.cfgvalue(self, section)
 end
 function updown.write(self, section, value)
        if self.option == "stop" then
 end
 function updown.write(self, section, value)
        if self.option == "stop" then
-               local pid = sys.exec("%s | grep %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
-               sys.process.signal(pid,15)
+               local pid = s.getPID(section)
+               if pid ~= nil then
+                       sys.process.signal(pid,15)
+               end
        else
                luci.sys.call("/etc/init.d/openvpn start %s" % section)
        end
        else
                luci.sys.call("/etc/init.d/openvpn start %s" % section)
        end