summaryrefslogtreecommitdiff
path: root/source4/scripting/python
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2010-08-24 15:42:54 +1000
committerAndrew Tridgell <tridge@samba.org>2010-08-25 08:40:04 +1000
commit495bd182f5e26cbcb721ab8209f8acad4d612726 (patch)
treece3623ed6855ecd1c0ff176848741a5315dbbd10 /source4/scripting/python
parent4cacdf00020c808ae8e3ef59378da682ce346133 (diff)
downloadsamba-495bd182f5e26cbcb721ab8209f8acad4d612726.tar.gz
samba-495bd182f5e26cbcb721ab8209f8acad4d612726.tar.bz2
samba-495bd182f5e26cbcb721ab8209f8acad4d612726.zip
s4-rodc: next step in RODC join code
a RODC net join can now replicate the schame, config and base partitions, by calling the net.replicate*() python hooks, and driving the GetNCChanges calls from python Pair-Programmed-With: Andrew Bartlett <abartlet@samba.org>
Diffstat (limited to 'source4/scripting/python')
-rw-r--r--source4/scripting/python/samba/join.py344
1 files changed, 218 insertions, 126 deletions
diff --git a/source4/scripting/python/samba/join.py b/source4/scripting/python/samba/join.py
index 6eda582769..d185974faf 100644
--- a/source4/scripting/python/samba/join.py
+++ b/source4/scripting/python/samba/join.py
@@ -22,14 +22,21 @@ import samba.getopt as options
from samba.auth import system_session
from samba.samdb import SamDB
from samba import gensec
-import ldb, samba
+import ldb, samba, sys
from samba.ndr import ndr_pack, ndr_unpack, ndr_print
from samba.dcerpc import security
from samba.dcerpc import drsuapi, misc, netlogon
from samba.credentials import Credentials, DONT_USE_KERBEROS
-from samba.provision import secretsdb_self_join
+from samba.provision import secretsdb_self_join, provision, FILL_DRS, find_setup_dir
+from samba.net import Net
+import logging
-def join_rodc(server=None, creds=None, lp=None, site=None, netbios_name=None):
+class join_ctx:
+ '''hold join context variables'''
+ pass
+
+def join_rodc(server=None, creds=None, lp=None, site=None, netbios_name=None,
+ targetdir=None):
"""join as a RODC"""
if server is None:
@@ -42,19 +49,19 @@ def join_rodc(server=None, creds=None, lp=None, site=None, netbios_name=None):
except:
pass
- def cleanup_old_join(samdb, acct_dn, server_dn, ntds_dn,
- krbtgt_dn, connection_dn, topology_dn):
+ def cleanup_old_join(ctx):
+ '''remove any DNs from a previous join'''
try:
# find the krbtgt link
- res = samdb.search(base=acct_dn, scope=ldb.SCOPE_BASE, attrs=["msDS-krbTgtLink"])
- del_noerror(samdb, acct_dn)
- del_noerror(samdb, connection_dn)
- del_noerror(samdb, krbtgt_dn)
- del_noerror(samdb, ntds_dn)
- del_noerror(samdb, server_dn)
- del_noerror(samdb, topology_dn)
- new_krbtgt_dn = res[0]["msDS-Krbtgtlink"][0]
- del_noerror(samdb, new_krbtgt_dn)
+ res = ctx.samdb.search(base=ctx.acct_dn, scope=ldb.SCOPE_BASE, attrs=["msDS-krbTgtLink"])
+ del_noerror(ctx.samdb, ctx.acct_dn)
+ del_noerror(ctx.samdb, ctx.connection_dn)
+ del_noerror(ctx.samdb, ctx.krbtgt_dn)
+ del_noerror(ctx.samdb, ctx.ntds_dn)
+ del_noerror(ctx.samdb, ctx.server_dn)
+ del_noerror(ctx.samdb, ctx.topology_dn)
+ ctx.new_krbtgt_dn = res[0]["msDS-Krbtgtlink"][0]
+ del_noerror(ctx.samdb, ctx.new_krbtgt_dn)
except:
pass
@@ -71,6 +78,11 @@ def join_rodc(server=None, creds=None, lp=None, site=None, netbios_name=None):
binsid = res[0]["tokenGroups"][0]
return samdb.schema_format_value("objectSID", binsid)
+ def get_domain_name(samdb):
+ # this should be done via CLDAP
+ res = samdb.search(base=samdb.get_default_basedn(), scope=ldb.SCOPE_BASE, attrs=["name"])
+ return res[0]["name"][0]
+
def do_DsBind(drs):
'''make a DsBind call, returning the binding handle'''
bind_info = drsuapi.DsBindInfoCtr()
@@ -107,183 +119,263 @@ def join_rodc(server=None, creds=None, lp=None, site=None, netbios_name=None):
(info, handle) = drs.DsBind(misc.GUID(drsuapi.DRSUAPI_DS_BIND_GUID), bind_info)
return handle
-
- creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
-
- samdb = SamDB(url="ldap://%s" % server,
- session_info=system_session(),
- credentials=creds, lp=lp)
-
- myname = netbios_name
- samname = "%s$" % myname
- base_dn = str(samdb.get_default_basedn())
- domsid = samdb.get_domain_sid()
- dc_ntds_dn = get_dsServiceName(samdb)
- dc_dnsHostName = get_dnsHostName(samdb)
- acct_pass = samba.generate_random_password(12, 32)
- mysid = get_mysid(samdb)
+ def replicate_partition(ctx, dn, schema=False):
+ '''replicate a partition'''
+
+ # setup for a GetNCChanges call
+ req8 = drsuapi.DsGetNCChangesRequest8()
+
+ exop = drsuapi.DRSUAPI_EXOP_NONE
+ null_guid = misc.GUID()
+ req8.destination_dsa_guid = misc.GUID("9c637462-5b8c-4467-aef2-bdb1f57bc4ef")
+ req8.source_dsa_invocation_id = misc.GUID(ctx.samdb.get_invocation_id())
+ req8.naming_context = drsuapi.DsReplicaObjectIdentifier()
+ req8.naming_context.dn = dn.decode("utf-8")
+ req8.highwatermark = drsuapi.DsReplicaHighWaterMark()
+ req8.highwatermark.tmp_highest_usn = 0
+ req8.highwatermark.reserved_usn = 0
+ req8.highwatermark.highest_usn = 0
+ req8.uptodateness_vector = None
+ req8.replica_flags = 0
+ req8.replica_flags |= (drsuapi.DRSUAPI_DRS_INIT_SYNC |
+ drsuapi.DRSUAPI_DRS_PER_SYNC |
+ drsuapi.DRSUAPI_DRS_GET_ANC |
+ drsuapi.DRSUAPI_DRS_NEVER_SYNCED)
+ req8.max_object_count = 402
+ req8.max_ndr_size = 402116
+ req8.extended_op = exop
+ req8.fsmo_info = 0
+ req8.partial_attribute_set = None
+ req8.partial_attribute_set_ex = None
+ req8.mapping_ctr.num_mappings = 0
+ req8.mapping_ctr.mappings = None
+
+ while True:
+ (level, ctr) = ctx.drs.DsGetNCChanges(ctx.drs_handle, 8, req8)
+ net.replicate_chunk(ctx.replication_state, level, ctr, schema=schema)
+ if ctr.more_data == 0:
+ break
+ req8.highwatermark.tmp_highest_usn = ctr.new_highwatermark.tmp_highest_usn
+
+
+ # main join code
+ ctx = join_ctx()
+ ctx.creds = creds
+ ctx.lp = lp
+ ctx.site = site
+ ctx.netbios_name = netbios_name
+ ctx.targetdir = targetdir
+ ctx.server = server
+
+ ctx.creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
+
+ ctx.samdb = SamDB(url="ldap://%s" % ctx.server,
+ session_info=system_session(),
+ credentials=ctx.creds, lp=ctx.lp)
+
+ ctx.myname = netbios_name
+ ctx.samname = "%s$" % ctx.myname
+ ctx.base_dn = str(ctx.samdb.get_default_basedn())
+ ctx.root_dn = str(ctx.samdb.get_root_basedn())
+ ctx.schema_dn = str(ctx.samdb.get_schema_basedn())
+ ctx.config_dn = str(ctx.samdb.get_config_basedn())
+ ctx.domsid = ctx.samdb.get_domain_sid()
+
+ ctx.dc_ntds_dn = get_dsServiceName(ctx.samdb)
+ ctx.dc_dnsHostName = get_dnsHostName(ctx.samdb)
+ ctx.acct_pass = samba.generate_random_password(12, 32)
+ ctx.mysid = get_mysid(ctx.samdb)
# work out the DNs of all the objects we will be adding
- admin_dn = "<SID=%s>" % mysid
- krbtgt_dn = "CN=krbtgt_%s,CN=Users,%s" % (myname, base_dn)
- server_dn = "CN=%s,CN=Servers,CN=%s,CN=Sites,%s" % (myname, site, samdb.get_config_basedn())
- ntds_dn = "CN=NTDS Settings,%s" % server_dn
- connection_dn = "CN=RODC Connection (FRS),%s" % ntds_dn
- topology_dn = "CN=%s,CN=Topology,CN=Domain System Volume,CN=DFSR-GlobalSettings,CN=System,%s" % (myname, base_dn)
+ ctx.admin_dn = "<SID=%s>" % ctx.mysid
+ ctx.krbtgt_dn = "CN=krbtgt_%s,CN=Users,%s" % (ctx.myname, ctx.base_dn)
+ ctx.server_dn = "CN=%s,CN=Servers,CN=%s,CN=Sites,%s" % (ctx.myname, ctx.site, ctx.config_dn)
+ ctx.ntds_dn = "CN=NTDS Settings,%s" % ctx.server_dn
+ ctx.connection_dn = "CN=RODC Connection (FRS),%s" % ctx.ntds_dn
+ ctx.topology_dn = "CN=%s,CN=Topology,CN=Domain System Volume,CN=DFSR-GlobalSettings,CN=System,%s" % (ctx.myname, ctx.base_dn)
- never_reveal_sid = "%s-572" % domsid;
- reveal_sid = "%s-571" % domsid;
+ # we should lookup these SIDs, and have far more never reveal SIDs
+ ctx.never_reveal_sid = "%s-572" % ctx.domsid;
+ ctx.reveal_sid = "%s-571" % ctx.domsid;
- dnsdomain = ldb.Dn(samdb, base_dn).canonical_str().split('/')[0]
- dnshostname = "%s.%s" % (myname, dnsdomain)
+ ctx.dnsdomain = ldb.Dn(ctx.samdb, ctx.base_dn).canonical_str().split('/')[0]
+ ctx.realm = ctx.dnsdomain
+ ctx.dnshostname = "%s.%s" % (ctx.myname, ctx.dnsdomain)
- acct_dn = "CN=%s,OU=Domain Controllers,%s" % (myname, base_dn)
+ ctx.acct_dn = "CN=%s,OU=Domain Controllers,%s" % (ctx.myname, ctx.base_dn)
- cleanup_old_join(samdb, acct_dn, server_dn, ntds_dn,
- krbtgt_dn, connection_dn, topology_dn)
+ cleanup_old_join(ctx)
- print "Adding %s" % acct_dn
+ print "Adding %s" % ctx.acct_dn
rec = {
- "dn" : acct_dn,
+ "dn" : ctx.acct_dn,
"objectClass": "computer",
- "displayname": samname,
- "samaccountname" : samname,
+ "displayname": ctx.samname,
+ "samaccountname" : ctx.samname,
"useraccountcontrol" : "83890176",
- "managedby" : admin_dn,
- "dnshostname" : dnshostname,
- "msDS-NeverRevealGroup" : "<SID=%s>" % never_reveal_sid,
- "msDS-RevealOnDemandGroup" : "<SID=%s>" % reveal_sid}
- samdb.add(rec)
+ "managedby" : ctx.admin_dn,
+ "dnshostname" : ctx.dnshostname,
+ "msDS-NeverRevealGroup" : "<SID=%s>" % ctx.never_reveal_sid,
+ "msDS-RevealOnDemandGroup" : "<SID=%s>" % ctx.reveal_sid}
+ ctx.samdb.add(rec)
- print "Adding %s" % krbtgt_dn
+ print "Adding %s" % ctx.krbtgt_dn
rec = {
- "dn" : krbtgt_dn,
+ "dn" : ctx.krbtgt_dn,
"objectclass" : "user",
"useraccountcontrol" : "514",
"showinadvancedviewonly" : "TRUE",
"description" : "tricky account"}
- samdb.add(rec, ["rodc_join:1:1"])
+ ctx.samdb.add(rec, ["rodc_join:1:1"])
# now we need to search for the samAccountName attribute on the krbtgt DN,
# as this will have been magically set to the krbtgt number
- res = samdb.search(base=krbtgt_dn, scope=ldb.SCOPE_BASE, attrs=["samAccountName"])
- krbtgt_name = res[0]["samAccountName"][0]
+ res = ctx.samdb.search(base=ctx.krbtgt_dn, scope=ldb.SCOPE_BASE, attrs=["samAccountName"])
+ ctx.krbtgt_name = res[0]["samAccountName"][0]
- print "Got krbtgt_name=%s" % krbtgt_name
+ print "Got krbtgt_name=%s" % ctx.krbtgt_name
m = ldb.Message()
- m.dn = ldb.Dn(samdb, acct_dn)
- m["msDS-krbTgtLink"] = ldb.MessageElement(krbtgt_dn,
+ m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
+ m["msDS-krbTgtLink"] = ldb.MessageElement(ctx.krbtgt_dn,
ldb.FLAG_MOD_REPLACE, "msDS-krbTgtLink")
- samdb.modify(m)
+ ctx.samdb.modify(m)
- new_krbtgt_dn = "CN=%s,CN=Users,%s" % (krbtgt_name, base_dn)
- print "Renaming %s to %s" % (krbtgt_dn, new_krbtgt_dn)
- samdb.rename(krbtgt_dn, new_krbtgt_dn)
+ ctx.new_krbtgt_dn = "CN=%s,CN=Users,%s" % (ctx.krbtgt_name, ctx.base_dn)
+ print "Renaming %s to %s" % (ctx.krbtgt_dn, ctx.new_krbtgt_dn)
+ ctx.samdb.rename(ctx.krbtgt_dn, ctx.new_krbtgt_dn)
- print "Adding %s" % server_dn
+ print "Adding %s" % ctx.server_dn
rec = {
- "dn": server_dn,
+ "dn": ctx.server_dn,
"objectclass" : "server",
"systemFlags" : "1375731712",
- "serverReference" : acct_dn,
- "dnsHostName" : dnshostname}
- samdb.add(rec)
+ "serverReference" : ctx.acct_dn,
+ "dnsHostName" : ctx.dnshostname}
+ ctx.samdb.add(rec)
- print "Adding %s" % ntds_dn
+ print "Adding %s" % ctx.ntds_dn
rec = {
- "dn" : ntds_dn,
+ "dn" : ctx.ntds_dn,
"objectclass" : "nTDSDSA",
- "objectCategory" : "CN=NTDS-DSA-RO,%s" % samdb.get_schema_basedn(),
+ "objectCategory" : "CN=NTDS-DSA-RO,%s" % ctx.schema_dn,
"systemFlags" : "33554432",
- "dMDLocation" : str(samdb.get_schema_basedn()),
+ "dMDLocation" : ctx.schema_dn,
"options" : "37",
"msDS-Behavior-Version" : "4",
- "msDS-HasDomainNCs" : str(samdb.get_default_basedn()),
- "msDS-HasFullReplicaNCs" : [ str(samdb.get_default_basedn()),
- str(samdb.get_config_basedn()),
- str(samdb.get_schema_basedn()) ]}
- samdb.add(rec, ["rodc_join:1:1"])
+ "msDS-HasDomainNCs" : ctx.base_dn,
+ "msDS-HasFullReplicaNCs" : [ ctx.base_dn, ctx.config_dn, ctx.schema_dn ]}
+ ctx.samdb.add(rec, ["rodc_join:1:1"])
- print "Adding %s" % connection_dn
+ print "Adding %s" % ctx.connection_dn
rec = {
- "dn" : connection_dn,
+ "dn" : ctx.connection_dn,
"objectclass" : "nTDSConnection",
"enabledconnection" : "TRUE",
"options" : "65",
- "fromServer" : dc_ntds_dn}
- samdb.add(rec)
+ "fromServer" : ctx.dc_ntds_dn}
+ ctx.samdb.add(rec)
- print "Adding %s" % topology_dn
+ print "Adding %s" % ctx.topology_dn
rec = {
- "dn" : topology_dn,
+ "dn" : ctx.topology_dn,
"objectclass" : "msDFSR-Member",
- "msDFSR-ComputerReference" : acct_dn,
- "serverReference" : ntds_dn}
- samdb.add(rec)
+ "msDFSR-ComputerReference" : ctx.acct_dn,
+ "serverReference" : ctx.ntds_dn}
+ ctx.samdb.add(rec)
- print "Adding HOST SPNs to %s" % acct_dn
+ print "Adding HOST SPNs to %s" % ctx.acct_dn
m = ldb.Message()
- m.dn = ldb.Dn(samdb, acct_dn)
- SPNs = [ "HOST/%s" % myname,
- "HOST/%s" % dnshostname ]
+ m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
+ SPNs = [ "HOST/%s" % ctx.myname,
+ "HOST/%s" % ctx.dnshostname ]
m["servicePrincipalName"] = ldb.MessageElement(SPNs,
ldb.FLAG_MOD_ADD,
"servicePrincipalName")
- samdb.modify(m)
+ ctx.samdb.modify(m)
- print "Adding RestrictedKrbHost SPNs to %s" % acct_dn
+ print "Adding RestrictedKrbHost SPNs to %s" % ctx.acct_dn
m = ldb.Message()
- m.dn = ldb.Dn(samdb, acct_dn)
- SPNs = [ "RestrictedKrbHost/%s" % myname,
- "RestrictedKrbHost/%s" % dnshostname ]
+ m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
+ SPNs = [ "RestrictedKrbHost/%s" % ctx.myname,
+ "RestrictedKrbHost/%s" % ctx.dnshostname ]
m["servicePrincipalName"] = ldb.MessageElement(SPNs,
ldb.FLAG_MOD_ADD,
"servicePrincipalName")
- samdb.modify(m)
+ ctx.samdb.modify(m)
- print "Setting account password for %s" % samname
- samdb.setpassword("(&(objectClass=user)(sAMAccountName=%s))" % samname,
- acct_pass,
- force_change_at_next_login=False,
- username=samname)
+ print "Setting account password for %s" % ctx.samname
+ ctx.samdb.setpassword("(&(objectClass=user)(sAMAccountName=%s))" % ctx.samname,
+ ctx.acct_pass,
+ force_change_at_next_login=False,
+ username=ctx.samname)
- print "Enabling account %s" % acct_dn
+ print "Enabling account %s" % ctx.acct_dn
# weird, its already enabled, but w2k8r2 disables then re-enables again
m = ldb.Message()
- m.dn = ldb.Dn(samdb, acct_dn)
+ m.dn = ldb.Dn(ctx.samdb, ctx.acct_dn)
m["userAccountControl"] = ldb.MessageElement("83890178",
ldb.FLAG_MOD_REPLACE,
"userAccountControl")
- samdb.modify(m)
+ ctx.samdb.modify(m)
m["userAccountControl"] = ldb.MessageElement("83890176",
ldb.FLAG_MOD_REPLACE,
"userAccountControl")
- samdb.modify(m)
+ ctx.samdb.modify(m)
- print "Doing DsBind as %s" % samname
+ print "Doing DsBind as %s" % ctx.samname
- acct_creds = Credentials()
- acct_creds.guess(lp)
- acct_creds.set_kerberos_state(DONT_USE_KERBEROS)
- acct_creds.set_username(samname)
- acct_creds.set_password(acct_pass)
+ ctx.acct_creds = Credentials()
+ ctx.acct_creds.guess(ctx.lp)
+ ctx.acct_creds.set_kerberos_state(DONT_USE_KERBEROS)
+ ctx.acct_creds.set_username(ctx.samname)
+ ctx.acct_creds.set_password(ctx.acct_pass)
- drs = drsuapi.drsuapi("ncacn_ip_tcp:w2k8[seal,print]", lp, acct_creds)
- drs_handle = do_DsBind(drs)
- print "DRS Handle: %s" % drs_handle
+ ctx.drs = drsuapi.drsuapi("ncacn_ip_tcp:%s[seal,print]" % ctx.server, ctx.lp, ctx.creds)
+ ctx.drs_handle = do_DsBind(ctx.drs)
+ print "DRS Handle: %s" % ctx.drs_handle
print "Calling DsRGetDCNameEx2"
- netr = netlogon.netlogon("ncacn_np:w2k8[print]", lp, acct_creds)
- dcname = netr.netr_DsRGetDCNameEx2(server_unc=dc_dnsHostName.decode("utf-8"),
- client_account=None,
- mask=0,
- domain_name=dnsdomain.decode("utf-8"),
- domain_guid=None, site_name=None, flags=0x40001020)
-
- print "Calling secrets self join"
- secretsdb_self_join()
-
- print "Note: RODC join is a work in progress - replication not done"
- #print ndr_print(dcname)
+ netr = netlogon.netlogon("ncacn_np:%s[print]" % ctx.server, ctx.lp, ctx.acct_creds)
+ dcname = netr.netr_DsRGetDCNameEx2(server_unc=ctx.dc_dnsHostName.decode("utf-8"),
+ client_account=None,
+ mask=0,
+ domain_name=ctx.dnsdomain.decode("utf-8"),
+ domain_guid=None, site_name=None,
+ flags=0x40001020)
+
+ print ndr_print(dcname)
+ print "Calling bare provision"
+
+ setup_dir = find_setup_dir()
+ logger = logging.getLogger("provision")
+ logger.addHandler(logging.StreamHandler(sys.stdout))
+ smbconf = lp.configfile
+
+ presult = provision(setup_dir, logger, system_session(), None,
+ smbconf=smbconf, targetdir=targetdir, samdb_fill=FILL_DRS,
+ realm=ctx.realm, rootdn=ctx.root_dn, domaindn=ctx.base_dn,
+ schemadn=ctx.schema_dn,
+ configdn=ctx.config_dn,
+ serverdn=ctx.server_dn, domain=get_domain_name(ctx.samdb),
+ hostname=ctx.myname, hostip="127.0.0.1", domainsid=ctx.domsid,
+ machinepass=ctx.acct_pass, serverrole="domain controller",
+ sitename=ctx.site)
+ print "Provision OK for domain DN %s" % presult.domaindn
+ ctx.local_samdb = presult.samdb
+ ctx.lp = presult.lp
+
+
+ print "Starting replication"
+ ctx.local_samdb.transaction_start()
+
+ net = Net(creds=ctx.creds, lp=ctx.lp)
+ ctx.replication_state = net.replicate_init(ctx.local_samdb, ctx.lp, ctx.drs)
+
+ replicate_partition(ctx, ctx.schema_dn, schema=True)
+ replicate_partition(ctx, ctx.config_dn)
+ replicate_partition(ctx, ctx.base_dn)
+
+ print "Committing"
+ ctx.local_samdb.transaction_commit()