Commit ce4bf3a3 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Make SSH config usable "as is" with recent SSH versions

The config filterer has several more rules (which required a switch to
awk)
parent 508cf1c1
Loading
Loading
Loading
Loading
+67 −19
Original line number Diff line number Diff line
@@ -7,8 +7,9 @@ if [ $PLATFORM = windows ]; then
	if ! has sed; then
		die "This script requires 'sed'"
	fi
	# Hope it's GNU sed?
	PLATFORM=gnu
	if ! has awk; then
		die "This script requires 'awk'"
	fi
fi


@@ -19,20 +20,10 @@ ssh_version() {
	return 1
}

config_sed() {
	case $PLATFORM in
		darwin|bsd) SED_WORD_START='[[:<:]]' SED_WORD_END='[[:>:]]' ;;
		gnu) case "$CURRENT_SHELL" in
			bash) SED_WORD_START='\b' SED_WORD_END='\b' ;;
			zsh|sh) SED_WORD_START='\\b' SED_WORD_END='\\b' ;;
		esac ;;
	esac
}

make_config() {
	[ -z "${SSH_CONFIG-}" ] || return 0

	ssh_version && config_sed || return 1
	ssh_version || return 1

	SSH_CONFIG=$HOME/.ssh/configs/$PLATFORM.$KERNEL.$SSH_MAJOR.$SSH_MINOR.config

@@ -48,9 +39,18 @@ make_config() {
	mkdir -p `dirname $SSH_CONFIG`

	cat ~/.ssh/config ~/.ssh/config.local 2>/dev/null |
		sed >"$SSH_CONFIG" "
		awk >"$SSH_CONFIG" "
			function remove_line() { sub(/^/, \"\#\"); }
			/^$/ || /^#/ { print \$0; next }

			`rule_match`
			`rule_unix_sockets`
			`rule_mux`
			`rule_mux_persist`
			`rule_canonisation`
			`rule_include`

			1
		"

	# Ensure the GPG agent is running if installed
@@ -62,19 +62,67 @@ make_config() {

# Filter rules

rule_match() {
	# Match requires >=6.5
	# Replace with a match-all (Host *) or match-none (default, remove upto the
	# next Match or Host directive) depending on #default: directive
	[ $SSH_MAJOR -gt 6 ] && return
	[ $SSH_MAJOR -eq 6 ] && [ $SSH_MINOR -ge 5 ] && return

	cat <<-'END_AWK'
		BEGIN { is_match=0 }
		match($1, /^Match$/) {
		  if ( match($0, /#default:[[:space:]]*match-all/) ) {
		    sub(/^.*$/, "Host *")
		  } else {
		    is_match=1
		  }
		}
		match($1, /^Host$/) { is_match=0 }
		is_match { remove_line() }
	END_AWK
}

rule_unix_sockets() {
	# Windows has no unix sockets so disable shared connections
	# (Control* settings)
	if [ $KERNEL = nt ]; then
		echo '/^\s*Control.*/d;'
		echo 'match($1, /^Control/) { print $1 > "/dev/stderr"; remove_line() }'
	fi
}

rule_mux() {
	# ControlMaster & ControlPath require >=3.9
	[ $SSH_MAJOR -gt 3 ] && return
	[ $SSH_MAJOR -eq 3 ] && [ $SSH_MINOR -ge 9 ] && return
	echo 'match($1, /^Control(Master|Path)/) { remove_line() }'
}

rule_mux_persist() {
	# ControlPersist requires >=5.6
	[ $SSH_MAJOR -gt 5 ] && return
	[ $SSH_MAJOR -eq 5 ] && [ $SSH_MINOR -ge 6 ] && return
	echo 'match($1, /^ControlPersist$/) { remove_line() }'
}

rule_canonisation() {
	# OpenSSH < 6.6 does not have name canonisation
	# (Canonical* settings)
	if [ $SSH_MAJOR -gt 6 ]; then return; fi
	if [ $SSH_MAJOR -lt 6 ] || [ $SSH_MINOR -lt 6 ]; then
		echo '/Canonical/d;'
	fi
	[ $SSH_MAJOR -gt 6 ] && return
	[ $SSH_MAJOR -eq 6 ] && [ $SSH_MINOR -ge 6 ] && return
	echo 'match($1, /^Canonical/) { remove_line() }'
}

rule_include() {
	# Include requires >=7.2
	# If including config.local, remove as it is concatenated by this script
	cat <<-'END_AWK'
		match($1, /^Include$/) {
		  gsub("(~/.ssh/)?config.local", "")
		  if ( NF == 1 ) remove_line()
		}
	END_AWK
	[ $SSH_MAJOR -gt 7 ] && return
	[ $SSH_MAJOR -eq 7 ] && [ $SSH_MINOR -ge 2 ] && return
	echo 'match($1, /^Include$/) { remove_line() }'
}
+12 −1
Original line number Diff line number Diff line
HashKnownHosts no
GSSAPIAuthentication no

## REQUIRES: (Canonical*) 6.6
CanonicalizeHostname yes
CanonicalDomains net.kodo.org.uk lw.kodo.org.uk kodo.org.uk actual-experience.com

@@ -27,7 +29,16 @@ Host 192.168.* 10.* *.lan *.local localhost

# === Connection Sharing ===
# Use connection sharing for any host that does not explicitly block it.
Host *

## REQUIRES: (Match exec) 6.5
## REQUIRES: (ControlMaster, ControlPath) 3.9
## REQUIRES: (ControlPersist) 5.6
Match exec "test ${PLATFORM:-unknown} != windows" #default: match-all
	ControlMaster auto
	ControlPath %d/.ssh/socks:%l/%r@%h:%p
	ControlPersist 2h

# === Local Includes ===
## REQUIRES: (Include) 7.2
Host *
	Include config.local