From 093dac43312c97a90b68f18ccc80716db84c25f5 Mon Sep 17 00:00:00 2001 From: Matthieu Patou Date: Mon, 28 Nov 2011 20:48:59 +0100 Subject: s4-python: externalize some function to the drs_utils module so that they can be reused --- source4/scripting/python/samba/drs_utils.py | 57 ++++++++++++++++++++++++++++ source4/scripting/python/samba/netcmd/drs.py | 34 +++++------------ 2 files changed, 66 insertions(+), 25 deletions(-) (limited to 'source4') diff --git a/source4/scripting/python/samba/drs_utils.py b/source4/scripting/python/samba/drs_utils.py index fbcbf2f9c9..b712932b59 100644 --- a/source4/scripting/python/samba/drs_utils.py +++ b/source4/scripting/python/samba/drs_utils.py @@ -24,6 +24,63 @@ from samba.net import Net import samba, ldb +class drsException: + """Base element for drs errors""" + + def __init__(self, value): + self.value = value + + def __str__(self): + return "drsException: " + self.value + + +def drsuapi_connect(server, lp, creds): + """ + make a DRSUAPI connection to the server + :param server: the name of the server to connect to + :param lp: a samba line parameter object + :param creds: credential used for the connection + :return: A tuple with the drsuapi bind object, the drsuapi handle + and the supported extensions. + :raise drsException: if the connection fails + """ + + binding_options = "seal" + if int(lp.get("log level")) >= 5: + binding_options += ",print" + binding_string = "ncacn_ip_tcp:%s[%s]" % (server, binding_options) + try: + drsuapiBind = drsuapi.drsuapi(binding_string, lp, creds) + (drsuapiHandle, bindSupportedExtensions) = drs_DsBind(drsuapiBind) + except Exception, e: + raise drsException("DRS connection to %s failed" % server, e) + + return (drsuapiBind, drsuapiHandle, bindSupportedExtensions) + +def sendDsReplicaSync(drsuapiBind, drsuapi_handle, source_dsa_guid, naming_context, req_option): + """ + :param drsuapiBind: a drsuapi Bind object + :param drsuapi_handle: a drsuapi hanle on the drsuapi connection + :param source_dsa_guid: the guid of the source dsa for the replication + :param naming_context: the DN of the naming context to replicate + :param req_options: replication options for the DsReplicaSync call + :raise drsException: if any error occur while sending and receiving the reply + for the dsReplicaSync + """ + + nc = drsuapi.DsReplicaObjectIdentifier() + nc.dn = naming_context + + req1 = drsuapi.DsReplicaSyncRequest1() + req1.naming_context = nc; + req1.options = req_option + req1.source_dsa_guid = misc.GUID(source_dsa_guid) + + try: + drsuapiBind.DsReplicaSync(drsuapi_handle, 1, req1) + except Exception, estr: + raise drsException("DsReplicaSync failed %s" % estr) + def drs_DsBind(drs): '''make a DsBind call, returning the binding handle''' bind_info = drsuapi.DsBindInfoCtr() diff --git a/source4/scripting/python/samba/netcmd/drs.py b/source4/scripting/python/samba/netcmd/drs.py index 056d3ee5b4..c508e2f331 100644 --- a/source4/scripting/python/samba/netcmd/drs.py +++ b/source4/scripting/python/samba/netcmd/drs.py @@ -36,22 +36,13 @@ from samba import drs_utils, nttime2string, dsdb from samba.dcerpc import drsuapi, misc import common - - def drsuapi_connect(ctx): '''make a DRSUAPI connection to the server''' - binding_options = "seal" - if int(ctx.lp.get("log level")) >= 5: - binding_options += ",print" - binding_string = "ncacn_ip_tcp:%s[%s]" % (ctx.server, binding_options) try: - ctx.drsuapi = drsuapi.drsuapi(binding_string, ctx.lp, ctx.creds) - (ctx.drsuapi_handle, ctx.bind_supported_extensions) = drs_utils.drs_DsBind(ctx.drsuapi) + (ctx.drsuapi, ctx.drsuapi_handle, ctx.bind_supported_extensions) = drs_utils.drsuapi_connect(ctx.server, ctx.lp, ctx.creds) except Exception, e: raise CommandError("DRS connection to %s failed" % ctx.server, e) - - def samdb_connect(ctx): '''make a ldap connection to the server''' try: @@ -61,8 +52,6 @@ def samdb_connect(ctx): except Exception, e: raise CommandError("LDAP connection to %s failed" % ctx.server, e) - - def drs_errmsg(werr): '''return "was successful" or an error string''' (ecode, estring) = werr @@ -315,27 +304,22 @@ class cmd_drs_replicate(Command): source_dsa_guid = msg[0]['objectGUID'][0] dsa_options = int(attr_default(msg, 'options', 0)) - nc = drsuapi.DsReplicaObjectIdentifier() - nc.dn = NC - req1 = drsuapi.DsReplicaSyncRequest1() - req1.naming_context = nc; - req1.options = 0 + req_options = 0 if not (dsa_options & dsdb.DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL): - req1.options |= drsuapi.DRSUAPI_DRS_WRIT_REP + req_options |= drsuapi.DRSUAPI_DRS_WRIT_REP if add_ref: - req1.options |= drsuapi.DRSUAPI_DRS_ADD_REF + req_options |= drsuapi.DRSUAPI_DRS_ADD_REF if sync_forced: - req1.options |= drsuapi.DRSUAPI_DRS_SYNC_FORCED + req_options |= drsuapi.DRSUAPI_DRS_SYNC_FORCED if sync_all: - req1.options |= drsuapi.DRSUAPI_DRS_SYNC_ALL + req_options |= drsuapi.DRSUAPI_DRS_SYNC_ALL if full_sync: - req1.options |= drsuapi.DRSUAPI_DRS_FULL_SYNC_NOW - req1.source_dsa_guid = misc.GUID(source_dsa_guid) + req_options |= drsuapi.DRSUAPI_DRS_FULL_SYNC_NOW try: - self.drsuapi.DsReplicaSync(self.drsuapi_handle, 1, req1) - except Exception, estr: + drs_utils.sendDsReplicaSync(self.drsuapi, self.drsuapi_handle, source_dsa_guid, NC, req_options) + except drs_utils.drsException, estr: raise CommandError("DsReplicaSync failed", estr) self.message("Replicate from %s to %s was successful." % (SOURCE_DC, DEST_DC)) -- cgit