VirtualMailManager/password.py
author Pascal Volk <user@localhost.localdomain.org>
Sun, 04 Nov 2012 18:33:11 +0000
branchv0.7.x
changeset 637 ca6621caff2f
parent 636 27334cfc0c90
child 638 0de0b9e75c9f
permissions -rw-r--r--
VMM: Removed module pycompat.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
568
14abdd04ddf5 Updated copyright notices to include the year 2012.
Pascal Volk <user@localhost.localdomain.org>
parents: 464
diff changeset
     2
# Copyright (c) 2010 - 2012, Pascal Volk
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
"""
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
    VirtualMailManager.password
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
    VirtualMailManager's password module to generate password hashes from
287
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
     9
    passwords or random passwords. This module provides following
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
    10
    functions:
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
        hashed_password = pwhash(password[, scheme][, user])
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    13
        random_password = randompw()
287
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
    14
        scheme, encoding = verify_scheme(scheme)
461
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    15
        schemes, encodings = list_schemes()
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
"""
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
636
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
    18
import hashlib
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
    19
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
from crypt import crypt
284
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
    21
from random import SystemRandom
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
from subprocess import Popen, PIPE
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
272
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 268
diff changeset
    24
from VirtualMailManager import ENCODING
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    25
from VirtualMailManager.emailaddress import EmailAddress
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
from VirtualMailManager.common import get_unicode, version_str
316
31d8931dc535 VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 292
diff changeset
    27
from VirtualMailManager.constants import VMM_ERROR
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
from VirtualMailManager.errors import VMMError
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
SALTCHARS = './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
PASSWDCHARS = '._-+#*23456789abcdefghikmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
DEFAULT_B64 = (None, 'B64', 'BASE64')
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
DEFAULT_HEX = (None, 'HEX')
292
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    34
CRYPT_ID_MD5 = 1
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    35
CRYPT_ID_BLF = '2a'
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    36
CRYPT_ID_SHA256 = 5
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    37
CRYPT_ID_SHA512 = 6
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    38
CRYPT_SALT_LEN = 2
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    39
CRYPT_BLF_ROUNDS_MIN = 4
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    40
CRYPT_BLF_ROUNDS_MAX = 31
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    41
CRYPT_BLF_SALT_LEN = 22
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    42
CRYPT_MD5_SALT_LEN = 8
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    43
CRYPT_SHA2_ROUNDS_DEFAULT = 5000
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    44
CRYPT_SHA2_ROUNDS_MIN = 1000
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    45
CRYPT_SHA2_ROUNDS_MAX = 999999999
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    46
CRYPT_SHA2_SALT_LEN = 16
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    47
SALTED_ALGO_SALT_LEN = 4
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    48
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
_ = lambda msg: msg
272
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 268
diff changeset
    51
cfg_dget = lambda option: None
284
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
    52
_sys_rand = SystemRandom()
292
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    53
_choice = _sys_rand.choice
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
    54
_get_salt = lambda s_len: ''.join(_choice(SALTCHARS) for x in xrange(s_len))
284
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
    55
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
    56
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
def _dovecotpw(password, scheme, encoding):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
    """Communicates with dovecotpw (Dovecot 2.0: `doveadm pw`) and returns
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
    the hashed password: {scheme[.encoding]}hash
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
    """
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
    if encoding:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
        scheme = '.'.join((scheme, encoding))
272
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 268
diff changeset
    63
    cmd_args = [cfg_dget('bin.dovecotpw'), '-s', scheme, '-p',
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
                get_unicode(password)]
272
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 268
diff changeset
    65
    if cfg_dget('misc.dovecot_version') >= 0x20000a01:
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
        cmd_args.insert(1, 'pw')
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
    process = Popen(cmd_args, stdout=PIPE, stderr=PIPE)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
    stdout, stderr = process.communicate()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
    if process.returncode:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
        raise VMMError(stderr.strip(), VMM_ERROR)
274
45ec5c3cfef4 VMM/password: added small output check on _dovecotpw().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 272
diff changeset
    71
    hashed = stdout.strip()
