1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
#!/usr/bin/env python3
#
# Test large LDAP response behaviour in Samba
# Copyright (C) Andrew Bartlett 2019
#
# Based on Unit tests for the notification control
# Copyright (C) Stefan Metzmacher 2016
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import optparse
import sys
import os
import random
sys.path.insert(0, "bin/python")
import samba
from samba.tests.subunitrun import SubunitOptions, TestProgram
import samba.getopt as options
from samba.auth import system_session
from samba import ldb
from samba.samdb import SamDB
from samba.ndr import ndr_unpack
from samba import gensec
from samba.credentials import Credentials
import samba.tests
from ldb import SCOPE_SUBTREE, SCOPE_ONELEVEL, SCOPE_BASE, LdbError
from ldb import ERR_TIME_LIMIT_EXCEEDED, ERR_ADMIN_LIMIT_EXCEEDED, ERR_UNWILLING_TO_PERFORM
from ldb import Message
parser = optparse.OptionParser("large_ldap.py [options] <host>")
sambaopts = options.SambaOptions(parser)
parser.add_option_group(sambaopts)
parser.add_option_group(options.VersionOptions(parser))
# use command line creds if available
credopts = options.CredentialsOptions(parser)
parser.add_option_group(credopts)
subunitopts = SubunitOptions(parser)
parser.add_option_group(subunitopts)
opts, args = parser.parse_args()
if len(args) < 1:
parser.print_usage()
sys.exit(1)
url = args[0]
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
class ManyLDAPTest(samba.tests.TestCase):
def setUp(self):
super(ManyLDAPTest, self).setUp()
self.ldb = SamDB(url, credentials=creds, session_info=system_session(lp), lp=lp)
self.base_dn = self.ldb.domain_dn()
self.OU_NAME_MANY="many_ou" + format(random.randint(0, 99999), "05")
self.ou_dn = ldb.Dn(self.ldb, "ou=" + self.OU_NAME_MANY + "," + str(self.base_dn))
samba.tests.delete_force(self.ldb, self.ou_dn,
controls=['tree_delete:1'])
self.ldb.add({
"dn": self.ou_dn,
"objectclass": "organizationalUnit",
"ou": self.OU_NAME_MANY})
for x in range(2000):
ou_name = self.OU_NAME_MANY + str(x)
self.ldb.add({
"dn": "ou=" + ou_name + "," + str(self.ou_dn),
"objectclass": "organizationalUnit",
"ou": ou_name})
def tearDown(self):
samba.tests.delete_force(self.ldb, self.ou_dn,
controls=['tree_delete:1'])
def test_unindexed_iterator_search(self):
"""Testing a search for all the OUs.
Needed to test that more that IOV_MAX responses can be returned
"""
if not url.startswith("ldap"):
self.fail(msg="This test is only valid on ldap")
count = 0
msg1 = None
search1 = self.ldb.search_iterator(base=self.ou_dn,
expression="(ou=" + self.OU_NAME_MANY + "*)",
scope=ldb.SCOPE_SUBTREE,
attrs=["objectGUID", "samAccountName"])
for reply in search1:
self.assertIsInstance(reply, ldb.Message)
count += 1
res1 = search1.result()
# Check we got everything
self.assertEqual(count, 2001)
class LargeLDAPTest(samba.tests.TestCase):
def setUp(self):
super(LargeLDAPTest, self).setUp()
self.ldb = SamDB(url, credentials=creds, session_info=system_session(lp), lp=lp)
self.base_dn = self.ldb.domain_dn()
self.USER_NAME = "large_user" + format(random.randint(0, 99999), "05") + "-"
self.OU_NAME="large_user_ou" + format(random.randint(0, 99999), "05")
self.ou_dn = ldb.Dn(self.ldb, "ou=" + self.OU_NAME + "," + str(self.base_dn))
samba.tests.delete_force(self.ldb, self.ou_dn,
controls=['tree_delete:1'])
self.ldb.add({
"dn": self.ou_dn,
"objectclass": "organizationalUnit",
"ou": self.OU_NAME})
for x in range(200):
user_name = self.USER_NAME + format(x, "03")
self.ldb.add({
"dn": "cn=" + user_name + "," + str(self.ou_dn),
"objectclass": "user",
"sAMAccountName": user_name,
"jpegPhoto": b'a' * (2 * 1024 * 1024)})
def tearDown(self):
# Remake the connection for tear-down (old Samba drops the socket)
self.ldb = SamDB(url, credentials=creds, session_info=system_session(lp), lp=lp)
samba.tests.delete_force(self.ldb, self.ou_dn,
controls=['tree_delete:1'])
def test_unindexed_iterator_search(self):
"""Testing an unindexed search that will break the result size limit"""
if not url.startswith("ldap"):
self.fail(msg="This test is only valid on ldap")
count = 0
msg1 = None
search1 = self.ldb.search_iterator(base=self.ou_dn,
expression="(sAMAccountName=" + self.USER_NAME + "*)",
scope=ldb.SCOPE_SUBTREE,
attrs=["objectGUID", "samAccountName"])
for reply in search1:
self.assertIsInstance(reply, ldb.Message)
count += 1
res1 = search1.result()
self.assertEqual(count, 200)
# Now try breaking the 256MB limit
count_jpeg = 0
msg1 = None
search1 = self.ldb.search_iterator(base=self.ou_dn,
expression="(sAMAccountName=" + self.USER_NAME + "*)",
scope=ldb.SCOPE_SUBTREE,
attrs=["objectGUID", "samAccountName", "jpegPhoto"])
try:
for reply in search1:
self.assertIsInstance(reply, ldb.Message)
msg1 = reply
count_jpeg += 1
except LdbError as e:
enum = err.args[0]
self.assertEqual(enum, ldb.ERR_SIZE_LIMIT_EXCEEDED)
# Assert we don't get all the entries but still the error
self.assertGreater(count, count_jpeg)
# Now try for just 100MB (server will do some chunking for this)
count_jpeg2 = 0
msg1 = None
try:
search1 = self.ldb.search_iterator(base=self.ou_dn,
expression="(sAMAccountName=" + self.USER_NAME + "1*)",
scope=ldb.SCOPE_SUBTREE,
attrs=["objectGUID", "samAccountName", "jpegPhoto"])
except LdbError as e:
enum = e.args[0]
estr = e.args[1]
self.fail(estr)
for reply in search1:
self.assertIsInstance(reply, ldb.Message)
msg1 = reply
count_jpeg2 += 1
# Assert we got some entries
self.assertEqual(count_jpeg2, 100)
def test_iterator_search(self):
"""Testing an indexed search that will break the result size limit"""
if not url.startswith("ldap"):
self.fail(msg="This test is only valid on ldap")
count = 0
msg1 = None
search1 = self.ldb.search_iterator(base=self.ou_dn,
expression="(&(objectClass=user)(sAMAccountName=" + self.USER_NAME + "*))",
scope=ldb.SCOPE_SUBTREE,
attrs=["objectGUID", "samAccountName"])
for reply in search1:
self.assertIsInstance(reply, ldb.Message)
count += 1
res1 = search1.result()
# Now try breaking the 256MB limit
count_jpeg = 0
msg1 = None
search1 = self.ldb.search_iterator(base=self.ou_dn,
expression="(&(objectClass=user)(sAMAccountName=" + self.USER_NAME + "*))",
scope=ldb.SCOPE_SUBTREE,
attrs=["objectGUID", "samAccountName", "jpegPhoto"])
try:
for reply in search1:
self.assertIsInstance(reply, ldb.Message)
count_jpeg =+ 1
except LdbError as e:
enum = err.args[0]
self.assertEqual(enum, ldb.ERR_SIZE_LIMIT_EXCEEDED)
# Assert we don't get all the entries but still the error
self.assertGreater(count, count_jpeg)
if "://" not in url:
if os.path.isfile(url):
url = "tdb://%s" % url
else:
url = "ldap://%s" % url
TestProgram(module=__name__, opts=subunitopts)
|