From 72a2cb48a847c91ffcc1febc085436f490afaad9 Mon Sep 17 00:00:00 2001 Message-Id: <72a2cb48a847c91ffcc1febc085436f490afaad9.1716636227.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sun, 5 Apr 2009 12:28:33 +0100 Subject: [PATCH] Python module now supports the same authorization hash functions as C. This means we need at least Python 2.5 (which shouldn't be a very onerous requirement any more!) Organization: Straylight/Edgeware From: Richard Kettlewell --- configure.ac | 2 +- python/disorder.py.in | 17 ++++++++++++--- tests/Makefile.am | 3 ++- tests/hashes.py | 50 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100755 tests/hashes.py diff --git a/configure.ac b/configure.ac index eb49d6a..94cffce 100644 --- a/configure.ac +++ b/configure.ac @@ -342,7 +342,7 @@ if test $want_gtk = yes; then fi fi if test $want_tests = yes && test $want_python = yes; then - AM_PATH_PYTHON([2.4]) + AM_PATH_PYTHON([2.5]) subdirs="${subdirs} python tests" fi AC_SUBST([subdirs]) diff --git a/python/disorder.py.in b/python/disorder.py.in index e873e49..f6fe1a4 100644 --- a/python/disorder.py.in +++ b/python/disorder.py.in @@ -50,7 +50,7 @@ import os import pwd import socket import binascii -import sha +import hashlib import sys import locale @@ -66,6 +66,18 @@ _unquoted = re.compile("[^\"' \\t\\n\\r][^ \t\n\r]*") _response = re.compile("([0-9]{3}) ?(.*)") +# hashes +_hashes = { + "sha1": hashlib.sha1, + "SHA1": hashlib.sha1, + "sha256": hashlib.sha256, + "SHA256": hashlib.sha256, + "sha384": hashlib.sha384, + "SHA384": hashlib.sha384, + "sha512": hashlib.sha512, + "SHA512": hashlib.sha512, +}; + version = "_version_" ######################################################################## @@ -387,8 +399,7 @@ class client: password = self.config['password'] else: password = self.password - # TODO support algorithms other than SHA-1 - h = sha.sha() + h = _hashes[algo]() h.update(password) h.update(binascii.unhexlify(challenge)) self._simple("user", user, h.hexdigest()) diff --git a/tests/Makefile.am b/tests/Makefile.am index 2508499..9936f17 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,3 +1,4 @@ + # # This file is part of DisOrder. # Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell @@ -26,7 +27,7 @@ disorder_udplog_DEPENDENCIES=../lib/libdisorder.a TESTS=cookie.py dbversion.py dump.py files.py play.py queue.py \ recode.py search.py user-upgrade.py user.py aliases.py \ - schedule.py + schedule.py hashes.py TESTS_ENVIRONMENT=${PYTHON} -u diff --git a/tests/hashes.py b/tests/hashes.py new file mode 100755 index 0000000..813951b --- /dev/null +++ b/tests/hashes.py @@ -0,0 +1,50 @@ +#! /usr/bin/env python +# +# This file is part of DisOrder. +# Copyright (C) 2009 Richard Kettlewell +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +import dtest,disorder,re + +def set_auth_algo(h): + configpath = "%s/config" % dtest.testroot + config = open(configpath, "r").readlines() + config = filter(lambda l: not re.match('authorization_algorithm', l), + config) + config.append('authorization_algorithm %s\n' % h) + open(configpath, "w").write("".join(config)) + +def test(): + """Authentication hash tests""" + created = False + for h in ['sha1', 'SHA1', 'sha256', 'SHA256', + 'sha384', 'SHA384', 'sha512', "SHA512" ]: + print " setting authorization hash to %s" % h + set_auth_algo(h) + dtest.start_daemon() + if not created: + dtest.create_user() + created = True + print " exercising C implementation" + dtest.command(["disorder", + "--config", disorder._configfile, "--no-per-user-config", + "--user", "root", "version"]) + print " exercising Python implementation" + c = disorder.client() + c.version() + dtest.stop_daemon() + +if __name__ == '__main__': + dtest.run() -- [mdw]