s4-rodc: broke up RODC join into separate functions
[nivanova/samba-autobuild/.git] / source4 / scripting / python / samba / join.py
1 #!/usr/bin/env python
2 #
3 # python join code
4 # Copyright Andrew Tridgell 2010
5 # Copyright Andrew Bartlett 2010
6 #
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.
11 #
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.
16 #
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/>.
19 #
20
21 import samba.getopt as options
22 from samba.auth import system_session
23 from samba.samdb import SamDB
24 from samba import gensec
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
32 import logging
33
34 class join_ctx:
35     '''hold join context variables'''
36     pass
37
38 def join_rodc(server=None, creds=None, lp=None, site=None, netbios_name=None,
39               targetdir=None):
40     """join as a RODC"""
41
42     if server is None:
43         raise Exception("You must supply a server for a RODC join")
44
45     def del_noerror(samdb, dn):
46         try:
47             samdb.delete(dn)
48             print "Deleted %s" % dn
49         except:
50             pass
51
52     def cleanup_old_join(ctx):
53         '''remove any DNs from a previous join'''
54         try:
55             # find the krbtgt link
56             res = ctx.samdb.search(base=ctx.acct_dn, scope=ldb.SCOPE_BASE, attrs=["msDS-krbTgtLink"])
57             del_noerror(ctx.samdb, ctx.acct_dn)
58             del_noerror(ctx.samdb, ctx.connection_dn)
59             del_noerror(ctx.samdb, ctx.krbtgt_dn)
60             del_noerror(ctx.samdb, ctx.ntds_dn)
61             del_noerror(ctx.samdb, ctx.server_dn)
62             del_noerror(ctx.samdb, ctx.topology_dn)
63             ctx.new_krbtgt_dn = res[0]["msDS-Krbtgtlink"][0]
64             del_noerror(ctx.samdb, ctx.new_krbtgt_dn)
65         except:
66             pass
67
68     def get_dsServiceName(samdb):
69         res = samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["dsServiceName"])
70         return res[0]["dsServiceName"][0]
71
72     def get_dnsHostName(samdb):
73         res = samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["dnsHostName"])
74         return res[0]["dnsHostName"][0]
75
76     def get_mysid(samdb):
77         res = samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["tokenGroups"])
78         binsid = res[0]["tokenGroups"][0]
79         return samdb.schema_format_value("objectSID", binsid)
80
81     def get_domain_name(samdb):
82         # this should be done via CLDAP
83         res = samdb.search(base=samdb.get_default_basedn(), scope=ldb.SCOPE_BASE, attrs=["name"])
84         return res[0]["name"][0]
85
86     def do_DsBind(drs):
87         '''make a DsBind call, returning the binding handle'''
88         bind_info = drsuapi.DsBindInfoCtr()
89         bind_info.length = 28
90         bind_info.info = drsuapi.DsBindInfo28()
91         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_BASE;
92         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
93         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
94         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
95         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
96         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
97         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
98         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
99         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
100         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
101         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
102         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
103         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
104         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
105         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
106         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
107         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
108         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
109         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
110         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
111         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
112         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
113         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
114         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
115         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
116         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
117         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
118         bind_info.info.supported_extensions     |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
119         (info, handle) = drs.DsBind(misc.GUID(drsuapi.DRSUAPI_DS_BIND_GUID), bind_info)
120         return handle
121
122     def get_rodc_partial_attribute_set(ctx):
123         '''get a list of attributes for RODC replication'''
124         partial_attribute_set = drsuapi.DsPartialAttributeSet()
125         partial_attribute_set.version = 1
126
127         ctx.attids = []
128
129         # the exact list of attids we send is quite critical. Note that
130         # we do ask for the secret attributes, but set set SPECIAL_SECRET_PROCESSING
131         # to zero them out
132         res = ctx.local_samdb.search(base=ctx.schema_dn, scope=ldb.SCOPE_SUBTREE,
133                                      expression="objectClass=attributeSchema",
134                                      attrs=["lDAPDisplayName", "systemFlags",
135                                             "searchFlags"])
136
137         for r in res:
138             ldap_display_name = r["lDAPDisplayName"][0]
139             if "systemFlags" in r:
140                 system_flags      = r["systemFlags"][0]
141                 if (int(system_flags) & (samba.dsdb.DS_FLAG_ATTR_NOT_REPLICATED |
142                                          samba.dsdb.DS_FLAG_ATTR_IS_CONSTRUCTED)):
143                     continue
144             search_flags = r["searchFlags"][0]
145             if (int(search_flags) & samba.dsdb.SEARCH_FLAG_RODC_ATTRIBUTE):
146                 continue
147             attid = ctx.local_samdb.get_attid_from_lDAPDisplayName(ldap_display_name)
148             ctx.attids.append(int(attid))
149
150         # the attids do need to be sorted, or windows doesn't return
151         # all the attributes we need
152         ctx.attids.sort()
153         partial_attribute_set.attids         = ctx.attids
154         partial_attribute_set.num_attids = len(ctx.attids)
155         return partial_attribute_set
156
157
158     def replicate_partition(ctx, dn, schema=False, exop=drsuapi.DRSUAPI_EXOP_NONE):
159         '''replicate a partition'''
160
161         # setup for a GetNCChanges call
162         req8 = drsuapi.DsGetNCChangesRequest8()
163
164         req8.destination_dsa_guid           = ctx.ntds_guid
165         req8.source_dsa_invocation_id       = misc.GUID(ctx.samdb.get_invocation_id())
166         req8.naming_context                 = drsuapi.DsReplicaObjectIdentifier()
167         req8.naming_context.dn              = dn.decode("utf-8")
168         req8.highwatermark                  = drsuapi.DsReplicaHighWaterMark()
169         req8.highwatermark.tmp_highest_usn  = 0
170         req8.highwatermark.reserved_usn     = 0
171         req8.highwatermark.highest_usn      = 0
172         req8.uptodateness_vector            = None
173         if exop == drsuapi.DRSUAPI_EXOP_REPL_SECRET:
174             req8.replica_flags              = 0
175         else:
176             req8.replica_flags              =  (drsuapi.DRSUAPI_DRS_INIT_SYNC |
177                                                 drsuapi.DRSUAPI_DRS_PER_SYNC |
178                                                 drsuapi.DRSUAPI_DRS_GET_ANC |
179                                                 drsuapi.DRSUAPI_DRS_NEVER_SYNCED |
180                                                 drsuapi.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING)
181         req8.max_object_count                = 402
182         req8.max_ndr_size                    = 402116
183         req8.extended_op                     = exop
184         req8.fsmo_info                       = 0
185         req8.partial_attribute_set           = None
186         req8.partial_attribute_set_ex        = None
187         req8.mapping_ctr.num_mappings        = 0
188         req8.mapping_ctr.mappings            = None
189
190         while True:
191             if not schema:
192                 req8.partial_attribute_set = get_rodc_partial_attribute_set(ctx)
193
194             (level, ctr) = ctx.drs.DsGetNCChanges(ctx.drs_handle, 8, req8)
195             ctx.net.replicate_chunk(ctx.replication_state, level, ctr, schema=schema)
196             if ctr.more_data == 0:
197                 break
198             req8.highwatermark.tmp_highest_usn = ctr.new_highwatermark.tmp_highest_usn
199
200
201     def join_add_objects(ctx):
202         '''add the various objects needed for the join'''
203         print "Adding %s" % ctx.acct_dn
204         rec = {
205             "dn" : ctx.acct_dn,
206             "objectClass": "computer",
207             "displayname": ctx.samname,
208             "samaccountname" : ctx.samname,
209             "useraccountcontrol" : str(samba.dsdb.UF_WORKSTATION_TRUST_ACCOUNT |
210                                        samba.dsdb.UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION |
211                                        samba.dsdb.UF_PARTIAL_SECRETS_ACCOUNT),
212             "managedby" : ctx.admin_dn,
213             "dnshostname" : ctx.dnshostname,
214             "msDS-NeverRevealGroup" : ctx.never_reveal_sid,
215             "msDS-RevealOnDemandGroup" : ctx.reveal_sid}
216         ctx.samdb.add(rec)
217
218         print "Adding %s" % ctx.krbtgt_dn
219         rec = {
220             "dn" : ctx.krbtgt_dn,
221             "objectclass" : "user",
222             "useraccountcontrol" : str(samba.dsdb.UF_NORMAL_ACCOUNT |
223                                        samba.dsdb.UF_ACCOUNTDISABLE),
224             "showinadvancedviewonly" : "TRUE",
225             "description" : "tricky account"}
226         ctx.samdb.add(rec, ["rodc_join:1:1"])
227
228         # now we need to search for the samAccountName attribute on the krbtgt DN,
229         # as this will have been magically set to the krbtgt number
230         res = ctx.samdb.search(base=ctx.krbtgt_dn, scope=ldb.SCOPE_BASE, attrs=["samAccountName"])
231         ctx.krbtgt_name = res[0]["samAccountName"][0]
232
233         print "Got krbtgt_name=%s" % ctx.krbtgt_name
234
235         m = ldb.Message()
236         m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
237         m["msDS-krbTgtLink"] = ldb.MessageElement(ctx.krbtgt_dn,
238                                                   ldb.FLAG_MOD_REPLACE, "msDS-krbTgtLink")
239         ctx.samdb.modify(m)
240
241         ctx.new_krbtgt_dn = "CN=%s,CN=Users,%s" % (ctx.krbtgt_name, ctx.base_dn)
242         print "Renaming %s to %s" % (ctx.krbtgt_dn, ctx.new_krbtgt_dn)
243         ctx.samdb.rename(ctx.krbtgt_dn, ctx.new_krbtgt_dn)
244
245         print "Adding %s" % ctx.server_dn
246         rec = {
247             "dn": ctx.server_dn,
248             "objectclass" : "server",
249             "systemFlags" : str(samba.dsdb.SYSTEM_FLAG_CONFIG_ALLOW_RENAME |
250                                 samba.dsdb.SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE |
251                                 samba.dsdb.SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE),
252             "serverReference" : ctx.acct_dn,
253             "dnsHostName" : ctx.dnshostname}
254         ctx.samdb.add(rec)
255
256         print "Adding %s" % ctx.ntds_dn
257         rec = {
258             "dn" : ctx.ntds_dn,
259             "objectclass" : "nTDSDSA",
260             "objectCategory" : "CN=NTDS-DSA-RO,%s" % ctx.schema_dn,
261             "systemFlags" : str(samba.dsdb.SYSTEM_FLAG_DISALLOW_MOVE_ON_DELETE),
262             "dMDLocation" : ctx.schema_dn,
263             "options" : "37",
264             "msDS-Behavior-Version" : "4",
265             "msDS-HasDomainNCs" : ctx.base_dn,
266             "msDS-HasFullReplicaNCs" : [ ctx.base_dn, ctx.config_dn, ctx.schema_dn ]}
267         ctx.samdb.add(rec, ["rodc_join:1:1"])
268
269         # find the GUID of our NTDS DN
270         res = ctx.samdb.search(base=ctx.ntds_dn, scope=ldb.SCOPE_BASE, attrs=["objectGUID"])
271         ctx.ntds_guid = misc.GUID(ctx.samdb.schema_format_value("objectGUID", res[0]["objectGUID"][0]))
272
273         print "Adding %s" % ctx.connection_dn
274         rec = {
275             "dn" : ctx.connection_dn,
276             "objectclass" : "nTDSConnection",
277             "enabledconnection" : "TRUE",
278             "options" : "65",
279             "fromServer" : ctx.dc_ntds_dn}
280         ctx.samdb.add(rec)
281
282         print "Adding %s" % ctx.topology_dn
283         rec = {
284             "dn" : ctx.topology_dn,
285             "objectclass" : "msDFSR-Member",
286             "msDFSR-ComputerReference" : ctx.acct_dn,
287             "serverReference" : ctx.ntds_dn}
288         ctx.samdb.add(rec)
289
290         print "Adding HOST SPNs to %s" % ctx.acct_dn
291         m = ldb.Message()
292         m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
293         SPNs = [ "HOST/%s" % ctx.myname,
294                  "HOST/%s" % ctx.dnshostname ]
295         m["servicePrincipalName"] = ldb.MessageElement(SPNs,
296                                                        ldb.FLAG_MOD_ADD,
297                                                        "servicePrincipalName")
298         ctx.samdb.modify(m)
299
300         print "Adding RestrictedKrbHost SPNs to %s" % ctx.acct_dn
301         m = ldb.Message()
302         m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
303         SPNs = [ "RestrictedKrbHost/%s" % ctx.myname,
304                  "RestrictedKrbHost/%s" % ctx.dnshostname ]
305         m["servicePrincipalName"] = ldb.MessageElement(SPNs,
306                                                        ldb.FLAG_MOD_ADD,
307                                                        "servicePrincipalName")
308         ctx.samdb.modify(m)
309
310         print "Setting account password for %s" % ctx.samname
311         ctx.samdb.setpassword("(&(objectClass=user)(sAMAccountName=%s))" % ctx.samname,
312                               ctx.acct_pass,
313                               force_change_at_next_login=False,
314                               username=ctx.samname)
315
316
317     def join_drs_connect(ctx):
318         '''connect to DRSUAPI'''
319         print "Doing DsBind as %s" % ctx.samname
320         ctx.acct_creds = Credentials()
321         ctx.acct_creds.guess(ctx.lp)
322         ctx.acct_creds.set_kerberos_state(DONT_USE_KERBEROS)
323         ctx.acct_creds.set_username(ctx.samname)
324         ctx.acct_creds.set_password(ctx.acct_pass)
325
326         ctx.drs = drsuapi.drsuapi("ncacn_ip_tcp:%s[seal,print]" % ctx.server, ctx.lp, ctx.acct_creds)
327         ctx.drs_handle = do_DsBind(ctx.drs)
328
329
330     def join_provision(ctx):
331         '''provision the local SAM'''
332
333         print "Calling bare provision"
334
335         setup_dir = find_setup_dir()
336         logger = logging.getLogger("provision")
337         logger.addHandler(logging.StreamHandler(sys.stdout))
338         smbconf = lp.configfile
339
340         presult = provision(setup_dir, logger, system_session(), None,
341                             smbconf=smbconf, targetdir=targetdir, samdb_fill=FILL_DRS,
342                             realm=ctx.realm, rootdn=ctx.root_dn, domaindn=ctx.base_dn,
343                             schemadn=ctx.schema_dn,
344                             configdn=ctx.config_dn,
345                             serverdn=ctx.server_dn, domain=ctx.domain_name,
346                             hostname=ctx.myname, hostip="127.0.0.1", domainsid=ctx.domsid,
347                             machinepass=ctx.acct_pass, serverrole="domain controller",
348                             sitename=ctx.site)
349         print "Provision OK for domain DN %s" % presult.domaindn
350         ctx.local_samdb = presult.samdb
351         ctx.lp          = presult.lp
352
353
354     def join_replicate(ctx):
355         '''replicate the SAM'''
356
357         print "Starting replication"
358         ctx.local_samdb.transaction_start()
359
360         ctx.replication_state = ctx.net.replicate_init(ctx.local_samdb, ctx.lp, ctx.drs)
361
362         replicate_partition(ctx, ctx.schema_dn, schema=True)
363         replicate_partition(ctx, ctx.config_dn)
364         replicate_partition(ctx, ctx.base_dn)
365         replicate_partition(ctx, ctx.acct_dn, exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET)
366         replicate_partition(ctx, ctx.new_krbtgt_dn, exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET)
367
368         print "Committing SAM database"
369         ctx.local_samdb.transaction_commit()
370
371
372     # main join code
373     ctx = join_ctx()
374     ctx.creds = creds
375     ctx.lp = lp
376     ctx.site = site
377     ctx.netbios_name = netbios_name
378     ctx.targetdir = targetdir
379     ctx.server = server
380
381     ctx.creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
382
383     ctx.samdb = SamDB(url="ldap://%s" % ctx.server,
384                       session_info=system_session(),
385                       credentials=ctx.creds, lp=ctx.lp)
386     ctx.net = Net(creds=ctx.creds, lp=ctx.lp)
387
388     ctx.myname = netbios_name
389     ctx.samname = "%s$" % ctx.myname
390     ctx.base_dn = str(ctx.samdb.get_default_basedn())
391     ctx.root_dn = str(ctx.samdb.get_root_basedn())
392     ctx.schema_dn = str(ctx.samdb.get_schema_basedn())
393     ctx.config_dn = str(ctx.samdb.get_config_basedn())
394     ctx.domsid = ctx.samdb.get_domain_sid()
395     ctx.domain_name = get_domain_name(ctx.samdb)
396
397     ctx.dc_ntds_dn = get_dsServiceName(ctx.samdb)
398     ctx.dc_dnsHostName = get_dnsHostName(ctx.samdb)
399     ctx.acct_pass = samba.generate_random_password(12, 32)
400     ctx.mysid = get_mysid(ctx.samdb)
401
402     # work out the DNs of all the objects we will be adding
403     ctx.admin_dn = "<SID=%s>" % ctx.mysid
404     ctx.krbtgt_dn = "CN=krbtgt_%s,CN=Users,%s" % (ctx.myname, ctx.base_dn)
405     ctx.server_dn = "CN=%s,CN=Servers,CN=%s,CN=Sites,%s" % (ctx.myname, ctx.site, ctx.config_dn)
406     ctx.ntds_dn = "CN=NTDS Settings,%s" % ctx.server_dn
407     ctx.connection_dn = "CN=RODC Connection (FRS),%s" % ctx.ntds_dn
408     ctx.topology_dn = "CN=%s,CN=Topology,CN=Domain System Volume,CN=DFSR-GlobalSettings,CN=System,%s" % (ctx.myname, ctx.base_dn)
409
410     # setup some defaults for accounts that should be replicated to this RODC
411     ctx.never_reveal_sid = [ "<SID=%s-%s>" % (ctx.domsid, security.DOMAIN_RID_RODC_DENY),
412                              "<SID=%s>" % security.SID_BUILTIN_ADMINISTRATORS,
413                              "<SID=%s>" % security.SID_BUILTIN_SERVER_OPERATORS,
414                              "<SID=%s>" % security.SID_BUILTIN_BACKUP_OPERATORS,
415                              "<SID=%s>" % security.SID_BUILTIN_ACCOUNT_OPERATORS ]
416     ctx.reveal_sid = "<SID=%s-571>" % ctx.domsid;
417
418     ctx.dnsdomain = ldb.Dn(ctx.samdb, ctx.base_dn).canonical_str().split('/')[0]
419     ctx.realm = ctx.dnsdomain
420     ctx.dnshostname = "%s.%s" % (ctx.myname, ctx.dnsdomain)
421
422     ctx.acct_dn = "CN=%s,OU=Domain Controllers,%s" % (ctx.myname, ctx.base_dn)
423
424     cleanup_old_join(ctx)
425     try:
426         join_add_objects(ctx)
427         join_drs_connect(ctx)
428         join_provision(ctx)
429         join_replicate(ctx)
430     except:
431         print "Join failed - cleaning up"
432         cleanup_old_join(ctx)
433         raise
434
435     print "Joined domain %s (SID %s) as an RODC" % (ctx.domain_name, ctx.domsid)
436