45ec5c3cfef4 VMM/password: added small output check on _dovecotpw().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 272
diff changeset
    72
    if not hashed.startswith('{%s}' % scheme):
45ec5c3cfef4 VMM/password: added small output check on _dovecotpw().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 272
diff changeset
    73
        raise VMMError('Unexpected result from %s: %s' %
45ec5c3cfef4 VMM/password: added small output check on _dovecotpw().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 272
diff changeset
    74
                       (cfg_dget('bin.dovecotpw'), hashed), VMM_ERROR)
45ec5c3cfef4 VMM/password: added small output check on _dovecotpw().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 272
diff changeset
    75
    return hashed
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
def _md4_new():
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
    """Returns an new MD4-hash object if supported by the hashlib or
636
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
    80
    provided by PyCrypto - otherwise `None`.
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
    """
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
    try:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
        return hashlib.new('md4')
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
    except ValueError, err:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        if str(err) == 'unsupported hash type':
636
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
    86
            try:
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
    87
                from Crypto.Hash import MD4
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
    88
                return MD4.new()
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
    89
            except ImportError:
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
    90
                return None
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
        else:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
            raise
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
def _format_digest(digest, scheme, encoding):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
    """Formats the arguments to a string: {scheme[.encoding]}digest."""
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
    if not encoding:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
        return '{%s}%s' % (scheme, digest)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
    return '{%s.%s}%s' % (scheme, encoding, digest)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   102
def _clear_hash(password, scheme, encoding):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   103
    """Generates a (encoded) CLEARTEXT/PLAIN 'hash'."""
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   104
    if encoding:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   105
        if encoding == 'HEX':
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   106
            password = password.encode('hex')
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
        else:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   108
            password = password.encode('base64').replace('\n', '')
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   109
        return _format_digest(password, scheme, encoding)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   110
    return get_unicode('{%s}%s' % (scheme, password))
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   111
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   112
284
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   113
def _get_crypt_blowfish_salt():
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   114
    """Generates a salt for Blowfish crypt."""
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   115
    rounds = cfg_dget('misc.crypt_blowfish_rounds')
292
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   116
    if rounds < CRYPT_BLF_ROUNDS_MIN:
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   117
        rounds = CRYPT_BLF_ROUNDS_MIN
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   118
    elif rounds > CRYPT_BLF_ROUNDS_MAX:
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   119
        rounds = CRYPT_BLF_ROUNDS_MAX
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   120
    return '$%s$%02d$%s' % (CRYPT_ID_BLF, rounds,
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   121
                            _get_salt(CRYPT_BLF_SALT_LEN))
284
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   122
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   123
291
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   124
def _get_crypt_sha2_salt(crypt_id):
284
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   125
    """Generates a salt for crypt using the SHA-256 or SHA-512 encryption
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   126
    method.
292
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   127
    *crypt_id* must be either `5` (SHA-256) or `6` (SHA-512).
284
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   128
    """
292
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   129
    assert crypt_id in (CRYPT_ID_SHA256, CRYPT_ID_SHA512), 'invalid crypt ' \
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   130
           'id: %r' % crypt_id
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   131
    if crypt_id is CRYPT_ID_SHA512:
284
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   132
        rounds = cfg_dget('misc.crypt_sha512_rounds')
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   133
    else:
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   134
        rounds = cfg_dget('misc.crypt_sha256_rounds')
292
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   135
    if rounds < CRYPT_SHA2_ROUNDS_MIN:
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   136
        rounds = CRYPT_SHA2_ROUNDS_MIN
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   137
    elif rounds > CRYPT_SHA2_ROUNDS_MAX:
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   138
        rounds = CRYPT_SHA2_ROUNDS_MAX
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   139
    if rounds == CRYPT_SHA2_ROUNDS_DEFAULT:
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   140
        return '$%d$%s' % (crypt_id, _get_salt(CRYPT_SHA2_SALT_LEN))
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   141
    return '$%d$rounds=%d$%s' % (crypt_id, rounds,
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   142
                                 _get_salt(CRYPT_SHA2_SALT_LEN))
