8 sys
.path
.insert(0, "bin/python")
10 import samba
.getopt
as options
11 from samba
.dcerpc
import drsblobs
, misc
12 from samba
.ndr
import ndr_pack
, ndr_unpack
15 parser
= optparse
.OptionParser("get-descriptor [options]")
16 sambaopts
= options
.SambaOptions(parser
)
17 credopts
= options
.CredentialsOptions(parser
)
18 parser
.add_option_group(credopts
)
20 parser
.add_option("-b", type="string", metavar
="BASE",
21 help="set base DN for the search")
22 parser
.add_option("--host", type="string", metavar
="HOST",
23 help="Ip of the host")
25 lp
= sambaopts
.get_loadparm()
26 creds
= credopts
.get_credentials(lp
)
28 opts
= parser
.parse_args()[0]
30 def printdirsync(ctl
):
32 if arr
[0] == 'dirsync':
33 print "Need to continue: %s" % arr
[1]
34 cookie
= ndr_unpack(drsblobs
.ldapControlDirSyncCookie
, base64
.b64decode(arr
[3]))
35 print "DC's NTDS guid: %s " % cookie
.blob
.guid1
36 print "highest usn %s" % cookie
.blob
.highwatermark
.highest_usn
37 print "tmp higest usn %s" % cookie
.blob
.highwatermark
.tmp_highest_usn
38 print "reserved usn %s" % cookie
.blob
.highwatermark
.reserved_usn
39 if cookie
.blob
.extra_length
>0:
40 print "highest usn in extra %s" % cookie
.blob
.extra
.ctr
.cursors
[0].highest_usn
43 remote_ldb
= Ldb("ldap://" + opts
.host
+ ":389", credentials
=creds
, lp
=lp
)
51 (msgs
, ctrls
) = remote_ldb
.search(expression
="(samaccountname=administrator)", base
=base
, attrs
=["objectClass"], controls
=["dirsync:1:1:50"])
55 if arr
[0] == 'dirsync':
56 cookie
= ndr_unpack(drsblobs
.ldapControlDirSyncCookie
, base64
.b64decode(arr
[3]))
57 guid
= cookie
.blob
.guid1
60 print "No dirsync control ... strange"
64 print "Getting first guest without any cookie"
65 (msgs
, ctrls
) = remote_ldb
.searchex(expression
="(samaccountname=guest)", base
=base
, attrs
=["objectClass"], controls
=["dirsync:1:1:50"])
69 cookie
= printdirsync(ctl
)
70 print "Returned %d entries" % len(msgs
)
75 print "Getting allusers with cookie"
76 controls
=["dirsync:1:1:50:%s" % base64
.b64encode(ndr_pack(cookie
))]
77 (msgs
, ctrls
) = remote_ldb
.searchex(expression
="(samaccountname=*)", base
=base
, attrs
=["objectClass"], controls
=controls
)
80 cookie
= printdirsync(ctl
)
81 print "Returned %d entries" % len(msgs
)
84 cookie
.blob
.guid1
= misc
.GUID("128a99bf-e2df-4832-ac0a-1fb625e530db")
85 if cookie
.blob
.extra_length
> 0:
86 cookie
.blob
.extra
.ctr
.cursors
[0].source_dsa_invocation_id
= misc
.GUID("128a99bf-e2df-4832-ac0a-1fb625e530db")
89 print "Getting all the entries"
90 controls
=["dirsync:1:1:50:%s" % base64
.b64encode(ndr_pack(cookie
))]
91 (msgs
, ctrls
) = remote_ldb
.searchex(expression
="(objectclass=*)", base
=base
, controls
=controls
)
95 cookie
= printdirsync(ctl
)
97 cont
= (ctl
.split(':'))[1]
98 print "Returned %d entries" % len(msgs
)
100 usn
= cookie
.blob
.highwatermark
.tmp_highest_usn
101 if cookie
.blob
.extra_length
> 0:
102 bigusn
= cookie
.blob
.extra
.ctr
.cursors
[0].highest_usn
107 controls
=["dirsync:1:1:50:%s" % base64
.b64encode(ndr_pack(cookie
))]
108 (msgs
, ctrls
) = remote_ldb
.searchex(expression
="(objectclass=*)", base
=base
, controls
=controls
)
111 cookie
= printdirsync(ctl
)
113 cont
= (ctl
.split(':'))[1]
114 print "Returned %d entries" % len(msgs
)
117 print "Getting with cookie but usn changed to %d we should use the one in extra" % (bigusn
- 1)
118 cookie
.blob
.highwatermark
.highest_usn
= 0
119 cookie
.blob
.highwatermark
.tmp_highest_usn
= usn
- 2
120 if cookie
.blob
.extra_length
> 0:
122 cookie
.blob
.extra
.ctr
.cursors
[0].highest_usn
= bigusn
- 1
123 controls
=["dirsync:1:1:50:%s" % base64
.b64encode(ndr_pack(cookie
))]
124 (msgs
, ctrls
) = remote_ldb
.searchex(expression
="(objectclass=*)", base
=base
, controls
=controls
)
127 cookie
= printdirsync(ctl
)
128 print "Returned %d entries" % len(msgs
)
131 print "Getting with cookie but usn %d changed and extra/cursor GUID too" % (usn
- 2)
132 print " so that it's (tmp)highest_usn that drives the limit"
133 cookie
.blob
.highwatermark
.highest_usn
= 0
134 cookie
.blob
.highwatermark
.tmp_highest_usn
= usn
- 2
135 if cookie
.blob
.extra_length
> 0:
136 cookie
.blob
.extra
.ctr
.cursors
[0].source_dsa_invocation_id
= misc
.GUID("128a99bf-e2df-4832-ac0a-1fb625e530db")
137 cookie
.blob
.extra
.ctr
.cursors
[0].highest_usn
= bigusn
- 1
138 controls
=["dirsync:1:1:50:%s" % base64
.b64encode(ndr_pack(cookie
))]
139 (msgs
, ctrls
) = remote_ldb
.searchex(expression
="(objectclass=*)", base
=base
, controls
=controls
)
142 cookie
= printdirsync(ctl
)
143 print "Returned %d entries" % len(msgs
)
146 print "Getting with cookie but usn changed to %d" % (usn
- 2)
147 cookie
.blob
.highwatermark
.highest_usn
= 0
148 cookie
.blob
.highwatermark
.tmp_highest_usn
= (usn
- 2)
149 if cookie
.blob
.extra_length
> 0:
150 cookie
.blob
.extra
.ctr
.cursors
[0].highest_usn
= (usn
- 2)
151 controls
=["dirsync:1:1:50:%s" % base64
.b64encode(ndr_pack(cookie
))]
152 (msgs
, ctrls
) = remote_ldb
.searchex(expression
="(objectclass=*)", base
=base
, controls
=controls
)
155 cookie
= printdirsync(ctl
)
156 print "Returned %d entries" % len(msgs
)