diff options
author | Andrew Bartlett <abartlet@samba.org> | 2023-05-09 15:50:46 +1200 |
---|---|---|
committer | Andrew Bartlett <abartlet@samba.org> | 2023-05-16 23:29:32 +0000 |
commit | 844eb0737676af73b499fd722b48256d6df587f4 (patch) | |
tree | 3624d10eafafb8de22ce77061238defee858d7ce /python | |
parent | 59694ad0a4cc489f1baa4c2c94c6322c0f22c1df (diff) | |
download | samba-844eb0737676af73b499fd722b48256d6df587f4.tar.gz |
python: Move helper functions for functional levels into a new file
Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Diffstat (limited to 'python')
-rw-r--r-- | python/samba/functional_level.py | 62 | ||||
-rw-r--r-- | python/samba/netcmd/domain/__init__.py | 2 | ||||
-rw-r--r-- | python/samba/netcmd/domain/common.py | 47 | ||||
-rw-r--r-- | python/samba/netcmd/domain/functional_prep.py | 9 | ||||
-rw-r--r-- | python/samba/netcmd/domain/level.py | 13 |
5 files changed, 78 insertions, 55 deletions
diff --git a/python/samba/functional_level.py b/python/samba/functional_level.py new file mode 100644 index 00000000000..1cb09ce1d79 --- /dev/null +++ b/python/samba/functional_level.py @@ -0,0 +1,62 @@ +# domain management - common code +# +# Copyright Catlayst .Net Ltd 2017-2023 +# Copyright Jelmer Vernooij 2007-2012 +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +from samba.dsdb import ( + DS_DOMAIN_FUNCTION_2000, + DS_DOMAIN_FUNCTION_2003, + DS_DOMAIN_FUNCTION_2008, + DS_DOMAIN_FUNCTION_2008_R2, + DS_DOMAIN_FUNCTION_2012, + DS_DOMAIN_FUNCTION_2012_R2, + DS_DOMAIN_FUNCTION_2003_MIXED, + DS_DOMAIN_FUNCTION_2016 +) + +string_version_to_constant = { + "2000": DS_DOMAIN_FUNCTION_2000, + "2003": DS_DOMAIN_FUNCTION_2003, + "2008": DS_DOMAIN_FUNCTION_2008, + "2008_R2": DS_DOMAIN_FUNCTION_2008_R2, + "2012": DS_DOMAIN_FUNCTION_2012, + "2012_R2": DS_DOMAIN_FUNCTION_2012_R2, + "2016": DS_DOMAIN_FUNCTION_2016, +} + + +def string_to_level(string): + """Interpret a string indicating a functional level.""" + return string_version_to_constant[string] + + +def level_to_string(level): + """turn the level enum number into a printable string.""" + if level < DS_DOMAIN_FUNCTION_2000: + return "invalid" + strings = { + DS_DOMAIN_FUNCTION_2000: "2000", + DS_DOMAIN_FUNCTION_2003_MIXED: \ + "2003 with mixed domains/interim (NT4 DC support)", + DS_DOMAIN_FUNCTION_2003: "2003", + DS_DOMAIN_FUNCTION_2008: "2008", + DS_DOMAIN_FUNCTION_2008_R2: "2008 R2", + DS_DOMAIN_FUNCTION_2012: "2012", + DS_DOMAIN_FUNCTION_2012_R2: "2012 R2", + DS_DOMAIN_FUNCTION_2016: "2016", + } + return strings.get(level, "higher than 2016") diff --git a/python/samba/netcmd/domain/__init__.py b/python/samba/netcmd/domain/__init__.py index 552784e180d..3defc120be7 100644 --- a/python/samba/netcmd/domain/__init__.py +++ b/python/samba/netcmd/domain/__init__.py @@ -29,7 +29,7 @@ from .backup import cmd_domain_backup from .claim import cmd_domain_claim from .classicupgrade import cmd_domain_classicupgrade from .common import (common_join_options, common_ntvfs_options, - common_provision_join_options, string_to_level) + common_provision_join_options) from .dcpromo import cmd_domain_dcpromo from .demote import cmd_domain_demote from .functional_prep import cmd_domain_functional_prep diff --git a/python/samba/netcmd/domain/common.py b/python/samba/netcmd/domain/common.py index f89146f4abd..15c8935dae7 100644 --- a/python/samba/netcmd/domain/common.py +++ b/python/samba/netcmd/domain/common.py @@ -24,16 +24,6 @@ from uuid import UUID -from samba.dsdb import ( - DS_DOMAIN_FUNCTION_2000, - DS_DOMAIN_FUNCTION_2003, - DS_DOMAIN_FUNCTION_2008, - DS_DOMAIN_FUNCTION_2008_R2, - DS_DOMAIN_FUNCTION_2012, - DS_DOMAIN_FUNCTION_2012_R2, - DS_DOMAIN_FUNCTION_2003_MIXED, - DS_DOMAIN_FUNCTION_2016 -) from samba.netcmd import CommandError, Option from samba.samdb import get_default_backend_store @@ -76,43 +66,6 @@ common_join_options = [ ] -string_version_to_constant = { - "2000": DS_DOMAIN_FUNCTION_2000, - "2003": DS_DOMAIN_FUNCTION_2003, - "2008": DS_DOMAIN_FUNCTION_2008, - "2008_R2": DS_DOMAIN_FUNCTION_2008_R2, - "2012": DS_DOMAIN_FUNCTION_2012, - "2012_R2": DS_DOMAIN_FUNCTION_2012_R2, - "2016": DS_DOMAIN_FUNCTION_2016, -} - - -def string_to_level(string): - """Interpret a string indicating a functional level.""" - try: - return string_version_to_constant[string] - except KeyError as e: - raise CommandError(f"'{string}' is not a valid domain level") - - -def level_to_string(level): - """turn the level enum number into a printable string.""" - if level < DS_DOMAIN_FUNCTION_2000: - return "invalid" - strings = { - DS_DOMAIN_FUNCTION_2000: "2000", - DS_DOMAIN_FUNCTION_2003_MIXED: \ - "2003 with mixed domains/interim (NT4 DC support)", - DS_DOMAIN_FUNCTION_2003: "2003", - DS_DOMAIN_FUNCTION_2008: "2008", - DS_DOMAIN_FUNCTION_2008_R2: "2008 R2", - DS_DOMAIN_FUNCTION_2012: "2012", - DS_DOMAIN_FUNCTION_2012_R2: "2012 R2", - DS_DOMAIN_FUNCTION_2016: "2016", - } - return strings.get(level, "higher than 2016") - - def parse_text(value): """Parse message element to string value.""" if value is not None: diff --git a/python/samba/netcmd/domain/functional_prep.py b/python/samba/netcmd/domain/functional_prep.py index 4253ffd7521..3e1d4e155c1 100644 --- a/python/samba/netcmd/domain/functional_prep.py +++ b/python/samba/netcmd/domain/functional_prep.py @@ -30,7 +30,7 @@ from samba.netcmd import Command, CommandError, Option from samba.netcmd.fsmo import get_fsmo_roleowner from samba.samdb import SamDB -from .common import string_to_level +from samba import functional_level class cmd_domain_functional_prep(Command): @@ -66,7 +66,12 @@ class cmd_domain_functional_prep(Command): lp = sambaopts.get_loadparm() creds = credopts.get_credentials(lp) H = kwargs.get("H") - target_level = string_to_level(kwargs.get("function_level")) + function_level = kwargs.get("function_level") + try: + target_level = functional_level.string_to_level(function_level) + except KeyError: + raise CommandError(f"'{function_level}' is not known to Samba as an AD functional level") + forest_prep = kwargs.get("forest_prep") domain_prep = kwargs.get("domain_prep") diff --git a/python/samba/netcmd/domain/level.py b/python/samba/netcmd/domain/level.py index c5773d4e9ce..4a12a46dff2 100644 --- a/python/samba/netcmd/domain/level.py +++ b/python/samba/netcmd/domain/level.py @@ -29,7 +29,7 @@ from samba.dsdb import DS_DOMAIN_FUNCTION_2000 from samba.netcmd import Command, CommandError, Option from samba.samdb import SamDB -from .common import level_to_string, string_to_level +from samba import functional_level class cmd_domain_level(Command): @@ -118,23 +118,26 @@ class cmd_domain_level(Command): self.message("") - outstr = level_to_string(level_forest) + outstr = functional_level.level_to_string(level_forest) self.message("Forest function level: (Windows) " + outstr) if level_domain == DS_DOMAIN_FUNCTION_2000 and level_domain_mixed: outstr = "2000 mixed (NT4 DC support)" else: - outstr = level_to_string(level_domain) + outstr = functional_level.level_to_string(level_domain) self.message("Domain function level: (Windows) " + outstr) - outstr = level_to_string(min_level_dc) + outstr = functional_level.level_to_string(min_level_dc) self.message("Lowest function level of a DC: (Windows) " + outstr) elif subcommand == "raise": msgs = [] if domain_level is not None: - new_level_domain = string_to_level(domain_level) + try: + new_level_domain = functional_level.string_to_level(domain_level) + except KeyError: + raise CommandError(f"New functional level '{domain_level}' is not known to Samba as an AD functional level") if new_level_domain <= level_domain and level_domain_mixed == 0: raise CommandError("Domain function level can't be smaller than or equal to the actual one!") |