2013-08-31 14:50:59 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2015-03-30 17:59:07 +00:00
|
|
|
import sys, getpass, urllib.request, urllib.error, json, re
|
2013-08-31 14:50:59 +00:00
|
|
|
|
2014-08-08 12:31:22 +00:00
|
|
|
def mgmt(cmd, data=None, is_json=False):
|
2014-10-05 12:55:28 +00:00
|
|
|
# The base URL for the management daemon. (Listens on IPv4 only.)
|
|
|
|
mgmt_uri = 'http://127.0.0.1:10222'
|
2014-06-21 23:49:09 +00:00
|
|
|
|
|
|
|
setup_key_auth(mgmt_uri)
|
|
|
|
|
|
|
|
req = urllib.request.Request(mgmt_uri + cmd, urllib.parse.urlencode(data).encode("utf8") if data else None)
|
2014-06-03 13:24:48 +00:00
|
|
|
try:
|
|
|
|
response = urllib.request.urlopen(req)
|
|
|
|
except urllib.error.HTTPError as e:
|
2014-07-25 13:51:35 +00:00
|
|
|
if e.code == 401:
|
2014-08-17 22:43:57 +00:00
|
|
|
try:
|
|
|
|
print(e.read().decode("utf8"))
|
|
|
|
except:
|
|
|
|
pass
|
2014-07-25 13:51:35 +00:00
|
|
|
print("The management daemon refused access. The API key file may be out of sync. Try 'service mailinabox restart'.", file=sys.stderr)
|
|
|
|
elif hasattr(e, 'read'):
|
|
|
|
print(e.read().decode('utf8'), file=sys.stderr)
|
|
|
|
else:
|
|
|
|
print(e, file=sys.stderr)
|
2014-06-03 13:24:48 +00:00
|
|
|
sys.exit(1)
|
2014-08-08 12:31:22 +00:00
|
|
|
resp = response.read().decode('utf8')
|
|
|
|
if is_json: resp = json.loads(resp)
|
|
|
|
return resp
|
2013-08-31 14:50:59 +00:00
|
|
|
|
2014-06-06 21:07:30 +00:00
|
|
|
def read_password():
|
2015-03-29 16:54:37 +00:00
|
|
|
while True:
|
|
|
|
first = getpass.getpass('password: ')
|
2017-02-14 19:24:59 +00:00
|
|
|
if len(first) < 8:
|
|
|
|
print("Passwords must be at least eight characters.")
|
2015-03-30 17:59:07 +00:00
|
|
|
continue
|
|
|
|
if re.search(r'[\s]', first):
|
|
|
|
print("Passwords cannot contain spaces.")
|
2015-03-29 16:54:37 +00:00
|
|
|
continue
|
|
|
|
second = getpass.getpass(' (again): ')
|
|
|
|
if first != second:
|
2015-03-30 17:59:07 +00:00
|
|
|
print("Passwords not the same. Try again.")
|
2015-03-29 16:54:37 +00:00
|
|
|
continue
|
|
|
|
break
|
|
|
|
return first
|
2014-06-06 21:07:30 +00:00
|
|
|
|
2014-06-21 23:49:09 +00:00
|
|
|
def setup_key_auth(mgmt_uri):
|
|
|
|
key = open('/var/lib/mailinabox/api.key').read().strip()
|
|
|
|
|
|
|
|
auth_handler = urllib.request.HTTPBasicAuthHandler()
|
|
|
|
auth_handler.add_password(
|
|
|
|
realm='Mail-in-a-Box Management Server',
|
|
|
|
uri=mgmt_uri,
|
|
|
|
user=key,
|
|
|
|
passwd='')
|
|
|
|
opener = urllib.request.build_opener(auth_handler)
|
|
|
|
urllib.request.install_opener(opener)
|
|
|
|
|
2013-08-31 14:50:59 +00:00
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print("Usage: ")
|
2019-02-01 13:36:27 +00:00
|
|
|
print(" tools/mail.py system default-quota [new default]")
|
2019-01-29 11:51:18 +00:00
|
|
|
print(" tools/mail.py user (lists users with quotas)")
|
2014-03-16 20:29:47 +00:00
|
|
|
print(" tools/mail.py user add user@domain.com [password]")
|
|
|
|
print(" tools/mail.py user password user@domain.com [password]")
|
2019-02-01 13:36:27 +00:00
|
|
|
print(" tools/mail.py user quota user@domain [new-quota]")
|
2014-03-16 20:29:47 +00:00
|
|
|
print(" tools/mail.py user remove user@domain.com")
|
2014-08-08 12:31:22 +00:00
|
|
|
print(" tools/mail.py user make-admin user@domain.com")
|
|
|
|
print(" tools/mail.py user remove-admin user@domain.com")
|
2014-08-16 12:59:29 +00:00
|
|
|
print(" tools/mail.py user admins (lists admins)")
|
2014-03-16 20:29:47 +00:00
|
|
|
print(" tools/mail.py alias (lists aliases)")
|
|
|
|
print(" tools/mail.py alias add incoming.name@domain.com sent.to@other.domain.com")
|
2014-08-26 18:45:03 +00:00
|
|
|
print(" tools/mail.py alias add incoming.name@domain.com 'sent.to@other.domain.com, multiple.people@other.domain.com'")
|
2014-03-16 20:29:47 +00:00
|
|
|
print(" tools/mail.py alias remove incoming.name@domain.com")
|
2013-08-31 14:50:59 +00:00
|
|
|
print()
|
|
|
|
print("Removing a mail user does not delete their mail folders on disk. It only prevents IMAP/SMTP login.")
|
|
|
|
print()
|
|
|
|
|
|
|
|
elif sys.argv[1] == "user" and len(sys.argv) == 2:
|
2014-08-08 12:31:22 +00:00
|
|
|
# Dump a list of users, one per line. Mark admins with an asterisk.
|
|
|
|
users = mgmt("/mail/users?format=json", is_json=True)
|
2014-10-07 19:28:07 +00:00
|
|
|
for domain in users:
|
|
|
|
for user in domain["users"]:
|
|
|
|
if user['status'] == 'inactive': continue
|
|
|
|
print(user['email'], end='')
|
|
|
|
if "admin" in user['privileges']:
|
|
|
|
print("*", end='')
|
2019-01-29 11:51:18 +00:00
|
|
|
if user['quota'] == '0':
|
|
|
|
print(" unlimited", end='')
|
|
|
|
else:
|
|
|
|
print(" " + user['quota'], end='')
|
2014-10-07 19:28:07 +00:00
|
|
|
print()
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
elif sys.argv[1] == "user" and sys.argv[2] in ("add", "password"):
|
|
|
|
if len(sys.argv) < 5:
|
|
|
|
if len(sys.argv) < 4:
|
|
|
|
email = input("email: ")
|
|
|
|
else:
|
|
|
|
email = sys.argv[3]
|
2014-06-06 21:07:30 +00:00
|
|
|
pw = read_password()
|
2013-08-31 14:50:59 +00:00
|
|
|
else:
|
|
|
|
email, pw = sys.argv[3:5]
|
|
|
|
|
|
|
|
if sys.argv[2] == "add":
|
2014-06-03 13:24:48 +00:00
|
|
|
print(mgmt("/mail/users/add", { "email": email, "password": pw }))
|
2013-08-31 14:50:59 +00:00
|
|
|
elif sys.argv[2] == "password":
|
2014-06-03 13:24:48 +00:00
|
|
|
print(mgmt("/mail/users/password", { "email": email, "password": pw }))
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
elif sys.argv[1] == "user" and sys.argv[2] == "remove" and len(sys.argv) == 4:
|
2014-06-03 13:24:48 +00:00
|
|
|
print(mgmt("/mail/users/remove", { "email": sys.argv[3] }))
|
2013-08-31 14:50:59 +00:00
|
|
|
|
2014-08-08 12:31:22 +00:00
|
|
|
elif sys.argv[1] == "user" and sys.argv[2] in ("make-admin", "remove-admin") and len(sys.argv) == 4:
|
|
|
|
if sys.argv[2] == "make-admin":
|
|
|
|
action = "add"
|
|
|
|
else:
|
|
|
|
action = "remove"
|
|
|
|
print(mgmt("/mail/users/privileges/" + action, { "email": sys.argv[3], "privilege": "admin" }))
|
|
|
|
|
2014-08-16 12:59:29 +00:00
|
|
|
elif sys.argv[1] == "user" and sys.argv[2] == "admins":
|
|
|
|
# Dump a list of admin users.
|
|
|
|
users = mgmt("/mail/users?format=json", is_json=True)
|
2014-10-07 19:28:07 +00:00
|
|
|
for domain in users:
|
|
|
|
for user in domain["users"]:
|
|
|
|
if "admin" in user['privileges']:
|
|
|
|
print(user['email'])
|
2014-08-16 12:59:29 +00:00
|
|
|
|
2019-02-01 13:36:27 +00:00
|
|
|
elif sys.argv[1] == "user" and sys.argv[2] == "quota" and len(sys.argv) == 4:
|
|
|
|
# Set a user's quota
|
|
|
|
print(mgmt("/mail/users/quota?text=1&email=%s" % sys.argv[3]))
|
|
|
|
|
2019-01-29 11:51:18 +00:00
|
|
|
elif sys.argv[1] == "user" and sys.argv[2] == "quota" and len(sys.argv) == 5:
|
|
|
|
# Set a user's quota
|
|
|
|
users = mgmt("/mail/users/quota", { "email": sys.argv[3], "quota": sys.argv[4] })
|
|
|
|
|
2013-08-31 14:50:59 +00:00
|
|
|
elif sys.argv[1] == "alias" and len(sys.argv) == 2:
|
2014-06-03 13:24:48 +00:00
|
|
|
print(mgmt("/mail/aliases"))
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
elif sys.argv[1] == "alias" and sys.argv[2] == "add" and len(sys.argv) == 5:
|
2015-08-14 23:05:08 +00:00
|
|
|
print(mgmt("/mail/aliases/add", { "address": sys.argv[3], "forwards_to": sys.argv[4] }))
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
elif sys.argv[1] == "alias" and sys.argv[2] == "remove" and len(sys.argv) == 4:
|
2015-08-14 23:05:08 +00:00
|
|
|
print(mgmt("/mail/aliases/remove", { "address": sys.argv[3] }))
|
2013-08-31 14:50:59 +00:00
|
|
|
|
2019-02-01 13:36:27 +00:00
|
|
|
elif sys.argv[1] == "system" and sys.argv[2] == "default-quota" and len(sys.argv) == 3:
|
|
|
|
print(mgmt("/system/default-quota?text=1"))
|
|
|
|
|
|
|
|
elif sys.argv[1] == "system" and sys.argv[2] == "default-quota" and len(sys.argv) == 4:
|
|
|
|
print(mgmt("/system/default-quota", { "default_quota": sys.argv[3]}))
|
|
|
|
|
2013-08-31 14:50:59 +00:00
|
|
|
else:
|
|
|
|
print("Invalid command-line arguments.")
|
2014-08-10 14:10:25 +00:00
|
|
|
sys.exit(1)
|
2013-08-31 14:50:59 +00:00
|
|
|
|