4 # Copyright Andrew Tridgell 2010
5 # Copyright Andrew Bartlett 2010
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 samba.getopt as options
22 from samba.auth import system_session
23 from samba.samdb import SamDB
24 from samba import gensec, Ldb
25 import ldb, samba, sys
26 from samba.ndr import ndr_pack, ndr_unpack, ndr_print
27 from samba.dcerpc import security
28 from samba.dcerpc import drsuapi, misc, netlogon
29 from samba.credentials import Credentials, DONT_USE_KERBEROS
30 from samba.provision import secretsdb_self_join, provision, FILL_DRS, find_setup_dir
31 from samba.net import Net
33 from samba.drs_utils import drs_Replicate
35 # this makes debugging easier
36 samba.talloc_enable_null_tracking()
39 '''hold join context variables'''
42 def join_rodc(server=None, creds=None, lp=None, site=None, netbios_name=None,
43 targetdir=None, domain=None):
47 raise Exception("You must supply a server for a RODC join")
49 def del_noerror(samdb, dn):
52 print "Deleted %s" % dn
56 def cleanup_old_join(ctx):
57 '''remove any DNs from a previous join'''
59 # find the krbtgt link
60 res = ctx.samdb.search(base=ctx.acct_dn, scope=ldb.SCOPE_BASE, attrs=["msDS-krbTgtLink"])
61 del_noerror(ctx.samdb, ctx.acct_dn)
62 del_noerror(ctx.samdb, ctx.connection_dn)
63 del_noerror(ctx.samdb, ctx.krbtgt_dn)
64 del_noerror(ctx.samdb, ctx.ntds_dn)
65 del_noerror(ctx.samdb, ctx.server_dn)
66 del_noerror(ctx.samdb, ctx.topology_dn)
67 ctx.new_krbtgt_dn = res[0]["msDS-Krbtgtlink"][0]
68 del_noerror(ctx.samdb, ctx.new_krbtgt_dn)
72 def get_dsServiceName(samdb):
73 res = samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["dsServiceName"])
74 return res[0]["dsServiceName"][0]
76 def get_dnsHostName(samdb):
77 res = samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["dnsHostName"])
78 return res[0]["dnsHostName"][0]
80 def get_domain_name(samdb):
81 '''get netbios name of the domain from the partitions record'''
82 partitions_dn = samdb.get_partitions_dn()
83 res = samdb.search(base=partitions_dn, scope=ldb.SCOPE_ONELEVEL, attrs=["nETBIOSName"],
84 expression='ncName=%s' % samdb.get_default_basedn())
85 return res[0]["nETBIOSName"][0]
88 res = samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["tokenGroups"])
89 binsid = res[0]["tokenGroups"][0]
90 return samdb.schema_format_value("objectSID", binsid)
92 def join_add_objects(ctx):
93 '''add the various objects needed for the join'''
94 print "Adding %s" % ctx.acct_dn
97 "objectClass": "computer",
98 "displayname": ctx.samname,
99 "samaccountname" : ctx.samname,
100 "useraccountcontrol" : str(samba.dsdb.UF_WORKSTATION_TRUST_ACCOUNT |
101 samba.dsdb.UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION |
102 samba.dsdb.UF_PARTIAL_SECRETS_ACCOUNT),
103 "managedby" : ctx.admin_dn,
104 "dnshostname" : ctx.dnshostname,
105 "msDS-NeverRevealGroup" : ctx.never_reveal_sid,
106 "msDS-RevealOnDemandGroup" : ctx.reveal_sid}
109 print "Adding %s" % ctx.krbtgt_dn
111 "dn" : ctx.krbtgt_dn,
112 "objectclass" : "user",
113 "useraccountcontrol" : str(samba.dsdb.UF_NORMAL_ACCOUNT |
114 samba.dsdb.UF_ACCOUNTDISABLE),
115 "showinadvancedviewonly" : "TRUE",
116 "description" : "tricky account"}
117 ctx.samdb.add(rec, ["rodc_join:1:1"])
119 # now we need to search for the samAccountName attribute on the krbtgt DN,
120 # as this will have been magically set to the krbtgt number
121 res = ctx.samdb.search(base=ctx.krbtgt_dn, scope=ldb.SCOPE_BASE, attrs=["samAccountName"])
122 ctx.krbtgt_name = res[0]["samAccountName"][0]
124 print "Got krbtgt_name=%s" % ctx.krbtgt_name
127 m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
128 m["msDS-krbTgtLink"] = ldb.MessageElement(ctx.krbtgt_dn,
129 ldb.FLAG_MOD_REPLACE, "msDS-krbTgtLink")
132 ctx.new_krbtgt_dn = "CN=%s,CN=Users,%s" % (ctx.krbtgt_name, ctx.base_dn)
133 print "Renaming %s to %s" % (ctx.krbtgt_dn, ctx.new_krbtgt_dn)
134 ctx.samdb.rename(ctx.krbtgt_dn, ctx.new_krbtgt_dn)
136 print "Adding %s" % ctx.server_dn
139 "objectclass" : "server",
140 "systemFlags" : str(samba.dsdb.SYSTEM_FLAG_CONFIG_ALLOW_RENAME |
141 samba.dsdb.SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE |
142 samba.dsdb.SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE),
143 "serverReference" : ctx.acct_dn,
144 "dnsHostName" : ctx.dnshostname}
147 print "Adding %s" % ctx.ntds_dn
150 "objectclass" : "nTDSDSA",
151 "objectCategory" : "CN=NTDS-DSA-RO,%s" % ctx.schema_dn,
152 "systemFlags" : str(samba.dsdb.SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE),
153 "dMDLocation" : ctx.schema_dn,
155 "msDS-Behavior-Version" : "4",
156 "msDS-HasDomainNCs" : ctx.base_dn,
157 "msDS-HasFullReplicaNCs" : [ ctx.base_dn, ctx.config_dn, ctx.schema_dn ]}
158 ctx.samdb.add(rec, ["rodc_join:1:1"])
160 # find the GUID of our NTDS DN
161 res = ctx.samdb.search(base=ctx.ntds_dn, scope=ldb.SCOPE_BASE, attrs=["objectGUID"])
162 ctx.ntds_guid = misc.GUID(ctx.samdb.schema_format_value("objectGUID", res[0]["objectGUID"][0]))
164 print "Adding %s" % ctx.connection_dn
166 "dn" : ctx.connection_dn,
167 "objectclass" : "nTDSConnection",
168 "enabledconnection" : "TRUE",
170 "fromServer" : ctx.dc_ntds_dn}
173 print "Adding %s" % ctx.topology_dn
175 "dn" : ctx.topology_dn,
176 "objectclass" : "msDFSR-Member",
177 "msDFSR-ComputerReference" : ctx.acct_dn,
178 "serverReference" : ctx.ntds_dn}
181 print "Adding HOST SPNs to %s" % ctx.acct_dn
183 m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
184 SPNs = [ "HOST/%s" % ctx.myname,
185 "HOST/%s" % ctx.dnshostname ]
186 m["servicePrincipalName"] = ldb.MessageElement(SPNs,
188 "servicePrincipalName")
191 print "Adding RestrictedKrbHost SPNs to %s" % ctx.acct_dn
193 m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
194 SPNs = [ "RestrictedKrbHost/%s" % ctx.myname,
195 "RestrictedKrbHost/%s" % ctx.dnshostname ]
196 m["servicePrincipalName"] = ldb.MessageElement(SPNs,
198 "servicePrincipalName")
201 print "Setting account password for %s" % ctx.samname
202 ctx.samdb.setpassword("(&(objectClass=user)(sAMAccountName=%s))" % ctx.samname,
204 force_change_at_next_login=False,
205 username=ctx.samname)
208 def join_provision(ctx):
209 '''provision the local SAM'''
211 print "Calling bare provision"
213 setup_dir = find_setup_dir()
214 logger = logging.getLogger("provision")
215 logger.addHandler(logging.StreamHandler(sys.stdout))
216 smbconf = lp.configfile
218 presult = provision(setup_dir, logger, system_session(), None,
219 smbconf=smbconf, targetdir=targetdir, samdb_fill=FILL_DRS,
220 realm=ctx.realm, rootdn=ctx.root_dn, domaindn=ctx.base_dn,
221 schemadn=ctx.schema_dn,
222 configdn=ctx.config_dn,
223 serverdn=ctx.server_dn, domain=ctx.domain_name,
224 hostname=ctx.myname, hostip="127.0.0.1", domainsid=ctx.domsid,
225 machinepass=ctx.acct_pass, serverrole="domain controller",
227 print "Provision OK for domain DN %s" % presult.domaindn
228 ctx.local_samdb = presult.samdb
230 ctx.paths = presult.paths
233 def join_replicate(ctx):
234 '''replicate the SAM'''
236 print "Starting replication"
237 ctx.local_samdb.transaction_start()
239 source_dsa_invocation_id = misc.GUID(ctx.samdb.get_invocation_id())
241 acct_creds = Credentials()
242 acct_creds.guess(ctx.lp)
243 acct_creds.set_kerberos_state(DONT_USE_KERBEROS)
244 acct_creds.set_username(ctx.samname)
245 acct_creds.set_password(ctx.acct_pass)
247 repl = drs_Replicate("ncacn_ip_tcp:%s[seal]" % ctx.server, ctx.lp, acct_creds, ctx.local_samdb)
249 repl.replicate(ctx.schema_dn, source_dsa_invocation_id, ctx.ntds_guid, schema=True)
250 repl.replicate(ctx.config_dn, source_dsa_invocation_id, ctx.ntds_guid)
251 repl.replicate(ctx.base_dn, source_dsa_invocation_id, ctx.ntds_guid)
252 repl.replicate(ctx.acct_dn, source_dsa_invocation_id, ctx.ntds_guid, exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET)
253 repl.replicate(ctx.new_krbtgt_dn, source_dsa_invocation_id, ctx.ntds_guid, exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET)
255 print "Committing SAM database"
256 ctx.local_samdb.transaction_commit()
259 def join_finalise(ctx):
260 '''finalise the join, mark us synchronised and setup secrets db'''
262 print "Setting isSynchronized"
264 m.dn = ldb.Dn(ctx.samdb, '@ROOTDSE')
265 m["isSynchronized"] = ldb.MessageElement("TRUE", ldb.FLAG_MOD_REPLACE, "isSynchronized")
268 secrets_ldb = Ldb(ctx.paths.secrets, session_info=system_session(), lp=ctx.lp)
270 print "Setting up secrets database"
271 secretsdb_self_join(secrets_ldb, domain=ctx.domain_name,
273 dnsdomain=ctx.dnsdomain,
274 netbiosname=ctx.myname,
275 domainsid=security.dom_sid(ctx.domsid),
276 machinepass=ctx.acct_pass,
277 secure_channel_type=misc.SEC_CHAN_RODC)
286 ctx.netbios_name = netbios_name
287 ctx.targetdir = targetdir
290 ctx.creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
292 ctx.samdb = SamDB(url="ldap://%s" % ctx.server,
293 session_info=system_session(),
294 credentials=ctx.creds, lp=ctx.lp)
295 ctx.net = Net(creds=ctx.creds, lp=ctx.lp)
297 ctx.myname = netbios_name
298 ctx.samname = "%s$" % ctx.myname
299 ctx.base_dn = str(ctx.samdb.get_default_basedn())
300 ctx.root_dn = str(ctx.samdb.get_root_basedn())
301 ctx.schema_dn = str(ctx.samdb.get_schema_basedn())
302 ctx.config_dn = str(ctx.samdb.get_config_basedn())
303 ctx.domsid = ctx.samdb.get_domain_sid()
304 ctx.domain_name = get_domain_name(ctx.samdb)
306 ctx.dc_ntds_dn = get_dsServiceName(ctx.samdb)
307 ctx.dc_dnsHostName = get_dnsHostName(ctx.samdb)
308 ctx.acct_pass = samba.generate_random_password(12, 32)
309 ctx.mysid = get_mysid(ctx.samdb)
311 # work out the DNs of all the objects we will be adding
312 ctx.admin_dn = "<SID=%s>" % ctx.mysid
313 ctx.krbtgt_dn = "CN=krbtgt_%s,CN=Users,%s" % (ctx.myname, ctx.base_dn)
314 ctx.server_dn = "CN=%s,CN=Servers,CN=%s,CN=Sites,%s" % (ctx.myname, ctx.site, ctx.config_dn)
315 ctx.ntds_dn = "CN=NTDS Settings,%s" % ctx.server_dn
316 ctx.connection_dn = "CN=RODC Connection (FRS),%s" % ctx.ntds_dn
317 ctx.topology_dn = "CN=%s,CN=Topology,CN=Domain System Volume,CN=DFSR-GlobalSettings,CN=System,%s" % (ctx.myname, ctx.base_dn)
319 # setup some defaults for accounts that should be replicated to this RODC
320 ctx.never_reveal_sid = [ "<SID=%s-%s>" % (ctx.domsid, security.DOMAIN_RID_RODC_DENY),
321 "<SID=%s>" % security.SID_BUILTIN_ADMINISTRATORS,
322 "<SID=%s>" % security.SID_BUILTIN_SERVER_OPERATORS,
323 "<SID=%s>" % security.SID_BUILTIN_BACKUP_OPERATORS,
324 "<SID=%s>" % security.SID_BUILTIN_ACCOUNT_OPERATORS ]
325 ctx.reveal_sid = "<SID=%s-%s>" % (ctx.domsid, security.DOMAIN_RID_RODC_ALLOW)
327 ctx.dnsdomain = ldb.Dn(ctx.samdb, ctx.base_dn).canonical_str().split('/')[0]
328 ctx.realm = ctx.dnsdomain
329 ctx.dnshostname = "%s.%s" % (ctx.myname, ctx.dnsdomain)
331 ctx.acct_dn = "CN=%s,OU=Domain Controllers,%s" % (ctx.myname, ctx.base_dn)
333 cleanup_old_join(ctx)
335 join_add_objects(ctx)
340 print "Join failed - cleaning up"
341 cleanup_old_join(ctx)
344 print "Joined domain %s (SID %s) as an RODC" % (ctx.domain_name, ctx.domsid)