email: Parse command arguments with a mutually exclusive group

This commit is contained in:
fliu 2021-07-02 04:07:02 +00:00 committed by Sunil Mohan Adapa
parent 7166e63b02
commit df14e74972
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
3 changed files with 25 additions and 8 deletions

View File

@ -23,16 +23,22 @@ def reserved_for_root(fun):
def main():
parser = argparse.ArgumentParser()
parser.add_argument('ipc', nargs=3)
arguments = parser.parse_args()
subcommand_ipc(arguments)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-i', nargs=2, dest='ipc')
group.add_argument('-t', nargs=1, dest='touch_file')
adict = vars(parser.parse_args())
generator = (kv for kv in adict.items() if kv[1] is not None)
subcommand, arguments = next(generator)
function = globals()['subcommand_' + subcommand]
function(*arguments)
@reserved_for_root
def subcommand_ipc(arguments):
def subcommand_ipc(module_name, action_name):
import plinth.modules.email_server.audit as audit
_, module_name, action_name = arguments.ipc
# We only run actions defined in the audit module
if module_name not in audit.__all__:
logger.critical('Bad module name: %r', module_name)
@ -47,5 +53,16 @@ def subcommand_ipc(arguments):
function()
def subcommand_touch_file(path):
import pathlib
if os.getuid == 0:
logger.critical('Do not run as root')
sys.exit(EXIT_PERM)
# mode is influenced by umask
pathlib.Path(path).touch(mode=0o660, exist_ok=True)
if __name__ == '__main__':
main()

View File

@ -64,11 +64,11 @@ def repair():
POST /audit/ldap/repair
"""
logger.debug('Updating postconf: %r', default_config)
actions.superuser_run('email_server', ['ipc', 'ldap', 'set_sasl'])
actions.superuser_run('email_server', ['-i', 'ldap', 'set_sasl'])
logger.debug('Setting up postfix services:\n %r\n %r',
default_submission_options, default_smtps_options)
actions.superuser_run('email_server', ['ipc', 'ldap', 'set_submission'])
actions.superuser_run('email_server', ['-i', 'ldap', 'set_submission'])
def action_set_sasl():

View File

@ -19,7 +19,7 @@ logger = logging.getLogger(__name__)
def repair():
logger.debug('Updating postconf: %r', milter_config)
actions.superuser_run('email_server', ['ipc', 'spam', 'set_filter'])
actions.superuser_run('email_server', ['-i', 'spam', 'set_filter'])
def action_set_filter():