aboutsummaryrefslogtreecommitdiffstats
path: root/filter_plugins/mail.py
blob: 1735e62968bfb314600965a5563d10dd2f561711 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Copyright (c) 2018 Aaron LI <aly@aaronly.me>
# MIT License

"""
Custom Ansible template filters for "mail" role.
"""

import os
import base64
import crypt


def dovecot_makepass(p):
    """
    Generate the salted hashed password for Dovecot using the
    SHA512-CRYPT scheme.

    Implement the "doveadm pw -s SHA512-CRYPT" command.

    Dovecot password format: {<scheme>}$<type>$<salt>$<hash>
    """
    method, htype = "SHA512", "$6$"
    scheme = method + "-CRYPT"
    saltlen = 16
    salt = os.urandom(saltlen)
    salt = base64.b64encode(salt)[:saltlen]
    return "{%s}%s" % (scheme, crypt.crypt(p, htype+salt))


class FilterModule(object):
    def filters(self):
        return {
            "dovecot_makepass": dovecot_makepass,
        }