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@hrusecky.net>
(cherry picked from commit 86d8467c8ab792c79809a08c223dd9d40da6da2e)
This commit is contained in:
Michal Hrusecky 2020-07-23 12:10:45 +02:00 committed by Jo-Philipp Wich
parent eda9751d86
commit 8483bf3126
3 changed files with 23 additions and 13 deletions

View File

@ -112,6 +112,7 @@ define Package/openvpn-$(BUILD_VARIANT)/install
$(1)/etc/init.d \ $(1)/etc/init.d \
$(1)/etc/config \ $(1)/etc/config \
$(1)/etc/openvpn \ $(1)/etc/openvpn \
$(1)/lib/functions \
$(1)/lib/upgrade/keep.d \ $(1)/lib/upgrade/keep.d \
$(1)/usr/libexec \ $(1)/usr/libexec \
$(1)/etc/hotplug.d/openvpn $(1)/etc/hotplug.d/openvpn
@ -128,6 +129,10 @@ define Package/openvpn-$(BUILD_VARIANT)/install
files/usr/libexec/openvpn-hotplug \ files/usr/libexec/openvpn-hotplug \
$(1)/usr/libexec/openvpn-hotplug $(1)/usr/libexec/openvpn-hotplug
$(INSTALL_DATA) \
files/lib/functions/openvpn.sh \
$(1)/lib/functions/openvpn.sh
$(INSTALL_DATA) \ $(INSTALL_DATA) \
files/etc/hotplug.d/openvpn/01-user \ files/etc/hotplug.d/openvpn/01-user \
$(1)/etc/hotplug.d/openvpn/01-user $(1)/etc/hotplug.d/openvpn/01-user

View File

@ -1,17 +1,6 @@
#!/bin/sh #!/bin/sh
get_option() { . /lib/functions/openvpn.sh
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
}
[ -e "/etc/openvpn.user" ] && { [ -e "/etc/openvpn.user" ] && {
env -i ACTION="$ACTION" INSTANCE="$INSTANCE" \ env -i ACTION="$ACTION" INSTANCE="$INSTANCE" \
@ -23,7 +12,7 @@ get_option() {
# Wrap user defined scripts on up/down events # Wrap user defined scripts on up/down events
case "$ACTION" in case "$ACTION" in
up|down) up|down)
if get_option command "$ACTION"; then if get_openvpn_option "$config" command "$ACTION"; then
exec /bin/sh -c "$command $ACTION $INSTANCE $*" exec /bin/sh -c "$command $ACTION $INSTANCE $*"
fi fi
;; ;;

View File

@ -0,0 +1,16 @@
#!/bin/sh
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)"
[ -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
}