s4-devel: added a getncchanges developer script
[sfrench/samba-autobuild/.git] / source4 / scripting / devel / getncchanges
1 #!/usr/bin/env python
2
3 # script to call a DRS GetNCChanges from the command line
4 # this is useful for plugfest testing
5
6 import sys
7 from optparse import OptionParser
8
9 sys.path.insert(0, "bin/python")
10
11 import samba
12 import samba.getopt as options
13 from samba.dcerpc import drsuapi, misc
14 from samba.samdb import SamDB
15 from samba.auth import system_session
16
17 def do_DsBind(drs):
18     '''make a DsBind call, returning the binding handle'''
19     bind_info = drsuapi.DsBindInfoCtr()
20     bind_info.length = 28
21     bind_info.info = drsuapi.DsBindInfo28()
22     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_BASE
23     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION
24     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI
25     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2
26     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS
27     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1
28     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION
29     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE
30     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2
31     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION
32     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2
33     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD
34     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND
35     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO
36     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION
37     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01
38     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP
39     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY
40     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3
41     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2
42     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6
43     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS
44     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8
45     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5
46     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6
47     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3
48     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7
49     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT
50     (info, handle) = drs.DsBind(misc.GUID(drsuapi.DRSUAPI_DS_BIND_GUID), bind_info)
51     return handle
52
53 ########### main code ###########
54 if __name__ == "__main__":
55     parser = OptionParser("getncchanges [options] server")
56     sambaopts = options.SambaOptions(parser)
57     credopts = options.CredentialsOptionsDouble(parser)
58     parser.add_option_group(credopts)
59
60     parser.add_option("", "--dn", dest="dn", help="DN to replicate",)
61     parser.add_option("", "--exop", dest="exop", help="extended operation",)
62
63     (opts, args) = parser.parse_args()
64
65     lp = sambaopts.get_loadparm()
66     creds = credopts.get_credentials(lp)
67
68     if len(args) != 1:
69         parser.error("You must supply a server")
70
71     if creds.is_anonymous():
72         parser.error("You must supply credentials")
73
74     server = args[0]
75
76     binding_str = "ncacn_ip_tcp:%s[seal,print]" % server
77
78     drs = drsuapi.drsuapi(binding_str, lp, creds)
79     drs_handle = do_DsBind(drs)
80     print "DRS Handle: %s" % drs_handle
81
82     req8 = drsuapi.DsGetNCChangesRequest8()
83
84     samdb = SamDB(url="ldap://%s" % server,
85                   session_info=system_session(),
86                   credentials=creds, lp=lp)
87
88     if opts.dn is None:
89         opts.dn = str(samdb.get_default_basedn())
90
91     if opts.exop is None:
92         exop = drsuapi.DRSUAPI_EXOP_NONE
93     else:
94         exop = int(opts.exop)
95
96     null_guid = misc.GUID()
97     req8.destination_dsa_guid               = misc.GUID("9c637462-5b8c-4467-aef2-bdb1f57bc4ef")
98     req8.source_dsa_invocation_id           = misc.GUID(samdb.get_invocation_id())
99     req8.naming_context                     = drsuapi.DsReplicaObjectIdentifier()
100     req8.naming_context.dn                  = opts.dn.decode("utf-8")
101     req8.highwatermark                      = drsuapi.DsReplicaHighWaterMark()
102     req8.highwatermark.tmp_highest_usn      = 0
103     req8.highwatermark.reserved_usn         = 0
104     req8.highwatermark.highest_usn          = 0
105     req8.uptodateness_vector                = None
106     req8.replica_flags                      = 0
107     req8.replica_flags                      |=  (drsuapi.DRSUAPI_DRS_INIT_SYNC |
108                                                  drsuapi.DRSUAPI_DRS_PER_SYNC |
109                                                  drsuapi.DRSUAPI_DRS_GET_ANC |
110                                                  drsuapi.DRSUAPI_DRS_NEVER_SYNCED)
111     req8.max_object_count                    = 402
112     req8.max_ndr_size                        = 402116
113     req8.extended_op                         = exop
114     req8.fsmo_info                           = 0
115     req8.partial_attribute_set               = None
116     req8.partial_attribute_set_ex            = None
117     req8.mapping_ctr.num_mappings            = 0
118     req8.mapping_ctr.mappings                = None
119
120     drs.DsGetNCChanges(drs_handle, 8, req8)