2014-06-03 13:24:48 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2014-08-08 12:31:22 +00:00
|
|
|
import os, os.path, re, json
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
from functools import wraps
|
|
|
|
|
2014-08-08 12:31:22 +00:00
|
|
|
from flask import Flask, request, render_template, abort, Response
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2014-06-21 23:42:48 +00:00
|
|
|
import auth, utils
|
2014-08-17 22:43:57 +00:00
|
|
|
from mailconfig import get_mail_users, add_mail_user, set_mail_password, remove_mail_user, get_archived_mail_users
|
2014-08-08 12:31:22 +00:00
|
|
|
from mailconfig import get_mail_user_privileges, add_remove_mail_user_privilege
|
|
|
|
from mailconfig import get_mail_aliases, get_mail_domains, add_mail_alias, remove_mail_alias
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2014-06-03 20:21:17 +00:00
|
|
|
env = utils.load_environment()
|
|
|
|
|
2014-06-22 12:55:19 +00:00
|
|
|
auth_service = auth.KeyAuthService()
|
2014-06-21 23:42:48 +00:00
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
# We may deploy via a symbolic link, which confuses flask's template finding.
|
|
|
|
me = __file__
|
|
|
|
try:
|
|
|
|
me = os.readlink(__file__)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
app = Flask(__name__, template_folder=os.path.abspath(os.path.join(os.path.dirname(me), "templates")))
|
|
|
|
|
|
|
|
# Decorator to protect views that require authentication.
|
|
|
|
def authorized_personnel_only(viewfunc):
|
|
|
|
@wraps(viewfunc)
|
|
|
|
def newview(*args, **kwargs):
|
|
|
|
# Check if the user is authorized.
|
|
|
|
authorized_status = auth_service.is_authenticated(request, env)
|
|
|
|
if authorized_status == "OK":
|
|
|
|
# Authorized. Call view func.
|
|
|
|
return viewfunc(*args, **kwargs)
|
|
|
|
|
|
|
|
# Not authorized. Return a 401 (send auth) and a prompt to authorize by default.
|
|
|
|
status = 401
|
|
|
|
headers = { 'WWW-Authenticate': 'Basic realm="{0}"'.format(auth_service.auth_realm) }
|
|
|
|
|
|
|
|
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
|
|
|
|
# Don't issue a 401 to an AJAX request because the user will
|
|
|
|
# be prompted for credentials, which is not helpful.
|
|
|
|
status = 403
|
|
|
|
headers = None
|
|
|
|
|
|
|
|
if request.headers.get('Accept') in (None, "", "*/*"):
|
|
|
|
# Return plain text output.
|
|
|
|
return Response(authorized_status+"\n", status=status, mimetype='text/plain', headers=headers)
|
|
|
|
else:
|
|
|
|
# Return JSON output.
|
|
|
|
return Response(json.dumps({
|
|
|
|
"status": "error",
|
|
|
|
"reason": authorized_status
|
|
|
|
}+"\n"), status=status, mimetype='application/json', headers=headers)
|
|
|
|
|
|
|
|
return newview
|
2014-06-21 23:42:48 +00:00
|
|
|
|
|
|
|
@app.errorhandler(401)
|
|
|
|
def unauthorized(error):
|
|
|
|
return auth_service.make_unauthorized_response()
|
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
def json_response(data):
|
|
|
|
return Response(json.dumps(data), status=200, mimetype='application/json')
|
|
|
|
|
|
|
|
###################################
|
|
|
|
|
|
|
|
# Control Panel (unauthenticated views)
|
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
@app.route('/')
|
|
|
|
def index():
|
2014-08-17 22:43:57 +00:00
|
|
|
# Render the control panel. This route does not require user authentication
|
|
|
|
# so it must be safe!
|
|
|
|
return render_template('index.html',
|
|
|
|
hostname=env['PRIMARY_HOSTNAME'],
|
|
|
|
)
|
|
|
|
|
|
|
|
@app.route('/me')
|
|
|
|
def me():
|
|
|
|
# Is the caller authorized?
|
|
|
|
authorized_status = auth_service.is_authenticated(request, env)
|
|
|
|
if authorized_status != "OK":
|
|
|
|
return json_response({
|
|
|
|
"status": "not-authorized",
|
|
|
|
"reason": authorized_status,
|
|
|
|
})
|
|
|
|
return json_response({
|
|
|
|
"status": "authorized",
|
|
|
|
"api_key": auth_service.key,
|
|
|
|
})
|
2014-06-03 13:24:48 +00:00
|
|
|
|
|
|
|
# MAIL
|
|
|
|
|
|
|
|
@app.route('/mail/users')
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-06-03 13:24:48 +00:00
|
|
|
def mail_users():
|
2014-08-08 12:31:22 +00:00
|
|
|
if request.args.get("format", "") == "json":
|
2014-08-17 22:43:57 +00:00
|
|
|
return json_response(get_mail_users(env, as_json=True) + get_archived_mail_users(env))
|
2014-08-08 12:31:22 +00:00
|
|
|
else:
|
|
|
|
return "".join(x+"\n" for x in get_mail_users(env))
|
2014-06-03 13:24:48 +00:00
|
|
|
|
|
|
|
@app.route('/mail/users/add', methods=['POST'])
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-06-03 13:24:48 +00:00
|
|
|
def mail_users_add():
|
2014-08-17 22:43:57 +00:00
|
|
|
return add_mail_user(request.form.get('email', ''), request.form.get('password', ''), request.form.get('privileges', ''), env)
|
2014-06-03 13:24:48 +00:00
|
|
|
|
|
|
|
@app.route('/mail/users/password', methods=['POST'])
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-06-03 13:24:48 +00:00
|
|
|
def mail_users_password():
|
|
|
|
return set_mail_password(request.form.get('email', ''), request.form.get('password', ''), env)
|
|
|
|
|
|
|
|
@app.route('/mail/users/remove', methods=['POST'])
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-06-03 13:24:48 +00:00
|
|
|
def mail_users_remove():
|
|
|
|
return remove_mail_user(request.form.get('email', ''), env)
|
|
|
|
|
2014-08-08 12:31:22 +00:00
|
|
|
|
|
|
|
@app.route('/mail/users/privileges')
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-08-08 12:31:22 +00:00
|
|
|
def mail_user_privs():
|
|
|
|
privs = get_mail_user_privileges(request.args.get('email', ''), env)
|
|
|
|
if isinstance(privs, tuple): return privs # error
|
|
|
|
return "\n".join(privs)
|
|
|
|
|
|
|
|
@app.route('/mail/users/privileges/add', methods=['POST'])
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-08-08 12:31:22 +00:00
|
|
|
def mail_user_privs_add():
|
|
|
|
return add_remove_mail_user_privilege(request.form.get('email', ''), request.form.get('privilege', ''), "add", env)
|
|
|
|
|
|
|
|
@app.route('/mail/users/privileges/remove', methods=['POST'])
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-08-08 12:31:22 +00:00
|
|
|
def mail_user_privs_remove():
|
|
|
|
return add_remove_mail_user_privilege(request.form.get('email', ''), request.form.get('privilege', ''), "remove", env)
|
|
|
|
|
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
@app.route('/mail/aliases')
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-06-03 13:24:48 +00:00
|
|
|
def mail_aliases():
|
2014-08-17 22:43:57 +00:00
|
|
|
if request.args.get("format", "") == "json":
|
|
|
|
return json_response(get_mail_aliases(env, as_json=True))
|
|
|
|
else:
|
|
|
|
return "".join(x+"\t"+y+"\n" for x, y in get_mail_aliases(env))
|
2014-06-03 13:24:48 +00:00
|
|
|
|
|
|
|
@app.route('/mail/aliases/add', methods=['POST'])
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-06-03 13:24:48 +00:00
|
|
|
def mail_aliases_add():
|
2014-08-17 22:43:57 +00:00
|
|
|
return add_mail_alias(
|
|
|
|
request.form.get('source', ''),
|
|
|
|
request.form.get('destination', ''),
|
|
|
|
env,
|
|
|
|
update_if_exists=(request.form.get('update_if_exists', '') == '1')
|
|
|
|
)
|
2014-06-03 13:24:48 +00:00
|
|
|
|
|
|
|
@app.route('/mail/aliases/remove', methods=['POST'])
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-06-03 13:24:48 +00:00
|
|
|
def mail_aliases_remove():
|
|
|
|
return remove_mail_alias(request.form.get('source', ''), env)
|
|
|
|
|
|
|
|
@app.route('/mail/domains')
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-06-03 13:24:48 +00:00
|
|
|
def mail_domains():
|
|
|
|
return "".join(x+"\n" for x in get_mail_domains(env))
|
|
|
|
|
|
|
|
# DNS
|
|
|
|
|
|
|
|
@app.route('/dns/update', methods=['POST'])
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-06-03 13:24:48 +00:00
|
|
|
def dns_update():
|
|
|
|
from dns_update import do_dns_update
|
2014-06-17 22:21:12 +00:00
|
|
|
try:
|
2014-08-01 12:05:34 +00:00
|
|
|
return do_dns_update(env, force=request.form.get('force', '') == '1')
|
2014-06-17 22:21:12 +00:00
|
|
|
except Exception as e:
|
|
|
|
return (str(e), 500)
|
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
@app.route('/dns/dump')
|
|
|
|
@authorized_personnel_only
|
|
|
|
def dns_get_dump():
|
|
|
|
from dns_update import build_recommended_dns
|
|
|
|
return json_response(build_recommended_dns(env))
|
2014-06-03 13:24:48 +00:00
|
|
|
|
2014-06-20 01:16:38 +00:00
|
|
|
# WEB
|
|
|
|
|
|
|
|
@app.route('/web/update', methods=['POST'])
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-06-20 01:16:38 +00:00
|
|
|
def web_update():
|
|
|
|
from web_update import do_web_update
|
|
|
|
return do_web_update(env)
|
|
|
|
|
2014-06-05 20:57:25 +00:00
|
|
|
# System
|
|
|
|
|
2014-08-17 22:43:57 +00:00
|
|
|
@app.route('/system/status', methods=["POST"])
|
|
|
|
@authorized_personnel_only
|
|
|
|
def system_status():
|
|
|
|
from whats_next import run_checks
|
|
|
|
class WebOutput:
|
|
|
|
def __init__(self):
|
|
|
|
self.items = []
|
|
|
|
def add_heading(self, heading):
|
|
|
|
self.items.append({ "type": "heading", "text": heading, "extra": [] })
|
|
|
|
def print_ok(self, message):
|
|
|
|
self.items.append({ "type": "ok", "text": message, "extra": [] })
|
|
|
|
def print_error(self, message):
|
|
|
|
self.items.append({ "type": "error", "text": message, "extra": [] })
|
|
|
|
def print_line(self, message, monospace=False):
|
|
|
|
self.items[-1]["extra"].append({ "text": message, "monospace": monospace })
|
|
|
|
output = WebOutput()
|
|
|
|
run_checks(env, output)
|
|
|
|
return json_response(output.items)
|
|
|
|
|
2014-06-05 20:57:25 +00:00
|
|
|
@app.route('/system/updates')
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-06-05 20:57:25 +00:00
|
|
|
def show_updates():
|
2014-06-09 12:09:45 +00:00
|
|
|
utils.shell("check_call", ["/usr/bin/apt-get", "-qq", "update"])
|
|
|
|
simulated_install = utils.shell("check_output", ["/usr/bin/apt-get", "-qq", "-s", "upgrade"])
|
|
|
|
pkgs = []
|
|
|
|
for line in simulated_install.split('\n'):
|
|
|
|
if re.match(r'^Conf .*', line): continue # remove these lines, not informative
|
|
|
|
line = re.sub(r'^Inst (.*) \[(.*)\] \((\S*).*', r'Updated Package Available: \1 (\3)', line) # make these lines prettier
|
|
|
|
pkgs.append(line)
|
|
|
|
return "\n".join(pkgs)
|
2014-06-05 20:57:25 +00:00
|
|
|
|
|
|
|
@app.route('/system/update-packages', methods=["POST"])
|
2014-08-17 22:43:57 +00:00
|
|
|
@authorized_personnel_only
|
2014-06-05 20:57:25 +00:00
|
|
|
def do_updates():
|
2014-06-09 12:09:45 +00:00
|
|
|
utils.shell("check_call", ["/usr/bin/apt-get", "-qq", "update"])
|
|
|
|
return utils.shell("check_output", ["/usr/bin/apt-get", "-y", "upgrade"], env={
|
|
|
|
"DEBIAN_FRONTEND": "noninteractive"
|
|
|
|
})
|
2014-06-05 20:57:25 +00:00
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
# APP
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if "DEBUG" in os.environ: app.debug = True
|
2014-08-17 22:43:57 +00:00
|
|
|
if "APIKEY" in os.environ: auth_service.key = os.environ["APIKEY"]
|
2014-06-21 23:42:48 +00:00
|
|
|
|
2014-06-21 23:25:35 +00:00
|
|
|
if not app.debug:
|
|
|
|
app.logger.addHandler(utils.create_syslog_handler())
|
|
|
|
|
2014-06-21 23:42:48 +00:00
|
|
|
# For testing on the command line, you can use `curl` like so:
|
|
|
|
# curl --user $(</var/lib/mailinabox/api.key): http://localhost:10222/mail/users
|
|
|
|
auth_service.write_key()
|
|
|
|
|
|
|
|
# For testing in the browser, you can copy the API key that's output to the
|
|
|
|
# debug console and enter that as the username
|
|
|
|
app.logger.info('API key: ' + auth_service.key)
|
|
|
|
|
2014-06-03 13:24:48 +00:00
|
|
|
app.run(port=10222)
|
2014-06-21 23:42:48 +00:00
|
|
|
|