mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-06-17 11:10:23 +00:00
- Parse arguments in a readable way. - Convert decorator into simple call. - Make a simple call instead of looking for subcommand. - Don't setup logging in global scope. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
77 lines
2.1 KiB
Python
Executable File
77 lines
2.1 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(os.path.basename(__file__))
|
|
|
|
|
|
def main():
|
|
"""Parse arguments."""
|
|
# Set up logging
|
|
plinth.log.pipe_to_syslog(to_stderr='tty')
|
|
|
|
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)
|
|
_log_additional_info()
|
|
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)
|
|
|
|
action(*arguments)
|
|
|
|
|
|
def _log_additional_info():
|
|
"""Log additional debugging information."""
|
|
import grp
|
|
import pwd
|
|
resu = ','.join(pwd.getpwuid(uid).pw_name for uid in os.getresuid())
|
|
resg = ','.join(grp.getgrgid(gid).gr_name for gid in os.getresgid())
|
|
pyver = sys.version.replace('\n', ' ')
|
|
logger.error('--- Additional Information ---')
|
|
logger.error('resuid=%s, resgid=%s', resu, resg)
|
|
logger.error('argv=%r, cwd=%r', sys.argv, os.getcwd())
|
|
logger.error('pyver=%s (%s)', pyver, os.uname().machine)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|