mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
67 lines
1.7 KiB
Python
Executable File
67 lines
1.7 KiB
Python
Executable File
#!/usr/bin/python3
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""
|
|
Configuration helper for email server.
|
|
"""
|
|
|
|
import argparse
|
|
import logging
|
|
import os
|
|
import sys
|
|
|
|
import plinth.log
|
|
from plinth.modules.email_server import audit
|
|
|
|
EXIT_SYNTAX = 10
|
|
EXIT_PERM = 20
|
|
|
|
logger = logging.getLogger(__file__)
|
|
|
|
|
|
def main():
|
|
"""Parse arguments."""
|
|
plinth.log.action_init()
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('module', help='Module to trigger action in')
|
|
parser.add_argument('action', help='Action to trigger in module')
|
|
parser.add_argument('arguments', help='String arguments for action',
|
|
nargs='*')
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
_call(args.module, args.action, args.arguments)
|
|
except Exception as exception:
|
|
logger.exception(exception)
|
|
sys.exit(1)
|
|
|
|
|
|
def _call(module_name, action_name, arguments):
|
|
"""Import the module and run action as superuser."""
|
|
if os.getuid() != 0:
|
|
logger.critical('This action is reserved for root')
|
|
sys.exit(EXIT_PERM)
|
|
|
|
# We only run actions defined in the audit module
|
|
if module_name not in audit.__all__:
|
|
logger.critical('Bad module name: %r', module_name)
|
|
sys.exit(EXIT_SYNTAX)
|
|
|
|
module = getattr(audit, module_name)
|
|
try:
|
|
action = getattr(module, 'action_' + action_name)
|
|
except AttributeError:
|
|
logger.critical('Bad action: %s/%r', module_name, action_name)
|
|
sys.exit(EXIT_SYNTAX)
|
|
|
|
for argument in arguments:
|
|
if not isinstance(argument, str):
|
|
logger.critical('Bad argument: %s', argument)
|
|
sys.exit(EXIT_SYNTAX)
|
|
|
|
action(*arguments)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|