2013-08-31 14:50:59 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2015-02-22 21:20:02 +00:00
|
|
|
import sys
|
|
|
|
import getpass
|
|
|
|
import urllib.request
|
|
|
|
import urllib.error
|
|
|
|
import json
|
|
|
|
|
2013-08-31 14:50:59 +00:00
|
|
|
|
2014-08-08 12:31:22 +00:00
|
|
|
def mgmt(cmd, data=None, is_json=False):
|
2015-02-22 21:20:02 +00:00
|
|
|
# The base URL for the management daemon. (Listens on IPv4 only.)
|
|
|
|
mgmt_uri = 'http://127.0.0.1:10222'
|
|
|
|
|
|
|
|
setup_key_auth(mgmt_uri)
|
|
|
|
|
|
|
|
req = urllib.request.Request(mgmt_uri + cmd, urllib.parse.urlencode(data).encode("utf8") if data else None)
|
|
|
|
try:
|
|
|
|
response = urllib.request.urlopen(req)
|
|
|
|
except urllib.error.HTTPError as e:
|
|
|
|
if e.code == 401:
|
|
|
|
try:
|
|
|
|
print(e.read().decode("utf8"))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
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)
|
|
|
|
sys.exit(1)
|
|
|
|
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-02-22 21:20:02 +00:00
|
|
|
first = getpass.getpass('password: ')
|
|
|
|
second = getpass.getpass(' (again): ')
|
|
|
|
while first != second:
|
|
|
|
print('Passwords not the same. Try again.')
|
|
|
|
first = getpass.getpass('password: ')
|
|
|
|
second = getpass.getpass(' (again): ')
|
|
|
|
return first
|
|
|
|
|
2014-06-06 21:07:30 +00:00
|
|
|
|
2014-06-21 23:49:09 +00:00
|
|
|
def setup_key_auth(mgmt_uri):
|
2015-02-22 21:20:02 +00:00
|
|
|
key = open('/var/lib/mailinabox/api.key').read().strip()
|
2014-06-21 23:49:09 +00:00
|
|
|
|
2015-02-22 21:20:02 +00:00
|
|
|
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)
|
2014-06-21 23:49:09 +00:00
|
|
|
|
2013-08-31 14:50:59 +00:00
|
|
|
if len(sys.argv) < 2:
|
2015-02-22 21:20:02 +00:00
|
|
|
print("Usage: ")
|
|
|
|
print(" tools/mail.py user (lists users)")
|
|
|
|
print(" tools/mail.py user add user@domain.com [password]")
|
|
|
|
print(" tools/mail.py user password user@domain.com [password]")
|
|
|
|
print(" tools/mail.py user remove user@domain.com")
|
|
|
|
print(" tools/mail.py user make-admin user@domain.com")
|
|
|
|
print(" tools/mail.py user remove-admin user@domain.com")
|
|
|
|
print(" tools/mail.py user admins (lists admins)")
|
|
|
|
print(" tools/mail.py alias (lists aliases)")
|
|
|
|
print(" tools/mail.py alias add incoming.name@domain.com sent.to@other.domain.com")
|
|
|
|
print(" tools/mail.py alias add incoming.name@domain.com 'sent.to@other.domain.com, multiple.people@other.domain.com'")
|
|
|
|
print(" tools/mail.py alias remove incoming.name@domain.com")
|
|
|
|
print()
|
|
|
|
print("Removing a mail user does not delete their mail folders on disk. It only prevents IMAP/SMTP login.")
|
|
|
|
print()
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
elif sys.argv[1] == "user" and len(sys.argv) == 2:
|
2015-02-22 21:20:02 +00:00
|
|
|
# Dump a list of users, one per line. Mark admins with an asterisk.
|
|
|
|
users = mgmt("/mail/users?format=json", is_json=True)
|
|
|
|
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='')
|
|
|
|
print()
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
elif sys.argv[1] == "user" and sys.argv[2] in ("add", "password"):
|
2015-02-22 21:20:02 +00:00
|
|
|
if len(sys.argv) < 5:
|
|
|
|
if len(sys.argv) < 4:
|
|
|
|
email = input("email: ")
|
|
|
|
else:
|
|
|
|
email = sys.argv[3]
|
|
|
|
pw = read_password()
|
|
|
|
else:
|
|
|
|
email, pw = sys.argv[3:5]
|
|
|
|
|
|
|
|
if sys.argv[2] == "add":
|
|
|
|
print(mgmt("/mail/users/add", {"email": email, "password": pw}))
|
|
|
|
elif sys.argv[2] == "password":
|
|
|
|
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:
|
2015-02-22 21:20:02 +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:
|
2015-02-22 21:20:02 +00:00
|
|
|
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-08 12:31:22 +00:00
|
|
|
|
2014-08-16 12:59:29 +00:00
|
|
|
elif sys.argv[1] == "user" and sys.argv[2] == "admins":
|
2015-02-22 21:20:02 +00:00
|
|
|
# Dump a list of admin users.
|
|
|
|
users = mgmt("/mail/users?format=json", is_json=True)
|
|
|
|
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
|
|
|
|
2013-08-31 14:50:59 +00:00
|
|
|
elif sys.argv[1] == "alias" and len(sys.argv) == 2:
|
2015-02-22 21:20:02 +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-02-22 21:20:02 +00:00
|
|
|
print(mgmt("/mail/aliases/add", {"source": sys.argv[3], "destination": 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-02-22 21:20:02 +00:00
|
|
|
print(mgmt("/mail/aliases/remove", {"source": sys.argv[3]}))
|
2013-08-31 14:50:59 +00:00
|
|
|
|
|
|
|
else:
|
2015-02-22 21:20:02 +00:00
|
|
|
print("Invalid command-line arguments.")
|
|
|
|
sys.exit(1)
|