[openwrt/openwrt] openpvn: Split out config parsing code for reuse

LEDE Commits lede-commits at lists.infradead.org
Thu Jul 23 07:10:12 EDT 2020


jow pushed a commit to openwrt/openwrt.git, branch master:
https://git.openwrt.org/8483bf3126583041bd6f5808978dc3b14ad15a1a

commit 8483bf3126583041bd6f5808978dc3b14ad15a1a
Author: Michal Hrusecky <michal at hrusecky.net>
AuthorDate: Thu Jul 23 12:10:45 2020 +0200

    openpvn: Split out config parsing code for reuse
    
    Split out code that parses openvpn configuration file into separate file
    that can be later included in various scripts and reused.
    
    Signed-off-by: Michal Hrusecky <michal at hrusecky.net>
    (cherry picked from commit 86d8467c8ab792c79809a08c223dd9d40da6da2e)
---
 package/network/services/openvpn/Makefile          |  5 +++++
 .../openvpn/files/etc/hotplug.d/openvpn/01-user    | 15 ++------------
 .../openvpn/01-user => lib/functions/openvpn.sh}   | 24 ++++------------------
 3 files changed, 11 insertions(+), 33 deletions(-)

diff --git a/package/network/services/openvpn/Makefile b/package/network/services/openvpn/Makefile
index f1170bbd37..3bd7ad8d1c 100644
--- a/package/network/services/openvpn/Makefile
+++ b/package/network/services/openvpn/Makefile
@@ -112,6 +112,7 @@ define Package/openvpn-$(BUILD_VARIANT)/install
 		$(1)/etc/init.d \
 		$(1)/etc/config \
 		$(1)/etc/openvpn \
+		$(1)/lib/functions \
 		$(1)/lib/upgrade/keep.d \
 		$(1)/usr/libexec \
 		$(1)/etc/hotplug.d/openvpn
@@ -128,6 +129,10 @@ define Package/openvpn-$(BUILD_VARIANT)/install
 		files/usr/libexec/openvpn-hotplug \
 		$(1)/usr/libexec/openvpn-hotplug
 
+	$(INSTALL_DATA) \
+		files/lib/functions/openvpn.sh \
+		$(1)/lib/functions/openvpn.sh
+
 	$(INSTALL_DATA) \
 		files/etc/hotplug.d/openvpn/01-user \
 		$(1)/etc/hotplug.d/openvpn/01-user
diff --git a/package/network/services/openvpn/files/etc/hotplug.d/openvpn/01-user b/package/network/services/openvpn/files/etc/hotplug.d/openvpn/01-user
index 6d45f0b7c6..86be69e805 100644
--- a/package/network/services/openvpn/files/etc/hotplug.d/openvpn/01-user
+++ b/package/network/services/openvpn/files/etc/hotplug.d/openvpn/01-user
@@ -1,17 +1,6 @@
 #!/bin/sh
 
-get_option() {
-	local variable="$1"
-	local option="$2"
-
-	local value="$(sed -rne 's/^[ \t]*'"$option"'[ \t]+(([^ \t\\]|\\.)+)[ \t]*$/\1/p' "$config" | tail -n1 | sed -re 's/\\(.)/\1/g')"
-	[ -n "$value" ] || value="$(sed -rne 's/^[ \t]*'"$option"'[ \t]+'"'([^']+)'"'[ \t]*$/\1/p' "$config" | tail -n1)"
-	[ -n "$value" ] || value="$(sed -rne 's/^[ \t]*'"$option"'[ \t]+"(([^"\\]|\\.)+)"[ \t]*$/\1/p' "$config" | tail -n1 | sed -re 's/\\(.)/\1/g')"
-	[ -n "$value" ] || return 1
-
-	export -n "$variable=$value"
-	return 0
-}
+. /lib/functions/openvpn.sh
 
 [ -e "/etc/openvpn.user" ] && {
 	env -i ACTION="$ACTION" INSTANCE="$INSTANCE" \
@@ -23,7 +12,7 @@ get_option() {
 # Wrap user defined scripts on up/down events
 case "$ACTION" in
 	up|down)
-		if get_option command "$ACTION"; then
+		if get_openvpn_option "$config" command "$ACTION"; then
 			exec /bin/sh -c "$command $ACTION $INSTANCE $*"
 		fi
 	;;
diff --git a/package/network/services/openvpn/files/etc/hotplug.d/openvpn/01-user b/package/network/services/openvpn/files/lib/functions/openvpn.sh
similarity index 56%
copy from package/network/services/openvpn/files/etc/hotplug.d/openvpn/01-user
copy to package/network/services/openvpn/files/lib/functions/openvpn.sh
index 6d45f0b7c6..83fb1bb453 100644
--- a/package/network/services/openvpn/files/etc/hotplug.d/openvpn/01-user
+++ b/package/network/services/openvpn/files/lib/functions/openvpn.sh
@@ -1,8 +1,9 @@
 #!/bin/sh
 
-get_option() {
-	local variable="$1"
-	local option="$2"
+get_openvpn_option() {
+	local config="$1"
+	local variable="$2"
+	local option="$3"
 
 	local value="$(sed -rne 's/^[ \t]*'"$option"'[ \t]+(([^ \t\\]|\\.)+)[ \t]*$/\1/p' "$config" | tail -n1 | sed -re 's/\\(.)/\1/g')"
 	[ -n "$value" ] || value="$(sed -rne 's/^[ \t]*'"$option"'[ \t]+'"'([^']+)'"'[ \t]*$/\1/p' "$config" | tail -n1)"
@@ -13,20 +14,3 @@ get_option() {
 	return 0
 }
 
-[ -e "/etc/openvpn.user" ] && {
-	env -i ACTION="$ACTION" INSTANCE="$INSTANCE" \
-		/bin/sh \
-		/etc/openvpn.user \
-		$*
-}
-
-# Wrap user defined scripts on up/down events
-case "$ACTION" in
-	up|down)
-		if get_option command "$ACTION"; then
-			exec /bin/sh -c "$command $ACTION $INSTANCE $*"
-		fi
-	;;
-esac
-
-exit 0



More information about the lede-commits mailing list