2014-06-30 14:20:58 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
import subprocess, shutil, os, sqlite3, re
|
2014-06-09 12:09:45 +00:00
|
|
|
import utils
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2014-07-13 12:13:41 +00:00
|
|
|
def validate_email(email, mode=None):
|
2014-06-06 13:58:58 +00:00
|
|
|
# There are a lot of characters permitted in email addresses, but
|
|
|
|
# Dovecot's sqlite driver seems to get confused if there are any
|
|
|
|
# unusual characters in the address. Bah. Also note that since
|
|
|
|
# the mailbox path name is based on the email address, the address
|
|
|
|
# shouldn't be absurdly long and must not have a forward slash.
|
|
|
|
|
|
|
|
if len(email) > 255: return False
|
|
|
|
|
2014-07-13 12:13:41 +00:00
|
|
|
if mode == 'user':
|
2014-06-06 13:58:58 +00:00
|
|
|
# For Dovecot's benefit, only allow basic characters.
|
|
|
|
ATEXT = r'[\w\-]'
|
2014-07-13 12:13:41 +00:00
|
|
|
elif mode == 'alias':
|
|
|
|
# For aliases, we can allow any valid email address.
|
2014-06-06 13:58:58 +00:00
|
|
|
# Based on RFC 2822 and https://github.com/SyrusAkbary/validate_email/blob/master/validate_email.py,
|
2014-07-13 12:13:41 +00:00
|
|
|
# these characters are permitted in email addresses.
|
2014-06-06 13:58:58 +00:00
|
|
|
ATEXT = r'[\w!#$%&\'\*\+\-/=\?\^`\{\|\}~]' # see 3.2.4
|
2014-07-13 12:13:41 +00:00
|
|
|
else:
|
|
|
|
raise ValueError(mode)
|
|
|
|
|
|
|
|
# per RFC 2822 3.2.4
|
|
|
|
DOT_ATOM_TEXT_LOCAL = ATEXT + r'+(?:\.' + ATEXT + r'+)*'
|
|
|
|
if mode == 'alias':
|
|
|
|
# For aliases, Postfix accepts '@domain.tld' format for
|
|
|
|
# catch-all addresses. Make the local part optional.
|
|
|
|
DOT_ATOM_TEXT_LOCAL = '(?:' + DOT_ATOM_TEXT_LOCAL + ')?'
|
2014-06-06 13:58:58 +00:00
|
|
|
|
2014-07-13 12:13:41 +00:00
|
|
|
# as above, but we can require that the host part have at least
|
|
|
|
# one period in it, so use a "+" rather than a "*" at the end
|
|
|
|
DOT_ATOM_TEXT_HOST = ATEXT + r'+(?:\.' + ATEXT + r'+)+'
|
2014-07-12 11:17:13 +00:00
|
|
|
|
2014-07-13 12:13:41 +00:00
|
|
|
# per RFC 2822 3.4.1
|
|
|
|
ADDR_SPEC = '^%s@%s$' % (DOT_ATOM_TEXT_LOCAL, DOT_ATOM_TEXT_HOST)
|
2014-06-06 13:58:58 +00:00
|
|
|
|
|
|
|
return re.match(ADDR_SPEC, email)
|
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
def open_database(env, with_connection=False):
|
|
|
|
conn = sqlite3.connect(env["STORAGE_ROOT"] + "/mail/users.sqlite")
|
|
|
|
if not with_connection:
|
|
|
|
return conn.cursor()
|
|
|
|
else:
|
|
|
|
return conn, conn.cursor()
|
|
|
|
|
2014-10-07 19:28:07 +00:00
|
|
|
def get_mail_users(env):
|
|
|
|
# Returns a flat, sorted list of all user accounts.
|
|
|
|
c = open_database(env)
|
|
|
|
c.execute('SELECT email FROM users')
|
|
|
|
users = [ row[0] for row in c.fetchall() ]
|
|
|
|
return utils.sort_email_addresses(users, env)
|
|
|
|
|
2014-10-07 20:24:11 +00:00
|
|
|
def get_mail_users_ex(env, with_archived=False, with_slow_info=False):
|
2014-10-07 19:28:07 +00:00
|
|
|
# Returns a complex data structure of all user accounts, optionally
|
|
|
|
# including archived (status="inactive") accounts.
|
|
|
|
#
|
|
|
|
# [
|
|
|
|
# {
|
|
|
|
# domain: "domain.tld",
|
|
|
|
# users: [
|
|
|
|
# {
|
|
|
|
# email: "name@domain.tld",
|
|
|
|
# privileges: [ "priv1", "priv2", ... ],
|
|
|
|
# status: "active",
|
|
|
|
# aliases: [
|
|
|
|
# ("alias@domain.tld", ["indirect.alias@domain.tld", ...]),
|
|
|
|
# ...
|
|
|
|
# ]
|
|
|
|
# },
|
|
|
|
# ...
|
|
|
|
# ]
|
|
|
|
# },
|
|
|
|
# ...
|
|
|
|
# ]
|
|
|
|
|
|
|
|
# Pre-load all aliases.
|
|
|
|
aliases = get_mail_alias_map(env)
|
|
|
|
|
|
|
|
# Get users and their privileges.
|
|
|
|
users = []
|
|
|
|
active_accounts = set()
|
2014-06-03 13:24:48 +00:00
|
|
|
c = open_database(env)
|
2014-08-08 12:31:22 +00:00
|
|
|
c.execute('SELECT email, privileges FROM users')
|
2014-10-07 19:28:07 +00:00
|
|
|
for email, privileges in c.fetchall():
|
|
|
|
active_accounts.add(email)
|
2014-10-07 20:24:11 +00:00
|
|
|
|
|
|
|
user = {
|
2014-10-07 19:28:07 +00:00
|
|
|
"email": email,
|
|
|
|
"privileges": parse_privs(privileges),
|
|
|
|
"status": "active",
|
2014-10-07 20:24:11 +00:00
|
|
|
}
|
|
|
|
users.append(user)
|
|
|
|
|
|
|
|
if with_slow_info:
|
|
|
|
user["aliases"] = [
|
2014-10-07 19:28:07 +00:00
|
|
|
(alias, sorted(evaluate_mail_alias_map(alias, aliases, env)))
|
|
|
|
for alias in aliases.get(email.lower(), [])
|
|
|
|
]
|
2014-10-07 20:24:11 +00:00
|
|
|
user["mailbox_size"] = utils.du(os.path.join(env['STORAGE_ROOT'], 'mail/mailboxes', *reversed(email.split("@"))))
|
2014-10-07 19:28:07 +00:00
|
|
|
|
|
|
|
# Add in archived accounts.
|
|
|
|
if with_archived:
|
|
|
|
root = os.path.join(env['STORAGE_ROOT'], 'mail/mailboxes')
|
|
|
|
for domain in os.listdir(root):
|
|
|
|
for user in os.listdir(os.path.join(root, domain)):
|
|
|
|
email = user + "@" + domain
|
2014-10-07 20:24:11 +00:00
|
|
|
mbox = os.path.join(root, domain, user)
|
2014-10-07 19:28:07 +00:00
|
|
|
if email in active_accounts: continue
|
2014-10-07 20:24:11 +00:00
|
|
|
user = {
|
2014-10-07 19:28:07 +00:00
|
|
|
"email": email,
|
|
|
|
"privileges": "",
|
|
|
|
"status": "inactive",
|
2014-10-07 20:24:11 +00:00
|
|
|
"mailbox": mbox,
|
|
|
|
}
|
|
|
|
users.append(user)
|
|
|
|
if with_slow_info:
|
|
|
|
user["mailbox_size"] = utils.du(mbox)
|
2014-10-07 19:28:07 +00:00
|
|
|
|
|
|
|
# Group by domain.
|
|
|
|
domains = { }
|
|
|
|
for user in users:
|
|
|
|
domain = get_domain(user["email"])
|
|
|
|
if domain not in domains:
|
|
|
|
domains[domain] = {
|
|
|
|
"domain": domain,
|
|
|
|
"users": []
|
|
|
|
}
|
|
|
|
domains[domain]["users"].append(user)
|
|
|
|
|
|
|
|
# Sort domains.
|
|
|
|
domains = [domains[domain] for domain in utils.sort_domains(domains.keys(), env)]
|
|
|
|
|
|
|
|
# Sort users within each domain first by status then lexicographically by email address.
|
|
|
|
for domain in domains:
|
|
|
|
domain["users"].sort(key = lambda user : (user["status"] != "active", user["email"]))
|
|
|
|
|
|
|
|
return domains
|
|
|
|
|
|
|
|
def get_admins(env):
|
|
|
|
# Returns a set of users with admin privileges.
|
|
|
|
users = set()
|
|
|
|
for domain in get_mail_users_ex(env):
|
|
|
|
for user in domain["users"]:
|
|
|
|
if "admin" in user["privileges"]:
|
|
|
|
users.add(user["email"])
|
|
|
|
return users
|
2014-08-17 22:43:57 +00:00
|
|
|
|
2014-10-07 19:47:30 +00:00
|
|
|
def get_mail_aliases(env):
|
|
|
|
# Returns a sorted list of tuples of (alias, forward-to string).
|
2014-06-03 13:24:48 +00:00
|
|
|
c = open_database(env)
|
|
|
|
c.execute('SELECT source, destination FROM aliases')
|
2014-08-17 22:43:57 +00:00
|
|
|
aliases = { row[0]: row[1] for row in c.fetchall() } # make dict
|
|
|
|
|
|
|
|
# put in a canonical order: sort by domain, then by email address lexicographically
|
2014-10-07 19:47:30 +00:00
|
|
|
aliases = [ (source, aliases[source]) for source in utils.sort_email_addresses(aliases.keys(), env) ]
|
2014-08-17 22:43:57 +00:00
|
|
|
return aliases
|
|
|
|
|
2014-10-07 19:47:30 +00:00
|
|
|
def get_mail_aliases_ex(env):
|
|
|
|
# Returns a complex data structure of all mail aliases, similar
|
|
|
|
# to get_mail_users_ex.
|
|
|
|
#
|
|
|
|
# [
|
|
|
|
# {
|
|
|
|
# domain: "domain.tld",
|
|
|
|
# alias: [
|
|
|
|
# {
|
|
|
|
# source: "name@domain.tld",
|
|
|
|
# destination: ["target1@domain.com", "target2@domain.com", ...],
|
|
|
|
# required: True|False
|
|
|
|
# },
|
|
|
|
# ...
|
|
|
|
# ]
|
|
|
|
# },
|
|
|
|
# ...
|
|
|
|
# ]
|
|
|
|
|
|
|
|
required_aliases = get_required_aliases(env)
|
|
|
|
domains = {}
|
|
|
|
for source, destination in get_mail_aliases(env):
|
|
|
|
# get alias info
|
|
|
|
domain = get_domain(source)
|
|
|
|
required = ((source in required_aliases) or (source == get_system_administrator(env)))
|
|
|
|
|
|
|
|
# add to list
|
|
|
|
if not domain in domains:
|
|
|
|
domains[domain] = {
|
|
|
|
"domain": domain,
|
|
|
|
"aliases": [],
|
|
|
|
}
|
|
|
|
domains[domain]["aliases"].append({
|
|
|
|
"source": source,
|
|
|
|
"destination": [d.strip() for d in destination.split(",")],
|
|
|
|
"required": required,
|
|
|
|
})
|
|
|
|
|
|
|
|
# Sort domains.
|
|
|
|
domains = [domains[domain] for domain in utils.sort_domains(domains.keys(), env)]
|
|
|
|
|
|
|
|
# Sort aliases within each domain first by required-ness then lexicographically by source address.
|
|
|
|
for domain in domains:
|
|
|
|
domain["aliases"].sort(key = lambda alias : (alias["required"], alias["source"]))
|
|
|
|
return domains
|
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
def get_mail_alias_map(env):
|
|
|
|
aliases = { }
|
|
|
|
for alias, targets in get_mail_aliases(env):
|
|
|
|
for em in targets.split(","):
|
|
|
|
em = em.strip().lower()
|
|
|
|
aliases.setdefault(em, []).append(alias)
|
|
|
|
return aliases
|
|
|
|
|
|
|
|
def evaluate_mail_alias_map(email, aliases, env):
|
|
|
|
ret = set()
|
|
|
|
for alias in aliases.get(email.lower(), []):
|
|
|
|
ret.add(alias)
|
|
|
|
ret |= evaluate_mail_alias_map(alias, aliases, env)
|
|
|
|
return ret
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2014-10-07 19:28:07 +00:00
|
|
|
def get_domain(emailaddr):
|
|
|
|
return emailaddr.split('@', 1)[1]
|
|
|
|
|
2014-07-09 19:29:46 +00:00
|
|
|
def get_mail_domains(env, filter_aliases=lambda alias : True):
|
|
|
|
return set(
|
|
|
|
[get_domain(addr) for addr in get_mail_users(env)]
|
|
|
|
+ [get_domain(source) for source, target in get_mail_aliases(env) if filter_aliases((source, target)) ]
|
|
|
|
)
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
def add_mail_user(email, pw, privs, env):
|
|
|
|
# validate email
|
|
|
|
if email.strip() == "":
|
|
|
|
return ("No email address provided.", 400)
|
2014-07-13 12:13:41 +00:00
|
|
|
if not validate_email(email, mode='user'):
|
2014-06-03 13:24:48 +00:00
|
|
|
return ("Invalid email address.", 400)
|
|
|
|
|
2014-09-21 17:24:01 +00:00
|
|
|
validate_password(pw)
|
2014-08-17 22:43:57 +00:00
|
|
|
|
|
|
|
# validate privileges
|
|
|
|
if privs is None or privs.strip() == "":
|
|
|
|
privs = []
|
|
|
|
else:
|
|
|
|
privs = privs.split("\n")
|
|
|
|
for p in privs:
|
|
|
|
validation = validate_privilege(p)
|
|
|
|
if validation: return validation
|
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
# get the database
|
|
|
|
conn, c = open_database(env, with_connection=True)
|
|
|
|
|
|
|
|
# hash the password
|
2014-06-09 12:09:45 +00:00
|
|
|
pw = utils.shell('check_output', ["/usr/bin/doveadm", "pw", "-s", "SHA512-CRYPT", "-p", pw]).strip()
|
2014-06-03 13:24:48 +00:00
|
|
|
|
|
|
|
# add the user to the database
|
|
|
|
try:
|
2014-08-17 22:43:57 +00:00
|
|
|
c.execute("INSERT INTO users (email, password, privileges) VALUES (?, ?, ?)",
|
|
|
|
(email, pw, "\n".join(privs)))
|
2014-06-03 13:24:48 +00:00
|
|
|
except sqlite3.IntegrityError:
|
|
|
|
return ("User already exists.", 400)
|
2014-07-12 11:17:13 +00:00
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
# write databasebefore next step
|
|
|
|
conn.commit()
|
|
|
|
|
2014-08-09 16:49:57 +00:00
|
|
|
# Create the user's INBOX, Spam, and Drafts folders, and subscribe them.
|
|
|
|
# K-9 mail will poll every 90 seconds if a Drafts folder does not exist, so create it
|
|
|
|
# to avoid unnecessary polling.
|
2014-06-03 13:24:48 +00:00
|
|
|
|
|
|
|
# Check if the mailboxes exist before creating them. When creating a user that had previously
|
|
|
|
# been deleted, the mailboxes will still exist because they are still on disk.
|
2014-06-06 13:58:58 +00:00
|
|
|
try:
|
2014-06-09 12:09:45 +00:00
|
|
|
existing_mboxes = utils.shell('check_output', ["doveadm", "mailbox", "list", "-u", email, "-8"], capture_stderr=True).split("\n")
|
2014-06-06 13:58:58 +00:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
c.execute("DELETE FROM users WHERE email=?", (email,))
|
|
|
|
conn.commit()
|
|
|
|
return ("Failed to initialize the user: " + e.output.decode("utf8"), 400)
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2014-08-09 16:49:57 +00:00
|
|
|
for folder in ("INBOX", "Spam", "Drafts"):
|
|
|
|
if folder not in existing_mboxes:
|
|
|
|
utils.shell('check_call', ["doveadm", "mailbox", "create", "-u", email, "-s", folder])
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2014-07-09 19:29:46 +00:00
|
|
|
# Update things in case any new domains are added.
|
2014-07-06 12:16:50 +00:00
|
|
|
return kick(env, "mail user added")
|
2014-06-03 13:24:48 +00:00
|
|
|
|
|
|
|
def set_mail_password(email, pw, env):
|
2014-09-21 17:24:01 +00:00
|
|
|
validate_password(pw)
|
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
# hash the password
|
2014-06-09 12:09:45 +00:00
|
|
|
pw = utils.shell('check_output', ["/usr/bin/doveadm", "pw", "-s", "SHA512-CRYPT", "-p", pw]).strip()
|
2014-06-03 13:24:48 +00:00
|
|
|
|
|
|
|
# update the database
|
|
|
|
conn, c = open_database(env, with_connection=True)
|
|
|
|
c.execute("UPDATE users SET password=? WHERE email=?", (pw, email))
|
|
|
|
if c.rowcount != 1:
|
|
|
|
return ("That's not a user (%s)." % email, 400)
|
|
|
|
conn.commit()
|
|
|
|
return "OK"
|
|
|
|
|
|
|
|
def remove_mail_user(email, env):
|
|
|
|
conn, c = open_database(env, with_connection=True)
|
|
|
|
c.execute("DELETE FROM users WHERE email=?", (email,))
|
|
|
|
if c.rowcount != 1:
|
|
|
|
return ("That's not a user (%s)." % email, 400)
|
|
|
|
conn.commit()
|
|
|
|
|
2014-07-09 19:29:46 +00:00
|
|
|
# Update things in case any domains are removed.
|
2014-07-06 12:16:50 +00:00
|
|
|
return kick(env, "mail user removed")
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2014-08-08 12:31:22 +00:00
|
|
|
def parse_privs(value):
|
|
|
|
return [p for p in value.split("\n") if p.strip() != ""]
|
|
|
|
|
|
|
|
def get_mail_user_privileges(email, env):
|
|
|
|
c = open_database(env)
|
|
|
|
c.execute('SELECT privileges FROM users WHERE email=?', (email,))
|
|
|
|
rows = c.fetchall()
|
|
|
|
if len(rows) != 1:
|
|
|
|
return ("That's not a user (%s)." % email, 400)
|
|
|
|
return parse_privs(rows[0][0])
|
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
def validate_privilege(priv):
|
2014-08-08 12:31:22 +00:00
|
|
|
if "\n" in priv or priv.strip() == "":
|
|
|
|
return ("That's not a valid privilege (%s)." % priv, 400)
|
2014-08-17 22:43:57 +00:00
|
|
|
return None
|
2014-08-08 12:31:22 +00:00
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
def add_remove_mail_user_privilege(email, priv, action, env):
|
|
|
|
# validate
|
|
|
|
validation = validate_privilege(priv)
|
|
|
|
if validation: return validation
|
|
|
|
|
|
|
|
# get existing privs, but may fail
|
2014-08-08 12:31:22 +00:00
|
|
|
privs = get_mail_user_privileges(email, env)
|
|
|
|
if isinstance(privs, tuple): return privs # error
|
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
# update privs set
|
2014-08-08 12:31:22 +00:00
|
|
|
if action == "add":
|
|
|
|
if priv not in privs:
|
|
|
|
privs.append(priv)
|
|
|
|
elif action == "remove":
|
|
|
|
privs = [p for p in privs if p != priv]
|
|
|
|
else:
|
|
|
|
return ("Invalid action.", 400)
|
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
# commit to database
|
2014-08-08 12:31:22 +00:00
|
|
|
conn, c = open_database(env, with_connection=True)
|
|
|
|
c.execute("UPDATE users SET privileges=? WHERE email=?", ("\n".join(privs), email))
|
|
|
|
if c.rowcount != 1:
|
|
|
|
return ("Something went wrong.", 400)
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
return "OK"
|
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
def add_mail_alias(source, destination, env, update_if_exists=False, do_kick=True):
|
|
|
|
# validate source
|
|
|
|
if source.strip() == "":
|
|
|
|
return ("No incoming email address provided.", 400)
|
2014-07-13 12:13:41 +00:00
|
|
|
if not validate_email(source, mode='alias'):
|
2014-08-17 22:43:57 +00:00
|
|
|
return ("Invalid incoming email address (%s)." % source, 400)
|
|
|
|
|
|
|
|
# parse comma and \n-separated destination emails & validate
|
|
|
|
dests = []
|
|
|
|
for line in destination.split("\n"):
|
|
|
|
for email in line.split(","):
|
|
|
|
email = email.strip()
|
|
|
|
if email == "": continue
|
|
|
|
if not validate_email(email, mode='alias'):
|
|
|
|
return ("Invalid destination email address (%s)." % email, 400)
|
|
|
|
dests.append(email)
|
|
|
|
if len(destination) == 0:
|
|
|
|
return ("No destination email address(es) provided.", 400)
|
|
|
|
destination = ",".join(dests)
|
2014-06-06 13:58:58 +00:00
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
conn, c = open_database(env, with_connection=True)
|
|
|
|
try:
|
|
|
|
c.execute("INSERT INTO aliases (source, destination) VALUES (?, ?)", (source, destination))
|
2014-08-17 22:43:57 +00:00
|
|
|
return_status = "alias added"
|
2014-06-03 13:24:48 +00:00
|
|
|
except sqlite3.IntegrityError:
|
2014-08-17 22:43:57 +00:00
|
|
|
if not update_if_exists:
|
|
|
|
return ("Alias already exists (%s)." % source, 400)
|
|
|
|
else:
|
|
|
|
c.execute("UPDATE aliases SET destination = ? WHERE source = ?", (destination, source))
|
|
|
|
return_status = "alias updated"
|
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
conn.commit()
|
|
|
|
|
2014-07-09 19:29:46 +00:00
|
|
|
if do_kick:
|
|
|
|
# Update things in case any new domains are added.
|
2014-08-17 22:43:57 +00:00
|
|
|
return kick(env, return_status)
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2014-07-09 19:29:46 +00:00
|
|
|
def remove_mail_alias(source, env, do_kick=True):
|
2014-06-03 13:24:48 +00:00
|
|
|
conn, c = open_database(env, with_connection=True)
|
|
|
|
c.execute("DELETE FROM aliases WHERE source=?", (source,))
|
|
|
|
if c.rowcount != 1:
|
|
|
|
return ("That's not an alias (%s)." % source, 400)
|
|
|
|
conn.commit()
|
|
|
|
|
2014-07-09 19:29:46 +00:00
|
|
|
if do_kick:
|
|
|
|
# Update things in case any domains are removed.
|
|
|
|
return kick(env, "alias removed")
|
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
def get_system_administrator(env):
|
|
|
|
return "administrator@" + env['PRIMARY_HOSTNAME']
|
|
|
|
|
|
|
|
def get_required_aliases(env):
|
|
|
|
# These are the aliases that must exist.
|
|
|
|
aliases = set()
|
|
|
|
|
2014-09-09 11:41:44 +00:00
|
|
|
# The hostmaster alias is exposed in the DNS SOA for each zone.
|
2014-08-17 22:43:57 +00:00
|
|
|
aliases.add("hostmaster@" + env['PRIMARY_HOSTNAME'])
|
|
|
|
|
|
|
|
# Get a list of domains we serve mail for, except ones for which the only
|
|
|
|
# email on that domain is a postmaster/admin alias to the administrator.
|
|
|
|
real_mail_domains = get_mail_domains(env,
|
|
|
|
filter_aliases = lambda alias : \
|
|
|
|
(not alias[0].startswith("postmaster@") \
|
|
|
|
and not alias[0].startswith("admin@")) \
|
|
|
|
or alias[1] != get_system_administrator(env) \
|
|
|
|
)
|
|
|
|
|
|
|
|
# Create postmaster@ and admin@ for all domains we serve mail on.
|
|
|
|
# postmaster@ is assumed to exist by our Postfix configuration. admin@
|
|
|
|
# isn't anything, but it might save the user some trouble e.g. when
|
|
|
|
# buying an SSL certificate.
|
|
|
|
for domain in real_mail_domains:
|
|
|
|
aliases.add("postmaster@" + domain)
|
|
|
|
aliases.add("admin@" + domain)
|
|
|
|
|
|
|
|
return aliases
|
|
|
|
|
2014-07-09 19:29:46 +00:00
|
|
|
def kick(env, mail_result=None):
|
|
|
|
results = []
|
|
|
|
|
|
|
|
# Inclde the current operation's result in output.
|
|
|
|
|
|
|
|
if mail_result is not None:
|
|
|
|
results.append(mail_result + "\n")
|
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
# Ensure every required alias exists.
|
2014-07-09 19:29:46 +00:00
|
|
|
|
2014-09-09 11:41:44 +00:00
|
|
|
existing_users = get_mail_users(env)
|
2014-07-09 19:29:46 +00:00
|
|
|
existing_aliases = get_mail_aliases(env)
|
2014-08-17 22:43:57 +00:00
|
|
|
required_aliases = get_required_aliases(env)
|
2014-07-09 19:29:46 +00:00
|
|
|
|
|
|
|
def ensure_admin_alias_exists(source):
|
2014-09-09 11:41:44 +00:00
|
|
|
# If a user account exists with that address, we're good.
|
|
|
|
if source in existing_users:
|
|
|
|
return
|
|
|
|
|
2014-07-09 19:29:46 +00:00
|
|
|
# Does this alias exists?
|
|
|
|
for s, t in existing_aliases:
|
|
|
|
if s == source:
|
|
|
|
return
|
|
|
|
# Doesn't exist.
|
2014-08-17 22:43:57 +00:00
|
|
|
administrator = get_system_administrator(env)
|
2014-07-09 19:29:46 +00:00
|
|
|
add_mail_alias(source, administrator, env, do_kick=False)
|
|
|
|
results.append("added alias %s (=> %s)\n" % (source, administrator))
|
|
|
|
|
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
for alias in required_aliases:
|
|
|
|
ensure_admin_alias_exists(alias)
|
2014-07-09 19:29:46 +00:00
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
# Remove auto-generated postmaster/admin on domains we no
|
2014-07-09 19:29:46 +00:00
|
|
|
# longer have any other email addresses for.
|
|
|
|
for source, target in existing_aliases:
|
|
|
|
user, domain = source.split("@")
|
2014-08-17 22:43:57 +00:00
|
|
|
if user in ("postmaster", "admin") \
|
|
|
|
and source not in required_aliases \
|
|
|
|
and target == get_system_administrator(env):
|
2014-07-09 19:29:46 +00:00
|
|
|
remove_mail_alias(source, env, do_kick=False)
|
|
|
|
results.append("removed alias %s (was to %s; domain no longer used for email)\n" % (source, target))
|
2014-07-06 12:16:50 +00:00
|
|
|
|
|
|
|
# Update DNS and nginx in case any domains are added/removed.
|
2014-07-09 19:29:46 +00:00
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
from dns_update import do_dns_update
|
2014-07-09 19:29:46 +00:00
|
|
|
results.append( do_dns_update(env) )
|
|
|
|
|
2014-07-06 12:16:50 +00:00
|
|
|
from web_update import do_web_update
|
2014-07-09 19:29:46 +00:00
|
|
|
results.append( do_web_update(env) )
|
|
|
|
|
2014-07-06 12:16:50 +00:00
|
|
|
return "".join(s for s in results if s != "")
|
2014-06-30 14:20:58 +00:00
|
|
|
|
2014-09-21 17:24:01 +00:00
|
|
|
def validate_password(pw):
|
|
|
|
# validate password
|
|
|
|
if pw.strip() == "":
|
|
|
|
raise ValueError("No password provided.")
|
|
|
|
if re.search(r"[\s]", pw):
|
|
|
|
raise ValueError("Passwords cannot contain spaces.")
|
|
|
|
if len(pw) < 4:
|
|
|
|
raise ValueError("Passwords must be at least four characters.")
|
|
|
|
|
|
|
|
|
2014-06-30 14:20:58 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import sys
|
|
|
|
if len(sys.argv) > 2 and sys.argv[1] == "validate-email":
|
|
|
|
# Validate that we can create a Dovecot account for a given string.
|
2014-07-13 12:13:41 +00:00
|
|
|
if validate_email(sys.argv[2], mode='user'):
|
2014-06-30 14:20:58 +00:00
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
sys.exit(1)
|
2014-07-09 19:29:46 +00:00
|
|
|
|
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == "update":
|
|
|
|
from utils import load_environment
|
|
|
|
print(kick(load_environment()))
|