From e4367653d0f5d2a2b94b1f4e68f8dacd25086f76 Mon Sep 17 00:00:00 2001
From: Aaron LI <aly@aaronly.me>
Date: Sat, 17 Mar 2018 14:04:34 +0800
Subject: Add custom filter mail.py with dovecot_makepass

---
 filter_plugins/mail.py | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 filter_plugins/mail.py

diff --git a/filter_plugins/mail.py b/filter_plugins/mail.py
new file mode 100644
index 0000000..1735e62
--- /dev/null
+++ b/filter_plugins/mail.py
@@ -0,0 +1,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,
+        }
-- 
cgit v1.2.2