| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 4 |
# Copyright (C) 2020-2025 Red Hat, Inc. |
| 5 |
# All rights reserved. |
| 6 |
# |
| 7 |
# License: GPL (version 3 or any later version). |
| 8 |
# See LICENSE for details. |
| 9 |
# --- END COPYRIGHT BLOCK --- |
| 10 |
# |
| 11 |
# PYTHON_ARGCOMPLETE_OK |
| 12 |
|
| 13 |
import argparse, argcomplete |
| 14 |
import ldap |
| 15 |
import sys |
| 16 |
import signal |
| 17 |
import json |
| 18 |
from lib389._constants import DSRC_HOME |
| 19 |
from lib389.cli_conf import config as cli_config |
| 20 |
from lib389.cli_conf import backend as cli_backend |
| 21 |
from lib389.cli_conf import directory_manager as cli_directory_manager |
| 22 |
from lib389.cli_conf import logging as cli_logs |
| 23 |
from lib389.cli_conf import plugin as cli_plugin |
| 24 |
from lib389.cli_conf import schema as cli_schema |
| 25 |
from lib389.cli_conf import monitor as cli_monitor |
| 26 |
from lib389.cli_conf import saslmappings as cli_sasl |
| 27 |
from lib389.cli_conf import pwpolicy as cli_pwpolicy |
| 28 |
from lib389.cli_conf import backup as cli_backup |
| 29 |
from lib389.cli_conf import replication as cli_replication |
| 30 |
from lib389.cli_conf import chaining as cli_chaining |
| 31 |
from lib389.cli_conf import conflicts as cli_repl_conflicts |
| 32 |
from lib389.cli_conf import security as cli_security |
| 33 |
from lib389.cli_base import disconnect_instance, connect_instance |
| 34 |
from lib389.cli_base.dsrc import dsrc_to_ldap, dsrc_arg_concat |
| 35 |
from lib389.cli_base import setup_script_logger |
| 36 |
from lib389.cli_base import format_error_to_dict |
| 37 |
from lib389.cli_base import parent_argparser |
| 38 |
from lib389.cli_base import format_pretty_error |
| 39 |
from lib389.utils import instance_choices |
| 40 |
|
| 41 |
parser = argparse.ArgumentParser(allow_abbrev=True, parents=[parent_argparser]) |
| 42 |
parser.add_argument('instance', |
| 43 |
help="The name of the instance or its LDAP URL, such as ldap://server.example.com:389", |
| 44 |
).completer = instance_choices |
| 45 |
parser.add_argument('-D', '--binddn', |
| 46 |
help="The account to bind as for executing operations", |
| 47 |
default=None |
| 48 |
) |
| 49 |
parser.add_argument('-w', '--bindpw', |
| 50 |
help="Password for the bind DN", |
| 51 |
default=None |
| 52 |
) |
| 53 |
parser.add_argument('-W', '--prompt', |
| 54 |
action='store_true', default=False, |
| 55 |
help="Prompt for password of the bind DN" |
| 56 |
) |
| 57 |
parser.add_argument('-y', '--pwdfile', |
| 58 |
help="Specifies a file containing the password of the bind DN", |
| 59 |
default=None |
| 60 |
) |
| 61 |
parser.add_argument('-b', '--basedn', |
| 62 |
help="Base DN (root naming context) of the instance to manage", |
| 63 |
default=None |
| 64 |
) |
| 65 |
parser.add_argument('-Z', '--starttls', |
| 66 |
help="Connect with StartTLS", |
| 67 |
default=False, action='store_true' |
| 68 |
) |
| 69 |
|
| 70 |
subparsers = parser.add_subparsers(help="resources to act upon") |
| 71 |
|
| 72 |
cli_backend.create_parser(subparsers) |
| 73 |
cli_backup.create_parser(subparsers) |
| 74 |
cli_chaining.create_parser(subparsers) |
| 75 |
cli_config.create_parser(subparsers) |
| 76 |
cli_directory_manager.create_parsers(subparsers) |
| 77 |
cli_logs.create_parser(subparsers) |
| 78 |
cli_monitor.create_parser(subparsers) |
| 79 |
cli_plugin.create_parser(subparsers) |
| 80 |
cli_pwpolicy.create_parser(subparsers) |
| 81 |
cli_replication.create_parser(subparsers) |
| 82 |
cli_repl_conflicts.create_parser(subparsers) |
| 83 |
cli_sasl.create_parser(subparsers) |
| 84 |
cli_security.create_parser(subparsers) |
| 85 |
cli_schema.create_parser(subparsers) |
| 86 |
|
| 87 |
argcomplete.autocomplete(parser) |
| 88 |
|
| 89 |
|
| 90 |
# handle a control-c gracefully |
| 91 |
def signal_handler(signal, frame): |
| 92 |
print('\n\nExiting...') |
| 93 |
sys.exit(0) |
| 94 |
|
| 95 |
|
| 96 |
if __name__ == '__main__': |
| 97 |
|
| 98 |
defbase = ldap.get_option(ldap.OPT_DEFBASE) |
| 99 |
args = parser.parse_args() |
| 100 |
log = setup_script_logger('dsconf', args.verbose) |
| 101 |
|
| 102 |
log.debug("The 389 Directory Server Configuration Tool") |
| 103 |
# Leave this comment here: UofA let me take this code with me provided |
| 104 |
# I gave attribution. -- wibrown |
| 105 |
log.debug("Inspired by works of: ITS, The University of Adelaide") |
| 106 |
|
| 107 |
# Now that we have our args, see how they relate with our instance. |
| 108 |
dsrc_inst = dsrc_to_ldap(DSRC_HOME, args.instance, log.getChild('dsrc')) |
| 109 |
|
| 110 |
# Now combine this with our arguments |
| 111 |
dsrc_inst = dsrc_arg_concat(args, dsrc_inst) |
| 112 |
|
| 113 |
log.debug("Called with: %s" % args) |
| 114 |
log.debug("Instance details: %s" % dsrc_inst) |
| 115 |
|
| 116 |
# Assert we have a resources to work on. |
| 117 |
if not hasattr(args, 'func'): |
| 118 |
errmsg = "No action provided, here is some --help." |
| 119 |
if args.json: |
| 120 |
sys.stderr.write('{"desc": "%s"}\n' % errmsg) |
| 121 |
else: |
| 122 |
log.error(errmsg) |
| 123 |
parser.print_help() |
| 124 |
sys.exit(1) |
| 125 |
|
| 126 |
if not args.verbose: |
| 127 |
signal.signal(signal.SIGINT, signal_handler) |
| 128 |
|
| 129 |
# Connect |
| 130 |
# We don't need a basedn, because the config objects derive it properly |
| 131 |
inst = None |
| 132 |
result = False |
| 133 |
try: |
| 134 |
inst = connect_instance(dsrc_inst=dsrc_inst, verbose=args.verbose, args=args) |
| 135 |
result = args.func(inst, None, log, args) |
| 136 |
if args.verbose: |
| 137 |
log.info("Command successful.") |
| 138 |
except Exception as e: |
| 139 |
result = False |
| 140 |
log.debug(e, exc_info=True) |
| 141 |
msg = format_error_to_dict(e) |
| 142 |
|
| 143 |
if args and args.json: |
| 144 |
sys.stderr.write(f"{json.dumps(msg, indent=4)}\n") |
| 145 |
else: |
| 146 |
if not args.verbose: |
| 147 |
msg = format_pretty_error(msg) |
| 148 |
log.error("Error: %s" % " - ".join(str(val) for val in msg.values())) |
| 149 |
|
| 150 |
disconnect_instance(inst) |
| 151 |
|
| 152 |
# Done! |
| 153 |
if result is False: |
| 154 |
sys.exit(1) |