1*19261079SEd Maste# $OpenBSD: sshcfgparse.sh,v 1.9 2021/06/08 07:05:27 dtucker Exp $ 2076ad2f8SDag-Erling Smørgrav# Placed in the Public Domain. 3076ad2f8SDag-Erling Smørgrav 4076ad2f8SDag-Erling Smørgravtid="ssh config parse" 5076ad2f8SDag-Erling Smørgrav 6*19261079SEd Mastedsa=0 7*19261079SEd Mastefor t in $SSH_KEYTYPES; do 8*19261079SEd Maste case "$t" in 9*19261079SEd Maste ssh-dss) dsa=1 ;; 10*19261079SEd Maste esac 11*19261079SEd Mastedone 12*19261079SEd Maste 13190cef3dSDag-Erling Smørgravexpect_result_present() { 14190cef3dSDag-Erling Smørgrav _str="$1" ; shift 15190cef3dSDag-Erling Smørgrav for _expect in "$@" ; do 16190cef3dSDag-Erling Smørgrav echo "$f" | tr ',' '\n' | grep "^$_expect\$" >/dev/null 17190cef3dSDag-Erling Smørgrav if test $? -ne 0 ; then 18190cef3dSDag-Erling Smørgrav fail "missing expected \"$_expect\" from \"$_str\"" 19190cef3dSDag-Erling Smørgrav fi 20190cef3dSDag-Erling Smørgrav done 21190cef3dSDag-Erling Smørgrav} 22190cef3dSDag-Erling Smørgravexpect_result_absent() { 23190cef3dSDag-Erling Smørgrav _str="$1" ; shift 24190cef3dSDag-Erling Smørgrav for _expect in "$@" ; do 25190cef3dSDag-Erling Smørgrav echo "$f" | tr ',' '\n' | grep "^$_expect\$" >/dev/null 26190cef3dSDag-Erling Smørgrav if test $? -eq 0 ; then 27190cef3dSDag-Erling Smørgrav fail "unexpected \"$_expect\" present in \"$_str\"" 28190cef3dSDag-Erling Smørgrav fi 29190cef3dSDag-Erling Smørgrav done 30190cef3dSDag-Erling Smørgrav} 31190cef3dSDag-Erling Smørgrav 32076ad2f8SDag-Erling Smørgravverbose "reparse minimal config" 33076ad2f8SDag-Erling Smørgrav(${SSH} -G -F $OBJ/ssh_config somehost >$OBJ/ssh_config.1 && 34076ad2f8SDag-Erling Smørgrav ${SSH} -G -F $OBJ/ssh_config.1 somehost >$OBJ/ssh_config.2 && 35*19261079SEd Maste diff $OBJ/ssh_config.1 $OBJ/ssh_config.2) || fail "failed to reparse minimal" 36076ad2f8SDag-Erling Smørgrav 37076ad2f8SDag-Erling Smørgravverbose "ssh -W opts" 38076ad2f8SDag-Erling Smørgravf=`${SSH} -GF $OBJ/ssh_config host | awk '/exitonforwardfailure/{print $2}'` 39076ad2f8SDag-Erling Smørgravtest "$f" = "no" || fail "exitonforwardfailure default" 40076ad2f8SDag-Erling Smørgravf=`${SSH} -GF $OBJ/ssh_config -W a:1 h | awk '/exitonforwardfailure/{print $2}'` 41076ad2f8SDag-Erling Smørgravtest "$f" = "yes" || fail "exitonforwardfailure enable" 42076ad2f8SDag-Erling Smørgravf=`${SSH} -GF $OBJ/ssh_config -W a:1 -o exitonforwardfailure=no h | \ 43076ad2f8SDag-Erling Smørgrav awk '/exitonforwardfailure/{print $2}'` 44076ad2f8SDag-Erling Smørgravtest "$f" = "no" || fail "exitonforwardfailure override" 45076ad2f8SDag-Erling Smørgrav 46076ad2f8SDag-Erling Smørgravf=`${SSH} -GF $OBJ/ssh_config host | awk '/clearallforwardings/{print $2}'` 47076ad2f8SDag-Erling Smørgravtest "$f" = "no" || fail "clearallforwardings default" 48076ad2f8SDag-Erling Smørgravf=`${SSH} -GF $OBJ/ssh_config -W a:1 h | awk '/clearallforwardings/{print $2}'` 49076ad2f8SDag-Erling Smørgravtest "$f" = "yes" || fail "clearallforwardings enable" 50076ad2f8SDag-Erling Smørgravf=`${SSH} -GF $OBJ/ssh_config -W a:1 -o clearallforwardings=no h | \ 51076ad2f8SDag-Erling Smørgrav awk '/clearallforwardings/{print $2}'` 52076ad2f8SDag-Erling Smørgravtest "$f" = "no" || fail "clearallforwardings override" 53076ad2f8SDag-Erling Smørgrav 54190cef3dSDag-Erling Smørgravverbose "user first match" 55190cef3dSDag-Erling Smørgravuser=`awk '$1=="User" {print $2}' $OBJ/ssh_config` 56190cef3dSDag-Erling Smørgravf=`${SSH} -GF $OBJ/ssh_config host | awk '/^user /{print $2}'` 57190cef3dSDag-Erling Smørgravtest "$f" = "$user" || fail "user from config, expected '$user' got '$f'" 58190cef3dSDag-Erling Smørgravf=`${SSH} -GF $OBJ/ssh_config -o user=foo -l bar baz@host | awk '/^user /{print $2}'` 59190cef3dSDag-Erling Smørgravtest "$f" = "foo" || fail "user first match -oUser, expected 'foo' got '$f' " 60190cef3dSDag-Erling Smørgravf=`${SSH} -GF $OBJ/ssh_config -lbar baz@host user=foo baz@host | awk '/^user /{print $2}'` 61190cef3dSDag-Erling Smørgravtest "$f" = "bar" || fail "user first match -l, expected 'bar' got '$f'" 62190cef3dSDag-Erling Smørgravf=`${SSH} -GF $OBJ/ssh_config baz@host -o user=foo -l bar baz@host | awk '/^user /{print $2}'` 63190cef3dSDag-Erling Smørgravtest "$f" = "baz" || fail "user first match user@host, expected 'baz' got '$f'" 64190cef3dSDag-Erling Smørgrav 65*19261079SEd Masteverbose "pubkeyacceptedalgorithms" 66190cef3dSDag-Erling Smørgrav# Default set 67*19261079SEd Mastef=`${SSH} -GF none host | awk '/^pubkeyacceptedalgorithms /{print $2}'` 68190cef3dSDag-Erling Smørgravexpect_result_present "$f" "ssh-ed25519" "ssh-ed25519-cert-v01.*" 69190cef3dSDag-Erling Smørgravexpect_result_absent "$f" "ssh-dss" 70190cef3dSDag-Erling Smørgrav# Explicit override 71*19261079SEd Mastef=`${SSH} -GF none -opubkeyacceptedalgorithms=ssh-ed25519 host | \ 72*19261079SEd Maste awk '/^pubkeyacceptedalgorithms /{print $2}'` 73190cef3dSDag-Erling Smørgravexpect_result_present "$f" "ssh-ed25519" 74190cef3dSDag-Erling Smørgravexpect_result_absent "$f" "ssh-ed25519-cert-v01.*" "ssh-dss" 75190cef3dSDag-Erling Smørgrav# Removal from default set 76*19261079SEd Mastef=`${SSH} -GF none -opubkeyacceptedalgorithms=-ssh-ed25519-cert* host | \ 77*19261079SEd Maste awk '/^pubkeyacceptedalgorithms /{print $2}'` 78190cef3dSDag-Erling Smørgravexpect_result_present "$f" "ssh-ed25519" 79190cef3dSDag-Erling Smørgravexpect_result_absent "$f" "ssh-ed25519-cert-v01.*" "ssh-dss" 80*19261079SEd Mastef=`${SSH} -GF none -opubkeyacceptedalgorithms=-ssh-ed25519 host | \ 81*19261079SEd Maste awk '/^pubkeyacceptedalgorithms /{print $2}'` 82190cef3dSDag-Erling Smørgravexpect_result_present "$f" "ssh-ed25519-cert-v01.*" 83190cef3dSDag-Erling Smørgravexpect_result_absent "$f" "ssh-ed25519" "ssh-dss" 84190cef3dSDag-Erling Smørgrav# Append to default set. 85*19261079SEd Maste# This is not tested when built !WITH_OPENSSL 86*19261079SEd Masteif [ "$dsa" = "1" ]; then 87*19261079SEd Maste f=`${SSH} -GF none -opubkeyacceptedalgorithms=+ssh-dss-cert* host | \ 88*19261079SEd Maste awk '/^pubkeyacceptedalgorithms /{print $2}'` 89190cef3dSDag-Erling Smørgrav expect_result_present "$f" "ssh-ed25519" "ssh-dss-cert-v01.*" 90190cef3dSDag-Erling Smørgrav expect_result_absent "$f" "ssh-dss" 91*19261079SEd Maste f=`${SSH} -GF none -opubkeyacceptedalgorithms=+ssh-dss host | \ 92*19261079SEd Maste awk '/^pubkeyacceptedalgorithms /{print $2}'` 93190cef3dSDag-Erling Smørgrav expect_result_present "$f" "ssh-ed25519" "ssh-ed25519-cert-v01.*" "ssh-dss" 94190cef3dSDag-Erling Smørgrav expect_result_absent "$f" "ssh-dss-cert-v01.*" 95*19261079SEd Mastefi 96*19261079SEd Maste 97*19261079SEd Masteverbose "agentforwarding" 98*19261079SEd Mastef=`${SSH} -GF none host | awk '/^forwardagent /{print$2}'` 99*19261079SEd Masteexpect_result_present "$f" "no" 100*19261079SEd Mastef=`${SSH} -GF none -oforwardagent=no host | awk '/^forwardagent /{print$2}'` 101*19261079SEd Masteexpect_result_present "$f" "no" 102*19261079SEd Mastef=`${SSH} -GF none -oforwardagent=yes host | awk '/^forwardagent /{print$2}'` 103*19261079SEd Masteexpect_result_present "$f" "yes" 104*19261079SEd Mastef=`${SSH} -GF none '-oforwardagent=SSH_AUTH_SOCK.forward' host | awk '/^forwardagent /{print$2}'` 105*19261079SEd Masteexpect_result_present "$f" "SSH_AUTH_SOCK.forward" 106*19261079SEd Maste 107*19261079SEd Masteverbose "command line override" 108*19261079SEd Mastecat >$OBJ/ssh_config.0 <<EOD 109*19261079SEd MasteHost * 110*19261079SEd Maste IPQoS af21 cs1 111*19261079SEd Maste TunnelDevice 1:2 112*19261079SEd MasteEOD 113*19261079SEd Mastef=`${SSH} -GF $OBJ/ssh_config.0 -oipqos=cs1 host | awk '/^ipqos /{print$2}'` 114*19261079SEd Masteexpect_result_present "$f" "cs1" 115*19261079SEd Mastef=`${SSH} -GF $OBJ/ssh_config.0 -otunneldevice=3:4 host | awk '/^tunneldevice /{print$2}'` 116*19261079SEd Masteexpect_result_present "$f" "3:4" 117190cef3dSDag-Erling Smørgrav 118076ad2f8SDag-Erling Smørgrav# cleanup 119076ad2f8SDag-Erling Smørgravrm -f $OBJ/ssh_config.[012] 120