284
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   143
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   144
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   145
def _crypt_hash(password, scheme, encoding):
291
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   146
    """Generates (encoded) CRYPT/MD5/{BLF,MD5,SHA{256,512}}-CRYPT hashes."""
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   147
    if scheme == 'CRYPT':
292
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   148
        salt = _get_salt(CRYPT_SALT_LEN)
291
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   149
    elif scheme == 'BLF-CRYPT':
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   150
        salt = _get_crypt_blowfish_salt()
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   151
    elif scheme in ('MD5-CRYPT', 'MD5'):
292
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   152
        salt = '$%d$%s' % (CRYPT_ID_MD5, _get_salt(CRYPT_MD5_SALT_LEN))
291
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   153
    elif scheme == 'SHA256-CRYPT':
292
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   154
        salt = _get_crypt_sha2_salt(CRYPT_ID_SHA256)
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   155
    else:
292
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   156
        salt = _get_crypt_sha2_salt(CRYPT_ID_SHA512)
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   157
    encrypted = crypt(password, salt)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   158
    if encoding:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   159
        if encoding == 'HEX':
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   160
            encrypted = encrypted.encode('hex')
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   161
        else:
284
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   162
            encrypted = encrypted.encode('base64').replace('\n', '')
291
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   163
    if scheme in ('BLF-CRYPT', 'SHA256-CRYPT', 'SHA512-CRYPT') and \
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   164
       cfg_dget('misc.dovecot_version') < 0x20000b06:
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   165
        scheme = 'CRYPT'
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   166
    return _format_digest(encrypted, scheme, encoding)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   167
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   168
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   169
def _md4_hash(password, scheme, encoding):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   170
    """Generates encoded PLAIN-MD4 hashes."""
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   171
    md4 = _md4_new()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   172
    if md4:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   173
        md4.update(password)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   174
        if encoding in DEFAULT_HEX:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   175
            digest = md4.hexdigest()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   176
        else:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   177
            digest = md4.digest().encode('base64').rstrip()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   178
        return _format_digest(digest, scheme, encoding)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   179
    return _dovecotpw(password, scheme, encoding)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   180
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   181
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   182
def _md5_hash(password, scheme, encoding, user=None):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   183
    """Generates DIGEST-MD5 aka PLAIN-MD5 and LDAP-MD5 hashes."""
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   184
    md5 = hashlib.md5()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   185
    if scheme == 'DIGEST-MD5':
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   186
        #  Prior to Dovecot v1.1.12/v1.2.beta2 there was a problem with a
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   187
        #  empty auth_realms setting in dovecot.conf and user@domain.tld
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   188
        #  usernames. So we have to generate different hashes for different
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   189
        #  versions. See also:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   190
        #       http://dovecot.org/list/dovecot-news/2009-March/000103.html
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   191
        #       http://hg.dovecot.org/dovecot-1.1/rev/2b0043ba89ae
272
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 268
diff changeset
   192
        if cfg_dget('misc.dovecot_version') >= 0x1010cf00:
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   193
            md5.update('%s:%s:' % (user.localpart, user.domainname))
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   194
        else:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   195
            md5.update('%s::' % user)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   196
    md5.update(password)
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 289
diff changeset
   197
    if (scheme in ('PLAIN-MD5', 'DIGEST-MD5') and encoding in DEFAULT_HEX) or \
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 289
diff changeset
   198
       (scheme == 'LDAP-MD5' and encoding == 'HEX'):
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   199
        digest = md5.hexdigest()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   200
    else:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   201
        digest = md5.digest().encode('base64').rstrip()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   202
    return _format_digest(digest, scheme, encoding)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   203
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   204
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   205
def _ntlm_hash(password, scheme, encoding):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   206
    """Generates NTLM hashes."""
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   207
    md4 = _md4_new()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   208
    if md4:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   209
        password = ''.join('%s\x00' % c for c in password)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   210
        md4.update(password)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   211
        if encoding in DEFAULT_HEX:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   212
            digest = md4.hexdigest()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   213
        else:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   214
            digest = md4.digest().encode('base64').rstrip()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   215
        return _format_digest(digest, scheme, encoding)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   216
    return _dovecotpw(password, scheme, encoding)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   217
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   218
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   219
def _sha1_hash(password, scheme, encoding):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   220
    """Generates SHA1 aka SHA hashes."""
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   221
    sha1 = hashlib.sha1(password)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   222
    if encoding in DEFAULT_B64:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   223
        digest = sha1.digest().encode('base64').rstrip()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   224
    else:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   225
        digest = sha1.hexdigest()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   226
    return _format_digest(digest, scheme, encoding)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   227
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   228
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   229
def _sha256_hash(password, scheme, encoding):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   230
    """Generates SHA256 hashes."""
