1# $OpenBSD: sshcfgparse.sh,v 1.10 2025/05/06 06:05:48 djm Exp $ 2# Placed in the Public Domain. 3 4tid="ssh config parse" 5 6expect_result_present() { 7 _str="$1" ; shift 8 for _expect in "$@" ; do 9 echo "$f" | tr ',' '\n' | grep "^$_expect\$" >/dev/null 10 if test $? -ne 0 ; then 11 fail "missing expected \"$_expect\" from \"$_str\"" 12 fi 13 done 14} 15expect_result_absent() { 16 _str="$1" ; shift 17 for _expect in "$@" ; do 18 echo "$f" | tr ',' '\n' | grep "^$_expect\$" >/dev/null 19 if test $? -eq 0 ; then 20 fail "unexpected \"$_expect\" present in \"$_str\"" 21 fi 22 done 23} 24 25verbose "reparse minimal config" 26(${SSH} -G -F $OBJ/ssh_config somehost >$OBJ/ssh_config.1 && 27 ${SSH} -G -F $OBJ/ssh_config.1 somehost >$OBJ/ssh_config.2 && 28 diff $OBJ/ssh_config.1 $OBJ/ssh_config.2) || fail "failed to reparse minimal" 29 30verbose "ssh -W opts" 31f=`${SSH} -GF $OBJ/ssh_config host | awk '/exitonforwardfailure/{print $2}'` 32test "$f" = "no" || fail "exitonforwardfailure default" 33f=`${SSH} -GF $OBJ/ssh_config -W a:1 h | awk '/exitonforwardfailure/{print $2}'` 34test "$f" = "yes" || fail "exitonforwardfailure enable" 35f=`${SSH} -GF $OBJ/ssh_config -W a:1 -o exitonforwardfailure=no h | \ 36 awk '/exitonforwardfailure/{print $2}'` 37test "$f" = "no" || fail "exitonforwardfailure override" 38 39f=`${SSH} -GF $OBJ/ssh_config host | awk '/clearallforwardings/{print $2}'` 40test "$f" = "no" || fail "clearallforwardings default" 41f=`${SSH} -GF $OBJ/ssh_config -W a:1 h | awk '/clearallforwardings/{print $2}'` 42test "$f" = "yes" || fail "clearallforwardings enable" 43f=`${SSH} -GF $OBJ/ssh_config -W a:1 -o clearallforwardings=no h | \ 44 awk '/clearallforwardings/{print $2}'` 45test "$f" = "no" || fail "clearallforwardings override" 46 47verbose "user first match" 48user=`awk '$1=="User" {print $2}' $OBJ/ssh_config` 49f=`${SSH} -GF $OBJ/ssh_config host | awk '/^user /{print $2}'` 50test "$f" = "$user" || fail "user from config, expected '$user' got '$f'" 51f=`${SSH} -GF $OBJ/ssh_config -o user=foo -l bar baz@host | awk '/^user /{print $2}'` 52test "$f" = "foo" || fail "user first match -oUser, expected 'foo' got '$f' " 53f=`${SSH} -GF $OBJ/ssh_config -lbar baz@host user=foo baz@host | awk '/^user /{print $2}'` 54test "$f" = "bar" || fail "user first match -l, expected 'bar' got '$f'" 55f=`${SSH} -GF $OBJ/ssh_config baz@host -o user=foo -l bar baz@host | awk '/^user /{print $2}'` 56test "$f" = "baz" || fail "user first match user@host, expected 'baz' got '$f'" 57 58verbose "pubkeyacceptedalgorithms" 59# Default set 60f=`${SSH} -GF none host | awk '/^pubkeyacceptedalgorithms /{print $2}'` 61expect_result_present "$f" "ssh-ed25519" "ssh-ed25519-cert-v01.*" 62# Explicit override 63f=`${SSH} -GF none -opubkeyacceptedalgorithms=ssh-ed25519 host | \ 64 awk '/^pubkeyacceptedalgorithms /{print $2}'` 65expect_result_present "$f" "ssh-ed25519" 66expect_result_absent "$f" "ssh-ed25519-cert-v01.*" 67# Removal from default set 68f=`${SSH} -GF none -opubkeyacceptedalgorithms=-ssh-ed25519-cert* host | \ 69 awk '/^pubkeyacceptedalgorithms /{print $2}'` 70expect_result_present "$f" "ssh-ed25519" 71expect_result_absent "$f" "ssh-ed25519-cert-v01.*" 72f=`${SSH} -GF none -opubkeyacceptedalgorithms=-ssh-ed25519 host | \ 73 awk '/^pubkeyacceptedalgorithms /{print $2}'` 74expect_result_present "$f" "ssh-ed25519-cert-v01.*" 75expect_result_absent "$f" "ssh-ed25519" 76# Append to default set. 77# This is not tested when built !WITH_OPENSSL 78# XXX need a test for this 79 80verbose "agentforwarding" 81f=`${SSH} -GF none host | awk '/^forwardagent /{print$2}'` 82expect_result_present "$f" "no" 83f=`${SSH} -GF none -oforwardagent=no host | awk '/^forwardagent /{print$2}'` 84expect_result_present "$f" "no" 85f=`${SSH} -GF none -oforwardagent=yes host | awk '/^forwardagent /{print$2}'` 86expect_result_present "$f" "yes" 87f=`${SSH} -GF none '-oforwardagent=SSH_AUTH_SOCK.forward' host | awk '/^forwardagent /{print$2}'` 88expect_result_present "$f" "SSH_AUTH_SOCK.forward" 89 90verbose "command line override" 91cat >$OBJ/ssh_config.0 <<EOD 92Host * 93 IPQoS af21 cs1 94 TunnelDevice 1:2 95EOD 96f=`${SSH} -GF $OBJ/ssh_config.0 -oipqos=cs1 host | awk '/^ipqos /{print$2}'` 97expect_result_present "$f" "cs1" 98f=`${SSH} -GF $OBJ/ssh_config.0 -otunneldevice=3:4 host | awk '/^tunneldevice /{print$2}'` 99expect_result_present "$f" "3:4" 100 101# cleanup 102rm -f $OBJ/ssh_config.[012] 103