All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: wireguard@lists.zx2c4.com, Daniel Kahn Gillmor <dkg@fifthhorseman.net>
Subject: [PATCH v2] wg-quick: use bind mount for DNS when no openresolv
Date: Thu, 26 Oct 2017 15:41:25 +0200	[thread overview]
Message-ID: <20171026134125.2946-1-Jason@zx2c4.com> (raw)
In-Reply-To: <20171026013240.13503-1-Jason@zx2c4.com>

Version 2 uses the mount namespace trick, so that we don't leave
anything dangling under any circumstances, and chroot envrionments still
have the ability to bind mount /etc/resolv.conf to their chroots.

---
 src/tools/wg-quick.bash | 40 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 37 insertions(+), 3 deletions(-)

diff --git a/src/tools/wg-quick.bash b/src/tools/wg-quick.bash
index def78af..b6903ec 100755
--- a/src/tools/wg-quick.bash
+++ b/src/tools/wg-quick.bash
@@ -83,6 +83,7 @@ add_if() {
 
 del_if() {
 	local fwmark
+	[[ $HAVE_SET_DNS -eq 0 ]] || unset_dns
 	fwmark="$(wg show "$INTERFACE" fwmark)"
 	DEFAULT_TABLE=0
 	[[ $fwmark != off ]] && DEFAULT_TABLE=$(( fwmark ))
@@ -130,12 +131,45 @@ set_mtu() {
 	cmd ip link set mtu $(( mtu - 80 )) dev "$INTERFACE"
 }
 
+HAVE_SET_DNS=0
 set_dns() {
-	[[ ${#DNS[@]} -eq 0 ]] || printf 'nameserver %s\n' "${DNS[@]}" | cmd resolvconf -a "tun.$INTERFACE" -m 0 -x
+	[[ ${#DNS[@]} -gt 0 ]] || return 0
+
+	if [[ $(resolvconf --version 2>/dev/null) == openresolv\ * ]]; then
+		printf 'nameserver %s\n' "${DNS[@]}" | cmd resolvconf -a "$INTERFACE" -m 0 -x
+	else
+		echo "[#] mount \`${DNS[*]}' /etc/resolv.conf" >&2
+		[[ -e /etc/resolv.conf ]] || touch /etc/resolv.conf
+		{ cat <<-_EOF
+			# This file was generated by wg-quick(8) for use with
+			# the WireGuard interface $INTERFACE. It cannot be
+			# removed or altered directly. You may remove this file
+			# by running \`wg-quick down $INTERFACE', or if that
+			# poses problems, run \`umount /etc/resolv.conf'.
+
+		_EOF
+		printf 'nameserver %s\n' "${DNS[@]}"
+		} | unshare -m --propagation shared bash -c "$(cat <<-_EOF
+			set -e
+			mount --make-private /tmp
+			mount -t tmpfs none /tmp
+			cat > /tmp/resolv.conf
+			mount -o remount,ro /tmp
+			mount -B /tmp/resolv.conf /etc/resolv.conf
+		_EOF
+		)"
+	fi
+	HAVE_SET_DNS=1
 }
 
 unset_dns() {
-	[[ ${#DNS[@]} -eq 0 ]] || cmd resolvconf -d "tun.$INTERFACE"
+	[[ ${#DNS[@]} -gt 0 ]] || return 0
+
+	if [[ $(resolvconf --version 2>/dev/null) == openresolv\ * ]]; then
+		cmd resolvconf -d "$INTERFACE"
+	else
+		cmd umount /etc/resolv.conf
+	fi
 }
 
 add_route() {
@@ -254,8 +288,8 @@ cmd_down() {
 	[[ " $(wg show interfaces) " == *" $INTERFACE "* ]] || die "\`$INTERFACE' is not a WireGuard interface"
 	execute_hooks "${PRE_DOWN[@]}"
 	[[ $SAVE_CONFIG -eq 0 ]] || save_config
-	unset_dns
 	del_if
+	unset_dns
 	execute_hooks "${POST_DOWN[@]}"
 }
 
-- 
2.14.2

  parent reply	other threads:[~2017-10-26 13:39 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-25 22:43 Fixing wg-quick's DNS= directive with a hatchet Jason A. Donenfeld
2017-10-25 23:37 ` Kalin KOZHUHAROV
2017-10-26  0:55   ` Jason A. Donenfeld
2017-10-26  1:32 ` [PATCH] wg-quick: use bind mount for DNS when no openresolv Jason A. Donenfeld
2017-10-26  1:53   ` Kalin KOZHUHAROV
2017-10-26  1:54     ` Jason A. Donenfeld
2017-10-26 13:41   ` Jason A. Donenfeld [this message]
2017-10-26  2:54 ` Fixing wg-quick's DNS= directive with a hatchet Eric Light
2017-10-26  3:21   ` Jason A. Donenfeld
2017-10-26 13:11 ` Jason A. Donenfeld
2017-10-26 16:56 ` Joe Doss
2017-10-26 17:24   ` Jason A. Donenfeld
2017-10-26 21:22   ` Jason A. Donenfeld
2017-10-27 10:07     ` Martin Hauke
2017-10-27 13:22       ` Jason A. Donenfeld
2017-10-27 14:47     ` Joe Doss
2017-10-27 14:51       ` Jason A. Donenfeld
2017-10-27 15:02         ` Jason A. Donenfeld
2017-10-27 15:38           ` Joe Doss
2017-10-27 22:04           ` Bruno Wolff III
2017-10-27 15:38         ` Joe Doss
2017-10-27 17:15           ` Jason A. Donenfeld
2017-10-27 17:52             ` Jason A. Donenfeld
2017-10-27 22:06             ` Daniel Kahn Gillmor
2017-10-28  2:24               ` Jason A. Donenfeld
2017-10-28  2:39                 ` Jason A. Donenfeld
2017-10-28 14:35                 ` Daniel Kahn Gillmor
2017-10-28 17:57                   ` Jason A. Donenfeld
2017-10-29 12:21                     ` Geo Kozey
2017-10-29 17:07                       ` Jason A. Donenfeld
2017-10-30 11:58                       ` Daniel Kahn Gillmor
2017-10-30 12:10                     ` Daniel Kahn Gillmor
2017-10-29 22:06                   ` Jason A. Donenfeld
2017-10-30 12:16                     ` Daniel Kahn Gillmor
2017-10-31 10:49 ` Jason A. Donenfeld

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171026134125.2946-1-Jason@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=dkg@fifthhorseman.net \
    --cc=wireguard@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.