author | Pascal Volk <user@localhost.localdomain.org> |
Mon, 10 Feb 2014 22:54:43 +0000 | |
branch | v0.7.x |
changeset 718 | 65a29a373c19 |
parent 711 | 2a75058fc064 |
child 722 | e37f60b0e3b5 |
permissions | -rw-r--r-- |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
703
58815c004a61
Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents:
675
diff
changeset
|
2 |
# Copyright (c) 2010 - 2014, 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 |
|
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
20 |
from base64 import b64encode |
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
21 |
from binascii import b2a_hex |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
22 |
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
|
23 |
from random import SystemRandom |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
24 |
from subprocess import Popen, PIPE |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
25 |
|
272
446483386914
VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
268
diff
changeset
|
26 |
from VirtualMailManager import ENCODING |
320
011066435e6f
VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
316
diff
changeset
|
27 |
from VirtualMailManager.emailaddress import EmailAddress |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
28 |
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
|
29 |
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
|
30 |
from VirtualMailManager.errors import VMMError |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
31 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
32 |
SALTCHARS = './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
33 |
PASSWDCHARS = '._-+#*23456789abcdefghikmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
34 |
DEFAULT_B64 = (None, 'B64', 'BASE64') |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
35 |
DEFAULT_HEX = (None, 'HEX') |
292
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
36 |
CRYPT_ID_MD5 = 1 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
37 |
CRYPT_ID_BLF = '2a' |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
38 |
CRYPT_ID_SHA256 = 5 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
39 |
CRYPT_ID_SHA512 = 6 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
40 |
CRYPT_SALT_LEN = 2 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
41 |
CRYPT_BLF_ROUNDS_MIN = 4 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
42 |
CRYPT_BLF_ROUNDS_MAX = 31 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
43 |
CRYPT_BLF_SALT_LEN = 22 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
44 |
CRYPT_MD5_SALT_LEN = 8 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
45 |
CRYPT_SHA2_ROUNDS_DEFAULT = 5000 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
46 |
CRYPT_SHA2_ROUNDS_MIN = 1000 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
47 |
CRYPT_SHA2_ROUNDS_MAX = 999999999 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
48 |
CRYPT_SHA2_SALT_LEN = 16 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
49 |
SALTED_ALGO_SALT_LEN = 4 |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
50 |
|
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
51 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
52 |
_ = lambda msg: msg |
272
446483386914
VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
268
diff
changeset
|
53 |
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
|
54 |
_sys_rand = SystemRandom() |
292
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
55 |
_choice = _sys_rand.choice |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
638
diff
changeset
|
56 |
_get_salt = lambda s_len: ''.join(_choice(SALTCHARS) for x in range(s_len)) |
284
ec1966828246
VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
274
diff
changeset
|
57 |
|
ec1966828246
VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
274
diff
changeset
|
58 |
|
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
59 |
def _dovecotpw(password, scheme, encoding): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
60 |
"""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
|
61 |
the hashed password: {scheme[.encoding]}hash |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
62 |
""" |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
63 |
if encoding: |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
get_unicode(password)] |
272
446483386914
VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
268
diff
changeset
|
67 |
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
|
68 |
cmd_args.insert(1, 'pw') |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
69 |
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
|
70 |
stdout, stderr = process.communicate() |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
71 |
if process.returncode: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
72 |
raise VMMError(stderr.strip().decode(ENCODING), VMM_ERROR) |
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
73 |
hashed = stdout.strip().decode(ENCODING) |
274
45ec5c3cfef4
VMM/password: added small output check on _dovecotpw().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
272
diff
changeset
|
74 |
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
|
75 |
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
|
76 |
(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
|
77 |
return hashed |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
78 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
79 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
80 |
def _md4_new(): |
655
2bf68600e914
VMM/password: Code cleanups:
Pascal Volk <user@localhost.localdomain.org>
parents:
651
diff
changeset
|
81 |
"""Returns an new MD4-hash object if supported by the hashlib - |
2bf68600e914
VMM/password: Code cleanups:
Pascal Volk <user@localhost.localdomain.org>
parents:
651
diff
changeset
|
82 |
otherwise `None`. |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
83 |
""" |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
84 |
try: |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
85 |
return hashlib.new('md4') |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
638
diff
changeset
|
86 |
except ValueError as err: |
655
2bf68600e914
VMM/password: Code cleanups:
Pascal Volk <user@localhost.localdomain.org>
parents:
651
diff
changeset
|
87 |
if err.args[0].startswith('unsupported hash type'): |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
88 |
return None |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
89 |
else: |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
90 |
raise |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
91 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
92 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
93 |
def _format_digest(digest, scheme, encoding): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
94 |
"""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
|
95 |
if not encoding: |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
96 |
return '{%s}%s' % (scheme, digest) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
97 |
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
|
98 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
99 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
100 |
def _clear_hash(password, scheme, encoding): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
101 |
"""Generates a (encoded) CLEARTEXT/PLAIN 'hash'.""" |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
102 |
password = password.decode(ENCODING) |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
103 |
if encoding: |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
104 |
if encoding == 'HEX': |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
105 |
password = b2a_hex(password.encode()).decode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
106 |
else: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
107 |
password = b64encode(password.encode()).decode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
108 |
return _format_digest(password, scheme, encoding) |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
109 |
return '{%s}%s' % (scheme, password) |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
110 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
111 |
|
284
ec1966828246
VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
274
diff
changeset
|
112 |
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
|
113 |
"""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
|
114 |
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
|
115 |
if rounds < CRYPT_BLF_ROUNDS_MIN: |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
116 |
rounds = CRYPT_BLF_ROUNDS_MIN |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
117 |
elif rounds > CRYPT_BLF_ROUNDS_MAX: |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
118 |
rounds = CRYPT_BLF_ROUNDS_MAX |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
119 |
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
|
120 |
_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
|
121 |
|
ec1966828246
VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
274
diff
changeset
|
122 |
|
291
7ef3f117a230
VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
123 |
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
|
124 |
"""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
|
125 |
method. |
292
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
126 |
*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
|
127 |
""" |
292
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
128 |
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
|
129 |
'id: %r' % crypt_id |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
130 |
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
|
131 |
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
|
132 |
else: |
ec1966828246
VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
274
diff
changeset
|
133 |
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
|
134 |
if rounds < CRYPT_SHA2_ROUNDS_MIN: |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
135 |
rounds = CRYPT_SHA2_ROUNDS_MIN |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
136 |
elif rounds > CRYPT_SHA2_ROUNDS_MAX: |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
137 |
rounds = CRYPT_SHA2_ROUNDS_MAX |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
138 |
if rounds == CRYPT_SHA2_ROUNDS_DEFAULT: |
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
139 |
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
|
140 |
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
|
141 |
_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
|
142 |
|
ec1966828246
VMM/password: Added support Blowfish/SHA-256/SHA-512 crypt().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
274
diff
changeset
|
143 |
|
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
144 |
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
|
145 |
"""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
|
146 |
if scheme == 'CRYPT': |
292
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
elif scheme in ('MD5-CRYPT', 'MD5'): |
292
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
151 |
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
|
152 |
elif scheme == 'SHA256-CRYPT': |
292
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
153 |
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
|
154 |
else: |
292
619dadc0fd25
VMM/password: added some CRYPT_* constants.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
291
diff
changeset
|
155 |
salt = _get_crypt_sha2_salt(CRYPT_ID_SHA512) |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
156 |
encrypted = crypt(password.decode(ENCODING), salt) |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
157 |
if encoding: |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
158 |
if encoding == 'HEX': |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
159 |
encrypted = b2a_hex(encrypted.encode()).decode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
160 |
else: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
161 |
encrypted = b64encode(encrypted.encode()).decode() |
291
7ef3f117a230
VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
162 |
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
|
163 |
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
|
164 |
scheme = 'CRYPT' |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
165 |
return _format_digest(encrypted, scheme, encoding) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
166 |
|
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 |
def _md4_hash(password, scheme, encoding): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
169 |
"""Generates encoded PLAIN-MD4 hashes.""" |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
170 |
md4 = _md4_new() |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
171 |
if md4: |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
172 |
md4.update(password) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
173 |
if encoding in DEFAULT_HEX: |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
174 |
digest = md4.hexdigest() |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
175 |
else: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
176 |
digest = b64encode(md4.digest()).decode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
177 |
return _format_digest(digest, scheme, encoding) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
178 |
return _dovecotpw(password, scheme, encoding) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
179 |
|
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 |
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
|
182 |
"""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
|
183 |
md5 = hashlib.md5() |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
184 |
if scheme == 'DIGEST-MD5': |
691
932cd76bf879
VMM: Dropped support for Dovecot versions before v1.2.0.
Pascal Volk <user@localhost.localdomain.org>
parents:
676
diff
changeset
|
185 |
md5.update(user.localpart.encode() + b':' + |
932cd76bf879
VMM: Dropped support for Dovecot versions before v1.2.0.
Pascal Volk <user@localhost.localdomain.org>
parents:
676
diff
changeset
|
186 |
user.domainname.encode() + b':') |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
187 |
md5.update(password) |
290
e2785e04f92e
VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
289
diff
changeset
|
188 |
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
|
189 |
(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
|
190 |
digest = md5.hexdigest() |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
191 |
else: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
192 |
digest = b64encode(md5.digest()).decode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
193 |
return _format_digest(digest, scheme, encoding) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
194 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
195 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
196 |
def _ntlm_hash(password, scheme, encoding): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
197 |
"""Generates NTLM hashes.""" |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
198 |
md4 = _md4_new() |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
199 |
if md4: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
200 |
password = b''.join(bytes(x) |
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
201 |
for x in zip(password, bytes(len(password)))) |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
202 |
md4.update(password) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
203 |
if encoding in DEFAULT_HEX: |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
204 |
digest = md4.hexdigest() |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
205 |
else: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
206 |
digest = b64encode(md4.digest()).decode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
207 |
return _format_digest(digest, scheme, encoding) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
208 |
return _dovecotpw(password, scheme, encoding) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
209 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
210 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
211 |
def _sha1_hash(password, scheme, encoding): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
212 |
"""Generates SHA1 aka SHA hashes.""" |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
213 |
sha1 = hashlib.sha1(password) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
214 |
if encoding in DEFAULT_B64: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
215 |
digest = b64encode(sha1.digest()).decode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
216 |
else: |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
217 |
digest = sha1.hexdigest() |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
218 |
return _format_digest(digest, scheme, encoding) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
219 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
220 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
221 |
def _sha256_hash(password, scheme, encoding): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
222 |
"""Generates SHA256 hashes.""" |
636
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
223 |
sha256 = hashlib.sha256(password) |
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
224 |
if encoding in DEFAULT_B64: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
225 |
digest = b64encode(sha256.digest()).decode() |
636
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
226 |
else: |
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
227 |
digest = sha256.hexdigest() |
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
228 |
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
|
229 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
230 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
231 |
def _sha512_hash(password, scheme, encoding): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
232 |
"""Generates SHA512 hashes.""" |
636
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
233 |
sha512 = hashlib.sha512(password) |
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
234 |
if encoding in DEFAULT_B64: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
235 |
digest = b64encode(sha512.digest()).decode() |
636
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
236 |
else: |
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
237 |
digest = sha512.hexdigest() |
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
238 |
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
|
239 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
240 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
241 |
def _smd5_hash(password, scheme, encoding): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
242 |
"""Generates SMD5 (salted PLAIN-MD5) hashes.""" |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
243 |
md5 = hashlib.md5(password) |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
244 |
salt = _get_salt(SALTED_ALGO_SALT_LEN).encode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
245 |
md5.update(salt) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
246 |
if encoding in DEFAULT_B64: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
247 |
digest = b64encode(md5.digest() + salt).decode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
248 |
else: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
249 |
digest = md5.hexdigest() + b2a_hex(salt).decode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
250 |
return _format_digest(digest, scheme, encoding) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
251 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
252 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
253 |
def _ssha1_hash(password, scheme, encoding): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
254 |
"""Generates SSHA (salted SHA/SHA1) hashes.""" |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
255 |
sha1 = hashlib.sha1(password) |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
256 |
salt = _get_salt(SALTED_ALGO_SALT_LEN).encode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
257 |
sha1.update(salt) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
258 |
if encoding in DEFAULT_B64: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
259 |
digest = b64encode(sha1.digest() + salt).decode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
260 |
else: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
261 |
digest = sha1.hexdigest() + b2a_hex(salt).decode() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
262 |
return _format_digest(digest, scheme, encoding) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
263 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
264 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
265 |
def _ssha256_hash(password, scheme, encoding): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
266 |
"""Generates SSHA256 (salted SHA256) hashes.""" |
636
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
267 |
sha256 = hashlib.sha256(password) |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
268 |
salt = _get_salt(SALTED_ALGO_SALT_LEN).encode() |
636
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
269 |
sha256.update(salt) |
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
270 |
if encoding in DEFAULT_B64: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
271 |
digest = b64encode(sha256.digest() + salt).decode() |
636
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
272 |
else: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
273 |
digest = sha256.hexdigest() + b2a_hex(salt).decode() |
636
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
274 |
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
|
275 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
276 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
277 |
def _ssha512_hash(password, scheme, encoding): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
278 |
"""Generates SSHA512 (salted SHA512) hashes.""" |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
279 |
salt = _get_salt(SALTED_ALGO_SALT_LEN).encode() |
636
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
280 |
sha512 = hashlib.sha512(password + salt) |
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
281 |
if encoding in DEFAULT_B64: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
282 |
digest = b64encode(sha512.digest() + salt).decode() |
636
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
283 |
else: |
651
6937cb38db71
VMM/password: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
284 |
digest = sha512.hexdigest() + b2a_hex(salt).decode() |
636
27334cfc0c90
VMM/pycompat: Removed module hashlib.
Pascal Volk <user@localhost.localdomain.org>
parents:
633
diff
changeset
|
285 |
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
|
286 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
287 |
_scheme_info = { |
664
b5cc967a45ad
VMM/password: Added scheme CLEAR.
Pascal Volk <user@localhost.localdomain.org>
parents:
655
diff
changeset
|
288 |
'CLEAR': (_clear_hash, 0x2010df00), |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
289 |
'CLEARTEXT': (_clear_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
290 |
'CRAM-MD5': (_dovecotpw, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
291 |
'CRYPT': (_crypt_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
292 |
'DIGEST-MD5': (_md5_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
293 |
'HMAC-MD5': (_dovecotpw, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
294 |
'LANMAN': (_dovecotpw, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
295 |
'LDAP-MD5': (_md5_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
296 |
'MD5': (_crypt_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
297 |
'MD5-CRYPT': (_crypt_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
298 |
'NTLM': (_ntlm_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
299 |
'OTP': (_dovecotpw, 0x10100a01), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
300 |
'PLAIN': (_clear_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
301 |
'PLAIN-MD4': (_md4_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
302 |
'PLAIN-MD5': (_md5_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
303 |
'RPA': (_dovecotpw, 0x10000f00), |
633
8ea32fbfc9df
VMM/password: Added scheme SCRAM-SHA-1.
Pascal Volk <user@localhost.localdomain.org>
parents:
568
diff
changeset
|
304 |
'SCRAM-SHA-1': (_dovecotpw, 0x20200a01), |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
305 |
'SHA': (_sha1_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
306 |
'SHA1': (_sha1_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
307 |
'SHA256': (_sha256_hash, 0x10100a01), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
308 |
'SHA512': (_sha512_hash, 0x20000b03), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
309 |
'SKEY': (_dovecotpw, 0x10100a01), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
310 |
'SMD5': (_smd5_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
311 |
'SSHA': (_ssha1_hash, 0x10000f00), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
312 |
'SSHA256': (_ssha256_hash, 0x10200a04), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
313 |
'SSHA512': (_ssha512_hash, 0x20000b03), |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
314 |
} |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
315 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
316 |
|
461
cabdf94ec580
VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
417
diff
changeset
|
317 |
def list_schemes(): |
cabdf94ec580
VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
417
diff
changeset
|
318 |
"""Returns the tuple (schemes, encodings). |
cabdf94ec580
VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
417
diff
changeset
|
319 |
|
cabdf94ec580
VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
417
diff
changeset
|
320 |
`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
|
321 |
the used Dovecot version and features of the libc). |
691
932cd76bf879
VMM: Dropped support for Dovecot versions before v1.2.0.
Pascal Volk <user@localhost.localdomain.org>
parents:
676
diff
changeset
|
322 |
`encodings` is a tuple with all usable encoding suffixes. |
461
cabdf94ec580
VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
417
diff
changeset
|
323 |
""" |
cabdf94ec580
VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
417
diff
changeset
|
324 |
dcv = cfg_dget('misc.dovecot_version') |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
638
diff
changeset
|
325 |
schemes = (k for (k, v) in _scheme_info.items() if v[1] <= dcv) |
691
932cd76bf879
VMM: Dropped support for Dovecot versions before v1.2.0.
Pascal Volk <user@localhost.localdomain.org>
parents:
676
diff
changeset
|
326 |
encodings = ('.B64', '.BASE64', '.HEX') |
461
cabdf94ec580
VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
417
diff
changeset
|
327 |
return schemes, encodings |
cabdf94ec580
VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
417
diff
changeset
|
328 |
|
cabdf94ec580
VMM/password: Added function list_schemes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
417
diff
changeset
|
329 |
|
287
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
330 |
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
|
331 |
"""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
|
332 |
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
|
333 |
|
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
334 |
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
|
335 |
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
|
336 |
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
|
337 |
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
|
338 |
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
|
339 |
|
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
340 |
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
|
341 |
* is unknown |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
342 |
* 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
|
343 |
* 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
|
344 |
""" |
655
2bf68600e914
VMM/password: Code cleanups:
Pascal Volk <user@localhost.localdomain.org>
parents:
651
diff
changeset
|
345 |
assert isinstance(scheme, str), 'Not a str: {!r}'.format(scheme) |
287
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
346 |
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
|
347 |
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
|
348 |
if scheme not in _scheme_info: |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
638
diff
changeset
|
349 |
raise VMMError(_("Unsupported password scheme: '%s'") % scheme, |
287
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
350 |
VMM_ERROR) |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
351 |
if cfg_dget('misc.dovecot_version') < _scheme_info[scheme][1]: |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
638
diff
changeset
|
352 |
raise VMMError(_("The password scheme '%(scheme)s' requires Dovecot " |
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
638
diff
changeset
|
353 |
">= 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
|
354 |
'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
|
355 |
VMM_ERROR) |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
356 |
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
|
357 |
if scheme_encoding[1] not in ('B64', 'BASE64', 'HEX'): |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
638
diff
changeset
|
358 |
raise VMMError(_("Unsupported password encoding: '%s'") % |
287
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
359 |
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
|
360 |
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
|
361 |
else: |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
362 |
encoding = None |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
363 |
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
|
364 |
|
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
365 |
|
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
366 |
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
|
367 |
"""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
|
368 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
369 |
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
|
370 |
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
|
371 |
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
|
372 |
""" |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
638
diff
changeset
|
373 |
if not isinstance(password, str): |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
374 |
raise TypeError('Password is not a string: %r' % password) |
655
2bf68600e914
VMM/password: Code cleanups:
Pascal Volk <user@localhost.localdomain.org>
parents:
651
diff
changeset
|
375 |
password = password.encode(ENCODING).strip() |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
376 |
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
|
377 |
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
|
378 |
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
|
379 |
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
|
380 |
scheme, encoding = verify_scheme(scheme) |
268
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
381 |
if scheme == 'DIGEST-MD5': |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
382 |
assert isinstance(user, EmailAddress) |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
383 |
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
|
384 |
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
|
385 |
|
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 |
def randompw(): |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
388 |
"""Generates a plain text random password. |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
389 |
|
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
390 |
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
|
391 |
(account.password_length). |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
392 |
""" |
272
446483386914
VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
268
diff
changeset
|
393 |
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
|
394 |
if pw_len < 8: |
beb8f4421f92
VMM: added new modules password and pycompat.hashlib.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
395 |
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
|
396 |
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
|
397 |
|
291
7ef3f117a230
VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
398 |
|
7ef3f117a230
VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
399 |
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
|
400 |
"""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
|
401 |
_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
|
402 |
_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
|
403 |
9qrb1jehe7hn4' |
7ef3f117a230
VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
404 |
_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
|
405 |
JoouxdSqJO71l9Ld3tVrfOatEjarhghvEYADkq//LpDnTeO90tcbtHR1' |
7ef3f117a230
VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
406 |
|
7ef3f117a230
VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
407 |
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
|
408 |
_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
|
409 |
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
|
410 |
_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
|
411 |
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
|
412 |
_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
|
413 |
|
7ef3f117a230
VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
414 |
_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
|
415 |
del _, cfg_dget, _test_crypt_algorithms |