pyldb: avoid segfault when adding an element with no name
[sfrench/samba-autobuild/.git] / source4 / scripting / devel / getncchanges
1 #!/usr/bin/env python3
2
3 # script to call a DRS GetNCChanges from the command line
4 # this is useful for plugfest testing
5 from __future__ import print_function
6 import sys
7 from optparse import OptionParser
8
9 sys.path.insert(0, "bin/python")
10
11 import samba, ldb
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 from samba.ndr import ndr_unpack
17 from samba.drs_utils import drs_get_rodc_partial_attribute_set, drs_DsBind
18
19
20 ########### main code ###########
21 if __name__ == "__main__":
22     parser = OptionParser("getncchanges [options] server")
23     sambaopts = options.SambaOptions(parser)
24     parser.add_option_group(sambaopts)
25     credopts = options.CredentialsOptionsDouble(parser)
26     parser.add_option_group(credopts)
27
28     parser.add_option("", "--dn", dest="dn", help="DN to replicate",)
29     parser.add_option("", "--exop", dest="exop", help="extended operation",)
30     parser.add_option("", "--pas", dest="use_pas", action='store_true', default=False,
31                       help="send partial attribute set (for RODC)")
32     parser.add_option("", "--nb-iter", type='int', help="Number of getncchange iterations")
33     parser.add_option("", "--dest-dsa", type='str', help="destination DSA GUID")
34     parser.add_option("", "--rodc", action='store_true', default=False,
35                       help='use RODC replica flags')
36     parser.add_option("", "--partial-rw", action='store_true', default=False,
37                       help='use RW partial replica flags, not be confused with --pas')
38     parser.add_option("", "--replica-flags", type='int',
39                       default=drsuapi.DRSUAPI_DRS_INIT_SYNC |
40                       drsuapi.DRSUAPI_DRS_PER_SYNC |
41                       drsuapi.DRSUAPI_DRS_WRIT_REP |
42                       drsuapi.DRSUAPI_DRS_GET_ANC |
43                       drsuapi.DRSUAPI_DRS_NEVER_SYNCED,
44                       help='replica flags')
45
46     (opts, args) = parser.parse_args()
47     if opts.rodc:
48         opts.replica_flags = drsuapi.DRSUAPI_DRS_INIT_SYNC |\
49                              drsuapi.DRSUAPI_DRS_PER_SYNC |\
50                              drsuapi.DRSUAPI_DRS_GET_ANC |\
51                              drsuapi.DRSUAPI_DRS_NEVER_SYNCED |\
52                              drsuapi.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING |\
53                              drsuapi.DRSUAPI_DRS_GET_ALL_GROUP_MEMBERSHIP
54
55     if opts.partial_rw:
56         opts.replica_flags = drsuapi.DRSUAPI_DRS_INIT_SYNC |\
57                              drsuapi.DRSUAPI_DRS_PER_SYNC |\
58                              drsuapi.DRSUAPI_DRS_GET_ANC |\
59                              drsuapi.DRSUAPI_DRS_NEVER_SYNCED
60
61     lp = sambaopts.get_loadparm()
62     creds = credopts.get_credentials(lp)
63
64     if len(args) != 1:
65         parser.error("You must supply a server")
66
67     if creds.is_anonymous():
68         parser.error("You must supply credentials")
69
70     if opts.partial_rw and opts.rodc:
71         parser.error("Can't specify --partial-rw and --rodc")
72
73     server = args[0]
74
75     binding_str = "ncacn_ip_tcp:%s[seal,print]" % server
76
77     drs = drsuapi.drsuapi(binding_str, lp, creds)
78     drs_handle, supported_extensions = drs_DsBind(drs)
79     print("DRS Handle: %s" % drs_handle)
80
81     req8 = drsuapi.DsGetNCChangesRequest8()
82
83     samdb = SamDB(url="ldap://%s" % server,
84                   session_info=system_session(),
85                   credentials=creds, lp=lp)
86
87     if opts.use_pas:
88         local_samdb = SamDB(url=None, session_info=system_session(),
89                             credentials=creds, lp=lp)
90
91     if opts.dn is None:
92         opts.dn = str(samdb.get_default_basedn())
93
94     if opts.exop is None:
95         exop = drsuapi.DRSUAPI_EXOP_NONE
96     else:
97         exop = int(opts.exop)
98
99     dest_dsa = opts.dest_dsa
100     if not dest_dsa:
101         print("no dest_dsa specified trying to figure out from ldap")
102         msgs = samdb.search(controls=["search_options:1:2"],
103                            expression='(objectclass=ntdsdsa)')
104         if len(msgs) == 1:
105             dest_dsa = str(ndr_unpack(misc.GUID,  msgs[0]["invocationId"][0]))
106             print("Found this dsa: %s" % dest_dsa)
107         else:
108             # TODO fixme
109             pass
110         if not dest_dsa:
111             print("Unable to find the dest_dsa automatically please specify it")
112             import sys
113             sys.exit(1)
114
115     null_guid = misc.GUID()
116     req8.destination_dsa_guid               = misc.GUID(dest_dsa)
117     req8.source_dsa_invocation_id           = misc.GUID(samdb.get_invocation_id())
118     req8.naming_context                     = drsuapi.DsReplicaObjectIdentifier()
119     req8.naming_context.dn                  = opts.dn.decode("utf-8")
120     req8.highwatermark                      = drsuapi.DsReplicaHighWaterMark()
121     req8.highwatermark.tmp_highest_usn      = 0
122     req8.highwatermark.reserved_usn         = 0
123     req8.highwatermark.highest_usn          = 0
124     req8.uptodateness_vector                = None
125     req8.replica_flags                      = opts.replica_flags
126     req8.max_object_count                    = 402
127     req8.max_ndr_size                        = 402116
128     req8.extended_op                         = exop
129     req8.fsmo_info                           = 0
130     if opts.use_pas:
131         req8.partial_attribute_set           = drs_get_rodc_partial_attribute_set(local_samdb)
132     else:
133         req8.partial_attribute_set           = None
134     req8.partial_attribute_set_ex            = None
135     req8.mapping_ctr.num_mappings            = 0
136     req8.mapping_ctr.mappings                = None
137
138     nb_iter = 0
139     while True:
140         (level, ctr) = drs.DsGetNCChanges(drs_handle, 8, req8)
141         nb_iter += 1
142         if ctr.more_data == 0 or opts.nb_iter == nb_iter:
143             break
144         req8.highwatermark = ctr.new_highwatermark