VirtualMailManager/pycompat/hashlib.py
author martin f. krafft <madduck@madduck.net>
Tue, 10 Apr 2012 22:56:30 +0200
branchv0.6.x
changeset 514 d863a44a6353
parent 366 d6573da35b5f
child 568 14abdd04ddf5
permissions -rw-r--r--
Make PL/pgSQL function feed back identity for mailboxes/relocated when there are catchall destinations. Without catchall aliases, if no virtual_alias matches, the query can just return NULL and Postfix will later check mailboxes/relocated for the address to rewrite. However, since virtual aliases are handled long before mailboxes/relocated, a catchall alias would also catch mail to mailboxes and relocated addresses, which we do not want. The way to tell postfix to keep delivering is for the virtual alias map to return the search key itself (identity function). This patch changes the postfix_virtual_alias_maps Pl/pgSQL function to do exactly that, but only if there are catchall destinations defined for the domain in question — otherwise it returns NULL when no match is found.

# -*- coding: UTF-8 -*-
# Copyright (c) 2010 - 2011, Pascal Volk
# See COPYING for distribution information.

"""
    VirtualMailManager.pycompat.hashlib

    VirtualMailManager's minimal hashlib emulation for Python 2.4

    hashlib.md5(...), hashlib.sha1(...), hashlib.new('md5', ...) and
    hashlib.new('sha1', ...) will work always.

    When the PyCrypto module <http://www.pycrypto.org/> could be found in
    sys.path hashlib.new('md4', ...) will also work.

    With PyCrypto >= 2.1.0alpha1 hashlib.new('sha256', ...) and
    hashlib.sha256(...) becomes functional.
"""


import md5 as _md5
import sha as _sha1

try:
    import Crypto
except ImportError:
    _md4 = None
    SHA256 = None
else:
    from Crypto.Hash import MD4 as _md4
    if hasattr(Crypto, 'version_info'):  # <- Available since v2.1.0alpha1
        from Crypto.Hash import SHA256   # SHA256 works since v2.1.0alpha1
        sha256 = SHA256.new
    else:
        SHA256 = None
    del Crypto


compat = 0x01
md5 = _md5.new
sha1 = _sha1.new


def new(name, string=''):
    """Return a new hashing object using the named algorithm, optionally
    initialized with the provided string.
    """
    if name in ('md5', 'MD5'):
        return _md5.new(string)
    if name in ('sha1', 'SHA1'):
        return _sha1.new(string)
    if not _md4:
        raise ValueError('unsupported hash type')
    if name in ('md4', 'MD4'):
        return _md4.new(string)
    if name in ('sha256', 'SHA256') and SHA256:
        return SHA256.new(string)
    raise ValueError('unsupported hash type')