636
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   231
    sha256 = hashlib.sha256(password)
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   232
    if encoding in DEFAULT_B64:
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   233
        digest = sha256.digest().encode('base64').rstrip()
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   234
    else:
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   235
        digest = sha256.hexdigest()
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   236
    return _format_digest(digest, scheme, encoding)
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   237
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   238
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   239
def _sha512_hash(password, scheme, encoding):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   240
    """Generates SHA512 hashes."""
636
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   241
    sha512 = hashlib.sha512(password)
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   242
    if encoding in DEFAULT_B64:
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   243
        digest = sha512.digest().encode('base64').replace('\n', '')
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   244
    else:
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   245
        digest = sha512.hexdigest()
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   246
    return _format_digest(digest, scheme, encoding)
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   247
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   248
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   249
def _smd5_hash(password, scheme, encoding):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   250
    """Generates SMD5 (salted PLAIN-MD5) hashes."""
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   251
    md5 = hashlib.md5(password)
292
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   252
    salt = _get_salt(SALTED_ALGO_SALT_LEN)
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   253
    md5.update(salt)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   254
    if encoding in DEFAULT_B64:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   255
        digest = (md5.digest() + salt).encode('base64').rstrip()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   256
    else:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   257
        digest = md5.hexdigest() + salt.encode('hex')
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   258
    return _format_digest(digest, scheme, encoding)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   259
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   260
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   261
def _ssha1_hash(password, scheme, encoding):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   262
    """Generates SSHA (salted SHA/SHA1) hashes."""
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   263
    sha1 = hashlib.sha1(password)
292
619dadc0fd25 VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 291
diff changeset
   264
    salt = _get_salt(SALTED_ALGO_SALT_LEN)
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   265
    sha1.update(salt)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   266
    if encoding in DEFAULT_B64:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   267
        digest = (sha1.digest() + salt).encode('base64').rstrip()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   268
    else:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   269
        digest = sha1.hexdigest() + salt.encode('hex')
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   270
    return _format_digest(digest, scheme, encoding)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   271
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   272
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   273
def _ssha256_hash(password, scheme, encoding):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   274
    """Generates SSHA256 (salted SHA256) hashes."""
636
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   275
    sha256 = hashlib.sha256(password)
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   276
    salt = _get_salt(SALTED_ALGO_SALT_LEN)
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   277
    sha256.update(salt)
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   278
    if encoding in DEFAULT_B64:
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   279
        digest = (sha256.digest() + salt).encode('base64').rstrip()
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   280
    else:
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   281
        digest = sha256.hexdigest() + salt.encode('hex')
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   282
    return _format_digest(digest, scheme, encoding)
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   283
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   284
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   285
def _ssha512_hash(password, scheme, encoding):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   286
    """Generates SSHA512 (salted SHA512) hashes."""
636
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   287
    salt = _get_salt(SALTED_ALGO_SALT_LEN)
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   288
    sha512 = hashlib.sha512(password + salt)
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   289
    if encoding in DEFAULT_B64:
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   290
        digest = (sha512.digest() + salt).encode('base64').replace('\n', '')
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   291
    else:
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   292
        digest = sha512.hexdigest() + salt.encode('hex')
