Commit b5cd037a by Scott Duckworth

provide a different set of lookup methods

parent 9d4ed075
#!/bin/bash #!/bin/sh
# Copyright 2013 Scott Duckworth # Copyright 2013 Scott Duckworth
# #
# This file is part of django-sshkey. # This file is part of django-sshkey.
...@@ -16,15 +16,14 @@ ...@@ -16,15 +16,14 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with django-sshkey. If not, see <http://www.gnu.org/licenses/>. # along with django-sshkey. If not, see <http://www.gnu.org/licenses/>.
url="$1" if [ $# -eq 0 ]; then
echo "Usage: $0 URL [USERNAME]" >&2
exit 1
fi
SSHKEY_LOOKUP_URL="$1"
export SSHKEY_LOOKUP_URL
if [ $# -eq 1 ]; then if [ $# -eq 1 ]; then
read line exec `dirname $0`/django-sshkey-lookup-by-fingerprint
fingerprint=$(ssh-keygen -lf /dev/stdin <<< $line | cut -f2 -d' ')
exec curl -s -G "$url" --data-urlencode "fingerprint=${fingerprint}"
elif [ $# -eq 2 ]; then
username="$2"
exec curl -s -G "$url" --data-urlencode "username=${username}"
else else
echo "Invalid number of arguments" >&2 exec `dirname $0`/django-sshkey-lookup-by-username "$2"
exit 2
fi fi
#!/bin/sh
# Copyright 2013 Scott Duckworth
#
# This file is part of django-sshkey.
#
# django-sshkey is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# django-sshkey is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-sshkey. If not, see <http://www.gnu.org/licenses/>.
url="${SSHKEY_LOOKUP_URL:-http://localhost:8000/sshkey/lookup}"
if type curl >/dev/null 2>&1; then
exec curl -s "$url"
else
exec wget -q -O - "$url"
fi
#!/bin/bash
# Copyright 2013 Scott Duckworth
#
# This file is part of django-sshkey.
#
# django-sshkey is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# django-sshkey is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-sshkey. If not, see <http://www.gnu.org/licenses/>.
url="${SSHKEY_LOOKUP_URL:-http://localhost:8000/sshkey/lookup}"
if [ "x$SSH_KEY_FINGERPRINT" != "x" ]; then
fingerprint="$SSH_KEY_FINGERPRINT"
else
if [ "x$SSH_KEY" == "x" ] && ! read SSH_KEY; then
echo "Error: cannot retrieve fingerprint from environment or stdin" >&2
exit 1
fi
info="$(ssh-keygen -lf /dev/stdin <<< "$SSH_KEY")"
if [ $? -ne 0 ]; then
echo "Error: $info" >&2
exit 1
fi
info=($info)
fingerprint="${info[1]}"
fi
if type curl >/dev/null 2>&1; then
exec curl -s -G "$url" --data-urlencode "fingerprint=${fingerprint}"
else
exec wget -q -O - "${url}?fingerprint=${fingerprint}"
fi
#!/bin/sh
# Copyright 2013 Scott Duckworth
#
# This file is part of django-sshkey.
#
# django-sshkey is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# django-sshkey is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-sshkey. If not, see <http://www.gnu.org/licenses/>.
url="${SSHKEY_LOOKUP_URL:-http://localhost:8000/sshkey/lookup}"
if type curl >/dev/null 2>&1; then
exec curl -s -G "$url" --data-urlencode "username=$1"
else
exec wget -q -O - "${url}?username=$1"
fi
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
import re import re
SSHKEY_LOOKUP_URL_DEFAULT = 'http://localhost:8000/sshkey/lookup'
sshkey_re = re.compile(r'(?P<type>[\w-]+)\s+(?P<b64key>\S+)(?:\s+(?P<comment>\S.+))?$') sshkey_re = re.compile(r'(?P<type>[\w-]+)\s+(?P<b64key>\S+)(?:\s+(?P<comment>\S.+))?$')
def sshkey_fingerprint(b64key): def sshkey_fingerprint(b64key):
...@@ -26,30 +28,76 @@ def sshkey_fingerprint(b64key): ...@@ -26,30 +28,76 @@ def sshkey_fingerprint(b64key):
fp_plain = hashlib.md5(key).hexdigest() fp_plain = hashlib.md5(key).hexdigest()
return ':'.join(a+b for a,b in zip(fp_plain[::2], fp_plain[1::2])) return ':'.join(a+b for a,b in zip(fp_plain[::2], fp_plain[1::2]))
def lookup_command(args): def lookup_all(url):
import sys
import urllib import urllib
if len(args) == 1:
url = args[0]
line = sys.stdin.readline()
if not line:
sys.stderr.write('no input given\n')
sys.exit(2)
fingerprint = sshkey_fingerprint(line)
url += '?fingerprint=' + urllib.quote_plus(fingerprint)
elif len(args) == 2:
url, username = args
url += '?username=' + urllib.quote_plus(username)
else:
sys.stderr.write('Invalid number of arguments\n')
sys.exit(2)
response = urllib.urlopen(url) response = urllib.urlopen(url)
status = 1 return response.readlines()
for line in response.readlines():
status = 0 def lookup_by_username(url, username):
sys.stdout.write(line) import urllib
sys.exit(status) url += '?' + urllib.urlencode({'username': username})
response = urllib.urlopen(url)
return response.readlines()
def lookup_by_fingerprint(url, fingerprint):
import urllib
url += '?' + urllib.urlencode({'fingerprint': fingerprint})
response = urllib.urlopen(url)
return response.readlines()
def lookup_all_main():
import sys
from os import getenv
url = getenv('SSHKEY_LOOKUP_URL', SSHKEY_LOOKUP_URL_DEFAULT)
for key in lookup_all(url):
sys.stdout.write(key)
def lookup_by_username_main():
import sys
from os import getenv
if len(sys.argv) < 2:
sys.stderr.write('Usage: %s USERNAME\n' % sys.argv[0])
sys.exit(1)
username = sys.argv[1]
url = getenv('SSHKEY_LOOKUP_URL', SSHKEY_LOOKUP_URL_DEFAULT)
for key in lookup_by_username(url, username):
sys.stdout.write(key)
def lookup_by_fingerprint_main():
import sys
from os import getenv
fingerprint = getenv('SSH_KEY_FINGERPRINT')
if fingerprint is None:
key = getenv('SSH_KEY')
if key is None:
key = sys.stdin.readline()
if not key:
sys.stderr.write(
"Error: cannot retrieve fingerprint from environment or stdin\n"
)
sys.exit(1)
m = sshkey_re.match(key)
if not m:
sys.stderr.write(
"Error: cannot parse SSH protocol 2 base64-encoded key"
)
sys.exit(1)
fingerprint = sshkey_fingerprint(m.group('b64key'))
url = getenv('SSHKEY_LOOKUP_URL', SSHKEY_LOOKUP_URL_DEFAULT)
for key in lookup_by_fingerprint(url, fingerprint):
sys.stdout.write(key)
def lookup_main(): def lookup_main():
import sys import sys
lookup_command(sys.argv[1:]) from os import environ
if len(sys.argv) < 2:
sys.stderr.write('Usage: %s URL [USERNAME]\n' % sys.argv[0])
sys.exit(1)
url = sys.argv[1]
if len(sys.argv) == 2:
environ['SSHKEY_LOOKUP_URL'] = url
lookup_by_fingerprint_main()
else:
username = sys.argv[2]
for key in lookup_by_username(url, username):
sys.stdout.write(key)
...@@ -32,10 +32,16 @@ setup( ...@@ -32,10 +32,16 @@ setup(
'Topic :: Internet :: WWW/HTTP :: Dynamic Content', 'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
], ],
scripts=[ scripts=[
'django-sshkey-lookup-all',
'django-sshkey-lookup-by-username',
'django-sshkey-lookup-by-fingerprint',
'django-sshkey-lookup', 'django-sshkey-lookup',
], ],
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'django-sshkey-pylookup-all = django_sshkey.util:lookup_all_main',
'django-sshkey-pylookup-by-username = django_sshkey.util:lookup_by_username_main',
'django-sshkey-pylookup-by-fingerprint = django_sshkey.util:lookup_by_fingerprint_main',
'django-sshkey-pylookup = django_sshkey.util:lookup_main', 'django-sshkey-pylookup = django_sshkey.util:lookup_main',
], ],
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment