wg-quick: support FreeBSD/Darwin search path
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
		
							parent
							
								
									b818e71ba5
								
							
						
					
					
						commit
						d40231c766
					
				
					 4 changed files with 39 additions and 16 deletions
				
			
		| 
						 | 
					@ -2,17 +2,23 @@
 | 
				
			||||||
# Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 | 
					# Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_wg_quick_completion() {
 | 
					_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
 | 
						if [[ $COMP_CWORD -eq 1 ]]; then
 | 
				
			||||||
		COMPREPLY+=( $(compgen -W "up down" -- "${COMP_WORDS[1]}") )
 | 
							COMPREPLY+=( $(compgen -W "up down" -- "${COMP_WORDS[1]}") )
 | 
				
			||||||
	elif [[ $COMP_CWORD -eq 2 ]]; then
 | 
						elif [[ $COMP_CWORD -eq 2 ]]; then
 | 
				
			||||||
		if [[ ${COMP_WORDS[1]} == up ]]; then
 | 
							if [[ ${COMP_WORDS[1]} == up ]]; then
 | 
				
			||||||
			local old_glob="$(shopt -p nullglob)"
 | 
								local old_glob="$(shopt -p nullglob)"
 | 
				
			||||||
			shopt -s nullglob
 | 
								shopt -s nullglob
 | 
				
			||||||
			for i in /etc/wireguard/*.conf; do
 | 
								for p in "${search_paths[@]}"; do
 | 
				
			||||||
				i="${i##*/}"; i="${i%.conf}"
 | 
									for i in "$p"/*.conf; do
 | 
				
			||||||
				mapfile -t a < <(compgen -W "$i" -- "${COMP_WORDS[2]}")
 | 
										i="${i##*/}"; i="${i%.conf}"
 | 
				
			||||||
				COMPREPLY+=( "${a[@]}" )
 | 
										mapfile -t a < <(compgen -W "$i" -- "${COMP_WORDS[2]}")
 | 
				
			||||||
 | 
										COMPREPLY+=( "${a[@]}" )
 | 
				
			||||||
 | 
									done
 | 
				
			||||||
			done
 | 
								done
 | 
				
			||||||
			eval "$old_glob"
 | 
								eval "$old_glob"
 | 
				
			||||||
			mapfile -t a < <(compgen -f -X '!*.conf' -- "${COMP_WORDS[2]}")
 | 
								mapfile -t a < <(compgen -f -X '!*.conf' -- "${COMP_WORDS[2]}")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -31,7 +31,8 @@ interface without bringing the interface down.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
\fICONFIG_FILE\fP is a configuration file, whose filename is the interface name
 | 
					\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
 | 
					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
 | 
					Generally speaking, this utility is just a simple script that wraps invocations to
 | 
				
			||||||
.BR wg (8)
 | 
					.BR wg (8)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -40,10 +40,16 @@ die() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[[ ${BASH_VERSINFO[0]} -ge 4 ]] || die "Version mismatch: bash ${BASH_VERSINFO[0]} detected, when bash 4+ required"
 | 
					[[ ${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() {
 | 
					parse_options() {
 | 
				
			||||||
	local interface_section=0 line key value stripped
 | 
						local interface_section=0 line key value stripped path
 | 
				
			||||||
	CONFIG_FILE="$1"
 | 
						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"
 | 
						[[ -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 =~ (^|/)([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##*/}"
 | 
						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
 | 
						  CONFIG_FILE is a configuration file, whose filename is the interface name
 | 
				
			||||||
	  followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
 | 
						  followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
 | 
				
			||||||
	  configuration found at /etc/wireguard/INTERFACE.conf. It is to be readable
 | 
						  configuration found at:
 | 
				
			||||||
	  by wg(8)'s \`setconf' sub-command, with the exception of the following additions
 | 
						  ${CONFIG_SEARCH_PATHS[@]/%//INTERFACE.conf}.
 | 
				
			||||||
	  to the [Interface] section, which are handled by $PROGRAM:
 | 
						  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
 | 
						  - 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.
 | 
						    IP addresses (with an optional CIDR mask) to be set for the interface.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -38,10 +38,16 @@ die() {
 | 
				
			||||||
	exit 1
 | 
						exit 1
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CONFIG_SEARCH_PATHS=( /etc/wireguard /usr/local/etc/wireguard )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
parse_options() {
 | 
					parse_options() {
 | 
				
			||||||
	local interface_section=0 line key value stripped
 | 
						local interface_section=0 line key value stripped path
 | 
				
			||||||
	CONFIG_FILE="$1"
 | 
						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"
 | 
						[[ -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 =~ (^|/)([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")"
 | 
						CONFIG_FILE="$(readlink -f "$CONFIG_FILE")"
 | 
				
			||||||
| 
						 | 
					@ -334,9 +340,11 @@ cmd_usage() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	  CONFIG_FILE is a configuration file, whose filename is the interface name
 | 
						  CONFIG_FILE is a configuration file, whose filename is the interface name
 | 
				
			||||||
	  followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
 | 
						  followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
 | 
				
			||||||
	  configuration found at /etc/wireguard/INTERFACE.conf. It is to be readable
 | 
						  configuration found at:
 | 
				
			||||||
	  by wg(8)'s \`setconf' sub-command, with the exception of the following additions
 | 
						  ${CONFIG_SEARCH_PATHS[@]/%//INTERFACE.conf}.
 | 
				
			||||||
	  to the [Interface] section, which are handled by $PROGRAM:
 | 
						  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
 | 
						  - 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.
 | 
						    IP addresses (with an optional CIDR mask) to be set for the interface.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue