djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 1 | # $OpenBSD: hostkey-agent.sh,v 1.7 2017/04/30 23:34:55 djm Exp $ |
djm@openbsd.org | 26b3425 | 2015-01-17 18:54:30 +0000 | [diff] [blame] | 2 | # Placed in the Public Domain. |
| 3 | |
| 4 | tid="hostkey agent" |
| 5 | |
djm@openbsd.org | 1d6424a | 2015-01-20 08:02:33 +0000 | [diff] [blame] | 6 | rm -f $OBJ/agent-key.* $OBJ/ssh_proxy.orig $OBJ/known_hosts.orig |
djm@openbsd.org | 26b3425 | 2015-01-17 18:54:30 +0000 | [diff] [blame] | 7 | |
| 8 | trace "start agent" |
| 9 | eval `${SSHAGENT} -s` > /dev/null |
| 10 | r=$? |
| 11 | [ $r -ne 0 ] && fatal "could not start ssh-agent: exit code $r" |
| 12 | |
| 13 | grep -vi 'hostkey' $OBJ/sshd_proxy > $OBJ/sshd_proxy.orig |
| 14 | echo "HostKeyAgent $SSH_AUTH_SOCK" >> $OBJ/sshd_proxy.orig |
djm@openbsd.org | 26b3425 | 2015-01-17 18:54:30 +0000 | [diff] [blame] | 15 | |
| 16 | trace "load hostkeys" |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 17 | for k in `${SSH} -Q key-plain` ; do |
djm@openbsd.org | 1d6424a | 2015-01-20 08:02:33 +0000 | [diff] [blame] | 18 | ${SSHKEYGEN} -qt $k -f $OBJ/agent-key.$k -N '' || fatal "ssh-keygen $k" |
djm@openbsd.org | 26b3425 | 2015-01-17 18:54:30 +0000 | [diff] [blame] | 19 | ( |
djm@openbsd.org | e56aa87 | 2015-01-27 12:01:36 +0000 | [diff] [blame] | 20 | printf 'localhost-with-alias,127.0.0.1,::1 ' |
djm@openbsd.org | 1d6424a | 2015-01-20 08:02:33 +0000 | [diff] [blame] | 21 | cat $OBJ/agent-key.$k.pub |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 22 | ) >> $OBJ/known_hosts.orig |
djm@openbsd.org | 1d6424a | 2015-01-20 08:02:33 +0000 | [diff] [blame] | 23 | ${SSHADD} $OBJ/agent-key.$k >/dev/null 2>&1 || \ |
| 24 | fatal "couldn't load key $OBJ/agent-key.$k" |
djm@openbsd.org | 1a779a0 | 2015-02-21 20:51:02 +0000 | [diff] [blame] | 25 | echo "Hostkey $OBJ/agent-key.${k}" >> $OBJ/sshd_proxy.orig |
djm@openbsd.org | 26b3425 | 2015-01-17 18:54:30 +0000 | [diff] [blame] | 26 | # Remove private key so the server can't use it. |
djm@openbsd.org | 1d6424a | 2015-01-20 08:02:33 +0000 | [diff] [blame] | 27 | rm $OBJ/agent-key.$k || fatal "couldn't rm $OBJ/agent-key.$k" |
djm@openbsd.org | 26b3425 | 2015-01-17 18:54:30 +0000 | [diff] [blame] | 28 | done |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 29 | cp $OBJ/known_hosts.orig $OBJ/known_hosts |
djm@openbsd.org | 26b3425 | 2015-01-17 18:54:30 +0000 | [diff] [blame] | 30 | |
| 31 | unset SSH_AUTH_SOCK |
| 32 | |
| 33 | for ps in no yes; do |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 34 | for k in `${SSH} -Q key-plain` ; do |
djm@openbsd.org | 26b3425 | 2015-01-17 18:54:30 +0000 | [diff] [blame] | 35 | verbose "key type $k privsep=$ps" |
markus@openbsd.org | 5bf0933 | 2015-07-10 06:23:25 +0000 | [diff] [blame] | 36 | cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy |
| 37 | echo "UsePrivilegeSeparation $ps" >> $OBJ/sshd_proxy |
| 38 | echo "HostKeyAlgorithms $k" >> $OBJ/sshd_proxy |
djm@openbsd.org | 26b3425 | 2015-01-17 18:54:30 +0000 | [diff] [blame] | 39 | opts="-oHostKeyAlgorithms=$k -F $OBJ/ssh_proxy" |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 40 | cp $OBJ/known_hosts.orig $OBJ/known_hosts |
djm@openbsd.org | 26b3425 | 2015-01-17 18:54:30 +0000 | [diff] [blame] | 41 | SSH_CONNECTION=`${SSH} $opts host 'echo $SSH_CONNECTION'` |
| 42 | if [ $? -ne 0 ]; then |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 43 | fail "privsep=$ps failed" |
djm@openbsd.org | 26b3425 | 2015-01-17 18:54:30 +0000 | [diff] [blame] | 44 | fi |
| 45 | if [ "$SSH_CONNECTION" != "UNKNOWN 65535 UNKNOWN 65535" ]; then |
| 46 | fail "bad SSH_CONNECTION key type $k privsep=$ps" |
| 47 | fi |
| 48 | done |
| 49 | done |
| 50 | |
| 51 | trace "kill agent" |
| 52 | ${SSHAGENT} -k > /dev/null |
| 53 | |