Debian Codegraph @

dirsrvtests/tests/suites/acl/repeated_ldap_add_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2020 Red Hat, Inc.
3 # All rights reserved.
4 #
5 # License: GPL (version 3 or any later version).
6 # See LICENSE for details.
7 # --- END COPYRIGHT BLOCK ---
8 #
9 from subprocess import Popen
10
11 import pytest
12 from lib389.paths import Paths
13 from lib389.tasks import *
14 from lib389.utils import *
15 from test389.topologies import topology_st
16
17 from lib389._constants import DN_DM, DEFAULT_SUFFIX, PASSWORD, SERVERID_STANDALONE
18
19 pytestmark = pytest.mark.tier1
20
21 logging.getLogger(__name__).setLevel(logging.DEBUG)
22 log = logging.getLogger(__name__)
23
24 CONFIG_DN = 'cn=config'
25 BOU = 'BOU'
26 BINDOU = 'ou=%s,%s' % (BOU, DEFAULT_SUFFIX)
27 BUID = 'buser123'
28 TUID = 'tuser0'
29 BINDDN = 'uid=%s,%s' % (BUID, BINDOU)
30 BINDPW = BUID
31 TESTDN = 'uid=%s,ou=people,%s' % (TUID, DEFAULT_SUFFIX)
32 TESTPW = TUID
33 BOGUSDN = 'uid=bogus,%s' % DEFAULT_SUFFIX
34 BOGUSDN2 = 'uid=bogus,ou=people,%s' % DEFAULT_SUFFIX
35 BOGUSSUFFIX = 'uid=bogus,ou=people,dc=bogus'
36 GROUPOU = 'ou=groups,%s' % DEFAULT_SUFFIX
37 BOGUSOU = 'ou=OU,%s' % DEFAULT_SUFFIX
38
39 def get_ldap_error_msg(e, type):
40 return e.args[0][type]
41
42 def pattern_accesslog(file, log_pattern):
43 for i in range(5):
44 try:
45 pattern_accesslog.last_pos += 1
46 except AttributeError:
47 pattern_accesslog.last_pos = 0
48
49 found = None
50 file.seek(pattern_accesslog.last_pos)
51
52 # Use a while true iteration because 'for line in file: hit a
53 # python bug that break file.tell()
54 while True:
55 line = file.readline()
56 found = log_pattern.search(line)
57 if ((line == '') or (found)):
58 break
59
60 pattern_accesslog.last_pos = file.tell()
61 if found:
62 return line
63 else:
64 time.sleep(1)
65 return None
66
67
68 def check_op_result(server, op, dn, superior, exists, rc):
69 targetdn = dn
70 if op == 'search':
71 if exists:
72 opstr = 'Searching existing entry'
73 else:
74 opstr = 'Searching non-existing entry'
75 elif op == 'add':
76 if exists:
77 opstr = 'Adding existing entry'
78 else:
79 opstr = 'Adding non-existing entry'
80 elif op == 'modify':
81 if exists:
82 opstr = 'Modifying existing entry'
83 else:
84 opstr = 'Modifying non-existing entry'
85 elif op == 'modrdn':
86 if superior is not None:
87 targetdn = superior
88 if exists:
89 opstr = 'Moving to existing superior'
90 else:
91 opstr = 'Moving to non-existing superior'
92 else:
93 if exists:
94 opstr = 'Renaming existing entry'
95 else:
96 opstr = 'Renaming non-existing entry'
97 elif op == 'delete':
98 if exists:
99 opstr = 'Deleting existing entry'
100 else:
101 opstr = 'Deleting non-existing entry'
102
103 if ldap.SUCCESS == rc:
104 expstr = 'be ok'
105 else:
106 expstr = 'fail with %s' % rc.__name__
107
108 log.info('%s %s, which should %s.' % (opstr, targetdn, expstr))
109 time.sleep(1)
110 hit = 0
111 try:
112 if op == 'search':
113 centry = server.search_s(dn, ldap.SCOPE_BASE, 'objectclass=*')
114 elif op == 'add':
115 server.add_s(Entry((dn, {'objectclass': 'top extensibleObject'.split(),
116 'cn': 'test entry'})))
117 elif op == 'modify':
118 server.modify_s(dn, [(ldap.MOD_REPLACE, 'description', b'test')])
119 elif op == 'modrdn':
120 if superior is not None:
121 server.rename_s(dn, 'uid=new', newsuperior=superior, delold=1)
122 else:
123 server.rename_s(dn, 'uid=new', delold=1)
124 elif op == 'delete':
125 server.delete_s(dn)
126 else:
127 log.fatal('Unknown operation %s' % op)
128 assert False
129 except ldap.LDAPError as e:
130 hit = 1
131 log.info("Exception (expected): %s" % type(e).__name__)
132 log.info('Desc {}'.format(get_ldap_error_msg(e,'desc')))
133 assert isinstance(e, rc)
134 if 'matched' in e.args:
135 log.info('Matched is returned: {}'.format(get_ldap_error_msg(e, 'matched')))
136 if rc != ldap.NO_SUCH_OBJECT:
137 assert False
138
139 if ldap.SUCCESS == rc:
140 if op == 'search':
141 log.info('Search should return none')
142 assert len(centry) == 0
143 else:
144 if 0 == hit:
145 log.info('Expected to fail with %s, but passed' % rc.__name__)
146 assert False
147
148 log.info('PASSED\n')
149
150
151 def test_repeated_ldap_add(topology_st):
152 """Prevent revealing the entry info to whom has no access rights.
153
154 :id: 76d278bd-3e51-4579-951a-753e6703b4df
155 :setup: Standalone instance
156 :steps:
157 1. Disable accesslog logbuffering
158 2. Bind as "cn=Directory Manager"
159 3. Add a organisational unit as BOU
160 4. Add a bind user as uid=buser123,ou=BOU,dc=example,dc=com
161 5. Add a test user as uid=tuser0,ou=People,dc=example,dc=com
162 6. Delete aci in dc=example,dc=com
163 7. Bind as Directory Manager, acquire an access log path and instance dir
164 8. Bind as uid=buser123,ou=BOU,dc=example,dc=com who has no right to read the entry
165 9. Bind as uid=bogus,ou=people,dc=bogus,bogus who does not exist
166 10. Bind as uid=buser123,ou=BOU,dc=example,dc=com,bogus with wrong password
167 11. Adding aci for uid=buser123,ou=BOU,dc=example,dc=com to ou=BOU,dc=example,dc=com.
168 12. Bind as uid=buser123,ou=BOU,dc=example,dc=com now who has right to read the entry
169 :expectedresults:
170 1. Operation should be successful
171 2. Operation should be successful
172 3. Operation should be successful
173 4. Operation should be successful
174 5. Operation should be successful
175 6. Operation should be successful
176 7. Operation should be successful
177 8. Bind operation should be successful with no search result
178 9. Bind operation should Fail
179 10. Bind operation should Fail
180 11. Operation should be successful
181 12. Bind operation should be successful with search result
182 """
183 log.info('Testing Bug 1347760 - Information disclosure via repeated use of LDAP ADD operation, etc.')
184
185 log.info('Disabling accesslog logbuffering')
186 topology_st.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE, 'nsslapd-accesslog-logbuffering', b'off')])
187
188 log.info('Bind as {%s,%s}' % (DN_DM, PASSWORD))
189 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
190
191 log.info('Adding ou=%s a bind user belongs to.' % BOU)
192 topology_st.standalone.add_s(Entry((BINDOU, {
193 'objectclass': 'top organizationalunit'.split(),
194 'ou': BOU})))
195
196 log.info('Adding a bind user.')
197 topology_st.standalone.add_s(Entry((BINDDN,
198 {'objectclass': "top person organizationalPerson inetOrgPerson".split(),
199 'cn': 'bind user',
200 'sn': 'user',
201 'userPassword': BINDPW})))
202
203 log.info('Adding a test user.')
204 topology_st.standalone.add_s(Entry((TESTDN,
205 {'objectclass': "top person organizationalPerson inetOrgPerson".split(),
206 'cn': 'test user',
207 'sn': 'user',
208 'userPassword': TESTPW})))
209
210 log.info('Deleting aci in %s.' % DEFAULT_SUFFIX)
211 topology_st.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_DELETE, 'aci', None)])
212
213 log.info('While binding as DM, acquire an access log path and instance dir')
214 ds_paths = Paths(serverid=topology_st.standalone.serverid,
215 instance=topology_st.standalone)
216 file_path = ds_paths.access_log
217 inst_dir = ds_paths.inst_dir
218
219 log.info('Bind case 1. the bind user has no rights to read the entry itself, bind should be successful.')
220 log.info('Bind as {%s,%s} who has no access rights.' % (BINDDN, BINDPW))
221 try:
222 topology_st.standalone.simple_bind_s(BINDDN, BINDPW)
223 except ldap.LDAPError as e:
224 log.info('Desc {}'.format(get_ldap_error_msg(e,'desc')))
225 assert False
226
227 file_obj = open(file_path, "r")
228 log.info('Access log path: %s' % file_path)
229
230 log.info(
231 'Bind case 2-1. the bind user does not exist, bind should fail with error %s' % ldap.INVALID_CREDENTIALS.__name__)
232 log.info('Bind as {%s,%s} who does not exist.' % (BOGUSDN, 'bogus'))
233 try:
234 topology_st.standalone.simple_bind_s(BOGUSDN, 'bogus')
235 except ldap.LDAPError as e:
236 log.info("Exception (expected): %s" % type(e).__name__)
237 log.info('Desc {}'.format(get_ldap_error_msg(e,'desc')))
238 assert isinstance(e, ldap.INVALID_CREDENTIALS)
239 regex = re.compile('No such entry')
240 cause = pattern_accesslog(file_obj, regex)
241 if cause is None:
242 log.fatal('Cause not found - %s' % cause)
243 assert False
244 else:
245 log.info('Cause found - %s' % cause)
246 time.sleep(1)
247
248 log.info(
249 'Bind case 2-2. the bind user\'s suffix does not exist, bind should fail with error %s' % ldap.INVALID_CREDENTIALS.__name__)
250 log.info('Bind as {%s,%s} who does not exist.' % (BOGUSSUFFIX, 'bogus'))
251 with pytest.raises(ldap.INVALID_CREDENTIALS):
252 topology_st.standalone.simple_bind_s(BOGUSSUFFIX, 'bogus')
253 regex = re.compile('No suffix for bind')
254 cause = pattern_accesslog(file_obj, regex)
255 if cause is None:
256 log.fatal('Cause not found - %s' % cause)
257 assert False
258 else:
259 log.info('Cause found - %s' % cause)
260 time.sleep(1)
261
262 log.info(
263 'Bind case 2-3. the bind user\'s password is wrong, bind should fail with error %s' % ldap.INVALID_CREDENTIALS.__name__)
264 log.info('Bind as {%s,%s} who does not exist.' % (BINDDN, 'bogus'))
265 try:
266 topology_st.standalone.simple_bind_s(BINDDN, 'bogus')
267 except ldap.LDAPError as e:
268 log.info("Exception (expected): %s" % type(e).__name__)
269 log.info('Desc {}'.format(get_ldap_error_msg(e,'desc')))
270 assert isinstance(e, ldap.INVALID_CREDENTIALS)
271 regex = re.compile('Invalid credentials')
272 cause = pattern_accesslog(file_obj, regex)
273 if cause is None:
274 log.fatal('Cause not found - %s' % cause)
275 assert False
276 else:
277 log.info('Cause found - %s' % cause)
278 time.sleep(1)
279
280 log.info('Adding aci for %s to %s.' % (BINDDN, BINDOU))
281 acival = '(targetattr="*")(version 3.0; acl "%s"; allow(all) userdn = "ldap:///%s";)' % (BUID, BINDDN)
282 log.info('aci: %s' % acival)
283 log.info('Bind as {%s,%s}' % (DN_DM, PASSWORD))
284 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
285 topology_st.standalone.modify_s(BINDOU, [(ldap.MOD_ADD, 'aci', ensure_bytes(acival))])
286 time.sleep(1)
287
288 log.info('Bind case 3. the bind user has the right to read the entry itself, bind should be successful.')
289 log.info('Bind as {%s,%s} which should be ok.\n' % (BINDDN, BINDPW))
290 topology_st.standalone.simple_bind_s(BINDDN, BINDPW)
291
292 log.info('The following operations are against the subtree the bind user %s has no rights.' % BINDDN)
293 # Search
294 exists = True
295 rc = ldap.SUCCESS
296 log.info(
297 'Search case 1. the bind user has no rights to read the search entry, it should return no search results with %s' % rc)
298 check_op_result(topology_st.standalone, 'search', TESTDN, None, exists, rc)
299
300 exists = False
301 rc = ldap.SUCCESS
302 log.info(
303 'Search case 2-1. the search entry does not exist, the search should return no search results with %s' % rc.__name__)
304 check_op_result(topology_st.standalone, 'search', BOGUSDN, None, exists, rc)
305
306 exists = False
307 rc = ldap.SUCCESS
308 log.info(
309 'Search case 2-2. the search entry does not exist, the search should return no search results with %s' % rc.__name__)
310 check_op_result(topology_st.standalone, 'search', BOGUSDN2, None, exists, rc)
311
312 # Add
313 exists = True
314 rc = ldap.INSUFFICIENT_ACCESS
315 log.info(
316 'Add case 1. the bind user has no rights AND the adding entry exists, it should fail with %s' % rc.__name__)
317 check_op_result(topology_st.standalone, 'add', TESTDN, None, exists, rc)
318
319 exists = False
320 rc = ldap.INSUFFICIENT_ACCESS
321 log.info(
322 'Add case 2-1. the bind user has no rights AND the adding entry does not exist, it should fail with %s' % rc.__name__)
323 check_op_result(topology_st.standalone, 'add', BOGUSDN, None, exists, rc)
324
325 exists = False
326 rc = ldap.INSUFFICIENT_ACCESS
327 log.info(
328 'Add case 2-2. the bind user has no rights AND the adding entry does not exist, it should fail with %s' % rc.__name__)
329 check_op_result(topology_st.standalone, 'add', BOGUSDN2, None, exists, rc)
330
331 # Modify
332 exists = True
333 rc = ldap.INSUFFICIENT_ACCESS
334 log.info(
335 'Modify case 1. the bind user has no rights AND the modifying entry exists, it should fail with %s' % rc.__name__)
336 check_op_result(topology_st.standalone, 'modify', TESTDN, None, exists, rc)
337
338 exists = False
339 rc = ldap.INSUFFICIENT_ACCESS
340 log.info(
341 'Modify case 2-1. the bind user has no rights AND the modifying entry does not exist, it should fail with %s' % rc.__name__)
342 check_op_result(topology_st.standalone, 'modify', BOGUSDN, None, exists, rc)
343
344 exists = False
345 rc = ldap.INSUFFICIENT_ACCESS
346 log.info(
347 'Modify case 2-2. the bind user has no rights AND the modifying entry does not exist, it should fail with %s' % rc.__name__)
348 check_op_result(topology_st.standalone, 'modify', BOGUSDN2, None, exists, rc)
349
350 # Modrdn
351 exists = True
352 rc = ldap.INSUFFICIENT_ACCESS
353 log.info(
354 'Modrdn case 1. the bind user has no rights AND the renaming entry exists, it should fail with %s' % rc.__name__)
355 check_op_result(topology_st.standalone, 'modrdn', TESTDN, None, exists, rc)
356
357 exists = False
358 rc = ldap.INSUFFICIENT_ACCESS
359 log.info(
360 'Modrdn case 2-1. the bind user has no rights AND the renaming entry does not exist, it should fail with %s' % rc.__name__)
361 check_op_result(topology_st.standalone, 'modrdn', BOGUSDN, None, exists, rc)
362
363 exists = False
364 rc = ldap.INSUFFICIENT_ACCESS
365 log.info(
366 'Modrdn case 2-2. the bind user has no rights AND the renaming entry does not exist, it should fail with %s' % rc.__name__)
367 check_op_result(topology_st.standalone, 'modrdn', BOGUSDN2, None, exists, rc)
368
369 exists = True
370 rc = ldap.INSUFFICIENT_ACCESS
371 log.info(
372 'Modrdn case 3. the bind user has no rights AND the node moving an entry to exists, it should fail with %s' % rc.__name__)
373 check_op_result(topology_st.standalone, 'modrdn', TESTDN, GROUPOU, exists, rc)
374
375 exists = False
376 rc = ldap.INSUFFICIENT_ACCESS
377 log.info(
378 'Modrdn case 4-1. the bind user has no rights AND the node moving an entry to does not, it should fail with %s' % rc.__name__)
379 check_op_result(topology_st.standalone, 'modrdn', TESTDN, BOGUSOU, exists, rc)
380
381 exists = False
382 rc = ldap.INSUFFICIENT_ACCESS
383 log.info(
384 'Modrdn case 4-2. the bind user has no rights AND the node moving an entry to does not, it should fail with %s' % rc.__name__)
385 check_op_result(topology_st.standalone, 'modrdn', TESTDN, BOGUSOU, exists, rc)
386
387 # Delete
388 exists = True
389 rc = ldap.INSUFFICIENT_ACCESS
390 log.info(
391 'Delete case 1. the bind user has no rights AND the deleting entry exists, it should fail with %s' % rc.__name__)
392 check_op_result(topology_st.standalone, 'delete', TESTDN, None, exists, rc)
393
394 exists = False
395 rc = ldap.INSUFFICIENT_ACCESS
396 log.info(
397 'Delete case 2-1. the bind user has no rights AND the deleting entry does not exist, it should fail with %s' % rc.__name__)
398 check_op_result(topology_st.standalone, 'delete', BOGUSDN, None, exists, rc)
399
400 exists = False
401 rc = ldap.INSUFFICIENT_ACCESS
402 log.info(
403 'Delete case 2-2. the bind user has no rights AND the deleting entry does not exist, it should fail with %s' % rc.__name__)
404 check_op_result(topology_st.standalone, 'delete', BOGUSDN2, None, exists, rc)
405
406 log.info('EXTRA: Check no regressions')
407 log.info('Adding aci for %s to %s.' % (BINDDN, DEFAULT_SUFFIX))
408 acival = '(targetattr="*")(version 3.0; acl "%s-all"; allow(all) userdn = "ldap:///%s";)' % (BUID, BINDDN)
409 log.info('Bind as {%s,%s}' % (DN_DM, PASSWORD))
410 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
411 topology_st.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_ADD, 'aci', ensure_bytes(acival))])
412 time.sleep(1)
413
414 log.info('Bind as {%s,%s}.' % (BINDDN, BINDPW))
415 try:
416 topology_st.standalone.simple_bind_s(BINDDN, BINDPW)
417 except ldap.LDAPError as e:
418 log.info('Desc {}'.format(get_ldap_error_msg(e,'desc')))
419 assert False
420 time.sleep(1)
421
422 exists = False
423 rc = ldap.NO_SUCH_OBJECT
424 log.info('Search case. the search entry does not exist, the search should fail with %s' % rc.__name__)
425 check_op_result(topology_st.standalone, 'search', BOGUSDN2, None, exists, rc)
426 file_obj.close()
427
428 exists = True
429 rc = ldap.ALREADY_EXISTS
430 log.info('Add case. the adding entry already exists, it should fail with %s' % rc.__name__)
431 check_op_result(topology_st.standalone, 'add', TESTDN, None, exists, rc)
432
433 exists = False
434 rc = ldap.NO_SUCH_OBJECT
435 log.info('Modify case. the modifying entry does not exist, it should fail with %s' % rc.__name__)
436 check_op_result(topology_st.standalone, 'modify', BOGUSDN, None, exists, rc)
437
438 exists = False
439 rc = ldap.NO_SUCH_OBJECT
440 log.info('Modrdn case 1. the renaming entry does not exist, it should fail with %s' % rc.__name__)
441 check_op_result(topology_st.standalone, 'modrdn', BOGUSDN, None, exists, rc)
442
443 exists = False
444 rc = ldap.NO_SUCH_OBJECT
445 log.info('Modrdn case 2. the node moving an entry to does not, it should fail with %s' % rc.__name__)
446 check_op_result(topology_st.standalone, 'modrdn', TESTDN, BOGUSOU, exists, rc)
447
448 exists = False
449 rc = ldap.NO_SUCH_OBJECT
450 log.info('Delete case. the deleting entry does not exist, it should fail with %s' % rc.__name__)
451 check_op_result(topology_st.standalone, 'delete', BOGUSDN, None, exists, rc)
452
453 log.info('Inactivate %s' % BINDDN)
454 if ds_paths.version < '1.3':
455 nsinactivate = '%s/ns-inactivate.pl' % inst_dir
456 cli_cmd = [nsinactivate, '-D', DN_DM, '-w', PASSWORD, '-I', BINDDN]
457 else:
458 dsidm = '%s/dsidm' % ds_paths.sbin_dir
459 cli_cmd = [dsidm, SERVERID_STANDALONE, '-b', DEFAULT_SUFFIX, 'account', 'lock', BINDDN]
460 log.info(cli_cmd)
461 p = Popen(cli_cmd)
462 assert (p.wait() == 0)
463
464 log.info('Bind as {%s,%s} which should fail with %s.' % (BINDDN, BUID, ldap.UNWILLING_TO_PERFORM.__name__))
465 try:
466 topology_st.standalone.simple_bind_s(BINDDN, BUID)
467 except ldap.LDAPError as e:
468 log.info("Exception (expected): %s" % type(e).__name__)
469 log.info('Desc {}'.format(get_ldap_error_msg(e,'desc')))
470 assert isinstance(e, ldap.UNWILLING_TO_PERFORM)
471
472 log.info('Bind as {%s,%s} which should fail with %s.' % (BINDDN, 'bogus', ldap.UNWILLING_TO_PERFORM.__name__))
473 try:
474 topology_st.standalone.simple_bind_s(BINDDN, 'bogus')
475 except ldap.LDAPError as e:
476 log.info("Exception (expected): %s" % type(e).__name__)
477 log.info('Desc {}'.format(get_ldap_error_msg(e,'desc')))
478 assert isinstance(e, ldap.UNWILLING_TO_PERFORM)
479
480 log.info('SUCCESS')
481
482
483 if __name__ == '__main__':
484 # Run isolated
485 # -s for DEBUG mode
486 CURRENT_FILE = os.path.realpath(__file__)
487 pytest.main("-s %s" % CURRENT_FILE)
488