ctdb: Save a few lines with talloc_zero()
[samba4-gss.git] / python / samba / functional_level.py
blobe5ccf3988fb773d94d039f4da1612559e667f8a7
1 # domain management - common code
3 # Copyright Catlayst .Net Ltd 2017-2023
4 # Copyright Jelmer Vernooij 2007-2012
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from samba.dsdb import (
21 DS_DOMAIN_FUNCTION_2000,
22 DS_DOMAIN_FUNCTION_2003,
23 DS_DOMAIN_FUNCTION_2008,
24 DS_DOMAIN_FUNCTION_2008_R2,
25 DS_DOMAIN_FUNCTION_2012,
26 DS_DOMAIN_FUNCTION_2012_R2,
27 DS_DOMAIN_FUNCTION_2003_MIXED,
28 DS_DOMAIN_FUNCTION_2016
31 string_version_to_constant = {
32 "2000": DS_DOMAIN_FUNCTION_2000,
33 "2003": DS_DOMAIN_FUNCTION_2003,
34 "2008": DS_DOMAIN_FUNCTION_2008,
35 "2008_R2": DS_DOMAIN_FUNCTION_2008_R2,
36 "2012": DS_DOMAIN_FUNCTION_2012,
37 "2012_R2": DS_DOMAIN_FUNCTION_2012_R2,
38 "2016": DS_DOMAIN_FUNCTION_2016,
42 def string_to_level(string):
43 """Interpret a string indicating a functional level."""
44 return string_version_to_constant[string]
47 def level_to_string(level):
48 """turn the level enum number into a printable string."""
49 if level < DS_DOMAIN_FUNCTION_2000:
50 return "invalid"
51 strings = {
52 DS_DOMAIN_FUNCTION_2000: "2000",
53 DS_DOMAIN_FUNCTION_2003_MIXED:
54 "2003 with mixed domains/interim (NT4 DC support)",
55 DS_DOMAIN_FUNCTION_2003: "2003",
56 DS_DOMAIN_FUNCTION_2008: "2008",
57 DS_DOMAIN_FUNCTION_2008_R2: "2008 R2",
58 DS_DOMAIN_FUNCTION_2012: "2012",
59 DS_DOMAIN_FUNCTION_2012_R2: "2012 R2",
60 DS_DOMAIN_FUNCTION_2016: "2016",
62 return strings.get(level, "higher than 2016")
64 def dc_level_from_lp(lp):
65 """Return the ad dc functional level as an integer from a LoadParm"""
67 # I don't like the RuntimeError here, but these "can't happen"
68 # except by a developer stuffup.
70 smb_conf_dc_functional_level = lp.get('ad dc functional level')
71 if smb_conf_dc_functional_level is None:
72 # This shouldn't be possible, except if the default option
73 # value is not in the loadparm enum table
74 raise RuntimeError("'ad dc functional level' in smb.conf unrecognised!")
76 try:
77 return string_to_level(smb_conf_dc_functional_level)
78 except KeyError:
79 # This shouldn't be possible at all, unless the table in
80 # python/samba/functional_level.py is not a superset of that
81 # in lib/param/param_table.c
82 raise RuntimeError(f"'ad dc functional level = {smb_conf_dc_functional_level}'"
83 " in smb.conf is not valid!")