s4-rodc: added REPL_SECRET exop replication of accounts
[samba.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         for r in res:
137             ldap_display_name = r["lDAPDisplayName"][0]
138             if "systemFlags" in r:
139                 system_flags      = r["systemFlags"][0]
140                 if (int(system_flags) & (samba.dsdb.DS_FLAG_ATTR_NOT_REPLICATED |
141                                          samba.dsdb.DS_FLAG_ATTR_IS_CONSTRUCTED)):
142                     continue
143             search_flags = r["searchFlags"][0]
144             if (int(search_flags) & samba.dsdb.SEARCH_FLAG_RODC_ATTRIBUTE):
145                 continue
146             attid = ctx.local_samdb.get_attid_from_lDAPDisplayName(ldap_display_name)
147             ctx.attids.append(int(attid))
148
149         # the attids do need to be sorted, or windows doesn't return
150         # all the attributes we need
151         ctx.attids.sort()
152         partial_attribute_set.attids         = ctx.attids
153         partial_attribute_set.num_attids = len(ctx.attids)
154         return partial_attribute_set
155
156
157     def replicate_partition(ctx, dn, schema=False, exop=drsuapi.DRSUAPI_EXOP_NONE):
158         '''replicate a partition'''
159
160         # setup for a GetNCChanges call
161         req8 = drsuapi.DsGetNCChangesRequest8()
162
163         req8.destination_dsa_guid           = ctx.ntds_guid
164         req8.source_dsa_invocation_id       = misc.GUID(ctx.samdb.get_invocation_id())
165         req8.naming_context                 = drsuapi.DsReplicaObjectIdentifier()
166         req8.naming_context.dn              = dn.decode("utf-8")
167         req8.highwatermark                  = drsuapi.DsReplicaHighWaterMark()
168         req8.highwatermark.tmp_highest_usn  = 0
169         req8.highwatermark.reserved_usn     = 0
170         req8.highwatermark.highest_usn      = 0
171         req8.uptodateness_vector            = None
172         if exop == drsuapi.DRSUAPI_EXOP_REPL_SECRET:
173             req8.replica_flags              = 0
174         else:
175             req8.replica_flags              =  (drsuapi.DRSUAPI_DRS_INIT_SYNC |
176                                                 drsuapi.DRSUAPI_DRS_PER_SYNC |
177                                                 drsuapi.DRSUAPI_DRS_GET_ANC |
178                                                 drsuapi.DRSUAPI_DRS_NEVER_SYNCED |
179                                                 drsuapi.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING)
180         req8.max_object_count                = 402
181         req8.max_ndr_size                    = 402116
182         req8.extended_op                     = exop
183         req8.fsmo_info                       = 0
184         req8.partial_attribute_set           = None
185         req8.partial_attribute_set_ex        = None
186         req8.mapping_ctr.num_mappings        = 0
187         req8.mapping_ctr.mappings            = None
188
189         while True:
190             if not schema:
191                 req8.partial_attribute_set = get_rodc_partial_attribute_set(ctx)
192             (level, ctr) = ctx.drs.DsGetNCChanges(ctx.drs_handle, 8, req8)
193             net.replicate_chunk(ctx.replication_state, level, ctr, schema=schema)
194             if ctr.more_data == 0:
195                 break
196             req8.highwatermark.tmp_highest_usn = ctr.new_highwatermark.tmp_highest_usn
197
198
199     # main join code
200     ctx = join_ctx()
201     ctx.creds = creds
202     ctx.lp = lp
203     ctx.site = site
204     ctx.netbios_name = netbios_name
205     ctx.targetdir = targetdir
206     ctx.server = server
207
208     ctx.creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
209
210     ctx.samdb = SamDB(url="ldap://%s" % ctx.server,
211                       session_info=system_session(),
212                       credentials=ctx.creds, lp=ctx.lp)
213
214     ctx.myname = netbios_name
215     ctx.samname = "%s$" % ctx.myname
216     ctx.base_dn = str(ctx.samdb.get_default_basedn())
217     ctx.root_dn = str(ctx.samdb.get_root_basedn())
218     ctx.schema_dn = str(ctx.samdb.get_schema_basedn())
219     ctx.config_dn = str(ctx.samdb.get_config_basedn())
220     ctx.domsid = ctx.samdb.get_domain_sid()
221
222     ctx.dc_ntds_dn = get_dsServiceName(ctx.samdb)
223     ctx.dc_dnsHostName = get_dnsHostName(ctx.samdb)
224     ctx.acct_pass = samba.generate_random_password(12, 32)
225     ctx.mysid = get_mysid(ctx.samdb)
226
227     # work out the DNs of all the objects we will be adding
228     ctx.admin_dn = "<SID=%s>" % ctx.mysid
229     ctx.krbtgt_dn = "CN=krbtgt_%s,CN=Users,%s" % (ctx.myname, ctx.base_dn)
230     ctx.server_dn = "CN=%s,CN=Servers,CN=%s,CN=Sites,%s" % (ctx.myname, ctx.site, ctx.config_dn)
231     ctx.ntds_dn = "CN=NTDS Settings,%s" % ctx.server_dn
232     ctx.connection_dn = "CN=RODC Connection (FRS),%s" % ctx.ntds_dn
233     ctx.topology_dn = "CN=%s,CN=Topology,CN=Domain System Volume,CN=DFSR-GlobalSettings,CN=System,%s" % (ctx.myname, ctx.base_dn)
234
235     # we should lookup these SIDs, and have far more never reveal SIDs
236     ctx.never_reveal_sid = "%s-572" % ctx.domsid;
237     ctx.reveal_sid = "%s-571" % ctx.domsid;
238
239     ctx.dnsdomain = ldb.Dn(ctx.samdb, ctx.base_dn).canonical_str().split('/')[0]
240     ctx.realm = ctx.dnsdomain
241     ctx.dnshostname = "%s.%s" % (ctx.myname, ctx.dnsdomain)
242
243     ctx.acct_dn = "CN=%s,OU=Domain Controllers,%s" % (ctx.myname, ctx.base_dn)
244
245     cleanup_old_join(ctx)
246
247     print "Adding %s" % ctx.acct_dn
248     rec = {
249         "dn" : ctx.acct_dn,
250         "objectClass": "computer",
251         "displayname": ctx.samname,
252         "samaccountname" : ctx.samname,
253         "useraccountcontrol" : "83890176",
254         "managedby" : ctx.admin_dn,
255         "dnshostname" : ctx.dnshostname,
256         "msDS-NeverRevealGroup" : "<SID=%s>" % ctx.never_reveal_sid,
257         "msDS-RevealOnDemandGroup" : "<SID=%s>" % ctx.reveal_sid}
258     ctx.samdb.add(rec)
259
260     print "Adding %s" % ctx.krbtgt_dn
261     rec = {
262         "dn" : ctx.krbtgt_dn,
263         "objectclass" : "user",
264         "useraccountcontrol" : "514",
265         "showinadvancedviewonly" : "TRUE",
266         "description" : "tricky account"}
267     ctx.samdb.add(rec, ["rodc_join:1:1"])
268
269     # now we need to search for the samAccountName attribute on the krbtgt DN,
270     # as this will have been magically set to the krbtgt number
271     res = ctx.samdb.search(base=ctx.krbtgt_dn, scope=ldb.SCOPE_BASE, attrs=["samAccountName"])
272     ctx.krbtgt_name = res[0]["samAccountName"][0]
273
274     print "Got krbtgt_name=%s" % ctx.krbtgt_name
275
276     m = ldb.Message()
277     m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
278     m["msDS-krbTgtLink"] = ldb.MessageElement(ctx.krbtgt_dn,
279                                               ldb.FLAG_MOD_REPLACE, "msDS-krbTgtLink")
280     ctx.samdb.modify(m)
281
282     ctx.new_krbtgt_dn = "CN=%s,CN=Users,%s" % (ctx.krbtgt_name, ctx.base_dn)
283     print "Renaming %s to %s" % (ctx.krbtgt_dn, ctx.new_krbtgt_dn)
284     ctx.samdb.rename(ctx.krbtgt_dn, ctx.new_krbtgt_dn)
285
286     print "Adding %s" % ctx.server_dn
287     rec = {
288         "dn": ctx.server_dn,
289         "objectclass" : "server",
290         "systemFlags" : "1375731712",
291         "serverReference" : ctx.acct_dn,
292         "dnsHostName" : ctx.dnshostname}
293     ctx.samdb.add(rec)
294
295     print "Adding %s" % ctx.ntds_dn
296     rec = {
297         "dn" : ctx.ntds_dn,
298         "objectclass" : "nTDSDSA",
299         "objectCategory" : "CN=NTDS-DSA-RO,%s" % ctx.schema_dn,
300         "systemFlags" : "33554432",
301         "dMDLocation" : ctx.schema_dn,
302         "options" : "37",
303         "msDS-Behavior-Version" : "4",
304         "msDS-HasDomainNCs" : ctx.base_dn,
305         "msDS-HasFullReplicaNCs" : [ ctx.base_dn, ctx.config_dn, ctx.schema_dn ]}
306     ctx.samdb.add(rec, ["rodc_join:1:1"])
307
308     # find the GUID of our NTDS DN
309     res = ctx.samdb.search(base=ctx.ntds_dn, scope=ldb.SCOPE_BASE, attrs=["objectGUID"])
310     ctx.ntds_guid = misc.GUID(ctx.samdb.schema_format_value("objectGUID", res[0]["objectGUID"][0]))
311
312     print "Adding %s" % ctx.connection_dn
313     rec = {
314         "dn" : ctx.connection_dn,
315         "objectclass" : "nTDSConnection",
316         "enabledconnection" : "TRUE",
317         "options" : "65",
318         "fromServer" : ctx.dc_ntds_dn}
319     ctx.samdb.add(rec)
320
321     print "Adding %s" % ctx.topology_dn
322     rec = {
323         "dn" : ctx.topology_dn,
324         "objectclass" : "msDFSR-Member",
325         "msDFSR-ComputerReference" : ctx.acct_dn,
326         "serverReference" : ctx.ntds_dn}
327     ctx.samdb.add(rec)
328
329     print "Adding HOST SPNs to %s" % ctx.acct_dn
330     m = ldb.Message()
331     m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
332     SPNs = [ "HOST/%s" % ctx.myname,
333              "HOST/%s" % ctx.dnshostname ]
334     m["servicePrincipalName"] = ldb.MessageElement(SPNs,
335                                                    ldb.FLAG_MOD_ADD,
336                                                    "servicePrincipalName")
337     ctx.samdb.modify(m)
338
339     print "Adding RestrictedKrbHost SPNs to %s" % ctx.acct_dn
340     m = ldb.Message()
341     m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
342     SPNs = [ "RestrictedKrbHost/%s" % ctx.myname,
343              "RestrictedKrbHost/%s" % ctx.dnshostname ]
344     m["servicePrincipalName"] = ldb.MessageElement(SPNs,
345                                                    ldb.FLAG_MOD_ADD,
346                                                    "servicePrincipalName")
347     ctx.samdb.modify(m)
348
349     print "Setting account password for %s" % ctx.samname
350     ctx.samdb.setpassword("(&(objectClass=user)(sAMAccountName=%s))" % ctx.samname,
351                       ctx.acct_pass,
352                       force_change_at_next_login=False,
353                       username=ctx.samname)
354
355     print "Enabling account %s" % ctx.acct_dn
356     # weird, its already enabled, but w2k8r2 disables then re-enables again
357     m = ldb.Message()
358     m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
359     m["userAccountControl"] = ldb.MessageElement("83890178",
360                                                  ldb.FLAG_MOD_REPLACE,
361                                                  "userAccountControl")
362     ctx.samdb.modify(m)
363
364     m["userAccountControl"] = ldb.MessageElement("83890176",
365                                                  ldb.FLAG_MOD_REPLACE,
366                                                  "userAccountControl")
367     ctx.samdb.modify(m)
368
369     print "Doing DsBind as %s" % ctx.samname
370
371     ctx.acct_creds = Credentials()
372     ctx.acct_creds.guess(ctx.lp)
373     ctx.acct_creds.set_kerberos_state(DONT_USE_KERBEROS)
374     ctx.acct_creds.set_username(ctx.samname)
375     ctx.acct_creds.set_password(ctx.acct_pass)
376
377     ctx.drs = drsuapi.drsuapi("ncacn_ip_tcp:%s[seal,print]" % ctx.server, ctx.lp, ctx.acct_creds)
378     ctx.drs_handle = do_DsBind(ctx.drs)
379     print "DRS Handle: %s" % ctx.drs_handle
380
381     print "Calling DsRGetDCNameEx2"
382     netr = netlogon.netlogon("ncacn_np:%s[print]" % ctx.server, ctx.lp, ctx.acct_creds)
383     dcname = netr.netr_DsRGetDCNameEx2(server_unc=ctx.dc_dnsHostName.decode("utf-8"),
384                                        client_account=None,
385                                        mask=0,
386                                        domain_name=ctx.dnsdomain.decode("utf-8"),
387                                        domain_guid=None, site_name=None,
388                                        flags=0x40001020)
389
390     print ndr_print(dcname)
391     print "Calling bare provision"
392
393     setup_dir = find_setup_dir()
394     logger = logging.getLogger("provision")
395     logger.addHandler(logging.StreamHandler(sys.stdout))
396     smbconf = lp.configfile
397
398     presult = provision(setup_dir, logger, system_session(), None,
399                         smbconf=smbconf, targetdir=targetdir, samdb_fill=FILL_DRS,
400                         realm=ctx.realm, rootdn=ctx.root_dn, domaindn=ctx.base_dn,
401                         schemadn=ctx.schema_dn,
402                         configdn=ctx.config_dn,
403                         serverdn=ctx.server_dn, domain=get_domain_name(ctx.samdb),
404                         hostname=ctx.myname, hostip="127.0.0.1", domainsid=ctx.domsid,
405                         machinepass=ctx.acct_pass, serverrole="domain controller",
406                         sitename=ctx.site)
407     print "Provision OK for domain DN %s" % presult.domaindn
408     ctx.local_samdb = presult.samdb
409     ctx.lp          = presult.lp
410
411
412     print "Starting replication"
413     ctx.local_samdb.transaction_start()
414
415     net = Net(creds=ctx.creds, lp=ctx.lp)
416     ctx.replication_state = net.replicate_init(ctx.local_samdb, ctx.lp, ctx.drs)
417
418     replicate_partition(ctx, ctx.schema_dn, schema=True)
419     replicate_partition(ctx, ctx.config_dn)
420     replicate_partition(ctx, ctx.base_dn)
421     ctx.local_samdb.transaction_commit()
422
423     ctx.local_samdb.transaction_start()
424     replicate_partition(ctx, ctx.acct_dn, exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET)
425     replicate_partition(ctx, ctx.new_krbtgt_dn, exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET)
426
427     print "Committing SAM database"
428     ctx.local_samdb.transaction_commit()