From a62f4ce1da764591f66f5a1ceb8f791fc05ed074 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 23 May 2018 15:08:30 +0200 Subject: wg-quick: support FreeBSD/Darwin search path --- src/tools/completion/wg-quick.bash-completion | 16 +++++++++++----- src/tools/man/wg-quick.8 | 3 ++- src/tools/wg-quick/darwin.bash | 18 +++++++++++++----- src/tools/wg-quick/freebsd.bash | 18 +++++++++++++----- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/tools/completion/wg-quick.bash-completion b/src/tools/completion/wg-quick.bash-completion index e5e3983..3474120 100644 --- a/src/tools/completion/wg-quick.bash-completion +++ b/src/tools/completion/wg-quick.bash-completion @@ -2,17 +2,23 @@ # Copyright (C) 2015-2018 Jason A. Donenfeld . All Rights Reserved. _wg_quick_completion() { - local i a + local p i a search_paths + search_paths=( /etc/wireguard ) + + [[ $OSTYPE == *freebsd* || $OSTYPE == *darwin* ]] && search_paths+=( /usr/local/etc/wireguard ) + if [[ $COMP_CWORD -eq 1 ]]; then COMPREPLY+=( $(compgen -W "up down" -- "${COMP_WORDS[1]}") ) elif [[ $COMP_CWORD -eq 2 ]]; then if [[ ${COMP_WORDS[1]} == up ]]; then local old_glob="$(shopt -p nullglob)" shopt -s nullglob - for i in /etc/wireguard/*.conf; do - i="${i##*/}"; i="${i%.conf}" - mapfile -t a < <(compgen -W "$i" -- "${COMP_WORDS[2]}") - COMPREPLY+=( "${a[@]}" ) + for p in "${search_paths[@]}"; do + for i in "$p"/*.conf; do + i="${i##*/}"; i="${i%.conf}" + mapfile -t a < <(compgen -W "$i" -- "${COMP_WORDS[2]}") + COMPREPLY+=( "${a[@]}" ) + done done eval "$old_glob" mapfile -t a < <(compgen -f -X '!*.conf' -- "${COMP_WORDS[2]}") diff --git a/src/tools/man/wg-quick.8 b/src/tools/man/wg-quick.8 index 5e27d10..d97fec9 100644 --- a/src/tools/man/wg-quick.8 +++ b/src/tools/man/wg-quick.8 @@ -31,7 +31,8 @@ interface without bringing the interface down. \fICONFIG_FILE\fP is a configuration file, whose filename is the interface name followed by `.conf'. Otherwise, \fIINTERFACE\fP is an interface name, with configuration -found at `/etc/wireguard/\fIINTERFACE\fP.conf'. +found at `/etc/wireguard/\fIINTERFACE\fP.conf', searched first, followed by distro-specific +search paths. Generally speaking, this utility is just a simple script that wraps invocations to .BR wg (8) diff --git a/src/tools/wg-quick/darwin.bash b/src/tools/wg-quick/darwin.bash index 17cf2b8..d1c83c0 100755 --- a/src/tools/wg-quick/darwin.bash +++ b/src/tools/wg-quick/darwin.bash @@ -40,10 +40,16 @@ die() { [[ ${BASH_VERSINFO[0]} -ge 4 ]] || die "Version mismatch: bash ${BASH_VERSINFO[0]} detected, when bash 4+ required" +CONFIG_SEARCH_PATHS=( /etc/wireguard /usr/local/etc/wireguard ) + parse_options() { - local interface_section=0 line key value stripped + local interface_section=0 line key value stripped path CONFIG_FILE="$1" - [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]] && CONFIG_FILE="/etc/wireguard/$CONFIG_FILE.conf" + if [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]]; then + for path in "${CONFIG_SEARCH_PATHS[@]}"; do + [[ -e $path/$CONFIG_FILE.conf ]] && { CONFIG_FILE="$path/$CONFIG_FILE.conf"; break; } + done + fi [[ -e $CONFIG_FILE ]] || die "\`$CONFIG_FILE' does not exist" [[ $CONFIG_FILE =~ (^|/)([a-zA-Z0-9_=+.-]{1,15})\.conf$ ]] || die "The config file must be a valid interface name, followed by .conf" CONFIG_FILE="$(cd "${CONFIG_FILE%/*}" && pwd -P)/${CONFIG_FILE##*/}" @@ -380,9 +386,11 @@ cmd_usage() { CONFIG_FILE is a configuration file, whose filename is the interface name followed by \`.conf'. Otherwise, INTERFACE is an interface name, with - configuration found at /etc/wireguard/INTERFACE.conf. It is to be readable - by wg(8)'s \`setconf' sub-command, with the exception of the following additions - to the [Interface] section, which are handled by $PROGRAM: + configuration found at: + ${CONFIG_SEARCH_PATHS[@]/%//INTERFACE.conf}. + It is to be readable by wg(8)'s \`setconf' sub-command, with the exception + of the following additions to the [Interface] section, which are handled + by $PROGRAM: - Address: may be specified one or more times and contains one or more IP addresses (with an optional CIDR mask) to be set for the interface. diff --git a/src/tools/wg-quick/freebsd.bash b/src/tools/wg-quick/freebsd.bash index 6d698e2..cc3b8e6 100755 --- a/src/tools/wg-quick/freebsd.bash +++ b/src/tools/wg-quick/freebsd.bash @@ -38,10 +38,16 @@ die() { exit 1 } +CONFIG_SEARCH_PATHS=( /etc/wireguard /usr/local/etc/wireguard ) + parse_options() { - local interface_section=0 line key value stripped + local interface_section=0 line key value stripped path CONFIG_FILE="$1" - [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]] && CONFIG_FILE="/etc/wireguard/$CONFIG_FILE.conf" + if [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]]; then + for path in "${CONFIG_SEARCH_PATHS[@]}"; do + [[ -e $path/$CONFIG_FILE.conf ]] && { CONFIG_FILE="$path/$CONFIG_FILE.conf"; break; } + done + fi [[ -e $CONFIG_FILE ]] || die "\`$CONFIG_FILE' does not exist" [[ $CONFIG_FILE =~ (^|/)([a-zA-Z0-9_=+.-]{1,15})\.conf$ ]] || die "The config file must be a valid interface name, followed by .conf" CONFIG_FILE="$(readlink -f "$CONFIG_FILE")" @@ -334,9 +340,11 @@ cmd_usage() { CONFIG_FILE is a configuration file, whose filename is the interface name followed by \`.conf'. Otherwise, INTERFACE is an interface name, with - configuration found at /etc/wireguard/INTERFACE.conf. It is to be readable - by wg(8)'s \`setconf' sub-command, with the exception of the following additions - to the [Interface] section, which are handled by $PROGRAM: + configuration found at: + ${CONFIG_SEARCH_PATHS[@]/%//INTERFACE.conf}. + It is to be readable by wg(8)'s \`setconf' sub-command, with the exception + of the following additions to the [Interface] section, which are handled + by $PROGRAM: - Address: may be specified one or more times and contains one or more IP addresses (with an optional CIDR mask) to be set for the interface. -- cgit v1.2.3-59-g8ed1b