From 414c4c0a30892543c041c0131fdfe13aedf7d491 Mon Sep 17 00:00:00 2001 From: Nahum Shalman Date: Mon, 4 Jul 2022 13:38:26 +0000 Subject: [PATCH 1/3] Makefile: add LDLIBS for sunos --- src/Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Makefile b/src/Makefile index 0533910b..134dffaf 100644 --- a/src/Makefile +++ b/src/Makefile @@ -58,6 +58,9 @@ endif ifeq ($(PLATFORM),haiku) LDLIBS += -lnetwork -lbsd endif +ifeq ($(PLATFORM),sunos) +LDLIBS += -lnsl -lsocket +endif ifeq ($(PLATFORM),windows) CC := x86_64-w64-mingw32-clang WINDRES := $(shell $(CC) $(CFLAGS) -print-prog-name=windres 2>/dev/null) From 5efe8dd4e2c739faea12800281d0fddbb9ee82ef Mon Sep 17 00:00:00 2001 From: Nahum Shalman Date: Mon, 4 Jul 2022 14:36:20 +0000 Subject: [PATCH 2/3] copy openbsd.bash to sunos.bash --- src/wg-quick/sunos.bash | 481 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 481 insertions(+) create mode 100755 src/wg-quick/sunos.bash diff --git a/src/wg-quick/sunos.bash b/src/wg-quick/sunos.bash new file mode 100755 index 00000000..2adfe460 --- /dev/null +++ b/src/wg-quick/sunos.bash @@ -0,0 +1,481 @@ +#!/usr/local/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2015-2020 Jason A. Donenfeld . All Rights Reserved. +# + +set -e -o pipefail +shopt -s extglob +export LC_ALL=C + +exec 3>&2 +SELF="$(readlink -f "${BASH_SOURCE[0]}")" +export PATH="${SELF%/*}:$PATH" + +WG_CONFIG="" +INTERFACE="" +ADDRESSES=( ) +MTU="" +DNS=( ) +DNS_SEARCH=( ) +TABLE="" +PRE_UP=( ) +POST_UP=( ) +PRE_DOWN=( ) +POST_DOWN=( ) +SAVE_CONFIG=0 +CONFIG_FILE="" +PROGRAM="${0##*/}" +ARGS=( "$@" ) + +cmd() { + echo "[#] $*" >&3 + "$@" +} + +die() { + echo "$PROGRAM: $*" >&2 + exit 1 +} + +parse_options() { + local interface_section=0 line key value stripped + CONFIG_FILE="$1" + [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]] && CONFIG_FILE="/etc/wireguard/$CONFIG_FILE.conf" + [[ -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")" + ((($(stat -f '0%#p' "$CONFIG_FILE") & $(stat -f '0%#p' "${CONFIG_FILE%/*}") & 0007) == 0)) || echo "Warning: \`$CONFIG_FILE' is world accessible" >&2 + INTERFACE="${BASH_REMATCH[2]}" + shopt -s nocasematch + while read -r line || [[ -n $line ]]; do + stripped="${line%%\#*}" + key="${stripped%%=*}"; key="${key##*([[:space:]])}"; key="${key%%*([[:space:]])}" + value="${stripped#*=}"; value="${value##*([[:space:]])}"; value="${value%%*([[:space:]])}" + [[ $key == "["* ]] && interface_section=0 + [[ $key == "[Interface]" ]] && interface_section=1 + if [[ $interface_section -eq 1 ]]; then + case "$key" in + Address) ADDRESSES+=( ${value//,/ } ); continue ;; + MTU) MTU="$value"; continue ;; + DNS) for v in ${value//,/ }; do + [[ $v =~ (^[0-9.]+$)|(^.*:.*$) ]] && DNS+=( $v ) || DNS_SEARCH+=( $v ) + done; continue ;; + Table) TABLE="$value"; continue ;; + PreUp) PRE_UP+=( "$value" ); continue ;; + PreDown) PRE_DOWN+=( "$value" ); continue ;; + PostUp) POST_UP+=( "$value" ); continue ;; + PostDown) POST_DOWN+=( "$value" ); continue ;; + SaveConfig) read_bool SAVE_CONFIG "$value"; continue ;; + esac + fi + WG_CONFIG+="$line"$'\n' + done < "$CONFIG_FILE" + shopt -u nocasematch +} + +read_bool() { + case "$2" in + true) printf -v "$1" 1 ;; + false) printf -v "$1" 0 ;; + *) die "\`$2' is neither true nor false" + esac +} + +auto_su() { + [[ $UID == 0 ]] || exec doas -- "$BASH" -- "$SELF" "${ARGS[@]}" +} + + +get_real_interface() { + local interface line + while IFS= read -r line; do + if [[ $line =~ ^([a-z]+[0-9]+):\ .+ ]]; then + interface="${BASH_REMATCH[1]}" + continue + fi + if [[ $interface == wg* && $line =~ ^\ description:\ wg-quick:\ (.+) && ${BASH_REMATCH[1]} == "$INTERFACE" ]]; then + REAL_INTERFACE="$interface" + return 0 + fi + done < <(ifconfig) + return 1 +} + +add_if() { + while true; do + local -A existing_ifs="( $(wg show interfaces | sed 's/\([^ ]*\)/[\1]=1/g') )" + local index ret + for ((index=0; index <= 2147483647; ++index)); do [[ -v existing_ifs[wg$index] ]] || break; done + if ret="$(cmd ifconfig wg$index create description "wg-quick: $INTERFACE" 2>&1)"; then + REAL_INTERFACE="wg$index" + return 0 + fi + [[ $ret == *"ifconfig: SIOCIFCREATE: File exists"* ]] && continue + echo "$ret" >&3 + return 1 + done +} + +del_routes() { + local todelete=( ) destination gateway netif + [[ -n $REAL_INTERFACE ]] || return 0 + while read -r destination _ _ _ _ netif _; do + [[ $netif == "$REAL_INTERFACE" ]] && todelete+=( "$destination" ) + done < <(netstat -nr -f inet) + for destination in "${todelete[@]}"; do + cmd route -q -n delete -inet "$destination" || true + done + todelete=( ) + while read -r destination gateway _ netif; do + [[ $netif == "$REAL_INTERFACE" || ( $netif == lo* && $gateway == "$REAL_INTERFACE" ) ]] && todelete+=( "$destination" ) + done < <(netstat -nr -f inet6) + for destination in "${todelete[@]}"; do + cmd route -q -n delete -inet6 "$destination" || true + done + for destination in "${ENDPOINTS[@]}"; do + if [[ $destination == *:* ]]; then + cmd route -q -n delete -inet6 "$destination" || true + else + cmd route -q -n delete -inet "$destination" || true + fi + done +} + +del_if() { + unset_dns + [[ -n $REAL_INTERFACE ]] && cmd ifconfig $REAL_INTERFACE destroy +} + +up_if() { + cmd ifconfig "$REAL_INTERFACE" up +} + +add_addr() { + local family + if [[ $1 == *:* ]]; then + family=inet6 + [[ -n $FIRSTADDR6 ]] || FIRSTADDR6="${1%/*}" + else + family=inet + [[ -n $FIRSTADDR4 ]] || FIRSTADDR4="${1%/*}" + fi + cmd ifconfig "$REAL_INTERFACE" $family "$1" alias +} + +set_mtu() { + local mtu=0 endpoint output family + if [[ -n $MTU ]]; then + cmd ifconfig "$REAL_INTERFACE" mtu "$MTU" + return + fi + while read -r _ endpoint; do + [[ $endpoint =~ ^\[?([a-z0-9:.]+)\]?:[0-9]+$ ]] || continue + family=inet + [[ ${BASH_REMATCH[1]} == *:* ]] && family=inet6 + output="$(route -n get "-$family" "${BASH_REMATCH[1]}" || true)" + [[ $output =~ interface:\ ([^ ]+)$'\n' && $(ifconfig "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}" + done < <(wg show "$REAL_INTERFACE" endpoints) + if [[ $mtu -eq 0 ]]; then + read -r output < <(route -n get default || true) || true + [[ $output =~ interface:\ ([^ ]+)$'\n' && $(ifconfig "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}" + fi + [[ $mtu -gt 0 ]] || mtu=1500 + cmd ifconfig "$REAL_INTERFACE" mtu $(( mtu - 80 )) +} + + +collect_gateways() { + local destination gateway + + GATEWAY4="" + while read -r destination gateway _; do + [[ $destination == default ]] || continue + GATEWAY4="$gateway" + break + done < <(netstat -nr -f inet) + + GATEWAY6="" + while read -r destination gateway _; do + [[ $destination == default ]] || continue + GATEWAY6="$gateway" + break + done < <(netstat -nr -f inet6) +} + +collect_endpoints() { + ENDPOINTS=( ) + while read -r _ endpoint; do + [[ $endpoint =~ ^\[?([a-z0-9:.]+)\]?:[0-9]+$ ]] || continue + ENDPOINTS+=( "${BASH_REMATCH[1]}" ) + done < <(wg show "$REAL_INTERFACE" endpoints) +} + +set_endpoint_direct_route() { + local old_endpoints endpoint old_gateway4 old_gateway6 remove_all_old=0 added=( ) + old_endpoints=( "${ENDPOINTS[@]}" ) + old_gateway4="$GATEWAY4" + old_gateway6="$GATEWAY6" + collect_gateways + collect_endpoints + + [[ $old_gateway4 != "$GATEWAY4" || $old_gateway6 != "$GATEWAY6" ]] && remove_all_old=1 + + if [[ $remove_all_old -eq 1 ]]; then + for endpoint in "${ENDPOINTS[@]}"; do + [[ " ${old_endpoints[*]} " == *" $endpoint "* ]] || old_endpoints+=( "$endpoint" ) + done + fi + + for endpoint in "${old_endpoints[@]}"; do + [[ $remove_all_old -eq 0 && " ${ENDPOINTS[*]} " == *" $endpoint "* ]] && continue + if [[ $endpoint == *:* && $AUTO_ROUTE6 -eq 1 ]]; then + cmd route -q -n delete -inet6 "$endpoint" 2>/dev/null || true + elif [[ $AUTO_ROUTE4 -eq 1 ]]; then + cmd route -q -n delete -inet "$endpoint" 2>/dev/null || true + fi + done + + for endpoint in "${ENDPOINTS[@]}"; do + if [[ $remove_all_old -eq 0 && " ${old_endpoints[*]} " == *" $endpoint "* ]]; then + added+=( "$endpoint" ) + continue + fi + if [[ $endpoint == *:* && $AUTO_ROUTE6 -eq 1 ]]; then + if [[ -n $GATEWAY6 ]]; then + cmd route -q -n add -inet6 "$endpoint" -gateway "$GATEWAY6" || true + else + # Prevent routing loop + cmd route -q -n add -inet6 "$endpoint" ::1 -blackhole || true + fi + added+=( "$endpoint" ) + elif [[ $AUTO_ROUTE4 -eq 1 ]]; then + if [[ -n $GATEWAY4 ]]; then + cmd route -q -n add -inet "$endpoint" -gateway "$GATEWAY4" || true + else + # Prevent routing loop + cmd route -q -n add -inet "$endpoint" 127.0.0.1 -blackhole || true + fi + added+=( "$endpoint" ) + fi + done + ENDPOINTS=( "${added[@]}" ) +} + +monitor_daemon() { + echo "[+] Backgrounding route monitor" >&2 + (trap 'del_routes; exit 0' INT TERM EXIT + exec >/dev/null 2>&1 + exec 19< <(exec route -n monitor) + local event pid=$! + # TODO: this should also check to see if the endpoint actually changes + # in response to incoming packets, and then call set_endpoint_direct_route + # then too. That function should be able to gracefully cleanup if the + # endpoints change. + while read -u 19 -r event; do + [[ $event == RTM_* ]] || continue + ifconfig "$REAL_INTERFACE" >/dev/null 2>&1 || break + [[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route + # TODO: set the mtu as well, but only if up + done + kill $pid) & disown +} + +set_dns() { + [[ ${#DNS[@]} -gt 0 ]] || return 0 + + # TODO: add exclusive support for nameservers + if pgrep -qx unwind; then + echo "[!] WARNING: unwind will leak DNS queries" >&2 + elif pgrep -qx resolvd; then + echo "[!] WARNING: resolvd may leak DNS queries" >&2 + else + echo "[+] resolvd is not running, DNS will not be configured" >&2 + return 0 + fi + + cmd cp /etc/resolv.conf "/etc/resolv.conf.wg-quick-backup.$INTERFACE" + [[ ${#DNS_SEARCH[@]} -eq 0 ]] || cmd printf 'search %s\n' "${DNS_SEARCH[*]}" > /etc/resolv.conf + route nameserver ${REAL_INTERFACE} ${DNS[@]} +} + +unset_dns() { + [[ -f "/etc/resolv.conf.wg-quick-backup.$INTERFACE" ]] || return 0 + route nameserver ${REAL_INTERFACE} + cmd mv "/etc/resolv.conf.wg-quick-backup.$INTERFACE" /etc/resolv.conf +} + +add_route() { + [[ $TABLE != off ]] || return 0 + local family ifaceroute + + if [[ $1 == *:* ]]; then + family=inet6 + [[ -n $FIRSTADDR6 ]] || die "Local IPv6 address must be set to have routes" + ifaceroute="$FIRSTADDR6" + else + family=inet + [[ -n $FIRSTADDR4 ]] || die "Local IPv4 address must be set to have routes" + ifaceroute="$FIRSTADDR4" + fi + + if [[ -n $TABLE && $TABLE != auto ]]; then + cmd route -q -n -T "$TABLE" add "-$family" "$1" -iface "$ifaceroute" + elif [[ $1 == */0 ]]; then + if [[ $1 == *:* ]]; then + AUTO_ROUTE6=1 + cmd route -q -n add -inet6 ::/1 -iface "$ifaceroute" + cmd route -q -n add -inet6 8000::/1 -iface "$ifaceroute" + else + AUTO_ROUTE4=1 + cmd route -q -n add -inet 0.0.0.0/1 -iface "$ifaceroute" + cmd route -q -n add -inet 128.0.0.0/1 -iface "$ifaceroute" + fi + else + [[ $(route -n get "-$family" "$1" 2>/dev/null) =~ interface:\ $REAL_INTERFACE$'\n' ]] || cmd route -q -n add "-$family" "$1" -iface "$ifaceroute" + fi +} + +set_config() { + cmd wg setconf "$REAL_INTERFACE" <(echo "$WG_CONFIG") +} + +save_config() { + local old_umask new_config current_config address network cmd + new_config=$'[Interface]\n' + { read -r _; while read -r _ _ network address _; do + [[ $network == *Link* ]] || new_config+="Address = $address"$'\n' + done } < <(netstat -I "$REAL_INTERFACE" -n -v) + # TODO: actually determine current DNS for interface + for address in "${DNS[@]}"; do + new_config+="DNS = $address"$'\n' + done + [[ -n $MTU ]] && new_config+="MTU = $MTU"$'\n' + [[ -n $TABLE ]] && new_config+="Table = $TABLE"$'\n' + [[ $SAVE_CONFIG -eq 0 ]] || new_config+=$'SaveConfig = true\n' + for cmd in "${PRE_UP[@]}"; do + new_config+="PreUp = $cmd"$'\n' + done + for cmd in "${POST_UP[@]}"; do + new_config+="PostUp = $cmd"$'\n' + done + for cmd in "${PRE_DOWN[@]}"; do + new_config+="PreDown = $cmd"$'\n' + done + for cmd in "${POST_DOWN[@]}"; do + new_config+="PostDown = $cmd"$'\n' + done + old_umask="$(umask)" + umask 077 + current_config="$(cmd wg showconf "$REAL_INTERFACE")" + trap 'rm -f "$CONFIG_FILE.tmp"; exit' INT TERM EXIT + echo "${current_config/\[Interface\]$'\n'/$new_config}" > "$CONFIG_FILE.tmp" || die "Could not write configuration file" + sync "$CONFIG_FILE.tmp" + mv "$CONFIG_FILE.tmp" "$CONFIG_FILE" || die "Could not move configuration file" + trap - INT TERM EXIT + umask "$old_umask" +} + +execute_hooks() { + local hook + for hook in "$@"; do + hook="${hook//%i/$REAL_INTERFACE}" + hook="${hook//%I/$INTERFACE}" + echo "[#] $hook" >&2 + (eval "$hook") + done +} + +cmd_usage() { + cat >&2 <<-_EOF + Usage: $PROGRAM [ up | down | save | strip ] [ CONFIG_FILE | INTERFACE ] + + 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: + + - 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. + - DNS: an optional DNS server to use while the device is up. + - MTU: an optional MTU for the interface; if unspecified, auto-calculated. + - Table: an optional routing table to which routes will be added; if + unspecified or \`auto', the default table is used. If \`off', no routes + are added. + - PreUp, PostUp, PreDown, PostDown: script snippets which will be executed + by bash(1) at the corresponding phases of the link, most commonly used + to configure DNS. The string \`%i' is expanded to INTERFACE. + - SaveConfig: if set to \`true', the configuration is saved from the current + state of the interface upon shutdown. + + See wg-quick(8) for more info and examples. + _EOF +} + +cmd_up() { + local i + get_real_interface && die "\`$INTERFACE' already exists as \`$REAL_INTERFACE'" + trap 'del_if; del_routes; exit' INT TERM EXIT + execute_hooks "${PRE_UP[@]}" + add_if + set_config + for i in "${ADDRESSES[@]}"; do + add_addr "$i" + done + set_mtu + up_if + set_dns + for i in $(while read -r _ i; do for i in $i; do [[ $i =~ ^[0-9a-z:.]+/[0-9]+$ ]] && echo "$i"; done; done < <(wg show "$REAL_INTERFACE" allowed-ips) | sort -nr -k 2 -t /); do + add_route "$i" + done + [[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route + monitor_daemon + execute_hooks "${POST_UP[@]}" + trap - INT TERM EXIT +} + +cmd_down() { + get_real_interface || die "\`$INTERFACE' is not a WireGuard interface" + execute_hooks "${PRE_DOWN[@]}" + [[ $SAVE_CONFIG -eq 0 ]] || save_config + del_if + unset_dns + execute_hooks "${POST_DOWN[@]}" +} + +cmd_save() { + get_real_interface || die "\`$INTERFACE' is not a WireGuard interface" + save_config +} + +cmd_strip() { + echo "$WG_CONFIG" +} + +# ~~ function override insertion point ~~ + +if [[ $# -eq 1 && ( $1 == --help || $1 == -h || $1 == help ) ]]; then + cmd_usage +elif [[ $# -eq 2 && $1 == up ]]; then + auto_su + parse_options "$2" + cmd_up +elif [[ $# -eq 2 && $1 == down ]]; then + auto_su + parse_options "$2" + cmd_down +elif [[ $# -eq 2 && $1 == save ]]; then + auto_su + parse_options "$2" + cmd_save +elif [[ $# -eq 2 && $1 == strip ]]; then + auto_su + parse_options "$2" + cmd_strip +else + cmd_usage + exit 1 +fi + +exit 0 From 77de1e949f7e754b28f5b258c0176718c63741a5 Mon Sep 17 00:00:00 2001 From: Nahum Shalman Date: Tue, 5 Jul 2022 02:42:14 +0000 Subject: [PATCH 3/3] Basic SunOS support --- contrib/smf/README.md | 28 +++++++++++++ contrib/smf/wg-quick.xml | 41 ++++++++++++++++++ src/wg-quick/sunos.bash | 91 +++++++++++++++++++--------------------- 3 files changed, 111 insertions(+), 49 deletions(-) create mode 100644 contrib/smf/README.md create mode 100644 contrib/smf/wg-quick.xml diff --git a/contrib/smf/README.md b/contrib/smf/README.md new file mode 100644 index 00000000..43a29994 --- /dev/null +++ b/contrib/smf/README.md @@ -0,0 +1,28 @@ +WireGuard for SMF +================= + +The example `wg-quick.xml` file may be used for running wg-quick(8) +using SMF. + +Usage +----- + +Choose a name for your wg-quick config. It can be pretty much any simple string. +It will be used for naming your config file and your SMF service instance name. +The underlying tun device will be allocated dynamically. + +Create `/etc/wireguard/.conf` + +``` +# svccfg import wg-quick.xml +# svccfg -s svc:/vpn/wg-quick add something +# svcadm enable wg-quick:something +``` + +Helpful references +------------------ + +- https://blogs.warwick.ac.uk/chrismay/entry/solaris_smf_manifest/ +- (archive) https://web.archive.org/web/20210613085120/https://blogs.warwick.ac.uk/chrismay/entry/solaris_smf_manifest/ +- https://github.com/omniosorg/omnios-extra/blob/800489a/build/wireguard-tools/files/wg-quick.xml +- https://github.com/TritonDataCenter/pkgsrc-extra/blob/4423c14/wireguard-tools/files/smf/manifest.xml diff --git a/contrib/smf/wg-quick.xml b/contrib/smf/wg-quick.xml new file mode 100644 index 00000000..90df6f4d --- /dev/null +++ b/contrib/smf/wg-quick.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/wg-quick/sunos.bash b/src/wg-quick/sunos.bash index 2adfe460..73049186 100755 --- a/src/wg-quick/sunos.bash +++ b/src/wg-quick/sunos.bash @@ -1,14 +1,13 @@ -#!/usr/local/bin/bash +#!/usr/bin/env bash # SPDX-License-Identifier: GPL-2.0 # -# Copyright (C) 2015-2020 Jason A. Donenfeld . All Rights Reserved. +# Copyright (C) 2015-2023 Jason A. Donenfeld . All Rights Reserved. # set -e -o pipefail shopt -s extglob export LC_ALL=C -exec 3>&2 SELF="$(readlink -f "${BASH_SOURCE[0]}")" export PATH="${SELF%/*}:$PATH" @@ -29,7 +28,7 @@ PROGRAM="${0##*/}" ARGS=( "$@" ) cmd() { - echo "[#] $*" >&3 + echo "[#] $*" >&2 "$@" } @@ -45,7 +44,7 @@ parse_options() { [[ -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")" - ((($(stat -f '0%#p' "$CONFIG_FILE") & $(stat -f '0%#p' "${CONFIG_FILE%/*}") & 0007) == 0)) || echo "Warning: \`$CONFIG_FILE' is world accessible" >&2 + ((($(stat -c '0%#a' "$CONFIG_FILE") & $(stat -c '0%#a' "${CONFIG_FILE%/*}") & 0007) == 0)) || echo "Warning: \`$CONFIG_FILE' is world accessible" >&2 INTERFACE="${BASH_REMATCH[2]}" shopt -s nocasematch while read -r line || [[ -n $line ]]; do @@ -83,37 +82,32 @@ read_bool() { } auto_su() { - [[ $UID == 0 ]] || exec doas -- "$BASH" -- "$SELF" "${ARGS[@]}" + # Other platforms appear to escalate priviliges silently. + # We will just require root permissions. + [[ $UID == 0 ]] || die "must be run as root" } - get_real_interface() { - local interface line - while IFS= read -r line; do - if [[ $line =~ ^([a-z]+[0-9]+):\ .+ ]]; then - interface="${BASH_REMATCH[1]}" - continue - fi - if [[ $interface == wg* && $line =~ ^\ description:\ wg-quick:\ (.+) && ${BASH_REMATCH[1]} == "$INTERFACE" ]]; then - REAL_INTERFACE="$interface" - return 0 - fi - done < <(ifconfig) - return 1 + local interface diff + wg show interfaces >/dev/null + [[ -f "/var/run/wireguard/$INTERFACE.name" ]] || return 1 + interface="$(< "/var/run/wireguard/$INTERFACE.name")" + [[ -n $interface && -S "/var/run/wireguard/$interface.sock" ]] || return 1 + diff=$(( $(stat -c %Y "/var/run/wireguard/$interface.sock" 2>/dev/null || echo 200) - $(stat -c %Y "/var/run/wireguard/$INTERFACE.name" 2>/dev/null || echo 100) )) + [[ $diff -ge 2 || $diff -le -2 ]] && return 1 + REAL_INTERFACE="$interface" + echo "[+] Interface for $INTERFACE is $REAL_INTERFACE" >&2 + return 0 } add_if() { - while true; do - local -A existing_ifs="( $(wg show interfaces | sed 's/\([^ ]*\)/[\1]=1/g') )" - local index ret - for ((index=0; index <= 2147483647; ++index)); do [[ -v existing_ifs[wg$index] ]] || break; done - if ret="$(cmd ifconfig wg$index create description "wg-quick: $INTERFACE" 2>&1)"; then - REAL_INTERFACE="wg$index" - return 0 - fi - [[ $ret == *"ifconfig: SIOCIFCREATE: File exists"* ]] && continue - echo "$ret" >&3 - return 1 + export WG_TUN_NAME_FILE="/var/run/wireguard/$INTERFACE.name" + mkdir -p "/var/run/wireguard/" + cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-wireguard-go}" tun + cmd sleep 0.1 + get_real_interface + until ipadm show-if $REAL_INTERFACE 2>/dev/null | grep -q $REAL_INTERFACE; do + cmd sleep 0.1 done } @@ -143,12 +137,18 @@ del_routes() { } del_if() { + local addr unset_dns - [[ -n $REAL_INTERFACE ]] && cmd ifconfig $REAL_INTERFACE destroy + [[ -n $REAL_INTERFACE ]] && ipadm show-addr -p -o addrobj | grep $REAL_INTERFACE/ | while read addr; do + cmd ipadm delete-addr $addr + done + [[ -z $REAL_INTERFACE ]] || cmd rm -f "/var/run/wireguard/$REAL_INTERFACE.sock" + cmd rm -f "/var/run/wireguard/$INTERFACE.name" } up_if() { - cmd ifconfig "$REAL_INTERFACE" up + # Due to illumos tun driver quirks there is nothing to do here. + true } add_addr() { @@ -160,7 +160,9 @@ add_addr() { family=inet [[ -n $FIRSTADDR4 ]] || FIRSTADDR4="${1%/*}" fi - cmd ifconfig "$REAL_INTERFACE" $family "$1" alias + # cleanup just in case + ipadm delete-addr "$REAL_INTERFACE/$2" &>/dev/null || true + cmd ipadm create-addr -t -T static -a "local=$1,remote=${1%/*}" "$REAL_INTERFACE/$2" } set_mtu() { @@ -283,25 +285,14 @@ monitor_daemon() { set_dns() { [[ ${#DNS[@]} -gt 0 ]] || return 0 - - # TODO: add exclusive support for nameservers - if pgrep -qx unwind; then - echo "[!] WARNING: unwind will leak DNS queries" >&2 - elif pgrep -qx resolvd; then - echo "[!] WARNING: resolvd may leak DNS queries" >&2 - else - echo "[+] resolvd is not running, DNS will not be configured" >&2 - return 0 - fi - cmd cp /etc/resolv.conf "/etc/resolv.conf.wg-quick-backup.$INTERFACE" - [[ ${#DNS_SEARCH[@]} -eq 0 ]] || cmd printf 'search %s\n' "${DNS_SEARCH[*]}" > /etc/resolv.conf - route nameserver ${REAL_INTERFACE} ${DNS[@]} + { cmd printf 'nameserver %s\n' "${DNS[@]}" + [[ ${#DNS_SEARCH[@]} -eq 0 ]] || cmd printf 'search %s\n' "${DNS_SEARCH[*]}" + } > /etc/resolv.conf } unset_dns() { [[ -f "/etc/resolv.conf.wg-quick-backup.$INTERFACE" ]] || return 0 - route nameserver ${REAL_INTERFACE} cmd mv "/etc/resolv.conf.wg-quick-backup.$INTERFACE" /etc/resolv.conf } @@ -415,14 +406,16 @@ cmd_usage() { cmd_up() { local i + local n=0 get_real_interface && die "\`$INTERFACE' already exists as \`$REAL_INTERFACE'" trap 'del_if; del_routes; exit' INT TERM EXIT execute_hooks "${PRE_UP[@]}" add_if - set_config for i in "${ADDRESSES[@]}"; do - add_addr "$i" + add_addr "$i" "$INTERFACE$n" + n=$((n + 1)) done + set_config set_mtu up_if set_dns