s4-netlogon: implement dcesrv_netr_DsRAddressToSitenamesExW
[Samba/aatanasov.git] / source4 / setup / domainlevel
blob811e29cb2dec6eb258bbb0237627519a369f61b9
1 #!/usr/bin/python
3 # Raises domain and forest function levels
5 # Copyright Matthias Dieter Wallnoefer 2009
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 import sys
23 # Find right directory when running from source tree
24 sys.path.insert(0, "bin/python")
26 import samba.getopt as options
27 import optparse
28 import ldb
30 from samba.auth import system_session
31 from samba.samdb import SamDB
32 from samba import DS_DOMAIN_FUNCTION_2000, DS_DOMAIN_FUNCTION_2003
33 from samba import DS_DOMAIN_FUNCTION_2008, DS_DOMAIN_FUNCTION_2008_R2
35 parser = optparse.OptionParser("domainlevel (show | raise <options>)")
36 sambaopts = options.SambaOptions(parser)
37 parser.add_option_group(sambaopts)
38 parser.add_option_group(options.VersionOptions(parser))
39 credopts = options.CredentialsOptions(parser)
40 parser.add_option_group(credopts)
41 parser.add_option("--quiet", help="Be quiet", action="store_true")
42 parser.add_option("--forest",
43 help="The forest function level (2000 | 2003 | 2008 | 2008_R2). We don't support mixed/interim (NT4 DC support) levels.", type=str)
44 parser.add_option("--domain",
45 help="The domain function level (2000 | 2003 | 2008 | 2008_R2). We don't support mixed/interim (NT4 DC support) levels.", type=str)
46 opts, args = parser.parse_args()
49 # print a message if quiet is not set
51 def message(text):
52 if not opts.quiet:
53 print text
55 if len(args) == 0:
56 parser.print_usage()
57 sys.exit(1)
59 lp = sambaopts.get_loadparm()
60 creds = credopts.get_credentials(lp)
62 samdb = SamDB(url=lp.get("sam database"), session_info=system_session(),
63 credentials=creds, lp=lp)
65 domain_dn = SamDB.domain_dn(samdb)
67 res_forest = samdb.search("CN=Partitions,CN=Configuration," + domain_dn,
68 scope=ldb.SCOPE_BASE, attrs=["msDS-Behavior-Version"])
69 assert(len(res_forest) == 1)
71 res_domain = samdb.search(domain_dn, scope=ldb.SCOPE_BASE,
72 attrs=["msDS-Behavior-Version"])
73 assert(len(res_domain) == 1)
75 try:
76 level_forest = int(res_forest[0]["msDS-Behavior-Version"][0])
77 level_domain = int(res_domain[0]["msDS-Behavior-Version"][0])
79 if level_forest < 0 or level_forest == 1 or level_forest > 4 or level_domain < 0 or level_domain == 1 or level_domain > 4:
80 print "ERROR: Domain and/or forest functional level(s) is/are invalid. Correct them or reprovision!"
81 sys.exit(1)
82 if level_forest > level_domain:
83 print "ERROR: Forest function level is higher than the domain level(s). That can't be. Correct this or reprovision!"
84 sys.exit(1)
85 except:
86 print "ERROR: Could not retrieve the actual domain and forest level!"
87 if args[0] == "show":
88 print "So the levels can't be displayed!"
89 sys.exit(1)
91 if args[0] == "show":
92 message("Domain and forest function level for domain '" + domain_dn + "'")
93 message("")
95 if level_forest == DS_DOMAIN_FUNCTION_2000:
96 outstr = "2000"
97 elif level_forest == DS_DOMAIN_FUNCTION_2003:
98 outstr = "2003"
99 elif level_forest == DS_DOMAIN_FUNCTION_2008:
100 outstr = "2008"
101 elif level_forest == DS_DOMAIN_FUNCTION_2008_R2:
102 outstr = "2008 R2"
103 message("Forest function level: (Windows) " + outstr)
105 if level_domain == DS_DOMAIN_FUNCTION_2000:
106 outstr = "2000"
107 elif level_domain == DS_DOMAIN_FUNCTION_2003:
108 outstr = "2003"
109 elif level_domain == DS_DOMAIN_FUNCTION_2008:
110 outstr = "2008"
111 elif level_domain == DS_DOMAIN_FUNCTION_2008_R2:
112 outstr = "2008 R2"
113 message("Domain function level: (Windows) " + outstr)
115 elif args[0] == "raise":
116 msgs = []
118 if opts.domain is not None:
119 arg = opts.domain
121 if arg == "2000":
122 new_level_domain = DS_DOMAIN_FUNCTION_2000
123 elif arg == "2003":
124 new_level_domain = DS_DOMAIN_FUNCTION_2003
125 elif arg == "2008":
126 new_level_domain = DS_DOMAIN_FUNCTION_2008
127 elif arg == "2008_R2":
128 new_level_domain = DS_DOMAIN_FUNCTION_2008_R2
129 else:
130 print "ERROR: Wrong argument '" + arg + "'!"
131 sys.exit(1)
133 if new_level_domain <= level_domain:
134 print "ERROR: Domain function level can't be smaller equal to the actual one!"
135 sys.exit(1)
137 m = ldb.Message()
138 m.dn = ldb.Dn(samdb, domain_dn)
139 m["msDS-Behavior-Version"]= ldb.MessageElement(
140 str(new_level_domain), ldb.FLAG_MOD_REPLACE,
141 "msDS-Behavior-Version")
142 samdb.modify(m)
144 level_domain = new_level_domain
146 msgs.append("Domain function level changed!")
148 if opts.forest is not None:
149 arg = opts.forest
151 if arg == "2000":
152 new_level_forest = DS_DOMAIN_FUNCTION_2000
153 elif arg == "2003":
154 new_level_forest = DS_DOMAIN_FUNCTION_2003
155 elif arg == "2008":
156 new_level_forest = DS_DOMAIN_FUNCTION_2008
157 elif arg == "2008_R2":
158 new_level_forest = DS_DOMAIN_FUNCTION_2008_R2
159 else:
160 print "ERROR: Wrong argument '" + arg + "'!"
161 sys.exit(1)
163 if new_level_forest <= level_forest:
164 print "ERROR: Forest function level can't be smaller equal to the actual one!"
165 sys.exit(1)
167 if new_level_forest > level_domain:
168 print "ERROR: Forest function level can't be higher than the domain function level(s). Please raise it/them first!"
169 sys.exit(1)
171 m = ldb.Message()
173 m.dn = ldb.Dn(samdb, "CN=Partitions,CN=Configuration,"
174 + domain_dn)
175 m["msDS-Behavior-Version"]= ldb.MessageElement(
176 str(new_level_forest), ldb.FLAG_MOD_REPLACE,
177 "msDS-Behavior-Version")
178 samdb.modify(m)
180 msgs.append("Forest function level changed!")
182 msgs.append("All changes applied successfully!")
184 message("\n".join(msgs))
185 else:
186 print "ERROR: Wrong argument '" + args[0] + "'!"
187 sys.exit(1)