27334cfc0c90 VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents: 633
diff changeset
   293
    return _format_digest(digest, scheme, encoding)
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   294
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   295
_scheme_info = {
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   296
    'CLEARTEXT': (_clear_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   297
    'CRAM-MD5': (_dovecotpw, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   298
    'CRYPT': (_crypt_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   299
    'DIGEST-MD5': (_md5_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   300
    'HMAC-MD5': (_dovecotpw, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   301
    'LANMAN': (_dovecotpw, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   302
    'LDAP-MD5': (_md5_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   303
    'MD5': (_crypt_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   304
    'MD5-CRYPT': (_crypt_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   305
    'NTLM': (_ntlm_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   306
    'OTP': (_dovecotpw, 0x10100a01),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   307
    'PLAIN': (_clear_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   308
    'PLAIN-MD4': (_md4_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   309
    'PLAIN-MD5': (_md5_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   310
    'RPA': (_dovecotpw, 0x10000f00),
633
8ea32fbfc9df VMM/password: Added scheme SCRAM-SHA-1.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   311
    'SCRAM-SHA-1': (_dovecotpw, 0x20200a01),
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   312
    'SHA': (_sha1_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   313
    'SHA1': (_sha1_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   314
    'SHA256': (_sha256_hash, 0x10100a01),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   315
    'SHA512': (_sha512_hash, 0x20000b03),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   316
    'SKEY': (_dovecotpw, 0x10100a01),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   317
    'SMD5': (_smd5_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   318
    'SSHA': (_ssha1_hash, 0x10000f00),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   319
    'SSHA256': (_ssha256_hash, 0x10200a04),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   320
    'SSHA512': (_ssha512_hash, 0x20000b03),
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   321
}
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   322
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   323
461
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   324
def list_schemes():
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   325
    """Returns the tuple (schemes, encodings).
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   326
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   327
    `schemes` is an iterator for all supported password schemes (depends on
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   328
    the used Dovecot version and features of the libc).
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   329
    `encodings` is a tuple with all usable encoding suffixes. The tuple may
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   330
    be empty.
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   331
    """
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   332
    dcv = cfg_dget('misc.dovecot_version')
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   333
    schemes = (k for (k, v) in _scheme_info.iteritems() if v[1] <= dcv)
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   334
    if dcv >= 0x10100a01:
464
7d4d79ff08d0 VMM/password: list_schemes(): report suffixes with a leading dot.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 461
diff changeset
   335
        encodings = ('.B64', '.BASE64', '.HEX')
461
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   336
    else:
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   337
        encodings = ()
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   338
    return schemes, encodings
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   339
cabdf94ec580 VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   340
287
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   341
def verify_scheme(scheme):
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   342
    """Checks if the password scheme *scheme* is known and supported by the
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   343
    configured `misc.dovecot_version`.
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   344
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   345
    The *scheme* maybe a password scheme's name (e.g.: 'PLAIN') or a scheme
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   346
    name with a encoding suffix (e.g. 'PLAIN.BASE64').  If the scheme is
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   347
    known and supported by the used Dovecot version,
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   348
    a tuple ``(scheme, encoding)`` will be returned.
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   349
    The `encoding` in the tuple may be `None`.
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   350
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   351
    Raises a `VMMError` if the password scheme:
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   352
      * is unknown
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   353
      * depends on a newer Dovecot version
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   354
      * has a unknown encoding suffix
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   355
    """
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   356
    assert isinstance(scheme, basestring), 'Not a str/unicode: %r' % scheme
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   357
    scheme_encoding = scheme.upper().split('.')
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   358
    scheme = scheme_encoding[0]
291
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   359
    if scheme not in _scheme_info:
287
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   360
        raise VMMError(_(u"Unsupported password scheme: '%s'") % scheme,
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   361
                       VMM_ERROR)
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   362
    if cfg_dget('misc.dovecot_version') < _scheme_info[scheme][1]:
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 289
diff changeset
   363
        raise VMMError(_(u"The password scheme '%(scheme)s' requires Dovecot "
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   364
                         u">= v%(version)s.") % {'scheme': scheme,
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 289
diff changeset
   365
                       'version': version_str(_scheme_info[scheme][1])},
287
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   366
                       VMM_ERROR)
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   367
    if len(scheme_encoding) > 1:
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   368
        if cfg_dget('misc.dovecot_version') < 0x10100a01:
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 289
diff changeset
   369
            raise VMMError(_(u'Encoding suffixes for password schemes require '
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   370
                             u'Dovecot >= v1.1.alpha1.'), VMM_ERROR)
287
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   371
        if scheme_encoding[1] not in ('B64', 'BASE64', 'HEX'):
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   372
            raise VMMError(_(u"Unsupported password encoding: '%s'") %
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   373
                           scheme_encoding[1], VMM_ERROR)
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   374
        encoding = scheme_encoding[1]
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   375
    else:
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   376
        encoding = None
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   377
    return scheme, encoding
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   378
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   379
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   380
def pwhash(password, scheme=None, user=None):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   381
    """Generates a password hash from the plain text *password* string.
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   382
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   383
    If no *scheme* is given the password scheme from the configuration will
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   384
    be used for the hash generation.  When 'DIGEST-MD5' is used as scheme,
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   385
    also an EmailAddress instance must be given as *user* argument.
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   386
    """
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   387
    if not isinstance(password, basestring):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   388
        raise TypeError('Password is not a string: %r' % password)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   389
    if isinstance(password, unicode):
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   390
        password = password.encode(ENCODING)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   391
    password = password.strip()
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   392
    if not password:
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   393
        raise ValueError("Could not accept empty password.")
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   394
    if scheme is None:
272
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 268
diff changeset
   395
        scheme = cfg_dget('misc.password_scheme')
287
1e77dd639fa3 VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 284
diff changeset
   396
    scheme, encoding = verify_scheme(scheme)
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   397
    if scheme == 'DIGEST-MD5':
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   398
        assert isinstance(user, EmailAddress)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   399
        return _md5_hash(password, scheme, encoding, user)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   400
    return _scheme_info[scheme][0](password, scheme, encoding)
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   401
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   402
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   403
def randompw():
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   404
    """Generates a plain text random password.
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   405
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   406
    The length of the password can be configured in the ``vmm.cfg``
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   407
    (account.password_length).
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   408
    """
272
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 268
diff changeset
   409
    pw_len = cfg_dget('account.password_length')
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   410
    if pw_len < 8:
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   411
        pw_len = 8
284
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   412
    return ''.join(_sys_rand.sample(PASSWDCHARS, pw_len))
268
beb8f4421f92 VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   413
291
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   414
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   415
def _test_crypt_algorithms():
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   416
    """Check for Blowfish/SHA-256/SHA-512 support in crypt.crypt()."""
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   417
    _blowfish = '$2a$04$0123456789abcdefABCDE.N.drYX5yIAL1LkTaaZotW3yI0hQhZru'
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   418
    _sha256 = '$5$rounds=1000$0123456789abcdef$K/DksR0DT01hGc8g/kt9McEgrbFMKi\
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   419
9qrb1jehe7hn4'
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   420
    _sha512 = '$6$rounds=1000$0123456789abcdef$ZIAd5WqfyLkpvsVCVUU1GrvqaZTqvh\
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   421
JoouxdSqJO71l9Ld3tVrfOatEjarhghvEYADkq//LpDnTeO90tcbtHR1'
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   422
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   423
    if crypt('08/15!test~4711', '$2a$04$0123456789abcdefABCDEF$') == _blowfish:
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   424
        _scheme_info['BLF-CRYPT'] = (_crypt_hash, 0x10000f00)
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   425
    if crypt('08/15!test~4711', '$5$rounds=1000$0123456789abcdef$') == _sha256:
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   426
        _scheme_info['SHA256-CRYPT'] = (_crypt_hash, 0x10000f00)
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   427
    if crypt('08/15!test~4711', '$6$rounds=1000$0123456789abcdef$') == _sha512:
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   428
        _scheme_info['SHA512-CRYPT'] = (_crypt_hash, 0x10000f00)
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   429
7ef3f117a230 VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   430
_test_crypt_algorithms()
284
ec1966828246 VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 274
diff changeset
   431
del _, cfg_dget, _test_crypt_algorithms