#!/bin/sh

# monkeysphere-ssh-proxycommand: MonkeySphere ssh ProxyCommand hook
#
# The monkeysphere scripts are written by:
# Jameson Rollins <jrollins@fifthhorseman.net>
#
# They are Copyright 2008, and are all released under the GPL, version 3
# or later.

# This is meant to be run as an ssh ProxyCommand to initiate a
# monkeysphere known_hosts update before an ssh connection to host is
# established.  Can be added to ~/.ssh/config as follows:
#  ProxyCommand monkeysphere-ssh-proxycommand %h %p

usage() {
cat <<EOF >&2
usage: ssh -o ProxyCommand="$(basename $0) %h %p" ...
EOF
}

log() {
    echo "$@" >&2
}

if [ "$1" = '--no-connect' ] ; then
    NO_CONNECT='true'
    shift 1
fi

HOST="$1"
PORT="$2"

MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}

if [ -z "$HOST" ] ; then
    log "host must be specified."
    usage
    exit 1
fi
if [ -z "$PORT" ] ; then
    PORT=22
fi

# set the host URI
if [ "$PORT" != '22' ] ; then
    HOSTP="${HOST}:${PORT}"
else
    HOSTP="${HOST}"
fi
URI="ssh://${HOSTP}"

# if the host is in the gpg keyring...
if gpg --list-key ="${URI}" 2>&1 >/dev/null ; then
    # do not check the keyserver
    CHECK_KEYSERVER="false"

# if the host is NOT in the keyring...
else
    # if the host key is found in the known_hosts file...
    # FIXME: this only works for default known_hosts location
    hostKey=$(ssh-keygen -F "$HOST")

    if [ "$hostKey" ] ; then
	# do not check the keyserver
	# FIXME: more nuanced checking should be done here to properly
	# take into consideration hosts that join monkeysphere by
	# converting an existing and known ssh key
	CHECK_KEYSERVER="false"

    # if the host key is not found in the known_hosts file...
    else
	# check the keyserver
	CHECK_KEYSERVER="true"
    fi
fi

MONKEYSPHERE_CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="$CHECK_KEYSERVER"}
export MONKEYSPHERE_CHECK_KEYSERVER

# update the known_hosts file for the host
monkeysphere update-known_hosts "$HOSTP"

# exec a netcat passthrough to host for the ssh connection
if [ -z "$NO_CONNECT" ] ; then
    if (which nc 2>/dev/null >/dev/null); then
	exec nc "$HOST" "$PORT"
    elif (which socat 2>/dev/null >/dev/null); then
	exec socat STDIO "TCP:$HOST:$PORT"
    else
	log "Neither netcat nor socat found -- could not complete monkeysphere-ssh-proxycommand connection to $HOST:$PORT"
	exit 1
    fi
fi
/tags/ce?h=master'>ce</a>/<a href='/notmuch/diff/tags/ce/18?h=master'>18</a>/<a href='/notmuch/diff/tags/ce/18/22334.819.743982.649044@chiark.greenend.org.uk?h=master'>22334.819.743982.649044@chiark.greenend.org.uk</a></div><div class='content'><div class='cgit-panel'><b>diff options</b><form method='get'><input type='hidden' name='h' value='master'/><table><tr><td colspan='2'/></tr><tr><td class='label'>context:</td><td class='ctrl'><select name='context' onchange='this.form.submit();'><option value='1'>1</option><option value='2'>2</option><option value='3' selected='selected'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='15'>15</option><option value='20'>20</option><option value='25'>25</option><option value='30'>30</option><option value='35'>35</option><option value='40'>40</option></select></td></tr><tr><td class='label'>space:</td><td class='ctrl'><select name='ignorews' onchange='this.form.submit();'><option value='0' selected='selected'>include</option><option value='1'>ignore</option></select></td></tr><tr><td class='label'>mode:</td><td class='ctrl'><select name='dt' onchange='this.form.submit();'><option value='0' selected='selected'>unified</option><option value='1'>ssdiff</option><option value='2'>stat only</option></select></td></tr><tr><td/><td class='ctrl'><noscript><input type='submit' value='reload'/></noscript></td></tr></table></form></div><div class='diffstat-header'><a href='/notmuch/diff/?h=master'>Diffstat</a> (limited to 'tags/ce/18/22334.819.743982.649044@chiark.greenend.org.uk')</div><table summary='diffstat' class='diffstat'><tr><td class='mode'>-rw-r--r--</td><td class='add'><a href='/notmuch/diff/tags/ce/18/22334.819.743982.649044@chiark.greenend.org.uk/debian?h=master'>tags/ce/18/22334.819.743982.649044@chiark.greenend.org.uk/debian</a></td><td class='right'>0</td><td class='graph'><table summary='file diffstat' width='0%'><tr><td class='add' style='width: -nan%;'/><td class='rem' style='width: -nan%;'/><td class='none' style='width: -nan%;'/></tr></table></td></tr>
<tr><td class='mode'>-rw-r--r--</td><td class='add'><a href='/notmuch/diff/tags/ce/18/22334.819.743982.649044@chiark.greenend.org.uk/inbox?h=master'>tags/ce/18/22334.819.743982.649044@chiark.greenend.org.uk/inbox</a></td><td class='right'>0</td><td class='graph'><table summary='file diffstat' width='0%'><tr><td class='add' style='width: -nan%;'/><td class='rem' style='width: -nan%;'/><td class='none' style='width: -nan%;'/></tr></table></td></tr>
<tr><td class='mode'>-rw-r--r--</td><td class='add'><a href='/notmuch/diff/tags/ce/18/22334.819.743982.649044@chiark.greenend.org.uk/old?h=master'>tags/ce/18/22334.819.743982.649044@chiark.greenend.org.uk/old</a></td><td class='right'>0</td><td class='graph'><table summary='file diffstat' width='0%'><tr><td class='add' style='width: -nan%;'/><td class='rem' style='width: -nan%;'/><td class='none' style='width: -nan%;'/></tr></table></td></tr>
</table><div class='diffstat-summary'>3 files changed, 0 insertions, 0 deletions</div><table summary='diff' class='diff'><tr><td>